blob: 7a98f54045f8125ff3caa1c4ad1ae618eb6057a9 [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) \
cristy4ee2b0c2012-05-15 00:30:35 +0000315 dynamic_number_threads(image,image->columns,image->rows,key == ~0UL)
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)
cristyac245f82012-05-05 17:13:57 +0000481 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +0000482 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +0000483#endif
cristybb503372010-05-27 20:51:26 +0000484 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000485 {
486 MagickBooleanType
487 sync;
488
cristy4c08aed2011-07-01 19:47:50 +0000489 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000490 pixel;
491
492 Quantum
493 quantum;
494
cristy4c08aed2011-07-01 19:47:50 +0000495 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000496 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000497
cristybb503372010-05-27 20:51:26 +0000498 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000499 x;
500
cristy4c08aed2011-07-01 19:47:50 +0000501 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000502 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000503
504 if (status == MagickFalse)
505 continue;
506 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
507 q=QueueCacheViewAuthenticPixels(shift_view,0,y,shift_image->columns,1,
508 exception);
cristy4c08aed2011-07-01 19:47:50 +0000509 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000510 {
511 status=MagickFalse;
512 continue;
513 }
cristybb503372010-05-27 20:51:26 +0000514 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000515 {
cristy4c08aed2011-07-01 19:47:50 +0000516 quantum=GetPixelRed(image,p);
517 if (GetPixelGreen(image,p) < quantum)
518 quantum=GetPixelGreen(image,p);
519 if (GetPixelBlue(image,p) < quantum)
520 quantum=GetPixelBlue(image,p);
521 pixel.red=0.5*(GetPixelRed(image,p)+factor*quantum);
522 pixel.green=0.5*(GetPixelGreen(image,p)+factor*quantum);
523 pixel.blue=0.5*(GetPixelBlue(image,p)+factor*quantum);
524 quantum=GetPixelRed(image,p);
525 if (GetPixelGreen(image,p) > quantum)
526 quantum=GetPixelGreen(image,p);
527 if (GetPixelBlue(image,p) > quantum)
528 quantum=GetPixelBlue(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000529 pixel.red=0.5*(pixel.red+factor*quantum);
530 pixel.green=0.5*(pixel.green+factor*quantum);
531 pixel.blue=0.5*(pixel.blue+factor*quantum);
cristy4c08aed2011-07-01 19:47:50 +0000532 SetPixelRed(shift_image,ClampToQuantum(pixel.red),q);
533 SetPixelGreen(shift_image,ClampToQuantum(pixel.green),q);
534 SetPixelBlue(shift_image,ClampToQuantum(pixel.blue),q);
cristyed231572011-07-14 02:18:59 +0000535 p+=GetPixelChannels(image);
536 q+=GetPixelChannels(shift_image);
cristy3ed852e2009-09-05 21:47:34 +0000537 }
538 sync=SyncCacheViewAuthenticPixels(shift_view,exception);
539 if (sync == MagickFalse)
540 status=MagickFalse;
541 if (image->progress_monitor != (MagickProgressMonitor) NULL)
542 {
543 MagickBooleanType
544 proceed;
545
cristy319a1e72010-02-21 15:13:11 +0000546#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +0000547 #pragma omp critical (MagickCore_BlueShiftImage)
cristy3ed852e2009-09-05 21:47:34 +0000548#endif
549 proceed=SetImageProgress(image,BlueShiftImageTag,progress++,
550 image->rows);
551 if (proceed == MagickFalse)
552 status=MagickFalse;
553 }
554 }
555 image_view=DestroyCacheView(image_view);
556 shift_view=DestroyCacheView(shift_view);
557 if (status == MagickFalse)
558 shift_image=DestroyImage(shift_image);
559 return(shift_image);
560}
561
562/*
563%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
564% %
565% %
566% %
567% C h a r c o a l I m a g e %
568% %
569% %
570% %
571%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
572%
573% CharcoalImage() creates a new image that is a copy of an existing one with
574% the edge highlighted. It allocates the memory necessary for the new Image
575% structure and returns a pointer to the new image.
576%
577% The format of the CharcoalImage method is:
578%
579% Image *CharcoalImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +0000580% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000581%
582% A description of each parameter follows:
583%
584% o image: the image.
585%
586% o radius: the radius of the pixel neighborhood.
587%
588% o sigma: the standard deviation of the Gaussian, in pixels.
589%
590% o exception: return any errors or warnings in this structure.
591%
592*/
593MagickExport Image *CharcoalImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +0000594 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000595{
596 Image
597 *charcoal_image,
598 *clone_image,
599 *edge_image;
600
601 assert(image != (Image *) NULL);
602 assert(image->signature == MagickSignature);
603 if (image->debug != MagickFalse)
604 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
605 assert(exception != (ExceptionInfo *) NULL);
606 assert(exception->signature == MagickSignature);
607 clone_image=CloneImage(image,0,0,MagickTrue,exception);
608 if (clone_image == (Image *) NULL)
609 return((Image *) NULL);
cristy018f07f2011-09-04 21:15:19 +0000610 (void) SetImageType(clone_image,GrayscaleType,exception);
cristy8ae632d2011-09-05 17:29:53 +0000611 edge_image=EdgeImage(clone_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +0000612 clone_image=DestroyImage(clone_image);
613 if (edge_image == (Image *) NULL)
614 return((Image *) NULL);
cristyaa2c16c2012-03-25 22:21:35 +0000615 charcoal_image=BlurImage(edge_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +0000616 edge_image=DestroyImage(edge_image);
617 if (charcoal_image == (Image *) NULL)
618 return((Image *) NULL);
cristye23ec9d2011-08-16 18:15:40 +0000619 (void) NormalizeImage(charcoal_image,exception);
cristyb3e7c6c2011-07-24 01:43:55 +0000620 (void) NegateImage(charcoal_image,MagickFalse,exception);
cristy018f07f2011-09-04 21:15:19 +0000621 (void) SetImageType(charcoal_image,GrayscaleType,exception);
cristy3ed852e2009-09-05 21:47:34 +0000622 return(charcoal_image);
623}
624
625/*
626%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
627% %
628% %
629% %
630% C o l o r i z e I m a g e %
631% %
632% %
633% %
634%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
635%
636% ColorizeImage() blends the fill color with each pixel in the image.
637% A percentage blend is specified with opacity. Control the application
638% of different color components by specifying a different percentage for
639% each component (e.g. 90/100/10 is 90% red, 100% green, and 10% blue).
640%
641% The format of the ColorizeImage method is:
642%
cristyc7e6ff62011-10-03 13:46:11 +0000643% Image *ColorizeImage(const Image *image,const char *blend,
644% const PixelInfo *colorize,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000645%
646% A description of each parameter follows:
647%
648% o image: the image.
649%
cristyc7e6ff62011-10-03 13:46:11 +0000650% o blend: A character string indicating the level of blending as a
cristy3ed852e2009-09-05 21:47:34 +0000651% percentage.
652%
653% o colorize: A color value.
654%
655% o exception: return any errors or warnings in this structure.
656%
657*/
cristyc7e6ff62011-10-03 13:46:11 +0000658MagickExport Image *ColorizeImage(const Image *image,const char *blend,
659 const PixelInfo *colorize,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000660{
661#define ColorizeImageTag "Colorize/Image"
cristy20c3aed2012-04-10 01:06:21 +0000662#define Colorize(pixel,blend_percentage,colorize) \
663 (pixel)=((pixel)*(100.0-(blend_percentage))+(colorize)*(blend_percentage))/100.0;
cristy3ed852e2009-09-05 21:47:34 +0000664
cristyc4c8d132010-01-07 01:58:38 +0000665 CacheView
666 *colorize_view,
667 *image_view;
668
cristy3ed852e2009-09-05 21:47:34 +0000669 GeometryInfo
670 geometry_info;
671
672 Image
673 *colorize_image;
674
cristy3ed852e2009-09-05 21:47:34 +0000675 MagickBooleanType
676 status;
677
cristybb503372010-05-27 20:51:26 +0000678 MagickOffsetType
679 progress;
680
cristy3ed852e2009-09-05 21:47:34 +0000681 MagickStatusType
682 flags;
683
cristyc7e6ff62011-10-03 13:46:11 +0000684 PixelInfo
cristy20c3aed2012-04-10 01:06:21 +0000685 blend_percentage;
cristyc7e6ff62011-10-03 13:46:11 +0000686
cristybb503372010-05-27 20:51:26 +0000687 ssize_t
688 y;
689
cristy3ed852e2009-09-05 21:47:34 +0000690 /*
691 Allocate colorized image.
692 */
693 assert(image != (const Image *) NULL);
694 assert(image->signature == MagickSignature);
695 if (image->debug != MagickFalse)
696 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
697 assert(exception != (ExceptionInfo *) NULL);
698 assert(exception->signature == MagickSignature);
cristy768165d2012-04-09 15:01:35 +0000699 colorize_image=CloneImage(image,image->columns,image->rows,MagickTrue,
700 exception);
701 if (colorize_image == (Image *) NULL)
702 return((Image *) NULL);
703 if (SetImageStorageClass(colorize_image,DirectClass,exception) == MagickFalse)
704 {
705 colorize_image=DestroyImage(colorize_image);
706 return((Image *) NULL);
707 }
cristy9b7a4fc2012-04-08 22:26:56 +0000708 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
cristy20c3aed2012-04-10 01:06:21 +0000709 (IsPixelInfoGray(colorize) != MagickFalse))
cristy52f2e1e2012-04-10 00:51:19 +0000710 (void) SetImageColorspace(colorize_image,sRGBColorspace,exception);
cristy20c3aed2012-04-10 01:06:21 +0000711 if ((colorize_image->matte == MagickFalse) &&
712 (colorize->matte != MagickFalse))
cristy768165d2012-04-09 15:01:35 +0000713 (void) SetImageAlpha(colorize_image,OpaqueAlpha,exception);
714 if (blend == (const char *) NULL)
715 return(colorize_image);
cristy20c3aed2012-04-10 01:06:21 +0000716 GetPixelInfo(image,&blend_percentage);
717 flags=ParseGeometry(blend,&geometry_info);
718 blend_percentage.red=geometry_info.rho;
719 blend_percentage.green=geometry_info.rho;
720 blend_percentage.blue=geometry_info.rho;
721 blend_percentage.black=geometry_info.rho;
cristy3ee7de52012-04-14 23:40:47 +0000722 blend_percentage.alpha=geometry_info.rho;
cristy20c3aed2012-04-10 01:06:21 +0000723 if ((flags & SigmaValue) != 0)
724 blend_percentage.green=geometry_info.sigma;
725 if ((flags & XiValue) != 0)
726 blend_percentage.blue=geometry_info.xi;
727 if ((flags & PsiValue) != 0)
728 blend_percentage.alpha=geometry_info.psi;
729 if (blend_percentage.colorspace == CMYKColorspace)
730 {
731 if ((flags & PsiValue) != 0)
732 blend_percentage.black=geometry_info.psi;
733 if ((flags & ChiValue) != 0)
734 blend_percentage.alpha=geometry_info.chi;
735 }
cristy3ed852e2009-09-05 21:47:34 +0000736 /*
737 Colorize DirectClass image.
738 */
739 status=MagickTrue;
740 progress=0;
cristydb070952012-04-20 14:33:00 +0000741 image_view=AcquireVirtualCacheView(image,exception);
742 colorize_view=AcquireAuthenticCacheView(colorize_image,exception);
cristy319a1e72010-02-21 15:13:11 +0000743#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000744 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +0000745 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +0000746#endif
cristybb503372010-05-27 20:51:26 +0000747 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000748 {
749 MagickBooleanType
750 sync;
751
cristyf9bf43e2012-04-07 18:13:07 +0000752 PixelInfo
753 pixel;
754
cristy4c08aed2011-07-01 19:47:50 +0000755 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000756 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000757
cristybb503372010-05-27 20:51:26 +0000758 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000759 x;
760
cristy4c08aed2011-07-01 19:47:50 +0000761 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000762 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000763
764 if (status == MagickFalse)
765 continue;
766 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
767 q=QueueCacheViewAuthenticPixels(colorize_view,0,y,colorize_image->columns,1,
768 exception);
cristy4c08aed2011-07-01 19:47:50 +0000769 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000770 {
771 status=MagickFalse;
772 continue;
773 }
cristy66d3e1f2012-04-14 23:21:39 +0000774 GetPixelInfo(colorize_image,&pixel);
cristybb503372010-05-27 20:51:26 +0000775 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000776 {
cristyd6803382012-04-10 01:41:25 +0000777 if (GetPixelMask(colorize_image,q) != 0)
778 {
779 p+=GetPixelChannels(image);
780 q+=GetPixelChannels(colorize_image);
781 continue;
782 }
cristyf9bf43e2012-04-07 18:13:07 +0000783 GetPixelInfoPixel(image,p,&pixel);
cristy20c3aed2012-04-10 01:06:21 +0000784 Colorize(pixel.red,blend_percentage.red,colorize->red);
785 Colorize(pixel.green,blend_percentage.green,colorize->green);
786 Colorize(pixel.blue,blend_percentage.blue,colorize->blue);
787 Colorize(pixel.black,blend_percentage.black,colorize->black);
788 Colorize(pixel.alpha,blend_percentage.alpha,colorize->alpha);
cristyf9bf43e2012-04-07 18:13:07 +0000789 SetPixelInfoPixel(colorize_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +0000790 p+=GetPixelChannels(image);
791 q+=GetPixelChannels(colorize_image);
cristy3ed852e2009-09-05 21:47:34 +0000792 }
793 sync=SyncCacheViewAuthenticPixels(colorize_view,exception);
794 if (sync == MagickFalse)
795 status=MagickFalse;
796 if (image->progress_monitor != (MagickProgressMonitor) NULL)
797 {
798 MagickBooleanType
799 proceed;
800
cristy319a1e72010-02-21 15:13:11 +0000801#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +0000802 #pragma omp critical (MagickCore_ColorizeImage)
cristy3ed852e2009-09-05 21:47:34 +0000803#endif
804 proceed=SetImageProgress(image,ColorizeImageTag,progress++,image->rows);
805 if (proceed == MagickFalse)
806 status=MagickFalse;
807 }
808 }
809 image_view=DestroyCacheView(image_view);
810 colorize_view=DestroyCacheView(colorize_view);
811 if (status == MagickFalse)
812 colorize_image=DestroyImage(colorize_image);
813 return(colorize_image);
814}
815
816/*
817%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
818% %
819% %
820% %
cristye6365592010-04-02 17:31:23 +0000821% C o l o r M a t r i x I m a g e %
822% %
823% %
824% %
825%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
826%
827% ColorMatrixImage() applies color transformation to an image. This method
828% permits saturation changes, hue rotation, luminance to alpha, and various
829% other effects. Although variable-sized transformation matrices can be used,
830% typically one uses a 5x5 matrix for an RGBA image and a 6x6 for CMYKA
831% (or RGBA with offsets). The matrix is similar to those used by Adobe Flash
832% except offsets are in column 6 rather than 5 (in support of CMYKA images)
833% and offsets are normalized (divide Flash offset by 255).
834%
835% The format of the ColorMatrixImage method is:
836%
837% Image *ColorMatrixImage(const Image *image,
838% const KernelInfo *color_matrix,ExceptionInfo *exception)
839%
840% A description of each parameter follows:
841%
842% o image: the image.
843%
844% o color_matrix: the color matrix.
845%
846% o exception: return any errors or warnings in this structure.
847%
848*/
anthonyfd706f92012-01-19 04:22:02 +0000849/* FUTURE: modify to make use of a MagickMatrix Mutliply function
850 That should be provided in "matrix.c"
851 (ASIDE: actually distorts should do this too but currently doesn't)
852*/
853
cristye6365592010-04-02 17:31:23 +0000854MagickExport Image *ColorMatrixImage(const Image *image,
855 const KernelInfo *color_matrix,ExceptionInfo *exception)
856{
857#define ColorMatrixImageTag "ColorMatrix/Image"
858
859 CacheView
860 *color_view,
861 *image_view;
862
863 double
864 ColorMatrix[6][6] =
865 {
866 { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
867 { 0.0, 1.0, 0.0, 0.0, 0.0, 0.0 },
868 { 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 },
869 { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0 },
870 { 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 },
871 { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 }
872 };
873
874 Image
875 *color_image;
876
cristye6365592010-04-02 17:31:23 +0000877 MagickBooleanType
878 status;
879
cristybb503372010-05-27 20:51:26 +0000880 MagickOffsetType
881 progress;
882
883 register ssize_t
cristye6365592010-04-02 17:31:23 +0000884 i;
885
cristybb503372010-05-27 20:51:26 +0000886 ssize_t
887 u,
888 v,
889 y;
890
cristye6365592010-04-02 17:31:23 +0000891 /*
anthonyfd706f92012-01-19 04:22:02 +0000892 Map given color_matrix, into a 6x6 matrix RGBKA and a constant
cristye6365592010-04-02 17:31:23 +0000893 */
894 assert(image != (Image *) NULL);
895 assert(image->signature == MagickSignature);
896 if (image->debug != MagickFalse)
897 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
898 assert(exception != (ExceptionInfo *) NULL);
899 assert(exception->signature == MagickSignature);
900 i=0;
cristybb503372010-05-27 20:51:26 +0000901 for (v=0; v < (ssize_t) color_matrix->height; v++)
902 for (u=0; u < (ssize_t) color_matrix->width; u++)
cristye6365592010-04-02 17:31:23 +0000903 {
904 if ((v < 6) && (u < 6))
905 ColorMatrix[v][u]=color_matrix->values[i];
906 i++;
907 }
908 /*
909 Initialize color image.
910 */
cristy12550e62010-06-07 12:46:40 +0000911 color_image=CloneImage(image,0,0,MagickTrue,exception);
cristye6365592010-04-02 17:31:23 +0000912 if (color_image == (Image *) NULL)
913 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000914 if (SetImageStorageClass(color_image,DirectClass,exception) == MagickFalse)
cristye6365592010-04-02 17:31:23 +0000915 {
cristye6365592010-04-02 17:31:23 +0000916 color_image=DestroyImage(color_image);
917 return((Image *) NULL);
918 }
919 if (image->debug != MagickFalse)
920 {
921 char
922 format[MaxTextExtent],
923 *message;
924
925 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
926 " ColorMatrix image with color matrix:");
927 message=AcquireString("");
928 for (v=0; v < 6; v++)
929 {
930 *message='\0';
cristyb51dff52011-05-19 16:55:47 +0000931 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
cristye6365592010-04-02 17:31:23 +0000932 (void) ConcatenateString(&message,format);
933 for (u=0; u < 6; u++)
934 {
cristyb51dff52011-05-19 16:55:47 +0000935 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",
cristye6365592010-04-02 17:31:23 +0000936 ColorMatrix[v][u]);
937 (void) ConcatenateString(&message,format);
938 }
939 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
940 }
941 message=DestroyString(message);
942 }
943 /*
anthonyfd706f92012-01-19 04:22:02 +0000944 Apply the ColorMatrix to image.
cristye6365592010-04-02 17:31:23 +0000945 */
946 status=MagickTrue;
947 progress=0;
cristydb070952012-04-20 14:33:00 +0000948 image_view=AcquireVirtualCacheView(image,exception);
949 color_view=AcquireAuthenticCacheView(color_image,exception);
cristye6365592010-04-02 17:31:23 +0000950#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000951 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +0000952 dynamic_number_threads(image,image->columns,image->rows,1)
cristye6365592010-04-02 17:31:23 +0000953#endif
cristybb503372010-05-27 20:51:26 +0000954 for (y=0; y < (ssize_t) image->rows; y++)
cristye6365592010-04-02 17:31:23 +0000955 {
cristyfcc25d92012-02-19 23:06:48 +0000956 PixelInfo
cristye6365592010-04-02 17:31:23 +0000957 pixel;
958
cristy4c08aed2011-07-01 19:47:50 +0000959 register const Quantum
cristye6365592010-04-02 17:31:23 +0000960 *restrict p;
961
cristy4c08aed2011-07-01 19:47:50 +0000962 register Quantum
963 *restrict q;
964
cristybb503372010-05-27 20:51:26 +0000965 register ssize_t
cristye6365592010-04-02 17:31:23 +0000966 x;
967
cristye6365592010-04-02 17:31:23 +0000968 if (status == MagickFalse)
969 continue;
970 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
971 q=GetCacheViewAuthenticPixels(color_view,0,y,color_image->columns,1,
972 exception);
cristy4c08aed2011-07-01 19:47:50 +0000973 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristye6365592010-04-02 17:31:23 +0000974 {
975 status=MagickFalse;
976 continue;
977 }
cristyfcc25d92012-02-19 23:06:48 +0000978 GetPixelInfo(image,&pixel);
cristybb503372010-05-27 20:51:26 +0000979 for (x=0; x < (ssize_t) image->columns; x++)
cristye6365592010-04-02 17:31:23 +0000980 {
cristybb503372010-05-27 20:51:26 +0000981 register ssize_t
cristye6365592010-04-02 17:31:23 +0000982 v;
983
cristybb503372010-05-27 20:51:26 +0000984 size_t
cristye6365592010-04-02 17:31:23 +0000985 height;
986
cristyfcc25d92012-02-19 23:06:48 +0000987 GetPixelInfoPixel(image,p,&pixel);
cristye6365592010-04-02 17:31:23 +0000988 height=color_matrix->height > 6 ? 6UL : color_matrix->height;
cristybb503372010-05-27 20:51:26 +0000989 for (v=0; v < (ssize_t) height; v++)
cristye6365592010-04-02 17:31:23 +0000990 {
cristyfcc25d92012-02-19 23:06:48 +0000991 MagickRealType
992 sum;
993
994 sum=ColorMatrix[v][0]*GetPixelRed(image,p)+ColorMatrix[v][1]*
cristy4c08aed2011-07-01 19:47:50 +0000995 GetPixelGreen(image,p)+ColorMatrix[v][2]*GetPixelBlue(image,p);
cristye6365592010-04-02 17:31:23 +0000996 if (image->colorspace == CMYKColorspace)
cristyfcc25d92012-02-19 23:06:48 +0000997 sum+=ColorMatrix[v][3]*GetPixelBlack(image,p);
cristy4c08aed2011-07-01 19:47:50 +0000998 if (image->matte != MagickFalse)
cristyfcc25d92012-02-19 23:06:48 +0000999 sum+=ColorMatrix[v][4]*GetPixelAlpha(image,p);
1000 sum+=QuantumRange*ColorMatrix[v][5];
cristye6365592010-04-02 17:31:23 +00001001 switch (v)
1002 {
cristyfcc25d92012-02-19 23:06:48 +00001003 case 0: pixel.red=sum; break;
1004 case 1: pixel.green=sum; break;
1005 case 2: pixel.blue=sum; break;
1006 case 3: pixel.black=sum; break;
1007 case 4: pixel.alpha=sum; break;
1008 default: break;
cristye6365592010-04-02 17:31:23 +00001009 }
1010 }
cristyfcc25d92012-02-19 23:06:48 +00001011 SetPixelInfoPixel(color_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +00001012 p+=GetPixelChannels(image);
1013 q+=GetPixelChannels(color_image);
cristye6365592010-04-02 17:31:23 +00001014 }
1015 if (SyncCacheViewAuthenticPixels(color_view,exception) == MagickFalse)
1016 status=MagickFalse;
1017 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1018 {
1019 MagickBooleanType
1020 proceed;
1021
1022#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00001023 #pragma omp critical (MagickCore_ColorMatrixImage)
cristye6365592010-04-02 17:31:23 +00001024#endif
1025 proceed=SetImageProgress(image,ColorMatrixImageTag,progress++,
1026 image->rows);
1027 if (proceed == MagickFalse)
1028 status=MagickFalse;
1029 }
1030 }
1031 color_view=DestroyCacheView(color_view);
1032 image_view=DestroyCacheView(image_view);
1033 if (status == MagickFalse)
1034 color_image=DestroyImage(color_image);
1035 return(color_image);
1036}
1037
1038/*
1039%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1040% %
1041% %
1042% %
cristy3ed852e2009-09-05 21:47:34 +00001043+ D e s t r o y F x I n f o %
1044% %
1045% %
1046% %
1047%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1048%
1049% DestroyFxInfo() deallocates memory associated with an FxInfo structure.
1050%
1051% The format of the DestroyFxInfo method is:
1052%
1053% ImageInfo *DestroyFxInfo(ImageInfo *fx_info)
1054%
1055% A description of each parameter follows:
1056%
1057% o fx_info: the fx info.
1058%
1059*/
cristy7832dc22011-09-05 01:21:53 +00001060MagickPrivate FxInfo *DestroyFxInfo(FxInfo *fx_info)
cristy3ed852e2009-09-05 21:47:34 +00001061{
cristybb503372010-05-27 20:51:26 +00001062 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001063 i;
1064
1065 fx_info->exception=DestroyExceptionInfo(fx_info->exception);
1066 fx_info->expression=DestroyString(fx_info->expression);
1067 fx_info->symbols=DestroySplayTree(fx_info->symbols);
1068 fx_info->colors=DestroySplayTree(fx_info->colors);
cristy0ea377f2011-03-24 00:54:19 +00001069 for (i=(ssize_t) GetImageListLength(fx_info->images)-1; i >= 0; i--)
cristyd76c51e2011-03-26 00:21:26 +00001070 fx_info->view[i]=DestroyCacheView(fx_info->view[i]);
1071 fx_info->view=(CacheView **) RelinquishMagickMemory(fx_info->view);
cristy3ed852e2009-09-05 21:47:34 +00001072 fx_info->random_info=DestroyRandomInfo(fx_info->random_info);
1073 fx_info=(FxInfo *) RelinquishMagickMemory(fx_info);
1074 return(fx_info);
1075}
1076
1077/*
1078%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1079% %
1080% %
1081% %
cristy3ed852e2009-09-05 21:47:34 +00001082+ 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 %
1083% %
1084% %
1085% %
1086%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1087%
1088% FxEvaluateChannelExpression() evaluates an expression and returns the
1089% results.
1090%
1091% The format of the FxEvaluateExpression method is:
1092%
1093% MagickRealType FxEvaluateChannelExpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00001094% const PixelChannel channel,const ssize_t x,const ssize_t y,
cristy3ed852e2009-09-05 21:47:34 +00001095% MagickRealType *alpha,Exceptioninfo *exception)
1096% MagickRealType FxEvaluateExpression(FxInfo *fx_info,
1097% MagickRealType *alpha,Exceptioninfo *exception)
1098%
1099% A description of each parameter follows:
1100%
1101% o fx_info: the fx info.
1102%
1103% o channel: the channel.
1104%
1105% o x,y: the pixel position.
1106%
1107% o alpha: the result.
1108%
1109% o exception: return any errors or warnings in this structure.
1110%
1111*/
1112
cristy351842f2010-03-07 15:27:38 +00001113static inline double MagickMax(const double x,const double y)
1114{
1115 if (x > y)
1116 return(x);
1117 return(y);
1118}
1119
1120static inline double MagickMin(const double x,const double y)
1121{
1122 if (x < y)
1123 return(x);
1124 return(y);
1125}
1126
cristy3ed852e2009-09-05 21:47:34 +00001127static MagickRealType FxChannelStatistics(FxInfo *fx_info,const Image *image,
cristy0568ffc2011-07-25 16:54:14 +00001128 PixelChannel channel,const char *symbol,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001129{
1130 char
1131 key[MaxTextExtent],
1132 statistic[MaxTextExtent];
1133
1134 const char
1135 *value;
1136
1137 register const char
1138 *p;
1139
1140 for (p=symbol; (*p != '.') && (*p != '\0'); p++) ;
1141 if (*p == '.')
1142 switch (*++p) /* e.g. depth.r */
1143 {
cristy541ae572011-08-05 19:08:59 +00001144 case 'r': channel=RedPixelChannel; break;
1145 case 'g': channel=GreenPixelChannel; break;
1146 case 'b': channel=BluePixelChannel; break;
1147 case 'c': channel=CyanPixelChannel; break;
1148 case 'm': channel=MagentaPixelChannel; break;
1149 case 'y': channel=YellowPixelChannel; break;
1150 case 'k': channel=BlackPixelChannel; break;
cristy3ed852e2009-09-05 21:47:34 +00001151 default: break;
1152 }
cristyb51dff52011-05-19 16:55:47 +00001153 (void) FormatLocaleString(key,MaxTextExtent,"%p.%.20g.%s",(void *) image,
cristye8c25f92010-06-03 00:53:06 +00001154 (double) channel,symbol);
cristy3ed852e2009-09-05 21:47:34 +00001155 value=(const char *) GetValueFromSplayTree(fx_info->symbols,key);
1156 if (value != (const char *) NULL)
cristydbdd0e32011-11-04 23:29:40 +00001157 return(QuantumScale*StringToDouble(value,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001158 (void) DeleteNodeFromSplayTree(fx_info->symbols,key);
1159 if (LocaleNCompare(symbol,"depth",5) == 0)
1160 {
cristybb503372010-05-27 20:51:26 +00001161 size_t
cristy3ed852e2009-09-05 21:47:34 +00001162 depth;
1163
cristyfefab1b2011-07-05 00:33:22 +00001164 depth=GetImageDepth(image,exception);
1165 (void) FormatLocaleString(statistic,MaxTextExtent,"%.20g",(double) depth);
cristy3ed852e2009-09-05 21:47:34 +00001166 }
1167 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1168 {
1169 double
1170 kurtosis,
1171 skewness;
1172
cristyd42d9952011-07-08 14:21:50 +00001173 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
cristyb51dff52011-05-19 16:55:47 +00001174 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",kurtosis);
cristy3ed852e2009-09-05 21:47:34 +00001175 }
1176 if (LocaleNCompare(symbol,"maxima",6) == 0)
1177 {
1178 double
1179 maxima,
1180 minima;
1181
cristyd42d9952011-07-08 14:21:50 +00001182 (void) GetImageRange(image,&minima,&maxima,exception);
cristyb51dff52011-05-19 16:55:47 +00001183 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",maxima);
cristy3ed852e2009-09-05 21:47:34 +00001184 }
1185 if (LocaleNCompare(symbol,"mean",4) == 0)
1186 {
1187 double
1188 mean,
1189 standard_deviation;
1190
cristyd42d9952011-07-08 14:21:50 +00001191 (void) GetImageMean(image,&mean,&standard_deviation,exception);
cristyb51dff52011-05-19 16:55:47 +00001192 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",mean);
cristy3ed852e2009-09-05 21:47:34 +00001193 }
1194 if (LocaleNCompare(symbol,"minima",6) == 0)
1195 {
1196 double
1197 maxima,
1198 minima;
1199
cristyd42d9952011-07-08 14:21:50 +00001200 (void) GetImageRange(image,&minima,&maxima,exception);
cristyb51dff52011-05-19 16:55:47 +00001201 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",minima);
cristy3ed852e2009-09-05 21:47:34 +00001202 }
1203 if (LocaleNCompare(symbol,"skewness",8) == 0)
1204 {
1205 double
1206 kurtosis,
1207 skewness;
1208
cristyd42d9952011-07-08 14:21:50 +00001209 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
cristyb51dff52011-05-19 16:55:47 +00001210 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",skewness);
cristy3ed852e2009-09-05 21:47:34 +00001211 }
1212 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1213 {
1214 double
1215 mean,
1216 standard_deviation;
1217
cristyd42d9952011-07-08 14:21:50 +00001218 (void) GetImageMean(image,&mean,&standard_deviation,exception);
cristyb51dff52011-05-19 16:55:47 +00001219 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",
cristy3ed852e2009-09-05 21:47:34 +00001220 standard_deviation);
1221 }
1222 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(key),
1223 ConstantString(statistic));
cristydbdd0e32011-11-04 23:29:40 +00001224 return(QuantumScale*StringToDouble(statistic,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001225}
1226
1227static MagickRealType
cristy0568ffc2011-07-25 16:54:14 +00001228 FxEvaluateSubexpression(FxInfo *,const PixelChannel,const ssize_t,
cristye85007d2010-06-06 22:51:36 +00001229 const ssize_t,const char *,MagickRealType *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +00001230
cristyb0aad4c2011-11-02 19:30:35 +00001231static MagickOffsetType FxGCD(MagickOffsetType alpha,MagickOffsetType beta)
1232{
1233 if (beta != 0)
1234 return(FxGCD(beta,alpha % beta));
1235 return(alpha);
1236}
1237
cristy3ed852e2009-09-05 21:47:34 +00001238static inline const char *FxSubexpression(const char *expression,
1239 ExceptionInfo *exception)
1240{
1241 const char
1242 *subexpression;
1243
cristybb503372010-05-27 20:51:26 +00001244 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001245 level;
1246
1247 level=0;
1248 subexpression=expression;
1249 while ((*subexpression != '\0') &&
1250 ((level != 1) || (strchr(")",(int) *subexpression) == (char *) NULL)))
1251 {
1252 if (strchr("(",(int) *subexpression) != (char *) NULL)
1253 level++;
1254 else
1255 if (strchr(")",(int) *subexpression) != (char *) NULL)
1256 level--;
1257 subexpression++;
1258 }
1259 if (*subexpression == '\0')
1260 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
anthonye5b39652012-04-21 05:37:29 +00001261 "UnbalancedParenthesis","'%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00001262 return(subexpression);
1263}
1264
cristy0568ffc2011-07-25 16:54:14 +00001265static MagickRealType FxGetSymbol(FxInfo *fx_info,const PixelChannel channel,
cristye85007d2010-06-06 22:51:36 +00001266 const ssize_t x,const ssize_t y,const char *expression,
1267 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001268{
1269 char
1270 *q,
1271 subexpression[MaxTextExtent],
1272 symbol[MaxTextExtent];
1273
1274 const char
1275 *p,
1276 *value;
1277
1278 Image
1279 *image;
1280
cristy4c08aed2011-07-01 19:47:50 +00001281 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001282 pixel;
1283
1284 MagickRealType
1285 alpha,
1286 beta;
1287
1288 PointInfo
1289 point;
1290
cristybb503372010-05-27 20:51:26 +00001291 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001292 i;
1293
1294 size_t
cristy1707c6c2012-01-18 23:30:54 +00001295 length,
cristy3ed852e2009-09-05 21:47:34 +00001296 level;
1297
1298 p=expression;
1299 i=GetImageIndexInList(fx_info->images);
1300 level=0;
1301 point.x=(double) x;
1302 point.y=(double) y;
1303 if (isalpha((int) *(p+1)) == 0)
1304 {
1305 if (strchr("suv",(int) *p) != (char *) NULL)
1306 {
1307 switch (*p)
1308 {
1309 case 's':
1310 default:
1311 {
1312 i=GetImageIndexInList(fx_info->images);
1313 break;
1314 }
1315 case 'u': i=0; break;
1316 case 'v': i=1; break;
1317 }
1318 p++;
1319 if (*p == '[')
1320 {
1321 level++;
1322 q=subexpression;
1323 for (p++; *p != '\0'; )
1324 {
1325 if (*p == '[')
1326 level++;
1327 else
1328 if (*p == ']')
1329 {
1330 level--;
1331 if (level == 0)
1332 break;
1333 }
1334 *q++=(*p++);
1335 }
1336 *q='\0';
1337 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1338 &beta,exception);
cristybb503372010-05-27 20:51:26 +00001339 i=(ssize_t) (alpha+0.5);
cristy3ed852e2009-09-05 21:47:34 +00001340 p++;
1341 }
1342 if (*p == '.')
1343 p++;
1344 }
1345 if ((isalpha((int) *(p+1)) == 0) && (*p == 'p'))
1346 {
1347 p++;
1348 if (*p == '{')
1349 {
1350 level++;
1351 q=subexpression;
1352 for (p++; *p != '\0'; )
1353 {
1354 if (*p == '{')
1355 level++;
1356 else
1357 if (*p == '}')
1358 {
1359 level--;
1360 if (level == 0)
1361 break;
1362 }
1363 *q++=(*p++);
1364 }
1365 *q='\0';
1366 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1367 &beta,exception);
1368 point.x=alpha;
1369 point.y=beta;
1370 p++;
1371 }
1372 else
1373 if (*p == '[')
1374 {
1375 level++;
1376 q=subexpression;
1377 for (p++; *p != '\0'; )
1378 {
1379 if (*p == '[')
1380 level++;
1381 else
1382 if (*p == ']')
1383 {
1384 level--;
1385 if (level == 0)
1386 break;
1387 }
1388 *q++=(*p++);
1389 }
1390 *q='\0';
1391 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1392 &beta,exception);
1393 point.x+=alpha;
1394 point.y+=beta;
1395 p++;
1396 }
1397 if (*p == '.')
1398 p++;
1399 }
1400 }
1401 length=GetImageListLength(fx_info->images);
1402 while (i < 0)
cristybb503372010-05-27 20:51:26 +00001403 i+=(ssize_t) length;
cristy3ed852e2009-09-05 21:47:34 +00001404 i%=length;
1405 image=GetImageFromList(fx_info->images,i);
1406 if (image == (Image *) NULL)
1407 {
1408 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
anthonye5b39652012-04-21 05:37:29 +00001409 "NoSuchImage","'%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00001410 return(0.0);
1411 }
cristy4c08aed2011-07-01 19:47:50 +00001412 GetPixelInfo(image,&pixel);
1413 (void) InterpolatePixelInfo(image,fx_info->view[i],image->interpolate,
cristy4f820712011-04-01 12:35:43 +00001414 point.x,point.y,&pixel,exception);
cristy1707c6c2012-01-18 23:30:54 +00001415 if ((strlen(p) > 2) && (LocaleCompare(p,"intensity") != 0) &&
1416 (LocaleCompare(p,"luminance") != 0) && (LocaleCompare(p,"hue") != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00001417 (LocaleCompare(p,"saturation") != 0) &&
1418 (LocaleCompare(p,"lightness") != 0))
1419 {
1420 char
1421 name[MaxTextExtent];
1422
1423 (void) CopyMagickString(name,p,MaxTextExtent);
1424 for (q=name+(strlen(name)-1); q > name; q--)
1425 {
1426 if (*q == ')')
1427 break;
1428 if (*q == '.')
1429 {
1430 *q='\0';
1431 break;
1432 }
1433 }
1434 if ((strlen(name) > 2) &&
1435 (GetValueFromSplayTree(fx_info->symbols,name) == (const char *) NULL))
1436 {
cristy4c08aed2011-07-01 19:47:50 +00001437 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001438 *color;
1439
cristy4c08aed2011-07-01 19:47:50 +00001440 color=(PixelInfo *) GetValueFromSplayTree(fx_info->colors,name);
1441 if (color != (PixelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001442 {
1443 pixel=(*color);
1444 p+=strlen(name);
1445 }
1446 else
cristy1707c6c2012-01-18 23:30:54 +00001447 {
1448 MagickBooleanType
1449 status;
1450
1451 status=QueryColorCompliance(name,AllCompliance,&pixel,
1452 fx_info->exception);
1453 if (status != MagickFalse)
1454 {
1455 (void) AddValueToSplayTree(fx_info->colors,ConstantString(
1456 name),ClonePixelInfo(&pixel));
1457 p+=strlen(name);
1458 }
1459 }
cristy3ed852e2009-09-05 21:47:34 +00001460 }
1461 }
1462 (void) CopyMagickString(symbol,p,MaxTextExtent);
1463 StripString(symbol);
1464 if (*symbol == '\0')
1465 {
1466 switch (channel)
1467 {
cristy0568ffc2011-07-25 16:54:14 +00001468 case RedPixelChannel: return(QuantumScale*pixel.red);
1469 case GreenPixelChannel: return(QuantumScale*pixel.green);
1470 case BluePixelChannel: return(QuantumScale*pixel.blue);
1471 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001472 {
1473 if (image->colorspace != CMYKColorspace)
1474 {
1475 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00001476 ImageError,"ColorSeparatedImageRequired","'%s'",
cristy3ed852e2009-09-05 21:47:34 +00001477 image->filename);
1478 return(0.0);
1479 }
cristy4c08aed2011-07-01 19:47:50 +00001480 return(QuantumScale*pixel.black);
1481 }
cristy0568ffc2011-07-25 16:54:14 +00001482 case AlphaPixelChannel:
cristy4c08aed2011-07-01 19:47:50 +00001483 {
1484 MagickRealType
1485 alpha;
1486
1487 if (pixel.matte == MagickFalse)
1488 return(1.0);
1489 alpha=(MagickRealType) (QuantumScale*pixel.alpha);
1490 return(alpha);
cristy3ed852e2009-09-05 21:47:34 +00001491 }
cristya382aca2011-12-06 18:22:48 +00001492 case IndexPixelChannel:
1493 return(0.0);
cristyb3a73b52011-07-26 01:34:43 +00001494 case IntensityPixelChannel:
cristyf364ed42010-12-15 01:54:43 +00001495 {
cristy4c08aed2011-07-01 19:47:50 +00001496 return(QuantumScale*GetPixelInfoIntensity(&pixel));
cristyf364ed42010-12-15 01:54:43 +00001497 }
cristy3ed852e2009-09-05 21:47:34 +00001498 default:
1499 break;
1500 }
1501 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
anthonye5b39652012-04-21 05:37:29 +00001502 "UnableToParseExpression","'%s'",p);
cristy3ed852e2009-09-05 21:47:34 +00001503 return(0.0);
1504 }
1505 switch (*symbol)
1506 {
1507 case 'A':
1508 case 'a':
1509 {
1510 if (LocaleCompare(symbol,"a") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001511 return((MagickRealType) (QuantumScale*pixel.alpha));
cristy3ed852e2009-09-05 21:47:34 +00001512 break;
1513 }
1514 case 'B':
1515 case 'b':
1516 {
1517 if (LocaleCompare(symbol,"b") == 0)
1518 return(QuantumScale*pixel.blue);
1519 break;
1520 }
1521 case 'C':
1522 case 'c':
1523 {
1524 if (LocaleNCompare(symbol,"channel",7) == 0)
1525 {
1526 GeometryInfo
1527 channel_info;
1528
1529 MagickStatusType
1530 flags;
1531
1532 flags=ParseGeometry(symbol+7,&channel_info);
1533 if (image->colorspace == CMYKColorspace)
1534 switch (channel)
1535 {
cristy0568ffc2011-07-25 16:54:14 +00001536 case CyanPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001537 {
1538 if ((flags & RhoValue) == 0)
1539 return(0.0);
1540 return(channel_info.rho);
1541 }
cristy0568ffc2011-07-25 16:54:14 +00001542 case MagentaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001543 {
1544 if ((flags & SigmaValue) == 0)
1545 return(0.0);
1546 return(channel_info.sigma);
1547 }
cristy0568ffc2011-07-25 16:54:14 +00001548 case YellowPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001549 {
1550 if ((flags & XiValue) == 0)
1551 return(0.0);
1552 return(channel_info.xi);
1553 }
cristy0568ffc2011-07-25 16:54:14 +00001554 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001555 {
1556 if ((flags & PsiValue) == 0)
1557 return(0.0);
1558 return(channel_info.psi);
1559 }
cristy0568ffc2011-07-25 16:54:14 +00001560 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001561 {
1562 if ((flags & ChiValue) == 0)
1563 return(0.0);
1564 return(channel_info.chi);
1565 }
1566 default:
1567 return(0.0);
1568 }
1569 switch (channel)
1570 {
cristy0568ffc2011-07-25 16:54:14 +00001571 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001572 {
1573 if ((flags & RhoValue) == 0)
1574 return(0.0);
1575 return(channel_info.rho);
1576 }
cristy0568ffc2011-07-25 16:54:14 +00001577 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001578 {
1579 if ((flags & SigmaValue) == 0)
1580 return(0.0);
1581 return(channel_info.sigma);
1582 }
cristy0568ffc2011-07-25 16:54:14 +00001583 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001584 {
1585 if ((flags & XiValue) == 0)
1586 return(0.0);
1587 return(channel_info.xi);
1588 }
cristy0568ffc2011-07-25 16:54:14 +00001589 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001590 {
1591 if ((flags & ChiValue) == 0)
1592 return(0.0);
1593 return(channel_info.chi);
1594 }
cristy0568ffc2011-07-25 16:54:14 +00001595 case AlphaPixelChannel:
cristy4c08aed2011-07-01 19:47:50 +00001596 {
1597 if ((flags & PsiValue) == 0)
1598 return(0.0);
1599 return(channel_info.psi);
1600 }
cristy3ed852e2009-09-05 21:47:34 +00001601 default:
1602 return(0.0);
1603 }
1604 return(0.0);
1605 }
1606 if (LocaleCompare(symbol,"c") == 0)
1607 return(QuantumScale*pixel.red);
1608 break;
1609 }
1610 case 'D':
1611 case 'd':
1612 {
1613 if (LocaleNCompare(symbol,"depth",5) == 0)
1614 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1615 break;
1616 }
1617 case 'G':
1618 case 'g':
1619 {
1620 if (LocaleCompare(symbol,"g") == 0)
1621 return(QuantumScale*pixel.green);
1622 break;
1623 }
1624 case 'K':
1625 case 'k':
1626 {
1627 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1628 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1629 if (LocaleCompare(symbol,"k") == 0)
1630 {
1631 if (image->colorspace != CMYKColorspace)
1632 {
1633 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00001634 OptionError,"ColorSeparatedImageRequired","'%s'",
cristy3ed852e2009-09-05 21:47:34 +00001635 image->filename);
1636 return(0.0);
1637 }
cristy4c08aed2011-07-01 19:47:50 +00001638 return(QuantumScale*pixel.black);
cristy3ed852e2009-09-05 21:47:34 +00001639 }
1640 break;
1641 }
1642 case 'H':
1643 case 'h':
1644 {
1645 if (LocaleCompare(symbol,"h") == 0)
1646 return((MagickRealType) image->rows);
1647 if (LocaleCompare(symbol,"hue") == 0)
1648 {
1649 double
1650 hue,
1651 lightness,
1652 saturation;
1653
cristy0a39a5c2012-06-27 12:51:45 +00001654 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
cristyda1f9c12011-10-02 21:39:49 +00001655 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001656 return(hue);
1657 }
1658 break;
1659 }
1660 case 'I':
1661 case 'i':
1662 {
1663 if ((LocaleCompare(symbol,"image.depth") == 0) ||
1664 (LocaleCompare(symbol,"image.minima") == 0) ||
1665 (LocaleCompare(symbol,"image.maxima") == 0) ||
1666 (LocaleCompare(symbol,"image.mean") == 0) ||
1667 (LocaleCompare(symbol,"image.kurtosis") == 0) ||
1668 (LocaleCompare(symbol,"image.skewness") == 0) ||
1669 (LocaleCompare(symbol,"image.standard_deviation") == 0))
1670 return(FxChannelStatistics(fx_info,image,channel,symbol+6,exception));
1671 if (LocaleCompare(symbol,"image.resolution.x") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001672 return(image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00001673 if (LocaleCompare(symbol,"image.resolution.y") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001674 return(image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001675 if (LocaleCompare(symbol,"intensity") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001676 return(QuantumScale*GetPixelInfoIntensity(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001677 if (LocaleCompare(symbol,"i") == 0)
1678 return((MagickRealType) x);
1679 break;
1680 }
1681 case 'J':
1682 case 'j':
1683 {
1684 if (LocaleCompare(symbol,"j") == 0)
1685 return((MagickRealType) y);
1686 break;
1687 }
1688 case 'L':
1689 case 'l':
1690 {
1691 if (LocaleCompare(symbol,"lightness") == 0)
1692 {
1693 double
1694 hue,
1695 lightness,
1696 saturation;
1697
cristy0a39a5c2012-06-27 12:51:45 +00001698 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
cristyda1f9c12011-10-02 21:39:49 +00001699 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001700 return(lightness);
1701 }
1702 if (LocaleCompare(symbol,"luminance") == 0)
1703 {
1704 double
1705 luminence;
1706
1707 luminence=0.2126*pixel.red+0.7152*pixel.green+0.0722*pixel.blue;
1708 return(QuantumScale*luminence);
1709 }
1710 break;
1711 }
1712 case 'M':
1713 case 'm':
1714 {
1715 if (LocaleNCompare(symbol,"maxima",6) == 0)
1716 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1717 if (LocaleNCompare(symbol,"mean",4) == 0)
1718 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1719 if (LocaleNCompare(symbol,"minima",6) == 0)
1720 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1721 if (LocaleCompare(symbol,"m") == 0)
1722 return(QuantumScale*pixel.blue);
1723 break;
1724 }
1725 case 'N':
1726 case 'n':
1727 {
1728 if (LocaleCompare(symbol,"n") == 0)
anthony374f5dd2011-03-25 10:08:53 +00001729 return((MagickRealType) GetImageListLength(fx_info->images));
cristy3ed852e2009-09-05 21:47:34 +00001730 break;
1731 }
1732 case 'O':
1733 case 'o':
1734 {
1735 if (LocaleCompare(symbol,"o") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001736 return(QuantumScale*pixel.alpha);
cristy3ed852e2009-09-05 21:47:34 +00001737 break;
1738 }
1739 case 'P':
1740 case 'p':
1741 {
1742 if (LocaleCompare(symbol,"page.height") == 0)
1743 return((MagickRealType) image->page.height);
1744 if (LocaleCompare(symbol,"page.width") == 0)
1745 return((MagickRealType) image->page.width);
1746 if (LocaleCompare(symbol,"page.x") == 0)
1747 return((MagickRealType) image->page.x);
1748 if (LocaleCompare(symbol,"page.y") == 0)
1749 return((MagickRealType) image->page.y);
1750 break;
1751 }
1752 case 'R':
1753 case 'r':
1754 {
1755 if (LocaleCompare(symbol,"resolution.x") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001756 return(image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00001757 if (LocaleCompare(symbol,"resolution.y") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001758 return(image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001759 if (LocaleCompare(symbol,"r") == 0)
1760 return(QuantumScale*pixel.red);
1761 break;
1762 }
1763 case 'S':
1764 case 's':
1765 {
1766 if (LocaleCompare(symbol,"saturation") == 0)
1767 {
1768 double
1769 hue,
1770 lightness,
1771 saturation;
1772
cristy0a39a5c2012-06-27 12:51:45 +00001773 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
cristyda1f9c12011-10-02 21:39:49 +00001774 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001775 return(saturation);
1776 }
1777 if (LocaleNCompare(symbol,"skewness",8) == 0)
1778 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1779 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1780 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1781 break;
1782 }
1783 case 'T':
1784 case 't':
1785 {
1786 if (LocaleCompare(symbol,"t") == 0)
cristy5a15b932011-03-26 12:50:33 +00001787 return((MagickRealType) GetImageIndexInList(fx_info->images));
cristy3ed852e2009-09-05 21:47:34 +00001788 break;
1789 }
1790 case 'W':
1791 case 'w':
1792 {
1793 if (LocaleCompare(symbol,"w") == 0)
1794 return((MagickRealType) image->columns);
1795 break;
1796 }
1797 case 'Y':
1798 case 'y':
1799 {
1800 if (LocaleCompare(symbol,"y") == 0)
1801 return(QuantumScale*pixel.green);
1802 break;
1803 }
1804 case 'Z':
1805 case 'z':
1806 {
1807 if (LocaleCompare(symbol,"z") == 0)
1808 {
1809 MagickRealType
1810 depth;
1811
cristyfefab1b2011-07-05 00:33:22 +00001812 depth=(MagickRealType) GetImageDepth(image,fx_info->exception);
cristy3ed852e2009-09-05 21:47:34 +00001813 return(depth);
1814 }
1815 break;
1816 }
1817 default:
1818 break;
1819 }
1820 value=(const char *) GetValueFromSplayTree(fx_info->symbols,symbol);
1821 if (value != (const char *) NULL)
cristydbdd0e32011-11-04 23:29:40 +00001822 return((MagickRealType) StringToDouble(value,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001823 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
anthonye5b39652012-04-21 05:37:29 +00001824 "UnableToParseExpression","'%s'",symbol);
cristy3ed852e2009-09-05 21:47:34 +00001825 return(0.0);
1826}
1827
1828static const char *FxOperatorPrecedence(const char *expression,
1829 ExceptionInfo *exception)
1830{
1831 typedef enum
1832 {
1833 UndefinedPrecedence,
1834 NullPrecedence,
1835 BitwiseComplementPrecedence,
1836 ExponentPrecedence,
cristy116af162010-08-13 01:25:47 +00001837 ExponentialNotationPrecedence,
cristy3ed852e2009-09-05 21:47:34 +00001838 MultiplyPrecedence,
1839 AdditionPrecedence,
1840 ShiftPrecedence,
1841 RelationalPrecedence,
1842 EquivalencyPrecedence,
1843 BitwiseAndPrecedence,
1844 BitwiseOrPrecedence,
1845 LogicalAndPrecedence,
1846 LogicalOrPrecedence,
1847 TernaryPrecedence,
1848 AssignmentPrecedence,
1849 CommaPrecedence,
1850 SeparatorPrecedence
1851 } FxPrecedence;
1852
1853 FxPrecedence
1854 precedence,
1855 target;
1856
1857 register const char
1858 *subexpression;
1859
1860 register int
1861 c;
1862
cristybb503372010-05-27 20:51:26 +00001863 size_t
cristy3ed852e2009-09-05 21:47:34 +00001864 level;
1865
1866 c=0;
1867 level=0;
1868 subexpression=(const char *) NULL;
1869 target=NullPrecedence;
1870 while (*expression != '\0')
1871 {
1872 precedence=UndefinedPrecedence;
1873 if ((isspace((int) ((char) *expression)) != 0) || (c == (int) '@'))
1874 {
1875 expression++;
1876 continue;
1877 }
cristy488fa882010-03-01 22:34:24 +00001878 switch (*expression)
1879 {
1880 case 'A':
1881 case 'a':
cristy3ed852e2009-09-05 21:47:34 +00001882 {
cristyb33454f2011-08-03 02:10:45 +00001883#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00001884 if (LocaleNCompare(expression,"acosh",5) == 0)
1885 {
1886 expression+=5;
1887 break;
1888 }
cristyb33454f2011-08-03 02:10:45 +00001889#endif
1890#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00001891 if (LocaleNCompare(expression,"asinh",5) == 0)
1892 {
1893 expression+=5;
1894 break;
1895 }
cristyb33454f2011-08-03 02:10:45 +00001896#endif
1897#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00001898 if (LocaleNCompare(expression,"atanh",5) == 0)
cristy488fa882010-03-01 22:34:24 +00001899 {
1900 expression+=5;
1901 break;
1902 }
cristyb33454f2011-08-03 02:10:45 +00001903#endif
anthony62838e52012-05-24 12:41:54 +00001904 if (LocaleNCompare(expression,"atan2",5) == 0)
1905 {
1906 expression+=5;
1907 break;
1908 }
cristy488fa882010-03-01 22:34:24 +00001909 break;
cristy3ed852e2009-09-05 21:47:34 +00001910 }
cristy62d455f2011-11-03 11:42:28 +00001911 case 'E':
1912 case 'e':
1913 {
1914 if ((LocaleNCompare(expression,"E+",2) == 0) ||
1915 (LocaleNCompare(expression,"E-",2) == 0))
1916 {
1917 expression+=2; /* scientific notation */
1918 break;
1919 }
1920 }
cristy488fa882010-03-01 22:34:24 +00001921 case 'J':
1922 case 'j':
1923 {
1924 if ((LocaleNCompare(expression,"j0",2) == 0) ||
1925 (LocaleNCompare(expression,"j1",2) == 0))
1926 {
1927 expression+=2;
1928 break;
1929 }
1930 break;
1931 }
cristy2def9322010-06-18 23:59:37 +00001932 case '#':
1933 {
1934 while (isxdigit((int) ((unsigned char) *(expression+1))) != 0)
1935 expression++;
1936 break;
1937 }
cristy488fa882010-03-01 22:34:24 +00001938 default:
1939 break;
1940 }
cristy3ed852e2009-09-05 21:47:34 +00001941 if ((c == (int) '{') || (c == (int) '['))
1942 level++;
1943 else
1944 if ((c == (int) '}') || (c == (int) ']'))
1945 level--;
1946 if (level == 0)
1947 switch ((unsigned char) *expression)
1948 {
1949 case '~':
1950 case '!':
1951 {
1952 precedence=BitwiseComplementPrecedence;
1953 break;
1954 }
1955 case '^':
cristy6621e252010-08-13 00:42:57 +00001956 case '@':
cristy3ed852e2009-09-05 21:47:34 +00001957 {
1958 precedence=ExponentPrecedence;
1959 break;
1960 }
1961 default:
1962 {
1963 if (((c != 0) && ((isdigit((int) ((char) c)) != 0) ||
1964 (strchr(")",c) != (char *) NULL))) &&
1965 (((islower((int) ((char) *expression)) != 0) ||
1966 (strchr("(",(int) *expression) != (char *) NULL)) ||
1967 ((isdigit((int) ((char) c)) == 0) &&
1968 (isdigit((int) ((char) *expression)) != 0))) &&
1969 (strchr("xy",(int) *expression) == (char *) NULL))
1970 precedence=MultiplyPrecedence;
1971 break;
1972 }
1973 case '*':
1974 case '/':
1975 case '%':
1976 {
1977 precedence=MultiplyPrecedence;
1978 break;
1979 }
1980 case '+':
1981 case '-':
1982 {
1983 if ((strchr("(+-/*%:&^|<>~,",c) == (char *) NULL) ||
1984 (isalpha(c) != 0))
1985 precedence=AdditionPrecedence;
1986 break;
1987 }
1988 case LeftShiftOperator:
1989 case RightShiftOperator:
1990 {
1991 precedence=ShiftPrecedence;
1992 break;
1993 }
1994 case '<':
1995 case LessThanEqualOperator:
1996 case GreaterThanEqualOperator:
1997 case '>':
1998 {
1999 precedence=RelationalPrecedence;
2000 break;
2001 }
2002 case EqualOperator:
2003 case NotEqualOperator:
2004 {
2005 precedence=EquivalencyPrecedence;
2006 break;
2007 }
2008 case '&':
2009 {
2010 precedence=BitwiseAndPrecedence;
2011 break;
2012 }
2013 case '|':
2014 {
2015 precedence=BitwiseOrPrecedence;
2016 break;
2017 }
2018 case LogicalAndOperator:
2019 {
2020 precedence=LogicalAndPrecedence;
2021 break;
2022 }
2023 case LogicalOrOperator:
2024 {
2025 precedence=LogicalOrPrecedence;
2026 break;
2027 }
cristy116af162010-08-13 01:25:47 +00002028 case ExponentialNotation:
2029 {
2030 precedence=ExponentialNotationPrecedence;
2031 break;
2032 }
cristy3ed852e2009-09-05 21:47:34 +00002033 case ':':
2034 case '?':
2035 {
2036 precedence=TernaryPrecedence;
2037 break;
2038 }
2039 case '=':
2040 {
2041 precedence=AssignmentPrecedence;
2042 break;
2043 }
2044 case ',':
2045 {
2046 precedence=CommaPrecedence;
2047 break;
2048 }
2049 case ';':
2050 {
2051 precedence=SeparatorPrecedence;
2052 break;
2053 }
2054 }
2055 if ((precedence == BitwiseComplementPrecedence) ||
2056 (precedence == TernaryPrecedence) ||
2057 (precedence == AssignmentPrecedence))
2058 {
2059 if (precedence > target)
2060 {
2061 /*
2062 Right-to-left associativity.
2063 */
2064 target=precedence;
2065 subexpression=expression;
2066 }
2067 }
2068 else
2069 if (precedence >= target)
2070 {
2071 /*
2072 Left-to-right associativity.
2073 */
2074 target=precedence;
2075 subexpression=expression;
2076 }
2077 if (strchr("(",(int) *expression) != (char *) NULL)
2078 expression=FxSubexpression(expression,exception);
2079 c=(int) (*expression++);
2080 }
2081 return(subexpression);
2082}
2083
2084static MagickRealType FxEvaluateSubexpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002085 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristye85007d2010-06-06 22:51:36 +00002086 const char *expression,MagickRealType *beta,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002087{
2088 char
2089 *q,
2090 subexpression[MaxTextExtent];
2091
2092 MagickRealType
2093 alpha,
2094 gamma;
2095
2096 register const char
2097 *p;
2098
2099 *beta=0.0;
2100 if (exception->severity != UndefinedException)
2101 return(0.0);
2102 while (isspace((int) *expression) != 0)
2103 expression++;
2104 if (*expression == '\0')
2105 {
2106 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
anthonye5b39652012-04-21 05:37:29 +00002107 "MissingExpression","'%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00002108 return(0.0);
2109 }
cristy66322f02010-05-17 11:40:48 +00002110 *subexpression='\0';
cristy3ed852e2009-09-05 21:47:34 +00002111 p=FxOperatorPrecedence(expression,exception);
2112 if (p != (const char *) NULL)
2113 {
2114 (void) CopyMagickString(subexpression,expression,(size_t)
2115 (p-expression+1));
2116 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2117 exception);
2118 switch ((unsigned char) *p)
2119 {
2120 case '~':
2121 {
2122 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristybb503372010-05-27 20:51:26 +00002123 *beta=(MagickRealType) (~(size_t) *beta);
cristy3ed852e2009-09-05 21:47:34 +00002124 return(*beta);
2125 }
2126 case '!':
2127 {
2128 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2129 return(*beta == 0.0 ? 1.0 : 0.0);
2130 }
2131 case '^':
2132 {
2133 *beta=pow((double) alpha,(double) FxEvaluateSubexpression(fx_info,
2134 channel,x,y,++p,beta,exception));
2135 return(*beta);
2136 }
2137 case '*':
cristy116af162010-08-13 01:25:47 +00002138 case ExponentialNotation:
cristy3ed852e2009-09-05 21:47:34 +00002139 {
2140 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2141 return(alpha*(*beta));
2142 }
2143 case '/':
2144 {
2145 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2146 if (*beta == 0.0)
2147 {
2148 if (exception->severity == UndefinedException)
2149 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00002150 OptionError,"DivideByZero","'%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00002151 return(0.0);
2152 }
2153 return(alpha/(*beta));
2154 }
2155 case '%':
2156 {
2157 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2158 *beta=fabs(floor(((double) *beta)+0.5));
2159 if (*beta == 0.0)
2160 {
2161 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00002162 OptionError,"DivideByZero","'%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00002163 return(0.0);
2164 }
2165 return(fmod((double) alpha,(double) *beta));
2166 }
2167 case '+':
2168 {
2169 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2170 return(alpha+(*beta));
2171 }
2172 case '-':
2173 {
2174 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2175 return(alpha-(*beta));
2176 }
2177 case LeftShiftOperator:
2178 {
2179 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy1707c6c2012-01-18 23:30:54 +00002180 *beta=(MagickRealType) ((size_t) (alpha+0.5) << (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002181 return(*beta);
2182 }
2183 case RightShiftOperator:
2184 {
2185 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy1707c6c2012-01-18 23:30:54 +00002186 *beta=(MagickRealType) ((size_t) (alpha+0.5) >> (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002187 return(*beta);
2188 }
2189 case '<':
2190 {
2191 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2192 return(alpha < *beta ? 1.0 : 0.0);
2193 }
2194 case LessThanEqualOperator:
2195 {
2196 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2197 return(alpha <= *beta ? 1.0 : 0.0);
2198 }
2199 case '>':
2200 {
2201 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2202 return(alpha > *beta ? 1.0 : 0.0);
2203 }
2204 case GreaterThanEqualOperator:
2205 {
2206 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2207 return(alpha >= *beta ? 1.0 : 0.0);
2208 }
2209 case EqualOperator:
2210 {
2211 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy9b528342012-06-02 00:59:20 +00002212 return(fabs(alpha-(*beta)) < MagickEpsilon ? MagickEpsilon : 0.0);
cristy3ed852e2009-09-05 21:47:34 +00002213 }
2214 case NotEqualOperator:
2215 {
2216 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy972050b2012-06-04 22:09:17 +00002217 return(fabs(alpha-(*beta)) >= MagickEpsilon ? 1.0 : 0.0);
cristy3ed852e2009-09-05 21:47:34 +00002218 }
2219 case '&':
2220 {
2221 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy1707c6c2012-01-18 23:30:54 +00002222 *beta=(MagickRealType) ((size_t) (alpha+0.5) & (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002223 return(*beta);
2224 }
2225 case '|':
2226 {
2227 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy1707c6c2012-01-18 23:30:54 +00002228 *beta=(MagickRealType) ((size_t) (alpha+0.5) | (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002229 return(*beta);
2230 }
2231 case LogicalAndOperator:
2232 {
2233 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2234 *beta=(alpha > 0.0) && (gamma > 0.0) ? 1.0 : 0.0;
2235 return(*beta);
2236 }
2237 case LogicalOrOperator:
2238 {
2239 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2240 *beta=(alpha > 0.0) || (gamma > 0.0) ? 1.0 : 0.0;
2241 return(*beta);
2242 }
2243 case '?':
2244 {
2245 MagickRealType
2246 gamma;
2247
2248 (void) CopyMagickString(subexpression,++p,MaxTextExtent);
2249 q=subexpression;
2250 p=StringToken(":",&q);
2251 if (q == (char *) NULL)
2252 {
2253 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00002254 OptionError,"UnableToParseExpression","'%s'",subexpression);
cristy3ed852e2009-09-05 21:47:34 +00002255 return(0.0);
2256 }
cristy972050b2012-06-04 22:09:17 +00002257 if (fabs((double) alpha) >= MagickEpsilon)
cristy3ed852e2009-09-05 21:47:34 +00002258 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,exception);
2259 else
2260 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,q,beta,exception);
2261 return(gamma);
2262 }
2263 case '=':
2264 {
2265 char
2266 numeric[MaxTextExtent];
2267
2268 q=subexpression;
2269 while (isalpha((int) ((unsigned char) *q)) != 0)
2270 q++;
2271 if (*q != '\0')
2272 {
2273 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00002274 OptionError,"UnableToParseExpression","'%s'",subexpression);
cristy3ed852e2009-09-05 21:47:34 +00002275 return(0.0);
2276 }
2277 ClearMagickException(exception);
2278 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristyb51dff52011-05-19 16:55:47 +00002279 (void) FormatLocaleString(numeric,MaxTextExtent,"%g",(double)
cristy8cd5b312010-01-07 01:10:24 +00002280 *beta);
cristy3ed852e2009-09-05 21:47:34 +00002281 (void) DeleteNodeFromSplayTree(fx_info->symbols,subexpression);
2282 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(
2283 subexpression),ConstantString(numeric));
2284 return(*beta);
2285 }
2286 case ',':
2287 {
2288 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2289 return(alpha);
2290 }
2291 case ';':
2292 {
2293 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2294 return(*beta);
2295 }
2296 default:
2297 {
2298 gamma=alpha*FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,
2299 exception);
2300 return(gamma);
2301 }
2302 }
2303 }
2304 if (strchr("(",(int) *expression) != (char *) NULL)
2305 {
2306 (void) CopyMagickString(subexpression,expression+1,MaxTextExtent);
2307 subexpression[strlen(subexpression)-1]='\0';
2308 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2309 exception);
2310 return(gamma);
2311 }
cristy8b8a3ae2010-10-23 18:49:46 +00002312 switch (*expression)
cristy3ed852e2009-09-05 21:47:34 +00002313 {
2314 case '+':
2315 {
2316 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2317 exception);
2318 return(1.0*gamma);
2319 }
2320 case '-':
2321 {
2322 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2323 exception);
2324 return(-1.0*gamma);
2325 }
2326 case '~':
2327 {
2328 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2329 exception);
cristybb503372010-05-27 20:51:26 +00002330 return((MagickRealType) (~(size_t) (gamma+0.5)));
cristy3ed852e2009-09-05 21:47:34 +00002331 }
2332 case 'A':
2333 case 'a':
2334 {
2335 if (LocaleNCompare(expression,"abs",3) == 0)
2336 {
2337 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2338 exception);
2339 return((MagickRealType) fabs((double) alpha));
2340 }
cristyb33454f2011-08-03 02:10:45 +00002341#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00002342 if (LocaleNCompare(expression,"acosh",5) == 0)
2343 {
2344 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2345 exception);
2346 return((MagickRealType) acosh((double) alpha));
2347 }
cristyb33454f2011-08-03 02:10:45 +00002348#endif
cristy3ed852e2009-09-05 21:47:34 +00002349 if (LocaleNCompare(expression,"acos",4) == 0)
2350 {
2351 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2352 exception);
2353 return((MagickRealType) acos((double) alpha));
2354 }
cristy43c22f42010-03-30 12:34:07 +00002355#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002356 if (LocaleNCompare(expression,"airy",4) == 0)
2357 {
2358 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2359 exception);
2360 if (alpha == 0.0)
cristy2dd03222010-03-30 22:12:11 +00002361 return(1.0);
2362 gamma=2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha);
cristy43c22f42010-03-30 12:34:07 +00002363 return(gamma*gamma);
cristyee56cf12010-03-01 22:17:06 +00002364 }
cristy43c22f42010-03-30 12:34:07 +00002365#endif
cristyb33454f2011-08-03 02:10:45 +00002366#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00002367 if (LocaleNCompare(expression,"asinh",5) == 0)
2368 {
2369 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2370 exception);
2371 return((MagickRealType) asinh((double) alpha));
2372 }
cristyb33454f2011-08-03 02:10:45 +00002373#endif
cristy3ed852e2009-09-05 21:47:34 +00002374 if (LocaleNCompare(expression,"asin",4) == 0)
2375 {
2376 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2377 exception);
2378 return((MagickRealType) asin((double) alpha));
2379 }
2380 if (LocaleNCompare(expression,"alt",3) == 0)
2381 {
2382 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2383 exception);
cristybb503372010-05-27 20:51:26 +00002384 return(((ssize_t) alpha) & 0x01 ? -1.0 : 1.0);
cristy3ed852e2009-09-05 21:47:34 +00002385 }
2386 if (LocaleNCompare(expression,"atan2",5) == 0)
2387 {
2388 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2389 exception);
2390 return((MagickRealType) atan2((double) alpha,(double) *beta));
2391 }
cristyb33454f2011-08-03 02:10:45 +00002392#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00002393 if (LocaleNCompare(expression,"atanh",5) == 0)
2394 {
2395 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2396 exception);
2397 return((MagickRealType) atanh((double) alpha));
2398 }
cristyb33454f2011-08-03 02:10:45 +00002399#endif
cristy3ed852e2009-09-05 21:47:34 +00002400 if (LocaleNCompare(expression,"atan",4) == 0)
2401 {
2402 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2403 exception);
2404 return((MagickRealType) atan((double) alpha));
2405 }
2406 if (LocaleCompare(expression,"a") == 0)
2407 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2408 break;
2409 }
2410 case 'B':
2411 case 'b':
2412 {
2413 if (LocaleCompare(expression,"b") == 0)
2414 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2415 break;
2416 }
2417 case 'C':
2418 case 'c':
2419 {
2420 if (LocaleNCompare(expression,"ceil",4) == 0)
2421 {
2422 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2423 exception);
2424 return((MagickRealType) ceil((double) alpha));
2425 }
2426 if (LocaleNCompare(expression,"cosh",4) == 0)
2427 {
2428 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2429 exception);
2430 return((MagickRealType) cosh((double) alpha));
2431 }
2432 if (LocaleNCompare(expression,"cos",3) == 0)
2433 {
2434 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2435 exception);
2436 return((MagickRealType) cos((double) alpha));
2437 }
2438 if (LocaleCompare(expression,"c") == 0)
2439 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2440 break;
2441 }
2442 case 'D':
2443 case 'd':
2444 {
2445 if (LocaleNCompare(expression,"debug",5) == 0)
2446 {
2447 const char
2448 *type;
2449
2450 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2451 exception);
2452 if (fx_info->images->colorspace == CMYKColorspace)
2453 switch (channel)
2454 {
cristy0568ffc2011-07-25 16:54:14 +00002455 case CyanPixelChannel: type="cyan"; break;
2456 case MagentaPixelChannel: type="magenta"; break;
2457 case YellowPixelChannel: type="yellow"; break;
2458 case AlphaPixelChannel: type="opacity"; break;
2459 case BlackPixelChannel: type="black"; break;
cristy3ed852e2009-09-05 21:47:34 +00002460 default: type="unknown"; break;
2461 }
2462 else
2463 switch (channel)
2464 {
cristy0568ffc2011-07-25 16:54:14 +00002465 case RedPixelChannel: type="red"; break;
2466 case GreenPixelChannel: type="green"; break;
2467 case BluePixelChannel: type="blue"; break;
2468 case AlphaPixelChannel: type="opacity"; break;
cristy3ed852e2009-09-05 21:47:34 +00002469 default: type="unknown"; break;
2470 }
2471 (void) CopyMagickString(subexpression,expression+6,MaxTextExtent);
2472 if (strlen(subexpression) > 1)
2473 subexpression[strlen(subexpression)-1]='\0';
2474 if (fx_info->file != (FILE *) NULL)
cristy1707c6c2012-01-18 23:30:54 +00002475 (void) FormatLocaleFile(fx_info->file,"%s[%.20g,%.20g].%s: "
2476 "%s=%.*g\n",fx_info->images->filename,(double) x,(double) y,type,
2477 subexpression,GetMagickPrecision(),(double) alpha);
cristy3ed852e2009-09-05 21:47:34 +00002478 return(0.0);
2479 }
cristy5597a8d2011-11-04 00:25:32 +00002480 if (LocaleNCompare(expression,"drc",3) == 0)
2481 {
2482 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2483 exception);
2484 return((MagickRealType) (alpha/(*beta*(alpha-1.0)+1.0)));
2485 }
cristy3ed852e2009-09-05 21:47:34 +00002486 break;
2487 }
2488 case 'E':
2489 case 'e':
2490 {
2491 if (LocaleCompare(expression,"epsilon") == 0)
2492 return((MagickRealType) MagickEpsilon);
2493 if (LocaleNCompare(expression,"exp",3) == 0)
2494 {
2495 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2496 exception);
2497 return((MagickRealType) exp((double) alpha));
2498 }
2499 if (LocaleCompare(expression,"e") == 0)
2500 return((MagickRealType) 2.7182818284590452354);
2501 break;
2502 }
2503 case 'F':
2504 case 'f':
2505 {
2506 if (LocaleNCompare(expression,"floor",5) == 0)
2507 {
2508 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2509 exception);
2510 return((MagickRealType) floor((double) alpha));
2511 }
2512 break;
2513 }
2514 case 'G':
2515 case 'g':
2516 {
cristy9eeedea2011-11-02 19:04:05 +00002517 if (LocaleNCompare(expression,"gauss",5) == 0)
2518 {
2519 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2520 exception);
2521 gamma=exp((double) (-alpha*alpha/2.0))/sqrt(2.0*MagickPI);
2522 return((MagickRealType) gamma);
2523 }
cristyb0aad4c2011-11-02 19:30:35 +00002524 if (LocaleNCompare(expression,"gcd",3) == 0)
2525 {
2526 MagickOffsetType
2527 gcd;
2528
2529 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2530 exception);
cristy1707c6c2012-01-18 23:30:54 +00002531 gcd=FxGCD((MagickOffsetType) (alpha+0.5),(MagickOffsetType) (*beta+
2532 0.5));
cristyb0aad4c2011-11-02 19:30:35 +00002533 return((MagickRealType) gcd);
2534 }
cristy3ed852e2009-09-05 21:47:34 +00002535 if (LocaleCompare(expression,"g") == 0)
2536 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2537 break;
2538 }
2539 case 'H':
2540 case 'h':
2541 {
2542 if (LocaleCompare(expression,"h") == 0)
2543 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2544 if (LocaleCompare(expression,"hue") == 0)
2545 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2546 if (LocaleNCompare(expression,"hypot",5) == 0)
2547 {
2548 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2549 exception);
2550 return((MagickRealType) hypot((double) alpha,(double) *beta));
2551 }
2552 break;
2553 }
2554 case 'K':
2555 case 'k':
2556 {
2557 if (LocaleCompare(expression,"k") == 0)
2558 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2559 break;
2560 }
2561 case 'I':
2562 case 'i':
2563 {
2564 if (LocaleCompare(expression,"intensity") == 0)
2565 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2566 if (LocaleNCompare(expression,"int",3) == 0)
2567 {
2568 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2569 exception);
cristy16788e42010-08-13 13:44:26 +00002570 return((MagickRealType) floor(alpha));
cristy3ed852e2009-09-05 21:47:34 +00002571 }
cristy82b20722011-11-05 21:52:36 +00002572#if defined(MAGICKCORE_HAVE_ISNAN)
cristy639399c2011-11-02 19:16:15 +00002573 if (LocaleNCompare(expression,"isnan",5) == 0)
2574 {
2575 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2576 exception);
cristy17a10202011-11-02 19:17:04 +00002577 return((MagickRealType) !!isnan((double) alpha));
cristy639399c2011-11-02 19:16:15 +00002578 }
cristy82b20722011-11-05 21:52:36 +00002579#endif
cristy3ed852e2009-09-05 21:47:34 +00002580 if (LocaleCompare(expression,"i") == 0)
2581 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2582 break;
2583 }
2584 case 'J':
2585 case 'j':
2586 {
2587 if (LocaleCompare(expression,"j") == 0)
2588 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
cristy161b9262010-03-20 19:34:32 +00002589#if defined(MAGICKCORE_HAVE_J0)
cristyee56cf12010-03-01 22:17:06 +00002590 if (LocaleNCompare(expression,"j0",2) == 0)
2591 {
2592 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2593 exception);
2594 return((MagickRealType) j0((double) alpha));
2595 }
cristy161b9262010-03-20 19:34:32 +00002596#endif
2597#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002598 if (LocaleNCompare(expression,"j1",2) == 0)
2599 {
2600 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2601 exception);
2602 return((MagickRealType) j1((double) alpha));
2603 }
cristy161b9262010-03-20 19:34:32 +00002604#endif
cristyaa018fa2010-04-08 23:03:54 +00002605#if defined(MAGICKCORE_HAVE_J1)
cristya6a09e72010-03-02 14:51:02 +00002606 if (LocaleNCompare(expression,"jinc",4) == 0)
2607 {
2608 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2609 exception);
cristy0946a822010-03-12 17:14:58 +00002610 if (alpha == 0.0)
2611 return(1.0);
cristy1707c6c2012-01-18 23:30:54 +00002612 gamma=(MagickRealType) (2.0*j1((double) (MagickPI*alpha))/(MagickPI*
2613 alpha));
cristyfce2f7b2010-03-12 00:29:49 +00002614 return(gamma);
cristya6a09e72010-03-02 14:51:02 +00002615 }
cristyaa018fa2010-04-08 23:03:54 +00002616#endif
cristy3ed852e2009-09-05 21:47:34 +00002617 break;
2618 }
2619 case 'L':
2620 case 'l':
2621 {
2622 if (LocaleNCompare(expression,"ln",2) == 0)
2623 {
2624 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2625 exception);
2626 return((MagickRealType) log((double) alpha));
2627 }
cristyc8ed5322010-08-31 12:07:59 +00002628 if (LocaleNCompare(expression,"logtwo",6) == 0)
cristy3ed852e2009-09-05 21:47:34 +00002629 {
cristyc8ed5322010-08-31 12:07:59 +00002630 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
cristy3ed852e2009-09-05 21:47:34 +00002631 exception);
2632 return((MagickRealType) log10((double) alpha))/log10(2.0);
2633 }
2634 if (LocaleNCompare(expression,"log",3) == 0)
2635 {
2636 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2637 exception);
2638 return((MagickRealType) log10((double) alpha));
2639 }
2640 if (LocaleCompare(expression,"lightness") == 0)
2641 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2642 break;
2643 }
2644 case 'M':
2645 case 'm':
2646 {
2647 if (LocaleCompare(expression,"MaxRGB") == 0)
2648 return((MagickRealType) QuantumRange);
2649 if (LocaleNCompare(expression,"maxima",6) == 0)
2650 break;
2651 if (LocaleNCompare(expression,"max",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,"minima",6) == 0)
2658 break;
2659 if (LocaleNCompare(expression,"min",3) == 0)
cristy984049c2011-11-03 18:34:58 +00002660 {
2661 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2662 exception);
2663 return(alpha < *beta ? alpha : *beta);
2664 }
cristy3ed852e2009-09-05 21:47:34 +00002665 if (LocaleNCompare(expression,"mod",3) == 0)
2666 {
2667 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2668 exception);
cristy984049c2011-11-03 18:34:58 +00002669 gamma=alpha-floor((double) (alpha/(*beta)))*(*beta);
2670 return(gamma);
cristy3ed852e2009-09-05 21:47:34 +00002671 }
2672 if (LocaleCompare(expression,"m") == 0)
2673 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2674 break;
2675 }
2676 case 'N':
2677 case 'n':
2678 {
cristyad3502e2011-11-02 19:10:45 +00002679 if (LocaleNCompare(expression,"not",3) == 0)
2680 {
2681 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2682 exception);
2683 return((MagickRealType) (alpha < MagickEpsilon));
2684 }
cristy3ed852e2009-09-05 21:47:34 +00002685 if (LocaleCompare(expression,"n") == 0)
2686 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2687 break;
2688 }
2689 case 'O':
2690 case 'o':
2691 {
2692 if (LocaleCompare(expression,"Opaque") == 0)
2693 return(1.0);
2694 if (LocaleCompare(expression,"o") == 0)
2695 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2696 break;
2697 }
2698 case 'P':
2699 case 'p':
2700 {
cristy670aa3c2011-11-03 00:54:00 +00002701 if (LocaleCompare(expression,"phi") == 0)
2702 return((MagickRealType) MagickPHI);
cristy3ed852e2009-09-05 21:47:34 +00002703 if (LocaleCompare(expression,"pi") == 0)
2704 return((MagickRealType) MagickPI);
2705 if (LocaleNCompare(expression,"pow",3) == 0)
2706 {
2707 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2708 exception);
2709 return((MagickRealType) pow((double) alpha,(double) *beta));
2710 }
2711 if (LocaleCompare(expression,"p") == 0)
2712 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2713 break;
2714 }
2715 case 'Q':
2716 case 'q':
2717 {
2718 if (LocaleCompare(expression,"QuantumRange") == 0)
2719 return((MagickRealType) QuantumRange);
2720 if (LocaleCompare(expression,"QuantumScale") == 0)
2721 return((MagickRealType) QuantumScale);
2722 break;
2723 }
2724 case 'R':
2725 case 'r':
2726 {
2727 if (LocaleNCompare(expression,"rand",4) == 0)
2728 return((MagickRealType) GetPseudoRandomValue(fx_info->random_info));
2729 if (LocaleNCompare(expression,"round",5) == 0)
2730 {
2731 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2732 exception);
cristy16788e42010-08-13 13:44:26 +00002733 return((MagickRealType) floor((double) alpha+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002734 }
2735 if (LocaleCompare(expression,"r") == 0)
2736 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2737 break;
2738 }
2739 case 'S':
2740 case 's':
2741 {
2742 if (LocaleCompare(expression,"saturation") == 0)
2743 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2744 if (LocaleNCompare(expression,"sign",4) == 0)
2745 {
2746 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2747 exception);
2748 return(alpha < 0.0 ? -1.0 : 1.0);
2749 }
cristya6a09e72010-03-02 14:51:02 +00002750 if (LocaleNCompare(expression,"sinc",4) == 0)
2751 {
2752 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2753 exception);
2754 if (alpha == 0)
2755 return(1.0);
2756 gamma=(MagickRealType) (sin((double) (MagickPI*alpha))/
2757 (MagickPI*alpha));
2758 return(gamma);
2759 }
cristy3ed852e2009-09-05 21:47:34 +00002760 if (LocaleNCompare(expression,"sinh",4) == 0)
2761 {
2762 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2763 exception);
2764 return((MagickRealType) sinh((double) alpha));
2765 }
2766 if (LocaleNCompare(expression,"sin",3) == 0)
2767 {
2768 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2769 exception);
2770 return((MagickRealType) sin((double) alpha));
2771 }
2772 if (LocaleNCompare(expression,"sqrt",4) == 0)
2773 {
2774 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2775 exception);
2776 return((MagickRealType) sqrt((double) alpha));
2777 }
cristy9eeedea2011-11-02 19:04:05 +00002778 if (LocaleNCompare(expression,"squish",6) == 0)
2779 {
2780 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2781 exception);
cristyf7b28ea2012-04-27 11:22:49 +00002782 return((MagickRealType) (1.0/(1.0+exp((double) (-alpha)))));
cristy9eeedea2011-11-02 19:04:05 +00002783 }
cristy3ed852e2009-09-05 21:47:34 +00002784 if (LocaleCompare(expression,"s") == 0)
2785 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2786 break;
2787 }
2788 case 'T':
2789 case 't':
2790 {
2791 if (LocaleNCompare(expression,"tanh",4) == 0)
2792 {
2793 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2794 exception);
2795 return((MagickRealType) tanh((double) alpha));
2796 }
2797 if (LocaleNCompare(expression,"tan",3) == 0)
2798 {
2799 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2800 exception);
2801 return((MagickRealType) tan((double) alpha));
2802 }
2803 if (LocaleCompare(expression,"Transparent") == 0)
2804 return(0.0);
cristy16788e42010-08-13 13:44:26 +00002805 if (LocaleNCompare(expression,"trunc",5) == 0)
2806 {
2807 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2808 exception);
2809 if (alpha >= 0.0)
2810 return((MagickRealType) floor((double) alpha));
2811 return((MagickRealType) ceil((double) alpha));
2812 }
cristy3ed852e2009-09-05 21:47:34 +00002813 if (LocaleCompare(expression,"t") == 0)
2814 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2815 break;
2816 }
2817 case 'U':
2818 case 'u':
2819 {
2820 if (LocaleCompare(expression,"u") == 0)
2821 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2822 break;
2823 }
2824 case 'V':
2825 case 'v':
2826 {
2827 if (LocaleCompare(expression,"v") == 0)
2828 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2829 break;
2830 }
2831 case 'W':
2832 case 'w':
2833 {
cristy9eeedea2011-11-02 19:04:05 +00002834 if (LocaleNCompare(expression,"while",5) == 0)
2835 {
2836 do
2837 {
2838 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2839 exception);
2840 } while (fabs((double) alpha) >= MagickEpsilon);
2841 return((MagickRealType) *beta);
2842 }
cristy3ed852e2009-09-05 21:47:34 +00002843 if (LocaleCompare(expression,"w") == 0)
2844 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2845 break;
2846 }
2847 case 'Y':
2848 case 'y':
2849 {
2850 if (LocaleCompare(expression,"y") == 0)
2851 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2852 break;
2853 }
2854 case 'Z':
2855 case 'z':
2856 {
2857 if (LocaleCompare(expression,"z") == 0)
2858 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2859 break;
2860 }
2861 default:
2862 break;
2863 }
2864 q=(char *) expression;
cristydbdd0e32011-11-04 23:29:40 +00002865 alpha=InterpretSiPrefixValue(expression,&q);
cristy3ed852e2009-09-05 21:47:34 +00002866 if (q == expression)
2867 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2868 return(alpha);
2869}
2870
cristy7832dc22011-09-05 01:21:53 +00002871MagickPrivate MagickBooleanType FxEvaluateExpression(FxInfo *fx_info,
cristy3ed852e2009-09-05 21:47:34 +00002872 MagickRealType *alpha,ExceptionInfo *exception)
2873{
2874 MagickBooleanType
2875 status;
2876
cristy541ae572011-08-05 19:08:59 +00002877 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2878 exception);
cristy3ed852e2009-09-05 21:47:34 +00002879 return(status);
2880}
2881
2882MagickExport MagickBooleanType FxPreprocessExpression(FxInfo *fx_info,
2883 MagickRealType *alpha,ExceptionInfo *exception)
2884{
2885 FILE
2886 *file;
2887
2888 MagickBooleanType
2889 status;
2890
2891 file=fx_info->file;
2892 fx_info->file=(FILE *) NULL;
cristy541ae572011-08-05 19:08:59 +00002893 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2894 exception);
cristy3ed852e2009-09-05 21:47:34 +00002895 fx_info->file=file;
2896 return(status);
2897}
2898
cristy7832dc22011-09-05 01:21:53 +00002899MagickPrivate MagickBooleanType FxEvaluateChannelExpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002900 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristye85007d2010-06-06 22:51:36 +00002901 MagickRealType *alpha,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002902{
2903 MagickRealType
2904 beta;
2905
2906 beta=0.0;
2907 *alpha=FxEvaluateSubexpression(fx_info,channel,x,y,fx_info->expression,&beta,
2908 exception);
2909 return(exception->severity == OptionError ? MagickFalse : MagickTrue);
2910}
2911
2912/*
2913%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2914% %
2915% %
2916% %
2917% F x I m a g e %
2918% %
2919% %
2920% %
2921%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2922%
2923% FxImage() applies a mathematical expression to the specified image.
2924%
2925% The format of the FxImage method is:
2926%
2927% Image *FxImage(const Image *image,const char *expression,
2928% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002929%
2930% A description of each parameter follows:
2931%
2932% o image: the image.
2933%
cristy3ed852e2009-09-05 21:47:34 +00002934% o expression: A mathematical expression.
2935%
2936% o exception: return any errors or warnings in this structure.
2937%
2938*/
2939
2940static FxInfo **DestroyFxThreadSet(FxInfo **fx_info)
2941{
cristybb503372010-05-27 20:51:26 +00002942 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002943 i;
2944
2945 assert(fx_info != (FxInfo **) NULL);
cristyac245f82012-05-05 17:13:57 +00002946 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
cristy3ed852e2009-09-05 21:47:34 +00002947 if (fx_info[i] != (FxInfo *) NULL)
2948 fx_info[i]=DestroyFxInfo(fx_info[i]);
cristyb41ee102010-10-04 16:46:15 +00002949 fx_info=(FxInfo **) RelinquishMagickMemory(fx_info);
cristy3ed852e2009-09-05 21:47:34 +00002950 return(fx_info);
2951}
2952
2953static FxInfo **AcquireFxThreadSet(const Image *image,const char *expression,
2954 ExceptionInfo *exception)
2955{
2956 char
2957 *fx_expression;
2958
2959 FxInfo
2960 **fx_info;
2961
2962 MagickRealType
2963 alpha;
2964
cristybb503372010-05-27 20:51:26 +00002965 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002966 i;
2967
cristybb503372010-05-27 20:51:26 +00002968 size_t
cristy3ed852e2009-09-05 21:47:34 +00002969 number_threads;
2970
cristyfeeb98d2012-05-09 16:32:12 +00002971 number_threads=GetOpenMPMaximumThreads();
cristyb41ee102010-10-04 16:46:15 +00002972 fx_info=(FxInfo **) AcquireQuantumMemory(number_threads,sizeof(*fx_info));
cristy3ed852e2009-09-05 21:47:34 +00002973 if (fx_info == (FxInfo **) NULL)
2974 return((FxInfo **) NULL);
2975 (void) ResetMagickMemory(fx_info,0,number_threads*sizeof(*fx_info));
2976 if (*expression != '@')
2977 fx_expression=ConstantString(expression);
2978 else
2979 fx_expression=FileToString(expression+1,~0,exception);
cristybb503372010-05-27 20:51:26 +00002980 for (i=0; i < (ssize_t) number_threads; i++)
cristy3ed852e2009-09-05 21:47:34 +00002981 {
cristydb070952012-04-20 14:33:00 +00002982 fx_info[i]=AcquireFxInfo(image,fx_expression,exception);
cristy3ed852e2009-09-05 21:47:34 +00002983 if (fx_info[i] == (FxInfo *) NULL)
2984 return(DestroyFxThreadSet(fx_info));
2985 (void) FxPreprocessExpression(fx_info[i],&alpha,fx_info[i]->exception);
2986 }
2987 fx_expression=DestroyString(fx_expression);
2988 return(fx_info);
2989}
2990
2991MagickExport Image *FxImage(const Image *image,const char *expression,
2992 ExceptionInfo *exception)
2993{
cristy3ed852e2009-09-05 21:47:34 +00002994#define FxImageTag "Fx/Image"
2995
cristyfa112112010-01-04 17:48:07 +00002996 CacheView
cristy79cedc72011-07-25 00:41:15 +00002997 *fx_view,
2998 *image_view;
cristyfa112112010-01-04 17:48:07 +00002999
cristy3ed852e2009-09-05 21:47:34 +00003000 FxInfo
cristyfa112112010-01-04 17:48:07 +00003001 **restrict fx_info;
cristy3ed852e2009-09-05 21:47:34 +00003002
3003 Image
3004 *fx_image;
3005
cristy3ed852e2009-09-05 21:47:34 +00003006 MagickBooleanType
3007 status;
3008
cristybb503372010-05-27 20:51:26 +00003009 MagickOffsetType
3010 progress;
3011
cristy3ed852e2009-09-05 21:47:34 +00003012 MagickRealType
3013 alpha;
3014
cristybb503372010-05-27 20:51:26 +00003015 ssize_t
3016 y;
3017
cristy3ed852e2009-09-05 21:47:34 +00003018 assert(image != (Image *) NULL);
3019 assert(image->signature == MagickSignature);
3020 if (image->debug != MagickFalse)
3021 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy79cedc72011-07-25 00:41:15 +00003022 fx_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00003023 if (fx_image == (Image *) NULL)
3024 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003025 if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003026 {
cristy3ed852e2009-09-05 21:47:34 +00003027 fx_image=DestroyImage(fx_image);
3028 return((Image *) NULL);
3029 }
3030 fx_info=AcquireFxThreadSet(image,expression,exception);
3031 if (fx_info == (FxInfo **) NULL)
3032 {
3033 fx_image=DestroyImage(fx_image);
3034 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3035 }
3036 status=FxPreprocessExpression(fx_info[0],&alpha,exception);
3037 if (status == MagickFalse)
3038 {
3039 fx_image=DestroyImage(fx_image);
3040 fx_info=DestroyFxThreadSet(fx_info);
3041 return((Image *) NULL);
3042 }
3043 /*
3044 Fx image.
3045 */
3046 status=MagickTrue;
3047 progress=0;
cristydb070952012-04-20 14:33:00 +00003048 image_view=AcquireVirtualCacheView(image,exception);
3049 fx_view=AcquireAuthenticCacheView(fx_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00003050#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003051 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00003052 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003053#endif
cristybb503372010-05-27 20:51:26 +00003054 for (y=0; y < (ssize_t) fx_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003055 {
cristy5c9e6f22010-09-17 17:31:01 +00003056 const int
3057 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00003058
cristy79cedc72011-07-25 00:41:15 +00003059 register const Quantum
3060 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003061
cristy4c08aed2011-07-01 19:47:50 +00003062 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003063 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003064
cristy79cedc72011-07-25 00:41:15 +00003065 register ssize_t
3066 x;
3067
cristy3ed852e2009-09-05 21:47:34 +00003068 if (status == MagickFalse)
3069 continue;
cristy79cedc72011-07-25 00:41:15 +00003070 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +00003071 q=QueueCacheViewAuthenticPixels(fx_view,0,y,fx_image->columns,1,exception);
cristy79cedc72011-07-25 00:41:15 +00003072 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003073 {
3074 status=MagickFalse;
3075 continue;
3076 }
cristybb503372010-05-27 20:51:26 +00003077 for (x=0; x < (ssize_t) fx_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003078 {
cristy79cedc72011-07-25 00:41:15 +00003079 register ssize_t
3080 i;
3081
cristy177e41c2012-04-15 15:08:25 +00003082 if (GetPixelMask(image,p) != 0)
3083 {
3084 p+=GetPixelChannels(image);
3085 q+=GetPixelChannels(fx_image);
3086 continue;
3087 }
cristy79cedc72011-07-25 00:41:15 +00003088 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3089 {
3090 MagickRealType
3091 alpha;
3092
3093 PixelChannel
3094 channel;
3095
3096 PixelTrait
3097 fx_traits,
3098 traits;
3099
cristye2a912b2011-12-05 20:02:07 +00003100 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003101 traits=GetPixelChannelMapTraits(image,channel);
cristy79cedc72011-07-25 00:41:15 +00003102 fx_traits=GetPixelChannelMapTraits(fx_image,channel);
cristy010d7d12011-08-31 01:02:48 +00003103 if ((traits == UndefinedPixelTrait) ||
3104 (fx_traits == UndefinedPixelTrait))
cristy79cedc72011-07-25 00:41:15 +00003105 continue;
cristy177e41c2012-04-15 15:08:25 +00003106 if ((fx_traits & CopyPixelTrait) != 0)
cristy79cedc72011-07-25 00:41:15 +00003107 {
cristy0beccfa2011-09-25 20:47:53 +00003108 SetPixelChannel(fx_image,channel,p[i],q);
cristy79cedc72011-07-25 00:41:15 +00003109 continue;
3110 }
3111 alpha=0.0;
cristya382aca2011-12-06 18:22:48 +00003112 (void) FxEvaluateChannelExpression(fx_info[id],channel,x,y,&alpha,
3113 exception);
cristyb3a73b52011-07-26 01:34:43 +00003114 q[i]=ClampToQuantum((MagickRealType) QuantumRange*alpha);
cristy79cedc72011-07-25 00:41:15 +00003115 }
3116 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003117 q+=GetPixelChannels(fx_image);
cristy3ed852e2009-09-05 21:47:34 +00003118 }
3119 if (SyncCacheViewAuthenticPixels(fx_view,exception) == MagickFalse)
3120 status=MagickFalse;
3121 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3122 {
3123 MagickBooleanType
3124 proceed;
3125
cristyb5d5f722009-11-04 03:03:49 +00003126#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003127 #pragma omp critical (MagickCore_FxImage)
cristy3ed852e2009-09-05 21:47:34 +00003128#endif
3129 proceed=SetImageProgress(image,FxImageTag,progress++,image->rows);
3130 if (proceed == MagickFalse)
3131 status=MagickFalse;
3132 }
3133 }
cristy3ed852e2009-09-05 21:47:34 +00003134 fx_view=DestroyCacheView(fx_view);
cristy79cedc72011-07-25 00:41:15 +00003135 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003136 fx_info=DestroyFxThreadSet(fx_info);
3137 if (status == MagickFalse)
3138 fx_image=DestroyImage(fx_image);
3139 return(fx_image);
3140}
3141
3142/*
3143%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3144% %
3145% %
3146% %
3147% I m p l o d e I m a g e %
3148% %
3149% %
3150% %
3151%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3152%
3153% ImplodeImage() creates a new image that is a copy of an existing
3154% one with the image pixels "implode" by the specified percentage. It
3155% allocates the memory necessary for the new Image structure and returns a
3156% pointer to the new image.
3157%
3158% The format of the ImplodeImage method is:
3159%
3160% Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003161% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003162%
3163% A description of each parameter follows:
3164%
3165% o implode_image: Method ImplodeImage returns a pointer to the image
3166% after it is implode. A null image is returned if there is a memory
3167% shortage.
3168%
3169% o image: the image.
3170%
3171% o amount: Define the extent of the implosion.
3172%
cristy76f512e2011-09-12 01:26:56 +00003173% o method: the pixel interpolation method.
3174%
cristy3ed852e2009-09-05 21:47:34 +00003175% o exception: return any errors or warnings in this structure.
3176%
3177*/
3178MagickExport Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003179 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003180{
3181#define ImplodeImageTag "Implode/Image"
3182
cristyfa112112010-01-04 17:48:07 +00003183 CacheView
3184 *image_view,
3185 *implode_view;
3186
cristy3ed852e2009-09-05 21:47:34 +00003187 Image
3188 *implode_image;
3189
cristy3ed852e2009-09-05 21:47:34 +00003190 MagickBooleanType
3191 status;
3192
cristybb503372010-05-27 20:51:26 +00003193 MagickOffsetType
3194 progress;
3195
cristy3ed852e2009-09-05 21:47:34 +00003196 MagickRealType
3197 radius;
3198
3199 PointInfo
3200 center,
3201 scale;
3202
cristybb503372010-05-27 20:51:26 +00003203 ssize_t
3204 y;
3205
cristy3ed852e2009-09-05 21:47:34 +00003206 /*
3207 Initialize implode image attributes.
3208 */
3209 assert(image != (Image *) NULL);
3210 assert(image->signature == MagickSignature);
3211 if (image->debug != MagickFalse)
3212 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3213 assert(exception != (ExceptionInfo *) NULL);
3214 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00003215 implode_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3216 exception);
cristy3ed852e2009-09-05 21:47:34 +00003217 if (implode_image == (Image *) NULL)
3218 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003219 if (SetImageStorageClass(implode_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003220 {
cristy3ed852e2009-09-05 21:47:34 +00003221 implode_image=DestroyImage(implode_image);
3222 return((Image *) NULL);
3223 }
cristy4c08aed2011-07-01 19:47:50 +00003224 if (implode_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00003225 implode_image->matte=MagickTrue;
3226 /*
3227 Compute scaling factor.
3228 */
3229 scale.x=1.0;
3230 scale.y=1.0;
3231 center.x=0.5*image->columns;
3232 center.y=0.5*image->rows;
3233 radius=center.x;
3234 if (image->columns > image->rows)
3235 scale.y=(double) image->columns/(double) image->rows;
3236 else
3237 if (image->columns < image->rows)
3238 {
3239 scale.x=(double) image->rows/(double) image->columns;
3240 radius=center.y;
3241 }
3242 /*
3243 Implode image.
3244 */
3245 status=MagickTrue;
3246 progress=0;
cristydb070952012-04-20 14:33:00 +00003247 image_view=AcquireVirtualCacheView(image,exception);
3248 implode_view=AcquireAuthenticCacheView(implode_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00003249#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003250 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00003251 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003252#endif
cristybb503372010-05-27 20:51:26 +00003253 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003254 {
cristy3ed852e2009-09-05 21:47:34 +00003255 MagickRealType
3256 distance;
3257
3258 PointInfo
3259 delta;
3260
cristy6d188022011-09-12 13:23:33 +00003261 register const Quantum
3262 *restrict p;
3263
cristybb503372010-05-27 20:51:26 +00003264 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003265 x;
3266
cristy4c08aed2011-07-01 19:47:50 +00003267 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003268 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003269
3270 if (status == MagickFalse)
3271 continue;
cristy6d188022011-09-12 13:23:33 +00003272 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +00003273 q=QueueCacheViewAuthenticPixels(implode_view,0,y,implode_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003274 exception);
cristy6d188022011-09-12 13:23:33 +00003275 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003276 {
3277 status=MagickFalse;
3278 continue;
3279 }
cristy3ed852e2009-09-05 21:47:34 +00003280 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00003281 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003282 {
cristy6d188022011-09-12 13:23:33 +00003283 register ssize_t
3284 i;
3285
cristy3ed852e2009-09-05 21:47:34 +00003286 /*
3287 Determine if the pixel is within an ellipse.
3288 */
cristy10a6c612012-01-29 21:41:05 +00003289 if (GetPixelMask(image,p) != 0)
3290 {
3291 p+=GetPixelChannels(image);
3292 q+=GetPixelChannels(implode_image);
3293 continue;
3294 }
cristy3ed852e2009-09-05 21:47:34 +00003295 delta.x=scale.x*(double) (x-center.x);
3296 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00003297 if (distance >= (radius*radius))
cristya6d7a9b2012-01-18 20:04:48 +00003298 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy6d188022011-09-12 13:23:33 +00003299 {
cristya6d7a9b2012-01-18 20:04:48 +00003300 PixelChannel
3301 channel;
3302
cristy1707c6c2012-01-18 23:30:54 +00003303 PixelTrait
3304 implode_traits,
3305 traits;
3306
cristya6d7a9b2012-01-18 20:04:48 +00003307 channel=GetPixelChannelMapChannel(image,i);
cristy1707c6c2012-01-18 23:30:54 +00003308 traits=GetPixelChannelMapTraits(image,channel);
3309 implode_traits=GetPixelChannelMapTraits(implode_image,channel);
3310 if ((traits == UndefinedPixelTrait) ||
3311 (implode_traits == UndefinedPixelTrait))
3312 continue;
cristya6d7a9b2012-01-18 20:04:48 +00003313 SetPixelChannel(implode_image,channel,p[i],q);
cristy6d188022011-09-12 13:23:33 +00003314 }
3315 else
cristy3ed852e2009-09-05 21:47:34 +00003316 {
3317 double
3318 factor;
3319
3320 /*
3321 Implode the pixel.
3322 */
3323 factor=1.0;
3324 if (distance > 0.0)
cristy1707c6c2012-01-18 23:30:54 +00003325 factor=pow(sin((double) (MagickPI*sqrt((double) distance)/radius/
3326 2)),-amount);
cristy76f512e2011-09-12 01:26:56 +00003327 status=InterpolatePixelChannels(image,image_view,implode_image,method,
3328 (double) (factor*delta.x/scale.x+center.x),(double) (factor*delta.y/
3329 scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00003330 }
cristy6d188022011-09-12 13:23:33 +00003331 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003332 q+=GetPixelChannels(implode_image);
cristy3ed852e2009-09-05 21:47:34 +00003333 }
3334 if (SyncCacheViewAuthenticPixels(implode_view,exception) == MagickFalse)
3335 status=MagickFalse;
3336 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3337 {
3338 MagickBooleanType
3339 proceed;
3340
cristyb5d5f722009-11-04 03:03:49 +00003341#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003342 #pragma omp critical (MagickCore_ImplodeImage)
cristy3ed852e2009-09-05 21:47:34 +00003343#endif
3344 proceed=SetImageProgress(image,ImplodeImageTag,progress++,image->rows);
3345 if (proceed == MagickFalse)
3346 status=MagickFalse;
3347 }
3348 }
3349 implode_view=DestroyCacheView(implode_view);
3350 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003351 if (status == MagickFalse)
3352 implode_image=DestroyImage(implode_image);
3353 return(implode_image);
3354}
3355
3356/*
3357%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3358% %
3359% %
3360% %
3361% M o r p h I m a g e s %
3362% %
3363% %
3364% %
3365%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3366%
3367% The MorphImages() method requires a minimum of two images. The first
3368% image is transformed into the second by a number of intervening images
3369% as specified by frames.
3370%
3371% The format of the MorphImage method is:
3372%
cristybb503372010-05-27 20:51:26 +00003373% Image *MorphImages(const Image *image,const size_t number_frames,
cristy3ed852e2009-09-05 21:47:34 +00003374% ExceptionInfo *exception)
3375%
3376% A description of each parameter follows:
3377%
3378% o image: the image.
3379%
3380% o number_frames: Define the number of in-between image to generate.
3381% The more in-between frames, the smoother the morph.
3382%
3383% o exception: return any errors or warnings in this structure.
3384%
3385*/
3386MagickExport Image *MorphImages(const Image *image,
cristybb503372010-05-27 20:51:26 +00003387 const size_t number_frames,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003388{
3389#define MorphImageTag "Morph/Image"
3390
3391 Image
3392 *morph_image,
3393 *morph_images;
3394
cristy9d314ff2011-03-09 01:30:28 +00003395 MagickBooleanType
3396 status;
cristy3ed852e2009-09-05 21:47:34 +00003397
3398 MagickOffsetType
3399 scene;
3400
3401 MagickRealType
3402 alpha,
3403 beta;
3404
3405 register const Image
3406 *next;
3407
cristybb503372010-05-27 20:51:26 +00003408 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003409 i;
3410
cristy9d314ff2011-03-09 01:30:28 +00003411 ssize_t
3412 y;
cristy3ed852e2009-09-05 21:47:34 +00003413
3414 /*
3415 Clone first frame in sequence.
3416 */
3417 assert(image != (Image *) NULL);
3418 assert(image->signature == MagickSignature);
3419 if (image->debug != MagickFalse)
3420 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3421 assert(exception != (ExceptionInfo *) NULL);
3422 assert(exception->signature == MagickSignature);
3423 morph_images=CloneImage(image,0,0,MagickTrue,exception);
3424 if (morph_images == (Image *) NULL)
3425 return((Image *) NULL);
3426 if (GetNextImageInList(image) == (Image *) NULL)
3427 {
3428 /*
3429 Morph single image.
3430 */
cristybb503372010-05-27 20:51:26 +00003431 for (i=1; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003432 {
3433 morph_image=CloneImage(image,0,0,MagickTrue,exception);
3434 if (morph_image == (Image *) NULL)
3435 {
3436 morph_images=DestroyImageList(morph_images);
3437 return((Image *) NULL);
3438 }
3439 AppendImageToList(&morph_images,morph_image);
cristy8b27a6d2010-02-14 03:31:15 +00003440 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003441 {
cristy8b27a6d2010-02-14 03:31:15 +00003442 MagickBooleanType
3443 proceed;
3444
cristybb503372010-05-27 20:51:26 +00003445 proceed=SetImageProgress(image,MorphImageTag,(MagickOffsetType) i,
3446 number_frames);
cristy8b27a6d2010-02-14 03:31:15 +00003447 if (proceed == MagickFalse)
3448 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00003449 }
3450 }
3451 return(GetFirstImageInList(morph_images));
3452 }
3453 /*
3454 Morph image sequence.
3455 */
3456 status=MagickTrue;
3457 scene=0;
3458 next=image;
3459 for ( ; GetNextImageInList(next) != (Image *) NULL; next=GetNextImageInList(next))
3460 {
cristybb503372010-05-27 20:51:26 +00003461 for (i=0; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003462 {
3463 CacheView
3464 *image_view,
3465 *morph_view;
3466
3467 beta=(MagickRealType) (i+1.0)/(MagickRealType) (number_frames+1.0);
3468 alpha=1.0-beta;
cristy15b98cd2010-09-12 19:42:50 +00003469 morph_image=ResizeImage(next,(size_t) (alpha*next->columns+beta*
cristyaa2c16c2012-03-25 22:21:35 +00003470 GetNextImageInList(next)->columns+0.5),(size_t) (alpha*next->rows+beta*
3471 GetNextImageInList(next)->rows+0.5),next->filter,exception);
cristy3ed852e2009-09-05 21:47:34 +00003472 if (morph_image == (Image *) NULL)
3473 {
3474 morph_images=DestroyImageList(morph_images);
3475 return((Image *) NULL);
3476 }
cristy1707c6c2012-01-18 23:30:54 +00003477 status=SetImageStorageClass(morph_image,DirectClass,exception);
3478 if (status == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003479 {
cristy3ed852e2009-09-05 21:47:34 +00003480 morph_image=DestroyImage(morph_image);
3481 return((Image *) NULL);
3482 }
3483 AppendImageToList(&morph_images,morph_image);
3484 morph_images=GetLastImageInList(morph_images);
cristy15b98cd2010-09-12 19:42:50 +00003485 morph_image=ResizeImage(GetNextImageInList(next),morph_images->columns,
cristyaa2c16c2012-03-25 22:21:35 +00003486 morph_images->rows,GetNextImageInList(next)->filter,exception);
cristy3ed852e2009-09-05 21:47:34 +00003487 if (morph_image == (Image *) NULL)
3488 {
3489 morph_images=DestroyImageList(morph_images);
3490 return((Image *) NULL);
3491 }
cristydb070952012-04-20 14:33:00 +00003492 image_view=AcquireVirtualCacheView(morph_image,exception);
3493 morph_view=AcquireAuthenticCacheView(morph_images,exception);
cristyb5d5f722009-11-04 03:03:49 +00003494#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003495 #pragma omp parallel for schedule(static,4) shared(status) \
cristy4ee2b0c2012-05-15 00:30:35 +00003496 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003497#endif
cristybb503372010-05-27 20:51:26 +00003498 for (y=0; y < (ssize_t) morph_images->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003499 {
3500 MagickBooleanType
3501 sync;
3502
cristy4c08aed2011-07-01 19:47:50 +00003503 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003504 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003505
cristybb503372010-05-27 20:51:26 +00003506 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003507 x;
3508
cristy4c08aed2011-07-01 19:47:50 +00003509 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003510 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003511
3512 if (status == MagickFalse)
3513 continue;
3514 p=GetCacheViewVirtualPixels(image_view,0,y,morph_image->columns,1,
3515 exception);
3516 q=GetCacheViewAuthenticPixels(morph_view,0,y,morph_images->columns,1,
3517 exception);
cristy4c08aed2011-07-01 19:47:50 +00003518 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003519 {
3520 status=MagickFalse;
3521 continue;
3522 }
cristybb503372010-05-27 20:51:26 +00003523 for (x=0; x < (ssize_t) morph_images->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003524 {
cristy1707c6c2012-01-18 23:30:54 +00003525 register ssize_t
3526 i;
3527
cristy177e41c2012-04-15 15:08:25 +00003528 if (GetPixelMask(image,p) != 0)
3529 {
3530 p+=GetPixelChannels(image);
3531 q+=GetPixelChannels(morph_image);
3532 continue;
3533 }
cristy10a6c612012-01-29 21:41:05 +00003534 for (i=0; i < (ssize_t) GetPixelChannels(morph_image); i++)
cristy1707c6c2012-01-18 23:30:54 +00003535 {
3536 PixelChannel
3537 channel;
3538
3539 PixelTrait
3540 morph_traits,
3541 traits;
3542
3543 channel=GetPixelChannelMapChannel(image,i);
3544 traits=GetPixelChannelMapTraits(image,channel);
3545 morph_traits=GetPixelChannelMapTraits(morph_image,channel);
3546 if ((traits == UndefinedPixelTrait) ||
3547 (morph_traits == UndefinedPixelTrait))
3548 continue;
cristy177e41c2012-04-15 15:08:25 +00003549 if ((morph_traits & CopyPixelTrait) != 0)
cristy1707c6c2012-01-18 23:30:54 +00003550 {
3551 SetPixelChannel(morph_image,channel,p[i],q);
3552 continue;
3553 }
3554 SetPixelChannel(morph_image,channel,ClampToQuantum(alpha*
3555 GetPixelChannel(morph_images,channel,q)+beta*p[i]),q);
3556 }
cristyed231572011-07-14 02:18:59 +00003557 p+=GetPixelChannels(morph_image);
3558 q+=GetPixelChannels(morph_images);
cristy3ed852e2009-09-05 21:47:34 +00003559 }
3560 sync=SyncCacheViewAuthenticPixels(morph_view,exception);
3561 if (sync == MagickFalse)
3562 status=MagickFalse;
3563 }
3564 morph_view=DestroyCacheView(morph_view);
3565 image_view=DestroyCacheView(image_view);
3566 morph_image=DestroyImage(morph_image);
3567 }
cristybb503372010-05-27 20:51:26 +00003568 if (i < (ssize_t) number_frames)
cristy3ed852e2009-09-05 21:47:34 +00003569 break;
3570 /*
3571 Clone last frame in sequence.
3572 */
3573 morph_image=CloneImage(GetNextImageInList(next),0,0,MagickTrue,exception);
3574 if (morph_image == (Image *) NULL)
3575 {
3576 morph_images=DestroyImageList(morph_images);
3577 return((Image *) NULL);
3578 }
3579 AppendImageToList(&morph_images,morph_image);
3580 morph_images=GetLastImageInList(morph_images);
3581 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3582 {
3583 MagickBooleanType
3584 proceed;
3585
cristyb5d5f722009-11-04 03:03:49 +00003586#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003587 #pragma omp critical (MagickCore_MorphImages)
cristy3ed852e2009-09-05 21:47:34 +00003588#endif
3589 proceed=SetImageProgress(image,MorphImageTag,scene,
3590 GetImageListLength(image));
3591 if (proceed == MagickFalse)
3592 status=MagickFalse;
3593 }
3594 scene++;
3595 }
3596 if (GetNextImageInList(next) != (Image *) NULL)
3597 {
3598 morph_images=DestroyImageList(morph_images);
3599 return((Image *) NULL);
3600 }
3601 return(GetFirstImageInList(morph_images));
3602}
3603
3604/*
3605%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3606% %
3607% %
3608% %
3609% P l a s m a I m a g e %
3610% %
3611% %
3612% %
3613%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3614%
3615% PlasmaImage() initializes an image with plasma fractal values. The image
3616% must be initialized with a base color and the random number generator
3617% seeded before this method is called.
3618%
3619% The format of the PlasmaImage method is:
3620%
3621% MagickBooleanType PlasmaImage(Image *image,const SegmentInfo *segment,
cristy5cbc0162011-08-29 00:36:28 +00003622% size_t attenuate,size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003623%
3624% A description of each parameter follows:
3625%
3626% o image: the image.
3627%
3628% o segment: Define the region to apply plasma fractals values.
3629%
glennrp7dae1ca2010-09-16 12:17:35 +00003630% o attenuate: Define the plasma attenuation factor.
cristy3ed852e2009-09-05 21:47:34 +00003631%
3632% o depth: Limit the plasma recursion depth.
3633%
cristy5cbc0162011-08-29 00:36:28 +00003634% o exception: return any errors or warnings in this structure.
3635%
cristy3ed852e2009-09-05 21:47:34 +00003636*/
3637
3638static inline Quantum PlasmaPixel(RandomInfo *random_info,
3639 const MagickRealType pixel,const MagickRealType noise)
3640{
3641 Quantum
3642 plasma;
3643
cristyce70c172010-01-07 17:15:30 +00003644 plasma=ClampToQuantum(pixel+noise*GetPseudoRandomValue(random_info)-
cristy3ed852e2009-09-05 21:47:34 +00003645 noise/2.0);
3646 return(plasma);
3647}
3648
cristyda1f9c12011-10-02 21:39:49 +00003649static MagickBooleanType PlasmaImageProxy(Image *image,CacheView *image_view,
3650 CacheView *u_view,CacheView *v_view,RandomInfo *random_info,
3651 const SegmentInfo *segment,size_t attenuate,size_t depth,
3652 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003653{
cristy3ed852e2009-09-05 21:47:34 +00003654 MagickRealType
3655 plasma;
3656
cristyda1f9c12011-10-02 21:39:49 +00003657 PixelChannel
3658 channel;
3659
3660 PixelTrait
3661 traits;
3662
3663 register const Quantum
3664 *restrict u,
3665 *restrict v;
3666
3667 register Quantum
3668 *restrict q;
3669
3670 register ssize_t
3671 i;
cristy3ed852e2009-09-05 21:47:34 +00003672
cristy9d314ff2011-03-09 01:30:28 +00003673 ssize_t
3674 x,
3675 x_mid,
3676 y,
3677 y_mid;
3678
cristy3ed852e2009-09-05 21:47:34 +00003679 if (((segment->x2-segment->x1) == 0.0) && ((segment->y2-segment->y1) == 0.0))
3680 return(MagickTrue);
3681 if (depth != 0)
3682 {
3683 SegmentInfo
3684 local_info;
3685
3686 /*
3687 Divide the area into quadrants and recurse.
3688 */
3689 depth--;
3690 attenuate++;
cristybb503372010-05-27 20:51:26 +00003691 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3692 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003693 local_info=(*segment);
3694 local_info.x2=(double) x_mid;
3695 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003696 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3697 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003698 local_info=(*segment);
3699 local_info.y1=(double) y_mid;
3700 local_info.x2=(double) x_mid;
cristyda1f9c12011-10-02 21:39:49 +00003701 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3702 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003703 local_info=(*segment);
3704 local_info.x1=(double) x_mid;
3705 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003706 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3707 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003708 local_info=(*segment);
3709 local_info.x1=(double) x_mid;
3710 local_info.y1=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003711 return(PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3712 &local_info,attenuate,depth,exception));
cristy3ed852e2009-09-05 21:47:34 +00003713 }
cristybb503372010-05-27 20:51:26 +00003714 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3715 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003716 if ((segment->x1 == (double) x_mid) && (segment->x2 == (double) x_mid) &&
3717 (segment->y1 == (double) y_mid) && (segment->y2 == (double) y_mid))
3718 return(MagickFalse);
3719 /*
3720 Average pixels and apply plasma.
3721 */
cristy3ed852e2009-09-05 21:47:34 +00003722 plasma=(MagickRealType) QuantumRange/(2.0*attenuate);
3723 if ((segment->x1 != (double) x_mid) || (segment->x2 != (double) x_mid))
3724 {
cristy3ed852e2009-09-05 21:47:34 +00003725 /*
3726 Left pixel.
3727 */
cristybb503372010-05-27 20:51:26 +00003728 x=(ssize_t) ceil(segment->x1-0.5);
cristy1707c6c2012-01-18 23:30:54 +00003729 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),1,1,
3730 exception);
3731 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),1,1,
3732 exception);
cristyc5c6f662010-09-22 14:23:02 +00003733 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003734 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3735 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003736 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003737 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3738 {
cristye2a912b2011-12-05 20:02:07 +00003739 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003740 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003741 if (traits == UndefinedPixelTrait)
3742 continue;
3743 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3744 }
cristyc5c6f662010-09-22 14:23:02 +00003745 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003746 if (segment->x1 != segment->x2)
3747 {
3748 /*
3749 Right pixel.
3750 */
cristybb503372010-05-27 20:51:26 +00003751 x=(ssize_t) ceil(segment->x2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003752 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3753 1,1,exception);
3754 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3755 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003756 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003757 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3758 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003759 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003760 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3761 {
cristye2a912b2011-12-05 20:02:07 +00003762 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003763 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003764 if (traits == UndefinedPixelTrait)
3765 continue;
3766 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3767 }
cristyc5c6f662010-09-22 14:23:02 +00003768 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003769 }
3770 }
3771 if ((segment->y1 != (double) y_mid) || (segment->y2 != (double) y_mid))
3772 {
3773 if ((segment->x1 != (double) x_mid) || (segment->y2 != (double) y_mid))
3774 {
cristy3ed852e2009-09-05 21:47:34 +00003775 /*
3776 Bottom pixel.
3777 */
cristybb503372010-05-27 20:51:26 +00003778 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003779 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3780 1,1,exception);
3781 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3782 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003783 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003784 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3785 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003786 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003787 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3788 {
cristye2a912b2011-12-05 20:02:07 +00003789 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003790 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003791 if (traits == UndefinedPixelTrait)
3792 continue;
3793 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3794 }
cristyc5c6f662010-09-22 14:23:02 +00003795 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003796 }
3797 if (segment->y1 != segment->y2)
3798 {
cristy3ed852e2009-09-05 21:47:34 +00003799 /*
3800 Top pixel.
3801 */
cristybb503372010-05-27 20:51:26 +00003802 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003803 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3804 1,1,exception);
3805 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3806 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003807 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003808 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3809 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003810 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003811 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3812 {
cristye2a912b2011-12-05 20:02:07 +00003813 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003814 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003815 if (traits == UndefinedPixelTrait)
3816 continue;
3817 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3818 }
cristyc5c6f662010-09-22 14:23:02 +00003819 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003820 }
3821 }
3822 if ((segment->x1 != segment->x2) || (segment->y1 != segment->y2))
3823 {
cristy3ed852e2009-09-05 21:47:34 +00003824 /*
3825 Middle pixel.
3826 */
cristybb503372010-05-27 20:51:26 +00003827 x=(ssize_t) ceil(segment->x1-0.5);
3828 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003829 u=GetCacheViewVirtualPixels(u_view,x,y,1,1,exception);
cristybb503372010-05-27 20:51:26 +00003830 x=(ssize_t) ceil(segment->x2-0.5);
3831 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003832 v=GetCacheViewVirtualPixels(v_view,x,y,1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003833 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003834 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3835 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003836 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003837 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3838 {
cristye2a912b2011-12-05 20:02:07 +00003839 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003840 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003841 if (traits == UndefinedPixelTrait)
3842 continue;
3843 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3844 }
cristyc5c6f662010-09-22 14:23:02 +00003845 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003846 }
3847 if (((segment->x2-segment->x1) < 3.0) && ((segment->y2-segment->y1) < 3.0))
3848 return(MagickTrue);
3849 return(MagickFalse);
3850}
cristyda1f9c12011-10-02 21:39:49 +00003851
cristy3ed852e2009-09-05 21:47:34 +00003852MagickExport MagickBooleanType PlasmaImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00003853 const SegmentInfo *segment,size_t attenuate,size_t depth,
3854 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003855{
cristyc5c6f662010-09-22 14:23:02 +00003856 CacheView
cristyda1f9c12011-10-02 21:39:49 +00003857 *image_view,
3858 *u_view,
3859 *v_view;
cristyc5c6f662010-09-22 14:23:02 +00003860
cristy3ed852e2009-09-05 21:47:34 +00003861 MagickBooleanType
3862 status;
3863
3864 RandomInfo
3865 *random_info;
3866
3867 if (image->debug != MagickFalse)
3868 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3869 assert(image != (Image *) NULL);
3870 assert(image->signature == MagickSignature);
3871 if (image->debug != MagickFalse)
3872 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
cristy5cbc0162011-08-29 00:36:28 +00003873 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristyc5c6f662010-09-22 14:23:02 +00003874 return(MagickFalse);
cristydb070952012-04-20 14:33:00 +00003875 image_view=AcquireAuthenticCacheView(image,exception);
3876 u_view=AcquireVirtualCacheView(image,exception);
3877 v_view=AcquireVirtualCacheView(image,exception);
cristy3ed852e2009-09-05 21:47:34 +00003878 random_info=AcquireRandomInfo();
cristyda1f9c12011-10-02 21:39:49 +00003879 status=PlasmaImageProxy(image,image_view,u_view,v_view,random_info,segment,
3880 attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003881 random_info=DestroyRandomInfo(random_info);
cristyda1f9c12011-10-02 21:39:49 +00003882 v_view=DestroyCacheView(v_view);
3883 u_view=DestroyCacheView(u_view);
cristyc5c6f662010-09-22 14:23:02 +00003884 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003885 return(status);
3886}
3887
3888/*
3889%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3890% %
3891% %
3892% %
3893% P o l a r o i d I m a g e %
3894% %
3895% %
3896% %
3897%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3898%
3899% PolaroidImage() simulates a Polaroid picture.
3900%
3901% The format of the AnnotateImage method is:
3902%
3903% Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristye9e3d382011-12-14 01:50:13 +00003904% const char *caption,const double angle,
3905% const PixelInterpolateMethod method,ExceptionInfo exception)
cristy3ed852e2009-09-05 21:47:34 +00003906%
3907% A description of each parameter follows:
3908%
3909% o image: the image.
3910%
3911% o draw_info: the draw info.
3912%
cristye9e3d382011-12-14 01:50:13 +00003913% o caption: the Polaroid caption.
3914%
cristycee97112010-05-28 00:44:52 +00003915% o angle: Apply the effect along this angle.
cristy3ed852e2009-09-05 21:47:34 +00003916%
cristy5c4e2582011-09-11 19:21:03 +00003917% o method: the pixel interpolation method.
3918%
cristy3ed852e2009-09-05 21:47:34 +00003919% o exception: return any errors or warnings in this structure.
3920%
3921*/
3922MagickExport Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristye9e3d382011-12-14 01:50:13 +00003923 const char *caption,const double angle,const PixelInterpolateMethod method,
cristy5c4e2582011-09-11 19:21:03 +00003924 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003925{
cristy3ed852e2009-09-05 21:47:34 +00003926 Image
3927 *bend_image,
3928 *caption_image,
3929 *flop_image,
3930 *picture_image,
3931 *polaroid_image,
3932 *rotate_image,
3933 *trim_image;
3934
cristybb503372010-05-27 20:51:26 +00003935 size_t
cristy3ed852e2009-09-05 21:47:34 +00003936 height;
3937
cristy9d314ff2011-03-09 01:30:28 +00003938 ssize_t
3939 quantum;
3940
cristy3ed852e2009-09-05 21:47:34 +00003941 /*
3942 Simulate a Polaroid picture.
3943 */
3944 assert(image != (Image *) NULL);
3945 assert(image->signature == MagickSignature);
3946 if (image->debug != MagickFalse)
3947 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3948 assert(exception != (ExceptionInfo *) NULL);
3949 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00003950 quantum=(ssize_t) MagickMax(MagickMax((double) image->columns,(double)
cristy3ed852e2009-09-05 21:47:34 +00003951 image->rows)/25.0,10.0);
3952 height=image->rows+2*quantum;
3953 caption_image=(Image *) NULL;
cristye9e3d382011-12-14 01:50:13 +00003954 if (caption != (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003955 {
3956 char
cristye9e3d382011-12-14 01:50:13 +00003957 geometry[MaxTextExtent],
3958 *text;
cristy3ed852e2009-09-05 21:47:34 +00003959
3960 DrawInfo
3961 *annotate_info;
3962
cristy3ed852e2009-09-05 21:47:34 +00003963 MagickBooleanType
3964 status;
3965
cristy9d314ff2011-03-09 01:30:28 +00003966 ssize_t
3967 count;
3968
cristy3ed852e2009-09-05 21:47:34 +00003969 TypeMetric
3970 metrics;
3971
3972 /*
3973 Generate caption image.
3974 */
3975 caption_image=CloneImage(image,image->columns,1,MagickTrue,exception);
3976 if (caption_image == (Image *) NULL)
3977 return((Image *) NULL);
3978 annotate_info=CloneDrawInfo((const ImageInfo *) NULL,draw_info);
cristye9e3d382011-12-14 01:50:13 +00003979 text=InterpretImageProperties((ImageInfo *) NULL,(Image *) image,caption,
3980 exception);
3981 (void) CloneString(&annotate_info->text,text);
cristy6b1d05e2010-09-22 19:17:27 +00003982 count=FormatMagickCaption(caption_image,annotate_info,MagickTrue,&metrics,
cristye9e3d382011-12-14 01:50:13 +00003983 &text,exception);
3984 status=SetImageExtent(caption_image,image->columns,(size_t) ((count+1)*
3985 (metrics.ascent-metrics.descent)+0.5),exception);
cristy3ed852e2009-09-05 21:47:34 +00003986 if (status == MagickFalse)
3987 caption_image=DestroyImage(caption_image);
3988 else
3989 {
3990 caption_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00003991 (void) SetImageBackgroundColor(caption_image,exception);
cristye9e3d382011-12-14 01:50:13 +00003992 (void) CloneString(&annotate_info->text,text);
cristyb51dff52011-05-19 16:55:47 +00003993 (void) FormatLocaleString(geometry,MaxTextExtent,"+0+%g",
cristy3ed852e2009-09-05 21:47:34 +00003994 metrics.ascent);
3995 if (annotate_info->gravity == UndefinedGravity)
3996 (void) CloneString(&annotate_info->geometry,AcquireString(
3997 geometry));
cristy5cbc0162011-08-29 00:36:28 +00003998 (void) AnnotateImage(caption_image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00003999 height+=caption_image->rows;
4000 }
4001 annotate_info=DestroyDrawInfo(annotate_info);
cristye9e3d382011-12-14 01:50:13 +00004002 text=DestroyString(text);
cristy3ed852e2009-09-05 21:47:34 +00004003 }
4004 picture_image=CloneImage(image,image->columns+2*quantum,height,MagickTrue,
4005 exception);
4006 if (picture_image == (Image *) NULL)
4007 {
4008 if (caption_image != (Image *) NULL)
4009 caption_image=DestroyImage(caption_image);
4010 return((Image *) NULL);
4011 }
4012 picture_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00004013 (void) SetImageBackgroundColor(picture_image,exception);
cristy39172402012-03-30 13:04:39 +00004014 (void) CompositeImage(picture_image,image,OverCompositeOp,MagickTrue,quantum,
cristyfeb3e962012-03-29 17:25:55 +00004015 quantum,exception);
cristy3ed852e2009-09-05 21:47:34 +00004016 if (caption_image != (Image *) NULL)
4017 {
cristyfeb3e962012-03-29 17:25:55 +00004018 (void) CompositeImage(picture_image,caption_image,OverCompositeOp,
cristy39172402012-03-30 13:04:39 +00004019 MagickTrue,quantum,(ssize_t) (image->rows+3*quantum/2),exception);
cristy3ed852e2009-09-05 21:47:34 +00004020 caption_image=DestroyImage(caption_image);
4021 }
cristy9950d572011-10-01 18:22:35 +00004022 (void) QueryColorCompliance("none",AllCompliance,
4023 &picture_image->background_color,exception);
cristy63240882011-08-05 19:05:27 +00004024 (void) SetImageAlphaChannel(picture_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004025 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 bend_image=WaveImage(picture_image,0.01*picture_image->rows,2.0*
cristy5c4e2582011-09-11 19:21:03 +00004031 picture_image->columns,method,exception);
cristy3ed852e2009-09-05 21:47:34 +00004032 picture_image=DestroyImage(picture_image);
4033 if (bend_image == (Image *) NULL)
4034 return((Image *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00004035 picture_image=bend_image;
4036 rotate_image=RotateImage(picture_image,-90.0,exception);
4037 picture_image=DestroyImage(picture_image);
4038 if (rotate_image == (Image *) NULL)
4039 return((Image *) NULL);
4040 picture_image=rotate_image;
4041 picture_image->background_color=image->background_color;
anthonyf46d4262012-03-26 03:30:34 +00004042 polaroid_image=ShadowImage(picture_image,80.0,2.0,quantum/3,quantum/3,
cristy3ed852e2009-09-05 21:47:34 +00004043 exception);
4044 if (polaroid_image == (Image *) NULL)
4045 {
4046 picture_image=DestroyImage(picture_image);
4047 return(picture_image);
4048 }
4049 flop_image=FlopImage(polaroid_image,exception);
4050 polaroid_image=DestroyImage(polaroid_image);
4051 if (flop_image == (Image *) NULL)
4052 {
4053 picture_image=DestroyImage(picture_image);
4054 return(picture_image);
4055 }
4056 polaroid_image=flop_image;
cristyfeb3e962012-03-29 17:25:55 +00004057 (void) CompositeImage(polaroid_image,picture_image,OverCompositeOp,
cristy39172402012-03-30 13:04:39 +00004058 MagickTrue,(ssize_t) (-0.01*picture_image->columns/2.0),0L,exception);
cristy3ed852e2009-09-05 21:47:34 +00004059 picture_image=DestroyImage(picture_image);
cristy9950d572011-10-01 18:22:35 +00004060 (void) QueryColorCompliance("none",AllCompliance,
4061 &polaroid_image->background_color,exception);
cristy3ed852e2009-09-05 21:47:34 +00004062 rotate_image=RotateImage(polaroid_image,angle,exception);
4063 polaroid_image=DestroyImage(polaroid_image);
4064 if (rotate_image == (Image *) NULL)
4065 return((Image *) NULL);
4066 polaroid_image=rotate_image;
4067 trim_image=TrimImage(polaroid_image,exception);
4068 polaroid_image=DestroyImage(polaroid_image);
4069 if (trim_image == (Image *) NULL)
4070 return((Image *) NULL);
4071 polaroid_image=trim_image;
4072 return(polaroid_image);
4073}
4074
4075/*
4076%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4077% %
4078% %
4079% %
cristy3ed852e2009-09-05 21:47:34 +00004080% S e p i a T o n e I m a g e %
4081% %
4082% %
4083% %
4084%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4085%
4086% MagickSepiaToneImage() applies a special effect to the image, similar to the
4087% effect achieved in a photo darkroom by sepia toning. Threshold ranges from
4088% 0 to QuantumRange and is a measure of the extent of the sepia toning. A
4089% threshold of 80% is a good starting point for a reasonable tone.
4090%
4091% The format of the SepiaToneImage method is:
4092%
4093% Image *SepiaToneImage(const Image *image,const double threshold,
4094% ExceptionInfo *exception)
4095%
4096% A description of each parameter follows:
4097%
4098% o image: the image.
4099%
4100% o threshold: the tone threshold.
4101%
4102% o exception: return any errors or warnings in this structure.
4103%
4104*/
4105MagickExport Image *SepiaToneImage(const Image *image,const double threshold,
4106 ExceptionInfo *exception)
4107{
4108#define SepiaToneImageTag "SepiaTone/Image"
4109
cristyc4c8d132010-01-07 01:58:38 +00004110 CacheView
4111 *image_view,
4112 *sepia_view;
4113
cristy3ed852e2009-09-05 21:47:34 +00004114 Image
4115 *sepia_image;
4116
cristy3ed852e2009-09-05 21:47:34 +00004117 MagickBooleanType
4118 status;
4119
cristybb503372010-05-27 20:51:26 +00004120 MagickOffsetType
4121 progress;
4122
4123 ssize_t
4124 y;
4125
cristy3ed852e2009-09-05 21:47:34 +00004126 /*
4127 Initialize sepia-toned image attributes.
4128 */
4129 assert(image != (const Image *) NULL);
4130 assert(image->signature == MagickSignature);
4131 if (image->debug != MagickFalse)
4132 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4133 assert(exception != (ExceptionInfo *) NULL);
4134 assert(exception->signature == MagickSignature);
cristy1707c6c2012-01-18 23:30:54 +00004135 sepia_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004136 if (sepia_image == (Image *) NULL)
4137 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004138 if (SetImageStorageClass(sepia_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004139 {
cristy3ed852e2009-09-05 21:47:34 +00004140 sepia_image=DestroyImage(sepia_image);
4141 return((Image *) NULL);
4142 }
4143 /*
4144 Tone each row of the image.
4145 */
4146 status=MagickTrue;
4147 progress=0;
cristydb070952012-04-20 14:33:00 +00004148 image_view=AcquireVirtualCacheView(image,exception);
4149 sepia_view=AcquireAuthenticCacheView(sepia_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00004150#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00004151 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00004152 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00004153#endif
cristybb503372010-05-27 20:51:26 +00004154 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004155 {
cristy4c08aed2011-07-01 19:47:50 +00004156 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004157 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00004158
cristybb503372010-05-27 20:51:26 +00004159 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004160 x;
4161
cristy4c08aed2011-07-01 19:47:50 +00004162 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004163 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004164
4165 if (status == MagickFalse)
4166 continue;
4167 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy1707c6c2012-01-18 23:30:54 +00004168 q=GetCacheViewAuthenticPixels(sepia_view,0,y,sepia_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00004169 exception);
cristy4c08aed2011-07-01 19:47:50 +00004170 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004171 {
4172 status=MagickFalse;
4173 continue;
4174 }
cristybb503372010-05-27 20:51:26 +00004175 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004176 {
4177 MagickRealType
4178 intensity,
4179 tone;
4180
cristy4c08aed2011-07-01 19:47:50 +00004181 intensity=(MagickRealType) GetPixelIntensity(image,p);
cristy3ed852e2009-09-05 21:47:34 +00004182 tone=intensity > threshold ? (MagickRealType) QuantumRange : intensity+
4183 (MagickRealType) QuantumRange-threshold;
cristy4c08aed2011-07-01 19:47:50 +00004184 SetPixelRed(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004185 tone=intensity > (7.0*threshold/6.0) ? (MagickRealType) QuantumRange :
4186 intensity+(MagickRealType) QuantumRange-7.0*threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004187 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004188 tone=intensity < (threshold/6.0) ? 0 : intensity-threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004189 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004190 tone=threshold/7.0;
cristy4c08aed2011-07-01 19:47:50 +00004191 if ((MagickRealType) GetPixelGreen(image,q) < tone)
4192 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
4193 if ((MagickRealType) GetPixelBlue(image,q) < tone)
4194 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristyed231572011-07-14 02:18:59 +00004195 p+=GetPixelChannels(image);
4196 q+=GetPixelChannels(sepia_image);
cristy3ed852e2009-09-05 21:47:34 +00004197 }
4198 if (SyncCacheViewAuthenticPixels(sepia_view,exception) == MagickFalse)
4199 status=MagickFalse;
4200 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4201 {
4202 MagickBooleanType
4203 proceed;
4204
cristyb5d5f722009-11-04 03:03:49 +00004205#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00004206 #pragma omp critical (MagickCore_SepiaToneImage)
cristy3ed852e2009-09-05 21:47:34 +00004207#endif
4208 proceed=SetImageProgress(image,SepiaToneImageTag,progress++,
4209 image->rows);
4210 if (proceed == MagickFalse)
4211 status=MagickFalse;
4212 }
4213 }
4214 sepia_view=DestroyCacheView(sepia_view);
4215 image_view=DestroyCacheView(image_view);
cristye23ec9d2011-08-16 18:15:40 +00004216 (void) NormalizeImage(sepia_image,exception);
4217 (void) ContrastImage(sepia_image,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004218 if (status == MagickFalse)
4219 sepia_image=DestroyImage(sepia_image);
4220 return(sepia_image);
4221}
4222
4223/*
4224%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4225% %
4226% %
4227% %
4228% S h a d o w I m a g e %
4229% %
4230% %
4231% %
4232%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4233%
4234% ShadowImage() simulates a shadow from the specified image and returns it.
4235%
4236% The format of the ShadowImage method is:
4237%
cristy70cddf72011-12-10 22:42:42 +00004238% Image *ShadowImage(const Image *image,const double alpha,
cristyaa2c16c2012-03-25 22:21:35 +00004239% const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4240% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004241%
4242% A description of each parameter follows:
4243%
4244% o image: the image.
4245%
cristy70cddf72011-12-10 22:42:42 +00004246% o alpha: percentage transparency.
cristy3ed852e2009-09-05 21:47:34 +00004247%
4248% o sigma: the standard deviation of the Gaussian, in pixels.
4249%
4250% o x_offset: the shadow x-offset.
4251%
4252% o y_offset: the shadow y-offset.
4253%
4254% o exception: return any errors or warnings in this structure.
4255%
4256*/
cristy70cddf72011-12-10 22:42:42 +00004257MagickExport Image *ShadowImage(const Image *image,const double alpha,
cristyaa2c16c2012-03-25 22:21:35 +00004258 const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4259 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004260{
4261#define ShadowImageTag "Shadow/Image"
4262
cristy70cddf72011-12-10 22:42:42 +00004263 CacheView
4264 *image_view;
4265
cristybd5a96c2011-08-21 00:04:26 +00004266 ChannelType
4267 channel_mask;
4268
cristy3ed852e2009-09-05 21:47:34 +00004269 Image
4270 *border_image,
4271 *clone_image,
4272 *shadow_image;
4273
cristy70cddf72011-12-10 22:42:42 +00004274 MagickBooleanType
4275 status;
4276
cristy3ed852e2009-09-05 21:47:34 +00004277 RectangleInfo
4278 border_info;
4279
cristy70cddf72011-12-10 22:42:42 +00004280 ssize_t
4281 y;
4282
cristy3ed852e2009-09-05 21:47:34 +00004283 assert(image != (Image *) NULL);
4284 assert(image->signature == MagickSignature);
4285 if (image->debug != MagickFalse)
4286 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4287 assert(exception != (ExceptionInfo *) NULL);
4288 assert(exception->signature == MagickSignature);
4289 clone_image=CloneImage(image,0,0,MagickTrue,exception);
4290 if (clone_image == (Image *) NULL)
4291 return((Image *) NULL);
cristy0898eba2012-04-09 16:38:29 +00004292 if (IsGrayColorspace(image->colorspace) != MagickFalse)
4293 (void) TransformImageColorspace(clone_image,sRGBColorspace,exception);
cristy0ce08762012-06-30 01:33:18 +00004294 (void) SetImageVirtualPixelMethod(clone_image,TransparentVirtualPixelMethod,
cristy387430f2012-02-07 13:09:46 +00004295 exception);
cristybb503372010-05-27 20:51:26 +00004296 border_info.width=(size_t) floor(2.0*sigma+0.5);
4297 border_info.height=(size_t) floor(2.0*sigma+0.5);
cristy3ed852e2009-09-05 21:47:34 +00004298 border_info.x=0;
4299 border_info.y=0;
cristy9950d572011-10-01 18:22:35 +00004300 (void) QueryColorCompliance("none",AllCompliance,&clone_image->border_color,
4301 exception);
cristy70cddf72011-12-10 22:42:42 +00004302 clone_image->matte=MagickTrue;
4303 border_image=BorderImage(clone_image,&border_info,OverCompositeOp,exception);
cristy3ed852e2009-09-05 21:47:34 +00004304 clone_image=DestroyImage(clone_image);
4305 if (border_image == (Image *) NULL)
4306 return((Image *) NULL);
4307 if (border_image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00004308 (void) SetImageAlphaChannel(border_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004309 /*
4310 Shadow image.
4311 */
cristy70cddf72011-12-10 22:42:42 +00004312 status=MagickTrue;
cristydb070952012-04-20 14:33:00 +00004313 image_view=AcquireAuthenticCacheView(border_image,exception);
cristy70cddf72011-12-10 22:42:42 +00004314 for (y=0; y < (ssize_t) border_image->rows; y++)
4315 {
4316 PixelInfo
4317 background_color;
4318
4319 register Quantum
4320 *restrict q;
4321
4322 register ssize_t
4323 x;
4324
4325 if (status == MagickFalse)
4326 continue;
4327 q=QueueCacheViewAuthenticPixels(image_view,0,y,border_image->columns,1,
4328 exception);
4329 if (q == (Quantum *) NULL)
4330 {
4331 status=MagickFalse;
4332 continue;
4333 }
4334 background_color=border_image->background_color;
4335 background_color.matte=MagickTrue;
4336 for (x=0; x < (ssize_t) border_image->columns; x++)
4337 {
4338 if (border_image->matte != MagickFalse)
4339 background_color.alpha=GetPixelAlpha(border_image,q)*alpha/100.0;
4340 SetPixelInfoPixel(border_image,&background_color,q);
4341 q+=GetPixelChannels(border_image);
4342 }
4343 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4344 status=MagickFalse;
4345 }
4346 image_view=DestroyCacheView(image_view);
4347 if (status == MagickFalse)
4348 {
4349 border_image=DestroyImage(border_image);
4350 return((Image *) NULL);
4351 }
cristybd5a96c2011-08-21 00:04:26 +00004352 channel_mask=SetPixelChannelMask(border_image,AlphaChannel);
cristyaa2c16c2012-03-25 22:21:35 +00004353 shadow_image=BlurImage(border_image,0.0,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +00004354 border_image=DestroyImage(border_image);
4355 if (shadow_image == (Image *) NULL)
4356 return((Image *) NULL);
cristyae1969f2011-12-10 03:07:36 +00004357 (void) SetPixelChannelMapMask(shadow_image,channel_mask);
cristy3ed852e2009-09-05 21:47:34 +00004358 if (shadow_image->page.width == 0)
4359 shadow_image->page.width=shadow_image->columns;
4360 if (shadow_image->page.height == 0)
4361 shadow_image->page.height=shadow_image->rows;
cristybb503372010-05-27 20:51:26 +00004362 shadow_image->page.width+=x_offset-(ssize_t) border_info.width;
4363 shadow_image->page.height+=y_offset-(ssize_t) border_info.height;
4364 shadow_image->page.x+=x_offset-(ssize_t) border_info.width;
4365 shadow_image->page.y+=y_offset-(ssize_t) border_info.height;
cristy3ed852e2009-09-05 21:47:34 +00004366 return(shadow_image);
4367}
4368
4369/*
4370%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4371% %
4372% %
4373% %
4374% S k e t c h I m a g e %
4375% %
4376% %
4377% %
4378%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4379%
4380% SketchImage() simulates a pencil sketch. We convolve the image with a
4381% Gaussian operator of the given radius and standard deviation (sigma). For
4382% reasonable results, radius should be larger than sigma. Use a radius of 0
4383% and SketchImage() selects a suitable radius for you. Angle gives the angle
4384% of the sketch.
4385%
4386% The format of the SketchImage method is:
4387%
4388% Image *SketchImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00004389% const double sigma,const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004390%
4391% A description of each parameter follows:
4392%
4393% o image: the image.
4394%
cristy574cc262011-08-05 01:23:58 +00004395% o radius: the radius of the Gaussian, in pixels, not counting the
4396% center pixel.
cristy3ed852e2009-09-05 21:47:34 +00004397%
4398% o sigma: the standard deviation of the Gaussian, in pixels.
4399%
cristyf7ef0252011-09-09 14:50:06 +00004400% o angle: apply the effect along this angle.
4401%
cristy3ed852e2009-09-05 21:47:34 +00004402% o exception: return any errors or warnings in this structure.
4403%
4404*/
4405MagickExport Image *SketchImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00004406 const double sigma,const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004407{
cristyfa112112010-01-04 17:48:07 +00004408 CacheView
4409 *random_view;
4410
cristy3ed852e2009-09-05 21:47:34 +00004411 Image
4412 *blend_image,
4413 *blur_image,
4414 *dodge_image,
4415 *random_image,
4416 *sketch_image;
4417
cristy3ed852e2009-09-05 21:47:34 +00004418 MagickBooleanType
4419 status;
4420
cristy3ed852e2009-09-05 21:47:34 +00004421 RandomInfo
cristyfa112112010-01-04 17:48:07 +00004422 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00004423
cristy9d314ff2011-03-09 01:30:28 +00004424 ssize_t
4425 y;
4426
cristy57340e02012-05-05 00:53:23 +00004427 unsigned long
4428 key;
4429
cristy3ed852e2009-09-05 21:47:34 +00004430 /*
4431 Sketch image.
4432 */
4433 random_image=CloneImage(image,image->columns << 1,image->rows << 1,
4434 MagickTrue,exception);
4435 if (random_image == (Image *) NULL)
4436 return((Image *) NULL);
4437 status=MagickTrue;
cristy1b784432009-12-19 02:20:40 +00004438 random_info=AcquireRandomInfoThreadSet();
cristy57340e02012-05-05 00:53:23 +00004439 key=GetRandomSecretKey(random_info[0]);
cristydb070952012-04-20 14:33:00 +00004440 random_view=AcquireAuthenticCacheView(random_image,exception);
cristy1b784432009-12-19 02:20:40 +00004441#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy57340e02012-05-05 00:53:23 +00004442 #pragma omp parallel for schedule(static,4) shared(status) \
cristy4ee2b0c2012-05-15 00:30:35 +00004443 dynamic_number_threads(image,image->columns,image->rows,key == ~0UL)
cristy1b784432009-12-19 02:20:40 +00004444#endif
cristybb503372010-05-27 20:51:26 +00004445 for (y=0; y < (ssize_t) random_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004446 {
cristy5c9e6f22010-09-17 17:31:01 +00004447 const int
4448 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00004449
cristybb503372010-05-27 20:51:26 +00004450 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004451 x;
4452
cristy4c08aed2011-07-01 19:47:50 +00004453 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004454 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004455
cristy1b784432009-12-19 02:20:40 +00004456 if (status == MagickFalse)
4457 continue;
cristy3ed852e2009-09-05 21:47:34 +00004458 q=QueueCacheViewAuthenticPixels(random_view,0,y,random_image->columns,1,
4459 exception);
cristyacd2ed22011-08-30 01:44:23 +00004460 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004461 {
4462 status=MagickFalse;
4463 continue;
4464 }
cristybb503372010-05-27 20:51:26 +00004465 for (x=0; x < (ssize_t) random_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004466 {
cristy76f512e2011-09-12 01:26:56 +00004467 MagickRealType
4468 value;
4469
4470 register ssize_t
4471 i;
4472
cristy10a6c612012-01-29 21:41:05 +00004473 if (GetPixelMask(random_image,q) != 0)
4474 {
4475 q+=GetPixelChannels(random_image);
4476 continue;
4477 }
cristy76f512e2011-09-12 01:26:56 +00004478 value=GetPseudoRandomValue(random_info[id]);
4479 for (i=0; i < (ssize_t) GetPixelChannels(random_image); i++)
4480 {
cristyabace412011-12-11 15:56:53 +00004481 PixelChannel
4482 channel;
4483
cristy76f512e2011-09-12 01:26:56 +00004484 PixelTrait
4485 traits;
4486
cristyabace412011-12-11 15:56:53 +00004487 channel=GetPixelChannelMapChannel(image,i);
4488 traits=GetPixelChannelMapTraits(image,channel);
cristy76f512e2011-09-12 01:26:56 +00004489 if (traits == UndefinedPixelTrait)
4490 continue;
4491 q[i]=ClampToQuantum(QuantumRange*value);
4492 }
cristyed231572011-07-14 02:18:59 +00004493 q+=GetPixelChannels(random_image);
cristy3ed852e2009-09-05 21:47:34 +00004494 }
4495 if (SyncCacheViewAuthenticPixels(random_view,exception) == MagickFalse)
4496 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004497 }
4498 random_view=DestroyCacheView(random_view);
cristy1b784432009-12-19 02:20:40 +00004499 random_info=DestroyRandomInfoThreadSet(random_info);
cristy3ed852e2009-09-05 21:47:34 +00004500 if (status == MagickFalse)
4501 {
4502 random_image=DestroyImage(random_image);
4503 return(random_image);
4504 }
cristyaa2c16c2012-03-25 22:21:35 +00004505 blur_image=MotionBlurImage(random_image,radius,sigma,angle,exception);
cristy3ed852e2009-09-05 21:47:34 +00004506 random_image=DestroyImage(random_image);
4507 if (blur_image == (Image *) NULL)
4508 return((Image *) NULL);
cristy6bfd6902011-12-09 01:33:45 +00004509 dodge_image=EdgeImage(blur_image,radius,1.0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004510 blur_image=DestroyImage(blur_image);
4511 if (dodge_image == (Image *) NULL)
4512 return((Image *) NULL);
cristye23ec9d2011-08-16 18:15:40 +00004513 (void) NormalizeImage(dodge_image,exception);
cristyb3e7c6c2011-07-24 01:43:55 +00004514 (void) NegateImage(dodge_image,MagickFalse,exception);
cristye941a752011-10-15 01:52:48 +00004515 (void) TransformImage(&dodge_image,(char *) NULL,"50%",exception);
cristy3ed852e2009-09-05 21:47:34 +00004516 sketch_image=CloneImage(image,0,0,MagickTrue,exception);
4517 if (sketch_image == (Image *) NULL)
4518 {
4519 dodge_image=DestroyImage(dodge_image);
4520 return((Image *) NULL);
4521 }
cristyfeb3e962012-03-29 17:25:55 +00004522 (void) CompositeImage(sketch_image,dodge_image,ColorDodgeCompositeOp,
cristy39172402012-03-30 13:04:39 +00004523 MagickTrue,0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004524 dodge_image=DestroyImage(dodge_image);
4525 blend_image=CloneImage(image,0,0,MagickTrue,exception);
4526 if (blend_image == (Image *) NULL)
4527 {
4528 sketch_image=DestroyImage(sketch_image);
4529 return((Image *) NULL);
4530 }
4531 (void) SetImageArtifact(blend_image,"compose:args","20x80");
cristy39172402012-03-30 13:04:39 +00004532 (void) CompositeImage(sketch_image,blend_image,BlendCompositeOp,MagickTrue,
cristyfeb3e962012-03-29 17:25:55 +00004533 0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004534 blend_image=DestroyImage(blend_image);
4535 return(sketch_image);
4536}
4537
4538/*
4539%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4540% %
4541% %
4542% %
4543% S o l a r i z e I m a g e %
4544% %
4545% %
4546% %
4547%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4548%
4549% SolarizeImage() applies a special effect to the image, similar to the effect
4550% achieved in a photo darkroom by selectively exposing areas of photo
4551% sensitive paper to light. Threshold ranges from 0 to QuantumRange and is a
4552% measure of the extent of the solarization.
4553%
4554% The format of the SolarizeImage method is:
4555%
cristy5cbc0162011-08-29 00:36:28 +00004556% MagickBooleanType SolarizeImage(Image *image,const double threshold,
4557% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004558%
4559% A description of each parameter follows:
4560%
4561% o image: the image.
4562%
4563% o threshold: Define the extent of the solarization.
4564%
cristy5cbc0162011-08-29 00:36:28 +00004565% o exception: return any errors or warnings in this structure.
4566%
cristy3ed852e2009-09-05 21:47:34 +00004567*/
4568MagickExport MagickBooleanType SolarizeImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00004569 const double threshold,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004570{
4571#define SolarizeImageTag "Solarize/Image"
4572
cristyc4c8d132010-01-07 01:58:38 +00004573 CacheView
4574 *image_view;
4575
cristy3ed852e2009-09-05 21:47:34 +00004576 MagickBooleanType
4577 status;
4578
cristybb503372010-05-27 20:51:26 +00004579 MagickOffsetType
4580 progress;
4581
4582 ssize_t
4583 y;
4584
cristy3ed852e2009-09-05 21:47:34 +00004585 assert(image != (Image *) NULL);
4586 assert(image->signature == MagickSignature);
4587 if (image->debug != MagickFalse)
4588 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4589 if (image->storage_class == PseudoClass)
4590 {
cristybb503372010-05-27 20:51:26 +00004591 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004592 i;
4593
4594 /*
4595 Solarize colormap.
4596 */
cristybb503372010-05-27 20:51:26 +00004597 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00004598 {
4599 if ((MagickRealType) image->colormap[i].red > threshold)
cristy6e963d82012-06-19 15:23:24 +00004600 image->colormap[i].red=QuantumRange-image->colormap[i].red;
cristy3ed852e2009-09-05 21:47:34 +00004601 if ((MagickRealType) image->colormap[i].green > threshold)
cristy6e963d82012-06-19 15:23:24 +00004602 image->colormap[i].green=QuantumRange-
cristy3ed852e2009-09-05 21:47:34 +00004603 image->colormap[i].green;
4604 if ((MagickRealType) image->colormap[i].blue > threshold)
cristy6e963d82012-06-19 15:23:24 +00004605 image->colormap[i].blue=QuantumRange-
cristy3ed852e2009-09-05 21:47:34 +00004606 image->colormap[i].blue;
4607 }
4608 }
4609 /*
4610 Solarize image.
4611 */
4612 status=MagickTrue;
4613 progress=0;
cristydb070952012-04-20 14:33:00 +00004614 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00004615#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00004616 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00004617 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00004618#endif
cristybb503372010-05-27 20:51:26 +00004619 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004620 {
cristybb503372010-05-27 20:51:26 +00004621 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004622 x;
4623
cristy4c08aed2011-07-01 19:47:50 +00004624 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004625 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004626
4627 if (status == MagickFalse)
4628 continue;
cristy5cbc0162011-08-29 00:36:28 +00004629 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00004630 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004631 {
4632 status=MagickFalse;
4633 continue;
4634 }
cristybb503372010-05-27 20:51:26 +00004635 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004636 {
cristy76f512e2011-09-12 01:26:56 +00004637 register ssize_t
4638 i;
4639
cristy10a6c612012-01-29 21:41:05 +00004640 if (GetPixelMask(image,q) != 0)
4641 {
4642 q+=GetPixelChannels(image);
4643 continue;
4644 }
cristy76f512e2011-09-12 01:26:56 +00004645 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4646 {
cristyabace412011-12-11 15:56:53 +00004647 PixelChannel
4648 channel;
4649
cristy76f512e2011-09-12 01:26:56 +00004650 PixelTrait
4651 traits;
4652
cristyabace412011-12-11 15:56:53 +00004653 channel=GetPixelChannelMapChannel(image,i);
4654 traits=GetPixelChannelMapTraits(image,channel);
cristy76f512e2011-09-12 01:26:56 +00004655 if ((traits == UndefinedPixelTrait) ||
4656 ((traits & CopyPixelTrait) != 0))
4657 continue;
4658 if ((MagickRealType) q[i] > threshold)
4659 q[i]=QuantumRange-q[i];
4660 }
cristyed231572011-07-14 02:18:59 +00004661 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00004662 }
4663 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4664 status=MagickFalse;
4665 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4666 {
4667 MagickBooleanType
4668 proceed;
4669
cristyb5d5f722009-11-04 03:03:49 +00004670#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00004671 #pragma omp critical (MagickCore_SolarizeImage)
cristy3ed852e2009-09-05 21:47:34 +00004672#endif
4673 proceed=SetImageProgress(image,SolarizeImageTag,progress++,image->rows);
4674 if (proceed == MagickFalse)
4675 status=MagickFalse;
4676 }
4677 }
4678 image_view=DestroyCacheView(image_view);
4679 return(status);
4680}
4681
4682/*
4683%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4684% %
4685% %
4686% %
4687% S t e g a n o I m a g e %
4688% %
4689% %
4690% %
4691%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4692%
4693% SteganoImage() hides a digital watermark within the image. Recover
4694% the hidden watermark later to prove that the authenticity of an image.
4695% Offset defines the start position within the image to hide the watermark.
4696%
4697% The format of the SteganoImage method is:
4698%
4699% Image *SteganoImage(const Image *image,Image *watermark,
4700% ExceptionInfo *exception)
4701%
4702% A description of each parameter follows:
4703%
4704% o image: the image.
4705%
4706% o watermark: the watermark image.
4707%
4708% o exception: return any errors or warnings in this structure.
4709%
4710*/
4711MagickExport Image *SteganoImage(const Image *image,const Image *watermark,
4712 ExceptionInfo *exception)
4713{
cristye1bf8ad2010-09-19 17:07:03 +00004714#define GetBit(alpha,i) ((((size_t) (alpha) >> (size_t) (i)) & 0x01) != 0)
cristy4c08aed2011-07-01 19:47:50 +00004715#define SetBit(alpha,i,set) (Quantum) ((set) != 0 ? (size_t) (alpha) \
cristyeaedf062010-05-29 22:36:02 +00004716 | (one << (size_t) (i)) : (size_t) (alpha) & ~(one << (size_t) (i)))
cristy3ed852e2009-09-05 21:47:34 +00004717#define SteganoImageTag "Stegano/Image"
4718
cristyb0d3bb92010-09-22 14:37:58 +00004719 CacheView
4720 *stegano_view,
4721 *watermark_view;
4722
cristy3ed852e2009-09-05 21:47:34 +00004723 Image
4724 *stegano_image;
4725
4726 int
4727 c;
4728
cristy3ed852e2009-09-05 21:47:34 +00004729 MagickBooleanType
4730 status;
4731
cristy101ab702011-10-13 13:06:32 +00004732 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00004733 pixel;
4734
cristy4c08aed2011-07-01 19:47:50 +00004735 register Quantum
cristy3ed852e2009-09-05 21:47:34 +00004736 *q;
4737
cristye1bf8ad2010-09-19 17:07:03 +00004738 register ssize_t
4739 x;
4740
cristybb503372010-05-27 20:51:26 +00004741 size_t
cristyeaedf062010-05-29 22:36:02 +00004742 depth,
4743 one;
cristy3ed852e2009-09-05 21:47:34 +00004744
cristye1bf8ad2010-09-19 17:07:03 +00004745 ssize_t
4746 i,
4747 j,
4748 k,
4749 y;
4750
cristy3ed852e2009-09-05 21:47:34 +00004751 /*
4752 Initialize steganographic image attributes.
4753 */
4754 assert(image != (const Image *) NULL);
4755 assert(image->signature == MagickSignature);
4756 if (image->debug != MagickFalse)
4757 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4758 assert(watermark != (const Image *) NULL);
4759 assert(watermark->signature == MagickSignature);
4760 assert(exception != (ExceptionInfo *) NULL);
4761 assert(exception->signature == MagickSignature);
cristyeaedf062010-05-29 22:36:02 +00004762 one=1UL;
cristy3ed852e2009-09-05 21:47:34 +00004763 stegano_image=CloneImage(image,0,0,MagickTrue,exception);
4764 if (stegano_image == (Image *) NULL)
4765 return((Image *) NULL);
cristyf61b1832012-04-01 01:38:19 +00004766 stegano_image->depth=MAGICKCORE_QUANTUM_DEPTH;
cristy574cc262011-08-05 01:23:58 +00004767 if (SetImageStorageClass(stegano_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004768 {
cristy3ed852e2009-09-05 21:47:34 +00004769 stegano_image=DestroyImage(stegano_image);
4770 return((Image *) NULL);
4771 }
cristy3ed852e2009-09-05 21:47:34 +00004772 /*
4773 Hide watermark in low-order bits of image.
4774 */
4775 c=0;
4776 i=0;
4777 j=0;
4778 depth=stegano_image->depth;
cristyf61b1832012-04-01 01:38:19 +00004779 k=stegano_image->offset;
cristyda16f162011-02-19 23:52:17 +00004780 status=MagickTrue;
cristydb070952012-04-20 14:33:00 +00004781 watermark_view=AcquireVirtualCacheView(watermark,exception);
4782 stegano_view=AcquireAuthenticCacheView(stegano_image,exception);
cristybb503372010-05-27 20:51:26 +00004783 for (i=(ssize_t) depth-1; (i >= 0) && (j < (ssize_t) depth); i--)
cristy3ed852e2009-09-05 21:47:34 +00004784 {
cristybb503372010-05-27 20:51:26 +00004785 for (y=0; (y < (ssize_t) watermark->rows) && (j < (ssize_t) depth); y++)
cristy3ed852e2009-09-05 21:47:34 +00004786 {
cristybb503372010-05-27 20:51:26 +00004787 for (x=0; (x < (ssize_t) watermark->columns) && (j < (ssize_t) depth); x++)
cristy3ed852e2009-09-05 21:47:34 +00004788 {
cristy1707c6c2012-01-18 23:30:54 +00004789 ssize_t
4790 offset;
4791
cristyf05d4942012-03-17 16:26:09 +00004792 (void) GetOneCacheViewVirtualPixelInfo(watermark_view,x,y,&pixel,
cristyda1f9c12011-10-02 21:39:49 +00004793 exception);
cristy1707c6c2012-01-18 23:30:54 +00004794 offset=k/(ssize_t) stegano_image->columns;
4795 if (offset >= (ssize_t) stegano_image->rows)
cristy3ed852e2009-09-05 21:47:34 +00004796 break;
cristyb0d3bb92010-09-22 14:37:58 +00004797 q=GetCacheViewAuthenticPixels(stegano_view,k % (ssize_t)
4798 stegano_image->columns,k/(ssize_t) stegano_image->columns,1,1,
4799 exception);
cristyacd2ed22011-08-30 01:44:23 +00004800 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004801 break;
4802 switch (c)
4803 {
4804 case 0:
4805 {
cristyf61b1832012-04-01 01:38:19 +00004806 SetPixelRed(stegano_image,SetBit(GetPixelRed(stegano_image,q),j,
4807 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004808 break;
4809 }
4810 case 1:
4811 {
cristyf61b1832012-04-01 01:38:19 +00004812 SetPixelGreen(stegano_image,SetBit(GetPixelGreen(stegano_image,q),j,
4813 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004814 break;
4815 }
4816 case 2:
4817 {
cristyf61b1832012-04-01 01:38:19 +00004818 SetPixelBlue(stegano_image,SetBit(GetPixelBlue(stegano_image,q),j,
4819 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004820 break;
4821 }
4822 }
cristyb0d3bb92010-09-22 14:37:58 +00004823 if (SyncCacheViewAuthenticPixels(stegano_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004824 break;
4825 c++;
4826 if (c == 3)
4827 c=0;
4828 k++;
cristybb503372010-05-27 20:51:26 +00004829 if (k == (ssize_t) (stegano_image->columns*stegano_image->columns))
cristy3ed852e2009-09-05 21:47:34 +00004830 k=0;
cristyf61b1832012-04-01 01:38:19 +00004831 if (k == stegano_image->offset)
cristy3ed852e2009-09-05 21:47:34 +00004832 j++;
4833 }
4834 }
cristy8b27a6d2010-02-14 03:31:15 +00004835 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004836 {
cristy8b27a6d2010-02-14 03:31:15 +00004837 MagickBooleanType
4838 proceed;
4839
4840 proceed=SetImageProgress(image,SteganoImageTag,(MagickOffsetType)
4841 (depth-i),depth);
4842 if (proceed == MagickFalse)
4843 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004844 }
4845 }
cristyb0d3bb92010-09-22 14:37:58 +00004846 stegano_view=DestroyCacheView(stegano_view);
4847 watermark_view=DestroyCacheView(watermark_view);
cristyda16f162011-02-19 23:52:17 +00004848 if (status == MagickFalse)
4849 {
4850 stegano_image=DestroyImage(stegano_image);
4851 return((Image *) NULL);
4852 }
cristy3ed852e2009-09-05 21:47:34 +00004853 return(stegano_image);
4854}
4855
4856/*
4857%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4858% %
4859% %
4860% %
4861% S t e r e o A n a g l y p h I m a g e %
4862% %
4863% %
4864% %
4865%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4866%
4867% StereoAnaglyphImage() combines two images and produces a single image that
4868% is the composite of a left and right image of a stereo pair. Special
4869% red-green stereo glasses are required to view this effect.
4870%
4871% The format of the StereoAnaglyphImage method is:
4872%
4873% Image *StereoImage(const Image *left_image,const Image *right_image,
4874% ExceptionInfo *exception)
4875% Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004876% const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004877% ExceptionInfo *exception)
4878%
4879% A description of each parameter follows:
4880%
4881% o left_image: the left image.
4882%
4883% o right_image: the right image.
4884%
4885% o exception: return any errors or warnings in this structure.
4886%
4887% o x_offset: amount, in pixels, by which the left image is offset to the
4888% right of the right image.
4889%
4890% o y_offset: amount, in pixels, by which the left image is offset to the
4891% bottom of the right image.
4892%
4893%
4894*/
4895MagickExport Image *StereoImage(const Image *left_image,
4896 const Image *right_image,ExceptionInfo *exception)
4897{
4898 return(StereoAnaglyphImage(left_image,right_image,0,0,exception));
4899}
4900
4901MagickExport Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004902 const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004903 ExceptionInfo *exception)
4904{
4905#define StereoImageTag "Stereo/Image"
4906
4907 const Image
4908 *image;
4909
4910 Image
4911 *stereo_image;
4912
cristy3ed852e2009-09-05 21:47:34 +00004913 MagickBooleanType
4914 status;
4915
cristy9d314ff2011-03-09 01:30:28 +00004916 ssize_t
4917 y;
4918
cristy3ed852e2009-09-05 21:47:34 +00004919 assert(left_image != (const Image *) NULL);
4920 assert(left_image->signature == MagickSignature);
4921 if (left_image->debug != MagickFalse)
4922 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4923 left_image->filename);
4924 assert(right_image != (const Image *) NULL);
4925 assert(right_image->signature == MagickSignature);
4926 assert(exception != (ExceptionInfo *) NULL);
4927 assert(exception->signature == MagickSignature);
4928 assert(right_image != (const Image *) NULL);
4929 image=left_image;
4930 if ((left_image->columns != right_image->columns) ||
4931 (left_image->rows != right_image->rows))
4932 ThrowImageException(ImageError,"LeftAndRightImageSizesDiffer");
4933 /*
4934 Initialize stereo image attributes.
4935 */
4936 stereo_image=CloneImage(left_image,left_image->columns,left_image->rows,
4937 MagickTrue,exception);
4938 if (stereo_image == (Image *) NULL)
4939 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004940 if (SetImageStorageClass(stereo_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004941 {
cristy3ed852e2009-09-05 21:47:34 +00004942 stereo_image=DestroyImage(stereo_image);
4943 return((Image *) NULL);
4944 }
cristy079c78d2012-07-03 11:53:48 +00004945 (void) SetImageColorspace(stereo_image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00004946 /*
4947 Copy left image to red channel and right image to blue channel.
4948 */
cristyda16f162011-02-19 23:52:17 +00004949 status=MagickTrue;
cristybb503372010-05-27 20:51:26 +00004950 for (y=0; y < (ssize_t) stereo_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004951 {
cristy4c08aed2011-07-01 19:47:50 +00004952 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004953 *restrict p,
4954 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004955
cristybb503372010-05-27 20:51:26 +00004956 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004957 x;
4958
cristy4c08aed2011-07-01 19:47:50 +00004959 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004960 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +00004961
4962 p=GetVirtualPixels(left_image,-x_offset,y-y_offset,image->columns,1,
4963 exception);
4964 q=GetVirtualPixels(right_image,0,y,right_image->columns,1,exception);
4965 r=QueueAuthenticPixels(stereo_image,0,y,stereo_image->columns,1,exception);
cristy76f512e2011-09-12 01:26:56 +00004966 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL) ||
4967 (r == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004968 break;
cristybb503372010-05-27 20:51:26 +00004969 for (x=0; x < (ssize_t) stereo_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004970 {
cristy4c08aed2011-07-01 19:47:50 +00004971 SetPixelRed(image,GetPixelRed(left_image,p),r);
cristy76f512e2011-09-12 01:26:56 +00004972 SetPixelGreen(image,GetPixelGreen(right_image,q),r);
4973 SetPixelBlue(image,GetPixelBlue(right_image,q),r);
4974 if ((GetPixelAlphaTraits(stereo_image) & CopyPixelTrait) != 0)
4975 SetPixelAlpha(image,(GetPixelAlpha(left_image,p)+
4976 GetPixelAlpha(right_image,q))/2,r);
cristyed231572011-07-14 02:18:59 +00004977 p+=GetPixelChannels(left_image);
cristy76f512e2011-09-12 01:26:56 +00004978 q+=GetPixelChannels(right_image);
4979 r+=GetPixelChannels(stereo_image);
cristy3ed852e2009-09-05 21:47:34 +00004980 }
4981 if (SyncAuthenticPixels(stereo_image,exception) == MagickFalse)
4982 break;
cristy8b27a6d2010-02-14 03:31:15 +00004983 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004984 {
cristy8b27a6d2010-02-14 03:31:15 +00004985 MagickBooleanType
4986 proceed;
4987
cristybb503372010-05-27 20:51:26 +00004988 proceed=SetImageProgress(image,StereoImageTag,(MagickOffsetType) y,
4989 stereo_image->rows);
cristy8b27a6d2010-02-14 03:31:15 +00004990 if (proceed == MagickFalse)
4991 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004992 }
4993 }
cristyda16f162011-02-19 23:52:17 +00004994 if (status == MagickFalse)
4995 {
4996 stereo_image=DestroyImage(stereo_image);
4997 return((Image *) NULL);
4998 }
cristy3ed852e2009-09-05 21:47:34 +00004999 return(stereo_image);
5000}
5001
5002/*
5003%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5004% %
5005% %
5006% %
5007% S w i r l I m a g e %
5008% %
5009% %
5010% %
5011%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5012%
5013% SwirlImage() swirls the pixels about the center of the image, where
5014% degrees indicates the sweep of the arc through which each pixel is moved.
5015% You get a more dramatic effect as the degrees move from 1 to 360.
5016%
5017% The format of the SwirlImage method is:
5018%
5019% 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% A description of each parameter follows:
5023%
5024% o image: the image.
5025%
5026% o degrees: Define the tightness of the swirling effect.
5027%
cristy76f512e2011-09-12 01:26:56 +00005028% o method: the pixel interpolation method.
5029%
cristy3ed852e2009-09-05 21:47:34 +00005030% o exception: return any errors or warnings in this structure.
5031%
5032*/
5033MagickExport Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00005034 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005035{
5036#define SwirlImageTag "Swirl/Image"
5037
cristyfa112112010-01-04 17:48:07 +00005038 CacheView
5039 *image_view,
5040 *swirl_view;
5041
cristy3ed852e2009-09-05 21:47:34 +00005042 Image
5043 *swirl_image;
5044
cristy3ed852e2009-09-05 21:47:34 +00005045 MagickBooleanType
5046 status;
5047
cristybb503372010-05-27 20:51:26 +00005048 MagickOffsetType
5049 progress;
5050
cristy3ed852e2009-09-05 21:47:34 +00005051 MagickRealType
5052 radius;
5053
5054 PointInfo
5055 center,
5056 scale;
5057
cristybb503372010-05-27 20:51:26 +00005058 ssize_t
5059 y;
5060
cristy3ed852e2009-09-05 21:47:34 +00005061 /*
5062 Initialize swirl image attributes.
5063 */
5064 assert(image != (const Image *) NULL);
5065 assert(image->signature == MagickSignature);
5066 if (image->debug != MagickFalse)
5067 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5068 assert(exception != (ExceptionInfo *) NULL);
5069 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00005070 swirl_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005071 if (swirl_image == (Image *) NULL)
5072 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005073 if (SetImageStorageClass(swirl_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005074 {
cristy3ed852e2009-09-05 21:47:34 +00005075 swirl_image=DestroyImage(swirl_image);
5076 return((Image *) NULL);
5077 }
cristy4c08aed2011-07-01 19:47:50 +00005078 if (swirl_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00005079 swirl_image->matte=MagickTrue;
5080 /*
5081 Compute scaling factor.
5082 */
5083 center.x=(double) image->columns/2.0;
5084 center.y=(double) image->rows/2.0;
5085 radius=MagickMax(center.x,center.y);
5086 scale.x=1.0;
5087 scale.y=1.0;
5088 if (image->columns > image->rows)
5089 scale.y=(double) image->columns/(double) image->rows;
5090 else
5091 if (image->columns < image->rows)
5092 scale.x=(double) image->rows/(double) image->columns;
5093 degrees=(double) DegreesToRadians(degrees);
5094 /*
5095 Swirl image.
5096 */
5097 status=MagickTrue;
5098 progress=0;
cristydb070952012-04-20 14:33:00 +00005099 image_view=AcquireVirtualCacheView(image,exception);
5100 swirl_view=AcquireAuthenticCacheView(swirl_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00005101#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00005102 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00005103 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00005104#endif
cristybb503372010-05-27 20:51:26 +00005105 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005106 {
cristy3ed852e2009-09-05 21:47:34 +00005107 MagickRealType
5108 distance;
5109
5110 PointInfo
5111 delta;
5112
cristy6d188022011-09-12 13:23:33 +00005113 register const Quantum
5114 *restrict p;
5115
cristybb503372010-05-27 20:51:26 +00005116 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005117 x;
5118
cristy4c08aed2011-07-01 19:47:50 +00005119 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005120 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005121
5122 if (status == MagickFalse)
5123 continue;
cristy6d188022011-09-12 13:23:33 +00005124 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy1707c6c2012-01-18 23:30:54 +00005125 q=QueueCacheViewAuthenticPixels(swirl_view,0,y,swirl_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00005126 exception);
cristy6d188022011-09-12 13:23:33 +00005127 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005128 {
5129 status=MagickFalse;
5130 continue;
5131 }
cristy3ed852e2009-09-05 21:47:34 +00005132 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00005133 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005134 {
5135 /*
5136 Determine if the pixel is within an ellipse.
5137 */
cristy10a6c612012-01-29 21:41:05 +00005138 if (GetPixelMask(image,p) != 0)
5139 {
5140 p+=GetPixelChannels(image);
5141 q+=GetPixelChannels(swirl_image);
5142 continue;
5143 }
cristy3ed852e2009-09-05 21:47:34 +00005144 delta.x=scale.x*(double) (x-center.x);
5145 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00005146 if (distance >= (radius*radius))
5147 {
cristy1707c6c2012-01-18 23:30:54 +00005148 register ssize_t
5149 i;
5150
cristy6d188022011-09-12 13:23:33 +00005151 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy1707c6c2012-01-18 23:30:54 +00005152 {
5153 PixelChannel
5154 channel;
5155
5156 PixelTrait
5157 swirl_traits,
5158 traits;
5159
5160 channel=GetPixelChannelMapChannel(image,i);
5161 traits=GetPixelChannelMapTraits(image,channel);
5162 swirl_traits=GetPixelChannelMapTraits(swirl_image,channel);
5163 if ((traits == UndefinedPixelTrait) ||
5164 (swirl_traits == UndefinedPixelTrait))
5165 continue;
5166 SetPixelChannel(swirl_image,channel,p[i],q);
5167 }
cristy6d188022011-09-12 13:23:33 +00005168 }
5169 else
cristy3ed852e2009-09-05 21:47:34 +00005170 {
5171 MagickRealType
5172 cosine,
5173 factor,
5174 sine;
5175
5176 /*
5177 Swirl the pixel.
5178 */
5179 factor=1.0-sqrt((double) distance)/radius;
5180 sine=sin((double) (degrees*factor*factor));
5181 cosine=cos((double) (degrees*factor*factor));
cristy76f512e2011-09-12 01:26:56 +00005182 status=InterpolatePixelChannels(image,image_view,swirl_image,method,
5183 ((cosine*delta.x-sine*delta.y)/scale.x+center.x),(double)
5184 ((sine*delta.x+cosine*delta.y)/scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00005185 }
cristy6d188022011-09-12 13:23:33 +00005186 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00005187 q+=GetPixelChannels(swirl_image);
cristy3ed852e2009-09-05 21:47:34 +00005188 }
5189 if (SyncCacheViewAuthenticPixels(swirl_view,exception) == MagickFalse)
5190 status=MagickFalse;
5191 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5192 {
5193 MagickBooleanType
5194 proceed;
5195
cristyb5d5f722009-11-04 03:03:49 +00005196#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005197 #pragma omp critical (MagickCore_SwirlImage)
cristy3ed852e2009-09-05 21:47:34 +00005198#endif
5199 proceed=SetImageProgress(image,SwirlImageTag,progress++,image->rows);
5200 if (proceed == MagickFalse)
5201 status=MagickFalse;
5202 }
5203 }
5204 swirl_view=DestroyCacheView(swirl_view);
5205 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00005206 if (status == MagickFalse)
5207 swirl_image=DestroyImage(swirl_image);
5208 return(swirl_image);
5209}
5210
5211/*
5212%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5213% %
5214% %
5215% %
5216% T i n t I m a g e %
5217% %
5218% %
5219% %
5220%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5221%
5222% TintImage() applies a color vector to each pixel in the image. The length
5223% of the vector is 0 for black and white and at its maximum for the midtones.
5224% The vector weighting function is f(x)=(1-(4.0*((x-0.5)*(x-0.5))))
5225%
5226% The format of the TintImage method is:
5227%
cristyb817c3f2011-10-03 14:00:35 +00005228% Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005229% const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005230%
5231% A description of each parameter follows:
5232%
5233% o image: the image.
5234%
cristyb817c3f2011-10-03 14:00:35 +00005235% o blend: A color value used for tinting.
cristy3ed852e2009-09-05 21:47:34 +00005236%
5237% o tint: A color value used for tinting.
5238%
5239% o exception: return any errors or warnings in this structure.
5240%
5241*/
cristyb817c3f2011-10-03 14:00:35 +00005242MagickExport Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005243 const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005244{
5245#define TintImageTag "Tint/Image"
5246
cristyc4c8d132010-01-07 01:58:38 +00005247 CacheView
5248 *image_view,
5249 *tint_view;
5250
cristy3ed852e2009-09-05 21:47:34 +00005251 GeometryInfo
5252 geometry_info;
5253
5254 Image
5255 *tint_image;
5256
cristy3ed852e2009-09-05 21:47:34 +00005257 MagickBooleanType
5258 status;
5259
cristybb503372010-05-27 20:51:26 +00005260 MagickOffsetType
5261 progress;
cristy3ed852e2009-09-05 21:47:34 +00005262
cristy28474bf2011-09-11 23:32:52 +00005263 MagickRealType
5264 intensity;
5265
cristy4c08aed2011-07-01 19:47:50 +00005266 PixelInfo
cristy1707c6c2012-01-18 23:30:54 +00005267 color_vector;
cristy3ed852e2009-09-05 21:47:34 +00005268
cristybb503372010-05-27 20:51:26 +00005269 MagickStatusType
5270 flags;
5271
5272 ssize_t
5273 y;
5274
cristy3ed852e2009-09-05 21:47:34 +00005275 /*
5276 Allocate tint image.
5277 */
5278 assert(image != (const Image *) NULL);
5279 assert(image->signature == MagickSignature);
5280 if (image->debug != MagickFalse)
5281 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5282 assert(exception != (ExceptionInfo *) NULL);
5283 assert(exception->signature == MagickSignature);
5284 tint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5285 if (tint_image == (Image *) NULL)
5286 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005287 if (SetImageStorageClass(tint_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005288 {
cristy3ed852e2009-09-05 21:47:34 +00005289 tint_image=DestroyImage(tint_image);
5290 return((Image *) NULL);
5291 }
cristy71aac542012-05-18 12:06:35 +00005292 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
5293 (IsPixelInfoGray(tint) == MagickFalse))
5294 (void) SetImageColorspace(tint_image,sRGBColorspace,exception);
cristyaed9c382011-10-03 17:54:21 +00005295 if (blend == (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005296 return(tint_image);
5297 /*
5298 Determine RGB values of the color.
5299 */
cristy1707c6c2012-01-18 23:30:54 +00005300 GetPixelInfo(image,&color_vector);
cristyb817c3f2011-10-03 14:00:35 +00005301 flags=ParseGeometry(blend,&geometry_info);
cristy1707c6c2012-01-18 23:30:54 +00005302 color_vector.red=geometry_info.rho;
5303 color_vector.green=geometry_info.rho;
5304 color_vector.blue=geometry_info.rho;
5305 color_vector.alpha=OpaqueAlpha;
cristy3ed852e2009-09-05 21:47:34 +00005306 if ((flags & SigmaValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005307 color_vector.green=geometry_info.sigma;
cristy3ed852e2009-09-05 21:47:34 +00005308 if ((flags & XiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005309 color_vector.blue=geometry_info.xi;
cristyb817c3f2011-10-03 14:00:35 +00005310 if ((flags & PsiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005311 color_vector.alpha=geometry_info.psi;
cristy76f512e2011-09-12 01:26:56 +00005312 if (image->colorspace == CMYKColorspace)
5313 {
cristy1707c6c2012-01-18 23:30:54 +00005314 color_vector.black=geometry_info.rho;
cristy76f512e2011-09-12 01:26:56 +00005315 if ((flags & PsiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005316 color_vector.black=geometry_info.psi;
cristy76f512e2011-09-12 01:26:56 +00005317 if ((flags & ChiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005318 color_vector.alpha=geometry_info.chi;
cristy76f512e2011-09-12 01:26:56 +00005319 }
cristy28474bf2011-09-11 23:32:52 +00005320 intensity=(MagickRealType) GetPixelInfoIntensity(tint);
cristy1707c6c2012-01-18 23:30:54 +00005321 color_vector.red=(MagickRealType) (color_vector.red*tint->red/100.0-
5322 intensity);
5323 color_vector.green=(MagickRealType) (color_vector.green*tint->green/100.0-
5324 intensity);
5325 color_vector.blue=(MagickRealType) (color_vector.blue*tint->blue/100.0-
5326 intensity);
5327 color_vector.black=(MagickRealType) (color_vector.black*tint->black/100.0-
5328 intensity);
5329 color_vector.alpha=(MagickRealType) (color_vector.alpha*tint->alpha/100.0-
5330 intensity);
cristy3ed852e2009-09-05 21:47:34 +00005331 /*
5332 Tint image.
5333 */
5334 status=MagickTrue;
5335 progress=0;
cristydb070952012-04-20 14:33:00 +00005336 image_view=AcquireVirtualCacheView(image,exception);
5337 tint_view=AcquireAuthenticCacheView(tint_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00005338#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00005339 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00005340 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00005341#endif
cristybb503372010-05-27 20:51:26 +00005342 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005343 {
cristy4c08aed2011-07-01 19:47:50 +00005344 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00005345 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00005346
cristy4c08aed2011-07-01 19:47:50 +00005347 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005348 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005349
cristy6b91acb2011-04-19 12:23:54 +00005350 register ssize_t
5351 x;
5352
cristy3ed852e2009-09-05 21:47:34 +00005353 if (status == MagickFalse)
5354 continue;
5355 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5356 q=QueueCacheViewAuthenticPixels(tint_view,0,y,tint_image->columns,1,
5357 exception);
cristy4c08aed2011-07-01 19:47:50 +00005358 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005359 {
5360 status=MagickFalse;
5361 continue;
5362 }
cristybb503372010-05-27 20:51:26 +00005363 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005364 {
cristy4c08aed2011-07-01 19:47:50 +00005365 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00005366 pixel;
5367
5368 MagickRealType
5369 weight;
5370
cristy1707c6c2012-01-18 23:30:54 +00005371 register ssize_t
5372 i;
5373
cristy10a6c612012-01-29 21:41:05 +00005374 if (GetPixelMask(image,p) != 0)
5375 {
5376 p+=GetPixelChannels(image);
5377 q+=GetPixelChannels(tint_image);
5378 continue;
5379 }
cristy1707c6c2012-01-18 23:30:54 +00005380 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5381 {
5382 PixelChannel
5383 channel;
5384
5385 PixelTrait
5386 tint_traits,
5387 traits;
5388
5389 channel=GetPixelChannelMapChannel(image,i);
5390 traits=GetPixelChannelMapTraits(image,channel);
5391 tint_traits=GetPixelChannelMapTraits(tint_image,channel);
5392 if ((traits == UndefinedPixelTrait) ||
5393 (tint_traits == UndefinedPixelTrait))
5394 continue;
5395 if ((tint_traits & CopyPixelTrait) != 0)
5396 {
5397 SetPixelChannel(tint_image,channel,p[i],q);
5398 continue;
5399 }
5400 }
5401 GetPixelInfo(image,&pixel);
5402 weight=QuantumScale*GetPixelRed(image,p)-0.5;
5403 pixel.red=(MagickRealType) GetPixelRed(image,p)+color_vector.red*
5404 (1.0-(4.0*(weight*weight)));
5405 weight=QuantumScale*GetPixelGreen(image,p)-0.5;
5406 pixel.green=(MagickRealType) GetPixelGreen(image,p)+color_vector.green*
5407 (1.0-(4.0*(weight*weight)));
5408 weight=QuantumScale*GetPixelBlue(image,p)-0.5;
5409 pixel.blue=(MagickRealType) GetPixelBlue(image,p)+color_vector.blue*
5410 (1.0-(4.0*(weight*weight)));
5411 weight=QuantumScale*GetPixelBlack(image,p)-0.5;
5412 pixel.black=(MagickRealType) GetPixelBlack(image,p)+color_vector.black*
5413 (1.0-(4.0*(weight*weight)));
5414 SetPixelInfoPixel(tint_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +00005415 p+=GetPixelChannels(image);
5416 q+=GetPixelChannels(tint_image);
cristy3ed852e2009-09-05 21:47:34 +00005417 }
5418 if (SyncCacheViewAuthenticPixels(tint_view,exception) == MagickFalse)
5419 status=MagickFalse;
5420 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5421 {
5422 MagickBooleanType
5423 proceed;
5424
cristyb5d5f722009-11-04 03:03:49 +00005425#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005426 #pragma omp critical (MagickCore_TintImage)
cristy3ed852e2009-09-05 21:47:34 +00005427#endif
5428 proceed=SetImageProgress(image,TintImageTag,progress++,image->rows);
5429 if (proceed == MagickFalse)
5430 status=MagickFalse;
5431 }
5432 }
5433 tint_view=DestroyCacheView(tint_view);
5434 image_view=DestroyCacheView(image_view);
5435 if (status == MagickFalse)
5436 tint_image=DestroyImage(tint_image);
5437 return(tint_image);
5438}
5439
5440/*
5441%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5442% %
5443% %
5444% %
5445% V i g n e t t e I m a g e %
5446% %
5447% %
5448% %
5449%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5450%
5451% VignetteImage() softens the edges of the image in vignette style.
5452%
5453% The format of the VignetteImage method is:
5454%
5455% Image *VignetteImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00005456% const double sigma,const ssize_t x,const ssize_t y,
cristy05c0c9a2011-09-05 23:16:13 +00005457% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005458%
5459% A description of each parameter follows:
5460%
5461% o image: the image.
5462%
5463% o radius: the radius of the pixel neighborhood.
5464%
5465% o sigma: the standard deviation of the Gaussian, in pixels.
5466%
5467% o x, y: Define the x and y ellipse offset.
5468%
5469% o exception: return any errors or warnings in this structure.
5470%
5471*/
5472MagickExport Image *VignetteImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00005473 const double sigma,const ssize_t x,const ssize_t y,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005474{
5475 char
5476 ellipse[MaxTextExtent];
5477
5478 DrawInfo
5479 *draw_info;
5480
5481 Image
5482 *canvas_image,
5483 *blur_image,
5484 *oval_image,
5485 *vignette_image;
5486
5487 assert(image != (Image *) NULL);
5488 assert(image->signature == MagickSignature);
5489 if (image->debug != MagickFalse)
5490 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5491 assert(exception != (ExceptionInfo *) NULL);
5492 assert(exception->signature == MagickSignature);
5493 canvas_image=CloneImage(image,0,0,MagickTrue,exception);
5494 if (canvas_image == (Image *) NULL)
5495 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005496 if (SetImageStorageClass(canvas_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005497 {
cristy3ed852e2009-09-05 21:47:34 +00005498 canvas_image=DestroyImage(canvas_image);
5499 return((Image *) NULL);
5500 }
5501 canvas_image->matte=MagickTrue;
cristy98621462011-12-31 22:31:11 +00005502 oval_image=CloneImage(canvas_image,canvas_image->columns,canvas_image->rows,
5503 MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005504 if (oval_image == (Image *) NULL)
5505 {
5506 canvas_image=DestroyImage(canvas_image);
5507 return((Image *) NULL);
5508 }
cristy9950d572011-10-01 18:22:35 +00005509 (void) QueryColorCompliance("#000000",AllCompliance,
5510 &oval_image->background_color,exception);
cristyea1a8aa2011-10-20 13:24:06 +00005511 (void) SetImageBackgroundColor(oval_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00005512 draw_info=CloneDrawInfo((const ImageInfo *) NULL,(const DrawInfo *) NULL);
cristy9950d572011-10-01 18:22:35 +00005513 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->fill,
5514 exception);
5515 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->stroke,
5516 exception);
cristy1707c6c2012-01-18 23:30:54 +00005517 (void) FormatLocaleString(ellipse,MaxTextExtent,"ellipse %g,%g,%g,%g,"
5518 "0.0,360.0",image->columns/2.0,image->rows/2.0,image->columns/2.0-x,
5519 image->rows/2.0-y);
cristy3ed852e2009-09-05 21:47:34 +00005520 draw_info->primitive=AcquireString(ellipse);
cristy018f07f2011-09-04 21:15:19 +00005521 (void) DrawImage(oval_image,draw_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00005522 draw_info=DestroyDrawInfo(draw_info);
cristyaa2c16c2012-03-25 22:21:35 +00005523 blur_image=BlurImage(oval_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +00005524 oval_image=DestroyImage(oval_image);
5525 if (blur_image == (Image *) NULL)
5526 {
5527 canvas_image=DestroyImage(canvas_image);
5528 return((Image *) NULL);
5529 }
5530 blur_image->matte=MagickFalse;
cristy39172402012-03-30 13:04:39 +00005531 (void) CompositeImage(canvas_image,blur_image,IntensityCompositeOp,MagickTrue,
5532 0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00005533 blur_image=DestroyImage(blur_image);
5534 vignette_image=MergeImageLayers(canvas_image,FlattenLayer,exception);
5535 canvas_image=DestroyImage(canvas_image);
cristy66d26122012-06-23 21:56:40 +00005536 if (vignette_image != (Image *) NULL)
5537 (void) TransformImageColorspace(vignette_image,image->colorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00005538 return(vignette_image);
5539}
5540
5541/*
5542%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5543% %
5544% %
5545% %
5546% W a v e I m a g e %
5547% %
5548% %
5549% %
5550%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5551%
5552% WaveImage() creates a "ripple" effect in the image by shifting the pixels
cristycee97112010-05-28 00:44:52 +00005553% vertically along a sine wave whose amplitude and wavelength is specified
cristy3ed852e2009-09-05 21:47:34 +00005554% by the given parameters.
5555%
5556% The format of the WaveImage method is:
5557%
5558% Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005559% const double wave_length,const PixelInterpolateMethod method,
5560% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005561%
5562% A description of each parameter follows:
5563%
5564% o image: the image.
5565%
5566% o amplitude, wave_length: Define the amplitude and wave length of the
5567% sine wave.
5568%
cristy5c4e2582011-09-11 19:21:03 +00005569% o interpolate: the pixel interpolation method.
5570%
cristy3ed852e2009-09-05 21:47:34 +00005571% o exception: return any errors or warnings in this structure.
5572%
5573*/
5574MagickExport Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005575 const double wave_length,const PixelInterpolateMethod method,
5576 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005577{
5578#define WaveImageTag "Wave/Image"
5579
cristyfa112112010-01-04 17:48:07 +00005580 CacheView
cristyd76c51e2011-03-26 00:21:26 +00005581 *image_view,
cristyfa112112010-01-04 17:48:07 +00005582 *wave_view;
5583
cristy3ed852e2009-09-05 21:47:34 +00005584 Image
5585 *wave_image;
5586
cristy3ed852e2009-09-05 21:47:34 +00005587 MagickBooleanType
5588 status;
5589
cristybb503372010-05-27 20:51:26 +00005590 MagickOffsetType
5591 progress;
5592
cristy3ed852e2009-09-05 21:47:34 +00005593 MagickRealType
5594 *sine_map;
5595
cristybb503372010-05-27 20:51:26 +00005596 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005597 i;
5598
cristybb503372010-05-27 20:51:26 +00005599 ssize_t
5600 y;
5601
cristy3ed852e2009-09-05 21:47:34 +00005602 /*
5603 Initialize wave image attributes.
5604 */
5605 assert(image != (Image *) NULL);
5606 assert(image->signature == MagickSignature);
5607 if (image->debug != MagickFalse)
5608 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5609 assert(exception != (ExceptionInfo *) NULL);
5610 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00005611 wave_image=CloneImage(image,image->columns,(size_t) (image->rows+2.0*
cristy3ed852e2009-09-05 21:47:34 +00005612 fabs(amplitude)),MagickTrue,exception);
5613 if (wave_image == (Image *) NULL)
5614 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005615 if (SetImageStorageClass(wave_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005616 {
cristy3ed852e2009-09-05 21:47:34 +00005617 wave_image=DestroyImage(wave_image);
5618 return((Image *) NULL);
5619 }
cristy4c08aed2011-07-01 19:47:50 +00005620 if (wave_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00005621 wave_image->matte=MagickTrue;
5622 /*
5623 Allocate sine map.
5624 */
5625 sine_map=(MagickRealType *) AcquireQuantumMemory((size_t) wave_image->columns,
5626 sizeof(*sine_map));
5627 if (sine_map == (MagickRealType *) NULL)
5628 {
5629 wave_image=DestroyImage(wave_image);
5630 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
5631 }
cristybb503372010-05-27 20:51:26 +00005632 for (i=0; i < (ssize_t) wave_image->columns; i++)
cristy4205a3c2010-09-12 20:19:59 +00005633 sine_map[i]=fabs(amplitude)+amplitude*sin((double) ((2.0*MagickPI*i)/
5634 wave_length));
cristy3ed852e2009-09-05 21:47:34 +00005635 /*
5636 Wave image.
5637 */
5638 status=MagickTrue;
5639 progress=0;
cristydb070952012-04-20 14:33:00 +00005640 image_view=AcquireVirtualCacheView(image,exception);
5641 wave_view=AcquireAuthenticCacheView(wave_image,exception);
cristyd76c51e2011-03-26 00:21:26 +00005642 (void) SetCacheViewVirtualPixelMethod(image_view,
5643 BackgroundVirtualPixelMethod);
cristyb5d5f722009-11-04 03:03:49 +00005644#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00005645 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00005646 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00005647#endif
cristybb503372010-05-27 20:51:26 +00005648 for (y=0; y < (ssize_t) wave_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005649 {
cristy4c08aed2011-07-01 19:47:50 +00005650 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005651 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005652
cristye97bb922011-04-03 01:36:52 +00005653 register ssize_t
5654 x;
5655
cristy3ed852e2009-09-05 21:47:34 +00005656 if (status == MagickFalse)
5657 continue;
5658 q=QueueCacheViewAuthenticPixels(wave_view,0,y,wave_image->columns,1,
5659 exception);
cristyacd2ed22011-08-30 01:44:23 +00005660 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005661 {
5662 status=MagickFalse;
5663 continue;
5664 }
cristybb503372010-05-27 20:51:26 +00005665 for (x=0; x < (ssize_t) wave_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005666 {
cristy5c4e2582011-09-11 19:21:03 +00005667 status=InterpolatePixelChannels(image,image_view,wave_image,method,
5668 (double) x,(double) (y-sine_map[x]),q,exception);
cristyed231572011-07-14 02:18:59 +00005669 q+=GetPixelChannels(wave_image);
cristy3ed852e2009-09-05 21:47:34 +00005670 }
5671 if (SyncCacheViewAuthenticPixels(wave_view,exception) == MagickFalse)
5672 status=MagickFalse;
5673 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5674 {
5675 MagickBooleanType
5676 proceed;
5677
cristyb5d5f722009-11-04 03:03:49 +00005678#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005679 #pragma omp critical (MagickCore_WaveImage)
cristy3ed852e2009-09-05 21:47:34 +00005680#endif
5681 proceed=SetImageProgress(image,WaveImageTag,progress++,image->rows);
5682 if (proceed == MagickFalse)
5683 status=MagickFalse;
5684 }
5685 }
5686 wave_view=DestroyCacheView(wave_view);
cristyd76c51e2011-03-26 00:21:26 +00005687 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00005688 sine_map=(MagickRealType *) RelinquishMagickMemory(sine_map);
5689 if (status == MagickFalse)
5690 wave_image=DestroyImage(wave_image);
5691 return(wave_image);
5692}