blob: b845f0b2afd0032bbe024355771553927e5c612f [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% FFFFF X X %
7% F X X %
8% FFF X %
9% F X X %
10% F X X %
11% %
12% %
13% MagickCore Image Special Effects Methods %
14% %
15% Software Design %
16% John Cristy %
17% October 1996 %
18% %
19% %
cristy1454be72011-12-19 01:52:48 +000020% Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37%
38*/
39
40/*
41 Include declarations.
42*/
cristy4c08aed2011-07-01 19:47:50 +000043#include "MagickCore/studio.h"
44#include "MagickCore/annotate.h"
45#include "MagickCore/artifact.h"
46#include "MagickCore/attribute.h"
47#include "MagickCore/cache.h"
48#include "MagickCore/cache-view.h"
49#include "MagickCore/color.h"
50#include "MagickCore/color-private.h"
cristy9b7a4fc2012-04-08 22:26:56 +000051#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000052#include "MagickCore/composite.h"
53#include "MagickCore/decorate.h"
cristyc53413d2011-11-17 13:04:26 +000054#include "MagickCore/distort.h"
cristy4c08aed2011-07-01 19:47:50 +000055#include "MagickCore/draw.h"
56#include "MagickCore/effect.h"
57#include "MagickCore/enhance.h"
58#include "MagickCore/exception.h"
59#include "MagickCore/exception-private.h"
60#include "MagickCore/fx.h"
61#include "MagickCore/fx-private.h"
62#include "MagickCore/gem.h"
cristy8ea81222011-09-04 10:33:32 +000063#include "MagickCore/gem-private.h"
cristy4c08aed2011-07-01 19:47:50 +000064#include "MagickCore/geometry.h"
65#include "MagickCore/layer.h"
66#include "MagickCore/list.h"
67#include "MagickCore/log.h"
68#include "MagickCore/image.h"
69#include "MagickCore/image-private.h"
70#include "MagickCore/magick.h"
71#include "MagickCore/memory_.h"
72#include "MagickCore/monitor.h"
73#include "MagickCore/monitor-private.h"
74#include "MagickCore/option.h"
75#include "MagickCore/pixel.h"
76#include "MagickCore/pixel-accessor.h"
77#include "MagickCore/property.h"
78#include "MagickCore/quantum.h"
79#include "MagickCore/quantum-private.h"
80#include "MagickCore/random_.h"
81#include "MagickCore/random-private.h"
82#include "MagickCore/resample.h"
83#include "MagickCore/resample-private.h"
84#include "MagickCore/resize.h"
cristy57340e02012-05-05 00:53:23 +000085#include "MagickCore/resource_.h"
cristy4c08aed2011-07-01 19:47:50 +000086#include "MagickCore/splay-tree.h"
87#include "MagickCore/statistic.h"
88#include "MagickCore/string_.h"
89#include "MagickCore/string-private.h"
90#include "MagickCore/thread-private.h"
91#include "MagickCore/transform.h"
92#include "MagickCore/utility.h"
cristy3ed852e2009-09-05 21:47:34 +000093
94/*
95 Define declarations.
96*/
97#define LeftShiftOperator 0xf5
98#define RightShiftOperator 0xf6
99#define LessThanEqualOperator 0xf7
100#define GreaterThanEqualOperator 0xf8
101#define EqualOperator 0xf9
102#define NotEqualOperator 0xfa
103#define LogicalAndOperator 0xfb
104#define LogicalOrOperator 0xfc
cristy116af162010-08-13 01:25:47 +0000105#define ExponentialNotation 0xfd
cristy3ed852e2009-09-05 21:47:34 +0000106
107struct _FxInfo
108{
109 const Image
110 *images;
111
cristy3ed852e2009-09-05 21:47:34 +0000112 char
113 *expression;
114
115 FILE
116 *file;
117
118 SplayTreeInfo
119 *colors,
120 *symbols;
121
cristyd76c51e2011-03-26 00:21:26 +0000122 CacheView
123 **view;
cristy3ed852e2009-09-05 21:47:34 +0000124
125 RandomInfo
126 *random_info;
127
128 ExceptionInfo
129 *exception;
130};
131
132/*
133%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
134% %
135% %
136% %
137+ A c q u i r e F x I n f o %
138% %
139% %
140% %
141%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
142%
143% AcquireFxInfo() allocates the FxInfo structure.
144%
145% The format of the AcquireFxInfo method is:
146%
cristydb070952012-04-20 14:33:00 +0000147% FxInfo *AcquireFxInfo(Image *image,const char *expression,
148% ExceptionInfo *exception)
cristy0a9b3722010-10-23 18:45:49 +0000149%
cristy3ed852e2009-09-05 21:47:34 +0000150% A description of each parameter follows:
151%
152% o image: the image.
153%
154% o expression: the expression.
155%
cristydb070952012-04-20 14:33:00 +0000156% o exception: return any errors or warnings in this structure.
157%
cristy3ed852e2009-09-05 21:47:34 +0000158*/
cristydb070952012-04-20 14:33:00 +0000159MagickPrivate FxInfo *AcquireFxInfo(const Image *image,const char *expression,
160 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000161{
162 char
163 fx_op[2];
164
cristycb180922011-03-11 14:41:24 +0000165 const Image
166 *next;
167
cristy3ed852e2009-09-05 21:47:34 +0000168 FxInfo
169 *fx_info;
170
cristybb503372010-05-27 20:51:26 +0000171 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000172 i;
173
cristy73bd4a52010-10-05 11:24:23 +0000174 fx_info=(FxInfo *) AcquireMagickMemory(sizeof(*fx_info));
cristy3ed852e2009-09-05 21:47:34 +0000175 if (fx_info == (FxInfo *) NULL)
176 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
177 (void) ResetMagickMemory(fx_info,0,sizeof(*fx_info));
178 fx_info->exception=AcquireExceptionInfo();
anthony7d86e172011-03-23 12:37:06 +0000179 fx_info->images=image;
cristy3ed852e2009-09-05 21:47:34 +0000180 fx_info->colors=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
181 RelinquishMagickMemory);
182 fx_info->symbols=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
183 RelinquishMagickMemory);
cristyd76c51e2011-03-26 00:21:26 +0000184 fx_info->view=(CacheView **) AcquireQuantumMemory(GetImageListLength(
185 fx_info->images),sizeof(*fx_info->view));
186 if (fx_info->view == (CacheView **) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000187 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
cristya2262262011-03-11 02:50:37 +0000188 i=0;
cristy0ea377f2011-03-24 00:54:19 +0000189 next=GetFirstImageInList(fx_info->images);
190 for ( ; next != (Image *) NULL; next=next->next)
cristy3ed852e2009-09-05 21:47:34 +0000191 {
cristydb070952012-04-20 14:33:00 +0000192 fx_info->view[i]=AcquireVirtualCacheView(next,exception);
cristya2262262011-03-11 02:50:37 +0000193 i++;
cristy3ed852e2009-09-05 21:47:34 +0000194 }
195 fx_info->random_info=AcquireRandomInfo();
196 fx_info->expression=ConstantString(expression);
197 fx_info->file=stderr;
198 (void) SubstituteString(&fx_info->expression," ",""); /* compact string */
cristy37af0912011-05-23 16:09:42 +0000199 /*
200 Force right-to-left associativity for unary negation.
201 */
202 (void) SubstituteString(&fx_info->expression,"-","-1.0*");
cristy8fbb96d2012-07-18 01:18:52 +0000203 (void) SubstituteString(&fx_info->expression,"E-1.0*","E-");
204 (void) SubstituteString(&fx_info->expression,"e-1.0*","e-");
cristy8b8a3ae2010-10-23 18:49:46 +0000205 /*
cristy3ed852e2009-09-05 21:47:34 +0000206 Convert complex to simple operators.
207 */
208 fx_op[1]='\0';
209 *fx_op=(char) LeftShiftOperator;
210 (void) SubstituteString(&fx_info->expression,"<<",fx_op);
211 *fx_op=(char) RightShiftOperator;
212 (void) SubstituteString(&fx_info->expression,">>",fx_op);
213 *fx_op=(char) LessThanEqualOperator;
214 (void) SubstituteString(&fx_info->expression,"<=",fx_op);
215 *fx_op=(char) GreaterThanEqualOperator;
216 (void) SubstituteString(&fx_info->expression,">=",fx_op);
217 *fx_op=(char) EqualOperator;
218 (void) SubstituteString(&fx_info->expression,"==",fx_op);
219 *fx_op=(char) NotEqualOperator;
220 (void) SubstituteString(&fx_info->expression,"!=",fx_op);
221 *fx_op=(char) LogicalAndOperator;
222 (void) SubstituteString(&fx_info->expression,"&&",fx_op);
223 *fx_op=(char) LogicalOrOperator;
224 (void) SubstituteString(&fx_info->expression,"||",fx_op);
cristy116af162010-08-13 01:25:47 +0000225 *fx_op=(char) ExponentialNotation;
226 (void) SubstituteString(&fx_info->expression,"**",fx_op);
cristy3ed852e2009-09-05 21:47:34 +0000227 return(fx_info);
228}
229
230/*
231%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
232% %
233% %
234% %
235% A d d N o i s e I m a g e %
236% %
237% %
238% %
239%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
240%
241% AddNoiseImage() adds random noise to the image.
242%
243% The format of the AddNoiseImage method is:
244%
245% Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
cristy9ed1f812011-10-08 02:00:08 +0000246% const double attenuate,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000247%
248% A description of each parameter follows:
249%
250% o image: the image.
251%
252% o channel: the channel type.
253%
254% o noise_type: The type of noise: Uniform, Gaussian, Multiplicative,
255% Impulse, Laplacian, or Poisson.
256%
cristy9ed1f812011-10-08 02:00:08 +0000257% o attenuate: attenuate the random distribution.
258%
cristy3ed852e2009-09-05 21:47:34 +0000259% o exception: return any errors or warnings in this structure.
260%
261*/
cristy9ed1f812011-10-08 02:00:08 +0000262MagickExport Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
263 const double attenuate,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000264{
265#define AddNoiseImageTag "AddNoise/Image"
266
cristyfa112112010-01-04 17:48:07 +0000267 CacheView
268 *image_view,
269 *noise_view;
270
cristy3ed852e2009-09-05 21:47:34 +0000271 Image
272 *noise_image;
273
cristy3ed852e2009-09-05 21:47:34 +0000274 MagickBooleanType
275 status;
276
cristybb503372010-05-27 20:51:26 +0000277 MagickOffsetType
278 progress;
279
cristy3ed852e2009-09-05 21:47:34 +0000280 RandomInfo
cristyfa112112010-01-04 17:48:07 +0000281 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +0000282
cristybb503372010-05-27 20:51:26 +0000283 ssize_t
284 y;
285
cristy57340e02012-05-05 00:53:23 +0000286 unsigned long
287 key;
288
cristy3ed852e2009-09-05 21:47:34 +0000289 /*
290 Initialize noise image attributes.
291 */
292 assert(image != (const Image *) NULL);
293 assert(image->signature == MagickSignature);
294 if (image->debug != MagickFalse)
295 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
296 assert(exception != (ExceptionInfo *) NULL);
297 assert(exception->signature == MagickSignature);
cristyb2145892011-10-10 00:55:32 +0000298 noise_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000299 if (noise_image == (Image *) NULL)
300 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000301 if (SetImageStorageClass(noise_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000302 {
cristy3ed852e2009-09-05 21:47:34 +0000303 noise_image=DestroyImage(noise_image);
304 return((Image *) NULL);
305 }
cristy98ccd782012-07-09 11:22:59 +0000306 if (IsGrayColorspace(image->colorspace) != MagickFalse)
cristyb09db112012-07-11 12:04:31 +0000307 (void) TransformImageColorspace(noise_image,RGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000308 /*
309 Add noise in each row.
310 */
cristy3ed852e2009-09-05 21:47:34 +0000311 status=MagickTrue;
312 progress=0;
313 random_info=AcquireRandomInfoThreadSet();
cristydb070952012-04-20 14:33:00 +0000314 image_view=AcquireVirtualCacheView(image,exception);
315 noise_view=AcquireAuthenticCacheView(noise_image,exception);
cristy57340e02012-05-05 00:53:23 +0000316 key=GetRandomSecretKey(random_info[0]);
cristy319a1e72010-02-21 15:13:11 +0000317#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy57340e02012-05-05 00:53:23 +0000318 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +0000319 dynamic_number_threads(image,image->columns,image->rows,key == ~0UL)
cristy3ed852e2009-09-05 21:47:34 +0000320#endif
cristybb503372010-05-27 20:51:26 +0000321 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000322 {
cristy5c9e6f22010-09-17 17:31:01 +0000323 const int
324 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +0000325
cristy3ed852e2009-09-05 21:47:34 +0000326 MagickBooleanType
327 sync;
328
cristy4c08aed2011-07-01 19:47:50 +0000329 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000330 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000331
cristybb503372010-05-27 20:51:26 +0000332 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000333 x;
334
cristy4c08aed2011-07-01 19:47:50 +0000335 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000336 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000337
338 if (status == MagickFalse)
339 continue;
340 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +0000341 q=QueueCacheViewAuthenticPixels(noise_view,0,y,noise_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +0000342 exception);
cristy4c08aed2011-07-01 19:47:50 +0000343 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000344 {
345 status=MagickFalse;
346 continue;
347 }
cristybb503372010-05-27 20:51:26 +0000348 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000349 {
cristy850b3072011-10-08 01:38:05 +0000350 register ssize_t
351 i;
352
cristy177e41c2012-04-15 15:08:25 +0000353 if (GetPixelMask(image,p) != 0)
354 {
355 p+=GetPixelChannels(image);
356 q+=GetPixelChannels(noise_image);
357 continue;
358 }
cristy850b3072011-10-08 01:38:05 +0000359 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
360 {
361 PixelChannel
362 channel;
363
364 PixelTrait
365 noise_traits,
366 traits;
367
cristye2a912b2011-12-05 20:02:07 +0000368 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +0000369 traits=GetPixelChannelMapTraits(image,channel);
cristy850b3072011-10-08 01:38:05 +0000370 noise_traits=GetPixelChannelMapTraits(noise_image,channel);
371 if ((traits == UndefinedPixelTrait) ||
372 (noise_traits == UndefinedPixelTrait))
373 continue;
cristy177e41c2012-04-15 15:08:25 +0000374 if ((noise_traits & CopyPixelTrait) != 0)
cristyb2145892011-10-10 00:55:32 +0000375 {
376 SetPixelChannel(noise_image,channel,p[i],q);
377 continue;
378 }
cristy850b3072011-10-08 01:38:05 +0000379 SetPixelChannel(noise_image,channel,ClampToQuantum(
380 GenerateDifferentialNoise(random_info[id],p[i],noise_type,attenuate)),
381 q);
382 }
cristyed231572011-07-14 02:18:59 +0000383 p+=GetPixelChannels(image);
384 q+=GetPixelChannels(noise_image);
cristy3ed852e2009-09-05 21:47:34 +0000385 }
386 sync=SyncCacheViewAuthenticPixels(noise_view,exception);
387 if (sync == MagickFalse)
388 status=MagickFalse;
389 if (image->progress_monitor != (MagickProgressMonitor) NULL)
390 {
391 MagickBooleanType
392 proceed;
393
cristyb5d5f722009-11-04 03:03:49 +0000394#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy319a1e72010-02-21 15:13:11 +0000395 #pragma omp critical (MagickCore_AddNoiseImage)
cristy3ed852e2009-09-05 21:47:34 +0000396#endif
397 proceed=SetImageProgress(image,AddNoiseImageTag,progress++,
398 image->rows);
399 if (proceed == MagickFalse)
400 status=MagickFalse;
401 }
402 }
403 noise_view=DestroyCacheView(noise_view);
404 image_view=DestroyCacheView(image_view);
405 random_info=DestroyRandomInfoThreadSet(random_info);
406 if (status == MagickFalse)
407 noise_image=DestroyImage(noise_image);
408 return(noise_image);
409}
410
411/*
412%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
413% %
414% %
415% %
416% B l u e S h i f t I m a g e %
417% %
418% %
419% %
420%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
421%
422% BlueShiftImage() mutes the colors of the image to simulate a scene at
423% nighttime in the moonlight.
424%
425% The format of the BlueShiftImage method is:
426%
427% Image *BlueShiftImage(const Image *image,const double factor,
428% ExceptionInfo *exception)
429%
430% A description of each parameter follows:
431%
432% o image: the image.
433%
434% o factor: the shift factor.
435%
436% o exception: return any errors or warnings in this structure.
437%
438*/
439MagickExport Image *BlueShiftImage(const Image *image,const double factor,
440 ExceptionInfo *exception)
441{
442#define BlueShiftImageTag "BlueShift/Image"
443
cristyc4c8d132010-01-07 01:58:38 +0000444 CacheView
445 *image_view,
446 *shift_view;
447
cristy3ed852e2009-09-05 21:47:34 +0000448 Image
449 *shift_image;
450
cristy3ed852e2009-09-05 21:47:34 +0000451 MagickBooleanType
452 status;
453
cristybb503372010-05-27 20:51:26 +0000454 MagickOffsetType
455 progress;
456
457 ssize_t
458 y;
459
cristy3ed852e2009-09-05 21:47:34 +0000460 /*
461 Allocate blue shift image.
462 */
463 assert(image != (const Image *) NULL);
464 assert(image->signature == MagickSignature);
465 if (image->debug != MagickFalse)
466 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
467 assert(exception != (ExceptionInfo *) NULL);
468 assert(exception->signature == MagickSignature);
cristya6d7a9b2012-01-18 20:04:48 +0000469 shift_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000470 if (shift_image == (Image *) NULL)
471 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000472 if (SetImageStorageClass(shift_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000473 {
cristy3ed852e2009-09-05 21:47:34 +0000474 shift_image=DestroyImage(shift_image);
475 return((Image *) NULL);
476 }
477 /*
478 Blue-shift DirectClass image.
479 */
480 status=MagickTrue;
481 progress=0;
cristydb070952012-04-20 14:33:00 +0000482 image_view=AcquireVirtualCacheView(image,exception);
483 shift_view=AcquireAuthenticCacheView(shift_image,exception);
cristy319a1e72010-02-21 15:13:11 +0000484#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000485 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +0000486 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +0000487#endif
cristybb503372010-05-27 20:51:26 +0000488 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000489 {
490 MagickBooleanType
491 sync;
492
cristy4c08aed2011-07-01 19:47:50 +0000493 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000494 pixel;
495
496 Quantum
497 quantum;
498
cristy4c08aed2011-07-01 19:47:50 +0000499 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000500 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000501
cristybb503372010-05-27 20:51:26 +0000502 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000503 x;
504
cristy4c08aed2011-07-01 19:47:50 +0000505 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000506 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000507
508 if (status == MagickFalse)
509 continue;
510 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
511 q=QueueCacheViewAuthenticPixels(shift_view,0,y,shift_image->columns,1,
512 exception);
cristy4c08aed2011-07-01 19:47:50 +0000513 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000514 {
515 status=MagickFalse;
516 continue;
517 }
cristybb503372010-05-27 20:51:26 +0000518 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000519 {
cristy4c08aed2011-07-01 19:47:50 +0000520 quantum=GetPixelRed(image,p);
521 if (GetPixelGreen(image,p) < quantum)
522 quantum=GetPixelGreen(image,p);
523 if (GetPixelBlue(image,p) < quantum)
524 quantum=GetPixelBlue(image,p);
525 pixel.red=0.5*(GetPixelRed(image,p)+factor*quantum);
526 pixel.green=0.5*(GetPixelGreen(image,p)+factor*quantum);
527 pixel.blue=0.5*(GetPixelBlue(image,p)+factor*quantum);
528 quantum=GetPixelRed(image,p);
529 if (GetPixelGreen(image,p) > quantum)
530 quantum=GetPixelGreen(image,p);
531 if (GetPixelBlue(image,p) > quantum)
532 quantum=GetPixelBlue(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000533 pixel.red=0.5*(pixel.red+factor*quantum);
534 pixel.green=0.5*(pixel.green+factor*quantum);
535 pixel.blue=0.5*(pixel.blue+factor*quantum);
cristy4c08aed2011-07-01 19:47:50 +0000536 SetPixelRed(shift_image,ClampToQuantum(pixel.red),q);
537 SetPixelGreen(shift_image,ClampToQuantum(pixel.green),q);
538 SetPixelBlue(shift_image,ClampToQuantum(pixel.blue),q);
cristyed231572011-07-14 02:18:59 +0000539 p+=GetPixelChannels(image);
540 q+=GetPixelChannels(shift_image);
cristy3ed852e2009-09-05 21:47:34 +0000541 }
542 sync=SyncCacheViewAuthenticPixels(shift_view,exception);
543 if (sync == MagickFalse)
544 status=MagickFalse;
545 if (image->progress_monitor != (MagickProgressMonitor) NULL)
546 {
547 MagickBooleanType
548 proceed;
549
cristy319a1e72010-02-21 15:13:11 +0000550#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +0000551 #pragma omp critical (MagickCore_BlueShiftImage)
cristy3ed852e2009-09-05 21:47:34 +0000552#endif
553 proceed=SetImageProgress(image,BlueShiftImageTag,progress++,
554 image->rows);
555 if (proceed == MagickFalse)
556 status=MagickFalse;
557 }
558 }
559 image_view=DestroyCacheView(image_view);
560 shift_view=DestroyCacheView(shift_view);
561 if (status == MagickFalse)
562 shift_image=DestroyImage(shift_image);
563 return(shift_image);
564}
565
566/*
567%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
568% %
569% %
570% %
571% C h a r c o a l I m a g e %
572% %
573% %
574% %
575%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
576%
577% CharcoalImage() creates a new image that is a copy of an existing one with
578% the edge highlighted. It allocates the memory necessary for the new Image
579% structure and returns a pointer to the new image.
580%
581% The format of the CharcoalImage method is:
582%
583% Image *CharcoalImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +0000584% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000585%
586% A description of each parameter follows:
587%
588% o image: the image.
589%
590% o radius: the radius of the pixel neighborhood.
591%
592% o sigma: the standard deviation of the Gaussian, in pixels.
593%
594% o exception: return any errors or warnings in this structure.
595%
596*/
597MagickExport Image *CharcoalImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +0000598 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000599{
600 Image
601 *charcoal_image,
602 *clone_image,
603 *edge_image;
604
605 assert(image != (Image *) NULL);
606 assert(image->signature == MagickSignature);
607 if (image->debug != MagickFalse)
608 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
609 assert(exception != (ExceptionInfo *) NULL);
610 assert(exception->signature == MagickSignature);
611 clone_image=CloneImage(image,0,0,MagickTrue,exception);
612 if (clone_image == (Image *) NULL)
613 return((Image *) NULL);
cristy018f07f2011-09-04 21:15:19 +0000614 (void) SetImageType(clone_image,GrayscaleType,exception);
cristy8ae632d2011-09-05 17:29:53 +0000615 edge_image=EdgeImage(clone_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +0000616 clone_image=DestroyImage(clone_image);
617 if (edge_image == (Image *) NULL)
618 return((Image *) NULL);
cristyaa2c16c2012-03-25 22:21:35 +0000619 charcoal_image=BlurImage(edge_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +0000620 edge_image=DestroyImage(edge_image);
621 if (charcoal_image == (Image *) NULL)
622 return((Image *) NULL);
cristye23ec9d2011-08-16 18:15:40 +0000623 (void) NormalizeImage(charcoal_image,exception);
cristyb3e7c6c2011-07-24 01:43:55 +0000624 (void) NegateImage(charcoal_image,MagickFalse,exception);
cristy018f07f2011-09-04 21:15:19 +0000625 (void) SetImageType(charcoal_image,GrayscaleType,exception);
cristy3ed852e2009-09-05 21:47:34 +0000626 return(charcoal_image);
627}
628
629/*
630%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
631% %
632% %
633% %
634% C o l o r i z e I m a g e %
635% %
636% %
637% %
638%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
639%
640% ColorizeImage() blends the fill color with each pixel in the image.
641% A percentage blend is specified with opacity. Control the application
642% of different color components by specifying a different percentage for
643% each component (e.g. 90/100/10 is 90% red, 100% green, and 10% blue).
644%
645% The format of the ColorizeImage method is:
646%
cristyc7e6ff62011-10-03 13:46:11 +0000647% Image *ColorizeImage(const Image *image,const char *blend,
648% const PixelInfo *colorize,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000649%
650% A description of each parameter follows:
651%
652% o image: the image.
653%
cristyc7e6ff62011-10-03 13:46:11 +0000654% o blend: A character string indicating the level of blending as a
cristy3ed852e2009-09-05 21:47:34 +0000655% percentage.
656%
657% o colorize: A color value.
658%
659% o exception: return any errors or warnings in this structure.
660%
661*/
cristyc7e6ff62011-10-03 13:46:11 +0000662MagickExport Image *ColorizeImage(const Image *image,const char *blend,
663 const PixelInfo *colorize,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000664{
665#define ColorizeImageTag "Colorize/Image"
cristy20c3aed2012-04-10 01:06:21 +0000666#define Colorize(pixel,blend_percentage,colorize) \
667 (pixel)=((pixel)*(100.0-(blend_percentage))+(colorize)*(blend_percentage))/100.0;
cristy3ed852e2009-09-05 21:47:34 +0000668
cristyc4c8d132010-01-07 01:58:38 +0000669 CacheView
670 *colorize_view,
671 *image_view;
672
cristy3ed852e2009-09-05 21:47:34 +0000673 GeometryInfo
674 geometry_info;
675
676 Image
677 *colorize_image;
678
cristy3ed852e2009-09-05 21:47:34 +0000679 MagickBooleanType
680 status;
681
cristybb503372010-05-27 20:51:26 +0000682 MagickOffsetType
683 progress;
684
cristy3ed852e2009-09-05 21:47:34 +0000685 MagickStatusType
686 flags;
687
cristyc7e6ff62011-10-03 13:46:11 +0000688 PixelInfo
cristy20c3aed2012-04-10 01:06:21 +0000689 blend_percentage;
cristyc7e6ff62011-10-03 13:46:11 +0000690
cristybb503372010-05-27 20:51:26 +0000691 ssize_t
692 y;
693
cristy3ed852e2009-09-05 21:47:34 +0000694 /*
695 Allocate colorized image.
696 */
697 assert(image != (const Image *) NULL);
698 assert(image->signature == MagickSignature);
699 if (image->debug != MagickFalse)
700 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
701 assert(exception != (ExceptionInfo *) NULL);
702 assert(exception->signature == MagickSignature);
cristy768165d2012-04-09 15:01:35 +0000703 colorize_image=CloneImage(image,image->columns,image->rows,MagickTrue,
704 exception);
705 if (colorize_image == (Image *) NULL)
706 return((Image *) NULL);
707 if (SetImageStorageClass(colorize_image,DirectClass,exception) == MagickFalse)
708 {
709 colorize_image=DestroyImage(colorize_image);
710 return((Image *) NULL);
711 }
cristy9b7a4fc2012-04-08 22:26:56 +0000712 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
cristy20c3aed2012-04-10 01:06:21 +0000713 (IsPixelInfoGray(colorize) != MagickFalse))
cristyb09db112012-07-11 12:04:31 +0000714 (void) SetImageColorspace(colorize_image,RGBColorspace,exception);
cristy20c3aed2012-04-10 01:06:21 +0000715 if ((colorize_image->matte == MagickFalse) &&
716 (colorize->matte != MagickFalse))
cristy768165d2012-04-09 15:01:35 +0000717 (void) SetImageAlpha(colorize_image,OpaqueAlpha,exception);
718 if (blend == (const char *) NULL)
719 return(colorize_image);
cristy20c3aed2012-04-10 01:06:21 +0000720 GetPixelInfo(image,&blend_percentage);
721 flags=ParseGeometry(blend,&geometry_info);
722 blend_percentage.red=geometry_info.rho;
723 blend_percentage.green=geometry_info.rho;
724 blend_percentage.blue=geometry_info.rho;
725 blend_percentage.black=geometry_info.rho;
cristy3ee7de52012-04-14 23:40:47 +0000726 blend_percentage.alpha=geometry_info.rho;
cristy20c3aed2012-04-10 01:06:21 +0000727 if ((flags & SigmaValue) != 0)
728 blend_percentage.green=geometry_info.sigma;
729 if ((flags & XiValue) != 0)
730 blend_percentage.blue=geometry_info.xi;
731 if ((flags & PsiValue) != 0)
732 blend_percentage.alpha=geometry_info.psi;
733 if (blend_percentage.colorspace == CMYKColorspace)
734 {
735 if ((flags & PsiValue) != 0)
736 blend_percentage.black=geometry_info.psi;
737 if ((flags & ChiValue) != 0)
738 blend_percentage.alpha=geometry_info.chi;
739 }
cristy3ed852e2009-09-05 21:47:34 +0000740 /*
741 Colorize DirectClass image.
742 */
743 status=MagickTrue;
744 progress=0;
cristydb070952012-04-20 14:33:00 +0000745 image_view=AcquireVirtualCacheView(image,exception);
746 colorize_view=AcquireAuthenticCacheView(colorize_image,exception);
cristy319a1e72010-02-21 15:13:11 +0000747#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000748 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +0000749 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +0000750#endif
cristybb503372010-05-27 20:51:26 +0000751 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000752 {
753 MagickBooleanType
754 sync;
755
cristyf9bf43e2012-04-07 18:13:07 +0000756 PixelInfo
757 pixel;
758
cristy4c08aed2011-07-01 19:47:50 +0000759 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000760 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000761
cristybb503372010-05-27 20:51:26 +0000762 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000763 x;
764
cristy4c08aed2011-07-01 19:47:50 +0000765 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000766 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000767
768 if (status == MagickFalse)
769 continue;
770 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
771 q=QueueCacheViewAuthenticPixels(colorize_view,0,y,colorize_image->columns,1,
772 exception);
cristy4c08aed2011-07-01 19:47:50 +0000773 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000774 {
775 status=MagickFalse;
776 continue;
777 }
cristy66d3e1f2012-04-14 23:21:39 +0000778 GetPixelInfo(colorize_image,&pixel);
cristybb503372010-05-27 20:51:26 +0000779 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000780 {
cristyd6803382012-04-10 01:41:25 +0000781 if (GetPixelMask(colorize_image,q) != 0)
782 {
783 p+=GetPixelChannels(image);
784 q+=GetPixelChannels(colorize_image);
785 continue;
786 }
cristyf9bf43e2012-04-07 18:13:07 +0000787 GetPixelInfoPixel(image,p,&pixel);
cristy20c3aed2012-04-10 01:06:21 +0000788 Colorize(pixel.red,blend_percentage.red,colorize->red);
789 Colorize(pixel.green,blend_percentage.green,colorize->green);
790 Colorize(pixel.blue,blend_percentage.blue,colorize->blue);
791 Colorize(pixel.black,blend_percentage.black,colorize->black);
792 Colorize(pixel.alpha,blend_percentage.alpha,colorize->alpha);
cristyf9bf43e2012-04-07 18:13:07 +0000793 SetPixelInfoPixel(colorize_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +0000794 p+=GetPixelChannels(image);
795 q+=GetPixelChannels(colorize_image);
cristy3ed852e2009-09-05 21:47:34 +0000796 }
797 sync=SyncCacheViewAuthenticPixels(colorize_view,exception);
798 if (sync == MagickFalse)
799 status=MagickFalse;
800 if (image->progress_monitor != (MagickProgressMonitor) NULL)
801 {
802 MagickBooleanType
803 proceed;
804
cristy319a1e72010-02-21 15:13:11 +0000805#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +0000806 #pragma omp critical (MagickCore_ColorizeImage)
cristy3ed852e2009-09-05 21:47:34 +0000807#endif
808 proceed=SetImageProgress(image,ColorizeImageTag,progress++,image->rows);
809 if (proceed == MagickFalse)
810 status=MagickFalse;
811 }
812 }
813 image_view=DestroyCacheView(image_view);
814 colorize_view=DestroyCacheView(colorize_view);
815 if (status == MagickFalse)
816 colorize_image=DestroyImage(colorize_image);
817 return(colorize_image);
818}
819
820/*
821%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
822% %
823% %
824% %
cristye6365592010-04-02 17:31:23 +0000825% C o l o r M a t r i x I m a g e %
826% %
827% %
828% %
829%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
830%
831% ColorMatrixImage() applies color transformation to an image. This method
832% permits saturation changes, hue rotation, luminance to alpha, and various
833% other effects. Although variable-sized transformation matrices can be used,
834% typically one uses a 5x5 matrix for an RGBA image and a 6x6 for CMYKA
835% (or RGBA with offsets). The matrix is similar to those used by Adobe Flash
836% except offsets are in column 6 rather than 5 (in support of CMYKA images)
837% and offsets are normalized (divide Flash offset by 255).
838%
839% The format of the ColorMatrixImage method is:
840%
841% Image *ColorMatrixImage(const Image *image,
842% const KernelInfo *color_matrix,ExceptionInfo *exception)
843%
844% A description of each parameter follows:
845%
846% o image: the image.
847%
848% o color_matrix: the color matrix.
849%
850% o exception: return any errors or warnings in this structure.
851%
852*/
anthonyfd706f92012-01-19 04:22:02 +0000853/* FUTURE: modify to make use of a MagickMatrix Mutliply function
854 That should be provided in "matrix.c"
855 (ASIDE: actually distorts should do this too but currently doesn't)
856*/
857
cristye6365592010-04-02 17:31:23 +0000858MagickExport Image *ColorMatrixImage(const Image *image,
859 const KernelInfo *color_matrix,ExceptionInfo *exception)
860{
861#define ColorMatrixImageTag "ColorMatrix/Image"
862
863 CacheView
864 *color_view,
865 *image_view;
866
867 double
868 ColorMatrix[6][6] =
869 {
870 { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
871 { 0.0, 1.0, 0.0, 0.0, 0.0, 0.0 },
872 { 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 },
873 { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0 },
874 { 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 },
875 { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 }
876 };
877
878 Image
879 *color_image;
880
cristye6365592010-04-02 17:31:23 +0000881 MagickBooleanType
882 status;
883
cristybb503372010-05-27 20:51:26 +0000884 MagickOffsetType
885 progress;
886
887 register ssize_t
cristye6365592010-04-02 17:31:23 +0000888 i;
889
cristybb503372010-05-27 20:51:26 +0000890 ssize_t
891 u,
892 v,
893 y;
894
cristye6365592010-04-02 17:31:23 +0000895 /*
anthonyfd706f92012-01-19 04:22:02 +0000896 Map given color_matrix, into a 6x6 matrix RGBKA and a constant
cristye6365592010-04-02 17:31:23 +0000897 */
898 assert(image != (Image *) NULL);
899 assert(image->signature == MagickSignature);
900 if (image->debug != MagickFalse)
901 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
902 assert(exception != (ExceptionInfo *) NULL);
903 assert(exception->signature == MagickSignature);
904 i=0;
cristybb503372010-05-27 20:51:26 +0000905 for (v=0; v < (ssize_t) color_matrix->height; v++)
906 for (u=0; u < (ssize_t) color_matrix->width; u++)
cristye6365592010-04-02 17:31:23 +0000907 {
908 if ((v < 6) && (u < 6))
909 ColorMatrix[v][u]=color_matrix->values[i];
910 i++;
911 }
912 /*
913 Initialize color image.
914 */
cristy12550e62010-06-07 12:46:40 +0000915 color_image=CloneImage(image,0,0,MagickTrue,exception);
cristye6365592010-04-02 17:31:23 +0000916 if (color_image == (Image *) NULL)
917 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000918 if (SetImageStorageClass(color_image,DirectClass,exception) == MagickFalse)
cristye6365592010-04-02 17:31:23 +0000919 {
cristye6365592010-04-02 17:31:23 +0000920 color_image=DestroyImage(color_image);
921 return((Image *) NULL);
922 }
923 if (image->debug != MagickFalse)
924 {
925 char
926 format[MaxTextExtent],
927 *message;
928
929 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
930 " ColorMatrix image with color matrix:");
931 message=AcquireString("");
932 for (v=0; v < 6; v++)
933 {
934 *message='\0';
cristyb51dff52011-05-19 16:55:47 +0000935 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
cristye6365592010-04-02 17:31:23 +0000936 (void) ConcatenateString(&message,format);
937 for (u=0; u < 6; u++)
938 {
cristyb51dff52011-05-19 16:55:47 +0000939 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",
cristye6365592010-04-02 17:31:23 +0000940 ColorMatrix[v][u]);
941 (void) ConcatenateString(&message,format);
942 }
943 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
944 }
945 message=DestroyString(message);
946 }
947 /*
anthonyfd706f92012-01-19 04:22:02 +0000948 Apply the ColorMatrix to image.
cristye6365592010-04-02 17:31:23 +0000949 */
950 status=MagickTrue;
951 progress=0;
cristydb070952012-04-20 14:33:00 +0000952 image_view=AcquireVirtualCacheView(image,exception);
953 color_view=AcquireAuthenticCacheView(color_image,exception);
cristye6365592010-04-02 17:31:23 +0000954#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000955 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +0000956 dynamic_number_threads(image,image->columns,image->rows,1)
cristye6365592010-04-02 17:31:23 +0000957#endif
cristybb503372010-05-27 20:51:26 +0000958 for (y=0; y < (ssize_t) image->rows; y++)
cristye6365592010-04-02 17:31:23 +0000959 {
cristyfcc25d92012-02-19 23:06:48 +0000960 PixelInfo
cristye6365592010-04-02 17:31:23 +0000961 pixel;
962
cristy4c08aed2011-07-01 19:47:50 +0000963 register const Quantum
cristye6365592010-04-02 17:31:23 +0000964 *restrict p;
965
cristy4c08aed2011-07-01 19:47:50 +0000966 register Quantum
967 *restrict q;
968
cristybb503372010-05-27 20:51:26 +0000969 register ssize_t
cristye6365592010-04-02 17:31:23 +0000970 x;
971
cristye6365592010-04-02 17:31:23 +0000972 if (status == MagickFalse)
973 continue;
974 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
975 q=GetCacheViewAuthenticPixels(color_view,0,y,color_image->columns,1,
976 exception);
cristy4c08aed2011-07-01 19:47:50 +0000977 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristye6365592010-04-02 17:31:23 +0000978 {
979 status=MagickFalse;
980 continue;
981 }
cristyfcc25d92012-02-19 23:06:48 +0000982 GetPixelInfo(image,&pixel);
cristybb503372010-05-27 20:51:26 +0000983 for (x=0; x < (ssize_t) image->columns; x++)
cristye6365592010-04-02 17:31:23 +0000984 {
cristybb503372010-05-27 20:51:26 +0000985 register ssize_t
cristye6365592010-04-02 17:31:23 +0000986 v;
987
cristybb503372010-05-27 20:51:26 +0000988 size_t
cristye6365592010-04-02 17:31:23 +0000989 height;
990
cristyfcc25d92012-02-19 23:06:48 +0000991 GetPixelInfoPixel(image,p,&pixel);
cristye6365592010-04-02 17:31:23 +0000992 height=color_matrix->height > 6 ? 6UL : color_matrix->height;
cristybb503372010-05-27 20:51:26 +0000993 for (v=0; v < (ssize_t) height; v++)
cristye6365592010-04-02 17:31:23 +0000994 {
cristyfcc25d92012-02-19 23:06:48 +0000995 MagickRealType
996 sum;
997
998 sum=ColorMatrix[v][0]*GetPixelRed(image,p)+ColorMatrix[v][1]*
cristy4c08aed2011-07-01 19:47:50 +0000999 GetPixelGreen(image,p)+ColorMatrix[v][2]*GetPixelBlue(image,p);
cristye6365592010-04-02 17:31:23 +00001000 if (image->colorspace == CMYKColorspace)
cristyfcc25d92012-02-19 23:06:48 +00001001 sum+=ColorMatrix[v][3]*GetPixelBlack(image,p);
cristy4c08aed2011-07-01 19:47:50 +00001002 if (image->matte != MagickFalse)
cristyfcc25d92012-02-19 23:06:48 +00001003 sum+=ColorMatrix[v][4]*GetPixelAlpha(image,p);
1004 sum+=QuantumRange*ColorMatrix[v][5];
cristye6365592010-04-02 17:31:23 +00001005 switch (v)
1006 {
cristyfcc25d92012-02-19 23:06:48 +00001007 case 0: pixel.red=sum; break;
1008 case 1: pixel.green=sum; break;
1009 case 2: pixel.blue=sum; break;
1010 case 3: pixel.black=sum; break;
1011 case 4: pixel.alpha=sum; break;
1012 default: break;
cristye6365592010-04-02 17:31:23 +00001013 }
1014 }
cristyfcc25d92012-02-19 23:06:48 +00001015 SetPixelInfoPixel(color_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +00001016 p+=GetPixelChannels(image);
1017 q+=GetPixelChannels(color_image);
cristye6365592010-04-02 17:31:23 +00001018 }
1019 if (SyncCacheViewAuthenticPixels(color_view,exception) == MagickFalse)
1020 status=MagickFalse;
1021 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1022 {
1023 MagickBooleanType
1024 proceed;
1025
1026#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00001027 #pragma omp critical (MagickCore_ColorMatrixImage)
cristye6365592010-04-02 17:31:23 +00001028#endif
1029 proceed=SetImageProgress(image,ColorMatrixImageTag,progress++,
1030 image->rows);
1031 if (proceed == MagickFalse)
1032 status=MagickFalse;
1033 }
1034 }
1035 color_view=DestroyCacheView(color_view);
1036 image_view=DestroyCacheView(image_view);
1037 if (status == MagickFalse)
1038 color_image=DestroyImage(color_image);
1039 return(color_image);
1040}
1041
1042/*
1043%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1044% %
1045% %
1046% %
cristy3ed852e2009-09-05 21:47:34 +00001047+ D e s t r o y F x I n f o %
1048% %
1049% %
1050% %
1051%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1052%
1053% DestroyFxInfo() deallocates memory associated with an FxInfo structure.
1054%
1055% The format of the DestroyFxInfo method is:
1056%
1057% ImageInfo *DestroyFxInfo(ImageInfo *fx_info)
1058%
1059% A description of each parameter follows:
1060%
1061% o fx_info: the fx info.
1062%
1063*/
cristy7832dc22011-09-05 01:21:53 +00001064MagickPrivate FxInfo *DestroyFxInfo(FxInfo *fx_info)
cristy3ed852e2009-09-05 21:47:34 +00001065{
cristybb503372010-05-27 20:51:26 +00001066 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001067 i;
1068
1069 fx_info->exception=DestroyExceptionInfo(fx_info->exception);
1070 fx_info->expression=DestroyString(fx_info->expression);
1071 fx_info->symbols=DestroySplayTree(fx_info->symbols);
1072 fx_info->colors=DestroySplayTree(fx_info->colors);
cristy0ea377f2011-03-24 00:54:19 +00001073 for (i=(ssize_t) GetImageListLength(fx_info->images)-1; i >= 0; i--)
cristyd76c51e2011-03-26 00:21:26 +00001074 fx_info->view[i]=DestroyCacheView(fx_info->view[i]);
1075 fx_info->view=(CacheView **) RelinquishMagickMemory(fx_info->view);
cristy3ed852e2009-09-05 21:47:34 +00001076 fx_info->random_info=DestroyRandomInfo(fx_info->random_info);
1077 fx_info=(FxInfo *) RelinquishMagickMemory(fx_info);
1078 return(fx_info);
1079}
1080
1081/*
1082%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1083% %
1084% %
1085% %
cristy3ed852e2009-09-05 21:47:34 +00001086+ F x E v a l u a t e C h a n n e l E x p r e s s i o n %
1087% %
1088% %
1089% %
1090%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1091%
1092% FxEvaluateChannelExpression() evaluates an expression and returns the
1093% results.
1094%
1095% The format of the FxEvaluateExpression method is:
1096%
1097% MagickRealType FxEvaluateChannelExpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00001098% const PixelChannel channel,const ssize_t x,const ssize_t y,
cristy3ed852e2009-09-05 21:47:34 +00001099% MagickRealType *alpha,Exceptioninfo *exception)
1100% MagickRealType FxEvaluateExpression(FxInfo *fx_info,
1101% MagickRealType *alpha,Exceptioninfo *exception)
1102%
1103% A description of each parameter follows:
1104%
1105% o fx_info: the fx info.
1106%
1107% o channel: the channel.
1108%
1109% o x,y: the pixel position.
1110%
1111% o alpha: the result.
1112%
1113% o exception: return any errors or warnings in this structure.
1114%
1115*/
1116
cristy351842f2010-03-07 15:27:38 +00001117static inline double MagickMax(const double x,const double y)
1118{
1119 if (x > y)
1120 return(x);
1121 return(y);
1122}
1123
1124static inline double MagickMin(const double x,const double y)
1125{
1126 if (x < y)
1127 return(x);
1128 return(y);
1129}
1130
cristy3ed852e2009-09-05 21:47:34 +00001131static MagickRealType FxChannelStatistics(FxInfo *fx_info,const Image *image,
cristy0568ffc2011-07-25 16:54:14 +00001132 PixelChannel channel,const char *symbol,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001133{
1134 char
1135 key[MaxTextExtent],
1136 statistic[MaxTextExtent];
1137
1138 const char
1139 *value;
1140
1141 register const char
1142 *p;
1143
1144 for (p=symbol; (*p != '.') && (*p != '\0'); p++) ;
1145 if (*p == '.')
1146 switch (*++p) /* e.g. depth.r */
1147 {
cristy541ae572011-08-05 19:08:59 +00001148 case 'r': channel=RedPixelChannel; break;
1149 case 'g': channel=GreenPixelChannel; break;
1150 case 'b': channel=BluePixelChannel; break;
1151 case 'c': channel=CyanPixelChannel; break;
1152 case 'm': channel=MagentaPixelChannel; break;
1153 case 'y': channel=YellowPixelChannel; break;
1154 case 'k': channel=BlackPixelChannel; break;
cristy3ed852e2009-09-05 21:47:34 +00001155 default: break;
1156 }
cristyb51dff52011-05-19 16:55:47 +00001157 (void) FormatLocaleString(key,MaxTextExtent,"%p.%.20g.%s",(void *) image,
cristye8c25f92010-06-03 00:53:06 +00001158 (double) channel,symbol);
cristy3ed852e2009-09-05 21:47:34 +00001159 value=(const char *) GetValueFromSplayTree(fx_info->symbols,key);
1160 if (value != (const char *) NULL)
cristydbdd0e32011-11-04 23:29:40 +00001161 return(QuantumScale*StringToDouble(value,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001162 (void) DeleteNodeFromSplayTree(fx_info->symbols,key);
1163 if (LocaleNCompare(symbol,"depth",5) == 0)
1164 {
cristybb503372010-05-27 20:51:26 +00001165 size_t
cristy3ed852e2009-09-05 21:47:34 +00001166 depth;
1167
cristyfefab1b2011-07-05 00:33:22 +00001168 depth=GetImageDepth(image,exception);
1169 (void) FormatLocaleString(statistic,MaxTextExtent,"%.20g",(double) depth);
cristy3ed852e2009-09-05 21:47:34 +00001170 }
1171 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1172 {
1173 double
1174 kurtosis,
1175 skewness;
1176
cristyd42d9952011-07-08 14:21:50 +00001177 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
cristyb51dff52011-05-19 16:55:47 +00001178 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",kurtosis);
cristy3ed852e2009-09-05 21:47:34 +00001179 }
1180 if (LocaleNCompare(symbol,"maxima",6) == 0)
1181 {
1182 double
1183 maxima,
1184 minima;
1185
cristyd42d9952011-07-08 14:21:50 +00001186 (void) GetImageRange(image,&minima,&maxima,exception);
cristyb51dff52011-05-19 16:55:47 +00001187 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",maxima);
cristy3ed852e2009-09-05 21:47:34 +00001188 }
1189 if (LocaleNCompare(symbol,"mean",4) == 0)
1190 {
1191 double
1192 mean,
1193 standard_deviation;
1194
cristyd42d9952011-07-08 14:21:50 +00001195 (void) GetImageMean(image,&mean,&standard_deviation,exception);
cristyb51dff52011-05-19 16:55:47 +00001196 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",mean);
cristy3ed852e2009-09-05 21:47:34 +00001197 }
1198 if (LocaleNCompare(symbol,"minima",6) == 0)
1199 {
1200 double
1201 maxima,
1202 minima;
1203
cristyd42d9952011-07-08 14:21:50 +00001204 (void) GetImageRange(image,&minima,&maxima,exception);
cristyb51dff52011-05-19 16:55:47 +00001205 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",minima);
cristy3ed852e2009-09-05 21:47:34 +00001206 }
1207 if (LocaleNCompare(symbol,"skewness",8) == 0)
1208 {
1209 double
1210 kurtosis,
1211 skewness;
1212
cristyd42d9952011-07-08 14:21:50 +00001213 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
cristyb51dff52011-05-19 16:55:47 +00001214 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",skewness);
cristy3ed852e2009-09-05 21:47:34 +00001215 }
1216 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1217 {
1218 double
1219 mean,
1220 standard_deviation;
1221
cristyd42d9952011-07-08 14:21:50 +00001222 (void) GetImageMean(image,&mean,&standard_deviation,exception);
cristyb51dff52011-05-19 16:55:47 +00001223 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",
cristy3ed852e2009-09-05 21:47:34 +00001224 standard_deviation);
1225 }
1226 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(key),
1227 ConstantString(statistic));
cristydbdd0e32011-11-04 23:29:40 +00001228 return(QuantumScale*StringToDouble(statistic,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001229}
1230
1231static MagickRealType
cristy0568ffc2011-07-25 16:54:14 +00001232 FxEvaluateSubexpression(FxInfo *,const PixelChannel,const ssize_t,
cristye85007d2010-06-06 22:51:36 +00001233 const ssize_t,const char *,MagickRealType *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +00001234
cristyb0aad4c2011-11-02 19:30:35 +00001235static MagickOffsetType FxGCD(MagickOffsetType alpha,MagickOffsetType beta)
1236{
1237 if (beta != 0)
1238 return(FxGCD(beta,alpha % beta));
1239 return(alpha);
1240}
1241
cristy3ed852e2009-09-05 21:47:34 +00001242static inline const char *FxSubexpression(const char *expression,
1243 ExceptionInfo *exception)
1244{
1245 const char
1246 *subexpression;
1247
cristybb503372010-05-27 20:51:26 +00001248 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001249 level;
1250
1251 level=0;
1252 subexpression=expression;
1253 while ((*subexpression != '\0') &&
1254 ((level != 1) || (strchr(")",(int) *subexpression) == (char *) NULL)))
1255 {
1256 if (strchr("(",(int) *subexpression) != (char *) NULL)
1257 level++;
1258 else
1259 if (strchr(")",(int) *subexpression) != (char *) NULL)
1260 level--;
1261 subexpression++;
1262 }
1263 if (*subexpression == '\0')
1264 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
anthonye5b39652012-04-21 05:37:29 +00001265 "UnbalancedParenthesis","'%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00001266 return(subexpression);
1267}
1268
cristy0568ffc2011-07-25 16:54:14 +00001269static MagickRealType FxGetSymbol(FxInfo *fx_info,const PixelChannel channel,
cristye85007d2010-06-06 22:51:36 +00001270 const ssize_t x,const ssize_t y,const char *expression,
1271 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001272{
1273 char
1274 *q,
1275 subexpression[MaxTextExtent],
1276 symbol[MaxTextExtent];
1277
1278 const char
1279 *p,
1280 *value;
1281
1282 Image
1283 *image;
1284
cristy4c08aed2011-07-01 19:47:50 +00001285 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001286 pixel;
1287
1288 MagickRealType
1289 alpha,
1290 beta;
1291
1292 PointInfo
1293 point;
1294
cristybb503372010-05-27 20:51:26 +00001295 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001296 i;
1297
1298 size_t
cristy1707c6c2012-01-18 23:30:54 +00001299 length,
cristy3ed852e2009-09-05 21:47:34 +00001300 level;
1301
1302 p=expression;
1303 i=GetImageIndexInList(fx_info->images);
1304 level=0;
1305 point.x=(double) x;
1306 point.y=(double) y;
1307 if (isalpha((int) *(p+1)) == 0)
1308 {
1309 if (strchr("suv",(int) *p) != (char *) NULL)
1310 {
1311 switch (*p)
1312 {
1313 case 's':
1314 default:
1315 {
1316 i=GetImageIndexInList(fx_info->images);
1317 break;
1318 }
1319 case 'u': i=0; break;
1320 case 'v': i=1; break;
1321 }
1322 p++;
1323 if (*p == '[')
1324 {
1325 level++;
1326 q=subexpression;
1327 for (p++; *p != '\0'; )
1328 {
1329 if (*p == '[')
1330 level++;
1331 else
1332 if (*p == ']')
1333 {
1334 level--;
1335 if (level == 0)
1336 break;
1337 }
1338 *q++=(*p++);
1339 }
1340 *q='\0';
1341 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1342 &beta,exception);
cristybb503372010-05-27 20:51:26 +00001343 i=(ssize_t) (alpha+0.5);
cristy3ed852e2009-09-05 21:47:34 +00001344 p++;
1345 }
1346 if (*p == '.')
1347 p++;
1348 }
1349 if ((isalpha((int) *(p+1)) == 0) && (*p == 'p'))
1350 {
1351 p++;
1352 if (*p == '{')
1353 {
1354 level++;
1355 q=subexpression;
1356 for (p++; *p != '\0'; )
1357 {
1358 if (*p == '{')
1359 level++;
1360 else
1361 if (*p == '}')
1362 {
1363 level--;
1364 if (level == 0)
1365 break;
1366 }
1367 *q++=(*p++);
1368 }
1369 *q='\0';
1370 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1371 &beta,exception);
1372 point.x=alpha;
1373 point.y=beta;
1374 p++;
1375 }
1376 else
1377 if (*p == '[')
1378 {
1379 level++;
1380 q=subexpression;
1381 for (p++; *p != '\0'; )
1382 {
1383 if (*p == '[')
1384 level++;
1385 else
1386 if (*p == ']')
1387 {
1388 level--;
1389 if (level == 0)
1390 break;
1391 }
1392 *q++=(*p++);
1393 }
1394 *q='\0';
1395 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1396 &beta,exception);
1397 point.x+=alpha;
1398 point.y+=beta;
1399 p++;
1400 }
1401 if (*p == '.')
1402 p++;
1403 }
1404 }
1405 length=GetImageListLength(fx_info->images);
1406 while (i < 0)
cristybb503372010-05-27 20:51:26 +00001407 i+=(ssize_t) length;
cristy3ed852e2009-09-05 21:47:34 +00001408 i%=length;
1409 image=GetImageFromList(fx_info->images,i);
1410 if (image == (Image *) NULL)
1411 {
1412 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
anthonye5b39652012-04-21 05:37:29 +00001413 "NoSuchImage","'%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00001414 return(0.0);
1415 }
cristy4c08aed2011-07-01 19:47:50 +00001416 GetPixelInfo(image,&pixel);
1417 (void) InterpolatePixelInfo(image,fx_info->view[i],image->interpolate,
cristy4f820712011-04-01 12:35:43 +00001418 point.x,point.y,&pixel,exception);
cristy1707c6c2012-01-18 23:30:54 +00001419 if ((strlen(p) > 2) && (LocaleCompare(p,"intensity") != 0) &&
1420 (LocaleCompare(p,"luminance") != 0) && (LocaleCompare(p,"hue") != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00001421 (LocaleCompare(p,"saturation") != 0) &&
1422 (LocaleCompare(p,"lightness") != 0))
1423 {
1424 char
1425 name[MaxTextExtent];
1426
1427 (void) CopyMagickString(name,p,MaxTextExtent);
1428 for (q=name+(strlen(name)-1); q > name; q--)
1429 {
1430 if (*q == ')')
1431 break;
1432 if (*q == '.')
1433 {
1434 *q='\0';
1435 break;
1436 }
1437 }
1438 if ((strlen(name) > 2) &&
1439 (GetValueFromSplayTree(fx_info->symbols,name) == (const char *) NULL))
1440 {
cristy4c08aed2011-07-01 19:47:50 +00001441 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001442 *color;
1443
cristy4c08aed2011-07-01 19:47:50 +00001444 color=(PixelInfo *) GetValueFromSplayTree(fx_info->colors,name);
1445 if (color != (PixelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001446 {
1447 pixel=(*color);
1448 p+=strlen(name);
1449 }
1450 else
cristy1707c6c2012-01-18 23:30:54 +00001451 {
1452 MagickBooleanType
1453 status;
1454
1455 status=QueryColorCompliance(name,AllCompliance,&pixel,
1456 fx_info->exception);
1457 if (status != MagickFalse)
1458 {
1459 (void) AddValueToSplayTree(fx_info->colors,ConstantString(
1460 name),ClonePixelInfo(&pixel));
1461 p+=strlen(name);
1462 }
1463 }
cristy3ed852e2009-09-05 21:47:34 +00001464 }
1465 }
1466 (void) CopyMagickString(symbol,p,MaxTextExtent);
1467 StripString(symbol);
1468 if (*symbol == '\0')
1469 {
1470 switch (channel)
1471 {
cristy0568ffc2011-07-25 16:54:14 +00001472 case RedPixelChannel: return(QuantumScale*pixel.red);
1473 case GreenPixelChannel: return(QuantumScale*pixel.green);
1474 case BluePixelChannel: return(QuantumScale*pixel.blue);
1475 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001476 {
1477 if (image->colorspace != CMYKColorspace)
1478 {
1479 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00001480 ImageError,"ColorSeparatedImageRequired","'%s'",
cristy3ed852e2009-09-05 21:47:34 +00001481 image->filename);
1482 return(0.0);
1483 }
cristy4c08aed2011-07-01 19:47:50 +00001484 return(QuantumScale*pixel.black);
1485 }
cristy0568ffc2011-07-25 16:54:14 +00001486 case AlphaPixelChannel:
cristy4c08aed2011-07-01 19:47:50 +00001487 {
1488 MagickRealType
1489 alpha;
1490
1491 if (pixel.matte == MagickFalse)
1492 return(1.0);
1493 alpha=(MagickRealType) (QuantumScale*pixel.alpha);
1494 return(alpha);
cristy3ed852e2009-09-05 21:47:34 +00001495 }
cristya382aca2011-12-06 18:22:48 +00001496 case IndexPixelChannel:
1497 return(0.0);
cristyb3a73b52011-07-26 01:34:43 +00001498 case IntensityPixelChannel:
cristyf364ed42010-12-15 01:54:43 +00001499 {
cristy4c08aed2011-07-01 19:47:50 +00001500 return(QuantumScale*GetPixelInfoIntensity(&pixel));
cristyf364ed42010-12-15 01:54:43 +00001501 }
cristy3ed852e2009-09-05 21:47:34 +00001502 default:
1503 break;
1504 }
1505 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
anthonye5b39652012-04-21 05:37:29 +00001506 "UnableToParseExpression","'%s'",p);
cristy3ed852e2009-09-05 21:47:34 +00001507 return(0.0);
1508 }
1509 switch (*symbol)
1510 {
1511 case 'A':
1512 case 'a':
1513 {
1514 if (LocaleCompare(symbol,"a") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001515 return((MagickRealType) (QuantumScale*pixel.alpha));
cristy3ed852e2009-09-05 21:47:34 +00001516 break;
1517 }
1518 case 'B':
1519 case 'b':
1520 {
1521 if (LocaleCompare(symbol,"b") == 0)
1522 return(QuantumScale*pixel.blue);
1523 break;
1524 }
1525 case 'C':
1526 case 'c':
1527 {
1528 if (LocaleNCompare(symbol,"channel",7) == 0)
1529 {
1530 GeometryInfo
1531 channel_info;
1532
1533 MagickStatusType
1534 flags;
1535
1536 flags=ParseGeometry(symbol+7,&channel_info);
1537 if (image->colorspace == CMYKColorspace)
1538 switch (channel)
1539 {
cristy0568ffc2011-07-25 16:54:14 +00001540 case CyanPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001541 {
1542 if ((flags & RhoValue) == 0)
1543 return(0.0);
1544 return(channel_info.rho);
1545 }
cristy0568ffc2011-07-25 16:54:14 +00001546 case MagentaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001547 {
1548 if ((flags & SigmaValue) == 0)
1549 return(0.0);
1550 return(channel_info.sigma);
1551 }
cristy0568ffc2011-07-25 16:54:14 +00001552 case YellowPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001553 {
1554 if ((flags & XiValue) == 0)
1555 return(0.0);
1556 return(channel_info.xi);
1557 }
cristy0568ffc2011-07-25 16:54:14 +00001558 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001559 {
1560 if ((flags & PsiValue) == 0)
1561 return(0.0);
1562 return(channel_info.psi);
1563 }
cristy0568ffc2011-07-25 16:54:14 +00001564 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001565 {
1566 if ((flags & ChiValue) == 0)
1567 return(0.0);
1568 return(channel_info.chi);
1569 }
1570 default:
1571 return(0.0);
1572 }
1573 switch (channel)
1574 {
cristy0568ffc2011-07-25 16:54:14 +00001575 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001576 {
1577 if ((flags & RhoValue) == 0)
1578 return(0.0);
1579 return(channel_info.rho);
1580 }
cristy0568ffc2011-07-25 16:54:14 +00001581 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001582 {
1583 if ((flags & SigmaValue) == 0)
1584 return(0.0);
1585 return(channel_info.sigma);
1586 }
cristy0568ffc2011-07-25 16:54:14 +00001587 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001588 {
1589 if ((flags & XiValue) == 0)
1590 return(0.0);
1591 return(channel_info.xi);
1592 }
cristy0568ffc2011-07-25 16:54:14 +00001593 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001594 {
1595 if ((flags & ChiValue) == 0)
1596 return(0.0);
1597 return(channel_info.chi);
1598 }
cristy0568ffc2011-07-25 16:54:14 +00001599 case AlphaPixelChannel:
cristy4c08aed2011-07-01 19:47:50 +00001600 {
1601 if ((flags & PsiValue) == 0)
1602 return(0.0);
1603 return(channel_info.psi);
1604 }
cristy3ed852e2009-09-05 21:47:34 +00001605 default:
1606 return(0.0);
1607 }
1608 return(0.0);
1609 }
1610 if (LocaleCompare(symbol,"c") == 0)
1611 return(QuantumScale*pixel.red);
1612 break;
1613 }
1614 case 'D':
1615 case 'd':
1616 {
1617 if (LocaleNCompare(symbol,"depth",5) == 0)
1618 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1619 break;
1620 }
1621 case 'G':
1622 case 'g':
1623 {
1624 if (LocaleCompare(symbol,"g") == 0)
1625 return(QuantumScale*pixel.green);
1626 break;
1627 }
1628 case 'K':
1629 case 'k':
1630 {
1631 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1632 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1633 if (LocaleCompare(symbol,"k") == 0)
1634 {
1635 if (image->colorspace != CMYKColorspace)
1636 {
1637 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00001638 OptionError,"ColorSeparatedImageRequired","'%s'",
cristy3ed852e2009-09-05 21:47:34 +00001639 image->filename);
1640 return(0.0);
1641 }
cristy4c08aed2011-07-01 19:47:50 +00001642 return(QuantumScale*pixel.black);
cristy3ed852e2009-09-05 21:47:34 +00001643 }
1644 break;
1645 }
1646 case 'H':
1647 case 'h':
1648 {
1649 if (LocaleCompare(symbol,"h") == 0)
1650 return((MagickRealType) image->rows);
1651 if (LocaleCompare(symbol,"hue") == 0)
1652 {
1653 double
1654 hue,
1655 lightness,
1656 saturation;
1657
cristy0a39a5c2012-06-27 12:51:45 +00001658 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
cristyda1f9c12011-10-02 21:39:49 +00001659 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001660 return(hue);
1661 }
1662 break;
1663 }
1664 case 'I':
1665 case 'i':
1666 {
1667 if ((LocaleCompare(symbol,"image.depth") == 0) ||
1668 (LocaleCompare(symbol,"image.minima") == 0) ||
1669 (LocaleCompare(symbol,"image.maxima") == 0) ||
1670 (LocaleCompare(symbol,"image.mean") == 0) ||
1671 (LocaleCompare(symbol,"image.kurtosis") == 0) ||
1672 (LocaleCompare(symbol,"image.skewness") == 0) ||
1673 (LocaleCompare(symbol,"image.standard_deviation") == 0))
1674 return(FxChannelStatistics(fx_info,image,channel,symbol+6,exception));
1675 if (LocaleCompare(symbol,"image.resolution.x") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001676 return(image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00001677 if (LocaleCompare(symbol,"image.resolution.y") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001678 return(image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001679 if (LocaleCompare(symbol,"intensity") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001680 return(QuantumScale*GetPixelInfoIntensity(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001681 if (LocaleCompare(symbol,"i") == 0)
1682 return((MagickRealType) x);
1683 break;
1684 }
1685 case 'J':
1686 case 'j':
1687 {
1688 if (LocaleCompare(symbol,"j") == 0)
1689 return((MagickRealType) y);
1690 break;
1691 }
1692 case 'L':
1693 case 'l':
1694 {
1695 if (LocaleCompare(symbol,"lightness") == 0)
1696 {
1697 double
1698 hue,
1699 lightness,
1700 saturation;
1701
cristy0a39a5c2012-06-27 12:51:45 +00001702 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
cristyda1f9c12011-10-02 21:39:49 +00001703 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001704 return(lightness);
1705 }
1706 if (LocaleCompare(symbol,"luminance") == 0)
1707 {
1708 double
1709 luminence;
1710
1711 luminence=0.2126*pixel.red+0.7152*pixel.green+0.0722*pixel.blue;
1712 return(QuantumScale*luminence);
1713 }
1714 break;
1715 }
1716 case 'M':
1717 case 'm':
1718 {
1719 if (LocaleNCompare(symbol,"maxima",6) == 0)
1720 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1721 if (LocaleNCompare(symbol,"mean",4) == 0)
1722 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1723 if (LocaleNCompare(symbol,"minima",6) == 0)
1724 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1725 if (LocaleCompare(symbol,"m") == 0)
1726 return(QuantumScale*pixel.blue);
1727 break;
1728 }
1729 case 'N':
1730 case 'n':
1731 {
1732 if (LocaleCompare(symbol,"n") == 0)
anthony374f5dd2011-03-25 10:08:53 +00001733 return((MagickRealType) GetImageListLength(fx_info->images));
cristy3ed852e2009-09-05 21:47:34 +00001734 break;
1735 }
1736 case 'O':
1737 case 'o':
1738 {
1739 if (LocaleCompare(symbol,"o") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001740 return(QuantumScale*pixel.alpha);
cristy3ed852e2009-09-05 21:47:34 +00001741 break;
1742 }
1743 case 'P':
1744 case 'p':
1745 {
1746 if (LocaleCompare(symbol,"page.height") == 0)
1747 return((MagickRealType) image->page.height);
1748 if (LocaleCompare(symbol,"page.width") == 0)
1749 return((MagickRealType) image->page.width);
1750 if (LocaleCompare(symbol,"page.x") == 0)
1751 return((MagickRealType) image->page.x);
1752 if (LocaleCompare(symbol,"page.y") == 0)
1753 return((MagickRealType) image->page.y);
1754 break;
1755 }
1756 case 'R':
1757 case 'r':
1758 {
1759 if (LocaleCompare(symbol,"resolution.x") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001760 return(image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00001761 if (LocaleCompare(symbol,"resolution.y") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001762 return(image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001763 if (LocaleCompare(symbol,"r") == 0)
1764 return(QuantumScale*pixel.red);
1765 break;
1766 }
1767 case 'S':
1768 case 's':
1769 {
1770 if (LocaleCompare(symbol,"saturation") == 0)
1771 {
1772 double
1773 hue,
1774 lightness,
1775 saturation;
1776
cristy0a39a5c2012-06-27 12:51:45 +00001777 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
cristyda1f9c12011-10-02 21:39:49 +00001778 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001779 return(saturation);
1780 }
1781 if (LocaleNCompare(symbol,"skewness",8) == 0)
1782 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1783 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1784 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1785 break;
1786 }
1787 case 'T':
1788 case 't':
1789 {
1790 if (LocaleCompare(symbol,"t") == 0)
cristy5a15b932011-03-26 12:50:33 +00001791 return((MagickRealType) GetImageIndexInList(fx_info->images));
cristy3ed852e2009-09-05 21:47:34 +00001792 break;
1793 }
1794 case 'W':
1795 case 'w':
1796 {
1797 if (LocaleCompare(symbol,"w") == 0)
1798 return((MagickRealType) image->columns);
1799 break;
1800 }
1801 case 'Y':
1802 case 'y':
1803 {
1804 if (LocaleCompare(symbol,"y") == 0)
1805 return(QuantumScale*pixel.green);
1806 break;
1807 }
1808 case 'Z':
1809 case 'z':
1810 {
1811 if (LocaleCompare(symbol,"z") == 0)
1812 {
1813 MagickRealType
1814 depth;
1815
cristyfefab1b2011-07-05 00:33:22 +00001816 depth=(MagickRealType) GetImageDepth(image,fx_info->exception);
cristy3ed852e2009-09-05 21:47:34 +00001817 return(depth);
1818 }
1819 break;
1820 }
1821 default:
1822 break;
1823 }
1824 value=(const char *) GetValueFromSplayTree(fx_info->symbols,symbol);
1825 if (value != (const char *) NULL)
cristydbdd0e32011-11-04 23:29:40 +00001826 return((MagickRealType) StringToDouble(value,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001827 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
anthonye5b39652012-04-21 05:37:29 +00001828 "UnableToParseExpression","'%s'",symbol);
cristy3ed852e2009-09-05 21:47:34 +00001829 return(0.0);
1830}
1831
1832static const char *FxOperatorPrecedence(const char *expression,
1833 ExceptionInfo *exception)
1834{
1835 typedef enum
1836 {
1837 UndefinedPrecedence,
1838 NullPrecedence,
1839 BitwiseComplementPrecedence,
1840 ExponentPrecedence,
cristy116af162010-08-13 01:25:47 +00001841 ExponentialNotationPrecedence,
cristy3ed852e2009-09-05 21:47:34 +00001842 MultiplyPrecedence,
1843 AdditionPrecedence,
1844 ShiftPrecedence,
1845 RelationalPrecedence,
1846 EquivalencyPrecedence,
1847 BitwiseAndPrecedence,
1848 BitwiseOrPrecedence,
1849 LogicalAndPrecedence,
1850 LogicalOrPrecedence,
1851 TernaryPrecedence,
1852 AssignmentPrecedence,
1853 CommaPrecedence,
1854 SeparatorPrecedence
1855 } FxPrecedence;
1856
1857 FxPrecedence
1858 precedence,
1859 target;
1860
1861 register const char
1862 *subexpression;
1863
1864 register int
1865 c;
1866
cristybb503372010-05-27 20:51:26 +00001867 size_t
cristy3ed852e2009-09-05 21:47:34 +00001868 level;
1869
1870 c=0;
1871 level=0;
1872 subexpression=(const char *) NULL;
1873 target=NullPrecedence;
1874 while (*expression != '\0')
1875 {
1876 precedence=UndefinedPrecedence;
1877 if ((isspace((int) ((char) *expression)) != 0) || (c == (int) '@'))
1878 {
1879 expression++;
1880 continue;
1881 }
cristy488fa882010-03-01 22:34:24 +00001882 switch (*expression)
1883 {
1884 case 'A':
1885 case 'a':
cristy3ed852e2009-09-05 21:47:34 +00001886 {
cristyb33454f2011-08-03 02:10:45 +00001887#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00001888 if (LocaleNCompare(expression,"acosh",5) == 0)
1889 {
1890 expression+=5;
1891 break;
1892 }
cristyb33454f2011-08-03 02:10:45 +00001893#endif
1894#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00001895 if (LocaleNCompare(expression,"asinh",5) == 0)
1896 {
1897 expression+=5;
1898 break;
1899 }
cristyb33454f2011-08-03 02:10:45 +00001900#endif
1901#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00001902 if (LocaleNCompare(expression,"atanh",5) == 0)
cristy488fa882010-03-01 22:34:24 +00001903 {
1904 expression+=5;
1905 break;
1906 }
cristyb33454f2011-08-03 02:10:45 +00001907#endif
anthony62838e52012-05-24 12:41:54 +00001908 if (LocaleNCompare(expression,"atan2",5) == 0)
1909 {
1910 expression+=5;
1911 break;
1912 }
cristy488fa882010-03-01 22:34:24 +00001913 break;
cristy3ed852e2009-09-05 21:47:34 +00001914 }
cristy62d455f2011-11-03 11:42:28 +00001915 case 'E':
1916 case 'e':
1917 {
1918 if ((LocaleNCompare(expression,"E+",2) == 0) ||
1919 (LocaleNCompare(expression,"E-",2) == 0))
1920 {
1921 expression+=2; /* scientific notation */
1922 break;
1923 }
1924 }
cristy488fa882010-03-01 22:34:24 +00001925 case 'J':
1926 case 'j':
1927 {
1928 if ((LocaleNCompare(expression,"j0",2) == 0) ||
1929 (LocaleNCompare(expression,"j1",2) == 0))
1930 {
1931 expression+=2;
1932 break;
1933 }
1934 break;
1935 }
cristy2def9322010-06-18 23:59:37 +00001936 case '#':
1937 {
1938 while (isxdigit((int) ((unsigned char) *(expression+1))) != 0)
1939 expression++;
1940 break;
1941 }
cristy488fa882010-03-01 22:34:24 +00001942 default:
1943 break;
1944 }
cristy3ed852e2009-09-05 21:47:34 +00001945 if ((c == (int) '{') || (c == (int) '['))
1946 level++;
1947 else
1948 if ((c == (int) '}') || (c == (int) ']'))
1949 level--;
1950 if (level == 0)
1951 switch ((unsigned char) *expression)
1952 {
1953 case '~':
1954 case '!':
1955 {
1956 precedence=BitwiseComplementPrecedence;
1957 break;
1958 }
1959 case '^':
cristy6621e252010-08-13 00:42:57 +00001960 case '@':
cristy3ed852e2009-09-05 21:47:34 +00001961 {
1962 precedence=ExponentPrecedence;
1963 break;
1964 }
1965 default:
1966 {
1967 if (((c != 0) && ((isdigit((int) ((char) c)) != 0) ||
1968 (strchr(")",c) != (char *) NULL))) &&
1969 (((islower((int) ((char) *expression)) != 0) ||
1970 (strchr("(",(int) *expression) != (char *) NULL)) ||
1971 ((isdigit((int) ((char) c)) == 0) &&
1972 (isdigit((int) ((char) *expression)) != 0))) &&
1973 (strchr("xy",(int) *expression) == (char *) NULL))
1974 precedence=MultiplyPrecedence;
1975 break;
1976 }
1977 case '*':
1978 case '/':
1979 case '%':
1980 {
1981 precedence=MultiplyPrecedence;
1982 break;
1983 }
1984 case '+':
1985 case '-':
1986 {
1987 if ((strchr("(+-/*%:&^|<>~,",c) == (char *) NULL) ||
1988 (isalpha(c) != 0))
1989 precedence=AdditionPrecedence;
1990 break;
1991 }
1992 case LeftShiftOperator:
1993 case RightShiftOperator:
1994 {
1995 precedence=ShiftPrecedence;
1996 break;
1997 }
1998 case '<':
1999 case LessThanEqualOperator:
2000 case GreaterThanEqualOperator:
2001 case '>':
2002 {
2003 precedence=RelationalPrecedence;
2004 break;
2005 }
2006 case EqualOperator:
2007 case NotEqualOperator:
2008 {
2009 precedence=EquivalencyPrecedence;
2010 break;
2011 }
2012 case '&':
2013 {
2014 precedence=BitwiseAndPrecedence;
2015 break;
2016 }
2017 case '|':
2018 {
2019 precedence=BitwiseOrPrecedence;
2020 break;
2021 }
2022 case LogicalAndOperator:
2023 {
2024 precedence=LogicalAndPrecedence;
2025 break;
2026 }
2027 case LogicalOrOperator:
2028 {
2029 precedence=LogicalOrPrecedence;
2030 break;
2031 }
cristy116af162010-08-13 01:25:47 +00002032 case ExponentialNotation:
2033 {
2034 precedence=ExponentialNotationPrecedence;
2035 break;
2036 }
cristy3ed852e2009-09-05 21:47:34 +00002037 case ':':
2038 case '?':
2039 {
2040 precedence=TernaryPrecedence;
2041 break;
2042 }
2043 case '=':
2044 {
2045 precedence=AssignmentPrecedence;
2046 break;
2047 }
2048 case ',':
2049 {
2050 precedence=CommaPrecedence;
2051 break;
2052 }
2053 case ';':
2054 {
2055 precedence=SeparatorPrecedence;
2056 break;
2057 }
2058 }
2059 if ((precedence == BitwiseComplementPrecedence) ||
2060 (precedence == TernaryPrecedence) ||
2061 (precedence == AssignmentPrecedence))
2062 {
2063 if (precedence > target)
2064 {
2065 /*
2066 Right-to-left associativity.
2067 */
2068 target=precedence;
2069 subexpression=expression;
2070 }
2071 }
2072 else
2073 if (precedence >= target)
2074 {
2075 /*
2076 Left-to-right associativity.
2077 */
2078 target=precedence;
2079 subexpression=expression;
2080 }
2081 if (strchr("(",(int) *expression) != (char *) NULL)
2082 expression=FxSubexpression(expression,exception);
2083 c=(int) (*expression++);
2084 }
2085 return(subexpression);
2086}
2087
2088static MagickRealType FxEvaluateSubexpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002089 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristye85007d2010-06-06 22:51:36 +00002090 const char *expression,MagickRealType *beta,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002091{
2092 char
2093 *q,
2094 subexpression[MaxTextExtent];
2095
2096 MagickRealType
2097 alpha,
2098 gamma;
2099
2100 register const char
2101 *p;
2102
2103 *beta=0.0;
2104 if (exception->severity != UndefinedException)
2105 return(0.0);
2106 while (isspace((int) *expression) != 0)
2107 expression++;
2108 if (*expression == '\0')
2109 {
2110 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
anthonye5b39652012-04-21 05:37:29 +00002111 "MissingExpression","'%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00002112 return(0.0);
2113 }
cristy66322f02010-05-17 11:40:48 +00002114 *subexpression='\0';
cristy3ed852e2009-09-05 21:47:34 +00002115 p=FxOperatorPrecedence(expression,exception);
2116 if (p != (const char *) NULL)
2117 {
2118 (void) CopyMagickString(subexpression,expression,(size_t)
2119 (p-expression+1));
2120 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2121 exception);
2122 switch ((unsigned char) *p)
2123 {
2124 case '~':
2125 {
2126 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristybb503372010-05-27 20:51:26 +00002127 *beta=(MagickRealType) (~(size_t) *beta);
cristy3ed852e2009-09-05 21:47:34 +00002128 return(*beta);
2129 }
2130 case '!':
2131 {
2132 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2133 return(*beta == 0.0 ? 1.0 : 0.0);
2134 }
2135 case '^':
2136 {
2137 *beta=pow((double) alpha,(double) FxEvaluateSubexpression(fx_info,
2138 channel,x,y,++p,beta,exception));
2139 return(*beta);
2140 }
2141 case '*':
cristy116af162010-08-13 01:25:47 +00002142 case ExponentialNotation:
cristy3ed852e2009-09-05 21:47:34 +00002143 {
2144 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2145 return(alpha*(*beta));
2146 }
2147 case '/':
2148 {
2149 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2150 if (*beta == 0.0)
2151 {
2152 if (exception->severity == UndefinedException)
2153 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00002154 OptionError,"DivideByZero","'%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00002155 return(0.0);
2156 }
2157 return(alpha/(*beta));
2158 }
2159 case '%':
2160 {
2161 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2162 *beta=fabs(floor(((double) *beta)+0.5));
2163 if (*beta == 0.0)
2164 {
2165 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00002166 OptionError,"DivideByZero","'%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00002167 return(0.0);
2168 }
2169 return(fmod((double) alpha,(double) *beta));
2170 }
2171 case '+':
2172 {
2173 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2174 return(alpha+(*beta));
2175 }
2176 case '-':
2177 {
2178 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2179 return(alpha-(*beta));
2180 }
2181 case LeftShiftOperator:
2182 {
2183 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy1707c6c2012-01-18 23:30:54 +00002184 *beta=(MagickRealType) ((size_t) (alpha+0.5) << (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002185 return(*beta);
2186 }
2187 case RightShiftOperator:
2188 {
2189 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy1707c6c2012-01-18 23:30:54 +00002190 *beta=(MagickRealType) ((size_t) (alpha+0.5) >> (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002191 return(*beta);
2192 }
2193 case '<':
2194 {
2195 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2196 return(alpha < *beta ? 1.0 : 0.0);
2197 }
2198 case LessThanEqualOperator:
2199 {
2200 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2201 return(alpha <= *beta ? 1.0 : 0.0);
2202 }
2203 case '>':
2204 {
2205 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2206 return(alpha > *beta ? 1.0 : 0.0);
2207 }
2208 case GreaterThanEqualOperator:
2209 {
2210 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2211 return(alpha >= *beta ? 1.0 : 0.0);
2212 }
2213 case EqualOperator:
2214 {
2215 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy9b528342012-06-02 00:59:20 +00002216 return(fabs(alpha-(*beta)) < MagickEpsilon ? MagickEpsilon : 0.0);
cristy3ed852e2009-09-05 21:47:34 +00002217 }
2218 case NotEqualOperator:
2219 {
2220 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy972050b2012-06-04 22:09:17 +00002221 return(fabs(alpha-(*beta)) >= MagickEpsilon ? 1.0 : 0.0);
cristy3ed852e2009-09-05 21:47:34 +00002222 }
2223 case '&':
2224 {
2225 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy1707c6c2012-01-18 23:30:54 +00002226 *beta=(MagickRealType) ((size_t) (alpha+0.5) & (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002227 return(*beta);
2228 }
2229 case '|':
2230 {
2231 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy1707c6c2012-01-18 23:30:54 +00002232 *beta=(MagickRealType) ((size_t) (alpha+0.5) | (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002233 return(*beta);
2234 }
2235 case LogicalAndOperator:
2236 {
2237 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2238 *beta=(alpha > 0.0) && (gamma > 0.0) ? 1.0 : 0.0;
2239 return(*beta);
2240 }
2241 case LogicalOrOperator:
2242 {
2243 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2244 *beta=(alpha > 0.0) || (gamma > 0.0) ? 1.0 : 0.0;
2245 return(*beta);
2246 }
2247 case '?':
2248 {
2249 MagickRealType
2250 gamma;
2251
2252 (void) CopyMagickString(subexpression,++p,MaxTextExtent);
2253 q=subexpression;
2254 p=StringToken(":",&q);
2255 if (q == (char *) NULL)
2256 {
2257 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00002258 OptionError,"UnableToParseExpression","'%s'",subexpression);
cristy3ed852e2009-09-05 21:47:34 +00002259 return(0.0);
2260 }
cristy972050b2012-06-04 22:09:17 +00002261 if (fabs((double) alpha) >= MagickEpsilon)
cristy3ed852e2009-09-05 21:47:34 +00002262 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,exception);
2263 else
2264 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,q,beta,exception);
2265 return(gamma);
2266 }
2267 case '=':
2268 {
2269 char
2270 numeric[MaxTextExtent];
2271
2272 q=subexpression;
2273 while (isalpha((int) ((unsigned char) *q)) != 0)
2274 q++;
2275 if (*q != '\0')
2276 {
2277 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00002278 OptionError,"UnableToParseExpression","'%s'",subexpression);
cristy3ed852e2009-09-05 21:47:34 +00002279 return(0.0);
2280 }
2281 ClearMagickException(exception);
2282 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristyb51dff52011-05-19 16:55:47 +00002283 (void) FormatLocaleString(numeric,MaxTextExtent,"%g",(double)
cristy8cd5b312010-01-07 01:10:24 +00002284 *beta);
cristy3ed852e2009-09-05 21:47:34 +00002285 (void) DeleteNodeFromSplayTree(fx_info->symbols,subexpression);
2286 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(
2287 subexpression),ConstantString(numeric));
2288 return(*beta);
2289 }
2290 case ',':
2291 {
2292 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2293 return(alpha);
2294 }
2295 case ';':
2296 {
2297 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2298 return(*beta);
2299 }
2300 default:
2301 {
2302 gamma=alpha*FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,
2303 exception);
2304 return(gamma);
2305 }
2306 }
2307 }
2308 if (strchr("(",(int) *expression) != (char *) NULL)
2309 {
2310 (void) CopyMagickString(subexpression,expression+1,MaxTextExtent);
2311 subexpression[strlen(subexpression)-1]='\0';
2312 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2313 exception);
2314 return(gamma);
2315 }
cristy8b8a3ae2010-10-23 18:49:46 +00002316 switch (*expression)
cristy3ed852e2009-09-05 21:47:34 +00002317 {
2318 case '+':
2319 {
2320 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2321 exception);
2322 return(1.0*gamma);
2323 }
2324 case '-':
2325 {
2326 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2327 exception);
2328 return(-1.0*gamma);
2329 }
2330 case '~':
2331 {
2332 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2333 exception);
cristybb503372010-05-27 20:51:26 +00002334 return((MagickRealType) (~(size_t) (gamma+0.5)));
cristy3ed852e2009-09-05 21:47:34 +00002335 }
2336 case 'A':
2337 case 'a':
2338 {
2339 if (LocaleNCompare(expression,"abs",3) == 0)
2340 {
2341 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2342 exception);
2343 return((MagickRealType) fabs((double) alpha));
2344 }
cristyb33454f2011-08-03 02:10:45 +00002345#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00002346 if (LocaleNCompare(expression,"acosh",5) == 0)
2347 {
2348 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2349 exception);
2350 return((MagickRealType) acosh((double) alpha));
2351 }
cristyb33454f2011-08-03 02:10:45 +00002352#endif
cristy3ed852e2009-09-05 21:47:34 +00002353 if (LocaleNCompare(expression,"acos",4) == 0)
2354 {
2355 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2356 exception);
2357 return((MagickRealType) acos((double) alpha));
2358 }
cristy43c22f42010-03-30 12:34:07 +00002359#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002360 if (LocaleNCompare(expression,"airy",4) == 0)
2361 {
2362 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2363 exception);
2364 if (alpha == 0.0)
cristy2dd03222010-03-30 22:12:11 +00002365 return(1.0);
2366 gamma=2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha);
cristy43c22f42010-03-30 12:34:07 +00002367 return(gamma*gamma);
cristyee56cf12010-03-01 22:17:06 +00002368 }
cristy43c22f42010-03-30 12:34:07 +00002369#endif
cristyb33454f2011-08-03 02:10:45 +00002370#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00002371 if (LocaleNCompare(expression,"asinh",5) == 0)
2372 {
2373 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2374 exception);
2375 return((MagickRealType) asinh((double) alpha));
2376 }
cristyb33454f2011-08-03 02:10:45 +00002377#endif
cristy3ed852e2009-09-05 21:47:34 +00002378 if (LocaleNCompare(expression,"asin",4) == 0)
2379 {
2380 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2381 exception);
2382 return((MagickRealType) asin((double) alpha));
2383 }
2384 if (LocaleNCompare(expression,"alt",3) == 0)
2385 {
2386 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2387 exception);
cristybb503372010-05-27 20:51:26 +00002388 return(((ssize_t) alpha) & 0x01 ? -1.0 : 1.0);
cristy3ed852e2009-09-05 21:47:34 +00002389 }
2390 if (LocaleNCompare(expression,"atan2",5) == 0)
2391 {
2392 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2393 exception);
2394 return((MagickRealType) atan2((double) alpha,(double) *beta));
2395 }
cristyb33454f2011-08-03 02:10:45 +00002396#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00002397 if (LocaleNCompare(expression,"atanh",5) == 0)
2398 {
2399 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2400 exception);
2401 return((MagickRealType) atanh((double) alpha));
2402 }
cristyb33454f2011-08-03 02:10:45 +00002403#endif
cristy3ed852e2009-09-05 21:47:34 +00002404 if (LocaleNCompare(expression,"atan",4) == 0)
2405 {
2406 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2407 exception);
2408 return((MagickRealType) atan((double) alpha));
2409 }
2410 if (LocaleCompare(expression,"a") == 0)
2411 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2412 break;
2413 }
2414 case 'B':
2415 case 'b':
2416 {
2417 if (LocaleCompare(expression,"b") == 0)
2418 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2419 break;
2420 }
2421 case 'C':
2422 case 'c':
2423 {
2424 if (LocaleNCompare(expression,"ceil",4) == 0)
2425 {
2426 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2427 exception);
2428 return((MagickRealType) ceil((double) alpha));
2429 }
2430 if (LocaleNCompare(expression,"cosh",4) == 0)
2431 {
2432 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2433 exception);
2434 return((MagickRealType) cosh((double) alpha));
2435 }
2436 if (LocaleNCompare(expression,"cos",3) == 0)
2437 {
2438 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2439 exception);
2440 return((MagickRealType) cos((double) alpha));
2441 }
2442 if (LocaleCompare(expression,"c") == 0)
2443 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2444 break;
2445 }
2446 case 'D':
2447 case 'd':
2448 {
2449 if (LocaleNCompare(expression,"debug",5) == 0)
2450 {
2451 const char
2452 *type;
2453
2454 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2455 exception);
2456 if (fx_info->images->colorspace == CMYKColorspace)
2457 switch (channel)
2458 {
cristy0568ffc2011-07-25 16:54:14 +00002459 case CyanPixelChannel: type="cyan"; break;
2460 case MagentaPixelChannel: type="magenta"; break;
2461 case YellowPixelChannel: type="yellow"; break;
2462 case AlphaPixelChannel: type="opacity"; break;
2463 case BlackPixelChannel: type="black"; break;
cristy3ed852e2009-09-05 21:47:34 +00002464 default: type="unknown"; break;
2465 }
2466 else
2467 switch (channel)
2468 {
cristy0568ffc2011-07-25 16:54:14 +00002469 case RedPixelChannel: type="red"; break;
2470 case GreenPixelChannel: type="green"; break;
2471 case BluePixelChannel: type="blue"; break;
2472 case AlphaPixelChannel: type="opacity"; break;
cristy3ed852e2009-09-05 21:47:34 +00002473 default: type="unknown"; break;
2474 }
2475 (void) CopyMagickString(subexpression,expression+6,MaxTextExtent);
2476 if (strlen(subexpression) > 1)
2477 subexpression[strlen(subexpression)-1]='\0';
2478 if (fx_info->file != (FILE *) NULL)
cristy1707c6c2012-01-18 23:30:54 +00002479 (void) FormatLocaleFile(fx_info->file,"%s[%.20g,%.20g].%s: "
2480 "%s=%.*g\n",fx_info->images->filename,(double) x,(double) y,type,
2481 subexpression,GetMagickPrecision(),(double) alpha);
cristy3ed852e2009-09-05 21:47:34 +00002482 return(0.0);
2483 }
cristy5597a8d2011-11-04 00:25:32 +00002484 if (LocaleNCompare(expression,"drc",3) == 0)
2485 {
2486 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2487 exception);
2488 return((MagickRealType) (alpha/(*beta*(alpha-1.0)+1.0)));
2489 }
cristy3ed852e2009-09-05 21:47:34 +00002490 break;
2491 }
2492 case 'E':
2493 case 'e':
2494 {
2495 if (LocaleCompare(expression,"epsilon") == 0)
2496 return((MagickRealType) MagickEpsilon);
2497 if (LocaleNCompare(expression,"exp",3) == 0)
2498 {
2499 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2500 exception);
2501 return((MagickRealType) exp((double) alpha));
2502 }
2503 if (LocaleCompare(expression,"e") == 0)
2504 return((MagickRealType) 2.7182818284590452354);
2505 break;
2506 }
2507 case 'F':
2508 case 'f':
2509 {
2510 if (LocaleNCompare(expression,"floor",5) == 0)
2511 {
2512 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2513 exception);
2514 return((MagickRealType) floor((double) alpha));
2515 }
2516 break;
2517 }
2518 case 'G':
2519 case 'g':
2520 {
cristy9eeedea2011-11-02 19:04:05 +00002521 if (LocaleNCompare(expression,"gauss",5) == 0)
2522 {
2523 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2524 exception);
2525 gamma=exp((double) (-alpha*alpha/2.0))/sqrt(2.0*MagickPI);
2526 return((MagickRealType) gamma);
2527 }
cristyb0aad4c2011-11-02 19:30:35 +00002528 if (LocaleNCompare(expression,"gcd",3) == 0)
2529 {
2530 MagickOffsetType
2531 gcd;
2532
2533 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2534 exception);
cristy1707c6c2012-01-18 23:30:54 +00002535 gcd=FxGCD((MagickOffsetType) (alpha+0.5),(MagickOffsetType) (*beta+
2536 0.5));
cristyb0aad4c2011-11-02 19:30:35 +00002537 return((MagickRealType) gcd);
2538 }
cristy3ed852e2009-09-05 21:47:34 +00002539 if (LocaleCompare(expression,"g") == 0)
2540 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2541 break;
2542 }
2543 case 'H':
2544 case 'h':
2545 {
2546 if (LocaleCompare(expression,"h") == 0)
2547 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2548 if (LocaleCompare(expression,"hue") == 0)
2549 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2550 if (LocaleNCompare(expression,"hypot",5) == 0)
2551 {
2552 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2553 exception);
2554 return((MagickRealType) hypot((double) alpha,(double) *beta));
2555 }
2556 break;
2557 }
2558 case 'K':
2559 case 'k':
2560 {
2561 if (LocaleCompare(expression,"k") == 0)
2562 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2563 break;
2564 }
2565 case 'I':
2566 case 'i':
2567 {
2568 if (LocaleCompare(expression,"intensity") == 0)
2569 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2570 if (LocaleNCompare(expression,"int",3) == 0)
2571 {
2572 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2573 exception);
cristy16788e42010-08-13 13:44:26 +00002574 return((MagickRealType) floor(alpha));
cristy3ed852e2009-09-05 21:47:34 +00002575 }
cristy82b20722011-11-05 21:52:36 +00002576#if defined(MAGICKCORE_HAVE_ISNAN)
cristy639399c2011-11-02 19:16:15 +00002577 if (LocaleNCompare(expression,"isnan",5) == 0)
2578 {
2579 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2580 exception);
cristy17a10202011-11-02 19:17:04 +00002581 return((MagickRealType) !!isnan((double) alpha));
cristy639399c2011-11-02 19:16:15 +00002582 }
cristy82b20722011-11-05 21:52:36 +00002583#endif
cristy3ed852e2009-09-05 21:47:34 +00002584 if (LocaleCompare(expression,"i") == 0)
2585 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2586 break;
2587 }
2588 case 'J':
2589 case 'j':
2590 {
2591 if (LocaleCompare(expression,"j") == 0)
2592 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
cristy161b9262010-03-20 19:34:32 +00002593#if defined(MAGICKCORE_HAVE_J0)
cristyee56cf12010-03-01 22:17:06 +00002594 if (LocaleNCompare(expression,"j0",2) == 0)
2595 {
2596 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2597 exception);
2598 return((MagickRealType) j0((double) alpha));
2599 }
cristy161b9262010-03-20 19:34:32 +00002600#endif
2601#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002602 if (LocaleNCompare(expression,"j1",2) == 0)
2603 {
2604 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2605 exception);
2606 return((MagickRealType) j1((double) alpha));
2607 }
cristy161b9262010-03-20 19:34:32 +00002608#endif
cristyaa018fa2010-04-08 23:03:54 +00002609#if defined(MAGICKCORE_HAVE_J1)
cristya6a09e72010-03-02 14:51:02 +00002610 if (LocaleNCompare(expression,"jinc",4) == 0)
2611 {
2612 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2613 exception);
cristy0946a822010-03-12 17:14:58 +00002614 if (alpha == 0.0)
2615 return(1.0);
cristy1707c6c2012-01-18 23:30:54 +00002616 gamma=(MagickRealType) (2.0*j1((double) (MagickPI*alpha))/(MagickPI*
2617 alpha));
cristyfce2f7b2010-03-12 00:29:49 +00002618 return(gamma);
cristya6a09e72010-03-02 14:51:02 +00002619 }
cristyaa018fa2010-04-08 23:03:54 +00002620#endif
cristy3ed852e2009-09-05 21:47:34 +00002621 break;
2622 }
2623 case 'L':
2624 case 'l':
2625 {
2626 if (LocaleNCompare(expression,"ln",2) == 0)
2627 {
2628 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2629 exception);
2630 return((MagickRealType) log((double) alpha));
2631 }
cristyc8ed5322010-08-31 12:07:59 +00002632 if (LocaleNCompare(expression,"logtwo",6) == 0)
cristy3ed852e2009-09-05 21:47:34 +00002633 {
cristyc8ed5322010-08-31 12:07:59 +00002634 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
cristy3ed852e2009-09-05 21:47:34 +00002635 exception);
2636 return((MagickRealType) log10((double) alpha))/log10(2.0);
2637 }
2638 if (LocaleNCompare(expression,"log",3) == 0)
2639 {
2640 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2641 exception);
2642 return((MagickRealType) log10((double) alpha));
2643 }
2644 if (LocaleCompare(expression,"lightness") == 0)
2645 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2646 break;
2647 }
2648 case 'M':
2649 case 'm':
2650 {
2651 if (LocaleCompare(expression,"MaxRGB") == 0)
2652 return((MagickRealType) QuantumRange);
2653 if (LocaleNCompare(expression,"maxima",6) == 0)
2654 break;
2655 if (LocaleNCompare(expression,"max",3) == 0)
cristy984049c2011-11-03 18:34:58 +00002656 {
2657 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2658 exception);
2659 return(alpha > *beta ? alpha : *beta);
2660 }
cristy3ed852e2009-09-05 21:47:34 +00002661 if (LocaleNCompare(expression,"minima",6) == 0)
2662 break;
2663 if (LocaleNCompare(expression,"min",3) == 0)
cristy984049c2011-11-03 18:34:58 +00002664 {
2665 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2666 exception);
2667 return(alpha < *beta ? alpha : *beta);
2668 }
cristy3ed852e2009-09-05 21:47:34 +00002669 if (LocaleNCompare(expression,"mod",3) == 0)
2670 {
2671 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2672 exception);
cristy984049c2011-11-03 18:34:58 +00002673 gamma=alpha-floor((double) (alpha/(*beta)))*(*beta);
2674 return(gamma);
cristy3ed852e2009-09-05 21:47:34 +00002675 }
2676 if (LocaleCompare(expression,"m") == 0)
2677 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2678 break;
2679 }
2680 case 'N':
2681 case 'n':
2682 {
cristyad3502e2011-11-02 19:10:45 +00002683 if (LocaleNCompare(expression,"not",3) == 0)
2684 {
2685 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2686 exception);
2687 return((MagickRealType) (alpha < MagickEpsilon));
2688 }
cristy3ed852e2009-09-05 21:47:34 +00002689 if (LocaleCompare(expression,"n") == 0)
2690 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2691 break;
2692 }
2693 case 'O':
2694 case 'o':
2695 {
2696 if (LocaleCompare(expression,"Opaque") == 0)
2697 return(1.0);
2698 if (LocaleCompare(expression,"o") == 0)
2699 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2700 break;
2701 }
2702 case 'P':
2703 case 'p':
2704 {
cristy670aa3c2011-11-03 00:54:00 +00002705 if (LocaleCompare(expression,"phi") == 0)
2706 return((MagickRealType) MagickPHI);
cristy3ed852e2009-09-05 21:47:34 +00002707 if (LocaleCompare(expression,"pi") == 0)
2708 return((MagickRealType) MagickPI);
2709 if (LocaleNCompare(expression,"pow",3) == 0)
2710 {
2711 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2712 exception);
2713 return((MagickRealType) pow((double) alpha,(double) *beta));
2714 }
2715 if (LocaleCompare(expression,"p") == 0)
2716 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2717 break;
2718 }
2719 case 'Q':
2720 case 'q':
2721 {
2722 if (LocaleCompare(expression,"QuantumRange") == 0)
2723 return((MagickRealType) QuantumRange);
2724 if (LocaleCompare(expression,"QuantumScale") == 0)
2725 return((MagickRealType) QuantumScale);
2726 break;
2727 }
2728 case 'R':
2729 case 'r':
2730 {
2731 if (LocaleNCompare(expression,"rand",4) == 0)
2732 return((MagickRealType) GetPseudoRandomValue(fx_info->random_info));
2733 if (LocaleNCompare(expression,"round",5) == 0)
2734 {
2735 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2736 exception);
cristy16788e42010-08-13 13:44:26 +00002737 return((MagickRealType) floor((double) alpha+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002738 }
2739 if (LocaleCompare(expression,"r") == 0)
2740 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2741 break;
2742 }
2743 case 'S':
2744 case 's':
2745 {
2746 if (LocaleCompare(expression,"saturation") == 0)
2747 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2748 if (LocaleNCompare(expression,"sign",4) == 0)
2749 {
2750 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2751 exception);
2752 return(alpha < 0.0 ? -1.0 : 1.0);
2753 }
cristya6a09e72010-03-02 14:51:02 +00002754 if (LocaleNCompare(expression,"sinc",4) == 0)
2755 {
2756 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2757 exception);
2758 if (alpha == 0)
2759 return(1.0);
2760 gamma=(MagickRealType) (sin((double) (MagickPI*alpha))/
2761 (MagickPI*alpha));
2762 return(gamma);
2763 }
cristy3ed852e2009-09-05 21:47:34 +00002764 if (LocaleNCompare(expression,"sinh",4) == 0)
2765 {
2766 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2767 exception);
2768 return((MagickRealType) sinh((double) alpha));
2769 }
2770 if (LocaleNCompare(expression,"sin",3) == 0)
2771 {
2772 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2773 exception);
2774 return((MagickRealType) sin((double) alpha));
2775 }
2776 if (LocaleNCompare(expression,"sqrt",4) == 0)
2777 {
2778 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2779 exception);
2780 return((MagickRealType) sqrt((double) alpha));
2781 }
cristy9eeedea2011-11-02 19:04:05 +00002782 if (LocaleNCompare(expression,"squish",6) == 0)
2783 {
2784 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2785 exception);
cristyf7b28ea2012-04-27 11:22:49 +00002786 return((MagickRealType) (1.0/(1.0+exp((double) (-alpha)))));
cristy9eeedea2011-11-02 19:04:05 +00002787 }
cristy3ed852e2009-09-05 21:47:34 +00002788 if (LocaleCompare(expression,"s") == 0)
2789 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2790 break;
2791 }
2792 case 'T':
2793 case 't':
2794 {
2795 if (LocaleNCompare(expression,"tanh",4) == 0)
2796 {
2797 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2798 exception);
2799 return((MagickRealType) tanh((double) alpha));
2800 }
2801 if (LocaleNCompare(expression,"tan",3) == 0)
2802 {
2803 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2804 exception);
2805 return((MagickRealType) tan((double) alpha));
2806 }
2807 if (LocaleCompare(expression,"Transparent") == 0)
2808 return(0.0);
cristy16788e42010-08-13 13:44:26 +00002809 if (LocaleNCompare(expression,"trunc",5) == 0)
2810 {
2811 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2812 exception);
2813 if (alpha >= 0.0)
2814 return((MagickRealType) floor((double) alpha));
2815 return((MagickRealType) ceil((double) alpha));
2816 }
cristy3ed852e2009-09-05 21:47:34 +00002817 if (LocaleCompare(expression,"t") == 0)
2818 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2819 break;
2820 }
2821 case 'U':
2822 case 'u':
2823 {
2824 if (LocaleCompare(expression,"u") == 0)
2825 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2826 break;
2827 }
2828 case 'V':
2829 case 'v':
2830 {
2831 if (LocaleCompare(expression,"v") == 0)
2832 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2833 break;
2834 }
2835 case 'W':
2836 case 'w':
2837 {
cristy9eeedea2011-11-02 19:04:05 +00002838 if (LocaleNCompare(expression,"while",5) == 0)
2839 {
2840 do
2841 {
2842 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2843 exception);
2844 } while (fabs((double) alpha) >= MagickEpsilon);
2845 return((MagickRealType) *beta);
2846 }
cristy3ed852e2009-09-05 21:47:34 +00002847 if (LocaleCompare(expression,"w") == 0)
2848 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2849 break;
2850 }
2851 case 'Y':
2852 case 'y':
2853 {
2854 if (LocaleCompare(expression,"y") == 0)
2855 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2856 break;
2857 }
2858 case 'Z':
2859 case 'z':
2860 {
2861 if (LocaleCompare(expression,"z") == 0)
2862 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2863 break;
2864 }
2865 default:
2866 break;
2867 }
2868 q=(char *) expression;
cristydbdd0e32011-11-04 23:29:40 +00002869 alpha=InterpretSiPrefixValue(expression,&q);
cristy3ed852e2009-09-05 21:47:34 +00002870 if (q == expression)
2871 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2872 return(alpha);
2873}
2874
cristy7832dc22011-09-05 01:21:53 +00002875MagickPrivate MagickBooleanType FxEvaluateExpression(FxInfo *fx_info,
cristy3ed852e2009-09-05 21:47:34 +00002876 MagickRealType *alpha,ExceptionInfo *exception)
2877{
2878 MagickBooleanType
2879 status;
2880
cristy541ae572011-08-05 19:08:59 +00002881 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2882 exception);
cristy3ed852e2009-09-05 21:47:34 +00002883 return(status);
2884}
2885
2886MagickExport MagickBooleanType FxPreprocessExpression(FxInfo *fx_info,
2887 MagickRealType *alpha,ExceptionInfo *exception)
2888{
2889 FILE
2890 *file;
2891
2892 MagickBooleanType
2893 status;
2894
2895 file=fx_info->file;
2896 fx_info->file=(FILE *) NULL;
cristy541ae572011-08-05 19:08:59 +00002897 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2898 exception);
cristy3ed852e2009-09-05 21:47:34 +00002899 fx_info->file=file;
2900 return(status);
2901}
2902
cristy7832dc22011-09-05 01:21:53 +00002903MagickPrivate MagickBooleanType FxEvaluateChannelExpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002904 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristye85007d2010-06-06 22:51:36 +00002905 MagickRealType *alpha,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002906{
2907 MagickRealType
2908 beta;
2909
2910 beta=0.0;
2911 *alpha=FxEvaluateSubexpression(fx_info,channel,x,y,fx_info->expression,&beta,
2912 exception);
2913 return(exception->severity == OptionError ? MagickFalse : MagickTrue);
2914}
2915
2916/*
2917%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2918% %
2919% %
2920% %
2921% F x I m a g e %
2922% %
2923% %
2924% %
2925%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2926%
2927% FxImage() applies a mathematical expression to the specified image.
2928%
2929% The format of the FxImage method is:
2930%
2931% Image *FxImage(const Image *image,const char *expression,
2932% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002933%
2934% A description of each parameter follows:
2935%
2936% o image: the image.
2937%
cristy3ed852e2009-09-05 21:47:34 +00002938% o expression: A mathematical expression.
2939%
2940% o exception: return any errors or warnings in this structure.
2941%
2942*/
2943
2944static FxInfo **DestroyFxThreadSet(FxInfo **fx_info)
2945{
cristybb503372010-05-27 20:51:26 +00002946 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002947 i;
2948
2949 assert(fx_info != (FxInfo **) NULL);
cristyac245f82012-05-05 17:13:57 +00002950 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
cristy3ed852e2009-09-05 21:47:34 +00002951 if (fx_info[i] != (FxInfo *) NULL)
2952 fx_info[i]=DestroyFxInfo(fx_info[i]);
cristyb41ee102010-10-04 16:46:15 +00002953 fx_info=(FxInfo **) RelinquishMagickMemory(fx_info);
cristy3ed852e2009-09-05 21:47:34 +00002954 return(fx_info);
2955}
2956
2957static FxInfo **AcquireFxThreadSet(const Image *image,const char *expression,
2958 ExceptionInfo *exception)
2959{
2960 char
2961 *fx_expression;
2962
2963 FxInfo
2964 **fx_info;
2965
2966 MagickRealType
2967 alpha;
2968
cristybb503372010-05-27 20:51:26 +00002969 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002970 i;
2971
cristybb503372010-05-27 20:51:26 +00002972 size_t
cristy3ed852e2009-09-05 21:47:34 +00002973 number_threads;
2974
cristy9357bdd2012-07-30 12:28:34 +00002975 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
cristyb41ee102010-10-04 16:46:15 +00002976 fx_info=(FxInfo **) AcquireQuantumMemory(number_threads,sizeof(*fx_info));
cristy3ed852e2009-09-05 21:47:34 +00002977 if (fx_info == (FxInfo **) NULL)
2978 return((FxInfo **) NULL);
2979 (void) ResetMagickMemory(fx_info,0,number_threads*sizeof(*fx_info));
2980 if (*expression != '@')
2981 fx_expression=ConstantString(expression);
2982 else
2983 fx_expression=FileToString(expression+1,~0,exception);
cristybb503372010-05-27 20:51:26 +00002984 for (i=0; i < (ssize_t) number_threads; i++)
cristy3ed852e2009-09-05 21:47:34 +00002985 {
cristydb070952012-04-20 14:33:00 +00002986 fx_info[i]=AcquireFxInfo(image,fx_expression,exception);
cristy3ed852e2009-09-05 21:47:34 +00002987 if (fx_info[i] == (FxInfo *) NULL)
2988 return(DestroyFxThreadSet(fx_info));
2989 (void) FxPreprocessExpression(fx_info[i],&alpha,fx_info[i]->exception);
2990 }
2991 fx_expression=DestroyString(fx_expression);
2992 return(fx_info);
2993}
2994
2995MagickExport Image *FxImage(const Image *image,const char *expression,
2996 ExceptionInfo *exception)
2997{
cristy3ed852e2009-09-05 21:47:34 +00002998#define FxImageTag "Fx/Image"
2999
cristyfa112112010-01-04 17:48:07 +00003000 CacheView
cristy79cedc72011-07-25 00:41:15 +00003001 *fx_view,
3002 *image_view;
cristyfa112112010-01-04 17:48:07 +00003003
cristy3ed852e2009-09-05 21:47:34 +00003004 FxInfo
cristyfa112112010-01-04 17:48:07 +00003005 **restrict fx_info;
cristy3ed852e2009-09-05 21:47:34 +00003006
3007 Image
3008 *fx_image;
3009
cristy3ed852e2009-09-05 21:47:34 +00003010 MagickBooleanType
3011 status;
3012
cristybb503372010-05-27 20:51:26 +00003013 MagickOffsetType
3014 progress;
3015
cristy3ed852e2009-09-05 21:47:34 +00003016 MagickRealType
3017 alpha;
3018
cristybb503372010-05-27 20:51:26 +00003019 ssize_t
3020 y;
3021
cristy3ed852e2009-09-05 21:47:34 +00003022 assert(image != (Image *) NULL);
3023 assert(image->signature == MagickSignature);
3024 if (image->debug != MagickFalse)
3025 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy79cedc72011-07-25 00:41:15 +00003026 fx_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00003027 if (fx_image == (Image *) NULL)
3028 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003029 if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003030 {
cristy3ed852e2009-09-05 21:47:34 +00003031 fx_image=DestroyImage(fx_image);
3032 return((Image *) NULL);
3033 }
3034 fx_info=AcquireFxThreadSet(image,expression,exception);
3035 if (fx_info == (FxInfo **) NULL)
3036 {
3037 fx_image=DestroyImage(fx_image);
3038 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3039 }
3040 status=FxPreprocessExpression(fx_info[0],&alpha,exception);
3041 if (status == MagickFalse)
3042 {
3043 fx_image=DestroyImage(fx_image);
3044 fx_info=DestroyFxThreadSet(fx_info);
3045 return((Image *) NULL);
3046 }
3047 /*
3048 Fx image.
3049 */
3050 status=MagickTrue;
3051 progress=0;
cristydb070952012-04-20 14:33:00 +00003052 image_view=AcquireVirtualCacheView(image,exception);
3053 fx_view=AcquireAuthenticCacheView(fx_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00003054#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003055 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00003056 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003057#endif
cristybb503372010-05-27 20:51:26 +00003058 for (y=0; y < (ssize_t) fx_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003059 {
cristy5c9e6f22010-09-17 17:31:01 +00003060 const int
3061 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00003062
cristy79cedc72011-07-25 00:41:15 +00003063 register const Quantum
3064 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003065
cristy4c08aed2011-07-01 19:47:50 +00003066 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003067 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003068
cristy79cedc72011-07-25 00:41:15 +00003069 register ssize_t
3070 x;
3071
cristy3ed852e2009-09-05 21:47:34 +00003072 if (status == MagickFalse)
3073 continue;
cristy79cedc72011-07-25 00:41:15 +00003074 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +00003075 q=QueueCacheViewAuthenticPixels(fx_view,0,y,fx_image->columns,1,exception);
cristy79cedc72011-07-25 00:41:15 +00003076 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003077 {
3078 status=MagickFalse;
3079 continue;
3080 }
cristybb503372010-05-27 20:51:26 +00003081 for (x=0; x < (ssize_t) fx_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003082 {
cristy79cedc72011-07-25 00:41:15 +00003083 register ssize_t
3084 i;
3085
cristy177e41c2012-04-15 15:08:25 +00003086 if (GetPixelMask(image,p) != 0)
3087 {
3088 p+=GetPixelChannels(image);
3089 q+=GetPixelChannels(fx_image);
3090 continue;
3091 }
cristy79cedc72011-07-25 00:41:15 +00003092 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3093 {
3094 MagickRealType
3095 alpha;
3096
3097 PixelChannel
3098 channel;
3099
3100 PixelTrait
3101 fx_traits,
3102 traits;
3103
cristye2a912b2011-12-05 20:02:07 +00003104 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003105 traits=GetPixelChannelMapTraits(image,channel);
cristy79cedc72011-07-25 00:41:15 +00003106 fx_traits=GetPixelChannelMapTraits(fx_image,channel);
cristy010d7d12011-08-31 01:02:48 +00003107 if ((traits == UndefinedPixelTrait) ||
3108 (fx_traits == UndefinedPixelTrait))
cristy79cedc72011-07-25 00:41:15 +00003109 continue;
cristy177e41c2012-04-15 15:08:25 +00003110 if ((fx_traits & CopyPixelTrait) != 0)
cristy79cedc72011-07-25 00:41:15 +00003111 {
cristy0beccfa2011-09-25 20:47:53 +00003112 SetPixelChannel(fx_image,channel,p[i],q);
cristy79cedc72011-07-25 00:41:15 +00003113 continue;
3114 }
3115 alpha=0.0;
cristya382aca2011-12-06 18:22:48 +00003116 (void) FxEvaluateChannelExpression(fx_info[id],channel,x,y,&alpha,
3117 exception);
cristy8cd03c32012-07-07 18:57:59 +00003118 q[i]=ClampToQuantum(QuantumRange*alpha);
cristy79cedc72011-07-25 00:41:15 +00003119 }
3120 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003121 q+=GetPixelChannels(fx_image);
cristy3ed852e2009-09-05 21:47:34 +00003122 }
3123 if (SyncCacheViewAuthenticPixels(fx_view,exception) == MagickFalse)
3124 status=MagickFalse;
3125 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3126 {
3127 MagickBooleanType
3128 proceed;
3129
cristyb5d5f722009-11-04 03:03:49 +00003130#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003131 #pragma omp critical (MagickCore_FxImage)
cristy3ed852e2009-09-05 21:47:34 +00003132#endif
3133 proceed=SetImageProgress(image,FxImageTag,progress++,image->rows);
3134 if (proceed == MagickFalse)
3135 status=MagickFalse;
3136 }
3137 }
cristy3ed852e2009-09-05 21:47:34 +00003138 fx_view=DestroyCacheView(fx_view);
cristy79cedc72011-07-25 00:41:15 +00003139 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003140 fx_info=DestroyFxThreadSet(fx_info);
3141 if (status == MagickFalse)
3142 fx_image=DestroyImage(fx_image);
3143 return(fx_image);
3144}
3145
3146/*
3147%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3148% %
3149% %
3150% %
3151% I m p l o d e I m a g e %
3152% %
3153% %
3154% %
3155%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3156%
3157% ImplodeImage() creates a new image that is a copy of an existing
3158% one with the image pixels "implode" by the specified percentage. It
3159% allocates the memory necessary for the new Image structure and returns a
3160% pointer to the new image.
3161%
3162% The format of the ImplodeImage method is:
3163%
3164% Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003165% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003166%
3167% A description of each parameter follows:
3168%
3169% o implode_image: Method ImplodeImage returns a pointer to the image
3170% after it is implode. A null image is returned if there is a memory
3171% shortage.
3172%
3173% o image: the image.
3174%
3175% o amount: Define the extent of the implosion.
3176%
cristy76f512e2011-09-12 01:26:56 +00003177% o method: the pixel interpolation method.
3178%
cristy3ed852e2009-09-05 21:47:34 +00003179% o exception: return any errors or warnings in this structure.
3180%
3181*/
3182MagickExport Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003183 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003184{
3185#define ImplodeImageTag "Implode/Image"
3186
cristyfa112112010-01-04 17:48:07 +00003187 CacheView
3188 *image_view,
3189 *implode_view;
3190
cristy3ed852e2009-09-05 21:47:34 +00003191 Image
3192 *implode_image;
3193
cristy3ed852e2009-09-05 21:47:34 +00003194 MagickBooleanType
3195 status;
3196
cristybb503372010-05-27 20:51:26 +00003197 MagickOffsetType
3198 progress;
3199
cristy3ed852e2009-09-05 21:47:34 +00003200 MagickRealType
3201 radius;
3202
3203 PointInfo
3204 center,
3205 scale;
3206
cristybb503372010-05-27 20:51:26 +00003207 ssize_t
3208 y;
3209
cristy3ed852e2009-09-05 21:47:34 +00003210 /*
3211 Initialize implode image attributes.
3212 */
3213 assert(image != (Image *) NULL);
3214 assert(image->signature == MagickSignature);
3215 if (image->debug != MagickFalse)
3216 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3217 assert(exception != (ExceptionInfo *) NULL);
3218 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00003219 implode_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3220 exception);
cristy3ed852e2009-09-05 21:47:34 +00003221 if (implode_image == (Image *) NULL)
3222 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003223 if (SetImageStorageClass(implode_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003224 {
cristy3ed852e2009-09-05 21:47:34 +00003225 implode_image=DestroyImage(implode_image);
3226 return((Image *) NULL);
3227 }
cristy4c08aed2011-07-01 19:47:50 +00003228 if (implode_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00003229 implode_image->matte=MagickTrue;
3230 /*
3231 Compute scaling factor.
3232 */
3233 scale.x=1.0;
3234 scale.y=1.0;
3235 center.x=0.5*image->columns;
3236 center.y=0.5*image->rows;
3237 radius=center.x;
3238 if (image->columns > image->rows)
3239 scale.y=(double) image->columns/(double) image->rows;
3240 else
3241 if (image->columns < image->rows)
3242 {
3243 scale.x=(double) image->rows/(double) image->columns;
3244 radius=center.y;
3245 }
3246 /*
3247 Implode image.
3248 */
3249 status=MagickTrue;
3250 progress=0;
cristydb070952012-04-20 14:33:00 +00003251 image_view=AcquireVirtualCacheView(image,exception);
3252 implode_view=AcquireAuthenticCacheView(implode_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00003253#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003254 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00003255 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003256#endif
cristybb503372010-05-27 20:51:26 +00003257 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003258 {
cristy3ed852e2009-09-05 21:47:34 +00003259 MagickRealType
3260 distance;
3261
3262 PointInfo
3263 delta;
3264
cristy6d188022011-09-12 13:23:33 +00003265 register const Quantum
3266 *restrict p;
3267
cristybb503372010-05-27 20:51:26 +00003268 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003269 x;
3270
cristy4c08aed2011-07-01 19:47:50 +00003271 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003272 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003273
3274 if (status == MagickFalse)
3275 continue;
cristy6d188022011-09-12 13:23:33 +00003276 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +00003277 q=QueueCacheViewAuthenticPixels(implode_view,0,y,implode_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003278 exception);
cristy6d188022011-09-12 13:23:33 +00003279 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003280 {
3281 status=MagickFalse;
3282 continue;
3283 }
cristy3ed852e2009-09-05 21:47:34 +00003284 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00003285 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003286 {
cristy6d188022011-09-12 13:23:33 +00003287 register ssize_t
3288 i;
3289
cristy3ed852e2009-09-05 21:47:34 +00003290 /*
3291 Determine if the pixel is within an ellipse.
3292 */
cristy10a6c612012-01-29 21:41:05 +00003293 if (GetPixelMask(image,p) != 0)
3294 {
3295 p+=GetPixelChannels(image);
3296 q+=GetPixelChannels(implode_image);
3297 continue;
3298 }
cristy3ed852e2009-09-05 21:47:34 +00003299 delta.x=scale.x*(double) (x-center.x);
3300 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00003301 if (distance >= (radius*radius))
cristya6d7a9b2012-01-18 20:04:48 +00003302 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy6d188022011-09-12 13:23:33 +00003303 {
cristya6d7a9b2012-01-18 20:04:48 +00003304 PixelChannel
3305 channel;
3306
cristy1707c6c2012-01-18 23:30:54 +00003307 PixelTrait
3308 implode_traits,
3309 traits;
3310
cristya6d7a9b2012-01-18 20:04:48 +00003311 channel=GetPixelChannelMapChannel(image,i);
cristy1707c6c2012-01-18 23:30:54 +00003312 traits=GetPixelChannelMapTraits(image,channel);
3313 implode_traits=GetPixelChannelMapTraits(implode_image,channel);
3314 if ((traits == UndefinedPixelTrait) ||
3315 (implode_traits == UndefinedPixelTrait))
3316 continue;
cristya6d7a9b2012-01-18 20:04:48 +00003317 SetPixelChannel(implode_image,channel,p[i],q);
cristy6d188022011-09-12 13:23:33 +00003318 }
3319 else
cristy3ed852e2009-09-05 21:47:34 +00003320 {
3321 double
3322 factor;
3323
3324 /*
3325 Implode the pixel.
3326 */
3327 factor=1.0;
3328 if (distance > 0.0)
cristy1707c6c2012-01-18 23:30:54 +00003329 factor=pow(sin((double) (MagickPI*sqrt((double) distance)/radius/
3330 2)),-amount);
cristy76f512e2011-09-12 01:26:56 +00003331 status=InterpolatePixelChannels(image,image_view,implode_image,method,
3332 (double) (factor*delta.x/scale.x+center.x),(double) (factor*delta.y/
3333 scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00003334 }
cristy6d188022011-09-12 13:23:33 +00003335 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003336 q+=GetPixelChannels(implode_image);
cristy3ed852e2009-09-05 21:47:34 +00003337 }
3338 if (SyncCacheViewAuthenticPixels(implode_view,exception) == MagickFalse)
3339 status=MagickFalse;
3340 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3341 {
3342 MagickBooleanType
3343 proceed;
3344
cristyb5d5f722009-11-04 03:03:49 +00003345#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003346 #pragma omp critical (MagickCore_ImplodeImage)
cristy3ed852e2009-09-05 21:47:34 +00003347#endif
3348 proceed=SetImageProgress(image,ImplodeImageTag,progress++,image->rows);
3349 if (proceed == MagickFalse)
3350 status=MagickFalse;
3351 }
3352 }
3353 implode_view=DestroyCacheView(implode_view);
3354 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003355 if (status == MagickFalse)
3356 implode_image=DestroyImage(implode_image);
3357 return(implode_image);
3358}
3359
3360/*
3361%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3362% %
3363% %
3364% %
3365% M o r p h I m a g e s %
3366% %
3367% %
3368% %
3369%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3370%
3371% The MorphImages() method requires a minimum of two images. The first
3372% image is transformed into the second by a number of intervening images
3373% as specified by frames.
3374%
3375% The format of the MorphImage method is:
3376%
cristybb503372010-05-27 20:51:26 +00003377% Image *MorphImages(const Image *image,const size_t number_frames,
cristy3ed852e2009-09-05 21:47:34 +00003378% ExceptionInfo *exception)
3379%
3380% A description of each parameter follows:
3381%
3382% o image: the image.
3383%
3384% o number_frames: Define the number of in-between image to generate.
3385% The more in-between frames, the smoother the morph.
3386%
3387% o exception: return any errors or warnings in this structure.
3388%
3389*/
3390MagickExport Image *MorphImages(const Image *image,
cristybb503372010-05-27 20:51:26 +00003391 const size_t number_frames,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003392{
3393#define MorphImageTag "Morph/Image"
3394
3395 Image
3396 *morph_image,
3397 *morph_images;
3398
cristy9d314ff2011-03-09 01:30:28 +00003399 MagickBooleanType
3400 status;
cristy3ed852e2009-09-05 21:47:34 +00003401
3402 MagickOffsetType
3403 scene;
3404
3405 MagickRealType
3406 alpha,
3407 beta;
3408
3409 register const Image
3410 *next;
3411
cristybb503372010-05-27 20:51:26 +00003412 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003413 i;
3414
cristy9d314ff2011-03-09 01:30:28 +00003415 ssize_t
3416 y;
cristy3ed852e2009-09-05 21:47:34 +00003417
3418 /*
3419 Clone first frame in sequence.
3420 */
3421 assert(image != (Image *) NULL);
3422 assert(image->signature == MagickSignature);
3423 if (image->debug != MagickFalse)
3424 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3425 assert(exception != (ExceptionInfo *) NULL);
3426 assert(exception->signature == MagickSignature);
3427 morph_images=CloneImage(image,0,0,MagickTrue,exception);
3428 if (morph_images == (Image *) NULL)
3429 return((Image *) NULL);
3430 if (GetNextImageInList(image) == (Image *) NULL)
3431 {
3432 /*
3433 Morph single image.
3434 */
cristybb503372010-05-27 20:51:26 +00003435 for (i=1; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003436 {
3437 morph_image=CloneImage(image,0,0,MagickTrue,exception);
3438 if (morph_image == (Image *) NULL)
3439 {
3440 morph_images=DestroyImageList(morph_images);
3441 return((Image *) NULL);
3442 }
3443 AppendImageToList(&morph_images,morph_image);
cristy8b27a6d2010-02-14 03:31:15 +00003444 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003445 {
cristy8b27a6d2010-02-14 03:31:15 +00003446 MagickBooleanType
3447 proceed;
3448
cristybb503372010-05-27 20:51:26 +00003449 proceed=SetImageProgress(image,MorphImageTag,(MagickOffsetType) i,
3450 number_frames);
cristy8b27a6d2010-02-14 03:31:15 +00003451 if (proceed == MagickFalse)
3452 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00003453 }
3454 }
3455 return(GetFirstImageInList(morph_images));
3456 }
3457 /*
3458 Morph image sequence.
3459 */
3460 status=MagickTrue;
3461 scene=0;
3462 next=image;
3463 for ( ; GetNextImageInList(next) != (Image *) NULL; next=GetNextImageInList(next))
3464 {
cristybb503372010-05-27 20:51:26 +00003465 for (i=0; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003466 {
3467 CacheView
3468 *image_view,
3469 *morph_view;
3470
3471 beta=(MagickRealType) (i+1.0)/(MagickRealType) (number_frames+1.0);
3472 alpha=1.0-beta;
cristy15b98cd2010-09-12 19:42:50 +00003473 morph_image=ResizeImage(next,(size_t) (alpha*next->columns+beta*
cristyaa2c16c2012-03-25 22:21:35 +00003474 GetNextImageInList(next)->columns+0.5),(size_t) (alpha*next->rows+beta*
3475 GetNextImageInList(next)->rows+0.5),next->filter,exception);
cristy3ed852e2009-09-05 21:47:34 +00003476 if (morph_image == (Image *) NULL)
3477 {
3478 morph_images=DestroyImageList(morph_images);
3479 return((Image *) NULL);
3480 }
cristy1707c6c2012-01-18 23:30:54 +00003481 status=SetImageStorageClass(morph_image,DirectClass,exception);
3482 if (status == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003483 {
cristy3ed852e2009-09-05 21:47:34 +00003484 morph_image=DestroyImage(morph_image);
3485 return((Image *) NULL);
3486 }
3487 AppendImageToList(&morph_images,morph_image);
3488 morph_images=GetLastImageInList(morph_images);
cristy15b98cd2010-09-12 19:42:50 +00003489 morph_image=ResizeImage(GetNextImageInList(next),morph_images->columns,
cristyaa2c16c2012-03-25 22:21:35 +00003490 morph_images->rows,GetNextImageInList(next)->filter,exception);
cristy3ed852e2009-09-05 21:47:34 +00003491 if (morph_image == (Image *) NULL)
3492 {
3493 morph_images=DestroyImageList(morph_images);
3494 return((Image *) NULL);
3495 }
cristydb070952012-04-20 14:33:00 +00003496 image_view=AcquireVirtualCacheView(morph_image,exception);
3497 morph_view=AcquireAuthenticCacheView(morph_images,exception);
cristyb5d5f722009-11-04 03:03:49 +00003498#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003499 #pragma omp parallel for schedule(static,4) shared(status) \
cristy4ee2b0c2012-05-15 00:30:35 +00003500 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003501#endif
cristybb503372010-05-27 20:51:26 +00003502 for (y=0; y < (ssize_t) morph_images->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003503 {
3504 MagickBooleanType
3505 sync;
3506
cristy4c08aed2011-07-01 19:47:50 +00003507 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003508 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003509
cristybb503372010-05-27 20:51:26 +00003510 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003511 x;
3512
cristy4c08aed2011-07-01 19:47:50 +00003513 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003514 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003515
3516 if (status == MagickFalse)
3517 continue;
3518 p=GetCacheViewVirtualPixels(image_view,0,y,morph_image->columns,1,
3519 exception);
3520 q=GetCacheViewAuthenticPixels(morph_view,0,y,morph_images->columns,1,
3521 exception);
cristy4c08aed2011-07-01 19:47:50 +00003522 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003523 {
3524 status=MagickFalse;
3525 continue;
3526 }
cristybb503372010-05-27 20:51:26 +00003527 for (x=0; x < (ssize_t) morph_images->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003528 {
cristy1707c6c2012-01-18 23:30:54 +00003529 register ssize_t
3530 i;
3531
cristy177e41c2012-04-15 15:08:25 +00003532 if (GetPixelMask(image,p) != 0)
3533 {
3534 p+=GetPixelChannels(image);
3535 q+=GetPixelChannels(morph_image);
3536 continue;
3537 }
cristy10a6c612012-01-29 21:41:05 +00003538 for (i=0; i < (ssize_t) GetPixelChannels(morph_image); i++)
cristy1707c6c2012-01-18 23:30:54 +00003539 {
3540 PixelChannel
3541 channel;
3542
3543 PixelTrait
3544 morph_traits,
3545 traits;
3546
3547 channel=GetPixelChannelMapChannel(image,i);
3548 traits=GetPixelChannelMapTraits(image,channel);
3549 morph_traits=GetPixelChannelMapTraits(morph_image,channel);
3550 if ((traits == UndefinedPixelTrait) ||
3551 (morph_traits == UndefinedPixelTrait))
3552 continue;
cristy177e41c2012-04-15 15:08:25 +00003553 if ((morph_traits & CopyPixelTrait) != 0)
cristy1707c6c2012-01-18 23:30:54 +00003554 {
3555 SetPixelChannel(morph_image,channel,p[i],q);
3556 continue;
3557 }
3558 SetPixelChannel(morph_image,channel,ClampToQuantum(alpha*
3559 GetPixelChannel(morph_images,channel,q)+beta*p[i]),q);
3560 }
cristyed231572011-07-14 02:18:59 +00003561 p+=GetPixelChannels(morph_image);
3562 q+=GetPixelChannels(morph_images);
cristy3ed852e2009-09-05 21:47:34 +00003563 }
3564 sync=SyncCacheViewAuthenticPixels(morph_view,exception);
3565 if (sync == MagickFalse)
3566 status=MagickFalse;
3567 }
3568 morph_view=DestroyCacheView(morph_view);
3569 image_view=DestroyCacheView(image_view);
3570 morph_image=DestroyImage(morph_image);
3571 }
cristybb503372010-05-27 20:51:26 +00003572 if (i < (ssize_t) number_frames)
cristy3ed852e2009-09-05 21:47:34 +00003573 break;
3574 /*
3575 Clone last frame in sequence.
3576 */
3577 morph_image=CloneImage(GetNextImageInList(next),0,0,MagickTrue,exception);
3578 if (morph_image == (Image *) NULL)
3579 {
3580 morph_images=DestroyImageList(morph_images);
3581 return((Image *) NULL);
3582 }
3583 AppendImageToList(&morph_images,morph_image);
3584 morph_images=GetLastImageInList(morph_images);
3585 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3586 {
3587 MagickBooleanType
3588 proceed;
3589
cristyb5d5f722009-11-04 03:03:49 +00003590#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003591 #pragma omp critical (MagickCore_MorphImages)
cristy3ed852e2009-09-05 21:47:34 +00003592#endif
3593 proceed=SetImageProgress(image,MorphImageTag,scene,
3594 GetImageListLength(image));
3595 if (proceed == MagickFalse)
3596 status=MagickFalse;
3597 }
3598 scene++;
3599 }
3600 if (GetNextImageInList(next) != (Image *) NULL)
3601 {
3602 morph_images=DestroyImageList(morph_images);
3603 return((Image *) NULL);
3604 }
3605 return(GetFirstImageInList(morph_images));
3606}
3607
3608/*
3609%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3610% %
3611% %
3612% %
3613% P l a s m a I m a g e %
3614% %
3615% %
3616% %
3617%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3618%
3619% PlasmaImage() initializes an image with plasma fractal values. The image
3620% must be initialized with a base color and the random number generator
3621% seeded before this method is called.
3622%
3623% The format of the PlasmaImage method is:
3624%
3625% MagickBooleanType PlasmaImage(Image *image,const SegmentInfo *segment,
cristy5cbc0162011-08-29 00:36:28 +00003626% size_t attenuate,size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003627%
3628% A description of each parameter follows:
3629%
3630% o image: the image.
3631%
3632% o segment: Define the region to apply plasma fractals values.
3633%
glennrp7dae1ca2010-09-16 12:17:35 +00003634% o attenuate: Define the plasma attenuation factor.
cristy3ed852e2009-09-05 21:47:34 +00003635%
3636% o depth: Limit the plasma recursion depth.
3637%
cristy5cbc0162011-08-29 00:36:28 +00003638% o exception: return any errors or warnings in this structure.
3639%
cristy3ed852e2009-09-05 21:47:34 +00003640*/
3641
3642static inline Quantum PlasmaPixel(RandomInfo *random_info,
3643 const MagickRealType pixel,const MagickRealType noise)
3644{
3645 Quantum
3646 plasma;
3647
cristyce70c172010-01-07 17:15:30 +00003648 plasma=ClampToQuantum(pixel+noise*GetPseudoRandomValue(random_info)-
cristy3ed852e2009-09-05 21:47:34 +00003649 noise/2.0);
3650 return(plasma);
3651}
3652
cristyda1f9c12011-10-02 21:39:49 +00003653static MagickBooleanType PlasmaImageProxy(Image *image,CacheView *image_view,
3654 CacheView *u_view,CacheView *v_view,RandomInfo *random_info,
3655 const SegmentInfo *segment,size_t attenuate,size_t depth,
3656 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003657{
cristy3ed852e2009-09-05 21:47:34 +00003658 MagickRealType
3659 plasma;
3660
cristyda1f9c12011-10-02 21:39:49 +00003661 PixelChannel
3662 channel;
3663
3664 PixelTrait
3665 traits;
3666
3667 register const Quantum
3668 *restrict u,
3669 *restrict v;
3670
3671 register Quantum
3672 *restrict q;
3673
3674 register ssize_t
3675 i;
cristy3ed852e2009-09-05 21:47:34 +00003676
cristy9d314ff2011-03-09 01:30:28 +00003677 ssize_t
3678 x,
3679 x_mid,
3680 y,
3681 y_mid;
3682
cristy3ed852e2009-09-05 21:47:34 +00003683 if (((segment->x2-segment->x1) == 0.0) && ((segment->y2-segment->y1) == 0.0))
3684 return(MagickTrue);
3685 if (depth != 0)
3686 {
3687 SegmentInfo
3688 local_info;
3689
3690 /*
3691 Divide the area into quadrants and recurse.
3692 */
3693 depth--;
3694 attenuate++;
cristybb503372010-05-27 20:51:26 +00003695 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3696 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003697 local_info=(*segment);
3698 local_info.x2=(double) x_mid;
3699 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003700 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3701 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003702 local_info=(*segment);
3703 local_info.y1=(double) y_mid;
3704 local_info.x2=(double) x_mid;
cristyda1f9c12011-10-02 21:39:49 +00003705 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3706 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003707 local_info=(*segment);
3708 local_info.x1=(double) x_mid;
3709 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003710 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3711 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003712 local_info=(*segment);
3713 local_info.x1=(double) x_mid;
3714 local_info.y1=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003715 return(PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3716 &local_info,attenuate,depth,exception));
cristy3ed852e2009-09-05 21:47:34 +00003717 }
cristybb503372010-05-27 20:51:26 +00003718 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3719 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003720 if ((segment->x1 == (double) x_mid) && (segment->x2 == (double) x_mid) &&
3721 (segment->y1 == (double) y_mid) && (segment->y2 == (double) y_mid))
3722 return(MagickFalse);
3723 /*
3724 Average pixels and apply plasma.
3725 */
cristy3ed852e2009-09-05 21:47:34 +00003726 plasma=(MagickRealType) QuantumRange/(2.0*attenuate);
3727 if ((segment->x1 != (double) x_mid) || (segment->x2 != (double) x_mid))
3728 {
cristy3ed852e2009-09-05 21:47:34 +00003729 /*
3730 Left pixel.
3731 */
cristybb503372010-05-27 20:51:26 +00003732 x=(ssize_t) ceil(segment->x1-0.5);
cristy1707c6c2012-01-18 23:30:54 +00003733 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),1,1,
3734 exception);
3735 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),1,1,
3736 exception);
cristyc5c6f662010-09-22 14:23:02 +00003737 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003738 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3739 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003740 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003741 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3742 {
cristye2a912b2011-12-05 20:02:07 +00003743 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003744 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003745 if (traits == UndefinedPixelTrait)
3746 continue;
3747 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3748 }
cristyc5c6f662010-09-22 14:23:02 +00003749 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003750 if (segment->x1 != segment->x2)
3751 {
3752 /*
3753 Right pixel.
3754 */
cristybb503372010-05-27 20:51:26 +00003755 x=(ssize_t) ceil(segment->x2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003756 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3757 1,1,exception);
3758 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3759 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003760 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003761 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3762 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003763 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003764 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3765 {
cristye2a912b2011-12-05 20:02:07 +00003766 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003767 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003768 if (traits == UndefinedPixelTrait)
3769 continue;
3770 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3771 }
cristyc5c6f662010-09-22 14:23:02 +00003772 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003773 }
3774 }
3775 if ((segment->y1 != (double) y_mid) || (segment->y2 != (double) y_mid))
3776 {
3777 if ((segment->x1 != (double) x_mid) || (segment->y2 != (double) y_mid))
3778 {
cristy3ed852e2009-09-05 21:47:34 +00003779 /*
3780 Bottom pixel.
3781 */
cristybb503372010-05-27 20:51:26 +00003782 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003783 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3784 1,1,exception);
3785 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3786 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003787 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003788 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3789 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003790 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003791 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3792 {
cristye2a912b2011-12-05 20:02:07 +00003793 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003794 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003795 if (traits == UndefinedPixelTrait)
3796 continue;
3797 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3798 }
cristyc5c6f662010-09-22 14:23:02 +00003799 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003800 }
3801 if (segment->y1 != segment->y2)
3802 {
cristy3ed852e2009-09-05 21:47:34 +00003803 /*
3804 Top pixel.
3805 */
cristybb503372010-05-27 20:51:26 +00003806 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003807 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3808 1,1,exception);
3809 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3810 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003811 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003812 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3813 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003814 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003815 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3816 {
cristye2a912b2011-12-05 20:02:07 +00003817 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003818 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003819 if (traits == UndefinedPixelTrait)
3820 continue;
3821 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3822 }
cristyc5c6f662010-09-22 14:23:02 +00003823 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003824 }
3825 }
3826 if ((segment->x1 != segment->x2) || (segment->y1 != segment->y2))
3827 {
cristy3ed852e2009-09-05 21:47:34 +00003828 /*
3829 Middle pixel.
3830 */
cristybb503372010-05-27 20:51:26 +00003831 x=(ssize_t) ceil(segment->x1-0.5);
3832 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003833 u=GetCacheViewVirtualPixels(u_view,x,y,1,1,exception);
cristybb503372010-05-27 20:51:26 +00003834 x=(ssize_t) ceil(segment->x2-0.5);
3835 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003836 v=GetCacheViewVirtualPixels(v_view,x,y,1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003837 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003838 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3839 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003840 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003841 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3842 {
cristye2a912b2011-12-05 20:02:07 +00003843 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003844 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003845 if (traits == UndefinedPixelTrait)
3846 continue;
3847 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3848 }
cristyc5c6f662010-09-22 14:23:02 +00003849 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003850 }
3851 if (((segment->x2-segment->x1) < 3.0) && ((segment->y2-segment->y1) < 3.0))
3852 return(MagickTrue);
3853 return(MagickFalse);
3854}
cristyda1f9c12011-10-02 21:39:49 +00003855
cristy3ed852e2009-09-05 21:47:34 +00003856MagickExport MagickBooleanType PlasmaImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00003857 const SegmentInfo *segment,size_t attenuate,size_t depth,
3858 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003859{
cristyc5c6f662010-09-22 14:23:02 +00003860 CacheView
cristyda1f9c12011-10-02 21:39:49 +00003861 *image_view,
3862 *u_view,
3863 *v_view;
cristyc5c6f662010-09-22 14:23:02 +00003864
cristy3ed852e2009-09-05 21:47:34 +00003865 MagickBooleanType
3866 status;
3867
3868 RandomInfo
3869 *random_info;
3870
3871 if (image->debug != MagickFalse)
3872 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3873 assert(image != (Image *) NULL);
3874 assert(image->signature == MagickSignature);
3875 if (image->debug != MagickFalse)
3876 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
cristy5cbc0162011-08-29 00:36:28 +00003877 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristyc5c6f662010-09-22 14:23:02 +00003878 return(MagickFalse);
cristydb070952012-04-20 14:33:00 +00003879 image_view=AcquireAuthenticCacheView(image,exception);
3880 u_view=AcquireVirtualCacheView(image,exception);
3881 v_view=AcquireVirtualCacheView(image,exception);
cristy3ed852e2009-09-05 21:47:34 +00003882 random_info=AcquireRandomInfo();
cristyda1f9c12011-10-02 21:39:49 +00003883 status=PlasmaImageProxy(image,image_view,u_view,v_view,random_info,segment,
3884 attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003885 random_info=DestroyRandomInfo(random_info);
cristyda1f9c12011-10-02 21:39:49 +00003886 v_view=DestroyCacheView(v_view);
3887 u_view=DestroyCacheView(u_view);
cristyc5c6f662010-09-22 14:23:02 +00003888 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003889 return(status);
3890}
3891
3892/*
3893%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3894% %
3895% %
3896% %
3897% P o l a r o i d I m a g e %
3898% %
3899% %
3900% %
3901%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3902%
3903% PolaroidImage() simulates a Polaroid picture.
3904%
3905% The format of the AnnotateImage method is:
3906%
3907% Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristye9e3d382011-12-14 01:50:13 +00003908% const char *caption,const double angle,
3909% const PixelInterpolateMethod method,ExceptionInfo exception)
cristy3ed852e2009-09-05 21:47:34 +00003910%
3911% A description of each parameter follows:
3912%
3913% o image: the image.
3914%
3915% o draw_info: the draw info.
3916%
cristye9e3d382011-12-14 01:50:13 +00003917% o caption: the Polaroid caption.
3918%
cristycee97112010-05-28 00:44:52 +00003919% o angle: Apply the effect along this angle.
cristy3ed852e2009-09-05 21:47:34 +00003920%
cristy5c4e2582011-09-11 19:21:03 +00003921% o method: the pixel interpolation method.
3922%
cristy3ed852e2009-09-05 21:47:34 +00003923% o exception: return any errors or warnings in this structure.
3924%
3925*/
3926MagickExport Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristye9e3d382011-12-14 01:50:13 +00003927 const char *caption,const double angle,const PixelInterpolateMethod method,
cristy5c4e2582011-09-11 19:21:03 +00003928 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003929{
cristy3ed852e2009-09-05 21:47:34 +00003930 Image
3931 *bend_image,
3932 *caption_image,
3933 *flop_image,
3934 *picture_image,
3935 *polaroid_image,
3936 *rotate_image,
3937 *trim_image;
3938
cristybb503372010-05-27 20:51:26 +00003939 size_t
cristy3ed852e2009-09-05 21:47:34 +00003940 height;
3941
cristy9d314ff2011-03-09 01:30:28 +00003942 ssize_t
3943 quantum;
3944
cristy3ed852e2009-09-05 21:47:34 +00003945 /*
3946 Simulate a Polaroid picture.
3947 */
3948 assert(image != (Image *) NULL);
3949 assert(image->signature == MagickSignature);
3950 if (image->debug != MagickFalse)
3951 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3952 assert(exception != (ExceptionInfo *) NULL);
3953 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00003954 quantum=(ssize_t) MagickMax(MagickMax((double) image->columns,(double)
cristy3ed852e2009-09-05 21:47:34 +00003955 image->rows)/25.0,10.0);
3956 height=image->rows+2*quantum;
3957 caption_image=(Image *) NULL;
cristye9e3d382011-12-14 01:50:13 +00003958 if (caption != (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003959 {
3960 char
cristye9e3d382011-12-14 01:50:13 +00003961 geometry[MaxTextExtent],
3962 *text;
cristy3ed852e2009-09-05 21:47:34 +00003963
3964 DrawInfo
3965 *annotate_info;
3966
cristy3ed852e2009-09-05 21:47:34 +00003967 MagickBooleanType
3968 status;
3969
cristy9d314ff2011-03-09 01:30:28 +00003970 ssize_t
3971 count;
3972
cristy3ed852e2009-09-05 21:47:34 +00003973 TypeMetric
3974 metrics;
3975
3976 /*
3977 Generate caption image.
3978 */
3979 caption_image=CloneImage(image,image->columns,1,MagickTrue,exception);
3980 if (caption_image == (Image *) NULL)
3981 return((Image *) NULL);
3982 annotate_info=CloneDrawInfo((const ImageInfo *) NULL,draw_info);
cristye9e3d382011-12-14 01:50:13 +00003983 text=InterpretImageProperties((ImageInfo *) NULL,(Image *) image,caption,
3984 exception);
3985 (void) CloneString(&annotate_info->text,text);
cristy6b1d05e2010-09-22 19:17:27 +00003986 count=FormatMagickCaption(caption_image,annotate_info,MagickTrue,&metrics,
cristye9e3d382011-12-14 01:50:13 +00003987 &text,exception);
3988 status=SetImageExtent(caption_image,image->columns,(size_t) ((count+1)*
3989 (metrics.ascent-metrics.descent)+0.5),exception);
cristy3ed852e2009-09-05 21:47:34 +00003990 if (status == MagickFalse)
3991 caption_image=DestroyImage(caption_image);
3992 else
3993 {
3994 caption_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00003995 (void) SetImageBackgroundColor(caption_image,exception);
cristye9e3d382011-12-14 01:50:13 +00003996 (void) CloneString(&annotate_info->text,text);
cristyb51dff52011-05-19 16:55:47 +00003997 (void) FormatLocaleString(geometry,MaxTextExtent,"+0+%g",
cristy3ed852e2009-09-05 21:47:34 +00003998 metrics.ascent);
3999 if (annotate_info->gravity == UndefinedGravity)
4000 (void) CloneString(&annotate_info->geometry,AcquireString(
4001 geometry));
cristy5cbc0162011-08-29 00:36:28 +00004002 (void) AnnotateImage(caption_image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00004003 height+=caption_image->rows;
4004 }
4005 annotate_info=DestroyDrawInfo(annotate_info);
cristye9e3d382011-12-14 01:50:13 +00004006 text=DestroyString(text);
cristy3ed852e2009-09-05 21:47:34 +00004007 }
4008 picture_image=CloneImage(image,image->columns+2*quantum,height,MagickTrue,
4009 exception);
4010 if (picture_image == (Image *) NULL)
4011 {
4012 if (caption_image != (Image *) NULL)
4013 caption_image=DestroyImage(caption_image);
4014 return((Image *) NULL);
4015 }
4016 picture_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00004017 (void) SetImageBackgroundColor(picture_image,exception);
cristy39172402012-03-30 13:04:39 +00004018 (void) CompositeImage(picture_image,image,OverCompositeOp,MagickTrue,quantum,
cristyfeb3e962012-03-29 17:25:55 +00004019 quantum,exception);
cristy3ed852e2009-09-05 21:47:34 +00004020 if (caption_image != (Image *) NULL)
4021 {
cristyfeb3e962012-03-29 17:25:55 +00004022 (void) CompositeImage(picture_image,caption_image,OverCompositeOp,
cristy39172402012-03-30 13:04:39 +00004023 MagickTrue,quantum,(ssize_t) (image->rows+3*quantum/2),exception);
cristy3ed852e2009-09-05 21:47:34 +00004024 caption_image=DestroyImage(caption_image);
4025 }
cristy9950d572011-10-01 18:22:35 +00004026 (void) QueryColorCompliance("none",AllCompliance,
4027 &picture_image->background_color,exception);
cristy63240882011-08-05 19:05:27 +00004028 (void) SetImageAlphaChannel(picture_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004029 rotate_image=RotateImage(picture_image,90.0,exception);
4030 picture_image=DestroyImage(picture_image);
4031 if (rotate_image == (Image *) NULL)
4032 return((Image *) NULL);
4033 picture_image=rotate_image;
4034 bend_image=WaveImage(picture_image,0.01*picture_image->rows,2.0*
cristy5c4e2582011-09-11 19:21:03 +00004035 picture_image->columns,method,exception);
cristy3ed852e2009-09-05 21:47:34 +00004036 picture_image=DestroyImage(picture_image);
4037 if (bend_image == (Image *) NULL)
4038 return((Image *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00004039 picture_image=bend_image;
4040 rotate_image=RotateImage(picture_image,-90.0,exception);
4041 picture_image=DestroyImage(picture_image);
4042 if (rotate_image == (Image *) NULL)
4043 return((Image *) NULL);
4044 picture_image=rotate_image;
4045 picture_image->background_color=image->background_color;
anthonyf46d4262012-03-26 03:30:34 +00004046 polaroid_image=ShadowImage(picture_image,80.0,2.0,quantum/3,quantum/3,
cristy3ed852e2009-09-05 21:47:34 +00004047 exception);
4048 if (polaroid_image == (Image *) NULL)
4049 {
4050 picture_image=DestroyImage(picture_image);
4051 return(picture_image);
4052 }
4053 flop_image=FlopImage(polaroid_image,exception);
4054 polaroid_image=DestroyImage(polaroid_image);
4055 if (flop_image == (Image *) NULL)
4056 {
4057 picture_image=DestroyImage(picture_image);
4058 return(picture_image);
4059 }
4060 polaroid_image=flop_image;
cristyfeb3e962012-03-29 17:25:55 +00004061 (void) CompositeImage(polaroid_image,picture_image,OverCompositeOp,
cristy39172402012-03-30 13:04:39 +00004062 MagickTrue,(ssize_t) (-0.01*picture_image->columns/2.0),0L,exception);
cristy3ed852e2009-09-05 21:47:34 +00004063 picture_image=DestroyImage(picture_image);
cristy9950d572011-10-01 18:22:35 +00004064 (void) QueryColorCompliance("none",AllCompliance,
4065 &polaroid_image->background_color,exception);
cristy3ed852e2009-09-05 21:47:34 +00004066 rotate_image=RotateImage(polaroid_image,angle,exception);
4067 polaroid_image=DestroyImage(polaroid_image);
4068 if (rotate_image == (Image *) NULL)
4069 return((Image *) NULL);
4070 polaroid_image=rotate_image;
4071 trim_image=TrimImage(polaroid_image,exception);
4072 polaroid_image=DestroyImage(polaroid_image);
4073 if (trim_image == (Image *) NULL)
4074 return((Image *) NULL);
4075 polaroid_image=trim_image;
4076 return(polaroid_image);
4077}
4078
4079/*
4080%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4081% %
4082% %
4083% %
cristy3ed852e2009-09-05 21:47:34 +00004084% S e p i a T o n e I m a g e %
4085% %
4086% %
4087% %
4088%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4089%
4090% MagickSepiaToneImage() applies a special effect to the image, similar to the
4091% effect achieved in a photo darkroom by sepia toning. Threshold ranges from
4092% 0 to QuantumRange and is a measure of the extent of the sepia toning. A
4093% threshold of 80% is a good starting point for a reasonable tone.
4094%
4095% The format of the SepiaToneImage method is:
4096%
4097% Image *SepiaToneImage(const Image *image,const double threshold,
4098% ExceptionInfo *exception)
4099%
4100% A description of each parameter follows:
4101%
4102% o image: the image.
4103%
4104% o threshold: the tone threshold.
4105%
4106% o exception: return any errors or warnings in this structure.
4107%
4108*/
4109MagickExport Image *SepiaToneImage(const Image *image,const double threshold,
4110 ExceptionInfo *exception)
4111{
4112#define SepiaToneImageTag "SepiaTone/Image"
4113
cristyc4c8d132010-01-07 01:58:38 +00004114 CacheView
4115 *image_view,
4116 *sepia_view;
4117
cristy3ed852e2009-09-05 21:47:34 +00004118 Image
4119 *sepia_image;
4120
cristy3ed852e2009-09-05 21:47:34 +00004121 MagickBooleanType
4122 status;
4123
cristybb503372010-05-27 20:51:26 +00004124 MagickOffsetType
4125 progress;
4126
4127 ssize_t
4128 y;
4129
cristy3ed852e2009-09-05 21:47:34 +00004130 /*
4131 Initialize sepia-toned image attributes.
4132 */
4133 assert(image != (const Image *) NULL);
4134 assert(image->signature == MagickSignature);
4135 if (image->debug != MagickFalse)
4136 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4137 assert(exception != (ExceptionInfo *) NULL);
4138 assert(exception->signature == MagickSignature);
cristy1707c6c2012-01-18 23:30:54 +00004139 sepia_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004140 if (sepia_image == (Image *) NULL)
4141 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004142 if (SetImageStorageClass(sepia_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004143 {
cristy3ed852e2009-09-05 21:47:34 +00004144 sepia_image=DestroyImage(sepia_image);
4145 return((Image *) NULL);
4146 }
4147 /*
4148 Tone each row of the image.
4149 */
4150 status=MagickTrue;
4151 progress=0;
cristydb070952012-04-20 14:33:00 +00004152 image_view=AcquireVirtualCacheView(image,exception);
4153 sepia_view=AcquireAuthenticCacheView(sepia_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00004154#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00004155 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00004156 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00004157#endif
cristybb503372010-05-27 20:51:26 +00004158 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004159 {
cristy4c08aed2011-07-01 19:47:50 +00004160 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004161 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00004162
cristybb503372010-05-27 20:51:26 +00004163 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004164 x;
4165
cristy4c08aed2011-07-01 19:47:50 +00004166 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004167 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004168
4169 if (status == MagickFalse)
4170 continue;
4171 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy1707c6c2012-01-18 23:30:54 +00004172 q=GetCacheViewAuthenticPixels(sepia_view,0,y,sepia_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00004173 exception);
cristy4c08aed2011-07-01 19:47:50 +00004174 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004175 {
4176 status=MagickFalse;
4177 continue;
4178 }
cristybb503372010-05-27 20:51:26 +00004179 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004180 {
4181 MagickRealType
4182 intensity,
4183 tone;
4184
cristy4c08aed2011-07-01 19:47:50 +00004185 intensity=(MagickRealType) GetPixelIntensity(image,p);
cristy3ed852e2009-09-05 21:47:34 +00004186 tone=intensity > threshold ? (MagickRealType) QuantumRange : intensity+
4187 (MagickRealType) QuantumRange-threshold;
cristy4c08aed2011-07-01 19:47:50 +00004188 SetPixelRed(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004189 tone=intensity > (7.0*threshold/6.0) ? (MagickRealType) QuantumRange :
4190 intensity+(MagickRealType) QuantumRange-7.0*threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004191 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004192 tone=intensity < (threshold/6.0) ? 0 : intensity-threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004193 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004194 tone=threshold/7.0;
cristy4c08aed2011-07-01 19:47:50 +00004195 if ((MagickRealType) GetPixelGreen(image,q) < tone)
4196 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
4197 if ((MagickRealType) GetPixelBlue(image,q) < tone)
4198 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristyed231572011-07-14 02:18:59 +00004199 p+=GetPixelChannels(image);
4200 q+=GetPixelChannels(sepia_image);
cristy3ed852e2009-09-05 21:47:34 +00004201 }
4202 if (SyncCacheViewAuthenticPixels(sepia_view,exception) == MagickFalse)
4203 status=MagickFalse;
4204 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4205 {
4206 MagickBooleanType
4207 proceed;
4208
cristyb5d5f722009-11-04 03:03:49 +00004209#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00004210 #pragma omp critical (MagickCore_SepiaToneImage)
cristy3ed852e2009-09-05 21:47:34 +00004211#endif
4212 proceed=SetImageProgress(image,SepiaToneImageTag,progress++,
4213 image->rows);
4214 if (proceed == MagickFalse)
4215 status=MagickFalse;
4216 }
4217 }
4218 sepia_view=DestroyCacheView(sepia_view);
4219 image_view=DestroyCacheView(image_view);
cristye23ec9d2011-08-16 18:15:40 +00004220 (void) NormalizeImage(sepia_image,exception);
4221 (void) ContrastImage(sepia_image,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004222 if (status == MagickFalse)
4223 sepia_image=DestroyImage(sepia_image);
4224 return(sepia_image);
4225}
4226
4227/*
4228%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4229% %
4230% %
4231% %
4232% S h a d o w I m a g e %
4233% %
4234% %
4235% %
4236%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4237%
4238% ShadowImage() simulates a shadow from the specified image and returns it.
4239%
4240% The format of the ShadowImage method is:
4241%
cristy70cddf72011-12-10 22:42:42 +00004242% Image *ShadowImage(const Image *image,const double alpha,
cristyaa2c16c2012-03-25 22:21:35 +00004243% const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4244% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004245%
4246% A description of each parameter follows:
4247%
4248% o image: the image.
4249%
cristy70cddf72011-12-10 22:42:42 +00004250% o alpha: percentage transparency.
cristy3ed852e2009-09-05 21:47:34 +00004251%
4252% o sigma: the standard deviation of the Gaussian, in pixels.
4253%
4254% o x_offset: the shadow x-offset.
4255%
4256% o y_offset: the shadow y-offset.
4257%
4258% o exception: return any errors or warnings in this structure.
4259%
4260*/
cristy70cddf72011-12-10 22:42:42 +00004261MagickExport Image *ShadowImage(const Image *image,const double alpha,
cristyaa2c16c2012-03-25 22:21:35 +00004262 const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4263 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004264{
4265#define ShadowImageTag "Shadow/Image"
4266
cristy70cddf72011-12-10 22:42:42 +00004267 CacheView
4268 *image_view;
4269
cristybd5a96c2011-08-21 00:04:26 +00004270 ChannelType
4271 channel_mask;
4272
cristy3ed852e2009-09-05 21:47:34 +00004273 Image
4274 *border_image,
4275 *clone_image,
4276 *shadow_image;
4277
cristy70cddf72011-12-10 22:42:42 +00004278 MagickBooleanType
4279 status;
4280
cristy3ed852e2009-09-05 21:47:34 +00004281 RectangleInfo
4282 border_info;
4283
cristy70cddf72011-12-10 22:42:42 +00004284 ssize_t
4285 y;
4286
cristy3ed852e2009-09-05 21:47:34 +00004287 assert(image != (Image *) NULL);
4288 assert(image->signature == MagickSignature);
4289 if (image->debug != MagickFalse)
4290 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4291 assert(exception != (ExceptionInfo *) NULL);
4292 assert(exception->signature == MagickSignature);
4293 clone_image=CloneImage(image,0,0,MagickTrue,exception);
4294 if (clone_image == (Image *) NULL)
4295 return((Image *) NULL);
cristy0898eba2012-04-09 16:38:29 +00004296 if (IsGrayColorspace(image->colorspace) != MagickFalse)
cristyb09db112012-07-11 12:04:31 +00004297 (void) TransformImageColorspace(clone_image,RGBColorspace,exception);
cristy0ce08762012-06-30 01:33:18 +00004298 (void) SetImageVirtualPixelMethod(clone_image,TransparentVirtualPixelMethod,
cristy387430f2012-02-07 13:09:46 +00004299 exception);
cristybb503372010-05-27 20:51:26 +00004300 border_info.width=(size_t) floor(2.0*sigma+0.5);
4301 border_info.height=(size_t) floor(2.0*sigma+0.5);
cristy3ed852e2009-09-05 21:47:34 +00004302 border_info.x=0;
4303 border_info.y=0;
cristy9950d572011-10-01 18:22:35 +00004304 (void) QueryColorCompliance("none",AllCompliance,&clone_image->border_color,
4305 exception);
cristy70cddf72011-12-10 22:42:42 +00004306 clone_image->matte=MagickTrue;
4307 border_image=BorderImage(clone_image,&border_info,OverCompositeOp,exception);
cristy3ed852e2009-09-05 21:47:34 +00004308 clone_image=DestroyImage(clone_image);
4309 if (border_image == (Image *) NULL)
4310 return((Image *) NULL);
4311 if (border_image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00004312 (void) SetImageAlphaChannel(border_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004313 /*
4314 Shadow image.
4315 */
cristy70cddf72011-12-10 22:42:42 +00004316 status=MagickTrue;
cristydb070952012-04-20 14:33:00 +00004317 image_view=AcquireAuthenticCacheView(border_image,exception);
cristy70cddf72011-12-10 22:42:42 +00004318 for (y=0; y < (ssize_t) border_image->rows; y++)
4319 {
4320 PixelInfo
4321 background_color;
4322
4323 register Quantum
4324 *restrict q;
4325
4326 register ssize_t
4327 x;
4328
4329 if (status == MagickFalse)
4330 continue;
4331 q=QueueCacheViewAuthenticPixels(image_view,0,y,border_image->columns,1,
4332 exception);
4333 if (q == (Quantum *) NULL)
4334 {
4335 status=MagickFalse;
4336 continue;
4337 }
4338 background_color=border_image->background_color;
4339 background_color.matte=MagickTrue;
4340 for (x=0; x < (ssize_t) border_image->columns; x++)
4341 {
4342 if (border_image->matte != MagickFalse)
4343 background_color.alpha=GetPixelAlpha(border_image,q)*alpha/100.0;
4344 SetPixelInfoPixel(border_image,&background_color,q);
4345 q+=GetPixelChannels(border_image);
4346 }
4347 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4348 status=MagickFalse;
4349 }
4350 image_view=DestroyCacheView(image_view);
4351 if (status == MagickFalse)
4352 {
4353 border_image=DestroyImage(border_image);
4354 return((Image *) NULL);
4355 }
cristybd5a96c2011-08-21 00:04:26 +00004356 channel_mask=SetPixelChannelMask(border_image,AlphaChannel);
cristyaa2c16c2012-03-25 22:21:35 +00004357 shadow_image=BlurImage(border_image,0.0,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +00004358 border_image=DestroyImage(border_image);
4359 if (shadow_image == (Image *) NULL)
4360 return((Image *) NULL);
cristyae1969f2011-12-10 03:07:36 +00004361 (void) SetPixelChannelMapMask(shadow_image,channel_mask);
cristy3ed852e2009-09-05 21:47:34 +00004362 if (shadow_image->page.width == 0)
4363 shadow_image->page.width=shadow_image->columns;
4364 if (shadow_image->page.height == 0)
4365 shadow_image->page.height=shadow_image->rows;
cristybb503372010-05-27 20:51:26 +00004366 shadow_image->page.width+=x_offset-(ssize_t) border_info.width;
4367 shadow_image->page.height+=y_offset-(ssize_t) border_info.height;
4368 shadow_image->page.x+=x_offset-(ssize_t) border_info.width;
4369 shadow_image->page.y+=y_offset-(ssize_t) border_info.height;
cristy3ed852e2009-09-05 21:47:34 +00004370 return(shadow_image);
4371}
4372
4373/*
4374%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4375% %
4376% %
4377% %
4378% S k e t c h I m a g e %
4379% %
4380% %
4381% %
4382%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4383%
4384% SketchImage() simulates a pencil sketch. We convolve the image with a
4385% Gaussian operator of the given radius and standard deviation (sigma). For
4386% reasonable results, radius should be larger than sigma. Use a radius of 0
4387% and SketchImage() selects a suitable radius for you. Angle gives the angle
4388% of the sketch.
4389%
4390% The format of the SketchImage method is:
4391%
4392% Image *SketchImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00004393% const double sigma,const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004394%
4395% A description of each parameter follows:
4396%
4397% o image: the image.
4398%
cristy574cc262011-08-05 01:23:58 +00004399% o radius: the radius of the Gaussian, in pixels, not counting the
4400% center pixel.
cristy3ed852e2009-09-05 21:47:34 +00004401%
4402% o sigma: the standard deviation of the Gaussian, in pixels.
4403%
cristyf7ef0252011-09-09 14:50:06 +00004404% o angle: apply the effect along this angle.
4405%
cristy3ed852e2009-09-05 21:47:34 +00004406% o exception: return any errors or warnings in this structure.
4407%
4408*/
4409MagickExport Image *SketchImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00004410 const double sigma,const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004411{
cristyfa112112010-01-04 17:48:07 +00004412 CacheView
4413 *random_view;
4414
cristy3ed852e2009-09-05 21:47:34 +00004415 Image
4416 *blend_image,
4417 *blur_image,
4418 *dodge_image,
4419 *random_image,
4420 *sketch_image;
4421
cristy3ed852e2009-09-05 21:47:34 +00004422 MagickBooleanType
4423 status;
4424
cristy3ed852e2009-09-05 21:47:34 +00004425 RandomInfo
cristyfa112112010-01-04 17:48:07 +00004426 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00004427
cristy9d314ff2011-03-09 01:30:28 +00004428 ssize_t
4429 y;
4430
cristy57340e02012-05-05 00:53:23 +00004431 unsigned long
4432 key;
4433
cristy3ed852e2009-09-05 21:47:34 +00004434 /*
4435 Sketch image.
4436 */
4437 random_image=CloneImage(image,image->columns << 1,image->rows << 1,
4438 MagickTrue,exception);
4439 if (random_image == (Image *) NULL)
4440 return((Image *) NULL);
4441 status=MagickTrue;
cristy1b784432009-12-19 02:20:40 +00004442 random_info=AcquireRandomInfoThreadSet();
cristy57340e02012-05-05 00:53:23 +00004443 key=GetRandomSecretKey(random_info[0]);
cristydb070952012-04-20 14:33:00 +00004444 random_view=AcquireAuthenticCacheView(random_image,exception);
cristy1b784432009-12-19 02:20:40 +00004445#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy57340e02012-05-05 00:53:23 +00004446 #pragma omp parallel for schedule(static,4) shared(status) \
cristy4ee2b0c2012-05-15 00:30:35 +00004447 dynamic_number_threads(image,image->columns,image->rows,key == ~0UL)
cristy1b784432009-12-19 02:20:40 +00004448#endif
cristybb503372010-05-27 20:51:26 +00004449 for (y=0; y < (ssize_t) random_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004450 {
cristy5c9e6f22010-09-17 17:31:01 +00004451 const int
4452 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00004453
cristybb503372010-05-27 20:51:26 +00004454 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004455 x;
4456
cristy4c08aed2011-07-01 19:47:50 +00004457 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004458 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004459
cristy1b784432009-12-19 02:20:40 +00004460 if (status == MagickFalse)
4461 continue;
cristy3ed852e2009-09-05 21:47:34 +00004462 q=QueueCacheViewAuthenticPixels(random_view,0,y,random_image->columns,1,
4463 exception);
cristyacd2ed22011-08-30 01:44:23 +00004464 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004465 {
4466 status=MagickFalse;
4467 continue;
4468 }
cristybb503372010-05-27 20:51:26 +00004469 for (x=0; x < (ssize_t) random_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004470 {
cristy76f512e2011-09-12 01:26:56 +00004471 MagickRealType
4472 value;
4473
4474 register ssize_t
4475 i;
4476
cristy10a6c612012-01-29 21:41:05 +00004477 if (GetPixelMask(random_image,q) != 0)
4478 {
4479 q+=GetPixelChannels(random_image);
4480 continue;
4481 }
cristy76f512e2011-09-12 01:26:56 +00004482 value=GetPseudoRandomValue(random_info[id]);
4483 for (i=0; i < (ssize_t) GetPixelChannels(random_image); i++)
4484 {
cristyabace412011-12-11 15:56:53 +00004485 PixelChannel
4486 channel;
4487
cristy76f512e2011-09-12 01:26:56 +00004488 PixelTrait
4489 traits;
4490
cristyabace412011-12-11 15:56:53 +00004491 channel=GetPixelChannelMapChannel(image,i);
4492 traits=GetPixelChannelMapTraits(image,channel);
cristy76f512e2011-09-12 01:26:56 +00004493 if (traits == UndefinedPixelTrait)
4494 continue;
4495 q[i]=ClampToQuantum(QuantumRange*value);
4496 }
cristyed231572011-07-14 02:18:59 +00004497 q+=GetPixelChannels(random_image);
cristy3ed852e2009-09-05 21:47:34 +00004498 }
4499 if (SyncCacheViewAuthenticPixels(random_view,exception) == MagickFalse)
4500 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004501 }
4502 random_view=DestroyCacheView(random_view);
cristy1b784432009-12-19 02:20:40 +00004503 random_info=DestroyRandomInfoThreadSet(random_info);
cristy3ed852e2009-09-05 21:47:34 +00004504 if (status == MagickFalse)
4505 {
4506 random_image=DestroyImage(random_image);
4507 return(random_image);
4508 }
cristyaa2c16c2012-03-25 22:21:35 +00004509 blur_image=MotionBlurImage(random_image,radius,sigma,angle,exception);
cristy3ed852e2009-09-05 21:47:34 +00004510 random_image=DestroyImage(random_image);
4511 if (blur_image == (Image *) NULL)
4512 return((Image *) NULL);
cristy6bfd6902011-12-09 01:33:45 +00004513 dodge_image=EdgeImage(blur_image,radius,1.0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004514 blur_image=DestroyImage(blur_image);
4515 if (dodge_image == (Image *) NULL)
4516 return((Image *) NULL);
cristye23ec9d2011-08-16 18:15:40 +00004517 (void) NormalizeImage(dodge_image,exception);
cristyb3e7c6c2011-07-24 01:43:55 +00004518 (void) NegateImage(dodge_image,MagickFalse,exception);
cristye941a752011-10-15 01:52:48 +00004519 (void) TransformImage(&dodge_image,(char *) NULL,"50%",exception);
cristy3ed852e2009-09-05 21:47:34 +00004520 sketch_image=CloneImage(image,0,0,MagickTrue,exception);
4521 if (sketch_image == (Image *) NULL)
4522 {
4523 dodge_image=DestroyImage(dodge_image);
4524 return((Image *) NULL);
4525 }
cristyfeb3e962012-03-29 17:25:55 +00004526 (void) CompositeImage(sketch_image,dodge_image,ColorDodgeCompositeOp,
cristy39172402012-03-30 13:04:39 +00004527 MagickTrue,0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004528 dodge_image=DestroyImage(dodge_image);
4529 blend_image=CloneImage(image,0,0,MagickTrue,exception);
4530 if (blend_image == (Image *) NULL)
4531 {
4532 sketch_image=DestroyImage(sketch_image);
4533 return((Image *) NULL);
4534 }
4535 (void) SetImageArtifact(blend_image,"compose:args","20x80");
cristy39172402012-03-30 13:04:39 +00004536 (void) CompositeImage(sketch_image,blend_image,BlendCompositeOp,MagickTrue,
cristyfeb3e962012-03-29 17:25:55 +00004537 0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004538 blend_image=DestroyImage(blend_image);
4539 return(sketch_image);
4540}
4541
4542/*
4543%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4544% %
4545% %
4546% %
4547% S o l a r i z e I m a g e %
4548% %
4549% %
4550% %
4551%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4552%
4553% SolarizeImage() applies a special effect to the image, similar to the effect
4554% achieved in a photo darkroom by selectively exposing areas of photo
4555% sensitive paper to light. Threshold ranges from 0 to QuantumRange and is a
4556% measure of the extent of the solarization.
4557%
4558% The format of the SolarizeImage method is:
4559%
cristy5cbc0162011-08-29 00:36:28 +00004560% MagickBooleanType SolarizeImage(Image *image,const double threshold,
4561% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004562%
4563% A description of each parameter follows:
4564%
4565% o image: the image.
4566%
4567% o threshold: Define the extent of the solarization.
4568%
cristy5cbc0162011-08-29 00:36:28 +00004569% o exception: return any errors or warnings in this structure.
4570%
cristy3ed852e2009-09-05 21:47:34 +00004571*/
4572MagickExport MagickBooleanType SolarizeImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00004573 const double threshold,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004574{
4575#define SolarizeImageTag "Solarize/Image"
4576
cristyc4c8d132010-01-07 01:58:38 +00004577 CacheView
4578 *image_view;
4579
cristy3ed852e2009-09-05 21:47:34 +00004580 MagickBooleanType
4581 status;
4582
cristybb503372010-05-27 20:51:26 +00004583 MagickOffsetType
4584 progress;
4585
4586 ssize_t
4587 y;
4588
cristy3ed852e2009-09-05 21:47:34 +00004589 assert(image != (Image *) NULL);
4590 assert(image->signature == MagickSignature);
4591 if (image->debug != MagickFalse)
4592 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4593 if (image->storage_class == PseudoClass)
4594 {
cristybb503372010-05-27 20:51:26 +00004595 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004596 i;
4597
4598 /*
4599 Solarize colormap.
4600 */
cristybb503372010-05-27 20:51:26 +00004601 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00004602 {
4603 if ((MagickRealType) image->colormap[i].red > threshold)
cristy6e963d82012-06-19 15:23:24 +00004604 image->colormap[i].red=QuantumRange-image->colormap[i].red;
cristy3ed852e2009-09-05 21:47:34 +00004605 if ((MagickRealType) image->colormap[i].green > threshold)
cristy6e963d82012-06-19 15:23:24 +00004606 image->colormap[i].green=QuantumRange-
cristy3ed852e2009-09-05 21:47:34 +00004607 image->colormap[i].green;
4608 if ((MagickRealType) image->colormap[i].blue > threshold)
cristy6e963d82012-06-19 15:23:24 +00004609 image->colormap[i].blue=QuantumRange-
cristy3ed852e2009-09-05 21:47:34 +00004610 image->colormap[i].blue;
4611 }
4612 }
4613 /*
4614 Solarize image.
4615 */
4616 status=MagickTrue;
4617 progress=0;
cristydb070952012-04-20 14:33:00 +00004618 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00004619#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00004620 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00004621 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00004622#endif
cristybb503372010-05-27 20:51:26 +00004623 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004624 {
cristybb503372010-05-27 20:51:26 +00004625 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004626 x;
4627
cristy4c08aed2011-07-01 19:47:50 +00004628 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004629 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004630
4631 if (status == MagickFalse)
4632 continue;
cristy5cbc0162011-08-29 00:36:28 +00004633 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00004634 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004635 {
4636 status=MagickFalse;
4637 continue;
4638 }
cristybb503372010-05-27 20:51:26 +00004639 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004640 {
cristy76f512e2011-09-12 01:26:56 +00004641 register ssize_t
4642 i;
4643
cristy10a6c612012-01-29 21:41:05 +00004644 if (GetPixelMask(image,q) != 0)
4645 {
4646 q+=GetPixelChannels(image);
4647 continue;
4648 }
cristy76f512e2011-09-12 01:26:56 +00004649 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4650 {
cristyabace412011-12-11 15:56:53 +00004651 PixelChannel
4652 channel;
4653
cristy76f512e2011-09-12 01:26:56 +00004654 PixelTrait
4655 traits;
4656
cristyabace412011-12-11 15:56:53 +00004657 channel=GetPixelChannelMapChannel(image,i);
4658 traits=GetPixelChannelMapTraits(image,channel);
cristy76f512e2011-09-12 01:26:56 +00004659 if ((traits == UndefinedPixelTrait) ||
4660 ((traits & CopyPixelTrait) != 0))
4661 continue;
4662 if ((MagickRealType) q[i] > threshold)
4663 q[i]=QuantumRange-q[i];
4664 }
cristyed231572011-07-14 02:18:59 +00004665 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00004666 }
4667 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4668 status=MagickFalse;
4669 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4670 {
4671 MagickBooleanType
4672 proceed;
4673
cristyb5d5f722009-11-04 03:03:49 +00004674#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00004675 #pragma omp critical (MagickCore_SolarizeImage)
cristy3ed852e2009-09-05 21:47:34 +00004676#endif
4677 proceed=SetImageProgress(image,SolarizeImageTag,progress++,image->rows);
4678 if (proceed == MagickFalse)
4679 status=MagickFalse;
4680 }
4681 }
4682 image_view=DestroyCacheView(image_view);
4683 return(status);
4684}
4685
4686/*
4687%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4688% %
4689% %
4690% %
4691% S t e g a n o I m a g e %
4692% %
4693% %
4694% %
4695%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4696%
4697% SteganoImage() hides a digital watermark within the image. Recover
4698% the hidden watermark later to prove that the authenticity of an image.
4699% Offset defines the start position within the image to hide the watermark.
4700%
4701% The format of the SteganoImage method is:
4702%
4703% Image *SteganoImage(const Image *image,Image *watermark,
4704% ExceptionInfo *exception)
4705%
4706% A description of each parameter follows:
4707%
4708% o image: the image.
4709%
4710% o watermark: the watermark image.
4711%
4712% o exception: return any errors or warnings in this structure.
4713%
4714*/
4715MagickExport Image *SteganoImage(const Image *image,const Image *watermark,
4716 ExceptionInfo *exception)
4717{
cristye1bf8ad2010-09-19 17:07:03 +00004718#define GetBit(alpha,i) ((((size_t) (alpha) >> (size_t) (i)) & 0x01) != 0)
cristy4c08aed2011-07-01 19:47:50 +00004719#define SetBit(alpha,i,set) (Quantum) ((set) != 0 ? (size_t) (alpha) \
cristyeaedf062010-05-29 22:36:02 +00004720 | (one << (size_t) (i)) : (size_t) (alpha) & ~(one << (size_t) (i)))
cristy3ed852e2009-09-05 21:47:34 +00004721#define SteganoImageTag "Stegano/Image"
4722
cristyb0d3bb92010-09-22 14:37:58 +00004723 CacheView
4724 *stegano_view,
4725 *watermark_view;
4726
cristy3ed852e2009-09-05 21:47:34 +00004727 Image
4728 *stegano_image;
4729
4730 int
4731 c;
4732
cristy3ed852e2009-09-05 21:47:34 +00004733 MagickBooleanType
4734 status;
4735
cristy101ab702011-10-13 13:06:32 +00004736 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00004737 pixel;
4738
cristy4c08aed2011-07-01 19:47:50 +00004739 register Quantum
cristy3ed852e2009-09-05 21:47:34 +00004740 *q;
4741
cristye1bf8ad2010-09-19 17:07:03 +00004742 register ssize_t
4743 x;
4744
cristybb503372010-05-27 20:51:26 +00004745 size_t
cristyeaedf062010-05-29 22:36:02 +00004746 depth,
4747 one;
cristy3ed852e2009-09-05 21:47:34 +00004748
cristye1bf8ad2010-09-19 17:07:03 +00004749 ssize_t
4750 i,
4751 j,
4752 k,
4753 y;
4754
cristy3ed852e2009-09-05 21:47:34 +00004755 /*
4756 Initialize steganographic image attributes.
4757 */
4758 assert(image != (const Image *) NULL);
4759 assert(image->signature == MagickSignature);
4760 if (image->debug != MagickFalse)
4761 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4762 assert(watermark != (const Image *) NULL);
4763 assert(watermark->signature == MagickSignature);
4764 assert(exception != (ExceptionInfo *) NULL);
4765 assert(exception->signature == MagickSignature);
cristyeaedf062010-05-29 22:36:02 +00004766 one=1UL;
cristy3ed852e2009-09-05 21:47:34 +00004767 stegano_image=CloneImage(image,0,0,MagickTrue,exception);
4768 if (stegano_image == (Image *) NULL)
4769 return((Image *) NULL);
cristyf61b1832012-04-01 01:38:19 +00004770 stegano_image->depth=MAGICKCORE_QUANTUM_DEPTH;
cristy574cc262011-08-05 01:23:58 +00004771 if (SetImageStorageClass(stegano_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004772 {
cristy3ed852e2009-09-05 21:47:34 +00004773 stegano_image=DestroyImage(stegano_image);
4774 return((Image *) NULL);
4775 }
cristy3ed852e2009-09-05 21:47:34 +00004776 /*
4777 Hide watermark in low-order bits of image.
4778 */
4779 c=0;
4780 i=0;
4781 j=0;
4782 depth=stegano_image->depth;
cristyf61b1832012-04-01 01:38:19 +00004783 k=stegano_image->offset;
cristyda16f162011-02-19 23:52:17 +00004784 status=MagickTrue;
cristydb070952012-04-20 14:33:00 +00004785 watermark_view=AcquireVirtualCacheView(watermark,exception);
4786 stegano_view=AcquireAuthenticCacheView(stegano_image,exception);
cristybb503372010-05-27 20:51:26 +00004787 for (i=(ssize_t) depth-1; (i >= 0) && (j < (ssize_t) depth); i--)
cristy3ed852e2009-09-05 21:47:34 +00004788 {
cristybb503372010-05-27 20:51:26 +00004789 for (y=0; (y < (ssize_t) watermark->rows) && (j < (ssize_t) depth); y++)
cristy3ed852e2009-09-05 21:47:34 +00004790 {
cristybb503372010-05-27 20:51:26 +00004791 for (x=0; (x < (ssize_t) watermark->columns) && (j < (ssize_t) depth); x++)
cristy3ed852e2009-09-05 21:47:34 +00004792 {
cristy1707c6c2012-01-18 23:30:54 +00004793 ssize_t
4794 offset;
4795
cristyf05d4942012-03-17 16:26:09 +00004796 (void) GetOneCacheViewVirtualPixelInfo(watermark_view,x,y,&pixel,
cristyda1f9c12011-10-02 21:39:49 +00004797 exception);
cristy1707c6c2012-01-18 23:30:54 +00004798 offset=k/(ssize_t) stegano_image->columns;
4799 if (offset >= (ssize_t) stegano_image->rows)
cristy3ed852e2009-09-05 21:47:34 +00004800 break;
cristyb0d3bb92010-09-22 14:37:58 +00004801 q=GetCacheViewAuthenticPixels(stegano_view,k % (ssize_t)
4802 stegano_image->columns,k/(ssize_t) stegano_image->columns,1,1,
4803 exception);
cristyacd2ed22011-08-30 01:44:23 +00004804 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004805 break;
4806 switch (c)
4807 {
4808 case 0:
4809 {
cristyf61b1832012-04-01 01:38:19 +00004810 SetPixelRed(stegano_image,SetBit(GetPixelRed(stegano_image,q),j,
4811 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004812 break;
4813 }
4814 case 1:
4815 {
cristyf61b1832012-04-01 01:38:19 +00004816 SetPixelGreen(stegano_image,SetBit(GetPixelGreen(stegano_image,q),j,
4817 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004818 break;
4819 }
4820 case 2:
4821 {
cristyf61b1832012-04-01 01:38:19 +00004822 SetPixelBlue(stegano_image,SetBit(GetPixelBlue(stegano_image,q),j,
4823 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004824 break;
4825 }
4826 }
cristyb0d3bb92010-09-22 14:37:58 +00004827 if (SyncCacheViewAuthenticPixels(stegano_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004828 break;
4829 c++;
4830 if (c == 3)
4831 c=0;
4832 k++;
cristybb503372010-05-27 20:51:26 +00004833 if (k == (ssize_t) (stegano_image->columns*stegano_image->columns))
cristy3ed852e2009-09-05 21:47:34 +00004834 k=0;
cristyf61b1832012-04-01 01:38:19 +00004835 if (k == stegano_image->offset)
cristy3ed852e2009-09-05 21:47:34 +00004836 j++;
4837 }
4838 }
cristy8b27a6d2010-02-14 03:31:15 +00004839 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004840 {
cristy8b27a6d2010-02-14 03:31:15 +00004841 MagickBooleanType
4842 proceed;
4843
4844 proceed=SetImageProgress(image,SteganoImageTag,(MagickOffsetType)
4845 (depth-i),depth);
4846 if (proceed == MagickFalse)
4847 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004848 }
4849 }
cristyb0d3bb92010-09-22 14:37:58 +00004850 stegano_view=DestroyCacheView(stegano_view);
4851 watermark_view=DestroyCacheView(watermark_view);
cristyda16f162011-02-19 23:52:17 +00004852 if (status == MagickFalse)
4853 {
4854 stegano_image=DestroyImage(stegano_image);
4855 return((Image *) NULL);
4856 }
cristy3ed852e2009-09-05 21:47:34 +00004857 return(stegano_image);
4858}
4859
4860/*
4861%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4862% %
4863% %
4864% %
4865% S t e r e o A n a g l y p h I m a g e %
4866% %
4867% %
4868% %
4869%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4870%
4871% StereoAnaglyphImage() combines two images and produces a single image that
4872% is the composite of a left and right image of a stereo pair. Special
4873% red-green stereo glasses are required to view this effect.
4874%
4875% The format of the StereoAnaglyphImage method is:
4876%
4877% Image *StereoImage(const Image *left_image,const Image *right_image,
4878% ExceptionInfo *exception)
4879% Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004880% const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004881% ExceptionInfo *exception)
4882%
4883% A description of each parameter follows:
4884%
4885% o left_image: the left image.
4886%
4887% o right_image: the right image.
4888%
4889% o exception: return any errors or warnings in this structure.
4890%
4891% o x_offset: amount, in pixels, by which the left image is offset to the
4892% right of the right image.
4893%
4894% o y_offset: amount, in pixels, by which the left image is offset to the
4895% bottom of the right image.
4896%
4897%
4898*/
4899MagickExport Image *StereoImage(const Image *left_image,
4900 const Image *right_image,ExceptionInfo *exception)
4901{
4902 return(StereoAnaglyphImage(left_image,right_image,0,0,exception));
4903}
4904
4905MagickExport Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004906 const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004907 ExceptionInfo *exception)
4908{
4909#define StereoImageTag "Stereo/Image"
4910
4911 const Image
4912 *image;
4913
4914 Image
4915 *stereo_image;
4916
cristy3ed852e2009-09-05 21:47:34 +00004917 MagickBooleanType
4918 status;
4919
cristy9d314ff2011-03-09 01:30:28 +00004920 ssize_t
4921 y;
4922
cristy3ed852e2009-09-05 21:47:34 +00004923 assert(left_image != (const Image *) NULL);
4924 assert(left_image->signature == MagickSignature);
4925 if (left_image->debug != MagickFalse)
4926 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4927 left_image->filename);
4928 assert(right_image != (const Image *) NULL);
4929 assert(right_image->signature == MagickSignature);
4930 assert(exception != (ExceptionInfo *) NULL);
4931 assert(exception->signature == MagickSignature);
4932 assert(right_image != (const Image *) NULL);
4933 image=left_image;
4934 if ((left_image->columns != right_image->columns) ||
4935 (left_image->rows != right_image->rows))
4936 ThrowImageException(ImageError,"LeftAndRightImageSizesDiffer");
4937 /*
4938 Initialize stereo image attributes.
4939 */
4940 stereo_image=CloneImage(left_image,left_image->columns,left_image->rows,
4941 MagickTrue,exception);
4942 if (stereo_image == (Image *) NULL)
4943 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004944 if (SetImageStorageClass(stereo_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004945 {
cristy3ed852e2009-09-05 21:47:34 +00004946 stereo_image=DestroyImage(stereo_image);
4947 return((Image *) NULL);
4948 }
cristy079c78d2012-07-03 11:53:48 +00004949 (void) SetImageColorspace(stereo_image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00004950 /*
4951 Copy left image to red channel and right image to blue channel.
4952 */
cristyda16f162011-02-19 23:52:17 +00004953 status=MagickTrue;
cristybb503372010-05-27 20:51:26 +00004954 for (y=0; y < (ssize_t) stereo_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004955 {
cristy4c08aed2011-07-01 19:47:50 +00004956 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004957 *restrict p,
4958 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004959
cristybb503372010-05-27 20:51:26 +00004960 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004961 x;
4962
cristy4c08aed2011-07-01 19:47:50 +00004963 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004964 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +00004965
4966 p=GetVirtualPixels(left_image,-x_offset,y-y_offset,image->columns,1,
4967 exception);
4968 q=GetVirtualPixels(right_image,0,y,right_image->columns,1,exception);
4969 r=QueueAuthenticPixels(stereo_image,0,y,stereo_image->columns,1,exception);
cristy76f512e2011-09-12 01:26:56 +00004970 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL) ||
4971 (r == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004972 break;
cristybb503372010-05-27 20:51:26 +00004973 for (x=0; x < (ssize_t) stereo_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004974 {
cristy4c08aed2011-07-01 19:47:50 +00004975 SetPixelRed(image,GetPixelRed(left_image,p),r);
cristy76f512e2011-09-12 01:26:56 +00004976 SetPixelGreen(image,GetPixelGreen(right_image,q),r);
4977 SetPixelBlue(image,GetPixelBlue(right_image,q),r);
4978 if ((GetPixelAlphaTraits(stereo_image) & CopyPixelTrait) != 0)
4979 SetPixelAlpha(image,(GetPixelAlpha(left_image,p)+
4980 GetPixelAlpha(right_image,q))/2,r);
cristyed231572011-07-14 02:18:59 +00004981 p+=GetPixelChannels(left_image);
cristy76f512e2011-09-12 01:26:56 +00004982 q+=GetPixelChannels(right_image);
4983 r+=GetPixelChannels(stereo_image);
cristy3ed852e2009-09-05 21:47:34 +00004984 }
4985 if (SyncAuthenticPixels(stereo_image,exception) == MagickFalse)
4986 break;
cristy8b27a6d2010-02-14 03:31:15 +00004987 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004988 {
cristy8b27a6d2010-02-14 03:31:15 +00004989 MagickBooleanType
4990 proceed;
4991
cristybb503372010-05-27 20:51:26 +00004992 proceed=SetImageProgress(image,StereoImageTag,(MagickOffsetType) y,
4993 stereo_image->rows);
cristy8b27a6d2010-02-14 03:31:15 +00004994 if (proceed == MagickFalse)
4995 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004996 }
4997 }
cristyda16f162011-02-19 23:52:17 +00004998 if (status == MagickFalse)
4999 {
5000 stereo_image=DestroyImage(stereo_image);
5001 return((Image *) NULL);
5002 }
cristy3ed852e2009-09-05 21:47:34 +00005003 return(stereo_image);
5004}
5005
5006/*
5007%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5008% %
5009% %
5010% %
5011% S w i r l I m a g e %
5012% %
5013% %
5014% %
5015%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5016%
5017% SwirlImage() swirls the pixels about the center of the image, where
5018% degrees indicates the sweep of the arc through which each pixel is moved.
5019% You get a more dramatic effect as the degrees move from 1 to 360.
5020%
5021% The format of the SwirlImage method is:
5022%
5023% Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00005024% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005025%
5026% A description of each parameter follows:
5027%
5028% o image: the image.
5029%
5030% o degrees: Define the tightness of the swirling effect.
5031%
cristy76f512e2011-09-12 01:26:56 +00005032% o method: the pixel interpolation method.
5033%
cristy3ed852e2009-09-05 21:47:34 +00005034% o exception: return any errors or warnings in this structure.
5035%
5036*/
5037MagickExport Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00005038 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005039{
5040#define SwirlImageTag "Swirl/Image"
5041
cristyfa112112010-01-04 17:48:07 +00005042 CacheView
5043 *image_view,
5044 *swirl_view;
5045
cristy3ed852e2009-09-05 21:47:34 +00005046 Image
5047 *swirl_image;
5048
cristy3ed852e2009-09-05 21:47:34 +00005049 MagickBooleanType
5050 status;
5051
cristybb503372010-05-27 20:51:26 +00005052 MagickOffsetType
5053 progress;
5054
cristy3ed852e2009-09-05 21:47:34 +00005055 MagickRealType
5056 radius;
5057
5058 PointInfo
5059 center,
5060 scale;
5061
cristybb503372010-05-27 20:51:26 +00005062 ssize_t
5063 y;
5064
cristy3ed852e2009-09-05 21:47:34 +00005065 /*
5066 Initialize swirl image attributes.
5067 */
5068 assert(image != (const Image *) NULL);
5069 assert(image->signature == MagickSignature);
5070 if (image->debug != MagickFalse)
5071 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5072 assert(exception != (ExceptionInfo *) NULL);
5073 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00005074 swirl_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005075 if (swirl_image == (Image *) NULL)
5076 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005077 if (SetImageStorageClass(swirl_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005078 {
cristy3ed852e2009-09-05 21:47:34 +00005079 swirl_image=DestroyImage(swirl_image);
5080 return((Image *) NULL);
5081 }
cristy4c08aed2011-07-01 19:47:50 +00005082 if (swirl_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00005083 swirl_image->matte=MagickTrue;
5084 /*
5085 Compute scaling factor.
5086 */
5087 center.x=(double) image->columns/2.0;
5088 center.y=(double) image->rows/2.0;
5089 radius=MagickMax(center.x,center.y);
5090 scale.x=1.0;
5091 scale.y=1.0;
5092 if (image->columns > image->rows)
5093 scale.y=(double) image->columns/(double) image->rows;
5094 else
5095 if (image->columns < image->rows)
5096 scale.x=(double) image->rows/(double) image->columns;
5097 degrees=(double) DegreesToRadians(degrees);
5098 /*
5099 Swirl image.
5100 */
5101 status=MagickTrue;
5102 progress=0;
cristydb070952012-04-20 14:33:00 +00005103 image_view=AcquireVirtualCacheView(image,exception);
5104 swirl_view=AcquireAuthenticCacheView(swirl_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00005105#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00005106 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00005107 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00005108#endif
cristybb503372010-05-27 20:51:26 +00005109 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005110 {
cristy3ed852e2009-09-05 21:47:34 +00005111 MagickRealType
5112 distance;
5113
5114 PointInfo
5115 delta;
5116
cristy6d188022011-09-12 13:23:33 +00005117 register const Quantum
5118 *restrict p;
5119
cristybb503372010-05-27 20:51:26 +00005120 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005121 x;
5122
cristy4c08aed2011-07-01 19:47:50 +00005123 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005124 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005125
5126 if (status == MagickFalse)
5127 continue;
cristy6d188022011-09-12 13:23:33 +00005128 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy1707c6c2012-01-18 23:30:54 +00005129 q=QueueCacheViewAuthenticPixels(swirl_view,0,y,swirl_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00005130 exception);
cristy6d188022011-09-12 13:23:33 +00005131 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005132 {
5133 status=MagickFalse;
5134 continue;
5135 }
cristy3ed852e2009-09-05 21:47:34 +00005136 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00005137 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005138 {
5139 /*
5140 Determine if the pixel is within an ellipse.
5141 */
cristy10a6c612012-01-29 21:41:05 +00005142 if (GetPixelMask(image,p) != 0)
5143 {
5144 p+=GetPixelChannels(image);
5145 q+=GetPixelChannels(swirl_image);
5146 continue;
5147 }
cristy3ed852e2009-09-05 21:47:34 +00005148 delta.x=scale.x*(double) (x-center.x);
5149 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00005150 if (distance >= (radius*radius))
5151 {
cristy1707c6c2012-01-18 23:30:54 +00005152 register ssize_t
5153 i;
5154
cristy6d188022011-09-12 13:23:33 +00005155 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy1707c6c2012-01-18 23:30:54 +00005156 {
5157 PixelChannel
5158 channel;
5159
5160 PixelTrait
5161 swirl_traits,
5162 traits;
5163
5164 channel=GetPixelChannelMapChannel(image,i);
5165 traits=GetPixelChannelMapTraits(image,channel);
5166 swirl_traits=GetPixelChannelMapTraits(swirl_image,channel);
5167 if ((traits == UndefinedPixelTrait) ||
5168 (swirl_traits == UndefinedPixelTrait))
5169 continue;
5170 SetPixelChannel(swirl_image,channel,p[i],q);
5171 }
cristy6d188022011-09-12 13:23:33 +00005172 }
5173 else
cristy3ed852e2009-09-05 21:47:34 +00005174 {
5175 MagickRealType
5176 cosine,
5177 factor,
5178 sine;
5179
5180 /*
5181 Swirl the pixel.
5182 */
5183 factor=1.0-sqrt((double) distance)/radius;
5184 sine=sin((double) (degrees*factor*factor));
5185 cosine=cos((double) (degrees*factor*factor));
cristy76f512e2011-09-12 01:26:56 +00005186 status=InterpolatePixelChannels(image,image_view,swirl_image,method,
5187 ((cosine*delta.x-sine*delta.y)/scale.x+center.x),(double)
5188 ((sine*delta.x+cosine*delta.y)/scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00005189 }
cristy6d188022011-09-12 13:23:33 +00005190 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00005191 q+=GetPixelChannels(swirl_image);
cristy3ed852e2009-09-05 21:47:34 +00005192 }
5193 if (SyncCacheViewAuthenticPixels(swirl_view,exception) == MagickFalse)
5194 status=MagickFalse;
5195 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5196 {
5197 MagickBooleanType
5198 proceed;
5199
cristyb5d5f722009-11-04 03:03:49 +00005200#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005201 #pragma omp critical (MagickCore_SwirlImage)
cristy3ed852e2009-09-05 21:47:34 +00005202#endif
5203 proceed=SetImageProgress(image,SwirlImageTag,progress++,image->rows);
5204 if (proceed == MagickFalse)
5205 status=MagickFalse;
5206 }
5207 }
5208 swirl_view=DestroyCacheView(swirl_view);
5209 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00005210 if (status == MagickFalse)
5211 swirl_image=DestroyImage(swirl_image);
5212 return(swirl_image);
5213}
5214
5215/*
5216%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5217% %
5218% %
5219% %
5220% T i n t I m a g e %
5221% %
5222% %
5223% %
5224%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5225%
5226% TintImage() applies a color vector to each pixel in the image. The length
5227% of the vector is 0 for black and white and at its maximum for the midtones.
5228% The vector weighting function is f(x)=(1-(4.0*((x-0.5)*(x-0.5))))
5229%
5230% The format of the TintImage method is:
5231%
cristyb817c3f2011-10-03 14:00:35 +00005232% Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005233% const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005234%
5235% A description of each parameter follows:
5236%
5237% o image: the image.
5238%
cristyb817c3f2011-10-03 14:00:35 +00005239% o blend: A color value used for tinting.
cristy3ed852e2009-09-05 21:47:34 +00005240%
5241% o tint: A color value used for tinting.
5242%
5243% o exception: return any errors or warnings in this structure.
5244%
5245*/
cristyb817c3f2011-10-03 14:00:35 +00005246MagickExport Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005247 const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005248{
5249#define TintImageTag "Tint/Image"
5250
cristyc4c8d132010-01-07 01:58:38 +00005251 CacheView
5252 *image_view,
5253 *tint_view;
5254
cristy3ed852e2009-09-05 21:47:34 +00005255 GeometryInfo
5256 geometry_info;
5257
5258 Image
5259 *tint_image;
5260
cristy3ed852e2009-09-05 21:47:34 +00005261 MagickBooleanType
5262 status;
5263
cristybb503372010-05-27 20:51:26 +00005264 MagickOffsetType
5265 progress;
cristy3ed852e2009-09-05 21:47:34 +00005266
cristy28474bf2011-09-11 23:32:52 +00005267 MagickRealType
5268 intensity;
5269
cristy4c08aed2011-07-01 19:47:50 +00005270 PixelInfo
cristy1707c6c2012-01-18 23:30:54 +00005271 color_vector;
cristy3ed852e2009-09-05 21:47:34 +00005272
cristybb503372010-05-27 20:51:26 +00005273 MagickStatusType
5274 flags;
5275
5276 ssize_t
5277 y;
5278
cristy3ed852e2009-09-05 21:47:34 +00005279 /*
5280 Allocate tint image.
5281 */
5282 assert(image != (const Image *) NULL);
5283 assert(image->signature == MagickSignature);
5284 if (image->debug != MagickFalse)
5285 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5286 assert(exception != (ExceptionInfo *) NULL);
5287 assert(exception->signature == MagickSignature);
5288 tint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5289 if (tint_image == (Image *) NULL)
5290 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005291 if (SetImageStorageClass(tint_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005292 {
cristy3ed852e2009-09-05 21:47:34 +00005293 tint_image=DestroyImage(tint_image);
5294 return((Image *) NULL);
5295 }
cristy71aac542012-05-18 12:06:35 +00005296 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
5297 (IsPixelInfoGray(tint) == MagickFalse))
cristyb09db112012-07-11 12:04:31 +00005298 (void) SetImageColorspace(tint_image,RGBColorspace,exception);
cristyaed9c382011-10-03 17:54:21 +00005299 if (blend == (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005300 return(tint_image);
5301 /*
5302 Determine RGB values of the color.
5303 */
cristy1707c6c2012-01-18 23:30:54 +00005304 GetPixelInfo(image,&color_vector);
cristyb817c3f2011-10-03 14:00:35 +00005305 flags=ParseGeometry(blend,&geometry_info);
cristy1707c6c2012-01-18 23:30:54 +00005306 color_vector.red=geometry_info.rho;
5307 color_vector.green=geometry_info.rho;
5308 color_vector.blue=geometry_info.rho;
5309 color_vector.alpha=OpaqueAlpha;
cristy3ed852e2009-09-05 21:47:34 +00005310 if ((flags & SigmaValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005311 color_vector.green=geometry_info.sigma;
cristy3ed852e2009-09-05 21:47:34 +00005312 if ((flags & XiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005313 color_vector.blue=geometry_info.xi;
cristyb817c3f2011-10-03 14:00:35 +00005314 if ((flags & PsiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005315 color_vector.alpha=geometry_info.psi;
cristy76f512e2011-09-12 01:26:56 +00005316 if (image->colorspace == CMYKColorspace)
5317 {
cristy1707c6c2012-01-18 23:30:54 +00005318 color_vector.black=geometry_info.rho;
cristy76f512e2011-09-12 01:26:56 +00005319 if ((flags & PsiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005320 color_vector.black=geometry_info.psi;
cristy76f512e2011-09-12 01:26:56 +00005321 if ((flags & ChiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005322 color_vector.alpha=geometry_info.chi;
cristy76f512e2011-09-12 01:26:56 +00005323 }
cristy28474bf2011-09-11 23:32:52 +00005324 intensity=(MagickRealType) GetPixelInfoIntensity(tint);
cristy1707c6c2012-01-18 23:30:54 +00005325 color_vector.red=(MagickRealType) (color_vector.red*tint->red/100.0-
5326 intensity);
5327 color_vector.green=(MagickRealType) (color_vector.green*tint->green/100.0-
5328 intensity);
5329 color_vector.blue=(MagickRealType) (color_vector.blue*tint->blue/100.0-
5330 intensity);
5331 color_vector.black=(MagickRealType) (color_vector.black*tint->black/100.0-
5332 intensity);
5333 color_vector.alpha=(MagickRealType) (color_vector.alpha*tint->alpha/100.0-
5334 intensity);
cristy3ed852e2009-09-05 21:47:34 +00005335 /*
5336 Tint image.
5337 */
5338 status=MagickTrue;
5339 progress=0;
cristydb070952012-04-20 14:33:00 +00005340 image_view=AcquireVirtualCacheView(image,exception);
5341 tint_view=AcquireAuthenticCacheView(tint_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00005342#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00005343 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00005344 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00005345#endif
cristybb503372010-05-27 20:51:26 +00005346 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005347 {
cristy4c08aed2011-07-01 19:47:50 +00005348 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00005349 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00005350
cristy4c08aed2011-07-01 19:47:50 +00005351 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005352 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005353
cristy6b91acb2011-04-19 12:23:54 +00005354 register ssize_t
5355 x;
5356
cristy3ed852e2009-09-05 21:47:34 +00005357 if (status == MagickFalse)
5358 continue;
5359 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5360 q=QueueCacheViewAuthenticPixels(tint_view,0,y,tint_image->columns,1,
5361 exception);
cristy4c08aed2011-07-01 19:47:50 +00005362 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005363 {
5364 status=MagickFalse;
5365 continue;
5366 }
cristybb503372010-05-27 20:51:26 +00005367 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005368 {
cristy4c08aed2011-07-01 19:47:50 +00005369 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00005370 pixel;
5371
5372 MagickRealType
5373 weight;
5374
cristy1707c6c2012-01-18 23:30:54 +00005375 register ssize_t
5376 i;
5377
cristy10a6c612012-01-29 21:41:05 +00005378 if (GetPixelMask(image,p) != 0)
5379 {
5380 p+=GetPixelChannels(image);
5381 q+=GetPixelChannels(tint_image);
5382 continue;
5383 }
cristy1707c6c2012-01-18 23:30:54 +00005384 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5385 {
5386 PixelChannel
5387 channel;
5388
5389 PixelTrait
5390 tint_traits,
5391 traits;
5392
5393 channel=GetPixelChannelMapChannel(image,i);
5394 traits=GetPixelChannelMapTraits(image,channel);
5395 tint_traits=GetPixelChannelMapTraits(tint_image,channel);
5396 if ((traits == UndefinedPixelTrait) ||
5397 (tint_traits == UndefinedPixelTrait))
5398 continue;
5399 if ((tint_traits & CopyPixelTrait) != 0)
5400 {
5401 SetPixelChannel(tint_image,channel,p[i],q);
5402 continue;
5403 }
5404 }
5405 GetPixelInfo(image,&pixel);
5406 weight=QuantumScale*GetPixelRed(image,p)-0.5;
5407 pixel.red=(MagickRealType) GetPixelRed(image,p)+color_vector.red*
5408 (1.0-(4.0*(weight*weight)));
5409 weight=QuantumScale*GetPixelGreen(image,p)-0.5;
5410 pixel.green=(MagickRealType) GetPixelGreen(image,p)+color_vector.green*
5411 (1.0-(4.0*(weight*weight)));
5412 weight=QuantumScale*GetPixelBlue(image,p)-0.5;
5413 pixel.blue=(MagickRealType) GetPixelBlue(image,p)+color_vector.blue*
5414 (1.0-(4.0*(weight*weight)));
5415 weight=QuantumScale*GetPixelBlack(image,p)-0.5;
5416 pixel.black=(MagickRealType) GetPixelBlack(image,p)+color_vector.black*
5417 (1.0-(4.0*(weight*weight)));
5418 SetPixelInfoPixel(tint_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +00005419 p+=GetPixelChannels(image);
5420 q+=GetPixelChannels(tint_image);
cristy3ed852e2009-09-05 21:47:34 +00005421 }
5422 if (SyncCacheViewAuthenticPixels(tint_view,exception) == MagickFalse)
5423 status=MagickFalse;
5424 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5425 {
5426 MagickBooleanType
5427 proceed;
5428
cristyb5d5f722009-11-04 03:03:49 +00005429#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005430 #pragma omp critical (MagickCore_TintImage)
cristy3ed852e2009-09-05 21:47:34 +00005431#endif
5432 proceed=SetImageProgress(image,TintImageTag,progress++,image->rows);
5433 if (proceed == MagickFalse)
5434 status=MagickFalse;
5435 }
5436 }
5437 tint_view=DestroyCacheView(tint_view);
5438 image_view=DestroyCacheView(image_view);
5439 if (status == MagickFalse)
5440 tint_image=DestroyImage(tint_image);
5441 return(tint_image);
5442}
5443
5444/*
5445%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5446% %
5447% %
5448% %
5449% V i g n e t t e I m a g e %
5450% %
5451% %
5452% %
5453%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5454%
5455% VignetteImage() softens the edges of the image in vignette style.
5456%
5457% The format of the VignetteImage method is:
5458%
5459% Image *VignetteImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00005460% const double sigma,const ssize_t x,const ssize_t y,
cristy05c0c9a2011-09-05 23:16:13 +00005461% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005462%
5463% A description of each parameter follows:
5464%
5465% o image: the image.
5466%
5467% o radius: the radius of the pixel neighborhood.
5468%
5469% o sigma: the standard deviation of the Gaussian, in pixels.
5470%
5471% o x, y: Define the x and y ellipse offset.
5472%
5473% o exception: return any errors or warnings in this structure.
5474%
5475*/
5476MagickExport Image *VignetteImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00005477 const double sigma,const ssize_t x,const ssize_t y,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005478{
5479 char
5480 ellipse[MaxTextExtent];
5481
5482 DrawInfo
5483 *draw_info;
5484
5485 Image
5486 *canvas_image,
5487 *blur_image,
5488 *oval_image,
5489 *vignette_image;
5490
5491 assert(image != (Image *) NULL);
5492 assert(image->signature == MagickSignature);
5493 if (image->debug != MagickFalse)
5494 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5495 assert(exception != (ExceptionInfo *) NULL);
5496 assert(exception->signature == MagickSignature);
5497 canvas_image=CloneImage(image,0,0,MagickTrue,exception);
5498 if (canvas_image == (Image *) NULL)
5499 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005500 if (SetImageStorageClass(canvas_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005501 {
cristy3ed852e2009-09-05 21:47:34 +00005502 canvas_image=DestroyImage(canvas_image);
5503 return((Image *) NULL);
5504 }
5505 canvas_image->matte=MagickTrue;
cristy98621462011-12-31 22:31:11 +00005506 oval_image=CloneImage(canvas_image,canvas_image->columns,canvas_image->rows,
5507 MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005508 if (oval_image == (Image *) NULL)
5509 {
5510 canvas_image=DestroyImage(canvas_image);
5511 return((Image *) NULL);
5512 }
cristy9950d572011-10-01 18:22:35 +00005513 (void) QueryColorCompliance("#000000",AllCompliance,
5514 &oval_image->background_color,exception);
cristyea1a8aa2011-10-20 13:24:06 +00005515 (void) SetImageBackgroundColor(oval_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00005516 draw_info=CloneDrawInfo((const ImageInfo *) NULL,(const DrawInfo *) NULL);
cristy9950d572011-10-01 18:22:35 +00005517 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->fill,
5518 exception);
5519 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->stroke,
5520 exception);
cristy1707c6c2012-01-18 23:30:54 +00005521 (void) FormatLocaleString(ellipse,MaxTextExtent,"ellipse %g,%g,%g,%g,"
5522 "0.0,360.0",image->columns/2.0,image->rows/2.0,image->columns/2.0-x,
5523 image->rows/2.0-y);
cristy3ed852e2009-09-05 21:47:34 +00005524 draw_info->primitive=AcquireString(ellipse);
cristy018f07f2011-09-04 21:15:19 +00005525 (void) DrawImage(oval_image,draw_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00005526 draw_info=DestroyDrawInfo(draw_info);
cristyaa2c16c2012-03-25 22:21:35 +00005527 blur_image=BlurImage(oval_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +00005528 oval_image=DestroyImage(oval_image);
5529 if (blur_image == (Image *) NULL)
5530 {
5531 canvas_image=DestroyImage(canvas_image);
5532 return((Image *) NULL);
5533 }
5534 blur_image->matte=MagickFalse;
cristy39172402012-03-30 13:04:39 +00005535 (void) CompositeImage(canvas_image,blur_image,IntensityCompositeOp,MagickTrue,
5536 0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00005537 blur_image=DestroyImage(blur_image);
5538 vignette_image=MergeImageLayers(canvas_image,FlattenLayer,exception);
5539 canvas_image=DestroyImage(canvas_image);
cristy66d26122012-06-23 21:56:40 +00005540 if (vignette_image != (Image *) NULL)
5541 (void) TransformImageColorspace(vignette_image,image->colorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00005542 return(vignette_image);
5543}
5544
5545/*
5546%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5547% %
5548% %
5549% %
5550% W a v e I m a g e %
5551% %
5552% %
5553% %
5554%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5555%
5556% WaveImage() creates a "ripple" effect in the image by shifting the pixels
cristycee97112010-05-28 00:44:52 +00005557% vertically along a sine wave whose amplitude and wavelength is specified
cristy3ed852e2009-09-05 21:47:34 +00005558% by the given parameters.
5559%
5560% The format of the WaveImage method is:
5561%
5562% Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005563% const double wave_length,const PixelInterpolateMethod method,
5564% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005565%
5566% A description of each parameter follows:
5567%
5568% o image: the image.
5569%
5570% o amplitude, wave_length: Define the amplitude and wave length of the
5571% sine wave.
5572%
cristy5c4e2582011-09-11 19:21:03 +00005573% o interpolate: the pixel interpolation method.
5574%
cristy3ed852e2009-09-05 21:47:34 +00005575% o exception: return any errors or warnings in this structure.
5576%
5577*/
5578MagickExport Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005579 const double wave_length,const PixelInterpolateMethod method,
5580 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005581{
5582#define WaveImageTag "Wave/Image"
5583
cristyfa112112010-01-04 17:48:07 +00005584 CacheView
cristyd76c51e2011-03-26 00:21:26 +00005585 *image_view,
cristyfa112112010-01-04 17:48:07 +00005586 *wave_view;
5587
cristy3ed852e2009-09-05 21:47:34 +00005588 Image
5589 *wave_image;
5590
cristy3ed852e2009-09-05 21:47:34 +00005591 MagickBooleanType
5592 status;
5593
cristybb503372010-05-27 20:51:26 +00005594 MagickOffsetType
5595 progress;
5596
cristy3ed852e2009-09-05 21:47:34 +00005597 MagickRealType
5598 *sine_map;
5599
cristybb503372010-05-27 20:51:26 +00005600 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005601 i;
5602
cristybb503372010-05-27 20:51:26 +00005603 ssize_t
5604 y;
5605
cristy3ed852e2009-09-05 21:47:34 +00005606 /*
5607 Initialize wave image attributes.
5608 */
5609 assert(image != (Image *) NULL);
5610 assert(image->signature == MagickSignature);
5611 if (image->debug != MagickFalse)
5612 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5613 assert(exception != (ExceptionInfo *) NULL);
5614 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00005615 wave_image=CloneImage(image,image->columns,(size_t) (image->rows+2.0*
cristy3ed852e2009-09-05 21:47:34 +00005616 fabs(amplitude)),MagickTrue,exception);
5617 if (wave_image == (Image *) NULL)
5618 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005619 if (SetImageStorageClass(wave_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005620 {
cristy3ed852e2009-09-05 21:47:34 +00005621 wave_image=DestroyImage(wave_image);
5622 return((Image *) NULL);
5623 }
cristy4c08aed2011-07-01 19:47:50 +00005624 if (wave_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00005625 wave_image->matte=MagickTrue;
5626 /*
5627 Allocate sine map.
5628 */
5629 sine_map=(MagickRealType *) AcquireQuantumMemory((size_t) wave_image->columns,
5630 sizeof(*sine_map));
5631 if (sine_map == (MagickRealType *) NULL)
5632 {
5633 wave_image=DestroyImage(wave_image);
5634 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
5635 }
cristybb503372010-05-27 20:51:26 +00005636 for (i=0; i < (ssize_t) wave_image->columns; i++)
cristy4205a3c2010-09-12 20:19:59 +00005637 sine_map[i]=fabs(amplitude)+amplitude*sin((double) ((2.0*MagickPI*i)/
5638 wave_length));
cristy3ed852e2009-09-05 21:47:34 +00005639 /*
5640 Wave image.
5641 */
5642 status=MagickTrue;
5643 progress=0;
cristydb070952012-04-20 14:33:00 +00005644 image_view=AcquireVirtualCacheView(image,exception);
5645 wave_view=AcquireAuthenticCacheView(wave_image,exception);
cristyd76c51e2011-03-26 00:21:26 +00005646 (void) SetCacheViewVirtualPixelMethod(image_view,
5647 BackgroundVirtualPixelMethod);
cristyb5d5f722009-11-04 03:03:49 +00005648#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00005649 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00005650 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00005651#endif
cristybb503372010-05-27 20:51:26 +00005652 for (y=0; y < (ssize_t) wave_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005653 {
cristy4c08aed2011-07-01 19:47:50 +00005654 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005655 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005656
cristye97bb922011-04-03 01:36:52 +00005657 register ssize_t
5658 x;
5659
cristy3ed852e2009-09-05 21:47:34 +00005660 if (status == MagickFalse)
5661 continue;
5662 q=QueueCacheViewAuthenticPixels(wave_view,0,y,wave_image->columns,1,
5663 exception);
cristyacd2ed22011-08-30 01:44:23 +00005664 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005665 {
5666 status=MagickFalse;
5667 continue;
5668 }
cristybb503372010-05-27 20:51:26 +00005669 for (x=0; x < (ssize_t) wave_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005670 {
cristy5c4e2582011-09-11 19:21:03 +00005671 status=InterpolatePixelChannels(image,image_view,wave_image,method,
5672 (double) x,(double) (y-sine_map[x]),q,exception);
cristyed231572011-07-14 02:18:59 +00005673 q+=GetPixelChannels(wave_image);
cristy3ed852e2009-09-05 21:47:34 +00005674 }
5675 if (SyncCacheViewAuthenticPixels(wave_view,exception) == MagickFalse)
5676 status=MagickFalse;
5677 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5678 {
5679 MagickBooleanType
5680 proceed;
5681
cristyb5d5f722009-11-04 03:03:49 +00005682#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005683 #pragma omp critical (MagickCore_WaveImage)
cristy3ed852e2009-09-05 21:47:34 +00005684#endif
5685 proceed=SetImageProgress(image,WaveImageTag,progress++,image->rows);
5686 if (proceed == MagickFalse)
5687 status=MagickFalse;
5688 }
5689 }
5690 wave_view=DestroyCacheView(wave_view);
cristyd76c51e2011-03-26 00:21:26 +00005691 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00005692 sine_map=(MagickRealType *) RelinquishMagickMemory(sine_map);
5693 if (status == MagickFalse)
5694 wave_image=DestroyImage(wave_image);
5695 return(wave_image);
5696}