blob: 787958ed0078667c5076e384f148225098446a23 [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% %
cristy7e41fe82010-12-04 23:12:08 +000020% Copyright 1999-2011 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"
51#include "MagickCore/composite.h"
52#include "MagickCore/decorate.h"
53#include "MagickCore/draw.h"
54#include "MagickCore/effect.h"
55#include "MagickCore/enhance.h"
56#include "MagickCore/exception.h"
57#include "MagickCore/exception-private.h"
58#include "MagickCore/fx.h"
59#include "MagickCore/fx-private.h"
60#include "MagickCore/gem.h"
cristy8ea81222011-09-04 10:33:32 +000061#include "MagickCore/gem-private.h"
cristy4c08aed2011-07-01 19:47:50 +000062#include "MagickCore/geometry.h"
63#include "MagickCore/layer.h"
64#include "MagickCore/list.h"
65#include "MagickCore/log.h"
66#include "MagickCore/image.h"
67#include "MagickCore/image-private.h"
68#include "MagickCore/magick.h"
69#include "MagickCore/memory_.h"
70#include "MagickCore/monitor.h"
71#include "MagickCore/monitor-private.h"
72#include "MagickCore/option.h"
73#include "MagickCore/pixel.h"
74#include "MagickCore/pixel-accessor.h"
75#include "MagickCore/property.h"
76#include "MagickCore/quantum.h"
77#include "MagickCore/quantum-private.h"
78#include "MagickCore/random_.h"
79#include "MagickCore/random-private.h"
80#include "MagickCore/resample.h"
81#include "MagickCore/resample-private.h"
82#include "MagickCore/resize.h"
83#include "MagickCore/shear.h"
84#include "MagickCore/splay-tree.h"
85#include "MagickCore/statistic.h"
86#include "MagickCore/string_.h"
87#include "MagickCore/string-private.h"
88#include "MagickCore/thread-private.h"
89#include "MagickCore/transform.h"
90#include "MagickCore/utility.h"
cristy3ed852e2009-09-05 21:47:34 +000091
92/*
93 Define declarations.
94*/
95#define LeftShiftOperator 0xf5
96#define RightShiftOperator 0xf6
97#define LessThanEqualOperator 0xf7
98#define GreaterThanEqualOperator 0xf8
99#define EqualOperator 0xf9
100#define NotEqualOperator 0xfa
101#define LogicalAndOperator 0xfb
102#define LogicalOrOperator 0xfc
cristy116af162010-08-13 01:25:47 +0000103#define ExponentialNotation 0xfd
cristy3ed852e2009-09-05 21:47:34 +0000104
105struct _FxInfo
106{
107 const Image
108 *images;
109
cristy3ed852e2009-09-05 21:47:34 +0000110 char
111 *expression;
112
113 FILE
114 *file;
115
116 SplayTreeInfo
117 *colors,
118 *symbols;
119
cristyd76c51e2011-03-26 00:21:26 +0000120 CacheView
121 **view;
cristy3ed852e2009-09-05 21:47:34 +0000122
123 RandomInfo
124 *random_info;
125
126 ExceptionInfo
127 *exception;
128};
129
130/*
131%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
132% %
133% %
134% %
135+ A c q u i r e F x I n f o %
136% %
137% %
138% %
139%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
140%
141% AcquireFxInfo() allocates the FxInfo structure.
142%
143% The format of the AcquireFxInfo method is:
144%
145% FxInfo *AcquireFxInfo(Image *image,const char *expression)
cristy0a9b3722010-10-23 18:45:49 +0000146%
cristy3ed852e2009-09-05 21:47:34 +0000147% A description of each parameter follows:
148%
149% o image: the image.
150%
151% o expression: the expression.
152%
153*/
cristy7832dc22011-09-05 01:21:53 +0000154MagickPrivate FxInfo *AcquireFxInfo(const Image *image,const char *expression)
cristy3ed852e2009-09-05 21:47:34 +0000155{
156 char
157 fx_op[2];
158
cristycb180922011-03-11 14:41:24 +0000159 const Image
160 *next;
161
cristy3ed852e2009-09-05 21:47:34 +0000162 FxInfo
163 *fx_info;
164
cristybb503372010-05-27 20:51:26 +0000165 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000166 i;
167
cristy73bd4a52010-10-05 11:24:23 +0000168 fx_info=(FxInfo *) AcquireMagickMemory(sizeof(*fx_info));
cristy3ed852e2009-09-05 21:47:34 +0000169 if (fx_info == (FxInfo *) NULL)
170 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
171 (void) ResetMagickMemory(fx_info,0,sizeof(*fx_info));
172 fx_info->exception=AcquireExceptionInfo();
anthony7d86e172011-03-23 12:37:06 +0000173 fx_info->images=image;
cristy3ed852e2009-09-05 21:47:34 +0000174 fx_info->colors=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
175 RelinquishMagickMemory);
176 fx_info->symbols=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
177 RelinquishMagickMemory);
cristyd76c51e2011-03-26 00:21:26 +0000178 fx_info->view=(CacheView **) AcquireQuantumMemory(GetImageListLength(
179 fx_info->images),sizeof(*fx_info->view));
180 if (fx_info->view == (CacheView **) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000181 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
cristya2262262011-03-11 02:50:37 +0000182 i=0;
cristy0ea377f2011-03-24 00:54:19 +0000183 next=GetFirstImageInList(fx_info->images);
184 for ( ; next != (Image *) NULL; next=next->next)
cristy3ed852e2009-09-05 21:47:34 +0000185 {
cristyd76c51e2011-03-26 00:21:26 +0000186 fx_info->view[i]=AcquireCacheView(next);
cristya2262262011-03-11 02:50:37 +0000187 i++;
cristy3ed852e2009-09-05 21:47:34 +0000188 }
189 fx_info->random_info=AcquireRandomInfo();
190 fx_info->expression=ConstantString(expression);
191 fx_info->file=stderr;
192 (void) SubstituteString(&fx_info->expression," ",""); /* compact string */
cristy37af0912011-05-23 16:09:42 +0000193 /*
194 Force right-to-left associativity for unary negation.
195 */
196 (void) SubstituteString(&fx_info->expression,"-","-1.0*");
cristy3ed852e2009-09-05 21:47:34 +0000197 if ((strstr(fx_info->expression,"e+") != (char *) NULL) ||
198 (strstr(fx_info->expression,"e-") != (char *) NULL))
199 {
200 /*
cristy116af162010-08-13 01:25:47 +0000201 Convert scientific notation.
cristy3ed852e2009-09-05 21:47:34 +0000202 */
cristy116af162010-08-13 01:25:47 +0000203 (void) SubstituteString(&fx_info->expression,"0e+","0**10^");
204 (void) SubstituteString(&fx_info->expression,"1e+","1**10^");
205 (void) SubstituteString(&fx_info->expression,"2e+","2**10^");
206 (void) SubstituteString(&fx_info->expression,"3e+","3**10^");
207 (void) SubstituteString(&fx_info->expression,"4e+","4**10^");
208 (void) SubstituteString(&fx_info->expression,"5e+","5**10^");
209 (void) SubstituteString(&fx_info->expression,"6e+","6**10^");
210 (void) SubstituteString(&fx_info->expression,"7e+","7**10^");
211 (void) SubstituteString(&fx_info->expression,"8e+","8**10^");
212 (void) SubstituteString(&fx_info->expression,"9e+","9**10^");
cristy2ffa8cd2011-05-24 12:45:55 +0000213 (void) SubstituteString(&fx_info->expression,"0e-1.0*","0**10^-");
cristy37af0912011-05-23 16:09:42 +0000214 (void) SubstituteString(&fx_info->expression,"1e-1.0*","1**10^-");
215 (void) SubstituteString(&fx_info->expression,"2e-1.0*","2**10^-");
216 (void) SubstituteString(&fx_info->expression,"3e-1.0*","3**10^-");
217 (void) SubstituteString(&fx_info->expression,"4e-1.0*","4**10^-");
218 (void) SubstituteString(&fx_info->expression,"5e-1.0*","5**10^-");
219 (void) SubstituteString(&fx_info->expression,"6e-1.0*","6**10^-");
220 (void) SubstituteString(&fx_info->expression,"7e-1.0*","7**10^-");
221 (void) SubstituteString(&fx_info->expression,"8e-1.0*","8**10^-");
222 (void) SubstituteString(&fx_info->expression,"9e-1.0*","9**10^-");
cristy3ed852e2009-09-05 21:47:34 +0000223 }
cristy37af0912011-05-23 16:09:42 +0000224 if ((strstr(fx_info->expression,"E+") != (char *) NULL) ||
225 (strstr(fx_info->expression,"E-") != (char *) NULL))
226 {
227 /*
228 Convert scientific notation.
229 */
230 (void) SubstituteString(&fx_info->expression,"0E+","0**10^");
231 (void) SubstituteString(&fx_info->expression,"1E+","1**10^");
232 (void) SubstituteString(&fx_info->expression,"2E+","2**10^");
233 (void) SubstituteString(&fx_info->expression,"3E+","3**10^");
234 (void) SubstituteString(&fx_info->expression,"4E+","4**10^");
235 (void) SubstituteString(&fx_info->expression,"5E+","5**10^");
236 (void) SubstituteString(&fx_info->expression,"6E+","6**10^");
237 (void) SubstituteString(&fx_info->expression,"7E+","7**10^");
238 (void) SubstituteString(&fx_info->expression,"8E+","8**10^");
239 (void) SubstituteString(&fx_info->expression,"9E+","9**10^");
cristy2ffa8cd2011-05-24 12:45:55 +0000240 (void) SubstituteString(&fx_info->expression,"0E-1.0*","0**10^-");
cristy37af0912011-05-23 16:09:42 +0000241 (void) SubstituteString(&fx_info->expression,"1E-1.0*","1**10^-");
242 (void) SubstituteString(&fx_info->expression,"2E-1.0*","2**10^-");
243 (void) SubstituteString(&fx_info->expression,"3E-1.0*","3**10^-");
244 (void) SubstituteString(&fx_info->expression,"4E-1.0*","4**10^-");
245 (void) SubstituteString(&fx_info->expression,"5E-1.0*","5**10^-");
246 (void) SubstituteString(&fx_info->expression,"6E-1.0*","6**10^-");
247 (void) SubstituteString(&fx_info->expression,"7E-1.0*","7**10^-");
248 (void) SubstituteString(&fx_info->expression,"8E-1.0*","8**10^-");
249 (void) SubstituteString(&fx_info->expression,"9E-1.0*","9**10^-");
250 }
cristy8b8a3ae2010-10-23 18:49:46 +0000251 /*
cristy3ed852e2009-09-05 21:47:34 +0000252 Convert complex to simple operators.
253 */
254 fx_op[1]='\0';
255 *fx_op=(char) LeftShiftOperator;
256 (void) SubstituteString(&fx_info->expression,"<<",fx_op);
257 *fx_op=(char) RightShiftOperator;
258 (void) SubstituteString(&fx_info->expression,">>",fx_op);
259 *fx_op=(char) LessThanEqualOperator;
260 (void) SubstituteString(&fx_info->expression,"<=",fx_op);
261 *fx_op=(char) GreaterThanEqualOperator;
262 (void) SubstituteString(&fx_info->expression,">=",fx_op);
263 *fx_op=(char) EqualOperator;
264 (void) SubstituteString(&fx_info->expression,"==",fx_op);
265 *fx_op=(char) NotEqualOperator;
266 (void) SubstituteString(&fx_info->expression,"!=",fx_op);
267 *fx_op=(char) LogicalAndOperator;
268 (void) SubstituteString(&fx_info->expression,"&&",fx_op);
269 *fx_op=(char) LogicalOrOperator;
270 (void) SubstituteString(&fx_info->expression,"||",fx_op);
cristy116af162010-08-13 01:25:47 +0000271 *fx_op=(char) ExponentialNotation;
272 (void) SubstituteString(&fx_info->expression,"**",fx_op);
cristy3ed852e2009-09-05 21:47:34 +0000273 return(fx_info);
274}
275
276/*
277%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
278% %
279% %
280% %
281% A d d N o i s e I m a g e %
282% %
283% %
284% %
285%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
286%
287% AddNoiseImage() adds random noise to the image.
288%
289% The format of the AddNoiseImage method is:
290%
291% Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
cristy9ed1f812011-10-08 02:00:08 +0000292% const double attenuate,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000293%
294% A description of each parameter follows:
295%
296% o image: the image.
297%
298% o channel: the channel type.
299%
300% o noise_type: The type of noise: Uniform, Gaussian, Multiplicative,
301% Impulse, Laplacian, or Poisson.
302%
cristy9ed1f812011-10-08 02:00:08 +0000303% o attenuate: attenuate the random distribution.
304%
cristy3ed852e2009-09-05 21:47:34 +0000305% o exception: return any errors or warnings in this structure.
306%
307*/
cristy9ed1f812011-10-08 02:00:08 +0000308MagickExport Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
309 const double attenuate,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000310{
311#define AddNoiseImageTag "AddNoise/Image"
312
cristyfa112112010-01-04 17:48:07 +0000313 CacheView
314 *image_view,
315 *noise_view;
316
cristy3ed852e2009-09-05 21:47:34 +0000317 Image
318 *noise_image;
319
cristy3ed852e2009-09-05 21:47:34 +0000320 MagickBooleanType
321 status;
322
cristybb503372010-05-27 20:51:26 +0000323 MagickOffsetType
324 progress;
325
cristy3ed852e2009-09-05 21:47:34 +0000326 RandomInfo
cristyfa112112010-01-04 17:48:07 +0000327 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +0000328
cristybb503372010-05-27 20:51:26 +0000329 ssize_t
330 y;
331
cristy3ed852e2009-09-05 21:47:34 +0000332 /*
333 Initialize noise image attributes.
334 */
335 assert(image != (const Image *) NULL);
336 assert(image->signature == MagickSignature);
337 if (image->debug != MagickFalse)
338 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
339 assert(exception != (ExceptionInfo *) NULL);
340 assert(exception->signature == MagickSignature);
cristyb2145892011-10-10 00:55:32 +0000341 noise_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000342 if (noise_image == (Image *) NULL)
343 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000344 if (SetImageStorageClass(noise_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000345 {
cristy3ed852e2009-09-05 21:47:34 +0000346 noise_image=DestroyImage(noise_image);
347 return((Image *) NULL);
348 }
349 /*
350 Add noise in each row.
351 */
cristy3ed852e2009-09-05 21:47:34 +0000352 status=MagickTrue;
353 progress=0;
354 random_info=AcquireRandomInfoThreadSet();
355 image_view=AcquireCacheView(image);
356 noise_view=AcquireCacheView(noise_image);
cristy319a1e72010-02-21 15:13:11 +0000357#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyb5d5f722009-11-04 03:03:49 +0000358 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000359#endif
cristybb503372010-05-27 20:51:26 +0000360 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000361 {
cristy5c9e6f22010-09-17 17:31:01 +0000362 const int
363 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +0000364
cristy3ed852e2009-09-05 21:47:34 +0000365 MagickBooleanType
366 sync;
367
cristy4c08aed2011-07-01 19:47:50 +0000368 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000369 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000370
cristybb503372010-05-27 20:51:26 +0000371 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000372 x;
373
cristy4c08aed2011-07-01 19:47:50 +0000374 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000375 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000376
377 if (status == MagickFalse)
378 continue;
379 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
380 q=GetCacheViewAuthenticPixels(noise_view,0,y,noise_image->columns,1,
381 exception);
cristy4c08aed2011-07-01 19:47:50 +0000382 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000383 {
384 status=MagickFalse;
385 continue;
386 }
cristybb503372010-05-27 20:51:26 +0000387 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000388 {
cristy850b3072011-10-08 01:38:05 +0000389 register ssize_t
390 i;
391
392 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
393 {
394 PixelChannel
395 channel;
396
397 PixelTrait
398 noise_traits,
399 traits;
400
401 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
402 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
403 noise_traits=GetPixelChannelMapTraits(noise_image,channel);
404 if ((traits == UndefinedPixelTrait) ||
405 (noise_traits == UndefinedPixelTrait))
406 continue;
cristy9eeedea2011-11-02 19:04:05 +0000407 if ((noise_traits & CopyPixelTrait) != 0)
cristyb2145892011-10-10 00:55:32 +0000408 {
409 SetPixelChannel(noise_image,channel,p[i],q);
410 continue;
411 }
cristy850b3072011-10-08 01:38:05 +0000412 SetPixelChannel(noise_image,channel,ClampToQuantum(
413 GenerateDifferentialNoise(random_info[id],p[i],noise_type,attenuate)),
414 q);
415 }
cristyed231572011-07-14 02:18:59 +0000416 p+=GetPixelChannels(image);
417 q+=GetPixelChannels(noise_image);
cristy3ed852e2009-09-05 21:47:34 +0000418 }
419 sync=SyncCacheViewAuthenticPixels(noise_view,exception);
420 if (sync == MagickFalse)
421 status=MagickFalse;
422 if (image->progress_monitor != (MagickProgressMonitor) NULL)
423 {
424 MagickBooleanType
425 proceed;
426
cristyb5d5f722009-11-04 03:03:49 +0000427#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy319a1e72010-02-21 15:13:11 +0000428 #pragma omp critical (MagickCore_AddNoiseImage)
cristy3ed852e2009-09-05 21:47:34 +0000429#endif
430 proceed=SetImageProgress(image,AddNoiseImageTag,progress++,
431 image->rows);
432 if (proceed == MagickFalse)
433 status=MagickFalse;
434 }
435 }
436 noise_view=DestroyCacheView(noise_view);
437 image_view=DestroyCacheView(image_view);
438 random_info=DestroyRandomInfoThreadSet(random_info);
439 if (status == MagickFalse)
440 noise_image=DestroyImage(noise_image);
441 return(noise_image);
442}
443
444/*
445%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
446% %
447% %
448% %
449% B l u e S h i f t I m a g e %
450% %
451% %
452% %
453%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
454%
455% BlueShiftImage() mutes the colors of the image to simulate a scene at
456% nighttime in the moonlight.
457%
458% The format of the BlueShiftImage method is:
459%
460% Image *BlueShiftImage(const Image *image,const double factor,
461% ExceptionInfo *exception)
462%
463% A description of each parameter follows:
464%
465% o image: the image.
466%
467% o factor: the shift factor.
468%
469% o exception: return any errors or warnings in this structure.
470%
471*/
472MagickExport Image *BlueShiftImage(const Image *image,const double factor,
473 ExceptionInfo *exception)
474{
475#define BlueShiftImageTag "BlueShift/Image"
476
cristyc4c8d132010-01-07 01:58:38 +0000477 CacheView
478 *image_view,
479 *shift_view;
480
cristy3ed852e2009-09-05 21:47:34 +0000481 Image
482 *shift_image;
483
cristy3ed852e2009-09-05 21:47:34 +0000484 MagickBooleanType
485 status;
486
cristybb503372010-05-27 20:51:26 +0000487 MagickOffsetType
488 progress;
489
490 ssize_t
491 y;
492
cristy3ed852e2009-09-05 21:47:34 +0000493 /*
494 Allocate blue shift image.
495 */
496 assert(image != (const Image *) NULL);
497 assert(image->signature == MagickSignature);
498 if (image->debug != MagickFalse)
499 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
500 assert(exception != (ExceptionInfo *) NULL);
501 assert(exception->signature == MagickSignature);
502 shift_image=CloneImage(image,image->columns,image->rows,MagickTrue,
503 exception);
504 if (shift_image == (Image *) NULL)
505 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000506 if (SetImageStorageClass(shift_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000507 {
cristy3ed852e2009-09-05 21:47:34 +0000508 shift_image=DestroyImage(shift_image);
509 return((Image *) NULL);
510 }
511 /*
512 Blue-shift DirectClass image.
513 */
514 status=MagickTrue;
515 progress=0;
516 image_view=AcquireCacheView(image);
517 shift_view=AcquireCacheView(shift_image);
cristy319a1e72010-02-21 15:13:11 +0000518#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyb5d5f722009-11-04 03:03:49 +0000519 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000520#endif
cristybb503372010-05-27 20:51:26 +0000521 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000522 {
523 MagickBooleanType
524 sync;
525
cristy4c08aed2011-07-01 19:47:50 +0000526 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000527 pixel;
528
529 Quantum
530 quantum;
531
cristy4c08aed2011-07-01 19:47:50 +0000532 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000533 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000534
cristybb503372010-05-27 20:51:26 +0000535 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000536 x;
537
cristy4c08aed2011-07-01 19:47:50 +0000538 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000539 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000540
541 if (status == MagickFalse)
542 continue;
543 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
544 q=QueueCacheViewAuthenticPixels(shift_view,0,y,shift_image->columns,1,
545 exception);
cristy4c08aed2011-07-01 19:47:50 +0000546 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000547 {
548 status=MagickFalse;
549 continue;
550 }
cristybb503372010-05-27 20:51:26 +0000551 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000552 {
cristy4c08aed2011-07-01 19:47:50 +0000553 quantum=GetPixelRed(image,p);
554 if (GetPixelGreen(image,p) < quantum)
555 quantum=GetPixelGreen(image,p);
556 if (GetPixelBlue(image,p) < quantum)
557 quantum=GetPixelBlue(image,p);
558 pixel.red=0.5*(GetPixelRed(image,p)+factor*quantum);
559 pixel.green=0.5*(GetPixelGreen(image,p)+factor*quantum);
560 pixel.blue=0.5*(GetPixelBlue(image,p)+factor*quantum);
561 quantum=GetPixelRed(image,p);
562 if (GetPixelGreen(image,p) > quantum)
563 quantum=GetPixelGreen(image,p);
564 if (GetPixelBlue(image,p) > quantum)
565 quantum=GetPixelBlue(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000566 pixel.red=0.5*(pixel.red+factor*quantum);
567 pixel.green=0.5*(pixel.green+factor*quantum);
568 pixel.blue=0.5*(pixel.blue+factor*quantum);
cristy4c08aed2011-07-01 19:47:50 +0000569 SetPixelRed(shift_image,ClampToQuantum(pixel.red),q);
570 SetPixelGreen(shift_image,ClampToQuantum(pixel.green),q);
571 SetPixelBlue(shift_image,ClampToQuantum(pixel.blue),q);
cristyed231572011-07-14 02:18:59 +0000572 p+=GetPixelChannels(image);
573 q+=GetPixelChannels(shift_image);
cristy3ed852e2009-09-05 21:47:34 +0000574 }
575 sync=SyncCacheViewAuthenticPixels(shift_view,exception);
576 if (sync == MagickFalse)
577 status=MagickFalse;
578 if (image->progress_monitor != (MagickProgressMonitor) NULL)
579 {
580 MagickBooleanType
581 proceed;
582
cristy319a1e72010-02-21 15:13:11 +0000583#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000584 #pragma omp critical (MagickCore_BlueShiftImage)
585#endif
586 proceed=SetImageProgress(image,BlueShiftImageTag,progress++,
587 image->rows);
588 if (proceed == MagickFalse)
589 status=MagickFalse;
590 }
591 }
592 image_view=DestroyCacheView(image_view);
593 shift_view=DestroyCacheView(shift_view);
594 if (status == MagickFalse)
595 shift_image=DestroyImage(shift_image);
596 return(shift_image);
597}
598
599/*
600%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
601% %
602% %
603% %
604% C h a r c o a l I m a g e %
605% %
606% %
607% %
608%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
609%
610% CharcoalImage() creates a new image that is a copy of an existing one with
611% the edge highlighted. It allocates the memory necessary for the new Image
612% structure and returns a pointer to the new image.
613%
614% The format of the CharcoalImage method is:
615%
616% Image *CharcoalImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +0000617% const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000618%
619% A description of each parameter follows:
620%
621% o image: the image.
622%
623% o radius: the radius of the pixel neighborhood.
624%
625% o sigma: the standard deviation of the Gaussian, in pixels.
626%
cristy05c0c9a2011-09-05 23:16:13 +0000627% o bias: the bias.
628%
cristy3ed852e2009-09-05 21:47:34 +0000629% o exception: return any errors or warnings in this structure.
630%
631*/
632MagickExport Image *CharcoalImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +0000633 const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000634{
635 Image
636 *charcoal_image,
637 *clone_image,
638 *edge_image;
639
640 assert(image != (Image *) NULL);
641 assert(image->signature == MagickSignature);
642 if (image->debug != MagickFalse)
643 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
644 assert(exception != (ExceptionInfo *) NULL);
645 assert(exception->signature == MagickSignature);
646 clone_image=CloneImage(image,0,0,MagickTrue,exception);
647 if (clone_image == (Image *) NULL)
648 return((Image *) NULL);
cristy018f07f2011-09-04 21:15:19 +0000649 (void) SetImageType(clone_image,GrayscaleType,exception);
cristy8ae632d2011-09-05 17:29:53 +0000650 edge_image=EdgeImage(clone_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +0000651 clone_image=DestroyImage(clone_image);
652 if (edge_image == (Image *) NULL)
653 return((Image *) NULL);
cristy05c0c9a2011-09-05 23:16:13 +0000654 charcoal_image=BlurImage(edge_image,radius,sigma,bias,exception);
cristy3ed852e2009-09-05 21:47:34 +0000655 edge_image=DestroyImage(edge_image);
656 if (charcoal_image == (Image *) NULL)
657 return((Image *) NULL);
cristye23ec9d2011-08-16 18:15:40 +0000658 (void) NormalizeImage(charcoal_image,exception);
cristyb3e7c6c2011-07-24 01:43:55 +0000659 (void) NegateImage(charcoal_image,MagickFalse,exception);
cristy018f07f2011-09-04 21:15:19 +0000660 (void) SetImageType(charcoal_image,GrayscaleType,exception);
cristy3ed852e2009-09-05 21:47:34 +0000661 return(charcoal_image);
662}
663
664/*
665%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
666% %
667% %
668% %
669% C o l o r i z e I m a g e %
670% %
671% %
672% %
673%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
674%
675% ColorizeImage() blends the fill color with each pixel in the image.
676% A percentage blend is specified with opacity. Control the application
677% of different color components by specifying a different percentage for
678% each component (e.g. 90/100/10 is 90% red, 100% green, and 10% blue).
679%
680% The format of the ColorizeImage method is:
681%
cristyc7e6ff62011-10-03 13:46:11 +0000682% Image *ColorizeImage(const Image *image,const char *blend,
683% const PixelInfo *colorize,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000684%
685% A description of each parameter follows:
686%
687% o image: the image.
688%
cristyc7e6ff62011-10-03 13:46:11 +0000689% o blend: A character string indicating the level of blending as a
cristy3ed852e2009-09-05 21:47:34 +0000690% percentage.
691%
692% o colorize: A color value.
693%
694% o exception: return any errors or warnings in this structure.
695%
696*/
cristyc7e6ff62011-10-03 13:46:11 +0000697MagickExport Image *ColorizeImage(const Image *image,const char *blend,
698 const PixelInfo *colorize,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000699{
700#define ColorizeImageTag "Colorize/Image"
701
cristyc4c8d132010-01-07 01:58:38 +0000702 CacheView
703 *colorize_view,
704 *image_view;
705
cristy3ed852e2009-09-05 21:47:34 +0000706 GeometryInfo
707 geometry_info;
708
709 Image
710 *colorize_image;
711
cristy3ed852e2009-09-05 21:47:34 +0000712 MagickBooleanType
713 status;
714
cristybb503372010-05-27 20:51:26 +0000715 MagickOffsetType
716 progress;
717
cristy3ed852e2009-09-05 21:47:34 +0000718 MagickStatusType
719 flags;
720
cristyc7e6ff62011-10-03 13:46:11 +0000721 PixelInfo
722 pixel;
723
cristybb503372010-05-27 20:51:26 +0000724 ssize_t
725 y;
726
cristy3ed852e2009-09-05 21:47:34 +0000727 /*
728 Allocate colorized image.
729 */
730 assert(image != (const Image *) NULL);
731 assert(image->signature == MagickSignature);
732 if (image->debug != MagickFalse)
733 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
734 assert(exception != (ExceptionInfo *) NULL);
735 assert(exception->signature == MagickSignature);
736 colorize_image=CloneImage(image,image->columns,image->rows,MagickTrue,
737 exception);
738 if (colorize_image == (Image *) NULL)
739 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000740 if (SetImageStorageClass(colorize_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000741 {
cristy3ed852e2009-09-05 21:47:34 +0000742 colorize_image=DestroyImage(colorize_image);
743 return((Image *) NULL);
744 }
cristyc7e6ff62011-10-03 13:46:11 +0000745 if (blend == (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000746 return(colorize_image);
747 /*
748 Determine RGB values of the pen color.
749 */
cristyc7e6ff62011-10-03 13:46:11 +0000750 GetPixelInfo(image,&pixel);
cristyb817c3f2011-10-03 14:00:35 +0000751 flags=ParseGeometry(blend,&geometry_info);
cristy3ed852e2009-09-05 21:47:34 +0000752 pixel.red=geometry_info.rho;
753 pixel.green=geometry_info.rho;
754 pixel.blue=geometry_info.rho;
cristyc7e6ff62011-10-03 13:46:11 +0000755 pixel.alpha=100.0;
cristy3ed852e2009-09-05 21:47:34 +0000756 if ((flags & SigmaValue) != 0)
757 pixel.green=geometry_info.sigma;
758 if ((flags & XiValue) != 0)
759 pixel.blue=geometry_info.xi;
760 if ((flags & PsiValue) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000761 pixel.alpha=geometry_info.psi;
cristyc7e6ff62011-10-03 13:46:11 +0000762 if (pixel.colorspace == CMYKColorspace)
763 {
764 pixel.black=geometry_info.rho;
765 if ((flags & PsiValue) != 0)
766 pixel.black=geometry_info.psi;
767 if ((flags & ChiValue) != 0)
768 pixel.alpha=geometry_info.chi;
769 }
cristy3ed852e2009-09-05 21:47:34 +0000770 /*
771 Colorize DirectClass image.
772 */
773 status=MagickTrue;
774 progress=0;
775 image_view=AcquireCacheView(image);
776 colorize_view=AcquireCacheView(colorize_image);
cristy319a1e72010-02-21 15:13:11 +0000777#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyb5d5f722009-11-04 03:03:49 +0000778 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000779#endif
cristybb503372010-05-27 20:51:26 +0000780 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000781 {
782 MagickBooleanType
783 sync;
784
cristy4c08aed2011-07-01 19:47:50 +0000785 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000786 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000787
cristybb503372010-05-27 20:51:26 +0000788 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000789 x;
790
cristy4c08aed2011-07-01 19:47:50 +0000791 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000792 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000793
794 if (status == MagickFalse)
795 continue;
796 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
797 q=QueueCacheViewAuthenticPixels(colorize_view,0,y,colorize_image->columns,1,
798 exception);
cristy4c08aed2011-07-01 19:47:50 +0000799 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000800 {
801 status=MagickFalse;
802 continue;
803 }
cristybb503372010-05-27 20:51:26 +0000804 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000805 {
cristyc7e6ff62011-10-03 13:46:11 +0000806 register ssize_t
807 i;
808
809 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
810 {
811 PixelChannel
812 channel;
813
814 PixelTrait
815 colorize_traits,
816 traits;
817
818 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
819 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
820 colorize_traits=GetPixelChannelMapTraits(colorize_image,channel);
821 if ((traits == UndefinedPixelTrait) ||
822 (colorize_traits == UndefinedPixelTrait))
823 continue;
824 if ((colorize_traits & CopyPixelTrait) != 0)
825 {
826 SetPixelChannel(colorize_image,channel,p[i],q);
827 continue;
828 }
829 switch (channel)
830 {
831 case RedPixelChannel:
832 {
833 SetPixelChannel(colorize_image,channel,ClampToQuantum((p[i]*
834 (100.0-pixel.red)+colorize->red*pixel.red)/100.0),q);
835 break;
836 }
837 case GreenPixelChannel:
838 {
839 SetPixelChannel(colorize_image,channel,ClampToQuantum((p[i]*
840 (100.0-pixel.green)+colorize->green*pixel.green)/100.0),q);
841 break;
842 }
843 case BluePixelChannel:
844 {
845 SetPixelChannel(colorize_image,channel,ClampToQuantum((p[i]*
846 (100.0-pixel.blue)+colorize->blue*pixel.blue)/100.0),q);
847 break;
848 }
849 case BlackPixelChannel:
850 {
851 SetPixelChannel(colorize_image,channel,ClampToQuantum((p[i]*
852 (100.0-pixel.black)+colorize->black*pixel.black)/100.0),q);
853 break;
854 }
855 case AlphaPixelChannel:
856 {
857 SetPixelChannel(colorize_image,channel,ClampToQuantum((p[i]*
858 (100.0-pixel.alpha)+colorize->alpha*pixel.alpha)/100.0),q);
859 break;
860 }
861 default:
862 {
863 SetPixelChannel(colorize_image,channel,p[i],q);
864 break;
865 }
866 }
867 }
cristyed231572011-07-14 02:18:59 +0000868 p+=GetPixelChannels(image);
869 q+=GetPixelChannels(colorize_image);
cristy3ed852e2009-09-05 21:47:34 +0000870 }
871 sync=SyncCacheViewAuthenticPixels(colorize_view,exception);
872 if (sync == MagickFalse)
873 status=MagickFalse;
874 if (image->progress_monitor != (MagickProgressMonitor) NULL)
875 {
876 MagickBooleanType
877 proceed;
878
cristy319a1e72010-02-21 15:13:11 +0000879#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000880 #pragma omp critical (MagickCore_ColorizeImage)
881#endif
882 proceed=SetImageProgress(image,ColorizeImageTag,progress++,image->rows);
883 if (proceed == MagickFalse)
884 status=MagickFalse;
885 }
886 }
887 image_view=DestroyCacheView(image_view);
888 colorize_view=DestroyCacheView(colorize_view);
889 if (status == MagickFalse)
890 colorize_image=DestroyImage(colorize_image);
891 return(colorize_image);
892}
893
894/*
895%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
896% %
897% %
898% %
cristye6365592010-04-02 17:31:23 +0000899% C o l o r M a t r i x I m a g e %
900% %
901% %
902% %
903%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
904%
905% ColorMatrixImage() applies color transformation to an image. This method
906% permits saturation changes, hue rotation, luminance to alpha, and various
907% other effects. Although variable-sized transformation matrices can be used,
908% typically one uses a 5x5 matrix for an RGBA image and a 6x6 for CMYKA
909% (or RGBA with offsets). The matrix is similar to those used by Adobe Flash
910% except offsets are in column 6 rather than 5 (in support of CMYKA images)
911% and offsets are normalized (divide Flash offset by 255).
912%
913% The format of the ColorMatrixImage method is:
914%
915% Image *ColorMatrixImage(const Image *image,
916% const KernelInfo *color_matrix,ExceptionInfo *exception)
917%
918% A description of each parameter follows:
919%
920% o image: the image.
921%
922% o color_matrix: the color matrix.
923%
924% o exception: return any errors or warnings in this structure.
925%
926*/
927MagickExport Image *ColorMatrixImage(const Image *image,
928 const KernelInfo *color_matrix,ExceptionInfo *exception)
929{
930#define ColorMatrixImageTag "ColorMatrix/Image"
931
932 CacheView
933 *color_view,
934 *image_view;
935
936 double
937 ColorMatrix[6][6] =
938 {
939 { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
940 { 0.0, 1.0, 0.0, 0.0, 0.0, 0.0 },
941 { 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 },
942 { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0 },
943 { 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 },
944 { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 }
945 };
946
947 Image
948 *color_image;
949
cristye6365592010-04-02 17:31:23 +0000950 MagickBooleanType
951 status;
952
cristybb503372010-05-27 20:51:26 +0000953 MagickOffsetType
954 progress;
955
956 register ssize_t
cristye6365592010-04-02 17:31:23 +0000957 i;
958
cristybb503372010-05-27 20:51:26 +0000959 ssize_t
960 u,
961 v,
962 y;
963
cristye6365592010-04-02 17:31:23 +0000964 /*
965 Create color matrix.
966 */
967 assert(image != (Image *) NULL);
968 assert(image->signature == MagickSignature);
969 if (image->debug != MagickFalse)
970 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
971 assert(exception != (ExceptionInfo *) NULL);
972 assert(exception->signature == MagickSignature);
973 i=0;
cristybb503372010-05-27 20:51:26 +0000974 for (v=0; v < (ssize_t) color_matrix->height; v++)
975 for (u=0; u < (ssize_t) color_matrix->width; u++)
cristye6365592010-04-02 17:31:23 +0000976 {
977 if ((v < 6) && (u < 6))
978 ColorMatrix[v][u]=color_matrix->values[i];
979 i++;
980 }
981 /*
982 Initialize color image.
983 */
cristy12550e62010-06-07 12:46:40 +0000984 color_image=CloneImage(image,0,0,MagickTrue,exception);
cristye6365592010-04-02 17:31:23 +0000985 if (color_image == (Image *) NULL)
986 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000987 if (SetImageStorageClass(color_image,DirectClass,exception) == MagickFalse)
cristye6365592010-04-02 17:31:23 +0000988 {
cristye6365592010-04-02 17:31:23 +0000989 color_image=DestroyImage(color_image);
990 return((Image *) NULL);
991 }
992 if (image->debug != MagickFalse)
993 {
994 char
995 format[MaxTextExtent],
996 *message;
997
998 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
999 " ColorMatrix image with color matrix:");
1000 message=AcquireString("");
1001 for (v=0; v < 6; v++)
1002 {
1003 *message='\0';
cristyb51dff52011-05-19 16:55:47 +00001004 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
cristye6365592010-04-02 17:31:23 +00001005 (void) ConcatenateString(&message,format);
1006 for (u=0; u < 6; u++)
1007 {
cristyb51dff52011-05-19 16:55:47 +00001008 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",
cristye6365592010-04-02 17:31:23 +00001009 ColorMatrix[v][u]);
1010 (void) ConcatenateString(&message,format);
1011 }
1012 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
1013 }
1014 message=DestroyString(message);
1015 }
1016 /*
1017 ColorMatrix image.
1018 */
1019 status=MagickTrue;
1020 progress=0;
1021 image_view=AcquireCacheView(image);
1022 color_view=AcquireCacheView(color_image);
1023#if defined(MAGICKCORE_OPENMP_SUPPORT)
1024 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1025#endif
cristybb503372010-05-27 20:51:26 +00001026 for (y=0; y < (ssize_t) image->rows; y++)
cristye6365592010-04-02 17:31:23 +00001027 {
1028 MagickRealType
1029 pixel;
1030
cristy4c08aed2011-07-01 19:47:50 +00001031 register const Quantum
cristye6365592010-04-02 17:31:23 +00001032 *restrict p;
1033
cristy4c08aed2011-07-01 19:47:50 +00001034 register Quantum
1035 *restrict q;
1036
cristybb503372010-05-27 20:51:26 +00001037 register ssize_t
cristye6365592010-04-02 17:31:23 +00001038 x;
1039
cristye6365592010-04-02 17:31:23 +00001040 if (status == MagickFalse)
1041 continue;
1042 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1043 q=GetCacheViewAuthenticPixels(color_view,0,y,color_image->columns,1,
1044 exception);
cristy4c08aed2011-07-01 19:47:50 +00001045 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristye6365592010-04-02 17:31:23 +00001046 {
1047 status=MagickFalse;
1048 continue;
1049 }
cristybb503372010-05-27 20:51:26 +00001050 for (x=0; x < (ssize_t) image->columns; x++)
cristye6365592010-04-02 17:31:23 +00001051 {
cristybb503372010-05-27 20:51:26 +00001052 register ssize_t
cristye6365592010-04-02 17:31:23 +00001053 v;
1054
cristybb503372010-05-27 20:51:26 +00001055 size_t
cristye6365592010-04-02 17:31:23 +00001056 height;
1057
1058 height=color_matrix->height > 6 ? 6UL : color_matrix->height;
cristybb503372010-05-27 20:51:26 +00001059 for (v=0; v < (ssize_t) height; v++)
cristye6365592010-04-02 17:31:23 +00001060 {
cristy4c08aed2011-07-01 19:47:50 +00001061 pixel=ColorMatrix[v][0]*GetPixelRed(image,p)+ColorMatrix[v][1]*
1062 GetPixelGreen(image,p)+ColorMatrix[v][2]*GetPixelBlue(image,p);
cristye6365592010-04-02 17:31:23 +00001063 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00001064 pixel+=ColorMatrix[v][3]*GetPixelBlack(image,p);
1065 if (image->matte != MagickFalse)
1066 pixel+=ColorMatrix[v][4]*GetPixelAlpha(image,p);
cristye6365592010-04-02 17:31:23 +00001067 pixel+=QuantumRange*ColorMatrix[v][5];
1068 switch (v)
1069 {
cristy4c08aed2011-07-01 19:47:50 +00001070 case 0: SetPixelRed(color_image,ClampToQuantum(pixel),q); break;
1071 case 1: SetPixelGreen(color_image,ClampToQuantum(pixel),q); break;
1072 case 2: SetPixelBlue(color_image,ClampToQuantum(pixel),q); break;
cristye6365592010-04-02 17:31:23 +00001073 case 3:
1074 {
cristy4c08aed2011-07-01 19:47:50 +00001075 if (image->colorspace == CMYKColorspace)
1076 SetPixelBlack(color_image,ClampToQuantum(pixel),q);
cristye6365592010-04-02 17:31:23 +00001077 break;
1078 }
1079 case 4:
1080 {
cristy4c08aed2011-07-01 19:47:50 +00001081 if (image->matte != MagickFalse)
1082 SetPixelAlpha(color_image,ClampToQuantum(pixel),q);
cristye6365592010-04-02 17:31:23 +00001083 break;
1084 }
1085 }
1086 }
cristyed231572011-07-14 02:18:59 +00001087 p+=GetPixelChannels(image);
1088 q+=GetPixelChannels(color_image);
cristye6365592010-04-02 17:31:23 +00001089 }
1090 if (SyncCacheViewAuthenticPixels(color_view,exception) == MagickFalse)
1091 status=MagickFalse;
1092 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1093 {
1094 MagickBooleanType
1095 proceed;
1096
1097#if defined(MAGICKCORE_OPENMP_SUPPORT)
1098 #pragma omp critical (MagickCore_ColorMatrixImage)
1099#endif
1100 proceed=SetImageProgress(image,ColorMatrixImageTag,progress++,
1101 image->rows);
1102 if (proceed == MagickFalse)
1103 status=MagickFalse;
1104 }
1105 }
1106 color_view=DestroyCacheView(color_view);
1107 image_view=DestroyCacheView(image_view);
1108 if (status == MagickFalse)
1109 color_image=DestroyImage(color_image);
1110 return(color_image);
1111}
1112
1113/*
1114%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1115% %
1116% %
1117% %
cristy3ed852e2009-09-05 21:47:34 +00001118+ D e s t r o y F x I n f o %
1119% %
1120% %
1121% %
1122%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1123%
1124% DestroyFxInfo() deallocates memory associated with an FxInfo structure.
1125%
1126% The format of the DestroyFxInfo method is:
1127%
1128% ImageInfo *DestroyFxInfo(ImageInfo *fx_info)
1129%
1130% A description of each parameter follows:
1131%
1132% o fx_info: the fx info.
1133%
1134*/
cristy7832dc22011-09-05 01:21:53 +00001135MagickPrivate FxInfo *DestroyFxInfo(FxInfo *fx_info)
cristy3ed852e2009-09-05 21:47:34 +00001136{
cristybb503372010-05-27 20:51:26 +00001137 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001138 i;
1139
1140 fx_info->exception=DestroyExceptionInfo(fx_info->exception);
1141 fx_info->expression=DestroyString(fx_info->expression);
1142 fx_info->symbols=DestroySplayTree(fx_info->symbols);
1143 fx_info->colors=DestroySplayTree(fx_info->colors);
cristy0ea377f2011-03-24 00:54:19 +00001144 for (i=(ssize_t) GetImageListLength(fx_info->images)-1; i >= 0; i--)
cristyd76c51e2011-03-26 00:21:26 +00001145 fx_info->view[i]=DestroyCacheView(fx_info->view[i]);
1146 fx_info->view=(CacheView **) RelinquishMagickMemory(fx_info->view);
cristy3ed852e2009-09-05 21:47:34 +00001147 fx_info->random_info=DestroyRandomInfo(fx_info->random_info);
1148 fx_info=(FxInfo *) RelinquishMagickMemory(fx_info);
1149 return(fx_info);
1150}
1151
1152/*
1153%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1154% %
1155% %
1156% %
cristy3ed852e2009-09-05 21:47:34 +00001157+ 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 %
1158% %
1159% %
1160% %
1161%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1162%
1163% FxEvaluateChannelExpression() evaluates an expression and returns the
1164% results.
1165%
1166% The format of the FxEvaluateExpression method is:
1167%
1168% MagickRealType FxEvaluateChannelExpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00001169% const PixelChannel channel,const ssize_t x,const ssize_t y,
cristy3ed852e2009-09-05 21:47:34 +00001170% MagickRealType *alpha,Exceptioninfo *exception)
1171% MagickRealType FxEvaluateExpression(FxInfo *fx_info,
1172% MagickRealType *alpha,Exceptioninfo *exception)
1173%
1174% A description of each parameter follows:
1175%
1176% o fx_info: the fx info.
1177%
1178% o channel: the channel.
1179%
1180% o x,y: the pixel position.
1181%
1182% o alpha: the result.
1183%
1184% o exception: return any errors or warnings in this structure.
1185%
1186*/
1187
cristy351842f2010-03-07 15:27:38 +00001188static inline double MagickMax(const double x,const double y)
1189{
1190 if (x > y)
1191 return(x);
1192 return(y);
1193}
1194
1195static inline double MagickMin(const double x,const double y)
1196{
1197 if (x < y)
1198 return(x);
1199 return(y);
1200}
1201
cristy3ed852e2009-09-05 21:47:34 +00001202static MagickRealType FxChannelStatistics(FxInfo *fx_info,const Image *image,
cristy0568ffc2011-07-25 16:54:14 +00001203 PixelChannel channel,const char *symbol,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001204{
1205 char
1206 key[MaxTextExtent],
1207 statistic[MaxTextExtent];
1208
1209 const char
1210 *value;
1211
1212 register const char
1213 *p;
1214
1215 for (p=symbol; (*p != '.') && (*p != '\0'); p++) ;
1216 if (*p == '.')
1217 switch (*++p) /* e.g. depth.r */
1218 {
cristy541ae572011-08-05 19:08:59 +00001219 case 'r': channel=RedPixelChannel; break;
1220 case 'g': channel=GreenPixelChannel; break;
1221 case 'b': channel=BluePixelChannel; break;
1222 case 'c': channel=CyanPixelChannel; break;
1223 case 'm': channel=MagentaPixelChannel; break;
1224 case 'y': channel=YellowPixelChannel; break;
1225 case 'k': channel=BlackPixelChannel; break;
cristy3ed852e2009-09-05 21:47:34 +00001226 default: break;
1227 }
cristyb51dff52011-05-19 16:55:47 +00001228 (void) FormatLocaleString(key,MaxTextExtent,"%p.%.20g.%s",(void *) image,
cristye8c25f92010-06-03 00:53:06 +00001229 (double) channel,symbol);
cristy3ed852e2009-09-05 21:47:34 +00001230 value=(const char *) GetValueFromSplayTree(fx_info->symbols,key);
1231 if (value != (const char *) NULL)
cristyc1acd842011-05-19 23:05:47 +00001232 return(QuantumScale*InterpretLocaleValue(value,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001233 (void) DeleteNodeFromSplayTree(fx_info->symbols,key);
1234 if (LocaleNCompare(symbol,"depth",5) == 0)
1235 {
cristybb503372010-05-27 20:51:26 +00001236 size_t
cristy3ed852e2009-09-05 21:47:34 +00001237 depth;
1238
cristyfefab1b2011-07-05 00:33:22 +00001239 depth=GetImageDepth(image,exception);
1240 (void) FormatLocaleString(statistic,MaxTextExtent,"%.20g",(double) depth);
cristy3ed852e2009-09-05 21:47:34 +00001241 }
1242 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1243 {
1244 double
1245 kurtosis,
1246 skewness;
1247
cristyd42d9952011-07-08 14:21:50 +00001248 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
cristyb51dff52011-05-19 16:55:47 +00001249 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",kurtosis);
cristy3ed852e2009-09-05 21:47:34 +00001250 }
1251 if (LocaleNCompare(symbol,"maxima",6) == 0)
1252 {
1253 double
1254 maxima,
1255 minima;
1256
cristyd42d9952011-07-08 14:21:50 +00001257 (void) GetImageRange(image,&minima,&maxima,exception);
cristyb51dff52011-05-19 16:55:47 +00001258 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",maxima);
cristy3ed852e2009-09-05 21:47:34 +00001259 }
1260 if (LocaleNCompare(symbol,"mean",4) == 0)
1261 {
1262 double
1263 mean,
1264 standard_deviation;
1265
cristyd42d9952011-07-08 14:21:50 +00001266 (void) GetImageMean(image,&mean,&standard_deviation,exception);
cristyb51dff52011-05-19 16:55:47 +00001267 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",mean);
cristy3ed852e2009-09-05 21:47:34 +00001268 }
1269 if (LocaleNCompare(symbol,"minima",6) == 0)
1270 {
1271 double
1272 maxima,
1273 minima;
1274
cristyd42d9952011-07-08 14:21:50 +00001275 (void) GetImageRange(image,&minima,&maxima,exception);
cristyb51dff52011-05-19 16:55:47 +00001276 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",minima);
cristy3ed852e2009-09-05 21:47:34 +00001277 }
1278 if (LocaleNCompare(symbol,"skewness",8) == 0)
1279 {
1280 double
1281 kurtosis,
1282 skewness;
1283
cristyd42d9952011-07-08 14:21:50 +00001284 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
cristyb51dff52011-05-19 16:55:47 +00001285 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",skewness);
cristy3ed852e2009-09-05 21:47:34 +00001286 }
1287 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1288 {
1289 double
1290 mean,
1291 standard_deviation;
1292
cristyd42d9952011-07-08 14:21:50 +00001293 (void) GetImageMean(image,&mean,&standard_deviation,exception);
cristyb51dff52011-05-19 16:55:47 +00001294 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",
cristy3ed852e2009-09-05 21:47:34 +00001295 standard_deviation);
1296 }
1297 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(key),
1298 ConstantString(statistic));
cristyc1acd842011-05-19 23:05:47 +00001299 return(QuantumScale*InterpretLocaleValue(statistic,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001300}
1301
1302static MagickRealType
cristy0568ffc2011-07-25 16:54:14 +00001303 FxEvaluateSubexpression(FxInfo *,const PixelChannel,const ssize_t,
cristye85007d2010-06-06 22:51:36 +00001304 const ssize_t,const char *,MagickRealType *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +00001305
cristyb0aad4c2011-11-02 19:30:35 +00001306static MagickOffsetType FxGCD(MagickOffsetType alpha,MagickOffsetType beta)
1307{
1308 if (beta != 0)
1309 return(FxGCD(beta,alpha % beta));
1310 return(alpha);
1311}
1312
cristy0568ffc2011-07-25 16:54:14 +00001313static inline MagickRealType FxMax(FxInfo *fx_info,const PixelChannel channel,
cristye85007d2010-06-06 22:51:36 +00001314 const ssize_t x,const ssize_t y,const char *expression,
1315 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001316{
1317 MagickRealType
1318 alpha,
1319 beta;
1320
1321 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression,&beta,exception);
1322 return((MagickRealType) MagickMax((double) alpha,(double) beta));
1323}
1324
cristy0568ffc2011-07-25 16:54:14 +00001325static inline MagickRealType FxMin(FxInfo *fx_info,PixelChannel channel,
cristye85007d2010-06-06 22:51:36 +00001326 const ssize_t x,const ssize_t y,const char *expression,
1327 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001328{
1329 MagickRealType
1330 alpha,
1331 beta;
1332
1333 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression,&beta,exception);
1334 return((MagickRealType) MagickMin((double) alpha,(double) beta));
1335}
1336
1337static inline const char *FxSubexpression(const char *expression,
1338 ExceptionInfo *exception)
1339{
1340 const char
1341 *subexpression;
1342
cristybb503372010-05-27 20:51:26 +00001343 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001344 level;
1345
1346 level=0;
1347 subexpression=expression;
1348 while ((*subexpression != '\0') &&
1349 ((level != 1) || (strchr(")",(int) *subexpression) == (char *) NULL)))
1350 {
1351 if (strchr("(",(int) *subexpression) != (char *) NULL)
1352 level++;
1353 else
1354 if (strchr(")",(int) *subexpression) != (char *) NULL)
1355 level--;
1356 subexpression++;
1357 }
1358 if (*subexpression == '\0')
1359 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1360 "UnbalancedParenthesis","`%s'",expression);
1361 return(subexpression);
1362}
1363
cristy0568ffc2011-07-25 16:54:14 +00001364static MagickRealType FxGetSymbol(FxInfo *fx_info,const PixelChannel channel,
cristye85007d2010-06-06 22:51:36 +00001365 const ssize_t x,const ssize_t y,const char *expression,
1366 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001367{
1368 char
1369 *q,
1370 subexpression[MaxTextExtent],
1371 symbol[MaxTextExtent];
1372
1373 const char
1374 *p,
1375 *value;
1376
1377 Image
1378 *image;
1379
cristy4c08aed2011-07-01 19:47:50 +00001380 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001381 pixel;
1382
1383 MagickRealType
1384 alpha,
1385 beta;
1386
1387 PointInfo
1388 point;
1389
cristybb503372010-05-27 20:51:26 +00001390 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001391 i;
1392
1393 size_t
1394 length;
1395
cristybb503372010-05-27 20:51:26 +00001396 size_t
cristy3ed852e2009-09-05 21:47:34 +00001397 level;
1398
1399 p=expression;
1400 i=GetImageIndexInList(fx_info->images);
1401 level=0;
1402 point.x=(double) x;
1403 point.y=(double) y;
1404 if (isalpha((int) *(p+1)) == 0)
1405 {
1406 if (strchr("suv",(int) *p) != (char *) NULL)
1407 {
1408 switch (*p)
1409 {
1410 case 's':
1411 default:
1412 {
1413 i=GetImageIndexInList(fx_info->images);
1414 break;
1415 }
1416 case 'u': i=0; break;
1417 case 'v': i=1; break;
1418 }
1419 p++;
1420 if (*p == '[')
1421 {
1422 level++;
1423 q=subexpression;
1424 for (p++; *p != '\0'; )
1425 {
1426 if (*p == '[')
1427 level++;
1428 else
1429 if (*p == ']')
1430 {
1431 level--;
1432 if (level == 0)
1433 break;
1434 }
1435 *q++=(*p++);
1436 }
1437 *q='\0';
1438 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1439 &beta,exception);
cristybb503372010-05-27 20:51:26 +00001440 i=(ssize_t) (alpha+0.5);
cristy3ed852e2009-09-05 21:47:34 +00001441 p++;
1442 }
1443 if (*p == '.')
1444 p++;
1445 }
1446 if ((isalpha((int) *(p+1)) == 0) && (*p == 'p'))
1447 {
1448 p++;
1449 if (*p == '{')
1450 {
1451 level++;
1452 q=subexpression;
1453 for (p++; *p != '\0'; )
1454 {
1455 if (*p == '{')
1456 level++;
1457 else
1458 if (*p == '}')
1459 {
1460 level--;
1461 if (level == 0)
1462 break;
1463 }
1464 *q++=(*p++);
1465 }
1466 *q='\0';
1467 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1468 &beta,exception);
1469 point.x=alpha;
1470 point.y=beta;
1471 p++;
1472 }
1473 else
1474 if (*p == '[')
1475 {
1476 level++;
1477 q=subexpression;
1478 for (p++; *p != '\0'; )
1479 {
1480 if (*p == '[')
1481 level++;
1482 else
1483 if (*p == ']')
1484 {
1485 level--;
1486 if (level == 0)
1487 break;
1488 }
1489 *q++=(*p++);
1490 }
1491 *q='\0';
1492 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1493 &beta,exception);
1494 point.x+=alpha;
1495 point.y+=beta;
1496 p++;
1497 }
1498 if (*p == '.')
1499 p++;
1500 }
1501 }
1502 length=GetImageListLength(fx_info->images);
1503 while (i < 0)
cristybb503372010-05-27 20:51:26 +00001504 i+=(ssize_t) length;
cristy3ed852e2009-09-05 21:47:34 +00001505 i%=length;
1506 image=GetImageFromList(fx_info->images,i);
1507 if (image == (Image *) NULL)
1508 {
1509 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1510 "NoSuchImage","`%s'",expression);
1511 return(0.0);
1512 }
cristy4c08aed2011-07-01 19:47:50 +00001513 GetPixelInfo(image,&pixel);
1514 (void) InterpolatePixelInfo(image,fx_info->view[i],image->interpolate,
cristy4f820712011-04-01 12:35:43 +00001515 point.x,point.y,&pixel,exception);
cristy3ed852e2009-09-05 21:47:34 +00001516 if ((strlen(p) > 2) &&
1517 (LocaleCompare(p,"intensity") != 0) &&
1518 (LocaleCompare(p,"luminance") != 0) &&
1519 (LocaleCompare(p,"hue") != 0) &&
1520 (LocaleCompare(p,"saturation") != 0) &&
1521 (LocaleCompare(p,"lightness") != 0))
1522 {
1523 char
1524 name[MaxTextExtent];
1525
1526 (void) CopyMagickString(name,p,MaxTextExtent);
1527 for (q=name+(strlen(name)-1); q > name; q--)
1528 {
1529 if (*q == ')')
1530 break;
1531 if (*q == '.')
1532 {
1533 *q='\0';
1534 break;
1535 }
1536 }
1537 if ((strlen(name) > 2) &&
1538 (GetValueFromSplayTree(fx_info->symbols,name) == (const char *) NULL))
1539 {
cristy4c08aed2011-07-01 19:47:50 +00001540 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001541 *color;
1542
cristy4c08aed2011-07-01 19:47:50 +00001543 color=(PixelInfo *) GetValueFromSplayTree(fx_info->colors,name);
1544 if (color != (PixelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001545 {
1546 pixel=(*color);
1547 p+=strlen(name);
1548 }
1549 else
cristy269c9412011-10-13 23:41:15 +00001550 if (QueryColorCompliance(name,AllCompliance,&pixel,fx_info->exception) != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001551 {
1552 (void) AddValueToSplayTree(fx_info->colors,ConstantString(name),
cristy4c08aed2011-07-01 19:47:50 +00001553 ClonePixelInfo(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001554 p+=strlen(name);
1555 }
1556 }
1557 }
1558 (void) CopyMagickString(symbol,p,MaxTextExtent);
1559 StripString(symbol);
1560 if (*symbol == '\0')
1561 {
1562 switch (channel)
1563 {
cristy0568ffc2011-07-25 16:54:14 +00001564 case RedPixelChannel: return(QuantumScale*pixel.red);
1565 case GreenPixelChannel: return(QuantumScale*pixel.green);
1566 case BluePixelChannel: return(QuantumScale*pixel.blue);
1567 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001568 {
1569 if (image->colorspace != CMYKColorspace)
1570 {
1571 (void) ThrowMagickException(exception,GetMagickModule(),
1572 OptionError,"ColorSeparatedImageRequired","`%s'",
1573 image->filename);
1574 return(0.0);
1575 }
cristy4c08aed2011-07-01 19:47:50 +00001576 return(QuantumScale*pixel.black);
1577 }
cristy0568ffc2011-07-25 16:54:14 +00001578 case AlphaPixelChannel:
cristy4c08aed2011-07-01 19:47:50 +00001579 {
1580 MagickRealType
1581 alpha;
1582
1583 if (pixel.matte == MagickFalse)
1584 return(1.0);
1585 alpha=(MagickRealType) (QuantumScale*pixel.alpha);
1586 return(alpha);
cristy3ed852e2009-09-05 21:47:34 +00001587 }
cristyb3a73b52011-07-26 01:34:43 +00001588 case IntensityPixelChannel:
cristyf364ed42010-12-15 01:54:43 +00001589 {
cristy4c08aed2011-07-01 19:47:50 +00001590 return(QuantumScale*GetPixelInfoIntensity(&pixel));
cristyf364ed42010-12-15 01:54:43 +00001591 }
cristy3ed852e2009-09-05 21:47:34 +00001592 default:
1593 break;
1594 }
1595 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1596 "UnableToParseExpression","`%s'",p);
1597 return(0.0);
1598 }
1599 switch (*symbol)
1600 {
1601 case 'A':
1602 case 'a':
1603 {
1604 if (LocaleCompare(symbol,"a") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001605 return((MagickRealType) (QuantumScale*pixel.alpha));
cristy3ed852e2009-09-05 21:47:34 +00001606 break;
1607 }
1608 case 'B':
1609 case 'b':
1610 {
1611 if (LocaleCompare(symbol,"b") == 0)
1612 return(QuantumScale*pixel.blue);
1613 break;
1614 }
1615 case 'C':
1616 case 'c':
1617 {
1618 if (LocaleNCompare(symbol,"channel",7) == 0)
1619 {
1620 GeometryInfo
1621 channel_info;
1622
1623 MagickStatusType
1624 flags;
1625
1626 flags=ParseGeometry(symbol+7,&channel_info);
1627 if (image->colorspace == CMYKColorspace)
1628 switch (channel)
1629 {
cristy0568ffc2011-07-25 16:54:14 +00001630 case CyanPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001631 {
1632 if ((flags & RhoValue) == 0)
1633 return(0.0);
1634 return(channel_info.rho);
1635 }
cristy0568ffc2011-07-25 16:54:14 +00001636 case MagentaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001637 {
1638 if ((flags & SigmaValue) == 0)
1639 return(0.0);
1640 return(channel_info.sigma);
1641 }
cristy0568ffc2011-07-25 16:54:14 +00001642 case YellowPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001643 {
1644 if ((flags & XiValue) == 0)
1645 return(0.0);
1646 return(channel_info.xi);
1647 }
cristy0568ffc2011-07-25 16:54:14 +00001648 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001649 {
1650 if ((flags & PsiValue) == 0)
1651 return(0.0);
1652 return(channel_info.psi);
1653 }
cristy0568ffc2011-07-25 16:54:14 +00001654 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001655 {
1656 if ((flags & ChiValue) == 0)
1657 return(0.0);
1658 return(channel_info.chi);
1659 }
1660 default:
1661 return(0.0);
1662 }
1663 switch (channel)
1664 {
cristy0568ffc2011-07-25 16:54:14 +00001665 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001666 {
1667 if ((flags & RhoValue) == 0)
1668 return(0.0);
1669 return(channel_info.rho);
1670 }
cristy0568ffc2011-07-25 16:54:14 +00001671 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001672 {
1673 if ((flags & SigmaValue) == 0)
1674 return(0.0);
1675 return(channel_info.sigma);
1676 }
cristy0568ffc2011-07-25 16:54:14 +00001677 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001678 {
1679 if ((flags & XiValue) == 0)
1680 return(0.0);
1681 return(channel_info.xi);
1682 }
cristy0568ffc2011-07-25 16:54:14 +00001683 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001684 {
1685 if ((flags & ChiValue) == 0)
1686 return(0.0);
1687 return(channel_info.chi);
1688 }
cristy0568ffc2011-07-25 16:54:14 +00001689 case AlphaPixelChannel:
cristy4c08aed2011-07-01 19:47:50 +00001690 {
1691 if ((flags & PsiValue) == 0)
1692 return(0.0);
1693 return(channel_info.psi);
1694 }
cristy3ed852e2009-09-05 21:47:34 +00001695 default:
1696 return(0.0);
1697 }
1698 return(0.0);
1699 }
1700 if (LocaleCompare(symbol,"c") == 0)
1701 return(QuantumScale*pixel.red);
1702 break;
1703 }
1704 case 'D':
1705 case 'd':
1706 {
1707 if (LocaleNCompare(symbol,"depth",5) == 0)
1708 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1709 break;
1710 }
1711 case 'G':
1712 case 'g':
1713 {
1714 if (LocaleCompare(symbol,"g") == 0)
1715 return(QuantumScale*pixel.green);
1716 break;
1717 }
1718 case 'K':
1719 case 'k':
1720 {
1721 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1722 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1723 if (LocaleCompare(symbol,"k") == 0)
1724 {
1725 if (image->colorspace != CMYKColorspace)
1726 {
1727 (void) ThrowMagickException(exception,GetMagickModule(),
1728 OptionError,"ColorSeparatedImageRequired","`%s'",
1729 image->filename);
1730 return(0.0);
1731 }
cristy4c08aed2011-07-01 19:47:50 +00001732 return(QuantumScale*pixel.black);
cristy3ed852e2009-09-05 21:47:34 +00001733 }
1734 break;
1735 }
1736 case 'H':
1737 case 'h':
1738 {
1739 if (LocaleCompare(symbol,"h") == 0)
1740 return((MagickRealType) image->rows);
1741 if (LocaleCompare(symbol,"hue") == 0)
1742 {
1743 double
1744 hue,
1745 lightness,
1746 saturation;
1747
cristyda1f9c12011-10-02 21:39:49 +00001748 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1749 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001750 return(hue);
1751 }
1752 break;
1753 }
1754 case 'I':
1755 case 'i':
1756 {
1757 if ((LocaleCompare(symbol,"image.depth") == 0) ||
1758 (LocaleCompare(symbol,"image.minima") == 0) ||
1759 (LocaleCompare(symbol,"image.maxima") == 0) ||
1760 (LocaleCompare(symbol,"image.mean") == 0) ||
1761 (LocaleCompare(symbol,"image.kurtosis") == 0) ||
1762 (LocaleCompare(symbol,"image.skewness") == 0) ||
1763 (LocaleCompare(symbol,"image.standard_deviation") == 0))
1764 return(FxChannelStatistics(fx_info,image,channel,symbol+6,exception));
1765 if (LocaleCompare(symbol,"image.resolution.x") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001766 return(image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00001767 if (LocaleCompare(symbol,"image.resolution.y") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001768 return(image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001769 if (LocaleCompare(symbol,"intensity") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001770 return(QuantumScale*GetPixelInfoIntensity(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001771 if (LocaleCompare(symbol,"i") == 0)
1772 return((MagickRealType) x);
1773 break;
1774 }
1775 case 'J':
1776 case 'j':
1777 {
1778 if (LocaleCompare(symbol,"j") == 0)
1779 return((MagickRealType) y);
1780 break;
1781 }
1782 case 'L':
1783 case 'l':
1784 {
1785 if (LocaleCompare(symbol,"lightness") == 0)
1786 {
1787 double
1788 hue,
1789 lightness,
1790 saturation;
1791
cristyda1f9c12011-10-02 21:39:49 +00001792 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1793 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001794 return(lightness);
1795 }
1796 if (LocaleCompare(symbol,"luminance") == 0)
1797 {
1798 double
1799 luminence;
1800
1801 luminence=0.2126*pixel.red+0.7152*pixel.green+0.0722*pixel.blue;
1802 return(QuantumScale*luminence);
1803 }
1804 break;
1805 }
1806 case 'M':
1807 case 'm':
1808 {
1809 if (LocaleNCompare(symbol,"maxima",6) == 0)
1810 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1811 if (LocaleNCompare(symbol,"mean",4) == 0)
1812 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1813 if (LocaleNCompare(symbol,"minima",6) == 0)
1814 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1815 if (LocaleCompare(symbol,"m") == 0)
1816 return(QuantumScale*pixel.blue);
1817 break;
1818 }
1819 case 'N':
1820 case 'n':
1821 {
1822 if (LocaleCompare(symbol,"n") == 0)
anthony374f5dd2011-03-25 10:08:53 +00001823 return((MagickRealType) GetImageListLength(fx_info->images));
cristy3ed852e2009-09-05 21:47:34 +00001824 break;
1825 }
1826 case 'O':
1827 case 'o':
1828 {
1829 if (LocaleCompare(symbol,"o") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001830 return(QuantumScale*pixel.alpha);
cristy3ed852e2009-09-05 21:47:34 +00001831 break;
1832 }
1833 case 'P':
1834 case 'p':
1835 {
1836 if (LocaleCompare(symbol,"page.height") == 0)
1837 return((MagickRealType) image->page.height);
1838 if (LocaleCompare(symbol,"page.width") == 0)
1839 return((MagickRealType) image->page.width);
1840 if (LocaleCompare(symbol,"page.x") == 0)
1841 return((MagickRealType) image->page.x);
1842 if (LocaleCompare(symbol,"page.y") == 0)
1843 return((MagickRealType) image->page.y);
1844 break;
1845 }
1846 case 'R':
1847 case 'r':
1848 {
1849 if (LocaleCompare(symbol,"resolution.x") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001850 return(image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00001851 if (LocaleCompare(symbol,"resolution.y") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001852 return(image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001853 if (LocaleCompare(symbol,"r") == 0)
1854 return(QuantumScale*pixel.red);
1855 break;
1856 }
1857 case 'S':
1858 case 's':
1859 {
1860 if (LocaleCompare(symbol,"saturation") == 0)
1861 {
1862 double
1863 hue,
1864 lightness,
1865 saturation;
1866
cristyda1f9c12011-10-02 21:39:49 +00001867 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1868 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001869 return(saturation);
1870 }
1871 if (LocaleNCompare(symbol,"skewness",8) == 0)
1872 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1873 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1874 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1875 break;
1876 }
1877 case 'T':
1878 case 't':
1879 {
1880 if (LocaleCompare(symbol,"t") == 0)
cristy5a15b932011-03-26 12:50:33 +00001881 return((MagickRealType) GetImageIndexInList(fx_info->images));
cristy3ed852e2009-09-05 21:47:34 +00001882 break;
1883 }
1884 case 'W':
1885 case 'w':
1886 {
1887 if (LocaleCompare(symbol,"w") == 0)
1888 return((MagickRealType) image->columns);
1889 break;
1890 }
1891 case 'Y':
1892 case 'y':
1893 {
1894 if (LocaleCompare(symbol,"y") == 0)
1895 return(QuantumScale*pixel.green);
1896 break;
1897 }
1898 case 'Z':
1899 case 'z':
1900 {
1901 if (LocaleCompare(symbol,"z") == 0)
1902 {
1903 MagickRealType
1904 depth;
1905
cristyfefab1b2011-07-05 00:33:22 +00001906 depth=(MagickRealType) GetImageDepth(image,fx_info->exception);
cristy3ed852e2009-09-05 21:47:34 +00001907 return(depth);
1908 }
1909 break;
1910 }
1911 default:
1912 break;
1913 }
1914 value=(const char *) GetValueFromSplayTree(fx_info->symbols,symbol);
1915 if (value != (const char *) NULL)
cristyc1acd842011-05-19 23:05:47 +00001916 return((MagickRealType) InterpretLocaleValue(value,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001917 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1918 "UnableToParseExpression","`%s'",symbol);
1919 return(0.0);
1920}
1921
1922static const char *FxOperatorPrecedence(const char *expression,
1923 ExceptionInfo *exception)
1924{
1925 typedef enum
1926 {
1927 UndefinedPrecedence,
1928 NullPrecedence,
1929 BitwiseComplementPrecedence,
1930 ExponentPrecedence,
cristy116af162010-08-13 01:25:47 +00001931 ExponentialNotationPrecedence,
cristy3ed852e2009-09-05 21:47:34 +00001932 MultiplyPrecedence,
1933 AdditionPrecedence,
1934 ShiftPrecedence,
1935 RelationalPrecedence,
1936 EquivalencyPrecedence,
1937 BitwiseAndPrecedence,
1938 BitwiseOrPrecedence,
1939 LogicalAndPrecedence,
1940 LogicalOrPrecedence,
1941 TernaryPrecedence,
1942 AssignmentPrecedence,
1943 CommaPrecedence,
1944 SeparatorPrecedence
1945 } FxPrecedence;
1946
1947 FxPrecedence
1948 precedence,
1949 target;
1950
1951 register const char
1952 *subexpression;
1953
1954 register int
1955 c;
1956
cristybb503372010-05-27 20:51:26 +00001957 size_t
cristy3ed852e2009-09-05 21:47:34 +00001958 level;
1959
1960 c=0;
1961 level=0;
1962 subexpression=(const char *) NULL;
1963 target=NullPrecedence;
1964 while (*expression != '\0')
1965 {
1966 precedence=UndefinedPrecedence;
1967 if ((isspace((int) ((char) *expression)) != 0) || (c == (int) '@'))
1968 {
1969 expression++;
1970 continue;
1971 }
cristy488fa882010-03-01 22:34:24 +00001972 switch (*expression)
1973 {
1974 case 'A':
1975 case 'a':
cristy3ed852e2009-09-05 21:47:34 +00001976 {
cristyb33454f2011-08-03 02:10:45 +00001977#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00001978 if (LocaleNCompare(expression,"acosh",5) == 0)
1979 {
1980 expression+=5;
1981 break;
1982 }
cristyb33454f2011-08-03 02:10:45 +00001983#endif
1984#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00001985 if (LocaleNCompare(expression,"asinh",5) == 0)
1986 {
1987 expression+=5;
1988 break;
1989 }
cristyb33454f2011-08-03 02:10:45 +00001990#endif
1991#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00001992 if (LocaleNCompare(expression,"atanh",5) == 0)
cristy488fa882010-03-01 22:34:24 +00001993 {
1994 expression+=5;
1995 break;
1996 }
cristyb33454f2011-08-03 02:10:45 +00001997#endif
cristy488fa882010-03-01 22:34:24 +00001998 break;
cristy3ed852e2009-09-05 21:47:34 +00001999 }
cristy488fa882010-03-01 22:34:24 +00002000 case 'J':
2001 case 'j':
2002 {
2003 if ((LocaleNCompare(expression,"j0",2) == 0) ||
2004 (LocaleNCompare(expression,"j1",2) == 0))
2005 {
2006 expression+=2;
2007 break;
2008 }
2009 break;
2010 }
cristy2def9322010-06-18 23:59:37 +00002011 case '#':
2012 {
2013 while (isxdigit((int) ((unsigned char) *(expression+1))) != 0)
2014 expression++;
2015 break;
2016 }
cristy488fa882010-03-01 22:34:24 +00002017 default:
2018 break;
2019 }
cristy3ed852e2009-09-05 21:47:34 +00002020 if ((c == (int) '{') || (c == (int) '['))
2021 level++;
2022 else
2023 if ((c == (int) '}') || (c == (int) ']'))
2024 level--;
2025 if (level == 0)
2026 switch ((unsigned char) *expression)
2027 {
2028 case '~':
2029 case '!':
2030 {
2031 precedence=BitwiseComplementPrecedence;
2032 break;
2033 }
2034 case '^':
cristy6621e252010-08-13 00:42:57 +00002035 case '@':
cristy3ed852e2009-09-05 21:47:34 +00002036 {
2037 precedence=ExponentPrecedence;
2038 break;
2039 }
2040 default:
2041 {
2042 if (((c != 0) && ((isdigit((int) ((char) c)) != 0) ||
2043 (strchr(")",c) != (char *) NULL))) &&
2044 (((islower((int) ((char) *expression)) != 0) ||
2045 (strchr("(",(int) *expression) != (char *) NULL)) ||
2046 ((isdigit((int) ((char) c)) == 0) &&
2047 (isdigit((int) ((char) *expression)) != 0))) &&
2048 (strchr("xy",(int) *expression) == (char *) NULL))
2049 precedence=MultiplyPrecedence;
2050 break;
2051 }
2052 case '*':
2053 case '/':
2054 case '%':
2055 {
2056 precedence=MultiplyPrecedence;
2057 break;
2058 }
2059 case '+':
2060 case '-':
2061 {
2062 if ((strchr("(+-/*%:&^|<>~,",c) == (char *) NULL) ||
2063 (isalpha(c) != 0))
2064 precedence=AdditionPrecedence;
2065 break;
2066 }
2067 case LeftShiftOperator:
2068 case RightShiftOperator:
2069 {
2070 precedence=ShiftPrecedence;
2071 break;
2072 }
2073 case '<':
2074 case LessThanEqualOperator:
2075 case GreaterThanEqualOperator:
2076 case '>':
2077 {
2078 precedence=RelationalPrecedence;
2079 break;
2080 }
2081 case EqualOperator:
2082 case NotEqualOperator:
2083 {
2084 precedence=EquivalencyPrecedence;
2085 break;
2086 }
2087 case '&':
2088 {
2089 precedence=BitwiseAndPrecedence;
2090 break;
2091 }
2092 case '|':
2093 {
2094 precedence=BitwiseOrPrecedence;
2095 break;
2096 }
2097 case LogicalAndOperator:
2098 {
2099 precedence=LogicalAndPrecedence;
2100 break;
2101 }
2102 case LogicalOrOperator:
2103 {
2104 precedence=LogicalOrPrecedence;
2105 break;
2106 }
cristy116af162010-08-13 01:25:47 +00002107 case ExponentialNotation:
2108 {
2109 precedence=ExponentialNotationPrecedence;
2110 break;
2111 }
cristy3ed852e2009-09-05 21:47:34 +00002112 case ':':
2113 case '?':
2114 {
2115 precedence=TernaryPrecedence;
2116 break;
2117 }
2118 case '=':
2119 {
2120 precedence=AssignmentPrecedence;
2121 break;
2122 }
2123 case ',':
2124 {
2125 precedence=CommaPrecedence;
2126 break;
2127 }
2128 case ';':
2129 {
2130 precedence=SeparatorPrecedence;
2131 break;
2132 }
2133 }
2134 if ((precedence == BitwiseComplementPrecedence) ||
2135 (precedence == TernaryPrecedence) ||
2136 (precedence == AssignmentPrecedence))
2137 {
2138 if (precedence > target)
2139 {
2140 /*
2141 Right-to-left associativity.
2142 */
2143 target=precedence;
2144 subexpression=expression;
2145 }
2146 }
2147 else
2148 if (precedence >= target)
2149 {
2150 /*
2151 Left-to-right associativity.
2152 */
2153 target=precedence;
2154 subexpression=expression;
2155 }
2156 if (strchr("(",(int) *expression) != (char *) NULL)
2157 expression=FxSubexpression(expression,exception);
2158 c=(int) (*expression++);
2159 }
2160 return(subexpression);
2161}
2162
2163static MagickRealType FxEvaluateSubexpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002164 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristye85007d2010-06-06 22:51:36 +00002165 const char *expression,MagickRealType *beta,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002166{
2167 char
2168 *q,
2169 subexpression[MaxTextExtent];
2170
2171 MagickRealType
2172 alpha,
2173 gamma;
2174
2175 register const char
2176 *p;
2177
2178 *beta=0.0;
2179 if (exception->severity != UndefinedException)
2180 return(0.0);
2181 while (isspace((int) *expression) != 0)
2182 expression++;
2183 if (*expression == '\0')
2184 {
2185 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
2186 "MissingExpression","`%s'",expression);
2187 return(0.0);
2188 }
cristy66322f02010-05-17 11:40:48 +00002189 *subexpression='\0';
cristy3ed852e2009-09-05 21:47:34 +00002190 p=FxOperatorPrecedence(expression,exception);
2191 if (p != (const char *) NULL)
2192 {
2193 (void) CopyMagickString(subexpression,expression,(size_t)
2194 (p-expression+1));
2195 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2196 exception);
2197 switch ((unsigned char) *p)
2198 {
2199 case '~':
2200 {
2201 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristybb503372010-05-27 20:51:26 +00002202 *beta=(MagickRealType) (~(size_t) *beta);
cristy3ed852e2009-09-05 21:47:34 +00002203 return(*beta);
2204 }
2205 case '!':
2206 {
2207 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2208 return(*beta == 0.0 ? 1.0 : 0.0);
2209 }
2210 case '^':
2211 {
2212 *beta=pow((double) alpha,(double) FxEvaluateSubexpression(fx_info,
2213 channel,x,y,++p,beta,exception));
2214 return(*beta);
2215 }
2216 case '*':
cristy116af162010-08-13 01:25:47 +00002217 case ExponentialNotation:
cristy3ed852e2009-09-05 21:47:34 +00002218 {
2219 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2220 return(alpha*(*beta));
2221 }
2222 case '/':
2223 {
2224 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2225 if (*beta == 0.0)
2226 {
2227 if (exception->severity == UndefinedException)
2228 (void) ThrowMagickException(exception,GetMagickModule(),
2229 OptionError,"DivideByZero","`%s'",expression);
2230 return(0.0);
2231 }
2232 return(alpha/(*beta));
2233 }
2234 case '%':
2235 {
2236 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2237 *beta=fabs(floor(((double) *beta)+0.5));
2238 if (*beta == 0.0)
2239 {
2240 (void) ThrowMagickException(exception,GetMagickModule(),
2241 OptionError,"DivideByZero","`%s'",expression);
2242 return(0.0);
2243 }
2244 return(fmod((double) alpha,(double) *beta));
2245 }
2246 case '+':
2247 {
2248 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2249 return(alpha+(*beta));
2250 }
2251 case '-':
2252 {
2253 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2254 return(alpha-(*beta));
2255 }
2256 case LeftShiftOperator:
2257 {
2258 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristybb503372010-05-27 20:51:26 +00002259 *beta=(MagickRealType) ((size_t) (alpha+0.5) << (size_t)
cristy3ed852e2009-09-05 21:47:34 +00002260 (gamma+0.5));
2261 return(*beta);
2262 }
2263 case RightShiftOperator:
2264 {
2265 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristybb503372010-05-27 20:51:26 +00002266 *beta=(MagickRealType) ((size_t) (alpha+0.5) >> (size_t)
cristy3ed852e2009-09-05 21:47:34 +00002267 (gamma+0.5));
2268 return(*beta);
2269 }
2270 case '<':
2271 {
2272 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2273 return(alpha < *beta ? 1.0 : 0.0);
2274 }
2275 case LessThanEqualOperator:
2276 {
2277 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2278 return(alpha <= *beta ? 1.0 : 0.0);
2279 }
2280 case '>':
2281 {
2282 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2283 return(alpha > *beta ? 1.0 : 0.0);
2284 }
2285 case GreaterThanEqualOperator:
2286 {
2287 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2288 return(alpha >= *beta ? 1.0 : 0.0);
2289 }
2290 case EqualOperator:
2291 {
2292 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2293 return(fabs(alpha-(*beta)) <= MagickEpsilon ? 1.0 : 0.0);
2294 }
2295 case NotEqualOperator:
2296 {
2297 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2298 return(fabs(alpha-(*beta)) > MagickEpsilon ? 1.0 : 0.0);
2299 }
2300 case '&':
2301 {
2302 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristybb503372010-05-27 20:51:26 +00002303 *beta=(MagickRealType) ((size_t) (alpha+0.5) & (size_t)
cristy3ed852e2009-09-05 21:47:34 +00002304 (gamma+0.5));
2305 return(*beta);
2306 }
2307 case '|':
2308 {
2309 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristybb503372010-05-27 20:51:26 +00002310 *beta=(MagickRealType) ((size_t) (alpha+0.5) | (size_t)
cristy3ed852e2009-09-05 21:47:34 +00002311 (gamma+0.5));
2312 return(*beta);
2313 }
2314 case LogicalAndOperator:
2315 {
2316 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2317 *beta=(alpha > 0.0) && (gamma > 0.0) ? 1.0 : 0.0;
2318 return(*beta);
2319 }
2320 case LogicalOrOperator:
2321 {
2322 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2323 *beta=(alpha > 0.0) || (gamma > 0.0) ? 1.0 : 0.0;
2324 return(*beta);
2325 }
2326 case '?':
2327 {
2328 MagickRealType
2329 gamma;
2330
2331 (void) CopyMagickString(subexpression,++p,MaxTextExtent);
2332 q=subexpression;
2333 p=StringToken(":",&q);
2334 if (q == (char *) NULL)
2335 {
2336 (void) ThrowMagickException(exception,GetMagickModule(),
2337 OptionError,"UnableToParseExpression","`%s'",subexpression);
2338 return(0.0);
2339 }
2340 if (fabs((double) alpha) > MagickEpsilon)
2341 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,exception);
2342 else
2343 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,q,beta,exception);
2344 return(gamma);
2345 }
2346 case '=':
2347 {
2348 char
2349 numeric[MaxTextExtent];
2350
2351 q=subexpression;
2352 while (isalpha((int) ((unsigned char) *q)) != 0)
2353 q++;
2354 if (*q != '\0')
2355 {
2356 (void) ThrowMagickException(exception,GetMagickModule(),
2357 OptionError,"UnableToParseExpression","`%s'",subexpression);
2358 return(0.0);
2359 }
2360 ClearMagickException(exception);
2361 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristyb51dff52011-05-19 16:55:47 +00002362 (void) FormatLocaleString(numeric,MaxTextExtent,"%g",(double)
cristy8cd5b312010-01-07 01:10:24 +00002363 *beta);
cristy3ed852e2009-09-05 21:47:34 +00002364 (void) DeleteNodeFromSplayTree(fx_info->symbols,subexpression);
2365 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(
2366 subexpression),ConstantString(numeric));
2367 return(*beta);
2368 }
2369 case ',':
2370 {
2371 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2372 return(alpha);
2373 }
2374 case ';':
2375 {
2376 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2377 return(*beta);
2378 }
2379 default:
2380 {
2381 gamma=alpha*FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,
2382 exception);
2383 return(gamma);
2384 }
2385 }
2386 }
2387 if (strchr("(",(int) *expression) != (char *) NULL)
2388 {
2389 (void) CopyMagickString(subexpression,expression+1,MaxTextExtent);
2390 subexpression[strlen(subexpression)-1]='\0';
2391 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2392 exception);
2393 return(gamma);
2394 }
cristy8b8a3ae2010-10-23 18:49:46 +00002395 switch (*expression)
cristy3ed852e2009-09-05 21:47:34 +00002396 {
2397 case '+':
2398 {
2399 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2400 exception);
2401 return(1.0*gamma);
2402 }
2403 case '-':
2404 {
2405 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2406 exception);
2407 return(-1.0*gamma);
2408 }
2409 case '~':
2410 {
2411 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2412 exception);
cristybb503372010-05-27 20:51:26 +00002413 return((MagickRealType) (~(size_t) (gamma+0.5)));
cristy3ed852e2009-09-05 21:47:34 +00002414 }
2415 case 'A':
2416 case 'a':
2417 {
2418 if (LocaleNCompare(expression,"abs",3) == 0)
2419 {
2420 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2421 exception);
2422 return((MagickRealType) fabs((double) alpha));
2423 }
cristyb33454f2011-08-03 02:10:45 +00002424#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00002425 if (LocaleNCompare(expression,"acosh",5) == 0)
2426 {
2427 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2428 exception);
2429 return((MagickRealType) acosh((double) alpha));
2430 }
cristyb33454f2011-08-03 02:10:45 +00002431#endif
cristy3ed852e2009-09-05 21:47:34 +00002432 if (LocaleNCompare(expression,"acos",4) == 0)
2433 {
2434 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2435 exception);
2436 return((MagickRealType) acos((double) alpha));
2437 }
cristy43c22f42010-03-30 12:34:07 +00002438#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002439 if (LocaleNCompare(expression,"airy",4) == 0)
2440 {
2441 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2442 exception);
2443 if (alpha == 0.0)
cristy2dd03222010-03-30 22:12:11 +00002444 return(1.0);
2445 gamma=2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha);
cristy43c22f42010-03-30 12:34:07 +00002446 return(gamma*gamma);
cristyee56cf12010-03-01 22:17:06 +00002447 }
cristy43c22f42010-03-30 12:34:07 +00002448#endif
cristyb33454f2011-08-03 02:10:45 +00002449#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00002450 if (LocaleNCompare(expression,"asinh",5) == 0)
2451 {
2452 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2453 exception);
2454 return((MagickRealType) asinh((double) alpha));
2455 }
cristyb33454f2011-08-03 02:10:45 +00002456#endif
cristy3ed852e2009-09-05 21:47:34 +00002457 if (LocaleNCompare(expression,"asin",4) == 0)
2458 {
2459 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2460 exception);
2461 return((MagickRealType) asin((double) alpha));
2462 }
2463 if (LocaleNCompare(expression,"alt",3) == 0)
2464 {
2465 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2466 exception);
cristybb503372010-05-27 20:51:26 +00002467 return(((ssize_t) alpha) & 0x01 ? -1.0 : 1.0);
cristy3ed852e2009-09-05 21:47:34 +00002468 }
2469 if (LocaleNCompare(expression,"atan2",5) == 0)
2470 {
2471 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2472 exception);
2473 return((MagickRealType) atan2((double) alpha,(double) *beta));
2474 }
cristyb33454f2011-08-03 02:10:45 +00002475#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00002476 if (LocaleNCompare(expression,"atanh",5) == 0)
2477 {
2478 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2479 exception);
2480 return((MagickRealType) atanh((double) alpha));
2481 }
cristyb33454f2011-08-03 02:10:45 +00002482#endif
cristy3ed852e2009-09-05 21:47:34 +00002483 if (LocaleNCompare(expression,"atan",4) == 0)
2484 {
2485 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2486 exception);
2487 return((MagickRealType) atan((double) alpha));
2488 }
2489 if (LocaleCompare(expression,"a") == 0)
2490 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2491 break;
2492 }
2493 case 'B':
2494 case 'b':
2495 {
2496 if (LocaleCompare(expression,"b") == 0)
2497 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2498 break;
2499 }
2500 case 'C':
2501 case 'c':
2502 {
2503 if (LocaleNCompare(expression,"ceil",4) == 0)
2504 {
2505 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2506 exception);
2507 return((MagickRealType) ceil((double) alpha));
2508 }
2509 if (LocaleNCompare(expression,"cosh",4) == 0)
2510 {
2511 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2512 exception);
2513 return((MagickRealType) cosh((double) alpha));
2514 }
2515 if (LocaleNCompare(expression,"cos",3) == 0)
2516 {
2517 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2518 exception);
2519 return((MagickRealType) cos((double) alpha));
2520 }
2521 if (LocaleCompare(expression,"c") == 0)
2522 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2523 break;
2524 }
2525 case 'D':
2526 case 'd':
2527 {
2528 if (LocaleNCompare(expression,"debug",5) == 0)
2529 {
2530 const char
2531 *type;
2532
2533 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2534 exception);
2535 if (fx_info->images->colorspace == CMYKColorspace)
2536 switch (channel)
2537 {
cristy0568ffc2011-07-25 16:54:14 +00002538 case CyanPixelChannel: type="cyan"; break;
2539 case MagentaPixelChannel: type="magenta"; break;
2540 case YellowPixelChannel: type="yellow"; break;
2541 case AlphaPixelChannel: type="opacity"; break;
2542 case BlackPixelChannel: type="black"; break;
cristy3ed852e2009-09-05 21:47:34 +00002543 default: type="unknown"; break;
2544 }
2545 else
2546 switch (channel)
2547 {
cristy0568ffc2011-07-25 16:54:14 +00002548 case RedPixelChannel: type="red"; break;
2549 case GreenPixelChannel: type="green"; break;
2550 case BluePixelChannel: type="blue"; break;
2551 case AlphaPixelChannel: type="opacity"; break;
cristy3ed852e2009-09-05 21:47:34 +00002552 default: type="unknown"; break;
2553 }
2554 (void) CopyMagickString(subexpression,expression+6,MaxTextExtent);
2555 if (strlen(subexpression) > 1)
2556 subexpression[strlen(subexpression)-1]='\0';
2557 if (fx_info->file != (FILE *) NULL)
cristy1e604812011-05-19 18:07:50 +00002558 (void) FormatLocaleFile(fx_info->file,
2559 "%s[%.20g,%.20g].%s: %s=%.*g\n",fx_info->images->filename,
2560 (double) x,(double) y,type,subexpression,GetMagickPrecision(),
2561 (double) alpha);
cristy3ed852e2009-09-05 21:47:34 +00002562 return(0.0);
2563 }
2564 break;
2565 }
2566 case 'E':
2567 case 'e':
2568 {
2569 if (LocaleCompare(expression,"epsilon") == 0)
2570 return((MagickRealType) MagickEpsilon);
2571 if (LocaleNCompare(expression,"exp",3) == 0)
2572 {
2573 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2574 exception);
2575 return((MagickRealType) exp((double) alpha));
2576 }
2577 if (LocaleCompare(expression,"e") == 0)
2578 return((MagickRealType) 2.7182818284590452354);
2579 break;
2580 }
2581 case 'F':
2582 case 'f':
2583 {
2584 if (LocaleNCompare(expression,"floor",5) == 0)
2585 {
2586 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2587 exception);
2588 return((MagickRealType) floor((double) alpha));
2589 }
2590 break;
2591 }
2592 case 'G':
2593 case 'g':
2594 {
cristy9eeedea2011-11-02 19:04:05 +00002595 if (LocaleNCompare(expression,"gauss",5) == 0)
2596 {
2597 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2598 exception);
2599 gamma=exp((double) (-alpha*alpha/2.0))/sqrt(2.0*MagickPI);
2600 return((MagickRealType) gamma);
2601 }
cristyb0aad4c2011-11-02 19:30:35 +00002602 if (LocaleNCompare(expression,"gcd",3) == 0)
2603 {
2604 MagickOffsetType
2605 gcd;
2606
2607 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2608 exception);
cristy76461162011-11-02 19:32:19 +00002609 gcd=FxGCD((MagickOffsetType) (alpha+0.5),(MagickOffsetType)
2610 (*beta+0.5));
cristyb0aad4c2011-11-02 19:30:35 +00002611 return((MagickRealType) gcd);
2612 }
cristy3ed852e2009-09-05 21:47:34 +00002613 if (LocaleCompare(expression,"g") == 0)
2614 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2615 break;
2616 }
2617 case 'H':
2618 case 'h':
2619 {
2620 if (LocaleCompare(expression,"h") == 0)
2621 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2622 if (LocaleCompare(expression,"hue") == 0)
2623 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2624 if (LocaleNCompare(expression,"hypot",5) == 0)
2625 {
2626 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2627 exception);
2628 return((MagickRealType) hypot((double) alpha,(double) *beta));
2629 }
2630 break;
2631 }
2632 case 'K':
2633 case 'k':
2634 {
2635 if (LocaleCompare(expression,"k") == 0)
2636 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2637 break;
2638 }
2639 case 'I':
2640 case 'i':
2641 {
2642 if (LocaleCompare(expression,"intensity") == 0)
2643 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2644 if (LocaleNCompare(expression,"int",3) == 0)
2645 {
2646 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2647 exception);
cristy16788e42010-08-13 13:44:26 +00002648 return((MagickRealType) floor(alpha));
cristy3ed852e2009-09-05 21:47:34 +00002649 }
cristy639399c2011-11-02 19:16:15 +00002650 if (LocaleNCompare(expression,"isnan",5) == 0)
2651 {
2652 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2653 exception);
cristy17a10202011-11-02 19:17:04 +00002654 return((MagickRealType) !!isnan((double) alpha));
cristy639399c2011-11-02 19:16:15 +00002655 }
cristy3ed852e2009-09-05 21:47:34 +00002656 if (LocaleCompare(expression,"i") == 0)
2657 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2658 break;
2659 }
2660 case 'J':
2661 case 'j':
2662 {
2663 if (LocaleCompare(expression,"j") == 0)
2664 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
cristy161b9262010-03-20 19:34:32 +00002665#if defined(MAGICKCORE_HAVE_J0)
cristyee56cf12010-03-01 22:17:06 +00002666 if (LocaleNCompare(expression,"j0",2) == 0)
2667 {
2668 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2669 exception);
2670 return((MagickRealType) j0((double) alpha));
2671 }
cristy161b9262010-03-20 19:34:32 +00002672#endif
2673#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002674 if (LocaleNCompare(expression,"j1",2) == 0)
2675 {
2676 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2677 exception);
2678 return((MagickRealType) j1((double) alpha));
2679 }
cristy161b9262010-03-20 19:34:32 +00002680#endif
cristyaa018fa2010-04-08 23:03:54 +00002681#if defined(MAGICKCORE_HAVE_J1)
cristya6a09e72010-03-02 14:51:02 +00002682 if (LocaleNCompare(expression,"jinc",4) == 0)
2683 {
2684 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2685 exception);
cristy0946a822010-03-12 17:14:58 +00002686 if (alpha == 0.0)
2687 return(1.0);
cristy69928f92010-03-12 13:27:09 +00002688 gamma=(MagickRealType) (2.0*j1((double) (MagickPI*alpha))/
cristyfce2f7b2010-03-12 00:29:49 +00002689 (MagickPI*alpha));
2690 return(gamma);
cristya6a09e72010-03-02 14:51:02 +00002691 }
cristyaa018fa2010-04-08 23:03:54 +00002692#endif
cristy3ed852e2009-09-05 21:47:34 +00002693 break;
2694 }
2695 case 'L':
2696 case 'l':
2697 {
2698 if (LocaleNCompare(expression,"ln",2) == 0)
2699 {
2700 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2701 exception);
2702 return((MagickRealType) log((double) alpha));
2703 }
cristyc8ed5322010-08-31 12:07:59 +00002704 if (LocaleNCompare(expression,"logtwo",6) == 0)
cristy3ed852e2009-09-05 21:47:34 +00002705 {
cristyc8ed5322010-08-31 12:07:59 +00002706 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
cristy3ed852e2009-09-05 21:47:34 +00002707 exception);
2708 return((MagickRealType) log10((double) alpha))/log10(2.0);
2709 }
2710 if (LocaleNCompare(expression,"log",3) == 0)
2711 {
2712 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2713 exception);
2714 return((MagickRealType) log10((double) alpha));
2715 }
2716 if (LocaleCompare(expression,"lightness") == 0)
2717 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2718 break;
2719 }
2720 case 'M':
2721 case 'm':
2722 {
2723 if (LocaleCompare(expression,"MaxRGB") == 0)
2724 return((MagickRealType) QuantumRange);
2725 if (LocaleNCompare(expression,"maxima",6) == 0)
2726 break;
2727 if (LocaleNCompare(expression,"max",3) == 0)
2728 return(FxMax(fx_info,channel,x,y,expression+3,exception));
2729 if (LocaleNCompare(expression,"minima",6) == 0)
2730 break;
2731 if (LocaleNCompare(expression,"min",3) == 0)
2732 return(FxMin(fx_info,channel,x,y,expression+3,exception));
2733 if (LocaleNCompare(expression,"mod",3) == 0)
2734 {
2735 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2736 exception);
2737 return((MagickRealType) fmod((double) alpha,(double) *beta));
2738 }
2739 if (LocaleCompare(expression,"m") == 0)
2740 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2741 break;
2742 }
2743 case 'N':
2744 case 'n':
2745 {
cristyad3502e2011-11-02 19:10:45 +00002746 if (LocaleNCompare(expression,"not",3) == 0)
2747 {
2748 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2749 exception);
2750 return((MagickRealType) (alpha < MagickEpsilon));
2751 }
cristy3ed852e2009-09-05 21:47:34 +00002752 if (LocaleCompare(expression,"n") == 0)
2753 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2754 break;
2755 }
2756 case 'O':
2757 case 'o':
2758 {
2759 if (LocaleCompare(expression,"Opaque") == 0)
2760 return(1.0);
2761 if (LocaleCompare(expression,"o") == 0)
2762 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2763 break;
2764 }
2765 case 'P':
2766 case 'p':
2767 {
2768 if (LocaleCompare(expression,"pi") == 0)
2769 return((MagickRealType) MagickPI);
2770 if (LocaleNCompare(expression,"pow",3) == 0)
2771 {
2772 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2773 exception);
2774 return((MagickRealType) pow((double) alpha,(double) *beta));
2775 }
2776 if (LocaleCompare(expression,"p") == 0)
2777 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2778 break;
2779 }
2780 case 'Q':
2781 case 'q':
2782 {
2783 if (LocaleCompare(expression,"QuantumRange") == 0)
2784 return((MagickRealType) QuantumRange);
2785 if (LocaleCompare(expression,"QuantumScale") == 0)
2786 return((MagickRealType) QuantumScale);
2787 break;
2788 }
2789 case 'R':
2790 case 'r':
2791 {
2792 if (LocaleNCompare(expression,"rand",4) == 0)
2793 return((MagickRealType) GetPseudoRandomValue(fx_info->random_info));
2794 if (LocaleNCompare(expression,"round",5) == 0)
2795 {
2796 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2797 exception);
cristy16788e42010-08-13 13:44:26 +00002798 return((MagickRealType) floor((double) alpha+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002799 }
2800 if (LocaleCompare(expression,"r") == 0)
2801 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2802 break;
2803 }
2804 case 'S':
2805 case 's':
2806 {
2807 if (LocaleCompare(expression,"saturation") == 0)
2808 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2809 if (LocaleNCompare(expression,"sign",4) == 0)
2810 {
2811 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2812 exception);
2813 return(alpha < 0.0 ? -1.0 : 1.0);
2814 }
cristya6a09e72010-03-02 14:51:02 +00002815 if (LocaleNCompare(expression,"sinc",4) == 0)
2816 {
2817 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2818 exception);
2819 if (alpha == 0)
2820 return(1.0);
2821 gamma=(MagickRealType) (sin((double) (MagickPI*alpha))/
2822 (MagickPI*alpha));
2823 return(gamma);
2824 }
cristy3ed852e2009-09-05 21:47:34 +00002825 if (LocaleNCompare(expression,"sinh",4) == 0)
2826 {
2827 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2828 exception);
2829 return((MagickRealType) sinh((double) alpha));
2830 }
2831 if (LocaleNCompare(expression,"sin",3) == 0)
2832 {
2833 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2834 exception);
2835 return((MagickRealType) sin((double) alpha));
2836 }
2837 if (LocaleNCompare(expression,"sqrt",4) == 0)
2838 {
2839 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2840 exception);
2841 return((MagickRealType) sqrt((double) alpha));
2842 }
cristy9eeedea2011-11-02 19:04:05 +00002843 if (LocaleNCompare(expression,"squish",6) == 0)
2844 {
2845 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2846 exception);
2847 return((MagickRealType) (1.0/(1.0+exp((double) (4.0*alpha)))));
2848 }
cristy3ed852e2009-09-05 21:47:34 +00002849 if (LocaleCompare(expression,"s") == 0)
2850 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2851 break;
2852 }
2853 case 'T':
2854 case 't':
2855 {
2856 if (LocaleNCompare(expression,"tanh",4) == 0)
2857 {
2858 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2859 exception);
2860 return((MagickRealType) tanh((double) alpha));
2861 }
2862 if (LocaleNCompare(expression,"tan",3) == 0)
2863 {
2864 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2865 exception);
2866 return((MagickRealType) tan((double) alpha));
2867 }
2868 if (LocaleCompare(expression,"Transparent") == 0)
2869 return(0.0);
cristy16788e42010-08-13 13:44:26 +00002870 if (LocaleNCompare(expression,"trunc",5) == 0)
2871 {
2872 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2873 exception);
2874 if (alpha >= 0.0)
2875 return((MagickRealType) floor((double) alpha));
2876 return((MagickRealType) ceil((double) alpha));
2877 }
cristy3ed852e2009-09-05 21:47:34 +00002878 if (LocaleCompare(expression,"t") == 0)
2879 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2880 break;
2881 }
2882 case 'U':
2883 case 'u':
2884 {
2885 if (LocaleCompare(expression,"u") == 0)
2886 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2887 break;
2888 }
2889 case 'V':
2890 case 'v':
2891 {
2892 if (LocaleCompare(expression,"v") == 0)
2893 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2894 break;
2895 }
2896 case 'W':
2897 case 'w':
2898 {
cristy9eeedea2011-11-02 19:04:05 +00002899 if (LocaleNCompare(expression,"while",5) == 0)
2900 {
2901 do
2902 {
2903 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2904 exception);
2905 } while (fabs((double) alpha) >= MagickEpsilon);
2906 return((MagickRealType) *beta);
2907 }
cristy3ed852e2009-09-05 21:47:34 +00002908 if (LocaleCompare(expression,"w") == 0)
2909 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2910 break;
2911 }
2912 case 'Y':
2913 case 'y':
2914 {
2915 if (LocaleCompare(expression,"y") == 0)
2916 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2917 break;
2918 }
2919 case 'Z':
2920 case 'z':
2921 {
2922 if (LocaleCompare(expression,"z") == 0)
2923 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2924 break;
2925 }
2926 default:
2927 break;
2928 }
2929 q=(char *) expression;
cristyc1acd842011-05-19 23:05:47 +00002930 alpha=InterpretLocaleValue(expression,&q);
cristy3ed852e2009-09-05 21:47:34 +00002931 if (q == expression)
2932 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2933 return(alpha);
2934}
2935
cristy7832dc22011-09-05 01:21:53 +00002936MagickPrivate MagickBooleanType FxEvaluateExpression(FxInfo *fx_info,
cristy3ed852e2009-09-05 21:47:34 +00002937 MagickRealType *alpha,ExceptionInfo *exception)
2938{
2939 MagickBooleanType
2940 status;
2941
cristy541ae572011-08-05 19:08:59 +00002942 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2943 exception);
cristy3ed852e2009-09-05 21:47:34 +00002944 return(status);
2945}
2946
2947MagickExport MagickBooleanType FxPreprocessExpression(FxInfo *fx_info,
2948 MagickRealType *alpha,ExceptionInfo *exception)
2949{
2950 FILE
2951 *file;
2952
2953 MagickBooleanType
2954 status;
2955
2956 file=fx_info->file;
2957 fx_info->file=(FILE *) NULL;
cristy541ae572011-08-05 19:08:59 +00002958 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2959 exception);
cristy3ed852e2009-09-05 21:47:34 +00002960 fx_info->file=file;
2961 return(status);
2962}
2963
cristy7832dc22011-09-05 01:21:53 +00002964MagickPrivate MagickBooleanType FxEvaluateChannelExpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002965 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristye85007d2010-06-06 22:51:36 +00002966 MagickRealType *alpha,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002967{
2968 MagickRealType
2969 beta;
2970
2971 beta=0.0;
2972 *alpha=FxEvaluateSubexpression(fx_info,channel,x,y,fx_info->expression,&beta,
2973 exception);
2974 return(exception->severity == OptionError ? MagickFalse : MagickTrue);
2975}
2976
2977/*
2978%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2979% %
2980% %
2981% %
2982% F x I m a g e %
2983% %
2984% %
2985% %
2986%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2987%
2988% FxImage() applies a mathematical expression to the specified image.
2989%
2990% The format of the FxImage method is:
2991%
2992% Image *FxImage(const Image *image,const char *expression,
2993% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002994%
2995% A description of each parameter follows:
2996%
2997% o image: the image.
2998%
cristy3ed852e2009-09-05 21:47:34 +00002999% o expression: A mathematical expression.
3000%
3001% o exception: return any errors or warnings in this structure.
3002%
3003*/
3004
3005static FxInfo **DestroyFxThreadSet(FxInfo **fx_info)
3006{
cristybb503372010-05-27 20:51:26 +00003007 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003008 i;
3009
3010 assert(fx_info != (FxInfo **) NULL);
cristybb503372010-05-27 20:51:26 +00003011 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
cristy3ed852e2009-09-05 21:47:34 +00003012 if (fx_info[i] != (FxInfo *) NULL)
3013 fx_info[i]=DestroyFxInfo(fx_info[i]);
cristyb41ee102010-10-04 16:46:15 +00003014 fx_info=(FxInfo **) RelinquishMagickMemory(fx_info);
cristy3ed852e2009-09-05 21:47:34 +00003015 return(fx_info);
3016}
3017
3018static FxInfo **AcquireFxThreadSet(const Image *image,const char *expression,
3019 ExceptionInfo *exception)
3020{
3021 char
3022 *fx_expression;
3023
3024 FxInfo
3025 **fx_info;
3026
3027 MagickRealType
3028 alpha;
3029
cristybb503372010-05-27 20:51:26 +00003030 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003031 i;
3032
cristybb503372010-05-27 20:51:26 +00003033 size_t
cristy3ed852e2009-09-05 21:47:34 +00003034 number_threads;
3035
3036 number_threads=GetOpenMPMaximumThreads();
cristyb41ee102010-10-04 16:46:15 +00003037 fx_info=(FxInfo **) AcquireQuantumMemory(number_threads,sizeof(*fx_info));
cristy3ed852e2009-09-05 21:47:34 +00003038 if (fx_info == (FxInfo **) NULL)
3039 return((FxInfo **) NULL);
3040 (void) ResetMagickMemory(fx_info,0,number_threads*sizeof(*fx_info));
3041 if (*expression != '@')
3042 fx_expression=ConstantString(expression);
3043 else
3044 fx_expression=FileToString(expression+1,~0,exception);
cristybb503372010-05-27 20:51:26 +00003045 for (i=0; i < (ssize_t) number_threads; i++)
cristy3ed852e2009-09-05 21:47:34 +00003046 {
3047 fx_info[i]=AcquireFxInfo(image,fx_expression);
3048 if (fx_info[i] == (FxInfo *) NULL)
3049 return(DestroyFxThreadSet(fx_info));
3050 (void) FxPreprocessExpression(fx_info[i],&alpha,fx_info[i]->exception);
3051 }
3052 fx_expression=DestroyString(fx_expression);
3053 return(fx_info);
3054}
3055
3056MagickExport Image *FxImage(const Image *image,const char *expression,
3057 ExceptionInfo *exception)
3058{
cristy3ed852e2009-09-05 21:47:34 +00003059#define FxImageTag "Fx/Image"
3060
cristyfa112112010-01-04 17:48:07 +00003061 CacheView
cristy79cedc72011-07-25 00:41:15 +00003062 *fx_view,
3063 *image_view;
cristyfa112112010-01-04 17:48:07 +00003064
cristy3ed852e2009-09-05 21:47:34 +00003065 FxInfo
cristyfa112112010-01-04 17:48:07 +00003066 **restrict fx_info;
cristy3ed852e2009-09-05 21:47:34 +00003067
3068 Image
3069 *fx_image;
3070
cristy3ed852e2009-09-05 21:47:34 +00003071 MagickBooleanType
3072 status;
3073
cristybb503372010-05-27 20:51:26 +00003074 MagickOffsetType
3075 progress;
3076
cristy3ed852e2009-09-05 21:47:34 +00003077 MagickRealType
3078 alpha;
3079
cristybb503372010-05-27 20:51:26 +00003080 ssize_t
3081 y;
3082
cristy3ed852e2009-09-05 21:47:34 +00003083 assert(image != (Image *) NULL);
3084 assert(image->signature == MagickSignature);
3085 if (image->debug != MagickFalse)
3086 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy79cedc72011-07-25 00:41:15 +00003087 fx_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00003088 if (fx_image == (Image *) NULL)
3089 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003090 if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003091 {
cristy3ed852e2009-09-05 21:47:34 +00003092 fx_image=DestroyImage(fx_image);
3093 return((Image *) NULL);
3094 }
3095 fx_info=AcquireFxThreadSet(image,expression,exception);
3096 if (fx_info == (FxInfo **) NULL)
3097 {
3098 fx_image=DestroyImage(fx_image);
3099 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3100 }
3101 status=FxPreprocessExpression(fx_info[0],&alpha,exception);
3102 if (status == MagickFalse)
3103 {
3104 fx_image=DestroyImage(fx_image);
3105 fx_info=DestroyFxThreadSet(fx_info);
3106 return((Image *) NULL);
3107 }
3108 /*
3109 Fx image.
3110 */
3111 status=MagickTrue;
3112 progress=0;
cristy79cedc72011-07-25 00:41:15 +00003113 image_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00003114 fx_view=AcquireCacheView(fx_image);
cristyb5d5f722009-11-04 03:03:49 +00003115#if defined(MAGICKCORE_OPENMP_SUPPORT)
3116 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003117#endif
cristybb503372010-05-27 20:51:26 +00003118 for (y=0; y < (ssize_t) fx_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003119 {
cristy5c9e6f22010-09-17 17:31:01 +00003120 const int
3121 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00003122
cristy79cedc72011-07-25 00:41:15 +00003123 register const Quantum
3124 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003125
cristy4c08aed2011-07-01 19:47:50 +00003126 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003127 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003128
cristy79cedc72011-07-25 00:41:15 +00003129 register ssize_t
3130 x;
3131
cristy3ed852e2009-09-05 21:47:34 +00003132 if (status == MagickFalse)
3133 continue;
cristy79cedc72011-07-25 00:41:15 +00003134 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy3ed852e2009-09-05 21:47:34 +00003135 q=GetCacheViewAuthenticPixels(fx_view,0,y,fx_image->columns,1,exception);
cristy79cedc72011-07-25 00:41:15 +00003136 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003137 {
3138 status=MagickFalse;
3139 continue;
3140 }
cristybb503372010-05-27 20:51:26 +00003141 for (x=0; x < (ssize_t) fx_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003142 {
cristy79cedc72011-07-25 00:41:15 +00003143 register ssize_t
3144 i;
3145
3146 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3147 {
3148 MagickRealType
3149 alpha;
3150
3151 PixelChannel
3152 channel;
3153
3154 PixelTrait
3155 fx_traits,
3156 traits;
3157
3158 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
cristy79cedc72011-07-25 00:41:15 +00003159 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
3160 fx_traits=GetPixelChannelMapTraits(fx_image,channel);
cristy010d7d12011-08-31 01:02:48 +00003161 if ((traits == UndefinedPixelTrait) ||
3162 (fx_traits == UndefinedPixelTrait))
cristy79cedc72011-07-25 00:41:15 +00003163 continue;
3164 if ((fx_traits & CopyPixelTrait) != 0)
3165 {
cristy0beccfa2011-09-25 20:47:53 +00003166 SetPixelChannel(fx_image,channel,p[i],q);
cristy79cedc72011-07-25 00:41:15 +00003167 continue;
3168 }
3169 alpha=0.0;
cristy0568ffc2011-07-25 16:54:14 +00003170 (void) FxEvaluateChannelExpression(fx_info[id],(PixelChannel) i,x,y,
3171 &alpha,exception);
cristyb3a73b52011-07-26 01:34:43 +00003172 q[i]=ClampToQuantum((MagickRealType) QuantumRange*alpha);
cristy79cedc72011-07-25 00:41:15 +00003173 }
3174 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003175 q+=GetPixelChannels(fx_image);
cristy3ed852e2009-09-05 21:47:34 +00003176 }
3177 if (SyncCacheViewAuthenticPixels(fx_view,exception) == MagickFalse)
3178 status=MagickFalse;
3179 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3180 {
3181 MagickBooleanType
3182 proceed;
3183
cristyb5d5f722009-11-04 03:03:49 +00003184#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy490408a2011-07-07 14:42:05 +00003185 #pragma omp critical (MagickCore_FxImage)
cristy3ed852e2009-09-05 21:47:34 +00003186#endif
3187 proceed=SetImageProgress(image,FxImageTag,progress++,image->rows);
3188 if (proceed == MagickFalse)
3189 status=MagickFalse;
3190 }
3191 }
cristy3ed852e2009-09-05 21:47:34 +00003192 fx_view=DestroyCacheView(fx_view);
cristy79cedc72011-07-25 00:41:15 +00003193 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003194 fx_info=DestroyFxThreadSet(fx_info);
3195 if (status == MagickFalse)
3196 fx_image=DestroyImage(fx_image);
3197 return(fx_image);
3198}
3199
3200/*
3201%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3202% %
3203% %
3204% %
3205% I m p l o d e I m a g e %
3206% %
3207% %
3208% %
3209%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3210%
3211% ImplodeImage() creates a new image that is a copy of an existing
3212% one with the image pixels "implode" by the specified percentage. It
3213% allocates the memory necessary for the new Image structure and returns a
3214% pointer to the new image.
3215%
3216% The format of the ImplodeImage method is:
3217%
3218% Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003219% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003220%
3221% A description of each parameter follows:
3222%
3223% o implode_image: Method ImplodeImage returns a pointer to the image
3224% after it is implode. A null image is returned if there is a memory
3225% shortage.
3226%
3227% o image: the image.
3228%
3229% o amount: Define the extent of the implosion.
3230%
cristy76f512e2011-09-12 01:26:56 +00003231% o method: the pixel interpolation method.
3232%
cristy3ed852e2009-09-05 21:47:34 +00003233% o exception: return any errors or warnings in this structure.
3234%
3235*/
3236MagickExport Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003237 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003238{
3239#define ImplodeImageTag "Implode/Image"
3240
cristyfa112112010-01-04 17:48:07 +00003241 CacheView
3242 *image_view,
3243 *implode_view;
3244
cristy3ed852e2009-09-05 21:47:34 +00003245 Image
3246 *implode_image;
3247
cristy3ed852e2009-09-05 21:47:34 +00003248 MagickBooleanType
3249 status;
3250
cristybb503372010-05-27 20:51:26 +00003251 MagickOffsetType
3252 progress;
3253
cristy3ed852e2009-09-05 21:47:34 +00003254 MagickRealType
3255 radius;
3256
3257 PointInfo
3258 center,
3259 scale;
3260
cristybb503372010-05-27 20:51:26 +00003261 ssize_t
3262 y;
3263
cristy3ed852e2009-09-05 21:47:34 +00003264 /*
3265 Initialize implode image attributes.
3266 */
3267 assert(image != (Image *) NULL);
3268 assert(image->signature == MagickSignature);
3269 if (image->debug != MagickFalse)
3270 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3271 assert(exception != (ExceptionInfo *) NULL);
3272 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00003273 implode_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3274 exception);
cristy3ed852e2009-09-05 21:47:34 +00003275 if (implode_image == (Image *) NULL)
3276 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003277 if (SetImageStorageClass(implode_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003278 {
cristy3ed852e2009-09-05 21:47:34 +00003279 implode_image=DestroyImage(implode_image);
3280 return((Image *) NULL);
3281 }
cristy4c08aed2011-07-01 19:47:50 +00003282 if (implode_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00003283 implode_image->matte=MagickTrue;
3284 /*
3285 Compute scaling factor.
3286 */
3287 scale.x=1.0;
3288 scale.y=1.0;
3289 center.x=0.5*image->columns;
3290 center.y=0.5*image->rows;
3291 radius=center.x;
3292 if (image->columns > image->rows)
3293 scale.y=(double) image->columns/(double) image->rows;
3294 else
3295 if (image->columns < image->rows)
3296 {
3297 scale.x=(double) image->rows/(double) image->columns;
3298 radius=center.y;
3299 }
3300 /*
3301 Implode image.
3302 */
3303 status=MagickTrue;
3304 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00003305 image_view=AcquireCacheView(image);
3306 implode_view=AcquireCacheView(implode_image);
cristyb5d5f722009-11-04 03:03:49 +00003307#if defined(MAGICKCORE_OPENMP_SUPPORT)
3308 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003309#endif
cristybb503372010-05-27 20:51:26 +00003310 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003311 {
cristy3ed852e2009-09-05 21:47:34 +00003312 MagickRealType
3313 distance;
3314
3315 PointInfo
3316 delta;
3317
cristy6d188022011-09-12 13:23:33 +00003318 register const Quantum
3319 *restrict p;
3320
cristybb503372010-05-27 20:51:26 +00003321 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003322 x;
3323
cristy4c08aed2011-07-01 19:47:50 +00003324 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003325 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003326
3327 if (status == MagickFalse)
3328 continue;
cristy6d188022011-09-12 13:23:33 +00003329 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy3ed852e2009-09-05 21:47:34 +00003330 q=GetCacheViewAuthenticPixels(implode_view,0,y,implode_image->columns,1,
3331 exception);
cristy6d188022011-09-12 13:23:33 +00003332 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003333 {
3334 status=MagickFalse;
3335 continue;
3336 }
cristy3ed852e2009-09-05 21:47:34 +00003337 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00003338 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003339 {
cristy6d188022011-09-12 13:23:33 +00003340 register ssize_t
3341 i;
3342
cristy3ed852e2009-09-05 21:47:34 +00003343 /*
3344 Determine if the pixel is within an ellipse.
3345 */
3346 delta.x=scale.x*(double) (x-center.x);
3347 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00003348 if (distance >= (radius*radius))
3349 {
3350 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3351 q[i]=p[i];
3352 }
3353 else
cristy3ed852e2009-09-05 21:47:34 +00003354 {
3355 double
3356 factor;
3357
3358 /*
3359 Implode the pixel.
3360 */
3361 factor=1.0;
3362 if (distance > 0.0)
3363 factor=pow(sin((double) (MagickPI*sqrt((double) distance)/
3364 radius/2)),-amount);
cristy76f512e2011-09-12 01:26:56 +00003365 status=InterpolatePixelChannels(image,image_view,implode_image,method,
3366 (double) (factor*delta.x/scale.x+center.x),(double) (factor*delta.y/
3367 scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00003368 }
cristy6d188022011-09-12 13:23:33 +00003369 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003370 q+=GetPixelChannels(implode_image);
cristy3ed852e2009-09-05 21:47:34 +00003371 }
3372 if (SyncCacheViewAuthenticPixels(implode_view,exception) == MagickFalse)
3373 status=MagickFalse;
3374 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3375 {
3376 MagickBooleanType
3377 proceed;
3378
cristyb5d5f722009-11-04 03:03:49 +00003379#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003380 #pragma omp critical (MagickCore_ImplodeImage)
3381#endif
3382 proceed=SetImageProgress(image,ImplodeImageTag,progress++,image->rows);
3383 if (proceed == MagickFalse)
3384 status=MagickFalse;
3385 }
3386 }
3387 implode_view=DestroyCacheView(implode_view);
3388 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003389 if (status == MagickFalse)
3390 implode_image=DestroyImage(implode_image);
3391 return(implode_image);
3392}
3393
3394/*
3395%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3396% %
3397% %
3398% %
3399% M o r p h I m a g e s %
3400% %
3401% %
3402% %
3403%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3404%
3405% The MorphImages() method requires a minimum of two images. The first
3406% image is transformed into the second by a number of intervening images
3407% as specified by frames.
3408%
3409% The format of the MorphImage method is:
3410%
cristybb503372010-05-27 20:51:26 +00003411% Image *MorphImages(const Image *image,const size_t number_frames,
cristy3ed852e2009-09-05 21:47:34 +00003412% ExceptionInfo *exception)
3413%
3414% A description of each parameter follows:
3415%
3416% o image: the image.
3417%
3418% o number_frames: Define the number of in-between image to generate.
3419% The more in-between frames, the smoother the morph.
3420%
3421% o exception: return any errors or warnings in this structure.
3422%
3423*/
3424MagickExport Image *MorphImages(const Image *image,
cristybb503372010-05-27 20:51:26 +00003425 const size_t number_frames,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003426{
3427#define MorphImageTag "Morph/Image"
3428
3429 Image
3430 *morph_image,
3431 *morph_images;
3432
cristy9d314ff2011-03-09 01:30:28 +00003433 MagickBooleanType
3434 status;
cristy3ed852e2009-09-05 21:47:34 +00003435
3436 MagickOffsetType
3437 scene;
3438
3439 MagickRealType
3440 alpha,
3441 beta;
3442
3443 register const Image
3444 *next;
3445
cristybb503372010-05-27 20:51:26 +00003446 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003447 i;
3448
cristy9d314ff2011-03-09 01:30:28 +00003449 ssize_t
3450 y;
cristy3ed852e2009-09-05 21:47:34 +00003451
3452 /*
3453 Clone first frame in sequence.
3454 */
3455 assert(image != (Image *) NULL);
3456 assert(image->signature == MagickSignature);
3457 if (image->debug != MagickFalse)
3458 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3459 assert(exception != (ExceptionInfo *) NULL);
3460 assert(exception->signature == MagickSignature);
3461 morph_images=CloneImage(image,0,0,MagickTrue,exception);
3462 if (morph_images == (Image *) NULL)
3463 return((Image *) NULL);
3464 if (GetNextImageInList(image) == (Image *) NULL)
3465 {
3466 /*
3467 Morph single image.
3468 */
cristybb503372010-05-27 20:51:26 +00003469 for (i=1; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003470 {
3471 morph_image=CloneImage(image,0,0,MagickTrue,exception);
3472 if (morph_image == (Image *) NULL)
3473 {
3474 morph_images=DestroyImageList(morph_images);
3475 return((Image *) NULL);
3476 }
3477 AppendImageToList(&morph_images,morph_image);
cristy8b27a6d2010-02-14 03:31:15 +00003478 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003479 {
cristy8b27a6d2010-02-14 03:31:15 +00003480 MagickBooleanType
3481 proceed;
3482
cristybb503372010-05-27 20:51:26 +00003483 proceed=SetImageProgress(image,MorphImageTag,(MagickOffsetType) i,
3484 number_frames);
cristy8b27a6d2010-02-14 03:31:15 +00003485 if (proceed == MagickFalse)
3486 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00003487 }
3488 }
3489 return(GetFirstImageInList(morph_images));
3490 }
3491 /*
3492 Morph image sequence.
3493 */
3494 status=MagickTrue;
3495 scene=0;
3496 next=image;
3497 for ( ; GetNextImageInList(next) != (Image *) NULL; next=GetNextImageInList(next))
3498 {
cristybb503372010-05-27 20:51:26 +00003499 for (i=0; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003500 {
3501 CacheView
3502 *image_view,
3503 *morph_view;
3504
3505 beta=(MagickRealType) (i+1.0)/(MagickRealType) (number_frames+1.0);
3506 alpha=1.0-beta;
cristy15b98cd2010-09-12 19:42:50 +00003507 morph_image=ResizeImage(next,(size_t) (alpha*next->columns+beta*
cristybb503372010-05-27 20:51:26 +00003508 GetNextImageInList(next)->columns+0.5),(size_t) (alpha*
cristy15b98cd2010-09-12 19:42:50 +00003509 next->rows+beta*GetNextImageInList(next)->rows+0.5),
3510 next->filter,next->blur,exception);
cristy3ed852e2009-09-05 21:47:34 +00003511 if (morph_image == (Image *) NULL)
3512 {
3513 morph_images=DestroyImageList(morph_images);
3514 return((Image *) NULL);
3515 }
cristy574cc262011-08-05 01:23:58 +00003516 if (SetImageStorageClass(morph_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003517 {
cristy3ed852e2009-09-05 21:47:34 +00003518 morph_image=DestroyImage(morph_image);
3519 return((Image *) NULL);
3520 }
3521 AppendImageToList(&morph_images,morph_image);
3522 morph_images=GetLastImageInList(morph_images);
cristy15b98cd2010-09-12 19:42:50 +00003523 morph_image=ResizeImage(GetNextImageInList(next),morph_images->columns,
3524 morph_images->rows,GetNextImageInList(next)->filter,
3525 GetNextImageInList(next)->blur,exception);
cristy3ed852e2009-09-05 21:47:34 +00003526 if (morph_image == (Image *) NULL)
3527 {
3528 morph_images=DestroyImageList(morph_images);
3529 return((Image *) NULL);
3530 }
3531 image_view=AcquireCacheView(morph_image);
3532 morph_view=AcquireCacheView(morph_images);
cristyb5d5f722009-11-04 03:03:49 +00003533#if defined(MAGICKCORE_OPENMP_SUPPORT)
3534 #pragma omp parallel for schedule(dynamic,4) shared(status)
cristy3ed852e2009-09-05 21:47:34 +00003535#endif
cristybb503372010-05-27 20:51:26 +00003536 for (y=0; y < (ssize_t) morph_images->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003537 {
3538 MagickBooleanType
3539 sync;
3540
cristy4c08aed2011-07-01 19:47:50 +00003541 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003542 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003543
cristybb503372010-05-27 20:51:26 +00003544 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003545 x;
3546
cristy4c08aed2011-07-01 19:47:50 +00003547 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003548 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003549
3550 if (status == MagickFalse)
3551 continue;
3552 p=GetCacheViewVirtualPixels(image_view,0,y,morph_image->columns,1,
3553 exception);
3554 q=GetCacheViewAuthenticPixels(morph_view,0,y,morph_images->columns,1,
3555 exception);
cristy4c08aed2011-07-01 19:47:50 +00003556 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003557 {
3558 status=MagickFalse;
3559 continue;
3560 }
cristybb503372010-05-27 20:51:26 +00003561 for (x=0; x < (ssize_t) morph_images->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003562 {
cristy4c08aed2011-07-01 19:47:50 +00003563 SetPixelRed(morph_images,ClampToQuantum(alpha*
3564 GetPixelRed(morph_images,q)+beta*GetPixelRed(morph_image,p)),q);
3565 SetPixelGreen(morph_images,ClampToQuantum(alpha*
3566 GetPixelGreen(morph_images,q)+beta*GetPixelGreen(morph_image,p)),q);
3567 SetPixelBlue(morph_images,ClampToQuantum(alpha*
3568 GetPixelBlue(morph_images,q)+beta*GetPixelBlue(morph_image,p)),q);
3569 SetPixelAlpha(morph_images,ClampToQuantum(alpha*
3570 GetPixelAlpha(morph_images,q)+beta*GetPixelAlpha(morph_image,p)),q);
3571 if ((morph_image->colorspace == CMYKColorspace) &&
3572 (morph_images->colorspace == CMYKColorspace))
3573 SetPixelBlack(morph_images,ClampToQuantum(alpha*
3574 GetPixelBlack(morph_images,q)+beta*GetPixelBlack(morph_image,p)),
3575 q);
cristyed231572011-07-14 02:18:59 +00003576 p+=GetPixelChannels(morph_image);
3577 q+=GetPixelChannels(morph_images);
cristy3ed852e2009-09-05 21:47:34 +00003578 }
3579 sync=SyncCacheViewAuthenticPixels(morph_view,exception);
3580 if (sync == MagickFalse)
3581 status=MagickFalse;
3582 }
3583 morph_view=DestroyCacheView(morph_view);
3584 image_view=DestroyCacheView(image_view);
3585 morph_image=DestroyImage(morph_image);
3586 }
cristybb503372010-05-27 20:51:26 +00003587 if (i < (ssize_t) number_frames)
cristy3ed852e2009-09-05 21:47:34 +00003588 break;
3589 /*
3590 Clone last frame in sequence.
3591 */
3592 morph_image=CloneImage(GetNextImageInList(next),0,0,MagickTrue,exception);
3593 if (morph_image == (Image *) NULL)
3594 {
3595 morph_images=DestroyImageList(morph_images);
3596 return((Image *) NULL);
3597 }
3598 AppendImageToList(&morph_images,morph_image);
3599 morph_images=GetLastImageInList(morph_images);
3600 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3601 {
3602 MagickBooleanType
3603 proceed;
3604
cristyb5d5f722009-11-04 03:03:49 +00003605#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003606 #pragma omp critical (MagickCore_MorphImages)
3607#endif
3608 proceed=SetImageProgress(image,MorphImageTag,scene,
3609 GetImageListLength(image));
3610 if (proceed == MagickFalse)
3611 status=MagickFalse;
3612 }
3613 scene++;
3614 }
3615 if (GetNextImageInList(next) != (Image *) NULL)
3616 {
3617 morph_images=DestroyImageList(morph_images);
3618 return((Image *) NULL);
3619 }
3620 return(GetFirstImageInList(morph_images));
3621}
3622
3623/*
3624%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3625% %
3626% %
3627% %
3628% P l a s m a I m a g e %
3629% %
3630% %
3631% %
3632%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3633%
3634% PlasmaImage() initializes an image with plasma fractal values. The image
3635% must be initialized with a base color and the random number generator
3636% seeded before this method is called.
3637%
3638% The format of the PlasmaImage method is:
3639%
3640% MagickBooleanType PlasmaImage(Image *image,const SegmentInfo *segment,
cristy5cbc0162011-08-29 00:36:28 +00003641% size_t attenuate,size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003642%
3643% A description of each parameter follows:
3644%
3645% o image: the image.
3646%
3647% o segment: Define the region to apply plasma fractals values.
3648%
glennrp7dae1ca2010-09-16 12:17:35 +00003649% o attenuate: Define the plasma attenuation factor.
cristy3ed852e2009-09-05 21:47:34 +00003650%
3651% o depth: Limit the plasma recursion depth.
3652%
cristy5cbc0162011-08-29 00:36:28 +00003653% o exception: return any errors or warnings in this structure.
3654%
cristy3ed852e2009-09-05 21:47:34 +00003655*/
3656
3657static inline Quantum PlasmaPixel(RandomInfo *random_info,
3658 const MagickRealType pixel,const MagickRealType noise)
3659{
3660 Quantum
3661 plasma;
3662
cristyce70c172010-01-07 17:15:30 +00003663 plasma=ClampToQuantum(pixel+noise*GetPseudoRandomValue(random_info)-
cristy3ed852e2009-09-05 21:47:34 +00003664 noise/2.0);
3665 return(plasma);
3666}
3667
cristyda1f9c12011-10-02 21:39:49 +00003668static MagickBooleanType PlasmaImageProxy(Image *image,CacheView *image_view,
3669 CacheView *u_view,CacheView *v_view,RandomInfo *random_info,
3670 const SegmentInfo *segment,size_t attenuate,size_t depth,
3671 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003672{
cristy3ed852e2009-09-05 21:47:34 +00003673 MagickRealType
3674 plasma;
3675
cristyda1f9c12011-10-02 21:39:49 +00003676 PixelChannel
3677 channel;
3678
3679 PixelTrait
3680 traits;
3681
3682 register const Quantum
3683 *restrict u,
3684 *restrict v;
3685
3686 register Quantum
3687 *restrict q;
3688
3689 register ssize_t
3690 i;
cristy3ed852e2009-09-05 21:47:34 +00003691
cristy9d314ff2011-03-09 01:30:28 +00003692 ssize_t
3693 x,
3694 x_mid,
3695 y,
3696 y_mid;
3697
cristy3ed852e2009-09-05 21:47:34 +00003698 if (((segment->x2-segment->x1) == 0.0) && ((segment->y2-segment->y1) == 0.0))
3699 return(MagickTrue);
3700 if (depth != 0)
3701 {
3702 SegmentInfo
3703 local_info;
3704
3705 /*
3706 Divide the area into quadrants and recurse.
3707 */
3708 depth--;
3709 attenuate++;
cristybb503372010-05-27 20:51:26 +00003710 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3711 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003712 local_info=(*segment);
3713 local_info.x2=(double) x_mid;
3714 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003715 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3716 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003717 local_info=(*segment);
3718 local_info.y1=(double) y_mid;
3719 local_info.x2=(double) x_mid;
cristyda1f9c12011-10-02 21:39:49 +00003720 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3721 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003722 local_info=(*segment);
3723 local_info.x1=(double) x_mid;
3724 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003725 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3726 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003727 local_info=(*segment);
3728 local_info.x1=(double) x_mid;
3729 local_info.y1=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003730 return(PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3731 &local_info,attenuate,depth,exception));
cristy3ed852e2009-09-05 21:47:34 +00003732 }
cristybb503372010-05-27 20:51:26 +00003733 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3734 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003735 if ((segment->x1 == (double) x_mid) && (segment->x2 == (double) x_mid) &&
3736 (segment->y1 == (double) y_mid) && (segment->y2 == (double) y_mid))
3737 return(MagickFalse);
3738 /*
3739 Average pixels and apply plasma.
3740 */
cristy3ed852e2009-09-05 21:47:34 +00003741 plasma=(MagickRealType) QuantumRange/(2.0*attenuate);
3742 if ((segment->x1 != (double) x_mid) || (segment->x2 != (double) x_mid))
3743 {
cristy3ed852e2009-09-05 21:47:34 +00003744 /*
3745 Left pixel.
3746 */
cristybb503372010-05-27 20:51:26 +00003747 x=(ssize_t) ceil(segment->x1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003748 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3749 1,1,exception);
3750 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3751 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003752 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003753 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3754 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003755 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003756 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3757 {
3758 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3759 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
3760 if (traits == UndefinedPixelTrait)
3761 continue;
3762 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3763 }
cristyc5c6f662010-09-22 14:23:02 +00003764 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003765 if (segment->x1 != segment->x2)
3766 {
3767 /*
3768 Right pixel.
3769 */
cristybb503372010-05-27 20:51:26 +00003770 x=(ssize_t) ceil(segment->x2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003771 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3772 1,1,exception);
3773 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3774 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003775 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003776 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3777 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003778 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003779 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3780 {
3781 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3782 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
3783 if (traits == UndefinedPixelTrait)
3784 continue;
3785 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3786 }
cristyc5c6f662010-09-22 14:23:02 +00003787 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003788 }
3789 }
3790 if ((segment->y1 != (double) y_mid) || (segment->y2 != (double) y_mid))
3791 {
3792 if ((segment->x1 != (double) x_mid) || (segment->y2 != (double) y_mid))
3793 {
cristy3ed852e2009-09-05 21:47:34 +00003794 /*
3795 Bottom pixel.
3796 */
cristybb503372010-05-27 20:51:26 +00003797 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003798 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3799 1,1,exception);
3800 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3801 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003802 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003803 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3804 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003805 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003806 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3807 {
3808 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3809 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
3810 if (traits == UndefinedPixelTrait)
3811 continue;
3812 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3813 }
cristyc5c6f662010-09-22 14:23:02 +00003814 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003815 }
3816 if (segment->y1 != segment->y2)
3817 {
cristy3ed852e2009-09-05 21:47:34 +00003818 /*
3819 Top pixel.
3820 */
cristybb503372010-05-27 20:51:26 +00003821 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003822 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3823 1,1,exception);
3824 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3825 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003826 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003827 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3828 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003829 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003830 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3831 {
3832 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3833 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
3834 if (traits == UndefinedPixelTrait)
3835 continue;
3836 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3837 }
cristyc5c6f662010-09-22 14:23:02 +00003838 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003839 }
3840 }
3841 if ((segment->x1 != segment->x2) || (segment->y1 != segment->y2))
3842 {
cristy3ed852e2009-09-05 21:47:34 +00003843 /*
3844 Middle pixel.
3845 */
cristybb503372010-05-27 20:51:26 +00003846 x=(ssize_t) ceil(segment->x1-0.5);
3847 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003848 u=GetCacheViewVirtualPixels(u_view,x,y,1,1,exception);
cristybb503372010-05-27 20:51:26 +00003849 x=(ssize_t) ceil(segment->x2-0.5);
3850 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003851 v=GetCacheViewVirtualPixels(v_view,x,y,1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003852 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003853 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3854 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003855 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003856 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3857 {
3858 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3859 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
3860 if (traits == UndefinedPixelTrait)
3861 continue;
3862 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3863 }
cristyc5c6f662010-09-22 14:23:02 +00003864 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003865 }
3866 if (((segment->x2-segment->x1) < 3.0) && ((segment->y2-segment->y1) < 3.0))
3867 return(MagickTrue);
3868 return(MagickFalse);
3869}
cristyda1f9c12011-10-02 21:39:49 +00003870
cristy3ed852e2009-09-05 21:47:34 +00003871MagickExport MagickBooleanType PlasmaImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00003872 const SegmentInfo *segment,size_t attenuate,size_t depth,
3873 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003874{
cristyc5c6f662010-09-22 14:23:02 +00003875 CacheView
cristyda1f9c12011-10-02 21:39:49 +00003876 *image_view,
3877 *u_view,
3878 *v_view;
cristyc5c6f662010-09-22 14:23:02 +00003879
cristy3ed852e2009-09-05 21:47:34 +00003880 MagickBooleanType
3881 status;
3882
3883 RandomInfo
3884 *random_info;
3885
3886 if (image->debug != MagickFalse)
3887 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3888 assert(image != (Image *) NULL);
3889 assert(image->signature == MagickSignature);
3890 if (image->debug != MagickFalse)
3891 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
cristy5cbc0162011-08-29 00:36:28 +00003892 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristyc5c6f662010-09-22 14:23:02 +00003893 return(MagickFalse);
3894 image_view=AcquireCacheView(image);
cristyda1f9c12011-10-02 21:39:49 +00003895 u_view=AcquireCacheView(image);
3896 v_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00003897 random_info=AcquireRandomInfo();
cristyda1f9c12011-10-02 21:39:49 +00003898 status=PlasmaImageProxy(image,image_view,u_view,v_view,random_info,segment,
3899 attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003900 random_info=DestroyRandomInfo(random_info);
cristyda1f9c12011-10-02 21:39:49 +00003901 v_view=DestroyCacheView(v_view);
3902 u_view=DestroyCacheView(u_view);
cristyc5c6f662010-09-22 14:23:02 +00003903 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003904 return(status);
3905}
3906
3907/*
3908%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3909% %
3910% %
3911% %
3912% P o l a r o i d I m a g e %
3913% %
3914% %
3915% %
3916%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3917%
3918% PolaroidImage() simulates a Polaroid picture.
3919%
3920% The format of the AnnotateImage method is:
3921%
3922% Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristy5c4e2582011-09-11 19:21:03 +00003923% const double angle,const PixelInterpolateMethod method,
3924% ExceptionInfo exception)
cristy3ed852e2009-09-05 21:47:34 +00003925%
3926% A description of each parameter follows:
3927%
3928% o image: the image.
3929%
3930% o draw_info: the draw info.
3931%
cristycee97112010-05-28 00:44:52 +00003932% o angle: Apply the effect along this angle.
cristy3ed852e2009-09-05 21:47:34 +00003933%
cristy5c4e2582011-09-11 19:21:03 +00003934% o method: the pixel interpolation method.
3935%
cristy3ed852e2009-09-05 21:47:34 +00003936% o exception: return any errors or warnings in this structure.
3937%
3938*/
3939MagickExport Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristy5c4e2582011-09-11 19:21:03 +00003940 const double angle,const PixelInterpolateMethod method,
3941 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003942{
3943 const char
3944 *value;
3945
cristy3ed852e2009-09-05 21:47:34 +00003946 Image
3947 *bend_image,
3948 *caption_image,
3949 *flop_image,
3950 *picture_image,
3951 *polaroid_image,
3952 *rotate_image,
3953 *trim_image;
3954
cristybb503372010-05-27 20:51:26 +00003955 size_t
cristy3ed852e2009-09-05 21:47:34 +00003956 height;
3957
cristy9d314ff2011-03-09 01:30:28 +00003958 ssize_t
3959 quantum;
3960
cristy3ed852e2009-09-05 21:47:34 +00003961 /*
3962 Simulate a Polaroid picture.
3963 */
3964 assert(image != (Image *) NULL);
3965 assert(image->signature == MagickSignature);
3966 if (image->debug != MagickFalse)
3967 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3968 assert(exception != (ExceptionInfo *) NULL);
3969 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00003970 quantum=(ssize_t) MagickMax(MagickMax((double) image->columns,(double)
cristy3ed852e2009-09-05 21:47:34 +00003971 image->rows)/25.0,10.0);
3972 height=image->rows+2*quantum;
3973 caption_image=(Image *) NULL;
cristyd15e6592011-10-15 00:13:06 +00003974 value=GetImageProperty(image,"caption",exception);
cristy3ed852e2009-09-05 21:47:34 +00003975 if (value != (const char *) NULL)
3976 {
3977 char
3978 *caption,
3979 geometry[MaxTextExtent];
3980
3981 DrawInfo
3982 *annotate_info;
3983
cristy3ed852e2009-09-05 21:47:34 +00003984 MagickBooleanType
3985 status;
3986
cristy9d314ff2011-03-09 01:30:28 +00003987 ssize_t
3988 count;
3989
cristy3ed852e2009-09-05 21:47:34 +00003990 TypeMetric
3991 metrics;
3992
3993 /*
3994 Generate caption image.
3995 */
3996 caption_image=CloneImage(image,image->columns,1,MagickTrue,exception);
3997 if (caption_image == (Image *) NULL)
3998 return((Image *) NULL);
3999 annotate_info=CloneDrawInfo((const ImageInfo *) NULL,draw_info);
4000 caption=InterpretImageProperties((ImageInfo *) NULL,(Image *) image,
cristy018f07f2011-09-04 21:15:19 +00004001 value,exception);
cristy3ed852e2009-09-05 21:47:34 +00004002 (void) CloneString(&annotate_info->text,caption);
cristy6b1d05e2010-09-22 19:17:27 +00004003 count=FormatMagickCaption(caption_image,annotate_info,MagickTrue,&metrics,
cristy5cbc0162011-08-29 00:36:28 +00004004 &caption,exception);
cristybb503372010-05-27 20:51:26 +00004005 status=SetImageExtent(caption_image,image->columns,(size_t)
cristy63240882011-08-05 19:05:27 +00004006 ((count+1)*(metrics.ascent-metrics.descent)+0.5),exception);
cristy3ed852e2009-09-05 21:47:34 +00004007 if (status == MagickFalse)
4008 caption_image=DestroyImage(caption_image);
4009 else
4010 {
4011 caption_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00004012 (void) SetImageBackgroundColor(caption_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00004013 (void) CloneString(&annotate_info->text,caption);
cristyb51dff52011-05-19 16:55:47 +00004014 (void) FormatLocaleString(geometry,MaxTextExtent,"+0+%g",
cristy3ed852e2009-09-05 21:47:34 +00004015 metrics.ascent);
4016 if (annotate_info->gravity == UndefinedGravity)
4017 (void) CloneString(&annotate_info->geometry,AcquireString(
4018 geometry));
cristy5cbc0162011-08-29 00:36:28 +00004019 (void) AnnotateImage(caption_image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00004020 height+=caption_image->rows;
4021 }
4022 annotate_info=DestroyDrawInfo(annotate_info);
4023 caption=DestroyString(caption);
4024 }
4025 picture_image=CloneImage(image,image->columns+2*quantum,height,MagickTrue,
4026 exception);
4027 if (picture_image == (Image *) NULL)
4028 {
4029 if (caption_image != (Image *) NULL)
4030 caption_image=DestroyImage(caption_image);
4031 return((Image *) NULL);
4032 }
4033 picture_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00004034 (void) SetImageBackgroundColor(picture_image,exception);
cristye941a752011-10-15 01:52:48 +00004035 (void) CompositeImage(picture_image,OverCompositeOp,image,quantum,quantum,
4036 exception);
cristy3ed852e2009-09-05 21:47:34 +00004037 if (caption_image != (Image *) NULL)
4038 {
4039 (void) CompositeImage(picture_image,OverCompositeOp,caption_image,
cristye941a752011-10-15 01:52:48 +00004040 quantum,(ssize_t) (image->rows+3*quantum/2),exception);
cristy3ed852e2009-09-05 21:47:34 +00004041 caption_image=DestroyImage(caption_image);
4042 }
cristy9950d572011-10-01 18:22:35 +00004043 (void) QueryColorCompliance("none",AllCompliance,
4044 &picture_image->background_color,exception);
cristy63240882011-08-05 19:05:27 +00004045 (void) SetImageAlphaChannel(picture_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004046 rotate_image=RotateImage(picture_image,90.0,exception);
4047 picture_image=DestroyImage(picture_image);
4048 if (rotate_image == (Image *) NULL)
4049 return((Image *) NULL);
4050 picture_image=rotate_image;
4051 bend_image=WaveImage(picture_image,0.01*picture_image->rows,2.0*
cristy5c4e2582011-09-11 19:21:03 +00004052 picture_image->columns,method,exception);
cristy3ed852e2009-09-05 21:47:34 +00004053 picture_image=DestroyImage(picture_image);
4054 if (bend_image == (Image *) NULL)
4055 return((Image *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00004056 picture_image=bend_image;
4057 rotate_image=RotateImage(picture_image,-90.0,exception);
4058 picture_image=DestroyImage(picture_image);
4059 if (rotate_image == (Image *) NULL)
4060 return((Image *) NULL);
4061 picture_image=rotate_image;
4062 picture_image->background_color=image->background_color;
4063 polaroid_image=ShadowImage(picture_image,80.0,2.0,quantum/3,quantum/3,
4064 exception);
4065 if (polaroid_image == (Image *) NULL)
4066 {
4067 picture_image=DestroyImage(picture_image);
4068 return(picture_image);
4069 }
4070 flop_image=FlopImage(polaroid_image,exception);
4071 polaroid_image=DestroyImage(polaroid_image);
4072 if (flop_image == (Image *) NULL)
4073 {
4074 picture_image=DestroyImage(picture_image);
4075 return(picture_image);
4076 }
4077 polaroid_image=flop_image;
4078 (void) CompositeImage(polaroid_image,OverCompositeOp,picture_image,
cristye941a752011-10-15 01:52:48 +00004079 (ssize_t) (-0.01*picture_image->columns/2.0),0L,exception);
cristy3ed852e2009-09-05 21:47:34 +00004080 picture_image=DestroyImage(picture_image);
cristy9950d572011-10-01 18:22:35 +00004081 (void) QueryColorCompliance("none",AllCompliance,
4082 &polaroid_image->background_color,exception);
cristy3ed852e2009-09-05 21:47:34 +00004083 rotate_image=RotateImage(polaroid_image,angle,exception);
4084 polaroid_image=DestroyImage(polaroid_image);
4085 if (rotate_image == (Image *) NULL)
4086 return((Image *) NULL);
4087 polaroid_image=rotate_image;
4088 trim_image=TrimImage(polaroid_image,exception);
4089 polaroid_image=DestroyImage(polaroid_image);
4090 if (trim_image == (Image *) NULL)
4091 return((Image *) NULL);
4092 polaroid_image=trim_image;
4093 return(polaroid_image);
4094}
4095
4096/*
4097%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4098% %
4099% %
4100% %
cristy3ed852e2009-09-05 21:47:34 +00004101% S e p i a T o n e I m a g e %
4102% %
4103% %
4104% %
4105%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4106%
4107% MagickSepiaToneImage() applies a special effect to the image, similar to the
4108% effect achieved in a photo darkroom by sepia toning. Threshold ranges from
4109% 0 to QuantumRange and is a measure of the extent of the sepia toning. A
4110% threshold of 80% is a good starting point for a reasonable tone.
4111%
4112% The format of the SepiaToneImage method is:
4113%
4114% Image *SepiaToneImage(const Image *image,const double threshold,
4115% ExceptionInfo *exception)
4116%
4117% A description of each parameter follows:
4118%
4119% o image: the image.
4120%
4121% o threshold: the tone threshold.
4122%
4123% o exception: return any errors or warnings in this structure.
4124%
4125*/
4126MagickExport Image *SepiaToneImage(const Image *image,const double threshold,
4127 ExceptionInfo *exception)
4128{
4129#define SepiaToneImageTag "SepiaTone/Image"
4130
cristyc4c8d132010-01-07 01:58:38 +00004131 CacheView
4132 *image_view,
4133 *sepia_view;
4134
cristy3ed852e2009-09-05 21:47:34 +00004135 Image
4136 *sepia_image;
4137
cristy3ed852e2009-09-05 21:47:34 +00004138 MagickBooleanType
4139 status;
4140
cristybb503372010-05-27 20:51:26 +00004141 MagickOffsetType
4142 progress;
4143
4144 ssize_t
4145 y;
4146
cristy3ed852e2009-09-05 21:47:34 +00004147 /*
4148 Initialize sepia-toned image attributes.
4149 */
4150 assert(image != (const Image *) NULL);
4151 assert(image->signature == MagickSignature);
4152 if (image->debug != MagickFalse)
4153 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4154 assert(exception != (ExceptionInfo *) NULL);
4155 assert(exception->signature == MagickSignature);
4156 sepia_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
4157 if (sepia_image == (Image *) NULL)
4158 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004159 if (SetImageStorageClass(sepia_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004160 {
cristy3ed852e2009-09-05 21:47:34 +00004161 sepia_image=DestroyImage(sepia_image);
4162 return((Image *) NULL);
4163 }
4164 /*
4165 Tone each row of the image.
4166 */
4167 status=MagickTrue;
4168 progress=0;
4169 image_view=AcquireCacheView(image);
4170 sepia_view=AcquireCacheView(sepia_image);
cristyb5d5f722009-11-04 03:03:49 +00004171#if defined(MAGICKCORE_OPENMP_SUPPORT)
4172 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00004173#endif
cristybb503372010-05-27 20:51:26 +00004174 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004175 {
cristy4c08aed2011-07-01 19:47:50 +00004176 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004177 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00004178
cristybb503372010-05-27 20:51:26 +00004179 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004180 x;
4181
cristy4c08aed2011-07-01 19:47:50 +00004182 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004183 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004184
4185 if (status == MagickFalse)
4186 continue;
4187 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
4188 q=QueueCacheViewAuthenticPixels(sepia_view,0,y,sepia_image->columns,1,
4189 exception);
cristy4c08aed2011-07-01 19:47:50 +00004190 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004191 {
4192 status=MagickFalse;
4193 continue;
4194 }
cristybb503372010-05-27 20:51:26 +00004195 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004196 {
4197 MagickRealType
4198 intensity,
4199 tone;
4200
cristy4c08aed2011-07-01 19:47:50 +00004201 intensity=(MagickRealType) GetPixelIntensity(image,p);
cristy3ed852e2009-09-05 21:47:34 +00004202 tone=intensity > threshold ? (MagickRealType) QuantumRange : intensity+
4203 (MagickRealType) QuantumRange-threshold;
cristy4c08aed2011-07-01 19:47:50 +00004204 SetPixelRed(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004205 tone=intensity > (7.0*threshold/6.0) ? (MagickRealType) QuantumRange :
4206 intensity+(MagickRealType) QuantumRange-7.0*threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004207 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004208 tone=intensity < (threshold/6.0) ? 0 : intensity-threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004209 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004210 tone=threshold/7.0;
cristy4c08aed2011-07-01 19:47:50 +00004211 if ((MagickRealType) GetPixelGreen(image,q) < tone)
4212 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
4213 if ((MagickRealType) GetPixelBlue(image,q) < tone)
4214 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristyed231572011-07-14 02:18:59 +00004215 p+=GetPixelChannels(image);
4216 q+=GetPixelChannels(sepia_image);
cristy3ed852e2009-09-05 21:47:34 +00004217 }
4218 if (SyncCacheViewAuthenticPixels(sepia_view,exception) == MagickFalse)
4219 status=MagickFalse;
4220 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4221 {
4222 MagickBooleanType
4223 proceed;
4224
cristyb5d5f722009-11-04 03:03:49 +00004225#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00004226 #pragma omp critical (MagickCore_SepiaToneImage)
4227#endif
4228 proceed=SetImageProgress(image,SepiaToneImageTag,progress++,
4229 image->rows);
4230 if (proceed == MagickFalse)
4231 status=MagickFalse;
4232 }
4233 }
4234 sepia_view=DestroyCacheView(sepia_view);
4235 image_view=DestroyCacheView(image_view);
cristye23ec9d2011-08-16 18:15:40 +00004236 (void) NormalizeImage(sepia_image,exception);
4237 (void) ContrastImage(sepia_image,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004238 if (status == MagickFalse)
4239 sepia_image=DestroyImage(sepia_image);
4240 return(sepia_image);
4241}
4242
4243/*
4244%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4245% %
4246% %
4247% %
4248% S h a d o w I m a g e %
4249% %
4250% %
4251% %
4252%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4253%
4254% ShadowImage() simulates a shadow from the specified image and returns it.
4255%
4256% The format of the ShadowImage method is:
4257%
4258% Image *ShadowImage(const Image *image,const double opacity,
cristybb503372010-05-27 20:51:26 +00004259% const double sigma,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004260% ExceptionInfo *exception)
4261%
4262% A description of each parameter follows:
4263%
4264% o image: the image.
4265%
4266% o opacity: percentage transparency.
4267%
4268% o sigma: the standard deviation of the Gaussian, in pixels.
4269%
4270% o x_offset: the shadow x-offset.
4271%
4272% o y_offset: the shadow y-offset.
4273%
4274% o exception: return any errors or warnings in this structure.
4275%
4276*/
4277MagickExport Image *ShadowImage(const Image *image,const double opacity,
cristybb503372010-05-27 20:51:26 +00004278 const double sigma,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004279 ExceptionInfo *exception)
4280{
4281#define ShadowImageTag "Shadow/Image"
4282
cristybd5a96c2011-08-21 00:04:26 +00004283 ChannelType
4284 channel_mask;
4285
cristy3ed852e2009-09-05 21:47:34 +00004286 Image
4287 *border_image,
4288 *clone_image,
4289 *shadow_image;
4290
cristy3ed852e2009-09-05 21:47:34 +00004291 RectangleInfo
4292 border_info;
4293
cristy3ed852e2009-09-05 21:47:34 +00004294 assert(image != (Image *) NULL);
4295 assert(image->signature == MagickSignature);
4296 if (image->debug != MagickFalse)
4297 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4298 assert(exception != (ExceptionInfo *) NULL);
4299 assert(exception->signature == MagickSignature);
4300 clone_image=CloneImage(image,0,0,MagickTrue,exception);
4301 if (clone_image == (Image *) NULL)
4302 return((Image *) NULL);
4303 (void) SetImageVirtualPixelMethod(clone_image,EdgeVirtualPixelMethod);
4304 clone_image->compose=OverCompositeOp;
cristybb503372010-05-27 20:51:26 +00004305 border_info.width=(size_t) floor(2.0*sigma+0.5);
4306 border_info.height=(size_t) floor(2.0*sigma+0.5);
cristy3ed852e2009-09-05 21:47:34 +00004307 border_info.x=0;
4308 border_info.y=0;
cristy9950d572011-10-01 18:22:35 +00004309 (void) QueryColorCompliance("none",AllCompliance,&clone_image->border_color,
4310 exception);
cristy633f0c62011-09-15 13:27:36 +00004311 border_image=BorderImage(clone_image,&border_info,image->compose,exception);
cristy3ed852e2009-09-05 21:47:34 +00004312 clone_image=DestroyImage(clone_image);
4313 if (border_image == (Image *) NULL)
4314 return((Image *) NULL);
4315 if (border_image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00004316 (void) SetImageAlphaChannel(border_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004317 /*
4318 Shadow image.
4319 */
cristyea1a8aa2011-10-20 13:24:06 +00004320 (void) SetImageBackgroundColor(border_image,exception);
cristybd5a96c2011-08-21 00:04:26 +00004321 channel_mask=SetPixelChannelMask(border_image,AlphaChannel);
cristy05c0c9a2011-09-05 23:16:13 +00004322 shadow_image=BlurImage(border_image,0.0,sigma,image->bias,exception);
cristybd5a96c2011-08-21 00:04:26 +00004323 (void) SetPixelChannelMap(border_image,channel_mask);
cristy3ed852e2009-09-05 21:47:34 +00004324 border_image=DestroyImage(border_image);
4325 if (shadow_image == (Image *) NULL)
4326 return((Image *) NULL);
4327 if (shadow_image->page.width == 0)
4328 shadow_image->page.width=shadow_image->columns;
4329 if (shadow_image->page.height == 0)
4330 shadow_image->page.height=shadow_image->rows;
cristybb503372010-05-27 20:51:26 +00004331 shadow_image->page.width+=x_offset-(ssize_t) border_info.width;
4332 shadow_image->page.height+=y_offset-(ssize_t) border_info.height;
4333 shadow_image->page.x+=x_offset-(ssize_t) border_info.width;
4334 shadow_image->page.y+=y_offset-(ssize_t) border_info.height;
cristy3ed852e2009-09-05 21:47:34 +00004335 return(shadow_image);
4336}
4337
4338/*
4339%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4340% %
4341% %
4342% %
4343% S k e t c h I m a g e %
4344% %
4345% %
4346% %
4347%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4348%
4349% SketchImage() simulates a pencil sketch. We convolve the image with a
4350% Gaussian operator of the given radius and standard deviation (sigma). For
4351% reasonable results, radius should be larger than sigma. Use a radius of 0
4352% and SketchImage() selects a suitable radius for you. Angle gives the angle
4353% of the sketch.
4354%
4355% The format of the SketchImage method is:
4356%
4357% Image *SketchImage(const Image *image,const double radius,
cristyf7ef0252011-09-09 14:50:06 +00004358% const double sigma,const double angle,const double bias,
4359% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004360%
4361% A description of each parameter follows:
4362%
4363% o image: the image.
4364%
cristy574cc262011-08-05 01:23:58 +00004365% o radius: the radius of the Gaussian, in pixels, not counting the
4366% center pixel.
cristy3ed852e2009-09-05 21:47:34 +00004367%
4368% o sigma: the standard deviation of the Gaussian, in pixels.
4369%
cristyf7ef0252011-09-09 14:50:06 +00004370% o angle: apply the effect along this angle.
4371%
4372% o bias: the bias.
cristy3ed852e2009-09-05 21:47:34 +00004373%
4374% o exception: return any errors or warnings in this structure.
4375%
4376*/
4377MagickExport Image *SketchImage(const Image *image,const double radius,
cristyf7ef0252011-09-09 14:50:06 +00004378 const double sigma,const double angle,const double bias,
4379 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004380{
cristyfa112112010-01-04 17:48:07 +00004381 CacheView
4382 *random_view;
4383
cristy3ed852e2009-09-05 21:47:34 +00004384 Image
4385 *blend_image,
4386 *blur_image,
4387 *dodge_image,
4388 *random_image,
4389 *sketch_image;
4390
cristy3ed852e2009-09-05 21:47:34 +00004391 MagickBooleanType
4392 status;
4393
cristy3ed852e2009-09-05 21:47:34 +00004394 RandomInfo
cristyfa112112010-01-04 17:48:07 +00004395 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00004396
cristy9d314ff2011-03-09 01:30:28 +00004397 ssize_t
4398 y;
4399
cristy3ed852e2009-09-05 21:47:34 +00004400 /*
4401 Sketch image.
4402 */
4403 random_image=CloneImage(image,image->columns << 1,image->rows << 1,
4404 MagickTrue,exception);
4405 if (random_image == (Image *) NULL)
4406 return((Image *) NULL);
4407 status=MagickTrue;
cristy1b784432009-12-19 02:20:40 +00004408 random_info=AcquireRandomInfoThreadSet();
cristy3ed852e2009-09-05 21:47:34 +00004409 random_view=AcquireCacheView(random_image);
cristy1b784432009-12-19 02:20:40 +00004410#if defined(MAGICKCORE_OPENMP_SUPPORT)
4411 #pragma omp parallel for schedule(dynamic,4) shared(status)
4412#endif
cristybb503372010-05-27 20:51:26 +00004413 for (y=0; y < (ssize_t) random_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004414 {
cristy5c9e6f22010-09-17 17:31:01 +00004415 const int
4416 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00004417
cristybb503372010-05-27 20:51:26 +00004418 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004419 x;
4420
cristy4c08aed2011-07-01 19:47:50 +00004421 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004422 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004423
cristy1b784432009-12-19 02:20:40 +00004424 if (status == MagickFalse)
4425 continue;
cristy3ed852e2009-09-05 21:47:34 +00004426 q=QueueCacheViewAuthenticPixels(random_view,0,y,random_image->columns,1,
4427 exception);
cristyacd2ed22011-08-30 01:44:23 +00004428 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004429 {
4430 status=MagickFalse;
4431 continue;
4432 }
cristybb503372010-05-27 20:51:26 +00004433 for (x=0; x < (ssize_t) random_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004434 {
cristy76f512e2011-09-12 01:26:56 +00004435 MagickRealType
4436 value;
4437
4438 register ssize_t
4439 i;
4440
4441 value=GetPseudoRandomValue(random_info[id]);
4442 for (i=0; i < (ssize_t) GetPixelChannels(random_image); i++)
4443 {
4444 PixelTrait
4445 traits;
4446
4447 traits=GetPixelChannelMapTraits(random_image,(PixelChannel) i);
4448 if (traits == UndefinedPixelTrait)
4449 continue;
4450 q[i]=ClampToQuantum(QuantumRange*value);
4451 }
cristyed231572011-07-14 02:18:59 +00004452 q+=GetPixelChannels(random_image);
cristy3ed852e2009-09-05 21:47:34 +00004453 }
4454 if (SyncCacheViewAuthenticPixels(random_view,exception) == MagickFalse)
4455 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004456 }
4457 random_view=DestroyCacheView(random_view);
cristy1b784432009-12-19 02:20:40 +00004458 random_info=DestroyRandomInfoThreadSet(random_info);
cristy3ed852e2009-09-05 21:47:34 +00004459 if (status == MagickFalse)
4460 {
4461 random_image=DestroyImage(random_image);
4462 return(random_image);
4463 }
cristyf7ef0252011-09-09 14:50:06 +00004464 blur_image=MotionBlurImage(random_image,radius,sigma,angle,bias,exception);
cristy3ed852e2009-09-05 21:47:34 +00004465 random_image=DestroyImage(random_image);
4466 if (blur_image == (Image *) NULL)
4467 return((Image *) NULL);
cristy8ae632d2011-09-05 17:29:53 +00004468 dodge_image=EdgeImage(blur_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +00004469 blur_image=DestroyImage(blur_image);
4470 if (dodge_image == (Image *) NULL)
4471 return((Image *) NULL);
cristye23ec9d2011-08-16 18:15:40 +00004472 (void) NormalizeImage(dodge_image,exception);
cristyb3e7c6c2011-07-24 01:43:55 +00004473 (void) NegateImage(dodge_image,MagickFalse,exception);
cristye941a752011-10-15 01:52:48 +00004474 (void) TransformImage(&dodge_image,(char *) NULL,"50%",exception);
cristy3ed852e2009-09-05 21:47:34 +00004475 sketch_image=CloneImage(image,0,0,MagickTrue,exception);
4476 if (sketch_image == (Image *) NULL)
4477 {
4478 dodge_image=DestroyImage(dodge_image);
4479 return((Image *) NULL);
4480 }
cristye941a752011-10-15 01:52:48 +00004481 (void) CompositeImage(sketch_image,ColorDodgeCompositeOp,dodge_image,0,0,
4482 exception);
cristy3ed852e2009-09-05 21:47:34 +00004483 dodge_image=DestroyImage(dodge_image);
4484 blend_image=CloneImage(image,0,0,MagickTrue,exception);
4485 if (blend_image == (Image *) NULL)
4486 {
4487 sketch_image=DestroyImage(sketch_image);
4488 return((Image *) NULL);
4489 }
4490 (void) SetImageArtifact(blend_image,"compose:args","20x80");
cristye941a752011-10-15 01:52:48 +00004491 (void) CompositeImage(sketch_image,BlendCompositeOp,blend_image,0,0,
4492 exception);
cristy3ed852e2009-09-05 21:47:34 +00004493 blend_image=DestroyImage(blend_image);
4494 return(sketch_image);
4495}
4496
4497/*
4498%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4499% %
4500% %
4501% %
4502% S o l a r i z e I m a g e %
4503% %
4504% %
4505% %
4506%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4507%
4508% SolarizeImage() applies a special effect to the image, similar to the effect
4509% achieved in a photo darkroom by selectively exposing areas of photo
4510% sensitive paper to light. Threshold ranges from 0 to QuantumRange and is a
4511% measure of the extent of the solarization.
4512%
4513% The format of the SolarizeImage method is:
4514%
cristy5cbc0162011-08-29 00:36:28 +00004515% MagickBooleanType SolarizeImage(Image *image,const double threshold,
4516% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004517%
4518% A description of each parameter follows:
4519%
4520% o image: the image.
4521%
4522% o threshold: Define the extent of the solarization.
4523%
cristy5cbc0162011-08-29 00:36:28 +00004524% o exception: return any errors or warnings in this structure.
4525%
cristy3ed852e2009-09-05 21:47:34 +00004526*/
4527MagickExport MagickBooleanType SolarizeImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00004528 const double threshold,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004529{
4530#define SolarizeImageTag "Solarize/Image"
4531
cristyc4c8d132010-01-07 01:58:38 +00004532 CacheView
4533 *image_view;
4534
cristy3ed852e2009-09-05 21:47:34 +00004535 MagickBooleanType
4536 status;
4537
cristybb503372010-05-27 20:51:26 +00004538 MagickOffsetType
4539 progress;
4540
4541 ssize_t
4542 y;
4543
cristy3ed852e2009-09-05 21:47:34 +00004544 assert(image != (Image *) NULL);
4545 assert(image->signature == MagickSignature);
4546 if (image->debug != MagickFalse)
4547 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4548 if (image->storage_class == PseudoClass)
4549 {
cristybb503372010-05-27 20:51:26 +00004550 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004551 i;
4552
4553 /*
4554 Solarize colormap.
4555 */
cristybb503372010-05-27 20:51:26 +00004556 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00004557 {
4558 if ((MagickRealType) image->colormap[i].red > threshold)
4559 image->colormap[i].red=(Quantum) QuantumRange-image->colormap[i].red;
4560 if ((MagickRealType) image->colormap[i].green > threshold)
4561 image->colormap[i].green=(Quantum) QuantumRange-
4562 image->colormap[i].green;
4563 if ((MagickRealType) image->colormap[i].blue > threshold)
4564 image->colormap[i].blue=(Quantum) QuantumRange-
4565 image->colormap[i].blue;
4566 }
4567 }
4568 /*
4569 Solarize image.
4570 */
4571 status=MagickTrue;
4572 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00004573 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00004574#if defined(MAGICKCORE_OPENMP_SUPPORT)
4575 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00004576#endif
cristybb503372010-05-27 20:51:26 +00004577 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004578 {
cristybb503372010-05-27 20:51:26 +00004579 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004580 x;
4581
cristy4c08aed2011-07-01 19:47:50 +00004582 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004583 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004584
4585 if (status == MagickFalse)
4586 continue;
cristy5cbc0162011-08-29 00:36:28 +00004587 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00004588 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004589 {
4590 status=MagickFalse;
4591 continue;
4592 }
cristybb503372010-05-27 20:51:26 +00004593 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004594 {
cristy76f512e2011-09-12 01:26:56 +00004595 register ssize_t
4596 i;
4597
4598 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4599 {
4600 PixelTrait
4601 traits;
4602
4603 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
4604 if ((traits == UndefinedPixelTrait) ||
4605 ((traits & CopyPixelTrait) != 0))
4606 continue;
4607 if ((MagickRealType) q[i] > threshold)
4608 q[i]=QuantumRange-q[i];
4609 }
cristyed231572011-07-14 02:18:59 +00004610 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00004611 }
4612 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4613 status=MagickFalse;
4614 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4615 {
4616 MagickBooleanType
4617 proceed;
4618
cristyb5d5f722009-11-04 03:03:49 +00004619#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00004620 #pragma omp critical (MagickCore_SolarizeImage)
4621#endif
4622 proceed=SetImageProgress(image,SolarizeImageTag,progress++,image->rows);
4623 if (proceed == MagickFalse)
4624 status=MagickFalse;
4625 }
4626 }
4627 image_view=DestroyCacheView(image_view);
4628 return(status);
4629}
4630
4631/*
4632%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4633% %
4634% %
4635% %
4636% S t e g a n o I m a g e %
4637% %
4638% %
4639% %
4640%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4641%
4642% SteganoImage() hides a digital watermark within the image. Recover
4643% the hidden watermark later to prove that the authenticity of an image.
4644% Offset defines the start position within the image to hide the watermark.
4645%
4646% The format of the SteganoImage method is:
4647%
4648% Image *SteganoImage(const Image *image,Image *watermark,
4649% ExceptionInfo *exception)
4650%
4651% A description of each parameter follows:
4652%
4653% o image: the image.
4654%
4655% o watermark: the watermark image.
4656%
4657% o exception: return any errors or warnings in this structure.
4658%
4659*/
4660MagickExport Image *SteganoImage(const Image *image,const Image *watermark,
4661 ExceptionInfo *exception)
4662{
cristye1bf8ad2010-09-19 17:07:03 +00004663#define GetBit(alpha,i) ((((size_t) (alpha) >> (size_t) (i)) & 0x01) != 0)
cristy4c08aed2011-07-01 19:47:50 +00004664#define SetBit(alpha,i,set) (Quantum) ((set) != 0 ? (size_t) (alpha) \
cristyeaedf062010-05-29 22:36:02 +00004665 | (one << (size_t) (i)) : (size_t) (alpha) & ~(one << (size_t) (i)))
cristy3ed852e2009-09-05 21:47:34 +00004666#define SteganoImageTag "Stegano/Image"
4667
cristyb0d3bb92010-09-22 14:37:58 +00004668 CacheView
4669 *stegano_view,
4670 *watermark_view;
4671
cristy3ed852e2009-09-05 21:47:34 +00004672 Image
4673 *stegano_image;
4674
4675 int
4676 c;
4677
cristy3ed852e2009-09-05 21:47:34 +00004678 MagickBooleanType
4679 status;
4680
cristy101ab702011-10-13 13:06:32 +00004681 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00004682 pixel;
4683
cristy4c08aed2011-07-01 19:47:50 +00004684 register Quantum
cristy3ed852e2009-09-05 21:47:34 +00004685 *q;
4686
cristye1bf8ad2010-09-19 17:07:03 +00004687 register ssize_t
4688 x;
4689
cristybb503372010-05-27 20:51:26 +00004690 size_t
cristyeaedf062010-05-29 22:36:02 +00004691 depth,
4692 one;
cristy3ed852e2009-09-05 21:47:34 +00004693
cristye1bf8ad2010-09-19 17:07:03 +00004694 ssize_t
4695 i,
4696 j,
4697 k,
4698 y;
4699
cristy3ed852e2009-09-05 21:47:34 +00004700 /*
4701 Initialize steganographic image attributes.
4702 */
4703 assert(image != (const Image *) NULL);
4704 assert(image->signature == MagickSignature);
4705 if (image->debug != MagickFalse)
4706 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4707 assert(watermark != (const Image *) NULL);
4708 assert(watermark->signature == MagickSignature);
4709 assert(exception != (ExceptionInfo *) NULL);
4710 assert(exception->signature == MagickSignature);
cristyeaedf062010-05-29 22:36:02 +00004711 one=1UL;
cristy3ed852e2009-09-05 21:47:34 +00004712 stegano_image=CloneImage(image,0,0,MagickTrue,exception);
4713 if (stegano_image == (Image *) NULL)
4714 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004715 if (SetImageStorageClass(stegano_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004716 {
cristy3ed852e2009-09-05 21:47:34 +00004717 stegano_image=DestroyImage(stegano_image);
4718 return((Image *) NULL);
4719 }
4720 stegano_image->depth=MAGICKCORE_QUANTUM_DEPTH;
4721 /*
4722 Hide watermark in low-order bits of image.
4723 */
4724 c=0;
4725 i=0;
4726 j=0;
4727 depth=stegano_image->depth;
4728 k=image->offset;
cristyda16f162011-02-19 23:52:17 +00004729 status=MagickTrue;
cristyb0d3bb92010-09-22 14:37:58 +00004730 watermark_view=AcquireCacheView(watermark);
4731 stegano_view=AcquireCacheView(stegano_image);
cristybb503372010-05-27 20:51:26 +00004732 for (i=(ssize_t) depth-1; (i >= 0) && (j < (ssize_t) depth); i--)
cristy3ed852e2009-09-05 21:47:34 +00004733 {
cristybb503372010-05-27 20:51:26 +00004734 for (y=0; (y < (ssize_t) watermark->rows) && (j < (ssize_t) depth); y++)
cristy3ed852e2009-09-05 21:47:34 +00004735 {
cristybb503372010-05-27 20:51:26 +00004736 for (x=0; (x < (ssize_t) watermark->columns) && (j < (ssize_t) depth); x++)
cristy3ed852e2009-09-05 21:47:34 +00004737 {
cristyda1f9c12011-10-02 21:39:49 +00004738 Quantum
cristy5f95f4f2011-10-23 01:01:01 +00004739 virtual_pixel[CompositePixelChannel];
cristyda1f9c12011-10-02 21:39:49 +00004740
4741 (void) GetOneCacheViewVirtualPixel(watermark_view,x,y,virtual_pixel,
4742 exception);
4743 pixel.red=(double) virtual_pixel[RedPixelChannel];
4744 pixel.green=(double) virtual_pixel[GreenPixelChannel];
4745 pixel.blue=(double) virtual_pixel[BluePixelChannel];
4746 pixel.alpha=(double) virtual_pixel[AlphaPixelChannel];
cristybb503372010-05-27 20:51:26 +00004747 if ((k/(ssize_t) stegano_image->columns) >= (ssize_t) stegano_image->rows)
cristy3ed852e2009-09-05 21:47:34 +00004748 break;
cristyb0d3bb92010-09-22 14:37:58 +00004749 q=GetCacheViewAuthenticPixels(stegano_view,k % (ssize_t)
4750 stegano_image->columns,k/(ssize_t) stegano_image->columns,1,1,
4751 exception);
cristyacd2ed22011-08-30 01:44:23 +00004752 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004753 break;
4754 switch (c)
4755 {
4756 case 0:
4757 {
cristy4c08aed2011-07-01 19:47:50 +00004758 SetPixelRed(image,SetBit(GetPixelRed(image,q),j,GetBit(
cristy101ab702011-10-13 13:06:32 +00004759 GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004760 break;
4761 }
4762 case 1:
4763 {
cristy4c08aed2011-07-01 19:47:50 +00004764 SetPixelGreen(image,SetBit(GetPixelGreen(image,q),j,GetBit(
cristy101ab702011-10-13 13:06:32 +00004765 GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004766 break;
4767 }
4768 case 2:
4769 {
cristy4c08aed2011-07-01 19:47:50 +00004770 SetPixelBlue(image,SetBit(GetPixelBlue(image,q),j,GetBit(
cristy101ab702011-10-13 13:06:32 +00004771 GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004772 break;
4773 }
4774 }
cristyb0d3bb92010-09-22 14:37:58 +00004775 if (SyncCacheViewAuthenticPixels(stegano_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004776 break;
4777 c++;
4778 if (c == 3)
4779 c=0;
4780 k++;
cristybb503372010-05-27 20:51:26 +00004781 if (k == (ssize_t) (stegano_image->columns*stegano_image->columns))
cristy3ed852e2009-09-05 21:47:34 +00004782 k=0;
4783 if (k == image->offset)
4784 j++;
4785 }
4786 }
cristy8b27a6d2010-02-14 03:31:15 +00004787 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004788 {
cristy8b27a6d2010-02-14 03:31:15 +00004789 MagickBooleanType
4790 proceed;
4791
4792 proceed=SetImageProgress(image,SteganoImageTag,(MagickOffsetType)
4793 (depth-i),depth);
4794 if (proceed == MagickFalse)
4795 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004796 }
4797 }
cristyb0d3bb92010-09-22 14:37:58 +00004798 stegano_view=DestroyCacheView(stegano_view);
4799 watermark_view=DestroyCacheView(watermark_view);
cristy3ed852e2009-09-05 21:47:34 +00004800 if (stegano_image->storage_class == PseudoClass)
cristyea1a8aa2011-10-20 13:24:06 +00004801 (void) SyncImage(stegano_image,exception);
cristyda16f162011-02-19 23:52:17 +00004802 if (status == MagickFalse)
4803 {
4804 stegano_image=DestroyImage(stegano_image);
4805 return((Image *) NULL);
4806 }
cristy3ed852e2009-09-05 21:47:34 +00004807 return(stegano_image);
4808}
4809
4810/*
4811%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4812% %
4813% %
4814% %
4815% S t e r e o A n a g l y p h I m a g e %
4816% %
4817% %
4818% %
4819%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4820%
4821% StereoAnaglyphImage() combines two images and produces a single image that
4822% is the composite of a left and right image of a stereo pair. Special
4823% red-green stereo glasses are required to view this effect.
4824%
4825% The format of the StereoAnaglyphImage method is:
4826%
4827% Image *StereoImage(const Image *left_image,const Image *right_image,
4828% ExceptionInfo *exception)
4829% Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004830% const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004831% ExceptionInfo *exception)
4832%
4833% A description of each parameter follows:
4834%
4835% o left_image: the left image.
4836%
4837% o right_image: the right image.
4838%
4839% o exception: return any errors or warnings in this structure.
4840%
4841% o x_offset: amount, in pixels, by which the left image is offset to the
4842% right of the right image.
4843%
4844% o y_offset: amount, in pixels, by which the left image is offset to the
4845% bottom of the right image.
4846%
4847%
4848*/
4849MagickExport Image *StereoImage(const Image *left_image,
4850 const Image *right_image,ExceptionInfo *exception)
4851{
4852 return(StereoAnaglyphImage(left_image,right_image,0,0,exception));
4853}
4854
4855MagickExport Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004856 const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004857 ExceptionInfo *exception)
4858{
4859#define StereoImageTag "Stereo/Image"
4860
4861 const Image
4862 *image;
4863
4864 Image
4865 *stereo_image;
4866
cristy3ed852e2009-09-05 21:47:34 +00004867 MagickBooleanType
4868 status;
4869
cristy9d314ff2011-03-09 01:30:28 +00004870 ssize_t
4871 y;
4872
cristy3ed852e2009-09-05 21:47:34 +00004873 assert(left_image != (const Image *) NULL);
4874 assert(left_image->signature == MagickSignature);
4875 if (left_image->debug != MagickFalse)
4876 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4877 left_image->filename);
4878 assert(right_image != (const Image *) NULL);
4879 assert(right_image->signature == MagickSignature);
4880 assert(exception != (ExceptionInfo *) NULL);
4881 assert(exception->signature == MagickSignature);
4882 assert(right_image != (const Image *) NULL);
4883 image=left_image;
4884 if ((left_image->columns != right_image->columns) ||
4885 (left_image->rows != right_image->rows))
4886 ThrowImageException(ImageError,"LeftAndRightImageSizesDiffer");
4887 /*
4888 Initialize stereo image attributes.
4889 */
4890 stereo_image=CloneImage(left_image,left_image->columns,left_image->rows,
4891 MagickTrue,exception);
4892 if (stereo_image == (Image *) NULL)
4893 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004894 if (SetImageStorageClass(stereo_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004895 {
cristy3ed852e2009-09-05 21:47:34 +00004896 stereo_image=DestroyImage(stereo_image);
4897 return((Image *) NULL);
4898 }
4899 /*
4900 Copy left image to red channel and right image to blue channel.
4901 */
cristyda16f162011-02-19 23:52:17 +00004902 status=MagickTrue;
cristybb503372010-05-27 20:51:26 +00004903 for (y=0; y < (ssize_t) stereo_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004904 {
cristy4c08aed2011-07-01 19:47:50 +00004905 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004906 *restrict p,
4907 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004908
cristybb503372010-05-27 20:51:26 +00004909 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004910 x;
4911
cristy4c08aed2011-07-01 19:47:50 +00004912 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004913 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +00004914
4915 p=GetVirtualPixels(left_image,-x_offset,y-y_offset,image->columns,1,
4916 exception);
4917 q=GetVirtualPixels(right_image,0,y,right_image->columns,1,exception);
4918 r=QueueAuthenticPixels(stereo_image,0,y,stereo_image->columns,1,exception);
cristy76f512e2011-09-12 01:26:56 +00004919 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL) ||
4920 (r == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004921 break;
cristybb503372010-05-27 20:51:26 +00004922 for (x=0; x < (ssize_t) stereo_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004923 {
cristy4c08aed2011-07-01 19:47:50 +00004924 SetPixelRed(image,GetPixelRed(left_image,p),r);
cristy76f512e2011-09-12 01:26:56 +00004925 SetPixelGreen(image,GetPixelGreen(right_image,q),r);
4926 SetPixelBlue(image,GetPixelBlue(right_image,q),r);
4927 if ((GetPixelAlphaTraits(stereo_image) & CopyPixelTrait) != 0)
4928 SetPixelAlpha(image,(GetPixelAlpha(left_image,p)+
4929 GetPixelAlpha(right_image,q))/2,r);
cristyed231572011-07-14 02:18:59 +00004930 p+=GetPixelChannels(left_image);
cristy76f512e2011-09-12 01:26:56 +00004931 q+=GetPixelChannels(right_image);
4932 r+=GetPixelChannels(stereo_image);
cristy3ed852e2009-09-05 21:47:34 +00004933 }
4934 if (SyncAuthenticPixels(stereo_image,exception) == MagickFalse)
4935 break;
cristy8b27a6d2010-02-14 03:31:15 +00004936 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004937 {
cristy8b27a6d2010-02-14 03:31:15 +00004938 MagickBooleanType
4939 proceed;
4940
cristybb503372010-05-27 20:51:26 +00004941 proceed=SetImageProgress(image,StereoImageTag,(MagickOffsetType) y,
4942 stereo_image->rows);
cristy8b27a6d2010-02-14 03:31:15 +00004943 if (proceed == MagickFalse)
4944 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004945 }
4946 }
cristyda16f162011-02-19 23:52:17 +00004947 if (status == MagickFalse)
4948 {
4949 stereo_image=DestroyImage(stereo_image);
4950 return((Image *) NULL);
4951 }
cristy3ed852e2009-09-05 21:47:34 +00004952 return(stereo_image);
4953}
4954
4955/*
4956%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4957% %
4958% %
4959% %
4960% S w i r l I m a g e %
4961% %
4962% %
4963% %
4964%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4965%
4966% SwirlImage() swirls the pixels about the center of the image, where
4967% degrees indicates the sweep of the arc through which each pixel is moved.
4968% You get a more dramatic effect as the degrees move from 1 to 360.
4969%
4970% The format of the SwirlImage method is:
4971%
4972% Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00004973% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004974%
4975% A description of each parameter follows:
4976%
4977% o image: the image.
4978%
4979% o degrees: Define the tightness of the swirling effect.
4980%
cristy76f512e2011-09-12 01:26:56 +00004981% o method: the pixel interpolation method.
4982%
cristy3ed852e2009-09-05 21:47:34 +00004983% o exception: return any errors or warnings in this structure.
4984%
4985*/
4986MagickExport Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00004987 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004988{
4989#define SwirlImageTag "Swirl/Image"
4990
cristyfa112112010-01-04 17:48:07 +00004991 CacheView
4992 *image_view,
4993 *swirl_view;
4994
cristy3ed852e2009-09-05 21:47:34 +00004995 Image
4996 *swirl_image;
4997
cristy3ed852e2009-09-05 21:47:34 +00004998 MagickBooleanType
4999 status;
5000
cristybb503372010-05-27 20:51:26 +00005001 MagickOffsetType
5002 progress;
5003
cristy3ed852e2009-09-05 21:47:34 +00005004 MagickRealType
5005 radius;
5006
5007 PointInfo
5008 center,
5009 scale;
5010
cristybb503372010-05-27 20:51:26 +00005011 ssize_t
5012 y;
5013
cristy3ed852e2009-09-05 21:47:34 +00005014 /*
5015 Initialize swirl image attributes.
5016 */
5017 assert(image != (const Image *) NULL);
5018 assert(image->signature == MagickSignature);
5019 if (image->debug != MagickFalse)
5020 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5021 assert(exception != (ExceptionInfo *) NULL);
5022 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00005023 swirl_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005024 if (swirl_image == (Image *) NULL)
5025 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005026 if (SetImageStorageClass(swirl_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005027 {
cristy3ed852e2009-09-05 21:47:34 +00005028 swirl_image=DestroyImage(swirl_image);
5029 return((Image *) NULL);
5030 }
cristy4c08aed2011-07-01 19:47:50 +00005031 if (swirl_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00005032 swirl_image->matte=MagickTrue;
5033 /*
5034 Compute scaling factor.
5035 */
5036 center.x=(double) image->columns/2.0;
5037 center.y=(double) image->rows/2.0;
5038 radius=MagickMax(center.x,center.y);
5039 scale.x=1.0;
5040 scale.y=1.0;
5041 if (image->columns > image->rows)
5042 scale.y=(double) image->columns/(double) image->rows;
5043 else
5044 if (image->columns < image->rows)
5045 scale.x=(double) image->rows/(double) image->columns;
5046 degrees=(double) DegreesToRadians(degrees);
5047 /*
5048 Swirl image.
5049 */
5050 status=MagickTrue;
5051 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00005052 image_view=AcquireCacheView(image);
5053 swirl_view=AcquireCacheView(swirl_image);
cristyb5d5f722009-11-04 03:03:49 +00005054#if defined(MAGICKCORE_OPENMP_SUPPORT)
5055 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00005056#endif
cristybb503372010-05-27 20:51:26 +00005057 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005058 {
cristy3ed852e2009-09-05 21:47:34 +00005059 MagickRealType
5060 distance;
5061
5062 PointInfo
5063 delta;
5064
cristy6d188022011-09-12 13:23:33 +00005065 register const Quantum
5066 *restrict p;
5067
cristybb503372010-05-27 20:51:26 +00005068 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005069 x;
5070
cristy4c08aed2011-07-01 19:47:50 +00005071 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005072 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005073
5074 if (status == MagickFalse)
5075 continue;
cristy6d188022011-09-12 13:23:33 +00005076 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy3ed852e2009-09-05 21:47:34 +00005077 q=GetCacheViewAuthenticPixels(swirl_view,0,y,swirl_image->columns,1,
5078 exception);
cristy6d188022011-09-12 13:23:33 +00005079 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005080 {
5081 status=MagickFalse;
5082 continue;
5083 }
cristy3ed852e2009-09-05 21:47:34 +00005084 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00005085 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005086 {
cristy6d188022011-09-12 13:23:33 +00005087 register ssize_t
5088 i;
5089
cristy3ed852e2009-09-05 21:47:34 +00005090 /*
5091 Determine if the pixel is within an ellipse.
5092 */
5093 delta.x=scale.x*(double) (x-center.x);
5094 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00005095 if (distance >= (radius*radius))
5096 {
5097 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5098 q[i]=p[i];
5099 }
5100 else
cristy3ed852e2009-09-05 21:47:34 +00005101 {
5102 MagickRealType
5103 cosine,
5104 factor,
5105 sine;
5106
5107 /*
5108 Swirl the pixel.
5109 */
5110 factor=1.0-sqrt((double) distance)/radius;
5111 sine=sin((double) (degrees*factor*factor));
5112 cosine=cos((double) (degrees*factor*factor));
cristy76f512e2011-09-12 01:26:56 +00005113 status=InterpolatePixelChannels(image,image_view,swirl_image,method,
5114 ((cosine*delta.x-sine*delta.y)/scale.x+center.x),(double)
5115 ((sine*delta.x+cosine*delta.y)/scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00005116 }
cristy6d188022011-09-12 13:23:33 +00005117 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00005118 q+=GetPixelChannels(swirl_image);
cristy3ed852e2009-09-05 21:47:34 +00005119 }
5120 if (SyncCacheViewAuthenticPixels(swirl_view,exception) == MagickFalse)
5121 status=MagickFalse;
5122 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5123 {
5124 MagickBooleanType
5125 proceed;
5126
cristyb5d5f722009-11-04 03:03:49 +00005127#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00005128 #pragma omp critical (MagickCore_SwirlImage)
5129#endif
5130 proceed=SetImageProgress(image,SwirlImageTag,progress++,image->rows);
5131 if (proceed == MagickFalse)
5132 status=MagickFalse;
5133 }
5134 }
5135 swirl_view=DestroyCacheView(swirl_view);
5136 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00005137 if (status == MagickFalse)
5138 swirl_image=DestroyImage(swirl_image);
5139 return(swirl_image);
5140}
5141
5142/*
5143%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5144% %
5145% %
5146% %
5147% T i n t I m a g e %
5148% %
5149% %
5150% %
5151%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5152%
5153% TintImage() applies a color vector to each pixel in the image. The length
5154% of the vector is 0 for black and white and at its maximum for the midtones.
5155% The vector weighting function is f(x)=(1-(4.0*((x-0.5)*(x-0.5))))
5156%
5157% The format of the TintImage method is:
5158%
cristyb817c3f2011-10-03 14:00:35 +00005159% Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005160% const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005161%
5162% A description of each parameter follows:
5163%
5164% o image: the image.
5165%
cristyb817c3f2011-10-03 14:00:35 +00005166% o blend: A color value used for tinting.
cristy3ed852e2009-09-05 21:47:34 +00005167%
5168% o tint: A color value used for tinting.
5169%
5170% o exception: return any errors or warnings in this structure.
5171%
5172*/
cristyb817c3f2011-10-03 14:00:35 +00005173MagickExport Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005174 const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005175{
5176#define TintImageTag "Tint/Image"
5177
cristyc4c8d132010-01-07 01:58:38 +00005178 CacheView
5179 *image_view,
5180 *tint_view;
5181
cristy3ed852e2009-09-05 21:47:34 +00005182 GeometryInfo
5183 geometry_info;
5184
5185 Image
5186 *tint_image;
5187
cristy3ed852e2009-09-05 21:47:34 +00005188 MagickBooleanType
5189 status;
5190
cristybb503372010-05-27 20:51:26 +00005191 MagickOffsetType
5192 progress;
cristy3ed852e2009-09-05 21:47:34 +00005193
cristy28474bf2011-09-11 23:32:52 +00005194 MagickRealType
5195 intensity;
5196
cristy4c08aed2011-07-01 19:47:50 +00005197 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00005198 color_vector,
5199 pixel;
5200
cristybb503372010-05-27 20:51:26 +00005201 MagickStatusType
5202 flags;
5203
5204 ssize_t
5205 y;
5206
cristy3ed852e2009-09-05 21:47:34 +00005207 /*
5208 Allocate tint image.
5209 */
5210 assert(image != (const Image *) NULL);
5211 assert(image->signature == MagickSignature);
5212 if (image->debug != MagickFalse)
5213 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5214 assert(exception != (ExceptionInfo *) NULL);
5215 assert(exception->signature == MagickSignature);
5216 tint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5217 if (tint_image == (Image *) NULL)
5218 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005219 if (SetImageStorageClass(tint_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005220 {
cristy3ed852e2009-09-05 21:47:34 +00005221 tint_image=DestroyImage(tint_image);
5222 return((Image *) NULL);
5223 }
cristyaed9c382011-10-03 17:54:21 +00005224 if (blend == (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005225 return(tint_image);
5226 /*
5227 Determine RGB values of the color.
5228 */
cristy28474bf2011-09-11 23:32:52 +00005229 GetPixelInfo(image,&pixel);
cristyb817c3f2011-10-03 14:00:35 +00005230 flags=ParseGeometry(blend,&geometry_info);
cristy3ed852e2009-09-05 21:47:34 +00005231 pixel.red=geometry_info.rho;
cristy76f512e2011-09-12 01:26:56 +00005232 pixel.green=geometry_info.rho;
5233 pixel.blue=geometry_info.rho;
cristy76f512e2011-09-12 01:26:56 +00005234 pixel.alpha=OpaqueAlpha;
cristy3ed852e2009-09-05 21:47:34 +00005235 if ((flags & SigmaValue) != 0)
5236 pixel.green=geometry_info.sigma;
cristy3ed852e2009-09-05 21:47:34 +00005237 if ((flags & XiValue) != 0)
5238 pixel.blue=geometry_info.xi;
cristyb817c3f2011-10-03 14:00:35 +00005239 if ((flags & PsiValue) != 0)
5240 pixel.alpha=geometry_info.psi;
cristy76f512e2011-09-12 01:26:56 +00005241 if (image->colorspace == CMYKColorspace)
5242 {
cristyb817c3f2011-10-03 14:00:35 +00005243 pixel.black=geometry_info.rho;
cristy76f512e2011-09-12 01:26:56 +00005244 if ((flags & PsiValue) != 0)
5245 pixel.black=geometry_info.psi;
5246 if ((flags & ChiValue) != 0)
5247 pixel.alpha=geometry_info.chi;
5248 }
cristy28474bf2011-09-11 23:32:52 +00005249 intensity=(MagickRealType) GetPixelInfoIntensity(tint);
5250 color_vector.red=(MagickRealType) (pixel.red*tint->red/100.0-intensity);
5251 color_vector.green=(MagickRealType) (pixel.green*tint->green/100.0-intensity);
5252 color_vector.blue=(MagickRealType) (pixel.blue*tint->blue/100.0-intensity);
5253 color_vector.black=(MagickRealType) (pixel.black*tint->black/100.0-intensity);
5254 color_vector.alpha=(MagickRealType) (pixel.alpha*tint->alpha/100.0-intensity);
cristy3ed852e2009-09-05 21:47:34 +00005255 /*
5256 Tint image.
5257 */
5258 status=MagickTrue;
5259 progress=0;
5260 image_view=AcquireCacheView(image);
5261 tint_view=AcquireCacheView(tint_image);
cristyb5d5f722009-11-04 03:03:49 +00005262#if defined(MAGICKCORE_OPENMP_SUPPORT)
5263 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00005264#endif
cristybb503372010-05-27 20:51:26 +00005265 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005266 {
cristy4c08aed2011-07-01 19:47:50 +00005267 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00005268 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00005269
cristy4c08aed2011-07-01 19:47:50 +00005270 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005271 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005272
cristy6b91acb2011-04-19 12:23:54 +00005273 register ssize_t
5274 x;
5275
cristy3ed852e2009-09-05 21:47:34 +00005276 if (status == MagickFalse)
5277 continue;
5278 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5279 q=QueueCacheViewAuthenticPixels(tint_view,0,y,tint_image->columns,1,
5280 exception);
cristy4c08aed2011-07-01 19:47:50 +00005281 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005282 {
5283 status=MagickFalse;
5284 continue;
5285 }
cristybb503372010-05-27 20:51:26 +00005286 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005287 {
cristy4c08aed2011-07-01 19:47:50 +00005288 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00005289 pixel;
5290
5291 MagickRealType
5292 weight;
5293
cristy76f512e2011-09-12 01:26:56 +00005294 if ((GetPixelRedTraits(tint_image) & UpdatePixelTrait) != 0)
cristy28474bf2011-09-11 23:32:52 +00005295 {
5296 weight=QuantumScale*GetPixelRed(image,p)-0.5;
5297 pixel.red=(MagickRealType) GetPixelRed(image,p)+
5298 color_vector.red*(1.0-(4.0*(weight*weight)));
5299 SetPixelRed(tint_image,ClampToQuantum(pixel.red),q);
5300 }
cristy76f512e2011-09-12 01:26:56 +00005301 if ((GetPixelGreenTraits(tint_image) & UpdatePixelTrait) != 0)
cristy28474bf2011-09-11 23:32:52 +00005302 {
5303 weight=QuantumScale*GetPixelGreen(image,p)-0.5;
5304 pixel.green=(MagickRealType) GetPixelGreen(image,p)+
5305 color_vector.green*(1.0-(4.0*(weight*weight)));
5306 SetPixelGreen(tint_image,ClampToQuantum(pixel.green),q);
5307 }
cristy76f512e2011-09-12 01:26:56 +00005308 if ((GetPixelBlueTraits(tint_image) & UpdatePixelTrait) != 0)
cristy28474bf2011-09-11 23:32:52 +00005309 {
5310 weight=QuantumScale*GetPixelBlue(image,p)-0.5;
5311 pixel.blue=(MagickRealType) GetPixelBlue(image,p)+
5312 color_vector.blue*(1.0-(4.0*(weight*weight)));
5313 SetPixelBlue(tint_image,ClampToQuantum(pixel.blue),q);
5314 }
cristy76f512e2011-09-12 01:26:56 +00005315 if ((GetPixelBlackTraits(tint_image) & UpdatePixelTrait) != 0)
cristy28474bf2011-09-11 23:32:52 +00005316 {
5317 weight=QuantumScale*GetPixelBlack(image,p)-0.5;
5318 pixel.black=(MagickRealType) GetPixelBlack(image,p)+
5319 color_vector.black*(1.0-(4.0*(weight*weight)));
5320 SetPixelBlack(tint_image,ClampToQuantum(pixel.black),q);
5321 }
cristy76f512e2011-09-12 01:26:56 +00005322 if ((GetPixelAlphaTraits(tint_image) & CopyPixelTrait) != 0)
cristy28474bf2011-09-11 23:32:52 +00005323 SetPixelAlpha(tint_image,GetPixelAlpha(image,p),q);
cristyed231572011-07-14 02:18:59 +00005324 p+=GetPixelChannels(image);
5325 q+=GetPixelChannels(tint_image);
cristy3ed852e2009-09-05 21:47:34 +00005326 }
5327 if (SyncCacheViewAuthenticPixels(tint_view,exception) == MagickFalse)
5328 status=MagickFalse;
5329 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5330 {
5331 MagickBooleanType
5332 proceed;
5333
cristyb5d5f722009-11-04 03:03:49 +00005334#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00005335 #pragma omp critical (MagickCore_TintImage)
5336#endif
5337 proceed=SetImageProgress(image,TintImageTag,progress++,image->rows);
5338 if (proceed == MagickFalse)
5339 status=MagickFalse;
5340 }
5341 }
5342 tint_view=DestroyCacheView(tint_view);
5343 image_view=DestroyCacheView(image_view);
5344 if (status == MagickFalse)
5345 tint_image=DestroyImage(tint_image);
5346 return(tint_image);
5347}
5348
5349/*
5350%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5351% %
5352% %
5353% %
5354% V i g n e t t e I m a g e %
5355% %
5356% %
5357% %
5358%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5359%
5360% VignetteImage() softens the edges of the image in vignette style.
5361%
5362% The format of the VignetteImage method is:
5363%
5364% Image *VignetteImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +00005365% const double sigma,const ssize_t x,const ssize_t y,
5366% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005367%
5368% A description of each parameter follows:
5369%
5370% o image: the image.
5371%
5372% o radius: the radius of the pixel neighborhood.
5373%
5374% o sigma: the standard deviation of the Gaussian, in pixels.
5375%
5376% o x, y: Define the x and y ellipse offset.
5377%
5378% o exception: return any errors or warnings in this structure.
5379%
5380*/
5381MagickExport Image *VignetteImage(const Image *image,const double radius,
cristybb503372010-05-27 20:51:26 +00005382 const double sigma,const ssize_t x,const ssize_t y,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005383{
5384 char
5385 ellipse[MaxTextExtent];
5386
5387 DrawInfo
5388 *draw_info;
5389
5390 Image
5391 *canvas_image,
5392 *blur_image,
5393 *oval_image,
5394 *vignette_image;
5395
5396 assert(image != (Image *) NULL);
5397 assert(image->signature == MagickSignature);
5398 if (image->debug != MagickFalse)
5399 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5400 assert(exception != (ExceptionInfo *) NULL);
5401 assert(exception->signature == MagickSignature);
5402 canvas_image=CloneImage(image,0,0,MagickTrue,exception);
5403 if (canvas_image == (Image *) NULL)
5404 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005405 if (SetImageStorageClass(canvas_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005406 {
cristy3ed852e2009-09-05 21:47:34 +00005407 canvas_image=DestroyImage(canvas_image);
5408 return((Image *) NULL);
5409 }
5410 canvas_image->matte=MagickTrue;
cristyb3a73b52011-07-26 01:34:43 +00005411 oval_image=CloneImage(canvas_image,canvas_image->columns,
5412 canvas_image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005413 if (oval_image == (Image *) NULL)
5414 {
5415 canvas_image=DestroyImage(canvas_image);
5416 return((Image *) NULL);
5417 }
cristy9950d572011-10-01 18:22:35 +00005418 (void) QueryColorCompliance("#000000",AllCompliance,
5419 &oval_image->background_color,exception);
cristyea1a8aa2011-10-20 13:24:06 +00005420 (void) SetImageBackgroundColor(oval_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00005421 draw_info=CloneDrawInfo((const ImageInfo *) NULL,(const DrawInfo *) NULL);
cristy9950d572011-10-01 18:22:35 +00005422 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->fill,
5423 exception);
5424 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->stroke,
5425 exception);
cristyb51dff52011-05-19 16:55:47 +00005426 (void) FormatLocaleString(ellipse,MaxTextExtent,
cristye7f51092010-01-17 00:39:37 +00005427 "ellipse %g,%g,%g,%g,0.0,360.0",image->columns/2.0,
cristy8cd5b312010-01-07 01:10:24 +00005428 image->rows/2.0,image->columns/2.0-x,image->rows/2.0-y);
cristy3ed852e2009-09-05 21:47:34 +00005429 draw_info->primitive=AcquireString(ellipse);
cristy018f07f2011-09-04 21:15:19 +00005430 (void) DrawImage(oval_image,draw_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00005431 draw_info=DestroyDrawInfo(draw_info);
cristy05c0c9a2011-09-05 23:16:13 +00005432 blur_image=BlurImage(oval_image,radius,sigma,image->bias,exception);
cristy3ed852e2009-09-05 21:47:34 +00005433 oval_image=DestroyImage(oval_image);
5434 if (blur_image == (Image *) NULL)
5435 {
5436 canvas_image=DestroyImage(canvas_image);
5437 return((Image *) NULL);
5438 }
5439 blur_image->matte=MagickFalse;
cristye941a752011-10-15 01:52:48 +00005440 (void) CompositeImage(canvas_image,CopyOpacityCompositeOp,blur_image,0,0,
5441 exception);
cristy3ed852e2009-09-05 21:47:34 +00005442 blur_image=DestroyImage(blur_image);
5443 vignette_image=MergeImageLayers(canvas_image,FlattenLayer,exception);
5444 canvas_image=DestroyImage(canvas_image);
5445 return(vignette_image);
5446}
5447
5448/*
5449%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5450% %
5451% %
5452% %
5453% W a v e I m a g e %
5454% %
5455% %
5456% %
5457%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5458%
5459% WaveImage() creates a "ripple" effect in the image by shifting the pixels
cristycee97112010-05-28 00:44:52 +00005460% vertically along a sine wave whose amplitude and wavelength is specified
cristy3ed852e2009-09-05 21:47:34 +00005461% by the given parameters.
5462%
5463% The format of the WaveImage method is:
5464%
5465% Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005466% const double wave_length,const PixelInterpolateMethod method,
5467% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005468%
5469% A description of each parameter follows:
5470%
5471% o image: the image.
5472%
5473% o amplitude, wave_length: Define the amplitude and wave length of the
5474% sine wave.
5475%
cristy5c4e2582011-09-11 19:21:03 +00005476% o interpolate: the pixel interpolation method.
5477%
cristy3ed852e2009-09-05 21:47:34 +00005478% o exception: return any errors or warnings in this structure.
5479%
5480*/
5481MagickExport Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005482 const double wave_length,const PixelInterpolateMethod method,
5483 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005484{
5485#define WaveImageTag "Wave/Image"
5486
cristyfa112112010-01-04 17:48:07 +00005487 CacheView
cristyd76c51e2011-03-26 00:21:26 +00005488 *image_view,
cristyfa112112010-01-04 17:48:07 +00005489 *wave_view;
5490
cristy3ed852e2009-09-05 21:47:34 +00005491 Image
5492 *wave_image;
5493
cristy3ed852e2009-09-05 21:47:34 +00005494 MagickBooleanType
5495 status;
5496
cristybb503372010-05-27 20:51:26 +00005497 MagickOffsetType
5498 progress;
5499
cristy3ed852e2009-09-05 21:47:34 +00005500 MagickRealType
5501 *sine_map;
5502
cristybb503372010-05-27 20:51:26 +00005503 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005504 i;
5505
cristybb503372010-05-27 20:51:26 +00005506 ssize_t
5507 y;
5508
cristy3ed852e2009-09-05 21:47:34 +00005509 /*
5510 Initialize wave image attributes.
5511 */
5512 assert(image != (Image *) NULL);
5513 assert(image->signature == MagickSignature);
5514 if (image->debug != MagickFalse)
5515 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5516 assert(exception != (ExceptionInfo *) NULL);
5517 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00005518 wave_image=CloneImage(image,image->columns,(size_t) (image->rows+2.0*
cristy3ed852e2009-09-05 21:47:34 +00005519 fabs(amplitude)),MagickTrue,exception);
5520 if (wave_image == (Image *) NULL)
5521 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005522 if (SetImageStorageClass(wave_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005523 {
cristy3ed852e2009-09-05 21:47:34 +00005524 wave_image=DestroyImage(wave_image);
5525 return((Image *) NULL);
5526 }
cristy4c08aed2011-07-01 19:47:50 +00005527 if (wave_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00005528 wave_image->matte=MagickTrue;
5529 /*
5530 Allocate sine map.
5531 */
5532 sine_map=(MagickRealType *) AcquireQuantumMemory((size_t) wave_image->columns,
5533 sizeof(*sine_map));
5534 if (sine_map == (MagickRealType *) NULL)
5535 {
5536 wave_image=DestroyImage(wave_image);
5537 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
5538 }
cristybb503372010-05-27 20:51:26 +00005539 for (i=0; i < (ssize_t) wave_image->columns; i++)
cristy4205a3c2010-09-12 20:19:59 +00005540 sine_map[i]=fabs(amplitude)+amplitude*sin((double) ((2.0*MagickPI*i)/
5541 wave_length));
cristy3ed852e2009-09-05 21:47:34 +00005542 /*
5543 Wave image.
5544 */
5545 status=MagickTrue;
5546 progress=0;
cristyd76c51e2011-03-26 00:21:26 +00005547 image_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00005548 wave_view=AcquireCacheView(wave_image);
cristyd76c51e2011-03-26 00:21:26 +00005549 (void) SetCacheViewVirtualPixelMethod(image_view,
5550 BackgroundVirtualPixelMethod);
cristyb5d5f722009-11-04 03:03:49 +00005551#if defined(MAGICKCORE_OPENMP_SUPPORT)
5552 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00005553#endif
cristybb503372010-05-27 20:51:26 +00005554 for (y=0; y < (ssize_t) wave_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005555 {
cristy4c08aed2011-07-01 19:47:50 +00005556 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005557 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005558
cristye97bb922011-04-03 01:36:52 +00005559 register ssize_t
5560 x;
5561
cristy3ed852e2009-09-05 21:47:34 +00005562 if (status == MagickFalse)
5563 continue;
5564 q=QueueCacheViewAuthenticPixels(wave_view,0,y,wave_image->columns,1,
5565 exception);
cristyacd2ed22011-08-30 01:44:23 +00005566 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005567 {
5568 status=MagickFalse;
5569 continue;
5570 }
cristybb503372010-05-27 20:51:26 +00005571 for (x=0; x < (ssize_t) wave_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005572 {
cristy5c4e2582011-09-11 19:21:03 +00005573 status=InterpolatePixelChannels(image,image_view,wave_image,method,
5574 (double) x,(double) (y-sine_map[x]),q,exception);
cristyed231572011-07-14 02:18:59 +00005575 q+=GetPixelChannels(wave_image);
cristy3ed852e2009-09-05 21:47:34 +00005576 }
5577 if (SyncCacheViewAuthenticPixels(wave_view,exception) == MagickFalse)
5578 status=MagickFalse;
5579 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5580 {
5581 MagickBooleanType
5582 proceed;
5583
cristyb5d5f722009-11-04 03:03:49 +00005584#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00005585 #pragma omp critical (MagickCore_WaveImage)
5586#endif
5587 proceed=SetImageProgress(image,WaveImageTag,progress++,image->rows);
5588 if (proceed == MagickFalse)
5589 status=MagickFalse;
5590 }
5591 }
5592 wave_view=DestroyCacheView(wave_view);
cristyd76c51e2011-03-26 00:21:26 +00005593 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00005594 sine_map=(MagickRealType *) RelinquishMagickMemory(sine_map);
5595 if (status == MagickFalse)
5596 wave_image=DestroyImage(wave_image);
5597 return(wave_image);
5598}