blob: c74623f53bf84049981dcc110ee2346f12102083 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% FFFFF X X %
7% F X X %
8% FFF X %
9% F X X %
10% F X X %
11% %
12% %
13% MagickCore Image Special Effects Methods %
14% %
15% Software Design %
16% John Cristy %
17% October 1996 %
18% %
19% %
cristy1454be72011-12-19 01:52:48 +000020% Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37%
38*/
39
40/*
41 Include declarations.
42*/
cristy4c08aed2011-07-01 19:47:50 +000043#include "MagickCore/studio.h"
44#include "MagickCore/annotate.h"
45#include "MagickCore/artifact.h"
46#include "MagickCore/attribute.h"
47#include "MagickCore/cache.h"
48#include "MagickCore/cache-view.h"
49#include "MagickCore/color.h"
50#include "MagickCore/color-private.h"
cristy9b7a4fc2012-04-08 22:26:56 +000051#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000052#include "MagickCore/composite.h"
53#include "MagickCore/decorate.h"
cristyc53413d2011-11-17 13:04:26 +000054#include "MagickCore/distort.h"
cristy4c08aed2011-07-01 19:47:50 +000055#include "MagickCore/draw.h"
56#include "MagickCore/effect.h"
57#include "MagickCore/enhance.h"
58#include "MagickCore/exception.h"
59#include "MagickCore/exception-private.h"
60#include "MagickCore/fx.h"
61#include "MagickCore/fx-private.h"
62#include "MagickCore/gem.h"
cristy8ea81222011-09-04 10:33:32 +000063#include "MagickCore/gem-private.h"
cristy4c08aed2011-07-01 19:47:50 +000064#include "MagickCore/geometry.h"
65#include "MagickCore/layer.h"
66#include "MagickCore/list.h"
67#include "MagickCore/log.h"
68#include "MagickCore/image.h"
69#include "MagickCore/image-private.h"
70#include "MagickCore/magick.h"
71#include "MagickCore/memory_.h"
72#include "MagickCore/monitor.h"
73#include "MagickCore/monitor-private.h"
74#include "MagickCore/option.h"
75#include "MagickCore/pixel.h"
76#include "MagickCore/pixel-accessor.h"
77#include "MagickCore/property.h"
78#include "MagickCore/quantum.h"
79#include "MagickCore/quantum-private.h"
80#include "MagickCore/random_.h"
81#include "MagickCore/random-private.h"
82#include "MagickCore/resample.h"
83#include "MagickCore/resample-private.h"
84#include "MagickCore/resize.h"
cristy57340e02012-05-05 00:53:23 +000085#include "MagickCore/resource_.h"
cristy4c08aed2011-07-01 19:47:50 +000086#include "MagickCore/splay-tree.h"
87#include "MagickCore/statistic.h"
88#include "MagickCore/string_.h"
89#include "MagickCore/string-private.h"
90#include "MagickCore/thread-private.h"
91#include "MagickCore/transform.h"
92#include "MagickCore/utility.h"
cristy3ed852e2009-09-05 21:47:34 +000093
94/*
95 Define declarations.
96*/
97#define LeftShiftOperator 0xf5
98#define RightShiftOperator 0xf6
99#define LessThanEqualOperator 0xf7
100#define GreaterThanEqualOperator 0xf8
101#define EqualOperator 0xf9
102#define NotEqualOperator 0xfa
103#define LogicalAndOperator 0xfb
104#define LogicalOrOperator 0xfc
cristy116af162010-08-13 01:25:47 +0000105#define ExponentialNotation 0xfd
cristy3ed852e2009-09-05 21:47:34 +0000106
107struct _FxInfo
108{
109 const Image
110 *images;
111
cristy3ed852e2009-09-05 21:47:34 +0000112 char
113 *expression;
114
115 FILE
116 *file;
117
118 SplayTreeInfo
119 *colors,
120 *symbols;
121
cristyd76c51e2011-03-26 00:21:26 +0000122 CacheView
123 **view;
cristy3ed852e2009-09-05 21:47:34 +0000124
125 RandomInfo
126 *random_info;
127
128 ExceptionInfo
129 *exception;
130};
131
132/*
133%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
134% %
135% %
136% %
137+ A c q u i r e F x I n f o %
138% %
139% %
140% %
141%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
142%
143% AcquireFxInfo() allocates the FxInfo structure.
144%
145% The format of the AcquireFxInfo method is:
146%
cristydb070952012-04-20 14:33:00 +0000147% FxInfo *AcquireFxInfo(Image *image,const char *expression,
148% ExceptionInfo *exception)
cristy0a9b3722010-10-23 18:45:49 +0000149%
cristy3ed852e2009-09-05 21:47:34 +0000150% A description of each parameter follows:
151%
152% o image: the image.
153%
154% o expression: the expression.
155%
cristydb070952012-04-20 14:33:00 +0000156% o exception: return any errors or warnings in this structure.
157%
cristy3ed852e2009-09-05 21:47:34 +0000158*/
cristydb070952012-04-20 14:33:00 +0000159MagickPrivate FxInfo *AcquireFxInfo(const Image *image,const char *expression,
160 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000161{
162 char
163 fx_op[2];
164
cristycb180922011-03-11 14:41:24 +0000165 const Image
166 *next;
167
cristy3ed852e2009-09-05 21:47:34 +0000168 FxInfo
169 *fx_info;
170
cristybb503372010-05-27 20:51:26 +0000171 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000172 i;
173
cristy73bd4a52010-10-05 11:24:23 +0000174 fx_info=(FxInfo *) AcquireMagickMemory(sizeof(*fx_info));
cristy3ed852e2009-09-05 21:47:34 +0000175 if (fx_info == (FxInfo *) NULL)
176 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
177 (void) ResetMagickMemory(fx_info,0,sizeof(*fx_info));
178 fx_info->exception=AcquireExceptionInfo();
anthony7d86e172011-03-23 12:37:06 +0000179 fx_info->images=image;
cristy3ed852e2009-09-05 21:47:34 +0000180 fx_info->colors=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
181 RelinquishMagickMemory);
182 fx_info->symbols=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
183 RelinquishMagickMemory);
cristyd76c51e2011-03-26 00:21:26 +0000184 fx_info->view=(CacheView **) AcquireQuantumMemory(GetImageListLength(
185 fx_info->images),sizeof(*fx_info->view));
186 if (fx_info->view == (CacheView **) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000187 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
cristya2262262011-03-11 02:50:37 +0000188 i=0;
cristy0ea377f2011-03-24 00:54:19 +0000189 next=GetFirstImageInList(fx_info->images);
190 for ( ; next != (Image *) NULL; next=next->next)
cristy3ed852e2009-09-05 21:47:34 +0000191 {
cristydb070952012-04-20 14:33:00 +0000192 fx_info->view[i]=AcquireVirtualCacheView(next,exception);
cristya2262262011-03-11 02:50:37 +0000193 i++;
cristy3ed852e2009-09-05 21:47:34 +0000194 }
195 fx_info->random_info=AcquireRandomInfo();
196 fx_info->expression=ConstantString(expression);
197 fx_info->file=stderr;
198 (void) SubstituteString(&fx_info->expression," ",""); /* compact string */
cristy37af0912011-05-23 16:09:42 +0000199 /*
200 Force right-to-left associativity for unary negation.
201 */
202 (void) SubstituteString(&fx_info->expression,"-","-1.0*");
cristy8b8a3ae2010-10-23 18:49:46 +0000203 /*
cristy3ed852e2009-09-05 21:47:34 +0000204 Convert complex to simple operators.
205 */
206 fx_op[1]='\0';
207 *fx_op=(char) LeftShiftOperator;
208 (void) SubstituteString(&fx_info->expression,"<<",fx_op);
209 *fx_op=(char) RightShiftOperator;
210 (void) SubstituteString(&fx_info->expression,">>",fx_op);
211 *fx_op=(char) LessThanEqualOperator;
212 (void) SubstituteString(&fx_info->expression,"<=",fx_op);
213 *fx_op=(char) GreaterThanEqualOperator;
214 (void) SubstituteString(&fx_info->expression,">=",fx_op);
215 *fx_op=(char) EqualOperator;
216 (void) SubstituteString(&fx_info->expression,"==",fx_op);
217 *fx_op=(char) NotEqualOperator;
218 (void) SubstituteString(&fx_info->expression,"!=",fx_op);
219 *fx_op=(char) LogicalAndOperator;
220 (void) SubstituteString(&fx_info->expression,"&&",fx_op);
221 *fx_op=(char) LogicalOrOperator;
222 (void) SubstituteString(&fx_info->expression,"||",fx_op);
cristy116af162010-08-13 01:25:47 +0000223 *fx_op=(char) ExponentialNotation;
224 (void) SubstituteString(&fx_info->expression,"**",fx_op);
cristy3ed852e2009-09-05 21:47:34 +0000225 return(fx_info);
226}
227
228/*
229%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
230% %
231% %
232% %
233% A d d N o i s e I m a g e %
234% %
235% %
236% %
237%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
238%
239% AddNoiseImage() adds random noise to the image.
240%
241% The format of the AddNoiseImage method is:
242%
243% Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
cristy9ed1f812011-10-08 02:00:08 +0000244% const double attenuate,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000245%
246% A description of each parameter follows:
247%
248% o image: the image.
249%
250% o channel: the channel type.
251%
252% o noise_type: The type of noise: Uniform, Gaussian, Multiplicative,
253% Impulse, Laplacian, or Poisson.
254%
cristy9ed1f812011-10-08 02:00:08 +0000255% o attenuate: attenuate the random distribution.
256%
cristy3ed852e2009-09-05 21:47:34 +0000257% o exception: return any errors or warnings in this structure.
258%
259*/
cristy9ed1f812011-10-08 02:00:08 +0000260MagickExport Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
261 const double attenuate,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000262{
263#define AddNoiseImageTag "AddNoise/Image"
264
cristyfa112112010-01-04 17:48:07 +0000265 CacheView
266 *image_view,
267 *noise_view;
268
cristy3ed852e2009-09-05 21:47:34 +0000269 Image
270 *noise_image;
271
cristy3ed852e2009-09-05 21:47:34 +0000272 MagickBooleanType
273 status;
274
cristybb503372010-05-27 20:51:26 +0000275 MagickOffsetType
276 progress;
277
cristy3ed852e2009-09-05 21:47:34 +0000278 RandomInfo
cristyfa112112010-01-04 17:48:07 +0000279 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +0000280
cristybb503372010-05-27 20:51:26 +0000281 ssize_t
282 y;
283
cristy57340e02012-05-05 00:53:23 +0000284 unsigned long
285 key;
286
cristy3ed852e2009-09-05 21:47:34 +0000287 /*
288 Initialize noise image attributes.
289 */
290 assert(image != (const Image *) NULL);
291 assert(image->signature == MagickSignature);
292 if (image->debug != MagickFalse)
293 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
294 assert(exception != (ExceptionInfo *) NULL);
295 assert(exception->signature == MagickSignature);
cristyb2145892011-10-10 00:55:32 +0000296 noise_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000297 if (noise_image == (Image *) NULL)
298 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000299 if (SetImageStorageClass(noise_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000300 {
cristy3ed852e2009-09-05 21:47:34 +0000301 noise_image=DestroyImage(noise_image);
302 return((Image *) NULL);
303 }
cristy98ccd782012-07-09 11:22:59 +0000304 if (IsGrayColorspace(image->colorspace) != MagickFalse)
cristyb09db112012-07-11 12:04:31 +0000305 (void) TransformImageColorspace(noise_image,RGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000306 /*
307 Add noise in each row.
308 */
cristy3ed852e2009-09-05 21:47:34 +0000309 status=MagickTrue;
310 progress=0;
311 random_info=AcquireRandomInfoThreadSet();
cristydb070952012-04-20 14:33:00 +0000312 image_view=AcquireVirtualCacheView(image,exception);
313 noise_view=AcquireAuthenticCacheView(noise_image,exception);
cristy57340e02012-05-05 00:53:23 +0000314 key=GetRandomSecretKey(random_info[0]);
cristy319a1e72010-02-21 15:13:11 +0000315#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy57340e02012-05-05 00:53:23 +0000316 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +0000317 dynamic_number_threads(image,image->columns,image->rows,key == ~0UL)
cristy3ed852e2009-09-05 21:47:34 +0000318#endif
cristybb503372010-05-27 20:51:26 +0000319 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000320 {
cristy5c9e6f22010-09-17 17:31:01 +0000321 const int
322 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +0000323
cristy3ed852e2009-09-05 21:47:34 +0000324 MagickBooleanType
325 sync;
326
cristy4c08aed2011-07-01 19:47:50 +0000327 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000328 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000329
cristybb503372010-05-27 20:51:26 +0000330 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000331 x;
332
cristy4c08aed2011-07-01 19:47:50 +0000333 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000334 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000335
336 if (status == MagickFalse)
337 continue;
338 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +0000339 q=QueueCacheViewAuthenticPixels(noise_view,0,y,noise_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +0000340 exception);
cristy4c08aed2011-07-01 19:47:50 +0000341 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000342 {
343 status=MagickFalse;
344 continue;
345 }
cristybb503372010-05-27 20:51:26 +0000346 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000347 {
cristy850b3072011-10-08 01:38:05 +0000348 register ssize_t
349 i;
350
cristy177e41c2012-04-15 15:08:25 +0000351 if (GetPixelMask(image,p) != 0)
352 {
353 p+=GetPixelChannels(image);
354 q+=GetPixelChannels(noise_image);
355 continue;
356 }
cristy850b3072011-10-08 01:38:05 +0000357 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
358 {
359 PixelChannel
360 channel;
361
362 PixelTrait
363 noise_traits,
364 traits;
365
cristye2a912b2011-12-05 20:02:07 +0000366 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +0000367 traits=GetPixelChannelMapTraits(image,channel);
cristy850b3072011-10-08 01:38:05 +0000368 noise_traits=GetPixelChannelMapTraits(noise_image,channel);
369 if ((traits == UndefinedPixelTrait) ||
370 (noise_traits == UndefinedPixelTrait))
371 continue;
cristy177e41c2012-04-15 15:08:25 +0000372 if ((noise_traits & CopyPixelTrait) != 0)
cristyb2145892011-10-10 00:55:32 +0000373 {
374 SetPixelChannel(noise_image,channel,p[i],q);
375 continue;
376 }
cristy850b3072011-10-08 01:38:05 +0000377 SetPixelChannel(noise_image,channel,ClampToQuantum(
378 GenerateDifferentialNoise(random_info[id],p[i],noise_type,attenuate)),
379 q);
380 }
cristyed231572011-07-14 02:18:59 +0000381 p+=GetPixelChannels(image);
382 q+=GetPixelChannels(noise_image);
cristy3ed852e2009-09-05 21:47:34 +0000383 }
384 sync=SyncCacheViewAuthenticPixels(noise_view,exception);
385 if (sync == MagickFalse)
386 status=MagickFalse;
387 if (image->progress_monitor != (MagickProgressMonitor) NULL)
388 {
389 MagickBooleanType
390 proceed;
391
cristyb5d5f722009-11-04 03:03:49 +0000392#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy319a1e72010-02-21 15:13:11 +0000393 #pragma omp critical (MagickCore_AddNoiseImage)
cristy3ed852e2009-09-05 21:47:34 +0000394#endif
395 proceed=SetImageProgress(image,AddNoiseImageTag,progress++,
396 image->rows);
397 if (proceed == MagickFalse)
398 status=MagickFalse;
399 }
400 }
401 noise_view=DestroyCacheView(noise_view);
402 image_view=DestroyCacheView(image_view);
403 random_info=DestroyRandomInfoThreadSet(random_info);
404 if (status == MagickFalse)
405 noise_image=DestroyImage(noise_image);
406 return(noise_image);
407}
408
409/*
410%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
411% %
412% %
413% %
414% B l u e S h i f t I m a g e %
415% %
416% %
417% %
418%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
419%
420% BlueShiftImage() mutes the colors of the image to simulate a scene at
421% nighttime in the moonlight.
422%
423% The format of the BlueShiftImage method is:
424%
425% Image *BlueShiftImage(const Image *image,const double factor,
426% ExceptionInfo *exception)
427%
428% A description of each parameter follows:
429%
430% o image: the image.
431%
432% o factor: the shift factor.
433%
434% o exception: return any errors or warnings in this structure.
435%
436*/
437MagickExport Image *BlueShiftImage(const Image *image,const double factor,
438 ExceptionInfo *exception)
439{
440#define BlueShiftImageTag "BlueShift/Image"
441
cristyc4c8d132010-01-07 01:58:38 +0000442 CacheView
443 *image_view,
444 *shift_view;
445
cristy3ed852e2009-09-05 21:47:34 +0000446 Image
447 *shift_image;
448
cristy3ed852e2009-09-05 21:47:34 +0000449 MagickBooleanType
450 status;
451
cristybb503372010-05-27 20:51:26 +0000452 MagickOffsetType
453 progress;
454
455 ssize_t
456 y;
457
cristy3ed852e2009-09-05 21:47:34 +0000458 /*
459 Allocate blue shift image.
460 */
461 assert(image != (const Image *) NULL);
462 assert(image->signature == MagickSignature);
463 if (image->debug != MagickFalse)
464 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
465 assert(exception != (ExceptionInfo *) NULL);
466 assert(exception->signature == MagickSignature);
cristya6d7a9b2012-01-18 20:04:48 +0000467 shift_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000468 if (shift_image == (Image *) NULL)
469 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000470 if (SetImageStorageClass(shift_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000471 {
cristy3ed852e2009-09-05 21:47:34 +0000472 shift_image=DestroyImage(shift_image);
473 return((Image *) NULL);
474 }
475 /*
476 Blue-shift DirectClass image.
477 */
478 status=MagickTrue;
479 progress=0;
cristydb070952012-04-20 14:33:00 +0000480 image_view=AcquireVirtualCacheView(image,exception);
481 shift_view=AcquireAuthenticCacheView(shift_image,exception);
cristy319a1e72010-02-21 15:13:11 +0000482#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000483 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +0000484 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +0000485#endif
cristybb503372010-05-27 20:51:26 +0000486 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000487 {
488 MagickBooleanType
489 sync;
490
cristy4c08aed2011-07-01 19:47:50 +0000491 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000492 pixel;
493
494 Quantum
495 quantum;
496
cristy4c08aed2011-07-01 19:47:50 +0000497 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000498 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000499
cristybb503372010-05-27 20:51:26 +0000500 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000501 x;
502
cristy4c08aed2011-07-01 19:47:50 +0000503 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000504 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000505
506 if (status == MagickFalse)
507 continue;
508 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
509 q=QueueCacheViewAuthenticPixels(shift_view,0,y,shift_image->columns,1,
510 exception);
cristy4c08aed2011-07-01 19:47:50 +0000511 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000512 {
513 status=MagickFalse;
514 continue;
515 }
cristybb503372010-05-27 20:51:26 +0000516 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000517 {
cristy4c08aed2011-07-01 19:47:50 +0000518 quantum=GetPixelRed(image,p);
519 if (GetPixelGreen(image,p) < quantum)
520 quantum=GetPixelGreen(image,p);
521 if (GetPixelBlue(image,p) < quantum)
522 quantum=GetPixelBlue(image,p);
523 pixel.red=0.5*(GetPixelRed(image,p)+factor*quantum);
524 pixel.green=0.5*(GetPixelGreen(image,p)+factor*quantum);
525 pixel.blue=0.5*(GetPixelBlue(image,p)+factor*quantum);
526 quantum=GetPixelRed(image,p);
527 if (GetPixelGreen(image,p) > quantum)
528 quantum=GetPixelGreen(image,p);
529 if (GetPixelBlue(image,p) > quantum)
530 quantum=GetPixelBlue(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000531 pixel.red=0.5*(pixel.red+factor*quantum);
532 pixel.green=0.5*(pixel.green+factor*quantum);
533 pixel.blue=0.5*(pixel.blue+factor*quantum);
cristy4c08aed2011-07-01 19:47:50 +0000534 SetPixelRed(shift_image,ClampToQuantum(pixel.red),q);
535 SetPixelGreen(shift_image,ClampToQuantum(pixel.green),q);
536 SetPixelBlue(shift_image,ClampToQuantum(pixel.blue),q);
cristyed231572011-07-14 02:18:59 +0000537 p+=GetPixelChannels(image);
538 q+=GetPixelChannels(shift_image);
cristy3ed852e2009-09-05 21:47:34 +0000539 }
540 sync=SyncCacheViewAuthenticPixels(shift_view,exception);
541 if (sync == MagickFalse)
542 status=MagickFalse;
543 if (image->progress_monitor != (MagickProgressMonitor) NULL)
544 {
545 MagickBooleanType
546 proceed;
547
cristy319a1e72010-02-21 15:13:11 +0000548#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +0000549 #pragma omp critical (MagickCore_BlueShiftImage)
cristy3ed852e2009-09-05 21:47:34 +0000550#endif
551 proceed=SetImageProgress(image,BlueShiftImageTag,progress++,
552 image->rows);
553 if (proceed == MagickFalse)
554 status=MagickFalse;
555 }
556 }
557 image_view=DestroyCacheView(image_view);
558 shift_view=DestroyCacheView(shift_view);
559 if (status == MagickFalse)
560 shift_image=DestroyImage(shift_image);
561 return(shift_image);
562}
563
564/*
565%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
566% %
567% %
568% %
569% C h a r c o a l I m a g e %
570% %
571% %
572% %
573%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
574%
575% CharcoalImage() creates a new image that is a copy of an existing one with
576% the edge highlighted. It allocates the memory necessary for the new Image
577% structure and returns a pointer to the new image.
578%
579% The format of the CharcoalImage method is:
580%
581% Image *CharcoalImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +0000582% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000583%
584% A description of each parameter follows:
585%
586% o image: the image.
587%
588% o radius: the radius of the pixel neighborhood.
589%
590% o sigma: the standard deviation of the Gaussian, in pixels.
591%
592% o exception: return any errors or warnings in this structure.
593%
594*/
595MagickExport Image *CharcoalImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +0000596 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000597{
598 Image
599 *charcoal_image,
600 *clone_image,
601 *edge_image;
602
603 assert(image != (Image *) NULL);
604 assert(image->signature == MagickSignature);
605 if (image->debug != MagickFalse)
606 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
607 assert(exception != (ExceptionInfo *) NULL);
608 assert(exception->signature == MagickSignature);
609 clone_image=CloneImage(image,0,0,MagickTrue,exception);
610 if (clone_image == (Image *) NULL)
611 return((Image *) NULL);
cristy018f07f2011-09-04 21:15:19 +0000612 (void) SetImageType(clone_image,GrayscaleType,exception);
cristy8ae632d2011-09-05 17:29:53 +0000613 edge_image=EdgeImage(clone_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +0000614 clone_image=DestroyImage(clone_image);
615 if (edge_image == (Image *) NULL)
616 return((Image *) NULL);
cristyaa2c16c2012-03-25 22:21:35 +0000617 charcoal_image=BlurImage(edge_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +0000618 edge_image=DestroyImage(edge_image);
619 if (charcoal_image == (Image *) NULL)
620 return((Image *) NULL);
cristye23ec9d2011-08-16 18:15:40 +0000621 (void) NormalizeImage(charcoal_image,exception);
cristyb3e7c6c2011-07-24 01:43:55 +0000622 (void) NegateImage(charcoal_image,MagickFalse,exception);
cristy018f07f2011-09-04 21:15:19 +0000623 (void) SetImageType(charcoal_image,GrayscaleType,exception);
cristy3ed852e2009-09-05 21:47:34 +0000624 return(charcoal_image);
625}
626
627/*
628%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
629% %
630% %
631% %
632% C o l o r i z e I m a g e %
633% %
634% %
635% %
636%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
637%
638% ColorizeImage() blends the fill color with each pixel in the image.
639% A percentage blend is specified with opacity. Control the application
640% of different color components by specifying a different percentage for
641% each component (e.g. 90/100/10 is 90% red, 100% green, and 10% blue).
642%
643% The format of the ColorizeImage method is:
644%
cristyc7e6ff62011-10-03 13:46:11 +0000645% Image *ColorizeImage(const Image *image,const char *blend,
646% const PixelInfo *colorize,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000647%
648% A description of each parameter follows:
649%
650% o image: the image.
651%
cristyc7e6ff62011-10-03 13:46:11 +0000652% o blend: A character string indicating the level of blending as a
cristy3ed852e2009-09-05 21:47:34 +0000653% percentage.
654%
655% o colorize: A color value.
656%
657% o exception: return any errors or warnings in this structure.
658%
659*/
cristyc7e6ff62011-10-03 13:46:11 +0000660MagickExport Image *ColorizeImage(const Image *image,const char *blend,
661 const PixelInfo *colorize,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000662{
663#define ColorizeImageTag "Colorize/Image"
cristy20c3aed2012-04-10 01:06:21 +0000664#define Colorize(pixel,blend_percentage,colorize) \
665 (pixel)=((pixel)*(100.0-(blend_percentage))+(colorize)*(blend_percentage))/100.0;
cristy3ed852e2009-09-05 21:47:34 +0000666
cristyc4c8d132010-01-07 01:58:38 +0000667 CacheView
668 *colorize_view,
669 *image_view;
670
cristy3ed852e2009-09-05 21:47:34 +0000671 GeometryInfo
672 geometry_info;
673
674 Image
675 *colorize_image;
676
cristy3ed852e2009-09-05 21:47:34 +0000677 MagickBooleanType
678 status;
679
cristybb503372010-05-27 20:51:26 +0000680 MagickOffsetType
681 progress;
682
cristy3ed852e2009-09-05 21:47:34 +0000683 MagickStatusType
684 flags;
685
cristyc7e6ff62011-10-03 13:46:11 +0000686 PixelInfo
cristy20c3aed2012-04-10 01:06:21 +0000687 blend_percentage;
cristyc7e6ff62011-10-03 13:46:11 +0000688
cristybb503372010-05-27 20:51:26 +0000689 ssize_t
690 y;
691
cristy3ed852e2009-09-05 21:47:34 +0000692 /*
693 Allocate colorized image.
694 */
695 assert(image != (const Image *) NULL);
696 assert(image->signature == MagickSignature);
697 if (image->debug != MagickFalse)
698 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
699 assert(exception != (ExceptionInfo *) NULL);
700 assert(exception->signature == MagickSignature);
cristy768165d2012-04-09 15:01:35 +0000701 colorize_image=CloneImage(image,image->columns,image->rows,MagickTrue,
702 exception);
703 if (colorize_image == (Image *) NULL)
704 return((Image *) NULL);
705 if (SetImageStorageClass(colorize_image,DirectClass,exception) == MagickFalse)
706 {
707 colorize_image=DestroyImage(colorize_image);
708 return((Image *) NULL);
709 }
cristy9b7a4fc2012-04-08 22:26:56 +0000710 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
cristy20c3aed2012-04-10 01:06:21 +0000711 (IsPixelInfoGray(colorize) != MagickFalse))
cristyb09db112012-07-11 12:04:31 +0000712 (void) SetImageColorspace(colorize_image,RGBColorspace,exception);
cristy20c3aed2012-04-10 01:06:21 +0000713 if ((colorize_image->matte == MagickFalse) &&
714 (colorize->matte != MagickFalse))
cristy768165d2012-04-09 15:01:35 +0000715 (void) SetImageAlpha(colorize_image,OpaqueAlpha,exception);
716 if (blend == (const char *) NULL)
717 return(colorize_image);
cristy20c3aed2012-04-10 01:06:21 +0000718 GetPixelInfo(image,&blend_percentage);
719 flags=ParseGeometry(blend,&geometry_info);
720 blend_percentage.red=geometry_info.rho;
721 blend_percentage.green=geometry_info.rho;
722 blend_percentage.blue=geometry_info.rho;
723 blend_percentage.black=geometry_info.rho;
cristy3ee7de52012-04-14 23:40:47 +0000724 blend_percentage.alpha=geometry_info.rho;
cristy20c3aed2012-04-10 01:06:21 +0000725 if ((flags & SigmaValue) != 0)
726 blend_percentage.green=geometry_info.sigma;
727 if ((flags & XiValue) != 0)
728 blend_percentage.blue=geometry_info.xi;
729 if ((flags & PsiValue) != 0)
730 blend_percentage.alpha=geometry_info.psi;
731 if (blend_percentage.colorspace == CMYKColorspace)
732 {
733 if ((flags & PsiValue) != 0)
734 blend_percentage.black=geometry_info.psi;
735 if ((flags & ChiValue) != 0)
736 blend_percentage.alpha=geometry_info.chi;
737 }
cristy3ed852e2009-09-05 21:47:34 +0000738 /*
739 Colorize DirectClass image.
740 */
741 status=MagickTrue;
742 progress=0;
cristydb070952012-04-20 14:33:00 +0000743 image_view=AcquireVirtualCacheView(image,exception);
744 colorize_view=AcquireAuthenticCacheView(colorize_image,exception);
cristy319a1e72010-02-21 15:13:11 +0000745#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000746 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +0000747 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +0000748#endif
cristybb503372010-05-27 20:51:26 +0000749 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000750 {
751 MagickBooleanType
752 sync;
753
cristyf9bf43e2012-04-07 18:13:07 +0000754 PixelInfo
755 pixel;
756
cristy4c08aed2011-07-01 19:47:50 +0000757 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000758 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000759
cristybb503372010-05-27 20:51:26 +0000760 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000761 x;
762
cristy4c08aed2011-07-01 19:47:50 +0000763 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000764 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000765
766 if (status == MagickFalse)
767 continue;
768 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
769 q=QueueCacheViewAuthenticPixels(colorize_view,0,y,colorize_image->columns,1,
770 exception);
cristy4c08aed2011-07-01 19:47:50 +0000771 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000772 {
773 status=MagickFalse;
774 continue;
775 }
cristy66d3e1f2012-04-14 23:21:39 +0000776 GetPixelInfo(colorize_image,&pixel);
cristybb503372010-05-27 20:51:26 +0000777 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000778 {
cristyd6803382012-04-10 01:41:25 +0000779 if (GetPixelMask(colorize_image,q) != 0)
780 {
781 p+=GetPixelChannels(image);
782 q+=GetPixelChannels(colorize_image);
783 continue;
784 }
cristyf9bf43e2012-04-07 18:13:07 +0000785 GetPixelInfoPixel(image,p,&pixel);
cristy20c3aed2012-04-10 01:06:21 +0000786 Colorize(pixel.red,blend_percentage.red,colorize->red);
787 Colorize(pixel.green,blend_percentage.green,colorize->green);
788 Colorize(pixel.blue,blend_percentage.blue,colorize->blue);
789 Colorize(pixel.black,blend_percentage.black,colorize->black);
790 Colorize(pixel.alpha,blend_percentage.alpha,colorize->alpha);
cristyf9bf43e2012-04-07 18:13:07 +0000791 SetPixelInfoPixel(colorize_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +0000792 p+=GetPixelChannels(image);
793 q+=GetPixelChannels(colorize_image);
cristy3ed852e2009-09-05 21:47:34 +0000794 }
795 sync=SyncCacheViewAuthenticPixels(colorize_view,exception);
796 if (sync == MagickFalse)
797 status=MagickFalse;
798 if (image->progress_monitor != (MagickProgressMonitor) NULL)
799 {
800 MagickBooleanType
801 proceed;
802
cristy319a1e72010-02-21 15:13:11 +0000803#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +0000804 #pragma omp critical (MagickCore_ColorizeImage)
cristy3ed852e2009-09-05 21:47:34 +0000805#endif
806 proceed=SetImageProgress(image,ColorizeImageTag,progress++,image->rows);
807 if (proceed == MagickFalse)
808 status=MagickFalse;
809 }
810 }
811 image_view=DestroyCacheView(image_view);
812 colorize_view=DestroyCacheView(colorize_view);
813 if (status == MagickFalse)
814 colorize_image=DestroyImage(colorize_image);
815 return(colorize_image);
816}
817
818/*
819%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
820% %
821% %
822% %
cristye6365592010-04-02 17:31:23 +0000823% C o l o r M a t r i x I m a g e %
824% %
825% %
826% %
827%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
828%
829% ColorMatrixImage() applies color transformation to an image. This method
830% permits saturation changes, hue rotation, luminance to alpha, and various
831% other effects. Although variable-sized transformation matrices can be used,
832% typically one uses a 5x5 matrix for an RGBA image and a 6x6 for CMYKA
833% (or RGBA with offsets). The matrix is similar to those used by Adobe Flash
834% except offsets are in column 6 rather than 5 (in support of CMYKA images)
835% and offsets are normalized (divide Flash offset by 255).
836%
837% The format of the ColorMatrixImage method is:
838%
839% Image *ColorMatrixImage(const Image *image,
840% const KernelInfo *color_matrix,ExceptionInfo *exception)
841%
842% A description of each parameter follows:
843%
844% o image: the image.
845%
846% o color_matrix: the color matrix.
847%
848% o exception: return any errors or warnings in this structure.
849%
850*/
anthonyfd706f92012-01-19 04:22:02 +0000851/* FUTURE: modify to make use of a MagickMatrix Mutliply function
852 That should be provided in "matrix.c"
853 (ASIDE: actually distorts should do this too but currently doesn't)
854*/
855
cristye6365592010-04-02 17:31:23 +0000856MagickExport Image *ColorMatrixImage(const Image *image,
857 const KernelInfo *color_matrix,ExceptionInfo *exception)
858{
859#define ColorMatrixImageTag "ColorMatrix/Image"
860
861 CacheView
862 *color_view,
863 *image_view;
864
865 double
866 ColorMatrix[6][6] =
867 {
868 { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
869 { 0.0, 1.0, 0.0, 0.0, 0.0, 0.0 },
870 { 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 },
871 { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0 },
872 { 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 },
873 { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 }
874 };
875
876 Image
877 *color_image;
878
cristye6365592010-04-02 17:31:23 +0000879 MagickBooleanType
880 status;
881
cristybb503372010-05-27 20:51:26 +0000882 MagickOffsetType
883 progress;
884
885 register ssize_t
cristye6365592010-04-02 17:31:23 +0000886 i;
887
cristybb503372010-05-27 20:51:26 +0000888 ssize_t
889 u,
890 v,
891 y;
892
cristye6365592010-04-02 17:31:23 +0000893 /*
anthonyfd706f92012-01-19 04:22:02 +0000894 Map given color_matrix, into a 6x6 matrix RGBKA and a constant
cristye6365592010-04-02 17:31:23 +0000895 */
896 assert(image != (Image *) NULL);
897 assert(image->signature == MagickSignature);
898 if (image->debug != MagickFalse)
899 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
900 assert(exception != (ExceptionInfo *) NULL);
901 assert(exception->signature == MagickSignature);
902 i=0;
cristybb503372010-05-27 20:51:26 +0000903 for (v=0; v < (ssize_t) color_matrix->height; v++)
904 for (u=0; u < (ssize_t) color_matrix->width; u++)
cristye6365592010-04-02 17:31:23 +0000905 {
906 if ((v < 6) && (u < 6))
907 ColorMatrix[v][u]=color_matrix->values[i];
908 i++;
909 }
910 /*
911 Initialize color image.
912 */
cristy12550e62010-06-07 12:46:40 +0000913 color_image=CloneImage(image,0,0,MagickTrue,exception);
cristye6365592010-04-02 17:31:23 +0000914 if (color_image == (Image *) NULL)
915 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000916 if (SetImageStorageClass(color_image,DirectClass,exception) == MagickFalse)
cristye6365592010-04-02 17:31:23 +0000917 {
cristye6365592010-04-02 17:31:23 +0000918 color_image=DestroyImage(color_image);
919 return((Image *) NULL);
920 }
921 if (image->debug != MagickFalse)
922 {
923 char
924 format[MaxTextExtent],
925 *message;
926
927 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
928 " ColorMatrix image with color matrix:");
929 message=AcquireString("");
930 for (v=0; v < 6; v++)
931 {
932 *message='\0';
cristyb51dff52011-05-19 16:55:47 +0000933 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
cristye6365592010-04-02 17:31:23 +0000934 (void) ConcatenateString(&message,format);
935 for (u=0; u < 6; u++)
936 {
cristyb51dff52011-05-19 16:55:47 +0000937 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",
cristye6365592010-04-02 17:31:23 +0000938 ColorMatrix[v][u]);
939 (void) ConcatenateString(&message,format);
940 }
941 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
942 }
943 message=DestroyString(message);
944 }
945 /*
anthonyfd706f92012-01-19 04:22:02 +0000946 Apply the ColorMatrix to image.
cristye6365592010-04-02 17:31:23 +0000947 */
948 status=MagickTrue;
949 progress=0;
cristydb070952012-04-20 14:33:00 +0000950 image_view=AcquireVirtualCacheView(image,exception);
951 color_view=AcquireAuthenticCacheView(color_image,exception);
cristye6365592010-04-02 17:31:23 +0000952#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000953 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +0000954 dynamic_number_threads(image,image->columns,image->rows,1)
cristye6365592010-04-02 17:31:23 +0000955#endif
cristybb503372010-05-27 20:51:26 +0000956 for (y=0; y < (ssize_t) image->rows; y++)
cristye6365592010-04-02 17:31:23 +0000957 {
cristyfcc25d92012-02-19 23:06:48 +0000958 PixelInfo
cristye6365592010-04-02 17:31:23 +0000959 pixel;
960
cristy4c08aed2011-07-01 19:47:50 +0000961 register const Quantum
cristye6365592010-04-02 17:31:23 +0000962 *restrict p;
963
cristy4c08aed2011-07-01 19:47:50 +0000964 register Quantum
965 *restrict q;
966
cristybb503372010-05-27 20:51:26 +0000967 register ssize_t
cristye6365592010-04-02 17:31:23 +0000968 x;
969
cristye6365592010-04-02 17:31:23 +0000970 if (status == MagickFalse)
971 continue;
972 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
973 q=GetCacheViewAuthenticPixels(color_view,0,y,color_image->columns,1,
974 exception);
cristy4c08aed2011-07-01 19:47:50 +0000975 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristye6365592010-04-02 17:31:23 +0000976 {
977 status=MagickFalse;
978 continue;
979 }
cristyfcc25d92012-02-19 23:06:48 +0000980 GetPixelInfo(image,&pixel);
cristybb503372010-05-27 20:51:26 +0000981 for (x=0; x < (ssize_t) image->columns; x++)
cristye6365592010-04-02 17:31:23 +0000982 {
cristybb503372010-05-27 20:51:26 +0000983 register ssize_t
cristye6365592010-04-02 17:31:23 +0000984 v;
985
cristybb503372010-05-27 20:51:26 +0000986 size_t
cristye6365592010-04-02 17:31:23 +0000987 height;
988
cristyfcc25d92012-02-19 23:06:48 +0000989 GetPixelInfoPixel(image,p,&pixel);
cristye6365592010-04-02 17:31:23 +0000990 height=color_matrix->height > 6 ? 6UL : color_matrix->height;
cristybb503372010-05-27 20:51:26 +0000991 for (v=0; v < (ssize_t) height; v++)
cristye6365592010-04-02 17:31:23 +0000992 {
cristyfcc25d92012-02-19 23:06:48 +0000993 MagickRealType
994 sum;
995
996 sum=ColorMatrix[v][0]*GetPixelRed(image,p)+ColorMatrix[v][1]*
cristy4c08aed2011-07-01 19:47:50 +0000997 GetPixelGreen(image,p)+ColorMatrix[v][2]*GetPixelBlue(image,p);
cristye6365592010-04-02 17:31:23 +0000998 if (image->colorspace == CMYKColorspace)
cristyfcc25d92012-02-19 23:06:48 +0000999 sum+=ColorMatrix[v][3]*GetPixelBlack(image,p);
cristy4c08aed2011-07-01 19:47:50 +00001000 if (image->matte != MagickFalse)
cristyfcc25d92012-02-19 23:06:48 +00001001 sum+=ColorMatrix[v][4]*GetPixelAlpha(image,p);
1002 sum+=QuantumRange*ColorMatrix[v][5];
cristye6365592010-04-02 17:31:23 +00001003 switch (v)
1004 {
cristyfcc25d92012-02-19 23:06:48 +00001005 case 0: pixel.red=sum; break;
1006 case 1: pixel.green=sum; break;
1007 case 2: pixel.blue=sum; break;
1008 case 3: pixel.black=sum; break;
1009 case 4: pixel.alpha=sum; break;
1010 default: break;
cristye6365592010-04-02 17:31:23 +00001011 }
1012 }
cristyfcc25d92012-02-19 23:06:48 +00001013 SetPixelInfoPixel(color_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +00001014 p+=GetPixelChannels(image);
1015 q+=GetPixelChannels(color_image);
cristye6365592010-04-02 17:31:23 +00001016 }
1017 if (SyncCacheViewAuthenticPixels(color_view,exception) == MagickFalse)
1018 status=MagickFalse;
1019 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1020 {
1021 MagickBooleanType
1022 proceed;
1023
1024#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00001025 #pragma omp critical (MagickCore_ColorMatrixImage)
cristye6365592010-04-02 17:31:23 +00001026#endif
1027 proceed=SetImageProgress(image,ColorMatrixImageTag,progress++,
1028 image->rows);
1029 if (proceed == MagickFalse)
1030 status=MagickFalse;
1031 }
1032 }
1033 color_view=DestroyCacheView(color_view);
1034 image_view=DestroyCacheView(image_view);
1035 if (status == MagickFalse)
1036 color_image=DestroyImage(color_image);
1037 return(color_image);
1038}
1039
1040/*
1041%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1042% %
1043% %
1044% %
cristy3ed852e2009-09-05 21:47:34 +00001045+ D e s t r o y F x I n f o %
1046% %
1047% %
1048% %
1049%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1050%
1051% DestroyFxInfo() deallocates memory associated with an FxInfo structure.
1052%
1053% The format of the DestroyFxInfo method is:
1054%
1055% ImageInfo *DestroyFxInfo(ImageInfo *fx_info)
1056%
1057% A description of each parameter follows:
1058%
1059% o fx_info: the fx info.
1060%
1061*/
cristy7832dc22011-09-05 01:21:53 +00001062MagickPrivate FxInfo *DestroyFxInfo(FxInfo *fx_info)
cristy3ed852e2009-09-05 21:47:34 +00001063{
cristybb503372010-05-27 20:51:26 +00001064 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001065 i;
1066
1067 fx_info->exception=DestroyExceptionInfo(fx_info->exception);
1068 fx_info->expression=DestroyString(fx_info->expression);
1069 fx_info->symbols=DestroySplayTree(fx_info->symbols);
1070 fx_info->colors=DestroySplayTree(fx_info->colors);
cristy0ea377f2011-03-24 00:54:19 +00001071 for (i=(ssize_t) GetImageListLength(fx_info->images)-1; i >= 0; i--)
cristyd76c51e2011-03-26 00:21:26 +00001072 fx_info->view[i]=DestroyCacheView(fx_info->view[i]);
1073 fx_info->view=(CacheView **) RelinquishMagickMemory(fx_info->view);
cristy3ed852e2009-09-05 21:47:34 +00001074 fx_info->random_info=DestroyRandomInfo(fx_info->random_info);
1075 fx_info=(FxInfo *) RelinquishMagickMemory(fx_info);
1076 return(fx_info);
1077}
1078
1079/*
1080%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1081% %
1082% %
1083% %
cristy3ed852e2009-09-05 21:47:34 +00001084+ 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 %
1085% %
1086% %
1087% %
1088%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1089%
1090% FxEvaluateChannelExpression() evaluates an expression and returns the
1091% results.
1092%
1093% The format of the FxEvaluateExpression method is:
1094%
1095% MagickRealType FxEvaluateChannelExpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00001096% const PixelChannel channel,const ssize_t x,const ssize_t y,
cristy3ed852e2009-09-05 21:47:34 +00001097% MagickRealType *alpha,Exceptioninfo *exception)
1098% MagickRealType FxEvaluateExpression(FxInfo *fx_info,
1099% MagickRealType *alpha,Exceptioninfo *exception)
1100%
1101% A description of each parameter follows:
1102%
1103% o fx_info: the fx info.
1104%
1105% o channel: the channel.
1106%
1107% o x,y: the pixel position.
1108%
1109% o alpha: the result.
1110%
1111% o exception: return any errors or warnings in this structure.
1112%
1113*/
1114
cristy351842f2010-03-07 15:27:38 +00001115static inline double MagickMax(const double x,const double y)
1116{
1117 if (x > y)
1118 return(x);
1119 return(y);
1120}
1121
1122static inline double MagickMin(const double x,const double y)
1123{
1124 if (x < y)
1125 return(x);
1126 return(y);
1127}
1128
cristy3ed852e2009-09-05 21:47:34 +00001129static MagickRealType FxChannelStatistics(FxInfo *fx_info,const Image *image,
cristy0568ffc2011-07-25 16:54:14 +00001130 PixelChannel channel,const char *symbol,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001131{
1132 char
1133 key[MaxTextExtent],
1134 statistic[MaxTextExtent];
1135
1136 const char
1137 *value;
1138
1139 register const char
1140 *p;
1141
1142 for (p=symbol; (*p != '.') && (*p != '\0'); p++) ;
1143 if (*p == '.')
1144 switch (*++p) /* e.g. depth.r */
1145 {
cristy541ae572011-08-05 19:08:59 +00001146 case 'r': channel=RedPixelChannel; break;
1147 case 'g': channel=GreenPixelChannel; break;
1148 case 'b': channel=BluePixelChannel; break;
1149 case 'c': channel=CyanPixelChannel; break;
1150 case 'm': channel=MagentaPixelChannel; break;
1151 case 'y': channel=YellowPixelChannel; break;
1152 case 'k': channel=BlackPixelChannel; break;
cristy3ed852e2009-09-05 21:47:34 +00001153 default: break;
1154 }
cristyb51dff52011-05-19 16:55:47 +00001155 (void) FormatLocaleString(key,MaxTextExtent,"%p.%.20g.%s",(void *) image,
cristye8c25f92010-06-03 00:53:06 +00001156 (double) channel,symbol);
cristy3ed852e2009-09-05 21:47:34 +00001157 value=(const char *) GetValueFromSplayTree(fx_info->symbols,key);
1158 if (value != (const char *) NULL)
cristydbdd0e32011-11-04 23:29:40 +00001159 return(QuantumScale*StringToDouble(value,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001160 (void) DeleteNodeFromSplayTree(fx_info->symbols,key);
1161 if (LocaleNCompare(symbol,"depth",5) == 0)
1162 {
cristybb503372010-05-27 20:51:26 +00001163 size_t
cristy3ed852e2009-09-05 21:47:34 +00001164 depth;
1165
cristyfefab1b2011-07-05 00:33:22 +00001166 depth=GetImageDepth(image,exception);
1167 (void) FormatLocaleString(statistic,MaxTextExtent,"%.20g",(double) depth);
cristy3ed852e2009-09-05 21:47:34 +00001168 }
1169 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1170 {
1171 double
1172 kurtosis,
1173 skewness;
1174
cristyd42d9952011-07-08 14:21:50 +00001175 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
cristyb51dff52011-05-19 16:55:47 +00001176 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",kurtosis);
cristy3ed852e2009-09-05 21:47:34 +00001177 }
1178 if (LocaleNCompare(symbol,"maxima",6) == 0)
1179 {
1180 double
1181 maxima,
1182 minima;
1183
cristyd42d9952011-07-08 14:21:50 +00001184 (void) GetImageRange(image,&minima,&maxima,exception);
cristyb51dff52011-05-19 16:55:47 +00001185 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",maxima);
cristy3ed852e2009-09-05 21:47:34 +00001186 }
1187 if (LocaleNCompare(symbol,"mean",4) == 0)
1188 {
1189 double
1190 mean,
1191 standard_deviation;
1192
cristyd42d9952011-07-08 14:21:50 +00001193 (void) GetImageMean(image,&mean,&standard_deviation,exception);
cristyb51dff52011-05-19 16:55:47 +00001194 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",mean);
cristy3ed852e2009-09-05 21:47:34 +00001195 }
1196 if (LocaleNCompare(symbol,"minima",6) == 0)
1197 {
1198 double
1199 maxima,
1200 minima;
1201
cristyd42d9952011-07-08 14:21:50 +00001202 (void) GetImageRange(image,&minima,&maxima,exception);
cristyb51dff52011-05-19 16:55:47 +00001203 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",minima);
cristy3ed852e2009-09-05 21:47:34 +00001204 }
1205 if (LocaleNCompare(symbol,"skewness",8) == 0)
1206 {
1207 double
1208 kurtosis,
1209 skewness;
1210
cristyd42d9952011-07-08 14:21:50 +00001211 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
cristyb51dff52011-05-19 16:55:47 +00001212 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",skewness);
cristy3ed852e2009-09-05 21:47:34 +00001213 }
1214 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1215 {
1216 double
1217 mean,
1218 standard_deviation;
1219
cristyd42d9952011-07-08 14:21:50 +00001220 (void) GetImageMean(image,&mean,&standard_deviation,exception);
cristyb51dff52011-05-19 16:55:47 +00001221 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",
cristy3ed852e2009-09-05 21:47:34 +00001222 standard_deviation);
1223 }
1224 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(key),
1225 ConstantString(statistic));
cristydbdd0e32011-11-04 23:29:40 +00001226 return(QuantumScale*StringToDouble(statistic,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001227}
1228
1229static MagickRealType
cristy0568ffc2011-07-25 16:54:14 +00001230 FxEvaluateSubexpression(FxInfo *,const PixelChannel,const ssize_t,
cristye85007d2010-06-06 22:51:36 +00001231 const ssize_t,const char *,MagickRealType *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +00001232
cristyb0aad4c2011-11-02 19:30:35 +00001233static MagickOffsetType FxGCD(MagickOffsetType alpha,MagickOffsetType beta)
1234{
1235 if (beta != 0)
1236 return(FxGCD(beta,alpha % beta));
1237 return(alpha);
1238}
1239
cristy3ed852e2009-09-05 21:47:34 +00001240static inline const char *FxSubexpression(const char *expression,
1241 ExceptionInfo *exception)
1242{
1243 const char
1244 *subexpression;
1245
cristybb503372010-05-27 20:51:26 +00001246 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001247 level;
1248
1249 level=0;
1250 subexpression=expression;
1251 while ((*subexpression != '\0') &&
1252 ((level != 1) || (strchr(")",(int) *subexpression) == (char *) NULL)))
1253 {
1254 if (strchr("(",(int) *subexpression) != (char *) NULL)
1255 level++;
1256 else
1257 if (strchr(")",(int) *subexpression) != (char *) NULL)
1258 level--;
1259 subexpression++;
1260 }
1261 if (*subexpression == '\0')
1262 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
anthonye5b39652012-04-21 05:37:29 +00001263 "UnbalancedParenthesis","'%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00001264 return(subexpression);
1265}
1266
cristy0568ffc2011-07-25 16:54:14 +00001267static MagickRealType FxGetSymbol(FxInfo *fx_info,const PixelChannel channel,
cristye85007d2010-06-06 22:51:36 +00001268 const ssize_t x,const ssize_t y,const char *expression,
1269 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001270{
1271 char
1272 *q,
1273 subexpression[MaxTextExtent],
1274 symbol[MaxTextExtent];
1275
1276 const char
1277 *p,
1278 *value;
1279
1280 Image
1281 *image;
1282
cristy4c08aed2011-07-01 19:47:50 +00001283 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001284 pixel;
1285
1286 MagickRealType
1287 alpha,
1288 beta;
1289
1290 PointInfo
1291 point;
1292
cristybb503372010-05-27 20:51:26 +00001293 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001294 i;
1295
1296 size_t
cristy1707c6c2012-01-18 23:30:54 +00001297 length,
cristy3ed852e2009-09-05 21:47:34 +00001298 level;
1299
1300 p=expression;
1301 i=GetImageIndexInList(fx_info->images);
1302 level=0;
1303 point.x=(double) x;
1304 point.y=(double) y;
1305 if (isalpha((int) *(p+1)) == 0)
1306 {
1307 if (strchr("suv",(int) *p) != (char *) NULL)
1308 {
1309 switch (*p)
1310 {
1311 case 's':
1312 default:
1313 {
1314 i=GetImageIndexInList(fx_info->images);
1315 break;
1316 }
1317 case 'u': i=0; break;
1318 case 'v': i=1; break;
1319 }
1320 p++;
1321 if (*p == '[')
1322 {
1323 level++;
1324 q=subexpression;
1325 for (p++; *p != '\0'; )
1326 {
1327 if (*p == '[')
1328 level++;
1329 else
1330 if (*p == ']')
1331 {
1332 level--;
1333 if (level == 0)
1334 break;
1335 }
1336 *q++=(*p++);
1337 }
1338 *q='\0';
1339 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1340 &beta,exception);
cristybb503372010-05-27 20:51:26 +00001341 i=(ssize_t) (alpha+0.5);
cristy3ed852e2009-09-05 21:47:34 +00001342 p++;
1343 }
1344 if (*p == '.')
1345 p++;
1346 }
1347 if ((isalpha((int) *(p+1)) == 0) && (*p == 'p'))
1348 {
1349 p++;
1350 if (*p == '{')
1351 {
1352 level++;
1353 q=subexpression;
1354 for (p++; *p != '\0'; )
1355 {
1356 if (*p == '{')
1357 level++;
1358 else
1359 if (*p == '}')
1360 {
1361 level--;
1362 if (level == 0)
1363 break;
1364 }
1365 *q++=(*p++);
1366 }
1367 *q='\0';
1368 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1369 &beta,exception);
1370 point.x=alpha;
1371 point.y=beta;
1372 p++;
1373 }
1374 else
1375 if (*p == '[')
1376 {
1377 level++;
1378 q=subexpression;
1379 for (p++; *p != '\0'; )
1380 {
1381 if (*p == '[')
1382 level++;
1383 else
1384 if (*p == ']')
1385 {
1386 level--;
1387 if (level == 0)
1388 break;
1389 }
1390 *q++=(*p++);
1391 }
1392 *q='\0';
1393 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1394 &beta,exception);
1395 point.x+=alpha;
1396 point.y+=beta;
1397 p++;
1398 }
1399 if (*p == '.')
1400 p++;
1401 }
1402 }
1403 length=GetImageListLength(fx_info->images);
1404 while (i < 0)
cristybb503372010-05-27 20:51:26 +00001405 i+=(ssize_t) length;
cristy3ed852e2009-09-05 21:47:34 +00001406 i%=length;
1407 image=GetImageFromList(fx_info->images,i);
1408 if (image == (Image *) NULL)
1409 {
1410 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
anthonye5b39652012-04-21 05:37:29 +00001411 "NoSuchImage","'%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00001412 return(0.0);
1413 }
cristy4c08aed2011-07-01 19:47:50 +00001414 GetPixelInfo(image,&pixel);
1415 (void) InterpolatePixelInfo(image,fx_info->view[i],image->interpolate,
cristy4f820712011-04-01 12:35:43 +00001416 point.x,point.y,&pixel,exception);
cristy1707c6c2012-01-18 23:30:54 +00001417 if ((strlen(p) > 2) && (LocaleCompare(p,"intensity") != 0) &&
1418 (LocaleCompare(p,"luminance") != 0) && (LocaleCompare(p,"hue") != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00001419 (LocaleCompare(p,"saturation") != 0) &&
1420 (LocaleCompare(p,"lightness") != 0))
1421 {
1422 char
1423 name[MaxTextExtent];
1424
1425 (void) CopyMagickString(name,p,MaxTextExtent);
1426 for (q=name+(strlen(name)-1); q > name; q--)
1427 {
1428 if (*q == ')')
1429 break;
1430 if (*q == '.')
1431 {
1432 *q='\0';
1433 break;
1434 }
1435 }
1436 if ((strlen(name) > 2) &&
1437 (GetValueFromSplayTree(fx_info->symbols,name) == (const char *) NULL))
1438 {
cristy4c08aed2011-07-01 19:47:50 +00001439 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001440 *color;
1441
cristy4c08aed2011-07-01 19:47:50 +00001442 color=(PixelInfo *) GetValueFromSplayTree(fx_info->colors,name);
1443 if (color != (PixelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001444 {
1445 pixel=(*color);
1446 p+=strlen(name);
1447 }
1448 else
cristy1707c6c2012-01-18 23:30:54 +00001449 {
1450 MagickBooleanType
1451 status;
1452
1453 status=QueryColorCompliance(name,AllCompliance,&pixel,
1454 fx_info->exception);
1455 if (status != MagickFalse)
1456 {
1457 (void) AddValueToSplayTree(fx_info->colors,ConstantString(
1458 name),ClonePixelInfo(&pixel));
1459 p+=strlen(name);
1460 }
1461 }
cristy3ed852e2009-09-05 21:47:34 +00001462 }
1463 }
1464 (void) CopyMagickString(symbol,p,MaxTextExtent);
1465 StripString(symbol);
1466 if (*symbol == '\0')
1467 {
1468 switch (channel)
1469 {
cristy0568ffc2011-07-25 16:54:14 +00001470 case RedPixelChannel: return(QuantumScale*pixel.red);
1471 case GreenPixelChannel: return(QuantumScale*pixel.green);
1472 case BluePixelChannel: return(QuantumScale*pixel.blue);
1473 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001474 {
1475 if (image->colorspace != CMYKColorspace)
1476 {
1477 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00001478 ImageError,"ColorSeparatedImageRequired","'%s'",
cristy3ed852e2009-09-05 21:47:34 +00001479 image->filename);
1480 return(0.0);
1481 }
cristy4c08aed2011-07-01 19:47:50 +00001482 return(QuantumScale*pixel.black);
1483 }
cristy0568ffc2011-07-25 16:54:14 +00001484 case AlphaPixelChannel:
cristy4c08aed2011-07-01 19:47:50 +00001485 {
1486 MagickRealType
1487 alpha;
1488
1489 if (pixel.matte == MagickFalse)
1490 return(1.0);
1491 alpha=(MagickRealType) (QuantumScale*pixel.alpha);
1492 return(alpha);
cristy3ed852e2009-09-05 21:47:34 +00001493 }
cristya382aca2011-12-06 18:22:48 +00001494 case IndexPixelChannel:
1495 return(0.0);
cristyb3a73b52011-07-26 01:34:43 +00001496 case IntensityPixelChannel:
cristyf364ed42010-12-15 01:54:43 +00001497 {
cristy4c08aed2011-07-01 19:47:50 +00001498 return(QuantumScale*GetPixelInfoIntensity(&pixel));
cristyf364ed42010-12-15 01:54:43 +00001499 }
cristy3ed852e2009-09-05 21:47:34 +00001500 default:
1501 break;
1502 }
1503 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
anthonye5b39652012-04-21 05:37:29 +00001504 "UnableToParseExpression","'%s'",p);
cristy3ed852e2009-09-05 21:47:34 +00001505 return(0.0);
1506 }
1507 switch (*symbol)
1508 {
1509 case 'A':
1510 case 'a':
1511 {
1512 if (LocaleCompare(symbol,"a") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001513 return((MagickRealType) (QuantumScale*pixel.alpha));
cristy3ed852e2009-09-05 21:47:34 +00001514 break;
1515 }
1516 case 'B':
1517 case 'b':
1518 {
1519 if (LocaleCompare(symbol,"b") == 0)
1520 return(QuantumScale*pixel.blue);
1521 break;
1522 }
1523 case 'C':
1524 case 'c':
1525 {
1526 if (LocaleNCompare(symbol,"channel",7) == 0)
1527 {
1528 GeometryInfo
1529 channel_info;
1530
1531 MagickStatusType
1532 flags;
1533
1534 flags=ParseGeometry(symbol+7,&channel_info);
1535 if (image->colorspace == CMYKColorspace)
1536 switch (channel)
1537 {
cristy0568ffc2011-07-25 16:54:14 +00001538 case CyanPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001539 {
1540 if ((flags & RhoValue) == 0)
1541 return(0.0);
1542 return(channel_info.rho);
1543 }
cristy0568ffc2011-07-25 16:54:14 +00001544 case MagentaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001545 {
1546 if ((flags & SigmaValue) == 0)
1547 return(0.0);
1548 return(channel_info.sigma);
1549 }
cristy0568ffc2011-07-25 16:54:14 +00001550 case YellowPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001551 {
1552 if ((flags & XiValue) == 0)
1553 return(0.0);
1554 return(channel_info.xi);
1555 }
cristy0568ffc2011-07-25 16:54:14 +00001556 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001557 {
1558 if ((flags & PsiValue) == 0)
1559 return(0.0);
1560 return(channel_info.psi);
1561 }
cristy0568ffc2011-07-25 16:54:14 +00001562 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001563 {
1564 if ((flags & ChiValue) == 0)
1565 return(0.0);
1566 return(channel_info.chi);
1567 }
1568 default:
1569 return(0.0);
1570 }
1571 switch (channel)
1572 {
cristy0568ffc2011-07-25 16:54:14 +00001573 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001574 {
1575 if ((flags & RhoValue) == 0)
1576 return(0.0);
1577 return(channel_info.rho);
1578 }
cristy0568ffc2011-07-25 16:54:14 +00001579 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001580 {
1581 if ((flags & SigmaValue) == 0)
1582 return(0.0);
1583 return(channel_info.sigma);
1584 }
cristy0568ffc2011-07-25 16:54:14 +00001585 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001586 {
1587 if ((flags & XiValue) == 0)
1588 return(0.0);
1589 return(channel_info.xi);
1590 }
cristy0568ffc2011-07-25 16:54:14 +00001591 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001592 {
1593 if ((flags & ChiValue) == 0)
1594 return(0.0);
1595 return(channel_info.chi);
1596 }
cristy0568ffc2011-07-25 16:54:14 +00001597 case AlphaPixelChannel:
cristy4c08aed2011-07-01 19:47:50 +00001598 {
1599 if ((flags & PsiValue) == 0)
1600 return(0.0);
1601 return(channel_info.psi);
1602 }
cristy3ed852e2009-09-05 21:47:34 +00001603 default:
1604 return(0.0);
1605 }
1606 return(0.0);
1607 }
1608 if (LocaleCompare(symbol,"c") == 0)
1609 return(QuantumScale*pixel.red);
1610 break;
1611 }
1612 case 'D':
1613 case 'd':
1614 {
1615 if (LocaleNCompare(symbol,"depth",5) == 0)
1616 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1617 break;
1618 }
1619 case 'G':
1620 case 'g':
1621 {
1622 if (LocaleCompare(symbol,"g") == 0)
1623 return(QuantumScale*pixel.green);
1624 break;
1625 }
1626 case 'K':
1627 case 'k':
1628 {
1629 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1630 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1631 if (LocaleCompare(symbol,"k") == 0)
1632 {
1633 if (image->colorspace != CMYKColorspace)
1634 {
1635 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00001636 OptionError,"ColorSeparatedImageRequired","'%s'",
cristy3ed852e2009-09-05 21:47:34 +00001637 image->filename);
1638 return(0.0);
1639 }
cristy4c08aed2011-07-01 19:47:50 +00001640 return(QuantumScale*pixel.black);
cristy3ed852e2009-09-05 21:47:34 +00001641 }
1642 break;
1643 }
1644 case 'H':
1645 case 'h':
1646 {
1647 if (LocaleCompare(symbol,"h") == 0)
1648 return((MagickRealType) image->rows);
1649 if (LocaleCompare(symbol,"hue") == 0)
1650 {
1651 double
1652 hue,
1653 lightness,
1654 saturation;
1655
cristy0a39a5c2012-06-27 12:51:45 +00001656 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
cristyda1f9c12011-10-02 21:39:49 +00001657 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001658 return(hue);
1659 }
1660 break;
1661 }
1662 case 'I':
1663 case 'i':
1664 {
1665 if ((LocaleCompare(symbol,"image.depth") == 0) ||
1666 (LocaleCompare(symbol,"image.minima") == 0) ||
1667 (LocaleCompare(symbol,"image.maxima") == 0) ||
1668 (LocaleCompare(symbol,"image.mean") == 0) ||
1669 (LocaleCompare(symbol,"image.kurtosis") == 0) ||
1670 (LocaleCompare(symbol,"image.skewness") == 0) ||
1671 (LocaleCompare(symbol,"image.standard_deviation") == 0))
1672 return(FxChannelStatistics(fx_info,image,channel,symbol+6,exception));
1673 if (LocaleCompare(symbol,"image.resolution.x") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001674 return(image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00001675 if (LocaleCompare(symbol,"image.resolution.y") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001676 return(image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001677 if (LocaleCompare(symbol,"intensity") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001678 return(QuantumScale*GetPixelInfoIntensity(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001679 if (LocaleCompare(symbol,"i") == 0)
1680 return((MagickRealType) x);
1681 break;
1682 }
1683 case 'J':
1684 case 'j':
1685 {
1686 if (LocaleCompare(symbol,"j") == 0)
1687 return((MagickRealType) y);
1688 break;
1689 }
1690 case 'L':
1691 case 'l':
1692 {
1693 if (LocaleCompare(symbol,"lightness") == 0)
1694 {
1695 double
1696 hue,
1697 lightness,
1698 saturation;
1699
cristy0a39a5c2012-06-27 12:51:45 +00001700 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
cristyda1f9c12011-10-02 21:39:49 +00001701 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001702 return(lightness);
1703 }
1704 if (LocaleCompare(symbol,"luminance") == 0)
1705 {
1706 double
1707 luminence;
1708
1709 luminence=0.2126*pixel.red+0.7152*pixel.green+0.0722*pixel.blue;
1710 return(QuantumScale*luminence);
1711 }
1712 break;
1713 }
1714 case 'M':
1715 case 'm':
1716 {
1717 if (LocaleNCompare(symbol,"maxima",6) == 0)
1718 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1719 if (LocaleNCompare(symbol,"mean",4) == 0)
1720 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1721 if (LocaleNCompare(symbol,"minima",6) == 0)
1722 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1723 if (LocaleCompare(symbol,"m") == 0)
1724 return(QuantumScale*pixel.blue);
1725 break;
1726 }
1727 case 'N':
1728 case 'n':
1729 {
1730 if (LocaleCompare(symbol,"n") == 0)
anthony374f5dd2011-03-25 10:08:53 +00001731 return((MagickRealType) GetImageListLength(fx_info->images));
cristy3ed852e2009-09-05 21:47:34 +00001732 break;
1733 }
1734 case 'O':
1735 case 'o':
1736 {
1737 if (LocaleCompare(symbol,"o") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001738 return(QuantumScale*pixel.alpha);
cristy3ed852e2009-09-05 21:47:34 +00001739 break;
1740 }
1741 case 'P':
1742 case 'p':
1743 {
1744 if (LocaleCompare(symbol,"page.height") == 0)
1745 return((MagickRealType) image->page.height);
1746 if (LocaleCompare(symbol,"page.width") == 0)
1747 return((MagickRealType) image->page.width);
1748 if (LocaleCompare(symbol,"page.x") == 0)
1749 return((MagickRealType) image->page.x);
1750 if (LocaleCompare(symbol,"page.y") == 0)
1751 return((MagickRealType) image->page.y);
1752 break;
1753 }
1754 case 'R':
1755 case 'r':
1756 {
1757 if (LocaleCompare(symbol,"resolution.x") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001758 return(image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00001759 if (LocaleCompare(symbol,"resolution.y") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001760 return(image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001761 if (LocaleCompare(symbol,"r") == 0)
1762 return(QuantumScale*pixel.red);
1763 break;
1764 }
1765 case 'S':
1766 case 's':
1767 {
1768 if (LocaleCompare(symbol,"saturation") == 0)
1769 {
1770 double
1771 hue,
1772 lightness,
1773 saturation;
1774
cristy0a39a5c2012-06-27 12:51:45 +00001775 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
cristyda1f9c12011-10-02 21:39:49 +00001776 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001777 return(saturation);
1778 }
1779 if (LocaleNCompare(symbol,"skewness",8) == 0)
1780 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1781 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1782 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1783 break;
1784 }
1785 case 'T':
1786 case 't':
1787 {
1788 if (LocaleCompare(symbol,"t") == 0)
cristy5a15b932011-03-26 12:50:33 +00001789 return((MagickRealType) GetImageIndexInList(fx_info->images));
cristy3ed852e2009-09-05 21:47:34 +00001790 break;
1791 }
1792 case 'W':
1793 case 'w':
1794 {
1795 if (LocaleCompare(symbol,"w") == 0)
1796 return((MagickRealType) image->columns);
1797 break;
1798 }
1799 case 'Y':
1800 case 'y':
1801 {
1802 if (LocaleCompare(symbol,"y") == 0)
1803 return(QuantumScale*pixel.green);
1804 break;
1805 }
1806 case 'Z':
1807 case 'z':
1808 {
1809 if (LocaleCompare(symbol,"z") == 0)
1810 {
1811 MagickRealType
1812 depth;
1813
cristyfefab1b2011-07-05 00:33:22 +00001814 depth=(MagickRealType) GetImageDepth(image,fx_info->exception);
cristy3ed852e2009-09-05 21:47:34 +00001815 return(depth);
1816 }
1817 break;
1818 }
1819 default:
1820 break;
1821 }
1822 value=(const char *) GetValueFromSplayTree(fx_info->symbols,symbol);
1823 if (value != (const char *) NULL)
cristydbdd0e32011-11-04 23:29:40 +00001824 return((MagickRealType) StringToDouble(value,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001825 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
anthonye5b39652012-04-21 05:37:29 +00001826 "UnableToParseExpression","'%s'",symbol);
cristy3ed852e2009-09-05 21:47:34 +00001827 return(0.0);
1828}
1829
1830static const char *FxOperatorPrecedence(const char *expression,
1831 ExceptionInfo *exception)
1832{
1833 typedef enum
1834 {
1835 UndefinedPrecedence,
1836 NullPrecedence,
1837 BitwiseComplementPrecedence,
1838 ExponentPrecedence,
cristy116af162010-08-13 01:25:47 +00001839 ExponentialNotationPrecedence,
cristy3ed852e2009-09-05 21:47:34 +00001840 MultiplyPrecedence,
1841 AdditionPrecedence,
1842 ShiftPrecedence,
1843 RelationalPrecedence,
1844 EquivalencyPrecedence,
1845 BitwiseAndPrecedence,
1846 BitwiseOrPrecedence,
1847 LogicalAndPrecedence,
1848 LogicalOrPrecedence,
1849 TernaryPrecedence,
1850 AssignmentPrecedence,
1851 CommaPrecedence,
1852 SeparatorPrecedence
1853 } FxPrecedence;
1854
1855 FxPrecedence
1856 precedence,
1857 target;
1858
1859 register const char
1860 *subexpression;
1861
1862 register int
1863 c;
1864
cristybb503372010-05-27 20:51:26 +00001865 size_t
cristy3ed852e2009-09-05 21:47:34 +00001866 level;
1867
1868 c=0;
1869 level=0;
1870 subexpression=(const char *) NULL;
1871 target=NullPrecedence;
1872 while (*expression != '\0')
1873 {
1874 precedence=UndefinedPrecedence;
1875 if ((isspace((int) ((char) *expression)) != 0) || (c == (int) '@'))
1876 {
1877 expression++;
1878 continue;
1879 }
cristy488fa882010-03-01 22:34:24 +00001880 switch (*expression)
1881 {
1882 case 'A':
1883 case 'a':
cristy3ed852e2009-09-05 21:47:34 +00001884 {
cristyb33454f2011-08-03 02:10:45 +00001885#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00001886 if (LocaleNCompare(expression,"acosh",5) == 0)
1887 {
1888 expression+=5;
1889 break;
1890 }
cristyb33454f2011-08-03 02:10:45 +00001891#endif
1892#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00001893 if (LocaleNCompare(expression,"asinh",5) == 0)
1894 {
1895 expression+=5;
1896 break;
1897 }
cristyb33454f2011-08-03 02:10:45 +00001898#endif
1899#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00001900 if (LocaleNCompare(expression,"atanh",5) == 0)
cristy488fa882010-03-01 22:34:24 +00001901 {
1902 expression+=5;
1903 break;
1904 }
cristyb33454f2011-08-03 02:10:45 +00001905#endif
anthony62838e52012-05-24 12:41:54 +00001906 if (LocaleNCompare(expression,"atan2",5) == 0)
1907 {
1908 expression+=5;
1909 break;
1910 }
cristy488fa882010-03-01 22:34:24 +00001911 break;
cristy3ed852e2009-09-05 21:47:34 +00001912 }
cristy62d455f2011-11-03 11:42:28 +00001913 case 'E':
1914 case 'e':
1915 {
1916 if ((LocaleNCompare(expression,"E+",2) == 0) ||
1917 (LocaleNCompare(expression,"E-",2) == 0))
1918 {
1919 expression+=2; /* scientific notation */
1920 break;
1921 }
1922 }
cristy488fa882010-03-01 22:34:24 +00001923 case 'J':
1924 case 'j':
1925 {
1926 if ((LocaleNCompare(expression,"j0",2) == 0) ||
1927 (LocaleNCompare(expression,"j1",2) == 0))
1928 {
1929 expression+=2;
1930 break;
1931 }
1932 break;
1933 }
cristy2def9322010-06-18 23:59:37 +00001934 case '#':
1935 {
1936 while (isxdigit((int) ((unsigned char) *(expression+1))) != 0)
1937 expression++;
1938 break;
1939 }
cristy488fa882010-03-01 22:34:24 +00001940 default:
1941 break;
1942 }
cristy3ed852e2009-09-05 21:47:34 +00001943 if ((c == (int) '{') || (c == (int) '['))
1944 level++;
1945 else
1946 if ((c == (int) '}') || (c == (int) ']'))
1947 level--;
1948 if (level == 0)
1949 switch ((unsigned char) *expression)
1950 {
1951 case '~':
1952 case '!':
1953 {
1954 precedence=BitwiseComplementPrecedence;
1955 break;
1956 }
1957 case '^':
cristy6621e252010-08-13 00:42:57 +00001958 case '@':
cristy3ed852e2009-09-05 21:47:34 +00001959 {
1960 precedence=ExponentPrecedence;
1961 break;
1962 }
1963 default:
1964 {
1965 if (((c != 0) && ((isdigit((int) ((char) c)) != 0) ||
1966 (strchr(")",c) != (char *) NULL))) &&
1967 (((islower((int) ((char) *expression)) != 0) ||
1968 (strchr("(",(int) *expression) != (char *) NULL)) ||
1969 ((isdigit((int) ((char) c)) == 0) &&
1970 (isdigit((int) ((char) *expression)) != 0))) &&
1971 (strchr("xy",(int) *expression) == (char *) NULL))
1972 precedence=MultiplyPrecedence;
1973 break;
1974 }
1975 case '*':
1976 case '/':
1977 case '%':
1978 {
1979 precedence=MultiplyPrecedence;
1980 break;
1981 }
1982 case '+':
1983 case '-':
1984 {
1985 if ((strchr("(+-/*%:&^|<>~,",c) == (char *) NULL) ||
1986 (isalpha(c) != 0))
1987 precedence=AdditionPrecedence;
1988 break;
1989 }
1990 case LeftShiftOperator:
1991 case RightShiftOperator:
1992 {
1993 precedence=ShiftPrecedence;
1994 break;
1995 }
1996 case '<':
1997 case LessThanEqualOperator:
1998 case GreaterThanEqualOperator:
1999 case '>':
2000 {
2001 precedence=RelationalPrecedence;
2002 break;
2003 }
2004 case EqualOperator:
2005 case NotEqualOperator:
2006 {
2007 precedence=EquivalencyPrecedence;
2008 break;
2009 }
2010 case '&':
2011 {
2012 precedence=BitwiseAndPrecedence;
2013 break;
2014 }
2015 case '|':
2016 {
2017 precedence=BitwiseOrPrecedence;
2018 break;
2019 }
2020 case LogicalAndOperator:
2021 {
2022 precedence=LogicalAndPrecedence;
2023 break;
2024 }
2025 case LogicalOrOperator:
2026 {
2027 precedence=LogicalOrPrecedence;
2028 break;
2029 }
cristy116af162010-08-13 01:25:47 +00002030 case ExponentialNotation:
2031 {
2032 precedence=ExponentialNotationPrecedence;
2033 break;
2034 }
cristy3ed852e2009-09-05 21:47:34 +00002035 case ':':
2036 case '?':
2037 {
2038 precedence=TernaryPrecedence;
2039 break;
2040 }
2041 case '=':
2042 {
2043 precedence=AssignmentPrecedence;
2044 break;
2045 }
2046 case ',':
2047 {
2048 precedence=CommaPrecedence;
2049 break;
2050 }
2051 case ';':
2052 {
2053 precedence=SeparatorPrecedence;
2054 break;
2055 }
2056 }
2057 if ((precedence == BitwiseComplementPrecedence) ||
2058 (precedence == TernaryPrecedence) ||
2059 (precedence == AssignmentPrecedence))
2060 {
2061 if (precedence > target)
2062 {
2063 /*
2064 Right-to-left associativity.
2065 */
2066 target=precedence;
2067 subexpression=expression;
2068 }
2069 }
2070 else
2071 if (precedence >= target)
2072 {
2073 /*
2074 Left-to-right associativity.
2075 */
2076 target=precedence;
2077 subexpression=expression;
2078 }
2079 if (strchr("(",(int) *expression) != (char *) NULL)
2080 expression=FxSubexpression(expression,exception);
2081 c=(int) (*expression++);
2082 }
2083 return(subexpression);
2084}
2085
2086static MagickRealType FxEvaluateSubexpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002087 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristye85007d2010-06-06 22:51:36 +00002088 const char *expression,MagickRealType *beta,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002089{
2090 char
2091 *q,
2092 subexpression[MaxTextExtent];
2093
2094 MagickRealType
2095 alpha,
2096 gamma;
2097
2098 register const char
2099 *p;
2100
2101 *beta=0.0;
2102 if (exception->severity != UndefinedException)
2103 return(0.0);
2104 while (isspace((int) *expression) != 0)
2105 expression++;
2106 if (*expression == '\0')
2107 {
2108 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
anthonye5b39652012-04-21 05:37:29 +00002109 "MissingExpression","'%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00002110 return(0.0);
2111 }
cristy66322f02010-05-17 11:40:48 +00002112 *subexpression='\0';
cristy3ed852e2009-09-05 21:47:34 +00002113 p=FxOperatorPrecedence(expression,exception);
2114 if (p != (const char *) NULL)
2115 {
2116 (void) CopyMagickString(subexpression,expression,(size_t)
2117 (p-expression+1));
2118 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2119 exception);
2120 switch ((unsigned char) *p)
2121 {
2122 case '~':
2123 {
2124 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristybb503372010-05-27 20:51:26 +00002125 *beta=(MagickRealType) (~(size_t) *beta);
cristy3ed852e2009-09-05 21:47:34 +00002126 return(*beta);
2127 }
2128 case '!':
2129 {
2130 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2131 return(*beta == 0.0 ? 1.0 : 0.0);
2132 }
2133 case '^':
2134 {
2135 *beta=pow((double) alpha,(double) FxEvaluateSubexpression(fx_info,
2136 channel,x,y,++p,beta,exception));
2137 return(*beta);
2138 }
2139 case '*':
cristy116af162010-08-13 01:25:47 +00002140 case ExponentialNotation:
cristy3ed852e2009-09-05 21:47:34 +00002141 {
2142 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2143 return(alpha*(*beta));
2144 }
2145 case '/':
2146 {
2147 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2148 if (*beta == 0.0)
2149 {
2150 if (exception->severity == UndefinedException)
2151 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00002152 OptionError,"DivideByZero","'%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00002153 return(0.0);
2154 }
2155 return(alpha/(*beta));
2156 }
2157 case '%':
2158 {
2159 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2160 *beta=fabs(floor(((double) *beta)+0.5));
2161 if (*beta == 0.0)
2162 {
2163 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00002164 OptionError,"DivideByZero","'%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00002165 return(0.0);
2166 }
2167 return(fmod((double) alpha,(double) *beta));
2168 }
2169 case '+':
2170 {
2171 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2172 return(alpha+(*beta));
2173 }
2174 case '-':
2175 {
2176 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2177 return(alpha-(*beta));
2178 }
2179 case LeftShiftOperator:
2180 {
2181 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy1707c6c2012-01-18 23:30:54 +00002182 *beta=(MagickRealType) ((size_t) (alpha+0.5) << (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002183 return(*beta);
2184 }
2185 case RightShiftOperator:
2186 {
2187 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy1707c6c2012-01-18 23:30:54 +00002188 *beta=(MagickRealType) ((size_t) (alpha+0.5) >> (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002189 return(*beta);
2190 }
2191 case '<':
2192 {
2193 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2194 return(alpha < *beta ? 1.0 : 0.0);
2195 }
2196 case LessThanEqualOperator:
2197 {
2198 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2199 return(alpha <= *beta ? 1.0 : 0.0);
2200 }
2201 case '>':
2202 {
2203 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2204 return(alpha > *beta ? 1.0 : 0.0);
2205 }
2206 case GreaterThanEqualOperator:
2207 {
2208 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2209 return(alpha >= *beta ? 1.0 : 0.0);
2210 }
2211 case EqualOperator:
2212 {
2213 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy9b528342012-06-02 00:59:20 +00002214 return(fabs(alpha-(*beta)) < MagickEpsilon ? MagickEpsilon : 0.0);
cristy3ed852e2009-09-05 21:47:34 +00002215 }
2216 case NotEqualOperator:
2217 {
2218 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy972050b2012-06-04 22:09:17 +00002219 return(fabs(alpha-(*beta)) >= MagickEpsilon ? 1.0 : 0.0);
cristy3ed852e2009-09-05 21:47:34 +00002220 }
2221 case '&':
2222 {
2223 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy1707c6c2012-01-18 23:30:54 +00002224 *beta=(MagickRealType) ((size_t) (alpha+0.5) & (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002225 return(*beta);
2226 }
2227 case '|':
2228 {
2229 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy1707c6c2012-01-18 23:30:54 +00002230 *beta=(MagickRealType) ((size_t) (alpha+0.5) | (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002231 return(*beta);
2232 }
2233 case LogicalAndOperator:
2234 {
2235 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2236 *beta=(alpha > 0.0) && (gamma > 0.0) ? 1.0 : 0.0;
2237 return(*beta);
2238 }
2239 case LogicalOrOperator:
2240 {
2241 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2242 *beta=(alpha > 0.0) || (gamma > 0.0) ? 1.0 : 0.0;
2243 return(*beta);
2244 }
2245 case '?':
2246 {
2247 MagickRealType
2248 gamma;
2249
2250 (void) CopyMagickString(subexpression,++p,MaxTextExtent);
2251 q=subexpression;
2252 p=StringToken(":",&q);
2253 if (q == (char *) NULL)
2254 {
2255 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00002256 OptionError,"UnableToParseExpression","'%s'",subexpression);
cristy3ed852e2009-09-05 21:47:34 +00002257 return(0.0);
2258 }
cristy972050b2012-06-04 22:09:17 +00002259 if (fabs((double) alpha) >= MagickEpsilon)
cristy3ed852e2009-09-05 21:47:34 +00002260 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,exception);
2261 else
2262 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,q,beta,exception);
2263 return(gamma);
2264 }
2265 case '=':
2266 {
2267 char
2268 numeric[MaxTextExtent];
2269
2270 q=subexpression;
2271 while (isalpha((int) ((unsigned char) *q)) != 0)
2272 q++;
2273 if (*q != '\0')
2274 {
2275 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00002276 OptionError,"UnableToParseExpression","'%s'",subexpression);
cristy3ed852e2009-09-05 21:47:34 +00002277 return(0.0);
2278 }
2279 ClearMagickException(exception);
2280 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristyb51dff52011-05-19 16:55:47 +00002281 (void) FormatLocaleString(numeric,MaxTextExtent,"%g",(double)
cristy8cd5b312010-01-07 01:10:24 +00002282 *beta);
cristy3ed852e2009-09-05 21:47:34 +00002283 (void) DeleteNodeFromSplayTree(fx_info->symbols,subexpression);
2284 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(
2285 subexpression),ConstantString(numeric));
2286 return(*beta);
2287 }
2288 case ',':
2289 {
2290 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2291 return(alpha);
2292 }
2293 case ';':
2294 {
2295 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2296 return(*beta);
2297 }
2298 default:
2299 {
2300 gamma=alpha*FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,
2301 exception);
2302 return(gamma);
2303 }
2304 }
2305 }
2306 if (strchr("(",(int) *expression) != (char *) NULL)
2307 {
2308 (void) CopyMagickString(subexpression,expression+1,MaxTextExtent);
2309 subexpression[strlen(subexpression)-1]='\0';
2310 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2311 exception);
2312 return(gamma);
2313 }
cristy8b8a3ae2010-10-23 18:49:46 +00002314 switch (*expression)
cristy3ed852e2009-09-05 21:47:34 +00002315 {
2316 case '+':
2317 {
2318 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2319 exception);
2320 return(1.0*gamma);
2321 }
2322 case '-':
2323 {
2324 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2325 exception);
2326 return(-1.0*gamma);
2327 }
2328 case '~':
2329 {
2330 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2331 exception);
cristybb503372010-05-27 20:51:26 +00002332 return((MagickRealType) (~(size_t) (gamma+0.5)));
cristy3ed852e2009-09-05 21:47:34 +00002333 }
2334 case 'A':
2335 case 'a':
2336 {
2337 if (LocaleNCompare(expression,"abs",3) == 0)
2338 {
2339 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2340 exception);
2341 return((MagickRealType) fabs((double) alpha));
2342 }
cristyb33454f2011-08-03 02:10:45 +00002343#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00002344 if (LocaleNCompare(expression,"acosh",5) == 0)
2345 {
2346 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2347 exception);
2348 return((MagickRealType) acosh((double) alpha));
2349 }
cristyb33454f2011-08-03 02:10:45 +00002350#endif
cristy3ed852e2009-09-05 21:47:34 +00002351 if (LocaleNCompare(expression,"acos",4) == 0)
2352 {
2353 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2354 exception);
2355 return((MagickRealType) acos((double) alpha));
2356 }
cristy43c22f42010-03-30 12:34:07 +00002357#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002358 if (LocaleNCompare(expression,"airy",4) == 0)
2359 {
2360 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2361 exception);
2362 if (alpha == 0.0)
cristy2dd03222010-03-30 22:12:11 +00002363 return(1.0);
2364 gamma=2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha);
cristy43c22f42010-03-30 12:34:07 +00002365 return(gamma*gamma);
cristyee56cf12010-03-01 22:17:06 +00002366 }
cristy43c22f42010-03-30 12:34:07 +00002367#endif
cristyb33454f2011-08-03 02:10:45 +00002368#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00002369 if (LocaleNCompare(expression,"asinh",5) == 0)
2370 {
2371 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2372 exception);
2373 return((MagickRealType) asinh((double) alpha));
2374 }
cristyb33454f2011-08-03 02:10:45 +00002375#endif
cristy3ed852e2009-09-05 21:47:34 +00002376 if (LocaleNCompare(expression,"asin",4) == 0)
2377 {
2378 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2379 exception);
2380 return((MagickRealType) asin((double) alpha));
2381 }
2382 if (LocaleNCompare(expression,"alt",3) == 0)
2383 {
2384 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2385 exception);
cristybb503372010-05-27 20:51:26 +00002386 return(((ssize_t) alpha) & 0x01 ? -1.0 : 1.0);
cristy3ed852e2009-09-05 21:47:34 +00002387 }
2388 if (LocaleNCompare(expression,"atan2",5) == 0)
2389 {
2390 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2391 exception);
2392 return((MagickRealType) atan2((double) alpha,(double) *beta));
2393 }
cristyb33454f2011-08-03 02:10:45 +00002394#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00002395 if (LocaleNCompare(expression,"atanh",5) == 0)
2396 {
2397 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2398 exception);
2399 return((MagickRealType) atanh((double) alpha));
2400 }
cristyb33454f2011-08-03 02:10:45 +00002401#endif
cristy3ed852e2009-09-05 21:47:34 +00002402 if (LocaleNCompare(expression,"atan",4) == 0)
2403 {
2404 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2405 exception);
2406 return((MagickRealType) atan((double) alpha));
2407 }
2408 if (LocaleCompare(expression,"a") == 0)
2409 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2410 break;
2411 }
2412 case 'B':
2413 case 'b':
2414 {
2415 if (LocaleCompare(expression,"b") == 0)
2416 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2417 break;
2418 }
2419 case 'C':
2420 case 'c':
2421 {
2422 if (LocaleNCompare(expression,"ceil",4) == 0)
2423 {
2424 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2425 exception);
2426 return((MagickRealType) ceil((double) alpha));
2427 }
2428 if (LocaleNCompare(expression,"cosh",4) == 0)
2429 {
2430 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2431 exception);
2432 return((MagickRealType) cosh((double) alpha));
2433 }
2434 if (LocaleNCompare(expression,"cos",3) == 0)
2435 {
2436 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2437 exception);
2438 return((MagickRealType) cos((double) alpha));
2439 }
2440 if (LocaleCompare(expression,"c") == 0)
2441 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2442 break;
2443 }
2444 case 'D':
2445 case 'd':
2446 {
2447 if (LocaleNCompare(expression,"debug",5) == 0)
2448 {
2449 const char
2450 *type;
2451
2452 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2453 exception);
2454 if (fx_info->images->colorspace == CMYKColorspace)
2455 switch (channel)
2456 {
cristy0568ffc2011-07-25 16:54:14 +00002457 case CyanPixelChannel: type="cyan"; break;
2458 case MagentaPixelChannel: type="magenta"; break;
2459 case YellowPixelChannel: type="yellow"; break;
2460 case AlphaPixelChannel: type="opacity"; break;
2461 case BlackPixelChannel: type="black"; break;
cristy3ed852e2009-09-05 21:47:34 +00002462 default: type="unknown"; break;
2463 }
2464 else
2465 switch (channel)
2466 {
cristy0568ffc2011-07-25 16:54:14 +00002467 case RedPixelChannel: type="red"; break;
2468 case GreenPixelChannel: type="green"; break;
2469 case BluePixelChannel: type="blue"; break;
2470 case AlphaPixelChannel: type="opacity"; break;
cristy3ed852e2009-09-05 21:47:34 +00002471 default: type="unknown"; break;
2472 }
2473 (void) CopyMagickString(subexpression,expression+6,MaxTextExtent);
2474 if (strlen(subexpression) > 1)
2475 subexpression[strlen(subexpression)-1]='\0';
2476 if (fx_info->file != (FILE *) NULL)
cristy1707c6c2012-01-18 23:30:54 +00002477 (void) FormatLocaleFile(fx_info->file,"%s[%.20g,%.20g].%s: "
2478 "%s=%.*g\n",fx_info->images->filename,(double) x,(double) y,type,
2479 subexpression,GetMagickPrecision(),(double) alpha);
cristy3ed852e2009-09-05 21:47:34 +00002480 return(0.0);
2481 }
cristy5597a8d2011-11-04 00:25:32 +00002482 if (LocaleNCompare(expression,"drc",3) == 0)
2483 {
2484 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2485 exception);
2486 return((MagickRealType) (alpha/(*beta*(alpha-1.0)+1.0)));
2487 }
cristy3ed852e2009-09-05 21:47:34 +00002488 break;
2489 }
2490 case 'E':
2491 case 'e':
2492 {
2493 if (LocaleCompare(expression,"epsilon") == 0)
2494 return((MagickRealType) MagickEpsilon);
2495 if (LocaleNCompare(expression,"exp",3) == 0)
2496 {
2497 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2498 exception);
2499 return((MagickRealType) exp((double) alpha));
2500 }
2501 if (LocaleCompare(expression,"e") == 0)
2502 return((MagickRealType) 2.7182818284590452354);
2503 break;
2504 }
2505 case 'F':
2506 case 'f':
2507 {
2508 if (LocaleNCompare(expression,"floor",5) == 0)
2509 {
2510 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2511 exception);
2512 return((MagickRealType) floor((double) alpha));
2513 }
2514 break;
2515 }
2516 case 'G':
2517 case 'g':
2518 {
cristy9eeedea2011-11-02 19:04:05 +00002519 if (LocaleNCompare(expression,"gauss",5) == 0)
2520 {
2521 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2522 exception);
2523 gamma=exp((double) (-alpha*alpha/2.0))/sqrt(2.0*MagickPI);
2524 return((MagickRealType) gamma);
2525 }
cristyb0aad4c2011-11-02 19:30:35 +00002526 if (LocaleNCompare(expression,"gcd",3) == 0)
2527 {
2528 MagickOffsetType
2529 gcd;
2530
2531 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2532 exception);
cristy1707c6c2012-01-18 23:30:54 +00002533 gcd=FxGCD((MagickOffsetType) (alpha+0.5),(MagickOffsetType) (*beta+
2534 0.5));
cristyb0aad4c2011-11-02 19:30:35 +00002535 return((MagickRealType) gcd);
2536 }
cristy3ed852e2009-09-05 21:47:34 +00002537 if (LocaleCompare(expression,"g") == 0)
2538 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2539 break;
2540 }
2541 case 'H':
2542 case 'h':
2543 {
2544 if (LocaleCompare(expression,"h") == 0)
2545 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2546 if (LocaleCompare(expression,"hue") == 0)
2547 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2548 if (LocaleNCompare(expression,"hypot",5) == 0)
2549 {
2550 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2551 exception);
2552 return((MagickRealType) hypot((double) alpha,(double) *beta));
2553 }
2554 break;
2555 }
2556 case 'K':
2557 case 'k':
2558 {
2559 if (LocaleCompare(expression,"k") == 0)
2560 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2561 break;
2562 }
2563 case 'I':
2564 case 'i':
2565 {
2566 if (LocaleCompare(expression,"intensity") == 0)
2567 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2568 if (LocaleNCompare(expression,"int",3) == 0)
2569 {
2570 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2571 exception);
cristy16788e42010-08-13 13:44:26 +00002572 return((MagickRealType) floor(alpha));
cristy3ed852e2009-09-05 21:47:34 +00002573 }
cristy82b20722011-11-05 21:52:36 +00002574#if defined(MAGICKCORE_HAVE_ISNAN)
cristy639399c2011-11-02 19:16:15 +00002575 if (LocaleNCompare(expression,"isnan",5) == 0)
2576 {
2577 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2578 exception);
cristy17a10202011-11-02 19:17:04 +00002579 return((MagickRealType) !!isnan((double) alpha));
cristy639399c2011-11-02 19:16:15 +00002580 }
cristy82b20722011-11-05 21:52:36 +00002581#endif
cristy3ed852e2009-09-05 21:47:34 +00002582 if (LocaleCompare(expression,"i") == 0)
2583 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2584 break;
2585 }
2586 case 'J':
2587 case 'j':
2588 {
2589 if (LocaleCompare(expression,"j") == 0)
2590 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
cristy161b9262010-03-20 19:34:32 +00002591#if defined(MAGICKCORE_HAVE_J0)
cristyee56cf12010-03-01 22:17:06 +00002592 if (LocaleNCompare(expression,"j0",2) == 0)
2593 {
2594 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2595 exception);
2596 return((MagickRealType) j0((double) alpha));
2597 }
cristy161b9262010-03-20 19:34:32 +00002598#endif
2599#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002600 if (LocaleNCompare(expression,"j1",2) == 0)
2601 {
2602 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2603 exception);
2604 return((MagickRealType) j1((double) alpha));
2605 }
cristy161b9262010-03-20 19:34:32 +00002606#endif
cristyaa018fa2010-04-08 23:03:54 +00002607#if defined(MAGICKCORE_HAVE_J1)
cristya6a09e72010-03-02 14:51:02 +00002608 if (LocaleNCompare(expression,"jinc",4) == 0)
2609 {
2610 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2611 exception);
cristy0946a822010-03-12 17:14:58 +00002612 if (alpha == 0.0)
2613 return(1.0);
cristy1707c6c2012-01-18 23:30:54 +00002614 gamma=(MagickRealType) (2.0*j1((double) (MagickPI*alpha))/(MagickPI*
2615 alpha));
cristyfce2f7b2010-03-12 00:29:49 +00002616 return(gamma);
cristya6a09e72010-03-02 14:51:02 +00002617 }
cristyaa018fa2010-04-08 23:03:54 +00002618#endif
cristy3ed852e2009-09-05 21:47:34 +00002619 break;
2620 }
2621 case 'L':
2622 case 'l':
2623 {
2624 if (LocaleNCompare(expression,"ln",2) == 0)
2625 {
2626 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2627 exception);
2628 return((MagickRealType) log((double) alpha));
2629 }
cristyc8ed5322010-08-31 12:07:59 +00002630 if (LocaleNCompare(expression,"logtwo",6) == 0)
cristy3ed852e2009-09-05 21:47:34 +00002631 {
cristyc8ed5322010-08-31 12:07:59 +00002632 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
cristy3ed852e2009-09-05 21:47:34 +00002633 exception);
2634 return((MagickRealType) log10((double) alpha))/log10(2.0);
2635 }
2636 if (LocaleNCompare(expression,"log",3) == 0)
2637 {
2638 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2639 exception);
2640 return((MagickRealType) log10((double) alpha));
2641 }
2642 if (LocaleCompare(expression,"lightness") == 0)
2643 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2644 break;
2645 }
2646 case 'M':
2647 case 'm':
2648 {
2649 if (LocaleCompare(expression,"MaxRGB") == 0)
2650 return((MagickRealType) QuantumRange);
2651 if (LocaleNCompare(expression,"maxima",6) == 0)
2652 break;
2653 if (LocaleNCompare(expression,"max",3) == 0)
cristy984049c2011-11-03 18:34:58 +00002654 {
2655 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2656 exception);
2657 return(alpha > *beta ? alpha : *beta);
2658 }
cristy3ed852e2009-09-05 21:47:34 +00002659 if (LocaleNCompare(expression,"minima",6) == 0)
2660 break;
2661 if (LocaleNCompare(expression,"min",3) == 0)
cristy984049c2011-11-03 18:34:58 +00002662 {
2663 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2664 exception);
2665 return(alpha < *beta ? alpha : *beta);
2666 }
cristy3ed852e2009-09-05 21:47:34 +00002667 if (LocaleNCompare(expression,"mod",3) == 0)
2668 {
2669 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2670 exception);
cristy984049c2011-11-03 18:34:58 +00002671 gamma=alpha-floor((double) (alpha/(*beta)))*(*beta);
2672 return(gamma);
cristy3ed852e2009-09-05 21:47:34 +00002673 }
2674 if (LocaleCompare(expression,"m") == 0)
2675 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2676 break;
2677 }
2678 case 'N':
2679 case 'n':
2680 {
cristyad3502e2011-11-02 19:10:45 +00002681 if (LocaleNCompare(expression,"not",3) == 0)
2682 {
2683 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2684 exception);
2685 return((MagickRealType) (alpha < MagickEpsilon));
2686 }
cristy3ed852e2009-09-05 21:47:34 +00002687 if (LocaleCompare(expression,"n") == 0)
2688 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2689 break;
2690 }
2691 case 'O':
2692 case 'o':
2693 {
2694 if (LocaleCompare(expression,"Opaque") == 0)
2695 return(1.0);
2696 if (LocaleCompare(expression,"o") == 0)
2697 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2698 break;
2699 }
2700 case 'P':
2701 case 'p':
2702 {
cristy670aa3c2011-11-03 00:54:00 +00002703 if (LocaleCompare(expression,"phi") == 0)
2704 return((MagickRealType) MagickPHI);
cristy3ed852e2009-09-05 21:47:34 +00002705 if (LocaleCompare(expression,"pi") == 0)
2706 return((MagickRealType) MagickPI);
2707 if (LocaleNCompare(expression,"pow",3) == 0)
2708 {
2709 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2710 exception);
2711 return((MagickRealType) pow((double) alpha,(double) *beta));
2712 }
2713 if (LocaleCompare(expression,"p") == 0)
2714 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2715 break;
2716 }
2717 case 'Q':
2718 case 'q':
2719 {
2720 if (LocaleCompare(expression,"QuantumRange") == 0)
2721 return((MagickRealType) QuantumRange);
2722 if (LocaleCompare(expression,"QuantumScale") == 0)
2723 return((MagickRealType) QuantumScale);
2724 break;
2725 }
2726 case 'R':
2727 case 'r':
2728 {
2729 if (LocaleNCompare(expression,"rand",4) == 0)
2730 return((MagickRealType) GetPseudoRandomValue(fx_info->random_info));
2731 if (LocaleNCompare(expression,"round",5) == 0)
2732 {
2733 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2734 exception);
cristy16788e42010-08-13 13:44:26 +00002735 return((MagickRealType) floor((double) alpha+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002736 }
2737 if (LocaleCompare(expression,"r") == 0)
2738 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2739 break;
2740 }
2741 case 'S':
2742 case 's':
2743 {
2744 if (LocaleCompare(expression,"saturation") == 0)
2745 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2746 if (LocaleNCompare(expression,"sign",4) == 0)
2747 {
2748 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2749 exception);
2750 return(alpha < 0.0 ? -1.0 : 1.0);
2751 }
cristya6a09e72010-03-02 14:51:02 +00002752 if (LocaleNCompare(expression,"sinc",4) == 0)
2753 {
2754 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2755 exception);
2756 if (alpha == 0)
2757 return(1.0);
2758 gamma=(MagickRealType) (sin((double) (MagickPI*alpha))/
2759 (MagickPI*alpha));
2760 return(gamma);
2761 }
cristy3ed852e2009-09-05 21:47:34 +00002762 if (LocaleNCompare(expression,"sinh",4) == 0)
2763 {
2764 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2765 exception);
2766 return((MagickRealType) sinh((double) alpha));
2767 }
2768 if (LocaleNCompare(expression,"sin",3) == 0)
2769 {
2770 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2771 exception);
2772 return((MagickRealType) sin((double) alpha));
2773 }
2774 if (LocaleNCompare(expression,"sqrt",4) == 0)
2775 {
2776 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2777 exception);
2778 return((MagickRealType) sqrt((double) alpha));
2779 }
cristy9eeedea2011-11-02 19:04:05 +00002780 if (LocaleNCompare(expression,"squish",6) == 0)
2781 {
2782 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2783 exception);
cristyf7b28ea2012-04-27 11:22:49 +00002784 return((MagickRealType) (1.0/(1.0+exp((double) (-alpha)))));
cristy9eeedea2011-11-02 19:04:05 +00002785 }
cristy3ed852e2009-09-05 21:47:34 +00002786 if (LocaleCompare(expression,"s") == 0)
2787 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2788 break;
2789 }
2790 case 'T':
2791 case 't':
2792 {
2793 if (LocaleNCompare(expression,"tanh",4) == 0)
2794 {
2795 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2796 exception);
2797 return((MagickRealType) tanh((double) alpha));
2798 }
2799 if (LocaleNCompare(expression,"tan",3) == 0)
2800 {
2801 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2802 exception);
2803 return((MagickRealType) tan((double) alpha));
2804 }
2805 if (LocaleCompare(expression,"Transparent") == 0)
2806 return(0.0);
cristy16788e42010-08-13 13:44:26 +00002807 if (LocaleNCompare(expression,"trunc",5) == 0)
2808 {
2809 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2810 exception);
2811 if (alpha >= 0.0)
2812 return((MagickRealType) floor((double) alpha));
2813 return((MagickRealType) ceil((double) alpha));
2814 }
cristy3ed852e2009-09-05 21:47:34 +00002815 if (LocaleCompare(expression,"t") == 0)
2816 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2817 break;
2818 }
2819 case 'U':
2820 case 'u':
2821 {
2822 if (LocaleCompare(expression,"u") == 0)
2823 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2824 break;
2825 }
2826 case 'V':
2827 case 'v':
2828 {
2829 if (LocaleCompare(expression,"v") == 0)
2830 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2831 break;
2832 }
2833 case 'W':
2834 case 'w':
2835 {
cristy9eeedea2011-11-02 19:04:05 +00002836 if (LocaleNCompare(expression,"while",5) == 0)
2837 {
2838 do
2839 {
2840 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2841 exception);
2842 } while (fabs((double) alpha) >= MagickEpsilon);
2843 return((MagickRealType) *beta);
2844 }
cristy3ed852e2009-09-05 21:47:34 +00002845 if (LocaleCompare(expression,"w") == 0)
2846 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2847 break;
2848 }
2849 case 'Y':
2850 case 'y':
2851 {
2852 if (LocaleCompare(expression,"y") == 0)
2853 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2854 break;
2855 }
2856 case 'Z':
2857 case 'z':
2858 {
2859 if (LocaleCompare(expression,"z") == 0)
2860 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2861 break;
2862 }
2863 default:
2864 break;
2865 }
2866 q=(char *) expression;
cristydbdd0e32011-11-04 23:29:40 +00002867 alpha=InterpretSiPrefixValue(expression,&q);
cristy3ed852e2009-09-05 21:47:34 +00002868 if (q == expression)
2869 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2870 return(alpha);
2871}
2872
cristy7832dc22011-09-05 01:21:53 +00002873MagickPrivate MagickBooleanType FxEvaluateExpression(FxInfo *fx_info,
cristy3ed852e2009-09-05 21:47:34 +00002874 MagickRealType *alpha,ExceptionInfo *exception)
2875{
2876 MagickBooleanType
2877 status;
2878
cristy541ae572011-08-05 19:08:59 +00002879 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2880 exception);
cristy3ed852e2009-09-05 21:47:34 +00002881 return(status);
2882}
2883
2884MagickExport MagickBooleanType FxPreprocessExpression(FxInfo *fx_info,
2885 MagickRealType *alpha,ExceptionInfo *exception)
2886{
2887 FILE
2888 *file;
2889
2890 MagickBooleanType
2891 status;
2892
2893 file=fx_info->file;
2894 fx_info->file=(FILE *) NULL;
cristy541ae572011-08-05 19:08:59 +00002895 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2896 exception);
cristy3ed852e2009-09-05 21:47:34 +00002897 fx_info->file=file;
2898 return(status);
2899}
2900
cristy7832dc22011-09-05 01:21:53 +00002901MagickPrivate MagickBooleanType FxEvaluateChannelExpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002902 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristye85007d2010-06-06 22:51:36 +00002903 MagickRealType *alpha,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002904{
2905 MagickRealType
2906 beta;
2907
2908 beta=0.0;
2909 *alpha=FxEvaluateSubexpression(fx_info,channel,x,y,fx_info->expression,&beta,
2910 exception);
2911 return(exception->severity == OptionError ? MagickFalse : MagickTrue);
2912}
2913
2914/*
2915%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2916% %
2917% %
2918% %
2919% F x I m a g e %
2920% %
2921% %
2922% %
2923%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2924%
2925% FxImage() applies a mathematical expression to the specified image.
2926%
2927% The format of the FxImage method is:
2928%
2929% Image *FxImage(const Image *image,const char *expression,
2930% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002931%
2932% A description of each parameter follows:
2933%
2934% o image: the image.
2935%
cristy3ed852e2009-09-05 21:47:34 +00002936% o expression: A mathematical expression.
2937%
2938% o exception: return any errors or warnings in this structure.
2939%
2940*/
2941
2942static FxInfo **DestroyFxThreadSet(FxInfo **fx_info)
2943{
cristybb503372010-05-27 20:51:26 +00002944 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002945 i;
2946
2947 assert(fx_info != (FxInfo **) NULL);
cristyac245f82012-05-05 17:13:57 +00002948 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
cristy3ed852e2009-09-05 21:47:34 +00002949 if (fx_info[i] != (FxInfo *) NULL)
2950 fx_info[i]=DestroyFxInfo(fx_info[i]);
cristyb41ee102010-10-04 16:46:15 +00002951 fx_info=(FxInfo **) RelinquishMagickMemory(fx_info);
cristy3ed852e2009-09-05 21:47:34 +00002952 return(fx_info);
2953}
2954
2955static FxInfo **AcquireFxThreadSet(const Image *image,const char *expression,
2956 ExceptionInfo *exception)
2957{
2958 char
2959 *fx_expression;
2960
2961 FxInfo
2962 **fx_info;
2963
2964 MagickRealType
2965 alpha;
2966
cristybb503372010-05-27 20:51:26 +00002967 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002968 i;
2969
cristybb503372010-05-27 20:51:26 +00002970 size_t
cristy3ed852e2009-09-05 21:47:34 +00002971 number_threads;
2972
cristyfeeb98d2012-05-09 16:32:12 +00002973 number_threads=GetOpenMPMaximumThreads();
cristyb41ee102010-10-04 16:46:15 +00002974 fx_info=(FxInfo **) AcquireQuantumMemory(number_threads,sizeof(*fx_info));
cristy3ed852e2009-09-05 21:47:34 +00002975 if (fx_info == (FxInfo **) NULL)
2976 return((FxInfo **) NULL);
2977 (void) ResetMagickMemory(fx_info,0,number_threads*sizeof(*fx_info));
2978 if (*expression != '@')
2979 fx_expression=ConstantString(expression);
2980 else
2981 fx_expression=FileToString(expression+1,~0,exception);
cristybb503372010-05-27 20:51:26 +00002982 for (i=0; i < (ssize_t) number_threads; i++)
cristy3ed852e2009-09-05 21:47:34 +00002983 {
cristydb070952012-04-20 14:33:00 +00002984 fx_info[i]=AcquireFxInfo(image,fx_expression,exception);
cristy3ed852e2009-09-05 21:47:34 +00002985 if (fx_info[i] == (FxInfo *) NULL)
2986 return(DestroyFxThreadSet(fx_info));
2987 (void) FxPreprocessExpression(fx_info[i],&alpha,fx_info[i]->exception);
2988 }
2989 fx_expression=DestroyString(fx_expression);
2990 return(fx_info);
2991}
2992
2993MagickExport Image *FxImage(const Image *image,const char *expression,
2994 ExceptionInfo *exception)
2995{
cristy3ed852e2009-09-05 21:47:34 +00002996#define FxImageTag "Fx/Image"
2997
cristyfa112112010-01-04 17:48:07 +00002998 CacheView
cristy79cedc72011-07-25 00:41:15 +00002999 *fx_view,
3000 *image_view;
cristyfa112112010-01-04 17:48:07 +00003001
cristy3ed852e2009-09-05 21:47:34 +00003002 FxInfo
cristyfa112112010-01-04 17:48:07 +00003003 **restrict fx_info;
cristy3ed852e2009-09-05 21:47:34 +00003004
3005 Image
3006 *fx_image;
3007
cristy3ed852e2009-09-05 21:47:34 +00003008 MagickBooleanType
3009 status;
3010
cristybb503372010-05-27 20:51:26 +00003011 MagickOffsetType
3012 progress;
3013
cristy3ed852e2009-09-05 21:47:34 +00003014 MagickRealType
3015 alpha;
3016
cristybb503372010-05-27 20:51:26 +00003017 ssize_t
3018 y;
3019
cristy3ed852e2009-09-05 21:47:34 +00003020 assert(image != (Image *) NULL);
3021 assert(image->signature == MagickSignature);
3022 if (image->debug != MagickFalse)
3023 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy79cedc72011-07-25 00:41:15 +00003024 fx_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00003025 if (fx_image == (Image *) NULL)
3026 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003027 if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003028 {
cristy3ed852e2009-09-05 21:47:34 +00003029 fx_image=DestroyImage(fx_image);
3030 return((Image *) NULL);
3031 }
3032 fx_info=AcquireFxThreadSet(image,expression,exception);
3033 if (fx_info == (FxInfo **) NULL)
3034 {
3035 fx_image=DestroyImage(fx_image);
3036 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3037 }
3038 status=FxPreprocessExpression(fx_info[0],&alpha,exception);
3039 if (status == MagickFalse)
3040 {
3041 fx_image=DestroyImage(fx_image);
3042 fx_info=DestroyFxThreadSet(fx_info);
3043 return((Image *) NULL);
3044 }
3045 /*
3046 Fx image.
3047 */
3048 status=MagickTrue;
3049 progress=0;
cristydb070952012-04-20 14:33:00 +00003050 image_view=AcquireVirtualCacheView(image,exception);
3051 fx_view=AcquireAuthenticCacheView(fx_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00003052#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003053 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00003054 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003055#endif
cristybb503372010-05-27 20:51:26 +00003056 for (y=0; y < (ssize_t) fx_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003057 {
cristy5c9e6f22010-09-17 17:31:01 +00003058 const int
3059 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00003060
cristy79cedc72011-07-25 00:41:15 +00003061 register const Quantum
3062 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003063
cristy4c08aed2011-07-01 19:47:50 +00003064 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003065 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003066
cristy79cedc72011-07-25 00:41:15 +00003067 register ssize_t
3068 x;
3069
cristy3ed852e2009-09-05 21:47:34 +00003070 if (status == MagickFalse)
3071 continue;
cristy79cedc72011-07-25 00:41:15 +00003072 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +00003073 q=QueueCacheViewAuthenticPixels(fx_view,0,y,fx_image->columns,1,exception);
cristy79cedc72011-07-25 00:41:15 +00003074 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003075 {
3076 status=MagickFalse;
3077 continue;
3078 }
cristybb503372010-05-27 20:51:26 +00003079 for (x=0; x < (ssize_t) fx_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003080 {
cristy79cedc72011-07-25 00:41:15 +00003081 register ssize_t
3082 i;
3083
cristy177e41c2012-04-15 15:08:25 +00003084 if (GetPixelMask(image,p) != 0)
3085 {
3086 p+=GetPixelChannels(image);
3087 q+=GetPixelChannels(fx_image);
3088 continue;
3089 }
cristy79cedc72011-07-25 00:41:15 +00003090 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3091 {
3092 MagickRealType
3093 alpha;
3094
3095 PixelChannel
3096 channel;
3097
3098 PixelTrait
3099 fx_traits,
3100 traits;
3101
cristye2a912b2011-12-05 20:02:07 +00003102 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003103 traits=GetPixelChannelMapTraits(image,channel);
cristy79cedc72011-07-25 00:41:15 +00003104 fx_traits=GetPixelChannelMapTraits(fx_image,channel);
cristy010d7d12011-08-31 01:02:48 +00003105 if ((traits == UndefinedPixelTrait) ||
3106 (fx_traits == UndefinedPixelTrait))
cristy79cedc72011-07-25 00:41:15 +00003107 continue;
cristy177e41c2012-04-15 15:08:25 +00003108 if ((fx_traits & CopyPixelTrait) != 0)
cristy79cedc72011-07-25 00:41:15 +00003109 {
cristy0beccfa2011-09-25 20:47:53 +00003110 SetPixelChannel(fx_image,channel,p[i],q);
cristy79cedc72011-07-25 00:41:15 +00003111 continue;
3112 }
3113 alpha=0.0;
cristya382aca2011-12-06 18:22:48 +00003114 (void) FxEvaluateChannelExpression(fx_info[id],channel,x,y,&alpha,
3115 exception);
cristy8cd03c32012-07-07 18:57:59 +00003116 q[i]=ClampToQuantum(QuantumRange*alpha);
cristy79cedc72011-07-25 00:41:15 +00003117 }
3118 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003119 q+=GetPixelChannels(fx_image);
cristy3ed852e2009-09-05 21:47:34 +00003120 }
3121 if (SyncCacheViewAuthenticPixels(fx_view,exception) == MagickFalse)
3122 status=MagickFalse;
3123 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3124 {
3125 MagickBooleanType
3126 proceed;
3127
cristyb5d5f722009-11-04 03:03:49 +00003128#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003129 #pragma omp critical (MagickCore_FxImage)
cristy3ed852e2009-09-05 21:47:34 +00003130#endif
3131 proceed=SetImageProgress(image,FxImageTag,progress++,image->rows);
3132 if (proceed == MagickFalse)
3133 status=MagickFalse;
3134 }
3135 }
cristy3ed852e2009-09-05 21:47:34 +00003136 fx_view=DestroyCacheView(fx_view);
cristy79cedc72011-07-25 00:41:15 +00003137 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003138 fx_info=DestroyFxThreadSet(fx_info);
3139 if (status == MagickFalse)
3140 fx_image=DestroyImage(fx_image);
3141 return(fx_image);
3142}
3143
3144/*
3145%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3146% %
3147% %
3148% %
3149% I m p l o d e I m a g e %
3150% %
3151% %
3152% %
3153%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3154%
3155% ImplodeImage() creates a new image that is a copy of an existing
3156% one with the image pixels "implode" by the specified percentage. It
3157% allocates the memory necessary for the new Image structure and returns a
3158% pointer to the new image.
3159%
3160% The format of the ImplodeImage method is:
3161%
3162% Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003163% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003164%
3165% A description of each parameter follows:
3166%
3167% o implode_image: Method ImplodeImage returns a pointer to the image
3168% after it is implode. A null image is returned if there is a memory
3169% shortage.
3170%
3171% o image: the image.
3172%
3173% o amount: Define the extent of the implosion.
3174%
cristy76f512e2011-09-12 01:26:56 +00003175% o method: the pixel interpolation method.
3176%
cristy3ed852e2009-09-05 21:47:34 +00003177% o exception: return any errors or warnings in this structure.
3178%
3179*/
3180MagickExport Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003181 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003182{
3183#define ImplodeImageTag "Implode/Image"
3184
cristyfa112112010-01-04 17:48:07 +00003185 CacheView
3186 *image_view,
3187 *implode_view;
3188
cristy3ed852e2009-09-05 21:47:34 +00003189 Image
3190 *implode_image;
3191
cristy3ed852e2009-09-05 21:47:34 +00003192 MagickBooleanType
3193 status;
3194
cristybb503372010-05-27 20:51:26 +00003195 MagickOffsetType
3196 progress;
3197
cristy3ed852e2009-09-05 21:47:34 +00003198 MagickRealType
3199 radius;
3200
3201 PointInfo
3202 center,
3203 scale;
3204
cristybb503372010-05-27 20:51:26 +00003205 ssize_t
3206 y;
3207
cristy3ed852e2009-09-05 21:47:34 +00003208 /*
3209 Initialize implode image attributes.
3210 */
3211 assert(image != (Image *) NULL);
3212 assert(image->signature == MagickSignature);
3213 if (image->debug != MagickFalse)
3214 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3215 assert(exception != (ExceptionInfo *) NULL);
3216 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00003217 implode_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3218 exception);
cristy3ed852e2009-09-05 21:47:34 +00003219 if (implode_image == (Image *) NULL)
3220 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003221 if (SetImageStorageClass(implode_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003222 {
cristy3ed852e2009-09-05 21:47:34 +00003223 implode_image=DestroyImage(implode_image);
3224 return((Image *) NULL);
3225 }
cristy4c08aed2011-07-01 19:47:50 +00003226 if (implode_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00003227 implode_image->matte=MagickTrue;
3228 /*
3229 Compute scaling factor.
3230 */
3231 scale.x=1.0;
3232 scale.y=1.0;
3233 center.x=0.5*image->columns;
3234 center.y=0.5*image->rows;
3235 radius=center.x;
3236 if (image->columns > image->rows)
3237 scale.y=(double) image->columns/(double) image->rows;
3238 else
3239 if (image->columns < image->rows)
3240 {
3241 scale.x=(double) image->rows/(double) image->columns;
3242 radius=center.y;
3243 }
3244 /*
3245 Implode image.
3246 */
3247 status=MagickTrue;
3248 progress=0;
cristydb070952012-04-20 14:33:00 +00003249 image_view=AcquireVirtualCacheView(image,exception);
3250 implode_view=AcquireAuthenticCacheView(implode_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00003251#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003252 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00003253 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003254#endif
cristybb503372010-05-27 20:51:26 +00003255 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003256 {
cristy3ed852e2009-09-05 21:47:34 +00003257 MagickRealType
3258 distance;
3259
3260 PointInfo
3261 delta;
3262
cristy6d188022011-09-12 13:23:33 +00003263 register const Quantum
3264 *restrict p;
3265
cristybb503372010-05-27 20:51:26 +00003266 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003267 x;
3268
cristy4c08aed2011-07-01 19:47:50 +00003269 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003270 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003271
3272 if (status == MagickFalse)
3273 continue;
cristy6d188022011-09-12 13:23:33 +00003274 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +00003275 q=QueueCacheViewAuthenticPixels(implode_view,0,y,implode_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003276 exception);
cristy6d188022011-09-12 13:23:33 +00003277 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003278 {
3279 status=MagickFalse;
3280 continue;
3281 }
cristy3ed852e2009-09-05 21:47:34 +00003282 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00003283 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003284 {
cristy6d188022011-09-12 13:23:33 +00003285 register ssize_t
3286 i;
3287
cristy3ed852e2009-09-05 21:47:34 +00003288 /*
3289 Determine if the pixel is within an ellipse.
3290 */
cristy10a6c612012-01-29 21:41:05 +00003291 if (GetPixelMask(image,p) != 0)
3292 {
3293 p+=GetPixelChannels(image);
3294 q+=GetPixelChannels(implode_image);
3295 continue;
3296 }
cristy3ed852e2009-09-05 21:47:34 +00003297 delta.x=scale.x*(double) (x-center.x);
3298 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00003299 if (distance >= (radius*radius))
cristya6d7a9b2012-01-18 20:04:48 +00003300 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy6d188022011-09-12 13:23:33 +00003301 {
cristya6d7a9b2012-01-18 20:04:48 +00003302 PixelChannel
3303 channel;
3304
cristy1707c6c2012-01-18 23:30:54 +00003305 PixelTrait
3306 implode_traits,
3307 traits;
3308
cristya6d7a9b2012-01-18 20:04:48 +00003309 channel=GetPixelChannelMapChannel(image,i);
cristy1707c6c2012-01-18 23:30:54 +00003310 traits=GetPixelChannelMapTraits(image,channel);
3311 implode_traits=GetPixelChannelMapTraits(implode_image,channel);
3312 if ((traits == UndefinedPixelTrait) ||
3313 (implode_traits == UndefinedPixelTrait))
3314 continue;
cristya6d7a9b2012-01-18 20:04:48 +00003315 SetPixelChannel(implode_image,channel,p[i],q);
cristy6d188022011-09-12 13:23:33 +00003316 }
3317 else
cristy3ed852e2009-09-05 21:47:34 +00003318 {
3319 double
3320 factor;
3321
3322 /*
3323 Implode the pixel.
3324 */
3325 factor=1.0;
3326 if (distance > 0.0)
cristy1707c6c2012-01-18 23:30:54 +00003327 factor=pow(sin((double) (MagickPI*sqrt((double) distance)/radius/
3328 2)),-amount);
cristy76f512e2011-09-12 01:26:56 +00003329 status=InterpolatePixelChannels(image,image_view,implode_image,method,
3330 (double) (factor*delta.x/scale.x+center.x),(double) (factor*delta.y/
3331 scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00003332 }
cristy6d188022011-09-12 13:23:33 +00003333 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003334 q+=GetPixelChannels(implode_image);
cristy3ed852e2009-09-05 21:47:34 +00003335 }
3336 if (SyncCacheViewAuthenticPixels(implode_view,exception) == MagickFalse)
3337 status=MagickFalse;
3338 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3339 {
3340 MagickBooleanType
3341 proceed;
3342
cristyb5d5f722009-11-04 03:03:49 +00003343#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003344 #pragma omp critical (MagickCore_ImplodeImage)
cristy3ed852e2009-09-05 21:47:34 +00003345#endif
3346 proceed=SetImageProgress(image,ImplodeImageTag,progress++,image->rows);
3347 if (proceed == MagickFalse)
3348 status=MagickFalse;
3349 }
3350 }
3351 implode_view=DestroyCacheView(implode_view);
3352 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003353 if (status == MagickFalse)
3354 implode_image=DestroyImage(implode_image);
3355 return(implode_image);
3356}
3357
3358/*
3359%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3360% %
3361% %
3362% %
3363% M o r p h I m a g e s %
3364% %
3365% %
3366% %
3367%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3368%
3369% The MorphImages() method requires a minimum of two images. The first
3370% image is transformed into the second by a number of intervening images
3371% as specified by frames.
3372%
3373% The format of the MorphImage method is:
3374%
cristybb503372010-05-27 20:51:26 +00003375% Image *MorphImages(const Image *image,const size_t number_frames,
cristy3ed852e2009-09-05 21:47:34 +00003376% ExceptionInfo *exception)
3377%
3378% A description of each parameter follows:
3379%
3380% o image: the image.
3381%
3382% o number_frames: Define the number of in-between image to generate.
3383% The more in-between frames, the smoother the morph.
3384%
3385% o exception: return any errors or warnings in this structure.
3386%
3387*/
3388MagickExport Image *MorphImages(const Image *image,
cristybb503372010-05-27 20:51:26 +00003389 const size_t number_frames,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003390{
3391#define MorphImageTag "Morph/Image"
3392
3393 Image
3394 *morph_image,
3395 *morph_images;
3396
cristy9d314ff2011-03-09 01:30:28 +00003397 MagickBooleanType
3398 status;
cristy3ed852e2009-09-05 21:47:34 +00003399
3400 MagickOffsetType
3401 scene;
3402
3403 MagickRealType
3404 alpha,
3405 beta;
3406
3407 register const Image
3408 *next;
3409
cristybb503372010-05-27 20:51:26 +00003410 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003411 i;
3412
cristy9d314ff2011-03-09 01:30:28 +00003413 ssize_t
3414 y;
cristy3ed852e2009-09-05 21:47:34 +00003415
3416 /*
3417 Clone first frame in sequence.
3418 */
3419 assert(image != (Image *) NULL);
3420 assert(image->signature == MagickSignature);
3421 if (image->debug != MagickFalse)
3422 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3423 assert(exception != (ExceptionInfo *) NULL);
3424 assert(exception->signature == MagickSignature);
3425 morph_images=CloneImage(image,0,0,MagickTrue,exception);
3426 if (morph_images == (Image *) NULL)
3427 return((Image *) NULL);
3428 if (GetNextImageInList(image) == (Image *) NULL)
3429 {
3430 /*
3431 Morph single image.
3432 */
cristybb503372010-05-27 20:51:26 +00003433 for (i=1; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003434 {
3435 morph_image=CloneImage(image,0,0,MagickTrue,exception);
3436 if (morph_image == (Image *) NULL)
3437 {
3438 morph_images=DestroyImageList(morph_images);
3439 return((Image *) NULL);
3440 }
3441 AppendImageToList(&morph_images,morph_image);
cristy8b27a6d2010-02-14 03:31:15 +00003442 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003443 {
cristy8b27a6d2010-02-14 03:31:15 +00003444 MagickBooleanType
3445 proceed;
3446
cristybb503372010-05-27 20:51:26 +00003447 proceed=SetImageProgress(image,MorphImageTag,(MagickOffsetType) i,
3448 number_frames);
cristy8b27a6d2010-02-14 03:31:15 +00003449 if (proceed == MagickFalse)
3450 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00003451 }
3452 }
3453 return(GetFirstImageInList(morph_images));
3454 }
3455 /*
3456 Morph image sequence.
3457 */
3458 status=MagickTrue;
3459 scene=0;
3460 next=image;
3461 for ( ; GetNextImageInList(next) != (Image *) NULL; next=GetNextImageInList(next))
3462 {
cristybb503372010-05-27 20:51:26 +00003463 for (i=0; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003464 {
3465 CacheView
3466 *image_view,
3467 *morph_view;
3468
3469 beta=(MagickRealType) (i+1.0)/(MagickRealType) (number_frames+1.0);
3470 alpha=1.0-beta;
cristy15b98cd2010-09-12 19:42:50 +00003471 morph_image=ResizeImage(next,(size_t) (alpha*next->columns+beta*
cristyaa2c16c2012-03-25 22:21:35 +00003472 GetNextImageInList(next)->columns+0.5),(size_t) (alpha*next->rows+beta*
3473 GetNextImageInList(next)->rows+0.5),next->filter,exception);
cristy3ed852e2009-09-05 21:47:34 +00003474 if (morph_image == (Image *) NULL)
3475 {
3476 morph_images=DestroyImageList(morph_images);
3477 return((Image *) NULL);
3478 }
cristy1707c6c2012-01-18 23:30:54 +00003479 status=SetImageStorageClass(morph_image,DirectClass,exception);
3480 if (status == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003481 {
cristy3ed852e2009-09-05 21:47:34 +00003482 morph_image=DestroyImage(morph_image);
3483 return((Image *) NULL);
3484 }
3485 AppendImageToList(&morph_images,morph_image);
3486 morph_images=GetLastImageInList(morph_images);
cristy15b98cd2010-09-12 19:42:50 +00003487 morph_image=ResizeImage(GetNextImageInList(next),morph_images->columns,
cristyaa2c16c2012-03-25 22:21:35 +00003488 morph_images->rows,GetNextImageInList(next)->filter,exception);
cristy3ed852e2009-09-05 21:47:34 +00003489 if (morph_image == (Image *) NULL)
3490 {
3491 morph_images=DestroyImageList(morph_images);
3492 return((Image *) NULL);
3493 }
cristydb070952012-04-20 14:33:00 +00003494 image_view=AcquireVirtualCacheView(morph_image,exception);
3495 morph_view=AcquireAuthenticCacheView(morph_images,exception);
cristyb5d5f722009-11-04 03:03:49 +00003496#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003497 #pragma omp parallel for schedule(static,4) shared(status) \
cristy4ee2b0c2012-05-15 00:30:35 +00003498 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003499#endif
cristybb503372010-05-27 20:51:26 +00003500 for (y=0; y < (ssize_t) morph_images->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003501 {
3502 MagickBooleanType
3503 sync;
3504
cristy4c08aed2011-07-01 19:47:50 +00003505 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003506 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003507
cristybb503372010-05-27 20:51:26 +00003508 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003509 x;
3510
cristy4c08aed2011-07-01 19:47:50 +00003511 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003512 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003513
3514 if (status == MagickFalse)
3515 continue;
3516 p=GetCacheViewVirtualPixels(image_view,0,y,morph_image->columns,1,
3517 exception);
3518 q=GetCacheViewAuthenticPixels(morph_view,0,y,morph_images->columns,1,
3519 exception);
cristy4c08aed2011-07-01 19:47:50 +00003520 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003521 {
3522 status=MagickFalse;
3523 continue;
3524 }
cristybb503372010-05-27 20:51:26 +00003525 for (x=0; x < (ssize_t) morph_images->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003526 {
cristy1707c6c2012-01-18 23:30:54 +00003527 register ssize_t
3528 i;
3529
cristy177e41c2012-04-15 15:08:25 +00003530 if (GetPixelMask(image,p) != 0)
3531 {
3532 p+=GetPixelChannels(image);
3533 q+=GetPixelChannels(morph_image);
3534 continue;
3535 }
cristy10a6c612012-01-29 21:41:05 +00003536 for (i=0; i < (ssize_t) GetPixelChannels(morph_image); i++)
cristy1707c6c2012-01-18 23:30:54 +00003537 {
3538 PixelChannel
3539 channel;
3540
3541 PixelTrait
3542 morph_traits,
3543 traits;
3544
3545 channel=GetPixelChannelMapChannel(image,i);
3546 traits=GetPixelChannelMapTraits(image,channel);
3547 morph_traits=GetPixelChannelMapTraits(morph_image,channel);
3548 if ((traits == UndefinedPixelTrait) ||
3549 (morph_traits == UndefinedPixelTrait))
3550 continue;
cristy177e41c2012-04-15 15:08:25 +00003551 if ((morph_traits & CopyPixelTrait) != 0)
cristy1707c6c2012-01-18 23:30:54 +00003552 {
3553 SetPixelChannel(morph_image,channel,p[i],q);
3554 continue;
3555 }
3556 SetPixelChannel(morph_image,channel,ClampToQuantum(alpha*
3557 GetPixelChannel(morph_images,channel,q)+beta*p[i]),q);
3558 }
cristyed231572011-07-14 02:18:59 +00003559 p+=GetPixelChannels(morph_image);
3560 q+=GetPixelChannels(morph_images);
cristy3ed852e2009-09-05 21:47:34 +00003561 }
3562 sync=SyncCacheViewAuthenticPixels(morph_view,exception);
3563 if (sync == MagickFalse)
3564 status=MagickFalse;
3565 }
3566 morph_view=DestroyCacheView(morph_view);
3567 image_view=DestroyCacheView(image_view);
3568 morph_image=DestroyImage(morph_image);
3569 }
cristybb503372010-05-27 20:51:26 +00003570 if (i < (ssize_t) number_frames)
cristy3ed852e2009-09-05 21:47:34 +00003571 break;
3572 /*
3573 Clone last frame in sequence.
3574 */
3575 morph_image=CloneImage(GetNextImageInList(next),0,0,MagickTrue,exception);
3576 if (morph_image == (Image *) NULL)
3577 {
3578 morph_images=DestroyImageList(morph_images);
3579 return((Image *) NULL);
3580 }
3581 AppendImageToList(&morph_images,morph_image);
3582 morph_images=GetLastImageInList(morph_images);
3583 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3584 {
3585 MagickBooleanType
3586 proceed;
3587
cristyb5d5f722009-11-04 03:03:49 +00003588#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003589 #pragma omp critical (MagickCore_MorphImages)
cristy3ed852e2009-09-05 21:47:34 +00003590#endif
3591 proceed=SetImageProgress(image,MorphImageTag,scene,
3592 GetImageListLength(image));
3593 if (proceed == MagickFalse)
3594 status=MagickFalse;
3595 }
3596 scene++;
3597 }
3598 if (GetNextImageInList(next) != (Image *) NULL)
3599 {
3600 morph_images=DestroyImageList(morph_images);
3601 return((Image *) NULL);
3602 }
3603 return(GetFirstImageInList(morph_images));
3604}
3605
3606/*
3607%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3608% %
3609% %
3610% %
3611% P l a s m a I m a g e %
3612% %
3613% %
3614% %
3615%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3616%
3617% PlasmaImage() initializes an image with plasma fractal values. The image
3618% must be initialized with a base color and the random number generator
3619% seeded before this method is called.
3620%
3621% The format of the PlasmaImage method is:
3622%
3623% MagickBooleanType PlasmaImage(Image *image,const SegmentInfo *segment,
cristy5cbc0162011-08-29 00:36:28 +00003624% size_t attenuate,size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003625%
3626% A description of each parameter follows:
3627%
3628% o image: the image.
3629%
3630% o segment: Define the region to apply plasma fractals values.
3631%
glennrp7dae1ca2010-09-16 12:17:35 +00003632% o attenuate: Define the plasma attenuation factor.
cristy3ed852e2009-09-05 21:47:34 +00003633%
3634% o depth: Limit the plasma recursion depth.
3635%
cristy5cbc0162011-08-29 00:36:28 +00003636% o exception: return any errors or warnings in this structure.
3637%
cristy3ed852e2009-09-05 21:47:34 +00003638*/
3639
3640static inline Quantum PlasmaPixel(RandomInfo *random_info,
3641 const MagickRealType pixel,const MagickRealType noise)
3642{
3643 Quantum
3644 plasma;
3645
cristyce70c172010-01-07 17:15:30 +00003646 plasma=ClampToQuantum(pixel+noise*GetPseudoRandomValue(random_info)-
cristy3ed852e2009-09-05 21:47:34 +00003647 noise/2.0);
3648 return(plasma);
3649}
3650
cristyda1f9c12011-10-02 21:39:49 +00003651static MagickBooleanType PlasmaImageProxy(Image *image,CacheView *image_view,
3652 CacheView *u_view,CacheView *v_view,RandomInfo *random_info,
3653 const SegmentInfo *segment,size_t attenuate,size_t depth,
3654 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003655{
cristy3ed852e2009-09-05 21:47:34 +00003656 MagickRealType
3657 plasma;
3658
cristyda1f9c12011-10-02 21:39:49 +00003659 PixelChannel
3660 channel;
3661
3662 PixelTrait
3663 traits;
3664
3665 register const Quantum
3666 *restrict u,
3667 *restrict v;
3668
3669 register Quantum
3670 *restrict q;
3671
3672 register ssize_t
3673 i;
cristy3ed852e2009-09-05 21:47:34 +00003674
cristy9d314ff2011-03-09 01:30:28 +00003675 ssize_t
3676 x,
3677 x_mid,
3678 y,
3679 y_mid;
3680
cristy3ed852e2009-09-05 21:47:34 +00003681 if (((segment->x2-segment->x1) == 0.0) && ((segment->y2-segment->y1) == 0.0))
3682 return(MagickTrue);
3683 if (depth != 0)
3684 {
3685 SegmentInfo
3686 local_info;
3687
3688 /*
3689 Divide the area into quadrants and recurse.
3690 */
3691 depth--;
3692 attenuate++;
cristybb503372010-05-27 20:51:26 +00003693 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3694 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003695 local_info=(*segment);
3696 local_info.x2=(double) x_mid;
3697 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003698 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3699 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003700 local_info=(*segment);
3701 local_info.y1=(double) y_mid;
3702 local_info.x2=(double) x_mid;
cristyda1f9c12011-10-02 21:39:49 +00003703 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3704 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003705 local_info=(*segment);
3706 local_info.x1=(double) x_mid;
3707 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003708 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3709 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003710 local_info=(*segment);
3711 local_info.x1=(double) x_mid;
3712 local_info.y1=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003713 return(PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3714 &local_info,attenuate,depth,exception));
cristy3ed852e2009-09-05 21:47:34 +00003715 }
cristybb503372010-05-27 20:51:26 +00003716 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3717 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003718 if ((segment->x1 == (double) x_mid) && (segment->x2 == (double) x_mid) &&
3719 (segment->y1 == (double) y_mid) && (segment->y2 == (double) y_mid))
3720 return(MagickFalse);
3721 /*
3722 Average pixels and apply plasma.
3723 */
cristy3ed852e2009-09-05 21:47:34 +00003724 plasma=(MagickRealType) QuantumRange/(2.0*attenuate);
3725 if ((segment->x1 != (double) x_mid) || (segment->x2 != (double) x_mid))
3726 {
cristy3ed852e2009-09-05 21:47:34 +00003727 /*
3728 Left pixel.
3729 */
cristybb503372010-05-27 20:51:26 +00003730 x=(ssize_t) ceil(segment->x1-0.5);
cristy1707c6c2012-01-18 23:30:54 +00003731 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),1,1,
3732 exception);
3733 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),1,1,
3734 exception);
cristyc5c6f662010-09-22 14:23:02 +00003735 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003736 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3737 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003738 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003739 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3740 {
cristye2a912b2011-12-05 20:02:07 +00003741 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003742 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003743 if (traits == UndefinedPixelTrait)
3744 continue;
3745 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3746 }
cristyc5c6f662010-09-22 14:23:02 +00003747 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003748 if (segment->x1 != segment->x2)
3749 {
3750 /*
3751 Right pixel.
3752 */
cristybb503372010-05-27 20:51:26 +00003753 x=(ssize_t) ceil(segment->x2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003754 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3755 1,1,exception);
3756 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3757 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003758 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003759 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3760 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003761 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003762 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3763 {
cristye2a912b2011-12-05 20:02:07 +00003764 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003765 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003766 if (traits == UndefinedPixelTrait)
3767 continue;
3768 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3769 }
cristyc5c6f662010-09-22 14:23:02 +00003770 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003771 }
3772 }
3773 if ((segment->y1 != (double) y_mid) || (segment->y2 != (double) y_mid))
3774 {
3775 if ((segment->x1 != (double) x_mid) || (segment->y2 != (double) y_mid))
3776 {
cristy3ed852e2009-09-05 21:47:34 +00003777 /*
3778 Bottom pixel.
3779 */
cristybb503372010-05-27 20:51:26 +00003780 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003781 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3782 1,1,exception);
3783 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3784 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003785 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003786 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3787 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003788 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003789 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3790 {
cristye2a912b2011-12-05 20:02:07 +00003791 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003792 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003793 if (traits == UndefinedPixelTrait)
3794 continue;
3795 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3796 }
cristyc5c6f662010-09-22 14:23:02 +00003797 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003798 }
3799 if (segment->y1 != segment->y2)
3800 {
cristy3ed852e2009-09-05 21:47:34 +00003801 /*
3802 Top pixel.
3803 */
cristybb503372010-05-27 20:51:26 +00003804 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003805 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3806 1,1,exception);
3807 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3808 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003809 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003810 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3811 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003812 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003813 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3814 {
cristye2a912b2011-12-05 20:02:07 +00003815 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003816 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003817 if (traits == UndefinedPixelTrait)
3818 continue;
3819 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3820 }
cristyc5c6f662010-09-22 14:23:02 +00003821 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003822 }
3823 }
3824 if ((segment->x1 != segment->x2) || (segment->y1 != segment->y2))
3825 {
cristy3ed852e2009-09-05 21:47:34 +00003826 /*
3827 Middle pixel.
3828 */
cristybb503372010-05-27 20:51:26 +00003829 x=(ssize_t) ceil(segment->x1-0.5);
3830 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003831 u=GetCacheViewVirtualPixels(u_view,x,y,1,1,exception);
cristybb503372010-05-27 20:51:26 +00003832 x=(ssize_t) ceil(segment->x2-0.5);
3833 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003834 v=GetCacheViewVirtualPixels(v_view,x,y,1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003835 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003836 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3837 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003838 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003839 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3840 {
cristye2a912b2011-12-05 20:02:07 +00003841 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003842 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003843 if (traits == UndefinedPixelTrait)
3844 continue;
3845 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3846 }
cristyc5c6f662010-09-22 14:23:02 +00003847 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003848 }
3849 if (((segment->x2-segment->x1) < 3.0) && ((segment->y2-segment->y1) < 3.0))
3850 return(MagickTrue);
3851 return(MagickFalse);
3852}
cristyda1f9c12011-10-02 21:39:49 +00003853
cristy3ed852e2009-09-05 21:47:34 +00003854MagickExport MagickBooleanType PlasmaImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00003855 const SegmentInfo *segment,size_t attenuate,size_t depth,
3856 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003857{
cristyc5c6f662010-09-22 14:23:02 +00003858 CacheView
cristyda1f9c12011-10-02 21:39:49 +00003859 *image_view,
3860 *u_view,
3861 *v_view;
cristyc5c6f662010-09-22 14:23:02 +00003862
cristy3ed852e2009-09-05 21:47:34 +00003863 MagickBooleanType
3864 status;
3865
3866 RandomInfo
3867 *random_info;
3868
3869 if (image->debug != MagickFalse)
3870 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3871 assert(image != (Image *) NULL);
3872 assert(image->signature == MagickSignature);
3873 if (image->debug != MagickFalse)
3874 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
cristy5cbc0162011-08-29 00:36:28 +00003875 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristyc5c6f662010-09-22 14:23:02 +00003876 return(MagickFalse);
cristydb070952012-04-20 14:33:00 +00003877 image_view=AcquireAuthenticCacheView(image,exception);
3878 u_view=AcquireVirtualCacheView(image,exception);
3879 v_view=AcquireVirtualCacheView(image,exception);
cristy3ed852e2009-09-05 21:47:34 +00003880 random_info=AcquireRandomInfo();
cristyda1f9c12011-10-02 21:39:49 +00003881 status=PlasmaImageProxy(image,image_view,u_view,v_view,random_info,segment,
3882 attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003883 random_info=DestroyRandomInfo(random_info);
cristyda1f9c12011-10-02 21:39:49 +00003884 v_view=DestroyCacheView(v_view);
3885 u_view=DestroyCacheView(u_view);
cristyc5c6f662010-09-22 14:23:02 +00003886 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003887 return(status);
3888}
3889
3890/*
3891%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3892% %
3893% %
3894% %
3895% P o l a r o i d I m a g e %
3896% %
3897% %
3898% %
3899%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3900%
3901% PolaroidImage() simulates a Polaroid picture.
3902%
3903% The format of the AnnotateImage method is:
3904%
3905% Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristye9e3d382011-12-14 01:50:13 +00003906% const char *caption,const double angle,
3907% const PixelInterpolateMethod method,ExceptionInfo exception)
cristy3ed852e2009-09-05 21:47:34 +00003908%
3909% A description of each parameter follows:
3910%
3911% o image: the image.
3912%
3913% o draw_info: the draw info.
3914%
cristye9e3d382011-12-14 01:50:13 +00003915% o caption: the Polaroid caption.
3916%
cristycee97112010-05-28 00:44:52 +00003917% o angle: Apply the effect along this angle.
cristy3ed852e2009-09-05 21:47:34 +00003918%
cristy5c4e2582011-09-11 19:21:03 +00003919% o method: the pixel interpolation method.
3920%
cristy3ed852e2009-09-05 21:47:34 +00003921% o exception: return any errors or warnings in this structure.
3922%
3923*/
3924MagickExport Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristye9e3d382011-12-14 01:50:13 +00003925 const char *caption,const double angle,const PixelInterpolateMethod method,
cristy5c4e2582011-09-11 19:21:03 +00003926 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003927{
cristy3ed852e2009-09-05 21:47:34 +00003928 Image
3929 *bend_image,
3930 *caption_image,
3931 *flop_image,
3932 *picture_image,
3933 *polaroid_image,
3934 *rotate_image,
3935 *trim_image;
3936
cristybb503372010-05-27 20:51:26 +00003937 size_t
cristy3ed852e2009-09-05 21:47:34 +00003938 height;
3939
cristy9d314ff2011-03-09 01:30:28 +00003940 ssize_t
3941 quantum;
3942
cristy3ed852e2009-09-05 21:47:34 +00003943 /*
3944 Simulate a Polaroid picture.
3945 */
3946 assert(image != (Image *) NULL);
3947 assert(image->signature == MagickSignature);
3948 if (image->debug != MagickFalse)
3949 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3950 assert(exception != (ExceptionInfo *) NULL);
3951 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00003952 quantum=(ssize_t) MagickMax(MagickMax((double) image->columns,(double)
cristy3ed852e2009-09-05 21:47:34 +00003953 image->rows)/25.0,10.0);
3954 height=image->rows+2*quantum;
3955 caption_image=(Image *) NULL;
cristye9e3d382011-12-14 01:50:13 +00003956 if (caption != (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003957 {
3958 char
cristye9e3d382011-12-14 01:50:13 +00003959 geometry[MaxTextExtent],
3960 *text;
cristy3ed852e2009-09-05 21:47:34 +00003961
3962 DrawInfo
3963 *annotate_info;
3964
cristy3ed852e2009-09-05 21:47:34 +00003965 MagickBooleanType
3966 status;
3967
cristy9d314ff2011-03-09 01:30:28 +00003968 ssize_t
3969 count;
3970
cristy3ed852e2009-09-05 21:47:34 +00003971 TypeMetric
3972 metrics;
3973
3974 /*
3975 Generate caption image.
3976 */
3977 caption_image=CloneImage(image,image->columns,1,MagickTrue,exception);
3978 if (caption_image == (Image *) NULL)
3979 return((Image *) NULL);
3980 annotate_info=CloneDrawInfo((const ImageInfo *) NULL,draw_info);
cristye9e3d382011-12-14 01:50:13 +00003981 text=InterpretImageProperties((ImageInfo *) NULL,(Image *) image,caption,
3982 exception);
3983 (void) CloneString(&annotate_info->text,text);
cristy6b1d05e2010-09-22 19:17:27 +00003984 count=FormatMagickCaption(caption_image,annotate_info,MagickTrue,&metrics,
cristye9e3d382011-12-14 01:50:13 +00003985 &text,exception);
3986 status=SetImageExtent(caption_image,image->columns,(size_t) ((count+1)*
3987 (metrics.ascent-metrics.descent)+0.5),exception);
cristy3ed852e2009-09-05 21:47:34 +00003988 if (status == MagickFalse)
3989 caption_image=DestroyImage(caption_image);
3990 else
3991 {
3992 caption_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00003993 (void) SetImageBackgroundColor(caption_image,exception);
cristye9e3d382011-12-14 01:50:13 +00003994 (void) CloneString(&annotate_info->text,text);
cristyb51dff52011-05-19 16:55:47 +00003995 (void) FormatLocaleString(geometry,MaxTextExtent,"+0+%g",
cristy3ed852e2009-09-05 21:47:34 +00003996 metrics.ascent);
3997 if (annotate_info->gravity == UndefinedGravity)
3998 (void) CloneString(&annotate_info->geometry,AcquireString(
3999 geometry));
cristy5cbc0162011-08-29 00:36:28 +00004000 (void) AnnotateImage(caption_image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00004001 height+=caption_image->rows;
4002 }
4003 annotate_info=DestroyDrawInfo(annotate_info);
cristye9e3d382011-12-14 01:50:13 +00004004 text=DestroyString(text);
cristy3ed852e2009-09-05 21:47:34 +00004005 }
4006 picture_image=CloneImage(image,image->columns+2*quantum,height,MagickTrue,
4007 exception);
4008 if (picture_image == (Image *) NULL)
4009 {
4010 if (caption_image != (Image *) NULL)
4011 caption_image=DestroyImage(caption_image);
4012 return((Image *) NULL);
4013 }
4014 picture_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00004015 (void) SetImageBackgroundColor(picture_image,exception);
cristy39172402012-03-30 13:04:39 +00004016 (void) CompositeImage(picture_image,image,OverCompositeOp,MagickTrue,quantum,
cristyfeb3e962012-03-29 17:25:55 +00004017 quantum,exception);
cristy3ed852e2009-09-05 21:47:34 +00004018 if (caption_image != (Image *) NULL)
4019 {
cristyfeb3e962012-03-29 17:25:55 +00004020 (void) CompositeImage(picture_image,caption_image,OverCompositeOp,
cristy39172402012-03-30 13:04:39 +00004021 MagickTrue,quantum,(ssize_t) (image->rows+3*quantum/2),exception);
cristy3ed852e2009-09-05 21:47:34 +00004022 caption_image=DestroyImage(caption_image);
4023 }
cristy9950d572011-10-01 18:22:35 +00004024 (void) QueryColorCompliance("none",AllCompliance,
4025 &picture_image->background_color,exception);
cristy63240882011-08-05 19:05:27 +00004026 (void) SetImageAlphaChannel(picture_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004027 rotate_image=RotateImage(picture_image,90.0,exception);
4028 picture_image=DestroyImage(picture_image);
4029 if (rotate_image == (Image *) NULL)
4030 return((Image *) NULL);
4031 picture_image=rotate_image;
4032 bend_image=WaveImage(picture_image,0.01*picture_image->rows,2.0*
cristy5c4e2582011-09-11 19:21:03 +00004033 picture_image->columns,method,exception);
cristy3ed852e2009-09-05 21:47:34 +00004034 picture_image=DestroyImage(picture_image);
4035 if (bend_image == (Image *) NULL)
4036 return((Image *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00004037 picture_image=bend_image;
4038 rotate_image=RotateImage(picture_image,-90.0,exception);
4039 picture_image=DestroyImage(picture_image);
4040 if (rotate_image == (Image *) NULL)
4041 return((Image *) NULL);
4042 picture_image=rotate_image;
4043 picture_image->background_color=image->background_color;
anthonyf46d4262012-03-26 03:30:34 +00004044 polaroid_image=ShadowImage(picture_image,80.0,2.0,quantum/3,quantum/3,
cristy3ed852e2009-09-05 21:47:34 +00004045 exception);
4046 if (polaroid_image == (Image *) NULL)
4047 {
4048 picture_image=DestroyImage(picture_image);
4049 return(picture_image);
4050 }
4051 flop_image=FlopImage(polaroid_image,exception);
4052 polaroid_image=DestroyImage(polaroid_image);
4053 if (flop_image == (Image *) NULL)
4054 {
4055 picture_image=DestroyImage(picture_image);
4056 return(picture_image);
4057 }
4058 polaroid_image=flop_image;
cristyfeb3e962012-03-29 17:25:55 +00004059 (void) CompositeImage(polaroid_image,picture_image,OverCompositeOp,
cristy39172402012-03-30 13:04:39 +00004060 MagickTrue,(ssize_t) (-0.01*picture_image->columns/2.0),0L,exception);
cristy3ed852e2009-09-05 21:47:34 +00004061 picture_image=DestroyImage(picture_image);
cristy9950d572011-10-01 18:22:35 +00004062 (void) QueryColorCompliance("none",AllCompliance,
4063 &polaroid_image->background_color,exception);
cristy3ed852e2009-09-05 21:47:34 +00004064 rotate_image=RotateImage(polaroid_image,angle,exception);
4065 polaroid_image=DestroyImage(polaroid_image);
4066 if (rotate_image == (Image *) NULL)
4067 return((Image *) NULL);
4068 polaroid_image=rotate_image;
4069 trim_image=TrimImage(polaroid_image,exception);
4070 polaroid_image=DestroyImage(polaroid_image);
4071 if (trim_image == (Image *) NULL)
4072 return((Image *) NULL);
4073 polaroid_image=trim_image;
4074 return(polaroid_image);
4075}
4076
4077/*
4078%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4079% %
4080% %
4081% %
cristy3ed852e2009-09-05 21:47:34 +00004082% S e p i a T o n e I m a g e %
4083% %
4084% %
4085% %
4086%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4087%
4088% MagickSepiaToneImage() applies a special effect to the image, similar to the
4089% effect achieved in a photo darkroom by sepia toning. Threshold ranges from
4090% 0 to QuantumRange and is a measure of the extent of the sepia toning. A
4091% threshold of 80% is a good starting point for a reasonable tone.
4092%
4093% The format of the SepiaToneImage method is:
4094%
4095% Image *SepiaToneImage(const Image *image,const double threshold,
4096% ExceptionInfo *exception)
4097%
4098% A description of each parameter follows:
4099%
4100% o image: the image.
4101%
4102% o threshold: the tone threshold.
4103%
4104% o exception: return any errors or warnings in this structure.
4105%
4106*/
4107MagickExport Image *SepiaToneImage(const Image *image,const double threshold,
4108 ExceptionInfo *exception)
4109{
4110#define SepiaToneImageTag "SepiaTone/Image"
4111
cristyc4c8d132010-01-07 01:58:38 +00004112 CacheView
4113 *image_view,
4114 *sepia_view;
4115
cristy3ed852e2009-09-05 21:47:34 +00004116 Image
4117 *sepia_image;
4118
cristy3ed852e2009-09-05 21:47:34 +00004119 MagickBooleanType
4120 status;
4121
cristybb503372010-05-27 20:51:26 +00004122 MagickOffsetType
4123 progress;
4124
4125 ssize_t
4126 y;
4127
cristy3ed852e2009-09-05 21:47:34 +00004128 /*
4129 Initialize sepia-toned image attributes.
4130 */
4131 assert(image != (const Image *) NULL);
4132 assert(image->signature == MagickSignature);
4133 if (image->debug != MagickFalse)
4134 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4135 assert(exception != (ExceptionInfo *) NULL);
4136 assert(exception->signature == MagickSignature);
cristy1707c6c2012-01-18 23:30:54 +00004137 sepia_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004138 if (sepia_image == (Image *) NULL)
4139 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004140 if (SetImageStorageClass(sepia_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004141 {
cristy3ed852e2009-09-05 21:47:34 +00004142 sepia_image=DestroyImage(sepia_image);
4143 return((Image *) NULL);
4144 }
4145 /*
4146 Tone each row of the image.
4147 */
4148 status=MagickTrue;
4149 progress=0;
cristydb070952012-04-20 14:33:00 +00004150 image_view=AcquireVirtualCacheView(image,exception);
4151 sepia_view=AcquireAuthenticCacheView(sepia_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00004152#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00004153 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00004154 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00004155#endif
cristybb503372010-05-27 20:51:26 +00004156 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004157 {
cristy4c08aed2011-07-01 19:47:50 +00004158 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004159 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00004160
cristybb503372010-05-27 20:51:26 +00004161 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004162 x;
4163
cristy4c08aed2011-07-01 19:47:50 +00004164 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004165 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004166
4167 if (status == MagickFalse)
4168 continue;
4169 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy1707c6c2012-01-18 23:30:54 +00004170 q=GetCacheViewAuthenticPixels(sepia_view,0,y,sepia_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00004171 exception);
cristy4c08aed2011-07-01 19:47:50 +00004172 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004173 {
4174 status=MagickFalse;
4175 continue;
4176 }
cristybb503372010-05-27 20:51:26 +00004177 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004178 {
4179 MagickRealType
4180 intensity,
4181 tone;
4182
cristy4c08aed2011-07-01 19:47:50 +00004183 intensity=(MagickRealType) GetPixelIntensity(image,p);
cristy3ed852e2009-09-05 21:47:34 +00004184 tone=intensity > threshold ? (MagickRealType) QuantumRange : intensity+
4185 (MagickRealType) QuantumRange-threshold;
cristy4c08aed2011-07-01 19:47:50 +00004186 SetPixelRed(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004187 tone=intensity > (7.0*threshold/6.0) ? (MagickRealType) QuantumRange :
4188 intensity+(MagickRealType) QuantumRange-7.0*threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004189 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004190 tone=intensity < (threshold/6.0) ? 0 : intensity-threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004191 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004192 tone=threshold/7.0;
cristy4c08aed2011-07-01 19:47:50 +00004193 if ((MagickRealType) GetPixelGreen(image,q) < tone)
4194 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
4195 if ((MagickRealType) GetPixelBlue(image,q) < tone)
4196 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristyed231572011-07-14 02:18:59 +00004197 p+=GetPixelChannels(image);
4198 q+=GetPixelChannels(sepia_image);
cristy3ed852e2009-09-05 21:47:34 +00004199 }
4200 if (SyncCacheViewAuthenticPixels(sepia_view,exception) == MagickFalse)
4201 status=MagickFalse;
4202 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4203 {
4204 MagickBooleanType
4205 proceed;
4206
cristyb5d5f722009-11-04 03:03:49 +00004207#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00004208 #pragma omp critical (MagickCore_SepiaToneImage)
cristy3ed852e2009-09-05 21:47:34 +00004209#endif
4210 proceed=SetImageProgress(image,SepiaToneImageTag,progress++,
4211 image->rows);
4212 if (proceed == MagickFalse)
4213 status=MagickFalse;
4214 }
4215 }
4216 sepia_view=DestroyCacheView(sepia_view);
4217 image_view=DestroyCacheView(image_view);
cristye23ec9d2011-08-16 18:15:40 +00004218 (void) NormalizeImage(sepia_image,exception);
4219 (void) ContrastImage(sepia_image,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004220 if (status == MagickFalse)
4221 sepia_image=DestroyImage(sepia_image);
4222 return(sepia_image);
4223}
4224
4225/*
4226%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4227% %
4228% %
4229% %
4230% S h a d o w I m a g e %
4231% %
4232% %
4233% %
4234%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4235%
4236% ShadowImage() simulates a shadow from the specified image and returns it.
4237%
4238% The format of the ShadowImage method is:
4239%
cristy70cddf72011-12-10 22:42:42 +00004240% Image *ShadowImage(const Image *image,const double alpha,
cristyaa2c16c2012-03-25 22:21:35 +00004241% const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4242% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004243%
4244% A description of each parameter follows:
4245%
4246% o image: the image.
4247%
cristy70cddf72011-12-10 22:42:42 +00004248% o alpha: percentage transparency.
cristy3ed852e2009-09-05 21:47:34 +00004249%
4250% o sigma: the standard deviation of the Gaussian, in pixels.
4251%
4252% o x_offset: the shadow x-offset.
4253%
4254% o y_offset: the shadow y-offset.
4255%
4256% o exception: return any errors or warnings in this structure.
4257%
4258*/
cristy70cddf72011-12-10 22:42:42 +00004259MagickExport Image *ShadowImage(const Image *image,const double alpha,
cristyaa2c16c2012-03-25 22:21:35 +00004260 const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4261 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004262{
4263#define ShadowImageTag "Shadow/Image"
4264
cristy70cddf72011-12-10 22:42:42 +00004265 CacheView
4266 *image_view;
4267
cristybd5a96c2011-08-21 00:04:26 +00004268 ChannelType
4269 channel_mask;
4270
cristy3ed852e2009-09-05 21:47:34 +00004271 Image
4272 *border_image,
4273 *clone_image,
4274 *shadow_image;
4275
cristy70cddf72011-12-10 22:42:42 +00004276 MagickBooleanType
4277 status;
4278
cristy3ed852e2009-09-05 21:47:34 +00004279 RectangleInfo
4280 border_info;
4281
cristy70cddf72011-12-10 22:42:42 +00004282 ssize_t
4283 y;
4284
cristy3ed852e2009-09-05 21:47:34 +00004285 assert(image != (Image *) NULL);
4286 assert(image->signature == MagickSignature);
4287 if (image->debug != MagickFalse)
4288 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4289 assert(exception != (ExceptionInfo *) NULL);
4290 assert(exception->signature == MagickSignature);
4291 clone_image=CloneImage(image,0,0,MagickTrue,exception);
4292 if (clone_image == (Image *) NULL)
4293 return((Image *) NULL);
cristy0898eba2012-04-09 16:38:29 +00004294 if (IsGrayColorspace(image->colorspace) != MagickFalse)
cristyb09db112012-07-11 12:04:31 +00004295 (void) TransformImageColorspace(clone_image,RGBColorspace,exception);
cristy0ce08762012-06-30 01:33:18 +00004296 (void) SetImageVirtualPixelMethod(clone_image,TransparentVirtualPixelMethod,
cristy387430f2012-02-07 13:09:46 +00004297 exception);
cristybb503372010-05-27 20:51:26 +00004298 border_info.width=(size_t) floor(2.0*sigma+0.5);
4299 border_info.height=(size_t) floor(2.0*sigma+0.5);
cristy3ed852e2009-09-05 21:47:34 +00004300 border_info.x=0;
4301 border_info.y=0;
cristy9950d572011-10-01 18:22:35 +00004302 (void) QueryColorCompliance("none",AllCompliance,&clone_image->border_color,
4303 exception);
cristy70cddf72011-12-10 22:42:42 +00004304 clone_image->matte=MagickTrue;
4305 border_image=BorderImage(clone_image,&border_info,OverCompositeOp,exception);
cristy3ed852e2009-09-05 21:47:34 +00004306 clone_image=DestroyImage(clone_image);
4307 if (border_image == (Image *) NULL)
4308 return((Image *) NULL);
4309 if (border_image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00004310 (void) SetImageAlphaChannel(border_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004311 /*
4312 Shadow image.
4313 */
cristy70cddf72011-12-10 22:42:42 +00004314 status=MagickTrue;
cristydb070952012-04-20 14:33:00 +00004315 image_view=AcquireAuthenticCacheView(border_image,exception);
cristy70cddf72011-12-10 22:42:42 +00004316 for (y=0; y < (ssize_t) border_image->rows; y++)
4317 {
4318 PixelInfo
4319 background_color;
4320
4321 register Quantum
4322 *restrict q;
4323
4324 register ssize_t
4325 x;
4326
4327 if (status == MagickFalse)
4328 continue;
4329 q=QueueCacheViewAuthenticPixels(image_view,0,y,border_image->columns,1,
4330 exception);
4331 if (q == (Quantum *) NULL)
4332 {
4333 status=MagickFalse;
4334 continue;
4335 }
4336 background_color=border_image->background_color;
4337 background_color.matte=MagickTrue;
4338 for (x=0; x < (ssize_t) border_image->columns; x++)
4339 {
4340 if (border_image->matte != MagickFalse)
4341 background_color.alpha=GetPixelAlpha(border_image,q)*alpha/100.0;
4342 SetPixelInfoPixel(border_image,&background_color,q);
4343 q+=GetPixelChannels(border_image);
4344 }
4345 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4346 status=MagickFalse;
4347 }
4348 image_view=DestroyCacheView(image_view);
4349 if (status == MagickFalse)
4350 {
4351 border_image=DestroyImage(border_image);
4352 return((Image *) NULL);
4353 }
cristybd5a96c2011-08-21 00:04:26 +00004354 channel_mask=SetPixelChannelMask(border_image,AlphaChannel);
cristyaa2c16c2012-03-25 22:21:35 +00004355 shadow_image=BlurImage(border_image,0.0,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +00004356 border_image=DestroyImage(border_image);
4357 if (shadow_image == (Image *) NULL)
4358 return((Image *) NULL);
cristyae1969f2011-12-10 03:07:36 +00004359 (void) SetPixelChannelMapMask(shadow_image,channel_mask);
cristy3ed852e2009-09-05 21:47:34 +00004360 if (shadow_image->page.width == 0)
4361 shadow_image->page.width=shadow_image->columns;
4362 if (shadow_image->page.height == 0)
4363 shadow_image->page.height=shadow_image->rows;
cristybb503372010-05-27 20:51:26 +00004364 shadow_image->page.width+=x_offset-(ssize_t) border_info.width;
4365 shadow_image->page.height+=y_offset-(ssize_t) border_info.height;
4366 shadow_image->page.x+=x_offset-(ssize_t) border_info.width;
4367 shadow_image->page.y+=y_offset-(ssize_t) border_info.height;
cristy3ed852e2009-09-05 21:47:34 +00004368 return(shadow_image);
4369}
4370
4371/*
4372%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4373% %
4374% %
4375% %
4376% S k e t c h I m a g e %
4377% %
4378% %
4379% %
4380%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4381%
4382% SketchImage() simulates a pencil sketch. We convolve the image with a
4383% Gaussian operator of the given radius and standard deviation (sigma). For
4384% reasonable results, radius should be larger than sigma. Use a radius of 0
4385% and SketchImage() selects a suitable radius for you. Angle gives the angle
4386% of the sketch.
4387%
4388% The format of the SketchImage method is:
4389%
4390% Image *SketchImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00004391% const double sigma,const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004392%
4393% A description of each parameter follows:
4394%
4395% o image: the image.
4396%
cristy574cc262011-08-05 01:23:58 +00004397% o radius: the radius of the Gaussian, in pixels, not counting the
4398% center pixel.
cristy3ed852e2009-09-05 21:47:34 +00004399%
4400% o sigma: the standard deviation of the Gaussian, in pixels.
4401%
cristyf7ef0252011-09-09 14:50:06 +00004402% o angle: apply the effect along this angle.
4403%
cristy3ed852e2009-09-05 21:47:34 +00004404% o exception: return any errors or warnings in this structure.
4405%
4406*/
4407MagickExport Image *SketchImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00004408 const double sigma,const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004409{
cristyfa112112010-01-04 17:48:07 +00004410 CacheView
4411 *random_view;
4412
cristy3ed852e2009-09-05 21:47:34 +00004413 Image
4414 *blend_image,
4415 *blur_image,
4416 *dodge_image,
4417 *random_image,
4418 *sketch_image;
4419
cristy3ed852e2009-09-05 21:47:34 +00004420 MagickBooleanType
4421 status;
4422
cristy3ed852e2009-09-05 21:47:34 +00004423 RandomInfo
cristyfa112112010-01-04 17:48:07 +00004424 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00004425
cristy9d314ff2011-03-09 01:30:28 +00004426 ssize_t
4427 y;
4428
cristy57340e02012-05-05 00:53:23 +00004429 unsigned long
4430 key;
4431
cristy3ed852e2009-09-05 21:47:34 +00004432 /*
4433 Sketch image.
4434 */
4435 random_image=CloneImage(image,image->columns << 1,image->rows << 1,
4436 MagickTrue,exception);
4437 if (random_image == (Image *) NULL)
4438 return((Image *) NULL);
4439 status=MagickTrue;
cristy1b784432009-12-19 02:20:40 +00004440 random_info=AcquireRandomInfoThreadSet();
cristy57340e02012-05-05 00:53:23 +00004441 key=GetRandomSecretKey(random_info[0]);
cristydb070952012-04-20 14:33:00 +00004442 random_view=AcquireAuthenticCacheView(random_image,exception);
cristy1b784432009-12-19 02:20:40 +00004443#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy57340e02012-05-05 00:53:23 +00004444 #pragma omp parallel for schedule(static,4) shared(status) \
cristy4ee2b0c2012-05-15 00:30:35 +00004445 dynamic_number_threads(image,image->columns,image->rows,key == ~0UL)
cristy1b784432009-12-19 02:20:40 +00004446#endif
cristybb503372010-05-27 20:51:26 +00004447 for (y=0; y < (ssize_t) random_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004448 {
cristy5c9e6f22010-09-17 17:31:01 +00004449 const int
4450 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00004451
cristybb503372010-05-27 20:51:26 +00004452 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004453 x;
4454
cristy4c08aed2011-07-01 19:47:50 +00004455 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004456 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004457
cristy1b784432009-12-19 02:20:40 +00004458 if (status == MagickFalse)
4459 continue;
cristy3ed852e2009-09-05 21:47:34 +00004460 q=QueueCacheViewAuthenticPixels(random_view,0,y,random_image->columns,1,
4461 exception);
cristyacd2ed22011-08-30 01:44:23 +00004462 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004463 {
4464 status=MagickFalse;
4465 continue;
4466 }
cristybb503372010-05-27 20:51:26 +00004467 for (x=0; x < (ssize_t) random_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004468 {
cristy76f512e2011-09-12 01:26:56 +00004469 MagickRealType
4470 value;
4471
4472 register ssize_t
4473 i;
4474
cristy10a6c612012-01-29 21:41:05 +00004475 if (GetPixelMask(random_image,q) != 0)
4476 {
4477 q+=GetPixelChannels(random_image);
4478 continue;
4479 }
cristy76f512e2011-09-12 01:26:56 +00004480 value=GetPseudoRandomValue(random_info[id]);
4481 for (i=0; i < (ssize_t) GetPixelChannels(random_image); i++)
4482 {
cristyabace412011-12-11 15:56:53 +00004483 PixelChannel
4484 channel;
4485
cristy76f512e2011-09-12 01:26:56 +00004486 PixelTrait
4487 traits;
4488
cristyabace412011-12-11 15:56:53 +00004489 channel=GetPixelChannelMapChannel(image,i);
4490 traits=GetPixelChannelMapTraits(image,channel);
cristy76f512e2011-09-12 01:26:56 +00004491 if (traits == UndefinedPixelTrait)
4492 continue;
4493 q[i]=ClampToQuantum(QuantumRange*value);
4494 }
cristyed231572011-07-14 02:18:59 +00004495 q+=GetPixelChannels(random_image);
cristy3ed852e2009-09-05 21:47:34 +00004496 }
4497 if (SyncCacheViewAuthenticPixels(random_view,exception) == MagickFalse)
4498 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004499 }
4500 random_view=DestroyCacheView(random_view);
cristy1b784432009-12-19 02:20:40 +00004501 random_info=DestroyRandomInfoThreadSet(random_info);
cristy3ed852e2009-09-05 21:47:34 +00004502 if (status == MagickFalse)
4503 {
4504 random_image=DestroyImage(random_image);
4505 return(random_image);
4506 }
cristyaa2c16c2012-03-25 22:21:35 +00004507 blur_image=MotionBlurImage(random_image,radius,sigma,angle,exception);
cristy3ed852e2009-09-05 21:47:34 +00004508 random_image=DestroyImage(random_image);
4509 if (blur_image == (Image *) NULL)
4510 return((Image *) NULL);
cristy6bfd6902011-12-09 01:33:45 +00004511 dodge_image=EdgeImage(blur_image,radius,1.0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004512 blur_image=DestroyImage(blur_image);
4513 if (dodge_image == (Image *) NULL)
4514 return((Image *) NULL);
cristye23ec9d2011-08-16 18:15:40 +00004515 (void) NormalizeImage(dodge_image,exception);
cristyb3e7c6c2011-07-24 01:43:55 +00004516 (void) NegateImage(dodge_image,MagickFalse,exception);
cristye941a752011-10-15 01:52:48 +00004517 (void) TransformImage(&dodge_image,(char *) NULL,"50%",exception);
cristy3ed852e2009-09-05 21:47:34 +00004518 sketch_image=CloneImage(image,0,0,MagickTrue,exception);
4519 if (sketch_image == (Image *) NULL)
4520 {
4521 dodge_image=DestroyImage(dodge_image);
4522 return((Image *) NULL);
4523 }
cristyfeb3e962012-03-29 17:25:55 +00004524 (void) CompositeImage(sketch_image,dodge_image,ColorDodgeCompositeOp,
cristy39172402012-03-30 13:04:39 +00004525 MagickTrue,0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004526 dodge_image=DestroyImage(dodge_image);
4527 blend_image=CloneImage(image,0,0,MagickTrue,exception);
4528 if (blend_image == (Image *) NULL)
4529 {
4530 sketch_image=DestroyImage(sketch_image);
4531 return((Image *) NULL);
4532 }
4533 (void) SetImageArtifact(blend_image,"compose:args","20x80");
cristy39172402012-03-30 13:04:39 +00004534 (void) CompositeImage(sketch_image,blend_image,BlendCompositeOp,MagickTrue,
cristyfeb3e962012-03-29 17:25:55 +00004535 0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004536 blend_image=DestroyImage(blend_image);
4537 return(sketch_image);
4538}
4539
4540/*
4541%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4542% %
4543% %
4544% %
4545% S o l a r i z e I m a g e %
4546% %
4547% %
4548% %
4549%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4550%
4551% SolarizeImage() applies a special effect to the image, similar to the effect
4552% achieved in a photo darkroom by selectively exposing areas of photo
4553% sensitive paper to light. Threshold ranges from 0 to QuantumRange and is a
4554% measure of the extent of the solarization.
4555%
4556% The format of the SolarizeImage method is:
4557%
cristy5cbc0162011-08-29 00:36:28 +00004558% MagickBooleanType SolarizeImage(Image *image,const double threshold,
4559% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004560%
4561% A description of each parameter follows:
4562%
4563% o image: the image.
4564%
4565% o threshold: Define the extent of the solarization.
4566%
cristy5cbc0162011-08-29 00:36:28 +00004567% o exception: return any errors or warnings in this structure.
4568%
cristy3ed852e2009-09-05 21:47:34 +00004569*/
4570MagickExport MagickBooleanType SolarizeImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00004571 const double threshold,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004572{
4573#define SolarizeImageTag "Solarize/Image"
4574
cristyc4c8d132010-01-07 01:58:38 +00004575 CacheView
4576 *image_view;
4577
cristy3ed852e2009-09-05 21:47:34 +00004578 MagickBooleanType
4579 status;
4580
cristybb503372010-05-27 20:51:26 +00004581 MagickOffsetType
4582 progress;
4583
4584 ssize_t
4585 y;
4586
cristy3ed852e2009-09-05 21:47:34 +00004587 assert(image != (Image *) NULL);
4588 assert(image->signature == MagickSignature);
4589 if (image->debug != MagickFalse)
4590 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4591 if (image->storage_class == PseudoClass)
4592 {
cristybb503372010-05-27 20:51:26 +00004593 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004594 i;
4595
4596 /*
4597 Solarize colormap.
4598 */
cristybb503372010-05-27 20:51:26 +00004599 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00004600 {
4601 if ((MagickRealType) image->colormap[i].red > threshold)
cristy6e963d82012-06-19 15:23:24 +00004602 image->colormap[i].red=QuantumRange-image->colormap[i].red;
cristy3ed852e2009-09-05 21:47:34 +00004603 if ((MagickRealType) image->colormap[i].green > threshold)
cristy6e963d82012-06-19 15:23:24 +00004604 image->colormap[i].green=QuantumRange-
cristy3ed852e2009-09-05 21:47:34 +00004605 image->colormap[i].green;
4606 if ((MagickRealType) image->colormap[i].blue > threshold)
cristy6e963d82012-06-19 15:23:24 +00004607 image->colormap[i].blue=QuantumRange-
cristy3ed852e2009-09-05 21:47:34 +00004608 image->colormap[i].blue;
4609 }
4610 }
4611 /*
4612 Solarize image.
4613 */
4614 status=MagickTrue;
4615 progress=0;
cristydb070952012-04-20 14:33:00 +00004616 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00004617#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00004618 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00004619 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00004620#endif
cristybb503372010-05-27 20:51:26 +00004621 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004622 {
cristybb503372010-05-27 20:51:26 +00004623 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004624 x;
4625
cristy4c08aed2011-07-01 19:47:50 +00004626 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004627 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004628
4629 if (status == MagickFalse)
4630 continue;
cristy5cbc0162011-08-29 00:36:28 +00004631 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00004632 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004633 {
4634 status=MagickFalse;
4635 continue;
4636 }
cristybb503372010-05-27 20:51:26 +00004637 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004638 {
cristy76f512e2011-09-12 01:26:56 +00004639 register ssize_t
4640 i;
4641
cristy10a6c612012-01-29 21:41:05 +00004642 if (GetPixelMask(image,q) != 0)
4643 {
4644 q+=GetPixelChannels(image);
4645 continue;
4646 }
cristy76f512e2011-09-12 01:26:56 +00004647 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4648 {
cristyabace412011-12-11 15:56:53 +00004649 PixelChannel
4650 channel;
4651
cristy76f512e2011-09-12 01:26:56 +00004652 PixelTrait
4653 traits;
4654
cristyabace412011-12-11 15:56:53 +00004655 channel=GetPixelChannelMapChannel(image,i);
4656 traits=GetPixelChannelMapTraits(image,channel);
cristy76f512e2011-09-12 01:26:56 +00004657 if ((traits == UndefinedPixelTrait) ||
4658 ((traits & CopyPixelTrait) != 0))
4659 continue;
4660 if ((MagickRealType) q[i] > threshold)
4661 q[i]=QuantumRange-q[i];
4662 }
cristyed231572011-07-14 02:18:59 +00004663 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00004664 }
4665 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4666 status=MagickFalse;
4667 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4668 {
4669 MagickBooleanType
4670 proceed;
4671
cristyb5d5f722009-11-04 03:03:49 +00004672#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00004673 #pragma omp critical (MagickCore_SolarizeImage)
cristy3ed852e2009-09-05 21:47:34 +00004674#endif
4675 proceed=SetImageProgress(image,SolarizeImageTag,progress++,image->rows);
4676 if (proceed == MagickFalse)
4677 status=MagickFalse;
4678 }
4679 }
4680 image_view=DestroyCacheView(image_view);
4681 return(status);
4682}
4683
4684/*
4685%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4686% %
4687% %
4688% %
4689% S t e g a n o I m a g e %
4690% %
4691% %
4692% %
4693%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4694%
4695% SteganoImage() hides a digital watermark within the image. Recover
4696% the hidden watermark later to prove that the authenticity of an image.
4697% Offset defines the start position within the image to hide the watermark.
4698%
4699% The format of the SteganoImage method is:
4700%
4701% Image *SteganoImage(const Image *image,Image *watermark,
4702% ExceptionInfo *exception)
4703%
4704% A description of each parameter follows:
4705%
4706% o image: the image.
4707%
4708% o watermark: the watermark image.
4709%
4710% o exception: return any errors or warnings in this structure.
4711%
4712*/
4713MagickExport Image *SteganoImage(const Image *image,const Image *watermark,
4714 ExceptionInfo *exception)
4715{
cristye1bf8ad2010-09-19 17:07:03 +00004716#define GetBit(alpha,i) ((((size_t) (alpha) >> (size_t) (i)) & 0x01) != 0)
cristy4c08aed2011-07-01 19:47:50 +00004717#define SetBit(alpha,i,set) (Quantum) ((set) != 0 ? (size_t) (alpha) \
cristyeaedf062010-05-29 22:36:02 +00004718 | (one << (size_t) (i)) : (size_t) (alpha) & ~(one << (size_t) (i)))
cristy3ed852e2009-09-05 21:47:34 +00004719#define SteganoImageTag "Stegano/Image"
4720
cristyb0d3bb92010-09-22 14:37:58 +00004721 CacheView
4722 *stegano_view,
4723 *watermark_view;
4724
cristy3ed852e2009-09-05 21:47:34 +00004725 Image
4726 *stegano_image;
4727
4728 int
4729 c;
4730
cristy3ed852e2009-09-05 21:47:34 +00004731 MagickBooleanType
4732 status;
4733
cristy101ab702011-10-13 13:06:32 +00004734 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00004735 pixel;
4736
cristy4c08aed2011-07-01 19:47:50 +00004737 register Quantum
cristy3ed852e2009-09-05 21:47:34 +00004738 *q;
4739
cristye1bf8ad2010-09-19 17:07:03 +00004740 register ssize_t
4741 x;
4742
cristybb503372010-05-27 20:51:26 +00004743 size_t
cristyeaedf062010-05-29 22:36:02 +00004744 depth,
4745 one;
cristy3ed852e2009-09-05 21:47:34 +00004746
cristye1bf8ad2010-09-19 17:07:03 +00004747 ssize_t
4748 i,
4749 j,
4750 k,
4751 y;
4752
cristy3ed852e2009-09-05 21:47:34 +00004753 /*
4754 Initialize steganographic image attributes.
4755 */
4756 assert(image != (const Image *) NULL);
4757 assert(image->signature == MagickSignature);
4758 if (image->debug != MagickFalse)
4759 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4760 assert(watermark != (const Image *) NULL);
4761 assert(watermark->signature == MagickSignature);
4762 assert(exception != (ExceptionInfo *) NULL);
4763 assert(exception->signature == MagickSignature);
cristyeaedf062010-05-29 22:36:02 +00004764 one=1UL;
cristy3ed852e2009-09-05 21:47:34 +00004765 stegano_image=CloneImage(image,0,0,MagickTrue,exception);
4766 if (stegano_image == (Image *) NULL)
4767 return((Image *) NULL);
cristyf61b1832012-04-01 01:38:19 +00004768 stegano_image->depth=MAGICKCORE_QUANTUM_DEPTH;
cristy574cc262011-08-05 01:23:58 +00004769 if (SetImageStorageClass(stegano_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004770 {
cristy3ed852e2009-09-05 21:47:34 +00004771 stegano_image=DestroyImage(stegano_image);
4772 return((Image *) NULL);
4773 }
cristy3ed852e2009-09-05 21:47:34 +00004774 /*
4775 Hide watermark in low-order bits of image.
4776 */
4777 c=0;
4778 i=0;
4779 j=0;
4780 depth=stegano_image->depth;
cristyf61b1832012-04-01 01:38:19 +00004781 k=stegano_image->offset;
cristyda16f162011-02-19 23:52:17 +00004782 status=MagickTrue;
cristydb070952012-04-20 14:33:00 +00004783 watermark_view=AcquireVirtualCacheView(watermark,exception);
4784 stegano_view=AcquireAuthenticCacheView(stegano_image,exception);
cristybb503372010-05-27 20:51:26 +00004785 for (i=(ssize_t) depth-1; (i >= 0) && (j < (ssize_t) depth); i--)
cristy3ed852e2009-09-05 21:47:34 +00004786 {
cristybb503372010-05-27 20:51:26 +00004787 for (y=0; (y < (ssize_t) watermark->rows) && (j < (ssize_t) depth); y++)
cristy3ed852e2009-09-05 21:47:34 +00004788 {
cristybb503372010-05-27 20:51:26 +00004789 for (x=0; (x < (ssize_t) watermark->columns) && (j < (ssize_t) depth); x++)
cristy3ed852e2009-09-05 21:47:34 +00004790 {
cristy1707c6c2012-01-18 23:30:54 +00004791 ssize_t
4792 offset;
4793
cristyf05d4942012-03-17 16:26:09 +00004794 (void) GetOneCacheViewVirtualPixelInfo(watermark_view,x,y,&pixel,
cristyda1f9c12011-10-02 21:39:49 +00004795 exception);
cristy1707c6c2012-01-18 23:30:54 +00004796 offset=k/(ssize_t) stegano_image->columns;
4797 if (offset >= (ssize_t) stegano_image->rows)
cristy3ed852e2009-09-05 21:47:34 +00004798 break;
cristyb0d3bb92010-09-22 14:37:58 +00004799 q=GetCacheViewAuthenticPixels(stegano_view,k % (ssize_t)
4800 stegano_image->columns,k/(ssize_t) stegano_image->columns,1,1,
4801 exception);
cristyacd2ed22011-08-30 01:44:23 +00004802 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004803 break;
4804 switch (c)
4805 {
4806 case 0:
4807 {
cristyf61b1832012-04-01 01:38:19 +00004808 SetPixelRed(stegano_image,SetBit(GetPixelRed(stegano_image,q),j,
4809 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004810 break;
4811 }
4812 case 1:
4813 {
cristyf61b1832012-04-01 01:38:19 +00004814 SetPixelGreen(stegano_image,SetBit(GetPixelGreen(stegano_image,q),j,
4815 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004816 break;
4817 }
4818 case 2:
4819 {
cristyf61b1832012-04-01 01:38:19 +00004820 SetPixelBlue(stegano_image,SetBit(GetPixelBlue(stegano_image,q),j,
4821 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004822 break;
4823 }
4824 }
cristyb0d3bb92010-09-22 14:37:58 +00004825 if (SyncCacheViewAuthenticPixels(stegano_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004826 break;
4827 c++;
4828 if (c == 3)
4829 c=0;
4830 k++;
cristybb503372010-05-27 20:51:26 +00004831 if (k == (ssize_t) (stegano_image->columns*stegano_image->columns))
cristy3ed852e2009-09-05 21:47:34 +00004832 k=0;
cristyf61b1832012-04-01 01:38:19 +00004833 if (k == stegano_image->offset)
cristy3ed852e2009-09-05 21:47:34 +00004834 j++;
4835 }
4836 }
cristy8b27a6d2010-02-14 03:31:15 +00004837 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004838 {
cristy8b27a6d2010-02-14 03:31:15 +00004839 MagickBooleanType
4840 proceed;
4841
4842 proceed=SetImageProgress(image,SteganoImageTag,(MagickOffsetType)
4843 (depth-i),depth);
4844 if (proceed == MagickFalse)
4845 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004846 }
4847 }
cristyb0d3bb92010-09-22 14:37:58 +00004848 stegano_view=DestroyCacheView(stegano_view);
4849 watermark_view=DestroyCacheView(watermark_view);
cristyda16f162011-02-19 23:52:17 +00004850 if (status == MagickFalse)
4851 {
4852 stegano_image=DestroyImage(stegano_image);
4853 return((Image *) NULL);
4854 }
cristy3ed852e2009-09-05 21:47:34 +00004855 return(stegano_image);
4856}
4857
4858/*
4859%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4860% %
4861% %
4862% %
4863% S t e r e o A n a g l y p h I m a g e %
4864% %
4865% %
4866% %
4867%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4868%
4869% StereoAnaglyphImage() combines two images and produces a single image that
4870% is the composite of a left and right image of a stereo pair. Special
4871% red-green stereo glasses are required to view this effect.
4872%
4873% The format of the StereoAnaglyphImage method is:
4874%
4875% Image *StereoImage(const Image *left_image,const Image *right_image,
4876% ExceptionInfo *exception)
4877% Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004878% const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004879% ExceptionInfo *exception)
4880%
4881% A description of each parameter follows:
4882%
4883% o left_image: the left image.
4884%
4885% o right_image: the right image.
4886%
4887% o exception: return any errors or warnings in this structure.
4888%
4889% o x_offset: amount, in pixels, by which the left image is offset to the
4890% right of the right image.
4891%
4892% o y_offset: amount, in pixels, by which the left image is offset to the
4893% bottom of the right image.
4894%
4895%
4896*/
4897MagickExport Image *StereoImage(const Image *left_image,
4898 const Image *right_image,ExceptionInfo *exception)
4899{
4900 return(StereoAnaglyphImage(left_image,right_image,0,0,exception));
4901}
4902
4903MagickExport Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004904 const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004905 ExceptionInfo *exception)
4906{
4907#define StereoImageTag "Stereo/Image"
4908
4909 const Image
4910 *image;
4911
4912 Image
4913 *stereo_image;
4914
cristy3ed852e2009-09-05 21:47:34 +00004915 MagickBooleanType
4916 status;
4917
cristy9d314ff2011-03-09 01:30:28 +00004918 ssize_t
4919 y;
4920
cristy3ed852e2009-09-05 21:47:34 +00004921 assert(left_image != (const Image *) NULL);
4922 assert(left_image->signature == MagickSignature);
4923 if (left_image->debug != MagickFalse)
4924 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4925 left_image->filename);
4926 assert(right_image != (const Image *) NULL);
4927 assert(right_image->signature == MagickSignature);
4928 assert(exception != (ExceptionInfo *) NULL);
4929 assert(exception->signature == MagickSignature);
4930 assert(right_image != (const Image *) NULL);
4931 image=left_image;
4932 if ((left_image->columns != right_image->columns) ||
4933 (left_image->rows != right_image->rows))
4934 ThrowImageException(ImageError,"LeftAndRightImageSizesDiffer");
4935 /*
4936 Initialize stereo image attributes.
4937 */
4938 stereo_image=CloneImage(left_image,left_image->columns,left_image->rows,
4939 MagickTrue,exception);
4940 if (stereo_image == (Image *) NULL)
4941 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004942 if (SetImageStorageClass(stereo_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004943 {
cristy3ed852e2009-09-05 21:47:34 +00004944 stereo_image=DestroyImage(stereo_image);
4945 return((Image *) NULL);
4946 }
cristy079c78d2012-07-03 11:53:48 +00004947 (void) SetImageColorspace(stereo_image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00004948 /*
4949 Copy left image to red channel and right image to blue channel.
4950 */
cristyda16f162011-02-19 23:52:17 +00004951 status=MagickTrue;
cristybb503372010-05-27 20:51:26 +00004952 for (y=0; y < (ssize_t) stereo_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004953 {
cristy4c08aed2011-07-01 19:47:50 +00004954 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004955 *restrict p,
4956 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004957
cristybb503372010-05-27 20:51:26 +00004958 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004959 x;
4960
cristy4c08aed2011-07-01 19:47:50 +00004961 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004962 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +00004963
4964 p=GetVirtualPixels(left_image,-x_offset,y-y_offset,image->columns,1,
4965 exception);
4966 q=GetVirtualPixels(right_image,0,y,right_image->columns,1,exception);
4967 r=QueueAuthenticPixels(stereo_image,0,y,stereo_image->columns,1,exception);
cristy76f512e2011-09-12 01:26:56 +00004968 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL) ||
4969 (r == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004970 break;
cristybb503372010-05-27 20:51:26 +00004971 for (x=0; x < (ssize_t) stereo_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004972 {
cristy4c08aed2011-07-01 19:47:50 +00004973 SetPixelRed(image,GetPixelRed(left_image,p),r);
cristy76f512e2011-09-12 01:26:56 +00004974 SetPixelGreen(image,GetPixelGreen(right_image,q),r);
4975 SetPixelBlue(image,GetPixelBlue(right_image,q),r);
4976 if ((GetPixelAlphaTraits(stereo_image) & CopyPixelTrait) != 0)
4977 SetPixelAlpha(image,(GetPixelAlpha(left_image,p)+
4978 GetPixelAlpha(right_image,q))/2,r);
cristyed231572011-07-14 02:18:59 +00004979 p+=GetPixelChannels(left_image);
cristy76f512e2011-09-12 01:26:56 +00004980 q+=GetPixelChannels(right_image);
4981 r+=GetPixelChannels(stereo_image);
cristy3ed852e2009-09-05 21:47:34 +00004982 }
4983 if (SyncAuthenticPixels(stereo_image,exception) == MagickFalse)
4984 break;
cristy8b27a6d2010-02-14 03:31:15 +00004985 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004986 {
cristy8b27a6d2010-02-14 03:31:15 +00004987 MagickBooleanType
4988 proceed;
4989
cristybb503372010-05-27 20:51:26 +00004990 proceed=SetImageProgress(image,StereoImageTag,(MagickOffsetType) y,
4991 stereo_image->rows);
cristy8b27a6d2010-02-14 03:31:15 +00004992 if (proceed == MagickFalse)
4993 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004994 }
4995 }
cristyda16f162011-02-19 23:52:17 +00004996 if (status == MagickFalse)
4997 {
4998 stereo_image=DestroyImage(stereo_image);
4999 return((Image *) NULL);
5000 }
cristy3ed852e2009-09-05 21:47:34 +00005001 return(stereo_image);
5002}
5003
5004/*
5005%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5006% %
5007% %
5008% %
5009% S w i r l I m a g e %
5010% %
5011% %
5012% %
5013%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5014%
5015% SwirlImage() swirls the pixels about the center of the image, where
5016% degrees indicates the sweep of the arc through which each pixel is moved.
5017% You get a more dramatic effect as the degrees move from 1 to 360.
5018%
5019% The format of the SwirlImage method is:
5020%
5021% Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00005022% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005023%
5024% A description of each parameter follows:
5025%
5026% o image: the image.
5027%
5028% o degrees: Define the tightness of the swirling effect.
5029%
cristy76f512e2011-09-12 01:26:56 +00005030% o method: the pixel interpolation method.
5031%
cristy3ed852e2009-09-05 21:47:34 +00005032% o exception: return any errors or warnings in this structure.
5033%
5034*/
5035MagickExport Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00005036 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005037{
5038#define SwirlImageTag "Swirl/Image"
5039
cristyfa112112010-01-04 17:48:07 +00005040 CacheView
5041 *image_view,
5042 *swirl_view;
5043
cristy3ed852e2009-09-05 21:47:34 +00005044 Image
5045 *swirl_image;
5046
cristy3ed852e2009-09-05 21:47:34 +00005047 MagickBooleanType
5048 status;
5049
cristybb503372010-05-27 20:51:26 +00005050 MagickOffsetType
5051 progress;
5052
cristy3ed852e2009-09-05 21:47:34 +00005053 MagickRealType
5054 radius;
5055
5056 PointInfo
5057 center,
5058 scale;
5059
cristybb503372010-05-27 20:51:26 +00005060 ssize_t
5061 y;
5062
cristy3ed852e2009-09-05 21:47:34 +00005063 /*
5064 Initialize swirl image attributes.
5065 */
5066 assert(image != (const Image *) NULL);
5067 assert(image->signature == MagickSignature);
5068 if (image->debug != MagickFalse)
5069 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5070 assert(exception != (ExceptionInfo *) NULL);
5071 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00005072 swirl_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005073 if (swirl_image == (Image *) NULL)
5074 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005075 if (SetImageStorageClass(swirl_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005076 {
cristy3ed852e2009-09-05 21:47:34 +00005077 swirl_image=DestroyImage(swirl_image);
5078 return((Image *) NULL);
5079 }
cristy4c08aed2011-07-01 19:47:50 +00005080 if (swirl_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00005081 swirl_image->matte=MagickTrue;
5082 /*
5083 Compute scaling factor.
5084 */
5085 center.x=(double) image->columns/2.0;
5086 center.y=(double) image->rows/2.0;
5087 radius=MagickMax(center.x,center.y);
5088 scale.x=1.0;
5089 scale.y=1.0;
5090 if (image->columns > image->rows)
5091 scale.y=(double) image->columns/(double) image->rows;
5092 else
5093 if (image->columns < image->rows)
5094 scale.x=(double) image->rows/(double) image->columns;
5095 degrees=(double) DegreesToRadians(degrees);
5096 /*
5097 Swirl image.
5098 */
5099 status=MagickTrue;
5100 progress=0;
cristydb070952012-04-20 14:33:00 +00005101 image_view=AcquireVirtualCacheView(image,exception);
5102 swirl_view=AcquireAuthenticCacheView(swirl_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00005103#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00005104 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00005105 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00005106#endif
cristybb503372010-05-27 20:51:26 +00005107 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005108 {
cristy3ed852e2009-09-05 21:47:34 +00005109 MagickRealType
5110 distance;
5111
5112 PointInfo
5113 delta;
5114
cristy6d188022011-09-12 13:23:33 +00005115 register const Quantum
5116 *restrict p;
5117
cristybb503372010-05-27 20:51:26 +00005118 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005119 x;
5120
cristy4c08aed2011-07-01 19:47:50 +00005121 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005122 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005123
5124 if (status == MagickFalse)
5125 continue;
cristy6d188022011-09-12 13:23:33 +00005126 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy1707c6c2012-01-18 23:30:54 +00005127 q=QueueCacheViewAuthenticPixels(swirl_view,0,y,swirl_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00005128 exception);
cristy6d188022011-09-12 13:23:33 +00005129 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005130 {
5131 status=MagickFalse;
5132 continue;
5133 }
cristy3ed852e2009-09-05 21:47:34 +00005134 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00005135 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005136 {
5137 /*
5138 Determine if the pixel is within an ellipse.
5139 */
cristy10a6c612012-01-29 21:41:05 +00005140 if (GetPixelMask(image,p) != 0)
5141 {
5142 p+=GetPixelChannels(image);
5143 q+=GetPixelChannels(swirl_image);
5144 continue;
5145 }
cristy3ed852e2009-09-05 21:47:34 +00005146 delta.x=scale.x*(double) (x-center.x);
5147 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00005148 if (distance >= (radius*radius))
5149 {
cristy1707c6c2012-01-18 23:30:54 +00005150 register ssize_t
5151 i;
5152
cristy6d188022011-09-12 13:23:33 +00005153 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy1707c6c2012-01-18 23:30:54 +00005154 {
5155 PixelChannel
5156 channel;
5157
5158 PixelTrait
5159 swirl_traits,
5160 traits;
5161
5162 channel=GetPixelChannelMapChannel(image,i);
5163 traits=GetPixelChannelMapTraits(image,channel);
5164 swirl_traits=GetPixelChannelMapTraits(swirl_image,channel);
5165 if ((traits == UndefinedPixelTrait) ||
5166 (swirl_traits == UndefinedPixelTrait))
5167 continue;
5168 SetPixelChannel(swirl_image,channel,p[i],q);
5169 }
cristy6d188022011-09-12 13:23:33 +00005170 }
5171 else
cristy3ed852e2009-09-05 21:47:34 +00005172 {
5173 MagickRealType
5174 cosine,
5175 factor,
5176 sine;
5177
5178 /*
5179 Swirl the pixel.
5180 */
5181 factor=1.0-sqrt((double) distance)/radius;
5182 sine=sin((double) (degrees*factor*factor));
5183 cosine=cos((double) (degrees*factor*factor));
cristy76f512e2011-09-12 01:26:56 +00005184 status=InterpolatePixelChannels(image,image_view,swirl_image,method,
5185 ((cosine*delta.x-sine*delta.y)/scale.x+center.x),(double)
5186 ((sine*delta.x+cosine*delta.y)/scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00005187 }
cristy6d188022011-09-12 13:23:33 +00005188 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00005189 q+=GetPixelChannels(swirl_image);
cristy3ed852e2009-09-05 21:47:34 +00005190 }
5191 if (SyncCacheViewAuthenticPixels(swirl_view,exception) == MagickFalse)
5192 status=MagickFalse;
5193 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5194 {
5195 MagickBooleanType
5196 proceed;
5197
cristyb5d5f722009-11-04 03:03:49 +00005198#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005199 #pragma omp critical (MagickCore_SwirlImage)
cristy3ed852e2009-09-05 21:47:34 +00005200#endif
5201 proceed=SetImageProgress(image,SwirlImageTag,progress++,image->rows);
5202 if (proceed == MagickFalse)
5203 status=MagickFalse;
5204 }
5205 }
5206 swirl_view=DestroyCacheView(swirl_view);
5207 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00005208 if (status == MagickFalse)
5209 swirl_image=DestroyImage(swirl_image);
5210 return(swirl_image);
5211}
5212
5213/*
5214%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5215% %
5216% %
5217% %
5218% T i n t I m a g e %
5219% %
5220% %
5221% %
5222%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5223%
5224% TintImage() applies a color vector to each pixel in the image. The length
5225% of the vector is 0 for black and white and at its maximum for the midtones.
5226% The vector weighting function is f(x)=(1-(4.0*((x-0.5)*(x-0.5))))
5227%
5228% The format of the TintImage method is:
5229%
cristyb817c3f2011-10-03 14:00:35 +00005230% Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005231% const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005232%
5233% A description of each parameter follows:
5234%
5235% o image: the image.
5236%
cristyb817c3f2011-10-03 14:00:35 +00005237% o blend: A color value used for tinting.
cristy3ed852e2009-09-05 21:47:34 +00005238%
5239% o tint: A color value used for tinting.
5240%
5241% o exception: return any errors or warnings in this structure.
5242%
5243*/
cristyb817c3f2011-10-03 14:00:35 +00005244MagickExport Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005245 const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005246{
5247#define TintImageTag "Tint/Image"
5248
cristyc4c8d132010-01-07 01:58:38 +00005249 CacheView
5250 *image_view,
5251 *tint_view;
5252
cristy3ed852e2009-09-05 21:47:34 +00005253 GeometryInfo
5254 geometry_info;
5255
5256 Image
5257 *tint_image;
5258
cristy3ed852e2009-09-05 21:47:34 +00005259 MagickBooleanType
5260 status;
5261
cristybb503372010-05-27 20:51:26 +00005262 MagickOffsetType
5263 progress;
cristy3ed852e2009-09-05 21:47:34 +00005264
cristy28474bf2011-09-11 23:32:52 +00005265 MagickRealType
5266 intensity;
5267
cristy4c08aed2011-07-01 19:47:50 +00005268 PixelInfo
cristy1707c6c2012-01-18 23:30:54 +00005269 color_vector;
cristy3ed852e2009-09-05 21:47:34 +00005270
cristybb503372010-05-27 20:51:26 +00005271 MagickStatusType
5272 flags;
5273
5274 ssize_t
5275 y;
5276
cristy3ed852e2009-09-05 21:47:34 +00005277 /*
5278 Allocate tint image.
5279 */
5280 assert(image != (const Image *) NULL);
5281 assert(image->signature == MagickSignature);
5282 if (image->debug != MagickFalse)
5283 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5284 assert(exception != (ExceptionInfo *) NULL);
5285 assert(exception->signature == MagickSignature);
5286 tint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5287 if (tint_image == (Image *) NULL)
5288 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005289 if (SetImageStorageClass(tint_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005290 {
cristy3ed852e2009-09-05 21:47:34 +00005291 tint_image=DestroyImage(tint_image);
5292 return((Image *) NULL);
5293 }
cristy71aac542012-05-18 12:06:35 +00005294 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
5295 (IsPixelInfoGray(tint) == MagickFalse))
cristyb09db112012-07-11 12:04:31 +00005296 (void) SetImageColorspace(tint_image,RGBColorspace,exception);
cristyaed9c382011-10-03 17:54:21 +00005297 if (blend == (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005298 return(tint_image);
5299 /*
5300 Determine RGB values of the color.
5301 */
cristy1707c6c2012-01-18 23:30:54 +00005302 GetPixelInfo(image,&color_vector);
cristyb817c3f2011-10-03 14:00:35 +00005303 flags=ParseGeometry(blend,&geometry_info);
cristy1707c6c2012-01-18 23:30:54 +00005304 color_vector.red=geometry_info.rho;
5305 color_vector.green=geometry_info.rho;
5306 color_vector.blue=geometry_info.rho;
5307 color_vector.alpha=OpaqueAlpha;
cristy3ed852e2009-09-05 21:47:34 +00005308 if ((flags & SigmaValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005309 color_vector.green=geometry_info.sigma;
cristy3ed852e2009-09-05 21:47:34 +00005310 if ((flags & XiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005311 color_vector.blue=geometry_info.xi;
cristyb817c3f2011-10-03 14:00:35 +00005312 if ((flags & PsiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005313 color_vector.alpha=geometry_info.psi;
cristy76f512e2011-09-12 01:26:56 +00005314 if (image->colorspace == CMYKColorspace)
5315 {
cristy1707c6c2012-01-18 23:30:54 +00005316 color_vector.black=geometry_info.rho;
cristy76f512e2011-09-12 01:26:56 +00005317 if ((flags & PsiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005318 color_vector.black=geometry_info.psi;
cristy76f512e2011-09-12 01:26:56 +00005319 if ((flags & ChiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005320 color_vector.alpha=geometry_info.chi;
cristy76f512e2011-09-12 01:26:56 +00005321 }
cristy28474bf2011-09-11 23:32:52 +00005322 intensity=(MagickRealType) GetPixelInfoIntensity(tint);
cristy1707c6c2012-01-18 23:30:54 +00005323 color_vector.red=(MagickRealType) (color_vector.red*tint->red/100.0-
5324 intensity);
5325 color_vector.green=(MagickRealType) (color_vector.green*tint->green/100.0-
5326 intensity);
5327 color_vector.blue=(MagickRealType) (color_vector.blue*tint->blue/100.0-
5328 intensity);
5329 color_vector.black=(MagickRealType) (color_vector.black*tint->black/100.0-
5330 intensity);
5331 color_vector.alpha=(MagickRealType) (color_vector.alpha*tint->alpha/100.0-
5332 intensity);
cristy3ed852e2009-09-05 21:47:34 +00005333 /*
5334 Tint image.
5335 */
5336 status=MagickTrue;
5337 progress=0;
cristydb070952012-04-20 14:33:00 +00005338 image_view=AcquireVirtualCacheView(image,exception);
5339 tint_view=AcquireAuthenticCacheView(tint_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00005340#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00005341 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00005342 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00005343#endif
cristybb503372010-05-27 20:51:26 +00005344 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005345 {
cristy4c08aed2011-07-01 19:47:50 +00005346 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00005347 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00005348
cristy4c08aed2011-07-01 19:47:50 +00005349 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005350 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005351
cristy6b91acb2011-04-19 12:23:54 +00005352 register ssize_t
5353 x;
5354
cristy3ed852e2009-09-05 21:47:34 +00005355 if (status == MagickFalse)
5356 continue;
5357 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5358 q=QueueCacheViewAuthenticPixels(tint_view,0,y,tint_image->columns,1,
5359 exception);
cristy4c08aed2011-07-01 19:47:50 +00005360 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005361 {
5362 status=MagickFalse;
5363 continue;
5364 }
cristybb503372010-05-27 20:51:26 +00005365 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005366 {
cristy4c08aed2011-07-01 19:47:50 +00005367 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00005368 pixel;
5369
5370 MagickRealType
5371 weight;
5372
cristy1707c6c2012-01-18 23:30:54 +00005373 register ssize_t
5374 i;
5375
cristy10a6c612012-01-29 21:41:05 +00005376 if (GetPixelMask(image,p) != 0)
5377 {
5378 p+=GetPixelChannels(image);
5379 q+=GetPixelChannels(tint_image);
5380 continue;
5381 }
cristy1707c6c2012-01-18 23:30:54 +00005382 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5383 {
5384 PixelChannel
5385 channel;
5386
5387 PixelTrait
5388 tint_traits,
5389 traits;
5390
5391 channel=GetPixelChannelMapChannel(image,i);
5392 traits=GetPixelChannelMapTraits(image,channel);
5393 tint_traits=GetPixelChannelMapTraits(tint_image,channel);
5394 if ((traits == UndefinedPixelTrait) ||
5395 (tint_traits == UndefinedPixelTrait))
5396 continue;
5397 if ((tint_traits & CopyPixelTrait) != 0)
5398 {
5399 SetPixelChannel(tint_image,channel,p[i],q);
5400 continue;
5401 }
5402 }
5403 GetPixelInfo(image,&pixel);
5404 weight=QuantumScale*GetPixelRed(image,p)-0.5;
5405 pixel.red=(MagickRealType) GetPixelRed(image,p)+color_vector.red*
5406 (1.0-(4.0*(weight*weight)));
5407 weight=QuantumScale*GetPixelGreen(image,p)-0.5;
5408 pixel.green=(MagickRealType) GetPixelGreen(image,p)+color_vector.green*
5409 (1.0-(4.0*(weight*weight)));
5410 weight=QuantumScale*GetPixelBlue(image,p)-0.5;
5411 pixel.blue=(MagickRealType) GetPixelBlue(image,p)+color_vector.blue*
5412 (1.0-(4.0*(weight*weight)));
5413 weight=QuantumScale*GetPixelBlack(image,p)-0.5;
5414 pixel.black=(MagickRealType) GetPixelBlack(image,p)+color_vector.black*
5415 (1.0-(4.0*(weight*weight)));
5416 SetPixelInfoPixel(tint_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +00005417 p+=GetPixelChannels(image);
5418 q+=GetPixelChannels(tint_image);
cristy3ed852e2009-09-05 21:47:34 +00005419 }
5420 if (SyncCacheViewAuthenticPixels(tint_view,exception) == MagickFalse)
5421 status=MagickFalse;
5422 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5423 {
5424 MagickBooleanType
5425 proceed;
5426
cristyb5d5f722009-11-04 03:03:49 +00005427#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005428 #pragma omp critical (MagickCore_TintImage)
cristy3ed852e2009-09-05 21:47:34 +00005429#endif
5430 proceed=SetImageProgress(image,TintImageTag,progress++,image->rows);
5431 if (proceed == MagickFalse)
5432 status=MagickFalse;
5433 }
5434 }
5435 tint_view=DestroyCacheView(tint_view);
5436 image_view=DestroyCacheView(image_view);
5437 if (status == MagickFalse)
5438 tint_image=DestroyImage(tint_image);
5439 return(tint_image);
5440}
5441
5442/*
5443%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5444% %
5445% %
5446% %
5447% V i g n e t t e I m a g e %
5448% %
5449% %
5450% %
5451%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5452%
5453% VignetteImage() softens the edges of the image in vignette style.
5454%
5455% The format of the VignetteImage method is:
5456%
5457% Image *VignetteImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00005458% const double sigma,const ssize_t x,const ssize_t y,
cristy05c0c9a2011-09-05 23:16:13 +00005459% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005460%
5461% A description of each parameter follows:
5462%
5463% o image: the image.
5464%
5465% o radius: the radius of the pixel neighborhood.
5466%
5467% o sigma: the standard deviation of the Gaussian, in pixels.
5468%
5469% o x, y: Define the x and y ellipse offset.
5470%
5471% o exception: return any errors or warnings in this structure.
5472%
5473*/
5474MagickExport Image *VignetteImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00005475 const double sigma,const ssize_t x,const ssize_t y,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005476{
5477 char
5478 ellipse[MaxTextExtent];
5479
5480 DrawInfo
5481 *draw_info;
5482
5483 Image
5484 *canvas_image,
5485 *blur_image,
5486 *oval_image,
5487 *vignette_image;
5488
5489 assert(image != (Image *) NULL);
5490 assert(image->signature == MagickSignature);
5491 if (image->debug != MagickFalse)
5492 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5493 assert(exception != (ExceptionInfo *) NULL);
5494 assert(exception->signature == MagickSignature);
5495 canvas_image=CloneImage(image,0,0,MagickTrue,exception);
5496 if (canvas_image == (Image *) NULL)
5497 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005498 if (SetImageStorageClass(canvas_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005499 {
cristy3ed852e2009-09-05 21:47:34 +00005500 canvas_image=DestroyImage(canvas_image);
5501 return((Image *) NULL);
5502 }
5503 canvas_image->matte=MagickTrue;
cristy98621462011-12-31 22:31:11 +00005504 oval_image=CloneImage(canvas_image,canvas_image->columns,canvas_image->rows,
5505 MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005506 if (oval_image == (Image *) NULL)
5507 {
5508 canvas_image=DestroyImage(canvas_image);
5509 return((Image *) NULL);
5510 }
cristy9950d572011-10-01 18:22:35 +00005511 (void) QueryColorCompliance("#000000",AllCompliance,
5512 &oval_image->background_color,exception);
cristyea1a8aa2011-10-20 13:24:06 +00005513 (void) SetImageBackgroundColor(oval_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00005514 draw_info=CloneDrawInfo((const ImageInfo *) NULL,(const DrawInfo *) NULL);
cristy9950d572011-10-01 18:22:35 +00005515 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->fill,
5516 exception);
5517 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->stroke,
5518 exception);
cristy1707c6c2012-01-18 23:30:54 +00005519 (void) FormatLocaleString(ellipse,MaxTextExtent,"ellipse %g,%g,%g,%g,"
5520 "0.0,360.0",image->columns/2.0,image->rows/2.0,image->columns/2.0-x,
5521 image->rows/2.0-y);
cristy3ed852e2009-09-05 21:47:34 +00005522 draw_info->primitive=AcquireString(ellipse);
cristy018f07f2011-09-04 21:15:19 +00005523 (void) DrawImage(oval_image,draw_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00005524 draw_info=DestroyDrawInfo(draw_info);
cristyaa2c16c2012-03-25 22:21:35 +00005525 blur_image=BlurImage(oval_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +00005526 oval_image=DestroyImage(oval_image);
5527 if (blur_image == (Image *) NULL)
5528 {
5529 canvas_image=DestroyImage(canvas_image);
5530 return((Image *) NULL);
5531 }
5532 blur_image->matte=MagickFalse;
cristy39172402012-03-30 13:04:39 +00005533 (void) CompositeImage(canvas_image,blur_image,IntensityCompositeOp,MagickTrue,
5534 0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00005535 blur_image=DestroyImage(blur_image);
5536 vignette_image=MergeImageLayers(canvas_image,FlattenLayer,exception);
5537 canvas_image=DestroyImage(canvas_image);
cristy66d26122012-06-23 21:56:40 +00005538 if (vignette_image != (Image *) NULL)
5539 (void) TransformImageColorspace(vignette_image,image->colorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00005540 return(vignette_image);
5541}
5542
5543/*
5544%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5545% %
5546% %
5547% %
5548% W a v e I m a g e %
5549% %
5550% %
5551% %
5552%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5553%
5554% WaveImage() creates a "ripple" effect in the image by shifting the pixels
cristycee97112010-05-28 00:44:52 +00005555% vertically along a sine wave whose amplitude and wavelength is specified
cristy3ed852e2009-09-05 21:47:34 +00005556% by the given parameters.
5557%
5558% The format of the WaveImage method is:
5559%
5560% Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005561% const double wave_length,const PixelInterpolateMethod method,
5562% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005563%
5564% A description of each parameter follows:
5565%
5566% o image: the image.
5567%
5568% o amplitude, wave_length: Define the amplitude and wave length of the
5569% sine wave.
5570%
cristy5c4e2582011-09-11 19:21:03 +00005571% o interpolate: the pixel interpolation method.
5572%
cristy3ed852e2009-09-05 21:47:34 +00005573% o exception: return any errors or warnings in this structure.
5574%
5575*/
5576MagickExport Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005577 const double wave_length,const PixelInterpolateMethod method,
5578 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005579{
5580#define WaveImageTag "Wave/Image"
5581
cristyfa112112010-01-04 17:48:07 +00005582 CacheView
cristyd76c51e2011-03-26 00:21:26 +00005583 *image_view,
cristyfa112112010-01-04 17:48:07 +00005584 *wave_view;
5585
cristy3ed852e2009-09-05 21:47:34 +00005586 Image
5587 *wave_image;
5588
cristy3ed852e2009-09-05 21:47:34 +00005589 MagickBooleanType
5590 status;
5591
cristybb503372010-05-27 20:51:26 +00005592 MagickOffsetType
5593 progress;
5594
cristy3ed852e2009-09-05 21:47:34 +00005595 MagickRealType
5596 *sine_map;
5597
cristybb503372010-05-27 20:51:26 +00005598 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005599 i;
5600
cristybb503372010-05-27 20:51:26 +00005601 ssize_t
5602 y;
5603
cristy3ed852e2009-09-05 21:47:34 +00005604 /*
5605 Initialize wave image attributes.
5606 */
5607 assert(image != (Image *) NULL);
5608 assert(image->signature == MagickSignature);
5609 if (image->debug != MagickFalse)
5610 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5611 assert(exception != (ExceptionInfo *) NULL);
5612 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00005613 wave_image=CloneImage(image,image->columns,(size_t) (image->rows+2.0*
cristy3ed852e2009-09-05 21:47:34 +00005614 fabs(amplitude)),MagickTrue,exception);
5615 if (wave_image == (Image *) NULL)
5616 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005617 if (SetImageStorageClass(wave_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005618 {
cristy3ed852e2009-09-05 21:47:34 +00005619 wave_image=DestroyImage(wave_image);
5620 return((Image *) NULL);
5621 }
cristy4c08aed2011-07-01 19:47:50 +00005622 if (wave_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00005623 wave_image->matte=MagickTrue;
5624 /*
5625 Allocate sine map.
5626 */
5627 sine_map=(MagickRealType *) AcquireQuantumMemory((size_t) wave_image->columns,
5628 sizeof(*sine_map));
5629 if (sine_map == (MagickRealType *) NULL)
5630 {
5631 wave_image=DestroyImage(wave_image);
5632 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
5633 }
cristybb503372010-05-27 20:51:26 +00005634 for (i=0; i < (ssize_t) wave_image->columns; i++)
cristy4205a3c2010-09-12 20:19:59 +00005635 sine_map[i]=fabs(amplitude)+amplitude*sin((double) ((2.0*MagickPI*i)/
5636 wave_length));
cristy3ed852e2009-09-05 21:47:34 +00005637 /*
5638 Wave image.
5639 */
5640 status=MagickTrue;
5641 progress=0;
cristydb070952012-04-20 14:33:00 +00005642 image_view=AcquireVirtualCacheView(image,exception);
5643 wave_view=AcquireAuthenticCacheView(wave_image,exception);
cristyd76c51e2011-03-26 00:21:26 +00005644 (void) SetCacheViewVirtualPixelMethod(image_view,
5645 BackgroundVirtualPixelMethod);
cristyb5d5f722009-11-04 03:03:49 +00005646#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00005647 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00005648 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00005649#endif
cristybb503372010-05-27 20:51:26 +00005650 for (y=0; y < (ssize_t) wave_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005651 {
cristy4c08aed2011-07-01 19:47:50 +00005652 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005653 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005654
cristye97bb922011-04-03 01:36:52 +00005655 register ssize_t
5656 x;
5657
cristy3ed852e2009-09-05 21:47:34 +00005658 if (status == MagickFalse)
5659 continue;
5660 q=QueueCacheViewAuthenticPixels(wave_view,0,y,wave_image->columns,1,
5661 exception);
cristyacd2ed22011-08-30 01:44:23 +00005662 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005663 {
5664 status=MagickFalse;
5665 continue;
5666 }
cristybb503372010-05-27 20:51:26 +00005667 for (x=0; x < (ssize_t) wave_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005668 {
cristy5c4e2582011-09-11 19:21:03 +00005669 status=InterpolatePixelChannels(image,image_view,wave_image,method,
5670 (double) x,(double) (y-sine_map[x]),q,exception);
cristyed231572011-07-14 02:18:59 +00005671 q+=GetPixelChannels(wave_image);
cristy3ed852e2009-09-05 21:47:34 +00005672 }
5673 if (SyncCacheViewAuthenticPixels(wave_view,exception) == MagickFalse)
5674 status=MagickFalse;
5675 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5676 {
5677 MagickBooleanType
5678 proceed;
5679
cristyb5d5f722009-11-04 03:03:49 +00005680#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005681 #pragma omp critical (MagickCore_WaveImage)
cristy3ed852e2009-09-05 21:47:34 +00005682#endif
5683 proceed=SetImageProgress(image,WaveImageTag,progress++,image->rows);
5684 if (proceed == MagickFalse)
5685 status=MagickFalse;
5686 }
5687 }
5688 wave_view=DestroyCacheView(wave_view);
cristyd76c51e2011-03-26 00:21:26 +00005689 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00005690 sine_map=(MagickRealType *) RelinquishMagickMemory(sine_map);
5691 if (status == MagickFalse)
5692 wave_image=DestroyImage(wave_image);
5693 return(wave_image);
5694}