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"
104#define MaxTokenLen 100
106#define TableExtend 0.1
107#define InitNumOprStack 50
108#define MinValStackSize 100
109#define InitNumUserSymbols 50
111#if defined(MAGICKCORE_WINDOWS_SUPPORT)
119#define SECONDS_ERR -FLT_MAX
121typedef long double fxFltType;
177 {oAddEq,
"+=", 12, 1},
178 {oSubtractEq,
"-=", 12, 1},
179 {oMultiplyEq,
"*=", 13, 1},
180 {oDivideEq,
"/=", 13, 1},
181 {oPlusPlus,
"++", 12, 0},
182 {oSubSub,
"--", 12, 0},
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},
193 {oNotEq,
"!=", 9, 2},
194 {oLtEq,
"<=", 10, 2},
195 {oGtEq,
">=", 10, 2},
198 {oLogAnd,
"&&", 6, 2},
199 {oLogOr,
"||", 5, 2},
200 {oLogNot,
"!", 16, 1},
201 {oBitAnd,
"&", 8, 2},
203 {oBitNot,
"~", 16, 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}
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"}
254#define FirstFunc ((FunctionE) (oNull+1))
258#if defined(MAGICKCORE_HAVE_ACOSH)
262#if defined(MAGICKCORE_HAVE_J1)
266#if defined(MAGICKCORE_HAVE_ASINH)
270#if defined(MAGICKCORE_HAVE_ATANH)
282#if defined(MAGICKCORE_HAVE_ERF)
293#if defined(MAGICKCORE_HAVE_J0)
296#if defined(MAGICKCORE_HAVE_J1)
299#if defined(MAGICKCORE_HAVE_J1)
351#if defined(MAGICKCORE_HAVE_ACOSH)
352 {fAcosh,
"acosh" , 1},
355#if defined(MAGICKCORE_HAVE_J1)
359#if defined(MAGICKCORE_HAVE_ASINH)
360 {fAsinh,
"asinh" , 1},
363#if defined(MAGICKCORE_HAVE_ATANH)
364 {fAtanh,
"atanh" , 1},
366 {fAtan2,
"atan2" , 2},
369 {fChannel,
"channel", 5},
370 {fClamp,
"clamp" , 1},
373 {fDebug,
"debug" , 1},
375#if defined(MAGICKCORE_HAVE_ERF)
378 {fEpoch,
"epoch" , 1},
380 {fFloor,
"floor" , 1},
381 {fGauss,
"gauss" , 1},
383 {fHypot,
"hypot" , 2},
385 {fIsnan,
"isnan" , 1},
386#if defined(MAGICKCORE_HAVE_J0)
389#if defined(MAGICKCORE_HAVE_J1)
392#if defined(MAGICKCORE_HAVE_J1)
396 {fLogtwo,
"logtwo", 1},
398 {fMagickTime,
"magicktime", 0},
405 {fRound,
"round" , 1},
411 {fSquish,
"squish", 1},
414 {fTrunc,
"trunc" , 1},
418 {fWhile,
"while", 2},
431#define FirstImgAttr ((ImgAttrE) (fNull+1))
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}
507#define FirstSym ((SymbolE) (aNull+1))
538static const SymbolT Symbols[] = {
540 {sIntensity,
"intensity"},
541 {sLightness,
"lightness"},
543 {sLuminance,
"luminance"},
544 {sSaturation,
"saturation"},
565#define FirstCont (sNull+1)
592 {rGotoChk,
"gotochk", 0},
593 {rIfZeroGoto,
"ifzerogoto", 1},
594 {rIfNotZeroGoto,
"ifnotzerogoto", 1},
595 {rCopyFrom,
"copyfrom", 0},
596 {rCopyTo,
"copyto", 1},
597 {rZerStk,
"zerstk", 0},
601#define NULL_ADDRESS -2
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))
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},
663static const char * sElementTypes[] = {
720 fxFltType * ValStack;
721 fxFltType * UserSymVals;
729 MagickBooleanType NeedStats;
730 MagickBooleanType GotStats;
731 MagickBooleanType NeedHsl;
732 MagickBooleanType DebugOpt;
733 MagickBooleanType ContainsDebug;
736 char ShortExp[MagickPathExtent];
738 char token[MagickPathExtent];
749 OperatorE * OperatorStack;
755 **magick_restrict random_infos;
767static MagickBooleanType TranslateStatementList
768 (
FxInfo * pfx,
const char * strLimit,
char * chLimit);
770static MagickBooleanType TranslateExpression
771 (
FxInfo * pfx,
const char * strLimit,
char * chLimit, MagickBooleanType * needPopAll);
773static MagickBooleanType GetFunction (
FxInfo * pfx, FunctionE fe);
775static inline MagickBooleanType ChanIsVirtual (PixelChannel pc)
777 if (pc==HUE_CHANNEL || pc==SAT_CHANNEL || pc==LIGHT_CHANNEL || pc==INTENSITY_CHANNEL)
783static MagickBooleanType InitFx (
FxInfo * pfx,
const Image * img,
789 pfx->ImgListLen = GetImageListLength (img);
790 pfx->ImgNum = GetImageIndexInList (img);
791 pfx->image = (
Image *)img;
793 pfx->NeedStats = MagickFalse;
794 pfx->GotStats = MagickFalse;
795 pfx->NeedHsl = MagickFalse;
796 pfx->DebugOpt = IsStringTrue (GetImageArtifact (img,
"fx:debug"));
797 pfx->statistics = 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));
807 (void) ThrowMagickException (
808 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
810 (
unsigned long) pfx->ImgListLen);
814 next = GetFirstImageInList (img);
815 for ( ; next != (
Image *) NULL; next=next->next)
817 ImgT * pimg = &pfx->Imgs[i];
818 pimg->View = AcquireVirtualCacheView (next, pfx->exception);
820 (void) ThrowMagickException (
821 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
825 for ( ; i > 0; i--) {
826 pimg = &pfx->Imgs[i-1];
827 pimg->View = DestroyCacheView (pimg->View);
829 pfx->Imgs=(
ImgT *) RelinquishMagickMemory (pfx->Imgs);
835 pfx->Images = ImageListToArray (img, pfx->exception);
840static MagickBooleanType DeInitFx (
FxInfo * pfx)
844 if (pfx->Images) pfx->Images = (
Image**) RelinquishMagickMemory (pfx->Images);
847 for (i = (ssize_t)GetImageListLength(pfx->image); i > 0; i--) {
848 ImgT * pimg = &pfx->Imgs[i-1];
849 pimg->View = DestroyCacheView (pimg->View);
851 pfx->Imgs=(
ImgT *) RelinquishMagickMemory (pfx->Imgs);
853 pfx->random_infos = DestroyRandomInfoTLS (pfx->random_infos);
855 if (pfx->statistics) {
856 for (i = (ssize_t)GetImageListLength(pfx->image); i > 0; i--) {
857 pfx->statistics[i-1]=(
ChannelStatistics *) RelinquishMagickMemory (pfx->statistics[i-1]);
866static ElementTypeE TypeOfOpr (
int op)
868 if (op < oNull)
return etOperator;
869 if (op == oNull)
return etConstant;
870 if (op <= fNull)
return etFunction;
871 if (op <= aNull)
return etImgAttr;
872 if (op <= sNull)
return etSymbol;
873 if (op <= rNull)
return etControl;
875 return (ElementTypeE) 0;
878static char * SetPtrShortExp (
FxInfo * pfx,
char * pExp,
size_t len)
885 *pfx->ShortExp =
'\0';
888 slen = CopyMagickString (pfx->ShortExp, pExp, len);
890 (void) CopyMagickString (pfx->ShortExp+MaxLen,
"...", 4);
892 p = strchr (pfx->ShortExp,
'\n');
893 if (p) (void) CopyMagickString (p,
"...", 4);
894 p = strchr (pfx->ShortExp,
'\r');
895 if (p) (void) CopyMagickString (p,
"...", 4);
897 return pfx->ShortExp;
900static char * SetShortExp (
FxInfo * pfx)
902 return SetPtrShortExp (pfx, pfx->pex, MaxTokenLen-1);
905static int FindUserSymbol (
FxInfo * pfx,
char * name)
912 lenName = strlen (name);
913 for (i=0; i < pfx->usedUserSymbols; i++) {
915 if (lenName == pus->len && LocaleNCompare (name, pus->pex, lenName)==0)
break;
917 if (i == pfx->usedUserSymbols)
return NULL_ADDRESS;
921static MagickBooleanType ExtendUserSymbols (
FxInfo * pfx)
923 pfx->numUserSymbols = (int) ceil (pfx->numUserSymbols * (1 + TableExtend));
924 pfx->UserSymbols = (
UserSymbolT*) ResizeMagickMemory (pfx->UserSymbols, (
size_t) pfx->numUserSymbols *
sizeof(
UserSymbolT));
925 if (!pfx->UserSymbols) {
926 (void) ThrowMagickException (
927 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
929 pfx->numUserSymbols);
936static int AddUserSymbol (
FxInfo * pfx,
char * pex,
size_t len)
939 if (++pfx->usedUserSymbols >= pfx->numUserSymbols) {
940 if (!ExtendUserSymbols (pfx))
return -1;
942 pus = &pfx->UserSymbols[pfx->usedUserSymbols-1];
946 return pfx->usedUserSymbols-1;
949static void DumpTables (FILE * fh)
953 for (i=0; i <= rNull; i++) {
954 const char * str =
"";
955 if ( i < oNull) str = Operators[i].str;
956 if (i >= (
int) FirstFunc && i < fNull) str = Functions[i-(int) FirstFunc].str;
957 if (i >= (
int) FirstImgAttr && i < aNull) str = ImgAttrs[i-(
int) FirstImgAttr].str;
958 if (i >= (
int) FirstSym && i < sNull) str = Symbols[i-(
int) FirstSym].str;
959 if (i >= (
int) FirstCont && i < rNull) str = Controls[i-(
int) FirstCont].str;
960 if (i==0 ) fprintf (stderr,
"Operators:\n ");
961 else if (i==oNull) fprintf (stderr,
"\nFunctions:\n ");
962 else if (i==fNull) fprintf (stderr,
"\nImage attributes:\n ");
963 else if (i==aNull) fprintf (stderr,
"\nSymbols:\n ");
964 else if (i==sNull) fprintf (stderr,
"\nControls:\n ");
965 fprintf (fh,
" %s", str);
970static char * NameOfUserSym (
FxInfo * pfx,
int ndx,
char * buf)
973 assert (ndx >= 0 && ndx < pfx->usedUserSymbols);
974 pus = &pfx->UserSymbols[ndx];
975 (void) CopyMagickString (buf, pus->pex, pus->len+1);
979static void DumpUserSymbols (
FxInfo * pfx, FILE * fh)
981 char UserSym[MagickPathExtent];
983 fprintf (fh,
"UserSymbols (%i)\n", pfx->usedUserSymbols);
984 for (i=0; i < pfx->usedUserSymbols; i++) {
985 fprintf (fh,
" %i: '%s'\n", i, NameOfUserSym (pfx, i, UserSym));
989static MagickBooleanType BuildRPN (
FxInfo * pfx)
991 pfx->numUserSymbols = InitNumUserSymbols;
992 pfx->usedUserSymbols = 0;
993 pfx->UserSymbols = (
UserSymbolT*) AcquireMagickMemory ((
size_t) pfx->numUserSymbols *
sizeof(
UserSymbolT));
994 if (!pfx->UserSymbols) {
995 (void) ThrowMagickException (
996 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
998 pfx->numUserSymbols);
1002 pfx->numElements = RpnInit;
1003 pfx->usedElements = 0;
1004 pfx->Elements = NULL;
1006 pfx->Elements = (
ElementT*) AcquireMagickMemory ((
size_t) pfx->numElements *
sizeof(
ElementT));
1008 if (!pfx->Elements) {
1009 (void) ThrowMagickException (
1010 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
1016 pfx->usedOprStack = 0;
1017 pfx->maxUsedOprStack = 0;
1018 pfx->numOprStack = InitNumOprStack;
1019 pfx->OperatorStack = (OperatorE*) AcquireMagickMemory ((
size_t) pfx->numOprStack *
sizeof(OperatorE));
1020 if (!pfx->OperatorStack) {
1021 (void) ThrowMagickException (
1022 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
1023 "OperatorStack",
"%i",
1031static MagickBooleanType AllocFxRt (
FxInfo * pfx,
fxRtT * pfxrt)
1035 pfxrt->random_info = AcquireRandomInfo ();
1036 pfxrt->thisPixel = NULL;
1038 nRnd = 20 + 10 * (int) GetPseudoRandomValue (pfxrt->random_info);
1039 for (i=0; i < nRnd; i++) (
void) GetPseudoRandomValue (pfxrt->random_info);;
1041 pfxrt->usedValStack = 0;
1042 pfxrt->numValStack = 2 * pfx->maxUsedOprStack;
1043 if (pfxrt->numValStack < MinValStackSize) pfxrt->numValStack = MinValStackSize;
1044 pfxrt->ValStack = (fxFltType*) AcquireMagickMemory ((
size_t) pfxrt->numValStack *
sizeof(fxFltType));
1045 if (!pfxrt->ValStack) {
1046 (void) ThrowMagickException (
1047 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
1049 pfxrt->numValStack);
1053 pfxrt->UserSymVals = NULL;
1055 if (pfx->usedUserSymbols) {
1056 pfxrt->UserSymVals = (fxFltType*) AcquireMagickMemory ((
size_t) pfx->usedUserSymbols *
sizeof(fxFltType));
1057 if (!pfxrt->UserSymVals) {
1058 (void) ThrowMagickException (
1059 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
1060 "UserSymVals",
"%i",
1061 pfx->usedUserSymbols);
1064 for (i = 0; i < pfx->usedUserSymbols; i++) pfxrt->UserSymVals[i] = (fxFltType) 0;
1070static MagickBooleanType ExtendRPN (
FxInfo * pfx)
1072 pfx->numElements = (int) ceil (pfx->numElements * (1 + TableExtend));
1073 pfx->Elements = (
ElementT*) ResizeMagickMemory (pfx->Elements, (
size_t) pfx->numElements *
sizeof(
ElementT));
1074 if (!pfx->Elements) {
1075 (void) ThrowMagickException (
1076 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
1084static inline MagickBooleanType OprInPlace (
int op)
1086 return (op >= oAddEq && op <= oSubSub ? MagickTrue : MagickFalse);
1089static const char * OprStr (
int oprNum)
1092 if (oprNum < 0) str =
"bad OprStr";
1093 else if (oprNum <= oNull) str = Operators[oprNum].str;
1094 else if (oprNum <= fNull) str = Functions[oprNum-(int) FirstFunc].str;
1095 else if (oprNum <= aNull) str = ImgAttrs[oprNum-(int) FirstImgAttr].str;
1096 else if (oprNum <= sNull) str = Symbols[oprNum-(int) FirstSym].str;
1097 else if (oprNum <= rNull) str = Controls[oprNum-(int) FirstCont].str;
1104static MagickBooleanType DumpRPN (
FxInfo * pfx, FILE * fh)
1108 fprintf (fh,
"DumpRPN:");
1109 fprintf (fh,
" numElements=%i", pfx->numElements);
1110 fprintf (fh,
" usedElements=%i", pfx->usedElements);
1111 fprintf (fh,
" maxUsedOprStack=%i", pfx->maxUsedOprStack);
1112 fprintf (fh,
" ImgListLen=%g", (
double) pfx->ImgListLen);
1113 fprintf (fh,
" NeedStats=%s", pfx->NeedStats ?
"yes" :
"no");
1114 fprintf (fh,
" GotStats=%s", pfx->GotStats ?
"yes" :
"no");
1115 fprintf (fh,
" NeedHsl=%s\n", pfx->NeedHsl ?
"yes" :
"no");
1116 if (pfx->runType==rtEntireImage) fprintf (stderr,
"EntireImage");
1117 else if (pfx->runType==rtCornerOnly) fprintf (stderr,
"CornerOnly");
1121 for (i=0; i < pfx->usedElements; i++) {
1122 ElementT * pel = &pfx->Elements[i];
1123 pel->number_dest = 0;
1125 for (i=0; i < pfx->usedElements; i++) {
1126 ElementT * pel = &pfx->Elements[i];
1127 if (pel->operator_index == rGoto || pel->operator_index == rGotoChk || pel->operator_index == rIfZeroGoto || pel->operator_index == rIfNotZeroGoto) {
1128 if (pel->element_index >= 0 && pel->element_index < pfx->numElements) {
1129 ElementT * pelDest = &pfx->Elements[pel->element_index];
1130 pelDest->number_dest++;
1134 for (i=0; i < pfx->usedElements; i++) {
1135 char UserSym[MagickPathExtent];
1137 ElementT * pel = &pfx->Elements[i];
1138 const char * str = OprStr (pel->operator_index);
1139 const char *sRelAbs =
"";
1141 if (pel->operator_index == fP || pel->operator_index == fUP || pel->operator_index == fVP || pel->operator_index == fSP)
1142 sRelAbs = pel->is_relative ?
"[]" :
"{}";
1144 if (pel->type == etColourConstant)
1145 fprintf (fh,
" %i: %s vals=%.*Lg,%.*Lg,%.*Lg '%s%s' nArgs=%i ndx=%i %s",
1146 i, sElementTypes[pel->type],
1147 pfx->precision, pel->val, pfx->precision, pel->val1, pfx->precision, pel->val2,
1148 str, sRelAbs, pel->number_args, pel->element_index,
1149 pel->do_push ?
"push" :
"NO push");
1151 fprintf (fh,
" %i: %s val=%.*Lg '%s%s' nArgs=%i ndx=%i %s",
1152 i, sElementTypes[pel->type], pfx->precision, pel->val, str, sRelAbs,
1153 pel->number_args, pel->element_index,
1154 pel->do_push ?
"push" :
"NO push");
1156 if (pel->img_attr_qual != aNull)
1157 fprintf (fh,
" ia=%s", OprStr((
int) pel->img_attr_qual));
1159 if (pel->channel_qual != NO_CHAN_QUAL) {
1160 if (pel->channel_qual == THIS_CHANNEL) fprintf (stderr,
" ch=this");
1161 else fprintf (stderr,
" ch=%i", pel->channel_qual);
1164 if (pel->operator_index == rCopyTo) {
1165 fprintf (fh,
" CopyTo ==> %s", NameOfUserSym (pfx, pel->element_index, UserSym));
1166 }
else if (pel->operator_index == rCopyFrom) {
1167 fprintf (fh,
" CopyFrom <== %s", NameOfUserSym (pfx, pel->element_index, UserSym));
1168 }
else if (OprInPlace (pel->operator_index)) {
1169 fprintf (fh,
" <==> %s", NameOfUserSym (pfx, pel->element_index, UserSym));
1171 if (pel->number_dest > 0) fprintf (fh,
" <==dest(%i)", pel->number_dest);
1177static void DestroyRPN (
FxInfo * pfx)
1179 pfx->numOprStack = 0;
1180 pfx->usedOprStack = 0;
1181 if (pfx->OperatorStack) pfx->OperatorStack = (OperatorE*) RelinquishMagickMemory (pfx->OperatorStack);
1183 pfx->numElements = 0;
1184 pfx->usedElements = 0;
1185 if (pfx->Elements) pfx->Elements = (
ElementT*) RelinquishMagickMemory (pfx->Elements);
1187 pfx->usedUserSymbols = 0;
1188 if (pfx->UserSymbols) pfx->UserSymbols = (
UserSymbolT*) RelinquishMagickMemory (pfx->UserSymbols);
1191static void DestroyFxRt (
fxRtT * pfxrt)
1193 pfxrt->usedValStack = 0;
1194 if (pfxrt->ValStack) pfxrt->ValStack = (fxFltType*) RelinquishMagickMemory (pfxrt->ValStack);
1195 if (pfxrt->UserSymVals) pfxrt->UserSymVals = (fxFltType*) RelinquishMagickMemory (pfxrt->UserSymVals);
1197 pfxrt->random_info = DestroyRandomInfo (pfxrt->random_info);
1200static size_t GetToken (
FxInfo * pfx)
1211 char * p = pfx->pex;
1215 if (!isalpha((
int)*p))
return 0;
1222 if (LocaleNCompare (p,
"icc-", 4) == 0) {
1225 while (isalpha ((
int)*p)) { len++; p++; }
1226 }
else if (LocaleNCompare (p,
"device-", 7) == 0) {
1229 while (isalpha ((
int)*p)) { len++; p++; }
1231 while (isalpha ((
int)*p)) { len++; p++; }
1232 if (*p ==
'_') { len++; p++; }
1233 while (isalpha ((
int)*p)) { len++; p++; }
1234 while (isdigit ((
int)*p)) { len++; p++; }
1236 if (len >= MaxTokenLen) {
1237 (void) ThrowMagickException (
1238 pfx->exception, GetMagickModule(), OptionError,
1239 "GetToken: too long",
"%g at '%s'",
1240 (double) len, SetShortExp(pfx));
1244 (void) CopyMagickString (pfx->token, pfx->pex, (len+1<MaxTokenLen)?len+1:MaxTokenLen);
1247 pfx->lenToken = strlen (pfx->token);
1251static MagickBooleanType TokenMaybeUserSymbol (
FxInfo * pfx)
1253 char * p = pfx->token;
1256 if (!isalpha ((
int)*p++))
return MagickFalse;
1259 if (i < 2)
return MagickFalse;
1263static MagickBooleanType AddElement (
FxInfo * pfx, fxFltType val,
int oprNum)
1267 assert (oprNum <= rNull);
1269 if (++pfx->usedElements >= pfx->numElements) {
1270 if (!ExtendRPN (pfx))
return MagickFalse;
1273 pel = &pfx->Elements[pfx->usedElements-1];
1274 pel->type = TypeOfOpr (oprNum);
1276 pel->val1 = (fxFltType) 0;
1277 pel->val2 = (fxFltType) 0;
1278 pel->operator_index = oprNum;
1279 pel->do_push = MagickTrue;
1280 pel->element_index = 0;
1281 pel->channel_qual = NO_CHAN_QUAL;
1282 pel->img_attr_qual = aNull;
1283 pel->number_dest = 0;
1284 pel->exp_start = NULL;
1287 if (oprNum <= oNull) pel->number_args = Operators[oprNum].number_args;
1288 else if (oprNum <= fNull) pel->number_args = Functions[oprNum-(int) FirstFunc].number_args;
1289 else if (oprNum <= aNull) pel->number_args = 0;
1290 else if (oprNum <= sNull) pel->number_args = 0;
1291 else pel->number_args = Controls[oprNum-(int) FirstCont].number_args;
1296static MagickBooleanType AddAddressingElement (
FxInfo * pfx,
int oprNum,
int EleNdx)
1299 if (!AddElement (pfx, (fxFltType) 0, oprNum))
return MagickFalse;
1300 pel = &pfx->Elements[pfx->usedElements-1];
1301 pel->element_index = EleNdx;
1302 if (oprNum == rGoto || oprNum == rGotoChk || oprNum == rIfZeroGoto || oprNum == rIfNotZeroGoto
1303 || oprNum == rZerStk)
1305 pel->do_push = MagickFalse;
1315static MagickBooleanType AddColourElement (
FxInfo * pfx, fxFltType val0, fxFltType val1, fxFltType val2)
1318 if (!AddElement (pfx, val0, oNull))
return MagickFalse;
1319 pel = &pfx->Elements[pfx->usedElements-1];
1322 pel->type = etColourConstant;
1326static inline void SkipSpaces (
FxInfo * pfx)
1328 while (isspace ((
int)*pfx->pex)) pfx->pex++;
1331static inline char PeekChar (
FxInfo * pfx)
1337static inline MagickBooleanType PeekStr (
FxInfo * pfx,
const char * str)
1341 return (LocaleNCompare (pfx->pex, str, strlen(str))==0 ? MagickTrue : MagickFalse);
1344static MagickBooleanType ExpectChar (
FxInfo * pfx,
char c)
1346 if (PeekChar (pfx) != c) {
1347 (void) ThrowMagickException (
1348 pfx->exception, GetMagickModule(), OptionError,
1349 "Expected char",
"'%c' at '%s'", c, SetShortExp (pfx));
1356static int MaybeXYWH (
FxInfo * pfx, ImgAttrE * pop)
1363 if (*pop != aPage && *pop != aPrintsize && *pop != aRes)
return 0;
1365 if (PeekChar (pfx) !=
'.')
return 0;
1367 if (!ExpectChar (pfx,
'.'))
return 0;
1369 (void) GetToken (pfx);
1370 if (LocaleCompare (
"x", pfx->token)==0) ret=1;
1371 else if (LocaleCompare (
"y", pfx->token)==0) ret=2;
1372 else if (LocaleCompare (
"width", pfx->token)==0) ret=3;
1373 else if (LocaleCompare (
"height", pfx->token)==0) ret=4;
1376 (void) ThrowMagickException (
1377 pfx->exception, GetMagickModule(), OptionError,
1378 "Invalid 'x' or 'y' or 'width' or 'height' token=",
"'%s' at '%s'",
1379 pfx->token, SetShortExp(pfx));
1381 if (*pop == aPage) (*pop) = (ImgAttrE) ((
int) *pop + ret);
1384 (void) ThrowMagickException (
1385 pfx->exception, GetMagickModule(), OptionError,
1386 "Invalid 'width' or 'height' token=",
"'%s' at '%s'",
1387 pfx->token, SetShortExp(pfx));
1389 (*pop) = (ImgAttrE) ((
int) *pop + ret);
1392 pfx->pex+=pfx->lenToken;
1397static MagickBooleanType ExtendOperatorStack (
FxInfo * pfx)
1399 pfx->numOprStack = (int) ceil (pfx->numOprStack * (1 + TableExtend));
1400 pfx->OperatorStack = (OperatorE*) ResizeMagickMemory (pfx->OperatorStack, (
size_t) pfx->numOprStack *
sizeof(OperatorE));
1401 if (!pfx->OperatorStack) {
1402 (void) ThrowMagickException (
1403 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
1411static MagickBooleanType PushOperatorStack (
FxInfo * pfx,
int op)
1413 if (++pfx->usedOprStack >= pfx->numOprStack) {
1414 if (!ExtendOperatorStack (pfx))
1417 pfx->OperatorStack[pfx->usedOprStack-1] = (OperatorE) op;
1419 if (pfx->maxUsedOprStack < pfx->usedOprStack)
1420 pfx->maxUsedOprStack = pfx->usedOprStack;
1424static OperatorE GetLeadingOp (
FxInfo * pfx)
1426 OperatorE op = oNull;
1428 if (*pfx->pex ==
'-') op = oUnaryMinus;
1429 else if (*pfx->pex ==
'+') op = oUnaryPlus;
1430 else if (*pfx->pex ==
'~') op = oBitNot;
1431 else if (*pfx->pex ==
'!') op = oLogNot;
1432 else if (*pfx->pex ==
'(') op = oOpenParen;
1437static inline MagickBooleanType OprIsUnaryPrefix (OperatorE op)
1439 return (op == oUnaryMinus || op == oUnaryPlus || op == oBitNot || op == oLogNot ? MagickTrue : MagickFalse);
1442static MagickBooleanType TopOprIsUnaryPrefix (
FxInfo * pfx)
1444 if (!pfx->usedOprStack)
return MagickFalse;
1446 return OprIsUnaryPrefix (pfx->OperatorStack[pfx->usedOprStack-1]);
1449static MagickBooleanType PopOprOpenParen (
FxInfo * pfx, OperatorE op)
1452 if (!pfx->usedOprStack)
return MagickFalse;
1454 if (pfx->OperatorStack[pfx->usedOprStack-1] != op)
return MagickFalse;
1456 pfx->usedOprStack--;
1461static int GetCoordQualifier (
FxInfo * pfx,
int op)
1465 if (op != fU && op != fV && op != fS)
return -1;
1467 (void) GetToken (pfx);
1469 if (pfx->lenToken != 1) {
1472 if (*pfx->token !=
'p' && *pfx->token !=
'P')
return -1;
1473 if (!GetFunction (pfx, fP))
return -1;
1478static PixelChannel GetChannelQualifier (
FxInfo * pfx,
int op)
1480 if (op == fU || op == fV || op == fP ||
1481 op == fUP || op == fVP ||
1482 op == fS || (op >= (
int) FirstImgAttr && op <= aNull)
1485 const ChannelT * pch = &Channels[0];
1486 (void) GetToken (pfx);
1489 if (LocaleCompare (pch->str, pfx->token)==0) {
1491 if (op >= (
int) FirstImgAttr && op <= (int) ((OperatorE)aNull) &&
1492 ChanIsVirtual (pch->pixel_channel)
1495 (void) ThrowMagickException (
1496 pfx->exception, GetMagickModule(), OptionError,
1497 "Can't have image attribute with channel qualifier at",
"'%s' at '%s'",
1498 pfx->token, SetShortExp(pfx));
1499 return NO_CHAN_QUAL;
1502 pfx->pex += pfx->lenToken;
1503 return pch->pixel_channel;
1508 return NO_CHAN_QUAL;
1511static ImgAttrE GetImgAttrToken (
FxInfo * pfx)
1513 ImgAttrE ia = aNull;
1515 for (ia = FirstImgAttr; ia < aNull; ia=(ImgAttrE) (ia+1)) {
1516 iaStr = ImgAttrs[ia-(int) FirstImgAttr].str;
1517 if (LocaleCompare (iaStr, pfx->token)==0) {
1518 pfx->pex += strlen(pfx->token);
1519 if (ImgAttrs[ia-(
int) FirstImgAttr].need_stats != MagickFalse) pfx->NeedStats = MagickTrue;
1520 MaybeXYWH (pfx, &ia);
1525 if (ia == aPage || ia == aPrintsize || ia == aRes) {
1526 (void) ThrowMagickException (
1527 pfx->exception, GetMagickModule(), OptionError,
1528 "Attribute",
"'%s' needs qualifier at '%s'",
1529 iaStr, SetShortExp(pfx));
1535static ImgAttrE GetImgAttrQualifier (
FxInfo * pfx,
int op)
1537 ImgAttrE ia = aNull;
1538 if (op == (OperatorE)fU || op == (OperatorE)fV || op == (OperatorE)fP || op == (OperatorE)fS) {
1539 (void) GetToken (pfx);
1540 if (pfx->lenToken == 0) {
1543 ia = GetImgAttrToken (pfx);
1548static MagickBooleanType IsQualifier (
FxInfo * pfx)
1550 if (PeekChar (pfx) ==
'.') {
1557static MagickBooleanType ParseISO860(
const char* text,
struct tm* tp)
1567 memset(tp,0,
sizeof(
struct tm));
1568 if (MagickSscanf(text,
"%d-%d-%dT%d:%d:%d",&year,&month,&day,&hour,&min,&sec) != 6)
1569 return(MagickFalse);
1570 tp->tm_year=year-1900;
1580static ssize_t GetProperty (
FxInfo * pfx, fxFltType *val, fxFltType *seconds)
1587 if (seconds != NULL) *seconds = SECONDS_ERR;
1589 if (PeekStr (pfx,
"%[")) {
1592 char sProperty [MagickPathExtent];
1593 char * p = pfx->pex + 2;
1597 if (*p ==
'[') level++;
1598 else if (*p ==
']') {
1599 if (level == 0)
break;
1604 if (!*p || level != 0) {
1605 (void) ThrowMagickException (
1606 pfx->exception, GetMagickModule(), OptionError,
1607 "After '%[' expected ']' at",
"'%s'",
1612 len = (size_t) (p - pfx->pex + 1);
1613 if (len > MaxTokenLen) {
1614 (void) ThrowMagickException (
1615 pfx->exception, GetMagickModule(), OptionError,
1616 "Too much text between '%[' and ']' at",
"'%s'",
1621 (void) CopyMagickString (sProperty, pfx->pex, len+1);
1622 sProperty[len] =
'\0';
1626 text = InterpretImageProperties (pfx->image->image_info, pfx->image,
1627 sProperty, pfx->exception);
1628 if (!text || !*text) {
1629 text = DestroyString(text);
1630 (void) ThrowMagickException (
1631 pfx->exception, GetMagickModule(), OptionError,
1632 "Unknown property",
"'%s' at '%s'",
1633 sProperty, SetShortExp(pfx));
1637 if (seconds != NULL) {
1639 if (ParseISO860(text,&tp) == MagickFalse) {
1640 (void) ThrowMagickException (
1641 pfx->exception, GetMagickModule(), OptionError,
1642 "Function 'epoch' expected date property, found ",
"'%s' at '%s'",
1643 text, SetShortExp(pfx));
1644 text = DestroyString(text);
1645 *seconds = SECONDS_ERR;
1648 *seconds = (fxFltType)mktime (&tp);
1651 *val = strtold (text, &tailptr);
1652 if (text == tailptr) {
1653 text = DestroyString(text);
1654 (void) ThrowMagickException (
1655 pfx->exception, GetMagickModule(), OptionError,
1656 "Property",
"'%s' text '%s' is not a number at '%s'",
1657 sProperty, text, SetShortExp(pfx));
1658 text = DestroyString(text);
1662 text = DestroyString(text);
1664 return ((ssize_t) len);
1670static inline ssize_t GetConstantColour (
FxInfo * pfx, fxFltType *v0, fxFltType *v1, fxFltType *v2)
1681 *dummy_exception = AcquireExceptionInfo ();
1691 char ColSp[MagickPathExtent];
1692 (void) CopyMagickString (ColSp, pfx->token, MaxTokenLen);
1693 p = ColSp + pfx->lenToken - 1;
1694 if (*p ==
'a' || *p ==
'A') *p =
'\0';
1696 (void) GetPixelInfo (pfx->image, &colour);
1700 IsGray = (LocaleCompare (ColSp,
"gray") == 0) ? MagickTrue : MagickFalse;
1701 IsIcc = (LocaleCompare (ColSp,
"icc-color") == 0) ? MagickTrue : MagickFalse;
1702 IsDev = (LocaleNCompare (ColSp,
"device-", 7) == 0) ? MagickTrue : MagickFalse;
1706 if (!QueryColorCompliance (pfx->token, AllCompliance, &colour, dummy_exception) || IsGray) {
1707 ssize_t type = ParseCommandOption (MagickColorspaceOptions, MagickFalse, ColSp);
1708 if (type >= 0 || IsIcc || IsDev) {
1709 char * q = pfx->pex + pfx->lenToken;
1710 while (isspace((
int) ((
unsigned char) *q))) q++;
1713 char sFunc[MagickPathExtent];
1714 while (*q && *q !=
')') q++;
1716 (void) ThrowMagickException (
1717 pfx->exception, GetMagickModule(), OptionError,
1718 "constant color missing ')'",
"at '%s'",
1720 dummy_exception = DestroyExceptionInfo (dummy_exception);
1723 lenfun = (size_t) (q - pfx->pex + 1);
1724 if (lenfun > MaxTokenLen) {
1725 (void) ThrowMagickException (
1726 pfx->exception, GetMagickModule(), OptionError,
1727 "lenfun too long",
"'%lu' at '%s'",
1728 (
unsigned long) lenfun, SetShortExp(pfx));
1729 dummy_exception = DestroyExceptionInfo (dummy_exception);
1732 (void) CopyMagickString (sFunc, pfx->pex, lenfun+1);
1733 if (QueryColorCompliance (sFunc, AllCompliance, &colour, dummy_exception)) {
1734 *v0 = QuantumScale*colour.red;
1735 *v1 = QuantumScale*colour.green;
1736 *v2 = QuantumScale*colour.blue;
1737 dummy_exception = DestroyExceptionInfo (dummy_exception);
1738 return (ssize_t)lenfun;
1741 (void) ThrowMagickException (
1742 pfx->exception, GetMagickModule(), OptionError,
1743 "colorspace but not a valid color with '(...)' at",
"'%s'",
1745 dummy_exception = DestroyExceptionInfo (dummy_exception);
1750 dummy_exception = DestroyExceptionInfo (dummy_exception);
1755 *v0 = QuantumScale*colour.red;
1756 *v1 = QuantumScale*colour.green;
1757 *v2 = QuantumScale*colour.blue;
1759 dummy_exception = DestroyExceptionInfo (dummy_exception);
1760 return (ssize_t)strlen (pfx->token);
1763static inline ssize_t GetHexColour (
FxInfo * pfx, fxFltType *v0, fxFltType *v1, fxFltType *v2)
1772 if (*pfx->pex !=
'#')
return 0;
1776 while (isxdigit ((
int)*p)) p++;
1777 if (isalpha ((
int)*p)) {
1778 (void) ThrowMagickException (
1779 pfx->exception, GetMagickModule(), OptionError,
1780 "Bad hex number at",
"'%s'",
1785 len = (size_t) (p - pfx->pex);
1786 if (len < 1)
return 0;
1787 if (len >= MaxTokenLen) {
1788 (void) ThrowMagickException (
1789 pfx->exception, GetMagickModule(), OptionError,
1790 "Hex colour too long at",
"'%s'",
1794 (void) CopyMagickString (pfx->token, pfx->pex, len+1);
1796 (void) GetPixelInfo (pfx->image, &colour);
1798 if (!QueryColorCompliance (pfx->token, AllCompliance, &colour, pfx->exception)) {
1799 (void) ThrowMagickException (
1800 pfx->exception, GetMagickModule(), OptionError,
1801 "QueryColorCompliance rejected",
"'%s' at '%s'",
1802 pfx->token, SetShortExp(pfx));
1806 *v0 = QuantumScale*colour.red;
1807 *v1 = QuantumScale*colour.green;
1808 *v2 = QuantumScale*colour.blue;
1810 return (ssize_t) len;
1813static MagickBooleanType GetFunction (
FxInfo * pfx, FunctionE fe)
1817 const char * funStr = Functions[fe-(int) FirstFunc].str;
1818 int nArgs = Functions[fe-(int) FirstFunc].number_args;
1820 char expChLimit =
')';
1821 const char *strLimit =
",)";
1822 OperatorE pushOp = oOpenParen;
1829 int ndx0 = NULL_ADDRESS, ndx1 = NULL_ADDRESS, ndx2 = NULL_ADDRESS, ndx3 = NULL_ADDRESS;
1831 MagickBooleanType coordQual = MagickFalse;
1832 PixelChannel chQual = NO_CHAN_QUAL;
1833 ImgAttrE iaQual = aNull;
1835 pfx->pex += pfx->lenToken;
1838 char p = PeekChar (pfx);
1840 (void) ExpectChar (pfx,
'{');
1841 pushOp = oOpenBrace;
1845 }
else if (p==
'[') {
1846 (void) ExpectChar (pfx,
'[');
1847 pushOp = oOpenBracket;
1856 }
else if (fe == fU) {
1857 char p = PeekChar (pfx);
1859 (void) ExpectChar (pfx,
'[');
1860 pushOp = oOpenBracket;
1869 }
else if (fe == fV || fe == fS) {
1871 pushOp = oOpenBracket;
1875 if (!ExpectChar (pfx,
'('))
return MagickFalse;
1877 if (!PushOperatorStack (pfx, (
int) pushOp))
return MagickFalse;
1879 pExpStart = pfx->pex;
1880 ndx0 = pfx->usedElements;
1882 (void) AddAddressingElement (pfx, rGoto, NULL_ADDRESS);
1889 lenOptArt = GetProperty (pfx, &val, &seconds);
1890 if (seconds == SECONDS_ERR) {
1892 (void) ThrowMagickException (
1893 pfx->exception, GetMagickModule(), OptionError,
1894 "Function 'epoch' expected date property",
"at '%s'",
1898 if (lenOptArt < 0)
return MagickFalse;
1899 if (lenOptArt > 0) {
1900 (void) AddElement (pfx, seconds, oNull);
1901 pfx->pex += lenOptArt;
1902 if (!ExpectChar (pfx,
')'))
return MagickFalse;
1903 if (!PopOprOpenParen (pfx, pushOp))
return MagickFalse;
1910 if (TranslateStatementList (pfx, strLimit, &chLimit)) {
1914 (void) ThrowMagickException (
1915 pfx->exception, GetMagickModule(), OptionError,
1916 "For function",
"'%s' expected ')' at '%s'",
1917 funStr, SetShortExp(pfx));
1921 if (!chLimit)
break;
1922 if (fe == fP || fe == fS|| fe == fIf) {
1923 (void) AddElement (pfx, (fxFltType) 0, oNull);
1928 if (strchr (strLimit, chLimit)==NULL) {
1929 (void) ThrowMagickException (
1930 pfx->exception, GetMagickModule(), OptionError,
1931 "For function",
"'%s' expected one of '%s' after expression but found '%c' at '%s'",
1932 funStr, strLimit, chLimit ? chLimit :
' ', SetShortExp(pfx));
1941 if (ndx1 != NULL_ADDRESS) {
1942 (void) ThrowMagickException (
1943 pfx->exception, GetMagickModule(), OptionError,
1944 "For function",
"'%s' required argument is missing at '%s'",
1945 funStr, SetShortExp(pfx));
1948 ndx1 = pfx->usedElements;
1949 if (fe==fWhile || fe==fIf) {
1950 (void) AddAddressingElement (pfx, rIfZeroGoto, NULL_ADDRESS);
1951 }
else if (fe==fDo) {
1952 (void) AddAddressingElement (pfx, rIfZeroGoto, NULL_ADDRESS);
1953 }
else if (fe==fFor) {
1954 pfx->Elements[pfx->usedElements-1].do_push = MagickFalse;
1958 if (ndx2 != NULL_ADDRESS) {
1959 (void) ThrowMagickException (
1960 pfx->exception, GetMagickModule(), OptionError,
1961 "For function",
"'%s' required argument is missing at '%s'",
1962 funStr, SetShortExp(pfx));
1965 ndx2 = pfx->usedElements;
1967 pfx->Elements[pfx->usedElements-1].do_push = MagickFalse;
1968 (void) AddAddressingElement (pfx, rGotoChk, ndx0);
1969 }
else if (fe==fDo) {
1970 pfx->Elements[pfx->usedElements-1].do_push = MagickFalse;
1971 (void) AddAddressingElement (pfx, rGotoChk, ndx0 + 1);
1972 }
else if (fe==fFor) {
1973 (void) AddAddressingElement (pfx, rIfZeroGoto, NULL_ADDRESS);
1974 pfx->Elements[pfx->usedElements-1].do_push = MagickTrue;
1975 (void) AddAddressingElement (pfx, rZerStk, NULL_ADDRESS);
1976 }
else if (fe==fIf) {
1977 (void) AddAddressingElement (pfx, rGoto, NULL_ADDRESS);
1981 if (ndx3 != NULL_ADDRESS) {
1982 (void) ThrowMagickException (
1983 pfx->exception, GetMagickModule(), OptionError,
1984 "For function",
"'%s' required argument is missing at '%s'",
1985 funStr, SetShortExp(pfx));
1989 pfx->Elements[pfx->usedElements-1].do_push = MagickFalse;
1990 (void) AddAddressingElement (pfx, rGotoChk, ndx1);
1992 ndx3 = pfx->usedElements;
1997 if (chLimit == expChLimit) {
1998 lenExp = (size_t) (pfx->pex - pExpStart - 1);
2002 if (chLimit && chLimit != expChLimit && chLimit !=
',' ) {
2003 (void) ThrowMagickException (
2004 pfx->exception, GetMagickModule(), OptionError,
2005 "For function",
"'%s' expected '%c', found '%c' at '%s'",
2006 funStr, expChLimit, chLimit ? chLimit :
' ', SetShortExp(pfx));
2010 if (fe == fP || fe == fS || fe == fU || fe == fChannel) {
2011 while (FndArgs < Functions[fe-(
int) FirstFunc].number_args) {
2012 (void) AddElement (pfx, (fxFltType) 0, oNull);
2017 if (FndArgs > Functions[fe-(
int) FirstFunc].number_args)
2020 (void) ThrowMagickException (
2021 pfx->exception, GetMagickModule(), OptionError,
2022 "For function",
"'%s' expected up to %i arguments, found '%i' at '%s'",
2023 funStr, Functions[fe-(int) FirstFunc].number_args, FndArgs, SetShortExp(pfx));
2025 (void) ThrowMagickException (
2026 pfx->exception, GetMagickModule(), OptionError,
2027 "For function",
"'%s' expected %i arguments, found '%i' at '%s'",
2028 funStr, Functions[fe-(int) FirstFunc].number_args, FndArgs, SetShortExp(pfx));
2032 if (FndArgs < Functions[fe-(
int) FirstFunc].number_args) {
2033 (void) ThrowMagickException (
2034 pfx->exception, GetMagickModule(), OptionError,
2035 "For function",
"'%s' expected %i arguments, found too few (%i) at '%s'",
2036 funStr, Functions[fe-(int) FirstFunc].number_args, FndArgs, SetShortExp(pfx));
2039 if (fe != fS && fe != fV && FndArgs == 0 && Functions[fe-(
int) FirstFunc].number_args == 0) {
2041 chLimit = expChLimit;
2042 if (!ExpectChar (pfx,
')'))
return MagickFalse;
2045 if (chLimit != expChLimit) {
2046 (void) ThrowMagickException (
2047 pfx->exception, GetMagickModule(), OptionError,
2048 "For function",
"'%s', arguments don't end with '%c' at '%s'",
2049 funStr, expChLimit, SetShortExp(pfx));
2052 if (!PopOprOpenParen (pfx, pushOp)) {
2053 (void) ThrowMagickException (
2054 pfx->exception, GetMagickModule(), OptionError,
2055 "Bug: For function",
"'%s' tos not '%s' at '%s'",
2056 funStr, Operators[pushOp].str, SetShortExp(pfx));
2060 if (IsQualifier (pfx)) {
2062 if (fe == fU || fe == fV || fe == fS) {
2064 coordQual = (GetCoordQualifier (pfx, (
int) fe) == 1) ? MagickTrue : MagickFalse;
2069 ElementT * pel = &pfx->Elements[pfx->usedElements-1];
2070 if (pel->operator_index != fP) {
2071 (void) ThrowMagickException (
2072 pfx->exception, GetMagickModule(), OptionError,
2073 "Bug: For function",
"'%s' last element not 'p' at '%s'",
2074 funStr, SetShortExp(pfx));
2077 chQual = pel->channel_qual;
2078 expChLimit = (pel->is_relative) ?
']' :
'}';
2079 pfx->usedElements--;
2080 if (fe == fU) fe = fUP;
2081 else if (fe == fV) fe = fVP;
2082 else if (fe == fS) fe = fSP;
2083 funStr = Functions[fe-(int) FirstFunc].str;
2087 if ( chQual == NO_CHAN_QUAL &&
2088 (fe == fP || fe == fS || fe == fSP || fe == fU || fe == fUP || fe == fV || fe == fVP)
2091 chQual = GetChannelQualifier (pfx, (
int) fe);
2094 if (chQual == NO_CHAN_QUAL && (fe == fU || fe == fV || fe == fS)) {
2096 iaQual = GetImgAttrQualifier (pfx, (
int) fe);
2098 if (IsQualifier (pfx) && chQual == NO_CHAN_QUAL && iaQual != aNull) {
2099 chQual = GetChannelQualifier (pfx, (
int) fe);
2101 if (coordQual && iaQual != aNull) {
2102 (void) ThrowMagickException (
2103 pfx->exception, GetMagickModule(), OptionError,
2104 "For function",
"'%s', can't have qualifiers 'p' and image attribute '%s' at '%s'",
2105 funStr, pfx->token, SetShortExp(pfx));
2108 if (!coordQual && chQual == NO_CHAN_QUAL && iaQual == aNull) {
2109 (void) ThrowMagickException (
2110 pfx->exception, GetMagickModule(), OptionError,
2111 "For function",
"'%s', bad qualifier '%s' at '%s'",
2112 funStr, pfx->token, SetShortExp(pfx));
2115 if (!coordQual && chQual == CompositePixelChannel && iaQual == aNull) {
2116 (void) ThrowMagickException (
2117 pfx->exception, GetMagickModule(), OptionError,
2118 "For function",
"'%s', bad composite qualifier '%s' at '%s'",
2119 funStr, pfx->token, SetShortExp(pfx));
2123 if (chQual == HUE_CHANNEL || chQual == SAT_CHANNEL || chQual == LIGHT_CHANNEL) {
2124 pfx->NeedHsl = MagickTrue;
2126 if (iaQual >= FirstImgAttr && iaQual < aNull) {
2127 (void) ThrowMagickException (
2128 pfx->exception, GetMagickModule(), OptionError,
2129 "Can't have image attribute with HLS qualifier at",
"'%s'",
2136 if (iaQual != aNull && chQual != NO_CHAN_QUAL) {
2137 if (ImgAttrs[iaQual-(
int) FirstImgAttr].need_stats == MagickFalse) {
2138 (void) ThrowMagickException (
2139 pfx->exception, GetMagickModule(), OptionError,
2140 "Can't have image attribute ",
"'%s' with channel qualifier '%s' at '%s'",
2141 ImgAttrs[iaQual-(int) FirstImgAttr].str,
2142 pfx->token, SetShortExp(pfx));
2145 if (ChanIsVirtual (chQual)) {
2146 (void) ThrowMagickException (
2147 pfx->exception, GetMagickModule(), OptionError,
2148 "Can't have statistical image attribute ",
"'%s' with virtual channel qualifier '%s' at '%s'",
2149 ImgAttrs[iaQual-(int) FirstImgAttr].str,
2150 pfx->token, SetShortExp(pfx));
2157 pfx->Elements[ndx1].element_index = ndx2+1;
2158 }
else if (fe==fDo) {
2159 pfx->Elements[ndx0].element_index = ndx1+1;
2160 pfx->Elements[ndx1].element_index = ndx2+1;
2161 }
else if (fe==fFor) {
2162 pfx->Elements[ndx2].element_index = ndx3;
2163 }
else if (fe==fIf) {
2164 pfx->Elements[ndx1].element_index = ndx2 + 1;
2165 pfx->Elements[ndx2].element_index = ndx3;
2167 if (fe == fU && iaQual == aNull) {
2168 ElementT * pel = &pfx->Elements[pfx->usedElements-1];
2169 if (pel->type == etConstant && pel->val == 0.0) {
2170 pfx->usedElements--;
2174 (void) AddElement (pfx, (fxFltType) 0, (int) fe);
2175 if (fe == fP || fe == fU || fe == fU0 || fe == fUP ||
2176 fe == fV || fe == fVP || fe == fS || fe == fSP)
2178 ElementT * pel = &pfx->Elements[pfx->usedElements-1];
2179 pel->is_relative = (expChLimit ==
']' ? MagickTrue : MagickFalse);
2180 if (chQual >= 0) pel->channel_qual = chQual;
2181 if (iaQual != aNull && (fe == fU || fe == fV || fe == fS)) {
2183 pel->img_attr_qual = iaQual;
2188 if (pExpStart && lenExp) {
2189 ElementT * pel = &pfx->Elements[pfx->usedElements-1];
2190 pel->exp_start = pExpStart;
2191 pel->exp_len = lenExp;
2195 pfx->ContainsDebug = MagickTrue;
2200static MagickBooleanType IsStealth (
int op)
2202 return (op == fU0 || op == fUP || op == fSP || op == fVP ||
2203 (op >= FirstCont && op <= rNull) ? MagickTrue : MagickFalse
2207static MagickBooleanType GetOperand (
2208 FxInfo * pfx, MagickBooleanType * UserSymbol, MagickBooleanType * NewUserSymbol,
int * UserSymNdx,
2209 MagickBooleanType * needPopAll)
2212 *NewUserSymbol = *UserSymbol = MagickFalse;
2213 *UserSymNdx = NULL_ADDRESS;
2216 if (!*pfx->pex)
return MagickFalse;
2217 (void) GetToken (pfx);
2219 if (pfx->lenToken==0) {
2223 OperatorE op = GetLeadingOp (pfx);
2224 if (op==oOpenParen) {
2225 char chLimit =
'\0';
2226 if (!PushOperatorStack (pfx, (
int) op))
return MagickFalse;
2228 if (!TranslateExpression (pfx,
")", &chLimit, needPopAll)) {
2229 (void) ThrowMagickException (
2230 pfx->exception, GetMagickModule(), OptionError,
2231 "Empty expression in parentheses at",
"'%s'",
2235 if (chLimit !=
')') {
2236 (void) ThrowMagickException (
2237 pfx->exception, GetMagickModule(), OptionError,
2238 "'(' but no ')' at",
"'%s'",
2243 if (!PopOprOpenParen (pfx, oOpenParen)) {
2244 (void) ThrowMagickException (
2245 pfx->exception, GetMagickModule(), OptionError,
2246 "Bug: tos not '(' at",
"'%s'",
2251 }
else if (OprIsUnaryPrefix (op)) {
2252 if (!PushOperatorStack (pfx, (
int) op))
return MagickFalse;
2255 if (!*pfx->pex)
return MagickFalse;
2257 if (!GetOperand (pfx, UserSymbol, NewUserSymbol, UserSymNdx, needPopAll)) {
2258 (void) ThrowMagickException (
2259 pfx->exception, GetMagickModule(), OptionError,
2260 "After unary, bad operand at",
"'%s'",
2265 if (*NewUserSymbol) {
2266 (void) ThrowMagickException (
2267 pfx->exception, GetMagickModule(), OptionError,
2268 "After unary, NewUserSymbol at",
"'%s'",
2274 (void) AddAddressingElement (pfx, rCopyFrom, *UserSymNdx);
2275 *UserSymNdx = NULL_ADDRESS;
2277 *UserSymbol = MagickFalse;
2278 *NewUserSymbol = MagickFalse;
2281 (void) GetToken (pfx);
2283 }
else if (*pfx->pex ==
'#') {
2284 fxFltType v0=0, v1=0, v2=0;
2285 ssize_t lenToken = GetHexColour (pfx, &v0, &v1, &v2);
2287 (void) ThrowMagickException (
2288 pfx->exception, GetMagickModule(), OptionError,
2289 "Bad hex number at",
"'%s'",
2292 }
else if (lenToken > 0) {
2293 (void) AddColourElement (pfx, v0, v1, v2);
2304 fxFltType val = strtold (pfx->pex, &tailptr);
2305 if (pfx->pex != tailptr) {
2313 const char Prefixes[] =
"yzafpnum.kMGTPEZY";
2314 const char * pSi = strchr (Prefixes, *tailptr);
2315 if (pSi && *pSi !=
'.') Pow = (double) ((pSi - Prefixes) * 3 - 24);
2316 else if (*tailptr ==
'c') Pow = -2;
2317 else if (*tailptr ==
'h') Pow = 2;
2318 else if (*tailptr ==
'k') Pow = 3;
2320 if (*(++pfx->pex) ==
'i') {
2321 val *= pow (2.0, Pow/0.3);
2324 val *= pow (10.0, Pow);
2328 (void) AddElement (pfx, val, oNull);
2332 val = (fxFltType) 0;
2333 lenOptArt = GetProperty (pfx, &val, NULL);
2334 if (lenOptArt < 0)
return MagickFalse;
2335 if (lenOptArt > 0) {
2336 (void) AddElement (pfx, val, oNull);
2337 pfx->pex += lenOptArt;
2344 if (pfx->lenToken > 0) {
2349 for (ce = (ConstantE)0; ce < cNull; ce=(ConstantE) (ce+1)) {
2350 const char * ceStr = Constants[ce].str;
2351 if (LocaleCompare (ceStr, pfx->token)==0) {
2357 (void) AddElement (pfx, Constants[ce].val, oNull);
2358 pfx->pex += pfx->lenToken;
2367 for (fe = FirstFunc; fe < fNull; fe=(FunctionE) (fe+1)) {
2368 const char * feStr = Functions[fe-(int) FirstFunc].str;
2369 if (LocaleCompare (feStr, pfx->token)==0) {
2374 if (fe == fV && pfx->ImgListLen < 2) {
2375 (void) ThrowMagickException (
2376 pfx->exception, GetMagickModule(), OptionError,
2377 "Symbol 'v' but fewer than two images at",
"'%s'",
2382 if (IsStealth ((
int) fe)) {
2383 (void) ThrowMagickException (
2384 pfx->exception, GetMagickModule(), OptionError,
2385 "Function",
"'%s' not permitted at '%s'",
2386 pfx->token, SetShortExp(pfx));
2389 if (fe == fDo || fe == fFor || fe == fIf || fe == fWhile) {
2390 *needPopAll = MagickTrue;
2393 if (fe != fNull)
return (GetFunction (pfx, fe));
2399 ImgAttrE ia = GetImgAttrToken (pfx);
2402 (void) AddElement (pfx, val, (
int) ia);
2404 if (ImgAttrs[ia-(
int) FirstImgAttr].need_stats != MagickFalse) {
2405 if (IsQualifier (pfx)) {
2406 PixelChannel chQual = GetChannelQualifier (pfx, (
int) ia);
2408 if (chQual == NO_CHAN_QUAL) {
2409 (void) ThrowMagickException (
2410 pfx->exception, GetMagickModule(), OptionError,
2411 "Bad channel qualifier at",
"'%s'",
2416 pel = &pfx->Elements[pfx->usedElements-1];
2417 pel->channel_qual = chQual;
2428 for (se = FirstSym; se < sNull; se=(SymbolE) (se+1)) {
2429 const char * seStr = Symbols[se-(int) FirstSym].str;
2430 if (LocaleCompare (seStr, pfx->token)==0) {
2436 (void) AddElement (pfx, val, (
int) se);
2437 pfx->pex += pfx->lenToken;
2439 if (se==sHue || se==sSaturation || se==sLightness) pfx->NeedHsl = MagickTrue;
2447 fxFltType v0, v1, v2;
2448 ssize_t ColLen = GetConstantColour (pfx, &v0, &v1, &v2);
2449 if (ColLen < 0)
return MagickFalse;
2451 (void) AddColourElement (pfx, v0, v1, v2);
2460 const char *artifact;
2461 artifact = GetImageArtifact (pfx->image, pfx->token);
2462 if (artifact != (
const char *) NULL) {
2464 fxFltType val = strtold (artifact, &tailptr);
2465 if (pfx->token == tailptr) {
2466 (void) ThrowMagickException (
2467 pfx->exception, GetMagickModule(), OptionError,
2468 "Artifact",
"'%s' has value '%s', not a number, at '%s'",
2469 pfx->token, artifact, SetShortExp(pfx));
2472 (void) AddElement (pfx, val, oNull);
2473 pfx->pex+=pfx->lenToken;
2480 if (TokenMaybeUserSymbol (pfx)) {
2481 *UserSymbol = MagickTrue;
2482 *UserSymNdx = FindUserSymbol (pfx, pfx->token);
2483 if (*UserSymNdx == NULL_ADDRESS) {
2484 *UserSymNdx = AddUserSymbol (pfx, pfx->pex, pfx->lenToken);
2485 *NewUserSymbol = MagickTrue;
2488 pfx->pex += pfx->lenToken;
2494 (void) ThrowMagickException (
2495 pfx->exception, GetMagickModule(), OptionError,
2496 "Expected operand at",
"'%s'",
2502static inline MagickBooleanType IsRealOperator (OperatorE op)
2504 return (op < oOpenParen || op > oCloseBrace) ? MagickTrue : MagickFalse;
2507static inline MagickBooleanType ProcessTernaryOpr (
FxInfo * pfx,
TernaryT * ptern)
2512 if (pfx->usedOprStack == 0)
2514 if (pfx->OperatorStack[pfx->usedOprStack-1] == oQuery) {
2515 if (ptern->addr_query != NULL_ADDRESS) {
2516 (void) ThrowMagickException (
2517 pfx->exception, GetMagickModule(), OptionError,
2518 "Already have '?' in sub-expression at",
"'%s'",
2522 if (ptern->addr_colon != NULL_ADDRESS) {
2523 (void) ThrowMagickException (
2524 pfx->exception, GetMagickModule(), OptionError,
2525 "Already have ':' in sub-expression at",
"'%s'",
2529 pfx->usedOprStack--;
2530 ptern->addr_query = pfx->usedElements;
2531 (void) AddAddressingElement (pfx, rIfZeroGoto, NULL_ADDRESS);
2534 else if (pfx->OperatorStack[pfx->usedOprStack-1] == oColon) {
2535 if (ptern->addr_query == NULL_ADDRESS) {
2536 (void) ThrowMagickException (
2537 pfx->exception, GetMagickModule(), OptionError,
2538 "Need '?' in sub-expression at",
"'%s'",
2542 if (ptern->addr_colon != NULL_ADDRESS) {
2543 (void) ThrowMagickException (
2544 pfx->exception, GetMagickModule(), OptionError,
2545 "Already have ':' in sub-expression at",
"'%s'",
2549 pfx->usedOprStack--;
2550 ptern->addr_colon = pfx->usedElements;
2551 pfx->Elements[pfx->usedElements-1].do_push = MagickTrue;
2552 (void) AddAddressingElement (pfx, rGoto, NULL_ADDRESS);
2558static MagickBooleanType GetOperator (
2560 MagickBooleanType * Assign, MagickBooleanType * Update, MagickBooleanType * IncrDecr)
2564 MagickBooleanType DoneIt = MagickFalse;
2566 for (op = (OperatorE)0; op != oNull; op=(OperatorE) (op+1)) {
2567 const char * opStr = Operators[op].str;
2568 len = strlen(opStr);
2569 if (LocaleNCompare (opStr, pfx->pex, len)==0) {
2574 if (!IsRealOperator (op)) {
2575 (void) ThrowMagickException (
2576 pfx->exception, GetMagickModule(), OptionError,
2577 "Not a real operator at",
"'%s'",
2583 (void) ThrowMagickException (
2584 pfx->exception, GetMagickModule(), OptionError,
2585 "Expected operator at",
"'%s'",
2590 *Assign = (op==oAssign) ? MagickTrue : MagickFalse;
2591 *Update = OprInPlace ((
int) op);
2592 *IncrDecr = (op == oPlusPlus || op == oSubSub) ? MagickTrue : MagickFalse;
2599 while (pfx->usedOprStack > 0) {
2600 OperatorE top = pfx->OperatorStack[pfx->usedOprStack-1];
2601 int precTop, precNew;
2602 if (top == oOpenParen || top == oAssign || OprInPlace ((
int) top))
break;
2603 precTop = Operators[top].precedence;
2604 precNew = Operators[op].precedence;
2608 if (precTop < precNew)
break;
2609 (void) AddElement (pfx, (fxFltType) 0, (int) top);
2610 pfx->usedOprStack--;
2616 if (op==oCloseParen) {
2617 if (pfx->usedOprStack == 0) {
2618 (void) ThrowMagickException (
2619 pfx->exception, GetMagickModule(), OptionError,
2620 "Found ')' but nothing on stack at",
"'%s'",
2625 if (pfx->OperatorStack[pfx->usedOprStack-1] != oOpenParen) {
2626 (void) ThrowMagickException (
2627 pfx->exception, GetMagickModule(), OptionError,
2628 "Found ')' but no '(' on stack at",
"'%s'",
2632 pfx->usedOprStack--;
2633 DoneIt = MagickTrue;
2637 if (!PushOperatorStack (pfx, (
int) op))
return MagickFalse;
2645static MagickBooleanType ResolveTernaryAddresses (
FxInfo * pfx,
TernaryT * ptern)
2647 if (ptern->addr_query == NULL_ADDRESS && ptern->addr_colon == NULL_ADDRESS)
2650 if (ptern->addr_query != NULL_ADDRESS && ptern->addr_colon != NULL_ADDRESS) {
2651 pfx->Elements[ptern->addr_query].element_index = ptern->addr_colon + 1;
2652 pfx->Elements[ptern->addr_colon].element_index = pfx->usedElements;
2653 ptern->addr_query = NULL_ADDRESS;
2654 ptern->addr_colon = NULL_ADDRESS;
2655 }
else if (ptern->addr_query != NULL_ADDRESS) {
2656 (void) ThrowMagickException (
2657 pfx->exception, GetMagickModule(), OptionError,
2658 "'?' with no corresponding ':'",
"'%s' at '%s'",
2659 pfx->token, SetShortExp(pfx));
2661 }
else if (ptern->addr_colon != NULL_ADDRESS) {
2662 (void) ThrowMagickException (
2663 pfx->exception, GetMagickModule(), OptionError,
2664 "':' with no corresponding '?'",
"'%s' at '%s'",
2665 pfx->token, SetShortExp(pfx));
2671static MagickBooleanType TranslateExpression (
2672 FxInfo * pfx,
const char * strLimit,
char * chLimit, MagickBooleanType * needPopAll)
2676 MagickBooleanType UserSymbol, NewUserSymbol;
2677 int UserSymNdx0, UserSymNdx1;
2680 Assign = MagickFalse,
2681 Update = MagickFalse,
2682 IncrDecr = MagickFalse;
2687 ternary.addr_query = NULL_ADDRESS;
2688 ternary.addr_colon = NULL_ADDRESS;
2694 StartEleNdx = pfx->usedElements-1;
2695 if (StartEleNdx < 0) StartEleNdx = 0;
2704 if (strchr(strLimit,*pfx->pex)!=NULL) {
2705 *chLimit = *pfx->pex;
2712 if (!GetOperand (pfx, &UserSymbol, &NewUserSymbol, &UserSymNdx0, needPopAll))
return MagickFalse;
2717 while (*pfx->pex && (!*strLimit || (strchr(strLimit,*pfx->pex)==NULL))) {
2718 if (!GetOperator (pfx, &Assign, &Update, &IncrDecr))
return MagickFalse;
2720 if (NewUserSymbol && !Assign) {
2721 (void) ThrowMagickException (
2722 pfx->exception, GetMagickModule(), OptionError,
2723 "Expected assignment after new UserSymbol",
"'%s' at '%s'",
2724 pfx->token, SetShortExp(pfx));
2727 if (!UserSymbol && Assign) {
2728 (void) ThrowMagickException (
2729 pfx->exception, GetMagickModule(), OptionError,
2730 "Attempted assignment to non-UserSymbol",
"'%s' at '%s'",
2731 pfx->token, SetShortExp(pfx));
2734 if (!UserSymbol && Update) {
2735 (void) ThrowMagickException (
2736 pfx->exception, GetMagickModule(), OptionError,
2737 "Attempted update to non-UserSymbol",
"'%s' at '%s'",
2738 pfx->token, SetShortExp(pfx));
2741 if (UserSymbol && (Assign || Update) && !IncrDecr) {
2743 if (!TranslateExpression (pfx, strLimit, chLimit, needPopAll))
return MagickFalse;
2744 if (!*pfx->pex)
break;
2745 if (!*strLimit)
break;
2746 if (strchr(strLimit,*chLimit)!=NULL)
break;
2748 if (UserSymbol && !Assign && !Update && UserSymNdx0 != NULL_ADDRESS) {
2750 (void) AddAddressingElement (pfx, rCopyFrom, UserSymNdx0);
2751 UserSymNdx0 = NULL_ADDRESS;
2752 pel = &pfx->Elements[pfx->usedElements-1];
2753 pel->do_push = MagickTrue;
2757 while (TopOprIsUnaryPrefix (pfx)) {
2758 OperatorE op = pfx->OperatorStack[pfx->usedOprStack-1];
2759 (void) AddElement (pfx, (fxFltType) 0, (int) op);
2760 pfx->usedOprStack--;
2764 if (!ProcessTernaryOpr (pfx, &ternary))
return MagickFalse;
2766 if (ternary.addr_colon != NULL_ADDRESS) {
2767 if (!TranslateExpression (pfx,
",);", chLimit, needPopAll))
return MagickFalse;
2771 UserSymbol = NewUserSymbol = MagickFalse;
2773 if ( (!*pfx->pex) || (*strLimit && (strchr(strLimit,*pfx->pex)!=NULL) ) )
2775 if (IncrDecr)
break;
2777 (void) ThrowMagickException (
2778 pfx->exception, GetMagickModule(), OptionError,
2779 "Expected operand after operator",
"at '%s'",
2785 (void) ThrowMagickException (
2786 pfx->exception, GetMagickModule(), OptionError,
2787 "'++' and '--' must be the final operators in an expression at",
"'%s'",
2792 if (!GetOperand (pfx, &UserSymbol, &NewUserSymbol, &UserSymNdx1, needPopAll)) {
2793 (void) ThrowMagickException (
2794 pfx->exception, GetMagickModule(), OptionError,
2795 "Expected operand at",
"'%s'",
2800 if (NewUserSymbol && !Assign) {
2801 (void) ThrowMagickException (
2802 pfx->exception, GetMagickModule(), OptionError,
2803 "NewUserSymbol",
"'%s' after non-assignment operator at '%s'",
2804 pfx->token, SetShortExp(pfx));
2807 if (UserSymbol && !NewUserSymbol) {
2808 (void) AddAddressingElement (pfx, rCopyFrom, UserSymNdx1);
2809 UserSymNdx1 = NULL_ADDRESS;
2811 UserSymNdx0 = UserSymNdx1;
2814 if (UserSymbol && !Assign && !Update && UserSymNdx0 != NULL_ADDRESS) {
2816 if (NewUserSymbol) {
2817 (void) ThrowMagickException (
2818 pfx->exception, GetMagickModule(), OptionError,
2819 "NewUserSymbol",
"'%s' needs assignment operator at '%s'",
2820 pfx->token, SetShortExp(pfx));
2823 (void) AddAddressingElement (pfx, rCopyFrom, UserSymNdx0);
2824 pel = &pfx->Elements[pfx->usedElements-1];
2825 pel->do_push = MagickTrue;
2828 if (*pfx->pex && !*chLimit && (strchr(strLimit,*pfx->pex)!=NULL)) {
2829 *chLimit = *pfx->pex;
2832 while (pfx->usedOprStack) {
2833 OperatorE op = pfx->OperatorStack[pfx->usedOprStack-1];
2834 if (op == oOpenParen || op == oOpenBracket || op == oOpenBrace) {
2837 if ( (op==oAssign && !Assign) || (OprInPlace((
int) op) && !Update) ) {
2840 pfx->usedOprStack--;
2841 (void) AddElement (pfx, (fxFltType) 0, (int) op);
2842 if (op == oAssign) {
2843 if (UserSymNdx0 < 0) {
2844 (void) ThrowMagickException (
2845 pfx->exception, GetMagickModule(), OptionError,
2846 "Assignment to unknown user symbol at",
"'%s'",
2852 pfx->usedElements--;
2853 (void) AddAddressingElement (pfx, rCopyTo, UserSymNdx0);
2855 }
else if (OprInPlace ((
int) op)) {
2856 if (UserSymNdx0 < 0) {
2857 (void) ThrowMagickException (
2858 pfx->exception, GetMagickModule(), OptionError,
2859 "Operator-in-place to unknown user symbol at",
"'%s'",
2865 pfx->Elements[pfx->usedElements-1].element_index = UserSymNdx0;
2870 if (ternary.addr_query != NULL_ADDRESS) *needPopAll = MagickTrue;
2872 (void) ResolveTernaryAddresses (pfx, &ternary);
2876 if (!pfx->teDepth && *needPopAll) {
2877 (void) AddAddressingElement (pfx, rZerStk, NULL_ADDRESS);
2878 *needPopAll = MagickFalse;
2881 if (pfx->exception->severity >= ErrorException)
2888static MagickBooleanType TranslateStatement (
FxInfo * pfx,
char * strLimit,
char * chLimit)
2890 MagickBooleanType NeedPopAll = MagickFalse;
2894 if (!*pfx->pex)
return MagickFalse;
2896 if (!TranslateExpression (pfx, strLimit, chLimit, &NeedPopAll)) {
2899 if (pfx->usedElements && *chLimit==
';') {
2904 ElementT * pel = &pfx->Elements[pfx->usedElements-1];
2905 if (pel->do_push) pel->do_push = MagickFalse;
2911static MagickBooleanType TranslateStatementList (
FxInfo * pfx,
const char * strLimit,
char * chLimit)
2913#define MAX_SLIMIT 10
2914 char sLimits[MAX_SLIMIT];
2917 if (!*pfx->pex)
return MagickFalse;
2918 (void) CopyMagickString (sLimits, strLimit, MAX_SLIMIT-1);
2920 if (strchr(strLimit,
';')==NULL)
2921 (
void) ConcatenateMagickString (sLimits,
";", MAX_SLIMIT);
2924 if (!TranslateStatement (pfx, sLimits, chLimit))
return MagickFalse;
2926 if (!*pfx->pex)
break;
2928 if (*chLimit !=
';') {
2933 if (pfx->exception->severity >= ErrorException)
2952 for (ch=0; ch <= (int) MaxPixelChannels; ch++) {
2953 cs[ch].mean *= QuantumScale;
2954 cs[ch].median *= QuantumScale;
2955 cs[ch].maxima *= QuantumScale;
2956 cs[ch].minima *= QuantumScale;
2957 cs[ch].standard_deviation *= QuantumScale;
2963static MagickBooleanType CollectStatistics (
FxInfo * pfx)
2965 Image * img = GetFirstImageInList (pfx->image);
2970 if (!pfx->statistics) {
2971 (void) ThrowMagickException (
2972 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
2973 "Statistics",
"%lu",
2974 (
unsigned long) pfx->ImgListLen);
2979 pfx->statistics[imgNum] = CollectOneImgStats (pfx, img);
2981 if (++imgNum == pfx->ImgListLen)
break;
2982 img = GetNextImageInList (img);
2983 assert (img != (
Image *) NULL);
2985 pfx->GotStats = MagickTrue;
2990static inline MagickBooleanType PushVal (
FxInfo * pfx,
fxRtT * pfxrt, fxFltType val,
int addr)
2992 if (pfxrt->usedValStack >=pfxrt->numValStack) {
2993 (void) ThrowMagickException (
2994 pfx->exception, GetMagickModule(), OptionError,
2995 "ValStack overflow at addr=",
"%i",
3000 pfxrt->ValStack[pfxrt->usedValStack++] = val;
3004static inline fxFltType PopVal (
FxInfo * pfx,
fxRtT * pfxrt,
int addr)
3006 if (pfxrt->usedValStack <= 0) {
3007 (void) ThrowMagickException (
3008 pfx->exception, GetMagickModule(), OptionError,
3009 "ValStack underflow at addr=",
"%i",
3011 return (fxFltType) 0;
3014 return pfxrt->ValStack[--pfxrt->usedValStack];
3017static inline fxFltType ImageStat (
3018 FxInfo * pfx, ssize_t ImgNum, PixelChannel channel, ImgAttrE ia)
3022 MagickBooleanType NeedRelinq = MagickFalse;
3026 (void) ThrowMagickException(pfx->exception,GetMagickModule(),
3027 OptionError,
"NoSuchImage",
"%lu",(
unsigned long) ImgNum);
3031 if (pfx->GotStats) {
3032 if ((channel < 0) || (channel > MaxPixelChannels))
3034 (void) ThrowMagickException(pfx->exception,GetMagickModule(),
3035 OptionError,
"NoSuchImageChannel",
"%i",channel);
3036 channel=(PixelChannel) 0;
3038 cs = pfx->statistics[ImgNum];
3039 }
else if (pfx->NeedStats) {
3041 if ((channel < 0) || (channel > MaxPixelChannels))
3043 (void) ThrowMagickException(pfx->exception,GetMagickModule(),
3044 OptionError,
"NoSuchImageChannel",
"%i",channel);
3045 channel=(PixelChannel) 0;
3047 cs = CollectOneImgStats (pfx, pfx->Images[ImgNum]);
3048 NeedRelinq = MagickTrue;
3053 ret = (fxFltType) GetImageDepth (pfx->Images[ImgNum], pfx->exception);
3056 ret = (fxFltType) GetBlobSize (pfx->image);
3060 ret = cs[channel].kurtosis;
3064 ret = cs[channel].maxima;
3068 ret = cs[channel].mean;
3072 ret = cs[channel].median;
3076 ret = cs[channel].minima;
3082 ret = (fxFltType) pfx->Images[ImgNum]->page.x;
3085 ret = (fxFltType) pfx->Images[ImgNum]->page.y;
3088 ret = (fxFltType) pfx->Images[ImgNum]->page.width;
3091 ret = (fxFltType) pfx->Images[ImgNum]->page.height;
3097 ret = (fxFltType) MagickSafeReciprocal (pfx->Images[ImgNum]->resolution.x)
3098 * pfx->Images[ImgNum]->columns;
3101 ret = (fxFltType) MagickSafeReciprocal (pfx->Images[ImgNum]->resolution.y)
3102 * pfx->Images[ImgNum]->rows;
3105 ret = (fxFltType) pfx->Images[ImgNum]->quality;
3111 ret = pfx->Images[ImgNum]->resolution.x;
3114 ret = pfx->Images[ImgNum]->resolution.y;
3118 ret = cs[channel].skewness;
3122 ret = cs[channel].standard_deviation;
3125 ret = (fxFltType) pfx->Images[ImgNum]->rows;
3128 ret = (fxFltType) pfx->ImgListLen;
3131 ret = (fxFltType) ImgNum;
3134 ret = (fxFltType) pfx->Images[ImgNum]->columns;
3137 ret = (fxFltType) GetImageDepth (pfx->Images[ImgNum], pfx->exception);
3140 (void) ThrowMagickException (pfx->exception,GetMagickModule(),OptionError,
3141 "Unknown ia=",
"%i",ia);
3148static inline fxFltType FxGcd (fxFltType x, fxFltType y,
const size_t depth)
3150#define FxMaxFunctionDepth 200
3153 return (FxGcd (y, x, depth+1));
3154 if ((fabs((
double) y) < 0.001) || (depth >= FxMaxFunctionDepth))
3156 return (FxGcd (y, x-y*floor((
double) (x/y)), depth+1));
3159static inline ssize_t ChkImgNum (
FxInfo * pfx, fxFltType f)
3162 ssize_t i = (ssize_t) floor ((
double) f + 0.5);
3163 if (i < 0) i += (ssize_t) pfx->ImgListLen;
3164 if (i < 0 || i >= (ssize_t) pfx->ImgListLen) {
3165 (void) ThrowMagickException (
3166 pfx->exception, GetMagickModule(), OptionError,
3167 "ImgNum",
"%lu bad for ImgListLen %lu",
3168 (
unsigned long) i, (
unsigned long) pfx->ImgListLen);
3174#define WHICH_ATTR_CHAN \
3175 (pel->channel_qual == NO_CHAN_QUAL) ? CompositePixelChannel : \
3176 (pel->channel_qual == THIS_CHANNEL) ? channel : pel->channel_qual
3178#define WHICH_NON_ATTR_CHAN \
3179 (pel->channel_qual == NO_CHAN_QUAL || \
3180 pel->channel_qual == THIS_CHANNEL || \
3181 pel->channel_qual == CompositePixelChannel \
3182 ) ? (channel == CompositePixelChannel ? RedPixelChannel: channel) \
3185static fxFltType GetHslFlt (
FxInfo * pfx, ssize_t ImgNum,
const fxFltType fx,
const fxFltType fy,
3186 PixelChannel channel)
3188 Image * img = pfx->Images[ImgNum];
3190 double red, green, blue;
3191 double hue=0, saturation=0, lightness=0;
3193 MagickBooleanType okay = MagickTrue;
3194 if(!InterpolatePixelChannel (img, pfx->Imgs[ImgNum].View, RedPixelChannel, img->interpolate,
3195 (
double) fx, (
double) fy, &red, pfx->exception)) okay = MagickFalse;
3196 if(!InterpolatePixelChannel (img, pfx->Imgs[ImgNum].View, GreenPixelChannel, img->interpolate,
3197 (
double) fx, (
double) fy, &green, pfx->exception)) okay = MagickFalse;
3198 if(!InterpolatePixelChannel (img, pfx->Imgs[ImgNum].View, BluePixelChannel, img->interpolate,
3199 (
double) fx, (
double) fy, &blue, pfx->exception)) okay = MagickFalse;
3202 (void) ThrowMagickException (
3203 pfx->exception, GetMagickModule(), OptionError,
3204 "GetHslFlt failure",
"%lu %g,%g %i", (
unsigned long) ImgNum,
3205 (
double) fx, (double) fy, channel);
3209 &hue, &saturation, &lightness);
3211 if (channel == HUE_CHANNEL)
return hue;
3212 if (channel == SAT_CHANNEL)
return saturation;
3213 if (channel == LIGHT_CHANNEL)
return lightness;
3218static fxFltType GetHslInt (
FxInfo * pfx, ssize_t ImgNum,
const ssize_t imgx,
const ssize_t imgy, PixelChannel channel)
3220 Image * img = pfx->Images[ImgNum];
3222 double hue=0, saturation=0, lightness=0;
3224 const Quantum * p = GetCacheViewVirtualPixels (pfx->Imgs[ImgNum].View, imgx, imgy, 1, 1, pfx->exception);
3225 if (p == (
const Quantum *) NULL)
3227 (void) ThrowMagickException (pfx->exception,GetMagickModule(),
3228 OptionError,
"GetHslInt failure",
"%lu %li,%li %i",(
unsigned long) ImgNum,
3229 (
long) imgx,(long) imgy,channel);
3234 GetPixelRed (img, p), GetPixelGreen (img, p), GetPixelBlue (img, p),
3235 &hue, &saturation, &lightness);
3237 if (channel == HUE_CHANNEL)
return hue;
3238 if (channel == SAT_CHANNEL)
return saturation;
3239 if (channel == LIGHT_CHANNEL)
return lightness;
3244static inline fxFltType GetIntensity (
FxInfo * pfx, ssize_t ImgNum,
const fxFltType fx,
const fxFltType fy)
3247 quantum_pixel[MaxPixelChannels];
3252 Image * img = pfx->Images[ImgNum];
3254 (void) GetPixelInfo (img, &pixelinf);
3256 if (!InterpolatePixelInfo (img, pfx->Imgs[pfx->ImgNum].View, img->interpolate,
3257 (
double) fx, (
double) fy, &pixelinf, pfx->exception))
3259 (void) ThrowMagickException (
3260 pfx->exception, GetMagickModule(), OptionError,
3261 "GetIntensity failure",
"%lu %g,%g", (
unsigned long) ImgNum,
3262 (
double) fx, (double) fy);
3265 SetPixelViaPixelInfo (img, &pixelinf, quantum_pixel);
3266 return QuantumScale * GetPixelIntensity (img, quantum_pixel);
3269static MagickBooleanType ExecuteRPN (
FxInfo * pfx,
fxRtT * pfxrt, fxFltType *result,
3270 const PixelChannel channel,
const ssize_t imgx,
const ssize_t imgy)
3272 const Quantum * p = pfxrt->thisPixel;
3273 fxFltType regA=0, regB=0, regC=0, regD=0, regE=0;
3274 Image * img = pfx->image;
3276 MagickBooleanType NeedRelinq = MagickFalse;
3277 double hue=0, saturation=0, lightness=0;
3284 if (!p) p = GetCacheViewVirtualPixels (
3285 pfx->Imgs[pfx->ImgNum].View, imgx, imgy, 1, 1, pfx->exception);
3287 if (p == (
const Quantum *) NULL)
3289 (void) ThrowMagickException (pfx->exception,GetMagickModule(),
3290 OptionError,
"Can't get virtual pixels",
"%lu %li,%li",(
unsigned long)
3291 pfx->ImgNum,(
long) imgx,(long) imgy);
3292 return(MagickFalse);
3295 if (pfx->GotStats) {
3296 cs = pfx->statistics[pfx->ImgNum];
3297 }
else if (pfx->NeedStats) {
3298 cs = CollectOneImgStats (pfx, pfx->Images[pfx->ImgNum]);
3299 NeedRelinq = MagickTrue;
3306 GetPixelRed (img, p), GetPixelGreen (img, p), GetPixelBlue (img, p),
3307 &hue, &saturation, &lightness);
3310 for (i=0; i < pfx->usedElements; i++) {
3315 (void) ThrowMagickException (
3316 pfx->exception, GetMagickModule(), OptionError,
3317 "Bad run-time address",
"%i", i);
3319 pel=&pfx->Elements[i];
3320 switch (pel->number_args) {
3324 regA = PopVal (pfx, pfxrt, i);
3327 regB = PopVal (pfx, pfxrt, i);
3328 regA = PopVal (pfx, pfxrt, i);
3331 regC = PopVal (pfx, pfxrt, i);
3332 regB = PopVal (pfx, pfxrt, i);
3333 regA = PopVal (pfx, pfxrt, i);
3336 regD = PopVal (pfx, pfxrt, i);
3337 regC = PopVal (pfx, pfxrt, i);
3338 regB = PopVal (pfx, pfxrt, i);
3339 regA = PopVal (pfx, pfxrt, i);
3342 regE = PopVal (pfx, pfxrt, i);
3343 regD = PopVal (pfx, pfxrt, i);
3344 regC = PopVal (pfx, pfxrt, i);
3345 regB = PopVal (pfx, pfxrt, i);
3346 regA = PopVal (pfx, pfxrt, i);
3349 (void) ThrowMagickException (
3350 pfx->exception, GetMagickModule(), OptionError,
3351 "Too many args:",
"%i", pel->number_args);
3355 switch (pel->operator_index) {
3357 regA = (pfxrt->UserSymVals[pel->element_index] += regA);
3360 regA = (pfxrt->UserSymVals[pel->element_index] -= regA);
3363 regA = (pfxrt->UserSymVals[pel->element_index] *= regA);
3366 regA = (pfxrt->UserSymVals[pel->element_index] /= regA);
3369 regA = pfxrt->UserSymVals[pel->element_index]++;
3372 regA = pfxrt->UserSymVals[pel->element_index]--;
3387 regA = fmod ((
double) regA, fabs(floor((
double) regB+0.5)));
3396 if ((
size_t) (regB+0.5) >= (8*
sizeof(
size_t)))
3398 (void) ThrowMagickException ( pfx->exception, GetMagickModule(),
3399 OptionError,
"undefined shift",
"%g", (double) regB);
3400 regA = (fxFltType) 0.0;
3403 regA = (fxFltType) ((
size_t)(regA+0.5) << (
size_t)(regB+0.5));
3406 if ((
size_t) (regB+0.5) >= (8*
sizeof(
size_t)))
3408 (void) ThrowMagickException ( pfx->exception, GetMagickModule(),
3409 OptionError,
"undefined shift",
"%g", (double) regB);
3410 regA = (fxFltType) 0.0;
3413 regA = (fxFltType) ((
size_t)(regA+0.5) >> (
size_t)(regB+0.5));
3416 regA = fabs((
double) (regA-regB)) < MagickEpsilon ? 1.0 : 0.0;
3419 regA = fabs((
double) (regA-regB)) >= MagickEpsilon ? 1.0 : 0.0;
3422 regA = (regA <= regB) ? 1.0 : 0.0;
3425 regA = (regA >= regB) ? 1.0 : 0.0;
3428 regA = (regA < regB) ? 1.0 : 0.0;
3431 regA = (regA > regB) ? 1.0 : 0.0;
3434 regA = (regA<=0) ? 0.0 : (regB > 0) ? 1.0 : 0.0;
3437 regA = (regA>0) ? 1.0 : (regB > 0.0) ? 1.0 : 0.0;
3440 regA = (regA==0) ? 1.0 : 0.0;
3443 regA = (fxFltType) ((
size_t)(regA+0.5) & (
size_t)(regB+0.5));
3446 regA = (fxFltType) ((
size_t)(regA+0.5) | (
size_t)(regB+0.5));
3450 regA = (fxFltType) (~(
size_t)(regA+0.5));
3453 regA = pow ((
double) regA, (
double) regB);
3469 if (pel->type == etColourConstant) {
3470 switch (channel) {
default:
3471 case (PixelChannel) 0:
3474 case (PixelChannel) 1:
3477 case (PixelChannel) 2:
3487 regA = fabs ((
double) regA);
3489#if defined(MAGICKCORE_HAVE_ACOSH)
3491 regA = acosh ((
double) regA);
3495 regA = acos ((
double) regA);
3497#if defined(MAGICKCORE_HAVE_J1)
3499 if (regA==0) regA = 1.0;
3501 fxFltType gamma = 2.0 * __j1((
double) (MagickPI*regA)) / (MagickPI*regA);
3502 regA = gamma * gamma;
3507 regA = (fxFltType) (((ssize_t) regA) & 0x01 ? -1.0 : 1.0);
3509#if defined(MAGICKCORE_HAVE_ASINH)
3511 regA = asinh ((
double) regA);
3515 regA = asin ((
double) regA);
3517#if defined(MAGICKCORE_HAVE_ATANH)
3519 regA = atanh ((
double) regA);
3523 regA = atan2 ((
double) regA, (
double) regB);
3526 regA = atan ((
double) regA);
3529 regA = ceil ((
double) regA);
3533 case (PixelChannel) 0:
break;
3534 case (PixelChannel) 1: regA = regB;
break;
3535 case (PixelChannel) 2: regA = regC;
break;
3536 case (PixelChannel) 3: regA = regD;
break;
3537 case (PixelChannel) 4: regA = regE;
break;
3538 default: regA = 0.0;
3542 if (regA < 0) regA = 0.0;
3543 else if (regA > 1.0) regA = 1.0;
3546 regA = cosh ((
double) regA);
3549 regA = cos ((
double) regA);
3554 (void) fprintf (stderr,
"%s[%g,%g].[%i]: %s=%.*g\n",
3555 img->filename, (
double) imgx, (double) imgy,
3556 channel, SetPtrShortExp (pfx, pel->exp_start, (
size_t) (pel->exp_len+1)),
3557 pfx->precision, (double) regA);
3560 regA = regA / (regB*(regA-1.0) + 1.0);
3562#if defined(MAGICKCORE_HAVE_ERF)
3564 regA = erf ((
double) regA);
3571 regA = exp ((
double) regA);
3574 regA = floor ((
double) regA);
3577 regA = exp((
double) (-regA*regA/2.0))/sqrt(2.0*MagickPI);
3580 if (!IsNaN((
double) regA))
3581 regA = FxGcd (regA, regB, 0);
3584 regA = hypot ((
double) regA, (
double) regB);
3587 regA = floor ((
double) regA);
3590 regA = (fxFltType) (!!IsNaN ((
double) regA));
3592#if defined(MAGICKCORE_HAVE_J0)
3594 regA = __j0((
double) regA);
3597#if defined(MAGICKCORE_HAVE_J1)
3599 regA = __j1((
double) regA);
3602#if defined(MAGICKCORE_HAVE_J1)
3604 if (regA==0) regA = 1.0;
3605 else regA = 2.0 * __j1((
double) (MagickPI*regA))/(MagickPI*regA);
3609 regA = log ((
double) regA);
3612 regA = log10((
double) regA) / log10(2.0);
3615 regA = log10 ((
double) regA);
3618 regA = (fxFltType) GetMagickTime();
3621 regA = (regA > regB) ? regA : regB;
3624 regA = (regA < regB) ? regA : regB;
3630 regA = regA - floor((
double) (regA/regB))*regB;
3634 regA = (fxFltType) (regA < MagickEpsilon);
3637 regA = pow ((
double) regA, (
double) regB);
3640#if defined(MAGICKCORE_OPENMP_SUPPORT)
3641 #pragma omp critical (MagickCore_ExecuteRPN)
3643 regA = GetPseudoRandomValue (pfxrt->random_info);
3647 regA = floor ((
double) regA + 0.5);
3650 regA = (regA < 0) ? -1.0 : 1.0;
3653 regA = sin ((
double) (MagickPI*regA)) / (MagickPI*regA);
3656 regA = sinh ((
double) regA);
3659 regA = sin ((
double) regA);
3662 regA = sqrt ((
double) regA);
3665 regA = 1.0 / (1.0 + exp ((
double) -regA));
3668 regA = tanh ((
double) regA);
3671 regA = tan ((
double) regA);
3674 if (regA >= 0) regA = floor ((
double) regA);
3675 else regA = ceil ((
double) regA);
3687 ssize_t ImgNum = ChkImgNum (pfx, regA);
3688 if (ImgNum < 0)
break;
3689 regA = (fxFltType) 0;
3691 Image * pimg = pfx->Images[0];
3692 if (pel->img_attr_qual == aNull) {
3693 if ((
int) pel->channel_qual < 0) {
3694 if (pel->channel_qual == NO_CHAN_QUAL || pel->channel_qual == THIS_CHANNEL) {
3695 if (pfx->ImgNum==0) {
3696 regA = QuantumScale * (double) p[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3698 const Quantum * pv = GetCacheViewVirtualPixels (
3699 pfx->Imgs[0].View, imgx, imgy, 1,1, pfx->exception);
3701 (void) ThrowMagickException (
3702 pfx->exception, GetMagickModule(), OptionError,
3703 "fU can't get cache",
"%lu", (
unsigned long) ImgNum);
3706 regA = QuantumScale * (double) pv[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3708 }
else if (pel->channel_qual == HUE_CHANNEL || pel->channel_qual == SAT_CHANNEL ||
3709 pel->channel_qual == LIGHT_CHANNEL) {
3710 regA = GetHslInt (pfx, ImgNum, imgx, imgy, pel->channel_qual);
3712 }
else if (pel->channel_qual == INTENSITY_CHANNEL) {
3713 regA = GetIntensity (pfx, 0, (
double) imgx, (
double) imgy);
3717 if (pfx->ImgNum==0) {
3718 regA = QuantumScale * (double) p[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3720 const Quantum * pv = GetCacheViewVirtualPixels (
3721 pfx->Imgs[0].View, imgx, imgy, 1,1, pfx->exception);
3723 (void) ThrowMagickException (
3724 pfx->exception, GetMagickModule(), OptionError,
3725 "fU can't get cache",
"%lu", (
unsigned long) ImgNum);
3728 regA = QuantumScale * (double) pv[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3733 regA = ImageStat (pfx, 0, WHICH_ATTR_CHAN, pel->img_attr_qual);
3737 if (pel->img_attr_qual == aNull) {
3739 if ((
int) pel->channel_qual < 0) {
3740 if (pel->channel_qual == HUE_CHANNEL || pel->channel_qual == SAT_CHANNEL ||
3741 pel->channel_qual == LIGHT_CHANNEL)
3743 regA = GetHslInt (pfx, ImgNum, imgx, imgy, pel->channel_qual);
3745 }
else if (pel->channel_qual == INTENSITY_CHANNEL)
3747 regA = GetIntensity (pfx, ImgNum, (fxFltType) imgx, (fxFltType) imgy);
3752 pv = GetCacheViewVirtualPixels (
3753 pfx->Imgs[ImgNum].View, imgx, imgy, 1,1, pfx->exception);
3755 (void) ThrowMagickException (
3756 pfx->exception, GetMagickModule(), OptionError,
3757 "fU can't get cache",
"%lu", (
unsigned long) ImgNum);
3760 regA = QuantumScale * (double)
3761 pv[pfx->Images[ImgNum]->channel_map[WHICH_NON_ATTR_CHAN].offset];
3763 regA = ImageStat (pfx, ImgNum, WHICH_ATTR_CHAN, pel->img_attr_qual);
3772 Image * pimg = pfx->Images[0];
3773 if ((
int) pel->channel_qual < 0) {
3774 if (pel->channel_qual == NO_CHAN_QUAL || pel->channel_qual == THIS_CHANNEL) {
3776 if (pfx->ImgNum==0) {
3777 regA = QuantumScale * (double) p[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3779 const Quantum * pv = GetCacheViewVirtualPixels (
3780 pfx->Imgs[0].View, imgx, imgy, 1,1, pfx->exception);
3782 (void) ThrowMagickException (
3783 pfx->exception, GetMagickModule(), OptionError,
3784 "fU0 can't get cache",
"%i", 0);
3787 regA = QuantumScale * (double) pv[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3790 }
else if (pel->channel_qual == HUE_CHANNEL || pel->channel_qual == SAT_CHANNEL ||
3791 pel->channel_qual == LIGHT_CHANNEL) {
3792 regA = GetHslInt (pfx, 0, imgx, imgy, pel->channel_qual);
3794 }
else if (pel->channel_qual == INTENSITY_CHANNEL) {
3795 regA = GetIntensity (pfx, 0, (fxFltType) imgx, (fxFltType) imgy);
3798 if (pfx->ImgNum==0) {
3799 regA = QuantumScale * (double) p[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3801 const Quantum * pv = GetCacheViewVirtualPixels (
3802 pfx->Imgs[0].View, imgx, imgy, 1,1, pfx->exception);
3804 (void) ThrowMagickException (
3805 pfx->exception, GetMagickModule(), OptionError,
3806 "fU0 can't get cache",
"%i", 0);
3809 regA = QuantumScale * (double) pv[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3816 ssize_t ImgNum = ChkImgNum (pfx, regA);
3819 if (ImgNum < 0)
break;
3821 if (pel->is_relative) {
3829 if ((
int) pel->channel_qual < 0) {
3830 if (pel->channel_qual == HUE_CHANNEL || pel->channel_qual == SAT_CHANNEL
3831 || pel->channel_qual == LIGHT_CHANNEL) {
3832 regA = GetHslFlt (pfx, ImgNum, fx, fy, pel->channel_qual);
3834 }
else if (pel->channel_qual == INTENSITY_CHANNEL) {
3835 regA = GetIntensity (pfx, ImgNum, fx, fy);
3842 Image * imUP = pfx->Images[ImgNum];
3843 if (! InterpolatePixelChannel (imUP, pfx->Imgs[ImgNum].View, WHICH_NON_ATTR_CHAN,
3844 imUP->interpolate, (
double) fx, (
double) fy, &v, pfx->exception))
3846 (void) ThrowMagickException (
3847 pfx->exception, GetMagickModule(), OptionError,
3848 "fUP can't get interpolate",
"%lu", (
unsigned long) ImgNum);
3851 regA = v * QuantumScale;
3860 if (pel->operator_index == fS) ImgNum = pfx->ImgNum;
3862 if (pel->img_attr_qual == aNull) {
3863 const Quantum * pv = GetCacheViewVirtualPixels (
3864 pfx->Imgs[ImgNum].View, imgx, imgy, 1,1, pfx->exception);
3866 (void) ThrowMagickException (
3867 pfx->exception, GetMagickModule(), OptionError,
3868 "fV can't get cache",
"%lu", (
unsigned long) ImgNum);
3872 if ((
int) pel->channel_qual < 0) {
3873 if (pel->channel_qual == HUE_CHANNEL || pel->channel_qual == SAT_CHANNEL ||
3874 pel->channel_qual == LIGHT_CHANNEL) {
3875 regA = GetHslInt (pfx, ImgNum, imgx, imgy, pel->channel_qual);
3877 }
else if (pel->channel_qual == INTENSITY_CHANNEL) {
3878 regA = GetIntensity (pfx, ImgNum, (
double) imgx, (
double) imgy);
3883 regA = QuantumScale * (double)
3884 pv[pfx->Images[ImgNum]->channel_map[WHICH_NON_ATTR_CHAN].offset];
3886 regA = ImageStat (pfx, ImgNum, WHICH_ATTR_CHAN, pel->img_attr_qual);
3896 ssize_t ImgNum = pfx->ImgNum;
3897 if (pel->operator_index == fVP) ImgNum = 1;
3898 if (pel->is_relative) {
3905 if ((
int) pel->channel_qual < 0) {
3906 if (pel->channel_qual == HUE_CHANNEL || pel->channel_qual == SAT_CHANNEL ||
3907 pel->channel_qual == LIGHT_CHANNEL) {
3908 regA = GetHslFlt (pfx, ImgNum, fx, fy, pel->channel_qual);
3910 }
else if (pel->channel_qual == INTENSITY_CHANNEL) {
3911 regA = GetIntensity (pfx, ImgNum, fx, fy);
3919 if (! InterpolatePixelChannel (pfx->Images[ImgNum], pfx->Imgs[ImgNum].View,
3920 WHICH_NON_ATTR_CHAN, pfx->Images[ImgNum]->interpolate,
3921 (
double) fx, (
double) fy, &v, pfx->exception)
3924 (void) ThrowMagickException (
3925 pfx->exception, GetMagickModule(), OptionError,
3926 "fSP or fVP can't get interp",
"%lu", (
unsigned long) ImgNum);
3929 regA = v * (fxFltType)QuantumScale;
3937 regA = (fxFltType) GetImageDepth (img, pfx->exception);
3940 regA = (fxFltType) img->extent;
3944 regA = cs[WHICH_ATTR_CHAN].kurtosis;
3948 regA = cs[WHICH_ATTR_CHAN].maxima;
3952 regA = cs[WHICH_ATTR_CHAN].mean;
3956 regA = cs[WHICH_ATTR_CHAN].median;
3960 regA = cs[WHICH_ATTR_CHAN].minima;
3965 regA = (fxFltType) img->page.x;
3968 regA = (fxFltType) img->page.y;
3971 regA = (fxFltType) img->page.width;
3974 regA = (fxFltType) img->page.height;
3979 regA = (fxFltType) MagickSafeReciprocal (img->resolution.x) * img->columns;
3982 regA = (fxFltType) MagickSafeReciprocal (img->resolution.y) * img->rows;
3985 regA = (fxFltType) img->quality;
3990 regA = (fxFltType) img->resolution.x;
3993 regA = (fxFltType) img->resolution.y;
3997 regA = cs[WHICH_ATTR_CHAN].skewness;
4001 regA = cs[WHICH_ATTR_CHAN].standard_deviation;
4004 regA = (fxFltType) img->rows;
4007 regA = (fxFltType) pfx->ImgListLen;
4010 regA = (fxFltType) pfx->ImgNum;
4013 regA = (fxFltType) img->columns;
4016 regA = (fxFltType) GetImageDepth (img, pfx->exception);
4024 regA = GetIntensity (pfx, pfx->ImgNum, (
double) imgx, (
double) imgy);
4031 regA = QuantumScale * (0.212656 * (double) GetPixelRed (img,p) +
4032 0.715158 * (double) GetPixelGreen (img,p) +
4033 0.072186 * (double) GetPixelBlue (img,p));
4039 regA = QuantumScale * (double) GetPixelAlpha (img, p);
4042 regA = QuantumScale * (double) GetPixelBlue (img, p);
4045 regA = QuantumScale * (double) GetPixelCyan (img, p);
4048 regA = QuantumScale * (double) GetPixelGreen (img, p);
4051 regA = (fxFltType) imgx;
4054 regA = (fxFltType) imgy;
4057 regA = QuantumScale * (double) GetPixelBlack (img, p);
4060 regA = QuantumScale * (double) GetPixelGreen (img, p);
4063 regA = QuantumScale * (double) GetPixelAlpha (img, p);
4066 regA = QuantumScale * (double) GetPixelRed (img, p);
4069 regA = QuantumScale * (double) GetPixelYellow (img, p);
4075 assert (pel->element_index >= 0);
4076 i = pel->element_index-1;
4079 assert (pel->element_index >= 0);
4080 i = pel->element_index-1;
4081 if (IsImageTTLExpired(img) != MagickFalse) {
4082 i = pfx->usedElements-1;
4083 (void) ThrowMagickException (pfx->exception, GetMagickModule(),
4084 ResourceLimitFatalError,
"TimeLimitExceeded",
"`%s'", img->filename);
4088 assert (pel->element_index >= 0);
4089 if (fabs((
double) regA) < MagickEpsilon) i = pel->element_index-1;
4091 case rIfNotZeroGoto:
4092 assert (pel->element_index >= 0);
4093 if (fabs((
double) regA) > MagickEpsilon) i = pel->element_index-1;
4096 assert (pel->element_index >= 0);
4097 regA = pfxrt->UserSymVals[pel->element_index];
4100 assert (pel->element_index >= 0);
4101 pfxrt->UserSymVals[pel->element_index] = regA;
4104 pfxrt->usedValStack = 0;
4110 (void) ThrowMagickException (
4111 pfx->exception, GetMagickModule(), OptionError,
4112 "pel->oprNum",
"%i '%s' not yet implemented",
4113 (int)pel->operator_index, OprStr(pel->operator_index));
4117 if (!PushVal (pfx, pfxrt, regA, i))
break;
4120 if (pfxrt->usedValStack > 0) regA = PopVal (pfx, pfxrt, 9999);
4126 if (pfx->exception->severity >= ErrorException)
4129 if (pfxrt->usedValStack != 0) {
4130 (void) ThrowMagickException (
4131 pfx->exception, GetMagickModule(), OptionError,
4132 "ValStack not empty",
"(%i)", pfxrt->usedValStack);
4141MagickPrivate MagickBooleanType FxEvaluateChannelExpression (
4143 const PixelChannel channel,
const ssize_t x,
const ssize_t y,
4147 id = GetOpenMPThreadId();
4151 assert (pfx != NULL);
4152 assert (pfx->image != NULL);
4153 assert (pfx->Images != NULL);
4154 assert (pfx->Imgs != NULL);
4155 assert (pfx->fxrts != NULL);
4157 pfx->fxrts[id].thisPixel = NULL;
4159 if (!ExecuteRPN (pfx, &pfx->fxrts[
id], &ret, channel, x, y)) {
4160 (void) ThrowMagickException (
4161 exception, GetMagickModule(), OptionError,
4162 "ExecuteRPN failed",
" ");
4166 *result = (double) ret;
4171static FxInfo *AcquireFxInfoPrivate (
const Image * images,
const char * expression,
4176 FxInfo * pfx = (
FxInfo*) AcquireCriticalMemory (
sizeof (*pfx));
4178 memset (pfx, 0,
sizeof (*pfx));
4180 if (!InitFx (pfx, images, CalcAllStats, exception)) {
4181 pfx = (
FxInfo*) RelinquishMagickMemory(pfx);
4185 if (!BuildRPN (pfx)) {
4186 (void) DeInitFx (pfx);
4187 pfx = (
FxInfo*) RelinquishMagickMemory(pfx);
4191 if ((*expression ==
'@') && (strlen(expression) > 1))
4192 pfx->expression=FileToString(expression,~0UL,exception);
4193 if (pfx->expression == (
char *) NULL)
4194 pfx->expression=ConstantString(expression);
4195 pfx->pex = (
char *) pfx->expression;
4198 if (!TranslateStatementList (pfx,
";", &chLimit)) {
4199 (void) DestroyRPN (pfx);
4200 pfx->expression = DestroyString (pfx->expression);
4202 (void) DeInitFx (pfx);
4203 pfx = (
FxInfo*) RelinquishMagickMemory(pfx);
4208 (void) ThrowMagickException (
4209 pfx->exception, GetMagickModule(), OptionError,
4210 "Translate expression depth",
"(%i) not 0",
4213 (void) DestroyRPN (pfx);
4214 pfx->expression = DestroyString (pfx->expression);
4216 (void) DeInitFx (pfx);
4217 pfx = (
FxInfo*) RelinquishMagickMemory(pfx);
4221 if (chLimit !=
'\0' && chLimit !=
';') {
4222 (void) ThrowMagickException (
4223 pfx->exception, GetMagickModule(), OptionError,
4224 "AcquireFxInfo: TranslateExpression did not exhaust input",
"(chLimit=%i) at'%s'",
4225 (int)chLimit, pfx->pex);
4227 (void) DestroyRPN (pfx);
4228 pfx->expression = DestroyString (pfx->expression);
4230 (void) DeInitFx (pfx);
4231 pfx = (
FxInfo*) RelinquishMagickMemory(pfx);
4235 if (pfx->NeedStats && pfx->runType == rtEntireImage && !pfx->statistics) {
4236 if (!CollectStatistics (pfx)) {
4237 (void) DestroyRPN (pfx);
4238 pfx->expression = DestroyString (pfx->expression);
4240 (void) DeInitFx (pfx);
4241 pfx = (
FxInfo*) RelinquishMagickMemory(pfx);
4246 if (pfx->DebugOpt) {
4247 DumpTables (stderr);
4248 DumpUserSymbols (pfx, stderr);
4249 (void) DumpRPN (pfx, stderr);
4253 size_t number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
4256 pfx->fxrts = (
fxRtT *)AcquireQuantumMemory (number_threads,
sizeof(
fxRtT));
4258 (void) ThrowMagickException (
4259 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
4261 (
unsigned long) number_threads);
4262 (void) DestroyRPN (pfx);
4263 pfx->expression = DestroyString (pfx->expression);
4265 (void) DeInitFx (pfx);
4266 pfx = (
FxInfo*) RelinquishMagickMemory(pfx);
4269 for (t=0; t < (ssize_t) number_threads; t++) {
4270 if (!AllocFxRt (pfx, &pfx->fxrts[t])) {
4271 (void) ThrowMagickException (
4272 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
4273 "AllocFxRt t=",
"%g",
4277 for (t2 = t-1; t2 >= 0; t2--) {
4278 DestroyFxRt (&pfx->fxrts[t]);
4281 pfx->fxrts = (
fxRtT *) RelinquishMagickMemory (pfx->fxrts);
4282 (void) DestroyRPN (pfx);
4283 pfx->expression = DestroyString (pfx->expression);
4285 (void) DeInitFx (pfx);
4286 pfx = (
FxInfo*) RelinquishMagickMemory(pfx);
4296 return AcquireFxInfoPrivate (images, expression, MagickFalse, exception);
4303 assert (pfx != NULL);
4304 assert (pfx->image != NULL);
4305 assert (pfx->Images != NULL);
4306 assert (pfx->Imgs != NULL);
4307 assert (pfx->fxrts != NULL);
4309 for (t=0; t < (ssize_t) GetMagickResourceLimit(ThreadResource); t++) {
4310 DestroyFxRt (&pfx->fxrts[t]);
4312 pfx->fxrts = (
fxRtT *) RelinquishMagickMemory (pfx->fxrts);
4316 pfx->expression = DestroyString (pfx->expression);
4319 (void) DeInitFx (pfx);
4321 pfx = (
FxInfo*) RelinquishMagickMemory(pfx);
4328MagickExport
Image *FxImage(
const Image *image,
const char *expression,
4331#define FxImageTag "FxNew/Image"
4352 assert(image != (
Image *) NULL);
4353 assert(image->signature == MagickCoreSignature);
4354 if (IsEventLogging() != MagickFalse)
4355 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4356 if (expression == (
const char *) NULL)
4357 return(CloneImage(image,0,0,MagickTrue,exception));
4358 fx_image=CloneImage(image,0,0,MagickTrue,exception);
4359 if (!fx_image)
return NULL;
4360 if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse) {
4361 fx_image=DestroyImage(fx_image);
4365 pfx = AcquireFxInfoPrivate (image, expression, MagickTrue, exception);
4368 fx_image=DestroyImage(fx_image);
4372 assert (pfx->image != NULL);
4373 assert (pfx->Images != NULL);
4374 assert (pfx->Imgs != NULL);
4375 assert (pfx->fxrts != NULL);
4379 image_view = AcquireVirtualCacheView (image, pfx->exception);
4380 fx_view = AcquireAuthenticCacheView (fx_image, pfx->exception);
4381#if defined(MAGICKCORE_OPENMP_SUPPORT)
4382 #pragma omp parallel for schedule(dynamic) shared(progress,status) \
4383 magick_number_threads(image,fx_image,fx_image->rows, \
4384 pfx->ContainsDebug ? 0 : 1)
4386 for (y=0; y < (ssize_t) fx_image->rows; y++)
4389 id = GetOpenMPThreadId();
4403 if (status == MagickFalse)
4405 p = GetCacheViewVirtualPixels (image_view, 0, y, image->columns, 1, pfx->exception);
4406 q = QueueCacheViewAuthenticPixels (fx_view, 0, y, fx_image->columns, 1, pfx->exception);
4407 if ((p == (
const Quantum *) NULL) || (q == (Quantum *) NULL)) {
4411 for (x=0; x < (ssize_t) fx_image->columns; x++) {
4414 pfx->fxrts[id].thisPixel = (Quantum *)p;
4416 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4418 PixelChannel channel = GetPixelChannelChannel (image, i);
4419 PixelTrait traits = GetPixelChannelTraits (image, channel);
4420 PixelTrait fx_traits = GetPixelChannelTraits (fx_image, channel);
4421 if ((traits == UndefinedPixelTrait) ||
4422 (fx_traits == UndefinedPixelTrait))
4424 if ((fx_traits & CopyPixelTrait) != 0) {
4425 SetPixelChannel (fx_image, channel, p[i], q);
4429 if (!ExecuteRPN (pfx, &pfx->fxrts[
id], &result, channel, x, y)) {
4434 q[i] = ClampToQuantum ((MagickRealType) (QuantumRange*result));
4436 p+=(ptrdiff_t) GetPixelChannels (image);
4437 q+=(ptrdiff_t) GetPixelChannels (fx_image);
4439 if (SyncCacheViewAuthenticPixels(fx_view, pfx->exception) == MagickFalse)
4441 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4446#if defined(MAGICKCORE_OPENMP_SUPPORT)
4450 proceed = SetImageProgress (image, FxImageTag, progress, image->rows);
4451 if (proceed == MagickFalse)
4456 fx_view = DestroyCacheView (fx_view);
4457 image_view = DestroyCacheView (image_view);
4461 if (pfx->DebugOpt && pfx->usedUserSymbols) {
4463 char UserSym[MagickPathExtent];
4464 fprintf (stderr,
"User symbols (%i):\n", pfx->usedUserSymbols);
4465 for (t=0; t < (int) GetMagickResourceLimit(ThreadResource); t++) {
4466 for (i = 0; i < (int) pfx->usedUserSymbols; i++) {
4467 fprintf (stderr,
"th=%i us=%i '%s': %.*Lg\n",
4468 t, i, NameOfUserSym (pfx, i, UserSym), pfx->precision, pfx->fxrts[t].UserSymVals[i]);
4473 if ((status == MagickFalse) || (pfx->exception->severity >= ErrorException))
4474 fx_image=DestroyImage(fx_image);
4476 pfx=DestroyFxInfo(pfx);