blob: 900bd3717c421fb2209e15458e5be36a9a63da04 [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 %
cristyde984cd2013-12-01 14:49:27 +000016% Cristy %
cristy3ed852e2009-09-05 21:47:34 +000017% October 1996 %
18% %
19% %
cristyfe676ee2013-11-18 13:03:38 +000020% Copyright 1999-2014 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"
cristy6a2180c2013-05-27 10:28:36 +000049#include "MagickCore/channel.h"
cristy4c08aed2011-07-01 19:47:50 +000050#include "MagickCore/color.h"
51#include "MagickCore/color-private.h"
cristy9b7a4fc2012-04-08 22:26:56 +000052#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000053#include "MagickCore/composite.h"
54#include "MagickCore/decorate.h"
cristyc53413d2011-11-17 13:04:26 +000055#include "MagickCore/distort.h"
cristy4c08aed2011-07-01 19:47:50 +000056#include "MagickCore/draw.h"
57#include "MagickCore/effect.h"
58#include "MagickCore/enhance.h"
59#include "MagickCore/exception.h"
60#include "MagickCore/exception-private.h"
61#include "MagickCore/fx.h"
62#include "MagickCore/fx-private.h"
63#include "MagickCore/gem.h"
cristy8ea81222011-09-04 10:33:32 +000064#include "MagickCore/gem-private.h"
cristy4c08aed2011-07-01 19:47:50 +000065#include "MagickCore/geometry.h"
66#include "MagickCore/layer.h"
67#include "MagickCore/list.h"
68#include "MagickCore/log.h"
69#include "MagickCore/image.h"
70#include "MagickCore/image-private.h"
71#include "MagickCore/magick.h"
72#include "MagickCore/memory_.h"
73#include "MagickCore/monitor.h"
74#include "MagickCore/monitor-private.h"
75#include "MagickCore/option.h"
76#include "MagickCore/pixel.h"
77#include "MagickCore/pixel-accessor.h"
78#include "MagickCore/property.h"
79#include "MagickCore/quantum.h"
80#include "MagickCore/quantum-private.h"
81#include "MagickCore/random_.h"
82#include "MagickCore/random-private.h"
83#include "MagickCore/resample.h"
84#include "MagickCore/resample-private.h"
85#include "MagickCore/resize.h"
cristy57340e02012-05-05 00:53:23 +000086#include "MagickCore/resource_.h"
cristy4c08aed2011-07-01 19:47:50 +000087#include "MagickCore/splay-tree.h"
88#include "MagickCore/statistic.h"
89#include "MagickCore/string_.h"
90#include "MagickCore/string-private.h"
91#include "MagickCore/thread-private.h"
92#include "MagickCore/transform.h"
93#include "MagickCore/utility.h"
cristy3ed852e2009-09-05 21:47:34 +000094
95/*
96 Define declarations.
97*/
cristy41acfc72013-11-14 16:47:53 +000098#define LeftShiftOperator 0xf5U
99#define RightShiftOperator 0xf6U
100#define LessThanEqualOperator 0xf7U
101#define GreaterThanEqualOperator 0xf8U
102#define EqualOperator 0xf9U
103#define NotEqualOperator 0xfaU
104#define LogicalAndOperator 0xfbU
105#define LogicalOrOperator 0xfcU
106#define ExponentialNotation 0xfdU
107
cristy3ed852e2009-09-05 21:47:34 +0000108struct _FxInfo
109{
110 const Image
111 *images;
112
cristy3ed852e2009-09-05 21:47:34 +0000113 char
114 *expression;
115
116 FILE
117 *file;
118
119 SplayTreeInfo
120 *colors,
121 *symbols;
122
cristyd76c51e2011-03-26 00:21:26 +0000123 CacheView
124 **view;
cristy3ed852e2009-09-05 21:47:34 +0000125
126 RandomInfo
127 *random_info;
128
129 ExceptionInfo
130 *exception;
131};
132
133/*
134%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
135% %
136% %
137% %
138+ A c q u i r e F x I n f o %
139% %
140% %
141% %
142%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
143%
144% AcquireFxInfo() allocates the FxInfo structure.
145%
146% The format of the AcquireFxInfo method is:
147%
cristydb070952012-04-20 14:33:00 +0000148% FxInfo *AcquireFxInfo(Image *image,const char *expression,
149% ExceptionInfo *exception)
cristy0a9b3722010-10-23 18:45:49 +0000150%
cristy3ed852e2009-09-05 21:47:34 +0000151% A description of each parameter follows:
152%
153% o image: the image.
154%
155% o expression: the expression.
156%
cristydb070952012-04-20 14:33:00 +0000157% o exception: return any errors or warnings in this structure.
158%
cristy3ed852e2009-09-05 21:47:34 +0000159*/
cristydb070952012-04-20 14:33:00 +0000160MagickPrivate FxInfo *AcquireFxInfo(const Image *image,const char *expression,
161 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000162{
163 char
164 fx_op[2];
165
cristycb180922011-03-11 14:41:24 +0000166 const Image
167 *next;
168
cristy3ed852e2009-09-05 21:47:34 +0000169 FxInfo
170 *fx_info;
171
cristybb503372010-05-27 20:51:26 +0000172 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000173 i;
174
cristy73bd4a52010-10-05 11:24:23 +0000175 fx_info=(FxInfo *) AcquireMagickMemory(sizeof(*fx_info));
cristy3ed852e2009-09-05 21:47:34 +0000176 if (fx_info == (FxInfo *) NULL)
177 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
178 (void) ResetMagickMemory(fx_info,0,sizeof(*fx_info));
179 fx_info->exception=AcquireExceptionInfo();
anthony7d86e172011-03-23 12:37:06 +0000180 fx_info->images=image;
cristy3ed852e2009-09-05 21:47:34 +0000181 fx_info->colors=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
cristy1e12eb42013-08-17 16:19:01 +0000182 RelinquishAlignedMemory);
cristy3ed852e2009-09-05 21:47:34 +0000183 fx_info->symbols=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
184 RelinquishMagickMemory);
cristyd76c51e2011-03-26 00:21:26 +0000185 fx_info->view=(CacheView **) AcquireQuantumMemory(GetImageListLength(
186 fx_info->images),sizeof(*fx_info->view));
187 if (fx_info->view == (CacheView **) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000188 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
cristya2262262011-03-11 02:50:37 +0000189 i=0;
cristy0ea377f2011-03-24 00:54:19 +0000190 next=GetFirstImageInList(fx_info->images);
191 for ( ; next != (Image *) NULL; next=next->next)
cristy3ed852e2009-09-05 21:47:34 +0000192 {
cristy46ff2672012-12-14 15:32:26 +0000193 fx_info->view[i]=AcquireVirtualCacheView(next,exception);
cristya2262262011-03-11 02:50:37 +0000194 i++;
cristy3ed852e2009-09-05 21:47:34 +0000195 }
196 fx_info->random_info=AcquireRandomInfo();
197 fx_info->expression=ConstantString(expression);
198 fx_info->file=stderr;
199 (void) SubstituteString(&fx_info->expression," ",""); /* compact string */
cristy37af0912011-05-23 16:09:42 +0000200 /*
201 Force right-to-left associativity for unary negation.
202 */
203 (void) SubstituteString(&fx_info->expression,"-","-1.0*");
cristy422d5502012-12-22 22:20:57 +0000204 (void) SubstituteString(&fx_info->expression,"^-1.0*","^-");
cristy8fbb96d2012-07-18 01:18:52 +0000205 (void) SubstituteString(&fx_info->expression,"E-1.0*","E-");
206 (void) SubstituteString(&fx_info->expression,"e-1.0*","e-");
cristy8b8a3ae2010-10-23 18:49:46 +0000207 /*
cristydf751512013-08-17 14:19:12 +0000208 Convert compound to simple operators.
cristy3ed852e2009-09-05 21:47:34 +0000209 */
210 fx_op[1]='\0';
cristy41acfc72013-11-14 16:47:53 +0000211 *fx_op=(char) LeftShiftOperator;
cristy3ed852e2009-09-05 21:47:34 +0000212 (void) SubstituteString(&fx_info->expression,"<<",fx_op);
cristy41acfc72013-11-14 16:47:53 +0000213 *fx_op=(char) RightShiftOperator;
cristy3ed852e2009-09-05 21:47:34 +0000214 (void) SubstituteString(&fx_info->expression,">>",fx_op);
cristy41acfc72013-11-14 16:47:53 +0000215 *fx_op=(char) LessThanEqualOperator;
cristy3ed852e2009-09-05 21:47:34 +0000216 (void) SubstituteString(&fx_info->expression,"<=",fx_op);
cristy41acfc72013-11-14 16:47:53 +0000217 *fx_op=(char) GreaterThanEqualOperator;
cristy3ed852e2009-09-05 21:47:34 +0000218 (void) SubstituteString(&fx_info->expression,">=",fx_op);
cristy41acfc72013-11-14 16:47:53 +0000219 *fx_op=(char) EqualOperator;
cristy3ed852e2009-09-05 21:47:34 +0000220 (void) SubstituteString(&fx_info->expression,"==",fx_op);
cristy41acfc72013-11-14 16:47:53 +0000221 *fx_op=(char) NotEqualOperator;
cristy3ed852e2009-09-05 21:47:34 +0000222 (void) SubstituteString(&fx_info->expression,"!=",fx_op);
cristy41acfc72013-11-14 16:47:53 +0000223 *fx_op=(char) LogicalAndOperator;
cristy3ed852e2009-09-05 21:47:34 +0000224 (void) SubstituteString(&fx_info->expression,"&&",fx_op);
cristy41acfc72013-11-14 16:47:53 +0000225 *fx_op=(char) LogicalOrOperator;
cristy3ed852e2009-09-05 21:47:34 +0000226 (void) SubstituteString(&fx_info->expression,"||",fx_op);
cristy41acfc72013-11-14 16:47:53 +0000227 *fx_op=(char) ExponentialNotation;
cristy116af162010-08-13 01:25:47 +0000228 (void) SubstituteString(&fx_info->expression,"**",fx_op);
cristy3ed852e2009-09-05 21:47:34 +0000229 return(fx_info);
230}
231
232/*
233%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234% %
235% %
236% %
237% A d d N o i s e I m a g e %
238% %
239% %
240% %
241%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
242%
243% AddNoiseImage() adds random noise to the image.
244%
245% The format of the AddNoiseImage method is:
246%
247% Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
cristy9ed1f812011-10-08 02:00:08 +0000248% const double attenuate,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000249%
250% A description of each parameter follows:
251%
252% o image: the image.
253%
254% o channel: the channel type.
255%
256% o noise_type: The type of noise: Uniform, Gaussian, Multiplicative,
257% Impulse, Laplacian, or Poisson.
258%
cristy9ed1f812011-10-08 02:00:08 +0000259% o attenuate: attenuate the random distribution.
260%
cristy3ed852e2009-09-05 21:47:34 +0000261% o exception: return any errors or warnings in this structure.
262%
263*/
cristy9ed1f812011-10-08 02:00:08 +0000264MagickExport Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
265 const double attenuate,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000266{
267#define AddNoiseImageTag "AddNoise/Image"
268
cristyfa112112010-01-04 17:48:07 +0000269 CacheView
270 *image_view,
271 *noise_view;
272
cristy3ed852e2009-09-05 21:47:34 +0000273 Image
274 *noise_image;
275
cristy3ed852e2009-09-05 21:47:34 +0000276 MagickBooleanType
277 status;
278
cristybb503372010-05-27 20:51:26 +0000279 MagickOffsetType
280 progress;
281
cristy3ed852e2009-09-05 21:47:34 +0000282 RandomInfo
cristyfa112112010-01-04 17:48:07 +0000283 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +0000284
cristybb503372010-05-27 20:51:26 +0000285 ssize_t
286 y;
287
glennrpf7659d72012-09-24 18:14:56 +0000288#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy57340e02012-05-05 00:53:23 +0000289 unsigned long
290 key;
glennrpf7659d72012-09-24 18:14:56 +0000291#endif
cristy57340e02012-05-05 00:53:23 +0000292
cristy3ed852e2009-09-05 21:47:34 +0000293 /*
294 Initialize noise image attributes.
295 */
296 assert(image != (const Image *) NULL);
297 assert(image->signature == MagickSignature);
298 if (image->debug != MagickFalse)
299 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
300 assert(exception != (ExceptionInfo *) NULL);
301 assert(exception->signature == MagickSignature);
cristyb2145892011-10-10 00:55:32 +0000302 noise_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000303 if (noise_image == (Image *) NULL)
304 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000305 if (SetImageStorageClass(noise_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000306 {
cristy3ed852e2009-09-05 21:47:34 +0000307 noise_image=DestroyImage(noise_image);
308 return((Image *) NULL);
309 }
310 /*
311 Add noise in each row.
312 */
cristy3ed852e2009-09-05 21:47:34 +0000313 status=MagickTrue;
314 progress=0;
315 random_info=AcquireRandomInfoThreadSet();
cristy46ff2672012-12-14 15:32:26 +0000316 image_view=AcquireVirtualCacheView(image,exception);
317 noise_view=AcquireAuthenticCacheView(noise_image,exception);
cristy319a1e72010-02-21 15:13:11 +0000318#if defined(MAGICKCORE_OPENMP_SUPPORT)
glennrpf7659d72012-09-24 18:14:56 +0000319 key=GetRandomSecretKey(random_info[0]);
cristy57340e02012-05-05 00:53:23 +0000320 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +0000321 magick_threads(image,noise_image,image->rows,key == ~0UL)
cristy3ed852e2009-09-05 21:47:34 +0000322#endif
cristybb503372010-05-27 20:51:26 +0000323 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000324 {
cristy5c9e6f22010-09-17 17:31:01 +0000325 const int
326 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +0000327
cristy3ed852e2009-09-05 21:47:34 +0000328 MagickBooleanType
329 sync;
330
cristy4c08aed2011-07-01 19:47:50 +0000331 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000332 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000333
cristybb503372010-05-27 20:51:26 +0000334 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000335 x;
336
cristy4c08aed2011-07-01 19:47:50 +0000337 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000338 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000339
340 if (status == MagickFalse)
341 continue;
342 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +0000343 q=QueueCacheViewAuthenticPixels(noise_view,0,y,noise_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +0000344 exception);
cristy4c08aed2011-07-01 19:47:50 +0000345 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000346 {
347 status=MagickFalse;
348 continue;
349 }
cristybb503372010-05-27 20:51:26 +0000350 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000351 {
cristy850b3072011-10-08 01:38:05 +0000352 register ssize_t
353 i;
354
355 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
356 {
cristy5a23c552013-02-13 14:34:28 +0000357 PixelChannel channel=GetPixelChannelChannel(image,i);
358 PixelTrait traits=GetPixelChannelTraits(image,channel);
359 PixelTrait noise_traits=GetPixelChannelTraits(noise_image,channel);
cristy850b3072011-10-08 01:38:05 +0000360 if ((traits == UndefinedPixelTrait) ||
361 (noise_traits == UndefinedPixelTrait))
362 continue;
cristy1eced092012-08-10 23:10:56 +0000363 if (((noise_traits & CopyPixelTrait) != 0) ||
cristy883fde12013-04-08 00:50:13 +0000364 (GetPixelReadMask(image,p) == 0))
cristyb2145892011-10-10 00:55:32 +0000365 {
366 SetPixelChannel(noise_image,channel,p[i],q);
367 continue;
368 }
cristy850b3072011-10-08 01:38:05 +0000369 SetPixelChannel(noise_image,channel,ClampToQuantum(
370 GenerateDifferentialNoise(random_info[id],p[i],noise_type,attenuate)),
371 q);
372 }
cristyed231572011-07-14 02:18:59 +0000373 p+=GetPixelChannels(image);
374 q+=GetPixelChannels(noise_image);
cristy3ed852e2009-09-05 21:47:34 +0000375 }
376 sync=SyncCacheViewAuthenticPixels(noise_view,exception);
377 if (sync == MagickFalse)
378 status=MagickFalse;
379 if (image->progress_monitor != (MagickProgressMonitor) NULL)
380 {
381 MagickBooleanType
382 proceed;
383
cristyb5d5f722009-11-04 03:03:49 +0000384#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy319a1e72010-02-21 15:13:11 +0000385 #pragma omp critical (MagickCore_AddNoiseImage)
cristy3ed852e2009-09-05 21:47:34 +0000386#endif
387 proceed=SetImageProgress(image,AddNoiseImageTag,progress++,
388 image->rows);
389 if (proceed == MagickFalse)
390 status=MagickFalse;
391 }
392 }
393 noise_view=DestroyCacheView(noise_view);
394 image_view=DestroyCacheView(image_view);
395 random_info=DestroyRandomInfoThreadSet(random_info);
396 if (status == MagickFalse)
397 noise_image=DestroyImage(noise_image);
398 return(noise_image);
399}
400
401/*
402%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
403% %
404% %
405% %
406% B l u e S h i f t I m a g e %
407% %
408% %
409% %
410%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
411%
412% BlueShiftImage() mutes the colors of the image to simulate a scene at
413% nighttime in the moonlight.
414%
415% The format of the BlueShiftImage method is:
416%
417% Image *BlueShiftImage(const Image *image,const double factor,
418% ExceptionInfo *exception)
419%
420% A description of each parameter follows:
421%
422% o image: the image.
423%
424% o factor: the shift factor.
425%
426% o exception: return any errors or warnings in this structure.
427%
428*/
429MagickExport Image *BlueShiftImage(const Image *image,const double factor,
430 ExceptionInfo *exception)
431{
432#define BlueShiftImageTag "BlueShift/Image"
433
cristyc4c8d132010-01-07 01:58:38 +0000434 CacheView
435 *image_view,
436 *shift_view;
437
cristy3ed852e2009-09-05 21:47:34 +0000438 Image
439 *shift_image;
440
cristy3ed852e2009-09-05 21:47:34 +0000441 MagickBooleanType
442 status;
443
cristybb503372010-05-27 20:51:26 +0000444 MagickOffsetType
445 progress;
446
447 ssize_t
448 y;
449
cristy3ed852e2009-09-05 21:47:34 +0000450 /*
451 Allocate blue shift image.
452 */
453 assert(image != (const Image *) NULL);
454 assert(image->signature == MagickSignature);
455 if (image->debug != MagickFalse)
456 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
457 assert(exception != (ExceptionInfo *) NULL);
458 assert(exception->signature == MagickSignature);
cristya6d7a9b2012-01-18 20:04:48 +0000459 shift_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000460 if (shift_image == (Image *) NULL)
461 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000462 if (SetImageStorageClass(shift_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000463 {
cristy3ed852e2009-09-05 21:47:34 +0000464 shift_image=DestroyImage(shift_image);
465 return((Image *) NULL);
466 }
467 /*
468 Blue-shift DirectClass image.
469 */
470 status=MagickTrue;
471 progress=0;
cristy46ff2672012-12-14 15:32:26 +0000472 image_view=AcquireVirtualCacheView(image,exception);
473 shift_view=AcquireAuthenticCacheView(shift_image,exception);
cristy319a1e72010-02-21 15:13:11 +0000474#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000475 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +0000476 magick_threads(image,shift_image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +0000477#endif
cristybb503372010-05-27 20:51:26 +0000478 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000479 {
480 MagickBooleanType
481 sync;
482
cristy4c08aed2011-07-01 19:47:50 +0000483 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000484 pixel;
485
486 Quantum
487 quantum;
488
cristy4c08aed2011-07-01 19:47:50 +0000489 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000490 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000491
cristybb503372010-05-27 20:51:26 +0000492 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000493 x;
494
cristy4c08aed2011-07-01 19:47:50 +0000495 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000496 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000497
498 if (status == MagickFalse)
499 continue;
500 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
501 q=QueueCacheViewAuthenticPixels(shift_view,0,y,shift_image->columns,1,
502 exception);
cristy4c08aed2011-07-01 19:47:50 +0000503 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000504 {
505 status=MagickFalse;
506 continue;
507 }
cristybb503372010-05-27 20:51:26 +0000508 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000509 {
cristy4c08aed2011-07-01 19:47:50 +0000510 quantum=GetPixelRed(image,p);
511 if (GetPixelGreen(image,p) < quantum)
512 quantum=GetPixelGreen(image,p);
513 if (GetPixelBlue(image,p) < quantum)
514 quantum=GetPixelBlue(image,p);
515 pixel.red=0.5*(GetPixelRed(image,p)+factor*quantum);
516 pixel.green=0.5*(GetPixelGreen(image,p)+factor*quantum);
517 pixel.blue=0.5*(GetPixelBlue(image,p)+factor*quantum);
518 quantum=GetPixelRed(image,p);
519 if (GetPixelGreen(image,p) > quantum)
520 quantum=GetPixelGreen(image,p);
521 if (GetPixelBlue(image,p) > quantum)
522 quantum=GetPixelBlue(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000523 pixel.red=0.5*(pixel.red+factor*quantum);
524 pixel.green=0.5*(pixel.green+factor*quantum);
525 pixel.blue=0.5*(pixel.blue+factor*quantum);
cristy4c08aed2011-07-01 19:47:50 +0000526 SetPixelRed(shift_image,ClampToQuantum(pixel.red),q);
527 SetPixelGreen(shift_image,ClampToQuantum(pixel.green),q);
528 SetPixelBlue(shift_image,ClampToQuantum(pixel.blue),q);
cristyed231572011-07-14 02:18:59 +0000529 p+=GetPixelChannels(image);
530 q+=GetPixelChannels(shift_image);
cristy3ed852e2009-09-05 21:47:34 +0000531 }
532 sync=SyncCacheViewAuthenticPixels(shift_view,exception);
533 if (sync == MagickFalse)
534 status=MagickFalse;
535 if (image->progress_monitor != (MagickProgressMonitor) NULL)
536 {
537 MagickBooleanType
538 proceed;
539
cristy319a1e72010-02-21 15:13:11 +0000540#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +0000541 #pragma omp critical (MagickCore_BlueShiftImage)
cristy3ed852e2009-09-05 21:47:34 +0000542#endif
543 proceed=SetImageProgress(image,BlueShiftImageTag,progress++,
544 image->rows);
545 if (proceed == MagickFalse)
546 status=MagickFalse;
547 }
548 }
549 image_view=DestroyCacheView(image_view);
550 shift_view=DestroyCacheView(shift_view);
551 if (status == MagickFalse)
552 shift_image=DestroyImage(shift_image);
553 return(shift_image);
554}
555
556/*
557%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
558% %
559% %
560% %
561% C h a r c o a l I m a g e %
562% %
563% %
564% %
565%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
566%
567% CharcoalImage() creates a new image that is a copy of an existing one with
568% the edge highlighted. It allocates the memory necessary for the new Image
569% structure and returns a pointer to the new image.
570%
571% The format of the CharcoalImage method is:
572%
573% Image *CharcoalImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +0000574% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000575%
576% A description of each parameter follows:
577%
578% o image: the image.
579%
580% o radius: the radius of the pixel neighborhood.
581%
582% o sigma: the standard deviation of the Gaussian, in pixels.
583%
584% o exception: return any errors or warnings in this structure.
585%
586*/
587MagickExport Image *CharcoalImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +0000588 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000589{
590 Image
591 *charcoal_image,
592 *clone_image,
593 *edge_image;
594
595 assert(image != (Image *) NULL);
596 assert(image->signature == MagickSignature);
597 if (image->debug != MagickFalse)
598 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
599 assert(exception != (ExceptionInfo *) NULL);
600 assert(exception->signature == MagickSignature);
601 clone_image=CloneImage(image,0,0,MagickTrue,exception);
602 if (clone_image == (Image *) NULL)
603 return((Image *) NULL);
cristyb2f4d5d2013-05-20 20:10:33 +0000604 edge_image=EdgeImage(clone_image,radius,exception);
cristy3ed852e2009-09-05 21:47:34 +0000605 clone_image=DestroyImage(clone_image);
606 if (edge_image == (Image *) NULL)
607 return((Image *) NULL);
cristyaa2c16c2012-03-25 22:21:35 +0000608 charcoal_image=BlurImage(edge_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +0000609 edge_image=DestroyImage(edge_image);
610 if (charcoal_image == (Image *) NULL)
611 return((Image *) NULL);
cristye23ec9d2011-08-16 18:15:40 +0000612 (void) NormalizeImage(charcoal_image,exception);
cristyb3e7c6c2011-07-24 01:43:55 +0000613 (void) NegateImage(charcoal_image,MagickFalse,exception);
cristye247eff2013-04-15 11:50:40 +0000614 (void) GrayscaleImage(charcoal_image,image->intensity,exception);
cristy3ed852e2009-09-05 21:47:34 +0000615 return(charcoal_image);
616}
617
618/*
619%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
620% %
621% %
622% %
623% C o l o r i z e I m a g e %
624% %
625% %
626% %
627%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
628%
629% ColorizeImage() blends the fill color with each pixel in the image.
630% A percentage blend is specified with opacity. Control the application
631% of different color components by specifying a different percentage for
632% each component (e.g. 90/100/10 is 90% red, 100% green, and 10% blue).
633%
634% The format of the ColorizeImage method is:
635%
cristyc7e6ff62011-10-03 13:46:11 +0000636% Image *ColorizeImage(const Image *image,const char *blend,
637% const PixelInfo *colorize,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000638%
639% A description of each parameter follows:
640%
641% o image: the image.
642%
cristyc7e6ff62011-10-03 13:46:11 +0000643% o blend: A character string indicating the level of blending as a
cristy3ed852e2009-09-05 21:47:34 +0000644% percentage.
645%
646% o colorize: A color value.
647%
648% o exception: return any errors or warnings in this structure.
649%
650*/
cristyc7e6ff62011-10-03 13:46:11 +0000651MagickExport Image *ColorizeImage(const Image *image,const char *blend,
652 const PixelInfo *colorize,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000653{
654#define ColorizeImageTag "Colorize/Image"
cristy0cf6da52012-08-25 00:35:24 +0000655#define Colorize(pixel,blend_percentage,colorize) \
656 (((pixel)*(100.0-(blend_percentage))+(colorize)*(blend_percentage))/100.0)
cristy3ed852e2009-09-05 21:47:34 +0000657
cristyc4c8d132010-01-07 01:58:38 +0000658 CacheView
659 *colorize_view,
660 *image_view;
661
cristy3ed852e2009-09-05 21:47:34 +0000662 GeometryInfo
663 geometry_info;
664
665 Image
666 *colorize_image;
667
cristy3ed852e2009-09-05 21:47:34 +0000668 MagickBooleanType
669 status;
670
cristybb503372010-05-27 20:51:26 +0000671 MagickOffsetType
672 progress;
673
cristy3ed852e2009-09-05 21:47:34 +0000674 MagickStatusType
675 flags;
676
cristyc7e6ff62011-10-03 13:46:11 +0000677 PixelInfo
cristy20c3aed2012-04-10 01:06:21 +0000678 blend_percentage;
cristyc7e6ff62011-10-03 13:46:11 +0000679
cristybb503372010-05-27 20:51:26 +0000680 ssize_t
681 y;
682
cristy3ed852e2009-09-05 21:47:34 +0000683 /*
684 Allocate colorized image.
685 */
686 assert(image != (const Image *) NULL);
687 assert(image->signature == MagickSignature);
688 if (image->debug != MagickFalse)
689 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
690 assert(exception != (ExceptionInfo *) NULL);
691 assert(exception->signature == MagickSignature);
cristy768165d2012-04-09 15:01:35 +0000692 colorize_image=CloneImage(image,image->columns,image->rows,MagickTrue,
693 exception);
694 if (colorize_image == (Image *) NULL)
695 return((Image *) NULL);
696 if (SetImageStorageClass(colorize_image,DirectClass,exception) == MagickFalse)
697 {
698 colorize_image=DestroyImage(colorize_image);
699 return((Image *) NULL);
700 }
cristyf8da9f92013-04-16 19:52:28 +0000701 if ((IsGrayColorspace(image->colorspace) != MagickFalse) ||
cristy4dab3802013-03-15 22:08:15 +0000702 (IsPixelInfoGray(colorize) != MagickFalse))
cristya6400b12013-03-15 12:20:18 +0000703 (void) SetImageColorspace(colorize_image,sRGBColorspace,exception);
cristy8a46d822012-08-28 23:32:39 +0000704 if ((colorize_image->alpha_trait != BlendPixelTrait) &&
705 (colorize->alpha_trait == BlendPixelTrait))
cristy768165d2012-04-09 15:01:35 +0000706 (void) SetImageAlpha(colorize_image,OpaqueAlpha,exception);
707 if (blend == (const char *) NULL)
708 return(colorize_image);
cristy20c3aed2012-04-10 01:06:21 +0000709 GetPixelInfo(image,&blend_percentage);
710 flags=ParseGeometry(blend,&geometry_info);
711 blend_percentage.red=geometry_info.rho;
712 blend_percentage.green=geometry_info.rho;
713 blend_percentage.blue=geometry_info.rho;
714 blend_percentage.black=geometry_info.rho;
cristy3ee7de52012-04-14 23:40:47 +0000715 blend_percentage.alpha=geometry_info.rho;
cristy20c3aed2012-04-10 01:06:21 +0000716 if ((flags & SigmaValue) != 0)
717 blend_percentage.green=geometry_info.sigma;
718 if ((flags & XiValue) != 0)
719 blend_percentage.blue=geometry_info.xi;
720 if ((flags & PsiValue) != 0)
721 blend_percentage.alpha=geometry_info.psi;
722 if (blend_percentage.colorspace == CMYKColorspace)
723 {
724 if ((flags & PsiValue) != 0)
725 blend_percentage.black=geometry_info.psi;
726 if ((flags & ChiValue) != 0)
727 blend_percentage.alpha=geometry_info.chi;
728 }
cristy3ed852e2009-09-05 21:47:34 +0000729 /*
730 Colorize DirectClass image.
731 */
732 status=MagickTrue;
733 progress=0;
cristy46ff2672012-12-14 15:32:26 +0000734 image_view=AcquireVirtualCacheView(image,exception);
735 colorize_view=AcquireAuthenticCacheView(colorize_image,exception);
cristy319a1e72010-02-21 15:13:11 +0000736#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000737 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +0000738 magick_threads(image,colorize_image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +0000739#endif
cristybb503372010-05-27 20:51:26 +0000740 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000741 {
742 MagickBooleanType
743 sync;
744
cristy4c08aed2011-07-01 19:47:50 +0000745 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000746 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000747
cristy4c08aed2011-07-01 19:47:50 +0000748 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000749 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000750
cristy4a37c622012-08-23 18:47:56 +0000751 register ssize_t
752 x;
753
cristy3ed852e2009-09-05 21:47:34 +0000754 if (status == MagickFalse)
755 continue;
756 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
757 q=QueueCacheViewAuthenticPixels(colorize_view,0,y,colorize_image->columns,1,
758 exception);
cristy4c08aed2011-07-01 19:47:50 +0000759 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000760 {
761 status=MagickFalse;
762 continue;
763 }
cristybb503372010-05-27 20:51:26 +0000764 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000765 {
cristy0cf6da52012-08-25 00:35:24 +0000766 register ssize_t
767 i;
768
769 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
770 {
cristy5a23c552013-02-13 14:34:28 +0000771 PixelChannel channel=GetPixelChannelChannel(image,i);
772 PixelTrait traits=GetPixelChannelTraits(image,channel);
773 PixelTrait colorize_traits=GetPixelChannelTraits(colorize_image,
774 channel);
cristy0cf6da52012-08-25 00:35:24 +0000775 if ((traits == UndefinedPixelTrait) ||
776 (colorize_traits == UndefinedPixelTrait))
cristyd6803382012-04-10 01:41:25 +0000777 continue;
cristy0cf6da52012-08-25 00:35:24 +0000778 if (((colorize_traits & CopyPixelTrait) != 0) ||
cristy883fde12013-04-08 00:50:13 +0000779 (GetPixelReadMask(image,p) == 0))
cristy0cf6da52012-08-25 00:35:24 +0000780 {
781 SetPixelChannel(colorize_image,channel,p[i],q);
782 continue;
783 }
cristyb7113232013-02-15 00:35:19 +0000784 SetPixelChannel(colorize_image,channel,ClampToQuantum(Colorize(p[i],
785 GetPixelInfoChannel(&blend_percentage,channel),GetPixelInfoChannel(
786 colorize,channel))),q);
cristy0cf6da52012-08-25 00:35:24 +0000787 }
cristyed231572011-07-14 02:18:59 +0000788 p+=GetPixelChannels(image);
789 q+=GetPixelChannels(colorize_image);
cristy3ed852e2009-09-05 21:47:34 +0000790 }
791 sync=SyncCacheViewAuthenticPixels(colorize_view,exception);
792 if (sync == MagickFalse)
793 status=MagickFalse;
794 if (image->progress_monitor != (MagickProgressMonitor) NULL)
795 {
796 MagickBooleanType
797 proceed;
798
cristy319a1e72010-02-21 15:13:11 +0000799#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +0000800 #pragma omp critical (MagickCore_ColorizeImage)
cristy3ed852e2009-09-05 21:47:34 +0000801#endif
802 proceed=SetImageProgress(image,ColorizeImageTag,progress++,image->rows);
803 if (proceed == MagickFalse)
804 status=MagickFalse;
805 }
806 }
807 image_view=DestroyCacheView(image_view);
808 colorize_view=DestroyCacheView(colorize_view);
809 if (status == MagickFalse)
810 colorize_image=DestroyImage(colorize_image);
811 return(colorize_image);
812}
813
814/*
815%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
816% %
817% %
818% %
cristye6365592010-04-02 17:31:23 +0000819% C o l o r M a t r i x I m a g e %
820% %
821% %
822% %
823%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
824%
825% ColorMatrixImage() applies color transformation to an image. This method
826% permits saturation changes, hue rotation, luminance to alpha, and various
827% other effects. Although variable-sized transformation matrices can be used,
828% typically one uses a 5x5 matrix for an RGBA image and a 6x6 for CMYKA
829% (or RGBA with offsets). The matrix is similar to those used by Adobe Flash
830% except offsets are in column 6 rather than 5 (in support of CMYKA images)
831% and offsets are normalized (divide Flash offset by 255).
832%
833% The format of the ColorMatrixImage method is:
834%
835% Image *ColorMatrixImage(const Image *image,
836% const KernelInfo *color_matrix,ExceptionInfo *exception)
837%
838% A description of each parameter follows:
839%
840% o image: the image.
841%
842% o color_matrix: the color matrix.
843%
844% o exception: return any errors or warnings in this structure.
845%
846*/
anthonyfd706f92012-01-19 04:22:02 +0000847/* FUTURE: modify to make use of a MagickMatrix Mutliply function
848 That should be provided in "matrix.c"
849 (ASIDE: actually distorts should do this too but currently doesn't)
850*/
851
cristye6365592010-04-02 17:31:23 +0000852MagickExport Image *ColorMatrixImage(const Image *image,
853 const KernelInfo *color_matrix,ExceptionInfo *exception)
854{
855#define ColorMatrixImageTag "ColorMatrix/Image"
856
857 CacheView
858 *color_view,
859 *image_view;
860
861 double
862 ColorMatrix[6][6] =
863 {
864 { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
865 { 0.0, 1.0, 0.0, 0.0, 0.0, 0.0 },
866 { 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 },
867 { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0 },
868 { 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 },
869 { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 }
870 };
871
872 Image
873 *color_image;
874
cristye6365592010-04-02 17:31:23 +0000875 MagickBooleanType
876 status;
877
cristybb503372010-05-27 20:51:26 +0000878 MagickOffsetType
879 progress;
880
881 register ssize_t
cristye6365592010-04-02 17:31:23 +0000882 i;
883
cristybb503372010-05-27 20:51:26 +0000884 ssize_t
885 u,
886 v,
887 y;
888
cristye6365592010-04-02 17:31:23 +0000889 /*
anthonyfd706f92012-01-19 04:22:02 +0000890 Map given color_matrix, into a 6x6 matrix RGBKA and a constant
cristye6365592010-04-02 17:31:23 +0000891 */
892 assert(image != (Image *) NULL);
893 assert(image->signature == MagickSignature);
894 if (image->debug != MagickFalse)
895 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
896 assert(exception != (ExceptionInfo *) NULL);
897 assert(exception->signature == MagickSignature);
898 i=0;
cristybb503372010-05-27 20:51:26 +0000899 for (v=0; v < (ssize_t) color_matrix->height; v++)
900 for (u=0; u < (ssize_t) color_matrix->width; u++)
cristye6365592010-04-02 17:31:23 +0000901 {
902 if ((v < 6) && (u < 6))
903 ColorMatrix[v][u]=color_matrix->values[i];
904 i++;
905 }
906 /*
907 Initialize color image.
908 */
cristy12550e62010-06-07 12:46:40 +0000909 color_image=CloneImage(image,0,0,MagickTrue,exception);
cristye6365592010-04-02 17:31:23 +0000910 if (color_image == (Image *) NULL)
911 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000912 if (SetImageStorageClass(color_image,DirectClass,exception) == MagickFalse)
cristye6365592010-04-02 17:31:23 +0000913 {
cristye6365592010-04-02 17:31:23 +0000914 color_image=DestroyImage(color_image);
915 return((Image *) NULL);
916 }
917 if (image->debug != MagickFalse)
918 {
919 char
920 format[MaxTextExtent],
921 *message;
922
923 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
924 " ColorMatrix image with color matrix:");
925 message=AcquireString("");
926 for (v=0; v < 6; v++)
927 {
928 *message='\0';
cristyb51dff52011-05-19 16:55:47 +0000929 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
cristye6365592010-04-02 17:31:23 +0000930 (void) ConcatenateString(&message,format);
931 for (u=0; u < 6; u++)
932 {
cristyb51dff52011-05-19 16:55:47 +0000933 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",
cristye6365592010-04-02 17:31:23 +0000934 ColorMatrix[v][u]);
935 (void) ConcatenateString(&message,format);
936 }
937 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
938 }
939 message=DestroyString(message);
940 }
941 /*
anthonyfd706f92012-01-19 04:22:02 +0000942 Apply the ColorMatrix to image.
cristye6365592010-04-02 17:31:23 +0000943 */
944 status=MagickTrue;
945 progress=0;
cristy46ff2672012-12-14 15:32:26 +0000946 image_view=AcquireVirtualCacheView(image,exception);
947 color_view=AcquireAuthenticCacheView(color_image,exception);
cristye6365592010-04-02 17:31:23 +0000948#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000949 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +0000950 magick_threads(image,color_image,image->rows,1)
cristye6365592010-04-02 17:31:23 +0000951#endif
cristybb503372010-05-27 20:51:26 +0000952 for (y=0; y < (ssize_t) image->rows; y++)
cristye6365592010-04-02 17:31:23 +0000953 {
cristyfcc25d92012-02-19 23:06:48 +0000954 PixelInfo
cristye6365592010-04-02 17:31:23 +0000955 pixel;
956
cristy4c08aed2011-07-01 19:47:50 +0000957 register const Quantum
cristye6365592010-04-02 17:31:23 +0000958 *restrict p;
959
cristy4c08aed2011-07-01 19:47:50 +0000960 register Quantum
961 *restrict q;
962
cristybb503372010-05-27 20:51:26 +0000963 register ssize_t
cristye6365592010-04-02 17:31:23 +0000964 x;
965
cristye6365592010-04-02 17:31:23 +0000966 if (status == MagickFalse)
967 continue;
968 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
969 q=GetCacheViewAuthenticPixels(color_view,0,y,color_image->columns,1,
970 exception);
cristy4c08aed2011-07-01 19:47:50 +0000971 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristye6365592010-04-02 17:31:23 +0000972 {
973 status=MagickFalse;
974 continue;
975 }
cristyfcc25d92012-02-19 23:06:48 +0000976 GetPixelInfo(image,&pixel);
cristybb503372010-05-27 20:51:26 +0000977 for (x=0; x < (ssize_t) image->columns; x++)
cristye6365592010-04-02 17:31:23 +0000978 {
cristybb503372010-05-27 20:51:26 +0000979 register ssize_t
cristye6365592010-04-02 17:31:23 +0000980 v;
981
cristybb503372010-05-27 20:51:26 +0000982 size_t
cristye6365592010-04-02 17:31:23 +0000983 height;
984
cristyfcc25d92012-02-19 23:06:48 +0000985 GetPixelInfoPixel(image,p,&pixel);
cristye6365592010-04-02 17:31:23 +0000986 height=color_matrix->height > 6 ? 6UL : color_matrix->height;
cristybb503372010-05-27 20:51:26 +0000987 for (v=0; v < (ssize_t) height; v++)
cristye6365592010-04-02 17:31:23 +0000988 {
cristya19f1d72012-08-07 18:24:38 +0000989 double
cristyfcc25d92012-02-19 23:06:48 +0000990 sum;
991
992 sum=ColorMatrix[v][0]*GetPixelRed(image,p)+ColorMatrix[v][1]*
cristy4c08aed2011-07-01 19:47:50 +0000993 GetPixelGreen(image,p)+ColorMatrix[v][2]*GetPixelBlue(image,p);
cristye6365592010-04-02 17:31:23 +0000994 if (image->colorspace == CMYKColorspace)
cristyfcc25d92012-02-19 23:06:48 +0000995 sum+=ColorMatrix[v][3]*GetPixelBlack(image,p);
cristy8a46d822012-08-28 23:32:39 +0000996 if (image->alpha_trait == BlendPixelTrait)
cristyfcc25d92012-02-19 23:06:48 +0000997 sum+=ColorMatrix[v][4]*GetPixelAlpha(image,p);
998 sum+=QuantumRange*ColorMatrix[v][5];
cristye6365592010-04-02 17:31:23 +0000999 switch (v)
1000 {
cristyfcc25d92012-02-19 23:06:48 +00001001 case 0: pixel.red=sum; break;
1002 case 1: pixel.green=sum; break;
1003 case 2: pixel.blue=sum; break;
1004 case 3: pixel.black=sum; break;
1005 case 4: pixel.alpha=sum; break;
1006 default: break;
cristye6365592010-04-02 17:31:23 +00001007 }
1008 }
cristyfcc25d92012-02-19 23:06:48 +00001009 SetPixelInfoPixel(color_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +00001010 p+=GetPixelChannels(image);
1011 q+=GetPixelChannels(color_image);
cristye6365592010-04-02 17:31:23 +00001012 }
1013 if (SyncCacheViewAuthenticPixels(color_view,exception) == MagickFalse)
1014 status=MagickFalse;
1015 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1016 {
1017 MagickBooleanType
1018 proceed;
1019
1020#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00001021 #pragma omp critical (MagickCore_ColorMatrixImage)
cristye6365592010-04-02 17:31:23 +00001022#endif
1023 proceed=SetImageProgress(image,ColorMatrixImageTag,progress++,
1024 image->rows);
1025 if (proceed == MagickFalse)
1026 status=MagickFalse;
1027 }
1028 }
1029 color_view=DestroyCacheView(color_view);
1030 image_view=DestroyCacheView(image_view);
1031 if (status == MagickFalse)
1032 color_image=DestroyImage(color_image);
1033 return(color_image);
1034}
1035
1036/*
1037%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1038% %
1039% %
1040% %
cristy3ed852e2009-09-05 21:47:34 +00001041+ D e s t r o y F x I n f o %
1042% %
1043% %
1044% %
1045%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1046%
1047% DestroyFxInfo() deallocates memory associated with an FxInfo structure.
1048%
1049% The format of the DestroyFxInfo method is:
1050%
1051% ImageInfo *DestroyFxInfo(ImageInfo *fx_info)
1052%
1053% A description of each parameter follows:
1054%
1055% o fx_info: the fx info.
1056%
1057*/
cristy7832dc22011-09-05 01:21:53 +00001058MagickPrivate FxInfo *DestroyFxInfo(FxInfo *fx_info)
cristy3ed852e2009-09-05 21:47:34 +00001059{
cristybb503372010-05-27 20:51:26 +00001060 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001061 i;
1062
1063 fx_info->exception=DestroyExceptionInfo(fx_info->exception);
1064 fx_info->expression=DestroyString(fx_info->expression);
1065 fx_info->symbols=DestroySplayTree(fx_info->symbols);
1066 fx_info->colors=DestroySplayTree(fx_info->colors);
cristy0ea377f2011-03-24 00:54:19 +00001067 for (i=(ssize_t) GetImageListLength(fx_info->images)-1; i >= 0; i--)
cristyd76c51e2011-03-26 00:21:26 +00001068 fx_info->view[i]=DestroyCacheView(fx_info->view[i]);
1069 fx_info->view=(CacheView **) RelinquishMagickMemory(fx_info->view);
cristy3ed852e2009-09-05 21:47:34 +00001070 fx_info->random_info=DestroyRandomInfo(fx_info->random_info);
1071 fx_info=(FxInfo *) RelinquishMagickMemory(fx_info);
1072 return(fx_info);
1073}
1074
1075/*
1076%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1077% %
1078% %
1079% %
cristy3ed852e2009-09-05 21:47:34 +00001080+ 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 %
1081% %
1082% %
1083% %
1084%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1085%
1086% FxEvaluateChannelExpression() evaluates an expression and returns the
1087% results.
1088%
1089% The format of the FxEvaluateExpression method is:
1090%
cristya19f1d72012-08-07 18:24:38 +00001091% double FxEvaluateChannelExpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00001092% const PixelChannel channel,const ssize_t x,const ssize_t y,
cristya19f1d72012-08-07 18:24:38 +00001093% double *alpha,Exceptioninfo *exception)
1094% double FxEvaluateExpression(FxInfo *fx_info,
1095% double *alpha,Exceptioninfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001096%
1097% A description of each parameter follows:
1098%
1099% o fx_info: the fx info.
1100%
1101% o channel: the channel.
1102%
1103% o x,y: the pixel position.
1104%
1105% o alpha: the result.
1106%
1107% o exception: return any errors or warnings in this structure.
1108%
1109*/
1110
cristy351842f2010-03-07 15:27:38 +00001111static inline double MagickMax(const double x,const double y)
1112{
1113 if (x > y)
1114 return(x);
1115 return(y);
1116}
1117
cristya19f1d72012-08-07 18:24:38 +00001118static double FxChannelStatistics(FxInfo *fx_info,Image *image,
cristy0568ffc2011-07-25 16:54:14 +00001119 PixelChannel channel,const char *symbol,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001120{
cristy5048d302012-08-07 01:05:16 +00001121 ChannelType
1122 channel_mask;
1123
cristy3ed852e2009-09-05 21:47:34 +00001124 char
1125 key[MaxTextExtent],
1126 statistic[MaxTextExtent];
1127
1128 const char
1129 *value;
1130
1131 register const char
1132 *p;
1133
cristy5048d302012-08-07 01:05:16 +00001134 channel_mask=UndefinedChannel;
cristy3ed852e2009-09-05 21:47:34 +00001135 for (p=symbol; (*p != '.') && (*p != '\0'); p++) ;
1136 if (*p == '.')
cristy3ed852e2009-09-05 21:47:34 +00001137 {
cristy5048d302012-08-07 01:05:16 +00001138 ssize_t
1139 option;
1140
1141 option=ParseCommandOption(MagickPixelChannelOptions,MagickTrue,p+1);
1142 if (option >= 0)
1143 {
1144 channel=(PixelChannel) option;
1145 channel_mask=(ChannelType) (channel_mask | (1 << channel));
cristycf1296e2012-08-26 23:40:49 +00001146 SetPixelChannelMask(image,channel_mask);
cristy5048d302012-08-07 01:05:16 +00001147 }
cristy3ed852e2009-09-05 21:47:34 +00001148 }
cristyb51dff52011-05-19 16:55:47 +00001149 (void) FormatLocaleString(key,MaxTextExtent,"%p.%.20g.%s",(void *) image,
cristye8c25f92010-06-03 00:53:06 +00001150 (double) channel,symbol);
cristy3ed852e2009-09-05 21:47:34 +00001151 value=(const char *) GetValueFromSplayTree(fx_info->symbols,key);
1152 if (value != (const char *) NULL)
cristy5048d302012-08-07 01:05:16 +00001153 {
1154 if (channel_mask != UndefinedChannel)
cristycf1296e2012-08-26 23:40:49 +00001155 SetPixelChannelMask(image,channel_mask);
cristy5048d302012-08-07 01:05:16 +00001156 return(QuantumScale*StringToDouble(value,(char **) NULL));
1157 }
cristy3ed852e2009-09-05 21:47:34 +00001158 (void) DeleteNodeFromSplayTree(fx_info->symbols,key);
1159 if (LocaleNCompare(symbol,"depth",5) == 0)
1160 {
cristybb503372010-05-27 20:51:26 +00001161 size_t
cristy3ed852e2009-09-05 21:47:34 +00001162 depth;
1163
cristyfefab1b2011-07-05 00:33:22 +00001164 depth=GetImageDepth(image,exception);
1165 (void) FormatLocaleString(statistic,MaxTextExtent,"%.20g",(double) depth);
cristy3ed852e2009-09-05 21:47:34 +00001166 }
1167 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1168 {
1169 double
1170 kurtosis,
1171 skewness;
1172
cristyd42d9952011-07-08 14:21:50 +00001173 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
cristyb51dff52011-05-19 16:55:47 +00001174 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",kurtosis);
cristy3ed852e2009-09-05 21:47:34 +00001175 }
1176 if (LocaleNCompare(symbol,"maxima",6) == 0)
1177 {
1178 double
1179 maxima,
1180 minima;
1181
cristyd42d9952011-07-08 14:21:50 +00001182 (void) GetImageRange(image,&minima,&maxima,exception);
cristyb51dff52011-05-19 16:55:47 +00001183 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",maxima);
cristy3ed852e2009-09-05 21:47:34 +00001184 }
1185 if (LocaleNCompare(symbol,"mean",4) == 0)
1186 {
1187 double
1188 mean,
1189 standard_deviation;
1190
cristyd42d9952011-07-08 14:21:50 +00001191 (void) GetImageMean(image,&mean,&standard_deviation,exception);
cristyb51dff52011-05-19 16:55:47 +00001192 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",mean);
cristy3ed852e2009-09-05 21:47:34 +00001193 }
1194 if (LocaleNCompare(symbol,"minima",6) == 0)
1195 {
1196 double
1197 maxima,
1198 minima;
1199
cristyd42d9952011-07-08 14:21:50 +00001200 (void) GetImageRange(image,&minima,&maxima,exception);
cristyb51dff52011-05-19 16:55:47 +00001201 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",minima);
cristy3ed852e2009-09-05 21:47:34 +00001202 }
1203 if (LocaleNCompare(symbol,"skewness",8) == 0)
1204 {
1205 double
1206 kurtosis,
1207 skewness;
1208
cristyd42d9952011-07-08 14:21:50 +00001209 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
cristyb51dff52011-05-19 16:55:47 +00001210 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",skewness);
cristy3ed852e2009-09-05 21:47:34 +00001211 }
1212 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1213 {
1214 double
1215 mean,
1216 standard_deviation;
1217
cristyd42d9952011-07-08 14:21:50 +00001218 (void) GetImageMean(image,&mean,&standard_deviation,exception);
cristyb51dff52011-05-19 16:55:47 +00001219 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",
cristy3ed852e2009-09-05 21:47:34 +00001220 standard_deviation);
1221 }
cristy5048d302012-08-07 01:05:16 +00001222 if (channel_mask != UndefinedChannel)
cristycf1296e2012-08-26 23:40:49 +00001223 SetPixelChannelMask(image,channel_mask);
cristy3ed852e2009-09-05 21:47:34 +00001224 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(key),
1225 ConstantString(statistic));
cristydbdd0e32011-11-04 23:29:40 +00001226 return(QuantumScale*StringToDouble(statistic,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001227}
1228
cristya19f1d72012-08-07 18:24:38 +00001229static double
cristy0568ffc2011-07-25 16:54:14 +00001230 FxEvaluateSubexpression(FxInfo *,const PixelChannel,const ssize_t,
cristya19f1d72012-08-07 18:24:38 +00001231 const ssize_t,const char *,double *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +00001232
cristyb0aad4c2011-11-02 19:30:35 +00001233static MagickOffsetType FxGCD(MagickOffsetType alpha,MagickOffsetType beta)
1234{
1235 if (beta != 0)
1236 return(FxGCD(beta,alpha % beta));
1237 return(alpha);
1238}
1239
cristy3ed852e2009-09-05 21:47:34 +00001240static inline const char *FxSubexpression(const char *expression,
1241 ExceptionInfo *exception)
1242{
1243 const char
1244 *subexpression;
1245
cristybb503372010-05-27 20:51:26 +00001246 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001247 level;
1248
1249 level=0;
1250 subexpression=expression;
1251 while ((*subexpression != '\0') &&
1252 ((level != 1) || (strchr(")",(int) *subexpression) == (char *) NULL)))
1253 {
1254 if (strchr("(",(int) *subexpression) != (char *) NULL)
1255 level++;
1256 else
1257 if (strchr(")",(int) *subexpression) != (char *) NULL)
1258 level--;
1259 subexpression++;
1260 }
1261 if (*subexpression == '\0')
1262 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
cristyefe601c2013-01-05 17:51:12 +00001263 "UnbalancedParenthesis","`%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00001264 return(subexpression);
1265}
1266
cristya19f1d72012-08-07 18:24:38 +00001267static double FxGetSymbol(FxInfo *fx_info,const PixelChannel channel,
cristye85007d2010-06-06 22:51:36 +00001268 const ssize_t x,const ssize_t y,const char *expression,
1269 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001270{
1271 char
1272 *q,
1273 subexpression[MaxTextExtent],
1274 symbol[MaxTextExtent];
1275
1276 const char
1277 *p,
1278 *value;
1279
1280 Image
1281 *image;
1282
cristy4c08aed2011-07-01 19:47:50 +00001283 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001284 pixel;
1285
cristya19f1d72012-08-07 18:24:38 +00001286 double
cristy3ed852e2009-09-05 21:47:34 +00001287 alpha,
1288 beta;
1289
1290 PointInfo
1291 point;
1292
cristybb503372010-05-27 20:51:26 +00001293 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001294 i;
1295
1296 size_t
cristy1707c6c2012-01-18 23:30:54 +00001297 length,
cristy3ed852e2009-09-05 21:47:34 +00001298 level;
1299
1300 p=expression;
1301 i=GetImageIndexInList(fx_info->images);
1302 level=0;
1303 point.x=(double) x;
1304 point.y=(double) y;
cristy02e64f82013-11-14 16:20:04 +00001305 if (isalpha((int) ((unsigned char) *(p+1))) == 0)
cristy3ed852e2009-09-05 21:47:34 +00001306 {
1307 if (strchr("suv",(int) *p) != (char *) NULL)
1308 {
1309 switch (*p)
1310 {
1311 case 's':
1312 default:
1313 {
1314 i=GetImageIndexInList(fx_info->images);
1315 break;
1316 }
1317 case 'u': i=0; break;
1318 case 'v': i=1; break;
1319 }
1320 p++;
1321 if (*p == '[')
1322 {
1323 level++;
1324 q=subexpression;
1325 for (p++; *p != '\0'; )
1326 {
1327 if (*p == '[')
1328 level++;
1329 else
1330 if (*p == ']')
1331 {
1332 level--;
1333 if (level == 0)
1334 break;
1335 }
1336 *q++=(*p++);
1337 }
1338 *q='\0';
1339 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1340 &beta,exception);
cristybb503372010-05-27 20:51:26 +00001341 i=(ssize_t) (alpha+0.5);
cristy3ed852e2009-09-05 21:47:34 +00001342 p++;
1343 }
1344 if (*p == '.')
1345 p++;
1346 }
cristy02e64f82013-11-14 16:20:04 +00001347 if ((*p == 'p') && (isalpha((int) ((unsigned char) *(p+1))) == 0))
cristy3ed852e2009-09-05 21:47:34 +00001348 {
1349 p++;
1350 if (*p == '{')
1351 {
1352 level++;
1353 q=subexpression;
1354 for (p++; *p != '\0'; )
1355 {
1356 if (*p == '{')
1357 level++;
1358 else
1359 if (*p == '}')
1360 {
1361 level--;
1362 if (level == 0)
1363 break;
1364 }
1365 *q++=(*p++);
1366 }
1367 *q='\0';
1368 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1369 &beta,exception);
1370 point.x=alpha;
1371 point.y=beta;
1372 p++;
1373 }
1374 else
1375 if (*p == '[')
1376 {
1377 level++;
1378 q=subexpression;
1379 for (p++; *p != '\0'; )
1380 {
1381 if (*p == '[')
1382 level++;
1383 else
1384 if (*p == ']')
1385 {
1386 level--;
1387 if (level == 0)
1388 break;
1389 }
1390 *q++=(*p++);
1391 }
1392 *q='\0';
1393 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1394 &beta,exception);
1395 point.x+=alpha;
1396 point.y+=beta;
1397 p++;
1398 }
1399 if (*p == '.')
1400 p++;
1401 }
1402 }
1403 length=GetImageListLength(fx_info->images);
1404 while (i < 0)
cristybb503372010-05-27 20:51:26 +00001405 i+=(ssize_t) length;
cristy58a749e2014-05-25 17:36:53 +00001406 if (length != 0)
1407 i%=length;
cristy3ed852e2009-09-05 21:47:34 +00001408 image=GetImageFromList(fx_info->images,i);
1409 if (image == (Image *) NULL)
1410 {
1411 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
cristyefe601c2013-01-05 17:51:12 +00001412 "NoSuchImage","`%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00001413 return(0.0);
1414 }
cristy4c08aed2011-07-01 19:47:50 +00001415 GetPixelInfo(image,&pixel);
1416 (void) InterpolatePixelInfo(image,fx_info->view[i],image->interpolate,
cristy4f820712011-04-01 12:35:43 +00001417 point.x,point.y,&pixel,exception);
cristy1707c6c2012-01-18 23:30:54 +00001418 if ((strlen(p) > 2) && (LocaleCompare(p,"intensity") != 0) &&
cristyba213402013-06-28 14:05:12 +00001419 (LocaleCompare(p,"luma") != 0) && (LocaleCompare(p,"luminance") != 0) &&
1420 (LocaleCompare(p,"hue") != 0) && (LocaleCompare(p,"saturation") != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00001421 (LocaleCompare(p,"lightness") != 0))
1422 {
1423 char
1424 name[MaxTextExtent];
1425
1426 (void) CopyMagickString(name,p,MaxTextExtent);
1427 for (q=name+(strlen(name)-1); q > name; q--)
1428 {
1429 if (*q == ')')
1430 break;
1431 if (*q == '.')
1432 {
1433 *q='\0';
1434 break;
1435 }
1436 }
1437 if ((strlen(name) > 2) &&
1438 (GetValueFromSplayTree(fx_info->symbols,name) == (const char *) NULL))
1439 {
cristy4c08aed2011-07-01 19:47:50 +00001440 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001441 *color;
1442
cristy4c08aed2011-07-01 19:47:50 +00001443 color=(PixelInfo *) GetValueFromSplayTree(fx_info->colors,name);
1444 if (color != (PixelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001445 {
1446 pixel=(*color);
1447 p+=strlen(name);
1448 }
1449 else
cristy1707c6c2012-01-18 23:30:54 +00001450 {
1451 MagickBooleanType
1452 status;
1453
1454 status=QueryColorCompliance(name,AllCompliance,&pixel,
1455 fx_info->exception);
1456 if (status != MagickFalse)
1457 {
1458 (void) AddValueToSplayTree(fx_info->colors,ConstantString(
1459 name),ClonePixelInfo(&pixel));
1460 p+=strlen(name);
1461 }
1462 }
cristy3ed852e2009-09-05 21:47:34 +00001463 }
1464 }
1465 (void) CopyMagickString(symbol,p,MaxTextExtent);
1466 StripString(symbol);
1467 if (*symbol == '\0')
1468 {
1469 switch (channel)
1470 {
cristy0568ffc2011-07-25 16:54:14 +00001471 case RedPixelChannel: return(QuantumScale*pixel.red);
1472 case GreenPixelChannel: return(QuantumScale*pixel.green);
1473 case BluePixelChannel: return(QuantumScale*pixel.blue);
1474 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001475 {
1476 if (image->colorspace != CMYKColorspace)
1477 {
1478 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00001479 ImageError,"ColorSeparatedImageRequired","`%s'",
cristy3ed852e2009-09-05 21:47:34 +00001480 image->filename);
1481 return(0.0);
1482 }
cristy4c08aed2011-07-01 19:47:50 +00001483 return(QuantumScale*pixel.black);
1484 }
cristy0568ffc2011-07-25 16:54:14 +00001485 case AlphaPixelChannel:
cristy4c08aed2011-07-01 19:47:50 +00001486 {
cristya19f1d72012-08-07 18:24:38 +00001487 double
cristy4c08aed2011-07-01 19:47:50 +00001488 alpha;
1489
cristy8a46d822012-08-28 23:32:39 +00001490 if (pixel.alpha_trait != BlendPixelTrait)
cristy4c08aed2011-07-01 19:47:50 +00001491 return(1.0);
cristya19f1d72012-08-07 18:24:38 +00001492 alpha=(double) (QuantumScale*pixel.alpha);
cristy4c08aed2011-07-01 19:47:50 +00001493 return(alpha);
cristy3ed852e2009-09-05 21:47:34 +00001494 }
cristya382aca2011-12-06 18:22:48 +00001495 case IndexPixelChannel:
1496 return(0.0);
cristyb3a73b52011-07-26 01:34:43 +00001497 case IntensityPixelChannel:
cristyf364ed42010-12-15 01:54:43 +00001498 {
cristy4c08aed2011-07-01 19:47:50 +00001499 return(QuantumScale*GetPixelInfoIntensity(&pixel));
cristyf364ed42010-12-15 01:54:43 +00001500 }
cristy3ed852e2009-09-05 21:47:34 +00001501 default:
1502 break;
1503 }
1504 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
cristyefe601c2013-01-05 17:51:12 +00001505 "UnableToParseExpression","`%s'",p);
cristy3ed852e2009-09-05 21:47:34 +00001506 return(0.0);
1507 }
1508 switch (*symbol)
1509 {
1510 case 'A':
1511 case 'a':
1512 {
1513 if (LocaleCompare(symbol,"a") == 0)
cristya19f1d72012-08-07 18:24:38 +00001514 return((double) (QuantumScale*pixel.alpha));
cristy3ed852e2009-09-05 21:47:34 +00001515 break;
1516 }
1517 case 'B':
1518 case 'b':
1519 {
1520 if (LocaleCompare(symbol,"b") == 0)
1521 return(QuantumScale*pixel.blue);
1522 break;
1523 }
1524 case 'C':
1525 case 'c':
1526 {
1527 if (LocaleNCompare(symbol,"channel",7) == 0)
1528 {
1529 GeometryInfo
1530 channel_info;
1531
1532 MagickStatusType
1533 flags;
1534
1535 flags=ParseGeometry(symbol+7,&channel_info);
1536 if (image->colorspace == CMYKColorspace)
1537 switch (channel)
1538 {
cristy0568ffc2011-07-25 16:54:14 +00001539 case CyanPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001540 {
1541 if ((flags & RhoValue) == 0)
1542 return(0.0);
1543 return(channel_info.rho);
1544 }
cristy0568ffc2011-07-25 16:54:14 +00001545 case MagentaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001546 {
1547 if ((flags & SigmaValue) == 0)
1548 return(0.0);
1549 return(channel_info.sigma);
1550 }
cristy0568ffc2011-07-25 16:54:14 +00001551 case YellowPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001552 {
1553 if ((flags & XiValue) == 0)
1554 return(0.0);
1555 return(channel_info.xi);
1556 }
cristy0568ffc2011-07-25 16:54:14 +00001557 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001558 {
1559 if ((flags & PsiValue) == 0)
1560 return(0.0);
1561 return(channel_info.psi);
1562 }
cristy0568ffc2011-07-25 16:54:14 +00001563 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001564 {
1565 if ((flags & ChiValue) == 0)
1566 return(0.0);
1567 return(channel_info.chi);
1568 }
1569 default:
1570 return(0.0);
1571 }
1572 switch (channel)
1573 {
cristy0568ffc2011-07-25 16:54:14 +00001574 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001575 {
1576 if ((flags & RhoValue) == 0)
1577 return(0.0);
1578 return(channel_info.rho);
1579 }
cristy0568ffc2011-07-25 16:54:14 +00001580 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001581 {
1582 if ((flags & SigmaValue) == 0)
1583 return(0.0);
1584 return(channel_info.sigma);
1585 }
cristy0568ffc2011-07-25 16:54:14 +00001586 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001587 {
1588 if ((flags & XiValue) == 0)
1589 return(0.0);
1590 return(channel_info.xi);
1591 }
cristy0568ffc2011-07-25 16:54:14 +00001592 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001593 {
1594 if ((flags & ChiValue) == 0)
1595 return(0.0);
1596 return(channel_info.chi);
1597 }
cristy0568ffc2011-07-25 16:54:14 +00001598 case AlphaPixelChannel:
cristy4c08aed2011-07-01 19:47:50 +00001599 {
1600 if ((flags & PsiValue) == 0)
1601 return(0.0);
1602 return(channel_info.psi);
1603 }
cristy3ed852e2009-09-05 21:47:34 +00001604 default:
1605 return(0.0);
1606 }
cristy3ed852e2009-09-05 21:47:34 +00001607 }
1608 if (LocaleCompare(symbol,"c") == 0)
1609 return(QuantumScale*pixel.red);
1610 break;
1611 }
1612 case 'D':
1613 case 'd':
1614 {
1615 if (LocaleNCompare(symbol,"depth",5) == 0)
1616 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1617 break;
1618 }
1619 case 'G':
1620 case 'g':
1621 {
1622 if (LocaleCompare(symbol,"g") == 0)
1623 return(QuantumScale*pixel.green);
1624 break;
1625 }
1626 case 'K':
1627 case 'k':
1628 {
1629 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1630 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1631 if (LocaleCompare(symbol,"k") == 0)
1632 {
1633 if (image->colorspace != CMYKColorspace)
1634 {
1635 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00001636 OptionError,"ColorSeparatedImageRequired","`%s'",
cristy3ed852e2009-09-05 21:47:34 +00001637 image->filename);
1638 return(0.0);
1639 }
cristy4c08aed2011-07-01 19:47:50 +00001640 return(QuantumScale*pixel.black);
cristy3ed852e2009-09-05 21:47:34 +00001641 }
1642 break;
1643 }
1644 case 'H':
1645 case 'h':
1646 {
1647 if (LocaleCompare(symbol,"h") == 0)
cristya19f1d72012-08-07 18:24:38 +00001648 return((double) image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001649 if (LocaleCompare(symbol,"hue") == 0)
1650 {
1651 double
1652 hue,
1653 lightness,
1654 saturation;
1655
cristy0a39a5c2012-06-27 12:51:45 +00001656 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
cristyda1f9c12011-10-02 21:39:49 +00001657 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001658 return(hue);
1659 }
1660 break;
1661 }
1662 case 'I':
1663 case 'i':
1664 {
1665 if ((LocaleCompare(symbol,"image.depth") == 0) ||
1666 (LocaleCompare(symbol,"image.minima") == 0) ||
1667 (LocaleCompare(symbol,"image.maxima") == 0) ||
1668 (LocaleCompare(symbol,"image.mean") == 0) ||
1669 (LocaleCompare(symbol,"image.kurtosis") == 0) ||
1670 (LocaleCompare(symbol,"image.skewness") == 0) ||
1671 (LocaleCompare(symbol,"image.standard_deviation") == 0))
1672 return(FxChannelStatistics(fx_info,image,channel,symbol+6,exception));
1673 if (LocaleCompare(symbol,"image.resolution.x") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001674 return(image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00001675 if (LocaleCompare(symbol,"image.resolution.y") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001676 return(image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001677 if (LocaleCompare(symbol,"intensity") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001678 return(QuantumScale*GetPixelInfoIntensity(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001679 if (LocaleCompare(symbol,"i") == 0)
cristya19f1d72012-08-07 18:24:38 +00001680 return((double) x);
cristy3ed852e2009-09-05 21:47:34 +00001681 break;
1682 }
1683 case 'J':
1684 case 'j':
1685 {
1686 if (LocaleCompare(symbol,"j") == 0)
cristya19f1d72012-08-07 18:24:38 +00001687 return((double) y);
cristy3ed852e2009-09-05 21:47:34 +00001688 break;
1689 }
1690 case 'L':
1691 case 'l':
1692 {
1693 if (LocaleCompare(symbol,"lightness") == 0)
1694 {
1695 double
1696 hue,
1697 lightness,
1698 saturation;
1699
cristy0a39a5c2012-06-27 12:51:45 +00001700 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
cristyda1f9c12011-10-02 21:39:49 +00001701 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001702 return(lightness);
1703 }
cristyd3d2a272013-06-24 16:09:41 +00001704 if (LocaleCompare(symbol,"luma") == 0)
1705 {
1706 double
1707 luma;
1708
1709 luma=0.212656*pixel.red+0.715158*pixel.green+0.072186*pixel.blue;
1710 return(QuantumScale*luma);
1711 }
cristy3ed852e2009-09-05 21:47:34 +00001712 if (LocaleCompare(symbol,"luminance") == 0)
1713 {
1714 double
1715 luminence;
1716
cristyd3d2a272013-06-24 16:09:41 +00001717 luminence=0.212656*pixel.red+0.715158*pixel.green+0.072186*pixel.blue;
cristy3ed852e2009-09-05 21:47:34 +00001718 return(QuantumScale*luminence);
1719 }
1720 break;
1721 }
1722 case 'M':
1723 case 'm':
1724 {
1725 if (LocaleNCompare(symbol,"maxima",6) == 0)
1726 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1727 if (LocaleNCompare(symbol,"mean",4) == 0)
1728 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1729 if (LocaleNCompare(symbol,"minima",6) == 0)
1730 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1731 if (LocaleCompare(symbol,"m") == 0)
cristy1c8e4f62014-05-26 16:17:53 +00001732 return(QuantumScale*pixel.green);
cristy3ed852e2009-09-05 21:47:34 +00001733 break;
1734 }
1735 case 'N':
1736 case 'n':
1737 {
1738 if (LocaleCompare(symbol,"n") == 0)
cristya19f1d72012-08-07 18:24:38 +00001739 return((double) GetImageListLength(fx_info->images));
cristy3ed852e2009-09-05 21:47:34 +00001740 break;
1741 }
1742 case 'O':
1743 case 'o':
1744 {
1745 if (LocaleCompare(symbol,"o") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001746 return(QuantumScale*pixel.alpha);
cristy3ed852e2009-09-05 21:47:34 +00001747 break;
1748 }
1749 case 'P':
1750 case 'p':
1751 {
1752 if (LocaleCompare(symbol,"page.height") == 0)
cristya19f1d72012-08-07 18:24:38 +00001753 return((double) image->page.height);
cristy3ed852e2009-09-05 21:47:34 +00001754 if (LocaleCompare(symbol,"page.width") == 0)
cristya19f1d72012-08-07 18:24:38 +00001755 return((double) image->page.width);
cristy3ed852e2009-09-05 21:47:34 +00001756 if (LocaleCompare(symbol,"page.x") == 0)
cristya19f1d72012-08-07 18:24:38 +00001757 return((double) image->page.x);
cristy3ed852e2009-09-05 21:47:34 +00001758 if (LocaleCompare(symbol,"page.y") == 0)
cristya19f1d72012-08-07 18:24:38 +00001759 return((double) image->page.y);
cristy3ed852e2009-09-05 21:47:34 +00001760 break;
1761 }
1762 case 'R':
1763 case 'r':
1764 {
1765 if (LocaleCompare(symbol,"resolution.x") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001766 return(image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00001767 if (LocaleCompare(symbol,"resolution.y") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001768 return(image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001769 if (LocaleCompare(symbol,"r") == 0)
1770 return(QuantumScale*pixel.red);
1771 break;
1772 }
1773 case 'S':
1774 case 's':
1775 {
1776 if (LocaleCompare(symbol,"saturation") == 0)
1777 {
1778 double
1779 hue,
1780 lightness,
1781 saturation;
1782
cristy0a39a5c2012-06-27 12:51:45 +00001783 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
cristyda1f9c12011-10-02 21:39:49 +00001784 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001785 return(saturation);
1786 }
1787 if (LocaleNCompare(symbol,"skewness",8) == 0)
1788 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1789 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1790 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1791 break;
1792 }
1793 case 'T':
1794 case 't':
1795 {
1796 if (LocaleCompare(symbol,"t") == 0)
cristya19f1d72012-08-07 18:24:38 +00001797 return((double) GetImageIndexInList(fx_info->images));
cristy3ed852e2009-09-05 21:47:34 +00001798 break;
1799 }
1800 case 'W':
1801 case 'w':
1802 {
1803 if (LocaleCompare(symbol,"w") == 0)
cristya19f1d72012-08-07 18:24:38 +00001804 return((double) image->columns);
cristy3ed852e2009-09-05 21:47:34 +00001805 break;
1806 }
1807 case 'Y':
1808 case 'y':
1809 {
1810 if (LocaleCompare(symbol,"y") == 0)
cristy1c8e4f62014-05-26 16:17:53 +00001811 return(QuantumScale*pixel.blue);
cristy3ed852e2009-09-05 21:47:34 +00001812 break;
1813 }
1814 case 'Z':
1815 case 'z':
1816 {
1817 if (LocaleCompare(symbol,"z") == 0)
1818 {
cristya19f1d72012-08-07 18:24:38 +00001819 double
cristy3ed852e2009-09-05 21:47:34 +00001820 depth;
1821
cristya19f1d72012-08-07 18:24:38 +00001822 depth=(double) GetImageDepth(image,fx_info->exception);
cristy3ed852e2009-09-05 21:47:34 +00001823 return(depth);
1824 }
1825 break;
1826 }
1827 default:
1828 break;
1829 }
1830 value=(const char *) GetValueFromSplayTree(fx_info->symbols,symbol);
1831 if (value != (const char *) NULL)
cristya19f1d72012-08-07 18:24:38 +00001832 return((double) StringToDouble(value,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001833 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
cristyefe601c2013-01-05 17:51:12 +00001834 "UnableToParseExpression","`%s'",symbol);
cristy3ed852e2009-09-05 21:47:34 +00001835 return(0.0);
1836}
1837
1838static const char *FxOperatorPrecedence(const char *expression,
1839 ExceptionInfo *exception)
1840{
1841 typedef enum
1842 {
1843 UndefinedPrecedence,
1844 NullPrecedence,
1845 BitwiseComplementPrecedence,
1846 ExponentPrecedence,
cristy116af162010-08-13 01:25:47 +00001847 ExponentialNotationPrecedence,
cristy3ed852e2009-09-05 21:47:34 +00001848 MultiplyPrecedence,
1849 AdditionPrecedence,
1850 ShiftPrecedence,
1851 RelationalPrecedence,
1852 EquivalencyPrecedence,
1853 BitwiseAndPrecedence,
1854 BitwiseOrPrecedence,
1855 LogicalAndPrecedence,
1856 LogicalOrPrecedence,
1857 TernaryPrecedence,
1858 AssignmentPrecedence,
1859 CommaPrecedence,
1860 SeparatorPrecedence
1861 } FxPrecedence;
1862
1863 FxPrecedence
1864 precedence,
1865 target;
1866
1867 register const char
1868 *subexpression;
1869
1870 register int
1871 c;
1872
cristybb503372010-05-27 20:51:26 +00001873 size_t
cristy3ed852e2009-09-05 21:47:34 +00001874 level;
1875
1876 c=0;
1877 level=0;
1878 subexpression=(const char *) NULL;
1879 target=NullPrecedence;
1880 while (*expression != '\0')
1881 {
1882 precedence=UndefinedPrecedence;
cristy248a3ec2013-08-17 16:25:26 +00001883 if ((isspace((int) ((unsigned char) *expression)) != 0) || (c == (int) '@'))
cristy3ed852e2009-09-05 21:47:34 +00001884 {
1885 expression++;
1886 continue;
1887 }
cristy488fa882010-03-01 22:34:24 +00001888 switch (*expression)
1889 {
1890 case 'A':
1891 case 'a':
cristy3ed852e2009-09-05 21:47:34 +00001892 {
cristyb33454f2011-08-03 02:10:45 +00001893#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00001894 if (LocaleNCompare(expression,"acosh",5) == 0)
1895 {
1896 expression+=5;
1897 break;
1898 }
cristyb33454f2011-08-03 02:10:45 +00001899#endif
1900#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00001901 if (LocaleNCompare(expression,"asinh",5) == 0)
1902 {
1903 expression+=5;
1904 break;
1905 }
cristyb33454f2011-08-03 02:10:45 +00001906#endif
1907#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00001908 if (LocaleNCompare(expression,"atanh",5) == 0)
cristy488fa882010-03-01 22:34:24 +00001909 {
1910 expression+=5;
1911 break;
1912 }
cristyb33454f2011-08-03 02:10:45 +00001913#endif
anthony62838e52012-05-24 12:41:54 +00001914 if (LocaleNCompare(expression,"atan2",5) == 0)
1915 {
1916 expression+=5;
1917 break;
1918 }
cristy488fa882010-03-01 22:34:24 +00001919 break;
cristy3ed852e2009-09-05 21:47:34 +00001920 }
cristyf06a5612014-05-27 22:00:22 +00001921 case 'E':
1922 case 'e':
1923 {
1924 if ((isdigit((int) ((unsigned char) c)) != 0) &&
1925 ((LocaleNCompare(expression,"E+",2) == 0) ||
1926 (LocaleNCompare(expression,"E-",2) == 0)))
1927 {
1928 expression+=2; /* scientific notation */
1929 break;
1930 }
1931 }
cristy488fa882010-03-01 22:34:24 +00001932 case 'J':
1933 case 'j':
1934 {
1935 if ((LocaleNCompare(expression,"j0",2) == 0) ||
1936 (LocaleNCompare(expression,"j1",2) == 0))
1937 {
1938 expression+=2;
1939 break;
1940 }
1941 break;
1942 }
cristy2def9322010-06-18 23:59:37 +00001943 case '#':
1944 {
1945 while (isxdigit((int) ((unsigned char) *(expression+1))) != 0)
1946 expression++;
1947 break;
1948 }
cristy488fa882010-03-01 22:34:24 +00001949 default:
1950 break;
1951 }
cristy3ed852e2009-09-05 21:47:34 +00001952 if ((c == (int) '{') || (c == (int) '['))
1953 level++;
1954 else
1955 if ((c == (int) '}') || (c == (int) ']'))
1956 level--;
1957 if (level == 0)
1958 switch ((unsigned char) *expression)
1959 {
1960 case '~':
1961 case '!':
1962 {
1963 precedence=BitwiseComplementPrecedence;
1964 break;
1965 }
1966 case '^':
cristy6621e252010-08-13 00:42:57 +00001967 case '@':
cristy3ed852e2009-09-05 21:47:34 +00001968 {
1969 precedence=ExponentPrecedence;
1970 break;
1971 }
1972 default:
1973 {
cristy37466eb2013-08-17 17:04:59 +00001974 if (((c != 0) && ((isdigit((int) ((unsigned char) c)) != 0) ||
1975 (strchr(")",(int) ((unsigned char) c)) != (char *) NULL))) &&
1976 (((islower((int) ((unsigned char) *expression)) != 0) ||
1977 (strchr("(",(int) ((unsigned char) *expression)) != (char *) NULL)) ||
1978 ((isdigit((int) ((unsigned char) c)) == 0) &&
1979 (isdigit((int) ((unsigned char) *expression)) != 0))) &&
1980 (strchr("xy",(int) ((unsigned char) *expression)) == (char *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00001981 precedence=MultiplyPrecedence;
1982 break;
1983 }
1984 case '*':
1985 case '/':
1986 case '%':
1987 {
1988 precedence=MultiplyPrecedence;
1989 break;
1990 }
1991 case '+':
1992 case '-':
1993 {
1994 if ((strchr("(+-/*%:&^|<>~,",c) == (char *) NULL) ||
1995 (isalpha(c) != 0))
1996 precedence=AdditionPrecedence;
1997 break;
1998 }
1999 case LeftShiftOperator:
2000 case RightShiftOperator:
2001 {
2002 precedence=ShiftPrecedence;
2003 break;
2004 }
2005 case '<':
2006 case LessThanEqualOperator:
2007 case GreaterThanEqualOperator:
2008 case '>':
2009 {
2010 precedence=RelationalPrecedence;
2011 break;
2012 }
2013 case EqualOperator:
2014 case NotEqualOperator:
2015 {
2016 precedence=EquivalencyPrecedence;
2017 break;
2018 }
2019 case '&':
2020 {
2021 precedence=BitwiseAndPrecedence;
2022 break;
2023 }
2024 case '|':
2025 {
2026 precedence=BitwiseOrPrecedence;
2027 break;
2028 }
2029 case LogicalAndOperator:
2030 {
2031 precedence=LogicalAndPrecedence;
2032 break;
2033 }
2034 case LogicalOrOperator:
2035 {
2036 precedence=LogicalOrPrecedence;
2037 break;
2038 }
cristy116af162010-08-13 01:25:47 +00002039 case ExponentialNotation:
2040 {
2041 precedence=ExponentialNotationPrecedence;
2042 break;
2043 }
cristy3ed852e2009-09-05 21:47:34 +00002044 case ':':
2045 case '?':
2046 {
2047 precedence=TernaryPrecedence;
2048 break;
2049 }
2050 case '=':
2051 {
2052 precedence=AssignmentPrecedence;
2053 break;
2054 }
2055 case ',':
2056 {
2057 precedence=CommaPrecedence;
2058 break;
2059 }
2060 case ';':
2061 {
2062 precedence=SeparatorPrecedence;
2063 break;
2064 }
2065 }
2066 if ((precedence == BitwiseComplementPrecedence) ||
2067 (precedence == TernaryPrecedence) ||
2068 (precedence == AssignmentPrecedence))
2069 {
2070 if (precedence > target)
2071 {
2072 /*
2073 Right-to-left associativity.
2074 */
2075 target=precedence;
2076 subexpression=expression;
2077 }
2078 }
2079 else
2080 if (precedence >= target)
2081 {
2082 /*
2083 Left-to-right associativity.
2084 */
2085 target=precedence;
2086 subexpression=expression;
2087 }
2088 if (strchr("(",(int) *expression) != (char *) NULL)
2089 expression=FxSubexpression(expression,exception);
2090 c=(int) (*expression++);
2091 }
2092 return(subexpression);
2093}
2094
cristya19f1d72012-08-07 18:24:38 +00002095static double FxEvaluateSubexpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002096 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristya19f1d72012-08-07 18:24:38 +00002097 const char *expression,double *beta,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002098{
2099 char
2100 *q,
2101 subexpression[MaxTextExtent];
2102
cristya19f1d72012-08-07 18:24:38 +00002103 double
cristy3ed852e2009-09-05 21:47:34 +00002104 alpha,
2105 gamma;
2106
2107 register const char
2108 *p;
2109
2110 *beta=0.0;
2111 if (exception->severity != UndefinedException)
2112 return(0.0);
cristy248a3ec2013-08-17 16:25:26 +00002113 while (isspace((int) ((unsigned char) *expression)) != 0)
cristy3ed852e2009-09-05 21:47:34 +00002114 expression++;
2115 if (*expression == '\0')
cristydc569d82014-04-20 15:54:56 +00002116 return(0.0);
cristy66322f02010-05-17 11:40:48 +00002117 *subexpression='\0';
cristy3ed852e2009-09-05 21:47:34 +00002118 p=FxOperatorPrecedence(expression,exception);
2119 if (p != (const char *) NULL)
2120 {
2121 (void) CopyMagickString(subexpression,expression,(size_t)
2122 (p-expression+1));
2123 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2124 exception);
2125 switch ((unsigned char) *p)
2126 {
2127 case '~':
2128 {
2129 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristya19f1d72012-08-07 18:24:38 +00002130 *beta=(double) (~(size_t) *beta);
cristy3ed852e2009-09-05 21:47:34 +00002131 return(*beta);
2132 }
2133 case '!':
2134 {
2135 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2136 return(*beta == 0.0 ? 1.0 : 0.0);
2137 }
2138 case '^':
2139 {
2140 *beta=pow((double) alpha,(double) FxEvaluateSubexpression(fx_info,
2141 channel,x,y,++p,beta,exception));
2142 return(*beta);
2143 }
2144 case '*':
cristy116af162010-08-13 01:25:47 +00002145 case ExponentialNotation:
cristy3ed852e2009-09-05 21:47:34 +00002146 {
2147 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2148 return(alpha*(*beta));
2149 }
2150 case '/':
2151 {
2152 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2153 if (*beta == 0.0)
2154 {
2155 if (exception->severity == UndefinedException)
2156 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00002157 OptionError,"DivideByZero","`%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00002158 return(0.0);
2159 }
2160 return(alpha/(*beta));
2161 }
2162 case '%':
2163 {
2164 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2165 *beta=fabs(floor(((double) *beta)+0.5));
2166 if (*beta == 0.0)
2167 {
2168 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00002169 OptionError,"DivideByZero","`%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00002170 return(0.0);
2171 }
2172 return(fmod((double) alpha,(double) *beta));
2173 }
2174 case '+':
2175 {
2176 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2177 return(alpha+(*beta));
2178 }
2179 case '-':
2180 {
2181 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2182 return(alpha-(*beta));
2183 }
2184 case LeftShiftOperator:
2185 {
2186 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristya19f1d72012-08-07 18:24:38 +00002187 *beta=(double) ((size_t) (alpha+0.5) << (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002188 return(*beta);
2189 }
2190 case RightShiftOperator:
2191 {
2192 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristya19f1d72012-08-07 18:24:38 +00002193 *beta=(double) ((size_t) (alpha+0.5) >> (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002194 return(*beta);
2195 }
2196 case '<':
2197 {
2198 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2199 return(alpha < *beta ? 1.0 : 0.0);
2200 }
2201 case LessThanEqualOperator:
2202 {
2203 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2204 return(alpha <= *beta ? 1.0 : 0.0);
2205 }
2206 case '>':
2207 {
2208 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2209 return(alpha > *beta ? 1.0 : 0.0);
2210 }
2211 case GreaterThanEqualOperator:
2212 {
2213 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2214 return(alpha >= *beta ? 1.0 : 0.0);
2215 }
2216 case EqualOperator:
2217 {
2218 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristyc06e20e2014-05-15 15:31:50 +00002219 return(fabs(alpha-(*beta)) < MagickEpsilon ? 1.0 : 0.0);
cristy3ed852e2009-09-05 21:47:34 +00002220 }
2221 case NotEqualOperator:
2222 {
2223 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy972050b2012-06-04 22:09:17 +00002224 return(fabs(alpha-(*beta)) >= MagickEpsilon ? 1.0 : 0.0);
cristy3ed852e2009-09-05 21:47:34 +00002225 }
2226 case '&':
2227 {
2228 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristya19f1d72012-08-07 18:24:38 +00002229 *beta=(double) ((size_t) (alpha+0.5) & (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002230 return(*beta);
2231 }
2232 case '|':
2233 {
2234 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristya19f1d72012-08-07 18:24:38 +00002235 *beta=(double) ((size_t) (alpha+0.5) | (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002236 return(*beta);
2237 }
2238 case LogicalAndOperator:
2239 {
2240 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2241 *beta=(alpha > 0.0) && (gamma > 0.0) ? 1.0 : 0.0;
2242 return(*beta);
2243 }
2244 case LogicalOrOperator:
2245 {
2246 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2247 *beta=(alpha > 0.0) || (gamma > 0.0) ? 1.0 : 0.0;
2248 return(*beta);
2249 }
2250 case '?':
2251 {
cristya19f1d72012-08-07 18:24:38 +00002252 double
cristy3ed852e2009-09-05 21:47:34 +00002253 gamma;
2254
2255 (void) CopyMagickString(subexpression,++p,MaxTextExtent);
2256 q=subexpression;
2257 p=StringToken(":",&q);
2258 if (q == (char *) NULL)
2259 {
2260 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00002261 OptionError,"UnableToParseExpression","`%s'",subexpression);
cristy3ed852e2009-09-05 21:47:34 +00002262 return(0.0);
2263 }
cristy972050b2012-06-04 22:09:17 +00002264 if (fabs((double) alpha) >= MagickEpsilon)
cristy3ed852e2009-09-05 21:47:34 +00002265 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,exception);
2266 else
2267 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,q,beta,exception);
2268 return(gamma);
2269 }
2270 case '=':
2271 {
2272 char
2273 numeric[MaxTextExtent];
2274
2275 q=subexpression;
2276 while (isalpha((int) ((unsigned char) *q)) != 0)
2277 q++;
2278 if (*q != '\0')
2279 {
2280 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00002281 OptionError,"UnableToParseExpression","`%s'",subexpression);
cristy3ed852e2009-09-05 21:47:34 +00002282 return(0.0);
2283 }
2284 ClearMagickException(exception);
2285 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristyb51dff52011-05-19 16:55:47 +00002286 (void) FormatLocaleString(numeric,MaxTextExtent,"%g",(double)
cristy8cd5b312010-01-07 01:10:24 +00002287 *beta);
cristy3ed852e2009-09-05 21:47:34 +00002288 (void) DeleteNodeFromSplayTree(fx_info->symbols,subexpression);
2289 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(
2290 subexpression),ConstantString(numeric));
2291 return(*beta);
2292 }
2293 case ',':
2294 {
2295 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2296 return(alpha);
2297 }
2298 case ';':
2299 {
2300 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2301 return(*beta);
2302 }
2303 default:
2304 {
2305 gamma=alpha*FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,
2306 exception);
2307 return(gamma);
2308 }
2309 }
2310 }
2311 if (strchr("(",(int) *expression) != (char *) NULL)
2312 {
2313 (void) CopyMagickString(subexpression,expression+1,MaxTextExtent);
2314 subexpression[strlen(subexpression)-1]='\0';
2315 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2316 exception);
2317 return(gamma);
2318 }
cristy8b8a3ae2010-10-23 18:49:46 +00002319 switch (*expression)
cristy3ed852e2009-09-05 21:47:34 +00002320 {
2321 case '+':
2322 {
2323 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2324 exception);
2325 return(1.0*gamma);
2326 }
2327 case '-':
2328 {
2329 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2330 exception);
2331 return(-1.0*gamma);
2332 }
2333 case '~':
2334 {
2335 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2336 exception);
cristya19f1d72012-08-07 18:24:38 +00002337 return((double) (~(size_t) (gamma+0.5)));
cristy3ed852e2009-09-05 21:47:34 +00002338 }
2339 case 'A':
2340 case 'a':
2341 {
2342 if (LocaleNCompare(expression,"abs",3) == 0)
2343 {
2344 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2345 exception);
cristya19f1d72012-08-07 18:24:38 +00002346 return((double) fabs((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002347 }
cristyb33454f2011-08-03 02:10:45 +00002348#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00002349 if (LocaleNCompare(expression,"acosh",5) == 0)
2350 {
2351 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2352 exception);
cristya19f1d72012-08-07 18:24:38 +00002353 return((double) acosh((double) alpha));
cristy363772a2011-07-28 23:25:33 +00002354 }
cristyb33454f2011-08-03 02:10:45 +00002355#endif
cristy3ed852e2009-09-05 21:47:34 +00002356 if (LocaleNCompare(expression,"acos",4) == 0)
2357 {
2358 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2359 exception);
cristya19f1d72012-08-07 18:24:38 +00002360 return((double) acos((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002361 }
cristy43c22f42010-03-30 12:34:07 +00002362#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002363 if (LocaleNCompare(expression,"airy",4) == 0)
2364 {
2365 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2366 exception);
2367 if (alpha == 0.0)
cristy2dd03222010-03-30 22:12:11 +00002368 return(1.0);
2369 gamma=2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha);
cristy43c22f42010-03-30 12:34:07 +00002370 return(gamma*gamma);
cristyee56cf12010-03-01 22:17:06 +00002371 }
cristy43c22f42010-03-30 12:34:07 +00002372#endif
cristyb33454f2011-08-03 02:10:45 +00002373#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00002374 if (LocaleNCompare(expression,"asinh",5) == 0)
2375 {
2376 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2377 exception);
cristya19f1d72012-08-07 18:24:38 +00002378 return((double) asinh((double) alpha));
cristy363772a2011-07-28 23:25:33 +00002379 }
cristyb33454f2011-08-03 02:10:45 +00002380#endif
cristy3ed852e2009-09-05 21:47:34 +00002381 if (LocaleNCompare(expression,"asin",4) == 0)
2382 {
2383 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2384 exception);
cristya19f1d72012-08-07 18:24:38 +00002385 return((double) asin((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002386 }
2387 if (LocaleNCompare(expression,"alt",3) == 0)
2388 {
2389 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2390 exception);
cristybb503372010-05-27 20:51:26 +00002391 return(((ssize_t) alpha) & 0x01 ? -1.0 : 1.0);
cristy3ed852e2009-09-05 21:47:34 +00002392 }
2393 if (LocaleNCompare(expression,"atan2",5) == 0)
2394 {
2395 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2396 exception);
cristya19f1d72012-08-07 18:24:38 +00002397 return((double) atan2((double) alpha,(double) *beta));
cristy3ed852e2009-09-05 21:47:34 +00002398 }
cristyb33454f2011-08-03 02:10:45 +00002399#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00002400 if (LocaleNCompare(expression,"atanh",5) == 0)
2401 {
2402 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2403 exception);
cristya19f1d72012-08-07 18:24:38 +00002404 return((double) atanh((double) alpha));
cristy363772a2011-07-28 23:25:33 +00002405 }
cristyb33454f2011-08-03 02:10:45 +00002406#endif
cristy3ed852e2009-09-05 21:47:34 +00002407 if (LocaleNCompare(expression,"atan",4) == 0)
2408 {
2409 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2410 exception);
cristya19f1d72012-08-07 18:24:38 +00002411 return((double) atan((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002412 }
2413 if (LocaleCompare(expression,"a") == 0)
2414 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2415 break;
2416 }
2417 case 'B':
2418 case 'b':
2419 {
2420 if (LocaleCompare(expression,"b") == 0)
2421 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2422 break;
2423 }
2424 case 'C':
2425 case 'c':
2426 {
2427 if (LocaleNCompare(expression,"ceil",4) == 0)
2428 {
2429 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2430 exception);
cristya19f1d72012-08-07 18:24:38 +00002431 return((double) ceil((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002432 }
2433 if (LocaleNCompare(expression,"cosh",4) == 0)
2434 {
2435 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2436 exception);
cristya19f1d72012-08-07 18:24:38 +00002437 return((double) cosh((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002438 }
2439 if (LocaleNCompare(expression,"cos",3) == 0)
2440 {
2441 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2442 exception);
cristya19f1d72012-08-07 18:24:38 +00002443 return((double) cos((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002444 }
2445 if (LocaleCompare(expression,"c") == 0)
2446 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2447 break;
2448 }
2449 case 'D':
2450 case 'd':
2451 {
2452 if (LocaleNCompare(expression,"debug",5) == 0)
2453 {
2454 const char
2455 *type;
2456
2457 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2458 exception);
2459 if (fx_info->images->colorspace == CMYKColorspace)
2460 switch (channel)
2461 {
cristy0568ffc2011-07-25 16:54:14 +00002462 case CyanPixelChannel: type="cyan"; break;
2463 case MagentaPixelChannel: type="magenta"; break;
2464 case YellowPixelChannel: type="yellow"; break;
2465 case AlphaPixelChannel: type="opacity"; break;
2466 case BlackPixelChannel: type="black"; break;
cristy3ed852e2009-09-05 21:47:34 +00002467 default: type="unknown"; break;
2468 }
2469 else
2470 switch (channel)
2471 {
cristy0568ffc2011-07-25 16:54:14 +00002472 case RedPixelChannel: type="red"; break;
2473 case GreenPixelChannel: type="green"; break;
2474 case BluePixelChannel: type="blue"; break;
2475 case AlphaPixelChannel: type="opacity"; break;
cristy3ed852e2009-09-05 21:47:34 +00002476 default: type="unknown"; break;
2477 }
2478 (void) CopyMagickString(subexpression,expression+6,MaxTextExtent);
2479 if (strlen(subexpression) > 1)
2480 subexpression[strlen(subexpression)-1]='\0';
2481 if (fx_info->file != (FILE *) NULL)
cristy1707c6c2012-01-18 23:30:54 +00002482 (void) FormatLocaleFile(fx_info->file,"%s[%.20g,%.20g].%s: "
2483 "%s=%.*g\n",fx_info->images->filename,(double) x,(double) y,type,
2484 subexpression,GetMagickPrecision(),(double) alpha);
cristy3ed852e2009-09-05 21:47:34 +00002485 return(0.0);
2486 }
cristy5597a8d2011-11-04 00:25:32 +00002487 if (LocaleNCompare(expression,"drc",3) == 0)
2488 {
2489 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2490 exception);
cristya19f1d72012-08-07 18:24:38 +00002491 return((double) (alpha/(*beta*(alpha-1.0)+1.0)));
cristy5597a8d2011-11-04 00:25:32 +00002492 }
cristy3ed852e2009-09-05 21:47:34 +00002493 break;
2494 }
2495 case 'E':
2496 case 'e':
2497 {
2498 if (LocaleCompare(expression,"epsilon") == 0)
cristya19f1d72012-08-07 18:24:38 +00002499 return((double) MagickEpsilon);
cristy3ed852e2009-09-05 21:47:34 +00002500 if (LocaleNCompare(expression,"exp",3) == 0)
2501 {
2502 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2503 exception);
cristya19f1d72012-08-07 18:24:38 +00002504 return((double) exp((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002505 }
2506 if (LocaleCompare(expression,"e") == 0)
cristya19f1d72012-08-07 18:24:38 +00002507 return((double) 2.7182818284590452354);
cristy3ed852e2009-09-05 21:47:34 +00002508 break;
2509 }
2510 case 'F':
2511 case 'f':
2512 {
2513 if (LocaleNCompare(expression,"floor",5) == 0)
2514 {
2515 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2516 exception);
cristya19f1d72012-08-07 18:24:38 +00002517 return((double) floor((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002518 }
2519 break;
2520 }
2521 case 'G':
2522 case 'g':
2523 {
cristy9eeedea2011-11-02 19:04:05 +00002524 if (LocaleNCompare(expression,"gauss",5) == 0)
2525 {
2526 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2527 exception);
2528 gamma=exp((double) (-alpha*alpha/2.0))/sqrt(2.0*MagickPI);
cristya19f1d72012-08-07 18:24:38 +00002529 return((double) gamma);
cristy9eeedea2011-11-02 19:04:05 +00002530 }
cristyb0aad4c2011-11-02 19:30:35 +00002531 if (LocaleNCompare(expression,"gcd",3) == 0)
2532 {
2533 MagickOffsetType
2534 gcd;
2535
2536 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2537 exception);
cristy1707c6c2012-01-18 23:30:54 +00002538 gcd=FxGCD((MagickOffsetType) (alpha+0.5),(MagickOffsetType) (*beta+
2539 0.5));
cristya19f1d72012-08-07 18:24:38 +00002540 return((double) gcd);
cristyb0aad4c2011-11-02 19:30:35 +00002541 }
cristy3ed852e2009-09-05 21:47:34 +00002542 if (LocaleCompare(expression,"g") == 0)
2543 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2544 break;
2545 }
2546 case 'H':
2547 case 'h':
2548 {
2549 if (LocaleCompare(expression,"h") == 0)
2550 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2551 if (LocaleCompare(expression,"hue") == 0)
2552 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2553 if (LocaleNCompare(expression,"hypot",5) == 0)
2554 {
2555 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2556 exception);
cristya19f1d72012-08-07 18:24:38 +00002557 return((double) hypot((double) alpha,(double) *beta));
cristy3ed852e2009-09-05 21:47:34 +00002558 }
2559 break;
2560 }
2561 case 'K':
2562 case 'k':
2563 {
2564 if (LocaleCompare(expression,"k") == 0)
2565 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2566 break;
2567 }
2568 case 'I':
2569 case 'i':
2570 {
2571 if (LocaleCompare(expression,"intensity") == 0)
2572 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2573 if (LocaleNCompare(expression,"int",3) == 0)
2574 {
2575 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2576 exception);
cristya19f1d72012-08-07 18:24:38 +00002577 return((double) floor(alpha));
cristy3ed852e2009-09-05 21:47:34 +00002578 }
cristy82b20722011-11-05 21:52:36 +00002579#if defined(MAGICKCORE_HAVE_ISNAN)
cristy639399c2011-11-02 19:16:15 +00002580 if (LocaleNCompare(expression,"isnan",5) == 0)
2581 {
2582 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2583 exception);
cristya19f1d72012-08-07 18:24:38 +00002584 return((double) !!isnan((double) alpha));
cristy639399c2011-11-02 19:16:15 +00002585 }
cristy82b20722011-11-05 21:52:36 +00002586#endif
cristy3ed852e2009-09-05 21:47:34 +00002587 if (LocaleCompare(expression,"i") == 0)
2588 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2589 break;
2590 }
2591 case 'J':
2592 case 'j':
2593 {
2594 if (LocaleCompare(expression,"j") == 0)
2595 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
cristy161b9262010-03-20 19:34:32 +00002596#if defined(MAGICKCORE_HAVE_J0)
cristyee56cf12010-03-01 22:17:06 +00002597 if (LocaleNCompare(expression,"j0",2) == 0)
2598 {
2599 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2600 exception);
cristya19f1d72012-08-07 18:24:38 +00002601 return((double) j0((double) alpha));
cristyee56cf12010-03-01 22:17:06 +00002602 }
cristy161b9262010-03-20 19:34:32 +00002603#endif
2604#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002605 if (LocaleNCompare(expression,"j1",2) == 0)
2606 {
2607 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2608 exception);
cristya19f1d72012-08-07 18:24:38 +00002609 return((double) j1((double) alpha));
cristyee56cf12010-03-01 22:17:06 +00002610 }
cristy161b9262010-03-20 19:34:32 +00002611#endif
cristyaa018fa2010-04-08 23:03:54 +00002612#if defined(MAGICKCORE_HAVE_J1)
cristya6a09e72010-03-02 14:51:02 +00002613 if (LocaleNCompare(expression,"jinc",4) == 0)
2614 {
2615 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2616 exception);
cristy0946a822010-03-12 17:14:58 +00002617 if (alpha == 0.0)
2618 return(1.0);
cristyc90d70d2012-11-03 23:53:13 +00002619 gamma=(double) (2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha));
cristyfce2f7b2010-03-12 00:29:49 +00002620 return(gamma);
cristya6a09e72010-03-02 14:51:02 +00002621 }
cristyaa018fa2010-04-08 23:03:54 +00002622#endif
cristy3ed852e2009-09-05 21:47:34 +00002623 break;
2624 }
2625 case 'L':
2626 case 'l':
2627 {
2628 if (LocaleNCompare(expression,"ln",2) == 0)
2629 {
2630 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2631 exception);
cristya19f1d72012-08-07 18:24:38 +00002632 return((double) log((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002633 }
cristyc8ed5322010-08-31 12:07:59 +00002634 if (LocaleNCompare(expression,"logtwo",6) == 0)
cristy3ed852e2009-09-05 21:47:34 +00002635 {
cristyc8ed5322010-08-31 12:07:59 +00002636 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
cristy3ed852e2009-09-05 21:47:34 +00002637 exception);
cristya19f1d72012-08-07 18:24:38 +00002638 return((double) log10((double) alpha))/log10(2.0);
cristy3ed852e2009-09-05 21:47:34 +00002639 }
2640 if (LocaleNCompare(expression,"log",3) == 0)
2641 {
2642 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2643 exception);
cristya19f1d72012-08-07 18:24:38 +00002644 return((double) log10((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002645 }
2646 if (LocaleCompare(expression,"lightness") == 0)
2647 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2648 break;
2649 }
2650 case 'M':
2651 case 'm':
2652 {
2653 if (LocaleCompare(expression,"MaxRGB") == 0)
cristya19f1d72012-08-07 18:24:38 +00002654 return((double) QuantumRange);
cristy3ed852e2009-09-05 21:47:34 +00002655 if (LocaleNCompare(expression,"maxima",6) == 0)
2656 break;
2657 if (LocaleNCompare(expression,"max",3) == 0)
cristy984049c2011-11-03 18:34:58 +00002658 {
2659 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2660 exception);
2661 return(alpha > *beta ? alpha : *beta);
2662 }
cristy3ed852e2009-09-05 21:47:34 +00002663 if (LocaleNCompare(expression,"minima",6) == 0)
2664 break;
2665 if (LocaleNCompare(expression,"min",3) == 0)
cristy984049c2011-11-03 18:34:58 +00002666 {
2667 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2668 exception);
2669 return(alpha < *beta ? alpha : *beta);
2670 }
cristy3ed852e2009-09-05 21:47:34 +00002671 if (LocaleNCompare(expression,"mod",3) == 0)
2672 {
2673 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2674 exception);
cristy984049c2011-11-03 18:34:58 +00002675 gamma=alpha-floor((double) (alpha/(*beta)))*(*beta);
2676 return(gamma);
cristy3ed852e2009-09-05 21:47:34 +00002677 }
2678 if (LocaleCompare(expression,"m") == 0)
2679 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2680 break;
2681 }
2682 case 'N':
2683 case 'n':
2684 {
cristyad3502e2011-11-02 19:10:45 +00002685 if (LocaleNCompare(expression,"not",3) == 0)
2686 {
2687 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2688 exception);
cristya19f1d72012-08-07 18:24:38 +00002689 return((double) (alpha < MagickEpsilon));
cristyad3502e2011-11-02 19:10:45 +00002690 }
cristy3ed852e2009-09-05 21:47:34 +00002691 if (LocaleCompare(expression,"n") == 0)
2692 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2693 break;
2694 }
2695 case 'O':
2696 case 'o':
2697 {
2698 if (LocaleCompare(expression,"Opaque") == 0)
2699 return(1.0);
2700 if (LocaleCompare(expression,"o") == 0)
2701 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2702 break;
2703 }
2704 case 'P':
2705 case 'p':
2706 {
cristy670aa3c2011-11-03 00:54:00 +00002707 if (LocaleCompare(expression,"phi") == 0)
cristya19f1d72012-08-07 18:24:38 +00002708 return((double) MagickPHI);
cristy3ed852e2009-09-05 21:47:34 +00002709 if (LocaleCompare(expression,"pi") == 0)
cristya19f1d72012-08-07 18:24:38 +00002710 return((double) MagickPI);
cristy3ed852e2009-09-05 21:47:34 +00002711 if (LocaleNCompare(expression,"pow",3) == 0)
2712 {
2713 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2714 exception);
cristya19f1d72012-08-07 18:24:38 +00002715 return((double) pow((double) alpha,(double) *beta));
cristy3ed852e2009-09-05 21:47:34 +00002716 }
2717 if (LocaleCompare(expression,"p") == 0)
2718 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2719 break;
2720 }
2721 case 'Q':
2722 case 'q':
2723 {
2724 if (LocaleCompare(expression,"QuantumRange") == 0)
cristya19f1d72012-08-07 18:24:38 +00002725 return((double) QuantumRange);
cristy3ed852e2009-09-05 21:47:34 +00002726 if (LocaleCompare(expression,"QuantumScale") == 0)
cristya19f1d72012-08-07 18:24:38 +00002727 return((double) QuantumScale);
cristy3ed852e2009-09-05 21:47:34 +00002728 break;
2729 }
2730 case 'R':
2731 case 'r':
2732 {
2733 if (LocaleNCompare(expression,"rand",4) == 0)
cristya19f1d72012-08-07 18:24:38 +00002734 return((double) GetPseudoRandomValue(fx_info->random_info));
cristy3ed852e2009-09-05 21:47:34 +00002735 if (LocaleNCompare(expression,"round",5) == 0)
2736 {
2737 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2738 exception);
cristya19f1d72012-08-07 18:24:38 +00002739 return((double) floor((double) alpha+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002740 }
2741 if (LocaleCompare(expression,"r") == 0)
2742 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2743 break;
2744 }
2745 case 'S':
2746 case 's':
2747 {
2748 if (LocaleCompare(expression,"saturation") == 0)
2749 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2750 if (LocaleNCompare(expression,"sign",4) == 0)
2751 {
2752 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2753 exception);
2754 return(alpha < 0.0 ? -1.0 : 1.0);
2755 }
cristya6a09e72010-03-02 14:51:02 +00002756 if (LocaleNCompare(expression,"sinc",4) == 0)
2757 {
2758 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2759 exception);
2760 if (alpha == 0)
2761 return(1.0);
cristya19f1d72012-08-07 18:24:38 +00002762 gamma=(double) (sin((double) (MagickPI*alpha))/
cristya6a09e72010-03-02 14:51:02 +00002763 (MagickPI*alpha));
2764 return(gamma);
2765 }
cristy3ed852e2009-09-05 21:47:34 +00002766 if (LocaleNCompare(expression,"sinh",4) == 0)
2767 {
2768 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2769 exception);
cristya19f1d72012-08-07 18:24:38 +00002770 return((double) sinh((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002771 }
2772 if (LocaleNCompare(expression,"sin",3) == 0)
2773 {
2774 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2775 exception);
cristya19f1d72012-08-07 18:24:38 +00002776 return((double) sin((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002777 }
2778 if (LocaleNCompare(expression,"sqrt",4) == 0)
2779 {
2780 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2781 exception);
cristya19f1d72012-08-07 18:24:38 +00002782 return((double) sqrt((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002783 }
cristy9eeedea2011-11-02 19:04:05 +00002784 if (LocaleNCompare(expression,"squish",6) == 0)
2785 {
2786 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2787 exception);
cristya19f1d72012-08-07 18:24:38 +00002788 return((double) (1.0/(1.0+exp((double) (-alpha)))));
cristy9eeedea2011-11-02 19:04:05 +00002789 }
cristy3ed852e2009-09-05 21:47:34 +00002790 if (LocaleCompare(expression,"s") == 0)
2791 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2792 break;
2793 }
2794 case 'T':
2795 case 't':
2796 {
2797 if (LocaleNCompare(expression,"tanh",4) == 0)
2798 {
2799 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2800 exception);
cristya19f1d72012-08-07 18:24:38 +00002801 return((double) tanh((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002802 }
2803 if (LocaleNCompare(expression,"tan",3) == 0)
2804 {
2805 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2806 exception);
cristya19f1d72012-08-07 18:24:38 +00002807 return((double) tan((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002808 }
2809 if (LocaleCompare(expression,"Transparent") == 0)
2810 return(0.0);
cristy16788e42010-08-13 13:44:26 +00002811 if (LocaleNCompare(expression,"trunc",5) == 0)
2812 {
2813 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2814 exception);
2815 if (alpha >= 0.0)
cristya19f1d72012-08-07 18:24:38 +00002816 return((double) floor((double) alpha));
2817 return((double) ceil((double) alpha));
cristy16788e42010-08-13 13:44:26 +00002818 }
cristy3ed852e2009-09-05 21:47:34 +00002819 if (LocaleCompare(expression,"t") == 0)
2820 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2821 break;
2822 }
2823 case 'U':
2824 case 'u':
2825 {
2826 if (LocaleCompare(expression,"u") == 0)
2827 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2828 break;
2829 }
2830 case 'V':
2831 case 'v':
2832 {
2833 if (LocaleCompare(expression,"v") == 0)
2834 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2835 break;
2836 }
2837 case 'W':
2838 case 'w':
2839 {
cristy9eeedea2011-11-02 19:04:05 +00002840 if (LocaleNCompare(expression,"while",5) == 0)
2841 {
2842 do
2843 {
2844 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2845 exception);
2846 } while (fabs((double) alpha) >= MagickEpsilon);
cristya19f1d72012-08-07 18:24:38 +00002847 return((double) *beta);
cristy9eeedea2011-11-02 19:04:05 +00002848 }
cristy3ed852e2009-09-05 21:47:34 +00002849 if (LocaleCompare(expression,"w") == 0)
2850 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2851 break;
2852 }
2853 case 'Y':
2854 case 'y':
2855 {
2856 if (LocaleCompare(expression,"y") == 0)
2857 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2858 break;
2859 }
2860 case 'Z':
2861 case 'z':
2862 {
2863 if (LocaleCompare(expression,"z") == 0)
2864 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2865 break;
2866 }
2867 default:
2868 break;
2869 }
2870 q=(char *) expression;
cristydbdd0e32011-11-04 23:29:40 +00002871 alpha=InterpretSiPrefixValue(expression,&q);
cristy3ed852e2009-09-05 21:47:34 +00002872 if (q == expression)
2873 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2874 return(alpha);
2875}
2876
cristy7832dc22011-09-05 01:21:53 +00002877MagickPrivate MagickBooleanType FxEvaluateExpression(FxInfo *fx_info,
cristya19f1d72012-08-07 18:24:38 +00002878 double *alpha,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002879{
2880 MagickBooleanType
2881 status;
2882
cristy541ae572011-08-05 19:08:59 +00002883 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2884 exception);
cristy3ed852e2009-09-05 21:47:34 +00002885 return(status);
2886}
2887
2888MagickExport MagickBooleanType FxPreprocessExpression(FxInfo *fx_info,
cristya19f1d72012-08-07 18:24:38 +00002889 double *alpha,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002890{
2891 FILE
2892 *file;
2893
2894 MagickBooleanType
2895 status;
2896
2897 file=fx_info->file;
2898 fx_info->file=(FILE *) NULL;
cristy541ae572011-08-05 19:08:59 +00002899 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2900 exception);
cristy3ed852e2009-09-05 21:47:34 +00002901 fx_info->file=file;
2902 return(status);
2903}
2904
cristy7832dc22011-09-05 01:21:53 +00002905MagickPrivate MagickBooleanType FxEvaluateChannelExpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002906 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristya19f1d72012-08-07 18:24:38 +00002907 double *alpha,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002908{
cristya19f1d72012-08-07 18:24:38 +00002909 double
cristy3ed852e2009-09-05 21:47:34 +00002910 beta;
2911
2912 beta=0.0;
2913 *alpha=FxEvaluateSubexpression(fx_info,channel,x,y,fx_info->expression,&beta,
2914 exception);
2915 return(exception->severity == OptionError ? MagickFalse : MagickTrue);
2916}
2917
2918/*
2919%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2920% %
2921% %
2922% %
2923% F x I m a g e %
2924% %
2925% %
2926% %
2927%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2928%
2929% FxImage() applies a mathematical expression to the specified image.
2930%
2931% The format of the FxImage method is:
2932%
2933% Image *FxImage(const Image *image,const char *expression,
2934% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002935%
2936% A description of each parameter follows:
2937%
2938% o image: the image.
2939%
cristy3ed852e2009-09-05 21:47:34 +00002940% o expression: A mathematical expression.
2941%
2942% o exception: return any errors or warnings in this structure.
2943%
2944*/
2945
2946static FxInfo **DestroyFxThreadSet(FxInfo **fx_info)
2947{
cristybb503372010-05-27 20:51:26 +00002948 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002949 i;
2950
2951 assert(fx_info != (FxInfo **) NULL);
cristyac245f82012-05-05 17:13:57 +00002952 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
cristy3ed852e2009-09-05 21:47:34 +00002953 if (fx_info[i] != (FxInfo *) NULL)
2954 fx_info[i]=DestroyFxInfo(fx_info[i]);
cristyb41ee102010-10-04 16:46:15 +00002955 fx_info=(FxInfo **) RelinquishMagickMemory(fx_info);
cristy3ed852e2009-09-05 21:47:34 +00002956 return(fx_info);
2957}
2958
2959static FxInfo **AcquireFxThreadSet(const Image *image,const char *expression,
2960 ExceptionInfo *exception)
2961{
2962 char
2963 *fx_expression;
2964
2965 FxInfo
2966 **fx_info;
2967
cristya19f1d72012-08-07 18:24:38 +00002968 double
cristy3ed852e2009-09-05 21:47:34 +00002969 alpha;
2970
cristybb503372010-05-27 20:51:26 +00002971 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002972 i;
2973
cristybb503372010-05-27 20:51:26 +00002974 size_t
cristy3ed852e2009-09-05 21:47:34 +00002975 number_threads;
2976
cristy9357bdd2012-07-30 12:28:34 +00002977 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
cristyb41ee102010-10-04 16:46:15 +00002978 fx_info=(FxInfo **) AcquireQuantumMemory(number_threads,sizeof(*fx_info));
cristy3ed852e2009-09-05 21:47:34 +00002979 if (fx_info == (FxInfo **) NULL)
cristy65d161b2012-10-07 20:39:52 +00002980 {
2981 (void) ThrowMagickException(exception,GetMagickModule(),
2982 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
2983 return((FxInfo **) NULL);
2984 }
cristy3ed852e2009-09-05 21:47:34 +00002985 (void) ResetMagickMemory(fx_info,0,number_threads*sizeof(*fx_info));
2986 if (*expression != '@')
2987 fx_expression=ConstantString(expression);
2988 else
cristy3a5987c2013-11-07 14:18:46 +00002989 fx_expression=FileToString(expression+1,~0UL,exception);
cristybb503372010-05-27 20:51:26 +00002990 for (i=0; i < (ssize_t) number_threads; i++)
cristy3ed852e2009-09-05 21:47:34 +00002991 {
cristy65d161b2012-10-07 20:39:52 +00002992 MagickBooleanType
2993 status;
2994
cristydb070952012-04-20 14:33:00 +00002995 fx_info[i]=AcquireFxInfo(image,fx_expression,exception);
cristy3ed852e2009-09-05 21:47:34 +00002996 if (fx_info[i] == (FxInfo *) NULL)
cristy65d161b2012-10-07 20:39:52 +00002997 break;
2998 status=FxPreprocessExpression(fx_info[i],&alpha,exception);
2999 if (status == MagickFalse)
3000 break;
cristy3ed852e2009-09-05 21:47:34 +00003001 }
3002 fx_expression=DestroyString(fx_expression);
cristy65d161b2012-10-07 20:39:52 +00003003 if (i < (ssize_t) number_threads)
3004 fx_info=DestroyFxThreadSet(fx_info);
cristy3ed852e2009-09-05 21:47:34 +00003005 return(fx_info);
3006}
3007
3008MagickExport Image *FxImage(const Image *image,const char *expression,
3009 ExceptionInfo *exception)
3010{
cristy3ed852e2009-09-05 21:47:34 +00003011#define FxImageTag "Fx/Image"
3012
cristyfa112112010-01-04 17:48:07 +00003013 CacheView
cristy79cedc72011-07-25 00:41:15 +00003014 *fx_view,
3015 *image_view;
cristyfa112112010-01-04 17:48:07 +00003016
cristy3ed852e2009-09-05 21:47:34 +00003017 FxInfo
cristyfa112112010-01-04 17:48:07 +00003018 **restrict fx_info;
cristy3ed852e2009-09-05 21:47:34 +00003019
3020 Image
3021 *fx_image;
3022
cristy3ed852e2009-09-05 21:47:34 +00003023 MagickBooleanType
3024 status;
3025
cristybb503372010-05-27 20:51:26 +00003026 MagickOffsetType
3027 progress;
3028
cristybb503372010-05-27 20:51:26 +00003029 ssize_t
3030 y;
3031
cristy3ed852e2009-09-05 21:47:34 +00003032 assert(image != (Image *) NULL);
3033 assert(image->signature == MagickSignature);
3034 if (image->debug != MagickFalse)
3035 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3ed852e2009-09-05 21:47:34 +00003036 fx_info=AcquireFxThreadSet(image,expression,exception);
3037 if (fx_info == (FxInfo **) NULL)
cristy65d161b2012-10-07 20:39:52 +00003038 return((Image *) NULL);
3039 fx_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3040 if (fx_image == (Image *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003041 {
cristy3ed852e2009-09-05 21:47:34 +00003042 fx_info=DestroyFxThreadSet(fx_info);
3043 return((Image *) NULL);
3044 }
cristy65d161b2012-10-07 20:39:52 +00003045 if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse)
3046 {
3047 fx_info=DestroyFxThreadSet(fx_info);
3048 fx_image=DestroyImage(fx_image);
3049 return((Image *) NULL);
3050 }
cristy3ed852e2009-09-05 21:47:34 +00003051 /*
3052 Fx image.
3053 */
3054 status=MagickTrue;
3055 progress=0;
cristy46ff2672012-12-14 15:32:26 +00003056 image_view=AcquireVirtualCacheView(image,exception);
3057 fx_view=AcquireAuthenticCacheView(fx_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00003058#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003059 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00003060 magick_threads(image,fx_image,fx_image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003061#endif
cristybb503372010-05-27 20:51:26 +00003062 for (y=0; y < (ssize_t) fx_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003063 {
cristy5c9e6f22010-09-17 17:31:01 +00003064 const int
3065 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00003066
cristy79cedc72011-07-25 00:41:15 +00003067 register const Quantum
3068 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003069
cristy4c08aed2011-07-01 19:47:50 +00003070 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003071 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003072
cristy79cedc72011-07-25 00:41:15 +00003073 register ssize_t
3074 x;
3075
cristy3ed852e2009-09-05 21:47:34 +00003076 if (status == MagickFalse)
3077 continue;
cristy79cedc72011-07-25 00:41:15 +00003078 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +00003079 q=QueueCacheViewAuthenticPixels(fx_view,0,y,fx_image->columns,1,exception);
cristy79cedc72011-07-25 00:41:15 +00003080 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003081 {
3082 status=MagickFalse;
3083 continue;
3084 }
cristybb503372010-05-27 20:51:26 +00003085 for (x=0; x < (ssize_t) fx_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003086 {
cristy79cedc72011-07-25 00:41:15 +00003087 register ssize_t
3088 i;
3089
3090 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3091 {
cristya19f1d72012-08-07 18:24:38 +00003092 double
cristy79cedc72011-07-25 00:41:15 +00003093 alpha;
3094
cristy5a23c552013-02-13 14:34:28 +00003095 PixelChannel channel=GetPixelChannelChannel(image,i);
3096 PixelTrait traits=GetPixelChannelTraits(image,channel);
3097 PixelTrait fx_traits=GetPixelChannelTraits(fx_image,channel);
cristy010d7d12011-08-31 01:02:48 +00003098 if ((traits == UndefinedPixelTrait) ||
3099 (fx_traits == UndefinedPixelTrait))
cristy79cedc72011-07-25 00:41:15 +00003100 continue;
cristy1eced092012-08-10 23:10:56 +00003101 if (((fx_traits & CopyPixelTrait) != 0) ||
cristy883fde12013-04-08 00:50:13 +00003102 (GetPixelReadMask(image,p) == 0))
cristy79cedc72011-07-25 00:41:15 +00003103 {
cristy0beccfa2011-09-25 20:47:53 +00003104 SetPixelChannel(fx_image,channel,p[i],q);
cristy79cedc72011-07-25 00:41:15 +00003105 continue;
3106 }
3107 alpha=0.0;
cristya382aca2011-12-06 18:22:48 +00003108 (void) FxEvaluateChannelExpression(fx_info[id],channel,x,y,&alpha,
3109 exception);
cristy8cd03c32012-07-07 18:57:59 +00003110 q[i]=ClampToQuantum(QuantumRange*alpha);
cristy79cedc72011-07-25 00:41:15 +00003111 }
3112 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003113 q+=GetPixelChannels(fx_image);
cristy3ed852e2009-09-05 21:47:34 +00003114 }
3115 if (SyncCacheViewAuthenticPixels(fx_view,exception) == MagickFalse)
3116 status=MagickFalse;
3117 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3118 {
3119 MagickBooleanType
3120 proceed;
3121
cristyb5d5f722009-11-04 03:03:49 +00003122#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003123 #pragma omp critical (MagickCore_FxImage)
cristy3ed852e2009-09-05 21:47:34 +00003124#endif
3125 proceed=SetImageProgress(image,FxImageTag,progress++,image->rows);
3126 if (proceed == MagickFalse)
3127 status=MagickFalse;
3128 }
3129 }
cristy3ed852e2009-09-05 21:47:34 +00003130 fx_view=DestroyCacheView(fx_view);
cristy79cedc72011-07-25 00:41:15 +00003131 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003132 fx_info=DestroyFxThreadSet(fx_info);
3133 if (status == MagickFalse)
3134 fx_image=DestroyImage(fx_image);
3135 return(fx_image);
3136}
3137
3138/*
3139%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3140% %
3141% %
3142% %
3143% I m p l o d e I m a g e %
3144% %
3145% %
3146% %
3147%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3148%
3149% ImplodeImage() creates a new image that is a copy of an existing
3150% one with the image pixels "implode" by the specified percentage. It
3151% allocates the memory necessary for the new Image structure and returns a
3152% pointer to the new image.
3153%
3154% The format of the ImplodeImage method is:
3155%
3156% Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003157% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003158%
3159% A description of each parameter follows:
3160%
3161% o implode_image: Method ImplodeImage returns a pointer to the image
3162% after it is implode. A null image is returned if there is a memory
3163% shortage.
3164%
3165% o image: the image.
3166%
3167% o amount: Define the extent of the implosion.
3168%
cristy76f512e2011-09-12 01:26:56 +00003169% o method: the pixel interpolation method.
3170%
cristy3ed852e2009-09-05 21:47:34 +00003171% o exception: return any errors or warnings in this structure.
3172%
3173*/
3174MagickExport Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003175 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003176{
3177#define ImplodeImageTag "Implode/Image"
3178
cristyfa112112010-01-04 17:48:07 +00003179 CacheView
3180 *image_view,
cristy5ee937c2013-04-19 12:55:16 +00003181 *implode_view,
3182 *interpolate_view;
cristyfa112112010-01-04 17:48:07 +00003183
cristy3ed852e2009-09-05 21:47:34 +00003184 Image
3185 *implode_image;
3186
cristy3ed852e2009-09-05 21:47:34 +00003187 MagickBooleanType
3188 status;
3189
cristybb503372010-05-27 20:51:26 +00003190 MagickOffsetType
3191 progress;
3192
cristya19f1d72012-08-07 18:24:38 +00003193 double
cristy3ed852e2009-09-05 21:47:34 +00003194 radius;
3195
3196 PointInfo
3197 center,
3198 scale;
3199
cristybb503372010-05-27 20:51:26 +00003200 ssize_t
3201 y;
3202
cristy3ed852e2009-09-05 21:47:34 +00003203 /*
3204 Initialize implode image attributes.
3205 */
3206 assert(image != (Image *) NULL);
3207 assert(image->signature == MagickSignature);
3208 if (image->debug != MagickFalse)
3209 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3210 assert(exception != (ExceptionInfo *) NULL);
3211 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00003212 implode_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3213 exception);
cristy3ed852e2009-09-05 21:47:34 +00003214 if (implode_image == (Image *) NULL)
3215 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003216 if (SetImageStorageClass(implode_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003217 {
cristy3ed852e2009-09-05 21:47:34 +00003218 implode_image=DestroyImage(implode_image);
3219 return((Image *) NULL);
3220 }
cristy4c08aed2011-07-01 19:47:50 +00003221 if (implode_image->background_color.alpha != OpaqueAlpha)
cristy8a46d822012-08-28 23:32:39 +00003222 implode_image->alpha_trait=BlendPixelTrait;
cristy3ed852e2009-09-05 21:47:34 +00003223 /*
3224 Compute scaling factor.
3225 */
3226 scale.x=1.0;
3227 scale.y=1.0;
3228 center.x=0.5*image->columns;
3229 center.y=0.5*image->rows;
3230 radius=center.x;
3231 if (image->columns > image->rows)
3232 scale.y=(double) image->columns/(double) image->rows;
3233 else
3234 if (image->columns < image->rows)
3235 {
3236 scale.x=(double) image->rows/(double) image->columns;
3237 radius=center.y;
3238 }
3239 /*
3240 Implode image.
3241 */
3242 status=MagickTrue;
3243 progress=0;
cristy46ff2672012-12-14 15:32:26 +00003244 image_view=AcquireVirtualCacheView(image,exception);
cristy5ee937c2013-04-19 12:55:16 +00003245 interpolate_view=AcquireVirtualCacheView(image,exception);
cristy46ff2672012-12-14 15:32:26 +00003246 implode_view=AcquireAuthenticCacheView(implode_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00003247#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003248 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00003249 magick_threads(image,implode_image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003250#endif
cristybb503372010-05-27 20:51:26 +00003251 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003252 {
cristya19f1d72012-08-07 18:24:38 +00003253 double
cristy3ed852e2009-09-05 21:47:34 +00003254 distance;
3255
3256 PointInfo
3257 delta;
3258
cristy6d188022011-09-12 13:23:33 +00003259 register const Quantum
3260 *restrict p;
3261
cristybb503372010-05-27 20:51:26 +00003262 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003263 x;
3264
cristy4c08aed2011-07-01 19:47:50 +00003265 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003266 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003267
3268 if (status == MagickFalse)
3269 continue;
cristy1665ce12013-04-19 12:18:43 +00003270 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +00003271 q=QueueCacheViewAuthenticPixels(implode_view,0,y,implode_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003272 exception);
cristy6d188022011-09-12 13:23:33 +00003273 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003274 {
3275 status=MagickFalse;
3276 continue;
3277 }
cristy3ed852e2009-09-05 21:47:34 +00003278 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00003279 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003280 {
cristy6d188022011-09-12 13:23:33 +00003281 register ssize_t
3282 i;
3283
cristy3ed852e2009-09-05 21:47:34 +00003284 /*
3285 Determine if the pixel is within an ellipse.
3286 */
cristy883fde12013-04-08 00:50:13 +00003287 if (GetPixelReadMask(image,p) == 0)
cristy10a6c612012-01-29 21:41:05 +00003288 {
cristyc3a58022013-10-09 23:22:42 +00003289 SetPixelBackgoundColor(implode_image,q);
cristy10a6c612012-01-29 21:41:05 +00003290 p+=GetPixelChannels(image);
3291 q+=GetPixelChannels(implode_image);
3292 continue;
3293 }
cristy3ed852e2009-09-05 21:47:34 +00003294 delta.x=scale.x*(double) (x-center.x);
3295 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00003296 if (distance >= (radius*radius))
cristya6d7a9b2012-01-18 20:04:48 +00003297 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy6d188022011-09-12 13:23:33 +00003298 {
cristy5a23c552013-02-13 14:34:28 +00003299 PixelChannel channel=GetPixelChannelChannel(image,i);
3300 PixelTrait traits=GetPixelChannelTraits(image,channel);
3301 PixelTrait implode_traits=GetPixelChannelTraits(implode_image,
3302 channel);
cristy1707c6c2012-01-18 23:30:54 +00003303 if ((traits == UndefinedPixelTrait) ||
3304 (implode_traits == UndefinedPixelTrait))
3305 continue;
cristya6d7a9b2012-01-18 20:04:48 +00003306 SetPixelChannel(implode_image,channel,p[i],q);
cristy6d188022011-09-12 13:23:33 +00003307 }
3308 else
cristy3ed852e2009-09-05 21:47:34 +00003309 {
3310 double
3311 factor;
3312
3313 /*
3314 Implode the pixel.
3315 */
3316 factor=1.0;
3317 if (distance > 0.0)
cristy1707c6c2012-01-18 23:30:54 +00003318 factor=pow(sin((double) (MagickPI*sqrt((double) distance)/radius/
3319 2)),-amount);
cristy5ee937c2013-04-19 12:55:16 +00003320 status=InterpolatePixelChannels(image,interpolate_view,implode_image,
3321 method,(double) (factor*delta.x/scale.x+center.x),(double) (factor*
3322 delta.y/scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00003323 }
cristy6d188022011-09-12 13:23:33 +00003324 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003325 q+=GetPixelChannels(implode_image);
cristy3ed852e2009-09-05 21:47:34 +00003326 }
3327 if (SyncCacheViewAuthenticPixels(implode_view,exception) == MagickFalse)
3328 status=MagickFalse;
3329 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3330 {
3331 MagickBooleanType
3332 proceed;
3333
cristyb5d5f722009-11-04 03:03:49 +00003334#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003335 #pragma omp critical (MagickCore_ImplodeImage)
cristy3ed852e2009-09-05 21:47:34 +00003336#endif
3337 proceed=SetImageProgress(image,ImplodeImageTag,progress++,image->rows);
3338 if (proceed == MagickFalse)
3339 status=MagickFalse;
3340 }
3341 }
3342 implode_view=DestroyCacheView(implode_view);
cristy5ee937c2013-04-19 12:55:16 +00003343 interpolate_view=DestroyCacheView(interpolate_view);
cristy3ed852e2009-09-05 21:47:34 +00003344 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003345 if (status == MagickFalse)
3346 implode_image=DestroyImage(implode_image);
3347 return(implode_image);
3348}
3349
3350/*
3351%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3352% %
3353% %
3354% %
3355% M o r p h I m a g e s %
3356% %
3357% %
3358% %
3359%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3360%
3361% The MorphImages() method requires a minimum of two images. The first
3362% image is transformed into the second by a number of intervening images
3363% as specified by frames.
3364%
3365% The format of the MorphImage method is:
3366%
cristybb503372010-05-27 20:51:26 +00003367% Image *MorphImages(const Image *image,const size_t number_frames,
cristy3ed852e2009-09-05 21:47:34 +00003368% ExceptionInfo *exception)
3369%
3370% A description of each parameter follows:
3371%
3372% o image: the image.
3373%
3374% o number_frames: Define the number of in-between image to generate.
3375% The more in-between frames, the smoother the morph.
3376%
3377% o exception: return any errors or warnings in this structure.
3378%
3379*/
cristy7de7f742012-12-14 00:28:27 +00003380MagickExport Image *MorphImages(const Image *image,const size_t number_frames,
3381 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003382{
3383#define MorphImageTag "Morph/Image"
3384
cristyfab83642012-12-14 00:31:38 +00003385 double
3386 alpha,
3387 beta;
3388
cristy3ed852e2009-09-05 21:47:34 +00003389 Image
3390 *morph_image,
3391 *morph_images;
3392
cristy9d314ff2011-03-09 01:30:28 +00003393 MagickBooleanType
3394 status;
cristy3ed852e2009-09-05 21:47:34 +00003395
3396 MagickOffsetType
3397 scene;
3398
cristy3ed852e2009-09-05 21:47:34 +00003399 register const Image
3400 *next;
3401
cristybb503372010-05-27 20:51:26 +00003402 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003403 i;
3404
cristy9d314ff2011-03-09 01:30:28 +00003405 ssize_t
3406 y;
cristy3ed852e2009-09-05 21:47:34 +00003407
3408 /*
3409 Clone first frame in sequence.
3410 */
3411 assert(image != (Image *) NULL);
3412 assert(image->signature == MagickSignature);
3413 if (image->debug != MagickFalse)
3414 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3415 assert(exception != (ExceptionInfo *) NULL);
3416 assert(exception->signature == MagickSignature);
3417 morph_images=CloneImage(image,0,0,MagickTrue,exception);
3418 if (morph_images == (Image *) NULL)
3419 return((Image *) NULL);
3420 if (GetNextImageInList(image) == (Image *) NULL)
3421 {
3422 /*
3423 Morph single image.
3424 */
cristybb503372010-05-27 20:51:26 +00003425 for (i=1; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003426 {
3427 morph_image=CloneImage(image,0,0,MagickTrue,exception);
3428 if (morph_image == (Image *) NULL)
3429 {
3430 morph_images=DestroyImageList(morph_images);
3431 return((Image *) NULL);
3432 }
3433 AppendImageToList(&morph_images,morph_image);
cristy8b27a6d2010-02-14 03:31:15 +00003434 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003435 {
cristy8b27a6d2010-02-14 03:31:15 +00003436 MagickBooleanType
3437 proceed;
3438
cristybb503372010-05-27 20:51:26 +00003439 proceed=SetImageProgress(image,MorphImageTag,(MagickOffsetType) i,
3440 number_frames);
cristy8b27a6d2010-02-14 03:31:15 +00003441 if (proceed == MagickFalse)
3442 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00003443 }
3444 }
3445 return(GetFirstImageInList(morph_images));
3446 }
3447 /*
3448 Morph image sequence.
3449 */
3450 status=MagickTrue;
3451 scene=0;
3452 next=image;
3453 for ( ; GetNextImageInList(next) != (Image *) NULL; next=GetNextImageInList(next))
3454 {
cristybb503372010-05-27 20:51:26 +00003455 for (i=0; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003456 {
3457 CacheView
3458 *image_view,
3459 *morph_view;
3460
cristya19f1d72012-08-07 18:24:38 +00003461 beta=(double) (i+1.0)/(double) (number_frames+1.0);
cristy3ed852e2009-09-05 21:47:34 +00003462 alpha=1.0-beta;
cristy15b98cd2010-09-12 19:42:50 +00003463 morph_image=ResizeImage(next,(size_t) (alpha*next->columns+beta*
cristyaa2c16c2012-03-25 22:21:35 +00003464 GetNextImageInList(next)->columns+0.5),(size_t) (alpha*next->rows+beta*
3465 GetNextImageInList(next)->rows+0.5),next->filter,exception);
cristy3ed852e2009-09-05 21:47:34 +00003466 if (morph_image == (Image *) NULL)
3467 {
3468 morph_images=DestroyImageList(morph_images);
3469 return((Image *) NULL);
3470 }
cristy1707c6c2012-01-18 23:30:54 +00003471 status=SetImageStorageClass(morph_image,DirectClass,exception);
3472 if (status == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003473 {
cristy3ed852e2009-09-05 21:47:34 +00003474 morph_image=DestroyImage(morph_image);
3475 return((Image *) NULL);
3476 }
3477 AppendImageToList(&morph_images,morph_image);
3478 morph_images=GetLastImageInList(morph_images);
cristy15b98cd2010-09-12 19:42:50 +00003479 morph_image=ResizeImage(GetNextImageInList(next),morph_images->columns,
cristyaa2c16c2012-03-25 22:21:35 +00003480 morph_images->rows,GetNextImageInList(next)->filter,exception);
cristy3ed852e2009-09-05 21:47:34 +00003481 if (morph_image == (Image *) NULL)
3482 {
3483 morph_images=DestroyImageList(morph_images);
3484 return((Image *) NULL);
3485 }
cristy46ff2672012-12-14 15:32:26 +00003486 image_view=AcquireVirtualCacheView(morph_image,exception);
3487 morph_view=AcquireAuthenticCacheView(morph_images,exception);
cristyb5d5f722009-11-04 03:03:49 +00003488#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003489 #pragma omp parallel for schedule(static,4) shared(status) \
cristy5e6b2592012-12-19 14:08:11 +00003490 magick_threads(morph_image,morph_image,morph_image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003491#endif
cristybb503372010-05-27 20:51:26 +00003492 for (y=0; y < (ssize_t) morph_images->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003493 {
3494 MagickBooleanType
3495 sync;
3496
cristy4c08aed2011-07-01 19:47:50 +00003497 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003498 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003499
cristybb503372010-05-27 20:51:26 +00003500 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003501 x;
3502
cristy4c08aed2011-07-01 19:47:50 +00003503 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003504 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003505
3506 if (status == MagickFalse)
3507 continue;
3508 p=GetCacheViewVirtualPixels(image_view,0,y,morph_image->columns,1,
3509 exception);
3510 q=GetCacheViewAuthenticPixels(morph_view,0,y,morph_images->columns,1,
3511 exception);
cristy4c08aed2011-07-01 19:47:50 +00003512 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003513 {
3514 status=MagickFalse;
3515 continue;
3516 }
cristybb503372010-05-27 20:51:26 +00003517 for (x=0; x < (ssize_t) morph_images->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003518 {
cristy1707c6c2012-01-18 23:30:54 +00003519 register ssize_t
3520 i;
3521
cristy10a6c612012-01-29 21:41:05 +00003522 for (i=0; i < (ssize_t) GetPixelChannels(morph_image); i++)
cristy1707c6c2012-01-18 23:30:54 +00003523 {
cristy031a2462013-05-24 13:29:27 +00003524 PixelChannel channel=GetPixelChannelChannel(morph_image,i);
3525 PixelTrait traits=GetPixelChannelTraits(morph_image,channel);
3526 PixelTrait morph_traits=GetPixelChannelTraits(morph_images,channel);
cristy1707c6c2012-01-18 23:30:54 +00003527 if ((traits == UndefinedPixelTrait) ||
3528 (morph_traits == UndefinedPixelTrait))
3529 continue;
cristy1eced092012-08-10 23:10:56 +00003530 if (((morph_traits & CopyPixelTrait) != 0) ||
cristy031a2462013-05-24 13:29:27 +00003531 (GetPixelReadMask(morph_images,p) == 0))
cristy1707c6c2012-01-18 23:30:54 +00003532 {
3533 SetPixelChannel(morph_image,channel,p[i],q);
3534 continue;
3535 }
3536 SetPixelChannel(morph_image,channel,ClampToQuantum(alpha*
3537 GetPixelChannel(morph_images,channel,q)+beta*p[i]),q);
3538 }
cristyed231572011-07-14 02:18:59 +00003539 p+=GetPixelChannels(morph_image);
3540 q+=GetPixelChannels(morph_images);
cristy3ed852e2009-09-05 21:47:34 +00003541 }
3542 sync=SyncCacheViewAuthenticPixels(morph_view,exception);
3543 if (sync == MagickFalse)
3544 status=MagickFalse;
3545 }
3546 morph_view=DestroyCacheView(morph_view);
3547 image_view=DestroyCacheView(image_view);
3548 morph_image=DestroyImage(morph_image);
3549 }
cristybb503372010-05-27 20:51:26 +00003550 if (i < (ssize_t) number_frames)
cristy3ed852e2009-09-05 21:47:34 +00003551 break;
3552 /*
3553 Clone last frame in sequence.
3554 */
3555 morph_image=CloneImage(GetNextImageInList(next),0,0,MagickTrue,exception);
3556 if (morph_image == (Image *) NULL)
3557 {
3558 morph_images=DestroyImageList(morph_images);
3559 return((Image *) NULL);
3560 }
3561 AppendImageToList(&morph_images,morph_image);
3562 morph_images=GetLastImageInList(morph_images);
3563 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3564 {
3565 MagickBooleanType
3566 proceed;
3567
cristyb5d5f722009-11-04 03:03:49 +00003568#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003569 #pragma omp critical (MagickCore_MorphImages)
cristy3ed852e2009-09-05 21:47:34 +00003570#endif
3571 proceed=SetImageProgress(image,MorphImageTag,scene,
3572 GetImageListLength(image));
3573 if (proceed == MagickFalse)
3574 status=MagickFalse;
3575 }
3576 scene++;
3577 }
3578 if (GetNextImageInList(next) != (Image *) NULL)
3579 {
3580 morph_images=DestroyImageList(morph_images);
3581 return((Image *) NULL);
3582 }
3583 return(GetFirstImageInList(morph_images));
3584}
3585
3586/*
3587%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3588% %
3589% %
3590% %
3591% P l a s m a I m a g e %
3592% %
3593% %
3594% %
3595%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3596%
3597% PlasmaImage() initializes an image with plasma fractal values. The image
3598% must be initialized with a base color and the random number generator
3599% seeded before this method is called.
3600%
3601% The format of the PlasmaImage method is:
3602%
3603% MagickBooleanType PlasmaImage(Image *image,const SegmentInfo *segment,
cristy5cbc0162011-08-29 00:36:28 +00003604% size_t attenuate,size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003605%
3606% A description of each parameter follows:
3607%
3608% o image: the image.
3609%
3610% o segment: Define the region to apply plasma fractals values.
3611%
glennrp7dae1ca2010-09-16 12:17:35 +00003612% o attenuate: Define the plasma attenuation factor.
cristy3ed852e2009-09-05 21:47:34 +00003613%
3614% o depth: Limit the plasma recursion depth.
3615%
cristy5cbc0162011-08-29 00:36:28 +00003616% o exception: return any errors or warnings in this structure.
3617%
cristy3ed852e2009-09-05 21:47:34 +00003618*/
3619
3620static inline Quantum PlasmaPixel(RandomInfo *random_info,
cristya19f1d72012-08-07 18:24:38 +00003621 const double pixel,const double noise)
cristy3ed852e2009-09-05 21:47:34 +00003622{
3623 Quantum
3624 plasma;
3625
cristyce70c172010-01-07 17:15:30 +00003626 plasma=ClampToQuantum(pixel+noise*GetPseudoRandomValue(random_info)-
cristy3ed852e2009-09-05 21:47:34 +00003627 noise/2.0);
3628 return(plasma);
3629}
3630
cristyda1f9c12011-10-02 21:39:49 +00003631static MagickBooleanType PlasmaImageProxy(Image *image,CacheView *image_view,
3632 CacheView *u_view,CacheView *v_view,RandomInfo *random_info,
3633 const SegmentInfo *segment,size_t attenuate,size_t depth,
3634 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003635{
cristya19f1d72012-08-07 18:24:38 +00003636 double
cristy3ed852e2009-09-05 21:47:34 +00003637 plasma;
3638
cristyda1f9c12011-10-02 21:39:49 +00003639 register const Quantum
3640 *restrict u,
3641 *restrict v;
3642
3643 register Quantum
3644 *restrict q;
3645
3646 register ssize_t
3647 i;
cristy3ed852e2009-09-05 21:47:34 +00003648
cristy9d314ff2011-03-09 01:30:28 +00003649 ssize_t
3650 x,
3651 x_mid,
3652 y,
3653 y_mid;
3654
cristy3ed852e2009-09-05 21:47:34 +00003655 if (((segment->x2-segment->x1) == 0.0) && ((segment->y2-segment->y1) == 0.0))
3656 return(MagickTrue);
3657 if (depth != 0)
3658 {
3659 SegmentInfo
3660 local_info;
3661
3662 /*
3663 Divide the area into quadrants and recurse.
3664 */
3665 depth--;
3666 attenuate++;
cristybb503372010-05-27 20:51:26 +00003667 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3668 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003669 local_info=(*segment);
3670 local_info.x2=(double) x_mid;
3671 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003672 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3673 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003674 local_info=(*segment);
3675 local_info.y1=(double) y_mid;
3676 local_info.x2=(double) x_mid;
cristyda1f9c12011-10-02 21:39:49 +00003677 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3678 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003679 local_info=(*segment);
3680 local_info.x1=(double) x_mid;
3681 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003682 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3683 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003684 local_info=(*segment);
3685 local_info.x1=(double) x_mid;
3686 local_info.y1=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003687 return(PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3688 &local_info,attenuate,depth,exception));
cristy3ed852e2009-09-05 21:47:34 +00003689 }
cristybb503372010-05-27 20:51:26 +00003690 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3691 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003692 if ((segment->x1 == (double) x_mid) && (segment->x2 == (double) x_mid) &&
3693 (segment->y1 == (double) y_mid) && (segment->y2 == (double) y_mid))
3694 return(MagickFalse);
3695 /*
3696 Average pixels and apply plasma.
3697 */
cristya19f1d72012-08-07 18:24:38 +00003698 plasma=(double) QuantumRange/(2.0*attenuate);
cristy3ed852e2009-09-05 21:47:34 +00003699 if ((segment->x1 != (double) x_mid) || (segment->x2 != (double) x_mid))
3700 {
cristy3ed852e2009-09-05 21:47:34 +00003701 /*
3702 Left pixel.
3703 */
cristybb503372010-05-27 20:51:26 +00003704 x=(ssize_t) ceil(segment->x1-0.5);
cristy1707c6c2012-01-18 23:30:54 +00003705 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),1,1,
3706 exception);
3707 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),1,1,
3708 exception);
cristyc5c6f662010-09-22 14:23:02 +00003709 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003710 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3711 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003712 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003713 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3714 {
cristy5a23c552013-02-13 14:34:28 +00003715 PixelChannel channel=GetPixelChannelChannel(image,i);
3716 PixelTrait traits=GetPixelChannelTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003717 if (traits == UndefinedPixelTrait)
3718 continue;
cristy6e564992013-05-24 01:21:04 +00003719 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
cristyda1f9c12011-10-02 21:39:49 +00003720 }
cristyc5c6f662010-09-22 14:23:02 +00003721 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003722 if (segment->x1 != segment->x2)
3723 {
3724 /*
3725 Right pixel.
3726 */
cristybb503372010-05-27 20:51:26 +00003727 x=(ssize_t) ceil(segment->x2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003728 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3729 1,1,exception);
3730 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3731 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003732 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003733 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3734 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003735 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003736 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3737 {
cristy5a23c552013-02-13 14:34:28 +00003738 PixelChannel channel=GetPixelChannelChannel(image,i);
3739 PixelTrait traits=GetPixelChannelTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003740 if (traits == UndefinedPixelTrait)
3741 continue;
cristy6e564992013-05-24 01:21:04 +00003742 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
cristyda1f9c12011-10-02 21:39:49 +00003743 }
cristyc5c6f662010-09-22 14:23:02 +00003744 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003745 }
3746 }
3747 if ((segment->y1 != (double) y_mid) || (segment->y2 != (double) y_mid))
3748 {
3749 if ((segment->x1 != (double) x_mid) || (segment->y2 != (double) y_mid))
3750 {
cristy3ed852e2009-09-05 21:47:34 +00003751 /*
3752 Bottom pixel.
3753 */
cristybb503372010-05-27 20:51:26 +00003754 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003755 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3756 1,1,exception);
3757 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3758 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003759 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003760 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3761 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003762 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003763 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3764 {
cristy5a23c552013-02-13 14:34:28 +00003765 PixelChannel channel=GetPixelChannelChannel(image,i);
3766 PixelTrait traits=GetPixelChannelTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003767 if (traits == UndefinedPixelTrait)
3768 continue;
cristy6e564992013-05-24 01:21:04 +00003769 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
cristyda1f9c12011-10-02 21:39:49 +00003770 }
cristyc5c6f662010-09-22 14:23:02 +00003771 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003772 }
3773 if (segment->y1 != segment->y2)
3774 {
cristy3ed852e2009-09-05 21:47:34 +00003775 /*
3776 Top pixel.
3777 */
cristybb503372010-05-27 20:51:26 +00003778 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003779 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3780 1,1,exception);
3781 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3782 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003783 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003784 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3785 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003786 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003787 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3788 {
cristy5a23c552013-02-13 14:34:28 +00003789 PixelChannel channel=GetPixelChannelChannel(image,i);
3790 PixelTrait traits=GetPixelChannelTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003791 if (traits == UndefinedPixelTrait)
3792 continue;
cristy6e564992013-05-24 01:21:04 +00003793 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
cristyda1f9c12011-10-02 21:39:49 +00003794 }
cristyc5c6f662010-09-22 14:23:02 +00003795 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003796 }
3797 }
3798 if ((segment->x1 != segment->x2) || (segment->y1 != segment->y2))
3799 {
cristy3ed852e2009-09-05 21:47:34 +00003800 /*
3801 Middle pixel.
3802 */
cristybb503372010-05-27 20:51:26 +00003803 x=(ssize_t) ceil(segment->x1-0.5);
3804 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003805 u=GetCacheViewVirtualPixels(u_view,x,y,1,1,exception);
cristybb503372010-05-27 20:51:26 +00003806 x=(ssize_t) ceil(segment->x2-0.5);
3807 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003808 v=GetCacheViewVirtualPixels(v_view,x,y,1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003809 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003810 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3811 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003812 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003813 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3814 {
cristy5a23c552013-02-13 14:34:28 +00003815 PixelChannel channel=GetPixelChannelChannel(image,i);
3816 PixelTrait traits=GetPixelChannelTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003817 if (traits == UndefinedPixelTrait)
3818 continue;
cristy6e564992013-05-24 01:21:04 +00003819 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
cristyda1f9c12011-10-02 21:39:49 +00003820 }
cristyc5c6f662010-09-22 14:23:02 +00003821 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003822 }
3823 if (((segment->x2-segment->x1) < 3.0) && ((segment->y2-segment->y1) < 3.0))
3824 return(MagickTrue);
3825 return(MagickFalse);
3826}
cristyda1f9c12011-10-02 21:39:49 +00003827
cristy3ed852e2009-09-05 21:47:34 +00003828MagickExport MagickBooleanType PlasmaImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00003829 const SegmentInfo *segment,size_t attenuate,size_t depth,
3830 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003831{
cristyc5c6f662010-09-22 14:23:02 +00003832 CacheView
cristyda1f9c12011-10-02 21:39:49 +00003833 *image_view,
3834 *u_view,
3835 *v_view;
cristyc5c6f662010-09-22 14:23:02 +00003836
cristy3ed852e2009-09-05 21:47:34 +00003837 MagickBooleanType
3838 status;
3839
3840 RandomInfo
3841 *random_info;
3842
3843 if (image->debug != MagickFalse)
3844 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3845 assert(image != (Image *) NULL);
3846 assert(image->signature == MagickSignature);
3847 if (image->debug != MagickFalse)
3848 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
cristy5cbc0162011-08-29 00:36:28 +00003849 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristyc5c6f662010-09-22 14:23:02 +00003850 return(MagickFalse);
cristy46ff2672012-12-14 15:32:26 +00003851 image_view=AcquireAuthenticCacheView(image,exception);
3852 u_view=AcquireVirtualCacheView(image,exception);
3853 v_view=AcquireVirtualCacheView(image,exception);
cristy3ed852e2009-09-05 21:47:34 +00003854 random_info=AcquireRandomInfo();
cristyda1f9c12011-10-02 21:39:49 +00003855 status=PlasmaImageProxy(image,image_view,u_view,v_view,random_info,segment,
3856 attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003857 random_info=DestroyRandomInfo(random_info);
cristyda1f9c12011-10-02 21:39:49 +00003858 v_view=DestroyCacheView(v_view);
3859 u_view=DestroyCacheView(u_view);
cristyc5c6f662010-09-22 14:23:02 +00003860 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003861 return(status);
3862}
3863
3864/*
3865%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3866% %
3867% %
3868% %
3869% P o l a r o i d I m a g e %
3870% %
3871% %
3872% %
3873%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3874%
3875% PolaroidImage() simulates a Polaroid picture.
3876%
3877% The format of the AnnotateImage method is:
3878%
3879% Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristye9e3d382011-12-14 01:50:13 +00003880% const char *caption,const double angle,
3881% const PixelInterpolateMethod method,ExceptionInfo exception)
cristy3ed852e2009-09-05 21:47:34 +00003882%
3883% A description of each parameter follows:
3884%
3885% o image: the image.
3886%
3887% o draw_info: the draw info.
3888%
cristye9e3d382011-12-14 01:50:13 +00003889% o caption: the Polaroid caption.
3890%
cristycee97112010-05-28 00:44:52 +00003891% o angle: Apply the effect along this angle.
cristy3ed852e2009-09-05 21:47:34 +00003892%
cristy5c4e2582011-09-11 19:21:03 +00003893% o method: the pixel interpolation method.
3894%
cristy3ed852e2009-09-05 21:47:34 +00003895% o exception: return any errors or warnings in this structure.
3896%
3897*/
3898MagickExport Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristye9e3d382011-12-14 01:50:13 +00003899 const char *caption,const double angle,const PixelInterpolateMethod method,
cristy5c4e2582011-09-11 19:21:03 +00003900 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003901{
cristy3ed852e2009-09-05 21:47:34 +00003902 Image
3903 *bend_image,
3904 *caption_image,
3905 *flop_image,
3906 *picture_image,
3907 *polaroid_image,
3908 *rotate_image,
3909 *trim_image;
3910
cristybb503372010-05-27 20:51:26 +00003911 size_t
cristy3ed852e2009-09-05 21:47:34 +00003912 height;
3913
cristy9d314ff2011-03-09 01:30:28 +00003914 ssize_t
3915 quantum;
3916
cristy3ed852e2009-09-05 21:47:34 +00003917 /*
3918 Simulate a Polaroid picture.
3919 */
3920 assert(image != (Image *) NULL);
3921 assert(image->signature == MagickSignature);
3922 if (image->debug != MagickFalse)
3923 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3924 assert(exception != (ExceptionInfo *) NULL);
3925 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00003926 quantum=(ssize_t) MagickMax(MagickMax((double) image->columns,(double)
cristy3ed852e2009-09-05 21:47:34 +00003927 image->rows)/25.0,10.0);
3928 height=image->rows+2*quantum;
3929 caption_image=(Image *) NULL;
cristye9e3d382011-12-14 01:50:13 +00003930 if (caption != (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003931 {
3932 char
cristye9e3d382011-12-14 01:50:13 +00003933 geometry[MaxTextExtent],
3934 *text;
cristy3ed852e2009-09-05 21:47:34 +00003935
3936 DrawInfo
3937 *annotate_info;
3938
cristy72e0b772013-04-28 14:29:45 +00003939 ImageInfo
3940 *image_info;
3941
cristy3ed852e2009-09-05 21:47:34 +00003942 MagickBooleanType
3943 status;
3944
cristy9d314ff2011-03-09 01:30:28 +00003945 ssize_t
3946 count;
3947
cristy3ed852e2009-09-05 21:47:34 +00003948 TypeMetric
3949 metrics;
3950
3951 /*
3952 Generate caption image.
3953 */
3954 caption_image=CloneImage(image,image->columns,1,MagickTrue,exception);
3955 if (caption_image == (Image *) NULL)
3956 return((Image *) NULL);
cristy72e0b772013-04-28 14:29:45 +00003957 image_info=AcquireImageInfo();
cristy3ed852e2009-09-05 21:47:34 +00003958 annotate_info=CloneDrawInfo((const ImageInfo *) NULL,draw_info);
cristy72e0b772013-04-28 14:29:45 +00003959 text=InterpretImageProperties(image_info,(Image *) image,caption,
cristye9e3d382011-12-14 01:50:13 +00003960 exception);
cristy72e0b772013-04-28 14:29:45 +00003961 image_info=DestroyImageInfo(image_info);
cristye9e3d382011-12-14 01:50:13 +00003962 (void) CloneString(&annotate_info->text,text);
cristy6b1d05e2010-09-22 19:17:27 +00003963 count=FormatMagickCaption(caption_image,annotate_info,MagickTrue,&metrics,
cristye9e3d382011-12-14 01:50:13 +00003964 &text,exception);
3965 status=SetImageExtent(caption_image,image->columns,(size_t) ((count+1)*
3966 (metrics.ascent-metrics.descent)+0.5),exception);
cristy3ed852e2009-09-05 21:47:34 +00003967 if (status == MagickFalse)
3968 caption_image=DestroyImage(caption_image);
3969 else
3970 {
3971 caption_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00003972 (void) SetImageBackgroundColor(caption_image,exception);
cristye9e3d382011-12-14 01:50:13 +00003973 (void) CloneString(&annotate_info->text,text);
cristyb51dff52011-05-19 16:55:47 +00003974 (void) FormatLocaleString(geometry,MaxTextExtent,"+0+%g",
cristy3ed852e2009-09-05 21:47:34 +00003975 metrics.ascent);
3976 if (annotate_info->gravity == UndefinedGravity)
3977 (void) CloneString(&annotate_info->geometry,AcquireString(
3978 geometry));
cristy5cbc0162011-08-29 00:36:28 +00003979 (void) AnnotateImage(caption_image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00003980 height+=caption_image->rows;
3981 }
3982 annotate_info=DestroyDrawInfo(annotate_info);
cristye9e3d382011-12-14 01:50:13 +00003983 text=DestroyString(text);
cristy3ed852e2009-09-05 21:47:34 +00003984 }
3985 picture_image=CloneImage(image,image->columns+2*quantum,height,MagickTrue,
3986 exception);
3987 if (picture_image == (Image *) NULL)
3988 {
3989 if (caption_image != (Image *) NULL)
3990 caption_image=DestroyImage(caption_image);
3991 return((Image *) NULL);
3992 }
3993 picture_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00003994 (void) SetImageBackgroundColor(picture_image,exception);
cristy39172402012-03-30 13:04:39 +00003995 (void) CompositeImage(picture_image,image,OverCompositeOp,MagickTrue,quantum,
cristyfeb3e962012-03-29 17:25:55 +00003996 quantum,exception);
cristy3ed852e2009-09-05 21:47:34 +00003997 if (caption_image != (Image *) NULL)
3998 {
cristyfeb3e962012-03-29 17:25:55 +00003999 (void) CompositeImage(picture_image,caption_image,OverCompositeOp,
cristy39172402012-03-30 13:04:39 +00004000 MagickTrue,quantum,(ssize_t) (image->rows+3*quantum/2),exception);
cristy3ed852e2009-09-05 21:47:34 +00004001 caption_image=DestroyImage(caption_image);
4002 }
cristy9950d572011-10-01 18:22:35 +00004003 (void) QueryColorCompliance("none",AllCompliance,
4004 &picture_image->background_color,exception);
cristy63240882011-08-05 19:05:27 +00004005 (void) SetImageAlphaChannel(picture_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004006 rotate_image=RotateImage(picture_image,90.0,exception);
4007 picture_image=DestroyImage(picture_image);
4008 if (rotate_image == (Image *) NULL)
4009 return((Image *) NULL);
4010 picture_image=rotate_image;
4011 bend_image=WaveImage(picture_image,0.01*picture_image->rows,2.0*
cristy5c4e2582011-09-11 19:21:03 +00004012 picture_image->columns,method,exception);
cristy3ed852e2009-09-05 21:47:34 +00004013 picture_image=DestroyImage(picture_image);
4014 if (bend_image == (Image *) NULL)
4015 return((Image *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00004016 picture_image=bend_image;
4017 rotate_image=RotateImage(picture_image,-90.0,exception);
4018 picture_image=DestroyImage(picture_image);
4019 if (rotate_image == (Image *) NULL)
4020 return((Image *) NULL);
4021 picture_image=rotate_image;
4022 picture_image->background_color=image->background_color;
anthonyf46d4262012-03-26 03:30:34 +00004023 polaroid_image=ShadowImage(picture_image,80.0,2.0,quantum/3,quantum/3,
cristy3ed852e2009-09-05 21:47:34 +00004024 exception);
4025 if (polaroid_image == (Image *) NULL)
4026 {
4027 picture_image=DestroyImage(picture_image);
4028 return(picture_image);
4029 }
4030 flop_image=FlopImage(polaroid_image,exception);
4031 polaroid_image=DestroyImage(polaroid_image);
4032 if (flop_image == (Image *) NULL)
4033 {
4034 picture_image=DestroyImage(picture_image);
4035 return(picture_image);
4036 }
4037 polaroid_image=flop_image;
cristyfeb3e962012-03-29 17:25:55 +00004038 (void) CompositeImage(polaroid_image,picture_image,OverCompositeOp,
cristy39172402012-03-30 13:04:39 +00004039 MagickTrue,(ssize_t) (-0.01*picture_image->columns/2.0),0L,exception);
cristy3ed852e2009-09-05 21:47:34 +00004040 picture_image=DestroyImage(picture_image);
cristy9950d572011-10-01 18:22:35 +00004041 (void) QueryColorCompliance("none",AllCompliance,
4042 &polaroid_image->background_color,exception);
cristy3ed852e2009-09-05 21:47:34 +00004043 rotate_image=RotateImage(polaroid_image,angle,exception);
4044 polaroid_image=DestroyImage(polaroid_image);
4045 if (rotate_image == (Image *) NULL)
4046 return((Image *) NULL);
4047 polaroid_image=rotate_image;
4048 trim_image=TrimImage(polaroid_image,exception);
4049 polaroid_image=DestroyImage(polaroid_image);
4050 if (trim_image == (Image *) NULL)
4051 return((Image *) NULL);
4052 polaroid_image=trim_image;
4053 return(polaroid_image);
4054}
4055
4056/*
4057%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4058% %
4059% %
4060% %
cristy3ed852e2009-09-05 21:47:34 +00004061% S e p i a T o n e I m a g e %
4062% %
4063% %
4064% %
4065%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4066%
4067% MagickSepiaToneImage() applies a special effect to the image, similar to the
4068% effect achieved in a photo darkroom by sepia toning. Threshold ranges from
4069% 0 to QuantumRange and is a measure of the extent of the sepia toning. A
4070% threshold of 80% is a good starting point for a reasonable tone.
4071%
4072% The format of the SepiaToneImage method is:
4073%
4074% Image *SepiaToneImage(const Image *image,const double threshold,
4075% ExceptionInfo *exception)
4076%
4077% A description of each parameter follows:
4078%
4079% o image: the image.
4080%
4081% o threshold: the tone threshold.
4082%
4083% o exception: return any errors or warnings in this structure.
4084%
4085*/
4086MagickExport Image *SepiaToneImage(const Image *image,const double threshold,
4087 ExceptionInfo *exception)
4088{
4089#define SepiaToneImageTag "SepiaTone/Image"
4090
cristyc4c8d132010-01-07 01:58:38 +00004091 CacheView
4092 *image_view,
4093 *sepia_view;
4094
cristy3ed852e2009-09-05 21:47:34 +00004095 Image
4096 *sepia_image;
4097
cristy3ed852e2009-09-05 21:47:34 +00004098 MagickBooleanType
4099 status;
4100
cristybb503372010-05-27 20:51:26 +00004101 MagickOffsetType
4102 progress;
4103
4104 ssize_t
4105 y;
4106
cristy3ed852e2009-09-05 21:47:34 +00004107 /*
4108 Initialize sepia-toned image attributes.
4109 */
4110 assert(image != (const Image *) NULL);
4111 assert(image->signature == MagickSignature);
4112 if (image->debug != MagickFalse)
4113 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4114 assert(exception != (ExceptionInfo *) NULL);
4115 assert(exception->signature == MagickSignature);
cristy1707c6c2012-01-18 23:30:54 +00004116 sepia_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004117 if (sepia_image == (Image *) NULL)
4118 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004119 if (SetImageStorageClass(sepia_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004120 {
cristy3ed852e2009-09-05 21:47:34 +00004121 sepia_image=DestroyImage(sepia_image);
4122 return((Image *) NULL);
4123 }
4124 /*
4125 Tone each row of the image.
4126 */
4127 status=MagickTrue;
4128 progress=0;
cristy46ff2672012-12-14 15:32:26 +00004129 image_view=AcquireVirtualCacheView(image,exception);
4130 sepia_view=AcquireAuthenticCacheView(sepia_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00004131#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00004132 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00004133 magick_threads(image,sepia_image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00004134#endif
cristybb503372010-05-27 20:51:26 +00004135 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004136 {
cristy4c08aed2011-07-01 19:47:50 +00004137 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004138 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00004139
cristybb503372010-05-27 20:51:26 +00004140 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004141 x;
4142
cristy4c08aed2011-07-01 19:47:50 +00004143 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004144 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004145
4146 if (status == MagickFalse)
4147 continue;
4148 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy1707c6c2012-01-18 23:30:54 +00004149 q=GetCacheViewAuthenticPixels(sepia_view,0,y,sepia_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00004150 exception);
cristy4c08aed2011-07-01 19:47:50 +00004151 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004152 {
4153 status=MagickFalse;
4154 continue;
4155 }
cristybb503372010-05-27 20:51:26 +00004156 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004157 {
cristya19f1d72012-08-07 18:24:38 +00004158 double
cristy3ed852e2009-09-05 21:47:34 +00004159 intensity,
4160 tone;
4161
cristyf13c5942012-08-08 23:50:11 +00004162 intensity=GetPixelIntensity(image,p);
cristya19f1d72012-08-07 18:24:38 +00004163 tone=intensity > threshold ? (double) QuantumRange : intensity+
4164 (double) QuantumRange-threshold;
cristy4c08aed2011-07-01 19:47:50 +00004165 SetPixelRed(sepia_image,ClampToQuantum(tone),q);
cristya19f1d72012-08-07 18:24:38 +00004166 tone=intensity > (7.0*threshold/6.0) ? (double) QuantumRange :
4167 intensity+(double) QuantumRange-7.0*threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004168 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004169 tone=intensity < (threshold/6.0) ? 0 : intensity-threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004170 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004171 tone=threshold/7.0;
cristya19f1d72012-08-07 18:24:38 +00004172 if ((double) GetPixelGreen(image,q) < tone)
cristy4c08aed2011-07-01 19:47:50 +00004173 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
cristya19f1d72012-08-07 18:24:38 +00004174 if ((double) GetPixelBlue(image,q) < tone)
cristy4c08aed2011-07-01 19:47:50 +00004175 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristyc8a5f0e2014-04-04 11:36:37 +00004176 SetPixelAlpha(sepia_image,GetPixelAlpha(image,p),q);
cristyed231572011-07-14 02:18:59 +00004177 p+=GetPixelChannels(image);
4178 q+=GetPixelChannels(sepia_image);
cristy3ed852e2009-09-05 21:47:34 +00004179 }
4180 if (SyncCacheViewAuthenticPixels(sepia_view,exception) == MagickFalse)
4181 status=MagickFalse;
4182 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4183 {
4184 MagickBooleanType
4185 proceed;
4186
cristyb5d5f722009-11-04 03:03:49 +00004187#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00004188 #pragma omp critical (MagickCore_SepiaToneImage)
cristy3ed852e2009-09-05 21:47:34 +00004189#endif
4190 proceed=SetImageProgress(image,SepiaToneImageTag,progress++,
4191 image->rows);
4192 if (proceed == MagickFalse)
4193 status=MagickFalse;
4194 }
4195 }
4196 sepia_view=DestroyCacheView(sepia_view);
4197 image_view=DestroyCacheView(image_view);
cristye23ec9d2011-08-16 18:15:40 +00004198 (void) NormalizeImage(sepia_image,exception);
4199 (void) ContrastImage(sepia_image,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004200 if (status == MagickFalse)
4201 sepia_image=DestroyImage(sepia_image);
4202 return(sepia_image);
4203}
4204
4205/*
4206%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4207% %
4208% %
4209% %
4210% S h a d o w I m a g e %
4211% %
4212% %
4213% %
4214%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4215%
4216% ShadowImage() simulates a shadow from the specified image and returns it.
4217%
4218% The format of the ShadowImage method is:
4219%
cristy70cddf72011-12-10 22:42:42 +00004220% Image *ShadowImage(const Image *image,const double alpha,
cristyaa2c16c2012-03-25 22:21:35 +00004221% const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4222% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004223%
4224% A description of each parameter follows:
4225%
4226% o image: the image.
4227%
cristy70cddf72011-12-10 22:42:42 +00004228% o alpha: percentage transparency.
cristy3ed852e2009-09-05 21:47:34 +00004229%
4230% o sigma: the standard deviation of the Gaussian, in pixels.
4231%
4232% o x_offset: the shadow x-offset.
4233%
4234% o y_offset: the shadow y-offset.
4235%
4236% o exception: return any errors or warnings in this structure.
4237%
4238*/
cristy70cddf72011-12-10 22:42:42 +00004239MagickExport Image *ShadowImage(const Image *image,const double alpha,
cristyaa2c16c2012-03-25 22:21:35 +00004240 const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4241 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004242{
4243#define ShadowImageTag "Shadow/Image"
4244
cristy70cddf72011-12-10 22:42:42 +00004245 CacheView
4246 *image_view;
4247
cristybd5a96c2011-08-21 00:04:26 +00004248 ChannelType
4249 channel_mask;
4250
cristy3ed852e2009-09-05 21:47:34 +00004251 Image
4252 *border_image,
4253 *clone_image,
4254 *shadow_image;
4255
cristy70cddf72011-12-10 22:42:42 +00004256 MagickBooleanType
4257 status;
4258
cristy3ed852e2009-09-05 21:47:34 +00004259 RectangleInfo
4260 border_info;
4261
cristy70cddf72011-12-10 22:42:42 +00004262 ssize_t
4263 y;
4264
cristy3ed852e2009-09-05 21:47:34 +00004265 assert(image != (Image *) NULL);
4266 assert(image->signature == MagickSignature);
4267 if (image->debug != MagickFalse)
4268 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4269 assert(exception != (ExceptionInfo *) NULL);
4270 assert(exception->signature == MagickSignature);
4271 clone_image=CloneImage(image,0,0,MagickTrue,exception);
4272 if (clone_image == (Image *) NULL)
4273 return((Image *) NULL);
cristya6400b12013-03-15 12:20:18 +00004274 if (IsGrayColorspace(image->colorspace) != MagickFalse)
cristy0c81d062013-04-21 15:22:02 +00004275 (void) SetImageColorspace(clone_image,sRGBColorspace,exception);
cristy0ce08762012-06-30 01:33:18 +00004276 (void) SetImageVirtualPixelMethod(clone_image,TransparentVirtualPixelMethod,
cristy387430f2012-02-07 13:09:46 +00004277 exception);
cristybb503372010-05-27 20:51:26 +00004278 border_info.width=(size_t) floor(2.0*sigma+0.5);
4279 border_info.height=(size_t) floor(2.0*sigma+0.5);
cristy3ed852e2009-09-05 21:47:34 +00004280 border_info.x=0;
4281 border_info.y=0;
cristy9950d572011-10-01 18:22:35 +00004282 (void) QueryColorCompliance("none",AllCompliance,&clone_image->border_color,
4283 exception);
cristy8a46d822012-08-28 23:32:39 +00004284 clone_image->alpha_trait=BlendPixelTrait;
cristy70cddf72011-12-10 22:42:42 +00004285 border_image=BorderImage(clone_image,&border_info,OverCompositeOp,exception);
cristy3ed852e2009-09-05 21:47:34 +00004286 clone_image=DestroyImage(clone_image);
4287 if (border_image == (Image *) NULL)
4288 return((Image *) NULL);
cristy8a46d822012-08-28 23:32:39 +00004289 if (border_image->alpha_trait != BlendPixelTrait)
cristy63240882011-08-05 19:05:27 +00004290 (void) SetImageAlphaChannel(border_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004291 /*
4292 Shadow image.
4293 */
cristy70cddf72011-12-10 22:42:42 +00004294 status=MagickTrue;
cristy46ff2672012-12-14 15:32:26 +00004295 image_view=AcquireAuthenticCacheView(border_image,exception);
cristy70cddf72011-12-10 22:42:42 +00004296 for (y=0; y < (ssize_t) border_image->rows; y++)
4297 {
4298 PixelInfo
4299 background_color;
4300
4301 register Quantum
4302 *restrict q;
4303
4304 register ssize_t
4305 x;
4306
4307 if (status == MagickFalse)
4308 continue;
4309 q=QueueCacheViewAuthenticPixels(image_view,0,y,border_image->columns,1,
4310 exception);
4311 if (q == (Quantum *) NULL)
4312 {
4313 status=MagickFalse;
4314 continue;
4315 }
4316 background_color=border_image->background_color;
cristy8a46d822012-08-28 23:32:39 +00004317 background_color.alpha_trait=BlendPixelTrait;
cristy70cddf72011-12-10 22:42:42 +00004318 for (x=0; x < (ssize_t) border_image->columns; x++)
4319 {
cristy8a46d822012-08-28 23:32:39 +00004320 if (border_image->alpha_trait == BlendPixelTrait)
cristy70cddf72011-12-10 22:42:42 +00004321 background_color.alpha=GetPixelAlpha(border_image,q)*alpha/100.0;
4322 SetPixelInfoPixel(border_image,&background_color,q);
4323 q+=GetPixelChannels(border_image);
4324 }
4325 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4326 status=MagickFalse;
4327 }
4328 image_view=DestroyCacheView(image_view);
4329 if (status == MagickFalse)
4330 {
4331 border_image=DestroyImage(border_image);
4332 return((Image *) NULL);
4333 }
cristycf1296e2012-08-26 23:40:49 +00004334 channel_mask=SetImageChannelMask(border_image,AlphaChannel);
cristyaa2c16c2012-03-25 22:21:35 +00004335 shadow_image=BlurImage(border_image,0.0,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +00004336 border_image=DestroyImage(border_image);
4337 if (shadow_image == (Image *) NULL)
4338 return((Image *) NULL);
cristycf1296e2012-08-26 23:40:49 +00004339 (void) SetPixelChannelMask(shadow_image,channel_mask);
cristy3ed852e2009-09-05 21:47:34 +00004340 if (shadow_image->page.width == 0)
4341 shadow_image->page.width=shadow_image->columns;
4342 if (shadow_image->page.height == 0)
4343 shadow_image->page.height=shadow_image->rows;
cristybb503372010-05-27 20:51:26 +00004344 shadow_image->page.width+=x_offset-(ssize_t) border_info.width;
4345 shadow_image->page.height+=y_offset-(ssize_t) border_info.height;
4346 shadow_image->page.x+=x_offset-(ssize_t) border_info.width;
4347 shadow_image->page.y+=y_offset-(ssize_t) border_info.height;
cristy3ed852e2009-09-05 21:47:34 +00004348 return(shadow_image);
4349}
4350
4351/*
4352%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4353% %
4354% %
4355% %
4356% S k e t c h I m a g e %
4357% %
4358% %
4359% %
4360%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4361%
4362% SketchImage() simulates a pencil sketch. We convolve the image with a
4363% Gaussian operator of the given radius and standard deviation (sigma). For
4364% reasonable results, radius should be larger than sigma. Use a radius of 0
4365% and SketchImage() selects a suitable radius for you. Angle gives the angle
4366% of the sketch.
4367%
4368% The format of the SketchImage method is:
4369%
4370% Image *SketchImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00004371% const double sigma,const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004372%
4373% A description of each parameter follows:
4374%
4375% o image: the image.
4376%
cristy574cc262011-08-05 01:23:58 +00004377% o radius: the radius of the Gaussian, in pixels, not counting the
4378% center pixel.
cristy3ed852e2009-09-05 21:47:34 +00004379%
4380% o sigma: the standard deviation of the Gaussian, in pixels.
4381%
cristyf7ef0252011-09-09 14:50:06 +00004382% o angle: apply the effect along this angle.
4383%
cristy3ed852e2009-09-05 21:47:34 +00004384% o exception: return any errors or warnings in this structure.
4385%
4386*/
4387MagickExport Image *SketchImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00004388 const double sigma,const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004389{
cristyfa112112010-01-04 17:48:07 +00004390 CacheView
4391 *random_view;
4392
cristy3ed852e2009-09-05 21:47:34 +00004393 Image
4394 *blend_image,
4395 *blur_image,
4396 *dodge_image,
4397 *random_image,
4398 *sketch_image;
4399
cristy3ed852e2009-09-05 21:47:34 +00004400 MagickBooleanType
4401 status;
4402
cristy3ed852e2009-09-05 21:47:34 +00004403 RandomInfo
cristyfa112112010-01-04 17:48:07 +00004404 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00004405
cristy9d314ff2011-03-09 01:30:28 +00004406 ssize_t
4407 y;
4408
glennrpf7659d72012-09-24 18:14:56 +00004409#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy57340e02012-05-05 00:53:23 +00004410 unsigned long
4411 key;
glennrpf7659d72012-09-24 18:14:56 +00004412#endif
cristy57340e02012-05-05 00:53:23 +00004413
cristy3ed852e2009-09-05 21:47:34 +00004414 /*
4415 Sketch image.
4416 */
4417 random_image=CloneImage(image,image->columns << 1,image->rows << 1,
4418 MagickTrue,exception);
4419 if (random_image == (Image *) NULL)
4420 return((Image *) NULL);
4421 status=MagickTrue;
cristy1b784432009-12-19 02:20:40 +00004422 random_info=AcquireRandomInfoThreadSet();
glennrpf7659d72012-09-24 18:14:56 +00004423#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy57340e02012-05-05 00:53:23 +00004424 key=GetRandomSecretKey(random_info[0]);
glennrpf7659d72012-09-24 18:14:56 +00004425#endif
cristy46ff2672012-12-14 15:32:26 +00004426 random_view=AcquireAuthenticCacheView(random_image,exception);
cristy1b784432009-12-19 02:20:40 +00004427#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy57340e02012-05-05 00:53:23 +00004428 #pragma omp parallel for schedule(static,4) shared(status) \
cristy5e6b2592012-12-19 14:08:11 +00004429 magick_threads(random_image,random_image,random_image->rows,key == ~0UL)
cristy1b784432009-12-19 02:20:40 +00004430#endif
cristybb503372010-05-27 20:51:26 +00004431 for (y=0; y < (ssize_t) random_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004432 {
cristy5c9e6f22010-09-17 17:31:01 +00004433 const int
4434 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00004435
cristybb503372010-05-27 20:51:26 +00004436 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004437 x;
4438
cristy4c08aed2011-07-01 19:47:50 +00004439 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004440 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004441
cristy1b784432009-12-19 02:20:40 +00004442 if (status == MagickFalse)
4443 continue;
cristy3ed852e2009-09-05 21:47:34 +00004444 q=QueueCacheViewAuthenticPixels(random_view,0,y,random_image->columns,1,
4445 exception);
cristyacd2ed22011-08-30 01:44:23 +00004446 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004447 {
4448 status=MagickFalse;
4449 continue;
4450 }
cristybb503372010-05-27 20:51:26 +00004451 for (x=0; x < (ssize_t) random_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004452 {
cristya19f1d72012-08-07 18:24:38 +00004453 double
cristy76f512e2011-09-12 01:26:56 +00004454 value;
4455
4456 register ssize_t
4457 i;
4458
cristy883fde12013-04-08 00:50:13 +00004459 if (GetPixelReadMask(random_image,q) == 0)
cristy10a6c612012-01-29 21:41:05 +00004460 {
4461 q+=GetPixelChannels(random_image);
4462 continue;
4463 }
cristy76f512e2011-09-12 01:26:56 +00004464 value=GetPseudoRandomValue(random_info[id]);
4465 for (i=0; i < (ssize_t) GetPixelChannels(random_image); i++)
4466 {
cristy5a23c552013-02-13 14:34:28 +00004467 PixelChannel channel=GetPixelChannelChannel(image,i);
4468 PixelTrait traits=GetPixelChannelTraits(image,channel);
cristy76f512e2011-09-12 01:26:56 +00004469 if (traits == UndefinedPixelTrait)
4470 continue;
4471 q[i]=ClampToQuantum(QuantumRange*value);
4472 }
cristyed231572011-07-14 02:18:59 +00004473 q+=GetPixelChannels(random_image);
cristy3ed852e2009-09-05 21:47:34 +00004474 }
4475 if (SyncCacheViewAuthenticPixels(random_view,exception) == MagickFalse)
4476 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004477 }
4478 random_view=DestroyCacheView(random_view);
cristy1b784432009-12-19 02:20:40 +00004479 random_info=DestroyRandomInfoThreadSet(random_info);
cristy3ed852e2009-09-05 21:47:34 +00004480 if (status == MagickFalse)
4481 {
4482 random_image=DestroyImage(random_image);
4483 return(random_image);
4484 }
cristyaa2c16c2012-03-25 22:21:35 +00004485 blur_image=MotionBlurImage(random_image,radius,sigma,angle,exception);
cristy3ed852e2009-09-05 21:47:34 +00004486 random_image=DestroyImage(random_image);
4487 if (blur_image == (Image *) NULL)
4488 return((Image *) NULL);
cristy800446a2013-05-20 20:15:38 +00004489 dodge_image=EdgeImage(blur_image,radius,exception);
cristy3ed852e2009-09-05 21:47:34 +00004490 blur_image=DestroyImage(blur_image);
4491 if (dodge_image == (Image *) NULL)
4492 return((Image *) NULL);
cristye23ec9d2011-08-16 18:15:40 +00004493 (void) NormalizeImage(dodge_image,exception);
cristyb3e7c6c2011-07-24 01:43:55 +00004494 (void) NegateImage(dodge_image,MagickFalse,exception);
cristye941a752011-10-15 01:52:48 +00004495 (void) TransformImage(&dodge_image,(char *) NULL,"50%",exception);
cristy3ed852e2009-09-05 21:47:34 +00004496 sketch_image=CloneImage(image,0,0,MagickTrue,exception);
4497 if (sketch_image == (Image *) NULL)
4498 {
4499 dodge_image=DestroyImage(dodge_image);
4500 return((Image *) NULL);
4501 }
cristyfeb3e962012-03-29 17:25:55 +00004502 (void) CompositeImage(sketch_image,dodge_image,ColorDodgeCompositeOp,
cristy39172402012-03-30 13:04:39 +00004503 MagickTrue,0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004504 dodge_image=DestroyImage(dodge_image);
4505 blend_image=CloneImage(image,0,0,MagickTrue,exception);
4506 if (blend_image == (Image *) NULL)
4507 {
4508 sketch_image=DestroyImage(sketch_image);
4509 return((Image *) NULL);
4510 }
4511 (void) SetImageArtifact(blend_image,"compose:args","20x80");
cristy39172402012-03-30 13:04:39 +00004512 (void) CompositeImage(sketch_image,blend_image,BlendCompositeOp,MagickTrue,
cristyfeb3e962012-03-29 17:25:55 +00004513 0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004514 blend_image=DestroyImage(blend_image);
4515 return(sketch_image);
4516}
4517
4518/*
4519%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4520% %
4521% %
4522% %
4523% S o l a r i z e I m a g e %
4524% %
4525% %
4526% %
4527%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4528%
4529% SolarizeImage() applies a special effect to the image, similar to the effect
4530% achieved in a photo darkroom by selectively exposing areas of photo
4531% sensitive paper to light. Threshold ranges from 0 to QuantumRange and is a
4532% measure of the extent of the solarization.
4533%
4534% The format of the SolarizeImage method is:
4535%
cristy5cbc0162011-08-29 00:36:28 +00004536% MagickBooleanType SolarizeImage(Image *image,const double threshold,
4537% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004538%
4539% A description of each parameter follows:
4540%
4541% o image: the image.
4542%
4543% o threshold: Define the extent of the solarization.
4544%
cristy5cbc0162011-08-29 00:36:28 +00004545% o exception: return any errors or warnings in this structure.
4546%
cristy3ed852e2009-09-05 21:47:34 +00004547*/
4548MagickExport MagickBooleanType SolarizeImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00004549 const double threshold,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004550{
4551#define SolarizeImageTag "Solarize/Image"
4552
cristyc4c8d132010-01-07 01:58:38 +00004553 CacheView
4554 *image_view;
4555
cristy3ed852e2009-09-05 21:47:34 +00004556 MagickBooleanType
4557 status;
4558
cristybb503372010-05-27 20:51:26 +00004559 MagickOffsetType
4560 progress;
4561
4562 ssize_t
4563 y;
4564
cristy3ed852e2009-09-05 21:47:34 +00004565 assert(image != (Image *) NULL);
4566 assert(image->signature == MagickSignature);
4567 if (image->debug != MagickFalse)
4568 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristya6400b12013-03-15 12:20:18 +00004569 if (IsGrayColorspace(image->colorspace) != MagickFalse)
cristy0c81d062013-04-21 15:22:02 +00004570 (void) SetImageColorspace(image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00004571 if (image->storage_class == PseudoClass)
4572 {
cristybb503372010-05-27 20:51:26 +00004573 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004574 i;
4575
4576 /*
4577 Solarize colormap.
4578 */
cristybb503372010-05-27 20:51:26 +00004579 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00004580 {
cristya19f1d72012-08-07 18:24:38 +00004581 if ((double) image->colormap[i].red > threshold)
cristy6e963d82012-06-19 15:23:24 +00004582 image->colormap[i].red=QuantumRange-image->colormap[i].red;
cristya19f1d72012-08-07 18:24:38 +00004583 if ((double) image->colormap[i].green > threshold)
cristy13ceedd2013-12-02 17:57:23 +00004584 image->colormap[i].green=QuantumRange-image->colormap[i].green;
cristya19f1d72012-08-07 18:24:38 +00004585 if ((double) image->colormap[i].blue > threshold)
cristy13ceedd2013-12-02 17:57:23 +00004586 image->colormap[i].blue=QuantumRange-image->colormap[i].blue;
cristy3ed852e2009-09-05 21:47:34 +00004587 }
4588 }
4589 /*
4590 Solarize image.
4591 */
4592 status=MagickTrue;
4593 progress=0;
cristy46ff2672012-12-14 15:32:26 +00004594 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00004595#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00004596 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00004597 magick_threads(image,image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00004598#endif
cristybb503372010-05-27 20:51:26 +00004599 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004600 {
cristybb503372010-05-27 20:51:26 +00004601 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004602 x;
4603
cristy4c08aed2011-07-01 19:47:50 +00004604 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004605 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004606
4607 if (status == MagickFalse)
4608 continue;
cristy5cbc0162011-08-29 00:36:28 +00004609 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00004610 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004611 {
4612 status=MagickFalse;
4613 continue;
4614 }
cristybb503372010-05-27 20:51:26 +00004615 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004616 {
cristy76f512e2011-09-12 01:26:56 +00004617 register ssize_t
4618 i;
4619
cristy883fde12013-04-08 00:50:13 +00004620 if (GetPixelReadMask(image,q) == 0)
cristy10a6c612012-01-29 21:41:05 +00004621 {
4622 q+=GetPixelChannels(image);
4623 continue;
4624 }
cristy76f512e2011-09-12 01:26:56 +00004625 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4626 {
cristy5a23c552013-02-13 14:34:28 +00004627 PixelChannel channel=GetPixelChannelChannel(image,i);
4628 PixelTrait traits=GetPixelChannelTraits(image,channel);
cristy76f512e2011-09-12 01:26:56 +00004629 if ((traits == UndefinedPixelTrait) ||
4630 ((traits & CopyPixelTrait) != 0))
4631 continue;
cristya19f1d72012-08-07 18:24:38 +00004632 if ((double) q[i] > threshold)
cristy76f512e2011-09-12 01:26:56 +00004633 q[i]=QuantumRange-q[i];
4634 }
cristyed231572011-07-14 02:18:59 +00004635 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00004636 }
4637 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4638 status=MagickFalse;
4639 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4640 {
4641 MagickBooleanType
4642 proceed;
4643
cristyb5d5f722009-11-04 03:03:49 +00004644#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00004645 #pragma omp critical (MagickCore_SolarizeImage)
cristy3ed852e2009-09-05 21:47:34 +00004646#endif
4647 proceed=SetImageProgress(image,SolarizeImageTag,progress++,image->rows);
4648 if (proceed == MagickFalse)
4649 status=MagickFalse;
4650 }
4651 }
4652 image_view=DestroyCacheView(image_view);
4653 return(status);
4654}
4655
4656/*
4657%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4658% %
4659% %
4660% %
4661% S t e g a n o I m a g e %
4662% %
4663% %
4664% %
4665%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4666%
4667% SteganoImage() hides a digital watermark within the image. Recover
4668% the hidden watermark later to prove that the authenticity of an image.
4669% Offset defines the start position within the image to hide the watermark.
4670%
4671% The format of the SteganoImage method is:
4672%
4673% Image *SteganoImage(const Image *image,Image *watermark,
4674% ExceptionInfo *exception)
4675%
4676% A description of each parameter follows:
4677%
4678% o image: the image.
4679%
4680% o watermark: the watermark image.
4681%
4682% o exception: return any errors or warnings in this structure.
4683%
4684*/
4685MagickExport Image *SteganoImage(const Image *image,const Image *watermark,
4686 ExceptionInfo *exception)
4687{
cristye1bf8ad2010-09-19 17:07:03 +00004688#define GetBit(alpha,i) ((((size_t) (alpha) >> (size_t) (i)) & 0x01) != 0)
cristy4c08aed2011-07-01 19:47:50 +00004689#define SetBit(alpha,i,set) (Quantum) ((set) != 0 ? (size_t) (alpha) \
cristyeaedf062010-05-29 22:36:02 +00004690 | (one << (size_t) (i)) : (size_t) (alpha) & ~(one << (size_t) (i)))
cristy3ed852e2009-09-05 21:47:34 +00004691#define SteganoImageTag "Stegano/Image"
4692
cristyb0d3bb92010-09-22 14:37:58 +00004693 CacheView
4694 *stegano_view,
4695 *watermark_view;
4696
cristy3ed852e2009-09-05 21:47:34 +00004697 Image
4698 *stegano_image;
4699
4700 int
4701 c;
4702
cristy3ed852e2009-09-05 21:47:34 +00004703 MagickBooleanType
4704 status;
4705
cristy101ab702011-10-13 13:06:32 +00004706 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00004707 pixel;
4708
cristy4c08aed2011-07-01 19:47:50 +00004709 register Quantum
cristy3ed852e2009-09-05 21:47:34 +00004710 *q;
4711
cristye1bf8ad2010-09-19 17:07:03 +00004712 register ssize_t
4713 x;
4714
cristybb503372010-05-27 20:51:26 +00004715 size_t
cristyeaedf062010-05-29 22:36:02 +00004716 depth,
4717 one;
cristy3ed852e2009-09-05 21:47:34 +00004718
cristye1bf8ad2010-09-19 17:07:03 +00004719 ssize_t
4720 i,
4721 j,
4722 k,
4723 y;
4724
cristy3ed852e2009-09-05 21:47:34 +00004725 /*
4726 Initialize steganographic image attributes.
4727 */
4728 assert(image != (const Image *) NULL);
4729 assert(image->signature == MagickSignature);
4730 if (image->debug != MagickFalse)
4731 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4732 assert(watermark != (const Image *) NULL);
4733 assert(watermark->signature == MagickSignature);
4734 assert(exception != (ExceptionInfo *) NULL);
4735 assert(exception->signature == MagickSignature);
cristyeaedf062010-05-29 22:36:02 +00004736 one=1UL;
cristy3ed852e2009-09-05 21:47:34 +00004737 stegano_image=CloneImage(image,0,0,MagickTrue,exception);
4738 if (stegano_image == (Image *) NULL)
4739 return((Image *) NULL);
cristyf61b1832012-04-01 01:38:19 +00004740 stegano_image->depth=MAGICKCORE_QUANTUM_DEPTH;
cristy574cc262011-08-05 01:23:58 +00004741 if (SetImageStorageClass(stegano_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004742 {
cristy3ed852e2009-09-05 21:47:34 +00004743 stegano_image=DestroyImage(stegano_image);
4744 return((Image *) NULL);
4745 }
cristy3ed852e2009-09-05 21:47:34 +00004746 /*
4747 Hide watermark in low-order bits of image.
4748 */
4749 c=0;
4750 i=0;
4751 j=0;
4752 depth=stegano_image->depth;
cristyf61b1832012-04-01 01:38:19 +00004753 k=stegano_image->offset;
cristyda16f162011-02-19 23:52:17 +00004754 status=MagickTrue;
cristy46ff2672012-12-14 15:32:26 +00004755 watermark_view=AcquireVirtualCacheView(watermark,exception);
4756 stegano_view=AcquireAuthenticCacheView(stegano_image,exception);
cristybb503372010-05-27 20:51:26 +00004757 for (i=(ssize_t) depth-1; (i >= 0) && (j < (ssize_t) depth); i--)
cristy3ed852e2009-09-05 21:47:34 +00004758 {
cristybb503372010-05-27 20:51:26 +00004759 for (y=0; (y < (ssize_t) watermark->rows) && (j < (ssize_t) depth); y++)
cristy3ed852e2009-09-05 21:47:34 +00004760 {
cristybb503372010-05-27 20:51:26 +00004761 for (x=0; (x < (ssize_t) watermark->columns) && (j < (ssize_t) depth); x++)
cristy3ed852e2009-09-05 21:47:34 +00004762 {
cristy1707c6c2012-01-18 23:30:54 +00004763 ssize_t
4764 offset;
4765
cristyf05d4942012-03-17 16:26:09 +00004766 (void) GetOneCacheViewVirtualPixelInfo(watermark_view,x,y,&pixel,
cristyda1f9c12011-10-02 21:39:49 +00004767 exception);
cristy1707c6c2012-01-18 23:30:54 +00004768 offset=k/(ssize_t) stegano_image->columns;
4769 if (offset >= (ssize_t) stegano_image->rows)
cristy3ed852e2009-09-05 21:47:34 +00004770 break;
cristyb0d3bb92010-09-22 14:37:58 +00004771 q=GetCacheViewAuthenticPixels(stegano_view,k % (ssize_t)
4772 stegano_image->columns,k/(ssize_t) stegano_image->columns,1,1,
4773 exception);
cristyacd2ed22011-08-30 01:44:23 +00004774 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004775 break;
4776 switch (c)
4777 {
4778 case 0:
4779 {
cristyf61b1832012-04-01 01:38:19 +00004780 SetPixelRed(stegano_image,SetBit(GetPixelRed(stegano_image,q),j,
4781 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004782 break;
4783 }
4784 case 1:
4785 {
cristyf61b1832012-04-01 01:38:19 +00004786 SetPixelGreen(stegano_image,SetBit(GetPixelGreen(stegano_image,q),j,
4787 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004788 break;
4789 }
4790 case 2:
4791 {
cristyf61b1832012-04-01 01:38:19 +00004792 SetPixelBlue(stegano_image,SetBit(GetPixelBlue(stegano_image,q),j,
4793 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004794 break;
4795 }
4796 }
cristyb0d3bb92010-09-22 14:37:58 +00004797 if (SyncCacheViewAuthenticPixels(stegano_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004798 break;
4799 c++;
4800 if (c == 3)
4801 c=0;
4802 k++;
cristybb503372010-05-27 20:51:26 +00004803 if (k == (ssize_t) (stegano_image->columns*stegano_image->columns))
cristy3ed852e2009-09-05 21:47:34 +00004804 k=0;
cristyf61b1832012-04-01 01:38:19 +00004805 if (k == stegano_image->offset)
cristy3ed852e2009-09-05 21:47:34 +00004806 j++;
4807 }
4808 }
cristy8b27a6d2010-02-14 03:31:15 +00004809 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004810 {
cristy8b27a6d2010-02-14 03:31:15 +00004811 MagickBooleanType
4812 proceed;
4813
4814 proceed=SetImageProgress(image,SteganoImageTag,(MagickOffsetType)
4815 (depth-i),depth);
4816 if (proceed == MagickFalse)
4817 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004818 }
4819 }
cristyb0d3bb92010-09-22 14:37:58 +00004820 stegano_view=DestroyCacheView(stegano_view);
4821 watermark_view=DestroyCacheView(watermark_view);
cristyda16f162011-02-19 23:52:17 +00004822 if (status == MagickFalse)
cristyfab83642012-12-14 00:31:38 +00004823 stegano_image=DestroyImage(stegano_image);
cristy3ed852e2009-09-05 21:47:34 +00004824 return(stegano_image);
4825}
4826
4827/*
4828%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4829% %
4830% %
4831% %
4832% S t e r e o A n a g l y p h I m a g e %
4833% %
4834% %
4835% %
4836%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4837%
4838% StereoAnaglyphImage() combines two images and produces a single image that
4839% is the composite of a left and right image of a stereo pair. Special
4840% red-green stereo glasses are required to view this effect.
4841%
4842% The format of the StereoAnaglyphImage method is:
4843%
4844% Image *StereoImage(const Image *left_image,const Image *right_image,
4845% ExceptionInfo *exception)
4846% Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004847% const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004848% ExceptionInfo *exception)
4849%
4850% A description of each parameter follows:
4851%
4852% o left_image: the left image.
4853%
4854% o right_image: the right image.
4855%
4856% o exception: return any errors or warnings in this structure.
4857%
4858% o x_offset: amount, in pixels, by which the left image is offset to the
4859% right of the right image.
4860%
4861% o y_offset: amount, in pixels, by which the left image is offset to the
4862% bottom of the right image.
4863%
4864%
4865*/
4866MagickExport Image *StereoImage(const Image *left_image,
4867 const Image *right_image,ExceptionInfo *exception)
4868{
4869 return(StereoAnaglyphImage(left_image,right_image,0,0,exception));
4870}
4871
4872MagickExport Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004873 const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004874 ExceptionInfo *exception)
4875{
4876#define StereoImageTag "Stereo/Image"
4877
4878 const Image
4879 *image;
4880
4881 Image
4882 *stereo_image;
4883
cristy3ed852e2009-09-05 21:47:34 +00004884 MagickBooleanType
4885 status;
4886
cristy9d314ff2011-03-09 01:30:28 +00004887 ssize_t
4888 y;
4889
cristy3ed852e2009-09-05 21:47:34 +00004890 assert(left_image != (const Image *) NULL);
4891 assert(left_image->signature == MagickSignature);
4892 if (left_image->debug != MagickFalse)
4893 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4894 left_image->filename);
4895 assert(right_image != (const Image *) NULL);
4896 assert(right_image->signature == MagickSignature);
4897 assert(exception != (ExceptionInfo *) NULL);
4898 assert(exception->signature == MagickSignature);
4899 assert(right_image != (const Image *) NULL);
4900 image=left_image;
4901 if ((left_image->columns != right_image->columns) ||
4902 (left_image->rows != right_image->rows))
4903 ThrowImageException(ImageError,"LeftAndRightImageSizesDiffer");
4904 /*
4905 Initialize stereo image attributes.
4906 */
4907 stereo_image=CloneImage(left_image,left_image->columns,left_image->rows,
4908 MagickTrue,exception);
4909 if (stereo_image == (Image *) NULL)
4910 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004911 if (SetImageStorageClass(stereo_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004912 {
cristy3ed852e2009-09-05 21:47:34 +00004913 stereo_image=DestroyImage(stereo_image);
4914 return((Image *) NULL);
4915 }
cristy079c78d2012-07-03 11:53:48 +00004916 (void) SetImageColorspace(stereo_image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00004917 /*
4918 Copy left image to red channel and right image to blue channel.
4919 */
cristyda16f162011-02-19 23:52:17 +00004920 status=MagickTrue;
cristybb503372010-05-27 20:51:26 +00004921 for (y=0; y < (ssize_t) stereo_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004922 {
cristy4c08aed2011-07-01 19:47:50 +00004923 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004924 *restrict p,
4925 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004926
cristybb503372010-05-27 20:51:26 +00004927 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004928 x;
4929
cristy4c08aed2011-07-01 19:47:50 +00004930 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004931 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +00004932
4933 p=GetVirtualPixels(left_image,-x_offset,y-y_offset,image->columns,1,
4934 exception);
4935 q=GetVirtualPixels(right_image,0,y,right_image->columns,1,exception);
4936 r=QueueAuthenticPixels(stereo_image,0,y,stereo_image->columns,1,exception);
cristy76f512e2011-09-12 01:26:56 +00004937 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL) ||
4938 (r == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004939 break;
cristybb503372010-05-27 20:51:26 +00004940 for (x=0; x < (ssize_t) stereo_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004941 {
cristy4c08aed2011-07-01 19:47:50 +00004942 SetPixelRed(image,GetPixelRed(left_image,p),r);
cristy76f512e2011-09-12 01:26:56 +00004943 SetPixelGreen(image,GetPixelGreen(right_image,q),r);
4944 SetPixelBlue(image,GetPixelBlue(right_image,q),r);
4945 if ((GetPixelAlphaTraits(stereo_image) & CopyPixelTrait) != 0)
4946 SetPixelAlpha(image,(GetPixelAlpha(left_image,p)+
4947 GetPixelAlpha(right_image,q))/2,r);
cristyed231572011-07-14 02:18:59 +00004948 p+=GetPixelChannels(left_image);
cristy76f512e2011-09-12 01:26:56 +00004949 q+=GetPixelChannels(right_image);
4950 r+=GetPixelChannels(stereo_image);
cristy3ed852e2009-09-05 21:47:34 +00004951 }
4952 if (SyncAuthenticPixels(stereo_image,exception) == MagickFalse)
4953 break;
cristy8b27a6d2010-02-14 03:31:15 +00004954 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004955 {
cristy8b27a6d2010-02-14 03:31:15 +00004956 MagickBooleanType
4957 proceed;
4958
cristybb503372010-05-27 20:51:26 +00004959 proceed=SetImageProgress(image,StereoImageTag,(MagickOffsetType) y,
4960 stereo_image->rows);
cristy8b27a6d2010-02-14 03:31:15 +00004961 if (proceed == MagickFalse)
4962 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004963 }
4964 }
cristyda16f162011-02-19 23:52:17 +00004965 if (status == MagickFalse)
cristyfab83642012-12-14 00:31:38 +00004966 stereo_image=DestroyImage(stereo_image);
cristy3ed852e2009-09-05 21:47:34 +00004967 return(stereo_image);
4968}
4969
4970/*
4971%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4972% %
4973% %
4974% %
4975% S w i r l I m a g e %
4976% %
4977% %
4978% %
4979%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4980%
4981% SwirlImage() swirls the pixels about the center of the image, where
4982% degrees indicates the sweep of the arc through which each pixel is moved.
4983% You get a more dramatic effect as the degrees move from 1 to 360.
4984%
4985% The format of the SwirlImage method is:
4986%
4987% Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00004988% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004989%
4990% A description of each parameter follows:
4991%
4992% o image: the image.
4993%
4994% o degrees: Define the tightness of the swirling effect.
4995%
cristy76f512e2011-09-12 01:26:56 +00004996% o method: the pixel interpolation method.
4997%
cristy3ed852e2009-09-05 21:47:34 +00004998% o exception: return any errors or warnings in this structure.
4999%
5000*/
5001MagickExport Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00005002 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005003{
5004#define SwirlImageTag "Swirl/Image"
5005
cristyfa112112010-01-04 17:48:07 +00005006 CacheView
5007 *image_view,
cristy5ee937c2013-04-19 12:55:16 +00005008 *interpolate_view,
cristyfa112112010-01-04 17:48:07 +00005009 *swirl_view;
5010
cristy3ed852e2009-09-05 21:47:34 +00005011 Image
5012 *swirl_image;
5013
cristy3ed852e2009-09-05 21:47:34 +00005014 MagickBooleanType
5015 status;
5016
cristybb503372010-05-27 20:51:26 +00005017 MagickOffsetType
5018 progress;
5019
cristya19f1d72012-08-07 18:24:38 +00005020 double
cristy3ed852e2009-09-05 21:47:34 +00005021 radius;
5022
5023 PointInfo
5024 center,
5025 scale;
5026
cristybb503372010-05-27 20:51:26 +00005027 ssize_t
5028 y;
5029
cristy3ed852e2009-09-05 21:47:34 +00005030 /*
5031 Initialize swirl image attributes.
5032 */
5033 assert(image != (const Image *) NULL);
5034 assert(image->signature == MagickSignature);
5035 if (image->debug != MagickFalse)
5036 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5037 assert(exception != (ExceptionInfo *) NULL);
5038 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00005039 swirl_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005040 if (swirl_image == (Image *) NULL)
5041 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005042 if (SetImageStorageClass(swirl_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005043 {
cristy3ed852e2009-09-05 21:47:34 +00005044 swirl_image=DestroyImage(swirl_image);
5045 return((Image *) NULL);
5046 }
cristy4c08aed2011-07-01 19:47:50 +00005047 if (swirl_image->background_color.alpha != OpaqueAlpha)
cristy8a46d822012-08-28 23:32:39 +00005048 swirl_image->alpha_trait=BlendPixelTrait;
cristy3ed852e2009-09-05 21:47:34 +00005049 /*
5050 Compute scaling factor.
5051 */
5052 center.x=(double) image->columns/2.0;
5053 center.y=(double) image->rows/2.0;
5054 radius=MagickMax(center.x,center.y);
5055 scale.x=1.0;
5056 scale.y=1.0;
5057 if (image->columns > image->rows)
5058 scale.y=(double) image->columns/(double) image->rows;
5059 else
5060 if (image->columns < image->rows)
5061 scale.x=(double) image->rows/(double) image->columns;
5062 degrees=(double) DegreesToRadians(degrees);
5063 /*
5064 Swirl image.
5065 */
5066 status=MagickTrue;
5067 progress=0;
cristy46ff2672012-12-14 15:32:26 +00005068 image_view=AcquireVirtualCacheView(image,exception);
cristy5ee937c2013-04-19 12:55:16 +00005069 interpolate_view=AcquireVirtualCacheView(image,exception);
cristy46ff2672012-12-14 15:32:26 +00005070 swirl_view=AcquireAuthenticCacheView(swirl_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00005071#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00005072 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00005073 magick_threads(image,swirl_image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00005074#endif
cristybb503372010-05-27 20:51:26 +00005075 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005076 {
cristya19f1d72012-08-07 18:24:38 +00005077 double
cristy3ed852e2009-09-05 21:47:34 +00005078 distance;
5079
5080 PointInfo
5081 delta;
5082
cristy6d188022011-09-12 13:23:33 +00005083 register const Quantum
5084 *restrict p;
5085
cristybb503372010-05-27 20:51:26 +00005086 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005087 x;
5088
cristy4c08aed2011-07-01 19:47:50 +00005089 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005090 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005091
5092 if (status == MagickFalse)
5093 continue;
cristy1665ce12013-04-19 12:18:43 +00005094 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy1707c6c2012-01-18 23:30:54 +00005095 q=QueueCacheViewAuthenticPixels(swirl_view,0,y,swirl_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00005096 exception);
cristy6d188022011-09-12 13:23:33 +00005097 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005098 {
5099 status=MagickFalse;
5100 continue;
5101 }
cristy3ed852e2009-09-05 21:47:34 +00005102 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00005103 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005104 {
5105 /*
5106 Determine if the pixel is within an ellipse.
5107 */
cristy883fde12013-04-08 00:50:13 +00005108 if (GetPixelReadMask(image,p) == 0)
cristy10a6c612012-01-29 21:41:05 +00005109 {
cristyc3a58022013-10-09 23:22:42 +00005110 SetPixelBackgoundColor(swirl_image,q);
cristy10a6c612012-01-29 21:41:05 +00005111 p+=GetPixelChannels(image);
5112 q+=GetPixelChannels(swirl_image);
5113 continue;
5114 }
cristy3ed852e2009-09-05 21:47:34 +00005115 delta.x=scale.x*(double) (x-center.x);
5116 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00005117 if (distance >= (radius*radius))
5118 {
cristy1707c6c2012-01-18 23:30:54 +00005119 register ssize_t
5120 i;
5121
cristy6d188022011-09-12 13:23:33 +00005122 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy1707c6c2012-01-18 23:30:54 +00005123 {
cristy5a23c552013-02-13 14:34:28 +00005124 PixelChannel channel=GetPixelChannelChannel(image,i);
5125 PixelTrait traits=GetPixelChannelTraits(image,channel);
5126 PixelTrait swirl_traits=GetPixelChannelTraits(swirl_image,channel);
cristy1707c6c2012-01-18 23:30:54 +00005127 if ((traits == UndefinedPixelTrait) ||
5128 (swirl_traits == UndefinedPixelTrait))
5129 continue;
5130 SetPixelChannel(swirl_image,channel,p[i],q);
5131 }
cristy6d188022011-09-12 13:23:33 +00005132 }
5133 else
cristy3ed852e2009-09-05 21:47:34 +00005134 {
cristya19f1d72012-08-07 18:24:38 +00005135 double
cristy3ed852e2009-09-05 21:47:34 +00005136 cosine,
5137 factor,
5138 sine;
5139
5140 /*
5141 Swirl the pixel.
5142 */
5143 factor=1.0-sqrt((double) distance)/radius;
5144 sine=sin((double) (degrees*factor*factor));
5145 cosine=cos((double) (degrees*factor*factor));
cristy5ee937c2013-04-19 12:55:16 +00005146 status=InterpolatePixelChannels(image,interpolate_view,swirl_image,
5147 method,((cosine*delta.x-sine*delta.y)/scale.x+center.x),(double)
cristy76f512e2011-09-12 01:26:56 +00005148 ((sine*delta.x+cosine*delta.y)/scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00005149 }
cristy6d188022011-09-12 13:23:33 +00005150 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00005151 q+=GetPixelChannels(swirl_image);
cristy3ed852e2009-09-05 21:47:34 +00005152 }
5153 if (SyncCacheViewAuthenticPixels(swirl_view,exception) == MagickFalse)
5154 status=MagickFalse;
5155 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5156 {
5157 MagickBooleanType
5158 proceed;
5159
cristyb5d5f722009-11-04 03:03:49 +00005160#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005161 #pragma omp critical (MagickCore_SwirlImage)
cristy3ed852e2009-09-05 21:47:34 +00005162#endif
5163 proceed=SetImageProgress(image,SwirlImageTag,progress++,image->rows);
5164 if (proceed == MagickFalse)
5165 status=MagickFalse;
5166 }
5167 }
5168 swirl_view=DestroyCacheView(swirl_view);
cristy5ee937c2013-04-19 12:55:16 +00005169 interpolate_view=DestroyCacheView(interpolate_view);
cristy3ed852e2009-09-05 21:47:34 +00005170 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00005171 if (status == MagickFalse)
5172 swirl_image=DestroyImage(swirl_image);
5173 return(swirl_image);
5174}
5175
5176/*
5177%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5178% %
5179% %
5180% %
5181% T i n t I m a g e %
5182% %
5183% %
5184% %
5185%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5186%
5187% TintImage() applies a color vector to each pixel in the image. The length
5188% of the vector is 0 for black and white and at its maximum for the midtones.
5189% The vector weighting function is f(x)=(1-(4.0*((x-0.5)*(x-0.5))))
5190%
5191% The format of the TintImage method is:
5192%
cristyb817c3f2011-10-03 14:00:35 +00005193% Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005194% const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005195%
5196% A description of each parameter follows:
5197%
5198% o image: the image.
5199%
cristyb817c3f2011-10-03 14:00:35 +00005200% o blend: A color value used for tinting.
cristy3ed852e2009-09-05 21:47:34 +00005201%
5202% o tint: A color value used for tinting.
5203%
5204% o exception: return any errors or warnings in this structure.
5205%
5206*/
cristyb817c3f2011-10-03 14:00:35 +00005207MagickExport Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005208 const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005209{
5210#define TintImageTag "Tint/Image"
5211
cristyc4c8d132010-01-07 01:58:38 +00005212 CacheView
5213 *image_view,
5214 *tint_view;
5215
cristyf54150e2013-04-11 01:25:47 +00005216 double
5217 intensity;
5218
cristy3ed852e2009-09-05 21:47:34 +00005219 GeometryInfo
5220 geometry_info;
5221
5222 Image
5223 *tint_image;
5224
cristy3ed852e2009-09-05 21:47:34 +00005225 MagickBooleanType
5226 status;
5227
cristybb503372010-05-27 20:51:26 +00005228 MagickOffsetType
5229 progress;
cristy3ed852e2009-09-05 21:47:34 +00005230
cristy4c08aed2011-07-01 19:47:50 +00005231 PixelInfo
cristy1707c6c2012-01-18 23:30:54 +00005232 color_vector;
cristy3ed852e2009-09-05 21:47:34 +00005233
cristybb503372010-05-27 20:51:26 +00005234 MagickStatusType
5235 flags;
5236
5237 ssize_t
5238 y;
5239
cristy3ed852e2009-09-05 21:47:34 +00005240 /*
5241 Allocate tint image.
5242 */
5243 assert(image != (const Image *) NULL);
5244 assert(image->signature == MagickSignature);
5245 if (image->debug != MagickFalse)
5246 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5247 assert(exception != (ExceptionInfo *) NULL);
5248 assert(exception->signature == MagickSignature);
5249 tint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5250 if (tint_image == (Image *) NULL)
5251 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005252 if (SetImageStorageClass(tint_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005253 {
cristy3ed852e2009-09-05 21:47:34 +00005254 tint_image=DestroyImage(tint_image);
5255 return((Image *) NULL);
5256 }
cristya6400b12013-03-15 12:20:18 +00005257 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
cristy4dab3802013-03-15 22:08:15 +00005258 (IsPixelInfoGray(tint) == MagickFalse))
cristya6400b12013-03-15 12:20:18 +00005259 (void) SetImageColorspace(tint_image,sRGBColorspace,exception);
cristyaed9c382011-10-03 17:54:21 +00005260 if (blend == (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005261 return(tint_image);
5262 /*
5263 Determine RGB values of the color.
5264 */
cristy1707c6c2012-01-18 23:30:54 +00005265 GetPixelInfo(image,&color_vector);
cristyb817c3f2011-10-03 14:00:35 +00005266 flags=ParseGeometry(blend,&geometry_info);
cristy1707c6c2012-01-18 23:30:54 +00005267 color_vector.red=geometry_info.rho;
5268 color_vector.green=geometry_info.rho;
5269 color_vector.blue=geometry_info.rho;
cristy92ac5722013-03-09 15:09:38 +00005270 color_vector.alpha=(MagickRealType) OpaqueAlpha;
cristy3ed852e2009-09-05 21:47:34 +00005271 if ((flags & SigmaValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005272 color_vector.green=geometry_info.sigma;
cristy3ed852e2009-09-05 21:47:34 +00005273 if ((flags & XiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005274 color_vector.blue=geometry_info.xi;
cristyb817c3f2011-10-03 14:00:35 +00005275 if ((flags & PsiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005276 color_vector.alpha=geometry_info.psi;
cristy76f512e2011-09-12 01:26:56 +00005277 if (image->colorspace == CMYKColorspace)
5278 {
cristy1707c6c2012-01-18 23:30:54 +00005279 color_vector.black=geometry_info.rho;
cristy76f512e2011-09-12 01:26:56 +00005280 if ((flags & PsiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005281 color_vector.black=geometry_info.psi;
cristy76f512e2011-09-12 01:26:56 +00005282 if ((flags & ChiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005283 color_vector.alpha=geometry_info.chi;
cristy76f512e2011-09-12 01:26:56 +00005284 }
cristya19f1d72012-08-07 18:24:38 +00005285 intensity=(double) GetPixelInfoIntensity(tint);
cristyf54150e2013-04-11 01:25:47 +00005286 color_vector.red=(double) (color_vector.red*tint->red/100.0-intensity);
5287 color_vector.green=(double) (color_vector.green*tint->green/100.0-intensity);
5288 color_vector.blue=(double) (color_vector.blue*tint->blue/100.0-intensity);
5289 color_vector.black=(double) (color_vector.black*tint->black/100.0-intensity);
5290 color_vector.alpha=(double) (color_vector.alpha*tint->alpha/100.0-intensity);
cristy3ed852e2009-09-05 21:47:34 +00005291 /*
5292 Tint image.
5293 */
5294 status=MagickTrue;
5295 progress=0;
cristy46ff2672012-12-14 15:32:26 +00005296 image_view=AcquireVirtualCacheView(image,exception);
5297 tint_view=AcquireAuthenticCacheView(tint_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00005298#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00005299 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00005300 magick_threads(image,tint_image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00005301#endif
cristybb503372010-05-27 20:51:26 +00005302 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005303 {
cristy4c08aed2011-07-01 19:47:50 +00005304 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00005305 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00005306
cristy4c08aed2011-07-01 19:47:50 +00005307 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005308 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005309
cristy6b91acb2011-04-19 12:23:54 +00005310 register ssize_t
5311 x;
5312
cristy3ed852e2009-09-05 21:47:34 +00005313 if (status == MagickFalse)
5314 continue;
5315 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5316 q=QueueCacheViewAuthenticPixels(tint_view,0,y,tint_image->columns,1,
5317 exception);
cristy4c08aed2011-07-01 19:47:50 +00005318 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005319 {
5320 status=MagickFalse;
5321 continue;
5322 }
cristybb503372010-05-27 20:51:26 +00005323 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005324 {
cristy4c08aed2011-07-01 19:47:50 +00005325 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00005326 pixel;
5327
cristya19f1d72012-08-07 18:24:38 +00005328 double
cristy3ed852e2009-09-05 21:47:34 +00005329 weight;
5330
cristy1707c6c2012-01-18 23:30:54 +00005331 register ssize_t
5332 i;
5333
5334 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5335 {
cristy5a23c552013-02-13 14:34:28 +00005336 PixelChannel channel=GetPixelChannelChannel(image,i);
5337 PixelTrait traits=GetPixelChannelTraits(image,channel);
5338 PixelTrait tint_traits=GetPixelChannelTraits(tint_image,channel);
cristy1707c6c2012-01-18 23:30:54 +00005339 if ((traits == UndefinedPixelTrait) ||
5340 (tint_traits == UndefinedPixelTrait))
5341 continue;
cristy1eced092012-08-10 23:10:56 +00005342 if (((tint_traits & CopyPixelTrait) != 0) ||
cristy883fde12013-04-08 00:50:13 +00005343 (GetPixelReadMask(image,p) == 0))
cristy1707c6c2012-01-18 23:30:54 +00005344 {
5345 SetPixelChannel(tint_image,channel,p[i],q);
5346 continue;
5347 }
5348 }
5349 GetPixelInfo(image,&pixel);
5350 weight=QuantumScale*GetPixelRed(image,p)-0.5;
cristy1eced092012-08-10 23:10:56 +00005351 pixel.red=(double) GetPixelRed(image,p)+color_vector.red*(1.0-(4.0*
5352 (weight*weight)));
cristy1707c6c2012-01-18 23:30:54 +00005353 weight=QuantumScale*GetPixelGreen(image,p)-0.5;
cristy1eced092012-08-10 23:10:56 +00005354 pixel.green=(double) GetPixelGreen(image,p)+color_vector.green*(1.0-(4.0*
5355 (weight*weight)));
cristy1707c6c2012-01-18 23:30:54 +00005356 weight=QuantumScale*GetPixelBlue(image,p)-0.5;
cristy1eced092012-08-10 23:10:56 +00005357 pixel.blue=(double) GetPixelBlue(image,p)+color_vector.blue*(1.0-(4.0*
5358 (weight*weight)));
cristy1707c6c2012-01-18 23:30:54 +00005359 weight=QuantumScale*GetPixelBlack(image,p)-0.5;
cristy1eced092012-08-10 23:10:56 +00005360 pixel.black=(double) GetPixelBlack(image,p)+color_vector.black*(1.0-(4.0*
5361 (weight*weight)));
cristy1707c6c2012-01-18 23:30:54 +00005362 SetPixelInfoPixel(tint_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +00005363 p+=GetPixelChannels(image);
5364 q+=GetPixelChannels(tint_image);
cristy3ed852e2009-09-05 21:47:34 +00005365 }
5366 if (SyncCacheViewAuthenticPixels(tint_view,exception) == MagickFalse)
5367 status=MagickFalse;
5368 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5369 {
5370 MagickBooleanType
5371 proceed;
5372
cristyb5d5f722009-11-04 03:03:49 +00005373#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005374 #pragma omp critical (MagickCore_TintImage)
cristy3ed852e2009-09-05 21:47:34 +00005375#endif
5376 proceed=SetImageProgress(image,TintImageTag,progress++,image->rows);
5377 if (proceed == MagickFalse)
5378 status=MagickFalse;
5379 }
5380 }
5381 tint_view=DestroyCacheView(tint_view);
5382 image_view=DestroyCacheView(image_view);
5383 if (status == MagickFalse)
5384 tint_image=DestroyImage(tint_image);
5385 return(tint_image);
5386}
5387
5388/*
5389%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5390% %
5391% %
5392% %
5393% V i g n e t t e I m a g e %
5394% %
5395% %
5396% %
5397%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5398%
5399% VignetteImage() softens the edges of the image in vignette style.
5400%
5401% The format of the VignetteImage method is:
5402%
5403% Image *VignetteImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00005404% const double sigma,const ssize_t x,const ssize_t y,
cristy05c0c9a2011-09-05 23:16:13 +00005405% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005406%
5407% A description of each parameter follows:
5408%
5409% o image: the image.
5410%
5411% o radius: the radius of the pixel neighborhood.
5412%
5413% o sigma: the standard deviation of the Gaussian, in pixels.
5414%
5415% o x, y: Define the x and y ellipse offset.
5416%
5417% o exception: return any errors or warnings in this structure.
5418%
5419*/
5420MagickExport Image *VignetteImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00005421 const double sigma,const ssize_t x,const ssize_t y,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005422{
5423 char
5424 ellipse[MaxTextExtent];
5425
5426 DrawInfo
5427 *draw_info;
5428
5429 Image
5430 *canvas_image,
5431 *blur_image,
5432 *oval_image,
5433 *vignette_image;
5434
5435 assert(image != (Image *) NULL);
5436 assert(image->signature == MagickSignature);
5437 if (image->debug != MagickFalse)
5438 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5439 assert(exception != (ExceptionInfo *) NULL);
5440 assert(exception->signature == MagickSignature);
5441 canvas_image=CloneImage(image,0,0,MagickTrue,exception);
5442 if (canvas_image == (Image *) NULL)
5443 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005444 if (SetImageStorageClass(canvas_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005445 {
cristy3ed852e2009-09-05 21:47:34 +00005446 canvas_image=DestroyImage(canvas_image);
5447 return((Image *) NULL);
5448 }
cristy8a46d822012-08-28 23:32:39 +00005449 canvas_image->alpha_trait=BlendPixelTrait;
cristy98621462011-12-31 22:31:11 +00005450 oval_image=CloneImage(canvas_image,canvas_image->columns,canvas_image->rows,
5451 MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005452 if (oval_image == (Image *) NULL)
5453 {
5454 canvas_image=DestroyImage(canvas_image);
5455 return((Image *) NULL);
5456 }
cristy9950d572011-10-01 18:22:35 +00005457 (void) QueryColorCompliance("#000000",AllCompliance,
5458 &oval_image->background_color,exception);
cristyea1a8aa2011-10-20 13:24:06 +00005459 (void) SetImageBackgroundColor(oval_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00005460 draw_info=CloneDrawInfo((const ImageInfo *) NULL,(const DrawInfo *) NULL);
cristy9950d572011-10-01 18:22:35 +00005461 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->fill,
5462 exception);
5463 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->stroke,
5464 exception);
cristy1707c6c2012-01-18 23:30:54 +00005465 (void) FormatLocaleString(ellipse,MaxTextExtent,"ellipse %g,%g,%g,%g,"
5466 "0.0,360.0",image->columns/2.0,image->rows/2.0,image->columns/2.0-x,
5467 image->rows/2.0-y);
cristy3ed852e2009-09-05 21:47:34 +00005468 draw_info->primitive=AcquireString(ellipse);
cristy018f07f2011-09-04 21:15:19 +00005469 (void) DrawImage(oval_image,draw_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00005470 draw_info=DestroyDrawInfo(draw_info);
cristyaa2c16c2012-03-25 22:21:35 +00005471 blur_image=BlurImage(oval_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +00005472 oval_image=DestroyImage(oval_image);
5473 if (blur_image == (Image *) NULL)
5474 {
5475 canvas_image=DestroyImage(canvas_image);
5476 return((Image *) NULL);
5477 }
cristy8a46d822012-08-28 23:32:39 +00005478 blur_image->alpha_trait=UndefinedPixelTrait;
cristy39172402012-03-30 13:04:39 +00005479 (void) CompositeImage(canvas_image,blur_image,IntensityCompositeOp,MagickTrue,
5480 0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00005481 blur_image=DestroyImage(blur_image);
5482 vignette_image=MergeImageLayers(canvas_image,FlattenLayer,exception);
5483 canvas_image=DestroyImage(canvas_image);
cristy66d26122012-06-23 21:56:40 +00005484 if (vignette_image != (Image *) NULL)
5485 (void) TransformImageColorspace(vignette_image,image->colorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00005486 return(vignette_image);
5487}
5488
5489/*
5490%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5491% %
5492% %
5493% %
5494% W a v e I m a g e %
5495% %
5496% %
5497% %
5498%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5499%
5500% WaveImage() creates a "ripple" effect in the image by shifting the pixels
cristycee97112010-05-28 00:44:52 +00005501% vertically along a sine wave whose amplitude and wavelength is specified
cristy3ed852e2009-09-05 21:47:34 +00005502% by the given parameters.
5503%
5504% The format of the WaveImage method is:
5505%
5506% Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005507% const double wave_length,const PixelInterpolateMethod method,
5508% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005509%
5510% A description of each parameter follows:
5511%
5512% o image: the image.
5513%
5514% o amplitude, wave_length: Define the amplitude and wave length of the
5515% sine wave.
5516%
cristy5c4e2582011-09-11 19:21:03 +00005517% o interpolate: the pixel interpolation method.
5518%
cristy3ed852e2009-09-05 21:47:34 +00005519% o exception: return any errors or warnings in this structure.
5520%
5521*/
5522MagickExport Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005523 const double wave_length,const PixelInterpolateMethod method,
5524 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005525{
5526#define WaveImageTag "Wave/Image"
5527
cristyfa112112010-01-04 17:48:07 +00005528 CacheView
cristyd76c51e2011-03-26 00:21:26 +00005529 *image_view,
cristyfa112112010-01-04 17:48:07 +00005530 *wave_view;
5531
cristy3ed852e2009-09-05 21:47:34 +00005532 Image
5533 *wave_image;
5534
cristy3ed852e2009-09-05 21:47:34 +00005535 MagickBooleanType
5536 status;
5537
cristybb503372010-05-27 20:51:26 +00005538 MagickOffsetType
5539 progress;
5540
cristya19f1d72012-08-07 18:24:38 +00005541 double
cristy3ed852e2009-09-05 21:47:34 +00005542 *sine_map;
5543
cristybb503372010-05-27 20:51:26 +00005544 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005545 i;
5546
cristybb503372010-05-27 20:51:26 +00005547 ssize_t
5548 y;
5549
cristy3ed852e2009-09-05 21:47:34 +00005550 /*
5551 Initialize wave image attributes.
5552 */
5553 assert(image != (Image *) NULL);
5554 assert(image->signature == MagickSignature);
5555 if (image->debug != MagickFalse)
5556 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5557 assert(exception != (ExceptionInfo *) NULL);
5558 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00005559 wave_image=CloneImage(image,image->columns,(size_t) (image->rows+2.0*
cristy3ed852e2009-09-05 21:47:34 +00005560 fabs(amplitude)),MagickTrue,exception);
5561 if (wave_image == (Image *) NULL)
5562 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005563 if (SetImageStorageClass(wave_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005564 {
cristy3ed852e2009-09-05 21:47:34 +00005565 wave_image=DestroyImage(wave_image);
5566 return((Image *) NULL);
5567 }
cristy4c08aed2011-07-01 19:47:50 +00005568 if (wave_image->background_color.alpha != OpaqueAlpha)
cristy8a46d822012-08-28 23:32:39 +00005569 wave_image->alpha_trait=BlendPixelTrait;
cristy3ed852e2009-09-05 21:47:34 +00005570 /*
5571 Allocate sine map.
5572 */
cristya19f1d72012-08-07 18:24:38 +00005573 sine_map=(double *) AcquireQuantumMemory((size_t) wave_image->columns,
cristy3ed852e2009-09-05 21:47:34 +00005574 sizeof(*sine_map));
cristya19f1d72012-08-07 18:24:38 +00005575 if (sine_map == (double *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005576 {
5577 wave_image=DestroyImage(wave_image);
5578 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
5579 }
cristybb503372010-05-27 20:51:26 +00005580 for (i=0; i < (ssize_t) wave_image->columns; i++)
cristy4205a3c2010-09-12 20:19:59 +00005581 sine_map[i]=fabs(amplitude)+amplitude*sin((double) ((2.0*MagickPI*i)/
5582 wave_length));
cristy3ed852e2009-09-05 21:47:34 +00005583 /*
5584 Wave image.
5585 */
5586 status=MagickTrue;
5587 progress=0;
cristy46ff2672012-12-14 15:32:26 +00005588 image_view=AcquireVirtualCacheView(image,exception);
5589 wave_view=AcquireAuthenticCacheView(wave_image,exception);
cristyd76c51e2011-03-26 00:21:26 +00005590 (void) SetCacheViewVirtualPixelMethod(image_view,
5591 BackgroundVirtualPixelMethod);
cristyb5d5f722009-11-04 03:03:49 +00005592#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00005593 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00005594 magick_threads(image,wave_image,wave_image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00005595#endif
cristybb503372010-05-27 20:51:26 +00005596 for (y=0; y < (ssize_t) wave_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005597 {
cristy4c08aed2011-07-01 19:47:50 +00005598 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005599 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005600
cristye97bb922011-04-03 01:36:52 +00005601 register ssize_t
5602 x;
5603
cristy3ed852e2009-09-05 21:47:34 +00005604 if (status == MagickFalse)
5605 continue;
5606 q=QueueCacheViewAuthenticPixels(wave_view,0,y,wave_image->columns,1,
5607 exception);
cristyacd2ed22011-08-30 01:44:23 +00005608 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005609 {
5610 status=MagickFalse;
5611 continue;
5612 }
cristybb503372010-05-27 20:51:26 +00005613 for (x=0; x < (ssize_t) wave_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005614 {
cristy5c4e2582011-09-11 19:21:03 +00005615 status=InterpolatePixelChannels(image,image_view,wave_image,method,
5616 (double) x,(double) (y-sine_map[x]),q,exception);
cristyed231572011-07-14 02:18:59 +00005617 q+=GetPixelChannels(wave_image);
cristy3ed852e2009-09-05 21:47:34 +00005618 }
5619 if (SyncCacheViewAuthenticPixels(wave_view,exception) == MagickFalse)
5620 status=MagickFalse;
5621 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5622 {
5623 MagickBooleanType
5624 proceed;
5625
cristyb5d5f722009-11-04 03:03:49 +00005626#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005627 #pragma omp critical (MagickCore_WaveImage)
cristy3ed852e2009-09-05 21:47:34 +00005628#endif
5629 proceed=SetImageProgress(image,WaveImageTag,progress++,image->rows);
5630 if (proceed == MagickFalse)
5631 status=MagickFalse;
5632 }
5633 }
5634 wave_view=DestroyCacheView(wave_view);
cristyd76c51e2011-03-26 00:21:26 +00005635 image_view=DestroyCacheView(image_view);
cristya19f1d72012-08-07 18:24:38 +00005636 sine_map=(double *) RelinquishMagickMemory(sine_map);
cristy3ed852e2009-09-05 21:47:34 +00005637 if (status == MagickFalse)
5638 wave_image=DestroyImage(wave_image);
5639 return(wave_image);
5640}