blob: 3ac1c15e4be825acfca62ef27c8ec894c47e150d [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"
cristy9b7a4fc2012-04-08 22:26:56 +000051#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000052#include "MagickCore/composite.h"
53#include "MagickCore/decorate.h"
cristyc53413d2011-11-17 13:04:26 +000054#include "MagickCore/distort.h"
cristy4c08aed2011-07-01 19:47:50 +000055#include "MagickCore/draw.h"
56#include "MagickCore/effect.h"
57#include "MagickCore/enhance.h"
58#include "MagickCore/exception.h"
59#include "MagickCore/exception-private.h"
60#include "MagickCore/fx.h"
61#include "MagickCore/fx-private.h"
62#include "MagickCore/gem.h"
cristy8ea81222011-09-04 10:33:32 +000063#include "MagickCore/gem-private.h"
cristy4c08aed2011-07-01 19:47:50 +000064#include "MagickCore/geometry.h"
65#include "MagickCore/layer.h"
66#include "MagickCore/list.h"
67#include "MagickCore/log.h"
68#include "MagickCore/image.h"
69#include "MagickCore/image-private.h"
70#include "MagickCore/magick.h"
71#include "MagickCore/memory_.h"
72#include "MagickCore/monitor.h"
73#include "MagickCore/monitor-private.h"
74#include "MagickCore/option.h"
75#include "MagickCore/pixel.h"
76#include "MagickCore/pixel-accessor.h"
77#include "MagickCore/property.h"
78#include "MagickCore/quantum.h"
79#include "MagickCore/quantum-private.h"
80#include "MagickCore/random_.h"
81#include "MagickCore/random-private.h"
82#include "MagickCore/resample.h"
83#include "MagickCore/resample-private.h"
84#include "MagickCore/resize.h"
cristy4c08aed2011-07-01 19:47:50 +000085#include "MagickCore/splay-tree.h"
86#include "MagickCore/statistic.h"
87#include "MagickCore/string_.h"
88#include "MagickCore/string-private.h"
89#include "MagickCore/thread-private.h"
90#include "MagickCore/transform.h"
91#include "MagickCore/utility.h"
cristy3ed852e2009-09-05 21:47:34 +000092
93/*
94 Define declarations.
95*/
96#define LeftShiftOperator 0xf5
97#define RightShiftOperator 0xf6
98#define LessThanEqualOperator 0xf7
99#define GreaterThanEqualOperator 0xf8
100#define EqualOperator 0xf9
101#define NotEqualOperator 0xfa
102#define LogicalAndOperator 0xfb
103#define LogicalOrOperator 0xfc
cristy116af162010-08-13 01:25:47 +0000104#define ExponentialNotation 0xfd
cristy3ed852e2009-09-05 21:47:34 +0000105
106struct _FxInfo
107{
108 const Image
109 *images;
110
cristy3ed852e2009-09-05 21:47:34 +0000111 char
112 *expression;
113
114 FILE
115 *file;
116
117 SplayTreeInfo
118 *colors,
119 *symbols;
120
cristyd76c51e2011-03-26 00:21:26 +0000121 CacheView
122 **view;
cristy3ed852e2009-09-05 21:47:34 +0000123
124 RandomInfo
125 *random_info;
126
127 ExceptionInfo
128 *exception;
129};
130
131/*
132%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
133% %
134% %
135% %
136+ A c q u i r e F x I n f o %
137% %
138% %
139% %
140%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
141%
142% AcquireFxInfo() allocates the FxInfo structure.
143%
144% The format of the AcquireFxInfo method is:
145%
146% FxInfo *AcquireFxInfo(Image *image,const char *expression)
cristy0a9b3722010-10-23 18:45:49 +0000147%
cristy3ed852e2009-09-05 21:47:34 +0000148% A description of each parameter follows:
149%
150% o image: the image.
151%
152% o expression: the expression.
153%
154*/
cristy7832dc22011-09-05 01:21:53 +0000155MagickPrivate FxInfo *AcquireFxInfo(const Image *image,const char *expression)
cristy3ed852e2009-09-05 21:47:34 +0000156{
157 char
158 fx_op[2];
159
cristycb180922011-03-11 14:41:24 +0000160 const Image
161 *next;
162
cristy3ed852e2009-09-05 21:47:34 +0000163 FxInfo
164 *fx_info;
165
cristybb503372010-05-27 20:51:26 +0000166 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000167 i;
168
cristy73bd4a52010-10-05 11:24:23 +0000169 fx_info=(FxInfo *) AcquireMagickMemory(sizeof(*fx_info));
cristy3ed852e2009-09-05 21:47:34 +0000170 if (fx_info == (FxInfo *) NULL)
171 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
172 (void) ResetMagickMemory(fx_info,0,sizeof(*fx_info));
173 fx_info->exception=AcquireExceptionInfo();
anthony7d86e172011-03-23 12:37:06 +0000174 fx_info->images=image;
cristy3ed852e2009-09-05 21:47:34 +0000175 fx_info->colors=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
176 RelinquishMagickMemory);
177 fx_info->symbols=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
178 RelinquishMagickMemory);
cristyd76c51e2011-03-26 00:21:26 +0000179 fx_info->view=(CacheView **) AcquireQuantumMemory(GetImageListLength(
180 fx_info->images),sizeof(*fx_info->view));
181 if (fx_info->view == (CacheView **) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000182 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
cristya2262262011-03-11 02:50:37 +0000183 i=0;
cristy0ea377f2011-03-24 00:54:19 +0000184 next=GetFirstImageInList(fx_info->images);
185 for ( ; next != (Image *) NULL; next=next->next)
cristy3ed852e2009-09-05 21:47:34 +0000186 {
cristyd76c51e2011-03-26 00:21:26 +0000187 fx_info->view[i]=AcquireCacheView(next);
cristya2262262011-03-11 02:50:37 +0000188 i++;
cristy3ed852e2009-09-05 21:47:34 +0000189 }
190 fx_info->random_info=AcquireRandomInfo();
191 fx_info->expression=ConstantString(expression);
192 fx_info->file=stderr;
193 (void) SubstituteString(&fx_info->expression," ",""); /* compact string */
cristy37af0912011-05-23 16:09:42 +0000194 /*
195 Force right-to-left associativity for unary negation.
196 */
197 (void) SubstituteString(&fx_info->expression,"-","-1.0*");
cristy8b8a3ae2010-10-23 18:49:46 +0000198 /*
cristy3ed852e2009-09-05 21:47:34 +0000199 Convert complex to simple operators.
200 */
201 fx_op[1]='\0';
202 *fx_op=(char) LeftShiftOperator;
203 (void) SubstituteString(&fx_info->expression,"<<",fx_op);
204 *fx_op=(char) RightShiftOperator;
205 (void) SubstituteString(&fx_info->expression,">>",fx_op);
206 *fx_op=(char) LessThanEqualOperator;
207 (void) SubstituteString(&fx_info->expression,"<=",fx_op);
208 *fx_op=(char) GreaterThanEqualOperator;
209 (void) SubstituteString(&fx_info->expression,">=",fx_op);
210 *fx_op=(char) EqualOperator;
211 (void) SubstituteString(&fx_info->expression,"==",fx_op);
212 *fx_op=(char) NotEqualOperator;
213 (void) SubstituteString(&fx_info->expression,"!=",fx_op);
214 *fx_op=(char) LogicalAndOperator;
215 (void) SubstituteString(&fx_info->expression,"&&",fx_op);
216 *fx_op=(char) LogicalOrOperator;
217 (void) SubstituteString(&fx_info->expression,"||",fx_op);
cristy116af162010-08-13 01:25:47 +0000218 *fx_op=(char) ExponentialNotation;
219 (void) SubstituteString(&fx_info->expression,"**",fx_op);
cristy3ed852e2009-09-05 21:47:34 +0000220 return(fx_info);
221}
222
223/*
224%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
225% %
226% %
227% %
228% A d d N o i s e I m a g e %
229% %
230% %
231% %
232%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
233%
234% AddNoiseImage() adds random noise to the image.
235%
236% The format of the AddNoiseImage method is:
237%
238% Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
cristy9ed1f812011-10-08 02:00:08 +0000239% const double attenuate,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000240%
241% A description of each parameter follows:
242%
243% o image: the image.
244%
245% o channel: the channel type.
246%
247% o noise_type: The type of noise: Uniform, Gaussian, Multiplicative,
248% Impulse, Laplacian, or Poisson.
249%
cristy9ed1f812011-10-08 02:00:08 +0000250% o attenuate: attenuate the random distribution.
251%
cristy3ed852e2009-09-05 21:47:34 +0000252% o exception: return any errors or warnings in this structure.
253%
254*/
cristy9ed1f812011-10-08 02:00:08 +0000255MagickExport Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
256 const double attenuate,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000257{
258#define AddNoiseImageTag "AddNoise/Image"
259
cristyfa112112010-01-04 17:48:07 +0000260 CacheView
261 *image_view,
262 *noise_view;
263
cristy3ed852e2009-09-05 21:47:34 +0000264 Image
265 *noise_image;
266
cristy3ed852e2009-09-05 21:47:34 +0000267 MagickBooleanType
268 status;
269
cristybb503372010-05-27 20:51:26 +0000270 MagickOffsetType
271 progress;
272
cristy3ed852e2009-09-05 21:47:34 +0000273 RandomInfo
cristyfa112112010-01-04 17:48:07 +0000274 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +0000275
cristybb503372010-05-27 20:51:26 +0000276 ssize_t
277 y;
278
cristy3ed852e2009-09-05 21:47:34 +0000279 /*
280 Initialize noise image attributes.
281 */
282 assert(image != (const Image *) NULL);
283 assert(image->signature == MagickSignature);
284 if (image->debug != MagickFalse)
285 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
286 assert(exception != (ExceptionInfo *) NULL);
287 assert(exception->signature == MagickSignature);
cristyb2145892011-10-10 00:55:32 +0000288 noise_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000289 if (noise_image == (Image *) NULL)
290 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000291 if (SetImageStorageClass(noise_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000292 {
cristy3ed852e2009-09-05 21:47:34 +0000293 noise_image=DestroyImage(noise_image);
294 return((Image *) NULL);
295 }
296 /*
297 Add noise in each row.
298 */
cristy3ed852e2009-09-05 21:47:34 +0000299 status=MagickTrue;
300 progress=0;
301 random_info=AcquireRandomInfoThreadSet();
302 image_view=AcquireCacheView(image);
303 noise_view=AcquireCacheView(noise_image);
cristy319a1e72010-02-21 15:13:11 +0000304#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000305 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000306#endif
cristybb503372010-05-27 20:51:26 +0000307 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000308 {
cristy5c9e6f22010-09-17 17:31:01 +0000309 const int
310 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +0000311
cristy3ed852e2009-09-05 21:47:34 +0000312 MagickBooleanType
313 sync;
314
cristy4c08aed2011-07-01 19:47:50 +0000315 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000316 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000317
cristybb503372010-05-27 20:51:26 +0000318 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000319 x;
320
cristy4c08aed2011-07-01 19:47:50 +0000321 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000322 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000323
324 if (status == MagickFalse)
325 continue;
326 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +0000327 q=QueueCacheViewAuthenticPixels(noise_view,0,y,noise_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +0000328 exception);
cristy4c08aed2011-07-01 19:47:50 +0000329 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000330 {
331 status=MagickFalse;
332 continue;
333 }
cristybb503372010-05-27 20:51:26 +0000334 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000335 {
cristy850b3072011-10-08 01:38:05 +0000336 register ssize_t
337 i;
338
339 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
340 {
341 PixelChannel
342 channel;
343
344 PixelTrait
345 noise_traits,
346 traits;
347
cristye2a912b2011-12-05 20:02:07 +0000348 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +0000349 traits=GetPixelChannelMapTraits(image,channel);
cristy850b3072011-10-08 01:38:05 +0000350 noise_traits=GetPixelChannelMapTraits(noise_image,channel);
351 if ((traits == UndefinedPixelTrait) ||
352 (noise_traits == UndefinedPixelTrait))
353 continue;
cristyec9e3a62012-02-01 02:09:32 +0000354 if (((noise_traits & CopyPixelTrait) != 0) ||
355 (GetPixelMask(image,p) != 0))
cristyb2145892011-10-10 00:55:32 +0000356 {
357 SetPixelChannel(noise_image,channel,p[i],q);
358 continue;
359 }
cristy850b3072011-10-08 01:38:05 +0000360 SetPixelChannel(noise_image,channel,ClampToQuantum(
361 GenerateDifferentialNoise(random_info[id],p[i],noise_type,attenuate)),
362 q);
363 }
cristyed231572011-07-14 02:18:59 +0000364 p+=GetPixelChannels(image);
365 q+=GetPixelChannels(noise_image);
cristy3ed852e2009-09-05 21:47:34 +0000366 }
367 sync=SyncCacheViewAuthenticPixels(noise_view,exception);
368 if (sync == MagickFalse)
369 status=MagickFalse;
370 if (image->progress_monitor != (MagickProgressMonitor) NULL)
371 {
372 MagickBooleanType
373 proceed;
374
cristyb5d5f722009-11-04 03:03:49 +0000375#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy319a1e72010-02-21 15:13:11 +0000376 #pragma omp critical (MagickCore_AddNoiseImage)
cristy3ed852e2009-09-05 21:47:34 +0000377#endif
378 proceed=SetImageProgress(image,AddNoiseImageTag,progress++,
379 image->rows);
380 if (proceed == MagickFalse)
381 status=MagickFalse;
382 }
383 }
384 noise_view=DestroyCacheView(noise_view);
385 image_view=DestroyCacheView(image_view);
386 random_info=DestroyRandomInfoThreadSet(random_info);
387 if (status == MagickFalse)
388 noise_image=DestroyImage(noise_image);
389 return(noise_image);
390}
391
392/*
393%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
394% %
395% %
396% %
397% B l u e S h i f t I m a g e %
398% %
399% %
400% %
401%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
402%
403% BlueShiftImage() mutes the colors of the image to simulate a scene at
404% nighttime in the moonlight.
405%
406% The format of the BlueShiftImage method is:
407%
408% Image *BlueShiftImage(const Image *image,const double factor,
409% ExceptionInfo *exception)
410%
411% A description of each parameter follows:
412%
413% o image: the image.
414%
415% o factor: the shift factor.
416%
417% o exception: return any errors or warnings in this structure.
418%
419*/
420MagickExport Image *BlueShiftImage(const Image *image,const double factor,
421 ExceptionInfo *exception)
422{
423#define BlueShiftImageTag "BlueShift/Image"
424
cristyc4c8d132010-01-07 01:58:38 +0000425 CacheView
426 *image_view,
427 *shift_view;
428
cristy3ed852e2009-09-05 21:47:34 +0000429 Image
430 *shift_image;
431
cristy3ed852e2009-09-05 21:47:34 +0000432 MagickBooleanType
433 status;
434
cristybb503372010-05-27 20:51:26 +0000435 MagickOffsetType
436 progress;
437
438 ssize_t
439 y;
440
cristy3ed852e2009-09-05 21:47:34 +0000441 /*
442 Allocate blue shift image.
443 */
444 assert(image != (const Image *) NULL);
445 assert(image->signature == MagickSignature);
446 if (image->debug != MagickFalse)
447 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
448 assert(exception != (ExceptionInfo *) NULL);
449 assert(exception->signature == MagickSignature);
cristya6d7a9b2012-01-18 20:04:48 +0000450 shift_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000451 if (shift_image == (Image *) NULL)
452 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000453 if (SetImageStorageClass(shift_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000454 {
cristy3ed852e2009-09-05 21:47:34 +0000455 shift_image=DestroyImage(shift_image);
456 return((Image *) NULL);
457 }
458 /*
459 Blue-shift DirectClass image.
460 */
461 status=MagickTrue;
462 progress=0;
463 image_view=AcquireCacheView(image);
464 shift_view=AcquireCacheView(shift_image);
cristy319a1e72010-02-21 15:13:11 +0000465#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000466 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000467#endif
cristybb503372010-05-27 20:51:26 +0000468 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000469 {
470 MagickBooleanType
471 sync;
472
cristy4c08aed2011-07-01 19:47:50 +0000473 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000474 pixel;
475
476 Quantum
477 quantum;
478
cristy4c08aed2011-07-01 19:47:50 +0000479 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000480 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000481
cristybb503372010-05-27 20:51:26 +0000482 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000483 x;
484
cristy4c08aed2011-07-01 19:47:50 +0000485 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000486 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000487
488 if (status == MagickFalse)
489 continue;
490 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
491 q=QueueCacheViewAuthenticPixels(shift_view,0,y,shift_image->columns,1,
492 exception);
cristy4c08aed2011-07-01 19:47:50 +0000493 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000494 {
495 status=MagickFalse;
496 continue;
497 }
cristybb503372010-05-27 20:51:26 +0000498 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000499 {
cristy4c08aed2011-07-01 19:47:50 +0000500 quantum=GetPixelRed(image,p);
501 if (GetPixelGreen(image,p) < quantum)
502 quantum=GetPixelGreen(image,p);
503 if (GetPixelBlue(image,p) < quantum)
504 quantum=GetPixelBlue(image,p);
505 pixel.red=0.5*(GetPixelRed(image,p)+factor*quantum);
506 pixel.green=0.5*(GetPixelGreen(image,p)+factor*quantum);
507 pixel.blue=0.5*(GetPixelBlue(image,p)+factor*quantum);
508 quantum=GetPixelRed(image,p);
509 if (GetPixelGreen(image,p) > quantum)
510 quantum=GetPixelGreen(image,p);
511 if (GetPixelBlue(image,p) > quantum)
512 quantum=GetPixelBlue(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000513 pixel.red=0.5*(pixel.red+factor*quantum);
514 pixel.green=0.5*(pixel.green+factor*quantum);
515 pixel.blue=0.5*(pixel.blue+factor*quantum);
cristy4c08aed2011-07-01 19:47:50 +0000516 SetPixelRed(shift_image,ClampToQuantum(pixel.red),q);
517 SetPixelGreen(shift_image,ClampToQuantum(pixel.green),q);
518 SetPixelBlue(shift_image,ClampToQuantum(pixel.blue),q);
cristyed231572011-07-14 02:18:59 +0000519 p+=GetPixelChannels(image);
520 q+=GetPixelChannels(shift_image);
cristy3ed852e2009-09-05 21:47:34 +0000521 }
522 sync=SyncCacheViewAuthenticPixels(shift_view,exception);
523 if (sync == MagickFalse)
524 status=MagickFalse;
525 if (image->progress_monitor != (MagickProgressMonitor) NULL)
526 {
527 MagickBooleanType
528 proceed;
529
cristy319a1e72010-02-21 15:13:11 +0000530#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +0000531 #pragma omp critical (MagickCore_BlueShiftImage)
cristy3ed852e2009-09-05 21:47:34 +0000532#endif
533 proceed=SetImageProgress(image,BlueShiftImageTag,progress++,
534 image->rows);
535 if (proceed == MagickFalse)
536 status=MagickFalse;
537 }
538 }
539 image_view=DestroyCacheView(image_view);
540 shift_view=DestroyCacheView(shift_view);
541 if (status == MagickFalse)
542 shift_image=DestroyImage(shift_image);
543 return(shift_image);
544}
545
546/*
547%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
548% %
549% %
550% %
551% C h a r c o a l I m a g e %
552% %
553% %
554% %
555%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
556%
557% CharcoalImage() creates a new image that is a copy of an existing one with
558% the edge highlighted. It allocates the memory necessary for the new Image
559% structure and returns a pointer to the new image.
560%
561% The format of the CharcoalImage method is:
562%
563% Image *CharcoalImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +0000564% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000565%
566% A description of each parameter follows:
567%
568% o image: the image.
569%
570% o radius: the radius of the pixel neighborhood.
571%
572% o sigma: the standard deviation of the Gaussian, in pixels.
573%
574% o exception: return any errors or warnings in this structure.
575%
576*/
577MagickExport Image *CharcoalImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +0000578 const double sigma,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);
cristyaa2c16c2012-03-25 22:21:35 +0000599 charcoal_image=BlurImage(edge_image,radius,sigma,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"
cristya50544c2012-04-07 18:25:41 +0000646#define Colorize(pixel,fill_color,colorize) \
647 (pixel)=((pixel)*(100.0-(fill_color))+(colorize)*(fill_color))/100.0;
cristy3ed852e2009-09-05 21:47:34 +0000648
cristyc4c8d132010-01-07 01:58:38 +0000649 CacheView
650 *colorize_view,
651 *image_view;
652
cristy3ed852e2009-09-05 21:47:34 +0000653 GeometryInfo
654 geometry_info;
655
656 Image
657 *colorize_image;
658
cristy3ed852e2009-09-05 21:47:34 +0000659 MagickBooleanType
660 status;
661
cristybb503372010-05-27 20:51:26 +0000662 MagickOffsetType
663 progress;
664
cristy3ed852e2009-09-05 21:47:34 +0000665 MagickStatusType
666 flags;
667
cristyc7e6ff62011-10-03 13:46:11 +0000668 PixelInfo
cristyf9bf43e2012-04-07 18:13:07 +0000669 fill_color;
cristyc7e6ff62011-10-03 13:46:11 +0000670
cristybb503372010-05-27 20:51:26 +0000671 ssize_t
672 y;
673
cristy3ed852e2009-09-05 21:47:34 +0000674 /*
675 Allocate colorized image.
676 */
677 assert(image != (const Image *) NULL);
678 assert(image->signature == MagickSignature);
679 if (image->debug != MagickFalse)
680 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
681 assert(exception != (ExceptionInfo *) NULL);
682 assert(exception->signature == MagickSignature);
683 colorize_image=CloneImage(image,image->columns,image->rows,MagickTrue,
684 exception);
685 if (colorize_image == (Image *) NULL)
686 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000687 if (SetImageStorageClass(colorize_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000688 {
cristy3ed852e2009-09-05 21:47:34 +0000689 colorize_image=DestroyImage(colorize_image);
690 return((Image *) NULL);
691 }
cristy5b67d4e2012-02-07 19:43:53 +0000692 if ((colorize->matte != MagickFalse) &&
693 (colorize_image->matte == MagickFalse))
694 (void) SetImageAlpha(colorize_image,OpaqueAlpha,exception);
cristyc7e6ff62011-10-03 13:46:11 +0000695 if (blend == (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000696 return(colorize_image);
697 /*
anthonyfd706f92012-01-19 04:22:02 +0000698 Determine RGB values of the fill color for pixel
cristy3ed852e2009-09-05 21:47:34 +0000699 */
cristyf9bf43e2012-04-07 18:13:07 +0000700 GetPixelInfo(image,&fill_color);
cristyb817c3f2011-10-03 14:00:35 +0000701 flags=ParseGeometry(blend,&geometry_info);
cristyf9bf43e2012-04-07 18:13:07 +0000702 fill_color.red=geometry_info.rho;
703 fill_color.green=geometry_info.rho;
704 fill_color.blue=geometry_info.rho;
705 fill_color.black=geometry_info.rho;
706 fill_color.alpha=100.0;
cristy3ed852e2009-09-05 21:47:34 +0000707 if ((flags & SigmaValue) != 0)
cristyf9bf43e2012-04-07 18:13:07 +0000708 fill_color.green=geometry_info.sigma;
cristy3ed852e2009-09-05 21:47:34 +0000709 if ((flags & XiValue) != 0)
cristyf9bf43e2012-04-07 18:13:07 +0000710 fill_color.blue=geometry_info.xi;
cristy3ed852e2009-09-05 21:47:34 +0000711 if ((flags & PsiValue) != 0)
cristyf9bf43e2012-04-07 18:13:07 +0000712 fill_color.alpha=geometry_info.psi;
713 if (fill_color.colorspace == CMYKColorspace)
cristyc7e6ff62011-10-03 13:46:11 +0000714 {
cristyc7e6ff62011-10-03 13:46:11 +0000715 if ((flags & PsiValue) != 0)
cristyf9bf43e2012-04-07 18:13:07 +0000716 fill_color.black=geometry_info.psi;
cristyc7e6ff62011-10-03 13:46:11 +0000717 if ((flags & ChiValue) != 0)
cristyf9bf43e2012-04-07 18:13:07 +0000718 fill_color.alpha=geometry_info.chi;
cristyc7e6ff62011-10-03 13:46:11 +0000719 }
cristy9b7a4fc2012-04-08 22:26:56 +0000720 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
cristyf9bf43e2012-04-07 18:13:07 +0000721 (IsPixelInfoGray(&fill_color) != MagickFalse))
722 colorize_image->colorspace=sRGBColorspace;
cristy3ed852e2009-09-05 21:47:34 +0000723 /*
724 Colorize DirectClass image.
725 */
726 status=MagickTrue;
727 progress=0;
728 image_view=AcquireCacheView(image);
729 colorize_view=AcquireCacheView(colorize_image);
cristy319a1e72010-02-21 15:13:11 +0000730#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000731 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000732#endif
cristybb503372010-05-27 20:51:26 +0000733 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000734 {
735 MagickBooleanType
736 sync;
737
cristyf9bf43e2012-04-07 18:13:07 +0000738 PixelInfo
739 pixel;
740
cristy4c08aed2011-07-01 19:47:50 +0000741 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000742 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000743
cristybb503372010-05-27 20:51:26 +0000744 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000745 x;
746
cristy4c08aed2011-07-01 19:47:50 +0000747 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000748 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000749
750 if (status == MagickFalse)
751 continue;
752 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
753 q=QueueCacheViewAuthenticPixels(colorize_view,0,y,colorize_image->columns,1,
754 exception);
cristy4c08aed2011-07-01 19:47:50 +0000755 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000756 {
757 status=MagickFalse;
758 continue;
759 }
cristyf9bf43e2012-04-07 18:13:07 +0000760 GetPixelInfo(image,&pixel);
cristybb503372010-05-27 20:51:26 +0000761 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000762 {
cristyf9bf43e2012-04-07 18:13:07 +0000763 GetPixelInfoPixel(image,p,&pixel);
cristya50544c2012-04-07 18:25:41 +0000764 Colorize(pixel.red,fill_color.red,colorize->red);
765 Colorize(pixel.green,fill_color.green,colorize->green);
766 Colorize(pixel.blue,fill_color.blue,colorize->blue);
767 Colorize(pixel.black,fill_color.black,colorize->black);
768 Colorize(pixel.alpha,fill_color.alpha,colorize->alpha);
cristyf9bf43e2012-04-07 18:13:07 +0000769 SetPixelInfoPixel(colorize_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +0000770 p+=GetPixelChannels(image);
771 q+=GetPixelChannels(colorize_image);
cristy3ed852e2009-09-05 21:47:34 +0000772 }
773 sync=SyncCacheViewAuthenticPixels(colorize_view,exception);
774 if (sync == MagickFalse)
775 status=MagickFalse;
776 if (image->progress_monitor != (MagickProgressMonitor) NULL)
777 {
778 MagickBooleanType
779 proceed;
780
cristy319a1e72010-02-21 15:13:11 +0000781#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +0000782 #pragma omp critical (MagickCore_ColorizeImage)
cristy3ed852e2009-09-05 21:47:34 +0000783#endif
784 proceed=SetImageProgress(image,ColorizeImageTag,progress++,image->rows);
785 if (proceed == MagickFalse)
786 status=MagickFalse;
787 }
788 }
789 image_view=DestroyCacheView(image_view);
790 colorize_view=DestroyCacheView(colorize_view);
791 if (status == MagickFalse)
792 colorize_image=DestroyImage(colorize_image);
793 return(colorize_image);
794}
795
796/*
797%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
798% %
799% %
800% %
cristye6365592010-04-02 17:31:23 +0000801% C o l o r M a t r i x I m a g e %
802% %
803% %
804% %
805%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
806%
807% ColorMatrixImage() applies color transformation to an image. This method
808% permits saturation changes, hue rotation, luminance to alpha, and various
809% other effects. Although variable-sized transformation matrices can be used,
810% typically one uses a 5x5 matrix for an RGBA image and a 6x6 for CMYKA
811% (or RGBA with offsets). The matrix is similar to those used by Adobe Flash
812% except offsets are in column 6 rather than 5 (in support of CMYKA images)
813% and offsets are normalized (divide Flash offset by 255).
814%
815% The format of the ColorMatrixImage method is:
816%
817% Image *ColorMatrixImage(const Image *image,
818% const KernelInfo *color_matrix,ExceptionInfo *exception)
819%
820% A description of each parameter follows:
821%
822% o image: the image.
823%
824% o color_matrix: the color matrix.
825%
826% o exception: return any errors or warnings in this structure.
827%
828*/
anthonyfd706f92012-01-19 04:22:02 +0000829/* FUTURE: modify to make use of a MagickMatrix Mutliply function
830 That should be provided in "matrix.c"
831 (ASIDE: actually distorts should do this too but currently doesn't)
832*/
833
cristye6365592010-04-02 17:31:23 +0000834MagickExport Image *ColorMatrixImage(const Image *image,
835 const KernelInfo *color_matrix,ExceptionInfo *exception)
836{
837#define ColorMatrixImageTag "ColorMatrix/Image"
838
839 CacheView
840 *color_view,
841 *image_view;
842
843 double
844 ColorMatrix[6][6] =
845 {
846 { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
847 { 0.0, 1.0, 0.0, 0.0, 0.0, 0.0 },
848 { 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 },
849 { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0 },
850 { 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 },
851 { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 }
852 };
853
854 Image
855 *color_image;
856
cristye6365592010-04-02 17:31:23 +0000857 MagickBooleanType
858 status;
859
cristybb503372010-05-27 20:51:26 +0000860 MagickOffsetType
861 progress;
862
863 register ssize_t
cristye6365592010-04-02 17:31:23 +0000864 i;
865
cristybb503372010-05-27 20:51:26 +0000866 ssize_t
867 u,
868 v,
869 y;
870
cristye6365592010-04-02 17:31:23 +0000871 /*
anthonyfd706f92012-01-19 04:22:02 +0000872 Map given color_matrix, into a 6x6 matrix RGBKA and a constant
cristye6365592010-04-02 17:31:23 +0000873 */
874 assert(image != (Image *) NULL);
875 assert(image->signature == MagickSignature);
876 if (image->debug != MagickFalse)
877 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
878 assert(exception != (ExceptionInfo *) NULL);
879 assert(exception->signature == MagickSignature);
880 i=0;
cristybb503372010-05-27 20:51:26 +0000881 for (v=0; v < (ssize_t) color_matrix->height; v++)
882 for (u=0; u < (ssize_t) color_matrix->width; u++)
cristye6365592010-04-02 17:31:23 +0000883 {
884 if ((v < 6) && (u < 6))
885 ColorMatrix[v][u]=color_matrix->values[i];
886 i++;
887 }
888 /*
889 Initialize color image.
890 */
cristy12550e62010-06-07 12:46:40 +0000891 color_image=CloneImage(image,0,0,MagickTrue,exception);
cristye6365592010-04-02 17:31:23 +0000892 if (color_image == (Image *) NULL)
893 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000894 if (SetImageStorageClass(color_image,DirectClass,exception) == MagickFalse)
cristye6365592010-04-02 17:31:23 +0000895 {
cristye6365592010-04-02 17:31:23 +0000896 color_image=DestroyImage(color_image);
897 return((Image *) NULL);
898 }
899 if (image->debug != MagickFalse)
900 {
901 char
902 format[MaxTextExtent],
903 *message;
904
905 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
906 " ColorMatrix image with color matrix:");
907 message=AcquireString("");
908 for (v=0; v < 6; v++)
909 {
910 *message='\0';
cristyb51dff52011-05-19 16:55:47 +0000911 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
cristye6365592010-04-02 17:31:23 +0000912 (void) ConcatenateString(&message,format);
913 for (u=0; u < 6; u++)
914 {
cristyb51dff52011-05-19 16:55:47 +0000915 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",
cristye6365592010-04-02 17:31:23 +0000916 ColorMatrix[v][u]);
917 (void) ConcatenateString(&message,format);
918 }
919 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
920 }
921 message=DestroyString(message);
922 }
923 /*
anthonyfd706f92012-01-19 04:22:02 +0000924 Apply the ColorMatrix to image.
cristye6365592010-04-02 17:31:23 +0000925 */
926 status=MagickTrue;
927 progress=0;
928 image_view=AcquireCacheView(image);
929 color_view=AcquireCacheView(color_image);
930#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000931 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristye6365592010-04-02 17:31:23 +0000932#endif
cristybb503372010-05-27 20:51:26 +0000933 for (y=0; y < (ssize_t) image->rows; y++)
cristye6365592010-04-02 17:31:23 +0000934 {
cristyfcc25d92012-02-19 23:06:48 +0000935 PixelInfo
cristye6365592010-04-02 17:31:23 +0000936 pixel;
937
cristy4c08aed2011-07-01 19:47:50 +0000938 register const Quantum
cristye6365592010-04-02 17:31:23 +0000939 *restrict p;
940
cristy4c08aed2011-07-01 19:47:50 +0000941 register Quantum
942 *restrict q;
943
cristybb503372010-05-27 20:51:26 +0000944 register ssize_t
cristye6365592010-04-02 17:31:23 +0000945 x;
946
cristye6365592010-04-02 17:31:23 +0000947 if (status == MagickFalse)
948 continue;
949 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
950 q=GetCacheViewAuthenticPixels(color_view,0,y,color_image->columns,1,
951 exception);
cristy4c08aed2011-07-01 19:47:50 +0000952 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristye6365592010-04-02 17:31:23 +0000953 {
954 status=MagickFalse;
955 continue;
956 }
cristyfcc25d92012-02-19 23:06:48 +0000957 GetPixelInfo(image,&pixel);
cristybb503372010-05-27 20:51:26 +0000958 for (x=0; x < (ssize_t) image->columns; x++)
cristye6365592010-04-02 17:31:23 +0000959 {
cristybb503372010-05-27 20:51:26 +0000960 register ssize_t
cristye6365592010-04-02 17:31:23 +0000961 v;
962
cristybb503372010-05-27 20:51:26 +0000963 size_t
cristye6365592010-04-02 17:31:23 +0000964 height;
965
cristyfcc25d92012-02-19 23:06:48 +0000966 GetPixelInfoPixel(image,p,&pixel);
cristye6365592010-04-02 17:31:23 +0000967 height=color_matrix->height > 6 ? 6UL : color_matrix->height;
cristybb503372010-05-27 20:51:26 +0000968 for (v=0; v < (ssize_t) height; v++)
cristye6365592010-04-02 17:31:23 +0000969 {
cristyfcc25d92012-02-19 23:06:48 +0000970 MagickRealType
971 sum;
972
973 sum=ColorMatrix[v][0]*GetPixelRed(image,p)+ColorMatrix[v][1]*
cristy4c08aed2011-07-01 19:47:50 +0000974 GetPixelGreen(image,p)+ColorMatrix[v][2]*GetPixelBlue(image,p);
cristye6365592010-04-02 17:31:23 +0000975 if (image->colorspace == CMYKColorspace)
cristyfcc25d92012-02-19 23:06:48 +0000976 sum+=ColorMatrix[v][3]*GetPixelBlack(image,p);
cristy4c08aed2011-07-01 19:47:50 +0000977 if (image->matte != MagickFalse)
cristyfcc25d92012-02-19 23:06:48 +0000978 sum+=ColorMatrix[v][4]*GetPixelAlpha(image,p);
979 sum+=QuantumRange*ColorMatrix[v][5];
cristye6365592010-04-02 17:31:23 +0000980 switch (v)
981 {
cristyfcc25d92012-02-19 23:06:48 +0000982 case 0: pixel.red=sum; break;
983 case 1: pixel.green=sum; break;
984 case 2: pixel.blue=sum; break;
985 case 3: pixel.black=sum; break;
986 case 4: pixel.alpha=sum; break;
987 default: break;
cristye6365592010-04-02 17:31:23 +0000988 }
989 }
cristyfcc25d92012-02-19 23:06:48 +0000990 SetPixelInfoPixel(color_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +0000991 p+=GetPixelChannels(image);
992 q+=GetPixelChannels(color_image);
cristye6365592010-04-02 17:31:23 +0000993 }
994 if (SyncCacheViewAuthenticPixels(color_view,exception) == MagickFalse)
995 status=MagickFalse;
996 if (image->progress_monitor != (MagickProgressMonitor) NULL)
997 {
998 MagickBooleanType
999 proceed;
1000
1001#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00001002 #pragma omp critical (MagickCore_ColorMatrixImage)
cristye6365592010-04-02 17:31:23 +00001003#endif
1004 proceed=SetImageProgress(image,ColorMatrixImageTag,progress++,
1005 image->rows);
1006 if (proceed == MagickFalse)
1007 status=MagickFalse;
1008 }
1009 }
1010 color_view=DestroyCacheView(color_view);
1011 image_view=DestroyCacheView(image_view);
1012 if (status == MagickFalse)
1013 color_image=DestroyImage(color_image);
1014 return(color_image);
1015}
1016
1017/*
1018%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1019% %
1020% %
1021% %
cristy3ed852e2009-09-05 21:47:34 +00001022+ D e s t r o y F x I n f o %
1023% %
1024% %
1025% %
1026%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1027%
1028% DestroyFxInfo() deallocates memory associated with an FxInfo structure.
1029%
1030% The format of the DestroyFxInfo method is:
1031%
1032% ImageInfo *DestroyFxInfo(ImageInfo *fx_info)
1033%
1034% A description of each parameter follows:
1035%
1036% o fx_info: the fx info.
1037%
1038*/
cristy7832dc22011-09-05 01:21:53 +00001039MagickPrivate FxInfo *DestroyFxInfo(FxInfo *fx_info)
cristy3ed852e2009-09-05 21:47:34 +00001040{
cristybb503372010-05-27 20:51:26 +00001041 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001042 i;
1043
1044 fx_info->exception=DestroyExceptionInfo(fx_info->exception);
1045 fx_info->expression=DestroyString(fx_info->expression);
1046 fx_info->symbols=DestroySplayTree(fx_info->symbols);
1047 fx_info->colors=DestroySplayTree(fx_info->colors);
cristy0ea377f2011-03-24 00:54:19 +00001048 for (i=(ssize_t) GetImageListLength(fx_info->images)-1; i >= 0; i--)
cristyd76c51e2011-03-26 00:21:26 +00001049 fx_info->view[i]=DestroyCacheView(fx_info->view[i]);
1050 fx_info->view=(CacheView **) RelinquishMagickMemory(fx_info->view);
cristy3ed852e2009-09-05 21:47:34 +00001051 fx_info->random_info=DestroyRandomInfo(fx_info->random_info);
1052 fx_info=(FxInfo *) RelinquishMagickMemory(fx_info);
1053 return(fx_info);
1054}
1055
1056/*
1057%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1058% %
1059% %
1060% %
cristy3ed852e2009-09-05 21:47:34 +00001061+ 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 %
1062% %
1063% %
1064% %
1065%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1066%
1067% FxEvaluateChannelExpression() evaluates an expression and returns the
1068% results.
1069%
1070% The format of the FxEvaluateExpression method is:
1071%
1072% MagickRealType FxEvaluateChannelExpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00001073% const PixelChannel channel,const ssize_t x,const ssize_t y,
cristy3ed852e2009-09-05 21:47:34 +00001074% MagickRealType *alpha,Exceptioninfo *exception)
1075% MagickRealType FxEvaluateExpression(FxInfo *fx_info,
1076% MagickRealType *alpha,Exceptioninfo *exception)
1077%
1078% A description of each parameter follows:
1079%
1080% o fx_info: the fx info.
1081%
1082% o channel: the channel.
1083%
1084% o x,y: the pixel position.
1085%
1086% o alpha: the result.
1087%
1088% o exception: return any errors or warnings in this structure.
1089%
1090*/
1091
cristy351842f2010-03-07 15:27:38 +00001092static inline double MagickMax(const double x,const double y)
1093{
1094 if (x > y)
1095 return(x);
1096 return(y);
1097}
1098
1099static inline double MagickMin(const double x,const double y)
1100{
1101 if (x < y)
1102 return(x);
1103 return(y);
1104}
1105
cristy3ed852e2009-09-05 21:47:34 +00001106static MagickRealType FxChannelStatistics(FxInfo *fx_info,const Image *image,
cristy0568ffc2011-07-25 16:54:14 +00001107 PixelChannel channel,const char *symbol,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001108{
1109 char
1110 key[MaxTextExtent],
1111 statistic[MaxTextExtent];
1112
1113 const char
1114 *value;
1115
1116 register const char
1117 *p;
1118
1119 for (p=symbol; (*p != '.') && (*p != '\0'); p++) ;
1120 if (*p == '.')
1121 switch (*++p) /* e.g. depth.r */
1122 {
cristy541ae572011-08-05 19:08:59 +00001123 case 'r': channel=RedPixelChannel; break;
1124 case 'g': channel=GreenPixelChannel; break;
1125 case 'b': channel=BluePixelChannel; break;
1126 case 'c': channel=CyanPixelChannel; break;
1127 case 'm': channel=MagentaPixelChannel; break;
1128 case 'y': channel=YellowPixelChannel; break;
1129 case 'k': channel=BlackPixelChannel; break;
cristy3ed852e2009-09-05 21:47:34 +00001130 default: break;
1131 }
cristyb51dff52011-05-19 16:55:47 +00001132 (void) FormatLocaleString(key,MaxTextExtent,"%p.%.20g.%s",(void *) image,
cristye8c25f92010-06-03 00:53:06 +00001133 (double) channel,symbol);
cristy3ed852e2009-09-05 21:47:34 +00001134 value=(const char *) GetValueFromSplayTree(fx_info->symbols,key);
1135 if (value != (const char *) NULL)
cristydbdd0e32011-11-04 23:29:40 +00001136 return(QuantumScale*StringToDouble(value,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001137 (void) DeleteNodeFromSplayTree(fx_info->symbols,key);
1138 if (LocaleNCompare(symbol,"depth",5) == 0)
1139 {
cristybb503372010-05-27 20:51:26 +00001140 size_t
cristy3ed852e2009-09-05 21:47:34 +00001141 depth;
1142
cristyfefab1b2011-07-05 00:33:22 +00001143 depth=GetImageDepth(image,exception);
1144 (void) FormatLocaleString(statistic,MaxTextExtent,"%.20g",(double) depth);
cristy3ed852e2009-09-05 21:47:34 +00001145 }
1146 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1147 {
1148 double
1149 kurtosis,
1150 skewness;
1151
cristyd42d9952011-07-08 14:21:50 +00001152 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
cristyb51dff52011-05-19 16:55:47 +00001153 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",kurtosis);
cristy3ed852e2009-09-05 21:47:34 +00001154 }
1155 if (LocaleNCompare(symbol,"maxima",6) == 0)
1156 {
1157 double
1158 maxima,
1159 minima;
1160
cristyd42d9952011-07-08 14:21:50 +00001161 (void) GetImageRange(image,&minima,&maxima,exception);
cristyb51dff52011-05-19 16:55:47 +00001162 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",maxima);
cristy3ed852e2009-09-05 21:47:34 +00001163 }
1164 if (LocaleNCompare(symbol,"mean",4) == 0)
1165 {
1166 double
1167 mean,
1168 standard_deviation;
1169
cristyd42d9952011-07-08 14:21:50 +00001170 (void) GetImageMean(image,&mean,&standard_deviation,exception);
cristyb51dff52011-05-19 16:55:47 +00001171 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",mean);
cristy3ed852e2009-09-05 21:47:34 +00001172 }
1173 if (LocaleNCompare(symbol,"minima",6) == 0)
1174 {
1175 double
1176 maxima,
1177 minima;
1178
cristyd42d9952011-07-08 14:21:50 +00001179 (void) GetImageRange(image,&minima,&maxima,exception);
cristyb51dff52011-05-19 16:55:47 +00001180 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",minima);
cristy3ed852e2009-09-05 21:47:34 +00001181 }
1182 if (LocaleNCompare(symbol,"skewness",8) == 0)
1183 {
1184 double
1185 kurtosis,
1186 skewness;
1187
cristyd42d9952011-07-08 14:21:50 +00001188 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
cristyb51dff52011-05-19 16:55:47 +00001189 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",skewness);
cristy3ed852e2009-09-05 21:47:34 +00001190 }
1191 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1192 {
1193 double
1194 mean,
1195 standard_deviation;
1196
cristyd42d9952011-07-08 14:21:50 +00001197 (void) GetImageMean(image,&mean,&standard_deviation,exception);
cristyb51dff52011-05-19 16:55:47 +00001198 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",
cristy3ed852e2009-09-05 21:47:34 +00001199 standard_deviation);
1200 }
1201 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(key),
1202 ConstantString(statistic));
cristydbdd0e32011-11-04 23:29:40 +00001203 return(QuantumScale*StringToDouble(statistic,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001204}
1205
1206static MagickRealType
cristy0568ffc2011-07-25 16:54:14 +00001207 FxEvaluateSubexpression(FxInfo *,const PixelChannel,const ssize_t,
cristye85007d2010-06-06 22:51:36 +00001208 const ssize_t,const char *,MagickRealType *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +00001209
cristyb0aad4c2011-11-02 19:30:35 +00001210static MagickOffsetType FxGCD(MagickOffsetType alpha,MagickOffsetType beta)
1211{
1212 if (beta != 0)
1213 return(FxGCD(beta,alpha % beta));
1214 return(alpha);
1215}
1216
cristy3ed852e2009-09-05 21:47:34 +00001217static inline const char *FxSubexpression(const char *expression,
1218 ExceptionInfo *exception)
1219{
1220 const char
1221 *subexpression;
1222
cristybb503372010-05-27 20:51:26 +00001223 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001224 level;
1225
1226 level=0;
1227 subexpression=expression;
1228 while ((*subexpression != '\0') &&
1229 ((level != 1) || (strchr(")",(int) *subexpression) == (char *) NULL)))
1230 {
1231 if (strchr("(",(int) *subexpression) != (char *) NULL)
1232 level++;
1233 else
1234 if (strchr(")",(int) *subexpression) != (char *) NULL)
1235 level--;
1236 subexpression++;
1237 }
1238 if (*subexpression == '\0')
1239 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1240 "UnbalancedParenthesis","`%s'",expression);
1241 return(subexpression);
1242}
1243
cristy0568ffc2011-07-25 16:54:14 +00001244static MagickRealType FxGetSymbol(FxInfo *fx_info,const PixelChannel channel,
cristye85007d2010-06-06 22:51:36 +00001245 const ssize_t x,const ssize_t y,const char *expression,
1246 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001247{
1248 char
1249 *q,
1250 subexpression[MaxTextExtent],
1251 symbol[MaxTextExtent];
1252
1253 const char
1254 *p,
1255 *value;
1256
1257 Image
1258 *image;
1259
cristy4c08aed2011-07-01 19:47:50 +00001260 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001261 pixel;
1262
1263 MagickRealType
1264 alpha,
1265 beta;
1266
1267 PointInfo
1268 point;
1269
cristybb503372010-05-27 20:51:26 +00001270 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001271 i;
1272
1273 size_t
cristy1707c6c2012-01-18 23:30:54 +00001274 length,
cristy3ed852e2009-09-05 21:47:34 +00001275 level;
1276
1277 p=expression;
1278 i=GetImageIndexInList(fx_info->images);
1279 level=0;
1280 point.x=(double) x;
1281 point.y=(double) y;
1282 if (isalpha((int) *(p+1)) == 0)
1283 {
1284 if (strchr("suv",(int) *p) != (char *) NULL)
1285 {
1286 switch (*p)
1287 {
1288 case 's':
1289 default:
1290 {
1291 i=GetImageIndexInList(fx_info->images);
1292 break;
1293 }
1294 case 'u': i=0; break;
1295 case 'v': i=1; break;
1296 }
1297 p++;
1298 if (*p == '[')
1299 {
1300 level++;
1301 q=subexpression;
1302 for (p++; *p != '\0'; )
1303 {
1304 if (*p == '[')
1305 level++;
1306 else
1307 if (*p == ']')
1308 {
1309 level--;
1310 if (level == 0)
1311 break;
1312 }
1313 *q++=(*p++);
1314 }
1315 *q='\0';
1316 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1317 &beta,exception);
cristybb503372010-05-27 20:51:26 +00001318 i=(ssize_t) (alpha+0.5);
cristy3ed852e2009-09-05 21:47:34 +00001319 p++;
1320 }
1321 if (*p == '.')
1322 p++;
1323 }
1324 if ((isalpha((int) *(p+1)) == 0) && (*p == 'p'))
1325 {
1326 p++;
1327 if (*p == '{')
1328 {
1329 level++;
1330 q=subexpression;
1331 for (p++; *p != '\0'; )
1332 {
1333 if (*p == '{')
1334 level++;
1335 else
1336 if (*p == '}')
1337 {
1338 level--;
1339 if (level == 0)
1340 break;
1341 }
1342 *q++=(*p++);
1343 }
1344 *q='\0';
1345 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1346 &beta,exception);
1347 point.x=alpha;
1348 point.y=beta;
1349 p++;
1350 }
1351 else
1352 if (*p == '[')
1353 {
1354 level++;
1355 q=subexpression;
1356 for (p++; *p != '\0'; )
1357 {
1358 if (*p == '[')
1359 level++;
1360 else
1361 if (*p == ']')
1362 {
1363 level--;
1364 if (level == 0)
1365 break;
1366 }
1367 *q++=(*p++);
1368 }
1369 *q='\0';
1370 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1371 &beta,exception);
1372 point.x+=alpha;
1373 point.y+=beta;
1374 p++;
1375 }
1376 if (*p == '.')
1377 p++;
1378 }
1379 }
1380 length=GetImageListLength(fx_info->images);
1381 while (i < 0)
cristybb503372010-05-27 20:51:26 +00001382 i+=(ssize_t) length;
cristy3ed852e2009-09-05 21:47:34 +00001383 i%=length;
1384 image=GetImageFromList(fx_info->images,i);
1385 if (image == (Image *) NULL)
1386 {
1387 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1388 "NoSuchImage","`%s'",expression);
1389 return(0.0);
1390 }
cristy4c08aed2011-07-01 19:47:50 +00001391 GetPixelInfo(image,&pixel);
1392 (void) InterpolatePixelInfo(image,fx_info->view[i],image->interpolate,
cristy4f820712011-04-01 12:35:43 +00001393 point.x,point.y,&pixel,exception);
cristy1707c6c2012-01-18 23:30:54 +00001394 if ((strlen(p) > 2) && (LocaleCompare(p,"intensity") != 0) &&
1395 (LocaleCompare(p,"luminance") != 0) && (LocaleCompare(p,"hue") != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00001396 (LocaleCompare(p,"saturation") != 0) &&
1397 (LocaleCompare(p,"lightness") != 0))
1398 {
1399 char
1400 name[MaxTextExtent];
1401
1402 (void) CopyMagickString(name,p,MaxTextExtent);
1403 for (q=name+(strlen(name)-1); q > name; q--)
1404 {
1405 if (*q == ')')
1406 break;
1407 if (*q == '.')
1408 {
1409 *q='\0';
1410 break;
1411 }
1412 }
1413 if ((strlen(name) > 2) &&
1414 (GetValueFromSplayTree(fx_info->symbols,name) == (const char *) NULL))
1415 {
cristy4c08aed2011-07-01 19:47:50 +00001416 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001417 *color;
1418
cristy4c08aed2011-07-01 19:47:50 +00001419 color=(PixelInfo *) GetValueFromSplayTree(fx_info->colors,name);
1420 if (color != (PixelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001421 {
1422 pixel=(*color);
1423 p+=strlen(name);
1424 }
1425 else
cristy1707c6c2012-01-18 23:30:54 +00001426 {
1427 MagickBooleanType
1428 status;
1429
1430 status=QueryColorCompliance(name,AllCompliance,&pixel,
1431 fx_info->exception);
1432 if (status != MagickFalse)
1433 {
1434 (void) AddValueToSplayTree(fx_info->colors,ConstantString(
1435 name),ClonePixelInfo(&pixel));
1436 p+=strlen(name);
1437 }
1438 }
cristy3ed852e2009-09-05 21:47:34 +00001439 }
1440 }
1441 (void) CopyMagickString(symbol,p,MaxTextExtent);
1442 StripString(symbol);
1443 if (*symbol == '\0')
1444 {
1445 switch (channel)
1446 {
cristy0568ffc2011-07-25 16:54:14 +00001447 case RedPixelChannel: return(QuantumScale*pixel.red);
1448 case GreenPixelChannel: return(QuantumScale*pixel.green);
1449 case BluePixelChannel: return(QuantumScale*pixel.blue);
1450 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001451 {
1452 if (image->colorspace != CMYKColorspace)
1453 {
1454 (void) ThrowMagickException(exception,GetMagickModule(),
cristy1a020e42011-12-06 18:13:23 +00001455 ImageError,"ColorSeparatedImageRequired","`%s'",
cristy3ed852e2009-09-05 21:47:34 +00001456 image->filename);
1457 return(0.0);
1458 }
cristy4c08aed2011-07-01 19:47:50 +00001459 return(QuantumScale*pixel.black);
1460 }
cristy0568ffc2011-07-25 16:54:14 +00001461 case AlphaPixelChannel:
cristy4c08aed2011-07-01 19:47:50 +00001462 {
1463 MagickRealType
1464 alpha;
1465
1466 if (pixel.matte == MagickFalse)
1467 return(1.0);
1468 alpha=(MagickRealType) (QuantumScale*pixel.alpha);
1469 return(alpha);
cristy3ed852e2009-09-05 21:47:34 +00001470 }
cristya382aca2011-12-06 18:22:48 +00001471 case IndexPixelChannel:
1472 return(0.0);
cristyb3a73b52011-07-26 01:34:43 +00001473 case IntensityPixelChannel:
cristyf364ed42010-12-15 01:54:43 +00001474 {
cristy4c08aed2011-07-01 19:47:50 +00001475 return(QuantumScale*GetPixelInfoIntensity(&pixel));
cristyf364ed42010-12-15 01:54:43 +00001476 }
cristy3ed852e2009-09-05 21:47:34 +00001477 default:
1478 break;
1479 }
1480 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1481 "UnableToParseExpression","`%s'",p);
1482 return(0.0);
1483 }
1484 switch (*symbol)
1485 {
1486 case 'A':
1487 case 'a':
1488 {
1489 if (LocaleCompare(symbol,"a") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001490 return((MagickRealType) (QuantumScale*pixel.alpha));
cristy3ed852e2009-09-05 21:47:34 +00001491 break;
1492 }
1493 case 'B':
1494 case 'b':
1495 {
1496 if (LocaleCompare(symbol,"b") == 0)
1497 return(QuantumScale*pixel.blue);
1498 break;
1499 }
1500 case 'C':
1501 case 'c':
1502 {
1503 if (LocaleNCompare(symbol,"channel",7) == 0)
1504 {
1505 GeometryInfo
1506 channel_info;
1507
1508 MagickStatusType
1509 flags;
1510
1511 flags=ParseGeometry(symbol+7,&channel_info);
1512 if (image->colorspace == CMYKColorspace)
1513 switch (channel)
1514 {
cristy0568ffc2011-07-25 16:54:14 +00001515 case CyanPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001516 {
1517 if ((flags & RhoValue) == 0)
1518 return(0.0);
1519 return(channel_info.rho);
1520 }
cristy0568ffc2011-07-25 16:54:14 +00001521 case MagentaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001522 {
1523 if ((flags & SigmaValue) == 0)
1524 return(0.0);
1525 return(channel_info.sigma);
1526 }
cristy0568ffc2011-07-25 16:54:14 +00001527 case YellowPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001528 {
1529 if ((flags & XiValue) == 0)
1530 return(0.0);
1531 return(channel_info.xi);
1532 }
cristy0568ffc2011-07-25 16:54:14 +00001533 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001534 {
1535 if ((flags & PsiValue) == 0)
1536 return(0.0);
1537 return(channel_info.psi);
1538 }
cristy0568ffc2011-07-25 16:54:14 +00001539 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001540 {
1541 if ((flags & ChiValue) == 0)
1542 return(0.0);
1543 return(channel_info.chi);
1544 }
1545 default:
1546 return(0.0);
1547 }
1548 switch (channel)
1549 {
cristy0568ffc2011-07-25 16:54:14 +00001550 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001551 {
1552 if ((flags & RhoValue) == 0)
1553 return(0.0);
1554 return(channel_info.rho);
1555 }
cristy0568ffc2011-07-25 16:54:14 +00001556 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001557 {
1558 if ((flags & SigmaValue) == 0)
1559 return(0.0);
1560 return(channel_info.sigma);
1561 }
cristy0568ffc2011-07-25 16:54:14 +00001562 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001563 {
1564 if ((flags & XiValue) == 0)
1565 return(0.0);
1566 return(channel_info.xi);
1567 }
cristy0568ffc2011-07-25 16:54:14 +00001568 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001569 {
1570 if ((flags & ChiValue) == 0)
1571 return(0.0);
1572 return(channel_info.chi);
1573 }
cristy0568ffc2011-07-25 16:54:14 +00001574 case AlphaPixelChannel:
cristy4c08aed2011-07-01 19:47:50 +00001575 {
1576 if ((flags & PsiValue) == 0)
1577 return(0.0);
1578 return(channel_info.psi);
1579 }
cristy3ed852e2009-09-05 21:47:34 +00001580 default:
1581 return(0.0);
1582 }
1583 return(0.0);
1584 }
1585 if (LocaleCompare(symbol,"c") == 0)
1586 return(QuantumScale*pixel.red);
1587 break;
1588 }
1589 case 'D':
1590 case 'd':
1591 {
1592 if (LocaleNCompare(symbol,"depth",5) == 0)
1593 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1594 break;
1595 }
1596 case 'G':
1597 case 'g':
1598 {
1599 if (LocaleCompare(symbol,"g") == 0)
1600 return(QuantumScale*pixel.green);
1601 break;
1602 }
1603 case 'K':
1604 case 'k':
1605 {
1606 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1607 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1608 if (LocaleCompare(symbol,"k") == 0)
1609 {
1610 if (image->colorspace != CMYKColorspace)
1611 {
1612 (void) ThrowMagickException(exception,GetMagickModule(),
1613 OptionError,"ColorSeparatedImageRequired","`%s'",
1614 image->filename);
1615 return(0.0);
1616 }
cristy4c08aed2011-07-01 19:47:50 +00001617 return(QuantumScale*pixel.black);
cristy3ed852e2009-09-05 21:47:34 +00001618 }
1619 break;
1620 }
1621 case 'H':
1622 case 'h':
1623 {
1624 if (LocaleCompare(symbol,"h") == 0)
1625 return((MagickRealType) image->rows);
1626 if (LocaleCompare(symbol,"hue") == 0)
1627 {
1628 double
1629 hue,
1630 lightness,
1631 saturation;
1632
cristyda1f9c12011-10-02 21:39:49 +00001633 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1634 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001635 return(hue);
1636 }
1637 break;
1638 }
1639 case 'I':
1640 case 'i':
1641 {
1642 if ((LocaleCompare(symbol,"image.depth") == 0) ||
1643 (LocaleCompare(symbol,"image.minima") == 0) ||
1644 (LocaleCompare(symbol,"image.maxima") == 0) ||
1645 (LocaleCompare(symbol,"image.mean") == 0) ||
1646 (LocaleCompare(symbol,"image.kurtosis") == 0) ||
1647 (LocaleCompare(symbol,"image.skewness") == 0) ||
1648 (LocaleCompare(symbol,"image.standard_deviation") == 0))
1649 return(FxChannelStatistics(fx_info,image,channel,symbol+6,exception));
1650 if (LocaleCompare(symbol,"image.resolution.x") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001651 return(image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00001652 if (LocaleCompare(symbol,"image.resolution.y") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001653 return(image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001654 if (LocaleCompare(symbol,"intensity") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001655 return(QuantumScale*GetPixelInfoIntensity(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001656 if (LocaleCompare(symbol,"i") == 0)
1657 return((MagickRealType) x);
1658 break;
1659 }
1660 case 'J':
1661 case 'j':
1662 {
1663 if (LocaleCompare(symbol,"j") == 0)
1664 return((MagickRealType) y);
1665 break;
1666 }
1667 case 'L':
1668 case 'l':
1669 {
1670 if (LocaleCompare(symbol,"lightness") == 0)
1671 {
1672 double
1673 hue,
1674 lightness,
1675 saturation;
1676
cristyda1f9c12011-10-02 21:39:49 +00001677 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1678 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001679 return(lightness);
1680 }
1681 if (LocaleCompare(symbol,"luminance") == 0)
1682 {
1683 double
1684 luminence;
1685
1686 luminence=0.2126*pixel.red+0.7152*pixel.green+0.0722*pixel.blue;
1687 return(QuantumScale*luminence);
1688 }
1689 break;
1690 }
1691 case 'M':
1692 case 'm':
1693 {
1694 if (LocaleNCompare(symbol,"maxima",6) == 0)
1695 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1696 if (LocaleNCompare(symbol,"mean",4) == 0)
1697 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1698 if (LocaleNCompare(symbol,"minima",6) == 0)
1699 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1700 if (LocaleCompare(symbol,"m") == 0)
1701 return(QuantumScale*pixel.blue);
1702 break;
1703 }
1704 case 'N':
1705 case 'n':
1706 {
1707 if (LocaleCompare(symbol,"n") == 0)
anthony374f5dd2011-03-25 10:08:53 +00001708 return((MagickRealType) GetImageListLength(fx_info->images));
cristy3ed852e2009-09-05 21:47:34 +00001709 break;
1710 }
1711 case 'O':
1712 case 'o':
1713 {
1714 if (LocaleCompare(symbol,"o") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001715 return(QuantumScale*pixel.alpha);
cristy3ed852e2009-09-05 21:47:34 +00001716 break;
1717 }
1718 case 'P':
1719 case 'p':
1720 {
1721 if (LocaleCompare(symbol,"page.height") == 0)
1722 return((MagickRealType) image->page.height);
1723 if (LocaleCompare(symbol,"page.width") == 0)
1724 return((MagickRealType) image->page.width);
1725 if (LocaleCompare(symbol,"page.x") == 0)
1726 return((MagickRealType) image->page.x);
1727 if (LocaleCompare(symbol,"page.y") == 0)
1728 return((MagickRealType) image->page.y);
1729 break;
1730 }
1731 case 'R':
1732 case 'r':
1733 {
1734 if (LocaleCompare(symbol,"resolution.x") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001735 return(image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00001736 if (LocaleCompare(symbol,"resolution.y") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001737 return(image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001738 if (LocaleCompare(symbol,"r") == 0)
1739 return(QuantumScale*pixel.red);
1740 break;
1741 }
1742 case 'S':
1743 case 's':
1744 {
1745 if (LocaleCompare(symbol,"saturation") == 0)
1746 {
1747 double
1748 hue,
1749 lightness,
1750 saturation;
1751
cristyda1f9c12011-10-02 21:39:49 +00001752 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1753 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001754 return(saturation);
1755 }
1756 if (LocaleNCompare(symbol,"skewness",8) == 0)
1757 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1758 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1759 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1760 break;
1761 }
1762 case 'T':
1763 case 't':
1764 {
1765 if (LocaleCompare(symbol,"t") == 0)
cristy5a15b932011-03-26 12:50:33 +00001766 return((MagickRealType) GetImageIndexInList(fx_info->images));
cristy3ed852e2009-09-05 21:47:34 +00001767 break;
1768 }
1769 case 'W':
1770 case 'w':
1771 {
1772 if (LocaleCompare(symbol,"w") == 0)
1773 return((MagickRealType) image->columns);
1774 break;
1775 }
1776 case 'Y':
1777 case 'y':
1778 {
1779 if (LocaleCompare(symbol,"y") == 0)
1780 return(QuantumScale*pixel.green);
1781 break;
1782 }
1783 case 'Z':
1784 case 'z':
1785 {
1786 if (LocaleCompare(symbol,"z") == 0)
1787 {
1788 MagickRealType
1789 depth;
1790
cristyfefab1b2011-07-05 00:33:22 +00001791 depth=(MagickRealType) GetImageDepth(image,fx_info->exception);
cristy3ed852e2009-09-05 21:47:34 +00001792 return(depth);
1793 }
1794 break;
1795 }
1796 default:
1797 break;
1798 }
1799 value=(const char *) GetValueFromSplayTree(fx_info->symbols,symbol);
1800 if (value != (const char *) NULL)
cristydbdd0e32011-11-04 23:29:40 +00001801 return((MagickRealType) StringToDouble(value,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001802 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1803 "UnableToParseExpression","`%s'",symbol);
1804 return(0.0);
1805}
1806
1807static const char *FxOperatorPrecedence(const char *expression,
1808 ExceptionInfo *exception)
1809{
1810 typedef enum
1811 {
1812 UndefinedPrecedence,
1813 NullPrecedence,
1814 BitwiseComplementPrecedence,
1815 ExponentPrecedence,
cristy116af162010-08-13 01:25:47 +00001816 ExponentialNotationPrecedence,
cristy3ed852e2009-09-05 21:47:34 +00001817 MultiplyPrecedence,
1818 AdditionPrecedence,
1819 ShiftPrecedence,
1820 RelationalPrecedence,
1821 EquivalencyPrecedence,
1822 BitwiseAndPrecedence,
1823 BitwiseOrPrecedence,
1824 LogicalAndPrecedence,
1825 LogicalOrPrecedence,
1826 TernaryPrecedence,
1827 AssignmentPrecedence,
1828 CommaPrecedence,
1829 SeparatorPrecedence
1830 } FxPrecedence;
1831
1832 FxPrecedence
1833 precedence,
1834 target;
1835
1836 register const char
1837 *subexpression;
1838
1839 register int
1840 c;
1841
cristybb503372010-05-27 20:51:26 +00001842 size_t
cristy3ed852e2009-09-05 21:47:34 +00001843 level;
1844
1845 c=0;
1846 level=0;
1847 subexpression=(const char *) NULL;
1848 target=NullPrecedence;
1849 while (*expression != '\0')
1850 {
1851 precedence=UndefinedPrecedence;
1852 if ((isspace((int) ((char) *expression)) != 0) || (c == (int) '@'))
1853 {
1854 expression++;
1855 continue;
1856 }
cristy488fa882010-03-01 22:34:24 +00001857 switch (*expression)
1858 {
1859 case 'A':
1860 case 'a':
cristy3ed852e2009-09-05 21:47:34 +00001861 {
cristyb33454f2011-08-03 02:10:45 +00001862#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00001863 if (LocaleNCompare(expression,"acosh",5) == 0)
1864 {
1865 expression+=5;
1866 break;
1867 }
cristyb33454f2011-08-03 02:10:45 +00001868#endif
1869#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00001870 if (LocaleNCompare(expression,"asinh",5) == 0)
1871 {
1872 expression+=5;
1873 break;
1874 }
cristyb33454f2011-08-03 02:10:45 +00001875#endif
1876#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00001877 if (LocaleNCompare(expression,"atanh",5) == 0)
cristy488fa882010-03-01 22:34:24 +00001878 {
1879 expression+=5;
1880 break;
1881 }
cristyb33454f2011-08-03 02:10:45 +00001882#endif
cristy488fa882010-03-01 22:34:24 +00001883 break;
cristy3ed852e2009-09-05 21:47:34 +00001884 }
cristy62d455f2011-11-03 11:42:28 +00001885 case 'E':
1886 case 'e':
1887 {
1888 if ((LocaleNCompare(expression,"E+",2) == 0) ||
1889 (LocaleNCompare(expression,"E-",2) == 0))
1890 {
1891 expression+=2; /* scientific notation */
1892 break;
1893 }
1894 }
cristy488fa882010-03-01 22:34:24 +00001895 case 'J':
1896 case 'j':
1897 {
1898 if ((LocaleNCompare(expression,"j0",2) == 0) ||
1899 (LocaleNCompare(expression,"j1",2) == 0))
1900 {
1901 expression+=2;
1902 break;
1903 }
1904 break;
1905 }
cristy2def9322010-06-18 23:59:37 +00001906 case '#':
1907 {
1908 while (isxdigit((int) ((unsigned char) *(expression+1))) != 0)
1909 expression++;
1910 break;
1911 }
cristy488fa882010-03-01 22:34:24 +00001912 default:
1913 break;
1914 }
cristy3ed852e2009-09-05 21:47:34 +00001915 if ((c == (int) '{') || (c == (int) '['))
1916 level++;
1917 else
1918 if ((c == (int) '}') || (c == (int) ']'))
1919 level--;
1920 if (level == 0)
1921 switch ((unsigned char) *expression)
1922 {
1923 case '~':
1924 case '!':
1925 {
1926 precedence=BitwiseComplementPrecedence;
1927 break;
1928 }
1929 case '^':
cristy6621e252010-08-13 00:42:57 +00001930 case '@':
cristy3ed852e2009-09-05 21:47:34 +00001931 {
1932 precedence=ExponentPrecedence;
1933 break;
1934 }
1935 default:
1936 {
1937 if (((c != 0) && ((isdigit((int) ((char) c)) != 0) ||
1938 (strchr(")",c) != (char *) NULL))) &&
1939 (((islower((int) ((char) *expression)) != 0) ||
1940 (strchr("(",(int) *expression) != (char *) NULL)) ||
1941 ((isdigit((int) ((char) c)) == 0) &&
1942 (isdigit((int) ((char) *expression)) != 0))) &&
1943 (strchr("xy",(int) *expression) == (char *) NULL))
1944 precedence=MultiplyPrecedence;
1945 break;
1946 }
1947 case '*':
1948 case '/':
1949 case '%':
1950 {
1951 precedence=MultiplyPrecedence;
1952 break;
1953 }
1954 case '+':
1955 case '-':
1956 {
1957 if ((strchr("(+-/*%:&^|<>~,",c) == (char *) NULL) ||
1958 (isalpha(c) != 0))
1959 precedence=AdditionPrecedence;
1960 break;
1961 }
1962 case LeftShiftOperator:
1963 case RightShiftOperator:
1964 {
1965 precedence=ShiftPrecedence;
1966 break;
1967 }
1968 case '<':
1969 case LessThanEqualOperator:
1970 case GreaterThanEqualOperator:
1971 case '>':
1972 {
1973 precedence=RelationalPrecedence;
1974 break;
1975 }
1976 case EqualOperator:
1977 case NotEqualOperator:
1978 {
1979 precedence=EquivalencyPrecedence;
1980 break;
1981 }
1982 case '&':
1983 {
1984 precedence=BitwiseAndPrecedence;
1985 break;
1986 }
1987 case '|':
1988 {
1989 precedence=BitwiseOrPrecedence;
1990 break;
1991 }
1992 case LogicalAndOperator:
1993 {
1994 precedence=LogicalAndPrecedence;
1995 break;
1996 }
1997 case LogicalOrOperator:
1998 {
1999 precedence=LogicalOrPrecedence;
2000 break;
2001 }
cristy116af162010-08-13 01:25:47 +00002002 case ExponentialNotation:
2003 {
2004 precedence=ExponentialNotationPrecedence;
2005 break;
2006 }
cristy3ed852e2009-09-05 21:47:34 +00002007 case ':':
2008 case '?':
2009 {
2010 precedence=TernaryPrecedence;
2011 break;
2012 }
2013 case '=':
2014 {
2015 precedence=AssignmentPrecedence;
2016 break;
2017 }
2018 case ',':
2019 {
2020 precedence=CommaPrecedence;
2021 break;
2022 }
2023 case ';':
2024 {
2025 precedence=SeparatorPrecedence;
2026 break;
2027 }
2028 }
2029 if ((precedence == BitwiseComplementPrecedence) ||
2030 (precedence == TernaryPrecedence) ||
2031 (precedence == AssignmentPrecedence))
2032 {
2033 if (precedence > target)
2034 {
2035 /*
2036 Right-to-left associativity.
2037 */
2038 target=precedence;
2039 subexpression=expression;
2040 }
2041 }
2042 else
2043 if (precedence >= target)
2044 {
2045 /*
2046 Left-to-right associativity.
2047 */
2048 target=precedence;
2049 subexpression=expression;
2050 }
2051 if (strchr("(",(int) *expression) != (char *) NULL)
2052 expression=FxSubexpression(expression,exception);
2053 c=(int) (*expression++);
2054 }
2055 return(subexpression);
2056}
2057
2058static MagickRealType FxEvaluateSubexpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002059 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristye85007d2010-06-06 22:51:36 +00002060 const char *expression,MagickRealType *beta,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002061{
2062 char
2063 *q,
2064 subexpression[MaxTextExtent];
2065
2066 MagickRealType
2067 alpha,
2068 gamma;
2069
2070 register const char
2071 *p;
2072
2073 *beta=0.0;
2074 if (exception->severity != UndefinedException)
2075 return(0.0);
2076 while (isspace((int) *expression) != 0)
2077 expression++;
2078 if (*expression == '\0')
2079 {
2080 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
2081 "MissingExpression","`%s'",expression);
2082 return(0.0);
2083 }
cristy66322f02010-05-17 11:40:48 +00002084 *subexpression='\0';
cristy3ed852e2009-09-05 21:47:34 +00002085 p=FxOperatorPrecedence(expression,exception);
2086 if (p != (const char *) NULL)
2087 {
2088 (void) CopyMagickString(subexpression,expression,(size_t)
2089 (p-expression+1));
2090 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2091 exception);
2092 switch ((unsigned char) *p)
2093 {
2094 case '~':
2095 {
2096 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristybb503372010-05-27 20:51:26 +00002097 *beta=(MagickRealType) (~(size_t) *beta);
cristy3ed852e2009-09-05 21:47:34 +00002098 return(*beta);
2099 }
2100 case '!':
2101 {
2102 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2103 return(*beta == 0.0 ? 1.0 : 0.0);
2104 }
2105 case '^':
2106 {
2107 *beta=pow((double) alpha,(double) FxEvaluateSubexpression(fx_info,
2108 channel,x,y,++p,beta,exception));
2109 return(*beta);
2110 }
2111 case '*':
cristy116af162010-08-13 01:25:47 +00002112 case ExponentialNotation:
cristy3ed852e2009-09-05 21:47:34 +00002113 {
2114 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2115 return(alpha*(*beta));
2116 }
2117 case '/':
2118 {
2119 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2120 if (*beta == 0.0)
2121 {
2122 if (exception->severity == UndefinedException)
2123 (void) ThrowMagickException(exception,GetMagickModule(),
2124 OptionError,"DivideByZero","`%s'",expression);
2125 return(0.0);
2126 }
2127 return(alpha/(*beta));
2128 }
2129 case '%':
2130 {
2131 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2132 *beta=fabs(floor(((double) *beta)+0.5));
2133 if (*beta == 0.0)
2134 {
2135 (void) ThrowMagickException(exception,GetMagickModule(),
2136 OptionError,"DivideByZero","`%s'",expression);
2137 return(0.0);
2138 }
2139 return(fmod((double) alpha,(double) *beta));
2140 }
2141 case '+':
2142 {
2143 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2144 return(alpha+(*beta));
2145 }
2146 case '-':
2147 {
2148 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2149 return(alpha-(*beta));
2150 }
2151 case LeftShiftOperator:
2152 {
2153 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy1707c6c2012-01-18 23:30:54 +00002154 *beta=(MagickRealType) ((size_t) (alpha+0.5) << (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002155 return(*beta);
2156 }
2157 case RightShiftOperator:
2158 {
2159 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy1707c6c2012-01-18 23:30:54 +00002160 *beta=(MagickRealType) ((size_t) (alpha+0.5) >> (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002161 return(*beta);
2162 }
2163 case '<':
2164 {
2165 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2166 return(alpha < *beta ? 1.0 : 0.0);
2167 }
2168 case LessThanEqualOperator:
2169 {
2170 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2171 return(alpha <= *beta ? 1.0 : 0.0);
2172 }
2173 case '>':
2174 {
2175 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2176 return(alpha > *beta ? 1.0 : 0.0);
2177 }
2178 case GreaterThanEqualOperator:
2179 {
2180 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2181 return(alpha >= *beta ? 1.0 : 0.0);
2182 }
2183 case EqualOperator:
2184 {
2185 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2186 return(fabs(alpha-(*beta)) <= MagickEpsilon ? 1.0 : 0.0);
2187 }
2188 case NotEqualOperator:
2189 {
2190 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2191 return(fabs(alpha-(*beta)) > MagickEpsilon ? 1.0 : 0.0);
2192 }
2193 case '&':
2194 {
2195 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy1707c6c2012-01-18 23:30:54 +00002196 *beta=(MagickRealType) ((size_t) (alpha+0.5) & (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002197 return(*beta);
2198 }
2199 case '|':
2200 {
2201 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy1707c6c2012-01-18 23:30:54 +00002202 *beta=(MagickRealType) ((size_t) (alpha+0.5) | (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002203 return(*beta);
2204 }
2205 case LogicalAndOperator:
2206 {
2207 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2208 *beta=(alpha > 0.0) && (gamma > 0.0) ? 1.0 : 0.0;
2209 return(*beta);
2210 }
2211 case LogicalOrOperator:
2212 {
2213 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2214 *beta=(alpha > 0.0) || (gamma > 0.0) ? 1.0 : 0.0;
2215 return(*beta);
2216 }
2217 case '?':
2218 {
2219 MagickRealType
2220 gamma;
2221
2222 (void) CopyMagickString(subexpression,++p,MaxTextExtent);
2223 q=subexpression;
2224 p=StringToken(":",&q);
2225 if (q == (char *) NULL)
2226 {
2227 (void) ThrowMagickException(exception,GetMagickModule(),
2228 OptionError,"UnableToParseExpression","`%s'",subexpression);
2229 return(0.0);
2230 }
2231 if (fabs((double) alpha) > MagickEpsilon)
2232 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,exception);
2233 else
2234 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,q,beta,exception);
2235 return(gamma);
2236 }
2237 case '=':
2238 {
2239 char
2240 numeric[MaxTextExtent];
2241
2242 q=subexpression;
2243 while (isalpha((int) ((unsigned char) *q)) != 0)
2244 q++;
2245 if (*q != '\0')
2246 {
2247 (void) ThrowMagickException(exception,GetMagickModule(),
2248 OptionError,"UnableToParseExpression","`%s'",subexpression);
2249 return(0.0);
2250 }
2251 ClearMagickException(exception);
2252 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristyb51dff52011-05-19 16:55:47 +00002253 (void) FormatLocaleString(numeric,MaxTextExtent,"%g",(double)
cristy8cd5b312010-01-07 01:10:24 +00002254 *beta);
cristy3ed852e2009-09-05 21:47:34 +00002255 (void) DeleteNodeFromSplayTree(fx_info->symbols,subexpression);
2256 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(
2257 subexpression),ConstantString(numeric));
2258 return(*beta);
2259 }
2260 case ',':
2261 {
2262 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2263 return(alpha);
2264 }
2265 case ';':
2266 {
2267 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2268 return(*beta);
2269 }
2270 default:
2271 {
2272 gamma=alpha*FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,
2273 exception);
2274 return(gamma);
2275 }
2276 }
2277 }
2278 if (strchr("(",(int) *expression) != (char *) NULL)
2279 {
2280 (void) CopyMagickString(subexpression,expression+1,MaxTextExtent);
2281 subexpression[strlen(subexpression)-1]='\0';
2282 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2283 exception);
2284 return(gamma);
2285 }
cristy8b8a3ae2010-10-23 18:49:46 +00002286 switch (*expression)
cristy3ed852e2009-09-05 21:47:34 +00002287 {
2288 case '+':
2289 {
2290 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2291 exception);
2292 return(1.0*gamma);
2293 }
2294 case '-':
2295 {
2296 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2297 exception);
2298 return(-1.0*gamma);
2299 }
2300 case '~':
2301 {
2302 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2303 exception);
cristybb503372010-05-27 20:51:26 +00002304 return((MagickRealType) (~(size_t) (gamma+0.5)));
cristy3ed852e2009-09-05 21:47:34 +00002305 }
2306 case 'A':
2307 case 'a':
2308 {
2309 if (LocaleNCompare(expression,"abs",3) == 0)
2310 {
2311 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2312 exception);
2313 return((MagickRealType) fabs((double) alpha));
2314 }
cristyb33454f2011-08-03 02:10:45 +00002315#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00002316 if (LocaleNCompare(expression,"acosh",5) == 0)
2317 {
2318 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2319 exception);
2320 return((MagickRealType) acosh((double) alpha));
2321 }
cristyb33454f2011-08-03 02:10:45 +00002322#endif
cristy3ed852e2009-09-05 21:47:34 +00002323 if (LocaleNCompare(expression,"acos",4) == 0)
2324 {
2325 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2326 exception);
2327 return((MagickRealType) acos((double) alpha));
2328 }
cristy43c22f42010-03-30 12:34:07 +00002329#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002330 if (LocaleNCompare(expression,"airy",4) == 0)
2331 {
2332 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2333 exception);
2334 if (alpha == 0.0)
cristy2dd03222010-03-30 22:12:11 +00002335 return(1.0);
2336 gamma=2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha);
cristy43c22f42010-03-30 12:34:07 +00002337 return(gamma*gamma);
cristyee56cf12010-03-01 22:17:06 +00002338 }
cristy43c22f42010-03-30 12:34:07 +00002339#endif
cristyb33454f2011-08-03 02:10:45 +00002340#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00002341 if (LocaleNCompare(expression,"asinh",5) == 0)
2342 {
2343 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2344 exception);
2345 return((MagickRealType) asinh((double) alpha));
2346 }
cristyb33454f2011-08-03 02:10:45 +00002347#endif
cristy3ed852e2009-09-05 21:47:34 +00002348 if (LocaleNCompare(expression,"asin",4) == 0)
2349 {
2350 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2351 exception);
2352 return((MagickRealType) asin((double) alpha));
2353 }
2354 if (LocaleNCompare(expression,"alt",3) == 0)
2355 {
2356 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2357 exception);
cristybb503372010-05-27 20:51:26 +00002358 return(((ssize_t) alpha) & 0x01 ? -1.0 : 1.0);
cristy3ed852e2009-09-05 21:47:34 +00002359 }
2360 if (LocaleNCompare(expression,"atan2",5) == 0)
2361 {
2362 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2363 exception);
2364 return((MagickRealType) atan2((double) alpha,(double) *beta));
2365 }
cristyb33454f2011-08-03 02:10:45 +00002366#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00002367 if (LocaleNCompare(expression,"atanh",5) == 0)
2368 {
2369 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2370 exception);
2371 return((MagickRealType) atanh((double) alpha));
2372 }
cristyb33454f2011-08-03 02:10:45 +00002373#endif
cristy3ed852e2009-09-05 21:47:34 +00002374 if (LocaleNCompare(expression,"atan",4) == 0)
2375 {
2376 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2377 exception);
2378 return((MagickRealType) atan((double) alpha));
2379 }
2380 if (LocaleCompare(expression,"a") == 0)
2381 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2382 break;
2383 }
2384 case 'B':
2385 case 'b':
2386 {
2387 if (LocaleCompare(expression,"b") == 0)
2388 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2389 break;
2390 }
2391 case 'C':
2392 case 'c':
2393 {
2394 if (LocaleNCompare(expression,"ceil",4) == 0)
2395 {
2396 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2397 exception);
2398 return((MagickRealType) ceil((double) alpha));
2399 }
2400 if (LocaleNCompare(expression,"cosh",4) == 0)
2401 {
2402 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2403 exception);
2404 return((MagickRealType) cosh((double) alpha));
2405 }
2406 if (LocaleNCompare(expression,"cos",3) == 0)
2407 {
2408 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2409 exception);
2410 return((MagickRealType) cos((double) alpha));
2411 }
2412 if (LocaleCompare(expression,"c") == 0)
2413 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2414 break;
2415 }
2416 case 'D':
2417 case 'd':
2418 {
2419 if (LocaleNCompare(expression,"debug",5) == 0)
2420 {
2421 const char
2422 *type;
2423
2424 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2425 exception);
2426 if (fx_info->images->colorspace == CMYKColorspace)
2427 switch (channel)
2428 {
cristy0568ffc2011-07-25 16:54:14 +00002429 case CyanPixelChannel: type="cyan"; break;
2430 case MagentaPixelChannel: type="magenta"; break;
2431 case YellowPixelChannel: type="yellow"; break;
2432 case AlphaPixelChannel: type="opacity"; break;
2433 case BlackPixelChannel: type="black"; break;
cristy3ed852e2009-09-05 21:47:34 +00002434 default: type="unknown"; break;
2435 }
2436 else
2437 switch (channel)
2438 {
cristy0568ffc2011-07-25 16:54:14 +00002439 case RedPixelChannel: type="red"; break;
2440 case GreenPixelChannel: type="green"; break;
2441 case BluePixelChannel: type="blue"; break;
2442 case AlphaPixelChannel: type="opacity"; break;
cristy3ed852e2009-09-05 21:47:34 +00002443 default: type="unknown"; break;
2444 }
2445 (void) CopyMagickString(subexpression,expression+6,MaxTextExtent);
2446 if (strlen(subexpression) > 1)
2447 subexpression[strlen(subexpression)-1]='\0';
2448 if (fx_info->file != (FILE *) NULL)
cristy1707c6c2012-01-18 23:30:54 +00002449 (void) FormatLocaleFile(fx_info->file,"%s[%.20g,%.20g].%s: "
2450 "%s=%.*g\n",fx_info->images->filename,(double) x,(double) y,type,
2451 subexpression,GetMagickPrecision(),(double) alpha);
cristy3ed852e2009-09-05 21:47:34 +00002452 return(0.0);
2453 }
cristy5597a8d2011-11-04 00:25:32 +00002454 if (LocaleNCompare(expression,"drc",3) == 0)
2455 {
2456 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2457 exception);
2458 return((MagickRealType) (alpha/(*beta*(alpha-1.0)+1.0)));
2459 }
cristy3ed852e2009-09-05 21:47:34 +00002460 break;
2461 }
2462 case 'E':
2463 case 'e':
2464 {
2465 if (LocaleCompare(expression,"epsilon") == 0)
2466 return((MagickRealType) MagickEpsilon);
2467 if (LocaleNCompare(expression,"exp",3) == 0)
2468 {
2469 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2470 exception);
2471 return((MagickRealType) exp((double) alpha));
2472 }
2473 if (LocaleCompare(expression,"e") == 0)
2474 return((MagickRealType) 2.7182818284590452354);
2475 break;
2476 }
2477 case 'F':
2478 case 'f':
2479 {
2480 if (LocaleNCompare(expression,"floor",5) == 0)
2481 {
2482 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2483 exception);
2484 return((MagickRealType) floor((double) alpha));
2485 }
2486 break;
2487 }
2488 case 'G':
2489 case 'g':
2490 {
cristy9eeedea2011-11-02 19:04:05 +00002491 if (LocaleNCompare(expression,"gauss",5) == 0)
2492 {
2493 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2494 exception);
2495 gamma=exp((double) (-alpha*alpha/2.0))/sqrt(2.0*MagickPI);
2496 return((MagickRealType) gamma);
2497 }
cristyb0aad4c2011-11-02 19:30:35 +00002498 if (LocaleNCompare(expression,"gcd",3) == 0)
2499 {
2500 MagickOffsetType
2501 gcd;
2502
2503 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2504 exception);
cristy1707c6c2012-01-18 23:30:54 +00002505 gcd=FxGCD((MagickOffsetType) (alpha+0.5),(MagickOffsetType) (*beta+
2506 0.5));
cristyb0aad4c2011-11-02 19:30:35 +00002507 return((MagickRealType) gcd);
2508 }
cristy3ed852e2009-09-05 21:47:34 +00002509 if (LocaleCompare(expression,"g") == 0)
2510 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2511 break;
2512 }
2513 case 'H':
2514 case 'h':
2515 {
2516 if (LocaleCompare(expression,"h") == 0)
2517 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2518 if (LocaleCompare(expression,"hue") == 0)
2519 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2520 if (LocaleNCompare(expression,"hypot",5) == 0)
2521 {
2522 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2523 exception);
2524 return((MagickRealType) hypot((double) alpha,(double) *beta));
2525 }
2526 break;
2527 }
2528 case 'K':
2529 case 'k':
2530 {
2531 if (LocaleCompare(expression,"k") == 0)
2532 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2533 break;
2534 }
2535 case 'I':
2536 case 'i':
2537 {
2538 if (LocaleCompare(expression,"intensity") == 0)
2539 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2540 if (LocaleNCompare(expression,"int",3) == 0)
2541 {
2542 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2543 exception);
cristy16788e42010-08-13 13:44:26 +00002544 return((MagickRealType) floor(alpha));
cristy3ed852e2009-09-05 21:47:34 +00002545 }
cristy82b20722011-11-05 21:52:36 +00002546#if defined(MAGICKCORE_HAVE_ISNAN)
cristy639399c2011-11-02 19:16:15 +00002547 if (LocaleNCompare(expression,"isnan",5) == 0)
2548 {
2549 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2550 exception);
cristy17a10202011-11-02 19:17:04 +00002551 return((MagickRealType) !!isnan((double) alpha));
cristy639399c2011-11-02 19:16:15 +00002552 }
cristy82b20722011-11-05 21:52:36 +00002553#endif
cristy3ed852e2009-09-05 21:47:34 +00002554 if (LocaleCompare(expression,"i") == 0)
2555 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2556 break;
2557 }
2558 case 'J':
2559 case 'j':
2560 {
2561 if (LocaleCompare(expression,"j") == 0)
2562 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
cristy161b9262010-03-20 19:34:32 +00002563#if defined(MAGICKCORE_HAVE_J0)
cristyee56cf12010-03-01 22:17:06 +00002564 if (LocaleNCompare(expression,"j0",2) == 0)
2565 {
2566 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2567 exception);
2568 return((MagickRealType) j0((double) alpha));
2569 }
cristy161b9262010-03-20 19:34:32 +00002570#endif
2571#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002572 if (LocaleNCompare(expression,"j1",2) == 0)
2573 {
2574 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2575 exception);
2576 return((MagickRealType) j1((double) alpha));
2577 }
cristy161b9262010-03-20 19:34:32 +00002578#endif
cristyaa018fa2010-04-08 23:03:54 +00002579#if defined(MAGICKCORE_HAVE_J1)
cristya6a09e72010-03-02 14:51:02 +00002580 if (LocaleNCompare(expression,"jinc",4) == 0)
2581 {
2582 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2583 exception);
cristy0946a822010-03-12 17:14:58 +00002584 if (alpha == 0.0)
2585 return(1.0);
cristy1707c6c2012-01-18 23:30:54 +00002586 gamma=(MagickRealType) (2.0*j1((double) (MagickPI*alpha))/(MagickPI*
2587 alpha));
cristyfce2f7b2010-03-12 00:29:49 +00002588 return(gamma);
cristya6a09e72010-03-02 14:51:02 +00002589 }
cristyaa018fa2010-04-08 23:03:54 +00002590#endif
cristy3ed852e2009-09-05 21:47:34 +00002591 break;
2592 }
2593 case 'L':
2594 case 'l':
2595 {
2596 if (LocaleNCompare(expression,"ln",2) == 0)
2597 {
2598 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2599 exception);
2600 return((MagickRealType) log((double) alpha));
2601 }
cristyc8ed5322010-08-31 12:07:59 +00002602 if (LocaleNCompare(expression,"logtwo",6) == 0)
cristy3ed852e2009-09-05 21:47:34 +00002603 {
cristyc8ed5322010-08-31 12:07:59 +00002604 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
cristy3ed852e2009-09-05 21:47:34 +00002605 exception);
2606 return((MagickRealType) log10((double) alpha))/log10(2.0);
2607 }
2608 if (LocaleNCompare(expression,"log",3) == 0)
2609 {
2610 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2611 exception);
2612 return((MagickRealType) log10((double) alpha));
2613 }
2614 if (LocaleCompare(expression,"lightness") == 0)
2615 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2616 break;
2617 }
2618 case 'M':
2619 case 'm':
2620 {
2621 if (LocaleCompare(expression,"MaxRGB") == 0)
2622 return((MagickRealType) QuantumRange);
2623 if (LocaleNCompare(expression,"maxima",6) == 0)
2624 break;
2625 if (LocaleNCompare(expression,"max",3) == 0)
cristy984049c2011-11-03 18:34:58 +00002626 {
2627 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2628 exception);
2629 return(alpha > *beta ? alpha : *beta);
2630 }
cristy3ed852e2009-09-05 21:47:34 +00002631 if (LocaleNCompare(expression,"minima",6) == 0)
2632 break;
2633 if (LocaleNCompare(expression,"min",3) == 0)
cristy984049c2011-11-03 18:34:58 +00002634 {
2635 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2636 exception);
2637 return(alpha < *beta ? alpha : *beta);
2638 }
cristy3ed852e2009-09-05 21:47:34 +00002639 if (LocaleNCompare(expression,"mod",3) == 0)
2640 {
2641 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2642 exception);
cristy984049c2011-11-03 18:34:58 +00002643 gamma=alpha-floor((double) (alpha/(*beta)))*(*beta);
2644 return(gamma);
cristy3ed852e2009-09-05 21:47:34 +00002645 }
2646 if (LocaleCompare(expression,"m") == 0)
2647 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2648 break;
2649 }
2650 case 'N':
2651 case 'n':
2652 {
cristyad3502e2011-11-02 19:10:45 +00002653 if (LocaleNCompare(expression,"not",3) == 0)
2654 {
2655 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2656 exception);
2657 return((MagickRealType) (alpha < MagickEpsilon));
2658 }
cristy3ed852e2009-09-05 21:47:34 +00002659 if (LocaleCompare(expression,"n") == 0)
2660 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2661 break;
2662 }
2663 case 'O':
2664 case 'o':
2665 {
2666 if (LocaleCompare(expression,"Opaque") == 0)
2667 return(1.0);
2668 if (LocaleCompare(expression,"o") == 0)
2669 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2670 break;
2671 }
2672 case 'P':
2673 case 'p':
2674 {
cristy670aa3c2011-11-03 00:54:00 +00002675 if (LocaleCompare(expression,"phi") == 0)
2676 return((MagickRealType) MagickPHI);
cristy3ed852e2009-09-05 21:47:34 +00002677 if (LocaleCompare(expression,"pi") == 0)
2678 return((MagickRealType) MagickPI);
2679 if (LocaleNCompare(expression,"pow",3) == 0)
2680 {
2681 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2682 exception);
2683 return((MagickRealType) pow((double) alpha,(double) *beta));
2684 }
2685 if (LocaleCompare(expression,"p") == 0)
2686 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2687 break;
2688 }
2689 case 'Q':
2690 case 'q':
2691 {
2692 if (LocaleCompare(expression,"QuantumRange") == 0)
2693 return((MagickRealType) QuantumRange);
2694 if (LocaleCompare(expression,"QuantumScale") == 0)
2695 return((MagickRealType) QuantumScale);
2696 break;
2697 }
2698 case 'R':
2699 case 'r':
2700 {
2701 if (LocaleNCompare(expression,"rand",4) == 0)
2702 return((MagickRealType) GetPseudoRandomValue(fx_info->random_info));
2703 if (LocaleNCompare(expression,"round",5) == 0)
2704 {
2705 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2706 exception);
cristy16788e42010-08-13 13:44:26 +00002707 return((MagickRealType) floor((double) alpha+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002708 }
2709 if (LocaleCompare(expression,"r") == 0)
2710 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2711 break;
2712 }
2713 case 'S':
2714 case 's':
2715 {
2716 if (LocaleCompare(expression,"saturation") == 0)
2717 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2718 if (LocaleNCompare(expression,"sign",4) == 0)
2719 {
2720 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2721 exception);
2722 return(alpha < 0.0 ? -1.0 : 1.0);
2723 }
cristya6a09e72010-03-02 14:51:02 +00002724 if (LocaleNCompare(expression,"sinc",4) == 0)
2725 {
2726 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2727 exception);
2728 if (alpha == 0)
2729 return(1.0);
2730 gamma=(MagickRealType) (sin((double) (MagickPI*alpha))/
2731 (MagickPI*alpha));
2732 return(gamma);
2733 }
cristy3ed852e2009-09-05 21:47:34 +00002734 if (LocaleNCompare(expression,"sinh",4) == 0)
2735 {
2736 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2737 exception);
2738 return((MagickRealType) sinh((double) alpha));
2739 }
2740 if (LocaleNCompare(expression,"sin",3) == 0)
2741 {
2742 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2743 exception);
2744 return((MagickRealType) sin((double) alpha));
2745 }
2746 if (LocaleNCompare(expression,"sqrt",4) == 0)
2747 {
2748 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2749 exception);
2750 return((MagickRealType) sqrt((double) alpha));
2751 }
cristy9eeedea2011-11-02 19:04:05 +00002752 if (LocaleNCompare(expression,"squish",6) == 0)
2753 {
2754 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2755 exception);
2756 return((MagickRealType) (1.0/(1.0+exp((double) (4.0*alpha)))));
2757 }
cristy3ed852e2009-09-05 21:47:34 +00002758 if (LocaleCompare(expression,"s") == 0)
2759 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2760 break;
2761 }
2762 case 'T':
2763 case 't':
2764 {
2765 if (LocaleNCompare(expression,"tanh",4) == 0)
2766 {
2767 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2768 exception);
2769 return((MagickRealType) tanh((double) alpha));
2770 }
2771 if (LocaleNCompare(expression,"tan",3) == 0)
2772 {
2773 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2774 exception);
2775 return((MagickRealType) tan((double) alpha));
2776 }
2777 if (LocaleCompare(expression,"Transparent") == 0)
2778 return(0.0);
cristy16788e42010-08-13 13:44:26 +00002779 if (LocaleNCompare(expression,"trunc",5) == 0)
2780 {
2781 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2782 exception);
2783 if (alpha >= 0.0)
2784 return((MagickRealType) floor((double) alpha));
2785 return((MagickRealType) ceil((double) alpha));
2786 }
cristy3ed852e2009-09-05 21:47:34 +00002787 if (LocaleCompare(expression,"t") == 0)
2788 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2789 break;
2790 }
2791 case 'U':
2792 case 'u':
2793 {
2794 if (LocaleCompare(expression,"u") == 0)
2795 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2796 break;
2797 }
2798 case 'V':
2799 case 'v':
2800 {
2801 if (LocaleCompare(expression,"v") == 0)
2802 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2803 break;
2804 }
2805 case 'W':
2806 case 'w':
2807 {
cristy9eeedea2011-11-02 19:04:05 +00002808 if (LocaleNCompare(expression,"while",5) == 0)
2809 {
2810 do
2811 {
2812 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2813 exception);
2814 } while (fabs((double) alpha) >= MagickEpsilon);
2815 return((MagickRealType) *beta);
2816 }
cristy3ed852e2009-09-05 21:47:34 +00002817 if (LocaleCompare(expression,"w") == 0)
2818 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2819 break;
2820 }
2821 case 'Y':
2822 case 'y':
2823 {
2824 if (LocaleCompare(expression,"y") == 0)
2825 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2826 break;
2827 }
2828 case 'Z':
2829 case 'z':
2830 {
2831 if (LocaleCompare(expression,"z") == 0)
2832 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2833 break;
2834 }
2835 default:
2836 break;
2837 }
2838 q=(char *) expression;
cristydbdd0e32011-11-04 23:29:40 +00002839 alpha=InterpretSiPrefixValue(expression,&q);
cristy3ed852e2009-09-05 21:47:34 +00002840 if (q == expression)
2841 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2842 return(alpha);
2843}
2844
cristy7832dc22011-09-05 01:21:53 +00002845MagickPrivate MagickBooleanType FxEvaluateExpression(FxInfo *fx_info,
cristy3ed852e2009-09-05 21:47:34 +00002846 MagickRealType *alpha,ExceptionInfo *exception)
2847{
2848 MagickBooleanType
2849 status;
2850
cristy541ae572011-08-05 19:08:59 +00002851 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2852 exception);
cristy3ed852e2009-09-05 21:47:34 +00002853 return(status);
2854}
2855
2856MagickExport MagickBooleanType FxPreprocessExpression(FxInfo *fx_info,
2857 MagickRealType *alpha,ExceptionInfo *exception)
2858{
2859 FILE
2860 *file;
2861
2862 MagickBooleanType
2863 status;
2864
2865 file=fx_info->file;
2866 fx_info->file=(FILE *) NULL;
cristy541ae572011-08-05 19:08:59 +00002867 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2868 exception);
cristy3ed852e2009-09-05 21:47:34 +00002869 fx_info->file=file;
2870 return(status);
2871}
2872
cristy7832dc22011-09-05 01:21:53 +00002873MagickPrivate MagickBooleanType FxEvaluateChannelExpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002874 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristye85007d2010-06-06 22:51:36 +00002875 MagickRealType *alpha,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002876{
2877 MagickRealType
2878 beta;
2879
2880 beta=0.0;
2881 *alpha=FxEvaluateSubexpression(fx_info,channel,x,y,fx_info->expression,&beta,
2882 exception);
2883 return(exception->severity == OptionError ? MagickFalse : MagickTrue);
2884}
2885
2886/*
2887%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2888% %
2889% %
2890% %
2891% F x I m a g e %
2892% %
2893% %
2894% %
2895%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2896%
2897% FxImage() applies a mathematical expression to the specified image.
2898%
2899% The format of the FxImage method is:
2900%
2901% Image *FxImage(const Image *image,const char *expression,
2902% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002903%
2904% A description of each parameter follows:
2905%
2906% o image: the image.
2907%
cristy3ed852e2009-09-05 21:47:34 +00002908% o expression: A mathematical expression.
2909%
2910% o exception: return any errors or warnings in this structure.
2911%
2912*/
2913
2914static FxInfo **DestroyFxThreadSet(FxInfo **fx_info)
2915{
cristybb503372010-05-27 20:51:26 +00002916 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002917 i;
2918
2919 assert(fx_info != (FxInfo **) NULL);
cristybb503372010-05-27 20:51:26 +00002920 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
cristy3ed852e2009-09-05 21:47:34 +00002921 if (fx_info[i] != (FxInfo *) NULL)
2922 fx_info[i]=DestroyFxInfo(fx_info[i]);
cristyb41ee102010-10-04 16:46:15 +00002923 fx_info=(FxInfo **) RelinquishMagickMemory(fx_info);
cristy3ed852e2009-09-05 21:47:34 +00002924 return(fx_info);
2925}
2926
2927static FxInfo **AcquireFxThreadSet(const Image *image,const char *expression,
2928 ExceptionInfo *exception)
2929{
2930 char
2931 *fx_expression;
2932
2933 FxInfo
2934 **fx_info;
2935
2936 MagickRealType
2937 alpha;
2938
cristybb503372010-05-27 20:51:26 +00002939 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002940 i;
2941
cristybb503372010-05-27 20:51:26 +00002942 size_t
cristy3ed852e2009-09-05 21:47:34 +00002943 number_threads;
2944
2945 number_threads=GetOpenMPMaximumThreads();
cristyb41ee102010-10-04 16:46:15 +00002946 fx_info=(FxInfo **) AcquireQuantumMemory(number_threads,sizeof(*fx_info));
cristy3ed852e2009-09-05 21:47:34 +00002947 if (fx_info == (FxInfo **) NULL)
2948 return((FxInfo **) NULL);
2949 (void) ResetMagickMemory(fx_info,0,number_threads*sizeof(*fx_info));
2950 if (*expression != '@')
2951 fx_expression=ConstantString(expression);
2952 else
2953 fx_expression=FileToString(expression+1,~0,exception);
cristybb503372010-05-27 20:51:26 +00002954 for (i=0; i < (ssize_t) number_threads; i++)
cristy3ed852e2009-09-05 21:47:34 +00002955 {
2956 fx_info[i]=AcquireFxInfo(image,fx_expression);
2957 if (fx_info[i] == (FxInfo *) NULL)
2958 return(DestroyFxThreadSet(fx_info));
2959 (void) FxPreprocessExpression(fx_info[i],&alpha,fx_info[i]->exception);
2960 }
2961 fx_expression=DestroyString(fx_expression);
2962 return(fx_info);
2963}
2964
2965MagickExport Image *FxImage(const Image *image,const char *expression,
2966 ExceptionInfo *exception)
2967{
cristy3ed852e2009-09-05 21:47:34 +00002968#define FxImageTag "Fx/Image"
2969
cristyfa112112010-01-04 17:48:07 +00002970 CacheView
cristy79cedc72011-07-25 00:41:15 +00002971 *fx_view,
2972 *image_view;
cristyfa112112010-01-04 17:48:07 +00002973
cristy3ed852e2009-09-05 21:47:34 +00002974 FxInfo
cristyfa112112010-01-04 17:48:07 +00002975 **restrict fx_info;
cristy3ed852e2009-09-05 21:47:34 +00002976
2977 Image
2978 *fx_image;
2979
cristy3ed852e2009-09-05 21:47:34 +00002980 MagickBooleanType
2981 status;
2982
cristybb503372010-05-27 20:51:26 +00002983 MagickOffsetType
2984 progress;
2985
cristy3ed852e2009-09-05 21:47:34 +00002986 MagickRealType
2987 alpha;
2988
cristybb503372010-05-27 20:51:26 +00002989 ssize_t
2990 y;
2991
cristy3ed852e2009-09-05 21:47:34 +00002992 assert(image != (Image *) NULL);
2993 assert(image->signature == MagickSignature);
2994 if (image->debug != MagickFalse)
2995 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy79cedc72011-07-25 00:41:15 +00002996 fx_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00002997 if (fx_image == (Image *) NULL)
2998 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00002999 if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003000 {
cristy3ed852e2009-09-05 21:47:34 +00003001 fx_image=DestroyImage(fx_image);
3002 return((Image *) NULL);
3003 }
3004 fx_info=AcquireFxThreadSet(image,expression,exception);
3005 if (fx_info == (FxInfo **) NULL)
3006 {
3007 fx_image=DestroyImage(fx_image);
3008 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3009 }
3010 status=FxPreprocessExpression(fx_info[0],&alpha,exception);
3011 if (status == MagickFalse)
3012 {
3013 fx_image=DestroyImage(fx_image);
3014 fx_info=DestroyFxThreadSet(fx_info);
3015 return((Image *) NULL);
3016 }
3017 /*
3018 Fx image.
3019 */
3020 status=MagickTrue;
3021 progress=0;
cristy79cedc72011-07-25 00:41:15 +00003022 image_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00003023 fx_view=AcquireCacheView(fx_image);
cristyb5d5f722009-11-04 03:03:49 +00003024#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003025 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003026#endif
cristybb503372010-05-27 20:51:26 +00003027 for (y=0; y < (ssize_t) fx_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003028 {
cristy5c9e6f22010-09-17 17:31:01 +00003029 const int
3030 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00003031
cristy79cedc72011-07-25 00:41:15 +00003032 register const Quantum
3033 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003034
cristy4c08aed2011-07-01 19:47:50 +00003035 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003036 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003037
cristy79cedc72011-07-25 00:41:15 +00003038 register ssize_t
3039 x;
3040
cristy3ed852e2009-09-05 21:47:34 +00003041 if (status == MagickFalse)
3042 continue;
cristy79cedc72011-07-25 00:41:15 +00003043 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +00003044 q=QueueCacheViewAuthenticPixels(fx_view,0,y,fx_image->columns,1,exception);
cristy79cedc72011-07-25 00:41:15 +00003045 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003046 {
3047 status=MagickFalse;
3048 continue;
3049 }
cristybb503372010-05-27 20:51:26 +00003050 for (x=0; x < (ssize_t) fx_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003051 {
cristy79cedc72011-07-25 00:41:15 +00003052 register ssize_t
3053 i;
3054
3055 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3056 {
3057 MagickRealType
3058 alpha;
3059
3060 PixelChannel
3061 channel;
3062
3063 PixelTrait
3064 fx_traits,
3065 traits;
3066
cristye2a912b2011-12-05 20:02:07 +00003067 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003068 traits=GetPixelChannelMapTraits(image,channel);
cristy79cedc72011-07-25 00:41:15 +00003069 fx_traits=GetPixelChannelMapTraits(fx_image,channel);
cristy010d7d12011-08-31 01:02:48 +00003070 if ((traits == UndefinedPixelTrait) ||
3071 (fx_traits == UndefinedPixelTrait))
cristy79cedc72011-07-25 00:41:15 +00003072 continue;
cristyd09f8802012-02-04 16:44:10 +00003073 if (((fx_traits & CopyPixelTrait) != 0) ||
3074 (GetPixelMask(image,p) != 0))
cristy79cedc72011-07-25 00:41:15 +00003075 {
cristy0beccfa2011-09-25 20:47:53 +00003076 SetPixelChannel(fx_image,channel,p[i],q);
cristy79cedc72011-07-25 00:41:15 +00003077 continue;
3078 }
3079 alpha=0.0;
cristya382aca2011-12-06 18:22:48 +00003080 (void) FxEvaluateChannelExpression(fx_info[id],channel,x,y,&alpha,
3081 exception);
cristyb3a73b52011-07-26 01:34:43 +00003082 q[i]=ClampToQuantum((MagickRealType) QuantumRange*alpha);
cristy79cedc72011-07-25 00:41:15 +00003083 }
3084 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003085 q+=GetPixelChannels(fx_image);
cristy3ed852e2009-09-05 21:47:34 +00003086 }
3087 if (SyncCacheViewAuthenticPixels(fx_view,exception) == MagickFalse)
3088 status=MagickFalse;
3089 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3090 {
3091 MagickBooleanType
3092 proceed;
3093
cristyb5d5f722009-11-04 03:03:49 +00003094#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003095 #pragma omp critical (MagickCore_FxImage)
cristy3ed852e2009-09-05 21:47:34 +00003096#endif
3097 proceed=SetImageProgress(image,FxImageTag,progress++,image->rows);
3098 if (proceed == MagickFalse)
3099 status=MagickFalse;
3100 }
3101 }
cristy3ed852e2009-09-05 21:47:34 +00003102 fx_view=DestroyCacheView(fx_view);
cristy79cedc72011-07-25 00:41:15 +00003103 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003104 fx_info=DestroyFxThreadSet(fx_info);
3105 if (status == MagickFalse)
3106 fx_image=DestroyImage(fx_image);
3107 return(fx_image);
3108}
3109
3110/*
3111%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3112% %
3113% %
3114% %
3115% I m p l o d e I m a g e %
3116% %
3117% %
3118% %
3119%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3120%
3121% ImplodeImage() creates a new image that is a copy of an existing
3122% one with the image pixels "implode" by the specified percentage. It
3123% allocates the memory necessary for the new Image structure and returns a
3124% pointer to the new image.
3125%
3126% The format of the ImplodeImage method is:
3127%
3128% Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003129% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003130%
3131% A description of each parameter follows:
3132%
3133% o implode_image: Method ImplodeImage returns a pointer to the image
3134% after it is implode. A null image is returned if there is a memory
3135% shortage.
3136%
3137% o image: the image.
3138%
3139% o amount: Define the extent of the implosion.
3140%
cristy76f512e2011-09-12 01:26:56 +00003141% o method: the pixel interpolation method.
3142%
cristy3ed852e2009-09-05 21:47:34 +00003143% o exception: return any errors or warnings in this structure.
3144%
3145*/
3146MagickExport Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003147 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003148{
3149#define ImplodeImageTag "Implode/Image"
3150
cristyfa112112010-01-04 17:48:07 +00003151 CacheView
3152 *image_view,
3153 *implode_view;
3154
cristy3ed852e2009-09-05 21:47:34 +00003155 Image
3156 *implode_image;
3157
cristy3ed852e2009-09-05 21:47:34 +00003158 MagickBooleanType
3159 status;
3160
cristybb503372010-05-27 20:51:26 +00003161 MagickOffsetType
3162 progress;
3163
cristy3ed852e2009-09-05 21:47:34 +00003164 MagickRealType
3165 radius;
3166
3167 PointInfo
3168 center,
3169 scale;
3170
cristybb503372010-05-27 20:51:26 +00003171 ssize_t
3172 y;
3173
cristy3ed852e2009-09-05 21:47:34 +00003174 /*
3175 Initialize implode image attributes.
3176 */
3177 assert(image != (Image *) NULL);
3178 assert(image->signature == MagickSignature);
3179 if (image->debug != MagickFalse)
3180 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3181 assert(exception != (ExceptionInfo *) NULL);
3182 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00003183 implode_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3184 exception);
cristy3ed852e2009-09-05 21:47:34 +00003185 if (implode_image == (Image *) NULL)
3186 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003187 if (SetImageStorageClass(implode_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003188 {
cristy3ed852e2009-09-05 21:47:34 +00003189 implode_image=DestroyImage(implode_image);
3190 return((Image *) NULL);
3191 }
cristy4c08aed2011-07-01 19:47:50 +00003192 if (implode_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00003193 implode_image->matte=MagickTrue;
3194 /*
3195 Compute scaling factor.
3196 */
3197 scale.x=1.0;
3198 scale.y=1.0;
3199 center.x=0.5*image->columns;
3200 center.y=0.5*image->rows;
3201 radius=center.x;
3202 if (image->columns > image->rows)
3203 scale.y=(double) image->columns/(double) image->rows;
3204 else
3205 if (image->columns < image->rows)
3206 {
3207 scale.x=(double) image->rows/(double) image->columns;
3208 radius=center.y;
3209 }
3210 /*
3211 Implode image.
3212 */
3213 status=MagickTrue;
3214 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00003215 image_view=AcquireCacheView(image);
3216 implode_view=AcquireCacheView(implode_image);
cristyb5d5f722009-11-04 03:03:49 +00003217#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003218 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003219#endif
cristybb503372010-05-27 20:51:26 +00003220 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003221 {
cristy3ed852e2009-09-05 21:47:34 +00003222 MagickRealType
3223 distance;
3224
3225 PointInfo
3226 delta;
3227
cristy6d188022011-09-12 13:23:33 +00003228 register const Quantum
3229 *restrict p;
3230
cristybb503372010-05-27 20:51:26 +00003231 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003232 x;
3233
cristy4c08aed2011-07-01 19:47:50 +00003234 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003235 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003236
3237 if (status == MagickFalse)
3238 continue;
cristy6d188022011-09-12 13:23:33 +00003239 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +00003240 q=QueueCacheViewAuthenticPixels(implode_view,0,y,implode_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003241 exception);
cristy6d188022011-09-12 13:23:33 +00003242 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003243 {
3244 status=MagickFalse;
3245 continue;
3246 }
cristy3ed852e2009-09-05 21:47:34 +00003247 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00003248 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003249 {
cristy6d188022011-09-12 13:23:33 +00003250 register ssize_t
3251 i;
3252
cristy3ed852e2009-09-05 21:47:34 +00003253 /*
3254 Determine if the pixel is within an ellipse.
3255 */
cristy10a6c612012-01-29 21:41:05 +00003256 if (GetPixelMask(image,p) != 0)
3257 {
3258 p+=GetPixelChannels(image);
3259 q+=GetPixelChannels(implode_image);
3260 continue;
3261 }
cristy3ed852e2009-09-05 21:47:34 +00003262 delta.x=scale.x*(double) (x-center.x);
3263 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00003264 if (distance >= (radius*radius))
cristya6d7a9b2012-01-18 20:04:48 +00003265 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy6d188022011-09-12 13:23:33 +00003266 {
cristya6d7a9b2012-01-18 20:04:48 +00003267 PixelChannel
3268 channel;
3269
cristy1707c6c2012-01-18 23:30:54 +00003270 PixelTrait
3271 implode_traits,
3272 traits;
3273
cristya6d7a9b2012-01-18 20:04:48 +00003274 channel=GetPixelChannelMapChannel(image,i);
cristy1707c6c2012-01-18 23:30:54 +00003275 traits=GetPixelChannelMapTraits(image,channel);
3276 implode_traits=GetPixelChannelMapTraits(implode_image,channel);
3277 if ((traits == UndefinedPixelTrait) ||
3278 (implode_traits == UndefinedPixelTrait))
3279 continue;
cristya6d7a9b2012-01-18 20:04:48 +00003280 SetPixelChannel(implode_image,channel,p[i],q);
cristy6d188022011-09-12 13:23:33 +00003281 }
3282 else
cristy3ed852e2009-09-05 21:47:34 +00003283 {
3284 double
3285 factor;
3286
3287 /*
3288 Implode the pixel.
3289 */
3290 factor=1.0;
3291 if (distance > 0.0)
cristy1707c6c2012-01-18 23:30:54 +00003292 factor=pow(sin((double) (MagickPI*sqrt((double) distance)/radius/
3293 2)),-amount);
cristy76f512e2011-09-12 01:26:56 +00003294 status=InterpolatePixelChannels(image,image_view,implode_image,method,
3295 (double) (factor*delta.x/scale.x+center.x),(double) (factor*delta.y/
3296 scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00003297 }
cristy6d188022011-09-12 13:23:33 +00003298 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003299 q+=GetPixelChannels(implode_image);
cristy3ed852e2009-09-05 21:47:34 +00003300 }
3301 if (SyncCacheViewAuthenticPixels(implode_view,exception) == MagickFalse)
3302 status=MagickFalse;
3303 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3304 {
3305 MagickBooleanType
3306 proceed;
3307
cristyb5d5f722009-11-04 03:03:49 +00003308#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003309 #pragma omp critical (MagickCore_ImplodeImage)
cristy3ed852e2009-09-05 21:47:34 +00003310#endif
3311 proceed=SetImageProgress(image,ImplodeImageTag,progress++,image->rows);
3312 if (proceed == MagickFalse)
3313 status=MagickFalse;
3314 }
3315 }
3316 implode_view=DestroyCacheView(implode_view);
3317 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003318 if (status == MagickFalse)
3319 implode_image=DestroyImage(implode_image);
3320 return(implode_image);
3321}
3322
3323/*
3324%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3325% %
3326% %
3327% %
3328% M o r p h I m a g e s %
3329% %
3330% %
3331% %
3332%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3333%
3334% The MorphImages() method requires a minimum of two images. The first
3335% image is transformed into the second by a number of intervening images
3336% as specified by frames.
3337%
3338% The format of the MorphImage method is:
3339%
cristybb503372010-05-27 20:51:26 +00003340% Image *MorphImages(const Image *image,const size_t number_frames,
cristy3ed852e2009-09-05 21:47:34 +00003341% ExceptionInfo *exception)
3342%
3343% A description of each parameter follows:
3344%
3345% o image: the image.
3346%
3347% o number_frames: Define the number of in-between image to generate.
3348% The more in-between frames, the smoother the morph.
3349%
3350% o exception: return any errors or warnings in this structure.
3351%
3352*/
3353MagickExport Image *MorphImages(const Image *image,
cristybb503372010-05-27 20:51:26 +00003354 const size_t number_frames,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003355{
3356#define MorphImageTag "Morph/Image"
3357
3358 Image
3359 *morph_image,
3360 *morph_images;
3361
cristy9d314ff2011-03-09 01:30:28 +00003362 MagickBooleanType
3363 status;
cristy3ed852e2009-09-05 21:47:34 +00003364
3365 MagickOffsetType
3366 scene;
3367
3368 MagickRealType
3369 alpha,
3370 beta;
3371
3372 register const Image
3373 *next;
3374
cristybb503372010-05-27 20:51:26 +00003375 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003376 i;
3377
cristy9d314ff2011-03-09 01:30:28 +00003378 ssize_t
3379 y;
cristy3ed852e2009-09-05 21:47:34 +00003380
3381 /*
3382 Clone first frame in sequence.
3383 */
3384 assert(image != (Image *) NULL);
3385 assert(image->signature == MagickSignature);
3386 if (image->debug != MagickFalse)
3387 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3388 assert(exception != (ExceptionInfo *) NULL);
3389 assert(exception->signature == MagickSignature);
3390 morph_images=CloneImage(image,0,0,MagickTrue,exception);
3391 if (morph_images == (Image *) NULL)
3392 return((Image *) NULL);
3393 if (GetNextImageInList(image) == (Image *) NULL)
3394 {
3395 /*
3396 Morph single image.
3397 */
cristybb503372010-05-27 20:51:26 +00003398 for (i=1; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003399 {
3400 morph_image=CloneImage(image,0,0,MagickTrue,exception);
3401 if (morph_image == (Image *) NULL)
3402 {
3403 morph_images=DestroyImageList(morph_images);
3404 return((Image *) NULL);
3405 }
3406 AppendImageToList(&morph_images,morph_image);
cristy8b27a6d2010-02-14 03:31:15 +00003407 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003408 {
cristy8b27a6d2010-02-14 03:31:15 +00003409 MagickBooleanType
3410 proceed;
3411
cristybb503372010-05-27 20:51:26 +00003412 proceed=SetImageProgress(image,MorphImageTag,(MagickOffsetType) i,
3413 number_frames);
cristy8b27a6d2010-02-14 03:31:15 +00003414 if (proceed == MagickFalse)
3415 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00003416 }
3417 }
3418 return(GetFirstImageInList(morph_images));
3419 }
3420 /*
3421 Morph image sequence.
3422 */
3423 status=MagickTrue;
3424 scene=0;
3425 next=image;
3426 for ( ; GetNextImageInList(next) != (Image *) NULL; next=GetNextImageInList(next))
3427 {
cristybb503372010-05-27 20:51:26 +00003428 for (i=0; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003429 {
3430 CacheView
3431 *image_view,
3432 *morph_view;
3433
3434 beta=(MagickRealType) (i+1.0)/(MagickRealType) (number_frames+1.0);
3435 alpha=1.0-beta;
cristy15b98cd2010-09-12 19:42:50 +00003436 morph_image=ResizeImage(next,(size_t) (alpha*next->columns+beta*
cristyaa2c16c2012-03-25 22:21:35 +00003437 GetNextImageInList(next)->columns+0.5),(size_t) (alpha*next->rows+beta*
3438 GetNextImageInList(next)->rows+0.5),next->filter,exception);
cristy3ed852e2009-09-05 21:47:34 +00003439 if (morph_image == (Image *) NULL)
3440 {
3441 morph_images=DestroyImageList(morph_images);
3442 return((Image *) NULL);
3443 }
cristy1707c6c2012-01-18 23:30:54 +00003444 status=SetImageStorageClass(morph_image,DirectClass,exception);
3445 if (status == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003446 {
cristy3ed852e2009-09-05 21:47:34 +00003447 morph_image=DestroyImage(morph_image);
3448 return((Image *) NULL);
3449 }
3450 AppendImageToList(&morph_images,morph_image);
3451 morph_images=GetLastImageInList(morph_images);
cristy15b98cd2010-09-12 19:42:50 +00003452 morph_image=ResizeImage(GetNextImageInList(next),morph_images->columns,
cristyaa2c16c2012-03-25 22:21:35 +00003453 morph_images->rows,GetNextImageInList(next)->filter,exception);
cristy3ed852e2009-09-05 21:47:34 +00003454 if (morph_image == (Image *) NULL)
3455 {
3456 morph_images=DestroyImageList(morph_images);
3457 return((Image *) NULL);
3458 }
3459 image_view=AcquireCacheView(morph_image);
3460 morph_view=AcquireCacheView(morph_images);
cristyb5d5f722009-11-04 03:03:49 +00003461#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003462 #pragma omp parallel for schedule(static,4) shared(status)
cristy3ed852e2009-09-05 21:47:34 +00003463#endif
cristybb503372010-05-27 20:51:26 +00003464 for (y=0; y < (ssize_t) morph_images->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003465 {
3466 MagickBooleanType
3467 sync;
3468
cristy4c08aed2011-07-01 19:47:50 +00003469 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003470 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003471
cristybb503372010-05-27 20:51:26 +00003472 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003473 x;
3474
cristy4c08aed2011-07-01 19:47:50 +00003475 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003476 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003477
3478 if (status == MagickFalse)
3479 continue;
3480 p=GetCacheViewVirtualPixels(image_view,0,y,morph_image->columns,1,
3481 exception);
3482 q=GetCacheViewAuthenticPixels(morph_view,0,y,morph_images->columns,1,
3483 exception);
cristy4c08aed2011-07-01 19:47:50 +00003484 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003485 {
3486 status=MagickFalse;
3487 continue;
3488 }
cristybb503372010-05-27 20:51:26 +00003489 for (x=0; x < (ssize_t) morph_images->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003490 {
cristy1707c6c2012-01-18 23:30:54 +00003491 register ssize_t
3492 i;
3493
cristy10a6c612012-01-29 21:41:05 +00003494 for (i=0; i < (ssize_t) GetPixelChannels(morph_image); i++)
cristy1707c6c2012-01-18 23:30:54 +00003495 {
3496 PixelChannel
3497 channel;
3498
3499 PixelTrait
3500 morph_traits,
3501 traits;
3502
3503 channel=GetPixelChannelMapChannel(image,i);
3504 traits=GetPixelChannelMapTraits(image,channel);
3505 morph_traits=GetPixelChannelMapTraits(morph_image,channel);
3506 if ((traits == UndefinedPixelTrait) ||
3507 (morph_traits == UndefinedPixelTrait))
3508 continue;
cristyd09f8802012-02-04 16:44:10 +00003509 if (((morph_traits & CopyPixelTrait) != 0) ||
3510 (GetPixelMask(image,p) != 0))
cristy1707c6c2012-01-18 23:30:54 +00003511 {
3512 SetPixelChannel(morph_image,channel,p[i],q);
3513 continue;
3514 }
3515 SetPixelChannel(morph_image,channel,ClampToQuantum(alpha*
3516 GetPixelChannel(morph_images,channel,q)+beta*p[i]),q);
3517 }
cristyed231572011-07-14 02:18:59 +00003518 p+=GetPixelChannels(morph_image);
3519 q+=GetPixelChannels(morph_images);
cristy3ed852e2009-09-05 21:47:34 +00003520 }
3521 sync=SyncCacheViewAuthenticPixels(morph_view,exception);
3522 if (sync == MagickFalse)
3523 status=MagickFalse;
3524 }
3525 morph_view=DestroyCacheView(morph_view);
3526 image_view=DestroyCacheView(image_view);
3527 morph_image=DestroyImage(morph_image);
3528 }
cristybb503372010-05-27 20:51:26 +00003529 if (i < (ssize_t) number_frames)
cristy3ed852e2009-09-05 21:47:34 +00003530 break;
3531 /*
3532 Clone last frame in sequence.
3533 */
3534 morph_image=CloneImage(GetNextImageInList(next),0,0,MagickTrue,exception);
3535 if (morph_image == (Image *) NULL)
3536 {
3537 morph_images=DestroyImageList(morph_images);
3538 return((Image *) NULL);
3539 }
3540 AppendImageToList(&morph_images,morph_image);
3541 morph_images=GetLastImageInList(morph_images);
3542 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3543 {
3544 MagickBooleanType
3545 proceed;
3546
cristyb5d5f722009-11-04 03:03:49 +00003547#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003548 #pragma omp critical (MagickCore_MorphImages)
cristy3ed852e2009-09-05 21:47:34 +00003549#endif
3550 proceed=SetImageProgress(image,MorphImageTag,scene,
3551 GetImageListLength(image));
3552 if (proceed == MagickFalse)
3553 status=MagickFalse;
3554 }
3555 scene++;
3556 }
3557 if (GetNextImageInList(next) != (Image *) NULL)
3558 {
3559 morph_images=DestroyImageList(morph_images);
3560 return((Image *) NULL);
3561 }
3562 return(GetFirstImageInList(morph_images));
3563}
3564
3565/*
3566%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3567% %
3568% %
3569% %
3570% P l a s m a I m a g e %
3571% %
3572% %
3573% %
3574%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3575%
3576% PlasmaImage() initializes an image with plasma fractal values. The image
3577% must be initialized with a base color and the random number generator
3578% seeded before this method is called.
3579%
3580% The format of the PlasmaImage method is:
3581%
3582% MagickBooleanType PlasmaImage(Image *image,const SegmentInfo *segment,
cristy5cbc0162011-08-29 00:36:28 +00003583% size_t attenuate,size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003584%
3585% A description of each parameter follows:
3586%
3587% o image: the image.
3588%
3589% o segment: Define the region to apply plasma fractals values.
3590%
glennrp7dae1ca2010-09-16 12:17:35 +00003591% o attenuate: Define the plasma attenuation factor.
cristy3ed852e2009-09-05 21:47:34 +00003592%
3593% o depth: Limit the plasma recursion depth.
3594%
cristy5cbc0162011-08-29 00:36:28 +00003595% o exception: return any errors or warnings in this structure.
3596%
cristy3ed852e2009-09-05 21:47:34 +00003597*/
3598
3599static inline Quantum PlasmaPixel(RandomInfo *random_info,
3600 const MagickRealType pixel,const MagickRealType noise)
3601{
3602 Quantum
3603 plasma;
3604
cristyce70c172010-01-07 17:15:30 +00003605 plasma=ClampToQuantum(pixel+noise*GetPseudoRandomValue(random_info)-
cristy3ed852e2009-09-05 21:47:34 +00003606 noise/2.0);
3607 return(plasma);
3608}
3609
cristyda1f9c12011-10-02 21:39:49 +00003610static MagickBooleanType PlasmaImageProxy(Image *image,CacheView *image_view,
3611 CacheView *u_view,CacheView *v_view,RandomInfo *random_info,
3612 const SegmentInfo *segment,size_t attenuate,size_t depth,
3613 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003614{
cristy3ed852e2009-09-05 21:47:34 +00003615 MagickRealType
3616 plasma;
3617
cristyda1f9c12011-10-02 21:39:49 +00003618 PixelChannel
3619 channel;
3620
3621 PixelTrait
3622 traits;
3623
3624 register const Quantum
3625 *restrict u,
3626 *restrict v;
3627
3628 register Quantum
3629 *restrict q;
3630
3631 register ssize_t
3632 i;
cristy3ed852e2009-09-05 21:47:34 +00003633
cristy9d314ff2011-03-09 01:30:28 +00003634 ssize_t
3635 x,
3636 x_mid,
3637 y,
3638 y_mid;
3639
cristy3ed852e2009-09-05 21:47:34 +00003640 if (((segment->x2-segment->x1) == 0.0) && ((segment->y2-segment->y1) == 0.0))
3641 return(MagickTrue);
3642 if (depth != 0)
3643 {
3644 SegmentInfo
3645 local_info;
3646
3647 /*
3648 Divide the area into quadrants and recurse.
3649 */
3650 depth--;
3651 attenuate++;
cristybb503372010-05-27 20:51:26 +00003652 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3653 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003654 local_info=(*segment);
3655 local_info.x2=(double) x_mid;
3656 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003657 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3658 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003659 local_info=(*segment);
3660 local_info.y1=(double) y_mid;
3661 local_info.x2=(double) x_mid;
cristyda1f9c12011-10-02 21:39:49 +00003662 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3663 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003664 local_info=(*segment);
3665 local_info.x1=(double) x_mid;
3666 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003667 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3668 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003669 local_info=(*segment);
3670 local_info.x1=(double) x_mid;
3671 local_info.y1=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003672 return(PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3673 &local_info,attenuate,depth,exception));
cristy3ed852e2009-09-05 21:47:34 +00003674 }
cristybb503372010-05-27 20:51:26 +00003675 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3676 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003677 if ((segment->x1 == (double) x_mid) && (segment->x2 == (double) x_mid) &&
3678 (segment->y1 == (double) y_mid) && (segment->y2 == (double) y_mid))
3679 return(MagickFalse);
3680 /*
3681 Average pixels and apply plasma.
3682 */
cristy3ed852e2009-09-05 21:47:34 +00003683 plasma=(MagickRealType) QuantumRange/(2.0*attenuate);
3684 if ((segment->x1 != (double) x_mid) || (segment->x2 != (double) x_mid))
3685 {
cristy3ed852e2009-09-05 21:47:34 +00003686 /*
3687 Left pixel.
3688 */
cristybb503372010-05-27 20:51:26 +00003689 x=(ssize_t) ceil(segment->x1-0.5);
cristy1707c6c2012-01-18 23:30:54 +00003690 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),1,1,
3691 exception);
3692 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),1,1,
3693 exception);
cristyc5c6f662010-09-22 14:23:02 +00003694 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003695 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3696 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003697 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003698 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3699 {
cristye2a912b2011-12-05 20:02:07 +00003700 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003701 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003702 if (traits == UndefinedPixelTrait)
3703 continue;
3704 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3705 }
cristyc5c6f662010-09-22 14:23:02 +00003706 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003707 if (segment->x1 != segment->x2)
3708 {
3709 /*
3710 Right pixel.
3711 */
cristybb503372010-05-27 20:51:26 +00003712 x=(ssize_t) ceil(segment->x2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003713 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3714 1,1,exception);
3715 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3716 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003717 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003718 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3719 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003720 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003721 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3722 {
cristye2a912b2011-12-05 20:02:07 +00003723 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003724 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003725 if (traits == UndefinedPixelTrait)
3726 continue;
3727 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3728 }
cristyc5c6f662010-09-22 14:23:02 +00003729 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003730 }
3731 }
3732 if ((segment->y1 != (double) y_mid) || (segment->y2 != (double) y_mid))
3733 {
3734 if ((segment->x1 != (double) x_mid) || (segment->y2 != (double) y_mid))
3735 {
cristy3ed852e2009-09-05 21:47:34 +00003736 /*
3737 Bottom pixel.
3738 */
cristybb503372010-05-27 20:51:26 +00003739 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003740 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3741 1,1,exception);
3742 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3743 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003744 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003745 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3746 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003747 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003748 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3749 {
cristye2a912b2011-12-05 20:02:07 +00003750 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003751 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003752 if (traits == UndefinedPixelTrait)
3753 continue;
3754 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3755 }
cristyc5c6f662010-09-22 14:23:02 +00003756 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003757 }
3758 if (segment->y1 != segment->y2)
3759 {
cristy3ed852e2009-09-05 21:47:34 +00003760 /*
3761 Top pixel.
3762 */
cristybb503372010-05-27 20:51:26 +00003763 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003764 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3765 1,1,exception);
3766 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3767 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003768 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003769 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3770 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003771 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003772 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3773 {
cristye2a912b2011-12-05 20:02:07 +00003774 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003775 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003776 if (traits == UndefinedPixelTrait)
3777 continue;
3778 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3779 }
cristyc5c6f662010-09-22 14:23:02 +00003780 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003781 }
3782 }
3783 if ((segment->x1 != segment->x2) || (segment->y1 != segment->y2))
3784 {
cristy3ed852e2009-09-05 21:47:34 +00003785 /*
3786 Middle pixel.
3787 */
cristybb503372010-05-27 20:51:26 +00003788 x=(ssize_t) ceil(segment->x1-0.5);
3789 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003790 u=GetCacheViewVirtualPixels(u_view,x,y,1,1,exception);
cristybb503372010-05-27 20:51:26 +00003791 x=(ssize_t) ceil(segment->x2-0.5);
3792 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003793 v=GetCacheViewVirtualPixels(v_view,x,y,1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003794 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003795 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3796 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003797 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003798 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3799 {
cristye2a912b2011-12-05 20:02:07 +00003800 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003801 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003802 if (traits == UndefinedPixelTrait)
3803 continue;
3804 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3805 }
cristyc5c6f662010-09-22 14:23:02 +00003806 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003807 }
3808 if (((segment->x2-segment->x1) < 3.0) && ((segment->y2-segment->y1) < 3.0))
3809 return(MagickTrue);
3810 return(MagickFalse);
3811}
cristyda1f9c12011-10-02 21:39:49 +00003812
cristy3ed852e2009-09-05 21:47:34 +00003813MagickExport MagickBooleanType PlasmaImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00003814 const SegmentInfo *segment,size_t attenuate,size_t depth,
3815 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003816{
cristyc5c6f662010-09-22 14:23:02 +00003817 CacheView
cristyda1f9c12011-10-02 21:39:49 +00003818 *image_view,
3819 *u_view,
3820 *v_view;
cristyc5c6f662010-09-22 14:23:02 +00003821
cristy3ed852e2009-09-05 21:47:34 +00003822 MagickBooleanType
3823 status;
3824
3825 RandomInfo
3826 *random_info;
3827
3828 if (image->debug != MagickFalse)
3829 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3830 assert(image != (Image *) NULL);
3831 assert(image->signature == MagickSignature);
3832 if (image->debug != MagickFalse)
3833 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
cristy5cbc0162011-08-29 00:36:28 +00003834 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristyc5c6f662010-09-22 14:23:02 +00003835 return(MagickFalse);
3836 image_view=AcquireCacheView(image);
cristyda1f9c12011-10-02 21:39:49 +00003837 u_view=AcquireCacheView(image);
3838 v_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00003839 random_info=AcquireRandomInfo();
cristyda1f9c12011-10-02 21:39:49 +00003840 status=PlasmaImageProxy(image,image_view,u_view,v_view,random_info,segment,
3841 attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003842 random_info=DestroyRandomInfo(random_info);
cristyda1f9c12011-10-02 21:39:49 +00003843 v_view=DestroyCacheView(v_view);
3844 u_view=DestroyCacheView(u_view);
cristyc5c6f662010-09-22 14:23:02 +00003845 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003846 return(status);
3847}
3848
3849/*
3850%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3851% %
3852% %
3853% %
3854% P o l a r o i d I m a g e %
3855% %
3856% %
3857% %
3858%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3859%
3860% PolaroidImage() simulates a Polaroid picture.
3861%
3862% The format of the AnnotateImage method is:
3863%
3864% Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristye9e3d382011-12-14 01:50:13 +00003865% const char *caption,const double angle,
3866% const PixelInterpolateMethod method,ExceptionInfo exception)
cristy3ed852e2009-09-05 21:47:34 +00003867%
3868% A description of each parameter follows:
3869%
3870% o image: the image.
3871%
3872% o draw_info: the draw info.
3873%
cristye9e3d382011-12-14 01:50:13 +00003874% o caption: the Polaroid caption.
3875%
cristycee97112010-05-28 00:44:52 +00003876% o angle: Apply the effect along this angle.
cristy3ed852e2009-09-05 21:47:34 +00003877%
cristy5c4e2582011-09-11 19:21:03 +00003878% o method: the pixel interpolation method.
3879%
cristy3ed852e2009-09-05 21:47:34 +00003880% o exception: return any errors or warnings in this structure.
3881%
3882*/
3883MagickExport Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristye9e3d382011-12-14 01:50:13 +00003884 const char *caption,const double angle,const PixelInterpolateMethod method,
cristy5c4e2582011-09-11 19:21:03 +00003885 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003886{
cristy3ed852e2009-09-05 21:47:34 +00003887 Image
3888 *bend_image,
3889 *caption_image,
3890 *flop_image,
3891 *picture_image,
3892 *polaroid_image,
3893 *rotate_image,
3894 *trim_image;
3895
cristybb503372010-05-27 20:51:26 +00003896 size_t
cristy3ed852e2009-09-05 21:47:34 +00003897 height;
3898
cristy9d314ff2011-03-09 01:30:28 +00003899 ssize_t
3900 quantum;
3901
cristy3ed852e2009-09-05 21:47:34 +00003902 /*
3903 Simulate a Polaroid picture.
3904 */
3905 assert(image != (Image *) NULL);
3906 assert(image->signature == MagickSignature);
3907 if (image->debug != MagickFalse)
3908 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3909 assert(exception != (ExceptionInfo *) NULL);
3910 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00003911 quantum=(ssize_t) MagickMax(MagickMax((double) image->columns,(double)
cristy3ed852e2009-09-05 21:47:34 +00003912 image->rows)/25.0,10.0);
3913 height=image->rows+2*quantum;
3914 caption_image=(Image *) NULL;
cristye9e3d382011-12-14 01:50:13 +00003915 if (caption != (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003916 {
3917 char
cristye9e3d382011-12-14 01:50:13 +00003918 geometry[MaxTextExtent],
3919 *text;
cristy3ed852e2009-09-05 21:47:34 +00003920
3921 DrawInfo
3922 *annotate_info;
3923
cristy3ed852e2009-09-05 21:47:34 +00003924 MagickBooleanType
3925 status;
3926
cristy9d314ff2011-03-09 01:30:28 +00003927 ssize_t
3928 count;
3929
cristy3ed852e2009-09-05 21:47:34 +00003930 TypeMetric
3931 metrics;
3932
3933 /*
3934 Generate caption image.
3935 */
3936 caption_image=CloneImage(image,image->columns,1,MagickTrue,exception);
3937 if (caption_image == (Image *) NULL)
3938 return((Image *) NULL);
3939 annotate_info=CloneDrawInfo((const ImageInfo *) NULL,draw_info);
cristye9e3d382011-12-14 01:50:13 +00003940 text=InterpretImageProperties((ImageInfo *) NULL,(Image *) image,caption,
3941 exception);
3942 (void) CloneString(&annotate_info->text,text);
cristy6b1d05e2010-09-22 19:17:27 +00003943 count=FormatMagickCaption(caption_image,annotate_info,MagickTrue,&metrics,
cristye9e3d382011-12-14 01:50:13 +00003944 &text,exception);
3945 status=SetImageExtent(caption_image,image->columns,(size_t) ((count+1)*
3946 (metrics.ascent-metrics.descent)+0.5),exception);
cristy3ed852e2009-09-05 21:47:34 +00003947 if (status == MagickFalse)
3948 caption_image=DestroyImage(caption_image);
3949 else
3950 {
3951 caption_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00003952 (void) SetImageBackgroundColor(caption_image,exception);
cristye9e3d382011-12-14 01:50:13 +00003953 (void) CloneString(&annotate_info->text,text);
cristyb51dff52011-05-19 16:55:47 +00003954 (void) FormatLocaleString(geometry,MaxTextExtent,"+0+%g",
cristy3ed852e2009-09-05 21:47:34 +00003955 metrics.ascent);
3956 if (annotate_info->gravity == UndefinedGravity)
3957 (void) CloneString(&annotate_info->geometry,AcquireString(
3958 geometry));
cristy5cbc0162011-08-29 00:36:28 +00003959 (void) AnnotateImage(caption_image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00003960 height+=caption_image->rows;
3961 }
3962 annotate_info=DestroyDrawInfo(annotate_info);
cristye9e3d382011-12-14 01:50:13 +00003963 text=DestroyString(text);
cristy3ed852e2009-09-05 21:47:34 +00003964 }
3965 picture_image=CloneImage(image,image->columns+2*quantum,height,MagickTrue,
3966 exception);
3967 if (picture_image == (Image *) NULL)
3968 {
3969 if (caption_image != (Image *) NULL)
3970 caption_image=DestroyImage(caption_image);
3971 return((Image *) NULL);
3972 }
3973 picture_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00003974 (void) SetImageBackgroundColor(picture_image,exception);
cristy39172402012-03-30 13:04:39 +00003975 (void) CompositeImage(picture_image,image,OverCompositeOp,MagickTrue,quantum,
cristyfeb3e962012-03-29 17:25:55 +00003976 quantum,exception);
cristy3ed852e2009-09-05 21:47:34 +00003977 if (caption_image != (Image *) NULL)
3978 {
cristyfeb3e962012-03-29 17:25:55 +00003979 (void) CompositeImage(picture_image,caption_image,OverCompositeOp,
cristy39172402012-03-30 13:04:39 +00003980 MagickTrue,quantum,(ssize_t) (image->rows+3*quantum/2),exception);
cristy3ed852e2009-09-05 21:47:34 +00003981 caption_image=DestroyImage(caption_image);
3982 }
cristy9950d572011-10-01 18:22:35 +00003983 (void) QueryColorCompliance("none",AllCompliance,
3984 &picture_image->background_color,exception);
cristy63240882011-08-05 19:05:27 +00003985 (void) SetImageAlphaChannel(picture_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00003986 rotate_image=RotateImage(picture_image,90.0,exception);
3987 picture_image=DestroyImage(picture_image);
3988 if (rotate_image == (Image *) NULL)
3989 return((Image *) NULL);
3990 picture_image=rotate_image;
3991 bend_image=WaveImage(picture_image,0.01*picture_image->rows,2.0*
cristy5c4e2582011-09-11 19:21:03 +00003992 picture_image->columns,method,exception);
cristy3ed852e2009-09-05 21:47:34 +00003993 picture_image=DestroyImage(picture_image);
3994 if (bend_image == (Image *) NULL)
3995 return((Image *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00003996 picture_image=bend_image;
3997 rotate_image=RotateImage(picture_image,-90.0,exception);
3998 picture_image=DestroyImage(picture_image);
3999 if (rotate_image == (Image *) NULL)
4000 return((Image *) NULL);
4001 picture_image=rotate_image;
4002 picture_image->background_color=image->background_color;
anthonyf46d4262012-03-26 03:30:34 +00004003 polaroid_image=ShadowImage(picture_image,80.0,2.0,quantum/3,quantum/3,
cristy3ed852e2009-09-05 21:47:34 +00004004 exception);
4005 if (polaroid_image == (Image *) NULL)
4006 {
4007 picture_image=DestroyImage(picture_image);
4008 return(picture_image);
4009 }
4010 flop_image=FlopImage(polaroid_image,exception);
4011 polaroid_image=DestroyImage(polaroid_image);
4012 if (flop_image == (Image *) NULL)
4013 {
4014 picture_image=DestroyImage(picture_image);
4015 return(picture_image);
4016 }
4017 polaroid_image=flop_image;
cristyfeb3e962012-03-29 17:25:55 +00004018 (void) CompositeImage(polaroid_image,picture_image,OverCompositeOp,
cristy39172402012-03-30 13:04:39 +00004019 MagickTrue,(ssize_t) (-0.01*picture_image->columns/2.0),0L,exception);
cristy3ed852e2009-09-05 21:47:34 +00004020 picture_image=DestroyImage(picture_image);
cristy9950d572011-10-01 18:22:35 +00004021 (void) QueryColorCompliance("none",AllCompliance,
4022 &polaroid_image->background_color,exception);
cristy3ed852e2009-09-05 21:47:34 +00004023 rotate_image=RotateImage(polaroid_image,angle,exception);
4024 polaroid_image=DestroyImage(polaroid_image);
4025 if (rotate_image == (Image *) NULL)
4026 return((Image *) NULL);
4027 polaroid_image=rotate_image;
4028 trim_image=TrimImage(polaroid_image,exception);
4029 polaroid_image=DestroyImage(polaroid_image);
4030 if (trim_image == (Image *) NULL)
4031 return((Image *) NULL);
4032 polaroid_image=trim_image;
4033 return(polaroid_image);
4034}
4035
4036/*
4037%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4038% %
4039% %
4040% %
cristy3ed852e2009-09-05 21:47:34 +00004041% S e p i a T o n e I m a g e %
4042% %
4043% %
4044% %
4045%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4046%
4047% MagickSepiaToneImage() applies a special effect to the image, similar to the
4048% effect achieved in a photo darkroom by sepia toning. Threshold ranges from
4049% 0 to QuantumRange and is a measure of the extent of the sepia toning. A
4050% threshold of 80% is a good starting point for a reasonable tone.
4051%
4052% The format of the SepiaToneImage method is:
4053%
4054% Image *SepiaToneImage(const Image *image,const double threshold,
4055% ExceptionInfo *exception)
4056%
4057% A description of each parameter follows:
4058%
4059% o image: the image.
4060%
4061% o threshold: the tone threshold.
4062%
4063% o exception: return any errors or warnings in this structure.
4064%
4065*/
4066MagickExport Image *SepiaToneImage(const Image *image,const double threshold,
4067 ExceptionInfo *exception)
4068{
4069#define SepiaToneImageTag "SepiaTone/Image"
4070
cristyc4c8d132010-01-07 01:58:38 +00004071 CacheView
4072 *image_view,
4073 *sepia_view;
4074
cristy3ed852e2009-09-05 21:47:34 +00004075 Image
4076 *sepia_image;
4077
cristy3ed852e2009-09-05 21:47:34 +00004078 MagickBooleanType
4079 status;
4080
cristybb503372010-05-27 20:51:26 +00004081 MagickOffsetType
4082 progress;
4083
4084 ssize_t
4085 y;
4086
cristy3ed852e2009-09-05 21:47:34 +00004087 /*
4088 Initialize sepia-toned image attributes.
4089 */
4090 assert(image != (const Image *) NULL);
4091 assert(image->signature == MagickSignature);
4092 if (image->debug != MagickFalse)
4093 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4094 assert(exception != (ExceptionInfo *) NULL);
4095 assert(exception->signature == MagickSignature);
cristy1707c6c2012-01-18 23:30:54 +00004096 sepia_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004097 if (sepia_image == (Image *) NULL)
4098 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004099 if (SetImageStorageClass(sepia_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004100 {
cristy3ed852e2009-09-05 21:47:34 +00004101 sepia_image=DestroyImage(sepia_image);
4102 return((Image *) NULL);
4103 }
4104 /*
4105 Tone each row of the image.
4106 */
4107 status=MagickTrue;
4108 progress=0;
4109 image_view=AcquireCacheView(image);
4110 sepia_view=AcquireCacheView(sepia_image);
cristyb5d5f722009-11-04 03:03:49 +00004111#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00004112 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00004113#endif
cristybb503372010-05-27 20:51:26 +00004114 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004115 {
cristy4c08aed2011-07-01 19:47:50 +00004116 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004117 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00004118
cristybb503372010-05-27 20:51:26 +00004119 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004120 x;
4121
cristy4c08aed2011-07-01 19:47:50 +00004122 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004123 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004124
4125 if (status == MagickFalse)
4126 continue;
4127 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy1707c6c2012-01-18 23:30:54 +00004128 q=GetCacheViewAuthenticPixels(sepia_view,0,y,sepia_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00004129 exception);
cristy4c08aed2011-07-01 19:47:50 +00004130 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004131 {
4132 status=MagickFalse;
4133 continue;
4134 }
cristybb503372010-05-27 20:51:26 +00004135 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004136 {
4137 MagickRealType
4138 intensity,
4139 tone;
4140
cristy4c08aed2011-07-01 19:47:50 +00004141 intensity=(MagickRealType) GetPixelIntensity(image,p);
cristy3ed852e2009-09-05 21:47:34 +00004142 tone=intensity > threshold ? (MagickRealType) QuantumRange : intensity+
4143 (MagickRealType) QuantumRange-threshold;
cristy4c08aed2011-07-01 19:47:50 +00004144 SetPixelRed(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004145 tone=intensity > (7.0*threshold/6.0) ? (MagickRealType) QuantumRange :
4146 intensity+(MagickRealType) QuantumRange-7.0*threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004147 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004148 tone=intensity < (threshold/6.0) ? 0 : intensity-threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004149 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004150 tone=threshold/7.0;
cristy4c08aed2011-07-01 19:47:50 +00004151 if ((MagickRealType) GetPixelGreen(image,q) < tone)
4152 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
4153 if ((MagickRealType) GetPixelBlue(image,q) < tone)
4154 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristyed231572011-07-14 02:18:59 +00004155 p+=GetPixelChannels(image);
4156 q+=GetPixelChannels(sepia_image);
cristy3ed852e2009-09-05 21:47:34 +00004157 }
4158 if (SyncCacheViewAuthenticPixels(sepia_view,exception) == MagickFalse)
4159 status=MagickFalse;
4160 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4161 {
4162 MagickBooleanType
4163 proceed;
4164
cristyb5d5f722009-11-04 03:03:49 +00004165#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00004166 #pragma omp critical (MagickCore_SepiaToneImage)
cristy3ed852e2009-09-05 21:47:34 +00004167#endif
4168 proceed=SetImageProgress(image,SepiaToneImageTag,progress++,
4169 image->rows);
4170 if (proceed == MagickFalse)
4171 status=MagickFalse;
4172 }
4173 }
4174 sepia_view=DestroyCacheView(sepia_view);
4175 image_view=DestroyCacheView(image_view);
cristye23ec9d2011-08-16 18:15:40 +00004176 (void) NormalizeImage(sepia_image,exception);
4177 (void) ContrastImage(sepia_image,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004178 if (status == MagickFalse)
4179 sepia_image=DestroyImage(sepia_image);
4180 return(sepia_image);
4181}
4182
4183/*
4184%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4185% %
4186% %
4187% %
4188% S h a d o w I m a g e %
4189% %
4190% %
4191% %
4192%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4193%
4194% ShadowImage() simulates a shadow from the specified image and returns it.
4195%
4196% The format of the ShadowImage method is:
4197%
cristy70cddf72011-12-10 22:42:42 +00004198% Image *ShadowImage(const Image *image,const double alpha,
cristyaa2c16c2012-03-25 22:21:35 +00004199% const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4200% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004201%
4202% A description of each parameter follows:
4203%
4204% o image: the image.
4205%
cristy70cddf72011-12-10 22:42:42 +00004206% o alpha: percentage transparency.
cristy3ed852e2009-09-05 21:47:34 +00004207%
4208% o sigma: the standard deviation of the Gaussian, in pixels.
4209%
4210% o x_offset: the shadow x-offset.
4211%
4212% o y_offset: the shadow y-offset.
4213%
4214% o exception: return any errors or warnings in this structure.
4215%
4216*/
cristy70cddf72011-12-10 22:42:42 +00004217MagickExport Image *ShadowImage(const Image *image,const double alpha,
cristyaa2c16c2012-03-25 22:21:35 +00004218 const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4219 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004220{
4221#define ShadowImageTag "Shadow/Image"
4222
cristy70cddf72011-12-10 22:42:42 +00004223 CacheView
4224 *image_view;
4225
cristybd5a96c2011-08-21 00:04:26 +00004226 ChannelType
4227 channel_mask;
4228
cristy3ed852e2009-09-05 21:47:34 +00004229 Image
4230 *border_image,
4231 *clone_image,
4232 *shadow_image;
4233
cristy70cddf72011-12-10 22:42:42 +00004234 MagickBooleanType
4235 status;
4236
cristy3ed852e2009-09-05 21:47:34 +00004237 RectangleInfo
4238 border_info;
4239
cristy70cddf72011-12-10 22:42:42 +00004240 ssize_t
4241 y;
4242
cristy3ed852e2009-09-05 21:47:34 +00004243 assert(image != (Image *) NULL);
4244 assert(image->signature == MagickSignature);
4245 if (image->debug != MagickFalse)
4246 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4247 assert(exception != (ExceptionInfo *) NULL);
4248 assert(exception->signature == MagickSignature);
4249 clone_image=CloneImage(image,0,0,MagickTrue,exception);
4250 if (clone_image == (Image *) NULL)
4251 return((Image *) NULL);
cristy387430f2012-02-07 13:09:46 +00004252 (void) SetImageVirtualPixelMethod(clone_image,EdgeVirtualPixelMethod,
4253 exception);
cristybb503372010-05-27 20:51:26 +00004254 border_info.width=(size_t) floor(2.0*sigma+0.5);
4255 border_info.height=(size_t) floor(2.0*sigma+0.5);
cristy3ed852e2009-09-05 21:47:34 +00004256 border_info.x=0;
4257 border_info.y=0;
cristy9950d572011-10-01 18:22:35 +00004258 (void) QueryColorCompliance("none",AllCompliance,&clone_image->border_color,
4259 exception);
cristy70cddf72011-12-10 22:42:42 +00004260 clone_image->matte=MagickTrue;
4261 border_image=BorderImage(clone_image,&border_info,OverCompositeOp,exception);
cristy3ed852e2009-09-05 21:47:34 +00004262 clone_image=DestroyImage(clone_image);
4263 if (border_image == (Image *) NULL)
4264 return((Image *) NULL);
4265 if (border_image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00004266 (void) SetImageAlphaChannel(border_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004267 /*
4268 Shadow image.
4269 */
cristy70cddf72011-12-10 22:42:42 +00004270 status=MagickTrue;
4271 image_view=AcquireCacheView(border_image);
4272 for (y=0; y < (ssize_t) border_image->rows; y++)
4273 {
4274 PixelInfo
4275 background_color;
4276
4277 register Quantum
4278 *restrict q;
4279
4280 register ssize_t
4281 x;
4282
4283 if (status == MagickFalse)
4284 continue;
4285 q=QueueCacheViewAuthenticPixels(image_view,0,y,border_image->columns,1,
4286 exception);
4287 if (q == (Quantum *) NULL)
4288 {
4289 status=MagickFalse;
4290 continue;
4291 }
4292 background_color=border_image->background_color;
4293 background_color.matte=MagickTrue;
4294 for (x=0; x < (ssize_t) border_image->columns; x++)
4295 {
4296 if (border_image->matte != MagickFalse)
4297 background_color.alpha=GetPixelAlpha(border_image,q)*alpha/100.0;
4298 SetPixelInfoPixel(border_image,&background_color,q);
4299 q+=GetPixelChannels(border_image);
4300 }
4301 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4302 status=MagickFalse;
4303 }
4304 image_view=DestroyCacheView(image_view);
4305 if (status == MagickFalse)
4306 {
4307 border_image=DestroyImage(border_image);
4308 return((Image *) NULL);
4309 }
cristybd5a96c2011-08-21 00:04:26 +00004310 channel_mask=SetPixelChannelMask(border_image,AlphaChannel);
cristyaa2c16c2012-03-25 22:21:35 +00004311 shadow_image=BlurImage(border_image,0.0,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +00004312 border_image=DestroyImage(border_image);
4313 if (shadow_image == (Image *) NULL)
4314 return((Image *) NULL);
cristyae1969f2011-12-10 03:07:36 +00004315 (void) SetPixelChannelMapMask(shadow_image,channel_mask);
cristy3ed852e2009-09-05 21:47:34 +00004316 if (shadow_image->page.width == 0)
4317 shadow_image->page.width=shadow_image->columns;
4318 if (shadow_image->page.height == 0)
4319 shadow_image->page.height=shadow_image->rows;
cristybb503372010-05-27 20:51:26 +00004320 shadow_image->page.width+=x_offset-(ssize_t) border_info.width;
4321 shadow_image->page.height+=y_offset-(ssize_t) border_info.height;
4322 shadow_image->page.x+=x_offset-(ssize_t) border_info.width;
4323 shadow_image->page.y+=y_offset-(ssize_t) border_info.height;
cristy3ed852e2009-09-05 21:47:34 +00004324 return(shadow_image);
4325}
4326
4327/*
4328%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4329% %
4330% %
4331% %
4332% S k e t c h I m a g e %
4333% %
4334% %
4335% %
4336%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4337%
4338% SketchImage() simulates a pencil sketch. We convolve the image with a
4339% Gaussian operator of the given radius and standard deviation (sigma). For
4340% reasonable results, radius should be larger than sigma. Use a radius of 0
4341% and SketchImage() selects a suitable radius for you. Angle gives the angle
4342% of the sketch.
4343%
4344% The format of the SketchImage method is:
4345%
4346% Image *SketchImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00004347% const double sigma,const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004348%
4349% A description of each parameter follows:
4350%
4351% o image: the image.
4352%
cristy574cc262011-08-05 01:23:58 +00004353% o radius: the radius of the Gaussian, in pixels, not counting the
4354% center pixel.
cristy3ed852e2009-09-05 21:47:34 +00004355%
4356% o sigma: the standard deviation of the Gaussian, in pixels.
4357%
cristyf7ef0252011-09-09 14:50:06 +00004358% o angle: apply the effect along this angle.
4359%
cristy3ed852e2009-09-05 21:47:34 +00004360% o exception: return any errors or warnings in this structure.
4361%
4362*/
4363MagickExport Image *SketchImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00004364 const double sigma,const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004365{
cristyfa112112010-01-04 17:48:07 +00004366 CacheView
4367 *random_view;
4368
cristy3ed852e2009-09-05 21:47:34 +00004369 Image
4370 *blend_image,
4371 *blur_image,
4372 *dodge_image,
4373 *random_image,
4374 *sketch_image;
4375
cristy3ed852e2009-09-05 21:47:34 +00004376 MagickBooleanType
4377 status;
4378
cristy3ed852e2009-09-05 21:47:34 +00004379 RandomInfo
cristyfa112112010-01-04 17:48:07 +00004380 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00004381
cristy9d314ff2011-03-09 01:30:28 +00004382 ssize_t
4383 y;
4384
cristy3ed852e2009-09-05 21:47:34 +00004385 /*
4386 Sketch image.
4387 */
4388 random_image=CloneImage(image,image->columns << 1,image->rows << 1,
4389 MagickTrue,exception);
4390 if (random_image == (Image *) NULL)
4391 return((Image *) NULL);
4392 status=MagickTrue;
cristy1b784432009-12-19 02:20:40 +00004393 random_info=AcquireRandomInfoThreadSet();
cristy3ed852e2009-09-05 21:47:34 +00004394 random_view=AcquireCacheView(random_image);
cristy1b784432009-12-19 02:20:40 +00004395#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00004396 #pragma omp parallel for schedule(static,4) shared(status)
cristy1b784432009-12-19 02:20:40 +00004397#endif
cristybb503372010-05-27 20:51:26 +00004398 for (y=0; y < (ssize_t) random_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004399 {
cristy5c9e6f22010-09-17 17:31:01 +00004400 const int
4401 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00004402
cristybb503372010-05-27 20:51:26 +00004403 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004404 x;
4405
cristy4c08aed2011-07-01 19:47:50 +00004406 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004407 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004408
cristy1b784432009-12-19 02:20:40 +00004409 if (status == MagickFalse)
4410 continue;
cristy3ed852e2009-09-05 21:47:34 +00004411 q=QueueCacheViewAuthenticPixels(random_view,0,y,random_image->columns,1,
4412 exception);
cristyacd2ed22011-08-30 01:44:23 +00004413 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004414 {
4415 status=MagickFalse;
4416 continue;
4417 }
cristybb503372010-05-27 20:51:26 +00004418 for (x=0; x < (ssize_t) random_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004419 {
cristy76f512e2011-09-12 01:26:56 +00004420 MagickRealType
4421 value;
4422
4423 register ssize_t
4424 i;
4425
cristy10a6c612012-01-29 21:41:05 +00004426 if (GetPixelMask(random_image,q) != 0)
4427 {
4428 q+=GetPixelChannels(random_image);
4429 continue;
4430 }
cristy76f512e2011-09-12 01:26:56 +00004431 value=GetPseudoRandomValue(random_info[id]);
4432 for (i=0; i < (ssize_t) GetPixelChannels(random_image); i++)
4433 {
cristyabace412011-12-11 15:56:53 +00004434 PixelChannel
4435 channel;
4436
cristy76f512e2011-09-12 01:26:56 +00004437 PixelTrait
4438 traits;
4439
cristyabace412011-12-11 15:56:53 +00004440 channel=GetPixelChannelMapChannel(image,i);
4441 traits=GetPixelChannelMapTraits(image,channel);
cristy76f512e2011-09-12 01:26:56 +00004442 if (traits == UndefinedPixelTrait)
4443 continue;
4444 q[i]=ClampToQuantum(QuantumRange*value);
4445 }
cristyed231572011-07-14 02:18:59 +00004446 q+=GetPixelChannels(random_image);
cristy3ed852e2009-09-05 21:47:34 +00004447 }
4448 if (SyncCacheViewAuthenticPixels(random_view,exception) == MagickFalse)
4449 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004450 }
4451 random_view=DestroyCacheView(random_view);
cristy1b784432009-12-19 02:20:40 +00004452 random_info=DestroyRandomInfoThreadSet(random_info);
cristy3ed852e2009-09-05 21:47:34 +00004453 if (status == MagickFalse)
4454 {
4455 random_image=DestroyImage(random_image);
4456 return(random_image);
4457 }
cristyaa2c16c2012-03-25 22:21:35 +00004458 blur_image=MotionBlurImage(random_image,radius,sigma,angle,exception);
cristy3ed852e2009-09-05 21:47:34 +00004459 random_image=DestroyImage(random_image);
4460 if (blur_image == (Image *) NULL)
4461 return((Image *) NULL);
cristy6bfd6902011-12-09 01:33:45 +00004462 dodge_image=EdgeImage(blur_image,radius,1.0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004463 blur_image=DestroyImage(blur_image);
4464 if (dodge_image == (Image *) NULL)
4465 return((Image *) NULL);
cristye23ec9d2011-08-16 18:15:40 +00004466 (void) NormalizeImage(dodge_image,exception);
cristyb3e7c6c2011-07-24 01:43:55 +00004467 (void) NegateImage(dodge_image,MagickFalse,exception);
cristye941a752011-10-15 01:52:48 +00004468 (void) TransformImage(&dodge_image,(char *) NULL,"50%",exception);
cristy3ed852e2009-09-05 21:47:34 +00004469 sketch_image=CloneImage(image,0,0,MagickTrue,exception);
4470 if (sketch_image == (Image *) NULL)
4471 {
4472 dodge_image=DestroyImage(dodge_image);
4473 return((Image *) NULL);
4474 }
cristyfeb3e962012-03-29 17:25:55 +00004475 (void) CompositeImage(sketch_image,dodge_image,ColorDodgeCompositeOp,
cristy39172402012-03-30 13:04:39 +00004476 MagickTrue,0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004477 dodge_image=DestroyImage(dodge_image);
4478 blend_image=CloneImage(image,0,0,MagickTrue,exception);
4479 if (blend_image == (Image *) NULL)
4480 {
4481 sketch_image=DestroyImage(sketch_image);
4482 return((Image *) NULL);
4483 }
4484 (void) SetImageArtifact(blend_image,"compose:args","20x80");
cristy39172402012-03-30 13:04:39 +00004485 (void) CompositeImage(sketch_image,blend_image,BlendCompositeOp,MagickTrue,
cristyfeb3e962012-03-29 17:25:55 +00004486 0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004487 blend_image=DestroyImage(blend_image);
4488 return(sketch_image);
4489}
4490
4491/*
4492%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4493% %
4494% %
4495% %
4496% S o l a r i z e I m a g e %
4497% %
4498% %
4499% %
4500%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4501%
4502% SolarizeImage() applies a special effect to the image, similar to the effect
4503% achieved in a photo darkroom by selectively exposing areas of photo
4504% sensitive paper to light. Threshold ranges from 0 to QuantumRange and is a
4505% measure of the extent of the solarization.
4506%
4507% The format of the SolarizeImage method is:
4508%
cristy5cbc0162011-08-29 00:36:28 +00004509% MagickBooleanType SolarizeImage(Image *image,const double threshold,
4510% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004511%
4512% A description of each parameter follows:
4513%
4514% o image: the image.
4515%
4516% o threshold: Define the extent of the solarization.
4517%
cristy5cbc0162011-08-29 00:36:28 +00004518% o exception: return any errors or warnings in this structure.
4519%
cristy3ed852e2009-09-05 21:47:34 +00004520*/
4521MagickExport MagickBooleanType SolarizeImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00004522 const double threshold,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004523{
4524#define SolarizeImageTag "Solarize/Image"
4525
cristyc4c8d132010-01-07 01:58:38 +00004526 CacheView
4527 *image_view;
4528
cristy3ed852e2009-09-05 21:47:34 +00004529 MagickBooleanType
4530 status;
4531
cristybb503372010-05-27 20:51:26 +00004532 MagickOffsetType
4533 progress;
4534
4535 ssize_t
4536 y;
4537
cristy3ed852e2009-09-05 21:47:34 +00004538 assert(image != (Image *) NULL);
4539 assert(image->signature == MagickSignature);
4540 if (image->debug != MagickFalse)
4541 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4542 if (image->storage_class == PseudoClass)
4543 {
cristybb503372010-05-27 20:51:26 +00004544 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004545 i;
4546
4547 /*
4548 Solarize colormap.
4549 */
cristybb503372010-05-27 20:51:26 +00004550 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00004551 {
4552 if ((MagickRealType) image->colormap[i].red > threshold)
4553 image->colormap[i].red=(Quantum) QuantumRange-image->colormap[i].red;
4554 if ((MagickRealType) image->colormap[i].green > threshold)
4555 image->colormap[i].green=(Quantum) QuantumRange-
4556 image->colormap[i].green;
4557 if ((MagickRealType) image->colormap[i].blue > threshold)
4558 image->colormap[i].blue=(Quantum) QuantumRange-
4559 image->colormap[i].blue;
4560 }
4561 }
4562 /*
4563 Solarize image.
4564 */
4565 status=MagickTrue;
4566 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00004567 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00004568#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00004569 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00004570#endif
cristybb503372010-05-27 20:51:26 +00004571 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004572 {
cristybb503372010-05-27 20:51:26 +00004573 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004574 x;
4575
cristy4c08aed2011-07-01 19:47:50 +00004576 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004577 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004578
4579 if (status == MagickFalse)
4580 continue;
cristy5cbc0162011-08-29 00:36:28 +00004581 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00004582 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004583 {
4584 status=MagickFalse;
4585 continue;
4586 }
cristybb503372010-05-27 20:51:26 +00004587 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004588 {
cristy76f512e2011-09-12 01:26:56 +00004589 register ssize_t
4590 i;
4591
cristy10a6c612012-01-29 21:41:05 +00004592 if (GetPixelMask(image,q) != 0)
4593 {
4594 q+=GetPixelChannels(image);
4595 continue;
4596 }
cristy76f512e2011-09-12 01:26:56 +00004597 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4598 {
cristyabace412011-12-11 15:56:53 +00004599 PixelChannel
4600 channel;
4601
cristy76f512e2011-09-12 01:26:56 +00004602 PixelTrait
4603 traits;
4604
cristyabace412011-12-11 15:56:53 +00004605 channel=GetPixelChannelMapChannel(image,i);
4606 traits=GetPixelChannelMapTraits(image,channel);
cristy76f512e2011-09-12 01:26:56 +00004607 if ((traits == UndefinedPixelTrait) ||
4608 ((traits & CopyPixelTrait) != 0))
4609 continue;
4610 if ((MagickRealType) q[i] > threshold)
4611 q[i]=QuantumRange-q[i];
4612 }
cristyed231572011-07-14 02:18:59 +00004613 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00004614 }
4615 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4616 status=MagickFalse;
4617 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4618 {
4619 MagickBooleanType
4620 proceed;
4621
cristyb5d5f722009-11-04 03:03:49 +00004622#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00004623 #pragma omp critical (MagickCore_SolarizeImage)
cristy3ed852e2009-09-05 21:47:34 +00004624#endif
4625 proceed=SetImageProgress(image,SolarizeImageTag,progress++,image->rows);
4626 if (proceed == MagickFalse)
4627 status=MagickFalse;
4628 }
4629 }
4630 image_view=DestroyCacheView(image_view);
4631 return(status);
4632}
4633
4634/*
4635%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4636% %
4637% %
4638% %
4639% S t e g a n o I m a g e %
4640% %
4641% %
4642% %
4643%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4644%
4645% SteganoImage() hides a digital watermark within the image. Recover
4646% the hidden watermark later to prove that the authenticity of an image.
4647% Offset defines the start position within the image to hide the watermark.
4648%
4649% The format of the SteganoImage method is:
4650%
4651% Image *SteganoImage(const Image *image,Image *watermark,
4652% ExceptionInfo *exception)
4653%
4654% A description of each parameter follows:
4655%
4656% o image: the image.
4657%
4658% o watermark: the watermark image.
4659%
4660% o exception: return any errors or warnings in this structure.
4661%
4662*/
4663MagickExport Image *SteganoImage(const Image *image,const Image *watermark,
4664 ExceptionInfo *exception)
4665{
cristye1bf8ad2010-09-19 17:07:03 +00004666#define GetBit(alpha,i) ((((size_t) (alpha) >> (size_t) (i)) & 0x01) != 0)
cristy4c08aed2011-07-01 19:47:50 +00004667#define SetBit(alpha,i,set) (Quantum) ((set) != 0 ? (size_t) (alpha) \
cristyeaedf062010-05-29 22:36:02 +00004668 | (one << (size_t) (i)) : (size_t) (alpha) & ~(one << (size_t) (i)))
cristy3ed852e2009-09-05 21:47:34 +00004669#define SteganoImageTag "Stegano/Image"
4670
cristyb0d3bb92010-09-22 14:37:58 +00004671 CacheView
4672 *stegano_view,
4673 *watermark_view;
4674
cristy3ed852e2009-09-05 21:47:34 +00004675 Image
4676 *stegano_image;
4677
4678 int
4679 c;
4680
cristy3ed852e2009-09-05 21:47:34 +00004681 MagickBooleanType
4682 status;
4683
cristy101ab702011-10-13 13:06:32 +00004684 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00004685 pixel;
4686
cristy4c08aed2011-07-01 19:47:50 +00004687 register Quantum
cristy3ed852e2009-09-05 21:47:34 +00004688 *q;
4689
cristye1bf8ad2010-09-19 17:07:03 +00004690 register ssize_t
4691 x;
4692
cristybb503372010-05-27 20:51:26 +00004693 size_t
cristyeaedf062010-05-29 22:36:02 +00004694 depth,
4695 one;
cristy3ed852e2009-09-05 21:47:34 +00004696
cristye1bf8ad2010-09-19 17:07:03 +00004697 ssize_t
4698 i,
4699 j,
4700 k,
4701 y;
4702
cristy3ed852e2009-09-05 21:47:34 +00004703 /*
4704 Initialize steganographic image attributes.
4705 */
4706 assert(image != (const Image *) NULL);
4707 assert(image->signature == MagickSignature);
4708 if (image->debug != MagickFalse)
4709 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4710 assert(watermark != (const Image *) NULL);
4711 assert(watermark->signature == MagickSignature);
4712 assert(exception != (ExceptionInfo *) NULL);
4713 assert(exception->signature == MagickSignature);
cristyeaedf062010-05-29 22:36:02 +00004714 one=1UL;
cristy3ed852e2009-09-05 21:47:34 +00004715 stegano_image=CloneImage(image,0,0,MagickTrue,exception);
4716 if (stegano_image == (Image *) NULL)
4717 return((Image *) NULL);
cristyf61b1832012-04-01 01:38:19 +00004718 stegano_image->depth=MAGICKCORE_QUANTUM_DEPTH;
cristy574cc262011-08-05 01:23:58 +00004719 if (SetImageStorageClass(stegano_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004720 {
cristy3ed852e2009-09-05 21:47:34 +00004721 stegano_image=DestroyImage(stegano_image);
4722 return((Image *) NULL);
4723 }
cristy3ed852e2009-09-05 21:47:34 +00004724 /*
4725 Hide watermark in low-order bits of image.
4726 */
4727 c=0;
4728 i=0;
4729 j=0;
4730 depth=stegano_image->depth;
cristyf61b1832012-04-01 01:38:19 +00004731 k=stegano_image->offset;
cristyda16f162011-02-19 23:52:17 +00004732 status=MagickTrue;
cristyb0d3bb92010-09-22 14:37:58 +00004733 watermark_view=AcquireCacheView(watermark);
4734 stegano_view=AcquireCacheView(stegano_image);
cristybb503372010-05-27 20:51:26 +00004735 for (i=(ssize_t) depth-1; (i >= 0) && (j < (ssize_t) depth); i--)
cristy3ed852e2009-09-05 21:47:34 +00004736 {
cristybb503372010-05-27 20:51:26 +00004737 for (y=0; (y < (ssize_t) watermark->rows) && (j < (ssize_t) depth); y++)
cristy3ed852e2009-09-05 21:47:34 +00004738 {
cristybb503372010-05-27 20:51:26 +00004739 for (x=0; (x < (ssize_t) watermark->columns) && (j < (ssize_t) depth); x++)
cristy3ed852e2009-09-05 21:47:34 +00004740 {
cristy1707c6c2012-01-18 23:30:54 +00004741 ssize_t
4742 offset;
4743
cristyf05d4942012-03-17 16:26:09 +00004744 (void) GetOneCacheViewVirtualPixelInfo(watermark_view,x,y,&pixel,
cristyda1f9c12011-10-02 21:39:49 +00004745 exception);
cristy1707c6c2012-01-18 23:30:54 +00004746 offset=k/(ssize_t) stegano_image->columns;
4747 if (offset >= (ssize_t) stegano_image->rows)
cristy3ed852e2009-09-05 21:47:34 +00004748 break;
cristyb0d3bb92010-09-22 14:37:58 +00004749 q=GetCacheViewAuthenticPixels(stegano_view,k % (ssize_t)
4750 stegano_image->columns,k/(ssize_t) stegano_image->columns,1,1,
4751 exception);
cristyacd2ed22011-08-30 01:44:23 +00004752 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004753 break;
4754 switch (c)
4755 {
4756 case 0:
4757 {
cristyf61b1832012-04-01 01:38:19 +00004758 SetPixelRed(stegano_image,SetBit(GetPixelRed(stegano_image,q),j,
4759 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004760 break;
4761 }
4762 case 1:
4763 {
cristyf61b1832012-04-01 01:38:19 +00004764 SetPixelGreen(stegano_image,SetBit(GetPixelGreen(stegano_image,q),j,
4765 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004766 break;
4767 }
4768 case 2:
4769 {
cristyf61b1832012-04-01 01:38:19 +00004770 SetPixelBlue(stegano_image,SetBit(GetPixelBlue(stegano_image,q),j,
4771 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004772 break;
4773 }
4774 }
cristyb0d3bb92010-09-22 14:37:58 +00004775 if (SyncCacheViewAuthenticPixels(stegano_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004776 break;
4777 c++;
4778 if (c == 3)
4779 c=0;
4780 k++;
cristybb503372010-05-27 20:51:26 +00004781 if (k == (ssize_t) (stegano_image->columns*stegano_image->columns))
cristy3ed852e2009-09-05 21:47:34 +00004782 k=0;
cristyf61b1832012-04-01 01:38:19 +00004783 if (k == stegano_image->offset)
cristy3ed852e2009-09-05 21:47:34 +00004784 j++;
4785 }
4786 }
cristy8b27a6d2010-02-14 03:31:15 +00004787 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004788 {
cristy8b27a6d2010-02-14 03:31:15 +00004789 MagickBooleanType
4790 proceed;
4791
4792 proceed=SetImageProgress(image,SteganoImageTag,(MagickOffsetType)
4793 (depth-i),depth);
4794 if (proceed == MagickFalse)
4795 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004796 }
4797 }
cristyb0d3bb92010-09-22 14:37:58 +00004798 stegano_view=DestroyCacheView(stegano_view);
4799 watermark_view=DestroyCacheView(watermark_view);
cristyda16f162011-02-19 23:52:17 +00004800 if (status == MagickFalse)
4801 {
4802 stegano_image=DestroyImage(stegano_image);
4803 return((Image *) NULL);
4804 }
cristy3ed852e2009-09-05 21:47:34 +00004805 return(stegano_image);
4806}
4807
4808/*
4809%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4810% %
4811% %
4812% %
4813% S t e r e o A n a g l y p h I m a g e %
4814% %
4815% %
4816% %
4817%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4818%
4819% StereoAnaglyphImage() combines two images and produces a single image that
4820% is the composite of a left and right image of a stereo pair. Special
4821% red-green stereo glasses are required to view this effect.
4822%
4823% The format of the StereoAnaglyphImage method is:
4824%
4825% Image *StereoImage(const Image *left_image,const Image *right_image,
4826% ExceptionInfo *exception)
4827% Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004828% const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004829% ExceptionInfo *exception)
4830%
4831% A description of each parameter follows:
4832%
4833% o left_image: the left image.
4834%
4835% o right_image: the right image.
4836%
4837% o exception: return any errors or warnings in this structure.
4838%
4839% o x_offset: amount, in pixels, by which the left image is offset to the
4840% right of the right image.
4841%
4842% o y_offset: amount, in pixels, by which the left image is offset to the
4843% bottom of the right image.
4844%
4845%
4846*/
4847MagickExport Image *StereoImage(const Image *left_image,
4848 const Image *right_image,ExceptionInfo *exception)
4849{
4850 return(StereoAnaglyphImage(left_image,right_image,0,0,exception));
4851}
4852
4853MagickExport Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004854 const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004855 ExceptionInfo *exception)
4856{
4857#define StereoImageTag "Stereo/Image"
4858
4859 const Image
4860 *image;
4861
4862 Image
4863 *stereo_image;
4864
cristy3ed852e2009-09-05 21:47:34 +00004865 MagickBooleanType
4866 status;
4867
cristy9d314ff2011-03-09 01:30:28 +00004868 ssize_t
4869 y;
4870
cristy3ed852e2009-09-05 21:47:34 +00004871 assert(left_image != (const Image *) NULL);
4872 assert(left_image->signature == MagickSignature);
4873 if (left_image->debug != MagickFalse)
4874 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4875 left_image->filename);
4876 assert(right_image != (const Image *) NULL);
4877 assert(right_image->signature == MagickSignature);
4878 assert(exception != (ExceptionInfo *) NULL);
4879 assert(exception->signature == MagickSignature);
4880 assert(right_image != (const Image *) NULL);
4881 image=left_image;
4882 if ((left_image->columns != right_image->columns) ||
4883 (left_image->rows != right_image->rows))
4884 ThrowImageException(ImageError,"LeftAndRightImageSizesDiffer");
4885 /*
4886 Initialize stereo image attributes.
4887 */
4888 stereo_image=CloneImage(left_image,left_image->columns,left_image->rows,
4889 MagickTrue,exception);
4890 if (stereo_image == (Image *) NULL)
4891 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004892 if (SetImageStorageClass(stereo_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004893 {
cristy3ed852e2009-09-05 21:47:34 +00004894 stereo_image=DestroyImage(stereo_image);
4895 return((Image *) NULL);
4896 }
4897 /*
4898 Copy left image to red channel and right image to blue channel.
4899 */
cristyda16f162011-02-19 23:52:17 +00004900 status=MagickTrue;
cristybb503372010-05-27 20:51:26 +00004901 for (y=0; y < (ssize_t) stereo_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004902 {
cristy4c08aed2011-07-01 19:47:50 +00004903 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004904 *restrict p,
4905 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004906
cristybb503372010-05-27 20:51:26 +00004907 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004908 x;
4909
cristy4c08aed2011-07-01 19:47:50 +00004910 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004911 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +00004912
4913 p=GetVirtualPixels(left_image,-x_offset,y-y_offset,image->columns,1,
4914 exception);
4915 q=GetVirtualPixels(right_image,0,y,right_image->columns,1,exception);
4916 r=QueueAuthenticPixels(stereo_image,0,y,stereo_image->columns,1,exception);
cristy76f512e2011-09-12 01:26:56 +00004917 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL) ||
4918 (r == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004919 break;
cristybb503372010-05-27 20:51:26 +00004920 for (x=0; x < (ssize_t) stereo_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004921 {
cristy4c08aed2011-07-01 19:47:50 +00004922 SetPixelRed(image,GetPixelRed(left_image,p),r);
cristy76f512e2011-09-12 01:26:56 +00004923 SetPixelGreen(image,GetPixelGreen(right_image,q),r);
4924 SetPixelBlue(image,GetPixelBlue(right_image,q),r);
4925 if ((GetPixelAlphaTraits(stereo_image) & CopyPixelTrait) != 0)
4926 SetPixelAlpha(image,(GetPixelAlpha(left_image,p)+
4927 GetPixelAlpha(right_image,q))/2,r);
cristyed231572011-07-14 02:18:59 +00004928 p+=GetPixelChannels(left_image);
cristy76f512e2011-09-12 01:26:56 +00004929 q+=GetPixelChannels(right_image);
4930 r+=GetPixelChannels(stereo_image);
cristy3ed852e2009-09-05 21:47:34 +00004931 }
4932 if (SyncAuthenticPixels(stereo_image,exception) == MagickFalse)
4933 break;
cristy8b27a6d2010-02-14 03:31:15 +00004934 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004935 {
cristy8b27a6d2010-02-14 03:31:15 +00004936 MagickBooleanType
4937 proceed;
4938
cristybb503372010-05-27 20:51:26 +00004939 proceed=SetImageProgress(image,StereoImageTag,(MagickOffsetType) y,
4940 stereo_image->rows);
cristy8b27a6d2010-02-14 03:31:15 +00004941 if (proceed == MagickFalse)
4942 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004943 }
4944 }
cristyda16f162011-02-19 23:52:17 +00004945 if (status == MagickFalse)
4946 {
4947 stereo_image=DestroyImage(stereo_image);
4948 return((Image *) NULL);
4949 }
cristy3ed852e2009-09-05 21:47:34 +00004950 return(stereo_image);
4951}
4952
4953/*
4954%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4955% %
4956% %
4957% %
4958% S w i r l I m a g e %
4959% %
4960% %
4961% %
4962%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4963%
4964% SwirlImage() swirls the pixels about the center of the image, where
4965% degrees indicates the sweep of the arc through which each pixel is moved.
4966% You get a more dramatic effect as the degrees move from 1 to 360.
4967%
4968% The format of the SwirlImage method is:
4969%
4970% Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00004971% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004972%
4973% A description of each parameter follows:
4974%
4975% o image: the image.
4976%
4977% o degrees: Define the tightness of the swirling effect.
4978%
cristy76f512e2011-09-12 01:26:56 +00004979% o method: the pixel interpolation method.
4980%
cristy3ed852e2009-09-05 21:47:34 +00004981% o exception: return any errors or warnings in this structure.
4982%
4983*/
4984MagickExport 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#define SwirlImageTag "Swirl/Image"
4988
cristyfa112112010-01-04 17:48:07 +00004989 CacheView
4990 *image_view,
4991 *swirl_view;
4992
cristy3ed852e2009-09-05 21:47:34 +00004993 Image
4994 *swirl_image;
4995
cristy3ed852e2009-09-05 21:47:34 +00004996 MagickBooleanType
4997 status;
4998
cristybb503372010-05-27 20:51:26 +00004999 MagickOffsetType
5000 progress;
5001
cristy3ed852e2009-09-05 21:47:34 +00005002 MagickRealType
5003 radius;
5004
5005 PointInfo
5006 center,
5007 scale;
5008
cristybb503372010-05-27 20:51:26 +00005009 ssize_t
5010 y;
5011
cristy3ed852e2009-09-05 21:47:34 +00005012 /*
5013 Initialize swirl image attributes.
5014 */
5015 assert(image != (const Image *) NULL);
5016 assert(image->signature == MagickSignature);
5017 if (image->debug != MagickFalse)
5018 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5019 assert(exception != (ExceptionInfo *) NULL);
5020 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00005021 swirl_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005022 if (swirl_image == (Image *) NULL)
5023 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005024 if (SetImageStorageClass(swirl_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005025 {
cristy3ed852e2009-09-05 21:47:34 +00005026 swirl_image=DestroyImage(swirl_image);
5027 return((Image *) NULL);
5028 }
cristy4c08aed2011-07-01 19:47:50 +00005029 if (swirl_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00005030 swirl_image->matte=MagickTrue;
5031 /*
5032 Compute scaling factor.
5033 */
5034 center.x=(double) image->columns/2.0;
5035 center.y=(double) image->rows/2.0;
5036 radius=MagickMax(center.x,center.y);
5037 scale.x=1.0;
5038 scale.y=1.0;
5039 if (image->columns > image->rows)
5040 scale.y=(double) image->columns/(double) image->rows;
5041 else
5042 if (image->columns < image->rows)
5043 scale.x=(double) image->rows/(double) image->columns;
5044 degrees=(double) DegreesToRadians(degrees);
5045 /*
5046 Swirl image.
5047 */
5048 status=MagickTrue;
5049 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00005050 image_view=AcquireCacheView(image);
5051 swirl_view=AcquireCacheView(swirl_image);
cristyb5d5f722009-11-04 03:03:49 +00005052#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005053 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00005054#endif
cristybb503372010-05-27 20:51:26 +00005055 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005056 {
cristy3ed852e2009-09-05 21:47:34 +00005057 MagickRealType
5058 distance;
5059
5060 PointInfo
5061 delta;
5062
cristy6d188022011-09-12 13:23:33 +00005063 register const Quantum
5064 *restrict p;
5065
cristybb503372010-05-27 20:51:26 +00005066 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005067 x;
5068
cristy4c08aed2011-07-01 19:47:50 +00005069 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005070 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005071
5072 if (status == MagickFalse)
5073 continue;
cristy6d188022011-09-12 13:23:33 +00005074 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy1707c6c2012-01-18 23:30:54 +00005075 q=QueueCacheViewAuthenticPixels(swirl_view,0,y,swirl_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00005076 exception);
cristy6d188022011-09-12 13:23:33 +00005077 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005078 {
5079 status=MagickFalse;
5080 continue;
5081 }
cristy3ed852e2009-09-05 21:47:34 +00005082 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00005083 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005084 {
5085 /*
5086 Determine if the pixel is within an ellipse.
5087 */
cristy10a6c612012-01-29 21:41:05 +00005088 if (GetPixelMask(image,p) != 0)
5089 {
5090 p+=GetPixelChannels(image);
5091 q+=GetPixelChannels(swirl_image);
5092 continue;
5093 }
cristy3ed852e2009-09-05 21:47:34 +00005094 delta.x=scale.x*(double) (x-center.x);
5095 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00005096 if (distance >= (radius*radius))
5097 {
cristy1707c6c2012-01-18 23:30:54 +00005098 register ssize_t
5099 i;
5100
cristy6d188022011-09-12 13:23:33 +00005101 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy1707c6c2012-01-18 23:30:54 +00005102 {
5103 PixelChannel
5104 channel;
5105
5106 PixelTrait
5107 swirl_traits,
5108 traits;
5109
5110 channel=GetPixelChannelMapChannel(image,i);
5111 traits=GetPixelChannelMapTraits(image,channel);
5112 swirl_traits=GetPixelChannelMapTraits(swirl_image,channel);
5113 if ((traits == UndefinedPixelTrait) ||
5114 (swirl_traits == UndefinedPixelTrait))
5115 continue;
5116 SetPixelChannel(swirl_image,channel,p[i],q);
5117 }
cristy6d188022011-09-12 13:23:33 +00005118 }
5119 else
cristy3ed852e2009-09-05 21:47:34 +00005120 {
5121 MagickRealType
5122 cosine,
5123 factor,
5124 sine;
5125
5126 /*
5127 Swirl the pixel.
5128 */
5129 factor=1.0-sqrt((double) distance)/radius;
5130 sine=sin((double) (degrees*factor*factor));
5131 cosine=cos((double) (degrees*factor*factor));
cristy76f512e2011-09-12 01:26:56 +00005132 status=InterpolatePixelChannels(image,image_view,swirl_image,method,
5133 ((cosine*delta.x-sine*delta.y)/scale.x+center.x),(double)
5134 ((sine*delta.x+cosine*delta.y)/scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00005135 }
cristy6d188022011-09-12 13:23:33 +00005136 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00005137 q+=GetPixelChannels(swirl_image);
cristy3ed852e2009-09-05 21:47:34 +00005138 }
5139 if (SyncCacheViewAuthenticPixels(swirl_view,exception) == MagickFalse)
5140 status=MagickFalse;
5141 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5142 {
5143 MagickBooleanType
5144 proceed;
5145
cristyb5d5f722009-11-04 03:03:49 +00005146#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005147 #pragma omp critical (MagickCore_SwirlImage)
cristy3ed852e2009-09-05 21:47:34 +00005148#endif
5149 proceed=SetImageProgress(image,SwirlImageTag,progress++,image->rows);
5150 if (proceed == MagickFalse)
5151 status=MagickFalse;
5152 }
5153 }
5154 swirl_view=DestroyCacheView(swirl_view);
5155 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00005156 if (status == MagickFalse)
5157 swirl_image=DestroyImage(swirl_image);
5158 return(swirl_image);
5159}
5160
5161/*
5162%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5163% %
5164% %
5165% %
5166% T i n t I m a g e %
5167% %
5168% %
5169% %
5170%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5171%
5172% TintImage() applies a color vector to each pixel in the image. The length
5173% of the vector is 0 for black and white and at its maximum for the midtones.
5174% The vector weighting function is f(x)=(1-(4.0*((x-0.5)*(x-0.5))))
5175%
5176% The format of the TintImage method is:
5177%
cristyb817c3f2011-10-03 14:00:35 +00005178% Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005179% const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005180%
5181% A description of each parameter follows:
5182%
5183% o image: the image.
5184%
cristyb817c3f2011-10-03 14:00:35 +00005185% o blend: A color value used for tinting.
cristy3ed852e2009-09-05 21:47:34 +00005186%
5187% o tint: A color value used for tinting.
5188%
5189% o exception: return any errors or warnings in this structure.
5190%
5191*/
cristyb817c3f2011-10-03 14:00:35 +00005192MagickExport Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005193 const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005194{
5195#define TintImageTag "Tint/Image"
5196
cristyc4c8d132010-01-07 01:58:38 +00005197 CacheView
5198 *image_view,
5199 *tint_view;
5200
cristy3ed852e2009-09-05 21:47:34 +00005201 GeometryInfo
5202 geometry_info;
5203
5204 Image
5205 *tint_image;
5206
cristy3ed852e2009-09-05 21:47:34 +00005207 MagickBooleanType
5208 status;
5209
cristybb503372010-05-27 20:51:26 +00005210 MagickOffsetType
5211 progress;
cristy3ed852e2009-09-05 21:47:34 +00005212
cristy28474bf2011-09-11 23:32:52 +00005213 MagickRealType
5214 intensity;
5215
cristy4c08aed2011-07-01 19:47:50 +00005216 PixelInfo
cristy1707c6c2012-01-18 23:30:54 +00005217 color_vector;
cristy3ed852e2009-09-05 21:47:34 +00005218
cristybb503372010-05-27 20:51:26 +00005219 MagickStatusType
5220 flags;
5221
5222 ssize_t
5223 y;
5224
cristy3ed852e2009-09-05 21:47:34 +00005225 /*
5226 Allocate tint image.
5227 */
5228 assert(image != (const Image *) NULL);
5229 assert(image->signature == MagickSignature);
5230 if (image->debug != MagickFalse)
5231 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5232 assert(exception != (ExceptionInfo *) NULL);
5233 assert(exception->signature == MagickSignature);
5234 tint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5235 if (tint_image == (Image *) NULL)
5236 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005237 if (SetImageStorageClass(tint_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005238 {
cristy3ed852e2009-09-05 21:47:34 +00005239 tint_image=DestroyImage(tint_image);
5240 return((Image *) NULL);
5241 }
cristyaed9c382011-10-03 17:54:21 +00005242 if (blend == (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005243 return(tint_image);
5244 /*
5245 Determine RGB values of the color.
5246 */
cristy1707c6c2012-01-18 23:30:54 +00005247 GetPixelInfo(image,&color_vector);
cristyb817c3f2011-10-03 14:00:35 +00005248 flags=ParseGeometry(blend,&geometry_info);
cristy1707c6c2012-01-18 23:30:54 +00005249 color_vector.red=geometry_info.rho;
5250 color_vector.green=geometry_info.rho;
5251 color_vector.blue=geometry_info.rho;
5252 color_vector.alpha=OpaqueAlpha;
cristy3ed852e2009-09-05 21:47:34 +00005253 if ((flags & SigmaValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005254 color_vector.green=geometry_info.sigma;
cristy3ed852e2009-09-05 21:47:34 +00005255 if ((flags & XiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005256 color_vector.blue=geometry_info.xi;
cristyb817c3f2011-10-03 14:00:35 +00005257 if ((flags & PsiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005258 color_vector.alpha=geometry_info.psi;
cristy76f512e2011-09-12 01:26:56 +00005259 if (image->colorspace == CMYKColorspace)
5260 {
cristy1707c6c2012-01-18 23:30:54 +00005261 color_vector.black=geometry_info.rho;
cristy76f512e2011-09-12 01:26:56 +00005262 if ((flags & PsiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005263 color_vector.black=geometry_info.psi;
cristy76f512e2011-09-12 01:26:56 +00005264 if ((flags & ChiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005265 color_vector.alpha=geometry_info.chi;
cristy76f512e2011-09-12 01:26:56 +00005266 }
cristy28474bf2011-09-11 23:32:52 +00005267 intensity=(MagickRealType) GetPixelInfoIntensity(tint);
cristy1707c6c2012-01-18 23:30:54 +00005268 color_vector.red=(MagickRealType) (color_vector.red*tint->red/100.0-
5269 intensity);
5270 color_vector.green=(MagickRealType) (color_vector.green*tint->green/100.0-
5271 intensity);
5272 color_vector.blue=(MagickRealType) (color_vector.blue*tint->blue/100.0-
5273 intensity);
5274 color_vector.black=(MagickRealType) (color_vector.black*tint->black/100.0-
5275 intensity);
5276 color_vector.alpha=(MagickRealType) (color_vector.alpha*tint->alpha/100.0-
5277 intensity);
cristy3ed852e2009-09-05 21:47:34 +00005278 /*
5279 Tint image.
5280 */
5281 status=MagickTrue;
5282 progress=0;
5283 image_view=AcquireCacheView(image);
5284 tint_view=AcquireCacheView(tint_image);
cristyb5d5f722009-11-04 03:03:49 +00005285#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00005286 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00005287#endif
cristybb503372010-05-27 20:51:26 +00005288 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005289 {
cristy4c08aed2011-07-01 19:47:50 +00005290 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00005291 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00005292
cristy4c08aed2011-07-01 19:47:50 +00005293 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005294 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005295
cristy6b91acb2011-04-19 12:23:54 +00005296 register ssize_t
5297 x;
5298
cristy3ed852e2009-09-05 21:47:34 +00005299 if (status == MagickFalse)
5300 continue;
5301 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5302 q=QueueCacheViewAuthenticPixels(tint_view,0,y,tint_image->columns,1,
5303 exception);
cristy4c08aed2011-07-01 19:47:50 +00005304 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005305 {
5306 status=MagickFalse;
5307 continue;
5308 }
cristybb503372010-05-27 20:51:26 +00005309 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005310 {
cristy4c08aed2011-07-01 19:47:50 +00005311 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00005312 pixel;
5313
5314 MagickRealType
5315 weight;
5316
cristy1707c6c2012-01-18 23:30:54 +00005317 register ssize_t
5318 i;
5319
cristy10a6c612012-01-29 21:41:05 +00005320 if (GetPixelMask(image,p) != 0)
5321 {
5322 p+=GetPixelChannels(image);
5323 q+=GetPixelChannels(tint_image);
5324 continue;
5325 }
cristy1707c6c2012-01-18 23:30:54 +00005326 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5327 {
5328 PixelChannel
5329 channel;
5330
5331 PixelTrait
5332 tint_traits,
5333 traits;
5334
5335 channel=GetPixelChannelMapChannel(image,i);
5336 traits=GetPixelChannelMapTraits(image,channel);
5337 tint_traits=GetPixelChannelMapTraits(tint_image,channel);
5338 if ((traits == UndefinedPixelTrait) ||
5339 (tint_traits == UndefinedPixelTrait))
5340 continue;
5341 if ((tint_traits & CopyPixelTrait) != 0)
5342 {
5343 SetPixelChannel(tint_image,channel,p[i],q);
5344 continue;
5345 }
5346 }
5347 GetPixelInfo(image,&pixel);
5348 weight=QuantumScale*GetPixelRed(image,p)-0.5;
5349 pixel.red=(MagickRealType) GetPixelRed(image,p)+color_vector.red*
5350 (1.0-(4.0*(weight*weight)));
5351 weight=QuantumScale*GetPixelGreen(image,p)-0.5;
5352 pixel.green=(MagickRealType) GetPixelGreen(image,p)+color_vector.green*
5353 (1.0-(4.0*(weight*weight)));
5354 weight=QuantumScale*GetPixelBlue(image,p)-0.5;
5355 pixel.blue=(MagickRealType) GetPixelBlue(image,p)+color_vector.blue*
5356 (1.0-(4.0*(weight*weight)));
5357 weight=QuantumScale*GetPixelBlack(image,p)-0.5;
5358 pixel.black=(MagickRealType) GetPixelBlack(image,p)+color_vector.black*
5359 (1.0-(4.0*(weight*weight)));
5360 SetPixelInfoPixel(tint_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +00005361 p+=GetPixelChannels(image);
5362 q+=GetPixelChannels(tint_image);
cristy3ed852e2009-09-05 21:47:34 +00005363 }
5364 if (SyncCacheViewAuthenticPixels(tint_view,exception) == MagickFalse)
5365 status=MagickFalse;
5366 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5367 {
5368 MagickBooleanType
5369 proceed;
5370
cristyb5d5f722009-11-04 03:03:49 +00005371#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005372 #pragma omp critical (MagickCore_TintImage)
cristy3ed852e2009-09-05 21:47:34 +00005373#endif
5374 proceed=SetImageProgress(image,TintImageTag,progress++,image->rows);
5375 if (proceed == MagickFalse)
5376 status=MagickFalse;
5377 }
5378 }
5379 tint_view=DestroyCacheView(tint_view);
5380 image_view=DestroyCacheView(image_view);
5381 if (status == MagickFalse)
5382 tint_image=DestroyImage(tint_image);
5383 return(tint_image);
5384}
5385
5386/*
5387%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5388% %
5389% %
5390% %
5391% V i g n e t t e I m a g e %
5392% %
5393% %
5394% %
5395%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5396%
5397% VignetteImage() softens the edges of the image in vignette style.
5398%
5399% The format of the VignetteImage method is:
5400%
5401% Image *VignetteImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00005402% const double sigma,const ssize_t x,const ssize_t y,
cristy05c0c9a2011-09-05 23:16:13 +00005403% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005404%
5405% A description of each parameter follows:
5406%
5407% o image: the image.
5408%
5409% o radius: the radius of the pixel neighborhood.
5410%
5411% o sigma: the standard deviation of the Gaussian, in pixels.
5412%
5413% o x, y: Define the x and y ellipse offset.
5414%
5415% o exception: return any errors or warnings in this structure.
5416%
5417*/
5418MagickExport Image *VignetteImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00005419 const double sigma,const ssize_t x,const ssize_t y,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005420{
5421 char
5422 ellipse[MaxTextExtent];
5423
5424 DrawInfo
5425 *draw_info;
5426
5427 Image
5428 *canvas_image,
5429 *blur_image,
5430 *oval_image,
5431 *vignette_image;
5432
5433 assert(image != (Image *) NULL);
5434 assert(image->signature == MagickSignature);
5435 if (image->debug != MagickFalse)
5436 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5437 assert(exception != (ExceptionInfo *) NULL);
5438 assert(exception->signature == MagickSignature);
5439 canvas_image=CloneImage(image,0,0,MagickTrue,exception);
5440 if (canvas_image == (Image *) NULL)
5441 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005442 if (SetImageStorageClass(canvas_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005443 {
cristy3ed852e2009-09-05 21:47:34 +00005444 canvas_image=DestroyImage(canvas_image);
5445 return((Image *) NULL);
5446 }
5447 canvas_image->matte=MagickTrue;
cristy98621462011-12-31 22:31:11 +00005448 oval_image=CloneImage(canvas_image,canvas_image->columns,canvas_image->rows,
5449 MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005450 if (oval_image == (Image *) NULL)
5451 {
5452 canvas_image=DestroyImage(canvas_image);
5453 return((Image *) NULL);
5454 }
cristy9950d572011-10-01 18:22:35 +00005455 (void) QueryColorCompliance("#000000",AllCompliance,
5456 &oval_image->background_color,exception);
cristyea1a8aa2011-10-20 13:24:06 +00005457 (void) SetImageBackgroundColor(oval_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00005458 draw_info=CloneDrawInfo((const ImageInfo *) NULL,(const DrawInfo *) NULL);
cristy9950d572011-10-01 18:22:35 +00005459 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->fill,
5460 exception);
5461 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->stroke,
5462 exception);
cristy1707c6c2012-01-18 23:30:54 +00005463 (void) FormatLocaleString(ellipse,MaxTextExtent,"ellipse %g,%g,%g,%g,"
5464 "0.0,360.0",image->columns/2.0,image->rows/2.0,image->columns/2.0-x,
5465 image->rows/2.0-y);
cristy3ed852e2009-09-05 21:47:34 +00005466 draw_info->primitive=AcquireString(ellipse);
cristy018f07f2011-09-04 21:15:19 +00005467 (void) DrawImage(oval_image,draw_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00005468 draw_info=DestroyDrawInfo(draw_info);
cristyaa2c16c2012-03-25 22:21:35 +00005469 blur_image=BlurImage(oval_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +00005470 oval_image=DestroyImage(oval_image);
5471 if (blur_image == (Image *) NULL)
5472 {
5473 canvas_image=DestroyImage(canvas_image);
5474 return((Image *) NULL);
5475 }
5476 blur_image->matte=MagickFalse;
cristy39172402012-03-30 13:04:39 +00005477 (void) CompositeImage(canvas_image,blur_image,IntensityCompositeOp,MagickTrue,
5478 0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00005479 blur_image=DestroyImage(blur_image);
5480 vignette_image=MergeImageLayers(canvas_image,FlattenLayer,exception);
5481 canvas_image=DestroyImage(canvas_image);
5482 return(vignette_image);
5483}
5484
5485/*
5486%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5487% %
5488% %
5489% %
5490% W a v e I m a g e %
5491% %
5492% %
5493% %
5494%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5495%
5496% WaveImage() creates a "ripple" effect in the image by shifting the pixels
cristycee97112010-05-28 00:44:52 +00005497% vertically along a sine wave whose amplitude and wavelength is specified
cristy3ed852e2009-09-05 21:47:34 +00005498% by the given parameters.
5499%
5500% The format of the WaveImage method is:
5501%
5502% Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005503% const double wave_length,const PixelInterpolateMethod method,
5504% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005505%
5506% A description of each parameter follows:
5507%
5508% o image: the image.
5509%
5510% o amplitude, wave_length: Define the amplitude and wave length of the
5511% sine wave.
5512%
cristy5c4e2582011-09-11 19:21:03 +00005513% o interpolate: the pixel interpolation method.
5514%
cristy3ed852e2009-09-05 21:47:34 +00005515% o exception: return any errors or warnings in this structure.
5516%
5517*/
5518MagickExport Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005519 const double wave_length,const PixelInterpolateMethod method,
5520 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005521{
5522#define WaveImageTag "Wave/Image"
5523
cristyfa112112010-01-04 17:48:07 +00005524 CacheView
cristyd76c51e2011-03-26 00:21:26 +00005525 *image_view,
cristyfa112112010-01-04 17:48:07 +00005526 *wave_view;
5527
cristy3ed852e2009-09-05 21:47:34 +00005528 Image
5529 *wave_image;
5530
cristy3ed852e2009-09-05 21:47:34 +00005531 MagickBooleanType
5532 status;
5533
cristybb503372010-05-27 20:51:26 +00005534 MagickOffsetType
5535 progress;
5536
cristy3ed852e2009-09-05 21:47:34 +00005537 MagickRealType
5538 *sine_map;
5539
cristybb503372010-05-27 20:51:26 +00005540 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005541 i;
5542
cristybb503372010-05-27 20:51:26 +00005543 ssize_t
5544 y;
5545
cristy3ed852e2009-09-05 21:47:34 +00005546 /*
5547 Initialize wave image attributes.
5548 */
5549 assert(image != (Image *) NULL);
5550 assert(image->signature == MagickSignature);
5551 if (image->debug != MagickFalse)
5552 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5553 assert(exception != (ExceptionInfo *) NULL);
5554 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00005555 wave_image=CloneImage(image,image->columns,(size_t) (image->rows+2.0*
cristy3ed852e2009-09-05 21:47:34 +00005556 fabs(amplitude)),MagickTrue,exception);
5557 if (wave_image == (Image *) NULL)
5558 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005559 if (SetImageStorageClass(wave_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005560 {
cristy3ed852e2009-09-05 21:47:34 +00005561 wave_image=DestroyImage(wave_image);
5562 return((Image *) NULL);
5563 }
cristy4c08aed2011-07-01 19:47:50 +00005564 if (wave_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00005565 wave_image->matte=MagickTrue;
5566 /*
5567 Allocate sine map.
5568 */
5569 sine_map=(MagickRealType *) AcquireQuantumMemory((size_t) wave_image->columns,
5570 sizeof(*sine_map));
5571 if (sine_map == (MagickRealType *) NULL)
5572 {
5573 wave_image=DestroyImage(wave_image);
5574 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
5575 }
cristybb503372010-05-27 20:51:26 +00005576 for (i=0; i < (ssize_t) wave_image->columns; i++)
cristy4205a3c2010-09-12 20:19:59 +00005577 sine_map[i]=fabs(amplitude)+amplitude*sin((double) ((2.0*MagickPI*i)/
5578 wave_length));
cristy3ed852e2009-09-05 21:47:34 +00005579 /*
5580 Wave image.
5581 */
5582 status=MagickTrue;
5583 progress=0;
cristyd76c51e2011-03-26 00:21:26 +00005584 image_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00005585 wave_view=AcquireCacheView(wave_image);
cristyd76c51e2011-03-26 00:21:26 +00005586 (void) SetCacheViewVirtualPixelMethod(image_view,
5587 BackgroundVirtualPixelMethod);
cristyb5d5f722009-11-04 03:03:49 +00005588#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00005589 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00005590#endif
cristybb503372010-05-27 20:51:26 +00005591 for (y=0; y < (ssize_t) wave_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005592 {
cristy4c08aed2011-07-01 19:47:50 +00005593 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005594 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005595
cristye97bb922011-04-03 01:36:52 +00005596 register ssize_t
5597 x;
5598
cristy3ed852e2009-09-05 21:47:34 +00005599 if (status == MagickFalse)
5600 continue;
5601 q=QueueCacheViewAuthenticPixels(wave_view,0,y,wave_image->columns,1,
5602 exception);
cristyacd2ed22011-08-30 01:44:23 +00005603 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005604 {
5605 status=MagickFalse;
5606 continue;
5607 }
cristybb503372010-05-27 20:51:26 +00005608 for (x=0; x < (ssize_t) wave_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005609 {
cristy5c4e2582011-09-11 19:21:03 +00005610 status=InterpolatePixelChannels(image,image_view,wave_image,method,
5611 (double) x,(double) (y-sine_map[x]),q,exception);
cristyed231572011-07-14 02:18:59 +00005612 q+=GetPixelChannels(wave_image);
cristy3ed852e2009-09-05 21:47:34 +00005613 }
5614 if (SyncCacheViewAuthenticPixels(wave_view,exception) == MagickFalse)
5615 status=MagickFalse;
5616 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5617 {
5618 MagickBooleanType
5619 proceed;
5620
cristyb5d5f722009-11-04 03:03:49 +00005621#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005622 #pragma omp critical (MagickCore_WaveImage)
cristy3ed852e2009-09-05 21:47:34 +00005623#endif
5624 proceed=SetImageProgress(image,WaveImageTag,progress++,image->rows);
5625 if (proceed == MagickFalse)
5626 status=MagickFalse;
5627 }
5628 }
5629 wave_view=DestroyCacheView(wave_view);
cristyd76c51e2011-03-26 00:21:26 +00005630 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00005631 sine_map=(MagickRealType *) RelinquishMagickMemory(sine_map);
5632 if (status == MagickFalse)
5633 wave_image=DestroyImage(wave_image);
5634 return(wave_image);
5635}