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, MagickPathExtent);
891 (void) CopyMagickString(pfx->ShortExp + MaxLen,
"...", 4);
894 p = strchr(pfx->ShortExp,
'\n');
895 if (p) (void) CopyMagickString(p,
"...", 4);
897 p = strchr(pfx->ShortExp,
'\r');
898 if (p) (void) CopyMagickString(p,
"...", 4);
901 return pfx->ShortExp;
904static char * SetShortExp (
FxInfo * pfx)
906 return SetPtrShortExp (pfx, pfx->pex, MaxTokenLen-1);
909static int FindUserSymbol (
FxInfo * pfx,
char * name)
916 lenName = strlen (name);
917 for (i=0; i < pfx->usedUserSymbols; i++) {
919 if (lenName == pus->len && LocaleNCompare (name, pus->pex, lenName)==0)
break;
921 if (i == pfx->usedUserSymbols)
return NULL_ADDRESS;
925static MagickBooleanType ExtendUserSymbols (
FxInfo * pfx)
927 pfx->numUserSymbols = (int) ceil (pfx->numUserSymbols * (1 + TableExtend));
928 pfx->UserSymbols = (
UserSymbolT*) ResizeMagickMemory (pfx->UserSymbols, (
size_t) pfx->numUserSymbols *
sizeof(
UserSymbolT));
929 if (!pfx->UserSymbols) {
930 (void) ThrowMagickException (
931 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
933 pfx->numUserSymbols);
940static int AddUserSymbol (
FxInfo * pfx,
char * pex,
size_t len)
943 if (++pfx->usedUserSymbols >= pfx->numUserSymbols) {
944 if (!ExtendUserSymbols (pfx))
return -1;
946 pus = &pfx->UserSymbols[pfx->usedUserSymbols-1];
950 return pfx->usedUserSymbols-1;
953static void DumpTables (FILE * fh)
957 for (i=0; i <= rNull; i++) {
958 const char * str =
"";
959 if ( i < oNull) str = Operators[i].str;
960 if (i >= (
int) FirstFunc && i < fNull) str = Functions[i-(int) FirstFunc].str;
961 if (i >= (
int) FirstImgAttr && i < aNull) str = ImgAttrs[i-(
int) FirstImgAttr].str;
962 if (i >= (
int) FirstSym && i < sNull) str = Symbols[i-(
int) FirstSym].str;
963 if (i >= (
int) FirstCont && i < rNull) str = Controls[i-(
int) FirstCont].str;
964 if (i==0 ) fprintf (stderr,
"Operators:\n ");
965 else if (i==oNull) fprintf (stderr,
"\nFunctions:\n ");
966 else if (i==fNull) fprintf (stderr,
"\nImage attributes:\n ");
967 else if (i==aNull) fprintf (stderr,
"\nSymbols:\n ");
968 else if (i==sNull) fprintf (stderr,
"\nControls:\n ");
969 fprintf (fh,
" %s", str);
974static char * NameOfUserSym (
FxInfo * pfx,
int ndx,
char * buf)
977 assert (ndx >= 0 && ndx < pfx->usedUserSymbols);
978 pus = &pfx->UserSymbols[ndx];
979 (void) CopyMagickString (buf, pus->pex, pus->len+1);
983static void DumpUserSymbols (
FxInfo * pfx, FILE * fh)
985 char UserSym[MagickPathExtent];
987 fprintf (fh,
"UserSymbols (%i)\n", pfx->usedUserSymbols);
988 for (i=0; i < pfx->usedUserSymbols; i++) {
989 fprintf (fh,
" %i: '%s'\n", i, NameOfUserSym (pfx, i, UserSym));
993static MagickBooleanType BuildRPN (
FxInfo * pfx)
995 pfx->numUserSymbols = InitNumUserSymbols;
996 pfx->usedUserSymbols = 0;
997 pfx->UserSymbols = (
UserSymbolT*) AcquireMagickMemory ((
size_t) pfx->numUserSymbols *
sizeof(
UserSymbolT));
998 if (!pfx->UserSymbols) {
999 (void) ThrowMagickException (
1000 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
1001 "UserSymbols",
"%i",
1002 pfx->numUserSymbols);
1006 pfx->numElements = RpnInit;
1007 pfx->usedElements = 0;
1008 pfx->Elements = NULL;
1010 pfx->Elements = (
ElementT*) AcquireMagickMemory ((
size_t) pfx->numElements *
sizeof(
ElementT));
1012 if (!pfx->Elements) {
1013 (void) ThrowMagickException (
1014 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
1020 pfx->usedOprStack = 0;
1021 pfx->maxUsedOprStack = 0;
1022 pfx->numOprStack = InitNumOprStack;
1023 pfx->OperatorStack = (OperatorE*) AcquireMagickMemory ((
size_t) pfx->numOprStack *
sizeof(OperatorE));
1024 if (!pfx->OperatorStack) {
1025 (void) ThrowMagickException (
1026 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
1027 "OperatorStack",
"%i",
1035static MagickBooleanType AllocFxRt (
FxInfo * pfx,
fxRtT * pfxrt)
1039 pfxrt->random_info = AcquireRandomInfo ();
1040 pfxrt->thisPixel = NULL;
1042 nRnd = 20 + 10 * (int) GetPseudoRandomValue (pfxrt->random_info);
1043 for (i=0; i < nRnd; i++) (
void) GetPseudoRandomValue (pfxrt->random_info);;
1045 pfxrt->usedValStack = 0;
1046 pfxrt->numValStack = 2 * pfx->maxUsedOprStack;
1047 if (pfxrt->numValStack < MinValStackSize) pfxrt->numValStack = MinValStackSize;
1048 pfxrt->ValStack = (fxFltType*) AcquireMagickMemory ((
size_t) pfxrt->numValStack *
sizeof(fxFltType));
1049 if (!pfxrt->ValStack) {
1050 (void) ThrowMagickException (
1051 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
1053 pfxrt->numValStack);
1057 pfxrt->UserSymVals = NULL;
1059 if (pfx->usedUserSymbols) {
1060 pfxrt->UserSymVals = (fxFltType*) AcquireMagickMemory ((
size_t) pfx->usedUserSymbols *
sizeof(fxFltType));
1061 if (!pfxrt->UserSymVals) {
1062 (void) ThrowMagickException (
1063 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
1064 "UserSymVals",
"%i",
1065 pfx->usedUserSymbols);
1068 for (i = 0; i < pfx->usedUserSymbols; i++) pfxrt->UserSymVals[i] = (fxFltType) 0;
1074static MagickBooleanType ExtendRPN (
FxInfo * pfx)
1076 pfx->numElements = (int) ceil (pfx->numElements * (1 + TableExtend));
1077 pfx->Elements = (
ElementT*) ResizeMagickMemory (pfx->Elements, (
size_t) pfx->numElements *
sizeof(
ElementT));
1078 if (!pfx->Elements) {
1079 (void) ThrowMagickException (
1080 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
1088static inline MagickBooleanType OprInPlace (
int op)
1090 return (op >= oAddEq && op <= oSubSub ? MagickTrue : MagickFalse);
1093static const char * OprStr (
int oprNum)
1096 if (oprNum < 0) str =
"bad OprStr";
1097 else if (oprNum <= oNull) str = Operators[oprNum].str;
1098 else if (oprNum <= fNull) str = Functions[oprNum-(int) FirstFunc].str;
1099 else if (oprNum <= aNull) str = ImgAttrs[oprNum-(int) FirstImgAttr].str;
1100 else if (oprNum <= sNull) str = Symbols[oprNum-(int) FirstSym].str;
1101 else if (oprNum <= rNull) str = Controls[oprNum-(int) FirstCont].str;
1108static MagickBooleanType DumpRPN (
FxInfo * pfx, FILE * fh)
1112 fprintf (fh,
"DumpRPN:");
1113 fprintf (fh,
" numElements=%i", pfx->numElements);
1114 fprintf (fh,
" usedElements=%i", pfx->usedElements);
1115 fprintf (fh,
" maxUsedOprStack=%i", pfx->maxUsedOprStack);
1116 fprintf (fh,
" ImgListLen=%g", (
double) pfx->ImgListLen);
1117 fprintf (fh,
" NeedStats=%s", pfx->NeedStats ?
"yes" :
"no");
1118 fprintf (fh,
" GotStats=%s", pfx->GotStats ?
"yes" :
"no");
1119 fprintf (fh,
" NeedHsl=%s\n", pfx->NeedHsl ?
"yes" :
"no");
1120 if (pfx->runType==rtEntireImage) fprintf (stderr,
"EntireImage");
1121 else if (pfx->runType==rtCornerOnly) fprintf (stderr,
"CornerOnly");
1125 for (i=0; i < pfx->usedElements; i++) {
1126 ElementT * pel = &pfx->Elements[i];
1127 pel->number_dest = 0;
1129 for (i=0; i < pfx->usedElements; i++) {
1130 ElementT * pel = &pfx->Elements[i];
1131 if (pel->operator_index == rGoto || pel->operator_index == rGotoChk || pel->operator_index == rIfZeroGoto || pel->operator_index == rIfNotZeroGoto) {
1132 if (pel->element_index >= 0 && pel->element_index < pfx->numElements) {
1133 ElementT * pelDest = &pfx->Elements[pel->element_index];
1134 pelDest->number_dest++;
1138 for (i=0; i < pfx->usedElements; i++) {
1139 char UserSym[MagickPathExtent];
1141 ElementT * pel = &pfx->Elements[i];
1142 const char * str = OprStr (pel->operator_index);
1143 const char *sRelAbs =
"";
1145 if (pel->operator_index == fP || pel->operator_index == fUP || pel->operator_index == fVP || pel->operator_index == fSP)
1146 sRelAbs = pel->is_relative ?
"[]" :
"{}";
1148 if (pel->type == etColourConstant)
1149 fprintf (fh,
" %i: %s vals=%.*Lg,%.*Lg,%.*Lg '%s%s' nArgs=%i ndx=%i %s",
1150 i, sElementTypes[pel->type],
1151 pfx->precision, pel->val, pfx->precision, pel->val1, pfx->precision, pel->val2,
1152 str, sRelAbs, pel->number_args, pel->element_index,
1153 pel->do_push ?
"push" :
"NO push");
1155 fprintf (fh,
" %i: %s val=%.*Lg '%s%s' nArgs=%i ndx=%i %s",
1156 i, sElementTypes[pel->type], pfx->precision, pel->val, str, sRelAbs,
1157 pel->number_args, pel->element_index,
1158 pel->do_push ?
"push" :
"NO push");
1160 if (pel->img_attr_qual != aNull)
1161 fprintf (fh,
" ia=%s", OprStr((
int) pel->img_attr_qual));
1163 if (pel->channel_qual != NO_CHAN_QUAL) {
1164 if (pel->channel_qual == THIS_CHANNEL) fprintf (stderr,
" ch=this");
1165 else fprintf (stderr,
" ch=%i", pel->channel_qual);
1168 if (pel->operator_index == rCopyTo) {
1169 fprintf (fh,
" CopyTo ==> %s", NameOfUserSym (pfx, pel->element_index, UserSym));
1170 }
else if (pel->operator_index == rCopyFrom) {
1171 fprintf (fh,
" CopyFrom <== %s", NameOfUserSym (pfx, pel->element_index, UserSym));
1172 }
else if (OprInPlace (pel->operator_index)) {
1173 fprintf (fh,
" <==> %s", NameOfUserSym (pfx, pel->element_index, UserSym));
1175 if (pel->number_dest > 0) fprintf (fh,
" <==dest(%i)", pel->number_dest);
1181static void DestroyRPN (
FxInfo * pfx)
1183 pfx->numOprStack = 0;
1184 pfx->usedOprStack = 0;
1185 if (pfx->OperatorStack) pfx->OperatorStack = (OperatorE*) RelinquishMagickMemory (pfx->OperatorStack);
1187 pfx->numElements = 0;
1188 pfx->usedElements = 0;
1189 if (pfx->Elements) pfx->Elements = (
ElementT*) RelinquishMagickMemory (pfx->Elements);
1191 pfx->usedUserSymbols = 0;
1192 if (pfx->UserSymbols) pfx->UserSymbols = (
UserSymbolT*) RelinquishMagickMemory (pfx->UserSymbols);
1195static void DestroyFxRt (
fxRtT * pfxrt)
1197 pfxrt->usedValStack = 0;
1198 if (pfxrt->ValStack) pfxrt->ValStack = (fxFltType*) RelinquishMagickMemory (pfxrt->ValStack);
1199 if (pfxrt->UserSymVals) pfxrt->UserSymVals = (fxFltType*) RelinquishMagickMemory (pfxrt->UserSymVals);
1201 pfxrt->random_info = DestroyRandomInfo (pfxrt->random_info);
1204static size_t GetToken (
FxInfo * pfx)
1215 char * p = pfx->pex;
1219 if (!isalpha((
int)*p))
return 0;
1226 if (LocaleNCompare (p,
"icc-", 4) == 0) {
1229 while (isalpha ((
int)*p)) { len++; p++; }
1230 }
else if (LocaleNCompare (p,
"device-", 7) == 0) {
1233 while (isalpha ((
int)*p)) { len++; p++; }
1235 while (isalpha ((
int)*p)) { len++; p++; }
1236 if (*p ==
'_') { len++; p++; }
1237 while (isalpha ((
int)*p)) { len++; p++; }
1238 while (isdigit ((
int)*p)) { len++; p++; }
1240 if (len >= MaxTokenLen) {
1241 (void) ThrowMagickException (
1242 pfx->exception, GetMagickModule(), OptionError,
1243 "GetToken: too long",
"%g at '%s'",
1244 (double) len, SetShortExp(pfx));
1248 (void) CopyMagickString (pfx->token, pfx->pex, (len+1<MaxTokenLen)?len+1:MaxTokenLen);
1251 pfx->lenToken = strlen (pfx->token);
1255static MagickBooleanType TokenMaybeUserSymbol (
FxInfo * pfx)
1257 char * p = pfx->token;
1260 if (!isalpha ((
int)*p++))
return MagickFalse;
1263 if (i < 2)
return MagickFalse;
1267static MagickBooleanType AddElement (
FxInfo * pfx, fxFltType val,
int oprNum)
1271 assert (oprNum <= rNull);
1273 if (++pfx->usedElements >= pfx->numElements) {
1274 if (!ExtendRPN (pfx))
return MagickFalse;
1277 pel = &pfx->Elements[pfx->usedElements-1];
1278 pel->type = TypeOfOpr (oprNum);
1280 pel->val1 = (fxFltType) 0;
1281 pel->val2 = (fxFltType) 0;
1282 pel->operator_index = oprNum;
1283 pel->do_push = MagickTrue;
1284 pel->element_index = 0;
1285 pel->channel_qual = NO_CHAN_QUAL;
1286 pel->img_attr_qual = aNull;
1287 pel->number_dest = 0;
1288 pel->exp_start = NULL;
1291 if (oprNum <= oNull) pel->number_args = Operators[oprNum].number_args;
1292 else if (oprNum <= fNull) pel->number_args = Functions[oprNum-(int) FirstFunc].number_args;
1293 else if (oprNum <= aNull) pel->number_args = 0;
1294 else if (oprNum <= sNull) pel->number_args = 0;
1295 else pel->number_args = Controls[oprNum-(int) FirstCont].number_args;
1300static MagickBooleanType AddAddressingElement (
FxInfo * pfx,
int oprNum,
int EleNdx)
1303 if (!AddElement (pfx, (fxFltType) 0, oprNum))
return MagickFalse;
1304 pel = &pfx->Elements[pfx->usedElements-1];
1305 pel->element_index = EleNdx;
1306 if (oprNum == rGoto || oprNum == rGotoChk || oprNum == rIfZeroGoto || oprNum == rIfNotZeroGoto
1307 || oprNum == rZerStk)
1309 pel->do_push = MagickFalse;
1319static MagickBooleanType AddColourElement (
FxInfo * pfx, fxFltType val0, fxFltType val1, fxFltType val2)
1322 if (!AddElement (pfx, val0, oNull))
return MagickFalse;
1323 pel = &pfx->Elements[pfx->usedElements-1];
1326 pel->type = etColourConstant;
1330static inline void SkipSpaces (
FxInfo * pfx)
1332 while (isspace ((
int)*pfx->pex)) pfx->pex++;
1335static inline char PeekChar (
FxInfo * pfx)
1341static inline MagickBooleanType PeekStr (
FxInfo * pfx,
const char * str)
1345 return (LocaleNCompare (pfx->pex, str, strlen(str))==0 ? MagickTrue : MagickFalse);
1348static MagickBooleanType ExpectChar (
FxInfo * pfx,
char c)
1350 if (PeekChar (pfx) != c) {
1351 (void) ThrowMagickException (
1352 pfx->exception, GetMagickModule(), OptionError,
1353 "Expected char",
"'%c' at '%s'", c, SetShortExp (pfx));
1360static int MaybeXYWH (
FxInfo * pfx, ImgAttrE * pop)
1367 if (*pop != aPage && *pop != aPrintsize && *pop != aRes)
return 0;
1369 if (PeekChar (pfx) !=
'.')
return 0;
1371 if (!ExpectChar (pfx,
'.'))
return 0;
1373 (void) GetToken (pfx);
1374 if (LocaleCompare (
"x", pfx->token)==0) ret=1;
1375 else if (LocaleCompare (
"y", pfx->token)==0) ret=2;
1376 else if (LocaleCompare (
"width", pfx->token)==0) ret=3;
1377 else if (LocaleCompare (
"height", pfx->token)==0) ret=4;
1380 (void) ThrowMagickException (
1381 pfx->exception, GetMagickModule(), OptionError,
1382 "Invalid 'x' or 'y' or 'width' or 'height' token=",
"'%s' at '%s'",
1383 pfx->token, SetShortExp(pfx));
1385 if (*pop == aPage) (*pop) = (ImgAttrE) ((
int) *pop + ret);
1388 (void) ThrowMagickException (
1389 pfx->exception, GetMagickModule(), OptionError,
1390 "Invalid 'width' or 'height' token=",
"'%s' at '%s'",
1391 pfx->token, SetShortExp(pfx));
1393 (*pop) = (ImgAttrE) ((
int) *pop + ret);
1396 pfx->pex+=pfx->lenToken;
1401static MagickBooleanType ExtendOperatorStack (
FxInfo * pfx)
1403 pfx->numOprStack = (int) ceil (pfx->numOprStack * (1 + TableExtend));
1404 pfx->OperatorStack = (OperatorE*) ResizeMagickMemory (pfx->OperatorStack, (
size_t) pfx->numOprStack *
sizeof(OperatorE));
1405 if (!pfx->OperatorStack) {
1406 (void) ThrowMagickException (
1407 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
1415static MagickBooleanType PushOperatorStack (
FxInfo * pfx,
int op)
1417 if (++pfx->usedOprStack >= pfx->numOprStack) {
1418 if (!ExtendOperatorStack (pfx))
1421 pfx->OperatorStack[pfx->usedOprStack-1] = (OperatorE) op;
1423 if (pfx->maxUsedOprStack < pfx->usedOprStack)
1424 pfx->maxUsedOprStack = pfx->usedOprStack;
1428static OperatorE GetLeadingOp (
FxInfo * pfx)
1430 OperatorE op = oNull;
1432 if (*pfx->pex ==
'-') op = oUnaryMinus;
1433 else if (*pfx->pex ==
'+') op = oUnaryPlus;
1434 else if (*pfx->pex ==
'~') op = oBitNot;
1435 else if (*pfx->pex ==
'!') op = oLogNot;
1436 else if (*pfx->pex ==
'(') op = oOpenParen;
1441static inline MagickBooleanType OprIsUnaryPrefix (OperatorE op)
1443 return (op == oUnaryMinus || op == oUnaryPlus || op == oBitNot || op == oLogNot ? MagickTrue : MagickFalse);
1446static MagickBooleanType TopOprIsUnaryPrefix (
FxInfo * pfx)
1448 if (!pfx->usedOprStack)
return MagickFalse;
1450 return OprIsUnaryPrefix (pfx->OperatorStack[pfx->usedOprStack-1]);
1453static MagickBooleanType PopOprOpenParen (
FxInfo * pfx, OperatorE op)
1456 if (!pfx->usedOprStack)
return MagickFalse;
1458 if (pfx->OperatorStack[pfx->usedOprStack-1] != op)
return MagickFalse;
1460 pfx->usedOprStack--;
1465static int GetCoordQualifier (
FxInfo * pfx,
int op)
1469 if (op != fU && op != fV && op != fS)
return -1;
1471 (void) GetToken (pfx);
1473 if (pfx->lenToken != 1) {
1476 if (*pfx->token !=
'p' && *pfx->token !=
'P')
return -1;
1477 if (!GetFunction (pfx, fP))
return -1;
1482static PixelChannel GetChannelQualifier (
FxInfo * pfx,
int op)
1484 if (op == fU || op == fV || op == fP ||
1485 op == fUP || op == fVP ||
1486 op == fS || (op >= (
int) FirstImgAttr && op <= aNull)
1489 const ChannelT * pch = &Channels[0];
1490 (void) GetToken (pfx);
1493 if (LocaleCompare (pch->str, pfx->token)==0) {
1495 if (op >= (
int) FirstImgAttr && op <= (int) ((OperatorE)aNull) &&
1496 ChanIsVirtual (pch->pixel_channel)
1499 (void) ThrowMagickException (
1500 pfx->exception, GetMagickModule(), OptionError,
1501 "Can't have image attribute with channel qualifier at",
"'%s' at '%s'",
1502 pfx->token, SetShortExp(pfx));
1503 return NO_CHAN_QUAL;
1506 pfx->pex += pfx->lenToken;
1507 return pch->pixel_channel;
1512 return NO_CHAN_QUAL;
1515static ImgAttrE GetImgAttrToken (
FxInfo * pfx)
1517 ImgAttrE ia = aNull;
1519 for (ia = FirstImgAttr; ia < aNull; ia=(ImgAttrE) (ia+1)) {
1520 iaStr = ImgAttrs[ia-(int) FirstImgAttr].str;
1521 if (LocaleCompare (iaStr, pfx->token)==0) {
1522 pfx->pex += strlen(pfx->token);
1523 if (ImgAttrs[ia-(
int) FirstImgAttr].need_stats != MagickFalse) pfx->NeedStats = MagickTrue;
1524 MaybeXYWH (pfx, &ia);
1529 if (ia == aPage || ia == aPrintsize || ia == aRes) {
1530 (void) ThrowMagickException (
1531 pfx->exception, GetMagickModule(), OptionError,
1532 "Attribute",
"'%s' needs qualifier at '%s'",
1533 iaStr, SetShortExp(pfx));
1539static ImgAttrE GetImgAttrQualifier (
FxInfo * pfx,
int op)
1541 ImgAttrE ia = aNull;
1542 if (op == (OperatorE)fU || op == (OperatorE)fV || op == (OperatorE)fP || op == (OperatorE)fS) {
1543 (void) GetToken (pfx);
1544 if (pfx->lenToken == 0) {
1547 ia = GetImgAttrToken (pfx);
1552static MagickBooleanType IsQualifier (
FxInfo * pfx)
1554 if (PeekChar (pfx) ==
'.') {
1561static MagickBooleanType ParseISO860(
const char* text,
struct tm* tp)
1571 memset(tp,0,
sizeof(
struct tm));
1572 if (MagickSscanf(text,
"%d-%d-%dT%d:%d:%d",&year,&month,&day,&hour,&min,&sec) != 6)
1573 return(MagickFalse);
1574 tp->tm_year=year-1900;
1584static ssize_t GetProperty (
FxInfo * pfx, fxFltType *val, fxFltType *seconds)
1591 if (seconds != NULL) *seconds = SECONDS_ERR;
1593 if (PeekStr (pfx,
"%[")) {
1596 char sProperty [MagickPathExtent];
1597 char * p = pfx->pex + 2;
1601 if (*p ==
'[') level++;
1602 else if (*p ==
']') {
1603 if (level == 0)
break;
1608 if (!*p || level != 0) {
1609 (void) ThrowMagickException (
1610 pfx->exception, GetMagickModule(), OptionError,
1611 "After '%[' expected ']' at",
"'%s'",
1616 len = (size_t) (p - pfx->pex + 1);
1617 if (len > MaxTokenLen) {
1618 (void) ThrowMagickException (
1619 pfx->exception, GetMagickModule(), OptionError,
1620 "Too much text between '%[' and ']' at",
"'%s'",
1625 (void) CopyMagickString (sProperty, pfx->pex, len+1);
1626 sProperty[len] =
'\0';
1630 text = InterpretImageProperties (pfx->image->image_info, pfx->image,
1631 sProperty, pfx->exception);
1632 if (!text || !*text) {
1633 text = DestroyString(text);
1634 (void) ThrowMagickException (
1635 pfx->exception, GetMagickModule(), OptionError,
1636 "Unknown property",
"'%s' at '%s'",
1637 sProperty, SetShortExp(pfx));
1641 if (seconds != NULL) {
1643 if (ParseISO860(text,&tp) == MagickFalse) {
1644 (void) ThrowMagickException (
1645 pfx->exception, GetMagickModule(), OptionError,
1646 "Function 'epoch' expected date property, found ",
"'%s' at '%s'",
1647 text, SetShortExp(pfx));
1648 text = DestroyString(text);
1649 *seconds = SECONDS_ERR;
1652 *seconds = (fxFltType)mktime (&tp);
1655 *val = strtold (text, &tailptr);
1656 if (text == tailptr) {
1657 text = DestroyString(text);
1658 (void) ThrowMagickException (
1659 pfx->exception, GetMagickModule(), OptionError,
1660 "Property",
"'%s' text '%s' is not a number at '%s'",
1661 sProperty, text, SetShortExp(pfx));
1662 text = DestroyString(text);
1666 text = DestroyString(text);
1668 return ((ssize_t) len);
1674static inline ssize_t GetConstantColour (
FxInfo * pfx, fxFltType *v0, fxFltType *v1, fxFltType *v2)
1685 *dummy_exception = AcquireExceptionInfo ();
1695 char ColSp[MagickPathExtent];
1696 (void) CopyMagickString (ColSp, pfx->token, MaxTokenLen);
1697 p = ColSp + pfx->lenToken - 1;
1698 if (*p ==
'a' || *p ==
'A') *p =
'\0';
1700 (void) GetPixelInfo (pfx->image, &colour);
1704 IsGray = (LocaleCompare (ColSp,
"gray") == 0) ? MagickTrue : MagickFalse;
1705 IsIcc = (LocaleCompare (ColSp,
"icc-color") == 0) ? MagickTrue : MagickFalse;
1706 IsDev = (LocaleNCompare (ColSp,
"device-", 7) == 0) ? MagickTrue : MagickFalse;
1710 if (!QueryColorCompliance (pfx->token, AllCompliance, &colour, dummy_exception) || IsGray) {
1711 ssize_t type = ParseCommandOption (MagickColorspaceOptions, MagickFalse, ColSp);
1712 if (type >= 0 || IsIcc || IsDev) {
1713 char * q = pfx->pex + pfx->lenToken;
1714 while (isspace((
int) ((
unsigned char) *q))) q++;
1717 char sFunc[MagickPathExtent];
1718 while (*q && *q !=
')') q++;
1720 (void) ThrowMagickException (
1721 pfx->exception, GetMagickModule(), OptionError,
1722 "constant color missing ')'",
"at '%s'",
1724 dummy_exception = DestroyExceptionInfo (dummy_exception);
1727 lenfun = (size_t) (q - pfx->pex + 1);
1728 if (lenfun > MaxTokenLen) {
1729 (void) ThrowMagickException (
1730 pfx->exception, GetMagickModule(), OptionError,
1731 "lenfun too long",
"'%lu' at '%s'",
1732 (
unsigned long) lenfun, SetShortExp(pfx));
1733 dummy_exception = DestroyExceptionInfo (dummy_exception);
1736 (void) CopyMagickString (sFunc, pfx->pex, lenfun+1);
1737 if (QueryColorCompliance (sFunc, AllCompliance, &colour, dummy_exception)) {
1738 *v0 = QuantumScale*colour.red;
1739 *v1 = QuantumScale*colour.green;
1740 *v2 = QuantumScale*colour.blue;
1741 dummy_exception = DestroyExceptionInfo (dummy_exception);
1742 return (ssize_t)lenfun;
1745 (void) ThrowMagickException (
1746 pfx->exception, GetMagickModule(), OptionError,
1747 "colorspace but not a valid color with '(...)' at",
"'%s'",
1749 dummy_exception = DestroyExceptionInfo (dummy_exception);
1754 dummy_exception = DestroyExceptionInfo (dummy_exception);
1759 *v0 = QuantumScale*colour.red;
1760 *v1 = QuantumScale*colour.green;
1761 *v2 = QuantumScale*colour.blue;
1763 dummy_exception = DestroyExceptionInfo (dummy_exception);
1764 return (ssize_t)strlen (pfx->token);
1767static inline ssize_t GetHexColour (
FxInfo * pfx, fxFltType *v0, fxFltType *v1, fxFltType *v2)
1776 if (*pfx->pex !=
'#')
return 0;
1780 while (isxdigit ((
int)*p)) p++;
1781 if (isalpha ((
int)*p)) {
1782 (void) ThrowMagickException (
1783 pfx->exception, GetMagickModule(), OptionError,
1784 "Bad hex number at",
"'%s'",
1789 len = (size_t) (p - pfx->pex);
1790 if (len < 1)
return 0;
1791 if (len >= MaxTokenLen) {
1792 (void) ThrowMagickException (
1793 pfx->exception, GetMagickModule(), OptionError,
1794 "Hex colour too long at",
"'%s'",
1798 (void) CopyMagickString (pfx->token, pfx->pex, len+1);
1800 (void) GetPixelInfo (pfx->image, &colour);
1802 if (!QueryColorCompliance (pfx->token, AllCompliance, &colour, pfx->exception)) {
1803 (void) ThrowMagickException (
1804 pfx->exception, GetMagickModule(), OptionError,
1805 "QueryColorCompliance rejected",
"'%s' at '%s'",
1806 pfx->token, SetShortExp(pfx));
1810 *v0 = QuantumScale*colour.red;
1811 *v1 = QuantumScale*colour.green;
1812 *v2 = QuantumScale*colour.blue;
1814 return (ssize_t) len;
1817static MagickBooleanType GetFunction (
FxInfo * pfx, FunctionE fe)
1821 const char * funStr = Functions[fe-(int) FirstFunc].str;
1822 int nArgs = Functions[fe-(int) FirstFunc].number_args;
1824 char expChLimit =
')';
1825 const char *strLimit =
",)";
1826 OperatorE pushOp = oOpenParen;
1833 int ndx0 = NULL_ADDRESS, ndx1 = NULL_ADDRESS, ndx2 = NULL_ADDRESS, ndx3 = NULL_ADDRESS;
1835 MagickBooleanType coordQual = MagickFalse;
1836 PixelChannel chQual = NO_CHAN_QUAL;
1837 ImgAttrE iaQual = aNull;
1839 pfx->pex += pfx->lenToken;
1842 char p = PeekChar (pfx);
1844 (void) ExpectChar (pfx,
'{');
1845 pushOp = oOpenBrace;
1849 }
else if (p==
'[') {
1850 (void) ExpectChar (pfx,
'[');
1851 pushOp = oOpenBracket;
1860 }
else if (fe == fU) {
1861 char p = PeekChar (pfx);
1863 (void) ExpectChar (pfx,
'[');
1864 pushOp = oOpenBracket;
1873 }
else if (fe == fV || fe == fS) {
1875 pushOp = oOpenBracket;
1879 if (!ExpectChar (pfx,
'('))
return MagickFalse;
1881 if (!PushOperatorStack (pfx, (
int) pushOp))
return MagickFalse;
1883 pExpStart = pfx->pex;
1884 ndx0 = pfx->usedElements;
1886 (void) AddAddressingElement (pfx, rGoto, NULL_ADDRESS);
1893 lenOptArt = GetProperty (pfx, &val, &seconds);
1894 if (seconds == SECONDS_ERR) {
1896 (void) ThrowMagickException (
1897 pfx->exception, GetMagickModule(), OptionError,
1898 "Function 'epoch' expected date property",
"at '%s'",
1902 if (lenOptArt < 0)
return MagickFalse;
1903 if (lenOptArt > 0) {
1904 (void) AddElement (pfx, seconds, oNull);
1905 pfx->pex += lenOptArt;
1906 if (!ExpectChar (pfx,
')'))
return MagickFalse;
1907 if (!PopOprOpenParen (pfx, pushOp))
return MagickFalse;
1914 if (TranslateStatementList (pfx, strLimit, &chLimit)) {
1918 (void) ThrowMagickException (
1919 pfx->exception, GetMagickModule(), OptionError,
1920 "For function",
"'%s' expected ')' at '%s'",
1921 funStr, SetShortExp(pfx));
1925 if (!chLimit)
break;
1926 if (fe == fP || fe == fS|| fe == fIf) {
1927 (void) AddElement (pfx, (fxFltType) 0, oNull);
1932 if (strchr (strLimit, chLimit)==NULL) {
1933 (void) ThrowMagickException (
1934 pfx->exception, GetMagickModule(), OptionError,
1935 "For function",
"'%s' expected one of '%s' after expression but found '%c' at '%s'",
1936 funStr, strLimit, chLimit ? chLimit :
' ', SetShortExp(pfx));
1945 if (ndx1 != NULL_ADDRESS) {
1946 (void) ThrowMagickException (
1947 pfx->exception, GetMagickModule(), OptionError,
1948 "For function",
"'%s' required argument is missing at '%s'",
1949 funStr, SetShortExp(pfx));
1952 ndx1 = pfx->usedElements;
1953 if (fe==fWhile || fe==fIf) {
1954 (void) AddAddressingElement (pfx, rIfZeroGoto, NULL_ADDRESS);
1955 }
else if (fe==fDo) {
1956 (void) AddAddressingElement (pfx, rIfZeroGoto, NULL_ADDRESS);
1957 }
else if (fe==fFor) {
1958 pfx->Elements[pfx->usedElements-1].do_push = MagickFalse;
1962 if (ndx2 != NULL_ADDRESS) {
1963 (void) ThrowMagickException (
1964 pfx->exception, GetMagickModule(), OptionError,
1965 "For function",
"'%s' required argument is missing at '%s'",
1966 funStr, SetShortExp(pfx));
1969 ndx2 = pfx->usedElements;
1971 pfx->Elements[pfx->usedElements-1].do_push = MagickFalse;
1972 (void) AddAddressingElement (pfx, rGotoChk, ndx0);
1973 }
else if (fe==fDo) {
1974 pfx->Elements[pfx->usedElements-1].do_push = MagickFalse;
1975 (void) AddAddressingElement (pfx, rGotoChk, ndx0 + 1);
1976 }
else if (fe==fFor) {
1977 (void) AddAddressingElement (pfx, rIfZeroGoto, NULL_ADDRESS);
1978 pfx->Elements[pfx->usedElements-1].do_push = MagickTrue;
1979 (void) AddAddressingElement (pfx, rZerStk, NULL_ADDRESS);
1980 }
else if (fe==fIf) {
1981 (void) AddAddressingElement (pfx, rGoto, NULL_ADDRESS);
1985 if (ndx3 != NULL_ADDRESS) {
1986 (void) ThrowMagickException (
1987 pfx->exception, GetMagickModule(), OptionError,
1988 "For function",
"'%s' required argument is missing at '%s'",
1989 funStr, SetShortExp(pfx));
1993 pfx->Elements[pfx->usedElements-1].do_push = MagickFalse;
1994 (void) AddAddressingElement (pfx, rGotoChk, ndx1);
1996 ndx3 = pfx->usedElements;
2001 if (chLimit == expChLimit) {
2002 lenExp = (size_t) (pfx->pex - pExpStart - 1);
2006 if (chLimit && chLimit != expChLimit && chLimit !=
',' ) {
2007 (void) ThrowMagickException (
2008 pfx->exception, GetMagickModule(), OptionError,
2009 "For function",
"'%s' expected '%c', found '%c' at '%s'",
2010 funStr, expChLimit, chLimit ? chLimit :
' ', SetShortExp(pfx));
2014 if (fe == fP || fe == fS || fe == fU || fe == fChannel) {
2015 while (FndArgs < Functions[fe-(
int) FirstFunc].number_args) {
2016 (void) AddElement (pfx, (fxFltType) 0, oNull);
2021 if (FndArgs > Functions[fe-(
int) FirstFunc].number_args)
2024 (void) ThrowMagickException (
2025 pfx->exception, GetMagickModule(), OptionError,
2026 "For function",
"'%s' expected up to %i arguments, found '%i' at '%s'",
2027 funStr, Functions[fe-(int) FirstFunc].number_args, FndArgs, SetShortExp(pfx));
2029 (void) ThrowMagickException (
2030 pfx->exception, GetMagickModule(), OptionError,
2031 "For function",
"'%s' expected %i arguments, found '%i' at '%s'",
2032 funStr, Functions[fe-(int) FirstFunc].number_args, FndArgs, SetShortExp(pfx));
2036 if (FndArgs < Functions[fe-(
int) FirstFunc].number_args) {
2037 (void) ThrowMagickException (
2038 pfx->exception, GetMagickModule(), OptionError,
2039 "For function",
"'%s' expected %i arguments, found too few (%i) at '%s'",
2040 funStr, Functions[fe-(int) FirstFunc].number_args, FndArgs, SetShortExp(pfx));
2043 if (fe != fS && fe != fV && FndArgs == 0 && Functions[fe-(
int) FirstFunc].number_args == 0) {
2045 chLimit = expChLimit;
2046 if (!ExpectChar (pfx,
')'))
return MagickFalse;
2049 if (chLimit != expChLimit) {
2050 (void) ThrowMagickException (
2051 pfx->exception, GetMagickModule(), OptionError,
2052 "For function",
"'%s', arguments don't end with '%c' at '%s'",
2053 funStr, expChLimit, SetShortExp(pfx));
2056 if (!PopOprOpenParen (pfx, pushOp)) {
2057 (void) ThrowMagickException (
2058 pfx->exception, GetMagickModule(), OptionError,
2059 "Bug: For function",
"'%s' tos not '%s' at '%s'",
2060 funStr, Operators[pushOp].str, SetShortExp(pfx));
2064 if (IsQualifier (pfx)) {
2066 if (fe == fU || fe == fV || fe == fS) {
2068 coordQual = (GetCoordQualifier (pfx, (
int) fe) == 1) ? MagickTrue : MagickFalse;
2073 ElementT * pel = &pfx->Elements[pfx->usedElements-1];
2074 if (pel->operator_index != fP) {
2075 (void) ThrowMagickException (
2076 pfx->exception, GetMagickModule(), OptionError,
2077 "Bug: For function",
"'%s' last element not 'p' at '%s'",
2078 funStr, SetShortExp(pfx));
2081 chQual = pel->channel_qual;
2082 expChLimit = (pel->is_relative) ?
']' :
'}';
2083 pfx->usedElements--;
2084 if (fe == fU) fe = fUP;
2085 else if (fe == fV) fe = fVP;
2086 else if (fe == fS) fe = fSP;
2087 funStr = Functions[fe-(int) FirstFunc].str;
2091 if ( chQual == NO_CHAN_QUAL &&
2092 (fe == fP || fe == fS || fe == fSP || fe == fU || fe == fUP || fe == fV || fe == fVP)
2095 chQual = GetChannelQualifier (pfx, (
int) fe);
2098 if (chQual == NO_CHAN_QUAL && (fe == fU || fe == fV || fe == fS)) {
2100 iaQual = GetImgAttrQualifier (pfx, (
int) fe);
2102 if (IsQualifier (pfx) && chQual == NO_CHAN_QUAL && iaQual != aNull) {
2103 chQual = GetChannelQualifier (pfx, (
int) fe);
2105 if (coordQual && iaQual != aNull) {
2106 (void) ThrowMagickException (
2107 pfx->exception, GetMagickModule(), OptionError,
2108 "For function",
"'%s', can't have qualifiers 'p' and image attribute '%s' at '%s'",
2109 funStr, pfx->token, SetShortExp(pfx));
2112 if (!coordQual && chQual == NO_CHAN_QUAL && iaQual == aNull) {
2113 (void) ThrowMagickException (
2114 pfx->exception, GetMagickModule(), OptionError,
2115 "For function",
"'%s', bad qualifier '%s' at '%s'",
2116 funStr, pfx->token, SetShortExp(pfx));
2119 if (!coordQual && chQual == CompositePixelChannel && iaQual == aNull) {
2120 (void) ThrowMagickException (
2121 pfx->exception, GetMagickModule(), OptionError,
2122 "For function",
"'%s', bad composite qualifier '%s' at '%s'",
2123 funStr, pfx->token, SetShortExp(pfx));
2127 if (chQual == HUE_CHANNEL || chQual == SAT_CHANNEL || chQual == LIGHT_CHANNEL) {
2128 pfx->NeedHsl = MagickTrue;
2130 if (iaQual >= FirstImgAttr && iaQual < aNull) {
2131 (void) ThrowMagickException (
2132 pfx->exception, GetMagickModule(), OptionError,
2133 "Can't have image attribute with HLS qualifier at",
"'%s'",
2140 if (iaQual != aNull && chQual != NO_CHAN_QUAL) {
2141 if (ImgAttrs[iaQual-(
int) FirstImgAttr].need_stats == MagickFalse) {
2142 (void) ThrowMagickException (
2143 pfx->exception, GetMagickModule(), OptionError,
2144 "Can't have image attribute ",
"'%s' with channel qualifier '%s' at '%s'",
2145 ImgAttrs[iaQual-(int) FirstImgAttr].str,
2146 pfx->token, SetShortExp(pfx));
2149 if (ChanIsVirtual (chQual)) {
2150 (void) ThrowMagickException (
2151 pfx->exception, GetMagickModule(), OptionError,
2152 "Can't have statistical image attribute ",
"'%s' with virtual channel qualifier '%s' at '%s'",
2153 ImgAttrs[iaQual-(int) FirstImgAttr].str,
2154 pfx->token, SetShortExp(pfx));
2161 pfx->Elements[ndx1].element_index = ndx2+1;
2162 }
else if (fe==fDo) {
2163 pfx->Elements[ndx0].element_index = ndx1+1;
2164 pfx->Elements[ndx1].element_index = ndx2+1;
2165 }
else if (fe==fFor) {
2166 pfx->Elements[ndx2].element_index = ndx3;
2167 }
else if (fe==fIf) {
2168 pfx->Elements[ndx1].element_index = ndx2 + 1;
2169 pfx->Elements[ndx2].element_index = ndx3;
2171 if (fe == fU && iaQual == aNull) {
2172 ElementT * pel = &pfx->Elements[pfx->usedElements-1];
2173 if (pel->type == etConstant && pel->val == 0.0) {
2174 pfx->usedElements--;
2178 (void) AddElement (pfx, (fxFltType) 0, (int) fe);
2179 if (fe == fP || fe == fU || fe == fU0 || fe == fUP ||
2180 fe == fV || fe == fVP || fe == fS || fe == fSP)
2182 ElementT * pel = &pfx->Elements[pfx->usedElements-1];
2183 pel->is_relative = (expChLimit ==
']' ? MagickTrue : MagickFalse);
2184 if (chQual >= 0) pel->channel_qual = chQual;
2185 if (iaQual != aNull && (fe == fU || fe == fV || fe == fS)) {
2187 pel->img_attr_qual = iaQual;
2192 if (pExpStart && lenExp) {
2193 ElementT * pel = &pfx->Elements[pfx->usedElements-1];
2194 pel->exp_start = pExpStart;
2195 pel->exp_len = lenExp;
2199 pfx->ContainsDebug = MagickTrue;
2204static MagickBooleanType IsStealth (
int op)
2206 return (op == fU0 || op == fUP || op == fSP || op == fVP ||
2207 (op >= FirstCont && op <= rNull) ? MagickTrue : MagickFalse
2211static MagickBooleanType GetOperand (
2212 FxInfo * pfx, MagickBooleanType * UserSymbol, MagickBooleanType * NewUserSymbol,
int * UserSymNdx,
2213 MagickBooleanType * needPopAll)
2216 *NewUserSymbol = *UserSymbol = MagickFalse;
2217 *UserSymNdx = NULL_ADDRESS;
2220 if (!*pfx->pex)
return MagickFalse;
2221 (void) GetToken (pfx);
2223 if (pfx->lenToken==0) {
2227 OperatorE op = GetLeadingOp (pfx);
2228 if (op==oOpenParen) {
2229 char chLimit =
'\0';
2230 if (!PushOperatorStack (pfx, (
int) op))
return MagickFalse;
2232 if (!TranslateExpression (pfx,
")", &chLimit, needPopAll)) {
2233 (void) ThrowMagickException (
2234 pfx->exception, GetMagickModule(), OptionError,
2235 "Empty expression in parentheses at",
"'%s'",
2239 if (chLimit !=
')') {
2240 (void) ThrowMagickException (
2241 pfx->exception, GetMagickModule(), OptionError,
2242 "'(' but no ')' at",
"'%s'",
2247 if (!PopOprOpenParen (pfx, oOpenParen)) {
2248 (void) ThrowMagickException (
2249 pfx->exception, GetMagickModule(), OptionError,
2250 "Bug: tos not '(' at",
"'%s'",
2255 }
else if (OprIsUnaryPrefix (op)) {
2256 MagickBooleanType operand_ok;
2257 if (!PushOperatorStack (pfx, (
int) op))
return MagickFalse;
2260 if (!*pfx->pex)
return MagickFalse;
2261 if (pfx->teDepth >= MagickMaxRecursionDepth) {
2262 (void) ThrowMagickException (
2263 pfx->exception, GetMagickModule(), OptionError,
2264 "Expression too deeply nested",
"(depth %i exceeds limit %i)",
2265 pfx->teDepth, MagickMaxRecursionDepth);
2269 operand_ok=GetOperand (pfx, UserSymbol, NewUserSymbol, UserSymNdx, needPopAll);
2272 (void) ThrowMagickException (
2273 pfx->exception, GetMagickModule(), OptionError,
2274 "After unary, bad operand at",
"'%s'",
2279 if (*NewUserSymbol) {
2280 (void) ThrowMagickException (
2281 pfx->exception, GetMagickModule(), OptionError,
2282 "After unary, NewUserSymbol at",
"'%s'",
2288 (void) AddAddressingElement (pfx, rCopyFrom, *UserSymNdx);
2289 *UserSymNdx = NULL_ADDRESS;
2291 *UserSymbol = MagickFalse;
2292 *NewUserSymbol = MagickFalse;
2295 (void) GetToken (pfx);
2297 }
else if (*pfx->pex ==
'#') {
2298 fxFltType v0=0, v1=0, v2=0;
2299 ssize_t lenToken = GetHexColour (pfx, &v0, &v1, &v2);
2301 (void) ThrowMagickException (
2302 pfx->exception, GetMagickModule(), OptionError,
2303 "Bad hex number at",
"'%s'",
2306 }
else if (lenToken > 0) {
2307 (void) AddColourElement (pfx, v0, v1, v2);
2318 fxFltType val = strtold (pfx->pex, &tailptr);
2319 if (pfx->pex != tailptr) {
2327 const char Prefixes[] =
"yzafpnum.kMGTPEZY";
2328 const char * pSi = strchr (Prefixes, *tailptr);
2329 if (pSi && *pSi !=
'.') Pow = (double) ((pSi - Prefixes) * 3 - 24);
2330 else if (*tailptr ==
'c') Pow = -2;
2331 else if (*tailptr ==
'h') Pow = 2;
2332 else if (*tailptr ==
'k') Pow = 3;
2334 if (*(++pfx->pex) ==
'i') {
2335 val *= pow (2.0, Pow/0.3);
2338 val *= pow (10.0, Pow);
2342 (void) AddElement (pfx, val, oNull);
2346 val = (fxFltType) 0;
2347 lenOptArt = GetProperty (pfx, &val, NULL);
2348 if (lenOptArt < 0)
return MagickFalse;
2349 if (lenOptArt > 0) {
2350 (void) AddElement (pfx, val, oNull);
2351 pfx->pex += lenOptArt;
2358 if (pfx->lenToken > 0) {
2363 for (ce = (ConstantE)0; ce < cNull; ce=(ConstantE) (ce+1)) {
2364 const char * ceStr = Constants[ce].str;
2365 if (LocaleCompare (ceStr, pfx->token)==0) {
2371 (void) AddElement (pfx, Constants[ce].val, oNull);
2372 pfx->pex += pfx->lenToken;
2381 for (fe = FirstFunc; fe < fNull; fe=(FunctionE) (fe+1)) {
2382 const char * feStr = Functions[fe-(int) FirstFunc].str;
2383 if (LocaleCompare (feStr, pfx->token)==0) {
2388 if (fe == fV && pfx->ImgListLen < 2) {
2389 (void) ThrowMagickException (
2390 pfx->exception, GetMagickModule(), OptionError,
2391 "Symbol 'v' but fewer than two images at",
"'%s'",
2396 if (IsStealth ((
int) fe)) {
2397 (void) ThrowMagickException (
2398 pfx->exception, GetMagickModule(), OptionError,
2399 "Function",
"'%s' not permitted at '%s'",
2400 pfx->token, SetShortExp(pfx));
2403 if (fe == fDo || fe == fFor || fe == fIf || fe == fWhile) {
2404 *needPopAll = MagickTrue;
2407 if (fe != fNull)
return (GetFunction (pfx, fe));
2413 ImgAttrE ia = GetImgAttrToken (pfx);
2416 (void) AddElement (pfx, val, (
int) ia);
2418 if (ImgAttrs[ia-(
int) FirstImgAttr].need_stats != MagickFalse) {
2419 if (IsQualifier (pfx)) {
2420 PixelChannel chQual = GetChannelQualifier (pfx, (
int) ia);
2422 if (chQual == NO_CHAN_QUAL) {
2423 (void) ThrowMagickException (
2424 pfx->exception, GetMagickModule(), OptionError,
2425 "Bad channel qualifier at",
"'%s'",
2430 pel = &pfx->Elements[pfx->usedElements-1];
2431 pel->channel_qual = chQual;
2442 for (se = FirstSym; se < sNull; se=(SymbolE) (se+1)) {
2443 const char * seStr = Symbols[se-(int) FirstSym].str;
2444 if (LocaleCompare (seStr, pfx->token)==0) {
2450 (void) AddElement (pfx, val, (
int) se);
2451 pfx->pex += pfx->lenToken;
2453 if (se==sHue || se==sSaturation || se==sLightness) pfx->NeedHsl = MagickTrue;
2461 fxFltType v0, v1, v2;
2462 ssize_t ColLen = GetConstantColour (pfx, &v0, &v1, &v2);
2463 if (ColLen < 0)
return MagickFalse;
2465 (void) AddColourElement (pfx, v0, v1, v2);
2474 const char *artifact;
2475 artifact = GetImageArtifact (pfx->image, pfx->token);
2476 if (artifact != (
const char *) NULL) {
2478 fxFltType val = strtold (artifact, &tailptr);
2479 if (pfx->token == tailptr) {
2480 (void) ThrowMagickException (
2481 pfx->exception, GetMagickModule(), OptionError,
2482 "Artifact",
"'%s' has value '%s', not a number, at '%s'",
2483 pfx->token, artifact, SetShortExp(pfx));
2486 (void) AddElement (pfx, val, oNull);
2487 pfx->pex+=pfx->lenToken;
2494 if (TokenMaybeUserSymbol (pfx)) {
2495 *UserSymbol = MagickTrue;
2496 *UserSymNdx = FindUserSymbol (pfx, pfx->token);
2497 if (*UserSymNdx == NULL_ADDRESS) {
2498 *UserSymNdx = AddUserSymbol (pfx, pfx->pex, pfx->lenToken);
2499 *NewUserSymbol = MagickTrue;
2502 pfx->pex += pfx->lenToken;
2508 (void) ThrowMagickException (
2509 pfx->exception, GetMagickModule(), OptionError,
2510 "Expected operand at",
"'%s'",
2516static inline MagickBooleanType IsRealOperator (OperatorE op)
2518 return (op < oOpenParen || op > oCloseBrace) ? MagickTrue : MagickFalse;
2521static inline MagickBooleanType ProcessTernaryOpr (
FxInfo * pfx,
TernaryT * ptern)
2526 if (pfx->usedOprStack == 0)
2528 if (pfx->OperatorStack[pfx->usedOprStack-1] == oQuery) {
2529 if (ptern->addr_query != NULL_ADDRESS) {
2530 (void) ThrowMagickException (
2531 pfx->exception, GetMagickModule(), OptionError,
2532 "Already have '?' in sub-expression at",
"'%s'",
2536 if (ptern->addr_colon != NULL_ADDRESS) {
2537 (void) ThrowMagickException (
2538 pfx->exception, GetMagickModule(), OptionError,
2539 "Already have ':' in sub-expression at",
"'%s'",
2543 pfx->usedOprStack--;
2544 ptern->addr_query = pfx->usedElements;
2545 (void) AddAddressingElement (pfx, rIfZeroGoto, NULL_ADDRESS);
2548 else if (pfx->OperatorStack[pfx->usedOprStack-1] == oColon) {
2549 if (ptern->addr_query == NULL_ADDRESS) {
2550 (void) ThrowMagickException (
2551 pfx->exception, GetMagickModule(), OptionError,
2552 "Need '?' in sub-expression at",
"'%s'",
2556 if (ptern->addr_colon != NULL_ADDRESS) {
2557 (void) ThrowMagickException (
2558 pfx->exception, GetMagickModule(), OptionError,
2559 "Already have ':' in sub-expression at",
"'%s'",
2563 pfx->usedOprStack--;
2564 ptern->addr_colon = pfx->usedElements;
2565 pfx->Elements[pfx->usedElements-1].do_push = MagickTrue;
2566 (void) AddAddressingElement (pfx, rGoto, NULL_ADDRESS);
2572static MagickBooleanType GetOperator (
2574 MagickBooleanType * Assign, MagickBooleanType * Update, MagickBooleanType * IncrDecr)
2578 MagickBooleanType DoneIt = MagickFalse;
2580 for (op = (OperatorE)0; op != oNull; op=(OperatorE) (op+1)) {
2581 const char * opStr = Operators[op].str;
2582 len = strlen(opStr);
2583 if (LocaleNCompare (opStr, pfx->pex, len)==0) {
2588 if (!IsRealOperator (op)) {
2589 (void) ThrowMagickException (
2590 pfx->exception, GetMagickModule(), OptionError,
2591 "Not a real operator at",
"'%s'",
2597 (void) ThrowMagickException (
2598 pfx->exception, GetMagickModule(), OptionError,
2599 "Expected operator at",
"'%s'",
2604 *Assign = (op==oAssign) ? MagickTrue : MagickFalse;
2605 *Update = OprInPlace ((
int) op);
2606 *IncrDecr = (op == oPlusPlus || op == oSubSub) ? MagickTrue : MagickFalse;
2613 while (pfx->usedOprStack > 0) {
2614 OperatorE top = pfx->OperatorStack[pfx->usedOprStack-1];
2615 int precTop, precNew;
2616 if (top == oOpenParen || top == oAssign || OprInPlace ((
int) top))
break;
2617 precTop = Operators[top].precedence;
2618 precNew = Operators[op].precedence;
2622 if (precTop < precNew)
break;
2623 (void) AddElement (pfx, (fxFltType) 0, (int) top);
2624 pfx->usedOprStack--;
2630 if (op==oCloseParen) {
2631 if (pfx->usedOprStack == 0) {
2632 (void) ThrowMagickException (
2633 pfx->exception, GetMagickModule(), OptionError,
2634 "Found ')' but nothing on stack at",
"'%s'",
2639 if (pfx->OperatorStack[pfx->usedOprStack-1] != oOpenParen) {
2640 (void) ThrowMagickException (
2641 pfx->exception, GetMagickModule(), OptionError,
2642 "Found ')' but no '(' on stack at",
"'%s'",
2646 pfx->usedOprStack--;
2647 DoneIt = MagickTrue;
2651 if (!PushOperatorStack (pfx, (
int) op))
return MagickFalse;
2659static MagickBooleanType ResolveTernaryAddresses (
FxInfo * pfx,
TernaryT * ptern)
2661 if (ptern->addr_query == NULL_ADDRESS && ptern->addr_colon == NULL_ADDRESS)
2664 if (ptern->addr_query != NULL_ADDRESS && ptern->addr_colon != NULL_ADDRESS) {
2665 pfx->Elements[ptern->addr_query].element_index = ptern->addr_colon + 1;
2666 pfx->Elements[ptern->addr_colon].element_index = pfx->usedElements;
2667 ptern->addr_query = NULL_ADDRESS;
2668 ptern->addr_colon = NULL_ADDRESS;
2669 }
else if (ptern->addr_query != NULL_ADDRESS) {
2670 (void) ThrowMagickException (
2671 pfx->exception, GetMagickModule(), OptionError,
2672 "'?' with no corresponding ':'",
"'%s' at '%s'",
2673 pfx->token, SetShortExp(pfx));
2675 }
else if (ptern->addr_colon != NULL_ADDRESS) {
2676 (void) ThrowMagickException (
2677 pfx->exception, GetMagickModule(), OptionError,
2678 "':' with no corresponding '?'",
"'%s' at '%s'",
2679 pfx->token, SetShortExp(pfx));
2685static MagickBooleanType TranslateExpression (
2686 FxInfo * pfx,
const char * strLimit,
char * chLimit, MagickBooleanType * needPopAll)
2690 MagickBooleanType UserSymbol, NewUserSymbol;
2691 int UserSymNdx0, UserSymNdx1;
2694 Assign = MagickFalse,
2695 Update = MagickFalse,
2696 IncrDecr = MagickFalse;
2701 ternary.addr_query = NULL_ADDRESS;
2702 ternary.addr_colon = NULL_ADDRESS;
2704 if (pfx->teDepth >= MagickMaxRecursionDepth) {
2705 (void) ThrowMagickException(pfx->exception, GetMagickModule(), OptionError,
2706 "Expression too deeply nested",
"(depth %i exceeds limit %i)",
2707 pfx->teDepth, MagickMaxRecursionDepth);
2715 StartEleNdx = pfx->usedElements-1;
2716 if (StartEleNdx < 0) StartEleNdx = 0;
2725 if (strchr(strLimit,*pfx->pex)!=NULL) {
2726 *chLimit = *pfx->pex;
2733 if (!GetOperand (pfx, &UserSymbol, &NewUserSymbol, &UserSymNdx0, needPopAll))
return MagickFalse;
2738 while (*pfx->pex && (!*strLimit || (strchr(strLimit,*pfx->pex)==NULL))) {
2739 if (!GetOperator (pfx, &Assign, &Update, &IncrDecr))
return MagickFalse;
2741 if (NewUserSymbol && !Assign) {
2742 (void) ThrowMagickException (
2743 pfx->exception, GetMagickModule(), OptionError,
2744 "Expected assignment after new UserSymbol",
"'%s' at '%s'",
2745 pfx->token, SetShortExp(pfx));
2748 if (!UserSymbol && Assign) {
2749 (void) ThrowMagickException (
2750 pfx->exception, GetMagickModule(), OptionError,
2751 "Attempted assignment to non-UserSymbol",
"'%s' at '%s'",
2752 pfx->token, SetShortExp(pfx));
2755 if (!UserSymbol && Update) {
2756 (void) ThrowMagickException (
2757 pfx->exception, GetMagickModule(), OptionError,
2758 "Attempted update to non-UserSymbol",
"'%s' at '%s'",
2759 pfx->token, SetShortExp(pfx));
2762 if (UserSymbol && (Assign || Update) && !IncrDecr) {
2764 if (!TranslateExpression (pfx, strLimit, chLimit, needPopAll))
return MagickFalse;
2765 if (!*pfx->pex)
break;
2766 if (!*strLimit)
break;
2767 if (strchr(strLimit,*chLimit)!=NULL)
break;
2769 if (UserSymbol && !Assign && !Update && UserSymNdx0 != NULL_ADDRESS) {
2771 (void) AddAddressingElement (pfx, rCopyFrom, UserSymNdx0);
2772 UserSymNdx0 = NULL_ADDRESS;
2773 pel = &pfx->Elements[pfx->usedElements-1];
2774 pel->do_push = MagickTrue;
2778 while (TopOprIsUnaryPrefix (pfx)) {
2779 OperatorE op = pfx->OperatorStack[pfx->usedOprStack-1];
2780 (void) AddElement (pfx, (fxFltType) 0, (int) op);
2781 pfx->usedOprStack--;
2785 if (!ProcessTernaryOpr (pfx, &ternary))
return MagickFalse;
2787 if (ternary.addr_colon != NULL_ADDRESS) {
2788 if (!TranslateExpression (pfx,
",);", chLimit, needPopAll))
return MagickFalse;
2792 UserSymbol = NewUserSymbol = MagickFalse;
2794 if ( (!*pfx->pex) || (*strLimit && (strchr(strLimit,*pfx->pex)!=NULL) ) )
2796 if (IncrDecr)
break;
2798 (void) ThrowMagickException (
2799 pfx->exception, GetMagickModule(), OptionError,
2800 "Expected operand after operator",
"at '%s'",
2806 (void) ThrowMagickException (
2807 pfx->exception, GetMagickModule(), OptionError,
2808 "'++' and '--' must be the final operators in an expression at",
"'%s'",
2813 if (!GetOperand (pfx, &UserSymbol, &NewUserSymbol, &UserSymNdx1, needPopAll)) {
2814 (void) ThrowMagickException (
2815 pfx->exception, GetMagickModule(), OptionError,
2816 "Expected operand at",
"'%s'",
2821 if (NewUserSymbol && !Assign) {
2822 (void) ThrowMagickException (
2823 pfx->exception, GetMagickModule(), OptionError,
2824 "NewUserSymbol",
"'%s' after non-assignment operator at '%s'",
2825 pfx->token, SetShortExp(pfx));
2828 if (UserSymbol && !NewUserSymbol) {
2829 (void) AddAddressingElement (pfx, rCopyFrom, UserSymNdx1);
2830 UserSymNdx1 = NULL_ADDRESS;
2832 UserSymNdx0 = UserSymNdx1;
2835 if (UserSymbol && !Assign && !Update && UserSymNdx0 != NULL_ADDRESS) {
2837 if (NewUserSymbol) {
2838 (void) ThrowMagickException (
2839 pfx->exception, GetMagickModule(), OptionError,
2840 "NewUserSymbol",
"'%s' needs assignment operator at '%s'",
2841 pfx->token, SetShortExp(pfx));
2844 (void) AddAddressingElement (pfx, rCopyFrom, UserSymNdx0);
2845 pel = &pfx->Elements[pfx->usedElements-1];
2846 pel->do_push = MagickTrue;
2849 if (*pfx->pex && !*chLimit && (strchr(strLimit,*pfx->pex)!=NULL)) {
2850 *chLimit = *pfx->pex;
2853 while (pfx->usedOprStack) {
2854 OperatorE op = pfx->OperatorStack[pfx->usedOprStack-1];
2855 if (op == oOpenParen || op == oOpenBracket || op == oOpenBrace) {
2858 if ( (op==oAssign && !Assign) || (OprInPlace((
int) op) && !Update) ) {
2861 pfx->usedOprStack--;
2862 (void) AddElement (pfx, (fxFltType) 0, (int) op);
2863 if (op == oAssign) {
2864 if (UserSymNdx0 < 0) {
2865 (void) ThrowMagickException (
2866 pfx->exception, GetMagickModule(), OptionError,
2867 "Assignment to unknown user symbol at",
"'%s'",
2873 pfx->usedElements--;
2874 (void) AddAddressingElement (pfx, rCopyTo, UserSymNdx0);
2876 }
else if (OprInPlace ((
int) op)) {
2877 if (UserSymNdx0 < 0) {
2878 (void) ThrowMagickException (
2879 pfx->exception, GetMagickModule(), OptionError,
2880 "Operator-in-place to unknown user symbol at",
"'%s'",
2886 pfx->Elements[pfx->usedElements-1].element_index = UserSymNdx0;
2891 if (ternary.addr_query != NULL_ADDRESS) *needPopAll = MagickTrue;
2893 (void) ResolveTernaryAddresses (pfx, &ternary);
2897 if (!pfx->teDepth && *needPopAll) {
2898 (void) AddAddressingElement (pfx, rZerStk, NULL_ADDRESS);
2899 *needPopAll = MagickFalse;
2902 if (pfx->exception->severity >= ErrorException)
2909static MagickBooleanType TranslateStatement (
FxInfo * pfx,
char * strLimit,
char * chLimit)
2911 MagickBooleanType NeedPopAll = MagickFalse;
2915 if (!*pfx->pex)
return MagickFalse;
2917 if (!TranslateExpression (pfx, strLimit, chLimit, &NeedPopAll)) {
2920 if (pfx->usedElements && *chLimit==
';') {
2925 ElementT * pel = &pfx->Elements[pfx->usedElements-1];
2926 if (pel->do_push) pel->do_push = MagickFalse;
2932static MagickBooleanType TranslateStatementList (
FxInfo * pfx,
const char * strLimit,
char * chLimit)
2934#define MAX_SLIMIT 10
2935 char sLimits[MAX_SLIMIT];
2938 if (!*pfx->pex)
return MagickFalse;
2939 (void) CopyMagickString (sLimits, strLimit, MAX_SLIMIT-1);
2941 if (strchr(strLimit,
';')==NULL)
2942 (
void) ConcatenateMagickString (sLimits,
";", MAX_SLIMIT);
2945 if (!TranslateStatement (pfx, sLimits, chLimit))
return MagickFalse;
2947 if (!*pfx->pex)
break;
2949 if (*chLimit !=
';') {
2954 if (pfx->exception->severity >= ErrorException)
2973 for (ch=0; ch <= (int) MaxPixelChannels; ch++) {
2974 cs[ch].mean *= QuantumScale;
2975 cs[ch].median *= QuantumScale;
2976 cs[ch].maxima *= QuantumScale;
2977 cs[ch].minima *= QuantumScale;
2978 cs[ch].standard_deviation *= QuantumScale;
2984static MagickBooleanType CollectStatistics (
FxInfo * pfx)
2986 Image * img = GetFirstImageInList (pfx->image);
2991 if (!pfx->statistics) {
2992 (void) ThrowMagickException (
2993 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
2994 "Statistics",
"%lu",
2995 (
unsigned long) pfx->ImgListLen);
3000 pfx->statistics[imgNum] = CollectOneImgStats (pfx, img);
3002 if (++imgNum == pfx->ImgListLen)
break;
3003 img = GetNextImageInList (img);
3004 assert (img != (
Image *) NULL);
3006 pfx->GotStats = MagickTrue;
3011static inline MagickBooleanType PushVal (
FxInfo * pfx,
fxRtT * pfxrt, fxFltType val,
int addr)
3013 if (pfxrt->usedValStack >=pfxrt->numValStack) {
3014 (void) ThrowMagickException (
3015 pfx->exception, GetMagickModule(), OptionError,
3016 "ValStack overflow at addr=",
"%i",
3021 pfxrt->ValStack[pfxrt->usedValStack++] = val;
3025static inline fxFltType PopVal (
FxInfo * pfx,
fxRtT * pfxrt,
int addr)
3027 if (pfxrt->usedValStack <= 0) {
3028 (void) ThrowMagickException (
3029 pfx->exception, GetMagickModule(), OptionError,
3030 "ValStack underflow at addr=",
"%i",
3032 return (fxFltType) 0;
3035 return pfxrt->ValStack[--pfxrt->usedValStack];
3038static inline fxFltType ImageStat (
3039 FxInfo * pfx, ssize_t ImgNum, PixelChannel channel, ImgAttrE ia)
3043 MagickBooleanType NeedRelinq = MagickFalse;
3047 (void) ThrowMagickException(pfx->exception,GetMagickModule(),
3048 OptionError,
"NoSuchImage",
"%lu",(
unsigned long) ImgNum);
3052 if (pfx->GotStats) {
3053 if ((channel < 0) || (channel > MaxPixelChannels))
3055 (void) ThrowMagickException(pfx->exception,GetMagickModule(),
3056 OptionError,
"NoSuchImageChannel",
"%i",channel);
3057 channel=(PixelChannel) 0;
3059 cs = pfx->statistics[ImgNum];
3060 }
else if (pfx->NeedStats) {
3062 if ((channel < 0) || (channel > MaxPixelChannels))
3064 (void) ThrowMagickException(pfx->exception,GetMagickModule(),
3065 OptionError,
"NoSuchImageChannel",
"%i",channel);
3066 channel=(PixelChannel) 0;
3068 cs = CollectOneImgStats (pfx, pfx->Images[ImgNum]);
3069 NeedRelinq = MagickTrue;
3074 ret = (fxFltType) GetImageDepth (pfx->Images[ImgNum], pfx->exception);
3077 ret = (fxFltType) GetBlobSize (pfx->image);
3081 ret = cs[channel].kurtosis;
3085 ret = cs[channel].maxima;
3089 ret = cs[channel].mean;
3093 ret = cs[channel].median;
3097 ret = cs[channel].minima;
3103 ret = (fxFltType) pfx->Images[ImgNum]->page.x;
3106 ret = (fxFltType) pfx->Images[ImgNum]->page.y;
3109 ret = (fxFltType) pfx->Images[ImgNum]->page.width;
3112 ret = (fxFltType) pfx->Images[ImgNum]->page.height;
3118 ret = (fxFltType) MagickSafeReciprocal (pfx->Images[ImgNum]->resolution.x)
3119 * pfx->Images[ImgNum]->columns;
3122 ret = (fxFltType) MagickSafeReciprocal (pfx->Images[ImgNum]->resolution.y)
3123 * pfx->Images[ImgNum]->rows;
3126 ret = (fxFltType) pfx->Images[ImgNum]->quality;
3132 ret = pfx->Images[ImgNum]->resolution.x;
3135 ret = pfx->Images[ImgNum]->resolution.y;
3139 ret = cs[channel].skewness;
3143 ret = cs[channel].standard_deviation;
3146 ret = (fxFltType) pfx->Images[ImgNum]->rows;
3149 ret = (fxFltType) pfx->ImgListLen;
3152 ret = (fxFltType) ImgNum;
3155 ret = (fxFltType) pfx->Images[ImgNum]->columns;
3158 ret = (fxFltType) GetImageDepth (pfx->Images[ImgNum], pfx->exception);
3161 (void) ThrowMagickException (pfx->exception,GetMagickModule(),OptionError,
3162 "Unknown ia=",
"%i",ia);
3169static inline fxFltType FxGcd (fxFltType x, fxFltType y,
const size_t depth)
3171#define FxMaxFunctionDepth 200
3174 return (FxGcd (y, x, depth+1));
3175 if ((fabs((
double) y) < 0.001) || (depth >= FxMaxFunctionDepth))
3177 return (FxGcd (y, x-y*floor((
double) (x/y)), depth+1));
3180static inline ssize_t ChkImgNum (
FxInfo * pfx, fxFltType f)
3183 ssize_t i = (ssize_t) floor ((
double) f + 0.5);
3184 if (i < 0) i += (ssize_t) pfx->ImgListLen;
3185 if (i < 0 || i >= (ssize_t) pfx->ImgListLen) {
3186 (void) ThrowMagickException (
3187 pfx->exception, GetMagickModule(), OptionError,
3188 "ImgNum",
"%lu bad for ImgListLen %lu",
3189 (
unsigned long) i, (
unsigned long) pfx->ImgListLen);
3195#define WHICH_ATTR_CHAN \
3196 (pel->channel_qual == NO_CHAN_QUAL) ? CompositePixelChannel : \
3197 (pel->channel_qual == THIS_CHANNEL) ? channel : pel->channel_qual
3199#define WHICH_NON_ATTR_CHAN \
3200 (pel->channel_qual == NO_CHAN_QUAL || \
3201 pel->channel_qual == THIS_CHANNEL || \
3202 pel->channel_qual == CompositePixelChannel \
3203 ) ? (channel == CompositePixelChannel ? RedPixelChannel: channel) \
3206static fxFltType GetHslFlt (
FxInfo * pfx, ssize_t ImgNum,
const fxFltType fx,
const fxFltType fy,
3207 PixelChannel channel)
3209 Image * img = pfx->Images[ImgNum];
3211 double red, green, blue;
3212 double hue=0, saturation=0, lightness=0;
3214 MagickBooleanType okay = MagickTrue;
3215 if(!InterpolatePixelChannel (img, pfx->Imgs[ImgNum].View, RedPixelChannel, img->interpolate,
3216 (
double) fx, (
double) fy, &red, pfx->exception)) okay = MagickFalse;
3217 if(!InterpolatePixelChannel (img, pfx->Imgs[ImgNum].View, GreenPixelChannel, img->interpolate,
3218 (
double) fx, (
double) fy, &green, pfx->exception)) okay = MagickFalse;
3219 if(!InterpolatePixelChannel (img, pfx->Imgs[ImgNum].View, BluePixelChannel, img->interpolate,
3220 (
double) fx, (
double) fy, &blue, pfx->exception)) okay = MagickFalse;
3223 (void) ThrowMagickException (
3224 pfx->exception, GetMagickModule(), OptionError,
3225 "GetHslFlt failure",
"%lu %g,%g %i", (
unsigned long) ImgNum,
3226 (
double) fx, (double) fy, channel);
3230 &hue, &saturation, &lightness);
3232 if (channel == HUE_CHANNEL)
return hue;
3233 if (channel == SAT_CHANNEL)
return saturation;
3234 if (channel == LIGHT_CHANNEL)
return lightness;
3239static fxFltType GetHslInt (
FxInfo * pfx, ssize_t ImgNum,
const ssize_t imgx,
const ssize_t imgy, PixelChannel channel)
3241 Image * img = pfx->Images[ImgNum];
3243 double hue=0, saturation=0, lightness=0;
3245 const Quantum * p = GetCacheViewVirtualPixels (pfx->Imgs[ImgNum].View, imgx, imgy, 1, 1, pfx->exception);
3246 if (p == (
const Quantum *) NULL)
3248 (void) ThrowMagickException (pfx->exception,GetMagickModule(),
3249 OptionError,
"GetHslInt failure",
"%lu %li,%li %i",(
unsigned long) ImgNum,
3250 (
long) imgx,(long) imgy,channel);
3255 GetPixelRed (img, p), GetPixelGreen (img, p), GetPixelBlue (img, p),
3256 &hue, &saturation, &lightness);
3258 if (channel == HUE_CHANNEL)
return hue;
3259 if (channel == SAT_CHANNEL)
return saturation;
3260 if (channel == LIGHT_CHANNEL)
return lightness;
3265static inline fxFltType GetIntensity (
FxInfo * pfx, ssize_t ImgNum,
const fxFltType fx,
const fxFltType fy)
3268 quantum_pixel[MaxPixelChannels];
3273 Image * img = pfx->Images[ImgNum];
3275 (void) GetPixelInfo (img, &pixelinf);
3277 if (!InterpolatePixelInfo (img, pfx->Imgs[pfx->ImgNum].View, img->interpolate,
3278 (
double) fx, (
double) fy, &pixelinf, pfx->exception))
3280 (void) ThrowMagickException (
3281 pfx->exception, GetMagickModule(), OptionError,
3282 "GetIntensity failure",
"%lu %g,%g", (
unsigned long) ImgNum,
3283 (
double) fx, (double) fy);
3286 SetPixelViaPixelInfo (img, &pixelinf, quantum_pixel);
3287 return QuantumScale * GetPixelIntensity (img, quantum_pixel);
3290static MagickBooleanType ExecuteRPN (
FxInfo * pfx,
fxRtT * pfxrt, fxFltType *result,
3291 const PixelChannel channel,
const ssize_t imgx,
const ssize_t imgy)
3293 const Quantum * p = pfxrt->thisPixel;
3294 fxFltType regA=0, regB=0, regC=0, regD=0, regE=0;
3295 Image * img = pfx->image;
3297 MagickBooleanType NeedRelinq = MagickFalse;
3298 double hue=0, saturation=0, lightness=0;
3305 if (!p) p = GetCacheViewVirtualPixels (
3306 pfx->Imgs[pfx->ImgNum].View, imgx, imgy, 1, 1, pfx->exception);
3308 if (p == (
const Quantum *) NULL)
3310 (void) ThrowMagickException (pfx->exception,GetMagickModule(),
3311 OptionError,
"Can't get virtual pixels",
"%lu %li,%li",(
unsigned long)
3312 pfx->ImgNum,(
long) imgx,(long) imgy);
3313 return(MagickFalse);
3316 if (pfx->GotStats) {
3317 cs = pfx->statistics[pfx->ImgNum];
3318 }
else if (pfx->NeedStats) {
3319 cs = CollectOneImgStats (pfx, pfx->Images[pfx->ImgNum]);
3320 NeedRelinq = MagickTrue;
3327 GetPixelRed (img, p), GetPixelGreen (img, p), GetPixelBlue (img, p),
3328 &hue, &saturation, &lightness);
3331 for (i=0; i < pfx->usedElements; i++) {
3336 (void) ThrowMagickException (
3337 pfx->exception, GetMagickModule(), OptionError,
3338 "Bad run-time address",
"%i", i);
3340 pel=&pfx->Elements[i];
3341 switch (pel->number_args) {
3345 regA = PopVal (pfx, pfxrt, i);
3348 regB = PopVal (pfx, pfxrt, i);
3349 regA = PopVal (pfx, pfxrt, i);
3352 regC = PopVal (pfx, pfxrt, i);
3353 regB = PopVal (pfx, pfxrt, i);
3354 regA = PopVal (pfx, pfxrt, i);
3357 regD = PopVal (pfx, pfxrt, i);
3358 regC = PopVal (pfx, pfxrt, i);
3359 regB = PopVal (pfx, pfxrt, i);
3360 regA = PopVal (pfx, pfxrt, i);
3363 regE = PopVal (pfx, pfxrt, i);
3364 regD = PopVal (pfx, pfxrt, i);
3365 regC = PopVal (pfx, pfxrt, i);
3366 regB = PopVal (pfx, pfxrt, i);
3367 regA = PopVal (pfx, pfxrt, i);
3370 (void) ThrowMagickException (
3371 pfx->exception, GetMagickModule(), OptionError,
3372 "Too many args:",
"%i", pel->number_args);
3376 switch (pel->operator_index) {
3378 regA = (pfxrt->UserSymVals[pel->element_index] += regA);
3381 regA = (pfxrt->UserSymVals[pel->element_index] -= regA);
3384 regA = (pfxrt->UserSymVals[pel->element_index] *= regA);
3387 regA = (pfxrt->UserSymVals[pel->element_index] /= regA);
3390 regA = pfxrt->UserSymVals[pel->element_index]++;
3393 regA = pfxrt->UserSymVals[pel->element_index]--;
3408 regA = fmod ((
double) regA, fabs(floor((
double) regB+0.5)));
3417 if (CastDoubleToSizeT((
double) regB+0.5) >= (8*
sizeof(
size_t)))
3419 (void) ThrowMagickException ( pfx->exception, GetMagickModule(),
3420 OptionError,
"undefined shift",
"%g", (double) regB);
3421 regA = (fxFltType) 0.0;
3424 regA = (fxFltType) (CastDoubleToSizeT((
double) regA+0.5) << CastDoubleToSizeT((
double) regB+0.5));
3427 if (CastDoubleToSizeT((
double) regB+0.5) >= (8*
sizeof(
size_t)))
3429 (void) ThrowMagickException ( pfx->exception, GetMagickModule(),
3430 OptionError,
"undefined shift",
"%g", (double) regB);
3431 regA = (fxFltType) 0.0;
3434 regA = (fxFltType) (CastDoubleToSizeT((
double) regA+0.5) >> CastDoubleToSizeT((
double) regB+0.5));
3437 regA = fabs((
double) (regA-regB)) < MagickEpsilon ? 1.0 : 0.0;
3440 regA = fabs((
double) (regA-regB)) >= MagickEpsilon ? 1.0 : 0.0;
3443 regA = (regA <= regB) ? 1.0 : 0.0;
3446 regA = (regA >= regB) ? 1.0 : 0.0;
3449 regA = (regA < regB) ? 1.0 : 0.0;
3452 regA = (regA > regB) ? 1.0 : 0.0;
3455 regA = (regA<=0) ? 0.0 : (regB > 0) ? 1.0 : 0.0;
3458 regA = (regA>0) ? 1.0 : (regB > 0.0) ? 1.0 : 0.0;
3461 regA = (regA==0) ? 1.0 : 0.0;
3464 regA = (fxFltType) (CastDoubleToSizeT((
double) regA+0.5) & CastDoubleToSizeT((
double) regB+0.5));
3467 regA = (fxFltType) (CastDoubleToSizeT((
double) regA+0.5) | CastDoubleToSizeT((
double) regB+0.5));
3475 new_value=~CastDoubleToSizeT((
double) regA+0.5);
3476 regA=(fxFltType) new_value;
3480 regA = pow ((
double) regA, (
double) regB);
3496 if (pel->type == etColourConstant) {
3497 switch (channel) {
default:
3498 case (PixelChannel) 0:
3501 case (PixelChannel) 1:
3504 case (PixelChannel) 2:
3514 regA = fabs ((
double) regA);
3516#if defined(MAGICKCORE_HAVE_ACOSH)
3518 regA = acosh ((
double) regA);
3522 regA = acos ((
double) regA);
3524#if defined(MAGICKCORE_HAVE_J1)
3526 if (regA==0) regA = 1.0;
3528 fxFltType gamma = 2.0 * __j1((
double) (MagickPI*regA)) / (MagickPI*regA);
3529 regA = gamma * gamma;
3534 regA = (fxFltType) (((ssize_t) regA) & 0x01 ? -1.0 : 1.0);
3536#if defined(MAGICKCORE_HAVE_ASINH)
3538 regA = asinh ((
double) regA);
3542 regA = asin ((
double) regA);
3544#if defined(MAGICKCORE_HAVE_ATANH)
3546 regA = atanh ((
double) regA);
3550 regA = atan2 ((
double) regA, (
double) regB);
3553 regA = atan ((
double) regA);
3556 regA = ceil ((
double) regA);
3560 case (PixelChannel) 0:
break;
3561 case (PixelChannel) 1: regA = regB;
break;
3562 case (PixelChannel) 2: regA = regC;
break;
3563 case (PixelChannel) 3: regA = regD;
break;
3564 case (PixelChannel) 4: regA = regE;
break;
3565 default: regA = 0.0;
3569 if (regA < 0) regA = 0.0;
3570 else if (regA > 1.0) regA = 1.0;
3573 regA = cosh ((
double) regA);
3576 regA = cos ((
double) regA);
3581 (void) fprintf (stderr,
"%s[%g,%g].[%i]: %s=%.*g\n",
3582 img->filename, (
double) imgx, (double) imgy,
3583 channel, SetPtrShortExp (pfx, pel->exp_start, (
size_t) (pel->exp_len+1)),
3584 pfx->precision, (double) regA);
3587 regA = regA / (regB*(regA-1.0) + 1.0);
3589#if defined(MAGICKCORE_HAVE_ERF)
3591 regA = erf ((
double) regA);
3598 regA = exp ((
double) regA);
3601 regA = floor ((
double) regA);
3604 regA = exp((
double) (-regA*regA/2.0))/sqrt(2.0*MagickPI);
3607 if (!IsNaN((
double) regA))
3608 regA = FxGcd (regA, regB, 0);
3611 regA = hypot ((
double) regA, (
double) regB);
3614 regA = floor ((
double) regA);
3617 regA = (fxFltType) (!!IsNaN ((
double) regA));
3619#if defined(MAGICKCORE_HAVE_J0)
3621 regA = __j0((
double) regA);
3624#if defined(MAGICKCORE_HAVE_J1)
3626 regA = __j1((
double) regA);
3629#if defined(MAGICKCORE_HAVE_J1)
3631 if (regA==0) regA = 1.0;
3632 else regA = 2.0 * __j1((
double) (MagickPI*regA))/(MagickPI*regA);
3636 regA = log ((
double) regA);
3639 regA = log10((
double) regA) / log10(2.0);
3642 regA = log10 ((
double) regA);
3645 regA = (fxFltType) GetMagickTime();
3648 regA = (regA > regB) ? regA : regB;
3651 regA = (regA < regB) ? regA : regB;
3657 regA = regA - floor((
double) (regA/regB))*regB;
3661 regA = (fxFltType) (regA < MagickEpsilon);
3664 regA = pow ((
double) regA, (
double) regB);
3667#if defined(MAGICKCORE_OPENMP_SUPPORT)
3668 #pragma omp critical (MagickCore_ExecuteRPN)
3670 regA = GetPseudoRandomValue (pfxrt->random_info);
3674 regA = floor ((
double) regA + 0.5);
3677 regA = (regA < 0) ? -1.0 : 1.0;
3680 regA = sin ((
double) (MagickPI*regA)) / (MagickPI*regA);
3683 regA = sinh ((
double) regA);
3686 regA = sin ((
double) regA);
3689 regA = sqrt ((
double) regA);
3692 regA = 1.0 / (1.0 + exp ((
double) -regA));
3695 regA = tanh ((
double) regA);
3698 regA = tan ((
double) regA);
3701 if (regA >= 0) regA = floor ((
double) regA);
3702 else regA = ceil ((
double) regA);
3714 ssize_t ImgNum = ChkImgNum (pfx, regA);
3715 if (ImgNum < 0)
break;
3716 regA = (fxFltType) 0;
3718 Image * pimg = pfx->Images[0];
3719 if (pel->img_attr_qual == aNull) {
3720 if ((
int) pel->channel_qual < 0) {
3721 if (pel->channel_qual == NO_CHAN_QUAL || pel->channel_qual == THIS_CHANNEL) {
3722 if (pfx->ImgNum==0) {
3723 regA = QuantumScale * (double) p[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3725 const Quantum * pv = GetCacheViewVirtualPixels (
3726 pfx->Imgs[0].View, imgx, imgy, 1,1, pfx->exception);
3728 (void) ThrowMagickException (
3729 pfx->exception, GetMagickModule(), OptionError,
3730 "fU can't get cache",
"%lu", (
unsigned long) ImgNum);
3733 regA = QuantumScale * (double) pv[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3735 }
else if (pel->channel_qual == HUE_CHANNEL || pel->channel_qual == SAT_CHANNEL ||
3736 pel->channel_qual == LIGHT_CHANNEL) {
3737 regA = GetHslInt (pfx, ImgNum, imgx, imgy, pel->channel_qual);
3739 }
else if (pel->channel_qual == INTENSITY_CHANNEL) {
3740 regA = GetIntensity (pfx, 0, (
double) imgx, (
double) imgy);
3744 if (pfx->ImgNum==0) {
3745 regA = QuantumScale * (double) p[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3747 const Quantum * pv = GetCacheViewVirtualPixels (
3748 pfx->Imgs[0].View, imgx, imgy, 1,1, pfx->exception);
3750 (void) ThrowMagickException (
3751 pfx->exception, GetMagickModule(), OptionError,
3752 "fU can't get cache",
"%lu", (
unsigned long) ImgNum);
3755 regA = QuantumScale * (double) pv[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3760 regA = ImageStat (pfx, 0, WHICH_ATTR_CHAN, pel->img_attr_qual);
3764 if (pel->img_attr_qual == aNull) {
3766 if ((
int) pel->channel_qual < 0) {
3767 if (pel->channel_qual == HUE_CHANNEL || pel->channel_qual == SAT_CHANNEL ||
3768 pel->channel_qual == LIGHT_CHANNEL)
3770 regA = GetHslInt (pfx, ImgNum, imgx, imgy, pel->channel_qual);
3772 }
else if (pel->channel_qual == INTENSITY_CHANNEL)
3774 regA = GetIntensity (pfx, ImgNum, (fxFltType) imgx, (fxFltType) imgy);
3779 pv = GetCacheViewVirtualPixels (
3780 pfx->Imgs[ImgNum].View, imgx, imgy, 1,1, pfx->exception);
3782 (void) ThrowMagickException (
3783 pfx->exception, GetMagickModule(), OptionError,
3784 "fU can't get cache",
"%lu", (
unsigned long) ImgNum);
3787 regA = QuantumScale * (double)
3788 pv[pfx->Images[ImgNum]->channel_map[WHICH_NON_ATTR_CHAN].offset];
3790 regA = ImageStat (pfx, ImgNum, WHICH_ATTR_CHAN, pel->img_attr_qual);
3799 Image * pimg = pfx->Images[0];
3800 if ((
int) pel->channel_qual < 0) {
3801 if (pel->channel_qual == NO_CHAN_QUAL || pel->channel_qual == THIS_CHANNEL) {
3803 if (pfx->ImgNum==0) {
3804 regA = QuantumScale * (double) p[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3806 const Quantum * pv = GetCacheViewVirtualPixels (
3807 pfx->Imgs[0].View, imgx, imgy, 1,1, pfx->exception);
3809 (void) ThrowMagickException (
3810 pfx->exception, GetMagickModule(), OptionError,
3811 "fU0 can't get cache",
"%i", 0);
3814 regA = QuantumScale * (double) pv[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3817 }
else if (pel->channel_qual == HUE_CHANNEL || pel->channel_qual == SAT_CHANNEL ||
3818 pel->channel_qual == LIGHT_CHANNEL) {
3819 regA = GetHslInt (pfx, 0, imgx, imgy, pel->channel_qual);
3821 }
else if (pel->channel_qual == INTENSITY_CHANNEL) {
3822 regA = GetIntensity (pfx, 0, (fxFltType) imgx, (fxFltType) imgy);
3825 if (pfx->ImgNum==0) {
3826 regA = QuantumScale * (double) p[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3828 const Quantum * pv = GetCacheViewVirtualPixels (
3829 pfx->Imgs[0].View, imgx, imgy, 1,1, pfx->exception);
3831 (void) ThrowMagickException (
3832 pfx->exception, GetMagickModule(), OptionError,
3833 "fU0 can't get cache",
"%i", 0);
3836 regA = QuantumScale * (double) pv[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3843 ssize_t ImgNum = ChkImgNum (pfx, regA);
3846 if (ImgNum < 0)
break;
3848 if (pel->is_relative) {
3856 if ((
int) pel->channel_qual < 0) {
3857 if (pel->channel_qual == HUE_CHANNEL || pel->channel_qual == SAT_CHANNEL
3858 || pel->channel_qual == LIGHT_CHANNEL) {
3859 regA = GetHslFlt (pfx, ImgNum, fx, fy, pel->channel_qual);
3861 }
else if (pel->channel_qual == INTENSITY_CHANNEL) {
3862 regA = GetIntensity (pfx, ImgNum, fx, fy);
3869 Image * imUP = pfx->Images[ImgNum];
3870 if (! InterpolatePixelChannel (imUP, pfx->Imgs[ImgNum].View, WHICH_NON_ATTR_CHAN,
3871 imUP->interpolate, (
double) fx, (
double) fy, &v, pfx->exception))
3873 (void) ThrowMagickException (
3874 pfx->exception, GetMagickModule(), OptionError,
3875 "fUP can't get interpolate",
"%lu", (
unsigned long) ImgNum);
3878 regA = v * QuantumScale;
3887 if (pel->operator_index == fS) ImgNum = pfx->ImgNum;
3889 if (pel->img_attr_qual == aNull) {
3890 const Quantum * pv = GetCacheViewVirtualPixels (
3891 pfx->Imgs[ImgNum].View, imgx, imgy, 1,1, pfx->exception);
3893 (void) ThrowMagickException (
3894 pfx->exception, GetMagickModule(), OptionError,
3895 "fV can't get cache",
"%lu", (
unsigned long) ImgNum);
3899 if ((
int) pel->channel_qual < 0) {
3900 if (pel->channel_qual == HUE_CHANNEL || pel->channel_qual == SAT_CHANNEL ||
3901 pel->channel_qual == LIGHT_CHANNEL) {
3902 regA = GetHslInt (pfx, ImgNum, imgx, imgy, pel->channel_qual);
3904 }
else if (pel->channel_qual == INTENSITY_CHANNEL) {
3905 regA = GetIntensity (pfx, ImgNum, (
double) imgx, (
double) imgy);
3910 regA = QuantumScale * (double)
3911 pv[pfx->Images[ImgNum]->channel_map[WHICH_NON_ATTR_CHAN].offset];
3913 regA = ImageStat (pfx, ImgNum, WHICH_ATTR_CHAN, pel->img_attr_qual);
3923 ssize_t ImgNum = pfx->ImgNum;
3924 if (pel->operator_index == fVP) ImgNum = 1;
3925 if (pel->is_relative) {
3932 if ((
int) pel->channel_qual < 0) {
3933 if (pel->channel_qual == HUE_CHANNEL || pel->channel_qual == SAT_CHANNEL ||
3934 pel->channel_qual == LIGHT_CHANNEL) {
3935 regA = GetHslFlt (pfx, ImgNum, fx, fy, pel->channel_qual);
3937 }
else if (pel->channel_qual == INTENSITY_CHANNEL) {
3938 regA = GetIntensity (pfx, ImgNum, fx, fy);
3946 if (! InterpolatePixelChannel (pfx->Images[ImgNum], pfx->Imgs[ImgNum].View,
3947 WHICH_NON_ATTR_CHAN, pfx->Images[ImgNum]->interpolate,
3948 (
double) fx, (
double) fy, &v, pfx->exception)
3951 (void) ThrowMagickException (
3952 pfx->exception, GetMagickModule(), OptionError,
3953 "fSP or fVP can't get interp",
"%lu", (
unsigned long) ImgNum);
3956 regA = v * (fxFltType)QuantumScale;
3964 regA = (fxFltType) GetImageDepth (img, pfx->exception);
3967 regA = (fxFltType) img->extent;
3971 regA = cs[WHICH_ATTR_CHAN].kurtosis;
3975 regA = cs[WHICH_ATTR_CHAN].maxima;
3979 regA = cs[WHICH_ATTR_CHAN].mean;
3983 regA = cs[WHICH_ATTR_CHAN].median;
3987 regA = cs[WHICH_ATTR_CHAN].minima;
3992 regA = (fxFltType) img->page.x;
3995 regA = (fxFltType) img->page.y;
3998 regA = (fxFltType) img->page.width;
4001 regA = (fxFltType) img->page.height;
4006 regA = (fxFltType) MagickSafeReciprocal (img->resolution.x) * img->columns;
4009 regA = (fxFltType) MagickSafeReciprocal (img->resolution.y) * img->rows;
4012 regA = (fxFltType) img->quality;
4017 regA = (fxFltType) img->resolution.x;
4020 regA = (fxFltType) img->resolution.y;
4024 regA = cs[WHICH_ATTR_CHAN].skewness;
4028 regA = cs[WHICH_ATTR_CHAN].standard_deviation;
4031 regA = (fxFltType) img->rows;
4034 regA = (fxFltType) pfx->ImgListLen;
4037 regA = (fxFltType) pfx->ImgNum;
4040 regA = (fxFltType) img->columns;
4043 regA = (fxFltType) GetImageDepth (img, pfx->exception);
4051 regA = GetIntensity (pfx, pfx->ImgNum, (
double) imgx, (
double) imgy);
4058 regA = QuantumScale * (0.212656 * (double) GetPixelRed (img,p) +
4059 0.715158 * (double) GetPixelGreen (img,p) +
4060 0.072186 * (double) GetPixelBlue (img,p));
4066 regA = QuantumScale * (double) GetPixelAlpha (img, p);
4069 regA = QuantumScale * (double) GetPixelBlue (img, p);
4072 regA = QuantumScale * (double) GetPixelCyan (img, p);
4075 regA = QuantumScale * (double) GetPixelGreen (img, p);
4078 regA = (fxFltType) imgx;
4081 regA = (fxFltType) imgy;
4084 regA = QuantumScale * (double) GetPixelBlack (img, p);
4087 regA = QuantumScale * (double) GetPixelGreen (img, p);
4090 regA = QuantumScale * (double) GetPixelAlpha (img, p);
4093 regA = QuantumScale * (double) GetPixelRed (img, p);
4096 regA = QuantumScale * (double) GetPixelYellow (img, p);
4102 assert (pel->element_index >= 0);
4103 i = pel->element_index-1;
4106 assert (pel->element_index >= 0);
4107 i = pel->element_index-1;
4108 if (IsImageTTLExpired(img) != MagickFalse) {
4109 i = pfx->usedElements-1;
4110 (void) ThrowMagickException (pfx->exception, GetMagickModule(),
4111 ResourceLimitFatalError,
"TimeLimitExceeded",
"`%s'", img->filename);
4115 assert (pel->element_index >= 0);
4116 if (fabs((
double) regA) < MagickEpsilon) i = pel->element_index-1;
4118 case rIfNotZeroGoto:
4119 assert (pel->element_index >= 0);
4120 if (fabs((
double) regA) > MagickEpsilon) i = pel->element_index-1;
4123 assert (pel->element_index >= 0);
4124 regA = pfxrt->UserSymVals[pel->element_index];
4127 assert (pel->element_index >= 0);
4128 pfxrt->UserSymVals[pel->element_index] = regA;
4131 pfxrt->usedValStack = 0;
4137 (void) ThrowMagickException (
4138 pfx->exception, GetMagickModule(), OptionError,
4139 "pel->oprNum",
"%i '%s' not yet implemented",
4140 (int)pel->operator_index, OprStr(pel->operator_index));
4144 if (!PushVal (pfx, pfxrt, regA, i))
break;
4147 if (pfxrt->usedValStack > 0) regA = PopVal (pfx, pfxrt, 9999);
4153 if (pfx->exception->severity >= ErrorException)
4156 if (pfxrt->usedValStack != 0) {
4157 (void) ThrowMagickException (
4158 pfx->exception, GetMagickModule(), OptionError,
4159 "ValStack not empty",
"(%i)", pfxrt->usedValStack);
4168MagickPrivate MagickBooleanType FxEvaluateChannelExpression (
4170 const PixelChannel channel,
const ssize_t x,
const ssize_t y,
4174 id = GetOpenMPThreadId();
4178 assert (pfx != NULL);
4179 assert (pfx->image != NULL);
4180 assert (pfx->Images != NULL);
4181 assert (pfx->Imgs != NULL);
4182 assert (pfx->fxrts != NULL);
4184 pfx->fxrts[id].thisPixel = NULL;
4187 if (!ExecuteRPN (pfx, &pfx->fxrts[
id], &ret, channel, x, y)) {
4188 (void) ThrowMagickException (
4189 exception, GetMagickModule(), OptionError,
4190 "ExecuteRPN failed",
" ");
4194 *result = (double) ret;
4199static FxInfo *AcquireFxInfoPrivate (
const Image * images,
const char * expression,
4204 FxInfo * pfx = (
FxInfo*) AcquireCriticalMemory (
sizeof (*pfx));
4206 memset (pfx, 0,
sizeof (*pfx));
4208 if (!InitFx (pfx, images, CalcAllStats, exception)) {
4209 pfx = (
FxInfo*) RelinquishMagickMemory(pfx);
4213 if (!BuildRPN (pfx)) {
4214 (void) DeInitFx (pfx);
4215 pfx = (
FxInfo*) RelinquishMagickMemory(pfx);
4219 if ((*expression ==
'@') && (strlen(expression) > 1))
4220 pfx->expression=FileToString(expression,~0UL,exception);
4221 if (pfx->expression == (
char *) NULL)
4222 pfx->expression=ConstantString(expression);
4223 pfx->pex = (
char *) pfx->expression;
4226 if (!TranslateStatementList (pfx,
";", &chLimit)) {
4227 (void) DestroyRPN (pfx);
4228 pfx->expression = DestroyString (pfx->expression);
4230 (void) DeInitFx (pfx);
4231 pfx = (
FxInfo*) RelinquishMagickMemory(pfx);
4236 (void) ThrowMagickException (
4237 pfx->exception, GetMagickModule(), OptionError,
4238 "Translate expression depth",
"(%i) not 0",
4241 (void) DestroyRPN (pfx);
4242 pfx->expression = DestroyString (pfx->expression);
4244 (void) DeInitFx (pfx);
4245 pfx = (
FxInfo*) RelinquishMagickMemory(pfx);
4249 if (chLimit !=
'\0' && chLimit !=
';') {
4250 (void) ThrowMagickException (
4251 pfx->exception, GetMagickModule(), OptionError,
4252 "AcquireFxInfo: TranslateExpression did not exhaust input",
"(chLimit=%i) at'%s'",
4253 (int)chLimit, pfx->pex);
4255 (void) DestroyRPN (pfx);
4256 pfx->expression = DestroyString (pfx->expression);
4258 (void) DeInitFx (pfx);
4259 pfx = (
FxInfo*) RelinquishMagickMemory(pfx);
4263 if (pfx->NeedStats && pfx->runType == rtEntireImage && !pfx->statistics) {
4264 if (!CollectStatistics (pfx)) {
4265 (void) DestroyRPN (pfx);
4266 pfx->expression = DestroyString (pfx->expression);
4268 (void) DeInitFx (pfx);
4269 pfx = (
FxInfo*) RelinquishMagickMemory(pfx);
4274 if (pfx->DebugOpt) {
4275 DumpTables (stderr);
4276 DumpUserSymbols (pfx, stderr);
4277 (void) DumpRPN (pfx, stderr);
4281 size_t number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
4284 pfx->fxrts = (
fxRtT *)AcquireQuantumMemory (number_threads,
sizeof(
fxRtT));
4286 (void) ThrowMagickException (
4287 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
4289 (
unsigned long) number_threads);
4290 (void) DestroyRPN (pfx);
4291 pfx->expression = DestroyString (pfx->expression);
4293 (void) DeInitFx (pfx);
4294 pfx = (
FxInfo*) RelinquishMagickMemory(pfx);
4297 for (t=0; t < (ssize_t) number_threads; t++) {
4298 if (!AllocFxRt (pfx, &pfx->fxrts[t])) {
4299 (void) ThrowMagickException (
4300 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
4301 "AllocFxRt t=",
"%g",
4305 for (t2 = t-1; t2 >= 0; t2--) {
4306 DestroyFxRt (&pfx->fxrts[t]);
4309 pfx->fxrts = (
fxRtT *) RelinquishMagickMemory (pfx->fxrts);
4310 (void) DestroyRPN (pfx);
4311 pfx->expression = DestroyString (pfx->expression);
4313 (void) DeInitFx (pfx);
4314 pfx = (
FxInfo*) RelinquishMagickMemory(pfx);
4324 return AcquireFxInfoPrivate (images, expression, MagickFalse, exception);
4331 assert (pfx != NULL);
4332 assert (pfx->image != NULL);
4333 assert (pfx->Images != NULL);
4334 assert (pfx->Imgs != NULL);
4335 assert (pfx->fxrts != NULL);
4337 for (t=0; t < (ssize_t) GetMagickResourceLimit(ThreadResource); t++) {
4338 DestroyFxRt (&pfx->fxrts[t]);
4340 pfx->fxrts = (
fxRtT *) RelinquishMagickMemory (pfx->fxrts);
4344 pfx->expression = DestroyString (pfx->expression);
4347 (void) DeInitFx (pfx);
4349 pfx = (
FxInfo*) RelinquishMagickMemory(pfx);
4356MagickExport
Image *FxImage(
const Image *image,
const char *expression,
4359#define FxImageTag "FxNew/Image"
4380 assert(image != (
Image *) NULL);
4381 assert(image->signature == MagickCoreSignature);
4382 if (IsEventLogging() != MagickFalse)
4383 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4384 if (expression == (
const char *) NULL)
4385 return(CloneImage(image,0,0,MagickTrue,exception));
4386 fx_image=CloneImage(image,0,0,MagickTrue,exception);
4387 if (!fx_image)
return NULL;
4388 if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse) {
4389 fx_image=DestroyImage(fx_image);
4393 pfx = AcquireFxInfoPrivate (image, expression, MagickTrue, exception);
4396 fx_image=DestroyImage(fx_image);
4400 assert (pfx->image != NULL);
4401 assert (pfx->Images != NULL);
4402 assert (pfx->Imgs != NULL);
4403 assert (pfx->fxrts != NULL);
4407 image_view = AcquireVirtualCacheView (image, pfx->exception);
4408 fx_view = AcquireAuthenticCacheView (fx_image, pfx->exception);
4409#if defined(MAGICKCORE_OPENMP_SUPPORT)
4410 #pragma omp parallel for schedule(dynamic) shared(progress,status) \
4411 magick_number_threads(image,fx_image,fx_image->rows, \
4412 pfx->ContainsDebug ? 0 : 1)
4414 for (y=0; y < (ssize_t) fx_image->rows; y++)
4417 id = GetOpenMPThreadId();
4431 if (status == MagickFalse)
4433 p = GetCacheViewVirtualPixels (image_view, 0, y, image->columns, 1, pfx->exception);
4434 q = QueueCacheViewAuthenticPixels (fx_view, 0, y, fx_image->columns, 1, pfx->exception);
4435 if ((p == (
const Quantum *) NULL) || (q == (Quantum *) NULL)) {
4439 for (x=0; x < (ssize_t) fx_image->columns; x++) {
4442 pfx->fxrts[id].thisPixel = (Quantum *)p;
4444 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4446 PixelChannel channel = GetPixelChannelChannel (image, i);
4447 PixelTrait traits = GetPixelChannelTraits (image, channel);
4448 PixelTrait fx_traits = GetPixelChannelTraits (fx_image, channel);
4449 if ((traits == UndefinedPixelTrait) ||
4450 (fx_traits == UndefinedPixelTrait))
4452 if ((fx_traits & CopyPixelTrait) != 0) {
4453 SetPixelChannel (fx_image, channel, p[i], q);
4457 if (!ExecuteRPN (pfx, &pfx->fxrts[
id], &result, channel, x, y)) {
4462 q[i] = ClampToQuantum ((MagickRealType) (QuantumRange*result));
4464 p+=(ptrdiff_t) GetPixelChannels (image);
4465 q+=(ptrdiff_t) GetPixelChannels (fx_image);
4467 if (SyncCacheViewAuthenticPixels(fx_view, pfx->exception) == MagickFalse)
4469 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4474#if defined(MAGICKCORE_OPENMP_SUPPORT)
4478 proceed = SetImageProgress (image, FxImageTag, progress, image->rows);
4479 if (proceed == MagickFalse)
4484 fx_view = DestroyCacheView (fx_view);
4485 image_view = DestroyCacheView (image_view);
4489 if (pfx->DebugOpt && pfx->usedUserSymbols) {
4491 char UserSym[MagickPathExtent];
4492 fprintf (stderr,
"User symbols (%i):\n", pfx->usedUserSymbols);
4493 for (t=0; t < (int) GetMagickResourceLimit(ThreadResource); t++) {
4494 for (i = 0; i < (int) pfx->usedUserSymbols; i++) {
4495 fprintf (stderr,
"th=%i us=%i '%s': %.*Lg\n",
4496 t, i, NameOfUserSym (pfx, i, UserSym), pfx->precision, pfx->fxrts[t].UserSymVals[i]);
4501 if ((status == MagickFalse) || (pfx->exception->severity >= ErrorException))
4502 fx_image=DestroyImage(fx_image);
4504 pfx=DestroyFxInfo(pfx);