blob: 46875b6be541a6f5612a40874af208ff7edf5f2f [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"
cristy20c3aed2012-04-10 01:06:21 +0000646#define Colorize(pixel,blend_percentage,colorize) \
647 (pixel)=((pixel)*(100.0-(blend_percentage))+(colorize)*(blend_percentage))/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
cristy20c3aed2012-04-10 01:06:21 +0000669 blend_percentage;
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);
cristy768165d2012-04-09 15:01:35 +0000683 colorize_image=CloneImage(image,image->columns,image->rows,MagickTrue,
684 exception);
685 if (colorize_image == (Image *) NULL)
686 return((Image *) NULL);
687 if (SetImageStorageClass(colorize_image,DirectClass,exception) == MagickFalse)
688 {
689 colorize_image=DestroyImage(colorize_image);
690 return((Image *) NULL);
691 }
cristy9b7a4fc2012-04-08 22:26:56 +0000692 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
cristy20c3aed2012-04-10 01:06:21 +0000693 (IsPixelInfoGray(colorize) != MagickFalse))
cristy52f2e1e2012-04-10 00:51:19 +0000694 (void) SetImageColorspace(colorize_image,sRGBColorspace,exception);
cristy20c3aed2012-04-10 01:06:21 +0000695 if ((colorize_image->matte == MagickFalse) &&
696 (colorize->matte != MagickFalse))
cristy768165d2012-04-09 15:01:35 +0000697 (void) SetImageAlpha(colorize_image,OpaqueAlpha,exception);
698 if (blend == (const char *) NULL)
699 return(colorize_image);
cristy20c3aed2012-04-10 01:06:21 +0000700 GetPixelInfo(image,&blend_percentage);
701 flags=ParseGeometry(blend,&geometry_info);
702 blend_percentage.red=geometry_info.rho;
703 blend_percentage.green=geometry_info.rho;
704 blend_percentage.blue=geometry_info.rho;
705 blend_percentage.black=geometry_info.rho;
706 blend_percentage.alpha=100.0;
707 if ((flags & SigmaValue) != 0)
708 blend_percentage.green=geometry_info.sigma;
709 if ((flags & XiValue) != 0)
710 blend_percentage.blue=geometry_info.xi;
711 if ((flags & PsiValue) != 0)
712 blend_percentage.alpha=geometry_info.psi;
713 if (blend_percentage.colorspace == CMYKColorspace)
714 {
715 if ((flags & PsiValue) != 0)
716 blend_percentage.black=geometry_info.psi;
717 if ((flags & ChiValue) != 0)
718 blend_percentage.alpha=geometry_info.chi;
719 }
cristy3ed852e2009-09-05 21:47:34 +0000720 /*
721 Colorize DirectClass image.
722 */
723 status=MagickTrue;
724 progress=0;
725 image_view=AcquireCacheView(image);
726 colorize_view=AcquireCacheView(colorize_image);
cristy319a1e72010-02-21 15:13:11 +0000727#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000728 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000729#endif
cristybb503372010-05-27 20:51:26 +0000730 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000731 {
732 MagickBooleanType
733 sync;
734
cristyf9bf43e2012-04-07 18:13:07 +0000735 PixelInfo
736 pixel;
737
cristy4c08aed2011-07-01 19:47:50 +0000738 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000739 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000740
cristybb503372010-05-27 20:51:26 +0000741 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000742 x;
743
cristy4c08aed2011-07-01 19:47:50 +0000744 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000745 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000746
747 if (status == MagickFalse)
748 continue;
749 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
750 q=QueueCacheViewAuthenticPixels(colorize_view,0,y,colorize_image->columns,1,
751 exception);
cristy4c08aed2011-07-01 19:47:50 +0000752 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000753 {
754 status=MagickFalse;
755 continue;
756 }
cristyf9bf43e2012-04-07 18:13:07 +0000757 GetPixelInfo(image,&pixel);
cristybb503372010-05-27 20:51:26 +0000758 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000759 {
cristyd6803382012-04-10 01:41:25 +0000760 if (GetPixelMask(colorize_image,q) != 0)
761 {
762 p+=GetPixelChannels(image);
763 q+=GetPixelChannels(colorize_image);
764 continue;
765 }
cristyf9bf43e2012-04-07 18:13:07 +0000766 GetPixelInfoPixel(image,p,&pixel);
cristy20c3aed2012-04-10 01:06:21 +0000767 Colorize(pixel.red,blend_percentage.red,colorize->red);
768 Colorize(pixel.green,blend_percentage.green,colorize->green);
769 Colorize(pixel.blue,blend_percentage.blue,colorize->blue);
770 Colorize(pixel.black,blend_percentage.black,colorize->black);
771 Colorize(pixel.alpha,blend_percentage.alpha,colorize->alpha);
cristyf9bf43e2012-04-07 18:13:07 +0000772 SetPixelInfoPixel(colorize_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +0000773 p+=GetPixelChannels(image);
774 q+=GetPixelChannels(colorize_image);
cristy3ed852e2009-09-05 21:47:34 +0000775 }
776 sync=SyncCacheViewAuthenticPixels(colorize_view,exception);
777 if (sync == MagickFalse)
778 status=MagickFalse;
779 if (image->progress_monitor != (MagickProgressMonitor) NULL)
780 {
781 MagickBooleanType
782 proceed;
783
cristy319a1e72010-02-21 15:13:11 +0000784#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +0000785 #pragma omp critical (MagickCore_ColorizeImage)
cristy3ed852e2009-09-05 21:47:34 +0000786#endif
787 proceed=SetImageProgress(image,ColorizeImageTag,progress++,image->rows);
788 if (proceed == MagickFalse)
789 status=MagickFalse;
790 }
791 }
792 image_view=DestroyCacheView(image_view);
793 colorize_view=DestroyCacheView(colorize_view);
794 if (status == MagickFalse)
795 colorize_image=DestroyImage(colorize_image);
796 return(colorize_image);
797}
798
799/*
800%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
801% %
802% %
803% %
cristye6365592010-04-02 17:31:23 +0000804% C o l o r M a t r i x I m a g e %
805% %
806% %
807% %
808%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
809%
810% ColorMatrixImage() applies color transformation to an image. This method
811% permits saturation changes, hue rotation, luminance to alpha, and various
812% other effects. Although variable-sized transformation matrices can be used,
813% typically one uses a 5x5 matrix for an RGBA image and a 6x6 for CMYKA
814% (or RGBA with offsets). The matrix is similar to those used by Adobe Flash
815% except offsets are in column 6 rather than 5 (in support of CMYKA images)
816% and offsets are normalized (divide Flash offset by 255).
817%
818% The format of the ColorMatrixImage method is:
819%
820% Image *ColorMatrixImage(const Image *image,
821% const KernelInfo *color_matrix,ExceptionInfo *exception)
822%
823% A description of each parameter follows:
824%
825% o image: the image.
826%
827% o color_matrix: the color matrix.
828%
829% o exception: return any errors or warnings in this structure.
830%
831*/
anthonyfd706f92012-01-19 04:22:02 +0000832/* FUTURE: modify to make use of a MagickMatrix Mutliply function
833 That should be provided in "matrix.c"
834 (ASIDE: actually distorts should do this too but currently doesn't)
835*/
836
cristye6365592010-04-02 17:31:23 +0000837MagickExport Image *ColorMatrixImage(const Image *image,
838 const KernelInfo *color_matrix,ExceptionInfo *exception)
839{
840#define ColorMatrixImageTag "ColorMatrix/Image"
841
842 CacheView
843 *color_view,
844 *image_view;
845
846 double
847 ColorMatrix[6][6] =
848 {
849 { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
850 { 0.0, 1.0, 0.0, 0.0, 0.0, 0.0 },
851 { 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 },
852 { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0 },
853 { 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 },
854 { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 }
855 };
856
857 Image
858 *color_image;
859
cristye6365592010-04-02 17:31:23 +0000860 MagickBooleanType
861 status;
862
cristybb503372010-05-27 20:51:26 +0000863 MagickOffsetType
864 progress;
865
866 register ssize_t
cristye6365592010-04-02 17:31:23 +0000867 i;
868
cristybb503372010-05-27 20:51:26 +0000869 ssize_t
870 u,
871 v,
872 y;
873
cristye6365592010-04-02 17:31:23 +0000874 /*
anthonyfd706f92012-01-19 04:22:02 +0000875 Map given color_matrix, into a 6x6 matrix RGBKA and a constant
cristye6365592010-04-02 17:31:23 +0000876 */
877 assert(image != (Image *) NULL);
878 assert(image->signature == MagickSignature);
879 if (image->debug != MagickFalse)
880 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
881 assert(exception != (ExceptionInfo *) NULL);
882 assert(exception->signature == MagickSignature);
883 i=0;
cristybb503372010-05-27 20:51:26 +0000884 for (v=0; v < (ssize_t) color_matrix->height; v++)
885 for (u=0; u < (ssize_t) color_matrix->width; u++)
cristye6365592010-04-02 17:31:23 +0000886 {
887 if ((v < 6) && (u < 6))
888 ColorMatrix[v][u]=color_matrix->values[i];
889 i++;
890 }
891 /*
892 Initialize color image.
893 */
cristy12550e62010-06-07 12:46:40 +0000894 color_image=CloneImage(image,0,0,MagickTrue,exception);
cristye6365592010-04-02 17:31:23 +0000895 if (color_image == (Image *) NULL)
896 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000897 if (SetImageStorageClass(color_image,DirectClass,exception) == MagickFalse)
cristye6365592010-04-02 17:31:23 +0000898 {
cristye6365592010-04-02 17:31:23 +0000899 color_image=DestroyImage(color_image);
900 return((Image *) NULL);
901 }
902 if (image->debug != MagickFalse)
903 {
904 char
905 format[MaxTextExtent],
906 *message;
907
908 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
909 " ColorMatrix image with color matrix:");
910 message=AcquireString("");
911 for (v=0; v < 6; v++)
912 {
913 *message='\0';
cristyb51dff52011-05-19 16:55:47 +0000914 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
cristye6365592010-04-02 17:31:23 +0000915 (void) ConcatenateString(&message,format);
916 for (u=0; u < 6; u++)
917 {
cristyb51dff52011-05-19 16:55:47 +0000918 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",
cristye6365592010-04-02 17:31:23 +0000919 ColorMatrix[v][u]);
920 (void) ConcatenateString(&message,format);
921 }
922 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
923 }
924 message=DestroyString(message);
925 }
926 /*
anthonyfd706f92012-01-19 04:22:02 +0000927 Apply the ColorMatrix to image.
cristye6365592010-04-02 17:31:23 +0000928 */
929 status=MagickTrue;
930 progress=0;
931 image_view=AcquireCacheView(image);
932 color_view=AcquireCacheView(color_image);
933#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000934 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristye6365592010-04-02 17:31:23 +0000935#endif
cristybb503372010-05-27 20:51:26 +0000936 for (y=0; y < (ssize_t) image->rows; y++)
cristye6365592010-04-02 17:31:23 +0000937 {
cristyfcc25d92012-02-19 23:06:48 +0000938 PixelInfo
cristye6365592010-04-02 17:31:23 +0000939 pixel;
940
cristy4c08aed2011-07-01 19:47:50 +0000941 register const Quantum
cristye6365592010-04-02 17:31:23 +0000942 *restrict p;
943
cristy4c08aed2011-07-01 19:47:50 +0000944 register Quantum
945 *restrict q;
946
cristybb503372010-05-27 20:51:26 +0000947 register ssize_t
cristye6365592010-04-02 17:31:23 +0000948 x;
949
cristye6365592010-04-02 17:31:23 +0000950 if (status == MagickFalse)
951 continue;
952 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
953 q=GetCacheViewAuthenticPixels(color_view,0,y,color_image->columns,1,
954 exception);
cristy4c08aed2011-07-01 19:47:50 +0000955 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristye6365592010-04-02 17:31:23 +0000956 {
957 status=MagickFalse;
958 continue;
959 }
cristyfcc25d92012-02-19 23:06:48 +0000960 GetPixelInfo(image,&pixel);
cristybb503372010-05-27 20:51:26 +0000961 for (x=0; x < (ssize_t) image->columns; x++)
cristye6365592010-04-02 17:31:23 +0000962 {
cristybb503372010-05-27 20:51:26 +0000963 register ssize_t
cristye6365592010-04-02 17:31:23 +0000964 v;
965
cristybb503372010-05-27 20:51:26 +0000966 size_t
cristye6365592010-04-02 17:31:23 +0000967 height;
968
cristyfcc25d92012-02-19 23:06:48 +0000969 GetPixelInfoPixel(image,p,&pixel);
cristye6365592010-04-02 17:31:23 +0000970 height=color_matrix->height > 6 ? 6UL : color_matrix->height;
cristybb503372010-05-27 20:51:26 +0000971 for (v=0; v < (ssize_t) height; v++)
cristye6365592010-04-02 17:31:23 +0000972 {
cristyfcc25d92012-02-19 23:06:48 +0000973 MagickRealType
974 sum;
975
976 sum=ColorMatrix[v][0]*GetPixelRed(image,p)+ColorMatrix[v][1]*
cristy4c08aed2011-07-01 19:47:50 +0000977 GetPixelGreen(image,p)+ColorMatrix[v][2]*GetPixelBlue(image,p);
cristye6365592010-04-02 17:31:23 +0000978 if (image->colorspace == CMYKColorspace)
cristyfcc25d92012-02-19 23:06:48 +0000979 sum+=ColorMatrix[v][3]*GetPixelBlack(image,p);
cristy4c08aed2011-07-01 19:47:50 +0000980 if (image->matte != MagickFalse)
cristyfcc25d92012-02-19 23:06:48 +0000981 sum+=ColorMatrix[v][4]*GetPixelAlpha(image,p);
982 sum+=QuantumRange*ColorMatrix[v][5];
cristye6365592010-04-02 17:31:23 +0000983 switch (v)
984 {
cristyfcc25d92012-02-19 23:06:48 +0000985 case 0: pixel.red=sum; break;
986 case 1: pixel.green=sum; break;
987 case 2: pixel.blue=sum; break;
988 case 3: pixel.black=sum; break;
989 case 4: pixel.alpha=sum; break;
990 default: break;
cristye6365592010-04-02 17:31:23 +0000991 }
992 }
cristyfcc25d92012-02-19 23:06:48 +0000993 SetPixelInfoPixel(color_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +0000994 p+=GetPixelChannels(image);
995 q+=GetPixelChannels(color_image);
cristye6365592010-04-02 17:31:23 +0000996 }
997 if (SyncCacheViewAuthenticPixels(color_view,exception) == MagickFalse)
998 status=MagickFalse;
999 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1000 {
1001 MagickBooleanType
1002 proceed;
1003
1004#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00001005 #pragma omp critical (MagickCore_ColorMatrixImage)
cristye6365592010-04-02 17:31:23 +00001006#endif
1007 proceed=SetImageProgress(image,ColorMatrixImageTag,progress++,
1008 image->rows);
1009 if (proceed == MagickFalse)
1010 status=MagickFalse;
1011 }
1012 }
1013 color_view=DestroyCacheView(color_view);
1014 image_view=DestroyCacheView(image_view);
1015 if (status == MagickFalse)
1016 color_image=DestroyImage(color_image);
1017 return(color_image);
1018}
1019
1020/*
1021%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1022% %
1023% %
1024% %
cristy3ed852e2009-09-05 21:47:34 +00001025+ D e s t r o y F x I n f o %
1026% %
1027% %
1028% %
1029%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1030%
1031% DestroyFxInfo() deallocates memory associated with an FxInfo structure.
1032%
1033% The format of the DestroyFxInfo method is:
1034%
1035% ImageInfo *DestroyFxInfo(ImageInfo *fx_info)
1036%
1037% A description of each parameter follows:
1038%
1039% o fx_info: the fx info.
1040%
1041*/
cristy7832dc22011-09-05 01:21:53 +00001042MagickPrivate FxInfo *DestroyFxInfo(FxInfo *fx_info)
cristy3ed852e2009-09-05 21:47:34 +00001043{
cristybb503372010-05-27 20:51:26 +00001044 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001045 i;
1046
1047 fx_info->exception=DestroyExceptionInfo(fx_info->exception);
1048 fx_info->expression=DestroyString(fx_info->expression);
1049 fx_info->symbols=DestroySplayTree(fx_info->symbols);
1050 fx_info->colors=DestroySplayTree(fx_info->colors);
cristy0ea377f2011-03-24 00:54:19 +00001051 for (i=(ssize_t) GetImageListLength(fx_info->images)-1; i >= 0; i--)
cristyd76c51e2011-03-26 00:21:26 +00001052 fx_info->view[i]=DestroyCacheView(fx_info->view[i]);
1053 fx_info->view=(CacheView **) RelinquishMagickMemory(fx_info->view);
cristy3ed852e2009-09-05 21:47:34 +00001054 fx_info->random_info=DestroyRandomInfo(fx_info->random_info);
1055 fx_info=(FxInfo *) RelinquishMagickMemory(fx_info);
1056 return(fx_info);
1057}
1058
1059/*
1060%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1061% %
1062% %
1063% %
cristy3ed852e2009-09-05 21:47:34 +00001064+ 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 %
1065% %
1066% %
1067% %
1068%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1069%
1070% FxEvaluateChannelExpression() evaluates an expression and returns the
1071% results.
1072%
1073% The format of the FxEvaluateExpression method is:
1074%
1075% MagickRealType FxEvaluateChannelExpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00001076% const PixelChannel channel,const ssize_t x,const ssize_t y,
cristy3ed852e2009-09-05 21:47:34 +00001077% MagickRealType *alpha,Exceptioninfo *exception)
1078% MagickRealType FxEvaluateExpression(FxInfo *fx_info,
1079% MagickRealType *alpha,Exceptioninfo *exception)
1080%
1081% A description of each parameter follows:
1082%
1083% o fx_info: the fx info.
1084%
1085% o channel: the channel.
1086%
1087% o x,y: the pixel position.
1088%
1089% o alpha: the result.
1090%
1091% o exception: return any errors or warnings in this structure.
1092%
1093*/
1094
cristy351842f2010-03-07 15:27:38 +00001095static inline double MagickMax(const double x,const double y)
1096{
1097 if (x > y)
1098 return(x);
1099 return(y);
1100}
1101
1102static inline double MagickMin(const double x,const double y)
1103{
1104 if (x < y)
1105 return(x);
1106 return(y);
1107}
1108
cristy3ed852e2009-09-05 21:47:34 +00001109static MagickRealType FxChannelStatistics(FxInfo *fx_info,const Image *image,
cristy0568ffc2011-07-25 16:54:14 +00001110 PixelChannel channel,const char *symbol,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001111{
1112 char
1113 key[MaxTextExtent],
1114 statistic[MaxTextExtent];
1115
1116 const char
1117 *value;
1118
1119 register const char
1120 *p;
1121
1122 for (p=symbol; (*p != '.') && (*p != '\0'); p++) ;
1123 if (*p == '.')
1124 switch (*++p) /* e.g. depth.r */
1125 {
cristy541ae572011-08-05 19:08:59 +00001126 case 'r': channel=RedPixelChannel; break;
1127 case 'g': channel=GreenPixelChannel; break;
1128 case 'b': channel=BluePixelChannel; break;
1129 case 'c': channel=CyanPixelChannel; break;
1130 case 'm': channel=MagentaPixelChannel; break;
1131 case 'y': channel=YellowPixelChannel; break;
1132 case 'k': channel=BlackPixelChannel; break;
cristy3ed852e2009-09-05 21:47:34 +00001133 default: break;
1134 }
cristyb51dff52011-05-19 16:55:47 +00001135 (void) FormatLocaleString(key,MaxTextExtent,"%p.%.20g.%s",(void *) image,
cristye8c25f92010-06-03 00:53:06 +00001136 (double) channel,symbol);
cristy3ed852e2009-09-05 21:47:34 +00001137 value=(const char *) GetValueFromSplayTree(fx_info->symbols,key);
1138 if (value != (const char *) NULL)
cristydbdd0e32011-11-04 23:29:40 +00001139 return(QuantumScale*StringToDouble(value,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001140 (void) DeleteNodeFromSplayTree(fx_info->symbols,key);
1141 if (LocaleNCompare(symbol,"depth",5) == 0)
1142 {
cristybb503372010-05-27 20:51:26 +00001143 size_t
cristy3ed852e2009-09-05 21:47:34 +00001144 depth;
1145
cristyfefab1b2011-07-05 00:33:22 +00001146 depth=GetImageDepth(image,exception);
1147 (void) FormatLocaleString(statistic,MaxTextExtent,"%.20g",(double) depth);
cristy3ed852e2009-09-05 21:47:34 +00001148 }
1149 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1150 {
1151 double
1152 kurtosis,
1153 skewness;
1154
cristyd42d9952011-07-08 14:21:50 +00001155 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
cristyb51dff52011-05-19 16:55:47 +00001156 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",kurtosis);
cristy3ed852e2009-09-05 21:47:34 +00001157 }
1158 if (LocaleNCompare(symbol,"maxima",6) == 0)
1159 {
1160 double
1161 maxima,
1162 minima;
1163
cristyd42d9952011-07-08 14:21:50 +00001164 (void) GetImageRange(image,&minima,&maxima,exception);
cristyb51dff52011-05-19 16:55:47 +00001165 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",maxima);
cristy3ed852e2009-09-05 21:47:34 +00001166 }
1167 if (LocaleNCompare(symbol,"mean",4) == 0)
1168 {
1169 double
1170 mean,
1171 standard_deviation;
1172
cristyd42d9952011-07-08 14:21:50 +00001173 (void) GetImageMean(image,&mean,&standard_deviation,exception);
cristyb51dff52011-05-19 16:55:47 +00001174 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",mean);
cristy3ed852e2009-09-05 21:47:34 +00001175 }
1176 if (LocaleNCompare(symbol,"minima",6) == 0)
1177 {
1178 double
1179 maxima,
1180 minima;
1181
cristyd42d9952011-07-08 14:21:50 +00001182 (void) GetImageRange(image,&minima,&maxima,exception);
cristyb51dff52011-05-19 16:55:47 +00001183 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",minima);
cristy3ed852e2009-09-05 21:47:34 +00001184 }
1185 if (LocaleNCompare(symbol,"skewness",8) == 0)
1186 {
1187 double
1188 kurtosis,
1189 skewness;
1190
cristyd42d9952011-07-08 14:21:50 +00001191 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
cristyb51dff52011-05-19 16:55:47 +00001192 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",skewness);
cristy3ed852e2009-09-05 21:47:34 +00001193 }
1194 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1195 {
1196 double
1197 mean,
1198 standard_deviation;
1199
cristyd42d9952011-07-08 14:21:50 +00001200 (void) GetImageMean(image,&mean,&standard_deviation,exception);
cristyb51dff52011-05-19 16:55:47 +00001201 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",
cristy3ed852e2009-09-05 21:47:34 +00001202 standard_deviation);
1203 }
1204 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(key),
1205 ConstantString(statistic));
cristydbdd0e32011-11-04 23:29:40 +00001206 return(QuantumScale*StringToDouble(statistic,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001207}
1208
1209static MagickRealType
cristy0568ffc2011-07-25 16:54:14 +00001210 FxEvaluateSubexpression(FxInfo *,const PixelChannel,const ssize_t,
cristye85007d2010-06-06 22:51:36 +00001211 const ssize_t,const char *,MagickRealType *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +00001212
cristyb0aad4c2011-11-02 19:30:35 +00001213static MagickOffsetType FxGCD(MagickOffsetType alpha,MagickOffsetType beta)
1214{
1215 if (beta != 0)
1216 return(FxGCD(beta,alpha % beta));
1217 return(alpha);
1218}
1219
cristy3ed852e2009-09-05 21:47:34 +00001220static inline const char *FxSubexpression(const char *expression,
1221 ExceptionInfo *exception)
1222{
1223 const char
1224 *subexpression;
1225
cristybb503372010-05-27 20:51:26 +00001226 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001227 level;
1228
1229 level=0;
1230 subexpression=expression;
1231 while ((*subexpression != '\0') &&
1232 ((level != 1) || (strchr(")",(int) *subexpression) == (char *) NULL)))
1233 {
1234 if (strchr("(",(int) *subexpression) != (char *) NULL)
1235 level++;
1236 else
1237 if (strchr(")",(int) *subexpression) != (char *) NULL)
1238 level--;
1239 subexpression++;
1240 }
1241 if (*subexpression == '\0')
1242 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1243 "UnbalancedParenthesis","`%s'",expression);
1244 return(subexpression);
1245}
1246
cristy0568ffc2011-07-25 16:54:14 +00001247static MagickRealType FxGetSymbol(FxInfo *fx_info,const PixelChannel channel,
cristye85007d2010-06-06 22:51:36 +00001248 const ssize_t x,const ssize_t y,const char *expression,
1249 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001250{
1251 char
1252 *q,
1253 subexpression[MaxTextExtent],
1254 symbol[MaxTextExtent];
1255
1256 const char
1257 *p,
1258 *value;
1259
1260 Image
1261 *image;
1262
cristy4c08aed2011-07-01 19:47:50 +00001263 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001264 pixel;
1265
1266 MagickRealType
1267 alpha,
1268 beta;
1269
1270 PointInfo
1271 point;
1272
cristybb503372010-05-27 20:51:26 +00001273 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001274 i;
1275
1276 size_t
cristy1707c6c2012-01-18 23:30:54 +00001277 length,
cristy3ed852e2009-09-05 21:47:34 +00001278 level;
1279
1280 p=expression;
1281 i=GetImageIndexInList(fx_info->images);
1282 level=0;
1283 point.x=(double) x;
1284 point.y=(double) y;
1285 if (isalpha((int) *(p+1)) == 0)
1286 {
1287 if (strchr("suv",(int) *p) != (char *) NULL)
1288 {
1289 switch (*p)
1290 {
1291 case 's':
1292 default:
1293 {
1294 i=GetImageIndexInList(fx_info->images);
1295 break;
1296 }
1297 case 'u': i=0; break;
1298 case 'v': i=1; break;
1299 }
1300 p++;
1301 if (*p == '[')
1302 {
1303 level++;
1304 q=subexpression;
1305 for (p++; *p != '\0'; )
1306 {
1307 if (*p == '[')
1308 level++;
1309 else
1310 if (*p == ']')
1311 {
1312 level--;
1313 if (level == 0)
1314 break;
1315 }
1316 *q++=(*p++);
1317 }
1318 *q='\0';
1319 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1320 &beta,exception);
cristybb503372010-05-27 20:51:26 +00001321 i=(ssize_t) (alpha+0.5);
cristy3ed852e2009-09-05 21:47:34 +00001322 p++;
1323 }
1324 if (*p == '.')
1325 p++;
1326 }
1327 if ((isalpha((int) *(p+1)) == 0) && (*p == 'p'))
1328 {
1329 p++;
1330 if (*p == '{')
1331 {
1332 level++;
1333 q=subexpression;
1334 for (p++; *p != '\0'; )
1335 {
1336 if (*p == '{')
1337 level++;
1338 else
1339 if (*p == '}')
1340 {
1341 level--;
1342 if (level == 0)
1343 break;
1344 }
1345 *q++=(*p++);
1346 }
1347 *q='\0';
1348 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1349 &beta,exception);
1350 point.x=alpha;
1351 point.y=beta;
1352 p++;
1353 }
1354 else
1355 if (*p == '[')
1356 {
1357 level++;
1358 q=subexpression;
1359 for (p++; *p != '\0'; )
1360 {
1361 if (*p == '[')
1362 level++;
1363 else
1364 if (*p == ']')
1365 {
1366 level--;
1367 if (level == 0)
1368 break;
1369 }
1370 *q++=(*p++);
1371 }
1372 *q='\0';
1373 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1374 &beta,exception);
1375 point.x+=alpha;
1376 point.y+=beta;
1377 p++;
1378 }
1379 if (*p == '.')
1380 p++;
1381 }
1382 }
1383 length=GetImageListLength(fx_info->images);
1384 while (i < 0)
cristybb503372010-05-27 20:51:26 +00001385 i+=(ssize_t) length;
cristy3ed852e2009-09-05 21:47:34 +00001386 i%=length;
1387 image=GetImageFromList(fx_info->images,i);
1388 if (image == (Image *) NULL)
1389 {
1390 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1391 "NoSuchImage","`%s'",expression);
1392 return(0.0);
1393 }
cristy4c08aed2011-07-01 19:47:50 +00001394 GetPixelInfo(image,&pixel);
1395 (void) InterpolatePixelInfo(image,fx_info->view[i],image->interpolate,
cristy4f820712011-04-01 12:35:43 +00001396 point.x,point.y,&pixel,exception);
cristy1707c6c2012-01-18 23:30:54 +00001397 if ((strlen(p) > 2) && (LocaleCompare(p,"intensity") != 0) &&
1398 (LocaleCompare(p,"luminance") != 0) && (LocaleCompare(p,"hue") != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00001399 (LocaleCompare(p,"saturation") != 0) &&
1400 (LocaleCompare(p,"lightness") != 0))
1401 {
1402 char
1403 name[MaxTextExtent];
1404
1405 (void) CopyMagickString(name,p,MaxTextExtent);
1406 for (q=name+(strlen(name)-1); q > name; q--)
1407 {
1408 if (*q == ')')
1409 break;
1410 if (*q == '.')
1411 {
1412 *q='\0';
1413 break;
1414 }
1415 }
1416 if ((strlen(name) > 2) &&
1417 (GetValueFromSplayTree(fx_info->symbols,name) == (const char *) NULL))
1418 {
cristy4c08aed2011-07-01 19:47:50 +00001419 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001420 *color;
1421
cristy4c08aed2011-07-01 19:47:50 +00001422 color=(PixelInfo *) GetValueFromSplayTree(fx_info->colors,name);
1423 if (color != (PixelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001424 {
1425 pixel=(*color);
1426 p+=strlen(name);
1427 }
1428 else
cristy1707c6c2012-01-18 23:30:54 +00001429 {
1430 MagickBooleanType
1431 status;
1432
1433 status=QueryColorCompliance(name,AllCompliance,&pixel,
1434 fx_info->exception);
1435 if (status != MagickFalse)
1436 {
1437 (void) AddValueToSplayTree(fx_info->colors,ConstantString(
1438 name),ClonePixelInfo(&pixel));
1439 p+=strlen(name);
1440 }
1441 }
cristy3ed852e2009-09-05 21:47:34 +00001442 }
1443 }
1444 (void) CopyMagickString(symbol,p,MaxTextExtent);
1445 StripString(symbol);
1446 if (*symbol == '\0')
1447 {
1448 switch (channel)
1449 {
cristy0568ffc2011-07-25 16:54:14 +00001450 case RedPixelChannel: return(QuantumScale*pixel.red);
1451 case GreenPixelChannel: return(QuantumScale*pixel.green);
1452 case BluePixelChannel: return(QuantumScale*pixel.blue);
1453 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001454 {
1455 if (image->colorspace != CMYKColorspace)
1456 {
1457 (void) ThrowMagickException(exception,GetMagickModule(),
cristy1a020e42011-12-06 18:13:23 +00001458 ImageError,"ColorSeparatedImageRequired","`%s'",
cristy3ed852e2009-09-05 21:47:34 +00001459 image->filename);
1460 return(0.0);
1461 }
cristy4c08aed2011-07-01 19:47:50 +00001462 return(QuantumScale*pixel.black);
1463 }
cristy0568ffc2011-07-25 16:54:14 +00001464 case AlphaPixelChannel:
cristy4c08aed2011-07-01 19:47:50 +00001465 {
1466 MagickRealType
1467 alpha;
1468
1469 if (pixel.matte == MagickFalse)
1470 return(1.0);
1471 alpha=(MagickRealType) (QuantumScale*pixel.alpha);
1472 return(alpha);
cristy3ed852e2009-09-05 21:47:34 +00001473 }
cristya382aca2011-12-06 18:22:48 +00001474 case IndexPixelChannel:
1475 return(0.0);
cristyb3a73b52011-07-26 01:34:43 +00001476 case IntensityPixelChannel:
cristyf364ed42010-12-15 01:54:43 +00001477 {
cristy4c08aed2011-07-01 19:47:50 +00001478 return(QuantumScale*GetPixelInfoIntensity(&pixel));
cristyf364ed42010-12-15 01:54:43 +00001479 }
cristy3ed852e2009-09-05 21:47:34 +00001480 default:
1481 break;
1482 }
1483 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1484 "UnableToParseExpression","`%s'",p);
1485 return(0.0);
1486 }
1487 switch (*symbol)
1488 {
1489 case 'A':
1490 case 'a':
1491 {
1492 if (LocaleCompare(symbol,"a") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001493 return((MagickRealType) (QuantumScale*pixel.alpha));
cristy3ed852e2009-09-05 21:47:34 +00001494 break;
1495 }
1496 case 'B':
1497 case 'b':
1498 {
1499 if (LocaleCompare(symbol,"b") == 0)
1500 return(QuantumScale*pixel.blue);
1501 break;
1502 }
1503 case 'C':
1504 case 'c':
1505 {
1506 if (LocaleNCompare(symbol,"channel",7) == 0)
1507 {
1508 GeometryInfo
1509 channel_info;
1510
1511 MagickStatusType
1512 flags;
1513
1514 flags=ParseGeometry(symbol+7,&channel_info);
1515 if (image->colorspace == CMYKColorspace)
1516 switch (channel)
1517 {
cristy0568ffc2011-07-25 16:54:14 +00001518 case CyanPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001519 {
1520 if ((flags & RhoValue) == 0)
1521 return(0.0);
1522 return(channel_info.rho);
1523 }
cristy0568ffc2011-07-25 16:54:14 +00001524 case MagentaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001525 {
1526 if ((flags & SigmaValue) == 0)
1527 return(0.0);
1528 return(channel_info.sigma);
1529 }
cristy0568ffc2011-07-25 16:54:14 +00001530 case YellowPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001531 {
1532 if ((flags & XiValue) == 0)
1533 return(0.0);
1534 return(channel_info.xi);
1535 }
cristy0568ffc2011-07-25 16:54:14 +00001536 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001537 {
1538 if ((flags & PsiValue) == 0)
1539 return(0.0);
1540 return(channel_info.psi);
1541 }
cristy0568ffc2011-07-25 16:54:14 +00001542 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001543 {
1544 if ((flags & ChiValue) == 0)
1545 return(0.0);
1546 return(channel_info.chi);
1547 }
1548 default:
1549 return(0.0);
1550 }
1551 switch (channel)
1552 {
cristy0568ffc2011-07-25 16:54:14 +00001553 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001554 {
1555 if ((flags & RhoValue) == 0)
1556 return(0.0);
1557 return(channel_info.rho);
1558 }
cristy0568ffc2011-07-25 16:54:14 +00001559 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001560 {
1561 if ((flags & SigmaValue) == 0)
1562 return(0.0);
1563 return(channel_info.sigma);
1564 }
cristy0568ffc2011-07-25 16:54:14 +00001565 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001566 {
1567 if ((flags & XiValue) == 0)
1568 return(0.0);
1569 return(channel_info.xi);
1570 }
cristy0568ffc2011-07-25 16:54:14 +00001571 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001572 {
1573 if ((flags & ChiValue) == 0)
1574 return(0.0);
1575 return(channel_info.chi);
1576 }
cristy0568ffc2011-07-25 16:54:14 +00001577 case AlphaPixelChannel:
cristy4c08aed2011-07-01 19:47:50 +00001578 {
1579 if ((flags & PsiValue) == 0)
1580 return(0.0);
1581 return(channel_info.psi);
1582 }
cristy3ed852e2009-09-05 21:47:34 +00001583 default:
1584 return(0.0);
1585 }
1586 return(0.0);
1587 }
1588 if (LocaleCompare(symbol,"c") == 0)
1589 return(QuantumScale*pixel.red);
1590 break;
1591 }
1592 case 'D':
1593 case 'd':
1594 {
1595 if (LocaleNCompare(symbol,"depth",5) == 0)
1596 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1597 break;
1598 }
1599 case 'G':
1600 case 'g':
1601 {
1602 if (LocaleCompare(symbol,"g") == 0)
1603 return(QuantumScale*pixel.green);
1604 break;
1605 }
1606 case 'K':
1607 case 'k':
1608 {
1609 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1610 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1611 if (LocaleCompare(symbol,"k") == 0)
1612 {
1613 if (image->colorspace != CMYKColorspace)
1614 {
1615 (void) ThrowMagickException(exception,GetMagickModule(),
1616 OptionError,"ColorSeparatedImageRequired","`%s'",
1617 image->filename);
1618 return(0.0);
1619 }
cristy4c08aed2011-07-01 19:47:50 +00001620 return(QuantumScale*pixel.black);
cristy3ed852e2009-09-05 21:47:34 +00001621 }
1622 break;
1623 }
1624 case 'H':
1625 case 'h':
1626 {
1627 if (LocaleCompare(symbol,"h") == 0)
1628 return((MagickRealType) image->rows);
1629 if (LocaleCompare(symbol,"hue") == 0)
1630 {
1631 double
1632 hue,
1633 lightness,
1634 saturation;
1635
cristyda1f9c12011-10-02 21:39:49 +00001636 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1637 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001638 return(hue);
1639 }
1640 break;
1641 }
1642 case 'I':
1643 case 'i':
1644 {
1645 if ((LocaleCompare(symbol,"image.depth") == 0) ||
1646 (LocaleCompare(symbol,"image.minima") == 0) ||
1647 (LocaleCompare(symbol,"image.maxima") == 0) ||
1648 (LocaleCompare(symbol,"image.mean") == 0) ||
1649 (LocaleCompare(symbol,"image.kurtosis") == 0) ||
1650 (LocaleCompare(symbol,"image.skewness") == 0) ||
1651 (LocaleCompare(symbol,"image.standard_deviation") == 0))
1652 return(FxChannelStatistics(fx_info,image,channel,symbol+6,exception));
1653 if (LocaleCompare(symbol,"image.resolution.x") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001654 return(image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00001655 if (LocaleCompare(symbol,"image.resolution.y") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001656 return(image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001657 if (LocaleCompare(symbol,"intensity") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001658 return(QuantumScale*GetPixelInfoIntensity(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001659 if (LocaleCompare(symbol,"i") == 0)
1660 return((MagickRealType) x);
1661 break;
1662 }
1663 case 'J':
1664 case 'j':
1665 {
1666 if (LocaleCompare(symbol,"j") == 0)
1667 return((MagickRealType) y);
1668 break;
1669 }
1670 case 'L':
1671 case 'l':
1672 {
1673 if (LocaleCompare(symbol,"lightness") == 0)
1674 {
1675 double
1676 hue,
1677 lightness,
1678 saturation;
1679
cristyda1f9c12011-10-02 21:39:49 +00001680 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1681 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001682 return(lightness);
1683 }
1684 if (LocaleCompare(symbol,"luminance") == 0)
1685 {
1686 double
1687 luminence;
1688
1689 luminence=0.2126*pixel.red+0.7152*pixel.green+0.0722*pixel.blue;
1690 return(QuantumScale*luminence);
1691 }
1692 break;
1693 }
1694 case 'M':
1695 case 'm':
1696 {
1697 if (LocaleNCompare(symbol,"maxima",6) == 0)
1698 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1699 if (LocaleNCompare(symbol,"mean",4) == 0)
1700 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1701 if (LocaleNCompare(symbol,"minima",6) == 0)
1702 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1703 if (LocaleCompare(symbol,"m") == 0)
1704 return(QuantumScale*pixel.blue);
1705 break;
1706 }
1707 case 'N':
1708 case 'n':
1709 {
1710 if (LocaleCompare(symbol,"n") == 0)
anthony374f5dd2011-03-25 10:08:53 +00001711 return((MagickRealType) GetImageListLength(fx_info->images));
cristy3ed852e2009-09-05 21:47:34 +00001712 break;
1713 }
1714 case 'O':
1715 case 'o':
1716 {
1717 if (LocaleCompare(symbol,"o") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001718 return(QuantumScale*pixel.alpha);
cristy3ed852e2009-09-05 21:47:34 +00001719 break;
1720 }
1721 case 'P':
1722 case 'p':
1723 {
1724 if (LocaleCompare(symbol,"page.height") == 0)
1725 return((MagickRealType) image->page.height);
1726 if (LocaleCompare(symbol,"page.width") == 0)
1727 return((MagickRealType) image->page.width);
1728 if (LocaleCompare(symbol,"page.x") == 0)
1729 return((MagickRealType) image->page.x);
1730 if (LocaleCompare(symbol,"page.y") == 0)
1731 return((MagickRealType) image->page.y);
1732 break;
1733 }
1734 case 'R':
1735 case 'r':
1736 {
1737 if (LocaleCompare(symbol,"resolution.x") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001738 return(image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00001739 if (LocaleCompare(symbol,"resolution.y") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001740 return(image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001741 if (LocaleCompare(symbol,"r") == 0)
1742 return(QuantumScale*pixel.red);
1743 break;
1744 }
1745 case 'S':
1746 case 's':
1747 {
1748 if (LocaleCompare(symbol,"saturation") == 0)
1749 {
1750 double
1751 hue,
1752 lightness,
1753 saturation;
1754
cristyda1f9c12011-10-02 21:39:49 +00001755 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1756 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001757 return(saturation);
1758 }
1759 if (LocaleNCompare(symbol,"skewness",8) == 0)
1760 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1761 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1762 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1763 break;
1764 }
1765 case 'T':
1766 case 't':
1767 {
1768 if (LocaleCompare(symbol,"t") == 0)
cristy5a15b932011-03-26 12:50:33 +00001769 return((MagickRealType) GetImageIndexInList(fx_info->images));
cristy3ed852e2009-09-05 21:47:34 +00001770 break;
1771 }
1772 case 'W':
1773 case 'w':
1774 {
1775 if (LocaleCompare(symbol,"w") == 0)
1776 return((MagickRealType) image->columns);
1777 break;
1778 }
1779 case 'Y':
1780 case 'y':
1781 {
1782 if (LocaleCompare(symbol,"y") == 0)
1783 return(QuantumScale*pixel.green);
1784 break;
1785 }
1786 case 'Z':
1787 case 'z':
1788 {
1789 if (LocaleCompare(symbol,"z") == 0)
1790 {
1791 MagickRealType
1792 depth;
1793
cristyfefab1b2011-07-05 00:33:22 +00001794 depth=(MagickRealType) GetImageDepth(image,fx_info->exception);
cristy3ed852e2009-09-05 21:47:34 +00001795 return(depth);
1796 }
1797 break;
1798 }
1799 default:
1800 break;
1801 }
1802 value=(const char *) GetValueFromSplayTree(fx_info->symbols,symbol);
1803 if (value != (const char *) NULL)
cristydbdd0e32011-11-04 23:29:40 +00001804 return((MagickRealType) StringToDouble(value,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001805 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1806 "UnableToParseExpression","`%s'",symbol);
1807 return(0.0);
1808}
1809
1810static const char *FxOperatorPrecedence(const char *expression,
1811 ExceptionInfo *exception)
1812{
1813 typedef enum
1814 {
1815 UndefinedPrecedence,
1816 NullPrecedence,
1817 BitwiseComplementPrecedence,
1818 ExponentPrecedence,
cristy116af162010-08-13 01:25:47 +00001819 ExponentialNotationPrecedence,
cristy3ed852e2009-09-05 21:47:34 +00001820 MultiplyPrecedence,
1821 AdditionPrecedence,
1822 ShiftPrecedence,
1823 RelationalPrecedence,
1824 EquivalencyPrecedence,
1825 BitwiseAndPrecedence,
1826 BitwiseOrPrecedence,
1827 LogicalAndPrecedence,
1828 LogicalOrPrecedence,
1829 TernaryPrecedence,
1830 AssignmentPrecedence,
1831 CommaPrecedence,
1832 SeparatorPrecedence
1833 } FxPrecedence;
1834
1835 FxPrecedence
1836 precedence,
1837 target;
1838
1839 register const char
1840 *subexpression;
1841
1842 register int
1843 c;
1844
cristybb503372010-05-27 20:51:26 +00001845 size_t
cristy3ed852e2009-09-05 21:47:34 +00001846 level;
1847
1848 c=0;
1849 level=0;
1850 subexpression=(const char *) NULL;
1851 target=NullPrecedence;
1852 while (*expression != '\0')
1853 {
1854 precedence=UndefinedPrecedence;
1855 if ((isspace((int) ((char) *expression)) != 0) || (c == (int) '@'))
1856 {
1857 expression++;
1858 continue;
1859 }
cristy488fa882010-03-01 22:34:24 +00001860 switch (*expression)
1861 {
1862 case 'A':
1863 case 'a':
cristy3ed852e2009-09-05 21:47:34 +00001864 {
cristyb33454f2011-08-03 02:10:45 +00001865#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00001866 if (LocaleNCompare(expression,"acosh",5) == 0)
1867 {
1868 expression+=5;
1869 break;
1870 }
cristyb33454f2011-08-03 02:10:45 +00001871#endif
1872#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00001873 if (LocaleNCompare(expression,"asinh",5) == 0)
1874 {
1875 expression+=5;
1876 break;
1877 }
cristyb33454f2011-08-03 02:10:45 +00001878#endif
1879#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00001880 if (LocaleNCompare(expression,"atanh",5) == 0)
cristy488fa882010-03-01 22:34:24 +00001881 {
1882 expression+=5;
1883 break;
1884 }
cristyb33454f2011-08-03 02:10:45 +00001885#endif
cristy488fa882010-03-01 22:34:24 +00001886 break;
cristy3ed852e2009-09-05 21:47:34 +00001887 }
cristy62d455f2011-11-03 11:42:28 +00001888 case 'E':
1889 case 'e':
1890 {
1891 if ((LocaleNCompare(expression,"E+",2) == 0) ||
1892 (LocaleNCompare(expression,"E-",2) == 0))
1893 {
1894 expression+=2; /* scientific notation */
1895 break;
1896 }
1897 }
cristy488fa882010-03-01 22:34:24 +00001898 case 'J':
1899 case 'j':
1900 {
1901 if ((LocaleNCompare(expression,"j0",2) == 0) ||
1902 (LocaleNCompare(expression,"j1",2) == 0))
1903 {
1904 expression+=2;
1905 break;
1906 }
1907 break;
1908 }
cristy2def9322010-06-18 23:59:37 +00001909 case '#':
1910 {
1911 while (isxdigit((int) ((unsigned char) *(expression+1))) != 0)
1912 expression++;
1913 break;
1914 }
cristy488fa882010-03-01 22:34:24 +00001915 default:
1916 break;
1917 }
cristy3ed852e2009-09-05 21:47:34 +00001918 if ((c == (int) '{') || (c == (int) '['))
1919 level++;
1920 else
1921 if ((c == (int) '}') || (c == (int) ']'))
1922 level--;
1923 if (level == 0)
1924 switch ((unsigned char) *expression)
1925 {
1926 case '~':
1927 case '!':
1928 {
1929 precedence=BitwiseComplementPrecedence;
1930 break;
1931 }
1932 case '^':
cristy6621e252010-08-13 00:42:57 +00001933 case '@':
cristy3ed852e2009-09-05 21:47:34 +00001934 {
1935 precedence=ExponentPrecedence;
1936 break;
1937 }
1938 default:
1939 {
1940 if (((c != 0) && ((isdigit((int) ((char) c)) != 0) ||
1941 (strchr(")",c) != (char *) NULL))) &&
1942 (((islower((int) ((char) *expression)) != 0) ||
1943 (strchr("(",(int) *expression) != (char *) NULL)) ||
1944 ((isdigit((int) ((char) c)) == 0) &&
1945 (isdigit((int) ((char) *expression)) != 0))) &&
1946 (strchr("xy",(int) *expression) == (char *) NULL))
1947 precedence=MultiplyPrecedence;
1948 break;
1949 }
1950 case '*':
1951 case '/':
1952 case '%':
1953 {
1954 precedence=MultiplyPrecedence;
1955 break;
1956 }
1957 case '+':
1958 case '-':
1959 {
1960 if ((strchr("(+-/*%:&^|<>~,",c) == (char *) NULL) ||
1961 (isalpha(c) != 0))
1962 precedence=AdditionPrecedence;
1963 break;
1964 }
1965 case LeftShiftOperator:
1966 case RightShiftOperator:
1967 {
1968 precedence=ShiftPrecedence;
1969 break;
1970 }
1971 case '<':
1972 case LessThanEqualOperator:
1973 case GreaterThanEqualOperator:
1974 case '>':
1975 {
1976 precedence=RelationalPrecedence;
1977 break;
1978 }
1979 case EqualOperator:
1980 case NotEqualOperator:
1981 {
1982 precedence=EquivalencyPrecedence;
1983 break;
1984 }
1985 case '&':
1986 {
1987 precedence=BitwiseAndPrecedence;
1988 break;
1989 }
1990 case '|':
1991 {
1992 precedence=BitwiseOrPrecedence;
1993 break;
1994 }
1995 case LogicalAndOperator:
1996 {
1997 precedence=LogicalAndPrecedence;
1998 break;
1999 }
2000 case LogicalOrOperator:
2001 {
2002 precedence=LogicalOrPrecedence;
2003 break;
2004 }
cristy116af162010-08-13 01:25:47 +00002005 case ExponentialNotation:
2006 {
2007 precedence=ExponentialNotationPrecedence;
2008 break;
2009 }
cristy3ed852e2009-09-05 21:47:34 +00002010 case ':':
2011 case '?':
2012 {
2013 precedence=TernaryPrecedence;
2014 break;
2015 }
2016 case '=':
2017 {
2018 precedence=AssignmentPrecedence;
2019 break;
2020 }
2021 case ',':
2022 {
2023 precedence=CommaPrecedence;
2024 break;
2025 }
2026 case ';':
2027 {
2028 precedence=SeparatorPrecedence;
2029 break;
2030 }
2031 }
2032 if ((precedence == BitwiseComplementPrecedence) ||
2033 (precedence == TernaryPrecedence) ||
2034 (precedence == AssignmentPrecedence))
2035 {
2036 if (precedence > target)
2037 {
2038 /*
2039 Right-to-left associativity.
2040 */
2041 target=precedence;
2042 subexpression=expression;
2043 }
2044 }
2045 else
2046 if (precedence >= target)
2047 {
2048 /*
2049 Left-to-right associativity.
2050 */
2051 target=precedence;
2052 subexpression=expression;
2053 }
2054 if (strchr("(",(int) *expression) != (char *) NULL)
2055 expression=FxSubexpression(expression,exception);
2056 c=(int) (*expression++);
2057 }
2058 return(subexpression);
2059}
2060
2061static MagickRealType FxEvaluateSubexpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002062 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristye85007d2010-06-06 22:51:36 +00002063 const char *expression,MagickRealType *beta,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002064{
2065 char
2066 *q,
2067 subexpression[MaxTextExtent];
2068
2069 MagickRealType
2070 alpha,
2071 gamma;
2072
2073 register const char
2074 *p;
2075
2076 *beta=0.0;
2077 if (exception->severity != UndefinedException)
2078 return(0.0);
2079 while (isspace((int) *expression) != 0)
2080 expression++;
2081 if (*expression == '\0')
2082 {
2083 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
2084 "MissingExpression","`%s'",expression);
2085 return(0.0);
2086 }
cristy66322f02010-05-17 11:40:48 +00002087 *subexpression='\0';
cristy3ed852e2009-09-05 21:47:34 +00002088 p=FxOperatorPrecedence(expression,exception);
2089 if (p != (const char *) NULL)
2090 {
2091 (void) CopyMagickString(subexpression,expression,(size_t)
2092 (p-expression+1));
2093 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2094 exception);
2095 switch ((unsigned char) *p)
2096 {
2097 case '~':
2098 {
2099 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristybb503372010-05-27 20:51:26 +00002100 *beta=(MagickRealType) (~(size_t) *beta);
cristy3ed852e2009-09-05 21:47:34 +00002101 return(*beta);
2102 }
2103 case '!':
2104 {
2105 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2106 return(*beta == 0.0 ? 1.0 : 0.0);
2107 }
2108 case '^':
2109 {
2110 *beta=pow((double) alpha,(double) FxEvaluateSubexpression(fx_info,
2111 channel,x,y,++p,beta,exception));
2112 return(*beta);
2113 }
2114 case '*':
cristy116af162010-08-13 01:25:47 +00002115 case ExponentialNotation:
cristy3ed852e2009-09-05 21:47:34 +00002116 {
2117 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2118 return(alpha*(*beta));
2119 }
2120 case '/':
2121 {
2122 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2123 if (*beta == 0.0)
2124 {
2125 if (exception->severity == UndefinedException)
2126 (void) ThrowMagickException(exception,GetMagickModule(),
2127 OptionError,"DivideByZero","`%s'",expression);
2128 return(0.0);
2129 }
2130 return(alpha/(*beta));
2131 }
2132 case '%':
2133 {
2134 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2135 *beta=fabs(floor(((double) *beta)+0.5));
2136 if (*beta == 0.0)
2137 {
2138 (void) ThrowMagickException(exception,GetMagickModule(),
2139 OptionError,"DivideByZero","`%s'",expression);
2140 return(0.0);
2141 }
2142 return(fmod((double) alpha,(double) *beta));
2143 }
2144 case '+':
2145 {
2146 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2147 return(alpha+(*beta));
2148 }
2149 case '-':
2150 {
2151 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2152 return(alpha-(*beta));
2153 }
2154 case LeftShiftOperator:
2155 {
2156 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy1707c6c2012-01-18 23:30:54 +00002157 *beta=(MagickRealType) ((size_t) (alpha+0.5) << (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002158 return(*beta);
2159 }
2160 case RightShiftOperator:
2161 {
2162 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy1707c6c2012-01-18 23:30:54 +00002163 *beta=(MagickRealType) ((size_t) (alpha+0.5) >> (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002164 return(*beta);
2165 }
2166 case '<':
2167 {
2168 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2169 return(alpha < *beta ? 1.0 : 0.0);
2170 }
2171 case LessThanEqualOperator:
2172 {
2173 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2174 return(alpha <= *beta ? 1.0 : 0.0);
2175 }
2176 case '>':
2177 {
2178 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2179 return(alpha > *beta ? 1.0 : 0.0);
2180 }
2181 case GreaterThanEqualOperator:
2182 {
2183 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2184 return(alpha >= *beta ? 1.0 : 0.0);
2185 }
2186 case EqualOperator:
2187 {
2188 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2189 return(fabs(alpha-(*beta)) <= MagickEpsilon ? 1.0 : 0.0);
2190 }
2191 case NotEqualOperator:
2192 {
2193 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2194 return(fabs(alpha-(*beta)) > MagickEpsilon ? 1.0 : 0.0);
2195 }
2196 case '&':
2197 {
2198 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy1707c6c2012-01-18 23:30:54 +00002199 *beta=(MagickRealType) ((size_t) (alpha+0.5) & (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002200 return(*beta);
2201 }
2202 case '|':
2203 {
2204 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy1707c6c2012-01-18 23:30:54 +00002205 *beta=(MagickRealType) ((size_t) (alpha+0.5) | (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002206 return(*beta);
2207 }
2208 case LogicalAndOperator:
2209 {
2210 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2211 *beta=(alpha > 0.0) && (gamma > 0.0) ? 1.0 : 0.0;
2212 return(*beta);
2213 }
2214 case LogicalOrOperator:
2215 {
2216 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2217 *beta=(alpha > 0.0) || (gamma > 0.0) ? 1.0 : 0.0;
2218 return(*beta);
2219 }
2220 case '?':
2221 {
2222 MagickRealType
2223 gamma;
2224
2225 (void) CopyMagickString(subexpression,++p,MaxTextExtent);
2226 q=subexpression;
2227 p=StringToken(":",&q);
2228 if (q == (char *) NULL)
2229 {
2230 (void) ThrowMagickException(exception,GetMagickModule(),
2231 OptionError,"UnableToParseExpression","`%s'",subexpression);
2232 return(0.0);
2233 }
2234 if (fabs((double) alpha) > MagickEpsilon)
2235 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,exception);
2236 else
2237 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,q,beta,exception);
2238 return(gamma);
2239 }
2240 case '=':
2241 {
2242 char
2243 numeric[MaxTextExtent];
2244
2245 q=subexpression;
2246 while (isalpha((int) ((unsigned char) *q)) != 0)
2247 q++;
2248 if (*q != '\0')
2249 {
2250 (void) ThrowMagickException(exception,GetMagickModule(),
2251 OptionError,"UnableToParseExpression","`%s'",subexpression);
2252 return(0.0);
2253 }
2254 ClearMagickException(exception);
2255 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristyb51dff52011-05-19 16:55:47 +00002256 (void) FormatLocaleString(numeric,MaxTextExtent,"%g",(double)
cristy8cd5b312010-01-07 01:10:24 +00002257 *beta);
cristy3ed852e2009-09-05 21:47:34 +00002258 (void) DeleteNodeFromSplayTree(fx_info->symbols,subexpression);
2259 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(
2260 subexpression),ConstantString(numeric));
2261 return(*beta);
2262 }
2263 case ',':
2264 {
2265 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2266 return(alpha);
2267 }
2268 case ';':
2269 {
2270 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2271 return(*beta);
2272 }
2273 default:
2274 {
2275 gamma=alpha*FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,
2276 exception);
2277 return(gamma);
2278 }
2279 }
2280 }
2281 if (strchr("(",(int) *expression) != (char *) NULL)
2282 {
2283 (void) CopyMagickString(subexpression,expression+1,MaxTextExtent);
2284 subexpression[strlen(subexpression)-1]='\0';
2285 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2286 exception);
2287 return(gamma);
2288 }
cristy8b8a3ae2010-10-23 18:49:46 +00002289 switch (*expression)
cristy3ed852e2009-09-05 21:47:34 +00002290 {
2291 case '+':
2292 {
2293 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2294 exception);
2295 return(1.0*gamma);
2296 }
2297 case '-':
2298 {
2299 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2300 exception);
2301 return(-1.0*gamma);
2302 }
2303 case '~':
2304 {
2305 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2306 exception);
cristybb503372010-05-27 20:51:26 +00002307 return((MagickRealType) (~(size_t) (gamma+0.5)));
cristy3ed852e2009-09-05 21:47:34 +00002308 }
2309 case 'A':
2310 case 'a':
2311 {
2312 if (LocaleNCompare(expression,"abs",3) == 0)
2313 {
2314 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2315 exception);
2316 return((MagickRealType) fabs((double) alpha));
2317 }
cristyb33454f2011-08-03 02:10:45 +00002318#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00002319 if (LocaleNCompare(expression,"acosh",5) == 0)
2320 {
2321 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2322 exception);
2323 return((MagickRealType) acosh((double) alpha));
2324 }
cristyb33454f2011-08-03 02:10:45 +00002325#endif
cristy3ed852e2009-09-05 21:47:34 +00002326 if (LocaleNCompare(expression,"acos",4) == 0)
2327 {
2328 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2329 exception);
2330 return((MagickRealType) acos((double) alpha));
2331 }
cristy43c22f42010-03-30 12:34:07 +00002332#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002333 if (LocaleNCompare(expression,"airy",4) == 0)
2334 {
2335 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2336 exception);
2337 if (alpha == 0.0)
cristy2dd03222010-03-30 22:12:11 +00002338 return(1.0);
2339 gamma=2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha);
cristy43c22f42010-03-30 12:34:07 +00002340 return(gamma*gamma);
cristyee56cf12010-03-01 22:17:06 +00002341 }
cristy43c22f42010-03-30 12:34:07 +00002342#endif
cristyb33454f2011-08-03 02:10:45 +00002343#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00002344 if (LocaleNCompare(expression,"asinh",5) == 0)
2345 {
2346 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2347 exception);
2348 return((MagickRealType) asinh((double) alpha));
2349 }
cristyb33454f2011-08-03 02:10:45 +00002350#endif
cristy3ed852e2009-09-05 21:47:34 +00002351 if (LocaleNCompare(expression,"asin",4) == 0)
2352 {
2353 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2354 exception);
2355 return((MagickRealType) asin((double) alpha));
2356 }
2357 if (LocaleNCompare(expression,"alt",3) == 0)
2358 {
2359 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2360 exception);
cristybb503372010-05-27 20:51:26 +00002361 return(((ssize_t) alpha) & 0x01 ? -1.0 : 1.0);
cristy3ed852e2009-09-05 21:47:34 +00002362 }
2363 if (LocaleNCompare(expression,"atan2",5) == 0)
2364 {
2365 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2366 exception);
2367 return((MagickRealType) atan2((double) alpha,(double) *beta));
2368 }
cristyb33454f2011-08-03 02:10:45 +00002369#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00002370 if (LocaleNCompare(expression,"atanh",5) == 0)
2371 {
2372 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2373 exception);
2374 return((MagickRealType) atanh((double) alpha));
2375 }
cristyb33454f2011-08-03 02:10:45 +00002376#endif
cristy3ed852e2009-09-05 21:47:34 +00002377 if (LocaleNCompare(expression,"atan",4) == 0)
2378 {
2379 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2380 exception);
2381 return((MagickRealType) atan((double) alpha));
2382 }
2383 if (LocaleCompare(expression,"a") == 0)
2384 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2385 break;
2386 }
2387 case 'B':
2388 case 'b':
2389 {
2390 if (LocaleCompare(expression,"b") == 0)
2391 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2392 break;
2393 }
2394 case 'C':
2395 case 'c':
2396 {
2397 if (LocaleNCompare(expression,"ceil",4) == 0)
2398 {
2399 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2400 exception);
2401 return((MagickRealType) ceil((double) alpha));
2402 }
2403 if (LocaleNCompare(expression,"cosh",4) == 0)
2404 {
2405 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2406 exception);
2407 return((MagickRealType) cosh((double) alpha));
2408 }
2409 if (LocaleNCompare(expression,"cos",3) == 0)
2410 {
2411 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2412 exception);
2413 return((MagickRealType) cos((double) alpha));
2414 }
2415 if (LocaleCompare(expression,"c") == 0)
2416 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2417 break;
2418 }
2419 case 'D':
2420 case 'd':
2421 {
2422 if (LocaleNCompare(expression,"debug",5) == 0)
2423 {
2424 const char
2425 *type;
2426
2427 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2428 exception);
2429 if (fx_info->images->colorspace == CMYKColorspace)
2430 switch (channel)
2431 {
cristy0568ffc2011-07-25 16:54:14 +00002432 case CyanPixelChannel: type="cyan"; break;
2433 case MagentaPixelChannel: type="magenta"; break;
2434 case YellowPixelChannel: type="yellow"; break;
2435 case AlphaPixelChannel: type="opacity"; break;
2436 case BlackPixelChannel: type="black"; break;
cristy3ed852e2009-09-05 21:47:34 +00002437 default: type="unknown"; break;
2438 }
2439 else
2440 switch (channel)
2441 {
cristy0568ffc2011-07-25 16:54:14 +00002442 case RedPixelChannel: type="red"; break;
2443 case GreenPixelChannel: type="green"; break;
2444 case BluePixelChannel: type="blue"; break;
2445 case AlphaPixelChannel: type="opacity"; break;
cristy3ed852e2009-09-05 21:47:34 +00002446 default: type="unknown"; break;
2447 }
2448 (void) CopyMagickString(subexpression,expression+6,MaxTextExtent);
2449 if (strlen(subexpression) > 1)
2450 subexpression[strlen(subexpression)-1]='\0';
2451 if (fx_info->file != (FILE *) NULL)
cristy1707c6c2012-01-18 23:30:54 +00002452 (void) FormatLocaleFile(fx_info->file,"%s[%.20g,%.20g].%s: "
2453 "%s=%.*g\n",fx_info->images->filename,(double) x,(double) y,type,
2454 subexpression,GetMagickPrecision(),(double) alpha);
cristy3ed852e2009-09-05 21:47:34 +00002455 return(0.0);
2456 }
cristy5597a8d2011-11-04 00:25:32 +00002457 if (LocaleNCompare(expression,"drc",3) == 0)
2458 {
2459 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2460 exception);
2461 return((MagickRealType) (alpha/(*beta*(alpha-1.0)+1.0)));
2462 }
cristy3ed852e2009-09-05 21:47:34 +00002463 break;
2464 }
2465 case 'E':
2466 case 'e':
2467 {
2468 if (LocaleCompare(expression,"epsilon") == 0)
2469 return((MagickRealType) MagickEpsilon);
2470 if (LocaleNCompare(expression,"exp",3) == 0)
2471 {
2472 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2473 exception);
2474 return((MagickRealType) exp((double) alpha));
2475 }
2476 if (LocaleCompare(expression,"e") == 0)
2477 return((MagickRealType) 2.7182818284590452354);
2478 break;
2479 }
2480 case 'F':
2481 case 'f':
2482 {
2483 if (LocaleNCompare(expression,"floor",5) == 0)
2484 {
2485 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2486 exception);
2487 return((MagickRealType) floor((double) alpha));
2488 }
2489 break;
2490 }
2491 case 'G':
2492 case 'g':
2493 {
cristy9eeedea2011-11-02 19:04:05 +00002494 if (LocaleNCompare(expression,"gauss",5) == 0)
2495 {
2496 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2497 exception);
2498 gamma=exp((double) (-alpha*alpha/2.0))/sqrt(2.0*MagickPI);
2499 return((MagickRealType) gamma);
2500 }
cristyb0aad4c2011-11-02 19:30:35 +00002501 if (LocaleNCompare(expression,"gcd",3) == 0)
2502 {
2503 MagickOffsetType
2504 gcd;
2505
2506 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2507 exception);
cristy1707c6c2012-01-18 23:30:54 +00002508 gcd=FxGCD((MagickOffsetType) (alpha+0.5),(MagickOffsetType) (*beta+
2509 0.5));
cristyb0aad4c2011-11-02 19:30:35 +00002510 return((MagickRealType) gcd);
2511 }
cristy3ed852e2009-09-05 21:47:34 +00002512 if (LocaleCompare(expression,"g") == 0)
2513 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2514 break;
2515 }
2516 case 'H':
2517 case 'h':
2518 {
2519 if (LocaleCompare(expression,"h") == 0)
2520 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2521 if (LocaleCompare(expression,"hue") == 0)
2522 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2523 if (LocaleNCompare(expression,"hypot",5) == 0)
2524 {
2525 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2526 exception);
2527 return((MagickRealType) hypot((double) alpha,(double) *beta));
2528 }
2529 break;
2530 }
2531 case 'K':
2532 case 'k':
2533 {
2534 if (LocaleCompare(expression,"k") == 0)
2535 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2536 break;
2537 }
2538 case 'I':
2539 case 'i':
2540 {
2541 if (LocaleCompare(expression,"intensity") == 0)
2542 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2543 if (LocaleNCompare(expression,"int",3) == 0)
2544 {
2545 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2546 exception);
cristy16788e42010-08-13 13:44:26 +00002547 return((MagickRealType) floor(alpha));
cristy3ed852e2009-09-05 21:47:34 +00002548 }
cristy82b20722011-11-05 21:52:36 +00002549#if defined(MAGICKCORE_HAVE_ISNAN)
cristy639399c2011-11-02 19:16:15 +00002550 if (LocaleNCompare(expression,"isnan",5) == 0)
2551 {
2552 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2553 exception);
cristy17a10202011-11-02 19:17:04 +00002554 return((MagickRealType) !!isnan((double) alpha));
cristy639399c2011-11-02 19:16:15 +00002555 }
cristy82b20722011-11-05 21:52:36 +00002556#endif
cristy3ed852e2009-09-05 21:47:34 +00002557 if (LocaleCompare(expression,"i") == 0)
2558 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2559 break;
2560 }
2561 case 'J':
2562 case 'j':
2563 {
2564 if (LocaleCompare(expression,"j") == 0)
2565 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
cristy161b9262010-03-20 19:34:32 +00002566#if defined(MAGICKCORE_HAVE_J0)
cristyee56cf12010-03-01 22:17:06 +00002567 if (LocaleNCompare(expression,"j0",2) == 0)
2568 {
2569 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2570 exception);
2571 return((MagickRealType) j0((double) alpha));
2572 }
cristy161b9262010-03-20 19:34:32 +00002573#endif
2574#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002575 if (LocaleNCompare(expression,"j1",2) == 0)
2576 {
2577 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2578 exception);
2579 return((MagickRealType) j1((double) alpha));
2580 }
cristy161b9262010-03-20 19:34:32 +00002581#endif
cristyaa018fa2010-04-08 23:03:54 +00002582#if defined(MAGICKCORE_HAVE_J1)
cristya6a09e72010-03-02 14:51:02 +00002583 if (LocaleNCompare(expression,"jinc",4) == 0)
2584 {
2585 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2586 exception);
cristy0946a822010-03-12 17:14:58 +00002587 if (alpha == 0.0)
2588 return(1.0);
cristy1707c6c2012-01-18 23:30:54 +00002589 gamma=(MagickRealType) (2.0*j1((double) (MagickPI*alpha))/(MagickPI*
2590 alpha));
cristyfce2f7b2010-03-12 00:29:49 +00002591 return(gamma);
cristya6a09e72010-03-02 14:51:02 +00002592 }
cristyaa018fa2010-04-08 23:03:54 +00002593#endif
cristy3ed852e2009-09-05 21:47:34 +00002594 break;
2595 }
2596 case 'L':
2597 case 'l':
2598 {
2599 if (LocaleNCompare(expression,"ln",2) == 0)
2600 {
2601 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2602 exception);
2603 return((MagickRealType) log((double) alpha));
2604 }
cristyc8ed5322010-08-31 12:07:59 +00002605 if (LocaleNCompare(expression,"logtwo",6) == 0)
cristy3ed852e2009-09-05 21:47:34 +00002606 {
cristyc8ed5322010-08-31 12:07:59 +00002607 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
cristy3ed852e2009-09-05 21:47:34 +00002608 exception);
2609 return((MagickRealType) log10((double) alpha))/log10(2.0);
2610 }
2611 if (LocaleNCompare(expression,"log",3) == 0)
2612 {
2613 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2614 exception);
2615 return((MagickRealType) log10((double) alpha));
2616 }
2617 if (LocaleCompare(expression,"lightness") == 0)
2618 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2619 break;
2620 }
2621 case 'M':
2622 case 'm':
2623 {
2624 if (LocaleCompare(expression,"MaxRGB") == 0)
2625 return((MagickRealType) QuantumRange);
2626 if (LocaleNCompare(expression,"maxima",6) == 0)
2627 break;
2628 if (LocaleNCompare(expression,"max",3) == 0)
cristy984049c2011-11-03 18:34:58 +00002629 {
2630 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2631 exception);
2632 return(alpha > *beta ? alpha : *beta);
2633 }
cristy3ed852e2009-09-05 21:47:34 +00002634 if (LocaleNCompare(expression,"minima",6) == 0)
2635 break;
2636 if (LocaleNCompare(expression,"min",3) == 0)
cristy984049c2011-11-03 18:34:58 +00002637 {
2638 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2639 exception);
2640 return(alpha < *beta ? alpha : *beta);
2641 }
cristy3ed852e2009-09-05 21:47:34 +00002642 if (LocaleNCompare(expression,"mod",3) == 0)
2643 {
2644 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2645 exception);
cristy984049c2011-11-03 18:34:58 +00002646 gamma=alpha-floor((double) (alpha/(*beta)))*(*beta);
2647 return(gamma);
cristy3ed852e2009-09-05 21:47:34 +00002648 }
2649 if (LocaleCompare(expression,"m") == 0)
2650 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2651 break;
2652 }
2653 case 'N':
2654 case 'n':
2655 {
cristyad3502e2011-11-02 19:10:45 +00002656 if (LocaleNCompare(expression,"not",3) == 0)
2657 {
2658 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2659 exception);
2660 return((MagickRealType) (alpha < MagickEpsilon));
2661 }
cristy3ed852e2009-09-05 21:47:34 +00002662 if (LocaleCompare(expression,"n") == 0)
2663 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2664 break;
2665 }
2666 case 'O':
2667 case 'o':
2668 {
2669 if (LocaleCompare(expression,"Opaque") == 0)
2670 return(1.0);
2671 if (LocaleCompare(expression,"o") == 0)
2672 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2673 break;
2674 }
2675 case 'P':
2676 case 'p':
2677 {
cristy670aa3c2011-11-03 00:54:00 +00002678 if (LocaleCompare(expression,"phi") == 0)
2679 return((MagickRealType) MagickPHI);
cristy3ed852e2009-09-05 21:47:34 +00002680 if (LocaleCompare(expression,"pi") == 0)
2681 return((MagickRealType) MagickPI);
2682 if (LocaleNCompare(expression,"pow",3) == 0)
2683 {
2684 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2685 exception);
2686 return((MagickRealType) pow((double) alpha,(double) *beta));
2687 }
2688 if (LocaleCompare(expression,"p") == 0)
2689 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2690 break;
2691 }
2692 case 'Q':
2693 case 'q':
2694 {
2695 if (LocaleCompare(expression,"QuantumRange") == 0)
2696 return((MagickRealType) QuantumRange);
2697 if (LocaleCompare(expression,"QuantumScale") == 0)
2698 return((MagickRealType) QuantumScale);
2699 break;
2700 }
2701 case 'R':
2702 case 'r':
2703 {
2704 if (LocaleNCompare(expression,"rand",4) == 0)
2705 return((MagickRealType) GetPseudoRandomValue(fx_info->random_info));
2706 if (LocaleNCompare(expression,"round",5) == 0)
2707 {
2708 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2709 exception);
cristy16788e42010-08-13 13:44:26 +00002710 return((MagickRealType) floor((double) alpha+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002711 }
2712 if (LocaleCompare(expression,"r") == 0)
2713 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2714 break;
2715 }
2716 case 'S':
2717 case 's':
2718 {
2719 if (LocaleCompare(expression,"saturation") == 0)
2720 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2721 if (LocaleNCompare(expression,"sign",4) == 0)
2722 {
2723 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2724 exception);
2725 return(alpha < 0.0 ? -1.0 : 1.0);
2726 }
cristya6a09e72010-03-02 14:51:02 +00002727 if (LocaleNCompare(expression,"sinc",4) == 0)
2728 {
2729 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2730 exception);
2731 if (alpha == 0)
2732 return(1.0);
2733 gamma=(MagickRealType) (sin((double) (MagickPI*alpha))/
2734 (MagickPI*alpha));
2735 return(gamma);
2736 }
cristy3ed852e2009-09-05 21:47:34 +00002737 if (LocaleNCompare(expression,"sinh",4) == 0)
2738 {
2739 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2740 exception);
2741 return((MagickRealType) sinh((double) alpha));
2742 }
2743 if (LocaleNCompare(expression,"sin",3) == 0)
2744 {
2745 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2746 exception);
2747 return((MagickRealType) sin((double) alpha));
2748 }
2749 if (LocaleNCompare(expression,"sqrt",4) == 0)
2750 {
2751 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2752 exception);
2753 return((MagickRealType) sqrt((double) alpha));
2754 }
cristy9eeedea2011-11-02 19:04:05 +00002755 if (LocaleNCompare(expression,"squish",6) == 0)
2756 {
2757 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2758 exception);
2759 return((MagickRealType) (1.0/(1.0+exp((double) (4.0*alpha)))));
2760 }
cristy3ed852e2009-09-05 21:47:34 +00002761 if (LocaleCompare(expression,"s") == 0)
2762 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2763 break;
2764 }
2765 case 'T':
2766 case 't':
2767 {
2768 if (LocaleNCompare(expression,"tanh",4) == 0)
2769 {
2770 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2771 exception);
2772 return((MagickRealType) tanh((double) alpha));
2773 }
2774 if (LocaleNCompare(expression,"tan",3) == 0)
2775 {
2776 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2777 exception);
2778 return((MagickRealType) tan((double) alpha));
2779 }
2780 if (LocaleCompare(expression,"Transparent") == 0)
2781 return(0.0);
cristy16788e42010-08-13 13:44:26 +00002782 if (LocaleNCompare(expression,"trunc",5) == 0)
2783 {
2784 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2785 exception);
2786 if (alpha >= 0.0)
2787 return((MagickRealType) floor((double) alpha));
2788 return((MagickRealType) ceil((double) alpha));
2789 }
cristy3ed852e2009-09-05 21:47:34 +00002790 if (LocaleCompare(expression,"t") == 0)
2791 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2792 break;
2793 }
2794 case 'U':
2795 case 'u':
2796 {
2797 if (LocaleCompare(expression,"u") == 0)
2798 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2799 break;
2800 }
2801 case 'V':
2802 case 'v':
2803 {
2804 if (LocaleCompare(expression,"v") == 0)
2805 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2806 break;
2807 }
2808 case 'W':
2809 case 'w':
2810 {
cristy9eeedea2011-11-02 19:04:05 +00002811 if (LocaleNCompare(expression,"while",5) == 0)
2812 {
2813 do
2814 {
2815 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2816 exception);
2817 } while (fabs((double) alpha) >= MagickEpsilon);
2818 return((MagickRealType) *beta);
2819 }
cristy3ed852e2009-09-05 21:47:34 +00002820 if (LocaleCompare(expression,"w") == 0)
2821 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2822 break;
2823 }
2824 case 'Y':
2825 case 'y':
2826 {
2827 if (LocaleCompare(expression,"y") == 0)
2828 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2829 break;
2830 }
2831 case 'Z':
2832 case 'z':
2833 {
2834 if (LocaleCompare(expression,"z") == 0)
2835 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2836 break;
2837 }
2838 default:
2839 break;
2840 }
2841 q=(char *) expression;
cristydbdd0e32011-11-04 23:29:40 +00002842 alpha=InterpretSiPrefixValue(expression,&q);
cristy3ed852e2009-09-05 21:47:34 +00002843 if (q == expression)
2844 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2845 return(alpha);
2846}
2847
cristy7832dc22011-09-05 01:21:53 +00002848MagickPrivate MagickBooleanType FxEvaluateExpression(FxInfo *fx_info,
cristy3ed852e2009-09-05 21:47:34 +00002849 MagickRealType *alpha,ExceptionInfo *exception)
2850{
2851 MagickBooleanType
2852 status;
2853
cristy541ae572011-08-05 19:08:59 +00002854 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2855 exception);
cristy3ed852e2009-09-05 21:47:34 +00002856 return(status);
2857}
2858
2859MagickExport MagickBooleanType FxPreprocessExpression(FxInfo *fx_info,
2860 MagickRealType *alpha,ExceptionInfo *exception)
2861{
2862 FILE
2863 *file;
2864
2865 MagickBooleanType
2866 status;
2867
2868 file=fx_info->file;
2869 fx_info->file=(FILE *) NULL;
cristy541ae572011-08-05 19:08:59 +00002870 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2871 exception);
cristy3ed852e2009-09-05 21:47:34 +00002872 fx_info->file=file;
2873 return(status);
2874}
2875
cristy7832dc22011-09-05 01:21:53 +00002876MagickPrivate MagickBooleanType FxEvaluateChannelExpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002877 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristye85007d2010-06-06 22:51:36 +00002878 MagickRealType *alpha,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002879{
2880 MagickRealType
2881 beta;
2882
2883 beta=0.0;
2884 *alpha=FxEvaluateSubexpression(fx_info,channel,x,y,fx_info->expression,&beta,
2885 exception);
2886 return(exception->severity == OptionError ? MagickFalse : MagickTrue);
2887}
2888
2889/*
2890%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2891% %
2892% %
2893% %
2894% F x I m a g e %
2895% %
2896% %
2897% %
2898%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2899%
2900% FxImage() applies a mathematical expression to the specified image.
2901%
2902% The format of the FxImage method is:
2903%
2904% Image *FxImage(const Image *image,const char *expression,
2905% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002906%
2907% A description of each parameter follows:
2908%
2909% o image: the image.
2910%
cristy3ed852e2009-09-05 21:47:34 +00002911% o expression: A mathematical expression.
2912%
2913% o exception: return any errors or warnings in this structure.
2914%
2915*/
2916
2917static FxInfo **DestroyFxThreadSet(FxInfo **fx_info)
2918{
cristybb503372010-05-27 20:51:26 +00002919 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002920 i;
2921
2922 assert(fx_info != (FxInfo **) NULL);
cristybb503372010-05-27 20:51:26 +00002923 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
cristy3ed852e2009-09-05 21:47:34 +00002924 if (fx_info[i] != (FxInfo *) NULL)
2925 fx_info[i]=DestroyFxInfo(fx_info[i]);
cristyb41ee102010-10-04 16:46:15 +00002926 fx_info=(FxInfo **) RelinquishMagickMemory(fx_info);
cristy3ed852e2009-09-05 21:47:34 +00002927 return(fx_info);
2928}
2929
2930static FxInfo **AcquireFxThreadSet(const Image *image,const char *expression,
2931 ExceptionInfo *exception)
2932{
2933 char
2934 *fx_expression;
2935
2936 FxInfo
2937 **fx_info;
2938
2939 MagickRealType
2940 alpha;
2941
cristybb503372010-05-27 20:51:26 +00002942 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002943 i;
2944
cristybb503372010-05-27 20:51:26 +00002945 size_t
cristy3ed852e2009-09-05 21:47:34 +00002946 number_threads;
2947
2948 number_threads=GetOpenMPMaximumThreads();
cristyb41ee102010-10-04 16:46:15 +00002949 fx_info=(FxInfo **) AcquireQuantumMemory(number_threads,sizeof(*fx_info));
cristy3ed852e2009-09-05 21:47:34 +00002950 if (fx_info == (FxInfo **) NULL)
2951 return((FxInfo **) NULL);
2952 (void) ResetMagickMemory(fx_info,0,number_threads*sizeof(*fx_info));
2953 if (*expression != '@')
2954 fx_expression=ConstantString(expression);
2955 else
2956 fx_expression=FileToString(expression+1,~0,exception);
cristybb503372010-05-27 20:51:26 +00002957 for (i=0; i < (ssize_t) number_threads; i++)
cristy3ed852e2009-09-05 21:47:34 +00002958 {
2959 fx_info[i]=AcquireFxInfo(image,fx_expression);
2960 if (fx_info[i] == (FxInfo *) NULL)
2961 return(DestroyFxThreadSet(fx_info));
2962 (void) FxPreprocessExpression(fx_info[i],&alpha,fx_info[i]->exception);
2963 }
2964 fx_expression=DestroyString(fx_expression);
2965 return(fx_info);
2966}
2967
2968MagickExport Image *FxImage(const Image *image,const char *expression,
2969 ExceptionInfo *exception)
2970{
cristy3ed852e2009-09-05 21:47:34 +00002971#define FxImageTag "Fx/Image"
2972
cristyfa112112010-01-04 17:48:07 +00002973 CacheView
cristy79cedc72011-07-25 00:41:15 +00002974 *fx_view,
2975 *image_view;
cristyfa112112010-01-04 17:48:07 +00002976
cristy3ed852e2009-09-05 21:47:34 +00002977 FxInfo
cristyfa112112010-01-04 17:48:07 +00002978 **restrict fx_info;
cristy3ed852e2009-09-05 21:47:34 +00002979
2980 Image
2981 *fx_image;
2982
cristy3ed852e2009-09-05 21:47:34 +00002983 MagickBooleanType
2984 status;
2985
cristybb503372010-05-27 20:51:26 +00002986 MagickOffsetType
2987 progress;
2988
cristy3ed852e2009-09-05 21:47:34 +00002989 MagickRealType
2990 alpha;
2991
cristybb503372010-05-27 20:51:26 +00002992 ssize_t
2993 y;
2994
cristy3ed852e2009-09-05 21:47:34 +00002995 assert(image != (Image *) NULL);
2996 assert(image->signature == MagickSignature);
2997 if (image->debug != MagickFalse)
2998 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy79cedc72011-07-25 00:41:15 +00002999 fx_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00003000 if (fx_image == (Image *) NULL)
3001 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003002 if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003003 {
cristy3ed852e2009-09-05 21:47:34 +00003004 fx_image=DestroyImage(fx_image);
3005 return((Image *) NULL);
3006 }
3007 fx_info=AcquireFxThreadSet(image,expression,exception);
3008 if (fx_info == (FxInfo **) NULL)
3009 {
3010 fx_image=DestroyImage(fx_image);
3011 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3012 }
3013 status=FxPreprocessExpression(fx_info[0],&alpha,exception);
3014 if (status == MagickFalse)
3015 {
3016 fx_image=DestroyImage(fx_image);
3017 fx_info=DestroyFxThreadSet(fx_info);
3018 return((Image *) NULL);
3019 }
3020 /*
3021 Fx image.
3022 */
3023 status=MagickTrue;
3024 progress=0;
cristy79cedc72011-07-25 00:41:15 +00003025 image_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00003026 fx_view=AcquireCacheView(fx_image);
cristyb5d5f722009-11-04 03:03:49 +00003027#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003028 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003029#endif
cristybb503372010-05-27 20:51:26 +00003030 for (y=0; y < (ssize_t) fx_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003031 {
cristy5c9e6f22010-09-17 17:31:01 +00003032 const int
3033 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00003034
cristy79cedc72011-07-25 00:41:15 +00003035 register const Quantum
3036 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003037
cristy4c08aed2011-07-01 19:47:50 +00003038 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003039 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003040
cristy79cedc72011-07-25 00:41:15 +00003041 register ssize_t
3042 x;
3043
cristy3ed852e2009-09-05 21:47:34 +00003044 if (status == MagickFalse)
3045 continue;
cristy79cedc72011-07-25 00:41:15 +00003046 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +00003047 q=QueueCacheViewAuthenticPixels(fx_view,0,y,fx_image->columns,1,exception);
cristy79cedc72011-07-25 00:41:15 +00003048 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003049 {
3050 status=MagickFalse;
3051 continue;
3052 }
cristybb503372010-05-27 20:51:26 +00003053 for (x=0; x < (ssize_t) fx_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003054 {
cristy79cedc72011-07-25 00:41:15 +00003055 register ssize_t
3056 i;
3057
3058 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3059 {
3060 MagickRealType
3061 alpha;
3062
3063 PixelChannel
3064 channel;
3065
3066 PixelTrait
3067 fx_traits,
3068 traits;
3069
cristye2a912b2011-12-05 20:02:07 +00003070 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003071 traits=GetPixelChannelMapTraits(image,channel);
cristy79cedc72011-07-25 00:41:15 +00003072 fx_traits=GetPixelChannelMapTraits(fx_image,channel);
cristy010d7d12011-08-31 01:02:48 +00003073 if ((traits == UndefinedPixelTrait) ||
3074 (fx_traits == UndefinedPixelTrait))
cristy79cedc72011-07-25 00:41:15 +00003075 continue;
cristyd09f8802012-02-04 16:44:10 +00003076 if (((fx_traits & CopyPixelTrait) != 0) ||
3077 (GetPixelMask(image,p) != 0))
cristy79cedc72011-07-25 00:41:15 +00003078 {
cristy0beccfa2011-09-25 20:47:53 +00003079 SetPixelChannel(fx_image,channel,p[i],q);
cristy79cedc72011-07-25 00:41:15 +00003080 continue;
3081 }
3082 alpha=0.0;
cristya382aca2011-12-06 18:22:48 +00003083 (void) FxEvaluateChannelExpression(fx_info[id],channel,x,y,&alpha,
3084 exception);
cristyb3a73b52011-07-26 01:34:43 +00003085 q[i]=ClampToQuantum((MagickRealType) QuantumRange*alpha);
cristy79cedc72011-07-25 00:41:15 +00003086 }
3087 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003088 q+=GetPixelChannels(fx_image);
cristy3ed852e2009-09-05 21:47:34 +00003089 }
3090 if (SyncCacheViewAuthenticPixels(fx_view,exception) == MagickFalse)
3091 status=MagickFalse;
3092 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3093 {
3094 MagickBooleanType
3095 proceed;
3096
cristyb5d5f722009-11-04 03:03:49 +00003097#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003098 #pragma omp critical (MagickCore_FxImage)
cristy3ed852e2009-09-05 21:47:34 +00003099#endif
3100 proceed=SetImageProgress(image,FxImageTag,progress++,image->rows);
3101 if (proceed == MagickFalse)
3102 status=MagickFalse;
3103 }
3104 }
cristy3ed852e2009-09-05 21:47:34 +00003105 fx_view=DestroyCacheView(fx_view);
cristy79cedc72011-07-25 00:41:15 +00003106 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003107 fx_info=DestroyFxThreadSet(fx_info);
3108 if (status == MagickFalse)
3109 fx_image=DestroyImage(fx_image);
3110 return(fx_image);
3111}
3112
3113/*
3114%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3115% %
3116% %
3117% %
3118% I m p l o d e I m a g e %
3119% %
3120% %
3121% %
3122%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3123%
3124% ImplodeImage() creates a new image that is a copy of an existing
3125% one with the image pixels "implode" by the specified percentage. It
3126% allocates the memory necessary for the new Image structure and returns a
3127% pointer to the new image.
3128%
3129% The format of the ImplodeImage method is:
3130%
3131% Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003132% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003133%
3134% A description of each parameter follows:
3135%
3136% o implode_image: Method ImplodeImage returns a pointer to the image
3137% after it is implode. A null image is returned if there is a memory
3138% shortage.
3139%
3140% o image: the image.
3141%
3142% o amount: Define the extent of the implosion.
3143%
cristy76f512e2011-09-12 01:26:56 +00003144% o method: the pixel interpolation method.
3145%
cristy3ed852e2009-09-05 21:47:34 +00003146% o exception: return any errors or warnings in this structure.
3147%
3148*/
3149MagickExport Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003150 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003151{
3152#define ImplodeImageTag "Implode/Image"
3153
cristyfa112112010-01-04 17:48:07 +00003154 CacheView
3155 *image_view,
3156 *implode_view;
3157
cristy3ed852e2009-09-05 21:47:34 +00003158 Image
3159 *implode_image;
3160
cristy3ed852e2009-09-05 21:47:34 +00003161 MagickBooleanType
3162 status;
3163
cristybb503372010-05-27 20:51:26 +00003164 MagickOffsetType
3165 progress;
3166
cristy3ed852e2009-09-05 21:47:34 +00003167 MagickRealType
3168 radius;
3169
3170 PointInfo
3171 center,
3172 scale;
3173
cristybb503372010-05-27 20:51:26 +00003174 ssize_t
3175 y;
3176
cristy3ed852e2009-09-05 21:47:34 +00003177 /*
3178 Initialize implode image attributes.
3179 */
3180 assert(image != (Image *) NULL);
3181 assert(image->signature == MagickSignature);
3182 if (image->debug != MagickFalse)
3183 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3184 assert(exception != (ExceptionInfo *) NULL);
3185 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00003186 implode_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3187 exception);
cristy3ed852e2009-09-05 21:47:34 +00003188 if (implode_image == (Image *) NULL)
3189 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003190 if (SetImageStorageClass(implode_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003191 {
cristy3ed852e2009-09-05 21:47:34 +00003192 implode_image=DestroyImage(implode_image);
3193 return((Image *) NULL);
3194 }
cristy4c08aed2011-07-01 19:47:50 +00003195 if (implode_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00003196 implode_image->matte=MagickTrue;
3197 /*
3198 Compute scaling factor.
3199 */
3200 scale.x=1.0;
3201 scale.y=1.0;
3202 center.x=0.5*image->columns;
3203 center.y=0.5*image->rows;
3204 radius=center.x;
3205 if (image->columns > image->rows)
3206 scale.y=(double) image->columns/(double) image->rows;
3207 else
3208 if (image->columns < image->rows)
3209 {
3210 scale.x=(double) image->rows/(double) image->columns;
3211 radius=center.y;
3212 }
3213 /*
3214 Implode image.
3215 */
3216 status=MagickTrue;
3217 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00003218 image_view=AcquireCacheView(image);
3219 implode_view=AcquireCacheView(implode_image);
cristyb5d5f722009-11-04 03:03:49 +00003220#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003221 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003222#endif
cristybb503372010-05-27 20:51:26 +00003223 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003224 {
cristy3ed852e2009-09-05 21:47:34 +00003225 MagickRealType
3226 distance;
3227
3228 PointInfo
3229 delta;
3230
cristy6d188022011-09-12 13:23:33 +00003231 register const Quantum
3232 *restrict p;
3233
cristybb503372010-05-27 20:51:26 +00003234 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003235 x;
3236
cristy4c08aed2011-07-01 19:47:50 +00003237 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003238 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003239
3240 if (status == MagickFalse)
3241 continue;
cristy6d188022011-09-12 13:23:33 +00003242 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +00003243 q=QueueCacheViewAuthenticPixels(implode_view,0,y,implode_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003244 exception);
cristy6d188022011-09-12 13:23:33 +00003245 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003246 {
3247 status=MagickFalse;
3248 continue;
3249 }
cristy3ed852e2009-09-05 21:47:34 +00003250 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00003251 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003252 {
cristy6d188022011-09-12 13:23:33 +00003253 register ssize_t
3254 i;
3255
cristy3ed852e2009-09-05 21:47:34 +00003256 /*
3257 Determine if the pixel is within an ellipse.
3258 */
cristy10a6c612012-01-29 21:41:05 +00003259 if (GetPixelMask(image,p) != 0)
3260 {
3261 p+=GetPixelChannels(image);
3262 q+=GetPixelChannels(implode_image);
3263 continue;
3264 }
cristy3ed852e2009-09-05 21:47:34 +00003265 delta.x=scale.x*(double) (x-center.x);
3266 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00003267 if (distance >= (radius*radius))
cristya6d7a9b2012-01-18 20:04:48 +00003268 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy6d188022011-09-12 13:23:33 +00003269 {
cristya6d7a9b2012-01-18 20:04:48 +00003270 PixelChannel
3271 channel;
3272
cristy1707c6c2012-01-18 23:30:54 +00003273 PixelTrait
3274 implode_traits,
3275 traits;
3276
cristya6d7a9b2012-01-18 20:04:48 +00003277 channel=GetPixelChannelMapChannel(image,i);
cristy1707c6c2012-01-18 23:30:54 +00003278 traits=GetPixelChannelMapTraits(image,channel);
3279 implode_traits=GetPixelChannelMapTraits(implode_image,channel);
3280 if ((traits == UndefinedPixelTrait) ||
3281 (implode_traits == UndefinedPixelTrait))
3282 continue;
cristya6d7a9b2012-01-18 20:04:48 +00003283 SetPixelChannel(implode_image,channel,p[i],q);
cristy6d188022011-09-12 13:23:33 +00003284 }
3285 else
cristy3ed852e2009-09-05 21:47:34 +00003286 {
3287 double
3288 factor;
3289
3290 /*
3291 Implode the pixel.
3292 */
3293 factor=1.0;
3294 if (distance > 0.0)
cristy1707c6c2012-01-18 23:30:54 +00003295 factor=pow(sin((double) (MagickPI*sqrt((double) distance)/radius/
3296 2)),-amount);
cristy76f512e2011-09-12 01:26:56 +00003297 status=InterpolatePixelChannels(image,image_view,implode_image,method,
3298 (double) (factor*delta.x/scale.x+center.x),(double) (factor*delta.y/
3299 scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00003300 }
cristy6d188022011-09-12 13:23:33 +00003301 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003302 q+=GetPixelChannels(implode_image);
cristy3ed852e2009-09-05 21:47:34 +00003303 }
3304 if (SyncCacheViewAuthenticPixels(implode_view,exception) == MagickFalse)
3305 status=MagickFalse;
3306 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3307 {
3308 MagickBooleanType
3309 proceed;
3310
cristyb5d5f722009-11-04 03:03:49 +00003311#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003312 #pragma omp critical (MagickCore_ImplodeImage)
cristy3ed852e2009-09-05 21:47:34 +00003313#endif
3314 proceed=SetImageProgress(image,ImplodeImageTag,progress++,image->rows);
3315 if (proceed == MagickFalse)
3316 status=MagickFalse;
3317 }
3318 }
3319 implode_view=DestroyCacheView(implode_view);
3320 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003321 if (status == MagickFalse)
3322 implode_image=DestroyImage(implode_image);
3323 return(implode_image);
3324}
3325
3326/*
3327%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3328% %
3329% %
3330% %
3331% M o r p h I m a g e s %
3332% %
3333% %
3334% %
3335%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3336%
3337% The MorphImages() method requires a minimum of two images. The first
3338% image is transformed into the second by a number of intervening images
3339% as specified by frames.
3340%
3341% The format of the MorphImage method is:
3342%
cristybb503372010-05-27 20:51:26 +00003343% Image *MorphImages(const Image *image,const size_t number_frames,
cristy3ed852e2009-09-05 21:47:34 +00003344% ExceptionInfo *exception)
3345%
3346% A description of each parameter follows:
3347%
3348% o image: the image.
3349%
3350% o number_frames: Define the number of in-between image to generate.
3351% The more in-between frames, the smoother the morph.
3352%
3353% o exception: return any errors or warnings in this structure.
3354%
3355*/
3356MagickExport Image *MorphImages(const Image *image,
cristybb503372010-05-27 20:51:26 +00003357 const size_t number_frames,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003358{
3359#define MorphImageTag "Morph/Image"
3360
3361 Image
3362 *morph_image,
3363 *morph_images;
3364
cristy9d314ff2011-03-09 01:30:28 +00003365 MagickBooleanType
3366 status;
cristy3ed852e2009-09-05 21:47:34 +00003367
3368 MagickOffsetType
3369 scene;
3370
3371 MagickRealType
3372 alpha,
3373 beta;
3374
3375 register const Image
3376 *next;
3377
cristybb503372010-05-27 20:51:26 +00003378 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003379 i;
3380
cristy9d314ff2011-03-09 01:30:28 +00003381 ssize_t
3382 y;
cristy3ed852e2009-09-05 21:47:34 +00003383
3384 /*
3385 Clone first frame in sequence.
3386 */
3387 assert(image != (Image *) NULL);
3388 assert(image->signature == MagickSignature);
3389 if (image->debug != MagickFalse)
3390 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3391 assert(exception != (ExceptionInfo *) NULL);
3392 assert(exception->signature == MagickSignature);
3393 morph_images=CloneImage(image,0,0,MagickTrue,exception);
3394 if (morph_images == (Image *) NULL)
3395 return((Image *) NULL);
3396 if (GetNextImageInList(image) == (Image *) NULL)
3397 {
3398 /*
3399 Morph single image.
3400 */
cristybb503372010-05-27 20:51:26 +00003401 for (i=1; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003402 {
3403 morph_image=CloneImage(image,0,0,MagickTrue,exception);
3404 if (morph_image == (Image *) NULL)
3405 {
3406 morph_images=DestroyImageList(morph_images);
3407 return((Image *) NULL);
3408 }
3409 AppendImageToList(&morph_images,morph_image);
cristy8b27a6d2010-02-14 03:31:15 +00003410 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003411 {
cristy8b27a6d2010-02-14 03:31:15 +00003412 MagickBooleanType
3413 proceed;
3414
cristybb503372010-05-27 20:51:26 +00003415 proceed=SetImageProgress(image,MorphImageTag,(MagickOffsetType) i,
3416 number_frames);
cristy8b27a6d2010-02-14 03:31:15 +00003417 if (proceed == MagickFalse)
3418 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00003419 }
3420 }
3421 return(GetFirstImageInList(morph_images));
3422 }
3423 /*
3424 Morph image sequence.
3425 */
3426 status=MagickTrue;
3427 scene=0;
3428 next=image;
3429 for ( ; GetNextImageInList(next) != (Image *) NULL; next=GetNextImageInList(next))
3430 {
cristybb503372010-05-27 20:51:26 +00003431 for (i=0; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003432 {
3433 CacheView
3434 *image_view,
3435 *morph_view;
3436
3437 beta=(MagickRealType) (i+1.0)/(MagickRealType) (number_frames+1.0);
3438 alpha=1.0-beta;
cristy15b98cd2010-09-12 19:42:50 +00003439 morph_image=ResizeImage(next,(size_t) (alpha*next->columns+beta*
cristyaa2c16c2012-03-25 22:21:35 +00003440 GetNextImageInList(next)->columns+0.5),(size_t) (alpha*next->rows+beta*
3441 GetNextImageInList(next)->rows+0.5),next->filter,exception);
cristy3ed852e2009-09-05 21:47:34 +00003442 if (morph_image == (Image *) NULL)
3443 {
3444 morph_images=DestroyImageList(morph_images);
3445 return((Image *) NULL);
3446 }
cristy1707c6c2012-01-18 23:30:54 +00003447 status=SetImageStorageClass(morph_image,DirectClass,exception);
3448 if (status == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003449 {
cristy3ed852e2009-09-05 21:47:34 +00003450 morph_image=DestroyImage(morph_image);
3451 return((Image *) NULL);
3452 }
3453 AppendImageToList(&morph_images,morph_image);
3454 morph_images=GetLastImageInList(morph_images);
cristy15b98cd2010-09-12 19:42:50 +00003455 morph_image=ResizeImage(GetNextImageInList(next),morph_images->columns,
cristyaa2c16c2012-03-25 22:21:35 +00003456 morph_images->rows,GetNextImageInList(next)->filter,exception);
cristy3ed852e2009-09-05 21:47:34 +00003457 if (morph_image == (Image *) NULL)
3458 {
3459 morph_images=DestroyImageList(morph_images);
3460 return((Image *) NULL);
3461 }
3462 image_view=AcquireCacheView(morph_image);
3463 morph_view=AcquireCacheView(morph_images);
cristyb5d5f722009-11-04 03:03:49 +00003464#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003465 #pragma omp parallel for schedule(static,4) shared(status)
cristy3ed852e2009-09-05 21:47:34 +00003466#endif
cristybb503372010-05-27 20:51:26 +00003467 for (y=0; y < (ssize_t) morph_images->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003468 {
3469 MagickBooleanType
3470 sync;
3471
cristy4c08aed2011-07-01 19:47:50 +00003472 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003473 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003474
cristybb503372010-05-27 20:51:26 +00003475 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003476 x;
3477
cristy4c08aed2011-07-01 19:47:50 +00003478 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003479 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003480
3481 if (status == MagickFalse)
3482 continue;
3483 p=GetCacheViewVirtualPixels(image_view,0,y,morph_image->columns,1,
3484 exception);
3485 q=GetCacheViewAuthenticPixels(morph_view,0,y,morph_images->columns,1,
3486 exception);
cristy4c08aed2011-07-01 19:47:50 +00003487 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003488 {
3489 status=MagickFalse;
3490 continue;
3491 }
cristybb503372010-05-27 20:51:26 +00003492 for (x=0; x < (ssize_t) morph_images->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003493 {
cristy1707c6c2012-01-18 23:30:54 +00003494 register ssize_t
3495 i;
3496
cristy10a6c612012-01-29 21:41:05 +00003497 for (i=0; i < (ssize_t) GetPixelChannels(morph_image); i++)
cristy1707c6c2012-01-18 23:30:54 +00003498 {
3499 PixelChannel
3500 channel;
3501
3502 PixelTrait
3503 morph_traits,
3504 traits;
3505
3506 channel=GetPixelChannelMapChannel(image,i);
3507 traits=GetPixelChannelMapTraits(image,channel);
3508 morph_traits=GetPixelChannelMapTraits(morph_image,channel);
3509 if ((traits == UndefinedPixelTrait) ||
3510 (morph_traits == UndefinedPixelTrait))
3511 continue;
cristyd09f8802012-02-04 16:44:10 +00003512 if (((morph_traits & CopyPixelTrait) != 0) ||
3513 (GetPixelMask(image,p) != 0))
cristy1707c6c2012-01-18 23:30:54 +00003514 {
3515 SetPixelChannel(morph_image,channel,p[i],q);
3516 continue;
3517 }
3518 SetPixelChannel(morph_image,channel,ClampToQuantum(alpha*
3519 GetPixelChannel(morph_images,channel,q)+beta*p[i]),q);
3520 }
cristyed231572011-07-14 02:18:59 +00003521 p+=GetPixelChannels(morph_image);
3522 q+=GetPixelChannels(morph_images);
cristy3ed852e2009-09-05 21:47:34 +00003523 }
3524 sync=SyncCacheViewAuthenticPixels(morph_view,exception);
3525 if (sync == MagickFalse)
3526 status=MagickFalse;
3527 }
3528 morph_view=DestroyCacheView(morph_view);
3529 image_view=DestroyCacheView(image_view);
3530 morph_image=DestroyImage(morph_image);
3531 }
cristybb503372010-05-27 20:51:26 +00003532 if (i < (ssize_t) number_frames)
cristy3ed852e2009-09-05 21:47:34 +00003533 break;
3534 /*
3535 Clone last frame in sequence.
3536 */
3537 morph_image=CloneImage(GetNextImageInList(next),0,0,MagickTrue,exception);
3538 if (morph_image == (Image *) NULL)
3539 {
3540 morph_images=DestroyImageList(morph_images);
3541 return((Image *) NULL);
3542 }
3543 AppendImageToList(&morph_images,morph_image);
3544 morph_images=GetLastImageInList(morph_images);
3545 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3546 {
3547 MagickBooleanType
3548 proceed;
3549
cristyb5d5f722009-11-04 03:03:49 +00003550#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003551 #pragma omp critical (MagickCore_MorphImages)
cristy3ed852e2009-09-05 21:47:34 +00003552#endif
3553 proceed=SetImageProgress(image,MorphImageTag,scene,
3554 GetImageListLength(image));
3555 if (proceed == MagickFalse)
3556 status=MagickFalse;
3557 }
3558 scene++;
3559 }
3560 if (GetNextImageInList(next) != (Image *) NULL)
3561 {
3562 morph_images=DestroyImageList(morph_images);
3563 return((Image *) NULL);
3564 }
3565 return(GetFirstImageInList(morph_images));
3566}
3567
3568/*
3569%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3570% %
3571% %
3572% %
3573% P l a s m a I m a g e %
3574% %
3575% %
3576% %
3577%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3578%
3579% PlasmaImage() initializes an image with plasma fractal values. The image
3580% must be initialized with a base color and the random number generator
3581% seeded before this method is called.
3582%
3583% The format of the PlasmaImage method is:
3584%
3585% MagickBooleanType PlasmaImage(Image *image,const SegmentInfo *segment,
cristy5cbc0162011-08-29 00:36:28 +00003586% size_t attenuate,size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003587%
3588% A description of each parameter follows:
3589%
3590% o image: the image.
3591%
3592% o segment: Define the region to apply plasma fractals values.
3593%
glennrp7dae1ca2010-09-16 12:17:35 +00003594% o attenuate: Define the plasma attenuation factor.
cristy3ed852e2009-09-05 21:47:34 +00003595%
3596% o depth: Limit the plasma recursion depth.
3597%
cristy5cbc0162011-08-29 00:36:28 +00003598% o exception: return any errors or warnings in this structure.
3599%
cristy3ed852e2009-09-05 21:47:34 +00003600*/
3601
3602static inline Quantum PlasmaPixel(RandomInfo *random_info,
3603 const MagickRealType pixel,const MagickRealType noise)
3604{
3605 Quantum
3606 plasma;
3607
cristyce70c172010-01-07 17:15:30 +00003608 plasma=ClampToQuantum(pixel+noise*GetPseudoRandomValue(random_info)-
cristy3ed852e2009-09-05 21:47:34 +00003609 noise/2.0);
3610 return(plasma);
3611}
3612
cristyda1f9c12011-10-02 21:39:49 +00003613static MagickBooleanType PlasmaImageProxy(Image *image,CacheView *image_view,
3614 CacheView *u_view,CacheView *v_view,RandomInfo *random_info,
3615 const SegmentInfo *segment,size_t attenuate,size_t depth,
3616 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003617{
cristy3ed852e2009-09-05 21:47:34 +00003618 MagickRealType
3619 plasma;
3620
cristyda1f9c12011-10-02 21:39:49 +00003621 PixelChannel
3622 channel;
3623
3624 PixelTrait
3625 traits;
3626
3627 register const Quantum
3628 *restrict u,
3629 *restrict v;
3630
3631 register Quantum
3632 *restrict q;
3633
3634 register ssize_t
3635 i;
cristy3ed852e2009-09-05 21:47:34 +00003636
cristy9d314ff2011-03-09 01:30:28 +00003637 ssize_t
3638 x,
3639 x_mid,
3640 y,
3641 y_mid;
3642
cristy3ed852e2009-09-05 21:47:34 +00003643 if (((segment->x2-segment->x1) == 0.0) && ((segment->y2-segment->y1) == 0.0))
3644 return(MagickTrue);
3645 if (depth != 0)
3646 {
3647 SegmentInfo
3648 local_info;
3649
3650 /*
3651 Divide the area into quadrants and recurse.
3652 */
3653 depth--;
3654 attenuate++;
cristybb503372010-05-27 20:51:26 +00003655 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3656 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003657 local_info=(*segment);
3658 local_info.x2=(double) x_mid;
3659 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003660 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3661 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003662 local_info=(*segment);
3663 local_info.y1=(double) y_mid;
3664 local_info.x2=(double) x_mid;
cristyda1f9c12011-10-02 21:39:49 +00003665 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3666 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003667 local_info=(*segment);
3668 local_info.x1=(double) x_mid;
3669 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003670 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3671 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003672 local_info=(*segment);
3673 local_info.x1=(double) x_mid;
3674 local_info.y1=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003675 return(PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3676 &local_info,attenuate,depth,exception));
cristy3ed852e2009-09-05 21:47:34 +00003677 }
cristybb503372010-05-27 20:51:26 +00003678 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3679 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003680 if ((segment->x1 == (double) x_mid) && (segment->x2 == (double) x_mid) &&
3681 (segment->y1 == (double) y_mid) && (segment->y2 == (double) y_mid))
3682 return(MagickFalse);
3683 /*
3684 Average pixels and apply plasma.
3685 */
cristy3ed852e2009-09-05 21:47:34 +00003686 plasma=(MagickRealType) QuantumRange/(2.0*attenuate);
3687 if ((segment->x1 != (double) x_mid) || (segment->x2 != (double) x_mid))
3688 {
cristy3ed852e2009-09-05 21:47:34 +00003689 /*
3690 Left pixel.
3691 */
cristybb503372010-05-27 20:51:26 +00003692 x=(ssize_t) ceil(segment->x1-0.5);
cristy1707c6c2012-01-18 23:30:54 +00003693 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),1,1,
3694 exception);
3695 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),1,1,
3696 exception);
cristyc5c6f662010-09-22 14:23:02 +00003697 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003698 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3699 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003700 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003701 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3702 {
cristye2a912b2011-12-05 20:02:07 +00003703 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003704 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003705 if (traits == UndefinedPixelTrait)
3706 continue;
3707 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3708 }
cristyc5c6f662010-09-22 14:23:02 +00003709 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003710 if (segment->x1 != segment->x2)
3711 {
3712 /*
3713 Right pixel.
3714 */
cristybb503372010-05-27 20:51:26 +00003715 x=(ssize_t) ceil(segment->x2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003716 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3717 1,1,exception);
3718 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3719 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003720 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003721 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3722 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003723 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003724 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3725 {
cristye2a912b2011-12-05 20:02:07 +00003726 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003727 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003728 if (traits == UndefinedPixelTrait)
3729 continue;
3730 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3731 }
cristyc5c6f662010-09-22 14:23:02 +00003732 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003733 }
3734 }
3735 if ((segment->y1 != (double) y_mid) || (segment->y2 != (double) y_mid))
3736 {
3737 if ((segment->x1 != (double) x_mid) || (segment->y2 != (double) y_mid))
3738 {
cristy3ed852e2009-09-05 21:47:34 +00003739 /*
3740 Bottom pixel.
3741 */
cristybb503372010-05-27 20:51:26 +00003742 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003743 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3744 1,1,exception);
3745 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3746 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003747 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003748 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3749 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003750 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003751 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3752 {
cristye2a912b2011-12-05 20:02:07 +00003753 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003754 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003755 if (traits == UndefinedPixelTrait)
3756 continue;
3757 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3758 }
cristyc5c6f662010-09-22 14:23:02 +00003759 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003760 }
3761 if (segment->y1 != segment->y2)
3762 {
cristy3ed852e2009-09-05 21:47:34 +00003763 /*
3764 Top pixel.
3765 */
cristybb503372010-05-27 20:51:26 +00003766 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003767 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3768 1,1,exception);
3769 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3770 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003771 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003772 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3773 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003774 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003775 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3776 {
cristye2a912b2011-12-05 20:02:07 +00003777 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003778 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003779 if (traits == UndefinedPixelTrait)
3780 continue;
3781 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3782 }
cristyc5c6f662010-09-22 14:23:02 +00003783 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003784 }
3785 }
3786 if ((segment->x1 != segment->x2) || (segment->y1 != segment->y2))
3787 {
cristy3ed852e2009-09-05 21:47:34 +00003788 /*
3789 Middle pixel.
3790 */
cristybb503372010-05-27 20:51:26 +00003791 x=(ssize_t) ceil(segment->x1-0.5);
3792 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003793 u=GetCacheViewVirtualPixels(u_view,x,y,1,1,exception);
cristybb503372010-05-27 20:51:26 +00003794 x=(ssize_t) ceil(segment->x2-0.5);
3795 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003796 v=GetCacheViewVirtualPixels(v_view,x,y,1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003797 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003798 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3799 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003800 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003801 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3802 {
cristye2a912b2011-12-05 20:02:07 +00003803 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003804 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003805 if (traits == UndefinedPixelTrait)
3806 continue;
3807 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3808 }
cristyc5c6f662010-09-22 14:23:02 +00003809 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003810 }
3811 if (((segment->x2-segment->x1) < 3.0) && ((segment->y2-segment->y1) < 3.0))
3812 return(MagickTrue);
3813 return(MagickFalse);
3814}
cristyda1f9c12011-10-02 21:39:49 +00003815
cristy3ed852e2009-09-05 21:47:34 +00003816MagickExport MagickBooleanType PlasmaImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00003817 const SegmentInfo *segment,size_t attenuate,size_t depth,
3818 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003819{
cristyc5c6f662010-09-22 14:23:02 +00003820 CacheView
cristyda1f9c12011-10-02 21:39:49 +00003821 *image_view,
3822 *u_view,
3823 *v_view;
cristyc5c6f662010-09-22 14:23:02 +00003824
cristy3ed852e2009-09-05 21:47:34 +00003825 MagickBooleanType
3826 status;
3827
3828 RandomInfo
3829 *random_info;
3830
3831 if (image->debug != MagickFalse)
3832 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3833 assert(image != (Image *) NULL);
3834 assert(image->signature == MagickSignature);
3835 if (image->debug != MagickFalse)
3836 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
cristy5cbc0162011-08-29 00:36:28 +00003837 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristyc5c6f662010-09-22 14:23:02 +00003838 return(MagickFalse);
3839 image_view=AcquireCacheView(image);
cristyda1f9c12011-10-02 21:39:49 +00003840 u_view=AcquireCacheView(image);
3841 v_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00003842 random_info=AcquireRandomInfo();
cristyda1f9c12011-10-02 21:39:49 +00003843 status=PlasmaImageProxy(image,image_view,u_view,v_view,random_info,segment,
3844 attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003845 random_info=DestroyRandomInfo(random_info);
cristyda1f9c12011-10-02 21:39:49 +00003846 v_view=DestroyCacheView(v_view);
3847 u_view=DestroyCacheView(u_view);
cristyc5c6f662010-09-22 14:23:02 +00003848 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003849 return(status);
3850}
3851
3852/*
3853%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3854% %
3855% %
3856% %
3857% P o l a r o i d I m a g e %
3858% %
3859% %
3860% %
3861%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3862%
3863% PolaroidImage() simulates a Polaroid picture.
3864%
3865% The format of the AnnotateImage method is:
3866%
3867% Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristye9e3d382011-12-14 01:50:13 +00003868% const char *caption,const double angle,
3869% const PixelInterpolateMethod method,ExceptionInfo exception)
cristy3ed852e2009-09-05 21:47:34 +00003870%
3871% A description of each parameter follows:
3872%
3873% o image: the image.
3874%
3875% o draw_info: the draw info.
3876%
cristye9e3d382011-12-14 01:50:13 +00003877% o caption: the Polaroid caption.
3878%
cristycee97112010-05-28 00:44:52 +00003879% o angle: Apply the effect along this angle.
cristy3ed852e2009-09-05 21:47:34 +00003880%
cristy5c4e2582011-09-11 19:21:03 +00003881% o method: the pixel interpolation method.
3882%
cristy3ed852e2009-09-05 21:47:34 +00003883% o exception: return any errors or warnings in this structure.
3884%
3885*/
3886MagickExport Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristye9e3d382011-12-14 01:50:13 +00003887 const char *caption,const double angle,const PixelInterpolateMethod method,
cristy5c4e2582011-09-11 19:21:03 +00003888 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003889{
cristy3ed852e2009-09-05 21:47:34 +00003890 Image
3891 *bend_image,
3892 *caption_image,
3893 *flop_image,
3894 *picture_image,
3895 *polaroid_image,
3896 *rotate_image,
3897 *trim_image;
3898
cristybb503372010-05-27 20:51:26 +00003899 size_t
cristy3ed852e2009-09-05 21:47:34 +00003900 height;
3901
cristy9d314ff2011-03-09 01:30:28 +00003902 ssize_t
3903 quantum;
3904
cristy3ed852e2009-09-05 21:47:34 +00003905 /*
3906 Simulate a Polaroid picture.
3907 */
3908 assert(image != (Image *) NULL);
3909 assert(image->signature == MagickSignature);
3910 if (image->debug != MagickFalse)
3911 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3912 assert(exception != (ExceptionInfo *) NULL);
3913 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00003914 quantum=(ssize_t) MagickMax(MagickMax((double) image->columns,(double)
cristy3ed852e2009-09-05 21:47:34 +00003915 image->rows)/25.0,10.0);
3916 height=image->rows+2*quantum;
3917 caption_image=(Image *) NULL;
cristye9e3d382011-12-14 01:50:13 +00003918 if (caption != (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003919 {
3920 char
cristye9e3d382011-12-14 01:50:13 +00003921 geometry[MaxTextExtent],
3922 *text;
cristy3ed852e2009-09-05 21:47:34 +00003923
3924 DrawInfo
3925 *annotate_info;
3926
cristy3ed852e2009-09-05 21:47:34 +00003927 MagickBooleanType
3928 status;
3929
cristy9d314ff2011-03-09 01:30:28 +00003930 ssize_t
3931 count;
3932
cristy3ed852e2009-09-05 21:47:34 +00003933 TypeMetric
3934 metrics;
3935
3936 /*
3937 Generate caption image.
3938 */
3939 caption_image=CloneImage(image,image->columns,1,MagickTrue,exception);
3940 if (caption_image == (Image *) NULL)
3941 return((Image *) NULL);
3942 annotate_info=CloneDrawInfo((const ImageInfo *) NULL,draw_info);
cristye9e3d382011-12-14 01:50:13 +00003943 text=InterpretImageProperties((ImageInfo *) NULL,(Image *) image,caption,
3944 exception);
3945 (void) CloneString(&annotate_info->text,text);
cristy6b1d05e2010-09-22 19:17:27 +00003946 count=FormatMagickCaption(caption_image,annotate_info,MagickTrue,&metrics,
cristye9e3d382011-12-14 01:50:13 +00003947 &text,exception);
3948 status=SetImageExtent(caption_image,image->columns,(size_t) ((count+1)*
3949 (metrics.ascent-metrics.descent)+0.5),exception);
cristy3ed852e2009-09-05 21:47:34 +00003950 if (status == MagickFalse)
3951 caption_image=DestroyImage(caption_image);
3952 else
3953 {
3954 caption_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00003955 (void) SetImageBackgroundColor(caption_image,exception);
cristye9e3d382011-12-14 01:50:13 +00003956 (void) CloneString(&annotate_info->text,text);
cristyb51dff52011-05-19 16:55:47 +00003957 (void) FormatLocaleString(geometry,MaxTextExtent,"+0+%g",
cristy3ed852e2009-09-05 21:47:34 +00003958 metrics.ascent);
3959 if (annotate_info->gravity == UndefinedGravity)
3960 (void) CloneString(&annotate_info->geometry,AcquireString(
3961 geometry));
cristy5cbc0162011-08-29 00:36:28 +00003962 (void) AnnotateImage(caption_image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00003963 height+=caption_image->rows;
3964 }
3965 annotate_info=DestroyDrawInfo(annotate_info);
cristye9e3d382011-12-14 01:50:13 +00003966 text=DestroyString(text);
cristy3ed852e2009-09-05 21:47:34 +00003967 }
3968 picture_image=CloneImage(image,image->columns+2*quantum,height,MagickTrue,
3969 exception);
3970 if (picture_image == (Image *) NULL)
3971 {
3972 if (caption_image != (Image *) NULL)
3973 caption_image=DestroyImage(caption_image);
3974 return((Image *) NULL);
3975 }
3976 picture_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00003977 (void) SetImageBackgroundColor(picture_image,exception);
cristy39172402012-03-30 13:04:39 +00003978 (void) CompositeImage(picture_image,image,OverCompositeOp,MagickTrue,quantum,
cristyfeb3e962012-03-29 17:25:55 +00003979 quantum,exception);
cristy3ed852e2009-09-05 21:47:34 +00003980 if (caption_image != (Image *) NULL)
3981 {
cristyfeb3e962012-03-29 17:25:55 +00003982 (void) CompositeImage(picture_image,caption_image,OverCompositeOp,
cristy39172402012-03-30 13:04:39 +00003983 MagickTrue,quantum,(ssize_t) (image->rows+3*quantum/2),exception);
cristy3ed852e2009-09-05 21:47:34 +00003984 caption_image=DestroyImage(caption_image);
3985 }
cristy9950d572011-10-01 18:22:35 +00003986 (void) QueryColorCompliance("none",AllCompliance,
3987 &picture_image->background_color,exception);
cristy63240882011-08-05 19:05:27 +00003988 (void) SetImageAlphaChannel(picture_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00003989 rotate_image=RotateImage(picture_image,90.0,exception);
3990 picture_image=DestroyImage(picture_image);
3991 if (rotate_image == (Image *) NULL)
3992 return((Image *) NULL);
3993 picture_image=rotate_image;
3994 bend_image=WaveImage(picture_image,0.01*picture_image->rows,2.0*
cristy5c4e2582011-09-11 19:21:03 +00003995 picture_image->columns,method,exception);
cristy3ed852e2009-09-05 21:47:34 +00003996 picture_image=DestroyImage(picture_image);
3997 if (bend_image == (Image *) NULL)
3998 return((Image *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00003999 picture_image=bend_image;
4000 rotate_image=RotateImage(picture_image,-90.0,exception);
4001 picture_image=DestroyImage(picture_image);
4002 if (rotate_image == (Image *) NULL)
4003 return((Image *) NULL);
4004 picture_image=rotate_image;
4005 picture_image->background_color=image->background_color;
anthonyf46d4262012-03-26 03:30:34 +00004006 polaroid_image=ShadowImage(picture_image,80.0,2.0,quantum/3,quantum/3,
cristy3ed852e2009-09-05 21:47:34 +00004007 exception);
4008 if (polaroid_image == (Image *) NULL)
4009 {
4010 picture_image=DestroyImage(picture_image);
4011 return(picture_image);
4012 }
4013 flop_image=FlopImage(polaroid_image,exception);
4014 polaroid_image=DestroyImage(polaroid_image);
4015 if (flop_image == (Image *) NULL)
4016 {
4017 picture_image=DestroyImage(picture_image);
4018 return(picture_image);
4019 }
4020 polaroid_image=flop_image;
cristyfeb3e962012-03-29 17:25:55 +00004021 (void) CompositeImage(polaroid_image,picture_image,OverCompositeOp,
cristy39172402012-03-30 13:04:39 +00004022 MagickTrue,(ssize_t) (-0.01*picture_image->columns/2.0),0L,exception);
cristy3ed852e2009-09-05 21:47:34 +00004023 picture_image=DestroyImage(picture_image);
cristy9950d572011-10-01 18:22:35 +00004024 (void) QueryColorCompliance("none",AllCompliance,
4025 &polaroid_image->background_color,exception);
cristy3ed852e2009-09-05 21:47:34 +00004026 rotate_image=RotateImage(polaroid_image,angle,exception);
4027 polaroid_image=DestroyImage(polaroid_image);
4028 if (rotate_image == (Image *) NULL)
4029 return((Image *) NULL);
4030 polaroid_image=rotate_image;
4031 trim_image=TrimImage(polaroid_image,exception);
4032 polaroid_image=DestroyImage(polaroid_image);
4033 if (trim_image == (Image *) NULL)
4034 return((Image *) NULL);
4035 polaroid_image=trim_image;
4036 return(polaroid_image);
4037}
4038
4039/*
4040%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4041% %
4042% %
4043% %
cristy3ed852e2009-09-05 21:47:34 +00004044% S e p i a T o n e I m a g e %
4045% %
4046% %
4047% %
4048%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4049%
4050% MagickSepiaToneImage() applies a special effect to the image, similar to the
4051% effect achieved in a photo darkroom by sepia toning. Threshold ranges from
4052% 0 to QuantumRange and is a measure of the extent of the sepia toning. A
4053% threshold of 80% is a good starting point for a reasonable tone.
4054%
4055% The format of the SepiaToneImage method is:
4056%
4057% Image *SepiaToneImage(const Image *image,const double threshold,
4058% ExceptionInfo *exception)
4059%
4060% A description of each parameter follows:
4061%
4062% o image: the image.
4063%
4064% o threshold: the tone threshold.
4065%
4066% o exception: return any errors or warnings in this structure.
4067%
4068*/
4069MagickExport Image *SepiaToneImage(const Image *image,const double threshold,
4070 ExceptionInfo *exception)
4071{
4072#define SepiaToneImageTag "SepiaTone/Image"
4073
cristyc4c8d132010-01-07 01:58:38 +00004074 CacheView
4075 *image_view,
4076 *sepia_view;
4077
cristy3ed852e2009-09-05 21:47:34 +00004078 Image
4079 *sepia_image;
4080
cristy3ed852e2009-09-05 21:47:34 +00004081 MagickBooleanType
4082 status;
4083
cristybb503372010-05-27 20:51:26 +00004084 MagickOffsetType
4085 progress;
4086
4087 ssize_t
4088 y;
4089
cristy3ed852e2009-09-05 21:47:34 +00004090 /*
4091 Initialize sepia-toned image attributes.
4092 */
4093 assert(image != (const Image *) NULL);
4094 assert(image->signature == MagickSignature);
4095 if (image->debug != MagickFalse)
4096 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4097 assert(exception != (ExceptionInfo *) NULL);
4098 assert(exception->signature == MagickSignature);
cristy1707c6c2012-01-18 23:30:54 +00004099 sepia_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004100 if (sepia_image == (Image *) NULL)
4101 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004102 if (SetImageStorageClass(sepia_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004103 {
cristy3ed852e2009-09-05 21:47:34 +00004104 sepia_image=DestroyImage(sepia_image);
4105 return((Image *) NULL);
4106 }
4107 /*
4108 Tone each row of the image.
4109 */
4110 status=MagickTrue;
4111 progress=0;
4112 image_view=AcquireCacheView(image);
4113 sepia_view=AcquireCacheView(sepia_image);
cristyb5d5f722009-11-04 03:03:49 +00004114#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00004115 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00004116#endif
cristybb503372010-05-27 20:51:26 +00004117 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004118 {
cristy4c08aed2011-07-01 19:47:50 +00004119 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004120 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00004121
cristybb503372010-05-27 20:51:26 +00004122 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004123 x;
4124
cristy4c08aed2011-07-01 19:47:50 +00004125 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004126 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004127
4128 if (status == MagickFalse)
4129 continue;
4130 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy1707c6c2012-01-18 23:30:54 +00004131 q=GetCacheViewAuthenticPixels(sepia_view,0,y,sepia_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00004132 exception);
cristy4c08aed2011-07-01 19:47:50 +00004133 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004134 {
4135 status=MagickFalse;
4136 continue;
4137 }
cristybb503372010-05-27 20:51:26 +00004138 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004139 {
4140 MagickRealType
4141 intensity,
4142 tone;
4143
cristy4c08aed2011-07-01 19:47:50 +00004144 intensity=(MagickRealType) GetPixelIntensity(image,p);
cristy3ed852e2009-09-05 21:47:34 +00004145 tone=intensity > threshold ? (MagickRealType) QuantumRange : intensity+
4146 (MagickRealType) QuantumRange-threshold;
cristy4c08aed2011-07-01 19:47:50 +00004147 SetPixelRed(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004148 tone=intensity > (7.0*threshold/6.0) ? (MagickRealType) QuantumRange :
4149 intensity+(MagickRealType) QuantumRange-7.0*threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004150 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004151 tone=intensity < (threshold/6.0) ? 0 : intensity-threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004152 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004153 tone=threshold/7.0;
cristy4c08aed2011-07-01 19:47:50 +00004154 if ((MagickRealType) GetPixelGreen(image,q) < tone)
4155 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
4156 if ((MagickRealType) GetPixelBlue(image,q) < tone)
4157 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristyed231572011-07-14 02:18:59 +00004158 p+=GetPixelChannels(image);
4159 q+=GetPixelChannels(sepia_image);
cristy3ed852e2009-09-05 21:47:34 +00004160 }
4161 if (SyncCacheViewAuthenticPixels(sepia_view,exception) == MagickFalse)
4162 status=MagickFalse;
4163 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4164 {
4165 MagickBooleanType
4166 proceed;
4167
cristyb5d5f722009-11-04 03:03:49 +00004168#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00004169 #pragma omp critical (MagickCore_SepiaToneImage)
cristy3ed852e2009-09-05 21:47:34 +00004170#endif
4171 proceed=SetImageProgress(image,SepiaToneImageTag,progress++,
4172 image->rows);
4173 if (proceed == MagickFalse)
4174 status=MagickFalse;
4175 }
4176 }
4177 sepia_view=DestroyCacheView(sepia_view);
4178 image_view=DestroyCacheView(image_view);
cristye23ec9d2011-08-16 18:15:40 +00004179 (void) NormalizeImage(sepia_image,exception);
4180 (void) ContrastImage(sepia_image,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004181 if (status == MagickFalse)
4182 sepia_image=DestroyImage(sepia_image);
4183 return(sepia_image);
4184}
4185
4186/*
4187%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4188% %
4189% %
4190% %
4191% S h a d o w I m a g e %
4192% %
4193% %
4194% %
4195%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4196%
4197% ShadowImage() simulates a shadow from the specified image and returns it.
4198%
4199% The format of the ShadowImage method is:
4200%
cristy70cddf72011-12-10 22:42:42 +00004201% Image *ShadowImage(const Image *image,const double alpha,
cristyaa2c16c2012-03-25 22:21:35 +00004202% const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4203% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004204%
4205% A description of each parameter follows:
4206%
4207% o image: the image.
4208%
cristy70cddf72011-12-10 22:42:42 +00004209% o alpha: percentage transparency.
cristy3ed852e2009-09-05 21:47:34 +00004210%
4211% o sigma: the standard deviation of the Gaussian, in pixels.
4212%
4213% o x_offset: the shadow x-offset.
4214%
4215% o y_offset: the shadow y-offset.
4216%
4217% o exception: return any errors or warnings in this structure.
4218%
4219*/
cristy70cddf72011-12-10 22:42:42 +00004220MagickExport Image *ShadowImage(const Image *image,const double alpha,
cristyaa2c16c2012-03-25 22:21:35 +00004221 const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4222 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004223{
4224#define ShadowImageTag "Shadow/Image"
4225
cristy70cddf72011-12-10 22:42:42 +00004226 CacheView
4227 *image_view;
4228
cristybd5a96c2011-08-21 00:04:26 +00004229 ChannelType
4230 channel_mask;
4231
cristy3ed852e2009-09-05 21:47:34 +00004232 Image
4233 *border_image,
4234 *clone_image,
4235 *shadow_image;
4236
cristy70cddf72011-12-10 22:42:42 +00004237 MagickBooleanType
4238 status;
4239
cristy3ed852e2009-09-05 21:47:34 +00004240 RectangleInfo
4241 border_info;
4242
cristy70cddf72011-12-10 22:42:42 +00004243 ssize_t
4244 y;
4245
cristy3ed852e2009-09-05 21:47:34 +00004246 assert(image != (Image *) NULL);
4247 assert(image->signature == MagickSignature);
4248 if (image->debug != MagickFalse)
4249 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4250 assert(exception != (ExceptionInfo *) NULL);
4251 assert(exception->signature == MagickSignature);
4252 clone_image=CloneImage(image,0,0,MagickTrue,exception);
4253 if (clone_image == (Image *) NULL)
4254 return((Image *) NULL);
cristy0898eba2012-04-09 16:38:29 +00004255 if (IsGrayColorspace(image->colorspace) != MagickFalse)
4256 (void) TransformImageColorspace(clone_image,sRGBColorspace,exception);
cristy387430f2012-02-07 13:09:46 +00004257 (void) SetImageVirtualPixelMethod(clone_image,EdgeVirtualPixelMethod,
4258 exception);
cristybb503372010-05-27 20:51:26 +00004259 border_info.width=(size_t) floor(2.0*sigma+0.5);
4260 border_info.height=(size_t) floor(2.0*sigma+0.5);
cristy3ed852e2009-09-05 21:47:34 +00004261 border_info.x=0;
4262 border_info.y=0;
cristy9950d572011-10-01 18:22:35 +00004263 (void) QueryColorCompliance("none",AllCompliance,&clone_image->border_color,
4264 exception);
cristy70cddf72011-12-10 22:42:42 +00004265 clone_image->matte=MagickTrue;
4266 border_image=BorderImage(clone_image,&border_info,OverCompositeOp,exception);
cristy3ed852e2009-09-05 21:47:34 +00004267 clone_image=DestroyImage(clone_image);
4268 if (border_image == (Image *) NULL)
4269 return((Image *) NULL);
4270 if (border_image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00004271 (void) SetImageAlphaChannel(border_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004272 /*
4273 Shadow image.
4274 */
cristy70cddf72011-12-10 22:42:42 +00004275 status=MagickTrue;
4276 image_view=AcquireCacheView(border_image);
4277 for (y=0; y < (ssize_t) border_image->rows; y++)
4278 {
4279 PixelInfo
4280 background_color;
4281
4282 register Quantum
4283 *restrict q;
4284
4285 register ssize_t
4286 x;
4287
4288 if (status == MagickFalse)
4289 continue;
4290 q=QueueCacheViewAuthenticPixels(image_view,0,y,border_image->columns,1,
4291 exception);
4292 if (q == (Quantum *) NULL)
4293 {
4294 status=MagickFalse;
4295 continue;
4296 }
4297 background_color=border_image->background_color;
4298 background_color.matte=MagickTrue;
4299 for (x=0; x < (ssize_t) border_image->columns; x++)
4300 {
4301 if (border_image->matte != MagickFalse)
4302 background_color.alpha=GetPixelAlpha(border_image,q)*alpha/100.0;
4303 SetPixelInfoPixel(border_image,&background_color,q);
4304 q+=GetPixelChannels(border_image);
4305 }
4306 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4307 status=MagickFalse;
4308 }
4309 image_view=DestroyCacheView(image_view);
4310 if (status == MagickFalse)
4311 {
4312 border_image=DestroyImage(border_image);
4313 return((Image *) NULL);
4314 }
cristybd5a96c2011-08-21 00:04:26 +00004315 channel_mask=SetPixelChannelMask(border_image,AlphaChannel);
cristyaa2c16c2012-03-25 22:21:35 +00004316 shadow_image=BlurImage(border_image,0.0,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +00004317 border_image=DestroyImage(border_image);
4318 if (shadow_image == (Image *) NULL)
4319 return((Image *) NULL);
cristyae1969f2011-12-10 03:07:36 +00004320 (void) SetPixelChannelMapMask(shadow_image,channel_mask);
cristy3ed852e2009-09-05 21:47:34 +00004321 if (shadow_image->page.width == 0)
4322 shadow_image->page.width=shadow_image->columns;
4323 if (shadow_image->page.height == 0)
4324 shadow_image->page.height=shadow_image->rows;
cristybb503372010-05-27 20:51:26 +00004325 shadow_image->page.width+=x_offset-(ssize_t) border_info.width;
4326 shadow_image->page.height+=y_offset-(ssize_t) border_info.height;
4327 shadow_image->page.x+=x_offset-(ssize_t) border_info.width;
4328 shadow_image->page.y+=y_offset-(ssize_t) border_info.height;
cristy3ed852e2009-09-05 21:47:34 +00004329 return(shadow_image);
4330}
4331
4332/*
4333%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4334% %
4335% %
4336% %
4337% S k e t c h I m a g e %
4338% %
4339% %
4340% %
4341%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4342%
4343% SketchImage() simulates a pencil sketch. We convolve the image with a
4344% Gaussian operator of the given radius and standard deviation (sigma). For
4345% reasonable results, radius should be larger than sigma. Use a radius of 0
4346% and SketchImage() selects a suitable radius for you. Angle gives the angle
4347% of the sketch.
4348%
4349% The format of the SketchImage method is:
4350%
4351% Image *SketchImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00004352% const double sigma,const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004353%
4354% A description of each parameter follows:
4355%
4356% o image: the image.
4357%
cristy574cc262011-08-05 01:23:58 +00004358% o radius: the radius of the Gaussian, in pixels, not counting the
4359% center pixel.
cristy3ed852e2009-09-05 21:47:34 +00004360%
4361% o sigma: the standard deviation of the Gaussian, in pixels.
4362%
cristyf7ef0252011-09-09 14:50:06 +00004363% o angle: apply the effect along this angle.
4364%
cristy3ed852e2009-09-05 21:47:34 +00004365% o exception: return any errors or warnings in this structure.
4366%
4367*/
4368MagickExport Image *SketchImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00004369 const double sigma,const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004370{
cristyfa112112010-01-04 17:48:07 +00004371 CacheView
4372 *random_view;
4373
cristy3ed852e2009-09-05 21:47:34 +00004374 Image
4375 *blend_image,
4376 *blur_image,
4377 *dodge_image,
4378 *random_image,
4379 *sketch_image;
4380
cristy3ed852e2009-09-05 21:47:34 +00004381 MagickBooleanType
4382 status;
4383
cristy3ed852e2009-09-05 21:47:34 +00004384 RandomInfo
cristyfa112112010-01-04 17:48:07 +00004385 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00004386
cristy9d314ff2011-03-09 01:30:28 +00004387 ssize_t
4388 y;
4389
cristy3ed852e2009-09-05 21:47:34 +00004390 /*
4391 Sketch image.
4392 */
4393 random_image=CloneImage(image,image->columns << 1,image->rows << 1,
4394 MagickTrue,exception);
4395 if (random_image == (Image *) NULL)
4396 return((Image *) NULL);
4397 status=MagickTrue;
cristy1b784432009-12-19 02:20:40 +00004398 random_info=AcquireRandomInfoThreadSet();
cristy3ed852e2009-09-05 21:47:34 +00004399 random_view=AcquireCacheView(random_image);
cristy1b784432009-12-19 02:20:40 +00004400#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00004401 #pragma omp parallel for schedule(static,4) shared(status)
cristy1b784432009-12-19 02:20:40 +00004402#endif
cristybb503372010-05-27 20:51:26 +00004403 for (y=0; y < (ssize_t) random_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004404 {
cristy5c9e6f22010-09-17 17:31:01 +00004405 const int
4406 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00004407
cristybb503372010-05-27 20:51:26 +00004408 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004409 x;
4410
cristy4c08aed2011-07-01 19:47:50 +00004411 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004412 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004413
cristy1b784432009-12-19 02:20:40 +00004414 if (status == MagickFalse)
4415 continue;
cristy3ed852e2009-09-05 21:47:34 +00004416 q=QueueCacheViewAuthenticPixels(random_view,0,y,random_image->columns,1,
4417 exception);
cristyacd2ed22011-08-30 01:44:23 +00004418 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004419 {
4420 status=MagickFalse;
4421 continue;
4422 }
cristybb503372010-05-27 20:51:26 +00004423 for (x=0; x < (ssize_t) random_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004424 {
cristy76f512e2011-09-12 01:26:56 +00004425 MagickRealType
4426 value;
4427
4428 register ssize_t
4429 i;
4430
cristy10a6c612012-01-29 21:41:05 +00004431 if (GetPixelMask(random_image,q) != 0)
4432 {
4433 q+=GetPixelChannels(random_image);
4434 continue;
4435 }
cristy76f512e2011-09-12 01:26:56 +00004436 value=GetPseudoRandomValue(random_info[id]);
4437 for (i=0; i < (ssize_t) GetPixelChannels(random_image); i++)
4438 {
cristyabace412011-12-11 15:56:53 +00004439 PixelChannel
4440 channel;
4441
cristy76f512e2011-09-12 01:26:56 +00004442 PixelTrait
4443 traits;
4444
cristyabace412011-12-11 15:56:53 +00004445 channel=GetPixelChannelMapChannel(image,i);
4446 traits=GetPixelChannelMapTraits(image,channel);
cristy76f512e2011-09-12 01:26:56 +00004447 if (traits == UndefinedPixelTrait)
4448 continue;
4449 q[i]=ClampToQuantum(QuantumRange*value);
4450 }
cristyed231572011-07-14 02:18:59 +00004451 q+=GetPixelChannels(random_image);
cristy3ed852e2009-09-05 21:47:34 +00004452 }
4453 if (SyncCacheViewAuthenticPixels(random_view,exception) == MagickFalse)
4454 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004455 }
4456 random_view=DestroyCacheView(random_view);
cristy1b784432009-12-19 02:20:40 +00004457 random_info=DestroyRandomInfoThreadSet(random_info);
cristy3ed852e2009-09-05 21:47:34 +00004458 if (status == MagickFalse)
4459 {
4460 random_image=DestroyImage(random_image);
4461 return(random_image);
4462 }
cristyaa2c16c2012-03-25 22:21:35 +00004463 blur_image=MotionBlurImage(random_image,radius,sigma,angle,exception);
cristy3ed852e2009-09-05 21:47:34 +00004464 random_image=DestroyImage(random_image);
4465 if (blur_image == (Image *) NULL)
4466 return((Image *) NULL);
cristy6bfd6902011-12-09 01:33:45 +00004467 dodge_image=EdgeImage(blur_image,radius,1.0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004468 blur_image=DestroyImage(blur_image);
4469 if (dodge_image == (Image *) NULL)
4470 return((Image *) NULL);
cristye23ec9d2011-08-16 18:15:40 +00004471 (void) NormalizeImage(dodge_image,exception);
cristyb3e7c6c2011-07-24 01:43:55 +00004472 (void) NegateImage(dodge_image,MagickFalse,exception);
cristye941a752011-10-15 01:52:48 +00004473 (void) TransformImage(&dodge_image,(char *) NULL,"50%",exception);
cristy3ed852e2009-09-05 21:47:34 +00004474 sketch_image=CloneImage(image,0,0,MagickTrue,exception);
4475 if (sketch_image == (Image *) NULL)
4476 {
4477 dodge_image=DestroyImage(dodge_image);
4478 return((Image *) NULL);
4479 }
cristyfeb3e962012-03-29 17:25:55 +00004480 (void) CompositeImage(sketch_image,dodge_image,ColorDodgeCompositeOp,
cristy39172402012-03-30 13:04:39 +00004481 MagickTrue,0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004482 dodge_image=DestroyImage(dodge_image);
4483 blend_image=CloneImage(image,0,0,MagickTrue,exception);
4484 if (blend_image == (Image *) NULL)
4485 {
4486 sketch_image=DestroyImage(sketch_image);
4487 return((Image *) NULL);
4488 }
4489 (void) SetImageArtifact(blend_image,"compose:args","20x80");
cristy39172402012-03-30 13:04:39 +00004490 (void) CompositeImage(sketch_image,blend_image,BlendCompositeOp,MagickTrue,
cristyfeb3e962012-03-29 17:25:55 +00004491 0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004492 blend_image=DestroyImage(blend_image);
4493 return(sketch_image);
4494}
4495
4496/*
4497%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4498% %
4499% %
4500% %
4501% S o l a r i z e I m a g e %
4502% %
4503% %
4504% %
4505%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4506%
4507% SolarizeImage() applies a special effect to the image, similar to the effect
4508% achieved in a photo darkroom by selectively exposing areas of photo
4509% sensitive paper to light. Threshold ranges from 0 to QuantumRange and is a
4510% measure of the extent of the solarization.
4511%
4512% The format of the SolarizeImage method is:
4513%
cristy5cbc0162011-08-29 00:36:28 +00004514% MagickBooleanType SolarizeImage(Image *image,const double threshold,
4515% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004516%
4517% A description of each parameter follows:
4518%
4519% o image: the image.
4520%
4521% o threshold: Define the extent of the solarization.
4522%
cristy5cbc0162011-08-29 00:36:28 +00004523% o exception: return any errors or warnings in this structure.
4524%
cristy3ed852e2009-09-05 21:47:34 +00004525*/
4526MagickExport MagickBooleanType SolarizeImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00004527 const double threshold,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004528{
4529#define SolarizeImageTag "Solarize/Image"
4530
cristyc4c8d132010-01-07 01:58:38 +00004531 CacheView
4532 *image_view;
4533
cristy3ed852e2009-09-05 21:47:34 +00004534 MagickBooleanType
4535 status;
4536
cristybb503372010-05-27 20:51:26 +00004537 MagickOffsetType
4538 progress;
4539
4540 ssize_t
4541 y;
4542
cristy3ed852e2009-09-05 21:47:34 +00004543 assert(image != (Image *) NULL);
4544 assert(image->signature == MagickSignature);
4545 if (image->debug != MagickFalse)
4546 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4547 if (image->storage_class == PseudoClass)
4548 {
cristybb503372010-05-27 20:51:26 +00004549 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004550 i;
4551
4552 /*
4553 Solarize colormap.
4554 */
cristybb503372010-05-27 20:51:26 +00004555 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00004556 {
4557 if ((MagickRealType) image->colormap[i].red > threshold)
4558 image->colormap[i].red=(Quantum) QuantumRange-image->colormap[i].red;
4559 if ((MagickRealType) image->colormap[i].green > threshold)
4560 image->colormap[i].green=(Quantum) QuantumRange-
4561 image->colormap[i].green;
4562 if ((MagickRealType) image->colormap[i].blue > threshold)
4563 image->colormap[i].blue=(Quantum) QuantumRange-
4564 image->colormap[i].blue;
4565 }
4566 }
4567 /*
4568 Solarize image.
4569 */
4570 status=MagickTrue;
4571 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00004572 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00004573#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00004574 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00004575#endif
cristybb503372010-05-27 20:51:26 +00004576 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004577 {
cristybb503372010-05-27 20:51:26 +00004578 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004579 x;
4580
cristy4c08aed2011-07-01 19:47:50 +00004581 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004582 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004583
4584 if (status == MagickFalse)
4585 continue;
cristy5cbc0162011-08-29 00:36:28 +00004586 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00004587 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004588 {
4589 status=MagickFalse;
4590 continue;
4591 }
cristybb503372010-05-27 20:51:26 +00004592 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004593 {
cristy76f512e2011-09-12 01:26:56 +00004594 register ssize_t
4595 i;
4596
cristy10a6c612012-01-29 21:41:05 +00004597 if (GetPixelMask(image,q) != 0)
4598 {
4599 q+=GetPixelChannels(image);
4600 continue;
4601 }
cristy76f512e2011-09-12 01:26:56 +00004602 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4603 {
cristyabace412011-12-11 15:56:53 +00004604 PixelChannel
4605 channel;
4606
cristy76f512e2011-09-12 01:26:56 +00004607 PixelTrait
4608 traits;
4609
cristyabace412011-12-11 15:56:53 +00004610 channel=GetPixelChannelMapChannel(image,i);
4611 traits=GetPixelChannelMapTraits(image,channel);
cristy76f512e2011-09-12 01:26:56 +00004612 if ((traits == UndefinedPixelTrait) ||
4613 ((traits & CopyPixelTrait) != 0))
4614 continue;
4615 if ((MagickRealType) q[i] > threshold)
4616 q[i]=QuantumRange-q[i];
4617 }
cristyed231572011-07-14 02:18:59 +00004618 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00004619 }
4620 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4621 status=MagickFalse;
4622 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4623 {
4624 MagickBooleanType
4625 proceed;
4626
cristyb5d5f722009-11-04 03:03:49 +00004627#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00004628 #pragma omp critical (MagickCore_SolarizeImage)
cristy3ed852e2009-09-05 21:47:34 +00004629#endif
4630 proceed=SetImageProgress(image,SolarizeImageTag,progress++,image->rows);
4631 if (proceed == MagickFalse)
4632 status=MagickFalse;
4633 }
4634 }
4635 image_view=DestroyCacheView(image_view);
4636 return(status);
4637}
4638
4639/*
4640%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4641% %
4642% %
4643% %
4644% S t e g a n o I m a g e %
4645% %
4646% %
4647% %
4648%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4649%
4650% SteganoImage() hides a digital watermark within the image. Recover
4651% the hidden watermark later to prove that the authenticity of an image.
4652% Offset defines the start position within the image to hide the watermark.
4653%
4654% The format of the SteganoImage method is:
4655%
4656% Image *SteganoImage(const Image *image,Image *watermark,
4657% ExceptionInfo *exception)
4658%
4659% A description of each parameter follows:
4660%
4661% o image: the image.
4662%
4663% o watermark: the watermark image.
4664%
4665% o exception: return any errors or warnings in this structure.
4666%
4667*/
4668MagickExport Image *SteganoImage(const Image *image,const Image *watermark,
4669 ExceptionInfo *exception)
4670{
cristye1bf8ad2010-09-19 17:07:03 +00004671#define GetBit(alpha,i) ((((size_t) (alpha) >> (size_t) (i)) & 0x01) != 0)
cristy4c08aed2011-07-01 19:47:50 +00004672#define SetBit(alpha,i,set) (Quantum) ((set) != 0 ? (size_t) (alpha) \
cristyeaedf062010-05-29 22:36:02 +00004673 | (one << (size_t) (i)) : (size_t) (alpha) & ~(one << (size_t) (i)))
cristy3ed852e2009-09-05 21:47:34 +00004674#define SteganoImageTag "Stegano/Image"
4675
cristyb0d3bb92010-09-22 14:37:58 +00004676 CacheView
4677 *stegano_view,
4678 *watermark_view;
4679
cristy3ed852e2009-09-05 21:47:34 +00004680 Image
4681 *stegano_image;
4682
4683 int
4684 c;
4685
cristy3ed852e2009-09-05 21:47:34 +00004686 MagickBooleanType
4687 status;
4688
cristy101ab702011-10-13 13:06:32 +00004689 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00004690 pixel;
4691
cristy4c08aed2011-07-01 19:47:50 +00004692 register Quantum
cristy3ed852e2009-09-05 21:47:34 +00004693 *q;
4694
cristye1bf8ad2010-09-19 17:07:03 +00004695 register ssize_t
4696 x;
4697
cristybb503372010-05-27 20:51:26 +00004698 size_t
cristyeaedf062010-05-29 22:36:02 +00004699 depth,
4700 one;
cristy3ed852e2009-09-05 21:47:34 +00004701
cristye1bf8ad2010-09-19 17:07:03 +00004702 ssize_t
4703 i,
4704 j,
4705 k,
4706 y;
4707
cristy3ed852e2009-09-05 21:47:34 +00004708 /*
4709 Initialize steganographic image attributes.
4710 */
4711 assert(image != (const Image *) NULL);
4712 assert(image->signature == MagickSignature);
4713 if (image->debug != MagickFalse)
4714 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4715 assert(watermark != (const Image *) NULL);
4716 assert(watermark->signature == MagickSignature);
4717 assert(exception != (ExceptionInfo *) NULL);
4718 assert(exception->signature == MagickSignature);
cristyeaedf062010-05-29 22:36:02 +00004719 one=1UL;
cristy3ed852e2009-09-05 21:47:34 +00004720 stegano_image=CloneImage(image,0,0,MagickTrue,exception);
4721 if (stegano_image == (Image *) NULL)
4722 return((Image *) NULL);
cristyf61b1832012-04-01 01:38:19 +00004723 stegano_image->depth=MAGICKCORE_QUANTUM_DEPTH;
cristy574cc262011-08-05 01:23:58 +00004724 if (SetImageStorageClass(stegano_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004725 {
cristy3ed852e2009-09-05 21:47:34 +00004726 stegano_image=DestroyImage(stegano_image);
4727 return((Image *) NULL);
4728 }
cristy3ed852e2009-09-05 21:47:34 +00004729 /*
4730 Hide watermark in low-order bits of image.
4731 */
4732 c=0;
4733 i=0;
4734 j=0;
4735 depth=stegano_image->depth;
cristyf61b1832012-04-01 01:38:19 +00004736 k=stegano_image->offset;
cristyda16f162011-02-19 23:52:17 +00004737 status=MagickTrue;
cristyb0d3bb92010-09-22 14:37:58 +00004738 watermark_view=AcquireCacheView(watermark);
4739 stegano_view=AcquireCacheView(stegano_image);
cristybb503372010-05-27 20:51:26 +00004740 for (i=(ssize_t) depth-1; (i >= 0) && (j < (ssize_t) depth); i--)
cristy3ed852e2009-09-05 21:47:34 +00004741 {
cristybb503372010-05-27 20:51:26 +00004742 for (y=0; (y < (ssize_t) watermark->rows) && (j < (ssize_t) depth); y++)
cristy3ed852e2009-09-05 21:47:34 +00004743 {
cristybb503372010-05-27 20:51:26 +00004744 for (x=0; (x < (ssize_t) watermark->columns) && (j < (ssize_t) depth); x++)
cristy3ed852e2009-09-05 21:47:34 +00004745 {
cristy1707c6c2012-01-18 23:30:54 +00004746 ssize_t
4747 offset;
4748
cristyf05d4942012-03-17 16:26:09 +00004749 (void) GetOneCacheViewVirtualPixelInfo(watermark_view,x,y,&pixel,
cristyda1f9c12011-10-02 21:39:49 +00004750 exception);
cristy1707c6c2012-01-18 23:30:54 +00004751 offset=k/(ssize_t) stegano_image->columns;
4752 if (offset >= (ssize_t) stegano_image->rows)
cristy3ed852e2009-09-05 21:47:34 +00004753 break;
cristyb0d3bb92010-09-22 14:37:58 +00004754 q=GetCacheViewAuthenticPixels(stegano_view,k % (ssize_t)
4755 stegano_image->columns,k/(ssize_t) stegano_image->columns,1,1,
4756 exception);
cristyacd2ed22011-08-30 01:44:23 +00004757 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004758 break;
4759 switch (c)
4760 {
4761 case 0:
4762 {
cristyf61b1832012-04-01 01:38:19 +00004763 SetPixelRed(stegano_image,SetBit(GetPixelRed(stegano_image,q),j,
4764 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004765 break;
4766 }
4767 case 1:
4768 {
cristyf61b1832012-04-01 01:38:19 +00004769 SetPixelGreen(stegano_image,SetBit(GetPixelGreen(stegano_image,q),j,
4770 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004771 break;
4772 }
4773 case 2:
4774 {
cristyf61b1832012-04-01 01:38:19 +00004775 SetPixelBlue(stegano_image,SetBit(GetPixelBlue(stegano_image,q),j,
4776 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004777 break;
4778 }
4779 }
cristyb0d3bb92010-09-22 14:37:58 +00004780 if (SyncCacheViewAuthenticPixels(stegano_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004781 break;
4782 c++;
4783 if (c == 3)
4784 c=0;
4785 k++;
cristybb503372010-05-27 20:51:26 +00004786 if (k == (ssize_t) (stegano_image->columns*stegano_image->columns))
cristy3ed852e2009-09-05 21:47:34 +00004787 k=0;
cristyf61b1832012-04-01 01:38:19 +00004788 if (k == stegano_image->offset)
cristy3ed852e2009-09-05 21:47:34 +00004789 j++;
4790 }
4791 }
cristy8b27a6d2010-02-14 03:31:15 +00004792 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004793 {
cristy8b27a6d2010-02-14 03:31:15 +00004794 MagickBooleanType
4795 proceed;
4796
4797 proceed=SetImageProgress(image,SteganoImageTag,(MagickOffsetType)
4798 (depth-i),depth);
4799 if (proceed == MagickFalse)
4800 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004801 }
4802 }
cristyb0d3bb92010-09-22 14:37:58 +00004803 stegano_view=DestroyCacheView(stegano_view);
4804 watermark_view=DestroyCacheView(watermark_view);
cristyda16f162011-02-19 23:52:17 +00004805 if (status == MagickFalse)
4806 {
4807 stegano_image=DestroyImage(stegano_image);
4808 return((Image *) NULL);
4809 }
cristy3ed852e2009-09-05 21:47:34 +00004810 return(stegano_image);
4811}
4812
4813/*
4814%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4815% %
4816% %
4817% %
4818% S t e r e o A n a g l y p h I m a g e %
4819% %
4820% %
4821% %
4822%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4823%
4824% StereoAnaglyphImage() combines two images and produces a single image that
4825% is the composite of a left and right image of a stereo pair. Special
4826% red-green stereo glasses are required to view this effect.
4827%
4828% The format of the StereoAnaglyphImage method is:
4829%
4830% Image *StereoImage(const Image *left_image,const Image *right_image,
4831% ExceptionInfo *exception)
4832% Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004833% const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004834% ExceptionInfo *exception)
4835%
4836% A description of each parameter follows:
4837%
4838% o left_image: the left image.
4839%
4840% o right_image: the right image.
4841%
4842% o exception: return any errors or warnings in this structure.
4843%
4844% o x_offset: amount, in pixels, by which the left image is offset to the
4845% right of the right image.
4846%
4847% o y_offset: amount, in pixels, by which the left image is offset to the
4848% bottom of the right image.
4849%
4850%
4851*/
4852MagickExport Image *StereoImage(const Image *left_image,
4853 const Image *right_image,ExceptionInfo *exception)
4854{
4855 return(StereoAnaglyphImage(left_image,right_image,0,0,exception));
4856}
4857
4858MagickExport Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004859 const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004860 ExceptionInfo *exception)
4861{
4862#define StereoImageTag "Stereo/Image"
4863
4864 const Image
4865 *image;
4866
4867 Image
4868 *stereo_image;
4869
cristy3ed852e2009-09-05 21:47:34 +00004870 MagickBooleanType
4871 status;
4872
cristy9d314ff2011-03-09 01:30:28 +00004873 ssize_t
4874 y;
4875
cristy3ed852e2009-09-05 21:47:34 +00004876 assert(left_image != (const Image *) NULL);
4877 assert(left_image->signature == MagickSignature);
4878 if (left_image->debug != MagickFalse)
4879 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4880 left_image->filename);
4881 assert(right_image != (const Image *) NULL);
4882 assert(right_image->signature == MagickSignature);
4883 assert(exception != (ExceptionInfo *) NULL);
4884 assert(exception->signature == MagickSignature);
4885 assert(right_image != (const Image *) NULL);
4886 image=left_image;
4887 if ((left_image->columns != right_image->columns) ||
4888 (left_image->rows != right_image->rows))
4889 ThrowImageException(ImageError,"LeftAndRightImageSizesDiffer");
4890 /*
4891 Initialize stereo image attributes.
4892 */
4893 stereo_image=CloneImage(left_image,left_image->columns,left_image->rows,
4894 MagickTrue,exception);
4895 if (stereo_image == (Image *) NULL)
4896 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004897 if (SetImageStorageClass(stereo_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004898 {
cristy3ed852e2009-09-05 21:47:34 +00004899 stereo_image=DestroyImage(stereo_image);
4900 return((Image *) NULL);
4901 }
4902 /*
4903 Copy left image to red channel and right image to blue channel.
4904 */
cristyda16f162011-02-19 23:52:17 +00004905 status=MagickTrue;
cristybb503372010-05-27 20:51:26 +00004906 for (y=0; y < (ssize_t) stereo_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004907 {
cristy4c08aed2011-07-01 19:47:50 +00004908 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004909 *restrict p,
4910 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004911
cristybb503372010-05-27 20:51:26 +00004912 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004913 x;
4914
cristy4c08aed2011-07-01 19:47:50 +00004915 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004916 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +00004917
4918 p=GetVirtualPixels(left_image,-x_offset,y-y_offset,image->columns,1,
4919 exception);
4920 q=GetVirtualPixels(right_image,0,y,right_image->columns,1,exception);
4921 r=QueueAuthenticPixels(stereo_image,0,y,stereo_image->columns,1,exception);
cristy76f512e2011-09-12 01:26:56 +00004922 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL) ||
4923 (r == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004924 break;
cristybb503372010-05-27 20:51:26 +00004925 for (x=0; x < (ssize_t) stereo_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004926 {
cristy4c08aed2011-07-01 19:47:50 +00004927 SetPixelRed(image,GetPixelRed(left_image,p),r);
cristy76f512e2011-09-12 01:26:56 +00004928 SetPixelGreen(image,GetPixelGreen(right_image,q),r);
4929 SetPixelBlue(image,GetPixelBlue(right_image,q),r);
4930 if ((GetPixelAlphaTraits(stereo_image) & CopyPixelTrait) != 0)
4931 SetPixelAlpha(image,(GetPixelAlpha(left_image,p)+
4932 GetPixelAlpha(right_image,q))/2,r);
cristyed231572011-07-14 02:18:59 +00004933 p+=GetPixelChannels(left_image);
cristy76f512e2011-09-12 01:26:56 +00004934 q+=GetPixelChannels(right_image);
4935 r+=GetPixelChannels(stereo_image);
cristy3ed852e2009-09-05 21:47:34 +00004936 }
4937 if (SyncAuthenticPixels(stereo_image,exception) == MagickFalse)
4938 break;
cristy8b27a6d2010-02-14 03:31:15 +00004939 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004940 {
cristy8b27a6d2010-02-14 03:31:15 +00004941 MagickBooleanType
4942 proceed;
4943
cristybb503372010-05-27 20:51:26 +00004944 proceed=SetImageProgress(image,StereoImageTag,(MagickOffsetType) y,
4945 stereo_image->rows);
cristy8b27a6d2010-02-14 03:31:15 +00004946 if (proceed == MagickFalse)
4947 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004948 }
4949 }
cristyda16f162011-02-19 23:52:17 +00004950 if (status == MagickFalse)
4951 {
4952 stereo_image=DestroyImage(stereo_image);
4953 return((Image *) NULL);
4954 }
cristy3ed852e2009-09-05 21:47:34 +00004955 return(stereo_image);
4956}
4957
4958/*
4959%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4960% %
4961% %
4962% %
4963% S w i r l I m a g e %
4964% %
4965% %
4966% %
4967%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4968%
4969% SwirlImage() swirls the pixels about the center of the image, where
4970% degrees indicates the sweep of the arc through which each pixel is moved.
4971% You get a more dramatic effect as the degrees move from 1 to 360.
4972%
4973% The format of the SwirlImage method is:
4974%
4975% Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00004976% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004977%
4978% A description of each parameter follows:
4979%
4980% o image: the image.
4981%
4982% o degrees: Define the tightness of the swirling effect.
4983%
cristy76f512e2011-09-12 01:26:56 +00004984% o method: the pixel interpolation method.
4985%
cristy3ed852e2009-09-05 21:47:34 +00004986% o exception: return any errors or warnings in this structure.
4987%
4988*/
4989MagickExport Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00004990 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004991{
4992#define SwirlImageTag "Swirl/Image"
4993
cristyfa112112010-01-04 17:48:07 +00004994 CacheView
4995 *image_view,
4996 *swirl_view;
4997
cristy3ed852e2009-09-05 21:47:34 +00004998 Image
4999 *swirl_image;
5000
cristy3ed852e2009-09-05 21:47:34 +00005001 MagickBooleanType
5002 status;
5003
cristybb503372010-05-27 20:51:26 +00005004 MagickOffsetType
5005 progress;
5006
cristy3ed852e2009-09-05 21:47:34 +00005007 MagickRealType
5008 radius;
5009
5010 PointInfo
5011 center,
5012 scale;
5013
cristybb503372010-05-27 20:51:26 +00005014 ssize_t
5015 y;
5016
cristy3ed852e2009-09-05 21:47:34 +00005017 /*
5018 Initialize swirl image attributes.
5019 */
5020 assert(image != (const Image *) NULL);
5021 assert(image->signature == MagickSignature);
5022 if (image->debug != MagickFalse)
5023 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5024 assert(exception != (ExceptionInfo *) NULL);
5025 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00005026 swirl_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005027 if (swirl_image == (Image *) NULL)
5028 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005029 if (SetImageStorageClass(swirl_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005030 {
cristy3ed852e2009-09-05 21:47:34 +00005031 swirl_image=DestroyImage(swirl_image);
5032 return((Image *) NULL);
5033 }
cristy4c08aed2011-07-01 19:47:50 +00005034 if (swirl_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00005035 swirl_image->matte=MagickTrue;
5036 /*
5037 Compute scaling factor.
5038 */
5039 center.x=(double) image->columns/2.0;
5040 center.y=(double) image->rows/2.0;
5041 radius=MagickMax(center.x,center.y);
5042 scale.x=1.0;
5043 scale.y=1.0;
5044 if (image->columns > image->rows)
5045 scale.y=(double) image->columns/(double) image->rows;
5046 else
5047 if (image->columns < image->rows)
5048 scale.x=(double) image->rows/(double) image->columns;
5049 degrees=(double) DegreesToRadians(degrees);
5050 /*
5051 Swirl image.
5052 */
5053 status=MagickTrue;
5054 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00005055 image_view=AcquireCacheView(image);
5056 swirl_view=AcquireCacheView(swirl_image);
cristyb5d5f722009-11-04 03:03:49 +00005057#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005058 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00005059#endif
cristybb503372010-05-27 20:51:26 +00005060 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005061 {
cristy3ed852e2009-09-05 21:47:34 +00005062 MagickRealType
5063 distance;
5064
5065 PointInfo
5066 delta;
5067
cristy6d188022011-09-12 13:23:33 +00005068 register const Quantum
5069 *restrict p;
5070
cristybb503372010-05-27 20:51:26 +00005071 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005072 x;
5073
cristy4c08aed2011-07-01 19:47:50 +00005074 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005075 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005076
5077 if (status == MagickFalse)
5078 continue;
cristy6d188022011-09-12 13:23:33 +00005079 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy1707c6c2012-01-18 23:30:54 +00005080 q=QueueCacheViewAuthenticPixels(swirl_view,0,y,swirl_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00005081 exception);
cristy6d188022011-09-12 13:23:33 +00005082 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005083 {
5084 status=MagickFalse;
5085 continue;
5086 }
cristy3ed852e2009-09-05 21:47:34 +00005087 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00005088 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005089 {
5090 /*
5091 Determine if the pixel is within an ellipse.
5092 */
cristy10a6c612012-01-29 21:41:05 +00005093 if (GetPixelMask(image,p) != 0)
5094 {
5095 p+=GetPixelChannels(image);
5096 q+=GetPixelChannels(swirl_image);
5097 continue;
5098 }
cristy3ed852e2009-09-05 21:47:34 +00005099 delta.x=scale.x*(double) (x-center.x);
5100 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00005101 if (distance >= (radius*radius))
5102 {
cristy1707c6c2012-01-18 23:30:54 +00005103 register ssize_t
5104 i;
5105
cristy6d188022011-09-12 13:23:33 +00005106 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy1707c6c2012-01-18 23:30:54 +00005107 {
5108 PixelChannel
5109 channel;
5110
5111 PixelTrait
5112 swirl_traits,
5113 traits;
5114
5115 channel=GetPixelChannelMapChannel(image,i);
5116 traits=GetPixelChannelMapTraits(image,channel);
5117 swirl_traits=GetPixelChannelMapTraits(swirl_image,channel);
5118 if ((traits == UndefinedPixelTrait) ||
5119 (swirl_traits == UndefinedPixelTrait))
5120 continue;
5121 SetPixelChannel(swirl_image,channel,p[i],q);
5122 }
cristy6d188022011-09-12 13:23:33 +00005123 }
5124 else
cristy3ed852e2009-09-05 21:47:34 +00005125 {
5126 MagickRealType
5127 cosine,
5128 factor,
5129 sine;
5130
5131 /*
5132 Swirl the pixel.
5133 */
5134 factor=1.0-sqrt((double) distance)/radius;
5135 sine=sin((double) (degrees*factor*factor));
5136 cosine=cos((double) (degrees*factor*factor));
cristy76f512e2011-09-12 01:26:56 +00005137 status=InterpolatePixelChannels(image,image_view,swirl_image,method,
5138 ((cosine*delta.x-sine*delta.y)/scale.x+center.x),(double)
5139 ((sine*delta.x+cosine*delta.y)/scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00005140 }
cristy6d188022011-09-12 13:23:33 +00005141 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00005142 q+=GetPixelChannels(swirl_image);
cristy3ed852e2009-09-05 21:47:34 +00005143 }
5144 if (SyncCacheViewAuthenticPixels(swirl_view,exception) == MagickFalse)
5145 status=MagickFalse;
5146 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5147 {
5148 MagickBooleanType
5149 proceed;
5150
cristyb5d5f722009-11-04 03:03:49 +00005151#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005152 #pragma omp critical (MagickCore_SwirlImage)
cristy3ed852e2009-09-05 21:47:34 +00005153#endif
5154 proceed=SetImageProgress(image,SwirlImageTag,progress++,image->rows);
5155 if (proceed == MagickFalse)
5156 status=MagickFalse;
5157 }
5158 }
5159 swirl_view=DestroyCacheView(swirl_view);
5160 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00005161 if (status == MagickFalse)
5162 swirl_image=DestroyImage(swirl_image);
5163 return(swirl_image);
5164}
5165
5166/*
5167%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5168% %
5169% %
5170% %
5171% T i n t I m a g e %
5172% %
5173% %
5174% %
5175%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5176%
5177% TintImage() applies a color vector to each pixel in the image. The length
5178% of the vector is 0 for black and white and at its maximum for the midtones.
5179% The vector weighting function is f(x)=(1-(4.0*((x-0.5)*(x-0.5))))
5180%
5181% The format of the TintImage method is:
5182%
cristyb817c3f2011-10-03 14:00:35 +00005183% Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005184% const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005185%
5186% A description of each parameter follows:
5187%
5188% o image: the image.
5189%
cristyb817c3f2011-10-03 14:00:35 +00005190% o blend: A color value used for tinting.
cristy3ed852e2009-09-05 21:47:34 +00005191%
5192% o tint: A color value used for tinting.
5193%
5194% o exception: return any errors or warnings in this structure.
5195%
5196*/
cristyb817c3f2011-10-03 14:00:35 +00005197MagickExport Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005198 const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005199{
5200#define TintImageTag "Tint/Image"
5201
cristyc4c8d132010-01-07 01:58:38 +00005202 CacheView
5203 *image_view,
5204 *tint_view;
5205
cristy3ed852e2009-09-05 21:47:34 +00005206 GeometryInfo
5207 geometry_info;
5208
5209 Image
5210 *tint_image;
5211
cristy3ed852e2009-09-05 21:47:34 +00005212 MagickBooleanType
5213 status;
5214
cristybb503372010-05-27 20:51:26 +00005215 MagickOffsetType
5216 progress;
cristy3ed852e2009-09-05 21:47:34 +00005217
cristy28474bf2011-09-11 23:32:52 +00005218 MagickRealType
5219 intensity;
5220
cristy4c08aed2011-07-01 19:47:50 +00005221 PixelInfo
cristy1707c6c2012-01-18 23:30:54 +00005222 color_vector;
cristy3ed852e2009-09-05 21:47:34 +00005223
cristybb503372010-05-27 20:51:26 +00005224 MagickStatusType
5225 flags;
5226
5227 ssize_t
5228 y;
5229
cristy3ed852e2009-09-05 21:47:34 +00005230 /*
5231 Allocate tint image.
5232 */
5233 assert(image != (const Image *) NULL);
5234 assert(image->signature == MagickSignature);
5235 if (image->debug != MagickFalse)
5236 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5237 assert(exception != (ExceptionInfo *) NULL);
5238 assert(exception->signature == MagickSignature);
5239 tint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5240 if (tint_image == (Image *) NULL)
5241 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005242 if (SetImageStorageClass(tint_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005243 {
cristy3ed852e2009-09-05 21:47:34 +00005244 tint_image=DestroyImage(tint_image);
5245 return((Image *) NULL);
5246 }
cristyaed9c382011-10-03 17:54:21 +00005247 if (blend == (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005248 return(tint_image);
5249 /*
5250 Determine RGB values of the color.
5251 */
cristy1707c6c2012-01-18 23:30:54 +00005252 GetPixelInfo(image,&color_vector);
cristyb817c3f2011-10-03 14:00:35 +00005253 flags=ParseGeometry(blend,&geometry_info);
cristy1707c6c2012-01-18 23:30:54 +00005254 color_vector.red=geometry_info.rho;
5255 color_vector.green=geometry_info.rho;
5256 color_vector.blue=geometry_info.rho;
5257 color_vector.alpha=OpaqueAlpha;
cristy3ed852e2009-09-05 21:47:34 +00005258 if ((flags & SigmaValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005259 color_vector.green=geometry_info.sigma;
cristy3ed852e2009-09-05 21:47:34 +00005260 if ((flags & XiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005261 color_vector.blue=geometry_info.xi;
cristyb817c3f2011-10-03 14:00:35 +00005262 if ((flags & PsiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005263 color_vector.alpha=geometry_info.psi;
cristy76f512e2011-09-12 01:26:56 +00005264 if (image->colorspace == CMYKColorspace)
5265 {
cristy1707c6c2012-01-18 23:30:54 +00005266 color_vector.black=geometry_info.rho;
cristy76f512e2011-09-12 01:26:56 +00005267 if ((flags & PsiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005268 color_vector.black=geometry_info.psi;
cristy76f512e2011-09-12 01:26:56 +00005269 if ((flags & ChiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005270 color_vector.alpha=geometry_info.chi;
cristy76f512e2011-09-12 01:26:56 +00005271 }
cristy28474bf2011-09-11 23:32:52 +00005272 intensity=(MagickRealType) GetPixelInfoIntensity(tint);
cristy1707c6c2012-01-18 23:30:54 +00005273 color_vector.red=(MagickRealType) (color_vector.red*tint->red/100.0-
5274 intensity);
5275 color_vector.green=(MagickRealType) (color_vector.green*tint->green/100.0-
5276 intensity);
5277 color_vector.blue=(MagickRealType) (color_vector.blue*tint->blue/100.0-
5278 intensity);
5279 color_vector.black=(MagickRealType) (color_vector.black*tint->black/100.0-
5280 intensity);
5281 color_vector.alpha=(MagickRealType) (color_vector.alpha*tint->alpha/100.0-
5282 intensity);
cristy3ed852e2009-09-05 21:47:34 +00005283 /*
5284 Tint image.
5285 */
5286 status=MagickTrue;
5287 progress=0;
5288 image_view=AcquireCacheView(image);
5289 tint_view=AcquireCacheView(tint_image);
cristyb5d5f722009-11-04 03:03:49 +00005290#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00005291 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00005292#endif
cristybb503372010-05-27 20:51:26 +00005293 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005294 {
cristy4c08aed2011-07-01 19:47:50 +00005295 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00005296 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00005297
cristy4c08aed2011-07-01 19:47:50 +00005298 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005299 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005300
cristy6b91acb2011-04-19 12:23:54 +00005301 register ssize_t
5302 x;
5303
cristy3ed852e2009-09-05 21:47:34 +00005304 if (status == MagickFalse)
5305 continue;
5306 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5307 q=QueueCacheViewAuthenticPixels(tint_view,0,y,tint_image->columns,1,
5308 exception);
cristy4c08aed2011-07-01 19:47:50 +00005309 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005310 {
5311 status=MagickFalse;
5312 continue;
5313 }
cristybb503372010-05-27 20:51:26 +00005314 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005315 {
cristy4c08aed2011-07-01 19:47:50 +00005316 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00005317 pixel;
5318
5319 MagickRealType
5320 weight;
5321
cristy1707c6c2012-01-18 23:30:54 +00005322 register ssize_t
5323 i;
5324
cristy10a6c612012-01-29 21:41:05 +00005325 if (GetPixelMask(image,p) != 0)
5326 {
5327 p+=GetPixelChannels(image);
5328 q+=GetPixelChannels(tint_image);
5329 continue;
5330 }
cristy1707c6c2012-01-18 23:30:54 +00005331 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5332 {
5333 PixelChannel
5334 channel;
5335
5336 PixelTrait
5337 tint_traits,
5338 traits;
5339
5340 channel=GetPixelChannelMapChannel(image,i);
5341 traits=GetPixelChannelMapTraits(image,channel);
5342 tint_traits=GetPixelChannelMapTraits(tint_image,channel);
5343 if ((traits == UndefinedPixelTrait) ||
5344 (tint_traits == UndefinedPixelTrait))
5345 continue;
5346 if ((tint_traits & CopyPixelTrait) != 0)
5347 {
5348 SetPixelChannel(tint_image,channel,p[i],q);
5349 continue;
5350 }
5351 }
5352 GetPixelInfo(image,&pixel);
5353 weight=QuantumScale*GetPixelRed(image,p)-0.5;
5354 pixel.red=(MagickRealType) GetPixelRed(image,p)+color_vector.red*
5355 (1.0-(4.0*(weight*weight)));
5356 weight=QuantumScale*GetPixelGreen(image,p)-0.5;
5357 pixel.green=(MagickRealType) GetPixelGreen(image,p)+color_vector.green*
5358 (1.0-(4.0*(weight*weight)));
5359 weight=QuantumScale*GetPixelBlue(image,p)-0.5;
5360 pixel.blue=(MagickRealType) GetPixelBlue(image,p)+color_vector.blue*
5361 (1.0-(4.0*(weight*weight)));
5362 weight=QuantumScale*GetPixelBlack(image,p)-0.5;
5363 pixel.black=(MagickRealType) GetPixelBlack(image,p)+color_vector.black*
5364 (1.0-(4.0*(weight*weight)));
5365 SetPixelInfoPixel(tint_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +00005366 p+=GetPixelChannels(image);
5367 q+=GetPixelChannels(tint_image);
cristy3ed852e2009-09-05 21:47:34 +00005368 }
5369 if (SyncCacheViewAuthenticPixels(tint_view,exception) == MagickFalse)
5370 status=MagickFalse;
5371 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5372 {
5373 MagickBooleanType
5374 proceed;
5375
cristyb5d5f722009-11-04 03:03:49 +00005376#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005377 #pragma omp critical (MagickCore_TintImage)
cristy3ed852e2009-09-05 21:47:34 +00005378#endif
5379 proceed=SetImageProgress(image,TintImageTag,progress++,image->rows);
5380 if (proceed == MagickFalse)
5381 status=MagickFalse;
5382 }
5383 }
5384 tint_view=DestroyCacheView(tint_view);
5385 image_view=DestroyCacheView(image_view);
5386 if (status == MagickFalse)
5387 tint_image=DestroyImage(tint_image);
5388 return(tint_image);
5389}
5390
5391/*
5392%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5393% %
5394% %
5395% %
5396% V i g n e t t e I m a g e %
5397% %
5398% %
5399% %
5400%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5401%
5402% VignetteImage() softens the edges of the image in vignette style.
5403%
5404% The format of the VignetteImage method is:
5405%
5406% Image *VignetteImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00005407% const double sigma,const ssize_t x,const ssize_t y,
cristy05c0c9a2011-09-05 23:16:13 +00005408% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005409%
5410% A description of each parameter follows:
5411%
5412% o image: the image.
5413%
5414% o radius: the radius of the pixel neighborhood.
5415%
5416% o sigma: the standard deviation of the Gaussian, in pixels.
5417%
5418% o x, y: Define the x and y ellipse offset.
5419%
5420% o exception: return any errors or warnings in this structure.
5421%
5422*/
5423MagickExport Image *VignetteImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00005424 const double sigma,const ssize_t x,const ssize_t y,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005425{
5426 char
5427 ellipse[MaxTextExtent];
5428
5429 DrawInfo
5430 *draw_info;
5431
5432 Image
5433 *canvas_image,
5434 *blur_image,
5435 *oval_image,
5436 *vignette_image;
5437
5438 assert(image != (Image *) NULL);
5439 assert(image->signature == MagickSignature);
5440 if (image->debug != MagickFalse)
5441 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5442 assert(exception != (ExceptionInfo *) NULL);
5443 assert(exception->signature == MagickSignature);
5444 canvas_image=CloneImage(image,0,0,MagickTrue,exception);
5445 if (canvas_image == (Image *) NULL)
5446 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005447 if (SetImageStorageClass(canvas_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005448 {
cristy3ed852e2009-09-05 21:47:34 +00005449 canvas_image=DestroyImage(canvas_image);
5450 return((Image *) NULL);
5451 }
5452 canvas_image->matte=MagickTrue;
cristy98621462011-12-31 22:31:11 +00005453 oval_image=CloneImage(canvas_image,canvas_image->columns,canvas_image->rows,
5454 MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005455 if (oval_image == (Image *) NULL)
5456 {
5457 canvas_image=DestroyImage(canvas_image);
5458 return((Image *) NULL);
5459 }
cristy9950d572011-10-01 18:22:35 +00005460 (void) QueryColorCompliance("#000000",AllCompliance,
5461 &oval_image->background_color,exception);
cristyea1a8aa2011-10-20 13:24:06 +00005462 (void) SetImageBackgroundColor(oval_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00005463 draw_info=CloneDrawInfo((const ImageInfo *) NULL,(const DrawInfo *) NULL);
cristy9950d572011-10-01 18:22:35 +00005464 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->fill,
5465 exception);
5466 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->stroke,
5467 exception);
cristy1707c6c2012-01-18 23:30:54 +00005468 (void) FormatLocaleString(ellipse,MaxTextExtent,"ellipse %g,%g,%g,%g,"
5469 "0.0,360.0",image->columns/2.0,image->rows/2.0,image->columns/2.0-x,
5470 image->rows/2.0-y);
cristy3ed852e2009-09-05 21:47:34 +00005471 draw_info->primitive=AcquireString(ellipse);
cristy018f07f2011-09-04 21:15:19 +00005472 (void) DrawImage(oval_image,draw_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00005473 draw_info=DestroyDrawInfo(draw_info);
cristyaa2c16c2012-03-25 22:21:35 +00005474 blur_image=BlurImage(oval_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +00005475 oval_image=DestroyImage(oval_image);
5476 if (blur_image == (Image *) NULL)
5477 {
5478 canvas_image=DestroyImage(canvas_image);
5479 return((Image *) NULL);
5480 }
5481 blur_image->matte=MagickFalse;
cristy39172402012-03-30 13:04:39 +00005482 (void) CompositeImage(canvas_image,blur_image,IntensityCompositeOp,MagickTrue,
5483 0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00005484 blur_image=DestroyImage(blur_image);
5485 vignette_image=MergeImageLayers(canvas_image,FlattenLayer,exception);
5486 canvas_image=DestroyImage(canvas_image);
5487 return(vignette_image);
5488}
5489
5490/*
5491%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5492% %
5493% %
5494% %
5495% W a v e I m a g e %
5496% %
5497% %
5498% %
5499%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5500%
5501% WaveImage() creates a "ripple" effect in the image by shifting the pixels
cristycee97112010-05-28 00:44:52 +00005502% vertically along a sine wave whose amplitude and wavelength is specified
cristy3ed852e2009-09-05 21:47:34 +00005503% by the given parameters.
5504%
5505% The format of the WaveImage method is:
5506%
5507% Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005508% const double wave_length,const PixelInterpolateMethod method,
5509% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005510%
5511% A description of each parameter follows:
5512%
5513% o image: the image.
5514%
5515% o amplitude, wave_length: Define the amplitude and wave length of the
5516% sine wave.
5517%
cristy5c4e2582011-09-11 19:21:03 +00005518% o interpolate: the pixel interpolation method.
5519%
cristy3ed852e2009-09-05 21:47:34 +00005520% o exception: return any errors or warnings in this structure.
5521%
5522*/
5523MagickExport Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005524 const double wave_length,const PixelInterpolateMethod method,
5525 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005526{
5527#define WaveImageTag "Wave/Image"
5528
cristyfa112112010-01-04 17:48:07 +00005529 CacheView
cristyd76c51e2011-03-26 00:21:26 +00005530 *image_view,
cristyfa112112010-01-04 17:48:07 +00005531 *wave_view;
5532
cristy3ed852e2009-09-05 21:47:34 +00005533 Image
5534 *wave_image;
5535
cristy3ed852e2009-09-05 21:47:34 +00005536 MagickBooleanType
5537 status;
5538
cristybb503372010-05-27 20:51:26 +00005539 MagickOffsetType
5540 progress;
5541
cristy3ed852e2009-09-05 21:47:34 +00005542 MagickRealType
5543 *sine_map;
5544
cristybb503372010-05-27 20:51:26 +00005545 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005546 i;
5547
cristybb503372010-05-27 20:51:26 +00005548 ssize_t
5549 y;
5550
cristy3ed852e2009-09-05 21:47:34 +00005551 /*
5552 Initialize wave image attributes.
5553 */
5554 assert(image != (Image *) NULL);
5555 assert(image->signature == MagickSignature);
5556 if (image->debug != MagickFalse)
5557 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5558 assert(exception != (ExceptionInfo *) NULL);
5559 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00005560 wave_image=CloneImage(image,image->columns,(size_t) (image->rows+2.0*
cristy3ed852e2009-09-05 21:47:34 +00005561 fabs(amplitude)),MagickTrue,exception);
5562 if (wave_image == (Image *) NULL)
5563 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005564 if (SetImageStorageClass(wave_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005565 {
cristy3ed852e2009-09-05 21:47:34 +00005566 wave_image=DestroyImage(wave_image);
5567 return((Image *) NULL);
5568 }
cristy4c08aed2011-07-01 19:47:50 +00005569 if (wave_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00005570 wave_image->matte=MagickTrue;
5571 /*
5572 Allocate sine map.
5573 */
5574 sine_map=(MagickRealType *) AcquireQuantumMemory((size_t) wave_image->columns,
5575 sizeof(*sine_map));
5576 if (sine_map == (MagickRealType *) NULL)
5577 {
5578 wave_image=DestroyImage(wave_image);
5579 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
5580 }
cristybb503372010-05-27 20:51:26 +00005581 for (i=0; i < (ssize_t) wave_image->columns; i++)
cristy4205a3c2010-09-12 20:19:59 +00005582 sine_map[i]=fabs(amplitude)+amplitude*sin((double) ((2.0*MagickPI*i)/
5583 wave_length));
cristy3ed852e2009-09-05 21:47:34 +00005584 /*
5585 Wave image.
5586 */
5587 status=MagickTrue;
5588 progress=0;
cristyd76c51e2011-03-26 00:21:26 +00005589 image_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00005590 wave_view=AcquireCacheView(wave_image);
cristyd76c51e2011-03-26 00:21:26 +00005591 (void) SetCacheViewVirtualPixelMethod(image_view,
5592 BackgroundVirtualPixelMethod);
cristyb5d5f722009-11-04 03:03:49 +00005593#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00005594 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00005595#endif
cristybb503372010-05-27 20:51:26 +00005596 for (y=0; y < (ssize_t) wave_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005597 {
cristy4c08aed2011-07-01 19:47:50 +00005598 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005599 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005600
cristye97bb922011-04-03 01:36:52 +00005601 register ssize_t
5602 x;
5603
cristy3ed852e2009-09-05 21:47:34 +00005604 if (status == MagickFalse)
5605 continue;
5606 q=QueueCacheViewAuthenticPixels(wave_view,0,y,wave_image->columns,1,
5607 exception);
cristyacd2ed22011-08-30 01:44:23 +00005608 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005609 {
5610 status=MagickFalse;
5611 continue;
5612 }
cristybb503372010-05-27 20:51:26 +00005613 for (x=0; x < (ssize_t) wave_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005614 {
cristy5c4e2582011-09-11 19:21:03 +00005615 status=InterpolatePixelChannels(image,image_view,wave_image,method,
5616 (double) x,(double) (y-sine_map[x]),q,exception);
cristyed231572011-07-14 02:18:59 +00005617 q+=GetPixelChannels(wave_image);
cristy3ed852e2009-09-05 21:47:34 +00005618 }
5619 if (SyncCacheViewAuthenticPixels(wave_view,exception) == MagickFalse)
5620 status=MagickFalse;
5621 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5622 {
5623 MagickBooleanType
5624 proceed;
5625
cristyb5d5f722009-11-04 03:03:49 +00005626#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005627 #pragma omp critical (MagickCore_WaveImage)
cristy3ed852e2009-09-05 21:47:34 +00005628#endif
5629 proceed=SetImageProgress(image,WaveImageTag,progress++,image->rows);
5630 if (proceed == MagickFalse)
5631 status=MagickFalse;
5632 }
5633 }
5634 wave_view=DestroyCacheView(wave_view);
cristyd76c51e2011-03-26 00:21:26 +00005635 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00005636 sine_map=(MagickRealType *) RelinquishMagickMemory(sine_map);
5637 if (status == MagickFalse)
5638 wave_image=DestroyImage(wave_image);
5639 return(wave_image);
5640}