blob: 957870fccc88940be7f935fbfd674d716f503cfb [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
1315 length;
1316
cristybb503372010-05-27 20:51:26 +00001317 size_t
cristy3ed852e2009-09-05 21:47:34 +00001318 level;
1319
1320 p=expression;
1321 i=GetImageIndexInList(fx_info->images);
1322 level=0;
1323 point.x=(double) x;
1324 point.y=(double) y;
1325 if (isalpha((int) *(p+1)) == 0)
1326 {
1327 if (strchr("suv",(int) *p) != (char *) NULL)
1328 {
1329 switch (*p)
1330 {
1331 case 's':
1332 default:
1333 {
1334 i=GetImageIndexInList(fx_info->images);
1335 break;
1336 }
1337 case 'u': i=0; break;
1338 case 'v': i=1; break;
1339 }
1340 p++;
1341 if (*p == '[')
1342 {
1343 level++;
1344 q=subexpression;
1345 for (p++; *p != '\0'; )
1346 {
1347 if (*p == '[')
1348 level++;
1349 else
1350 if (*p == ']')
1351 {
1352 level--;
1353 if (level == 0)
1354 break;
1355 }
1356 *q++=(*p++);
1357 }
1358 *q='\0';
1359 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1360 &beta,exception);
cristybb503372010-05-27 20:51:26 +00001361 i=(ssize_t) (alpha+0.5);
cristy3ed852e2009-09-05 21:47:34 +00001362 p++;
1363 }
1364 if (*p == '.')
1365 p++;
1366 }
1367 if ((isalpha((int) *(p+1)) == 0) && (*p == 'p'))
1368 {
1369 p++;
1370 if (*p == '{')
1371 {
1372 level++;
1373 q=subexpression;
1374 for (p++; *p != '\0'; )
1375 {
1376 if (*p == '{')
1377 level++;
1378 else
1379 if (*p == '}')
1380 {
1381 level--;
1382 if (level == 0)
1383 break;
1384 }
1385 *q++=(*p++);
1386 }
1387 *q='\0';
1388 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1389 &beta,exception);
1390 point.x=alpha;
1391 point.y=beta;
1392 p++;
1393 }
1394 else
1395 if (*p == '[')
1396 {
1397 level++;
1398 q=subexpression;
1399 for (p++; *p != '\0'; )
1400 {
1401 if (*p == '[')
1402 level++;
1403 else
1404 if (*p == ']')
1405 {
1406 level--;
1407 if (level == 0)
1408 break;
1409 }
1410 *q++=(*p++);
1411 }
1412 *q='\0';
1413 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1414 &beta,exception);
1415 point.x+=alpha;
1416 point.y+=beta;
1417 p++;
1418 }
1419 if (*p == '.')
1420 p++;
1421 }
1422 }
1423 length=GetImageListLength(fx_info->images);
1424 while (i < 0)
cristybb503372010-05-27 20:51:26 +00001425 i+=(ssize_t) length;
cristy3ed852e2009-09-05 21:47:34 +00001426 i%=length;
1427 image=GetImageFromList(fx_info->images,i);
1428 if (image == (Image *) NULL)
1429 {
1430 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1431 "NoSuchImage","`%s'",expression);
1432 return(0.0);
1433 }
cristy4c08aed2011-07-01 19:47:50 +00001434 GetPixelInfo(image,&pixel);
1435 (void) InterpolatePixelInfo(image,fx_info->view[i],image->interpolate,
cristy4f820712011-04-01 12:35:43 +00001436 point.x,point.y,&pixel,exception);
cristy3ed852e2009-09-05 21:47:34 +00001437 if ((strlen(p) > 2) &&
1438 (LocaleCompare(p,"intensity") != 0) &&
1439 (LocaleCompare(p,"luminance") != 0) &&
1440 (LocaleCompare(p,"hue") != 0) &&
1441 (LocaleCompare(p,"saturation") != 0) &&
1442 (LocaleCompare(p,"lightness") != 0))
1443 {
1444 char
1445 name[MaxTextExtent];
1446
1447 (void) CopyMagickString(name,p,MaxTextExtent);
1448 for (q=name+(strlen(name)-1); q > name; q--)
1449 {
1450 if (*q == ')')
1451 break;
1452 if (*q == '.')
1453 {
1454 *q='\0';
1455 break;
1456 }
1457 }
1458 if ((strlen(name) > 2) &&
1459 (GetValueFromSplayTree(fx_info->symbols,name) == (const char *) NULL))
1460 {
cristy4c08aed2011-07-01 19:47:50 +00001461 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001462 *color;
1463
cristy4c08aed2011-07-01 19:47:50 +00001464 color=(PixelInfo *) GetValueFromSplayTree(fx_info->colors,name);
1465 if (color != (PixelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001466 {
1467 pixel=(*color);
1468 p+=strlen(name);
1469 }
1470 else
cristy269c9412011-10-13 23:41:15 +00001471 if (QueryColorCompliance(name,AllCompliance,&pixel,fx_info->exception) != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001472 {
1473 (void) AddValueToSplayTree(fx_info->colors,ConstantString(name),
cristy4c08aed2011-07-01 19:47:50 +00001474 ClonePixelInfo(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001475 p+=strlen(name);
1476 }
1477 }
1478 }
1479 (void) CopyMagickString(symbol,p,MaxTextExtent);
1480 StripString(symbol);
1481 if (*symbol == '\0')
1482 {
1483 switch (channel)
1484 {
cristy0568ffc2011-07-25 16:54:14 +00001485 case RedPixelChannel: return(QuantumScale*pixel.red);
1486 case GreenPixelChannel: return(QuantumScale*pixel.green);
1487 case BluePixelChannel: return(QuantumScale*pixel.blue);
1488 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001489 {
1490 if (image->colorspace != CMYKColorspace)
1491 {
1492 (void) ThrowMagickException(exception,GetMagickModule(),
cristy1a020e42011-12-06 18:13:23 +00001493 ImageError,"ColorSeparatedImageRequired","`%s'",
cristy3ed852e2009-09-05 21:47:34 +00001494 image->filename);
1495 return(0.0);
1496 }
cristy4c08aed2011-07-01 19:47:50 +00001497 return(QuantumScale*pixel.black);
1498 }
cristy0568ffc2011-07-25 16:54:14 +00001499 case AlphaPixelChannel:
cristy4c08aed2011-07-01 19:47:50 +00001500 {
1501 MagickRealType
1502 alpha;
1503
1504 if (pixel.matte == MagickFalse)
1505 return(1.0);
1506 alpha=(MagickRealType) (QuantumScale*pixel.alpha);
1507 return(alpha);
cristy3ed852e2009-09-05 21:47:34 +00001508 }
cristya382aca2011-12-06 18:22:48 +00001509 case IndexPixelChannel:
1510 return(0.0);
cristyb3a73b52011-07-26 01:34:43 +00001511 case IntensityPixelChannel:
cristyf364ed42010-12-15 01:54:43 +00001512 {
cristy4c08aed2011-07-01 19:47:50 +00001513 return(QuantumScale*GetPixelInfoIntensity(&pixel));
cristyf364ed42010-12-15 01:54:43 +00001514 }
cristy3ed852e2009-09-05 21:47:34 +00001515 default:
1516 break;
1517 }
1518 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1519 "UnableToParseExpression","`%s'",p);
1520 return(0.0);
1521 }
1522 switch (*symbol)
1523 {
1524 case 'A':
1525 case 'a':
1526 {
1527 if (LocaleCompare(symbol,"a") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001528 return((MagickRealType) (QuantumScale*pixel.alpha));
cristy3ed852e2009-09-05 21:47:34 +00001529 break;
1530 }
1531 case 'B':
1532 case 'b':
1533 {
1534 if (LocaleCompare(symbol,"b") == 0)
1535 return(QuantumScale*pixel.blue);
1536 break;
1537 }
1538 case 'C':
1539 case 'c':
1540 {
1541 if (LocaleNCompare(symbol,"channel",7) == 0)
1542 {
1543 GeometryInfo
1544 channel_info;
1545
1546 MagickStatusType
1547 flags;
1548
1549 flags=ParseGeometry(symbol+7,&channel_info);
1550 if (image->colorspace == CMYKColorspace)
1551 switch (channel)
1552 {
cristy0568ffc2011-07-25 16:54:14 +00001553 case CyanPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001554 {
1555 if ((flags & RhoValue) == 0)
1556 return(0.0);
1557 return(channel_info.rho);
1558 }
cristy0568ffc2011-07-25 16:54:14 +00001559 case MagentaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001560 {
1561 if ((flags & SigmaValue) == 0)
1562 return(0.0);
1563 return(channel_info.sigma);
1564 }
cristy0568ffc2011-07-25 16:54:14 +00001565 case YellowPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001566 {
1567 if ((flags & XiValue) == 0)
1568 return(0.0);
1569 return(channel_info.xi);
1570 }
cristy0568ffc2011-07-25 16:54:14 +00001571 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001572 {
1573 if ((flags & PsiValue) == 0)
1574 return(0.0);
1575 return(channel_info.psi);
1576 }
cristy0568ffc2011-07-25 16:54:14 +00001577 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001578 {
1579 if ((flags & ChiValue) == 0)
1580 return(0.0);
1581 return(channel_info.chi);
1582 }
1583 default:
1584 return(0.0);
1585 }
1586 switch (channel)
1587 {
cristy0568ffc2011-07-25 16:54:14 +00001588 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001589 {
1590 if ((flags & RhoValue) == 0)
1591 return(0.0);
1592 return(channel_info.rho);
1593 }
cristy0568ffc2011-07-25 16:54:14 +00001594 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001595 {
1596 if ((flags & SigmaValue) == 0)
1597 return(0.0);
1598 return(channel_info.sigma);
1599 }
cristy0568ffc2011-07-25 16:54:14 +00001600 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001601 {
1602 if ((flags & XiValue) == 0)
1603 return(0.0);
1604 return(channel_info.xi);
1605 }
cristy0568ffc2011-07-25 16:54:14 +00001606 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001607 {
1608 if ((flags & ChiValue) == 0)
1609 return(0.0);
1610 return(channel_info.chi);
1611 }
cristy0568ffc2011-07-25 16:54:14 +00001612 case AlphaPixelChannel:
cristy4c08aed2011-07-01 19:47:50 +00001613 {
1614 if ((flags & PsiValue) == 0)
1615 return(0.0);
1616 return(channel_info.psi);
1617 }
cristy3ed852e2009-09-05 21:47:34 +00001618 default:
1619 return(0.0);
1620 }
1621 return(0.0);
1622 }
1623 if (LocaleCompare(symbol,"c") == 0)
1624 return(QuantumScale*pixel.red);
1625 break;
1626 }
1627 case 'D':
1628 case 'd':
1629 {
1630 if (LocaleNCompare(symbol,"depth",5) == 0)
1631 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1632 break;
1633 }
1634 case 'G':
1635 case 'g':
1636 {
1637 if (LocaleCompare(symbol,"g") == 0)
1638 return(QuantumScale*pixel.green);
1639 break;
1640 }
1641 case 'K':
1642 case 'k':
1643 {
1644 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1645 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1646 if (LocaleCompare(symbol,"k") == 0)
1647 {
1648 if (image->colorspace != CMYKColorspace)
1649 {
1650 (void) ThrowMagickException(exception,GetMagickModule(),
1651 OptionError,"ColorSeparatedImageRequired","`%s'",
1652 image->filename);
1653 return(0.0);
1654 }
cristy4c08aed2011-07-01 19:47:50 +00001655 return(QuantumScale*pixel.black);
cristy3ed852e2009-09-05 21:47:34 +00001656 }
1657 break;
1658 }
1659 case 'H':
1660 case 'h':
1661 {
1662 if (LocaleCompare(symbol,"h") == 0)
1663 return((MagickRealType) image->rows);
1664 if (LocaleCompare(symbol,"hue") == 0)
1665 {
1666 double
1667 hue,
1668 lightness,
1669 saturation;
1670
cristyda1f9c12011-10-02 21:39:49 +00001671 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1672 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001673 return(hue);
1674 }
1675 break;
1676 }
1677 case 'I':
1678 case 'i':
1679 {
1680 if ((LocaleCompare(symbol,"image.depth") == 0) ||
1681 (LocaleCompare(symbol,"image.minima") == 0) ||
1682 (LocaleCompare(symbol,"image.maxima") == 0) ||
1683 (LocaleCompare(symbol,"image.mean") == 0) ||
1684 (LocaleCompare(symbol,"image.kurtosis") == 0) ||
1685 (LocaleCompare(symbol,"image.skewness") == 0) ||
1686 (LocaleCompare(symbol,"image.standard_deviation") == 0))
1687 return(FxChannelStatistics(fx_info,image,channel,symbol+6,exception));
1688 if (LocaleCompare(symbol,"image.resolution.x") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001689 return(image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00001690 if (LocaleCompare(symbol,"image.resolution.y") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001691 return(image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001692 if (LocaleCompare(symbol,"intensity") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001693 return(QuantumScale*GetPixelInfoIntensity(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001694 if (LocaleCompare(symbol,"i") == 0)
1695 return((MagickRealType) x);
1696 break;
1697 }
1698 case 'J':
1699 case 'j':
1700 {
1701 if (LocaleCompare(symbol,"j") == 0)
1702 return((MagickRealType) y);
1703 break;
1704 }
1705 case 'L':
1706 case 'l':
1707 {
1708 if (LocaleCompare(symbol,"lightness") == 0)
1709 {
1710 double
1711 hue,
1712 lightness,
1713 saturation;
1714
cristyda1f9c12011-10-02 21:39:49 +00001715 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1716 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001717 return(lightness);
1718 }
1719 if (LocaleCompare(symbol,"luminance") == 0)
1720 {
1721 double
1722 luminence;
1723
1724 luminence=0.2126*pixel.red+0.7152*pixel.green+0.0722*pixel.blue;
1725 return(QuantumScale*luminence);
1726 }
1727 break;
1728 }
1729 case 'M':
1730 case 'm':
1731 {
1732 if (LocaleNCompare(symbol,"maxima",6) == 0)
1733 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1734 if (LocaleNCompare(symbol,"mean",4) == 0)
1735 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1736 if (LocaleNCompare(symbol,"minima",6) == 0)
1737 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1738 if (LocaleCompare(symbol,"m") == 0)
1739 return(QuantumScale*pixel.blue);
1740 break;
1741 }
1742 case 'N':
1743 case 'n':
1744 {
1745 if (LocaleCompare(symbol,"n") == 0)
anthony374f5dd2011-03-25 10:08:53 +00001746 return((MagickRealType) GetImageListLength(fx_info->images));
cristy3ed852e2009-09-05 21:47:34 +00001747 break;
1748 }
1749 case 'O':
1750 case 'o':
1751 {
1752 if (LocaleCompare(symbol,"o") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001753 return(QuantumScale*pixel.alpha);
cristy3ed852e2009-09-05 21:47:34 +00001754 break;
1755 }
1756 case 'P':
1757 case 'p':
1758 {
1759 if (LocaleCompare(symbol,"page.height") == 0)
1760 return((MagickRealType) image->page.height);
1761 if (LocaleCompare(symbol,"page.width") == 0)
1762 return((MagickRealType) image->page.width);
1763 if (LocaleCompare(symbol,"page.x") == 0)
1764 return((MagickRealType) image->page.x);
1765 if (LocaleCompare(symbol,"page.y") == 0)
1766 return((MagickRealType) image->page.y);
1767 break;
1768 }
1769 case 'R':
1770 case 'r':
1771 {
1772 if (LocaleCompare(symbol,"resolution.x") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001773 return(image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00001774 if (LocaleCompare(symbol,"resolution.y") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001775 return(image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001776 if (LocaleCompare(symbol,"r") == 0)
1777 return(QuantumScale*pixel.red);
1778 break;
1779 }
1780 case 'S':
1781 case 's':
1782 {
1783 if (LocaleCompare(symbol,"saturation") == 0)
1784 {
1785 double
1786 hue,
1787 lightness,
1788 saturation;
1789
cristyda1f9c12011-10-02 21:39:49 +00001790 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1791 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001792 return(saturation);
1793 }
1794 if (LocaleNCompare(symbol,"skewness",8) == 0)
1795 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1796 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1797 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1798 break;
1799 }
1800 case 'T':
1801 case 't':
1802 {
1803 if (LocaleCompare(symbol,"t") == 0)
cristy5a15b932011-03-26 12:50:33 +00001804 return((MagickRealType) GetImageIndexInList(fx_info->images));
cristy3ed852e2009-09-05 21:47:34 +00001805 break;
1806 }
1807 case 'W':
1808 case 'w':
1809 {
1810 if (LocaleCompare(symbol,"w") == 0)
1811 return((MagickRealType) image->columns);
1812 break;
1813 }
1814 case 'Y':
1815 case 'y':
1816 {
1817 if (LocaleCompare(symbol,"y") == 0)
1818 return(QuantumScale*pixel.green);
1819 break;
1820 }
1821 case 'Z':
1822 case 'z':
1823 {
1824 if (LocaleCompare(symbol,"z") == 0)
1825 {
1826 MagickRealType
1827 depth;
1828
cristyfefab1b2011-07-05 00:33:22 +00001829 depth=(MagickRealType) GetImageDepth(image,fx_info->exception);
cristy3ed852e2009-09-05 21:47:34 +00001830 return(depth);
1831 }
1832 break;
1833 }
1834 default:
1835 break;
1836 }
1837 value=(const char *) GetValueFromSplayTree(fx_info->symbols,symbol);
1838 if (value != (const char *) NULL)
cristydbdd0e32011-11-04 23:29:40 +00001839 return((MagickRealType) StringToDouble(value,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001840 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1841 "UnableToParseExpression","`%s'",symbol);
1842 return(0.0);
1843}
1844
1845static const char *FxOperatorPrecedence(const char *expression,
1846 ExceptionInfo *exception)
1847{
1848 typedef enum
1849 {
1850 UndefinedPrecedence,
1851 NullPrecedence,
1852 BitwiseComplementPrecedence,
1853 ExponentPrecedence,
cristy116af162010-08-13 01:25:47 +00001854 ExponentialNotationPrecedence,
cristy3ed852e2009-09-05 21:47:34 +00001855 MultiplyPrecedence,
1856 AdditionPrecedence,
1857 ShiftPrecedence,
1858 RelationalPrecedence,
1859 EquivalencyPrecedence,
1860 BitwiseAndPrecedence,
1861 BitwiseOrPrecedence,
1862 LogicalAndPrecedence,
1863 LogicalOrPrecedence,
1864 TernaryPrecedence,
1865 AssignmentPrecedence,
1866 CommaPrecedence,
1867 SeparatorPrecedence
1868 } FxPrecedence;
1869
1870 FxPrecedence
1871 precedence,
1872 target;
1873
1874 register const char
1875 *subexpression;
1876
1877 register int
1878 c;
1879
cristybb503372010-05-27 20:51:26 +00001880 size_t
cristy3ed852e2009-09-05 21:47:34 +00001881 level;
1882
1883 c=0;
1884 level=0;
1885 subexpression=(const char *) NULL;
1886 target=NullPrecedence;
1887 while (*expression != '\0')
1888 {
1889 precedence=UndefinedPrecedence;
1890 if ((isspace((int) ((char) *expression)) != 0) || (c == (int) '@'))
1891 {
1892 expression++;
1893 continue;
1894 }
cristy488fa882010-03-01 22:34:24 +00001895 switch (*expression)
1896 {
1897 case 'A':
1898 case 'a':
cristy3ed852e2009-09-05 21:47:34 +00001899 {
cristyb33454f2011-08-03 02:10:45 +00001900#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00001901 if (LocaleNCompare(expression,"acosh",5) == 0)
1902 {
1903 expression+=5;
1904 break;
1905 }
cristyb33454f2011-08-03 02:10:45 +00001906#endif
1907#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00001908 if (LocaleNCompare(expression,"asinh",5) == 0)
1909 {
1910 expression+=5;
1911 break;
1912 }
cristyb33454f2011-08-03 02:10:45 +00001913#endif
1914#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00001915 if (LocaleNCompare(expression,"atanh",5) == 0)
cristy488fa882010-03-01 22:34:24 +00001916 {
1917 expression+=5;
1918 break;
1919 }
cristyb33454f2011-08-03 02:10:45 +00001920#endif
cristy488fa882010-03-01 22:34:24 +00001921 break;
cristy3ed852e2009-09-05 21:47:34 +00001922 }
cristy62d455f2011-11-03 11:42:28 +00001923 case 'E':
1924 case 'e':
1925 {
1926 if ((LocaleNCompare(expression,"E+",2) == 0) ||
1927 (LocaleNCompare(expression,"E-",2) == 0))
1928 {
1929 expression+=2; /* scientific notation */
1930 break;
1931 }
1932 }
cristy488fa882010-03-01 22:34:24 +00001933 case 'J':
1934 case 'j':
1935 {
1936 if ((LocaleNCompare(expression,"j0",2) == 0) ||
1937 (LocaleNCompare(expression,"j1",2) == 0))
1938 {
1939 expression+=2;
1940 break;
1941 }
1942 break;
1943 }
cristy2def9322010-06-18 23:59:37 +00001944 case '#':
1945 {
1946 while (isxdigit((int) ((unsigned char) *(expression+1))) != 0)
1947 expression++;
1948 break;
1949 }
cristy488fa882010-03-01 22:34:24 +00001950 default:
1951 break;
1952 }
cristy3ed852e2009-09-05 21:47:34 +00001953 if ((c == (int) '{') || (c == (int) '['))
1954 level++;
1955 else
1956 if ((c == (int) '}') || (c == (int) ']'))
1957 level--;
1958 if (level == 0)
1959 switch ((unsigned char) *expression)
1960 {
1961 case '~':
1962 case '!':
1963 {
1964 precedence=BitwiseComplementPrecedence;
1965 break;
1966 }
1967 case '^':
cristy6621e252010-08-13 00:42:57 +00001968 case '@':
cristy3ed852e2009-09-05 21:47:34 +00001969 {
1970 precedence=ExponentPrecedence;
1971 break;
1972 }
1973 default:
1974 {
1975 if (((c != 0) && ((isdigit((int) ((char) c)) != 0) ||
1976 (strchr(")",c) != (char *) NULL))) &&
1977 (((islower((int) ((char) *expression)) != 0) ||
1978 (strchr("(",(int) *expression) != (char *) NULL)) ||
1979 ((isdigit((int) ((char) c)) == 0) &&
1980 (isdigit((int) ((char) *expression)) != 0))) &&
1981 (strchr("xy",(int) *expression) == (char *) NULL))
1982 precedence=MultiplyPrecedence;
1983 break;
1984 }
1985 case '*':
1986 case '/':
1987 case '%':
1988 {
1989 precedence=MultiplyPrecedence;
1990 break;
1991 }
1992 case '+':
1993 case '-':
1994 {
1995 if ((strchr("(+-/*%:&^|<>~,",c) == (char *) NULL) ||
1996 (isalpha(c) != 0))
1997 precedence=AdditionPrecedence;
1998 break;
1999 }
2000 case LeftShiftOperator:
2001 case RightShiftOperator:
2002 {
2003 precedence=ShiftPrecedence;
2004 break;
2005 }
2006 case '<':
2007 case LessThanEqualOperator:
2008 case GreaterThanEqualOperator:
2009 case '>':
2010 {
2011 precedence=RelationalPrecedence;
2012 break;
2013 }
2014 case EqualOperator:
2015 case NotEqualOperator:
2016 {
2017 precedence=EquivalencyPrecedence;
2018 break;
2019 }
2020 case '&':
2021 {
2022 precedence=BitwiseAndPrecedence;
2023 break;
2024 }
2025 case '|':
2026 {
2027 precedence=BitwiseOrPrecedence;
2028 break;
2029 }
2030 case LogicalAndOperator:
2031 {
2032 precedence=LogicalAndPrecedence;
2033 break;
2034 }
2035 case LogicalOrOperator:
2036 {
2037 precedence=LogicalOrPrecedence;
2038 break;
2039 }
cristy116af162010-08-13 01:25:47 +00002040 case ExponentialNotation:
2041 {
2042 precedence=ExponentialNotationPrecedence;
2043 break;
2044 }
cristy3ed852e2009-09-05 21:47:34 +00002045 case ':':
2046 case '?':
2047 {
2048 precedence=TernaryPrecedence;
2049 break;
2050 }
2051 case '=':
2052 {
2053 precedence=AssignmentPrecedence;
2054 break;
2055 }
2056 case ',':
2057 {
2058 precedence=CommaPrecedence;
2059 break;
2060 }
2061 case ';':
2062 {
2063 precedence=SeparatorPrecedence;
2064 break;
2065 }
2066 }
2067 if ((precedence == BitwiseComplementPrecedence) ||
2068 (precedence == TernaryPrecedence) ||
2069 (precedence == AssignmentPrecedence))
2070 {
2071 if (precedence > target)
2072 {
2073 /*
2074 Right-to-left associativity.
2075 */
2076 target=precedence;
2077 subexpression=expression;
2078 }
2079 }
2080 else
2081 if (precedence >= target)
2082 {
2083 /*
2084 Left-to-right associativity.
2085 */
2086 target=precedence;
2087 subexpression=expression;
2088 }
2089 if (strchr("(",(int) *expression) != (char *) NULL)
2090 expression=FxSubexpression(expression,exception);
2091 c=(int) (*expression++);
2092 }
2093 return(subexpression);
2094}
2095
2096static MagickRealType FxEvaluateSubexpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002097 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristye85007d2010-06-06 22:51:36 +00002098 const char *expression,MagickRealType *beta,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002099{
2100 char
2101 *q,
2102 subexpression[MaxTextExtent];
2103
2104 MagickRealType
2105 alpha,
2106 gamma;
2107
2108 register const char
2109 *p;
2110
2111 *beta=0.0;
2112 if (exception->severity != UndefinedException)
2113 return(0.0);
2114 while (isspace((int) *expression) != 0)
2115 expression++;
2116 if (*expression == '\0')
2117 {
2118 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
2119 "MissingExpression","`%s'",expression);
2120 return(0.0);
2121 }
cristy66322f02010-05-17 11:40:48 +00002122 *subexpression='\0';
cristy3ed852e2009-09-05 21:47:34 +00002123 p=FxOperatorPrecedence(expression,exception);
2124 if (p != (const char *) NULL)
2125 {
2126 (void) CopyMagickString(subexpression,expression,(size_t)
2127 (p-expression+1));
2128 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2129 exception);
2130 switch ((unsigned char) *p)
2131 {
2132 case '~':
2133 {
2134 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristybb503372010-05-27 20:51:26 +00002135 *beta=(MagickRealType) (~(size_t) *beta);
cristy3ed852e2009-09-05 21:47:34 +00002136 return(*beta);
2137 }
2138 case '!':
2139 {
2140 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2141 return(*beta == 0.0 ? 1.0 : 0.0);
2142 }
2143 case '^':
2144 {
2145 *beta=pow((double) alpha,(double) FxEvaluateSubexpression(fx_info,
2146 channel,x,y,++p,beta,exception));
2147 return(*beta);
2148 }
2149 case '*':
cristy116af162010-08-13 01:25:47 +00002150 case ExponentialNotation:
cristy3ed852e2009-09-05 21:47:34 +00002151 {
2152 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2153 return(alpha*(*beta));
2154 }
2155 case '/':
2156 {
2157 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2158 if (*beta == 0.0)
2159 {
2160 if (exception->severity == UndefinedException)
2161 (void) ThrowMagickException(exception,GetMagickModule(),
2162 OptionError,"DivideByZero","`%s'",expression);
2163 return(0.0);
2164 }
2165 return(alpha/(*beta));
2166 }
2167 case '%':
2168 {
2169 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2170 *beta=fabs(floor(((double) *beta)+0.5));
2171 if (*beta == 0.0)
2172 {
2173 (void) ThrowMagickException(exception,GetMagickModule(),
2174 OptionError,"DivideByZero","`%s'",expression);
2175 return(0.0);
2176 }
2177 return(fmod((double) alpha,(double) *beta));
2178 }
2179 case '+':
2180 {
2181 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2182 return(alpha+(*beta));
2183 }
2184 case '-':
2185 {
2186 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2187 return(alpha-(*beta));
2188 }
2189 case LeftShiftOperator:
2190 {
2191 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristybb503372010-05-27 20:51:26 +00002192 *beta=(MagickRealType) ((size_t) (alpha+0.5) << (size_t)
cristy3ed852e2009-09-05 21:47:34 +00002193 (gamma+0.5));
2194 return(*beta);
2195 }
2196 case RightShiftOperator:
2197 {
2198 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristybb503372010-05-27 20:51:26 +00002199 *beta=(MagickRealType) ((size_t) (alpha+0.5) >> (size_t)
cristy3ed852e2009-09-05 21:47:34 +00002200 (gamma+0.5));
2201 return(*beta);
2202 }
2203 case '<':
2204 {
2205 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2206 return(alpha < *beta ? 1.0 : 0.0);
2207 }
2208 case LessThanEqualOperator:
2209 {
2210 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2211 return(alpha <= *beta ? 1.0 : 0.0);
2212 }
2213 case '>':
2214 {
2215 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2216 return(alpha > *beta ? 1.0 : 0.0);
2217 }
2218 case GreaterThanEqualOperator:
2219 {
2220 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2221 return(alpha >= *beta ? 1.0 : 0.0);
2222 }
2223 case EqualOperator:
2224 {
2225 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2226 return(fabs(alpha-(*beta)) <= MagickEpsilon ? 1.0 : 0.0);
2227 }
2228 case NotEqualOperator:
2229 {
2230 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2231 return(fabs(alpha-(*beta)) > MagickEpsilon ? 1.0 : 0.0);
2232 }
2233 case '&':
2234 {
2235 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristybb503372010-05-27 20:51:26 +00002236 *beta=(MagickRealType) ((size_t) (alpha+0.5) & (size_t)
cristy3ed852e2009-09-05 21:47:34 +00002237 (gamma+0.5));
2238 return(*beta);
2239 }
2240 case '|':
2241 {
2242 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristybb503372010-05-27 20:51:26 +00002243 *beta=(MagickRealType) ((size_t) (alpha+0.5) | (size_t)
cristy3ed852e2009-09-05 21:47:34 +00002244 (gamma+0.5));
2245 return(*beta);
2246 }
2247 case LogicalAndOperator:
2248 {
2249 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2250 *beta=(alpha > 0.0) && (gamma > 0.0) ? 1.0 : 0.0;
2251 return(*beta);
2252 }
2253 case LogicalOrOperator:
2254 {
2255 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2256 *beta=(alpha > 0.0) || (gamma > 0.0) ? 1.0 : 0.0;
2257 return(*beta);
2258 }
2259 case '?':
2260 {
2261 MagickRealType
2262 gamma;
2263
2264 (void) CopyMagickString(subexpression,++p,MaxTextExtent);
2265 q=subexpression;
2266 p=StringToken(":",&q);
2267 if (q == (char *) NULL)
2268 {
2269 (void) ThrowMagickException(exception,GetMagickModule(),
2270 OptionError,"UnableToParseExpression","`%s'",subexpression);
2271 return(0.0);
2272 }
2273 if (fabs((double) alpha) > MagickEpsilon)
2274 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,exception);
2275 else
2276 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,q,beta,exception);
2277 return(gamma);
2278 }
2279 case '=':
2280 {
2281 char
2282 numeric[MaxTextExtent];
2283
2284 q=subexpression;
2285 while (isalpha((int) ((unsigned char) *q)) != 0)
2286 q++;
2287 if (*q != '\0')
2288 {
2289 (void) ThrowMagickException(exception,GetMagickModule(),
2290 OptionError,"UnableToParseExpression","`%s'",subexpression);
2291 return(0.0);
2292 }
2293 ClearMagickException(exception);
2294 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristyb51dff52011-05-19 16:55:47 +00002295 (void) FormatLocaleString(numeric,MaxTextExtent,"%g",(double)
cristy8cd5b312010-01-07 01:10:24 +00002296 *beta);
cristy3ed852e2009-09-05 21:47:34 +00002297 (void) DeleteNodeFromSplayTree(fx_info->symbols,subexpression);
2298 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(
2299 subexpression),ConstantString(numeric));
2300 return(*beta);
2301 }
2302 case ',':
2303 {
2304 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2305 return(alpha);
2306 }
2307 case ';':
2308 {
2309 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2310 return(*beta);
2311 }
2312 default:
2313 {
2314 gamma=alpha*FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,
2315 exception);
2316 return(gamma);
2317 }
2318 }
2319 }
2320 if (strchr("(",(int) *expression) != (char *) NULL)
2321 {
2322 (void) CopyMagickString(subexpression,expression+1,MaxTextExtent);
2323 subexpression[strlen(subexpression)-1]='\0';
2324 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2325 exception);
2326 return(gamma);
2327 }
cristy8b8a3ae2010-10-23 18:49:46 +00002328 switch (*expression)
cristy3ed852e2009-09-05 21:47:34 +00002329 {
2330 case '+':
2331 {
2332 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2333 exception);
2334 return(1.0*gamma);
2335 }
2336 case '-':
2337 {
2338 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2339 exception);
2340 return(-1.0*gamma);
2341 }
2342 case '~':
2343 {
2344 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2345 exception);
cristybb503372010-05-27 20:51:26 +00002346 return((MagickRealType) (~(size_t) (gamma+0.5)));
cristy3ed852e2009-09-05 21:47:34 +00002347 }
2348 case 'A':
2349 case 'a':
2350 {
2351 if (LocaleNCompare(expression,"abs",3) == 0)
2352 {
2353 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2354 exception);
2355 return((MagickRealType) fabs((double) alpha));
2356 }
cristyb33454f2011-08-03 02:10:45 +00002357#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00002358 if (LocaleNCompare(expression,"acosh",5) == 0)
2359 {
2360 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2361 exception);
2362 return((MagickRealType) acosh((double) alpha));
2363 }
cristyb33454f2011-08-03 02:10:45 +00002364#endif
cristy3ed852e2009-09-05 21:47:34 +00002365 if (LocaleNCompare(expression,"acos",4) == 0)
2366 {
2367 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2368 exception);
2369 return((MagickRealType) acos((double) alpha));
2370 }
cristy43c22f42010-03-30 12:34:07 +00002371#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002372 if (LocaleNCompare(expression,"airy",4) == 0)
2373 {
2374 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2375 exception);
2376 if (alpha == 0.0)
cristy2dd03222010-03-30 22:12:11 +00002377 return(1.0);
2378 gamma=2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha);
cristy43c22f42010-03-30 12:34:07 +00002379 return(gamma*gamma);
cristyee56cf12010-03-01 22:17:06 +00002380 }
cristy43c22f42010-03-30 12:34:07 +00002381#endif
cristyb33454f2011-08-03 02:10:45 +00002382#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00002383 if (LocaleNCompare(expression,"asinh",5) == 0)
2384 {
2385 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2386 exception);
2387 return((MagickRealType) asinh((double) alpha));
2388 }
cristyb33454f2011-08-03 02:10:45 +00002389#endif
cristy3ed852e2009-09-05 21:47:34 +00002390 if (LocaleNCompare(expression,"asin",4) == 0)
2391 {
2392 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2393 exception);
2394 return((MagickRealType) asin((double) alpha));
2395 }
2396 if (LocaleNCompare(expression,"alt",3) == 0)
2397 {
2398 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2399 exception);
cristybb503372010-05-27 20:51:26 +00002400 return(((ssize_t) alpha) & 0x01 ? -1.0 : 1.0);
cristy3ed852e2009-09-05 21:47:34 +00002401 }
2402 if (LocaleNCompare(expression,"atan2",5) == 0)
2403 {
2404 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2405 exception);
2406 return((MagickRealType) atan2((double) alpha,(double) *beta));
2407 }
cristyb33454f2011-08-03 02:10:45 +00002408#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00002409 if (LocaleNCompare(expression,"atanh",5) == 0)
2410 {
2411 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2412 exception);
2413 return((MagickRealType) atanh((double) alpha));
2414 }
cristyb33454f2011-08-03 02:10:45 +00002415#endif
cristy3ed852e2009-09-05 21:47:34 +00002416 if (LocaleNCompare(expression,"atan",4) == 0)
2417 {
2418 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2419 exception);
2420 return((MagickRealType) atan((double) alpha));
2421 }
2422 if (LocaleCompare(expression,"a") == 0)
2423 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2424 break;
2425 }
2426 case 'B':
2427 case 'b':
2428 {
2429 if (LocaleCompare(expression,"b") == 0)
2430 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2431 break;
2432 }
2433 case 'C':
2434 case 'c':
2435 {
2436 if (LocaleNCompare(expression,"ceil",4) == 0)
2437 {
2438 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2439 exception);
2440 return((MagickRealType) ceil((double) alpha));
2441 }
2442 if (LocaleNCompare(expression,"cosh",4) == 0)
2443 {
2444 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2445 exception);
2446 return((MagickRealType) cosh((double) alpha));
2447 }
2448 if (LocaleNCompare(expression,"cos",3) == 0)
2449 {
2450 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2451 exception);
2452 return((MagickRealType) cos((double) alpha));
2453 }
2454 if (LocaleCompare(expression,"c") == 0)
2455 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2456 break;
2457 }
2458 case 'D':
2459 case 'd':
2460 {
2461 if (LocaleNCompare(expression,"debug",5) == 0)
2462 {
2463 const char
2464 *type;
2465
2466 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2467 exception);
2468 if (fx_info->images->colorspace == CMYKColorspace)
2469 switch (channel)
2470 {
cristy0568ffc2011-07-25 16:54:14 +00002471 case CyanPixelChannel: type="cyan"; break;
2472 case MagentaPixelChannel: type="magenta"; break;
2473 case YellowPixelChannel: type="yellow"; break;
2474 case AlphaPixelChannel: type="opacity"; break;
2475 case BlackPixelChannel: type="black"; break;
cristy3ed852e2009-09-05 21:47:34 +00002476 default: type="unknown"; break;
2477 }
2478 else
2479 switch (channel)
2480 {
cristy0568ffc2011-07-25 16:54:14 +00002481 case RedPixelChannel: type="red"; break;
2482 case GreenPixelChannel: type="green"; break;
2483 case BluePixelChannel: type="blue"; break;
2484 case AlphaPixelChannel: type="opacity"; break;
cristy3ed852e2009-09-05 21:47:34 +00002485 default: type="unknown"; break;
2486 }
2487 (void) CopyMagickString(subexpression,expression+6,MaxTextExtent);
2488 if (strlen(subexpression) > 1)
2489 subexpression[strlen(subexpression)-1]='\0';
2490 if (fx_info->file != (FILE *) NULL)
cristy1e604812011-05-19 18:07:50 +00002491 (void) FormatLocaleFile(fx_info->file,
2492 "%s[%.20g,%.20g].%s: %s=%.*g\n",fx_info->images->filename,
2493 (double) x,(double) y,type,subexpression,GetMagickPrecision(),
2494 (double) alpha);
cristy3ed852e2009-09-05 21:47:34 +00002495 return(0.0);
2496 }
cristy5597a8d2011-11-04 00:25:32 +00002497 if (LocaleNCompare(expression,"drc",3) == 0)
2498 {
2499 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2500 exception);
2501 return((MagickRealType) (alpha/(*beta*(alpha-1.0)+1.0)));
2502 }
cristy3ed852e2009-09-05 21:47:34 +00002503 break;
2504 }
2505 case 'E':
2506 case 'e':
2507 {
2508 if (LocaleCompare(expression,"epsilon") == 0)
2509 return((MagickRealType) MagickEpsilon);
2510 if (LocaleNCompare(expression,"exp",3) == 0)
2511 {
2512 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2513 exception);
2514 return((MagickRealType) exp((double) alpha));
2515 }
2516 if (LocaleCompare(expression,"e") == 0)
2517 return((MagickRealType) 2.7182818284590452354);
2518 break;
2519 }
2520 case 'F':
2521 case 'f':
2522 {
2523 if (LocaleNCompare(expression,"floor",5) == 0)
2524 {
2525 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2526 exception);
2527 return((MagickRealType) floor((double) alpha));
2528 }
2529 break;
2530 }
2531 case 'G':
2532 case 'g':
2533 {
cristy9eeedea2011-11-02 19:04:05 +00002534 if (LocaleNCompare(expression,"gauss",5) == 0)
2535 {
2536 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2537 exception);
2538 gamma=exp((double) (-alpha*alpha/2.0))/sqrt(2.0*MagickPI);
2539 return((MagickRealType) gamma);
2540 }
cristyb0aad4c2011-11-02 19:30:35 +00002541 if (LocaleNCompare(expression,"gcd",3) == 0)
2542 {
2543 MagickOffsetType
2544 gcd;
2545
2546 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2547 exception);
cristy76461162011-11-02 19:32:19 +00002548 gcd=FxGCD((MagickOffsetType) (alpha+0.5),(MagickOffsetType)
2549 (*beta+0.5));
cristyb0aad4c2011-11-02 19:30:35 +00002550 return((MagickRealType) gcd);
2551 }
cristy3ed852e2009-09-05 21:47:34 +00002552 if (LocaleCompare(expression,"g") == 0)
2553 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2554 break;
2555 }
2556 case 'H':
2557 case 'h':
2558 {
2559 if (LocaleCompare(expression,"h") == 0)
2560 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2561 if (LocaleCompare(expression,"hue") == 0)
2562 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2563 if (LocaleNCompare(expression,"hypot",5) == 0)
2564 {
2565 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2566 exception);
2567 return((MagickRealType) hypot((double) alpha,(double) *beta));
2568 }
2569 break;
2570 }
2571 case 'K':
2572 case 'k':
2573 {
2574 if (LocaleCompare(expression,"k") == 0)
2575 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2576 break;
2577 }
2578 case 'I':
2579 case 'i':
2580 {
2581 if (LocaleCompare(expression,"intensity") == 0)
2582 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2583 if (LocaleNCompare(expression,"int",3) == 0)
2584 {
2585 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2586 exception);
cristy16788e42010-08-13 13:44:26 +00002587 return((MagickRealType) floor(alpha));
cristy3ed852e2009-09-05 21:47:34 +00002588 }
cristy82b20722011-11-05 21:52:36 +00002589#if defined(MAGICKCORE_HAVE_ISNAN)
cristy639399c2011-11-02 19:16:15 +00002590 if (LocaleNCompare(expression,"isnan",5) == 0)
2591 {
2592 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2593 exception);
cristy17a10202011-11-02 19:17:04 +00002594 return((MagickRealType) !!isnan((double) alpha));
cristy639399c2011-11-02 19:16:15 +00002595 }
cristy82b20722011-11-05 21:52:36 +00002596#endif
cristy3ed852e2009-09-05 21:47:34 +00002597 if (LocaleCompare(expression,"i") == 0)
2598 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2599 break;
2600 }
2601 case 'J':
2602 case 'j':
2603 {
2604 if (LocaleCompare(expression,"j") == 0)
2605 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
cristy161b9262010-03-20 19:34:32 +00002606#if defined(MAGICKCORE_HAVE_J0)
cristyee56cf12010-03-01 22:17:06 +00002607 if (LocaleNCompare(expression,"j0",2) == 0)
2608 {
2609 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2610 exception);
2611 return((MagickRealType) j0((double) alpha));
2612 }
cristy161b9262010-03-20 19:34:32 +00002613#endif
2614#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002615 if (LocaleNCompare(expression,"j1",2) == 0)
2616 {
2617 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2618 exception);
2619 return((MagickRealType) j1((double) alpha));
2620 }
cristy161b9262010-03-20 19:34:32 +00002621#endif
cristyaa018fa2010-04-08 23:03:54 +00002622#if defined(MAGICKCORE_HAVE_J1)
cristya6a09e72010-03-02 14:51:02 +00002623 if (LocaleNCompare(expression,"jinc",4) == 0)
2624 {
2625 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2626 exception);
cristy0946a822010-03-12 17:14:58 +00002627 if (alpha == 0.0)
2628 return(1.0);
cristy69928f92010-03-12 13:27:09 +00002629 gamma=(MagickRealType) (2.0*j1((double) (MagickPI*alpha))/
cristyfce2f7b2010-03-12 00:29:49 +00002630 (MagickPI*alpha));
2631 return(gamma);
cristya6a09e72010-03-02 14:51:02 +00002632 }
cristyaa018fa2010-04-08 23:03:54 +00002633#endif
cristy3ed852e2009-09-05 21:47:34 +00002634 break;
2635 }
2636 case 'L':
2637 case 'l':
2638 {
2639 if (LocaleNCompare(expression,"ln",2) == 0)
2640 {
2641 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2642 exception);
2643 return((MagickRealType) log((double) alpha));
2644 }
cristyc8ed5322010-08-31 12:07:59 +00002645 if (LocaleNCompare(expression,"logtwo",6) == 0)
cristy3ed852e2009-09-05 21:47:34 +00002646 {
cristyc8ed5322010-08-31 12:07:59 +00002647 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
cristy3ed852e2009-09-05 21:47:34 +00002648 exception);
2649 return((MagickRealType) log10((double) alpha))/log10(2.0);
2650 }
2651 if (LocaleNCompare(expression,"log",3) == 0)
2652 {
2653 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2654 exception);
2655 return((MagickRealType) log10((double) alpha));
2656 }
2657 if (LocaleCompare(expression,"lightness") == 0)
2658 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2659 break;
2660 }
2661 case 'M':
2662 case 'm':
2663 {
2664 if (LocaleCompare(expression,"MaxRGB") == 0)
2665 return((MagickRealType) QuantumRange);
2666 if (LocaleNCompare(expression,"maxima",6) == 0)
2667 break;
2668 if (LocaleNCompare(expression,"max",3) == 0)
cristy984049c2011-11-03 18:34:58 +00002669 {
2670 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2671 exception);
2672 return(alpha > *beta ? alpha : *beta);
2673 }
cristy3ed852e2009-09-05 21:47:34 +00002674 if (LocaleNCompare(expression,"minima",6) == 0)
2675 break;
2676 if (LocaleNCompare(expression,"min",3) == 0)
cristy984049c2011-11-03 18:34:58 +00002677 {
2678 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2679 exception);
2680 return(alpha < *beta ? alpha : *beta);
2681 }
cristy3ed852e2009-09-05 21:47:34 +00002682 if (LocaleNCompare(expression,"mod",3) == 0)
2683 {
2684 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2685 exception);
cristy984049c2011-11-03 18:34:58 +00002686 gamma=alpha-floor((double) (alpha/(*beta)))*(*beta);
2687 return(gamma);
cristy3ed852e2009-09-05 21:47:34 +00002688 }
2689 if (LocaleCompare(expression,"m") == 0)
2690 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2691 break;
2692 }
2693 case 'N':
2694 case 'n':
2695 {
cristyad3502e2011-11-02 19:10:45 +00002696 if (LocaleNCompare(expression,"not",3) == 0)
2697 {
2698 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2699 exception);
2700 return((MagickRealType) (alpha < MagickEpsilon));
2701 }
cristy3ed852e2009-09-05 21:47:34 +00002702 if (LocaleCompare(expression,"n") == 0)
2703 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2704 break;
2705 }
2706 case 'O':
2707 case 'o':
2708 {
2709 if (LocaleCompare(expression,"Opaque") == 0)
2710 return(1.0);
2711 if (LocaleCompare(expression,"o") == 0)
2712 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2713 break;
2714 }
2715 case 'P':
2716 case 'p':
2717 {
cristy670aa3c2011-11-03 00:54:00 +00002718 if (LocaleCompare(expression,"phi") == 0)
2719 return((MagickRealType) MagickPHI);
cristy3ed852e2009-09-05 21:47:34 +00002720 if (LocaleCompare(expression,"pi") == 0)
2721 return((MagickRealType) MagickPI);
2722 if (LocaleNCompare(expression,"pow",3) == 0)
2723 {
2724 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2725 exception);
2726 return((MagickRealType) pow((double) alpha,(double) *beta));
2727 }
2728 if (LocaleCompare(expression,"p") == 0)
2729 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2730 break;
2731 }
2732 case 'Q':
2733 case 'q':
2734 {
2735 if (LocaleCompare(expression,"QuantumRange") == 0)
2736 return((MagickRealType) QuantumRange);
2737 if (LocaleCompare(expression,"QuantumScale") == 0)
2738 return((MagickRealType) QuantumScale);
2739 break;
2740 }
2741 case 'R':
2742 case 'r':
2743 {
2744 if (LocaleNCompare(expression,"rand",4) == 0)
2745 return((MagickRealType) GetPseudoRandomValue(fx_info->random_info));
2746 if (LocaleNCompare(expression,"round",5) == 0)
2747 {
2748 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2749 exception);
cristy16788e42010-08-13 13:44:26 +00002750 return((MagickRealType) floor((double) alpha+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002751 }
2752 if (LocaleCompare(expression,"r") == 0)
2753 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2754 break;
2755 }
2756 case 'S':
2757 case 's':
2758 {
2759 if (LocaleCompare(expression,"saturation") == 0)
2760 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2761 if (LocaleNCompare(expression,"sign",4) == 0)
2762 {
2763 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2764 exception);
2765 return(alpha < 0.0 ? -1.0 : 1.0);
2766 }
cristya6a09e72010-03-02 14:51:02 +00002767 if (LocaleNCompare(expression,"sinc",4) == 0)
2768 {
2769 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2770 exception);
2771 if (alpha == 0)
2772 return(1.0);
2773 gamma=(MagickRealType) (sin((double) (MagickPI*alpha))/
2774 (MagickPI*alpha));
2775 return(gamma);
2776 }
cristy3ed852e2009-09-05 21:47:34 +00002777 if (LocaleNCompare(expression,"sinh",4) == 0)
2778 {
2779 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2780 exception);
2781 return((MagickRealType) sinh((double) alpha));
2782 }
2783 if (LocaleNCompare(expression,"sin",3) == 0)
2784 {
2785 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2786 exception);
2787 return((MagickRealType) sin((double) alpha));
2788 }
2789 if (LocaleNCompare(expression,"sqrt",4) == 0)
2790 {
2791 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2792 exception);
2793 return((MagickRealType) sqrt((double) alpha));
2794 }
cristy9eeedea2011-11-02 19:04:05 +00002795 if (LocaleNCompare(expression,"squish",6) == 0)
2796 {
2797 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2798 exception);
2799 return((MagickRealType) (1.0/(1.0+exp((double) (4.0*alpha)))));
2800 }
cristy3ed852e2009-09-05 21:47:34 +00002801 if (LocaleCompare(expression,"s") == 0)
2802 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2803 break;
2804 }
2805 case 'T':
2806 case 't':
2807 {
2808 if (LocaleNCompare(expression,"tanh",4) == 0)
2809 {
2810 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2811 exception);
2812 return((MagickRealType) tanh((double) alpha));
2813 }
2814 if (LocaleNCompare(expression,"tan",3) == 0)
2815 {
2816 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2817 exception);
2818 return((MagickRealType) tan((double) alpha));
2819 }
2820 if (LocaleCompare(expression,"Transparent") == 0)
2821 return(0.0);
cristy16788e42010-08-13 13:44:26 +00002822 if (LocaleNCompare(expression,"trunc",5) == 0)
2823 {
2824 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2825 exception);
2826 if (alpha >= 0.0)
2827 return((MagickRealType) floor((double) alpha));
2828 return((MagickRealType) ceil((double) alpha));
2829 }
cristy3ed852e2009-09-05 21:47:34 +00002830 if (LocaleCompare(expression,"t") == 0)
2831 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2832 break;
2833 }
2834 case 'U':
2835 case 'u':
2836 {
2837 if (LocaleCompare(expression,"u") == 0)
2838 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2839 break;
2840 }
2841 case 'V':
2842 case 'v':
2843 {
2844 if (LocaleCompare(expression,"v") == 0)
2845 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2846 break;
2847 }
2848 case 'W':
2849 case 'w':
2850 {
cristy9eeedea2011-11-02 19:04:05 +00002851 if (LocaleNCompare(expression,"while",5) == 0)
2852 {
2853 do
2854 {
2855 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2856 exception);
2857 } while (fabs((double) alpha) >= MagickEpsilon);
2858 return((MagickRealType) *beta);
2859 }
cristy3ed852e2009-09-05 21:47:34 +00002860 if (LocaleCompare(expression,"w") == 0)
2861 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2862 break;
2863 }
2864 case 'Y':
2865 case 'y':
2866 {
2867 if (LocaleCompare(expression,"y") == 0)
2868 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2869 break;
2870 }
2871 case 'Z':
2872 case 'z':
2873 {
2874 if (LocaleCompare(expression,"z") == 0)
2875 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2876 break;
2877 }
2878 default:
2879 break;
2880 }
2881 q=(char *) expression;
cristydbdd0e32011-11-04 23:29:40 +00002882 alpha=InterpretSiPrefixValue(expression,&q);
cristy3ed852e2009-09-05 21:47:34 +00002883 if (q == expression)
2884 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2885 return(alpha);
2886}
2887
cristy7832dc22011-09-05 01:21:53 +00002888MagickPrivate MagickBooleanType FxEvaluateExpression(FxInfo *fx_info,
cristy3ed852e2009-09-05 21:47:34 +00002889 MagickRealType *alpha,ExceptionInfo *exception)
2890{
2891 MagickBooleanType
2892 status;
2893
cristy541ae572011-08-05 19:08:59 +00002894 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2895 exception);
cristy3ed852e2009-09-05 21:47:34 +00002896 return(status);
2897}
2898
2899MagickExport MagickBooleanType FxPreprocessExpression(FxInfo *fx_info,
2900 MagickRealType *alpha,ExceptionInfo *exception)
2901{
2902 FILE
2903 *file;
2904
2905 MagickBooleanType
2906 status;
2907
2908 file=fx_info->file;
2909 fx_info->file=(FILE *) NULL;
cristy541ae572011-08-05 19:08:59 +00002910 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2911 exception);
cristy3ed852e2009-09-05 21:47:34 +00002912 fx_info->file=file;
2913 return(status);
2914}
2915
cristy7832dc22011-09-05 01:21:53 +00002916MagickPrivate MagickBooleanType FxEvaluateChannelExpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002917 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristye85007d2010-06-06 22:51:36 +00002918 MagickRealType *alpha,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002919{
2920 MagickRealType
2921 beta;
2922
2923 beta=0.0;
2924 *alpha=FxEvaluateSubexpression(fx_info,channel,x,y,fx_info->expression,&beta,
2925 exception);
2926 return(exception->severity == OptionError ? MagickFalse : MagickTrue);
2927}
2928
2929/*
2930%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2931% %
2932% %
2933% %
2934% F x I m a g e %
2935% %
2936% %
2937% %
2938%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2939%
2940% FxImage() applies a mathematical expression to the specified image.
2941%
2942% The format of the FxImage method is:
2943%
2944% Image *FxImage(const Image *image,const char *expression,
2945% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002946%
2947% A description of each parameter follows:
2948%
2949% o image: the image.
2950%
cristy3ed852e2009-09-05 21:47:34 +00002951% o expression: A mathematical expression.
2952%
2953% o exception: return any errors or warnings in this structure.
2954%
2955*/
2956
2957static FxInfo **DestroyFxThreadSet(FxInfo **fx_info)
2958{
cristybb503372010-05-27 20:51:26 +00002959 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002960 i;
2961
2962 assert(fx_info != (FxInfo **) NULL);
cristybb503372010-05-27 20:51:26 +00002963 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
cristy3ed852e2009-09-05 21:47:34 +00002964 if (fx_info[i] != (FxInfo *) NULL)
2965 fx_info[i]=DestroyFxInfo(fx_info[i]);
cristyb41ee102010-10-04 16:46:15 +00002966 fx_info=(FxInfo **) RelinquishMagickMemory(fx_info);
cristy3ed852e2009-09-05 21:47:34 +00002967 return(fx_info);
2968}
2969
2970static FxInfo **AcquireFxThreadSet(const Image *image,const char *expression,
2971 ExceptionInfo *exception)
2972{
2973 char
2974 *fx_expression;
2975
2976 FxInfo
2977 **fx_info;
2978
2979 MagickRealType
2980 alpha;
2981
cristybb503372010-05-27 20:51:26 +00002982 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002983 i;
2984
cristybb503372010-05-27 20:51:26 +00002985 size_t
cristy3ed852e2009-09-05 21:47:34 +00002986 number_threads;
2987
2988 number_threads=GetOpenMPMaximumThreads();
cristyb41ee102010-10-04 16:46:15 +00002989 fx_info=(FxInfo **) AcquireQuantumMemory(number_threads,sizeof(*fx_info));
cristy3ed852e2009-09-05 21:47:34 +00002990 if (fx_info == (FxInfo **) NULL)
2991 return((FxInfo **) NULL);
2992 (void) ResetMagickMemory(fx_info,0,number_threads*sizeof(*fx_info));
2993 if (*expression != '@')
2994 fx_expression=ConstantString(expression);
2995 else
2996 fx_expression=FileToString(expression+1,~0,exception);
cristybb503372010-05-27 20:51:26 +00002997 for (i=0; i < (ssize_t) number_threads; i++)
cristy3ed852e2009-09-05 21:47:34 +00002998 {
2999 fx_info[i]=AcquireFxInfo(image,fx_expression);
3000 if (fx_info[i] == (FxInfo *) NULL)
3001 return(DestroyFxThreadSet(fx_info));
3002 (void) FxPreprocessExpression(fx_info[i],&alpha,fx_info[i]->exception);
3003 }
3004 fx_expression=DestroyString(fx_expression);
3005 return(fx_info);
3006}
3007
3008MagickExport Image *FxImage(const Image *image,const char *expression,
3009 ExceptionInfo *exception)
3010{
cristy3ed852e2009-09-05 21:47:34 +00003011#define FxImageTag "Fx/Image"
3012
cristyfa112112010-01-04 17:48:07 +00003013 CacheView
cristy79cedc72011-07-25 00:41:15 +00003014 *fx_view,
3015 *image_view;
cristyfa112112010-01-04 17:48:07 +00003016
cristy3ed852e2009-09-05 21:47:34 +00003017 FxInfo
cristyfa112112010-01-04 17:48:07 +00003018 **restrict fx_info;
cristy3ed852e2009-09-05 21:47:34 +00003019
3020 Image
3021 *fx_image;
3022
cristy3ed852e2009-09-05 21:47:34 +00003023 MagickBooleanType
3024 status;
3025
cristybb503372010-05-27 20:51:26 +00003026 MagickOffsetType
3027 progress;
3028
cristy3ed852e2009-09-05 21:47:34 +00003029 MagickRealType
3030 alpha;
3031
cristybb503372010-05-27 20:51:26 +00003032 ssize_t
3033 y;
3034
cristy3ed852e2009-09-05 21:47:34 +00003035 assert(image != (Image *) NULL);
3036 assert(image->signature == MagickSignature);
3037 if (image->debug != MagickFalse)
3038 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy79cedc72011-07-25 00:41:15 +00003039 fx_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00003040 if (fx_image == (Image *) NULL)
3041 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003042 if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003043 {
cristy3ed852e2009-09-05 21:47:34 +00003044 fx_image=DestroyImage(fx_image);
3045 return((Image *) NULL);
3046 }
3047 fx_info=AcquireFxThreadSet(image,expression,exception);
3048 if (fx_info == (FxInfo **) NULL)
3049 {
3050 fx_image=DestroyImage(fx_image);
3051 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3052 }
3053 status=FxPreprocessExpression(fx_info[0],&alpha,exception);
3054 if (status == MagickFalse)
3055 {
3056 fx_image=DestroyImage(fx_image);
3057 fx_info=DestroyFxThreadSet(fx_info);
3058 return((Image *) NULL);
3059 }
3060 /*
3061 Fx image.
3062 */
3063 status=MagickTrue;
3064 progress=0;
cristy79cedc72011-07-25 00:41:15 +00003065 image_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00003066 fx_view=AcquireCacheView(fx_image);
cristyb5d5f722009-11-04 03:03:49 +00003067#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003068 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003069#endif
cristybb503372010-05-27 20:51:26 +00003070 for (y=0; y < (ssize_t) fx_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003071 {
cristy5c9e6f22010-09-17 17:31:01 +00003072 const int
3073 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00003074
cristy79cedc72011-07-25 00:41:15 +00003075 register const Quantum
3076 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003077
cristy4c08aed2011-07-01 19:47:50 +00003078 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003079 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003080
cristy79cedc72011-07-25 00:41:15 +00003081 register ssize_t
3082 x;
3083
cristy3ed852e2009-09-05 21:47:34 +00003084 if (status == MagickFalse)
3085 continue;
cristy79cedc72011-07-25 00:41:15 +00003086 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +00003087 q=QueueCacheViewAuthenticPixels(fx_view,0,y,fx_image->columns,1,exception);
cristy79cedc72011-07-25 00:41:15 +00003088 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003089 {
3090 status=MagickFalse;
3091 continue;
3092 }
cristybb503372010-05-27 20:51:26 +00003093 for (x=0; x < (ssize_t) fx_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003094 {
cristy79cedc72011-07-25 00:41:15 +00003095 register ssize_t
3096 i;
3097
3098 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3099 {
3100 MagickRealType
3101 alpha;
3102
3103 PixelChannel
3104 channel;
3105
3106 PixelTrait
3107 fx_traits,
3108 traits;
3109
cristye2a912b2011-12-05 20:02:07 +00003110 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003111 traits=GetPixelChannelMapTraits(image,channel);
cristy79cedc72011-07-25 00:41:15 +00003112 fx_traits=GetPixelChannelMapTraits(fx_image,channel);
cristy010d7d12011-08-31 01:02:48 +00003113 if ((traits == UndefinedPixelTrait) ||
3114 (fx_traits == UndefinedPixelTrait))
cristy79cedc72011-07-25 00:41:15 +00003115 continue;
3116 if ((fx_traits & CopyPixelTrait) != 0)
3117 {
cristy0beccfa2011-09-25 20:47:53 +00003118 SetPixelChannel(fx_image,channel,p[i],q);
cristy79cedc72011-07-25 00:41:15 +00003119 continue;
3120 }
3121 alpha=0.0;
cristya382aca2011-12-06 18:22:48 +00003122 (void) FxEvaluateChannelExpression(fx_info[id],channel,x,y,&alpha,
3123 exception);
cristyb3a73b52011-07-26 01:34:43 +00003124 q[i]=ClampToQuantum((MagickRealType) QuantumRange*alpha);
cristy79cedc72011-07-25 00:41:15 +00003125 }
3126 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003127 q+=GetPixelChannels(fx_image);
cristy3ed852e2009-09-05 21:47:34 +00003128 }
3129 if (SyncCacheViewAuthenticPixels(fx_view,exception) == MagickFalse)
3130 status=MagickFalse;
3131 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3132 {
3133 MagickBooleanType
3134 proceed;
3135
cristyb5d5f722009-11-04 03:03:49 +00003136#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy490408a2011-07-07 14:42:05 +00003137 #pragma omp critical (MagickCore_FxImage)
cristy3ed852e2009-09-05 21:47:34 +00003138#endif
3139 proceed=SetImageProgress(image,FxImageTag,progress++,image->rows);
3140 if (proceed == MagickFalse)
3141 status=MagickFalse;
3142 }
3143 }
cristy3ed852e2009-09-05 21:47:34 +00003144 fx_view=DestroyCacheView(fx_view);
cristy79cedc72011-07-25 00:41:15 +00003145 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003146 fx_info=DestroyFxThreadSet(fx_info);
3147 if (status == MagickFalse)
3148 fx_image=DestroyImage(fx_image);
3149 return(fx_image);
3150}
3151
3152/*
3153%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3154% %
3155% %
3156% %
3157% I m p l o d e I m a g e %
3158% %
3159% %
3160% %
3161%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3162%
3163% ImplodeImage() creates a new image that is a copy of an existing
3164% one with the image pixels "implode" by the specified percentage. It
3165% allocates the memory necessary for the new Image structure and returns a
3166% pointer to the new image.
3167%
3168% The format of the ImplodeImage method is:
3169%
3170% Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003171% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003172%
3173% A description of each parameter follows:
3174%
3175% o implode_image: Method ImplodeImage returns a pointer to the image
3176% after it is implode. A null image is returned if there is a memory
3177% shortage.
3178%
3179% o image: the image.
3180%
3181% o amount: Define the extent of the implosion.
3182%
cristy76f512e2011-09-12 01:26:56 +00003183% o method: the pixel interpolation method.
3184%
cristy3ed852e2009-09-05 21:47:34 +00003185% o exception: return any errors or warnings in this structure.
3186%
3187*/
3188MagickExport Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003189 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003190{
3191#define ImplodeImageTag "Implode/Image"
3192
cristyfa112112010-01-04 17:48:07 +00003193 CacheView
3194 *image_view,
3195 *implode_view;
3196
cristy3ed852e2009-09-05 21:47:34 +00003197 Image
3198 *implode_image;
3199
cristy3ed852e2009-09-05 21:47:34 +00003200 MagickBooleanType
3201 status;
3202
cristybb503372010-05-27 20:51:26 +00003203 MagickOffsetType
3204 progress;
3205
cristy3ed852e2009-09-05 21:47:34 +00003206 MagickRealType
3207 radius;
3208
3209 PointInfo
3210 center,
3211 scale;
3212
cristybb503372010-05-27 20:51:26 +00003213 ssize_t
3214 y;
3215
cristy3ed852e2009-09-05 21:47:34 +00003216 /*
3217 Initialize implode image attributes.
3218 */
3219 assert(image != (Image *) NULL);
3220 assert(image->signature == MagickSignature);
3221 if (image->debug != MagickFalse)
3222 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3223 assert(exception != (ExceptionInfo *) NULL);
3224 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00003225 implode_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3226 exception);
cristy3ed852e2009-09-05 21:47:34 +00003227 if (implode_image == (Image *) NULL)
3228 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003229 if (SetImageStorageClass(implode_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003230 {
cristy3ed852e2009-09-05 21:47:34 +00003231 implode_image=DestroyImage(implode_image);
3232 return((Image *) NULL);
3233 }
cristy4c08aed2011-07-01 19:47:50 +00003234 if (implode_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00003235 implode_image->matte=MagickTrue;
3236 /*
3237 Compute scaling factor.
3238 */
3239 scale.x=1.0;
3240 scale.y=1.0;
3241 center.x=0.5*image->columns;
3242 center.y=0.5*image->rows;
3243 radius=center.x;
3244 if (image->columns > image->rows)
3245 scale.y=(double) image->columns/(double) image->rows;
3246 else
3247 if (image->columns < image->rows)
3248 {
3249 scale.x=(double) image->rows/(double) image->columns;
3250 radius=center.y;
3251 }
3252 /*
3253 Implode image.
3254 */
3255 status=MagickTrue;
3256 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00003257 image_view=AcquireCacheView(image);
3258 implode_view=AcquireCacheView(implode_image);
cristyb5d5f722009-11-04 03:03:49 +00003259#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003260 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003261#endif
cristybb503372010-05-27 20:51:26 +00003262 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003263 {
cristy3ed852e2009-09-05 21:47:34 +00003264 MagickRealType
3265 distance;
3266
3267 PointInfo
3268 delta;
3269
cristy6d188022011-09-12 13:23:33 +00003270 register const Quantum
3271 *restrict p;
3272
cristybb503372010-05-27 20:51:26 +00003273 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003274 x;
3275
cristy4c08aed2011-07-01 19:47:50 +00003276 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003277 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003278
3279 if (status == MagickFalse)
3280 continue;
cristy6d188022011-09-12 13:23:33 +00003281 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +00003282 q=QueueCacheViewAuthenticPixels(implode_view,0,y,implode_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003283 exception);
cristy6d188022011-09-12 13:23:33 +00003284 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003285 {
3286 status=MagickFalse;
3287 continue;
3288 }
cristy3ed852e2009-09-05 21:47:34 +00003289 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00003290 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003291 {
cristy6d188022011-09-12 13:23:33 +00003292 register ssize_t
3293 i;
3294
cristy3ed852e2009-09-05 21:47:34 +00003295 /*
3296 Determine if the pixel is within an ellipse.
3297 */
3298 delta.x=scale.x*(double) (x-center.x);
3299 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00003300 if (distance >= (radius*radius))
cristya6d7a9b2012-01-18 20:04:48 +00003301 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy6d188022011-09-12 13:23:33 +00003302 {
cristya6d7a9b2012-01-18 20:04:48 +00003303 PixelChannel
3304 channel;
3305
3306 channel=GetPixelChannelMapChannel(image,i);
3307 SetPixelChannel(implode_image,channel,p[i],q);
cristy6d188022011-09-12 13:23:33 +00003308 }
3309 else
cristy3ed852e2009-09-05 21:47:34 +00003310 {
3311 double
3312 factor;
3313
3314 /*
3315 Implode the pixel.
3316 */
3317 factor=1.0;
3318 if (distance > 0.0)
3319 factor=pow(sin((double) (MagickPI*sqrt((double) distance)/
3320 radius/2)),-amount);
cristy76f512e2011-09-12 01:26:56 +00003321 status=InterpolatePixelChannels(image,image_view,implode_image,method,
3322 (double) (factor*delta.x/scale.x+center.x),(double) (factor*delta.y/
3323 scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00003324 }
cristy6d188022011-09-12 13:23:33 +00003325 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003326 q+=GetPixelChannels(implode_image);
cristy3ed852e2009-09-05 21:47:34 +00003327 }
3328 if (SyncCacheViewAuthenticPixels(implode_view,exception) == MagickFalse)
3329 status=MagickFalse;
3330 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3331 {
3332 MagickBooleanType
3333 proceed;
3334
cristyb5d5f722009-11-04 03:03:49 +00003335#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003336 #pragma omp critical (MagickCore_ImplodeImage)
3337#endif
3338 proceed=SetImageProgress(image,ImplodeImageTag,progress++,image->rows);
3339 if (proceed == MagickFalse)
3340 status=MagickFalse;
3341 }
3342 }
3343 implode_view=DestroyCacheView(implode_view);
3344 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003345 if (status == MagickFalse)
3346 implode_image=DestroyImage(implode_image);
3347 return(implode_image);
3348}
3349
3350/*
3351%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3352% %
3353% %
3354% %
3355% M o r p h I m a g e s %
3356% %
3357% %
3358% %
3359%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3360%
3361% The MorphImages() method requires a minimum of two images. The first
3362% image is transformed into the second by a number of intervening images
3363% as specified by frames.
3364%
3365% The format of the MorphImage method is:
3366%
cristybb503372010-05-27 20:51:26 +00003367% Image *MorphImages(const Image *image,const size_t number_frames,
cristy3ed852e2009-09-05 21:47:34 +00003368% ExceptionInfo *exception)
3369%
3370% A description of each parameter follows:
3371%
3372% o image: the image.
3373%
3374% o number_frames: Define the number of in-between image to generate.
3375% The more in-between frames, the smoother the morph.
3376%
3377% o exception: return any errors or warnings in this structure.
3378%
3379*/
3380MagickExport Image *MorphImages(const Image *image,
cristybb503372010-05-27 20:51:26 +00003381 const size_t number_frames,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003382{
3383#define MorphImageTag "Morph/Image"
3384
3385 Image
3386 *morph_image,
3387 *morph_images;
3388
cristy9d314ff2011-03-09 01:30:28 +00003389 MagickBooleanType
3390 status;
cristy3ed852e2009-09-05 21:47:34 +00003391
3392 MagickOffsetType
3393 scene;
3394
3395 MagickRealType
3396 alpha,
3397 beta;
3398
3399 register const Image
3400 *next;
3401
cristybb503372010-05-27 20:51:26 +00003402 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003403 i;
3404
cristy9d314ff2011-03-09 01:30:28 +00003405 ssize_t
3406 y;
cristy3ed852e2009-09-05 21:47:34 +00003407
3408 /*
3409 Clone first frame in sequence.
3410 */
3411 assert(image != (Image *) NULL);
3412 assert(image->signature == MagickSignature);
3413 if (image->debug != MagickFalse)
3414 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3415 assert(exception != (ExceptionInfo *) NULL);
3416 assert(exception->signature == MagickSignature);
3417 morph_images=CloneImage(image,0,0,MagickTrue,exception);
3418 if (morph_images == (Image *) NULL)
3419 return((Image *) NULL);
3420 if (GetNextImageInList(image) == (Image *) NULL)
3421 {
3422 /*
3423 Morph single image.
3424 */
cristybb503372010-05-27 20:51:26 +00003425 for (i=1; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003426 {
3427 morph_image=CloneImage(image,0,0,MagickTrue,exception);
3428 if (morph_image == (Image *) NULL)
3429 {
3430 morph_images=DestroyImageList(morph_images);
3431 return((Image *) NULL);
3432 }
3433 AppendImageToList(&morph_images,morph_image);
cristy8b27a6d2010-02-14 03:31:15 +00003434 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003435 {
cristy8b27a6d2010-02-14 03:31:15 +00003436 MagickBooleanType
3437 proceed;
3438
cristybb503372010-05-27 20:51:26 +00003439 proceed=SetImageProgress(image,MorphImageTag,(MagickOffsetType) i,
3440 number_frames);
cristy8b27a6d2010-02-14 03:31:15 +00003441 if (proceed == MagickFalse)
3442 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00003443 }
3444 }
3445 return(GetFirstImageInList(morph_images));
3446 }
3447 /*
3448 Morph image sequence.
3449 */
3450 status=MagickTrue;
3451 scene=0;
3452 next=image;
3453 for ( ; GetNextImageInList(next) != (Image *) NULL; next=GetNextImageInList(next))
3454 {
cristybb503372010-05-27 20:51:26 +00003455 for (i=0; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003456 {
3457 CacheView
3458 *image_view,
3459 *morph_view;
3460
3461 beta=(MagickRealType) (i+1.0)/(MagickRealType) (number_frames+1.0);
3462 alpha=1.0-beta;
cristy15b98cd2010-09-12 19:42:50 +00003463 morph_image=ResizeImage(next,(size_t) (alpha*next->columns+beta*
cristybb503372010-05-27 20:51:26 +00003464 GetNextImageInList(next)->columns+0.5),(size_t) (alpha*
cristy15b98cd2010-09-12 19:42:50 +00003465 next->rows+beta*GetNextImageInList(next)->rows+0.5),
3466 next->filter,next->blur,exception);
cristy3ed852e2009-09-05 21:47:34 +00003467 if (morph_image == (Image *) NULL)
3468 {
3469 morph_images=DestroyImageList(morph_images);
3470 return((Image *) NULL);
3471 }
cristy574cc262011-08-05 01:23:58 +00003472 if (SetImageStorageClass(morph_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003473 {
cristy3ed852e2009-09-05 21:47:34 +00003474 morph_image=DestroyImage(morph_image);
3475 return((Image *) NULL);
3476 }
3477 AppendImageToList(&morph_images,morph_image);
3478 morph_images=GetLastImageInList(morph_images);
cristy15b98cd2010-09-12 19:42:50 +00003479 morph_image=ResizeImage(GetNextImageInList(next),morph_images->columns,
3480 morph_images->rows,GetNextImageInList(next)->filter,
3481 GetNextImageInList(next)->blur,exception);
cristy3ed852e2009-09-05 21:47:34 +00003482 if (morph_image == (Image *) NULL)
3483 {
3484 morph_images=DestroyImageList(morph_images);
3485 return((Image *) NULL);
3486 }
3487 image_view=AcquireCacheView(morph_image);
3488 morph_view=AcquireCacheView(morph_images);
cristyb5d5f722009-11-04 03:03:49 +00003489#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003490 #pragma omp parallel for schedule(static,4) shared(status)
cristy3ed852e2009-09-05 21:47:34 +00003491#endif
cristybb503372010-05-27 20:51:26 +00003492 for (y=0; y < (ssize_t) morph_images->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003493 {
3494 MagickBooleanType
3495 sync;
3496
cristy4c08aed2011-07-01 19:47:50 +00003497 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003498 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003499
cristybb503372010-05-27 20:51:26 +00003500 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003501 x;
3502
cristy4c08aed2011-07-01 19:47:50 +00003503 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003504 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003505
3506 if (status == MagickFalse)
3507 continue;
3508 p=GetCacheViewVirtualPixels(image_view,0,y,morph_image->columns,1,
3509 exception);
3510 q=GetCacheViewAuthenticPixels(morph_view,0,y,morph_images->columns,1,
3511 exception);
cristy4c08aed2011-07-01 19:47:50 +00003512 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003513 {
3514 status=MagickFalse;
3515 continue;
3516 }
cristybb503372010-05-27 20:51:26 +00003517 for (x=0; x < (ssize_t) morph_images->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003518 {
cristy4c08aed2011-07-01 19:47:50 +00003519 SetPixelRed(morph_images,ClampToQuantum(alpha*
3520 GetPixelRed(morph_images,q)+beta*GetPixelRed(morph_image,p)),q);
3521 SetPixelGreen(morph_images,ClampToQuantum(alpha*
3522 GetPixelGreen(morph_images,q)+beta*GetPixelGreen(morph_image,p)),q);
3523 SetPixelBlue(morph_images,ClampToQuantum(alpha*
3524 GetPixelBlue(morph_images,q)+beta*GetPixelBlue(morph_image,p)),q);
3525 SetPixelAlpha(morph_images,ClampToQuantum(alpha*
3526 GetPixelAlpha(morph_images,q)+beta*GetPixelAlpha(morph_image,p)),q);
3527 if ((morph_image->colorspace == CMYKColorspace) &&
3528 (morph_images->colorspace == CMYKColorspace))
3529 SetPixelBlack(morph_images,ClampToQuantum(alpha*
3530 GetPixelBlack(morph_images,q)+beta*GetPixelBlack(morph_image,p)),
3531 q);
cristyed231572011-07-14 02:18:59 +00003532 p+=GetPixelChannels(morph_image);
3533 q+=GetPixelChannels(morph_images);
cristy3ed852e2009-09-05 21:47:34 +00003534 }
3535 sync=SyncCacheViewAuthenticPixels(morph_view,exception);
3536 if (sync == MagickFalse)
3537 status=MagickFalse;
3538 }
3539 morph_view=DestroyCacheView(morph_view);
3540 image_view=DestroyCacheView(image_view);
3541 morph_image=DestroyImage(morph_image);
3542 }
cristybb503372010-05-27 20:51:26 +00003543 if (i < (ssize_t) number_frames)
cristy3ed852e2009-09-05 21:47:34 +00003544 break;
3545 /*
3546 Clone last frame in sequence.
3547 */
3548 morph_image=CloneImage(GetNextImageInList(next),0,0,MagickTrue,exception);
3549 if (morph_image == (Image *) NULL)
3550 {
3551 morph_images=DestroyImageList(morph_images);
3552 return((Image *) NULL);
3553 }
3554 AppendImageToList(&morph_images,morph_image);
3555 morph_images=GetLastImageInList(morph_images);
3556 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3557 {
3558 MagickBooleanType
3559 proceed;
3560
cristyb5d5f722009-11-04 03:03:49 +00003561#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003562 #pragma omp critical (MagickCore_MorphImages)
3563#endif
3564 proceed=SetImageProgress(image,MorphImageTag,scene,
3565 GetImageListLength(image));
3566 if (proceed == MagickFalse)
3567 status=MagickFalse;
3568 }
3569 scene++;
3570 }
3571 if (GetNextImageInList(next) != (Image *) NULL)
3572 {
3573 morph_images=DestroyImageList(morph_images);
3574 return((Image *) NULL);
3575 }
3576 return(GetFirstImageInList(morph_images));
3577}
3578
3579/*
3580%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3581% %
3582% %
3583% %
3584% P l a s m a I m a g e %
3585% %
3586% %
3587% %
3588%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3589%
3590% PlasmaImage() initializes an image with plasma fractal values. The image
3591% must be initialized with a base color and the random number generator
3592% seeded before this method is called.
3593%
3594% The format of the PlasmaImage method is:
3595%
3596% MagickBooleanType PlasmaImage(Image *image,const SegmentInfo *segment,
cristy5cbc0162011-08-29 00:36:28 +00003597% size_t attenuate,size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003598%
3599% A description of each parameter follows:
3600%
3601% o image: the image.
3602%
3603% o segment: Define the region to apply plasma fractals values.
3604%
glennrp7dae1ca2010-09-16 12:17:35 +00003605% o attenuate: Define the plasma attenuation factor.
cristy3ed852e2009-09-05 21:47:34 +00003606%
3607% o depth: Limit the plasma recursion depth.
3608%
cristy5cbc0162011-08-29 00:36:28 +00003609% o exception: return any errors or warnings in this structure.
3610%
cristy3ed852e2009-09-05 21:47:34 +00003611*/
3612
3613static inline Quantum PlasmaPixel(RandomInfo *random_info,
3614 const MagickRealType pixel,const MagickRealType noise)
3615{
3616 Quantum
3617 plasma;
3618
cristyce70c172010-01-07 17:15:30 +00003619 plasma=ClampToQuantum(pixel+noise*GetPseudoRandomValue(random_info)-
cristy3ed852e2009-09-05 21:47:34 +00003620 noise/2.0);
3621 return(plasma);
3622}
3623
cristyda1f9c12011-10-02 21:39:49 +00003624static MagickBooleanType PlasmaImageProxy(Image *image,CacheView *image_view,
3625 CacheView *u_view,CacheView *v_view,RandomInfo *random_info,
3626 const SegmentInfo *segment,size_t attenuate,size_t depth,
3627 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003628{
cristy3ed852e2009-09-05 21:47:34 +00003629 MagickRealType
3630 plasma;
3631
cristyda1f9c12011-10-02 21:39:49 +00003632 PixelChannel
3633 channel;
3634
3635 PixelTrait
3636 traits;
3637
3638 register const Quantum
3639 *restrict u,
3640 *restrict v;
3641
3642 register Quantum
3643 *restrict q;
3644
3645 register ssize_t
3646 i;
cristy3ed852e2009-09-05 21:47:34 +00003647
cristy9d314ff2011-03-09 01:30:28 +00003648 ssize_t
3649 x,
3650 x_mid,
3651 y,
3652 y_mid;
3653
cristy3ed852e2009-09-05 21:47:34 +00003654 if (((segment->x2-segment->x1) == 0.0) && ((segment->y2-segment->y1) == 0.0))
3655 return(MagickTrue);
3656 if (depth != 0)
3657 {
3658 SegmentInfo
3659 local_info;
3660
3661 /*
3662 Divide the area into quadrants and recurse.
3663 */
3664 depth--;
3665 attenuate++;
cristybb503372010-05-27 20:51:26 +00003666 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3667 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003668 local_info=(*segment);
3669 local_info.x2=(double) x_mid;
3670 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003671 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3672 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003673 local_info=(*segment);
3674 local_info.y1=(double) y_mid;
3675 local_info.x2=(double) x_mid;
cristyda1f9c12011-10-02 21:39:49 +00003676 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3677 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003678 local_info=(*segment);
3679 local_info.x1=(double) x_mid;
3680 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003681 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3682 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003683 local_info=(*segment);
3684 local_info.x1=(double) x_mid;
3685 local_info.y1=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003686 return(PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3687 &local_info,attenuate,depth,exception));
cristy3ed852e2009-09-05 21:47:34 +00003688 }
cristybb503372010-05-27 20:51:26 +00003689 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3690 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003691 if ((segment->x1 == (double) x_mid) && (segment->x2 == (double) x_mid) &&
3692 (segment->y1 == (double) y_mid) && (segment->y2 == (double) y_mid))
3693 return(MagickFalse);
3694 /*
3695 Average pixels and apply plasma.
3696 */
cristy3ed852e2009-09-05 21:47:34 +00003697 plasma=(MagickRealType) QuantumRange/(2.0*attenuate);
3698 if ((segment->x1 != (double) x_mid) || (segment->x2 != (double) x_mid))
3699 {
cristy3ed852e2009-09-05 21:47:34 +00003700 /*
3701 Left pixel.
3702 */
cristybb503372010-05-27 20:51:26 +00003703 x=(ssize_t) ceil(segment->x1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003704 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3705 1,1,exception);
3706 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3707 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003708 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003709 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3710 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003711 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003712 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3713 {
cristye2a912b2011-12-05 20:02:07 +00003714 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003715 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003716 if (traits == UndefinedPixelTrait)
3717 continue;
3718 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3719 }
cristyc5c6f662010-09-22 14:23:02 +00003720 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003721 if (segment->x1 != segment->x2)
3722 {
3723 /*
3724 Right pixel.
3725 */
cristybb503372010-05-27 20:51:26 +00003726 x=(ssize_t) ceil(segment->x2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003727 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3728 1,1,exception);
3729 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3730 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003731 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003732 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3733 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003734 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003735 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3736 {
cristye2a912b2011-12-05 20:02:07 +00003737 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003738 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003739 if (traits == UndefinedPixelTrait)
3740 continue;
3741 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3742 }
cristyc5c6f662010-09-22 14:23:02 +00003743 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003744 }
3745 }
3746 if ((segment->y1 != (double) y_mid) || (segment->y2 != (double) y_mid))
3747 {
3748 if ((segment->x1 != (double) x_mid) || (segment->y2 != (double) y_mid))
3749 {
cristy3ed852e2009-09-05 21:47:34 +00003750 /*
3751 Bottom pixel.
3752 */
cristybb503372010-05-27 20:51:26 +00003753 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003754 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3755 1,1,exception);
3756 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3757 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003758 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003759 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3760 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003761 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003762 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3763 {
cristye2a912b2011-12-05 20:02:07 +00003764 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003765 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003766 if (traits == UndefinedPixelTrait)
3767 continue;
3768 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3769 }
cristyc5c6f662010-09-22 14:23:02 +00003770 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003771 }
3772 if (segment->y1 != segment->y2)
3773 {
cristy3ed852e2009-09-05 21:47:34 +00003774 /*
3775 Top pixel.
3776 */
cristybb503372010-05-27 20:51:26 +00003777 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003778 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3779 1,1,exception);
3780 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3781 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003782 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003783 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3784 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003785 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003786 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3787 {
cristye2a912b2011-12-05 20:02:07 +00003788 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003789 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003790 if (traits == UndefinedPixelTrait)
3791 continue;
3792 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3793 }
cristyc5c6f662010-09-22 14:23:02 +00003794 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003795 }
3796 }
3797 if ((segment->x1 != segment->x2) || (segment->y1 != segment->y2))
3798 {
cristy3ed852e2009-09-05 21:47:34 +00003799 /*
3800 Middle pixel.
3801 */
cristybb503372010-05-27 20:51:26 +00003802 x=(ssize_t) ceil(segment->x1-0.5);
3803 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003804 u=GetCacheViewVirtualPixels(u_view,x,y,1,1,exception);
cristybb503372010-05-27 20:51:26 +00003805 x=(ssize_t) ceil(segment->x2-0.5);
3806 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003807 v=GetCacheViewVirtualPixels(v_view,x,y,1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003808 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003809 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3810 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003811 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003812 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3813 {
cristye2a912b2011-12-05 20:02:07 +00003814 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003815 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003816 if (traits == UndefinedPixelTrait)
3817 continue;
3818 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3819 }
cristyc5c6f662010-09-22 14:23:02 +00003820 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003821 }
3822 if (((segment->x2-segment->x1) < 3.0) && ((segment->y2-segment->y1) < 3.0))
3823 return(MagickTrue);
3824 return(MagickFalse);
3825}
cristyda1f9c12011-10-02 21:39:49 +00003826
cristy3ed852e2009-09-05 21:47:34 +00003827MagickExport MagickBooleanType PlasmaImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00003828 const SegmentInfo *segment,size_t attenuate,size_t depth,
3829 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003830{
cristyc5c6f662010-09-22 14:23:02 +00003831 CacheView
cristyda1f9c12011-10-02 21:39:49 +00003832 *image_view,
3833 *u_view,
3834 *v_view;
cristyc5c6f662010-09-22 14:23:02 +00003835
cristy3ed852e2009-09-05 21:47:34 +00003836 MagickBooleanType
3837 status;
3838
3839 RandomInfo
3840 *random_info;
3841
3842 if (image->debug != MagickFalse)
3843 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3844 assert(image != (Image *) NULL);
3845 assert(image->signature == MagickSignature);
3846 if (image->debug != MagickFalse)
3847 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
cristy5cbc0162011-08-29 00:36:28 +00003848 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristyc5c6f662010-09-22 14:23:02 +00003849 return(MagickFalse);
3850 image_view=AcquireCacheView(image);
cristyda1f9c12011-10-02 21:39:49 +00003851 u_view=AcquireCacheView(image);
3852 v_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00003853 random_info=AcquireRandomInfo();
cristyda1f9c12011-10-02 21:39:49 +00003854 status=PlasmaImageProxy(image,image_view,u_view,v_view,random_info,segment,
3855 attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003856 random_info=DestroyRandomInfo(random_info);
cristyda1f9c12011-10-02 21:39:49 +00003857 v_view=DestroyCacheView(v_view);
3858 u_view=DestroyCacheView(u_view);
cristyc5c6f662010-09-22 14:23:02 +00003859 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003860 return(status);
3861}
3862
3863/*
3864%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3865% %
3866% %
3867% %
3868% P o l a r o i d I m a g e %
3869% %
3870% %
3871% %
3872%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3873%
3874% PolaroidImage() simulates a Polaroid picture.
3875%
3876% The format of the AnnotateImage method is:
3877%
3878% Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristye9e3d382011-12-14 01:50:13 +00003879% const char *caption,const double angle,
3880% const PixelInterpolateMethod method,ExceptionInfo exception)
cristy3ed852e2009-09-05 21:47:34 +00003881%
3882% A description of each parameter follows:
3883%
3884% o image: the image.
3885%
3886% o draw_info: the draw info.
3887%
cristye9e3d382011-12-14 01:50:13 +00003888% o caption: the Polaroid caption.
3889%
cristycee97112010-05-28 00:44:52 +00003890% o angle: Apply the effect along this angle.
cristy3ed852e2009-09-05 21:47:34 +00003891%
cristy5c4e2582011-09-11 19:21:03 +00003892% o method: the pixel interpolation method.
3893%
cristy3ed852e2009-09-05 21:47:34 +00003894% o exception: return any errors or warnings in this structure.
3895%
3896*/
3897MagickExport Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristye9e3d382011-12-14 01:50:13 +00003898 const char *caption,const double angle,const PixelInterpolateMethod method,
cristy5c4e2582011-09-11 19:21:03 +00003899 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003900{
cristy3ed852e2009-09-05 21:47:34 +00003901 Image
3902 *bend_image,
3903 *caption_image,
3904 *flop_image,
3905 *picture_image,
3906 *polaroid_image,
3907 *rotate_image,
3908 *trim_image;
3909
cristybb503372010-05-27 20:51:26 +00003910 size_t
cristy3ed852e2009-09-05 21:47:34 +00003911 height;
3912
cristy9d314ff2011-03-09 01:30:28 +00003913 ssize_t
3914 quantum;
3915
cristy3ed852e2009-09-05 21:47:34 +00003916 /*
3917 Simulate a Polaroid picture.
3918 */
3919 assert(image != (Image *) NULL);
3920 assert(image->signature == MagickSignature);
3921 if (image->debug != MagickFalse)
3922 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3923 assert(exception != (ExceptionInfo *) NULL);
3924 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00003925 quantum=(ssize_t) MagickMax(MagickMax((double) image->columns,(double)
cristy3ed852e2009-09-05 21:47:34 +00003926 image->rows)/25.0,10.0);
3927 height=image->rows+2*quantum;
3928 caption_image=(Image *) NULL;
cristye9e3d382011-12-14 01:50:13 +00003929 if (caption != (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003930 {
3931 char
cristye9e3d382011-12-14 01:50:13 +00003932 geometry[MaxTextExtent],
3933 *text;
cristy3ed852e2009-09-05 21:47:34 +00003934
3935 DrawInfo
3936 *annotate_info;
3937
cristy3ed852e2009-09-05 21:47:34 +00003938 MagickBooleanType
3939 status;
3940
cristy9d314ff2011-03-09 01:30:28 +00003941 ssize_t
3942 count;
3943
cristy3ed852e2009-09-05 21:47:34 +00003944 TypeMetric
3945 metrics;
3946
3947 /*
3948 Generate caption image.
3949 */
3950 caption_image=CloneImage(image,image->columns,1,MagickTrue,exception);
3951 if (caption_image == (Image *) NULL)
3952 return((Image *) NULL);
3953 annotate_info=CloneDrawInfo((const ImageInfo *) NULL,draw_info);
cristye9e3d382011-12-14 01:50:13 +00003954 text=InterpretImageProperties((ImageInfo *) NULL,(Image *) image,caption,
3955 exception);
3956 (void) CloneString(&annotate_info->text,text);
cristy6b1d05e2010-09-22 19:17:27 +00003957 count=FormatMagickCaption(caption_image,annotate_info,MagickTrue,&metrics,
cristye9e3d382011-12-14 01:50:13 +00003958 &text,exception);
3959 status=SetImageExtent(caption_image,image->columns,(size_t) ((count+1)*
3960 (metrics.ascent-metrics.descent)+0.5),exception);
cristy3ed852e2009-09-05 21:47:34 +00003961 if (status == MagickFalse)
3962 caption_image=DestroyImage(caption_image);
3963 else
3964 {
3965 caption_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00003966 (void) SetImageBackgroundColor(caption_image,exception);
cristye9e3d382011-12-14 01:50:13 +00003967 (void) CloneString(&annotate_info->text,text);
cristyb51dff52011-05-19 16:55:47 +00003968 (void) FormatLocaleString(geometry,MaxTextExtent,"+0+%g",
cristy3ed852e2009-09-05 21:47:34 +00003969 metrics.ascent);
3970 if (annotate_info->gravity == UndefinedGravity)
3971 (void) CloneString(&annotate_info->geometry,AcquireString(
3972 geometry));
cristy5cbc0162011-08-29 00:36:28 +00003973 (void) AnnotateImage(caption_image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00003974 height+=caption_image->rows;
3975 }
3976 annotate_info=DestroyDrawInfo(annotate_info);
cristye9e3d382011-12-14 01:50:13 +00003977 text=DestroyString(text);
cristy3ed852e2009-09-05 21:47:34 +00003978 }
3979 picture_image=CloneImage(image,image->columns+2*quantum,height,MagickTrue,
3980 exception);
3981 if (picture_image == (Image *) NULL)
3982 {
3983 if (caption_image != (Image *) NULL)
3984 caption_image=DestroyImage(caption_image);
3985 return((Image *) NULL);
3986 }
3987 picture_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00003988 (void) SetImageBackgroundColor(picture_image,exception);
cristye941a752011-10-15 01:52:48 +00003989 (void) CompositeImage(picture_image,OverCompositeOp,image,quantum,quantum,
3990 exception);
cristy3ed852e2009-09-05 21:47:34 +00003991 if (caption_image != (Image *) NULL)
3992 {
3993 (void) CompositeImage(picture_image,OverCompositeOp,caption_image,
cristye941a752011-10-15 01:52:48 +00003994 quantum,(ssize_t) (image->rows+3*quantum/2),exception);
cristy3ed852e2009-09-05 21:47:34 +00003995 caption_image=DestroyImage(caption_image);
3996 }
cristy9950d572011-10-01 18:22:35 +00003997 (void) QueryColorCompliance("none",AllCompliance,
3998 &picture_image->background_color,exception);
cristy63240882011-08-05 19:05:27 +00003999 (void) SetImageAlphaChannel(picture_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004000 rotate_image=RotateImage(picture_image,90.0,exception);
4001 picture_image=DestroyImage(picture_image);
4002 if (rotate_image == (Image *) NULL)
4003 return((Image *) NULL);
4004 picture_image=rotate_image;
4005 bend_image=WaveImage(picture_image,0.01*picture_image->rows,2.0*
cristy5c4e2582011-09-11 19:21:03 +00004006 picture_image->columns,method,exception);
cristy3ed852e2009-09-05 21:47:34 +00004007 picture_image=DestroyImage(picture_image);
4008 if (bend_image == (Image *) NULL)
4009 return((Image *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00004010 picture_image=bend_image;
4011 rotate_image=RotateImage(picture_image,-90.0,exception);
4012 picture_image=DestroyImage(picture_image);
4013 if (rotate_image == (Image *) NULL)
4014 return((Image *) NULL);
4015 picture_image=rotate_image;
4016 picture_image->background_color=image->background_color;
cristy78ec1a92011-12-09 09:25:34 +00004017 polaroid_image=ShadowImage(picture_image,80.0,2.0,0.0,quantum/3,quantum/3,
cristy3ed852e2009-09-05 21:47:34 +00004018 exception);
4019 if (polaroid_image == (Image *) NULL)
4020 {
4021 picture_image=DestroyImage(picture_image);
4022 return(picture_image);
4023 }
4024 flop_image=FlopImage(polaroid_image,exception);
4025 polaroid_image=DestroyImage(polaroid_image);
4026 if (flop_image == (Image *) NULL)
4027 {
4028 picture_image=DestroyImage(picture_image);
4029 return(picture_image);
4030 }
4031 polaroid_image=flop_image;
4032 (void) CompositeImage(polaroid_image,OverCompositeOp,picture_image,
cristye941a752011-10-15 01:52:48 +00004033 (ssize_t) (-0.01*picture_image->columns/2.0),0L,exception);
cristy3ed852e2009-09-05 21:47:34 +00004034 picture_image=DestroyImage(picture_image);
cristy9950d572011-10-01 18:22:35 +00004035 (void) QueryColorCompliance("none",AllCompliance,
4036 &polaroid_image->background_color,exception);
cristy3ed852e2009-09-05 21:47:34 +00004037 rotate_image=RotateImage(polaroid_image,angle,exception);
4038 polaroid_image=DestroyImage(polaroid_image);
4039 if (rotate_image == (Image *) NULL)
4040 return((Image *) NULL);
4041 polaroid_image=rotate_image;
4042 trim_image=TrimImage(polaroid_image,exception);
4043 polaroid_image=DestroyImage(polaroid_image);
4044 if (trim_image == (Image *) NULL)
4045 return((Image *) NULL);
4046 polaroid_image=trim_image;
4047 return(polaroid_image);
4048}
4049
4050/*
4051%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4052% %
4053% %
4054% %
cristy3ed852e2009-09-05 21:47:34 +00004055% S e p i a T o n e I m a g e %
4056% %
4057% %
4058% %
4059%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4060%
4061% MagickSepiaToneImage() applies a special effect to the image, similar to the
4062% effect achieved in a photo darkroom by sepia toning. Threshold ranges from
4063% 0 to QuantumRange and is a measure of the extent of the sepia toning. A
4064% threshold of 80% is a good starting point for a reasonable tone.
4065%
4066% The format of the SepiaToneImage method is:
4067%
4068% Image *SepiaToneImage(const Image *image,const double threshold,
4069% ExceptionInfo *exception)
4070%
4071% A description of each parameter follows:
4072%
4073% o image: the image.
4074%
4075% o threshold: the tone threshold.
4076%
4077% o exception: return any errors or warnings in this structure.
4078%
4079*/
4080MagickExport Image *SepiaToneImage(const Image *image,const double threshold,
4081 ExceptionInfo *exception)
4082{
4083#define SepiaToneImageTag "SepiaTone/Image"
4084
cristyc4c8d132010-01-07 01:58:38 +00004085 CacheView
4086 *image_view,
4087 *sepia_view;
4088
cristy3ed852e2009-09-05 21:47:34 +00004089 Image
4090 *sepia_image;
4091
cristy3ed852e2009-09-05 21:47:34 +00004092 MagickBooleanType
4093 status;
4094
cristybb503372010-05-27 20:51:26 +00004095 MagickOffsetType
4096 progress;
4097
4098 ssize_t
4099 y;
4100
cristy3ed852e2009-09-05 21:47:34 +00004101 /*
4102 Initialize sepia-toned image attributes.
4103 */
4104 assert(image != (const Image *) NULL);
4105 assert(image->signature == MagickSignature);
4106 if (image->debug != MagickFalse)
4107 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4108 assert(exception != (ExceptionInfo *) NULL);
4109 assert(exception->signature == MagickSignature);
4110 sepia_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
4111 if (sepia_image == (Image *) NULL)
4112 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004113 if (SetImageStorageClass(sepia_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004114 {
cristy3ed852e2009-09-05 21:47:34 +00004115 sepia_image=DestroyImage(sepia_image);
4116 return((Image *) NULL);
4117 }
4118 /*
4119 Tone each row of the image.
4120 */
4121 status=MagickTrue;
4122 progress=0;
4123 image_view=AcquireCacheView(image);
4124 sepia_view=AcquireCacheView(sepia_image);
cristyb5d5f722009-11-04 03:03:49 +00004125#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00004126 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00004127#endif
cristybb503372010-05-27 20:51:26 +00004128 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004129 {
cristy4c08aed2011-07-01 19:47:50 +00004130 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004131 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00004132
cristybb503372010-05-27 20:51:26 +00004133 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004134 x;
4135
cristy4c08aed2011-07-01 19:47:50 +00004136 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004137 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004138
4139 if (status == MagickFalse)
4140 continue;
4141 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
4142 q=QueueCacheViewAuthenticPixels(sepia_view,0,y,sepia_image->columns,1,
4143 exception);
cristy4c08aed2011-07-01 19:47:50 +00004144 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004145 {
4146 status=MagickFalse;
4147 continue;
4148 }
cristybb503372010-05-27 20:51:26 +00004149 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004150 {
4151 MagickRealType
4152 intensity,
4153 tone;
4154
cristy4c08aed2011-07-01 19:47:50 +00004155 intensity=(MagickRealType) GetPixelIntensity(image,p);
cristy3ed852e2009-09-05 21:47:34 +00004156 tone=intensity > threshold ? (MagickRealType) QuantumRange : intensity+
4157 (MagickRealType) QuantumRange-threshold;
cristy4c08aed2011-07-01 19:47:50 +00004158 SetPixelRed(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004159 tone=intensity > (7.0*threshold/6.0) ? (MagickRealType) QuantumRange :
4160 intensity+(MagickRealType) QuantumRange-7.0*threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004161 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004162 tone=intensity < (threshold/6.0) ? 0 : intensity-threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004163 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004164 tone=threshold/7.0;
cristy4c08aed2011-07-01 19:47:50 +00004165 if ((MagickRealType) GetPixelGreen(image,q) < tone)
4166 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
4167 if ((MagickRealType) GetPixelBlue(image,q) < tone)
4168 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristyed231572011-07-14 02:18:59 +00004169 p+=GetPixelChannels(image);
4170 q+=GetPixelChannels(sepia_image);
cristy3ed852e2009-09-05 21:47:34 +00004171 }
4172 if (SyncCacheViewAuthenticPixels(sepia_view,exception) == MagickFalse)
4173 status=MagickFalse;
4174 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4175 {
4176 MagickBooleanType
4177 proceed;
4178
cristyb5d5f722009-11-04 03:03:49 +00004179#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00004180 #pragma omp critical (MagickCore_SepiaToneImage)
4181#endif
4182 proceed=SetImageProgress(image,SepiaToneImageTag,progress++,
4183 image->rows);
4184 if (proceed == MagickFalse)
4185 status=MagickFalse;
4186 }
4187 }
4188 sepia_view=DestroyCacheView(sepia_view);
4189 image_view=DestroyCacheView(image_view);
cristye23ec9d2011-08-16 18:15:40 +00004190 (void) NormalizeImage(sepia_image,exception);
4191 (void) ContrastImage(sepia_image,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004192 if (status == MagickFalse)
4193 sepia_image=DestroyImage(sepia_image);
4194 return(sepia_image);
4195}
4196
4197/*
4198%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4199% %
4200% %
4201% %
4202% S h a d o w I m a g e %
4203% %
4204% %
4205% %
4206%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4207%
4208% ShadowImage() simulates a shadow from the specified image and returns it.
4209%
4210% The format of the ShadowImage method is:
4211%
cristy70cddf72011-12-10 22:42:42 +00004212% Image *ShadowImage(const Image *image,const double alpha,
cristyeb6e6582011-12-09 09:14:23 +00004213% const double sigma,const double bias,const ssize_t x_offset,
4214% const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004215%
4216% A description of each parameter follows:
4217%
4218% o image: the image.
4219%
cristy70cddf72011-12-10 22:42:42 +00004220% o alpha: percentage transparency.
cristy3ed852e2009-09-05 21:47:34 +00004221%
4222% o sigma: the standard deviation of the Gaussian, in pixels.
4223%
cristyeb6e6582011-12-09 09:14:23 +00004224% o bias: the bias.
4225%
cristy3ed852e2009-09-05 21:47:34 +00004226% o x_offset: the shadow x-offset.
4227%
4228% o y_offset: the shadow y-offset.
4229%
4230% o exception: return any errors or warnings in this structure.
4231%
4232*/
cristy70cddf72011-12-10 22:42:42 +00004233MagickExport 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#define ShadowImageTag "Shadow/Image"
4238
cristy70cddf72011-12-10 22:42:42 +00004239 CacheView
4240 *image_view;
4241
cristybd5a96c2011-08-21 00:04:26 +00004242 ChannelType
4243 channel_mask;
4244
cristy3ed852e2009-09-05 21:47:34 +00004245 Image
4246 *border_image,
4247 *clone_image,
4248 *shadow_image;
4249
cristy70cddf72011-12-10 22:42:42 +00004250 MagickBooleanType
4251 status;
4252
cristy3ed852e2009-09-05 21:47:34 +00004253 RectangleInfo
4254 border_info;
4255
cristy70cddf72011-12-10 22:42:42 +00004256 ssize_t
4257 y;
4258
cristy3ed852e2009-09-05 21:47:34 +00004259 assert(image != (Image *) NULL);
4260 assert(image->signature == MagickSignature);
4261 if (image->debug != MagickFalse)
4262 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4263 assert(exception != (ExceptionInfo *) NULL);
4264 assert(exception->signature == MagickSignature);
4265 clone_image=CloneImage(image,0,0,MagickTrue,exception);
4266 if (clone_image == (Image *) NULL)
4267 return((Image *) NULL);
4268 (void) SetImageVirtualPixelMethod(clone_image,EdgeVirtualPixelMethod);
cristybb503372010-05-27 20:51:26 +00004269 border_info.width=(size_t) floor(2.0*sigma+0.5);
4270 border_info.height=(size_t) floor(2.0*sigma+0.5);
cristy3ed852e2009-09-05 21:47:34 +00004271 border_info.x=0;
4272 border_info.y=0;
cristy9950d572011-10-01 18:22:35 +00004273 (void) QueryColorCompliance("none",AllCompliance,&clone_image->border_color,
4274 exception);
cristy70cddf72011-12-10 22:42:42 +00004275 clone_image->matte=MagickTrue;
4276 border_image=BorderImage(clone_image,&border_info,OverCompositeOp,exception);
cristy3ed852e2009-09-05 21:47:34 +00004277 clone_image=DestroyImage(clone_image);
4278 if (border_image == (Image *) NULL)
4279 return((Image *) NULL);
4280 if (border_image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00004281 (void) SetImageAlphaChannel(border_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004282 /*
4283 Shadow image.
4284 */
cristy70cddf72011-12-10 22:42:42 +00004285 status=MagickTrue;
4286 image_view=AcquireCacheView(border_image);
4287 for (y=0; y < (ssize_t) border_image->rows; y++)
4288 {
4289 PixelInfo
4290 background_color;
4291
4292 register Quantum
4293 *restrict q;
4294
4295 register ssize_t
4296 x;
4297
4298 if (status == MagickFalse)
4299 continue;
4300 q=QueueCacheViewAuthenticPixels(image_view,0,y,border_image->columns,1,
4301 exception);
4302 if (q == (Quantum *) NULL)
4303 {
4304 status=MagickFalse;
4305 continue;
4306 }
4307 background_color=border_image->background_color;
4308 background_color.matte=MagickTrue;
4309 for (x=0; x < (ssize_t) border_image->columns; x++)
4310 {
4311 if (border_image->matte != MagickFalse)
4312 background_color.alpha=GetPixelAlpha(border_image,q)*alpha/100.0;
4313 SetPixelInfoPixel(border_image,&background_color,q);
4314 q+=GetPixelChannels(border_image);
4315 }
4316 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4317 status=MagickFalse;
4318 }
4319 image_view=DestroyCacheView(image_view);
4320 if (status == MagickFalse)
4321 {
4322 border_image=DestroyImage(border_image);
4323 return((Image *) NULL);
4324 }
cristybd5a96c2011-08-21 00:04:26 +00004325 channel_mask=SetPixelChannelMask(border_image,AlphaChannel);
cristyb27bb772011-12-11 16:12:50 +00004326 shadow_image=BlurImage(border_image,0.0,sigma,bias,exception);
cristy3ed852e2009-09-05 21:47:34 +00004327 border_image=DestroyImage(border_image);
4328 if (shadow_image == (Image *) NULL)
4329 return((Image *) NULL);
cristyae1969f2011-12-10 03:07:36 +00004330 (void) SetPixelChannelMapMask(shadow_image,channel_mask);
cristy3ed852e2009-09-05 21:47:34 +00004331 if (shadow_image->page.width == 0)
4332 shadow_image->page.width=shadow_image->columns;
4333 if (shadow_image->page.height == 0)
4334 shadow_image->page.height=shadow_image->rows;
cristybb503372010-05-27 20:51:26 +00004335 shadow_image->page.width+=x_offset-(ssize_t) border_info.width;
4336 shadow_image->page.height+=y_offset-(ssize_t) border_info.height;
4337 shadow_image->page.x+=x_offset-(ssize_t) border_info.width;
4338 shadow_image->page.y+=y_offset-(ssize_t) border_info.height;
cristy3ed852e2009-09-05 21:47:34 +00004339 return(shadow_image);
4340}
4341
4342/*
4343%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4344% %
4345% %
4346% %
4347% S k e t c h I m a g e %
4348% %
4349% %
4350% %
4351%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4352%
4353% SketchImage() simulates a pencil sketch. We convolve the image with a
4354% Gaussian operator of the given radius and standard deviation (sigma). For
4355% reasonable results, radius should be larger than sigma. Use a radius of 0
4356% and SketchImage() selects a suitable radius for you. Angle gives the angle
4357% of the sketch.
4358%
4359% The format of the SketchImage method is:
4360%
4361% Image *SketchImage(const Image *image,const double radius,
cristyf7ef0252011-09-09 14:50:06 +00004362% const double sigma,const double angle,const double bias,
4363% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004364%
4365% A description of each parameter follows:
4366%
4367% o image: the image.
4368%
cristy574cc262011-08-05 01:23:58 +00004369% o radius: the radius of the Gaussian, in pixels, not counting the
4370% center pixel.
cristy3ed852e2009-09-05 21:47:34 +00004371%
4372% o sigma: the standard deviation of the Gaussian, in pixels.
4373%
cristyf7ef0252011-09-09 14:50:06 +00004374% o angle: apply the effect along this angle.
4375%
4376% o bias: the bias.
cristy3ed852e2009-09-05 21:47:34 +00004377%
4378% o exception: return any errors or warnings in this structure.
4379%
4380*/
4381MagickExport Image *SketchImage(const Image *image,const double radius,
cristyf7ef0252011-09-09 14:50:06 +00004382 const double sigma,const double angle,const double bias,
4383 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004384{
cristyfa112112010-01-04 17:48:07 +00004385 CacheView
4386 *random_view;
4387
cristy3ed852e2009-09-05 21:47:34 +00004388 Image
4389 *blend_image,
4390 *blur_image,
4391 *dodge_image,
4392 *random_image,
4393 *sketch_image;
4394
cristy3ed852e2009-09-05 21:47:34 +00004395 MagickBooleanType
4396 status;
4397
cristy3ed852e2009-09-05 21:47:34 +00004398 RandomInfo
cristyfa112112010-01-04 17:48:07 +00004399 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00004400
cristy9d314ff2011-03-09 01:30:28 +00004401 ssize_t
4402 y;
4403
cristy3ed852e2009-09-05 21:47:34 +00004404 /*
4405 Sketch image.
4406 */
4407 random_image=CloneImage(image,image->columns << 1,image->rows << 1,
4408 MagickTrue,exception);
4409 if (random_image == (Image *) NULL)
4410 return((Image *) NULL);
4411 status=MagickTrue;
cristy1b784432009-12-19 02:20:40 +00004412 random_info=AcquireRandomInfoThreadSet();
cristy3ed852e2009-09-05 21:47:34 +00004413 random_view=AcquireCacheView(random_image);
cristy1b784432009-12-19 02:20:40 +00004414#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00004415 #pragma omp parallel for schedule(static,4) shared(status)
cristy1b784432009-12-19 02:20:40 +00004416#endif
cristybb503372010-05-27 20:51:26 +00004417 for (y=0; y < (ssize_t) random_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004418 {
cristy5c9e6f22010-09-17 17:31:01 +00004419 const int
4420 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00004421
cristybb503372010-05-27 20:51:26 +00004422 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004423 x;
4424
cristy4c08aed2011-07-01 19:47:50 +00004425 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004426 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004427
cristy1b784432009-12-19 02:20:40 +00004428 if (status == MagickFalse)
4429 continue;
cristy3ed852e2009-09-05 21:47:34 +00004430 q=QueueCacheViewAuthenticPixels(random_view,0,y,random_image->columns,1,
4431 exception);
cristyacd2ed22011-08-30 01:44:23 +00004432 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004433 {
4434 status=MagickFalse;
4435 continue;
4436 }
cristybb503372010-05-27 20:51:26 +00004437 for (x=0; x < (ssize_t) random_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004438 {
cristy76f512e2011-09-12 01:26:56 +00004439 MagickRealType
4440 value;
4441
4442 register ssize_t
4443 i;
4444
4445 value=GetPseudoRandomValue(random_info[id]);
4446 for (i=0; i < (ssize_t) GetPixelChannels(random_image); i++)
4447 {
cristyabace412011-12-11 15:56:53 +00004448 PixelChannel
4449 channel;
4450
cristy76f512e2011-09-12 01:26:56 +00004451 PixelTrait
4452 traits;
4453
cristyabace412011-12-11 15:56:53 +00004454 channel=GetPixelChannelMapChannel(image,i);
4455 traits=GetPixelChannelMapTraits(image,channel);
cristy76f512e2011-09-12 01:26:56 +00004456 if (traits == UndefinedPixelTrait)
4457 continue;
4458 q[i]=ClampToQuantum(QuantumRange*value);
4459 }
cristyed231572011-07-14 02:18:59 +00004460 q+=GetPixelChannels(random_image);
cristy3ed852e2009-09-05 21:47:34 +00004461 }
4462 if (SyncCacheViewAuthenticPixels(random_view,exception) == MagickFalse)
4463 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004464 }
4465 random_view=DestroyCacheView(random_view);
cristy1b784432009-12-19 02:20:40 +00004466 random_info=DestroyRandomInfoThreadSet(random_info);
cristy3ed852e2009-09-05 21:47:34 +00004467 if (status == MagickFalse)
4468 {
4469 random_image=DestroyImage(random_image);
4470 return(random_image);
4471 }
cristyf7ef0252011-09-09 14:50:06 +00004472 blur_image=MotionBlurImage(random_image,radius,sigma,angle,bias,exception);
cristy3ed852e2009-09-05 21:47:34 +00004473 random_image=DestroyImage(random_image);
4474 if (blur_image == (Image *) NULL)
4475 return((Image *) NULL);
cristy6bfd6902011-12-09 01:33:45 +00004476 dodge_image=EdgeImage(blur_image,radius,1.0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004477 blur_image=DestroyImage(blur_image);
4478 if (dodge_image == (Image *) NULL)
4479 return((Image *) NULL);
cristye23ec9d2011-08-16 18:15:40 +00004480 (void) NormalizeImage(dodge_image,exception);
cristyb3e7c6c2011-07-24 01:43:55 +00004481 (void) NegateImage(dodge_image,MagickFalse,exception);
cristye941a752011-10-15 01:52:48 +00004482 (void) TransformImage(&dodge_image,(char *) NULL,"50%",exception);
cristy3ed852e2009-09-05 21:47:34 +00004483 sketch_image=CloneImage(image,0,0,MagickTrue,exception);
4484 if (sketch_image == (Image *) NULL)
4485 {
4486 dodge_image=DestroyImage(dodge_image);
4487 return((Image *) NULL);
4488 }
cristye941a752011-10-15 01:52:48 +00004489 (void) CompositeImage(sketch_image,ColorDodgeCompositeOp,dodge_image,0,0,
4490 exception);
cristy3ed852e2009-09-05 21:47:34 +00004491 dodge_image=DestroyImage(dodge_image);
4492 blend_image=CloneImage(image,0,0,MagickTrue,exception);
4493 if (blend_image == (Image *) NULL)
4494 {
4495 sketch_image=DestroyImage(sketch_image);
4496 return((Image *) NULL);
4497 }
4498 (void) SetImageArtifact(blend_image,"compose:args","20x80");
cristye941a752011-10-15 01:52:48 +00004499 (void) CompositeImage(sketch_image,BlendCompositeOp,blend_image,0,0,
4500 exception);
cristy3ed852e2009-09-05 21:47:34 +00004501 blend_image=DestroyImage(blend_image);
4502 return(sketch_image);
4503}
4504
4505/*
4506%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4507% %
4508% %
4509% %
4510% S o l a r i z e I m a g e %
4511% %
4512% %
4513% %
4514%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4515%
4516% SolarizeImage() applies a special effect to the image, similar to the effect
4517% achieved in a photo darkroom by selectively exposing areas of photo
4518% sensitive paper to light. Threshold ranges from 0 to QuantumRange and is a
4519% measure of the extent of the solarization.
4520%
4521% The format of the SolarizeImage method is:
4522%
cristy5cbc0162011-08-29 00:36:28 +00004523% MagickBooleanType SolarizeImage(Image *image,const double threshold,
4524% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004525%
4526% A description of each parameter follows:
4527%
4528% o image: the image.
4529%
4530% o threshold: Define the extent of the solarization.
4531%
cristy5cbc0162011-08-29 00:36:28 +00004532% o exception: return any errors or warnings in this structure.
4533%
cristy3ed852e2009-09-05 21:47:34 +00004534*/
4535MagickExport MagickBooleanType SolarizeImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00004536 const double threshold,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004537{
4538#define SolarizeImageTag "Solarize/Image"
4539
cristyc4c8d132010-01-07 01:58:38 +00004540 CacheView
4541 *image_view;
4542
cristy3ed852e2009-09-05 21:47:34 +00004543 MagickBooleanType
4544 status;
4545
cristybb503372010-05-27 20:51:26 +00004546 MagickOffsetType
4547 progress;
4548
4549 ssize_t
4550 y;
4551
cristy3ed852e2009-09-05 21:47:34 +00004552 assert(image != (Image *) NULL);
4553 assert(image->signature == MagickSignature);
4554 if (image->debug != MagickFalse)
4555 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4556 if (image->storage_class == PseudoClass)
4557 {
cristybb503372010-05-27 20:51:26 +00004558 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004559 i;
4560
4561 /*
4562 Solarize colormap.
4563 */
cristybb503372010-05-27 20:51:26 +00004564 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00004565 {
4566 if ((MagickRealType) image->colormap[i].red > threshold)
4567 image->colormap[i].red=(Quantum) QuantumRange-image->colormap[i].red;
4568 if ((MagickRealType) image->colormap[i].green > threshold)
4569 image->colormap[i].green=(Quantum) QuantumRange-
4570 image->colormap[i].green;
4571 if ((MagickRealType) image->colormap[i].blue > threshold)
4572 image->colormap[i].blue=(Quantum) QuantumRange-
4573 image->colormap[i].blue;
4574 }
4575 }
4576 /*
4577 Solarize image.
4578 */
4579 status=MagickTrue;
4580 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00004581 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00004582#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00004583 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00004584#endif
cristybb503372010-05-27 20:51:26 +00004585 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004586 {
cristybb503372010-05-27 20:51:26 +00004587 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004588 x;
4589
cristy4c08aed2011-07-01 19:47:50 +00004590 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004591 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004592
4593 if (status == MagickFalse)
4594 continue;
cristy5cbc0162011-08-29 00:36:28 +00004595 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00004596 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004597 {
4598 status=MagickFalse;
4599 continue;
4600 }
cristybb503372010-05-27 20:51:26 +00004601 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004602 {
cristy76f512e2011-09-12 01:26:56 +00004603 register ssize_t
4604 i;
4605
4606 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4607 {
cristyabace412011-12-11 15:56:53 +00004608 PixelChannel
4609 channel;
4610
cristy76f512e2011-09-12 01:26:56 +00004611 PixelTrait
4612 traits;
4613
cristyabace412011-12-11 15:56:53 +00004614 channel=GetPixelChannelMapChannel(image,i);
4615 traits=GetPixelChannelMapTraits(image,channel);
cristy76f512e2011-09-12 01:26:56 +00004616 if ((traits == UndefinedPixelTrait) ||
4617 ((traits & CopyPixelTrait) != 0))
4618 continue;
4619 if ((MagickRealType) q[i] > threshold)
4620 q[i]=QuantumRange-q[i];
4621 }
cristyed231572011-07-14 02:18:59 +00004622 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00004623 }
4624 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4625 status=MagickFalse;
4626 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4627 {
4628 MagickBooleanType
4629 proceed;
4630
cristyb5d5f722009-11-04 03:03:49 +00004631#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00004632 #pragma omp critical (MagickCore_SolarizeImage)
4633#endif
4634 proceed=SetImageProgress(image,SolarizeImageTag,progress++,image->rows);
4635 if (proceed == MagickFalse)
4636 status=MagickFalse;
4637 }
4638 }
4639 image_view=DestroyCacheView(image_view);
4640 return(status);
4641}
4642
4643/*
4644%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4645% %
4646% %
4647% %
4648% S t e g a n o I m a g e %
4649% %
4650% %
4651% %
4652%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4653%
4654% SteganoImage() hides a digital watermark within the image. Recover
4655% the hidden watermark later to prove that the authenticity of an image.
4656% Offset defines the start position within the image to hide the watermark.
4657%
4658% The format of the SteganoImage method is:
4659%
4660% Image *SteganoImage(const Image *image,Image *watermark,
4661% ExceptionInfo *exception)
4662%
4663% A description of each parameter follows:
4664%
4665% o image: the image.
4666%
4667% o watermark: the watermark image.
4668%
4669% o exception: return any errors or warnings in this structure.
4670%
4671*/
4672MagickExport Image *SteganoImage(const Image *image,const Image *watermark,
4673 ExceptionInfo *exception)
4674{
cristye1bf8ad2010-09-19 17:07:03 +00004675#define GetBit(alpha,i) ((((size_t) (alpha) >> (size_t) (i)) & 0x01) != 0)
cristy4c08aed2011-07-01 19:47:50 +00004676#define SetBit(alpha,i,set) (Quantum) ((set) != 0 ? (size_t) (alpha) \
cristyeaedf062010-05-29 22:36:02 +00004677 | (one << (size_t) (i)) : (size_t) (alpha) & ~(one << (size_t) (i)))
cristy3ed852e2009-09-05 21:47:34 +00004678#define SteganoImageTag "Stegano/Image"
4679
cristyb0d3bb92010-09-22 14:37:58 +00004680 CacheView
4681 *stegano_view,
4682 *watermark_view;
4683
cristy3ed852e2009-09-05 21:47:34 +00004684 Image
4685 *stegano_image;
4686
4687 int
4688 c;
4689
cristy3ed852e2009-09-05 21:47:34 +00004690 MagickBooleanType
4691 status;
4692
cristy101ab702011-10-13 13:06:32 +00004693 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00004694 pixel;
4695
cristy4c08aed2011-07-01 19:47:50 +00004696 register Quantum
cristy3ed852e2009-09-05 21:47:34 +00004697 *q;
4698
cristye1bf8ad2010-09-19 17:07:03 +00004699 register ssize_t
4700 x;
4701
cristybb503372010-05-27 20:51:26 +00004702 size_t
cristyeaedf062010-05-29 22:36:02 +00004703 depth,
4704 one;
cristy3ed852e2009-09-05 21:47:34 +00004705
cristye1bf8ad2010-09-19 17:07:03 +00004706 ssize_t
4707 i,
4708 j,
4709 k,
4710 y;
4711
cristy3ed852e2009-09-05 21:47:34 +00004712 /*
4713 Initialize steganographic image attributes.
4714 */
4715 assert(image != (const Image *) NULL);
4716 assert(image->signature == MagickSignature);
4717 if (image->debug != MagickFalse)
4718 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4719 assert(watermark != (const Image *) NULL);
4720 assert(watermark->signature == MagickSignature);
4721 assert(exception != (ExceptionInfo *) NULL);
4722 assert(exception->signature == MagickSignature);
cristyeaedf062010-05-29 22:36:02 +00004723 one=1UL;
cristy3ed852e2009-09-05 21:47:34 +00004724 stegano_image=CloneImage(image,0,0,MagickTrue,exception);
4725 if (stegano_image == (Image *) NULL)
4726 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004727 if (SetImageStorageClass(stegano_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004728 {
cristy3ed852e2009-09-05 21:47:34 +00004729 stegano_image=DestroyImage(stegano_image);
4730 return((Image *) NULL);
4731 }
4732 stegano_image->depth=MAGICKCORE_QUANTUM_DEPTH;
4733 /*
4734 Hide watermark in low-order bits of image.
4735 */
4736 c=0;
4737 i=0;
4738 j=0;
4739 depth=stegano_image->depth;
4740 k=image->offset;
cristyda16f162011-02-19 23:52:17 +00004741 status=MagickTrue;
cristyb0d3bb92010-09-22 14:37:58 +00004742 watermark_view=AcquireCacheView(watermark);
4743 stegano_view=AcquireCacheView(stegano_image);
cristybb503372010-05-27 20:51:26 +00004744 for (i=(ssize_t) depth-1; (i >= 0) && (j < (ssize_t) depth); i--)
cristy3ed852e2009-09-05 21:47:34 +00004745 {
cristybb503372010-05-27 20:51:26 +00004746 for (y=0; (y < (ssize_t) watermark->rows) && (j < (ssize_t) depth); y++)
cristy3ed852e2009-09-05 21:47:34 +00004747 {
cristybb503372010-05-27 20:51:26 +00004748 for (x=0; (x < (ssize_t) watermark->columns) && (j < (ssize_t) depth); x++)
cristy3ed852e2009-09-05 21:47:34 +00004749 {
cristyda1f9c12011-10-02 21:39:49 +00004750 Quantum
cristy5f95f4f2011-10-23 01:01:01 +00004751 virtual_pixel[CompositePixelChannel];
cristyda1f9c12011-10-02 21:39:49 +00004752
4753 (void) GetOneCacheViewVirtualPixel(watermark_view,x,y,virtual_pixel,
4754 exception);
4755 pixel.red=(double) virtual_pixel[RedPixelChannel];
4756 pixel.green=(double) virtual_pixel[GreenPixelChannel];
4757 pixel.blue=(double) virtual_pixel[BluePixelChannel];
4758 pixel.alpha=(double) virtual_pixel[AlphaPixelChannel];
cristybb503372010-05-27 20:51:26 +00004759 if ((k/(ssize_t) stegano_image->columns) >= (ssize_t) stegano_image->rows)
cristy3ed852e2009-09-05 21:47:34 +00004760 break;
cristyb0d3bb92010-09-22 14:37:58 +00004761 q=GetCacheViewAuthenticPixels(stegano_view,k % (ssize_t)
4762 stegano_image->columns,k/(ssize_t) stegano_image->columns,1,1,
4763 exception);
cristyacd2ed22011-08-30 01:44:23 +00004764 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004765 break;
4766 switch (c)
4767 {
4768 case 0:
4769 {
cristy4c08aed2011-07-01 19:47:50 +00004770 SetPixelRed(image,SetBit(GetPixelRed(image,q),j,GetBit(
cristy101ab702011-10-13 13:06:32 +00004771 GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004772 break;
4773 }
4774 case 1:
4775 {
cristy4c08aed2011-07-01 19:47:50 +00004776 SetPixelGreen(image,SetBit(GetPixelGreen(image,q),j,GetBit(
cristy101ab702011-10-13 13:06:32 +00004777 GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004778 break;
4779 }
4780 case 2:
4781 {
cristy4c08aed2011-07-01 19:47:50 +00004782 SetPixelBlue(image,SetBit(GetPixelBlue(image,q),j,GetBit(
cristy101ab702011-10-13 13:06:32 +00004783 GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004784 break;
4785 }
4786 }
cristyb0d3bb92010-09-22 14:37:58 +00004787 if (SyncCacheViewAuthenticPixels(stegano_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004788 break;
4789 c++;
4790 if (c == 3)
4791 c=0;
4792 k++;
cristybb503372010-05-27 20:51:26 +00004793 if (k == (ssize_t) (stegano_image->columns*stegano_image->columns))
cristy3ed852e2009-09-05 21:47:34 +00004794 k=0;
4795 if (k == image->offset)
4796 j++;
4797 }
4798 }
cristy8b27a6d2010-02-14 03:31:15 +00004799 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004800 {
cristy8b27a6d2010-02-14 03:31:15 +00004801 MagickBooleanType
4802 proceed;
4803
4804 proceed=SetImageProgress(image,SteganoImageTag,(MagickOffsetType)
4805 (depth-i),depth);
4806 if (proceed == MagickFalse)
4807 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004808 }
4809 }
cristyb0d3bb92010-09-22 14:37:58 +00004810 stegano_view=DestroyCacheView(stegano_view);
4811 watermark_view=DestroyCacheView(watermark_view);
cristy3ed852e2009-09-05 21:47:34 +00004812 if (stegano_image->storage_class == PseudoClass)
cristyea1a8aa2011-10-20 13:24:06 +00004813 (void) SyncImage(stegano_image,exception);
cristyda16f162011-02-19 23:52:17 +00004814 if (status == MagickFalse)
4815 {
4816 stegano_image=DestroyImage(stegano_image);
4817 return((Image *) NULL);
4818 }
cristy3ed852e2009-09-05 21:47:34 +00004819 return(stegano_image);
4820}
4821
4822/*
4823%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4824% %
4825% %
4826% %
4827% S t e r e o A n a g l y p h I m a g e %
4828% %
4829% %
4830% %
4831%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4832%
4833% StereoAnaglyphImage() combines two images and produces a single image that
4834% is the composite of a left and right image of a stereo pair. Special
4835% red-green stereo glasses are required to view this effect.
4836%
4837% The format of the StereoAnaglyphImage method is:
4838%
4839% Image *StereoImage(const Image *left_image,const Image *right_image,
4840% ExceptionInfo *exception)
4841% Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004842% const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004843% ExceptionInfo *exception)
4844%
4845% A description of each parameter follows:
4846%
4847% o left_image: the left image.
4848%
4849% o right_image: the right image.
4850%
4851% o exception: return any errors or warnings in this structure.
4852%
4853% o x_offset: amount, in pixels, by which the left image is offset to the
4854% right of the right image.
4855%
4856% o y_offset: amount, in pixels, by which the left image is offset to the
4857% bottom of the right image.
4858%
4859%
4860*/
4861MagickExport Image *StereoImage(const Image *left_image,
4862 const Image *right_image,ExceptionInfo *exception)
4863{
4864 return(StereoAnaglyphImage(left_image,right_image,0,0,exception));
4865}
4866
4867MagickExport Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004868 const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004869 ExceptionInfo *exception)
4870{
4871#define StereoImageTag "Stereo/Image"
4872
4873 const Image
4874 *image;
4875
4876 Image
4877 *stereo_image;
4878
cristy3ed852e2009-09-05 21:47:34 +00004879 MagickBooleanType
4880 status;
4881
cristy9d314ff2011-03-09 01:30:28 +00004882 ssize_t
4883 y;
4884
cristy3ed852e2009-09-05 21:47:34 +00004885 assert(left_image != (const Image *) NULL);
4886 assert(left_image->signature == MagickSignature);
4887 if (left_image->debug != MagickFalse)
4888 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4889 left_image->filename);
4890 assert(right_image != (const Image *) NULL);
4891 assert(right_image->signature == MagickSignature);
4892 assert(exception != (ExceptionInfo *) NULL);
4893 assert(exception->signature == MagickSignature);
4894 assert(right_image != (const Image *) NULL);
4895 image=left_image;
4896 if ((left_image->columns != right_image->columns) ||
4897 (left_image->rows != right_image->rows))
4898 ThrowImageException(ImageError,"LeftAndRightImageSizesDiffer");
4899 /*
4900 Initialize stereo image attributes.
4901 */
4902 stereo_image=CloneImage(left_image,left_image->columns,left_image->rows,
4903 MagickTrue,exception);
4904 if (stereo_image == (Image *) NULL)
4905 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004906 if (SetImageStorageClass(stereo_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004907 {
cristy3ed852e2009-09-05 21:47:34 +00004908 stereo_image=DestroyImage(stereo_image);
4909 return((Image *) NULL);
4910 }
4911 /*
4912 Copy left image to red channel and right image to blue channel.
4913 */
cristyda16f162011-02-19 23:52:17 +00004914 status=MagickTrue;
cristybb503372010-05-27 20:51:26 +00004915 for (y=0; y < (ssize_t) stereo_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004916 {
cristy4c08aed2011-07-01 19:47:50 +00004917 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004918 *restrict p,
4919 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004920
cristybb503372010-05-27 20:51:26 +00004921 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004922 x;
4923
cristy4c08aed2011-07-01 19:47:50 +00004924 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004925 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +00004926
4927 p=GetVirtualPixels(left_image,-x_offset,y-y_offset,image->columns,1,
4928 exception);
4929 q=GetVirtualPixels(right_image,0,y,right_image->columns,1,exception);
4930 r=QueueAuthenticPixels(stereo_image,0,y,stereo_image->columns,1,exception);
cristy76f512e2011-09-12 01:26:56 +00004931 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL) ||
4932 (r == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004933 break;
cristybb503372010-05-27 20:51:26 +00004934 for (x=0; x < (ssize_t) stereo_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004935 {
cristy4c08aed2011-07-01 19:47:50 +00004936 SetPixelRed(image,GetPixelRed(left_image,p),r);
cristy76f512e2011-09-12 01:26:56 +00004937 SetPixelGreen(image,GetPixelGreen(right_image,q),r);
4938 SetPixelBlue(image,GetPixelBlue(right_image,q),r);
4939 if ((GetPixelAlphaTraits(stereo_image) & CopyPixelTrait) != 0)
4940 SetPixelAlpha(image,(GetPixelAlpha(left_image,p)+
4941 GetPixelAlpha(right_image,q))/2,r);
cristyed231572011-07-14 02:18:59 +00004942 p+=GetPixelChannels(left_image);
cristy76f512e2011-09-12 01:26:56 +00004943 q+=GetPixelChannels(right_image);
4944 r+=GetPixelChannels(stereo_image);
cristy3ed852e2009-09-05 21:47:34 +00004945 }
4946 if (SyncAuthenticPixels(stereo_image,exception) == MagickFalse)
4947 break;
cristy8b27a6d2010-02-14 03:31:15 +00004948 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004949 {
cristy8b27a6d2010-02-14 03:31:15 +00004950 MagickBooleanType
4951 proceed;
4952
cristybb503372010-05-27 20:51:26 +00004953 proceed=SetImageProgress(image,StereoImageTag,(MagickOffsetType) y,
4954 stereo_image->rows);
cristy8b27a6d2010-02-14 03:31:15 +00004955 if (proceed == MagickFalse)
4956 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004957 }
4958 }
cristyda16f162011-02-19 23:52:17 +00004959 if (status == MagickFalse)
4960 {
4961 stereo_image=DestroyImage(stereo_image);
4962 return((Image *) NULL);
4963 }
cristy3ed852e2009-09-05 21:47:34 +00004964 return(stereo_image);
4965}
4966
4967/*
4968%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4969% %
4970% %
4971% %
4972% S w i r l I m a g e %
4973% %
4974% %
4975% %
4976%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4977%
4978% SwirlImage() swirls the pixels about the center of the image, where
4979% degrees indicates the sweep of the arc through which each pixel is moved.
4980% You get a more dramatic effect as the degrees move from 1 to 360.
4981%
4982% The format of the SwirlImage method is:
4983%
4984% Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00004985% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004986%
4987% A description of each parameter follows:
4988%
4989% o image: the image.
4990%
4991% o degrees: Define the tightness of the swirling effect.
4992%
cristy76f512e2011-09-12 01:26:56 +00004993% o method: the pixel interpolation method.
4994%
cristy3ed852e2009-09-05 21:47:34 +00004995% o exception: return any errors or warnings in this structure.
4996%
4997*/
4998MagickExport Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00004999 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005000{
5001#define SwirlImageTag "Swirl/Image"
5002
cristyfa112112010-01-04 17:48:07 +00005003 CacheView
5004 *image_view,
5005 *swirl_view;
5006
cristy3ed852e2009-09-05 21:47:34 +00005007 Image
5008 *swirl_image;
5009
cristy3ed852e2009-09-05 21:47:34 +00005010 MagickBooleanType
5011 status;
5012
cristybb503372010-05-27 20:51:26 +00005013 MagickOffsetType
5014 progress;
5015
cristy3ed852e2009-09-05 21:47:34 +00005016 MagickRealType
5017 radius;
5018
5019 PointInfo
5020 center,
5021 scale;
5022
cristybb503372010-05-27 20:51:26 +00005023 ssize_t
5024 y;
5025
cristy3ed852e2009-09-05 21:47:34 +00005026 /*
5027 Initialize swirl image attributes.
5028 */
5029 assert(image != (const Image *) NULL);
5030 assert(image->signature == MagickSignature);
5031 if (image->debug != MagickFalse)
5032 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5033 assert(exception != (ExceptionInfo *) NULL);
5034 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00005035 swirl_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005036 if (swirl_image == (Image *) NULL)
5037 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005038 if (SetImageStorageClass(swirl_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005039 {
cristy3ed852e2009-09-05 21:47:34 +00005040 swirl_image=DestroyImage(swirl_image);
5041 return((Image *) NULL);
5042 }
cristy4c08aed2011-07-01 19:47:50 +00005043 if (swirl_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00005044 swirl_image->matte=MagickTrue;
5045 /*
5046 Compute scaling factor.
5047 */
5048 center.x=(double) image->columns/2.0;
5049 center.y=(double) image->rows/2.0;
5050 radius=MagickMax(center.x,center.y);
5051 scale.x=1.0;
5052 scale.y=1.0;
5053 if (image->columns > image->rows)
5054 scale.y=(double) image->columns/(double) image->rows;
5055 else
5056 if (image->columns < image->rows)
5057 scale.x=(double) image->rows/(double) image->columns;
5058 degrees=(double) DegreesToRadians(degrees);
5059 /*
5060 Swirl image.
5061 */
5062 status=MagickTrue;
5063 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00005064 image_view=AcquireCacheView(image);
5065 swirl_view=AcquireCacheView(swirl_image);
cristyb5d5f722009-11-04 03:03:49 +00005066#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy78778cb2012-01-17 14:48:47 +00005067 #pragma omp parallel for schedule(static,8) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00005068#endif
cristybb503372010-05-27 20:51:26 +00005069 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005070 {
cristy3ed852e2009-09-05 21:47:34 +00005071 MagickRealType
5072 distance;
5073
5074 PointInfo
5075 delta;
5076
cristy6d188022011-09-12 13:23:33 +00005077 register const Quantum
5078 *restrict p;
5079
cristybb503372010-05-27 20:51:26 +00005080 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005081 x;
5082
cristy4c08aed2011-07-01 19:47:50 +00005083 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005084 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005085
5086 if (status == MagickFalse)
5087 continue;
cristy6d188022011-09-12 13:23:33 +00005088 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy3ed852e2009-09-05 21:47:34 +00005089 q=GetCacheViewAuthenticPixels(swirl_view,0,y,swirl_image->columns,1,
5090 exception);
cristy6d188022011-09-12 13:23:33 +00005091 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005092 {
5093 status=MagickFalse;
5094 continue;
5095 }
cristy3ed852e2009-09-05 21:47:34 +00005096 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00005097 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005098 {
cristy6d188022011-09-12 13:23:33 +00005099 register ssize_t
5100 i;
5101
cristy3ed852e2009-09-05 21:47:34 +00005102 /*
5103 Determine if the pixel is within an ellipse.
5104 */
5105 delta.x=scale.x*(double) (x-center.x);
5106 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00005107 if (distance >= (radius*radius))
5108 {
5109 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5110 q[i]=p[i];
5111 }
5112 else
cristy3ed852e2009-09-05 21:47:34 +00005113 {
5114 MagickRealType
5115 cosine,
5116 factor,
5117 sine;
5118
5119 /*
5120 Swirl the pixel.
5121 */
5122 factor=1.0-sqrt((double) distance)/radius;
5123 sine=sin((double) (degrees*factor*factor));
5124 cosine=cos((double) (degrees*factor*factor));
cristy76f512e2011-09-12 01:26:56 +00005125 status=InterpolatePixelChannels(image,image_view,swirl_image,method,
5126 ((cosine*delta.x-sine*delta.y)/scale.x+center.x),(double)
5127 ((sine*delta.x+cosine*delta.y)/scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00005128 }
cristy6d188022011-09-12 13:23:33 +00005129 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00005130 q+=GetPixelChannels(swirl_image);
cristy3ed852e2009-09-05 21:47:34 +00005131 }
5132 if (SyncCacheViewAuthenticPixels(swirl_view,exception) == MagickFalse)
5133 status=MagickFalse;
5134 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5135 {
5136 MagickBooleanType
5137 proceed;
5138
cristyb5d5f722009-11-04 03:03:49 +00005139#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00005140 #pragma omp critical (MagickCore_SwirlImage)
5141#endif
5142 proceed=SetImageProgress(image,SwirlImageTag,progress++,image->rows);
5143 if (proceed == MagickFalse)
5144 status=MagickFalse;
5145 }
5146 }
5147 swirl_view=DestroyCacheView(swirl_view);
5148 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00005149 if (status == MagickFalse)
5150 swirl_image=DestroyImage(swirl_image);
5151 return(swirl_image);
5152}
5153
5154/*
5155%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5156% %
5157% %
5158% %
5159% T i n t I m a g e %
5160% %
5161% %
5162% %
5163%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5164%
5165% TintImage() applies a color vector to each pixel in the image. The length
5166% of the vector is 0 for black and white and at its maximum for the midtones.
5167% The vector weighting function is f(x)=(1-(4.0*((x-0.5)*(x-0.5))))
5168%
5169% The format of the TintImage method is:
5170%
cristyb817c3f2011-10-03 14:00:35 +00005171% Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005172% const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005173%
5174% A description of each parameter follows:
5175%
5176% o image: the image.
5177%
cristyb817c3f2011-10-03 14:00:35 +00005178% o blend: A color value used for tinting.
cristy3ed852e2009-09-05 21:47:34 +00005179%
5180% o tint: A color value used for tinting.
5181%
5182% o exception: return any errors or warnings in this structure.
5183%
5184*/
cristyb817c3f2011-10-03 14:00:35 +00005185MagickExport Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005186 const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005187{
5188#define TintImageTag "Tint/Image"
5189
cristyc4c8d132010-01-07 01:58:38 +00005190 CacheView
5191 *image_view,
5192 *tint_view;
5193
cristy3ed852e2009-09-05 21:47:34 +00005194 GeometryInfo
5195 geometry_info;
5196
5197 Image
5198 *tint_image;
5199
cristy3ed852e2009-09-05 21:47:34 +00005200 MagickBooleanType
5201 status;
5202
cristybb503372010-05-27 20:51:26 +00005203 MagickOffsetType
5204 progress;
cristy3ed852e2009-09-05 21:47:34 +00005205
cristy28474bf2011-09-11 23:32:52 +00005206 MagickRealType
5207 intensity;
5208
cristy4c08aed2011-07-01 19:47:50 +00005209 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00005210 color_vector,
5211 pixel;
5212
cristybb503372010-05-27 20:51:26 +00005213 MagickStatusType
5214 flags;
5215
5216 ssize_t
5217 y;
5218
cristy3ed852e2009-09-05 21:47:34 +00005219 /*
5220 Allocate tint image.
5221 */
5222 assert(image != (const Image *) NULL);
5223 assert(image->signature == MagickSignature);
5224 if (image->debug != MagickFalse)
5225 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5226 assert(exception != (ExceptionInfo *) NULL);
5227 assert(exception->signature == MagickSignature);
5228 tint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5229 if (tint_image == (Image *) NULL)
5230 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005231 if (SetImageStorageClass(tint_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005232 {
cristy3ed852e2009-09-05 21:47:34 +00005233 tint_image=DestroyImage(tint_image);
5234 return((Image *) NULL);
5235 }
cristyaed9c382011-10-03 17:54:21 +00005236 if (blend == (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005237 return(tint_image);
5238 /*
5239 Determine RGB values of the color.
5240 */
cristy28474bf2011-09-11 23:32:52 +00005241 GetPixelInfo(image,&pixel);
cristyb817c3f2011-10-03 14:00:35 +00005242 flags=ParseGeometry(blend,&geometry_info);
cristy3ed852e2009-09-05 21:47:34 +00005243 pixel.red=geometry_info.rho;
cristy76f512e2011-09-12 01:26:56 +00005244 pixel.green=geometry_info.rho;
5245 pixel.blue=geometry_info.rho;
cristy76f512e2011-09-12 01:26:56 +00005246 pixel.alpha=OpaqueAlpha;
cristy3ed852e2009-09-05 21:47:34 +00005247 if ((flags & SigmaValue) != 0)
5248 pixel.green=geometry_info.sigma;
cristy3ed852e2009-09-05 21:47:34 +00005249 if ((flags & XiValue) != 0)
5250 pixel.blue=geometry_info.xi;
cristyb817c3f2011-10-03 14:00:35 +00005251 if ((flags & PsiValue) != 0)
5252 pixel.alpha=geometry_info.psi;
cristy76f512e2011-09-12 01:26:56 +00005253 if (image->colorspace == CMYKColorspace)
5254 {
cristyb817c3f2011-10-03 14:00:35 +00005255 pixel.black=geometry_info.rho;
cristy76f512e2011-09-12 01:26:56 +00005256 if ((flags & PsiValue) != 0)
5257 pixel.black=geometry_info.psi;
5258 if ((flags & ChiValue) != 0)
5259 pixel.alpha=geometry_info.chi;
5260 }
cristy28474bf2011-09-11 23:32:52 +00005261 intensity=(MagickRealType) GetPixelInfoIntensity(tint);
5262 color_vector.red=(MagickRealType) (pixel.red*tint->red/100.0-intensity);
5263 color_vector.green=(MagickRealType) (pixel.green*tint->green/100.0-intensity);
5264 color_vector.blue=(MagickRealType) (pixel.blue*tint->blue/100.0-intensity);
5265 color_vector.black=(MagickRealType) (pixel.black*tint->black/100.0-intensity);
5266 color_vector.alpha=(MagickRealType) (pixel.alpha*tint->alpha/100.0-intensity);
cristy3ed852e2009-09-05 21:47:34 +00005267 /*
5268 Tint image.
5269 */
5270 status=MagickTrue;
5271 progress=0;
5272 image_view=AcquireCacheView(image);
5273 tint_view=AcquireCacheView(tint_image);
cristyb5d5f722009-11-04 03:03:49 +00005274#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00005275 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00005276#endif
cristybb503372010-05-27 20:51:26 +00005277 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005278 {
cristy4c08aed2011-07-01 19:47:50 +00005279 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00005280 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00005281
cristy4c08aed2011-07-01 19:47:50 +00005282 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005283 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005284
cristy6b91acb2011-04-19 12:23:54 +00005285 register ssize_t
5286 x;
5287
cristy3ed852e2009-09-05 21:47:34 +00005288 if (status == MagickFalse)
5289 continue;
5290 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5291 q=QueueCacheViewAuthenticPixels(tint_view,0,y,tint_image->columns,1,
5292 exception);
cristy4c08aed2011-07-01 19:47:50 +00005293 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005294 {
5295 status=MagickFalse;
5296 continue;
5297 }
cristybb503372010-05-27 20:51:26 +00005298 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005299 {
cristy4c08aed2011-07-01 19:47:50 +00005300 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00005301 pixel;
5302
5303 MagickRealType
5304 weight;
5305
cristy76f512e2011-09-12 01:26:56 +00005306 if ((GetPixelRedTraits(tint_image) & UpdatePixelTrait) != 0)
cristy28474bf2011-09-11 23:32:52 +00005307 {
5308 weight=QuantumScale*GetPixelRed(image,p)-0.5;
5309 pixel.red=(MagickRealType) GetPixelRed(image,p)+
5310 color_vector.red*(1.0-(4.0*(weight*weight)));
5311 SetPixelRed(tint_image,ClampToQuantum(pixel.red),q);
5312 }
cristy76f512e2011-09-12 01:26:56 +00005313 if ((GetPixelGreenTraits(tint_image) & UpdatePixelTrait) != 0)
cristy28474bf2011-09-11 23:32:52 +00005314 {
5315 weight=QuantumScale*GetPixelGreen(image,p)-0.5;
5316 pixel.green=(MagickRealType) GetPixelGreen(image,p)+
5317 color_vector.green*(1.0-(4.0*(weight*weight)));
5318 SetPixelGreen(tint_image,ClampToQuantum(pixel.green),q);
5319 }
cristy76f512e2011-09-12 01:26:56 +00005320 if ((GetPixelBlueTraits(tint_image) & UpdatePixelTrait) != 0)
cristy28474bf2011-09-11 23:32:52 +00005321 {
5322 weight=QuantumScale*GetPixelBlue(image,p)-0.5;
5323 pixel.blue=(MagickRealType) GetPixelBlue(image,p)+
5324 color_vector.blue*(1.0-(4.0*(weight*weight)));
5325 SetPixelBlue(tint_image,ClampToQuantum(pixel.blue),q);
5326 }
cristy76f512e2011-09-12 01:26:56 +00005327 if ((GetPixelBlackTraits(tint_image) & UpdatePixelTrait) != 0)
cristy28474bf2011-09-11 23:32:52 +00005328 {
5329 weight=QuantumScale*GetPixelBlack(image,p)-0.5;
5330 pixel.black=(MagickRealType) GetPixelBlack(image,p)+
5331 color_vector.black*(1.0-(4.0*(weight*weight)));
5332 SetPixelBlack(tint_image,ClampToQuantum(pixel.black),q);
5333 }
cristy76f512e2011-09-12 01:26:56 +00005334 if ((GetPixelAlphaTraits(tint_image) & CopyPixelTrait) != 0)
cristy28474bf2011-09-11 23:32:52 +00005335 SetPixelAlpha(tint_image,GetPixelAlpha(image,p),q);
cristyed231572011-07-14 02:18:59 +00005336 p+=GetPixelChannels(image);
5337 q+=GetPixelChannels(tint_image);
cristy3ed852e2009-09-05 21:47:34 +00005338 }
5339 if (SyncCacheViewAuthenticPixels(tint_view,exception) == MagickFalse)
5340 status=MagickFalse;
5341 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5342 {
5343 MagickBooleanType
5344 proceed;
5345
cristyb5d5f722009-11-04 03:03:49 +00005346#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00005347 #pragma omp critical (MagickCore_TintImage)
5348#endif
5349 proceed=SetImageProgress(image,TintImageTag,progress++,image->rows);
5350 if (proceed == MagickFalse)
5351 status=MagickFalse;
5352 }
5353 }
5354 tint_view=DestroyCacheView(tint_view);
5355 image_view=DestroyCacheView(image_view);
5356 if (status == MagickFalse)
5357 tint_image=DestroyImage(tint_image);
5358 return(tint_image);
5359}
5360
5361/*
5362%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5363% %
5364% %
5365% %
5366% V i g n e t t e I m a g e %
5367% %
5368% %
5369% %
5370%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5371%
5372% VignetteImage() softens the edges of the image in vignette style.
5373%
5374% The format of the VignetteImage method is:
5375%
5376% Image *VignetteImage(const Image *image,const double radius,
cristyeb6e6582011-12-09 09:14:23 +00005377% const double sigma,const double bias,const ssize_t x,const ssize_t y,
cristy05c0c9a2011-09-05 23:16:13 +00005378% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005379%
5380% A description of each parameter follows:
5381%
5382% o image: the image.
5383%
5384% o radius: the radius of the pixel neighborhood.
5385%
5386% o sigma: the standard deviation of the Gaussian, in pixels.
5387%
cristyeb6e6582011-12-09 09:14:23 +00005388% o bias: the bias.
5389%
cristy3ed852e2009-09-05 21:47:34 +00005390% o x, y: Define the x and y ellipse offset.
5391%
5392% o exception: return any errors or warnings in this structure.
5393%
5394*/
5395MagickExport Image *VignetteImage(const Image *image,const double radius,
cristyeb6e6582011-12-09 09:14:23 +00005396 const double sigma,const double bias,const ssize_t x,const ssize_t y,
5397 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005398{
5399 char
5400 ellipse[MaxTextExtent];
5401
5402 DrawInfo
5403 *draw_info;
5404
5405 Image
5406 *canvas_image,
5407 *blur_image,
5408 *oval_image,
5409 *vignette_image;
5410
5411 assert(image != (Image *) NULL);
5412 assert(image->signature == MagickSignature);
5413 if (image->debug != MagickFalse)
5414 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5415 assert(exception != (ExceptionInfo *) NULL);
5416 assert(exception->signature == MagickSignature);
5417 canvas_image=CloneImage(image,0,0,MagickTrue,exception);
5418 if (canvas_image == (Image *) NULL)
5419 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005420 if (SetImageStorageClass(canvas_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005421 {
cristy3ed852e2009-09-05 21:47:34 +00005422 canvas_image=DestroyImage(canvas_image);
5423 return((Image *) NULL);
5424 }
5425 canvas_image->matte=MagickTrue;
cristy98621462011-12-31 22:31:11 +00005426 oval_image=CloneImage(canvas_image,canvas_image->columns,canvas_image->rows,
5427 MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005428 if (oval_image == (Image *) NULL)
5429 {
5430 canvas_image=DestroyImage(canvas_image);
5431 return((Image *) NULL);
5432 }
cristy9950d572011-10-01 18:22:35 +00005433 (void) QueryColorCompliance("#000000",AllCompliance,
5434 &oval_image->background_color,exception);
cristyea1a8aa2011-10-20 13:24:06 +00005435 (void) SetImageBackgroundColor(oval_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00005436 draw_info=CloneDrawInfo((const ImageInfo *) NULL,(const DrawInfo *) NULL);
cristy9950d572011-10-01 18:22:35 +00005437 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->fill,
5438 exception);
5439 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->stroke,
5440 exception);
cristyb51dff52011-05-19 16:55:47 +00005441 (void) FormatLocaleString(ellipse,MaxTextExtent,
cristye7f51092010-01-17 00:39:37 +00005442 "ellipse %g,%g,%g,%g,0.0,360.0",image->columns/2.0,
cristy8cd5b312010-01-07 01:10:24 +00005443 image->rows/2.0,image->columns/2.0-x,image->rows/2.0-y);
cristy3ed852e2009-09-05 21:47:34 +00005444 draw_info->primitive=AcquireString(ellipse);
cristy018f07f2011-09-04 21:15:19 +00005445 (void) DrawImage(oval_image,draw_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00005446 draw_info=DestroyDrawInfo(draw_info);
cristyeb6e6582011-12-09 09:14:23 +00005447 blur_image=BlurImage(oval_image,radius,sigma,bias,exception);
cristy3ed852e2009-09-05 21:47:34 +00005448 oval_image=DestroyImage(oval_image);
5449 if (blur_image == (Image *) NULL)
5450 {
5451 canvas_image=DestroyImage(canvas_image);
5452 return((Image *) NULL);
5453 }
5454 blur_image->matte=MagickFalse;
cristy98621462011-12-31 22:31:11 +00005455 (void) CompositeImage(canvas_image,IntensityCompositeOp,blur_image,0,0,
cristye941a752011-10-15 01:52:48 +00005456 exception);
cristy3ed852e2009-09-05 21:47:34 +00005457 blur_image=DestroyImage(blur_image);
5458 vignette_image=MergeImageLayers(canvas_image,FlattenLayer,exception);
5459 canvas_image=DestroyImage(canvas_image);
5460 return(vignette_image);
5461}
5462
5463/*
5464%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5465% %
5466% %
5467% %
5468% W a v e I m a g e %
5469% %
5470% %
5471% %
5472%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5473%
5474% WaveImage() creates a "ripple" effect in the image by shifting the pixels
cristycee97112010-05-28 00:44:52 +00005475% vertically along a sine wave whose amplitude and wavelength is specified
cristy3ed852e2009-09-05 21:47:34 +00005476% by the given parameters.
5477%
5478% The format of the WaveImage method is:
5479%
5480% Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005481% const double wave_length,const PixelInterpolateMethod method,
5482% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005483%
5484% A description of each parameter follows:
5485%
5486% o image: the image.
5487%
5488% o amplitude, wave_length: Define the amplitude and wave length of the
5489% sine wave.
5490%
cristy5c4e2582011-09-11 19:21:03 +00005491% o interpolate: the pixel interpolation method.
5492%
cristy3ed852e2009-09-05 21:47:34 +00005493% o exception: return any errors or warnings in this structure.
5494%
5495*/
5496MagickExport Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005497 const double wave_length,const PixelInterpolateMethod method,
5498 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005499{
5500#define WaveImageTag "Wave/Image"
5501
cristyfa112112010-01-04 17:48:07 +00005502 CacheView
cristyd76c51e2011-03-26 00:21:26 +00005503 *image_view,
cristyfa112112010-01-04 17:48:07 +00005504 *wave_view;
5505
cristy3ed852e2009-09-05 21:47:34 +00005506 Image
5507 *wave_image;
5508
cristy3ed852e2009-09-05 21:47:34 +00005509 MagickBooleanType
5510 status;
5511
cristybb503372010-05-27 20:51:26 +00005512 MagickOffsetType
5513 progress;
5514
cristy3ed852e2009-09-05 21:47:34 +00005515 MagickRealType
5516 *sine_map;
5517
cristybb503372010-05-27 20:51:26 +00005518 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005519 i;
5520
cristybb503372010-05-27 20:51:26 +00005521 ssize_t
5522 y;
5523
cristy3ed852e2009-09-05 21:47:34 +00005524 /*
5525 Initialize wave image attributes.
5526 */
5527 assert(image != (Image *) NULL);
5528 assert(image->signature == MagickSignature);
5529 if (image->debug != MagickFalse)
5530 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5531 assert(exception != (ExceptionInfo *) NULL);
5532 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00005533 wave_image=CloneImage(image,image->columns,(size_t) (image->rows+2.0*
cristy3ed852e2009-09-05 21:47:34 +00005534 fabs(amplitude)),MagickTrue,exception);
5535 if (wave_image == (Image *) NULL)
5536 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005537 if (SetImageStorageClass(wave_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005538 {
cristy3ed852e2009-09-05 21:47:34 +00005539 wave_image=DestroyImage(wave_image);
5540 return((Image *) NULL);
5541 }
cristy4c08aed2011-07-01 19:47:50 +00005542 if (wave_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00005543 wave_image->matte=MagickTrue;
5544 /*
5545 Allocate sine map.
5546 */
5547 sine_map=(MagickRealType *) AcquireQuantumMemory((size_t) wave_image->columns,
5548 sizeof(*sine_map));
5549 if (sine_map == (MagickRealType *) NULL)
5550 {
5551 wave_image=DestroyImage(wave_image);
5552 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
5553 }
cristybb503372010-05-27 20:51:26 +00005554 for (i=0; i < (ssize_t) wave_image->columns; i++)
cristy4205a3c2010-09-12 20:19:59 +00005555 sine_map[i]=fabs(amplitude)+amplitude*sin((double) ((2.0*MagickPI*i)/
5556 wave_length));
cristy3ed852e2009-09-05 21:47:34 +00005557 /*
5558 Wave image.
5559 */
5560 status=MagickTrue;
5561 progress=0;
cristyd76c51e2011-03-26 00:21:26 +00005562 image_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00005563 wave_view=AcquireCacheView(wave_image);
cristyd76c51e2011-03-26 00:21:26 +00005564 (void) SetCacheViewVirtualPixelMethod(image_view,
5565 BackgroundVirtualPixelMethod);
cristyb5d5f722009-11-04 03:03:49 +00005566#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00005567 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00005568#endif
cristybb503372010-05-27 20:51:26 +00005569 for (y=0; y < (ssize_t) wave_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005570 {
cristy4c08aed2011-07-01 19:47:50 +00005571 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005572 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005573
cristye97bb922011-04-03 01:36:52 +00005574 register ssize_t
5575 x;
5576
cristy3ed852e2009-09-05 21:47:34 +00005577 if (status == MagickFalse)
5578 continue;
5579 q=QueueCacheViewAuthenticPixels(wave_view,0,y,wave_image->columns,1,
5580 exception);
cristyacd2ed22011-08-30 01:44:23 +00005581 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005582 {
5583 status=MagickFalse;
5584 continue;
5585 }
cristybb503372010-05-27 20:51:26 +00005586 for (x=0; x < (ssize_t) wave_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005587 {
cristy5c4e2582011-09-11 19:21:03 +00005588 status=InterpolatePixelChannels(image,image_view,wave_image,method,
5589 (double) x,(double) (y-sine_map[x]),q,exception);
cristyed231572011-07-14 02:18:59 +00005590 q+=GetPixelChannels(wave_image);
cristy3ed852e2009-09-05 21:47:34 +00005591 }
5592 if (SyncCacheViewAuthenticPixels(wave_view,exception) == MagickFalse)
5593 status=MagickFalse;
5594 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5595 {
5596 MagickBooleanType
5597 proceed;
5598
cristyb5d5f722009-11-04 03:03:49 +00005599#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00005600 #pragma omp critical (MagickCore_WaveImage)
5601#endif
5602 proceed=SetImageProgress(image,WaveImageTag,progress++,image->rows);
5603 if (proceed == MagickFalse)
5604 status=MagickFalse;
5605 }
5606 }
5607 wave_view=DestroyCacheView(wave_view);
cristyd76c51e2011-03-26 00:21:26 +00005608 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00005609 sine_map=(MagickRealType *) RelinquishMagickMemory(sine_map);
5610 if (status == MagickFalse)
5611 wave_image=DestroyImage(wave_image);
5612 return(wave_image);
5613}