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[] = {
717 RandomInfo * magick_restrict random_info;
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;
750 ChannelStatistics ** statistics;
755 **magick_restrict random_infos;
760 ExceptionInfo * exception;
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,
784 MagickBooleanType CalcAllStats, ExceptionInfo *exception)
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);
836 if (pfx->Images == (Image **) NULL) {
837 for ( ; i > 0; i--) {
838 ImgT * pimg = &pfx->Imgs[i-1];
839 pimg->View = DestroyCacheView (pimg->View);
841 pfx->Imgs=(
ImgT *) RelinquishMagickMemory (pfx->Imgs);
842 pfx->random_infos = DestroyRandomInfoTLS (pfx->random_infos);
849static MagickBooleanType DeInitFx (FxInfo * pfx)
853 if (pfx->Images) pfx->Images = (Image**) RelinquishMagickMemory (pfx->Images);
856 for (i = (ssize_t)GetImageListLength(pfx->image); i > 0; i--) {
857 ImgT * pimg = &pfx->Imgs[i-1];
858 pimg->View = DestroyCacheView (pimg->View);
860 pfx->Imgs=(
ImgT *) RelinquishMagickMemory (pfx->Imgs);
862 pfx->random_infos = DestroyRandomInfoTLS (pfx->random_infos);
864 if (pfx->statistics) {
865 for (i = (ssize_t)GetImageListLength(pfx->image); i > 0; i--) {
866 pfx->statistics[i-1]=(ChannelStatistics *) RelinquishMagickMemory (pfx->statistics[i-1]);
869 pfx->statistics = (ChannelStatistics**) RelinquishMagickMemory(pfx->statistics);
875static ElementTypeE TypeOfOpr (
int op)
877 if (op < oNull)
return etOperator;
878 if (op == oNull)
return etConstant;
879 if (op <= fNull)
return etFunction;
880 if (op <= aNull)
return etImgAttr;
881 if (op <= sNull)
return etSymbol;
882 if (op <= rNull)
return etControl;
884 return (ElementTypeE) 0;
887static char * SetPtrShortExp (FxInfo *pfx,
char *pExp,
size_t len)
894 *pfx->ShortExp =
'\0';
897 slen = CopyMagickString(pfx->ShortExp, pExp, MagickPathExtent);
900 (void) CopyMagickString(pfx->ShortExp + MaxLen,
"...", 4);
903 p = strchr(pfx->ShortExp,
'\n');
904 if (p) (void) CopyMagickString(p,
"...", 4);
906 p = strchr(pfx->ShortExp,
'\r');
907 if (p) (void) CopyMagickString(p,
"...", 4);
910 return pfx->ShortExp;
913static char * SetShortExp (FxInfo * pfx)
915 return SetPtrShortExp (pfx, pfx->pex, MaxTokenLen-1);
918static int FindUserSymbol (FxInfo * pfx,
char * name)
925 lenName = strlen (name);
926 for (i=0; i < pfx->usedUserSymbols; i++) {
928 if (lenName == pus->len && LocaleNCompare (name, pus->pex, lenName)==0)
break;
930 if (i == pfx->usedUserSymbols)
return NULL_ADDRESS;
934static MagickBooleanType ExtendUserSymbols (FxInfo * pfx)
942 symbol_count = (int) ceil (pfx->numUserSymbols * (1 + TableExtend));
943 new_symbols = (
UserSymbolT *) AcquireQuantumMemory ((
size_t) symbol_count,
944 sizeof (*new_symbols));
946 (void) ThrowMagickException (
947 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
952 if (pfx->usedUserSymbols > 0)
953 (void) memcpy (new_symbols, pfx->UserSymbols,
954 (
size_t) pfx->usedUserSymbols * sizeof (*new_symbols));
955 pfx->UserSymbols = (
UserSymbolT *) RelinquishMagickMemory (pfx->UserSymbols);
956 pfx->UserSymbols = new_symbols;
957 pfx->numUserSymbols = symbol_count;
962static int AddUserSymbol (FxInfo * pfx,
char * pex,
size_t len)
965 if (pfx->usedUserSymbols + 1 >= pfx->numUserSymbols) {
966 if (!ExtendUserSymbols (pfx))
return -1;
968 pus = &pfx->UserSymbols[pfx->usedUserSymbols++];
972 return pfx->usedUserSymbols-1;
975static void DumpTables (FILE * fh)
979 for (i=0; i <= rNull; i++) {
980 const char * str =
"";
981 if ( i < oNull) str = Operators[i].str;
982 if (i >= (
int) FirstFunc && i < fNull) str = Functions[i-(int) FirstFunc].str;
983 if (i >= (
int) FirstImgAttr && i < aNull) str = ImgAttrs[i-(int) FirstImgAttr].str;
984 if (i >= (
int) FirstSym && i < sNull) str = Symbols[i-(int) FirstSym].str;
985 if (i >= (
int) FirstCont && i < rNull) str = Controls[i-(int) FirstCont].str;
986 if (i==0 ) fprintf (fh,
"Operators:\n ");
987 else if (i==oNull) fprintf (fh,
"\nFunctions:\n ");
988 else if (i==fNull) fprintf (fh,
"\nImage attributes:\n ");
989 else if (i==aNull) fprintf (fh,
"\nSymbols:\n ");
990 else if (i==sNull) fprintf (fh,
"\nControls:\n ");
991 fprintf (fh,
" %s", str);
996static char * NameOfUserSym (FxInfo * pfx,
int ndx,
char * buf)
999 assert (ndx >= 0 && ndx < pfx->usedUserSymbols);
1000 pus = &pfx->UserSymbols[ndx];
1001 (void) CopyMagickString (buf, pus->pex, pus->len+1);
1005static void DumpUserSymbols (FxInfo * pfx, FILE * fh)
1007 char UserSym[MagickPathExtent];
1009 fprintf (fh,
"UserSymbols (%i)\n", pfx->usedUserSymbols);
1010 for (i=0; i < pfx->usedUserSymbols; i++) {
1011 fprintf (fh,
" %i: '%s'\n", i, NameOfUserSym (pfx, i, UserSym));
1015static MagickBooleanType BuildRPN (FxInfo * pfx)
1017 pfx->numUserSymbols = InitNumUserSymbols;
1018 pfx->usedUserSymbols = 0;
1019 pfx->UserSymbols = (
UserSymbolT*) AcquireMagickMemory ((
size_t) pfx->numUserSymbols *
sizeof(
UserSymbolT));
1020 if (!pfx->UserSymbols) {
1021 (void) ThrowMagickException (
1022 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
1023 "UserSymbols",
"%i",
1024 pfx->numUserSymbols);
1028 pfx->numElements = RpnInit;
1029 pfx->usedElements = 0;
1030 pfx->Elements = NULL;
1032 pfx->Elements = (
ElementT*) AcquireMagickMemory ((
size_t) pfx->numElements *
sizeof(
ElementT));
1034 if (!pfx->Elements) {
1035 (void) ThrowMagickException (
1036 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
1042 pfx->usedOprStack = 0;
1043 pfx->maxUsedOprStack = 0;
1044 pfx->numOprStack = InitNumOprStack;
1045 pfx->OperatorStack = (OperatorE*) AcquireMagickMemory ((
size_t) pfx->numOprStack *
sizeof(OperatorE));
1046 if (!pfx->OperatorStack) {
1047 (void) ThrowMagickException (
1048 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
1049 "OperatorStack",
"%i",
1057static MagickBooleanType AllocFxRt (FxInfo * pfx,
fxRtT * pfxrt)
1061 pfxrt->random_info = AcquireRandomInfo ();
1062 pfxrt->thisPixel = NULL;
1064 nRnd = 20 + 10 * (int) GetPseudoRandomValue (pfxrt->random_info);
1065 for (i=0; i < nRnd; i++) (
void) GetPseudoRandomValue (pfxrt->random_info);;
1067 pfxrt->usedValStack = 0;
1068 pfxrt->numValStack = 2 * pfx->maxUsedOprStack;
1069 if (pfxrt->numValStack < MinValStackSize) pfxrt->numValStack = MinValStackSize;
1070 pfxrt->ValStack = (fxFltType*) AcquireMagickMemory ((
size_t) pfxrt->numValStack *
sizeof(fxFltType));
1071 if (!pfxrt->ValStack) {
1072 (void) ThrowMagickException (
1073 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
1075 pfxrt->numValStack);
1079 pfxrt->UserSymVals = NULL;
1081 if (pfx->usedUserSymbols) {
1082 pfxrt->UserSymVals = (fxFltType*) AcquireMagickMemory ((
size_t) pfx->usedUserSymbols *
sizeof(fxFltType));
1083 if (!pfxrt->UserSymVals) {
1084 (void) ThrowMagickException (
1085 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
1086 "UserSymVals",
"%i",
1087 pfx->usedUserSymbols);
1090 for (i = 0; i < pfx->usedUserSymbols; i++) pfxrt->UserSymVals[i] = (fxFltType) 0;
1096static MagickBooleanType ExtendRPN(FxInfo * pfx)
1107 element_count=(int) ceil(pfx->numElements*(1+TableExtend));
1108 new_elements=(
ElementT *) AcquireQuantumMemory((
size_t) element_count,
1109 sizeof(*new_elements));
1110 if (new_elements == (
ElementT *) NULL)
1112 (void) ThrowMagickException(pfx->exception,GetMagickModule(),
1113 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"RPN table");
1114 return(MagickFalse);
1116 (void) memcpy(new_elements,pfx->Elements,(
size_t) pfx->usedElements*
1117 sizeof(*new_elements));
1118 pfx->Elements=(
ElementT *) RelinquishMagickMemory(pfx->Elements);
1119 pfx->Elements=new_elements;
1120 pfx->numElements=element_count;
1123 (void) ThrowMagickException(pfx->exception,GetMagickModule(),
1124 ResourceLimitFatalError,
"Elements",
"%i",pfx->numElements);
1125 return(MagickFalse);
1130static inline MagickBooleanType OprInPlace(
int op)
1132 return(op >= oAddEq && op <= oSubSub ? MagickTrue : MagickFalse);
1135static const char * OprStr (
int oprNum)
1138 if (oprNum < 0) str =
"bad OprStr";
1139 else if (oprNum <= oNull) str = Operators[oprNum].str;
1140 else if (oprNum <= fNull) str = Functions[oprNum-(int) FirstFunc].str;
1141 else if (oprNum <= aNull) str = ImgAttrs[oprNum-(int) FirstImgAttr].str;
1142 else if (oprNum <= sNull) str = Symbols[oprNum-(int) FirstSym].str;
1143 else if (oprNum <= rNull) str = Controls[oprNum-(int) FirstCont].str;
1150static MagickBooleanType DumpRPN (FxInfo * pfx, FILE * fh)
1154 fprintf (fh,
"DumpRPN:");
1155 fprintf (fh,
" numElements=%i", pfx->numElements);
1156 fprintf (fh,
" usedElements=%i", pfx->usedElements);
1157 fprintf (fh,
" maxUsedOprStack=%i", pfx->maxUsedOprStack);
1158 fprintf (fh,
" ImgListLen=%g", (
double) pfx->ImgListLen);
1159 fprintf (fh,
" NeedStats=%s", pfx->NeedStats ?
"yes" :
"no");
1160 fprintf (fh,
" GotStats=%s", pfx->GotStats ?
"yes" :
"no");
1161 fprintf (fh,
" NeedHsl=%s\n", pfx->NeedHsl ?
"yes" :
"no");
1162 if (pfx->runType==rtEntireImage) fprintf (fh,
"EntireImage");
1163 else if (pfx->runType==rtCornerOnly) fprintf (fh,
"CornerOnly");
1167 for (i=0; i < pfx->usedElements; i++) {
1168 ElementT * pel = &pfx->Elements[i];
1169 pel->number_dest = 0;
1171 for (i=0; i < pfx->usedElements; i++) {
1172 ElementT * pel = &pfx->Elements[i];
1173 if (pel->operator_index == rGoto || pel->operator_index == rGotoChk || pel->operator_index == rIfZeroGoto || pel->operator_index == rIfNotZeroGoto) {
1174 if (pel->element_index >= 0 && pel->element_index < pfx->numElements) {
1175 ElementT * pelDest = &pfx->Elements[pel->element_index];
1176 pelDest->number_dest++;
1180 for (i=0; i < pfx->usedElements; i++) {
1181 char UserSym[MagickPathExtent];
1183 ElementT * pel = &pfx->Elements[i];
1184 const char * str = OprStr (pel->operator_index);
1185 const char *sRelAbs =
"";
1187 if (pel->operator_index == fP || pel->operator_index == fUP || pel->operator_index == fVP || pel->operator_index == fSP)
1188 sRelAbs = pel->is_relative ?
"[]" :
"{}";
1190 if (pel->type == etColourConstant)
1191 fprintf (fh,
" %i: %s vals=%.*Lg,%.*Lg,%.*Lg '%s%s' nArgs=%i ndx=%i %s",
1192 i, sElementTypes[pel->type],
1193 pfx->precision, pel->val, pfx->precision, pel->val1, pfx->precision, pel->val2,
1194 str, sRelAbs, pel->number_args, pel->element_index,
1195 pel->do_push ?
"push" :
"NO push");
1197 fprintf (fh,
" %i: %s val=%.*Lg '%s%s' nArgs=%i ndx=%i %s",
1198 i, sElementTypes[pel->type], pfx->precision, pel->val, str, sRelAbs,
1199 pel->number_args, pel->element_index,
1200 pel->do_push ?
"push" :
"NO push");
1202 if (pel->img_attr_qual != aNull)
1203 fprintf (fh,
" ia=%s", OprStr((
int) pel->img_attr_qual));
1205 if (pel->channel_qual != NO_CHAN_QUAL) {
1206 if (pel->channel_qual == THIS_CHANNEL) fprintf (fh,
" ch=this");
1207 else fprintf (fh,
" ch=%i", pel->channel_qual);
1210 if (pel->operator_index == rCopyTo) {
1211 fprintf (fh,
" CopyTo ==> %s", NameOfUserSym (pfx, pel->element_index, UserSym));
1212 }
else if (pel->operator_index == rCopyFrom) {
1213 fprintf (fh,
" CopyFrom <== %s", NameOfUserSym (pfx, pel->element_index, UserSym));
1214 }
else if (OprInPlace (pel->operator_index)) {
1215 fprintf (fh,
" <==> %s", NameOfUserSym (pfx, pel->element_index, UserSym));
1217 if (pel->number_dest > 0) fprintf (fh,
" <==dest(%i)", pel->number_dest);
1223static void DestroyRPN (FxInfo * pfx)
1225 pfx->numOprStack = 0;
1226 pfx->usedOprStack = 0;
1227 if (pfx->OperatorStack) pfx->OperatorStack = (OperatorE*) RelinquishMagickMemory (pfx->OperatorStack);
1229 pfx->numElements = 0;
1230 pfx->usedElements = 0;
1231 if (pfx->Elements) pfx->Elements = (
ElementT*) RelinquishMagickMemory (pfx->Elements);
1233 pfx->usedUserSymbols = 0;
1234 if (pfx->UserSymbols) pfx->UserSymbols = (
UserSymbolT*) RelinquishMagickMemory (pfx->UserSymbols);
1237static void DestroyFxRt (
fxRtT * pfxrt)
1239 pfxrt->usedValStack = 0;
1240 if (pfxrt->ValStack) pfxrt->ValStack = (fxFltType*) RelinquishMagickMemory (pfxrt->ValStack);
1241 if (pfxrt->UserSymVals) pfxrt->UserSymVals = (fxFltType*) RelinquishMagickMemory (pfxrt->UserSymVals);
1243 pfxrt->random_info = DestroyRandomInfo (pfxrt->random_info);
1246static size_t GetToken (FxInfo * pfx)
1257 char * p = pfx->pex;
1261 if (!isalpha((
int) ((
unsigned char) *p)))
return 0;
1268 if (LocaleNCompare (p,
"icc-", 4) == 0) {
1271 while (isalpha ((
int) ((
unsigned char) *p))) { len++; p++; }
1272 }
else if (LocaleNCompare (p,
"device-", 7) == 0) {
1275 while (isalpha ((
int) ((
unsigned char) *p))) { len++; p++; }
1277 while (isalpha ((
int) ((
unsigned char) *p))) { len++; p++; }
1278 if (*p ==
'_') { len++; p++; }
1279 while (isalpha ((
int) ((
unsigned char) *p))) { len++; p++; }
1280 while (isdigit ((
int) ((
unsigned char) *p))) { len++; p++; }
1282 if (len >= MaxTokenLen) {
1283 (void) ThrowMagickException (
1284 pfx->exception, GetMagickModule(), OptionError,
1285 "GetToken: too long",
"%g at '%s'",
1286 (
double) len, SetShortExp(pfx));
1287 len = MaxTokenLen - 1;
1290 (void) CopyMagickString (pfx->token, pfx->pex, len+1);
1293 pfx->lenToken = strlen (pfx->token);
1297static MagickBooleanType TokenMaybeUserSymbol (FxInfo * pfx)
1299 char * p = pfx->token;
1302 if (!isalpha ((
int)*p++))
return MagickFalse;
1305 if (i < 2)
return MagickFalse;
1309static MagickBooleanType AddElement(FxInfo * pfx,fxFltType val,
int oprNum)
1313 assert (oprNum <= rNull);
1315 if (++pfx->usedElements >= pfx->numElements)
1317 if (ExtendRPN(pfx) == MagickFalse)
1319 pfx->usedElements--;
1320 return(MagickFalse);
1323 if (pfx->Elements == (
ElementT *) NULL)
1325 pfx->usedElements--;
1326 return(MagickFalse);
1328 pel = &pfx->Elements[pfx->usedElements-1];
1329 pel->type = TypeOfOpr (oprNum);
1331 pel->val1 = (fxFltType) 0;
1332 pel->val2 = (fxFltType) 0;
1333 pel->operator_index = oprNum;
1334 pel->do_push = MagickTrue;
1335 pel->element_index = 0;
1336 pel->channel_qual = NO_CHAN_QUAL;
1337 pel->img_attr_qual = aNull;
1338 pel->number_dest = 0;
1339 pel->exp_start = NULL;
1342 if (oprNum <= oNull) pel->number_args = Operators[oprNum].number_args;
1343 else if (oprNum <= fNull) pel->number_args = Functions[oprNum-(int) FirstFunc].number_args;
1344 else if (oprNum <= aNull) pel->number_args = 0;
1345 else if (oprNum <= sNull) pel->number_args = 0;
1346 else pel->number_args = Controls[oprNum-(int) FirstCont].number_args;
1351static MagickBooleanType AddAddressingElement (FxInfo * pfx,
int oprNum,
int EleNdx)
1354 if (!AddElement (pfx, (fxFltType) 0, oprNum))
return MagickFalse;
1355 pel = &pfx->Elements[pfx->usedElements-1];
1356 pel->element_index = EleNdx;
1357 if (oprNum == rGoto || oprNum == rGotoChk || oprNum == rIfZeroGoto || oprNum == rIfNotZeroGoto
1358 || oprNum == rZerStk)
1360 pel->do_push = MagickFalse;
1370static MagickBooleanType AddColourElement (FxInfo * pfx, fxFltType val0, fxFltType val1, fxFltType val2)
1373 if (!AddElement (pfx, val0, oNull))
return MagickFalse;
1374 pel = &pfx->Elements[pfx->usedElements-1];
1377 pel->type = etColourConstant;
1381static inline void SkipSpaces (FxInfo * pfx)
1383 while (isspace ((
int)*pfx->pex)) pfx->pex++;
1386static inline char PeekChar (FxInfo * pfx)
1392static inline MagickBooleanType PeekStr (FxInfo * pfx,
const char * str)
1396 return (LocaleNCompare (pfx->pex, str, strlen(str))==0 ? MagickTrue : MagickFalse);
1399static MagickBooleanType ExpectChar (FxInfo * pfx,
char c)
1401 if (PeekChar (pfx) != c) {
1402 (void) ThrowMagickException (
1403 pfx->exception, GetMagickModule(), OptionError,
1404 "Expected char",
"'%c' at '%s'", c, SetShortExp (pfx));
1411static int MaybeXYWH (FxInfo * pfx, ImgAttrE * pop)
1418 if (*pop != aPage && *pop != aPrintsize && *pop != aRes)
return 0;
1420 if (PeekChar (pfx) !=
'.')
return 0;
1422 if (!ExpectChar (pfx,
'.'))
return 0;
1424 (void) GetToken (pfx);
1425 if (LocaleCompare (
"x", pfx->token)==0) ret=1;
1426 else if (LocaleCompare (
"y", pfx->token)==0) ret=2;
1427 else if (LocaleCompare (
"width", pfx->token)==0) ret=3;
1428 else if (LocaleCompare (
"height", pfx->token)==0) ret=4;
1431 (void) ThrowMagickException (
1432 pfx->exception, GetMagickModule(), OptionError,
1433 "Invalid 'x' or 'y' or 'width' or 'height' token=",
"'%s' at '%s'",
1434 pfx->token, SetShortExp(pfx));
1436 if (*pop == aPage) (*pop) = (ImgAttrE) ((
int) *pop + ret);
1439 (void) ThrowMagickException (
1440 pfx->exception, GetMagickModule(), OptionError,
1441 "Invalid 'width' or 'height' token=",
"'%s' at '%s'",
1442 pfx->token, SetShortExp(pfx));
1444 (*pop) = (ImgAttrE) ((
int) *pop + ret);
1447 pfx->pex+=pfx->lenToken;
1452static MagickBooleanType ExtendOperatorStack (FxInfo * pfx)
1460 stack_count = (int) ceil (pfx->numOprStack * (1 + TableExtend));
1461 new_stack = (OperatorE *) AcquireQuantumMemory ((
size_t) stack_count,
1462 sizeof (*new_stack));
1463 if (new_stack == (OperatorE *) NULL) {
1464 (void) ThrowMagickException (
1465 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
1470 if (pfx->usedOprStack > 0)
1471 (void) memcpy (new_stack, pfx->OperatorStack,
1472 (
size_t) pfx->usedOprStack * sizeof (*new_stack));
1473 pfx->OperatorStack = (OperatorE *) RelinquishMagickMemory (pfx->OperatorStack);
1474 pfx->OperatorStack = new_stack;
1475 pfx->numOprStack = stack_count;
1479static MagickBooleanType PushOperatorStack (FxInfo * pfx,
int op)
1481 if (pfx->usedOprStack + 1 >= pfx->numOprStack) {
1482 if (!ExtendOperatorStack (pfx))
1485 pfx->OperatorStack[pfx->usedOprStack++] = (OperatorE) op;
1487 if (pfx->maxUsedOprStack < pfx->usedOprStack)
1488 pfx->maxUsedOprStack = pfx->usedOprStack;
1492static OperatorE GetLeadingOp (FxInfo * pfx)
1494 OperatorE op = oNull;
1496 if (*pfx->pex ==
'-') op = oUnaryMinus;
1497 else if (*pfx->pex ==
'+') op = oUnaryPlus;
1498 else if (*pfx->pex ==
'~') op = oBitNot;
1499 else if (*pfx->pex ==
'!') op = oLogNot;
1500 else if (*pfx->pex ==
'(') op = oOpenParen;
1505static inline MagickBooleanType OprIsUnaryPrefix (OperatorE op)
1507 return (op == oUnaryMinus || op == oUnaryPlus || op == oBitNot || op == oLogNot ? MagickTrue : MagickFalse);
1510static MagickBooleanType TopOprIsUnaryPrefix (FxInfo * pfx)
1512 if (!pfx->usedOprStack)
return MagickFalse;
1514 return OprIsUnaryPrefix (pfx->OperatorStack[pfx->usedOprStack-1]);
1517static MagickBooleanType PopOprOpenParen (FxInfo * pfx, OperatorE op)
1520 if (!pfx->usedOprStack)
return MagickFalse;
1522 if (pfx->OperatorStack[pfx->usedOprStack-1] != op)
return MagickFalse;
1524 pfx->usedOprStack--;
1529static int GetCoordQualifier (FxInfo * pfx,
int op)
1533 if (op != fU && op != fV && op != fS)
return -1;
1535 (void) GetToken (pfx);
1537 if (pfx->lenToken != 1) {
1540 if (*pfx->token !=
'p' && *pfx->token !=
'P')
return -1;
1541 if (!GetFunction (pfx, fP))
return -1;
1546static PixelChannel GetChannelQualifier (FxInfo * pfx,
int op)
1548 if (op == fU || op == fV || op == fP ||
1549 op == fUP || op == fVP ||
1550 op == fS || (op >= (
int) FirstImgAttr && op <= aNull)
1553 const ChannelT * pch = &Channels[0];
1554 (void) GetToken (pfx);
1557 if (LocaleCompare (pch->str, pfx->token)==0) {
1559 if (op >= (
int) FirstImgAttr && op <= (
int) ((OperatorE)aNull) &&
1560 ChanIsVirtual (pch->pixel_channel)
1563 (void) ThrowMagickException (
1564 pfx->exception, GetMagickModule(), OptionError,
1565 "Can't have image attribute with channel qualifier at",
"'%s' at '%s'",
1566 pfx->token, SetShortExp(pfx));
1567 return NO_CHAN_QUAL;
1570 pfx->pex += pfx->lenToken;
1571 return pch->pixel_channel;
1576 return NO_CHAN_QUAL;
1579static ImgAttrE GetImgAttrToken (FxInfo * pfx)
1581 ImgAttrE ia = aNull;
1582 const char * iaStr =
"";
1583 for (ia = FirstImgAttr; ia < aNull; ia=(ImgAttrE) (ia+1)) {
1584 iaStr = ImgAttrs[ia-(int) FirstImgAttr].str;
1585 if (LocaleCompare (iaStr, pfx->token)==0) {
1586 pfx->pex += strlen(pfx->token);
1587 if (ImgAttrs[ia-(
int) FirstImgAttr].need_stats != MagickFalse) pfx->NeedStats = MagickTrue;
1588 MaybeXYWH (pfx, &ia);
1593 if (ia == aPage || ia == aPrintsize || ia == aRes) {
1594 (void) ThrowMagickException (
1595 pfx->exception, GetMagickModule(), OptionError,
1596 "Attribute",
"'%s' needs qualifier at '%s'",
1597 iaStr, SetShortExp(pfx));
1603static ImgAttrE GetImgAttrQualifier (FxInfo * pfx,
int op)
1605 ImgAttrE ia = aNull;
1606 if (op == (OperatorE)fU || op == (OperatorE)fV || op == (OperatorE)fP || op == (OperatorE)fS) {
1607 (void) GetToken (pfx);
1608 if (pfx->lenToken == 0) {
1611 ia = GetImgAttrToken (pfx);
1616static MagickBooleanType IsQualifier (FxInfo * pfx)
1618 if (PeekChar (pfx) ==
'.') {
1625static MagickBooleanType ParseISO860(
const char* text,
struct tm* tp)
1635 memset(tp,0,
sizeof(
struct tm));
1636 if (MagickSscanf(text,
"%d-%d-%dT%d:%d:%d",&year,&month,&day,&hour,&min,&sec) != 6)
1637 return(MagickFalse);
1638 tp->tm_year=year-1900;
1648static ssize_t GetProperty (FxInfo * pfx, fxFltType *val, fxFltType *seconds)
1655 if (seconds != NULL) *seconds = SECONDS_ERR;
1657 if (PeekStr (pfx,
"%[")) {
1660 char sProperty [MagickPathExtent];
1661 char * p = pfx->pex + 2;
1665 if (*p ==
'[') level++;
1666 else if (*p ==
']') {
1667 if (level == 0)
break;
1672 if (!*p || level != 0) {
1673 (void) ThrowMagickException (
1674 pfx->exception, GetMagickModule(), OptionError,
1675 "After '%[' expected ']' at",
"'%s'",
1680 len = (size_t) (p - pfx->pex + 1);
1681 if (len > MaxTokenLen) {
1682 (void) ThrowMagickException (
1683 pfx->exception, GetMagickModule(), OptionError,
1684 "Too much text between '%[' and ']' at",
"'%s'",
1689 (void) CopyMagickString (sProperty, pfx->pex, len+1);
1690 sProperty[len] =
'\0';
1694 text = InterpretImageProperties (pfx->image->image_info, pfx->image,
1695 sProperty, pfx->exception);
1696 if (!text || !*text) {
1697 text = DestroyString(text);
1698 (void) ThrowMagickException (
1699 pfx->exception, GetMagickModule(), OptionError,
1700 "Unknown property",
"'%s' at '%s'",
1701 sProperty, SetShortExp(pfx));
1705 if (seconds != NULL) {
1707 if (ParseISO860(text,&tp) == MagickFalse) {
1708 (void) ThrowMagickException (
1709 pfx->exception, GetMagickModule(), OptionError,
1710 "Function 'epoch' expected date property, found ",
"'%s' at '%s'",
1711 text, SetShortExp(pfx));
1712 text = DestroyString(text);
1713 *seconds = SECONDS_ERR;
1716 *seconds = (fxFltType)mktime (&tp);
1719 *val = strtold (text, &tailptr);
1720 if (text == tailptr) {
1721 (void) ThrowMagickException (
1722 pfx->exception, GetMagickModule(), OptionError,
1723 "Property",
"'%s' text '%s' is not a number at '%s'",
1724 sProperty, text, SetShortExp(pfx));
1725 text = DestroyString(text);
1729 text = DestroyString(text);
1731 return ((ssize_t) len);
1737static inline ssize_t GetConstantColour (FxInfo * pfx, fxFltType *v0, fxFltType *v1, fxFltType *v2)
1748 *dummy_exception = AcquireExceptionInfo ();
1758 char ColSp[MagickPathExtent];
1759 if (pfx->lenToken == 0) {
1760 dummy_exception = DestroyExceptionInfo (dummy_exception);
1763 (void) CopyMagickString (ColSp, pfx->token, MaxTokenLen);
1764 p = ColSp + pfx->lenToken - 1;
1765 if (*p ==
'a' || *p ==
'A') *p =
'\0';
1767 (void) GetPixelInfo (pfx->image, &colour);
1771 IsGray = (LocaleCompare (ColSp,
"gray") == 0) ? MagickTrue : MagickFalse;
1772 IsIcc = (LocaleCompare (ColSp,
"icc-color") == 0) ? MagickTrue : MagickFalse;
1773 IsDev = (LocaleNCompare (ColSp,
"device-", 7) == 0) ? MagickTrue : MagickFalse;
1777 if (!QueryColorCompliance (pfx->token, AllCompliance, &colour, dummy_exception) || IsGray) {
1778 ssize_t type = ParseCommandOption (MagickColorspaceOptions, MagickFalse, ColSp);
1779 if (type >= 0 || IsIcc || IsDev) {
1780 char * q = pfx->pex + pfx->lenToken;
1781 while (isspace((
int) ((
unsigned char) *q))) q++;
1784 char sFunc[MagickPathExtent];
1785 while (*q && *q !=
')') q++;
1787 (void) ThrowMagickException (
1788 pfx->exception, GetMagickModule(), OptionError,
1789 "constant color missing ')'",
"at '%s'",
1791 dummy_exception = DestroyExceptionInfo (dummy_exception);
1794 lenfun = (size_t) (q - pfx->pex + 1);
1795 if (lenfun > MaxTokenLen) {
1796 (void) ThrowMagickException (
1797 pfx->exception, GetMagickModule(), OptionError,
1798 "lenfun too long",
"'%lu' at '%s'",
1799 (
unsigned long) lenfun, SetShortExp(pfx));
1800 dummy_exception = DestroyExceptionInfo (dummy_exception);
1803 (void) CopyMagickString (sFunc, pfx->pex, lenfun+1);
1804 if (QueryColorCompliance (sFunc, AllCompliance, &colour, dummy_exception)) {
1805 *v0 = QuantumScale*colour.red;
1806 *v1 = QuantumScale*colour.green;
1807 *v2 = QuantumScale*colour.blue;
1808 dummy_exception = DestroyExceptionInfo (dummy_exception);
1809 return (ssize_t)lenfun;
1812 (void) ThrowMagickException (
1813 pfx->exception, GetMagickModule(), OptionError,
1814 "colorspace but not a valid color with '(...)' at",
"'%s'",
1816 dummy_exception = DestroyExceptionInfo (dummy_exception);
1821 dummy_exception = DestroyExceptionInfo (dummy_exception);
1826 *v0 = QuantumScale*colour.red;
1827 *v1 = QuantumScale*colour.green;
1828 *v2 = QuantumScale*colour.blue;
1830 dummy_exception = DestroyExceptionInfo (dummy_exception);
1831 return (ssize_t)strlen (pfx->token);
1834static inline ssize_t GetHexColour (FxInfo * pfx, fxFltType *v0, fxFltType *v1, fxFltType *v2)
1843 if (*pfx->pex !=
'#')
return 0;
1847 while (isxdigit ((
int)*p)) p++;
1848 if (isalpha ((
int)*p)) {
1849 (void) ThrowMagickException (
1850 pfx->exception, GetMagickModule(), OptionError,
1851 "Bad hex number at",
"'%s'",
1856 len = (size_t) (p - pfx->pex);
1857 if (len < 1)
return 0;
1858 if (len >= MaxTokenLen) {
1859 (void) ThrowMagickException (
1860 pfx->exception, GetMagickModule(), OptionError,
1861 "Hex colour too long at",
"'%s'",
1865 (void) CopyMagickString (pfx->token, pfx->pex, len+1);
1867 (void) GetPixelInfo (pfx->image, &colour);
1869 if (!QueryColorCompliance (pfx->token, AllCompliance, &colour, pfx->exception)) {
1870 (void) ThrowMagickException (
1871 pfx->exception, GetMagickModule(), OptionError,
1872 "QueryColorCompliance rejected",
"'%s' at '%s'",
1873 pfx->token, SetShortExp(pfx));
1877 *v0 = QuantumScale*colour.red;
1878 *v1 = QuantumScale*colour.green;
1879 *v2 = QuantumScale*colour.blue;
1881 return (ssize_t) len;
1884static MagickBooleanType GetFunction (FxInfo * pfx, FunctionE fe)
1888 const char * funStr = Functions[fe-(int) FirstFunc].str;
1889 int nArgs = Functions[fe-(int) FirstFunc].number_args;
1891 char expChLimit =
')';
1892 const char *strLimit =
",)";
1893 OperatorE pushOp = oOpenParen;
1900 int ndx0 = NULL_ADDRESS, ndx1 = NULL_ADDRESS, ndx2 = NULL_ADDRESS, ndx3 = NULL_ADDRESS;
1902 MagickBooleanType coordQual = MagickFalse;
1903 PixelChannel chQual = NO_CHAN_QUAL;
1904 ImgAttrE iaQual = aNull;
1906 pfx->pex += pfx->lenToken;
1909 char p = PeekChar (pfx);
1911 (void) ExpectChar (pfx,
'{');
1912 pushOp = oOpenBrace;
1916 }
else if (p==
'[') {
1917 (void) ExpectChar (pfx,
'[');
1918 pushOp = oOpenBracket;
1927 }
else if (fe == fU) {
1928 char p = PeekChar (pfx);
1930 (void) ExpectChar (pfx,
'[');
1931 pushOp = oOpenBracket;
1940 }
else if (fe == fV || fe == fS) {
1942 pushOp = oOpenBracket;
1946 if (!ExpectChar (pfx,
'('))
return MagickFalse;
1948 if (!PushOperatorStack (pfx, (
int) pushOp))
return MagickFalse;
1950 pExpStart = pfx->pex;
1951 ndx0 = pfx->usedElements;
1953 (void) AddAddressingElement (pfx, rGoto, NULL_ADDRESS);
1960 lenOptArt = GetProperty (pfx, &val, &seconds);
1961 if (seconds == SECONDS_ERR) {
1963 (void) ThrowMagickException (
1964 pfx->exception, GetMagickModule(), OptionError,
1965 "Function 'epoch' expected date property",
"at '%s'",
1969 if (lenOptArt < 0)
return MagickFalse;
1970 if (lenOptArt > 0) {
1971 (void) AddElement (pfx, seconds, oNull);
1972 pfx->pex += lenOptArt;
1973 if (!ExpectChar (pfx,
')'))
return MagickFalse;
1974 if (!PopOprOpenParen (pfx, pushOp))
return MagickFalse;
1981 if (TranslateStatementList (pfx, strLimit, &chLimit)) {
1985 (void) ThrowMagickException (
1986 pfx->exception, GetMagickModule(), OptionError,
1987 "For function",
"'%s' expected ')' at '%s'",
1988 funStr, SetShortExp(pfx));
1992 if (!chLimit)
break;
1993 if (fe == fP || fe == fS|| fe == fIf) {
1994 (void) AddElement (pfx, (fxFltType) 0, oNull);
1999 if (strchr (strLimit, chLimit)==NULL) {
2000 (void) ThrowMagickException (
2001 pfx->exception, GetMagickModule(), OptionError,
2002 "For function",
"'%s' expected one of '%s' after expression but found '%c' at '%s'",
2003 funStr, strLimit, chLimit ? chLimit :
' ', SetShortExp(pfx));
2012 if (ndx1 != NULL_ADDRESS) {
2013 (void) ThrowMagickException (
2014 pfx->exception, GetMagickModule(), OptionError,
2015 "For function",
"'%s' required argument is missing at '%s'",
2016 funStr, SetShortExp(pfx));
2019 ndx1 = pfx->usedElements;
2020 if (fe==fWhile || fe==fIf) {
2021 (void) AddAddressingElement (pfx, rIfZeroGoto, NULL_ADDRESS);
2022 }
else if (fe==fDo) {
2023 (void) AddAddressingElement (pfx, rIfZeroGoto, NULL_ADDRESS);
2024 }
else if (fe==fFor) {
2025 pfx->Elements[pfx->usedElements-1].do_push = MagickFalse;
2029 if (ndx2 != NULL_ADDRESS) {
2030 (void) ThrowMagickException (
2031 pfx->exception, GetMagickModule(), OptionError,
2032 "For function",
"'%s' required argument is missing at '%s'",
2033 funStr, SetShortExp(pfx));
2036 ndx2 = pfx->usedElements;
2038 pfx->Elements[pfx->usedElements-1].do_push = MagickFalse;
2039 (void) AddAddressingElement (pfx, rGotoChk, ndx0);
2040 }
else if (fe==fDo) {
2041 pfx->Elements[pfx->usedElements-1].do_push = MagickFalse;
2042 (void) AddAddressingElement (pfx, rGotoChk, ndx0 + 1);
2043 }
else if (fe==fFor) {
2044 (void) AddAddressingElement (pfx, rIfZeroGoto, NULL_ADDRESS);
2045 pfx->Elements[pfx->usedElements-1].do_push = MagickTrue;
2046 (void) AddAddressingElement (pfx, rZerStk, NULL_ADDRESS);
2047 }
else if (fe==fIf) {
2048 (void) AddAddressingElement (pfx, rGoto, NULL_ADDRESS);
2052 if (ndx3 != NULL_ADDRESS) {
2053 (void) ThrowMagickException (
2054 pfx->exception, GetMagickModule(), OptionError,
2055 "For function",
"'%s' required argument is missing at '%s'",
2056 funStr, SetShortExp(pfx));
2060 pfx->Elements[pfx->usedElements-1].do_push = MagickFalse;
2061 (void) AddAddressingElement (pfx, rGotoChk, ndx1);
2063 ndx3 = pfx->usedElements;
2068 if (chLimit == expChLimit) {
2069 lenExp = (size_t) (pfx->pex - pExpStart - 1);
2073 if (chLimit && chLimit != expChLimit && chLimit !=
',' ) {
2074 (void) ThrowMagickException (
2075 pfx->exception, GetMagickModule(), OptionError,
2076 "For function",
"'%s' expected '%c', found '%c' at '%s'",
2077 funStr, expChLimit, chLimit ? chLimit :
' ', SetShortExp(pfx));
2081 if (fe == fP || fe == fS || fe == fU || fe == fChannel) {
2082 while (FndArgs < Functions[fe-(
int) FirstFunc].number_args) {
2083 (void) AddElement (pfx, (fxFltType) 0, oNull);
2088 if (FndArgs > Functions[fe-(
int) FirstFunc].number_args)
2091 (void) ThrowMagickException (
2092 pfx->exception, GetMagickModule(), OptionError,
2093 "For function",
"'%s' expected up to %i arguments, found '%i' at '%s'",
2094 funStr, Functions[fe-(
int) FirstFunc].number_args, FndArgs, SetShortExp(pfx));
2096 (void) ThrowMagickException (
2097 pfx->exception, GetMagickModule(), OptionError,
2098 "For function",
"'%s' expected %i arguments, found '%i' at '%s'",
2099 funStr, Functions[fe-(
int) FirstFunc].number_args, FndArgs, SetShortExp(pfx));
2103 if (FndArgs < Functions[fe-(
int) FirstFunc].number_args) {
2104 (void) ThrowMagickException (
2105 pfx->exception, GetMagickModule(), OptionError,
2106 "For function",
"'%s' expected %i arguments, found too few (%i) at '%s'",
2107 funStr, Functions[fe-(
int) FirstFunc].number_args, FndArgs, SetShortExp(pfx));
2110 if (fe != fS && fe != fV && FndArgs == 0 && Functions[fe-(
int) FirstFunc].number_args == 0) {
2112 chLimit = expChLimit;
2113 if (!ExpectChar (pfx,
')'))
return MagickFalse;
2116 if (chLimit != expChLimit) {
2117 (void) ThrowMagickException (
2118 pfx->exception, GetMagickModule(), OptionError,
2119 "For function",
"'%s', arguments don't end with '%c' at '%s'",
2120 funStr, expChLimit, SetShortExp(pfx));
2123 if (!PopOprOpenParen (pfx, pushOp)) {
2124 (void) ThrowMagickException (
2125 pfx->exception, GetMagickModule(), OptionError,
2126 "Bug: For function",
"'%s' tos not '%s' at '%s'",
2127 funStr, Operators[pushOp].str, SetShortExp(pfx));
2131 if (IsQualifier (pfx)) {
2133 if (fe == fU || fe == fV || fe == fS) {
2135 coordQual = (GetCoordQualifier (pfx, (
int) fe) == 1) ? MagickTrue : MagickFalse;
2140 ElementT * pel = &pfx->Elements[pfx->usedElements-1];
2141 if (pel->operator_index != fP) {
2142 (void) ThrowMagickException (
2143 pfx->exception, GetMagickModule(), OptionError,
2144 "Bug: For function",
"'%s' last element not 'p' at '%s'",
2145 funStr, SetShortExp(pfx));
2148 chQual = pel->channel_qual;
2149 expChLimit = (pel->is_relative) ?
']' :
'}';
2150 pfx->usedElements--;
2151 if (fe == fU) fe = fUP;
2152 else if (fe == fV) fe = fVP;
2153 else if (fe == fS) fe = fSP;
2154 funStr = Functions[fe-(int) FirstFunc].str;
2158 if ( chQual == NO_CHAN_QUAL &&
2159 (fe == fP || fe == fS || fe == fSP || fe == fU || fe == fUP || fe == fV || fe == fVP)
2162 chQual = GetChannelQualifier (pfx, (
int) fe);
2165 if (chQual == NO_CHAN_QUAL && (fe == fU || fe == fV || fe == fS)) {
2167 iaQual = GetImgAttrQualifier (pfx, (
int) fe);
2169 if (IsQualifier (pfx) && chQual == NO_CHAN_QUAL && iaQual != aNull) {
2170 chQual = GetChannelQualifier (pfx, (
int) fe);
2172 if (coordQual && iaQual != aNull) {
2173 (void) ThrowMagickException (
2174 pfx->exception, GetMagickModule(), OptionError,
2175 "For function",
"'%s', can't have qualifiers 'p' and image attribute '%s' at '%s'",
2176 funStr, pfx->token, SetShortExp(pfx));
2179 if (!coordQual && chQual == NO_CHAN_QUAL && iaQual == aNull) {
2180 (void) ThrowMagickException (
2181 pfx->exception, GetMagickModule(), OptionError,
2182 "For function",
"'%s', bad qualifier '%s' at '%s'",
2183 funStr, pfx->token, SetShortExp(pfx));
2186 if (!coordQual && chQual == CompositePixelChannel && iaQual == aNull) {
2187 (void) ThrowMagickException (
2188 pfx->exception, GetMagickModule(), OptionError,
2189 "For function",
"'%s', bad composite qualifier '%s' at '%s'",
2190 funStr, pfx->token, SetShortExp(pfx));
2194 if (chQual == HUE_CHANNEL || chQual == SAT_CHANNEL || chQual == LIGHT_CHANNEL) {
2195 pfx->NeedHsl = MagickTrue;
2197 if (iaQual >= FirstImgAttr && iaQual < aNull) {
2198 (void) ThrowMagickException (
2199 pfx->exception, GetMagickModule(), OptionError,
2200 "Can't have image attribute with HLS qualifier at",
"'%s'",
2207 if (iaQual != aNull && chQual != NO_CHAN_QUAL) {
2208 if (ImgAttrs[iaQual-(
int) FirstImgAttr].need_stats == MagickFalse) {
2209 (void) ThrowMagickException (
2210 pfx->exception, GetMagickModule(), OptionError,
2211 "Can't have image attribute ",
"'%s' with channel qualifier '%s' at '%s'",
2212 ImgAttrs[iaQual-(
int) FirstImgAttr].str,
2213 pfx->token, SetShortExp(pfx));
2216 if (ChanIsVirtual (chQual)) {
2217 (void) ThrowMagickException (
2218 pfx->exception, GetMagickModule(), OptionError,
2219 "Can't have statistical image attribute ",
"'%s' with virtual channel qualifier '%s' at '%s'",
2220 ImgAttrs[iaQual-(
int) FirstImgAttr].str,
2221 pfx->token, SetShortExp(pfx));
2228 pfx->Elements[ndx1].element_index = ndx2+1;
2229 }
else if (fe==fDo) {
2230 pfx->Elements[ndx0].element_index = ndx1+1;
2231 pfx->Elements[ndx1].element_index = ndx2+1;
2232 }
else if (fe==fFor) {
2233 pfx->Elements[ndx2].element_index = ndx3;
2234 }
else if (fe==fIf) {
2235 pfx->Elements[ndx1].element_index = ndx2 + 1;
2236 pfx->Elements[ndx2].element_index = ndx3;
2238 if (fe == fU && iaQual == aNull) {
2239 ElementT * pel = &pfx->Elements[pfx->usedElements-1];
2240 if (pel->type == etConstant && pel->val == 0.0) {
2241 pfx->usedElements--;
2245 (void) AddElement (pfx, (fxFltType) 0, (
int) fe);
2246 if (fe == fP || fe == fU || fe == fU0 || fe == fUP ||
2247 fe == fV || fe == fVP || fe == fS || fe == fSP)
2249 ElementT * pel = &pfx->Elements[pfx->usedElements-1];
2250 pel->is_relative = (expChLimit ==
']' ? MagickTrue : MagickFalse);
2251 if (chQual >= 0) pel->channel_qual = chQual;
2252 if (iaQual != aNull && (fe == fU || fe == fV || fe == fS)) {
2254 pel->img_attr_qual = iaQual;
2259 if (pExpStart && lenExp) {
2260 ElementT * pel = &pfx->Elements[pfx->usedElements-1];
2261 pel->exp_start = pExpStart;
2262 pel->exp_len = lenExp;
2266 pfx->ContainsDebug = MagickTrue;
2271static MagickBooleanType IsStealth (
int op)
2273 return (op == fU0 || op == fUP || op == fSP || op == fVP ||
2274 (op >= FirstCont && op <= rNull) ? MagickTrue : MagickFalse
2278static MagickBooleanType GetOperand (
2279 FxInfo * pfx, MagickBooleanType * UserSymbol, MagickBooleanType * NewUserSymbol,
int * UserSymNdx,
2280 MagickBooleanType * needPopAll)
2283 *NewUserSymbol = *UserSymbol = MagickFalse;
2284 *UserSymNdx = NULL_ADDRESS;
2287 if (!*pfx->pex)
return MagickFalse;
2288 (void) GetToken (pfx);
2290 if (pfx->lenToken==0) {
2294 OperatorE op = GetLeadingOp (pfx);
2295 if (op==oOpenParen) {
2296 char chLimit =
'\0';
2297 if (!PushOperatorStack (pfx, (
int) op))
return MagickFalse;
2299 if (!TranslateExpression (pfx,
")", &chLimit, needPopAll)) {
2300 (void) ThrowMagickException (
2301 pfx->exception, GetMagickModule(), OptionError,
2302 "Empty expression in parentheses at",
"'%s'",
2306 if (chLimit !=
')') {
2307 (void) ThrowMagickException (
2308 pfx->exception, GetMagickModule(), OptionError,
2309 "'(' but no ')' at",
"'%s'",
2314 if (!PopOprOpenParen (pfx, oOpenParen)) {
2315 (void) ThrowMagickException (
2316 pfx->exception, GetMagickModule(), OptionError,
2317 "Bug: tos not '(' at",
"'%s'",
2322 }
else if (OprIsUnaryPrefix (op)) {
2323 MagickBooleanType operand_ok;
2324 if (!PushOperatorStack (pfx, (
int) op))
return MagickFalse;
2327 if (!*pfx->pex)
return MagickFalse;
2328 if (pfx->teDepth >= MagickMaxRecursionDepth) {
2329 (void) ThrowMagickException (
2330 pfx->exception, GetMagickModule(), OptionError,
2331 "Expression too deeply nested",
"(depth %i exceeds limit %i)",
2332 pfx->teDepth, MagickMaxRecursionDepth);
2336 operand_ok=GetOperand (pfx, UserSymbol, NewUserSymbol, UserSymNdx, needPopAll);
2339 (void) ThrowMagickException (
2340 pfx->exception, GetMagickModule(), OptionError,
2341 "After unary, bad operand at",
"'%s'",
2346 if (*NewUserSymbol) {
2347 (void) ThrowMagickException (
2348 pfx->exception, GetMagickModule(), OptionError,
2349 "After unary, NewUserSymbol at",
"'%s'",
2355 (void) AddAddressingElement (pfx, rCopyFrom, *UserSymNdx);
2356 *UserSymNdx = NULL_ADDRESS;
2358 *UserSymbol = MagickFalse;
2359 *NewUserSymbol = MagickFalse;
2362 (void) GetToken (pfx);
2364 }
else if (*pfx->pex ==
'#') {
2365 fxFltType v0=0, v1=0, v2=0;
2366 ssize_t lenToken = GetHexColour (pfx, &v0, &v1, &v2);
2368 (void) ThrowMagickException (
2369 pfx->exception, GetMagickModule(), OptionError,
2370 "Bad hex number at",
"'%s'",
2373 }
else if (lenToken > 0) {
2374 (void) AddColourElement (pfx, v0, v1, v2);
2385 fxFltType val = strtold (pfx->pex, &tailptr);
2386 if (pfx->pex != tailptr) {
2394 const char Prefixes[] =
"yzafpnum.kMGTPEZY";
2395 const char * pSi = strchr (Prefixes, *tailptr);
2396 if (pSi && *pSi !=
'.') Pow = (double) ((pSi - Prefixes) * 3 - 24);
2397 else if (*tailptr ==
'c') Pow = -2;
2398 else if (*tailptr ==
'h') Pow = 2;
2399 else if (*tailptr ==
'k') Pow = 3;
2401 if (*(++pfx->pex) ==
'i') {
2402 val *= pow (2.0, Pow/0.3);
2405 val *= pow (10.0, Pow);
2409 (void) AddElement (pfx, val, oNull);
2413 val = (fxFltType) 0;
2414 lenOptArt = GetProperty (pfx, &val, NULL);
2415 if (lenOptArt < 0)
return MagickFalse;
2416 if (lenOptArt > 0) {
2417 (void) AddElement (pfx, val, oNull);
2418 pfx->pex += lenOptArt;
2425 if (pfx->lenToken > 0) {
2430 for (ce = (ConstantE)0; ce < cNull; ce=(ConstantE) (ce+1)) {
2431 const char * ceStr = Constants[ce].str;
2432 if (LocaleCompare (ceStr, pfx->token)==0) {
2438 (void) AddElement (pfx, Constants[ce].val, oNull);
2439 pfx->pex += pfx->lenToken;
2448 for (fe = FirstFunc; fe < fNull; fe=(FunctionE) (fe+1)) {
2449 const char * feStr = Functions[fe-(int) FirstFunc].str;
2450 if (LocaleCompare (feStr, pfx->token)==0) {
2455 if (fe == fV && pfx->ImgListLen < 2) {
2456 (void) ThrowMagickException (
2457 pfx->exception, GetMagickModule(), OptionError,
2458 "Symbol 'v' but fewer than two images at",
"'%s'",
2463 if (IsStealth ((
int) fe)) {
2464 (void) ThrowMagickException (
2465 pfx->exception, GetMagickModule(), OptionError,
2466 "Function",
"'%s' not permitted at '%s'",
2467 pfx->token, SetShortExp(pfx));
2471 if (fe == fDo || fe == fFor || fe == fIf || fe == fWhile) {
2472 *needPopAll = MagickTrue;
2475 if (fe != fNull)
return (GetFunction (pfx, fe));
2481 ImgAttrE ia = GetImgAttrToken (pfx);
2484 (void) AddElement (pfx, val, (
int) ia);
2486 if (ImgAttrs[ia-(
int) FirstImgAttr].need_stats != MagickFalse) {
2487 if (IsQualifier (pfx)) {
2488 PixelChannel chQual = GetChannelQualifier (pfx, (
int) ia);
2490 if (chQual == NO_CHAN_QUAL) {
2491 (void) ThrowMagickException (
2492 pfx->exception, GetMagickModule(), OptionError,
2493 "Bad channel qualifier at",
"'%s'",
2498 pel = &pfx->Elements[pfx->usedElements-1];
2499 pel->channel_qual = chQual;
2510 for (se = FirstSym; se < sNull; se=(SymbolE) (se+1)) {
2511 const char * seStr = Symbols[se-(int) FirstSym].str;
2512 if (LocaleCompare (seStr, pfx->token)==0) {
2518 (void) AddElement (pfx, val, (
int) se);
2519 pfx->pex += pfx->lenToken;
2521 if (se==sHue || se==sSaturation || se==sLightness) pfx->NeedHsl = MagickTrue;
2529 fxFltType v0, v1, v2;
2530 ssize_t ColLen = GetConstantColour (pfx, &v0, &v1, &v2);
2531 if (ColLen < 0)
return MagickFalse;
2533 (void) AddColourElement (pfx, v0, v1, v2);
2542 const char *artifact;
2543 artifact = GetImageArtifact (pfx->image, pfx->token);
2544 if (artifact != (
const char *) NULL) {
2546 fxFltType val = strtold (artifact, &tailptr);
2547 if (artifact == tailptr) {
2548 (void) ThrowMagickException (
2549 pfx->exception, GetMagickModule(), OptionError,
2550 "Artifact",
"'%s' has value '%s', not a number, at '%s'",
2551 pfx->token, artifact, SetShortExp(pfx));
2554 (void) AddElement (pfx, val, oNull);
2555 pfx->pex+=pfx->lenToken;
2562 if (TokenMaybeUserSymbol (pfx)) {
2563 *UserSymbol = MagickTrue;
2564 *UserSymNdx = FindUserSymbol (pfx, pfx->token);
2565 if (*UserSymNdx == NULL_ADDRESS) {
2566 *UserSymNdx = AddUserSymbol (pfx, pfx->pex, pfx->lenToken);
2567 if (*UserSymNdx < 0)
2569 *NewUserSymbol = MagickTrue;
2571 pfx->pex += pfx->lenToken;
2577 (void) ThrowMagickException (
2578 pfx->exception, GetMagickModule(), OptionError,
2579 "Expected operand at",
"'%s'",
2585static inline MagickBooleanType IsRealOperator (OperatorE op)
2587 return (op < oOpenParen || op > oCloseBrace) ? MagickTrue : MagickFalse;
2590static inline MagickBooleanType ProcessTernaryOpr (FxInfo * pfx,
TernaryT * ptern)
2595 if (pfx->usedOprStack == 0)
2597 if (pfx->OperatorStack[pfx->usedOprStack-1] == oQuery) {
2598 if (ptern->addr_query != NULL_ADDRESS) {
2599 (void) ThrowMagickException (
2600 pfx->exception, GetMagickModule(), OptionError,
2601 "Already have '?' in sub-expression at",
"'%s'",
2605 if (ptern->addr_colon != NULL_ADDRESS) {
2606 (void) ThrowMagickException (
2607 pfx->exception, GetMagickModule(), OptionError,
2608 "Already have ':' in sub-expression at",
"'%s'",
2612 pfx->usedOprStack--;
2613 ptern->addr_query = pfx->usedElements;
2614 (void) AddAddressingElement (pfx, rIfZeroGoto, NULL_ADDRESS);
2617 else if (pfx->OperatorStack[pfx->usedOprStack-1] == oColon) {
2618 if (ptern->addr_query == NULL_ADDRESS) {
2619 (void) ThrowMagickException (
2620 pfx->exception, GetMagickModule(), OptionError,
2621 "Need '?' in sub-expression at",
"'%s'",
2625 if (ptern->addr_colon != NULL_ADDRESS) {
2626 (void) ThrowMagickException (
2627 pfx->exception, GetMagickModule(), OptionError,
2628 "Already have ':' in sub-expression at",
"'%s'",
2632 pfx->usedOprStack--;
2633 ptern->addr_colon = pfx->usedElements;
2634 pfx->Elements[pfx->usedElements-1].do_push = MagickTrue;
2635 (void) AddAddressingElement (pfx, rGoto, NULL_ADDRESS);
2641static MagickBooleanType GetOperator (
2643 MagickBooleanType * Assign, MagickBooleanType * Update, MagickBooleanType * IncrDecr)
2647 MagickBooleanType DoneIt = MagickFalse;
2649 for (op = (OperatorE)0; op != oNull; op=(OperatorE) (op+1)) {
2650 const char * opStr = Operators[op].str;
2651 len = strlen(opStr);
2652 if (LocaleNCompare (opStr, pfx->pex, len)==0) {
2657 if (!IsRealOperator (op)) {
2658 (void) ThrowMagickException (
2659 pfx->exception, GetMagickModule(), OptionError,
2660 "Not a real operator at",
"'%s'",
2666 (void) ThrowMagickException (
2667 pfx->exception, GetMagickModule(), OptionError,
2668 "Expected operator at",
"'%s'",
2673 *Assign = (op==oAssign) ? MagickTrue : MagickFalse;
2674 *Update = OprInPlace ((
int) op);
2675 *IncrDecr = (op == oPlusPlus || op == oSubSub) ? MagickTrue : MagickFalse;
2682 while (pfx->usedOprStack > 0) {
2683 OperatorE top = pfx->OperatorStack[pfx->usedOprStack-1];
2684 int precTop, precNew;
2685 if (top == oOpenParen || top == oAssign || OprInPlace ((
int) top))
break;
2686 precTop = Operators[top].precedence;
2687 precNew = Operators[op].precedence;
2691 if (precTop < precNew)
break;
2692 (void) AddElement (pfx, (fxFltType) 0, (
int) top);
2693 pfx->usedOprStack--;
2699 if (op==oCloseParen) {
2700 if (pfx->usedOprStack == 0) {
2701 (void) ThrowMagickException (
2702 pfx->exception, GetMagickModule(), OptionError,
2703 "Found ')' but nothing on stack at",
"'%s'",
2708 if (pfx->OperatorStack[pfx->usedOprStack-1] != oOpenParen) {
2709 (void) ThrowMagickException (
2710 pfx->exception, GetMagickModule(), OptionError,
2711 "Found ')' but no '(' on stack at",
"'%s'",
2715 pfx->usedOprStack--;
2716 DoneIt = MagickTrue;
2720 if (!PushOperatorStack (pfx, (
int) op))
return MagickFalse;
2728static MagickBooleanType ResolveTernaryAddresses (FxInfo * pfx,
TernaryT * ptern)
2730 if (ptern->addr_query == NULL_ADDRESS && ptern->addr_colon == NULL_ADDRESS)
2733 if (ptern->addr_query != NULL_ADDRESS && ptern->addr_colon != NULL_ADDRESS) {
2734 pfx->Elements[ptern->addr_query].element_index = ptern->addr_colon + 1;
2735 pfx->Elements[ptern->addr_colon].element_index = pfx->usedElements;
2736 ptern->addr_query = NULL_ADDRESS;
2737 ptern->addr_colon = NULL_ADDRESS;
2738 }
else if (ptern->addr_query != NULL_ADDRESS) {
2739 (void) ThrowMagickException (
2740 pfx->exception, GetMagickModule(), OptionError,
2741 "'?' with no corresponding ':'",
"'%s' at '%s'",
2742 pfx->token, SetShortExp(pfx));
2744 }
else if (ptern->addr_colon != NULL_ADDRESS) {
2745 (void) ThrowMagickException (
2746 pfx->exception, GetMagickModule(), OptionError,
2747 "':' with no corresponding '?'",
"'%s' at '%s'",
2748 pfx->token, SetShortExp(pfx));
2754static MagickBooleanType TranslateExpression (
2755 FxInfo * pfx,
const char * strLimit,
char * chLimit, MagickBooleanType * needPopAll)
2759 MagickBooleanType UserSymbol, NewUserSymbol;
2760 int UserSymNdx0, UserSymNdx1;
2763 Assign = MagickFalse,
2764 Update = MagickFalse,
2765 IncrDecr = MagickFalse;
2770 ternary.addr_query = NULL_ADDRESS;
2771 ternary.addr_colon = NULL_ADDRESS;
2773 if (pfx->teDepth >= MagickMaxRecursionDepth) {
2774 (void) ThrowMagickException(pfx->exception, GetMagickModule(), OptionError,
2775 "Expression too deeply nested",
"(depth %i exceeds limit %i)",
2776 pfx->teDepth, MagickMaxRecursionDepth);
2784 StartEleNdx = pfx->usedElements-1;
2785 if (StartEleNdx < 0) StartEleNdx = 0;
2794 if (strchr(strLimit,*pfx->pex)!=NULL) {
2795 *chLimit = *pfx->pex;
2802 if (!GetOperand (pfx, &UserSymbol, &NewUserSymbol, &UserSymNdx0, needPopAll))
return MagickFalse;
2807 while (*pfx->pex && (!*strLimit || (strchr(strLimit,*pfx->pex)==NULL))) {
2808 if (!GetOperator (pfx, &Assign, &Update, &IncrDecr))
return MagickFalse;
2810 if (NewUserSymbol && !Assign) {
2811 (void) ThrowMagickException (
2812 pfx->exception, GetMagickModule(), OptionError,
2813 "Expected assignment after new UserSymbol",
"'%s' at '%s'",
2814 pfx->token, SetShortExp(pfx));
2817 if (!UserSymbol && Assign) {
2818 (void) ThrowMagickException (
2819 pfx->exception, GetMagickModule(), OptionError,
2820 "Attempted assignment to non-UserSymbol",
"'%s' at '%s'",
2821 pfx->token, SetShortExp(pfx));
2824 if (!UserSymbol && Update) {
2825 (void) ThrowMagickException (
2826 pfx->exception, GetMagickModule(), OptionError,
2827 "Attempted update to non-UserSymbol",
"'%s' at '%s'",
2828 pfx->token, SetShortExp(pfx));
2831 if (UserSymbol && (Assign || Update) && !IncrDecr) {
2833 if (!TranslateExpression (pfx, strLimit, chLimit, needPopAll))
return MagickFalse;
2834 if (!*pfx->pex)
break;
2835 if (!*strLimit)
break;
2836 if (strchr(strLimit,*chLimit)!=NULL)
break;
2838 if (UserSymbol && !Assign && !Update && UserSymNdx0 != NULL_ADDRESS) {
2840 (void) AddAddressingElement (pfx, rCopyFrom, UserSymNdx0);
2841 UserSymNdx0 = NULL_ADDRESS;
2842 pel = &pfx->Elements[pfx->usedElements-1];
2843 pel->do_push = MagickTrue;
2847 while (TopOprIsUnaryPrefix (pfx)) {
2848 OperatorE op = pfx->OperatorStack[pfx->usedOprStack-1];
2849 (void) AddElement (pfx, (fxFltType) 0, (
int) op);
2850 pfx->usedOprStack--;
2854 if (!ProcessTernaryOpr (pfx, &ternary))
return MagickFalse;
2856 if (ternary.addr_colon != NULL_ADDRESS) {
2857 if (!TranslateExpression (pfx,
",);", chLimit, needPopAll))
return MagickFalse;
2861 UserSymbol = NewUserSymbol = MagickFalse;
2863 if ( (!*pfx->pex) || (*strLimit && (strchr(strLimit,*pfx->pex)!=NULL) ) )
2865 if (IncrDecr)
break;
2867 (void) ThrowMagickException (
2868 pfx->exception, GetMagickModule(), OptionError,
2869 "Expected operand after operator",
"at '%s'",
2875 (void) ThrowMagickException (
2876 pfx->exception, GetMagickModule(), OptionError,
2877 "'++' and '--' must be the final operators in an expression at",
"'%s'",
2882 if (!GetOperand (pfx, &UserSymbol, &NewUserSymbol, &UserSymNdx1, needPopAll)) {
2883 (void) ThrowMagickException (
2884 pfx->exception, GetMagickModule(), OptionError,
2885 "Expected operand at",
"'%s'",
2890 if (NewUserSymbol && !Assign) {
2891 (void) ThrowMagickException (
2892 pfx->exception, GetMagickModule(), OptionError,
2893 "NewUserSymbol",
"'%s' after non-assignment operator at '%s'",
2894 pfx->token, SetShortExp(pfx));
2897 if (UserSymbol && !NewUserSymbol) {
2898 (void) AddAddressingElement (pfx, rCopyFrom, UserSymNdx1);
2899 UserSymNdx1 = NULL_ADDRESS;
2901 UserSymNdx0 = UserSymNdx1;
2904 if (UserSymbol && !Assign && !Update && UserSymNdx0 != NULL_ADDRESS) {
2906 if (NewUserSymbol) {
2907 (void) ThrowMagickException (
2908 pfx->exception, GetMagickModule(), OptionError,
2909 "NewUserSymbol",
"'%s' needs assignment operator at '%s'",
2910 pfx->token, SetShortExp(pfx));
2913 (void) AddAddressingElement (pfx, rCopyFrom, UserSymNdx0);
2914 pel = &pfx->Elements[pfx->usedElements-1];
2915 pel->do_push = MagickTrue;
2918 if (*pfx->pex && !*chLimit && (strchr(strLimit,*pfx->pex)!=NULL)) {
2919 *chLimit = *pfx->pex;
2922 while (pfx->usedOprStack) {
2923 OperatorE op = pfx->OperatorStack[pfx->usedOprStack-1];
2924 if (op == oOpenParen || op == oOpenBracket || op == oOpenBrace) {
2927 if ( (op==oAssign && !Assign) || (OprInPlace((
int) op) && !Update) ) {
2930 pfx->usedOprStack--;
2931 (void) AddElement (pfx, (fxFltType) 0, (
int) op);
2932 if (op == oAssign) {
2933 if (UserSymNdx0 < 0) {
2934 (void) ThrowMagickException (
2935 pfx->exception, GetMagickModule(), OptionError,
2936 "Assignment to unknown user symbol at",
"'%s'",
2942 pfx->usedElements--;
2943 (void) AddAddressingElement (pfx, rCopyTo, UserSymNdx0);
2945 }
else if (OprInPlace ((
int) op)) {
2946 if (UserSymNdx0 < 0) {
2947 (void) ThrowMagickException (
2948 pfx->exception, GetMagickModule(), OptionError,
2949 "Operator-in-place to unknown user symbol at",
"'%s'",
2955 pfx->Elements[pfx->usedElements-1].element_index = UserSymNdx0;
2960 if (ternary.addr_query != NULL_ADDRESS) *needPopAll = MagickTrue;
2962 (void) ResolveTernaryAddresses (pfx, &ternary);
2966 if (!pfx->teDepth && *needPopAll) {
2967 (void) AddAddressingElement (pfx, rZerStk, NULL_ADDRESS);
2968 *needPopAll = MagickFalse;
2971 if (pfx->exception->severity >= ErrorException)
2978static MagickBooleanType TranslateStatement (FxInfo * pfx,
char * strLimit,
char * chLimit)
2980 MagickBooleanType NeedPopAll = MagickFalse;
2984 if (!*pfx->pex)
return MagickFalse;
2986 if (!TranslateExpression (pfx, strLimit, chLimit, &NeedPopAll)) {
2989 if (pfx->usedElements && *chLimit==
';') {
2994 ElementT * pel = &pfx->Elements[pfx->usedElements-1];
2995 if (pel->do_push) pel->do_push = MagickFalse;
3001static MagickBooleanType TranslateStatementList (FxInfo * pfx,
const char * strLimit,
char * chLimit)
3003#define MAX_SLIMIT 10
3004 char sLimits[MAX_SLIMIT];
3007 if (!*pfx->pex)
return MagickFalse;
3008 (void) CopyMagickString (sLimits, strLimit, MAX_SLIMIT-1);
3010 if (strchr(strLimit,
';')==NULL)
3011 (void) ConcatenateMagickString (sLimits,
";", MAX_SLIMIT);
3014 if (!TranslateStatement (pfx, sLimits, chLimit))
return MagickFalse;
3016 if (!*pfx->pex)
break;
3018 if (*chLimit !=
';') {
3023 if (pfx->exception->severity >= ErrorException)
3033static ChannelStatistics *CollectOneImgStats (FxInfo * pfx, Image * img)
3036 ChannelStatistics * cs = GetImageStatistics (img, pfx->exception);
3039 if (cs == (ChannelStatistics *) NULL)
3040 return((ChannelStatistics *) NULL);
3042 for (ch=0; ch <= (int) MaxPixelChannels; ch++) {
3043 cs[ch].mean *= QuantumScale;
3044 cs[ch].median *= QuantumScale;
3045 cs[ch].maxima *= QuantumScale;
3046 cs[ch].minima *= QuantumScale;
3047 cs[ch].standard_deviation *= QuantumScale;
3053static MagickBooleanType CollectStatistics (FxInfo * pfx)
3055 Image * img = GetFirstImageInList (pfx->image);
3059 pfx->statistics = (ChannelStatistics**) AcquireMagickMemory ((
size_t) pfx->ImgListLen * sizeof (ChannelStatistics *));
3060 if (!pfx->statistics) {
3061 (void) ThrowMagickException (
3062 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
3063 "Statistics",
"%lu",
3064 (
unsigned long) pfx->ImgListLen);
3069 pfx->statistics[imgNum] = CollectOneImgStats (pfx, img);
3070 if (pfx->statistics[imgNum] == (ChannelStatistics *) NULL) {
3071 while (imgNum > 0) {
3073 pfx->statistics[imgNum] = (ChannelStatistics *) RelinquishMagickMemory (pfx->statistics[imgNum]);
3075 pfx->statistics = (ChannelStatistics **) RelinquishMagickMemory (pfx->statistics);
3079 if (++imgNum == pfx->ImgListLen)
break;
3080 img = GetNextImageInList (img);
3081 assert (img != (Image *) NULL);
3083 pfx->GotStats = MagickTrue;
3088static inline MagickBooleanType PushVal (FxInfo * pfx,
fxRtT * pfxrt, fxFltType val,
int addr)
3090 if (pfxrt->usedValStack >=pfxrt->numValStack) {
3091 (void) ThrowMagickException (
3092 pfx->exception, GetMagickModule(), OptionError,
3093 "ValStack overflow at addr=",
"%i",
3098 pfxrt->ValStack[pfxrt->usedValStack++] = val;
3102static inline fxFltType PopVal (FxInfo * pfx,
fxRtT * pfxrt,
int addr)
3104 if (pfxrt->usedValStack <= 0) {
3105 (void) ThrowMagickException (
3106 pfx->exception, GetMagickModule(), OptionError,
3107 "ValStack underflow at addr=",
"%i",
3109 return (fxFltType) 0;
3112 return pfxrt->ValStack[--pfxrt->usedValStack];
3115static inline fxFltType ImageStat (
3116 FxInfo * pfx, ssize_t ImgNum, PixelChannel channel, ImgAttrE ia)
3118 ChannelStatistics * cs = NULL;
3120 MagickBooleanType NeedRelinq = MagickFalse;
3124 (void) ThrowMagickException(pfx->exception,GetMagickModule(),
3125 OptionError,
"NoSuchImage",
"%lu",(
unsigned long) ImgNum);
3129 if (pfx->GotStats) {
3130 if ((channel < 0) || (channel > MaxPixelChannels))
3132 (void) ThrowMagickException(pfx->exception,GetMagickModule(),
3133 OptionError,
"NoSuchImageChannel",
"%i",channel);
3134 channel=(PixelChannel) 0;
3136 cs = pfx->statistics[ImgNum];
3137 }
else if (pfx->NeedStats) {
3139 if ((channel < 0) || (channel > MaxPixelChannels))
3141 (void) ThrowMagickException(pfx->exception,GetMagickModule(),
3142 OptionError,
"NoSuchImageChannel",
"%i",channel);
3143 channel=(PixelChannel) 0;
3145 cs = CollectOneImgStats (pfx, pfx->Images[ImgNum]);
3146 NeedRelinq = MagickTrue;
3151 ret = (fxFltType) GetImageDepth (pfx->Images[ImgNum], pfx->exception);
3154 ret = (fxFltType) GetBlobSize (pfx->Images[ImgNum]);
3157 if ((cs != (ChannelStatistics *) NULL) && (channel >= 0))
3158 ret = cs[channel].kurtosis;
3161 if ((cs != (ChannelStatistics *) NULL) && (channel >= 0))
3162 ret = cs[channel].maxima;
3165 if ((cs != (ChannelStatistics *) NULL) && (channel >= 0))
3166 ret = cs[channel].mean;
3169 if ((cs != (ChannelStatistics *) NULL) && (channel >= 0))
3170 ret = cs[channel].median;
3173 if ((cs != (ChannelStatistics *) NULL) && (channel >= 0))
3174 ret = cs[channel].minima;
3180 ret = (fxFltType) pfx->Images[ImgNum]->page.x;
3183 ret = (fxFltType) pfx->Images[ImgNum]->page.y;
3186 ret = (fxFltType) pfx->Images[ImgNum]->page.width;
3189 ret = (fxFltType) pfx->Images[ImgNum]->page.height;
3195 ret = (fxFltType) MagickSafeReciprocal (pfx->Images[ImgNum]->resolution.x)
3196 * pfx->Images[ImgNum]->columns;
3199 ret = (fxFltType) MagickSafeReciprocal (pfx->Images[ImgNum]->resolution.y)
3200 * pfx->Images[ImgNum]->rows;
3203 ret = (fxFltType) pfx->Images[ImgNum]->quality;
3209 ret = pfx->Images[ImgNum]->resolution.x;
3212 ret = pfx->Images[ImgNum]->resolution.y;
3215 if ((cs != (ChannelStatistics *) NULL) && (channel >= 0))
3216 ret = cs[channel].skewness;
3219 if ((cs != (ChannelStatistics *) NULL) && (channel >= 0))
3220 ret = cs[channel].standard_deviation;
3223 ret = (fxFltType) pfx->Images[ImgNum]->rows;
3226 ret = (fxFltType) pfx->ImgListLen;
3229 ret = (fxFltType) ImgNum;
3232 ret = (fxFltType) pfx->Images[ImgNum]->columns;
3235 ret = (fxFltType) GetImageDepth (pfx->Images[ImgNum], pfx->exception);
3238 (void) ThrowMagickException (pfx->exception,GetMagickModule(),OptionError,
3239 "Unknown ia=",
"%i",ia);
3241 if (NeedRelinq) cs = (ChannelStatistics *)RelinquishMagickMemory (cs);
3246static inline fxFltType FxGcd (fxFltType x, fxFltType y,
const size_t depth)
3248#define FxMaxFunctionDepth 200
3251 return (FxGcd (y, x, depth+1));
3252 if ((fabs((
double) y) < 0.001) || (depth >= FxMaxFunctionDepth))
3254 return (FxGcd (y, x-y*floor((
double) (x/y)), depth+1));
3257static inline ssize_t ChkImgNum (FxInfo * pfx, fxFltType f)
3260 ssize_t i = (ssize_t) floor ((
double) f + 0.5);
3261 if (i < 0) i += (ssize_t) pfx->ImgListLen;
3262 if (i < 0 || i >= (ssize_t) pfx->ImgListLen) {
3263 (void) ThrowMagickException (
3264 pfx->exception, GetMagickModule(), OptionError,
3265 "ImgNum",
"%lu bad for ImgListLen %lu",
3266 (
unsigned long) i, (
unsigned long) pfx->ImgListLen);
3272#define WHICH_ATTR_CHAN \
3273 (pel->channel_qual == NO_CHAN_QUAL) ? CompositePixelChannel : \
3274 (pel->channel_qual == THIS_CHANNEL) ? channel : pel->channel_qual
3276#define WHICH_NON_ATTR_CHAN \
3277 (pel->channel_qual == NO_CHAN_QUAL || \
3278 pel->channel_qual == THIS_CHANNEL || \
3279 pel->channel_qual == CompositePixelChannel \
3280 ) ? (channel == CompositePixelChannel ? RedPixelChannel: channel) \
3283static fxFltType GetHslFlt (FxInfo * pfx, ssize_t ImgNum,
const fxFltType fx,
const fxFltType fy,
3284 PixelChannel channel)
3286 Image * img = pfx->Images[ImgNum];
3288 double red, green, blue;
3289 double hue=0, saturation=0, lightness=0;
3291 MagickBooleanType okay = MagickTrue;
3292 if(!InterpolatePixelChannel (img, pfx->Imgs[ImgNum].View, RedPixelChannel, img->interpolate,
3293 (
double) fx, (
double) fy, &red, pfx->exception)) okay = MagickFalse;
3294 if(!InterpolatePixelChannel (img, pfx->Imgs[ImgNum].View, GreenPixelChannel, img->interpolate,
3295 (
double) fx, (
double) fy, &green, pfx->exception)) okay = MagickFalse;
3296 if(!InterpolatePixelChannel (img, pfx->Imgs[ImgNum].View, BluePixelChannel, img->interpolate,
3297 (
double) fx, (
double) fy, &blue, pfx->exception)) okay = MagickFalse;
3300 (void) ThrowMagickException (
3301 pfx->exception, GetMagickModule(), OptionError,
3302 "GetHslFlt failure",
"%lu %g,%g %i", (
unsigned long) ImgNum,
3303 (
double) fx, (
double) fy, channel);
3307 &hue, &saturation, &lightness);
3309 if (channel == HUE_CHANNEL)
return hue;
3310 if (channel == SAT_CHANNEL)
return saturation;
3311 if (channel == LIGHT_CHANNEL)
return lightness;
3316static fxFltType GetHslInt (FxInfo * pfx, ssize_t ImgNum,
const ssize_t imgx,
const ssize_t imgy, PixelChannel channel)
3318 Image * img = pfx->Images[ImgNum];
3320 double hue=0, saturation=0, lightness=0;
3322 const Quantum * p = GetCacheViewVirtualPixels (pfx->Imgs[ImgNum].View, imgx, imgy, 1, 1, pfx->exception);
3323 if (p == (
const Quantum *) NULL)
3325 (void) ThrowMagickException (pfx->exception,GetMagickModule(),
3326 OptionError,
"GetHslInt failure",
"%lu %li,%li %i",(
unsigned long) ImgNum,
3327 (
long) imgx,(
long) imgy,channel);
3332 GetPixelRed (img, p), GetPixelGreen (img, p), GetPixelBlue (img, p),
3333 &hue, &saturation, &lightness);
3335 if (channel == HUE_CHANNEL)
return hue;
3336 if (channel == SAT_CHANNEL)
return saturation;
3337 if (channel == LIGHT_CHANNEL)
return lightness;
3342static inline fxFltType GetIntensity (FxInfo * pfx, ssize_t ImgNum,
const fxFltType fx,
const fxFltType fy)
3345 quantum_pixel[MaxPixelChannels];
3350 Image * img = pfx->Images[ImgNum];
3352 (void) GetPixelInfo (img, &pixelinf);
3354 if (!InterpolatePixelInfo (img, pfx->Imgs[ImgNum].View, img->interpolate,
3355 (
double) fx, (
double) fy, &pixelinf, pfx->exception))
3357 (void) ThrowMagickException (
3358 pfx->exception, GetMagickModule(), OptionError,
3359 "GetIntensity failure",
"%lu %g,%g", (
unsigned long) ImgNum,
3360 (
double) fx, (
double) fy);
3363 SetPixelViaPixelInfo (img, &pixelinf, quantum_pixel);
3364 return QuantumScale * GetPixelIntensity (img, quantum_pixel);
3367static MagickBooleanType ExecuteRPN (FxInfo * pfx,
fxRtT * pfxrt, fxFltType *result,
3368 const PixelChannel channel,
const ssize_t imgx,
const ssize_t imgy)
3370 const Quantum * p = pfxrt->thisPixel;
3371 fxFltType regA=0, regB=0, regC=0, regD=0, regE=0;
3372 Image * img = pfx->image;
3373 ChannelStatistics * cs = NULL;
3374 MagickBooleanType NeedRelinq = MagickFalse;
3375 double hue=0, saturation=0, lightness=0;
3382 if (!p) p = GetCacheViewVirtualPixels (
3383 pfx->Imgs[pfx->ImgNum].View, imgx, imgy, 1, 1, pfx->exception);
3385 if (p == (
const Quantum *) NULL)
3387 (void) ThrowMagickException (pfx->exception,GetMagickModule(),
3388 OptionError,
"Can't get virtual pixels",
"%lu %li,%li",(
unsigned long)
3389 pfx->ImgNum,(
long) imgx,(
long) imgy);
3390 return(MagickFalse);
3393 if (pfx->GotStats) {
3394 cs = pfx->statistics[pfx->ImgNum];
3395 }
else if (pfx->NeedStats) {
3396 cs = CollectOneImgStats (pfx, pfx->Images[pfx->ImgNum]);
3397 NeedRelinq = MagickTrue;
3404 GetPixelRed (img, p), GetPixelGreen (img, p), GetPixelBlue (img, p),
3405 &hue, &saturation, &lightness);
3408 for (i=0; i < pfx->usedElements; i++) {
3413 (void) ThrowMagickException (
3414 pfx->exception, GetMagickModule(), OptionError,
3415 "Bad run-time address",
"%i", i);
3418 pel=&pfx->Elements[i];
3419 switch (pel->number_args) {
3423 regA = PopVal (pfx, pfxrt, i);
3426 regB = PopVal (pfx, pfxrt, i);
3427 regA = PopVal (pfx, pfxrt, i);
3430 regC = PopVal (pfx, pfxrt, i);
3431 regB = PopVal (pfx, pfxrt, i);
3432 regA = PopVal (pfx, pfxrt, i);
3435 regD = PopVal (pfx, pfxrt, i);
3436 regC = PopVal (pfx, pfxrt, i);
3437 regB = PopVal (pfx, pfxrt, i);
3438 regA = PopVal (pfx, pfxrt, i);
3441 regE = PopVal (pfx, pfxrt, i);
3442 regD = PopVal (pfx, pfxrt, i);
3443 regC = PopVal (pfx, pfxrt, i);
3444 regB = PopVal (pfx, pfxrt, i);
3445 regA = PopVal (pfx, pfxrt, i);
3448 (void) ThrowMagickException (
3449 pfx->exception, GetMagickModule(), OptionError,
3450 "Too many args:",
"%i", pel->number_args);
3454 switch (pel->operator_index) {
3456 regA = (pfxrt->UserSymVals[pel->element_index] += regA);
3459 regA = (pfxrt->UserSymVals[pel->element_index] -= regA);
3462 regA = (pfxrt->UserSymVals[pel->element_index] *= regA);
3465 if (fabs((
double) regA) < MagickEpsilon) {
3466 (void) ThrowMagickException (pfx->exception, GetMagickModule(),
3467 OptionError,
"DivideByZero",
"`%s'", SetShortExp(pfx));
3468 regA = (fxFltType) 0.0;
3470 regA = (pfxrt->UserSymVals[pel->element_index] /= regA);
3474 regA = pfxrt->UserSymVals[pel->element_index]++;
3477 regA = pfxrt->UserSymVals[pel->element_index]--;
3489 if (fabs((
double) regB) < MagickEpsilon) {
3490 (void) ThrowMagickException (pfx->exception, GetMagickModule(),
3491 OptionError,
"DivideByZero",
"`%s'", SetShortExp(pfx));
3492 regA = (fxFltType) 0.0;
3499 double mod_val = fabs(floor((
double) regB+0.5));
3500 if (mod_val < MagickEpsilon) {
3501 (void) ThrowMagickException (pfx->exception, GetMagickModule(),
3502 OptionError,
"DivideByZero",
"`%s'", SetShortExp(pfx));
3503 regA = (fxFltType) 0.0;
3505 regA = fmod ((
double) regA, mod_val);
3516 if ((regB < -0.5) || (CastDoubleToSizeT((
double) regB+0.5) >= (8*
sizeof(
size_t))))
3518 (void) ThrowMagickException ( pfx->exception, GetMagickModule(),
3519 OptionError,
"undefined shift",
"%g", (
double) regB);
3520 regA = (fxFltType) 0.0;
3523 regA = (fxFltType) (CastDoubleToSizeT((
double) regA+0.5) << CastDoubleToSizeT((
double) regB+0.5));
3526 if ((regB < -0.5) || (CastDoubleToSizeT((
double) regB+0.5) >= (8*
sizeof(
size_t))))
3528 (void) ThrowMagickException ( pfx->exception, GetMagickModule(),
3529 OptionError,
"undefined shift",
"%g", (
double) regB);
3530 regA = (fxFltType) 0.0;
3533 regA = (fxFltType) (CastDoubleToSizeT((
double) regA+0.5) >> CastDoubleToSizeT((
double) regB+0.5));
3536 regA = fabs((
double) (regA-regB)) < MagickEpsilon ? 1.0 : 0.0;
3539 regA = fabs((
double) (regA-regB)) >= MagickEpsilon ? 1.0 : 0.0;
3542 regA = (regA <= regB) ? 1.0 : 0.0;
3545 regA = (regA >= regB) ? 1.0 : 0.0;
3548 regA = (regA < regB) ? 1.0 : 0.0;
3551 regA = (regA > regB) ? 1.0 : 0.0;
3554 regA = (regA<=0) ? 0.0 : (regB > 0) ? 1.0 : 0.0;
3557 regA = (regA>0) ? 1.0 : (regB > 0.0) ? 1.0 : 0.0;
3560 regA = (regA==0) ? 1.0 : 0.0;
3563 regA = (fxFltType) (CastDoubleToSizeT((
double) regA+0.5) & CastDoubleToSizeT((
double) regB+0.5));
3566 regA = (fxFltType) (CastDoubleToSizeT((
double) regA+0.5) | CastDoubleToSizeT((
double) regB+0.5));
3574 new_value=~CastDoubleToSizeT((
double) regA+0.5);
3575 regA=(fxFltType) new_value;
3579 regA = pow ((
double) regA, (
double) regB);
3595 if (pel->type == etColourConstant) {
3596 switch (channel) {
default:
3597 case (PixelChannel) 0:
3600 case (PixelChannel) 1:
3603 case (PixelChannel) 2:
3613 regA = fabs ((
double) regA);
3615#if defined(MAGICKCORE_HAVE_ACOSH)
3617 regA = acosh ((
double) regA);
3621 regA = acos ((
double) regA);
3623#if defined(MAGICKCORE_HAVE_J1)
3625 if (regA==0) regA = 1.0;
3627 fxFltType gamma = 2.0 * __j1((
double) (MagickPI*regA)) / (MagickPI*regA);
3628 regA = gamma * gamma;
3633 regA = (fxFltType) (((ssize_t) regA) & 0x01 ? -1.0 : 1.0);
3635#if defined(MAGICKCORE_HAVE_ASINH)
3637 regA = asinh ((
double) regA);
3641 regA = asin ((
double) regA);
3643#if defined(MAGICKCORE_HAVE_ATANH)
3645 regA = atanh ((
double) regA);
3649 regA = atan2 ((
double) regA, (
double) regB);
3652 regA = atan ((
double) regA);
3655 regA = ceil ((
double) regA);
3659 case (PixelChannel) 0:
break;
3660 case (PixelChannel) 1: regA = regB;
break;
3661 case (PixelChannel) 2: regA = regC;
break;
3662 case (PixelChannel) 3: regA = regD;
break;
3663 case (PixelChannel) 4: regA = regE;
break;
3664 default: regA = 0.0;
3668 if (regA < 0) regA = 0.0;
3669 else if (regA > 1.0) regA = 1.0;
3672 regA = cosh ((
double) regA);
3675 regA = cos ((
double) regA);
3680 (void) fprintf (stderr,
"%s[%g,%g].[%i]: %s=%.*g\n",
3681 img->filename, (
double) imgx, (
double) imgy,
3682 channel, SetPtrShortExp (pfx, pel->exp_start, (
size_t) (pel->exp_len+1)),
3683 pfx->precision, (
double) regA);
3686 regA = regA / (regB*(regA-1.0) + 1.0);
3688#if defined(MAGICKCORE_HAVE_ERF)
3690 regA = erf ((
double) regA);
3697 regA = exp ((
double) regA);
3700 regA = floor ((
double) regA);
3703 regA = exp((
double) (-regA*regA/2.0))/sqrt(2.0*MagickPI);
3706 if (!IsNaN((
double) regA))
3707 regA = FxGcd (regA, regB, 0);
3710 regA = hypot ((
double) regA, (
double) regB);
3713 regA = floor ((
double) regA);
3716 regA = (fxFltType) (!!IsNaN ((
double) regA));
3718#if defined(MAGICKCORE_HAVE_J0)
3720 regA = __j0((
double) regA);
3723#if defined(MAGICKCORE_HAVE_J1)
3725 regA = __j1((
double) regA);
3728#if defined(MAGICKCORE_HAVE_J1)
3730 if (regA==0) regA = 1.0;
3731 else regA = 2.0 * __j1((
double) (MagickPI*regA))/(MagickPI*regA);
3735 regA = log ((
double) regA);
3738 regA = log2 ((
double) regA);
3741 regA = log10 ((
double) regA);
3744 regA = (fxFltType) GetMagickTime();
3747 regA = (regA > regB) ? regA : regB;
3750 regA = (regA < regB) ? regA : regB;
3753 if (fabs((
double) regB) < MagickEpsilon) {
3756 regA = regA - floor((
double) (regA/regB))*regB;
3760 regA = (fxFltType) (regA < MagickEpsilon);
3763 regA = pow ((
double) regA, (
double) regB);
3766 regA = GetPseudoRandomValue (pfxrt->random_info);
3770 regA = floor ((
double) regA + 0.5);
3773 regA = (regA < 0) ? -1.0 : 1.0;
3776 if (fabs((
double) regA) < MagickEpsilon)
3779 regA = sin ((
double) (MagickPI*regA)) / (MagickPI*regA);
3782 regA = sinh ((
double) regA);
3785 regA = sin ((
double) regA);
3788 regA = sqrt ((
double) regA);
3791 regA = 1.0 / (1.0 + exp ((
double) -regA));
3794 regA = tanh ((
double) regA);
3797 regA = tan ((
double) regA);
3800 if (regA >= 0) regA = floor ((
double) regA);
3801 else regA = ceil ((
double) regA);
3813 ssize_t ImgNum = ChkImgNum (pfx, regA);
3814 if (ImgNum < 0)
break;
3815 regA = (fxFltType) 0;
3817 Image * pimg = pfx->Images[0];
3818 if (pel->img_attr_qual == aNull) {
3819 if ((
int) pel->channel_qual < 0) {
3820 if (pel->channel_qual == NO_CHAN_QUAL || pel->channel_qual == THIS_CHANNEL) {
3821 if (pfx->ImgNum==0) {
3822 regA = QuantumScale * (double) p[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3824 const Quantum * pv = GetCacheViewVirtualPixels (
3825 pfx->Imgs[0].View, imgx, imgy, 1,1, pfx->exception);
3827 (void) ThrowMagickException (
3828 pfx->exception, GetMagickModule(), OptionError,
3829 "fU can't get cache",
"%lu", (
unsigned long) ImgNum);
3832 regA = QuantumScale * (double) pv[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3834 }
else if (pel->channel_qual == HUE_CHANNEL || pel->channel_qual == SAT_CHANNEL ||
3835 pel->channel_qual == LIGHT_CHANNEL) {
3836 regA = GetHslInt (pfx, ImgNum, imgx, imgy, pel->channel_qual);
3838 }
else if (pel->channel_qual == INTENSITY_CHANNEL) {
3839 regA = GetIntensity (pfx, 0, (
double) imgx, (
double) imgy);
3843 if (pfx->ImgNum==0) {
3844 regA = QuantumScale * (double) p[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3846 const Quantum * pv = GetCacheViewVirtualPixels (
3847 pfx->Imgs[0].View, imgx, imgy, 1,1, pfx->exception);
3849 (void) ThrowMagickException (
3850 pfx->exception, GetMagickModule(), OptionError,
3851 "fU can't get cache",
"%lu", (
unsigned long) ImgNum);
3854 regA = QuantumScale * (double) pv[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3859 regA = ImageStat (pfx, 0, WHICH_ATTR_CHAN, pel->img_attr_qual);
3863 if (pel->img_attr_qual == aNull) {
3865 if ((
int) pel->channel_qual < 0) {
3866 if (pel->channel_qual == HUE_CHANNEL || pel->channel_qual == SAT_CHANNEL ||
3867 pel->channel_qual == LIGHT_CHANNEL)
3869 regA = GetHslInt (pfx, ImgNum, imgx, imgy, pel->channel_qual);
3871 }
else if (pel->channel_qual == INTENSITY_CHANNEL)
3873 regA = GetIntensity (pfx, ImgNum, (fxFltType) imgx, (fxFltType) imgy);
3878 pv = GetCacheViewVirtualPixels (
3879 pfx->Imgs[ImgNum].View, imgx, imgy, 1,1, pfx->exception);
3881 (void) ThrowMagickException (
3882 pfx->exception, GetMagickModule(), OptionError,
3883 "fU can't get cache",
"%lu", (
unsigned long) ImgNum);
3886 regA = QuantumScale * (double)
3887 pv[pfx->Images[ImgNum]->channel_map[WHICH_NON_ATTR_CHAN].offset];
3889 regA = ImageStat (pfx, ImgNum, WHICH_ATTR_CHAN, pel->img_attr_qual);
3898 Image * pimg = pfx->Images[0];
3899 if ((
int) pel->channel_qual < 0) {
3900 if (pel->channel_qual == NO_CHAN_QUAL || pel->channel_qual == THIS_CHANNEL) {
3902 if (pfx->ImgNum==0) {
3903 regA = QuantumScale * (double) p[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3905 const Quantum * pv = GetCacheViewVirtualPixels (
3906 pfx->Imgs[0].View, imgx, imgy, 1,1, pfx->exception);
3908 (void) ThrowMagickException (
3909 pfx->exception, GetMagickModule(), OptionError,
3910 "fU0 can't get cache",
"%i", 0);
3913 regA = QuantumScale * (double) pv[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3916 }
else if (pel->channel_qual == HUE_CHANNEL || pel->channel_qual == SAT_CHANNEL ||
3917 pel->channel_qual == LIGHT_CHANNEL) {
3918 regA = GetHslInt (pfx, 0, imgx, imgy, pel->channel_qual);
3920 }
else if (pel->channel_qual == INTENSITY_CHANNEL) {
3921 regA = GetIntensity (pfx, 0, (fxFltType) imgx, (fxFltType) imgy);
3924 if (pfx->ImgNum==0) {
3925 regA = QuantumScale * (double) p[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3927 const Quantum * pv = GetCacheViewVirtualPixels (
3928 pfx->Imgs[0].View, imgx, imgy, 1,1, pfx->exception);
3930 (void) ThrowMagickException (
3931 pfx->exception, GetMagickModule(), OptionError,
3932 "fU0 can't get cache",
"%i", 0);
3935 regA = QuantumScale * (double) pv[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3942 ssize_t ImgNum = ChkImgNum (pfx, regA);
3945 if (ImgNum < 0)
break;
3947 if (pel->is_relative) {
3955 if ((
int) pel->channel_qual < 0) {
3956 if (pel->channel_qual == HUE_CHANNEL || pel->channel_qual == SAT_CHANNEL
3957 || pel->channel_qual == LIGHT_CHANNEL) {
3958 regA = GetHslFlt (pfx, ImgNum, fx, fy, pel->channel_qual);
3960 }
else if (pel->channel_qual == INTENSITY_CHANNEL) {
3961 regA = GetIntensity (pfx, ImgNum, fx, fy);
3968 Image * imUP = pfx->Images[ImgNum];
3969 if (! InterpolatePixelChannel (imUP, pfx->Imgs[ImgNum].View, WHICH_NON_ATTR_CHAN,
3970 imUP->interpolate, (
double) fx, (
double) fy, &v, pfx->exception))
3972 (void) ThrowMagickException (
3973 pfx->exception, GetMagickModule(), OptionError,
3974 "fUP can't get interpolate",
"%lu", (
unsigned long) ImgNum);
3977 regA = v * QuantumScale;
3986 if (pel->operator_index == fS) ImgNum = pfx->ImgNum;
3988 if (pel->img_attr_qual == aNull) {
3989 const Quantum * pv = GetCacheViewVirtualPixels (
3990 pfx->Imgs[ImgNum].View, imgx, imgy, 1,1, pfx->exception);
3992 (void) ThrowMagickException (
3993 pfx->exception, GetMagickModule(), OptionError,
3994 "fV can't get cache",
"%lu", (
unsigned long) ImgNum);
3998 if ((
int) pel->channel_qual < 0) {
3999 if (pel->channel_qual == HUE_CHANNEL || pel->channel_qual == SAT_CHANNEL ||
4000 pel->channel_qual == LIGHT_CHANNEL) {
4001 regA = GetHslInt (pfx, ImgNum, imgx, imgy, pel->channel_qual);
4003 }
else if (pel->channel_qual == INTENSITY_CHANNEL) {
4004 regA = GetIntensity (pfx, ImgNum, (
double) imgx, (
double) imgy);
4009 regA = QuantumScale * (double)
4010 pv[pfx->Images[ImgNum]->channel_map[WHICH_NON_ATTR_CHAN].offset];
4012 regA = ImageStat (pfx, ImgNum, WHICH_ATTR_CHAN, pel->img_attr_qual);
4022 ssize_t ImgNum = pfx->ImgNum;
4023 if (pel->operator_index == fVP) ImgNum = 1;
4024 if (pel->is_relative) {
4031 if ((
int) pel->channel_qual < 0) {
4032 if (pel->channel_qual == HUE_CHANNEL || pel->channel_qual == SAT_CHANNEL ||
4033 pel->channel_qual == LIGHT_CHANNEL) {
4034 regA = GetHslFlt (pfx, ImgNum, fx, fy, pel->channel_qual);
4036 }
else if (pel->channel_qual == INTENSITY_CHANNEL) {
4037 regA = GetIntensity (pfx, ImgNum, fx, fy);
4045 if (! InterpolatePixelChannel (pfx->Images[ImgNum], pfx->Imgs[ImgNum].View,
4046 WHICH_NON_ATTR_CHAN, pfx->Images[ImgNum]->interpolate,
4047 (
double) fx, (
double) fy, &v, pfx->exception)
4050 (void) ThrowMagickException (
4051 pfx->exception, GetMagickModule(), OptionError,
4052 "fSP or fVP can't get interp",
"%lu", (
unsigned long) ImgNum);
4055 regA = v * (fxFltType)QuantumScale;
4063 regA = (fxFltType) GetImageDepth (img, pfx->exception);
4066 regA = (fxFltType) GetBlobSize (img);
4069 if ((cs != (ChannelStatistics *) NULL) && (channel >= 0))
4070 regA = cs[WHICH_ATTR_CHAN].kurtosis;
4073 if ((cs != (ChannelStatistics *) NULL) && (channel >= 0))
4074 regA = cs[WHICH_ATTR_CHAN].maxima;
4077 if ((cs != (ChannelStatistics *) NULL) && (channel >= 0))
4078 regA = cs[WHICH_ATTR_CHAN].mean;
4081 if ((cs != (ChannelStatistics *) NULL) && (channel >= 0))
4082 regA = cs[WHICH_ATTR_CHAN].median;
4085 if ((cs != (ChannelStatistics *) NULL) && (channel >= 0))
4086 regA = cs[WHICH_ATTR_CHAN].minima;
4091 regA = (fxFltType) img->page.x;
4094 regA = (fxFltType) img->page.y;
4097 regA = (fxFltType) img->page.width;
4100 regA = (fxFltType) img->page.height;
4105 regA = (fxFltType) MagickSafeReciprocal (img->resolution.x) * img->columns;
4108 regA = (fxFltType) MagickSafeReciprocal (img->resolution.y) * img->rows;
4111 regA = (fxFltType) img->quality;
4116 regA = (fxFltType) img->resolution.x;
4119 regA = (fxFltType) img->resolution.y;
4122 if ((cs != (ChannelStatistics *) NULL) && (channel >= 0))
4123 regA = cs[WHICH_ATTR_CHAN].skewness;
4126 if ((cs != (ChannelStatistics *) NULL) && (channel >= 0))
4127 regA = cs[WHICH_ATTR_CHAN].standard_deviation;
4130 regA = (fxFltType) img->rows;
4133 regA = (fxFltType) pfx->ImgListLen;
4136 regA = (fxFltType) pfx->ImgNum;
4139 regA = (fxFltType) img->columns;
4142 regA = (fxFltType) GetImageDepth (img, pfx->exception);
4150 regA = GetIntensity (pfx, pfx->ImgNum, (
double) imgx, (
double) imgy);
4157 regA = QuantumScale * (0.212656 * (double) GetPixelRed (img,p) +
4158 0.715158 * (double) GetPixelGreen (img,p) +
4159 0.072186 * (double) GetPixelBlue (img,p));
4165 regA = QuantumScale * (double) GetPixelAlpha (img, p);
4168 regA = QuantumScale * (double) GetPixelBlue (img, p);
4171 regA = QuantumScale * (double) GetPixelCyan (img, p);
4174 regA = QuantumScale * (double) GetPixelGreen (img, p);
4177 regA = (fxFltType) imgx;
4180 regA = (fxFltType) imgy;
4183 regA = QuantumScale * (double) GetPixelBlack (img, p);
4186 regA = QuantumScale * (double) GetPixelGreen (img, p);
4189 regA = QuantumScale * (double) GetPixelAlpha (img, p);
4192 regA = QuantumScale * (double) GetPixelRed (img, p);
4195 regA = QuantumScale * (double) GetPixelYellow (img, p);
4201 assert (pel->element_index >= 0);
4202 i = pel->element_index-1;
4205 assert (pel->element_index >= 0);
4206 i = pel->element_index-1;
4207 if (IsImageTTLExpired(img) != MagickFalse) {
4208 i = pfx->usedElements-1;
4209 (void) ThrowMagickException (pfx->exception, GetMagickModule(),
4210 ResourceLimitFatalError,
"TimeLimitExceeded",
"`%s'", img->filename);
4214 assert (pel->element_index >= 0);
4215 if (fabs((
double) regA) < MagickEpsilon) i = pel->element_index-1;
4217 case rIfNotZeroGoto:
4218 assert (pel->element_index >= 0);
4219 if (fabs((
double) regA) > MagickEpsilon) i = pel->element_index-1;
4222 assert (pel->element_index >= 0);
4223 regA = pfxrt->UserSymVals[pel->element_index];
4226 assert (pel->element_index >= 0);
4227 pfxrt->UserSymVals[pel->element_index] = regA;
4230 pfxrt->usedValStack = 0;
4236 (void) ThrowMagickException (
4237 pfx->exception, GetMagickModule(), OptionError,
4238 "pel->oprNum",
"%i '%s' not yet implemented",
4239 (
int)pel->operator_index, OprStr(pel->operator_index));
4243 if (!PushVal (pfx, pfxrt, regA, i))
break;
4246 if (pfxrt->usedValStack > 0) regA = PopVal (pfx, pfxrt, 9999);
4250 if (NeedRelinq) cs = (ChannelStatistics *)RelinquishMagickMemory (cs);
4252 if (pfx->exception->severity >= ErrorException)
4255 if (pfxrt->usedValStack != 0) {
4256 (void) ThrowMagickException (
4257 pfx->exception, GetMagickModule(), OptionError,
4258 "ValStack not empty",
"(%i)", pfxrt->usedValStack);
4267MagickPrivate MagickBooleanType FxEvaluateChannelExpression (
4269 const PixelChannel channel,
const ssize_t x,
const ssize_t y,
4270 double *result, ExceptionInfo *exception)
4273 id = GetOpenMPThreadId();
4277 assert (pfx != NULL);
4278 assert (pfx->image != NULL);
4279 assert (pfx->Images != NULL);
4280 assert (pfx->Imgs != NULL);
4281 assert (pfx->fxrts != NULL);
4282 assert (
id >= 0 &&
id < (
int) GetMagickResourceLimit(ThreadResource));
4284 pfx->fxrts[id].thisPixel = NULL;
4287 if (!ExecuteRPN (pfx, &pfx->fxrts[
id], &ret, channel, x, y)) {
4288 (void) ThrowMagickException (
4289 exception, GetMagickModule(), OptionError,
4290 "ExecuteRPN failed",
" ");
4294 *result = (double) ret;
4299static FxInfo *AcquireFxInfoPrivate (
const Image * images,
const char * expression,
4300 MagickBooleanType CalcAllStats, ExceptionInfo *exception)
4304 FxInfo * pfx = (FxInfo*) AcquireCriticalMemory (
sizeof (*pfx));
4306 memset (pfx, 0,
sizeof (*pfx));
4308 if (!InitFx (pfx, images, CalcAllStats, exception)) {
4309 pfx = (FxInfo*) RelinquishMagickMemory(pfx);
4313 if (!BuildRPN (pfx)) {
4314 (void) DeInitFx (pfx);
4315 pfx = (FxInfo*) RelinquishMagickMemory(pfx);
4316 return((FxInfo *) NULL);
4319 if ((*expression ==
'@') && (strlen(expression) > 1))
4320 pfx->expression=FileToString(expression,~0UL,exception);
4321 if (pfx->expression == (
char *) NULL)
4322 pfx->expression=ConstantString(expression);
4323 pfx->pex = (
char *) pfx->expression;
4326 if (!TranslateStatementList (pfx,
";", &chLimit)) {
4327 (void) DestroyRPN (pfx);
4328 pfx->expression = DestroyString (pfx->expression);
4330 (void) DeInitFx (pfx);
4331 pfx = (FxInfo*) RelinquishMagickMemory(pfx);
4336 (void) ThrowMagickException (
4337 pfx->exception, GetMagickModule(), OptionError,
4338 "Translate expression depth",
"(%i) not 0",
4341 (void) DestroyRPN (pfx);
4342 pfx->expression = DestroyString (pfx->expression);
4344 (void) DeInitFx (pfx);
4345 pfx = (FxInfo*) RelinquishMagickMemory(pfx);
4349 if (chLimit !=
'\0' && chLimit !=
';') {
4350 (void) ThrowMagickException (
4351 pfx->exception, GetMagickModule(), OptionError,
4352 "AcquireFxInfo: TranslateExpression did not exhaust input",
"(chLimit=%i) at'%s'",
4353 (
int)chLimit, pfx->pex);
4355 (void) DestroyRPN (pfx);
4356 pfx->expression = DestroyString (pfx->expression);
4358 (void) DeInitFx (pfx);
4359 pfx = (FxInfo*) RelinquishMagickMemory(pfx);
4363 if (pfx->NeedStats && pfx->runType == rtEntireImage && !pfx->statistics) {
4364 if (!CollectStatistics (pfx)) {
4365 (void) DestroyRPN (pfx);
4366 pfx->expression = DestroyString (pfx->expression);
4368 (void) DeInitFx (pfx);
4369 pfx = (FxInfo*) RelinquishMagickMemory(pfx);
4374 if (pfx->DebugOpt) {
4375 DumpTables (stderr);
4376 DumpUserSymbols (pfx, stderr);
4377 (void) DumpRPN (pfx, stderr);
4381 size_t number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
4384 pfx->fxrts = (
fxRtT *)AcquireQuantumMemory (number_threads,
sizeof(
fxRtT));
4386 (void) ThrowMagickException (
4387 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
4389 (
unsigned long) number_threads);
4390 (void) DestroyRPN (pfx);
4391 pfx->expression = DestroyString (pfx->expression);
4393 (void) DeInitFx (pfx);
4394 pfx = (FxInfo*) RelinquishMagickMemory(pfx);
4397 for (t=0; t < (ssize_t) number_threads; t++) {
4398 if (!AllocFxRt (pfx, &pfx->fxrts[t])) {
4399 (void) ThrowMagickException (
4400 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
4401 "AllocFxRt t=",
"%g",
4405 for (t2 = t-1; t2 >= 0; t2--) {
4406 DestroyFxRt (&pfx->fxrts[t2]);
4409 pfx->fxrts = (
fxRtT *) RelinquishMagickMemory (pfx->fxrts);
4410 (void) DestroyRPN (pfx);
4411 pfx->expression = DestroyString (pfx->expression);
4413 (void) DeInitFx (pfx);
4414 pfx = (FxInfo*) RelinquishMagickMemory(pfx);
4422FxInfo *AcquireFxInfo (
const Image * images,
const char * expression, ExceptionInfo *exception)
4424 return AcquireFxInfoPrivate (images, expression, MagickFalse, exception);
4427FxInfo *DestroyFxInfo (FxInfo * pfx)
4431 assert (pfx != NULL);
4432 assert (pfx->image != NULL);
4433 assert (pfx->Images != NULL);
4434 assert (pfx->Imgs != NULL);
4435 assert (pfx->fxrts != NULL);
4437 for (t=0; t < (ssize_t) GetMagickResourceLimit(ThreadResource); t++) {
4438 DestroyFxRt (&pfx->fxrts[t]);
4440 pfx->fxrts = (
fxRtT *) RelinquishMagickMemory (pfx->fxrts);
4444 pfx->expression = DestroyString (pfx->expression);
4447 (void) DeInitFx (pfx);
4449 pfx = (FxInfo*) RelinquishMagickMemory(pfx);
4456MagickExport Image *FxImage(
const Image *image,
const char *expression,
4457 ExceptionInfo *exception)
4459#define FxImageTag "FxNew/Image"
4480 assert(image != (Image *) NULL);
4481 assert(image->signature == MagickCoreSignature);
4482 if (IsEventLogging() != MagickFalse)
4483 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4484 if (expression == (
const char *) NULL)
4485 return(CloneImage(image,0,0,MagickTrue,exception));
4486 fx_image=CloneImage(image,0,0,MagickTrue,exception);
4487 if (!fx_image)
return NULL;
4488 if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse) {
4489 fx_image=DestroyImage(fx_image);
4493 pfx = AcquireFxInfoPrivate (image, expression, MagickTrue, exception);
4496 fx_image=DestroyImage(fx_image);
4500 assert (pfx->image != NULL);
4501 assert (pfx->Images != NULL);
4502 assert (pfx->Imgs != NULL);
4503 assert (pfx->fxrts != NULL);
4507 image_view = AcquireVirtualCacheView (image, pfx->exception);
4508 fx_view = AcquireAuthenticCacheView (fx_image, pfx->exception);
4509#if defined(MAGICKCORE_OPENMP_SUPPORT)
4510 #pragma omp parallel for schedule(dynamic) shared(progress,status) \
4511 magick_number_threads(image,fx_image,fx_image->rows, \
4512 pfx->ContainsDebug ? 0 : 1)
4514 for (y=0; y < (ssize_t) fx_image->rows; y++)
4517 id = GetOpenMPThreadId();
4531 if (status == MagickFalse)
4533 p = GetCacheViewVirtualPixels (image_view, 0, y, image->columns, 1, pfx->exception);
4534 q = QueueCacheViewAuthenticPixels (fx_view, 0, y, fx_image->columns, 1, pfx->exception);
4535 if ((p == (
const Quantum *) NULL) || (q == (Quantum *) NULL)) {
4539 for (x=0; x < (ssize_t) fx_image->columns; x++) {
4542 pfx->fxrts[id].thisPixel = (Quantum *)p;
4544 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4546 PixelChannel channel = GetPixelChannelChannel (image, i);
4547 PixelTrait traits = GetPixelChannelTraits (image, channel);
4548 PixelTrait fx_traits = GetPixelChannelTraits (fx_image, channel);
4549 if ((traits == UndefinedPixelTrait) ||
4550 (fx_traits == UndefinedPixelTrait))
4552 if ((fx_traits & CopyPixelTrait) != 0) {
4553 SetPixelChannel (fx_image, channel, p[i], q);
4557 if (!ExecuteRPN (pfx, &pfx->fxrts[
id], &result, channel, x, y)) {
4562 q[i] = ClampToQuantum ((MagickRealType) (QuantumRange*result));
4564 if (status == MagickFalse)
4566 p+=(ptrdiff_t) GetPixelChannels (image);
4567 q+=(ptrdiff_t) GetPixelChannels (fx_image);
4569 if (SyncCacheViewAuthenticPixels(fx_view, pfx->exception) == MagickFalse)
4571 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4576#if defined(MAGICKCORE_OPENMP_SUPPORT)
4580 proceed = SetImageProgress (image, FxImageTag, progress, image->rows);
4581 if (proceed == MagickFalse)
4586 fx_view = DestroyCacheView (fx_view);
4587 image_view = DestroyCacheView (image_view);
4591 if (pfx->DebugOpt && pfx->usedUserSymbols) {
4593 char UserSym[MagickPathExtent];
4594 fprintf (stderr,
"User symbols (%i):\n", pfx->usedUserSymbols);
4595 for (t=0; t < (int) GetMagickResourceLimit(ThreadResource); t++) {
4596 for (i = 0; i < (int) pfx->usedUserSymbols; i++) {
4597 fprintf (stderr,
"th=%i us=%i '%s': %.*Lg\n",
4598 t, i, NameOfUserSym (pfx, i, UserSym), pfx->precision, pfx->fxrts[t].UserSymVals[i]);
4603 if ((status == MagickFalse) || (pfx->exception->severity >= ErrorException))
4604 fx_image=DestroyImage(fx_image);
4606 pfx=DestroyFxInfo(pfx);