blob: 4037b2e608e507c30b43c5f120f8754c8c7f6a7c [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) \
cristyac245f82012-05-05 17:13:57 +0000315 if (((image->rows*image->columns) > 8192) && (key == ~0UL)) \
316 num_threads(GetMagickResourceLimit(ThreadResource))
cristy3ed852e2009-09-05 21:47:34 +0000317#endif
cristybb503372010-05-27 20:51:26 +0000318 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000319 {
cristy5c9e6f22010-09-17 17:31:01 +0000320 const int
321 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +0000322
cristy3ed852e2009-09-05 21:47:34 +0000323 MagickBooleanType
324 sync;
325
cristy4c08aed2011-07-01 19:47:50 +0000326 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000327 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000328
cristybb503372010-05-27 20:51:26 +0000329 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000330 x;
331
cristy4c08aed2011-07-01 19:47:50 +0000332 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000333 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000334
335 if (status == MagickFalse)
336 continue;
337 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +0000338 q=QueueCacheViewAuthenticPixels(noise_view,0,y,noise_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +0000339 exception);
cristy4c08aed2011-07-01 19:47:50 +0000340 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000341 {
342 status=MagickFalse;
343 continue;
344 }
cristybb503372010-05-27 20:51:26 +0000345 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000346 {
cristy850b3072011-10-08 01:38:05 +0000347 register ssize_t
348 i;
349
cristy177e41c2012-04-15 15:08:25 +0000350 if (GetPixelMask(image,p) != 0)
351 {
352 p+=GetPixelChannels(image);
353 q+=GetPixelChannels(noise_image);
354 continue;
355 }
cristy850b3072011-10-08 01:38:05 +0000356 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
357 {
358 PixelChannel
359 channel;
360
361 PixelTrait
362 noise_traits,
363 traits;
364
cristye2a912b2011-12-05 20:02:07 +0000365 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +0000366 traits=GetPixelChannelMapTraits(image,channel);
cristy850b3072011-10-08 01:38:05 +0000367 noise_traits=GetPixelChannelMapTraits(noise_image,channel);
368 if ((traits == UndefinedPixelTrait) ||
369 (noise_traits == UndefinedPixelTrait))
370 continue;
cristy177e41c2012-04-15 15:08:25 +0000371 if ((noise_traits & CopyPixelTrait) != 0)
cristyb2145892011-10-10 00:55:32 +0000372 {
373 SetPixelChannel(noise_image,channel,p[i],q);
374 continue;
375 }
cristy850b3072011-10-08 01:38:05 +0000376 SetPixelChannel(noise_image,channel,ClampToQuantum(
377 GenerateDifferentialNoise(random_info[id],p[i],noise_type,attenuate)),
378 q);
379 }
cristyed231572011-07-14 02:18:59 +0000380 p+=GetPixelChannels(image);
381 q+=GetPixelChannels(noise_image);
cristy3ed852e2009-09-05 21:47:34 +0000382 }
383 sync=SyncCacheViewAuthenticPixels(noise_view,exception);
384 if (sync == MagickFalse)
385 status=MagickFalse;
386 if (image->progress_monitor != (MagickProgressMonitor) NULL)
387 {
388 MagickBooleanType
389 proceed;
390
cristyb5d5f722009-11-04 03:03:49 +0000391#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy319a1e72010-02-21 15:13:11 +0000392 #pragma omp critical (MagickCore_AddNoiseImage)
cristy3ed852e2009-09-05 21:47:34 +0000393#endif
394 proceed=SetImageProgress(image,AddNoiseImageTag,progress++,
395 image->rows);
396 if (proceed == MagickFalse)
397 status=MagickFalse;
398 }
399 }
400 noise_view=DestroyCacheView(noise_view);
401 image_view=DestroyCacheView(image_view);
402 random_info=DestroyRandomInfoThreadSet(random_info);
403 if (status == MagickFalse)
404 noise_image=DestroyImage(noise_image);
405 return(noise_image);
406}
407
408/*
409%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
410% %
411% %
412% %
413% B l u e S h i f t I m a g e %
414% %
415% %
416% %
417%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
418%
419% BlueShiftImage() mutes the colors of the image to simulate a scene at
420% nighttime in the moonlight.
421%
422% The format of the BlueShiftImage method is:
423%
424% Image *BlueShiftImage(const Image *image,const double factor,
425% ExceptionInfo *exception)
426%
427% A description of each parameter follows:
428%
429% o image: the image.
430%
431% o factor: the shift factor.
432%
433% o exception: return any errors or warnings in this structure.
434%
435*/
436MagickExport Image *BlueShiftImage(const Image *image,const double factor,
437 ExceptionInfo *exception)
438{
439#define BlueShiftImageTag "BlueShift/Image"
440
cristyc4c8d132010-01-07 01:58:38 +0000441 CacheView
442 *image_view,
443 *shift_view;
444
cristy3ed852e2009-09-05 21:47:34 +0000445 Image
446 *shift_image;
447
cristy3ed852e2009-09-05 21:47:34 +0000448 MagickBooleanType
449 status;
450
cristybb503372010-05-27 20:51:26 +0000451 MagickOffsetType
452 progress;
453
454 ssize_t
455 y;
456
cristy3ed852e2009-09-05 21:47:34 +0000457 /*
458 Allocate blue shift image.
459 */
460 assert(image != (const Image *) NULL);
461 assert(image->signature == MagickSignature);
462 if (image->debug != MagickFalse)
463 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
464 assert(exception != (ExceptionInfo *) NULL);
465 assert(exception->signature == MagickSignature);
cristya6d7a9b2012-01-18 20:04:48 +0000466 shift_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000467 if (shift_image == (Image *) NULL)
468 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000469 if (SetImageStorageClass(shift_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000470 {
cristy3ed852e2009-09-05 21:47:34 +0000471 shift_image=DestroyImage(shift_image);
472 return((Image *) NULL);
473 }
474 /*
475 Blue-shift DirectClass image.
476 */
477 status=MagickTrue;
478 progress=0;
cristydb070952012-04-20 14:33:00 +0000479 image_view=AcquireVirtualCacheView(image,exception);
480 shift_view=AcquireAuthenticCacheView(shift_image,exception);
cristy319a1e72010-02-21 15:13:11 +0000481#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000482 #pragma omp parallel for schedule(static,4) shared(progress,status) \
483 if ((image->rows*image->columns) > 8192) \
484 num_threads(GetMagickResourceLimit(ThreadResource))
cristy3ed852e2009-09-05 21:47:34 +0000485#endif
cristybb503372010-05-27 20:51:26 +0000486 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000487 {
488 MagickBooleanType
489 sync;
490
cristy4c08aed2011-07-01 19:47:50 +0000491 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000492 pixel;
493
494 Quantum
495 quantum;
496
cristy4c08aed2011-07-01 19:47:50 +0000497 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000498 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000499
cristybb503372010-05-27 20:51:26 +0000500 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000501 x;
502
cristy4c08aed2011-07-01 19:47:50 +0000503 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000504 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000505
506 if (status == MagickFalse)
507 continue;
508 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
509 q=QueueCacheViewAuthenticPixels(shift_view,0,y,shift_image->columns,1,
510 exception);
cristy4c08aed2011-07-01 19:47:50 +0000511 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000512 {
513 status=MagickFalse;
514 continue;
515 }
cristybb503372010-05-27 20:51:26 +0000516 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000517 {
cristy4c08aed2011-07-01 19:47:50 +0000518 quantum=GetPixelRed(image,p);
519 if (GetPixelGreen(image,p) < quantum)
520 quantum=GetPixelGreen(image,p);
521 if (GetPixelBlue(image,p) < quantum)
522 quantum=GetPixelBlue(image,p);
523 pixel.red=0.5*(GetPixelRed(image,p)+factor*quantum);
524 pixel.green=0.5*(GetPixelGreen(image,p)+factor*quantum);
525 pixel.blue=0.5*(GetPixelBlue(image,p)+factor*quantum);
526 quantum=GetPixelRed(image,p);
527 if (GetPixelGreen(image,p) > quantum)
528 quantum=GetPixelGreen(image,p);
529 if (GetPixelBlue(image,p) > quantum)
530 quantum=GetPixelBlue(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000531 pixel.red=0.5*(pixel.red+factor*quantum);
532 pixel.green=0.5*(pixel.green+factor*quantum);
533 pixel.blue=0.5*(pixel.blue+factor*quantum);
cristy4c08aed2011-07-01 19:47:50 +0000534 SetPixelRed(shift_image,ClampToQuantum(pixel.red),q);
535 SetPixelGreen(shift_image,ClampToQuantum(pixel.green),q);
536 SetPixelBlue(shift_image,ClampToQuantum(pixel.blue),q);
cristyed231572011-07-14 02:18:59 +0000537 p+=GetPixelChannels(image);
538 q+=GetPixelChannels(shift_image);
cristy3ed852e2009-09-05 21:47:34 +0000539 }
540 sync=SyncCacheViewAuthenticPixels(shift_view,exception);
541 if (sync == MagickFalse)
542 status=MagickFalse;
543 if (image->progress_monitor != (MagickProgressMonitor) NULL)
544 {
545 MagickBooleanType
546 proceed;
547
cristy319a1e72010-02-21 15:13:11 +0000548#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +0000549 #pragma omp critical (MagickCore_BlueShiftImage)
cristy3ed852e2009-09-05 21:47:34 +0000550#endif
551 proceed=SetImageProgress(image,BlueShiftImageTag,progress++,
552 image->rows);
553 if (proceed == MagickFalse)
554 status=MagickFalse;
555 }
556 }
557 image_view=DestroyCacheView(image_view);
558 shift_view=DestroyCacheView(shift_view);
559 if (status == MagickFalse)
560 shift_image=DestroyImage(shift_image);
561 return(shift_image);
562}
563
564/*
565%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
566% %
567% %
568% %
569% C h a r c o a l I m a g e %
570% %
571% %
572% %
573%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
574%
575% CharcoalImage() creates a new image that is a copy of an existing one with
576% the edge highlighted. It allocates the memory necessary for the new Image
577% structure and returns a pointer to the new image.
578%
579% The format of the CharcoalImage method is:
580%
581% Image *CharcoalImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +0000582% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000583%
584% A description of each parameter follows:
585%
586% o image: the image.
587%
588% o radius: the radius of the pixel neighborhood.
589%
590% o sigma: the standard deviation of the Gaussian, in pixels.
591%
592% o exception: return any errors or warnings in this structure.
593%
594*/
595MagickExport Image *CharcoalImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +0000596 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000597{
598 Image
599 *charcoal_image,
600 *clone_image,
601 *edge_image;
602
603 assert(image != (Image *) NULL);
604 assert(image->signature == MagickSignature);
605 if (image->debug != MagickFalse)
606 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
607 assert(exception != (ExceptionInfo *) NULL);
608 assert(exception->signature == MagickSignature);
609 clone_image=CloneImage(image,0,0,MagickTrue,exception);
610 if (clone_image == (Image *) NULL)
611 return((Image *) NULL);
cristy018f07f2011-09-04 21:15:19 +0000612 (void) SetImageType(clone_image,GrayscaleType,exception);
cristy8ae632d2011-09-05 17:29:53 +0000613 edge_image=EdgeImage(clone_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +0000614 clone_image=DestroyImage(clone_image);
615 if (edge_image == (Image *) NULL)
616 return((Image *) NULL);
cristyaa2c16c2012-03-25 22:21:35 +0000617 charcoal_image=BlurImage(edge_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +0000618 edge_image=DestroyImage(edge_image);
619 if (charcoal_image == (Image *) NULL)
620 return((Image *) NULL);
cristye23ec9d2011-08-16 18:15:40 +0000621 (void) NormalizeImage(charcoal_image,exception);
cristyb3e7c6c2011-07-24 01:43:55 +0000622 (void) NegateImage(charcoal_image,MagickFalse,exception);
cristy018f07f2011-09-04 21:15:19 +0000623 (void) SetImageType(charcoal_image,GrayscaleType,exception);
cristy3ed852e2009-09-05 21:47:34 +0000624 return(charcoal_image);
625}
626
627/*
628%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
629% %
630% %
631% %
632% C o l o r i z e I m a g e %
633% %
634% %
635% %
636%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
637%
638% ColorizeImage() blends the fill color with each pixel in the image.
639% A percentage blend is specified with opacity. Control the application
640% of different color components by specifying a different percentage for
641% each component (e.g. 90/100/10 is 90% red, 100% green, and 10% blue).
642%
643% The format of the ColorizeImage method is:
644%
cristyc7e6ff62011-10-03 13:46:11 +0000645% Image *ColorizeImage(const Image *image,const char *blend,
646% const PixelInfo *colorize,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000647%
648% A description of each parameter follows:
649%
650% o image: the image.
651%
cristyc7e6ff62011-10-03 13:46:11 +0000652% o blend: A character string indicating the level of blending as a
cristy3ed852e2009-09-05 21:47:34 +0000653% percentage.
654%
655% o colorize: A color value.
656%
657% o exception: return any errors or warnings in this structure.
658%
659*/
cristyc7e6ff62011-10-03 13:46:11 +0000660MagickExport Image *ColorizeImage(const Image *image,const char *blend,
661 const PixelInfo *colorize,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000662{
663#define ColorizeImageTag "Colorize/Image"
cristy20c3aed2012-04-10 01:06:21 +0000664#define Colorize(pixel,blend_percentage,colorize) \
665 (pixel)=((pixel)*(100.0-(blend_percentage))+(colorize)*(blend_percentage))/100.0;
cristy3ed852e2009-09-05 21:47:34 +0000666
cristyc4c8d132010-01-07 01:58:38 +0000667 CacheView
668 *colorize_view,
669 *image_view;
670
cristy3ed852e2009-09-05 21:47:34 +0000671 GeometryInfo
672 geometry_info;
673
674 Image
675 *colorize_image;
676
cristy3ed852e2009-09-05 21:47:34 +0000677 MagickBooleanType
678 status;
679
cristybb503372010-05-27 20:51:26 +0000680 MagickOffsetType
681 progress;
682
cristy3ed852e2009-09-05 21:47:34 +0000683 MagickStatusType
684 flags;
685
cristyc7e6ff62011-10-03 13:46:11 +0000686 PixelInfo
cristy20c3aed2012-04-10 01:06:21 +0000687 blend_percentage;
cristyc7e6ff62011-10-03 13:46:11 +0000688
cristybb503372010-05-27 20:51:26 +0000689 ssize_t
690 y;
691
cristy3ed852e2009-09-05 21:47:34 +0000692 /*
693 Allocate colorized image.
694 */
695 assert(image != (const Image *) NULL);
696 assert(image->signature == MagickSignature);
697 if (image->debug != MagickFalse)
698 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
699 assert(exception != (ExceptionInfo *) NULL);
700 assert(exception->signature == MagickSignature);
cristy768165d2012-04-09 15:01:35 +0000701 colorize_image=CloneImage(image,image->columns,image->rows,MagickTrue,
702 exception);
703 if (colorize_image == (Image *) NULL)
704 return((Image *) NULL);
705 if (SetImageStorageClass(colorize_image,DirectClass,exception) == MagickFalse)
706 {
707 colorize_image=DestroyImage(colorize_image);
708 return((Image *) NULL);
709 }
cristy9b7a4fc2012-04-08 22:26:56 +0000710 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
cristy20c3aed2012-04-10 01:06:21 +0000711 (IsPixelInfoGray(colorize) != MagickFalse))
cristy52f2e1e2012-04-10 00:51:19 +0000712 (void) SetImageColorspace(colorize_image,sRGBColorspace,exception);
cristy20c3aed2012-04-10 01:06:21 +0000713 if ((colorize_image->matte == MagickFalse) &&
714 (colorize->matte != MagickFalse))
cristy768165d2012-04-09 15:01:35 +0000715 (void) SetImageAlpha(colorize_image,OpaqueAlpha,exception);
716 if (blend == (const char *) NULL)
717 return(colorize_image);
cristy20c3aed2012-04-10 01:06:21 +0000718 GetPixelInfo(image,&blend_percentage);
719 flags=ParseGeometry(blend,&geometry_info);
720 blend_percentage.red=geometry_info.rho;
721 blend_percentage.green=geometry_info.rho;
722 blend_percentage.blue=geometry_info.rho;
723 blend_percentage.black=geometry_info.rho;
cristy3ee7de52012-04-14 23:40:47 +0000724 blend_percentage.alpha=geometry_info.rho;
cristy20c3aed2012-04-10 01:06:21 +0000725 if ((flags & SigmaValue) != 0)
726 blend_percentage.green=geometry_info.sigma;
727 if ((flags & XiValue) != 0)
728 blend_percentage.blue=geometry_info.xi;
729 if ((flags & PsiValue) != 0)
730 blend_percentage.alpha=geometry_info.psi;
731 if (blend_percentage.colorspace == CMYKColorspace)
732 {
733 if ((flags & PsiValue) != 0)
734 blend_percentage.black=geometry_info.psi;
735 if ((flags & ChiValue) != 0)
736 blend_percentage.alpha=geometry_info.chi;
737 }
cristy3ed852e2009-09-05 21:47:34 +0000738 /*
739 Colorize DirectClass image.
740 */
741 status=MagickTrue;
742 progress=0;
cristydb070952012-04-20 14:33:00 +0000743 image_view=AcquireVirtualCacheView(image,exception);
744 colorize_view=AcquireAuthenticCacheView(colorize_image,exception);
cristy319a1e72010-02-21 15:13:11 +0000745#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000746 #pragma omp parallel for schedule(static,4) shared(progress,status) \
747 if ((image->rows*image->columns) > 8192) \
748 num_threads(GetMagickResourceLimit(ThreadResource))
cristy3ed852e2009-09-05 21:47:34 +0000749#endif
cristybb503372010-05-27 20:51:26 +0000750 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000751 {
752 MagickBooleanType
753 sync;
754
cristyf9bf43e2012-04-07 18:13:07 +0000755 PixelInfo
756 pixel;
757
cristy4c08aed2011-07-01 19:47:50 +0000758 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000759 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000760
cristybb503372010-05-27 20:51:26 +0000761 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000762 x;
763
cristy4c08aed2011-07-01 19:47:50 +0000764 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000765 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000766
767 if (status == MagickFalse)
768 continue;
769 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
770 q=QueueCacheViewAuthenticPixels(colorize_view,0,y,colorize_image->columns,1,
771 exception);
cristy4c08aed2011-07-01 19:47:50 +0000772 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000773 {
774 status=MagickFalse;
775 continue;
776 }
cristy66d3e1f2012-04-14 23:21:39 +0000777 GetPixelInfo(colorize_image,&pixel);
cristybb503372010-05-27 20:51:26 +0000778 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000779 {
cristyd6803382012-04-10 01:41:25 +0000780 if (GetPixelMask(colorize_image,q) != 0)
781 {
782 p+=GetPixelChannels(image);
783 q+=GetPixelChannels(colorize_image);
784 continue;
785 }
cristyf9bf43e2012-04-07 18:13:07 +0000786 GetPixelInfoPixel(image,p,&pixel);
cristy20c3aed2012-04-10 01:06:21 +0000787 Colorize(pixel.red,blend_percentage.red,colorize->red);
788 Colorize(pixel.green,blend_percentage.green,colorize->green);
789 Colorize(pixel.blue,blend_percentage.blue,colorize->blue);
790 Colorize(pixel.black,blend_percentage.black,colorize->black);
791 Colorize(pixel.alpha,blend_percentage.alpha,colorize->alpha);
cristyf9bf43e2012-04-07 18:13:07 +0000792 SetPixelInfoPixel(colorize_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +0000793 p+=GetPixelChannels(image);
794 q+=GetPixelChannels(colorize_image);
cristy3ed852e2009-09-05 21:47:34 +0000795 }
796 sync=SyncCacheViewAuthenticPixels(colorize_view,exception);
797 if (sync == MagickFalse)
798 status=MagickFalse;
799 if (image->progress_monitor != (MagickProgressMonitor) NULL)
800 {
801 MagickBooleanType
802 proceed;
803
cristy319a1e72010-02-21 15:13:11 +0000804#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +0000805 #pragma omp critical (MagickCore_ColorizeImage)
cristy3ed852e2009-09-05 21:47:34 +0000806#endif
807 proceed=SetImageProgress(image,ColorizeImageTag,progress++,image->rows);
808 if (proceed == MagickFalse)
809 status=MagickFalse;
810 }
811 }
812 image_view=DestroyCacheView(image_view);
813 colorize_view=DestroyCacheView(colorize_view);
814 if (status == MagickFalse)
815 colorize_image=DestroyImage(colorize_image);
816 return(colorize_image);
817}
818
819/*
820%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
821% %
822% %
823% %
cristye6365592010-04-02 17:31:23 +0000824% C o l o r M a t r i x I m a g e %
825% %
826% %
827% %
828%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
829%
830% ColorMatrixImage() applies color transformation to an image. This method
831% permits saturation changes, hue rotation, luminance to alpha, and various
832% other effects. Although variable-sized transformation matrices can be used,
833% typically one uses a 5x5 matrix for an RGBA image and a 6x6 for CMYKA
834% (or RGBA with offsets). The matrix is similar to those used by Adobe Flash
835% except offsets are in column 6 rather than 5 (in support of CMYKA images)
836% and offsets are normalized (divide Flash offset by 255).
837%
838% The format of the ColorMatrixImage method is:
839%
840% Image *ColorMatrixImage(const Image *image,
841% const KernelInfo *color_matrix,ExceptionInfo *exception)
842%
843% A description of each parameter follows:
844%
845% o image: the image.
846%
847% o color_matrix: the color matrix.
848%
849% o exception: return any errors or warnings in this structure.
850%
851*/
anthonyfd706f92012-01-19 04:22:02 +0000852/* FUTURE: modify to make use of a MagickMatrix Mutliply function
853 That should be provided in "matrix.c"
854 (ASIDE: actually distorts should do this too but currently doesn't)
855*/
856
cristye6365592010-04-02 17:31:23 +0000857MagickExport Image *ColorMatrixImage(const Image *image,
858 const KernelInfo *color_matrix,ExceptionInfo *exception)
859{
860#define ColorMatrixImageTag "ColorMatrix/Image"
861
862 CacheView
863 *color_view,
864 *image_view;
865
866 double
867 ColorMatrix[6][6] =
868 {
869 { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
870 { 0.0, 1.0, 0.0, 0.0, 0.0, 0.0 },
871 { 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 },
872 { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0 },
873 { 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 },
874 { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 }
875 };
876
877 Image
878 *color_image;
879
cristye6365592010-04-02 17:31:23 +0000880 MagickBooleanType
881 status;
882
cristybb503372010-05-27 20:51:26 +0000883 MagickOffsetType
884 progress;
885
886 register ssize_t
cristye6365592010-04-02 17:31:23 +0000887 i;
888
cristybb503372010-05-27 20:51:26 +0000889 ssize_t
890 u,
891 v,
892 y;
893
cristye6365592010-04-02 17:31:23 +0000894 /*
anthonyfd706f92012-01-19 04:22:02 +0000895 Map given color_matrix, into a 6x6 matrix RGBKA and a constant
cristye6365592010-04-02 17:31:23 +0000896 */
897 assert(image != (Image *) NULL);
898 assert(image->signature == MagickSignature);
899 if (image->debug != MagickFalse)
900 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
901 assert(exception != (ExceptionInfo *) NULL);
902 assert(exception->signature == MagickSignature);
903 i=0;
cristybb503372010-05-27 20:51:26 +0000904 for (v=0; v < (ssize_t) color_matrix->height; v++)
905 for (u=0; u < (ssize_t) color_matrix->width; u++)
cristye6365592010-04-02 17:31:23 +0000906 {
907 if ((v < 6) && (u < 6))
908 ColorMatrix[v][u]=color_matrix->values[i];
909 i++;
910 }
911 /*
912 Initialize color image.
913 */
cristy12550e62010-06-07 12:46:40 +0000914 color_image=CloneImage(image,0,0,MagickTrue,exception);
cristye6365592010-04-02 17:31:23 +0000915 if (color_image == (Image *) NULL)
916 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000917 if (SetImageStorageClass(color_image,DirectClass,exception) == MagickFalse)
cristye6365592010-04-02 17:31:23 +0000918 {
cristye6365592010-04-02 17:31:23 +0000919 color_image=DestroyImage(color_image);
920 return((Image *) NULL);
921 }
922 if (image->debug != MagickFalse)
923 {
924 char
925 format[MaxTextExtent],
926 *message;
927
928 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
929 " ColorMatrix image with color matrix:");
930 message=AcquireString("");
931 for (v=0; v < 6; v++)
932 {
933 *message='\0';
cristyb51dff52011-05-19 16:55:47 +0000934 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
cristye6365592010-04-02 17:31:23 +0000935 (void) ConcatenateString(&message,format);
936 for (u=0; u < 6; u++)
937 {
cristyb51dff52011-05-19 16:55:47 +0000938 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",
cristye6365592010-04-02 17:31:23 +0000939 ColorMatrix[v][u]);
940 (void) ConcatenateString(&message,format);
941 }
942 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
943 }
944 message=DestroyString(message);
945 }
946 /*
anthonyfd706f92012-01-19 04:22:02 +0000947 Apply the ColorMatrix to image.
cristye6365592010-04-02 17:31:23 +0000948 */
949 status=MagickTrue;
950 progress=0;
cristydb070952012-04-20 14:33:00 +0000951 image_view=AcquireVirtualCacheView(image,exception);
952 color_view=AcquireAuthenticCacheView(color_image,exception);
cristye6365592010-04-02 17:31:23 +0000953#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000954 #pragma omp parallel for schedule(static,4) shared(progress,status) \
955 if ((image->rows*image->columns) > 8192) \
956 num_threads(GetMagickResourceLimit(ThreadResource))
cristye6365592010-04-02 17:31:23 +0000957#endif
cristybb503372010-05-27 20:51:26 +0000958 for (y=0; y < (ssize_t) image->rows; y++)
cristye6365592010-04-02 17:31:23 +0000959 {
cristyfcc25d92012-02-19 23:06:48 +0000960 PixelInfo
cristye6365592010-04-02 17:31:23 +0000961 pixel;
962
cristy4c08aed2011-07-01 19:47:50 +0000963 register const Quantum
cristye6365592010-04-02 17:31:23 +0000964 *restrict p;
965
cristy4c08aed2011-07-01 19:47:50 +0000966 register Quantum
967 *restrict q;
968
cristybb503372010-05-27 20:51:26 +0000969 register ssize_t
cristye6365592010-04-02 17:31:23 +0000970 x;
971
cristye6365592010-04-02 17:31:23 +0000972 if (status == MagickFalse)
973 continue;
974 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
975 q=GetCacheViewAuthenticPixels(color_view,0,y,color_image->columns,1,
976 exception);
cristy4c08aed2011-07-01 19:47:50 +0000977 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristye6365592010-04-02 17:31:23 +0000978 {
979 status=MagickFalse;
980 continue;
981 }
cristyfcc25d92012-02-19 23:06:48 +0000982 GetPixelInfo(image,&pixel);
cristybb503372010-05-27 20:51:26 +0000983 for (x=0; x < (ssize_t) image->columns; x++)
cristye6365592010-04-02 17:31:23 +0000984 {
cristybb503372010-05-27 20:51:26 +0000985 register ssize_t
cristye6365592010-04-02 17:31:23 +0000986 v;
987
cristybb503372010-05-27 20:51:26 +0000988 size_t
cristye6365592010-04-02 17:31:23 +0000989 height;
990
cristyfcc25d92012-02-19 23:06:48 +0000991 GetPixelInfoPixel(image,p,&pixel);
cristye6365592010-04-02 17:31:23 +0000992 height=color_matrix->height > 6 ? 6UL : color_matrix->height;
cristybb503372010-05-27 20:51:26 +0000993 for (v=0; v < (ssize_t) height; v++)
cristye6365592010-04-02 17:31:23 +0000994 {
cristyfcc25d92012-02-19 23:06:48 +0000995 MagickRealType
996 sum;
997
998 sum=ColorMatrix[v][0]*GetPixelRed(image,p)+ColorMatrix[v][1]*
cristy4c08aed2011-07-01 19:47:50 +0000999 GetPixelGreen(image,p)+ColorMatrix[v][2]*GetPixelBlue(image,p);
cristye6365592010-04-02 17:31:23 +00001000 if (image->colorspace == CMYKColorspace)
cristyfcc25d92012-02-19 23:06:48 +00001001 sum+=ColorMatrix[v][3]*GetPixelBlack(image,p);
cristy4c08aed2011-07-01 19:47:50 +00001002 if (image->matte != MagickFalse)
cristyfcc25d92012-02-19 23:06:48 +00001003 sum+=ColorMatrix[v][4]*GetPixelAlpha(image,p);
1004 sum+=QuantumRange*ColorMatrix[v][5];
cristye6365592010-04-02 17:31:23 +00001005 switch (v)
1006 {
cristyfcc25d92012-02-19 23:06:48 +00001007 case 0: pixel.red=sum; break;
1008 case 1: pixel.green=sum; break;
1009 case 2: pixel.blue=sum; break;
1010 case 3: pixel.black=sum; break;
1011 case 4: pixel.alpha=sum; break;
1012 default: break;
cristye6365592010-04-02 17:31:23 +00001013 }
1014 }
cristyfcc25d92012-02-19 23:06:48 +00001015 SetPixelInfoPixel(color_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +00001016 p+=GetPixelChannels(image);
1017 q+=GetPixelChannels(color_image);
cristye6365592010-04-02 17:31:23 +00001018 }
1019 if (SyncCacheViewAuthenticPixels(color_view,exception) == MagickFalse)
1020 status=MagickFalse;
1021 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1022 {
1023 MagickBooleanType
1024 proceed;
1025
1026#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00001027 #pragma omp critical (MagickCore_ColorMatrixImage)
cristye6365592010-04-02 17:31:23 +00001028#endif
1029 proceed=SetImageProgress(image,ColorMatrixImageTag,progress++,
1030 image->rows);
1031 if (proceed == MagickFalse)
1032 status=MagickFalse;
1033 }
1034 }
1035 color_view=DestroyCacheView(color_view);
1036 image_view=DestroyCacheView(image_view);
1037 if (status == MagickFalse)
1038 color_image=DestroyImage(color_image);
1039 return(color_image);
1040}
1041
1042/*
1043%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1044% %
1045% %
1046% %
cristy3ed852e2009-09-05 21:47:34 +00001047+ D e s t r o y F x I n f o %
1048% %
1049% %
1050% %
1051%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1052%
1053% DestroyFxInfo() deallocates memory associated with an FxInfo structure.
1054%
1055% The format of the DestroyFxInfo method is:
1056%
1057% ImageInfo *DestroyFxInfo(ImageInfo *fx_info)
1058%
1059% A description of each parameter follows:
1060%
1061% o fx_info: the fx info.
1062%
1063*/
cristy7832dc22011-09-05 01:21:53 +00001064MagickPrivate FxInfo *DestroyFxInfo(FxInfo *fx_info)
cristy3ed852e2009-09-05 21:47:34 +00001065{
cristybb503372010-05-27 20:51:26 +00001066 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001067 i;
1068
1069 fx_info->exception=DestroyExceptionInfo(fx_info->exception);
1070 fx_info->expression=DestroyString(fx_info->expression);
1071 fx_info->symbols=DestroySplayTree(fx_info->symbols);
1072 fx_info->colors=DestroySplayTree(fx_info->colors);
cristy0ea377f2011-03-24 00:54:19 +00001073 for (i=(ssize_t) GetImageListLength(fx_info->images)-1; i >= 0; i--)
cristyd76c51e2011-03-26 00:21:26 +00001074 fx_info->view[i]=DestroyCacheView(fx_info->view[i]);
1075 fx_info->view=(CacheView **) RelinquishMagickMemory(fx_info->view);
cristy3ed852e2009-09-05 21:47:34 +00001076 fx_info->random_info=DestroyRandomInfo(fx_info->random_info);
1077 fx_info=(FxInfo *) RelinquishMagickMemory(fx_info);
1078 return(fx_info);
1079}
1080
1081/*
1082%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1083% %
1084% %
1085% %
cristy3ed852e2009-09-05 21:47:34 +00001086+ 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 %
1087% %
1088% %
1089% %
1090%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1091%
1092% FxEvaluateChannelExpression() evaluates an expression and returns the
1093% results.
1094%
1095% The format of the FxEvaluateExpression method is:
1096%
1097% MagickRealType FxEvaluateChannelExpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00001098% const PixelChannel channel,const ssize_t x,const ssize_t y,
cristy3ed852e2009-09-05 21:47:34 +00001099% MagickRealType *alpha,Exceptioninfo *exception)
1100% MagickRealType FxEvaluateExpression(FxInfo *fx_info,
1101% MagickRealType *alpha,Exceptioninfo *exception)
1102%
1103% A description of each parameter follows:
1104%
1105% o fx_info: the fx info.
1106%
1107% o channel: the channel.
1108%
1109% o x,y: the pixel position.
1110%
1111% o alpha: the result.
1112%
1113% o exception: return any errors or warnings in this structure.
1114%
1115*/
1116
cristy351842f2010-03-07 15:27:38 +00001117static inline double MagickMax(const double x,const double y)
1118{
1119 if (x > y)
1120 return(x);
1121 return(y);
1122}
1123
1124static inline double MagickMin(const double x,const double y)
1125{
1126 if (x < y)
1127 return(x);
1128 return(y);
1129}
1130
cristy3ed852e2009-09-05 21:47:34 +00001131static MagickRealType FxChannelStatistics(FxInfo *fx_info,const Image *image,
cristy0568ffc2011-07-25 16:54:14 +00001132 PixelChannel channel,const char *symbol,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001133{
1134 char
1135 key[MaxTextExtent],
1136 statistic[MaxTextExtent];
1137
1138 const char
1139 *value;
1140
1141 register const char
1142 *p;
1143
1144 for (p=symbol; (*p != '.') && (*p != '\0'); p++) ;
1145 if (*p == '.')
1146 switch (*++p) /* e.g. depth.r */
1147 {
cristy541ae572011-08-05 19:08:59 +00001148 case 'r': channel=RedPixelChannel; break;
1149 case 'g': channel=GreenPixelChannel; break;
1150 case 'b': channel=BluePixelChannel; break;
1151 case 'c': channel=CyanPixelChannel; break;
1152 case 'm': channel=MagentaPixelChannel; break;
1153 case 'y': channel=YellowPixelChannel; break;
1154 case 'k': channel=BlackPixelChannel; break;
cristy3ed852e2009-09-05 21:47:34 +00001155 default: break;
1156 }
cristyb51dff52011-05-19 16:55:47 +00001157 (void) FormatLocaleString(key,MaxTextExtent,"%p.%.20g.%s",(void *) image,
cristye8c25f92010-06-03 00:53:06 +00001158 (double) channel,symbol);
cristy3ed852e2009-09-05 21:47:34 +00001159 value=(const char *) GetValueFromSplayTree(fx_info->symbols,key);
1160 if (value != (const char *) NULL)
cristydbdd0e32011-11-04 23:29:40 +00001161 return(QuantumScale*StringToDouble(value,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001162 (void) DeleteNodeFromSplayTree(fx_info->symbols,key);
1163 if (LocaleNCompare(symbol,"depth",5) == 0)
1164 {
cristybb503372010-05-27 20:51:26 +00001165 size_t
cristy3ed852e2009-09-05 21:47:34 +00001166 depth;
1167
cristyfefab1b2011-07-05 00:33:22 +00001168 depth=GetImageDepth(image,exception);
1169 (void) FormatLocaleString(statistic,MaxTextExtent,"%.20g",(double) depth);
cristy3ed852e2009-09-05 21:47:34 +00001170 }
1171 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1172 {
1173 double
1174 kurtosis,
1175 skewness;
1176
cristyd42d9952011-07-08 14:21:50 +00001177 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
cristyb51dff52011-05-19 16:55:47 +00001178 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",kurtosis);
cristy3ed852e2009-09-05 21:47:34 +00001179 }
1180 if (LocaleNCompare(symbol,"maxima",6) == 0)
1181 {
1182 double
1183 maxima,
1184 minima;
1185
cristyd42d9952011-07-08 14:21:50 +00001186 (void) GetImageRange(image,&minima,&maxima,exception);
cristyb51dff52011-05-19 16:55:47 +00001187 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",maxima);
cristy3ed852e2009-09-05 21:47:34 +00001188 }
1189 if (LocaleNCompare(symbol,"mean",4) == 0)
1190 {
1191 double
1192 mean,
1193 standard_deviation;
1194
cristyd42d9952011-07-08 14:21:50 +00001195 (void) GetImageMean(image,&mean,&standard_deviation,exception);
cristyb51dff52011-05-19 16:55:47 +00001196 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",mean);
cristy3ed852e2009-09-05 21:47:34 +00001197 }
1198 if (LocaleNCompare(symbol,"minima",6) == 0)
1199 {
1200 double
1201 maxima,
1202 minima;
1203
cristyd42d9952011-07-08 14:21:50 +00001204 (void) GetImageRange(image,&minima,&maxima,exception);
cristyb51dff52011-05-19 16:55:47 +00001205 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",minima);
cristy3ed852e2009-09-05 21:47:34 +00001206 }
1207 if (LocaleNCompare(symbol,"skewness",8) == 0)
1208 {
1209 double
1210 kurtosis,
1211 skewness;
1212
cristyd42d9952011-07-08 14:21:50 +00001213 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
cristyb51dff52011-05-19 16:55:47 +00001214 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",skewness);
cristy3ed852e2009-09-05 21:47:34 +00001215 }
1216 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1217 {
1218 double
1219 mean,
1220 standard_deviation;
1221
cristyd42d9952011-07-08 14:21:50 +00001222 (void) GetImageMean(image,&mean,&standard_deviation,exception);
cristyb51dff52011-05-19 16:55:47 +00001223 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",
cristy3ed852e2009-09-05 21:47:34 +00001224 standard_deviation);
1225 }
1226 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(key),
1227 ConstantString(statistic));
cristydbdd0e32011-11-04 23:29:40 +00001228 return(QuantumScale*StringToDouble(statistic,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001229}
1230
1231static MagickRealType
cristy0568ffc2011-07-25 16:54:14 +00001232 FxEvaluateSubexpression(FxInfo *,const PixelChannel,const ssize_t,
cristye85007d2010-06-06 22:51:36 +00001233 const ssize_t,const char *,MagickRealType *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +00001234
cristyb0aad4c2011-11-02 19:30:35 +00001235static MagickOffsetType FxGCD(MagickOffsetType alpha,MagickOffsetType beta)
1236{
1237 if (beta != 0)
1238 return(FxGCD(beta,alpha % beta));
1239 return(alpha);
1240}
1241
cristy3ed852e2009-09-05 21:47:34 +00001242static inline const char *FxSubexpression(const char *expression,
1243 ExceptionInfo *exception)
1244{
1245 const char
1246 *subexpression;
1247
cristybb503372010-05-27 20:51:26 +00001248 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001249 level;
1250
1251 level=0;
1252 subexpression=expression;
1253 while ((*subexpression != '\0') &&
1254 ((level != 1) || (strchr(")",(int) *subexpression) == (char *) NULL)))
1255 {
1256 if (strchr("(",(int) *subexpression) != (char *) NULL)
1257 level++;
1258 else
1259 if (strchr(")",(int) *subexpression) != (char *) NULL)
1260 level--;
1261 subexpression++;
1262 }
1263 if (*subexpression == '\0')
1264 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
anthonye5b39652012-04-21 05:37:29 +00001265 "UnbalancedParenthesis","'%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00001266 return(subexpression);
1267}
1268
cristy0568ffc2011-07-25 16:54:14 +00001269static MagickRealType FxGetSymbol(FxInfo *fx_info,const PixelChannel channel,
cristye85007d2010-06-06 22:51:36 +00001270 const ssize_t x,const ssize_t y,const char *expression,
1271 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001272{
1273 char
1274 *q,
1275 subexpression[MaxTextExtent],
1276 symbol[MaxTextExtent];
1277
1278 const char
1279 *p,
1280 *value;
1281
1282 Image
1283 *image;
1284
cristy4c08aed2011-07-01 19:47:50 +00001285 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001286 pixel;
1287
1288 MagickRealType
1289 alpha,
1290 beta;
1291
1292 PointInfo
1293 point;
1294
cristybb503372010-05-27 20:51:26 +00001295 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001296 i;
1297
1298 size_t
cristy1707c6c2012-01-18 23:30:54 +00001299 length,
cristy3ed852e2009-09-05 21:47:34 +00001300 level;
1301
1302 p=expression;
1303 i=GetImageIndexInList(fx_info->images);
1304 level=0;
1305 point.x=(double) x;
1306 point.y=(double) y;
1307 if (isalpha((int) *(p+1)) == 0)
1308 {
1309 if (strchr("suv",(int) *p) != (char *) NULL)
1310 {
1311 switch (*p)
1312 {
1313 case 's':
1314 default:
1315 {
1316 i=GetImageIndexInList(fx_info->images);
1317 break;
1318 }
1319 case 'u': i=0; break;
1320 case 'v': i=1; break;
1321 }
1322 p++;
1323 if (*p == '[')
1324 {
1325 level++;
1326 q=subexpression;
1327 for (p++; *p != '\0'; )
1328 {
1329 if (*p == '[')
1330 level++;
1331 else
1332 if (*p == ']')
1333 {
1334 level--;
1335 if (level == 0)
1336 break;
1337 }
1338 *q++=(*p++);
1339 }
1340 *q='\0';
1341 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1342 &beta,exception);
cristybb503372010-05-27 20:51:26 +00001343 i=(ssize_t) (alpha+0.5);
cristy3ed852e2009-09-05 21:47:34 +00001344 p++;
1345 }
1346 if (*p == '.')
1347 p++;
1348 }
1349 if ((isalpha((int) *(p+1)) == 0) && (*p == 'p'))
1350 {
1351 p++;
1352 if (*p == '{')
1353 {
1354 level++;
1355 q=subexpression;
1356 for (p++; *p != '\0'; )
1357 {
1358 if (*p == '{')
1359 level++;
1360 else
1361 if (*p == '}')
1362 {
1363 level--;
1364 if (level == 0)
1365 break;
1366 }
1367 *q++=(*p++);
1368 }
1369 *q='\0';
1370 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1371 &beta,exception);
1372 point.x=alpha;
1373 point.y=beta;
1374 p++;
1375 }
1376 else
1377 if (*p == '[')
1378 {
1379 level++;
1380 q=subexpression;
1381 for (p++; *p != '\0'; )
1382 {
1383 if (*p == '[')
1384 level++;
1385 else
1386 if (*p == ']')
1387 {
1388 level--;
1389 if (level == 0)
1390 break;
1391 }
1392 *q++=(*p++);
1393 }
1394 *q='\0';
1395 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1396 &beta,exception);
1397 point.x+=alpha;
1398 point.y+=beta;
1399 p++;
1400 }
1401 if (*p == '.')
1402 p++;
1403 }
1404 }
1405 length=GetImageListLength(fx_info->images);
1406 while (i < 0)
cristybb503372010-05-27 20:51:26 +00001407 i+=(ssize_t) length;
cristy3ed852e2009-09-05 21:47:34 +00001408 i%=length;
1409 image=GetImageFromList(fx_info->images,i);
1410 if (image == (Image *) NULL)
1411 {
1412 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
anthonye5b39652012-04-21 05:37:29 +00001413 "NoSuchImage","'%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00001414 return(0.0);
1415 }
cristy4c08aed2011-07-01 19:47:50 +00001416 GetPixelInfo(image,&pixel);
1417 (void) InterpolatePixelInfo(image,fx_info->view[i],image->interpolate,
cristy4f820712011-04-01 12:35:43 +00001418 point.x,point.y,&pixel,exception);
cristy1707c6c2012-01-18 23:30:54 +00001419 if ((strlen(p) > 2) && (LocaleCompare(p,"intensity") != 0) &&
1420 (LocaleCompare(p,"luminance") != 0) && (LocaleCompare(p,"hue") != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00001421 (LocaleCompare(p,"saturation") != 0) &&
1422 (LocaleCompare(p,"lightness") != 0))
1423 {
1424 char
1425 name[MaxTextExtent];
1426
1427 (void) CopyMagickString(name,p,MaxTextExtent);
1428 for (q=name+(strlen(name)-1); q > name; q--)
1429 {
1430 if (*q == ')')
1431 break;
1432 if (*q == '.')
1433 {
1434 *q='\0';
1435 break;
1436 }
1437 }
1438 if ((strlen(name) > 2) &&
1439 (GetValueFromSplayTree(fx_info->symbols,name) == (const char *) NULL))
1440 {
cristy4c08aed2011-07-01 19:47:50 +00001441 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001442 *color;
1443
cristy4c08aed2011-07-01 19:47:50 +00001444 color=(PixelInfo *) GetValueFromSplayTree(fx_info->colors,name);
1445 if (color != (PixelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001446 {
1447 pixel=(*color);
1448 p+=strlen(name);
1449 }
1450 else
cristy1707c6c2012-01-18 23:30:54 +00001451 {
1452 MagickBooleanType
1453 status;
1454
1455 status=QueryColorCompliance(name,AllCompliance,&pixel,
1456 fx_info->exception);
1457 if (status != MagickFalse)
1458 {
1459 (void) AddValueToSplayTree(fx_info->colors,ConstantString(
1460 name),ClonePixelInfo(&pixel));
1461 p+=strlen(name);
1462 }
1463 }
cristy3ed852e2009-09-05 21:47:34 +00001464 }
1465 }
1466 (void) CopyMagickString(symbol,p,MaxTextExtent);
1467 StripString(symbol);
1468 if (*symbol == '\0')
1469 {
1470 switch (channel)
1471 {
cristy0568ffc2011-07-25 16:54:14 +00001472 case RedPixelChannel: return(QuantumScale*pixel.red);
1473 case GreenPixelChannel: return(QuantumScale*pixel.green);
1474 case BluePixelChannel: return(QuantumScale*pixel.blue);
1475 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001476 {
1477 if (image->colorspace != CMYKColorspace)
1478 {
1479 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00001480 ImageError,"ColorSeparatedImageRequired","'%s'",
cristy3ed852e2009-09-05 21:47:34 +00001481 image->filename);
1482 return(0.0);
1483 }
cristy4c08aed2011-07-01 19:47:50 +00001484 return(QuantumScale*pixel.black);
1485 }
cristy0568ffc2011-07-25 16:54:14 +00001486 case AlphaPixelChannel:
cristy4c08aed2011-07-01 19:47:50 +00001487 {
1488 MagickRealType
1489 alpha;
1490
1491 if (pixel.matte == MagickFalse)
1492 return(1.0);
1493 alpha=(MagickRealType) (QuantumScale*pixel.alpha);
1494 return(alpha);
cristy3ed852e2009-09-05 21:47:34 +00001495 }
cristya382aca2011-12-06 18:22:48 +00001496 case IndexPixelChannel:
1497 return(0.0);
cristyb3a73b52011-07-26 01:34:43 +00001498 case IntensityPixelChannel:
cristyf364ed42010-12-15 01:54:43 +00001499 {
cristy4c08aed2011-07-01 19:47:50 +00001500 return(QuantumScale*GetPixelInfoIntensity(&pixel));
cristyf364ed42010-12-15 01:54:43 +00001501 }
cristy3ed852e2009-09-05 21:47:34 +00001502 default:
1503 break;
1504 }
1505 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
anthonye5b39652012-04-21 05:37:29 +00001506 "UnableToParseExpression","'%s'",p);
cristy3ed852e2009-09-05 21:47:34 +00001507 return(0.0);
1508 }
1509 switch (*symbol)
1510 {
1511 case 'A':
1512 case 'a':
1513 {
1514 if (LocaleCompare(symbol,"a") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001515 return((MagickRealType) (QuantumScale*pixel.alpha));
cristy3ed852e2009-09-05 21:47:34 +00001516 break;
1517 }
1518 case 'B':
1519 case 'b':
1520 {
1521 if (LocaleCompare(symbol,"b") == 0)
1522 return(QuantumScale*pixel.blue);
1523 break;
1524 }
1525 case 'C':
1526 case 'c':
1527 {
1528 if (LocaleNCompare(symbol,"channel",7) == 0)
1529 {
1530 GeometryInfo
1531 channel_info;
1532
1533 MagickStatusType
1534 flags;
1535
1536 flags=ParseGeometry(symbol+7,&channel_info);
1537 if (image->colorspace == CMYKColorspace)
1538 switch (channel)
1539 {
cristy0568ffc2011-07-25 16:54:14 +00001540 case CyanPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001541 {
1542 if ((flags & RhoValue) == 0)
1543 return(0.0);
1544 return(channel_info.rho);
1545 }
cristy0568ffc2011-07-25 16:54:14 +00001546 case MagentaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001547 {
1548 if ((flags & SigmaValue) == 0)
1549 return(0.0);
1550 return(channel_info.sigma);
1551 }
cristy0568ffc2011-07-25 16:54:14 +00001552 case YellowPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001553 {
1554 if ((flags & XiValue) == 0)
1555 return(0.0);
1556 return(channel_info.xi);
1557 }
cristy0568ffc2011-07-25 16:54:14 +00001558 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001559 {
1560 if ((flags & PsiValue) == 0)
1561 return(0.0);
1562 return(channel_info.psi);
1563 }
cristy0568ffc2011-07-25 16:54:14 +00001564 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001565 {
1566 if ((flags & ChiValue) == 0)
1567 return(0.0);
1568 return(channel_info.chi);
1569 }
1570 default:
1571 return(0.0);
1572 }
1573 switch (channel)
1574 {
cristy0568ffc2011-07-25 16:54:14 +00001575 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001576 {
1577 if ((flags & RhoValue) == 0)
1578 return(0.0);
1579 return(channel_info.rho);
1580 }
cristy0568ffc2011-07-25 16:54:14 +00001581 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001582 {
1583 if ((flags & SigmaValue) == 0)
1584 return(0.0);
1585 return(channel_info.sigma);
1586 }
cristy0568ffc2011-07-25 16:54:14 +00001587 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001588 {
1589 if ((flags & XiValue) == 0)
1590 return(0.0);
1591 return(channel_info.xi);
1592 }
cristy0568ffc2011-07-25 16:54:14 +00001593 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001594 {
1595 if ((flags & ChiValue) == 0)
1596 return(0.0);
1597 return(channel_info.chi);
1598 }
cristy0568ffc2011-07-25 16:54:14 +00001599 case AlphaPixelChannel:
cristy4c08aed2011-07-01 19:47:50 +00001600 {
1601 if ((flags & PsiValue) == 0)
1602 return(0.0);
1603 return(channel_info.psi);
1604 }
cristy3ed852e2009-09-05 21:47:34 +00001605 default:
1606 return(0.0);
1607 }
1608 return(0.0);
1609 }
1610 if (LocaleCompare(symbol,"c") == 0)
1611 return(QuantumScale*pixel.red);
1612 break;
1613 }
1614 case 'D':
1615 case 'd':
1616 {
1617 if (LocaleNCompare(symbol,"depth",5) == 0)
1618 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1619 break;
1620 }
1621 case 'G':
1622 case 'g':
1623 {
1624 if (LocaleCompare(symbol,"g") == 0)
1625 return(QuantumScale*pixel.green);
1626 break;
1627 }
1628 case 'K':
1629 case 'k':
1630 {
1631 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1632 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1633 if (LocaleCompare(symbol,"k") == 0)
1634 {
1635 if (image->colorspace != CMYKColorspace)
1636 {
1637 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00001638 OptionError,"ColorSeparatedImageRequired","'%s'",
cristy3ed852e2009-09-05 21:47:34 +00001639 image->filename);
1640 return(0.0);
1641 }
cristy4c08aed2011-07-01 19:47:50 +00001642 return(QuantumScale*pixel.black);
cristy3ed852e2009-09-05 21:47:34 +00001643 }
1644 break;
1645 }
1646 case 'H':
1647 case 'h':
1648 {
1649 if (LocaleCompare(symbol,"h") == 0)
1650 return((MagickRealType) image->rows);
1651 if (LocaleCompare(symbol,"hue") == 0)
1652 {
1653 double
1654 hue,
1655 lightness,
1656 saturation;
1657
cristyda1f9c12011-10-02 21:39:49 +00001658 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1659 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001660 return(hue);
1661 }
1662 break;
1663 }
1664 case 'I':
1665 case 'i':
1666 {
1667 if ((LocaleCompare(symbol,"image.depth") == 0) ||
1668 (LocaleCompare(symbol,"image.minima") == 0) ||
1669 (LocaleCompare(symbol,"image.maxima") == 0) ||
1670 (LocaleCompare(symbol,"image.mean") == 0) ||
1671 (LocaleCompare(symbol,"image.kurtosis") == 0) ||
1672 (LocaleCompare(symbol,"image.skewness") == 0) ||
1673 (LocaleCompare(symbol,"image.standard_deviation") == 0))
1674 return(FxChannelStatistics(fx_info,image,channel,symbol+6,exception));
1675 if (LocaleCompare(symbol,"image.resolution.x") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001676 return(image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00001677 if (LocaleCompare(symbol,"image.resolution.y") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001678 return(image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001679 if (LocaleCompare(symbol,"intensity") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001680 return(QuantumScale*GetPixelInfoIntensity(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001681 if (LocaleCompare(symbol,"i") == 0)
1682 return((MagickRealType) x);
1683 break;
1684 }
1685 case 'J':
1686 case 'j':
1687 {
1688 if (LocaleCompare(symbol,"j") == 0)
1689 return((MagickRealType) y);
1690 break;
1691 }
1692 case 'L':
1693 case 'l':
1694 {
1695 if (LocaleCompare(symbol,"lightness") == 0)
1696 {
1697 double
1698 hue,
1699 lightness,
1700 saturation;
1701
cristyda1f9c12011-10-02 21:39:49 +00001702 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1703 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001704 return(lightness);
1705 }
1706 if (LocaleCompare(symbol,"luminance") == 0)
1707 {
1708 double
1709 luminence;
1710
1711 luminence=0.2126*pixel.red+0.7152*pixel.green+0.0722*pixel.blue;
1712 return(QuantumScale*luminence);
1713 }
1714 break;
1715 }
1716 case 'M':
1717 case 'm':
1718 {
1719 if (LocaleNCompare(symbol,"maxima",6) == 0)
1720 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1721 if (LocaleNCompare(symbol,"mean",4) == 0)
1722 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1723 if (LocaleNCompare(symbol,"minima",6) == 0)
1724 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1725 if (LocaleCompare(symbol,"m") == 0)
1726 return(QuantumScale*pixel.blue);
1727 break;
1728 }
1729 case 'N':
1730 case 'n':
1731 {
1732 if (LocaleCompare(symbol,"n") == 0)
anthony374f5dd2011-03-25 10:08:53 +00001733 return((MagickRealType) GetImageListLength(fx_info->images));
cristy3ed852e2009-09-05 21:47:34 +00001734 break;
1735 }
1736 case 'O':
1737 case 'o':
1738 {
1739 if (LocaleCompare(symbol,"o") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001740 return(QuantumScale*pixel.alpha);
cristy3ed852e2009-09-05 21:47:34 +00001741 break;
1742 }
1743 case 'P':
1744 case 'p':
1745 {
1746 if (LocaleCompare(symbol,"page.height") == 0)
1747 return((MagickRealType) image->page.height);
1748 if (LocaleCompare(symbol,"page.width") == 0)
1749 return((MagickRealType) image->page.width);
1750 if (LocaleCompare(symbol,"page.x") == 0)
1751 return((MagickRealType) image->page.x);
1752 if (LocaleCompare(symbol,"page.y") == 0)
1753 return((MagickRealType) image->page.y);
1754 break;
1755 }
1756 case 'R':
1757 case 'r':
1758 {
1759 if (LocaleCompare(symbol,"resolution.x") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001760 return(image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00001761 if (LocaleCompare(symbol,"resolution.y") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001762 return(image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001763 if (LocaleCompare(symbol,"r") == 0)
1764 return(QuantumScale*pixel.red);
1765 break;
1766 }
1767 case 'S':
1768 case 's':
1769 {
1770 if (LocaleCompare(symbol,"saturation") == 0)
1771 {
1772 double
1773 hue,
1774 lightness,
1775 saturation;
1776
cristyda1f9c12011-10-02 21:39:49 +00001777 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1778 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001779 return(saturation);
1780 }
1781 if (LocaleNCompare(symbol,"skewness",8) == 0)
1782 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1783 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1784 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1785 break;
1786 }
1787 case 'T':
1788 case 't':
1789 {
1790 if (LocaleCompare(symbol,"t") == 0)
cristy5a15b932011-03-26 12:50:33 +00001791 return((MagickRealType) GetImageIndexInList(fx_info->images));
cristy3ed852e2009-09-05 21:47:34 +00001792 break;
1793 }
1794 case 'W':
1795 case 'w':
1796 {
1797 if (LocaleCompare(symbol,"w") == 0)
1798 return((MagickRealType) image->columns);
1799 break;
1800 }
1801 case 'Y':
1802 case 'y':
1803 {
1804 if (LocaleCompare(symbol,"y") == 0)
1805 return(QuantumScale*pixel.green);
1806 break;
1807 }
1808 case 'Z':
1809 case 'z':
1810 {
1811 if (LocaleCompare(symbol,"z") == 0)
1812 {
1813 MagickRealType
1814 depth;
1815
cristyfefab1b2011-07-05 00:33:22 +00001816 depth=(MagickRealType) GetImageDepth(image,fx_info->exception);
cristy3ed852e2009-09-05 21:47:34 +00001817 return(depth);
1818 }
1819 break;
1820 }
1821 default:
1822 break;
1823 }
1824 value=(const char *) GetValueFromSplayTree(fx_info->symbols,symbol);
1825 if (value != (const char *) NULL)
cristydbdd0e32011-11-04 23:29:40 +00001826 return((MagickRealType) StringToDouble(value,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001827 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
anthonye5b39652012-04-21 05:37:29 +00001828 "UnableToParseExpression","'%s'",symbol);
cristy3ed852e2009-09-05 21:47:34 +00001829 return(0.0);
1830}
1831
1832static const char *FxOperatorPrecedence(const char *expression,
1833 ExceptionInfo *exception)
1834{
1835 typedef enum
1836 {
1837 UndefinedPrecedence,
1838 NullPrecedence,
1839 BitwiseComplementPrecedence,
1840 ExponentPrecedence,
cristy116af162010-08-13 01:25:47 +00001841 ExponentialNotationPrecedence,
cristy3ed852e2009-09-05 21:47:34 +00001842 MultiplyPrecedence,
1843 AdditionPrecedence,
1844 ShiftPrecedence,
1845 RelationalPrecedence,
1846 EquivalencyPrecedence,
1847 BitwiseAndPrecedence,
1848 BitwiseOrPrecedence,
1849 LogicalAndPrecedence,
1850 LogicalOrPrecedence,
1851 TernaryPrecedence,
1852 AssignmentPrecedence,
1853 CommaPrecedence,
1854 SeparatorPrecedence
1855 } FxPrecedence;
1856
1857 FxPrecedence
1858 precedence,
1859 target;
1860
1861 register const char
1862 *subexpression;
1863
1864 register int
1865 c;
1866
cristybb503372010-05-27 20:51:26 +00001867 size_t
cristy3ed852e2009-09-05 21:47:34 +00001868 level;
1869
1870 c=0;
1871 level=0;
1872 subexpression=(const char *) NULL;
1873 target=NullPrecedence;
1874 while (*expression != '\0')
1875 {
1876 precedence=UndefinedPrecedence;
1877 if ((isspace((int) ((char) *expression)) != 0) || (c == (int) '@'))
1878 {
1879 expression++;
1880 continue;
1881 }
cristy488fa882010-03-01 22:34:24 +00001882 switch (*expression)
1883 {
1884 case 'A':
1885 case 'a':
cristy3ed852e2009-09-05 21:47:34 +00001886 {
cristyb33454f2011-08-03 02:10:45 +00001887#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00001888 if (LocaleNCompare(expression,"acosh",5) == 0)
1889 {
1890 expression+=5;
1891 break;
1892 }
cristyb33454f2011-08-03 02:10:45 +00001893#endif
1894#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00001895 if (LocaleNCompare(expression,"asinh",5) == 0)
1896 {
1897 expression+=5;
1898 break;
1899 }
cristyb33454f2011-08-03 02:10:45 +00001900#endif
1901#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00001902 if (LocaleNCompare(expression,"atanh",5) == 0)
cristy488fa882010-03-01 22:34:24 +00001903 {
1904 expression+=5;
1905 break;
1906 }
cristyb33454f2011-08-03 02:10:45 +00001907#endif
cristy488fa882010-03-01 22:34:24 +00001908 break;
cristy3ed852e2009-09-05 21:47:34 +00001909 }
cristy62d455f2011-11-03 11:42:28 +00001910 case 'E':
1911 case 'e':
1912 {
1913 if ((LocaleNCompare(expression,"E+",2) == 0) ||
1914 (LocaleNCompare(expression,"E-",2) == 0))
1915 {
1916 expression+=2; /* scientific notation */
1917 break;
1918 }
1919 }
cristy488fa882010-03-01 22:34:24 +00001920 case 'J':
1921 case 'j':
1922 {
1923 if ((LocaleNCompare(expression,"j0",2) == 0) ||
1924 (LocaleNCompare(expression,"j1",2) == 0))
1925 {
1926 expression+=2;
1927 break;
1928 }
1929 break;
1930 }
cristy2def9322010-06-18 23:59:37 +00001931 case '#':
1932 {
1933 while (isxdigit((int) ((unsigned char) *(expression+1))) != 0)
1934 expression++;
1935 break;
1936 }
cristy488fa882010-03-01 22:34:24 +00001937 default:
1938 break;
1939 }
cristy3ed852e2009-09-05 21:47:34 +00001940 if ((c == (int) '{') || (c == (int) '['))
1941 level++;
1942 else
1943 if ((c == (int) '}') || (c == (int) ']'))
1944 level--;
1945 if (level == 0)
1946 switch ((unsigned char) *expression)
1947 {
1948 case '~':
1949 case '!':
1950 {
1951 precedence=BitwiseComplementPrecedence;
1952 break;
1953 }
1954 case '^':
cristy6621e252010-08-13 00:42:57 +00001955 case '@':
cristy3ed852e2009-09-05 21:47:34 +00001956 {
1957 precedence=ExponentPrecedence;
1958 break;
1959 }
1960 default:
1961 {
1962 if (((c != 0) && ((isdigit((int) ((char) c)) != 0) ||
1963 (strchr(")",c) != (char *) NULL))) &&
1964 (((islower((int) ((char) *expression)) != 0) ||
1965 (strchr("(",(int) *expression) != (char *) NULL)) ||
1966 ((isdigit((int) ((char) c)) == 0) &&
1967 (isdigit((int) ((char) *expression)) != 0))) &&
1968 (strchr("xy",(int) *expression) == (char *) NULL))
1969 precedence=MultiplyPrecedence;
1970 break;
1971 }
1972 case '*':
1973 case '/':
1974 case '%':
1975 {
1976 precedence=MultiplyPrecedence;
1977 break;
1978 }
1979 case '+':
1980 case '-':
1981 {
1982 if ((strchr("(+-/*%:&^|<>~,",c) == (char *) NULL) ||
1983 (isalpha(c) != 0))
1984 precedence=AdditionPrecedence;
1985 break;
1986 }
1987 case LeftShiftOperator:
1988 case RightShiftOperator:
1989 {
1990 precedence=ShiftPrecedence;
1991 break;
1992 }
1993 case '<':
1994 case LessThanEqualOperator:
1995 case GreaterThanEqualOperator:
1996 case '>':
1997 {
1998 precedence=RelationalPrecedence;
1999 break;
2000 }
2001 case EqualOperator:
2002 case NotEqualOperator:
2003 {
2004 precedence=EquivalencyPrecedence;
2005 break;
2006 }
2007 case '&':
2008 {
2009 precedence=BitwiseAndPrecedence;
2010 break;
2011 }
2012 case '|':
2013 {
2014 precedence=BitwiseOrPrecedence;
2015 break;
2016 }
2017 case LogicalAndOperator:
2018 {
2019 precedence=LogicalAndPrecedence;
2020 break;
2021 }
2022 case LogicalOrOperator:
2023 {
2024 precedence=LogicalOrPrecedence;
2025 break;
2026 }
cristy116af162010-08-13 01:25:47 +00002027 case ExponentialNotation:
2028 {
2029 precedence=ExponentialNotationPrecedence;
2030 break;
2031 }
cristy3ed852e2009-09-05 21:47:34 +00002032 case ':':
2033 case '?':
2034 {
2035 precedence=TernaryPrecedence;
2036 break;
2037 }
2038 case '=':
2039 {
2040 precedence=AssignmentPrecedence;
2041 break;
2042 }
2043 case ',':
2044 {
2045 precedence=CommaPrecedence;
2046 break;
2047 }
2048 case ';':
2049 {
2050 precedence=SeparatorPrecedence;
2051 break;
2052 }
2053 }
2054 if ((precedence == BitwiseComplementPrecedence) ||
2055 (precedence == TernaryPrecedence) ||
2056 (precedence == AssignmentPrecedence))
2057 {
2058 if (precedence > target)
2059 {
2060 /*
2061 Right-to-left associativity.
2062 */
2063 target=precedence;
2064 subexpression=expression;
2065 }
2066 }
2067 else
2068 if (precedence >= target)
2069 {
2070 /*
2071 Left-to-right associativity.
2072 */
2073 target=precedence;
2074 subexpression=expression;
2075 }
2076 if (strchr("(",(int) *expression) != (char *) NULL)
2077 expression=FxSubexpression(expression,exception);
2078 c=(int) (*expression++);
2079 }
2080 return(subexpression);
2081}
2082
2083static MagickRealType FxEvaluateSubexpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002084 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristye85007d2010-06-06 22:51:36 +00002085 const char *expression,MagickRealType *beta,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002086{
2087 char
2088 *q,
2089 subexpression[MaxTextExtent];
2090
2091 MagickRealType
2092 alpha,
2093 gamma;
2094
2095 register const char
2096 *p;
2097
2098 *beta=0.0;
2099 if (exception->severity != UndefinedException)
2100 return(0.0);
2101 while (isspace((int) *expression) != 0)
2102 expression++;
2103 if (*expression == '\0')
2104 {
2105 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
anthonye5b39652012-04-21 05:37:29 +00002106 "MissingExpression","'%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00002107 return(0.0);
2108 }
cristy66322f02010-05-17 11:40:48 +00002109 *subexpression='\0';
cristy3ed852e2009-09-05 21:47:34 +00002110 p=FxOperatorPrecedence(expression,exception);
2111 if (p != (const char *) NULL)
2112 {
2113 (void) CopyMagickString(subexpression,expression,(size_t)
2114 (p-expression+1));
2115 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2116 exception);
2117 switch ((unsigned char) *p)
2118 {
2119 case '~':
2120 {
2121 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristybb503372010-05-27 20:51:26 +00002122 *beta=(MagickRealType) (~(size_t) *beta);
cristy3ed852e2009-09-05 21:47:34 +00002123 return(*beta);
2124 }
2125 case '!':
2126 {
2127 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2128 return(*beta == 0.0 ? 1.0 : 0.0);
2129 }
2130 case '^':
2131 {
2132 *beta=pow((double) alpha,(double) FxEvaluateSubexpression(fx_info,
2133 channel,x,y,++p,beta,exception));
2134 return(*beta);
2135 }
2136 case '*':
cristy116af162010-08-13 01:25:47 +00002137 case ExponentialNotation:
cristy3ed852e2009-09-05 21:47:34 +00002138 {
2139 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2140 return(alpha*(*beta));
2141 }
2142 case '/':
2143 {
2144 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2145 if (*beta == 0.0)
2146 {
2147 if (exception->severity == UndefinedException)
2148 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00002149 OptionError,"DivideByZero","'%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00002150 return(0.0);
2151 }
2152 return(alpha/(*beta));
2153 }
2154 case '%':
2155 {
2156 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2157 *beta=fabs(floor(((double) *beta)+0.5));
2158 if (*beta == 0.0)
2159 {
2160 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00002161 OptionError,"DivideByZero","'%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00002162 return(0.0);
2163 }
2164 return(fmod((double) alpha,(double) *beta));
2165 }
2166 case '+':
2167 {
2168 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2169 return(alpha+(*beta));
2170 }
2171 case '-':
2172 {
2173 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2174 return(alpha-(*beta));
2175 }
2176 case LeftShiftOperator:
2177 {
2178 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy1707c6c2012-01-18 23:30:54 +00002179 *beta=(MagickRealType) ((size_t) (alpha+0.5) << (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002180 return(*beta);
2181 }
2182 case RightShiftOperator:
2183 {
2184 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy1707c6c2012-01-18 23:30:54 +00002185 *beta=(MagickRealType) ((size_t) (alpha+0.5) >> (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002186 return(*beta);
2187 }
2188 case '<':
2189 {
2190 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2191 return(alpha < *beta ? 1.0 : 0.0);
2192 }
2193 case LessThanEqualOperator:
2194 {
2195 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2196 return(alpha <= *beta ? 1.0 : 0.0);
2197 }
2198 case '>':
2199 {
2200 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2201 return(alpha > *beta ? 1.0 : 0.0);
2202 }
2203 case GreaterThanEqualOperator:
2204 {
2205 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2206 return(alpha >= *beta ? 1.0 : 0.0);
2207 }
2208 case EqualOperator:
2209 {
2210 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2211 return(fabs(alpha-(*beta)) <= MagickEpsilon ? 1.0 : 0.0);
2212 }
2213 case NotEqualOperator:
2214 {
2215 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2216 return(fabs(alpha-(*beta)) > MagickEpsilon ? 1.0 : 0.0);
2217 }
2218 case '&':
2219 {
2220 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy1707c6c2012-01-18 23:30:54 +00002221 *beta=(MagickRealType) ((size_t) (alpha+0.5) & (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002222 return(*beta);
2223 }
2224 case '|':
2225 {
2226 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy1707c6c2012-01-18 23:30:54 +00002227 *beta=(MagickRealType) ((size_t) (alpha+0.5) | (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002228 return(*beta);
2229 }
2230 case LogicalAndOperator:
2231 {
2232 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2233 *beta=(alpha > 0.0) && (gamma > 0.0) ? 1.0 : 0.0;
2234 return(*beta);
2235 }
2236 case LogicalOrOperator:
2237 {
2238 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2239 *beta=(alpha > 0.0) || (gamma > 0.0) ? 1.0 : 0.0;
2240 return(*beta);
2241 }
2242 case '?':
2243 {
2244 MagickRealType
2245 gamma;
2246
2247 (void) CopyMagickString(subexpression,++p,MaxTextExtent);
2248 q=subexpression;
2249 p=StringToken(":",&q);
2250 if (q == (char *) NULL)
2251 {
2252 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00002253 OptionError,"UnableToParseExpression","'%s'",subexpression);
cristy3ed852e2009-09-05 21:47:34 +00002254 return(0.0);
2255 }
2256 if (fabs((double) alpha) > MagickEpsilon)
2257 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,exception);
2258 else
2259 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,q,beta,exception);
2260 return(gamma);
2261 }
2262 case '=':
2263 {
2264 char
2265 numeric[MaxTextExtent];
2266
2267 q=subexpression;
2268 while (isalpha((int) ((unsigned char) *q)) != 0)
2269 q++;
2270 if (*q != '\0')
2271 {
2272 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00002273 OptionError,"UnableToParseExpression","'%s'",subexpression);
cristy3ed852e2009-09-05 21:47:34 +00002274 return(0.0);
2275 }
2276 ClearMagickException(exception);
2277 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristyb51dff52011-05-19 16:55:47 +00002278 (void) FormatLocaleString(numeric,MaxTextExtent,"%g",(double)
cristy8cd5b312010-01-07 01:10:24 +00002279 *beta);
cristy3ed852e2009-09-05 21:47:34 +00002280 (void) DeleteNodeFromSplayTree(fx_info->symbols,subexpression);
2281 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(
2282 subexpression),ConstantString(numeric));
2283 return(*beta);
2284 }
2285 case ',':
2286 {
2287 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2288 return(alpha);
2289 }
2290 case ';':
2291 {
2292 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2293 return(*beta);
2294 }
2295 default:
2296 {
2297 gamma=alpha*FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,
2298 exception);
2299 return(gamma);
2300 }
2301 }
2302 }
2303 if (strchr("(",(int) *expression) != (char *) NULL)
2304 {
2305 (void) CopyMagickString(subexpression,expression+1,MaxTextExtent);
2306 subexpression[strlen(subexpression)-1]='\0';
2307 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2308 exception);
2309 return(gamma);
2310 }
cristy8b8a3ae2010-10-23 18:49:46 +00002311 switch (*expression)
cristy3ed852e2009-09-05 21:47:34 +00002312 {
2313 case '+':
2314 {
2315 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2316 exception);
2317 return(1.0*gamma);
2318 }
2319 case '-':
2320 {
2321 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2322 exception);
2323 return(-1.0*gamma);
2324 }
2325 case '~':
2326 {
2327 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2328 exception);
cristybb503372010-05-27 20:51:26 +00002329 return((MagickRealType) (~(size_t) (gamma+0.5)));
cristy3ed852e2009-09-05 21:47:34 +00002330 }
2331 case 'A':
2332 case 'a':
2333 {
2334 if (LocaleNCompare(expression,"abs",3) == 0)
2335 {
2336 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2337 exception);
2338 return((MagickRealType) fabs((double) alpha));
2339 }
cristyb33454f2011-08-03 02:10:45 +00002340#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00002341 if (LocaleNCompare(expression,"acosh",5) == 0)
2342 {
2343 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2344 exception);
2345 return((MagickRealType) acosh((double) alpha));
2346 }
cristyb33454f2011-08-03 02:10:45 +00002347#endif
cristy3ed852e2009-09-05 21:47:34 +00002348 if (LocaleNCompare(expression,"acos",4) == 0)
2349 {
2350 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2351 exception);
2352 return((MagickRealType) acos((double) alpha));
2353 }
cristy43c22f42010-03-30 12:34:07 +00002354#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002355 if (LocaleNCompare(expression,"airy",4) == 0)
2356 {
2357 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2358 exception);
2359 if (alpha == 0.0)
cristy2dd03222010-03-30 22:12:11 +00002360 return(1.0);
2361 gamma=2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha);
cristy43c22f42010-03-30 12:34:07 +00002362 return(gamma*gamma);
cristyee56cf12010-03-01 22:17:06 +00002363 }
cristy43c22f42010-03-30 12:34:07 +00002364#endif
cristyb33454f2011-08-03 02:10:45 +00002365#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00002366 if (LocaleNCompare(expression,"asinh",5) == 0)
2367 {
2368 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2369 exception);
2370 return((MagickRealType) asinh((double) alpha));
2371 }
cristyb33454f2011-08-03 02:10:45 +00002372#endif
cristy3ed852e2009-09-05 21:47:34 +00002373 if (LocaleNCompare(expression,"asin",4) == 0)
2374 {
2375 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2376 exception);
2377 return((MagickRealType) asin((double) alpha));
2378 }
2379 if (LocaleNCompare(expression,"alt",3) == 0)
2380 {
2381 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2382 exception);
cristybb503372010-05-27 20:51:26 +00002383 return(((ssize_t) alpha) & 0x01 ? -1.0 : 1.0);
cristy3ed852e2009-09-05 21:47:34 +00002384 }
2385 if (LocaleNCompare(expression,"atan2",5) == 0)
2386 {
2387 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2388 exception);
2389 return((MagickRealType) atan2((double) alpha,(double) *beta));
2390 }
cristyb33454f2011-08-03 02:10:45 +00002391#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00002392 if (LocaleNCompare(expression,"atanh",5) == 0)
2393 {
2394 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2395 exception);
2396 return((MagickRealType) atanh((double) alpha));
2397 }
cristyb33454f2011-08-03 02:10:45 +00002398#endif
cristy3ed852e2009-09-05 21:47:34 +00002399 if (LocaleNCompare(expression,"atan",4) == 0)
2400 {
2401 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2402 exception);
2403 return((MagickRealType) atan((double) alpha));
2404 }
2405 if (LocaleCompare(expression,"a") == 0)
2406 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2407 break;
2408 }
2409 case 'B':
2410 case 'b':
2411 {
2412 if (LocaleCompare(expression,"b") == 0)
2413 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2414 break;
2415 }
2416 case 'C':
2417 case 'c':
2418 {
2419 if (LocaleNCompare(expression,"ceil",4) == 0)
2420 {
2421 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2422 exception);
2423 return((MagickRealType) ceil((double) alpha));
2424 }
2425 if (LocaleNCompare(expression,"cosh",4) == 0)
2426 {
2427 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2428 exception);
2429 return((MagickRealType) cosh((double) alpha));
2430 }
2431 if (LocaleNCompare(expression,"cos",3) == 0)
2432 {
2433 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2434 exception);
2435 return((MagickRealType) cos((double) alpha));
2436 }
2437 if (LocaleCompare(expression,"c") == 0)
2438 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2439 break;
2440 }
2441 case 'D':
2442 case 'd':
2443 {
2444 if (LocaleNCompare(expression,"debug",5) == 0)
2445 {
2446 const char
2447 *type;
2448
2449 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2450 exception);
2451 if (fx_info->images->colorspace == CMYKColorspace)
2452 switch (channel)
2453 {
cristy0568ffc2011-07-25 16:54:14 +00002454 case CyanPixelChannel: type="cyan"; break;
2455 case MagentaPixelChannel: type="magenta"; break;
2456 case YellowPixelChannel: type="yellow"; break;
2457 case AlphaPixelChannel: type="opacity"; break;
2458 case BlackPixelChannel: type="black"; break;
cristy3ed852e2009-09-05 21:47:34 +00002459 default: type="unknown"; break;
2460 }
2461 else
2462 switch (channel)
2463 {
cristy0568ffc2011-07-25 16:54:14 +00002464 case RedPixelChannel: type="red"; break;
2465 case GreenPixelChannel: type="green"; break;
2466 case BluePixelChannel: type="blue"; break;
2467 case AlphaPixelChannel: type="opacity"; break;
cristy3ed852e2009-09-05 21:47:34 +00002468 default: type="unknown"; break;
2469 }
2470 (void) CopyMagickString(subexpression,expression+6,MaxTextExtent);
2471 if (strlen(subexpression) > 1)
2472 subexpression[strlen(subexpression)-1]='\0';
2473 if (fx_info->file != (FILE *) NULL)
cristy1707c6c2012-01-18 23:30:54 +00002474 (void) FormatLocaleFile(fx_info->file,"%s[%.20g,%.20g].%s: "
2475 "%s=%.*g\n",fx_info->images->filename,(double) x,(double) y,type,
2476 subexpression,GetMagickPrecision(),(double) alpha);
cristy3ed852e2009-09-05 21:47:34 +00002477 return(0.0);
2478 }
cristy5597a8d2011-11-04 00:25:32 +00002479 if (LocaleNCompare(expression,"drc",3) == 0)
2480 {
2481 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2482 exception);
2483 return((MagickRealType) (alpha/(*beta*(alpha-1.0)+1.0)));
2484 }
cristy3ed852e2009-09-05 21:47:34 +00002485 break;
2486 }
2487 case 'E':
2488 case 'e':
2489 {
2490 if (LocaleCompare(expression,"epsilon") == 0)
2491 return((MagickRealType) MagickEpsilon);
2492 if (LocaleNCompare(expression,"exp",3) == 0)
2493 {
2494 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2495 exception);
2496 return((MagickRealType) exp((double) alpha));
2497 }
2498 if (LocaleCompare(expression,"e") == 0)
2499 return((MagickRealType) 2.7182818284590452354);
2500 break;
2501 }
2502 case 'F':
2503 case 'f':
2504 {
2505 if (LocaleNCompare(expression,"floor",5) == 0)
2506 {
2507 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2508 exception);
2509 return((MagickRealType) floor((double) alpha));
2510 }
2511 break;
2512 }
2513 case 'G':
2514 case 'g':
2515 {
cristy9eeedea2011-11-02 19:04:05 +00002516 if (LocaleNCompare(expression,"gauss",5) == 0)
2517 {
2518 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2519 exception);
2520 gamma=exp((double) (-alpha*alpha/2.0))/sqrt(2.0*MagickPI);
2521 return((MagickRealType) gamma);
2522 }
cristyb0aad4c2011-11-02 19:30:35 +00002523 if (LocaleNCompare(expression,"gcd",3) == 0)
2524 {
2525 MagickOffsetType
2526 gcd;
2527
2528 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2529 exception);
cristy1707c6c2012-01-18 23:30:54 +00002530 gcd=FxGCD((MagickOffsetType) (alpha+0.5),(MagickOffsetType) (*beta+
2531 0.5));
cristyb0aad4c2011-11-02 19:30:35 +00002532 return((MagickRealType) gcd);
2533 }
cristy3ed852e2009-09-05 21:47:34 +00002534 if (LocaleCompare(expression,"g") == 0)
2535 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2536 break;
2537 }
2538 case 'H':
2539 case 'h':
2540 {
2541 if (LocaleCompare(expression,"h") == 0)
2542 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2543 if (LocaleCompare(expression,"hue") == 0)
2544 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2545 if (LocaleNCompare(expression,"hypot",5) == 0)
2546 {
2547 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2548 exception);
2549 return((MagickRealType) hypot((double) alpha,(double) *beta));
2550 }
2551 break;
2552 }
2553 case 'K':
2554 case 'k':
2555 {
2556 if (LocaleCompare(expression,"k") == 0)
2557 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2558 break;
2559 }
2560 case 'I':
2561 case 'i':
2562 {
2563 if (LocaleCompare(expression,"intensity") == 0)
2564 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2565 if (LocaleNCompare(expression,"int",3) == 0)
2566 {
2567 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2568 exception);
cristy16788e42010-08-13 13:44:26 +00002569 return((MagickRealType) floor(alpha));
cristy3ed852e2009-09-05 21:47:34 +00002570 }
cristy82b20722011-11-05 21:52:36 +00002571#if defined(MAGICKCORE_HAVE_ISNAN)
cristy639399c2011-11-02 19:16:15 +00002572 if (LocaleNCompare(expression,"isnan",5) == 0)
2573 {
2574 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2575 exception);
cristy17a10202011-11-02 19:17:04 +00002576 return((MagickRealType) !!isnan((double) alpha));
cristy639399c2011-11-02 19:16:15 +00002577 }
cristy82b20722011-11-05 21:52:36 +00002578#endif
cristy3ed852e2009-09-05 21:47:34 +00002579 if (LocaleCompare(expression,"i") == 0)
2580 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2581 break;
2582 }
2583 case 'J':
2584 case 'j':
2585 {
2586 if (LocaleCompare(expression,"j") == 0)
2587 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
cristy161b9262010-03-20 19:34:32 +00002588#if defined(MAGICKCORE_HAVE_J0)
cristyee56cf12010-03-01 22:17:06 +00002589 if (LocaleNCompare(expression,"j0",2) == 0)
2590 {
2591 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2592 exception);
2593 return((MagickRealType) j0((double) alpha));
2594 }
cristy161b9262010-03-20 19:34:32 +00002595#endif
2596#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002597 if (LocaleNCompare(expression,"j1",2) == 0)
2598 {
2599 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2600 exception);
2601 return((MagickRealType) j1((double) alpha));
2602 }
cristy161b9262010-03-20 19:34:32 +00002603#endif
cristyaa018fa2010-04-08 23:03:54 +00002604#if defined(MAGICKCORE_HAVE_J1)
cristya6a09e72010-03-02 14:51:02 +00002605 if (LocaleNCompare(expression,"jinc",4) == 0)
2606 {
2607 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2608 exception);
cristy0946a822010-03-12 17:14:58 +00002609 if (alpha == 0.0)
2610 return(1.0);
cristy1707c6c2012-01-18 23:30:54 +00002611 gamma=(MagickRealType) (2.0*j1((double) (MagickPI*alpha))/(MagickPI*
2612 alpha));
cristyfce2f7b2010-03-12 00:29:49 +00002613 return(gamma);
cristya6a09e72010-03-02 14:51:02 +00002614 }
cristyaa018fa2010-04-08 23:03:54 +00002615#endif
cristy3ed852e2009-09-05 21:47:34 +00002616 break;
2617 }
2618 case 'L':
2619 case 'l':
2620 {
2621 if (LocaleNCompare(expression,"ln",2) == 0)
2622 {
2623 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2624 exception);
2625 return((MagickRealType) log((double) alpha));
2626 }
cristyc8ed5322010-08-31 12:07:59 +00002627 if (LocaleNCompare(expression,"logtwo",6) == 0)
cristy3ed852e2009-09-05 21:47:34 +00002628 {
cristyc8ed5322010-08-31 12:07:59 +00002629 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
cristy3ed852e2009-09-05 21:47:34 +00002630 exception);
2631 return((MagickRealType) log10((double) alpha))/log10(2.0);
2632 }
2633 if (LocaleNCompare(expression,"log",3) == 0)
2634 {
2635 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2636 exception);
2637 return((MagickRealType) log10((double) alpha));
2638 }
2639 if (LocaleCompare(expression,"lightness") == 0)
2640 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2641 break;
2642 }
2643 case 'M':
2644 case 'm':
2645 {
2646 if (LocaleCompare(expression,"MaxRGB") == 0)
2647 return((MagickRealType) QuantumRange);
2648 if (LocaleNCompare(expression,"maxima",6) == 0)
2649 break;
2650 if (LocaleNCompare(expression,"max",3) == 0)
cristy984049c2011-11-03 18:34:58 +00002651 {
2652 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2653 exception);
2654 return(alpha > *beta ? alpha : *beta);
2655 }
cristy3ed852e2009-09-05 21:47:34 +00002656 if (LocaleNCompare(expression,"minima",6) == 0)
2657 break;
2658 if (LocaleNCompare(expression,"min",3) == 0)
cristy984049c2011-11-03 18:34:58 +00002659 {
2660 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2661 exception);
2662 return(alpha < *beta ? alpha : *beta);
2663 }
cristy3ed852e2009-09-05 21:47:34 +00002664 if (LocaleNCompare(expression,"mod",3) == 0)
2665 {
2666 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2667 exception);
cristy984049c2011-11-03 18:34:58 +00002668 gamma=alpha-floor((double) (alpha/(*beta)))*(*beta);
2669 return(gamma);
cristy3ed852e2009-09-05 21:47:34 +00002670 }
2671 if (LocaleCompare(expression,"m") == 0)
2672 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2673 break;
2674 }
2675 case 'N':
2676 case 'n':
2677 {
cristyad3502e2011-11-02 19:10:45 +00002678 if (LocaleNCompare(expression,"not",3) == 0)
2679 {
2680 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2681 exception);
2682 return((MagickRealType) (alpha < MagickEpsilon));
2683 }
cristy3ed852e2009-09-05 21:47:34 +00002684 if (LocaleCompare(expression,"n") == 0)
2685 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2686 break;
2687 }
2688 case 'O':
2689 case 'o':
2690 {
2691 if (LocaleCompare(expression,"Opaque") == 0)
2692 return(1.0);
2693 if (LocaleCompare(expression,"o") == 0)
2694 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2695 break;
2696 }
2697 case 'P':
2698 case 'p':
2699 {
cristy670aa3c2011-11-03 00:54:00 +00002700 if (LocaleCompare(expression,"phi") == 0)
2701 return((MagickRealType) MagickPHI);
cristy3ed852e2009-09-05 21:47:34 +00002702 if (LocaleCompare(expression,"pi") == 0)
2703 return((MagickRealType) MagickPI);
2704 if (LocaleNCompare(expression,"pow",3) == 0)
2705 {
2706 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2707 exception);
2708 return((MagickRealType) pow((double) alpha,(double) *beta));
2709 }
2710 if (LocaleCompare(expression,"p") == 0)
2711 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2712 break;
2713 }
2714 case 'Q':
2715 case 'q':
2716 {
2717 if (LocaleCompare(expression,"QuantumRange") == 0)
2718 return((MagickRealType) QuantumRange);
2719 if (LocaleCompare(expression,"QuantumScale") == 0)
2720 return((MagickRealType) QuantumScale);
2721 break;
2722 }
2723 case 'R':
2724 case 'r':
2725 {
2726 if (LocaleNCompare(expression,"rand",4) == 0)
2727 return((MagickRealType) GetPseudoRandomValue(fx_info->random_info));
2728 if (LocaleNCompare(expression,"round",5) == 0)
2729 {
2730 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2731 exception);
cristy16788e42010-08-13 13:44:26 +00002732 return((MagickRealType) floor((double) alpha+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002733 }
2734 if (LocaleCompare(expression,"r") == 0)
2735 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2736 break;
2737 }
2738 case 'S':
2739 case 's':
2740 {
2741 if (LocaleCompare(expression,"saturation") == 0)
2742 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2743 if (LocaleNCompare(expression,"sign",4) == 0)
2744 {
2745 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2746 exception);
2747 return(alpha < 0.0 ? -1.0 : 1.0);
2748 }
cristya6a09e72010-03-02 14:51:02 +00002749 if (LocaleNCompare(expression,"sinc",4) == 0)
2750 {
2751 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2752 exception);
2753 if (alpha == 0)
2754 return(1.0);
2755 gamma=(MagickRealType) (sin((double) (MagickPI*alpha))/
2756 (MagickPI*alpha));
2757 return(gamma);
2758 }
cristy3ed852e2009-09-05 21:47:34 +00002759 if (LocaleNCompare(expression,"sinh",4) == 0)
2760 {
2761 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2762 exception);
2763 return((MagickRealType) sinh((double) alpha));
2764 }
2765 if (LocaleNCompare(expression,"sin",3) == 0)
2766 {
2767 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2768 exception);
2769 return((MagickRealType) sin((double) alpha));
2770 }
2771 if (LocaleNCompare(expression,"sqrt",4) == 0)
2772 {
2773 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2774 exception);
2775 return((MagickRealType) sqrt((double) alpha));
2776 }
cristy9eeedea2011-11-02 19:04:05 +00002777 if (LocaleNCompare(expression,"squish",6) == 0)
2778 {
2779 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2780 exception);
cristyf7b28ea2012-04-27 11:22:49 +00002781 return((MagickRealType) (1.0/(1.0+exp((double) (-alpha)))));
cristy9eeedea2011-11-02 19:04:05 +00002782 }
cristy3ed852e2009-09-05 21:47:34 +00002783 if (LocaleCompare(expression,"s") == 0)
2784 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2785 break;
2786 }
2787 case 'T':
2788 case 't':
2789 {
2790 if (LocaleNCompare(expression,"tanh",4) == 0)
2791 {
2792 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2793 exception);
2794 return((MagickRealType) tanh((double) alpha));
2795 }
2796 if (LocaleNCompare(expression,"tan",3) == 0)
2797 {
2798 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2799 exception);
2800 return((MagickRealType) tan((double) alpha));
2801 }
2802 if (LocaleCompare(expression,"Transparent") == 0)
2803 return(0.0);
cristy16788e42010-08-13 13:44:26 +00002804 if (LocaleNCompare(expression,"trunc",5) == 0)
2805 {
2806 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2807 exception);
2808 if (alpha >= 0.0)
2809 return((MagickRealType) floor((double) alpha));
2810 return((MagickRealType) ceil((double) alpha));
2811 }
cristy3ed852e2009-09-05 21:47:34 +00002812 if (LocaleCompare(expression,"t") == 0)
2813 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2814 break;
2815 }
2816 case 'U':
2817 case 'u':
2818 {
2819 if (LocaleCompare(expression,"u") == 0)
2820 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2821 break;
2822 }
2823 case 'V':
2824 case 'v':
2825 {
2826 if (LocaleCompare(expression,"v") == 0)
2827 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2828 break;
2829 }
2830 case 'W':
2831 case 'w':
2832 {
cristy9eeedea2011-11-02 19:04:05 +00002833 if (LocaleNCompare(expression,"while",5) == 0)
2834 {
2835 do
2836 {
2837 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2838 exception);
2839 } while (fabs((double) alpha) >= MagickEpsilon);
2840 return((MagickRealType) *beta);
2841 }
cristy3ed852e2009-09-05 21:47:34 +00002842 if (LocaleCompare(expression,"w") == 0)
2843 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2844 break;
2845 }
2846 case 'Y':
2847 case 'y':
2848 {
2849 if (LocaleCompare(expression,"y") == 0)
2850 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2851 break;
2852 }
2853 case 'Z':
2854 case 'z':
2855 {
2856 if (LocaleCompare(expression,"z") == 0)
2857 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2858 break;
2859 }
2860 default:
2861 break;
2862 }
2863 q=(char *) expression;
cristydbdd0e32011-11-04 23:29:40 +00002864 alpha=InterpretSiPrefixValue(expression,&q);
cristy3ed852e2009-09-05 21:47:34 +00002865 if (q == expression)
2866 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2867 return(alpha);
2868}
2869
cristy7832dc22011-09-05 01:21:53 +00002870MagickPrivate MagickBooleanType FxEvaluateExpression(FxInfo *fx_info,
cristy3ed852e2009-09-05 21:47:34 +00002871 MagickRealType *alpha,ExceptionInfo *exception)
2872{
2873 MagickBooleanType
2874 status;
2875
cristy541ae572011-08-05 19:08:59 +00002876 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2877 exception);
cristy3ed852e2009-09-05 21:47:34 +00002878 return(status);
2879}
2880
2881MagickExport MagickBooleanType FxPreprocessExpression(FxInfo *fx_info,
2882 MagickRealType *alpha,ExceptionInfo *exception)
2883{
2884 FILE
2885 *file;
2886
2887 MagickBooleanType
2888 status;
2889
2890 file=fx_info->file;
2891 fx_info->file=(FILE *) NULL;
cristy541ae572011-08-05 19:08:59 +00002892 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2893 exception);
cristy3ed852e2009-09-05 21:47:34 +00002894 fx_info->file=file;
2895 return(status);
2896}
2897
cristy7832dc22011-09-05 01:21:53 +00002898MagickPrivate MagickBooleanType FxEvaluateChannelExpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002899 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristye85007d2010-06-06 22:51:36 +00002900 MagickRealType *alpha,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002901{
2902 MagickRealType
2903 beta;
2904
2905 beta=0.0;
2906 *alpha=FxEvaluateSubexpression(fx_info,channel,x,y,fx_info->expression,&beta,
2907 exception);
2908 return(exception->severity == OptionError ? MagickFalse : MagickTrue);
2909}
2910
2911/*
2912%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2913% %
2914% %
2915% %
2916% F x I m a g e %
2917% %
2918% %
2919% %
2920%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2921%
2922% FxImage() applies a mathematical expression to the specified image.
2923%
2924% The format of the FxImage method is:
2925%
2926% Image *FxImage(const Image *image,const char *expression,
2927% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002928%
2929% A description of each parameter follows:
2930%
2931% o image: the image.
2932%
cristy3ed852e2009-09-05 21:47:34 +00002933% o expression: A mathematical expression.
2934%
2935% o exception: return any errors or warnings in this structure.
2936%
2937*/
2938
2939static FxInfo **DestroyFxThreadSet(FxInfo **fx_info)
2940{
cristybb503372010-05-27 20:51:26 +00002941 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002942 i;
2943
2944 assert(fx_info != (FxInfo **) NULL);
cristyac245f82012-05-05 17:13:57 +00002945 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
cristy3ed852e2009-09-05 21:47:34 +00002946 if (fx_info[i] != (FxInfo *) NULL)
2947 fx_info[i]=DestroyFxInfo(fx_info[i]);
cristyb41ee102010-10-04 16:46:15 +00002948 fx_info=(FxInfo **) RelinquishMagickMemory(fx_info);
cristy3ed852e2009-09-05 21:47:34 +00002949 return(fx_info);
2950}
2951
2952static FxInfo **AcquireFxThreadSet(const Image *image,const char *expression,
2953 ExceptionInfo *exception)
2954{
2955 char
2956 *fx_expression;
2957
2958 FxInfo
2959 **fx_info;
2960
2961 MagickRealType
2962 alpha;
2963
cristybb503372010-05-27 20:51:26 +00002964 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002965 i;
2966
cristybb503372010-05-27 20:51:26 +00002967 size_t
cristy3ed852e2009-09-05 21:47:34 +00002968 number_threads;
2969
cristyac245f82012-05-05 17:13:57 +00002970 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
cristyb41ee102010-10-04 16:46:15 +00002971 fx_info=(FxInfo **) AcquireQuantumMemory(number_threads,sizeof(*fx_info));
cristy3ed852e2009-09-05 21:47:34 +00002972 if (fx_info == (FxInfo **) NULL)
2973 return((FxInfo **) NULL);
2974 (void) ResetMagickMemory(fx_info,0,number_threads*sizeof(*fx_info));
2975 if (*expression != '@')
2976 fx_expression=ConstantString(expression);
2977 else
2978 fx_expression=FileToString(expression+1,~0,exception);
cristybb503372010-05-27 20:51:26 +00002979 for (i=0; i < (ssize_t) number_threads; i++)
cristy3ed852e2009-09-05 21:47:34 +00002980 {
cristydb070952012-04-20 14:33:00 +00002981 fx_info[i]=AcquireFxInfo(image,fx_expression,exception);
cristy3ed852e2009-09-05 21:47:34 +00002982 if (fx_info[i] == (FxInfo *) NULL)
2983 return(DestroyFxThreadSet(fx_info));
2984 (void) FxPreprocessExpression(fx_info[i],&alpha,fx_info[i]->exception);
2985 }
2986 fx_expression=DestroyString(fx_expression);
2987 return(fx_info);
2988}
2989
2990MagickExport Image *FxImage(const Image *image,const char *expression,
2991 ExceptionInfo *exception)
2992{
cristy3ed852e2009-09-05 21:47:34 +00002993#define FxImageTag "Fx/Image"
2994
cristyfa112112010-01-04 17:48:07 +00002995 CacheView
cristy79cedc72011-07-25 00:41:15 +00002996 *fx_view,
2997 *image_view;
cristyfa112112010-01-04 17:48:07 +00002998
cristy3ed852e2009-09-05 21:47:34 +00002999 FxInfo
cristyfa112112010-01-04 17:48:07 +00003000 **restrict fx_info;
cristy3ed852e2009-09-05 21:47:34 +00003001
3002 Image
3003 *fx_image;
3004
cristy3ed852e2009-09-05 21:47:34 +00003005 MagickBooleanType
3006 status;
3007
cristybb503372010-05-27 20:51:26 +00003008 MagickOffsetType
3009 progress;
3010
cristy3ed852e2009-09-05 21:47:34 +00003011 MagickRealType
3012 alpha;
3013
cristybb503372010-05-27 20:51:26 +00003014 ssize_t
3015 y;
3016
cristy3ed852e2009-09-05 21:47:34 +00003017 assert(image != (Image *) NULL);
3018 assert(image->signature == MagickSignature);
3019 if (image->debug != MagickFalse)
3020 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy79cedc72011-07-25 00:41:15 +00003021 fx_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00003022 if (fx_image == (Image *) NULL)
3023 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003024 if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003025 {
cristy3ed852e2009-09-05 21:47:34 +00003026 fx_image=DestroyImage(fx_image);
3027 return((Image *) NULL);
3028 }
3029 fx_info=AcquireFxThreadSet(image,expression,exception);
3030 if (fx_info == (FxInfo **) NULL)
3031 {
3032 fx_image=DestroyImage(fx_image);
3033 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3034 }
3035 status=FxPreprocessExpression(fx_info[0],&alpha,exception);
3036 if (status == MagickFalse)
3037 {
3038 fx_image=DestroyImage(fx_image);
3039 fx_info=DestroyFxThreadSet(fx_info);
3040 return((Image *) NULL);
3041 }
3042 /*
3043 Fx image.
3044 */
3045 status=MagickTrue;
3046 progress=0;
cristydb070952012-04-20 14:33:00 +00003047 image_view=AcquireVirtualCacheView(image,exception);
3048 fx_view=AcquireAuthenticCacheView(fx_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00003049#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003050 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3051 if ((image->rows*image->columns) > 8192) \
3052 num_threads(GetMagickResourceLimit(ThreadResource))
cristy3ed852e2009-09-05 21:47:34 +00003053#endif
cristybb503372010-05-27 20:51:26 +00003054 for (y=0; y < (ssize_t) fx_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003055 {
cristy5c9e6f22010-09-17 17:31:01 +00003056 const int
3057 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00003058
cristy79cedc72011-07-25 00:41:15 +00003059 register const Quantum
3060 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003061
cristy4c08aed2011-07-01 19:47:50 +00003062 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003063 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003064
cristy79cedc72011-07-25 00:41:15 +00003065 register ssize_t
3066 x;
3067
cristy3ed852e2009-09-05 21:47:34 +00003068 if (status == MagickFalse)
3069 continue;
cristy79cedc72011-07-25 00:41:15 +00003070 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +00003071 q=QueueCacheViewAuthenticPixels(fx_view,0,y,fx_image->columns,1,exception);
cristy79cedc72011-07-25 00:41:15 +00003072 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003073 {
3074 status=MagickFalse;
3075 continue;
3076 }
cristybb503372010-05-27 20:51:26 +00003077 for (x=0; x < (ssize_t) fx_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003078 {
cristy79cedc72011-07-25 00:41:15 +00003079 register ssize_t
3080 i;
3081
cristy177e41c2012-04-15 15:08:25 +00003082 if (GetPixelMask(image,p) != 0)
3083 {
3084 p+=GetPixelChannels(image);
3085 q+=GetPixelChannels(fx_image);
3086 continue;
3087 }
cristy79cedc72011-07-25 00:41:15 +00003088 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3089 {
3090 MagickRealType
3091 alpha;
3092
3093 PixelChannel
3094 channel;
3095
3096 PixelTrait
3097 fx_traits,
3098 traits;
3099
cristye2a912b2011-12-05 20:02:07 +00003100 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003101 traits=GetPixelChannelMapTraits(image,channel);
cristy79cedc72011-07-25 00:41:15 +00003102 fx_traits=GetPixelChannelMapTraits(fx_image,channel);
cristy010d7d12011-08-31 01:02:48 +00003103 if ((traits == UndefinedPixelTrait) ||
3104 (fx_traits == UndefinedPixelTrait))
cristy79cedc72011-07-25 00:41:15 +00003105 continue;
cristy177e41c2012-04-15 15:08:25 +00003106 if ((fx_traits & CopyPixelTrait) != 0)
cristy79cedc72011-07-25 00:41:15 +00003107 {
cristy0beccfa2011-09-25 20:47:53 +00003108 SetPixelChannel(fx_image,channel,p[i],q);
cristy79cedc72011-07-25 00:41:15 +00003109 continue;
3110 }
3111 alpha=0.0;
cristya382aca2011-12-06 18:22:48 +00003112 (void) FxEvaluateChannelExpression(fx_info[id],channel,x,y,&alpha,
3113 exception);
cristyb3a73b52011-07-26 01:34:43 +00003114 q[i]=ClampToQuantum((MagickRealType) QuantumRange*alpha);
cristy79cedc72011-07-25 00:41:15 +00003115 }
3116 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003117 q+=GetPixelChannels(fx_image);
cristy3ed852e2009-09-05 21:47:34 +00003118 }
3119 if (SyncCacheViewAuthenticPixels(fx_view,exception) == MagickFalse)
3120 status=MagickFalse;
3121 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3122 {
3123 MagickBooleanType
3124 proceed;
3125
cristyb5d5f722009-11-04 03:03:49 +00003126#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003127 #pragma omp critical (MagickCore_FxImage)
cristy3ed852e2009-09-05 21:47:34 +00003128#endif
3129 proceed=SetImageProgress(image,FxImageTag,progress++,image->rows);
3130 if (proceed == MagickFalse)
3131 status=MagickFalse;
3132 }
3133 }
cristy3ed852e2009-09-05 21:47:34 +00003134 fx_view=DestroyCacheView(fx_view);
cristy79cedc72011-07-25 00:41:15 +00003135 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003136 fx_info=DestroyFxThreadSet(fx_info);
3137 if (status == MagickFalse)
3138 fx_image=DestroyImage(fx_image);
3139 return(fx_image);
3140}
3141
3142/*
3143%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3144% %
3145% %
3146% %
3147% I m p l o d e I m a g e %
3148% %
3149% %
3150% %
3151%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3152%
3153% ImplodeImage() creates a new image that is a copy of an existing
3154% one with the image pixels "implode" by the specified percentage. It
3155% allocates the memory necessary for the new Image structure and returns a
3156% pointer to the new image.
3157%
3158% The format of the ImplodeImage method is:
3159%
3160% Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003161% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003162%
3163% A description of each parameter follows:
3164%
3165% o implode_image: Method ImplodeImage returns a pointer to the image
3166% after it is implode. A null image is returned if there is a memory
3167% shortage.
3168%
3169% o image: the image.
3170%
3171% o amount: Define the extent of the implosion.
3172%
cristy76f512e2011-09-12 01:26:56 +00003173% o method: the pixel interpolation method.
3174%
cristy3ed852e2009-09-05 21:47:34 +00003175% o exception: return any errors or warnings in this structure.
3176%
3177*/
3178MagickExport Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003179 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003180{
3181#define ImplodeImageTag "Implode/Image"
3182
cristyfa112112010-01-04 17:48:07 +00003183 CacheView
3184 *image_view,
3185 *implode_view;
3186
cristy3ed852e2009-09-05 21:47:34 +00003187 Image
3188 *implode_image;
3189
cristy3ed852e2009-09-05 21:47:34 +00003190 MagickBooleanType
3191 status;
3192
cristybb503372010-05-27 20:51:26 +00003193 MagickOffsetType
3194 progress;
3195
cristy3ed852e2009-09-05 21:47:34 +00003196 MagickRealType
3197 radius;
3198
3199 PointInfo
3200 center,
3201 scale;
3202
cristybb503372010-05-27 20:51:26 +00003203 ssize_t
3204 y;
3205
cristy3ed852e2009-09-05 21:47:34 +00003206 /*
3207 Initialize implode image attributes.
3208 */
3209 assert(image != (Image *) NULL);
3210 assert(image->signature == MagickSignature);
3211 if (image->debug != MagickFalse)
3212 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3213 assert(exception != (ExceptionInfo *) NULL);
3214 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00003215 implode_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3216 exception);
cristy3ed852e2009-09-05 21:47:34 +00003217 if (implode_image == (Image *) NULL)
3218 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003219 if (SetImageStorageClass(implode_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003220 {
cristy3ed852e2009-09-05 21:47:34 +00003221 implode_image=DestroyImage(implode_image);
3222 return((Image *) NULL);
3223 }
cristy4c08aed2011-07-01 19:47:50 +00003224 if (implode_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00003225 implode_image->matte=MagickTrue;
3226 /*
3227 Compute scaling factor.
3228 */
3229 scale.x=1.0;
3230 scale.y=1.0;
3231 center.x=0.5*image->columns;
3232 center.y=0.5*image->rows;
3233 radius=center.x;
3234 if (image->columns > image->rows)
3235 scale.y=(double) image->columns/(double) image->rows;
3236 else
3237 if (image->columns < image->rows)
3238 {
3239 scale.x=(double) image->rows/(double) image->columns;
3240 radius=center.y;
3241 }
3242 /*
3243 Implode image.
3244 */
3245 status=MagickTrue;
3246 progress=0;
cristydb070952012-04-20 14:33:00 +00003247 image_view=AcquireVirtualCacheView(image,exception);
3248 implode_view=AcquireAuthenticCacheView(implode_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00003249#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003250 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3251 if ((image->rows*image->columns) > 8192) \
3252 num_threads(GetMagickResourceLimit(ThreadResource))
cristy3ed852e2009-09-05 21:47:34 +00003253#endif
cristybb503372010-05-27 20:51:26 +00003254 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003255 {
cristy3ed852e2009-09-05 21:47:34 +00003256 MagickRealType
3257 distance;
3258
3259 PointInfo
3260 delta;
3261
cristy6d188022011-09-12 13:23:33 +00003262 register const Quantum
3263 *restrict p;
3264
cristybb503372010-05-27 20:51:26 +00003265 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003266 x;
3267
cristy4c08aed2011-07-01 19:47:50 +00003268 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003269 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003270
3271 if (status == MagickFalse)
3272 continue;
cristy6d188022011-09-12 13:23:33 +00003273 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +00003274 q=QueueCacheViewAuthenticPixels(implode_view,0,y,implode_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003275 exception);
cristy6d188022011-09-12 13:23:33 +00003276 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003277 {
3278 status=MagickFalse;
3279 continue;
3280 }
cristy3ed852e2009-09-05 21:47:34 +00003281 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00003282 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003283 {
cristy6d188022011-09-12 13:23:33 +00003284 register ssize_t
3285 i;
3286
cristy3ed852e2009-09-05 21:47:34 +00003287 /*
3288 Determine if the pixel is within an ellipse.
3289 */
cristy10a6c612012-01-29 21:41:05 +00003290 if (GetPixelMask(image,p) != 0)
3291 {
3292 p+=GetPixelChannels(image);
3293 q+=GetPixelChannels(implode_image);
3294 continue;
3295 }
cristy3ed852e2009-09-05 21:47:34 +00003296 delta.x=scale.x*(double) (x-center.x);
3297 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00003298 if (distance >= (radius*radius))
cristya6d7a9b2012-01-18 20:04:48 +00003299 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy6d188022011-09-12 13:23:33 +00003300 {
cristya6d7a9b2012-01-18 20:04:48 +00003301 PixelChannel
3302 channel;
3303
cristy1707c6c2012-01-18 23:30:54 +00003304 PixelTrait
3305 implode_traits,
3306 traits;
3307
cristya6d7a9b2012-01-18 20:04:48 +00003308 channel=GetPixelChannelMapChannel(image,i);
cristy1707c6c2012-01-18 23:30:54 +00003309 traits=GetPixelChannelMapTraits(image,channel);
3310 implode_traits=GetPixelChannelMapTraits(implode_image,channel);
3311 if ((traits == UndefinedPixelTrait) ||
3312 (implode_traits == UndefinedPixelTrait))
3313 continue;
cristya6d7a9b2012-01-18 20:04:48 +00003314 SetPixelChannel(implode_image,channel,p[i],q);
cristy6d188022011-09-12 13:23:33 +00003315 }
3316 else
cristy3ed852e2009-09-05 21:47:34 +00003317 {
3318 double
3319 factor;
3320
3321 /*
3322 Implode the pixel.
3323 */
3324 factor=1.0;
3325 if (distance > 0.0)
cristy1707c6c2012-01-18 23:30:54 +00003326 factor=pow(sin((double) (MagickPI*sqrt((double) distance)/radius/
3327 2)),-amount);
cristy76f512e2011-09-12 01:26:56 +00003328 status=InterpolatePixelChannels(image,image_view,implode_image,method,
3329 (double) (factor*delta.x/scale.x+center.x),(double) (factor*delta.y/
3330 scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00003331 }
cristy6d188022011-09-12 13:23:33 +00003332 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003333 q+=GetPixelChannels(implode_image);
cristy3ed852e2009-09-05 21:47:34 +00003334 }
3335 if (SyncCacheViewAuthenticPixels(implode_view,exception) == MagickFalse)
3336 status=MagickFalse;
3337 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3338 {
3339 MagickBooleanType
3340 proceed;
3341
cristyb5d5f722009-11-04 03:03:49 +00003342#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003343 #pragma omp critical (MagickCore_ImplodeImage)
cristy3ed852e2009-09-05 21:47:34 +00003344#endif
3345 proceed=SetImageProgress(image,ImplodeImageTag,progress++,image->rows);
3346 if (proceed == MagickFalse)
3347 status=MagickFalse;
3348 }
3349 }
3350 implode_view=DestroyCacheView(implode_view);
3351 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003352 if (status == MagickFalse)
3353 implode_image=DestroyImage(implode_image);
3354 return(implode_image);
3355}
3356
3357/*
3358%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3359% %
3360% %
3361% %
3362% M o r p h I m a g e s %
3363% %
3364% %
3365% %
3366%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3367%
3368% The MorphImages() method requires a minimum of two images. The first
3369% image is transformed into the second by a number of intervening images
3370% as specified by frames.
3371%
3372% The format of the MorphImage method is:
3373%
cristybb503372010-05-27 20:51:26 +00003374% Image *MorphImages(const Image *image,const size_t number_frames,
cristy3ed852e2009-09-05 21:47:34 +00003375% ExceptionInfo *exception)
3376%
3377% A description of each parameter follows:
3378%
3379% o image: the image.
3380%
3381% o number_frames: Define the number of in-between image to generate.
3382% The more in-between frames, the smoother the morph.
3383%
3384% o exception: return any errors or warnings in this structure.
3385%
3386*/
3387MagickExport Image *MorphImages(const Image *image,
cristybb503372010-05-27 20:51:26 +00003388 const size_t number_frames,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003389{
3390#define MorphImageTag "Morph/Image"
3391
3392 Image
3393 *morph_image,
3394 *morph_images;
3395
cristy9d314ff2011-03-09 01:30:28 +00003396 MagickBooleanType
3397 status;
cristy3ed852e2009-09-05 21:47:34 +00003398
3399 MagickOffsetType
3400 scene;
3401
3402 MagickRealType
3403 alpha,
3404 beta;
3405
3406 register const Image
3407 *next;
3408
cristybb503372010-05-27 20:51:26 +00003409 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003410 i;
3411
cristy9d314ff2011-03-09 01:30:28 +00003412 ssize_t
3413 y;
cristy3ed852e2009-09-05 21:47:34 +00003414
3415 /*
3416 Clone first frame in sequence.
3417 */
3418 assert(image != (Image *) NULL);
3419 assert(image->signature == MagickSignature);
3420 if (image->debug != MagickFalse)
3421 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3422 assert(exception != (ExceptionInfo *) NULL);
3423 assert(exception->signature == MagickSignature);
3424 morph_images=CloneImage(image,0,0,MagickTrue,exception);
3425 if (morph_images == (Image *) NULL)
3426 return((Image *) NULL);
3427 if (GetNextImageInList(image) == (Image *) NULL)
3428 {
3429 /*
3430 Morph single image.
3431 */
cristybb503372010-05-27 20:51:26 +00003432 for (i=1; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003433 {
3434 morph_image=CloneImage(image,0,0,MagickTrue,exception);
3435 if (morph_image == (Image *) NULL)
3436 {
3437 morph_images=DestroyImageList(morph_images);
3438 return((Image *) NULL);
3439 }
3440 AppendImageToList(&morph_images,morph_image);
cristy8b27a6d2010-02-14 03:31:15 +00003441 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003442 {
cristy8b27a6d2010-02-14 03:31:15 +00003443 MagickBooleanType
3444 proceed;
3445
cristybb503372010-05-27 20:51:26 +00003446 proceed=SetImageProgress(image,MorphImageTag,(MagickOffsetType) i,
3447 number_frames);
cristy8b27a6d2010-02-14 03:31:15 +00003448 if (proceed == MagickFalse)
3449 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00003450 }
3451 }
3452 return(GetFirstImageInList(morph_images));
3453 }
3454 /*
3455 Morph image sequence.
3456 */
3457 status=MagickTrue;
3458 scene=0;
3459 next=image;
3460 for ( ; GetNextImageInList(next) != (Image *) NULL; next=GetNextImageInList(next))
3461 {
cristybb503372010-05-27 20:51:26 +00003462 for (i=0; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003463 {
3464 CacheView
3465 *image_view,
3466 *morph_view;
3467
3468 beta=(MagickRealType) (i+1.0)/(MagickRealType) (number_frames+1.0);
3469 alpha=1.0-beta;
cristy15b98cd2010-09-12 19:42:50 +00003470 morph_image=ResizeImage(next,(size_t) (alpha*next->columns+beta*
cristyaa2c16c2012-03-25 22:21:35 +00003471 GetNextImageInList(next)->columns+0.5),(size_t) (alpha*next->rows+beta*
3472 GetNextImageInList(next)->rows+0.5),next->filter,exception);
cristy3ed852e2009-09-05 21:47:34 +00003473 if (morph_image == (Image *) NULL)
3474 {
3475 morph_images=DestroyImageList(morph_images);
3476 return((Image *) NULL);
3477 }
cristy1707c6c2012-01-18 23:30:54 +00003478 status=SetImageStorageClass(morph_image,DirectClass,exception);
3479 if (status == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003480 {
cristy3ed852e2009-09-05 21:47:34 +00003481 morph_image=DestroyImage(morph_image);
3482 return((Image *) NULL);
3483 }
3484 AppendImageToList(&morph_images,morph_image);
3485 morph_images=GetLastImageInList(morph_images);
cristy15b98cd2010-09-12 19:42:50 +00003486 morph_image=ResizeImage(GetNextImageInList(next),morph_images->columns,
cristyaa2c16c2012-03-25 22:21:35 +00003487 morph_images->rows,GetNextImageInList(next)->filter,exception);
cristy3ed852e2009-09-05 21:47:34 +00003488 if (morph_image == (Image *) NULL)
3489 {
3490 morph_images=DestroyImageList(morph_images);
3491 return((Image *) NULL);
3492 }
cristydb070952012-04-20 14:33:00 +00003493 image_view=AcquireVirtualCacheView(morph_image,exception);
3494 morph_view=AcquireAuthenticCacheView(morph_images,exception);
cristyb5d5f722009-11-04 03:03:49 +00003495#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003496 #pragma omp parallel for schedule(static,4) shared(status) \
3497 if ((morph_image->rows*morph_image->columns) > 8192) \
3498 num_threads(GetMagickResourceLimit(ThreadResource))
cristy3ed852e2009-09-05 21:47:34 +00003499#endif
cristybb503372010-05-27 20:51:26 +00003500 for (y=0; y < (ssize_t) morph_images->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003501 {
3502 MagickBooleanType
3503 sync;
3504
cristy4c08aed2011-07-01 19:47:50 +00003505 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003506 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003507
cristybb503372010-05-27 20:51:26 +00003508 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003509 x;
3510
cristy4c08aed2011-07-01 19:47:50 +00003511 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003512 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003513
3514 if (status == MagickFalse)
3515 continue;
3516 p=GetCacheViewVirtualPixels(image_view,0,y,morph_image->columns,1,
3517 exception);
3518 q=GetCacheViewAuthenticPixels(morph_view,0,y,morph_images->columns,1,
3519 exception);
cristy4c08aed2011-07-01 19:47:50 +00003520 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003521 {
3522 status=MagickFalse;
3523 continue;
3524 }
cristybb503372010-05-27 20:51:26 +00003525 for (x=0; x < (ssize_t) morph_images->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003526 {
cristy1707c6c2012-01-18 23:30:54 +00003527 register ssize_t
3528 i;
3529
cristy177e41c2012-04-15 15:08:25 +00003530 if (GetPixelMask(image,p) != 0)
3531 {
3532 p+=GetPixelChannels(image);
3533 q+=GetPixelChannels(morph_image);
3534 continue;
3535 }
cristy10a6c612012-01-29 21:41:05 +00003536 for (i=0; i < (ssize_t) GetPixelChannels(morph_image); i++)
cristy1707c6c2012-01-18 23:30:54 +00003537 {
3538 PixelChannel
3539 channel;
3540
3541 PixelTrait
3542 morph_traits,
3543 traits;
3544
3545 channel=GetPixelChannelMapChannel(image,i);
3546 traits=GetPixelChannelMapTraits(image,channel);
3547 morph_traits=GetPixelChannelMapTraits(morph_image,channel);
3548 if ((traits == UndefinedPixelTrait) ||
3549 (morph_traits == UndefinedPixelTrait))
3550 continue;
cristy177e41c2012-04-15 15:08:25 +00003551 if ((morph_traits & CopyPixelTrait) != 0)
cristy1707c6c2012-01-18 23:30:54 +00003552 {
3553 SetPixelChannel(morph_image,channel,p[i],q);
3554 continue;
3555 }
3556 SetPixelChannel(morph_image,channel,ClampToQuantum(alpha*
3557 GetPixelChannel(morph_images,channel,q)+beta*p[i]),q);
3558 }
cristyed231572011-07-14 02:18:59 +00003559 p+=GetPixelChannels(morph_image);
3560 q+=GetPixelChannels(morph_images);
cristy3ed852e2009-09-05 21:47:34 +00003561 }
3562 sync=SyncCacheViewAuthenticPixels(morph_view,exception);
3563 if (sync == MagickFalse)
3564 status=MagickFalse;
3565 }
3566 morph_view=DestroyCacheView(morph_view);
3567 image_view=DestroyCacheView(image_view);
3568 morph_image=DestroyImage(morph_image);
3569 }
cristybb503372010-05-27 20:51:26 +00003570 if (i < (ssize_t) number_frames)
cristy3ed852e2009-09-05 21:47:34 +00003571 break;
3572 /*
3573 Clone last frame in sequence.
3574 */
3575 morph_image=CloneImage(GetNextImageInList(next),0,0,MagickTrue,exception);
3576 if (morph_image == (Image *) NULL)
3577 {
3578 morph_images=DestroyImageList(morph_images);
3579 return((Image *) NULL);
3580 }
3581 AppendImageToList(&morph_images,morph_image);
3582 morph_images=GetLastImageInList(morph_images);
3583 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3584 {
3585 MagickBooleanType
3586 proceed;
3587
cristyb5d5f722009-11-04 03:03:49 +00003588#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003589 #pragma omp critical (MagickCore_MorphImages)
cristy3ed852e2009-09-05 21:47:34 +00003590#endif
3591 proceed=SetImageProgress(image,MorphImageTag,scene,
3592 GetImageListLength(image));
3593 if (proceed == MagickFalse)
3594 status=MagickFalse;
3595 }
3596 scene++;
3597 }
3598 if (GetNextImageInList(next) != (Image *) NULL)
3599 {
3600 morph_images=DestroyImageList(morph_images);
3601 return((Image *) NULL);
3602 }
3603 return(GetFirstImageInList(morph_images));
3604}
3605
3606/*
3607%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3608% %
3609% %
3610% %
3611% P l a s m a I m a g e %
3612% %
3613% %
3614% %
3615%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3616%
3617% PlasmaImage() initializes an image with plasma fractal values. The image
3618% must be initialized with a base color and the random number generator
3619% seeded before this method is called.
3620%
3621% The format of the PlasmaImage method is:
3622%
3623% MagickBooleanType PlasmaImage(Image *image,const SegmentInfo *segment,
cristy5cbc0162011-08-29 00:36:28 +00003624% size_t attenuate,size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003625%
3626% A description of each parameter follows:
3627%
3628% o image: the image.
3629%
3630% o segment: Define the region to apply plasma fractals values.
3631%
glennrp7dae1ca2010-09-16 12:17:35 +00003632% o attenuate: Define the plasma attenuation factor.
cristy3ed852e2009-09-05 21:47:34 +00003633%
3634% o depth: Limit the plasma recursion depth.
3635%
cristy5cbc0162011-08-29 00:36:28 +00003636% o exception: return any errors or warnings in this structure.
3637%
cristy3ed852e2009-09-05 21:47:34 +00003638*/
3639
3640static inline Quantum PlasmaPixel(RandomInfo *random_info,
3641 const MagickRealType pixel,const MagickRealType noise)
3642{
3643 Quantum
3644 plasma;
3645
cristyce70c172010-01-07 17:15:30 +00003646 plasma=ClampToQuantum(pixel+noise*GetPseudoRandomValue(random_info)-
cristy3ed852e2009-09-05 21:47:34 +00003647 noise/2.0);
3648 return(plasma);
3649}
3650
cristyda1f9c12011-10-02 21:39:49 +00003651static MagickBooleanType PlasmaImageProxy(Image *image,CacheView *image_view,
3652 CacheView *u_view,CacheView *v_view,RandomInfo *random_info,
3653 const SegmentInfo *segment,size_t attenuate,size_t depth,
3654 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003655{
cristy3ed852e2009-09-05 21:47:34 +00003656 MagickRealType
3657 plasma;
3658
cristyda1f9c12011-10-02 21:39:49 +00003659 PixelChannel
3660 channel;
3661
3662 PixelTrait
3663 traits;
3664
3665 register const Quantum
3666 *restrict u,
3667 *restrict v;
3668
3669 register Quantum
3670 *restrict q;
3671
3672 register ssize_t
3673 i;
cristy3ed852e2009-09-05 21:47:34 +00003674
cristy9d314ff2011-03-09 01:30:28 +00003675 ssize_t
3676 x,
3677 x_mid,
3678 y,
3679 y_mid;
3680
cristy3ed852e2009-09-05 21:47:34 +00003681 if (((segment->x2-segment->x1) == 0.0) && ((segment->y2-segment->y1) == 0.0))
3682 return(MagickTrue);
3683 if (depth != 0)
3684 {
3685 SegmentInfo
3686 local_info;
3687
3688 /*
3689 Divide the area into quadrants and recurse.
3690 */
3691 depth--;
3692 attenuate++;
cristybb503372010-05-27 20:51:26 +00003693 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3694 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003695 local_info=(*segment);
3696 local_info.x2=(double) x_mid;
3697 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003698 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3699 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003700 local_info=(*segment);
3701 local_info.y1=(double) y_mid;
3702 local_info.x2=(double) x_mid;
cristyda1f9c12011-10-02 21:39:49 +00003703 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3704 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003705 local_info=(*segment);
3706 local_info.x1=(double) x_mid;
3707 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003708 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3709 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003710 local_info=(*segment);
3711 local_info.x1=(double) x_mid;
3712 local_info.y1=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003713 return(PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3714 &local_info,attenuate,depth,exception));
cristy3ed852e2009-09-05 21:47:34 +00003715 }
cristybb503372010-05-27 20:51:26 +00003716 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3717 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003718 if ((segment->x1 == (double) x_mid) && (segment->x2 == (double) x_mid) &&
3719 (segment->y1 == (double) y_mid) && (segment->y2 == (double) y_mid))
3720 return(MagickFalse);
3721 /*
3722 Average pixels and apply plasma.
3723 */
cristy3ed852e2009-09-05 21:47:34 +00003724 plasma=(MagickRealType) QuantumRange/(2.0*attenuate);
3725 if ((segment->x1 != (double) x_mid) || (segment->x2 != (double) x_mid))
3726 {
cristy3ed852e2009-09-05 21:47:34 +00003727 /*
3728 Left pixel.
3729 */
cristybb503372010-05-27 20:51:26 +00003730 x=(ssize_t) ceil(segment->x1-0.5);
cristy1707c6c2012-01-18 23:30:54 +00003731 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),1,1,
3732 exception);
3733 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),1,1,
3734 exception);
cristyc5c6f662010-09-22 14:23:02 +00003735 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003736 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3737 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003738 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003739 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3740 {
cristye2a912b2011-12-05 20:02:07 +00003741 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003742 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003743 if (traits == UndefinedPixelTrait)
3744 continue;
3745 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3746 }
cristyc5c6f662010-09-22 14:23:02 +00003747 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003748 if (segment->x1 != segment->x2)
3749 {
3750 /*
3751 Right pixel.
3752 */
cristybb503372010-05-27 20:51:26 +00003753 x=(ssize_t) ceil(segment->x2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003754 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3755 1,1,exception);
3756 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3757 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003758 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003759 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3760 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003761 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003762 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3763 {
cristye2a912b2011-12-05 20:02:07 +00003764 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003765 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003766 if (traits == UndefinedPixelTrait)
3767 continue;
3768 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3769 }
cristyc5c6f662010-09-22 14:23:02 +00003770 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003771 }
3772 }
3773 if ((segment->y1 != (double) y_mid) || (segment->y2 != (double) y_mid))
3774 {
3775 if ((segment->x1 != (double) x_mid) || (segment->y2 != (double) y_mid))
3776 {
cristy3ed852e2009-09-05 21:47:34 +00003777 /*
3778 Bottom pixel.
3779 */
cristybb503372010-05-27 20:51:26 +00003780 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003781 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3782 1,1,exception);
3783 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3784 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003785 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003786 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3787 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003788 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003789 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3790 {
cristye2a912b2011-12-05 20:02:07 +00003791 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003792 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003793 if (traits == UndefinedPixelTrait)
3794 continue;
3795 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3796 }
cristyc5c6f662010-09-22 14:23:02 +00003797 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003798 }
3799 if (segment->y1 != segment->y2)
3800 {
cristy3ed852e2009-09-05 21:47:34 +00003801 /*
3802 Top pixel.
3803 */
cristybb503372010-05-27 20:51:26 +00003804 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003805 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3806 1,1,exception);
3807 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3808 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003809 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003810 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3811 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003812 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003813 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3814 {
cristye2a912b2011-12-05 20:02:07 +00003815 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003816 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003817 if (traits == UndefinedPixelTrait)
3818 continue;
3819 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3820 }
cristyc5c6f662010-09-22 14:23:02 +00003821 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003822 }
3823 }
3824 if ((segment->x1 != segment->x2) || (segment->y1 != segment->y2))
3825 {
cristy3ed852e2009-09-05 21:47:34 +00003826 /*
3827 Middle pixel.
3828 */
cristybb503372010-05-27 20:51:26 +00003829 x=(ssize_t) ceil(segment->x1-0.5);
3830 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003831 u=GetCacheViewVirtualPixels(u_view,x,y,1,1,exception);
cristybb503372010-05-27 20:51:26 +00003832 x=(ssize_t) ceil(segment->x2-0.5);
3833 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003834 v=GetCacheViewVirtualPixels(v_view,x,y,1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003835 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003836 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3837 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003838 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003839 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3840 {
cristye2a912b2011-12-05 20:02:07 +00003841 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003842 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003843 if (traits == UndefinedPixelTrait)
3844 continue;
3845 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3846 }
cristyc5c6f662010-09-22 14:23:02 +00003847 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003848 }
3849 if (((segment->x2-segment->x1) < 3.0) && ((segment->y2-segment->y1) < 3.0))
3850 return(MagickTrue);
3851 return(MagickFalse);
3852}
cristyda1f9c12011-10-02 21:39:49 +00003853
cristy3ed852e2009-09-05 21:47:34 +00003854MagickExport MagickBooleanType PlasmaImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00003855 const SegmentInfo *segment,size_t attenuate,size_t depth,
3856 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003857{
cristyc5c6f662010-09-22 14:23:02 +00003858 CacheView
cristyda1f9c12011-10-02 21:39:49 +00003859 *image_view,
3860 *u_view,
3861 *v_view;
cristyc5c6f662010-09-22 14:23:02 +00003862
cristy3ed852e2009-09-05 21:47:34 +00003863 MagickBooleanType
3864 status;
3865
3866 RandomInfo
3867 *random_info;
3868
3869 if (image->debug != MagickFalse)
3870 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3871 assert(image != (Image *) NULL);
3872 assert(image->signature == MagickSignature);
3873 if (image->debug != MagickFalse)
3874 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
cristy5cbc0162011-08-29 00:36:28 +00003875 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristyc5c6f662010-09-22 14:23:02 +00003876 return(MagickFalse);
cristydb070952012-04-20 14:33:00 +00003877 image_view=AcquireAuthenticCacheView(image,exception);
3878 u_view=AcquireVirtualCacheView(image,exception);
3879 v_view=AcquireVirtualCacheView(image,exception);
cristy3ed852e2009-09-05 21:47:34 +00003880 random_info=AcquireRandomInfo();
cristyda1f9c12011-10-02 21:39:49 +00003881 status=PlasmaImageProxy(image,image_view,u_view,v_view,random_info,segment,
3882 attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003883 random_info=DestroyRandomInfo(random_info);
cristyda1f9c12011-10-02 21:39:49 +00003884 v_view=DestroyCacheView(v_view);
3885 u_view=DestroyCacheView(u_view);
cristyc5c6f662010-09-22 14:23:02 +00003886 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003887 return(status);
3888}
3889
3890/*
3891%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3892% %
3893% %
3894% %
3895% P o l a r o i d I m a g e %
3896% %
3897% %
3898% %
3899%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3900%
3901% PolaroidImage() simulates a Polaroid picture.
3902%
3903% The format of the AnnotateImage method is:
3904%
3905% Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristye9e3d382011-12-14 01:50:13 +00003906% const char *caption,const double angle,
3907% const PixelInterpolateMethod method,ExceptionInfo exception)
cristy3ed852e2009-09-05 21:47:34 +00003908%
3909% A description of each parameter follows:
3910%
3911% o image: the image.
3912%
3913% o draw_info: the draw info.
3914%
cristye9e3d382011-12-14 01:50:13 +00003915% o caption: the Polaroid caption.
3916%
cristycee97112010-05-28 00:44:52 +00003917% o angle: Apply the effect along this angle.
cristy3ed852e2009-09-05 21:47:34 +00003918%
cristy5c4e2582011-09-11 19:21:03 +00003919% o method: the pixel interpolation method.
3920%
cristy3ed852e2009-09-05 21:47:34 +00003921% o exception: return any errors or warnings in this structure.
3922%
3923*/
3924MagickExport Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristye9e3d382011-12-14 01:50:13 +00003925 const char *caption,const double angle,const PixelInterpolateMethod method,
cristy5c4e2582011-09-11 19:21:03 +00003926 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003927{
cristy3ed852e2009-09-05 21:47:34 +00003928 Image
3929 *bend_image,
3930 *caption_image,
3931 *flop_image,
3932 *picture_image,
3933 *polaroid_image,
3934 *rotate_image,
3935 *trim_image;
3936
cristybb503372010-05-27 20:51:26 +00003937 size_t
cristy3ed852e2009-09-05 21:47:34 +00003938 height;
3939
cristy9d314ff2011-03-09 01:30:28 +00003940 ssize_t
3941 quantum;
3942
cristy3ed852e2009-09-05 21:47:34 +00003943 /*
3944 Simulate a Polaroid picture.
3945 */
3946 assert(image != (Image *) NULL);
3947 assert(image->signature == MagickSignature);
3948 if (image->debug != MagickFalse)
3949 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3950 assert(exception != (ExceptionInfo *) NULL);
3951 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00003952 quantum=(ssize_t) MagickMax(MagickMax((double) image->columns,(double)
cristy3ed852e2009-09-05 21:47:34 +00003953 image->rows)/25.0,10.0);
3954 height=image->rows+2*quantum;
3955 caption_image=(Image *) NULL;
cristye9e3d382011-12-14 01:50:13 +00003956 if (caption != (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003957 {
3958 char
cristye9e3d382011-12-14 01:50:13 +00003959 geometry[MaxTextExtent],
3960 *text;
cristy3ed852e2009-09-05 21:47:34 +00003961
3962 DrawInfo
3963 *annotate_info;
3964
cristy3ed852e2009-09-05 21:47:34 +00003965 MagickBooleanType
3966 status;
3967
cristy9d314ff2011-03-09 01:30:28 +00003968 ssize_t
3969 count;
3970
cristy3ed852e2009-09-05 21:47:34 +00003971 TypeMetric
3972 metrics;
3973
3974 /*
3975 Generate caption image.
3976 */
3977 caption_image=CloneImage(image,image->columns,1,MagickTrue,exception);
3978 if (caption_image == (Image *) NULL)
3979 return((Image *) NULL);
3980 annotate_info=CloneDrawInfo((const ImageInfo *) NULL,draw_info);
cristye9e3d382011-12-14 01:50:13 +00003981 text=InterpretImageProperties((ImageInfo *) NULL,(Image *) image,caption,
3982 exception);
3983 (void) CloneString(&annotate_info->text,text);
cristy6b1d05e2010-09-22 19:17:27 +00003984 count=FormatMagickCaption(caption_image,annotate_info,MagickTrue,&metrics,
cristye9e3d382011-12-14 01:50:13 +00003985 &text,exception);
3986 status=SetImageExtent(caption_image,image->columns,(size_t) ((count+1)*
3987 (metrics.ascent-metrics.descent)+0.5),exception);
cristy3ed852e2009-09-05 21:47:34 +00003988 if (status == MagickFalse)
3989 caption_image=DestroyImage(caption_image);
3990 else
3991 {
3992 caption_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00003993 (void) SetImageBackgroundColor(caption_image,exception);
cristye9e3d382011-12-14 01:50:13 +00003994 (void) CloneString(&annotate_info->text,text);
cristyb51dff52011-05-19 16:55:47 +00003995 (void) FormatLocaleString(geometry,MaxTextExtent,"+0+%g",
cristy3ed852e2009-09-05 21:47:34 +00003996 metrics.ascent);
3997 if (annotate_info->gravity == UndefinedGravity)
3998 (void) CloneString(&annotate_info->geometry,AcquireString(
3999 geometry));
cristy5cbc0162011-08-29 00:36:28 +00004000 (void) AnnotateImage(caption_image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00004001 height+=caption_image->rows;
4002 }
4003 annotate_info=DestroyDrawInfo(annotate_info);
cristye9e3d382011-12-14 01:50:13 +00004004 text=DestroyString(text);
cristy3ed852e2009-09-05 21:47:34 +00004005 }
4006 picture_image=CloneImage(image,image->columns+2*quantum,height,MagickTrue,
4007 exception);
4008 if (picture_image == (Image *) NULL)
4009 {
4010 if (caption_image != (Image *) NULL)
4011 caption_image=DestroyImage(caption_image);
4012 return((Image *) NULL);
4013 }
4014 picture_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00004015 (void) SetImageBackgroundColor(picture_image,exception);
cristy39172402012-03-30 13:04:39 +00004016 (void) CompositeImage(picture_image,image,OverCompositeOp,MagickTrue,quantum,
cristyfeb3e962012-03-29 17:25:55 +00004017 quantum,exception);
cristy3ed852e2009-09-05 21:47:34 +00004018 if (caption_image != (Image *) NULL)
4019 {
cristyfeb3e962012-03-29 17:25:55 +00004020 (void) CompositeImage(picture_image,caption_image,OverCompositeOp,
cristy39172402012-03-30 13:04:39 +00004021 MagickTrue,quantum,(ssize_t) (image->rows+3*quantum/2),exception);
cristy3ed852e2009-09-05 21:47:34 +00004022 caption_image=DestroyImage(caption_image);
4023 }
cristy9950d572011-10-01 18:22:35 +00004024 (void) QueryColorCompliance("none",AllCompliance,
4025 &picture_image->background_color,exception);
cristy63240882011-08-05 19:05:27 +00004026 (void) SetImageAlphaChannel(picture_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004027 rotate_image=RotateImage(picture_image,90.0,exception);
4028 picture_image=DestroyImage(picture_image);
4029 if (rotate_image == (Image *) NULL)
4030 return((Image *) NULL);
4031 picture_image=rotate_image;
4032 bend_image=WaveImage(picture_image,0.01*picture_image->rows,2.0*
cristy5c4e2582011-09-11 19:21:03 +00004033 picture_image->columns,method,exception);
cristy3ed852e2009-09-05 21:47:34 +00004034 picture_image=DestroyImage(picture_image);
4035 if (bend_image == (Image *) NULL)
4036 return((Image *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00004037 picture_image=bend_image;
4038 rotate_image=RotateImage(picture_image,-90.0,exception);
4039 picture_image=DestroyImage(picture_image);
4040 if (rotate_image == (Image *) NULL)
4041 return((Image *) NULL);
4042 picture_image=rotate_image;
4043 picture_image->background_color=image->background_color;
anthonyf46d4262012-03-26 03:30:34 +00004044 polaroid_image=ShadowImage(picture_image,80.0,2.0,quantum/3,quantum/3,
cristy3ed852e2009-09-05 21:47:34 +00004045 exception);
4046 if (polaroid_image == (Image *) NULL)
4047 {
4048 picture_image=DestroyImage(picture_image);
4049 return(picture_image);
4050 }
4051 flop_image=FlopImage(polaroid_image,exception);
4052 polaroid_image=DestroyImage(polaroid_image);
4053 if (flop_image == (Image *) NULL)
4054 {
4055 picture_image=DestroyImage(picture_image);
4056 return(picture_image);
4057 }
4058 polaroid_image=flop_image;
cristyfeb3e962012-03-29 17:25:55 +00004059 (void) CompositeImage(polaroid_image,picture_image,OverCompositeOp,
cristy39172402012-03-30 13:04:39 +00004060 MagickTrue,(ssize_t) (-0.01*picture_image->columns/2.0),0L,exception);
cristy3ed852e2009-09-05 21:47:34 +00004061 picture_image=DestroyImage(picture_image);
cristy9950d572011-10-01 18:22:35 +00004062 (void) QueryColorCompliance("none",AllCompliance,
4063 &polaroid_image->background_color,exception);
cristy3ed852e2009-09-05 21:47:34 +00004064 rotate_image=RotateImage(polaroid_image,angle,exception);
4065 polaroid_image=DestroyImage(polaroid_image);
4066 if (rotate_image == (Image *) NULL)
4067 return((Image *) NULL);
4068 polaroid_image=rotate_image;
4069 trim_image=TrimImage(polaroid_image,exception);
4070 polaroid_image=DestroyImage(polaroid_image);
4071 if (trim_image == (Image *) NULL)
4072 return((Image *) NULL);
4073 polaroid_image=trim_image;
4074 return(polaroid_image);
4075}
4076
4077/*
4078%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4079% %
4080% %
4081% %
cristy3ed852e2009-09-05 21:47:34 +00004082% S e p i a T o n e I m a g e %
4083% %
4084% %
4085% %
4086%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4087%
4088% MagickSepiaToneImage() applies a special effect to the image, similar to the
4089% effect achieved in a photo darkroom by sepia toning. Threshold ranges from
4090% 0 to QuantumRange and is a measure of the extent of the sepia toning. A
4091% threshold of 80% is a good starting point for a reasonable tone.
4092%
4093% The format of the SepiaToneImage method is:
4094%
4095% Image *SepiaToneImage(const Image *image,const double threshold,
4096% ExceptionInfo *exception)
4097%
4098% A description of each parameter follows:
4099%
4100% o image: the image.
4101%
4102% o threshold: the tone threshold.
4103%
4104% o exception: return any errors or warnings in this structure.
4105%
4106*/
4107MagickExport Image *SepiaToneImage(const Image *image,const double threshold,
4108 ExceptionInfo *exception)
4109{
4110#define SepiaToneImageTag "SepiaTone/Image"
4111
cristyc4c8d132010-01-07 01:58:38 +00004112 CacheView
4113 *image_view,
4114 *sepia_view;
4115
cristy3ed852e2009-09-05 21:47:34 +00004116 Image
4117 *sepia_image;
4118
cristy3ed852e2009-09-05 21:47:34 +00004119 MagickBooleanType
4120 status;
4121
cristybb503372010-05-27 20:51:26 +00004122 MagickOffsetType
4123 progress;
4124
4125 ssize_t
4126 y;
4127
cristy3ed852e2009-09-05 21:47:34 +00004128 /*
4129 Initialize sepia-toned image attributes.
4130 */
4131 assert(image != (const Image *) NULL);
4132 assert(image->signature == MagickSignature);
4133 if (image->debug != MagickFalse)
4134 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4135 assert(exception != (ExceptionInfo *) NULL);
4136 assert(exception->signature == MagickSignature);
cristy1707c6c2012-01-18 23:30:54 +00004137 sepia_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004138 if (sepia_image == (Image *) NULL)
4139 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004140 if (SetImageStorageClass(sepia_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004141 {
cristy3ed852e2009-09-05 21:47:34 +00004142 sepia_image=DestroyImage(sepia_image);
4143 return((Image *) NULL);
4144 }
4145 /*
4146 Tone each row of the image.
4147 */
4148 status=MagickTrue;
4149 progress=0;
cristydb070952012-04-20 14:33:00 +00004150 image_view=AcquireVirtualCacheView(image,exception);
4151 sepia_view=AcquireAuthenticCacheView(sepia_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00004152#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00004153 #pragma omp parallel for schedule(static,4) shared(progress,status) \
4154 if ((image->rows*image->columns) > 8192) \
4155 num_threads(GetMagickResourceLimit(ThreadResource))
cristy3ed852e2009-09-05 21:47:34 +00004156#endif
cristybb503372010-05-27 20:51:26 +00004157 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004158 {
cristy4c08aed2011-07-01 19:47:50 +00004159 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004160 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00004161
cristybb503372010-05-27 20:51:26 +00004162 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004163 x;
4164
cristy4c08aed2011-07-01 19:47:50 +00004165 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004166 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004167
4168 if (status == MagickFalse)
4169 continue;
4170 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy1707c6c2012-01-18 23:30:54 +00004171 q=GetCacheViewAuthenticPixels(sepia_view,0,y,sepia_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00004172 exception);
cristy4c08aed2011-07-01 19:47:50 +00004173 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004174 {
4175 status=MagickFalse;
4176 continue;
4177 }
cristybb503372010-05-27 20:51:26 +00004178 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004179 {
4180 MagickRealType
4181 intensity,
4182 tone;
4183
cristy4c08aed2011-07-01 19:47:50 +00004184 intensity=(MagickRealType) GetPixelIntensity(image,p);
cristy3ed852e2009-09-05 21:47:34 +00004185 tone=intensity > threshold ? (MagickRealType) QuantumRange : intensity+
4186 (MagickRealType) QuantumRange-threshold;
cristy4c08aed2011-07-01 19:47:50 +00004187 SetPixelRed(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004188 tone=intensity > (7.0*threshold/6.0) ? (MagickRealType) QuantumRange :
4189 intensity+(MagickRealType) QuantumRange-7.0*threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004190 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004191 tone=intensity < (threshold/6.0) ? 0 : intensity-threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004192 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004193 tone=threshold/7.0;
cristy4c08aed2011-07-01 19:47:50 +00004194 if ((MagickRealType) GetPixelGreen(image,q) < tone)
4195 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
4196 if ((MagickRealType) GetPixelBlue(image,q) < tone)
4197 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristyed231572011-07-14 02:18:59 +00004198 p+=GetPixelChannels(image);
4199 q+=GetPixelChannels(sepia_image);
cristy3ed852e2009-09-05 21:47:34 +00004200 }
4201 if (SyncCacheViewAuthenticPixels(sepia_view,exception) == MagickFalse)
4202 status=MagickFalse;
4203 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4204 {
4205 MagickBooleanType
4206 proceed;
4207
cristyb5d5f722009-11-04 03:03:49 +00004208#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00004209 #pragma omp critical (MagickCore_SepiaToneImage)
cristy3ed852e2009-09-05 21:47:34 +00004210#endif
4211 proceed=SetImageProgress(image,SepiaToneImageTag,progress++,
4212 image->rows);
4213 if (proceed == MagickFalse)
4214 status=MagickFalse;
4215 }
4216 }
4217 sepia_view=DestroyCacheView(sepia_view);
4218 image_view=DestroyCacheView(image_view);
cristye23ec9d2011-08-16 18:15:40 +00004219 (void) NormalizeImage(sepia_image,exception);
4220 (void) ContrastImage(sepia_image,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004221 if (status == MagickFalse)
4222 sepia_image=DestroyImage(sepia_image);
4223 return(sepia_image);
4224}
4225
4226/*
4227%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4228% %
4229% %
4230% %
4231% S h a d o w I m a g e %
4232% %
4233% %
4234% %
4235%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4236%
4237% ShadowImage() simulates a shadow from the specified image and returns it.
4238%
4239% The format of the ShadowImage method is:
4240%
cristy70cddf72011-12-10 22:42:42 +00004241% Image *ShadowImage(const Image *image,const double alpha,
cristyaa2c16c2012-03-25 22:21:35 +00004242% const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4243% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004244%
4245% A description of each parameter follows:
4246%
4247% o image: the image.
4248%
cristy70cddf72011-12-10 22:42:42 +00004249% o alpha: percentage transparency.
cristy3ed852e2009-09-05 21:47:34 +00004250%
4251% o sigma: the standard deviation of the Gaussian, in pixels.
4252%
4253% o x_offset: the shadow x-offset.
4254%
4255% o y_offset: the shadow y-offset.
4256%
4257% o exception: return any errors or warnings in this structure.
4258%
4259*/
cristy70cddf72011-12-10 22:42:42 +00004260MagickExport Image *ShadowImage(const Image *image,const double alpha,
cristyaa2c16c2012-03-25 22:21:35 +00004261 const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4262 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004263{
4264#define ShadowImageTag "Shadow/Image"
4265
cristy70cddf72011-12-10 22:42:42 +00004266 CacheView
4267 *image_view;
4268
cristybd5a96c2011-08-21 00:04:26 +00004269 ChannelType
4270 channel_mask;
4271
cristy3ed852e2009-09-05 21:47:34 +00004272 Image
4273 *border_image,
4274 *clone_image,
4275 *shadow_image;
4276
cristy70cddf72011-12-10 22:42:42 +00004277 MagickBooleanType
4278 status;
4279
cristy3ed852e2009-09-05 21:47:34 +00004280 RectangleInfo
4281 border_info;
4282
cristy70cddf72011-12-10 22:42:42 +00004283 ssize_t
4284 y;
4285
cristy3ed852e2009-09-05 21:47:34 +00004286 assert(image != (Image *) NULL);
4287 assert(image->signature == MagickSignature);
4288 if (image->debug != MagickFalse)
4289 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4290 assert(exception != (ExceptionInfo *) NULL);
4291 assert(exception->signature == MagickSignature);
4292 clone_image=CloneImage(image,0,0,MagickTrue,exception);
4293 if (clone_image == (Image *) NULL)
4294 return((Image *) NULL);
cristy0898eba2012-04-09 16:38:29 +00004295 if (IsGrayColorspace(image->colorspace) != MagickFalse)
4296 (void) TransformImageColorspace(clone_image,sRGBColorspace,exception);
cristy387430f2012-02-07 13:09:46 +00004297 (void) SetImageVirtualPixelMethod(clone_image,EdgeVirtualPixelMethod,
4298 exception);
cristybb503372010-05-27 20:51:26 +00004299 border_info.width=(size_t) floor(2.0*sigma+0.5);
4300 border_info.height=(size_t) floor(2.0*sigma+0.5);
cristy3ed852e2009-09-05 21:47:34 +00004301 border_info.x=0;
4302 border_info.y=0;
cristy9950d572011-10-01 18:22:35 +00004303 (void) QueryColorCompliance("none",AllCompliance,&clone_image->border_color,
4304 exception);
cristy70cddf72011-12-10 22:42:42 +00004305 clone_image->matte=MagickTrue;
4306 border_image=BorderImage(clone_image,&border_info,OverCompositeOp,exception);
cristy3ed852e2009-09-05 21:47:34 +00004307 clone_image=DestroyImage(clone_image);
4308 if (border_image == (Image *) NULL)
4309 return((Image *) NULL);
4310 if (border_image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00004311 (void) SetImageAlphaChannel(border_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004312 /*
4313 Shadow image.
4314 */
cristy70cddf72011-12-10 22:42:42 +00004315 status=MagickTrue;
cristydb070952012-04-20 14:33:00 +00004316 image_view=AcquireAuthenticCacheView(border_image,exception);
cristy70cddf72011-12-10 22:42:42 +00004317 for (y=0; y < (ssize_t) border_image->rows; y++)
4318 {
4319 PixelInfo
4320 background_color;
4321
4322 register Quantum
4323 *restrict q;
4324
4325 register ssize_t
4326 x;
4327
4328 if (status == MagickFalse)
4329 continue;
4330 q=QueueCacheViewAuthenticPixels(image_view,0,y,border_image->columns,1,
4331 exception);
4332 if (q == (Quantum *) NULL)
4333 {
4334 status=MagickFalse;
4335 continue;
4336 }
4337 background_color=border_image->background_color;
4338 background_color.matte=MagickTrue;
4339 for (x=0; x < (ssize_t) border_image->columns; x++)
4340 {
4341 if (border_image->matte != MagickFalse)
4342 background_color.alpha=GetPixelAlpha(border_image,q)*alpha/100.0;
4343 SetPixelInfoPixel(border_image,&background_color,q);
4344 q+=GetPixelChannels(border_image);
4345 }
4346 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4347 status=MagickFalse;
4348 }
4349 image_view=DestroyCacheView(image_view);
4350 if (status == MagickFalse)
4351 {
4352 border_image=DestroyImage(border_image);
4353 return((Image *) NULL);
4354 }
cristybd5a96c2011-08-21 00:04:26 +00004355 channel_mask=SetPixelChannelMask(border_image,AlphaChannel);
cristyaa2c16c2012-03-25 22:21:35 +00004356 shadow_image=BlurImage(border_image,0.0,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +00004357 border_image=DestroyImage(border_image);
4358 if (shadow_image == (Image *) NULL)
4359 return((Image *) NULL);
cristyae1969f2011-12-10 03:07:36 +00004360 (void) SetPixelChannelMapMask(shadow_image,channel_mask);
cristy3ed852e2009-09-05 21:47:34 +00004361 if (shadow_image->page.width == 0)
4362 shadow_image->page.width=shadow_image->columns;
4363 if (shadow_image->page.height == 0)
4364 shadow_image->page.height=shadow_image->rows;
cristybb503372010-05-27 20:51:26 +00004365 shadow_image->page.width+=x_offset-(ssize_t) border_info.width;
4366 shadow_image->page.height+=y_offset-(ssize_t) border_info.height;
4367 shadow_image->page.x+=x_offset-(ssize_t) border_info.width;
4368 shadow_image->page.y+=y_offset-(ssize_t) border_info.height;
cristy3ed852e2009-09-05 21:47:34 +00004369 return(shadow_image);
4370}
4371
4372/*
4373%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4374% %
4375% %
4376% %
4377% S k e t c h I m a g e %
4378% %
4379% %
4380% %
4381%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4382%
4383% SketchImage() simulates a pencil sketch. We convolve the image with a
4384% Gaussian operator of the given radius and standard deviation (sigma). For
4385% reasonable results, radius should be larger than sigma. Use a radius of 0
4386% and SketchImage() selects a suitable radius for you. Angle gives the angle
4387% of the sketch.
4388%
4389% The format of the SketchImage method is:
4390%
4391% Image *SketchImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00004392% const double sigma,const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004393%
4394% A description of each parameter follows:
4395%
4396% o image: the image.
4397%
cristy574cc262011-08-05 01:23:58 +00004398% o radius: the radius of the Gaussian, in pixels, not counting the
4399% center pixel.
cristy3ed852e2009-09-05 21:47:34 +00004400%
4401% o sigma: the standard deviation of the Gaussian, in pixels.
4402%
cristyf7ef0252011-09-09 14:50:06 +00004403% o angle: apply the effect along this angle.
4404%
cristy3ed852e2009-09-05 21:47:34 +00004405% o exception: return any errors or warnings in this structure.
4406%
4407*/
4408MagickExport Image *SketchImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00004409 const double sigma,const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004410{
cristyfa112112010-01-04 17:48:07 +00004411 CacheView
4412 *random_view;
4413
cristy3ed852e2009-09-05 21:47:34 +00004414 Image
4415 *blend_image,
4416 *blur_image,
4417 *dodge_image,
4418 *random_image,
4419 *sketch_image;
4420
cristy3ed852e2009-09-05 21:47:34 +00004421 MagickBooleanType
4422 status;
4423
cristy3ed852e2009-09-05 21:47:34 +00004424 RandomInfo
cristyfa112112010-01-04 17:48:07 +00004425 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00004426
cristy9d314ff2011-03-09 01:30:28 +00004427 ssize_t
4428 y;
4429
cristy57340e02012-05-05 00:53:23 +00004430 unsigned long
4431 key;
4432
cristy3ed852e2009-09-05 21:47:34 +00004433 /*
4434 Sketch image.
4435 */
4436 random_image=CloneImage(image,image->columns << 1,image->rows << 1,
4437 MagickTrue,exception);
4438 if (random_image == (Image *) NULL)
4439 return((Image *) NULL);
4440 status=MagickTrue;
cristy1b784432009-12-19 02:20:40 +00004441 random_info=AcquireRandomInfoThreadSet();
cristy57340e02012-05-05 00:53:23 +00004442 key=GetRandomSecretKey(random_info[0]);
cristydb070952012-04-20 14:33:00 +00004443 random_view=AcquireAuthenticCacheView(random_image,exception);
cristy1b784432009-12-19 02:20:40 +00004444#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy57340e02012-05-05 00:53:23 +00004445 #pragma omp parallel for schedule(static,4) shared(status) \
cristyac245f82012-05-05 17:13:57 +00004446 if (((image->rows*image->columns) > 8192) && (key == ~0UL)) \
4447 num_threads(GetMagickResourceLimit(ThreadResource))
cristy1b784432009-12-19 02:20:40 +00004448#endif
cristybb503372010-05-27 20:51:26 +00004449 for (y=0; y < (ssize_t) random_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004450 {
cristy5c9e6f22010-09-17 17:31:01 +00004451 const int
4452 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00004453
cristybb503372010-05-27 20:51:26 +00004454 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004455 x;
4456
cristy4c08aed2011-07-01 19:47:50 +00004457 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004458 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004459
cristy1b784432009-12-19 02:20:40 +00004460 if (status == MagickFalse)
4461 continue;
cristy3ed852e2009-09-05 21:47:34 +00004462 q=QueueCacheViewAuthenticPixels(random_view,0,y,random_image->columns,1,
4463 exception);
cristyacd2ed22011-08-30 01:44:23 +00004464 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004465 {
4466 status=MagickFalse;
4467 continue;
4468 }
cristybb503372010-05-27 20:51:26 +00004469 for (x=0; x < (ssize_t) random_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004470 {
cristy76f512e2011-09-12 01:26:56 +00004471 MagickRealType
4472 value;
4473
4474 register ssize_t
4475 i;
4476
cristy10a6c612012-01-29 21:41:05 +00004477 if (GetPixelMask(random_image,q) != 0)
4478 {
4479 q+=GetPixelChannels(random_image);
4480 continue;
4481 }
cristy76f512e2011-09-12 01:26:56 +00004482 value=GetPseudoRandomValue(random_info[id]);
4483 for (i=0; i < (ssize_t) GetPixelChannels(random_image); i++)
4484 {
cristyabace412011-12-11 15:56:53 +00004485 PixelChannel
4486 channel;
4487
cristy76f512e2011-09-12 01:26:56 +00004488 PixelTrait
4489 traits;
4490
cristyabace412011-12-11 15:56:53 +00004491 channel=GetPixelChannelMapChannel(image,i);
4492 traits=GetPixelChannelMapTraits(image,channel);
cristy76f512e2011-09-12 01:26:56 +00004493 if (traits == UndefinedPixelTrait)
4494 continue;
4495 q[i]=ClampToQuantum(QuantumRange*value);
4496 }
cristyed231572011-07-14 02:18:59 +00004497 q+=GetPixelChannels(random_image);
cristy3ed852e2009-09-05 21:47:34 +00004498 }
4499 if (SyncCacheViewAuthenticPixels(random_view,exception) == MagickFalse)
4500 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004501 }
4502 random_view=DestroyCacheView(random_view);
cristy1b784432009-12-19 02:20:40 +00004503 random_info=DestroyRandomInfoThreadSet(random_info);
cristy3ed852e2009-09-05 21:47:34 +00004504 if (status == MagickFalse)
4505 {
4506 random_image=DestroyImage(random_image);
4507 return(random_image);
4508 }
cristyaa2c16c2012-03-25 22:21:35 +00004509 blur_image=MotionBlurImage(random_image,radius,sigma,angle,exception);
cristy3ed852e2009-09-05 21:47:34 +00004510 random_image=DestroyImage(random_image);
4511 if (blur_image == (Image *) NULL)
4512 return((Image *) NULL);
cristy6bfd6902011-12-09 01:33:45 +00004513 dodge_image=EdgeImage(blur_image,radius,1.0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004514 blur_image=DestroyImage(blur_image);
4515 if (dodge_image == (Image *) NULL)
4516 return((Image *) NULL);
cristye23ec9d2011-08-16 18:15:40 +00004517 (void) NormalizeImage(dodge_image,exception);
cristyb3e7c6c2011-07-24 01:43:55 +00004518 (void) NegateImage(dodge_image,MagickFalse,exception);
cristye941a752011-10-15 01:52:48 +00004519 (void) TransformImage(&dodge_image,(char *) NULL,"50%",exception);
cristy3ed852e2009-09-05 21:47:34 +00004520 sketch_image=CloneImage(image,0,0,MagickTrue,exception);
4521 if (sketch_image == (Image *) NULL)
4522 {
4523 dodge_image=DestroyImage(dodge_image);
4524 return((Image *) NULL);
4525 }
cristyfeb3e962012-03-29 17:25:55 +00004526 (void) CompositeImage(sketch_image,dodge_image,ColorDodgeCompositeOp,
cristy39172402012-03-30 13:04:39 +00004527 MagickTrue,0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004528 dodge_image=DestroyImage(dodge_image);
4529 blend_image=CloneImage(image,0,0,MagickTrue,exception);
4530 if (blend_image == (Image *) NULL)
4531 {
4532 sketch_image=DestroyImage(sketch_image);
4533 return((Image *) NULL);
4534 }
4535 (void) SetImageArtifact(blend_image,"compose:args","20x80");
cristy39172402012-03-30 13:04:39 +00004536 (void) CompositeImage(sketch_image,blend_image,BlendCompositeOp,MagickTrue,
cristyfeb3e962012-03-29 17:25:55 +00004537 0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004538 blend_image=DestroyImage(blend_image);
4539 return(sketch_image);
4540}
4541
4542/*
4543%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4544% %
4545% %
4546% %
4547% S o l a r i z e I m a g e %
4548% %
4549% %
4550% %
4551%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4552%
4553% SolarizeImage() applies a special effect to the image, similar to the effect
4554% achieved in a photo darkroom by selectively exposing areas of photo
4555% sensitive paper to light. Threshold ranges from 0 to QuantumRange and is a
4556% measure of the extent of the solarization.
4557%
4558% The format of the SolarizeImage method is:
4559%
cristy5cbc0162011-08-29 00:36:28 +00004560% MagickBooleanType SolarizeImage(Image *image,const double threshold,
4561% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004562%
4563% A description of each parameter follows:
4564%
4565% o image: the image.
4566%
4567% o threshold: Define the extent of the solarization.
4568%
cristy5cbc0162011-08-29 00:36:28 +00004569% o exception: return any errors or warnings in this structure.
4570%
cristy3ed852e2009-09-05 21:47:34 +00004571*/
4572MagickExport MagickBooleanType SolarizeImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00004573 const double threshold,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004574{
4575#define SolarizeImageTag "Solarize/Image"
4576
cristyc4c8d132010-01-07 01:58:38 +00004577 CacheView
4578 *image_view;
4579
cristy3ed852e2009-09-05 21:47:34 +00004580 MagickBooleanType
4581 status;
4582
cristybb503372010-05-27 20:51:26 +00004583 MagickOffsetType
4584 progress;
4585
4586 ssize_t
4587 y;
4588
cristy3ed852e2009-09-05 21:47:34 +00004589 assert(image != (Image *) NULL);
4590 assert(image->signature == MagickSignature);
4591 if (image->debug != MagickFalse)
4592 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4593 if (image->storage_class == PseudoClass)
4594 {
cristybb503372010-05-27 20:51:26 +00004595 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004596 i;
4597
4598 /*
4599 Solarize colormap.
4600 */
cristybb503372010-05-27 20:51:26 +00004601 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00004602 {
4603 if ((MagickRealType) image->colormap[i].red > threshold)
4604 image->colormap[i].red=(Quantum) QuantumRange-image->colormap[i].red;
4605 if ((MagickRealType) image->colormap[i].green > threshold)
4606 image->colormap[i].green=(Quantum) QuantumRange-
4607 image->colormap[i].green;
4608 if ((MagickRealType) image->colormap[i].blue > threshold)
4609 image->colormap[i].blue=(Quantum) QuantumRange-
4610 image->colormap[i].blue;
4611 }
4612 }
4613 /*
4614 Solarize image.
4615 */
4616 status=MagickTrue;
4617 progress=0;
cristydb070952012-04-20 14:33:00 +00004618 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00004619#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00004620 #pragma omp parallel for schedule(static,4) shared(progress,status) \
4621 if ((image->rows*image->columns) > 8192) \
4622 num_threads(GetMagickResourceLimit(ThreadResource))
cristy3ed852e2009-09-05 21:47:34 +00004623#endif
cristybb503372010-05-27 20:51:26 +00004624 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004625 {
cristybb503372010-05-27 20:51:26 +00004626 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004627 x;
4628
cristy4c08aed2011-07-01 19:47:50 +00004629 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004630 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004631
4632 if (status == MagickFalse)
4633 continue;
cristy5cbc0162011-08-29 00:36:28 +00004634 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00004635 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004636 {
4637 status=MagickFalse;
4638 continue;
4639 }
cristybb503372010-05-27 20:51:26 +00004640 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004641 {
cristy76f512e2011-09-12 01:26:56 +00004642 register ssize_t
4643 i;
4644
cristy10a6c612012-01-29 21:41:05 +00004645 if (GetPixelMask(image,q) != 0)
4646 {
4647 q+=GetPixelChannels(image);
4648 continue;
4649 }
cristy76f512e2011-09-12 01:26:56 +00004650 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4651 {
cristyabace412011-12-11 15:56:53 +00004652 PixelChannel
4653 channel;
4654
cristy76f512e2011-09-12 01:26:56 +00004655 PixelTrait
4656 traits;
4657
cristyabace412011-12-11 15:56:53 +00004658 channel=GetPixelChannelMapChannel(image,i);
4659 traits=GetPixelChannelMapTraits(image,channel);
cristy76f512e2011-09-12 01:26:56 +00004660 if ((traits == UndefinedPixelTrait) ||
4661 ((traits & CopyPixelTrait) != 0))
4662 continue;
4663 if ((MagickRealType) q[i] > threshold)
4664 q[i]=QuantumRange-q[i];
4665 }
cristyed231572011-07-14 02:18:59 +00004666 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00004667 }
4668 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4669 status=MagickFalse;
4670 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4671 {
4672 MagickBooleanType
4673 proceed;
4674
cristyb5d5f722009-11-04 03:03:49 +00004675#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00004676 #pragma omp critical (MagickCore_SolarizeImage)
cristy3ed852e2009-09-05 21:47:34 +00004677#endif
4678 proceed=SetImageProgress(image,SolarizeImageTag,progress++,image->rows);
4679 if (proceed == MagickFalse)
4680 status=MagickFalse;
4681 }
4682 }
4683 image_view=DestroyCacheView(image_view);
4684 return(status);
4685}
4686
4687/*
4688%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4689% %
4690% %
4691% %
4692% S t e g a n o I m a g e %
4693% %
4694% %
4695% %
4696%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4697%
4698% SteganoImage() hides a digital watermark within the image. Recover
4699% the hidden watermark later to prove that the authenticity of an image.
4700% Offset defines the start position within the image to hide the watermark.
4701%
4702% The format of the SteganoImage method is:
4703%
4704% Image *SteganoImage(const Image *image,Image *watermark,
4705% ExceptionInfo *exception)
4706%
4707% A description of each parameter follows:
4708%
4709% o image: the image.
4710%
4711% o watermark: the watermark image.
4712%
4713% o exception: return any errors or warnings in this structure.
4714%
4715*/
4716MagickExport Image *SteganoImage(const Image *image,const Image *watermark,
4717 ExceptionInfo *exception)
4718{
cristye1bf8ad2010-09-19 17:07:03 +00004719#define GetBit(alpha,i) ((((size_t) (alpha) >> (size_t) (i)) & 0x01) != 0)
cristy4c08aed2011-07-01 19:47:50 +00004720#define SetBit(alpha,i,set) (Quantum) ((set) != 0 ? (size_t) (alpha) \
cristyeaedf062010-05-29 22:36:02 +00004721 | (one << (size_t) (i)) : (size_t) (alpha) & ~(one << (size_t) (i)))
cristy3ed852e2009-09-05 21:47:34 +00004722#define SteganoImageTag "Stegano/Image"
4723
cristyb0d3bb92010-09-22 14:37:58 +00004724 CacheView
4725 *stegano_view,
4726 *watermark_view;
4727
cristy3ed852e2009-09-05 21:47:34 +00004728 Image
4729 *stegano_image;
4730
4731 int
4732 c;
4733
cristy3ed852e2009-09-05 21:47:34 +00004734 MagickBooleanType
4735 status;
4736
cristy101ab702011-10-13 13:06:32 +00004737 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00004738 pixel;
4739
cristy4c08aed2011-07-01 19:47:50 +00004740 register Quantum
cristy3ed852e2009-09-05 21:47:34 +00004741 *q;
4742
cristye1bf8ad2010-09-19 17:07:03 +00004743 register ssize_t
4744 x;
4745
cristybb503372010-05-27 20:51:26 +00004746 size_t
cristyeaedf062010-05-29 22:36:02 +00004747 depth,
4748 one;
cristy3ed852e2009-09-05 21:47:34 +00004749
cristye1bf8ad2010-09-19 17:07:03 +00004750 ssize_t
4751 i,
4752 j,
4753 k,
4754 y;
4755
cristy3ed852e2009-09-05 21:47:34 +00004756 /*
4757 Initialize steganographic image attributes.
4758 */
4759 assert(image != (const Image *) NULL);
4760 assert(image->signature == MagickSignature);
4761 if (image->debug != MagickFalse)
4762 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4763 assert(watermark != (const Image *) NULL);
4764 assert(watermark->signature == MagickSignature);
4765 assert(exception != (ExceptionInfo *) NULL);
4766 assert(exception->signature == MagickSignature);
cristyeaedf062010-05-29 22:36:02 +00004767 one=1UL;
cristy3ed852e2009-09-05 21:47:34 +00004768 stegano_image=CloneImage(image,0,0,MagickTrue,exception);
4769 if (stegano_image == (Image *) NULL)
4770 return((Image *) NULL);
cristyf61b1832012-04-01 01:38:19 +00004771 stegano_image->depth=MAGICKCORE_QUANTUM_DEPTH;
cristy574cc262011-08-05 01:23:58 +00004772 if (SetImageStorageClass(stegano_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004773 {
cristy3ed852e2009-09-05 21:47:34 +00004774 stegano_image=DestroyImage(stegano_image);
4775 return((Image *) NULL);
4776 }
cristy3ed852e2009-09-05 21:47:34 +00004777 /*
4778 Hide watermark in low-order bits of image.
4779 */
4780 c=0;
4781 i=0;
4782 j=0;
4783 depth=stegano_image->depth;
cristyf61b1832012-04-01 01:38:19 +00004784 k=stegano_image->offset;
cristyda16f162011-02-19 23:52:17 +00004785 status=MagickTrue;
cristydb070952012-04-20 14:33:00 +00004786 watermark_view=AcquireVirtualCacheView(watermark,exception);
4787 stegano_view=AcquireAuthenticCacheView(stegano_image,exception);
cristybb503372010-05-27 20:51:26 +00004788 for (i=(ssize_t) depth-1; (i >= 0) && (j < (ssize_t) depth); i--)
cristy3ed852e2009-09-05 21:47:34 +00004789 {
cristybb503372010-05-27 20:51:26 +00004790 for (y=0; (y < (ssize_t) watermark->rows) && (j < (ssize_t) depth); y++)
cristy3ed852e2009-09-05 21:47:34 +00004791 {
cristybb503372010-05-27 20:51:26 +00004792 for (x=0; (x < (ssize_t) watermark->columns) && (j < (ssize_t) depth); x++)
cristy3ed852e2009-09-05 21:47:34 +00004793 {
cristy1707c6c2012-01-18 23:30:54 +00004794 ssize_t
4795 offset;
4796
cristyf05d4942012-03-17 16:26:09 +00004797 (void) GetOneCacheViewVirtualPixelInfo(watermark_view,x,y,&pixel,
cristyda1f9c12011-10-02 21:39:49 +00004798 exception);
cristy1707c6c2012-01-18 23:30:54 +00004799 offset=k/(ssize_t) stegano_image->columns;
4800 if (offset >= (ssize_t) stegano_image->rows)
cristy3ed852e2009-09-05 21:47:34 +00004801 break;
cristyb0d3bb92010-09-22 14:37:58 +00004802 q=GetCacheViewAuthenticPixels(stegano_view,k % (ssize_t)
4803 stegano_image->columns,k/(ssize_t) stegano_image->columns,1,1,
4804 exception);
cristyacd2ed22011-08-30 01:44:23 +00004805 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004806 break;
4807 switch (c)
4808 {
4809 case 0:
4810 {
cristyf61b1832012-04-01 01:38:19 +00004811 SetPixelRed(stegano_image,SetBit(GetPixelRed(stegano_image,q),j,
4812 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004813 break;
4814 }
4815 case 1:
4816 {
cristyf61b1832012-04-01 01:38:19 +00004817 SetPixelGreen(stegano_image,SetBit(GetPixelGreen(stegano_image,q),j,
4818 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004819 break;
4820 }
4821 case 2:
4822 {
cristyf61b1832012-04-01 01:38:19 +00004823 SetPixelBlue(stegano_image,SetBit(GetPixelBlue(stegano_image,q),j,
4824 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004825 break;
4826 }
4827 }
cristyb0d3bb92010-09-22 14:37:58 +00004828 if (SyncCacheViewAuthenticPixels(stegano_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004829 break;
4830 c++;
4831 if (c == 3)
4832 c=0;
4833 k++;
cristybb503372010-05-27 20:51:26 +00004834 if (k == (ssize_t) (stegano_image->columns*stegano_image->columns))
cristy3ed852e2009-09-05 21:47:34 +00004835 k=0;
cristyf61b1832012-04-01 01:38:19 +00004836 if (k == stegano_image->offset)
cristy3ed852e2009-09-05 21:47:34 +00004837 j++;
4838 }
4839 }
cristy8b27a6d2010-02-14 03:31:15 +00004840 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004841 {
cristy8b27a6d2010-02-14 03:31:15 +00004842 MagickBooleanType
4843 proceed;
4844
4845 proceed=SetImageProgress(image,SteganoImageTag,(MagickOffsetType)
4846 (depth-i),depth);
4847 if (proceed == MagickFalse)
4848 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004849 }
4850 }
cristyb0d3bb92010-09-22 14:37:58 +00004851 stegano_view=DestroyCacheView(stegano_view);
4852 watermark_view=DestroyCacheView(watermark_view);
cristyda16f162011-02-19 23:52:17 +00004853 if (status == MagickFalse)
4854 {
4855 stegano_image=DestroyImage(stegano_image);
4856 return((Image *) NULL);
4857 }
cristy3ed852e2009-09-05 21:47:34 +00004858 return(stegano_image);
4859}
4860
4861/*
4862%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4863% %
4864% %
4865% %
4866% S t e r e o A n a g l y p h I m a g e %
4867% %
4868% %
4869% %
4870%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4871%
4872% StereoAnaglyphImage() combines two images and produces a single image that
4873% is the composite of a left and right image of a stereo pair. Special
4874% red-green stereo glasses are required to view this effect.
4875%
4876% The format of the StereoAnaglyphImage method is:
4877%
4878% Image *StereoImage(const Image *left_image,const Image *right_image,
4879% ExceptionInfo *exception)
4880% Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004881% const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004882% ExceptionInfo *exception)
4883%
4884% A description of each parameter follows:
4885%
4886% o left_image: the left image.
4887%
4888% o right_image: the right image.
4889%
4890% o exception: return any errors or warnings in this structure.
4891%
4892% o x_offset: amount, in pixels, by which the left image is offset to the
4893% right of the right image.
4894%
4895% o y_offset: amount, in pixels, by which the left image is offset to the
4896% bottom of the right image.
4897%
4898%
4899*/
4900MagickExport Image *StereoImage(const Image *left_image,
4901 const Image *right_image,ExceptionInfo *exception)
4902{
4903 return(StereoAnaglyphImage(left_image,right_image,0,0,exception));
4904}
4905
4906MagickExport Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004907 const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004908 ExceptionInfo *exception)
4909{
4910#define StereoImageTag "Stereo/Image"
4911
4912 const Image
4913 *image;
4914
4915 Image
4916 *stereo_image;
4917
cristy3ed852e2009-09-05 21:47:34 +00004918 MagickBooleanType
4919 status;
4920
cristy9d314ff2011-03-09 01:30:28 +00004921 ssize_t
4922 y;
4923
cristy3ed852e2009-09-05 21:47:34 +00004924 assert(left_image != (const Image *) NULL);
4925 assert(left_image->signature == MagickSignature);
4926 if (left_image->debug != MagickFalse)
4927 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4928 left_image->filename);
4929 assert(right_image != (const Image *) NULL);
4930 assert(right_image->signature == MagickSignature);
4931 assert(exception != (ExceptionInfo *) NULL);
4932 assert(exception->signature == MagickSignature);
4933 assert(right_image != (const Image *) NULL);
4934 image=left_image;
4935 if ((left_image->columns != right_image->columns) ||
4936 (left_image->rows != right_image->rows))
4937 ThrowImageException(ImageError,"LeftAndRightImageSizesDiffer");
4938 /*
4939 Initialize stereo image attributes.
4940 */
4941 stereo_image=CloneImage(left_image,left_image->columns,left_image->rows,
4942 MagickTrue,exception);
4943 if (stereo_image == (Image *) NULL)
4944 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004945 if (SetImageStorageClass(stereo_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004946 {
cristy3ed852e2009-09-05 21:47:34 +00004947 stereo_image=DestroyImage(stereo_image);
4948 return((Image *) NULL);
4949 }
4950 /*
4951 Copy left image to red channel and right image to blue channel.
4952 */
cristyda16f162011-02-19 23:52:17 +00004953 status=MagickTrue;
cristybb503372010-05-27 20:51:26 +00004954 for (y=0; y < (ssize_t) stereo_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004955 {
cristy4c08aed2011-07-01 19:47:50 +00004956 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004957 *restrict p,
4958 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004959
cristybb503372010-05-27 20:51:26 +00004960 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004961 x;
4962
cristy4c08aed2011-07-01 19:47:50 +00004963 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004964 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +00004965
4966 p=GetVirtualPixels(left_image,-x_offset,y-y_offset,image->columns,1,
4967 exception);
4968 q=GetVirtualPixels(right_image,0,y,right_image->columns,1,exception);
4969 r=QueueAuthenticPixels(stereo_image,0,y,stereo_image->columns,1,exception);
cristy76f512e2011-09-12 01:26:56 +00004970 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL) ||
4971 (r == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004972 break;
cristybb503372010-05-27 20:51:26 +00004973 for (x=0; x < (ssize_t) stereo_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004974 {
cristy4c08aed2011-07-01 19:47:50 +00004975 SetPixelRed(image,GetPixelRed(left_image,p),r);
cristy76f512e2011-09-12 01:26:56 +00004976 SetPixelGreen(image,GetPixelGreen(right_image,q),r);
4977 SetPixelBlue(image,GetPixelBlue(right_image,q),r);
4978 if ((GetPixelAlphaTraits(stereo_image) & CopyPixelTrait) != 0)
4979 SetPixelAlpha(image,(GetPixelAlpha(left_image,p)+
4980 GetPixelAlpha(right_image,q))/2,r);
cristyed231572011-07-14 02:18:59 +00004981 p+=GetPixelChannels(left_image);
cristy76f512e2011-09-12 01:26:56 +00004982 q+=GetPixelChannels(right_image);
4983 r+=GetPixelChannels(stereo_image);
cristy3ed852e2009-09-05 21:47:34 +00004984 }
4985 if (SyncAuthenticPixels(stereo_image,exception) == MagickFalse)
4986 break;
cristy8b27a6d2010-02-14 03:31:15 +00004987 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004988 {
cristy8b27a6d2010-02-14 03:31:15 +00004989 MagickBooleanType
4990 proceed;
4991
cristybb503372010-05-27 20:51:26 +00004992 proceed=SetImageProgress(image,StereoImageTag,(MagickOffsetType) y,
4993 stereo_image->rows);
cristy8b27a6d2010-02-14 03:31:15 +00004994 if (proceed == MagickFalse)
4995 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004996 }
4997 }
cristyda16f162011-02-19 23:52:17 +00004998 if (status == MagickFalse)
4999 {
5000 stereo_image=DestroyImage(stereo_image);
5001 return((Image *) NULL);
5002 }
cristy3ed852e2009-09-05 21:47:34 +00005003 return(stereo_image);
5004}
5005
5006/*
5007%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5008% %
5009% %
5010% %
5011% S w i r l I m a g e %
5012% %
5013% %
5014% %
5015%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5016%
5017% SwirlImage() swirls the pixels about the center of the image, where
5018% degrees indicates the sweep of the arc through which each pixel is moved.
5019% You get a more dramatic effect as the degrees move from 1 to 360.
5020%
5021% The format of the SwirlImage method is:
5022%
5023% Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00005024% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005025%
5026% A description of each parameter follows:
5027%
5028% o image: the image.
5029%
5030% o degrees: Define the tightness of the swirling effect.
5031%
cristy76f512e2011-09-12 01:26:56 +00005032% o method: the pixel interpolation method.
5033%
cristy3ed852e2009-09-05 21:47:34 +00005034% o exception: return any errors or warnings in this structure.
5035%
5036*/
5037MagickExport Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00005038 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005039{
5040#define SwirlImageTag "Swirl/Image"
5041
cristyfa112112010-01-04 17:48:07 +00005042 CacheView
5043 *image_view,
5044 *swirl_view;
5045
cristy3ed852e2009-09-05 21:47:34 +00005046 Image
5047 *swirl_image;
5048
cristy3ed852e2009-09-05 21:47:34 +00005049 MagickBooleanType
5050 status;
5051
cristybb503372010-05-27 20:51:26 +00005052 MagickOffsetType
5053 progress;
5054
cristy3ed852e2009-09-05 21:47:34 +00005055 MagickRealType
5056 radius;
5057
5058 PointInfo
5059 center,
5060 scale;
5061
cristybb503372010-05-27 20:51:26 +00005062 ssize_t
5063 y;
5064
cristy3ed852e2009-09-05 21:47:34 +00005065 /*
5066 Initialize swirl image attributes.
5067 */
5068 assert(image != (const Image *) NULL);
5069 assert(image->signature == MagickSignature);
5070 if (image->debug != MagickFalse)
5071 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5072 assert(exception != (ExceptionInfo *) NULL);
5073 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00005074 swirl_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005075 if (swirl_image == (Image *) NULL)
5076 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005077 if (SetImageStorageClass(swirl_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005078 {
cristy3ed852e2009-09-05 21:47:34 +00005079 swirl_image=DestroyImage(swirl_image);
5080 return((Image *) NULL);
5081 }
cristy4c08aed2011-07-01 19:47:50 +00005082 if (swirl_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00005083 swirl_image->matte=MagickTrue;
5084 /*
5085 Compute scaling factor.
5086 */
5087 center.x=(double) image->columns/2.0;
5088 center.y=(double) image->rows/2.0;
5089 radius=MagickMax(center.x,center.y);
5090 scale.x=1.0;
5091 scale.y=1.0;
5092 if (image->columns > image->rows)
5093 scale.y=(double) image->columns/(double) image->rows;
5094 else
5095 if (image->columns < image->rows)
5096 scale.x=(double) image->rows/(double) image->columns;
5097 degrees=(double) DegreesToRadians(degrees);
5098 /*
5099 Swirl image.
5100 */
5101 status=MagickTrue;
5102 progress=0;
cristydb070952012-04-20 14:33:00 +00005103 image_view=AcquireVirtualCacheView(image,exception);
5104 swirl_view=AcquireAuthenticCacheView(swirl_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00005105#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00005106 #pragma omp parallel for schedule(static,4) shared(progress,status) \
5107 if ((image->rows*image->columns) > 8192) \
5108 num_threads(GetMagickResourceLimit(ThreadResource))
cristy3ed852e2009-09-05 21:47:34 +00005109#endif
cristybb503372010-05-27 20:51:26 +00005110 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005111 {
cristy3ed852e2009-09-05 21:47:34 +00005112 MagickRealType
5113 distance;
5114
5115 PointInfo
5116 delta;
5117
cristy6d188022011-09-12 13:23:33 +00005118 register const Quantum
5119 *restrict p;
5120
cristybb503372010-05-27 20:51:26 +00005121 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005122 x;
5123
cristy4c08aed2011-07-01 19:47:50 +00005124 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005125 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005126
5127 if (status == MagickFalse)
5128 continue;
cristy6d188022011-09-12 13:23:33 +00005129 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy1707c6c2012-01-18 23:30:54 +00005130 q=QueueCacheViewAuthenticPixels(swirl_view,0,y,swirl_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00005131 exception);
cristy6d188022011-09-12 13:23:33 +00005132 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005133 {
5134 status=MagickFalse;
5135 continue;
5136 }
cristy3ed852e2009-09-05 21:47:34 +00005137 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00005138 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005139 {
5140 /*
5141 Determine if the pixel is within an ellipse.
5142 */
cristy10a6c612012-01-29 21:41:05 +00005143 if (GetPixelMask(image,p) != 0)
5144 {
5145 p+=GetPixelChannels(image);
5146 q+=GetPixelChannels(swirl_image);
5147 continue;
5148 }
cristy3ed852e2009-09-05 21:47:34 +00005149 delta.x=scale.x*(double) (x-center.x);
5150 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00005151 if (distance >= (radius*radius))
5152 {
cristy1707c6c2012-01-18 23:30:54 +00005153 register ssize_t
5154 i;
5155
cristy6d188022011-09-12 13:23:33 +00005156 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy1707c6c2012-01-18 23:30:54 +00005157 {
5158 PixelChannel
5159 channel;
5160
5161 PixelTrait
5162 swirl_traits,
5163 traits;
5164
5165 channel=GetPixelChannelMapChannel(image,i);
5166 traits=GetPixelChannelMapTraits(image,channel);
5167 swirl_traits=GetPixelChannelMapTraits(swirl_image,channel);
5168 if ((traits == UndefinedPixelTrait) ||
5169 (swirl_traits == UndefinedPixelTrait))
5170 continue;
5171 SetPixelChannel(swirl_image,channel,p[i],q);
5172 }
cristy6d188022011-09-12 13:23:33 +00005173 }
5174 else
cristy3ed852e2009-09-05 21:47:34 +00005175 {
5176 MagickRealType
5177 cosine,
5178 factor,
5179 sine;
5180
5181 /*
5182 Swirl the pixel.
5183 */
5184 factor=1.0-sqrt((double) distance)/radius;
5185 sine=sin((double) (degrees*factor*factor));
5186 cosine=cos((double) (degrees*factor*factor));
cristy76f512e2011-09-12 01:26:56 +00005187 status=InterpolatePixelChannels(image,image_view,swirl_image,method,
5188 ((cosine*delta.x-sine*delta.y)/scale.x+center.x),(double)
5189 ((sine*delta.x+cosine*delta.y)/scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00005190 }
cristy6d188022011-09-12 13:23:33 +00005191 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00005192 q+=GetPixelChannels(swirl_image);
cristy3ed852e2009-09-05 21:47:34 +00005193 }
5194 if (SyncCacheViewAuthenticPixels(swirl_view,exception) == MagickFalse)
5195 status=MagickFalse;
5196 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5197 {
5198 MagickBooleanType
5199 proceed;
5200
cristyb5d5f722009-11-04 03:03:49 +00005201#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005202 #pragma omp critical (MagickCore_SwirlImage)
cristy3ed852e2009-09-05 21:47:34 +00005203#endif
5204 proceed=SetImageProgress(image,SwirlImageTag,progress++,image->rows);
5205 if (proceed == MagickFalse)
5206 status=MagickFalse;
5207 }
5208 }
5209 swirl_view=DestroyCacheView(swirl_view);
5210 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00005211 if (status == MagickFalse)
5212 swirl_image=DestroyImage(swirl_image);
5213 return(swirl_image);
5214}
5215
5216/*
5217%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5218% %
5219% %
5220% %
5221% T i n t I m a g e %
5222% %
5223% %
5224% %
5225%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5226%
5227% TintImage() applies a color vector to each pixel in the image. The length
5228% of the vector is 0 for black and white and at its maximum for the midtones.
5229% The vector weighting function is f(x)=(1-(4.0*((x-0.5)*(x-0.5))))
5230%
5231% The format of the TintImage method is:
5232%
cristyb817c3f2011-10-03 14:00:35 +00005233% Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005234% const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005235%
5236% A description of each parameter follows:
5237%
5238% o image: the image.
5239%
cristyb817c3f2011-10-03 14:00:35 +00005240% o blend: A color value used for tinting.
cristy3ed852e2009-09-05 21:47:34 +00005241%
5242% o tint: A color value used for tinting.
5243%
5244% o exception: return any errors or warnings in this structure.
5245%
5246*/
cristyb817c3f2011-10-03 14:00:35 +00005247MagickExport Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005248 const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005249{
5250#define TintImageTag "Tint/Image"
5251
cristyc4c8d132010-01-07 01:58:38 +00005252 CacheView
5253 *image_view,
5254 *tint_view;
5255
cristy3ed852e2009-09-05 21:47:34 +00005256 GeometryInfo
5257 geometry_info;
5258
5259 Image
5260 *tint_image;
5261
cristy3ed852e2009-09-05 21:47:34 +00005262 MagickBooleanType
5263 status;
5264
cristybb503372010-05-27 20:51:26 +00005265 MagickOffsetType
5266 progress;
cristy3ed852e2009-09-05 21:47:34 +00005267
cristy28474bf2011-09-11 23:32:52 +00005268 MagickRealType
5269 intensity;
5270
cristy4c08aed2011-07-01 19:47:50 +00005271 PixelInfo
cristy1707c6c2012-01-18 23:30:54 +00005272 color_vector;
cristy3ed852e2009-09-05 21:47:34 +00005273
cristybb503372010-05-27 20:51:26 +00005274 MagickStatusType
5275 flags;
5276
5277 ssize_t
5278 y;
5279
cristy3ed852e2009-09-05 21:47:34 +00005280 /*
5281 Allocate tint image.
5282 */
5283 assert(image != (const Image *) NULL);
5284 assert(image->signature == MagickSignature);
5285 if (image->debug != MagickFalse)
5286 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5287 assert(exception != (ExceptionInfo *) NULL);
5288 assert(exception->signature == MagickSignature);
5289 tint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5290 if (tint_image == (Image *) NULL)
5291 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005292 if (SetImageStorageClass(tint_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005293 {
cristy3ed852e2009-09-05 21:47:34 +00005294 tint_image=DestroyImage(tint_image);
5295 return((Image *) NULL);
5296 }
cristyaed9c382011-10-03 17:54:21 +00005297 if (blend == (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005298 return(tint_image);
5299 /*
5300 Determine RGB values of the color.
5301 */
cristy1707c6c2012-01-18 23:30:54 +00005302 GetPixelInfo(image,&color_vector);
cristyb817c3f2011-10-03 14:00:35 +00005303 flags=ParseGeometry(blend,&geometry_info);
cristy1707c6c2012-01-18 23:30:54 +00005304 color_vector.red=geometry_info.rho;
5305 color_vector.green=geometry_info.rho;
5306 color_vector.blue=geometry_info.rho;
5307 color_vector.alpha=OpaqueAlpha;
cristy3ed852e2009-09-05 21:47:34 +00005308 if ((flags & SigmaValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005309 color_vector.green=geometry_info.sigma;
cristy3ed852e2009-09-05 21:47:34 +00005310 if ((flags & XiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005311 color_vector.blue=geometry_info.xi;
cristyb817c3f2011-10-03 14:00:35 +00005312 if ((flags & PsiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005313 color_vector.alpha=geometry_info.psi;
cristy76f512e2011-09-12 01:26:56 +00005314 if (image->colorspace == CMYKColorspace)
5315 {
cristy1707c6c2012-01-18 23:30:54 +00005316 color_vector.black=geometry_info.rho;
cristy76f512e2011-09-12 01:26:56 +00005317 if ((flags & PsiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005318 color_vector.black=geometry_info.psi;
cristy76f512e2011-09-12 01:26:56 +00005319 if ((flags & ChiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005320 color_vector.alpha=geometry_info.chi;
cristy76f512e2011-09-12 01:26:56 +00005321 }
cristy28474bf2011-09-11 23:32:52 +00005322 intensity=(MagickRealType) GetPixelInfoIntensity(tint);
cristy1707c6c2012-01-18 23:30:54 +00005323 color_vector.red=(MagickRealType) (color_vector.red*tint->red/100.0-
5324 intensity);
5325 color_vector.green=(MagickRealType) (color_vector.green*tint->green/100.0-
5326 intensity);
5327 color_vector.blue=(MagickRealType) (color_vector.blue*tint->blue/100.0-
5328 intensity);
5329 color_vector.black=(MagickRealType) (color_vector.black*tint->black/100.0-
5330 intensity);
5331 color_vector.alpha=(MagickRealType) (color_vector.alpha*tint->alpha/100.0-
5332 intensity);
cristy3ed852e2009-09-05 21:47:34 +00005333 /*
5334 Tint image.
5335 */
5336 status=MagickTrue;
5337 progress=0;
cristydb070952012-04-20 14:33:00 +00005338 image_view=AcquireVirtualCacheView(image,exception);
5339 tint_view=AcquireAuthenticCacheView(tint_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00005340#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00005341 #pragma omp parallel for schedule(static,4) shared(progress,status) \
5342 if ((image->rows*image->columns) > 8192) \
5343 num_threads(GetMagickResourceLimit(ThreadResource))
cristy3ed852e2009-09-05 21:47:34 +00005344#endif
cristybb503372010-05-27 20:51:26 +00005345 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005346 {
cristy4c08aed2011-07-01 19:47:50 +00005347 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00005348 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00005349
cristy4c08aed2011-07-01 19:47:50 +00005350 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005351 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005352
cristy6b91acb2011-04-19 12:23:54 +00005353 register ssize_t
5354 x;
5355
cristy3ed852e2009-09-05 21:47:34 +00005356 if (status == MagickFalse)
5357 continue;
5358 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5359 q=QueueCacheViewAuthenticPixels(tint_view,0,y,tint_image->columns,1,
5360 exception);
cristy4c08aed2011-07-01 19:47:50 +00005361 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005362 {
5363 status=MagickFalse;
5364 continue;
5365 }
cristybb503372010-05-27 20:51:26 +00005366 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005367 {
cristy4c08aed2011-07-01 19:47:50 +00005368 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00005369 pixel;
5370
5371 MagickRealType
5372 weight;
5373
cristy1707c6c2012-01-18 23:30:54 +00005374 register ssize_t
5375 i;
5376
cristy10a6c612012-01-29 21:41:05 +00005377 if (GetPixelMask(image,p) != 0)
5378 {
5379 p+=GetPixelChannels(image);
5380 q+=GetPixelChannels(tint_image);
5381 continue;
5382 }
cristy1707c6c2012-01-18 23:30:54 +00005383 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5384 {
5385 PixelChannel
5386 channel;
5387
5388 PixelTrait
5389 tint_traits,
5390 traits;
5391
5392 channel=GetPixelChannelMapChannel(image,i);
5393 traits=GetPixelChannelMapTraits(image,channel);
5394 tint_traits=GetPixelChannelMapTraits(tint_image,channel);
5395 if ((traits == UndefinedPixelTrait) ||
5396 (tint_traits == UndefinedPixelTrait))
5397 continue;
5398 if ((tint_traits & CopyPixelTrait) != 0)
5399 {
5400 SetPixelChannel(tint_image,channel,p[i],q);
5401 continue;
5402 }
5403 }
5404 GetPixelInfo(image,&pixel);
5405 weight=QuantumScale*GetPixelRed(image,p)-0.5;
5406 pixel.red=(MagickRealType) GetPixelRed(image,p)+color_vector.red*
5407 (1.0-(4.0*(weight*weight)));
5408 weight=QuantumScale*GetPixelGreen(image,p)-0.5;
5409 pixel.green=(MagickRealType) GetPixelGreen(image,p)+color_vector.green*
5410 (1.0-(4.0*(weight*weight)));
5411 weight=QuantumScale*GetPixelBlue(image,p)-0.5;
5412 pixel.blue=(MagickRealType) GetPixelBlue(image,p)+color_vector.blue*
5413 (1.0-(4.0*(weight*weight)));
5414 weight=QuantumScale*GetPixelBlack(image,p)-0.5;
5415 pixel.black=(MagickRealType) GetPixelBlack(image,p)+color_vector.black*
5416 (1.0-(4.0*(weight*weight)));
5417 SetPixelInfoPixel(tint_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +00005418 p+=GetPixelChannels(image);
5419 q+=GetPixelChannels(tint_image);
cristy3ed852e2009-09-05 21:47:34 +00005420 }
5421 if (SyncCacheViewAuthenticPixels(tint_view,exception) == MagickFalse)
5422 status=MagickFalse;
5423 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5424 {
5425 MagickBooleanType
5426 proceed;
5427
cristyb5d5f722009-11-04 03:03:49 +00005428#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005429 #pragma omp critical (MagickCore_TintImage)
cristy3ed852e2009-09-05 21:47:34 +00005430#endif
5431 proceed=SetImageProgress(image,TintImageTag,progress++,image->rows);
5432 if (proceed == MagickFalse)
5433 status=MagickFalse;
5434 }
5435 }
5436 tint_view=DestroyCacheView(tint_view);
5437 image_view=DestroyCacheView(image_view);
5438 if (status == MagickFalse)
5439 tint_image=DestroyImage(tint_image);
5440 return(tint_image);
5441}
5442
5443/*
5444%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5445% %
5446% %
5447% %
5448% V i g n e t t e I m a g e %
5449% %
5450% %
5451% %
5452%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5453%
5454% VignetteImage() softens the edges of the image in vignette style.
5455%
5456% The format of the VignetteImage method is:
5457%
5458% Image *VignetteImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00005459% const double sigma,const ssize_t x,const ssize_t y,
cristy05c0c9a2011-09-05 23:16:13 +00005460% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005461%
5462% A description of each parameter follows:
5463%
5464% o image: the image.
5465%
5466% o radius: the radius of the pixel neighborhood.
5467%
5468% o sigma: the standard deviation of the Gaussian, in pixels.
5469%
5470% o x, y: Define the x and y ellipse offset.
5471%
5472% o exception: return any errors or warnings in this structure.
5473%
5474*/
5475MagickExport Image *VignetteImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00005476 const double sigma,const ssize_t x,const ssize_t y,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005477{
5478 char
5479 ellipse[MaxTextExtent];
5480
5481 DrawInfo
5482 *draw_info;
5483
5484 Image
5485 *canvas_image,
5486 *blur_image,
5487 *oval_image,
5488 *vignette_image;
5489
5490 assert(image != (Image *) NULL);
5491 assert(image->signature == MagickSignature);
5492 if (image->debug != MagickFalse)
5493 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5494 assert(exception != (ExceptionInfo *) NULL);
5495 assert(exception->signature == MagickSignature);
5496 canvas_image=CloneImage(image,0,0,MagickTrue,exception);
5497 if (canvas_image == (Image *) NULL)
5498 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005499 if (SetImageStorageClass(canvas_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005500 {
cristy3ed852e2009-09-05 21:47:34 +00005501 canvas_image=DestroyImage(canvas_image);
5502 return((Image *) NULL);
5503 }
5504 canvas_image->matte=MagickTrue;
cristy98621462011-12-31 22:31:11 +00005505 oval_image=CloneImage(canvas_image,canvas_image->columns,canvas_image->rows,
5506 MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005507 if (oval_image == (Image *) NULL)
5508 {
5509 canvas_image=DestroyImage(canvas_image);
5510 return((Image *) NULL);
5511 }
cristy9950d572011-10-01 18:22:35 +00005512 (void) QueryColorCompliance("#000000",AllCompliance,
5513 &oval_image->background_color,exception);
cristyea1a8aa2011-10-20 13:24:06 +00005514 (void) SetImageBackgroundColor(oval_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00005515 draw_info=CloneDrawInfo((const ImageInfo *) NULL,(const DrawInfo *) NULL);
cristy9950d572011-10-01 18:22:35 +00005516 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->fill,
5517 exception);
5518 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->stroke,
5519 exception);
cristy1707c6c2012-01-18 23:30:54 +00005520 (void) FormatLocaleString(ellipse,MaxTextExtent,"ellipse %g,%g,%g,%g,"
5521 "0.0,360.0",image->columns/2.0,image->rows/2.0,image->columns/2.0-x,
5522 image->rows/2.0-y);
cristy3ed852e2009-09-05 21:47:34 +00005523 draw_info->primitive=AcquireString(ellipse);
cristy018f07f2011-09-04 21:15:19 +00005524 (void) DrawImage(oval_image,draw_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00005525 draw_info=DestroyDrawInfo(draw_info);
cristyaa2c16c2012-03-25 22:21:35 +00005526 blur_image=BlurImage(oval_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +00005527 oval_image=DestroyImage(oval_image);
5528 if (blur_image == (Image *) NULL)
5529 {
5530 canvas_image=DestroyImage(canvas_image);
5531 return((Image *) NULL);
5532 }
5533 blur_image->matte=MagickFalse;
cristy39172402012-03-30 13:04:39 +00005534 (void) CompositeImage(canvas_image,blur_image,IntensityCompositeOp,MagickTrue,
5535 0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00005536 blur_image=DestroyImage(blur_image);
5537 vignette_image=MergeImageLayers(canvas_image,FlattenLayer,exception);
5538 canvas_image=DestroyImage(canvas_image);
5539 return(vignette_image);
5540}
5541
5542/*
5543%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5544% %
5545% %
5546% %
5547% W a v e I m a g e %
5548% %
5549% %
5550% %
5551%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5552%
5553% WaveImage() creates a "ripple" effect in the image by shifting the pixels
cristycee97112010-05-28 00:44:52 +00005554% vertically along a sine wave whose amplitude and wavelength is specified
cristy3ed852e2009-09-05 21:47:34 +00005555% by the given parameters.
5556%
5557% The format of the WaveImage method is:
5558%
5559% Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005560% const double wave_length,const PixelInterpolateMethod method,
5561% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005562%
5563% A description of each parameter follows:
5564%
5565% o image: the image.
5566%
5567% o amplitude, wave_length: Define the amplitude and wave length of the
5568% sine wave.
5569%
cristy5c4e2582011-09-11 19:21:03 +00005570% o interpolate: the pixel interpolation method.
5571%
cristy3ed852e2009-09-05 21:47:34 +00005572% o exception: return any errors or warnings in this structure.
5573%
5574*/
5575MagickExport Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005576 const double wave_length,const PixelInterpolateMethod method,
5577 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005578{
5579#define WaveImageTag "Wave/Image"
5580
cristyfa112112010-01-04 17:48:07 +00005581 CacheView
cristyd76c51e2011-03-26 00:21:26 +00005582 *image_view,
cristyfa112112010-01-04 17:48:07 +00005583 *wave_view;
5584
cristy3ed852e2009-09-05 21:47:34 +00005585 Image
5586 *wave_image;
5587
cristy3ed852e2009-09-05 21:47:34 +00005588 MagickBooleanType
5589 status;
5590
cristybb503372010-05-27 20:51:26 +00005591 MagickOffsetType
5592 progress;
5593
cristy3ed852e2009-09-05 21:47:34 +00005594 MagickRealType
5595 *sine_map;
5596
cristybb503372010-05-27 20:51:26 +00005597 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005598 i;
5599
cristybb503372010-05-27 20:51:26 +00005600 ssize_t
5601 y;
5602
cristy3ed852e2009-09-05 21:47:34 +00005603 /*
5604 Initialize wave image attributes.
5605 */
5606 assert(image != (Image *) NULL);
5607 assert(image->signature == MagickSignature);
5608 if (image->debug != MagickFalse)
5609 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5610 assert(exception != (ExceptionInfo *) NULL);
5611 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00005612 wave_image=CloneImage(image,image->columns,(size_t) (image->rows+2.0*
cristy3ed852e2009-09-05 21:47:34 +00005613 fabs(amplitude)),MagickTrue,exception);
5614 if (wave_image == (Image *) NULL)
5615 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005616 if (SetImageStorageClass(wave_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005617 {
cristy3ed852e2009-09-05 21:47:34 +00005618 wave_image=DestroyImage(wave_image);
5619 return((Image *) NULL);
5620 }
cristy4c08aed2011-07-01 19:47:50 +00005621 if (wave_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00005622 wave_image->matte=MagickTrue;
5623 /*
5624 Allocate sine map.
5625 */
5626 sine_map=(MagickRealType *) AcquireQuantumMemory((size_t) wave_image->columns,
5627 sizeof(*sine_map));
5628 if (sine_map == (MagickRealType *) NULL)
5629 {
5630 wave_image=DestroyImage(wave_image);
5631 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
5632 }
cristybb503372010-05-27 20:51:26 +00005633 for (i=0; i < (ssize_t) wave_image->columns; i++)
cristy4205a3c2010-09-12 20:19:59 +00005634 sine_map[i]=fabs(amplitude)+amplitude*sin((double) ((2.0*MagickPI*i)/
5635 wave_length));
cristy3ed852e2009-09-05 21:47:34 +00005636 /*
5637 Wave image.
5638 */
5639 status=MagickTrue;
5640 progress=0;
cristydb070952012-04-20 14:33:00 +00005641 image_view=AcquireVirtualCacheView(image,exception);
5642 wave_view=AcquireAuthenticCacheView(wave_image,exception);
cristyd76c51e2011-03-26 00:21:26 +00005643 (void) SetCacheViewVirtualPixelMethod(image_view,
5644 BackgroundVirtualPixelMethod);
cristyb5d5f722009-11-04 03:03:49 +00005645#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00005646 #pragma omp parallel for schedule(static,4) shared(progress,status) \
5647 if ((image->rows*image->columns) > 8192) \
5648 num_threads(GetMagickResourceLimit(ThreadResource))
cristy3ed852e2009-09-05 21:47:34 +00005649#endif
cristybb503372010-05-27 20:51:26 +00005650 for (y=0; y < (ssize_t) wave_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005651 {
cristy4c08aed2011-07-01 19:47:50 +00005652 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005653 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005654
cristye97bb922011-04-03 01:36:52 +00005655 register ssize_t
5656 x;
5657
cristy3ed852e2009-09-05 21:47:34 +00005658 if (status == MagickFalse)
5659 continue;
5660 q=QueueCacheViewAuthenticPixels(wave_view,0,y,wave_image->columns,1,
5661 exception);
cristyacd2ed22011-08-30 01:44:23 +00005662 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005663 {
5664 status=MagickFalse;
5665 continue;
5666 }
cristybb503372010-05-27 20:51:26 +00005667 for (x=0; x < (ssize_t) wave_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005668 {
cristy5c4e2582011-09-11 19:21:03 +00005669 status=InterpolatePixelChannels(image,image_view,wave_image,method,
5670 (double) x,(double) (y-sine_map[x]),q,exception);
cristyed231572011-07-14 02:18:59 +00005671 q+=GetPixelChannels(wave_image);
cristy3ed852e2009-09-05 21:47:34 +00005672 }
5673 if (SyncCacheViewAuthenticPixels(wave_view,exception) == MagickFalse)
5674 status=MagickFalse;
5675 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5676 {
5677 MagickBooleanType
5678 proceed;
5679
cristyb5d5f722009-11-04 03:03:49 +00005680#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005681 #pragma omp critical (MagickCore_WaveImage)
cristy3ed852e2009-09-05 21:47:34 +00005682#endif
5683 proceed=SetImageProgress(image,WaveImageTag,progress++,image->rows);
5684 if (proceed == MagickFalse)
5685 status=MagickFalse;
5686 }
5687 }
5688 wave_view=DestroyCacheView(wave_view);
cristyd76c51e2011-03-26 00:21:26 +00005689 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00005690 sine_map=(MagickRealType *) RelinquishMagickMemory(sine_map);
5691 if (status == MagickFalse)
5692 wave_image=DestroyImage(wave_image);
5693 return(wave_image);
5694}