blob: 80bfabce0defba2f14fbdd29620aba1557a13a43 [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*");
cristy8fbb96d2012-07-18 01:18:52 +0000203 (void) SubstituteString(&fx_info->expression,"E-1.0*","E-");
204 (void) SubstituteString(&fx_info->expression,"e-1.0*","e-");
cristy8b8a3ae2010-10-23 18:49:46 +0000205 /*
cristy3ed852e2009-09-05 21:47:34 +0000206 Convert complex to simple operators.
207 */
208 fx_op[1]='\0';
209 *fx_op=(char) LeftShiftOperator;
210 (void) SubstituteString(&fx_info->expression,"<<",fx_op);
211 *fx_op=(char) RightShiftOperator;
212 (void) SubstituteString(&fx_info->expression,">>",fx_op);
213 *fx_op=(char) LessThanEqualOperator;
214 (void) SubstituteString(&fx_info->expression,"<=",fx_op);
215 *fx_op=(char) GreaterThanEqualOperator;
216 (void) SubstituteString(&fx_info->expression,">=",fx_op);
217 *fx_op=(char) EqualOperator;
218 (void) SubstituteString(&fx_info->expression,"==",fx_op);
219 *fx_op=(char) NotEqualOperator;
220 (void) SubstituteString(&fx_info->expression,"!=",fx_op);
221 *fx_op=(char) LogicalAndOperator;
222 (void) SubstituteString(&fx_info->expression,"&&",fx_op);
223 *fx_op=(char) LogicalOrOperator;
224 (void) SubstituteString(&fx_info->expression,"||",fx_op);
cristy116af162010-08-13 01:25:47 +0000225 *fx_op=(char) ExponentialNotation;
226 (void) SubstituteString(&fx_info->expression,"**",fx_op);
cristy3ed852e2009-09-05 21:47:34 +0000227 return(fx_info);
228}
229
230/*
231%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
232% %
233% %
234% %
235% A d d N o i s e I m a g e %
236% %
237% %
238% %
239%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
240%
241% AddNoiseImage() adds random noise to the image.
242%
243% The format of the AddNoiseImage method is:
244%
245% Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
cristy9ed1f812011-10-08 02:00:08 +0000246% const double attenuate,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000247%
248% A description of each parameter follows:
249%
250% o image: the image.
251%
252% o channel: the channel type.
253%
254% o noise_type: The type of noise: Uniform, Gaussian, Multiplicative,
255% Impulse, Laplacian, or Poisson.
256%
cristy9ed1f812011-10-08 02:00:08 +0000257% o attenuate: attenuate the random distribution.
258%
cristy3ed852e2009-09-05 21:47:34 +0000259% o exception: return any errors or warnings in this structure.
260%
261*/
cristy9ed1f812011-10-08 02:00:08 +0000262MagickExport Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
263 const double attenuate,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000264{
265#define AddNoiseImageTag "AddNoise/Image"
266
cristyfa112112010-01-04 17:48:07 +0000267 CacheView
268 *image_view,
269 *noise_view;
270
cristy3ed852e2009-09-05 21:47:34 +0000271 Image
272 *noise_image;
273
cristy3ed852e2009-09-05 21:47:34 +0000274 MagickBooleanType
275 status;
276
cristybb503372010-05-27 20:51:26 +0000277 MagickOffsetType
278 progress;
279
cristy3ed852e2009-09-05 21:47:34 +0000280 RandomInfo
cristyfa112112010-01-04 17:48:07 +0000281 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +0000282
cristybb503372010-05-27 20:51:26 +0000283 ssize_t
284 y;
285
cristy57340e02012-05-05 00:53:23 +0000286 unsigned long
287 key;
288
cristy3ed852e2009-09-05 21:47:34 +0000289 /*
290 Initialize noise image attributes.
291 */
292 assert(image != (const Image *) NULL);
293 assert(image->signature == MagickSignature);
294 if (image->debug != MagickFalse)
295 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
296 assert(exception != (ExceptionInfo *) NULL);
297 assert(exception->signature == MagickSignature);
cristyb2145892011-10-10 00:55:32 +0000298 noise_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000299 if (noise_image == (Image *) NULL)
300 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000301 if (SetImageStorageClass(noise_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000302 {
cristy3ed852e2009-09-05 21:47:34 +0000303 noise_image=DestroyImage(noise_image);
304 return((Image *) NULL);
305 }
cristy98ccd782012-07-09 11:22:59 +0000306 if (IsGrayColorspace(image->colorspace) != MagickFalse)
cristyb09db112012-07-11 12:04:31 +0000307 (void) TransformImageColorspace(noise_image,RGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000308 /*
309 Add noise in each row.
310 */
cristy3ed852e2009-09-05 21:47:34 +0000311 status=MagickTrue;
312 progress=0;
313 random_info=AcquireRandomInfoThreadSet();
cristydb070952012-04-20 14:33:00 +0000314 image_view=AcquireVirtualCacheView(image,exception);
315 noise_view=AcquireAuthenticCacheView(noise_image,exception);
cristy57340e02012-05-05 00:53:23 +0000316 key=GetRandomSecretKey(random_info[0]);
cristy319a1e72010-02-21 15:13:11 +0000317#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy57340e02012-05-05 00:53:23 +0000318 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +0000319 dynamic_number_threads(image,image->columns,image->rows,key == ~0UL)
cristy3ed852e2009-09-05 21:47:34 +0000320#endif
cristybb503372010-05-27 20:51:26 +0000321 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000322 {
cristy5c9e6f22010-09-17 17:31:01 +0000323 const int
324 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +0000325
cristy3ed852e2009-09-05 21:47:34 +0000326 MagickBooleanType
327 sync;
328
cristy4c08aed2011-07-01 19:47:50 +0000329 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000330 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000331
cristybb503372010-05-27 20:51:26 +0000332 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000333 x;
334
cristy4c08aed2011-07-01 19:47:50 +0000335 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000336 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000337
338 if (status == MagickFalse)
339 continue;
340 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +0000341 q=QueueCacheViewAuthenticPixels(noise_view,0,y,noise_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +0000342 exception);
cristy4c08aed2011-07-01 19:47:50 +0000343 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000344 {
345 status=MagickFalse;
346 continue;
347 }
cristybb503372010-05-27 20:51:26 +0000348 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000349 {
cristy850b3072011-10-08 01:38:05 +0000350 register ssize_t
351 i;
352
353 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
354 {
355 PixelChannel
356 channel;
357
358 PixelTrait
359 noise_traits,
360 traits;
361
cristycf1296e2012-08-26 23:40:49 +0000362 channel=GetPixelChannelChannel(image,i);
363 traits=GetPixelChannelTraits(image,channel);
364 noise_traits=GetPixelChannelTraits(noise_image,channel);
cristy850b3072011-10-08 01:38:05 +0000365 if ((traits == UndefinedPixelTrait) ||
366 (noise_traits == UndefinedPixelTrait))
367 continue;
cristy1eced092012-08-10 23:10:56 +0000368 if (((noise_traits & CopyPixelTrait) != 0) ||
369 (GetPixelMask(image,p) != 0))
cristyb2145892011-10-10 00:55:32 +0000370 {
371 SetPixelChannel(noise_image,channel,p[i],q);
372 continue;
373 }
cristy850b3072011-10-08 01:38:05 +0000374 SetPixelChannel(noise_image,channel,ClampToQuantum(
375 GenerateDifferentialNoise(random_info[id],p[i],noise_type,attenuate)),
376 q);
377 }
cristyed231572011-07-14 02:18:59 +0000378 p+=GetPixelChannels(image);
379 q+=GetPixelChannels(noise_image);
cristy3ed852e2009-09-05 21:47:34 +0000380 }
381 sync=SyncCacheViewAuthenticPixels(noise_view,exception);
382 if (sync == MagickFalse)
383 status=MagickFalse;
384 if (image->progress_monitor != (MagickProgressMonitor) NULL)
385 {
386 MagickBooleanType
387 proceed;
388
cristyb5d5f722009-11-04 03:03:49 +0000389#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy319a1e72010-02-21 15:13:11 +0000390 #pragma omp critical (MagickCore_AddNoiseImage)
cristy3ed852e2009-09-05 21:47:34 +0000391#endif
392 proceed=SetImageProgress(image,AddNoiseImageTag,progress++,
393 image->rows);
394 if (proceed == MagickFalse)
395 status=MagickFalse;
396 }
397 }
398 noise_view=DestroyCacheView(noise_view);
399 image_view=DestroyCacheView(image_view);
400 random_info=DestroyRandomInfoThreadSet(random_info);
401 if (status == MagickFalse)
402 noise_image=DestroyImage(noise_image);
403 return(noise_image);
404}
405
406/*
407%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
408% %
409% %
410% %
411% B l u e S h i f t I m a g e %
412% %
413% %
414% %
415%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
416%
417% BlueShiftImage() mutes the colors of the image to simulate a scene at
418% nighttime in the moonlight.
419%
420% The format of the BlueShiftImage method is:
421%
422% Image *BlueShiftImage(const Image *image,const double factor,
423% ExceptionInfo *exception)
424%
425% A description of each parameter follows:
426%
427% o image: the image.
428%
429% o factor: the shift factor.
430%
431% o exception: return any errors or warnings in this structure.
432%
433*/
434MagickExport Image *BlueShiftImage(const Image *image,const double factor,
435 ExceptionInfo *exception)
436{
437#define BlueShiftImageTag "BlueShift/Image"
438
cristyc4c8d132010-01-07 01:58:38 +0000439 CacheView
440 *image_view,
441 *shift_view;
442
cristy3ed852e2009-09-05 21:47:34 +0000443 Image
444 *shift_image;
445
cristy3ed852e2009-09-05 21:47:34 +0000446 MagickBooleanType
447 status;
448
cristybb503372010-05-27 20:51:26 +0000449 MagickOffsetType
450 progress;
451
452 ssize_t
453 y;
454
cristy3ed852e2009-09-05 21:47:34 +0000455 /*
456 Allocate blue shift image.
457 */
458 assert(image != (const Image *) NULL);
459 assert(image->signature == MagickSignature);
460 if (image->debug != MagickFalse)
461 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
462 assert(exception != (ExceptionInfo *) NULL);
463 assert(exception->signature == MagickSignature);
cristya6d7a9b2012-01-18 20:04:48 +0000464 shift_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000465 if (shift_image == (Image *) NULL)
466 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000467 if (SetImageStorageClass(shift_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000468 {
cristy3ed852e2009-09-05 21:47:34 +0000469 shift_image=DestroyImage(shift_image);
470 return((Image *) NULL);
471 }
472 /*
473 Blue-shift DirectClass image.
474 */
475 status=MagickTrue;
476 progress=0;
cristydb070952012-04-20 14:33:00 +0000477 image_view=AcquireVirtualCacheView(image,exception);
478 shift_view=AcquireAuthenticCacheView(shift_image,exception);
cristy319a1e72010-02-21 15:13:11 +0000479#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000480 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +0000481 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +0000482#endif
cristybb503372010-05-27 20:51:26 +0000483 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000484 {
485 MagickBooleanType
486 sync;
487
cristy4c08aed2011-07-01 19:47:50 +0000488 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000489 pixel;
490
491 Quantum
492 quantum;
493
cristy4c08aed2011-07-01 19:47:50 +0000494 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000495 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000496
cristybb503372010-05-27 20:51:26 +0000497 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000498 x;
499
cristy4c08aed2011-07-01 19:47:50 +0000500 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000501 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000502
503 if (status == MagickFalse)
504 continue;
505 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
506 q=QueueCacheViewAuthenticPixels(shift_view,0,y,shift_image->columns,1,
507 exception);
cristy4c08aed2011-07-01 19:47:50 +0000508 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000509 {
510 status=MagickFalse;
511 continue;
512 }
cristybb503372010-05-27 20:51:26 +0000513 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000514 {
cristy4c08aed2011-07-01 19:47:50 +0000515 quantum=GetPixelRed(image,p);
516 if (GetPixelGreen(image,p) < quantum)
517 quantum=GetPixelGreen(image,p);
518 if (GetPixelBlue(image,p) < quantum)
519 quantum=GetPixelBlue(image,p);
520 pixel.red=0.5*(GetPixelRed(image,p)+factor*quantum);
521 pixel.green=0.5*(GetPixelGreen(image,p)+factor*quantum);
522 pixel.blue=0.5*(GetPixelBlue(image,p)+factor*quantum);
523 quantum=GetPixelRed(image,p);
524 if (GetPixelGreen(image,p) > quantum)
525 quantum=GetPixelGreen(image,p);
526 if (GetPixelBlue(image,p) > quantum)
527 quantum=GetPixelBlue(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000528 pixel.red=0.5*(pixel.red+factor*quantum);
529 pixel.green=0.5*(pixel.green+factor*quantum);
530 pixel.blue=0.5*(pixel.blue+factor*quantum);
cristy4c08aed2011-07-01 19:47:50 +0000531 SetPixelRed(shift_image,ClampToQuantum(pixel.red),q);
532 SetPixelGreen(shift_image,ClampToQuantum(pixel.green),q);
533 SetPixelBlue(shift_image,ClampToQuantum(pixel.blue),q);
cristyed231572011-07-14 02:18:59 +0000534 p+=GetPixelChannels(image);
535 q+=GetPixelChannels(shift_image);
cristy3ed852e2009-09-05 21:47:34 +0000536 }
537 sync=SyncCacheViewAuthenticPixels(shift_view,exception);
538 if (sync == MagickFalse)
539 status=MagickFalse;
540 if (image->progress_monitor != (MagickProgressMonitor) NULL)
541 {
542 MagickBooleanType
543 proceed;
544
cristy319a1e72010-02-21 15:13:11 +0000545#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +0000546 #pragma omp critical (MagickCore_BlueShiftImage)
cristy3ed852e2009-09-05 21:47:34 +0000547#endif
548 proceed=SetImageProgress(image,BlueShiftImageTag,progress++,
549 image->rows);
550 if (proceed == MagickFalse)
551 status=MagickFalse;
552 }
553 }
554 image_view=DestroyCacheView(image_view);
555 shift_view=DestroyCacheView(shift_view);
556 if (status == MagickFalse)
557 shift_image=DestroyImage(shift_image);
558 return(shift_image);
559}
560
561/*
562%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
563% %
564% %
565% %
566% C h a r c o a l I m a g e %
567% %
568% %
569% %
570%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
571%
572% CharcoalImage() creates a new image that is a copy of an existing one with
573% the edge highlighted. It allocates the memory necessary for the new Image
574% structure and returns a pointer to the new image.
575%
576% The format of the CharcoalImage method is:
577%
578% Image *CharcoalImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +0000579% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000580%
581% A description of each parameter follows:
582%
583% o image: the image.
584%
585% o radius: the radius of the pixel neighborhood.
586%
587% o sigma: the standard deviation of the Gaussian, in pixels.
588%
589% o exception: return any errors or warnings in this structure.
590%
591*/
592MagickExport Image *CharcoalImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +0000593 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000594{
595 Image
596 *charcoal_image,
597 *clone_image,
598 *edge_image;
599
600 assert(image != (Image *) NULL);
601 assert(image->signature == MagickSignature);
602 if (image->debug != MagickFalse)
603 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
604 assert(exception != (ExceptionInfo *) NULL);
605 assert(exception->signature == MagickSignature);
606 clone_image=CloneImage(image,0,0,MagickTrue,exception);
607 if (clone_image == (Image *) NULL)
608 return((Image *) NULL);
cristy018f07f2011-09-04 21:15:19 +0000609 (void) SetImageType(clone_image,GrayscaleType,exception);
cristy8ae632d2011-09-05 17:29:53 +0000610 edge_image=EdgeImage(clone_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +0000611 clone_image=DestroyImage(clone_image);
612 if (edge_image == (Image *) NULL)
613 return((Image *) NULL);
cristyaa2c16c2012-03-25 22:21:35 +0000614 charcoal_image=BlurImage(edge_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +0000615 edge_image=DestroyImage(edge_image);
616 if (charcoal_image == (Image *) NULL)
617 return((Image *) NULL);
cristye23ec9d2011-08-16 18:15:40 +0000618 (void) NormalizeImage(charcoal_image,exception);
cristyb3e7c6c2011-07-24 01:43:55 +0000619 (void) NegateImage(charcoal_image,MagickFalse,exception);
cristy018f07f2011-09-04 21:15:19 +0000620 (void) SetImageType(charcoal_image,GrayscaleType,exception);
cristy3ed852e2009-09-05 21:47:34 +0000621 return(charcoal_image);
622}
623
624/*
625%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
626% %
627% %
628% %
629% C o l o r i z e I m a g e %
630% %
631% %
632% %
633%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
634%
635% ColorizeImage() blends the fill color with each pixel in the image.
636% A percentage blend is specified with opacity. Control the application
637% of different color components by specifying a different percentage for
638% each component (e.g. 90/100/10 is 90% red, 100% green, and 10% blue).
639%
640% The format of the ColorizeImage method is:
641%
cristyc7e6ff62011-10-03 13:46:11 +0000642% Image *ColorizeImage(const Image *image,const char *blend,
643% const PixelInfo *colorize,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000644%
645% A description of each parameter follows:
646%
647% o image: the image.
648%
cristyc7e6ff62011-10-03 13:46:11 +0000649% o blend: A character string indicating the level of blending as a
cristy3ed852e2009-09-05 21:47:34 +0000650% percentage.
651%
652% o colorize: A color value.
653%
654% o exception: return any errors or warnings in this structure.
655%
656*/
cristyc7e6ff62011-10-03 13:46:11 +0000657MagickExport Image *ColorizeImage(const Image *image,const char *blend,
658 const PixelInfo *colorize,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000659{
660#define ColorizeImageTag "Colorize/Image"
cristy0cf6da52012-08-25 00:35:24 +0000661#define Colorize(pixel,blend_percentage,colorize) \
662 (((pixel)*(100.0-(blend_percentage))+(colorize)*(blend_percentage))/100.0)
cristy3ed852e2009-09-05 21:47:34 +0000663
cristyc4c8d132010-01-07 01:58:38 +0000664 CacheView
665 *colorize_view,
666 *image_view;
667
cristy3ed852e2009-09-05 21:47:34 +0000668 GeometryInfo
669 geometry_info;
670
671 Image
672 *colorize_image;
673
cristy3ed852e2009-09-05 21:47:34 +0000674 MagickBooleanType
675 status;
676
cristybb503372010-05-27 20:51:26 +0000677 MagickOffsetType
678 progress;
679
cristy3ed852e2009-09-05 21:47:34 +0000680 MagickStatusType
681 flags;
682
cristyc7e6ff62011-10-03 13:46:11 +0000683 PixelInfo
cristy20c3aed2012-04-10 01:06:21 +0000684 blend_percentage;
cristyc7e6ff62011-10-03 13:46:11 +0000685
cristybb503372010-05-27 20:51:26 +0000686 ssize_t
687 y;
688
cristy3ed852e2009-09-05 21:47:34 +0000689 /*
690 Allocate colorized image.
691 */
692 assert(image != (const Image *) NULL);
693 assert(image->signature == MagickSignature);
694 if (image->debug != MagickFalse)
695 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
696 assert(exception != (ExceptionInfo *) NULL);
697 assert(exception->signature == MagickSignature);
cristy768165d2012-04-09 15:01:35 +0000698 colorize_image=CloneImage(image,image->columns,image->rows,MagickTrue,
699 exception);
700 if (colorize_image == (Image *) NULL)
701 return((Image *) NULL);
702 if (SetImageStorageClass(colorize_image,DirectClass,exception) == MagickFalse)
703 {
704 colorize_image=DestroyImage(colorize_image);
705 return((Image *) NULL);
706 }
cristy9b7a4fc2012-04-08 22:26:56 +0000707 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
cristy20c3aed2012-04-10 01:06:21 +0000708 (IsPixelInfoGray(colorize) != MagickFalse))
cristyb09db112012-07-11 12:04:31 +0000709 (void) SetImageColorspace(colorize_image,RGBColorspace,exception);
cristy8a46d822012-08-28 23:32:39 +0000710 if ((colorize_image->alpha_trait != BlendPixelTrait) &&
711 (colorize->alpha_trait == BlendPixelTrait))
cristy768165d2012-04-09 15:01:35 +0000712 (void) SetImageAlpha(colorize_image,OpaqueAlpha,exception);
713 if (blend == (const char *) NULL)
714 return(colorize_image);
cristy20c3aed2012-04-10 01:06:21 +0000715 GetPixelInfo(image,&blend_percentage);
716 flags=ParseGeometry(blend,&geometry_info);
717 blend_percentage.red=geometry_info.rho;
718 blend_percentage.green=geometry_info.rho;
719 blend_percentage.blue=geometry_info.rho;
720 blend_percentage.black=geometry_info.rho;
cristy3ee7de52012-04-14 23:40:47 +0000721 blend_percentage.alpha=geometry_info.rho;
cristy20c3aed2012-04-10 01:06:21 +0000722 if ((flags & SigmaValue) != 0)
723 blend_percentage.green=geometry_info.sigma;
724 if ((flags & XiValue) != 0)
725 blend_percentage.blue=geometry_info.xi;
726 if ((flags & PsiValue) != 0)
727 blend_percentage.alpha=geometry_info.psi;
728 if (blend_percentage.colorspace == CMYKColorspace)
729 {
730 if ((flags & PsiValue) != 0)
731 blend_percentage.black=geometry_info.psi;
732 if ((flags & ChiValue) != 0)
733 blend_percentage.alpha=geometry_info.chi;
734 }
cristy3ed852e2009-09-05 21:47:34 +0000735 /*
736 Colorize DirectClass image.
737 */
738 status=MagickTrue;
739 progress=0;
cristydb070952012-04-20 14:33:00 +0000740 image_view=AcquireVirtualCacheView(image,exception);
741 colorize_view=AcquireAuthenticCacheView(colorize_image,exception);
cristy319a1e72010-02-21 15:13:11 +0000742#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000743 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +0000744 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +0000745#endif
cristybb503372010-05-27 20:51:26 +0000746 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000747 {
748 MagickBooleanType
749 sync;
750
cristy4c08aed2011-07-01 19:47:50 +0000751 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000752 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000753
cristy4c08aed2011-07-01 19:47:50 +0000754 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000755 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000756
cristy4a37c622012-08-23 18:47:56 +0000757 register ssize_t
758 x;
759
cristy3ed852e2009-09-05 21:47:34 +0000760 if (status == MagickFalse)
761 continue;
762 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
763 q=QueueCacheViewAuthenticPixels(colorize_view,0,y,colorize_image->columns,1,
764 exception);
cristy4c08aed2011-07-01 19:47:50 +0000765 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000766 {
767 status=MagickFalse;
768 continue;
769 }
cristybb503372010-05-27 20:51:26 +0000770 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000771 {
cristy0cf6da52012-08-25 00:35:24 +0000772 register ssize_t
773 i;
774
775 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
776 {
777 PixelChannel
778 channel;
779
780 PixelTrait
781 colorize_traits,
782 traits;
783
cristycf1296e2012-08-26 23:40:49 +0000784 channel=GetPixelChannelChannel(image,i);
785 traits=GetPixelChannelTraits(image,channel);
786 colorize_traits=GetPixelChannelTraits(colorize_image,channel);
cristy0cf6da52012-08-25 00:35:24 +0000787 if ((traits == UndefinedPixelTrait) ||
788 (colorize_traits == UndefinedPixelTrait))
cristyd6803382012-04-10 01:41:25 +0000789 continue;
cristy0cf6da52012-08-25 00:35:24 +0000790 if (((colorize_traits & CopyPixelTrait) != 0) ||
791 (GetPixelMask(image,p) != 0))
792 {
793 SetPixelChannel(colorize_image,channel,p[i],q);
794 continue;
795 }
cristycf1296e2012-08-26 23:40:49 +0000796 SetPixelChannel(colorize_image,channel,
797 ClampToQuantum(Colorize(p[i],GetPixelInfoChannel(&blend_percentage,
798 channel),GetPixelInfoChannel(colorize,channel))),q);
cristy0cf6da52012-08-25 00:35:24 +0000799 }
cristyed231572011-07-14 02:18:59 +0000800 p+=GetPixelChannels(image);
801 q+=GetPixelChannels(colorize_image);
cristy3ed852e2009-09-05 21:47:34 +0000802 }
803 sync=SyncCacheViewAuthenticPixels(colorize_view,exception);
804 if (sync == MagickFalse)
805 status=MagickFalse;
806 if (image->progress_monitor != (MagickProgressMonitor) NULL)
807 {
808 MagickBooleanType
809 proceed;
810
cristy319a1e72010-02-21 15:13:11 +0000811#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +0000812 #pragma omp critical (MagickCore_ColorizeImage)
cristy3ed852e2009-09-05 21:47:34 +0000813#endif
814 proceed=SetImageProgress(image,ColorizeImageTag,progress++,image->rows);
815 if (proceed == MagickFalse)
816 status=MagickFalse;
817 }
818 }
819 image_view=DestroyCacheView(image_view);
820 colorize_view=DestroyCacheView(colorize_view);
821 if (status == MagickFalse)
822 colorize_image=DestroyImage(colorize_image);
823 return(colorize_image);
824}
825
826/*
827%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
828% %
829% %
830% %
cristye6365592010-04-02 17:31:23 +0000831% C o l o r M a t r i x I m a g e %
832% %
833% %
834% %
835%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
836%
837% ColorMatrixImage() applies color transformation to an image. This method
838% permits saturation changes, hue rotation, luminance to alpha, and various
839% other effects. Although variable-sized transformation matrices can be used,
840% typically one uses a 5x5 matrix for an RGBA image and a 6x6 for CMYKA
841% (or RGBA with offsets). The matrix is similar to those used by Adobe Flash
842% except offsets are in column 6 rather than 5 (in support of CMYKA images)
843% and offsets are normalized (divide Flash offset by 255).
844%
845% The format of the ColorMatrixImage method is:
846%
847% Image *ColorMatrixImage(const Image *image,
848% const KernelInfo *color_matrix,ExceptionInfo *exception)
849%
850% A description of each parameter follows:
851%
852% o image: the image.
853%
854% o color_matrix: the color matrix.
855%
856% o exception: return any errors or warnings in this structure.
857%
858*/
anthonyfd706f92012-01-19 04:22:02 +0000859/* FUTURE: modify to make use of a MagickMatrix Mutliply function
860 That should be provided in "matrix.c"
861 (ASIDE: actually distorts should do this too but currently doesn't)
862*/
863
cristye6365592010-04-02 17:31:23 +0000864MagickExport Image *ColorMatrixImage(const Image *image,
865 const KernelInfo *color_matrix,ExceptionInfo *exception)
866{
867#define ColorMatrixImageTag "ColorMatrix/Image"
868
869 CacheView
870 *color_view,
871 *image_view;
872
873 double
874 ColorMatrix[6][6] =
875 {
876 { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
877 { 0.0, 1.0, 0.0, 0.0, 0.0, 0.0 },
878 { 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 },
879 { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0 },
880 { 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 },
881 { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 }
882 };
883
884 Image
885 *color_image;
886
cristye6365592010-04-02 17:31:23 +0000887 MagickBooleanType
888 status;
889
cristybb503372010-05-27 20:51:26 +0000890 MagickOffsetType
891 progress;
892
893 register ssize_t
cristye6365592010-04-02 17:31:23 +0000894 i;
895
cristybb503372010-05-27 20:51:26 +0000896 ssize_t
897 u,
898 v,
899 y;
900
cristye6365592010-04-02 17:31:23 +0000901 /*
anthonyfd706f92012-01-19 04:22:02 +0000902 Map given color_matrix, into a 6x6 matrix RGBKA and a constant
cristye6365592010-04-02 17:31:23 +0000903 */
904 assert(image != (Image *) NULL);
905 assert(image->signature == MagickSignature);
906 if (image->debug != MagickFalse)
907 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
908 assert(exception != (ExceptionInfo *) NULL);
909 assert(exception->signature == MagickSignature);
910 i=0;
cristybb503372010-05-27 20:51:26 +0000911 for (v=0; v < (ssize_t) color_matrix->height; v++)
912 for (u=0; u < (ssize_t) color_matrix->width; u++)
cristye6365592010-04-02 17:31:23 +0000913 {
914 if ((v < 6) && (u < 6))
915 ColorMatrix[v][u]=color_matrix->values[i];
916 i++;
917 }
918 /*
919 Initialize color image.
920 */
cristy12550e62010-06-07 12:46:40 +0000921 color_image=CloneImage(image,0,0,MagickTrue,exception);
cristye6365592010-04-02 17:31:23 +0000922 if (color_image == (Image *) NULL)
923 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000924 if (SetImageStorageClass(color_image,DirectClass,exception) == MagickFalse)
cristye6365592010-04-02 17:31:23 +0000925 {
cristye6365592010-04-02 17:31:23 +0000926 color_image=DestroyImage(color_image);
927 return((Image *) NULL);
928 }
929 if (image->debug != MagickFalse)
930 {
931 char
932 format[MaxTextExtent],
933 *message;
934
935 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
936 " ColorMatrix image with color matrix:");
937 message=AcquireString("");
938 for (v=0; v < 6; v++)
939 {
940 *message='\0';
cristyb51dff52011-05-19 16:55:47 +0000941 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
cristye6365592010-04-02 17:31:23 +0000942 (void) ConcatenateString(&message,format);
943 for (u=0; u < 6; u++)
944 {
cristyb51dff52011-05-19 16:55:47 +0000945 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",
cristye6365592010-04-02 17:31:23 +0000946 ColorMatrix[v][u]);
947 (void) ConcatenateString(&message,format);
948 }
949 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
950 }
951 message=DestroyString(message);
952 }
953 /*
anthonyfd706f92012-01-19 04:22:02 +0000954 Apply the ColorMatrix to image.
cristye6365592010-04-02 17:31:23 +0000955 */
956 status=MagickTrue;
957 progress=0;
cristydb070952012-04-20 14:33:00 +0000958 image_view=AcquireVirtualCacheView(image,exception);
959 color_view=AcquireAuthenticCacheView(color_image,exception);
cristye6365592010-04-02 17:31:23 +0000960#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000961 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +0000962 dynamic_number_threads(image,image->columns,image->rows,1)
cristye6365592010-04-02 17:31:23 +0000963#endif
cristybb503372010-05-27 20:51:26 +0000964 for (y=0; y < (ssize_t) image->rows; y++)
cristye6365592010-04-02 17:31:23 +0000965 {
cristyfcc25d92012-02-19 23:06:48 +0000966 PixelInfo
cristye6365592010-04-02 17:31:23 +0000967 pixel;
968
cristy4c08aed2011-07-01 19:47:50 +0000969 register const Quantum
cristye6365592010-04-02 17:31:23 +0000970 *restrict p;
971
cristy4c08aed2011-07-01 19:47:50 +0000972 register Quantum
973 *restrict q;
974
cristybb503372010-05-27 20:51:26 +0000975 register ssize_t
cristye6365592010-04-02 17:31:23 +0000976 x;
977
cristye6365592010-04-02 17:31:23 +0000978 if (status == MagickFalse)
979 continue;
980 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
981 q=GetCacheViewAuthenticPixels(color_view,0,y,color_image->columns,1,
982 exception);
cristy4c08aed2011-07-01 19:47:50 +0000983 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristye6365592010-04-02 17:31:23 +0000984 {
985 status=MagickFalse;
986 continue;
987 }
cristyfcc25d92012-02-19 23:06:48 +0000988 GetPixelInfo(image,&pixel);
cristybb503372010-05-27 20:51:26 +0000989 for (x=0; x < (ssize_t) image->columns; x++)
cristye6365592010-04-02 17:31:23 +0000990 {
cristybb503372010-05-27 20:51:26 +0000991 register ssize_t
cristye6365592010-04-02 17:31:23 +0000992 v;
993
cristybb503372010-05-27 20:51:26 +0000994 size_t
cristye6365592010-04-02 17:31:23 +0000995 height;
996
cristyfcc25d92012-02-19 23:06:48 +0000997 GetPixelInfoPixel(image,p,&pixel);
cristye6365592010-04-02 17:31:23 +0000998 height=color_matrix->height > 6 ? 6UL : color_matrix->height;
cristybb503372010-05-27 20:51:26 +0000999 for (v=0; v < (ssize_t) height; v++)
cristye6365592010-04-02 17:31:23 +00001000 {
cristya19f1d72012-08-07 18:24:38 +00001001 double
cristyfcc25d92012-02-19 23:06:48 +00001002 sum;
1003
1004 sum=ColorMatrix[v][0]*GetPixelRed(image,p)+ColorMatrix[v][1]*
cristy4c08aed2011-07-01 19:47:50 +00001005 GetPixelGreen(image,p)+ColorMatrix[v][2]*GetPixelBlue(image,p);
cristye6365592010-04-02 17:31:23 +00001006 if (image->colorspace == CMYKColorspace)
cristyfcc25d92012-02-19 23:06:48 +00001007 sum+=ColorMatrix[v][3]*GetPixelBlack(image,p);
cristy8a46d822012-08-28 23:32:39 +00001008 if (image->alpha_trait == BlendPixelTrait)
cristyfcc25d92012-02-19 23:06:48 +00001009 sum+=ColorMatrix[v][4]*GetPixelAlpha(image,p);
1010 sum+=QuantumRange*ColorMatrix[v][5];
cristye6365592010-04-02 17:31:23 +00001011 switch (v)
1012 {
cristyfcc25d92012-02-19 23:06:48 +00001013 case 0: pixel.red=sum; break;
1014 case 1: pixel.green=sum; break;
1015 case 2: pixel.blue=sum; break;
1016 case 3: pixel.black=sum; break;
1017 case 4: pixel.alpha=sum; break;
1018 default: break;
cristye6365592010-04-02 17:31:23 +00001019 }
1020 }
cristyfcc25d92012-02-19 23:06:48 +00001021 SetPixelInfoPixel(color_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +00001022 p+=GetPixelChannels(image);
1023 q+=GetPixelChannels(color_image);
cristye6365592010-04-02 17:31:23 +00001024 }
1025 if (SyncCacheViewAuthenticPixels(color_view,exception) == MagickFalse)
1026 status=MagickFalse;
1027 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1028 {
1029 MagickBooleanType
1030 proceed;
1031
1032#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00001033 #pragma omp critical (MagickCore_ColorMatrixImage)
cristye6365592010-04-02 17:31:23 +00001034#endif
1035 proceed=SetImageProgress(image,ColorMatrixImageTag,progress++,
1036 image->rows);
1037 if (proceed == MagickFalse)
1038 status=MagickFalse;
1039 }
1040 }
1041 color_view=DestroyCacheView(color_view);
1042 image_view=DestroyCacheView(image_view);
1043 if (status == MagickFalse)
1044 color_image=DestroyImage(color_image);
1045 return(color_image);
1046}
1047
1048/*
1049%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1050% %
1051% %
1052% %
cristy3ed852e2009-09-05 21:47:34 +00001053+ D e s t r o y F x I n f o %
1054% %
1055% %
1056% %
1057%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1058%
1059% DestroyFxInfo() deallocates memory associated with an FxInfo structure.
1060%
1061% The format of the DestroyFxInfo method is:
1062%
1063% ImageInfo *DestroyFxInfo(ImageInfo *fx_info)
1064%
1065% A description of each parameter follows:
1066%
1067% o fx_info: the fx info.
1068%
1069*/
cristy7832dc22011-09-05 01:21:53 +00001070MagickPrivate FxInfo *DestroyFxInfo(FxInfo *fx_info)
cristy3ed852e2009-09-05 21:47:34 +00001071{
cristybb503372010-05-27 20:51:26 +00001072 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001073 i;
1074
1075 fx_info->exception=DestroyExceptionInfo(fx_info->exception);
1076 fx_info->expression=DestroyString(fx_info->expression);
1077 fx_info->symbols=DestroySplayTree(fx_info->symbols);
1078 fx_info->colors=DestroySplayTree(fx_info->colors);
cristy0ea377f2011-03-24 00:54:19 +00001079 for (i=(ssize_t) GetImageListLength(fx_info->images)-1; i >= 0; i--)
cristyd76c51e2011-03-26 00:21:26 +00001080 fx_info->view[i]=DestroyCacheView(fx_info->view[i]);
1081 fx_info->view=(CacheView **) RelinquishMagickMemory(fx_info->view);
cristy3ed852e2009-09-05 21:47:34 +00001082 fx_info->random_info=DestroyRandomInfo(fx_info->random_info);
1083 fx_info=(FxInfo *) RelinquishMagickMemory(fx_info);
1084 return(fx_info);
1085}
1086
1087/*
1088%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1089% %
1090% %
1091% %
cristy3ed852e2009-09-05 21:47:34 +00001092+ 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 %
1093% %
1094% %
1095% %
1096%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1097%
1098% FxEvaluateChannelExpression() evaluates an expression and returns the
1099% results.
1100%
1101% The format of the FxEvaluateExpression method is:
1102%
cristya19f1d72012-08-07 18:24:38 +00001103% double FxEvaluateChannelExpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00001104% const PixelChannel channel,const ssize_t x,const ssize_t y,
cristya19f1d72012-08-07 18:24:38 +00001105% double *alpha,Exceptioninfo *exception)
1106% double FxEvaluateExpression(FxInfo *fx_info,
1107% double *alpha,Exceptioninfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001108%
1109% A description of each parameter follows:
1110%
1111% o fx_info: the fx info.
1112%
1113% o channel: the channel.
1114%
1115% o x,y: the pixel position.
1116%
1117% o alpha: the result.
1118%
1119% o exception: return any errors or warnings in this structure.
1120%
1121*/
1122
cristy351842f2010-03-07 15:27:38 +00001123static inline double MagickMax(const double x,const double y)
1124{
1125 if (x > y)
1126 return(x);
1127 return(y);
1128}
1129
1130static inline double MagickMin(const double x,const double y)
1131{
1132 if (x < y)
1133 return(x);
1134 return(y);
1135}
1136
cristya19f1d72012-08-07 18:24:38 +00001137static double FxChannelStatistics(FxInfo *fx_info,Image *image,
cristy0568ffc2011-07-25 16:54:14 +00001138 PixelChannel channel,const char *symbol,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001139{
cristy5048d302012-08-07 01:05:16 +00001140 ChannelType
1141 channel_mask;
1142
cristy3ed852e2009-09-05 21:47:34 +00001143 char
1144 key[MaxTextExtent],
1145 statistic[MaxTextExtent];
1146
1147 const char
1148 *value;
1149
1150 register const char
1151 *p;
1152
cristy5048d302012-08-07 01:05:16 +00001153 channel_mask=UndefinedChannel;
cristy3ed852e2009-09-05 21:47:34 +00001154 for (p=symbol; (*p != '.') && (*p != '\0'); p++) ;
1155 if (*p == '.')
cristy3ed852e2009-09-05 21:47:34 +00001156 {
cristy5048d302012-08-07 01:05:16 +00001157 ssize_t
1158 option;
1159
1160 option=ParseCommandOption(MagickPixelChannelOptions,MagickTrue,p+1);
1161 if (option >= 0)
1162 {
1163 channel=(PixelChannel) option;
1164 channel_mask=(ChannelType) (channel_mask | (1 << channel));
cristycf1296e2012-08-26 23:40:49 +00001165 SetPixelChannelMask(image,channel_mask);
cristy5048d302012-08-07 01:05:16 +00001166 }
cristy3ed852e2009-09-05 21:47:34 +00001167 }
cristyb51dff52011-05-19 16:55:47 +00001168 (void) FormatLocaleString(key,MaxTextExtent,"%p.%.20g.%s",(void *) image,
cristye8c25f92010-06-03 00:53:06 +00001169 (double) channel,symbol);
cristy3ed852e2009-09-05 21:47:34 +00001170 value=(const char *) GetValueFromSplayTree(fx_info->symbols,key);
1171 if (value != (const char *) NULL)
cristy5048d302012-08-07 01:05:16 +00001172 {
1173 if (channel_mask != UndefinedChannel)
cristycf1296e2012-08-26 23:40:49 +00001174 SetPixelChannelMask(image,channel_mask);
cristy5048d302012-08-07 01:05:16 +00001175 return(QuantumScale*StringToDouble(value,(char **) NULL));
1176 }
cristy3ed852e2009-09-05 21:47:34 +00001177 (void) DeleteNodeFromSplayTree(fx_info->symbols,key);
1178 if (LocaleNCompare(symbol,"depth",5) == 0)
1179 {
cristybb503372010-05-27 20:51:26 +00001180 size_t
cristy3ed852e2009-09-05 21:47:34 +00001181 depth;
1182
cristyfefab1b2011-07-05 00:33:22 +00001183 depth=GetImageDepth(image,exception);
1184 (void) FormatLocaleString(statistic,MaxTextExtent,"%.20g",(double) depth);
cristy3ed852e2009-09-05 21:47:34 +00001185 }
1186 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1187 {
1188 double
1189 kurtosis,
1190 skewness;
1191
cristyd42d9952011-07-08 14:21:50 +00001192 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
cristyb51dff52011-05-19 16:55:47 +00001193 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",kurtosis);
cristy3ed852e2009-09-05 21:47:34 +00001194 }
1195 if (LocaleNCompare(symbol,"maxima",6) == 0)
1196 {
1197 double
1198 maxima,
1199 minima;
1200
cristyd42d9952011-07-08 14:21:50 +00001201 (void) GetImageRange(image,&minima,&maxima,exception);
cristyb51dff52011-05-19 16:55:47 +00001202 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",maxima);
cristy3ed852e2009-09-05 21:47:34 +00001203 }
1204 if (LocaleNCompare(symbol,"mean",4) == 0)
1205 {
1206 double
1207 mean,
1208 standard_deviation;
1209
cristyd42d9952011-07-08 14:21:50 +00001210 (void) GetImageMean(image,&mean,&standard_deviation,exception);
cristyb51dff52011-05-19 16:55:47 +00001211 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",mean);
cristy3ed852e2009-09-05 21:47:34 +00001212 }
1213 if (LocaleNCompare(symbol,"minima",6) == 0)
1214 {
1215 double
1216 maxima,
1217 minima;
1218
cristyd42d9952011-07-08 14:21:50 +00001219 (void) GetImageRange(image,&minima,&maxima,exception);
cristyb51dff52011-05-19 16:55:47 +00001220 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",minima);
cristy3ed852e2009-09-05 21:47:34 +00001221 }
1222 if (LocaleNCompare(symbol,"skewness",8) == 0)
1223 {
1224 double
1225 kurtosis,
1226 skewness;
1227
cristyd42d9952011-07-08 14:21:50 +00001228 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
cristyb51dff52011-05-19 16:55:47 +00001229 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",skewness);
cristy3ed852e2009-09-05 21:47:34 +00001230 }
1231 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1232 {
1233 double
1234 mean,
1235 standard_deviation;
1236
cristyd42d9952011-07-08 14:21:50 +00001237 (void) GetImageMean(image,&mean,&standard_deviation,exception);
cristyb51dff52011-05-19 16:55:47 +00001238 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",
cristy3ed852e2009-09-05 21:47:34 +00001239 standard_deviation);
1240 }
cristy5048d302012-08-07 01:05:16 +00001241 if (channel_mask != UndefinedChannel)
cristycf1296e2012-08-26 23:40:49 +00001242 SetPixelChannelMask(image,channel_mask);
cristy3ed852e2009-09-05 21:47:34 +00001243 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(key),
1244 ConstantString(statistic));
cristydbdd0e32011-11-04 23:29:40 +00001245 return(QuantumScale*StringToDouble(statistic,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001246}
1247
cristya19f1d72012-08-07 18:24:38 +00001248static double
cristy0568ffc2011-07-25 16:54:14 +00001249 FxEvaluateSubexpression(FxInfo *,const PixelChannel,const ssize_t,
cristya19f1d72012-08-07 18:24:38 +00001250 const ssize_t,const char *,double *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +00001251
cristyb0aad4c2011-11-02 19:30:35 +00001252static MagickOffsetType FxGCD(MagickOffsetType alpha,MagickOffsetType beta)
1253{
1254 if (beta != 0)
1255 return(FxGCD(beta,alpha % beta));
1256 return(alpha);
1257}
1258
cristy3ed852e2009-09-05 21:47:34 +00001259static inline const char *FxSubexpression(const char *expression,
1260 ExceptionInfo *exception)
1261{
1262 const char
1263 *subexpression;
1264
cristybb503372010-05-27 20:51:26 +00001265 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001266 level;
1267
1268 level=0;
1269 subexpression=expression;
1270 while ((*subexpression != '\0') &&
1271 ((level != 1) || (strchr(")",(int) *subexpression) == (char *) NULL)))
1272 {
1273 if (strchr("(",(int) *subexpression) != (char *) NULL)
1274 level++;
1275 else
1276 if (strchr(")",(int) *subexpression) != (char *) NULL)
1277 level--;
1278 subexpression++;
1279 }
1280 if (*subexpression == '\0')
1281 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
anthonye5b39652012-04-21 05:37:29 +00001282 "UnbalancedParenthesis","'%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00001283 return(subexpression);
1284}
1285
cristya19f1d72012-08-07 18:24:38 +00001286static double FxGetSymbol(FxInfo *fx_info,const PixelChannel channel,
cristye85007d2010-06-06 22:51:36 +00001287 const ssize_t x,const ssize_t y,const char *expression,
1288 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001289{
1290 char
1291 *q,
1292 subexpression[MaxTextExtent],
1293 symbol[MaxTextExtent];
1294
1295 const char
1296 *p,
1297 *value;
1298
1299 Image
1300 *image;
1301
cristy4c08aed2011-07-01 19:47:50 +00001302 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001303 pixel;
1304
cristya19f1d72012-08-07 18:24:38 +00001305 double
cristy3ed852e2009-09-05 21:47:34 +00001306 alpha,
1307 beta;
1308
1309 PointInfo
1310 point;
1311
cristybb503372010-05-27 20:51:26 +00001312 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001313 i;
1314
1315 size_t
cristy1707c6c2012-01-18 23:30:54 +00001316 length,
cristy3ed852e2009-09-05 21:47:34 +00001317 level;
1318
1319 p=expression;
1320 i=GetImageIndexInList(fx_info->images);
1321 level=0;
1322 point.x=(double) x;
1323 point.y=(double) y;
1324 if (isalpha((int) *(p+1)) == 0)
1325 {
1326 if (strchr("suv",(int) *p) != (char *) NULL)
1327 {
1328 switch (*p)
1329 {
1330 case 's':
1331 default:
1332 {
1333 i=GetImageIndexInList(fx_info->images);
1334 break;
1335 }
1336 case 'u': i=0; break;
1337 case 'v': i=1; break;
1338 }
1339 p++;
1340 if (*p == '[')
1341 {
1342 level++;
1343 q=subexpression;
1344 for (p++; *p != '\0'; )
1345 {
1346 if (*p == '[')
1347 level++;
1348 else
1349 if (*p == ']')
1350 {
1351 level--;
1352 if (level == 0)
1353 break;
1354 }
1355 *q++=(*p++);
1356 }
1357 *q='\0';
1358 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1359 &beta,exception);
cristybb503372010-05-27 20:51:26 +00001360 i=(ssize_t) (alpha+0.5);
cristy3ed852e2009-09-05 21:47:34 +00001361 p++;
1362 }
1363 if (*p == '.')
1364 p++;
1365 }
1366 if ((isalpha((int) *(p+1)) == 0) && (*p == 'p'))
1367 {
1368 p++;
1369 if (*p == '{')
1370 {
1371 level++;
1372 q=subexpression;
1373 for (p++; *p != '\0'; )
1374 {
1375 if (*p == '{')
1376 level++;
1377 else
1378 if (*p == '}')
1379 {
1380 level--;
1381 if (level == 0)
1382 break;
1383 }
1384 *q++=(*p++);
1385 }
1386 *q='\0';
1387 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1388 &beta,exception);
1389 point.x=alpha;
1390 point.y=beta;
1391 p++;
1392 }
1393 else
1394 if (*p == '[')
1395 {
1396 level++;
1397 q=subexpression;
1398 for (p++; *p != '\0'; )
1399 {
1400 if (*p == '[')
1401 level++;
1402 else
1403 if (*p == ']')
1404 {
1405 level--;
1406 if (level == 0)
1407 break;
1408 }
1409 *q++=(*p++);
1410 }
1411 *q='\0';
1412 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1413 &beta,exception);
1414 point.x+=alpha;
1415 point.y+=beta;
1416 p++;
1417 }
1418 if (*p == '.')
1419 p++;
1420 }
1421 }
1422 length=GetImageListLength(fx_info->images);
1423 while (i < 0)
cristybb503372010-05-27 20:51:26 +00001424 i+=(ssize_t) length;
cristy3ed852e2009-09-05 21:47:34 +00001425 i%=length;
1426 image=GetImageFromList(fx_info->images,i);
1427 if (image == (Image *) NULL)
1428 {
1429 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
anthonye5b39652012-04-21 05:37:29 +00001430 "NoSuchImage","'%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00001431 return(0.0);
1432 }
cristy4c08aed2011-07-01 19:47:50 +00001433 GetPixelInfo(image,&pixel);
1434 (void) InterpolatePixelInfo(image,fx_info->view[i],image->interpolate,
cristy4f820712011-04-01 12:35:43 +00001435 point.x,point.y,&pixel,exception);
cristy1707c6c2012-01-18 23:30:54 +00001436 if ((strlen(p) > 2) && (LocaleCompare(p,"intensity") != 0) &&
1437 (LocaleCompare(p,"luminance") != 0) && (LocaleCompare(p,"hue") != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00001438 (LocaleCompare(p,"saturation") != 0) &&
1439 (LocaleCompare(p,"lightness") != 0))
1440 {
1441 char
1442 name[MaxTextExtent];
1443
1444 (void) CopyMagickString(name,p,MaxTextExtent);
1445 for (q=name+(strlen(name)-1); q > name; q--)
1446 {
1447 if (*q == ')')
1448 break;
1449 if (*q == '.')
1450 {
1451 *q='\0';
1452 break;
1453 }
1454 }
1455 if ((strlen(name) > 2) &&
1456 (GetValueFromSplayTree(fx_info->symbols,name) == (const char *) NULL))
1457 {
cristy4c08aed2011-07-01 19:47:50 +00001458 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001459 *color;
1460
cristy4c08aed2011-07-01 19:47:50 +00001461 color=(PixelInfo *) GetValueFromSplayTree(fx_info->colors,name);
1462 if (color != (PixelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001463 {
1464 pixel=(*color);
1465 p+=strlen(name);
1466 }
1467 else
cristy1707c6c2012-01-18 23:30:54 +00001468 {
1469 MagickBooleanType
1470 status;
1471
1472 status=QueryColorCompliance(name,AllCompliance,&pixel,
1473 fx_info->exception);
1474 if (status != MagickFalse)
1475 {
1476 (void) AddValueToSplayTree(fx_info->colors,ConstantString(
1477 name),ClonePixelInfo(&pixel));
1478 p+=strlen(name);
1479 }
1480 }
cristy3ed852e2009-09-05 21:47:34 +00001481 }
1482 }
1483 (void) CopyMagickString(symbol,p,MaxTextExtent);
1484 StripString(symbol);
1485 if (*symbol == '\0')
1486 {
1487 switch (channel)
1488 {
cristy0568ffc2011-07-25 16:54:14 +00001489 case RedPixelChannel: return(QuantumScale*pixel.red);
1490 case GreenPixelChannel: return(QuantumScale*pixel.green);
1491 case BluePixelChannel: return(QuantumScale*pixel.blue);
1492 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001493 {
1494 if (image->colorspace != CMYKColorspace)
1495 {
1496 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00001497 ImageError,"ColorSeparatedImageRequired","'%s'",
cristy3ed852e2009-09-05 21:47:34 +00001498 image->filename);
1499 return(0.0);
1500 }
cristy4c08aed2011-07-01 19:47:50 +00001501 return(QuantumScale*pixel.black);
1502 }
cristy0568ffc2011-07-25 16:54:14 +00001503 case AlphaPixelChannel:
cristy4c08aed2011-07-01 19:47:50 +00001504 {
cristya19f1d72012-08-07 18:24:38 +00001505 double
cristy4c08aed2011-07-01 19:47:50 +00001506 alpha;
1507
cristy8a46d822012-08-28 23:32:39 +00001508 if (pixel.alpha_trait != BlendPixelTrait)
cristy4c08aed2011-07-01 19:47:50 +00001509 return(1.0);
cristya19f1d72012-08-07 18:24:38 +00001510 alpha=(double) (QuantumScale*pixel.alpha);
cristy4c08aed2011-07-01 19:47:50 +00001511 return(alpha);
cristy3ed852e2009-09-05 21:47:34 +00001512 }
cristya382aca2011-12-06 18:22:48 +00001513 case IndexPixelChannel:
1514 return(0.0);
cristyb3a73b52011-07-26 01:34:43 +00001515 case IntensityPixelChannel:
cristyf364ed42010-12-15 01:54:43 +00001516 {
cristy4c08aed2011-07-01 19:47:50 +00001517 return(QuantumScale*GetPixelInfoIntensity(&pixel));
cristyf364ed42010-12-15 01:54:43 +00001518 }
cristy3ed852e2009-09-05 21:47:34 +00001519 default:
1520 break;
1521 }
1522 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
anthonye5b39652012-04-21 05:37:29 +00001523 "UnableToParseExpression","'%s'",p);
cristy3ed852e2009-09-05 21:47:34 +00001524 return(0.0);
1525 }
1526 switch (*symbol)
1527 {
1528 case 'A':
1529 case 'a':
1530 {
1531 if (LocaleCompare(symbol,"a") == 0)
cristya19f1d72012-08-07 18:24:38 +00001532 return((double) (QuantumScale*pixel.alpha));
cristy3ed852e2009-09-05 21:47:34 +00001533 break;
1534 }
1535 case 'B':
1536 case 'b':
1537 {
1538 if (LocaleCompare(symbol,"b") == 0)
1539 return(QuantumScale*pixel.blue);
1540 break;
1541 }
1542 case 'C':
1543 case 'c':
1544 {
1545 if (LocaleNCompare(symbol,"channel",7) == 0)
1546 {
1547 GeometryInfo
1548 channel_info;
1549
1550 MagickStatusType
1551 flags;
1552
1553 flags=ParseGeometry(symbol+7,&channel_info);
1554 if (image->colorspace == CMYKColorspace)
1555 switch (channel)
1556 {
cristy0568ffc2011-07-25 16:54:14 +00001557 case CyanPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001558 {
1559 if ((flags & RhoValue) == 0)
1560 return(0.0);
1561 return(channel_info.rho);
1562 }
cristy0568ffc2011-07-25 16:54:14 +00001563 case MagentaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001564 {
1565 if ((flags & SigmaValue) == 0)
1566 return(0.0);
1567 return(channel_info.sigma);
1568 }
cristy0568ffc2011-07-25 16:54:14 +00001569 case YellowPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001570 {
1571 if ((flags & XiValue) == 0)
1572 return(0.0);
1573 return(channel_info.xi);
1574 }
cristy0568ffc2011-07-25 16:54:14 +00001575 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001576 {
1577 if ((flags & PsiValue) == 0)
1578 return(0.0);
1579 return(channel_info.psi);
1580 }
cristy0568ffc2011-07-25 16:54:14 +00001581 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001582 {
1583 if ((flags & ChiValue) == 0)
1584 return(0.0);
1585 return(channel_info.chi);
1586 }
1587 default:
1588 return(0.0);
1589 }
1590 switch (channel)
1591 {
cristy0568ffc2011-07-25 16:54:14 +00001592 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001593 {
1594 if ((flags & RhoValue) == 0)
1595 return(0.0);
1596 return(channel_info.rho);
1597 }
cristy0568ffc2011-07-25 16:54:14 +00001598 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001599 {
1600 if ((flags & SigmaValue) == 0)
1601 return(0.0);
1602 return(channel_info.sigma);
1603 }
cristy0568ffc2011-07-25 16:54:14 +00001604 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001605 {
1606 if ((flags & XiValue) == 0)
1607 return(0.0);
1608 return(channel_info.xi);
1609 }
cristy0568ffc2011-07-25 16:54:14 +00001610 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001611 {
1612 if ((flags & ChiValue) == 0)
1613 return(0.0);
1614 return(channel_info.chi);
1615 }
cristy0568ffc2011-07-25 16:54:14 +00001616 case AlphaPixelChannel:
cristy4c08aed2011-07-01 19:47:50 +00001617 {
1618 if ((flags & PsiValue) == 0)
1619 return(0.0);
1620 return(channel_info.psi);
1621 }
cristy3ed852e2009-09-05 21:47:34 +00001622 default:
1623 return(0.0);
1624 }
1625 return(0.0);
1626 }
1627 if (LocaleCompare(symbol,"c") == 0)
1628 return(QuantumScale*pixel.red);
1629 break;
1630 }
1631 case 'D':
1632 case 'd':
1633 {
1634 if (LocaleNCompare(symbol,"depth",5) == 0)
1635 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1636 break;
1637 }
1638 case 'G':
1639 case 'g':
1640 {
1641 if (LocaleCompare(symbol,"g") == 0)
1642 return(QuantumScale*pixel.green);
1643 break;
1644 }
1645 case 'K':
1646 case 'k':
1647 {
1648 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1649 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1650 if (LocaleCompare(symbol,"k") == 0)
1651 {
1652 if (image->colorspace != CMYKColorspace)
1653 {
1654 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00001655 OptionError,"ColorSeparatedImageRequired","'%s'",
cristy3ed852e2009-09-05 21:47:34 +00001656 image->filename);
1657 return(0.0);
1658 }
cristy4c08aed2011-07-01 19:47:50 +00001659 return(QuantumScale*pixel.black);
cristy3ed852e2009-09-05 21:47:34 +00001660 }
1661 break;
1662 }
1663 case 'H':
1664 case 'h':
1665 {
1666 if (LocaleCompare(symbol,"h") == 0)
cristya19f1d72012-08-07 18:24:38 +00001667 return((double) image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001668 if (LocaleCompare(symbol,"hue") == 0)
1669 {
1670 double
1671 hue,
1672 lightness,
1673 saturation;
1674
cristy0a39a5c2012-06-27 12:51:45 +00001675 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
cristyda1f9c12011-10-02 21:39:49 +00001676 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001677 return(hue);
1678 }
1679 break;
1680 }
1681 case 'I':
1682 case 'i':
1683 {
1684 if ((LocaleCompare(symbol,"image.depth") == 0) ||
1685 (LocaleCompare(symbol,"image.minima") == 0) ||
1686 (LocaleCompare(symbol,"image.maxima") == 0) ||
1687 (LocaleCompare(symbol,"image.mean") == 0) ||
1688 (LocaleCompare(symbol,"image.kurtosis") == 0) ||
1689 (LocaleCompare(symbol,"image.skewness") == 0) ||
1690 (LocaleCompare(symbol,"image.standard_deviation") == 0))
1691 return(FxChannelStatistics(fx_info,image,channel,symbol+6,exception));
1692 if (LocaleCompare(symbol,"image.resolution.x") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001693 return(image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00001694 if (LocaleCompare(symbol,"image.resolution.y") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001695 return(image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001696 if (LocaleCompare(symbol,"intensity") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001697 return(QuantumScale*GetPixelInfoIntensity(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001698 if (LocaleCompare(symbol,"i") == 0)
cristya19f1d72012-08-07 18:24:38 +00001699 return((double) x);
cristy3ed852e2009-09-05 21:47:34 +00001700 break;
1701 }
1702 case 'J':
1703 case 'j':
1704 {
1705 if (LocaleCompare(symbol,"j") == 0)
cristya19f1d72012-08-07 18:24:38 +00001706 return((double) y);
cristy3ed852e2009-09-05 21:47:34 +00001707 break;
1708 }
1709 case 'L':
1710 case 'l':
1711 {
1712 if (LocaleCompare(symbol,"lightness") == 0)
1713 {
1714 double
1715 hue,
1716 lightness,
1717 saturation;
1718
cristy0a39a5c2012-06-27 12:51:45 +00001719 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
cristyda1f9c12011-10-02 21:39:49 +00001720 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001721 return(lightness);
1722 }
1723 if (LocaleCompare(symbol,"luminance") == 0)
1724 {
1725 double
1726 luminence;
1727
cristy94d535d2012-08-17 17:39:40 +00001728 luminence=0.21267*pixel.red+0.71516*pixel.green+0.07217*pixel.blue;
cristy3ed852e2009-09-05 21:47:34 +00001729 return(QuantumScale*luminence);
1730 }
1731 break;
1732 }
1733 case 'M':
1734 case 'm':
1735 {
1736 if (LocaleNCompare(symbol,"maxima",6) == 0)
1737 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1738 if (LocaleNCompare(symbol,"mean",4) == 0)
1739 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1740 if (LocaleNCompare(symbol,"minima",6) == 0)
1741 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1742 if (LocaleCompare(symbol,"m") == 0)
1743 return(QuantumScale*pixel.blue);
1744 break;
1745 }
1746 case 'N':
1747 case 'n':
1748 {
1749 if (LocaleCompare(symbol,"n") == 0)
cristya19f1d72012-08-07 18:24:38 +00001750 return((double) GetImageListLength(fx_info->images));
cristy3ed852e2009-09-05 21:47:34 +00001751 break;
1752 }
1753 case 'O':
1754 case 'o':
1755 {
1756 if (LocaleCompare(symbol,"o") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001757 return(QuantumScale*pixel.alpha);
cristy3ed852e2009-09-05 21:47:34 +00001758 break;
1759 }
1760 case 'P':
1761 case 'p':
1762 {
1763 if (LocaleCompare(symbol,"page.height") == 0)
cristya19f1d72012-08-07 18:24:38 +00001764 return((double) image->page.height);
cristy3ed852e2009-09-05 21:47:34 +00001765 if (LocaleCompare(symbol,"page.width") == 0)
cristya19f1d72012-08-07 18:24:38 +00001766 return((double) image->page.width);
cristy3ed852e2009-09-05 21:47:34 +00001767 if (LocaleCompare(symbol,"page.x") == 0)
cristya19f1d72012-08-07 18:24:38 +00001768 return((double) image->page.x);
cristy3ed852e2009-09-05 21:47:34 +00001769 if (LocaleCompare(symbol,"page.y") == 0)
cristya19f1d72012-08-07 18:24:38 +00001770 return((double) image->page.y);
cristy3ed852e2009-09-05 21:47:34 +00001771 break;
1772 }
1773 case 'R':
1774 case 'r':
1775 {
1776 if (LocaleCompare(symbol,"resolution.x") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001777 return(image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00001778 if (LocaleCompare(symbol,"resolution.y") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001779 return(image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001780 if (LocaleCompare(symbol,"r") == 0)
1781 return(QuantumScale*pixel.red);
1782 break;
1783 }
1784 case 'S':
1785 case 's':
1786 {
1787 if (LocaleCompare(symbol,"saturation") == 0)
1788 {
1789 double
1790 hue,
1791 lightness,
1792 saturation;
1793
cristy0a39a5c2012-06-27 12:51:45 +00001794 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
cristyda1f9c12011-10-02 21:39:49 +00001795 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001796 return(saturation);
1797 }
1798 if (LocaleNCompare(symbol,"skewness",8) == 0)
1799 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1800 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1801 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1802 break;
1803 }
1804 case 'T':
1805 case 't':
1806 {
1807 if (LocaleCompare(symbol,"t") == 0)
cristya19f1d72012-08-07 18:24:38 +00001808 return((double) GetImageIndexInList(fx_info->images));
cristy3ed852e2009-09-05 21:47:34 +00001809 break;
1810 }
1811 case 'W':
1812 case 'w':
1813 {
1814 if (LocaleCompare(symbol,"w") == 0)
cristya19f1d72012-08-07 18:24:38 +00001815 return((double) image->columns);
cristy3ed852e2009-09-05 21:47:34 +00001816 break;
1817 }
1818 case 'Y':
1819 case 'y':
1820 {
1821 if (LocaleCompare(symbol,"y") == 0)
1822 return(QuantumScale*pixel.green);
1823 break;
1824 }
1825 case 'Z':
1826 case 'z':
1827 {
1828 if (LocaleCompare(symbol,"z") == 0)
1829 {
cristya19f1d72012-08-07 18:24:38 +00001830 double
cristy3ed852e2009-09-05 21:47:34 +00001831 depth;
1832
cristya19f1d72012-08-07 18:24:38 +00001833 depth=(double) GetImageDepth(image,fx_info->exception);
cristy3ed852e2009-09-05 21:47:34 +00001834 return(depth);
1835 }
1836 break;
1837 }
1838 default:
1839 break;
1840 }
1841 value=(const char *) GetValueFromSplayTree(fx_info->symbols,symbol);
1842 if (value != (const char *) NULL)
cristya19f1d72012-08-07 18:24:38 +00001843 return((double) StringToDouble(value,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001844 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
anthonye5b39652012-04-21 05:37:29 +00001845 "UnableToParseExpression","'%s'",symbol);
cristy3ed852e2009-09-05 21:47:34 +00001846 return(0.0);
1847}
1848
1849static const char *FxOperatorPrecedence(const char *expression,
1850 ExceptionInfo *exception)
1851{
1852 typedef enum
1853 {
1854 UndefinedPrecedence,
1855 NullPrecedence,
1856 BitwiseComplementPrecedence,
1857 ExponentPrecedence,
cristy116af162010-08-13 01:25:47 +00001858 ExponentialNotationPrecedence,
cristy3ed852e2009-09-05 21:47:34 +00001859 MultiplyPrecedence,
1860 AdditionPrecedence,
1861 ShiftPrecedence,
1862 RelationalPrecedence,
1863 EquivalencyPrecedence,
1864 BitwiseAndPrecedence,
1865 BitwiseOrPrecedence,
1866 LogicalAndPrecedence,
1867 LogicalOrPrecedence,
1868 TernaryPrecedence,
1869 AssignmentPrecedence,
1870 CommaPrecedence,
1871 SeparatorPrecedence
1872 } FxPrecedence;
1873
1874 FxPrecedence
1875 precedence,
1876 target;
1877
1878 register const char
1879 *subexpression;
1880
1881 register int
1882 c;
1883
cristybb503372010-05-27 20:51:26 +00001884 size_t
cristy3ed852e2009-09-05 21:47:34 +00001885 level;
1886
1887 c=0;
1888 level=0;
1889 subexpression=(const char *) NULL;
1890 target=NullPrecedence;
1891 while (*expression != '\0')
1892 {
1893 precedence=UndefinedPrecedence;
1894 if ((isspace((int) ((char) *expression)) != 0) || (c == (int) '@'))
1895 {
1896 expression++;
1897 continue;
1898 }
cristy488fa882010-03-01 22:34:24 +00001899 switch (*expression)
1900 {
1901 case 'A':
1902 case 'a':
cristy3ed852e2009-09-05 21:47:34 +00001903 {
cristyb33454f2011-08-03 02:10:45 +00001904#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00001905 if (LocaleNCompare(expression,"acosh",5) == 0)
1906 {
1907 expression+=5;
1908 break;
1909 }
cristyb33454f2011-08-03 02:10:45 +00001910#endif
1911#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00001912 if (LocaleNCompare(expression,"asinh",5) == 0)
1913 {
1914 expression+=5;
1915 break;
1916 }
cristyb33454f2011-08-03 02:10:45 +00001917#endif
1918#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00001919 if (LocaleNCompare(expression,"atanh",5) == 0)
cristy488fa882010-03-01 22:34:24 +00001920 {
1921 expression+=5;
1922 break;
1923 }
cristyb33454f2011-08-03 02:10:45 +00001924#endif
anthony62838e52012-05-24 12:41:54 +00001925 if (LocaleNCompare(expression,"atan2",5) == 0)
1926 {
1927 expression+=5;
1928 break;
1929 }
cristy488fa882010-03-01 22:34:24 +00001930 break;
cristy3ed852e2009-09-05 21:47:34 +00001931 }
cristy62d455f2011-11-03 11:42:28 +00001932 case 'E':
1933 case 'e':
1934 {
1935 if ((LocaleNCompare(expression,"E+",2) == 0) ||
1936 (LocaleNCompare(expression,"E-",2) == 0))
1937 {
1938 expression+=2; /* scientific notation */
1939 break;
1940 }
1941 }
cristy488fa882010-03-01 22:34:24 +00001942 case 'J':
1943 case 'j':
1944 {
1945 if ((LocaleNCompare(expression,"j0",2) == 0) ||
1946 (LocaleNCompare(expression,"j1",2) == 0))
1947 {
1948 expression+=2;
1949 break;
1950 }
1951 break;
1952 }
cristy2def9322010-06-18 23:59:37 +00001953 case '#':
1954 {
1955 while (isxdigit((int) ((unsigned char) *(expression+1))) != 0)
1956 expression++;
1957 break;
1958 }
cristy488fa882010-03-01 22:34:24 +00001959 default:
1960 break;
1961 }
cristy3ed852e2009-09-05 21:47:34 +00001962 if ((c == (int) '{') || (c == (int) '['))
1963 level++;
1964 else
1965 if ((c == (int) '}') || (c == (int) ']'))
1966 level--;
1967 if (level == 0)
1968 switch ((unsigned char) *expression)
1969 {
1970 case '~':
1971 case '!':
1972 {
1973 precedence=BitwiseComplementPrecedence;
1974 break;
1975 }
1976 case '^':
cristy6621e252010-08-13 00:42:57 +00001977 case '@':
cristy3ed852e2009-09-05 21:47:34 +00001978 {
1979 precedence=ExponentPrecedence;
1980 break;
1981 }
1982 default:
1983 {
1984 if (((c != 0) && ((isdigit((int) ((char) c)) != 0) ||
1985 (strchr(")",c) != (char *) NULL))) &&
1986 (((islower((int) ((char) *expression)) != 0) ||
1987 (strchr("(",(int) *expression) != (char *) NULL)) ||
1988 ((isdigit((int) ((char) c)) == 0) &&
1989 (isdigit((int) ((char) *expression)) != 0))) &&
1990 (strchr("xy",(int) *expression) == (char *) NULL))
1991 precedence=MultiplyPrecedence;
1992 break;
1993 }
1994 case '*':
1995 case '/':
1996 case '%':
1997 {
1998 precedence=MultiplyPrecedence;
1999 break;
2000 }
2001 case '+':
2002 case '-':
2003 {
2004 if ((strchr("(+-/*%:&^|<>~,",c) == (char *) NULL) ||
2005 (isalpha(c) != 0))
2006 precedence=AdditionPrecedence;
2007 break;
2008 }
2009 case LeftShiftOperator:
2010 case RightShiftOperator:
2011 {
2012 precedence=ShiftPrecedence;
2013 break;
2014 }
2015 case '<':
2016 case LessThanEqualOperator:
2017 case GreaterThanEqualOperator:
2018 case '>':
2019 {
2020 precedence=RelationalPrecedence;
2021 break;
2022 }
2023 case EqualOperator:
2024 case NotEqualOperator:
2025 {
2026 precedence=EquivalencyPrecedence;
2027 break;
2028 }
2029 case '&':
2030 {
2031 precedence=BitwiseAndPrecedence;
2032 break;
2033 }
2034 case '|':
2035 {
2036 precedence=BitwiseOrPrecedence;
2037 break;
2038 }
2039 case LogicalAndOperator:
2040 {
2041 precedence=LogicalAndPrecedence;
2042 break;
2043 }
2044 case LogicalOrOperator:
2045 {
2046 precedence=LogicalOrPrecedence;
2047 break;
2048 }
cristy116af162010-08-13 01:25:47 +00002049 case ExponentialNotation:
2050 {
2051 precedence=ExponentialNotationPrecedence;
2052 break;
2053 }
cristy3ed852e2009-09-05 21:47:34 +00002054 case ':':
2055 case '?':
2056 {
2057 precedence=TernaryPrecedence;
2058 break;
2059 }
2060 case '=':
2061 {
2062 precedence=AssignmentPrecedence;
2063 break;
2064 }
2065 case ',':
2066 {
2067 precedence=CommaPrecedence;
2068 break;
2069 }
2070 case ';':
2071 {
2072 precedence=SeparatorPrecedence;
2073 break;
2074 }
2075 }
2076 if ((precedence == BitwiseComplementPrecedence) ||
2077 (precedence == TernaryPrecedence) ||
2078 (precedence == AssignmentPrecedence))
2079 {
2080 if (precedence > target)
2081 {
2082 /*
2083 Right-to-left associativity.
2084 */
2085 target=precedence;
2086 subexpression=expression;
2087 }
2088 }
2089 else
2090 if (precedence >= target)
2091 {
2092 /*
2093 Left-to-right associativity.
2094 */
2095 target=precedence;
2096 subexpression=expression;
2097 }
2098 if (strchr("(",(int) *expression) != (char *) NULL)
2099 expression=FxSubexpression(expression,exception);
2100 c=(int) (*expression++);
2101 }
2102 return(subexpression);
2103}
2104
cristya19f1d72012-08-07 18:24:38 +00002105static double FxEvaluateSubexpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002106 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristya19f1d72012-08-07 18:24:38 +00002107 const char *expression,double *beta,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002108{
2109 char
2110 *q,
2111 subexpression[MaxTextExtent];
2112
cristya19f1d72012-08-07 18:24:38 +00002113 double
cristy3ed852e2009-09-05 21:47:34 +00002114 alpha,
2115 gamma;
2116
2117 register const char
2118 *p;
2119
2120 *beta=0.0;
2121 if (exception->severity != UndefinedException)
2122 return(0.0);
2123 while (isspace((int) *expression) != 0)
2124 expression++;
2125 if (*expression == '\0')
2126 {
2127 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
anthonye5b39652012-04-21 05:37:29 +00002128 "MissingExpression","'%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00002129 return(0.0);
2130 }
cristy66322f02010-05-17 11:40:48 +00002131 *subexpression='\0';
cristy3ed852e2009-09-05 21:47:34 +00002132 p=FxOperatorPrecedence(expression,exception);
2133 if (p != (const char *) NULL)
2134 {
2135 (void) CopyMagickString(subexpression,expression,(size_t)
2136 (p-expression+1));
2137 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2138 exception);
2139 switch ((unsigned char) *p)
2140 {
2141 case '~':
2142 {
2143 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristya19f1d72012-08-07 18:24:38 +00002144 *beta=(double) (~(size_t) *beta);
cristy3ed852e2009-09-05 21:47:34 +00002145 return(*beta);
2146 }
2147 case '!':
2148 {
2149 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2150 return(*beta == 0.0 ? 1.0 : 0.0);
2151 }
2152 case '^':
2153 {
2154 *beta=pow((double) alpha,(double) FxEvaluateSubexpression(fx_info,
2155 channel,x,y,++p,beta,exception));
2156 return(*beta);
2157 }
2158 case '*':
cristy116af162010-08-13 01:25:47 +00002159 case ExponentialNotation:
cristy3ed852e2009-09-05 21:47:34 +00002160 {
2161 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2162 return(alpha*(*beta));
2163 }
2164 case '/':
2165 {
2166 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2167 if (*beta == 0.0)
2168 {
2169 if (exception->severity == UndefinedException)
2170 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00002171 OptionError,"DivideByZero","'%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00002172 return(0.0);
2173 }
2174 return(alpha/(*beta));
2175 }
2176 case '%':
2177 {
2178 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2179 *beta=fabs(floor(((double) *beta)+0.5));
2180 if (*beta == 0.0)
2181 {
2182 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00002183 OptionError,"DivideByZero","'%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00002184 return(0.0);
2185 }
2186 return(fmod((double) alpha,(double) *beta));
2187 }
2188 case '+':
2189 {
2190 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2191 return(alpha+(*beta));
2192 }
2193 case '-':
2194 {
2195 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2196 return(alpha-(*beta));
2197 }
2198 case LeftShiftOperator:
2199 {
2200 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristya19f1d72012-08-07 18:24:38 +00002201 *beta=(double) ((size_t) (alpha+0.5) << (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002202 return(*beta);
2203 }
2204 case RightShiftOperator:
2205 {
2206 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristya19f1d72012-08-07 18:24:38 +00002207 *beta=(double) ((size_t) (alpha+0.5) >> (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002208 return(*beta);
2209 }
2210 case '<':
2211 {
2212 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2213 return(alpha < *beta ? 1.0 : 0.0);
2214 }
2215 case LessThanEqualOperator:
2216 {
2217 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2218 return(alpha <= *beta ? 1.0 : 0.0);
2219 }
2220 case '>':
2221 {
2222 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2223 return(alpha > *beta ? 1.0 : 0.0);
2224 }
2225 case GreaterThanEqualOperator:
2226 {
2227 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2228 return(alpha >= *beta ? 1.0 : 0.0);
2229 }
2230 case EqualOperator:
2231 {
2232 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy9b528342012-06-02 00:59:20 +00002233 return(fabs(alpha-(*beta)) < MagickEpsilon ? MagickEpsilon : 0.0);
cristy3ed852e2009-09-05 21:47:34 +00002234 }
2235 case NotEqualOperator:
2236 {
2237 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy972050b2012-06-04 22:09:17 +00002238 return(fabs(alpha-(*beta)) >= MagickEpsilon ? 1.0 : 0.0);
cristy3ed852e2009-09-05 21:47:34 +00002239 }
2240 case '&':
2241 {
2242 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristya19f1d72012-08-07 18:24:38 +00002243 *beta=(double) ((size_t) (alpha+0.5) & (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002244 return(*beta);
2245 }
2246 case '|':
2247 {
2248 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristya19f1d72012-08-07 18:24:38 +00002249 *beta=(double) ((size_t) (alpha+0.5) | (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002250 return(*beta);
2251 }
2252 case LogicalAndOperator:
2253 {
2254 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2255 *beta=(alpha > 0.0) && (gamma > 0.0) ? 1.0 : 0.0;
2256 return(*beta);
2257 }
2258 case LogicalOrOperator:
2259 {
2260 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2261 *beta=(alpha > 0.0) || (gamma > 0.0) ? 1.0 : 0.0;
2262 return(*beta);
2263 }
2264 case '?':
2265 {
cristya19f1d72012-08-07 18:24:38 +00002266 double
cristy3ed852e2009-09-05 21:47:34 +00002267 gamma;
2268
2269 (void) CopyMagickString(subexpression,++p,MaxTextExtent);
2270 q=subexpression;
2271 p=StringToken(":",&q);
2272 if (q == (char *) NULL)
2273 {
2274 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00002275 OptionError,"UnableToParseExpression","'%s'",subexpression);
cristy3ed852e2009-09-05 21:47:34 +00002276 return(0.0);
2277 }
cristy972050b2012-06-04 22:09:17 +00002278 if (fabs((double) alpha) >= MagickEpsilon)
cristy3ed852e2009-09-05 21:47:34 +00002279 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,exception);
2280 else
2281 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,q,beta,exception);
2282 return(gamma);
2283 }
2284 case '=':
2285 {
2286 char
2287 numeric[MaxTextExtent];
2288
2289 q=subexpression;
2290 while (isalpha((int) ((unsigned char) *q)) != 0)
2291 q++;
2292 if (*q != '\0')
2293 {
2294 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00002295 OptionError,"UnableToParseExpression","'%s'",subexpression);
cristy3ed852e2009-09-05 21:47:34 +00002296 return(0.0);
2297 }
2298 ClearMagickException(exception);
2299 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristyb51dff52011-05-19 16:55:47 +00002300 (void) FormatLocaleString(numeric,MaxTextExtent,"%g",(double)
cristy8cd5b312010-01-07 01:10:24 +00002301 *beta);
cristy3ed852e2009-09-05 21:47:34 +00002302 (void) DeleteNodeFromSplayTree(fx_info->symbols,subexpression);
2303 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(
2304 subexpression),ConstantString(numeric));
2305 return(*beta);
2306 }
2307 case ',':
2308 {
2309 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2310 return(alpha);
2311 }
2312 case ';':
2313 {
2314 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2315 return(*beta);
2316 }
2317 default:
2318 {
2319 gamma=alpha*FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,
2320 exception);
2321 return(gamma);
2322 }
2323 }
2324 }
2325 if (strchr("(",(int) *expression) != (char *) NULL)
2326 {
2327 (void) CopyMagickString(subexpression,expression+1,MaxTextExtent);
2328 subexpression[strlen(subexpression)-1]='\0';
2329 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2330 exception);
2331 return(gamma);
2332 }
cristy8b8a3ae2010-10-23 18:49:46 +00002333 switch (*expression)
cristy3ed852e2009-09-05 21:47:34 +00002334 {
2335 case '+':
2336 {
2337 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2338 exception);
2339 return(1.0*gamma);
2340 }
2341 case '-':
2342 {
2343 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2344 exception);
2345 return(-1.0*gamma);
2346 }
2347 case '~':
2348 {
2349 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2350 exception);
cristya19f1d72012-08-07 18:24:38 +00002351 return((double) (~(size_t) (gamma+0.5)));
cristy3ed852e2009-09-05 21:47:34 +00002352 }
2353 case 'A':
2354 case 'a':
2355 {
2356 if (LocaleNCompare(expression,"abs",3) == 0)
2357 {
2358 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2359 exception);
cristya19f1d72012-08-07 18:24:38 +00002360 return((double) fabs((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002361 }
cristyb33454f2011-08-03 02:10:45 +00002362#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00002363 if (LocaleNCompare(expression,"acosh",5) == 0)
2364 {
2365 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2366 exception);
cristya19f1d72012-08-07 18:24:38 +00002367 return((double) acosh((double) alpha));
cristy363772a2011-07-28 23:25:33 +00002368 }
cristyb33454f2011-08-03 02:10:45 +00002369#endif
cristy3ed852e2009-09-05 21:47:34 +00002370 if (LocaleNCompare(expression,"acos",4) == 0)
2371 {
2372 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2373 exception);
cristya19f1d72012-08-07 18:24:38 +00002374 return((double) acos((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002375 }
cristy43c22f42010-03-30 12:34:07 +00002376#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002377 if (LocaleNCompare(expression,"airy",4) == 0)
2378 {
2379 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2380 exception);
2381 if (alpha == 0.0)
cristy2dd03222010-03-30 22:12:11 +00002382 return(1.0);
2383 gamma=2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha);
cristy43c22f42010-03-30 12:34:07 +00002384 return(gamma*gamma);
cristyee56cf12010-03-01 22:17:06 +00002385 }
cristy43c22f42010-03-30 12:34:07 +00002386#endif
cristyb33454f2011-08-03 02:10:45 +00002387#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00002388 if (LocaleNCompare(expression,"asinh",5) == 0)
2389 {
2390 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2391 exception);
cristya19f1d72012-08-07 18:24:38 +00002392 return((double) asinh((double) alpha));
cristy363772a2011-07-28 23:25:33 +00002393 }
cristyb33454f2011-08-03 02:10:45 +00002394#endif
cristy3ed852e2009-09-05 21:47:34 +00002395 if (LocaleNCompare(expression,"asin",4) == 0)
2396 {
2397 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2398 exception);
cristya19f1d72012-08-07 18:24:38 +00002399 return((double) asin((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002400 }
2401 if (LocaleNCompare(expression,"alt",3) == 0)
2402 {
2403 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2404 exception);
cristybb503372010-05-27 20:51:26 +00002405 return(((ssize_t) alpha) & 0x01 ? -1.0 : 1.0);
cristy3ed852e2009-09-05 21:47:34 +00002406 }
2407 if (LocaleNCompare(expression,"atan2",5) == 0)
2408 {
2409 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2410 exception);
cristya19f1d72012-08-07 18:24:38 +00002411 return((double) atan2((double) alpha,(double) *beta));
cristy3ed852e2009-09-05 21:47:34 +00002412 }
cristyb33454f2011-08-03 02:10:45 +00002413#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00002414 if (LocaleNCompare(expression,"atanh",5) == 0)
2415 {
2416 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2417 exception);
cristya19f1d72012-08-07 18:24:38 +00002418 return((double) atanh((double) alpha));
cristy363772a2011-07-28 23:25:33 +00002419 }
cristyb33454f2011-08-03 02:10:45 +00002420#endif
cristy3ed852e2009-09-05 21:47:34 +00002421 if (LocaleNCompare(expression,"atan",4) == 0)
2422 {
2423 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2424 exception);
cristya19f1d72012-08-07 18:24:38 +00002425 return((double) atan((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002426 }
2427 if (LocaleCompare(expression,"a") == 0)
2428 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2429 break;
2430 }
2431 case 'B':
2432 case 'b':
2433 {
2434 if (LocaleCompare(expression,"b") == 0)
2435 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2436 break;
2437 }
2438 case 'C':
2439 case 'c':
2440 {
2441 if (LocaleNCompare(expression,"ceil",4) == 0)
2442 {
2443 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2444 exception);
cristya19f1d72012-08-07 18:24:38 +00002445 return((double) ceil((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002446 }
2447 if (LocaleNCompare(expression,"cosh",4) == 0)
2448 {
2449 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2450 exception);
cristya19f1d72012-08-07 18:24:38 +00002451 return((double) cosh((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002452 }
2453 if (LocaleNCompare(expression,"cos",3) == 0)
2454 {
2455 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2456 exception);
cristya19f1d72012-08-07 18:24:38 +00002457 return((double) cos((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002458 }
2459 if (LocaleCompare(expression,"c") == 0)
2460 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2461 break;
2462 }
2463 case 'D':
2464 case 'd':
2465 {
2466 if (LocaleNCompare(expression,"debug",5) == 0)
2467 {
2468 const char
2469 *type;
2470
2471 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2472 exception);
2473 if (fx_info->images->colorspace == CMYKColorspace)
2474 switch (channel)
2475 {
cristy0568ffc2011-07-25 16:54:14 +00002476 case CyanPixelChannel: type="cyan"; break;
2477 case MagentaPixelChannel: type="magenta"; break;
2478 case YellowPixelChannel: type="yellow"; break;
2479 case AlphaPixelChannel: type="opacity"; break;
2480 case BlackPixelChannel: type="black"; break;
cristy3ed852e2009-09-05 21:47:34 +00002481 default: type="unknown"; break;
2482 }
2483 else
2484 switch (channel)
2485 {
cristy0568ffc2011-07-25 16:54:14 +00002486 case RedPixelChannel: type="red"; break;
2487 case GreenPixelChannel: type="green"; break;
2488 case BluePixelChannel: type="blue"; break;
2489 case AlphaPixelChannel: type="opacity"; break;
cristy3ed852e2009-09-05 21:47:34 +00002490 default: type="unknown"; break;
2491 }
2492 (void) CopyMagickString(subexpression,expression+6,MaxTextExtent);
2493 if (strlen(subexpression) > 1)
2494 subexpression[strlen(subexpression)-1]='\0';
2495 if (fx_info->file != (FILE *) NULL)
cristy1707c6c2012-01-18 23:30:54 +00002496 (void) FormatLocaleFile(fx_info->file,"%s[%.20g,%.20g].%s: "
2497 "%s=%.*g\n",fx_info->images->filename,(double) x,(double) y,type,
2498 subexpression,GetMagickPrecision(),(double) alpha);
cristy3ed852e2009-09-05 21:47:34 +00002499 return(0.0);
2500 }
cristy5597a8d2011-11-04 00:25:32 +00002501 if (LocaleNCompare(expression,"drc",3) == 0)
2502 {
2503 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2504 exception);
cristya19f1d72012-08-07 18:24:38 +00002505 return((double) (alpha/(*beta*(alpha-1.0)+1.0)));
cristy5597a8d2011-11-04 00:25:32 +00002506 }
cristy3ed852e2009-09-05 21:47:34 +00002507 break;
2508 }
2509 case 'E':
2510 case 'e':
2511 {
2512 if (LocaleCompare(expression,"epsilon") == 0)
cristya19f1d72012-08-07 18:24:38 +00002513 return((double) MagickEpsilon);
cristy3ed852e2009-09-05 21:47:34 +00002514 if (LocaleNCompare(expression,"exp",3) == 0)
2515 {
2516 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2517 exception);
cristya19f1d72012-08-07 18:24:38 +00002518 return((double) exp((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002519 }
2520 if (LocaleCompare(expression,"e") == 0)
cristya19f1d72012-08-07 18:24:38 +00002521 return((double) 2.7182818284590452354);
cristy3ed852e2009-09-05 21:47:34 +00002522 break;
2523 }
2524 case 'F':
2525 case 'f':
2526 {
2527 if (LocaleNCompare(expression,"floor",5) == 0)
2528 {
2529 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2530 exception);
cristya19f1d72012-08-07 18:24:38 +00002531 return((double) floor((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002532 }
2533 break;
2534 }
2535 case 'G':
2536 case 'g':
2537 {
cristy9eeedea2011-11-02 19:04:05 +00002538 if (LocaleNCompare(expression,"gauss",5) == 0)
2539 {
2540 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2541 exception);
2542 gamma=exp((double) (-alpha*alpha/2.0))/sqrt(2.0*MagickPI);
cristya19f1d72012-08-07 18:24:38 +00002543 return((double) gamma);
cristy9eeedea2011-11-02 19:04:05 +00002544 }
cristyb0aad4c2011-11-02 19:30:35 +00002545 if (LocaleNCompare(expression,"gcd",3) == 0)
2546 {
2547 MagickOffsetType
2548 gcd;
2549
2550 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2551 exception);
cristy1707c6c2012-01-18 23:30:54 +00002552 gcd=FxGCD((MagickOffsetType) (alpha+0.5),(MagickOffsetType) (*beta+
2553 0.5));
cristya19f1d72012-08-07 18:24:38 +00002554 return((double) gcd);
cristyb0aad4c2011-11-02 19:30:35 +00002555 }
cristy3ed852e2009-09-05 21:47:34 +00002556 if (LocaleCompare(expression,"g") == 0)
2557 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2558 break;
2559 }
2560 case 'H':
2561 case 'h':
2562 {
2563 if (LocaleCompare(expression,"h") == 0)
2564 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2565 if (LocaleCompare(expression,"hue") == 0)
2566 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2567 if (LocaleNCompare(expression,"hypot",5) == 0)
2568 {
2569 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2570 exception);
cristya19f1d72012-08-07 18:24:38 +00002571 return((double) hypot((double) alpha,(double) *beta));
cristy3ed852e2009-09-05 21:47:34 +00002572 }
2573 break;
2574 }
2575 case 'K':
2576 case 'k':
2577 {
2578 if (LocaleCompare(expression,"k") == 0)
2579 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2580 break;
2581 }
2582 case 'I':
2583 case 'i':
2584 {
2585 if (LocaleCompare(expression,"intensity") == 0)
2586 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2587 if (LocaleNCompare(expression,"int",3) == 0)
2588 {
2589 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2590 exception);
cristya19f1d72012-08-07 18:24:38 +00002591 return((double) floor(alpha));
cristy3ed852e2009-09-05 21:47:34 +00002592 }
cristy82b20722011-11-05 21:52:36 +00002593#if defined(MAGICKCORE_HAVE_ISNAN)
cristy639399c2011-11-02 19:16:15 +00002594 if (LocaleNCompare(expression,"isnan",5) == 0)
2595 {
2596 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2597 exception);
cristya19f1d72012-08-07 18:24:38 +00002598 return((double) !!isnan((double) alpha));
cristy639399c2011-11-02 19:16:15 +00002599 }
cristy82b20722011-11-05 21:52:36 +00002600#endif
cristy3ed852e2009-09-05 21:47:34 +00002601 if (LocaleCompare(expression,"i") == 0)
2602 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2603 break;
2604 }
2605 case 'J':
2606 case 'j':
2607 {
2608 if (LocaleCompare(expression,"j") == 0)
2609 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
cristy161b9262010-03-20 19:34:32 +00002610#if defined(MAGICKCORE_HAVE_J0)
cristyee56cf12010-03-01 22:17:06 +00002611 if (LocaleNCompare(expression,"j0",2) == 0)
2612 {
2613 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2614 exception);
cristya19f1d72012-08-07 18:24:38 +00002615 return((double) j0((double) alpha));
cristyee56cf12010-03-01 22:17:06 +00002616 }
cristy161b9262010-03-20 19:34:32 +00002617#endif
2618#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002619 if (LocaleNCompare(expression,"j1",2) == 0)
2620 {
2621 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2622 exception);
cristya19f1d72012-08-07 18:24:38 +00002623 return((double) j1((double) alpha));
cristyee56cf12010-03-01 22:17:06 +00002624 }
cristy161b9262010-03-20 19:34:32 +00002625#endif
cristyaa018fa2010-04-08 23:03:54 +00002626#if defined(MAGICKCORE_HAVE_J1)
cristya6a09e72010-03-02 14:51:02 +00002627 if (LocaleNCompare(expression,"jinc",4) == 0)
2628 {
2629 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2630 exception);
cristy0946a822010-03-12 17:14:58 +00002631 if (alpha == 0.0)
2632 return(1.0);
cristya19f1d72012-08-07 18:24:38 +00002633 gamma=(double) (2.0*j1((double) (MagickPI*alpha))/(MagickPI*
cristy1707c6c2012-01-18 23:30:54 +00002634 alpha));
cristyfce2f7b2010-03-12 00:29:49 +00002635 return(gamma);
cristya6a09e72010-03-02 14:51:02 +00002636 }
cristyaa018fa2010-04-08 23:03:54 +00002637#endif
cristy3ed852e2009-09-05 21:47:34 +00002638 break;
2639 }
2640 case 'L':
2641 case 'l':
2642 {
2643 if (LocaleNCompare(expression,"ln",2) == 0)
2644 {
2645 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2646 exception);
cristya19f1d72012-08-07 18:24:38 +00002647 return((double) log((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002648 }
cristyc8ed5322010-08-31 12:07:59 +00002649 if (LocaleNCompare(expression,"logtwo",6) == 0)
cristy3ed852e2009-09-05 21:47:34 +00002650 {
cristyc8ed5322010-08-31 12:07:59 +00002651 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
cristy3ed852e2009-09-05 21:47:34 +00002652 exception);
cristya19f1d72012-08-07 18:24:38 +00002653 return((double) log10((double) alpha))/log10(2.0);
cristy3ed852e2009-09-05 21:47:34 +00002654 }
2655 if (LocaleNCompare(expression,"log",3) == 0)
2656 {
2657 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2658 exception);
cristya19f1d72012-08-07 18:24:38 +00002659 return((double) log10((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002660 }
2661 if (LocaleCompare(expression,"lightness") == 0)
2662 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2663 break;
2664 }
2665 case 'M':
2666 case 'm':
2667 {
2668 if (LocaleCompare(expression,"MaxRGB") == 0)
cristya19f1d72012-08-07 18:24:38 +00002669 return((double) QuantumRange);
cristy3ed852e2009-09-05 21:47:34 +00002670 if (LocaleNCompare(expression,"maxima",6) == 0)
2671 break;
2672 if (LocaleNCompare(expression,"max",3) == 0)
cristy984049c2011-11-03 18:34:58 +00002673 {
2674 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2675 exception);
2676 return(alpha > *beta ? alpha : *beta);
2677 }
cristy3ed852e2009-09-05 21:47:34 +00002678 if (LocaleNCompare(expression,"minima",6) == 0)
2679 break;
2680 if (LocaleNCompare(expression,"min",3) == 0)
cristy984049c2011-11-03 18:34:58 +00002681 {
2682 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2683 exception);
2684 return(alpha < *beta ? alpha : *beta);
2685 }
cristy3ed852e2009-09-05 21:47:34 +00002686 if (LocaleNCompare(expression,"mod",3) == 0)
2687 {
2688 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2689 exception);
cristy984049c2011-11-03 18:34:58 +00002690 gamma=alpha-floor((double) (alpha/(*beta)))*(*beta);
2691 return(gamma);
cristy3ed852e2009-09-05 21:47:34 +00002692 }
2693 if (LocaleCompare(expression,"m") == 0)
2694 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2695 break;
2696 }
2697 case 'N':
2698 case 'n':
2699 {
cristyad3502e2011-11-02 19:10:45 +00002700 if (LocaleNCompare(expression,"not",3) == 0)
2701 {
2702 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2703 exception);
cristya19f1d72012-08-07 18:24:38 +00002704 return((double) (alpha < MagickEpsilon));
cristyad3502e2011-11-02 19:10:45 +00002705 }
cristy3ed852e2009-09-05 21:47:34 +00002706 if (LocaleCompare(expression,"n") == 0)
2707 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2708 break;
2709 }
2710 case 'O':
2711 case 'o':
2712 {
2713 if (LocaleCompare(expression,"Opaque") == 0)
2714 return(1.0);
2715 if (LocaleCompare(expression,"o") == 0)
2716 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2717 break;
2718 }
2719 case 'P':
2720 case 'p':
2721 {
cristy670aa3c2011-11-03 00:54:00 +00002722 if (LocaleCompare(expression,"phi") == 0)
cristya19f1d72012-08-07 18:24:38 +00002723 return((double) MagickPHI);
cristy3ed852e2009-09-05 21:47:34 +00002724 if (LocaleCompare(expression,"pi") == 0)
cristya19f1d72012-08-07 18:24:38 +00002725 return((double) MagickPI);
cristy3ed852e2009-09-05 21:47:34 +00002726 if (LocaleNCompare(expression,"pow",3) == 0)
2727 {
2728 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2729 exception);
cristya19f1d72012-08-07 18:24:38 +00002730 return((double) pow((double) alpha,(double) *beta));
cristy3ed852e2009-09-05 21:47:34 +00002731 }
2732 if (LocaleCompare(expression,"p") == 0)
2733 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2734 break;
2735 }
2736 case 'Q':
2737 case 'q':
2738 {
2739 if (LocaleCompare(expression,"QuantumRange") == 0)
cristya19f1d72012-08-07 18:24:38 +00002740 return((double) QuantumRange);
cristy3ed852e2009-09-05 21:47:34 +00002741 if (LocaleCompare(expression,"QuantumScale") == 0)
cristya19f1d72012-08-07 18:24:38 +00002742 return((double) QuantumScale);
cristy3ed852e2009-09-05 21:47:34 +00002743 break;
2744 }
2745 case 'R':
2746 case 'r':
2747 {
2748 if (LocaleNCompare(expression,"rand",4) == 0)
cristya19f1d72012-08-07 18:24:38 +00002749 return((double) GetPseudoRandomValue(fx_info->random_info));
cristy3ed852e2009-09-05 21:47:34 +00002750 if (LocaleNCompare(expression,"round",5) == 0)
2751 {
2752 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2753 exception);
cristya19f1d72012-08-07 18:24:38 +00002754 return((double) floor((double) alpha+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002755 }
2756 if (LocaleCompare(expression,"r") == 0)
2757 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2758 break;
2759 }
2760 case 'S':
2761 case 's':
2762 {
2763 if (LocaleCompare(expression,"saturation") == 0)
2764 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2765 if (LocaleNCompare(expression,"sign",4) == 0)
2766 {
2767 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2768 exception);
2769 return(alpha < 0.0 ? -1.0 : 1.0);
2770 }
cristya6a09e72010-03-02 14:51:02 +00002771 if (LocaleNCompare(expression,"sinc",4) == 0)
2772 {
2773 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2774 exception);
2775 if (alpha == 0)
2776 return(1.0);
cristya19f1d72012-08-07 18:24:38 +00002777 gamma=(double) (sin((double) (MagickPI*alpha))/
cristya6a09e72010-03-02 14:51:02 +00002778 (MagickPI*alpha));
2779 return(gamma);
2780 }
cristy3ed852e2009-09-05 21:47:34 +00002781 if (LocaleNCompare(expression,"sinh",4) == 0)
2782 {
2783 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2784 exception);
cristya19f1d72012-08-07 18:24:38 +00002785 return((double) sinh((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002786 }
2787 if (LocaleNCompare(expression,"sin",3) == 0)
2788 {
2789 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2790 exception);
cristya19f1d72012-08-07 18:24:38 +00002791 return((double) sin((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002792 }
2793 if (LocaleNCompare(expression,"sqrt",4) == 0)
2794 {
2795 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2796 exception);
cristya19f1d72012-08-07 18:24:38 +00002797 return((double) sqrt((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002798 }
cristy9eeedea2011-11-02 19:04:05 +00002799 if (LocaleNCompare(expression,"squish",6) == 0)
2800 {
2801 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2802 exception);
cristya19f1d72012-08-07 18:24:38 +00002803 return((double) (1.0/(1.0+exp((double) (-alpha)))));
cristy9eeedea2011-11-02 19:04:05 +00002804 }
cristy3ed852e2009-09-05 21:47:34 +00002805 if (LocaleCompare(expression,"s") == 0)
2806 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2807 break;
2808 }
2809 case 'T':
2810 case 't':
2811 {
2812 if (LocaleNCompare(expression,"tanh",4) == 0)
2813 {
2814 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2815 exception);
cristya19f1d72012-08-07 18:24:38 +00002816 return((double) tanh((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002817 }
2818 if (LocaleNCompare(expression,"tan",3) == 0)
2819 {
2820 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2821 exception);
cristya19f1d72012-08-07 18:24:38 +00002822 return((double) tan((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002823 }
2824 if (LocaleCompare(expression,"Transparent") == 0)
2825 return(0.0);
cristy16788e42010-08-13 13:44:26 +00002826 if (LocaleNCompare(expression,"trunc",5) == 0)
2827 {
2828 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2829 exception);
2830 if (alpha >= 0.0)
cristya19f1d72012-08-07 18:24:38 +00002831 return((double) floor((double) alpha));
2832 return((double) ceil((double) alpha));
cristy16788e42010-08-13 13:44:26 +00002833 }
cristy3ed852e2009-09-05 21:47:34 +00002834 if (LocaleCompare(expression,"t") == 0)
2835 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2836 break;
2837 }
2838 case 'U':
2839 case 'u':
2840 {
2841 if (LocaleCompare(expression,"u") == 0)
2842 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2843 break;
2844 }
2845 case 'V':
2846 case 'v':
2847 {
2848 if (LocaleCompare(expression,"v") == 0)
2849 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2850 break;
2851 }
2852 case 'W':
2853 case 'w':
2854 {
cristy9eeedea2011-11-02 19:04:05 +00002855 if (LocaleNCompare(expression,"while",5) == 0)
2856 {
2857 do
2858 {
2859 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2860 exception);
2861 } while (fabs((double) alpha) >= MagickEpsilon);
cristya19f1d72012-08-07 18:24:38 +00002862 return((double) *beta);
cristy9eeedea2011-11-02 19:04:05 +00002863 }
cristy3ed852e2009-09-05 21:47:34 +00002864 if (LocaleCompare(expression,"w") == 0)
2865 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2866 break;
2867 }
2868 case 'Y':
2869 case 'y':
2870 {
2871 if (LocaleCompare(expression,"y") == 0)
2872 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2873 break;
2874 }
2875 case 'Z':
2876 case 'z':
2877 {
2878 if (LocaleCompare(expression,"z") == 0)
2879 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2880 break;
2881 }
2882 default:
2883 break;
2884 }
2885 q=(char *) expression;
cristydbdd0e32011-11-04 23:29:40 +00002886 alpha=InterpretSiPrefixValue(expression,&q);
cristy3ed852e2009-09-05 21:47:34 +00002887 if (q == expression)
2888 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2889 return(alpha);
2890}
2891
cristy7832dc22011-09-05 01:21:53 +00002892MagickPrivate MagickBooleanType FxEvaluateExpression(FxInfo *fx_info,
cristya19f1d72012-08-07 18:24:38 +00002893 double *alpha,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002894{
2895 MagickBooleanType
2896 status;
2897
cristy541ae572011-08-05 19:08:59 +00002898 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2899 exception);
cristy3ed852e2009-09-05 21:47:34 +00002900 return(status);
2901}
2902
2903MagickExport MagickBooleanType FxPreprocessExpression(FxInfo *fx_info,
cristya19f1d72012-08-07 18:24:38 +00002904 double *alpha,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002905{
2906 FILE
2907 *file;
2908
2909 MagickBooleanType
2910 status;
2911
2912 file=fx_info->file;
2913 fx_info->file=(FILE *) NULL;
cristy541ae572011-08-05 19:08:59 +00002914 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2915 exception);
cristy3ed852e2009-09-05 21:47:34 +00002916 fx_info->file=file;
2917 return(status);
2918}
2919
cristy7832dc22011-09-05 01:21:53 +00002920MagickPrivate MagickBooleanType FxEvaluateChannelExpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002921 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristya19f1d72012-08-07 18:24:38 +00002922 double *alpha,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002923{
cristya19f1d72012-08-07 18:24:38 +00002924 double
cristy3ed852e2009-09-05 21:47:34 +00002925 beta;
2926
2927 beta=0.0;
2928 *alpha=FxEvaluateSubexpression(fx_info,channel,x,y,fx_info->expression,&beta,
2929 exception);
2930 return(exception->severity == OptionError ? MagickFalse : MagickTrue);
2931}
2932
2933/*
2934%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2935% %
2936% %
2937% %
2938% F x I m a g e %
2939% %
2940% %
2941% %
2942%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2943%
2944% FxImage() applies a mathematical expression to the specified image.
2945%
2946% The format of the FxImage method is:
2947%
2948% Image *FxImage(const Image *image,const char *expression,
2949% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002950%
2951% A description of each parameter follows:
2952%
2953% o image: the image.
2954%
cristy3ed852e2009-09-05 21:47:34 +00002955% o expression: A mathematical expression.
2956%
2957% o exception: return any errors or warnings in this structure.
2958%
2959*/
2960
2961static FxInfo **DestroyFxThreadSet(FxInfo **fx_info)
2962{
cristybb503372010-05-27 20:51:26 +00002963 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002964 i;
2965
2966 assert(fx_info != (FxInfo **) NULL);
cristyac245f82012-05-05 17:13:57 +00002967 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
cristy3ed852e2009-09-05 21:47:34 +00002968 if (fx_info[i] != (FxInfo *) NULL)
2969 fx_info[i]=DestroyFxInfo(fx_info[i]);
cristyb41ee102010-10-04 16:46:15 +00002970 fx_info=(FxInfo **) RelinquishMagickMemory(fx_info);
cristy3ed852e2009-09-05 21:47:34 +00002971 return(fx_info);
2972}
2973
2974static FxInfo **AcquireFxThreadSet(const Image *image,const char *expression,
2975 ExceptionInfo *exception)
2976{
2977 char
2978 *fx_expression;
2979
2980 FxInfo
2981 **fx_info;
2982
cristya19f1d72012-08-07 18:24:38 +00002983 double
cristy3ed852e2009-09-05 21:47:34 +00002984 alpha;
2985
cristybb503372010-05-27 20:51:26 +00002986 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002987 i;
2988
cristybb503372010-05-27 20:51:26 +00002989 size_t
cristy3ed852e2009-09-05 21:47:34 +00002990 number_threads;
2991
cristy9357bdd2012-07-30 12:28:34 +00002992 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
cristyb41ee102010-10-04 16:46:15 +00002993 fx_info=(FxInfo **) AcquireQuantumMemory(number_threads,sizeof(*fx_info));
cristy3ed852e2009-09-05 21:47:34 +00002994 if (fx_info == (FxInfo **) NULL)
2995 return((FxInfo **) NULL);
2996 (void) ResetMagickMemory(fx_info,0,number_threads*sizeof(*fx_info));
2997 if (*expression != '@')
2998 fx_expression=ConstantString(expression);
2999 else
3000 fx_expression=FileToString(expression+1,~0,exception);
cristybb503372010-05-27 20:51:26 +00003001 for (i=0; i < (ssize_t) number_threads; i++)
cristy3ed852e2009-09-05 21:47:34 +00003002 {
cristydb070952012-04-20 14:33:00 +00003003 fx_info[i]=AcquireFxInfo(image,fx_expression,exception);
cristy3ed852e2009-09-05 21:47:34 +00003004 if (fx_info[i] == (FxInfo *) NULL)
3005 return(DestroyFxThreadSet(fx_info));
3006 (void) FxPreprocessExpression(fx_info[i],&alpha,fx_info[i]->exception);
3007 }
3008 fx_expression=DestroyString(fx_expression);
3009 return(fx_info);
3010}
3011
3012MagickExport Image *FxImage(const Image *image,const char *expression,
3013 ExceptionInfo *exception)
3014{
cristy3ed852e2009-09-05 21:47:34 +00003015#define FxImageTag "Fx/Image"
3016
cristyfa112112010-01-04 17:48:07 +00003017 CacheView
cristy79cedc72011-07-25 00:41:15 +00003018 *fx_view,
3019 *image_view;
cristyfa112112010-01-04 17:48:07 +00003020
cristy3ed852e2009-09-05 21:47:34 +00003021 FxInfo
cristyfa112112010-01-04 17:48:07 +00003022 **restrict fx_info;
cristy3ed852e2009-09-05 21:47:34 +00003023
3024 Image
3025 *fx_image;
3026
cristy3ed852e2009-09-05 21:47:34 +00003027 MagickBooleanType
3028 status;
3029
cristybb503372010-05-27 20:51:26 +00003030 MagickOffsetType
3031 progress;
3032
cristya19f1d72012-08-07 18:24:38 +00003033 double
cristy3ed852e2009-09-05 21:47:34 +00003034 alpha;
3035
cristybb503372010-05-27 20:51:26 +00003036 ssize_t
3037 y;
3038
cristy3ed852e2009-09-05 21:47:34 +00003039 assert(image != (Image *) NULL);
3040 assert(image->signature == MagickSignature);
3041 if (image->debug != MagickFalse)
3042 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy79cedc72011-07-25 00:41:15 +00003043 fx_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00003044 if (fx_image == (Image *) NULL)
3045 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003046 if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003047 {
cristy3ed852e2009-09-05 21:47:34 +00003048 fx_image=DestroyImage(fx_image);
3049 return((Image *) NULL);
3050 }
3051 fx_info=AcquireFxThreadSet(image,expression,exception);
3052 if (fx_info == (FxInfo **) NULL)
3053 {
3054 fx_image=DestroyImage(fx_image);
3055 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3056 }
3057 status=FxPreprocessExpression(fx_info[0],&alpha,exception);
3058 if (status == MagickFalse)
3059 {
3060 fx_image=DestroyImage(fx_image);
3061 fx_info=DestroyFxThreadSet(fx_info);
3062 return((Image *) NULL);
3063 }
3064 /*
3065 Fx image.
3066 */
3067 status=MagickTrue;
3068 progress=0;
cristydb070952012-04-20 14:33:00 +00003069 image_view=AcquireVirtualCacheView(image,exception);
3070 fx_view=AcquireAuthenticCacheView(fx_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00003071#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003072 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00003073 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003074#endif
cristybb503372010-05-27 20:51:26 +00003075 for (y=0; y < (ssize_t) fx_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003076 {
cristy5c9e6f22010-09-17 17:31:01 +00003077 const int
3078 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00003079
cristy79cedc72011-07-25 00:41:15 +00003080 register const Quantum
3081 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003082
cristy4c08aed2011-07-01 19:47:50 +00003083 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003084 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003085
cristy79cedc72011-07-25 00:41:15 +00003086 register ssize_t
3087 x;
3088
cristy3ed852e2009-09-05 21:47:34 +00003089 if (status == MagickFalse)
3090 continue;
cristy79cedc72011-07-25 00:41:15 +00003091 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +00003092 q=QueueCacheViewAuthenticPixels(fx_view,0,y,fx_image->columns,1,exception);
cristy79cedc72011-07-25 00:41:15 +00003093 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003094 {
3095 status=MagickFalse;
3096 continue;
3097 }
cristybb503372010-05-27 20:51:26 +00003098 for (x=0; x < (ssize_t) fx_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003099 {
cristy79cedc72011-07-25 00:41:15 +00003100 register ssize_t
3101 i;
3102
3103 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3104 {
cristya19f1d72012-08-07 18:24:38 +00003105 double
cristy79cedc72011-07-25 00:41:15 +00003106 alpha;
3107
3108 PixelChannel
3109 channel;
3110
3111 PixelTrait
3112 fx_traits,
3113 traits;
3114
cristycf1296e2012-08-26 23:40:49 +00003115 channel=GetPixelChannelChannel(image,i);
3116 traits=GetPixelChannelTraits(image,channel);
3117 fx_traits=GetPixelChannelTraits(fx_image,channel);
cristy010d7d12011-08-31 01:02:48 +00003118 if ((traits == UndefinedPixelTrait) ||
3119 (fx_traits == UndefinedPixelTrait))
cristy79cedc72011-07-25 00:41:15 +00003120 continue;
cristy1eced092012-08-10 23:10:56 +00003121 if (((fx_traits & CopyPixelTrait) != 0) ||
3122 (GetPixelMask(image,p) != 0))
cristy79cedc72011-07-25 00:41:15 +00003123 {
cristy0beccfa2011-09-25 20:47:53 +00003124 SetPixelChannel(fx_image,channel,p[i],q);
cristy79cedc72011-07-25 00:41:15 +00003125 continue;
3126 }
3127 alpha=0.0;
cristya382aca2011-12-06 18:22:48 +00003128 (void) FxEvaluateChannelExpression(fx_info[id],channel,x,y,&alpha,
3129 exception);
cristy8cd03c32012-07-07 18:57:59 +00003130 q[i]=ClampToQuantum(QuantumRange*alpha);
cristy79cedc72011-07-25 00:41:15 +00003131 }
3132 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003133 q+=GetPixelChannels(fx_image);
cristy3ed852e2009-09-05 21:47:34 +00003134 }
3135 if (SyncCacheViewAuthenticPixels(fx_view,exception) == MagickFalse)
3136 status=MagickFalse;
3137 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3138 {
3139 MagickBooleanType
3140 proceed;
3141
cristyb5d5f722009-11-04 03:03:49 +00003142#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003143 #pragma omp critical (MagickCore_FxImage)
cristy3ed852e2009-09-05 21:47:34 +00003144#endif
3145 proceed=SetImageProgress(image,FxImageTag,progress++,image->rows);
3146 if (proceed == MagickFalse)
3147 status=MagickFalse;
3148 }
3149 }
cristy3ed852e2009-09-05 21:47:34 +00003150 fx_view=DestroyCacheView(fx_view);
cristy79cedc72011-07-25 00:41:15 +00003151 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003152 fx_info=DestroyFxThreadSet(fx_info);
3153 if (status == MagickFalse)
3154 fx_image=DestroyImage(fx_image);
3155 return(fx_image);
3156}
3157
3158/*
3159%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3160% %
3161% %
3162% %
3163% I m p l o d e I m a g e %
3164% %
3165% %
3166% %
3167%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3168%
3169% ImplodeImage() creates a new image that is a copy of an existing
3170% one with the image pixels "implode" by the specified percentage. It
3171% allocates the memory necessary for the new Image structure and returns a
3172% pointer to the new image.
3173%
3174% The format of the ImplodeImage method is:
3175%
3176% Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003177% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003178%
3179% A description of each parameter follows:
3180%
3181% o implode_image: Method ImplodeImage returns a pointer to the image
3182% after it is implode. A null image is returned if there is a memory
3183% shortage.
3184%
3185% o image: the image.
3186%
3187% o amount: Define the extent of the implosion.
3188%
cristy76f512e2011-09-12 01:26:56 +00003189% o method: the pixel interpolation method.
3190%
cristy3ed852e2009-09-05 21:47:34 +00003191% o exception: return any errors or warnings in this structure.
3192%
3193*/
3194MagickExport Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003195 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003196{
3197#define ImplodeImageTag "Implode/Image"
3198
cristyfa112112010-01-04 17:48:07 +00003199 CacheView
3200 *image_view,
3201 *implode_view;
3202
cristy3ed852e2009-09-05 21:47:34 +00003203 Image
3204 *implode_image;
3205
cristy3ed852e2009-09-05 21:47:34 +00003206 MagickBooleanType
3207 status;
3208
cristybb503372010-05-27 20:51:26 +00003209 MagickOffsetType
3210 progress;
3211
cristya19f1d72012-08-07 18:24:38 +00003212 double
cristy3ed852e2009-09-05 21:47:34 +00003213 radius;
3214
3215 PointInfo
3216 center,
3217 scale;
3218
cristybb503372010-05-27 20:51:26 +00003219 ssize_t
3220 y;
3221
cristy3ed852e2009-09-05 21:47:34 +00003222 /*
3223 Initialize implode image attributes.
3224 */
3225 assert(image != (Image *) NULL);
3226 assert(image->signature == MagickSignature);
3227 if (image->debug != MagickFalse)
3228 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3229 assert(exception != (ExceptionInfo *) NULL);
3230 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00003231 implode_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3232 exception);
cristy3ed852e2009-09-05 21:47:34 +00003233 if (implode_image == (Image *) NULL)
3234 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003235 if (SetImageStorageClass(implode_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003236 {
cristy3ed852e2009-09-05 21:47:34 +00003237 implode_image=DestroyImage(implode_image);
3238 return((Image *) NULL);
3239 }
cristy4c08aed2011-07-01 19:47:50 +00003240 if (implode_image->background_color.alpha != OpaqueAlpha)
cristy8a46d822012-08-28 23:32:39 +00003241 implode_image->alpha_trait=BlendPixelTrait;
cristy3ed852e2009-09-05 21:47:34 +00003242 /*
3243 Compute scaling factor.
3244 */
3245 scale.x=1.0;
3246 scale.y=1.0;
3247 center.x=0.5*image->columns;
3248 center.y=0.5*image->rows;
3249 radius=center.x;
3250 if (image->columns > image->rows)
3251 scale.y=(double) image->columns/(double) image->rows;
3252 else
3253 if (image->columns < image->rows)
3254 {
3255 scale.x=(double) image->rows/(double) image->columns;
3256 radius=center.y;
3257 }
3258 /*
3259 Implode image.
3260 */
3261 status=MagickTrue;
3262 progress=0;
cristydb070952012-04-20 14:33:00 +00003263 image_view=AcquireVirtualCacheView(image,exception);
3264 implode_view=AcquireAuthenticCacheView(implode_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00003265#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003266 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00003267 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003268#endif
cristybb503372010-05-27 20:51:26 +00003269 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003270 {
cristya19f1d72012-08-07 18:24:38 +00003271 double
cristy3ed852e2009-09-05 21:47:34 +00003272 distance;
3273
3274 PointInfo
3275 delta;
3276
cristy6d188022011-09-12 13:23:33 +00003277 register const Quantum
3278 *restrict p;
3279
cristybb503372010-05-27 20:51:26 +00003280 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003281 x;
3282
cristy4c08aed2011-07-01 19:47:50 +00003283 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003284 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003285
3286 if (status == MagickFalse)
3287 continue;
cristy6d188022011-09-12 13:23:33 +00003288 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +00003289 q=QueueCacheViewAuthenticPixels(implode_view,0,y,implode_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003290 exception);
cristy6d188022011-09-12 13:23:33 +00003291 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003292 {
3293 status=MagickFalse;
3294 continue;
3295 }
cristy3ed852e2009-09-05 21:47:34 +00003296 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00003297 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003298 {
cristy6d188022011-09-12 13:23:33 +00003299 register ssize_t
3300 i;
3301
cristy3ed852e2009-09-05 21:47:34 +00003302 /*
3303 Determine if the pixel is within an ellipse.
3304 */
cristy10a6c612012-01-29 21:41:05 +00003305 if (GetPixelMask(image,p) != 0)
3306 {
3307 p+=GetPixelChannels(image);
3308 q+=GetPixelChannels(implode_image);
3309 continue;
3310 }
cristy3ed852e2009-09-05 21:47:34 +00003311 delta.x=scale.x*(double) (x-center.x);
3312 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00003313 if (distance >= (radius*radius))
cristya6d7a9b2012-01-18 20:04:48 +00003314 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy6d188022011-09-12 13:23:33 +00003315 {
cristya6d7a9b2012-01-18 20:04:48 +00003316 PixelChannel
3317 channel;
3318
cristy1707c6c2012-01-18 23:30:54 +00003319 PixelTrait
3320 implode_traits,
3321 traits;
3322
cristycf1296e2012-08-26 23:40:49 +00003323 channel=GetPixelChannelChannel(image,i);
3324 traits=GetPixelChannelTraits(image,channel);
3325 implode_traits=GetPixelChannelTraits(implode_image,channel);
cristy1707c6c2012-01-18 23:30:54 +00003326 if ((traits == UndefinedPixelTrait) ||
3327 (implode_traits == UndefinedPixelTrait))
3328 continue;
cristya6d7a9b2012-01-18 20:04:48 +00003329 SetPixelChannel(implode_image,channel,p[i],q);
cristy6d188022011-09-12 13:23:33 +00003330 }
3331 else
cristy3ed852e2009-09-05 21:47:34 +00003332 {
3333 double
3334 factor;
3335
3336 /*
3337 Implode the pixel.
3338 */
3339 factor=1.0;
3340 if (distance > 0.0)
cristy1707c6c2012-01-18 23:30:54 +00003341 factor=pow(sin((double) (MagickPI*sqrt((double) distance)/radius/
3342 2)),-amount);
cristy76f512e2011-09-12 01:26:56 +00003343 status=InterpolatePixelChannels(image,image_view,implode_image,method,
3344 (double) (factor*delta.x/scale.x+center.x),(double) (factor*delta.y/
3345 scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00003346 }
cristy6d188022011-09-12 13:23:33 +00003347 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003348 q+=GetPixelChannels(implode_image);
cristy3ed852e2009-09-05 21:47:34 +00003349 }
3350 if (SyncCacheViewAuthenticPixels(implode_view,exception) == MagickFalse)
3351 status=MagickFalse;
3352 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3353 {
3354 MagickBooleanType
3355 proceed;
3356
cristyb5d5f722009-11-04 03:03:49 +00003357#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003358 #pragma omp critical (MagickCore_ImplodeImage)
cristy3ed852e2009-09-05 21:47:34 +00003359#endif
3360 proceed=SetImageProgress(image,ImplodeImageTag,progress++,image->rows);
3361 if (proceed == MagickFalse)
3362 status=MagickFalse;
3363 }
3364 }
3365 implode_view=DestroyCacheView(implode_view);
3366 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003367 if (status == MagickFalse)
3368 implode_image=DestroyImage(implode_image);
3369 return(implode_image);
3370}
3371
3372/*
3373%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3374% %
3375% %
3376% %
3377% M o r p h I m a g e s %
3378% %
3379% %
3380% %
3381%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3382%
3383% The MorphImages() method requires a minimum of two images. The first
3384% image is transformed into the second by a number of intervening images
3385% as specified by frames.
3386%
3387% The format of the MorphImage method is:
3388%
cristybb503372010-05-27 20:51:26 +00003389% Image *MorphImages(const Image *image,const size_t number_frames,
cristy3ed852e2009-09-05 21:47:34 +00003390% ExceptionInfo *exception)
3391%
3392% A description of each parameter follows:
3393%
3394% o image: the image.
3395%
3396% o number_frames: Define the number of in-between image to generate.
3397% The more in-between frames, the smoother the morph.
3398%
3399% o exception: return any errors or warnings in this structure.
3400%
3401*/
3402MagickExport Image *MorphImages(const Image *image,
cristybb503372010-05-27 20:51:26 +00003403 const size_t number_frames,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003404{
3405#define MorphImageTag "Morph/Image"
3406
3407 Image
3408 *morph_image,
3409 *morph_images;
3410
cristy9d314ff2011-03-09 01:30:28 +00003411 MagickBooleanType
3412 status;
cristy3ed852e2009-09-05 21:47:34 +00003413
3414 MagickOffsetType
3415 scene;
3416
cristya19f1d72012-08-07 18:24:38 +00003417 double
cristy3ed852e2009-09-05 21:47:34 +00003418 alpha,
3419 beta;
3420
3421 register const Image
3422 *next;
3423
cristybb503372010-05-27 20:51:26 +00003424 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003425 i;
3426
cristy9d314ff2011-03-09 01:30:28 +00003427 ssize_t
3428 y;
cristy3ed852e2009-09-05 21:47:34 +00003429
3430 /*
3431 Clone first frame in sequence.
3432 */
3433 assert(image != (Image *) NULL);
3434 assert(image->signature == MagickSignature);
3435 if (image->debug != MagickFalse)
3436 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3437 assert(exception != (ExceptionInfo *) NULL);
3438 assert(exception->signature == MagickSignature);
3439 morph_images=CloneImage(image,0,0,MagickTrue,exception);
3440 if (morph_images == (Image *) NULL)
3441 return((Image *) NULL);
3442 if (GetNextImageInList(image) == (Image *) NULL)
3443 {
3444 /*
3445 Morph single image.
3446 */
cristybb503372010-05-27 20:51:26 +00003447 for (i=1; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003448 {
3449 morph_image=CloneImage(image,0,0,MagickTrue,exception);
3450 if (morph_image == (Image *) NULL)
3451 {
3452 morph_images=DestroyImageList(morph_images);
3453 return((Image *) NULL);
3454 }
3455 AppendImageToList(&morph_images,morph_image);
cristy8b27a6d2010-02-14 03:31:15 +00003456 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003457 {
cristy8b27a6d2010-02-14 03:31:15 +00003458 MagickBooleanType
3459 proceed;
3460
cristybb503372010-05-27 20:51:26 +00003461 proceed=SetImageProgress(image,MorphImageTag,(MagickOffsetType) i,
3462 number_frames);
cristy8b27a6d2010-02-14 03:31:15 +00003463 if (proceed == MagickFalse)
3464 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00003465 }
3466 }
3467 return(GetFirstImageInList(morph_images));
3468 }
3469 /*
3470 Morph image sequence.
3471 */
3472 status=MagickTrue;
3473 scene=0;
3474 next=image;
3475 for ( ; GetNextImageInList(next) != (Image *) NULL; next=GetNextImageInList(next))
3476 {
cristybb503372010-05-27 20:51:26 +00003477 for (i=0; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003478 {
3479 CacheView
3480 *image_view,
3481 *morph_view;
3482
cristya19f1d72012-08-07 18:24:38 +00003483 beta=(double) (i+1.0)/(double) (number_frames+1.0);
cristy3ed852e2009-09-05 21:47:34 +00003484 alpha=1.0-beta;
cristy15b98cd2010-09-12 19:42:50 +00003485 morph_image=ResizeImage(next,(size_t) (alpha*next->columns+beta*
cristyaa2c16c2012-03-25 22:21:35 +00003486 GetNextImageInList(next)->columns+0.5),(size_t) (alpha*next->rows+beta*
3487 GetNextImageInList(next)->rows+0.5),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 }
cristy1707c6c2012-01-18 23:30:54 +00003493 status=SetImageStorageClass(morph_image,DirectClass,exception);
3494 if (status == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003495 {
cristy3ed852e2009-09-05 21:47:34 +00003496 morph_image=DestroyImage(morph_image);
3497 return((Image *) NULL);
3498 }
3499 AppendImageToList(&morph_images,morph_image);
3500 morph_images=GetLastImageInList(morph_images);
cristy15b98cd2010-09-12 19:42:50 +00003501 morph_image=ResizeImage(GetNextImageInList(next),morph_images->columns,
cristyaa2c16c2012-03-25 22:21:35 +00003502 morph_images->rows,GetNextImageInList(next)->filter,exception);
cristy3ed852e2009-09-05 21:47:34 +00003503 if (morph_image == (Image *) NULL)
3504 {
3505 morph_images=DestroyImageList(morph_images);
3506 return((Image *) NULL);
3507 }
cristydb070952012-04-20 14:33:00 +00003508 image_view=AcquireVirtualCacheView(morph_image,exception);
3509 morph_view=AcquireAuthenticCacheView(morph_images,exception);
cristyb5d5f722009-11-04 03:03:49 +00003510#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003511 #pragma omp parallel for schedule(static,4) shared(status) \
cristy4ee2b0c2012-05-15 00:30:35 +00003512 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003513#endif
cristybb503372010-05-27 20:51:26 +00003514 for (y=0; y < (ssize_t) morph_images->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003515 {
3516 MagickBooleanType
3517 sync;
3518
cristy4c08aed2011-07-01 19:47:50 +00003519 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003520 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003521
cristybb503372010-05-27 20:51:26 +00003522 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003523 x;
3524
cristy4c08aed2011-07-01 19:47:50 +00003525 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003526 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003527
3528 if (status == MagickFalse)
3529 continue;
3530 p=GetCacheViewVirtualPixels(image_view,0,y,morph_image->columns,1,
3531 exception);
3532 q=GetCacheViewAuthenticPixels(morph_view,0,y,morph_images->columns,1,
3533 exception);
cristy4c08aed2011-07-01 19:47:50 +00003534 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003535 {
3536 status=MagickFalse;
3537 continue;
3538 }
cristybb503372010-05-27 20:51:26 +00003539 for (x=0; x < (ssize_t) morph_images->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003540 {
cristy1707c6c2012-01-18 23:30:54 +00003541 register ssize_t
3542 i;
3543
cristy10a6c612012-01-29 21:41:05 +00003544 for (i=0; i < (ssize_t) GetPixelChannels(morph_image); i++)
cristy1707c6c2012-01-18 23:30:54 +00003545 {
3546 PixelChannel
3547 channel;
3548
3549 PixelTrait
3550 morph_traits,
3551 traits;
3552
cristycf1296e2012-08-26 23:40:49 +00003553 channel=GetPixelChannelChannel(image,i);
3554 traits=GetPixelChannelTraits(image,channel);
3555 morph_traits=GetPixelChannelTraits(morph_image,channel);
cristy1707c6c2012-01-18 23:30:54 +00003556 if ((traits == UndefinedPixelTrait) ||
3557 (morph_traits == UndefinedPixelTrait))
3558 continue;
cristy1eced092012-08-10 23:10:56 +00003559 if (((morph_traits & CopyPixelTrait) != 0) ||
3560 (GetPixelMask(image,p) != 0))
cristy1707c6c2012-01-18 23:30:54 +00003561 {
3562 SetPixelChannel(morph_image,channel,p[i],q);
3563 continue;
3564 }
3565 SetPixelChannel(morph_image,channel,ClampToQuantum(alpha*
3566 GetPixelChannel(morph_images,channel,q)+beta*p[i]),q);
3567 }
cristyed231572011-07-14 02:18:59 +00003568 p+=GetPixelChannels(morph_image);
3569 q+=GetPixelChannels(morph_images);
cristy3ed852e2009-09-05 21:47:34 +00003570 }
3571 sync=SyncCacheViewAuthenticPixels(morph_view,exception);
3572 if (sync == MagickFalse)
3573 status=MagickFalse;
3574 }
3575 morph_view=DestroyCacheView(morph_view);
3576 image_view=DestroyCacheView(image_view);
3577 morph_image=DestroyImage(morph_image);
3578 }
cristybb503372010-05-27 20:51:26 +00003579 if (i < (ssize_t) number_frames)
cristy3ed852e2009-09-05 21:47:34 +00003580 break;
3581 /*
3582 Clone last frame in sequence.
3583 */
3584 morph_image=CloneImage(GetNextImageInList(next),0,0,MagickTrue,exception);
3585 if (morph_image == (Image *) NULL)
3586 {
3587 morph_images=DestroyImageList(morph_images);
3588 return((Image *) NULL);
3589 }
3590 AppendImageToList(&morph_images,morph_image);
3591 morph_images=GetLastImageInList(morph_images);
3592 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3593 {
3594 MagickBooleanType
3595 proceed;
3596
cristyb5d5f722009-11-04 03:03:49 +00003597#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003598 #pragma omp critical (MagickCore_MorphImages)
cristy3ed852e2009-09-05 21:47:34 +00003599#endif
3600 proceed=SetImageProgress(image,MorphImageTag,scene,
3601 GetImageListLength(image));
3602 if (proceed == MagickFalse)
3603 status=MagickFalse;
3604 }
3605 scene++;
3606 }
3607 if (GetNextImageInList(next) != (Image *) NULL)
3608 {
3609 morph_images=DestroyImageList(morph_images);
3610 return((Image *) NULL);
3611 }
3612 return(GetFirstImageInList(morph_images));
3613}
3614
3615/*
3616%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3617% %
3618% %
3619% %
3620% P l a s m a I m a g e %
3621% %
3622% %
3623% %
3624%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3625%
3626% PlasmaImage() initializes an image with plasma fractal values. The image
3627% must be initialized with a base color and the random number generator
3628% seeded before this method is called.
3629%
3630% The format of the PlasmaImage method is:
3631%
3632% MagickBooleanType PlasmaImage(Image *image,const SegmentInfo *segment,
cristy5cbc0162011-08-29 00:36:28 +00003633% size_t attenuate,size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003634%
3635% A description of each parameter follows:
3636%
3637% o image: the image.
3638%
3639% o segment: Define the region to apply plasma fractals values.
3640%
glennrp7dae1ca2010-09-16 12:17:35 +00003641% o attenuate: Define the plasma attenuation factor.
cristy3ed852e2009-09-05 21:47:34 +00003642%
3643% o depth: Limit the plasma recursion depth.
3644%
cristy5cbc0162011-08-29 00:36:28 +00003645% o exception: return any errors or warnings in this structure.
3646%
cristy3ed852e2009-09-05 21:47:34 +00003647*/
3648
3649static inline Quantum PlasmaPixel(RandomInfo *random_info,
cristya19f1d72012-08-07 18:24:38 +00003650 const double pixel,const double noise)
cristy3ed852e2009-09-05 21:47:34 +00003651{
3652 Quantum
3653 plasma;
3654
cristyce70c172010-01-07 17:15:30 +00003655 plasma=ClampToQuantum(pixel+noise*GetPseudoRandomValue(random_info)-
cristy3ed852e2009-09-05 21:47:34 +00003656 noise/2.0);
3657 return(plasma);
3658}
3659
cristyda1f9c12011-10-02 21:39:49 +00003660static MagickBooleanType PlasmaImageProxy(Image *image,CacheView *image_view,
3661 CacheView *u_view,CacheView *v_view,RandomInfo *random_info,
3662 const SegmentInfo *segment,size_t attenuate,size_t depth,
3663 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003664{
cristya19f1d72012-08-07 18:24:38 +00003665 double
cristy3ed852e2009-09-05 21:47:34 +00003666 plasma;
3667
cristyda1f9c12011-10-02 21:39:49 +00003668 PixelChannel
3669 channel;
3670
3671 PixelTrait
3672 traits;
3673
3674 register const Quantum
3675 *restrict u,
3676 *restrict v;
3677
3678 register Quantum
3679 *restrict q;
3680
3681 register ssize_t
3682 i;
cristy3ed852e2009-09-05 21:47:34 +00003683
cristy9d314ff2011-03-09 01:30:28 +00003684 ssize_t
3685 x,
3686 x_mid,
3687 y,
3688 y_mid;
3689
cristy3ed852e2009-09-05 21:47:34 +00003690 if (((segment->x2-segment->x1) == 0.0) && ((segment->y2-segment->y1) == 0.0))
3691 return(MagickTrue);
3692 if (depth != 0)
3693 {
3694 SegmentInfo
3695 local_info;
3696
3697 /*
3698 Divide the area into quadrants and recurse.
3699 */
3700 depth--;
3701 attenuate++;
cristybb503372010-05-27 20:51:26 +00003702 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3703 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003704 local_info=(*segment);
3705 local_info.x2=(double) x_mid;
3706 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003707 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3708 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003709 local_info=(*segment);
3710 local_info.y1=(double) y_mid;
3711 local_info.x2=(double) x_mid;
cristyda1f9c12011-10-02 21:39:49 +00003712 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3713 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003714 local_info=(*segment);
3715 local_info.x1=(double) x_mid;
3716 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003717 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3718 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003719 local_info=(*segment);
3720 local_info.x1=(double) x_mid;
3721 local_info.y1=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003722 return(PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3723 &local_info,attenuate,depth,exception));
cristy3ed852e2009-09-05 21:47:34 +00003724 }
cristybb503372010-05-27 20:51:26 +00003725 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3726 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003727 if ((segment->x1 == (double) x_mid) && (segment->x2 == (double) x_mid) &&
3728 (segment->y1 == (double) y_mid) && (segment->y2 == (double) y_mid))
3729 return(MagickFalse);
3730 /*
3731 Average pixels and apply plasma.
3732 */
cristya19f1d72012-08-07 18:24:38 +00003733 plasma=(double) QuantumRange/(2.0*attenuate);
cristy3ed852e2009-09-05 21:47:34 +00003734 if ((segment->x1 != (double) x_mid) || (segment->x2 != (double) x_mid))
3735 {
cristy3ed852e2009-09-05 21:47:34 +00003736 /*
3737 Left pixel.
3738 */
cristybb503372010-05-27 20:51:26 +00003739 x=(ssize_t) ceil(segment->x1-0.5);
cristy1707c6c2012-01-18 23:30:54 +00003740 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),1,1,
3741 exception);
3742 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),1,1,
3743 exception);
cristyc5c6f662010-09-22 14:23:02 +00003744 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003745 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3746 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003747 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003748 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3749 {
cristycf1296e2012-08-26 23:40:49 +00003750 channel=GetPixelChannelChannel(image,i);
3751 traits=GetPixelChannelTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003752 if (traits == UndefinedPixelTrait)
3753 continue;
3754 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3755 }
cristyc5c6f662010-09-22 14:23:02 +00003756 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003757 if (segment->x1 != segment->x2)
3758 {
3759 /*
3760 Right pixel.
3761 */
cristybb503372010-05-27 20:51:26 +00003762 x=(ssize_t) ceil(segment->x2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003763 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3764 1,1,exception);
3765 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3766 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003767 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003768 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3769 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003770 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003771 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3772 {
cristycf1296e2012-08-26 23:40:49 +00003773 channel=GetPixelChannelChannel(image,i);
3774 traits=GetPixelChannelTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003775 if (traits == UndefinedPixelTrait)
3776 continue;
3777 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3778 }
cristyc5c6f662010-09-22 14:23:02 +00003779 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003780 }
3781 }
3782 if ((segment->y1 != (double) y_mid) || (segment->y2 != (double) y_mid))
3783 {
3784 if ((segment->x1 != (double) x_mid) || (segment->y2 != (double) y_mid))
3785 {
cristy3ed852e2009-09-05 21:47:34 +00003786 /*
3787 Bottom pixel.
3788 */
cristybb503372010-05-27 20:51:26 +00003789 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003790 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3791 1,1,exception);
3792 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3793 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003794 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003795 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3796 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003797 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003798 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3799 {
cristycf1296e2012-08-26 23:40:49 +00003800 channel=GetPixelChannelChannel(image,i);
3801 traits=GetPixelChannelTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003802 if (traits == UndefinedPixelTrait)
3803 continue;
3804 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3805 }
cristyc5c6f662010-09-22 14:23:02 +00003806 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003807 }
3808 if (segment->y1 != segment->y2)
3809 {
cristy3ed852e2009-09-05 21:47:34 +00003810 /*
3811 Top pixel.
3812 */
cristybb503372010-05-27 20:51:26 +00003813 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003814 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3815 1,1,exception);
3816 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3817 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003818 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003819 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3820 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003821 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003822 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3823 {
cristycf1296e2012-08-26 23:40:49 +00003824 channel=GetPixelChannelChannel(image,i);
3825 traits=GetPixelChannelTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003826 if (traits == UndefinedPixelTrait)
3827 continue;
3828 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3829 }
cristyc5c6f662010-09-22 14:23:02 +00003830 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003831 }
3832 }
3833 if ((segment->x1 != segment->x2) || (segment->y1 != segment->y2))
3834 {
cristy3ed852e2009-09-05 21:47:34 +00003835 /*
3836 Middle pixel.
3837 */
cristybb503372010-05-27 20:51:26 +00003838 x=(ssize_t) ceil(segment->x1-0.5);
3839 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003840 u=GetCacheViewVirtualPixels(u_view,x,y,1,1,exception);
cristybb503372010-05-27 20:51:26 +00003841 x=(ssize_t) ceil(segment->x2-0.5);
3842 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003843 v=GetCacheViewVirtualPixels(v_view,x,y,1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003844 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003845 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3846 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003847 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003848 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3849 {
cristycf1296e2012-08-26 23:40:49 +00003850 channel=GetPixelChannelChannel(image,i);
3851 traits=GetPixelChannelTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003852 if (traits == UndefinedPixelTrait)
3853 continue;
3854 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3855 }
cristyc5c6f662010-09-22 14:23:02 +00003856 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003857 }
3858 if (((segment->x2-segment->x1) < 3.0) && ((segment->y2-segment->y1) < 3.0))
3859 return(MagickTrue);
3860 return(MagickFalse);
3861}
cristyda1f9c12011-10-02 21:39:49 +00003862
cristy3ed852e2009-09-05 21:47:34 +00003863MagickExport MagickBooleanType PlasmaImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00003864 const SegmentInfo *segment,size_t attenuate,size_t depth,
3865 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003866{
cristyc5c6f662010-09-22 14:23:02 +00003867 CacheView
cristyda1f9c12011-10-02 21:39:49 +00003868 *image_view,
3869 *u_view,
3870 *v_view;
cristyc5c6f662010-09-22 14:23:02 +00003871
cristy3ed852e2009-09-05 21:47:34 +00003872 MagickBooleanType
3873 status;
3874
3875 RandomInfo
3876 *random_info;
3877
3878 if (image->debug != MagickFalse)
3879 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3880 assert(image != (Image *) NULL);
3881 assert(image->signature == MagickSignature);
3882 if (image->debug != MagickFalse)
3883 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
cristy5cbc0162011-08-29 00:36:28 +00003884 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristyc5c6f662010-09-22 14:23:02 +00003885 return(MagickFalse);
cristydb070952012-04-20 14:33:00 +00003886 image_view=AcquireAuthenticCacheView(image,exception);
3887 u_view=AcquireVirtualCacheView(image,exception);
3888 v_view=AcquireVirtualCacheView(image,exception);
cristy3ed852e2009-09-05 21:47:34 +00003889 random_info=AcquireRandomInfo();
cristyda1f9c12011-10-02 21:39:49 +00003890 status=PlasmaImageProxy(image,image_view,u_view,v_view,random_info,segment,
3891 attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003892 random_info=DestroyRandomInfo(random_info);
cristyda1f9c12011-10-02 21:39:49 +00003893 v_view=DestroyCacheView(v_view);
3894 u_view=DestroyCacheView(u_view);
cristyc5c6f662010-09-22 14:23:02 +00003895 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003896 return(status);
3897}
3898
3899/*
3900%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3901% %
3902% %
3903% %
3904% P o l a r o i d I m a g e %
3905% %
3906% %
3907% %
3908%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3909%
3910% PolaroidImage() simulates a Polaroid picture.
3911%
3912% The format of the AnnotateImage method is:
3913%
3914% Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristye9e3d382011-12-14 01:50:13 +00003915% const char *caption,const double angle,
3916% const PixelInterpolateMethod method,ExceptionInfo exception)
cristy3ed852e2009-09-05 21:47:34 +00003917%
3918% A description of each parameter follows:
3919%
3920% o image: the image.
3921%
3922% o draw_info: the draw info.
3923%
cristye9e3d382011-12-14 01:50:13 +00003924% o caption: the Polaroid caption.
3925%
cristycee97112010-05-28 00:44:52 +00003926% o angle: Apply the effect along this angle.
cristy3ed852e2009-09-05 21:47:34 +00003927%
cristy5c4e2582011-09-11 19:21:03 +00003928% o method: the pixel interpolation method.
3929%
cristy3ed852e2009-09-05 21:47:34 +00003930% o exception: return any errors or warnings in this structure.
3931%
3932*/
3933MagickExport Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristye9e3d382011-12-14 01:50:13 +00003934 const char *caption,const double angle,const PixelInterpolateMethod method,
cristy5c4e2582011-09-11 19:21:03 +00003935 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003936{
cristy3ed852e2009-09-05 21:47:34 +00003937 Image
3938 *bend_image,
3939 *caption_image,
3940 *flop_image,
3941 *picture_image,
3942 *polaroid_image,
3943 *rotate_image,
3944 *trim_image;
3945
cristybb503372010-05-27 20:51:26 +00003946 size_t
cristy3ed852e2009-09-05 21:47:34 +00003947 height;
3948
cristy9d314ff2011-03-09 01:30:28 +00003949 ssize_t
3950 quantum;
3951
cristy3ed852e2009-09-05 21:47:34 +00003952 /*
3953 Simulate a Polaroid picture.
3954 */
3955 assert(image != (Image *) NULL);
3956 assert(image->signature == MagickSignature);
3957 if (image->debug != MagickFalse)
3958 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3959 assert(exception != (ExceptionInfo *) NULL);
3960 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00003961 quantum=(ssize_t) MagickMax(MagickMax((double) image->columns,(double)
cristy3ed852e2009-09-05 21:47:34 +00003962 image->rows)/25.0,10.0);
3963 height=image->rows+2*quantum;
3964 caption_image=(Image *) NULL;
cristye9e3d382011-12-14 01:50:13 +00003965 if (caption != (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003966 {
3967 char
cristye9e3d382011-12-14 01:50:13 +00003968 geometry[MaxTextExtent],
3969 *text;
cristy3ed852e2009-09-05 21:47:34 +00003970
3971 DrawInfo
3972 *annotate_info;
3973
cristy3ed852e2009-09-05 21:47:34 +00003974 MagickBooleanType
3975 status;
3976
cristy9d314ff2011-03-09 01:30:28 +00003977 ssize_t
3978 count;
3979
cristy3ed852e2009-09-05 21:47:34 +00003980 TypeMetric
3981 metrics;
3982
3983 /*
3984 Generate caption image.
3985 */
3986 caption_image=CloneImage(image,image->columns,1,MagickTrue,exception);
3987 if (caption_image == (Image *) NULL)
3988 return((Image *) NULL);
3989 annotate_info=CloneDrawInfo((const ImageInfo *) NULL,draw_info);
cristye9e3d382011-12-14 01:50:13 +00003990 text=InterpretImageProperties((ImageInfo *) NULL,(Image *) image,caption,
3991 exception);
3992 (void) CloneString(&annotate_info->text,text);
cristy6b1d05e2010-09-22 19:17:27 +00003993 count=FormatMagickCaption(caption_image,annotate_info,MagickTrue,&metrics,
cristye9e3d382011-12-14 01:50:13 +00003994 &text,exception);
3995 status=SetImageExtent(caption_image,image->columns,(size_t) ((count+1)*
3996 (metrics.ascent-metrics.descent)+0.5),exception);
cristy3ed852e2009-09-05 21:47:34 +00003997 if (status == MagickFalse)
3998 caption_image=DestroyImage(caption_image);
3999 else
4000 {
4001 caption_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00004002 (void) SetImageBackgroundColor(caption_image,exception);
cristye9e3d382011-12-14 01:50:13 +00004003 (void) CloneString(&annotate_info->text,text);
cristyb51dff52011-05-19 16:55:47 +00004004 (void) FormatLocaleString(geometry,MaxTextExtent,"+0+%g",
cristy3ed852e2009-09-05 21:47:34 +00004005 metrics.ascent);
4006 if (annotate_info->gravity == UndefinedGravity)
4007 (void) CloneString(&annotate_info->geometry,AcquireString(
4008 geometry));
cristy5cbc0162011-08-29 00:36:28 +00004009 (void) AnnotateImage(caption_image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00004010 height+=caption_image->rows;
4011 }
4012 annotate_info=DestroyDrawInfo(annotate_info);
cristye9e3d382011-12-14 01:50:13 +00004013 text=DestroyString(text);
cristy3ed852e2009-09-05 21:47:34 +00004014 }
4015 picture_image=CloneImage(image,image->columns+2*quantum,height,MagickTrue,
4016 exception);
4017 if (picture_image == (Image *) NULL)
4018 {
4019 if (caption_image != (Image *) NULL)
4020 caption_image=DestroyImage(caption_image);
4021 return((Image *) NULL);
4022 }
4023 picture_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00004024 (void) SetImageBackgroundColor(picture_image,exception);
cristy39172402012-03-30 13:04:39 +00004025 (void) CompositeImage(picture_image,image,OverCompositeOp,MagickTrue,quantum,
cristyfeb3e962012-03-29 17:25:55 +00004026 quantum,exception);
cristy3ed852e2009-09-05 21:47:34 +00004027 if (caption_image != (Image *) NULL)
4028 {
cristyfeb3e962012-03-29 17:25:55 +00004029 (void) CompositeImage(picture_image,caption_image,OverCompositeOp,
cristy39172402012-03-30 13:04:39 +00004030 MagickTrue,quantum,(ssize_t) (image->rows+3*quantum/2),exception);
cristy3ed852e2009-09-05 21:47:34 +00004031 caption_image=DestroyImage(caption_image);
4032 }
cristy9950d572011-10-01 18:22:35 +00004033 (void) QueryColorCompliance("none",AllCompliance,
4034 &picture_image->background_color,exception);
cristy63240882011-08-05 19:05:27 +00004035 (void) SetImageAlphaChannel(picture_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004036 rotate_image=RotateImage(picture_image,90.0,exception);
4037 picture_image=DestroyImage(picture_image);
4038 if (rotate_image == (Image *) NULL)
4039 return((Image *) NULL);
4040 picture_image=rotate_image;
4041 bend_image=WaveImage(picture_image,0.01*picture_image->rows,2.0*
cristy5c4e2582011-09-11 19:21:03 +00004042 picture_image->columns,method,exception);
cristy3ed852e2009-09-05 21:47:34 +00004043 picture_image=DestroyImage(picture_image);
4044 if (bend_image == (Image *) NULL)
4045 return((Image *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00004046 picture_image=bend_image;
4047 rotate_image=RotateImage(picture_image,-90.0,exception);
4048 picture_image=DestroyImage(picture_image);
4049 if (rotate_image == (Image *) NULL)
4050 return((Image *) NULL);
4051 picture_image=rotate_image;
4052 picture_image->background_color=image->background_color;
anthonyf46d4262012-03-26 03:30:34 +00004053 polaroid_image=ShadowImage(picture_image,80.0,2.0,quantum/3,quantum/3,
cristy3ed852e2009-09-05 21:47:34 +00004054 exception);
4055 if (polaroid_image == (Image *) NULL)
4056 {
4057 picture_image=DestroyImage(picture_image);
4058 return(picture_image);
4059 }
4060 flop_image=FlopImage(polaroid_image,exception);
4061 polaroid_image=DestroyImage(polaroid_image);
4062 if (flop_image == (Image *) NULL)
4063 {
4064 picture_image=DestroyImage(picture_image);
4065 return(picture_image);
4066 }
4067 polaroid_image=flop_image;
cristyfeb3e962012-03-29 17:25:55 +00004068 (void) CompositeImage(polaroid_image,picture_image,OverCompositeOp,
cristy39172402012-03-30 13:04:39 +00004069 MagickTrue,(ssize_t) (-0.01*picture_image->columns/2.0),0L,exception);
cristy3ed852e2009-09-05 21:47:34 +00004070 picture_image=DestroyImage(picture_image);
cristy9950d572011-10-01 18:22:35 +00004071 (void) QueryColorCompliance("none",AllCompliance,
4072 &polaroid_image->background_color,exception);
cristy3ed852e2009-09-05 21:47:34 +00004073 rotate_image=RotateImage(polaroid_image,angle,exception);
4074 polaroid_image=DestroyImage(polaroid_image);
4075 if (rotate_image == (Image *) NULL)
4076 return((Image *) NULL);
4077 polaroid_image=rotate_image;
4078 trim_image=TrimImage(polaroid_image,exception);
4079 polaroid_image=DestroyImage(polaroid_image);
4080 if (trim_image == (Image *) NULL)
4081 return((Image *) NULL);
4082 polaroid_image=trim_image;
4083 return(polaroid_image);
4084}
4085
4086/*
4087%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4088% %
4089% %
4090% %
cristy3ed852e2009-09-05 21:47:34 +00004091% S e p i a T o n e I m a g e %
4092% %
4093% %
4094% %
4095%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4096%
4097% MagickSepiaToneImage() applies a special effect to the image, similar to the
4098% effect achieved in a photo darkroom by sepia toning. Threshold ranges from
4099% 0 to QuantumRange and is a measure of the extent of the sepia toning. A
4100% threshold of 80% is a good starting point for a reasonable tone.
4101%
4102% The format of the SepiaToneImage method is:
4103%
4104% Image *SepiaToneImage(const Image *image,const double threshold,
4105% ExceptionInfo *exception)
4106%
4107% A description of each parameter follows:
4108%
4109% o image: the image.
4110%
4111% o threshold: the tone threshold.
4112%
4113% o exception: return any errors or warnings in this structure.
4114%
4115*/
4116MagickExport Image *SepiaToneImage(const Image *image,const double threshold,
4117 ExceptionInfo *exception)
4118{
4119#define SepiaToneImageTag "SepiaTone/Image"
4120
cristyc4c8d132010-01-07 01:58:38 +00004121 CacheView
4122 *image_view,
4123 *sepia_view;
4124
cristy3ed852e2009-09-05 21:47:34 +00004125 Image
4126 *sepia_image;
4127
cristy3ed852e2009-09-05 21:47:34 +00004128 MagickBooleanType
4129 status;
4130
cristybb503372010-05-27 20:51:26 +00004131 MagickOffsetType
4132 progress;
4133
4134 ssize_t
4135 y;
4136
cristy3ed852e2009-09-05 21:47:34 +00004137 /*
4138 Initialize sepia-toned image attributes.
4139 */
4140 assert(image != (const Image *) NULL);
4141 assert(image->signature == MagickSignature);
4142 if (image->debug != MagickFalse)
4143 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4144 assert(exception != (ExceptionInfo *) NULL);
4145 assert(exception->signature == MagickSignature);
cristy1707c6c2012-01-18 23:30:54 +00004146 sepia_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004147 if (sepia_image == (Image *) NULL)
4148 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004149 if (SetImageStorageClass(sepia_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004150 {
cristy3ed852e2009-09-05 21:47:34 +00004151 sepia_image=DestroyImage(sepia_image);
4152 return((Image *) NULL);
4153 }
4154 /*
4155 Tone each row of the image.
4156 */
4157 status=MagickTrue;
4158 progress=0;
cristydb070952012-04-20 14:33:00 +00004159 image_view=AcquireVirtualCacheView(image,exception);
4160 sepia_view=AcquireAuthenticCacheView(sepia_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00004161#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00004162 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00004163 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00004164#endif
cristybb503372010-05-27 20:51:26 +00004165 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004166 {
cristy4c08aed2011-07-01 19:47:50 +00004167 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004168 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00004169
cristybb503372010-05-27 20:51:26 +00004170 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004171 x;
4172
cristy4c08aed2011-07-01 19:47:50 +00004173 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004174 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004175
4176 if (status == MagickFalse)
4177 continue;
4178 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy1707c6c2012-01-18 23:30:54 +00004179 q=GetCacheViewAuthenticPixels(sepia_view,0,y,sepia_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00004180 exception);
cristy4c08aed2011-07-01 19:47:50 +00004181 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004182 {
4183 status=MagickFalse;
4184 continue;
4185 }
cristybb503372010-05-27 20:51:26 +00004186 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004187 {
cristya19f1d72012-08-07 18:24:38 +00004188 double
cristy3ed852e2009-09-05 21:47:34 +00004189 intensity,
4190 tone;
4191
cristyf13c5942012-08-08 23:50:11 +00004192 intensity=GetPixelIntensity(image,p);
cristya19f1d72012-08-07 18:24:38 +00004193 tone=intensity > threshold ? (double) QuantumRange : intensity+
4194 (double) QuantumRange-threshold;
cristy4c08aed2011-07-01 19:47:50 +00004195 SetPixelRed(sepia_image,ClampToQuantum(tone),q);
cristya19f1d72012-08-07 18:24:38 +00004196 tone=intensity > (7.0*threshold/6.0) ? (double) QuantumRange :
4197 intensity+(double) QuantumRange-7.0*threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004198 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004199 tone=intensity < (threshold/6.0) ? 0 : intensity-threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004200 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004201 tone=threshold/7.0;
cristya19f1d72012-08-07 18:24:38 +00004202 if ((double) GetPixelGreen(image,q) < tone)
cristy4c08aed2011-07-01 19:47:50 +00004203 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
cristya19f1d72012-08-07 18:24:38 +00004204 if ((double) GetPixelBlue(image,q) < tone)
cristy4c08aed2011-07-01 19:47:50 +00004205 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristyed231572011-07-14 02:18:59 +00004206 p+=GetPixelChannels(image);
4207 q+=GetPixelChannels(sepia_image);
cristy3ed852e2009-09-05 21:47:34 +00004208 }
4209 if (SyncCacheViewAuthenticPixels(sepia_view,exception) == MagickFalse)
4210 status=MagickFalse;
4211 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4212 {
4213 MagickBooleanType
4214 proceed;
4215
cristyb5d5f722009-11-04 03:03:49 +00004216#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00004217 #pragma omp critical (MagickCore_SepiaToneImage)
cristy3ed852e2009-09-05 21:47:34 +00004218#endif
4219 proceed=SetImageProgress(image,SepiaToneImageTag,progress++,
4220 image->rows);
4221 if (proceed == MagickFalse)
4222 status=MagickFalse;
4223 }
4224 }
4225 sepia_view=DestroyCacheView(sepia_view);
4226 image_view=DestroyCacheView(image_view);
cristye23ec9d2011-08-16 18:15:40 +00004227 (void) NormalizeImage(sepia_image,exception);
4228 (void) ContrastImage(sepia_image,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004229 if (status == MagickFalse)
4230 sepia_image=DestroyImage(sepia_image);
4231 return(sepia_image);
4232}
4233
4234/*
4235%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4236% %
4237% %
4238% %
4239% S h a d o w I m a g e %
4240% %
4241% %
4242% %
4243%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4244%
4245% ShadowImage() simulates a shadow from the specified image and returns it.
4246%
4247% The format of the ShadowImage method is:
4248%
cristy70cddf72011-12-10 22:42:42 +00004249% Image *ShadowImage(const Image *image,const double alpha,
cristyaa2c16c2012-03-25 22:21:35 +00004250% const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4251% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004252%
4253% A description of each parameter follows:
4254%
4255% o image: the image.
4256%
cristy70cddf72011-12-10 22:42:42 +00004257% o alpha: percentage transparency.
cristy3ed852e2009-09-05 21:47:34 +00004258%
4259% o sigma: the standard deviation of the Gaussian, in pixels.
4260%
4261% o x_offset: the shadow x-offset.
4262%
4263% o y_offset: the shadow y-offset.
4264%
4265% o exception: return any errors or warnings in this structure.
4266%
4267*/
cristy70cddf72011-12-10 22:42:42 +00004268MagickExport Image *ShadowImage(const Image *image,const double alpha,
cristyaa2c16c2012-03-25 22:21:35 +00004269 const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4270 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004271{
4272#define ShadowImageTag "Shadow/Image"
4273
cristy70cddf72011-12-10 22:42:42 +00004274 CacheView
4275 *image_view;
4276
cristybd5a96c2011-08-21 00:04:26 +00004277 ChannelType
4278 channel_mask;
4279
cristy3ed852e2009-09-05 21:47:34 +00004280 Image
4281 *border_image,
4282 *clone_image,
4283 *shadow_image;
4284
cristy70cddf72011-12-10 22:42:42 +00004285 MagickBooleanType
4286 status;
4287
cristy3ed852e2009-09-05 21:47:34 +00004288 RectangleInfo
4289 border_info;
4290
cristy70cddf72011-12-10 22:42:42 +00004291 ssize_t
4292 y;
4293
cristy3ed852e2009-09-05 21:47:34 +00004294 assert(image != (Image *) NULL);
4295 assert(image->signature == MagickSignature);
4296 if (image->debug != MagickFalse)
4297 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4298 assert(exception != (ExceptionInfo *) NULL);
4299 assert(exception->signature == MagickSignature);
4300 clone_image=CloneImage(image,0,0,MagickTrue,exception);
4301 if (clone_image == (Image *) NULL)
4302 return((Image *) NULL);
cristy0898eba2012-04-09 16:38:29 +00004303 if (IsGrayColorspace(image->colorspace) != MagickFalse)
cristyb09db112012-07-11 12:04:31 +00004304 (void) TransformImageColorspace(clone_image,RGBColorspace,exception);
cristy0ce08762012-06-30 01:33:18 +00004305 (void) SetImageVirtualPixelMethod(clone_image,TransparentVirtualPixelMethod,
cristy387430f2012-02-07 13:09:46 +00004306 exception);
cristybb503372010-05-27 20:51:26 +00004307 border_info.width=(size_t) floor(2.0*sigma+0.5);
4308 border_info.height=(size_t) floor(2.0*sigma+0.5);
cristy3ed852e2009-09-05 21:47:34 +00004309 border_info.x=0;
4310 border_info.y=0;
cristy9950d572011-10-01 18:22:35 +00004311 (void) QueryColorCompliance("none",AllCompliance,&clone_image->border_color,
4312 exception);
cristy8a46d822012-08-28 23:32:39 +00004313 clone_image->alpha_trait=BlendPixelTrait;
cristy70cddf72011-12-10 22:42:42 +00004314 border_image=BorderImage(clone_image,&border_info,OverCompositeOp,exception);
cristy3ed852e2009-09-05 21:47:34 +00004315 clone_image=DestroyImage(clone_image);
4316 if (border_image == (Image *) NULL)
4317 return((Image *) NULL);
cristy8a46d822012-08-28 23:32:39 +00004318 if (border_image->alpha_trait != BlendPixelTrait)
cristy63240882011-08-05 19:05:27 +00004319 (void) SetImageAlphaChannel(border_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004320 /*
4321 Shadow image.
4322 */
cristy70cddf72011-12-10 22:42:42 +00004323 status=MagickTrue;
cristydb070952012-04-20 14:33:00 +00004324 image_view=AcquireAuthenticCacheView(border_image,exception);
cristy70cddf72011-12-10 22:42:42 +00004325 for (y=0; y < (ssize_t) border_image->rows; y++)
4326 {
4327 PixelInfo
4328 background_color;
4329
4330 register Quantum
4331 *restrict q;
4332
4333 register ssize_t
4334 x;
4335
4336 if (status == MagickFalse)
4337 continue;
4338 q=QueueCacheViewAuthenticPixels(image_view,0,y,border_image->columns,1,
4339 exception);
4340 if (q == (Quantum *) NULL)
4341 {
4342 status=MagickFalse;
4343 continue;
4344 }
4345 background_color=border_image->background_color;
cristy8a46d822012-08-28 23:32:39 +00004346 background_color.alpha_trait=BlendPixelTrait;
cristy70cddf72011-12-10 22:42:42 +00004347 for (x=0; x < (ssize_t) border_image->columns; x++)
4348 {
cristy8a46d822012-08-28 23:32:39 +00004349 if (border_image->alpha_trait == BlendPixelTrait)
cristy70cddf72011-12-10 22:42:42 +00004350 background_color.alpha=GetPixelAlpha(border_image,q)*alpha/100.0;
4351 SetPixelInfoPixel(border_image,&background_color,q);
4352 q+=GetPixelChannels(border_image);
4353 }
4354 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4355 status=MagickFalse;
4356 }
4357 image_view=DestroyCacheView(image_view);
4358 if (status == MagickFalse)
4359 {
4360 border_image=DestroyImage(border_image);
4361 return((Image *) NULL);
4362 }
cristycf1296e2012-08-26 23:40:49 +00004363 channel_mask=SetImageChannelMask(border_image,AlphaChannel);
cristyaa2c16c2012-03-25 22:21:35 +00004364 shadow_image=BlurImage(border_image,0.0,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +00004365 border_image=DestroyImage(border_image);
4366 if (shadow_image == (Image *) NULL)
4367 return((Image *) NULL);
cristycf1296e2012-08-26 23:40:49 +00004368 (void) SetPixelChannelMask(shadow_image,channel_mask);
cristy3ed852e2009-09-05 21:47:34 +00004369 if (shadow_image->page.width == 0)
4370 shadow_image->page.width=shadow_image->columns;
4371 if (shadow_image->page.height == 0)
4372 shadow_image->page.height=shadow_image->rows;
cristybb503372010-05-27 20:51:26 +00004373 shadow_image->page.width+=x_offset-(ssize_t) border_info.width;
4374 shadow_image->page.height+=y_offset-(ssize_t) border_info.height;
4375 shadow_image->page.x+=x_offset-(ssize_t) border_info.width;
4376 shadow_image->page.y+=y_offset-(ssize_t) border_info.height;
cristy3ed852e2009-09-05 21:47:34 +00004377 return(shadow_image);
4378}
4379
4380/*
4381%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4382% %
4383% %
4384% %
4385% S k e t c h I m a g e %
4386% %
4387% %
4388% %
4389%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4390%
4391% SketchImage() simulates a pencil sketch. We convolve the image with a
4392% Gaussian operator of the given radius and standard deviation (sigma). For
4393% reasonable results, radius should be larger than sigma. Use a radius of 0
4394% and SketchImage() selects a suitable radius for you. Angle gives the angle
4395% of the sketch.
4396%
4397% The format of the SketchImage method is:
4398%
4399% Image *SketchImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00004400% const double sigma,const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004401%
4402% A description of each parameter follows:
4403%
4404% o image: the image.
4405%
cristy574cc262011-08-05 01:23:58 +00004406% o radius: the radius of the Gaussian, in pixels, not counting the
4407% center pixel.
cristy3ed852e2009-09-05 21:47:34 +00004408%
4409% o sigma: the standard deviation of the Gaussian, in pixels.
4410%
cristyf7ef0252011-09-09 14:50:06 +00004411% o angle: apply the effect along this angle.
4412%
cristy3ed852e2009-09-05 21:47:34 +00004413% o exception: return any errors or warnings in this structure.
4414%
4415*/
4416MagickExport Image *SketchImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00004417 const double sigma,const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004418{
cristyfa112112010-01-04 17:48:07 +00004419 CacheView
4420 *random_view;
4421
cristy3ed852e2009-09-05 21:47:34 +00004422 Image
4423 *blend_image,
4424 *blur_image,
4425 *dodge_image,
4426 *random_image,
4427 *sketch_image;
4428
cristy3ed852e2009-09-05 21:47:34 +00004429 MagickBooleanType
4430 status;
4431
cristy3ed852e2009-09-05 21:47:34 +00004432 RandomInfo
cristyfa112112010-01-04 17:48:07 +00004433 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00004434
cristy9d314ff2011-03-09 01:30:28 +00004435 ssize_t
4436 y;
4437
cristy57340e02012-05-05 00:53:23 +00004438 unsigned long
4439 key;
4440
cristy3ed852e2009-09-05 21:47:34 +00004441 /*
4442 Sketch image.
4443 */
4444 random_image=CloneImage(image,image->columns << 1,image->rows << 1,
4445 MagickTrue,exception);
4446 if (random_image == (Image *) NULL)
4447 return((Image *) NULL);
4448 status=MagickTrue;
cristy1b784432009-12-19 02:20:40 +00004449 random_info=AcquireRandomInfoThreadSet();
cristy57340e02012-05-05 00:53:23 +00004450 key=GetRandomSecretKey(random_info[0]);
cristydb070952012-04-20 14:33:00 +00004451 random_view=AcquireAuthenticCacheView(random_image,exception);
cristy1b784432009-12-19 02:20:40 +00004452#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy57340e02012-05-05 00:53:23 +00004453 #pragma omp parallel for schedule(static,4) shared(status) \
cristy4ee2b0c2012-05-15 00:30:35 +00004454 dynamic_number_threads(image,image->columns,image->rows,key == ~0UL)
cristy1b784432009-12-19 02:20:40 +00004455#endif
cristybb503372010-05-27 20:51:26 +00004456 for (y=0; y < (ssize_t) random_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004457 {
cristy5c9e6f22010-09-17 17:31:01 +00004458 const int
4459 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00004460
cristybb503372010-05-27 20:51:26 +00004461 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004462 x;
4463
cristy4c08aed2011-07-01 19:47:50 +00004464 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004465 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004466
cristy1b784432009-12-19 02:20:40 +00004467 if (status == MagickFalse)
4468 continue;
cristy3ed852e2009-09-05 21:47:34 +00004469 q=QueueCacheViewAuthenticPixels(random_view,0,y,random_image->columns,1,
4470 exception);
cristyacd2ed22011-08-30 01:44:23 +00004471 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004472 {
4473 status=MagickFalse;
4474 continue;
4475 }
cristybb503372010-05-27 20:51:26 +00004476 for (x=0; x < (ssize_t) random_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004477 {
cristya19f1d72012-08-07 18:24:38 +00004478 double
cristy76f512e2011-09-12 01:26:56 +00004479 value;
4480
4481 register ssize_t
4482 i;
4483
cristy10a6c612012-01-29 21:41:05 +00004484 if (GetPixelMask(random_image,q) != 0)
4485 {
4486 q+=GetPixelChannels(random_image);
4487 continue;
4488 }
cristy76f512e2011-09-12 01:26:56 +00004489 value=GetPseudoRandomValue(random_info[id]);
4490 for (i=0; i < (ssize_t) GetPixelChannels(random_image); i++)
4491 {
cristyabace412011-12-11 15:56:53 +00004492 PixelChannel
4493 channel;
4494
cristy76f512e2011-09-12 01:26:56 +00004495 PixelTrait
4496 traits;
4497
cristycf1296e2012-08-26 23:40:49 +00004498 channel=GetPixelChannelChannel(image,i);
4499 traits=GetPixelChannelTraits(image,channel);
cristy76f512e2011-09-12 01:26:56 +00004500 if (traits == UndefinedPixelTrait)
4501 continue;
4502 q[i]=ClampToQuantum(QuantumRange*value);
4503 }
cristyed231572011-07-14 02:18:59 +00004504 q+=GetPixelChannels(random_image);
cristy3ed852e2009-09-05 21:47:34 +00004505 }
4506 if (SyncCacheViewAuthenticPixels(random_view,exception) == MagickFalse)
4507 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004508 }
4509 random_view=DestroyCacheView(random_view);
cristy1b784432009-12-19 02:20:40 +00004510 random_info=DestroyRandomInfoThreadSet(random_info);
cristy3ed852e2009-09-05 21:47:34 +00004511 if (status == MagickFalse)
4512 {
4513 random_image=DestroyImage(random_image);
4514 return(random_image);
4515 }
cristyaa2c16c2012-03-25 22:21:35 +00004516 blur_image=MotionBlurImage(random_image,radius,sigma,angle,exception);
cristy3ed852e2009-09-05 21:47:34 +00004517 random_image=DestroyImage(random_image);
4518 if (blur_image == (Image *) NULL)
4519 return((Image *) NULL);
cristy6bfd6902011-12-09 01:33:45 +00004520 dodge_image=EdgeImage(blur_image,radius,1.0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004521 blur_image=DestroyImage(blur_image);
4522 if (dodge_image == (Image *) NULL)
4523 return((Image *) NULL);
cristye23ec9d2011-08-16 18:15:40 +00004524 (void) NormalizeImage(dodge_image,exception);
cristyb3e7c6c2011-07-24 01:43:55 +00004525 (void) NegateImage(dodge_image,MagickFalse,exception);
cristye941a752011-10-15 01:52:48 +00004526 (void) TransformImage(&dodge_image,(char *) NULL,"50%",exception);
cristy3ed852e2009-09-05 21:47:34 +00004527 sketch_image=CloneImage(image,0,0,MagickTrue,exception);
4528 if (sketch_image == (Image *) NULL)
4529 {
4530 dodge_image=DestroyImage(dodge_image);
4531 return((Image *) NULL);
4532 }
cristyfeb3e962012-03-29 17:25:55 +00004533 (void) CompositeImage(sketch_image,dodge_image,ColorDodgeCompositeOp,
cristy39172402012-03-30 13:04:39 +00004534 MagickTrue,0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004535 dodge_image=DestroyImage(dodge_image);
4536 blend_image=CloneImage(image,0,0,MagickTrue,exception);
4537 if (blend_image == (Image *) NULL)
4538 {
4539 sketch_image=DestroyImage(sketch_image);
4540 return((Image *) NULL);
4541 }
4542 (void) SetImageArtifact(blend_image,"compose:args","20x80");
cristy39172402012-03-30 13:04:39 +00004543 (void) CompositeImage(sketch_image,blend_image,BlendCompositeOp,MagickTrue,
cristyfeb3e962012-03-29 17:25:55 +00004544 0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004545 blend_image=DestroyImage(blend_image);
4546 return(sketch_image);
4547}
4548
4549/*
4550%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4551% %
4552% %
4553% %
4554% S o l a r i z e I m a g e %
4555% %
4556% %
4557% %
4558%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4559%
4560% SolarizeImage() applies a special effect to the image, similar to the effect
4561% achieved in a photo darkroom by selectively exposing areas of photo
4562% sensitive paper to light. Threshold ranges from 0 to QuantumRange and is a
4563% measure of the extent of the solarization.
4564%
4565% The format of the SolarizeImage method is:
4566%
cristy5cbc0162011-08-29 00:36:28 +00004567% MagickBooleanType SolarizeImage(Image *image,const double threshold,
4568% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004569%
4570% A description of each parameter follows:
4571%
4572% o image: the image.
4573%
4574% o threshold: Define the extent of the solarization.
4575%
cristy5cbc0162011-08-29 00:36:28 +00004576% o exception: return any errors or warnings in this structure.
4577%
cristy3ed852e2009-09-05 21:47:34 +00004578*/
4579MagickExport MagickBooleanType SolarizeImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00004580 const double threshold,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004581{
4582#define SolarizeImageTag "Solarize/Image"
4583
cristyc4c8d132010-01-07 01:58:38 +00004584 CacheView
4585 *image_view;
4586
cristy3ed852e2009-09-05 21:47:34 +00004587 MagickBooleanType
4588 status;
4589
cristybb503372010-05-27 20:51:26 +00004590 MagickOffsetType
4591 progress;
4592
4593 ssize_t
4594 y;
4595
cristy3ed852e2009-09-05 21:47:34 +00004596 assert(image != (Image *) NULL);
4597 assert(image->signature == MagickSignature);
4598 if (image->debug != MagickFalse)
4599 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4600 if (image->storage_class == PseudoClass)
4601 {
cristybb503372010-05-27 20:51:26 +00004602 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004603 i;
4604
4605 /*
4606 Solarize colormap.
4607 */
cristybb503372010-05-27 20:51:26 +00004608 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00004609 {
cristya19f1d72012-08-07 18:24:38 +00004610 if ((double) image->colormap[i].red > threshold)
cristy6e963d82012-06-19 15:23:24 +00004611 image->colormap[i].red=QuantumRange-image->colormap[i].red;
cristya19f1d72012-08-07 18:24:38 +00004612 if ((double) image->colormap[i].green > threshold)
cristy6e963d82012-06-19 15:23:24 +00004613 image->colormap[i].green=QuantumRange-
cristy3ed852e2009-09-05 21:47:34 +00004614 image->colormap[i].green;
cristya19f1d72012-08-07 18:24:38 +00004615 if ((double) image->colormap[i].blue > threshold)
cristy6e963d82012-06-19 15:23:24 +00004616 image->colormap[i].blue=QuantumRange-
cristy3ed852e2009-09-05 21:47:34 +00004617 image->colormap[i].blue;
4618 }
4619 }
4620 /*
4621 Solarize image.
4622 */
4623 status=MagickTrue;
4624 progress=0;
cristydb070952012-04-20 14:33:00 +00004625 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00004626#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00004627 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00004628 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00004629#endif
cristybb503372010-05-27 20:51:26 +00004630 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004631 {
cristybb503372010-05-27 20:51:26 +00004632 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004633 x;
4634
cristy4c08aed2011-07-01 19:47:50 +00004635 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004636 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004637
4638 if (status == MagickFalse)
4639 continue;
cristy5cbc0162011-08-29 00:36:28 +00004640 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00004641 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004642 {
4643 status=MagickFalse;
4644 continue;
4645 }
cristybb503372010-05-27 20:51:26 +00004646 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004647 {
cristy76f512e2011-09-12 01:26:56 +00004648 register ssize_t
4649 i;
4650
cristy10a6c612012-01-29 21:41:05 +00004651 if (GetPixelMask(image,q) != 0)
4652 {
4653 q+=GetPixelChannels(image);
4654 continue;
4655 }
cristy76f512e2011-09-12 01:26:56 +00004656 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4657 {
cristyabace412011-12-11 15:56:53 +00004658 PixelChannel
4659 channel;
4660
cristy76f512e2011-09-12 01:26:56 +00004661 PixelTrait
4662 traits;
4663
cristycf1296e2012-08-26 23:40:49 +00004664 channel=GetPixelChannelChannel(image,i);
4665 traits=GetPixelChannelTraits(image,channel);
cristy76f512e2011-09-12 01:26:56 +00004666 if ((traits == UndefinedPixelTrait) ||
4667 ((traits & CopyPixelTrait) != 0))
4668 continue;
cristya19f1d72012-08-07 18:24:38 +00004669 if ((double) q[i] > threshold)
cristy76f512e2011-09-12 01:26:56 +00004670 q[i]=QuantumRange-q[i];
4671 }
cristyed231572011-07-14 02:18:59 +00004672 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00004673 }
4674 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4675 status=MagickFalse;
4676 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4677 {
4678 MagickBooleanType
4679 proceed;
4680
cristyb5d5f722009-11-04 03:03:49 +00004681#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00004682 #pragma omp critical (MagickCore_SolarizeImage)
cristy3ed852e2009-09-05 21:47:34 +00004683#endif
4684 proceed=SetImageProgress(image,SolarizeImageTag,progress++,image->rows);
4685 if (proceed == MagickFalse)
4686 status=MagickFalse;
4687 }
4688 }
4689 image_view=DestroyCacheView(image_view);
4690 return(status);
4691}
4692
4693/*
4694%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4695% %
4696% %
4697% %
4698% S t e g a n o I m a g e %
4699% %
4700% %
4701% %
4702%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4703%
4704% SteganoImage() hides a digital watermark within the image. Recover
4705% the hidden watermark later to prove that the authenticity of an image.
4706% Offset defines the start position within the image to hide the watermark.
4707%
4708% The format of the SteganoImage method is:
4709%
4710% Image *SteganoImage(const Image *image,Image *watermark,
4711% ExceptionInfo *exception)
4712%
4713% A description of each parameter follows:
4714%
4715% o image: the image.
4716%
4717% o watermark: the watermark image.
4718%
4719% o exception: return any errors or warnings in this structure.
4720%
4721*/
4722MagickExport Image *SteganoImage(const Image *image,const Image *watermark,
4723 ExceptionInfo *exception)
4724{
cristye1bf8ad2010-09-19 17:07:03 +00004725#define GetBit(alpha,i) ((((size_t) (alpha) >> (size_t) (i)) & 0x01) != 0)
cristy4c08aed2011-07-01 19:47:50 +00004726#define SetBit(alpha,i,set) (Quantum) ((set) != 0 ? (size_t) (alpha) \
cristyeaedf062010-05-29 22:36:02 +00004727 | (one << (size_t) (i)) : (size_t) (alpha) & ~(one << (size_t) (i)))
cristy3ed852e2009-09-05 21:47:34 +00004728#define SteganoImageTag "Stegano/Image"
4729
cristyb0d3bb92010-09-22 14:37:58 +00004730 CacheView
4731 *stegano_view,
4732 *watermark_view;
4733
cristy3ed852e2009-09-05 21:47:34 +00004734 Image
4735 *stegano_image;
4736
4737 int
4738 c;
4739
cristy3ed852e2009-09-05 21:47:34 +00004740 MagickBooleanType
4741 status;
4742
cristy101ab702011-10-13 13:06:32 +00004743 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00004744 pixel;
4745
cristy4c08aed2011-07-01 19:47:50 +00004746 register Quantum
cristy3ed852e2009-09-05 21:47:34 +00004747 *q;
4748
cristye1bf8ad2010-09-19 17:07:03 +00004749 register ssize_t
4750 x;
4751
cristybb503372010-05-27 20:51:26 +00004752 size_t
cristyeaedf062010-05-29 22:36:02 +00004753 depth,
4754 one;
cristy3ed852e2009-09-05 21:47:34 +00004755
cristye1bf8ad2010-09-19 17:07:03 +00004756 ssize_t
4757 i,
4758 j,
4759 k,
4760 y;
4761
cristy3ed852e2009-09-05 21:47:34 +00004762 /*
4763 Initialize steganographic image attributes.
4764 */
4765 assert(image != (const Image *) NULL);
4766 assert(image->signature == MagickSignature);
4767 if (image->debug != MagickFalse)
4768 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4769 assert(watermark != (const Image *) NULL);
4770 assert(watermark->signature == MagickSignature);
4771 assert(exception != (ExceptionInfo *) NULL);
4772 assert(exception->signature == MagickSignature);
cristyeaedf062010-05-29 22:36:02 +00004773 one=1UL;
cristy3ed852e2009-09-05 21:47:34 +00004774 stegano_image=CloneImage(image,0,0,MagickTrue,exception);
4775 if (stegano_image == (Image *) NULL)
4776 return((Image *) NULL);
cristyf61b1832012-04-01 01:38:19 +00004777 stegano_image->depth=MAGICKCORE_QUANTUM_DEPTH;
cristy574cc262011-08-05 01:23:58 +00004778 if (SetImageStorageClass(stegano_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004779 {
cristy3ed852e2009-09-05 21:47:34 +00004780 stegano_image=DestroyImage(stegano_image);
4781 return((Image *) NULL);
4782 }
cristy3ed852e2009-09-05 21:47:34 +00004783 /*
4784 Hide watermark in low-order bits of image.
4785 */
4786 c=0;
4787 i=0;
4788 j=0;
4789 depth=stegano_image->depth;
cristyf61b1832012-04-01 01:38:19 +00004790 k=stegano_image->offset;
cristyda16f162011-02-19 23:52:17 +00004791 status=MagickTrue;
cristydb070952012-04-20 14:33:00 +00004792 watermark_view=AcquireVirtualCacheView(watermark,exception);
4793 stegano_view=AcquireAuthenticCacheView(stegano_image,exception);
cristybb503372010-05-27 20:51:26 +00004794 for (i=(ssize_t) depth-1; (i >= 0) && (j < (ssize_t) depth); i--)
cristy3ed852e2009-09-05 21:47:34 +00004795 {
cristybb503372010-05-27 20:51:26 +00004796 for (y=0; (y < (ssize_t) watermark->rows) && (j < (ssize_t) depth); y++)
cristy3ed852e2009-09-05 21:47:34 +00004797 {
cristybb503372010-05-27 20:51:26 +00004798 for (x=0; (x < (ssize_t) watermark->columns) && (j < (ssize_t) depth); x++)
cristy3ed852e2009-09-05 21:47:34 +00004799 {
cristy1707c6c2012-01-18 23:30:54 +00004800 ssize_t
4801 offset;
4802
cristyf05d4942012-03-17 16:26:09 +00004803 (void) GetOneCacheViewVirtualPixelInfo(watermark_view,x,y,&pixel,
cristyda1f9c12011-10-02 21:39:49 +00004804 exception);
cristy1707c6c2012-01-18 23:30:54 +00004805 offset=k/(ssize_t) stegano_image->columns;
4806 if (offset >= (ssize_t) stegano_image->rows)
cristy3ed852e2009-09-05 21:47:34 +00004807 break;
cristyb0d3bb92010-09-22 14:37:58 +00004808 q=GetCacheViewAuthenticPixels(stegano_view,k % (ssize_t)
4809 stegano_image->columns,k/(ssize_t) stegano_image->columns,1,1,
4810 exception);
cristyacd2ed22011-08-30 01:44:23 +00004811 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004812 break;
4813 switch (c)
4814 {
4815 case 0:
4816 {
cristyf61b1832012-04-01 01:38:19 +00004817 SetPixelRed(stegano_image,SetBit(GetPixelRed(stegano_image,q),j,
4818 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004819 break;
4820 }
4821 case 1:
4822 {
cristyf61b1832012-04-01 01:38:19 +00004823 SetPixelGreen(stegano_image,SetBit(GetPixelGreen(stegano_image,q),j,
4824 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004825 break;
4826 }
4827 case 2:
4828 {
cristyf61b1832012-04-01 01:38:19 +00004829 SetPixelBlue(stegano_image,SetBit(GetPixelBlue(stegano_image,q),j,
4830 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004831 break;
4832 }
4833 }
cristyb0d3bb92010-09-22 14:37:58 +00004834 if (SyncCacheViewAuthenticPixels(stegano_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004835 break;
4836 c++;
4837 if (c == 3)
4838 c=0;
4839 k++;
cristybb503372010-05-27 20:51:26 +00004840 if (k == (ssize_t) (stegano_image->columns*stegano_image->columns))
cristy3ed852e2009-09-05 21:47:34 +00004841 k=0;
cristyf61b1832012-04-01 01:38:19 +00004842 if (k == stegano_image->offset)
cristy3ed852e2009-09-05 21:47:34 +00004843 j++;
4844 }
4845 }
cristy8b27a6d2010-02-14 03:31:15 +00004846 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004847 {
cristy8b27a6d2010-02-14 03:31:15 +00004848 MagickBooleanType
4849 proceed;
4850
4851 proceed=SetImageProgress(image,SteganoImageTag,(MagickOffsetType)
4852 (depth-i),depth);
4853 if (proceed == MagickFalse)
4854 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004855 }
4856 }
cristyb0d3bb92010-09-22 14:37:58 +00004857 stegano_view=DestroyCacheView(stegano_view);
4858 watermark_view=DestroyCacheView(watermark_view);
cristyda16f162011-02-19 23:52:17 +00004859 if (status == MagickFalse)
4860 {
4861 stegano_image=DestroyImage(stegano_image);
4862 return((Image *) NULL);
4863 }
cristy3ed852e2009-09-05 21:47:34 +00004864 return(stegano_image);
4865}
4866
4867/*
4868%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4869% %
4870% %
4871% %
4872% S t e r e o A n a g l y p h I m a g e %
4873% %
4874% %
4875% %
4876%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4877%
4878% StereoAnaglyphImage() combines two images and produces a single image that
4879% is the composite of a left and right image of a stereo pair. Special
4880% red-green stereo glasses are required to view this effect.
4881%
4882% The format of the StereoAnaglyphImage method is:
4883%
4884% Image *StereoImage(const Image *left_image,const Image *right_image,
4885% ExceptionInfo *exception)
4886% Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004887% const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004888% ExceptionInfo *exception)
4889%
4890% A description of each parameter follows:
4891%
4892% o left_image: the left image.
4893%
4894% o right_image: the right image.
4895%
4896% o exception: return any errors or warnings in this structure.
4897%
4898% o x_offset: amount, in pixels, by which the left image is offset to the
4899% right of the right image.
4900%
4901% o y_offset: amount, in pixels, by which the left image is offset to the
4902% bottom of the right image.
4903%
4904%
4905*/
4906MagickExport Image *StereoImage(const Image *left_image,
4907 const Image *right_image,ExceptionInfo *exception)
4908{
4909 return(StereoAnaglyphImage(left_image,right_image,0,0,exception));
4910}
4911
4912MagickExport Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004913 const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004914 ExceptionInfo *exception)
4915{
4916#define StereoImageTag "Stereo/Image"
4917
4918 const Image
4919 *image;
4920
4921 Image
4922 *stereo_image;
4923
cristy3ed852e2009-09-05 21:47:34 +00004924 MagickBooleanType
4925 status;
4926
cristy9d314ff2011-03-09 01:30:28 +00004927 ssize_t
4928 y;
4929
cristy3ed852e2009-09-05 21:47:34 +00004930 assert(left_image != (const Image *) NULL);
4931 assert(left_image->signature == MagickSignature);
4932 if (left_image->debug != MagickFalse)
4933 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4934 left_image->filename);
4935 assert(right_image != (const Image *) NULL);
4936 assert(right_image->signature == MagickSignature);
4937 assert(exception != (ExceptionInfo *) NULL);
4938 assert(exception->signature == MagickSignature);
4939 assert(right_image != (const Image *) NULL);
4940 image=left_image;
4941 if ((left_image->columns != right_image->columns) ||
4942 (left_image->rows != right_image->rows))
4943 ThrowImageException(ImageError,"LeftAndRightImageSizesDiffer");
4944 /*
4945 Initialize stereo image attributes.
4946 */
4947 stereo_image=CloneImage(left_image,left_image->columns,left_image->rows,
4948 MagickTrue,exception);
4949 if (stereo_image == (Image *) NULL)
4950 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004951 if (SetImageStorageClass(stereo_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004952 {
cristy3ed852e2009-09-05 21:47:34 +00004953 stereo_image=DestroyImage(stereo_image);
4954 return((Image *) NULL);
4955 }
cristy079c78d2012-07-03 11:53:48 +00004956 (void) SetImageColorspace(stereo_image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00004957 /*
4958 Copy left image to red channel and right image to blue channel.
4959 */
cristyda16f162011-02-19 23:52:17 +00004960 status=MagickTrue;
cristybb503372010-05-27 20:51:26 +00004961 for (y=0; y < (ssize_t) stereo_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004962 {
cristy4c08aed2011-07-01 19:47:50 +00004963 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004964 *restrict p,
4965 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004966
cristybb503372010-05-27 20:51:26 +00004967 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004968 x;
4969
cristy4c08aed2011-07-01 19:47:50 +00004970 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004971 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +00004972
4973 p=GetVirtualPixels(left_image,-x_offset,y-y_offset,image->columns,1,
4974 exception);
4975 q=GetVirtualPixels(right_image,0,y,right_image->columns,1,exception);
4976 r=QueueAuthenticPixels(stereo_image,0,y,stereo_image->columns,1,exception);
cristy76f512e2011-09-12 01:26:56 +00004977 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL) ||
4978 (r == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004979 break;
cristybb503372010-05-27 20:51:26 +00004980 for (x=0; x < (ssize_t) stereo_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004981 {
cristy4c08aed2011-07-01 19:47:50 +00004982 SetPixelRed(image,GetPixelRed(left_image,p),r);
cristy76f512e2011-09-12 01:26:56 +00004983 SetPixelGreen(image,GetPixelGreen(right_image,q),r);
4984 SetPixelBlue(image,GetPixelBlue(right_image,q),r);
4985 if ((GetPixelAlphaTraits(stereo_image) & CopyPixelTrait) != 0)
4986 SetPixelAlpha(image,(GetPixelAlpha(left_image,p)+
4987 GetPixelAlpha(right_image,q))/2,r);
cristyed231572011-07-14 02:18:59 +00004988 p+=GetPixelChannels(left_image);
cristy76f512e2011-09-12 01:26:56 +00004989 q+=GetPixelChannels(right_image);
4990 r+=GetPixelChannels(stereo_image);
cristy3ed852e2009-09-05 21:47:34 +00004991 }
4992 if (SyncAuthenticPixels(stereo_image,exception) == MagickFalse)
4993 break;
cristy8b27a6d2010-02-14 03:31:15 +00004994 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004995 {
cristy8b27a6d2010-02-14 03:31:15 +00004996 MagickBooleanType
4997 proceed;
4998
cristybb503372010-05-27 20:51:26 +00004999 proceed=SetImageProgress(image,StereoImageTag,(MagickOffsetType) y,
5000 stereo_image->rows);
cristy8b27a6d2010-02-14 03:31:15 +00005001 if (proceed == MagickFalse)
5002 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00005003 }
5004 }
cristyda16f162011-02-19 23:52:17 +00005005 if (status == MagickFalse)
5006 {
5007 stereo_image=DestroyImage(stereo_image);
5008 return((Image *) NULL);
5009 }
cristy3ed852e2009-09-05 21:47:34 +00005010 return(stereo_image);
5011}
5012
5013/*
5014%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5015% %
5016% %
5017% %
5018% S w i r l I m a g e %
5019% %
5020% %
5021% %
5022%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5023%
5024% SwirlImage() swirls the pixels about the center of the image, where
5025% degrees indicates the sweep of the arc through which each pixel is moved.
5026% You get a more dramatic effect as the degrees move from 1 to 360.
5027%
5028% The format of the SwirlImage method is:
5029%
5030% Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00005031% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005032%
5033% A description of each parameter follows:
5034%
5035% o image: the image.
5036%
5037% o degrees: Define the tightness of the swirling effect.
5038%
cristy76f512e2011-09-12 01:26:56 +00005039% o method: the pixel interpolation method.
5040%
cristy3ed852e2009-09-05 21:47:34 +00005041% o exception: return any errors or warnings in this structure.
5042%
5043*/
5044MagickExport Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00005045 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005046{
5047#define SwirlImageTag "Swirl/Image"
5048
cristyfa112112010-01-04 17:48:07 +00005049 CacheView
5050 *image_view,
5051 *swirl_view;
5052
cristy3ed852e2009-09-05 21:47:34 +00005053 Image
5054 *swirl_image;
5055
cristy3ed852e2009-09-05 21:47:34 +00005056 MagickBooleanType
5057 status;
5058
cristybb503372010-05-27 20:51:26 +00005059 MagickOffsetType
5060 progress;
5061
cristya19f1d72012-08-07 18:24:38 +00005062 double
cristy3ed852e2009-09-05 21:47:34 +00005063 radius;
5064
5065 PointInfo
5066 center,
5067 scale;
5068
cristybb503372010-05-27 20:51:26 +00005069 ssize_t
5070 y;
5071
cristy3ed852e2009-09-05 21:47:34 +00005072 /*
5073 Initialize swirl image attributes.
5074 */
5075 assert(image != (const Image *) NULL);
5076 assert(image->signature == MagickSignature);
5077 if (image->debug != MagickFalse)
5078 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5079 assert(exception != (ExceptionInfo *) NULL);
5080 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00005081 swirl_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005082 if (swirl_image == (Image *) NULL)
5083 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005084 if (SetImageStorageClass(swirl_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005085 {
cristy3ed852e2009-09-05 21:47:34 +00005086 swirl_image=DestroyImage(swirl_image);
5087 return((Image *) NULL);
5088 }
cristy4c08aed2011-07-01 19:47:50 +00005089 if (swirl_image->background_color.alpha != OpaqueAlpha)
cristy8a46d822012-08-28 23:32:39 +00005090 swirl_image->alpha_trait=BlendPixelTrait;
cristy3ed852e2009-09-05 21:47:34 +00005091 /*
5092 Compute scaling factor.
5093 */
5094 center.x=(double) image->columns/2.0;
5095 center.y=(double) image->rows/2.0;
5096 radius=MagickMax(center.x,center.y);
5097 scale.x=1.0;
5098 scale.y=1.0;
5099 if (image->columns > image->rows)
5100 scale.y=(double) image->columns/(double) image->rows;
5101 else
5102 if (image->columns < image->rows)
5103 scale.x=(double) image->rows/(double) image->columns;
5104 degrees=(double) DegreesToRadians(degrees);
5105 /*
5106 Swirl image.
5107 */
5108 status=MagickTrue;
5109 progress=0;
cristydb070952012-04-20 14:33:00 +00005110 image_view=AcquireVirtualCacheView(image,exception);
5111 swirl_view=AcquireAuthenticCacheView(swirl_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00005112#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00005113 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00005114 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00005115#endif
cristybb503372010-05-27 20:51:26 +00005116 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005117 {
cristya19f1d72012-08-07 18:24:38 +00005118 double
cristy3ed852e2009-09-05 21:47:34 +00005119 distance;
5120
5121 PointInfo
5122 delta;
5123
cristy6d188022011-09-12 13:23:33 +00005124 register const Quantum
5125 *restrict p;
5126
cristybb503372010-05-27 20:51:26 +00005127 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005128 x;
5129
cristy4c08aed2011-07-01 19:47:50 +00005130 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005131 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005132
5133 if (status == MagickFalse)
5134 continue;
cristy6d188022011-09-12 13:23:33 +00005135 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy1707c6c2012-01-18 23:30:54 +00005136 q=QueueCacheViewAuthenticPixels(swirl_view,0,y,swirl_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00005137 exception);
cristy6d188022011-09-12 13:23:33 +00005138 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005139 {
5140 status=MagickFalse;
5141 continue;
5142 }
cristy3ed852e2009-09-05 21:47:34 +00005143 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00005144 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005145 {
5146 /*
5147 Determine if the pixel is within an ellipse.
5148 */
cristy10a6c612012-01-29 21:41:05 +00005149 if (GetPixelMask(image,p) != 0)
5150 {
5151 p+=GetPixelChannels(image);
5152 q+=GetPixelChannels(swirl_image);
5153 continue;
5154 }
cristy3ed852e2009-09-05 21:47:34 +00005155 delta.x=scale.x*(double) (x-center.x);
5156 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00005157 if (distance >= (radius*radius))
5158 {
cristy1707c6c2012-01-18 23:30:54 +00005159 register ssize_t
5160 i;
5161
cristy6d188022011-09-12 13:23:33 +00005162 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy1707c6c2012-01-18 23:30:54 +00005163 {
5164 PixelChannel
5165 channel;
5166
5167 PixelTrait
5168 swirl_traits,
5169 traits;
5170
cristycf1296e2012-08-26 23:40:49 +00005171 channel=GetPixelChannelChannel(image,i);
5172 traits=GetPixelChannelTraits(image,channel);
5173 swirl_traits=GetPixelChannelTraits(swirl_image,channel);
cristy1707c6c2012-01-18 23:30:54 +00005174 if ((traits == UndefinedPixelTrait) ||
5175 (swirl_traits == UndefinedPixelTrait))
5176 continue;
5177 SetPixelChannel(swirl_image,channel,p[i],q);
5178 }
cristy6d188022011-09-12 13:23:33 +00005179 }
5180 else
cristy3ed852e2009-09-05 21:47:34 +00005181 {
cristya19f1d72012-08-07 18:24:38 +00005182 double
cristy3ed852e2009-09-05 21:47:34 +00005183 cosine,
5184 factor,
5185 sine;
5186
5187 /*
5188 Swirl the pixel.
5189 */
5190 factor=1.0-sqrt((double) distance)/radius;
5191 sine=sin((double) (degrees*factor*factor));
5192 cosine=cos((double) (degrees*factor*factor));
cristy76f512e2011-09-12 01:26:56 +00005193 status=InterpolatePixelChannels(image,image_view,swirl_image,method,
5194 ((cosine*delta.x-sine*delta.y)/scale.x+center.x),(double)
5195 ((sine*delta.x+cosine*delta.y)/scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00005196 }
cristy6d188022011-09-12 13:23:33 +00005197 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00005198 q+=GetPixelChannels(swirl_image);
cristy3ed852e2009-09-05 21:47:34 +00005199 }
5200 if (SyncCacheViewAuthenticPixels(swirl_view,exception) == MagickFalse)
5201 status=MagickFalse;
5202 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5203 {
5204 MagickBooleanType
5205 proceed;
5206
cristyb5d5f722009-11-04 03:03:49 +00005207#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005208 #pragma omp critical (MagickCore_SwirlImage)
cristy3ed852e2009-09-05 21:47:34 +00005209#endif
5210 proceed=SetImageProgress(image,SwirlImageTag,progress++,image->rows);
5211 if (proceed == MagickFalse)
5212 status=MagickFalse;
5213 }
5214 }
5215 swirl_view=DestroyCacheView(swirl_view);
5216 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00005217 if (status == MagickFalse)
5218 swirl_image=DestroyImage(swirl_image);
5219 return(swirl_image);
5220}
5221
5222/*
5223%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5224% %
5225% %
5226% %
5227% T i n t I m a g e %
5228% %
5229% %
5230% %
5231%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5232%
5233% TintImage() applies a color vector to each pixel in the image. The length
5234% of the vector is 0 for black and white and at its maximum for the midtones.
5235% The vector weighting function is f(x)=(1-(4.0*((x-0.5)*(x-0.5))))
5236%
5237% The format of the TintImage method is:
5238%
cristyb817c3f2011-10-03 14:00:35 +00005239% Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005240% const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005241%
5242% A description of each parameter follows:
5243%
5244% o image: the image.
5245%
cristyb817c3f2011-10-03 14:00:35 +00005246% o blend: A color value used for tinting.
cristy3ed852e2009-09-05 21:47:34 +00005247%
5248% o tint: A color value used for tinting.
5249%
5250% o exception: return any errors or warnings in this structure.
5251%
5252*/
cristyb817c3f2011-10-03 14:00:35 +00005253MagickExport Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005254 const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005255{
5256#define TintImageTag "Tint/Image"
5257
cristyc4c8d132010-01-07 01:58:38 +00005258 CacheView
5259 *image_view,
5260 *tint_view;
5261
cristy3ed852e2009-09-05 21:47:34 +00005262 GeometryInfo
5263 geometry_info;
5264
5265 Image
5266 *tint_image;
5267
cristy3ed852e2009-09-05 21:47:34 +00005268 MagickBooleanType
5269 status;
5270
cristybb503372010-05-27 20:51:26 +00005271 MagickOffsetType
5272 progress;
cristy3ed852e2009-09-05 21:47:34 +00005273
cristya19f1d72012-08-07 18:24:38 +00005274 double
cristy28474bf2011-09-11 23:32:52 +00005275 intensity;
5276
cristy4c08aed2011-07-01 19:47:50 +00005277 PixelInfo
cristy1707c6c2012-01-18 23:30:54 +00005278 color_vector;
cristy3ed852e2009-09-05 21:47:34 +00005279
cristybb503372010-05-27 20:51:26 +00005280 MagickStatusType
5281 flags;
5282
5283 ssize_t
5284 y;
5285
cristy3ed852e2009-09-05 21:47:34 +00005286 /*
5287 Allocate tint image.
5288 */
5289 assert(image != (const Image *) NULL);
5290 assert(image->signature == MagickSignature);
5291 if (image->debug != MagickFalse)
5292 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5293 assert(exception != (ExceptionInfo *) NULL);
5294 assert(exception->signature == MagickSignature);
5295 tint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5296 if (tint_image == (Image *) NULL)
5297 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005298 if (SetImageStorageClass(tint_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005299 {
cristy3ed852e2009-09-05 21:47:34 +00005300 tint_image=DestroyImage(tint_image);
5301 return((Image *) NULL);
5302 }
cristy71aac542012-05-18 12:06:35 +00005303 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
5304 (IsPixelInfoGray(tint) == MagickFalse))
cristyb09db112012-07-11 12:04:31 +00005305 (void) SetImageColorspace(tint_image,RGBColorspace,exception);
cristyaed9c382011-10-03 17:54:21 +00005306 if (blend == (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005307 return(tint_image);
5308 /*
5309 Determine RGB values of the color.
5310 */
cristy1707c6c2012-01-18 23:30:54 +00005311 GetPixelInfo(image,&color_vector);
cristyb817c3f2011-10-03 14:00:35 +00005312 flags=ParseGeometry(blend,&geometry_info);
cristy1707c6c2012-01-18 23:30:54 +00005313 color_vector.red=geometry_info.rho;
5314 color_vector.green=geometry_info.rho;
5315 color_vector.blue=geometry_info.rho;
5316 color_vector.alpha=OpaqueAlpha;
cristy3ed852e2009-09-05 21:47:34 +00005317 if ((flags & SigmaValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005318 color_vector.green=geometry_info.sigma;
cristy3ed852e2009-09-05 21:47:34 +00005319 if ((flags & XiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005320 color_vector.blue=geometry_info.xi;
cristyb817c3f2011-10-03 14:00:35 +00005321 if ((flags & PsiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005322 color_vector.alpha=geometry_info.psi;
cristy76f512e2011-09-12 01:26:56 +00005323 if (image->colorspace == CMYKColorspace)
5324 {
cristy1707c6c2012-01-18 23:30:54 +00005325 color_vector.black=geometry_info.rho;
cristy76f512e2011-09-12 01:26:56 +00005326 if ((flags & PsiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005327 color_vector.black=geometry_info.psi;
cristy76f512e2011-09-12 01:26:56 +00005328 if ((flags & ChiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005329 color_vector.alpha=geometry_info.chi;
cristy76f512e2011-09-12 01:26:56 +00005330 }
cristya19f1d72012-08-07 18:24:38 +00005331 intensity=(double) GetPixelInfoIntensity(tint);
5332 color_vector.red=(double) (color_vector.red*tint->red/100.0-
cristy1707c6c2012-01-18 23:30:54 +00005333 intensity);
cristya19f1d72012-08-07 18:24:38 +00005334 color_vector.green=(double) (color_vector.green*tint->green/100.0-
cristy1707c6c2012-01-18 23:30:54 +00005335 intensity);
cristya19f1d72012-08-07 18:24:38 +00005336 color_vector.blue=(double) (color_vector.blue*tint->blue/100.0-
cristy1707c6c2012-01-18 23:30:54 +00005337 intensity);
cristya19f1d72012-08-07 18:24:38 +00005338 color_vector.black=(double) (color_vector.black*tint->black/100.0-
cristy1707c6c2012-01-18 23:30:54 +00005339 intensity);
cristya19f1d72012-08-07 18:24:38 +00005340 color_vector.alpha=(double) (color_vector.alpha*tint->alpha/100.0-
cristy1707c6c2012-01-18 23:30:54 +00005341 intensity);
cristy3ed852e2009-09-05 21:47:34 +00005342 /*
5343 Tint image.
5344 */
5345 status=MagickTrue;
5346 progress=0;
cristydb070952012-04-20 14:33:00 +00005347 image_view=AcquireVirtualCacheView(image,exception);
5348 tint_view=AcquireAuthenticCacheView(tint_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00005349#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00005350 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00005351 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00005352#endif
cristybb503372010-05-27 20:51:26 +00005353 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005354 {
cristy4c08aed2011-07-01 19:47:50 +00005355 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00005356 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00005357
cristy4c08aed2011-07-01 19:47:50 +00005358 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005359 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005360
cristy6b91acb2011-04-19 12:23:54 +00005361 register ssize_t
5362 x;
5363
cristy3ed852e2009-09-05 21:47:34 +00005364 if (status == MagickFalse)
5365 continue;
5366 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5367 q=QueueCacheViewAuthenticPixels(tint_view,0,y,tint_image->columns,1,
5368 exception);
cristy4c08aed2011-07-01 19:47:50 +00005369 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005370 {
5371 status=MagickFalse;
5372 continue;
5373 }
cristybb503372010-05-27 20:51:26 +00005374 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005375 {
cristy4c08aed2011-07-01 19:47:50 +00005376 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00005377 pixel;
5378
cristya19f1d72012-08-07 18:24:38 +00005379 double
cristy3ed852e2009-09-05 21:47:34 +00005380 weight;
5381
cristy1707c6c2012-01-18 23:30:54 +00005382 register ssize_t
5383 i;
5384
5385 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5386 {
5387 PixelChannel
5388 channel;
5389
5390 PixelTrait
5391 tint_traits,
5392 traits;
5393
cristycf1296e2012-08-26 23:40:49 +00005394 channel=GetPixelChannelChannel(image,i);
5395 traits=GetPixelChannelTraits(image,channel);
5396 tint_traits=GetPixelChannelTraits(tint_image,channel);
cristy1707c6c2012-01-18 23:30:54 +00005397 if ((traits == UndefinedPixelTrait) ||
5398 (tint_traits == UndefinedPixelTrait))
5399 continue;
cristy1eced092012-08-10 23:10:56 +00005400 if (((tint_traits & CopyPixelTrait) != 0) ||
5401 (GetPixelMask(image,p) != 0))
cristy1707c6c2012-01-18 23:30:54 +00005402 {
5403 SetPixelChannel(tint_image,channel,p[i],q);
5404 continue;
5405 }
5406 }
5407 GetPixelInfo(image,&pixel);
5408 weight=QuantumScale*GetPixelRed(image,p)-0.5;
cristy1eced092012-08-10 23:10:56 +00005409 pixel.red=(double) GetPixelRed(image,p)+color_vector.red*(1.0-(4.0*
5410 (weight*weight)));
cristy1707c6c2012-01-18 23:30:54 +00005411 weight=QuantumScale*GetPixelGreen(image,p)-0.5;
cristy1eced092012-08-10 23:10:56 +00005412 pixel.green=(double) GetPixelGreen(image,p)+color_vector.green*(1.0-(4.0*
5413 (weight*weight)));
cristy1707c6c2012-01-18 23:30:54 +00005414 weight=QuantumScale*GetPixelBlue(image,p)-0.5;
cristy1eced092012-08-10 23:10:56 +00005415 pixel.blue=(double) GetPixelBlue(image,p)+color_vector.blue*(1.0-(4.0*
5416 (weight*weight)));
cristy1707c6c2012-01-18 23:30:54 +00005417 weight=QuantumScale*GetPixelBlack(image,p)-0.5;
cristy1eced092012-08-10 23:10:56 +00005418 pixel.black=(double) GetPixelBlack(image,p)+color_vector.black*(1.0-(4.0*
5419 (weight*weight)));
cristy1707c6c2012-01-18 23:30:54 +00005420 SetPixelInfoPixel(tint_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +00005421 p+=GetPixelChannels(image);
5422 q+=GetPixelChannels(tint_image);
cristy3ed852e2009-09-05 21:47:34 +00005423 }
5424 if (SyncCacheViewAuthenticPixels(tint_view,exception) == MagickFalse)
5425 status=MagickFalse;
5426 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5427 {
5428 MagickBooleanType
5429 proceed;
5430
cristyb5d5f722009-11-04 03:03:49 +00005431#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005432 #pragma omp critical (MagickCore_TintImage)
cristy3ed852e2009-09-05 21:47:34 +00005433#endif
5434 proceed=SetImageProgress(image,TintImageTag,progress++,image->rows);
5435 if (proceed == MagickFalse)
5436 status=MagickFalse;
5437 }
5438 }
5439 tint_view=DestroyCacheView(tint_view);
5440 image_view=DestroyCacheView(image_view);
5441 if (status == MagickFalse)
5442 tint_image=DestroyImage(tint_image);
5443 return(tint_image);
5444}
5445
5446/*
5447%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5448% %
5449% %
5450% %
5451% V i g n e t t e I m a g e %
5452% %
5453% %
5454% %
5455%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5456%
5457% VignetteImage() softens the edges of the image in vignette style.
5458%
5459% The format of the VignetteImage method is:
5460%
5461% Image *VignetteImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00005462% const double sigma,const ssize_t x,const ssize_t y,
cristy05c0c9a2011-09-05 23:16:13 +00005463% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005464%
5465% A description of each parameter follows:
5466%
5467% o image: the image.
5468%
5469% o radius: the radius of the pixel neighborhood.
5470%
5471% o sigma: the standard deviation of the Gaussian, in pixels.
5472%
5473% o x, y: Define the x and y ellipse offset.
5474%
5475% o exception: return any errors or warnings in this structure.
5476%
5477*/
5478MagickExport Image *VignetteImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00005479 const double sigma,const ssize_t x,const ssize_t y,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005480{
5481 char
5482 ellipse[MaxTextExtent];
5483
5484 DrawInfo
5485 *draw_info;
5486
5487 Image
5488 *canvas_image,
5489 *blur_image,
5490 *oval_image,
5491 *vignette_image;
5492
5493 assert(image != (Image *) NULL);
5494 assert(image->signature == MagickSignature);
5495 if (image->debug != MagickFalse)
5496 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5497 assert(exception != (ExceptionInfo *) NULL);
5498 assert(exception->signature == MagickSignature);
5499 canvas_image=CloneImage(image,0,0,MagickTrue,exception);
5500 if (canvas_image == (Image *) NULL)
5501 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005502 if (SetImageStorageClass(canvas_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005503 {
cristy3ed852e2009-09-05 21:47:34 +00005504 canvas_image=DestroyImage(canvas_image);
5505 return((Image *) NULL);
5506 }
cristy8a46d822012-08-28 23:32:39 +00005507 canvas_image->alpha_trait=BlendPixelTrait;
cristy98621462011-12-31 22:31:11 +00005508 oval_image=CloneImage(canvas_image,canvas_image->columns,canvas_image->rows,
5509 MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005510 if (oval_image == (Image *) NULL)
5511 {
5512 canvas_image=DestroyImage(canvas_image);
5513 return((Image *) NULL);
5514 }
cristy9950d572011-10-01 18:22:35 +00005515 (void) QueryColorCompliance("#000000",AllCompliance,
5516 &oval_image->background_color,exception);
cristyea1a8aa2011-10-20 13:24:06 +00005517 (void) SetImageBackgroundColor(oval_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00005518 draw_info=CloneDrawInfo((const ImageInfo *) NULL,(const DrawInfo *) NULL);
cristy9950d572011-10-01 18:22:35 +00005519 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->fill,
5520 exception);
5521 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->stroke,
5522 exception);
cristy1707c6c2012-01-18 23:30:54 +00005523 (void) FormatLocaleString(ellipse,MaxTextExtent,"ellipse %g,%g,%g,%g,"
5524 "0.0,360.0",image->columns/2.0,image->rows/2.0,image->columns/2.0-x,
5525 image->rows/2.0-y);
cristy3ed852e2009-09-05 21:47:34 +00005526 draw_info->primitive=AcquireString(ellipse);
cristy018f07f2011-09-04 21:15:19 +00005527 (void) DrawImage(oval_image,draw_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00005528 draw_info=DestroyDrawInfo(draw_info);
cristyaa2c16c2012-03-25 22:21:35 +00005529 blur_image=BlurImage(oval_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +00005530 oval_image=DestroyImage(oval_image);
5531 if (blur_image == (Image *) NULL)
5532 {
5533 canvas_image=DestroyImage(canvas_image);
5534 return((Image *) NULL);
5535 }
cristy8a46d822012-08-28 23:32:39 +00005536 blur_image->alpha_trait=UndefinedPixelTrait;
cristy39172402012-03-30 13:04:39 +00005537 (void) CompositeImage(canvas_image,blur_image,IntensityCompositeOp,MagickTrue,
5538 0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00005539 blur_image=DestroyImage(blur_image);
5540 vignette_image=MergeImageLayers(canvas_image,FlattenLayer,exception);
5541 canvas_image=DestroyImage(canvas_image);
cristy66d26122012-06-23 21:56:40 +00005542 if (vignette_image != (Image *) NULL)
5543 (void) TransformImageColorspace(vignette_image,image->colorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00005544 return(vignette_image);
5545}
5546
5547/*
5548%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5549% %
5550% %
5551% %
5552% W a v e I m a g e %
5553% %
5554% %
5555% %
5556%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5557%
5558% WaveImage() creates a "ripple" effect in the image by shifting the pixels
cristycee97112010-05-28 00:44:52 +00005559% vertically along a sine wave whose amplitude and wavelength is specified
cristy3ed852e2009-09-05 21:47:34 +00005560% by the given parameters.
5561%
5562% The format of the WaveImage method is:
5563%
5564% Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005565% const double wave_length,const PixelInterpolateMethod method,
5566% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005567%
5568% A description of each parameter follows:
5569%
5570% o image: the image.
5571%
5572% o amplitude, wave_length: Define the amplitude and wave length of the
5573% sine wave.
5574%
cristy5c4e2582011-09-11 19:21:03 +00005575% o interpolate: the pixel interpolation method.
5576%
cristy3ed852e2009-09-05 21:47:34 +00005577% o exception: return any errors or warnings in this structure.
5578%
5579*/
5580MagickExport Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005581 const double wave_length,const PixelInterpolateMethod method,
5582 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005583{
5584#define WaveImageTag "Wave/Image"
5585
cristyfa112112010-01-04 17:48:07 +00005586 CacheView
cristyd76c51e2011-03-26 00:21:26 +00005587 *image_view,
cristyfa112112010-01-04 17:48:07 +00005588 *wave_view;
5589
cristy3ed852e2009-09-05 21:47:34 +00005590 Image
5591 *wave_image;
5592
cristy3ed852e2009-09-05 21:47:34 +00005593 MagickBooleanType
5594 status;
5595
cristybb503372010-05-27 20:51:26 +00005596 MagickOffsetType
5597 progress;
5598
cristya19f1d72012-08-07 18:24:38 +00005599 double
cristy3ed852e2009-09-05 21:47:34 +00005600 *sine_map;
5601
cristybb503372010-05-27 20:51:26 +00005602 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005603 i;
5604
cristybb503372010-05-27 20:51:26 +00005605 ssize_t
5606 y;
5607
cristy3ed852e2009-09-05 21:47:34 +00005608 /*
5609 Initialize wave image attributes.
5610 */
5611 assert(image != (Image *) NULL);
5612 assert(image->signature == MagickSignature);
5613 if (image->debug != MagickFalse)
5614 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5615 assert(exception != (ExceptionInfo *) NULL);
5616 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00005617 wave_image=CloneImage(image,image->columns,(size_t) (image->rows+2.0*
cristy3ed852e2009-09-05 21:47:34 +00005618 fabs(amplitude)),MagickTrue,exception);
5619 if (wave_image == (Image *) NULL)
5620 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005621 if (SetImageStorageClass(wave_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005622 {
cristy3ed852e2009-09-05 21:47:34 +00005623 wave_image=DestroyImage(wave_image);
5624 return((Image *) NULL);
5625 }
cristy4c08aed2011-07-01 19:47:50 +00005626 if (wave_image->background_color.alpha != OpaqueAlpha)
cristy8a46d822012-08-28 23:32:39 +00005627 wave_image->alpha_trait=BlendPixelTrait;
cristy3ed852e2009-09-05 21:47:34 +00005628 /*
5629 Allocate sine map.
5630 */
cristya19f1d72012-08-07 18:24:38 +00005631 sine_map=(double *) AcquireQuantumMemory((size_t) wave_image->columns,
cristy3ed852e2009-09-05 21:47:34 +00005632 sizeof(*sine_map));
cristya19f1d72012-08-07 18:24:38 +00005633 if (sine_map == (double *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005634 {
5635 wave_image=DestroyImage(wave_image);
5636 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
5637 }
cristybb503372010-05-27 20:51:26 +00005638 for (i=0; i < (ssize_t) wave_image->columns; i++)
cristy4205a3c2010-09-12 20:19:59 +00005639 sine_map[i]=fabs(amplitude)+amplitude*sin((double) ((2.0*MagickPI*i)/
5640 wave_length));
cristy3ed852e2009-09-05 21:47:34 +00005641 /*
5642 Wave image.
5643 */
5644 status=MagickTrue;
5645 progress=0;
cristydb070952012-04-20 14:33:00 +00005646 image_view=AcquireVirtualCacheView(image,exception);
5647 wave_view=AcquireAuthenticCacheView(wave_image,exception);
cristyd76c51e2011-03-26 00:21:26 +00005648 (void) SetCacheViewVirtualPixelMethod(image_view,
5649 BackgroundVirtualPixelMethod);
cristyb5d5f722009-11-04 03:03:49 +00005650#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00005651 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00005652 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00005653#endif
cristybb503372010-05-27 20:51:26 +00005654 for (y=0; y < (ssize_t) wave_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005655 {
cristy4c08aed2011-07-01 19:47:50 +00005656 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005657 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005658
cristye97bb922011-04-03 01:36:52 +00005659 register ssize_t
5660 x;
5661
cristy3ed852e2009-09-05 21:47:34 +00005662 if (status == MagickFalse)
5663 continue;
5664 q=QueueCacheViewAuthenticPixels(wave_view,0,y,wave_image->columns,1,
5665 exception);
cristyacd2ed22011-08-30 01:44:23 +00005666 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005667 {
5668 status=MagickFalse;
5669 continue;
5670 }
cristybb503372010-05-27 20:51:26 +00005671 for (x=0; x < (ssize_t) wave_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005672 {
cristy5c4e2582011-09-11 19:21:03 +00005673 status=InterpolatePixelChannels(image,image_view,wave_image,method,
5674 (double) x,(double) (y-sine_map[x]),q,exception);
cristyed231572011-07-14 02:18:59 +00005675 q+=GetPixelChannels(wave_image);
cristy3ed852e2009-09-05 21:47:34 +00005676 }
5677 if (SyncCacheViewAuthenticPixels(wave_view,exception) == MagickFalse)
5678 status=MagickFalse;
5679 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5680 {
5681 MagickBooleanType
5682 proceed;
5683
cristyb5d5f722009-11-04 03:03:49 +00005684#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005685 #pragma omp critical (MagickCore_WaveImage)
cristy3ed852e2009-09-05 21:47:34 +00005686#endif
5687 proceed=SetImageProgress(image,WaveImageTag,progress++,image->rows);
5688 if (proceed == MagickFalse)
5689 status=MagickFalse;
5690 }
5691 }
5692 wave_view=DestroyCacheView(wave_view);
cristyd76c51e2011-03-26 00:21:26 +00005693 image_view=DestroyCacheView(image_view);
cristya19f1d72012-08-07 18:24:38 +00005694 sine_map=(double *) RelinquishMagickMemory(sine_map);
cristy3ed852e2009-09-05 21:47:34 +00005695 if (status == MagickFalse)
5696 wave_image=DestroyImage(wave_image);
5697 return(wave_image);
5698}