MagickCore 7.1.2-37
Convert, Edit, Or Compose Bitmap Images
Loading...
Searching...
No Matches
fx.c
1/*
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% snibgo (Alan Gibson) %
17% January 2022 %
18% %
19% %
20% %
21% Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization %
22% dedicated to making software imaging solutions freely available. %
23% %
24% You may not use this file except in compliance with the License. You may %
25% obtain a copy of the License at %
26% %
27% https://imagemagick.org/license/ %
28% %
29% Unless required by applicable law or agreed to in writing, software %
30% distributed under the License is distributed on an "AS IS" BASIS, %
31% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
32% See the License for the specific language governing permissions and %
33% limitations under the License. %
34% %
35%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36%
37%
38%
39*/
40␌
41/*
42 Include declarations.
43*/
44#include "MagickCore/studio.h"
45#include "MagickCore/accelerate-private.h"
46#include "MagickCore/annotate.h"
47#include "MagickCore/artifact.h"
48#include "MagickCore/attribute.h"
49#include "MagickCore/cache.h"
50#include "MagickCore/cache-view.h"
51#include "MagickCore/channel.h"
52#include "MagickCore/color.h"
53#include "MagickCore/color-private.h"
54#include "MagickCore/colorspace-private.h"
55#include "MagickCore/composite.h"
56#include "MagickCore/decorate.h"
57#include "MagickCore/distort.h"
58#include "MagickCore/draw.h"
59#include "MagickCore/effect.h"
60#include "MagickCore/enhance.h"
61#include "MagickCore/exception.h"
62#include "MagickCore/exception-private.h"
63#include "MagickCore/fx.h"
64#include "MagickCore/fx-private.h"
65#include "MagickCore/gem.h"
66#include "MagickCore/gem-private.h"
67#include "MagickCore/geometry.h"
68#include "MagickCore/layer.h"
69#include "MagickCore/list.h"
70#include "MagickCore/log.h"
71#include "MagickCore/image.h"
72#include "MagickCore/image-private.h"
73#include "MagickCore/magick.h"
74#include "MagickCore/memory_.h"
75#include "MagickCore/memory-private.h"
76#include "MagickCore/monitor.h"
77#include "MagickCore/monitor-private.h"
78#include "MagickCore/option.h"
79#include "MagickCore/pixel.h"
80#include "MagickCore/pixel-accessor.h"
81#include "MagickCore/policy.h"
82#include "MagickCore/property.h"
83#include "MagickCore/quantum.h"
84#include "MagickCore/quantum-private.h"
85#include "MagickCore/random_.h"
86#include "MagickCore/random-private.h"
87#include "MagickCore/resample.h"
88#include "MagickCore/resample-private.h"
89#include "MagickCore/resize.h"
90#include "MagickCore/resource_.h"
91#include "MagickCore/splay-tree.h"
92#include "MagickCore/statistic.h"
93#include "MagickCore/string_.h"
94#include "MagickCore/string-private.h"
95#include "MagickCore/thread-private.h"
96#include "MagickCore/threshold.h"
97#include "MagickCore/timer-private.h"
98#include "MagickCore/token.h"
99#include "MagickCore/transform.h"
100#include "MagickCore/transform-private.h"
101#include "MagickCore/utility.h"
102
103
104#define MaxTokenLen 100
105#define RpnInit 100
106#define TableExtend 0.1
107#define InitNumOprStack 50
108#define MinValStackSize 100
109#define InitNumUserSymbols 50
110
111#if defined(MAGICKCORE_WINDOWS_SUPPORT)
112#define __j0 _j0
113#define __j1 _j1
114#else
115#define __j0 j0
116#define __j1 j1
117#endif
118
119#define SECONDS_ERR -FLT_MAX
120
121typedef long double fxFltType;
122
123typedef enum {
124 oAddEq,
125 oSubtractEq,
126 oMultiplyEq,
127 oDivideEq,
128 oPlusPlus,
129 oSubSub,
130 oAdd,
131 oSubtract,
132 oMultiply,
133 oDivide,
134 oModulus,
135 oUnaryPlus,
136 oUnaryMinus,
137 oLshift,
138 oRshift,
139 oEq,
140 oNotEq,
141 oLtEq,
142 oGtEq,
143 oLt,
144 oGt,
145 oLogAnd,
146 oLogOr,
147 oLogNot,
148 oBitAnd,
149 oBitOr,
150 oBitNot,
151 oPow,
152 oQuery,
153 oColon,
154 oOpenParen,
155 oCloseParen,
156 oOpenBracket,
157 oCloseBracket,
158 oOpenBrace,
159 oCloseBrace,
160 oAssign,
161 oNull
162} OperatorE;
163
164typedef struct {
165 OperatorE
166 op;
167
168 const char *
169 str;
170
171 int
172 precedence, /* Higher number is higher precedence */
173 number_args;
174} OperatorT;
175
176static const OperatorT Operators[] = {
177 {oAddEq, "+=", 12, 1},
178 {oSubtractEq, "-=", 12, 1},
179 {oMultiplyEq, "*=", 13, 1},
180 {oDivideEq, "/=", 13, 1},
181 {oPlusPlus, "++", 12, 0},
182 {oSubSub, "--", 12, 0},
183 {oAdd, "+", 12, 2},
184 {oSubtract, "-", 12, 2},
185 {oMultiply, "*", 13, 2},
186 {oDivide, "/", 13, 2},
187 {oModulus, "%", 13, 2},
188 {oUnaryPlus, "+", 14, 1},
189 {oUnaryMinus, "-", 14, 1},
190 {oLshift, "<<", 11, 2},
191 {oRshift, ">>", 11, 2},
192 {oEq, "==", 9, 2},
193 {oNotEq, "!=", 9, 2},
194 {oLtEq, "<=", 10, 2},
195 {oGtEq, ">=", 10, 2},
196 {oLt, "<", 10, 2},
197 {oGt, ">", 10, 2},
198 {oLogAnd, "&&", 6, 2},
199 {oLogOr, "||", 5, 2},
200 {oLogNot, "!", 16, 1},
201 {oBitAnd, "&", 8, 2},
202 {oBitOr, "|", 7, 2},
203 {oBitNot, "~", 16, 1},
204 {oPow, "^", 15, 2},
205 {oQuery, "?", 4, 1},
206 {oColon, ":", 4, 1},
207 {oOpenParen, "(", 0, 0},
208 {oCloseParen, ")", 0, 0},
209 {oOpenBracket, "[", 0, 0},
210 {oCloseBracket,"]", 0, 0},
211 {oOpenBrace, "{", 0, 0},
212 {oCloseBrace, "}", 0, 0},
213 {oAssign, "=", 3, 1},
214 {oNull, "onull", 17, 0}
215};
216
217typedef enum {
218 cEpsilon,
219 cE,
220 cOpaque,
221 cPhi,
222 cPi,
223 cQuantumRange,
224 cQuantumScale,
225 cTransparent,
226 cMaxRgb,
227 cNull
228} ConstantE;
229
230typedef struct {
231 ConstantE
232 cons;
233
234 fxFltType
235 val;
236
237 const char
238 *str;
239} ConstantT;
240
241static const ConstantT Constants[] = {
242 {cEpsilon, MagickEpsilon, "epsilon"},
243 {cE, 2.7182818284590452354, "e"},
244 {cOpaque, 1.0, "opaque"},
245 {cPhi, MagickPHI, "phi"},
246 {cPi, MagickPI, "pi"},
247 {cQuantumRange, QuantumRange, "quantumrange"},
248 {cQuantumScale, QuantumScale, "quantumscale"},
249 {cTransparent, 0.0, "transparent"},
250 {cMaxRgb, QuantumRange, "MaxRGB"},
251 {cNull, 0.0, "cnull"}
252};
253
254#define FirstFunc ((FunctionE) (oNull+1))
255
256typedef enum {
257 fAbs = oNull+1,
258#if defined(MAGICKCORE_HAVE_ACOSH)
259 fAcosh,
260#endif
261 fAcos,
262#if defined(MAGICKCORE_HAVE_J1)
263 fAiry,
264#endif
265 fAlt,
266#if defined(MAGICKCORE_HAVE_ASINH)
267 fAsinh,
268#endif
269 fAsin,
270#if defined(MAGICKCORE_HAVE_ATANH)
271 fAtanh,
272#endif
273 fAtan2,
274 fAtan,
275 fCeil,
276 fChannel,
277 fClamp,
278 fCosh,
279 fCos,
280 fDebug,
281 fDrc,
282#if defined(MAGICKCORE_HAVE_ERF)
283 fErf,
284#endif
285 fEpoch,
286 fExp,
287 fFloor,
288 fGauss,
289 fGcd,
290 fHypot,
291 fInt,
292 fIsnan,
293#if defined(MAGICKCORE_HAVE_J0)
294 fJ0,
295#endif
296#if defined(MAGICKCORE_HAVE_J1)
297 fJ1,
298#endif
299#if defined(MAGICKCORE_HAVE_J1)
300 fJinc,
301#endif
302 fLn,
303 fLogtwo,
304 fLog,
305 fMagickTime,
306 fMax,
307 fMin,
308 fMod,
309 fNot,
310 fPow,
311 fRand,
312 fRound,
313 fSign,
314 fSinc,
315 fSinh,
316 fSin,
317 fSqrt,
318 fSquish,
319 fTanh,
320 fTan,
321 fTrunc,
322 fDo,
323 fFor,
324 fIf,
325 fWhile,
326 fU,
327 fU0,
328 fUP,
329 fS,
330 fV,
331 fP,
332 fSP,
333 fVP,
334
335 fNull
336} FunctionE;
337
338typedef struct {
339 FunctionE
340 func;
341
342 const char
343 *str;
344
345 int
346 number_args;
347} FunctionT;
348
349static const FunctionT Functions[] = {
350 {fAbs, "abs" , 1},
351#if defined(MAGICKCORE_HAVE_ACOSH)
352 {fAcosh, "acosh" , 1},
353#endif
354 {fAcos, "acos" , 1},
355#if defined(MAGICKCORE_HAVE_J1)
356 {fAiry, "airy" , 1},
357#endif
358 {fAlt, "alt" , 1},
359#if defined(MAGICKCORE_HAVE_ASINH)
360 {fAsinh, "asinh" , 1},
361#endif
362 {fAsin, "asin" , 1},
363#if defined(MAGICKCORE_HAVE_ATANH)
364 {fAtanh, "atanh" , 1},
365#endif
366 {fAtan2, "atan2" , 2},
367 {fAtan, "atan" , 1},
368 {fCeil, "ceil" , 1},
369 {fChannel, "channel", 5}, /* Special case: allow zero to five arguments. */
370 {fClamp, "clamp" , 1},
371 {fCosh, "cosh" , 1},
372 {fCos, "cos" , 1},
373 {fDebug, "debug" , 1},
374 {fDrc, "drc" , 2},
375#if defined(MAGICKCORE_HAVE_ERF)
376 {fErf, "erf" , 1},
377#endif
378 {fEpoch, "epoch" , 1}, /* Special case: needs a string date from a property eg %[date:modify] */
379 {fExp, "exp" , 1},
380 {fFloor, "floor" , 1},
381 {fGauss, "gauss" , 1},
382 {fGcd, "gcd" , 2},
383 {fHypot, "hypot" , 2},
384 {fInt, "int" , 1},
385 {fIsnan, "isnan" , 1},
386#if defined(MAGICKCORE_HAVE_J0)
387 {fJ0, "j0" , 1},
388#endif
389#if defined(MAGICKCORE_HAVE_J1)
390 {fJ1, "j1" , 1},
391#endif
392#if defined(MAGICKCORE_HAVE_J1)
393 {fJinc, "jinc" , 1},
394#endif
395 {fLn, "ln" , 1},
396 {fLogtwo, "logtwo", 1},
397 {fLog, "log" , 1},
398 {fMagickTime,"magicktime", 0},
399 {fMax, "max" , 2},
400 {fMin, "min" , 2},
401 {fMod, "mod" , 2},
402 {fNot, "not" , 1},
403 {fPow, "pow" , 2},
404 {fRand, "rand" , 0},
405 {fRound, "round" , 1},
406 {fSign, "sign" , 1},
407 {fSinc, "sinc" , 1},
408 {fSinh, "sinh" , 1},
409 {fSin, "sin" , 1},
410 {fSqrt, "sqrt" , 1},
411 {fSquish, "squish", 1},
412 {fTanh, "tanh" , 1},
413 {fTan, "tan" , 1},
414 {fTrunc, "trunc" , 1},
415 {fDo, "do", 2},
416 {fFor, "for", 3},
417 {fIf, "if", 3},
418 {fWhile, "while", 2},
419 {fU, "u", 1},
420 {fU0, "u0", 0},
421 {fUP, "up", 3},
422 {fS, "s", 0},
423 {fV, "v", 0},
424 {fP, "p", 2},
425 {fSP, "sp", 2},
426 {fVP, "vp", 2},
427
428 {fNull, "fnull" , 0}
429};
430
431#define FirstImgAttr ((ImgAttrE) (fNull+1))
432
433typedef enum {
434 aDepth = fNull+1,
435 aExtent,
436 aKurtosis,
437 aMaxima,
438 aMean,
439 aMedian,
440 aMinima,
441 aPage,
442 aPageX,
443 aPageY,
444 aPageWid,
445 aPageHt,
446 aPrintsize,
447 aPrintsizeX,
448 aPrintsizeY,
449 aQuality,
450 aRes,
451 aResX,
452 aResY,
453 aSkewness,
454 aStdDev,
455 aH,
456 aN,
457 aT,
458 aW,
459 aZ,
460 aNull
461} ImgAttrE;
462
463typedef struct {
464 ImgAttrE
465 attr;
466
467 const char
468 *str;
469
470 MagickBooleanType
471 need_stats;
472} ImgAttrT;
473
474static const ImgAttrT ImgAttrs[] = {
475 {aDepth, "depth", MagickTrue},
476 {aExtent, "extent", MagickFalse},
477 {aKurtosis, "kurtosis", MagickTrue},
478 {aMaxima, "maxima", MagickTrue},
479 {aMean, "mean", MagickTrue},
480 {aMedian, "median", MagickTrue},
481 {aMinima, "minima", MagickTrue},
482 {aPage, "page", MagickFalse},
483 {aPageX, "page.x", MagickFalse},
484 {aPageY, "page.y", MagickFalse},
485 {aPageWid, "page.width", MagickFalse},
486 {aPageHt, "page.height", MagickFalse},
487 {aPrintsize, "printsize", MagickFalse},
488 {aPrintsizeX, "printsize.x", MagickFalse},
489 {aPrintsizeY, "printsize.y", MagickFalse},
490 {aQuality, "quality", MagickFalse},
491 {aRes, "resolution", MagickFalse},
492 {aResX, "resolution.x", MagickFalse},
493 {aResY, "resolution.y", MagickFalse},
494 {aSkewness, "skewness", MagickTrue},
495 {aStdDev, "standard_deviation", MagickTrue},
496 {aH, "h", MagickFalse},
497 {aN, "n", MagickFalse},
498 {aT, "t", MagickFalse},
499 {aW, "w", MagickFalse},
500 {aZ, "z", MagickFalse},
501 {aNull, "anull", MagickFalse},
502 {aNull, "anull", MagickFalse},
503 {aNull, "anull", MagickFalse},
504 {aNull, "anull", MagickFalse}
505};
506
507#define FirstSym ((SymbolE) (aNull+1))
508
509typedef enum {
510 sHue = aNull+1,
511 sIntensity,
512 sLightness,
513 sLuma,
514 sLuminance,
515 sSaturation,
516 sA,
517 sB,
518 sC,
519 sG,
520 sI,
521 sJ,
522 sK,
523 sM,
524 sO,
525 sR,
526 sY,
527 sNull
528} SymbolE;
529
530typedef struct {
531 SymbolE
532 sym;
533
534 const char
535 *str;
536} SymbolT;
537
538static const SymbolT Symbols[] = {
539 {sHue, "hue"},
540 {sIntensity, "intensity"},
541 {sLightness, "lightness"},
542 {sLuma, "luma"},
543 {sLuminance, "luminance"},
544 {sSaturation, "saturation"},
545 {sA, "a"},
546 {sB, "b"},
547 {sC, "c"},
548 {sG, "g"},
549 {sI, "i"},
550 {sJ, "j"},
551 {sK, "k"},
552 {sM, "m"},
553 {sO, "o"},
554 {sR, "r"},
555 {sY, "y"},
556 {sNull, "snull"}
557};
558/*
559 There is no way to access new value of pixels. This might be a future enhancement, eg "q".
560 fP, oU and oV can have channel qualifier such as "u.r".
561 For meta channels, we might also allow numbered channels eg "u.2" or "u.16".
562 ... or have extra argument to p[].
563*/
564
565#define FirstCont (sNull+1)
566
567/* Run-time controls are in the RPN, not explicitly in the input string. */
568typedef enum {
569 rGoto = FirstCont,
570 rGotoChk,
571 rIfZeroGoto,
572 rIfNotZeroGoto,
573 rCopyFrom,
574 rCopyTo,
575 rZerStk,
576 rNull
577} ControlE;
578
579typedef struct {
580 ControlE
581 cont;
582
583 const char
584 *str;
585
586 int
587 number_args;
588} ControlT;
589
590static const ControlT Controls[] = {
591 {rGoto, "goto", 0},
592 {rGotoChk, "gotochk", 0},
593 {rIfZeroGoto, "ifzerogoto", 1},
594 {rIfNotZeroGoto, "ifnotzerogoto", 1},
595 {rCopyFrom, "copyfrom", 0},
596 {rCopyTo, "copyto", 1},
597 {rZerStk, "zerstk", 0},
598 {rNull, "rnull", 0}
599};
600
601#define NULL_ADDRESS -2
602
603typedef struct {
604 int
605 addr_query,
606 addr_colon;
607} TernaryT;
608
609typedef struct {
610 const char
611 *str;
612
613 PixelChannel
614 pixel_channel;
615} ChannelT;
616
617#define NO_CHAN_QUAL ((PixelChannel) (-1))
618#define THIS_CHANNEL ((PixelChannel) (-2))
619#define HUE_CHANNEL ((PixelChannel) (-3))
620#define SAT_CHANNEL ((PixelChannel) (-4))
621#define LIGHT_CHANNEL ((PixelChannel) (-5))
622#define INTENSITY_CHANNEL ((PixelChannel) (-6))
623
624static const ChannelT Channels[] = {
625 {"r", RedPixelChannel},
626 {"g", GreenPixelChannel},
627 {"b", BluePixelChannel},
628 {"c", CyanPixelChannel},
629 {"m", MagentaPixelChannel},
630 {"y", YellowPixelChannel},
631 {"k", BlackPixelChannel},
632 {"a", AlphaPixelChannel},
633 {"o", AlphaPixelChannel},
634 {"hue", HUE_CHANNEL},
635 {"saturation", SAT_CHANNEL},
636 {"lightness", LIGHT_CHANNEL},
637 {"intensity", INTENSITY_CHANNEL},
638 {"all", CompositePixelChannel},
639 {"this", THIS_CHANNEL},
640 {"", NO_CHAN_QUAL}
641};
642
643/* The index into UserSymbols is also the index into run-time UserSymVals.
644*/
645typedef struct {
646 char
647 *pex;
648
649 size_t
650 len;
652
653typedef enum {
654 etOperator,
655 etConstant,
656 etFunction,
657 etImgAttr,
658 etSymbol,
659 etColourConstant,
660 etControl
661} ElementTypeE;
662
663static const char * sElementTypes[] = {
664 "Operator",
665 "Constant",
666 "Function",
667 "ImgAttr",
668 "Symbol",
669 "ColConst",
670 "Control"
671};
672
673typedef struct {
674 char
675 *exp_start;
676
677 ElementTypeE
678 type;
679
680 fxFltType
681 val,
682 val1,
683 val2;
684
685 ImgAttrE
686 img_attr_qual;
687
688 int
689 element_index,
690 number_args,
691 number_dest, /* Number of Elements that "goto" this element */
692 operator_index;
693
694 MagickBooleanType
695 do_push,
696 is_relative;
697
698 PixelChannel
699 channel_qual;
700
701 size_t
702 exp_len;
703} ElementT;
704
705typedef enum {
706 rtUnknown,
707 rtEntireImage,
708 rtCornerOnly
709} RunTypeE;
710
711typedef struct {
712 CacheView *View;
713 /* Other per-image metadata could go here. */
714} ImgT;
715
716typedef struct {
717 RandomInfo * magick_restrict random_info;
718 int numValStack;
719 int usedValStack;
720 fxFltType * ValStack;
721 fxFltType * UserSymVals;
722 Quantum * thisPixel;
723} fxRtT;
724
725struct _FxInfo {
726 Image * image;
727 size_t ImgListLen;
728 ssize_t ImgNum;
729 MagickBooleanType NeedStats;
730 MagickBooleanType GotStats;
731 MagickBooleanType NeedHsl;
732 MagickBooleanType DebugOpt; /* Whether "-debug" option is in effect */
733 MagickBooleanType ContainsDebug; /* Whether expression contains "debug ()" function */
734 char * expression;
735 char * pex;
736 char ShortExp[MagickPathExtent]; /* for reporting */
737 int teDepth;
738 char token[MagickPathExtent];
739 size_t lenToken;
740 int numElements;
741 int usedElements;
742 ElementT * Elements; /* Elements is read-only at runtime. */
743 int numUserSymbols;
744 int usedUserSymbols;
745 UserSymbolT * UserSymbols;
746 int numOprStack;
747 int usedOprStack;
748 int maxUsedOprStack;
749 OperatorE * OperatorStack;
750 ChannelStatistics ** statistics;
751 int precision;
752 RunTypeE runType;
753
754 RandomInfo
755 **magick_restrict random_infos;
756
757 ImgT * Imgs;
758 Image ** Images;
759
760 ExceptionInfo * exception;
761
762 fxRtT * fxrts;
763};
764
765/* Forward declarations for recursion.
766*/
767static MagickBooleanType TranslateStatementList
768 (FxInfo * pfx, const char * strLimit, char * chLimit);
769
770static MagickBooleanType TranslateExpression
771 (FxInfo * pfx, const char * strLimit, char * chLimit, MagickBooleanType * needPopAll);
772
773static MagickBooleanType GetFunction (FxInfo * pfx, FunctionE fe);
774
775static inline MagickBooleanType ChanIsVirtual (PixelChannel pc)
776{
777 if (pc==HUE_CHANNEL || pc==SAT_CHANNEL || pc==LIGHT_CHANNEL || pc==INTENSITY_CHANNEL)
778 return MagickTrue;
779
780 return MagickFalse;
781}
782
783static MagickBooleanType InitFx (FxInfo * pfx, const Image * img,
784 MagickBooleanType CalcAllStats, ExceptionInfo *exception)
785{
786 ssize_t i=0;
787 const Image * next;
788
789 pfx->ImgListLen = GetImageListLength (img);
790 pfx->ImgNum = GetImageIndexInList (img);
791 pfx->image = (Image *)img;
792
793 pfx->NeedStats = MagickFalse;
794 pfx->GotStats = MagickFalse;
795 pfx->NeedHsl = MagickFalse;
796 pfx->DebugOpt = IsStringTrue (GetImageArtifact (img, "fx:debug"));
797 pfx->statistics = NULL;
798 pfx->Imgs = NULL;
799 pfx->Images = NULL;
800 pfx->exception = exception;
801 pfx->precision = GetMagickPrecision ();
802 pfx->random_infos = AcquireRandomInfoTLS ();
803 pfx->ContainsDebug = MagickFalse;
804 pfx->runType = (CalcAllStats) ? rtEntireImage : rtCornerOnly;
805 pfx->Imgs = (ImgT *)AcquireQuantumMemory (pfx->ImgListLen, sizeof (ImgT));
806 if (!pfx->Imgs) {
807 (void) ThrowMagickException (
808 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
809 "Imgs", "%lu",
810 (unsigned long) pfx->ImgListLen);
811 return MagickFalse;
812 }
813
814 next = GetFirstImageInList (img);
815 for ( ; next != (Image *) NULL; next=next->next)
816 {
817 ImgT * pimg = &pfx->Imgs[i];
818 pimg->View = AcquireVirtualCacheView (next, pfx->exception);
819 if (!pimg->View) {
820 (void) ThrowMagickException (
821 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
822 "View", "[%li]",
823 (long) i);
824 /* dealloc any done so far, and Imgs */
825 for ( ; i > 0; i--) {
826 pimg = &pfx->Imgs[i-1];
827 pimg->View = DestroyCacheView (pimg->View);
828 }
829 pfx->Imgs=(ImgT *) RelinquishMagickMemory (pfx->Imgs);
830 return MagickFalse;
831 }
832 i++;
833 }
834
835 pfx->Images = ImageListToArray (img, pfx->exception);
836 if (pfx->Images == (Image **) NULL) {
837 for ( ; i > 0; i--) {
838 ImgT * pimg = &pfx->Imgs[i-1];
839 pimg->View = DestroyCacheView (pimg->View);
840 }
841 pfx->Imgs=(ImgT *) RelinquishMagickMemory (pfx->Imgs);
842 pfx->random_infos = DestroyRandomInfoTLS (pfx->random_infos);
843 return MagickFalse;
844 }
845
846 return MagickTrue;
847}
848
849static MagickBooleanType DeInitFx (FxInfo * pfx)
850{
851 ssize_t i;
852
853 if (pfx->Images) pfx->Images = (Image**) RelinquishMagickMemory (pfx->Images);
854
855 if (pfx->Imgs) {
856 for (i = (ssize_t)GetImageListLength(pfx->image); i > 0; i--) {
857 ImgT * pimg = &pfx->Imgs[i-1];
858 pimg->View = DestroyCacheView (pimg->View);
859 }
860 pfx->Imgs=(ImgT *) RelinquishMagickMemory (pfx->Imgs);
861 }
862 pfx->random_infos = DestroyRandomInfoTLS (pfx->random_infos);
863
864 if (pfx->statistics) {
865 for (i = (ssize_t)GetImageListLength(pfx->image); i > 0; i--) {
866 pfx->statistics[i-1]=(ChannelStatistics *) RelinquishMagickMemory (pfx->statistics[i-1]);
867 }
868
869 pfx->statistics = (ChannelStatistics**) RelinquishMagickMemory(pfx->statistics);
870 }
871
872 return MagickTrue;
873}
874
875static ElementTypeE TypeOfOpr (int op)
876{
877 if (op < oNull) return etOperator;
878 if (op == oNull) return etConstant;
879 if (op <= fNull) return etFunction;
880 if (op <= aNull) return etImgAttr;
881 if (op <= sNull) return etSymbol;
882 if (op <= rNull) return etControl;
883
884 return (ElementTypeE) 0;
885}
886
887static char * SetPtrShortExp (FxInfo *pfx, char *pExp, size_t len)
888{
889 #define MaxLen 20
890
891 size_t slen;
892 char *p;
893
894 *pfx->ShortExp = '\0';
895
896 if (pExp && len) {
897 slen = CopyMagickString(pfx->ShortExp, pExp, MagickPathExtent);
898
899 if (slen > MaxLen) {
900 (void) CopyMagickString(pfx->ShortExp + MaxLen, "...", 4);
901 }
902
903 p = strchr(pfx->ShortExp, '\n');
904 if (p) (void) CopyMagickString(p, "...", 4);
905
906 p = strchr(pfx->ShortExp, '\r');
907 if (p) (void) CopyMagickString(p, "...", 4);
908 }
909
910 return pfx->ShortExp;
911}
912
913static char * SetShortExp (FxInfo * pfx)
914{
915 return SetPtrShortExp (pfx, pfx->pex, MaxTokenLen-1);
916}
917
918static int FindUserSymbol (FxInfo * pfx, char * name)
919/* returns index into pfx->UserSymbols, and thus into pfxrt->UserSymVals,
920 or NULL_ADDRESS if not found.
921*/
922{
923 int i;
924 size_t lenName;
925 lenName = strlen (name);
926 for (i=0; i < pfx->usedUserSymbols; i++) {
927 UserSymbolT *pus = &pfx->UserSymbols[i];
928 if (lenName == pus->len && LocaleNCompare (name, pus->pex, lenName)==0) break;
929 }
930 if (i == pfx->usedUserSymbols) return NULL_ADDRESS;
931 return i;
932}
933
934static MagickBooleanType ExtendUserSymbols (FxInfo * pfx)
935{
937 *new_symbols;
938
939 int
940 symbol_count;
941
942 symbol_count = (int) ceil (pfx->numUserSymbols * (1 + TableExtend));
943 new_symbols = (UserSymbolT *) AcquireQuantumMemory ((size_t) symbol_count,
944 sizeof (*new_symbols));
945 if (new_symbols == (UserSymbolT *) NULL) {
946 (void) ThrowMagickException (
947 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
948 "UserSymbols", "%i",
949 symbol_count);
950 return MagickFalse;
951 }
952 if (pfx->usedUserSymbols > 0)
953 (void) memcpy (new_symbols, pfx->UserSymbols,
954 (size_t) pfx->usedUserSymbols * sizeof (*new_symbols));
955 pfx->UserSymbols = (UserSymbolT *) RelinquishMagickMemory (pfx->UserSymbols);
956 pfx->UserSymbols = new_symbols;
957 pfx->numUserSymbols = symbol_count;
958
959 return MagickTrue;
960}
961
962static int AddUserSymbol (FxInfo * pfx, char * pex, size_t len)
963{
964 UserSymbolT *pus;
965 if (pfx->usedUserSymbols + 1 >= pfx->numUserSymbols) {
966 if (!ExtendUserSymbols (pfx)) return -1;
967 }
968 pus = &pfx->UserSymbols[pfx->usedUserSymbols++];
969 pus->pex = pex;
970 pus->len = len;
971
972 return pfx->usedUserSymbols-1;
973}
974
975static void DumpTables (FILE * fh)
976{
977
978 int i;
979 for (i=0; i <= rNull; i++) {
980 const char * str = "";
981 if ( i < oNull) str = Operators[i].str;
982 if (i >= (int) FirstFunc && i < fNull) str = Functions[i-(int) FirstFunc].str;
983 if (i >= (int) FirstImgAttr && i < aNull) str = ImgAttrs[i-(int) FirstImgAttr].str;
984 if (i >= (int) FirstSym && i < sNull) str = Symbols[i-(int) FirstSym].str;
985 if (i >= (int) FirstCont && i < rNull) str = Controls[i-(int) FirstCont].str;
986 if (i==0 ) fprintf (fh, "Operators:\n ");
987 else if (i==oNull) fprintf (fh, "\nFunctions:\n ");
988 else if (i==fNull) fprintf (fh, "\nImage attributes:\n ");
989 else if (i==aNull) fprintf (fh, "\nSymbols:\n ");
990 else if (i==sNull) fprintf (fh, "\nControls:\n ");
991 fprintf (fh, " %s", str);
992 }
993 fprintf (fh, "\n");
994}
995
996static char * NameOfUserSym (FxInfo * pfx, int ndx, char * buf)
997{
998 UserSymbolT * pus;
999 assert (ndx >= 0 && ndx < pfx->usedUserSymbols);
1000 pus = &pfx->UserSymbols[ndx];
1001 (void) CopyMagickString (buf, pus->pex, pus->len+1);
1002 return buf;
1003}
1004
1005static void DumpUserSymbols (FxInfo * pfx, FILE * fh)
1006{
1007 char UserSym[MagickPathExtent];
1008 int i;
1009 fprintf (fh, "UserSymbols (%i)\n", pfx->usedUserSymbols);
1010 for (i=0; i < pfx->usedUserSymbols; i++) {
1011 fprintf (fh, " %i: '%s'\n", i, NameOfUserSym (pfx, i, UserSym));
1012 }
1013}
1014
1015static MagickBooleanType BuildRPN (FxInfo * pfx)
1016{
1017 pfx->numUserSymbols = InitNumUserSymbols;
1018 pfx->usedUserSymbols = 0;
1019 pfx->UserSymbols = (UserSymbolT*) AcquireMagickMemory ((size_t) pfx->numUserSymbols * sizeof(UserSymbolT));
1020 if (!pfx->UserSymbols) {
1021 (void) ThrowMagickException (
1022 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
1023 "UserSymbols", "%i",
1024 pfx->numUserSymbols);
1025 return MagickFalse;
1026 }
1027
1028 pfx->numElements = RpnInit;
1029 pfx->usedElements = 0;
1030 pfx->Elements = NULL;
1031
1032 pfx->Elements = (ElementT*) AcquireMagickMemory ((size_t) pfx->numElements * sizeof(ElementT));
1033
1034 if (!pfx->Elements) {
1035 (void) ThrowMagickException (
1036 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
1037 "Elements", "%i",
1038 pfx->numElements);
1039 return MagickFalse;
1040 }
1041
1042 pfx->usedOprStack = 0;
1043 pfx->maxUsedOprStack = 0;
1044 pfx->numOprStack = InitNumOprStack;
1045 pfx->OperatorStack = (OperatorE*) AcquireMagickMemory ((size_t) pfx->numOprStack * sizeof(OperatorE));
1046 if (!pfx->OperatorStack) {
1047 (void) ThrowMagickException (
1048 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
1049 "OperatorStack", "%i",
1050 pfx->numOprStack);
1051 return MagickFalse;
1052 }
1053
1054 return MagickTrue;
1055}
1056
1057static MagickBooleanType AllocFxRt (FxInfo * pfx, fxRtT * pfxrt)
1058{
1059 int nRnd;
1060 int i;
1061 pfxrt->random_info = AcquireRandomInfo ();
1062 pfxrt->thisPixel = NULL;
1063
1064 nRnd = 20 + 10 * (int) GetPseudoRandomValue (pfxrt->random_info);
1065 for (i=0; i < nRnd; i++) (void) GetPseudoRandomValue (pfxrt->random_info);;
1066
1067 pfxrt->usedValStack = 0;
1068 pfxrt->numValStack = 2 * pfx->maxUsedOprStack;
1069 if (pfxrt->numValStack < MinValStackSize) pfxrt->numValStack = MinValStackSize;
1070 pfxrt->ValStack = (fxFltType*) AcquireMagickMemory ((size_t) pfxrt->numValStack * sizeof(fxFltType));
1071 if (!pfxrt->ValStack) {
1072 (void) ThrowMagickException (
1073 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
1074 "ValStack", "%i",
1075 pfxrt->numValStack);
1076 return MagickFalse;
1077 }
1078
1079 pfxrt->UserSymVals = NULL;
1080
1081 if (pfx->usedUserSymbols) {
1082 pfxrt->UserSymVals = (fxFltType*) AcquireMagickMemory ((size_t) pfx->usedUserSymbols * sizeof(fxFltType));
1083 if (!pfxrt->UserSymVals) {
1084 (void) ThrowMagickException (
1085 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
1086 "UserSymVals", "%i",
1087 pfx->usedUserSymbols);
1088 return MagickFalse;
1089 }
1090 for (i = 0; i < pfx->usedUserSymbols; i++) pfxrt->UserSymVals[i] = (fxFltType) 0;
1091 }
1092
1093 return MagickTrue;
1094}
1095
1096static MagickBooleanType ExtendRPN(FxInfo * pfx)
1097{
1098 ElementT
1099 *new_elements;
1100
1101 int
1102 element_count;
1103
1104 /*
1105 Keep the original table and size intact unless growth succeeds.
1106 */
1107 element_count=(int) ceil(pfx->numElements*(1+TableExtend));
1108 new_elements=(ElementT *) AcquireQuantumMemory((size_t) element_count,
1109 sizeof(*new_elements));
1110 if (new_elements == (ElementT *) NULL)
1111 {
1112 (void) ThrowMagickException(pfx->exception,GetMagickModule(),
1113 ResourceLimitError,"MemoryAllocationFailed","`%s'","RPN table");
1114 return(MagickFalse);
1115 }
1116 (void) memcpy(new_elements,pfx->Elements,(size_t) pfx->usedElements*
1117 sizeof(*new_elements));
1118 pfx->Elements=(ElementT *) RelinquishMagickMemory(pfx->Elements);
1119 pfx->Elements=new_elements;
1120 pfx->numElements=element_count;
1121 if (!pfx->Elements)
1122 {
1123 (void) ThrowMagickException(pfx->exception,GetMagickModule(),
1124 ResourceLimitFatalError,"Elements","%i",pfx->numElements);
1125 return(MagickFalse);
1126 }
1127 return(MagickTrue);
1128}
1129
1130static inline MagickBooleanType OprInPlace(int op)
1131{
1132 return(op >= oAddEq && op <= oSubSub ? MagickTrue : MagickFalse);
1133}
1134
1135static const char * OprStr (int oprNum)
1136{
1137 const char * str;
1138 if (oprNum < 0) str = "bad OprStr";
1139 else if (oprNum <= oNull) str = Operators[oprNum].str;
1140 else if (oprNum <= fNull) str = Functions[oprNum-(int) FirstFunc].str;
1141 else if (oprNum <= aNull) str = ImgAttrs[oprNum-(int) FirstImgAttr].str;
1142 else if (oprNum <= sNull) str = Symbols[oprNum-(int) FirstSym].str;
1143 else if (oprNum <= rNull) str = Controls[oprNum-(int) FirstCont].str;
1144 else {
1145 str = "bad OprStr";
1146 }
1147 return str;
1148}
1149
1150static MagickBooleanType DumpRPN (FxInfo * pfx, FILE * fh)
1151{
1152 int i;
1153
1154 fprintf (fh, "DumpRPN:");
1155 fprintf (fh, " numElements=%i", pfx->numElements);
1156 fprintf (fh, " usedElements=%i", pfx->usedElements);
1157 fprintf (fh, " maxUsedOprStack=%i", pfx->maxUsedOprStack);
1158 fprintf (fh, " ImgListLen=%g", (double) pfx->ImgListLen);
1159 fprintf (fh, " NeedStats=%s", pfx->NeedStats ? "yes" : "no");
1160 fprintf (fh, " GotStats=%s", pfx->GotStats ? "yes" : "no");
1161 fprintf (fh, " NeedHsl=%s\n", pfx->NeedHsl ? "yes" : "no");
1162 if (pfx->runType==rtEntireImage) fprintf (fh, "EntireImage");
1163 else if (pfx->runType==rtCornerOnly) fprintf (fh, "CornerOnly");
1164 fprintf (fh, "\n");
1165
1166
1167 for (i=0; i < pfx->usedElements; i++) {
1168 ElementT * pel = &pfx->Elements[i];
1169 pel->number_dest = 0;
1170 }
1171 for (i=0; i < pfx->usedElements; i++) {
1172 ElementT * pel = &pfx->Elements[i];
1173 if (pel->operator_index == rGoto || pel->operator_index == rGotoChk || pel->operator_index == rIfZeroGoto || pel->operator_index == rIfNotZeroGoto) {
1174 if (pel->element_index >= 0 && pel->element_index < pfx->numElements) {
1175 ElementT * pelDest = &pfx->Elements[pel->element_index];
1176 pelDest->number_dest++;
1177 }
1178 }
1179 }
1180 for (i=0; i < pfx->usedElements; i++) {
1181 char UserSym[MagickPathExtent];
1182
1183 ElementT * pel = &pfx->Elements[i];
1184 const char * str = OprStr (pel->operator_index);
1185 const char *sRelAbs = "";
1186
1187 if (pel->operator_index == fP || pel->operator_index == fUP || pel->operator_index == fVP || pel->operator_index == fSP)
1188 sRelAbs = pel->is_relative ? "[]" : "{}";
1189
1190 if (pel->type == etColourConstant)
1191 fprintf (fh, " %i: %s vals=%.*Lg,%.*Lg,%.*Lg '%s%s' nArgs=%i ndx=%i %s",
1192 i, sElementTypes[pel->type],
1193 pfx->precision, pel->val, pfx->precision, pel->val1, pfx->precision, pel->val2,
1194 str, sRelAbs, pel->number_args, pel->element_index,
1195 pel->do_push ? "push" : "NO push");
1196 else
1197 fprintf (fh, " %i: %s val=%.*Lg '%s%s' nArgs=%i ndx=%i %s",
1198 i, sElementTypes[pel->type], pfx->precision, pel->val, str, sRelAbs,
1199 pel->number_args, pel->element_index,
1200 pel->do_push ? "push" : "NO push");
1201
1202 if (pel->img_attr_qual != aNull)
1203 fprintf (fh, " ia=%s", OprStr((int) pel->img_attr_qual));
1204
1205 if (pel->channel_qual != NO_CHAN_QUAL) {
1206 if (pel->channel_qual == THIS_CHANNEL) fprintf (fh, " ch=this");
1207 else fprintf (fh, " ch=%i", pel->channel_qual);
1208 }
1209
1210 if (pel->operator_index == rCopyTo) {
1211 fprintf (fh, " CopyTo ==> %s", NameOfUserSym (pfx, pel->element_index, UserSym));
1212 } else if (pel->operator_index == rCopyFrom) {
1213 fprintf (fh, " CopyFrom <== %s", NameOfUserSym (pfx, pel->element_index, UserSym));
1214 } else if (OprInPlace (pel->operator_index)) {
1215 fprintf (fh, " <==> %s", NameOfUserSym (pfx, pel->element_index, UserSym));
1216 }
1217 if (pel->number_dest > 0) fprintf (fh, " <==dest(%i)", pel->number_dest);
1218 fprintf (fh, "\n");
1219 }
1220 return MagickTrue;
1221}
1222
1223static void DestroyRPN (FxInfo * pfx)
1224{
1225 pfx->numOprStack = 0;
1226 pfx->usedOprStack = 0;
1227 if (pfx->OperatorStack) pfx->OperatorStack = (OperatorE*) RelinquishMagickMemory (pfx->OperatorStack);
1228
1229 pfx->numElements = 0;
1230 pfx->usedElements = 0;
1231 if (pfx->Elements) pfx->Elements = (ElementT*) RelinquishMagickMemory (pfx->Elements);
1232
1233 pfx->usedUserSymbols = 0;
1234 if (pfx->UserSymbols) pfx->UserSymbols = (UserSymbolT*) RelinquishMagickMemory (pfx->UserSymbols);
1235}
1236
1237static void DestroyFxRt (fxRtT * pfxrt)
1238{
1239 pfxrt->usedValStack = 0;
1240 if (pfxrt->ValStack) pfxrt->ValStack = (fxFltType*) RelinquishMagickMemory (pfxrt->ValStack);
1241 if (pfxrt->UserSymVals) pfxrt->UserSymVals = (fxFltType*) RelinquishMagickMemory (pfxrt->UserSymVals);
1242
1243 pfxrt->random_info = DestroyRandomInfo (pfxrt->random_info);
1244}
1245
1246static size_t GetToken (FxInfo * pfx)
1247/* Returns length of token that starts with an alpha,
1248 or 0 if it isn't a token that starts with an alpha.
1249 j0 and j1 have trailing digit.
1250 Also colours like "gray47" have more trailing digits.
1251 After initial alpha(s) also allow single "_", eg "standard_deviation".
1252 Does not advance pfx->pex.
1253 This splits "mean.r" etc.
1254*/
1255{
1256
1257 char * p = pfx->pex;
1258 size_t len = 0;
1259 *pfx->token = '\0';
1260 pfx->lenToken = 0;
1261 if (!isalpha((int) ((unsigned char) *p))) return 0;
1262
1263 /* Regard strings that start "icc-" or "device-",
1264 followed by any number of alphas,
1265 as a token.
1266 */
1267
1268 if (LocaleNCompare (p, "icc-", 4) == 0) {
1269 len = 4;
1270 p += 4;
1271 while (isalpha ((int) ((unsigned char) *p))) { len++; p++; }
1272 } else if (LocaleNCompare (p, "device-", 7) == 0) {
1273 len = 7;
1274 p += 7;
1275 while (isalpha ((int) ((unsigned char) *p))) { len++; p++; }
1276 } else {
1277 while (isalpha ((int) ((unsigned char) *p))) { len++; p++; }
1278 if (*p == '_') { len++; p++; }
1279 while (isalpha ((int) ((unsigned char) *p))) { len++; p++; }
1280 while (isdigit ((int) ((unsigned char) *p))) { len++; p++; }
1281 }
1282 if (len >= MaxTokenLen) {
1283 (void) ThrowMagickException (
1284 pfx->exception, GetMagickModule(), OptionError,
1285 "GetToken: too long", "%g at '%s'",
1286 (double) len, SetShortExp(pfx));
1287 len = MaxTokenLen - 1;
1288 }
1289 if (len) {
1290 (void) CopyMagickString (pfx->token, pfx->pex, len+1);
1291 }
1292
1293 pfx->lenToken = strlen (pfx->token);
1294 return len;
1295}
1296
1297static MagickBooleanType TokenMaybeUserSymbol (FxInfo * pfx)
1298{
1299 char * p = pfx->token;
1300 int i = 0;
1301 while (*p) {
1302 if (!isalpha ((int)*p++)) return MagickFalse;
1303 i++;
1304 }
1305 if (i < 2) return MagickFalse;
1306 return MagickTrue;
1307}
1308
1309static MagickBooleanType AddElement(FxInfo * pfx,fxFltType val,int oprNum)
1310{
1311 ElementT * pel;
1312
1313 assert (oprNum <= rNull);
1314
1315 if (++pfx->usedElements >= pfx->numElements)
1316 {
1317 if (ExtendRPN(pfx) == MagickFalse)
1318 {
1319 pfx->usedElements--;
1320 return(MagickFalse);
1321 }
1322 }
1323 if (pfx->Elements == (ElementT *) NULL)
1324 {
1325 pfx->usedElements--;
1326 return(MagickFalse);
1327 }
1328 pel = &pfx->Elements[pfx->usedElements-1];
1329 pel->type = TypeOfOpr (oprNum);
1330 pel->val = val;
1331 pel->val1 = (fxFltType) 0;
1332 pel->val2 = (fxFltType) 0;
1333 pel->operator_index = oprNum;
1334 pel->do_push = MagickTrue;
1335 pel->element_index = 0;
1336 pel->channel_qual = NO_CHAN_QUAL;
1337 pel->img_attr_qual = aNull;
1338 pel->number_dest = 0;
1339 pel->exp_start = NULL;
1340 pel->exp_len = 0;
1341
1342 if (oprNum <= oNull) pel->number_args = Operators[oprNum].number_args;
1343 else if (oprNum <= fNull) pel->number_args = Functions[oprNum-(int) FirstFunc].number_args;
1344 else if (oprNum <= aNull) pel->number_args = 0;
1345 else if (oprNum <= sNull) pel->number_args = 0;
1346 else pel->number_args = Controls[oprNum-(int) FirstCont].number_args;
1347
1348 return MagickTrue;
1349}
1350
1351static MagickBooleanType AddAddressingElement (FxInfo * pfx, int oprNum, int EleNdx)
1352{
1353 ElementT * pel;
1354 if (!AddElement (pfx, (fxFltType) 0, oprNum)) return MagickFalse;
1355 pel = &pfx->Elements[pfx->usedElements-1];
1356 pel->element_index = EleNdx;
1357 if (oprNum == rGoto || oprNum == rGotoChk || oprNum == rIfZeroGoto || oprNum == rIfNotZeroGoto
1358 || oprNum == rZerStk)
1359 {
1360 pel->do_push = MagickFalse;
1361 }
1362
1363 /* Note: for() may or may not need pushing,
1364 depending on whether the value is needed, eg "for(...)+2" or debug(for(...)).
1365 */
1366
1367 return MagickTrue;
1368}
1369
1370static MagickBooleanType AddColourElement (FxInfo * pfx, fxFltType val0, fxFltType val1, fxFltType val2)
1371{
1372 ElementT * pel;
1373 if (!AddElement (pfx, val0, oNull)) return MagickFalse;
1374 pel = &pfx->Elements[pfx->usedElements-1];
1375 pel->val1 = val1;
1376 pel->val2 = val2;
1377 pel->type = etColourConstant;
1378 return MagickTrue;
1379}
1380
1381static inline void SkipSpaces (FxInfo * pfx)
1382{
1383 while (isspace ((int)*pfx->pex)) pfx->pex++;
1384}
1385
1386static inline char PeekChar (FxInfo * pfx)
1387{
1388 SkipSpaces (pfx);
1389 return *pfx->pex;
1390}
1391
1392static inline MagickBooleanType PeekStr (FxInfo * pfx, const char * str)
1393{
1394 SkipSpaces (pfx);
1395
1396 return (LocaleNCompare (pfx->pex, str, strlen(str))==0 ? MagickTrue : MagickFalse);
1397}
1398
1399static MagickBooleanType ExpectChar (FxInfo * pfx, char c)
1400{
1401 if (PeekChar (pfx) != c) {
1402 (void) ThrowMagickException (
1403 pfx->exception, GetMagickModule(), OptionError,
1404 "Expected char", "'%c' at '%s'", c, SetShortExp (pfx));
1405 return MagickFalse;
1406 }
1407 pfx->pex++;
1408 return MagickTrue;
1409}
1410
1411static int MaybeXYWH (FxInfo * pfx, ImgAttrE * pop)
1412/* If ".x" or ".y" or ".width" or ".height" increments *pop and returns 1 to 4 .
1413 Otherwise returns 0.
1414*/
1415{
1416 int ret=0;
1417
1418 if (*pop != aPage && *pop != aPrintsize && *pop != aRes) return 0;
1419
1420 if (PeekChar (pfx) != '.') return 0;
1421
1422 if (!ExpectChar (pfx, '.')) return 0;
1423
1424 (void) GetToken (pfx);
1425 if (LocaleCompare ("x", pfx->token)==0) ret=1;
1426 else if (LocaleCompare ("y", pfx->token)==0) ret=2;
1427 else if (LocaleCompare ("width", pfx->token)==0) ret=3;
1428 else if (LocaleCompare ("height", pfx->token)==0) ret=4;
1429
1430 if (!ret)
1431 (void) ThrowMagickException (
1432 pfx->exception, GetMagickModule(), OptionError,
1433 "Invalid 'x' or 'y' or 'width' or 'height' token=", "'%s' at '%s'",
1434 pfx->token, SetShortExp(pfx));
1435
1436 if (*pop == aPage) (*pop) = (ImgAttrE) ((int) *pop + ret);
1437 else {
1438 if (ret > 2) {
1439 (void) ThrowMagickException (
1440 pfx->exception, GetMagickModule(), OptionError,
1441 "Invalid 'width' or 'height' token=", "'%s' at '%s'",
1442 pfx->token, SetShortExp(pfx));
1443 } else {
1444 (*pop) = (ImgAttrE) ((int) *pop + ret);
1445 }
1446 }
1447 pfx->pex+=pfx->lenToken;
1448
1449 return ret;
1450}
1451
1452static MagickBooleanType ExtendOperatorStack (FxInfo * pfx)
1453{
1454 OperatorE
1455 *new_stack;
1456
1457 int
1458 stack_count;
1459
1460 stack_count = (int) ceil (pfx->numOprStack * (1 + TableExtend));
1461 new_stack = (OperatorE *) AcquireQuantumMemory ((size_t) stack_count,
1462 sizeof (*new_stack));
1463 if (new_stack == (OperatorE *) NULL) {
1464 (void) ThrowMagickException (
1465 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
1466 "OprStack", "%i",
1467 stack_count);
1468 return MagickFalse;
1469 }
1470 if (pfx->usedOprStack > 0)
1471 (void) memcpy (new_stack, pfx->OperatorStack,
1472 (size_t) pfx->usedOprStack * sizeof (*new_stack));
1473 pfx->OperatorStack = (OperatorE *) RelinquishMagickMemory (pfx->OperatorStack);
1474 pfx->OperatorStack = new_stack;
1475 pfx->numOprStack = stack_count;
1476 return MagickTrue;
1477}
1478
1479static MagickBooleanType PushOperatorStack (FxInfo * pfx, int op)
1480{
1481 if (pfx->usedOprStack + 1 >= pfx->numOprStack) {
1482 if (!ExtendOperatorStack (pfx))
1483 return MagickFalse;
1484 }
1485 pfx->OperatorStack[pfx->usedOprStack++] = (OperatorE) op;
1486
1487 if (pfx->maxUsedOprStack < pfx->usedOprStack)
1488 pfx->maxUsedOprStack = pfx->usedOprStack;
1489 return MagickTrue;
1490}
1491
1492static OperatorE GetLeadingOp (FxInfo * pfx)
1493{
1494 OperatorE op = oNull;
1495
1496 if (*pfx->pex == '-') op = oUnaryMinus;
1497 else if (*pfx->pex == '+') op = oUnaryPlus;
1498 else if (*pfx->pex == '~') op = oBitNot;
1499 else if (*pfx->pex == '!') op = oLogNot;
1500 else if (*pfx->pex == '(') op = oOpenParen;
1501
1502 return op;
1503}
1504
1505static inline MagickBooleanType OprIsUnaryPrefix (OperatorE op)
1506{
1507 return (op == oUnaryMinus || op == oUnaryPlus || op == oBitNot || op == oLogNot ? MagickTrue : MagickFalse);
1508}
1509
1510static MagickBooleanType TopOprIsUnaryPrefix (FxInfo * pfx)
1511{
1512 if (!pfx->usedOprStack) return MagickFalse;
1513
1514 return OprIsUnaryPrefix (pfx->OperatorStack[pfx->usedOprStack-1]);
1515}
1516
1517static MagickBooleanType PopOprOpenParen (FxInfo * pfx, OperatorE op)
1518{
1519
1520 if (!pfx->usedOprStack) return MagickFalse;
1521
1522 if (pfx->OperatorStack[pfx->usedOprStack-1] != op) return MagickFalse;
1523
1524 pfx->usedOprStack--;
1525
1526 return MagickTrue;
1527}
1528
1529static int GetCoordQualifier (FxInfo * pfx, int op)
1530/* Returns -1 if invalid CoordQualifier, +1 if valid and appropriate.
1531*/
1532{
1533 if (op != fU && op != fV && op != fS) return -1;
1534
1535 (void) GetToken (pfx);
1536
1537 if (pfx->lenToken != 1) {
1538 return -1;
1539 }
1540 if (*pfx->token != 'p' && *pfx->token != 'P') return -1;
1541 if (!GetFunction (pfx, fP)) return -1;
1542
1543 return 1;
1544}
1545
1546static PixelChannel GetChannelQualifier (FxInfo * pfx, int op)
1547{
1548 if (op == fU || op == fV || op == fP ||
1549 op == fUP || op == fVP ||
1550 op == fS || (op >= (int) FirstImgAttr && op <= aNull)
1551 )
1552 {
1553 const ChannelT * pch = &Channels[0];
1554 (void) GetToken (pfx);
1555
1556 while (*pch->str) {
1557 if (LocaleCompare (pch->str, pfx->token)==0) {
1558
1559 if (op >= (int) FirstImgAttr && op <= (int) ((OperatorE)aNull) &&
1560 ChanIsVirtual (pch->pixel_channel)
1561 )
1562 {
1563 (void) ThrowMagickException (
1564 pfx->exception, GetMagickModule(), OptionError,
1565 "Can't have image attribute with channel qualifier at", "'%s' at '%s'",
1566 pfx->token, SetShortExp(pfx));
1567 return NO_CHAN_QUAL;
1568 }
1569
1570 pfx->pex += pfx->lenToken;
1571 return pch->pixel_channel;
1572 }
1573 pch++;
1574 }
1575 }
1576 return NO_CHAN_QUAL;
1577}
1578
1579static ImgAttrE GetImgAttrToken (FxInfo * pfx)
1580{
1581 ImgAttrE ia = aNull;
1582 const char * iaStr = "";
1583 for (ia = FirstImgAttr; ia < aNull; ia=(ImgAttrE) (ia+1)) {
1584 iaStr = ImgAttrs[ia-(int) FirstImgAttr].str;
1585 if (LocaleCompare (iaStr, pfx->token)==0) {
1586 pfx->pex += strlen(pfx->token);
1587 if (ImgAttrs[ia-(int) FirstImgAttr].need_stats != MagickFalse) pfx->NeedStats = MagickTrue;
1588 MaybeXYWH (pfx, &ia);
1589 break;
1590 }
1591 }
1592
1593 if (ia == aPage || ia == aPrintsize || ia == aRes) {
1594 (void) ThrowMagickException (
1595 pfx->exception, GetMagickModule(), OptionError,
1596 "Attribute", "'%s' needs qualifier at '%s'",
1597 iaStr, SetShortExp(pfx));
1598 }
1599
1600 return ia;
1601}
1602
1603static ImgAttrE GetImgAttrQualifier (FxInfo * pfx, int op)
1604{
1605 ImgAttrE ia = aNull;
1606 if (op == (OperatorE)fU || op == (OperatorE)fV || op == (OperatorE)fP || op == (OperatorE)fS) {
1607 (void) GetToken (pfx);
1608 if (pfx->lenToken == 0) {
1609 return aNull;
1610 }
1611 ia = GetImgAttrToken (pfx);
1612 }
1613 return ia;
1614}
1615
1616static MagickBooleanType IsQualifier (FxInfo * pfx)
1617{
1618 if (PeekChar (pfx) == '.') {
1619 pfx->pex++;
1620 return MagickTrue;
1621 }
1622 return MagickFalse;
1623}
1624
1625static MagickBooleanType ParseISO860(const char* text,struct tm* tp)
1626{
1627 int
1628 year,
1629 month,
1630 day,
1631 hour,
1632 min,
1633 sec;
1634
1635 memset(tp,0,sizeof(struct tm));
1636 if (MagickSscanf(text,"%d-%d-%dT%d:%d:%d",&year,&month,&day,&hour,&min,&sec) != 6)
1637 return(MagickFalse);
1638 tp->tm_year=year-1900;
1639 tp->tm_mon=month-1;
1640 tp->tm_mday=day;
1641 tp->tm_hour=hour;
1642 tp->tm_min=min;
1643 tp->tm_sec=sec;
1644 tp->tm_isdst=-1;
1645 return(MagickTrue);
1646}
1647
1648static ssize_t GetProperty (FxInfo * pfx, fxFltType *val, fxFltType *seconds)
1649/* Returns number of characters to swallow.
1650 Returns "-1" means invalid input.
1651 Returns "0" means no relevant input (don't swallow, but not an error).
1652 If *seconds is not null, sets that from assumed date-time, or SECONDS_ERR if error.
1653*/
1654{
1655 if (seconds != NULL) *seconds = SECONDS_ERR;
1656
1657 if (PeekStr (pfx, "%[")) {
1658 int level = 0;
1659 size_t len;
1660 char sProperty [MagickPathExtent];
1661 char * p = pfx->pex + 2;
1662
1663 while (*p) {
1664
1665 if (*p == '[') level++;
1666 else if (*p == ']') {
1667 if (level == 0) break;
1668 level--;
1669 }
1670 p++;
1671 }
1672 if (!*p || level != 0) {
1673 (void) ThrowMagickException (
1674 pfx->exception, GetMagickModule(), OptionError,
1675 "After '%[' expected ']' at", "'%s'",
1676 SetShortExp(pfx));
1677 return -1;
1678 }
1679
1680 len = (size_t) (p - pfx->pex + 1);
1681 if (len > MaxTokenLen) {
1682 (void) ThrowMagickException (
1683 pfx->exception, GetMagickModule(), OptionError,
1684 "Too much text between '%[' and ']' at", "'%s'",
1685 SetShortExp(pfx));
1686 return -1;
1687 }
1688
1689 (void) CopyMagickString (sProperty, pfx->pex, len+1);
1690 sProperty[len] = '\0';
1691 {
1692 char * tailptr;
1693 char * text;
1694 text = InterpretImageProperties (pfx->image->image_info, pfx->image,
1695 sProperty, pfx->exception);
1696 if (!text || !*text) {
1697 text = DestroyString(text);
1698 (void) ThrowMagickException (
1699 pfx->exception, GetMagickModule(), OptionError,
1700 "Unknown property", "'%s' at '%s'",
1701 sProperty, SetShortExp(pfx));
1702 return -1;
1703 }
1704
1705 if (seconds != NULL) {
1706 struct tm tp;
1707 if (ParseISO860(text,&tp) == MagickFalse) {
1708 (void) ThrowMagickException (
1709 pfx->exception, GetMagickModule(), OptionError,
1710 "Function 'epoch' expected date property, found ", "'%s' at '%s'",
1711 text, SetShortExp(pfx));
1712 text = DestroyString(text);
1713 *seconds = SECONDS_ERR;
1714 return -1;
1715 }
1716 *seconds = (fxFltType)mktime (&tp);
1717 *val = *seconds;
1718 } else {
1719 *val = strtold (text, &tailptr);
1720 if (text == tailptr) {
1721 (void) ThrowMagickException (
1722 pfx->exception, GetMagickModule(), OptionError,
1723 "Property", "'%s' text '%s' is not a number at '%s'",
1724 sProperty, text, SetShortExp(pfx));
1725 text = DestroyString(text);
1726 return -1;
1727 }
1728 }
1729 text = DestroyString(text);
1730 }
1731 return ((ssize_t) len);
1732 }
1733
1734 return 0;
1735}
1736
1737static inline ssize_t GetConstantColour (FxInfo * pfx, fxFltType *v0, fxFltType *v1, fxFltType *v2)
1738/* Finds named colour such as "blue" and colorspace function such as "lab(10,20,30)".
1739 Returns number of characters to swallow.
1740 Return -1 means apparently a constant colour, but with an error.
1741 Return 0 means not a constant colour, but not an error.
1742*/
1743{
1744 PixelInfo
1745 colour;
1746
1747 ExceptionInfo
1748 *dummy_exception = AcquireExceptionInfo ();
1749
1750 char
1751 *p;
1752
1753 MagickBooleanType
1754 IsGray,
1755 IsIcc,
1756 IsDev;
1757
1758 char ColSp[MagickPathExtent];
1759 if (pfx->lenToken == 0) {
1760 dummy_exception = DestroyExceptionInfo (dummy_exception);
1761 return 0;
1762 }
1763 (void) CopyMagickString (ColSp, pfx->token, MaxTokenLen);
1764 p = ColSp + pfx->lenToken - 1;
1765 if (*p == 'a' || *p == 'A') *p = '\0';
1766
1767 (void) GetPixelInfo (pfx->image, &colour);
1768
1769 /* "gray" is both a colorspace and a named colour. */
1770
1771 IsGray = (LocaleCompare (ColSp, "gray") == 0) ? MagickTrue : MagickFalse;
1772 IsIcc = (LocaleCompare (ColSp, "icc-color") == 0) ? MagickTrue : MagickFalse;
1773 IsDev = (LocaleNCompare (ColSp, "device-", 7) == 0) ? MagickTrue : MagickFalse;
1774
1775 /* QueryColorCompliance will raise a warning if it isn't a colour, so we discard any exceptions.
1776 */
1777 if (!QueryColorCompliance (pfx->token, AllCompliance, &colour, dummy_exception) || IsGray) {
1778 ssize_t type = ParseCommandOption (MagickColorspaceOptions, MagickFalse, ColSp);
1779 if (type >= 0 || IsIcc || IsDev) {
1780 char * q = pfx->pex + pfx->lenToken;
1781 while (isspace((int) ((unsigned char) *q))) q++;
1782 if (*q == '(') {
1783 size_t lenfun;
1784 char sFunc[MagickPathExtent];
1785 while (*q && *q != ')') q++;
1786 if (!*q) {
1787 (void) ThrowMagickException (
1788 pfx->exception, GetMagickModule(), OptionError,
1789 "constant color missing ')'", "at '%s'",
1790 SetShortExp(pfx));
1791 dummy_exception = DestroyExceptionInfo (dummy_exception);
1792 return -1;
1793 }
1794 lenfun = (size_t) (q - pfx->pex + 1);
1795 if (lenfun > MaxTokenLen) {
1796 (void) ThrowMagickException (
1797 pfx->exception, GetMagickModule(), OptionError,
1798 "lenfun too long", "'%lu' at '%s'",
1799 (unsigned long) lenfun, SetShortExp(pfx));
1800 dummy_exception = DestroyExceptionInfo (dummy_exception);
1801 return -1;
1802 }
1803 (void) CopyMagickString (sFunc, pfx->pex, lenfun+1);
1804 if (QueryColorCompliance (sFunc, AllCompliance, &colour, dummy_exception)) {
1805 *v0 = QuantumScale*colour.red;
1806 *v1 = QuantumScale*colour.green;
1807 *v2 = QuantumScale*colour.blue;
1808 dummy_exception = DestroyExceptionInfo (dummy_exception);
1809 return (ssize_t)lenfun;
1810 }
1811 } else {
1812 (void) ThrowMagickException (
1813 pfx->exception, GetMagickModule(), OptionError,
1814 "colorspace but not a valid color with '(...)' at", "'%s'",
1815 SetShortExp(pfx));
1816 dummy_exception = DestroyExceptionInfo (dummy_exception);
1817 return -1;
1818 }
1819 }
1820 if (!IsGray) {
1821 dummy_exception = DestroyExceptionInfo (dummy_exception);
1822 return 0;
1823 }
1824 }
1825
1826 *v0 = QuantumScale*colour.red;
1827 *v1 = QuantumScale*colour.green;
1828 *v2 = QuantumScale*colour.blue;
1829
1830 dummy_exception = DestroyExceptionInfo (dummy_exception);
1831 return (ssize_t)strlen (pfx->token);
1832}
1833
1834static inline ssize_t GetHexColour (FxInfo * pfx, fxFltType *v0, fxFltType *v1, fxFltType *v2)
1835/* Returns number of characters to swallow.
1836 Negative return means it starts with '#', but invalid hex number.
1837*/
1838{
1839 char * p;
1840 size_t len;
1841 PixelInfo colour;
1842
1843 if (*pfx->pex != '#') return 0;
1844
1845 /* find end of hex digits. */
1846 p = pfx->pex + 1;
1847 while (isxdigit ((int)*p)) p++;
1848 if (isalpha ((int)*p)) {
1849 (void) ThrowMagickException (
1850 pfx->exception, GetMagickModule(), OptionError,
1851 "Bad hex number at", "'%s'",
1852 SetShortExp(pfx));
1853 return -1;
1854 }
1855
1856 len = (size_t) (p - pfx->pex);
1857 if (len < 1) return 0;
1858 if (len >= MaxTokenLen) {
1859 (void) ThrowMagickException (
1860 pfx->exception, GetMagickModule(), OptionError,
1861 "Hex colour too long at", "'%s'",
1862 SetShortExp(pfx));
1863 return -1;
1864 }
1865 (void) CopyMagickString (pfx->token, pfx->pex, len+1);
1866
1867 (void) GetPixelInfo (pfx->image, &colour);
1868
1869 if (!QueryColorCompliance (pfx->token, AllCompliance, &colour, pfx->exception)) {
1870 (void) ThrowMagickException (
1871 pfx->exception, GetMagickModule(), OptionError,
1872 "QueryColorCompliance rejected", "'%s' at '%s'",
1873 pfx->token, SetShortExp(pfx));
1874 return -1;
1875 }
1876
1877 *v0 = QuantumScale*colour.red;
1878 *v1 = QuantumScale*colour.green;
1879 *v2 = QuantumScale*colour.blue;
1880
1881 return (ssize_t) len;
1882}
1883
1884static MagickBooleanType GetFunction (FxInfo * pfx, FunctionE fe)
1885{
1886 /* A function, so get open-parens, n args, close-parens
1887 */
1888 const char * funStr = Functions[fe-(int) FirstFunc].str;
1889 int nArgs = Functions[fe-(int) FirstFunc].number_args;
1890 char chLimit = ')';
1891 char expChLimit = ')';
1892 const char *strLimit = ",)";
1893 OperatorE pushOp = oOpenParen;
1894
1895 char * pExpStart;
1896
1897 size_t lenExp = 0;
1898
1899 int FndArgs = 0;
1900 int ndx0 = NULL_ADDRESS, ndx1 = NULL_ADDRESS, ndx2 = NULL_ADDRESS, ndx3 = NULL_ADDRESS;
1901
1902 MagickBooleanType coordQual = MagickFalse;
1903 PixelChannel chQual = NO_CHAN_QUAL;
1904 ImgAttrE iaQual = aNull;
1905
1906 pfx->pex += pfx->lenToken;
1907
1908 if (fe == fP) {
1909 char p = PeekChar (pfx);
1910 if (p=='{') {
1911 (void) ExpectChar (pfx, '{');
1912 pushOp = oOpenBrace;
1913 strLimit = ",}";
1914 chLimit = '}';
1915 expChLimit = '}';
1916 } else if (p=='[') {
1917 (void) ExpectChar (pfx, '[');
1918 pushOp = oOpenBracket;
1919 strLimit = ",]";
1920 chLimit = ']';
1921 expChLimit = ']';
1922 } else {
1923 nArgs = 0;
1924 chLimit = ']';
1925 expChLimit = ']';
1926 }
1927 } else if (fe == fU) {
1928 char p = PeekChar (pfx);
1929 if (p=='[') {
1930 (void) ExpectChar (pfx, '[');
1931 pushOp = oOpenBracket;
1932 strLimit = ",]";
1933 chLimit = ']';
1934 expChLimit = ']';
1935 } else {
1936 nArgs = 0;
1937 chLimit = ']';
1938 expChLimit = ']';
1939 }
1940 } else if (fe == fV || fe == fS) {
1941 nArgs = 0;
1942 pushOp = oOpenBracket;
1943 chLimit = ']';
1944 expChLimit = ']';
1945 } else {
1946 if (!ExpectChar (pfx, '(')) return MagickFalse;
1947 }
1948 if (!PushOperatorStack (pfx, (int) pushOp)) return MagickFalse;
1949
1950 pExpStart = pfx->pex;
1951 ndx0 = pfx->usedElements;
1952 if (fe==fDo) {
1953 (void) AddAddressingElement (pfx, rGoto, NULL_ADDRESS); /* address will be ndx1+1 */
1954 }
1955 if (fe==fEpoch) {
1956 fxFltType
1957 val,
1958 seconds;
1959 ssize_t
1960 lenOptArt = GetProperty (pfx, &val, &seconds);
1961 if (seconds == SECONDS_ERR) {
1962 /* Exception may not have been raised. */
1963 (void) ThrowMagickException (
1964 pfx->exception, GetMagickModule(), OptionError,
1965 "Function 'epoch' expected date property", "at '%s'",
1966 SetShortExp(pfx));
1967 return MagickFalse;
1968 }
1969 if (lenOptArt < 0) return MagickFalse;
1970 if (lenOptArt > 0) {
1971 (void) AddElement (pfx, seconds, oNull);
1972 pfx->pex += lenOptArt;
1973 if (!ExpectChar (pfx, ')')) return MagickFalse;
1974 if (!PopOprOpenParen (pfx, pushOp)) return MagickFalse;
1975 return MagickTrue;
1976 }
1977 }
1978
1979 while (nArgs > 0) {
1980 int FndOne = 0;
1981 if (TranslateStatementList (pfx, strLimit, &chLimit)) {
1982 FndOne = 1;
1983 } else {
1984 if (!*pfx->pex) {
1985 (void) ThrowMagickException (
1986 pfx->exception, GetMagickModule(), OptionError,
1987 "For function", "'%s' expected ')' at '%s'",
1988 funStr, SetShortExp(pfx));
1989 return MagickFalse;
1990 }
1991 /* Maybe don't break because other expressions may be not empty. */
1992 if (!chLimit) break;
1993 if (fe == fP || fe == fS|| fe == fIf) {
1994 (void) AddElement (pfx, (fxFltType) 0, oNull);
1995 FndOne = 1;
1996 }
1997 }
1998
1999 if (strchr (strLimit, chLimit)==NULL) {
2000 (void) ThrowMagickException (
2001 pfx->exception, GetMagickModule(), OptionError,
2002 "For function", "'%s' expected one of '%s' after expression but found '%c' at '%s'",
2003 funStr, strLimit, chLimit ? chLimit : ' ', SetShortExp(pfx));
2004 return MagickFalse;
2005 }
2006 if (FndOne) {
2007 FndArgs++;
2008 nArgs--;
2009 }
2010 switch (FndArgs) {
2011 case 1:
2012 if (ndx1 != NULL_ADDRESS) {
2013 (void) ThrowMagickException (
2014 pfx->exception, GetMagickModule(), OptionError,
2015 "For function", "'%s' required argument is missing at '%s'",
2016 funStr, SetShortExp(pfx));
2017 return MagickFalse;
2018 }
2019 ndx1 = pfx->usedElements;
2020 if (fe==fWhile || fe==fIf) {
2021 (void) AddAddressingElement (pfx, rIfZeroGoto, NULL_ADDRESS); /* address will be ndx2+1 */
2022 } else if (fe==fDo) {
2023 (void) AddAddressingElement (pfx, rIfZeroGoto, NULL_ADDRESS); /* address will be ndx2+1 */
2024 } else if (fe==fFor) {
2025 pfx->Elements[pfx->usedElements-1].do_push = MagickFalse;
2026 }
2027 break;
2028 case 2:
2029 if (ndx2 != NULL_ADDRESS) {
2030 (void) ThrowMagickException (
2031 pfx->exception, GetMagickModule(), OptionError,
2032 "For function", "'%s' required argument is missing at '%s'",
2033 funStr, SetShortExp(pfx));
2034 return MagickFalse;
2035 }
2036 ndx2 = pfx->usedElements;
2037 if (fe==fWhile) {
2038 pfx->Elements[pfx->usedElements-1].do_push = MagickFalse;
2039 (void) AddAddressingElement (pfx, rGotoChk, ndx0);
2040 } else if (fe==fDo) {
2041 pfx->Elements[pfx->usedElements-1].do_push = MagickFalse;
2042 (void) AddAddressingElement (pfx, rGotoChk, ndx0 + 1);
2043 } else if (fe==fFor) {
2044 (void) AddAddressingElement (pfx, rIfZeroGoto, NULL_ADDRESS); /* address will be ndx3 */
2045 pfx->Elements[pfx->usedElements-1].do_push = MagickTrue; /* we may need return from for() */
2046 (void) AddAddressingElement (pfx, rZerStk, NULL_ADDRESS);
2047 } else if (fe==fIf) {
2048 (void) AddAddressingElement (pfx, rGoto, NULL_ADDRESS); /* address will be ndx3 */
2049 }
2050 break;
2051 case 3:
2052 if (ndx3 != NULL_ADDRESS) {
2053 (void) ThrowMagickException (
2054 pfx->exception, GetMagickModule(), OptionError,
2055 "For function", "'%s' required argument is missing at '%s'",
2056 funStr, SetShortExp(pfx));
2057 return MagickFalse;
2058 }
2059 if (fe==fFor) {
2060 pfx->Elements[pfx->usedElements-1].do_push = MagickFalse;
2061 (void) AddAddressingElement (pfx, rGotoChk, ndx1);
2062 }
2063 ndx3 = pfx->usedElements;
2064 break;
2065 default:
2066 break;
2067 }
2068 if (chLimit == expChLimit) {
2069 lenExp = (size_t) (pfx->pex - pExpStart - 1);
2070 break;
2071 }
2072 } /* end while args of a function */
2073 if (chLimit && chLimit != expChLimit && chLimit != ',' ) {
2074 (void) ThrowMagickException (
2075 pfx->exception, GetMagickModule(), OptionError,
2076 "For function", "'%s' expected '%c', found '%c' at '%s'",
2077 funStr, expChLimit, chLimit ? chLimit : ' ', SetShortExp(pfx));
2078 return MagickFalse;
2079 }
2080
2081 if (fe == fP || fe == fS || fe == fU || fe == fChannel) {
2082 while (FndArgs < Functions[fe-(int) FirstFunc].number_args) {
2083 (void) AddElement (pfx, (fxFltType) 0, oNull);
2084 FndArgs++;
2085 }
2086 }
2087
2088 if (FndArgs > Functions[fe-(int) FirstFunc].number_args)
2089 {
2090 if (fe==fChannel) {
2091 (void) ThrowMagickException (
2092 pfx->exception, GetMagickModule(), OptionError,
2093 "For function", "'%s' expected up to %i arguments, found '%i' at '%s'",
2094 funStr, Functions[fe-(int) FirstFunc].number_args, FndArgs, SetShortExp(pfx));
2095 } else {
2096 (void) ThrowMagickException (
2097 pfx->exception, GetMagickModule(), OptionError,
2098 "For function", "'%s' expected %i arguments, found '%i' at '%s'",
2099 funStr, Functions[fe-(int) FirstFunc].number_args, FndArgs, SetShortExp(pfx));
2100 }
2101 return MagickFalse;
2102 }
2103 if (FndArgs < Functions[fe-(int) FirstFunc].number_args) {
2104 (void) ThrowMagickException (
2105 pfx->exception, GetMagickModule(), OptionError,
2106 "For function", "'%s' expected %i arguments, found too few (%i) at '%s'",
2107 funStr, Functions[fe-(int) FirstFunc].number_args, FndArgs, SetShortExp(pfx));
2108 return MagickFalse;
2109 }
2110 if (fe != fS && fe != fV && FndArgs == 0 && Functions[fe-(int) FirstFunc].number_args == 0) {
2111 /* This is for "rand()" and similar. */
2112 chLimit = expChLimit;
2113 if (!ExpectChar (pfx, ')')) return MagickFalse;
2114 }
2115
2116 if (chLimit != expChLimit) {
2117 (void) ThrowMagickException (
2118 pfx->exception, GetMagickModule(), OptionError,
2119 "For function", "'%s', arguments don't end with '%c' at '%s'",
2120 funStr, expChLimit, SetShortExp(pfx));
2121 return MagickFalse;
2122 }
2123 if (!PopOprOpenParen (pfx, pushOp)) {
2124 (void) ThrowMagickException (
2125 pfx->exception, GetMagickModule(), OptionError,
2126 "Bug: For function", "'%s' tos not '%s' at '%s'",
2127 funStr, Operators[pushOp].str, SetShortExp(pfx));
2128 return MagickFalse;
2129 }
2130
2131 if (IsQualifier (pfx)) {
2132
2133 if (fe == fU || fe == fV || fe == fS) {
2134
2135 coordQual = (GetCoordQualifier (pfx, (int) fe) == 1) ? MagickTrue : MagickFalse;
2136
2137 if (coordQual) {
2138
2139 /* Remove last element, which should be fP */
2140 ElementT * pel = &pfx->Elements[pfx->usedElements-1];
2141 if (pel->operator_index != fP) {
2142 (void) ThrowMagickException (
2143 pfx->exception, GetMagickModule(), OptionError,
2144 "Bug: For function", "'%s' last element not 'p' at '%s'",
2145 funStr, SetShortExp(pfx));
2146 return MagickFalse;
2147 }
2148 chQual = pel->channel_qual;
2149 expChLimit = (pel->is_relative) ? ']' : '}';
2150 pfx->usedElements--;
2151 if (fe == fU) fe = fUP;
2152 else if (fe == fV) fe = fVP;
2153 else if (fe == fS) fe = fSP;
2154 funStr = Functions[fe-(int) FirstFunc].str;
2155 }
2156 }
2157
2158 if ( chQual == NO_CHAN_QUAL &&
2159 (fe == fP || fe == fS || fe == fSP || fe == fU || fe == fUP || fe == fV || fe == fVP)
2160 )
2161 {
2162 chQual = GetChannelQualifier (pfx, (int) fe);
2163 }
2164
2165 if (chQual == NO_CHAN_QUAL && (fe == fU || fe == fV || fe == fS)) {
2166 /* Note: we don't allow "p.mean" etc. */
2167 iaQual = GetImgAttrQualifier (pfx, (int) fe);
2168 }
2169 if (IsQualifier (pfx) && chQual == NO_CHAN_QUAL && iaQual != aNull) {
2170 chQual = GetChannelQualifier (pfx, (int) fe);
2171 }
2172 if (coordQual && iaQual != aNull) {
2173 (void) ThrowMagickException (
2174 pfx->exception, GetMagickModule(), OptionError,
2175 "For function", "'%s', can't have qualifiers 'p' and image attribute '%s' at '%s'",
2176 funStr, pfx->token, SetShortExp(pfx));
2177 return MagickFalse;
2178 }
2179 if (!coordQual && chQual == NO_CHAN_QUAL && iaQual == aNull) {
2180 (void) ThrowMagickException (
2181 pfx->exception, GetMagickModule(), OptionError,
2182 "For function", "'%s', bad qualifier '%s' at '%s'",
2183 funStr, pfx->token, SetShortExp(pfx));
2184 return MagickFalse;
2185 }
2186 if (!coordQual && chQual == CompositePixelChannel && iaQual == aNull) {
2187 (void) ThrowMagickException (
2188 pfx->exception, GetMagickModule(), OptionError,
2189 "For function", "'%s', bad composite qualifier '%s' at '%s'",
2190 funStr, pfx->token, SetShortExp(pfx));
2191 return MagickFalse;
2192 }
2193
2194 if (chQual == HUE_CHANNEL || chQual == SAT_CHANNEL || chQual == LIGHT_CHANNEL) {
2195 pfx->NeedHsl = MagickTrue;
2196
2197 if (iaQual >= FirstImgAttr && iaQual < aNull) {
2198 (void) ThrowMagickException (
2199 pfx->exception, GetMagickModule(), OptionError,
2200 "Can't have image attribute with HLS qualifier at", "'%s'",
2201 SetShortExp(pfx));
2202 return MagickFalse;
2203 }
2204 }
2205 }
2206
2207 if (iaQual != aNull && chQual != NO_CHAN_QUAL) {
2208 if (ImgAttrs[iaQual-(int) FirstImgAttr].need_stats == MagickFalse) {
2209 (void) ThrowMagickException (
2210 pfx->exception, GetMagickModule(), OptionError,
2211 "Can't have image attribute ", "'%s' with channel qualifier '%s' at '%s'",
2212 ImgAttrs[iaQual-(int) FirstImgAttr].str,
2213 pfx->token, SetShortExp(pfx));
2214 return MagickFalse;
2215 } else {
2216 if (ChanIsVirtual (chQual)) {
2217 (void) ThrowMagickException (
2218 pfx->exception, GetMagickModule(), OptionError,
2219 "Can't have statistical image attribute ", "'%s' with virtual channel qualifier '%s' at '%s'",
2220 ImgAttrs[iaQual-(int) FirstImgAttr].str,
2221 pfx->token, SetShortExp(pfx));
2222 return MagickFalse;
2223 }
2224 }
2225 }
2226
2227 if (fe==fWhile) {
2228 pfx->Elements[ndx1].element_index = ndx2+1;
2229 } else if (fe==fDo) {
2230 pfx->Elements[ndx0].element_index = ndx1+1;
2231 pfx->Elements[ndx1].element_index = ndx2+1;
2232 } else if (fe==fFor) {
2233 pfx->Elements[ndx2].element_index = ndx3;
2234 } else if (fe==fIf) {
2235 pfx->Elements[ndx1].element_index = ndx2 + 1;
2236 pfx->Elements[ndx2].element_index = ndx3;
2237 } else {
2238 if (fe == fU && iaQual == aNull) {
2239 ElementT * pel = &pfx->Elements[pfx->usedElements-1];
2240 if (pel->type == etConstant && pel->val == 0.0) {
2241 pfx->usedElements--;
2242 fe = fU0;
2243 }
2244 }
2245 (void) AddElement (pfx, (fxFltType) 0, (int) fe);
2246 if (fe == fP || fe == fU || fe == fU0 || fe == fUP ||
2247 fe == fV || fe == fVP || fe == fS || fe == fSP)
2248 {
2249 ElementT * pel = &pfx->Elements[pfx->usedElements-1];
2250 pel->is_relative = (expChLimit == ']' ? MagickTrue : MagickFalse);
2251 if (chQual >= 0) pel->channel_qual = chQual;
2252 if (iaQual != aNull && (fe == fU || fe == fV || fe == fS)) {
2253 /* Note: we don't allow "p[2,3].mean" or "p.mean" etc. */
2254 pel->img_attr_qual = iaQual;
2255 }
2256 }
2257 }
2258
2259 if (pExpStart && lenExp) {
2260 ElementT * pel = &pfx->Elements[pfx->usedElements-1];
2261 pel->exp_start = pExpStart;
2262 pel->exp_len = lenExp;
2263 }
2264
2265 if (fe == fDebug)
2266 pfx->ContainsDebug = MagickTrue;
2267
2268 return MagickTrue;
2269}
2270
2271static MagickBooleanType IsStealth (int op)
2272{
2273 return (op == fU0 || op == fUP || op == fSP || op == fVP ||
2274 (op >= FirstCont && op <= rNull) ? MagickTrue : MagickFalse
2275 );
2276}
2277
2278static MagickBooleanType GetOperand (
2279 FxInfo * pfx, MagickBooleanType * UserSymbol, MagickBooleanType * NewUserSymbol, int * UserSymNdx,
2280 MagickBooleanType * needPopAll)
2281{
2282
2283 *NewUserSymbol = *UserSymbol = MagickFalse;
2284 *UserSymNdx = NULL_ADDRESS;
2285
2286 SkipSpaces (pfx);
2287 if (!*pfx->pex) return MagickFalse;
2288 (void) GetToken (pfx);
2289
2290 if (pfx->lenToken==0) {
2291
2292 /* Try '(' or unary prefix
2293 */
2294 OperatorE op = GetLeadingOp (pfx);
2295 if (op==oOpenParen) {
2296 char chLimit = '\0';
2297 if (!PushOperatorStack (pfx, (int) op)) return MagickFalse;
2298 pfx->pex++;
2299 if (!TranslateExpression (pfx, ")", &chLimit, needPopAll)) {
2300 (void) ThrowMagickException (
2301 pfx->exception, GetMagickModule(), OptionError,
2302 "Empty expression in parentheses at", "'%s'",
2303 SetShortExp(pfx));
2304 return MagickFalse;
2305 }
2306 if (chLimit != ')') {
2307 (void) ThrowMagickException (
2308 pfx->exception, GetMagickModule(), OptionError,
2309 "'(' but no ')' at", "'%s'",
2310 SetShortExp(pfx));
2311 return MagickFalse;
2312 }
2313 /* Top of opr stack should be '('. */
2314 if (!PopOprOpenParen (pfx, oOpenParen)) {
2315 (void) ThrowMagickException (
2316 pfx->exception, GetMagickModule(), OptionError,
2317 "Bug: tos not '(' at", "'%s'",
2318 SetShortExp(pfx));
2319 return MagickFalse;
2320 }
2321 return MagickTrue;
2322 } else if (OprIsUnaryPrefix (op)) {
2323 MagickBooleanType operand_ok;
2324 if (!PushOperatorStack (pfx, (int) op)) return MagickFalse;
2325 pfx->pex++;
2326 SkipSpaces (pfx);
2327 if (!*pfx->pex) return MagickFalse;
2328 if (pfx->teDepth >= MagickMaxRecursionDepth) {
2329 (void) ThrowMagickException (
2330 pfx->exception, GetMagickModule(), OptionError,
2331 "Expression too deeply nested", "(depth %i exceeds limit %i)",
2332 pfx->teDepth, MagickMaxRecursionDepth);
2333 return MagickFalse;
2334 }
2335 pfx->teDepth++;
2336 operand_ok=GetOperand (pfx, UserSymbol, NewUserSymbol, UserSymNdx, needPopAll);
2337 pfx->teDepth--;
2338 if (!operand_ok) {
2339 (void) ThrowMagickException (
2340 pfx->exception, GetMagickModule(), OptionError,
2341 "After unary, bad operand at", "'%s'",
2342 SetShortExp(pfx));
2343 return MagickFalse;
2344 }
2345
2346 if (*NewUserSymbol) {
2347 (void) ThrowMagickException (
2348 pfx->exception, GetMagickModule(), OptionError,
2349 "After unary, NewUserSymbol at", "'%s'",
2350 SetShortExp(pfx));
2351 return MagickFalse;
2352 }
2353
2354 if (*UserSymbol) {
2355 (void) AddAddressingElement (pfx, rCopyFrom, *UserSymNdx);
2356 *UserSymNdx = NULL_ADDRESS;
2357
2358 *UserSymbol = MagickFalse;
2359 *NewUserSymbol = MagickFalse;
2360 }
2361
2362 (void) GetToken (pfx);
2363 return MagickTrue;
2364 } else if (*pfx->pex == '#') {
2365 fxFltType v0=0, v1=0, v2=0;
2366 ssize_t lenToken = GetHexColour (pfx, &v0, &v1, &v2);
2367 if (lenToken < 0) {
2368 (void) ThrowMagickException (
2369 pfx->exception, GetMagickModule(), OptionError,
2370 "Bad hex number at", "'%s'",
2371 SetShortExp(pfx));
2372 return MagickFalse;
2373 } else if (lenToken > 0) {
2374 (void) AddColourElement (pfx, v0, v1, v2);
2375 pfx->pex+=lenToken;
2376 }
2377 return MagickTrue;
2378 }
2379
2380 /* Try a constant number.
2381 */
2382 {
2383 char * tailptr;
2384 ssize_t lenOptArt;
2385 fxFltType val = strtold (pfx->pex, &tailptr);
2386 if (pfx->pex != tailptr) {
2387 pfx->pex = tailptr;
2388 if (*tailptr) {
2389 /* Could have "prefix" K, Ki, M etc.
2390 See https://en.wikipedia.org/wiki/Metric_prefix
2391 and https://en.wikipedia.org/wiki/Binary_prefix
2392 */
2393 double Pow = 0.0;
2394 const char Prefixes[] = "yzafpnum.kMGTPEZY";
2395 const char * pSi = strchr (Prefixes, *tailptr);
2396 if (pSi && *pSi != '.') Pow = (double) ((pSi - Prefixes) * 3 - 24);
2397 else if (*tailptr == 'c') Pow = -2;
2398 else if (*tailptr == 'h') Pow = 2;
2399 else if (*tailptr == 'k') Pow = 3;
2400 if (Pow != 0.0) {
2401 if (*(++pfx->pex) == 'i') {
2402 val *= pow (2.0, Pow/0.3);
2403 pfx->pex++;
2404 } else {
2405 val *= pow (10.0, Pow);
2406 }
2407 }
2408 }
2409 (void) AddElement (pfx, val, oNull);
2410 return MagickTrue;
2411 }
2412
2413 val = (fxFltType) 0;
2414 lenOptArt = GetProperty (pfx, &val, NULL);
2415 if (lenOptArt < 0) return MagickFalse;
2416 if (lenOptArt > 0) {
2417 (void) AddElement (pfx, val, oNull);
2418 pfx->pex += lenOptArt;
2419 return MagickTrue;
2420 }
2421 }
2422
2423 } /* end of lenToken==0 */
2424
2425 if (pfx->lenToken > 0) {
2426 /* Try a constant
2427 */
2428 {
2429 ConstantE ce;
2430 for (ce = (ConstantE)0; ce < cNull; ce=(ConstantE) (ce+1)) {
2431 const char * ceStr = Constants[ce].str;
2432 if (LocaleCompare (ceStr, pfx->token)==0) {
2433 break;
2434 }
2435 }
2436
2437 if (ce != cNull) {
2438 (void) AddElement (pfx, Constants[ce].val, oNull);
2439 pfx->pex += pfx->lenToken;
2440 return MagickTrue;
2441 }
2442 }
2443
2444 /* Try a function
2445 */
2446 {
2447 FunctionE fe;
2448 for (fe = FirstFunc; fe < fNull; fe=(FunctionE) (fe+1)) {
2449 const char * feStr = Functions[fe-(int) FirstFunc].str;
2450 if (LocaleCompare (feStr, pfx->token)==0) {
2451 break;
2452 }
2453 }
2454
2455 if (fe == fV && pfx->ImgListLen < 2) {
2456 (void) ThrowMagickException (
2457 pfx->exception, GetMagickModule(), OptionError,
2458 "Symbol 'v' but fewer than two images at", "'%s'",
2459 SetShortExp(pfx));
2460 return MagickFalse;
2461 }
2462
2463 if (IsStealth ((int) fe)) {
2464 (void) ThrowMagickException (
2465 pfx->exception, GetMagickModule(), OptionError,
2466 "Function", "'%s' not permitted at '%s'",
2467 pfx->token, SetShortExp(pfx));
2468 return MagickFalse;
2469 }
2470
2471 if (fe == fDo || fe == fFor || fe == fIf || fe == fWhile) {
2472 *needPopAll = MagickTrue;
2473 }
2474
2475 if (fe != fNull) return (GetFunction (pfx, fe));
2476 }
2477
2478 /* Try image attribute
2479 */
2480 {
2481 ImgAttrE ia = GetImgAttrToken (pfx);
2482 if (ia != aNull) {
2483 fxFltType val = 0;
2484 (void) AddElement (pfx, val, (int) ia);
2485
2486 if (ImgAttrs[ia-(int) FirstImgAttr].need_stats != MagickFalse) {
2487 if (IsQualifier (pfx)) {
2488 PixelChannel chQual = GetChannelQualifier (pfx, (int) ia);
2489 ElementT * pel;
2490 if (chQual == NO_CHAN_QUAL) {
2491 (void) ThrowMagickException (
2492 pfx->exception, GetMagickModule(), OptionError,
2493 "Bad channel qualifier at", "'%s'",
2494 SetShortExp(pfx));
2495 return MagickFalse;
2496 }
2497 /* Adjust the element */
2498 pel = &pfx->Elements[pfx->usedElements-1];
2499 pel->channel_qual = chQual;
2500 }
2501 }
2502 return MagickTrue;
2503 }
2504 }
2505
2506 /* Try symbol
2507 */
2508 {
2509 SymbolE se;
2510 for (se = FirstSym; se < sNull; se=(SymbolE) (se+1)) {
2511 const char * seStr = Symbols[se-(int) FirstSym].str;
2512 if (LocaleCompare (seStr, pfx->token)==0) {
2513 break;
2514 }
2515 }
2516 if (se != sNull) {
2517 fxFltType val = 0;
2518 (void) AddElement (pfx, val, (int) se);
2519 pfx->pex += pfx->lenToken;
2520
2521 if (se==sHue || se==sSaturation || se==sLightness) pfx->NeedHsl = MagickTrue;
2522 return MagickTrue;
2523 }
2524 }
2525
2526 /* Try constant colour.
2527 */
2528 {
2529 fxFltType v0, v1, v2;
2530 ssize_t ColLen = GetConstantColour (pfx, &v0, &v1, &v2);
2531 if (ColLen < 0) return MagickFalse;
2532 if (ColLen > 0) {
2533 (void) AddColourElement (pfx, v0, v1, v2);
2534 pfx->pex+=ColLen;
2535 return MagickTrue;
2536 }
2537 }
2538
2539 /* Try image artifact.
2540 */
2541 {
2542 const char *artifact;
2543 artifact = GetImageArtifact (pfx->image, pfx->token);
2544 if (artifact != (const char *) NULL) {
2545 char * tailptr;
2546 fxFltType val = strtold (artifact, &tailptr);
2547 if (artifact == tailptr) {
2548 (void) ThrowMagickException (
2549 pfx->exception, GetMagickModule(), OptionError,
2550 "Artifact", "'%s' has value '%s', not a number, at '%s'",
2551 pfx->token, artifact, SetShortExp(pfx));
2552 return MagickFalse;
2553 }
2554 (void) AddElement (pfx, val, oNull);
2555 pfx->pex+=pfx->lenToken;
2556 return MagickTrue;
2557 }
2558 }
2559
2560 /* Try user symbols. If it is, don't AddElement yet.
2561 */
2562 if (TokenMaybeUserSymbol (pfx)) {
2563 *UserSymbol = MagickTrue;
2564 *UserSymNdx = FindUserSymbol (pfx, pfx->token);
2565 if (*UserSymNdx == NULL_ADDRESS) {
2566 *UserSymNdx = AddUserSymbol (pfx, pfx->pex, pfx->lenToken);
2567 if (*UserSymNdx < 0)
2568 return MagickFalse;
2569 *NewUserSymbol = MagickTrue;
2570 }
2571 pfx->pex += pfx->lenToken;
2572
2573 return MagickTrue;
2574 }
2575 }
2576
2577 (void) ThrowMagickException (
2578 pfx->exception, GetMagickModule(), OptionError,
2579 "Expected operand at", "'%s'",
2580 SetShortExp(pfx));
2581
2582 return MagickFalse;
2583}
2584
2585static inline MagickBooleanType IsRealOperator (OperatorE op)
2586{
2587 return (op < oOpenParen || op > oCloseBrace) ? MagickTrue : MagickFalse;
2588}
2589
2590static inline MagickBooleanType ProcessTernaryOpr (FxInfo * pfx, TernaryT * ptern)
2591/* Ternary operator "... ? ... : ..."
2592 returns false iff we have exception
2593*/
2594{
2595 if (pfx->usedOprStack == 0)
2596 return MagickFalse;
2597 if (pfx->OperatorStack[pfx->usedOprStack-1] == oQuery) {
2598 if (ptern->addr_query != NULL_ADDRESS) {
2599 (void) ThrowMagickException (
2600 pfx->exception, GetMagickModule(), OptionError,
2601 "Already have '?' in sub-expression at", "'%s'",
2602 SetShortExp(pfx));
2603 return MagickFalse;
2604 }
2605 if (ptern->addr_colon != NULL_ADDRESS) {
2606 (void) ThrowMagickException (
2607 pfx->exception, GetMagickModule(), OptionError,
2608 "Already have ':' in sub-expression at", "'%s'",
2609 SetShortExp(pfx));
2610 return MagickFalse;
2611 }
2612 pfx->usedOprStack--;
2613 ptern->addr_query = pfx->usedElements;
2614 (void) AddAddressingElement (pfx, rIfZeroGoto, NULL_ADDRESS);
2615 /* address will be one after the Colon address. */
2616 }
2617 else if (pfx->OperatorStack[pfx->usedOprStack-1] == oColon) {
2618 if (ptern->addr_query == NULL_ADDRESS) {
2619 (void) ThrowMagickException (
2620 pfx->exception, GetMagickModule(), OptionError,
2621 "Need '?' in sub-expression at", "'%s'",
2622 SetShortExp(pfx));
2623 return MagickFalse;
2624 }
2625 if (ptern->addr_colon != NULL_ADDRESS) {
2626 (void) ThrowMagickException (
2627 pfx->exception, GetMagickModule(), OptionError,
2628 "Already have ':' in sub-expression at", "'%s'",
2629 SetShortExp(pfx));
2630 return MagickFalse;
2631 }
2632 pfx->usedOprStack--;
2633 ptern->addr_colon = pfx->usedElements;
2634 pfx->Elements[pfx->usedElements-1].do_push = MagickTrue;
2635 (void) AddAddressingElement (pfx, rGoto, NULL_ADDRESS);
2636 /* address will be after the subexpression */
2637 }
2638 return MagickTrue;
2639}
2640
2641static MagickBooleanType GetOperator (
2642 FxInfo * pfx,
2643 MagickBooleanType * Assign, MagickBooleanType * Update, MagickBooleanType * IncrDecr)
2644{
2645 OperatorE op;
2646 size_t len = 0;
2647 MagickBooleanType DoneIt = MagickFalse;
2648 SkipSpaces (pfx);
2649 for (op = (OperatorE)0; op != oNull; op=(OperatorE) (op+1)) {
2650 const char * opStr = Operators[op].str;
2651 len = strlen(opStr);
2652 if (LocaleNCompare (opStr, pfx->pex, len)==0) {
2653 break;
2654 }
2655 }
2656
2657 if (!IsRealOperator (op)) {
2658 (void) ThrowMagickException (
2659 pfx->exception, GetMagickModule(), OptionError,
2660 "Not a real operator at", "'%s'",
2661 SetShortExp(pfx));
2662 return MagickFalse;
2663 }
2664
2665 if (op==oNull) {
2666 (void) ThrowMagickException (
2667 pfx->exception, GetMagickModule(), OptionError,
2668 "Expected operator at", "'%s'",
2669 SetShortExp(pfx));
2670 return MagickFalse;
2671 }
2672
2673 *Assign = (op==oAssign) ? MagickTrue : MagickFalse;
2674 *Update = OprInPlace ((int) op);
2675 *IncrDecr = (op == oPlusPlus || op == oSubSub) ? MagickTrue : MagickFalse;
2676
2677 /* while top of OperatorStack is not empty and is not open-parens or assign,
2678 and top of OperatorStack is higher precedence than new op,
2679 then move top of OperatorStack to Element list.
2680 */
2681
2682 while (pfx->usedOprStack > 0) {
2683 OperatorE top = pfx->OperatorStack[pfx->usedOprStack-1];
2684 int precTop, precNew;
2685 if (top == oOpenParen || top == oAssign || OprInPlace ((int) top)) break;
2686 precTop = Operators[top].precedence;
2687 precNew = Operators[op].precedence;
2688 /* Assume left associativity.
2689 If right assoc, this would be "<=".
2690 */
2691 if (precTop < precNew) break;
2692 (void) AddElement (pfx, (fxFltType) 0, (int) top);
2693 pfx->usedOprStack--;
2694 }
2695
2696 /* If new op is close paren, and stack top is open paren,
2697 remove stack top.
2698 */
2699 if (op==oCloseParen) {
2700 if (pfx->usedOprStack == 0) {
2701 (void) ThrowMagickException (
2702 pfx->exception, GetMagickModule(), OptionError,
2703 "Found ')' but nothing on stack at", "'%s'",
2704 SetShortExp(pfx));
2705 return MagickFalse;
2706 }
2707
2708 if (pfx->OperatorStack[pfx->usedOprStack-1] != oOpenParen) {
2709 (void) ThrowMagickException (
2710 pfx->exception, GetMagickModule(), OptionError,
2711 "Found ')' but no '(' on stack at", "'%s'",
2712 SetShortExp(pfx));
2713 return MagickFalse;
2714 }
2715 pfx->usedOprStack--;
2716 DoneIt = MagickTrue;
2717 }
2718
2719 if (!DoneIt) {
2720 if (!PushOperatorStack (pfx, (int) op)) return MagickFalse;
2721 }
2722
2723 pfx->pex += len;
2724
2725 return MagickTrue;
2726}
2727
2728static MagickBooleanType ResolveTernaryAddresses (FxInfo * pfx, TernaryT * ptern)
2729{
2730 if (ptern->addr_query == NULL_ADDRESS && ptern->addr_colon == NULL_ADDRESS)
2731 return MagickTrue;
2732
2733 if (ptern->addr_query != NULL_ADDRESS && ptern->addr_colon != NULL_ADDRESS) {
2734 pfx->Elements[ptern->addr_query].element_index = ptern->addr_colon + 1;
2735 pfx->Elements[ptern->addr_colon].element_index = pfx->usedElements;
2736 ptern->addr_query = NULL_ADDRESS;
2737 ptern->addr_colon = NULL_ADDRESS;
2738 } else if (ptern->addr_query != NULL_ADDRESS) {
2739 (void) ThrowMagickException (
2740 pfx->exception, GetMagickModule(), OptionError,
2741 "'?' with no corresponding ':'", "'%s' at '%s'",
2742 pfx->token, SetShortExp(pfx));
2743 return MagickFalse;
2744 } else if (ptern->addr_colon != NULL_ADDRESS) {
2745 (void) ThrowMagickException (
2746 pfx->exception, GetMagickModule(), OptionError,
2747 "':' with no corresponding '?'", "'%s' at '%s'",
2748 pfx->token, SetShortExp(pfx));
2749 return MagickFalse;
2750 }
2751 return MagickTrue;
2752}
2753
2754static MagickBooleanType TranslateExpression (
2755 FxInfo * pfx, const char * strLimit, char * chLimit, MagickBooleanType * needPopAll)
2756{
2757 /* There should be only one New per expression (oAssign), but can be many Old.
2758 */
2759 MagickBooleanType UserSymbol, NewUserSymbol;
2760 int UserSymNdx0, UserSymNdx1;
2761
2762 MagickBooleanType
2763 Assign = MagickFalse,
2764 Update = MagickFalse,
2765 IncrDecr = MagickFalse;
2766
2767 int StartEleNdx;
2768
2769 TernaryT ternary;
2770 ternary.addr_query = NULL_ADDRESS;
2771 ternary.addr_colon = NULL_ADDRESS;
2772
2773 if (pfx->teDepth >= MagickMaxRecursionDepth) {
2774 (void) ThrowMagickException(pfx->exception, GetMagickModule(), OptionError,
2775 "Expression too deeply nested", "(depth %i exceeds limit %i)",
2776 pfx->teDepth, MagickMaxRecursionDepth);
2777 return MagickFalse;
2778 }
2779
2780 pfx->teDepth++;
2781
2782 *chLimit = '\0';
2783
2784 StartEleNdx = pfx->usedElements-1;
2785 if (StartEleNdx < 0) StartEleNdx = 0;
2786
2787 SkipSpaces (pfx);
2788
2789 if (!*pfx->pex) {
2790 pfx->teDepth--;
2791 return MagickFalse;
2792 }
2793
2794 if (strchr(strLimit,*pfx->pex)!=NULL) {
2795 *chLimit = *pfx->pex;
2796 pfx->pex++;
2797 pfx->teDepth--;
2798
2799 return MagickFalse;
2800 }
2801
2802 if (!GetOperand (pfx, &UserSymbol, &NewUserSymbol, &UserSymNdx0, needPopAll)) return MagickFalse;
2803 SkipSpaces (pfx);
2804
2805 /* Loop through Operator, Operand, Operator, Operand, ...
2806 */
2807 while (*pfx->pex && (!*strLimit || (strchr(strLimit,*pfx->pex)==NULL))) {
2808 if (!GetOperator (pfx, &Assign, &Update, &IncrDecr)) return MagickFalse;
2809 SkipSpaces (pfx);
2810 if (NewUserSymbol && !Assign) {
2811 (void) ThrowMagickException (
2812 pfx->exception, GetMagickModule(), OptionError,
2813 "Expected assignment after new UserSymbol", "'%s' at '%s'",
2814 pfx->token, SetShortExp(pfx));
2815 return MagickFalse;
2816 }
2817 if (!UserSymbol && Assign) {
2818 (void) ThrowMagickException (
2819 pfx->exception, GetMagickModule(), OptionError,
2820 "Attempted assignment to non-UserSymbol", "'%s' at '%s'",
2821 pfx->token, SetShortExp(pfx));
2822 return MagickFalse;
2823 }
2824 if (!UserSymbol && Update) {
2825 (void) ThrowMagickException (
2826 pfx->exception, GetMagickModule(), OptionError,
2827 "Attempted update to non-UserSymbol", "'%s' at '%s'",
2828 pfx->token, SetShortExp(pfx));
2829 return MagickFalse;
2830 }
2831 if (UserSymbol && (Assign || Update) && !IncrDecr) {
2832
2833 if (!TranslateExpression (pfx, strLimit, chLimit, needPopAll)) return MagickFalse;
2834 if (!*pfx->pex) break;
2835 if (!*strLimit) break;
2836 if (strchr(strLimit,*chLimit)!=NULL) break;
2837 }
2838 if (UserSymbol && !Assign && !Update && UserSymNdx0 != NULL_ADDRESS) {
2839 ElementT * pel;
2840 (void) AddAddressingElement (pfx, rCopyFrom, UserSymNdx0);
2841 UserSymNdx0 = NULL_ADDRESS;
2842 pel = &pfx->Elements[pfx->usedElements-1];
2843 pel->do_push = MagickTrue;
2844 }
2845
2846 if (UserSymbol) {
2847 while (TopOprIsUnaryPrefix (pfx)) {
2848 OperatorE op = pfx->OperatorStack[pfx->usedOprStack-1];
2849 (void) AddElement (pfx, (fxFltType) 0, (int) op);
2850 pfx->usedOprStack--;
2851 }
2852 }
2853
2854 if (!ProcessTernaryOpr (pfx, &ternary)) return MagickFalse;
2855
2856 if (ternary.addr_colon != NULL_ADDRESS) {
2857 if (!TranslateExpression (pfx, ",);", chLimit, needPopAll)) return MagickFalse;
2858 break;
2859 }
2860
2861 UserSymbol = NewUserSymbol = MagickFalse;
2862
2863 if ( (!*pfx->pex) || (*strLimit && (strchr(strLimit,*pfx->pex)!=NULL) ) )
2864 {
2865 if (IncrDecr) break;
2866
2867 (void) ThrowMagickException (
2868 pfx->exception, GetMagickModule(), OptionError,
2869 "Expected operand after operator", "at '%s'",
2870 SetShortExp(pfx));
2871 return MagickFalse;
2872 }
2873
2874 if (IncrDecr) {
2875 (void) ThrowMagickException (
2876 pfx->exception, GetMagickModule(), OptionError,
2877 "'++' and '--' must be the final operators in an expression at", "'%s'",
2878 SetShortExp(pfx));
2879 return MagickFalse;
2880 }
2881
2882 if (!GetOperand (pfx, &UserSymbol, &NewUserSymbol, &UserSymNdx1, needPopAll)) {
2883 (void) ThrowMagickException (
2884 pfx->exception, GetMagickModule(), OptionError,
2885 "Expected operand at", "'%s'",
2886 SetShortExp(pfx));
2887 return MagickFalse;
2888 }
2889 SkipSpaces (pfx);
2890 if (NewUserSymbol && !Assign) {
2891 (void) ThrowMagickException (
2892 pfx->exception, GetMagickModule(), OptionError,
2893 "NewUserSymbol", "'%s' after non-assignment operator at '%s'",
2894 pfx->token, SetShortExp(pfx));
2895 return MagickFalse;
2896 }
2897 if (UserSymbol && !NewUserSymbol) {
2898 (void) AddAddressingElement (pfx, rCopyFrom, UserSymNdx1);
2899 UserSymNdx1 = NULL_ADDRESS;
2900 }
2901 UserSymNdx0 = UserSymNdx1;
2902 }
2903
2904 if (UserSymbol && !Assign && !Update && UserSymNdx0 != NULL_ADDRESS) {
2905 ElementT * pel;
2906 if (NewUserSymbol) {
2907 (void) ThrowMagickException (
2908 pfx->exception, GetMagickModule(), OptionError,
2909 "NewUserSymbol", "'%s' needs assignment operator at '%s'",
2910 pfx->token, SetShortExp(pfx));
2911 return MagickFalse;
2912 }
2913 (void) AddAddressingElement (pfx, rCopyFrom, UserSymNdx0);
2914 pel = &pfx->Elements[pfx->usedElements-1];
2915 pel->do_push = MagickTrue;
2916 }
2917
2918 if (*pfx->pex && !*chLimit && (strchr(strLimit,*pfx->pex)!=NULL)) {
2919 *chLimit = *pfx->pex;
2920 pfx->pex++;
2921 }
2922 while (pfx->usedOprStack) {
2923 OperatorE op = pfx->OperatorStack[pfx->usedOprStack-1];
2924 if (op == oOpenParen || op == oOpenBracket || op == oOpenBrace) {
2925 break;
2926 }
2927 if ( (op==oAssign && !Assign) || (OprInPlace((int) op) && !Update) ) {
2928 break;
2929 }
2930 pfx->usedOprStack--;
2931 (void) AddElement (pfx, (fxFltType) 0, (int) op);
2932 if (op == oAssign) {
2933 if (UserSymNdx0 < 0) {
2934 (void) ThrowMagickException (
2935 pfx->exception, GetMagickModule(), OptionError,
2936 "Assignment to unknown user symbol at", "'%s'",
2937 SetShortExp(pfx));
2938 return MagickFalse;
2939 }
2940 /* Adjust last element, by deletion and add.
2941 */
2942 pfx->usedElements--;
2943 (void) AddAddressingElement (pfx, rCopyTo, UserSymNdx0);
2944 break;
2945 } else if (OprInPlace ((int) op)) {
2946 if (UserSymNdx0 < 0) {
2947 (void) ThrowMagickException (
2948 pfx->exception, GetMagickModule(), OptionError,
2949 "Operator-in-place to unknown user symbol at", "'%s'",
2950 SetShortExp(pfx));
2951 return MagickFalse;
2952 }
2953 /* Modify latest element.
2954 */
2955 pfx->Elements[pfx->usedElements-1].element_index = UserSymNdx0;
2956 break;
2957 }
2958 }
2959
2960 if (ternary.addr_query != NULL_ADDRESS) *needPopAll = MagickTrue;
2961
2962 (void) ResolveTernaryAddresses (pfx, &ternary);
2963
2964 pfx->teDepth--;
2965
2966 if (!pfx->teDepth && *needPopAll) {
2967 (void) AddAddressingElement (pfx, rZerStk, NULL_ADDRESS);
2968 *needPopAll = MagickFalse;
2969 }
2970
2971 if (pfx->exception->severity >= ErrorException)
2972 return MagickFalse;
2973
2974 return MagickTrue;
2975}
2976
2977
2978static MagickBooleanType TranslateStatement (FxInfo * pfx, char * strLimit, char * chLimit)
2979{
2980 MagickBooleanType NeedPopAll = MagickFalse;
2981
2982 SkipSpaces (pfx);
2983
2984 if (!*pfx->pex) return MagickFalse;
2985
2986 if (!TranslateExpression (pfx, strLimit, chLimit, &NeedPopAll)) {
2987 return MagickFalse;
2988 }
2989 if (pfx->usedElements && *chLimit==';') {
2990 /* FIXME: not necessarily the last element,
2991 but the last _executed_ element, eg "goto" in a "for()".,
2992 Pending a fix, we will use rZerStk.
2993 */
2994 ElementT * pel = &pfx->Elements[pfx->usedElements-1];
2995 if (pel->do_push) pel->do_push = MagickFalse;
2996 }
2997
2998 return MagickTrue;
2999}
3000
3001static MagickBooleanType TranslateStatementList (FxInfo * pfx, const char * strLimit, char * chLimit)
3002{
3003#define MAX_SLIMIT 10
3004 char sLimits[MAX_SLIMIT];
3005 SkipSpaces (pfx);
3006
3007 if (!*pfx->pex) return MagickFalse;
3008 (void) CopyMagickString (sLimits, strLimit, MAX_SLIMIT-1);
3009
3010 if (strchr(strLimit,';')==NULL)
3011 (void) ConcatenateMagickString (sLimits, ";", MAX_SLIMIT);
3012
3013 for (;;) {
3014 if (!TranslateStatement (pfx, sLimits, chLimit)) return MagickFalse;
3015
3016 if (!*pfx->pex) break;
3017
3018 if (*chLimit != ';') {
3019 break;
3020 }
3021 }
3022
3023 if (pfx->exception->severity >= ErrorException)
3024 return MagickFalse;
3025
3026 return MagickTrue;
3027}
3028
3029/*--------------------------------------------------------------------
3030 Run-time
3031*/
3032
3033static ChannelStatistics *CollectOneImgStats (FxInfo * pfx, Image * img)
3034{
3035 int ch;
3036 ChannelStatistics * cs = GetImageStatistics (img, pfx->exception);
3037 /* Use RelinquishMagickMemory() somewhere. */
3038
3039 if (cs == (ChannelStatistics *) NULL)
3040 return((ChannelStatistics *) NULL);
3041
3042 for (ch=0; ch <= (int) MaxPixelChannels; ch++) {
3043 cs[ch].mean *= QuantumScale;
3044 cs[ch].median *= QuantumScale;
3045 cs[ch].maxima *= QuantumScale;
3046 cs[ch].minima *= QuantumScale;
3047 cs[ch].standard_deviation *= QuantumScale;
3048 }
3049
3050 return cs;
3051}
3052
3053static MagickBooleanType CollectStatistics (FxInfo * pfx)
3054{
3055 Image * img = GetFirstImageInList (pfx->image);
3056
3057 size_t imgNum=0;
3058
3059 pfx->statistics = (ChannelStatistics**) AcquireMagickMemory ((size_t) pfx->ImgListLen * sizeof (ChannelStatistics *));
3060 if (!pfx->statistics) {
3061 (void) ThrowMagickException (
3062 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
3063 "Statistics", "%lu",
3064 (unsigned long) pfx->ImgListLen);
3065 return MagickFalse;
3066 }
3067
3068 for (;;) {
3069 pfx->statistics[imgNum] = CollectOneImgStats (pfx, img);
3070 if (pfx->statistics[imgNum] == (ChannelStatistics *) NULL) {
3071 while (imgNum > 0) {
3072 imgNum--;
3073 pfx->statistics[imgNum] = (ChannelStatistics *) RelinquishMagickMemory (pfx->statistics[imgNum]);
3074 }
3075 pfx->statistics = (ChannelStatistics **) RelinquishMagickMemory (pfx->statistics);
3076 return MagickFalse;
3077 }
3078
3079 if (++imgNum == pfx->ImgListLen) break;
3080 img = GetNextImageInList (img);
3081 assert (img != (Image *) NULL);
3082 }
3083 pfx->GotStats = MagickTrue;
3084
3085 return MagickTrue;
3086}
3087
3088static inline MagickBooleanType PushVal (FxInfo * pfx, fxRtT * pfxrt, fxFltType val, int addr)
3089{
3090 if (pfxrt->usedValStack >=pfxrt->numValStack) {
3091 (void) ThrowMagickException (
3092 pfx->exception, GetMagickModule(), OptionError,
3093 "ValStack overflow at addr=", "%i",
3094 addr);
3095 return MagickFalse;
3096 }
3097
3098 pfxrt->ValStack[pfxrt->usedValStack++] = val;
3099 return MagickTrue;
3100}
3101
3102static inline fxFltType PopVal (FxInfo * pfx, fxRtT * pfxrt, int addr)
3103{
3104 if (pfxrt->usedValStack <= 0) {
3105 (void) ThrowMagickException (
3106 pfx->exception, GetMagickModule(), OptionError,
3107 "ValStack underflow at addr=", "%i",
3108 addr);
3109 return (fxFltType) 0;
3110 }
3111
3112 return pfxrt->ValStack[--pfxrt->usedValStack];
3113}
3114
3115static inline fxFltType ImageStat (
3116 FxInfo * pfx, ssize_t ImgNum, PixelChannel channel, ImgAttrE ia)
3117{
3118 ChannelStatistics * cs = NULL;
3119 fxFltType ret = 0;
3120 MagickBooleanType NeedRelinq = MagickFalse;
3121
3122 if (ImgNum < 0)
3123 {
3124 (void) ThrowMagickException(pfx->exception,GetMagickModule(),
3125 OptionError,"NoSuchImage","%lu",(unsigned long) ImgNum);
3126 ImgNum=0;
3127 }
3128
3129 if (pfx->GotStats) {
3130 if ((channel < 0) || (channel > MaxPixelChannels))
3131 {
3132 (void) ThrowMagickException(pfx->exception,GetMagickModule(),
3133 OptionError,"NoSuchImageChannel","%i",channel);
3134 channel=(PixelChannel) 0;
3135 }
3136 cs = pfx->statistics[ImgNum];
3137 } else if (pfx->NeedStats) {
3138 /* If we need more than one statistic per pixel, this is inefficient. */
3139 if ((channel < 0) || (channel > MaxPixelChannels))
3140 {
3141 (void) ThrowMagickException(pfx->exception,GetMagickModule(),
3142 OptionError,"NoSuchImageChannel","%i",channel);
3143 channel=(PixelChannel) 0;
3144 }
3145 cs = CollectOneImgStats (pfx, pfx->Images[ImgNum]);
3146 NeedRelinq = MagickTrue;
3147 }
3148
3149 switch (ia) {
3150 case aDepth:
3151 ret = (fxFltType) GetImageDepth (pfx->Images[ImgNum], pfx->exception);
3152 break;
3153 case aExtent:
3154 ret = (fxFltType) GetBlobSize (pfx->Images[ImgNum]);
3155 break;
3156 case aKurtosis:
3157 if ((cs != (ChannelStatistics *) NULL) && (channel >= 0))
3158 ret = cs[channel].kurtosis;
3159 break;
3160 case aMaxima:
3161 if ((cs != (ChannelStatistics *) NULL) && (channel >= 0))
3162 ret = cs[channel].maxima;
3163 break;
3164 case aMean:
3165 if ((cs != (ChannelStatistics *) NULL) && (channel >= 0))
3166 ret = cs[channel].mean;
3167 break;
3168 case aMedian:
3169 if ((cs != (ChannelStatistics *) NULL) && (channel >= 0))
3170 ret = cs[channel].median;
3171 break;
3172 case aMinima:
3173 if ((cs != (ChannelStatistics *) NULL) && (channel >= 0))
3174 ret = cs[channel].minima;
3175 break;
3176 case aPage:
3177 /* Do nothing */
3178 break;
3179 case aPageX:
3180 ret = (fxFltType) pfx->Images[ImgNum]->page.x;
3181 break;
3182 case aPageY:
3183 ret = (fxFltType) pfx->Images[ImgNum]->page.y;
3184 break;
3185 case aPageWid:
3186 ret = (fxFltType) pfx->Images[ImgNum]->page.width;
3187 break;
3188 case aPageHt:
3189 ret = (fxFltType) pfx->Images[ImgNum]->page.height;
3190 break;
3191 case aPrintsize:
3192 /* Do nothing */
3193 break;
3194 case aPrintsizeX:
3195 ret = (fxFltType) MagickSafeReciprocal (pfx->Images[ImgNum]->resolution.x)
3196 * pfx->Images[ImgNum]->columns;
3197 break;
3198 case aPrintsizeY:
3199 ret = (fxFltType) MagickSafeReciprocal (pfx->Images[ImgNum]->resolution.y)
3200 * pfx->Images[ImgNum]->rows;
3201 break;
3202 case aQuality:
3203 ret = (fxFltType) pfx->Images[ImgNum]->quality;
3204 break;
3205 case aRes:
3206 /* Do nothing */
3207 break;
3208 case aResX:
3209 ret = pfx->Images[ImgNum]->resolution.x;
3210 break;
3211 case aResY:
3212 ret = pfx->Images[ImgNum]->resolution.y;
3213 break;
3214 case aSkewness:
3215 if ((cs != (ChannelStatistics *) NULL) && (channel >= 0))
3216 ret = cs[channel].skewness;
3217 break;
3218 case aStdDev:
3219 if ((cs != (ChannelStatistics *) NULL) && (channel >= 0))
3220 ret = cs[channel].standard_deviation;
3221 break;
3222 case aH:
3223 ret = (fxFltType) pfx->Images[ImgNum]->rows;
3224 break;
3225 case aN:
3226 ret = (fxFltType) pfx->ImgListLen;
3227 break;
3228 case aT: /* image index in list */
3229 ret = (fxFltType) ImgNum;
3230 break;
3231 case aW:
3232 ret = (fxFltType) pfx->Images[ImgNum]->columns;
3233 break;
3234 case aZ:
3235 ret = (fxFltType) GetImageDepth (pfx->Images[ImgNum], pfx->exception);
3236 break;
3237 default:
3238 (void) ThrowMagickException (pfx->exception,GetMagickModule(),OptionError,
3239 "Unknown ia=","%i",ia);
3240 }
3241 if (NeedRelinq) cs = (ChannelStatistics *)RelinquishMagickMemory (cs);
3242
3243 return ret;
3244}
3245
3246static inline fxFltType FxGcd (fxFltType x, fxFltType y, const size_t depth)
3247{
3248#define FxMaxFunctionDepth 200
3249
3250 if (x < y)
3251 return (FxGcd (y, x, depth+1));
3252 if ((fabs((double) y) < 0.001) || (depth >= FxMaxFunctionDepth))
3253 return (x);
3254 return (FxGcd (y, x-y*floor((double) (x/y)), depth+1));
3255}
3256
3257static inline ssize_t ChkImgNum (FxInfo * pfx, fxFltType f)
3258/* Returns -1 if f is too large. */
3259{
3260 ssize_t i = (ssize_t) floor ((double) f + 0.5);
3261 if (i < 0) i += (ssize_t) pfx->ImgListLen;
3262 if (i < 0 || i >= (ssize_t) pfx->ImgListLen) {
3263 (void) ThrowMagickException (
3264 pfx->exception, GetMagickModule(), OptionError,
3265 "ImgNum", "%lu bad for ImgListLen %lu",
3266 (unsigned long) i, (unsigned long) pfx->ImgListLen);
3267 i = -1;
3268 }
3269 return i;
3270}
3271
3272#define WHICH_ATTR_CHAN \
3273 (pel->channel_qual == NO_CHAN_QUAL) ? CompositePixelChannel : \
3274 (pel->channel_qual == THIS_CHANNEL) ? channel : pel->channel_qual
3275
3276#define WHICH_NON_ATTR_CHAN \
3277 (pel->channel_qual == NO_CHAN_QUAL || \
3278 pel->channel_qual == THIS_CHANNEL || \
3279 pel->channel_qual == CompositePixelChannel \
3280 ) ? (channel == CompositePixelChannel ? RedPixelChannel: channel) \
3281 : pel->channel_qual
3282
3283static fxFltType GetHslFlt (FxInfo * pfx, ssize_t ImgNum, const fxFltType fx, const fxFltType fy,
3284 PixelChannel channel)
3285{
3286 Image * img = pfx->Images[ImgNum];
3287
3288 double red, green, blue;
3289 double hue=0, saturation=0, lightness=0;
3290
3291 MagickBooleanType okay = MagickTrue;
3292 if(!InterpolatePixelChannel (img, pfx->Imgs[ImgNum].View, RedPixelChannel, img->interpolate,
3293 (double) fx, (double) fy, &red, pfx->exception)) okay = MagickFalse;
3294 if(!InterpolatePixelChannel (img, pfx->Imgs[ImgNum].View, GreenPixelChannel, img->interpolate,
3295 (double) fx, (double) fy, &green, pfx->exception)) okay = MagickFalse;
3296 if(!InterpolatePixelChannel (img, pfx->Imgs[ImgNum].View, BluePixelChannel, img->interpolate,
3297 (double) fx, (double) fy, &blue, pfx->exception)) okay = MagickFalse;
3298
3299 if (!okay)
3300 (void) ThrowMagickException (
3301 pfx->exception, GetMagickModule(), OptionError,
3302 "GetHslFlt failure", "%lu %g,%g %i", (unsigned long) ImgNum,
3303 (double) fx, (double) fy, channel);
3304
3305 ConvertRGBToHSL (
3306 red, green, blue,
3307 &hue, &saturation, &lightness);
3308
3309 if (channel == HUE_CHANNEL) return hue;
3310 if (channel == SAT_CHANNEL) return saturation;
3311 if (channel == LIGHT_CHANNEL) return lightness;
3312
3313 return 0.0;
3314}
3315
3316static fxFltType GetHslInt (FxInfo * pfx, ssize_t ImgNum, const ssize_t imgx, const ssize_t imgy, PixelChannel channel)
3317{
3318 Image * img = pfx->Images[ImgNum];
3319
3320 double hue=0, saturation=0, lightness=0;
3321
3322 const Quantum * p = GetCacheViewVirtualPixels (pfx->Imgs[ImgNum].View, imgx, imgy, 1, 1, pfx->exception);
3323 if (p == (const Quantum *) NULL)
3324 {
3325 (void) ThrowMagickException (pfx->exception,GetMagickModule(),
3326 OptionError,"GetHslInt failure","%lu %li,%li %i",(unsigned long) ImgNum,
3327 (long) imgx,(long) imgy,channel);
3328 return(0.0);
3329 }
3330
3331 ConvertRGBToHSL (
3332 GetPixelRed (img, p), GetPixelGreen (img, p), GetPixelBlue (img, p),
3333 &hue, &saturation, &lightness);
3334
3335 if (channel == HUE_CHANNEL) return hue;
3336 if (channel == SAT_CHANNEL) return saturation;
3337 if (channel == LIGHT_CHANNEL) return lightness;
3338
3339 return 0.0;
3340}
3341
3342static inline fxFltType GetIntensity (FxInfo * pfx, ssize_t ImgNum, const fxFltType fx, const fxFltType fy)
3343{
3344 Quantum
3345 quantum_pixel[MaxPixelChannels];
3346
3347 PixelInfo
3348 pixelinf;
3349
3350 Image * img = pfx->Images[ImgNum];
3351
3352 (void) GetPixelInfo (img, &pixelinf);
3353
3354 if (!InterpolatePixelInfo (img, pfx->Imgs[ImgNum].View, img->interpolate,
3355 (double) fx, (double) fy, &pixelinf, pfx->exception))
3356 {
3357 (void) ThrowMagickException (
3358 pfx->exception, GetMagickModule(), OptionError,
3359 "GetIntensity failure", "%lu %g,%g", (unsigned long) ImgNum,
3360 (double) fx, (double) fy);
3361 }
3362
3363 SetPixelViaPixelInfo (img, &pixelinf, quantum_pixel);
3364 return QuantumScale * GetPixelIntensity (img, quantum_pixel);
3365}
3366
3367static MagickBooleanType ExecuteRPN (FxInfo * pfx, fxRtT * pfxrt, fxFltType *result,
3368 const PixelChannel channel, const ssize_t imgx, const ssize_t imgy)
3369{
3370 const Quantum * p = pfxrt->thisPixel;
3371 fxFltType regA=0, regB=0, regC=0, regD=0, regE=0;
3372 Image * img = pfx->image;
3373 ChannelStatistics * cs = NULL;
3374 MagickBooleanType NeedRelinq = MagickFalse;
3375 double hue=0, saturation=0, lightness=0;
3376 int i;
3377
3378 /* For -fx, this sets p to ImgNum 0.
3379 for %[fx:...], this sets p to the current image.
3380 Similarly img.
3381 */
3382 if (!p) p = GetCacheViewVirtualPixels (
3383 pfx->Imgs[pfx->ImgNum].View, imgx, imgy, 1, 1, pfx->exception);
3384
3385 if (p == (const Quantum *) NULL)
3386 {
3387 (void) ThrowMagickException (pfx->exception,GetMagickModule(),
3388 OptionError,"Can't get virtual pixels","%lu %li,%li",(unsigned long)
3389 pfx->ImgNum,(long) imgx,(long) imgy);
3390 return(MagickFalse);
3391 }
3392
3393 if (pfx->GotStats) {
3394 cs = pfx->statistics[pfx->ImgNum];
3395 } else if (pfx->NeedStats) {
3396 cs = CollectOneImgStats (pfx, pfx->Images[pfx->ImgNum]);
3397 NeedRelinq = MagickTrue;
3398 }
3399
3400 /* Following is only for expressions like "saturation", with no image specifier.
3401 */
3402 if (pfx->NeedHsl) {
3403 ConvertRGBToHSL (
3404 GetPixelRed (img, p), GetPixelGreen (img, p), GetPixelBlue (img, p),
3405 &hue, &saturation, &lightness);
3406 }
3407
3408 for (i=0; i < pfx->usedElements; i++) {
3409 ElementT
3410 *pel;
3411
3412 if (i < 0) {
3413 (void) ThrowMagickException (
3414 pfx->exception, GetMagickModule(), OptionError,
3415 "Bad run-time address", "%i", i);
3416 break;
3417 }
3418 pel=&pfx->Elements[i];
3419 switch (pel->number_args) {
3420 case 0:
3421 break;
3422 case 1:
3423 regA = PopVal (pfx, pfxrt, i);
3424 break;
3425 case 2:
3426 regB = PopVal (pfx, pfxrt, i);
3427 regA = PopVal (pfx, pfxrt, i);
3428 break;
3429 case 3:
3430 regC = PopVal (pfx, pfxrt, i);
3431 regB = PopVal (pfx, pfxrt, i);
3432 regA = PopVal (pfx, pfxrt, i);
3433 break;
3434 case 4:
3435 regD = PopVal (pfx, pfxrt, i);
3436 regC = PopVal (pfx, pfxrt, i);
3437 regB = PopVal (pfx, pfxrt, i);
3438 regA = PopVal (pfx, pfxrt, i);
3439 break;
3440 case 5:
3441 regE = PopVal (pfx, pfxrt, i);
3442 regD = PopVal (pfx, pfxrt, i);
3443 regC = PopVal (pfx, pfxrt, i);
3444 regB = PopVal (pfx, pfxrt, i);
3445 regA = PopVal (pfx, pfxrt, i);
3446 break;
3447 default:
3448 (void) ThrowMagickException (
3449 pfx->exception, GetMagickModule(), OptionError,
3450 "Too many args:", "%i", pel->number_args);
3451 break;
3452 }
3453
3454 switch (pel->operator_index) {
3455 case oAddEq:
3456 regA = (pfxrt->UserSymVals[pel->element_index] += regA);
3457 break;
3458 case oSubtractEq:
3459 regA = (pfxrt->UserSymVals[pel->element_index] -= regA);
3460 break;
3461 case oMultiplyEq:
3462 regA = (pfxrt->UserSymVals[pel->element_index] *= regA);
3463 break;
3464 case oDivideEq:
3465 if (fabs((double) regA) < MagickEpsilon) {
3466 (void) ThrowMagickException (pfx->exception, GetMagickModule(),
3467 OptionError, "DivideByZero", "`%s'", SetShortExp(pfx));
3468 regA = (fxFltType) 0.0;
3469 } else {
3470 regA = (pfxrt->UserSymVals[pel->element_index] /= regA);
3471 }
3472 break;
3473 case oPlusPlus:
3474 regA = pfxrt->UserSymVals[pel->element_index]++;
3475 break;
3476 case oSubSub:
3477 regA = pfxrt->UserSymVals[pel->element_index]--;
3478 break;
3479 case oAdd:
3480 regA += regB;
3481 break;
3482 case oSubtract:
3483 regA -= regB;
3484 break;
3485 case oMultiply:
3486 regA *= regB;
3487 break;
3488 case oDivide:
3489 if (fabs((double) regB) < MagickEpsilon) {
3490 (void) ThrowMagickException (pfx->exception, GetMagickModule(),
3491 OptionError, "DivideByZero", "`%s'", SetShortExp(pfx));
3492 regA = (fxFltType) 0.0;
3493 } else {
3494 regA /= regB;
3495 }
3496 break;
3497 case oModulus:
3498 {
3499 double mod_val = fabs(floor((double) regB+0.5));
3500 if (mod_val < MagickEpsilon) {
3501 (void) ThrowMagickException (pfx->exception, GetMagickModule(),
3502 OptionError, "DivideByZero", "`%s'", SetShortExp(pfx));
3503 regA = (fxFltType) 0.0;
3504 } else {
3505 regA = fmod ((double) regA, mod_val);
3506 }
3507 break;
3508 }
3509 case oUnaryPlus:
3510 /* Do nothing. */
3511 break;
3512 case oUnaryMinus:
3513 regA = -regA;
3514 break;
3515 case oLshift:
3516 if ((regB < -0.5) || (CastDoubleToSizeT((double) regB+0.5) >= (8*sizeof(size_t))))
3517 {
3518 (void) ThrowMagickException ( pfx->exception, GetMagickModule(),
3519 OptionError, "undefined shift", "%g", (double) regB);
3520 regA = (fxFltType) 0.0;
3521 break;
3522 }
3523 regA = (fxFltType) (CastDoubleToSizeT((double) regA+0.5) << CastDoubleToSizeT((double) regB+0.5));
3524 break;
3525 case oRshift:
3526 if ((regB < -0.5) || (CastDoubleToSizeT((double) regB+0.5) >= (8*sizeof(size_t))))
3527 {
3528 (void) ThrowMagickException ( pfx->exception, GetMagickModule(),
3529 OptionError, "undefined shift", "%g", (double) regB);
3530 regA = (fxFltType) 0.0;
3531 break;
3532 }
3533 regA = (fxFltType) (CastDoubleToSizeT((double) regA+0.5) >> CastDoubleToSizeT((double) regB+0.5));
3534 break;
3535 case oEq:
3536 regA = fabs((double) (regA-regB)) < MagickEpsilon ? 1.0 : 0.0;
3537 break;
3538 case oNotEq:
3539 regA = fabs((double) (regA-regB)) >= MagickEpsilon ? 1.0 : 0.0;
3540 break;
3541 case oLtEq:
3542 regA = (regA <= regB) ? 1.0 : 0.0;
3543 break;
3544 case oGtEq:
3545 regA = (regA >= regB) ? 1.0 : 0.0;
3546 break;
3547 case oLt:
3548 regA = (regA < regB) ? 1.0 : 0.0;
3549 break;
3550 case oGt:
3551 regA = (regA > regB) ? 1.0 : 0.0;
3552 break;
3553 case oLogAnd:
3554 regA = (regA<=0) ? 0.0 : (regB > 0) ? 1.0 : 0.0;
3555 break;
3556 case oLogOr:
3557 regA = (regA>0) ? 1.0 : (regB > 0.0) ? 1.0 : 0.0;
3558 break;
3559 case oLogNot:
3560 regA = (regA==0) ? 1.0 : 0.0;
3561 break;
3562 case oBitAnd:
3563 regA = (fxFltType) (CastDoubleToSizeT((double) regA+0.5) & CastDoubleToSizeT((double) regB+0.5));
3564 break;
3565 case oBitOr:
3566 regA = (fxFltType) (CastDoubleToSizeT((double) regA+0.5) | CastDoubleToSizeT((double) regB+0.5));
3567 break;
3568 case oBitNot:
3569 {
3570 size_t
3571 new_value;
3572
3573 /* Old fx doesn't add 0.5. */
3574 new_value=~CastDoubleToSizeT((double) regA+0.5);
3575 regA=(fxFltType) new_value;
3576 break;
3577 }
3578 case oPow:
3579 regA = pow ((double) regA, (double) regB);
3580 break;
3581 case oQuery:
3582 case oColon:
3583 break;
3584 case oOpenParen:
3585 case oCloseParen:
3586 case oOpenBracket:
3587 case oCloseBracket:
3588 case oOpenBrace:
3589 case oCloseBrace:
3590 break;
3591 case oAssign:
3592 pel->val = regA;
3593 break;
3594 case oNull: {
3595 if (pel->type == etColourConstant) {
3596 switch (channel) { default:
3597 case (PixelChannel) 0:
3598 regA = pel->val;
3599 break;
3600 case (PixelChannel) 1:
3601 regA = pel->val1;
3602 break;
3603 case (PixelChannel) 2:
3604 regA = pel->val2;
3605 break;
3606 }
3607 } else {
3608 regA = pel->val;
3609 }
3610 break;
3611 }
3612 case fAbs:
3613 regA = fabs ((double) regA);
3614 break;
3615#if defined(MAGICKCORE_HAVE_ACOSH)
3616 case fAcosh:
3617 regA = acosh ((double) regA);
3618 break;
3619#endif
3620 case fAcos:
3621 regA = acos ((double) regA);
3622 break;
3623#if defined(MAGICKCORE_HAVE_J1)
3624 case fAiry:
3625 if (regA==0) regA = 1.0;
3626 else {
3627 fxFltType gamma = 2.0 * __j1((double) (MagickPI*regA)) / (MagickPI*regA);
3628 regA = gamma * gamma;
3629 }
3630 break;
3631#endif
3632 case fAlt:
3633 regA = (fxFltType) (((ssize_t) regA) & 0x01 ? -1.0 : 1.0);
3634 break;
3635#if defined(MAGICKCORE_HAVE_ASINH)
3636 case fAsinh:
3637 regA = asinh ((double) regA);
3638 break;
3639#endif
3640 case fAsin:
3641 regA = asin ((double) regA);
3642 break;
3643#if defined(MAGICKCORE_HAVE_ATANH)
3644 case fAtanh:
3645 regA = atanh ((double) regA);
3646 break;
3647#endif
3648 case fAtan2:
3649 regA = atan2 ((double) regA, (double) regB);
3650 break;
3651 case fAtan:
3652 regA = atan ((double) regA);
3653 break;
3654 case fCeil:
3655 regA = ceil ((double) regA);
3656 break;
3657 case fChannel:
3658 switch (channel) {
3659 case (PixelChannel) 0: break;
3660 case (PixelChannel) 1: regA = regB; break;
3661 case (PixelChannel) 2: regA = regC; break;
3662 case (PixelChannel) 3: regA = regD; break;
3663 case (PixelChannel) 4: regA = regE; break;
3664 default: regA = 0.0;
3665 }
3666 break;
3667 case fClamp:
3668 if (regA < 0) regA = 0.0;
3669 else if (regA > 1.0) regA = 1.0;
3670 break;
3671 case fCosh:
3672 regA = cosh ((double) regA);
3673 break;
3674 case fCos:
3675 regA = cos ((double) regA);
3676 break;
3677 case fDebug:
3678 /* FIXME: debug() should give channel name. */
3679
3680 (void) fprintf (stderr, "%s[%g,%g].[%i]: %s=%.*g\n",
3681 img->filename, (double) imgx, (double) imgy,
3682 channel, SetPtrShortExp (pfx, pel->exp_start, (size_t) (pel->exp_len+1)),
3683 pfx->precision, (double) regA);
3684 break;
3685 case fDrc:
3686 regA = regA / (regB*(regA-1.0) + 1.0);
3687 break;
3688#if defined(MAGICKCORE_HAVE_ERF)
3689 case fErf:
3690 regA = erf ((double) regA);
3691 break;
3692#endif
3693 case fEpoch:
3694 /* Do nothing. */
3695 break;
3696 case fExp:
3697 regA = exp ((double) regA);
3698 break;
3699 case fFloor:
3700 regA = floor ((double) regA);
3701 break;
3702 case fGauss:
3703 regA = exp((double) (-regA*regA/2.0))/sqrt(2.0*MagickPI);
3704 break;
3705 case fGcd:
3706 if (!IsNaN((double) regA))
3707 regA = FxGcd (regA, regB, 0);
3708 break;
3709 case fHypot:
3710 regA = hypot ((double) regA, (double) regB);
3711 break;
3712 case fInt:
3713 regA = floor ((double) regA);
3714 break;
3715 case fIsnan:
3716 regA = (fxFltType) (!!IsNaN ((double) regA));
3717 break;
3718#if defined(MAGICKCORE_HAVE_J0)
3719 case fJ0:
3720 regA = __j0((double) regA);
3721 break;
3722#endif
3723#if defined(MAGICKCORE_HAVE_J1)
3724 case fJ1:
3725 regA = __j1((double) regA);
3726 break;
3727#endif
3728#if defined(MAGICKCORE_HAVE_J1)
3729 case fJinc:
3730 if (regA==0) regA = 1.0;
3731 else regA = 2.0 * __j1((double) (MagickPI*regA))/(MagickPI*regA);
3732 break;
3733#endif
3734 case fLn:
3735 regA = log ((double) regA);
3736 break;
3737 case fLogtwo:
3738 regA = log2 ((double) regA);
3739 break;
3740 case fLog:
3741 regA = log10 ((double) regA);
3742 break;
3743 case fMagickTime:
3744 regA = (fxFltType) GetMagickTime();
3745 break;
3746 case fMax:
3747 regA = (regA > regB) ? regA : regB;
3748 break;
3749 case fMin:
3750 regA = (regA < regB) ? regA : regB;
3751 break;
3752 case fMod:
3753 if (fabs((double) regB) < MagickEpsilon) {
3754 regA = 0;
3755 } else {
3756 regA = regA - floor((double) (regA/regB))*regB;
3757 }
3758 break;
3759 case fNot:
3760 regA = (fxFltType) (regA < MagickEpsilon);
3761 break;
3762 case fPow:
3763 regA = pow ((double) regA, (double) regB);
3764 break;
3765 case fRand: {
3766 regA = GetPseudoRandomValue (pfxrt->random_info);
3767 break;
3768 }
3769 case fRound:
3770 regA = floor ((double) regA + 0.5);
3771 break;
3772 case fSign:
3773 regA = (regA < 0) ? -1.0 : 1.0;
3774 break;
3775 case fSinc:
3776 if (fabs((double) regA) < MagickEpsilon)
3777 regA = 1.0;
3778 else
3779 regA = sin ((double) (MagickPI*regA)) / (MagickPI*regA);
3780 break;
3781 case fSinh:
3782 regA = sinh ((double) regA);
3783 break;
3784 case fSin:
3785 regA = sin ((double) regA);
3786 break;
3787 case fSqrt:
3788 regA = sqrt ((double) regA);
3789 break;
3790 case fSquish:
3791 regA = 1.0 / (1.0 + exp ((double) -regA));
3792 break;
3793 case fTanh:
3794 regA = tanh ((double) regA);
3795 break;
3796 case fTan:
3797 regA = tan ((double) regA);
3798 break;
3799 case fTrunc:
3800 if (regA >= 0) regA = floor ((double) regA);
3801 else regA = ceil ((double) regA);
3802 break;
3803
3804 case fDo:
3805 case fFor:
3806 case fIf:
3807 case fWhile:
3808 break;
3809 case fU: {
3810 /* Note: 1 value is available, index into image list.
3811 May have ImgAttr qualifier or channel qualifier or both.
3812 */
3813 ssize_t ImgNum = ChkImgNum (pfx, regA);
3814 if (ImgNum < 0) break;
3815 regA = (fxFltType) 0;
3816 if (ImgNum == 0) {
3817 Image * pimg = pfx->Images[0];
3818 if (pel->img_attr_qual == aNull) {
3819 if ((int) pel->channel_qual < 0) {
3820 if (pel->channel_qual == NO_CHAN_QUAL || pel->channel_qual == THIS_CHANNEL) {
3821 if (pfx->ImgNum==0) {
3822 regA = QuantumScale * (double) p[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3823 } else {
3824 const Quantum * pv = GetCacheViewVirtualPixels (
3825 pfx->Imgs[0].View, imgx, imgy, 1,1, pfx->exception);
3826 if (!pv) {
3827 (void) ThrowMagickException (
3828 pfx->exception, GetMagickModule(), OptionError,
3829 "fU can't get cache", "%lu", (unsigned long) ImgNum);
3830 break;
3831 }
3832 regA = QuantumScale * (double) pv[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3833 }
3834 } else if (pel->channel_qual == HUE_CHANNEL || pel->channel_qual == SAT_CHANNEL ||
3835 pel->channel_qual == LIGHT_CHANNEL) {
3836 regA = GetHslInt (pfx, ImgNum, imgx, imgy, pel->channel_qual);
3837 break;
3838 } else if (pel->channel_qual == INTENSITY_CHANNEL) {
3839 regA = GetIntensity (pfx, 0, (double) imgx, (double) imgy);
3840 break;
3841 }
3842 } else {
3843 if (pfx->ImgNum==0) {
3844 regA = QuantumScale * (double) p[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3845 } else {
3846 const Quantum * pv = GetCacheViewVirtualPixels (
3847 pfx->Imgs[0].View, imgx, imgy, 1,1, pfx->exception);
3848 if (!pv) {
3849 (void) ThrowMagickException (
3850 pfx->exception, GetMagickModule(), OptionError,
3851 "fU can't get cache", "%lu", (unsigned long) ImgNum);
3852 break;
3853 }
3854 regA = QuantumScale * (double) pv[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3855 }
3856 }
3857 } else {
3858 /* we have an image attribute */
3859 regA = ImageStat (pfx, 0, WHICH_ATTR_CHAN, pel->img_attr_qual);
3860 }
3861 } else {
3862 /* We have non-zero ImgNum. */
3863 if (pel->img_attr_qual == aNull) {
3864 const Quantum * pv;
3865 if ((int) pel->channel_qual < 0) {
3866 if (pel->channel_qual == HUE_CHANNEL || pel->channel_qual == SAT_CHANNEL ||
3867 pel->channel_qual == LIGHT_CHANNEL)
3868 {
3869 regA = GetHslInt (pfx, ImgNum, imgx, imgy, pel->channel_qual);
3870 break;
3871 } else if (pel->channel_qual == INTENSITY_CHANNEL)
3872 {
3873 regA = GetIntensity (pfx, ImgNum, (fxFltType) imgx, (fxFltType) imgy);
3874 break;
3875 }
3876 }
3877
3878 pv = GetCacheViewVirtualPixels (
3879 pfx->Imgs[ImgNum].View, imgx, imgy, 1,1, pfx->exception);
3880 if (!pv) {
3881 (void) ThrowMagickException (
3882 pfx->exception, GetMagickModule(), OptionError,
3883 "fU can't get cache", "%lu", (unsigned long) ImgNum);
3884 break;
3885 }
3886 regA = QuantumScale * (double)
3887 pv[pfx->Images[ImgNum]->channel_map[WHICH_NON_ATTR_CHAN].offset];
3888 } else {
3889 regA = ImageStat (pfx, ImgNum, WHICH_ATTR_CHAN, pel->img_attr_qual);
3890 }
3891 }
3892 break;
3893 }
3894 case fU0: {
3895 /* No args. No image attribute. We may have a ChannelQual.
3896 If called from %[fx:...], ChannelQual will be CompositePixelChannel.
3897 */
3898 Image * pimg = pfx->Images[0];
3899 if ((int) pel->channel_qual < 0) {
3900 if (pel->channel_qual == NO_CHAN_QUAL || pel->channel_qual == THIS_CHANNEL) {
3901
3902 if (pfx->ImgNum==0) {
3903 regA = QuantumScale * (double) p[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3904 } else {
3905 const Quantum * pv = GetCacheViewVirtualPixels (
3906 pfx->Imgs[0].View, imgx, imgy, 1,1, pfx->exception);
3907 if (!pv) {
3908 (void) ThrowMagickException (
3909 pfx->exception, GetMagickModule(), OptionError,
3910 "fU0 can't get cache", "%i", 0);
3911 break;
3912 }
3913 regA = QuantumScale * (double) pv[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3914 }
3915
3916 } else if (pel->channel_qual == HUE_CHANNEL || pel->channel_qual == SAT_CHANNEL ||
3917 pel->channel_qual == LIGHT_CHANNEL) {
3918 regA = GetHslInt (pfx, 0, imgx, imgy, pel->channel_qual);
3919 break;
3920 } else if (pel->channel_qual == INTENSITY_CHANNEL) {
3921 regA = GetIntensity (pfx, 0, (fxFltType) imgx, (fxFltType) imgy);
3922 }
3923 } else {
3924 if (pfx->ImgNum==0) {
3925 regA = QuantumScale * (double) p[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3926 } else {
3927 const Quantum * pv = GetCacheViewVirtualPixels (
3928 pfx->Imgs[0].View, imgx, imgy, 1,1, pfx->exception);
3929 if (!pv) {
3930 (void) ThrowMagickException (
3931 pfx->exception, GetMagickModule(), OptionError,
3932 "fU0 can't get cache", "%i", 0);
3933 break;
3934 }
3935 regA = QuantumScale * (double) pv[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3936 }
3937 }
3938 break;
3939 }
3940 case fUP: {
3941 /* 3 args are: ImgNum, x, y */
3942 ssize_t ImgNum = ChkImgNum (pfx, regA);
3943 fxFltType fx, fy;
3944
3945 if (ImgNum < 0) break;
3946
3947 if (pel->is_relative) {
3948 fx = imgx + regB;
3949 fy = imgy + regC;
3950 } else {
3951 fx = regB;
3952 fy = regC;
3953 }
3954
3955 if ((int) pel->channel_qual < 0) {
3956 if (pel->channel_qual == HUE_CHANNEL || pel->channel_qual == SAT_CHANNEL
3957 || pel->channel_qual == LIGHT_CHANNEL) {
3958 regA = GetHslFlt (pfx, ImgNum, fx, fy, pel->channel_qual);
3959 break;
3960 } else if (pel->channel_qual == INTENSITY_CHANNEL) {
3961 regA = GetIntensity (pfx, ImgNum, fx, fy);
3962 break;
3963 }
3964 }
3965
3966 {
3967 double v;
3968 Image * imUP = pfx->Images[ImgNum];
3969 if (! InterpolatePixelChannel (imUP, pfx->Imgs[ImgNum].View, WHICH_NON_ATTR_CHAN,
3970 imUP->interpolate, (double) fx, (double) fy, &v, pfx->exception))
3971 {
3972 (void) ThrowMagickException (
3973 pfx->exception, GetMagickModule(), OptionError,
3974 "fUP can't get interpolate", "%lu", (unsigned long) ImgNum);
3975 break;
3976 }
3977 regA = v * QuantumScale;
3978 }
3979
3980 break;
3981 }
3982 case fS:
3983 case fV: {
3984 /* No args. */
3985 ssize_t ImgNum = 1;
3986 if (pel->operator_index == fS) ImgNum = pfx->ImgNum;
3987
3988 if (pel->img_attr_qual == aNull) {
3989 const Quantum * pv = GetCacheViewVirtualPixels (
3990 pfx->Imgs[ImgNum].View, imgx, imgy, 1,1, pfx->exception);
3991 if (!pv) {
3992 (void) ThrowMagickException (
3993 pfx->exception, GetMagickModule(), OptionError,
3994 "fV can't get cache", "%lu", (unsigned long) ImgNum);
3995 break;
3996 }
3997
3998 if ((int) pel->channel_qual < 0) {
3999 if (pel->channel_qual == HUE_CHANNEL || pel->channel_qual == SAT_CHANNEL ||
4000 pel->channel_qual == LIGHT_CHANNEL) {
4001 regA = GetHslInt (pfx, ImgNum, imgx, imgy, pel->channel_qual);
4002 break;
4003 } else if (pel->channel_qual == INTENSITY_CHANNEL) {
4004 regA = GetIntensity (pfx, ImgNum, (double) imgx, (double) imgy);
4005 break;
4006 }
4007 }
4008
4009 regA = QuantumScale * (double)
4010 pv[pfx->Images[ImgNum]->channel_map[WHICH_NON_ATTR_CHAN].offset];
4011 } else {
4012 regA = ImageStat (pfx, ImgNum, WHICH_ATTR_CHAN, pel->img_attr_qual);
4013 }
4014
4015 break;
4016 }
4017 case fP:
4018 case fSP:
4019 case fVP: {
4020 /* 2 args are: x, y */
4021 fxFltType fx, fy;
4022 ssize_t ImgNum = pfx->ImgNum;
4023 if (pel->operator_index == fVP) ImgNum = 1;
4024 if (pel->is_relative) {
4025 fx = imgx + regA;
4026 fy = imgy + regB;
4027 } else {
4028 fx = regA;
4029 fy = regB;
4030 }
4031 if ((int) pel->channel_qual < 0) {
4032 if (pel->channel_qual == HUE_CHANNEL || pel->channel_qual == SAT_CHANNEL ||
4033 pel->channel_qual == LIGHT_CHANNEL) {
4034 regA = GetHslFlt (pfx, ImgNum, fx, fy, pel->channel_qual);
4035 break;
4036 } else if (pel->channel_qual == INTENSITY_CHANNEL) {
4037 regA = GetIntensity (pfx, ImgNum, fx, fy);
4038 break;
4039 }
4040 }
4041
4042 {
4043 double v;
4044
4045 if (! InterpolatePixelChannel (pfx->Images[ImgNum], pfx->Imgs[ImgNum].View,
4046 WHICH_NON_ATTR_CHAN, pfx->Images[ImgNum]->interpolate,
4047 (double) fx, (double) fy, &v, pfx->exception)
4048 )
4049 {
4050 (void) ThrowMagickException (
4051 pfx->exception, GetMagickModule(), OptionError,
4052 "fSP or fVP can't get interp", "%lu", (unsigned long) ImgNum);
4053 break;
4054 }
4055 regA = v * (fxFltType)QuantumScale;
4056 }
4057
4058 break;
4059 }
4060 case fNull:
4061 break;
4062 case aDepth:
4063 regA = (fxFltType) GetImageDepth (img, pfx->exception);
4064 break;
4065 case aExtent:
4066 regA = (fxFltType) GetBlobSize (img);
4067 break;
4068 case aKurtosis:
4069 if ((cs != (ChannelStatistics *) NULL) && (channel >= 0))
4070 regA = cs[WHICH_ATTR_CHAN].kurtosis;
4071 break;
4072 case aMaxima:
4073 if ((cs != (ChannelStatistics *) NULL) && (channel >= 0))
4074 regA = cs[WHICH_ATTR_CHAN].maxima;
4075 break;
4076 case aMean:
4077 if ((cs != (ChannelStatistics *) NULL) && (channel >= 0))
4078 regA = cs[WHICH_ATTR_CHAN].mean;
4079 break;
4080 case aMedian:
4081 if ((cs != (ChannelStatistics *) NULL) && (channel >= 0))
4082 regA = cs[WHICH_ATTR_CHAN].median;
4083 break;
4084 case aMinima:
4085 if ((cs != (ChannelStatistics *) NULL) && (channel >= 0))
4086 regA = cs[WHICH_ATTR_CHAN].minima;
4087 break;
4088 case aPage:
4089 break;
4090 case aPageX:
4091 regA = (fxFltType) img->page.x;
4092 break;
4093 case aPageY:
4094 regA = (fxFltType) img->page.y;
4095 break;
4096 case aPageWid:
4097 regA = (fxFltType) img->page.width;
4098 break;
4099 case aPageHt:
4100 regA = (fxFltType) img->page.height;
4101 break;
4102 case aPrintsize:
4103 break;
4104 case aPrintsizeX:
4105 regA = (fxFltType) MagickSafeReciprocal (img->resolution.x) * img->columns;
4106 break;
4107 case aPrintsizeY:
4108 regA = (fxFltType) MagickSafeReciprocal (img->resolution.y) * img->rows;
4109 break;
4110 case aQuality:
4111 regA = (fxFltType) img->quality;
4112 break;
4113 case aRes:
4114 break;
4115 case aResX:
4116 regA = (fxFltType) img->resolution.x;
4117 break;
4118 case aResY:
4119 regA = (fxFltType) img->resolution.y;
4120 break;
4121 case aSkewness:
4122 if ((cs != (ChannelStatistics *) NULL) && (channel >= 0))
4123 regA = cs[WHICH_ATTR_CHAN].skewness;
4124 break;
4125 case aStdDev:
4126 if ((cs != (ChannelStatistics *) NULL) && (channel >= 0))
4127 regA = cs[WHICH_ATTR_CHAN].standard_deviation;
4128 break;
4129 case aH: /* image->rows */
4130 regA = (fxFltType) img->rows;
4131 break;
4132 case aN: /* image list length */
4133 regA = (fxFltType) pfx->ImgListLen;
4134 break;
4135 case aT: /* image index in list */
4136 regA = (fxFltType) pfx->ImgNum;
4137 break;
4138 case aW: /* image->columns */
4139 regA = (fxFltType) img->columns;
4140 break;
4141 case aZ: /* image depth */
4142 regA = (fxFltType) GetImageDepth (img, pfx->exception);
4143 break;
4144 case aNull:
4145 break;
4146 case sHue: /* of conversion to HSL */
4147 regA = hue;
4148 break;
4149 case sIntensity:
4150 regA = GetIntensity (pfx, pfx->ImgNum, (double) imgx, (double) imgy);
4151 break;
4152 case sLightness: /* of conversion to HSL */
4153 regA = lightness;
4154 break;
4155 case sLuma: /* calculation */
4156 case sLuminance: /* as Luma */
4157 regA = QuantumScale * (0.212656 * (double) GetPixelRed (img,p) +
4158 0.715158 * (double) GetPixelGreen (img,p) +
4159 0.072186 * (double) GetPixelBlue (img,p));
4160 break;
4161 case sSaturation: /* from conversion to HSL */
4162 regA = saturation;
4163 break;
4164 case sA: /* alpha */
4165 regA = QuantumScale * (double) GetPixelAlpha (img, p);
4166 break;
4167 case sB: /* blue */
4168 regA = QuantumScale * (double) GetPixelBlue (img, p);
4169 break;
4170 case sC: /* red (ie cyan) */
4171 regA = QuantumScale * (double) GetPixelCyan (img, p);
4172 break;
4173 case sG: /* green */
4174 regA = QuantumScale * (double) GetPixelGreen (img, p);
4175 break;
4176 case sI: /* current x-coordinate */
4177 regA = (fxFltType) imgx;
4178 break;
4179 case sJ: /* current y-coordinate */
4180 regA = (fxFltType) imgy;
4181 break;
4182 case sK: /* black of CMYK */
4183 regA = QuantumScale * (double) GetPixelBlack (img, p);
4184 break;
4185 case sM: /* green (ie magenta) */
4186 regA = QuantumScale * (double) GetPixelGreen (img, p);
4187 break;
4188 case sO: /* alpha */
4189 regA = QuantumScale * (double) GetPixelAlpha (img, p);
4190 break;
4191 case sR:
4192 regA = QuantumScale * (double) GetPixelRed (img, p);
4193 break;
4194 case sY:
4195 regA = QuantumScale * (double) GetPixelYellow (img, p);
4196 break;
4197 case sNull:
4198 break;
4199
4200 case rGoto:
4201 assert (pel->element_index >= 0);
4202 i = pel->element_index-1; /* -1 because 'for' loop will increment. */
4203 break;
4204 case rGotoChk:
4205 assert (pel->element_index >= 0);
4206 i = pel->element_index-1; /* -1 because 'for' loop will increment. */
4207 if (IsImageTTLExpired(img) != MagickFalse) {
4208 i = pfx->usedElements-1; /* Do no more opcodes. */
4209 (void) ThrowMagickException (pfx->exception, GetMagickModule(),
4210 ResourceLimitFatalError, "TimeLimitExceeded", "`%s'", img->filename);
4211 }
4212 break;
4213 case rIfZeroGoto:
4214 assert (pel->element_index >= 0);
4215 if (fabs((double) regA) < MagickEpsilon) i = pel->element_index-1;
4216 break;
4217 case rIfNotZeroGoto:
4218 assert (pel->element_index >= 0);
4219 if (fabs((double) regA) > MagickEpsilon) i = pel->element_index-1;
4220 break;
4221 case rCopyFrom:
4222 assert (pel->element_index >= 0);
4223 regA = pfxrt->UserSymVals[pel->element_index];
4224 break;
4225 case rCopyTo:
4226 assert (pel->element_index >= 0);
4227 pfxrt->UserSymVals[pel->element_index] = regA;
4228 break;
4229 case rZerStk:
4230 pfxrt->usedValStack = 0;
4231 break;
4232 case rNull:
4233 break;
4234
4235 default:
4236 (void) ThrowMagickException (
4237 pfx->exception, GetMagickModule(), OptionError,
4238 "pel->oprNum", "%i '%s' not yet implemented",
4239 (int)pel->operator_index, OprStr(pel->operator_index));
4240 break;
4241 }
4242 if (pel->do_push)
4243 if (!PushVal (pfx, pfxrt, regA, i)) break;
4244 }
4245
4246 if (pfxrt->usedValStack > 0) regA = PopVal (pfx, pfxrt, 9999);
4247
4248 *result = regA;
4249
4250 if (NeedRelinq) cs = (ChannelStatistics *)RelinquishMagickMemory (cs);
4251
4252 if (pfx->exception->severity >= ErrorException)
4253 return MagickFalse;
4254
4255 if (pfxrt->usedValStack != 0) {
4256 (void) ThrowMagickException (
4257 pfx->exception, GetMagickModule(), OptionError,
4258 "ValStack not empty", "(%i)", pfxrt->usedValStack);
4259 return MagickFalse;
4260 }
4261
4262 return MagickTrue;
4263}
4264
4265/* Following is substitute for FxEvaluateChannelExpression().
4266*/
4267MagickPrivate MagickBooleanType FxEvaluateChannelExpression (
4268 FxInfo *pfx,
4269 const PixelChannel channel, const ssize_t x, const ssize_t y,
4270 double *result, ExceptionInfo *exception)
4271{
4272 const int
4273 id = GetOpenMPThreadId();
4274
4275 fxFltType ret;
4276
4277 assert (pfx != NULL);
4278 assert (pfx->image != NULL);
4279 assert (pfx->Images != NULL);
4280 assert (pfx->Imgs != NULL);
4281 assert (pfx->fxrts != NULL);
4282 assert (id >= 0 && id < (int) GetMagickResourceLimit(ThreadResource));
4283
4284 pfx->fxrts[id].thisPixel = NULL;
4285
4286
4287 if (!ExecuteRPN (pfx, &pfx->fxrts[id], &ret, channel, x, y)) {
4288 (void) ThrowMagickException (
4289 exception, GetMagickModule(), OptionError,
4290 "ExecuteRPN failed", " ");
4291 return MagickFalse;
4292 }
4293
4294 *result = (double) ret;
4295
4296 return MagickTrue;
4297}
4298
4299static FxInfo *AcquireFxInfoPrivate (const Image * images, const char * expression,
4300 MagickBooleanType CalcAllStats, ExceptionInfo *exception)
4301{
4302 char chLimit;
4303
4304 FxInfo * pfx = (FxInfo*) AcquireCriticalMemory (sizeof (*pfx));
4305
4306 memset (pfx, 0, sizeof (*pfx));
4307
4308 if (!InitFx (pfx, images, CalcAllStats, exception)) {
4309 pfx = (FxInfo*) RelinquishMagickMemory(pfx);
4310 return NULL;
4311 }
4312
4313 if (!BuildRPN (pfx)) {
4314 (void) DeInitFx (pfx);
4315 pfx = (FxInfo*) RelinquishMagickMemory(pfx);
4316 return((FxInfo *) NULL);
4317 }
4318
4319 if ((*expression == '@') && (strlen(expression) > 1))
4320 pfx->expression=FileToString(expression,~0UL,exception);
4321 if (pfx->expression == (char *) NULL)
4322 pfx->expression=ConstantString(expression);
4323 pfx->pex = (char *) pfx->expression;
4324
4325 pfx->teDepth = 0;
4326 if (!TranslateStatementList (pfx, ";", &chLimit)) {
4327 (void) DestroyRPN (pfx);
4328 pfx->expression = DestroyString (pfx->expression);
4329 pfx->pex = NULL;
4330 (void) DeInitFx (pfx);
4331 pfx = (FxInfo*) RelinquishMagickMemory(pfx);
4332 return NULL;
4333 }
4334
4335 if (pfx->teDepth) {
4336 (void) ThrowMagickException (
4337 pfx->exception, GetMagickModule(), OptionError,
4338 "Translate expression depth", "(%i) not 0",
4339 pfx->teDepth);
4340
4341 (void) DestroyRPN (pfx);
4342 pfx->expression = DestroyString (pfx->expression);
4343 pfx->pex = NULL;
4344 (void) DeInitFx (pfx);
4345 pfx = (FxInfo*) RelinquishMagickMemory(pfx);
4346 return NULL;
4347 }
4348
4349 if (chLimit != '\0' && chLimit != ';') {
4350 (void) ThrowMagickException (
4351 pfx->exception, GetMagickModule(), OptionError,
4352 "AcquireFxInfo: TranslateExpression did not exhaust input", "(chLimit=%i) at'%s'",
4353 (int)chLimit, pfx->pex);
4354
4355 (void) DestroyRPN (pfx);
4356 pfx->expression = DestroyString (pfx->expression);
4357 pfx->pex = NULL;
4358 (void) DeInitFx (pfx);
4359 pfx = (FxInfo*) RelinquishMagickMemory(pfx);
4360 return NULL;
4361 }
4362
4363 if (pfx->NeedStats && pfx->runType == rtEntireImage && !pfx->statistics) {
4364 if (!CollectStatistics (pfx)) {
4365 (void) DestroyRPN (pfx);
4366 pfx->expression = DestroyString (pfx->expression);
4367 pfx->pex = NULL;
4368 (void) DeInitFx (pfx);
4369 pfx = (FxInfo*) RelinquishMagickMemory(pfx);
4370 return NULL;
4371 }
4372 }
4373
4374 if (pfx->DebugOpt) {
4375 DumpTables (stderr);
4376 DumpUserSymbols (pfx, stderr);
4377 (void) DumpRPN (pfx, stderr);
4378 }
4379
4380 {
4381 size_t number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
4382 ssize_t t;
4383
4384 pfx->fxrts = (fxRtT *)AcquireQuantumMemory (number_threads, sizeof(fxRtT));
4385 if (!pfx->fxrts) {
4386 (void) ThrowMagickException (
4387 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
4388 "fxrts", "%lu",
4389 (unsigned long) number_threads);
4390 (void) DestroyRPN (pfx);
4391 pfx->expression = DestroyString (pfx->expression);
4392 pfx->pex = NULL;
4393 (void) DeInitFx (pfx);
4394 pfx = (FxInfo*) RelinquishMagickMemory(pfx);
4395 return NULL;
4396 }
4397 for (t=0; t < (ssize_t) number_threads; t++) {
4398 if (!AllocFxRt (pfx, &pfx->fxrts[t])) {
4399 (void) ThrowMagickException (
4400 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
4401 "AllocFxRt t=", "%g",
4402 (double) t);
4403 {
4404 ssize_t t2;
4405 for (t2 = t-1; t2 >= 0; t2--) {
4406 DestroyFxRt (&pfx->fxrts[t2]);
4407 }
4408 }
4409 pfx->fxrts = (fxRtT *) RelinquishMagickMemory (pfx->fxrts);
4410 (void) DestroyRPN (pfx);
4411 pfx->expression = DestroyString (pfx->expression);
4412 pfx->pex = NULL;
4413 (void) DeInitFx (pfx);
4414 pfx = (FxInfo*) RelinquishMagickMemory(pfx);
4415 return NULL;
4416 }
4417 }
4418 }
4419 return pfx;
4420}
4421
4422FxInfo *AcquireFxInfo (const Image * images, const char * expression, ExceptionInfo *exception)
4423{
4424 return AcquireFxInfoPrivate (images, expression, MagickFalse, exception);
4425}
4426
4427FxInfo *DestroyFxInfo (FxInfo * pfx)
4428{
4429 ssize_t t;
4430
4431 assert (pfx != NULL);
4432 assert (pfx->image != NULL);
4433 assert (pfx->Images != NULL);
4434 assert (pfx->Imgs != NULL);
4435 assert (pfx->fxrts != NULL);
4436
4437 for (t=0; t < (ssize_t) GetMagickResourceLimit(ThreadResource); t++) {
4438 DestroyFxRt (&pfx->fxrts[t]);
4439 }
4440 pfx->fxrts = (fxRtT *) RelinquishMagickMemory (pfx->fxrts);
4441
4442 DestroyRPN (pfx);
4443
4444 pfx->expression = DestroyString (pfx->expression);
4445 pfx->pex = NULL;
4446
4447 (void) DeInitFx (pfx);
4448
4449 pfx = (FxInfo*) RelinquishMagickMemory(pfx);
4450
4451 return NULL;
4452}
4453
4454/* Following is substitute for FxImage().
4455*/
4456MagickExport Image *FxImage(const Image *image,const char *expression,
4457 ExceptionInfo *exception)
4458{
4459#define FxImageTag "FxNew/Image"
4460
4461 CacheView
4462 *fx_view,
4463 *image_view;
4464
4465 Image
4466 *fx_image;
4467
4468 MagickBooleanType
4469 status;
4470
4471 MagickOffsetType
4472 progress;
4473
4474 ssize_t
4475 y;
4476
4477 FxInfo
4478 *pfx;
4479
4480 assert(image != (Image *) NULL);
4481 assert(image->signature == MagickCoreSignature);
4482 if (IsEventLogging() != MagickFalse)
4483 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4484 if (expression == (const char *) NULL)
4485 return(CloneImage(image,0,0,MagickTrue,exception));
4486 fx_image=CloneImage(image,0,0,MagickTrue,exception);
4487 if (!fx_image) return NULL;
4488 if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse) {
4489 fx_image=DestroyImage(fx_image);
4490 return NULL;
4491 }
4492
4493 pfx = AcquireFxInfoPrivate (image, expression, MagickTrue, exception);
4494
4495 if (!pfx) {
4496 fx_image=DestroyImage(fx_image);
4497 return NULL;
4498 }
4499
4500 assert (pfx->image != NULL);
4501 assert (pfx->Images != NULL);
4502 assert (pfx->Imgs != NULL);
4503 assert (pfx->fxrts != NULL);
4504
4505 status=MagickTrue;
4506 progress=0;
4507 image_view = AcquireVirtualCacheView (image, pfx->exception);
4508 fx_view = AcquireAuthenticCacheView (fx_image, pfx->exception);
4509#if defined(MAGICKCORE_OPENMP_SUPPORT)
4510 #pragma omp parallel for schedule(dynamic) shared(progress,status) \
4511 magick_number_threads(image,fx_image,fx_image->rows, \
4512 pfx->ContainsDebug ? 0 : 1)
4513#endif
4514 for (y=0; y < (ssize_t) fx_image->rows; y++)
4515 {
4516 const int
4517 id = GetOpenMPThreadId();
4518
4519 const Quantum
4520 *magick_restrict p;
4521
4522 Quantum
4523 *magick_restrict q;
4524
4525 ssize_t
4526 x;
4527
4528 fxFltType
4529 result = 0.0;
4530
4531 if (status == MagickFalse)
4532 continue;
4533 p = GetCacheViewVirtualPixels (image_view, 0, y, image->columns, 1, pfx->exception);
4534 q = QueueCacheViewAuthenticPixels (fx_view, 0, y, fx_image->columns, 1, pfx->exception);
4535 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL)) {
4536 status=MagickFalse;
4537 continue;
4538 }
4539 for (x=0; x < (ssize_t) fx_image->columns; x++) {
4540 ssize_t i;
4541
4542 pfx->fxrts[id].thisPixel = (Quantum *)p;
4543
4544 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4545 {
4546 PixelChannel channel = GetPixelChannelChannel (image, i);
4547 PixelTrait traits = GetPixelChannelTraits (image, channel);
4548 PixelTrait fx_traits = GetPixelChannelTraits (fx_image, channel);
4549 if ((traits == UndefinedPixelTrait) ||
4550 (fx_traits == UndefinedPixelTrait))
4551 continue;
4552 if ((fx_traits & CopyPixelTrait) != 0) {
4553 SetPixelChannel (fx_image, channel, p[i], q);
4554 continue;
4555 }
4556
4557 if (!ExecuteRPN (pfx, &pfx->fxrts[id], &result, channel, x, y)) {
4558 status=MagickFalse;
4559 break;
4560 }
4561
4562 q[i] = ClampToQuantum ((MagickRealType) (QuantumRange*result));
4563 }
4564 if (status == MagickFalse)
4565 break;
4566 p+=(ptrdiff_t) GetPixelChannels (image);
4567 q+=(ptrdiff_t) GetPixelChannels (fx_image);
4568 }
4569 if (SyncCacheViewAuthenticPixels(fx_view, pfx->exception) == MagickFalse)
4570 status=MagickFalse;
4571 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4572 {
4573 MagickBooleanType
4574 proceed;
4575
4576#if defined(MAGICKCORE_OPENMP_SUPPORT)
4577 #pragma omp atomic
4578#endif
4579 progress++;
4580 proceed = SetImageProgress (image, FxImageTag, progress, image->rows);
4581 if (proceed == MagickFalse)
4582 status=MagickFalse;
4583 }
4584 }
4585
4586 fx_view = DestroyCacheView (fx_view);
4587 image_view = DestroyCacheView (image_view);
4588
4589 /* Before destroying the user symbol values, dump them to stderr.
4590 */
4591 if (pfx->DebugOpt && pfx->usedUserSymbols) {
4592 int t, i;
4593 char UserSym[MagickPathExtent];
4594 fprintf (stderr, "User symbols (%i):\n", pfx->usedUserSymbols);
4595 for (t=0; t < (int) GetMagickResourceLimit(ThreadResource); t++) {
4596 for (i = 0; i < (int) pfx->usedUserSymbols; i++) {
4597 fprintf (stderr, "th=%i us=%i '%s': %.*Lg\n",
4598 t, i, NameOfUserSym (pfx, i, UserSym), pfx->precision, pfx->fxrts[t].UserSymVals[i]);
4599 }
4600 }
4601 }
4602
4603 if ((status == MagickFalse) || (pfx->exception->severity >= ErrorException))
4604 fx_image=DestroyImage(fx_image);
4605
4606 pfx=DestroyFxInfo(pfx);
4607
4608 return(fx_image);
4609}
Definition fx.c:609
Definition fx.c:579
Definition fx.c:673
Definition fx.c:463
Definition fx.c:711
Definition fx.c:530
Definition fx.c:603
Definition fx.c:725
Definition fx.c:716