blob: b34ce09cc0eea43c6e0b74ea62024a74d1937954 [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"
cristy4c08aed2011-07-01 19:47:50 +000085#include "MagickCore/splay-tree.h"
86#include "MagickCore/statistic.h"
87#include "MagickCore/string_.h"
88#include "MagickCore/string-private.h"
89#include "MagickCore/thread-private.h"
90#include "MagickCore/transform.h"
91#include "MagickCore/utility.h"
cristy3ed852e2009-09-05 21:47:34 +000092
93/*
94 Define declarations.
95*/
96#define LeftShiftOperator 0xf5
97#define RightShiftOperator 0xf6
98#define LessThanEqualOperator 0xf7
99#define GreaterThanEqualOperator 0xf8
100#define EqualOperator 0xf9
101#define NotEqualOperator 0xfa
102#define LogicalAndOperator 0xfb
103#define LogicalOrOperator 0xfc
cristy116af162010-08-13 01:25:47 +0000104#define ExponentialNotation 0xfd
cristy3ed852e2009-09-05 21:47:34 +0000105
106struct _FxInfo
107{
108 const Image
109 *images;
110
cristy3ed852e2009-09-05 21:47:34 +0000111 char
112 *expression;
113
114 FILE
115 *file;
116
117 SplayTreeInfo
118 *colors,
119 *symbols;
120
cristyd76c51e2011-03-26 00:21:26 +0000121 CacheView
122 **view;
cristy3ed852e2009-09-05 21:47:34 +0000123
124 RandomInfo
125 *random_info;
126
127 ExceptionInfo
128 *exception;
129};
130
131/*
132%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
133% %
134% %
135% %
136+ A c q u i r e F x I n f o %
137% %
138% %
139% %
140%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
141%
142% AcquireFxInfo() allocates the FxInfo structure.
143%
144% The format of the AcquireFxInfo method is:
145%
146% FxInfo *AcquireFxInfo(Image *image,const char *expression)
cristy0a9b3722010-10-23 18:45:49 +0000147%
cristy3ed852e2009-09-05 21:47:34 +0000148% A description of each parameter follows:
149%
150% o image: the image.
151%
152% o expression: the expression.
153%
154*/
cristy7832dc22011-09-05 01:21:53 +0000155MagickPrivate FxInfo *AcquireFxInfo(const Image *image,const char *expression)
cristy3ed852e2009-09-05 21:47:34 +0000156{
157 char
158 fx_op[2];
159
cristycb180922011-03-11 14:41:24 +0000160 const Image
161 *next;
162
cristy3ed852e2009-09-05 21:47:34 +0000163 FxInfo
164 *fx_info;
165
cristybb503372010-05-27 20:51:26 +0000166 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000167 i;
168
cristy73bd4a52010-10-05 11:24:23 +0000169 fx_info=(FxInfo *) AcquireMagickMemory(sizeof(*fx_info));
cristy3ed852e2009-09-05 21:47:34 +0000170 if (fx_info == (FxInfo *) NULL)
171 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
172 (void) ResetMagickMemory(fx_info,0,sizeof(*fx_info));
173 fx_info->exception=AcquireExceptionInfo();
anthony7d86e172011-03-23 12:37:06 +0000174 fx_info->images=image;
cristy3ed852e2009-09-05 21:47:34 +0000175 fx_info->colors=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
176 RelinquishMagickMemory);
177 fx_info->symbols=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
178 RelinquishMagickMemory);
cristyd76c51e2011-03-26 00:21:26 +0000179 fx_info->view=(CacheView **) AcquireQuantumMemory(GetImageListLength(
180 fx_info->images),sizeof(*fx_info->view));
181 if (fx_info->view == (CacheView **) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000182 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
cristya2262262011-03-11 02:50:37 +0000183 i=0;
cristy0ea377f2011-03-24 00:54:19 +0000184 next=GetFirstImageInList(fx_info->images);
185 for ( ; next != (Image *) NULL; next=next->next)
cristy3ed852e2009-09-05 21:47:34 +0000186 {
cristyd76c51e2011-03-26 00:21:26 +0000187 fx_info->view[i]=AcquireCacheView(next);
cristya2262262011-03-11 02:50:37 +0000188 i++;
cristy3ed852e2009-09-05 21:47:34 +0000189 }
190 fx_info->random_info=AcquireRandomInfo();
191 fx_info->expression=ConstantString(expression);
192 fx_info->file=stderr;
193 (void) SubstituteString(&fx_info->expression," ",""); /* compact string */
cristy37af0912011-05-23 16:09:42 +0000194 /*
195 Force right-to-left associativity for unary negation.
196 */
197 (void) SubstituteString(&fx_info->expression,"-","-1.0*");
cristy8b8a3ae2010-10-23 18:49:46 +0000198 /*
cristy3ed852e2009-09-05 21:47:34 +0000199 Convert complex to simple operators.
200 */
201 fx_op[1]='\0';
202 *fx_op=(char) LeftShiftOperator;
203 (void) SubstituteString(&fx_info->expression,"<<",fx_op);
204 *fx_op=(char) RightShiftOperator;
205 (void) SubstituteString(&fx_info->expression,">>",fx_op);
206 *fx_op=(char) LessThanEqualOperator;
207 (void) SubstituteString(&fx_info->expression,"<=",fx_op);
208 *fx_op=(char) GreaterThanEqualOperator;
209 (void) SubstituteString(&fx_info->expression,">=",fx_op);
210 *fx_op=(char) EqualOperator;
211 (void) SubstituteString(&fx_info->expression,"==",fx_op);
212 *fx_op=(char) NotEqualOperator;
213 (void) SubstituteString(&fx_info->expression,"!=",fx_op);
214 *fx_op=(char) LogicalAndOperator;
215 (void) SubstituteString(&fx_info->expression,"&&",fx_op);
216 *fx_op=(char) LogicalOrOperator;
217 (void) SubstituteString(&fx_info->expression,"||",fx_op);
cristy116af162010-08-13 01:25:47 +0000218 *fx_op=(char) ExponentialNotation;
219 (void) SubstituteString(&fx_info->expression,"**",fx_op);
cristy3ed852e2009-09-05 21:47:34 +0000220 return(fx_info);
221}
222
223/*
224%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
225% %
226% %
227% %
228% A d d N o i s e I m a g e %
229% %
230% %
231% %
232%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
233%
234% AddNoiseImage() adds random noise to the image.
235%
236% The format of the AddNoiseImage method is:
237%
238% Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
cristy9ed1f812011-10-08 02:00:08 +0000239% const double attenuate,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000240%
241% A description of each parameter follows:
242%
243% o image: the image.
244%
245% o channel: the channel type.
246%
247% o noise_type: The type of noise: Uniform, Gaussian, Multiplicative,
248% Impulse, Laplacian, or Poisson.
249%
cristy9ed1f812011-10-08 02:00:08 +0000250% o attenuate: attenuate the random distribution.
251%
cristy3ed852e2009-09-05 21:47:34 +0000252% o exception: return any errors or warnings in this structure.
253%
254*/
cristy9ed1f812011-10-08 02:00:08 +0000255MagickExport Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
256 const double attenuate,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000257{
258#define AddNoiseImageTag "AddNoise/Image"
259
cristyfa112112010-01-04 17:48:07 +0000260 CacheView
261 *image_view,
262 *noise_view;
263
cristy3ed852e2009-09-05 21:47:34 +0000264 Image
265 *noise_image;
266
cristy3ed852e2009-09-05 21:47:34 +0000267 MagickBooleanType
cristye7452652012-04-14 01:34:21 +0000268 concurrent,
cristy3ed852e2009-09-05 21:47:34 +0000269 status;
270
cristybb503372010-05-27 20:51:26 +0000271 MagickOffsetType
272 progress;
273
cristy3ed852e2009-09-05 21:47:34 +0000274 RandomInfo
cristyfa112112010-01-04 17:48:07 +0000275 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +0000276
cristybb503372010-05-27 20:51:26 +0000277 ssize_t
278 y;
279
cristy3ed852e2009-09-05 21:47:34 +0000280 /*
281 Initialize noise image attributes.
282 */
283 assert(image != (const Image *) NULL);
284 assert(image->signature == MagickSignature);
285 if (image->debug != MagickFalse)
286 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
287 assert(exception != (ExceptionInfo *) NULL);
288 assert(exception->signature == MagickSignature);
cristyb2145892011-10-10 00:55:32 +0000289 noise_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000290 if (noise_image == (Image *) NULL)
291 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000292 if (SetImageStorageClass(noise_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000293 {
cristy3ed852e2009-09-05 21:47:34 +0000294 noise_image=DestroyImage(noise_image);
295 return((Image *) NULL);
296 }
297 /*
298 Add noise in each row.
299 */
cristy3ed852e2009-09-05 21:47:34 +0000300 status=MagickTrue;
301 progress=0;
302 random_info=AcquireRandomInfoThreadSet();
cristye7452652012-04-14 01:34:21 +0000303 concurrent=GetRandomSecretKey(random_info[0]) == ~0UL ? MagickTrue :
304 MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000305 image_view=AcquireCacheView(image);
306 noise_view=AcquireCacheView(noise_image);
cristy319a1e72010-02-21 15:13:11 +0000307#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye7452652012-04-14 01:34:21 +0000308 #pragma omp parallel for schedule(static,4) shared(progress,status) omp_concurrent(concurrent)
cristy3ed852e2009-09-05 21:47:34 +0000309#endif
cristybb503372010-05-27 20:51:26 +0000310 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000311 {
cristy5c9e6f22010-09-17 17:31:01 +0000312 const int
313 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +0000314
cristy3ed852e2009-09-05 21:47:34 +0000315 MagickBooleanType
316 sync;
317
cristy4c08aed2011-07-01 19:47:50 +0000318 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000319 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000320
cristybb503372010-05-27 20:51:26 +0000321 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000322 x;
323
cristy4c08aed2011-07-01 19:47:50 +0000324 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000325 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000326
327 if (status == MagickFalse)
328 continue;
329 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +0000330 q=QueueCacheViewAuthenticPixels(noise_view,0,y,noise_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +0000331 exception);
cristy4c08aed2011-07-01 19:47:50 +0000332 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000333 {
334 status=MagickFalse;
335 continue;
336 }
cristybb503372010-05-27 20:51:26 +0000337 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000338 {
cristy850b3072011-10-08 01:38:05 +0000339 register ssize_t
340 i;
341
342 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
343 {
344 PixelChannel
345 channel;
346
347 PixelTrait
348 noise_traits,
349 traits;
350
cristye2a912b2011-12-05 20:02:07 +0000351 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +0000352 traits=GetPixelChannelMapTraits(image,channel);
cristy850b3072011-10-08 01:38:05 +0000353 noise_traits=GetPixelChannelMapTraits(noise_image,channel);
354 if ((traits == UndefinedPixelTrait) ||
355 (noise_traits == UndefinedPixelTrait))
356 continue;
cristyec9e3a62012-02-01 02:09:32 +0000357 if (((noise_traits & CopyPixelTrait) != 0) ||
358 (GetPixelMask(image,p) != 0))
cristyb2145892011-10-10 00:55:32 +0000359 {
360 SetPixelChannel(noise_image,channel,p[i],q);
361 continue;
362 }
cristy850b3072011-10-08 01:38:05 +0000363 SetPixelChannel(noise_image,channel,ClampToQuantum(
364 GenerateDifferentialNoise(random_info[id],p[i],noise_type,attenuate)),
365 q);
366 }
cristyed231572011-07-14 02:18:59 +0000367 p+=GetPixelChannels(image);
368 q+=GetPixelChannels(noise_image);
cristy3ed852e2009-09-05 21:47:34 +0000369 }
370 sync=SyncCacheViewAuthenticPixels(noise_view,exception);
371 if (sync == MagickFalse)
372 status=MagickFalse;
373 if (image->progress_monitor != (MagickProgressMonitor) NULL)
374 {
375 MagickBooleanType
376 proceed;
377
cristyb5d5f722009-11-04 03:03:49 +0000378#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy319a1e72010-02-21 15:13:11 +0000379 #pragma omp critical (MagickCore_AddNoiseImage)
cristy3ed852e2009-09-05 21:47:34 +0000380#endif
381 proceed=SetImageProgress(image,AddNoiseImageTag,progress++,
382 image->rows);
383 if (proceed == MagickFalse)
384 status=MagickFalse;
385 }
386 }
387 noise_view=DestroyCacheView(noise_view);
388 image_view=DestroyCacheView(image_view);
389 random_info=DestroyRandomInfoThreadSet(random_info);
390 if (status == MagickFalse)
391 noise_image=DestroyImage(noise_image);
392 return(noise_image);
393}
394
395/*
396%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
397% %
398% %
399% %
400% B l u e S h i f t I m a g e %
401% %
402% %
403% %
404%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
405%
406% BlueShiftImage() mutes the colors of the image to simulate a scene at
407% nighttime in the moonlight.
408%
409% The format of the BlueShiftImage method is:
410%
411% Image *BlueShiftImage(const Image *image,const double factor,
412% ExceptionInfo *exception)
413%
414% A description of each parameter follows:
415%
416% o image: the image.
417%
418% o factor: the shift factor.
419%
420% o exception: return any errors or warnings in this structure.
421%
422*/
423MagickExport Image *BlueShiftImage(const Image *image,const double factor,
424 ExceptionInfo *exception)
425{
426#define BlueShiftImageTag "BlueShift/Image"
427
cristyc4c8d132010-01-07 01:58:38 +0000428 CacheView
429 *image_view,
430 *shift_view;
431
cristy3ed852e2009-09-05 21:47:34 +0000432 Image
433 *shift_image;
434
cristy3ed852e2009-09-05 21:47:34 +0000435 MagickBooleanType
436 status;
437
cristybb503372010-05-27 20:51:26 +0000438 MagickOffsetType
439 progress;
440
441 ssize_t
442 y;
443
cristy3ed852e2009-09-05 21:47:34 +0000444 /*
445 Allocate blue shift image.
446 */
447 assert(image != (const Image *) NULL);
448 assert(image->signature == MagickSignature);
449 if (image->debug != MagickFalse)
450 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
451 assert(exception != (ExceptionInfo *) NULL);
452 assert(exception->signature == MagickSignature);
cristya6d7a9b2012-01-18 20:04:48 +0000453 shift_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000454 if (shift_image == (Image *) NULL)
455 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000456 if (SetImageStorageClass(shift_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000457 {
cristy3ed852e2009-09-05 21:47:34 +0000458 shift_image=DestroyImage(shift_image);
459 return((Image *) NULL);
460 }
461 /*
462 Blue-shift DirectClass image.
463 */
464 status=MagickTrue;
465 progress=0;
466 image_view=AcquireCacheView(image);
467 shift_view=AcquireCacheView(shift_image);
cristy319a1e72010-02-21 15:13:11 +0000468#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000469 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000470#endif
cristybb503372010-05-27 20:51:26 +0000471 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000472 {
473 MagickBooleanType
474 sync;
475
cristy4c08aed2011-07-01 19:47:50 +0000476 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000477 pixel;
478
479 Quantum
480 quantum;
481
cristy4c08aed2011-07-01 19:47:50 +0000482 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000483 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000484
cristybb503372010-05-27 20:51:26 +0000485 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000486 x;
487
cristy4c08aed2011-07-01 19:47:50 +0000488 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000489 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000490
491 if (status == MagickFalse)
492 continue;
493 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
494 q=QueueCacheViewAuthenticPixels(shift_view,0,y,shift_image->columns,1,
495 exception);
cristy4c08aed2011-07-01 19:47:50 +0000496 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000497 {
498 status=MagickFalse;
499 continue;
500 }
cristybb503372010-05-27 20:51:26 +0000501 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000502 {
cristy4c08aed2011-07-01 19:47:50 +0000503 quantum=GetPixelRed(image,p);
504 if (GetPixelGreen(image,p) < quantum)
505 quantum=GetPixelGreen(image,p);
506 if (GetPixelBlue(image,p) < quantum)
507 quantum=GetPixelBlue(image,p);
508 pixel.red=0.5*(GetPixelRed(image,p)+factor*quantum);
509 pixel.green=0.5*(GetPixelGreen(image,p)+factor*quantum);
510 pixel.blue=0.5*(GetPixelBlue(image,p)+factor*quantum);
511 quantum=GetPixelRed(image,p);
512 if (GetPixelGreen(image,p) > quantum)
513 quantum=GetPixelGreen(image,p);
514 if (GetPixelBlue(image,p) > quantum)
515 quantum=GetPixelBlue(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000516 pixel.red=0.5*(pixel.red+factor*quantum);
517 pixel.green=0.5*(pixel.green+factor*quantum);
518 pixel.blue=0.5*(pixel.blue+factor*quantum);
cristy4c08aed2011-07-01 19:47:50 +0000519 SetPixelRed(shift_image,ClampToQuantum(pixel.red),q);
520 SetPixelGreen(shift_image,ClampToQuantum(pixel.green),q);
521 SetPixelBlue(shift_image,ClampToQuantum(pixel.blue),q);
cristyed231572011-07-14 02:18:59 +0000522 p+=GetPixelChannels(image);
523 q+=GetPixelChannels(shift_image);
cristy3ed852e2009-09-05 21:47:34 +0000524 }
525 sync=SyncCacheViewAuthenticPixels(shift_view,exception);
526 if (sync == MagickFalse)
527 status=MagickFalse;
528 if (image->progress_monitor != (MagickProgressMonitor) NULL)
529 {
530 MagickBooleanType
531 proceed;
532
cristy319a1e72010-02-21 15:13:11 +0000533#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +0000534 #pragma omp critical (MagickCore_BlueShiftImage)
cristy3ed852e2009-09-05 21:47:34 +0000535#endif
536 proceed=SetImageProgress(image,BlueShiftImageTag,progress++,
537 image->rows);
538 if (proceed == MagickFalse)
539 status=MagickFalse;
540 }
541 }
542 image_view=DestroyCacheView(image_view);
543 shift_view=DestroyCacheView(shift_view);
544 if (status == MagickFalse)
545 shift_image=DestroyImage(shift_image);
546 return(shift_image);
547}
548
549/*
550%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
551% %
552% %
553% %
554% C h a r c o a l I m a g e %
555% %
556% %
557% %
558%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
559%
560% CharcoalImage() creates a new image that is a copy of an existing one with
561% the edge highlighted. It allocates the memory necessary for the new Image
562% structure and returns a pointer to the new image.
563%
564% The format of the CharcoalImage method is:
565%
566% Image *CharcoalImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +0000567% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000568%
569% A description of each parameter follows:
570%
571% o image: the image.
572%
573% o radius: the radius of the pixel neighborhood.
574%
575% o sigma: the standard deviation of the Gaussian, in pixels.
576%
577% o exception: return any errors or warnings in this structure.
578%
579*/
580MagickExport Image *CharcoalImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +0000581 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000582{
583 Image
584 *charcoal_image,
585 *clone_image,
586 *edge_image;
587
588 assert(image != (Image *) NULL);
589 assert(image->signature == MagickSignature);
590 if (image->debug != MagickFalse)
591 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
592 assert(exception != (ExceptionInfo *) NULL);
593 assert(exception->signature == MagickSignature);
594 clone_image=CloneImage(image,0,0,MagickTrue,exception);
595 if (clone_image == (Image *) NULL)
596 return((Image *) NULL);
cristy018f07f2011-09-04 21:15:19 +0000597 (void) SetImageType(clone_image,GrayscaleType,exception);
cristy8ae632d2011-09-05 17:29:53 +0000598 edge_image=EdgeImage(clone_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +0000599 clone_image=DestroyImage(clone_image);
600 if (edge_image == (Image *) NULL)
601 return((Image *) NULL);
cristyaa2c16c2012-03-25 22:21:35 +0000602 charcoal_image=BlurImage(edge_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +0000603 edge_image=DestroyImage(edge_image);
604 if (charcoal_image == (Image *) NULL)
605 return((Image *) NULL);
cristye23ec9d2011-08-16 18:15:40 +0000606 (void) NormalizeImage(charcoal_image,exception);
cristyb3e7c6c2011-07-24 01:43:55 +0000607 (void) NegateImage(charcoal_image,MagickFalse,exception);
cristy018f07f2011-09-04 21:15:19 +0000608 (void) SetImageType(charcoal_image,GrayscaleType,exception);
cristy3ed852e2009-09-05 21:47:34 +0000609 return(charcoal_image);
610}
611
612/*
613%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
614% %
615% %
616% %
617% C o l o r i z e I m a g e %
618% %
619% %
620% %
621%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
622%
623% ColorizeImage() blends the fill color with each pixel in the image.
624% A percentage blend is specified with opacity. Control the application
625% of different color components by specifying a different percentage for
626% each component (e.g. 90/100/10 is 90% red, 100% green, and 10% blue).
627%
628% The format of the ColorizeImage method is:
629%
cristyc7e6ff62011-10-03 13:46:11 +0000630% Image *ColorizeImage(const Image *image,const char *blend,
631% const PixelInfo *colorize,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000632%
633% A description of each parameter follows:
634%
635% o image: the image.
636%
cristyc7e6ff62011-10-03 13:46:11 +0000637% o blend: A character string indicating the level of blending as a
cristy3ed852e2009-09-05 21:47:34 +0000638% percentage.
639%
640% o colorize: A color value.
641%
642% o exception: return any errors or warnings in this structure.
643%
644*/
cristyc7e6ff62011-10-03 13:46:11 +0000645MagickExport Image *ColorizeImage(const Image *image,const char *blend,
646 const PixelInfo *colorize,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000647{
648#define ColorizeImageTag "Colorize/Image"
cristy20c3aed2012-04-10 01:06:21 +0000649#define Colorize(pixel,blend_percentage,colorize) \
650 (pixel)=((pixel)*(100.0-(blend_percentage))+(colorize)*(blend_percentage))/100.0;
cristy3ed852e2009-09-05 21:47:34 +0000651
cristyc4c8d132010-01-07 01:58:38 +0000652 CacheView
653 *colorize_view,
654 *image_view;
655
cristy3ed852e2009-09-05 21:47:34 +0000656 GeometryInfo
657 geometry_info;
658
659 Image
660 *colorize_image;
661
cristy3ed852e2009-09-05 21:47:34 +0000662 MagickBooleanType
663 status;
664
cristybb503372010-05-27 20:51:26 +0000665 MagickOffsetType
666 progress;
667
cristy3ed852e2009-09-05 21:47:34 +0000668 MagickStatusType
669 flags;
670
cristyc7e6ff62011-10-03 13:46:11 +0000671 PixelInfo
cristy20c3aed2012-04-10 01:06:21 +0000672 blend_percentage;
cristyc7e6ff62011-10-03 13:46:11 +0000673
cristybb503372010-05-27 20:51:26 +0000674 ssize_t
675 y;
676
cristy3ed852e2009-09-05 21:47:34 +0000677 /*
678 Allocate colorized image.
679 */
680 assert(image != (const Image *) NULL);
681 assert(image->signature == MagickSignature);
682 if (image->debug != MagickFalse)
683 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
684 assert(exception != (ExceptionInfo *) NULL);
685 assert(exception->signature == MagickSignature);
cristy768165d2012-04-09 15:01:35 +0000686 colorize_image=CloneImage(image,image->columns,image->rows,MagickTrue,
687 exception);
688 if (colorize_image == (Image *) NULL)
689 return((Image *) NULL);
690 if (SetImageStorageClass(colorize_image,DirectClass,exception) == MagickFalse)
691 {
692 colorize_image=DestroyImage(colorize_image);
693 return((Image *) NULL);
694 }
cristy9b7a4fc2012-04-08 22:26:56 +0000695 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
cristy20c3aed2012-04-10 01:06:21 +0000696 (IsPixelInfoGray(colorize) != MagickFalse))
cristy52f2e1e2012-04-10 00:51:19 +0000697 (void) SetImageColorspace(colorize_image,sRGBColorspace,exception);
cristy20c3aed2012-04-10 01:06:21 +0000698 if ((colorize_image->matte == MagickFalse) &&
699 (colorize->matte != MagickFalse))
cristy768165d2012-04-09 15:01:35 +0000700 (void) SetImageAlpha(colorize_image,OpaqueAlpha,exception);
701 if (blend == (const char *) NULL)
702 return(colorize_image);
cristy20c3aed2012-04-10 01:06:21 +0000703 GetPixelInfo(image,&blend_percentage);
704 flags=ParseGeometry(blend,&geometry_info);
705 blend_percentage.red=geometry_info.rho;
706 blend_percentage.green=geometry_info.rho;
707 blend_percentage.blue=geometry_info.rho;
708 blend_percentage.black=geometry_info.rho;
709 blend_percentage.alpha=100.0;
710 if ((flags & SigmaValue) != 0)
711 blend_percentage.green=geometry_info.sigma;
712 if ((flags & XiValue) != 0)
713 blend_percentage.blue=geometry_info.xi;
714 if ((flags & PsiValue) != 0)
715 blend_percentage.alpha=geometry_info.psi;
716 if (blend_percentage.colorspace == CMYKColorspace)
717 {
718 if ((flags & PsiValue) != 0)
719 blend_percentage.black=geometry_info.psi;
720 if ((flags & ChiValue) != 0)
721 blend_percentage.alpha=geometry_info.chi;
722 }
cristy3ed852e2009-09-05 21:47:34 +0000723 /*
724 Colorize DirectClass image.
725 */
726 status=MagickTrue;
727 progress=0;
728 image_view=AcquireCacheView(image);
729 colorize_view=AcquireCacheView(colorize_image);
cristy319a1e72010-02-21 15:13:11 +0000730#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000731 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000732#endif
cristybb503372010-05-27 20:51:26 +0000733 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000734 {
735 MagickBooleanType
736 sync;
737
cristyf9bf43e2012-04-07 18:13:07 +0000738 PixelInfo
739 pixel;
740
cristy4c08aed2011-07-01 19:47:50 +0000741 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000742 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000743
cristybb503372010-05-27 20:51:26 +0000744 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000745 x;
746
cristy4c08aed2011-07-01 19:47:50 +0000747 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000748 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000749
750 if (status == MagickFalse)
751 continue;
752 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
753 q=QueueCacheViewAuthenticPixels(colorize_view,0,y,colorize_image->columns,1,
754 exception);
cristy4c08aed2011-07-01 19:47:50 +0000755 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000756 {
757 status=MagickFalse;
758 continue;
759 }
cristyf9bf43e2012-04-07 18:13:07 +0000760 GetPixelInfo(image,&pixel);
cristybb503372010-05-27 20:51:26 +0000761 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000762 {
cristyd6803382012-04-10 01:41:25 +0000763 if (GetPixelMask(colorize_image,q) != 0)
764 {
765 p+=GetPixelChannels(image);
766 q+=GetPixelChannels(colorize_image);
767 continue;
768 }
cristyf9bf43e2012-04-07 18:13:07 +0000769 GetPixelInfoPixel(image,p,&pixel);
cristy20c3aed2012-04-10 01:06:21 +0000770 Colorize(pixel.red,blend_percentage.red,colorize->red);
771 Colorize(pixel.green,blend_percentage.green,colorize->green);
772 Colorize(pixel.blue,blend_percentage.blue,colorize->blue);
773 Colorize(pixel.black,blend_percentage.black,colorize->black);
774 Colorize(pixel.alpha,blend_percentage.alpha,colorize->alpha);
cristyf9bf43e2012-04-07 18:13:07 +0000775 SetPixelInfoPixel(colorize_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +0000776 p+=GetPixelChannels(image);
777 q+=GetPixelChannels(colorize_image);
cristy3ed852e2009-09-05 21:47:34 +0000778 }
779 sync=SyncCacheViewAuthenticPixels(colorize_view,exception);
780 if (sync == MagickFalse)
781 status=MagickFalse;
782 if (image->progress_monitor != (MagickProgressMonitor) NULL)
783 {
784 MagickBooleanType
785 proceed;
786
cristy319a1e72010-02-21 15:13:11 +0000787#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +0000788 #pragma omp critical (MagickCore_ColorizeImage)
cristy3ed852e2009-09-05 21:47:34 +0000789#endif
790 proceed=SetImageProgress(image,ColorizeImageTag,progress++,image->rows);
791 if (proceed == MagickFalse)
792 status=MagickFalse;
793 }
794 }
795 image_view=DestroyCacheView(image_view);
796 colorize_view=DestroyCacheView(colorize_view);
797 if (status == MagickFalse)
798 colorize_image=DestroyImage(colorize_image);
799 return(colorize_image);
800}
801
802/*
803%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
804% %
805% %
806% %
cristye6365592010-04-02 17:31:23 +0000807% C o l o r M a t r i x I m a g e %
808% %
809% %
810% %
811%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
812%
813% ColorMatrixImage() applies color transformation to an image. This method
814% permits saturation changes, hue rotation, luminance to alpha, and various
815% other effects. Although variable-sized transformation matrices can be used,
816% typically one uses a 5x5 matrix for an RGBA image and a 6x6 for CMYKA
817% (or RGBA with offsets). The matrix is similar to those used by Adobe Flash
818% except offsets are in column 6 rather than 5 (in support of CMYKA images)
819% and offsets are normalized (divide Flash offset by 255).
820%
821% The format of the ColorMatrixImage method is:
822%
823% Image *ColorMatrixImage(const Image *image,
824% const KernelInfo *color_matrix,ExceptionInfo *exception)
825%
826% A description of each parameter follows:
827%
828% o image: the image.
829%
830% o color_matrix: the color matrix.
831%
832% o exception: return any errors or warnings in this structure.
833%
834*/
anthonyfd706f92012-01-19 04:22:02 +0000835/* FUTURE: modify to make use of a MagickMatrix Mutliply function
836 That should be provided in "matrix.c"
837 (ASIDE: actually distorts should do this too but currently doesn't)
838*/
839
cristye6365592010-04-02 17:31:23 +0000840MagickExport Image *ColorMatrixImage(const Image *image,
841 const KernelInfo *color_matrix,ExceptionInfo *exception)
842{
843#define ColorMatrixImageTag "ColorMatrix/Image"
844
845 CacheView
846 *color_view,
847 *image_view;
848
849 double
850 ColorMatrix[6][6] =
851 {
852 { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
853 { 0.0, 1.0, 0.0, 0.0, 0.0, 0.0 },
854 { 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 },
855 { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0 },
856 { 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 },
857 { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 }
858 };
859
860 Image
861 *color_image;
862
cristye6365592010-04-02 17:31:23 +0000863 MagickBooleanType
864 status;
865
cristybb503372010-05-27 20:51:26 +0000866 MagickOffsetType
867 progress;
868
869 register ssize_t
cristye6365592010-04-02 17:31:23 +0000870 i;
871
cristybb503372010-05-27 20:51:26 +0000872 ssize_t
873 u,
874 v,
875 y;
876
cristye6365592010-04-02 17:31:23 +0000877 /*
anthonyfd706f92012-01-19 04:22:02 +0000878 Map given color_matrix, into a 6x6 matrix RGBKA and a constant
cristye6365592010-04-02 17:31:23 +0000879 */
880 assert(image != (Image *) NULL);
881 assert(image->signature == MagickSignature);
882 if (image->debug != MagickFalse)
883 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
884 assert(exception != (ExceptionInfo *) NULL);
885 assert(exception->signature == MagickSignature);
886 i=0;
cristybb503372010-05-27 20:51:26 +0000887 for (v=0; v < (ssize_t) color_matrix->height; v++)
888 for (u=0; u < (ssize_t) color_matrix->width; u++)
cristye6365592010-04-02 17:31:23 +0000889 {
890 if ((v < 6) && (u < 6))
891 ColorMatrix[v][u]=color_matrix->values[i];
892 i++;
893 }
894 /*
895 Initialize color image.
896 */
cristy12550e62010-06-07 12:46:40 +0000897 color_image=CloneImage(image,0,0,MagickTrue,exception);
cristye6365592010-04-02 17:31:23 +0000898 if (color_image == (Image *) NULL)
899 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000900 if (SetImageStorageClass(color_image,DirectClass,exception) == MagickFalse)
cristye6365592010-04-02 17:31:23 +0000901 {
cristye6365592010-04-02 17:31:23 +0000902 color_image=DestroyImage(color_image);
903 return((Image *) NULL);
904 }
905 if (image->debug != MagickFalse)
906 {
907 char
908 format[MaxTextExtent],
909 *message;
910
911 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
912 " ColorMatrix image with color matrix:");
913 message=AcquireString("");
914 for (v=0; v < 6; v++)
915 {
916 *message='\0';
cristyb51dff52011-05-19 16:55:47 +0000917 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
cristye6365592010-04-02 17:31:23 +0000918 (void) ConcatenateString(&message,format);
919 for (u=0; u < 6; u++)
920 {
cristyb51dff52011-05-19 16:55:47 +0000921 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",
cristye6365592010-04-02 17:31:23 +0000922 ColorMatrix[v][u]);
923 (void) ConcatenateString(&message,format);
924 }
925 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
926 }
927 message=DestroyString(message);
928 }
929 /*
anthonyfd706f92012-01-19 04:22:02 +0000930 Apply the ColorMatrix to image.
cristye6365592010-04-02 17:31:23 +0000931 */
932 status=MagickTrue;
933 progress=0;
934 image_view=AcquireCacheView(image);
935 color_view=AcquireCacheView(color_image);
936#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000937 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristye6365592010-04-02 17:31:23 +0000938#endif
cristybb503372010-05-27 20:51:26 +0000939 for (y=0; y < (ssize_t) image->rows; y++)
cristye6365592010-04-02 17:31:23 +0000940 {
cristyfcc25d92012-02-19 23:06:48 +0000941 PixelInfo
cristye6365592010-04-02 17:31:23 +0000942 pixel;
943
cristy4c08aed2011-07-01 19:47:50 +0000944 register const Quantum
cristye6365592010-04-02 17:31:23 +0000945 *restrict p;
946
cristy4c08aed2011-07-01 19:47:50 +0000947 register Quantum
948 *restrict q;
949
cristybb503372010-05-27 20:51:26 +0000950 register ssize_t
cristye6365592010-04-02 17:31:23 +0000951 x;
952
cristye6365592010-04-02 17:31:23 +0000953 if (status == MagickFalse)
954 continue;
955 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
956 q=GetCacheViewAuthenticPixels(color_view,0,y,color_image->columns,1,
957 exception);
cristy4c08aed2011-07-01 19:47:50 +0000958 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristye6365592010-04-02 17:31:23 +0000959 {
960 status=MagickFalse;
961 continue;
962 }
cristyfcc25d92012-02-19 23:06:48 +0000963 GetPixelInfo(image,&pixel);
cristybb503372010-05-27 20:51:26 +0000964 for (x=0; x < (ssize_t) image->columns; x++)
cristye6365592010-04-02 17:31:23 +0000965 {
cristybb503372010-05-27 20:51:26 +0000966 register ssize_t
cristye6365592010-04-02 17:31:23 +0000967 v;
968
cristybb503372010-05-27 20:51:26 +0000969 size_t
cristye6365592010-04-02 17:31:23 +0000970 height;
971
cristyfcc25d92012-02-19 23:06:48 +0000972 GetPixelInfoPixel(image,p,&pixel);
cristye6365592010-04-02 17:31:23 +0000973 height=color_matrix->height > 6 ? 6UL : color_matrix->height;
cristybb503372010-05-27 20:51:26 +0000974 for (v=0; v < (ssize_t) height; v++)
cristye6365592010-04-02 17:31:23 +0000975 {
cristyfcc25d92012-02-19 23:06:48 +0000976 MagickRealType
977 sum;
978
979 sum=ColorMatrix[v][0]*GetPixelRed(image,p)+ColorMatrix[v][1]*
cristy4c08aed2011-07-01 19:47:50 +0000980 GetPixelGreen(image,p)+ColorMatrix[v][2]*GetPixelBlue(image,p);
cristye6365592010-04-02 17:31:23 +0000981 if (image->colorspace == CMYKColorspace)
cristyfcc25d92012-02-19 23:06:48 +0000982 sum+=ColorMatrix[v][3]*GetPixelBlack(image,p);
cristy4c08aed2011-07-01 19:47:50 +0000983 if (image->matte != MagickFalse)
cristyfcc25d92012-02-19 23:06:48 +0000984 sum+=ColorMatrix[v][4]*GetPixelAlpha(image,p);
985 sum+=QuantumRange*ColorMatrix[v][5];
cristye6365592010-04-02 17:31:23 +0000986 switch (v)
987 {
cristyfcc25d92012-02-19 23:06:48 +0000988 case 0: pixel.red=sum; break;
989 case 1: pixel.green=sum; break;
990 case 2: pixel.blue=sum; break;
991 case 3: pixel.black=sum; break;
992 case 4: pixel.alpha=sum; break;
993 default: break;
cristye6365592010-04-02 17:31:23 +0000994 }
995 }
cristyfcc25d92012-02-19 23:06:48 +0000996 SetPixelInfoPixel(color_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +0000997 p+=GetPixelChannels(image);
998 q+=GetPixelChannels(color_image);
cristye6365592010-04-02 17:31:23 +0000999 }
1000 if (SyncCacheViewAuthenticPixels(color_view,exception) == MagickFalse)
1001 status=MagickFalse;
1002 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1003 {
1004 MagickBooleanType
1005 proceed;
1006
1007#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00001008 #pragma omp critical (MagickCore_ColorMatrixImage)
cristye6365592010-04-02 17:31:23 +00001009#endif
1010 proceed=SetImageProgress(image,ColorMatrixImageTag,progress++,
1011 image->rows);
1012 if (proceed == MagickFalse)
1013 status=MagickFalse;
1014 }
1015 }
1016 color_view=DestroyCacheView(color_view);
1017 image_view=DestroyCacheView(image_view);
1018 if (status == MagickFalse)
1019 color_image=DestroyImage(color_image);
1020 return(color_image);
1021}
1022
1023/*
1024%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1025% %
1026% %
1027% %
cristy3ed852e2009-09-05 21:47:34 +00001028+ D e s t r o y F x I n f o %
1029% %
1030% %
1031% %
1032%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1033%
1034% DestroyFxInfo() deallocates memory associated with an FxInfo structure.
1035%
1036% The format of the DestroyFxInfo method is:
1037%
1038% ImageInfo *DestroyFxInfo(ImageInfo *fx_info)
1039%
1040% A description of each parameter follows:
1041%
1042% o fx_info: the fx info.
1043%
1044*/
cristy7832dc22011-09-05 01:21:53 +00001045MagickPrivate FxInfo *DestroyFxInfo(FxInfo *fx_info)
cristy3ed852e2009-09-05 21:47:34 +00001046{
cristybb503372010-05-27 20:51:26 +00001047 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001048 i;
1049
1050 fx_info->exception=DestroyExceptionInfo(fx_info->exception);
1051 fx_info->expression=DestroyString(fx_info->expression);
1052 fx_info->symbols=DestroySplayTree(fx_info->symbols);
1053 fx_info->colors=DestroySplayTree(fx_info->colors);
cristy0ea377f2011-03-24 00:54:19 +00001054 for (i=(ssize_t) GetImageListLength(fx_info->images)-1; i >= 0; i--)
cristyd76c51e2011-03-26 00:21:26 +00001055 fx_info->view[i]=DestroyCacheView(fx_info->view[i]);
1056 fx_info->view=(CacheView **) RelinquishMagickMemory(fx_info->view);
cristy3ed852e2009-09-05 21:47:34 +00001057 fx_info->random_info=DestroyRandomInfo(fx_info->random_info);
1058 fx_info=(FxInfo *) RelinquishMagickMemory(fx_info);
1059 return(fx_info);
1060}
1061
1062/*
1063%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1064% %
1065% %
1066% %
cristy3ed852e2009-09-05 21:47:34 +00001067+ 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 %
1068% %
1069% %
1070% %
1071%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1072%
1073% FxEvaluateChannelExpression() evaluates an expression and returns the
1074% results.
1075%
1076% The format of the FxEvaluateExpression method is:
1077%
1078% MagickRealType FxEvaluateChannelExpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00001079% const PixelChannel channel,const ssize_t x,const ssize_t y,
cristy3ed852e2009-09-05 21:47:34 +00001080% MagickRealType *alpha,Exceptioninfo *exception)
1081% MagickRealType FxEvaluateExpression(FxInfo *fx_info,
1082% MagickRealType *alpha,Exceptioninfo *exception)
1083%
1084% A description of each parameter follows:
1085%
1086% o fx_info: the fx info.
1087%
1088% o channel: the channel.
1089%
1090% o x,y: the pixel position.
1091%
1092% o alpha: the result.
1093%
1094% o exception: return any errors or warnings in this structure.
1095%
1096*/
1097
cristy351842f2010-03-07 15:27:38 +00001098static inline double MagickMax(const double x,const double y)
1099{
1100 if (x > y)
1101 return(x);
1102 return(y);
1103}
1104
1105static inline double MagickMin(const double x,const double y)
1106{
1107 if (x < y)
1108 return(x);
1109 return(y);
1110}
1111
cristy3ed852e2009-09-05 21:47:34 +00001112static MagickRealType FxChannelStatistics(FxInfo *fx_info,const Image *image,
cristy0568ffc2011-07-25 16:54:14 +00001113 PixelChannel channel,const char *symbol,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001114{
1115 char
1116 key[MaxTextExtent],
1117 statistic[MaxTextExtent];
1118
1119 const char
1120 *value;
1121
1122 register const char
1123 *p;
1124
1125 for (p=symbol; (*p != '.') && (*p != '\0'); p++) ;
1126 if (*p == '.')
1127 switch (*++p) /* e.g. depth.r */
1128 {
cristy541ae572011-08-05 19:08:59 +00001129 case 'r': channel=RedPixelChannel; break;
1130 case 'g': channel=GreenPixelChannel; break;
1131 case 'b': channel=BluePixelChannel; break;
1132 case 'c': channel=CyanPixelChannel; break;
1133 case 'm': channel=MagentaPixelChannel; break;
1134 case 'y': channel=YellowPixelChannel; break;
1135 case 'k': channel=BlackPixelChannel; break;
cristy3ed852e2009-09-05 21:47:34 +00001136 default: break;
1137 }
cristyb51dff52011-05-19 16:55:47 +00001138 (void) FormatLocaleString(key,MaxTextExtent,"%p.%.20g.%s",(void *) image,
cristye8c25f92010-06-03 00:53:06 +00001139 (double) channel,symbol);
cristy3ed852e2009-09-05 21:47:34 +00001140 value=(const char *) GetValueFromSplayTree(fx_info->symbols,key);
1141 if (value != (const char *) NULL)
cristydbdd0e32011-11-04 23:29:40 +00001142 return(QuantumScale*StringToDouble(value,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001143 (void) DeleteNodeFromSplayTree(fx_info->symbols,key);
1144 if (LocaleNCompare(symbol,"depth",5) == 0)
1145 {
cristybb503372010-05-27 20:51:26 +00001146 size_t
cristy3ed852e2009-09-05 21:47:34 +00001147 depth;
1148
cristyfefab1b2011-07-05 00:33:22 +00001149 depth=GetImageDepth(image,exception);
1150 (void) FormatLocaleString(statistic,MaxTextExtent,"%.20g",(double) depth);
cristy3ed852e2009-09-05 21:47:34 +00001151 }
1152 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1153 {
1154 double
1155 kurtosis,
1156 skewness;
1157
cristyd42d9952011-07-08 14:21:50 +00001158 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
cristyb51dff52011-05-19 16:55:47 +00001159 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",kurtosis);
cristy3ed852e2009-09-05 21:47:34 +00001160 }
1161 if (LocaleNCompare(symbol,"maxima",6) == 0)
1162 {
1163 double
1164 maxima,
1165 minima;
1166
cristyd42d9952011-07-08 14:21:50 +00001167 (void) GetImageRange(image,&minima,&maxima,exception);
cristyb51dff52011-05-19 16:55:47 +00001168 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",maxima);
cristy3ed852e2009-09-05 21:47:34 +00001169 }
1170 if (LocaleNCompare(symbol,"mean",4) == 0)
1171 {
1172 double
1173 mean,
1174 standard_deviation;
1175
cristyd42d9952011-07-08 14:21:50 +00001176 (void) GetImageMean(image,&mean,&standard_deviation,exception);
cristyb51dff52011-05-19 16:55:47 +00001177 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",mean);
cristy3ed852e2009-09-05 21:47:34 +00001178 }
1179 if (LocaleNCompare(symbol,"minima",6) == 0)
1180 {
1181 double
1182 maxima,
1183 minima;
1184
cristyd42d9952011-07-08 14:21:50 +00001185 (void) GetImageRange(image,&minima,&maxima,exception);
cristyb51dff52011-05-19 16:55:47 +00001186 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",minima);
cristy3ed852e2009-09-05 21:47:34 +00001187 }
1188 if (LocaleNCompare(symbol,"skewness",8) == 0)
1189 {
1190 double
1191 kurtosis,
1192 skewness;
1193
cristyd42d9952011-07-08 14:21:50 +00001194 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
cristyb51dff52011-05-19 16:55:47 +00001195 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",skewness);
cristy3ed852e2009-09-05 21:47:34 +00001196 }
1197 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1198 {
1199 double
1200 mean,
1201 standard_deviation;
1202
cristyd42d9952011-07-08 14:21:50 +00001203 (void) GetImageMean(image,&mean,&standard_deviation,exception);
cristyb51dff52011-05-19 16:55:47 +00001204 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",
cristy3ed852e2009-09-05 21:47:34 +00001205 standard_deviation);
1206 }
1207 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(key),
1208 ConstantString(statistic));
cristydbdd0e32011-11-04 23:29:40 +00001209 return(QuantumScale*StringToDouble(statistic,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001210}
1211
1212static MagickRealType
cristy0568ffc2011-07-25 16:54:14 +00001213 FxEvaluateSubexpression(FxInfo *,const PixelChannel,const ssize_t,
cristye85007d2010-06-06 22:51:36 +00001214 const ssize_t,const char *,MagickRealType *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +00001215
cristyb0aad4c2011-11-02 19:30:35 +00001216static MagickOffsetType FxGCD(MagickOffsetType alpha,MagickOffsetType beta)
1217{
1218 if (beta != 0)
1219 return(FxGCD(beta,alpha % beta));
1220 return(alpha);
1221}
1222
cristy3ed852e2009-09-05 21:47:34 +00001223static inline const char *FxSubexpression(const char *expression,
1224 ExceptionInfo *exception)
1225{
1226 const char
1227 *subexpression;
1228
cristybb503372010-05-27 20:51:26 +00001229 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001230 level;
1231
1232 level=0;
1233 subexpression=expression;
1234 while ((*subexpression != '\0') &&
1235 ((level != 1) || (strchr(")",(int) *subexpression) == (char *) NULL)))
1236 {
1237 if (strchr("(",(int) *subexpression) != (char *) NULL)
1238 level++;
1239 else
1240 if (strchr(")",(int) *subexpression) != (char *) NULL)
1241 level--;
1242 subexpression++;
1243 }
1244 if (*subexpression == '\0')
1245 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1246 "UnbalancedParenthesis","`%s'",expression);
1247 return(subexpression);
1248}
1249
cristy0568ffc2011-07-25 16:54:14 +00001250static MagickRealType FxGetSymbol(FxInfo *fx_info,const PixelChannel channel,
cristye85007d2010-06-06 22:51:36 +00001251 const ssize_t x,const ssize_t y,const char *expression,
1252 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001253{
1254 char
1255 *q,
1256 subexpression[MaxTextExtent],
1257 symbol[MaxTextExtent];
1258
1259 const char
1260 *p,
1261 *value;
1262
1263 Image
1264 *image;
1265
cristy4c08aed2011-07-01 19:47:50 +00001266 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001267 pixel;
1268
1269 MagickRealType
1270 alpha,
1271 beta;
1272
1273 PointInfo
1274 point;
1275
cristybb503372010-05-27 20:51:26 +00001276 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001277 i;
1278
1279 size_t
cristy1707c6c2012-01-18 23:30:54 +00001280 length,
cristy3ed852e2009-09-05 21:47:34 +00001281 level;
1282
1283 p=expression;
1284 i=GetImageIndexInList(fx_info->images);
1285 level=0;
1286 point.x=(double) x;
1287 point.y=(double) y;
1288 if (isalpha((int) *(p+1)) == 0)
1289 {
1290 if (strchr("suv",(int) *p) != (char *) NULL)
1291 {
1292 switch (*p)
1293 {
1294 case 's':
1295 default:
1296 {
1297 i=GetImageIndexInList(fx_info->images);
1298 break;
1299 }
1300 case 'u': i=0; break;
1301 case 'v': i=1; break;
1302 }
1303 p++;
1304 if (*p == '[')
1305 {
1306 level++;
1307 q=subexpression;
1308 for (p++; *p != '\0'; )
1309 {
1310 if (*p == '[')
1311 level++;
1312 else
1313 if (*p == ']')
1314 {
1315 level--;
1316 if (level == 0)
1317 break;
1318 }
1319 *q++=(*p++);
1320 }
1321 *q='\0';
1322 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1323 &beta,exception);
cristybb503372010-05-27 20:51:26 +00001324 i=(ssize_t) (alpha+0.5);
cristy3ed852e2009-09-05 21:47:34 +00001325 p++;
1326 }
1327 if (*p == '.')
1328 p++;
1329 }
1330 if ((isalpha((int) *(p+1)) == 0) && (*p == 'p'))
1331 {
1332 p++;
1333 if (*p == '{')
1334 {
1335 level++;
1336 q=subexpression;
1337 for (p++; *p != '\0'; )
1338 {
1339 if (*p == '{')
1340 level++;
1341 else
1342 if (*p == '}')
1343 {
1344 level--;
1345 if (level == 0)
1346 break;
1347 }
1348 *q++=(*p++);
1349 }
1350 *q='\0';
1351 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1352 &beta,exception);
1353 point.x=alpha;
1354 point.y=beta;
1355 p++;
1356 }
1357 else
1358 if (*p == '[')
1359 {
1360 level++;
1361 q=subexpression;
1362 for (p++; *p != '\0'; )
1363 {
1364 if (*p == '[')
1365 level++;
1366 else
1367 if (*p == ']')
1368 {
1369 level--;
1370 if (level == 0)
1371 break;
1372 }
1373 *q++=(*p++);
1374 }
1375 *q='\0';
1376 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1377 &beta,exception);
1378 point.x+=alpha;
1379 point.y+=beta;
1380 p++;
1381 }
1382 if (*p == '.')
1383 p++;
1384 }
1385 }
1386 length=GetImageListLength(fx_info->images);
1387 while (i < 0)
cristybb503372010-05-27 20:51:26 +00001388 i+=(ssize_t) length;
cristy3ed852e2009-09-05 21:47:34 +00001389 i%=length;
1390 image=GetImageFromList(fx_info->images,i);
1391 if (image == (Image *) NULL)
1392 {
1393 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1394 "NoSuchImage","`%s'",expression);
1395 return(0.0);
1396 }
cristy4c08aed2011-07-01 19:47:50 +00001397 GetPixelInfo(image,&pixel);
1398 (void) InterpolatePixelInfo(image,fx_info->view[i],image->interpolate,
cristy4f820712011-04-01 12:35:43 +00001399 point.x,point.y,&pixel,exception);
cristy1707c6c2012-01-18 23:30:54 +00001400 if ((strlen(p) > 2) && (LocaleCompare(p,"intensity") != 0) &&
1401 (LocaleCompare(p,"luminance") != 0) && (LocaleCompare(p,"hue") != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00001402 (LocaleCompare(p,"saturation") != 0) &&
1403 (LocaleCompare(p,"lightness") != 0))
1404 {
1405 char
1406 name[MaxTextExtent];
1407
1408 (void) CopyMagickString(name,p,MaxTextExtent);
1409 for (q=name+(strlen(name)-1); q > name; q--)
1410 {
1411 if (*q == ')')
1412 break;
1413 if (*q == '.')
1414 {
1415 *q='\0';
1416 break;
1417 }
1418 }
1419 if ((strlen(name) > 2) &&
1420 (GetValueFromSplayTree(fx_info->symbols,name) == (const char *) NULL))
1421 {
cristy4c08aed2011-07-01 19:47:50 +00001422 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001423 *color;
1424
cristy4c08aed2011-07-01 19:47:50 +00001425 color=(PixelInfo *) GetValueFromSplayTree(fx_info->colors,name);
1426 if (color != (PixelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001427 {
1428 pixel=(*color);
1429 p+=strlen(name);
1430 }
1431 else
cristy1707c6c2012-01-18 23:30:54 +00001432 {
1433 MagickBooleanType
1434 status;
1435
1436 status=QueryColorCompliance(name,AllCompliance,&pixel,
1437 fx_info->exception);
1438 if (status != MagickFalse)
1439 {
1440 (void) AddValueToSplayTree(fx_info->colors,ConstantString(
1441 name),ClonePixelInfo(&pixel));
1442 p+=strlen(name);
1443 }
1444 }
cristy3ed852e2009-09-05 21:47:34 +00001445 }
1446 }
1447 (void) CopyMagickString(symbol,p,MaxTextExtent);
1448 StripString(symbol);
1449 if (*symbol == '\0')
1450 {
1451 switch (channel)
1452 {
cristy0568ffc2011-07-25 16:54:14 +00001453 case RedPixelChannel: return(QuantumScale*pixel.red);
1454 case GreenPixelChannel: return(QuantumScale*pixel.green);
1455 case BluePixelChannel: return(QuantumScale*pixel.blue);
1456 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001457 {
1458 if (image->colorspace != CMYKColorspace)
1459 {
1460 (void) ThrowMagickException(exception,GetMagickModule(),
cristy1a020e42011-12-06 18:13:23 +00001461 ImageError,"ColorSeparatedImageRequired","`%s'",
cristy3ed852e2009-09-05 21:47:34 +00001462 image->filename);
1463 return(0.0);
1464 }
cristy4c08aed2011-07-01 19:47:50 +00001465 return(QuantumScale*pixel.black);
1466 }
cristy0568ffc2011-07-25 16:54:14 +00001467 case AlphaPixelChannel:
cristy4c08aed2011-07-01 19:47:50 +00001468 {
1469 MagickRealType
1470 alpha;
1471
1472 if (pixel.matte == MagickFalse)
1473 return(1.0);
1474 alpha=(MagickRealType) (QuantumScale*pixel.alpha);
1475 return(alpha);
cristy3ed852e2009-09-05 21:47:34 +00001476 }
cristya382aca2011-12-06 18:22:48 +00001477 case IndexPixelChannel:
1478 return(0.0);
cristyb3a73b52011-07-26 01:34:43 +00001479 case IntensityPixelChannel:
cristyf364ed42010-12-15 01:54:43 +00001480 {
cristy4c08aed2011-07-01 19:47:50 +00001481 return(QuantumScale*GetPixelInfoIntensity(&pixel));
cristyf364ed42010-12-15 01:54:43 +00001482 }
cristy3ed852e2009-09-05 21:47:34 +00001483 default:
1484 break;
1485 }
1486 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1487 "UnableToParseExpression","`%s'",p);
1488 return(0.0);
1489 }
1490 switch (*symbol)
1491 {
1492 case 'A':
1493 case 'a':
1494 {
1495 if (LocaleCompare(symbol,"a") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001496 return((MagickRealType) (QuantumScale*pixel.alpha));
cristy3ed852e2009-09-05 21:47:34 +00001497 break;
1498 }
1499 case 'B':
1500 case 'b':
1501 {
1502 if (LocaleCompare(symbol,"b") == 0)
1503 return(QuantumScale*pixel.blue);
1504 break;
1505 }
1506 case 'C':
1507 case 'c':
1508 {
1509 if (LocaleNCompare(symbol,"channel",7) == 0)
1510 {
1511 GeometryInfo
1512 channel_info;
1513
1514 MagickStatusType
1515 flags;
1516
1517 flags=ParseGeometry(symbol+7,&channel_info);
1518 if (image->colorspace == CMYKColorspace)
1519 switch (channel)
1520 {
cristy0568ffc2011-07-25 16:54:14 +00001521 case CyanPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001522 {
1523 if ((flags & RhoValue) == 0)
1524 return(0.0);
1525 return(channel_info.rho);
1526 }
cristy0568ffc2011-07-25 16:54:14 +00001527 case MagentaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001528 {
1529 if ((flags & SigmaValue) == 0)
1530 return(0.0);
1531 return(channel_info.sigma);
1532 }
cristy0568ffc2011-07-25 16:54:14 +00001533 case YellowPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001534 {
1535 if ((flags & XiValue) == 0)
1536 return(0.0);
1537 return(channel_info.xi);
1538 }
cristy0568ffc2011-07-25 16:54:14 +00001539 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001540 {
1541 if ((flags & PsiValue) == 0)
1542 return(0.0);
1543 return(channel_info.psi);
1544 }
cristy0568ffc2011-07-25 16:54:14 +00001545 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001546 {
1547 if ((flags & ChiValue) == 0)
1548 return(0.0);
1549 return(channel_info.chi);
1550 }
1551 default:
1552 return(0.0);
1553 }
1554 switch (channel)
1555 {
cristy0568ffc2011-07-25 16:54:14 +00001556 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001557 {
1558 if ((flags & RhoValue) == 0)
1559 return(0.0);
1560 return(channel_info.rho);
1561 }
cristy0568ffc2011-07-25 16:54:14 +00001562 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001563 {
1564 if ((flags & SigmaValue) == 0)
1565 return(0.0);
1566 return(channel_info.sigma);
1567 }
cristy0568ffc2011-07-25 16:54:14 +00001568 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001569 {
1570 if ((flags & XiValue) == 0)
1571 return(0.0);
1572 return(channel_info.xi);
1573 }
cristy0568ffc2011-07-25 16:54:14 +00001574 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001575 {
1576 if ((flags & ChiValue) == 0)
1577 return(0.0);
1578 return(channel_info.chi);
1579 }
cristy0568ffc2011-07-25 16:54:14 +00001580 case AlphaPixelChannel:
cristy4c08aed2011-07-01 19:47:50 +00001581 {
1582 if ((flags & PsiValue) == 0)
1583 return(0.0);
1584 return(channel_info.psi);
1585 }
cristy3ed852e2009-09-05 21:47:34 +00001586 default:
1587 return(0.0);
1588 }
1589 return(0.0);
1590 }
1591 if (LocaleCompare(symbol,"c") == 0)
1592 return(QuantumScale*pixel.red);
1593 break;
1594 }
1595 case 'D':
1596 case 'd':
1597 {
1598 if (LocaleNCompare(symbol,"depth",5) == 0)
1599 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1600 break;
1601 }
1602 case 'G':
1603 case 'g':
1604 {
1605 if (LocaleCompare(symbol,"g") == 0)
1606 return(QuantumScale*pixel.green);
1607 break;
1608 }
1609 case 'K':
1610 case 'k':
1611 {
1612 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1613 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1614 if (LocaleCompare(symbol,"k") == 0)
1615 {
1616 if (image->colorspace != CMYKColorspace)
1617 {
1618 (void) ThrowMagickException(exception,GetMagickModule(),
1619 OptionError,"ColorSeparatedImageRequired","`%s'",
1620 image->filename);
1621 return(0.0);
1622 }
cristy4c08aed2011-07-01 19:47:50 +00001623 return(QuantumScale*pixel.black);
cristy3ed852e2009-09-05 21:47:34 +00001624 }
1625 break;
1626 }
1627 case 'H':
1628 case 'h':
1629 {
1630 if (LocaleCompare(symbol,"h") == 0)
1631 return((MagickRealType) image->rows);
1632 if (LocaleCompare(symbol,"hue") == 0)
1633 {
1634 double
1635 hue,
1636 lightness,
1637 saturation;
1638
cristyda1f9c12011-10-02 21:39:49 +00001639 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1640 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001641 return(hue);
1642 }
1643 break;
1644 }
1645 case 'I':
1646 case 'i':
1647 {
1648 if ((LocaleCompare(symbol,"image.depth") == 0) ||
1649 (LocaleCompare(symbol,"image.minima") == 0) ||
1650 (LocaleCompare(symbol,"image.maxima") == 0) ||
1651 (LocaleCompare(symbol,"image.mean") == 0) ||
1652 (LocaleCompare(symbol,"image.kurtosis") == 0) ||
1653 (LocaleCompare(symbol,"image.skewness") == 0) ||
1654 (LocaleCompare(symbol,"image.standard_deviation") == 0))
1655 return(FxChannelStatistics(fx_info,image,channel,symbol+6,exception));
1656 if (LocaleCompare(symbol,"image.resolution.x") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001657 return(image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00001658 if (LocaleCompare(symbol,"image.resolution.y") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001659 return(image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001660 if (LocaleCompare(symbol,"intensity") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001661 return(QuantumScale*GetPixelInfoIntensity(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001662 if (LocaleCompare(symbol,"i") == 0)
1663 return((MagickRealType) x);
1664 break;
1665 }
1666 case 'J':
1667 case 'j':
1668 {
1669 if (LocaleCompare(symbol,"j") == 0)
1670 return((MagickRealType) y);
1671 break;
1672 }
1673 case 'L':
1674 case 'l':
1675 {
1676 if (LocaleCompare(symbol,"lightness") == 0)
1677 {
1678 double
1679 hue,
1680 lightness,
1681 saturation;
1682
cristyda1f9c12011-10-02 21:39:49 +00001683 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1684 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001685 return(lightness);
1686 }
1687 if (LocaleCompare(symbol,"luminance") == 0)
1688 {
1689 double
1690 luminence;
1691
1692 luminence=0.2126*pixel.red+0.7152*pixel.green+0.0722*pixel.blue;
1693 return(QuantumScale*luminence);
1694 }
1695 break;
1696 }
1697 case 'M':
1698 case 'm':
1699 {
1700 if (LocaleNCompare(symbol,"maxima",6) == 0)
1701 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1702 if (LocaleNCompare(symbol,"mean",4) == 0)
1703 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1704 if (LocaleNCompare(symbol,"minima",6) == 0)
1705 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1706 if (LocaleCompare(symbol,"m") == 0)
1707 return(QuantumScale*pixel.blue);
1708 break;
1709 }
1710 case 'N':
1711 case 'n':
1712 {
1713 if (LocaleCompare(symbol,"n") == 0)
anthony374f5dd2011-03-25 10:08:53 +00001714 return((MagickRealType) GetImageListLength(fx_info->images));
cristy3ed852e2009-09-05 21:47:34 +00001715 break;
1716 }
1717 case 'O':
1718 case 'o':
1719 {
1720 if (LocaleCompare(symbol,"o") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001721 return(QuantumScale*pixel.alpha);
cristy3ed852e2009-09-05 21:47:34 +00001722 break;
1723 }
1724 case 'P':
1725 case 'p':
1726 {
1727 if (LocaleCompare(symbol,"page.height") == 0)
1728 return((MagickRealType) image->page.height);
1729 if (LocaleCompare(symbol,"page.width") == 0)
1730 return((MagickRealType) image->page.width);
1731 if (LocaleCompare(symbol,"page.x") == 0)
1732 return((MagickRealType) image->page.x);
1733 if (LocaleCompare(symbol,"page.y") == 0)
1734 return((MagickRealType) image->page.y);
1735 break;
1736 }
1737 case 'R':
1738 case 'r':
1739 {
1740 if (LocaleCompare(symbol,"resolution.x") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001741 return(image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00001742 if (LocaleCompare(symbol,"resolution.y") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001743 return(image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001744 if (LocaleCompare(symbol,"r") == 0)
1745 return(QuantumScale*pixel.red);
1746 break;
1747 }
1748 case 'S':
1749 case 's':
1750 {
1751 if (LocaleCompare(symbol,"saturation") == 0)
1752 {
1753 double
1754 hue,
1755 lightness,
1756 saturation;
1757
cristyda1f9c12011-10-02 21:39:49 +00001758 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1759 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001760 return(saturation);
1761 }
1762 if (LocaleNCompare(symbol,"skewness",8) == 0)
1763 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1764 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1765 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1766 break;
1767 }
1768 case 'T':
1769 case 't':
1770 {
1771 if (LocaleCompare(symbol,"t") == 0)
cristy5a15b932011-03-26 12:50:33 +00001772 return((MagickRealType) GetImageIndexInList(fx_info->images));
cristy3ed852e2009-09-05 21:47:34 +00001773 break;
1774 }
1775 case 'W':
1776 case 'w':
1777 {
1778 if (LocaleCompare(symbol,"w") == 0)
1779 return((MagickRealType) image->columns);
1780 break;
1781 }
1782 case 'Y':
1783 case 'y':
1784 {
1785 if (LocaleCompare(symbol,"y") == 0)
1786 return(QuantumScale*pixel.green);
1787 break;
1788 }
1789 case 'Z':
1790 case 'z':
1791 {
1792 if (LocaleCompare(symbol,"z") == 0)
1793 {
1794 MagickRealType
1795 depth;
1796
cristyfefab1b2011-07-05 00:33:22 +00001797 depth=(MagickRealType) GetImageDepth(image,fx_info->exception);
cristy3ed852e2009-09-05 21:47:34 +00001798 return(depth);
1799 }
1800 break;
1801 }
1802 default:
1803 break;
1804 }
1805 value=(const char *) GetValueFromSplayTree(fx_info->symbols,symbol);
1806 if (value != (const char *) NULL)
cristydbdd0e32011-11-04 23:29:40 +00001807 return((MagickRealType) StringToDouble(value,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001808 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1809 "UnableToParseExpression","`%s'",symbol);
1810 return(0.0);
1811}
1812
1813static const char *FxOperatorPrecedence(const char *expression,
1814 ExceptionInfo *exception)
1815{
1816 typedef enum
1817 {
1818 UndefinedPrecedence,
1819 NullPrecedence,
1820 BitwiseComplementPrecedence,
1821 ExponentPrecedence,
cristy116af162010-08-13 01:25:47 +00001822 ExponentialNotationPrecedence,
cristy3ed852e2009-09-05 21:47:34 +00001823 MultiplyPrecedence,
1824 AdditionPrecedence,
1825 ShiftPrecedence,
1826 RelationalPrecedence,
1827 EquivalencyPrecedence,
1828 BitwiseAndPrecedence,
1829 BitwiseOrPrecedence,
1830 LogicalAndPrecedence,
1831 LogicalOrPrecedence,
1832 TernaryPrecedence,
1833 AssignmentPrecedence,
1834 CommaPrecedence,
1835 SeparatorPrecedence
1836 } FxPrecedence;
1837
1838 FxPrecedence
1839 precedence,
1840 target;
1841
1842 register const char
1843 *subexpression;
1844
1845 register int
1846 c;
1847
cristybb503372010-05-27 20:51:26 +00001848 size_t
cristy3ed852e2009-09-05 21:47:34 +00001849 level;
1850
1851 c=0;
1852 level=0;
1853 subexpression=(const char *) NULL;
1854 target=NullPrecedence;
1855 while (*expression != '\0')
1856 {
1857 precedence=UndefinedPrecedence;
1858 if ((isspace((int) ((char) *expression)) != 0) || (c == (int) '@'))
1859 {
1860 expression++;
1861 continue;
1862 }
cristy488fa882010-03-01 22:34:24 +00001863 switch (*expression)
1864 {
1865 case 'A':
1866 case 'a':
cristy3ed852e2009-09-05 21:47:34 +00001867 {
cristyb33454f2011-08-03 02:10:45 +00001868#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00001869 if (LocaleNCompare(expression,"acosh",5) == 0)
1870 {
1871 expression+=5;
1872 break;
1873 }
cristyb33454f2011-08-03 02:10:45 +00001874#endif
1875#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00001876 if (LocaleNCompare(expression,"asinh",5) == 0)
1877 {
1878 expression+=5;
1879 break;
1880 }
cristyb33454f2011-08-03 02:10:45 +00001881#endif
1882#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00001883 if (LocaleNCompare(expression,"atanh",5) == 0)
cristy488fa882010-03-01 22:34:24 +00001884 {
1885 expression+=5;
1886 break;
1887 }
cristyb33454f2011-08-03 02:10:45 +00001888#endif
cristy488fa882010-03-01 22:34:24 +00001889 break;
cristy3ed852e2009-09-05 21:47:34 +00001890 }
cristy62d455f2011-11-03 11:42:28 +00001891 case 'E':
1892 case 'e':
1893 {
1894 if ((LocaleNCompare(expression,"E+",2) == 0) ||
1895 (LocaleNCompare(expression,"E-",2) == 0))
1896 {
1897 expression+=2; /* scientific notation */
1898 break;
1899 }
1900 }
cristy488fa882010-03-01 22:34:24 +00001901 case 'J':
1902 case 'j':
1903 {
1904 if ((LocaleNCompare(expression,"j0",2) == 0) ||
1905 (LocaleNCompare(expression,"j1",2) == 0))
1906 {
1907 expression+=2;
1908 break;
1909 }
1910 break;
1911 }
cristy2def9322010-06-18 23:59:37 +00001912 case '#':
1913 {
1914 while (isxdigit((int) ((unsigned char) *(expression+1))) != 0)
1915 expression++;
1916 break;
1917 }
cristy488fa882010-03-01 22:34:24 +00001918 default:
1919 break;
1920 }
cristy3ed852e2009-09-05 21:47:34 +00001921 if ((c == (int) '{') || (c == (int) '['))
1922 level++;
1923 else
1924 if ((c == (int) '}') || (c == (int) ']'))
1925 level--;
1926 if (level == 0)
1927 switch ((unsigned char) *expression)
1928 {
1929 case '~':
1930 case '!':
1931 {
1932 precedence=BitwiseComplementPrecedence;
1933 break;
1934 }
1935 case '^':
cristy6621e252010-08-13 00:42:57 +00001936 case '@':
cristy3ed852e2009-09-05 21:47:34 +00001937 {
1938 precedence=ExponentPrecedence;
1939 break;
1940 }
1941 default:
1942 {
1943 if (((c != 0) && ((isdigit((int) ((char) c)) != 0) ||
1944 (strchr(")",c) != (char *) NULL))) &&
1945 (((islower((int) ((char) *expression)) != 0) ||
1946 (strchr("(",(int) *expression) != (char *) NULL)) ||
1947 ((isdigit((int) ((char) c)) == 0) &&
1948 (isdigit((int) ((char) *expression)) != 0))) &&
1949 (strchr("xy",(int) *expression) == (char *) NULL))
1950 precedence=MultiplyPrecedence;
1951 break;
1952 }
1953 case '*':
1954 case '/':
1955 case '%':
1956 {
1957 precedence=MultiplyPrecedence;
1958 break;
1959 }
1960 case '+':
1961 case '-':
1962 {
1963 if ((strchr("(+-/*%:&^|<>~,",c) == (char *) NULL) ||
1964 (isalpha(c) != 0))
1965 precedence=AdditionPrecedence;
1966 break;
1967 }
1968 case LeftShiftOperator:
1969 case RightShiftOperator:
1970 {
1971 precedence=ShiftPrecedence;
1972 break;
1973 }
1974 case '<':
1975 case LessThanEqualOperator:
1976 case GreaterThanEqualOperator:
1977 case '>':
1978 {
1979 precedence=RelationalPrecedence;
1980 break;
1981 }
1982 case EqualOperator:
1983 case NotEqualOperator:
1984 {
1985 precedence=EquivalencyPrecedence;
1986 break;
1987 }
1988 case '&':
1989 {
1990 precedence=BitwiseAndPrecedence;
1991 break;
1992 }
1993 case '|':
1994 {
1995 precedence=BitwiseOrPrecedence;
1996 break;
1997 }
1998 case LogicalAndOperator:
1999 {
2000 precedence=LogicalAndPrecedence;
2001 break;
2002 }
2003 case LogicalOrOperator:
2004 {
2005 precedence=LogicalOrPrecedence;
2006 break;
2007 }
cristy116af162010-08-13 01:25:47 +00002008 case ExponentialNotation:
2009 {
2010 precedence=ExponentialNotationPrecedence;
2011 break;
2012 }
cristy3ed852e2009-09-05 21:47:34 +00002013 case ':':
2014 case '?':
2015 {
2016 precedence=TernaryPrecedence;
2017 break;
2018 }
2019 case '=':
2020 {
2021 precedence=AssignmentPrecedence;
2022 break;
2023 }
2024 case ',':
2025 {
2026 precedence=CommaPrecedence;
2027 break;
2028 }
2029 case ';':
2030 {
2031 precedence=SeparatorPrecedence;
2032 break;
2033 }
2034 }
2035 if ((precedence == BitwiseComplementPrecedence) ||
2036 (precedence == TernaryPrecedence) ||
2037 (precedence == AssignmentPrecedence))
2038 {
2039 if (precedence > target)
2040 {
2041 /*
2042 Right-to-left associativity.
2043 */
2044 target=precedence;
2045 subexpression=expression;
2046 }
2047 }
2048 else
2049 if (precedence >= target)
2050 {
2051 /*
2052 Left-to-right associativity.
2053 */
2054 target=precedence;
2055 subexpression=expression;
2056 }
2057 if (strchr("(",(int) *expression) != (char *) NULL)
2058 expression=FxSubexpression(expression,exception);
2059 c=(int) (*expression++);
2060 }
2061 return(subexpression);
2062}
2063
2064static MagickRealType FxEvaluateSubexpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002065 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristye85007d2010-06-06 22:51:36 +00002066 const char *expression,MagickRealType *beta,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002067{
2068 char
2069 *q,
2070 subexpression[MaxTextExtent];
2071
2072 MagickRealType
2073 alpha,
2074 gamma;
2075
2076 register const char
2077 *p;
2078
2079 *beta=0.0;
2080 if (exception->severity != UndefinedException)
2081 return(0.0);
2082 while (isspace((int) *expression) != 0)
2083 expression++;
2084 if (*expression == '\0')
2085 {
2086 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
2087 "MissingExpression","`%s'",expression);
2088 return(0.0);
2089 }
cristy66322f02010-05-17 11:40:48 +00002090 *subexpression='\0';
cristy3ed852e2009-09-05 21:47:34 +00002091 p=FxOperatorPrecedence(expression,exception);
2092 if (p != (const char *) NULL)
2093 {
2094 (void) CopyMagickString(subexpression,expression,(size_t)
2095 (p-expression+1));
2096 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2097 exception);
2098 switch ((unsigned char) *p)
2099 {
2100 case '~':
2101 {
2102 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristybb503372010-05-27 20:51:26 +00002103 *beta=(MagickRealType) (~(size_t) *beta);
cristy3ed852e2009-09-05 21:47:34 +00002104 return(*beta);
2105 }
2106 case '!':
2107 {
2108 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2109 return(*beta == 0.0 ? 1.0 : 0.0);
2110 }
2111 case '^':
2112 {
2113 *beta=pow((double) alpha,(double) FxEvaluateSubexpression(fx_info,
2114 channel,x,y,++p,beta,exception));
2115 return(*beta);
2116 }
2117 case '*':
cristy116af162010-08-13 01:25:47 +00002118 case ExponentialNotation:
cristy3ed852e2009-09-05 21:47:34 +00002119 {
2120 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2121 return(alpha*(*beta));
2122 }
2123 case '/':
2124 {
2125 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2126 if (*beta == 0.0)
2127 {
2128 if (exception->severity == UndefinedException)
2129 (void) ThrowMagickException(exception,GetMagickModule(),
2130 OptionError,"DivideByZero","`%s'",expression);
2131 return(0.0);
2132 }
2133 return(alpha/(*beta));
2134 }
2135 case '%':
2136 {
2137 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2138 *beta=fabs(floor(((double) *beta)+0.5));
2139 if (*beta == 0.0)
2140 {
2141 (void) ThrowMagickException(exception,GetMagickModule(),
2142 OptionError,"DivideByZero","`%s'",expression);
2143 return(0.0);
2144 }
2145 return(fmod((double) alpha,(double) *beta));
2146 }
2147 case '+':
2148 {
2149 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2150 return(alpha+(*beta));
2151 }
2152 case '-':
2153 {
2154 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2155 return(alpha-(*beta));
2156 }
2157 case LeftShiftOperator:
2158 {
2159 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy1707c6c2012-01-18 23:30:54 +00002160 *beta=(MagickRealType) ((size_t) (alpha+0.5) << (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002161 return(*beta);
2162 }
2163 case RightShiftOperator:
2164 {
2165 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy1707c6c2012-01-18 23:30:54 +00002166 *beta=(MagickRealType) ((size_t) (alpha+0.5) >> (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002167 return(*beta);
2168 }
2169 case '<':
2170 {
2171 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2172 return(alpha < *beta ? 1.0 : 0.0);
2173 }
2174 case LessThanEqualOperator:
2175 {
2176 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2177 return(alpha <= *beta ? 1.0 : 0.0);
2178 }
2179 case '>':
2180 {
2181 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2182 return(alpha > *beta ? 1.0 : 0.0);
2183 }
2184 case GreaterThanEqualOperator:
2185 {
2186 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2187 return(alpha >= *beta ? 1.0 : 0.0);
2188 }
2189 case EqualOperator:
2190 {
2191 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2192 return(fabs(alpha-(*beta)) <= MagickEpsilon ? 1.0 : 0.0);
2193 }
2194 case NotEqualOperator:
2195 {
2196 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2197 return(fabs(alpha-(*beta)) > MagickEpsilon ? 1.0 : 0.0);
2198 }
2199 case '&':
2200 {
2201 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy1707c6c2012-01-18 23:30:54 +00002202 *beta=(MagickRealType) ((size_t) (alpha+0.5) & (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002203 return(*beta);
2204 }
2205 case '|':
2206 {
2207 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy1707c6c2012-01-18 23:30:54 +00002208 *beta=(MagickRealType) ((size_t) (alpha+0.5) | (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002209 return(*beta);
2210 }
2211 case LogicalAndOperator:
2212 {
2213 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2214 *beta=(alpha > 0.0) && (gamma > 0.0) ? 1.0 : 0.0;
2215 return(*beta);
2216 }
2217 case LogicalOrOperator:
2218 {
2219 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2220 *beta=(alpha > 0.0) || (gamma > 0.0) ? 1.0 : 0.0;
2221 return(*beta);
2222 }
2223 case '?':
2224 {
2225 MagickRealType
2226 gamma;
2227
2228 (void) CopyMagickString(subexpression,++p,MaxTextExtent);
2229 q=subexpression;
2230 p=StringToken(":",&q);
2231 if (q == (char *) NULL)
2232 {
2233 (void) ThrowMagickException(exception,GetMagickModule(),
2234 OptionError,"UnableToParseExpression","`%s'",subexpression);
2235 return(0.0);
2236 }
2237 if (fabs((double) alpha) > MagickEpsilon)
2238 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,exception);
2239 else
2240 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,q,beta,exception);
2241 return(gamma);
2242 }
2243 case '=':
2244 {
2245 char
2246 numeric[MaxTextExtent];
2247
2248 q=subexpression;
2249 while (isalpha((int) ((unsigned char) *q)) != 0)
2250 q++;
2251 if (*q != '\0')
2252 {
2253 (void) ThrowMagickException(exception,GetMagickModule(),
2254 OptionError,"UnableToParseExpression","`%s'",subexpression);
2255 return(0.0);
2256 }
2257 ClearMagickException(exception);
2258 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristyb51dff52011-05-19 16:55:47 +00002259 (void) FormatLocaleString(numeric,MaxTextExtent,"%g",(double)
cristy8cd5b312010-01-07 01:10:24 +00002260 *beta);
cristy3ed852e2009-09-05 21:47:34 +00002261 (void) DeleteNodeFromSplayTree(fx_info->symbols,subexpression);
2262 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(
2263 subexpression),ConstantString(numeric));
2264 return(*beta);
2265 }
2266 case ',':
2267 {
2268 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2269 return(alpha);
2270 }
2271 case ';':
2272 {
2273 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2274 return(*beta);
2275 }
2276 default:
2277 {
2278 gamma=alpha*FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,
2279 exception);
2280 return(gamma);
2281 }
2282 }
2283 }
2284 if (strchr("(",(int) *expression) != (char *) NULL)
2285 {
2286 (void) CopyMagickString(subexpression,expression+1,MaxTextExtent);
2287 subexpression[strlen(subexpression)-1]='\0';
2288 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2289 exception);
2290 return(gamma);
2291 }
cristy8b8a3ae2010-10-23 18:49:46 +00002292 switch (*expression)
cristy3ed852e2009-09-05 21:47:34 +00002293 {
2294 case '+':
2295 {
2296 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2297 exception);
2298 return(1.0*gamma);
2299 }
2300 case '-':
2301 {
2302 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2303 exception);
2304 return(-1.0*gamma);
2305 }
2306 case '~':
2307 {
2308 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2309 exception);
cristybb503372010-05-27 20:51:26 +00002310 return((MagickRealType) (~(size_t) (gamma+0.5)));
cristy3ed852e2009-09-05 21:47:34 +00002311 }
2312 case 'A':
2313 case 'a':
2314 {
2315 if (LocaleNCompare(expression,"abs",3) == 0)
2316 {
2317 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2318 exception);
2319 return((MagickRealType) fabs((double) alpha));
2320 }
cristyb33454f2011-08-03 02:10:45 +00002321#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00002322 if (LocaleNCompare(expression,"acosh",5) == 0)
2323 {
2324 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2325 exception);
2326 return((MagickRealType) acosh((double) alpha));
2327 }
cristyb33454f2011-08-03 02:10:45 +00002328#endif
cristy3ed852e2009-09-05 21:47:34 +00002329 if (LocaleNCompare(expression,"acos",4) == 0)
2330 {
2331 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2332 exception);
2333 return((MagickRealType) acos((double) alpha));
2334 }
cristy43c22f42010-03-30 12:34:07 +00002335#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002336 if (LocaleNCompare(expression,"airy",4) == 0)
2337 {
2338 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2339 exception);
2340 if (alpha == 0.0)
cristy2dd03222010-03-30 22:12:11 +00002341 return(1.0);
2342 gamma=2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha);
cristy43c22f42010-03-30 12:34:07 +00002343 return(gamma*gamma);
cristyee56cf12010-03-01 22:17:06 +00002344 }
cristy43c22f42010-03-30 12:34:07 +00002345#endif
cristyb33454f2011-08-03 02:10:45 +00002346#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00002347 if (LocaleNCompare(expression,"asinh",5) == 0)
2348 {
2349 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2350 exception);
2351 return((MagickRealType) asinh((double) alpha));
2352 }
cristyb33454f2011-08-03 02:10:45 +00002353#endif
cristy3ed852e2009-09-05 21:47:34 +00002354 if (LocaleNCompare(expression,"asin",4) == 0)
2355 {
2356 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2357 exception);
2358 return((MagickRealType) asin((double) alpha));
2359 }
2360 if (LocaleNCompare(expression,"alt",3) == 0)
2361 {
2362 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2363 exception);
cristybb503372010-05-27 20:51:26 +00002364 return(((ssize_t) alpha) & 0x01 ? -1.0 : 1.0);
cristy3ed852e2009-09-05 21:47:34 +00002365 }
2366 if (LocaleNCompare(expression,"atan2",5) == 0)
2367 {
2368 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2369 exception);
2370 return((MagickRealType) atan2((double) alpha,(double) *beta));
2371 }
cristyb33454f2011-08-03 02:10:45 +00002372#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00002373 if (LocaleNCompare(expression,"atanh",5) == 0)
2374 {
2375 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2376 exception);
2377 return((MagickRealType) atanh((double) alpha));
2378 }
cristyb33454f2011-08-03 02:10:45 +00002379#endif
cristy3ed852e2009-09-05 21:47:34 +00002380 if (LocaleNCompare(expression,"atan",4) == 0)
2381 {
2382 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2383 exception);
2384 return((MagickRealType) atan((double) alpha));
2385 }
2386 if (LocaleCompare(expression,"a") == 0)
2387 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2388 break;
2389 }
2390 case 'B':
2391 case 'b':
2392 {
2393 if (LocaleCompare(expression,"b") == 0)
2394 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2395 break;
2396 }
2397 case 'C':
2398 case 'c':
2399 {
2400 if (LocaleNCompare(expression,"ceil",4) == 0)
2401 {
2402 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2403 exception);
2404 return((MagickRealType) ceil((double) alpha));
2405 }
2406 if (LocaleNCompare(expression,"cosh",4) == 0)
2407 {
2408 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2409 exception);
2410 return((MagickRealType) cosh((double) alpha));
2411 }
2412 if (LocaleNCompare(expression,"cos",3) == 0)
2413 {
2414 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2415 exception);
2416 return((MagickRealType) cos((double) alpha));
2417 }
2418 if (LocaleCompare(expression,"c") == 0)
2419 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2420 break;
2421 }
2422 case 'D':
2423 case 'd':
2424 {
2425 if (LocaleNCompare(expression,"debug",5) == 0)
2426 {
2427 const char
2428 *type;
2429
2430 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2431 exception);
2432 if (fx_info->images->colorspace == CMYKColorspace)
2433 switch (channel)
2434 {
cristy0568ffc2011-07-25 16:54:14 +00002435 case CyanPixelChannel: type="cyan"; break;
2436 case MagentaPixelChannel: type="magenta"; break;
2437 case YellowPixelChannel: type="yellow"; break;
2438 case AlphaPixelChannel: type="opacity"; break;
2439 case BlackPixelChannel: type="black"; break;
cristy3ed852e2009-09-05 21:47:34 +00002440 default: type="unknown"; break;
2441 }
2442 else
2443 switch (channel)
2444 {
cristy0568ffc2011-07-25 16:54:14 +00002445 case RedPixelChannel: type="red"; break;
2446 case GreenPixelChannel: type="green"; break;
2447 case BluePixelChannel: type="blue"; break;
2448 case AlphaPixelChannel: type="opacity"; break;
cristy3ed852e2009-09-05 21:47:34 +00002449 default: type="unknown"; break;
2450 }
2451 (void) CopyMagickString(subexpression,expression+6,MaxTextExtent);
2452 if (strlen(subexpression) > 1)
2453 subexpression[strlen(subexpression)-1]='\0';
2454 if (fx_info->file != (FILE *) NULL)
cristy1707c6c2012-01-18 23:30:54 +00002455 (void) FormatLocaleFile(fx_info->file,"%s[%.20g,%.20g].%s: "
2456 "%s=%.*g\n",fx_info->images->filename,(double) x,(double) y,type,
2457 subexpression,GetMagickPrecision(),(double) alpha);
cristy3ed852e2009-09-05 21:47:34 +00002458 return(0.0);
2459 }
cristy5597a8d2011-11-04 00:25:32 +00002460 if (LocaleNCompare(expression,"drc",3) == 0)
2461 {
2462 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2463 exception);
2464 return((MagickRealType) (alpha/(*beta*(alpha-1.0)+1.0)));
2465 }
cristy3ed852e2009-09-05 21:47:34 +00002466 break;
2467 }
2468 case 'E':
2469 case 'e':
2470 {
2471 if (LocaleCompare(expression,"epsilon") == 0)
2472 return((MagickRealType) MagickEpsilon);
2473 if (LocaleNCompare(expression,"exp",3) == 0)
2474 {
2475 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2476 exception);
2477 return((MagickRealType) exp((double) alpha));
2478 }
2479 if (LocaleCompare(expression,"e") == 0)
2480 return((MagickRealType) 2.7182818284590452354);
2481 break;
2482 }
2483 case 'F':
2484 case 'f':
2485 {
2486 if (LocaleNCompare(expression,"floor",5) == 0)
2487 {
2488 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2489 exception);
2490 return((MagickRealType) floor((double) alpha));
2491 }
2492 break;
2493 }
2494 case 'G':
2495 case 'g':
2496 {
cristy9eeedea2011-11-02 19:04:05 +00002497 if (LocaleNCompare(expression,"gauss",5) == 0)
2498 {
2499 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2500 exception);
2501 gamma=exp((double) (-alpha*alpha/2.0))/sqrt(2.0*MagickPI);
2502 return((MagickRealType) gamma);
2503 }
cristyb0aad4c2011-11-02 19:30:35 +00002504 if (LocaleNCompare(expression,"gcd",3) == 0)
2505 {
2506 MagickOffsetType
2507 gcd;
2508
2509 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2510 exception);
cristy1707c6c2012-01-18 23:30:54 +00002511 gcd=FxGCD((MagickOffsetType) (alpha+0.5),(MagickOffsetType) (*beta+
2512 0.5));
cristyb0aad4c2011-11-02 19:30:35 +00002513 return((MagickRealType) gcd);
2514 }
cristy3ed852e2009-09-05 21:47:34 +00002515 if (LocaleCompare(expression,"g") == 0)
2516 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2517 break;
2518 }
2519 case 'H':
2520 case 'h':
2521 {
2522 if (LocaleCompare(expression,"h") == 0)
2523 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2524 if (LocaleCompare(expression,"hue") == 0)
2525 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2526 if (LocaleNCompare(expression,"hypot",5) == 0)
2527 {
2528 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2529 exception);
2530 return((MagickRealType) hypot((double) alpha,(double) *beta));
2531 }
2532 break;
2533 }
2534 case 'K':
2535 case 'k':
2536 {
2537 if (LocaleCompare(expression,"k") == 0)
2538 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2539 break;
2540 }
2541 case 'I':
2542 case 'i':
2543 {
2544 if (LocaleCompare(expression,"intensity") == 0)
2545 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2546 if (LocaleNCompare(expression,"int",3) == 0)
2547 {
2548 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2549 exception);
cristy16788e42010-08-13 13:44:26 +00002550 return((MagickRealType) floor(alpha));
cristy3ed852e2009-09-05 21:47:34 +00002551 }
cristy82b20722011-11-05 21:52:36 +00002552#if defined(MAGICKCORE_HAVE_ISNAN)
cristy639399c2011-11-02 19:16:15 +00002553 if (LocaleNCompare(expression,"isnan",5) == 0)
2554 {
2555 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2556 exception);
cristy17a10202011-11-02 19:17:04 +00002557 return((MagickRealType) !!isnan((double) alpha));
cristy639399c2011-11-02 19:16:15 +00002558 }
cristy82b20722011-11-05 21:52:36 +00002559#endif
cristy3ed852e2009-09-05 21:47:34 +00002560 if (LocaleCompare(expression,"i") == 0)
2561 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2562 break;
2563 }
2564 case 'J':
2565 case 'j':
2566 {
2567 if (LocaleCompare(expression,"j") == 0)
2568 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
cristy161b9262010-03-20 19:34:32 +00002569#if defined(MAGICKCORE_HAVE_J0)
cristyee56cf12010-03-01 22:17:06 +00002570 if (LocaleNCompare(expression,"j0",2) == 0)
2571 {
2572 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2573 exception);
2574 return((MagickRealType) j0((double) alpha));
2575 }
cristy161b9262010-03-20 19:34:32 +00002576#endif
2577#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002578 if (LocaleNCompare(expression,"j1",2) == 0)
2579 {
2580 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2581 exception);
2582 return((MagickRealType) j1((double) alpha));
2583 }
cristy161b9262010-03-20 19:34:32 +00002584#endif
cristyaa018fa2010-04-08 23:03:54 +00002585#if defined(MAGICKCORE_HAVE_J1)
cristya6a09e72010-03-02 14:51:02 +00002586 if (LocaleNCompare(expression,"jinc",4) == 0)
2587 {
2588 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2589 exception);
cristy0946a822010-03-12 17:14:58 +00002590 if (alpha == 0.0)
2591 return(1.0);
cristy1707c6c2012-01-18 23:30:54 +00002592 gamma=(MagickRealType) (2.0*j1((double) (MagickPI*alpha))/(MagickPI*
2593 alpha));
cristyfce2f7b2010-03-12 00:29:49 +00002594 return(gamma);
cristya6a09e72010-03-02 14:51:02 +00002595 }
cristyaa018fa2010-04-08 23:03:54 +00002596#endif
cristy3ed852e2009-09-05 21:47:34 +00002597 break;
2598 }
2599 case 'L':
2600 case 'l':
2601 {
2602 if (LocaleNCompare(expression,"ln",2) == 0)
2603 {
2604 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2605 exception);
2606 return((MagickRealType) log((double) alpha));
2607 }
cristyc8ed5322010-08-31 12:07:59 +00002608 if (LocaleNCompare(expression,"logtwo",6) == 0)
cristy3ed852e2009-09-05 21:47:34 +00002609 {
cristyc8ed5322010-08-31 12:07:59 +00002610 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
cristy3ed852e2009-09-05 21:47:34 +00002611 exception);
2612 return((MagickRealType) log10((double) alpha))/log10(2.0);
2613 }
2614 if (LocaleNCompare(expression,"log",3) == 0)
2615 {
2616 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2617 exception);
2618 return((MagickRealType) log10((double) alpha));
2619 }
2620 if (LocaleCompare(expression,"lightness") == 0)
2621 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2622 break;
2623 }
2624 case 'M':
2625 case 'm':
2626 {
2627 if (LocaleCompare(expression,"MaxRGB") == 0)
2628 return((MagickRealType) QuantumRange);
2629 if (LocaleNCompare(expression,"maxima",6) == 0)
2630 break;
2631 if (LocaleNCompare(expression,"max",3) == 0)
cristy984049c2011-11-03 18:34:58 +00002632 {
2633 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2634 exception);
2635 return(alpha > *beta ? alpha : *beta);
2636 }
cristy3ed852e2009-09-05 21:47:34 +00002637 if (LocaleNCompare(expression,"minima",6) == 0)
2638 break;
2639 if (LocaleNCompare(expression,"min",3) == 0)
cristy984049c2011-11-03 18:34:58 +00002640 {
2641 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2642 exception);
2643 return(alpha < *beta ? alpha : *beta);
2644 }
cristy3ed852e2009-09-05 21:47:34 +00002645 if (LocaleNCompare(expression,"mod",3) == 0)
2646 {
2647 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2648 exception);
cristy984049c2011-11-03 18:34:58 +00002649 gamma=alpha-floor((double) (alpha/(*beta)))*(*beta);
2650 return(gamma);
cristy3ed852e2009-09-05 21:47:34 +00002651 }
2652 if (LocaleCompare(expression,"m") == 0)
2653 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2654 break;
2655 }
2656 case 'N':
2657 case 'n':
2658 {
cristyad3502e2011-11-02 19:10:45 +00002659 if (LocaleNCompare(expression,"not",3) == 0)
2660 {
2661 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2662 exception);
2663 return((MagickRealType) (alpha < MagickEpsilon));
2664 }
cristy3ed852e2009-09-05 21:47:34 +00002665 if (LocaleCompare(expression,"n") == 0)
2666 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2667 break;
2668 }
2669 case 'O':
2670 case 'o':
2671 {
2672 if (LocaleCompare(expression,"Opaque") == 0)
2673 return(1.0);
2674 if (LocaleCompare(expression,"o") == 0)
2675 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2676 break;
2677 }
2678 case 'P':
2679 case 'p':
2680 {
cristy670aa3c2011-11-03 00:54:00 +00002681 if (LocaleCompare(expression,"phi") == 0)
2682 return((MagickRealType) MagickPHI);
cristy3ed852e2009-09-05 21:47:34 +00002683 if (LocaleCompare(expression,"pi") == 0)
2684 return((MagickRealType) MagickPI);
2685 if (LocaleNCompare(expression,"pow",3) == 0)
2686 {
2687 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2688 exception);
2689 return((MagickRealType) pow((double) alpha,(double) *beta));
2690 }
2691 if (LocaleCompare(expression,"p") == 0)
2692 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2693 break;
2694 }
2695 case 'Q':
2696 case 'q':
2697 {
2698 if (LocaleCompare(expression,"QuantumRange") == 0)
2699 return((MagickRealType) QuantumRange);
2700 if (LocaleCompare(expression,"QuantumScale") == 0)
2701 return((MagickRealType) QuantumScale);
2702 break;
2703 }
2704 case 'R':
2705 case 'r':
2706 {
2707 if (LocaleNCompare(expression,"rand",4) == 0)
2708 return((MagickRealType) GetPseudoRandomValue(fx_info->random_info));
2709 if (LocaleNCompare(expression,"round",5) == 0)
2710 {
2711 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2712 exception);
cristy16788e42010-08-13 13:44:26 +00002713 return((MagickRealType) floor((double) alpha+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002714 }
2715 if (LocaleCompare(expression,"r") == 0)
2716 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2717 break;
2718 }
2719 case 'S':
2720 case 's':
2721 {
2722 if (LocaleCompare(expression,"saturation") == 0)
2723 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2724 if (LocaleNCompare(expression,"sign",4) == 0)
2725 {
2726 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2727 exception);
2728 return(alpha < 0.0 ? -1.0 : 1.0);
2729 }
cristya6a09e72010-03-02 14:51:02 +00002730 if (LocaleNCompare(expression,"sinc",4) == 0)
2731 {
2732 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2733 exception);
2734 if (alpha == 0)
2735 return(1.0);
2736 gamma=(MagickRealType) (sin((double) (MagickPI*alpha))/
2737 (MagickPI*alpha));
2738 return(gamma);
2739 }
cristy3ed852e2009-09-05 21:47:34 +00002740 if (LocaleNCompare(expression,"sinh",4) == 0)
2741 {
2742 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2743 exception);
2744 return((MagickRealType) sinh((double) alpha));
2745 }
2746 if (LocaleNCompare(expression,"sin",3) == 0)
2747 {
2748 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2749 exception);
2750 return((MagickRealType) sin((double) alpha));
2751 }
2752 if (LocaleNCompare(expression,"sqrt",4) == 0)
2753 {
2754 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2755 exception);
2756 return((MagickRealType) sqrt((double) alpha));
2757 }
cristy9eeedea2011-11-02 19:04:05 +00002758 if (LocaleNCompare(expression,"squish",6) == 0)
2759 {
2760 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2761 exception);
2762 return((MagickRealType) (1.0/(1.0+exp((double) (4.0*alpha)))));
2763 }
cristy3ed852e2009-09-05 21:47:34 +00002764 if (LocaleCompare(expression,"s") == 0)
2765 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2766 break;
2767 }
2768 case 'T':
2769 case 't':
2770 {
2771 if (LocaleNCompare(expression,"tanh",4) == 0)
2772 {
2773 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2774 exception);
2775 return((MagickRealType) tanh((double) alpha));
2776 }
2777 if (LocaleNCompare(expression,"tan",3) == 0)
2778 {
2779 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2780 exception);
2781 return((MagickRealType) tan((double) alpha));
2782 }
2783 if (LocaleCompare(expression,"Transparent") == 0)
2784 return(0.0);
cristy16788e42010-08-13 13:44:26 +00002785 if (LocaleNCompare(expression,"trunc",5) == 0)
2786 {
2787 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2788 exception);
2789 if (alpha >= 0.0)
2790 return((MagickRealType) floor((double) alpha));
2791 return((MagickRealType) ceil((double) alpha));
2792 }
cristy3ed852e2009-09-05 21:47:34 +00002793 if (LocaleCompare(expression,"t") == 0)
2794 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2795 break;
2796 }
2797 case 'U':
2798 case 'u':
2799 {
2800 if (LocaleCompare(expression,"u") == 0)
2801 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2802 break;
2803 }
2804 case 'V':
2805 case 'v':
2806 {
2807 if (LocaleCompare(expression,"v") == 0)
2808 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2809 break;
2810 }
2811 case 'W':
2812 case 'w':
2813 {
cristy9eeedea2011-11-02 19:04:05 +00002814 if (LocaleNCompare(expression,"while",5) == 0)
2815 {
2816 do
2817 {
2818 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2819 exception);
2820 } while (fabs((double) alpha) >= MagickEpsilon);
2821 return((MagickRealType) *beta);
2822 }
cristy3ed852e2009-09-05 21:47:34 +00002823 if (LocaleCompare(expression,"w") == 0)
2824 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2825 break;
2826 }
2827 case 'Y':
2828 case 'y':
2829 {
2830 if (LocaleCompare(expression,"y") == 0)
2831 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2832 break;
2833 }
2834 case 'Z':
2835 case 'z':
2836 {
2837 if (LocaleCompare(expression,"z") == 0)
2838 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2839 break;
2840 }
2841 default:
2842 break;
2843 }
2844 q=(char *) expression;
cristydbdd0e32011-11-04 23:29:40 +00002845 alpha=InterpretSiPrefixValue(expression,&q);
cristy3ed852e2009-09-05 21:47:34 +00002846 if (q == expression)
2847 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2848 return(alpha);
2849}
2850
cristy7832dc22011-09-05 01:21:53 +00002851MagickPrivate MagickBooleanType FxEvaluateExpression(FxInfo *fx_info,
cristy3ed852e2009-09-05 21:47:34 +00002852 MagickRealType *alpha,ExceptionInfo *exception)
2853{
2854 MagickBooleanType
2855 status;
2856
cristy541ae572011-08-05 19:08:59 +00002857 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2858 exception);
cristy3ed852e2009-09-05 21:47:34 +00002859 return(status);
2860}
2861
2862MagickExport MagickBooleanType FxPreprocessExpression(FxInfo *fx_info,
2863 MagickRealType *alpha,ExceptionInfo *exception)
2864{
2865 FILE
2866 *file;
2867
2868 MagickBooleanType
2869 status;
2870
2871 file=fx_info->file;
2872 fx_info->file=(FILE *) NULL;
cristy541ae572011-08-05 19:08:59 +00002873 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2874 exception);
cristy3ed852e2009-09-05 21:47:34 +00002875 fx_info->file=file;
2876 return(status);
2877}
2878
cristy7832dc22011-09-05 01:21:53 +00002879MagickPrivate MagickBooleanType FxEvaluateChannelExpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002880 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristye85007d2010-06-06 22:51:36 +00002881 MagickRealType *alpha,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002882{
2883 MagickRealType
2884 beta;
2885
2886 beta=0.0;
2887 *alpha=FxEvaluateSubexpression(fx_info,channel,x,y,fx_info->expression,&beta,
2888 exception);
2889 return(exception->severity == OptionError ? MagickFalse : MagickTrue);
2890}
2891
2892/*
2893%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2894% %
2895% %
2896% %
2897% F x I m a g e %
2898% %
2899% %
2900% %
2901%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2902%
2903% FxImage() applies a mathematical expression to the specified image.
2904%
2905% The format of the FxImage method is:
2906%
2907% Image *FxImage(const Image *image,const char *expression,
2908% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002909%
2910% A description of each parameter follows:
2911%
2912% o image: the image.
2913%
cristy3ed852e2009-09-05 21:47:34 +00002914% o expression: A mathematical expression.
2915%
2916% o exception: return any errors or warnings in this structure.
2917%
2918*/
2919
2920static FxInfo **DestroyFxThreadSet(FxInfo **fx_info)
2921{
cristybb503372010-05-27 20:51:26 +00002922 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002923 i;
2924
2925 assert(fx_info != (FxInfo **) NULL);
cristybb503372010-05-27 20:51:26 +00002926 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
cristy3ed852e2009-09-05 21:47:34 +00002927 if (fx_info[i] != (FxInfo *) NULL)
2928 fx_info[i]=DestroyFxInfo(fx_info[i]);
cristyb41ee102010-10-04 16:46:15 +00002929 fx_info=(FxInfo **) RelinquishMagickMemory(fx_info);
cristy3ed852e2009-09-05 21:47:34 +00002930 return(fx_info);
2931}
2932
2933static FxInfo **AcquireFxThreadSet(const Image *image,const char *expression,
2934 ExceptionInfo *exception)
2935{
2936 char
2937 *fx_expression;
2938
2939 FxInfo
2940 **fx_info;
2941
2942 MagickRealType
2943 alpha;
2944
cristybb503372010-05-27 20:51:26 +00002945 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002946 i;
2947
cristybb503372010-05-27 20:51:26 +00002948 size_t
cristy3ed852e2009-09-05 21:47:34 +00002949 number_threads;
2950
2951 number_threads=GetOpenMPMaximumThreads();
cristyb41ee102010-10-04 16:46:15 +00002952 fx_info=(FxInfo **) AcquireQuantumMemory(number_threads,sizeof(*fx_info));
cristy3ed852e2009-09-05 21:47:34 +00002953 if (fx_info == (FxInfo **) NULL)
2954 return((FxInfo **) NULL);
2955 (void) ResetMagickMemory(fx_info,0,number_threads*sizeof(*fx_info));
2956 if (*expression != '@')
2957 fx_expression=ConstantString(expression);
2958 else
2959 fx_expression=FileToString(expression+1,~0,exception);
cristybb503372010-05-27 20:51:26 +00002960 for (i=0; i < (ssize_t) number_threads; i++)
cristy3ed852e2009-09-05 21:47:34 +00002961 {
2962 fx_info[i]=AcquireFxInfo(image,fx_expression);
2963 if (fx_info[i] == (FxInfo *) NULL)
2964 return(DestroyFxThreadSet(fx_info));
2965 (void) FxPreprocessExpression(fx_info[i],&alpha,fx_info[i]->exception);
2966 }
2967 fx_expression=DestroyString(fx_expression);
2968 return(fx_info);
2969}
2970
2971MagickExport Image *FxImage(const Image *image,const char *expression,
2972 ExceptionInfo *exception)
2973{
cristy3ed852e2009-09-05 21:47:34 +00002974#define FxImageTag "Fx/Image"
2975
cristyfa112112010-01-04 17:48:07 +00002976 CacheView
cristy79cedc72011-07-25 00:41:15 +00002977 *fx_view,
2978 *image_view;
cristyfa112112010-01-04 17:48:07 +00002979
cristy3ed852e2009-09-05 21:47:34 +00002980 FxInfo
cristyfa112112010-01-04 17:48:07 +00002981 **restrict fx_info;
cristy3ed852e2009-09-05 21:47:34 +00002982
2983 Image
2984 *fx_image;
2985
cristy3ed852e2009-09-05 21:47:34 +00002986 MagickBooleanType
2987 status;
2988
cristybb503372010-05-27 20:51:26 +00002989 MagickOffsetType
2990 progress;
2991
cristy3ed852e2009-09-05 21:47:34 +00002992 MagickRealType
2993 alpha;
2994
cristybb503372010-05-27 20:51:26 +00002995 ssize_t
2996 y;
2997
cristy3ed852e2009-09-05 21:47:34 +00002998 assert(image != (Image *) NULL);
2999 assert(image->signature == MagickSignature);
3000 if (image->debug != MagickFalse)
3001 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy79cedc72011-07-25 00:41:15 +00003002 fx_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00003003 if (fx_image == (Image *) NULL)
3004 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003005 if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003006 {
cristy3ed852e2009-09-05 21:47:34 +00003007 fx_image=DestroyImage(fx_image);
3008 return((Image *) NULL);
3009 }
3010 fx_info=AcquireFxThreadSet(image,expression,exception);
3011 if (fx_info == (FxInfo **) NULL)
3012 {
3013 fx_image=DestroyImage(fx_image);
3014 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3015 }
3016 status=FxPreprocessExpression(fx_info[0],&alpha,exception);
3017 if (status == MagickFalse)
3018 {
3019 fx_image=DestroyImage(fx_image);
3020 fx_info=DestroyFxThreadSet(fx_info);
3021 return((Image *) NULL);
3022 }
3023 /*
3024 Fx image.
3025 */
3026 status=MagickTrue;
3027 progress=0;
cristy79cedc72011-07-25 00:41:15 +00003028 image_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00003029 fx_view=AcquireCacheView(fx_image);
cristyb5d5f722009-11-04 03:03:49 +00003030#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003031 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003032#endif
cristybb503372010-05-27 20:51:26 +00003033 for (y=0; y < (ssize_t) fx_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003034 {
cristy5c9e6f22010-09-17 17:31:01 +00003035 const int
3036 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00003037
cristy79cedc72011-07-25 00:41:15 +00003038 register const Quantum
3039 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003040
cristy4c08aed2011-07-01 19:47:50 +00003041 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003042 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003043
cristy79cedc72011-07-25 00:41:15 +00003044 register ssize_t
3045 x;
3046
cristy3ed852e2009-09-05 21:47:34 +00003047 if (status == MagickFalse)
3048 continue;
cristy79cedc72011-07-25 00:41:15 +00003049 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +00003050 q=QueueCacheViewAuthenticPixels(fx_view,0,y,fx_image->columns,1,exception);
cristy79cedc72011-07-25 00:41:15 +00003051 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003052 {
3053 status=MagickFalse;
3054 continue;
3055 }
cristybb503372010-05-27 20:51:26 +00003056 for (x=0; x < (ssize_t) fx_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003057 {
cristy79cedc72011-07-25 00:41:15 +00003058 register ssize_t
3059 i;
3060
3061 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3062 {
3063 MagickRealType
3064 alpha;
3065
3066 PixelChannel
3067 channel;
3068
3069 PixelTrait
3070 fx_traits,
3071 traits;
3072
cristye2a912b2011-12-05 20:02:07 +00003073 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003074 traits=GetPixelChannelMapTraits(image,channel);
cristy79cedc72011-07-25 00:41:15 +00003075 fx_traits=GetPixelChannelMapTraits(fx_image,channel);
cristy010d7d12011-08-31 01:02:48 +00003076 if ((traits == UndefinedPixelTrait) ||
3077 (fx_traits == UndefinedPixelTrait))
cristy79cedc72011-07-25 00:41:15 +00003078 continue;
cristyd09f8802012-02-04 16:44:10 +00003079 if (((fx_traits & CopyPixelTrait) != 0) ||
3080 (GetPixelMask(image,p) != 0))
cristy79cedc72011-07-25 00:41:15 +00003081 {
cristy0beccfa2011-09-25 20:47:53 +00003082 SetPixelChannel(fx_image,channel,p[i],q);
cristy79cedc72011-07-25 00:41:15 +00003083 continue;
3084 }
3085 alpha=0.0;
cristya382aca2011-12-06 18:22:48 +00003086 (void) FxEvaluateChannelExpression(fx_info[id],channel,x,y,&alpha,
3087 exception);
cristyb3a73b52011-07-26 01:34:43 +00003088 q[i]=ClampToQuantum((MagickRealType) QuantumRange*alpha);
cristy79cedc72011-07-25 00:41:15 +00003089 }
3090 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003091 q+=GetPixelChannels(fx_image);
cristy3ed852e2009-09-05 21:47:34 +00003092 }
3093 if (SyncCacheViewAuthenticPixels(fx_view,exception) == MagickFalse)
3094 status=MagickFalse;
3095 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3096 {
3097 MagickBooleanType
3098 proceed;
3099
cristyb5d5f722009-11-04 03:03:49 +00003100#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003101 #pragma omp critical (MagickCore_FxImage)
cristy3ed852e2009-09-05 21:47:34 +00003102#endif
3103 proceed=SetImageProgress(image,FxImageTag,progress++,image->rows);
3104 if (proceed == MagickFalse)
3105 status=MagickFalse;
3106 }
3107 }
cristy3ed852e2009-09-05 21:47:34 +00003108 fx_view=DestroyCacheView(fx_view);
cristy79cedc72011-07-25 00:41:15 +00003109 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003110 fx_info=DestroyFxThreadSet(fx_info);
3111 if (status == MagickFalse)
3112 fx_image=DestroyImage(fx_image);
3113 return(fx_image);
3114}
3115
3116/*
3117%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3118% %
3119% %
3120% %
3121% I m p l o d e I m a g e %
3122% %
3123% %
3124% %
3125%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3126%
3127% ImplodeImage() creates a new image that is a copy of an existing
3128% one with the image pixels "implode" by the specified percentage. It
3129% allocates the memory necessary for the new Image structure and returns a
3130% pointer to the new image.
3131%
3132% The format of the ImplodeImage method is:
3133%
3134% Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003135% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003136%
3137% A description of each parameter follows:
3138%
3139% o implode_image: Method ImplodeImage returns a pointer to the image
3140% after it is implode. A null image is returned if there is a memory
3141% shortage.
3142%
3143% o image: the image.
3144%
3145% o amount: Define the extent of the implosion.
3146%
cristy76f512e2011-09-12 01:26:56 +00003147% o method: the pixel interpolation method.
3148%
cristy3ed852e2009-09-05 21:47:34 +00003149% o exception: return any errors or warnings in this structure.
3150%
3151*/
3152MagickExport Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003153 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003154{
3155#define ImplodeImageTag "Implode/Image"
3156
cristyfa112112010-01-04 17:48:07 +00003157 CacheView
3158 *image_view,
3159 *implode_view;
3160
cristy3ed852e2009-09-05 21:47:34 +00003161 Image
3162 *implode_image;
3163
cristy3ed852e2009-09-05 21:47:34 +00003164 MagickBooleanType
3165 status;
3166
cristybb503372010-05-27 20:51:26 +00003167 MagickOffsetType
3168 progress;
3169
cristy3ed852e2009-09-05 21:47:34 +00003170 MagickRealType
3171 radius;
3172
3173 PointInfo
3174 center,
3175 scale;
3176
cristybb503372010-05-27 20:51:26 +00003177 ssize_t
3178 y;
3179
cristy3ed852e2009-09-05 21:47:34 +00003180 /*
3181 Initialize implode image attributes.
3182 */
3183 assert(image != (Image *) NULL);
3184 assert(image->signature == MagickSignature);
3185 if (image->debug != MagickFalse)
3186 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3187 assert(exception != (ExceptionInfo *) NULL);
3188 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00003189 implode_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3190 exception);
cristy3ed852e2009-09-05 21:47:34 +00003191 if (implode_image == (Image *) NULL)
3192 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003193 if (SetImageStorageClass(implode_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003194 {
cristy3ed852e2009-09-05 21:47:34 +00003195 implode_image=DestroyImage(implode_image);
3196 return((Image *) NULL);
3197 }
cristy4c08aed2011-07-01 19:47:50 +00003198 if (implode_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00003199 implode_image->matte=MagickTrue;
3200 /*
3201 Compute scaling factor.
3202 */
3203 scale.x=1.0;
3204 scale.y=1.0;
3205 center.x=0.5*image->columns;
3206 center.y=0.5*image->rows;
3207 radius=center.x;
3208 if (image->columns > image->rows)
3209 scale.y=(double) image->columns/(double) image->rows;
3210 else
3211 if (image->columns < image->rows)
3212 {
3213 scale.x=(double) image->rows/(double) image->columns;
3214 radius=center.y;
3215 }
3216 /*
3217 Implode image.
3218 */
3219 status=MagickTrue;
3220 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00003221 image_view=AcquireCacheView(image);
3222 implode_view=AcquireCacheView(implode_image);
cristyb5d5f722009-11-04 03:03:49 +00003223#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003224 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003225#endif
cristybb503372010-05-27 20:51:26 +00003226 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003227 {
cristy3ed852e2009-09-05 21:47:34 +00003228 MagickRealType
3229 distance;
3230
3231 PointInfo
3232 delta;
3233
cristy6d188022011-09-12 13:23:33 +00003234 register const Quantum
3235 *restrict p;
3236
cristybb503372010-05-27 20:51:26 +00003237 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003238 x;
3239
cristy4c08aed2011-07-01 19:47:50 +00003240 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003241 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003242
3243 if (status == MagickFalse)
3244 continue;
cristy6d188022011-09-12 13:23:33 +00003245 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +00003246 q=QueueCacheViewAuthenticPixels(implode_view,0,y,implode_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003247 exception);
cristy6d188022011-09-12 13:23:33 +00003248 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003249 {
3250 status=MagickFalse;
3251 continue;
3252 }
cristy3ed852e2009-09-05 21:47:34 +00003253 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00003254 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003255 {
cristy6d188022011-09-12 13:23:33 +00003256 register ssize_t
3257 i;
3258
cristy3ed852e2009-09-05 21:47:34 +00003259 /*
3260 Determine if the pixel is within an ellipse.
3261 */
cristy10a6c612012-01-29 21:41:05 +00003262 if (GetPixelMask(image,p) != 0)
3263 {
3264 p+=GetPixelChannels(image);
3265 q+=GetPixelChannels(implode_image);
3266 continue;
3267 }
cristy3ed852e2009-09-05 21:47:34 +00003268 delta.x=scale.x*(double) (x-center.x);
3269 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00003270 if (distance >= (radius*radius))
cristya6d7a9b2012-01-18 20:04:48 +00003271 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy6d188022011-09-12 13:23:33 +00003272 {
cristya6d7a9b2012-01-18 20:04:48 +00003273 PixelChannel
3274 channel;
3275
cristy1707c6c2012-01-18 23:30:54 +00003276 PixelTrait
3277 implode_traits,
3278 traits;
3279
cristya6d7a9b2012-01-18 20:04:48 +00003280 channel=GetPixelChannelMapChannel(image,i);
cristy1707c6c2012-01-18 23:30:54 +00003281 traits=GetPixelChannelMapTraits(image,channel);
3282 implode_traits=GetPixelChannelMapTraits(implode_image,channel);
3283 if ((traits == UndefinedPixelTrait) ||
3284 (implode_traits == UndefinedPixelTrait))
3285 continue;
cristya6d7a9b2012-01-18 20:04:48 +00003286 SetPixelChannel(implode_image,channel,p[i],q);
cristy6d188022011-09-12 13:23:33 +00003287 }
3288 else
cristy3ed852e2009-09-05 21:47:34 +00003289 {
3290 double
3291 factor;
3292
3293 /*
3294 Implode the pixel.
3295 */
3296 factor=1.0;
3297 if (distance > 0.0)
cristy1707c6c2012-01-18 23:30:54 +00003298 factor=pow(sin((double) (MagickPI*sqrt((double) distance)/radius/
3299 2)),-amount);
cristy76f512e2011-09-12 01:26:56 +00003300 status=InterpolatePixelChannels(image,image_view,implode_image,method,
3301 (double) (factor*delta.x/scale.x+center.x),(double) (factor*delta.y/
3302 scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00003303 }
cristy6d188022011-09-12 13:23:33 +00003304 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003305 q+=GetPixelChannels(implode_image);
cristy3ed852e2009-09-05 21:47:34 +00003306 }
3307 if (SyncCacheViewAuthenticPixels(implode_view,exception) == MagickFalse)
3308 status=MagickFalse;
3309 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3310 {
3311 MagickBooleanType
3312 proceed;
3313
cristyb5d5f722009-11-04 03:03:49 +00003314#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003315 #pragma omp critical (MagickCore_ImplodeImage)
cristy3ed852e2009-09-05 21:47:34 +00003316#endif
3317 proceed=SetImageProgress(image,ImplodeImageTag,progress++,image->rows);
3318 if (proceed == MagickFalse)
3319 status=MagickFalse;
3320 }
3321 }
3322 implode_view=DestroyCacheView(implode_view);
3323 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003324 if (status == MagickFalse)
3325 implode_image=DestroyImage(implode_image);
3326 return(implode_image);
3327}
3328
3329/*
3330%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3331% %
3332% %
3333% %
3334% M o r p h I m a g e s %
3335% %
3336% %
3337% %
3338%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3339%
3340% The MorphImages() method requires a minimum of two images. The first
3341% image is transformed into the second by a number of intervening images
3342% as specified by frames.
3343%
3344% The format of the MorphImage method is:
3345%
cristybb503372010-05-27 20:51:26 +00003346% Image *MorphImages(const Image *image,const size_t number_frames,
cristy3ed852e2009-09-05 21:47:34 +00003347% ExceptionInfo *exception)
3348%
3349% A description of each parameter follows:
3350%
3351% o image: the image.
3352%
3353% o number_frames: Define the number of in-between image to generate.
3354% The more in-between frames, the smoother the morph.
3355%
3356% o exception: return any errors or warnings in this structure.
3357%
3358*/
3359MagickExport Image *MorphImages(const Image *image,
cristybb503372010-05-27 20:51:26 +00003360 const size_t number_frames,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003361{
3362#define MorphImageTag "Morph/Image"
3363
3364 Image
3365 *morph_image,
3366 *morph_images;
3367
cristy9d314ff2011-03-09 01:30:28 +00003368 MagickBooleanType
3369 status;
cristy3ed852e2009-09-05 21:47:34 +00003370
3371 MagickOffsetType
3372 scene;
3373
3374 MagickRealType
3375 alpha,
3376 beta;
3377
3378 register const Image
3379 *next;
3380
cristybb503372010-05-27 20:51:26 +00003381 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003382 i;
3383
cristy9d314ff2011-03-09 01:30:28 +00003384 ssize_t
3385 y;
cristy3ed852e2009-09-05 21:47:34 +00003386
3387 /*
3388 Clone first frame in sequence.
3389 */
3390 assert(image != (Image *) NULL);
3391 assert(image->signature == MagickSignature);
3392 if (image->debug != MagickFalse)
3393 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3394 assert(exception != (ExceptionInfo *) NULL);
3395 assert(exception->signature == MagickSignature);
3396 morph_images=CloneImage(image,0,0,MagickTrue,exception);
3397 if (morph_images == (Image *) NULL)
3398 return((Image *) NULL);
3399 if (GetNextImageInList(image) == (Image *) NULL)
3400 {
3401 /*
3402 Morph single image.
3403 */
cristybb503372010-05-27 20:51:26 +00003404 for (i=1; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003405 {
3406 morph_image=CloneImage(image,0,0,MagickTrue,exception);
3407 if (morph_image == (Image *) NULL)
3408 {
3409 morph_images=DestroyImageList(morph_images);
3410 return((Image *) NULL);
3411 }
3412 AppendImageToList(&morph_images,morph_image);
cristy8b27a6d2010-02-14 03:31:15 +00003413 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003414 {
cristy8b27a6d2010-02-14 03:31:15 +00003415 MagickBooleanType
3416 proceed;
3417
cristybb503372010-05-27 20:51:26 +00003418 proceed=SetImageProgress(image,MorphImageTag,(MagickOffsetType) i,
3419 number_frames);
cristy8b27a6d2010-02-14 03:31:15 +00003420 if (proceed == MagickFalse)
3421 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00003422 }
3423 }
3424 return(GetFirstImageInList(morph_images));
3425 }
3426 /*
3427 Morph image sequence.
3428 */
3429 status=MagickTrue;
3430 scene=0;
3431 next=image;
3432 for ( ; GetNextImageInList(next) != (Image *) NULL; next=GetNextImageInList(next))
3433 {
cristybb503372010-05-27 20:51:26 +00003434 for (i=0; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003435 {
3436 CacheView
3437 *image_view,
3438 *morph_view;
3439
3440 beta=(MagickRealType) (i+1.0)/(MagickRealType) (number_frames+1.0);
3441 alpha=1.0-beta;
cristy15b98cd2010-09-12 19:42:50 +00003442 morph_image=ResizeImage(next,(size_t) (alpha*next->columns+beta*
cristyaa2c16c2012-03-25 22:21:35 +00003443 GetNextImageInList(next)->columns+0.5),(size_t) (alpha*next->rows+beta*
3444 GetNextImageInList(next)->rows+0.5),next->filter,exception);
cristy3ed852e2009-09-05 21:47:34 +00003445 if (morph_image == (Image *) NULL)
3446 {
3447 morph_images=DestroyImageList(morph_images);
3448 return((Image *) NULL);
3449 }
cristy1707c6c2012-01-18 23:30:54 +00003450 status=SetImageStorageClass(morph_image,DirectClass,exception);
3451 if (status == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003452 {
cristy3ed852e2009-09-05 21:47:34 +00003453 morph_image=DestroyImage(morph_image);
3454 return((Image *) NULL);
3455 }
3456 AppendImageToList(&morph_images,morph_image);
3457 morph_images=GetLastImageInList(morph_images);
cristy15b98cd2010-09-12 19:42:50 +00003458 morph_image=ResizeImage(GetNextImageInList(next),morph_images->columns,
cristyaa2c16c2012-03-25 22:21:35 +00003459 morph_images->rows,GetNextImageInList(next)->filter,exception);
cristy3ed852e2009-09-05 21:47:34 +00003460 if (morph_image == (Image *) NULL)
3461 {
3462 morph_images=DestroyImageList(morph_images);
3463 return((Image *) NULL);
3464 }
3465 image_view=AcquireCacheView(morph_image);
3466 morph_view=AcquireCacheView(morph_images);
cristyb5d5f722009-11-04 03:03:49 +00003467#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003468 #pragma omp parallel for schedule(static,4) shared(status)
cristy3ed852e2009-09-05 21:47:34 +00003469#endif
cristybb503372010-05-27 20:51:26 +00003470 for (y=0; y < (ssize_t) morph_images->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003471 {
3472 MagickBooleanType
3473 sync;
3474
cristy4c08aed2011-07-01 19:47:50 +00003475 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003476 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003477
cristybb503372010-05-27 20:51:26 +00003478 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003479 x;
3480
cristy4c08aed2011-07-01 19:47:50 +00003481 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003482 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003483
3484 if (status == MagickFalse)
3485 continue;
3486 p=GetCacheViewVirtualPixels(image_view,0,y,morph_image->columns,1,
3487 exception);
3488 q=GetCacheViewAuthenticPixels(morph_view,0,y,morph_images->columns,1,
3489 exception);
cristy4c08aed2011-07-01 19:47:50 +00003490 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003491 {
3492 status=MagickFalse;
3493 continue;
3494 }
cristybb503372010-05-27 20:51:26 +00003495 for (x=0; x < (ssize_t) morph_images->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003496 {
cristy1707c6c2012-01-18 23:30:54 +00003497 register ssize_t
3498 i;
3499
cristy10a6c612012-01-29 21:41:05 +00003500 for (i=0; i < (ssize_t) GetPixelChannels(morph_image); i++)
cristy1707c6c2012-01-18 23:30:54 +00003501 {
3502 PixelChannel
3503 channel;
3504
3505 PixelTrait
3506 morph_traits,
3507 traits;
3508
3509 channel=GetPixelChannelMapChannel(image,i);
3510 traits=GetPixelChannelMapTraits(image,channel);
3511 morph_traits=GetPixelChannelMapTraits(morph_image,channel);
3512 if ((traits == UndefinedPixelTrait) ||
3513 (morph_traits == UndefinedPixelTrait))
3514 continue;
cristyd09f8802012-02-04 16:44:10 +00003515 if (((morph_traits & CopyPixelTrait) != 0) ||
3516 (GetPixelMask(image,p) != 0))
cristy1707c6c2012-01-18 23:30:54 +00003517 {
3518 SetPixelChannel(morph_image,channel,p[i],q);
3519 continue;
3520 }
3521 SetPixelChannel(morph_image,channel,ClampToQuantum(alpha*
3522 GetPixelChannel(morph_images,channel,q)+beta*p[i]),q);
3523 }
cristyed231572011-07-14 02:18:59 +00003524 p+=GetPixelChannels(morph_image);
3525 q+=GetPixelChannels(morph_images);
cristy3ed852e2009-09-05 21:47:34 +00003526 }
3527 sync=SyncCacheViewAuthenticPixels(morph_view,exception);
3528 if (sync == MagickFalse)
3529 status=MagickFalse;
3530 }
3531 morph_view=DestroyCacheView(morph_view);
3532 image_view=DestroyCacheView(image_view);
3533 morph_image=DestroyImage(morph_image);
3534 }
cristybb503372010-05-27 20:51:26 +00003535 if (i < (ssize_t) number_frames)
cristy3ed852e2009-09-05 21:47:34 +00003536 break;
3537 /*
3538 Clone last frame in sequence.
3539 */
3540 morph_image=CloneImage(GetNextImageInList(next),0,0,MagickTrue,exception);
3541 if (morph_image == (Image *) NULL)
3542 {
3543 morph_images=DestroyImageList(morph_images);
3544 return((Image *) NULL);
3545 }
3546 AppendImageToList(&morph_images,morph_image);
3547 morph_images=GetLastImageInList(morph_images);
3548 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3549 {
3550 MagickBooleanType
3551 proceed;
3552
cristyb5d5f722009-11-04 03:03:49 +00003553#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003554 #pragma omp critical (MagickCore_MorphImages)
cristy3ed852e2009-09-05 21:47:34 +00003555#endif
3556 proceed=SetImageProgress(image,MorphImageTag,scene,
3557 GetImageListLength(image));
3558 if (proceed == MagickFalse)
3559 status=MagickFalse;
3560 }
3561 scene++;
3562 }
3563 if (GetNextImageInList(next) != (Image *) NULL)
3564 {
3565 morph_images=DestroyImageList(morph_images);
3566 return((Image *) NULL);
3567 }
3568 return(GetFirstImageInList(morph_images));
3569}
3570
3571/*
3572%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3573% %
3574% %
3575% %
3576% P l a s m a I m a g e %
3577% %
3578% %
3579% %
3580%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3581%
3582% PlasmaImage() initializes an image with plasma fractal values. The image
3583% must be initialized with a base color and the random number generator
3584% seeded before this method is called.
3585%
3586% The format of the PlasmaImage method is:
3587%
3588% MagickBooleanType PlasmaImage(Image *image,const SegmentInfo *segment,
cristy5cbc0162011-08-29 00:36:28 +00003589% size_t attenuate,size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003590%
3591% A description of each parameter follows:
3592%
3593% o image: the image.
3594%
3595% o segment: Define the region to apply plasma fractals values.
3596%
glennrp7dae1ca2010-09-16 12:17:35 +00003597% o attenuate: Define the plasma attenuation factor.
cristy3ed852e2009-09-05 21:47:34 +00003598%
3599% o depth: Limit the plasma recursion depth.
3600%
cristy5cbc0162011-08-29 00:36:28 +00003601% o exception: return any errors or warnings in this structure.
3602%
cristy3ed852e2009-09-05 21:47:34 +00003603*/
3604
3605static inline Quantum PlasmaPixel(RandomInfo *random_info,
3606 const MagickRealType pixel,const MagickRealType noise)
3607{
3608 Quantum
3609 plasma;
3610
cristyce70c172010-01-07 17:15:30 +00003611 plasma=ClampToQuantum(pixel+noise*GetPseudoRandomValue(random_info)-
cristy3ed852e2009-09-05 21:47:34 +00003612 noise/2.0);
3613 return(plasma);
3614}
3615
cristyda1f9c12011-10-02 21:39:49 +00003616static MagickBooleanType PlasmaImageProxy(Image *image,CacheView *image_view,
3617 CacheView *u_view,CacheView *v_view,RandomInfo *random_info,
3618 const SegmentInfo *segment,size_t attenuate,size_t depth,
3619 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003620{
cristy3ed852e2009-09-05 21:47:34 +00003621 MagickRealType
3622 plasma;
3623
cristyda1f9c12011-10-02 21:39:49 +00003624 PixelChannel
3625 channel;
3626
3627 PixelTrait
3628 traits;
3629
3630 register const Quantum
3631 *restrict u,
3632 *restrict v;
3633
3634 register Quantum
3635 *restrict q;
3636
3637 register ssize_t
3638 i;
cristy3ed852e2009-09-05 21:47:34 +00003639
cristy9d314ff2011-03-09 01:30:28 +00003640 ssize_t
3641 x,
3642 x_mid,
3643 y,
3644 y_mid;
3645
cristy3ed852e2009-09-05 21:47:34 +00003646 if (((segment->x2-segment->x1) == 0.0) && ((segment->y2-segment->y1) == 0.0))
3647 return(MagickTrue);
3648 if (depth != 0)
3649 {
3650 SegmentInfo
3651 local_info;
3652
3653 /*
3654 Divide the area into quadrants and recurse.
3655 */
3656 depth--;
3657 attenuate++;
cristybb503372010-05-27 20:51:26 +00003658 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3659 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003660 local_info=(*segment);
3661 local_info.x2=(double) x_mid;
3662 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003663 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3664 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003665 local_info=(*segment);
3666 local_info.y1=(double) y_mid;
3667 local_info.x2=(double) x_mid;
cristyda1f9c12011-10-02 21:39:49 +00003668 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3669 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003670 local_info=(*segment);
3671 local_info.x1=(double) x_mid;
3672 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003673 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3674 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003675 local_info=(*segment);
3676 local_info.x1=(double) x_mid;
3677 local_info.y1=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003678 return(PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3679 &local_info,attenuate,depth,exception));
cristy3ed852e2009-09-05 21:47:34 +00003680 }
cristybb503372010-05-27 20:51:26 +00003681 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3682 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003683 if ((segment->x1 == (double) x_mid) && (segment->x2 == (double) x_mid) &&
3684 (segment->y1 == (double) y_mid) && (segment->y2 == (double) y_mid))
3685 return(MagickFalse);
3686 /*
3687 Average pixels and apply plasma.
3688 */
cristy3ed852e2009-09-05 21:47:34 +00003689 plasma=(MagickRealType) QuantumRange/(2.0*attenuate);
3690 if ((segment->x1 != (double) x_mid) || (segment->x2 != (double) x_mid))
3691 {
cristy3ed852e2009-09-05 21:47:34 +00003692 /*
3693 Left pixel.
3694 */
cristybb503372010-05-27 20:51:26 +00003695 x=(ssize_t) ceil(segment->x1-0.5);
cristy1707c6c2012-01-18 23:30:54 +00003696 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),1,1,
3697 exception);
3698 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),1,1,
3699 exception);
cristyc5c6f662010-09-22 14:23:02 +00003700 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003701 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3702 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003703 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003704 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3705 {
cristye2a912b2011-12-05 20:02:07 +00003706 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003707 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003708 if (traits == UndefinedPixelTrait)
3709 continue;
3710 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3711 }
cristyc5c6f662010-09-22 14:23:02 +00003712 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003713 if (segment->x1 != segment->x2)
3714 {
3715 /*
3716 Right pixel.
3717 */
cristybb503372010-05-27 20:51:26 +00003718 x=(ssize_t) ceil(segment->x2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003719 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3720 1,1,exception);
3721 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3722 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003723 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003724 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3725 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003726 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003727 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3728 {
cristye2a912b2011-12-05 20:02:07 +00003729 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003730 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003731 if (traits == UndefinedPixelTrait)
3732 continue;
3733 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3734 }
cristyc5c6f662010-09-22 14:23:02 +00003735 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003736 }
3737 }
3738 if ((segment->y1 != (double) y_mid) || (segment->y2 != (double) y_mid))
3739 {
3740 if ((segment->x1 != (double) x_mid) || (segment->y2 != (double) y_mid))
3741 {
cristy3ed852e2009-09-05 21:47:34 +00003742 /*
3743 Bottom pixel.
3744 */
cristybb503372010-05-27 20:51:26 +00003745 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003746 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3747 1,1,exception);
3748 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3749 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003750 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003751 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3752 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003753 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003754 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3755 {
cristye2a912b2011-12-05 20:02:07 +00003756 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003757 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003758 if (traits == UndefinedPixelTrait)
3759 continue;
3760 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3761 }
cristyc5c6f662010-09-22 14:23:02 +00003762 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003763 }
3764 if (segment->y1 != segment->y2)
3765 {
cristy3ed852e2009-09-05 21:47:34 +00003766 /*
3767 Top pixel.
3768 */
cristybb503372010-05-27 20:51:26 +00003769 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003770 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3771 1,1,exception);
3772 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3773 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003774 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003775 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3776 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003777 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003778 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3779 {
cristye2a912b2011-12-05 20:02:07 +00003780 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003781 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003782 if (traits == UndefinedPixelTrait)
3783 continue;
3784 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3785 }
cristyc5c6f662010-09-22 14:23:02 +00003786 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003787 }
3788 }
3789 if ((segment->x1 != segment->x2) || (segment->y1 != segment->y2))
3790 {
cristy3ed852e2009-09-05 21:47:34 +00003791 /*
3792 Middle pixel.
3793 */
cristybb503372010-05-27 20:51:26 +00003794 x=(ssize_t) ceil(segment->x1-0.5);
3795 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003796 u=GetCacheViewVirtualPixels(u_view,x,y,1,1,exception);
cristybb503372010-05-27 20:51:26 +00003797 x=(ssize_t) ceil(segment->x2-0.5);
3798 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003799 v=GetCacheViewVirtualPixels(v_view,x,y,1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003800 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003801 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3802 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003803 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003804 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3805 {
cristye2a912b2011-12-05 20:02:07 +00003806 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003807 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003808 if (traits == UndefinedPixelTrait)
3809 continue;
3810 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3811 }
cristyc5c6f662010-09-22 14:23:02 +00003812 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003813 }
3814 if (((segment->x2-segment->x1) < 3.0) && ((segment->y2-segment->y1) < 3.0))
3815 return(MagickTrue);
3816 return(MagickFalse);
3817}
cristyda1f9c12011-10-02 21:39:49 +00003818
cristy3ed852e2009-09-05 21:47:34 +00003819MagickExport MagickBooleanType PlasmaImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00003820 const SegmentInfo *segment,size_t attenuate,size_t depth,
3821 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003822{
cristyc5c6f662010-09-22 14:23:02 +00003823 CacheView
cristyda1f9c12011-10-02 21:39:49 +00003824 *image_view,
3825 *u_view,
3826 *v_view;
cristyc5c6f662010-09-22 14:23:02 +00003827
cristy3ed852e2009-09-05 21:47:34 +00003828 MagickBooleanType
3829 status;
3830
3831 RandomInfo
3832 *random_info;
3833
3834 if (image->debug != MagickFalse)
3835 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3836 assert(image != (Image *) NULL);
3837 assert(image->signature == MagickSignature);
3838 if (image->debug != MagickFalse)
3839 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
cristy5cbc0162011-08-29 00:36:28 +00003840 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristyc5c6f662010-09-22 14:23:02 +00003841 return(MagickFalse);
3842 image_view=AcquireCacheView(image);
cristyda1f9c12011-10-02 21:39:49 +00003843 u_view=AcquireCacheView(image);
3844 v_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00003845 random_info=AcquireRandomInfo();
cristyda1f9c12011-10-02 21:39:49 +00003846 status=PlasmaImageProxy(image,image_view,u_view,v_view,random_info,segment,
3847 attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003848 random_info=DestroyRandomInfo(random_info);
cristyda1f9c12011-10-02 21:39:49 +00003849 v_view=DestroyCacheView(v_view);
3850 u_view=DestroyCacheView(u_view);
cristyc5c6f662010-09-22 14:23:02 +00003851 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003852 return(status);
3853}
3854
3855/*
3856%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3857% %
3858% %
3859% %
3860% P o l a r o i d I m a g e %
3861% %
3862% %
3863% %
3864%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3865%
3866% PolaroidImage() simulates a Polaroid picture.
3867%
3868% The format of the AnnotateImage method is:
3869%
3870% Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristye9e3d382011-12-14 01:50:13 +00003871% const char *caption,const double angle,
3872% const PixelInterpolateMethod method,ExceptionInfo exception)
cristy3ed852e2009-09-05 21:47:34 +00003873%
3874% A description of each parameter follows:
3875%
3876% o image: the image.
3877%
3878% o draw_info: the draw info.
3879%
cristye9e3d382011-12-14 01:50:13 +00003880% o caption: the Polaroid caption.
3881%
cristycee97112010-05-28 00:44:52 +00003882% o angle: Apply the effect along this angle.
cristy3ed852e2009-09-05 21:47:34 +00003883%
cristy5c4e2582011-09-11 19:21:03 +00003884% o method: the pixel interpolation method.
3885%
cristy3ed852e2009-09-05 21:47:34 +00003886% o exception: return any errors or warnings in this structure.
3887%
3888*/
3889MagickExport Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristye9e3d382011-12-14 01:50:13 +00003890 const char *caption,const double angle,const PixelInterpolateMethod method,
cristy5c4e2582011-09-11 19:21:03 +00003891 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003892{
cristy3ed852e2009-09-05 21:47:34 +00003893 Image
3894 *bend_image,
3895 *caption_image,
3896 *flop_image,
3897 *picture_image,
3898 *polaroid_image,
3899 *rotate_image,
3900 *trim_image;
3901
cristybb503372010-05-27 20:51:26 +00003902 size_t
cristy3ed852e2009-09-05 21:47:34 +00003903 height;
3904
cristy9d314ff2011-03-09 01:30:28 +00003905 ssize_t
3906 quantum;
3907
cristy3ed852e2009-09-05 21:47:34 +00003908 /*
3909 Simulate a Polaroid picture.
3910 */
3911 assert(image != (Image *) NULL);
3912 assert(image->signature == MagickSignature);
3913 if (image->debug != MagickFalse)
3914 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3915 assert(exception != (ExceptionInfo *) NULL);
3916 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00003917 quantum=(ssize_t) MagickMax(MagickMax((double) image->columns,(double)
cristy3ed852e2009-09-05 21:47:34 +00003918 image->rows)/25.0,10.0);
3919 height=image->rows+2*quantum;
3920 caption_image=(Image *) NULL;
cristye9e3d382011-12-14 01:50:13 +00003921 if (caption != (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003922 {
3923 char
cristye9e3d382011-12-14 01:50:13 +00003924 geometry[MaxTextExtent],
3925 *text;
cristy3ed852e2009-09-05 21:47:34 +00003926
3927 DrawInfo
3928 *annotate_info;
3929
cristy3ed852e2009-09-05 21:47:34 +00003930 MagickBooleanType
3931 status;
3932
cristy9d314ff2011-03-09 01:30:28 +00003933 ssize_t
3934 count;
3935
cristy3ed852e2009-09-05 21:47:34 +00003936 TypeMetric
3937 metrics;
3938
3939 /*
3940 Generate caption image.
3941 */
3942 caption_image=CloneImage(image,image->columns,1,MagickTrue,exception);
3943 if (caption_image == (Image *) NULL)
3944 return((Image *) NULL);
3945 annotate_info=CloneDrawInfo((const ImageInfo *) NULL,draw_info);
cristye9e3d382011-12-14 01:50:13 +00003946 text=InterpretImageProperties((ImageInfo *) NULL,(Image *) image,caption,
3947 exception);
3948 (void) CloneString(&annotate_info->text,text);
cristy6b1d05e2010-09-22 19:17:27 +00003949 count=FormatMagickCaption(caption_image,annotate_info,MagickTrue,&metrics,
cristye9e3d382011-12-14 01:50:13 +00003950 &text,exception);
3951 status=SetImageExtent(caption_image,image->columns,(size_t) ((count+1)*
3952 (metrics.ascent-metrics.descent)+0.5),exception);
cristy3ed852e2009-09-05 21:47:34 +00003953 if (status == MagickFalse)
3954 caption_image=DestroyImage(caption_image);
3955 else
3956 {
3957 caption_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00003958 (void) SetImageBackgroundColor(caption_image,exception);
cristye9e3d382011-12-14 01:50:13 +00003959 (void) CloneString(&annotate_info->text,text);
cristyb51dff52011-05-19 16:55:47 +00003960 (void) FormatLocaleString(geometry,MaxTextExtent,"+0+%g",
cristy3ed852e2009-09-05 21:47:34 +00003961 metrics.ascent);
3962 if (annotate_info->gravity == UndefinedGravity)
3963 (void) CloneString(&annotate_info->geometry,AcquireString(
3964 geometry));
cristy5cbc0162011-08-29 00:36:28 +00003965 (void) AnnotateImage(caption_image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00003966 height+=caption_image->rows;
3967 }
3968 annotate_info=DestroyDrawInfo(annotate_info);
cristye9e3d382011-12-14 01:50:13 +00003969 text=DestroyString(text);
cristy3ed852e2009-09-05 21:47:34 +00003970 }
3971 picture_image=CloneImage(image,image->columns+2*quantum,height,MagickTrue,
3972 exception);
3973 if (picture_image == (Image *) NULL)
3974 {
3975 if (caption_image != (Image *) NULL)
3976 caption_image=DestroyImage(caption_image);
3977 return((Image *) NULL);
3978 }
3979 picture_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00003980 (void) SetImageBackgroundColor(picture_image,exception);
cristy39172402012-03-30 13:04:39 +00003981 (void) CompositeImage(picture_image,image,OverCompositeOp,MagickTrue,quantum,
cristyfeb3e962012-03-29 17:25:55 +00003982 quantum,exception);
cristy3ed852e2009-09-05 21:47:34 +00003983 if (caption_image != (Image *) NULL)
3984 {
cristyfeb3e962012-03-29 17:25:55 +00003985 (void) CompositeImage(picture_image,caption_image,OverCompositeOp,
cristy39172402012-03-30 13:04:39 +00003986 MagickTrue,quantum,(ssize_t) (image->rows+3*quantum/2),exception);
cristy3ed852e2009-09-05 21:47:34 +00003987 caption_image=DestroyImage(caption_image);
3988 }
cristy9950d572011-10-01 18:22:35 +00003989 (void) QueryColorCompliance("none",AllCompliance,
3990 &picture_image->background_color,exception);
cristy63240882011-08-05 19:05:27 +00003991 (void) SetImageAlphaChannel(picture_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00003992 rotate_image=RotateImage(picture_image,90.0,exception);
3993 picture_image=DestroyImage(picture_image);
3994 if (rotate_image == (Image *) NULL)
3995 return((Image *) NULL);
3996 picture_image=rotate_image;
3997 bend_image=WaveImage(picture_image,0.01*picture_image->rows,2.0*
cristy5c4e2582011-09-11 19:21:03 +00003998 picture_image->columns,method,exception);
cristy3ed852e2009-09-05 21:47:34 +00003999 picture_image=DestroyImage(picture_image);
4000 if (bend_image == (Image *) NULL)
4001 return((Image *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00004002 picture_image=bend_image;
4003 rotate_image=RotateImage(picture_image,-90.0,exception);
4004 picture_image=DestroyImage(picture_image);
4005 if (rotate_image == (Image *) NULL)
4006 return((Image *) NULL);
4007 picture_image=rotate_image;
4008 picture_image->background_color=image->background_color;
anthonyf46d4262012-03-26 03:30:34 +00004009 polaroid_image=ShadowImage(picture_image,80.0,2.0,quantum/3,quantum/3,
cristy3ed852e2009-09-05 21:47:34 +00004010 exception);
4011 if (polaroid_image == (Image *) NULL)
4012 {
4013 picture_image=DestroyImage(picture_image);
4014 return(picture_image);
4015 }
4016 flop_image=FlopImage(polaroid_image,exception);
4017 polaroid_image=DestroyImage(polaroid_image);
4018 if (flop_image == (Image *) NULL)
4019 {
4020 picture_image=DestroyImage(picture_image);
4021 return(picture_image);
4022 }
4023 polaroid_image=flop_image;
cristyfeb3e962012-03-29 17:25:55 +00004024 (void) CompositeImage(polaroid_image,picture_image,OverCompositeOp,
cristy39172402012-03-30 13:04:39 +00004025 MagickTrue,(ssize_t) (-0.01*picture_image->columns/2.0),0L,exception);
cristy3ed852e2009-09-05 21:47:34 +00004026 picture_image=DestroyImage(picture_image);
cristy9950d572011-10-01 18:22:35 +00004027 (void) QueryColorCompliance("none",AllCompliance,
4028 &polaroid_image->background_color,exception);
cristy3ed852e2009-09-05 21:47:34 +00004029 rotate_image=RotateImage(polaroid_image,angle,exception);
4030 polaroid_image=DestroyImage(polaroid_image);
4031 if (rotate_image == (Image *) NULL)
4032 return((Image *) NULL);
4033 polaroid_image=rotate_image;
4034 trim_image=TrimImage(polaroid_image,exception);
4035 polaroid_image=DestroyImage(polaroid_image);
4036 if (trim_image == (Image *) NULL)
4037 return((Image *) NULL);
4038 polaroid_image=trim_image;
4039 return(polaroid_image);
4040}
4041
4042/*
4043%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4044% %
4045% %
4046% %
cristy3ed852e2009-09-05 21:47:34 +00004047% S e p i a T o n e I m a g e %
4048% %
4049% %
4050% %
4051%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4052%
4053% MagickSepiaToneImage() applies a special effect to the image, similar to the
4054% effect achieved in a photo darkroom by sepia toning. Threshold ranges from
4055% 0 to QuantumRange and is a measure of the extent of the sepia toning. A
4056% threshold of 80% is a good starting point for a reasonable tone.
4057%
4058% The format of the SepiaToneImage method is:
4059%
4060% Image *SepiaToneImage(const Image *image,const double threshold,
4061% ExceptionInfo *exception)
4062%
4063% A description of each parameter follows:
4064%
4065% o image: the image.
4066%
4067% o threshold: the tone threshold.
4068%
4069% o exception: return any errors or warnings in this structure.
4070%
4071*/
4072MagickExport Image *SepiaToneImage(const Image *image,const double threshold,
4073 ExceptionInfo *exception)
4074{
4075#define SepiaToneImageTag "SepiaTone/Image"
4076
cristyc4c8d132010-01-07 01:58:38 +00004077 CacheView
4078 *image_view,
4079 *sepia_view;
4080
cristy3ed852e2009-09-05 21:47:34 +00004081 Image
4082 *sepia_image;
4083
cristy3ed852e2009-09-05 21:47:34 +00004084 MagickBooleanType
4085 status;
4086
cristybb503372010-05-27 20:51:26 +00004087 MagickOffsetType
4088 progress;
4089
4090 ssize_t
4091 y;
4092
cristy3ed852e2009-09-05 21:47:34 +00004093 /*
4094 Initialize sepia-toned image attributes.
4095 */
4096 assert(image != (const Image *) NULL);
4097 assert(image->signature == MagickSignature);
4098 if (image->debug != MagickFalse)
4099 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4100 assert(exception != (ExceptionInfo *) NULL);
4101 assert(exception->signature == MagickSignature);
cristy1707c6c2012-01-18 23:30:54 +00004102 sepia_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004103 if (sepia_image == (Image *) NULL)
4104 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004105 if (SetImageStorageClass(sepia_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004106 {
cristy3ed852e2009-09-05 21:47:34 +00004107 sepia_image=DestroyImage(sepia_image);
4108 return((Image *) NULL);
4109 }
4110 /*
4111 Tone each row of the image.
4112 */
4113 status=MagickTrue;
4114 progress=0;
4115 image_view=AcquireCacheView(image);
4116 sepia_view=AcquireCacheView(sepia_image);
cristyb5d5f722009-11-04 03:03:49 +00004117#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00004118 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00004119#endif
cristybb503372010-05-27 20:51:26 +00004120 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004121 {
cristy4c08aed2011-07-01 19:47:50 +00004122 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004123 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00004124
cristybb503372010-05-27 20:51:26 +00004125 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004126 x;
4127
cristy4c08aed2011-07-01 19:47:50 +00004128 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004129 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004130
4131 if (status == MagickFalse)
4132 continue;
4133 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy1707c6c2012-01-18 23:30:54 +00004134 q=GetCacheViewAuthenticPixels(sepia_view,0,y,sepia_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00004135 exception);
cristy4c08aed2011-07-01 19:47:50 +00004136 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004137 {
4138 status=MagickFalse;
4139 continue;
4140 }
cristybb503372010-05-27 20:51:26 +00004141 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004142 {
4143 MagickRealType
4144 intensity,
4145 tone;
4146
cristy4c08aed2011-07-01 19:47:50 +00004147 intensity=(MagickRealType) GetPixelIntensity(image,p);
cristy3ed852e2009-09-05 21:47:34 +00004148 tone=intensity > threshold ? (MagickRealType) QuantumRange : intensity+
4149 (MagickRealType) QuantumRange-threshold;
cristy4c08aed2011-07-01 19:47:50 +00004150 SetPixelRed(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004151 tone=intensity > (7.0*threshold/6.0) ? (MagickRealType) QuantumRange :
4152 intensity+(MagickRealType) QuantumRange-7.0*threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004153 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004154 tone=intensity < (threshold/6.0) ? 0 : intensity-threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004155 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004156 tone=threshold/7.0;
cristy4c08aed2011-07-01 19:47:50 +00004157 if ((MagickRealType) GetPixelGreen(image,q) < tone)
4158 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
4159 if ((MagickRealType) GetPixelBlue(image,q) < tone)
4160 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristyed231572011-07-14 02:18:59 +00004161 p+=GetPixelChannels(image);
4162 q+=GetPixelChannels(sepia_image);
cristy3ed852e2009-09-05 21:47:34 +00004163 }
4164 if (SyncCacheViewAuthenticPixels(sepia_view,exception) == MagickFalse)
4165 status=MagickFalse;
4166 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4167 {
4168 MagickBooleanType
4169 proceed;
4170
cristyb5d5f722009-11-04 03:03:49 +00004171#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00004172 #pragma omp critical (MagickCore_SepiaToneImage)
cristy3ed852e2009-09-05 21:47:34 +00004173#endif
4174 proceed=SetImageProgress(image,SepiaToneImageTag,progress++,
4175 image->rows);
4176 if (proceed == MagickFalse)
4177 status=MagickFalse;
4178 }
4179 }
4180 sepia_view=DestroyCacheView(sepia_view);
4181 image_view=DestroyCacheView(image_view);
cristye23ec9d2011-08-16 18:15:40 +00004182 (void) NormalizeImage(sepia_image,exception);
4183 (void) ContrastImage(sepia_image,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004184 if (status == MagickFalse)
4185 sepia_image=DestroyImage(sepia_image);
4186 return(sepia_image);
4187}
4188
4189/*
4190%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4191% %
4192% %
4193% %
4194% S h a d o w I m a g e %
4195% %
4196% %
4197% %
4198%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4199%
4200% ShadowImage() simulates a shadow from the specified image and returns it.
4201%
4202% The format of the ShadowImage method is:
4203%
cristy70cddf72011-12-10 22:42:42 +00004204% Image *ShadowImage(const Image *image,const double alpha,
cristyaa2c16c2012-03-25 22:21:35 +00004205% const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4206% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004207%
4208% A description of each parameter follows:
4209%
4210% o image: the image.
4211%
cristy70cddf72011-12-10 22:42:42 +00004212% o alpha: percentage transparency.
cristy3ed852e2009-09-05 21:47:34 +00004213%
4214% o sigma: the standard deviation of the Gaussian, in pixels.
4215%
4216% o x_offset: the shadow x-offset.
4217%
4218% o y_offset: the shadow y-offset.
4219%
4220% o exception: return any errors or warnings in this structure.
4221%
4222*/
cristy70cddf72011-12-10 22:42:42 +00004223MagickExport Image *ShadowImage(const Image *image,const double alpha,
cristyaa2c16c2012-03-25 22:21:35 +00004224 const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4225 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004226{
4227#define ShadowImageTag "Shadow/Image"
4228
cristy70cddf72011-12-10 22:42:42 +00004229 CacheView
4230 *image_view;
4231
cristybd5a96c2011-08-21 00:04:26 +00004232 ChannelType
4233 channel_mask;
4234
cristy3ed852e2009-09-05 21:47:34 +00004235 Image
4236 *border_image,
4237 *clone_image,
4238 *shadow_image;
4239
cristy70cddf72011-12-10 22:42:42 +00004240 MagickBooleanType
4241 status;
4242
cristy3ed852e2009-09-05 21:47:34 +00004243 RectangleInfo
4244 border_info;
4245
cristy70cddf72011-12-10 22:42:42 +00004246 ssize_t
4247 y;
4248
cristy3ed852e2009-09-05 21:47:34 +00004249 assert(image != (Image *) NULL);
4250 assert(image->signature == MagickSignature);
4251 if (image->debug != MagickFalse)
4252 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4253 assert(exception != (ExceptionInfo *) NULL);
4254 assert(exception->signature == MagickSignature);
4255 clone_image=CloneImage(image,0,0,MagickTrue,exception);
4256 if (clone_image == (Image *) NULL)
4257 return((Image *) NULL);
cristy0898eba2012-04-09 16:38:29 +00004258 if (IsGrayColorspace(image->colorspace) != MagickFalse)
4259 (void) TransformImageColorspace(clone_image,sRGBColorspace,exception);
cristy387430f2012-02-07 13:09:46 +00004260 (void) SetImageVirtualPixelMethod(clone_image,EdgeVirtualPixelMethod,
4261 exception);
cristybb503372010-05-27 20:51:26 +00004262 border_info.width=(size_t) floor(2.0*sigma+0.5);
4263 border_info.height=(size_t) floor(2.0*sigma+0.5);
cristy3ed852e2009-09-05 21:47:34 +00004264 border_info.x=0;
4265 border_info.y=0;
cristy9950d572011-10-01 18:22:35 +00004266 (void) QueryColorCompliance("none",AllCompliance,&clone_image->border_color,
4267 exception);
cristy70cddf72011-12-10 22:42:42 +00004268 clone_image->matte=MagickTrue;
4269 border_image=BorderImage(clone_image,&border_info,OverCompositeOp,exception);
cristy3ed852e2009-09-05 21:47:34 +00004270 clone_image=DestroyImage(clone_image);
4271 if (border_image == (Image *) NULL)
4272 return((Image *) NULL);
4273 if (border_image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00004274 (void) SetImageAlphaChannel(border_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004275 /*
4276 Shadow image.
4277 */
cristy70cddf72011-12-10 22:42:42 +00004278 status=MagickTrue;
4279 image_view=AcquireCacheView(border_image);
4280 for (y=0; y < (ssize_t) border_image->rows; y++)
4281 {
4282 PixelInfo
4283 background_color;
4284
4285 register Quantum
4286 *restrict q;
4287
4288 register ssize_t
4289 x;
4290
4291 if (status == MagickFalse)
4292 continue;
4293 q=QueueCacheViewAuthenticPixels(image_view,0,y,border_image->columns,1,
4294 exception);
4295 if (q == (Quantum *) NULL)
4296 {
4297 status=MagickFalse;
4298 continue;
4299 }
4300 background_color=border_image->background_color;
4301 background_color.matte=MagickTrue;
4302 for (x=0; x < (ssize_t) border_image->columns; x++)
4303 {
4304 if (border_image->matte != MagickFalse)
4305 background_color.alpha=GetPixelAlpha(border_image,q)*alpha/100.0;
4306 SetPixelInfoPixel(border_image,&background_color,q);
4307 q+=GetPixelChannels(border_image);
4308 }
4309 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4310 status=MagickFalse;
4311 }
4312 image_view=DestroyCacheView(image_view);
4313 if (status == MagickFalse)
4314 {
4315 border_image=DestroyImage(border_image);
4316 return((Image *) NULL);
4317 }
cristybd5a96c2011-08-21 00:04:26 +00004318 channel_mask=SetPixelChannelMask(border_image,AlphaChannel);
cristyaa2c16c2012-03-25 22:21:35 +00004319 shadow_image=BlurImage(border_image,0.0,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +00004320 border_image=DestroyImage(border_image);
4321 if (shadow_image == (Image *) NULL)
4322 return((Image *) NULL);
cristyae1969f2011-12-10 03:07:36 +00004323 (void) SetPixelChannelMapMask(shadow_image,channel_mask);
cristy3ed852e2009-09-05 21:47:34 +00004324 if (shadow_image->page.width == 0)
4325 shadow_image->page.width=shadow_image->columns;
4326 if (shadow_image->page.height == 0)
4327 shadow_image->page.height=shadow_image->rows;
cristybb503372010-05-27 20:51:26 +00004328 shadow_image->page.width+=x_offset-(ssize_t) border_info.width;
4329 shadow_image->page.height+=y_offset-(ssize_t) border_info.height;
4330 shadow_image->page.x+=x_offset-(ssize_t) border_info.width;
4331 shadow_image->page.y+=y_offset-(ssize_t) border_info.height;
cristy3ed852e2009-09-05 21:47:34 +00004332 return(shadow_image);
4333}
4334
4335/*
4336%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4337% %
4338% %
4339% %
4340% S k e t c h I m a g e %
4341% %
4342% %
4343% %
4344%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4345%
4346% SketchImage() simulates a pencil sketch. We convolve the image with a
4347% Gaussian operator of the given radius and standard deviation (sigma). For
4348% reasonable results, radius should be larger than sigma. Use a radius of 0
4349% and SketchImage() selects a suitable radius for you. Angle gives the angle
4350% of the sketch.
4351%
4352% The format of the SketchImage method is:
4353%
4354% Image *SketchImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00004355% const double sigma,const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004356%
4357% A description of each parameter follows:
4358%
4359% o image: the image.
4360%
cristy574cc262011-08-05 01:23:58 +00004361% o radius: the radius of the Gaussian, in pixels, not counting the
4362% center pixel.
cristy3ed852e2009-09-05 21:47:34 +00004363%
4364% o sigma: the standard deviation of the Gaussian, in pixels.
4365%
cristyf7ef0252011-09-09 14:50:06 +00004366% o angle: apply the effect along this angle.
4367%
cristy3ed852e2009-09-05 21:47:34 +00004368% o exception: return any errors or warnings in this structure.
4369%
4370*/
4371MagickExport Image *SketchImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00004372 const double sigma,const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004373{
cristyfa112112010-01-04 17:48:07 +00004374 CacheView
4375 *random_view;
4376
cristy3ed852e2009-09-05 21:47:34 +00004377 Image
4378 *blend_image,
4379 *blur_image,
4380 *dodge_image,
4381 *random_image,
4382 *sketch_image;
4383
cristy3ed852e2009-09-05 21:47:34 +00004384 MagickBooleanType
cristye7452652012-04-14 01:34:21 +00004385 concurrent,
cristy3ed852e2009-09-05 21:47:34 +00004386 status;
4387
cristy3ed852e2009-09-05 21:47:34 +00004388 RandomInfo
cristyfa112112010-01-04 17:48:07 +00004389 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00004390
cristy9d314ff2011-03-09 01:30:28 +00004391 ssize_t
4392 y;
4393
cristy3ed852e2009-09-05 21:47:34 +00004394 /*
4395 Sketch image.
4396 */
4397 random_image=CloneImage(image,image->columns << 1,image->rows << 1,
4398 MagickTrue,exception);
4399 if (random_image == (Image *) NULL)
4400 return((Image *) NULL);
4401 status=MagickTrue;
cristy1b784432009-12-19 02:20:40 +00004402 random_info=AcquireRandomInfoThreadSet();
cristye7452652012-04-14 01:34:21 +00004403 concurrent=GetRandomSecretKey(random_info[0]) == ~0UL ? MagickTrue :
4404 MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004405 random_view=AcquireCacheView(random_image);
cristy1b784432009-12-19 02:20:40 +00004406#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye7452652012-04-14 01:34:21 +00004407 #pragma omp parallel for schedule(static,4) shared(status) omp_concurrent(concurrent)
cristy1b784432009-12-19 02:20:40 +00004408#endif
cristybb503372010-05-27 20:51:26 +00004409 for (y=0; y < (ssize_t) random_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004410 {
cristy5c9e6f22010-09-17 17:31:01 +00004411 const int
4412 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00004413
cristybb503372010-05-27 20:51:26 +00004414 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004415 x;
4416
cristy4c08aed2011-07-01 19:47:50 +00004417 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004418 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004419
cristy1b784432009-12-19 02:20:40 +00004420 if (status == MagickFalse)
4421 continue;
cristy3ed852e2009-09-05 21:47:34 +00004422 q=QueueCacheViewAuthenticPixels(random_view,0,y,random_image->columns,1,
4423 exception);
cristyacd2ed22011-08-30 01:44:23 +00004424 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004425 {
4426 status=MagickFalse;
4427 continue;
4428 }
cristybb503372010-05-27 20:51:26 +00004429 for (x=0; x < (ssize_t) random_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004430 {
cristy76f512e2011-09-12 01:26:56 +00004431 MagickRealType
4432 value;
4433
4434 register ssize_t
4435 i;
4436
cristy10a6c612012-01-29 21:41:05 +00004437 if (GetPixelMask(random_image,q) != 0)
4438 {
4439 q+=GetPixelChannels(random_image);
4440 continue;
4441 }
cristy76f512e2011-09-12 01:26:56 +00004442 value=GetPseudoRandomValue(random_info[id]);
4443 for (i=0; i < (ssize_t) GetPixelChannels(random_image); i++)
4444 {
cristyabace412011-12-11 15:56:53 +00004445 PixelChannel
4446 channel;
4447
cristy76f512e2011-09-12 01:26:56 +00004448 PixelTrait
4449 traits;
4450
cristyabace412011-12-11 15:56:53 +00004451 channel=GetPixelChannelMapChannel(image,i);
4452 traits=GetPixelChannelMapTraits(image,channel);
cristy76f512e2011-09-12 01:26:56 +00004453 if (traits == UndefinedPixelTrait)
4454 continue;
4455 q[i]=ClampToQuantum(QuantumRange*value);
4456 }
cristyed231572011-07-14 02:18:59 +00004457 q+=GetPixelChannels(random_image);
cristy3ed852e2009-09-05 21:47:34 +00004458 }
4459 if (SyncCacheViewAuthenticPixels(random_view,exception) == MagickFalse)
4460 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004461 }
4462 random_view=DestroyCacheView(random_view);
cristy1b784432009-12-19 02:20:40 +00004463 random_info=DestroyRandomInfoThreadSet(random_info);
cristy3ed852e2009-09-05 21:47:34 +00004464 if (status == MagickFalse)
4465 {
4466 random_image=DestroyImage(random_image);
4467 return(random_image);
4468 }
cristyaa2c16c2012-03-25 22:21:35 +00004469 blur_image=MotionBlurImage(random_image,radius,sigma,angle,exception);
cristy3ed852e2009-09-05 21:47:34 +00004470 random_image=DestroyImage(random_image);
4471 if (blur_image == (Image *) NULL)
4472 return((Image *) NULL);
cristy6bfd6902011-12-09 01:33:45 +00004473 dodge_image=EdgeImage(blur_image,radius,1.0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004474 blur_image=DestroyImage(blur_image);
4475 if (dodge_image == (Image *) NULL)
4476 return((Image *) NULL);
cristye23ec9d2011-08-16 18:15:40 +00004477 (void) NormalizeImage(dodge_image,exception);
cristyb3e7c6c2011-07-24 01:43:55 +00004478 (void) NegateImage(dodge_image,MagickFalse,exception);
cristye941a752011-10-15 01:52:48 +00004479 (void) TransformImage(&dodge_image,(char *) NULL,"50%",exception);
cristy3ed852e2009-09-05 21:47:34 +00004480 sketch_image=CloneImage(image,0,0,MagickTrue,exception);
4481 if (sketch_image == (Image *) NULL)
4482 {
4483 dodge_image=DestroyImage(dodge_image);
4484 return((Image *) NULL);
4485 }
cristyfeb3e962012-03-29 17:25:55 +00004486 (void) CompositeImage(sketch_image,dodge_image,ColorDodgeCompositeOp,
cristy39172402012-03-30 13:04:39 +00004487 MagickTrue,0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004488 dodge_image=DestroyImage(dodge_image);
4489 blend_image=CloneImage(image,0,0,MagickTrue,exception);
4490 if (blend_image == (Image *) NULL)
4491 {
4492 sketch_image=DestroyImage(sketch_image);
4493 return((Image *) NULL);
4494 }
4495 (void) SetImageArtifact(blend_image,"compose:args","20x80");
cristy39172402012-03-30 13:04:39 +00004496 (void) CompositeImage(sketch_image,blend_image,BlendCompositeOp,MagickTrue,
cristyfeb3e962012-03-29 17:25:55 +00004497 0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004498 blend_image=DestroyImage(blend_image);
4499 return(sketch_image);
4500}
4501
4502/*
4503%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4504% %
4505% %
4506% %
4507% S o l a r i z e I m a g e %
4508% %
4509% %
4510% %
4511%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4512%
4513% SolarizeImage() applies a special effect to the image, similar to the effect
4514% achieved in a photo darkroom by selectively exposing areas of photo
4515% sensitive paper to light. Threshold ranges from 0 to QuantumRange and is a
4516% measure of the extent of the solarization.
4517%
4518% The format of the SolarizeImage method is:
4519%
cristy5cbc0162011-08-29 00:36:28 +00004520% MagickBooleanType SolarizeImage(Image *image,const double threshold,
4521% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004522%
4523% A description of each parameter follows:
4524%
4525% o image: the image.
4526%
4527% o threshold: Define the extent of the solarization.
4528%
cristy5cbc0162011-08-29 00:36:28 +00004529% o exception: return any errors or warnings in this structure.
4530%
cristy3ed852e2009-09-05 21:47:34 +00004531*/
4532MagickExport MagickBooleanType SolarizeImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00004533 const double threshold,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004534{
4535#define SolarizeImageTag "Solarize/Image"
4536
cristyc4c8d132010-01-07 01:58:38 +00004537 CacheView
4538 *image_view;
4539
cristy3ed852e2009-09-05 21:47:34 +00004540 MagickBooleanType
4541 status;
4542
cristybb503372010-05-27 20:51:26 +00004543 MagickOffsetType
4544 progress;
4545
4546 ssize_t
4547 y;
4548
cristy3ed852e2009-09-05 21:47:34 +00004549 assert(image != (Image *) NULL);
4550 assert(image->signature == MagickSignature);
4551 if (image->debug != MagickFalse)
4552 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4553 if (image->storage_class == PseudoClass)
4554 {
cristybb503372010-05-27 20:51:26 +00004555 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004556 i;
4557
4558 /*
4559 Solarize colormap.
4560 */
cristybb503372010-05-27 20:51:26 +00004561 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00004562 {
4563 if ((MagickRealType) image->colormap[i].red > threshold)
4564 image->colormap[i].red=(Quantum) QuantumRange-image->colormap[i].red;
4565 if ((MagickRealType) image->colormap[i].green > threshold)
4566 image->colormap[i].green=(Quantum) QuantumRange-
4567 image->colormap[i].green;
4568 if ((MagickRealType) image->colormap[i].blue > threshold)
4569 image->colormap[i].blue=(Quantum) QuantumRange-
4570 image->colormap[i].blue;
4571 }
4572 }
4573 /*
4574 Solarize image.
4575 */
4576 status=MagickTrue;
4577 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00004578 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00004579#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00004580 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00004581#endif
cristybb503372010-05-27 20:51:26 +00004582 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004583 {
cristybb503372010-05-27 20:51:26 +00004584 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004585 x;
4586
cristy4c08aed2011-07-01 19:47:50 +00004587 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004588 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004589
4590 if (status == MagickFalse)
4591 continue;
cristy5cbc0162011-08-29 00:36:28 +00004592 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00004593 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004594 {
4595 status=MagickFalse;
4596 continue;
4597 }
cristybb503372010-05-27 20:51:26 +00004598 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004599 {
cristy76f512e2011-09-12 01:26:56 +00004600 register ssize_t
4601 i;
4602
cristy10a6c612012-01-29 21:41:05 +00004603 if (GetPixelMask(image,q) != 0)
4604 {
4605 q+=GetPixelChannels(image);
4606 continue;
4607 }
cristy76f512e2011-09-12 01:26:56 +00004608 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4609 {
cristyabace412011-12-11 15:56:53 +00004610 PixelChannel
4611 channel;
4612
cristy76f512e2011-09-12 01:26:56 +00004613 PixelTrait
4614 traits;
4615
cristyabace412011-12-11 15:56:53 +00004616 channel=GetPixelChannelMapChannel(image,i);
4617 traits=GetPixelChannelMapTraits(image,channel);
cristy76f512e2011-09-12 01:26:56 +00004618 if ((traits == UndefinedPixelTrait) ||
4619 ((traits & CopyPixelTrait) != 0))
4620 continue;
4621 if ((MagickRealType) q[i] > threshold)
4622 q[i]=QuantumRange-q[i];
4623 }
cristyed231572011-07-14 02:18:59 +00004624 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00004625 }
4626 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4627 status=MagickFalse;
4628 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4629 {
4630 MagickBooleanType
4631 proceed;
4632
cristyb5d5f722009-11-04 03:03:49 +00004633#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00004634 #pragma omp critical (MagickCore_SolarizeImage)
cristy3ed852e2009-09-05 21:47:34 +00004635#endif
4636 proceed=SetImageProgress(image,SolarizeImageTag,progress++,image->rows);
4637 if (proceed == MagickFalse)
4638 status=MagickFalse;
4639 }
4640 }
4641 image_view=DestroyCacheView(image_view);
4642 return(status);
4643}
4644
4645/*
4646%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4647% %
4648% %
4649% %
4650% S t e g a n o I m a g e %
4651% %
4652% %
4653% %
4654%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4655%
4656% SteganoImage() hides a digital watermark within the image. Recover
4657% the hidden watermark later to prove that the authenticity of an image.
4658% Offset defines the start position within the image to hide the watermark.
4659%
4660% The format of the SteganoImage method is:
4661%
4662% Image *SteganoImage(const Image *image,Image *watermark,
4663% ExceptionInfo *exception)
4664%
4665% A description of each parameter follows:
4666%
4667% o image: the image.
4668%
4669% o watermark: the watermark image.
4670%
4671% o exception: return any errors or warnings in this structure.
4672%
4673*/
4674MagickExport Image *SteganoImage(const Image *image,const Image *watermark,
4675 ExceptionInfo *exception)
4676{
cristye1bf8ad2010-09-19 17:07:03 +00004677#define GetBit(alpha,i) ((((size_t) (alpha) >> (size_t) (i)) & 0x01) != 0)
cristy4c08aed2011-07-01 19:47:50 +00004678#define SetBit(alpha,i,set) (Quantum) ((set) != 0 ? (size_t) (alpha) \
cristyeaedf062010-05-29 22:36:02 +00004679 | (one << (size_t) (i)) : (size_t) (alpha) & ~(one << (size_t) (i)))
cristy3ed852e2009-09-05 21:47:34 +00004680#define SteganoImageTag "Stegano/Image"
4681
cristyb0d3bb92010-09-22 14:37:58 +00004682 CacheView
4683 *stegano_view,
4684 *watermark_view;
4685
cristy3ed852e2009-09-05 21:47:34 +00004686 Image
4687 *stegano_image;
4688
4689 int
4690 c;
4691
cristy3ed852e2009-09-05 21:47:34 +00004692 MagickBooleanType
4693 status;
4694
cristy101ab702011-10-13 13:06:32 +00004695 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00004696 pixel;
4697
cristy4c08aed2011-07-01 19:47:50 +00004698 register Quantum
cristy3ed852e2009-09-05 21:47:34 +00004699 *q;
4700
cristye1bf8ad2010-09-19 17:07:03 +00004701 register ssize_t
4702 x;
4703
cristybb503372010-05-27 20:51:26 +00004704 size_t
cristyeaedf062010-05-29 22:36:02 +00004705 depth,
4706 one;
cristy3ed852e2009-09-05 21:47:34 +00004707
cristye1bf8ad2010-09-19 17:07:03 +00004708 ssize_t
4709 i,
4710 j,
4711 k,
4712 y;
4713
cristy3ed852e2009-09-05 21:47:34 +00004714 /*
4715 Initialize steganographic image attributes.
4716 */
4717 assert(image != (const Image *) NULL);
4718 assert(image->signature == MagickSignature);
4719 if (image->debug != MagickFalse)
4720 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4721 assert(watermark != (const Image *) NULL);
4722 assert(watermark->signature == MagickSignature);
4723 assert(exception != (ExceptionInfo *) NULL);
4724 assert(exception->signature == MagickSignature);
cristyeaedf062010-05-29 22:36:02 +00004725 one=1UL;
cristy3ed852e2009-09-05 21:47:34 +00004726 stegano_image=CloneImage(image,0,0,MagickTrue,exception);
4727 if (stegano_image == (Image *) NULL)
4728 return((Image *) NULL);
cristyf61b1832012-04-01 01:38:19 +00004729 stegano_image->depth=MAGICKCORE_QUANTUM_DEPTH;
cristy574cc262011-08-05 01:23:58 +00004730 if (SetImageStorageClass(stegano_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004731 {
cristy3ed852e2009-09-05 21:47:34 +00004732 stegano_image=DestroyImage(stegano_image);
4733 return((Image *) NULL);
4734 }
cristy3ed852e2009-09-05 21:47:34 +00004735 /*
4736 Hide watermark in low-order bits of image.
4737 */
4738 c=0;
4739 i=0;
4740 j=0;
4741 depth=stegano_image->depth;
cristyf61b1832012-04-01 01:38:19 +00004742 k=stegano_image->offset;
cristyda16f162011-02-19 23:52:17 +00004743 status=MagickTrue;
cristyb0d3bb92010-09-22 14:37:58 +00004744 watermark_view=AcquireCacheView(watermark);
4745 stegano_view=AcquireCacheView(stegano_image);
cristybb503372010-05-27 20:51:26 +00004746 for (i=(ssize_t) depth-1; (i >= 0) && (j < (ssize_t) depth); i--)
cristy3ed852e2009-09-05 21:47:34 +00004747 {
cristybb503372010-05-27 20:51:26 +00004748 for (y=0; (y < (ssize_t) watermark->rows) && (j < (ssize_t) depth); y++)
cristy3ed852e2009-09-05 21:47:34 +00004749 {
cristybb503372010-05-27 20:51:26 +00004750 for (x=0; (x < (ssize_t) watermark->columns) && (j < (ssize_t) depth); x++)
cristy3ed852e2009-09-05 21:47:34 +00004751 {
cristy1707c6c2012-01-18 23:30:54 +00004752 ssize_t
4753 offset;
4754
cristyf05d4942012-03-17 16:26:09 +00004755 (void) GetOneCacheViewVirtualPixelInfo(watermark_view,x,y,&pixel,
cristyda1f9c12011-10-02 21:39:49 +00004756 exception);
cristy1707c6c2012-01-18 23:30:54 +00004757 offset=k/(ssize_t) stegano_image->columns;
4758 if (offset >= (ssize_t) stegano_image->rows)
cristy3ed852e2009-09-05 21:47:34 +00004759 break;
cristyb0d3bb92010-09-22 14:37:58 +00004760 q=GetCacheViewAuthenticPixels(stegano_view,k % (ssize_t)
4761 stegano_image->columns,k/(ssize_t) stegano_image->columns,1,1,
4762 exception);
cristyacd2ed22011-08-30 01:44:23 +00004763 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004764 break;
4765 switch (c)
4766 {
4767 case 0:
4768 {
cristyf61b1832012-04-01 01:38:19 +00004769 SetPixelRed(stegano_image,SetBit(GetPixelRed(stegano_image,q),j,
4770 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004771 break;
4772 }
4773 case 1:
4774 {
cristyf61b1832012-04-01 01:38:19 +00004775 SetPixelGreen(stegano_image,SetBit(GetPixelGreen(stegano_image,q),j,
4776 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004777 break;
4778 }
4779 case 2:
4780 {
cristyf61b1832012-04-01 01:38:19 +00004781 SetPixelBlue(stegano_image,SetBit(GetPixelBlue(stegano_image,q),j,
4782 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004783 break;
4784 }
4785 }
cristyb0d3bb92010-09-22 14:37:58 +00004786 if (SyncCacheViewAuthenticPixels(stegano_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004787 break;
4788 c++;
4789 if (c == 3)
4790 c=0;
4791 k++;
cristybb503372010-05-27 20:51:26 +00004792 if (k == (ssize_t) (stegano_image->columns*stegano_image->columns))
cristy3ed852e2009-09-05 21:47:34 +00004793 k=0;
cristyf61b1832012-04-01 01:38:19 +00004794 if (k == stegano_image->offset)
cristy3ed852e2009-09-05 21:47:34 +00004795 j++;
4796 }
4797 }
cristy8b27a6d2010-02-14 03:31:15 +00004798 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004799 {
cristy8b27a6d2010-02-14 03:31:15 +00004800 MagickBooleanType
4801 proceed;
4802
4803 proceed=SetImageProgress(image,SteganoImageTag,(MagickOffsetType)
4804 (depth-i),depth);
4805 if (proceed == MagickFalse)
4806 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004807 }
4808 }
cristyb0d3bb92010-09-22 14:37:58 +00004809 stegano_view=DestroyCacheView(stegano_view);
4810 watermark_view=DestroyCacheView(watermark_view);
cristyda16f162011-02-19 23:52:17 +00004811 if (status == MagickFalse)
4812 {
4813 stegano_image=DestroyImage(stegano_image);
4814 return((Image *) NULL);
4815 }
cristy3ed852e2009-09-05 21:47:34 +00004816 return(stegano_image);
4817}
4818
4819/*
4820%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4821% %
4822% %
4823% %
4824% S t e r e o A n a g l y p h I m a g e %
4825% %
4826% %
4827% %
4828%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4829%
4830% StereoAnaglyphImage() combines two images and produces a single image that
4831% is the composite of a left and right image of a stereo pair. Special
4832% red-green stereo glasses are required to view this effect.
4833%
4834% The format of the StereoAnaglyphImage method is:
4835%
4836% Image *StereoImage(const Image *left_image,const Image *right_image,
4837% ExceptionInfo *exception)
4838% Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004839% const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004840% ExceptionInfo *exception)
4841%
4842% A description of each parameter follows:
4843%
4844% o left_image: the left image.
4845%
4846% o right_image: the right image.
4847%
4848% o exception: return any errors or warnings in this structure.
4849%
4850% o x_offset: amount, in pixels, by which the left image is offset to the
4851% right of the right image.
4852%
4853% o y_offset: amount, in pixels, by which the left image is offset to the
4854% bottom of the right image.
4855%
4856%
4857*/
4858MagickExport Image *StereoImage(const Image *left_image,
4859 const Image *right_image,ExceptionInfo *exception)
4860{
4861 return(StereoAnaglyphImage(left_image,right_image,0,0,exception));
4862}
4863
4864MagickExport Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004865 const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004866 ExceptionInfo *exception)
4867{
4868#define StereoImageTag "Stereo/Image"
4869
4870 const Image
4871 *image;
4872
4873 Image
4874 *stereo_image;
4875
cristy3ed852e2009-09-05 21:47:34 +00004876 MagickBooleanType
4877 status;
4878
cristy9d314ff2011-03-09 01:30:28 +00004879 ssize_t
4880 y;
4881
cristy3ed852e2009-09-05 21:47:34 +00004882 assert(left_image != (const Image *) NULL);
4883 assert(left_image->signature == MagickSignature);
4884 if (left_image->debug != MagickFalse)
4885 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4886 left_image->filename);
4887 assert(right_image != (const Image *) NULL);
4888 assert(right_image->signature == MagickSignature);
4889 assert(exception != (ExceptionInfo *) NULL);
4890 assert(exception->signature == MagickSignature);
4891 assert(right_image != (const Image *) NULL);
4892 image=left_image;
4893 if ((left_image->columns != right_image->columns) ||
4894 (left_image->rows != right_image->rows))
4895 ThrowImageException(ImageError,"LeftAndRightImageSizesDiffer");
4896 /*
4897 Initialize stereo image attributes.
4898 */
4899 stereo_image=CloneImage(left_image,left_image->columns,left_image->rows,
4900 MagickTrue,exception);
4901 if (stereo_image == (Image *) NULL)
4902 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004903 if (SetImageStorageClass(stereo_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004904 {
cristy3ed852e2009-09-05 21:47:34 +00004905 stereo_image=DestroyImage(stereo_image);
4906 return((Image *) NULL);
4907 }
4908 /*
4909 Copy left image to red channel and right image to blue channel.
4910 */
cristyda16f162011-02-19 23:52:17 +00004911 status=MagickTrue;
cristybb503372010-05-27 20:51:26 +00004912 for (y=0; y < (ssize_t) stereo_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004913 {
cristy4c08aed2011-07-01 19:47:50 +00004914 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004915 *restrict p,
4916 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004917
cristybb503372010-05-27 20:51:26 +00004918 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004919 x;
4920
cristy4c08aed2011-07-01 19:47:50 +00004921 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004922 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +00004923
4924 p=GetVirtualPixels(left_image,-x_offset,y-y_offset,image->columns,1,
4925 exception);
4926 q=GetVirtualPixels(right_image,0,y,right_image->columns,1,exception);
4927 r=QueueAuthenticPixels(stereo_image,0,y,stereo_image->columns,1,exception);
cristy76f512e2011-09-12 01:26:56 +00004928 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL) ||
4929 (r == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004930 break;
cristybb503372010-05-27 20:51:26 +00004931 for (x=0; x < (ssize_t) stereo_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004932 {
cristy4c08aed2011-07-01 19:47:50 +00004933 SetPixelRed(image,GetPixelRed(left_image,p),r);
cristy76f512e2011-09-12 01:26:56 +00004934 SetPixelGreen(image,GetPixelGreen(right_image,q),r);
4935 SetPixelBlue(image,GetPixelBlue(right_image,q),r);
4936 if ((GetPixelAlphaTraits(stereo_image) & CopyPixelTrait) != 0)
4937 SetPixelAlpha(image,(GetPixelAlpha(left_image,p)+
4938 GetPixelAlpha(right_image,q))/2,r);
cristyed231572011-07-14 02:18:59 +00004939 p+=GetPixelChannels(left_image);
cristy76f512e2011-09-12 01:26:56 +00004940 q+=GetPixelChannels(right_image);
4941 r+=GetPixelChannels(stereo_image);
cristy3ed852e2009-09-05 21:47:34 +00004942 }
4943 if (SyncAuthenticPixels(stereo_image,exception) == MagickFalse)
4944 break;
cristy8b27a6d2010-02-14 03:31:15 +00004945 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004946 {
cristy8b27a6d2010-02-14 03:31:15 +00004947 MagickBooleanType
4948 proceed;
4949
cristybb503372010-05-27 20:51:26 +00004950 proceed=SetImageProgress(image,StereoImageTag,(MagickOffsetType) y,
4951 stereo_image->rows);
cristy8b27a6d2010-02-14 03:31:15 +00004952 if (proceed == MagickFalse)
4953 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004954 }
4955 }
cristyda16f162011-02-19 23:52:17 +00004956 if (status == MagickFalse)
4957 {
4958 stereo_image=DestroyImage(stereo_image);
4959 return((Image *) NULL);
4960 }
cristy3ed852e2009-09-05 21:47:34 +00004961 return(stereo_image);
4962}
4963
4964/*
4965%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4966% %
4967% %
4968% %
4969% S w i r l I m a g e %
4970% %
4971% %
4972% %
4973%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4974%
4975% SwirlImage() swirls the pixels about the center of the image, where
4976% degrees indicates the sweep of the arc through which each pixel is moved.
4977% You get a more dramatic effect as the degrees move from 1 to 360.
4978%
4979% The format of the SwirlImage method is:
4980%
4981% Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00004982% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004983%
4984% A description of each parameter follows:
4985%
4986% o image: the image.
4987%
4988% o degrees: Define the tightness of the swirling effect.
4989%
cristy76f512e2011-09-12 01:26:56 +00004990% o method: the pixel interpolation method.
4991%
cristy3ed852e2009-09-05 21:47:34 +00004992% o exception: return any errors or warnings in this structure.
4993%
4994*/
4995MagickExport Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00004996 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004997{
4998#define SwirlImageTag "Swirl/Image"
4999
cristyfa112112010-01-04 17:48:07 +00005000 CacheView
5001 *image_view,
5002 *swirl_view;
5003
cristy3ed852e2009-09-05 21:47:34 +00005004 Image
5005 *swirl_image;
5006
cristy3ed852e2009-09-05 21:47:34 +00005007 MagickBooleanType
5008 status;
5009
cristybb503372010-05-27 20:51:26 +00005010 MagickOffsetType
5011 progress;
5012
cristy3ed852e2009-09-05 21:47:34 +00005013 MagickRealType
5014 radius;
5015
5016 PointInfo
5017 center,
5018 scale;
5019
cristybb503372010-05-27 20:51:26 +00005020 ssize_t
5021 y;
5022
cristy3ed852e2009-09-05 21:47:34 +00005023 /*
5024 Initialize swirl image attributes.
5025 */
5026 assert(image != (const Image *) NULL);
5027 assert(image->signature == MagickSignature);
5028 if (image->debug != MagickFalse)
5029 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5030 assert(exception != (ExceptionInfo *) NULL);
5031 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00005032 swirl_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005033 if (swirl_image == (Image *) NULL)
5034 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005035 if (SetImageStorageClass(swirl_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005036 {
cristy3ed852e2009-09-05 21:47:34 +00005037 swirl_image=DestroyImage(swirl_image);
5038 return((Image *) NULL);
5039 }
cristy4c08aed2011-07-01 19:47:50 +00005040 if (swirl_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00005041 swirl_image->matte=MagickTrue;
5042 /*
5043 Compute scaling factor.
5044 */
5045 center.x=(double) image->columns/2.0;
5046 center.y=(double) image->rows/2.0;
5047 radius=MagickMax(center.x,center.y);
5048 scale.x=1.0;
5049 scale.y=1.0;
5050 if (image->columns > image->rows)
5051 scale.y=(double) image->columns/(double) image->rows;
5052 else
5053 if (image->columns < image->rows)
5054 scale.x=(double) image->rows/(double) image->columns;
5055 degrees=(double) DegreesToRadians(degrees);
5056 /*
5057 Swirl image.
5058 */
5059 status=MagickTrue;
5060 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00005061 image_view=AcquireCacheView(image);
5062 swirl_view=AcquireCacheView(swirl_image);
cristyb5d5f722009-11-04 03:03:49 +00005063#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005064 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00005065#endif
cristybb503372010-05-27 20:51:26 +00005066 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005067 {
cristy3ed852e2009-09-05 21:47:34 +00005068 MagickRealType
5069 distance;
5070
5071 PointInfo
5072 delta;
5073
cristy6d188022011-09-12 13:23:33 +00005074 register const Quantum
5075 *restrict p;
5076
cristybb503372010-05-27 20:51:26 +00005077 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005078 x;
5079
cristy4c08aed2011-07-01 19:47:50 +00005080 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005081 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005082
5083 if (status == MagickFalse)
5084 continue;
cristy6d188022011-09-12 13:23:33 +00005085 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy1707c6c2012-01-18 23:30:54 +00005086 q=QueueCacheViewAuthenticPixels(swirl_view,0,y,swirl_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00005087 exception);
cristy6d188022011-09-12 13:23:33 +00005088 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005089 {
5090 status=MagickFalse;
5091 continue;
5092 }
cristy3ed852e2009-09-05 21:47:34 +00005093 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00005094 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005095 {
5096 /*
5097 Determine if the pixel is within an ellipse.
5098 */
cristy10a6c612012-01-29 21:41:05 +00005099 if (GetPixelMask(image,p) != 0)
5100 {
5101 p+=GetPixelChannels(image);
5102 q+=GetPixelChannels(swirl_image);
5103 continue;
5104 }
cristy3ed852e2009-09-05 21:47:34 +00005105 delta.x=scale.x*(double) (x-center.x);
5106 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00005107 if (distance >= (radius*radius))
5108 {
cristy1707c6c2012-01-18 23:30:54 +00005109 register ssize_t
5110 i;
5111
cristy6d188022011-09-12 13:23:33 +00005112 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy1707c6c2012-01-18 23:30:54 +00005113 {
5114 PixelChannel
5115 channel;
5116
5117 PixelTrait
5118 swirl_traits,
5119 traits;
5120
5121 channel=GetPixelChannelMapChannel(image,i);
5122 traits=GetPixelChannelMapTraits(image,channel);
5123 swirl_traits=GetPixelChannelMapTraits(swirl_image,channel);
5124 if ((traits == UndefinedPixelTrait) ||
5125 (swirl_traits == UndefinedPixelTrait))
5126 continue;
5127 SetPixelChannel(swirl_image,channel,p[i],q);
5128 }
cristy6d188022011-09-12 13:23:33 +00005129 }
5130 else
cristy3ed852e2009-09-05 21:47:34 +00005131 {
5132 MagickRealType
5133 cosine,
5134 factor,
5135 sine;
5136
5137 /*
5138 Swirl the pixel.
5139 */
5140 factor=1.0-sqrt((double) distance)/radius;
5141 sine=sin((double) (degrees*factor*factor));
5142 cosine=cos((double) (degrees*factor*factor));
cristy76f512e2011-09-12 01:26:56 +00005143 status=InterpolatePixelChannels(image,image_view,swirl_image,method,
5144 ((cosine*delta.x-sine*delta.y)/scale.x+center.x),(double)
5145 ((sine*delta.x+cosine*delta.y)/scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00005146 }
cristy6d188022011-09-12 13:23:33 +00005147 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00005148 q+=GetPixelChannels(swirl_image);
cristy3ed852e2009-09-05 21:47:34 +00005149 }
5150 if (SyncCacheViewAuthenticPixels(swirl_view,exception) == MagickFalse)
5151 status=MagickFalse;
5152 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5153 {
5154 MagickBooleanType
5155 proceed;
5156
cristyb5d5f722009-11-04 03:03:49 +00005157#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005158 #pragma omp critical (MagickCore_SwirlImage)
cristy3ed852e2009-09-05 21:47:34 +00005159#endif
5160 proceed=SetImageProgress(image,SwirlImageTag,progress++,image->rows);
5161 if (proceed == MagickFalse)
5162 status=MagickFalse;
5163 }
5164 }
5165 swirl_view=DestroyCacheView(swirl_view);
5166 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00005167 if (status == MagickFalse)
5168 swirl_image=DestroyImage(swirl_image);
5169 return(swirl_image);
5170}
5171
5172/*
5173%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5174% %
5175% %
5176% %
5177% T i n t I m a g e %
5178% %
5179% %
5180% %
5181%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5182%
5183% TintImage() applies a color vector to each pixel in the image. The length
5184% of the vector is 0 for black and white and at its maximum for the midtones.
5185% The vector weighting function is f(x)=(1-(4.0*((x-0.5)*(x-0.5))))
5186%
5187% The format of the TintImage method is:
5188%
cristyb817c3f2011-10-03 14:00:35 +00005189% Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005190% const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005191%
5192% A description of each parameter follows:
5193%
5194% o image: the image.
5195%
cristyb817c3f2011-10-03 14:00:35 +00005196% o blend: A color value used for tinting.
cristy3ed852e2009-09-05 21:47:34 +00005197%
5198% o tint: A color value used for tinting.
5199%
5200% o exception: return any errors or warnings in this structure.
5201%
5202*/
cristyb817c3f2011-10-03 14:00:35 +00005203MagickExport Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005204 const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005205{
5206#define TintImageTag "Tint/Image"
5207
cristyc4c8d132010-01-07 01:58:38 +00005208 CacheView
5209 *image_view,
5210 *tint_view;
5211
cristy3ed852e2009-09-05 21:47:34 +00005212 GeometryInfo
5213 geometry_info;
5214
5215 Image
5216 *tint_image;
5217
cristy3ed852e2009-09-05 21:47:34 +00005218 MagickBooleanType
5219 status;
5220
cristybb503372010-05-27 20:51:26 +00005221 MagickOffsetType
5222 progress;
cristy3ed852e2009-09-05 21:47:34 +00005223
cristy28474bf2011-09-11 23:32:52 +00005224 MagickRealType
5225 intensity;
5226
cristy4c08aed2011-07-01 19:47:50 +00005227 PixelInfo
cristy1707c6c2012-01-18 23:30:54 +00005228 color_vector;
cristy3ed852e2009-09-05 21:47:34 +00005229
cristybb503372010-05-27 20:51:26 +00005230 MagickStatusType
5231 flags;
5232
5233 ssize_t
5234 y;
5235
cristy3ed852e2009-09-05 21:47:34 +00005236 /*
5237 Allocate tint image.
5238 */
5239 assert(image != (const Image *) NULL);
5240 assert(image->signature == MagickSignature);
5241 if (image->debug != MagickFalse)
5242 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5243 assert(exception != (ExceptionInfo *) NULL);
5244 assert(exception->signature == MagickSignature);
5245 tint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5246 if (tint_image == (Image *) NULL)
5247 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005248 if (SetImageStorageClass(tint_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005249 {
cristy3ed852e2009-09-05 21:47:34 +00005250 tint_image=DestroyImage(tint_image);
5251 return((Image *) NULL);
5252 }
cristyaed9c382011-10-03 17:54:21 +00005253 if (blend == (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005254 return(tint_image);
5255 /*
5256 Determine RGB values of the color.
5257 */
cristy1707c6c2012-01-18 23:30:54 +00005258 GetPixelInfo(image,&color_vector);
cristyb817c3f2011-10-03 14:00:35 +00005259 flags=ParseGeometry(blend,&geometry_info);
cristy1707c6c2012-01-18 23:30:54 +00005260 color_vector.red=geometry_info.rho;
5261 color_vector.green=geometry_info.rho;
5262 color_vector.blue=geometry_info.rho;
5263 color_vector.alpha=OpaqueAlpha;
cristy3ed852e2009-09-05 21:47:34 +00005264 if ((flags & SigmaValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005265 color_vector.green=geometry_info.sigma;
cristy3ed852e2009-09-05 21:47:34 +00005266 if ((flags & XiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005267 color_vector.blue=geometry_info.xi;
cristyb817c3f2011-10-03 14:00:35 +00005268 if ((flags & PsiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005269 color_vector.alpha=geometry_info.psi;
cristy76f512e2011-09-12 01:26:56 +00005270 if (image->colorspace == CMYKColorspace)
5271 {
cristy1707c6c2012-01-18 23:30:54 +00005272 color_vector.black=geometry_info.rho;
cristy76f512e2011-09-12 01:26:56 +00005273 if ((flags & PsiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005274 color_vector.black=geometry_info.psi;
cristy76f512e2011-09-12 01:26:56 +00005275 if ((flags & ChiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005276 color_vector.alpha=geometry_info.chi;
cristy76f512e2011-09-12 01:26:56 +00005277 }
cristy28474bf2011-09-11 23:32:52 +00005278 intensity=(MagickRealType) GetPixelInfoIntensity(tint);
cristy1707c6c2012-01-18 23:30:54 +00005279 color_vector.red=(MagickRealType) (color_vector.red*tint->red/100.0-
5280 intensity);
5281 color_vector.green=(MagickRealType) (color_vector.green*tint->green/100.0-
5282 intensity);
5283 color_vector.blue=(MagickRealType) (color_vector.blue*tint->blue/100.0-
5284 intensity);
5285 color_vector.black=(MagickRealType) (color_vector.black*tint->black/100.0-
5286 intensity);
5287 color_vector.alpha=(MagickRealType) (color_vector.alpha*tint->alpha/100.0-
5288 intensity);
cristy3ed852e2009-09-05 21:47:34 +00005289 /*
5290 Tint image.
5291 */
5292 status=MagickTrue;
5293 progress=0;
5294 image_view=AcquireCacheView(image);
5295 tint_view=AcquireCacheView(tint_image);
cristyb5d5f722009-11-04 03:03:49 +00005296#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00005297 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00005298#endif
cristybb503372010-05-27 20:51:26 +00005299 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005300 {
cristy4c08aed2011-07-01 19:47:50 +00005301 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00005302 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00005303
cristy4c08aed2011-07-01 19:47:50 +00005304 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005305 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005306
cristy6b91acb2011-04-19 12:23:54 +00005307 register ssize_t
5308 x;
5309
cristy3ed852e2009-09-05 21:47:34 +00005310 if (status == MagickFalse)
5311 continue;
5312 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5313 q=QueueCacheViewAuthenticPixels(tint_view,0,y,tint_image->columns,1,
5314 exception);
cristy4c08aed2011-07-01 19:47:50 +00005315 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005316 {
5317 status=MagickFalse;
5318 continue;
5319 }
cristybb503372010-05-27 20:51:26 +00005320 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005321 {
cristy4c08aed2011-07-01 19:47:50 +00005322 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00005323 pixel;
5324
5325 MagickRealType
5326 weight;
5327
cristy1707c6c2012-01-18 23:30:54 +00005328 register ssize_t
5329 i;
5330
cristy10a6c612012-01-29 21:41:05 +00005331 if (GetPixelMask(image,p) != 0)
5332 {
5333 p+=GetPixelChannels(image);
5334 q+=GetPixelChannels(tint_image);
5335 continue;
5336 }
cristy1707c6c2012-01-18 23:30:54 +00005337 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5338 {
5339 PixelChannel
5340 channel;
5341
5342 PixelTrait
5343 tint_traits,
5344 traits;
5345
5346 channel=GetPixelChannelMapChannel(image,i);
5347 traits=GetPixelChannelMapTraits(image,channel);
5348 tint_traits=GetPixelChannelMapTraits(tint_image,channel);
5349 if ((traits == UndefinedPixelTrait) ||
5350 (tint_traits == UndefinedPixelTrait))
5351 continue;
5352 if ((tint_traits & CopyPixelTrait) != 0)
5353 {
5354 SetPixelChannel(tint_image,channel,p[i],q);
5355 continue;
5356 }
5357 }
5358 GetPixelInfo(image,&pixel);
5359 weight=QuantumScale*GetPixelRed(image,p)-0.5;
5360 pixel.red=(MagickRealType) GetPixelRed(image,p)+color_vector.red*
5361 (1.0-(4.0*(weight*weight)));
5362 weight=QuantumScale*GetPixelGreen(image,p)-0.5;
5363 pixel.green=(MagickRealType) GetPixelGreen(image,p)+color_vector.green*
5364 (1.0-(4.0*(weight*weight)));
5365 weight=QuantumScale*GetPixelBlue(image,p)-0.5;
5366 pixel.blue=(MagickRealType) GetPixelBlue(image,p)+color_vector.blue*
5367 (1.0-(4.0*(weight*weight)));
5368 weight=QuantumScale*GetPixelBlack(image,p)-0.5;
5369 pixel.black=(MagickRealType) GetPixelBlack(image,p)+color_vector.black*
5370 (1.0-(4.0*(weight*weight)));
5371 SetPixelInfoPixel(tint_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +00005372 p+=GetPixelChannels(image);
5373 q+=GetPixelChannels(tint_image);
cristy3ed852e2009-09-05 21:47:34 +00005374 }
5375 if (SyncCacheViewAuthenticPixels(tint_view,exception) == MagickFalse)
5376 status=MagickFalse;
5377 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5378 {
5379 MagickBooleanType
5380 proceed;
5381
cristyb5d5f722009-11-04 03:03:49 +00005382#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005383 #pragma omp critical (MagickCore_TintImage)
cristy3ed852e2009-09-05 21:47:34 +00005384#endif
5385 proceed=SetImageProgress(image,TintImageTag,progress++,image->rows);
5386 if (proceed == MagickFalse)
5387 status=MagickFalse;
5388 }
5389 }
5390 tint_view=DestroyCacheView(tint_view);
5391 image_view=DestroyCacheView(image_view);
5392 if (status == MagickFalse)
5393 tint_image=DestroyImage(tint_image);
5394 return(tint_image);
5395}
5396
5397/*
5398%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5399% %
5400% %
5401% %
5402% V i g n e t t e I m a g e %
5403% %
5404% %
5405% %
5406%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5407%
5408% VignetteImage() softens the edges of the image in vignette style.
5409%
5410% The format of the VignetteImage method is:
5411%
5412% Image *VignetteImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00005413% const double sigma,const ssize_t x,const ssize_t y,
cristy05c0c9a2011-09-05 23:16:13 +00005414% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005415%
5416% A description of each parameter follows:
5417%
5418% o image: the image.
5419%
5420% o radius: the radius of the pixel neighborhood.
5421%
5422% o sigma: the standard deviation of the Gaussian, in pixels.
5423%
5424% o x, y: Define the x and y ellipse offset.
5425%
5426% o exception: return any errors or warnings in this structure.
5427%
5428*/
5429MagickExport Image *VignetteImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00005430 const double sigma,const ssize_t x,const ssize_t y,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005431{
5432 char
5433 ellipse[MaxTextExtent];
5434
5435 DrawInfo
5436 *draw_info;
5437
5438 Image
5439 *canvas_image,
5440 *blur_image,
5441 *oval_image,
5442 *vignette_image;
5443
5444 assert(image != (Image *) NULL);
5445 assert(image->signature == MagickSignature);
5446 if (image->debug != MagickFalse)
5447 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5448 assert(exception != (ExceptionInfo *) NULL);
5449 assert(exception->signature == MagickSignature);
5450 canvas_image=CloneImage(image,0,0,MagickTrue,exception);
5451 if (canvas_image == (Image *) NULL)
5452 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005453 if (SetImageStorageClass(canvas_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005454 {
cristy3ed852e2009-09-05 21:47:34 +00005455 canvas_image=DestroyImage(canvas_image);
5456 return((Image *) NULL);
5457 }
5458 canvas_image->matte=MagickTrue;
cristy98621462011-12-31 22:31:11 +00005459 oval_image=CloneImage(canvas_image,canvas_image->columns,canvas_image->rows,
5460 MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005461 if (oval_image == (Image *) NULL)
5462 {
5463 canvas_image=DestroyImage(canvas_image);
5464 return((Image *) NULL);
5465 }
cristy9950d572011-10-01 18:22:35 +00005466 (void) QueryColorCompliance("#000000",AllCompliance,
5467 &oval_image->background_color,exception);
cristyea1a8aa2011-10-20 13:24:06 +00005468 (void) SetImageBackgroundColor(oval_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00005469 draw_info=CloneDrawInfo((const ImageInfo *) NULL,(const DrawInfo *) NULL);
cristy9950d572011-10-01 18:22:35 +00005470 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->fill,
5471 exception);
5472 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->stroke,
5473 exception);
cristy1707c6c2012-01-18 23:30:54 +00005474 (void) FormatLocaleString(ellipse,MaxTextExtent,"ellipse %g,%g,%g,%g,"
5475 "0.0,360.0",image->columns/2.0,image->rows/2.0,image->columns/2.0-x,
5476 image->rows/2.0-y);
cristy3ed852e2009-09-05 21:47:34 +00005477 draw_info->primitive=AcquireString(ellipse);
cristy018f07f2011-09-04 21:15:19 +00005478 (void) DrawImage(oval_image,draw_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00005479 draw_info=DestroyDrawInfo(draw_info);
cristyaa2c16c2012-03-25 22:21:35 +00005480 blur_image=BlurImage(oval_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +00005481 oval_image=DestroyImage(oval_image);
5482 if (blur_image == (Image *) NULL)
5483 {
5484 canvas_image=DestroyImage(canvas_image);
5485 return((Image *) NULL);
5486 }
5487 blur_image->matte=MagickFalse;
cristy39172402012-03-30 13:04:39 +00005488 (void) CompositeImage(canvas_image,blur_image,IntensityCompositeOp,MagickTrue,
5489 0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00005490 blur_image=DestroyImage(blur_image);
5491 vignette_image=MergeImageLayers(canvas_image,FlattenLayer,exception);
5492 canvas_image=DestroyImage(canvas_image);
5493 return(vignette_image);
5494}
5495
5496/*
5497%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5498% %
5499% %
5500% %
5501% W a v e I m a g e %
5502% %
5503% %
5504% %
5505%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5506%
5507% WaveImage() creates a "ripple" effect in the image by shifting the pixels
cristycee97112010-05-28 00:44:52 +00005508% vertically along a sine wave whose amplitude and wavelength is specified
cristy3ed852e2009-09-05 21:47:34 +00005509% by the given parameters.
5510%
5511% The format of the WaveImage method is:
5512%
5513% Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005514% const double wave_length,const PixelInterpolateMethod method,
5515% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005516%
5517% A description of each parameter follows:
5518%
5519% o image: the image.
5520%
5521% o amplitude, wave_length: Define the amplitude and wave length of the
5522% sine wave.
5523%
cristy5c4e2582011-09-11 19:21:03 +00005524% o interpolate: the pixel interpolation method.
5525%
cristy3ed852e2009-09-05 21:47:34 +00005526% o exception: return any errors or warnings in this structure.
5527%
5528*/
5529MagickExport Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005530 const double wave_length,const PixelInterpolateMethod method,
5531 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005532{
5533#define WaveImageTag "Wave/Image"
5534
cristyfa112112010-01-04 17:48:07 +00005535 CacheView
cristyd76c51e2011-03-26 00:21:26 +00005536 *image_view,
cristyfa112112010-01-04 17:48:07 +00005537 *wave_view;
5538
cristy3ed852e2009-09-05 21:47:34 +00005539 Image
5540 *wave_image;
5541
cristy3ed852e2009-09-05 21:47:34 +00005542 MagickBooleanType
5543 status;
5544
cristybb503372010-05-27 20:51:26 +00005545 MagickOffsetType
5546 progress;
5547
cristy3ed852e2009-09-05 21:47:34 +00005548 MagickRealType
5549 *sine_map;
5550
cristybb503372010-05-27 20:51:26 +00005551 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005552 i;
5553
cristybb503372010-05-27 20:51:26 +00005554 ssize_t
5555 y;
5556
cristy3ed852e2009-09-05 21:47:34 +00005557 /*
5558 Initialize wave image attributes.
5559 */
5560 assert(image != (Image *) NULL);
5561 assert(image->signature == MagickSignature);
5562 if (image->debug != MagickFalse)
5563 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5564 assert(exception != (ExceptionInfo *) NULL);
5565 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00005566 wave_image=CloneImage(image,image->columns,(size_t) (image->rows+2.0*
cristy3ed852e2009-09-05 21:47:34 +00005567 fabs(amplitude)),MagickTrue,exception);
5568 if (wave_image == (Image *) NULL)
5569 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005570 if (SetImageStorageClass(wave_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005571 {
cristy3ed852e2009-09-05 21:47:34 +00005572 wave_image=DestroyImage(wave_image);
5573 return((Image *) NULL);
5574 }
cristy4c08aed2011-07-01 19:47:50 +00005575 if (wave_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00005576 wave_image->matte=MagickTrue;
5577 /*
5578 Allocate sine map.
5579 */
5580 sine_map=(MagickRealType *) AcquireQuantumMemory((size_t) wave_image->columns,
5581 sizeof(*sine_map));
5582 if (sine_map == (MagickRealType *) NULL)
5583 {
5584 wave_image=DestroyImage(wave_image);
5585 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
5586 }
cristybb503372010-05-27 20:51:26 +00005587 for (i=0; i < (ssize_t) wave_image->columns; i++)
cristy4205a3c2010-09-12 20:19:59 +00005588 sine_map[i]=fabs(amplitude)+amplitude*sin((double) ((2.0*MagickPI*i)/
5589 wave_length));
cristy3ed852e2009-09-05 21:47:34 +00005590 /*
5591 Wave image.
5592 */
5593 status=MagickTrue;
5594 progress=0;
cristyd76c51e2011-03-26 00:21:26 +00005595 image_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00005596 wave_view=AcquireCacheView(wave_image);
cristyd76c51e2011-03-26 00:21:26 +00005597 (void) SetCacheViewVirtualPixelMethod(image_view,
5598 BackgroundVirtualPixelMethod);
cristyb5d5f722009-11-04 03:03:49 +00005599#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00005600 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00005601#endif
cristybb503372010-05-27 20:51:26 +00005602 for (y=0; y < (ssize_t) wave_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005603 {
cristy4c08aed2011-07-01 19:47:50 +00005604 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005605 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005606
cristye97bb922011-04-03 01:36:52 +00005607 register ssize_t
5608 x;
5609
cristy3ed852e2009-09-05 21:47:34 +00005610 if (status == MagickFalse)
5611 continue;
5612 q=QueueCacheViewAuthenticPixels(wave_view,0,y,wave_image->columns,1,
5613 exception);
cristyacd2ed22011-08-30 01:44:23 +00005614 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005615 {
5616 status=MagickFalse;
5617 continue;
5618 }
cristybb503372010-05-27 20:51:26 +00005619 for (x=0; x < (ssize_t) wave_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005620 {
cristy5c4e2582011-09-11 19:21:03 +00005621 status=InterpolatePixelChannels(image,image_view,wave_image,method,
5622 (double) x,(double) (y-sine_map[x]),q,exception);
cristyed231572011-07-14 02:18:59 +00005623 q+=GetPixelChannels(wave_image);
cristy3ed852e2009-09-05 21:47:34 +00005624 }
5625 if (SyncCacheViewAuthenticPixels(wave_view,exception) == MagickFalse)
5626 status=MagickFalse;
5627 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5628 {
5629 MagickBooleanType
5630 proceed;
5631
cristyb5d5f722009-11-04 03:03:49 +00005632#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005633 #pragma omp critical (MagickCore_WaveImage)
cristy3ed852e2009-09-05 21:47:34 +00005634#endif
5635 proceed=SetImageProgress(image,WaveImageTag,progress++,image->rows);
5636 if (proceed == MagickFalse)
5637 status=MagickFalse;
5638 }
5639 }
5640 wave_view=DestroyCacheView(wave_view);
cristyd76c51e2011-03-26 00:21:26 +00005641 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00005642 sine_map=(MagickRealType *) RelinquishMagickMemory(sine_map);
5643 if (status == MagickFalse)
5644 wave_image=DestroyImage(wave_image);
5645 return(wave_image);
5646}