blob: 971524d21842d633911cbf8d48e869bb2f265c1b [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) \
cristyddacdd12012-05-07 23:08:14 +0000315 dynamic_number_threads(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) \
cristyddacdd12012-05-07 23:08:14 +0000482 dynamic_number_threads(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) \
cristyddacdd12012-05-07 23:08:14 +0000745 dynamic_number_threads(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) \
cristyddacdd12012-05-07 23:08:14 +0000952 dynamic_number_threads(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
cristyda1f9c12011-10-02 21:39:49 +00001654 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1655 &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
cristyda1f9c12011-10-02 21:39:49 +00001698 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1699 &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
cristyda1f9c12011-10-02 21:39:49 +00001773 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1774 &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
cristy488fa882010-03-01 22:34:24 +00001904 break;
cristy3ed852e2009-09-05 21:47:34 +00001905 }
cristy62d455f2011-11-03 11:42:28 +00001906 case 'E':
1907 case 'e':
1908 {
1909 if ((LocaleNCompare(expression,"E+",2) == 0) ||
1910 (LocaleNCompare(expression,"E-",2) == 0))
1911 {
1912 expression+=2; /* scientific notation */
1913 break;
1914 }
1915 }
cristy488fa882010-03-01 22:34:24 +00001916 case 'J':
1917 case 'j':
1918 {
1919 if ((LocaleNCompare(expression,"j0",2) == 0) ||
1920 (LocaleNCompare(expression,"j1",2) == 0))
1921 {
1922 expression+=2;
1923 break;
1924 }
1925 break;
1926 }
cristy2def9322010-06-18 23:59:37 +00001927 case '#':
1928 {
1929 while (isxdigit((int) ((unsigned char) *(expression+1))) != 0)
1930 expression++;
1931 break;
1932 }
cristy488fa882010-03-01 22:34:24 +00001933 default:
1934 break;
1935 }
cristy3ed852e2009-09-05 21:47:34 +00001936 if ((c == (int) '{') || (c == (int) '['))
1937 level++;
1938 else
1939 if ((c == (int) '}') || (c == (int) ']'))
1940 level--;
1941 if (level == 0)
1942 switch ((unsigned char) *expression)
1943 {
1944 case '~':
1945 case '!':
1946 {
1947 precedence=BitwiseComplementPrecedence;
1948 break;
1949 }
1950 case '^':
cristy6621e252010-08-13 00:42:57 +00001951 case '@':
cristy3ed852e2009-09-05 21:47:34 +00001952 {
1953 precedence=ExponentPrecedence;
1954 break;
1955 }
1956 default:
1957 {
1958 if (((c != 0) && ((isdigit((int) ((char) c)) != 0) ||
1959 (strchr(")",c) != (char *) NULL))) &&
1960 (((islower((int) ((char) *expression)) != 0) ||
1961 (strchr("(",(int) *expression) != (char *) NULL)) ||
1962 ((isdigit((int) ((char) c)) == 0) &&
1963 (isdigit((int) ((char) *expression)) != 0))) &&
1964 (strchr("xy",(int) *expression) == (char *) NULL))
1965 precedence=MultiplyPrecedence;
1966 break;
1967 }
1968 case '*':
1969 case '/':
1970 case '%':
1971 {
1972 precedence=MultiplyPrecedence;
1973 break;
1974 }
1975 case '+':
1976 case '-':
1977 {
1978 if ((strchr("(+-/*%:&^|<>~,",c) == (char *) NULL) ||
1979 (isalpha(c) != 0))
1980 precedence=AdditionPrecedence;
1981 break;
1982 }
1983 case LeftShiftOperator:
1984 case RightShiftOperator:
1985 {
1986 precedence=ShiftPrecedence;
1987 break;
1988 }
1989 case '<':
1990 case LessThanEqualOperator:
1991 case GreaterThanEqualOperator:
1992 case '>':
1993 {
1994 precedence=RelationalPrecedence;
1995 break;
1996 }
1997 case EqualOperator:
1998 case NotEqualOperator:
1999 {
2000 precedence=EquivalencyPrecedence;
2001 break;
2002 }
2003 case '&':
2004 {
2005 precedence=BitwiseAndPrecedence;
2006 break;
2007 }
2008 case '|':
2009 {
2010 precedence=BitwiseOrPrecedence;
2011 break;
2012 }
2013 case LogicalAndOperator:
2014 {
2015 precedence=LogicalAndPrecedence;
2016 break;
2017 }
2018 case LogicalOrOperator:
2019 {
2020 precedence=LogicalOrPrecedence;
2021 break;
2022 }
cristy116af162010-08-13 01:25:47 +00002023 case ExponentialNotation:
2024 {
2025 precedence=ExponentialNotationPrecedence;
2026 break;
2027 }
cristy3ed852e2009-09-05 21:47:34 +00002028 case ':':
2029 case '?':
2030 {
2031 precedence=TernaryPrecedence;
2032 break;
2033 }
2034 case '=':
2035 {
2036 precedence=AssignmentPrecedence;
2037 break;
2038 }
2039 case ',':
2040 {
2041 precedence=CommaPrecedence;
2042 break;
2043 }
2044 case ';':
2045 {
2046 precedence=SeparatorPrecedence;
2047 break;
2048 }
2049 }
2050 if ((precedence == BitwiseComplementPrecedence) ||
2051 (precedence == TernaryPrecedence) ||
2052 (precedence == AssignmentPrecedence))
2053 {
2054 if (precedence > target)
2055 {
2056 /*
2057 Right-to-left associativity.
2058 */
2059 target=precedence;
2060 subexpression=expression;
2061 }
2062 }
2063 else
2064 if (precedence >= target)
2065 {
2066 /*
2067 Left-to-right associativity.
2068 */
2069 target=precedence;
2070 subexpression=expression;
2071 }
2072 if (strchr("(",(int) *expression) != (char *) NULL)
2073 expression=FxSubexpression(expression,exception);
2074 c=(int) (*expression++);
2075 }
2076 return(subexpression);
2077}
2078
2079static MagickRealType FxEvaluateSubexpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002080 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristye85007d2010-06-06 22:51:36 +00002081 const char *expression,MagickRealType *beta,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002082{
2083 char
2084 *q,
2085 subexpression[MaxTextExtent];
2086
2087 MagickRealType
2088 alpha,
2089 gamma;
2090
2091 register const char
2092 *p;
2093
2094 *beta=0.0;
2095 if (exception->severity != UndefinedException)
2096 return(0.0);
2097 while (isspace((int) *expression) != 0)
2098 expression++;
2099 if (*expression == '\0')
2100 {
2101 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
anthonye5b39652012-04-21 05:37:29 +00002102 "MissingExpression","'%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00002103 return(0.0);
2104 }
cristy66322f02010-05-17 11:40:48 +00002105 *subexpression='\0';
cristy3ed852e2009-09-05 21:47:34 +00002106 p=FxOperatorPrecedence(expression,exception);
2107 if (p != (const char *) NULL)
2108 {
2109 (void) CopyMagickString(subexpression,expression,(size_t)
2110 (p-expression+1));
2111 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2112 exception);
2113 switch ((unsigned char) *p)
2114 {
2115 case '~':
2116 {
2117 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristybb503372010-05-27 20:51:26 +00002118 *beta=(MagickRealType) (~(size_t) *beta);
cristy3ed852e2009-09-05 21:47:34 +00002119 return(*beta);
2120 }
2121 case '!':
2122 {
2123 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2124 return(*beta == 0.0 ? 1.0 : 0.0);
2125 }
2126 case '^':
2127 {
2128 *beta=pow((double) alpha,(double) FxEvaluateSubexpression(fx_info,
2129 channel,x,y,++p,beta,exception));
2130 return(*beta);
2131 }
2132 case '*':
cristy116af162010-08-13 01:25:47 +00002133 case ExponentialNotation:
cristy3ed852e2009-09-05 21:47:34 +00002134 {
2135 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2136 return(alpha*(*beta));
2137 }
2138 case '/':
2139 {
2140 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2141 if (*beta == 0.0)
2142 {
2143 if (exception->severity == UndefinedException)
2144 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00002145 OptionError,"DivideByZero","'%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00002146 return(0.0);
2147 }
2148 return(alpha/(*beta));
2149 }
2150 case '%':
2151 {
2152 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2153 *beta=fabs(floor(((double) *beta)+0.5));
2154 if (*beta == 0.0)
2155 {
2156 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00002157 OptionError,"DivideByZero","'%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00002158 return(0.0);
2159 }
2160 return(fmod((double) alpha,(double) *beta));
2161 }
2162 case '+':
2163 {
2164 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2165 return(alpha+(*beta));
2166 }
2167 case '-':
2168 {
2169 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2170 return(alpha-(*beta));
2171 }
2172 case LeftShiftOperator:
2173 {
2174 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy1707c6c2012-01-18 23:30:54 +00002175 *beta=(MagickRealType) ((size_t) (alpha+0.5) << (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002176 return(*beta);
2177 }
2178 case RightShiftOperator:
2179 {
2180 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy1707c6c2012-01-18 23:30:54 +00002181 *beta=(MagickRealType) ((size_t) (alpha+0.5) >> (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002182 return(*beta);
2183 }
2184 case '<':
2185 {
2186 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2187 return(alpha < *beta ? 1.0 : 0.0);
2188 }
2189 case LessThanEqualOperator:
2190 {
2191 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2192 return(alpha <= *beta ? 1.0 : 0.0);
2193 }
2194 case '>':
2195 {
2196 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2197 return(alpha > *beta ? 1.0 : 0.0);
2198 }
2199 case GreaterThanEqualOperator:
2200 {
2201 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2202 return(alpha >= *beta ? 1.0 : 0.0);
2203 }
2204 case EqualOperator:
2205 {
2206 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2207 return(fabs(alpha-(*beta)) <= MagickEpsilon ? 1.0 : 0.0);
2208 }
2209 case NotEqualOperator:
2210 {
2211 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2212 return(fabs(alpha-(*beta)) > MagickEpsilon ? 1.0 : 0.0);
2213 }
2214 case '&':
2215 {
2216 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy1707c6c2012-01-18 23:30:54 +00002217 *beta=(MagickRealType) ((size_t) (alpha+0.5) & (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002218 return(*beta);
2219 }
2220 case '|':
2221 {
2222 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy1707c6c2012-01-18 23:30:54 +00002223 *beta=(MagickRealType) ((size_t) (alpha+0.5) | (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002224 return(*beta);
2225 }
2226 case LogicalAndOperator:
2227 {
2228 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2229 *beta=(alpha > 0.0) && (gamma > 0.0) ? 1.0 : 0.0;
2230 return(*beta);
2231 }
2232 case LogicalOrOperator:
2233 {
2234 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2235 *beta=(alpha > 0.0) || (gamma > 0.0) ? 1.0 : 0.0;
2236 return(*beta);
2237 }
2238 case '?':
2239 {
2240 MagickRealType
2241 gamma;
2242
2243 (void) CopyMagickString(subexpression,++p,MaxTextExtent);
2244 q=subexpression;
2245 p=StringToken(":",&q);
2246 if (q == (char *) NULL)
2247 {
2248 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00002249 OptionError,"UnableToParseExpression","'%s'",subexpression);
cristy3ed852e2009-09-05 21:47:34 +00002250 return(0.0);
2251 }
2252 if (fabs((double) alpha) > MagickEpsilon)
2253 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,exception);
2254 else
2255 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,q,beta,exception);
2256 return(gamma);
2257 }
2258 case '=':
2259 {
2260 char
2261 numeric[MaxTextExtent];
2262
2263 q=subexpression;
2264 while (isalpha((int) ((unsigned char) *q)) != 0)
2265 q++;
2266 if (*q != '\0')
2267 {
2268 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00002269 OptionError,"UnableToParseExpression","'%s'",subexpression);
cristy3ed852e2009-09-05 21:47:34 +00002270 return(0.0);
2271 }
2272 ClearMagickException(exception);
2273 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristyb51dff52011-05-19 16:55:47 +00002274 (void) FormatLocaleString(numeric,MaxTextExtent,"%g",(double)
cristy8cd5b312010-01-07 01:10:24 +00002275 *beta);
cristy3ed852e2009-09-05 21:47:34 +00002276 (void) DeleteNodeFromSplayTree(fx_info->symbols,subexpression);
2277 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(
2278 subexpression),ConstantString(numeric));
2279 return(*beta);
2280 }
2281 case ',':
2282 {
2283 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2284 return(alpha);
2285 }
2286 case ';':
2287 {
2288 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2289 return(*beta);
2290 }
2291 default:
2292 {
2293 gamma=alpha*FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,
2294 exception);
2295 return(gamma);
2296 }
2297 }
2298 }
2299 if (strchr("(",(int) *expression) != (char *) NULL)
2300 {
2301 (void) CopyMagickString(subexpression,expression+1,MaxTextExtent);
2302 subexpression[strlen(subexpression)-1]='\0';
2303 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2304 exception);
2305 return(gamma);
2306 }
cristy8b8a3ae2010-10-23 18:49:46 +00002307 switch (*expression)
cristy3ed852e2009-09-05 21:47:34 +00002308 {
2309 case '+':
2310 {
2311 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2312 exception);
2313 return(1.0*gamma);
2314 }
2315 case '-':
2316 {
2317 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2318 exception);
2319 return(-1.0*gamma);
2320 }
2321 case '~':
2322 {
2323 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2324 exception);
cristybb503372010-05-27 20:51:26 +00002325 return((MagickRealType) (~(size_t) (gamma+0.5)));
cristy3ed852e2009-09-05 21:47:34 +00002326 }
2327 case 'A':
2328 case 'a':
2329 {
2330 if (LocaleNCompare(expression,"abs",3) == 0)
2331 {
2332 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2333 exception);
2334 return((MagickRealType) fabs((double) alpha));
2335 }
cristyb33454f2011-08-03 02:10:45 +00002336#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00002337 if (LocaleNCompare(expression,"acosh",5) == 0)
2338 {
2339 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2340 exception);
2341 return((MagickRealType) acosh((double) alpha));
2342 }
cristyb33454f2011-08-03 02:10:45 +00002343#endif
cristy3ed852e2009-09-05 21:47:34 +00002344 if (LocaleNCompare(expression,"acos",4) == 0)
2345 {
2346 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2347 exception);
2348 return((MagickRealType) acos((double) alpha));
2349 }
cristy43c22f42010-03-30 12:34:07 +00002350#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002351 if (LocaleNCompare(expression,"airy",4) == 0)
2352 {
2353 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2354 exception);
2355 if (alpha == 0.0)
cristy2dd03222010-03-30 22:12:11 +00002356 return(1.0);
2357 gamma=2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha);
cristy43c22f42010-03-30 12:34:07 +00002358 return(gamma*gamma);
cristyee56cf12010-03-01 22:17:06 +00002359 }
cristy43c22f42010-03-30 12:34:07 +00002360#endif
cristyb33454f2011-08-03 02:10:45 +00002361#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00002362 if (LocaleNCompare(expression,"asinh",5) == 0)
2363 {
2364 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2365 exception);
2366 return((MagickRealType) asinh((double) alpha));
2367 }
cristyb33454f2011-08-03 02:10:45 +00002368#endif
cristy3ed852e2009-09-05 21:47:34 +00002369 if (LocaleNCompare(expression,"asin",4) == 0)
2370 {
2371 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2372 exception);
2373 return((MagickRealType) asin((double) alpha));
2374 }
2375 if (LocaleNCompare(expression,"alt",3) == 0)
2376 {
2377 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2378 exception);
cristybb503372010-05-27 20:51:26 +00002379 return(((ssize_t) alpha) & 0x01 ? -1.0 : 1.0);
cristy3ed852e2009-09-05 21:47:34 +00002380 }
2381 if (LocaleNCompare(expression,"atan2",5) == 0)
2382 {
2383 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2384 exception);
2385 return((MagickRealType) atan2((double) alpha,(double) *beta));
2386 }
cristyb33454f2011-08-03 02:10:45 +00002387#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00002388 if (LocaleNCompare(expression,"atanh",5) == 0)
2389 {
2390 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2391 exception);
2392 return((MagickRealType) atanh((double) alpha));
2393 }
cristyb33454f2011-08-03 02:10:45 +00002394#endif
cristy3ed852e2009-09-05 21:47:34 +00002395 if (LocaleNCompare(expression,"atan",4) == 0)
2396 {
2397 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2398 exception);
2399 return((MagickRealType) atan((double) alpha));
2400 }
2401 if (LocaleCompare(expression,"a") == 0)
2402 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2403 break;
2404 }
2405 case 'B':
2406 case 'b':
2407 {
2408 if (LocaleCompare(expression,"b") == 0)
2409 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2410 break;
2411 }
2412 case 'C':
2413 case 'c':
2414 {
2415 if (LocaleNCompare(expression,"ceil",4) == 0)
2416 {
2417 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2418 exception);
2419 return((MagickRealType) ceil((double) alpha));
2420 }
2421 if (LocaleNCompare(expression,"cosh",4) == 0)
2422 {
2423 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2424 exception);
2425 return((MagickRealType) cosh((double) alpha));
2426 }
2427 if (LocaleNCompare(expression,"cos",3) == 0)
2428 {
2429 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2430 exception);
2431 return((MagickRealType) cos((double) alpha));
2432 }
2433 if (LocaleCompare(expression,"c") == 0)
2434 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2435 break;
2436 }
2437 case 'D':
2438 case 'd':
2439 {
2440 if (LocaleNCompare(expression,"debug",5) == 0)
2441 {
2442 const char
2443 *type;
2444
2445 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2446 exception);
2447 if (fx_info->images->colorspace == CMYKColorspace)
2448 switch (channel)
2449 {
cristy0568ffc2011-07-25 16:54:14 +00002450 case CyanPixelChannel: type="cyan"; break;
2451 case MagentaPixelChannel: type="magenta"; break;
2452 case YellowPixelChannel: type="yellow"; break;
2453 case AlphaPixelChannel: type="opacity"; break;
2454 case BlackPixelChannel: type="black"; break;
cristy3ed852e2009-09-05 21:47:34 +00002455 default: type="unknown"; break;
2456 }
2457 else
2458 switch (channel)
2459 {
cristy0568ffc2011-07-25 16:54:14 +00002460 case RedPixelChannel: type="red"; break;
2461 case GreenPixelChannel: type="green"; break;
2462 case BluePixelChannel: type="blue"; break;
2463 case AlphaPixelChannel: type="opacity"; break;
cristy3ed852e2009-09-05 21:47:34 +00002464 default: type="unknown"; break;
2465 }
2466 (void) CopyMagickString(subexpression,expression+6,MaxTextExtent);
2467 if (strlen(subexpression) > 1)
2468 subexpression[strlen(subexpression)-1]='\0';
2469 if (fx_info->file != (FILE *) NULL)
cristy1707c6c2012-01-18 23:30:54 +00002470 (void) FormatLocaleFile(fx_info->file,"%s[%.20g,%.20g].%s: "
2471 "%s=%.*g\n",fx_info->images->filename,(double) x,(double) y,type,
2472 subexpression,GetMagickPrecision(),(double) alpha);
cristy3ed852e2009-09-05 21:47:34 +00002473 return(0.0);
2474 }
cristy5597a8d2011-11-04 00:25:32 +00002475 if (LocaleNCompare(expression,"drc",3) == 0)
2476 {
2477 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2478 exception);
2479 return((MagickRealType) (alpha/(*beta*(alpha-1.0)+1.0)));
2480 }
cristy3ed852e2009-09-05 21:47:34 +00002481 break;
2482 }
2483 case 'E':
2484 case 'e':
2485 {
2486 if (LocaleCompare(expression,"epsilon") == 0)
2487 return((MagickRealType) MagickEpsilon);
2488 if (LocaleNCompare(expression,"exp",3) == 0)
2489 {
2490 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2491 exception);
2492 return((MagickRealType) exp((double) alpha));
2493 }
2494 if (LocaleCompare(expression,"e") == 0)
2495 return((MagickRealType) 2.7182818284590452354);
2496 break;
2497 }
2498 case 'F':
2499 case 'f':
2500 {
2501 if (LocaleNCompare(expression,"floor",5) == 0)
2502 {
2503 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2504 exception);
2505 return((MagickRealType) floor((double) alpha));
2506 }
2507 break;
2508 }
2509 case 'G':
2510 case 'g':
2511 {
cristy9eeedea2011-11-02 19:04:05 +00002512 if (LocaleNCompare(expression,"gauss",5) == 0)
2513 {
2514 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2515 exception);
2516 gamma=exp((double) (-alpha*alpha/2.0))/sqrt(2.0*MagickPI);
2517 return((MagickRealType) gamma);
2518 }
cristyb0aad4c2011-11-02 19:30:35 +00002519 if (LocaleNCompare(expression,"gcd",3) == 0)
2520 {
2521 MagickOffsetType
2522 gcd;
2523
2524 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2525 exception);
cristy1707c6c2012-01-18 23:30:54 +00002526 gcd=FxGCD((MagickOffsetType) (alpha+0.5),(MagickOffsetType) (*beta+
2527 0.5));
cristyb0aad4c2011-11-02 19:30:35 +00002528 return((MagickRealType) gcd);
2529 }
cristy3ed852e2009-09-05 21:47:34 +00002530 if (LocaleCompare(expression,"g") == 0)
2531 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2532 break;
2533 }
2534 case 'H':
2535 case 'h':
2536 {
2537 if (LocaleCompare(expression,"h") == 0)
2538 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2539 if (LocaleCompare(expression,"hue") == 0)
2540 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2541 if (LocaleNCompare(expression,"hypot",5) == 0)
2542 {
2543 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2544 exception);
2545 return((MagickRealType) hypot((double) alpha,(double) *beta));
2546 }
2547 break;
2548 }
2549 case 'K':
2550 case 'k':
2551 {
2552 if (LocaleCompare(expression,"k") == 0)
2553 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2554 break;
2555 }
2556 case 'I':
2557 case 'i':
2558 {
2559 if (LocaleCompare(expression,"intensity") == 0)
2560 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2561 if (LocaleNCompare(expression,"int",3) == 0)
2562 {
2563 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2564 exception);
cristy16788e42010-08-13 13:44:26 +00002565 return((MagickRealType) floor(alpha));
cristy3ed852e2009-09-05 21:47:34 +00002566 }
cristy82b20722011-11-05 21:52:36 +00002567#if defined(MAGICKCORE_HAVE_ISNAN)
cristy639399c2011-11-02 19:16:15 +00002568 if (LocaleNCompare(expression,"isnan",5) == 0)
2569 {
2570 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2571 exception);
cristy17a10202011-11-02 19:17:04 +00002572 return((MagickRealType) !!isnan((double) alpha));
cristy639399c2011-11-02 19:16:15 +00002573 }
cristy82b20722011-11-05 21:52:36 +00002574#endif
cristy3ed852e2009-09-05 21:47:34 +00002575 if (LocaleCompare(expression,"i") == 0)
2576 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2577 break;
2578 }
2579 case 'J':
2580 case 'j':
2581 {
2582 if (LocaleCompare(expression,"j") == 0)
2583 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
cristy161b9262010-03-20 19:34:32 +00002584#if defined(MAGICKCORE_HAVE_J0)
cristyee56cf12010-03-01 22:17:06 +00002585 if (LocaleNCompare(expression,"j0",2) == 0)
2586 {
2587 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2588 exception);
2589 return((MagickRealType) j0((double) alpha));
2590 }
cristy161b9262010-03-20 19:34:32 +00002591#endif
2592#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002593 if (LocaleNCompare(expression,"j1",2) == 0)
2594 {
2595 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2596 exception);
2597 return((MagickRealType) j1((double) alpha));
2598 }
cristy161b9262010-03-20 19:34:32 +00002599#endif
cristyaa018fa2010-04-08 23:03:54 +00002600#if defined(MAGICKCORE_HAVE_J1)
cristya6a09e72010-03-02 14:51:02 +00002601 if (LocaleNCompare(expression,"jinc",4) == 0)
2602 {
2603 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2604 exception);
cristy0946a822010-03-12 17:14:58 +00002605 if (alpha == 0.0)
2606 return(1.0);
cristy1707c6c2012-01-18 23:30:54 +00002607 gamma=(MagickRealType) (2.0*j1((double) (MagickPI*alpha))/(MagickPI*
2608 alpha));
cristyfce2f7b2010-03-12 00:29:49 +00002609 return(gamma);
cristya6a09e72010-03-02 14:51:02 +00002610 }
cristyaa018fa2010-04-08 23:03:54 +00002611#endif
cristy3ed852e2009-09-05 21:47:34 +00002612 break;
2613 }
2614 case 'L':
2615 case 'l':
2616 {
2617 if (LocaleNCompare(expression,"ln",2) == 0)
2618 {
2619 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2620 exception);
2621 return((MagickRealType) log((double) alpha));
2622 }
cristyc8ed5322010-08-31 12:07:59 +00002623 if (LocaleNCompare(expression,"logtwo",6) == 0)
cristy3ed852e2009-09-05 21:47:34 +00002624 {
cristyc8ed5322010-08-31 12:07:59 +00002625 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
cristy3ed852e2009-09-05 21:47:34 +00002626 exception);
2627 return((MagickRealType) log10((double) alpha))/log10(2.0);
2628 }
2629 if (LocaleNCompare(expression,"log",3) == 0)
2630 {
2631 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2632 exception);
2633 return((MagickRealType) log10((double) alpha));
2634 }
2635 if (LocaleCompare(expression,"lightness") == 0)
2636 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2637 break;
2638 }
2639 case 'M':
2640 case 'm':
2641 {
2642 if (LocaleCompare(expression,"MaxRGB") == 0)
2643 return((MagickRealType) QuantumRange);
2644 if (LocaleNCompare(expression,"maxima",6) == 0)
2645 break;
2646 if (LocaleNCompare(expression,"max",3) == 0)
cristy984049c2011-11-03 18:34:58 +00002647 {
2648 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2649 exception);
2650 return(alpha > *beta ? alpha : *beta);
2651 }
cristy3ed852e2009-09-05 21:47:34 +00002652 if (LocaleNCompare(expression,"minima",6) == 0)
2653 break;
2654 if (LocaleNCompare(expression,"min",3) == 0)
cristy984049c2011-11-03 18:34:58 +00002655 {
2656 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2657 exception);
2658 return(alpha < *beta ? alpha : *beta);
2659 }
cristy3ed852e2009-09-05 21:47:34 +00002660 if (LocaleNCompare(expression,"mod",3) == 0)
2661 {
2662 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2663 exception);
cristy984049c2011-11-03 18:34:58 +00002664 gamma=alpha-floor((double) (alpha/(*beta)))*(*beta);
2665 return(gamma);
cristy3ed852e2009-09-05 21:47:34 +00002666 }
2667 if (LocaleCompare(expression,"m") == 0)
2668 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2669 break;
2670 }
2671 case 'N':
2672 case 'n':
2673 {
cristyad3502e2011-11-02 19:10:45 +00002674 if (LocaleNCompare(expression,"not",3) == 0)
2675 {
2676 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2677 exception);
2678 return((MagickRealType) (alpha < MagickEpsilon));
2679 }
cristy3ed852e2009-09-05 21:47:34 +00002680 if (LocaleCompare(expression,"n") == 0)
2681 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2682 break;
2683 }
2684 case 'O':
2685 case 'o':
2686 {
2687 if (LocaleCompare(expression,"Opaque") == 0)
2688 return(1.0);
2689 if (LocaleCompare(expression,"o") == 0)
2690 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2691 break;
2692 }
2693 case 'P':
2694 case 'p':
2695 {
cristy670aa3c2011-11-03 00:54:00 +00002696 if (LocaleCompare(expression,"phi") == 0)
2697 return((MagickRealType) MagickPHI);
cristy3ed852e2009-09-05 21:47:34 +00002698 if (LocaleCompare(expression,"pi") == 0)
2699 return((MagickRealType) MagickPI);
2700 if (LocaleNCompare(expression,"pow",3) == 0)
2701 {
2702 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2703 exception);
2704 return((MagickRealType) pow((double) alpha,(double) *beta));
2705 }
2706 if (LocaleCompare(expression,"p") == 0)
2707 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2708 break;
2709 }
2710 case 'Q':
2711 case 'q':
2712 {
2713 if (LocaleCompare(expression,"QuantumRange") == 0)
2714 return((MagickRealType) QuantumRange);
2715 if (LocaleCompare(expression,"QuantumScale") == 0)
2716 return((MagickRealType) QuantumScale);
2717 break;
2718 }
2719 case 'R':
2720 case 'r':
2721 {
2722 if (LocaleNCompare(expression,"rand",4) == 0)
2723 return((MagickRealType) GetPseudoRandomValue(fx_info->random_info));
2724 if (LocaleNCompare(expression,"round",5) == 0)
2725 {
2726 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2727 exception);
cristy16788e42010-08-13 13:44:26 +00002728 return((MagickRealType) floor((double) alpha+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002729 }
2730 if (LocaleCompare(expression,"r") == 0)
2731 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2732 break;
2733 }
2734 case 'S':
2735 case 's':
2736 {
2737 if (LocaleCompare(expression,"saturation") == 0)
2738 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2739 if (LocaleNCompare(expression,"sign",4) == 0)
2740 {
2741 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2742 exception);
2743 return(alpha < 0.0 ? -1.0 : 1.0);
2744 }
cristya6a09e72010-03-02 14:51:02 +00002745 if (LocaleNCompare(expression,"sinc",4) == 0)
2746 {
2747 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2748 exception);
2749 if (alpha == 0)
2750 return(1.0);
2751 gamma=(MagickRealType) (sin((double) (MagickPI*alpha))/
2752 (MagickPI*alpha));
2753 return(gamma);
2754 }
cristy3ed852e2009-09-05 21:47:34 +00002755 if (LocaleNCompare(expression,"sinh",4) == 0)
2756 {
2757 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2758 exception);
2759 return((MagickRealType) sinh((double) alpha));
2760 }
2761 if (LocaleNCompare(expression,"sin",3) == 0)
2762 {
2763 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2764 exception);
2765 return((MagickRealType) sin((double) alpha));
2766 }
2767 if (LocaleNCompare(expression,"sqrt",4) == 0)
2768 {
2769 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2770 exception);
2771 return((MagickRealType) sqrt((double) alpha));
2772 }
cristy9eeedea2011-11-02 19:04:05 +00002773 if (LocaleNCompare(expression,"squish",6) == 0)
2774 {
2775 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2776 exception);
cristyf7b28ea2012-04-27 11:22:49 +00002777 return((MagickRealType) (1.0/(1.0+exp((double) (-alpha)))));
cristy9eeedea2011-11-02 19:04:05 +00002778 }
cristy3ed852e2009-09-05 21:47:34 +00002779 if (LocaleCompare(expression,"s") == 0)
2780 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2781 break;
2782 }
2783 case 'T':
2784 case 't':
2785 {
2786 if (LocaleNCompare(expression,"tanh",4) == 0)
2787 {
2788 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2789 exception);
2790 return((MagickRealType) tanh((double) alpha));
2791 }
2792 if (LocaleNCompare(expression,"tan",3) == 0)
2793 {
2794 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2795 exception);
2796 return((MagickRealType) tan((double) alpha));
2797 }
2798 if (LocaleCompare(expression,"Transparent") == 0)
2799 return(0.0);
cristy16788e42010-08-13 13:44:26 +00002800 if (LocaleNCompare(expression,"trunc",5) == 0)
2801 {
2802 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2803 exception);
2804 if (alpha >= 0.0)
2805 return((MagickRealType) floor((double) alpha));
2806 return((MagickRealType) ceil((double) alpha));
2807 }
cristy3ed852e2009-09-05 21:47:34 +00002808 if (LocaleCompare(expression,"t") == 0)
2809 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2810 break;
2811 }
2812 case 'U':
2813 case 'u':
2814 {
2815 if (LocaleCompare(expression,"u") == 0)
2816 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2817 break;
2818 }
2819 case 'V':
2820 case 'v':
2821 {
2822 if (LocaleCompare(expression,"v") == 0)
2823 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2824 break;
2825 }
2826 case 'W':
2827 case 'w':
2828 {
cristy9eeedea2011-11-02 19:04:05 +00002829 if (LocaleNCompare(expression,"while",5) == 0)
2830 {
2831 do
2832 {
2833 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2834 exception);
2835 } while (fabs((double) alpha) >= MagickEpsilon);
2836 return((MagickRealType) *beta);
2837 }
cristy3ed852e2009-09-05 21:47:34 +00002838 if (LocaleCompare(expression,"w") == 0)
2839 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2840 break;
2841 }
2842 case 'Y':
2843 case 'y':
2844 {
2845 if (LocaleCompare(expression,"y") == 0)
2846 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2847 break;
2848 }
2849 case 'Z':
2850 case 'z':
2851 {
2852 if (LocaleCompare(expression,"z") == 0)
2853 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2854 break;
2855 }
2856 default:
2857 break;
2858 }
2859 q=(char *) expression;
cristydbdd0e32011-11-04 23:29:40 +00002860 alpha=InterpretSiPrefixValue(expression,&q);
cristy3ed852e2009-09-05 21:47:34 +00002861 if (q == expression)
2862 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2863 return(alpha);
2864}
2865
cristy7832dc22011-09-05 01:21:53 +00002866MagickPrivate MagickBooleanType FxEvaluateExpression(FxInfo *fx_info,
cristy3ed852e2009-09-05 21:47:34 +00002867 MagickRealType *alpha,ExceptionInfo *exception)
2868{
2869 MagickBooleanType
2870 status;
2871
cristy541ae572011-08-05 19:08:59 +00002872 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2873 exception);
cristy3ed852e2009-09-05 21:47:34 +00002874 return(status);
2875}
2876
2877MagickExport MagickBooleanType FxPreprocessExpression(FxInfo *fx_info,
2878 MagickRealType *alpha,ExceptionInfo *exception)
2879{
2880 FILE
2881 *file;
2882
2883 MagickBooleanType
2884 status;
2885
2886 file=fx_info->file;
2887 fx_info->file=(FILE *) NULL;
cristy541ae572011-08-05 19:08:59 +00002888 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2889 exception);
cristy3ed852e2009-09-05 21:47:34 +00002890 fx_info->file=file;
2891 return(status);
2892}
2893
cristy7832dc22011-09-05 01:21:53 +00002894MagickPrivate MagickBooleanType FxEvaluateChannelExpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002895 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristye85007d2010-06-06 22:51:36 +00002896 MagickRealType *alpha,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002897{
2898 MagickRealType
2899 beta;
2900
2901 beta=0.0;
2902 *alpha=FxEvaluateSubexpression(fx_info,channel,x,y,fx_info->expression,&beta,
2903 exception);
2904 return(exception->severity == OptionError ? MagickFalse : MagickTrue);
2905}
2906
2907/*
2908%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2909% %
2910% %
2911% %
2912% F x I m a g e %
2913% %
2914% %
2915% %
2916%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2917%
2918% FxImage() applies a mathematical expression to the specified image.
2919%
2920% The format of the FxImage method is:
2921%
2922% Image *FxImage(const Image *image,const char *expression,
2923% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002924%
2925% A description of each parameter follows:
2926%
2927% o image: the image.
2928%
cristy3ed852e2009-09-05 21:47:34 +00002929% o expression: A mathematical expression.
2930%
2931% o exception: return any errors or warnings in this structure.
2932%
2933*/
2934
2935static FxInfo **DestroyFxThreadSet(FxInfo **fx_info)
2936{
cristybb503372010-05-27 20:51:26 +00002937 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002938 i;
2939
2940 assert(fx_info != (FxInfo **) NULL);
cristyac245f82012-05-05 17:13:57 +00002941 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
cristy3ed852e2009-09-05 21:47:34 +00002942 if (fx_info[i] != (FxInfo *) NULL)
2943 fx_info[i]=DestroyFxInfo(fx_info[i]);
cristyb41ee102010-10-04 16:46:15 +00002944 fx_info=(FxInfo **) RelinquishMagickMemory(fx_info);
cristy3ed852e2009-09-05 21:47:34 +00002945 return(fx_info);
2946}
2947
2948static FxInfo **AcquireFxThreadSet(const Image *image,const char *expression,
2949 ExceptionInfo *exception)
2950{
2951 char
2952 *fx_expression;
2953
2954 FxInfo
2955 **fx_info;
2956
2957 MagickRealType
2958 alpha;
2959
cristybb503372010-05-27 20:51:26 +00002960 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002961 i;
2962
cristybb503372010-05-27 20:51:26 +00002963 size_t
cristy3ed852e2009-09-05 21:47:34 +00002964 number_threads;
2965
cristyfeeb98d2012-05-09 16:32:12 +00002966 number_threads=GetOpenMPMaximumThreads();
cristyb41ee102010-10-04 16:46:15 +00002967 fx_info=(FxInfo **) AcquireQuantumMemory(number_threads,sizeof(*fx_info));
cristy3ed852e2009-09-05 21:47:34 +00002968 if (fx_info == (FxInfo **) NULL)
2969 return((FxInfo **) NULL);
2970 (void) ResetMagickMemory(fx_info,0,number_threads*sizeof(*fx_info));
2971 if (*expression != '@')
2972 fx_expression=ConstantString(expression);
2973 else
2974 fx_expression=FileToString(expression+1,~0,exception);
cristybb503372010-05-27 20:51:26 +00002975 for (i=0; i < (ssize_t) number_threads; i++)
cristy3ed852e2009-09-05 21:47:34 +00002976 {
cristydb070952012-04-20 14:33:00 +00002977 fx_info[i]=AcquireFxInfo(image,fx_expression,exception);
cristy3ed852e2009-09-05 21:47:34 +00002978 if (fx_info[i] == (FxInfo *) NULL)
2979 return(DestroyFxThreadSet(fx_info));
2980 (void) FxPreprocessExpression(fx_info[i],&alpha,fx_info[i]->exception);
2981 }
2982 fx_expression=DestroyString(fx_expression);
2983 return(fx_info);
2984}
2985
2986MagickExport Image *FxImage(const Image *image,const char *expression,
2987 ExceptionInfo *exception)
2988{
cristy3ed852e2009-09-05 21:47:34 +00002989#define FxImageTag "Fx/Image"
2990
cristyfa112112010-01-04 17:48:07 +00002991 CacheView
cristy79cedc72011-07-25 00:41:15 +00002992 *fx_view,
2993 *image_view;
cristyfa112112010-01-04 17:48:07 +00002994
cristy3ed852e2009-09-05 21:47:34 +00002995 FxInfo
cristyfa112112010-01-04 17:48:07 +00002996 **restrict fx_info;
cristy3ed852e2009-09-05 21:47:34 +00002997
2998 Image
2999 *fx_image;
3000
cristy3ed852e2009-09-05 21:47:34 +00003001 MagickBooleanType
3002 status;
3003
cristybb503372010-05-27 20:51:26 +00003004 MagickOffsetType
3005 progress;
3006
cristy3ed852e2009-09-05 21:47:34 +00003007 MagickRealType
3008 alpha;
3009
cristybb503372010-05-27 20:51:26 +00003010 ssize_t
3011 y;
3012
cristy3ed852e2009-09-05 21:47:34 +00003013 assert(image != (Image *) NULL);
3014 assert(image->signature == MagickSignature);
3015 if (image->debug != MagickFalse)
3016 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy79cedc72011-07-25 00:41:15 +00003017 fx_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00003018 if (fx_image == (Image *) NULL)
3019 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003020 if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003021 {
cristy3ed852e2009-09-05 21:47:34 +00003022 fx_image=DestroyImage(fx_image);
3023 return((Image *) NULL);
3024 }
3025 fx_info=AcquireFxThreadSet(image,expression,exception);
3026 if (fx_info == (FxInfo **) NULL)
3027 {
3028 fx_image=DestroyImage(fx_image);
3029 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3030 }
3031 status=FxPreprocessExpression(fx_info[0],&alpha,exception);
3032 if (status == MagickFalse)
3033 {
3034 fx_image=DestroyImage(fx_image);
3035 fx_info=DestroyFxThreadSet(fx_info);
3036 return((Image *) NULL);
3037 }
3038 /*
3039 Fx image.
3040 */
3041 status=MagickTrue;
3042 progress=0;
cristydb070952012-04-20 14:33:00 +00003043 image_view=AcquireVirtualCacheView(image,exception);
3044 fx_view=AcquireAuthenticCacheView(fx_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00003045#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003046 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristyddacdd12012-05-07 23:08:14 +00003047 dynamic_number_threads(image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003048#endif
cristybb503372010-05-27 20:51:26 +00003049 for (y=0; y < (ssize_t) fx_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003050 {
cristy5c9e6f22010-09-17 17:31:01 +00003051 const int
3052 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00003053
cristy79cedc72011-07-25 00:41:15 +00003054 register const Quantum
3055 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003056
cristy4c08aed2011-07-01 19:47:50 +00003057 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003058 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003059
cristy79cedc72011-07-25 00:41:15 +00003060 register ssize_t
3061 x;
3062
cristy3ed852e2009-09-05 21:47:34 +00003063 if (status == MagickFalse)
3064 continue;
cristy79cedc72011-07-25 00:41:15 +00003065 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +00003066 q=QueueCacheViewAuthenticPixels(fx_view,0,y,fx_image->columns,1,exception);
cristy79cedc72011-07-25 00:41:15 +00003067 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003068 {
3069 status=MagickFalse;
3070 continue;
3071 }
cristybb503372010-05-27 20:51:26 +00003072 for (x=0; x < (ssize_t) fx_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003073 {
cristy79cedc72011-07-25 00:41:15 +00003074 register ssize_t
3075 i;
3076
cristy177e41c2012-04-15 15:08:25 +00003077 if (GetPixelMask(image,p) != 0)
3078 {
3079 p+=GetPixelChannels(image);
3080 q+=GetPixelChannels(fx_image);
3081 continue;
3082 }
cristy79cedc72011-07-25 00:41:15 +00003083 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3084 {
3085 MagickRealType
3086 alpha;
3087
3088 PixelChannel
3089 channel;
3090
3091 PixelTrait
3092 fx_traits,
3093 traits;
3094
cristye2a912b2011-12-05 20:02:07 +00003095 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003096 traits=GetPixelChannelMapTraits(image,channel);
cristy79cedc72011-07-25 00:41:15 +00003097 fx_traits=GetPixelChannelMapTraits(fx_image,channel);
cristy010d7d12011-08-31 01:02:48 +00003098 if ((traits == UndefinedPixelTrait) ||
3099 (fx_traits == UndefinedPixelTrait))
cristy79cedc72011-07-25 00:41:15 +00003100 continue;
cristy177e41c2012-04-15 15:08:25 +00003101 if ((fx_traits & CopyPixelTrait) != 0)
cristy79cedc72011-07-25 00:41:15 +00003102 {
cristy0beccfa2011-09-25 20:47:53 +00003103 SetPixelChannel(fx_image,channel,p[i],q);
cristy79cedc72011-07-25 00:41:15 +00003104 continue;
3105 }
3106 alpha=0.0;
cristya382aca2011-12-06 18:22:48 +00003107 (void) FxEvaluateChannelExpression(fx_info[id],channel,x,y,&alpha,
3108 exception);
cristyb3a73b52011-07-26 01:34:43 +00003109 q[i]=ClampToQuantum((MagickRealType) QuantumRange*alpha);
cristy79cedc72011-07-25 00:41:15 +00003110 }
3111 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003112 q+=GetPixelChannels(fx_image);
cristy3ed852e2009-09-05 21:47:34 +00003113 }
3114 if (SyncCacheViewAuthenticPixels(fx_view,exception) == MagickFalse)
3115 status=MagickFalse;
3116 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3117 {
3118 MagickBooleanType
3119 proceed;
3120
cristyb5d5f722009-11-04 03:03:49 +00003121#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003122 #pragma omp critical (MagickCore_FxImage)
cristy3ed852e2009-09-05 21:47:34 +00003123#endif
3124 proceed=SetImageProgress(image,FxImageTag,progress++,image->rows);
3125 if (proceed == MagickFalse)
3126 status=MagickFalse;
3127 }
3128 }
cristy3ed852e2009-09-05 21:47:34 +00003129 fx_view=DestroyCacheView(fx_view);
cristy79cedc72011-07-25 00:41:15 +00003130 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003131 fx_info=DestroyFxThreadSet(fx_info);
3132 if (status == MagickFalse)
3133 fx_image=DestroyImage(fx_image);
3134 return(fx_image);
3135}
3136
3137/*
3138%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3139% %
3140% %
3141% %
3142% I m p l o d e I m a g e %
3143% %
3144% %
3145% %
3146%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3147%
3148% ImplodeImage() creates a new image that is a copy of an existing
3149% one with the image pixels "implode" by the specified percentage. It
3150% allocates the memory necessary for the new Image structure and returns a
3151% pointer to the new image.
3152%
3153% The format of the ImplodeImage method is:
3154%
3155% Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003156% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003157%
3158% A description of each parameter follows:
3159%
3160% o implode_image: Method ImplodeImage returns a pointer to the image
3161% after it is implode. A null image is returned if there is a memory
3162% shortage.
3163%
3164% o image: the image.
3165%
3166% o amount: Define the extent of the implosion.
3167%
cristy76f512e2011-09-12 01:26:56 +00003168% o method: the pixel interpolation method.
3169%
cristy3ed852e2009-09-05 21:47:34 +00003170% o exception: return any errors or warnings in this structure.
3171%
3172*/
3173MagickExport Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003174 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003175{
3176#define ImplodeImageTag "Implode/Image"
3177
cristyfa112112010-01-04 17:48:07 +00003178 CacheView
3179 *image_view,
3180 *implode_view;
3181
cristy3ed852e2009-09-05 21:47:34 +00003182 Image
3183 *implode_image;
3184
cristy3ed852e2009-09-05 21:47:34 +00003185 MagickBooleanType
3186 status;
3187
cristybb503372010-05-27 20:51:26 +00003188 MagickOffsetType
3189 progress;
3190
cristy3ed852e2009-09-05 21:47:34 +00003191 MagickRealType
3192 radius;
3193
3194 PointInfo
3195 center,
3196 scale;
3197
cristybb503372010-05-27 20:51:26 +00003198 ssize_t
3199 y;
3200
cristy3ed852e2009-09-05 21:47:34 +00003201 /*
3202 Initialize implode image attributes.
3203 */
3204 assert(image != (Image *) NULL);
3205 assert(image->signature == MagickSignature);
3206 if (image->debug != MagickFalse)
3207 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3208 assert(exception != (ExceptionInfo *) NULL);
3209 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00003210 implode_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3211 exception);
cristy3ed852e2009-09-05 21:47:34 +00003212 if (implode_image == (Image *) NULL)
3213 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003214 if (SetImageStorageClass(implode_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003215 {
cristy3ed852e2009-09-05 21:47:34 +00003216 implode_image=DestroyImage(implode_image);
3217 return((Image *) NULL);
3218 }
cristy4c08aed2011-07-01 19:47:50 +00003219 if (implode_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00003220 implode_image->matte=MagickTrue;
3221 /*
3222 Compute scaling factor.
3223 */
3224 scale.x=1.0;
3225 scale.y=1.0;
3226 center.x=0.5*image->columns;
3227 center.y=0.5*image->rows;
3228 radius=center.x;
3229 if (image->columns > image->rows)
3230 scale.y=(double) image->columns/(double) image->rows;
3231 else
3232 if (image->columns < image->rows)
3233 {
3234 scale.x=(double) image->rows/(double) image->columns;
3235 radius=center.y;
3236 }
3237 /*
3238 Implode image.
3239 */
3240 status=MagickTrue;
3241 progress=0;
cristydb070952012-04-20 14:33:00 +00003242 image_view=AcquireVirtualCacheView(image,exception);
3243 implode_view=AcquireAuthenticCacheView(implode_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00003244#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003245 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristyddacdd12012-05-07 23:08:14 +00003246 dynamic_number_threads(image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003247#endif
cristybb503372010-05-27 20:51:26 +00003248 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003249 {
cristy3ed852e2009-09-05 21:47:34 +00003250 MagickRealType
3251 distance;
3252
3253 PointInfo
3254 delta;
3255
cristy6d188022011-09-12 13:23:33 +00003256 register const Quantum
3257 *restrict p;
3258
cristybb503372010-05-27 20:51:26 +00003259 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003260 x;
3261
cristy4c08aed2011-07-01 19:47:50 +00003262 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003263 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003264
3265 if (status == MagickFalse)
3266 continue;
cristy6d188022011-09-12 13:23:33 +00003267 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +00003268 q=QueueCacheViewAuthenticPixels(implode_view,0,y,implode_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003269 exception);
cristy6d188022011-09-12 13:23:33 +00003270 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003271 {
3272 status=MagickFalse;
3273 continue;
3274 }
cristy3ed852e2009-09-05 21:47:34 +00003275 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00003276 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003277 {
cristy6d188022011-09-12 13:23:33 +00003278 register ssize_t
3279 i;
3280
cristy3ed852e2009-09-05 21:47:34 +00003281 /*
3282 Determine if the pixel is within an ellipse.
3283 */
cristy10a6c612012-01-29 21:41:05 +00003284 if (GetPixelMask(image,p) != 0)
3285 {
3286 p+=GetPixelChannels(image);
3287 q+=GetPixelChannels(implode_image);
3288 continue;
3289 }
cristy3ed852e2009-09-05 21:47:34 +00003290 delta.x=scale.x*(double) (x-center.x);
3291 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00003292 if (distance >= (radius*radius))
cristya6d7a9b2012-01-18 20:04:48 +00003293 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy6d188022011-09-12 13:23:33 +00003294 {
cristya6d7a9b2012-01-18 20:04:48 +00003295 PixelChannel
3296 channel;
3297
cristy1707c6c2012-01-18 23:30:54 +00003298 PixelTrait
3299 implode_traits,
3300 traits;
3301
cristya6d7a9b2012-01-18 20:04:48 +00003302 channel=GetPixelChannelMapChannel(image,i);
cristy1707c6c2012-01-18 23:30:54 +00003303 traits=GetPixelChannelMapTraits(image,channel);
3304 implode_traits=GetPixelChannelMapTraits(implode_image,channel);
3305 if ((traits == UndefinedPixelTrait) ||
3306 (implode_traits == UndefinedPixelTrait))
3307 continue;
cristya6d7a9b2012-01-18 20:04:48 +00003308 SetPixelChannel(implode_image,channel,p[i],q);
cristy6d188022011-09-12 13:23:33 +00003309 }
3310 else
cristy3ed852e2009-09-05 21:47:34 +00003311 {
3312 double
3313 factor;
3314
3315 /*
3316 Implode the pixel.
3317 */
3318 factor=1.0;
3319 if (distance > 0.0)
cristy1707c6c2012-01-18 23:30:54 +00003320 factor=pow(sin((double) (MagickPI*sqrt((double) distance)/radius/
3321 2)),-amount);
cristy76f512e2011-09-12 01:26:56 +00003322 status=InterpolatePixelChannels(image,image_view,implode_image,method,
3323 (double) (factor*delta.x/scale.x+center.x),(double) (factor*delta.y/
3324 scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00003325 }
cristy6d188022011-09-12 13:23:33 +00003326 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003327 q+=GetPixelChannels(implode_image);
cristy3ed852e2009-09-05 21:47:34 +00003328 }
3329 if (SyncCacheViewAuthenticPixels(implode_view,exception) == MagickFalse)
3330 status=MagickFalse;
3331 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3332 {
3333 MagickBooleanType
3334 proceed;
3335
cristyb5d5f722009-11-04 03:03:49 +00003336#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003337 #pragma omp critical (MagickCore_ImplodeImage)
cristy3ed852e2009-09-05 21:47:34 +00003338#endif
3339 proceed=SetImageProgress(image,ImplodeImageTag,progress++,image->rows);
3340 if (proceed == MagickFalse)
3341 status=MagickFalse;
3342 }
3343 }
3344 implode_view=DestroyCacheView(implode_view);
3345 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003346 if (status == MagickFalse)
3347 implode_image=DestroyImage(implode_image);
3348 return(implode_image);
3349}
3350
3351/*
3352%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3353% %
3354% %
3355% %
3356% M o r p h I m a g e s %
3357% %
3358% %
3359% %
3360%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3361%
3362% The MorphImages() method requires a minimum of two images. The first
3363% image is transformed into the second by a number of intervening images
3364% as specified by frames.
3365%
3366% The format of the MorphImage method is:
3367%
cristybb503372010-05-27 20:51:26 +00003368% Image *MorphImages(const Image *image,const size_t number_frames,
cristy3ed852e2009-09-05 21:47:34 +00003369% ExceptionInfo *exception)
3370%
3371% A description of each parameter follows:
3372%
3373% o image: the image.
3374%
3375% o number_frames: Define the number of in-between image to generate.
3376% The more in-between frames, the smoother the morph.
3377%
3378% o exception: return any errors or warnings in this structure.
3379%
3380*/
3381MagickExport Image *MorphImages(const Image *image,
cristybb503372010-05-27 20:51:26 +00003382 const size_t number_frames,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003383{
3384#define MorphImageTag "Morph/Image"
3385
3386 Image
3387 *morph_image,
3388 *morph_images;
3389
cristy9d314ff2011-03-09 01:30:28 +00003390 MagickBooleanType
3391 status;
cristy3ed852e2009-09-05 21:47:34 +00003392
3393 MagickOffsetType
3394 scene;
3395
3396 MagickRealType
3397 alpha,
3398 beta;
3399
3400 register const Image
3401 *next;
3402
cristybb503372010-05-27 20:51:26 +00003403 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003404 i;
3405
cristy9d314ff2011-03-09 01:30:28 +00003406 ssize_t
3407 y;
cristy3ed852e2009-09-05 21:47:34 +00003408
3409 /*
3410 Clone first frame in sequence.
3411 */
3412 assert(image != (Image *) NULL);
3413 assert(image->signature == MagickSignature);
3414 if (image->debug != MagickFalse)
3415 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3416 assert(exception != (ExceptionInfo *) NULL);
3417 assert(exception->signature == MagickSignature);
3418 morph_images=CloneImage(image,0,0,MagickTrue,exception);
3419 if (morph_images == (Image *) NULL)
3420 return((Image *) NULL);
3421 if (GetNextImageInList(image) == (Image *) NULL)
3422 {
3423 /*
3424 Morph single image.
3425 */
cristybb503372010-05-27 20:51:26 +00003426 for (i=1; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003427 {
3428 morph_image=CloneImage(image,0,0,MagickTrue,exception);
3429 if (morph_image == (Image *) NULL)
3430 {
3431 morph_images=DestroyImageList(morph_images);
3432 return((Image *) NULL);
3433 }
3434 AppendImageToList(&morph_images,morph_image);
cristy8b27a6d2010-02-14 03:31:15 +00003435 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003436 {
cristy8b27a6d2010-02-14 03:31:15 +00003437 MagickBooleanType
3438 proceed;
3439
cristybb503372010-05-27 20:51:26 +00003440 proceed=SetImageProgress(image,MorphImageTag,(MagickOffsetType) i,
3441 number_frames);
cristy8b27a6d2010-02-14 03:31:15 +00003442 if (proceed == MagickFalse)
3443 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00003444 }
3445 }
3446 return(GetFirstImageInList(morph_images));
3447 }
3448 /*
3449 Morph image sequence.
3450 */
3451 status=MagickTrue;
3452 scene=0;
3453 next=image;
3454 for ( ; GetNextImageInList(next) != (Image *) NULL; next=GetNextImageInList(next))
3455 {
cristybb503372010-05-27 20:51:26 +00003456 for (i=0; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003457 {
3458 CacheView
3459 *image_view,
3460 *morph_view;
3461
3462 beta=(MagickRealType) (i+1.0)/(MagickRealType) (number_frames+1.0);
3463 alpha=1.0-beta;
cristy15b98cd2010-09-12 19:42:50 +00003464 morph_image=ResizeImage(next,(size_t) (alpha*next->columns+beta*
cristyaa2c16c2012-03-25 22:21:35 +00003465 GetNextImageInList(next)->columns+0.5),(size_t) (alpha*next->rows+beta*
3466 GetNextImageInList(next)->rows+0.5),next->filter,exception);
cristy3ed852e2009-09-05 21:47:34 +00003467 if (morph_image == (Image *) NULL)
3468 {
3469 morph_images=DestroyImageList(morph_images);
3470 return((Image *) NULL);
3471 }
cristy1707c6c2012-01-18 23:30:54 +00003472 status=SetImageStorageClass(morph_image,DirectClass,exception);
3473 if (status == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003474 {
cristy3ed852e2009-09-05 21:47:34 +00003475 morph_image=DestroyImage(morph_image);
3476 return((Image *) NULL);
3477 }
3478 AppendImageToList(&morph_images,morph_image);
3479 morph_images=GetLastImageInList(morph_images);
cristy15b98cd2010-09-12 19:42:50 +00003480 morph_image=ResizeImage(GetNextImageInList(next),morph_images->columns,
cristyaa2c16c2012-03-25 22:21:35 +00003481 morph_images->rows,GetNextImageInList(next)->filter,exception);
cristy3ed852e2009-09-05 21:47:34 +00003482 if (morph_image == (Image *) NULL)
3483 {
3484 morph_images=DestroyImageList(morph_images);
3485 return((Image *) NULL);
3486 }
cristydb070952012-04-20 14:33:00 +00003487 image_view=AcquireVirtualCacheView(morph_image,exception);
3488 morph_view=AcquireAuthenticCacheView(morph_images,exception);
cristyb5d5f722009-11-04 03:03:49 +00003489#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003490 #pragma omp parallel for schedule(static,4) shared(status) \
cristyddacdd12012-05-07 23:08:14 +00003491 dynamic_number_threads(image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003492#endif
cristybb503372010-05-27 20:51:26 +00003493 for (y=0; y < (ssize_t) morph_images->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003494 {
3495 MagickBooleanType
3496 sync;
3497
cristy4c08aed2011-07-01 19:47:50 +00003498 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003499 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003500
cristybb503372010-05-27 20:51:26 +00003501 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003502 x;
3503
cristy4c08aed2011-07-01 19:47:50 +00003504 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003505 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003506
3507 if (status == MagickFalse)
3508 continue;
3509 p=GetCacheViewVirtualPixels(image_view,0,y,morph_image->columns,1,
3510 exception);
3511 q=GetCacheViewAuthenticPixels(morph_view,0,y,morph_images->columns,1,
3512 exception);
cristy4c08aed2011-07-01 19:47:50 +00003513 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003514 {
3515 status=MagickFalse;
3516 continue;
3517 }
cristybb503372010-05-27 20:51:26 +00003518 for (x=0; x < (ssize_t) morph_images->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003519 {
cristy1707c6c2012-01-18 23:30:54 +00003520 register ssize_t
3521 i;
3522
cristy177e41c2012-04-15 15:08:25 +00003523 if (GetPixelMask(image,p) != 0)
3524 {
3525 p+=GetPixelChannels(image);
3526 q+=GetPixelChannels(morph_image);
3527 continue;
3528 }
cristy10a6c612012-01-29 21:41:05 +00003529 for (i=0; i < (ssize_t) GetPixelChannels(morph_image); i++)
cristy1707c6c2012-01-18 23:30:54 +00003530 {
3531 PixelChannel
3532 channel;
3533
3534 PixelTrait
3535 morph_traits,
3536 traits;
3537
3538 channel=GetPixelChannelMapChannel(image,i);
3539 traits=GetPixelChannelMapTraits(image,channel);
3540 morph_traits=GetPixelChannelMapTraits(morph_image,channel);
3541 if ((traits == UndefinedPixelTrait) ||
3542 (morph_traits == UndefinedPixelTrait))
3543 continue;
cristy177e41c2012-04-15 15:08:25 +00003544 if ((morph_traits & CopyPixelTrait) != 0)
cristy1707c6c2012-01-18 23:30:54 +00003545 {
3546 SetPixelChannel(morph_image,channel,p[i],q);
3547 continue;
3548 }
3549 SetPixelChannel(morph_image,channel,ClampToQuantum(alpha*
3550 GetPixelChannel(morph_images,channel,q)+beta*p[i]),q);
3551 }
cristyed231572011-07-14 02:18:59 +00003552 p+=GetPixelChannels(morph_image);
3553 q+=GetPixelChannels(morph_images);
cristy3ed852e2009-09-05 21:47:34 +00003554 }
3555 sync=SyncCacheViewAuthenticPixels(morph_view,exception);
3556 if (sync == MagickFalse)
3557 status=MagickFalse;
3558 }
3559 morph_view=DestroyCacheView(morph_view);
3560 image_view=DestroyCacheView(image_view);
3561 morph_image=DestroyImage(morph_image);
3562 }
cristybb503372010-05-27 20:51:26 +00003563 if (i < (ssize_t) number_frames)
cristy3ed852e2009-09-05 21:47:34 +00003564 break;
3565 /*
3566 Clone last frame in sequence.
3567 */
3568 morph_image=CloneImage(GetNextImageInList(next),0,0,MagickTrue,exception);
3569 if (morph_image == (Image *) NULL)
3570 {
3571 morph_images=DestroyImageList(morph_images);
3572 return((Image *) NULL);
3573 }
3574 AppendImageToList(&morph_images,morph_image);
3575 morph_images=GetLastImageInList(morph_images);
3576 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3577 {
3578 MagickBooleanType
3579 proceed;
3580
cristyb5d5f722009-11-04 03:03:49 +00003581#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003582 #pragma omp critical (MagickCore_MorphImages)
cristy3ed852e2009-09-05 21:47:34 +00003583#endif
3584 proceed=SetImageProgress(image,MorphImageTag,scene,
3585 GetImageListLength(image));
3586 if (proceed == MagickFalse)
3587 status=MagickFalse;
3588 }
3589 scene++;
3590 }
3591 if (GetNextImageInList(next) != (Image *) NULL)
3592 {
3593 morph_images=DestroyImageList(morph_images);
3594 return((Image *) NULL);
3595 }
3596 return(GetFirstImageInList(morph_images));
3597}
3598
3599/*
3600%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3601% %
3602% %
3603% %
3604% P l a s m a I m a g e %
3605% %
3606% %
3607% %
3608%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3609%
3610% PlasmaImage() initializes an image with plasma fractal values. The image
3611% must be initialized with a base color and the random number generator
3612% seeded before this method is called.
3613%
3614% The format of the PlasmaImage method is:
3615%
3616% MagickBooleanType PlasmaImage(Image *image,const SegmentInfo *segment,
cristy5cbc0162011-08-29 00:36:28 +00003617% size_t attenuate,size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003618%
3619% A description of each parameter follows:
3620%
3621% o image: the image.
3622%
3623% o segment: Define the region to apply plasma fractals values.
3624%
glennrp7dae1ca2010-09-16 12:17:35 +00003625% o attenuate: Define the plasma attenuation factor.
cristy3ed852e2009-09-05 21:47:34 +00003626%
3627% o depth: Limit the plasma recursion depth.
3628%
cristy5cbc0162011-08-29 00:36:28 +00003629% o exception: return any errors or warnings in this structure.
3630%
cristy3ed852e2009-09-05 21:47:34 +00003631*/
3632
3633static inline Quantum PlasmaPixel(RandomInfo *random_info,
3634 const MagickRealType pixel,const MagickRealType noise)
3635{
3636 Quantum
3637 plasma;
3638
cristyce70c172010-01-07 17:15:30 +00003639 plasma=ClampToQuantum(pixel+noise*GetPseudoRandomValue(random_info)-
cristy3ed852e2009-09-05 21:47:34 +00003640 noise/2.0);
3641 return(plasma);
3642}
3643
cristyda1f9c12011-10-02 21:39:49 +00003644static MagickBooleanType PlasmaImageProxy(Image *image,CacheView *image_view,
3645 CacheView *u_view,CacheView *v_view,RandomInfo *random_info,
3646 const SegmentInfo *segment,size_t attenuate,size_t depth,
3647 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003648{
cristy3ed852e2009-09-05 21:47:34 +00003649 MagickRealType
3650 plasma;
3651
cristyda1f9c12011-10-02 21:39:49 +00003652 PixelChannel
3653 channel;
3654
3655 PixelTrait
3656 traits;
3657
3658 register const Quantum
3659 *restrict u,
3660 *restrict v;
3661
3662 register Quantum
3663 *restrict q;
3664
3665 register ssize_t
3666 i;
cristy3ed852e2009-09-05 21:47:34 +00003667
cristy9d314ff2011-03-09 01:30:28 +00003668 ssize_t
3669 x,
3670 x_mid,
3671 y,
3672 y_mid;
3673
cristy3ed852e2009-09-05 21:47:34 +00003674 if (((segment->x2-segment->x1) == 0.0) && ((segment->y2-segment->y1) == 0.0))
3675 return(MagickTrue);
3676 if (depth != 0)
3677 {
3678 SegmentInfo
3679 local_info;
3680
3681 /*
3682 Divide the area into quadrants and recurse.
3683 */
3684 depth--;
3685 attenuate++;
cristybb503372010-05-27 20:51:26 +00003686 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3687 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003688 local_info=(*segment);
3689 local_info.x2=(double) x_mid;
3690 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003691 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3692 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003693 local_info=(*segment);
3694 local_info.y1=(double) y_mid;
3695 local_info.x2=(double) x_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.x1=(double) x_mid;
3700 local_info.y2=(double) y_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.y1=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003706 return(PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3707 &local_info,attenuate,depth,exception));
cristy3ed852e2009-09-05 21:47:34 +00003708 }
cristybb503372010-05-27 20:51:26 +00003709 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3710 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003711 if ((segment->x1 == (double) x_mid) && (segment->x2 == (double) x_mid) &&
3712 (segment->y1 == (double) y_mid) && (segment->y2 == (double) y_mid))
3713 return(MagickFalse);
3714 /*
3715 Average pixels and apply plasma.
3716 */
cristy3ed852e2009-09-05 21:47:34 +00003717 plasma=(MagickRealType) QuantumRange/(2.0*attenuate);
3718 if ((segment->x1 != (double) x_mid) || (segment->x2 != (double) x_mid))
3719 {
cristy3ed852e2009-09-05 21:47:34 +00003720 /*
3721 Left pixel.
3722 */
cristybb503372010-05-27 20:51:26 +00003723 x=(ssize_t) ceil(segment->x1-0.5);
cristy1707c6c2012-01-18 23:30:54 +00003724 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),1,1,
3725 exception);
3726 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),1,1,
3727 exception);
cristyc5c6f662010-09-22 14:23:02 +00003728 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003729 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3730 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003731 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003732 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3733 {
cristye2a912b2011-12-05 20:02:07 +00003734 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003735 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003736 if (traits == UndefinedPixelTrait)
3737 continue;
3738 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3739 }
cristyc5c6f662010-09-22 14:23:02 +00003740 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003741 if (segment->x1 != segment->x2)
3742 {
3743 /*
3744 Right pixel.
3745 */
cristybb503372010-05-27 20:51:26 +00003746 x=(ssize_t) ceil(segment->x2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003747 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3748 1,1,exception);
3749 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3750 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003751 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003752 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3753 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003754 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003755 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3756 {
cristye2a912b2011-12-05 20:02:07 +00003757 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003758 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003759 if (traits == UndefinedPixelTrait)
3760 continue;
3761 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3762 }
cristyc5c6f662010-09-22 14:23:02 +00003763 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003764 }
3765 }
3766 if ((segment->y1 != (double) y_mid) || (segment->y2 != (double) y_mid))
3767 {
3768 if ((segment->x1 != (double) x_mid) || (segment->y2 != (double) y_mid))
3769 {
cristy3ed852e2009-09-05 21:47:34 +00003770 /*
3771 Bottom pixel.
3772 */
cristybb503372010-05-27 20:51:26 +00003773 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003774 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3775 1,1,exception);
3776 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3777 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003778 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003779 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3780 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003781 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003782 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3783 {
cristye2a912b2011-12-05 20:02:07 +00003784 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003785 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003786 if (traits == UndefinedPixelTrait)
3787 continue;
3788 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3789 }
cristyc5c6f662010-09-22 14:23:02 +00003790 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003791 }
3792 if (segment->y1 != segment->y2)
3793 {
cristy3ed852e2009-09-05 21:47:34 +00003794 /*
3795 Top pixel.
3796 */
cristybb503372010-05-27 20:51:26 +00003797 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003798 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3799 1,1,exception);
3800 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3801 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003802 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003803 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3804 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003805 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003806 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3807 {
cristye2a912b2011-12-05 20:02:07 +00003808 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003809 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003810 if (traits == UndefinedPixelTrait)
3811 continue;
3812 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3813 }
cristyc5c6f662010-09-22 14:23:02 +00003814 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003815 }
3816 }
3817 if ((segment->x1 != segment->x2) || (segment->y1 != segment->y2))
3818 {
cristy3ed852e2009-09-05 21:47:34 +00003819 /*
3820 Middle pixel.
3821 */
cristybb503372010-05-27 20:51:26 +00003822 x=(ssize_t) ceil(segment->x1-0.5);
3823 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003824 u=GetCacheViewVirtualPixels(u_view,x,y,1,1,exception);
cristybb503372010-05-27 20:51:26 +00003825 x=(ssize_t) ceil(segment->x2-0.5);
3826 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003827 v=GetCacheViewVirtualPixels(v_view,x,y,1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003828 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003829 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3830 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003831 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003832 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3833 {
cristye2a912b2011-12-05 20:02:07 +00003834 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003835 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003836 if (traits == UndefinedPixelTrait)
3837 continue;
3838 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3839 }
cristyc5c6f662010-09-22 14:23:02 +00003840 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003841 }
3842 if (((segment->x2-segment->x1) < 3.0) && ((segment->y2-segment->y1) < 3.0))
3843 return(MagickTrue);
3844 return(MagickFalse);
3845}
cristyda1f9c12011-10-02 21:39:49 +00003846
cristy3ed852e2009-09-05 21:47:34 +00003847MagickExport MagickBooleanType PlasmaImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00003848 const SegmentInfo *segment,size_t attenuate,size_t depth,
3849 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003850{
cristyc5c6f662010-09-22 14:23:02 +00003851 CacheView
cristyda1f9c12011-10-02 21:39:49 +00003852 *image_view,
3853 *u_view,
3854 *v_view;
cristyc5c6f662010-09-22 14:23:02 +00003855
cristy3ed852e2009-09-05 21:47:34 +00003856 MagickBooleanType
3857 status;
3858
3859 RandomInfo
3860 *random_info;
3861
3862 if (image->debug != MagickFalse)
3863 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3864 assert(image != (Image *) NULL);
3865 assert(image->signature == MagickSignature);
3866 if (image->debug != MagickFalse)
3867 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
cristy5cbc0162011-08-29 00:36:28 +00003868 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristyc5c6f662010-09-22 14:23:02 +00003869 return(MagickFalse);
cristydb070952012-04-20 14:33:00 +00003870 image_view=AcquireAuthenticCacheView(image,exception);
3871 u_view=AcquireVirtualCacheView(image,exception);
3872 v_view=AcquireVirtualCacheView(image,exception);
cristy3ed852e2009-09-05 21:47:34 +00003873 random_info=AcquireRandomInfo();
cristyda1f9c12011-10-02 21:39:49 +00003874 status=PlasmaImageProxy(image,image_view,u_view,v_view,random_info,segment,
3875 attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003876 random_info=DestroyRandomInfo(random_info);
cristyda1f9c12011-10-02 21:39:49 +00003877 v_view=DestroyCacheView(v_view);
3878 u_view=DestroyCacheView(u_view);
cristyc5c6f662010-09-22 14:23:02 +00003879 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003880 return(status);
3881}
3882
3883/*
3884%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3885% %
3886% %
3887% %
3888% P o l a r o i d I m a g e %
3889% %
3890% %
3891% %
3892%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3893%
3894% PolaroidImage() simulates a Polaroid picture.
3895%
3896% The format of the AnnotateImage method is:
3897%
3898% Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristye9e3d382011-12-14 01:50:13 +00003899% const char *caption,const double angle,
3900% const PixelInterpolateMethod method,ExceptionInfo exception)
cristy3ed852e2009-09-05 21:47:34 +00003901%
3902% A description of each parameter follows:
3903%
3904% o image: the image.
3905%
3906% o draw_info: the draw info.
3907%
cristye9e3d382011-12-14 01:50:13 +00003908% o caption: the Polaroid caption.
3909%
cristycee97112010-05-28 00:44:52 +00003910% o angle: Apply the effect along this angle.
cristy3ed852e2009-09-05 21:47:34 +00003911%
cristy5c4e2582011-09-11 19:21:03 +00003912% o method: the pixel interpolation method.
3913%
cristy3ed852e2009-09-05 21:47:34 +00003914% o exception: return any errors or warnings in this structure.
3915%
3916*/
3917MagickExport Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristye9e3d382011-12-14 01:50:13 +00003918 const char *caption,const double angle,const PixelInterpolateMethod method,
cristy5c4e2582011-09-11 19:21:03 +00003919 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003920{
cristy3ed852e2009-09-05 21:47:34 +00003921 Image
3922 *bend_image,
3923 *caption_image,
3924 *flop_image,
3925 *picture_image,
3926 *polaroid_image,
3927 *rotate_image,
3928 *trim_image;
3929
cristybb503372010-05-27 20:51:26 +00003930 size_t
cristy3ed852e2009-09-05 21:47:34 +00003931 height;
3932
cristy9d314ff2011-03-09 01:30:28 +00003933 ssize_t
3934 quantum;
3935
cristy3ed852e2009-09-05 21:47:34 +00003936 /*
3937 Simulate a Polaroid picture.
3938 */
3939 assert(image != (Image *) NULL);
3940 assert(image->signature == MagickSignature);
3941 if (image->debug != MagickFalse)
3942 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3943 assert(exception != (ExceptionInfo *) NULL);
3944 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00003945 quantum=(ssize_t) MagickMax(MagickMax((double) image->columns,(double)
cristy3ed852e2009-09-05 21:47:34 +00003946 image->rows)/25.0,10.0);
3947 height=image->rows+2*quantum;
3948 caption_image=(Image *) NULL;
cristye9e3d382011-12-14 01:50:13 +00003949 if (caption != (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003950 {
3951 char
cristye9e3d382011-12-14 01:50:13 +00003952 geometry[MaxTextExtent],
3953 *text;
cristy3ed852e2009-09-05 21:47:34 +00003954
3955 DrawInfo
3956 *annotate_info;
3957
cristy3ed852e2009-09-05 21:47:34 +00003958 MagickBooleanType
3959 status;
3960
cristy9d314ff2011-03-09 01:30:28 +00003961 ssize_t
3962 count;
3963
cristy3ed852e2009-09-05 21:47:34 +00003964 TypeMetric
3965 metrics;
3966
3967 /*
3968 Generate caption image.
3969 */
3970 caption_image=CloneImage(image,image->columns,1,MagickTrue,exception);
3971 if (caption_image == (Image *) NULL)
3972 return((Image *) NULL);
3973 annotate_info=CloneDrawInfo((const ImageInfo *) NULL,draw_info);
cristye9e3d382011-12-14 01:50:13 +00003974 text=InterpretImageProperties((ImageInfo *) NULL,(Image *) image,caption,
3975 exception);
3976 (void) CloneString(&annotate_info->text,text);
cristy6b1d05e2010-09-22 19:17:27 +00003977 count=FormatMagickCaption(caption_image,annotate_info,MagickTrue,&metrics,
cristye9e3d382011-12-14 01:50:13 +00003978 &text,exception);
3979 status=SetImageExtent(caption_image,image->columns,(size_t) ((count+1)*
3980 (metrics.ascent-metrics.descent)+0.5),exception);
cristy3ed852e2009-09-05 21:47:34 +00003981 if (status == MagickFalse)
3982 caption_image=DestroyImage(caption_image);
3983 else
3984 {
3985 caption_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00003986 (void) SetImageBackgroundColor(caption_image,exception);
cristye9e3d382011-12-14 01:50:13 +00003987 (void) CloneString(&annotate_info->text,text);
cristyb51dff52011-05-19 16:55:47 +00003988 (void) FormatLocaleString(geometry,MaxTextExtent,"+0+%g",
cristy3ed852e2009-09-05 21:47:34 +00003989 metrics.ascent);
3990 if (annotate_info->gravity == UndefinedGravity)
3991 (void) CloneString(&annotate_info->geometry,AcquireString(
3992 geometry));
cristy5cbc0162011-08-29 00:36:28 +00003993 (void) AnnotateImage(caption_image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00003994 height+=caption_image->rows;
3995 }
3996 annotate_info=DestroyDrawInfo(annotate_info);
cristye9e3d382011-12-14 01:50:13 +00003997 text=DestroyString(text);
cristy3ed852e2009-09-05 21:47:34 +00003998 }
3999 picture_image=CloneImage(image,image->columns+2*quantum,height,MagickTrue,
4000 exception);
4001 if (picture_image == (Image *) NULL)
4002 {
4003 if (caption_image != (Image *) NULL)
4004 caption_image=DestroyImage(caption_image);
4005 return((Image *) NULL);
4006 }
4007 picture_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00004008 (void) SetImageBackgroundColor(picture_image,exception);
cristy39172402012-03-30 13:04:39 +00004009 (void) CompositeImage(picture_image,image,OverCompositeOp,MagickTrue,quantum,
cristyfeb3e962012-03-29 17:25:55 +00004010 quantum,exception);
cristy3ed852e2009-09-05 21:47:34 +00004011 if (caption_image != (Image *) NULL)
4012 {
cristyfeb3e962012-03-29 17:25:55 +00004013 (void) CompositeImage(picture_image,caption_image,OverCompositeOp,
cristy39172402012-03-30 13:04:39 +00004014 MagickTrue,quantum,(ssize_t) (image->rows+3*quantum/2),exception);
cristy3ed852e2009-09-05 21:47:34 +00004015 caption_image=DestroyImage(caption_image);
4016 }
cristy9950d572011-10-01 18:22:35 +00004017 (void) QueryColorCompliance("none",AllCompliance,
4018 &picture_image->background_color,exception);
cristy63240882011-08-05 19:05:27 +00004019 (void) SetImageAlphaChannel(picture_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004020 rotate_image=RotateImage(picture_image,90.0,exception);
4021 picture_image=DestroyImage(picture_image);
4022 if (rotate_image == (Image *) NULL)
4023 return((Image *) NULL);
4024 picture_image=rotate_image;
4025 bend_image=WaveImage(picture_image,0.01*picture_image->rows,2.0*
cristy5c4e2582011-09-11 19:21:03 +00004026 picture_image->columns,method,exception);
cristy3ed852e2009-09-05 21:47:34 +00004027 picture_image=DestroyImage(picture_image);
4028 if (bend_image == (Image *) NULL)
4029 return((Image *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00004030 picture_image=bend_image;
4031 rotate_image=RotateImage(picture_image,-90.0,exception);
4032 picture_image=DestroyImage(picture_image);
4033 if (rotate_image == (Image *) NULL)
4034 return((Image *) NULL);
4035 picture_image=rotate_image;
4036 picture_image->background_color=image->background_color;
anthonyf46d4262012-03-26 03:30:34 +00004037 polaroid_image=ShadowImage(picture_image,80.0,2.0,quantum/3,quantum/3,
cristy3ed852e2009-09-05 21:47:34 +00004038 exception);
4039 if (polaroid_image == (Image *) NULL)
4040 {
4041 picture_image=DestroyImage(picture_image);
4042 return(picture_image);
4043 }
4044 flop_image=FlopImage(polaroid_image,exception);
4045 polaroid_image=DestroyImage(polaroid_image);
4046 if (flop_image == (Image *) NULL)
4047 {
4048 picture_image=DestroyImage(picture_image);
4049 return(picture_image);
4050 }
4051 polaroid_image=flop_image;
cristyfeb3e962012-03-29 17:25:55 +00004052 (void) CompositeImage(polaroid_image,picture_image,OverCompositeOp,
cristy39172402012-03-30 13:04:39 +00004053 MagickTrue,(ssize_t) (-0.01*picture_image->columns/2.0),0L,exception);
cristy3ed852e2009-09-05 21:47:34 +00004054 picture_image=DestroyImage(picture_image);
cristy9950d572011-10-01 18:22:35 +00004055 (void) QueryColorCompliance("none",AllCompliance,
4056 &polaroid_image->background_color,exception);
cristy3ed852e2009-09-05 21:47:34 +00004057 rotate_image=RotateImage(polaroid_image,angle,exception);
4058 polaroid_image=DestroyImage(polaroid_image);
4059 if (rotate_image == (Image *) NULL)
4060 return((Image *) NULL);
4061 polaroid_image=rotate_image;
4062 trim_image=TrimImage(polaroid_image,exception);
4063 polaroid_image=DestroyImage(polaroid_image);
4064 if (trim_image == (Image *) NULL)
4065 return((Image *) NULL);
4066 polaroid_image=trim_image;
4067 return(polaroid_image);
4068}
4069
4070/*
4071%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4072% %
4073% %
4074% %
cristy3ed852e2009-09-05 21:47:34 +00004075% S e p i a T o n e I m a g e %
4076% %
4077% %
4078% %
4079%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4080%
4081% MagickSepiaToneImage() applies a special effect to the image, similar to the
4082% effect achieved in a photo darkroom by sepia toning. Threshold ranges from
4083% 0 to QuantumRange and is a measure of the extent of the sepia toning. A
4084% threshold of 80% is a good starting point for a reasonable tone.
4085%
4086% The format of the SepiaToneImage method is:
4087%
4088% Image *SepiaToneImage(const Image *image,const double threshold,
4089% ExceptionInfo *exception)
4090%
4091% A description of each parameter follows:
4092%
4093% o image: the image.
4094%
4095% o threshold: the tone threshold.
4096%
4097% o exception: return any errors or warnings in this structure.
4098%
4099*/
4100MagickExport Image *SepiaToneImage(const Image *image,const double threshold,
4101 ExceptionInfo *exception)
4102{
4103#define SepiaToneImageTag "SepiaTone/Image"
4104
cristyc4c8d132010-01-07 01:58:38 +00004105 CacheView
4106 *image_view,
4107 *sepia_view;
4108
cristy3ed852e2009-09-05 21:47:34 +00004109 Image
4110 *sepia_image;
4111
cristy3ed852e2009-09-05 21:47:34 +00004112 MagickBooleanType
4113 status;
4114
cristybb503372010-05-27 20:51:26 +00004115 MagickOffsetType
4116 progress;
4117
4118 ssize_t
4119 y;
4120
cristy3ed852e2009-09-05 21:47:34 +00004121 /*
4122 Initialize sepia-toned image attributes.
4123 */
4124 assert(image != (const Image *) NULL);
4125 assert(image->signature == MagickSignature);
4126 if (image->debug != MagickFalse)
4127 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4128 assert(exception != (ExceptionInfo *) NULL);
4129 assert(exception->signature == MagickSignature);
cristy1707c6c2012-01-18 23:30:54 +00004130 sepia_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004131 if (sepia_image == (Image *) NULL)
4132 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004133 if (SetImageStorageClass(sepia_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004134 {
cristy3ed852e2009-09-05 21:47:34 +00004135 sepia_image=DestroyImage(sepia_image);
4136 return((Image *) NULL);
4137 }
4138 /*
4139 Tone each row of the image.
4140 */
4141 status=MagickTrue;
4142 progress=0;
cristydb070952012-04-20 14:33:00 +00004143 image_view=AcquireVirtualCacheView(image,exception);
4144 sepia_view=AcquireAuthenticCacheView(sepia_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00004145#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00004146 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristyddacdd12012-05-07 23:08:14 +00004147 dynamic_number_threads(image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00004148#endif
cristybb503372010-05-27 20:51:26 +00004149 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004150 {
cristy4c08aed2011-07-01 19:47:50 +00004151 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004152 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00004153
cristybb503372010-05-27 20:51:26 +00004154 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004155 x;
4156
cristy4c08aed2011-07-01 19:47:50 +00004157 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004158 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004159
4160 if (status == MagickFalse)
4161 continue;
4162 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy1707c6c2012-01-18 23:30:54 +00004163 q=GetCacheViewAuthenticPixels(sepia_view,0,y,sepia_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00004164 exception);
cristy4c08aed2011-07-01 19:47:50 +00004165 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004166 {
4167 status=MagickFalse;
4168 continue;
4169 }
cristybb503372010-05-27 20:51:26 +00004170 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004171 {
4172 MagickRealType
4173 intensity,
4174 tone;
4175
cristy4c08aed2011-07-01 19:47:50 +00004176 intensity=(MagickRealType) GetPixelIntensity(image,p);
cristy3ed852e2009-09-05 21:47:34 +00004177 tone=intensity > threshold ? (MagickRealType) QuantumRange : intensity+
4178 (MagickRealType) QuantumRange-threshold;
cristy4c08aed2011-07-01 19:47:50 +00004179 SetPixelRed(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004180 tone=intensity > (7.0*threshold/6.0) ? (MagickRealType) QuantumRange :
4181 intensity+(MagickRealType) QuantumRange-7.0*threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004182 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004183 tone=intensity < (threshold/6.0) ? 0 : intensity-threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004184 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004185 tone=threshold/7.0;
cristy4c08aed2011-07-01 19:47:50 +00004186 if ((MagickRealType) GetPixelGreen(image,q) < tone)
4187 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
4188 if ((MagickRealType) GetPixelBlue(image,q) < tone)
4189 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristyed231572011-07-14 02:18:59 +00004190 p+=GetPixelChannels(image);
4191 q+=GetPixelChannels(sepia_image);
cristy3ed852e2009-09-05 21:47:34 +00004192 }
4193 if (SyncCacheViewAuthenticPixels(sepia_view,exception) == MagickFalse)
4194 status=MagickFalse;
4195 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4196 {
4197 MagickBooleanType
4198 proceed;
4199
cristyb5d5f722009-11-04 03:03:49 +00004200#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00004201 #pragma omp critical (MagickCore_SepiaToneImage)
cristy3ed852e2009-09-05 21:47:34 +00004202#endif
4203 proceed=SetImageProgress(image,SepiaToneImageTag,progress++,
4204 image->rows);
4205 if (proceed == MagickFalse)
4206 status=MagickFalse;
4207 }
4208 }
4209 sepia_view=DestroyCacheView(sepia_view);
4210 image_view=DestroyCacheView(image_view);
cristye23ec9d2011-08-16 18:15:40 +00004211 (void) NormalizeImage(sepia_image,exception);
4212 (void) ContrastImage(sepia_image,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004213 if (status == MagickFalse)
4214 sepia_image=DestroyImage(sepia_image);
4215 return(sepia_image);
4216}
4217
4218/*
4219%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4220% %
4221% %
4222% %
4223% S h a d o w I m a g e %
4224% %
4225% %
4226% %
4227%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4228%
4229% ShadowImage() simulates a shadow from the specified image and returns it.
4230%
4231% The format of the ShadowImage method is:
4232%
cristy70cddf72011-12-10 22:42:42 +00004233% Image *ShadowImage(const Image *image,const double alpha,
cristyaa2c16c2012-03-25 22:21:35 +00004234% const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4235% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004236%
4237% A description of each parameter follows:
4238%
4239% o image: the image.
4240%
cristy70cddf72011-12-10 22:42:42 +00004241% o alpha: percentage transparency.
cristy3ed852e2009-09-05 21:47:34 +00004242%
4243% o sigma: the standard deviation of the Gaussian, in pixels.
4244%
4245% o x_offset: the shadow x-offset.
4246%
4247% o y_offset: the shadow y-offset.
4248%
4249% o exception: return any errors or warnings in this structure.
4250%
4251*/
cristy70cddf72011-12-10 22:42:42 +00004252MagickExport Image *ShadowImage(const Image *image,const double alpha,
cristyaa2c16c2012-03-25 22:21:35 +00004253 const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4254 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004255{
4256#define ShadowImageTag "Shadow/Image"
4257
cristy70cddf72011-12-10 22:42:42 +00004258 CacheView
4259 *image_view;
4260
cristybd5a96c2011-08-21 00:04:26 +00004261 ChannelType
4262 channel_mask;
4263
cristy3ed852e2009-09-05 21:47:34 +00004264 Image
4265 *border_image,
4266 *clone_image,
4267 *shadow_image;
4268
cristy70cddf72011-12-10 22:42:42 +00004269 MagickBooleanType
4270 status;
4271
cristy3ed852e2009-09-05 21:47:34 +00004272 RectangleInfo
4273 border_info;
4274
cristy70cddf72011-12-10 22:42:42 +00004275 ssize_t
4276 y;
4277
cristy3ed852e2009-09-05 21:47:34 +00004278 assert(image != (Image *) NULL);
4279 assert(image->signature == MagickSignature);
4280 if (image->debug != MagickFalse)
4281 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4282 assert(exception != (ExceptionInfo *) NULL);
4283 assert(exception->signature == MagickSignature);
4284 clone_image=CloneImage(image,0,0,MagickTrue,exception);
4285 if (clone_image == (Image *) NULL)
4286 return((Image *) NULL);
cristy0898eba2012-04-09 16:38:29 +00004287 if (IsGrayColorspace(image->colorspace) != MagickFalse)
4288 (void) TransformImageColorspace(clone_image,sRGBColorspace,exception);
cristy387430f2012-02-07 13:09:46 +00004289 (void) SetImageVirtualPixelMethod(clone_image,EdgeVirtualPixelMethod,
4290 exception);
cristybb503372010-05-27 20:51:26 +00004291 border_info.width=(size_t) floor(2.0*sigma+0.5);
4292 border_info.height=(size_t) floor(2.0*sigma+0.5);
cristy3ed852e2009-09-05 21:47:34 +00004293 border_info.x=0;
4294 border_info.y=0;
cristy9950d572011-10-01 18:22:35 +00004295 (void) QueryColorCompliance("none",AllCompliance,&clone_image->border_color,
4296 exception);
cristy70cddf72011-12-10 22:42:42 +00004297 clone_image->matte=MagickTrue;
4298 border_image=BorderImage(clone_image,&border_info,OverCompositeOp,exception);
cristy3ed852e2009-09-05 21:47:34 +00004299 clone_image=DestroyImage(clone_image);
4300 if (border_image == (Image *) NULL)
4301 return((Image *) NULL);
4302 if (border_image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00004303 (void) SetImageAlphaChannel(border_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004304 /*
4305 Shadow image.
4306 */
cristy70cddf72011-12-10 22:42:42 +00004307 status=MagickTrue;
cristydb070952012-04-20 14:33:00 +00004308 image_view=AcquireAuthenticCacheView(border_image,exception);
cristy70cddf72011-12-10 22:42:42 +00004309 for (y=0; y < (ssize_t) border_image->rows; y++)
4310 {
4311 PixelInfo
4312 background_color;
4313
4314 register Quantum
4315 *restrict q;
4316
4317 register ssize_t
4318 x;
4319
4320 if (status == MagickFalse)
4321 continue;
4322 q=QueueCacheViewAuthenticPixels(image_view,0,y,border_image->columns,1,
4323 exception);
4324 if (q == (Quantum *) NULL)
4325 {
4326 status=MagickFalse;
4327 continue;
4328 }
4329 background_color=border_image->background_color;
4330 background_color.matte=MagickTrue;
4331 for (x=0; x < (ssize_t) border_image->columns; x++)
4332 {
4333 if (border_image->matte != MagickFalse)
4334 background_color.alpha=GetPixelAlpha(border_image,q)*alpha/100.0;
4335 SetPixelInfoPixel(border_image,&background_color,q);
4336 q+=GetPixelChannels(border_image);
4337 }
4338 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4339 status=MagickFalse;
4340 }
4341 image_view=DestroyCacheView(image_view);
4342 if (status == MagickFalse)
4343 {
4344 border_image=DestroyImage(border_image);
4345 return((Image *) NULL);
4346 }
cristybd5a96c2011-08-21 00:04:26 +00004347 channel_mask=SetPixelChannelMask(border_image,AlphaChannel);
cristyaa2c16c2012-03-25 22:21:35 +00004348 shadow_image=BlurImage(border_image,0.0,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +00004349 border_image=DestroyImage(border_image);
4350 if (shadow_image == (Image *) NULL)
4351 return((Image *) NULL);
cristyae1969f2011-12-10 03:07:36 +00004352 (void) SetPixelChannelMapMask(shadow_image,channel_mask);
cristy3ed852e2009-09-05 21:47:34 +00004353 if (shadow_image->page.width == 0)
4354 shadow_image->page.width=shadow_image->columns;
4355 if (shadow_image->page.height == 0)
4356 shadow_image->page.height=shadow_image->rows;
cristybb503372010-05-27 20:51:26 +00004357 shadow_image->page.width+=x_offset-(ssize_t) border_info.width;
4358 shadow_image->page.height+=y_offset-(ssize_t) border_info.height;
4359 shadow_image->page.x+=x_offset-(ssize_t) border_info.width;
4360 shadow_image->page.y+=y_offset-(ssize_t) border_info.height;
cristy3ed852e2009-09-05 21:47:34 +00004361 return(shadow_image);
4362}
4363
4364/*
4365%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4366% %
4367% %
4368% %
4369% S k e t c h I m a g e %
4370% %
4371% %
4372% %
4373%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4374%
4375% SketchImage() simulates a pencil sketch. We convolve the image with a
4376% Gaussian operator of the given radius and standard deviation (sigma). For
4377% reasonable results, radius should be larger than sigma. Use a radius of 0
4378% and SketchImage() selects a suitable radius for you. Angle gives the angle
4379% of the sketch.
4380%
4381% The format of the SketchImage method is:
4382%
4383% Image *SketchImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00004384% const double sigma,const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004385%
4386% A description of each parameter follows:
4387%
4388% o image: the image.
4389%
cristy574cc262011-08-05 01:23:58 +00004390% o radius: the radius of the Gaussian, in pixels, not counting the
4391% center pixel.
cristy3ed852e2009-09-05 21:47:34 +00004392%
4393% o sigma: the standard deviation of the Gaussian, in pixels.
4394%
cristyf7ef0252011-09-09 14:50:06 +00004395% o angle: apply the effect along this angle.
4396%
cristy3ed852e2009-09-05 21:47:34 +00004397% o exception: return any errors or warnings in this structure.
4398%
4399*/
4400MagickExport Image *SketchImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00004401 const double sigma,const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004402{
cristyfa112112010-01-04 17:48:07 +00004403 CacheView
4404 *random_view;
4405
cristy3ed852e2009-09-05 21:47:34 +00004406 Image
4407 *blend_image,
4408 *blur_image,
4409 *dodge_image,
4410 *random_image,
4411 *sketch_image;
4412
cristy3ed852e2009-09-05 21:47:34 +00004413 MagickBooleanType
4414 status;
4415
cristy3ed852e2009-09-05 21:47:34 +00004416 RandomInfo
cristyfa112112010-01-04 17:48:07 +00004417 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00004418
cristy9d314ff2011-03-09 01:30:28 +00004419 ssize_t
4420 y;
4421
cristy57340e02012-05-05 00:53:23 +00004422 unsigned long
4423 key;
4424
cristy3ed852e2009-09-05 21:47:34 +00004425 /*
4426 Sketch image.
4427 */
4428 random_image=CloneImage(image,image->columns << 1,image->rows << 1,
4429 MagickTrue,exception);
4430 if (random_image == (Image *) NULL)
4431 return((Image *) NULL);
4432 status=MagickTrue;
cristy1b784432009-12-19 02:20:40 +00004433 random_info=AcquireRandomInfoThreadSet();
cristy57340e02012-05-05 00:53:23 +00004434 key=GetRandomSecretKey(random_info[0]);
cristydb070952012-04-20 14:33:00 +00004435 random_view=AcquireAuthenticCacheView(random_image,exception);
cristy1b784432009-12-19 02:20:40 +00004436#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy57340e02012-05-05 00:53:23 +00004437 #pragma omp parallel for schedule(static,4) shared(status) \
cristyddacdd12012-05-07 23:08:14 +00004438 dynamic_number_threads(image->columns,image->rows,key == ~0UL)
cristy1b784432009-12-19 02:20:40 +00004439#endif
cristybb503372010-05-27 20:51:26 +00004440 for (y=0; y < (ssize_t) random_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004441 {
cristy5c9e6f22010-09-17 17:31:01 +00004442 const int
4443 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00004444
cristybb503372010-05-27 20:51:26 +00004445 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004446 x;
4447
cristy4c08aed2011-07-01 19:47:50 +00004448 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004449 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004450
cristy1b784432009-12-19 02:20:40 +00004451 if (status == MagickFalse)
4452 continue;
cristy3ed852e2009-09-05 21:47:34 +00004453 q=QueueCacheViewAuthenticPixels(random_view,0,y,random_image->columns,1,
4454 exception);
cristyacd2ed22011-08-30 01:44:23 +00004455 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004456 {
4457 status=MagickFalse;
4458 continue;
4459 }
cristybb503372010-05-27 20:51:26 +00004460 for (x=0; x < (ssize_t) random_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004461 {
cristy76f512e2011-09-12 01:26:56 +00004462 MagickRealType
4463 value;
4464
4465 register ssize_t
4466 i;
4467
cristy10a6c612012-01-29 21:41:05 +00004468 if (GetPixelMask(random_image,q) != 0)
4469 {
4470 q+=GetPixelChannels(random_image);
4471 continue;
4472 }
cristy76f512e2011-09-12 01:26:56 +00004473 value=GetPseudoRandomValue(random_info[id]);
4474 for (i=0; i < (ssize_t) GetPixelChannels(random_image); i++)
4475 {
cristyabace412011-12-11 15:56:53 +00004476 PixelChannel
4477 channel;
4478
cristy76f512e2011-09-12 01:26:56 +00004479 PixelTrait
4480 traits;
4481
cristyabace412011-12-11 15:56:53 +00004482 channel=GetPixelChannelMapChannel(image,i);
4483 traits=GetPixelChannelMapTraits(image,channel);
cristy76f512e2011-09-12 01:26:56 +00004484 if (traits == UndefinedPixelTrait)
4485 continue;
4486 q[i]=ClampToQuantum(QuantumRange*value);
4487 }
cristyed231572011-07-14 02:18:59 +00004488 q+=GetPixelChannels(random_image);
cristy3ed852e2009-09-05 21:47:34 +00004489 }
4490 if (SyncCacheViewAuthenticPixels(random_view,exception) == MagickFalse)
4491 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004492 }
4493 random_view=DestroyCacheView(random_view);
cristy1b784432009-12-19 02:20:40 +00004494 random_info=DestroyRandomInfoThreadSet(random_info);
cristy3ed852e2009-09-05 21:47:34 +00004495 if (status == MagickFalse)
4496 {
4497 random_image=DestroyImage(random_image);
4498 return(random_image);
4499 }
cristyaa2c16c2012-03-25 22:21:35 +00004500 blur_image=MotionBlurImage(random_image,radius,sigma,angle,exception);
cristy3ed852e2009-09-05 21:47:34 +00004501 random_image=DestroyImage(random_image);
4502 if (blur_image == (Image *) NULL)
4503 return((Image *) NULL);
cristy6bfd6902011-12-09 01:33:45 +00004504 dodge_image=EdgeImage(blur_image,radius,1.0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004505 blur_image=DestroyImage(blur_image);
4506 if (dodge_image == (Image *) NULL)
4507 return((Image *) NULL);
cristye23ec9d2011-08-16 18:15:40 +00004508 (void) NormalizeImage(dodge_image,exception);
cristyb3e7c6c2011-07-24 01:43:55 +00004509 (void) NegateImage(dodge_image,MagickFalse,exception);
cristye941a752011-10-15 01:52:48 +00004510 (void) TransformImage(&dodge_image,(char *) NULL,"50%",exception);
cristy3ed852e2009-09-05 21:47:34 +00004511 sketch_image=CloneImage(image,0,0,MagickTrue,exception);
4512 if (sketch_image == (Image *) NULL)
4513 {
4514 dodge_image=DestroyImage(dodge_image);
4515 return((Image *) NULL);
4516 }
cristyfeb3e962012-03-29 17:25:55 +00004517 (void) CompositeImage(sketch_image,dodge_image,ColorDodgeCompositeOp,
cristy39172402012-03-30 13:04:39 +00004518 MagickTrue,0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004519 dodge_image=DestroyImage(dodge_image);
4520 blend_image=CloneImage(image,0,0,MagickTrue,exception);
4521 if (blend_image == (Image *) NULL)
4522 {
4523 sketch_image=DestroyImage(sketch_image);
4524 return((Image *) NULL);
4525 }
4526 (void) SetImageArtifact(blend_image,"compose:args","20x80");
cristy39172402012-03-30 13:04:39 +00004527 (void) CompositeImage(sketch_image,blend_image,BlendCompositeOp,MagickTrue,
cristyfeb3e962012-03-29 17:25:55 +00004528 0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004529 blend_image=DestroyImage(blend_image);
4530 return(sketch_image);
4531}
4532
4533/*
4534%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4535% %
4536% %
4537% %
4538% S o l a r i z e I m a g e %
4539% %
4540% %
4541% %
4542%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4543%
4544% SolarizeImage() applies a special effect to the image, similar to the effect
4545% achieved in a photo darkroom by selectively exposing areas of photo
4546% sensitive paper to light. Threshold ranges from 0 to QuantumRange and is a
4547% measure of the extent of the solarization.
4548%
4549% The format of the SolarizeImage method is:
4550%
cristy5cbc0162011-08-29 00:36:28 +00004551% MagickBooleanType SolarizeImage(Image *image,const double threshold,
4552% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004553%
4554% A description of each parameter follows:
4555%
4556% o image: the image.
4557%
4558% o threshold: Define the extent of the solarization.
4559%
cristy5cbc0162011-08-29 00:36:28 +00004560% o exception: return any errors or warnings in this structure.
4561%
cristy3ed852e2009-09-05 21:47:34 +00004562*/
4563MagickExport MagickBooleanType SolarizeImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00004564 const double threshold,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004565{
4566#define SolarizeImageTag "Solarize/Image"
4567
cristyc4c8d132010-01-07 01:58:38 +00004568 CacheView
4569 *image_view;
4570
cristy3ed852e2009-09-05 21:47:34 +00004571 MagickBooleanType
4572 status;
4573
cristybb503372010-05-27 20:51:26 +00004574 MagickOffsetType
4575 progress;
4576
4577 ssize_t
4578 y;
4579
cristy3ed852e2009-09-05 21:47:34 +00004580 assert(image != (Image *) NULL);
4581 assert(image->signature == MagickSignature);
4582 if (image->debug != MagickFalse)
4583 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4584 if (image->storage_class == PseudoClass)
4585 {
cristybb503372010-05-27 20:51:26 +00004586 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004587 i;
4588
4589 /*
4590 Solarize colormap.
4591 */
cristybb503372010-05-27 20:51:26 +00004592 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00004593 {
4594 if ((MagickRealType) image->colormap[i].red > threshold)
4595 image->colormap[i].red=(Quantum) QuantumRange-image->colormap[i].red;
4596 if ((MagickRealType) image->colormap[i].green > threshold)
4597 image->colormap[i].green=(Quantum) QuantumRange-
4598 image->colormap[i].green;
4599 if ((MagickRealType) image->colormap[i].blue > threshold)
4600 image->colormap[i].blue=(Quantum) QuantumRange-
4601 image->colormap[i].blue;
4602 }
4603 }
4604 /*
4605 Solarize image.
4606 */
4607 status=MagickTrue;
4608 progress=0;
cristydb070952012-04-20 14:33:00 +00004609 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00004610#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00004611 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristyddacdd12012-05-07 23:08:14 +00004612 dynamic_number_threads(image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00004613#endif
cristybb503372010-05-27 20:51:26 +00004614 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004615 {
cristybb503372010-05-27 20:51:26 +00004616 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004617 x;
4618
cristy4c08aed2011-07-01 19:47:50 +00004619 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004620 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004621
4622 if (status == MagickFalse)
4623 continue;
cristy5cbc0162011-08-29 00:36:28 +00004624 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00004625 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004626 {
4627 status=MagickFalse;
4628 continue;
4629 }
cristybb503372010-05-27 20:51:26 +00004630 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004631 {
cristy76f512e2011-09-12 01:26:56 +00004632 register ssize_t
4633 i;
4634
cristy10a6c612012-01-29 21:41:05 +00004635 if (GetPixelMask(image,q) != 0)
4636 {
4637 q+=GetPixelChannels(image);
4638 continue;
4639 }
cristy76f512e2011-09-12 01:26:56 +00004640 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4641 {
cristyabace412011-12-11 15:56:53 +00004642 PixelChannel
4643 channel;
4644
cristy76f512e2011-09-12 01:26:56 +00004645 PixelTrait
4646 traits;
4647
cristyabace412011-12-11 15:56:53 +00004648 channel=GetPixelChannelMapChannel(image,i);
4649 traits=GetPixelChannelMapTraits(image,channel);
cristy76f512e2011-09-12 01:26:56 +00004650 if ((traits == UndefinedPixelTrait) ||
4651 ((traits & CopyPixelTrait) != 0))
4652 continue;
4653 if ((MagickRealType) q[i] > threshold)
4654 q[i]=QuantumRange-q[i];
4655 }
cristyed231572011-07-14 02:18:59 +00004656 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00004657 }
4658 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4659 status=MagickFalse;
4660 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4661 {
4662 MagickBooleanType
4663 proceed;
4664
cristyb5d5f722009-11-04 03:03:49 +00004665#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00004666 #pragma omp critical (MagickCore_SolarizeImage)
cristy3ed852e2009-09-05 21:47:34 +00004667#endif
4668 proceed=SetImageProgress(image,SolarizeImageTag,progress++,image->rows);
4669 if (proceed == MagickFalse)
4670 status=MagickFalse;
4671 }
4672 }
4673 image_view=DestroyCacheView(image_view);
4674 return(status);
4675}
4676
4677/*
4678%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4679% %
4680% %
4681% %
4682% S t e g a n o I m a g e %
4683% %
4684% %
4685% %
4686%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4687%
4688% SteganoImage() hides a digital watermark within the image. Recover
4689% the hidden watermark later to prove that the authenticity of an image.
4690% Offset defines the start position within the image to hide the watermark.
4691%
4692% The format of the SteganoImage method is:
4693%
4694% Image *SteganoImage(const Image *image,Image *watermark,
4695% ExceptionInfo *exception)
4696%
4697% A description of each parameter follows:
4698%
4699% o image: the image.
4700%
4701% o watermark: the watermark image.
4702%
4703% o exception: return any errors or warnings in this structure.
4704%
4705*/
4706MagickExport Image *SteganoImage(const Image *image,const Image *watermark,
4707 ExceptionInfo *exception)
4708{
cristye1bf8ad2010-09-19 17:07:03 +00004709#define GetBit(alpha,i) ((((size_t) (alpha) >> (size_t) (i)) & 0x01) != 0)
cristy4c08aed2011-07-01 19:47:50 +00004710#define SetBit(alpha,i,set) (Quantum) ((set) != 0 ? (size_t) (alpha) \
cristyeaedf062010-05-29 22:36:02 +00004711 | (one << (size_t) (i)) : (size_t) (alpha) & ~(one << (size_t) (i)))
cristy3ed852e2009-09-05 21:47:34 +00004712#define SteganoImageTag "Stegano/Image"
4713
cristyb0d3bb92010-09-22 14:37:58 +00004714 CacheView
4715 *stegano_view,
4716 *watermark_view;
4717
cristy3ed852e2009-09-05 21:47:34 +00004718 Image
4719 *stegano_image;
4720
4721 int
4722 c;
4723
cristy3ed852e2009-09-05 21:47:34 +00004724 MagickBooleanType
4725 status;
4726
cristy101ab702011-10-13 13:06:32 +00004727 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00004728 pixel;
4729
cristy4c08aed2011-07-01 19:47:50 +00004730 register Quantum
cristy3ed852e2009-09-05 21:47:34 +00004731 *q;
4732
cristye1bf8ad2010-09-19 17:07:03 +00004733 register ssize_t
4734 x;
4735
cristybb503372010-05-27 20:51:26 +00004736 size_t
cristyeaedf062010-05-29 22:36:02 +00004737 depth,
4738 one;
cristy3ed852e2009-09-05 21:47:34 +00004739
cristye1bf8ad2010-09-19 17:07:03 +00004740 ssize_t
4741 i,
4742 j,
4743 k,
4744 y;
4745
cristy3ed852e2009-09-05 21:47:34 +00004746 /*
4747 Initialize steganographic image attributes.
4748 */
4749 assert(image != (const Image *) NULL);
4750 assert(image->signature == MagickSignature);
4751 if (image->debug != MagickFalse)
4752 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4753 assert(watermark != (const Image *) NULL);
4754 assert(watermark->signature == MagickSignature);
4755 assert(exception != (ExceptionInfo *) NULL);
4756 assert(exception->signature == MagickSignature);
cristyeaedf062010-05-29 22:36:02 +00004757 one=1UL;
cristy3ed852e2009-09-05 21:47:34 +00004758 stegano_image=CloneImage(image,0,0,MagickTrue,exception);
4759 if (stegano_image == (Image *) NULL)
4760 return((Image *) NULL);
cristyf61b1832012-04-01 01:38:19 +00004761 stegano_image->depth=MAGICKCORE_QUANTUM_DEPTH;
cristy574cc262011-08-05 01:23:58 +00004762 if (SetImageStorageClass(stegano_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004763 {
cristy3ed852e2009-09-05 21:47:34 +00004764 stegano_image=DestroyImage(stegano_image);
4765 return((Image *) NULL);
4766 }
cristy3ed852e2009-09-05 21:47:34 +00004767 /*
4768 Hide watermark in low-order bits of image.
4769 */
4770 c=0;
4771 i=0;
4772 j=0;
4773 depth=stegano_image->depth;
cristyf61b1832012-04-01 01:38:19 +00004774 k=stegano_image->offset;
cristyda16f162011-02-19 23:52:17 +00004775 status=MagickTrue;
cristydb070952012-04-20 14:33:00 +00004776 watermark_view=AcquireVirtualCacheView(watermark,exception);
4777 stegano_view=AcquireAuthenticCacheView(stegano_image,exception);
cristybb503372010-05-27 20:51:26 +00004778 for (i=(ssize_t) depth-1; (i >= 0) && (j < (ssize_t) depth); i--)
cristy3ed852e2009-09-05 21:47:34 +00004779 {
cristybb503372010-05-27 20:51:26 +00004780 for (y=0; (y < (ssize_t) watermark->rows) && (j < (ssize_t) depth); y++)
cristy3ed852e2009-09-05 21:47:34 +00004781 {
cristybb503372010-05-27 20:51:26 +00004782 for (x=0; (x < (ssize_t) watermark->columns) && (j < (ssize_t) depth); x++)
cristy3ed852e2009-09-05 21:47:34 +00004783 {
cristy1707c6c2012-01-18 23:30:54 +00004784 ssize_t
4785 offset;
4786
cristyf05d4942012-03-17 16:26:09 +00004787 (void) GetOneCacheViewVirtualPixelInfo(watermark_view,x,y,&pixel,
cristyda1f9c12011-10-02 21:39:49 +00004788 exception);
cristy1707c6c2012-01-18 23:30:54 +00004789 offset=k/(ssize_t) stegano_image->columns;
4790 if (offset >= (ssize_t) stegano_image->rows)
cristy3ed852e2009-09-05 21:47:34 +00004791 break;
cristyb0d3bb92010-09-22 14:37:58 +00004792 q=GetCacheViewAuthenticPixels(stegano_view,k % (ssize_t)
4793 stegano_image->columns,k/(ssize_t) stegano_image->columns,1,1,
4794 exception);
cristyacd2ed22011-08-30 01:44:23 +00004795 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004796 break;
4797 switch (c)
4798 {
4799 case 0:
4800 {
cristyf61b1832012-04-01 01:38:19 +00004801 SetPixelRed(stegano_image,SetBit(GetPixelRed(stegano_image,q),j,
4802 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004803 break;
4804 }
4805 case 1:
4806 {
cristyf61b1832012-04-01 01:38:19 +00004807 SetPixelGreen(stegano_image,SetBit(GetPixelGreen(stegano_image,q),j,
4808 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004809 break;
4810 }
4811 case 2:
4812 {
cristyf61b1832012-04-01 01:38:19 +00004813 SetPixelBlue(stegano_image,SetBit(GetPixelBlue(stegano_image,q),j,
4814 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004815 break;
4816 }
4817 }
cristyb0d3bb92010-09-22 14:37:58 +00004818 if (SyncCacheViewAuthenticPixels(stegano_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004819 break;
4820 c++;
4821 if (c == 3)
4822 c=0;
4823 k++;
cristybb503372010-05-27 20:51:26 +00004824 if (k == (ssize_t) (stegano_image->columns*stegano_image->columns))
cristy3ed852e2009-09-05 21:47:34 +00004825 k=0;
cristyf61b1832012-04-01 01:38:19 +00004826 if (k == stegano_image->offset)
cristy3ed852e2009-09-05 21:47:34 +00004827 j++;
4828 }
4829 }
cristy8b27a6d2010-02-14 03:31:15 +00004830 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004831 {
cristy8b27a6d2010-02-14 03:31:15 +00004832 MagickBooleanType
4833 proceed;
4834
4835 proceed=SetImageProgress(image,SteganoImageTag,(MagickOffsetType)
4836 (depth-i),depth);
4837 if (proceed == MagickFalse)
4838 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004839 }
4840 }
cristyb0d3bb92010-09-22 14:37:58 +00004841 stegano_view=DestroyCacheView(stegano_view);
4842 watermark_view=DestroyCacheView(watermark_view);
cristyda16f162011-02-19 23:52:17 +00004843 if (status == MagickFalse)
4844 {
4845 stegano_image=DestroyImage(stegano_image);
4846 return((Image *) NULL);
4847 }
cristy3ed852e2009-09-05 21:47:34 +00004848 return(stegano_image);
4849}
4850
4851/*
4852%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4853% %
4854% %
4855% %
4856% S t e r e o A n a g l y p h I m a g e %
4857% %
4858% %
4859% %
4860%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4861%
4862% StereoAnaglyphImage() combines two images and produces a single image that
4863% is the composite of a left and right image of a stereo pair. Special
4864% red-green stereo glasses are required to view this effect.
4865%
4866% The format of the StereoAnaglyphImage method is:
4867%
4868% Image *StereoImage(const Image *left_image,const Image *right_image,
4869% ExceptionInfo *exception)
4870% Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004871% const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004872% ExceptionInfo *exception)
4873%
4874% A description of each parameter follows:
4875%
4876% o left_image: the left image.
4877%
4878% o right_image: the right image.
4879%
4880% o exception: return any errors or warnings in this structure.
4881%
4882% o x_offset: amount, in pixels, by which the left image is offset to the
4883% right of the right image.
4884%
4885% o y_offset: amount, in pixels, by which the left image is offset to the
4886% bottom of the right image.
4887%
4888%
4889*/
4890MagickExport Image *StereoImage(const Image *left_image,
4891 const Image *right_image,ExceptionInfo *exception)
4892{
4893 return(StereoAnaglyphImage(left_image,right_image,0,0,exception));
4894}
4895
4896MagickExport Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004897 const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004898 ExceptionInfo *exception)
4899{
4900#define StereoImageTag "Stereo/Image"
4901
4902 const Image
4903 *image;
4904
4905 Image
4906 *stereo_image;
4907
cristy3ed852e2009-09-05 21:47:34 +00004908 MagickBooleanType
4909 status;
4910
cristy9d314ff2011-03-09 01:30:28 +00004911 ssize_t
4912 y;
4913
cristy3ed852e2009-09-05 21:47:34 +00004914 assert(left_image != (const Image *) NULL);
4915 assert(left_image->signature == MagickSignature);
4916 if (left_image->debug != MagickFalse)
4917 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4918 left_image->filename);
4919 assert(right_image != (const Image *) NULL);
4920 assert(right_image->signature == MagickSignature);
4921 assert(exception != (ExceptionInfo *) NULL);
4922 assert(exception->signature == MagickSignature);
4923 assert(right_image != (const Image *) NULL);
4924 image=left_image;
4925 if ((left_image->columns != right_image->columns) ||
4926 (left_image->rows != right_image->rows))
4927 ThrowImageException(ImageError,"LeftAndRightImageSizesDiffer");
4928 /*
4929 Initialize stereo image attributes.
4930 */
4931 stereo_image=CloneImage(left_image,left_image->columns,left_image->rows,
4932 MagickTrue,exception);
4933 if (stereo_image == (Image *) NULL)
4934 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004935 if (SetImageStorageClass(stereo_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004936 {
cristy3ed852e2009-09-05 21:47:34 +00004937 stereo_image=DestroyImage(stereo_image);
4938 return((Image *) NULL);
4939 }
4940 /*
4941 Copy left image to red channel and right image to blue channel.
4942 */
cristyda16f162011-02-19 23:52:17 +00004943 status=MagickTrue;
cristybb503372010-05-27 20:51:26 +00004944 for (y=0; y < (ssize_t) stereo_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004945 {
cristy4c08aed2011-07-01 19:47:50 +00004946 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004947 *restrict p,
4948 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004949
cristybb503372010-05-27 20:51:26 +00004950 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004951 x;
4952
cristy4c08aed2011-07-01 19:47:50 +00004953 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004954 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +00004955
4956 p=GetVirtualPixels(left_image,-x_offset,y-y_offset,image->columns,1,
4957 exception);
4958 q=GetVirtualPixels(right_image,0,y,right_image->columns,1,exception);
4959 r=QueueAuthenticPixels(stereo_image,0,y,stereo_image->columns,1,exception);
cristy76f512e2011-09-12 01:26:56 +00004960 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL) ||
4961 (r == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004962 break;
cristybb503372010-05-27 20:51:26 +00004963 for (x=0; x < (ssize_t) stereo_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004964 {
cristy4c08aed2011-07-01 19:47:50 +00004965 SetPixelRed(image,GetPixelRed(left_image,p),r);
cristy76f512e2011-09-12 01:26:56 +00004966 SetPixelGreen(image,GetPixelGreen(right_image,q),r);
4967 SetPixelBlue(image,GetPixelBlue(right_image,q),r);
4968 if ((GetPixelAlphaTraits(stereo_image) & CopyPixelTrait) != 0)
4969 SetPixelAlpha(image,(GetPixelAlpha(left_image,p)+
4970 GetPixelAlpha(right_image,q))/2,r);
cristyed231572011-07-14 02:18:59 +00004971 p+=GetPixelChannels(left_image);
cristy76f512e2011-09-12 01:26:56 +00004972 q+=GetPixelChannels(right_image);
4973 r+=GetPixelChannels(stereo_image);
cristy3ed852e2009-09-05 21:47:34 +00004974 }
4975 if (SyncAuthenticPixels(stereo_image,exception) == MagickFalse)
4976 break;
cristy8b27a6d2010-02-14 03:31:15 +00004977 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004978 {
cristy8b27a6d2010-02-14 03:31:15 +00004979 MagickBooleanType
4980 proceed;
4981
cristybb503372010-05-27 20:51:26 +00004982 proceed=SetImageProgress(image,StereoImageTag,(MagickOffsetType) y,
4983 stereo_image->rows);
cristy8b27a6d2010-02-14 03:31:15 +00004984 if (proceed == MagickFalse)
4985 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004986 }
4987 }
cristyda16f162011-02-19 23:52:17 +00004988 if (status == MagickFalse)
4989 {
4990 stereo_image=DestroyImage(stereo_image);
4991 return((Image *) NULL);
4992 }
cristy3ed852e2009-09-05 21:47:34 +00004993 return(stereo_image);
4994}
4995
4996/*
4997%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4998% %
4999% %
5000% %
5001% S w i r l I m a g e %
5002% %
5003% %
5004% %
5005%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5006%
5007% SwirlImage() swirls the pixels about the center of the image, where
5008% degrees indicates the sweep of the arc through which each pixel is moved.
5009% You get a more dramatic effect as the degrees move from 1 to 360.
5010%
5011% The format of the SwirlImage method is:
5012%
5013% Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00005014% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005015%
5016% A description of each parameter follows:
5017%
5018% o image: the image.
5019%
5020% o degrees: Define the tightness of the swirling effect.
5021%
cristy76f512e2011-09-12 01:26:56 +00005022% o method: the pixel interpolation method.
5023%
cristy3ed852e2009-09-05 21:47:34 +00005024% o exception: return any errors or warnings in this structure.
5025%
5026*/
5027MagickExport Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00005028 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005029{
5030#define SwirlImageTag "Swirl/Image"
5031
cristyfa112112010-01-04 17:48:07 +00005032 CacheView
5033 *image_view,
5034 *swirl_view;
5035
cristy3ed852e2009-09-05 21:47:34 +00005036 Image
5037 *swirl_image;
5038
cristy3ed852e2009-09-05 21:47:34 +00005039 MagickBooleanType
5040 status;
5041
cristybb503372010-05-27 20:51:26 +00005042 MagickOffsetType
5043 progress;
5044
cristy3ed852e2009-09-05 21:47:34 +00005045 MagickRealType
5046 radius;
5047
5048 PointInfo
5049 center,
5050 scale;
5051
cristybb503372010-05-27 20:51:26 +00005052 ssize_t
5053 y;
5054
cristy3ed852e2009-09-05 21:47:34 +00005055 /*
5056 Initialize swirl image attributes.
5057 */
5058 assert(image != (const Image *) NULL);
5059 assert(image->signature == MagickSignature);
5060 if (image->debug != MagickFalse)
5061 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5062 assert(exception != (ExceptionInfo *) NULL);
5063 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00005064 swirl_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005065 if (swirl_image == (Image *) NULL)
5066 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005067 if (SetImageStorageClass(swirl_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005068 {
cristy3ed852e2009-09-05 21:47:34 +00005069 swirl_image=DestroyImage(swirl_image);
5070 return((Image *) NULL);
5071 }
cristy4c08aed2011-07-01 19:47:50 +00005072 if (swirl_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00005073 swirl_image->matte=MagickTrue;
5074 /*
5075 Compute scaling factor.
5076 */
5077 center.x=(double) image->columns/2.0;
5078 center.y=(double) image->rows/2.0;
5079 radius=MagickMax(center.x,center.y);
5080 scale.x=1.0;
5081 scale.y=1.0;
5082 if (image->columns > image->rows)
5083 scale.y=(double) image->columns/(double) image->rows;
5084 else
5085 if (image->columns < image->rows)
5086 scale.x=(double) image->rows/(double) image->columns;
5087 degrees=(double) DegreesToRadians(degrees);
5088 /*
5089 Swirl image.
5090 */
5091 status=MagickTrue;
5092 progress=0;
cristydb070952012-04-20 14:33:00 +00005093 image_view=AcquireVirtualCacheView(image,exception);
5094 swirl_view=AcquireAuthenticCacheView(swirl_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00005095#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00005096 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristyddacdd12012-05-07 23:08:14 +00005097 dynamic_number_threads(image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00005098#endif
cristybb503372010-05-27 20:51:26 +00005099 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005100 {
cristy3ed852e2009-09-05 21:47:34 +00005101 MagickRealType
5102 distance;
5103
5104 PointInfo
5105 delta;
5106
cristy6d188022011-09-12 13:23:33 +00005107 register const Quantum
5108 *restrict p;
5109
cristybb503372010-05-27 20:51:26 +00005110 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005111 x;
5112
cristy4c08aed2011-07-01 19:47:50 +00005113 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005114 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005115
5116 if (status == MagickFalse)
5117 continue;
cristy6d188022011-09-12 13:23:33 +00005118 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy1707c6c2012-01-18 23:30:54 +00005119 q=QueueCacheViewAuthenticPixels(swirl_view,0,y,swirl_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00005120 exception);
cristy6d188022011-09-12 13:23:33 +00005121 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005122 {
5123 status=MagickFalse;
5124 continue;
5125 }
cristy3ed852e2009-09-05 21:47:34 +00005126 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00005127 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005128 {
5129 /*
5130 Determine if the pixel is within an ellipse.
5131 */
cristy10a6c612012-01-29 21:41:05 +00005132 if (GetPixelMask(image,p) != 0)
5133 {
5134 p+=GetPixelChannels(image);
5135 q+=GetPixelChannels(swirl_image);
5136 continue;
5137 }
cristy3ed852e2009-09-05 21:47:34 +00005138 delta.x=scale.x*(double) (x-center.x);
5139 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00005140 if (distance >= (radius*radius))
5141 {
cristy1707c6c2012-01-18 23:30:54 +00005142 register ssize_t
5143 i;
5144
cristy6d188022011-09-12 13:23:33 +00005145 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy1707c6c2012-01-18 23:30:54 +00005146 {
5147 PixelChannel
5148 channel;
5149
5150 PixelTrait
5151 swirl_traits,
5152 traits;
5153
5154 channel=GetPixelChannelMapChannel(image,i);
5155 traits=GetPixelChannelMapTraits(image,channel);
5156 swirl_traits=GetPixelChannelMapTraits(swirl_image,channel);
5157 if ((traits == UndefinedPixelTrait) ||
5158 (swirl_traits == UndefinedPixelTrait))
5159 continue;
5160 SetPixelChannel(swirl_image,channel,p[i],q);
5161 }
cristy6d188022011-09-12 13:23:33 +00005162 }
5163 else
cristy3ed852e2009-09-05 21:47:34 +00005164 {
5165 MagickRealType
5166 cosine,
5167 factor,
5168 sine;
5169
5170 /*
5171 Swirl the pixel.
5172 */
5173 factor=1.0-sqrt((double) distance)/radius;
5174 sine=sin((double) (degrees*factor*factor));
5175 cosine=cos((double) (degrees*factor*factor));
cristy76f512e2011-09-12 01:26:56 +00005176 status=InterpolatePixelChannels(image,image_view,swirl_image,method,
5177 ((cosine*delta.x-sine*delta.y)/scale.x+center.x),(double)
5178 ((sine*delta.x+cosine*delta.y)/scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00005179 }
cristy6d188022011-09-12 13:23:33 +00005180 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00005181 q+=GetPixelChannels(swirl_image);
cristy3ed852e2009-09-05 21:47:34 +00005182 }
5183 if (SyncCacheViewAuthenticPixels(swirl_view,exception) == MagickFalse)
5184 status=MagickFalse;
5185 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5186 {
5187 MagickBooleanType
5188 proceed;
5189
cristyb5d5f722009-11-04 03:03:49 +00005190#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005191 #pragma omp critical (MagickCore_SwirlImage)
cristy3ed852e2009-09-05 21:47:34 +00005192#endif
5193 proceed=SetImageProgress(image,SwirlImageTag,progress++,image->rows);
5194 if (proceed == MagickFalse)
5195 status=MagickFalse;
5196 }
5197 }
5198 swirl_view=DestroyCacheView(swirl_view);
5199 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00005200 if (status == MagickFalse)
5201 swirl_image=DestroyImage(swirl_image);
5202 return(swirl_image);
5203}
5204
5205/*
5206%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5207% %
5208% %
5209% %
5210% T i n t I m a g e %
5211% %
5212% %
5213% %
5214%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5215%
5216% TintImage() applies a color vector to each pixel in the image. The length
5217% of the vector is 0 for black and white and at its maximum for the midtones.
5218% The vector weighting function is f(x)=(1-(4.0*((x-0.5)*(x-0.5))))
5219%
5220% The format of the TintImage method is:
5221%
cristyb817c3f2011-10-03 14:00:35 +00005222% Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005223% const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005224%
5225% A description of each parameter follows:
5226%
5227% o image: the image.
5228%
cristyb817c3f2011-10-03 14:00:35 +00005229% o blend: A color value used for tinting.
cristy3ed852e2009-09-05 21:47:34 +00005230%
5231% o tint: A color value used for tinting.
5232%
5233% o exception: return any errors or warnings in this structure.
5234%
5235*/
cristyb817c3f2011-10-03 14:00:35 +00005236MagickExport Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005237 const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005238{
5239#define TintImageTag "Tint/Image"
5240
cristyc4c8d132010-01-07 01:58:38 +00005241 CacheView
5242 *image_view,
5243 *tint_view;
5244
cristy3ed852e2009-09-05 21:47:34 +00005245 GeometryInfo
5246 geometry_info;
5247
5248 Image
5249 *tint_image;
5250
cristy3ed852e2009-09-05 21:47:34 +00005251 MagickBooleanType
5252 status;
5253
cristybb503372010-05-27 20:51:26 +00005254 MagickOffsetType
5255 progress;
cristy3ed852e2009-09-05 21:47:34 +00005256
cristy28474bf2011-09-11 23:32:52 +00005257 MagickRealType
5258 intensity;
5259
cristy4c08aed2011-07-01 19:47:50 +00005260 PixelInfo
cristy1707c6c2012-01-18 23:30:54 +00005261 color_vector;
cristy3ed852e2009-09-05 21:47:34 +00005262
cristybb503372010-05-27 20:51:26 +00005263 MagickStatusType
5264 flags;
5265
5266 ssize_t
5267 y;
5268
cristy3ed852e2009-09-05 21:47:34 +00005269 /*
5270 Allocate tint image.
5271 */
5272 assert(image != (const Image *) NULL);
5273 assert(image->signature == MagickSignature);
5274 if (image->debug != MagickFalse)
5275 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5276 assert(exception != (ExceptionInfo *) NULL);
5277 assert(exception->signature == MagickSignature);
5278 tint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5279 if (tint_image == (Image *) NULL)
5280 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005281 if (SetImageStorageClass(tint_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005282 {
cristy3ed852e2009-09-05 21:47:34 +00005283 tint_image=DestroyImage(tint_image);
5284 return((Image *) NULL);
5285 }
cristyaed9c382011-10-03 17:54:21 +00005286 if (blend == (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005287 return(tint_image);
5288 /*
5289 Determine RGB values of the color.
5290 */
cristy1707c6c2012-01-18 23:30:54 +00005291 GetPixelInfo(image,&color_vector);
cristyb817c3f2011-10-03 14:00:35 +00005292 flags=ParseGeometry(blend,&geometry_info);
cristy1707c6c2012-01-18 23:30:54 +00005293 color_vector.red=geometry_info.rho;
5294 color_vector.green=geometry_info.rho;
5295 color_vector.blue=geometry_info.rho;
5296 color_vector.alpha=OpaqueAlpha;
cristy3ed852e2009-09-05 21:47:34 +00005297 if ((flags & SigmaValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005298 color_vector.green=geometry_info.sigma;
cristy3ed852e2009-09-05 21:47:34 +00005299 if ((flags & XiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005300 color_vector.blue=geometry_info.xi;
cristyb817c3f2011-10-03 14:00:35 +00005301 if ((flags & PsiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005302 color_vector.alpha=geometry_info.psi;
cristy76f512e2011-09-12 01:26:56 +00005303 if (image->colorspace == CMYKColorspace)
5304 {
cristy1707c6c2012-01-18 23:30:54 +00005305 color_vector.black=geometry_info.rho;
cristy76f512e2011-09-12 01:26:56 +00005306 if ((flags & PsiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005307 color_vector.black=geometry_info.psi;
cristy76f512e2011-09-12 01:26:56 +00005308 if ((flags & ChiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005309 color_vector.alpha=geometry_info.chi;
cristy76f512e2011-09-12 01:26:56 +00005310 }
cristy28474bf2011-09-11 23:32:52 +00005311 intensity=(MagickRealType) GetPixelInfoIntensity(tint);
cristy1707c6c2012-01-18 23:30:54 +00005312 color_vector.red=(MagickRealType) (color_vector.red*tint->red/100.0-
5313 intensity);
5314 color_vector.green=(MagickRealType) (color_vector.green*tint->green/100.0-
5315 intensity);
5316 color_vector.blue=(MagickRealType) (color_vector.blue*tint->blue/100.0-
5317 intensity);
5318 color_vector.black=(MagickRealType) (color_vector.black*tint->black/100.0-
5319 intensity);
5320 color_vector.alpha=(MagickRealType) (color_vector.alpha*tint->alpha/100.0-
5321 intensity);
cristy3ed852e2009-09-05 21:47:34 +00005322 /*
5323 Tint image.
5324 */
5325 status=MagickTrue;
5326 progress=0;
cristydb070952012-04-20 14:33:00 +00005327 image_view=AcquireVirtualCacheView(image,exception);
5328 tint_view=AcquireAuthenticCacheView(tint_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00005329#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00005330 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristyddacdd12012-05-07 23:08:14 +00005331 dynamic_number_threads(image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00005332#endif
cristybb503372010-05-27 20:51:26 +00005333 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005334 {
cristy4c08aed2011-07-01 19:47:50 +00005335 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00005336 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00005337
cristy4c08aed2011-07-01 19:47:50 +00005338 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005339 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005340
cristy6b91acb2011-04-19 12:23:54 +00005341 register ssize_t
5342 x;
5343
cristy3ed852e2009-09-05 21:47:34 +00005344 if (status == MagickFalse)
5345 continue;
5346 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5347 q=QueueCacheViewAuthenticPixels(tint_view,0,y,tint_image->columns,1,
5348 exception);
cristy4c08aed2011-07-01 19:47:50 +00005349 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005350 {
5351 status=MagickFalse;
5352 continue;
5353 }
cristybb503372010-05-27 20:51:26 +00005354 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005355 {
cristy4c08aed2011-07-01 19:47:50 +00005356 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00005357 pixel;
5358
5359 MagickRealType
5360 weight;
5361
cristy1707c6c2012-01-18 23:30:54 +00005362 register ssize_t
5363 i;
5364
cristy10a6c612012-01-29 21:41:05 +00005365 if (GetPixelMask(image,p) != 0)
5366 {
5367 p+=GetPixelChannels(image);
5368 q+=GetPixelChannels(tint_image);
5369 continue;
5370 }
cristy1707c6c2012-01-18 23:30:54 +00005371 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5372 {
5373 PixelChannel
5374 channel;
5375
5376 PixelTrait
5377 tint_traits,
5378 traits;
5379
5380 channel=GetPixelChannelMapChannel(image,i);
5381 traits=GetPixelChannelMapTraits(image,channel);
5382 tint_traits=GetPixelChannelMapTraits(tint_image,channel);
5383 if ((traits == UndefinedPixelTrait) ||
5384 (tint_traits == UndefinedPixelTrait))
5385 continue;
5386 if ((tint_traits & CopyPixelTrait) != 0)
5387 {
5388 SetPixelChannel(tint_image,channel,p[i],q);
5389 continue;
5390 }
5391 }
5392 GetPixelInfo(image,&pixel);
5393 weight=QuantumScale*GetPixelRed(image,p)-0.5;
5394 pixel.red=(MagickRealType) GetPixelRed(image,p)+color_vector.red*
5395 (1.0-(4.0*(weight*weight)));
5396 weight=QuantumScale*GetPixelGreen(image,p)-0.5;
5397 pixel.green=(MagickRealType) GetPixelGreen(image,p)+color_vector.green*
5398 (1.0-(4.0*(weight*weight)));
5399 weight=QuantumScale*GetPixelBlue(image,p)-0.5;
5400 pixel.blue=(MagickRealType) GetPixelBlue(image,p)+color_vector.blue*
5401 (1.0-(4.0*(weight*weight)));
5402 weight=QuantumScale*GetPixelBlack(image,p)-0.5;
5403 pixel.black=(MagickRealType) GetPixelBlack(image,p)+color_vector.black*
5404 (1.0-(4.0*(weight*weight)));
5405 SetPixelInfoPixel(tint_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +00005406 p+=GetPixelChannels(image);
5407 q+=GetPixelChannels(tint_image);
cristy3ed852e2009-09-05 21:47:34 +00005408 }
5409 if (SyncCacheViewAuthenticPixels(tint_view,exception) == MagickFalse)
5410 status=MagickFalse;
5411 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5412 {
5413 MagickBooleanType
5414 proceed;
5415
cristyb5d5f722009-11-04 03:03:49 +00005416#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005417 #pragma omp critical (MagickCore_TintImage)
cristy3ed852e2009-09-05 21:47:34 +00005418#endif
5419 proceed=SetImageProgress(image,TintImageTag,progress++,image->rows);
5420 if (proceed == MagickFalse)
5421 status=MagickFalse;
5422 }
5423 }
5424 tint_view=DestroyCacheView(tint_view);
5425 image_view=DestroyCacheView(image_view);
5426 if (status == MagickFalse)
5427 tint_image=DestroyImage(tint_image);
5428 return(tint_image);
5429}
5430
5431/*
5432%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5433% %
5434% %
5435% %
5436% V i g n e t t e I m a g e %
5437% %
5438% %
5439% %
5440%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5441%
5442% VignetteImage() softens the edges of the image in vignette style.
5443%
5444% The format of the VignetteImage method is:
5445%
5446% Image *VignetteImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00005447% const double sigma,const ssize_t x,const ssize_t y,
cristy05c0c9a2011-09-05 23:16:13 +00005448% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005449%
5450% A description of each parameter follows:
5451%
5452% o image: the image.
5453%
5454% o radius: the radius of the pixel neighborhood.
5455%
5456% o sigma: the standard deviation of the Gaussian, in pixels.
5457%
5458% o x, y: Define the x and y ellipse offset.
5459%
5460% o exception: return any errors or warnings in this structure.
5461%
5462*/
5463MagickExport Image *VignetteImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00005464 const double sigma,const ssize_t x,const ssize_t y,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005465{
5466 char
5467 ellipse[MaxTextExtent];
5468
5469 DrawInfo
5470 *draw_info;
5471
5472 Image
5473 *canvas_image,
5474 *blur_image,
5475 *oval_image,
5476 *vignette_image;
5477
5478 assert(image != (Image *) NULL);
5479 assert(image->signature == MagickSignature);
5480 if (image->debug != MagickFalse)
5481 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5482 assert(exception != (ExceptionInfo *) NULL);
5483 assert(exception->signature == MagickSignature);
5484 canvas_image=CloneImage(image,0,0,MagickTrue,exception);
5485 if (canvas_image == (Image *) NULL)
5486 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005487 if (SetImageStorageClass(canvas_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005488 {
cristy3ed852e2009-09-05 21:47:34 +00005489 canvas_image=DestroyImage(canvas_image);
5490 return((Image *) NULL);
5491 }
5492 canvas_image->matte=MagickTrue;
cristy98621462011-12-31 22:31:11 +00005493 oval_image=CloneImage(canvas_image,canvas_image->columns,canvas_image->rows,
5494 MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005495 if (oval_image == (Image *) NULL)
5496 {
5497 canvas_image=DestroyImage(canvas_image);
5498 return((Image *) NULL);
5499 }
cristy9950d572011-10-01 18:22:35 +00005500 (void) QueryColorCompliance("#000000",AllCompliance,
5501 &oval_image->background_color,exception);
cristyea1a8aa2011-10-20 13:24:06 +00005502 (void) SetImageBackgroundColor(oval_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00005503 draw_info=CloneDrawInfo((const ImageInfo *) NULL,(const DrawInfo *) NULL);
cristy9950d572011-10-01 18:22:35 +00005504 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->fill,
5505 exception);
5506 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->stroke,
5507 exception);
cristy1707c6c2012-01-18 23:30:54 +00005508 (void) FormatLocaleString(ellipse,MaxTextExtent,"ellipse %g,%g,%g,%g,"
5509 "0.0,360.0",image->columns/2.0,image->rows/2.0,image->columns/2.0-x,
5510 image->rows/2.0-y);
cristy3ed852e2009-09-05 21:47:34 +00005511 draw_info->primitive=AcquireString(ellipse);
cristy018f07f2011-09-04 21:15:19 +00005512 (void) DrawImage(oval_image,draw_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00005513 draw_info=DestroyDrawInfo(draw_info);
cristyaa2c16c2012-03-25 22:21:35 +00005514 blur_image=BlurImage(oval_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +00005515 oval_image=DestroyImage(oval_image);
5516 if (blur_image == (Image *) NULL)
5517 {
5518 canvas_image=DestroyImage(canvas_image);
5519 return((Image *) NULL);
5520 }
5521 blur_image->matte=MagickFalse;
cristy39172402012-03-30 13:04:39 +00005522 (void) CompositeImage(canvas_image,blur_image,IntensityCompositeOp,MagickTrue,
5523 0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00005524 blur_image=DestroyImage(blur_image);
5525 vignette_image=MergeImageLayers(canvas_image,FlattenLayer,exception);
5526 canvas_image=DestroyImage(canvas_image);
5527 return(vignette_image);
5528}
5529
5530/*
5531%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5532% %
5533% %
5534% %
5535% W a v e I m a g e %
5536% %
5537% %
5538% %
5539%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5540%
5541% WaveImage() creates a "ripple" effect in the image by shifting the pixels
cristycee97112010-05-28 00:44:52 +00005542% vertically along a sine wave whose amplitude and wavelength is specified
cristy3ed852e2009-09-05 21:47:34 +00005543% by the given parameters.
5544%
5545% The format of the WaveImage method is:
5546%
5547% Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005548% const double wave_length,const PixelInterpolateMethod method,
5549% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005550%
5551% A description of each parameter follows:
5552%
5553% o image: the image.
5554%
5555% o amplitude, wave_length: Define the amplitude and wave length of the
5556% sine wave.
5557%
cristy5c4e2582011-09-11 19:21:03 +00005558% o interpolate: the pixel interpolation method.
5559%
cristy3ed852e2009-09-05 21:47:34 +00005560% o exception: return any errors or warnings in this structure.
5561%
5562*/
5563MagickExport Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005564 const double wave_length,const PixelInterpolateMethod method,
5565 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005566{
5567#define WaveImageTag "Wave/Image"
5568
cristyfa112112010-01-04 17:48:07 +00005569 CacheView
cristyd76c51e2011-03-26 00:21:26 +00005570 *image_view,
cristyfa112112010-01-04 17:48:07 +00005571 *wave_view;
5572
cristy3ed852e2009-09-05 21:47:34 +00005573 Image
5574 *wave_image;
5575
cristy3ed852e2009-09-05 21:47:34 +00005576 MagickBooleanType
5577 status;
5578
cristybb503372010-05-27 20:51:26 +00005579 MagickOffsetType
5580 progress;
5581
cristy3ed852e2009-09-05 21:47:34 +00005582 MagickRealType
5583 *sine_map;
5584
cristybb503372010-05-27 20:51:26 +00005585 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005586 i;
5587
cristybb503372010-05-27 20:51:26 +00005588 ssize_t
5589 y;
5590
cristy3ed852e2009-09-05 21:47:34 +00005591 /*
5592 Initialize wave image attributes.
5593 */
5594 assert(image != (Image *) NULL);
5595 assert(image->signature == MagickSignature);
5596 if (image->debug != MagickFalse)
5597 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5598 assert(exception != (ExceptionInfo *) NULL);
5599 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00005600 wave_image=CloneImage(image,image->columns,(size_t) (image->rows+2.0*
cristy3ed852e2009-09-05 21:47:34 +00005601 fabs(amplitude)),MagickTrue,exception);
5602 if (wave_image == (Image *) NULL)
5603 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005604 if (SetImageStorageClass(wave_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005605 {
cristy3ed852e2009-09-05 21:47:34 +00005606 wave_image=DestroyImage(wave_image);
5607 return((Image *) NULL);
5608 }
cristy4c08aed2011-07-01 19:47:50 +00005609 if (wave_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00005610 wave_image->matte=MagickTrue;
5611 /*
5612 Allocate sine map.
5613 */
5614 sine_map=(MagickRealType *) AcquireQuantumMemory((size_t) wave_image->columns,
5615 sizeof(*sine_map));
5616 if (sine_map == (MagickRealType *) NULL)
5617 {
5618 wave_image=DestroyImage(wave_image);
5619 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
5620 }
cristybb503372010-05-27 20:51:26 +00005621 for (i=0; i < (ssize_t) wave_image->columns; i++)
cristy4205a3c2010-09-12 20:19:59 +00005622 sine_map[i]=fabs(amplitude)+amplitude*sin((double) ((2.0*MagickPI*i)/
5623 wave_length));
cristy3ed852e2009-09-05 21:47:34 +00005624 /*
5625 Wave image.
5626 */
5627 status=MagickTrue;
5628 progress=0;
cristydb070952012-04-20 14:33:00 +00005629 image_view=AcquireVirtualCacheView(image,exception);
5630 wave_view=AcquireAuthenticCacheView(wave_image,exception);
cristyd76c51e2011-03-26 00:21:26 +00005631 (void) SetCacheViewVirtualPixelMethod(image_view,
5632 BackgroundVirtualPixelMethod);
cristyb5d5f722009-11-04 03:03:49 +00005633#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00005634 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristyddacdd12012-05-07 23:08:14 +00005635 dynamic_number_threads(image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00005636#endif
cristybb503372010-05-27 20:51:26 +00005637 for (y=0; y < (ssize_t) wave_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005638 {
cristy4c08aed2011-07-01 19:47:50 +00005639 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005640 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005641
cristye97bb922011-04-03 01:36:52 +00005642 register ssize_t
5643 x;
5644
cristy3ed852e2009-09-05 21:47:34 +00005645 if (status == MagickFalse)
5646 continue;
5647 q=QueueCacheViewAuthenticPixels(wave_view,0,y,wave_image->columns,1,
5648 exception);
cristyacd2ed22011-08-30 01:44:23 +00005649 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005650 {
5651 status=MagickFalse;
5652 continue;
5653 }
cristybb503372010-05-27 20:51:26 +00005654 for (x=0; x < (ssize_t) wave_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005655 {
cristy5c4e2582011-09-11 19:21:03 +00005656 status=InterpolatePixelChannels(image,image_view,wave_image,method,
5657 (double) x,(double) (y-sine_map[x]),q,exception);
cristyed231572011-07-14 02:18:59 +00005658 q+=GetPixelChannels(wave_image);
cristy3ed852e2009-09-05 21:47:34 +00005659 }
5660 if (SyncCacheViewAuthenticPixels(wave_view,exception) == MagickFalse)
5661 status=MagickFalse;
5662 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5663 {
5664 MagickBooleanType
5665 proceed;
5666
cristyb5d5f722009-11-04 03:03:49 +00005667#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005668 #pragma omp critical (MagickCore_WaveImage)
cristy3ed852e2009-09-05 21:47:34 +00005669#endif
5670 proceed=SetImageProgress(image,WaveImageTag,progress++,image->rows);
5671 if (proceed == MagickFalse)
5672 status=MagickFalse;
5673 }
5674 }
5675 wave_view=DestroyCacheView(wave_view);
cristyd76c51e2011-03-26 00:21:26 +00005676 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00005677 sine_map=(MagickRealType *) RelinquishMagickMemory(sine_map);
5678 if (status == MagickFalse)
5679 wave_image=DestroyImage(wave_image);
5680 return(wave_image);
5681}