blob: 808f8e31234b041f63e1cd7a707446bbb690f39e [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"
cristy57340e02012-05-05 00:53:23 +000085#include "MagickCore/resource_.h"
cristy4c08aed2011-07-01 19:47:50 +000086#include "MagickCore/splay-tree.h"
87#include "MagickCore/statistic.h"
88#include "MagickCore/string_.h"
89#include "MagickCore/string-private.h"
90#include "MagickCore/thread-private.h"
91#include "MagickCore/transform.h"
92#include "MagickCore/utility.h"
cristy3ed852e2009-09-05 21:47:34 +000093
94/*
95 Define declarations.
96*/
97#define LeftShiftOperator 0xf5
98#define RightShiftOperator 0xf6
99#define LessThanEqualOperator 0xf7
100#define GreaterThanEqualOperator 0xf8
101#define EqualOperator 0xf9
102#define NotEqualOperator 0xfa
103#define LogicalAndOperator 0xfb
104#define LogicalOrOperator 0xfc
cristy116af162010-08-13 01:25:47 +0000105#define ExponentialNotation 0xfd
cristy3ed852e2009-09-05 21:47:34 +0000106
107struct _FxInfo
108{
109 const Image
110 *images;
111
cristy3ed852e2009-09-05 21:47:34 +0000112 char
113 *expression;
114
115 FILE
116 *file;
117
118 SplayTreeInfo
119 *colors,
120 *symbols;
121
cristyd76c51e2011-03-26 00:21:26 +0000122 CacheView
123 **view;
cristy3ed852e2009-09-05 21:47:34 +0000124
125 RandomInfo
126 *random_info;
127
128 ExceptionInfo
129 *exception;
130};
131
132/*
133%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
134% %
135% %
136% %
137+ A c q u i r e F x I n f o %
138% %
139% %
140% %
141%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
142%
143% AcquireFxInfo() allocates the FxInfo structure.
144%
145% The format of the AcquireFxInfo method is:
146%
cristydb070952012-04-20 14:33:00 +0000147% FxInfo *AcquireFxInfo(Image *image,const char *expression,
148% ExceptionInfo *exception)
cristy0a9b3722010-10-23 18:45:49 +0000149%
cristy3ed852e2009-09-05 21:47:34 +0000150% A description of each parameter follows:
151%
152% o image: the image.
153%
154% o expression: the expression.
155%
cristydb070952012-04-20 14:33:00 +0000156% o exception: return any errors or warnings in this structure.
157%
cristy3ed852e2009-09-05 21:47:34 +0000158*/
cristydb070952012-04-20 14:33:00 +0000159MagickPrivate FxInfo *AcquireFxInfo(const Image *image,const char *expression,
160 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000161{
162 char
163 fx_op[2];
164
cristycb180922011-03-11 14:41:24 +0000165 const Image
166 *next;
167
cristy3ed852e2009-09-05 21:47:34 +0000168 FxInfo
169 *fx_info;
170
cristybb503372010-05-27 20:51:26 +0000171 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000172 i;
173
cristy73bd4a52010-10-05 11:24:23 +0000174 fx_info=(FxInfo *) AcquireMagickMemory(sizeof(*fx_info));
cristy3ed852e2009-09-05 21:47:34 +0000175 if (fx_info == (FxInfo *) NULL)
176 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
177 (void) ResetMagickMemory(fx_info,0,sizeof(*fx_info));
178 fx_info->exception=AcquireExceptionInfo();
anthony7d86e172011-03-23 12:37:06 +0000179 fx_info->images=image;
cristy3ed852e2009-09-05 21:47:34 +0000180 fx_info->colors=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
181 RelinquishMagickMemory);
182 fx_info->symbols=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
183 RelinquishMagickMemory);
cristyd76c51e2011-03-26 00:21:26 +0000184 fx_info->view=(CacheView **) AcquireQuantumMemory(GetImageListLength(
185 fx_info->images),sizeof(*fx_info->view));
186 if (fx_info->view == (CacheView **) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000187 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
cristya2262262011-03-11 02:50:37 +0000188 i=0;
cristy0ea377f2011-03-24 00:54:19 +0000189 next=GetFirstImageInList(fx_info->images);
190 for ( ; next != (Image *) NULL; next=next->next)
cristy3ed852e2009-09-05 21:47:34 +0000191 {
cristydb070952012-04-20 14:33:00 +0000192 fx_info->view[i]=AcquireVirtualCacheView(next,exception);
cristya2262262011-03-11 02:50:37 +0000193 i++;
cristy3ed852e2009-09-05 21:47:34 +0000194 }
195 fx_info->random_info=AcquireRandomInfo();
196 fx_info->expression=ConstantString(expression);
197 fx_info->file=stderr;
198 (void) SubstituteString(&fx_info->expression," ",""); /* compact string */
cristy37af0912011-05-23 16:09:42 +0000199 /*
200 Force right-to-left associativity for unary negation.
201 */
202 (void) SubstituteString(&fx_info->expression,"-","-1.0*");
cristy8b8a3ae2010-10-23 18:49:46 +0000203 /*
cristy3ed852e2009-09-05 21:47:34 +0000204 Convert complex to simple operators.
205 */
206 fx_op[1]='\0';
207 *fx_op=(char) LeftShiftOperator;
208 (void) SubstituteString(&fx_info->expression,"<<",fx_op);
209 *fx_op=(char) RightShiftOperator;
210 (void) SubstituteString(&fx_info->expression,">>",fx_op);
211 *fx_op=(char) LessThanEqualOperator;
212 (void) SubstituteString(&fx_info->expression,"<=",fx_op);
213 *fx_op=(char) GreaterThanEqualOperator;
214 (void) SubstituteString(&fx_info->expression,">=",fx_op);
215 *fx_op=(char) EqualOperator;
216 (void) SubstituteString(&fx_info->expression,"==",fx_op);
217 *fx_op=(char) NotEqualOperator;
218 (void) SubstituteString(&fx_info->expression,"!=",fx_op);
219 *fx_op=(char) LogicalAndOperator;
220 (void) SubstituteString(&fx_info->expression,"&&",fx_op);
221 *fx_op=(char) LogicalOrOperator;
222 (void) SubstituteString(&fx_info->expression,"||",fx_op);
cristy116af162010-08-13 01:25:47 +0000223 *fx_op=(char) ExponentialNotation;
224 (void) SubstituteString(&fx_info->expression,"**",fx_op);
cristy3ed852e2009-09-05 21:47:34 +0000225 return(fx_info);
226}
227
228/*
229%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
230% %
231% %
232% %
233% A d d N o i s e I m a g e %
234% %
235% %
236% %
237%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
238%
239% AddNoiseImage() adds random noise to the image.
240%
241% The format of the AddNoiseImage method is:
242%
243% Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
cristy9ed1f812011-10-08 02:00:08 +0000244% const double attenuate,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000245%
246% A description of each parameter follows:
247%
248% o image: the image.
249%
250% o channel: the channel type.
251%
252% o noise_type: The type of noise: Uniform, Gaussian, Multiplicative,
253% Impulse, Laplacian, or Poisson.
254%
cristy9ed1f812011-10-08 02:00:08 +0000255% o attenuate: attenuate the random distribution.
256%
cristy3ed852e2009-09-05 21:47:34 +0000257% o exception: return any errors or warnings in this structure.
258%
259*/
cristy9ed1f812011-10-08 02:00:08 +0000260MagickExport Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
261 const double attenuate,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000262{
263#define AddNoiseImageTag "AddNoise/Image"
264
cristyfa112112010-01-04 17:48:07 +0000265 CacheView
266 *image_view,
267 *noise_view;
268
cristy3ed852e2009-09-05 21:47:34 +0000269 Image
270 *noise_image;
271
cristy3ed852e2009-09-05 21:47:34 +0000272 MagickBooleanType
273 status;
274
cristybb503372010-05-27 20:51:26 +0000275 MagickOffsetType
276 progress;
277
cristy3ed852e2009-09-05 21:47:34 +0000278 RandomInfo
cristyfa112112010-01-04 17:48:07 +0000279 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +0000280
cristybb503372010-05-27 20:51:26 +0000281 ssize_t
282 y;
283
cristy57340e02012-05-05 00:53:23 +0000284 unsigned long
285 key;
286
cristy3ed852e2009-09-05 21:47:34 +0000287 /*
288 Initialize noise image attributes.
289 */
290 assert(image != (const Image *) NULL);
291 assert(image->signature == MagickSignature);
292 if (image->debug != MagickFalse)
293 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
294 assert(exception != (ExceptionInfo *) NULL);
295 assert(exception->signature == MagickSignature);
cristyb2145892011-10-10 00:55:32 +0000296 noise_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000297 if (noise_image == (Image *) NULL)
298 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000299 if (SetImageStorageClass(noise_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000300 {
cristy3ed852e2009-09-05 21:47:34 +0000301 noise_image=DestroyImage(noise_image);
302 return((Image *) NULL);
303 }
304 /*
305 Add noise in each row.
306 */
cristy3ed852e2009-09-05 21:47:34 +0000307 status=MagickTrue;
308 progress=0;
309 random_info=AcquireRandomInfoThreadSet();
cristydb070952012-04-20 14:33:00 +0000310 image_view=AcquireVirtualCacheView(image,exception);
311 noise_view=AcquireAuthenticCacheView(noise_image,exception);
cristy57340e02012-05-05 00:53:23 +0000312 key=GetRandomSecretKey(random_info[0]);
cristy319a1e72010-02-21 15:13:11 +0000313#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy57340e02012-05-05 00:53:23 +0000314 #pragma omp parallel for schedule(static,4) shared(progress,status) \
315 if (key == ~0UL) num_threads(GetMagickResourceLimit(ThreadResource))
cristy3ed852e2009-09-05 21:47:34 +0000316#endif
cristybb503372010-05-27 20:51:26 +0000317 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000318 {
cristy5c9e6f22010-09-17 17:31:01 +0000319 const int
320 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +0000321
cristy3ed852e2009-09-05 21:47:34 +0000322 MagickBooleanType
323 sync;
324
cristy4c08aed2011-07-01 19:47:50 +0000325 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000326 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000327
cristybb503372010-05-27 20:51:26 +0000328 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000329 x;
330
cristy4c08aed2011-07-01 19:47:50 +0000331 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000332 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000333
334 if (status == MagickFalse)
335 continue;
336 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +0000337 q=QueueCacheViewAuthenticPixels(noise_view,0,y,noise_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +0000338 exception);
cristy4c08aed2011-07-01 19:47:50 +0000339 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000340 {
341 status=MagickFalse;
342 continue;
343 }
cristybb503372010-05-27 20:51:26 +0000344 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000345 {
cristy850b3072011-10-08 01:38:05 +0000346 register ssize_t
347 i;
348
cristy177e41c2012-04-15 15:08:25 +0000349 if (GetPixelMask(image,p) != 0)
350 {
351 p+=GetPixelChannels(image);
352 q+=GetPixelChannels(noise_image);
353 continue;
354 }
cristy850b3072011-10-08 01:38:05 +0000355 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
356 {
357 PixelChannel
358 channel;
359
360 PixelTrait
361 noise_traits,
362 traits;
363
cristye2a912b2011-12-05 20:02:07 +0000364 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +0000365 traits=GetPixelChannelMapTraits(image,channel);
cristy850b3072011-10-08 01:38:05 +0000366 noise_traits=GetPixelChannelMapTraits(noise_image,channel);
367 if ((traits == UndefinedPixelTrait) ||
368 (noise_traits == UndefinedPixelTrait))
369 continue;
cristy177e41c2012-04-15 15:08:25 +0000370 if ((noise_traits & CopyPixelTrait) != 0)
cristyb2145892011-10-10 00:55:32 +0000371 {
372 SetPixelChannel(noise_image,channel,p[i],q);
373 continue;
374 }
cristy850b3072011-10-08 01:38:05 +0000375 SetPixelChannel(noise_image,channel,ClampToQuantum(
376 GenerateDifferentialNoise(random_info[id],p[i],noise_type,attenuate)),
377 q);
378 }
cristyed231572011-07-14 02:18:59 +0000379 p+=GetPixelChannels(image);
380 q+=GetPixelChannels(noise_image);
cristy3ed852e2009-09-05 21:47:34 +0000381 }
382 sync=SyncCacheViewAuthenticPixels(noise_view,exception);
383 if (sync == MagickFalse)
384 status=MagickFalse;
385 if (image->progress_monitor != (MagickProgressMonitor) NULL)
386 {
387 MagickBooleanType
388 proceed;
389
cristyb5d5f722009-11-04 03:03:49 +0000390#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy319a1e72010-02-21 15:13:11 +0000391 #pragma omp critical (MagickCore_AddNoiseImage)
cristy3ed852e2009-09-05 21:47:34 +0000392#endif
393 proceed=SetImageProgress(image,AddNoiseImageTag,progress++,
394 image->rows);
395 if (proceed == MagickFalse)
396 status=MagickFalse;
397 }
398 }
399 noise_view=DestroyCacheView(noise_view);
400 image_view=DestroyCacheView(image_view);
401 random_info=DestroyRandomInfoThreadSet(random_info);
402 if (status == MagickFalse)
403 noise_image=DestroyImage(noise_image);
404 return(noise_image);
405}
406
407/*
408%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
409% %
410% %
411% %
412% B l u e S h i f t I m a g e %
413% %
414% %
415% %
416%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
417%
418% BlueShiftImage() mutes the colors of the image to simulate a scene at
419% nighttime in the moonlight.
420%
421% The format of the BlueShiftImage method is:
422%
423% Image *BlueShiftImage(const Image *image,const double factor,
424% ExceptionInfo *exception)
425%
426% A description of each parameter follows:
427%
428% o image: the image.
429%
430% o factor: the shift factor.
431%
432% o exception: return any errors or warnings in this structure.
433%
434*/
435MagickExport Image *BlueShiftImage(const Image *image,const double factor,
436 ExceptionInfo *exception)
437{
438#define BlueShiftImageTag "BlueShift/Image"
439
cristyc4c8d132010-01-07 01:58:38 +0000440 CacheView
441 *image_view,
442 *shift_view;
443
cristy3ed852e2009-09-05 21:47:34 +0000444 Image
445 *shift_image;
446
cristy3ed852e2009-09-05 21:47:34 +0000447 MagickBooleanType
448 status;
449
cristybb503372010-05-27 20:51:26 +0000450 MagickOffsetType
451 progress;
452
453 ssize_t
454 y;
455
cristy3ed852e2009-09-05 21:47:34 +0000456 /*
457 Allocate blue shift image.
458 */
459 assert(image != (const Image *) NULL);
460 assert(image->signature == MagickSignature);
461 if (image->debug != MagickFalse)
462 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
463 assert(exception != (ExceptionInfo *) NULL);
464 assert(exception->signature == MagickSignature);
cristya6d7a9b2012-01-18 20:04:48 +0000465 shift_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000466 if (shift_image == (Image *) NULL)
467 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000468 if (SetImageStorageClass(shift_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000469 {
cristy3ed852e2009-09-05 21:47:34 +0000470 shift_image=DestroyImage(shift_image);
471 return((Image *) NULL);
472 }
473 /*
474 Blue-shift DirectClass image.
475 */
476 status=MagickTrue;
477 progress=0;
cristydb070952012-04-20 14:33:00 +0000478 image_view=AcquireVirtualCacheView(image,exception);
479 shift_view=AcquireAuthenticCacheView(shift_image,exception);
cristy319a1e72010-02-21 15:13:11 +0000480#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000481 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000482#endif
cristybb503372010-05-27 20:51:26 +0000483 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000484 {
485 MagickBooleanType
486 sync;
487
cristy4c08aed2011-07-01 19:47:50 +0000488 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000489 pixel;
490
491 Quantum
492 quantum;
493
cristy4c08aed2011-07-01 19:47:50 +0000494 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000495 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000496
cristybb503372010-05-27 20:51:26 +0000497 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000498 x;
499
cristy4c08aed2011-07-01 19:47:50 +0000500 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000501 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000502
503 if (status == MagickFalse)
504 continue;
505 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
506 q=QueueCacheViewAuthenticPixels(shift_view,0,y,shift_image->columns,1,
507 exception);
cristy4c08aed2011-07-01 19:47:50 +0000508 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000509 {
510 status=MagickFalse;
511 continue;
512 }
cristybb503372010-05-27 20:51:26 +0000513 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000514 {
cristy4c08aed2011-07-01 19:47:50 +0000515 quantum=GetPixelRed(image,p);
516 if (GetPixelGreen(image,p) < quantum)
517 quantum=GetPixelGreen(image,p);
518 if (GetPixelBlue(image,p) < quantum)
519 quantum=GetPixelBlue(image,p);
520 pixel.red=0.5*(GetPixelRed(image,p)+factor*quantum);
521 pixel.green=0.5*(GetPixelGreen(image,p)+factor*quantum);
522 pixel.blue=0.5*(GetPixelBlue(image,p)+factor*quantum);
523 quantum=GetPixelRed(image,p);
524 if (GetPixelGreen(image,p) > quantum)
525 quantum=GetPixelGreen(image,p);
526 if (GetPixelBlue(image,p) > quantum)
527 quantum=GetPixelBlue(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000528 pixel.red=0.5*(pixel.red+factor*quantum);
529 pixel.green=0.5*(pixel.green+factor*quantum);
530 pixel.blue=0.5*(pixel.blue+factor*quantum);
cristy4c08aed2011-07-01 19:47:50 +0000531 SetPixelRed(shift_image,ClampToQuantum(pixel.red),q);
532 SetPixelGreen(shift_image,ClampToQuantum(pixel.green),q);
533 SetPixelBlue(shift_image,ClampToQuantum(pixel.blue),q);
cristyed231572011-07-14 02:18:59 +0000534 p+=GetPixelChannels(image);
535 q+=GetPixelChannels(shift_image);
cristy3ed852e2009-09-05 21:47:34 +0000536 }
537 sync=SyncCacheViewAuthenticPixels(shift_view,exception);
538 if (sync == MagickFalse)
539 status=MagickFalse;
540 if (image->progress_monitor != (MagickProgressMonitor) NULL)
541 {
542 MagickBooleanType
543 proceed;
544
cristy319a1e72010-02-21 15:13:11 +0000545#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +0000546 #pragma omp critical (MagickCore_BlueShiftImage)
cristy3ed852e2009-09-05 21:47:34 +0000547#endif
548 proceed=SetImageProgress(image,BlueShiftImageTag,progress++,
549 image->rows);
550 if (proceed == MagickFalse)
551 status=MagickFalse;
552 }
553 }
554 image_view=DestroyCacheView(image_view);
555 shift_view=DestroyCacheView(shift_view);
556 if (status == MagickFalse)
557 shift_image=DestroyImage(shift_image);
558 return(shift_image);
559}
560
561/*
562%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
563% %
564% %
565% %
566% C h a r c o a l I m a g e %
567% %
568% %
569% %
570%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
571%
572% CharcoalImage() creates a new image that is a copy of an existing one with
573% the edge highlighted. It allocates the memory necessary for the new Image
574% structure and returns a pointer to the new image.
575%
576% The format of the CharcoalImage method is:
577%
578% Image *CharcoalImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +0000579% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000580%
581% A description of each parameter follows:
582%
583% o image: the image.
584%
585% o radius: the radius of the pixel neighborhood.
586%
587% o sigma: the standard deviation of the Gaussian, in pixels.
588%
589% o exception: return any errors or warnings in this structure.
590%
591*/
592MagickExport Image *CharcoalImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +0000593 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000594{
595 Image
596 *charcoal_image,
597 *clone_image,
598 *edge_image;
599
600 assert(image != (Image *) NULL);
601 assert(image->signature == MagickSignature);
602 if (image->debug != MagickFalse)
603 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
604 assert(exception != (ExceptionInfo *) NULL);
605 assert(exception->signature == MagickSignature);
606 clone_image=CloneImage(image,0,0,MagickTrue,exception);
607 if (clone_image == (Image *) NULL)
608 return((Image *) NULL);
cristy018f07f2011-09-04 21:15:19 +0000609 (void) SetImageType(clone_image,GrayscaleType,exception);
cristy8ae632d2011-09-05 17:29:53 +0000610 edge_image=EdgeImage(clone_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +0000611 clone_image=DestroyImage(clone_image);
612 if (edge_image == (Image *) NULL)
613 return((Image *) NULL);
cristyaa2c16c2012-03-25 22:21:35 +0000614 charcoal_image=BlurImage(edge_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +0000615 edge_image=DestroyImage(edge_image);
616 if (charcoal_image == (Image *) NULL)
617 return((Image *) NULL);
cristye23ec9d2011-08-16 18:15:40 +0000618 (void) NormalizeImage(charcoal_image,exception);
cristyb3e7c6c2011-07-24 01:43:55 +0000619 (void) NegateImage(charcoal_image,MagickFalse,exception);
cristy018f07f2011-09-04 21:15:19 +0000620 (void) SetImageType(charcoal_image,GrayscaleType,exception);
cristy3ed852e2009-09-05 21:47:34 +0000621 return(charcoal_image);
622}
623
624/*
625%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
626% %
627% %
628% %
629% C o l o r i z e I m a g e %
630% %
631% %
632% %
633%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
634%
635% ColorizeImage() blends the fill color with each pixel in the image.
636% A percentage blend is specified with opacity. Control the application
637% of different color components by specifying a different percentage for
638% each component (e.g. 90/100/10 is 90% red, 100% green, and 10% blue).
639%
640% The format of the ColorizeImage method is:
641%
cristyc7e6ff62011-10-03 13:46:11 +0000642% Image *ColorizeImage(const Image *image,const char *blend,
643% const PixelInfo *colorize,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000644%
645% A description of each parameter follows:
646%
647% o image: the image.
648%
cristyc7e6ff62011-10-03 13:46:11 +0000649% o blend: A character string indicating the level of blending as a
cristy3ed852e2009-09-05 21:47:34 +0000650% percentage.
651%
652% o colorize: A color value.
653%
654% o exception: return any errors or warnings in this structure.
655%
656*/
cristyc7e6ff62011-10-03 13:46:11 +0000657MagickExport Image *ColorizeImage(const Image *image,const char *blend,
658 const PixelInfo *colorize,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000659{
660#define ColorizeImageTag "Colorize/Image"
cristy20c3aed2012-04-10 01:06:21 +0000661#define Colorize(pixel,blend_percentage,colorize) \
662 (pixel)=((pixel)*(100.0-(blend_percentage))+(colorize)*(blend_percentage))/100.0;
cristy3ed852e2009-09-05 21:47:34 +0000663
cristyc4c8d132010-01-07 01:58:38 +0000664 CacheView
665 *colorize_view,
666 *image_view;
667
cristy3ed852e2009-09-05 21:47:34 +0000668 GeometryInfo
669 geometry_info;
670
671 Image
672 *colorize_image;
673
cristy3ed852e2009-09-05 21:47:34 +0000674 MagickBooleanType
675 status;
676
cristybb503372010-05-27 20:51:26 +0000677 MagickOffsetType
678 progress;
679
cristy3ed852e2009-09-05 21:47:34 +0000680 MagickStatusType
681 flags;
682
cristyc7e6ff62011-10-03 13:46:11 +0000683 PixelInfo
cristy20c3aed2012-04-10 01:06:21 +0000684 blend_percentage;
cristyc7e6ff62011-10-03 13:46:11 +0000685
cristybb503372010-05-27 20:51:26 +0000686 ssize_t
687 y;
688
cristy3ed852e2009-09-05 21:47:34 +0000689 /*
690 Allocate colorized image.
691 */
692 assert(image != (const Image *) NULL);
693 assert(image->signature == MagickSignature);
694 if (image->debug != MagickFalse)
695 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
696 assert(exception != (ExceptionInfo *) NULL);
697 assert(exception->signature == MagickSignature);
cristy768165d2012-04-09 15:01:35 +0000698 colorize_image=CloneImage(image,image->columns,image->rows,MagickTrue,
699 exception);
700 if (colorize_image == (Image *) NULL)
701 return((Image *) NULL);
702 if (SetImageStorageClass(colorize_image,DirectClass,exception) == MagickFalse)
703 {
704 colorize_image=DestroyImage(colorize_image);
705 return((Image *) NULL);
706 }
cristy9b7a4fc2012-04-08 22:26:56 +0000707 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
cristy20c3aed2012-04-10 01:06:21 +0000708 (IsPixelInfoGray(colorize) != MagickFalse))
cristy52f2e1e2012-04-10 00:51:19 +0000709 (void) SetImageColorspace(colorize_image,sRGBColorspace,exception);
cristy20c3aed2012-04-10 01:06:21 +0000710 if ((colorize_image->matte == MagickFalse) &&
711 (colorize->matte != MagickFalse))
cristy768165d2012-04-09 15:01:35 +0000712 (void) SetImageAlpha(colorize_image,OpaqueAlpha,exception);
713 if (blend == (const char *) NULL)
714 return(colorize_image);
cristy20c3aed2012-04-10 01:06:21 +0000715 GetPixelInfo(image,&blend_percentage);
716 flags=ParseGeometry(blend,&geometry_info);
717 blend_percentage.red=geometry_info.rho;
718 blend_percentage.green=geometry_info.rho;
719 blend_percentage.blue=geometry_info.rho;
720 blend_percentage.black=geometry_info.rho;
cristy3ee7de52012-04-14 23:40:47 +0000721 blend_percentage.alpha=geometry_info.rho;
cristy20c3aed2012-04-10 01:06:21 +0000722 if ((flags & SigmaValue) != 0)
723 blend_percentage.green=geometry_info.sigma;
724 if ((flags & XiValue) != 0)
725 blend_percentage.blue=geometry_info.xi;
726 if ((flags & PsiValue) != 0)
727 blend_percentage.alpha=geometry_info.psi;
728 if (blend_percentage.colorspace == CMYKColorspace)
729 {
730 if ((flags & PsiValue) != 0)
731 blend_percentage.black=geometry_info.psi;
732 if ((flags & ChiValue) != 0)
733 blend_percentage.alpha=geometry_info.chi;
734 }
cristy3ed852e2009-09-05 21:47:34 +0000735 /*
736 Colorize DirectClass image.
737 */
738 status=MagickTrue;
739 progress=0;
cristydb070952012-04-20 14:33:00 +0000740 image_view=AcquireVirtualCacheView(image,exception);
741 colorize_view=AcquireAuthenticCacheView(colorize_image,exception);
cristy319a1e72010-02-21 15:13:11 +0000742#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000743 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000744#endif
cristybb503372010-05-27 20:51:26 +0000745 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000746 {
747 MagickBooleanType
748 sync;
749
cristyf9bf43e2012-04-07 18:13:07 +0000750 PixelInfo
751 pixel;
752
cristy4c08aed2011-07-01 19:47:50 +0000753 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000754 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000755
cristybb503372010-05-27 20:51:26 +0000756 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000757 x;
758
cristy4c08aed2011-07-01 19:47:50 +0000759 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000760 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000761
762 if (status == MagickFalse)
763 continue;
764 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
765 q=QueueCacheViewAuthenticPixels(colorize_view,0,y,colorize_image->columns,1,
766 exception);
cristy4c08aed2011-07-01 19:47:50 +0000767 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000768 {
769 status=MagickFalse;
770 continue;
771 }
cristy66d3e1f2012-04-14 23:21:39 +0000772 GetPixelInfo(colorize_image,&pixel);
cristybb503372010-05-27 20:51:26 +0000773 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000774 {
cristyd6803382012-04-10 01:41:25 +0000775 if (GetPixelMask(colorize_image,q) != 0)
776 {
777 p+=GetPixelChannels(image);
778 q+=GetPixelChannels(colorize_image);
779 continue;
780 }
cristyf9bf43e2012-04-07 18:13:07 +0000781 GetPixelInfoPixel(image,p,&pixel);
cristy20c3aed2012-04-10 01:06:21 +0000782 Colorize(pixel.red,blend_percentage.red,colorize->red);
783 Colorize(pixel.green,blend_percentage.green,colorize->green);
784 Colorize(pixel.blue,blend_percentage.blue,colorize->blue);
785 Colorize(pixel.black,blend_percentage.black,colorize->black);
786 Colorize(pixel.alpha,blend_percentage.alpha,colorize->alpha);
cristyf9bf43e2012-04-07 18:13:07 +0000787 SetPixelInfoPixel(colorize_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +0000788 p+=GetPixelChannels(image);
789 q+=GetPixelChannels(colorize_image);
cristy3ed852e2009-09-05 21:47:34 +0000790 }
791 sync=SyncCacheViewAuthenticPixels(colorize_view,exception);
792 if (sync == MagickFalse)
793 status=MagickFalse;
794 if (image->progress_monitor != (MagickProgressMonitor) NULL)
795 {
796 MagickBooleanType
797 proceed;
798
cristy319a1e72010-02-21 15:13:11 +0000799#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +0000800 #pragma omp critical (MagickCore_ColorizeImage)
cristy3ed852e2009-09-05 21:47:34 +0000801#endif
802 proceed=SetImageProgress(image,ColorizeImageTag,progress++,image->rows);
803 if (proceed == MagickFalse)
804 status=MagickFalse;
805 }
806 }
807 image_view=DestroyCacheView(image_view);
808 colorize_view=DestroyCacheView(colorize_view);
809 if (status == MagickFalse)
810 colorize_image=DestroyImage(colorize_image);
811 return(colorize_image);
812}
813
814/*
815%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
816% %
817% %
818% %
cristye6365592010-04-02 17:31:23 +0000819% C o l o r M a t r i x I m a g e %
820% %
821% %
822% %
823%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
824%
825% ColorMatrixImage() applies color transformation to an image. This method
826% permits saturation changes, hue rotation, luminance to alpha, and various
827% other effects. Although variable-sized transformation matrices can be used,
828% typically one uses a 5x5 matrix for an RGBA image and a 6x6 for CMYKA
829% (or RGBA with offsets). The matrix is similar to those used by Adobe Flash
830% except offsets are in column 6 rather than 5 (in support of CMYKA images)
831% and offsets are normalized (divide Flash offset by 255).
832%
833% The format of the ColorMatrixImage method is:
834%
835% Image *ColorMatrixImage(const Image *image,
836% const KernelInfo *color_matrix,ExceptionInfo *exception)
837%
838% A description of each parameter follows:
839%
840% o image: the image.
841%
842% o color_matrix: the color matrix.
843%
844% o exception: return any errors or warnings in this structure.
845%
846*/
anthonyfd706f92012-01-19 04:22:02 +0000847/* FUTURE: modify to make use of a MagickMatrix Mutliply function
848 That should be provided in "matrix.c"
849 (ASIDE: actually distorts should do this too but currently doesn't)
850*/
851
cristye6365592010-04-02 17:31:23 +0000852MagickExport Image *ColorMatrixImage(const Image *image,
853 const KernelInfo *color_matrix,ExceptionInfo *exception)
854{
855#define ColorMatrixImageTag "ColorMatrix/Image"
856
857 CacheView
858 *color_view,
859 *image_view;
860
861 double
862 ColorMatrix[6][6] =
863 {
864 { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
865 { 0.0, 1.0, 0.0, 0.0, 0.0, 0.0 },
866 { 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 },
867 { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0 },
868 { 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 },
869 { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 }
870 };
871
872 Image
873 *color_image;
874
cristye6365592010-04-02 17:31:23 +0000875 MagickBooleanType
876 status;
877
cristybb503372010-05-27 20:51:26 +0000878 MagickOffsetType
879 progress;
880
881 register ssize_t
cristye6365592010-04-02 17:31:23 +0000882 i;
883
cristybb503372010-05-27 20:51:26 +0000884 ssize_t
885 u,
886 v,
887 y;
888
cristye6365592010-04-02 17:31:23 +0000889 /*
anthonyfd706f92012-01-19 04:22:02 +0000890 Map given color_matrix, into a 6x6 matrix RGBKA and a constant
cristye6365592010-04-02 17:31:23 +0000891 */
892 assert(image != (Image *) NULL);
893 assert(image->signature == MagickSignature);
894 if (image->debug != MagickFalse)
895 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
896 assert(exception != (ExceptionInfo *) NULL);
897 assert(exception->signature == MagickSignature);
898 i=0;
cristybb503372010-05-27 20:51:26 +0000899 for (v=0; v < (ssize_t) color_matrix->height; v++)
900 for (u=0; u < (ssize_t) color_matrix->width; u++)
cristye6365592010-04-02 17:31:23 +0000901 {
902 if ((v < 6) && (u < 6))
903 ColorMatrix[v][u]=color_matrix->values[i];
904 i++;
905 }
906 /*
907 Initialize color image.
908 */
cristy12550e62010-06-07 12:46:40 +0000909 color_image=CloneImage(image,0,0,MagickTrue,exception);
cristye6365592010-04-02 17:31:23 +0000910 if (color_image == (Image *) NULL)
911 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000912 if (SetImageStorageClass(color_image,DirectClass,exception) == MagickFalse)
cristye6365592010-04-02 17:31:23 +0000913 {
cristye6365592010-04-02 17:31:23 +0000914 color_image=DestroyImage(color_image);
915 return((Image *) NULL);
916 }
917 if (image->debug != MagickFalse)
918 {
919 char
920 format[MaxTextExtent],
921 *message;
922
923 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
924 " ColorMatrix image with color matrix:");
925 message=AcquireString("");
926 for (v=0; v < 6; v++)
927 {
928 *message='\0';
cristyb51dff52011-05-19 16:55:47 +0000929 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
cristye6365592010-04-02 17:31:23 +0000930 (void) ConcatenateString(&message,format);
931 for (u=0; u < 6; u++)
932 {
cristyb51dff52011-05-19 16:55:47 +0000933 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",
cristye6365592010-04-02 17:31:23 +0000934 ColorMatrix[v][u]);
935 (void) ConcatenateString(&message,format);
936 }
937 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
938 }
939 message=DestroyString(message);
940 }
941 /*
anthonyfd706f92012-01-19 04:22:02 +0000942 Apply the ColorMatrix to image.
cristye6365592010-04-02 17:31:23 +0000943 */
944 status=MagickTrue;
945 progress=0;
cristydb070952012-04-20 14:33:00 +0000946 image_view=AcquireVirtualCacheView(image,exception);
947 color_view=AcquireAuthenticCacheView(color_image,exception);
cristye6365592010-04-02 17:31:23 +0000948#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000949 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristye6365592010-04-02 17:31:23 +0000950#endif
cristybb503372010-05-27 20:51:26 +0000951 for (y=0; y < (ssize_t) image->rows; y++)
cristye6365592010-04-02 17:31:23 +0000952 {
cristyfcc25d92012-02-19 23:06:48 +0000953 PixelInfo
cristye6365592010-04-02 17:31:23 +0000954 pixel;
955
cristy4c08aed2011-07-01 19:47:50 +0000956 register const Quantum
cristye6365592010-04-02 17:31:23 +0000957 *restrict p;
958
cristy4c08aed2011-07-01 19:47:50 +0000959 register Quantum
960 *restrict q;
961
cristybb503372010-05-27 20:51:26 +0000962 register ssize_t
cristye6365592010-04-02 17:31:23 +0000963 x;
964
cristye6365592010-04-02 17:31:23 +0000965 if (status == MagickFalse)
966 continue;
967 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
968 q=GetCacheViewAuthenticPixels(color_view,0,y,color_image->columns,1,
969 exception);
cristy4c08aed2011-07-01 19:47:50 +0000970 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristye6365592010-04-02 17:31:23 +0000971 {
972 status=MagickFalse;
973 continue;
974 }
cristyfcc25d92012-02-19 23:06:48 +0000975 GetPixelInfo(image,&pixel);
cristybb503372010-05-27 20:51:26 +0000976 for (x=0; x < (ssize_t) image->columns; x++)
cristye6365592010-04-02 17:31:23 +0000977 {
cristybb503372010-05-27 20:51:26 +0000978 register ssize_t
cristye6365592010-04-02 17:31:23 +0000979 v;
980
cristybb503372010-05-27 20:51:26 +0000981 size_t
cristye6365592010-04-02 17:31:23 +0000982 height;
983
cristyfcc25d92012-02-19 23:06:48 +0000984 GetPixelInfoPixel(image,p,&pixel);
cristye6365592010-04-02 17:31:23 +0000985 height=color_matrix->height > 6 ? 6UL : color_matrix->height;
cristybb503372010-05-27 20:51:26 +0000986 for (v=0; v < (ssize_t) height; v++)
cristye6365592010-04-02 17:31:23 +0000987 {
cristyfcc25d92012-02-19 23:06:48 +0000988 MagickRealType
989 sum;
990
991 sum=ColorMatrix[v][0]*GetPixelRed(image,p)+ColorMatrix[v][1]*
cristy4c08aed2011-07-01 19:47:50 +0000992 GetPixelGreen(image,p)+ColorMatrix[v][2]*GetPixelBlue(image,p);
cristye6365592010-04-02 17:31:23 +0000993 if (image->colorspace == CMYKColorspace)
cristyfcc25d92012-02-19 23:06:48 +0000994 sum+=ColorMatrix[v][3]*GetPixelBlack(image,p);
cristy4c08aed2011-07-01 19:47:50 +0000995 if (image->matte != MagickFalse)
cristyfcc25d92012-02-19 23:06:48 +0000996 sum+=ColorMatrix[v][4]*GetPixelAlpha(image,p);
997 sum+=QuantumRange*ColorMatrix[v][5];
cristye6365592010-04-02 17:31:23 +0000998 switch (v)
999 {
cristyfcc25d92012-02-19 23:06:48 +00001000 case 0: pixel.red=sum; break;
1001 case 1: pixel.green=sum; break;
1002 case 2: pixel.blue=sum; break;
1003 case 3: pixel.black=sum; break;
1004 case 4: pixel.alpha=sum; break;
1005 default: break;
cristye6365592010-04-02 17:31:23 +00001006 }
1007 }
cristyfcc25d92012-02-19 23:06:48 +00001008 SetPixelInfoPixel(color_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +00001009 p+=GetPixelChannels(image);
1010 q+=GetPixelChannels(color_image);
cristye6365592010-04-02 17:31:23 +00001011 }
1012 if (SyncCacheViewAuthenticPixels(color_view,exception) == MagickFalse)
1013 status=MagickFalse;
1014 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1015 {
1016 MagickBooleanType
1017 proceed;
1018
1019#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00001020 #pragma omp critical (MagickCore_ColorMatrixImage)
cristye6365592010-04-02 17:31:23 +00001021#endif
1022 proceed=SetImageProgress(image,ColorMatrixImageTag,progress++,
1023 image->rows);
1024 if (proceed == MagickFalse)
1025 status=MagickFalse;
1026 }
1027 }
1028 color_view=DestroyCacheView(color_view);
1029 image_view=DestroyCacheView(image_view);
1030 if (status == MagickFalse)
1031 color_image=DestroyImage(color_image);
1032 return(color_image);
1033}
1034
1035/*
1036%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1037% %
1038% %
1039% %
cristy3ed852e2009-09-05 21:47:34 +00001040+ D e s t r o y F x I n f o %
1041% %
1042% %
1043% %
1044%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1045%
1046% DestroyFxInfo() deallocates memory associated with an FxInfo structure.
1047%
1048% The format of the DestroyFxInfo method is:
1049%
1050% ImageInfo *DestroyFxInfo(ImageInfo *fx_info)
1051%
1052% A description of each parameter follows:
1053%
1054% o fx_info: the fx info.
1055%
1056*/
cristy7832dc22011-09-05 01:21:53 +00001057MagickPrivate FxInfo *DestroyFxInfo(FxInfo *fx_info)
cristy3ed852e2009-09-05 21:47:34 +00001058{
cristybb503372010-05-27 20:51:26 +00001059 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001060 i;
1061
1062 fx_info->exception=DestroyExceptionInfo(fx_info->exception);
1063 fx_info->expression=DestroyString(fx_info->expression);
1064 fx_info->symbols=DestroySplayTree(fx_info->symbols);
1065 fx_info->colors=DestroySplayTree(fx_info->colors);
cristy0ea377f2011-03-24 00:54:19 +00001066 for (i=(ssize_t) GetImageListLength(fx_info->images)-1; i >= 0; i--)
cristyd76c51e2011-03-26 00:21:26 +00001067 fx_info->view[i]=DestroyCacheView(fx_info->view[i]);
1068 fx_info->view=(CacheView **) RelinquishMagickMemory(fx_info->view);
cristy3ed852e2009-09-05 21:47:34 +00001069 fx_info->random_info=DestroyRandomInfo(fx_info->random_info);
1070 fx_info=(FxInfo *) RelinquishMagickMemory(fx_info);
1071 return(fx_info);
1072}
1073
1074/*
1075%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1076% %
1077% %
1078% %
cristy3ed852e2009-09-05 21:47:34 +00001079+ 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 %
1080% %
1081% %
1082% %
1083%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1084%
1085% FxEvaluateChannelExpression() evaluates an expression and returns the
1086% results.
1087%
1088% The format of the FxEvaluateExpression method is:
1089%
1090% MagickRealType FxEvaluateChannelExpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00001091% const PixelChannel channel,const ssize_t x,const ssize_t y,
cristy3ed852e2009-09-05 21:47:34 +00001092% MagickRealType *alpha,Exceptioninfo *exception)
1093% MagickRealType FxEvaluateExpression(FxInfo *fx_info,
1094% MagickRealType *alpha,Exceptioninfo *exception)
1095%
1096% A description of each parameter follows:
1097%
1098% o fx_info: the fx info.
1099%
1100% o channel: the channel.
1101%
1102% o x,y: the pixel position.
1103%
1104% o alpha: the result.
1105%
1106% o exception: return any errors or warnings in this structure.
1107%
1108*/
1109
cristy351842f2010-03-07 15:27:38 +00001110static inline double MagickMax(const double x,const double y)
1111{
1112 if (x > y)
1113 return(x);
1114 return(y);
1115}
1116
1117static inline double MagickMin(const double x,const double y)
1118{
1119 if (x < y)
1120 return(x);
1121 return(y);
1122}
1123
cristy3ed852e2009-09-05 21:47:34 +00001124static MagickRealType FxChannelStatistics(FxInfo *fx_info,const Image *image,
cristy0568ffc2011-07-25 16:54:14 +00001125 PixelChannel channel,const char *symbol,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001126{
1127 char
1128 key[MaxTextExtent],
1129 statistic[MaxTextExtent];
1130
1131 const char
1132 *value;
1133
1134 register const char
1135 *p;
1136
1137 for (p=symbol; (*p != '.') && (*p != '\0'); p++) ;
1138 if (*p == '.')
1139 switch (*++p) /* e.g. depth.r */
1140 {
cristy541ae572011-08-05 19:08:59 +00001141 case 'r': channel=RedPixelChannel; break;
1142 case 'g': channel=GreenPixelChannel; break;
1143 case 'b': channel=BluePixelChannel; break;
1144 case 'c': channel=CyanPixelChannel; break;
1145 case 'm': channel=MagentaPixelChannel; break;
1146 case 'y': channel=YellowPixelChannel; break;
1147 case 'k': channel=BlackPixelChannel; break;
cristy3ed852e2009-09-05 21:47:34 +00001148 default: break;
1149 }
cristyb51dff52011-05-19 16:55:47 +00001150 (void) FormatLocaleString(key,MaxTextExtent,"%p.%.20g.%s",(void *) image,
cristye8c25f92010-06-03 00:53:06 +00001151 (double) channel,symbol);
cristy3ed852e2009-09-05 21:47:34 +00001152 value=(const char *) GetValueFromSplayTree(fx_info->symbols,key);
1153 if (value != (const char *) NULL)
cristydbdd0e32011-11-04 23:29:40 +00001154 return(QuantumScale*StringToDouble(value,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001155 (void) DeleteNodeFromSplayTree(fx_info->symbols,key);
1156 if (LocaleNCompare(symbol,"depth",5) == 0)
1157 {
cristybb503372010-05-27 20:51:26 +00001158 size_t
cristy3ed852e2009-09-05 21:47:34 +00001159 depth;
1160
cristyfefab1b2011-07-05 00:33:22 +00001161 depth=GetImageDepth(image,exception);
1162 (void) FormatLocaleString(statistic,MaxTextExtent,"%.20g",(double) depth);
cristy3ed852e2009-09-05 21:47:34 +00001163 }
1164 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1165 {
1166 double
1167 kurtosis,
1168 skewness;
1169
cristyd42d9952011-07-08 14:21:50 +00001170 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
cristyb51dff52011-05-19 16:55:47 +00001171 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",kurtosis);
cristy3ed852e2009-09-05 21:47:34 +00001172 }
1173 if (LocaleNCompare(symbol,"maxima",6) == 0)
1174 {
1175 double
1176 maxima,
1177 minima;
1178
cristyd42d9952011-07-08 14:21:50 +00001179 (void) GetImageRange(image,&minima,&maxima,exception);
cristyb51dff52011-05-19 16:55:47 +00001180 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",maxima);
cristy3ed852e2009-09-05 21:47:34 +00001181 }
1182 if (LocaleNCompare(symbol,"mean",4) == 0)
1183 {
1184 double
1185 mean,
1186 standard_deviation;
1187
cristyd42d9952011-07-08 14:21:50 +00001188 (void) GetImageMean(image,&mean,&standard_deviation,exception);
cristyb51dff52011-05-19 16:55:47 +00001189 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",mean);
cristy3ed852e2009-09-05 21:47:34 +00001190 }
1191 if (LocaleNCompare(symbol,"minima",6) == 0)
1192 {
1193 double
1194 maxima,
1195 minima;
1196
cristyd42d9952011-07-08 14:21:50 +00001197 (void) GetImageRange(image,&minima,&maxima,exception);
cristyb51dff52011-05-19 16:55:47 +00001198 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",minima);
cristy3ed852e2009-09-05 21:47:34 +00001199 }
1200 if (LocaleNCompare(symbol,"skewness",8) == 0)
1201 {
1202 double
1203 kurtosis,
1204 skewness;
1205
cristyd42d9952011-07-08 14:21:50 +00001206 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
cristyb51dff52011-05-19 16:55:47 +00001207 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",skewness);
cristy3ed852e2009-09-05 21:47:34 +00001208 }
1209 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1210 {
1211 double
1212 mean,
1213 standard_deviation;
1214
cristyd42d9952011-07-08 14:21:50 +00001215 (void) GetImageMean(image,&mean,&standard_deviation,exception);
cristyb51dff52011-05-19 16:55:47 +00001216 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",
cristy3ed852e2009-09-05 21:47:34 +00001217 standard_deviation);
1218 }
1219 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(key),
1220 ConstantString(statistic));
cristydbdd0e32011-11-04 23:29:40 +00001221 return(QuantumScale*StringToDouble(statistic,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001222}
1223
1224static MagickRealType
cristy0568ffc2011-07-25 16:54:14 +00001225 FxEvaluateSubexpression(FxInfo *,const PixelChannel,const ssize_t,
cristye85007d2010-06-06 22:51:36 +00001226 const ssize_t,const char *,MagickRealType *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +00001227
cristyb0aad4c2011-11-02 19:30:35 +00001228static MagickOffsetType FxGCD(MagickOffsetType alpha,MagickOffsetType beta)
1229{
1230 if (beta != 0)
1231 return(FxGCD(beta,alpha % beta));
1232 return(alpha);
1233}
1234
cristy3ed852e2009-09-05 21:47:34 +00001235static inline const char *FxSubexpression(const char *expression,
1236 ExceptionInfo *exception)
1237{
1238 const char
1239 *subexpression;
1240
cristybb503372010-05-27 20:51:26 +00001241 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001242 level;
1243
1244 level=0;
1245 subexpression=expression;
1246 while ((*subexpression != '\0') &&
1247 ((level != 1) || (strchr(")",(int) *subexpression) == (char *) NULL)))
1248 {
1249 if (strchr("(",(int) *subexpression) != (char *) NULL)
1250 level++;
1251 else
1252 if (strchr(")",(int) *subexpression) != (char *) NULL)
1253 level--;
1254 subexpression++;
1255 }
1256 if (*subexpression == '\0')
1257 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
anthonye5b39652012-04-21 05:37:29 +00001258 "UnbalancedParenthesis","'%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00001259 return(subexpression);
1260}
1261
cristy0568ffc2011-07-25 16:54:14 +00001262static MagickRealType FxGetSymbol(FxInfo *fx_info,const PixelChannel channel,
cristye85007d2010-06-06 22:51:36 +00001263 const ssize_t x,const ssize_t y,const char *expression,
1264 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001265{
1266 char
1267 *q,
1268 subexpression[MaxTextExtent],
1269 symbol[MaxTextExtent];
1270
1271 const char
1272 *p,
1273 *value;
1274
1275 Image
1276 *image;
1277
cristy4c08aed2011-07-01 19:47:50 +00001278 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001279 pixel;
1280
1281 MagickRealType
1282 alpha,
1283 beta;
1284
1285 PointInfo
1286 point;
1287
cristybb503372010-05-27 20:51:26 +00001288 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001289 i;
1290
1291 size_t
cristy1707c6c2012-01-18 23:30:54 +00001292 length,
cristy3ed852e2009-09-05 21:47:34 +00001293 level;
1294
1295 p=expression;
1296 i=GetImageIndexInList(fx_info->images);
1297 level=0;
1298 point.x=(double) x;
1299 point.y=(double) y;
1300 if (isalpha((int) *(p+1)) == 0)
1301 {
1302 if (strchr("suv",(int) *p) != (char *) NULL)
1303 {
1304 switch (*p)
1305 {
1306 case 's':
1307 default:
1308 {
1309 i=GetImageIndexInList(fx_info->images);
1310 break;
1311 }
1312 case 'u': i=0; break;
1313 case 'v': i=1; break;
1314 }
1315 p++;
1316 if (*p == '[')
1317 {
1318 level++;
1319 q=subexpression;
1320 for (p++; *p != '\0'; )
1321 {
1322 if (*p == '[')
1323 level++;
1324 else
1325 if (*p == ']')
1326 {
1327 level--;
1328 if (level == 0)
1329 break;
1330 }
1331 *q++=(*p++);
1332 }
1333 *q='\0';
1334 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1335 &beta,exception);
cristybb503372010-05-27 20:51:26 +00001336 i=(ssize_t) (alpha+0.5);
cristy3ed852e2009-09-05 21:47:34 +00001337 p++;
1338 }
1339 if (*p == '.')
1340 p++;
1341 }
1342 if ((isalpha((int) *(p+1)) == 0) && (*p == 'p'))
1343 {
1344 p++;
1345 if (*p == '{')
1346 {
1347 level++;
1348 q=subexpression;
1349 for (p++; *p != '\0'; )
1350 {
1351 if (*p == '{')
1352 level++;
1353 else
1354 if (*p == '}')
1355 {
1356 level--;
1357 if (level == 0)
1358 break;
1359 }
1360 *q++=(*p++);
1361 }
1362 *q='\0';
1363 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1364 &beta,exception);
1365 point.x=alpha;
1366 point.y=beta;
1367 p++;
1368 }
1369 else
1370 if (*p == '[')
1371 {
1372 level++;
1373 q=subexpression;
1374 for (p++; *p != '\0'; )
1375 {
1376 if (*p == '[')
1377 level++;
1378 else
1379 if (*p == ']')
1380 {
1381 level--;
1382 if (level == 0)
1383 break;
1384 }
1385 *q++=(*p++);
1386 }
1387 *q='\0';
1388 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1389 &beta,exception);
1390 point.x+=alpha;
1391 point.y+=beta;
1392 p++;
1393 }
1394 if (*p == '.')
1395 p++;
1396 }
1397 }
1398 length=GetImageListLength(fx_info->images);
1399 while (i < 0)
cristybb503372010-05-27 20:51:26 +00001400 i+=(ssize_t) length;
cristy3ed852e2009-09-05 21:47:34 +00001401 i%=length;
1402 image=GetImageFromList(fx_info->images,i);
1403 if (image == (Image *) NULL)
1404 {
1405 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
anthonye5b39652012-04-21 05:37:29 +00001406 "NoSuchImage","'%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00001407 return(0.0);
1408 }
cristy4c08aed2011-07-01 19:47:50 +00001409 GetPixelInfo(image,&pixel);
1410 (void) InterpolatePixelInfo(image,fx_info->view[i],image->interpolate,
cristy4f820712011-04-01 12:35:43 +00001411 point.x,point.y,&pixel,exception);
cristy1707c6c2012-01-18 23:30:54 +00001412 if ((strlen(p) > 2) && (LocaleCompare(p,"intensity") != 0) &&
1413 (LocaleCompare(p,"luminance") != 0) && (LocaleCompare(p,"hue") != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00001414 (LocaleCompare(p,"saturation") != 0) &&
1415 (LocaleCompare(p,"lightness") != 0))
1416 {
1417 char
1418 name[MaxTextExtent];
1419
1420 (void) CopyMagickString(name,p,MaxTextExtent);
1421 for (q=name+(strlen(name)-1); q > name; q--)
1422 {
1423 if (*q == ')')
1424 break;
1425 if (*q == '.')
1426 {
1427 *q='\0';
1428 break;
1429 }
1430 }
1431 if ((strlen(name) > 2) &&
1432 (GetValueFromSplayTree(fx_info->symbols,name) == (const char *) NULL))
1433 {
cristy4c08aed2011-07-01 19:47:50 +00001434 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001435 *color;
1436
cristy4c08aed2011-07-01 19:47:50 +00001437 color=(PixelInfo *) GetValueFromSplayTree(fx_info->colors,name);
1438 if (color != (PixelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001439 {
1440 pixel=(*color);
1441 p+=strlen(name);
1442 }
1443 else
cristy1707c6c2012-01-18 23:30:54 +00001444 {
1445 MagickBooleanType
1446 status;
1447
1448 status=QueryColorCompliance(name,AllCompliance,&pixel,
1449 fx_info->exception);
1450 if (status != MagickFalse)
1451 {
1452 (void) AddValueToSplayTree(fx_info->colors,ConstantString(
1453 name),ClonePixelInfo(&pixel));
1454 p+=strlen(name);
1455 }
1456 }
cristy3ed852e2009-09-05 21:47:34 +00001457 }
1458 }
1459 (void) CopyMagickString(symbol,p,MaxTextExtent);
1460 StripString(symbol);
1461 if (*symbol == '\0')
1462 {
1463 switch (channel)
1464 {
cristy0568ffc2011-07-25 16:54:14 +00001465 case RedPixelChannel: return(QuantumScale*pixel.red);
1466 case GreenPixelChannel: return(QuantumScale*pixel.green);
1467 case BluePixelChannel: return(QuantumScale*pixel.blue);
1468 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001469 {
1470 if (image->colorspace != CMYKColorspace)
1471 {
1472 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00001473 ImageError,"ColorSeparatedImageRequired","'%s'",
cristy3ed852e2009-09-05 21:47:34 +00001474 image->filename);
1475 return(0.0);
1476 }
cristy4c08aed2011-07-01 19:47:50 +00001477 return(QuantumScale*pixel.black);
1478 }
cristy0568ffc2011-07-25 16:54:14 +00001479 case AlphaPixelChannel:
cristy4c08aed2011-07-01 19:47:50 +00001480 {
1481 MagickRealType
1482 alpha;
1483
1484 if (pixel.matte == MagickFalse)
1485 return(1.0);
1486 alpha=(MagickRealType) (QuantumScale*pixel.alpha);
1487 return(alpha);
cristy3ed852e2009-09-05 21:47:34 +00001488 }
cristya382aca2011-12-06 18:22:48 +00001489 case IndexPixelChannel:
1490 return(0.0);
cristyb3a73b52011-07-26 01:34:43 +00001491 case IntensityPixelChannel:
cristyf364ed42010-12-15 01:54:43 +00001492 {
cristy4c08aed2011-07-01 19:47:50 +00001493 return(QuantumScale*GetPixelInfoIntensity(&pixel));
cristyf364ed42010-12-15 01:54:43 +00001494 }
cristy3ed852e2009-09-05 21:47:34 +00001495 default:
1496 break;
1497 }
1498 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
anthonye5b39652012-04-21 05:37:29 +00001499 "UnableToParseExpression","'%s'",p);
cristy3ed852e2009-09-05 21:47:34 +00001500 return(0.0);
1501 }
1502 switch (*symbol)
1503 {
1504 case 'A':
1505 case 'a':
1506 {
1507 if (LocaleCompare(symbol,"a") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001508 return((MagickRealType) (QuantumScale*pixel.alpha));
cristy3ed852e2009-09-05 21:47:34 +00001509 break;
1510 }
1511 case 'B':
1512 case 'b':
1513 {
1514 if (LocaleCompare(symbol,"b") == 0)
1515 return(QuantumScale*pixel.blue);
1516 break;
1517 }
1518 case 'C':
1519 case 'c':
1520 {
1521 if (LocaleNCompare(symbol,"channel",7) == 0)
1522 {
1523 GeometryInfo
1524 channel_info;
1525
1526 MagickStatusType
1527 flags;
1528
1529 flags=ParseGeometry(symbol+7,&channel_info);
1530 if (image->colorspace == CMYKColorspace)
1531 switch (channel)
1532 {
cristy0568ffc2011-07-25 16:54:14 +00001533 case CyanPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001534 {
1535 if ((flags & RhoValue) == 0)
1536 return(0.0);
1537 return(channel_info.rho);
1538 }
cristy0568ffc2011-07-25 16:54:14 +00001539 case MagentaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001540 {
1541 if ((flags & SigmaValue) == 0)
1542 return(0.0);
1543 return(channel_info.sigma);
1544 }
cristy0568ffc2011-07-25 16:54:14 +00001545 case YellowPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001546 {
1547 if ((flags & XiValue) == 0)
1548 return(0.0);
1549 return(channel_info.xi);
1550 }
cristy0568ffc2011-07-25 16:54:14 +00001551 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001552 {
1553 if ((flags & PsiValue) == 0)
1554 return(0.0);
1555 return(channel_info.psi);
1556 }
cristy0568ffc2011-07-25 16:54:14 +00001557 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001558 {
1559 if ((flags & ChiValue) == 0)
1560 return(0.0);
1561 return(channel_info.chi);
1562 }
1563 default:
1564 return(0.0);
1565 }
1566 switch (channel)
1567 {
cristy0568ffc2011-07-25 16:54:14 +00001568 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001569 {
1570 if ((flags & RhoValue) == 0)
1571 return(0.0);
1572 return(channel_info.rho);
1573 }
cristy0568ffc2011-07-25 16:54:14 +00001574 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001575 {
1576 if ((flags & SigmaValue) == 0)
1577 return(0.0);
1578 return(channel_info.sigma);
1579 }
cristy0568ffc2011-07-25 16:54:14 +00001580 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001581 {
1582 if ((flags & XiValue) == 0)
1583 return(0.0);
1584 return(channel_info.xi);
1585 }
cristy0568ffc2011-07-25 16:54:14 +00001586 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001587 {
1588 if ((flags & ChiValue) == 0)
1589 return(0.0);
1590 return(channel_info.chi);
1591 }
cristy0568ffc2011-07-25 16:54:14 +00001592 case AlphaPixelChannel:
cristy4c08aed2011-07-01 19:47:50 +00001593 {
1594 if ((flags & PsiValue) == 0)
1595 return(0.0);
1596 return(channel_info.psi);
1597 }
cristy3ed852e2009-09-05 21:47:34 +00001598 default:
1599 return(0.0);
1600 }
1601 return(0.0);
1602 }
1603 if (LocaleCompare(symbol,"c") == 0)
1604 return(QuantumScale*pixel.red);
1605 break;
1606 }
1607 case 'D':
1608 case 'd':
1609 {
1610 if (LocaleNCompare(symbol,"depth",5) == 0)
1611 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1612 break;
1613 }
1614 case 'G':
1615 case 'g':
1616 {
1617 if (LocaleCompare(symbol,"g") == 0)
1618 return(QuantumScale*pixel.green);
1619 break;
1620 }
1621 case 'K':
1622 case 'k':
1623 {
1624 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1625 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1626 if (LocaleCompare(symbol,"k") == 0)
1627 {
1628 if (image->colorspace != CMYKColorspace)
1629 {
1630 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00001631 OptionError,"ColorSeparatedImageRequired","'%s'",
cristy3ed852e2009-09-05 21:47:34 +00001632 image->filename);
1633 return(0.0);
1634 }
cristy4c08aed2011-07-01 19:47:50 +00001635 return(QuantumScale*pixel.black);
cristy3ed852e2009-09-05 21:47:34 +00001636 }
1637 break;
1638 }
1639 case 'H':
1640 case 'h':
1641 {
1642 if (LocaleCompare(symbol,"h") == 0)
1643 return((MagickRealType) image->rows);
1644 if (LocaleCompare(symbol,"hue") == 0)
1645 {
1646 double
1647 hue,
1648 lightness,
1649 saturation;
1650
cristyda1f9c12011-10-02 21:39:49 +00001651 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1652 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001653 return(hue);
1654 }
1655 break;
1656 }
1657 case 'I':
1658 case 'i':
1659 {
1660 if ((LocaleCompare(symbol,"image.depth") == 0) ||
1661 (LocaleCompare(symbol,"image.minima") == 0) ||
1662 (LocaleCompare(symbol,"image.maxima") == 0) ||
1663 (LocaleCompare(symbol,"image.mean") == 0) ||
1664 (LocaleCompare(symbol,"image.kurtosis") == 0) ||
1665 (LocaleCompare(symbol,"image.skewness") == 0) ||
1666 (LocaleCompare(symbol,"image.standard_deviation") == 0))
1667 return(FxChannelStatistics(fx_info,image,channel,symbol+6,exception));
1668 if (LocaleCompare(symbol,"image.resolution.x") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001669 return(image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00001670 if (LocaleCompare(symbol,"image.resolution.y") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001671 return(image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001672 if (LocaleCompare(symbol,"intensity") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001673 return(QuantumScale*GetPixelInfoIntensity(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001674 if (LocaleCompare(symbol,"i") == 0)
1675 return((MagickRealType) x);
1676 break;
1677 }
1678 case 'J':
1679 case 'j':
1680 {
1681 if (LocaleCompare(symbol,"j") == 0)
1682 return((MagickRealType) y);
1683 break;
1684 }
1685 case 'L':
1686 case 'l':
1687 {
1688 if (LocaleCompare(symbol,"lightness") == 0)
1689 {
1690 double
1691 hue,
1692 lightness,
1693 saturation;
1694
cristyda1f9c12011-10-02 21:39:49 +00001695 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1696 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001697 return(lightness);
1698 }
1699 if (LocaleCompare(symbol,"luminance") == 0)
1700 {
1701 double
1702 luminence;
1703
1704 luminence=0.2126*pixel.red+0.7152*pixel.green+0.0722*pixel.blue;
1705 return(QuantumScale*luminence);
1706 }
1707 break;
1708 }
1709 case 'M':
1710 case 'm':
1711 {
1712 if (LocaleNCompare(symbol,"maxima",6) == 0)
1713 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1714 if (LocaleNCompare(symbol,"mean",4) == 0)
1715 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1716 if (LocaleNCompare(symbol,"minima",6) == 0)
1717 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1718 if (LocaleCompare(symbol,"m") == 0)
1719 return(QuantumScale*pixel.blue);
1720 break;
1721 }
1722 case 'N':
1723 case 'n':
1724 {
1725 if (LocaleCompare(symbol,"n") == 0)
anthony374f5dd2011-03-25 10:08:53 +00001726 return((MagickRealType) GetImageListLength(fx_info->images));
cristy3ed852e2009-09-05 21:47:34 +00001727 break;
1728 }
1729 case 'O':
1730 case 'o':
1731 {
1732 if (LocaleCompare(symbol,"o") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001733 return(QuantumScale*pixel.alpha);
cristy3ed852e2009-09-05 21:47:34 +00001734 break;
1735 }
1736 case 'P':
1737 case 'p':
1738 {
1739 if (LocaleCompare(symbol,"page.height") == 0)
1740 return((MagickRealType) image->page.height);
1741 if (LocaleCompare(symbol,"page.width") == 0)
1742 return((MagickRealType) image->page.width);
1743 if (LocaleCompare(symbol,"page.x") == 0)
1744 return((MagickRealType) image->page.x);
1745 if (LocaleCompare(symbol,"page.y") == 0)
1746 return((MagickRealType) image->page.y);
1747 break;
1748 }
1749 case 'R':
1750 case 'r':
1751 {
1752 if (LocaleCompare(symbol,"resolution.x") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001753 return(image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00001754 if (LocaleCompare(symbol,"resolution.y") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001755 return(image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001756 if (LocaleCompare(symbol,"r") == 0)
1757 return(QuantumScale*pixel.red);
1758 break;
1759 }
1760 case 'S':
1761 case 's':
1762 {
1763 if (LocaleCompare(symbol,"saturation") == 0)
1764 {
1765 double
1766 hue,
1767 lightness,
1768 saturation;
1769
cristyda1f9c12011-10-02 21:39:49 +00001770 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1771 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001772 return(saturation);
1773 }
1774 if (LocaleNCompare(symbol,"skewness",8) == 0)
1775 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1776 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1777 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1778 break;
1779 }
1780 case 'T':
1781 case 't':
1782 {
1783 if (LocaleCompare(symbol,"t") == 0)
cristy5a15b932011-03-26 12:50:33 +00001784 return((MagickRealType) GetImageIndexInList(fx_info->images));
cristy3ed852e2009-09-05 21:47:34 +00001785 break;
1786 }
1787 case 'W':
1788 case 'w':
1789 {
1790 if (LocaleCompare(symbol,"w") == 0)
1791 return((MagickRealType) image->columns);
1792 break;
1793 }
1794 case 'Y':
1795 case 'y':
1796 {
1797 if (LocaleCompare(symbol,"y") == 0)
1798 return(QuantumScale*pixel.green);
1799 break;
1800 }
1801 case 'Z':
1802 case 'z':
1803 {
1804 if (LocaleCompare(symbol,"z") == 0)
1805 {
1806 MagickRealType
1807 depth;
1808
cristyfefab1b2011-07-05 00:33:22 +00001809 depth=(MagickRealType) GetImageDepth(image,fx_info->exception);
cristy3ed852e2009-09-05 21:47:34 +00001810 return(depth);
1811 }
1812 break;
1813 }
1814 default:
1815 break;
1816 }
1817 value=(const char *) GetValueFromSplayTree(fx_info->symbols,symbol);
1818 if (value != (const char *) NULL)
cristydbdd0e32011-11-04 23:29:40 +00001819 return((MagickRealType) StringToDouble(value,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001820 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
anthonye5b39652012-04-21 05:37:29 +00001821 "UnableToParseExpression","'%s'",symbol);
cristy3ed852e2009-09-05 21:47:34 +00001822 return(0.0);
1823}
1824
1825static const char *FxOperatorPrecedence(const char *expression,
1826 ExceptionInfo *exception)
1827{
1828 typedef enum
1829 {
1830 UndefinedPrecedence,
1831 NullPrecedence,
1832 BitwiseComplementPrecedence,
1833 ExponentPrecedence,
cristy116af162010-08-13 01:25:47 +00001834 ExponentialNotationPrecedence,
cristy3ed852e2009-09-05 21:47:34 +00001835 MultiplyPrecedence,
1836 AdditionPrecedence,
1837 ShiftPrecedence,
1838 RelationalPrecedence,
1839 EquivalencyPrecedence,
1840 BitwiseAndPrecedence,
1841 BitwiseOrPrecedence,
1842 LogicalAndPrecedence,
1843 LogicalOrPrecedence,
1844 TernaryPrecedence,
1845 AssignmentPrecedence,
1846 CommaPrecedence,
1847 SeparatorPrecedence
1848 } FxPrecedence;
1849
1850 FxPrecedence
1851 precedence,
1852 target;
1853
1854 register const char
1855 *subexpression;
1856
1857 register int
1858 c;
1859
cristybb503372010-05-27 20:51:26 +00001860 size_t
cristy3ed852e2009-09-05 21:47:34 +00001861 level;
1862
1863 c=0;
1864 level=0;
1865 subexpression=(const char *) NULL;
1866 target=NullPrecedence;
1867 while (*expression != '\0')
1868 {
1869 precedence=UndefinedPrecedence;
1870 if ((isspace((int) ((char) *expression)) != 0) || (c == (int) '@'))
1871 {
1872 expression++;
1873 continue;
1874 }
cristy488fa882010-03-01 22:34:24 +00001875 switch (*expression)
1876 {
1877 case 'A':
1878 case 'a':
cristy3ed852e2009-09-05 21:47:34 +00001879 {
cristyb33454f2011-08-03 02:10:45 +00001880#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00001881 if (LocaleNCompare(expression,"acosh",5) == 0)
1882 {
1883 expression+=5;
1884 break;
1885 }
cristyb33454f2011-08-03 02:10:45 +00001886#endif
1887#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00001888 if (LocaleNCompare(expression,"asinh",5) == 0)
1889 {
1890 expression+=5;
1891 break;
1892 }
cristyb33454f2011-08-03 02:10:45 +00001893#endif
1894#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00001895 if (LocaleNCompare(expression,"atanh",5) == 0)
cristy488fa882010-03-01 22:34:24 +00001896 {
1897 expression+=5;
1898 break;
1899 }
cristyb33454f2011-08-03 02:10:45 +00001900#endif
cristy488fa882010-03-01 22:34:24 +00001901 break;
cristy3ed852e2009-09-05 21:47:34 +00001902 }
cristy62d455f2011-11-03 11:42:28 +00001903 case 'E':
1904 case 'e':
1905 {
1906 if ((LocaleNCompare(expression,"E+",2) == 0) ||
1907 (LocaleNCompare(expression,"E-",2) == 0))
1908 {
1909 expression+=2; /* scientific notation */
1910 break;
1911 }
1912 }
cristy488fa882010-03-01 22:34:24 +00001913 case 'J':
1914 case 'j':
1915 {
1916 if ((LocaleNCompare(expression,"j0",2) == 0) ||
1917 (LocaleNCompare(expression,"j1",2) == 0))
1918 {
1919 expression+=2;
1920 break;
1921 }
1922 break;
1923 }
cristy2def9322010-06-18 23:59:37 +00001924 case '#':
1925 {
1926 while (isxdigit((int) ((unsigned char) *(expression+1))) != 0)
1927 expression++;
1928 break;
1929 }
cristy488fa882010-03-01 22:34:24 +00001930 default:
1931 break;
1932 }
cristy3ed852e2009-09-05 21:47:34 +00001933 if ((c == (int) '{') || (c == (int) '['))
1934 level++;
1935 else
1936 if ((c == (int) '}') || (c == (int) ']'))
1937 level--;
1938 if (level == 0)
1939 switch ((unsigned char) *expression)
1940 {
1941 case '~':
1942 case '!':
1943 {
1944 precedence=BitwiseComplementPrecedence;
1945 break;
1946 }
1947 case '^':
cristy6621e252010-08-13 00:42:57 +00001948 case '@':
cristy3ed852e2009-09-05 21:47:34 +00001949 {
1950 precedence=ExponentPrecedence;
1951 break;
1952 }
1953 default:
1954 {
1955 if (((c != 0) && ((isdigit((int) ((char) c)) != 0) ||
1956 (strchr(")",c) != (char *) NULL))) &&
1957 (((islower((int) ((char) *expression)) != 0) ||
1958 (strchr("(",(int) *expression) != (char *) NULL)) ||
1959 ((isdigit((int) ((char) c)) == 0) &&
1960 (isdigit((int) ((char) *expression)) != 0))) &&
1961 (strchr("xy",(int) *expression) == (char *) NULL))
1962 precedence=MultiplyPrecedence;
1963 break;
1964 }
1965 case '*':
1966 case '/':
1967 case '%':
1968 {
1969 precedence=MultiplyPrecedence;
1970 break;
1971 }
1972 case '+':
1973 case '-':
1974 {
1975 if ((strchr("(+-/*%:&^|<>~,",c) == (char *) NULL) ||
1976 (isalpha(c) != 0))
1977 precedence=AdditionPrecedence;
1978 break;
1979 }
1980 case LeftShiftOperator:
1981 case RightShiftOperator:
1982 {
1983 precedence=ShiftPrecedence;
1984 break;
1985 }
1986 case '<':
1987 case LessThanEqualOperator:
1988 case GreaterThanEqualOperator:
1989 case '>':
1990 {
1991 precedence=RelationalPrecedence;
1992 break;
1993 }
1994 case EqualOperator:
1995 case NotEqualOperator:
1996 {
1997 precedence=EquivalencyPrecedence;
1998 break;
1999 }
2000 case '&':
2001 {
2002 precedence=BitwiseAndPrecedence;
2003 break;
2004 }
2005 case '|':
2006 {
2007 precedence=BitwiseOrPrecedence;
2008 break;
2009 }
2010 case LogicalAndOperator:
2011 {
2012 precedence=LogicalAndPrecedence;
2013 break;
2014 }
2015 case LogicalOrOperator:
2016 {
2017 precedence=LogicalOrPrecedence;
2018 break;
2019 }
cristy116af162010-08-13 01:25:47 +00002020 case ExponentialNotation:
2021 {
2022 precedence=ExponentialNotationPrecedence;
2023 break;
2024 }
cristy3ed852e2009-09-05 21:47:34 +00002025 case ':':
2026 case '?':
2027 {
2028 precedence=TernaryPrecedence;
2029 break;
2030 }
2031 case '=':
2032 {
2033 precedence=AssignmentPrecedence;
2034 break;
2035 }
2036 case ',':
2037 {
2038 precedence=CommaPrecedence;
2039 break;
2040 }
2041 case ';':
2042 {
2043 precedence=SeparatorPrecedence;
2044 break;
2045 }
2046 }
2047 if ((precedence == BitwiseComplementPrecedence) ||
2048 (precedence == TernaryPrecedence) ||
2049 (precedence == AssignmentPrecedence))
2050 {
2051 if (precedence > target)
2052 {
2053 /*
2054 Right-to-left associativity.
2055 */
2056 target=precedence;
2057 subexpression=expression;
2058 }
2059 }
2060 else
2061 if (precedence >= target)
2062 {
2063 /*
2064 Left-to-right associativity.
2065 */
2066 target=precedence;
2067 subexpression=expression;
2068 }
2069 if (strchr("(",(int) *expression) != (char *) NULL)
2070 expression=FxSubexpression(expression,exception);
2071 c=(int) (*expression++);
2072 }
2073 return(subexpression);
2074}
2075
2076static MagickRealType FxEvaluateSubexpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002077 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristye85007d2010-06-06 22:51:36 +00002078 const char *expression,MagickRealType *beta,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002079{
2080 char
2081 *q,
2082 subexpression[MaxTextExtent];
2083
2084 MagickRealType
2085 alpha,
2086 gamma;
2087
2088 register const char
2089 *p;
2090
2091 *beta=0.0;
2092 if (exception->severity != UndefinedException)
2093 return(0.0);
2094 while (isspace((int) *expression) != 0)
2095 expression++;
2096 if (*expression == '\0')
2097 {
2098 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
anthonye5b39652012-04-21 05:37:29 +00002099 "MissingExpression","'%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00002100 return(0.0);
2101 }
cristy66322f02010-05-17 11:40:48 +00002102 *subexpression='\0';
cristy3ed852e2009-09-05 21:47:34 +00002103 p=FxOperatorPrecedence(expression,exception);
2104 if (p != (const char *) NULL)
2105 {
2106 (void) CopyMagickString(subexpression,expression,(size_t)
2107 (p-expression+1));
2108 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2109 exception);
2110 switch ((unsigned char) *p)
2111 {
2112 case '~':
2113 {
2114 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristybb503372010-05-27 20:51:26 +00002115 *beta=(MagickRealType) (~(size_t) *beta);
cristy3ed852e2009-09-05 21:47:34 +00002116 return(*beta);
2117 }
2118 case '!':
2119 {
2120 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2121 return(*beta == 0.0 ? 1.0 : 0.0);
2122 }
2123 case '^':
2124 {
2125 *beta=pow((double) alpha,(double) FxEvaluateSubexpression(fx_info,
2126 channel,x,y,++p,beta,exception));
2127 return(*beta);
2128 }
2129 case '*':
cristy116af162010-08-13 01:25:47 +00002130 case ExponentialNotation:
cristy3ed852e2009-09-05 21:47:34 +00002131 {
2132 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2133 return(alpha*(*beta));
2134 }
2135 case '/':
2136 {
2137 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2138 if (*beta == 0.0)
2139 {
2140 if (exception->severity == UndefinedException)
2141 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00002142 OptionError,"DivideByZero","'%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00002143 return(0.0);
2144 }
2145 return(alpha/(*beta));
2146 }
2147 case '%':
2148 {
2149 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2150 *beta=fabs(floor(((double) *beta)+0.5));
2151 if (*beta == 0.0)
2152 {
2153 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00002154 OptionError,"DivideByZero","'%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00002155 return(0.0);
2156 }
2157 return(fmod((double) alpha,(double) *beta));
2158 }
2159 case '+':
2160 {
2161 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2162 return(alpha+(*beta));
2163 }
2164 case '-':
2165 {
2166 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2167 return(alpha-(*beta));
2168 }
2169 case LeftShiftOperator:
2170 {
2171 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy1707c6c2012-01-18 23:30:54 +00002172 *beta=(MagickRealType) ((size_t) (alpha+0.5) << (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002173 return(*beta);
2174 }
2175 case RightShiftOperator:
2176 {
2177 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy1707c6c2012-01-18 23:30:54 +00002178 *beta=(MagickRealType) ((size_t) (alpha+0.5) >> (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002179 return(*beta);
2180 }
2181 case '<':
2182 {
2183 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2184 return(alpha < *beta ? 1.0 : 0.0);
2185 }
2186 case LessThanEqualOperator:
2187 {
2188 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2189 return(alpha <= *beta ? 1.0 : 0.0);
2190 }
2191 case '>':
2192 {
2193 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2194 return(alpha > *beta ? 1.0 : 0.0);
2195 }
2196 case GreaterThanEqualOperator:
2197 {
2198 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2199 return(alpha >= *beta ? 1.0 : 0.0);
2200 }
2201 case EqualOperator:
2202 {
2203 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2204 return(fabs(alpha-(*beta)) <= MagickEpsilon ? 1.0 : 0.0);
2205 }
2206 case NotEqualOperator:
2207 {
2208 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2209 return(fabs(alpha-(*beta)) > MagickEpsilon ? 1.0 : 0.0);
2210 }
2211 case '&':
2212 {
2213 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy1707c6c2012-01-18 23:30:54 +00002214 *beta=(MagickRealType) ((size_t) (alpha+0.5) & (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002215 return(*beta);
2216 }
2217 case '|':
2218 {
2219 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy1707c6c2012-01-18 23:30:54 +00002220 *beta=(MagickRealType) ((size_t) (alpha+0.5) | (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002221 return(*beta);
2222 }
2223 case LogicalAndOperator:
2224 {
2225 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2226 *beta=(alpha > 0.0) && (gamma > 0.0) ? 1.0 : 0.0;
2227 return(*beta);
2228 }
2229 case LogicalOrOperator:
2230 {
2231 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2232 *beta=(alpha > 0.0) || (gamma > 0.0) ? 1.0 : 0.0;
2233 return(*beta);
2234 }
2235 case '?':
2236 {
2237 MagickRealType
2238 gamma;
2239
2240 (void) CopyMagickString(subexpression,++p,MaxTextExtent);
2241 q=subexpression;
2242 p=StringToken(":",&q);
2243 if (q == (char *) NULL)
2244 {
2245 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00002246 OptionError,"UnableToParseExpression","'%s'",subexpression);
cristy3ed852e2009-09-05 21:47:34 +00002247 return(0.0);
2248 }
2249 if (fabs((double) alpha) > MagickEpsilon)
2250 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,exception);
2251 else
2252 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,q,beta,exception);
2253 return(gamma);
2254 }
2255 case '=':
2256 {
2257 char
2258 numeric[MaxTextExtent];
2259
2260 q=subexpression;
2261 while (isalpha((int) ((unsigned char) *q)) != 0)
2262 q++;
2263 if (*q != '\0')
2264 {
2265 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00002266 OptionError,"UnableToParseExpression","'%s'",subexpression);
cristy3ed852e2009-09-05 21:47:34 +00002267 return(0.0);
2268 }
2269 ClearMagickException(exception);
2270 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristyb51dff52011-05-19 16:55:47 +00002271 (void) FormatLocaleString(numeric,MaxTextExtent,"%g",(double)
cristy8cd5b312010-01-07 01:10:24 +00002272 *beta);
cristy3ed852e2009-09-05 21:47:34 +00002273 (void) DeleteNodeFromSplayTree(fx_info->symbols,subexpression);
2274 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(
2275 subexpression),ConstantString(numeric));
2276 return(*beta);
2277 }
2278 case ',':
2279 {
2280 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2281 return(alpha);
2282 }
2283 case ';':
2284 {
2285 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2286 return(*beta);
2287 }
2288 default:
2289 {
2290 gamma=alpha*FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,
2291 exception);
2292 return(gamma);
2293 }
2294 }
2295 }
2296 if (strchr("(",(int) *expression) != (char *) NULL)
2297 {
2298 (void) CopyMagickString(subexpression,expression+1,MaxTextExtent);
2299 subexpression[strlen(subexpression)-1]='\0';
2300 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2301 exception);
2302 return(gamma);
2303 }
cristy8b8a3ae2010-10-23 18:49:46 +00002304 switch (*expression)
cristy3ed852e2009-09-05 21:47:34 +00002305 {
2306 case '+':
2307 {
2308 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2309 exception);
2310 return(1.0*gamma);
2311 }
2312 case '-':
2313 {
2314 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2315 exception);
2316 return(-1.0*gamma);
2317 }
2318 case '~':
2319 {
2320 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2321 exception);
cristybb503372010-05-27 20:51:26 +00002322 return((MagickRealType) (~(size_t) (gamma+0.5)));
cristy3ed852e2009-09-05 21:47:34 +00002323 }
2324 case 'A':
2325 case 'a':
2326 {
2327 if (LocaleNCompare(expression,"abs",3) == 0)
2328 {
2329 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2330 exception);
2331 return((MagickRealType) fabs((double) alpha));
2332 }
cristyb33454f2011-08-03 02:10:45 +00002333#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00002334 if (LocaleNCompare(expression,"acosh",5) == 0)
2335 {
2336 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2337 exception);
2338 return((MagickRealType) acosh((double) alpha));
2339 }
cristyb33454f2011-08-03 02:10:45 +00002340#endif
cristy3ed852e2009-09-05 21:47:34 +00002341 if (LocaleNCompare(expression,"acos",4) == 0)
2342 {
2343 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2344 exception);
2345 return((MagickRealType) acos((double) alpha));
2346 }
cristy43c22f42010-03-30 12:34:07 +00002347#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002348 if (LocaleNCompare(expression,"airy",4) == 0)
2349 {
2350 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2351 exception);
2352 if (alpha == 0.0)
cristy2dd03222010-03-30 22:12:11 +00002353 return(1.0);
2354 gamma=2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha);
cristy43c22f42010-03-30 12:34:07 +00002355 return(gamma*gamma);
cristyee56cf12010-03-01 22:17:06 +00002356 }
cristy43c22f42010-03-30 12:34:07 +00002357#endif
cristyb33454f2011-08-03 02:10:45 +00002358#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00002359 if (LocaleNCompare(expression,"asinh",5) == 0)
2360 {
2361 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2362 exception);
2363 return((MagickRealType) asinh((double) alpha));
2364 }
cristyb33454f2011-08-03 02:10:45 +00002365#endif
cristy3ed852e2009-09-05 21:47:34 +00002366 if (LocaleNCompare(expression,"asin",4) == 0)
2367 {
2368 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2369 exception);
2370 return((MagickRealType) asin((double) alpha));
2371 }
2372 if (LocaleNCompare(expression,"alt",3) == 0)
2373 {
2374 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2375 exception);
cristybb503372010-05-27 20:51:26 +00002376 return(((ssize_t) alpha) & 0x01 ? -1.0 : 1.0);
cristy3ed852e2009-09-05 21:47:34 +00002377 }
2378 if (LocaleNCompare(expression,"atan2",5) == 0)
2379 {
2380 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2381 exception);
2382 return((MagickRealType) atan2((double) alpha,(double) *beta));
2383 }
cristyb33454f2011-08-03 02:10:45 +00002384#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00002385 if (LocaleNCompare(expression,"atanh",5) == 0)
2386 {
2387 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2388 exception);
2389 return((MagickRealType) atanh((double) alpha));
2390 }
cristyb33454f2011-08-03 02:10:45 +00002391#endif
cristy3ed852e2009-09-05 21:47:34 +00002392 if (LocaleNCompare(expression,"atan",4) == 0)
2393 {
2394 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2395 exception);
2396 return((MagickRealType) atan((double) alpha));
2397 }
2398 if (LocaleCompare(expression,"a") == 0)
2399 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2400 break;
2401 }
2402 case 'B':
2403 case 'b':
2404 {
2405 if (LocaleCompare(expression,"b") == 0)
2406 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2407 break;
2408 }
2409 case 'C':
2410 case 'c':
2411 {
2412 if (LocaleNCompare(expression,"ceil",4) == 0)
2413 {
2414 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2415 exception);
2416 return((MagickRealType) ceil((double) alpha));
2417 }
2418 if (LocaleNCompare(expression,"cosh",4) == 0)
2419 {
2420 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2421 exception);
2422 return((MagickRealType) cosh((double) alpha));
2423 }
2424 if (LocaleNCompare(expression,"cos",3) == 0)
2425 {
2426 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2427 exception);
2428 return((MagickRealType) cos((double) alpha));
2429 }
2430 if (LocaleCompare(expression,"c") == 0)
2431 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2432 break;
2433 }
2434 case 'D':
2435 case 'd':
2436 {
2437 if (LocaleNCompare(expression,"debug",5) == 0)
2438 {
2439 const char
2440 *type;
2441
2442 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2443 exception);
2444 if (fx_info->images->colorspace == CMYKColorspace)
2445 switch (channel)
2446 {
cristy0568ffc2011-07-25 16:54:14 +00002447 case CyanPixelChannel: type="cyan"; break;
2448 case MagentaPixelChannel: type="magenta"; break;
2449 case YellowPixelChannel: type="yellow"; break;
2450 case AlphaPixelChannel: type="opacity"; break;
2451 case BlackPixelChannel: type="black"; break;
cristy3ed852e2009-09-05 21:47:34 +00002452 default: type="unknown"; break;
2453 }
2454 else
2455 switch (channel)
2456 {
cristy0568ffc2011-07-25 16:54:14 +00002457 case RedPixelChannel: type="red"; break;
2458 case GreenPixelChannel: type="green"; break;
2459 case BluePixelChannel: type="blue"; break;
2460 case AlphaPixelChannel: type="opacity"; break;
cristy3ed852e2009-09-05 21:47:34 +00002461 default: type="unknown"; break;
2462 }
2463 (void) CopyMagickString(subexpression,expression+6,MaxTextExtent);
2464 if (strlen(subexpression) > 1)
2465 subexpression[strlen(subexpression)-1]='\0';
2466 if (fx_info->file != (FILE *) NULL)
cristy1707c6c2012-01-18 23:30:54 +00002467 (void) FormatLocaleFile(fx_info->file,"%s[%.20g,%.20g].%s: "
2468 "%s=%.*g\n",fx_info->images->filename,(double) x,(double) y,type,
2469 subexpression,GetMagickPrecision(),(double) alpha);
cristy3ed852e2009-09-05 21:47:34 +00002470 return(0.0);
2471 }
cristy5597a8d2011-11-04 00:25:32 +00002472 if (LocaleNCompare(expression,"drc",3) == 0)
2473 {
2474 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2475 exception);
2476 return((MagickRealType) (alpha/(*beta*(alpha-1.0)+1.0)));
2477 }
cristy3ed852e2009-09-05 21:47:34 +00002478 break;
2479 }
2480 case 'E':
2481 case 'e':
2482 {
2483 if (LocaleCompare(expression,"epsilon") == 0)
2484 return((MagickRealType) MagickEpsilon);
2485 if (LocaleNCompare(expression,"exp",3) == 0)
2486 {
2487 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2488 exception);
2489 return((MagickRealType) exp((double) alpha));
2490 }
2491 if (LocaleCompare(expression,"e") == 0)
2492 return((MagickRealType) 2.7182818284590452354);
2493 break;
2494 }
2495 case 'F':
2496 case 'f':
2497 {
2498 if (LocaleNCompare(expression,"floor",5) == 0)
2499 {
2500 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2501 exception);
2502 return((MagickRealType) floor((double) alpha));
2503 }
2504 break;
2505 }
2506 case 'G':
2507 case 'g':
2508 {
cristy9eeedea2011-11-02 19:04:05 +00002509 if (LocaleNCompare(expression,"gauss",5) == 0)
2510 {
2511 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2512 exception);
2513 gamma=exp((double) (-alpha*alpha/2.0))/sqrt(2.0*MagickPI);
2514 return((MagickRealType) gamma);
2515 }
cristyb0aad4c2011-11-02 19:30:35 +00002516 if (LocaleNCompare(expression,"gcd",3) == 0)
2517 {
2518 MagickOffsetType
2519 gcd;
2520
2521 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2522 exception);
cristy1707c6c2012-01-18 23:30:54 +00002523 gcd=FxGCD((MagickOffsetType) (alpha+0.5),(MagickOffsetType) (*beta+
2524 0.5));
cristyb0aad4c2011-11-02 19:30:35 +00002525 return((MagickRealType) gcd);
2526 }
cristy3ed852e2009-09-05 21:47:34 +00002527 if (LocaleCompare(expression,"g") == 0)
2528 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2529 break;
2530 }
2531 case 'H':
2532 case 'h':
2533 {
2534 if (LocaleCompare(expression,"h") == 0)
2535 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2536 if (LocaleCompare(expression,"hue") == 0)
2537 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2538 if (LocaleNCompare(expression,"hypot",5) == 0)
2539 {
2540 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2541 exception);
2542 return((MagickRealType) hypot((double) alpha,(double) *beta));
2543 }
2544 break;
2545 }
2546 case 'K':
2547 case 'k':
2548 {
2549 if (LocaleCompare(expression,"k") == 0)
2550 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2551 break;
2552 }
2553 case 'I':
2554 case 'i':
2555 {
2556 if (LocaleCompare(expression,"intensity") == 0)
2557 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2558 if (LocaleNCompare(expression,"int",3) == 0)
2559 {
2560 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2561 exception);
cristy16788e42010-08-13 13:44:26 +00002562 return((MagickRealType) floor(alpha));
cristy3ed852e2009-09-05 21:47:34 +00002563 }
cristy82b20722011-11-05 21:52:36 +00002564#if defined(MAGICKCORE_HAVE_ISNAN)
cristy639399c2011-11-02 19:16:15 +00002565 if (LocaleNCompare(expression,"isnan",5) == 0)
2566 {
2567 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2568 exception);
cristy17a10202011-11-02 19:17:04 +00002569 return((MagickRealType) !!isnan((double) alpha));
cristy639399c2011-11-02 19:16:15 +00002570 }
cristy82b20722011-11-05 21:52:36 +00002571#endif
cristy3ed852e2009-09-05 21:47:34 +00002572 if (LocaleCompare(expression,"i") == 0)
2573 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2574 break;
2575 }
2576 case 'J':
2577 case 'j':
2578 {
2579 if (LocaleCompare(expression,"j") == 0)
2580 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
cristy161b9262010-03-20 19:34:32 +00002581#if defined(MAGICKCORE_HAVE_J0)
cristyee56cf12010-03-01 22:17:06 +00002582 if (LocaleNCompare(expression,"j0",2) == 0)
2583 {
2584 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2585 exception);
2586 return((MagickRealType) j0((double) alpha));
2587 }
cristy161b9262010-03-20 19:34:32 +00002588#endif
2589#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002590 if (LocaleNCompare(expression,"j1",2) == 0)
2591 {
2592 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2593 exception);
2594 return((MagickRealType) j1((double) alpha));
2595 }
cristy161b9262010-03-20 19:34:32 +00002596#endif
cristyaa018fa2010-04-08 23:03:54 +00002597#if defined(MAGICKCORE_HAVE_J1)
cristya6a09e72010-03-02 14:51:02 +00002598 if (LocaleNCompare(expression,"jinc",4) == 0)
2599 {
2600 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2601 exception);
cristy0946a822010-03-12 17:14:58 +00002602 if (alpha == 0.0)
2603 return(1.0);
cristy1707c6c2012-01-18 23:30:54 +00002604 gamma=(MagickRealType) (2.0*j1((double) (MagickPI*alpha))/(MagickPI*
2605 alpha));
cristyfce2f7b2010-03-12 00:29:49 +00002606 return(gamma);
cristya6a09e72010-03-02 14:51:02 +00002607 }
cristyaa018fa2010-04-08 23:03:54 +00002608#endif
cristy3ed852e2009-09-05 21:47:34 +00002609 break;
2610 }
2611 case 'L':
2612 case 'l':
2613 {
2614 if (LocaleNCompare(expression,"ln",2) == 0)
2615 {
2616 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2617 exception);
2618 return((MagickRealType) log((double) alpha));
2619 }
cristyc8ed5322010-08-31 12:07:59 +00002620 if (LocaleNCompare(expression,"logtwo",6) == 0)
cristy3ed852e2009-09-05 21:47:34 +00002621 {
cristyc8ed5322010-08-31 12:07:59 +00002622 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
cristy3ed852e2009-09-05 21:47:34 +00002623 exception);
2624 return((MagickRealType) log10((double) alpha))/log10(2.0);
2625 }
2626 if (LocaleNCompare(expression,"log",3) == 0)
2627 {
2628 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2629 exception);
2630 return((MagickRealType) log10((double) alpha));
2631 }
2632 if (LocaleCompare(expression,"lightness") == 0)
2633 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2634 break;
2635 }
2636 case 'M':
2637 case 'm':
2638 {
2639 if (LocaleCompare(expression,"MaxRGB") == 0)
2640 return((MagickRealType) QuantumRange);
2641 if (LocaleNCompare(expression,"maxima",6) == 0)
2642 break;
2643 if (LocaleNCompare(expression,"max",3) == 0)
cristy984049c2011-11-03 18:34:58 +00002644 {
2645 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2646 exception);
2647 return(alpha > *beta ? alpha : *beta);
2648 }
cristy3ed852e2009-09-05 21:47:34 +00002649 if (LocaleNCompare(expression,"minima",6) == 0)
2650 break;
2651 if (LocaleNCompare(expression,"min",3) == 0)
cristy984049c2011-11-03 18:34:58 +00002652 {
2653 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2654 exception);
2655 return(alpha < *beta ? alpha : *beta);
2656 }
cristy3ed852e2009-09-05 21:47:34 +00002657 if (LocaleNCompare(expression,"mod",3) == 0)
2658 {
2659 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2660 exception);
cristy984049c2011-11-03 18:34:58 +00002661 gamma=alpha-floor((double) (alpha/(*beta)))*(*beta);
2662 return(gamma);
cristy3ed852e2009-09-05 21:47:34 +00002663 }
2664 if (LocaleCompare(expression,"m") == 0)
2665 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2666 break;
2667 }
2668 case 'N':
2669 case 'n':
2670 {
cristyad3502e2011-11-02 19:10:45 +00002671 if (LocaleNCompare(expression,"not",3) == 0)
2672 {
2673 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2674 exception);
2675 return((MagickRealType) (alpha < MagickEpsilon));
2676 }
cristy3ed852e2009-09-05 21:47:34 +00002677 if (LocaleCompare(expression,"n") == 0)
2678 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2679 break;
2680 }
2681 case 'O':
2682 case 'o':
2683 {
2684 if (LocaleCompare(expression,"Opaque") == 0)
2685 return(1.0);
2686 if (LocaleCompare(expression,"o") == 0)
2687 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2688 break;
2689 }
2690 case 'P':
2691 case 'p':
2692 {
cristy670aa3c2011-11-03 00:54:00 +00002693 if (LocaleCompare(expression,"phi") == 0)
2694 return((MagickRealType) MagickPHI);
cristy3ed852e2009-09-05 21:47:34 +00002695 if (LocaleCompare(expression,"pi") == 0)
2696 return((MagickRealType) MagickPI);
2697 if (LocaleNCompare(expression,"pow",3) == 0)
2698 {
2699 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2700 exception);
2701 return((MagickRealType) pow((double) alpha,(double) *beta));
2702 }
2703 if (LocaleCompare(expression,"p") == 0)
2704 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2705 break;
2706 }
2707 case 'Q':
2708 case 'q':
2709 {
2710 if (LocaleCompare(expression,"QuantumRange") == 0)
2711 return((MagickRealType) QuantumRange);
2712 if (LocaleCompare(expression,"QuantumScale") == 0)
2713 return((MagickRealType) QuantumScale);
2714 break;
2715 }
2716 case 'R':
2717 case 'r':
2718 {
2719 if (LocaleNCompare(expression,"rand",4) == 0)
2720 return((MagickRealType) GetPseudoRandomValue(fx_info->random_info));
2721 if (LocaleNCompare(expression,"round",5) == 0)
2722 {
2723 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2724 exception);
cristy16788e42010-08-13 13:44:26 +00002725 return((MagickRealType) floor((double) alpha+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002726 }
2727 if (LocaleCompare(expression,"r") == 0)
2728 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2729 break;
2730 }
2731 case 'S':
2732 case 's':
2733 {
2734 if (LocaleCompare(expression,"saturation") == 0)
2735 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2736 if (LocaleNCompare(expression,"sign",4) == 0)
2737 {
2738 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2739 exception);
2740 return(alpha < 0.0 ? -1.0 : 1.0);
2741 }
cristya6a09e72010-03-02 14:51:02 +00002742 if (LocaleNCompare(expression,"sinc",4) == 0)
2743 {
2744 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2745 exception);
2746 if (alpha == 0)
2747 return(1.0);
2748 gamma=(MagickRealType) (sin((double) (MagickPI*alpha))/
2749 (MagickPI*alpha));
2750 return(gamma);
2751 }
cristy3ed852e2009-09-05 21:47:34 +00002752 if (LocaleNCompare(expression,"sinh",4) == 0)
2753 {
2754 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2755 exception);
2756 return((MagickRealType) sinh((double) alpha));
2757 }
2758 if (LocaleNCompare(expression,"sin",3) == 0)
2759 {
2760 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2761 exception);
2762 return((MagickRealType) sin((double) alpha));
2763 }
2764 if (LocaleNCompare(expression,"sqrt",4) == 0)
2765 {
2766 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2767 exception);
2768 return((MagickRealType) sqrt((double) alpha));
2769 }
cristy9eeedea2011-11-02 19:04:05 +00002770 if (LocaleNCompare(expression,"squish",6) == 0)
2771 {
2772 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2773 exception);
cristyf7b28ea2012-04-27 11:22:49 +00002774 return((MagickRealType) (1.0/(1.0+exp((double) (-alpha)))));
cristy9eeedea2011-11-02 19:04:05 +00002775 }
cristy3ed852e2009-09-05 21:47:34 +00002776 if (LocaleCompare(expression,"s") == 0)
2777 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2778 break;
2779 }
2780 case 'T':
2781 case 't':
2782 {
2783 if (LocaleNCompare(expression,"tanh",4) == 0)
2784 {
2785 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2786 exception);
2787 return((MagickRealType) tanh((double) alpha));
2788 }
2789 if (LocaleNCompare(expression,"tan",3) == 0)
2790 {
2791 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2792 exception);
2793 return((MagickRealType) tan((double) alpha));
2794 }
2795 if (LocaleCompare(expression,"Transparent") == 0)
2796 return(0.0);
cristy16788e42010-08-13 13:44:26 +00002797 if (LocaleNCompare(expression,"trunc",5) == 0)
2798 {
2799 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2800 exception);
2801 if (alpha >= 0.0)
2802 return((MagickRealType) floor((double) alpha));
2803 return((MagickRealType) ceil((double) alpha));
2804 }
cristy3ed852e2009-09-05 21:47:34 +00002805 if (LocaleCompare(expression,"t") == 0)
2806 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2807 break;
2808 }
2809 case 'U':
2810 case 'u':
2811 {
2812 if (LocaleCompare(expression,"u") == 0)
2813 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2814 break;
2815 }
2816 case 'V':
2817 case 'v':
2818 {
2819 if (LocaleCompare(expression,"v") == 0)
2820 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2821 break;
2822 }
2823 case 'W':
2824 case 'w':
2825 {
cristy9eeedea2011-11-02 19:04:05 +00002826 if (LocaleNCompare(expression,"while",5) == 0)
2827 {
2828 do
2829 {
2830 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2831 exception);
2832 } while (fabs((double) alpha) >= MagickEpsilon);
2833 return((MagickRealType) *beta);
2834 }
cristy3ed852e2009-09-05 21:47:34 +00002835 if (LocaleCompare(expression,"w") == 0)
2836 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2837 break;
2838 }
2839 case 'Y':
2840 case 'y':
2841 {
2842 if (LocaleCompare(expression,"y") == 0)
2843 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2844 break;
2845 }
2846 case 'Z':
2847 case 'z':
2848 {
2849 if (LocaleCompare(expression,"z") == 0)
2850 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2851 break;
2852 }
2853 default:
2854 break;
2855 }
2856 q=(char *) expression;
cristydbdd0e32011-11-04 23:29:40 +00002857 alpha=InterpretSiPrefixValue(expression,&q);
cristy3ed852e2009-09-05 21:47:34 +00002858 if (q == expression)
2859 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2860 return(alpha);
2861}
2862
cristy7832dc22011-09-05 01:21:53 +00002863MagickPrivate MagickBooleanType FxEvaluateExpression(FxInfo *fx_info,
cristy3ed852e2009-09-05 21:47:34 +00002864 MagickRealType *alpha,ExceptionInfo *exception)
2865{
2866 MagickBooleanType
2867 status;
2868
cristy541ae572011-08-05 19:08:59 +00002869 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2870 exception);
cristy3ed852e2009-09-05 21:47:34 +00002871 return(status);
2872}
2873
2874MagickExport MagickBooleanType FxPreprocessExpression(FxInfo *fx_info,
2875 MagickRealType *alpha,ExceptionInfo *exception)
2876{
2877 FILE
2878 *file;
2879
2880 MagickBooleanType
2881 status;
2882
2883 file=fx_info->file;
2884 fx_info->file=(FILE *) NULL;
cristy541ae572011-08-05 19:08:59 +00002885 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2886 exception);
cristy3ed852e2009-09-05 21:47:34 +00002887 fx_info->file=file;
2888 return(status);
2889}
2890
cristy7832dc22011-09-05 01:21:53 +00002891MagickPrivate MagickBooleanType FxEvaluateChannelExpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002892 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristye85007d2010-06-06 22:51:36 +00002893 MagickRealType *alpha,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002894{
2895 MagickRealType
2896 beta;
2897
2898 beta=0.0;
2899 *alpha=FxEvaluateSubexpression(fx_info,channel,x,y,fx_info->expression,&beta,
2900 exception);
2901 return(exception->severity == OptionError ? MagickFalse : MagickTrue);
2902}
2903
2904/*
2905%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2906% %
2907% %
2908% %
2909% F x I m a g e %
2910% %
2911% %
2912% %
2913%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2914%
2915% FxImage() applies a mathematical expression to the specified image.
2916%
2917% The format of the FxImage method is:
2918%
2919% Image *FxImage(const Image *image,const char *expression,
2920% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002921%
2922% A description of each parameter follows:
2923%
2924% o image: the image.
2925%
cristy3ed852e2009-09-05 21:47:34 +00002926% o expression: A mathematical expression.
2927%
2928% o exception: return any errors or warnings in this structure.
2929%
2930*/
2931
2932static FxInfo **DestroyFxThreadSet(FxInfo **fx_info)
2933{
cristybb503372010-05-27 20:51:26 +00002934 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002935 i;
2936
2937 assert(fx_info != (FxInfo **) NULL);
cristybb503372010-05-27 20:51:26 +00002938 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
cristy3ed852e2009-09-05 21:47:34 +00002939 if (fx_info[i] != (FxInfo *) NULL)
2940 fx_info[i]=DestroyFxInfo(fx_info[i]);
cristyb41ee102010-10-04 16:46:15 +00002941 fx_info=(FxInfo **) RelinquishMagickMemory(fx_info);
cristy3ed852e2009-09-05 21:47:34 +00002942 return(fx_info);
2943}
2944
2945static FxInfo **AcquireFxThreadSet(const Image *image,const char *expression,
2946 ExceptionInfo *exception)
2947{
2948 char
2949 *fx_expression;
2950
2951 FxInfo
2952 **fx_info;
2953
2954 MagickRealType
2955 alpha;
2956
cristybb503372010-05-27 20:51:26 +00002957 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002958 i;
2959
cristybb503372010-05-27 20:51:26 +00002960 size_t
cristy3ed852e2009-09-05 21:47:34 +00002961 number_threads;
2962
2963 number_threads=GetOpenMPMaximumThreads();
cristyb41ee102010-10-04 16:46:15 +00002964 fx_info=(FxInfo **) AcquireQuantumMemory(number_threads,sizeof(*fx_info));
cristy3ed852e2009-09-05 21:47:34 +00002965 if (fx_info == (FxInfo **) NULL)
2966 return((FxInfo **) NULL);
2967 (void) ResetMagickMemory(fx_info,0,number_threads*sizeof(*fx_info));
2968 if (*expression != '@')
2969 fx_expression=ConstantString(expression);
2970 else
2971 fx_expression=FileToString(expression+1,~0,exception);
cristybb503372010-05-27 20:51:26 +00002972 for (i=0; i < (ssize_t) number_threads; i++)
cristy3ed852e2009-09-05 21:47:34 +00002973 {
cristydb070952012-04-20 14:33:00 +00002974 fx_info[i]=AcquireFxInfo(image,fx_expression,exception);
cristy3ed852e2009-09-05 21:47:34 +00002975 if (fx_info[i] == (FxInfo *) NULL)
2976 return(DestroyFxThreadSet(fx_info));
2977 (void) FxPreprocessExpression(fx_info[i],&alpha,fx_info[i]->exception);
2978 }
2979 fx_expression=DestroyString(fx_expression);
2980 return(fx_info);
2981}
2982
2983MagickExport Image *FxImage(const Image *image,const char *expression,
2984 ExceptionInfo *exception)
2985{
cristy3ed852e2009-09-05 21:47:34 +00002986#define FxImageTag "Fx/Image"
2987
cristyfa112112010-01-04 17:48:07 +00002988 CacheView
cristy79cedc72011-07-25 00:41:15 +00002989 *fx_view,
2990 *image_view;
cristyfa112112010-01-04 17:48:07 +00002991
cristy3ed852e2009-09-05 21:47:34 +00002992 FxInfo
cristyfa112112010-01-04 17:48:07 +00002993 **restrict fx_info;
cristy3ed852e2009-09-05 21:47:34 +00002994
2995 Image
2996 *fx_image;
2997
cristy3ed852e2009-09-05 21:47:34 +00002998 MagickBooleanType
2999 status;
3000
cristybb503372010-05-27 20:51:26 +00003001 MagickOffsetType
3002 progress;
3003
cristy3ed852e2009-09-05 21:47:34 +00003004 MagickRealType
3005 alpha;
3006
cristybb503372010-05-27 20:51:26 +00003007 ssize_t
3008 y;
3009
cristy3ed852e2009-09-05 21:47:34 +00003010 assert(image != (Image *) NULL);
3011 assert(image->signature == MagickSignature);
3012 if (image->debug != MagickFalse)
3013 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy79cedc72011-07-25 00:41:15 +00003014 fx_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00003015 if (fx_image == (Image *) NULL)
3016 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003017 if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003018 {
cristy3ed852e2009-09-05 21:47:34 +00003019 fx_image=DestroyImage(fx_image);
3020 return((Image *) NULL);
3021 }
3022 fx_info=AcquireFxThreadSet(image,expression,exception);
3023 if (fx_info == (FxInfo **) NULL)
3024 {
3025 fx_image=DestroyImage(fx_image);
3026 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3027 }
3028 status=FxPreprocessExpression(fx_info[0],&alpha,exception);
3029 if (status == MagickFalse)
3030 {
3031 fx_image=DestroyImage(fx_image);
3032 fx_info=DestroyFxThreadSet(fx_info);
3033 return((Image *) NULL);
3034 }
3035 /*
3036 Fx image.
3037 */
3038 status=MagickTrue;
3039 progress=0;
cristydb070952012-04-20 14:33:00 +00003040 image_view=AcquireVirtualCacheView(image,exception);
3041 fx_view=AcquireAuthenticCacheView(fx_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00003042#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003043 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003044#endif
cristybb503372010-05-27 20:51:26 +00003045 for (y=0; y < (ssize_t) fx_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003046 {
cristy5c9e6f22010-09-17 17:31:01 +00003047 const int
3048 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00003049
cristy79cedc72011-07-25 00:41:15 +00003050 register const Quantum
3051 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003052
cristy4c08aed2011-07-01 19:47:50 +00003053 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003054 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003055
cristy79cedc72011-07-25 00:41:15 +00003056 register ssize_t
3057 x;
3058
cristy3ed852e2009-09-05 21:47:34 +00003059 if (status == MagickFalse)
3060 continue;
cristy79cedc72011-07-25 00:41:15 +00003061 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +00003062 q=QueueCacheViewAuthenticPixels(fx_view,0,y,fx_image->columns,1,exception);
cristy79cedc72011-07-25 00:41:15 +00003063 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003064 {
3065 status=MagickFalse;
3066 continue;
3067 }
cristybb503372010-05-27 20:51:26 +00003068 for (x=0; x < (ssize_t) fx_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003069 {
cristy79cedc72011-07-25 00:41:15 +00003070 register ssize_t
3071 i;
3072
cristy177e41c2012-04-15 15:08:25 +00003073 if (GetPixelMask(image,p) != 0)
3074 {
3075 p+=GetPixelChannels(image);
3076 q+=GetPixelChannels(fx_image);
3077 continue;
3078 }
cristy79cedc72011-07-25 00:41:15 +00003079 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3080 {
3081 MagickRealType
3082 alpha;
3083
3084 PixelChannel
3085 channel;
3086
3087 PixelTrait
3088 fx_traits,
3089 traits;
3090
cristye2a912b2011-12-05 20:02:07 +00003091 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003092 traits=GetPixelChannelMapTraits(image,channel);
cristy79cedc72011-07-25 00:41:15 +00003093 fx_traits=GetPixelChannelMapTraits(fx_image,channel);
cristy010d7d12011-08-31 01:02:48 +00003094 if ((traits == UndefinedPixelTrait) ||
3095 (fx_traits == UndefinedPixelTrait))
cristy79cedc72011-07-25 00:41:15 +00003096 continue;
cristy177e41c2012-04-15 15:08:25 +00003097 if ((fx_traits & CopyPixelTrait) != 0)
cristy79cedc72011-07-25 00:41:15 +00003098 {
cristy0beccfa2011-09-25 20:47:53 +00003099 SetPixelChannel(fx_image,channel,p[i],q);
cristy79cedc72011-07-25 00:41:15 +00003100 continue;
3101 }
3102 alpha=0.0;
cristya382aca2011-12-06 18:22:48 +00003103 (void) FxEvaluateChannelExpression(fx_info[id],channel,x,y,&alpha,
3104 exception);
cristyb3a73b52011-07-26 01:34:43 +00003105 q[i]=ClampToQuantum((MagickRealType) QuantumRange*alpha);
cristy79cedc72011-07-25 00:41:15 +00003106 }
3107 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003108 q+=GetPixelChannels(fx_image);
cristy3ed852e2009-09-05 21:47:34 +00003109 }
3110 if (SyncCacheViewAuthenticPixels(fx_view,exception) == MagickFalse)
3111 status=MagickFalse;
3112 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3113 {
3114 MagickBooleanType
3115 proceed;
3116
cristyb5d5f722009-11-04 03:03:49 +00003117#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003118 #pragma omp critical (MagickCore_FxImage)
cristy3ed852e2009-09-05 21:47:34 +00003119#endif
3120 proceed=SetImageProgress(image,FxImageTag,progress++,image->rows);
3121 if (proceed == MagickFalse)
3122 status=MagickFalse;
3123 }
3124 }
cristy3ed852e2009-09-05 21:47:34 +00003125 fx_view=DestroyCacheView(fx_view);
cristy79cedc72011-07-25 00:41:15 +00003126 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003127 fx_info=DestroyFxThreadSet(fx_info);
3128 if (status == MagickFalse)
3129 fx_image=DestroyImage(fx_image);
3130 return(fx_image);
3131}
3132
3133/*
3134%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3135% %
3136% %
3137% %
3138% I m p l o d e I m a g e %
3139% %
3140% %
3141% %
3142%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3143%
3144% ImplodeImage() creates a new image that is a copy of an existing
3145% one with the image pixels "implode" by the specified percentage. It
3146% allocates the memory necessary for the new Image structure and returns a
3147% pointer to the new image.
3148%
3149% The format of the ImplodeImage method is:
3150%
3151% Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003152% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003153%
3154% A description of each parameter follows:
3155%
3156% o implode_image: Method ImplodeImage returns a pointer to the image
3157% after it is implode. A null image is returned if there is a memory
3158% shortage.
3159%
3160% o image: the image.
3161%
3162% o amount: Define the extent of the implosion.
3163%
cristy76f512e2011-09-12 01:26:56 +00003164% o method: the pixel interpolation method.
3165%
cristy3ed852e2009-09-05 21:47:34 +00003166% o exception: return any errors or warnings in this structure.
3167%
3168*/
3169MagickExport Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003170 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003171{
3172#define ImplodeImageTag "Implode/Image"
3173
cristyfa112112010-01-04 17:48:07 +00003174 CacheView
3175 *image_view,
3176 *implode_view;
3177
cristy3ed852e2009-09-05 21:47:34 +00003178 Image
3179 *implode_image;
3180
cristy3ed852e2009-09-05 21:47:34 +00003181 MagickBooleanType
3182 status;
3183
cristybb503372010-05-27 20:51:26 +00003184 MagickOffsetType
3185 progress;
3186
cristy3ed852e2009-09-05 21:47:34 +00003187 MagickRealType
3188 radius;
3189
3190 PointInfo
3191 center,
3192 scale;
3193
cristybb503372010-05-27 20:51:26 +00003194 ssize_t
3195 y;
3196
cristy3ed852e2009-09-05 21:47:34 +00003197 /*
3198 Initialize implode image attributes.
3199 */
3200 assert(image != (Image *) NULL);
3201 assert(image->signature == MagickSignature);
3202 if (image->debug != MagickFalse)
3203 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3204 assert(exception != (ExceptionInfo *) NULL);
3205 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00003206 implode_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3207 exception);
cristy3ed852e2009-09-05 21:47:34 +00003208 if (implode_image == (Image *) NULL)
3209 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003210 if (SetImageStorageClass(implode_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003211 {
cristy3ed852e2009-09-05 21:47:34 +00003212 implode_image=DestroyImage(implode_image);
3213 return((Image *) NULL);
3214 }
cristy4c08aed2011-07-01 19:47:50 +00003215 if (implode_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00003216 implode_image->matte=MagickTrue;
3217 /*
3218 Compute scaling factor.
3219 */
3220 scale.x=1.0;
3221 scale.y=1.0;
3222 center.x=0.5*image->columns;
3223 center.y=0.5*image->rows;
3224 radius=center.x;
3225 if (image->columns > image->rows)
3226 scale.y=(double) image->columns/(double) image->rows;
3227 else
3228 if (image->columns < image->rows)
3229 {
3230 scale.x=(double) image->rows/(double) image->columns;
3231 radius=center.y;
3232 }
3233 /*
3234 Implode image.
3235 */
3236 status=MagickTrue;
3237 progress=0;
cristydb070952012-04-20 14:33:00 +00003238 image_view=AcquireVirtualCacheView(image,exception);
3239 implode_view=AcquireAuthenticCacheView(implode_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00003240#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003241 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003242#endif
cristybb503372010-05-27 20:51:26 +00003243 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003244 {
cristy3ed852e2009-09-05 21:47:34 +00003245 MagickRealType
3246 distance;
3247
3248 PointInfo
3249 delta;
3250
cristy6d188022011-09-12 13:23:33 +00003251 register const Quantum
3252 *restrict p;
3253
cristybb503372010-05-27 20:51:26 +00003254 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003255 x;
3256
cristy4c08aed2011-07-01 19:47:50 +00003257 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003258 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003259
3260 if (status == MagickFalse)
3261 continue;
cristy6d188022011-09-12 13:23:33 +00003262 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +00003263 q=QueueCacheViewAuthenticPixels(implode_view,0,y,implode_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003264 exception);
cristy6d188022011-09-12 13:23:33 +00003265 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003266 {
3267 status=MagickFalse;
3268 continue;
3269 }
cristy3ed852e2009-09-05 21:47:34 +00003270 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00003271 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003272 {
cristy6d188022011-09-12 13:23:33 +00003273 register ssize_t
3274 i;
3275
cristy3ed852e2009-09-05 21:47:34 +00003276 /*
3277 Determine if the pixel is within an ellipse.
3278 */
cristy10a6c612012-01-29 21:41:05 +00003279 if (GetPixelMask(image,p) != 0)
3280 {
3281 p+=GetPixelChannels(image);
3282 q+=GetPixelChannels(implode_image);
3283 continue;
3284 }
cristy3ed852e2009-09-05 21:47:34 +00003285 delta.x=scale.x*(double) (x-center.x);
3286 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00003287 if (distance >= (radius*radius))
cristya6d7a9b2012-01-18 20:04:48 +00003288 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy6d188022011-09-12 13:23:33 +00003289 {
cristya6d7a9b2012-01-18 20:04:48 +00003290 PixelChannel
3291 channel;
3292
cristy1707c6c2012-01-18 23:30:54 +00003293 PixelTrait
3294 implode_traits,
3295 traits;
3296
cristya6d7a9b2012-01-18 20:04:48 +00003297 channel=GetPixelChannelMapChannel(image,i);
cristy1707c6c2012-01-18 23:30:54 +00003298 traits=GetPixelChannelMapTraits(image,channel);
3299 implode_traits=GetPixelChannelMapTraits(implode_image,channel);
3300 if ((traits == UndefinedPixelTrait) ||
3301 (implode_traits == UndefinedPixelTrait))
3302 continue;
cristya6d7a9b2012-01-18 20:04:48 +00003303 SetPixelChannel(implode_image,channel,p[i],q);
cristy6d188022011-09-12 13:23:33 +00003304 }
3305 else
cristy3ed852e2009-09-05 21:47:34 +00003306 {
3307 double
3308 factor;
3309
3310 /*
3311 Implode the pixel.
3312 */
3313 factor=1.0;
3314 if (distance > 0.0)
cristy1707c6c2012-01-18 23:30:54 +00003315 factor=pow(sin((double) (MagickPI*sqrt((double) distance)/radius/
3316 2)),-amount);
cristy76f512e2011-09-12 01:26:56 +00003317 status=InterpolatePixelChannels(image,image_view,implode_image,method,
3318 (double) (factor*delta.x/scale.x+center.x),(double) (factor*delta.y/
3319 scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00003320 }
cristy6d188022011-09-12 13:23:33 +00003321 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003322 q+=GetPixelChannels(implode_image);
cristy3ed852e2009-09-05 21:47:34 +00003323 }
3324 if (SyncCacheViewAuthenticPixels(implode_view,exception) == MagickFalse)
3325 status=MagickFalse;
3326 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3327 {
3328 MagickBooleanType
3329 proceed;
3330
cristyb5d5f722009-11-04 03:03:49 +00003331#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003332 #pragma omp critical (MagickCore_ImplodeImage)
cristy3ed852e2009-09-05 21:47:34 +00003333#endif
3334 proceed=SetImageProgress(image,ImplodeImageTag,progress++,image->rows);
3335 if (proceed == MagickFalse)
3336 status=MagickFalse;
3337 }
3338 }
3339 implode_view=DestroyCacheView(implode_view);
3340 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003341 if (status == MagickFalse)
3342 implode_image=DestroyImage(implode_image);
3343 return(implode_image);
3344}
3345
3346/*
3347%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3348% %
3349% %
3350% %
3351% M o r p h I m a g e s %
3352% %
3353% %
3354% %
3355%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3356%
3357% The MorphImages() method requires a minimum of two images. The first
3358% image is transformed into the second by a number of intervening images
3359% as specified by frames.
3360%
3361% The format of the MorphImage method is:
3362%
cristybb503372010-05-27 20:51:26 +00003363% Image *MorphImages(const Image *image,const size_t number_frames,
cristy3ed852e2009-09-05 21:47:34 +00003364% ExceptionInfo *exception)
3365%
3366% A description of each parameter follows:
3367%
3368% o image: the image.
3369%
3370% o number_frames: Define the number of in-between image to generate.
3371% The more in-between frames, the smoother the morph.
3372%
3373% o exception: return any errors or warnings in this structure.
3374%
3375*/
3376MagickExport Image *MorphImages(const Image *image,
cristybb503372010-05-27 20:51:26 +00003377 const size_t number_frames,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003378{
3379#define MorphImageTag "Morph/Image"
3380
3381 Image
3382 *morph_image,
3383 *morph_images;
3384
cristy9d314ff2011-03-09 01:30:28 +00003385 MagickBooleanType
3386 status;
cristy3ed852e2009-09-05 21:47:34 +00003387
3388 MagickOffsetType
3389 scene;
3390
3391 MagickRealType
3392 alpha,
3393 beta;
3394
3395 register const Image
3396 *next;
3397
cristybb503372010-05-27 20:51:26 +00003398 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003399 i;
3400
cristy9d314ff2011-03-09 01:30:28 +00003401 ssize_t
3402 y;
cristy3ed852e2009-09-05 21:47:34 +00003403
3404 /*
3405 Clone first frame in sequence.
3406 */
3407 assert(image != (Image *) NULL);
3408 assert(image->signature == MagickSignature);
3409 if (image->debug != MagickFalse)
3410 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3411 assert(exception != (ExceptionInfo *) NULL);
3412 assert(exception->signature == MagickSignature);
3413 morph_images=CloneImage(image,0,0,MagickTrue,exception);
3414 if (morph_images == (Image *) NULL)
3415 return((Image *) NULL);
3416 if (GetNextImageInList(image) == (Image *) NULL)
3417 {
3418 /*
3419 Morph single image.
3420 */
cristybb503372010-05-27 20:51:26 +00003421 for (i=1; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003422 {
3423 morph_image=CloneImage(image,0,0,MagickTrue,exception);
3424 if (morph_image == (Image *) NULL)
3425 {
3426 morph_images=DestroyImageList(morph_images);
3427 return((Image *) NULL);
3428 }
3429 AppendImageToList(&morph_images,morph_image);
cristy8b27a6d2010-02-14 03:31:15 +00003430 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003431 {
cristy8b27a6d2010-02-14 03:31:15 +00003432 MagickBooleanType
3433 proceed;
3434
cristybb503372010-05-27 20:51:26 +00003435 proceed=SetImageProgress(image,MorphImageTag,(MagickOffsetType) i,
3436 number_frames);
cristy8b27a6d2010-02-14 03:31:15 +00003437 if (proceed == MagickFalse)
3438 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00003439 }
3440 }
3441 return(GetFirstImageInList(morph_images));
3442 }
3443 /*
3444 Morph image sequence.
3445 */
3446 status=MagickTrue;
3447 scene=0;
3448 next=image;
3449 for ( ; GetNextImageInList(next) != (Image *) NULL; next=GetNextImageInList(next))
3450 {
cristybb503372010-05-27 20:51:26 +00003451 for (i=0; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003452 {
3453 CacheView
3454 *image_view,
3455 *morph_view;
3456
3457 beta=(MagickRealType) (i+1.0)/(MagickRealType) (number_frames+1.0);
3458 alpha=1.0-beta;
cristy15b98cd2010-09-12 19:42:50 +00003459 morph_image=ResizeImage(next,(size_t) (alpha*next->columns+beta*
cristyaa2c16c2012-03-25 22:21:35 +00003460 GetNextImageInList(next)->columns+0.5),(size_t) (alpha*next->rows+beta*
3461 GetNextImageInList(next)->rows+0.5),next->filter,exception);
cristy3ed852e2009-09-05 21:47:34 +00003462 if (morph_image == (Image *) NULL)
3463 {
3464 morph_images=DestroyImageList(morph_images);
3465 return((Image *) NULL);
3466 }
cristy1707c6c2012-01-18 23:30:54 +00003467 status=SetImageStorageClass(morph_image,DirectClass,exception);
3468 if (status == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003469 {
cristy3ed852e2009-09-05 21:47:34 +00003470 morph_image=DestroyImage(morph_image);
3471 return((Image *) NULL);
3472 }
3473 AppendImageToList(&morph_images,morph_image);
3474 morph_images=GetLastImageInList(morph_images);
cristy15b98cd2010-09-12 19:42:50 +00003475 morph_image=ResizeImage(GetNextImageInList(next),morph_images->columns,
cristyaa2c16c2012-03-25 22:21:35 +00003476 morph_images->rows,GetNextImageInList(next)->filter,exception);
cristy3ed852e2009-09-05 21:47:34 +00003477 if (morph_image == (Image *) NULL)
3478 {
3479 morph_images=DestroyImageList(morph_images);
3480 return((Image *) NULL);
3481 }
cristydb070952012-04-20 14:33:00 +00003482 image_view=AcquireVirtualCacheView(morph_image,exception);
3483 morph_view=AcquireAuthenticCacheView(morph_images,exception);
cristyb5d5f722009-11-04 03:03:49 +00003484#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003485 #pragma omp parallel for schedule(static,4) shared(status)
cristy3ed852e2009-09-05 21:47:34 +00003486#endif
cristybb503372010-05-27 20:51:26 +00003487 for (y=0; y < (ssize_t) morph_images->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003488 {
3489 MagickBooleanType
3490 sync;
3491
cristy4c08aed2011-07-01 19:47:50 +00003492 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003493 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003494
cristybb503372010-05-27 20:51:26 +00003495 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003496 x;
3497
cristy4c08aed2011-07-01 19:47:50 +00003498 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003499 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003500
3501 if (status == MagickFalse)
3502 continue;
3503 p=GetCacheViewVirtualPixels(image_view,0,y,morph_image->columns,1,
3504 exception);
3505 q=GetCacheViewAuthenticPixels(morph_view,0,y,morph_images->columns,1,
3506 exception);
cristy4c08aed2011-07-01 19:47:50 +00003507 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003508 {
3509 status=MagickFalse;
3510 continue;
3511 }
cristybb503372010-05-27 20:51:26 +00003512 for (x=0; x < (ssize_t) morph_images->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003513 {
cristy1707c6c2012-01-18 23:30:54 +00003514 register ssize_t
3515 i;
3516
cristy177e41c2012-04-15 15:08:25 +00003517 if (GetPixelMask(image,p) != 0)
3518 {
3519 p+=GetPixelChannels(image);
3520 q+=GetPixelChannels(morph_image);
3521 continue;
3522 }
cristy10a6c612012-01-29 21:41:05 +00003523 for (i=0; i < (ssize_t) GetPixelChannels(morph_image); i++)
cristy1707c6c2012-01-18 23:30:54 +00003524 {
3525 PixelChannel
3526 channel;
3527
3528 PixelTrait
3529 morph_traits,
3530 traits;
3531
3532 channel=GetPixelChannelMapChannel(image,i);
3533 traits=GetPixelChannelMapTraits(image,channel);
3534 morph_traits=GetPixelChannelMapTraits(morph_image,channel);
3535 if ((traits == UndefinedPixelTrait) ||
3536 (morph_traits == UndefinedPixelTrait))
3537 continue;
cristy177e41c2012-04-15 15:08:25 +00003538 if ((morph_traits & CopyPixelTrait) != 0)
cristy1707c6c2012-01-18 23:30:54 +00003539 {
3540 SetPixelChannel(morph_image,channel,p[i],q);
3541 continue;
3542 }
3543 SetPixelChannel(morph_image,channel,ClampToQuantum(alpha*
3544 GetPixelChannel(morph_images,channel,q)+beta*p[i]),q);
3545 }
cristyed231572011-07-14 02:18:59 +00003546 p+=GetPixelChannels(morph_image);
3547 q+=GetPixelChannels(morph_images);
cristy3ed852e2009-09-05 21:47:34 +00003548 }
3549 sync=SyncCacheViewAuthenticPixels(morph_view,exception);
3550 if (sync == MagickFalse)
3551 status=MagickFalse;
3552 }
3553 morph_view=DestroyCacheView(morph_view);
3554 image_view=DestroyCacheView(image_view);
3555 morph_image=DestroyImage(morph_image);
3556 }
cristybb503372010-05-27 20:51:26 +00003557 if (i < (ssize_t) number_frames)
cristy3ed852e2009-09-05 21:47:34 +00003558 break;
3559 /*
3560 Clone last frame in sequence.
3561 */
3562 morph_image=CloneImage(GetNextImageInList(next),0,0,MagickTrue,exception);
3563 if (morph_image == (Image *) NULL)
3564 {
3565 morph_images=DestroyImageList(morph_images);
3566 return((Image *) NULL);
3567 }
3568 AppendImageToList(&morph_images,morph_image);
3569 morph_images=GetLastImageInList(morph_images);
3570 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3571 {
3572 MagickBooleanType
3573 proceed;
3574
cristyb5d5f722009-11-04 03:03:49 +00003575#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003576 #pragma omp critical (MagickCore_MorphImages)
cristy3ed852e2009-09-05 21:47:34 +00003577#endif
3578 proceed=SetImageProgress(image,MorphImageTag,scene,
3579 GetImageListLength(image));
3580 if (proceed == MagickFalse)
3581 status=MagickFalse;
3582 }
3583 scene++;
3584 }
3585 if (GetNextImageInList(next) != (Image *) NULL)
3586 {
3587 morph_images=DestroyImageList(morph_images);
3588 return((Image *) NULL);
3589 }
3590 return(GetFirstImageInList(morph_images));
3591}
3592
3593/*
3594%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3595% %
3596% %
3597% %
3598% P l a s m a I m a g e %
3599% %
3600% %
3601% %
3602%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3603%
3604% PlasmaImage() initializes an image with plasma fractal values. The image
3605% must be initialized with a base color and the random number generator
3606% seeded before this method is called.
3607%
3608% The format of the PlasmaImage method is:
3609%
3610% MagickBooleanType PlasmaImage(Image *image,const SegmentInfo *segment,
cristy5cbc0162011-08-29 00:36:28 +00003611% size_t attenuate,size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003612%
3613% A description of each parameter follows:
3614%
3615% o image: the image.
3616%
3617% o segment: Define the region to apply plasma fractals values.
3618%
glennrp7dae1ca2010-09-16 12:17:35 +00003619% o attenuate: Define the plasma attenuation factor.
cristy3ed852e2009-09-05 21:47:34 +00003620%
3621% o depth: Limit the plasma recursion depth.
3622%
cristy5cbc0162011-08-29 00:36:28 +00003623% o exception: return any errors or warnings in this structure.
3624%
cristy3ed852e2009-09-05 21:47:34 +00003625*/
3626
3627static inline Quantum PlasmaPixel(RandomInfo *random_info,
3628 const MagickRealType pixel,const MagickRealType noise)
3629{
3630 Quantum
3631 plasma;
3632
cristyce70c172010-01-07 17:15:30 +00003633 plasma=ClampToQuantum(pixel+noise*GetPseudoRandomValue(random_info)-
cristy3ed852e2009-09-05 21:47:34 +00003634 noise/2.0);
3635 return(plasma);
3636}
3637
cristyda1f9c12011-10-02 21:39:49 +00003638static MagickBooleanType PlasmaImageProxy(Image *image,CacheView *image_view,
3639 CacheView *u_view,CacheView *v_view,RandomInfo *random_info,
3640 const SegmentInfo *segment,size_t attenuate,size_t depth,
3641 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003642{
cristy3ed852e2009-09-05 21:47:34 +00003643 MagickRealType
3644 plasma;
3645
cristyda1f9c12011-10-02 21:39:49 +00003646 PixelChannel
3647 channel;
3648
3649 PixelTrait
3650 traits;
3651
3652 register const Quantum
3653 *restrict u,
3654 *restrict v;
3655
3656 register Quantum
3657 *restrict q;
3658
3659 register ssize_t
3660 i;
cristy3ed852e2009-09-05 21:47:34 +00003661
cristy9d314ff2011-03-09 01:30:28 +00003662 ssize_t
3663 x,
3664 x_mid,
3665 y,
3666 y_mid;
3667
cristy3ed852e2009-09-05 21:47:34 +00003668 if (((segment->x2-segment->x1) == 0.0) && ((segment->y2-segment->y1) == 0.0))
3669 return(MagickTrue);
3670 if (depth != 0)
3671 {
3672 SegmentInfo
3673 local_info;
3674
3675 /*
3676 Divide the area into quadrants and recurse.
3677 */
3678 depth--;
3679 attenuate++;
cristybb503372010-05-27 20:51:26 +00003680 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3681 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003682 local_info=(*segment);
3683 local_info.x2=(double) x_mid;
3684 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003685 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3686 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003687 local_info=(*segment);
3688 local_info.y1=(double) y_mid;
3689 local_info.x2=(double) x_mid;
cristyda1f9c12011-10-02 21:39:49 +00003690 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3691 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003692 local_info=(*segment);
3693 local_info.x1=(double) x_mid;
3694 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003695 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3696 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003697 local_info=(*segment);
3698 local_info.x1=(double) x_mid;
3699 local_info.y1=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003700 return(PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3701 &local_info,attenuate,depth,exception));
cristy3ed852e2009-09-05 21:47:34 +00003702 }
cristybb503372010-05-27 20:51:26 +00003703 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3704 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003705 if ((segment->x1 == (double) x_mid) && (segment->x2 == (double) x_mid) &&
3706 (segment->y1 == (double) y_mid) && (segment->y2 == (double) y_mid))
3707 return(MagickFalse);
3708 /*
3709 Average pixels and apply plasma.
3710 */
cristy3ed852e2009-09-05 21:47:34 +00003711 plasma=(MagickRealType) QuantumRange/(2.0*attenuate);
3712 if ((segment->x1 != (double) x_mid) || (segment->x2 != (double) x_mid))
3713 {
cristy3ed852e2009-09-05 21:47:34 +00003714 /*
3715 Left pixel.
3716 */
cristybb503372010-05-27 20:51:26 +00003717 x=(ssize_t) ceil(segment->x1-0.5);
cristy1707c6c2012-01-18 23:30:54 +00003718 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),1,1,
3719 exception);
3720 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),1,1,
3721 exception);
cristyc5c6f662010-09-22 14:23:02 +00003722 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003723 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3724 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003725 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003726 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3727 {
cristye2a912b2011-12-05 20:02:07 +00003728 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003729 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003730 if (traits == UndefinedPixelTrait)
3731 continue;
3732 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3733 }
cristyc5c6f662010-09-22 14:23:02 +00003734 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003735 if (segment->x1 != segment->x2)
3736 {
3737 /*
3738 Right pixel.
3739 */
cristybb503372010-05-27 20:51:26 +00003740 x=(ssize_t) ceil(segment->x2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003741 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3742 1,1,exception);
3743 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3744 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003745 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003746 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3747 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003748 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003749 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3750 {
cristye2a912b2011-12-05 20:02:07 +00003751 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003752 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003753 if (traits == UndefinedPixelTrait)
3754 continue;
3755 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3756 }
cristyc5c6f662010-09-22 14:23:02 +00003757 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003758 }
3759 }
3760 if ((segment->y1 != (double) y_mid) || (segment->y2 != (double) y_mid))
3761 {
3762 if ((segment->x1 != (double) x_mid) || (segment->y2 != (double) y_mid))
3763 {
cristy3ed852e2009-09-05 21:47:34 +00003764 /*
3765 Bottom pixel.
3766 */
cristybb503372010-05-27 20:51:26 +00003767 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003768 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3769 1,1,exception);
3770 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3771 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003772 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003773 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3774 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003775 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003776 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3777 {
cristye2a912b2011-12-05 20:02:07 +00003778 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003779 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003780 if (traits == UndefinedPixelTrait)
3781 continue;
3782 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3783 }
cristyc5c6f662010-09-22 14:23:02 +00003784 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003785 }
3786 if (segment->y1 != segment->y2)
3787 {
cristy3ed852e2009-09-05 21:47:34 +00003788 /*
3789 Top pixel.
3790 */
cristybb503372010-05-27 20:51:26 +00003791 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003792 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3793 1,1,exception);
3794 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3795 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003796 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003797 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3798 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003799 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003800 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3801 {
cristye2a912b2011-12-05 20:02:07 +00003802 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003803 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003804 if (traits == UndefinedPixelTrait)
3805 continue;
3806 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3807 }
cristyc5c6f662010-09-22 14:23:02 +00003808 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003809 }
3810 }
3811 if ((segment->x1 != segment->x2) || (segment->y1 != segment->y2))
3812 {
cristy3ed852e2009-09-05 21:47:34 +00003813 /*
3814 Middle pixel.
3815 */
cristybb503372010-05-27 20:51:26 +00003816 x=(ssize_t) ceil(segment->x1-0.5);
3817 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003818 u=GetCacheViewVirtualPixels(u_view,x,y,1,1,exception);
cristybb503372010-05-27 20:51:26 +00003819 x=(ssize_t) ceil(segment->x2-0.5);
3820 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003821 v=GetCacheViewVirtualPixels(v_view,x,y,1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003822 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003823 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3824 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003825 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003826 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3827 {
cristye2a912b2011-12-05 20:02:07 +00003828 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003829 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003830 if (traits == UndefinedPixelTrait)
3831 continue;
3832 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3833 }
cristyc5c6f662010-09-22 14:23:02 +00003834 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003835 }
3836 if (((segment->x2-segment->x1) < 3.0) && ((segment->y2-segment->y1) < 3.0))
3837 return(MagickTrue);
3838 return(MagickFalse);
3839}
cristyda1f9c12011-10-02 21:39:49 +00003840
cristy3ed852e2009-09-05 21:47:34 +00003841MagickExport MagickBooleanType PlasmaImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00003842 const SegmentInfo *segment,size_t attenuate,size_t depth,
3843 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003844{
cristyc5c6f662010-09-22 14:23:02 +00003845 CacheView
cristyda1f9c12011-10-02 21:39:49 +00003846 *image_view,
3847 *u_view,
3848 *v_view;
cristyc5c6f662010-09-22 14:23:02 +00003849
cristy3ed852e2009-09-05 21:47:34 +00003850 MagickBooleanType
3851 status;
3852
3853 RandomInfo
3854 *random_info;
3855
3856 if (image->debug != MagickFalse)
3857 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3858 assert(image != (Image *) NULL);
3859 assert(image->signature == MagickSignature);
3860 if (image->debug != MagickFalse)
3861 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
cristy5cbc0162011-08-29 00:36:28 +00003862 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristyc5c6f662010-09-22 14:23:02 +00003863 return(MagickFalse);
cristydb070952012-04-20 14:33:00 +00003864 image_view=AcquireAuthenticCacheView(image,exception);
3865 u_view=AcquireVirtualCacheView(image,exception);
3866 v_view=AcquireVirtualCacheView(image,exception);
cristy3ed852e2009-09-05 21:47:34 +00003867 random_info=AcquireRandomInfo();
cristyda1f9c12011-10-02 21:39:49 +00003868 status=PlasmaImageProxy(image,image_view,u_view,v_view,random_info,segment,
3869 attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003870 random_info=DestroyRandomInfo(random_info);
cristyda1f9c12011-10-02 21:39:49 +00003871 v_view=DestroyCacheView(v_view);
3872 u_view=DestroyCacheView(u_view);
cristyc5c6f662010-09-22 14:23:02 +00003873 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003874 return(status);
3875}
3876
3877/*
3878%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3879% %
3880% %
3881% %
3882% P o l a r o i d I m a g e %
3883% %
3884% %
3885% %
3886%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3887%
3888% PolaroidImage() simulates a Polaroid picture.
3889%
3890% The format of the AnnotateImage method is:
3891%
3892% Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristye9e3d382011-12-14 01:50:13 +00003893% const char *caption,const double angle,
3894% const PixelInterpolateMethod method,ExceptionInfo exception)
cristy3ed852e2009-09-05 21:47:34 +00003895%
3896% A description of each parameter follows:
3897%
3898% o image: the image.
3899%
3900% o draw_info: the draw info.
3901%
cristye9e3d382011-12-14 01:50:13 +00003902% o caption: the Polaroid caption.
3903%
cristycee97112010-05-28 00:44:52 +00003904% o angle: Apply the effect along this angle.
cristy3ed852e2009-09-05 21:47:34 +00003905%
cristy5c4e2582011-09-11 19:21:03 +00003906% o method: the pixel interpolation method.
3907%
cristy3ed852e2009-09-05 21:47:34 +00003908% o exception: return any errors or warnings in this structure.
3909%
3910*/
3911MagickExport Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristye9e3d382011-12-14 01:50:13 +00003912 const char *caption,const double angle,const PixelInterpolateMethod method,
cristy5c4e2582011-09-11 19:21:03 +00003913 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003914{
cristy3ed852e2009-09-05 21:47:34 +00003915 Image
3916 *bend_image,
3917 *caption_image,
3918 *flop_image,
3919 *picture_image,
3920 *polaroid_image,
3921 *rotate_image,
3922 *trim_image;
3923
cristybb503372010-05-27 20:51:26 +00003924 size_t
cristy3ed852e2009-09-05 21:47:34 +00003925 height;
3926
cristy9d314ff2011-03-09 01:30:28 +00003927 ssize_t
3928 quantum;
3929
cristy3ed852e2009-09-05 21:47:34 +00003930 /*
3931 Simulate a Polaroid picture.
3932 */
3933 assert(image != (Image *) NULL);
3934 assert(image->signature == MagickSignature);
3935 if (image->debug != MagickFalse)
3936 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3937 assert(exception != (ExceptionInfo *) NULL);
3938 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00003939 quantum=(ssize_t) MagickMax(MagickMax((double) image->columns,(double)
cristy3ed852e2009-09-05 21:47:34 +00003940 image->rows)/25.0,10.0);
3941 height=image->rows+2*quantum;
3942 caption_image=(Image *) NULL;
cristye9e3d382011-12-14 01:50:13 +00003943 if (caption != (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003944 {
3945 char
cristye9e3d382011-12-14 01:50:13 +00003946 geometry[MaxTextExtent],
3947 *text;
cristy3ed852e2009-09-05 21:47:34 +00003948
3949 DrawInfo
3950 *annotate_info;
3951
cristy3ed852e2009-09-05 21:47:34 +00003952 MagickBooleanType
3953 status;
3954
cristy9d314ff2011-03-09 01:30:28 +00003955 ssize_t
3956 count;
3957
cristy3ed852e2009-09-05 21:47:34 +00003958 TypeMetric
3959 metrics;
3960
3961 /*
3962 Generate caption image.
3963 */
3964 caption_image=CloneImage(image,image->columns,1,MagickTrue,exception);
3965 if (caption_image == (Image *) NULL)
3966 return((Image *) NULL);
3967 annotate_info=CloneDrawInfo((const ImageInfo *) NULL,draw_info);
cristye9e3d382011-12-14 01:50:13 +00003968 text=InterpretImageProperties((ImageInfo *) NULL,(Image *) image,caption,
3969 exception);
3970 (void) CloneString(&annotate_info->text,text);
cristy6b1d05e2010-09-22 19:17:27 +00003971 count=FormatMagickCaption(caption_image,annotate_info,MagickTrue,&metrics,
cristye9e3d382011-12-14 01:50:13 +00003972 &text,exception);
3973 status=SetImageExtent(caption_image,image->columns,(size_t) ((count+1)*
3974 (metrics.ascent-metrics.descent)+0.5),exception);
cristy3ed852e2009-09-05 21:47:34 +00003975 if (status == MagickFalse)
3976 caption_image=DestroyImage(caption_image);
3977 else
3978 {
3979 caption_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00003980 (void) SetImageBackgroundColor(caption_image,exception);
cristye9e3d382011-12-14 01:50:13 +00003981 (void) CloneString(&annotate_info->text,text);
cristyb51dff52011-05-19 16:55:47 +00003982 (void) FormatLocaleString(geometry,MaxTextExtent,"+0+%g",
cristy3ed852e2009-09-05 21:47:34 +00003983 metrics.ascent);
3984 if (annotate_info->gravity == UndefinedGravity)
3985 (void) CloneString(&annotate_info->geometry,AcquireString(
3986 geometry));
cristy5cbc0162011-08-29 00:36:28 +00003987 (void) AnnotateImage(caption_image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00003988 height+=caption_image->rows;
3989 }
3990 annotate_info=DestroyDrawInfo(annotate_info);
cristye9e3d382011-12-14 01:50:13 +00003991 text=DestroyString(text);
cristy3ed852e2009-09-05 21:47:34 +00003992 }
3993 picture_image=CloneImage(image,image->columns+2*quantum,height,MagickTrue,
3994 exception);
3995 if (picture_image == (Image *) NULL)
3996 {
3997 if (caption_image != (Image *) NULL)
3998 caption_image=DestroyImage(caption_image);
3999 return((Image *) NULL);
4000 }
4001 picture_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00004002 (void) SetImageBackgroundColor(picture_image,exception);
cristy39172402012-03-30 13:04:39 +00004003 (void) CompositeImage(picture_image,image,OverCompositeOp,MagickTrue,quantum,
cristyfeb3e962012-03-29 17:25:55 +00004004 quantum,exception);
cristy3ed852e2009-09-05 21:47:34 +00004005 if (caption_image != (Image *) NULL)
4006 {
cristyfeb3e962012-03-29 17:25:55 +00004007 (void) CompositeImage(picture_image,caption_image,OverCompositeOp,
cristy39172402012-03-30 13:04:39 +00004008 MagickTrue,quantum,(ssize_t) (image->rows+3*quantum/2),exception);
cristy3ed852e2009-09-05 21:47:34 +00004009 caption_image=DestroyImage(caption_image);
4010 }
cristy9950d572011-10-01 18:22:35 +00004011 (void) QueryColorCompliance("none",AllCompliance,
4012 &picture_image->background_color,exception);
cristy63240882011-08-05 19:05:27 +00004013 (void) SetImageAlphaChannel(picture_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004014 rotate_image=RotateImage(picture_image,90.0,exception);
4015 picture_image=DestroyImage(picture_image);
4016 if (rotate_image == (Image *) NULL)
4017 return((Image *) NULL);
4018 picture_image=rotate_image;
4019 bend_image=WaveImage(picture_image,0.01*picture_image->rows,2.0*
cristy5c4e2582011-09-11 19:21:03 +00004020 picture_image->columns,method,exception);
cristy3ed852e2009-09-05 21:47:34 +00004021 picture_image=DestroyImage(picture_image);
4022 if (bend_image == (Image *) NULL)
4023 return((Image *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00004024 picture_image=bend_image;
4025 rotate_image=RotateImage(picture_image,-90.0,exception);
4026 picture_image=DestroyImage(picture_image);
4027 if (rotate_image == (Image *) NULL)
4028 return((Image *) NULL);
4029 picture_image=rotate_image;
4030 picture_image->background_color=image->background_color;
anthonyf46d4262012-03-26 03:30:34 +00004031 polaroid_image=ShadowImage(picture_image,80.0,2.0,quantum/3,quantum/3,
cristy3ed852e2009-09-05 21:47:34 +00004032 exception);
4033 if (polaroid_image == (Image *) NULL)
4034 {
4035 picture_image=DestroyImage(picture_image);
4036 return(picture_image);
4037 }
4038 flop_image=FlopImage(polaroid_image,exception);
4039 polaroid_image=DestroyImage(polaroid_image);
4040 if (flop_image == (Image *) NULL)
4041 {
4042 picture_image=DestroyImage(picture_image);
4043 return(picture_image);
4044 }
4045 polaroid_image=flop_image;
cristyfeb3e962012-03-29 17:25:55 +00004046 (void) CompositeImage(polaroid_image,picture_image,OverCompositeOp,
cristy39172402012-03-30 13:04:39 +00004047 MagickTrue,(ssize_t) (-0.01*picture_image->columns/2.0),0L,exception);
cristy3ed852e2009-09-05 21:47:34 +00004048 picture_image=DestroyImage(picture_image);
cristy9950d572011-10-01 18:22:35 +00004049 (void) QueryColorCompliance("none",AllCompliance,
4050 &polaroid_image->background_color,exception);
cristy3ed852e2009-09-05 21:47:34 +00004051 rotate_image=RotateImage(polaroid_image,angle,exception);
4052 polaroid_image=DestroyImage(polaroid_image);
4053 if (rotate_image == (Image *) NULL)
4054 return((Image *) NULL);
4055 polaroid_image=rotate_image;
4056 trim_image=TrimImage(polaroid_image,exception);
4057 polaroid_image=DestroyImage(polaroid_image);
4058 if (trim_image == (Image *) NULL)
4059 return((Image *) NULL);
4060 polaroid_image=trim_image;
4061 return(polaroid_image);
4062}
4063
4064/*
4065%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4066% %
4067% %
4068% %
cristy3ed852e2009-09-05 21:47:34 +00004069% S e p i a T o n e I m a g e %
4070% %
4071% %
4072% %
4073%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4074%
4075% MagickSepiaToneImage() applies a special effect to the image, similar to the
4076% effect achieved in a photo darkroom by sepia toning. Threshold ranges from
4077% 0 to QuantumRange and is a measure of the extent of the sepia toning. A
4078% threshold of 80% is a good starting point for a reasonable tone.
4079%
4080% The format of the SepiaToneImage method is:
4081%
4082% Image *SepiaToneImage(const Image *image,const double threshold,
4083% ExceptionInfo *exception)
4084%
4085% A description of each parameter follows:
4086%
4087% o image: the image.
4088%
4089% o threshold: the tone threshold.
4090%
4091% o exception: return any errors or warnings in this structure.
4092%
4093*/
4094MagickExport Image *SepiaToneImage(const Image *image,const double threshold,
4095 ExceptionInfo *exception)
4096{
4097#define SepiaToneImageTag "SepiaTone/Image"
4098
cristyc4c8d132010-01-07 01:58:38 +00004099 CacheView
4100 *image_view,
4101 *sepia_view;
4102
cristy3ed852e2009-09-05 21:47:34 +00004103 Image
4104 *sepia_image;
4105
cristy3ed852e2009-09-05 21:47:34 +00004106 MagickBooleanType
4107 status;
4108
cristybb503372010-05-27 20:51:26 +00004109 MagickOffsetType
4110 progress;
4111
4112 ssize_t
4113 y;
4114
cristy3ed852e2009-09-05 21:47:34 +00004115 /*
4116 Initialize sepia-toned image attributes.
4117 */
4118 assert(image != (const Image *) NULL);
4119 assert(image->signature == MagickSignature);
4120 if (image->debug != MagickFalse)
4121 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4122 assert(exception != (ExceptionInfo *) NULL);
4123 assert(exception->signature == MagickSignature);
cristy1707c6c2012-01-18 23:30:54 +00004124 sepia_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004125 if (sepia_image == (Image *) NULL)
4126 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004127 if (SetImageStorageClass(sepia_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004128 {
cristy3ed852e2009-09-05 21:47:34 +00004129 sepia_image=DestroyImage(sepia_image);
4130 return((Image *) NULL);
4131 }
4132 /*
4133 Tone each row of the image.
4134 */
4135 status=MagickTrue;
4136 progress=0;
cristydb070952012-04-20 14:33:00 +00004137 image_view=AcquireVirtualCacheView(image,exception);
4138 sepia_view=AcquireAuthenticCacheView(sepia_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00004139#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00004140 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00004141#endif
cristybb503372010-05-27 20:51:26 +00004142 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004143 {
cristy4c08aed2011-07-01 19:47:50 +00004144 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004145 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00004146
cristybb503372010-05-27 20:51:26 +00004147 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004148 x;
4149
cristy4c08aed2011-07-01 19:47:50 +00004150 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004151 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004152
4153 if (status == MagickFalse)
4154 continue;
4155 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy1707c6c2012-01-18 23:30:54 +00004156 q=GetCacheViewAuthenticPixels(sepia_view,0,y,sepia_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00004157 exception);
cristy4c08aed2011-07-01 19:47:50 +00004158 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004159 {
4160 status=MagickFalse;
4161 continue;
4162 }
cristybb503372010-05-27 20:51:26 +00004163 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004164 {
4165 MagickRealType
4166 intensity,
4167 tone;
4168
cristy4c08aed2011-07-01 19:47:50 +00004169 intensity=(MagickRealType) GetPixelIntensity(image,p);
cristy3ed852e2009-09-05 21:47:34 +00004170 tone=intensity > threshold ? (MagickRealType) QuantumRange : intensity+
4171 (MagickRealType) QuantumRange-threshold;
cristy4c08aed2011-07-01 19:47:50 +00004172 SetPixelRed(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004173 tone=intensity > (7.0*threshold/6.0) ? (MagickRealType) QuantumRange :
4174 intensity+(MagickRealType) QuantumRange-7.0*threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004175 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004176 tone=intensity < (threshold/6.0) ? 0 : intensity-threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004177 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004178 tone=threshold/7.0;
cristy4c08aed2011-07-01 19:47:50 +00004179 if ((MagickRealType) GetPixelGreen(image,q) < tone)
4180 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
4181 if ((MagickRealType) GetPixelBlue(image,q) < tone)
4182 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristyed231572011-07-14 02:18:59 +00004183 p+=GetPixelChannels(image);
4184 q+=GetPixelChannels(sepia_image);
cristy3ed852e2009-09-05 21:47:34 +00004185 }
4186 if (SyncCacheViewAuthenticPixels(sepia_view,exception) == MagickFalse)
4187 status=MagickFalse;
4188 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4189 {
4190 MagickBooleanType
4191 proceed;
4192
cristyb5d5f722009-11-04 03:03:49 +00004193#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00004194 #pragma omp critical (MagickCore_SepiaToneImage)
cristy3ed852e2009-09-05 21:47:34 +00004195#endif
4196 proceed=SetImageProgress(image,SepiaToneImageTag,progress++,
4197 image->rows);
4198 if (proceed == MagickFalse)
4199 status=MagickFalse;
4200 }
4201 }
4202 sepia_view=DestroyCacheView(sepia_view);
4203 image_view=DestroyCacheView(image_view);
cristye23ec9d2011-08-16 18:15:40 +00004204 (void) NormalizeImage(sepia_image,exception);
4205 (void) ContrastImage(sepia_image,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004206 if (status == MagickFalse)
4207 sepia_image=DestroyImage(sepia_image);
4208 return(sepia_image);
4209}
4210
4211/*
4212%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4213% %
4214% %
4215% %
4216% S h a d o w I m a g e %
4217% %
4218% %
4219% %
4220%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4221%
4222% ShadowImage() simulates a shadow from the specified image and returns it.
4223%
4224% The format of the ShadowImage method is:
4225%
cristy70cddf72011-12-10 22:42:42 +00004226% Image *ShadowImage(const Image *image,const double alpha,
cristyaa2c16c2012-03-25 22:21:35 +00004227% const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4228% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004229%
4230% A description of each parameter follows:
4231%
4232% o image: the image.
4233%
cristy70cddf72011-12-10 22:42:42 +00004234% o alpha: percentage transparency.
cristy3ed852e2009-09-05 21:47:34 +00004235%
4236% o sigma: the standard deviation of the Gaussian, in pixels.
4237%
4238% o x_offset: the shadow x-offset.
4239%
4240% o y_offset: the shadow y-offset.
4241%
4242% o exception: return any errors or warnings in this structure.
4243%
4244*/
cristy70cddf72011-12-10 22:42:42 +00004245MagickExport Image *ShadowImage(const Image *image,const double alpha,
cristyaa2c16c2012-03-25 22:21:35 +00004246 const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4247 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004248{
4249#define ShadowImageTag "Shadow/Image"
4250
cristy70cddf72011-12-10 22:42:42 +00004251 CacheView
4252 *image_view;
4253
cristybd5a96c2011-08-21 00:04:26 +00004254 ChannelType
4255 channel_mask;
4256
cristy3ed852e2009-09-05 21:47:34 +00004257 Image
4258 *border_image,
4259 *clone_image,
4260 *shadow_image;
4261
cristy70cddf72011-12-10 22:42:42 +00004262 MagickBooleanType
4263 status;
4264
cristy3ed852e2009-09-05 21:47:34 +00004265 RectangleInfo
4266 border_info;
4267
cristy70cddf72011-12-10 22:42:42 +00004268 ssize_t
4269 y;
4270
cristy3ed852e2009-09-05 21:47:34 +00004271 assert(image != (Image *) NULL);
4272 assert(image->signature == MagickSignature);
4273 if (image->debug != MagickFalse)
4274 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4275 assert(exception != (ExceptionInfo *) NULL);
4276 assert(exception->signature == MagickSignature);
4277 clone_image=CloneImage(image,0,0,MagickTrue,exception);
4278 if (clone_image == (Image *) NULL)
4279 return((Image *) NULL);
cristy0898eba2012-04-09 16:38:29 +00004280 if (IsGrayColorspace(image->colorspace) != MagickFalse)
4281 (void) TransformImageColorspace(clone_image,sRGBColorspace,exception);
cristy387430f2012-02-07 13:09:46 +00004282 (void) SetImageVirtualPixelMethod(clone_image,EdgeVirtualPixelMethod,
4283 exception);
cristybb503372010-05-27 20:51:26 +00004284 border_info.width=(size_t) floor(2.0*sigma+0.5);
4285 border_info.height=(size_t) floor(2.0*sigma+0.5);
cristy3ed852e2009-09-05 21:47:34 +00004286 border_info.x=0;
4287 border_info.y=0;
cristy9950d572011-10-01 18:22:35 +00004288 (void) QueryColorCompliance("none",AllCompliance,&clone_image->border_color,
4289 exception);
cristy70cddf72011-12-10 22:42:42 +00004290 clone_image->matte=MagickTrue;
4291 border_image=BorderImage(clone_image,&border_info,OverCompositeOp,exception);
cristy3ed852e2009-09-05 21:47:34 +00004292 clone_image=DestroyImage(clone_image);
4293 if (border_image == (Image *) NULL)
4294 return((Image *) NULL);
4295 if (border_image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00004296 (void) SetImageAlphaChannel(border_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004297 /*
4298 Shadow image.
4299 */
cristy70cddf72011-12-10 22:42:42 +00004300 status=MagickTrue;
cristydb070952012-04-20 14:33:00 +00004301 image_view=AcquireAuthenticCacheView(border_image,exception);
cristy70cddf72011-12-10 22:42:42 +00004302 for (y=0; y < (ssize_t) border_image->rows; y++)
4303 {
4304 PixelInfo
4305 background_color;
4306
4307 register Quantum
4308 *restrict q;
4309
4310 register ssize_t
4311 x;
4312
4313 if (status == MagickFalse)
4314 continue;
4315 q=QueueCacheViewAuthenticPixels(image_view,0,y,border_image->columns,1,
4316 exception);
4317 if (q == (Quantum *) NULL)
4318 {
4319 status=MagickFalse;
4320 continue;
4321 }
4322 background_color=border_image->background_color;
4323 background_color.matte=MagickTrue;
4324 for (x=0; x < (ssize_t) border_image->columns; x++)
4325 {
4326 if (border_image->matte != MagickFalse)
4327 background_color.alpha=GetPixelAlpha(border_image,q)*alpha/100.0;
4328 SetPixelInfoPixel(border_image,&background_color,q);
4329 q+=GetPixelChannels(border_image);
4330 }
4331 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4332 status=MagickFalse;
4333 }
4334 image_view=DestroyCacheView(image_view);
4335 if (status == MagickFalse)
4336 {
4337 border_image=DestroyImage(border_image);
4338 return((Image *) NULL);
4339 }
cristybd5a96c2011-08-21 00:04:26 +00004340 channel_mask=SetPixelChannelMask(border_image,AlphaChannel);
cristyaa2c16c2012-03-25 22:21:35 +00004341 shadow_image=BlurImage(border_image,0.0,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +00004342 border_image=DestroyImage(border_image);
4343 if (shadow_image == (Image *) NULL)
4344 return((Image *) NULL);
cristyae1969f2011-12-10 03:07:36 +00004345 (void) SetPixelChannelMapMask(shadow_image,channel_mask);
cristy3ed852e2009-09-05 21:47:34 +00004346 if (shadow_image->page.width == 0)
4347 shadow_image->page.width=shadow_image->columns;
4348 if (shadow_image->page.height == 0)
4349 shadow_image->page.height=shadow_image->rows;
cristybb503372010-05-27 20:51:26 +00004350 shadow_image->page.width+=x_offset-(ssize_t) border_info.width;
4351 shadow_image->page.height+=y_offset-(ssize_t) border_info.height;
4352 shadow_image->page.x+=x_offset-(ssize_t) border_info.width;
4353 shadow_image->page.y+=y_offset-(ssize_t) border_info.height;
cristy3ed852e2009-09-05 21:47:34 +00004354 return(shadow_image);
4355}
4356
4357/*
4358%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4359% %
4360% %
4361% %
4362% S k e t c h I m a g e %
4363% %
4364% %
4365% %
4366%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4367%
4368% SketchImage() simulates a pencil sketch. We convolve the image with a
4369% Gaussian operator of the given radius and standard deviation (sigma). For
4370% reasonable results, radius should be larger than sigma. Use a radius of 0
4371% and SketchImage() selects a suitable radius for you. Angle gives the angle
4372% of the sketch.
4373%
4374% The format of the SketchImage method is:
4375%
4376% Image *SketchImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00004377% const double sigma,const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004378%
4379% A description of each parameter follows:
4380%
4381% o image: the image.
4382%
cristy574cc262011-08-05 01:23:58 +00004383% o radius: the radius of the Gaussian, in pixels, not counting the
4384% center pixel.
cristy3ed852e2009-09-05 21:47:34 +00004385%
4386% o sigma: the standard deviation of the Gaussian, in pixels.
4387%
cristyf7ef0252011-09-09 14:50:06 +00004388% o angle: apply the effect along this angle.
4389%
cristy3ed852e2009-09-05 21:47:34 +00004390% o exception: return any errors or warnings in this structure.
4391%
4392*/
4393MagickExport Image *SketchImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00004394 const double sigma,const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004395{
cristyfa112112010-01-04 17:48:07 +00004396 CacheView
4397 *random_view;
4398
cristy3ed852e2009-09-05 21:47:34 +00004399 Image
4400 *blend_image,
4401 *blur_image,
4402 *dodge_image,
4403 *random_image,
4404 *sketch_image;
4405
cristy3ed852e2009-09-05 21:47:34 +00004406 MagickBooleanType
4407 status;
4408
cristy3ed852e2009-09-05 21:47:34 +00004409 RandomInfo
cristyfa112112010-01-04 17:48:07 +00004410 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00004411
cristy9d314ff2011-03-09 01:30:28 +00004412 ssize_t
4413 y;
4414
cristy57340e02012-05-05 00:53:23 +00004415 unsigned long
4416 key;
4417
cristy3ed852e2009-09-05 21:47:34 +00004418 /*
4419 Sketch image.
4420 */
4421 random_image=CloneImage(image,image->columns << 1,image->rows << 1,
4422 MagickTrue,exception);
4423 if (random_image == (Image *) NULL)
4424 return((Image *) NULL);
4425 status=MagickTrue;
cristy1b784432009-12-19 02:20:40 +00004426 random_info=AcquireRandomInfoThreadSet();
cristy57340e02012-05-05 00:53:23 +00004427 key=GetRandomSecretKey(random_info[0]);
cristydb070952012-04-20 14:33:00 +00004428 random_view=AcquireAuthenticCacheView(random_image,exception);
cristy1b784432009-12-19 02:20:40 +00004429#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy57340e02012-05-05 00:53:23 +00004430 #pragma omp parallel for schedule(static,4) shared(status) \
4431 if (key == ~0UL) num_threads(GetMagickResourceLimit(ThreadResource))
cristy1b784432009-12-19 02:20:40 +00004432#endif
cristybb503372010-05-27 20:51:26 +00004433 for (y=0; y < (ssize_t) random_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004434 {
cristy5c9e6f22010-09-17 17:31:01 +00004435 const int
4436 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00004437
cristybb503372010-05-27 20:51:26 +00004438 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004439 x;
4440
cristy4c08aed2011-07-01 19:47:50 +00004441 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004442 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004443
cristy1b784432009-12-19 02:20:40 +00004444 if (status == MagickFalse)
4445 continue;
cristy3ed852e2009-09-05 21:47:34 +00004446 q=QueueCacheViewAuthenticPixels(random_view,0,y,random_image->columns,1,
4447 exception);
cristyacd2ed22011-08-30 01:44:23 +00004448 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004449 {
4450 status=MagickFalse;
4451 continue;
4452 }
cristybb503372010-05-27 20:51:26 +00004453 for (x=0; x < (ssize_t) random_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004454 {
cristy76f512e2011-09-12 01:26:56 +00004455 MagickRealType
4456 value;
4457
4458 register ssize_t
4459 i;
4460
cristy10a6c612012-01-29 21:41:05 +00004461 if (GetPixelMask(random_image,q) != 0)
4462 {
4463 q+=GetPixelChannels(random_image);
4464 continue;
4465 }
cristy76f512e2011-09-12 01:26:56 +00004466 value=GetPseudoRandomValue(random_info[id]);
4467 for (i=0; i < (ssize_t) GetPixelChannels(random_image); i++)
4468 {
cristyabace412011-12-11 15:56:53 +00004469 PixelChannel
4470 channel;
4471
cristy76f512e2011-09-12 01:26:56 +00004472 PixelTrait
4473 traits;
4474
cristyabace412011-12-11 15:56:53 +00004475 channel=GetPixelChannelMapChannel(image,i);
4476 traits=GetPixelChannelMapTraits(image,channel);
cristy76f512e2011-09-12 01:26:56 +00004477 if (traits == UndefinedPixelTrait)
4478 continue;
4479 q[i]=ClampToQuantum(QuantumRange*value);
4480 }
cristyed231572011-07-14 02:18:59 +00004481 q+=GetPixelChannels(random_image);
cristy3ed852e2009-09-05 21:47:34 +00004482 }
4483 if (SyncCacheViewAuthenticPixels(random_view,exception) == MagickFalse)
4484 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004485 }
4486 random_view=DestroyCacheView(random_view);
cristy1b784432009-12-19 02:20:40 +00004487 random_info=DestroyRandomInfoThreadSet(random_info);
cristy3ed852e2009-09-05 21:47:34 +00004488 if (status == MagickFalse)
4489 {
4490 random_image=DestroyImage(random_image);
4491 return(random_image);
4492 }
cristyaa2c16c2012-03-25 22:21:35 +00004493 blur_image=MotionBlurImage(random_image,radius,sigma,angle,exception);
cristy3ed852e2009-09-05 21:47:34 +00004494 random_image=DestroyImage(random_image);
4495 if (blur_image == (Image *) NULL)
4496 return((Image *) NULL);
cristy6bfd6902011-12-09 01:33:45 +00004497 dodge_image=EdgeImage(blur_image,radius,1.0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004498 blur_image=DestroyImage(blur_image);
4499 if (dodge_image == (Image *) NULL)
4500 return((Image *) NULL);
cristye23ec9d2011-08-16 18:15:40 +00004501 (void) NormalizeImage(dodge_image,exception);
cristyb3e7c6c2011-07-24 01:43:55 +00004502 (void) NegateImage(dodge_image,MagickFalse,exception);
cristye941a752011-10-15 01:52:48 +00004503 (void) TransformImage(&dodge_image,(char *) NULL,"50%",exception);
cristy3ed852e2009-09-05 21:47:34 +00004504 sketch_image=CloneImage(image,0,0,MagickTrue,exception);
4505 if (sketch_image == (Image *) NULL)
4506 {
4507 dodge_image=DestroyImage(dodge_image);
4508 return((Image *) NULL);
4509 }
cristyfeb3e962012-03-29 17:25:55 +00004510 (void) CompositeImage(sketch_image,dodge_image,ColorDodgeCompositeOp,
cristy39172402012-03-30 13:04:39 +00004511 MagickTrue,0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004512 dodge_image=DestroyImage(dodge_image);
4513 blend_image=CloneImage(image,0,0,MagickTrue,exception);
4514 if (blend_image == (Image *) NULL)
4515 {
4516 sketch_image=DestroyImage(sketch_image);
4517 return((Image *) NULL);
4518 }
4519 (void) SetImageArtifact(blend_image,"compose:args","20x80");
cristy39172402012-03-30 13:04:39 +00004520 (void) CompositeImage(sketch_image,blend_image,BlendCompositeOp,MagickTrue,
cristyfeb3e962012-03-29 17:25:55 +00004521 0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004522 blend_image=DestroyImage(blend_image);
4523 return(sketch_image);
4524}
4525
4526/*
4527%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4528% %
4529% %
4530% %
4531% S o l a r i z e I m a g e %
4532% %
4533% %
4534% %
4535%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4536%
4537% SolarizeImage() applies a special effect to the image, similar to the effect
4538% achieved in a photo darkroom by selectively exposing areas of photo
4539% sensitive paper to light. Threshold ranges from 0 to QuantumRange and is a
4540% measure of the extent of the solarization.
4541%
4542% The format of the SolarizeImage method is:
4543%
cristy5cbc0162011-08-29 00:36:28 +00004544% MagickBooleanType SolarizeImage(Image *image,const double threshold,
4545% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004546%
4547% A description of each parameter follows:
4548%
4549% o image: the image.
4550%
4551% o threshold: Define the extent of the solarization.
4552%
cristy5cbc0162011-08-29 00:36:28 +00004553% o exception: return any errors or warnings in this structure.
4554%
cristy3ed852e2009-09-05 21:47:34 +00004555*/
4556MagickExport MagickBooleanType SolarizeImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00004557 const double threshold,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004558{
4559#define SolarizeImageTag "Solarize/Image"
4560
cristyc4c8d132010-01-07 01:58:38 +00004561 CacheView
4562 *image_view;
4563
cristy3ed852e2009-09-05 21:47:34 +00004564 MagickBooleanType
4565 status;
4566
cristybb503372010-05-27 20:51:26 +00004567 MagickOffsetType
4568 progress;
4569
4570 ssize_t
4571 y;
4572
cristy3ed852e2009-09-05 21:47:34 +00004573 assert(image != (Image *) NULL);
4574 assert(image->signature == MagickSignature);
4575 if (image->debug != MagickFalse)
4576 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4577 if (image->storage_class == PseudoClass)
4578 {
cristybb503372010-05-27 20:51:26 +00004579 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004580 i;
4581
4582 /*
4583 Solarize colormap.
4584 */
cristybb503372010-05-27 20:51:26 +00004585 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00004586 {
4587 if ((MagickRealType) image->colormap[i].red > threshold)
4588 image->colormap[i].red=(Quantum) QuantumRange-image->colormap[i].red;
4589 if ((MagickRealType) image->colormap[i].green > threshold)
4590 image->colormap[i].green=(Quantum) QuantumRange-
4591 image->colormap[i].green;
4592 if ((MagickRealType) image->colormap[i].blue > threshold)
4593 image->colormap[i].blue=(Quantum) QuantumRange-
4594 image->colormap[i].blue;
4595 }
4596 }
4597 /*
4598 Solarize image.
4599 */
4600 status=MagickTrue;
4601 progress=0;
cristydb070952012-04-20 14:33:00 +00004602 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00004603#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00004604 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00004605#endif
cristybb503372010-05-27 20:51:26 +00004606 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004607 {
cristybb503372010-05-27 20:51:26 +00004608 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004609 x;
4610
cristy4c08aed2011-07-01 19:47:50 +00004611 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004612 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004613
4614 if (status == MagickFalse)
4615 continue;
cristy5cbc0162011-08-29 00:36:28 +00004616 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00004617 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004618 {
4619 status=MagickFalse;
4620 continue;
4621 }
cristybb503372010-05-27 20:51:26 +00004622 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004623 {
cristy76f512e2011-09-12 01:26:56 +00004624 register ssize_t
4625 i;
4626
cristy10a6c612012-01-29 21:41:05 +00004627 if (GetPixelMask(image,q) != 0)
4628 {
4629 q+=GetPixelChannels(image);
4630 continue;
4631 }
cristy76f512e2011-09-12 01:26:56 +00004632 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4633 {
cristyabace412011-12-11 15:56:53 +00004634 PixelChannel
4635 channel;
4636
cristy76f512e2011-09-12 01:26:56 +00004637 PixelTrait
4638 traits;
4639
cristyabace412011-12-11 15:56:53 +00004640 channel=GetPixelChannelMapChannel(image,i);
4641 traits=GetPixelChannelMapTraits(image,channel);
cristy76f512e2011-09-12 01:26:56 +00004642 if ((traits == UndefinedPixelTrait) ||
4643 ((traits & CopyPixelTrait) != 0))
4644 continue;
4645 if ((MagickRealType) q[i] > threshold)
4646 q[i]=QuantumRange-q[i];
4647 }
cristyed231572011-07-14 02:18:59 +00004648 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00004649 }
4650 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4651 status=MagickFalse;
4652 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4653 {
4654 MagickBooleanType
4655 proceed;
4656
cristyb5d5f722009-11-04 03:03:49 +00004657#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00004658 #pragma omp critical (MagickCore_SolarizeImage)
cristy3ed852e2009-09-05 21:47:34 +00004659#endif
4660 proceed=SetImageProgress(image,SolarizeImageTag,progress++,image->rows);
4661 if (proceed == MagickFalse)
4662 status=MagickFalse;
4663 }
4664 }
4665 image_view=DestroyCacheView(image_view);
4666 return(status);
4667}
4668
4669/*
4670%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4671% %
4672% %
4673% %
4674% S t e g a n o I m a g e %
4675% %
4676% %
4677% %
4678%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4679%
4680% SteganoImage() hides a digital watermark within the image. Recover
4681% the hidden watermark later to prove that the authenticity of an image.
4682% Offset defines the start position within the image to hide the watermark.
4683%
4684% The format of the SteganoImage method is:
4685%
4686% Image *SteganoImage(const Image *image,Image *watermark,
4687% ExceptionInfo *exception)
4688%
4689% A description of each parameter follows:
4690%
4691% o image: the image.
4692%
4693% o watermark: the watermark image.
4694%
4695% o exception: return any errors or warnings in this structure.
4696%
4697*/
4698MagickExport Image *SteganoImage(const Image *image,const Image *watermark,
4699 ExceptionInfo *exception)
4700{
cristye1bf8ad2010-09-19 17:07:03 +00004701#define GetBit(alpha,i) ((((size_t) (alpha) >> (size_t) (i)) & 0x01) != 0)
cristy4c08aed2011-07-01 19:47:50 +00004702#define SetBit(alpha,i,set) (Quantum) ((set) != 0 ? (size_t) (alpha) \
cristyeaedf062010-05-29 22:36:02 +00004703 | (one << (size_t) (i)) : (size_t) (alpha) & ~(one << (size_t) (i)))
cristy3ed852e2009-09-05 21:47:34 +00004704#define SteganoImageTag "Stegano/Image"
4705
cristyb0d3bb92010-09-22 14:37:58 +00004706 CacheView
4707 *stegano_view,
4708 *watermark_view;
4709
cristy3ed852e2009-09-05 21:47:34 +00004710 Image
4711 *stegano_image;
4712
4713 int
4714 c;
4715
cristy3ed852e2009-09-05 21:47:34 +00004716 MagickBooleanType
4717 status;
4718
cristy101ab702011-10-13 13:06:32 +00004719 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00004720 pixel;
4721
cristy4c08aed2011-07-01 19:47:50 +00004722 register Quantum
cristy3ed852e2009-09-05 21:47:34 +00004723 *q;
4724
cristye1bf8ad2010-09-19 17:07:03 +00004725 register ssize_t
4726 x;
4727
cristybb503372010-05-27 20:51:26 +00004728 size_t
cristyeaedf062010-05-29 22:36:02 +00004729 depth,
4730 one;
cristy3ed852e2009-09-05 21:47:34 +00004731
cristye1bf8ad2010-09-19 17:07:03 +00004732 ssize_t
4733 i,
4734 j,
4735 k,
4736 y;
4737
cristy3ed852e2009-09-05 21:47:34 +00004738 /*
4739 Initialize steganographic image attributes.
4740 */
4741 assert(image != (const Image *) NULL);
4742 assert(image->signature == MagickSignature);
4743 if (image->debug != MagickFalse)
4744 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4745 assert(watermark != (const Image *) NULL);
4746 assert(watermark->signature == MagickSignature);
4747 assert(exception != (ExceptionInfo *) NULL);
4748 assert(exception->signature == MagickSignature);
cristyeaedf062010-05-29 22:36:02 +00004749 one=1UL;
cristy3ed852e2009-09-05 21:47:34 +00004750 stegano_image=CloneImage(image,0,0,MagickTrue,exception);
4751 if (stegano_image == (Image *) NULL)
4752 return((Image *) NULL);
cristyf61b1832012-04-01 01:38:19 +00004753 stegano_image->depth=MAGICKCORE_QUANTUM_DEPTH;
cristy574cc262011-08-05 01:23:58 +00004754 if (SetImageStorageClass(stegano_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004755 {
cristy3ed852e2009-09-05 21:47:34 +00004756 stegano_image=DestroyImage(stegano_image);
4757 return((Image *) NULL);
4758 }
cristy3ed852e2009-09-05 21:47:34 +00004759 /*
4760 Hide watermark in low-order bits of image.
4761 */
4762 c=0;
4763 i=0;
4764 j=0;
4765 depth=stegano_image->depth;
cristyf61b1832012-04-01 01:38:19 +00004766 k=stegano_image->offset;
cristyda16f162011-02-19 23:52:17 +00004767 status=MagickTrue;
cristydb070952012-04-20 14:33:00 +00004768 watermark_view=AcquireVirtualCacheView(watermark,exception);
4769 stegano_view=AcquireAuthenticCacheView(stegano_image,exception);
cristybb503372010-05-27 20:51:26 +00004770 for (i=(ssize_t) depth-1; (i >= 0) && (j < (ssize_t) depth); i--)
cristy3ed852e2009-09-05 21:47:34 +00004771 {
cristybb503372010-05-27 20:51:26 +00004772 for (y=0; (y < (ssize_t) watermark->rows) && (j < (ssize_t) depth); y++)
cristy3ed852e2009-09-05 21:47:34 +00004773 {
cristybb503372010-05-27 20:51:26 +00004774 for (x=0; (x < (ssize_t) watermark->columns) && (j < (ssize_t) depth); x++)
cristy3ed852e2009-09-05 21:47:34 +00004775 {
cristy1707c6c2012-01-18 23:30:54 +00004776 ssize_t
4777 offset;
4778
cristyf05d4942012-03-17 16:26:09 +00004779 (void) GetOneCacheViewVirtualPixelInfo(watermark_view,x,y,&pixel,
cristyda1f9c12011-10-02 21:39:49 +00004780 exception);
cristy1707c6c2012-01-18 23:30:54 +00004781 offset=k/(ssize_t) stegano_image->columns;
4782 if (offset >= (ssize_t) stegano_image->rows)
cristy3ed852e2009-09-05 21:47:34 +00004783 break;
cristyb0d3bb92010-09-22 14:37:58 +00004784 q=GetCacheViewAuthenticPixels(stegano_view,k % (ssize_t)
4785 stegano_image->columns,k/(ssize_t) stegano_image->columns,1,1,
4786 exception);
cristyacd2ed22011-08-30 01:44:23 +00004787 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004788 break;
4789 switch (c)
4790 {
4791 case 0:
4792 {
cristyf61b1832012-04-01 01:38:19 +00004793 SetPixelRed(stegano_image,SetBit(GetPixelRed(stegano_image,q),j,
4794 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004795 break;
4796 }
4797 case 1:
4798 {
cristyf61b1832012-04-01 01:38:19 +00004799 SetPixelGreen(stegano_image,SetBit(GetPixelGreen(stegano_image,q),j,
4800 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004801 break;
4802 }
4803 case 2:
4804 {
cristyf61b1832012-04-01 01:38:19 +00004805 SetPixelBlue(stegano_image,SetBit(GetPixelBlue(stegano_image,q),j,
4806 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004807 break;
4808 }
4809 }
cristyb0d3bb92010-09-22 14:37:58 +00004810 if (SyncCacheViewAuthenticPixels(stegano_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004811 break;
4812 c++;
4813 if (c == 3)
4814 c=0;
4815 k++;
cristybb503372010-05-27 20:51:26 +00004816 if (k == (ssize_t) (stegano_image->columns*stegano_image->columns))
cristy3ed852e2009-09-05 21:47:34 +00004817 k=0;
cristyf61b1832012-04-01 01:38:19 +00004818 if (k == stegano_image->offset)
cristy3ed852e2009-09-05 21:47:34 +00004819 j++;
4820 }
4821 }
cristy8b27a6d2010-02-14 03:31:15 +00004822 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004823 {
cristy8b27a6d2010-02-14 03:31:15 +00004824 MagickBooleanType
4825 proceed;
4826
4827 proceed=SetImageProgress(image,SteganoImageTag,(MagickOffsetType)
4828 (depth-i),depth);
4829 if (proceed == MagickFalse)
4830 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004831 }
4832 }
cristyb0d3bb92010-09-22 14:37:58 +00004833 stegano_view=DestroyCacheView(stegano_view);
4834 watermark_view=DestroyCacheView(watermark_view);
cristyda16f162011-02-19 23:52:17 +00004835 if (status == MagickFalse)
4836 {
4837 stegano_image=DestroyImage(stegano_image);
4838 return((Image *) NULL);
4839 }
cristy3ed852e2009-09-05 21:47:34 +00004840 return(stegano_image);
4841}
4842
4843/*
4844%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4845% %
4846% %
4847% %
4848% S t e r e o A n a g l y p h I m a g e %
4849% %
4850% %
4851% %
4852%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4853%
4854% StereoAnaglyphImage() combines two images and produces a single image that
4855% is the composite of a left and right image of a stereo pair. Special
4856% red-green stereo glasses are required to view this effect.
4857%
4858% The format of the StereoAnaglyphImage method is:
4859%
4860% Image *StereoImage(const Image *left_image,const Image *right_image,
4861% ExceptionInfo *exception)
4862% Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004863% const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004864% ExceptionInfo *exception)
4865%
4866% A description of each parameter follows:
4867%
4868% o left_image: the left image.
4869%
4870% o right_image: the right image.
4871%
4872% o exception: return any errors or warnings in this structure.
4873%
4874% o x_offset: amount, in pixels, by which the left image is offset to the
4875% right of the right image.
4876%
4877% o y_offset: amount, in pixels, by which the left image is offset to the
4878% bottom of the right image.
4879%
4880%
4881*/
4882MagickExport Image *StereoImage(const Image *left_image,
4883 const Image *right_image,ExceptionInfo *exception)
4884{
4885 return(StereoAnaglyphImage(left_image,right_image,0,0,exception));
4886}
4887
4888MagickExport Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004889 const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004890 ExceptionInfo *exception)
4891{
4892#define StereoImageTag "Stereo/Image"
4893
4894 const Image
4895 *image;
4896
4897 Image
4898 *stereo_image;
4899
cristy3ed852e2009-09-05 21:47:34 +00004900 MagickBooleanType
4901 status;
4902
cristy9d314ff2011-03-09 01:30:28 +00004903 ssize_t
4904 y;
4905
cristy3ed852e2009-09-05 21:47:34 +00004906 assert(left_image != (const Image *) NULL);
4907 assert(left_image->signature == MagickSignature);
4908 if (left_image->debug != MagickFalse)
4909 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4910 left_image->filename);
4911 assert(right_image != (const Image *) NULL);
4912 assert(right_image->signature == MagickSignature);
4913 assert(exception != (ExceptionInfo *) NULL);
4914 assert(exception->signature == MagickSignature);
4915 assert(right_image != (const Image *) NULL);
4916 image=left_image;
4917 if ((left_image->columns != right_image->columns) ||
4918 (left_image->rows != right_image->rows))
4919 ThrowImageException(ImageError,"LeftAndRightImageSizesDiffer");
4920 /*
4921 Initialize stereo image attributes.
4922 */
4923 stereo_image=CloneImage(left_image,left_image->columns,left_image->rows,
4924 MagickTrue,exception);
4925 if (stereo_image == (Image *) NULL)
4926 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004927 if (SetImageStorageClass(stereo_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004928 {
cristy3ed852e2009-09-05 21:47:34 +00004929 stereo_image=DestroyImage(stereo_image);
4930 return((Image *) NULL);
4931 }
4932 /*
4933 Copy left image to red channel and right image to blue channel.
4934 */
cristyda16f162011-02-19 23:52:17 +00004935 status=MagickTrue;
cristybb503372010-05-27 20:51:26 +00004936 for (y=0; y < (ssize_t) stereo_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004937 {
cristy4c08aed2011-07-01 19:47:50 +00004938 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004939 *restrict p,
4940 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004941
cristybb503372010-05-27 20:51:26 +00004942 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004943 x;
4944
cristy4c08aed2011-07-01 19:47:50 +00004945 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004946 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +00004947
4948 p=GetVirtualPixels(left_image,-x_offset,y-y_offset,image->columns,1,
4949 exception);
4950 q=GetVirtualPixels(right_image,0,y,right_image->columns,1,exception);
4951 r=QueueAuthenticPixels(stereo_image,0,y,stereo_image->columns,1,exception);
cristy76f512e2011-09-12 01:26:56 +00004952 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL) ||
4953 (r == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004954 break;
cristybb503372010-05-27 20:51:26 +00004955 for (x=0; x < (ssize_t) stereo_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004956 {
cristy4c08aed2011-07-01 19:47:50 +00004957 SetPixelRed(image,GetPixelRed(left_image,p),r);
cristy76f512e2011-09-12 01:26:56 +00004958 SetPixelGreen(image,GetPixelGreen(right_image,q),r);
4959 SetPixelBlue(image,GetPixelBlue(right_image,q),r);
4960 if ((GetPixelAlphaTraits(stereo_image) & CopyPixelTrait) != 0)
4961 SetPixelAlpha(image,(GetPixelAlpha(left_image,p)+
4962 GetPixelAlpha(right_image,q))/2,r);
cristyed231572011-07-14 02:18:59 +00004963 p+=GetPixelChannels(left_image);
cristy76f512e2011-09-12 01:26:56 +00004964 q+=GetPixelChannels(right_image);
4965 r+=GetPixelChannels(stereo_image);
cristy3ed852e2009-09-05 21:47:34 +00004966 }
4967 if (SyncAuthenticPixels(stereo_image,exception) == MagickFalse)
4968 break;
cristy8b27a6d2010-02-14 03:31:15 +00004969 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004970 {
cristy8b27a6d2010-02-14 03:31:15 +00004971 MagickBooleanType
4972 proceed;
4973
cristybb503372010-05-27 20:51:26 +00004974 proceed=SetImageProgress(image,StereoImageTag,(MagickOffsetType) y,
4975 stereo_image->rows);
cristy8b27a6d2010-02-14 03:31:15 +00004976 if (proceed == MagickFalse)
4977 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004978 }
4979 }
cristyda16f162011-02-19 23:52:17 +00004980 if (status == MagickFalse)
4981 {
4982 stereo_image=DestroyImage(stereo_image);
4983 return((Image *) NULL);
4984 }
cristy3ed852e2009-09-05 21:47:34 +00004985 return(stereo_image);
4986}
4987
4988/*
4989%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4990% %
4991% %
4992% %
4993% S w i r l I m a g e %
4994% %
4995% %
4996% %
4997%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4998%
4999% SwirlImage() swirls the pixels about the center of the image, where
5000% degrees indicates the sweep of the arc through which each pixel is moved.
5001% You get a more dramatic effect as the degrees move from 1 to 360.
5002%
5003% The format of the SwirlImage method is:
5004%
5005% Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00005006% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005007%
5008% A description of each parameter follows:
5009%
5010% o image: the image.
5011%
5012% o degrees: Define the tightness of the swirling effect.
5013%
cristy76f512e2011-09-12 01:26:56 +00005014% o method: the pixel interpolation method.
5015%
cristy3ed852e2009-09-05 21:47:34 +00005016% o exception: return any errors or warnings in this structure.
5017%
5018*/
5019MagickExport Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00005020 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005021{
5022#define SwirlImageTag "Swirl/Image"
5023
cristyfa112112010-01-04 17:48:07 +00005024 CacheView
5025 *image_view,
5026 *swirl_view;
5027
cristy3ed852e2009-09-05 21:47:34 +00005028 Image
5029 *swirl_image;
5030
cristy3ed852e2009-09-05 21:47:34 +00005031 MagickBooleanType
5032 status;
5033
cristybb503372010-05-27 20:51:26 +00005034 MagickOffsetType
5035 progress;
5036
cristy3ed852e2009-09-05 21:47:34 +00005037 MagickRealType
5038 radius;
5039
5040 PointInfo
5041 center,
5042 scale;
5043
cristybb503372010-05-27 20:51:26 +00005044 ssize_t
5045 y;
5046
cristy3ed852e2009-09-05 21:47:34 +00005047 /*
5048 Initialize swirl image attributes.
5049 */
5050 assert(image != (const Image *) NULL);
5051 assert(image->signature == MagickSignature);
5052 if (image->debug != MagickFalse)
5053 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5054 assert(exception != (ExceptionInfo *) NULL);
5055 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00005056 swirl_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005057 if (swirl_image == (Image *) NULL)
5058 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005059 if (SetImageStorageClass(swirl_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005060 {
cristy3ed852e2009-09-05 21:47:34 +00005061 swirl_image=DestroyImage(swirl_image);
5062 return((Image *) NULL);
5063 }
cristy4c08aed2011-07-01 19:47:50 +00005064 if (swirl_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00005065 swirl_image->matte=MagickTrue;
5066 /*
5067 Compute scaling factor.
5068 */
5069 center.x=(double) image->columns/2.0;
5070 center.y=(double) image->rows/2.0;
5071 radius=MagickMax(center.x,center.y);
5072 scale.x=1.0;
5073 scale.y=1.0;
5074 if (image->columns > image->rows)
5075 scale.y=(double) image->columns/(double) image->rows;
5076 else
5077 if (image->columns < image->rows)
5078 scale.x=(double) image->rows/(double) image->columns;
5079 degrees=(double) DegreesToRadians(degrees);
5080 /*
5081 Swirl image.
5082 */
5083 status=MagickTrue;
5084 progress=0;
cristydb070952012-04-20 14:33:00 +00005085 image_view=AcquireVirtualCacheView(image,exception);
5086 swirl_view=AcquireAuthenticCacheView(swirl_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00005087#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005088 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00005089#endif
cristybb503372010-05-27 20:51:26 +00005090 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005091 {
cristy3ed852e2009-09-05 21:47:34 +00005092 MagickRealType
5093 distance;
5094
5095 PointInfo
5096 delta;
5097
cristy6d188022011-09-12 13:23:33 +00005098 register const Quantum
5099 *restrict p;
5100
cristybb503372010-05-27 20:51:26 +00005101 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005102 x;
5103
cristy4c08aed2011-07-01 19:47:50 +00005104 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005105 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005106
5107 if (status == MagickFalse)
5108 continue;
cristy6d188022011-09-12 13:23:33 +00005109 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy1707c6c2012-01-18 23:30:54 +00005110 q=QueueCacheViewAuthenticPixels(swirl_view,0,y,swirl_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00005111 exception);
cristy6d188022011-09-12 13:23:33 +00005112 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005113 {
5114 status=MagickFalse;
5115 continue;
5116 }
cristy3ed852e2009-09-05 21:47:34 +00005117 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00005118 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005119 {
5120 /*
5121 Determine if the pixel is within an ellipse.
5122 */
cristy10a6c612012-01-29 21:41:05 +00005123 if (GetPixelMask(image,p) != 0)
5124 {
5125 p+=GetPixelChannels(image);
5126 q+=GetPixelChannels(swirl_image);
5127 continue;
5128 }
cristy3ed852e2009-09-05 21:47:34 +00005129 delta.x=scale.x*(double) (x-center.x);
5130 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00005131 if (distance >= (radius*radius))
5132 {
cristy1707c6c2012-01-18 23:30:54 +00005133 register ssize_t
5134 i;
5135
cristy6d188022011-09-12 13:23:33 +00005136 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy1707c6c2012-01-18 23:30:54 +00005137 {
5138 PixelChannel
5139 channel;
5140
5141 PixelTrait
5142 swirl_traits,
5143 traits;
5144
5145 channel=GetPixelChannelMapChannel(image,i);
5146 traits=GetPixelChannelMapTraits(image,channel);
5147 swirl_traits=GetPixelChannelMapTraits(swirl_image,channel);
5148 if ((traits == UndefinedPixelTrait) ||
5149 (swirl_traits == UndefinedPixelTrait))
5150 continue;
5151 SetPixelChannel(swirl_image,channel,p[i],q);
5152 }
cristy6d188022011-09-12 13:23:33 +00005153 }
5154 else
cristy3ed852e2009-09-05 21:47:34 +00005155 {
5156 MagickRealType
5157 cosine,
5158 factor,
5159 sine;
5160
5161 /*
5162 Swirl the pixel.
5163 */
5164 factor=1.0-sqrt((double) distance)/radius;
5165 sine=sin((double) (degrees*factor*factor));
5166 cosine=cos((double) (degrees*factor*factor));
cristy76f512e2011-09-12 01:26:56 +00005167 status=InterpolatePixelChannels(image,image_view,swirl_image,method,
5168 ((cosine*delta.x-sine*delta.y)/scale.x+center.x),(double)
5169 ((sine*delta.x+cosine*delta.y)/scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00005170 }
cristy6d188022011-09-12 13:23:33 +00005171 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00005172 q+=GetPixelChannels(swirl_image);
cristy3ed852e2009-09-05 21:47:34 +00005173 }
5174 if (SyncCacheViewAuthenticPixels(swirl_view,exception) == MagickFalse)
5175 status=MagickFalse;
5176 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5177 {
5178 MagickBooleanType
5179 proceed;
5180
cristyb5d5f722009-11-04 03:03:49 +00005181#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005182 #pragma omp critical (MagickCore_SwirlImage)
cristy3ed852e2009-09-05 21:47:34 +00005183#endif
5184 proceed=SetImageProgress(image,SwirlImageTag,progress++,image->rows);
5185 if (proceed == MagickFalse)
5186 status=MagickFalse;
5187 }
5188 }
5189 swirl_view=DestroyCacheView(swirl_view);
5190 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00005191 if (status == MagickFalse)
5192 swirl_image=DestroyImage(swirl_image);
5193 return(swirl_image);
5194}
5195
5196/*
5197%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5198% %
5199% %
5200% %
5201% T i n t I m a g e %
5202% %
5203% %
5204% %
5205%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5206%
5207% TintImage() applies a color vector to each pixel in the image. The length
5208% of the vector is 0 for black and white and at its maximum for the midtones.
5209% The vector weighting function is f(x)=(1-(4.0*((x-0.5)*(x-0.5))))
5210%
5211% The format of the TintImage method is:
5212%
cristyb817c3f2011-10-03 14:00:35 +00005213% Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005214% const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005215%
5216% A description of each parameter follows:
5217%
5218% o image: the image.
5219%
cristyb817c3f2011-10-03 14:00:35 +00005220% o blend: A color value used for tinting.
cristy3ed852e2009-09-05 21:47:34 +00005221%
5222% o tint: A color value used for tinting.
5223%
5224% o exception: return any errors or warnings in this structure.
5225%
5226*/
cristyb817c3f2011-10-03 14:00:35 +00005227MagickExport Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005228 const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005229{
5230#define TintImageTag "Tint/Image"
5231
cristyc4c8d132010-01-07 01:58:38 +00005232 CacheView
5233 *image_view,
5234 *tint_view;
5235
cristy3ed852e2009-09-05 21:47:34 +00005236 GeometryInfo
5237 geometry_info;
5238
5239 Image
5240 *tint_image;
5241
cristy3ed852e2009-09-05 21:47:34 +00005242 MagickBooleanType
5243 status;
5244
cristybb503372010-05-27 20:51:26 +00005245 MagickOffsetType
5246 progress;
cristy3ed852e2009-09-05 21:47:34 +00005247
cristy28474bf2011-09-11 23:32:52 +00005248 MagickRealType
5249 intensity;
5250
cristy4c08aed2011-07-01 19:47:50 +00005251 PixelInfo
cristy1707c6c2012-01-18 23:30:54 +00005252 color_vector;
cristy3ed852e2009-09-05 21:47:34 +00005253
cristybb503372010-05-27 20:51:26 +00005254 MagickStatusType
5255 flags;
5256
5257 ssize_t
5258 y;
5259
cristy3ed852e2009-09-05 21:47:34 +00005260 /*
5261 Allocate tint image.
5262 */
5263 assert(image != (const Image *) NULL);
5264 assert(image->signature == MagickSignature);
5265 if (image->debug != MagickFalse)
5266 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5267 assert(exception != (ExceptionInfo *) NULL);
5268 assert(exception->signature == MagickSignature);
5269 tint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5270 if (tint_image == (Image *) NULL)
5271 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005272 if (SetImageStorageClass(tint_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005273 {
cristy3ed852e2009-09-05 21:47:34 +00005274 tint_image=DestroyImage(tint_image);
5275 return((Image *) NULL);
5276 }
cristyaed9c382011-10-03 17:54:21 +00005277 if (blend == (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005278 return(tint_image);
5279 /*
5280 Determine RGB values of the color.
5281 */
cristy1707c6c2012-01-18 23:30:54 +00005282 GetPixelInfo(image,&color_vector);
cristyb817c3f2011-10-03 14:00:35 +00005283 flags=ParseGeometry(blend,&geometry_info);
cristy1707c6c2012-01-18 23:30:54 +00005284 color_vector.red=geometry_info.rho;
5285 color_vector.green=geometry_info.rho;
5286 color_vector.blue=geometry_info.rho;
5287 color_vector.alpha=OpaqueAlpha;
cristy3ed852e2009-09-05 21:47:34 +00005288 if ((flags & SigmaValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005289 color_vector.green=geometry_info.sigma;
cristy3ed852e2009-09-05 21:47:34 +00005290 if ((flags & XiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005291 color_vector.blue=geometry_info.xi;
cristyb817c3f2011-10-03 14:00:35 +00005292 if ((flags & PsiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005293 color_vector.alpha=geometry_info.psi;
cristy76f512e2011-09-12 01:26:56 +00005294 if (image->colorspace == CMYKColorspace)
5295 {
cristy1707c6c2012-01-18 23:30:54 +00005296 color_vector.black=geometry_info.rho;
cristy76f512e2011-09-12 01:26:56 +00005297 if ((flags & PsiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005298 color_vector.black=geometry_info.psi;
cristy76f512e2011-09-12 01:26:56 +00005299 if ((flags & ChiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005300 color_vector.alpha=geometry_info.chi;
cristy76f512e2011-09-12 01:26:56 +00005301 }
cristy28474bf2011-09-11 23:32:52 +00005302 intensity=(MagickRealType) GetPixelInfoIntensity(tint);
cristy1707c6c2012-01-18 23:30:54 +00005303 color_vector.red=(MagickRealType) (color_vector.red*tint->red/100.0-
5304 intensity);
5305 color_vector.green=(MagickRealType) (color_vector.green*tint->green/100.0-
5306 intensity);
5307 color_vector.blue=(MagickRealType) (color_vector.blue*tint->blue/100.0-
5308 intensity);
5309 color_vector.black=(MagickRealType) (color_vector.black*tint->black/100.0-
5310 intensity);
5311 color_vector.alpha=(MagickRealType) (color_vector.alpha*tint->alpha/100.0-
5312 intensity);
cristy3ed852e2009-09-05 21:47:34 +00005313 /*
5314 Tint image.
5315 */
5316 status=MagickTrue;
5317 progress=0;
cristydb070952012-04-20 14:33:00 +00005318 image_view=AcquireVirtualCacheView(image,exception);
5319 tint_view=AcquireAuthenticCacheView(tint_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00005320#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00005321 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00005322#endif
cristybb503372010-05-27 20:51:26 +00005323 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005324 {
cristy4c08aed2011-07-01 19:47:50 +00005325 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00005326 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00005327
cristy4c08aed2011-07-01 19:47:50 +00005328 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005329 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005330
cristy6b91acb2011-04-19 12:23:54 +00005331 register ssize_t
5332 x;
5333
cristy3ed852e2009-09-05 21:47:34 +00005334 if (status == MagickFalse)
5335 continue;
5336 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5337 q=QueueCacheViewAuthenticPixels(tint_view,0,y,tint_image->columns,1,
5338 exception);
cristy4c08aed2011-07-01 19:47:50 +00005339 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005340 {
5341 status=MagickFalse;
5342 continue;
5343 }
cristybb503372010-05-27 20:51:26 +00005344 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005345 {
cristy4c08aed2011-07-01 19:47:50 +00005346 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00005347 pixel;
5348
5349 MagickRealType
5350 weight;
5351
cristy1707c6c2012-01-18 23:30:54 +00005352 register ssize_t
5353 i;
5354
cristy10a6c612012-01-29 21:41:05 +00005355 if (GetPixelMask(image,p) != 0)
5356 {
5357 p+=GetPixelChannels(image);
5358 q+=GetPixelChannels(tint_image);
5359 continue;
5360 }
cristy1707c6c2012-01-18 23:30:54 +00005361 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5362 {
5363 PixelChannel
5364 channel;
5365
5366 PixelTrait
5367 tint_traits,
5368 traits;
5369
5370 channel=GetPixelChannelMapChannel(image,i);
5371 traits=GetPixelChannelMapTraits(image,channel);
5372 tint_traits=GetPixelChannelMapTraits(tint_image,channel);
5373 if ((traits == UndefinedPixelTrait) ||
5374 (tint_traits == UndefinedPixelTrait))
5375 continue;
5376 if ((tint_traits & CopyPixelTrait) != 0)
5377 {
5378 SetPixelChannel(tint_image,channel,p[i],q);
5379 continue;
5380 }
5381 }
5382 GetPixelInfo(image,&pixel);
5383 weight=QuantumScale*GetPixelRed(image,p)-0.5;
5384 pixel.red=(MagickRealType) GetPixelRed(image,p)+color_vector.red*
5385 (1.0-(4.0*(weight*weight)));
5386 weight=QuantumScale*GetPixelGreen(image,p)-0.5;
5387 pixel.green=(MagickRealType) GetPixelGreen(image,p)+color_vector.green*
5388 (1.0-(4.0*(weight*weight)));
5389 weight=QuantumScale*GetPixelBlue(image,p)-0.5;
5390 pixel.blue=(MagickRealType) GetPixelBlue(image,p)+color_vector.blue*
5391 (1.0-(4.0*(weight*weight)));
5392 weight=QuantumScale*GetPixelBlack(image,p)-0.5;
5393 pixel.black=(MagickRealType) GetPixelBlack(image,p)+color_vector.black*
5394 (1.0-(4.0*(weight*weight)));
5395 SetPixelInfoPixel(tint_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +00005396 p+=GetPixelChannels(image);
5397 q+=GetPixelChannels(tint_image);
cristy3ed852e2009-09-05 21:47:34 +00005398 }
5399 if (SyncCacheViewAuthenticPixels(tint_view,exception) == MagickFalse)
5400 status=MagickFalse;
5401 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5402 {
5403 MagickBooleanType
5404 proceed;
5405
cristyb5d5f722009-11-04 03:03:49 +00005406#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005407 #pragma omp critical (MagickCore_TintImage)
cristy3ed852e2009-09-05 21:47:34 +00005408#endif
5409 proceed=SetImageProgress(image,TintImageTag,progress++,image->rows);
5410 if (proceed == MagickFalse)
5411 status=MagickFalse;
5412 }
5413 }
5414 tint_view=DestroyCacheView(tint_view);
5415 image_view=DestroyCacheView(image_view);
5416 if (status == MagickFalse)
5417 tint_image=DestroyImage(tint_image);
5418 return(tint_image);
5419}
5420
5421/*
5422%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5423% %
5424% %
5425% %
5426% V i g n e t t e I m a g e %
5427% %
5428% %
5429% %
5430%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5431%
5432% VignetteImage() softens the edges of the image in vignette style.
5433%
5434% The format of the VignetteImage method is:
5435%
5436% Image *VignetteImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00005437% const double sigma,const ssize_t x,const ssize_t y,
cristy05c0c9a2011-09-05 23:16:13 +00005438% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005439%
5440% A description of each parameter follows:
5441%
5442% o image: the image.
5443%
5444% o radius: the radius of the pixel neighborhood.
5445%
5446% o sigma: the standard deviation of the Gaussian, in pixels.
5447%
5448% o x, y: Define the x and y ellipse offset.
5449%
5450% o exception: return any errors or warnings in this structure.
5451%
5452*/
5453MagickExport Image *VignetteImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00005454 const double sigma,const ssize_t x,const ssize_t y,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005455{
5456 char
5457 ellipse[MaxTextExtent];
5458
5459 DrawInfo
5460 *draw_info;
5461
5462 Image
5463 *canvas_image,
5464 *blur_image,
5465 *oval_image,
5466 *vignette_image;
5467
5468 assert(image != (Image *) NULL);
5469 assert(image->signature == MagickSignature);
5470 if (image->debug != MagickFalse)
5471 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5472 assert(exception != (ExceptionInfo *) NULL);
5473 assert(exception->signature == MagickSignature);
5474 canvas_image=CloneImage(image,0,0,MagickTrue,exception);
5475 if (canvas_image == (Image *) NULL)
5476 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005477 if (SetImageStorageClass(canvas_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005478 {
cristy3ed852e2009-09-05 21:47:34 +00005479 canvas_image=DestroyImage(canvas_image);
5480 return((Image *) NULL);
5481 }
5482 canvas_image->matte=MagickTrue;
cristy98621462011-12-31 22:31:11 +00005483 oval_image=CloneImage(canvas_image,canvas_image->columns,canvas_image->rows,
5484 MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005485 if (oval_image == (Image *) NULL)
5486 {
5487 canvas_image=DestroyImage(canvas_image);
5488 return((Image *) NULL);
5489 }
cristy9950d572011-10-01 18:22:35 +00005490 (void) QueryColorCompliance("#000000",AllCompliance,
5491 &oval_image->background_color,exception);
cristyea1a8aa2011-10-20 13:24:06 +00005492 (void) SetImageBackgroundColor(oval_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00005493 draw_info=CloneDrawInfo((const ImageInfo *) NULL,(const DrawInfo *) NULL);
cristy9950d572011-10-01 18:22:35 +00005494 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->fill,
5495 exception);
5496 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->stroke,
5497 exception);
cristy1707c6c2012-01-18 23:30:54 +00005498 (void) FormatLocaleString(ellipse,MaxTextExtent,"ellipse %g,%g,%g,%g,"
5499 "0.0,360.0",image->columns/2.0,image->rows/2.0,image->columns/2.0-x,
5500 image->rows/2.0-y);
cristy3ed852e2009-09-05 21:47:34 +00005501 draw_info->primitive=AcquireString(ellipse);
cristy018f07f2011-09-04 21:15:19 +00005502 (void) DrawImage(oval_image,draw_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00005503 draw_info=DestroyDrawInfo(draw_info);
cristyaa2c16c2012-03-25 22:21:35 +00005504 blur_image=BlurImage(oval_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +00005505 oval_image=DestroyImage(oval_image);
5506 if (blur_image == (Image *) NULL)
5507 {
5508 canvas_image=DestroyImage(canvas_image);
5509 return((Image *) NULL);
5510 }
5511 blur_image->matte=MagickFalse;
cristy39172402012-03-30 13:04:39 +00005512 (void) CompositeImage(canvas_image,blur_image,IntensityCompositeOp,MagickTrue,
5513 0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00005514 blur_image=DestroyImage(blur_image);
5515 vignette_image=MergeImageLayers(canvas_image,FlattenLayer,exception);
5516 canvas_image=DestroyImage(canvas_image);
5517 return(vignette_image);
5518}
5519
5520/*
5521%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5522% %
5523% %
5524% %
5525% W a v e I m a g e %
5526% %
5527% %
5528% %
5529%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5530%
5531% WaveImage() creates a "ripple" effect in the image by shifting the pixels
cristycee97112010-05-28 00:44:52 +00005532% vertically along a sine wave whose amplitude and wavelength is specified
cristy3ed852e2009-09-05 21:47:34 +00005533% by the given parameters.
5534%
5535% The format of the WaveImage method is:
5536%
5537% Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005538% const double wave_length,const PixelInterpolateMethod method,
5539% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005540%
5541% A description of each parameter follows:
5542%
5543% o image: the image.
5544%
5545% o amplitude, wave_length: Define the amplitude and wave length of the
5546% sine wave.
5547%
cristy5c4e2582011-09-11 19:21:03 +00005548% o interpolate: the pixel interpolation method.
5549%
cristy3ed852e2009-09-05 21:47:34 +00005550% o exception: return any errors or warnings in this structure.
5551%
5552*/
5553MagickExport Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005554 const double wave_length,const PixelInterpolateMethod method,
5555 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005556{
5557#define WaveImageTag "Wave/Image"
5558
cristyfa112112010-01-04 17:48:07 +00005559 CacheView
cristyd76c51e2011-03-26 00:21:26 +00005560 *image_view,
cristyfa112112010-01-04 17:48:07 +00005561 *wave_view;
5562
cristy3ed852e2009-09-05 21:47:34 +00005563 Image
5564 *wave_image;
5565
cristy3ed852e2009-09-05 21:47:34 +00005566 MagickBooleanType
5567 status;
5568
cristybb503372010-05-27 20:51:26 +00005569 MagickOffsetType
5570 progress;
5571
cristy3ed852e2009-09-05 21:47:34 +00005572 MagickRealType
5573 *sine_map;
5574
cristybb503372010-05-27 20:51:26 +00005575 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005576 i;
5577
cristybb503372010-05-27 20:51:26 +00005578 ssize_t
5579 y;
5580
cristy3ed852e2009-09-05 21:47:34 +00005581 /*
5582 Initialize wave image attributes.
5583 */
5584 assert(image != (Image *) NULL);
5585 assert(image->signature == MagickSignature);
5586 if (image->debug != MagickFalse)
5587 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5588 assert(exception != (ExceptionInfo *) NULL);
5589 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00005590 wave_image=CloneImage(image,image->columns,(size_t) (image->rows+2.0*
cristy3ed852e2009-09-05 21:47:34 +00005591 fabs(amplitude)),MagickTrue,exception);
5592 if (wave_image == (Image *) NULL)
5593 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005594 if (SetImageStorageClass(wave_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005595 {
cristy3ed852e2009-09-05 21:47:34 +00005596 wave_image=DestroyImage(wave_image);
5597 return((Image *) NULL);
5598 }
cristy4c08aed2011-07-01 19:47:50 +00005599 if (wave_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00005600 wave_image->matte=MagickTrue;
5601 /*
5602 Allocate sine map.
5603 */
5604 sine_map=(MagickRealType *) AcquireQuantumMemory((size_t) wave_image->columns,
5605 sizeof(*sine_map));
5606 if (sine_map == (MagickRealType *) NULL)
5607 {
5608 wave_image=DestroyImage(wave_image);
5609 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
5610 }
cristybb503372010-05-27 20:51:26 +00005611 for (i=0; i < (ssize_t) wave_image->columns; i++)
cristy4205a3c2010-09-12 20:19:59 +00005612 sine_map[i]=fabs(amplitude)+amplitude*sin((double) ((2.0*MagickPI*i)/
5613 wave_length));
cristy3ed852e2009-09-05 21:47:34 +00005614 /*
5615 Wave image.
5616 */
5617 status=MagickTrue;
5618 progress=0;
cristydb070952012-04-20 14:33:00 +00005619 image_view=AcquireVirtualCacheView(image,exception);
5620 wave_view=AcquireAuthenticCacheView(wave_image,exception);
cristyd76c51e2011-03-26 00:21:26 +00005621 (void) SetCacheViewVirtualPixelMethod(image_view,
5622 BackgroundVirtualPixelMethod);
cristyb5d5f722009-11-04 03:03:49 +00005623#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00005624 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00005625#endif
cristybb503372010-05-27 20:51:26 +00005626 for (y=0; y < (ssize_t) wave_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005627 {
cristy4c08aed2011-07-01 19:47:50 +00005628 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005629 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005630
cristye97bb922011-04-03 01:36:52 +00005631 register ssize_t
5632 x;
5633
cristy3ed852e2009-09-05 21:47:34 +00005634 if (status == MagickFalse)
5635 continue;
5636 q=QueueCacheViewAuthenticPixels(wave_view,0,y,wave_image->columns,1,
5637 exception);
cristyacd2ed22011-08-30 01:44:23 +00005638 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005639 {
5640 status=MagickFalse;
5641 continue;
5642 }
cristybb503372010-05-27 20:51:26 +00005643 for (x=0; x < (ssize_t) wave_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005644 {
cristy5c4e2582011-09-11 19:21:03 +00005645 status=InterpolatePixelChannels(image,image_view,wave_image,method,
5646 (double) x,(double) (y-sine_map[x]),q,exception);
cristyed231572011-07-14 02:18:59 +00005647 q+=GetPixelChannels(wave_image);
cristy3ed852e2009-09-05 21:47:34 +00005648 }
5649 if (SyncCacheViewAuthenticPixels(wave_view,exception) == MagickFalse)
5650 status=MagickFalse;
5651 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5652 {
5653 MagickBooleanType
5654 proceed;
5655
cristyb5d5f722009-11-04 03:03:49 +00005656#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005657 #pragma omp critical (MagickCore_WaveImage)
cristy3ed852e2009-09-05 21:47:34 +00005658#endif
5659 proceed=SetImageProgress(image,WaveImageTag,progress++,image->rows);
5660 if (proceed == MagickFalse)
5661 status=MagickFalse;
5662 }
5663 }
5664 wave_view=DestroyCacheView(wave_view);
cristyd76c51e2011-03-26 00:21:26 +00005665 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00005666 sine_map=(MagickRealType *) RelinquishMagickMemory(sine_map);
5667 if (status == MagickFalse)
5668 wave_image=DestroyImage(wave_image);
5669 return(wave_image);
5670}