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/statistic-private.h"
94#include "MagickCore/string_.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
111typedef long double fxFltType;
167 {oAddEq,
"+=", 12, 1},
168 {oSubtractEq,
"-=", 12, 1},
169 {oMultiplyEq,
"*=", 13, 1},
170 {oDivideEq,
"/=", 13, 1},
171 {oPlusPlus,
"++", 12, 0},
172 {oSubSub,
"--", 12, 0},
174 {oSubtract,
"-", 12, 2},
175 {oMultiply,
"*", 13, 2},
176 {oDivide,
"/", 13, 2},
177 {oModulus,
"%", 13, 2},
178 {oUnaryPlus,
"+", 14, 1},
179 {oUnaryMinus,
"-", 14, 1},
180 {oLshift,
"<<", 11, 2},
181 {oRshift,
">>", 11, 2},
183 {oNotEq,
"!=", 9, 2},
184 {oLtEq,
"<=", 10, 2},
185 {oGtEq,
">=", 10, 2},
188 {oLogAnd,
"&&", 6, 2},
189 {oLogOr,
"||", 5, 2},
190 {oLogNot,
"!", 16, 1},
191 {oBitAnd,
"&", 8, 2},
193 {oBitNot,
"~", 16, 1},
197 {oOpenParen,
"(", 0, 0},
198 {oCloseParen,
")", 0, 0},
199 {oOpenBracket,
"[", 0, 0},
200 {oCloseBracket,
"]", 0, 0},
201 {oOpenBrace,
"{", 0, 0},
202 {oCloseBrace,
"}", 0, 0},
203 {oAssign,
"=", 3, 1},
204 {oNull,
"onull", 17, 0}
232 {cEpsilon, MagickEpsilon,
"epsilon"},
233 {cE, 2.7182818284590452354,
"e"},
234 {cOpaque, 1.0,
"opaque"},
235 {cPhi, MagickPHI,
"phi"},
236 {cPi, MagickPI,
"pi"},
237 {cQuantumRange, QuantumRange,
"quantumrange"},
238 {cQuantumScale, QuantumScale,
"quantumscale"},
239 {cTransparent, 0.0,
"transparent"},
240 {cMaxRgb, QuantumRange,
"MaxRGB"},
241 {cNull, 0.0,
"cnull"}
244#define FirstFunc ((FunctionE) (oNull+1))
248#if defined(MAGICKCORE_HAVE_ACOSH)
252#if defined(MAGICKCORE_HAVE_J1)
256#if defined(MAGICKCORE_HAVE_ASINH)
260#if defined(MAGICKCORE_HAVE_ATANH)
272#if defined(MAGICKCORE_HAVE_ERF)
282#if defined(MAGICKCORE_HAVE_J0)
285#if defined(MAGICKCORE_HAVE_J1)
288#if defined(MAGICKCORE_HAVE_J1)
339#if defined(MAGICKCORE_HAVE_ACOSH)
340 {fAcosh,
"acosh" , 1},
343#if defined(MAGICKCORE_HAVE_J1)
347#if defined(MAGICKCORE_HAVE_ASINH)
348 {fAsinh,
"asinh" , 1},
351#if defined(MAGICKCORE_HAVE_ATANH)
352 {fAtanh,
"atanh" , 1},
354 {fAtan2,
"atan2" , 2},
357 {fChannel,
"channel", 5},
358 {fClamp,
"clamp" , 1},
361 {fDebug,
"debug" , 1},
363#if defined(MAGICKCORE_HAVE_ERF)
367 {fFloor,
"floor" , 1},
368 {fGauss,
"gauss" , 1},
370 {fHypot,
"hypot" , 2},
372 {fIsnan,
"isnan" , 1},
373#if defined(MAGICKCORE_HAVE_J0)
376#if defined(MAGICKCORE_HAVE_J1)
379#if defined(MAGICKCORE_HAVE_J1)
383 {fLogtwo,
"logtwo", 1},
391 {fRound,
"round" , 1},
397 {fSquish,
"squish", 1},
400 {fTrunc,
"trunc" , 1},
404 {fWhile,
"while", 2},
417#define FirstImgAttr ((ImgAttrE) (fNull+1))
461 {aDepth,
"depth", MagickTrue},
462 {aExtent,
"extent", MagickFalse},
463 {aKurtosis,
"kurtosis", MagickTrue},
464 {aMaxima,
"maxima", MagickTrue},
465 {aMean,
"mean", MagickTrue},
466 {aMedian,
"median", MagickTrue},
467 {aMinima,
"minima", MagickTrue},
468 {aPage,
"page", MagickFalse},
469 {aPageX,
"page.x", MagickFalse},
470 {aPageY,
"page.y", MagickFalse},
471 {aPageWid,
"page.width", MagickFalse},
472 {aPageHt,
"page.height", MagickFalse},
473 {aPrintsize,
"printsize", MagickFalse},
474 {aPrintsizeX,
"printsize.x", MagickFalse},
475 {aPrintsizeY,
"printsize.y", MagickFalse},
476 {aQuality,
"quality", MagickFalse},
477 {aRes,
"resolution", MagickFalse},
478 {aResX,
"resolution.x", MagickFalse},
479 {aResY,
"resolution.y", MagickFalse},
480 {aSkewness,
"skewness", MagickTrue},
481 {aStdDev,
"standard_deviation", MagickTrue},
482 {aH,
"h", MagickFalse},
483 {aN,
"n", MagickFalse},
484 {aT,
"t", MagickFalse},
485 {aW,
"w", MagickFalse},
486 {aZ,
"z", MagickFalse},
487 {aNull,
"anull", MagickFalse},
488 {aNull,
"anull", MagickFalse},
489 {aNull,
"anull", MagickFalse},
490 {aNull,
"anull", MagickFalse}
493#define FirstSym ((SymbolE) (aNull+1))
524static const SymbolT Symbols[] = {
526 {sIntensity,
"intensity"},
527 {sLightness,
"lightness"},
529 {sLuminance,
"luminance"},
530 {sSaturation,
"saturation"},
551#define FirstCont (sNull+1)
578 {rGotoChk,
"gotochk", 0},
579 {rIfZeroGoto,
"ifzerogoto", 1},
580 {rIfNotZeroGoto,
"ifnotzerogoto", 1},
581 {rCopyFrom,
"copyfrom", 0},
582 {rCopyTo,
"copyto", 1},
583 {rZerStk,
"zerstk", 0},
587#define NULL_ADDRESS -2
603#define NO_CHAN_QUAL ((PixelChannel) (-1))
604#define THIS_CHANNEL ((PixelChannel) (-2))
605#define HUE_CHANNEL ((PixelChannel) (-3))
606#define SAT_CHANNEL ((PixelChannel) (-4))
607#define LIGHT_CHANNEL ((PixelChannel) (-5))
608#define INTENSITY_CHANNEL ((PixelChannel) (-6))
611 {
"r", RedPixelChannel},
612 {
"g", GreenPixelChannel},
613 {
"b", BluePixelChannel},
614 {
"c", CyanPixelChannel},
615 {
"m", MagentaPixelChannel},
616 {
"y", YellowPixelChannel},
617 {
"k", BlackPixelChannel},
618 {
"a", AlphaPixelChannel},
619 {
"o", AlphaPixelChannel},
620 {
"hue", HUE_CHANNEL},
621 {
"saturation", SAT_CHANNEL},
622 {
"lightness", LIGHT_CHANNEL},
623 {
"intensity", INTENSITY_CHANNEL},
624 {
"all", CompositePixelChannel},
625 {
"this", THIS_CHANNEL},
649static const char * sElementTypes[] = {
706 fxFltType * ValStack;
707 fxFltType * UserSymVals;
715 MagickBooleanType NeedStats;
716 MagickBooleanType GotStats;
717 MagickBooleanType NeedHsl;
718 MagickBooleanType DebugOpt;
719 MagickBooleanType ContainsDebug;
722 char ShortExp[MagickPathExtent];
724 char token[MagickPathExtent];
735 OperatorE * OperatorStack;
741 **magick_restrict random_infos;
753static MagickBooleanType TranslateStatementList
754 (
FxInfo * pfx,
const char * strLimit,
char * chLimit);
756static MagickBooleanType TranslateExpression
757 (
FxInfo * pfx,
const char * strLimit,
char * chLimit, MagickBooleanType * needPopAll);
759static MagickBooleanType GetFunction (
FxInfo * pfx, FunctionE fe);
761static inline MagickBooleanType ChanIsVirtual (PixelChannel pc)
763 if (pc==HUE_CHANNEL || pc==SAT_CHANNEL || pc==LIGHT_CHANNEL || pc==INTENSITY_CHANNEL)
769static MagickBooleanType InitFx (
FxInfo * pfx,
const Image * img,
775 pfx->ImgListLen = GetImageListLength (img);
776 pfx->ImgNum = GetImageIndexInList (img);
777 pfx->image = (
Image *)img;
779 pfx->NeedStats = MagickFalse;
780 pfx->GotStats = MagickFalse;
781 pfx->NeedHsl = MagickFalse;
782 pfx->DebugOpt = IsStringTrue (GetImageArtifact (img,
"fx:debug"));
783 pfx->statistics = NULL;
786 pfx->exception = exception;
787 pfx->precision = GetMagickPrecision ();
788 pfx->random_infos = AcquireRandomInfoTLS ();
789 pfx->ContainsDebug = MagickFalse;
790 pfx->runType = (CalcAllStats) ? rtEntireImage : rtCornerOnly;
791 pfx->Imgs = (
ImgT *)AcquireQuantumMemory (pfx->ImgListLen, sizeof (
ImgT));
793 (void) ThrowMagickException (
794 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
796 (
unsigned long) pfx->ImgListLen);
800 next = GetFirstImageInList (img);
801 for ( ; next != (
Image *) NULL; next=next->next)
803 ImgT * pimg = &pfx->Imgs[i];
804 pimg->View = AcquireVirtualCacheView (next, pfx->exception);
806 (void) ThrowMagickException (
807 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
811 for ( ; i > 0; i--) {
812 pimg = &pfx->Imgs[i-1];
813 pimg->View = DestroyCacheView (pimg->View);
815 pfx->Imgs=(
ImgT *) RelinquishMagickMemory (pfx->Imgs);
821 pfx->Images = ImageListToArray (img, pfx->exception);
826static MagickBooleanType DeInitFx (
FxInfo * pfx)
830 if (pfx->Images) pfx->Images = (
Image**) RelinquishMagickMemory (pfx->Images);
833 for (i = (ssize_t)GetImageListLength(pfx->image); i > 0; i--) {
834 ImgT * pimg = &pfx->Imgs[i-1];
835 pimg->View = DestroyCacheView (pimg->View);
837 pfx->Imgs=(
ImgT *) RelinquishMagickMemory (pfx->Imgs);
839 pfx->random_infos = DestroyRandomInfoTLS (pfx->random_infos);
841 if (pfx->statistics) {
842 for (i = (ssize_t)GetImageListLength(pfx->image); i > 0; i--) {
843 pfx->statistics[i-1]=(
ChannelStatistics *) RelinquishMagickMemory (pfx->statistics[i-1]);
852static ElementTypeE TypeOfOpr (
int op)
854 if (op < oNull)
return etOperator;
855 if (op == oNull)
return etConstant;
856 if (op <= fNull)
return etFunction;
857 if (op <= aNull)
return etImgAttr;
858 if (op <= sNull)
return etSymbol;
859 if (op <= rNull)
return etControl;
861 return (ElementTypeE) 0;
864static char * SetPtrShortExp (
FxInfo * pfx,
char * pExp,
size_t len)
871 *pfx->ShortExp =
'\0';
874 slen = CopyMagickString (pfx->ShortExp, pExp, len);
876 (void) CopyMagickString (pfx->ShortExp+MaxLen,
"...", 4);
878 p = strchr (pfx->ShortExp,
'\n');
879 if (p) (void) CopyMagickString (p,
"...", 4);
880 p = strchr (pfx->ShortExp,
'\r');
881 if (p) (void) CopyMagickString (p,
"...", 4);
883 return pfx->ShortExp;
886static char * SetShortExp (
FxInfo * pfx)
888 return SetPtrShortExp (pfx, pfx->pex, MaxTokenLen-1);
891static int FindUserSymbol (
FxInfo * pfx,
char * name)
898 lenName = strlen (name);
899 for (i=0; i < pfx->usedUserSymbols; i++) {
901 if (lenName == pus->len && LocaleNCompare (name, pus->pex, lenName)==0)
break;
903 if (i == pfx->usedUserSymbols)
return NULL_ADDRESS;
907static MagickBooleanType ExtendUserSymbols (
FxInfo * pfx)
909 pfx->numUserSymbols = (int) ceil (pfx->numUserSymbols * (1 + TableExtend));
910 pfx->UserSymbols = (
UserSymbolT*) ResizeMagickMemory (pfx->UserSymbols, (
size_t) pfx->numUserSymbols *
sizeof(
UserSymbolT));
911 if (!pfx->UserSymbols) {
912 (void) ThrowMagickException (
913 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
915 pfx->numUserSymbols);
922static int AddUserSymbol (
FxInfo * pfx,
char * pex,
size_t len)
925 if (++pfx->usedUserSymbols >= pfx->numUserSymbols) {
926 if (!ExtendUserSymbols (pfx))
return -1;
928 pus = &pfx->UserSymbols[pfx->usedUserSymbols-1];
932 return pfx->usedUserSymbols-1;
935static void DumpTables (FILE * fh)
939 for (i=0; i <= rNull; i++) {
940 const char * str =
"";
941 if ( i < oNull) str = Operators[i].str;
942 if (i >= (
int) FirstFunc && i < fNull) str = Functions[i-(int) FirstFunc].str;
943 if (i >= (
int) FirstImgAttr && i < aNull) str = ImgAttrs[i-(
int) FirstImgAttr].str;
944 if (i >= (
int) FirstSym && i < sNull) str = Symbols[i-(
int) FirstSym].str;
945 if (i >= (
int) FirstCont && i < rNull) str = Controls[i-(
int) FirstCont].str;
946 if (i==0 ) fprintf (stderr,
"Operators:\n ");
947 else if (i==oNull) fprintf (stderr,
"\nFunctions:\n ");
948 else if (i==fNull) fprintf (stderr,
"\nImage attributes:\n ");
949 else if (i==aNull) fprintf (stderr,
"\nSymbols:\n ");
950 else if (i==sNull) fprintf (stderr,
"\nControls:\n ");
951 fprintf (fh,
" %s", str);
956static char * NameOfUserSym (
FxInfo * pfx,
int ndx,
char * buf)
959 assert (ndx >= 0 && ndx < pfx->usedUserSymbols);
960 pus = &pfx->UserSymbols[ndx];
961 (void) CopyMagickString (buf, pus->pex, pus->len+1);
965static void DumpUserSymbols (
FxInfo * pfx, FILE * fh)
967 char UserSym[MagickPathExtent];
969 fprintf (fh,
"UserSymbols (%i)\n", pfx->usedUserSymbols);
970 for (i=0; i < pfx->usedUserSymbols; i++) {
971 fprintf (fh,
" %i: '%s'\n", i, NameOfUserSym (pfx, i, UserSym));
975static MagickBooleanType BuildRPN (
FxInfo * pfx)
977 pfx->numUserSymbols = InitNumUserSymbols;
978 pfx->usedUserSymbols = 0;
979 pfx->UserSymbols = (
UserSymbolT*) AcquireMagickMemory ((
size_t) pfx->numUserSymbols *
sizeof(
UserSymbolT));
980 if (!pfx->UserSymbols) {
981 (void) ThrowMagickException (
982 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
984 pfx->numUserSymbols);
988 pfx->numElements = RpnInit;
989 pfx->usedElements = 0;
990 pfx->Elements = NULL;
992 pfx->Elements = (
ElementT*) AcquireMagickMemory ((
size_t) pfx->numElements *
sizeof(
ElementT));
994 if (!pfx->Elements) {
995 (void) ThrowMagickException (
996 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
1002 pfx->usedOprStack = 0;
1003 pfx->maxUsedOprStack = 0;
1004 pfx->numOprStack = InitNumOprStack;
1005 pfx->OperatorStack = (OperatorE*) AcquireMagickMemory ((
size_t) pfx->numOprStack *
sizeof(OperatorE));
1006 if (!pfx->OperatorStack) {
1007 (void) ThrowMagickException (
1008 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
1009 "OperatorStack",
"%i",
1017static MagickBooleanType AllocFxRt (
FxInfo * pfx,
fxRtT * pfxrt)
1021 pfxrt->random_info = AcquireRandomInfo ();
1022 pfxrt->thisPixel = NULL;
1024 nRnd = 20 + 10 * (int) GetPseudoRandomValue (pfxrt->random_info);
1025 for (i=0; i < nRnd; i++) (
void) GetPseudoRandomValue (pfxrt->random_info);;
1027 pfxrt->usedValStack = 0;
1028 pfxrt->numValStack = 2 * pfx->maxUsedOprStack;
1029 if (pfxrt->numValStack < MinValStackSize) pfxrt->numValStack = MinValStackSize;
1030 pfxrt->ValStack = (fxFltType*) AcquireMagickMemory ((
size_t) pfxrt->numValStack *
sizeof(fxFltType));
1031 if (!pfxrt->ValStack) {
1032 (void) ThrowMagickException (
1033 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
1035 pfxrt->numValStack);
1039 pfxrt->UserSymVals = NULL;
1041 if (pfx->usedUserSymbols) {
1042 pfxrt->UserSymVals = (fxFltType*) AcquireMagickMemory ((
size_t) pfx->usedUserSymbols *
sizeof(fxFltType));
1043 if (!pfxrt->UserSymVals) {
1044 (void) ThrowMagickException (
1045 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
1046 "UserSymVals",
"%i",
1047 pfx->usedUserSymbols);
1050 for (i = 0; i < pfx->usedUserSymbols; i++) pfxrt->UserSymVals[i] = (fxFltType) 0;
1056static MagickBooleanType ExtendRPN (
FxInfo * pfx)
1058 pfx->numElements = (int) ceil (pfx->numElements * (1 + TableExtend));
1059 pfx->Elements = (
ElementT*) ResizeMagickMemory (pfx->Elements, (
size_t) pfx->numElements *
sizeof(
ElementT));
1060 if (!pfx->Elements) {
1061 (void) ThrowMagickException (
1062 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
1070static inline MagickBooleanType OprInPlace (
int op)
1072 return (op >= oAddEq && op <= oSubSub ? MagickTrue : MagickFalse);
1075static const char * OprStr (
int oprNum)
1078 if (oprNum < 0) str =
"bad OprStr";
1079 else if (oprNum <= oNull) str = Operators[oprNum].str;
1080 else if (oprNum <= fNull) str = Functions[oprNum-(int) FirstFunc].str;
1081 else if (oprNum <= aNull) str = ImgAttrs[oprNum-(int) FirstImgAttr].str;
1082 else if (oprNum <= sNull) str = Symbols[oprNum-(int) FirstSym].str;
1083 else if (oprNum <= rNull) str = Controls[oprNum-(int) FirstCont].str;
1090static MagickBooleanType DumpRPN (
FxInfo * pfx, FILE * fh)
1094 fprintf (fh,
"DumpRPN:");
1095 fprintf (fh,
" numElements=%i", pfx->numElements);
1096 fprintf (fh,
" usedElements=%i", pfx->usedElements);
1097 fprintf (fh,
" maxUsedOprStack=%i", pfx->maxUsedOprStack);
1098 fprintf (fh,
" ImgListLen=%g", (
double) pfx->ImgListLen);
1099 fprintf (fh,
" NeedStats=%s", pfx->NeedStats ?
"yes" :
"no");
1100 fprintf (fh,
" GotStats=%s", pfx->GotStats ?
"yes" :
"no");
1101 fprintf (fh,
" NeedHsl=%s\n", pfx->NeedHsl ?
"yes" :
"no");
1102 if (pfx->runType==rtEntireImage) fprintf (stderr,
"EntireImage");
1103 else if (pfx->runType==rtCornerOnly) fprintf (stderr,
"CornerOnly");
1107 for (i=0; i < pfx->usedElements; i++) {
1108 ElementT * pel = &pfx->Elements[i];
1109 pel->number_dest = 0;
1111 for (i=0; i < pfx->usedElements; i++) {
1112 ElementT * pel = &pfx->Elements[i];
1113 if (pel->operator_index == rGoto || pel->operator_index == rGotoChk || pel->operator_index == rIfZeroGoto || pel->operator_index == rIfNotZeroGoto) {
1114 if (pel->element_index >= 0 && pel->element_index < pfx->numElements) {
1115 ElementT * pelDest = &pfx->Elements[pel->element_index];
1116 pelDest->number_dest++;
1120 for (i=0; i < pfx->usedElements; i++) {
1121 char UserSym[MagickPathExtent];
1123 ElementT * pel = &pfx->Elements[i];
1124 const char * str = OprStr (pel->operator_index);
1125 const char *sRelAbs =
"";
1127 if (pel->operator_index == fP || pel->operator_index == fUP || pel->operator_index == fVP || pel->operator_index == fSP)
1128 sRelAbs = pel->is_relative ?
"[]" :
"{}";
1130 if (pel->type == etColourConstant)
1131 fprintf (fh,
" %i: %s vals=%.*Lg,%.*Lg,%.*Lg '%s%s' nArgs=%i ndx=%i %s",
1132 i, sElementTypes[pel->type],
1133 pfx->precision, pel->val, pfx->precision, pel->val1, pfx->precision, pel->val2,
1134 str, sRelAbs, pel->number_args, pel->element_index,
1135 pel->do_push ?
"push" :
"NO push");
1137 fprintf (fh,
" %i: %s val=%.*Lg '%s%s' nArgs=%i ndx=%i %s",
1138 i, sElementTypes[pel->type], pfx->precision, pel->val, str, sRelAbs,
1139 pel->number_args, pel->element_index,
1140 pel->do_push ?
"push" :
"NO push");
1142 if (pel->img_attr_qual != aNull)
1143 fprintf (fh,
" ia=%s", OprStr((
int) pel->img_attr_qual));
1145 if (pel->channel_qual != NO_CHAN_QUAL) {
1146 if (pel->channel_qual == THIS_CHANNEL) fprintf (stderr,
" ch=this");
1147 else fprintf (stderr,
" ch=%i", pel->channel_qual);
1150 if (pel->operator_index == rCopyTo) {
1151 fprintf (fh,
" CopyTo ==> %s", NameOfUserSym (pfx, pel->element_index, UserSym));
1152 }
else if (pel->operator_index == rCopyFrom) {
1153 fprintf (fh,
" CopyFrom <== %s", NameOfUserSym (pfx, pel->element_index, UserSym));
1154 }
else if (OprInPlace (pel->operator_index)) {
1155 fprintf (fh,
" <==> %s", NameOfUserSym (pfx, pel->element_index, UserSym));
1157 if (pel->number_dest > 0) fprintf (fh,
" <==dest(%i)", pel->number_dest);
1163static void DestroyRPN (
FxInfo * pfx)
1165 pfx->numOprStack = 0;
1166 pfx->usedOprStack = 0;
1167 if (pfx->OperatorStack) pfx->OperatorStack = (OperatorE*) RelinquishMagickMemory (pfx->OperatorStack);
1169 pfx->numElements = 0;
1170 pfx->usedElements = 0;
1171 if (pfx->Elements) pfx->Elements = (
ElementT*) RelinquishMagickMemory (pfx->Elements);
1173 pfx->usedUserSymbols = 0;
1174 if (pfx->UserSymbols) pfx->UserSymbols = (
UserSymbolT*) RelinquishMagickMemory (pfx->UserSymbols);
1177static void DestroyFxRt (
fxRtT * pfxrt)
1179 pfxrt->usedValStack = 0;
1180 if (pfxrt->ValStack) pfxrt->ValStack = (fxFltType*) RelinquishMagickMemory (pfxrt->ValStack);
1181 if (pfxrt->UserSymVals) pfxrt->UserSymVals = (fxFltType*) RelinquishMagickMemory (pfxrt->UserSymVals);
1183 pfxrt->random_info = DestroyRandomInfo (pfxrt->random_info);
1186static size_t GetToken (
FxInfo * pfx)
1197 char * p = pfx->pex;
1201 if (!isalpha((
int)*p))
return 0;
1208 if (LocaleNCompare (p,
"icc-", 4) == 0) {
1211 while (isalpha ((
int)*p)) { len++; p++; }
1212 }
else if (LocaleNCompare (p,
"device-", 7) == 0) {
1215 while (isalpha ((
int)*p)) { len++; p++; }
1217 while (isalpha ((
int)*p)) { len++; p++; }
1218 if (*p ==
'_') { len++; p++; }
1219 while (isalpha ((
int)*p)) { len++; p++; }
1220 while (isdigit ((
int)*p)) { len++; p++; }
1222 if (len >= MaxTokenLen) {
1223 (void) ThrowMagickException (
1224 pfx->exception, GetMagickModule(), OptionError,
1225 "GetToken: too long",
"%g at '%s'",
1226 (double) len, SetShortExp(pfx));
1230 (void) CopyMagickString (pfx->token, pfx->pex, (len+1<MaxTokenLen)?len+1:MaxTokenLen);
1233 pfx->lenToken = strlen (pfx->token);
1237static MagickBooleanType TokenMaybeUserSymbol (
FxInfo * pfx)
1239 char * p = pfx->token;
1242 if (!isalpha ((
int)*p++))
return MagickFalse;
1245 if (i < 2)
return MagickFalse;
1249static MagickBooleanType AddElement (
FxInfo * pfx, fxFltType val,
int oprNum)
1253 assert (oprNum <= rNull);
1255 if (++pfx->usedElements >= pfx->numElements) {
1256 if (!ExtendRPN (pfx))
return MagickFalse;
1259 pel = &pfx->Elements[pfx->usedElements-1];
1260 pel->type = TypeOfOpr (oprNum);
1262 pel->val1 = (fxFltType) 0;
1263 pel->val2 = (fxFltType) 0;
1264 pel->operator_index = oprNum;
1265 pel->do_push = MagickTrue;
1266 pel->element_index = 0;
1267 pel->channel_qual = NO_CHAN_QUAL;
1268 pel->img_attr_qual = aNull;
1269 pel->number_dest = 0;
1270 pel->exp_start = NULL;
1273 if (oprNum <= oNull) pel->number_args = Operators[oprNum].number_args;
1274 else if (oprNum <= fNull) pel->number_args = Functions[oprNum-(int) FirstFunc].number_args;
1275 else if (oprNum <= aNull) pel->number_args = 0;
1276 else if (oprNum <= sNull) pel->number_args = 0;
1277 else pel->number_args = Controls[oprNum-(int) FirstCont].number_args;
1282static MagickBooleanType AddAddressingElement (
FxInfo * pfx,
int oprNum,
int EleNdx)
1285 if (!AddElement (pfx, (fxFltType) 0, oprNum))
return MagickFalse;
1286 pel = &pfx->Elements[pfx->usedElements-1];
1287 pel->element_index = EleNdx;
1288 if (oprNum == rGoto || oprNum == rGotoChk || oprNum == rIfZeroGoto || oprNum == rIfNotZeroGoto
1289 || oprNum == rZerStk)
1291 pel->do_push = MagickFalse;
1301static MagickBooleanType AddColourElement (
FxInfo * pfx, fxFltType val0, fxFltType val1, fxFltType val2)
1304 if (!AddElement (pfx, val0, oNull))
return MagickFalse;
1305 pel = &pfx->Elements[pfx->usedElements-1];
1308 pel->type = etColourConstant;
1312static inline void SkipSpaces (
FxInfo * pfx)
1314 while (isspace ((
int)*pfx->pex)) pfx->pex++;
1317static inline char PeekChar (
FxInfo * pfx)
1323static inline MagickBooleanType PeekStr (
FxInfo * pfx,
const char * str)
1327 return (LocaleNCompare (pfx->pex, str, strlen(str))==0 ? MagickTrue : MagickFalse);
1330static MagickBooleanType ExpectChar (
FxInfo * pfx,
char c)
1332 if (PeekChar (pfx) != c) {
1333 (void) ThrowMagickException (
1334 pfx->exception, GetMagickModule(), OptionError,
1335 "Expected char",
"'%c' at '%s'", c, SetShortExp (pfx));
1342static int MaybeXYWH (
FxInfo * pfx, ImgAttrE * pop)
1349 if (*pop != aPage && *pop != aPrintsize && *pop != aRes)
return 0;
1351 if (PeekChar (pfx) !=
'.')
return 0;
1353 if (!ExpectChar (pfx,
'.'))
return 0;
1355 (void) GetToken (pfx);
1356 if (LocaleCompare (
"x", pfx->token)==0) ret=1;
1357 else if (LocaleCompare (
"y", pfx->token)==0) ret=2;
1358 else if (LocaleCompare (
"width", pfx->token)==0) ret=3;
1359 else if (LocaleCompare (
"height", pfx->token)==0) ret=4;
1362 (void) ThrowMagickException (
1363 pfx->exception, GetMagickModule(), OptionError,
1364 "Invalid 'x' or 'y' or 'width' or 'height' token=",
"'%s' at '%s'",
1365 pfx->token, SetShortExp(pfx));
1367 if (*pop == aPage) (*pop) = (ImgAttrE) ((
int) *pop + ret);
1370 (void) ThrowMagickException (
1371 pfx->exception, GetMagickModule(), OptionError,
1372 "Invalid 'width' or 'height' token=",
"'%s' at '%s'",
1373 pfx->token, SetShortExp(pfx));
1375 (*pop) = (ImgAttrE) ((
int) *pop + ret);
1378 pfx->pex+=pfx->lenToken;
1383static MagickBooleanType ExtendOperatorStack (
FxInfo * pfx)
1385 pfx->numOprStack = (int) ceil (pfx->numOprStack * (1 + TableExtend));
1386 pfx->OperatorStack = (OperatorE*) ResizeMagickMemory (pfx->OperatorStack, (
size_t) pfx->numOprStack *
sizeof(OperatorE));
1387 if (!pfx->OperatorStack) {
1388 (void) ThrowMagickException (
1389 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
1397static MagickBooleanType PushOperatorStack (
FxInfo * pfx,
int op)
1399 if (++pfx->usedOprStack >= pfx->numOprStack) {
1400 if (!ExtendOperatorStack (pfx))
1403 pfx->OperatorStack[pfx->usedOprStack-1] = (OperatorE) op;
1405 if (pfx->maxUsedOprStack < pfx->usedOprStack)
1406 pfx->maxUsedOprStack = pfx->usedOprStack;
1410static OperatorE GetLeadingOp (
FxInfo * pfx)
1412 OperatorE op = oNull;
1414 if (*pfx->pex ==
'-') op = oUnaryMinus;
1415 else if (*pfx->pex ==
'+') op = oUnaryPlus;
1416 else if (*pfx->pex ==
'~') op = oBitNot;
1417 else if (*pfx->pex ==
'!') op = oLogNot;
1418 else if (*pfx->pex ==
'(') op = oOpenParen;
1423static inline MagickBooleanType OprIsUnaryPrefix (OperatorE op)
1425 return (op == oUnaryMinus || op == oUnaryPlus || op == oBitNot || op == oLogNot ? MagickTrue : MagickFalse);
1428static MagickBooleanType TopOprIsUnaryPrefix (
FxInfo * pfx)
1430 if (!pfx->usedOprStack)
return MagickFalse;
1432 return OprIsUnaryPrefix (pfx->OperatorStack[pfx->usedOprStack-1]);
1435static MagickBooleanType PopOprOpenParen (
FxInfo * pfx, OperatorE op)
1438 if (!pfx->usedOprStack)
return MagickFalse;
1440 if (pfx->OperatorStack[pfx->usedOprStack-1] != op)
return MagickFalse;
1442 pfx->usedOprStack--;
1447static int GetCoordQualifier (
FxInfo * pfx,
int op)
1451 if (op != fU && op != fV && op != fS)
return -1;
1453 (void) GetToken (pfx);
1455 if (pfx->lenToken != 1) {
1458 if (*pfx->token !=
'p' && *pfx->token !=
'P')
return -1;
1459 if (!GetFunction (pfx, fP))
return -1;
1464static PixelChannel GetChannelQualifier (
FxInfo * pfx,
int op)
1466 if (op == fU || op == fV || op == fP ||
1467 op == fUP || op == fVP ||
1468 op == fS || (op >= (
int) FirstImgAttr && op <= aNull)
1471 const ChannelT * pch = &Channels[0];
1472 (void) GetToken (pfx);
1475 if (LocaleCompare (pch->str, pfx->token)==0) {
1477 if (op >= (
int) FirstImgAttr && op <= (int) ((OperatorE)aNull) &&
1478 ChanIsVirtual (pch->pixel_channel)
1481 (void) ThrowMagickException (
1482 pfx->exception, GetMagickModule(), OptionError,
1483 "Can't have image attribute with channel qualifier at",
"'%s' at '%s'",
1484 pfx->token, SetShortExp(pfx));
1485 return NO_CHAN_QUAL;
1488 pfx->pex += pfx->lenToken;
1489 return pch->pixel_channel;
1494 return NO_CHAN_QUAL;
1497static ImgAttrE GetImgAttrToken (
FxInfo * pfx)
1499 ImgAttrE ia = aNull;
1501 for (ia = FirstImgAttr; ia < aNull; ia=(ImgAttrE) (ia+1)) {
1502 iaStr = ImgAttrs[ia-(int) FirstImgAttr].str;
1503 if (LocaleCompare (iaStr, pfx->token)==0) {
1504 pfx->pex += strlen(pfx->token);
1505 if (ImgAttrs[ia-(
int) FirstImgAttr].need_stats != MagickFalse) pfx->NeedStats = MagickTrue;
1506 MaybeXYWH (pfx, &ia);
1511 if (ia == aPage || ia == aPrintsize || ia == aRes) {
1512 (void) ThrowMagickException (
1513 pfx->exception, GetMagickModule(), OptionError,
1514 "Attribute",
"'%s' needs qualifier at '%s'",
1515 iaStr, SetShortExp(pfx));
1521static ImgAttrE GetImgAttrQualifier (
FxInfo * pfx,
int op)
1523 ImgAttrE ia = aNull;
1524 if (op == (OperatorE)fU || op == (OperatorE)fV || op == (OperatorE)fP || op == (OperatorE)fS) {
1525 (void) GetToken (pfx);
1526 if (pfx->lenToken == 0) {
1529 ia = GetImgAttrToken (pfx);
1534static MagickBooleanType IsQualifier (
FxInfo * pfx)
1536 if (PeekChar (pfx) ==
'.') {
1543static ssize_t GetProperty (
FxInfo * pfx, fxFltType *val)
1549 if (PeekStr (pfx,
"%[")) {
1552 char sProperty [MagickPathExtent];
1553 char * p = pfx->pex + 2;
1557 if (*p ==
'[') level++;
1558 else if (*p ==
']') {
1559 if (level == 0)
break;
1564 if (!*p || level != 0) {
1565 (void) ThrowMagickException (
1566 pfx->exception, GetMagickModule(), OptionError,
1567 "After '%[' expected ']' at",
"'%s'",
1572 len = (size_t) (p - pfx->pex + 1);
1573 if (len > MaxTokenLen) {
1574 (void) ThrowMagickException (
1575 pfx->exception, GetMagickModule(), OptionError,
1576 "Too much text between '%[' and ']' at",
"'%s'",
1581 (void) CopyMagickString (sProperty, pfx->pex, len+1);
1582 sProperty[len] =
'\0';
1586 text = InterpretImageProperties (pfx->image->image_info, pfx->image,
1587 sProperty, pfx->exception);
1588 if (!text || !*text) {
1589 text = DestroyString(text);
1590 (void) ThrowMagickException (
1591 pfx->exception, GetMagickModule(), OptionError,
1592 "Unknown property",
"'%s' at '%s'",
1593 sProperty, SetShortExp(pfx));
1597 *val = strtold (text, &tailptr);
1598 if (text == tailptr) {
1599 text = DestroyString(text);
1600 (void) ThrowMagickException (
1601 pfx->exception, GetMagickModule(), OptionError,
1602 "Property",
"'%s' text '%s' is not a number at '%s'",
1603 sProperty, text, SetShortExp(pfx));
1607 text = DestroyString(text);
1609 return ((ssize_t) len);
1615static inline ssize_t GetConstantColour (
FxInfo * pfx, fxFltType *v0, fxFltType *v1, fxFltType *v2)
1626 *dummy_exception = AcquireExceptionInfo ();
1636 char ColSp[MagickPathExtent];
1637 (void) CopyMagickString (ColSp, pfx->token, MaxTokenLen);
1638 p = ColSp + pfx->lenToken - 1;
1639 if (*p ==
'a' || *p ==
'A') *p =
'\0';
1641 (void) GetPixelInfo (pfx->image, &colour);
1645 IsGray = (LocaleCompare (ColSp,
"gray") == 0) ? MagickTrue : MagickFalse;
1646 IsIcc = (LocaleCompare (ColSp,
"icc-color") == 0) ? MagickTrue : MagickFalse;
1647 IsDev = (LocaleNCompare (ColSp,
"device-", 7) == 0) ? MagickTrue : MagickFalse;
1651 if (!QueryColorCompliance (pfx->token, AllCompliance, &colour, dummy_exception) || IsGray) {
1652 ssize_t type = ParseCommandOption (MagickColorspaceOptions, MagickFalse, ColSp);
1653 if (type >= 0 || IsIcc || IsDev) {
1654 char * q = pfx->pex + pfx->lenToken;
1655 while (isspace((
int) ((
unsigned char) *q))) q++;
1658 char sFunc[MagickPathExtent];
1659 while (*q && *q !=
')') q++;
1661 (void) ThrowMagickException (
1662 pfx->exception, GetMagickModule(), OptionError,
1663 "constant color missing ')'",
"at '%s'",
1665 dummy_exception = DestroyExceptionInfo (dummy_exception);
1668 lenfun = (size_t) (q - pfx->pex + 1);
1669 if (lenfun > MaxTokenLen) {
1670 (void) ThrowMagickException (
1671 pfx->exception, GetMagickModule(), OptionError,
1672 "lenfun too long",
"'%lu' at '%s'",
1673 (
unsigned long) lenfun, SetShortExp(pfx));
1674 dummy_exception = DestroyExceptionInfo (dummy_exception);
1677 (void) CopyMagickString (sFunc, pfx->pex, lenfun+1);
1678 if (QueryColorCompliance (sFunc, AllCompliance, &colour, dummy_exception)) {
1679 *v0 = QuantumScale*colour.red;
1680 *v1 = QuantumScale*colour.green;
1681 *v2 = QuantumScale*colour.blue;
1682 dummy_exception = DestroyExceptionInfo (dummy_exception);
1683 return (ssize_t)lenfun;
1686 (void) ThrowMagickException (
1687 pfx->exception, GetMagickModule(), OptionError,
1688 "colorspace but not a valid color with '(...)' at",
"'%s'",
1690 dummy_exception = DestroyExceptionInfo (dummy_exception);
1695 dummy_exception = DestroyExceptionInfo (dummy_exception);
1700 *v0 = QuantumScale*colour.red;
1701 *v1 = QuantumScale*colour.green;
1702 *v2 = QuantumScale*colour.blue;
1704 dummy_exception = DestroyExceptionInfo (dummy_exception);
1705 return (ssize_t)strlen (pfx->token);
1708static inline ssize_t GetHexColour (
FxInfo * pfx, fxFltType *v0, fxFltType *v1, fxFltType *v2)
1717 if (*pfx->pex !=
'#')
return 0;
1721 while (isxdigit ((
int)*p)) p++;
1722 if (isalpha ((
int)*p)) {
1723 (void) ThrowMagickException (
1724 pfx->exception, GetMagickModule(), OptionError,
1725 "Bad hex number at",
"'%s'",
1730 len = (size_t) (p - pfx->pex);
1731 if (len < 1)
return 0;
1732 if (len >= MaxTokenLen) {
1733 (void) ThrowMagickException (
1734 pfx->exception, GetMagickModule(), OptionError,
1735 "Hex colour too long at",
"'%s'",
1739 (void) CopyMagickString (pfx->token, pfx->pex, len+1);
1741 (void) GetPixelInfo (pfx->image, &colour);
1743 if (!QueryColorCompliance (pfx->token, AllCompliance, &colour, pfx->exception)) {
1744 (void) ThrowMagickException (
1745 pfx->exception, GetMagickModule(), OptionError,
1746 "QueryColorCompliance rejected",
"'%s' at '%s'",
1747 pfx->token, SetShortExp(pfx));
1751 *v0 = QuantumScale*colour.red;
1752 *v1 = QuantumScale*colour.green;
1753 *v2 = QuantumScale*colour.blue;
1755 return (ssize_t) len;
1758static MagickBooleanType GetFunction (
FxInfo * pfx, FunctionE fe)
1762 const char * funStr = Functions[fe-(int) FirstFunc].str;
1763 int nArgs = Functions[fe-(int) FirstFunc].number_args;
1765 char expChLimit =
')';
1766 const char *strLimit =
",)";
1767 OperatorE pushOp = oOpenParen;
1774 int ndx0 = NULL_ADDRESS, ndx1 = NULL_ADDRESS, ndx2 = NULL_ADDRESS, ndx3 = NULL_ADDRESS;
1776 MagickBooleanType coordQual = MagickFalse;
1777 PixelChannel chQual = NO_CHAN_QUAL;
1778 ImgAttrE iaQual = aNull;
1780 pfx->pex += pfx->lenToken;
1783 char p = PeekChar (pfx);
1785 (void) ExpectChar (pfx,
'{');
1786 pushOp = oOpenBrace;
1790 }
else if (p==
'[') {
1791 (void) ExpectChar (pfx,
'[');
1792 pushOp = oOpenBracket;
1801 }
else if (fe == fU) {
1802 char p = PeekChar (pfx);
1804 (void) ExpectChar (pfx,
'[');
1805 pushOp = oOpenBracket;
1814 }
else if (fe == fV || fe == fS) {
1816 pushOp = oOpenBracket;
1820 if (!ExpectChar (pfx,
'('))
return MagickFalse;
1822 if (!PushOperatorStack (pfx, (
int) pushOp))
return MagickFalse;
1824 pExpStart = pfx->pex;
1825 ndx0 = pfx->usedElements;
1827 (void) AddAddressingElement (pfx, rGoto, NULL_ADDRESS);
1831 if (TranslateStatementList (pfx, strLimit, &chLimit)) {
1835 (void) ThrowMagickException (
1836 pfx->exception, GetMagickModule(), OptionError,
1837 "For function",
"'%s' expected ')' at '%s'",
1838 funStr, SetShortExp(pfx));
1842 if (!chLimit)
break;
1843 if (fe == fP || fe == fS|| fe == fIf) {
1844 (void) AddElement (pfx, (fxFltType) 0, oNull);
1849 if (strchr (strLimit, chLimit)==NULL) {
1850 (void) ThrowMagickException (
1851 pfx->exception, GetMagickModule(), OptionError,
1852 "For function",
"'%s' expected one of '%s' after expression but found '%c' at '%s'",
1853 funStr, strLimit, chLimit ? chLimit :
' ', SetShortExp(pfx));
1862 if (ndx1 != NULL_ADDRESS) {
1863 (void) ThrowMagickException (
1864 pfx->exception, GetMagickModule(), OptionError,
1865 "For function",
"'%s' required argument is missing at '%s'",
1866 funStr, SetShortExp(pfx));
1869 ndx1 = pfx->usedElements;
1870 if (fe==fWhile || fe==fIf) {
1871 (void) AddAddressingElement (pfx, rIfZeroGoto, NULL_ADDRESS);
1872 }
else if (fe==fDo) {
1873 (void) AddAddressingElement (pfx, rIfZeroGoto, NULL_ADDRESS);
1874 }
else if (fe==fFor) {
1875 pfx->Elements[pfx->usedElements-1].do_push = MagickFalse;
1879 if (ndx2 != NULL_ADDRESS) {
1880 (void) ThrowMagickException (
1881 pfx->exception, GetMagickModule(), OptionError,
1882 "For function",
"'%s' required argument is missing at '%s'",
1883 funStr, SetShortExp(pfx));
1886 ndx2 = pfx->usedElements;
1888 pfx->Elements[pfx->usedElements-1].do_push = MagickFalse;
1889 (void) AddAddressingElement (pfx, rGotoChk, ndx0);
1890 }
else if (fe==fDo) {
1891 pfx->Elements[pfx->usedElements-1].do_push = MagickFalse;
1892 (void) AddAddressingElement (pfx, rGotoChk, ndx0 + 1);
1893 }
else if (fe==fFor) {
1894 (void) AddAddressingElement (pfx, rIfZeroGoto, NULL_ADDRESS);
1895 pfx->Elements[pfx->usedElements-1].do_push = MagickTrue;
1896 (void) AddAddressingElement (pfx, rZerStk, NULL_ADDRESS);
1897 }
else if (fe==fIf) {
1898 (void) AddAddressingElement (pfx, rGoto, NULL_ADDRESS);
1902 if (ndx3 != NULL_ADDRESS) {
1903 (void) ThrowMagickException (
1904 pfx->exception, GetMagickModule(), OptionError,
1905 "For function",
"'%s' required argument is missing at '%s'",
1906 funStr, SetShortExp(pfx));
1910 pfx->Elements[pfx->usedElements-1].do_push = MagickFalse;
1911 (void) AddAddressingElement (pfx, rGotoChk, ndx1);
1913 ndx3 = pfx->usedElements;
1918 if (chLimit == expChLimit) {
1919 lenExp = (size_t) (pfx->pex - pExpStart - 1);
1923 if (chLimit && chLimit != expChLimit && chLimit !=
',' ) {
1924 (void) ThrowMagickException (
1925 pfx->exception, GetMagickModule(), OptionError,
1926 "For function",
"'%s' expected '%c', found '%c' at '%s'",
1927 funStr, expChLimit, chLimit ? chLimit :
' ', SetShortExp(pfx));
1931 if (fe == fP || fe == fS || fe == fU || fe == fChannel) {
1932 while (FndArgs < Functions[fe-(
int) FirstFunc].number_args) {
1933 (void) AddElement (pfx, (fxFltType) 0, oNull);
1938 if (FndArgs > Functions[fe-(
int) FirstFunc].number_args)
1941 (void) ThrowMagickException (
1942 pfx->exception, GetMagickModule(), OptionError,
1943 "For function",
"'%s' expected up to %i arguments, found '%i' at '%s'",
1944 funStr, Functions[fe-(int) FirstFunc].number_args, FndArgs, SetShortExp(pfx));
1946 (void) ThrowMagickException (
1947 pfx->exception, GetMagickModule(), OptionError,
1948 "For function",
"'%s' expected %i arguments, found '%i' at '%s'",
1949 funStr, Functions[fe-(int) FirstFunc].number_args, FndArgs, SetShortExp(pfx));
1953 if (FndArgs < Functions[fe-(
int) FirstFunc].number_args) {
1954 (void) ThrowMagickException (
1955 pfx->exception, GetMagickModule(), OptionError,
1956 "For function",
"'%s' expected %i arguments, found too few (%i) at '%s'",
1957 funStr, Functions[fe-(int) FirstFunc].number_args, FndArgs, SetShortExp(pfx));
1960 if (fe != fS && fe != fV && FndArgs == 0 && Functions[fe-(
int) FirstFunc].number_args == 0) {
1962 chLimit = expChLimit;
1963 if (!ExpectChar (pfx,
')'))
return MagickFalse;
1966 if (chLimit != expChLimit) {
1967 (void) ThrowMagickException (
1968 pfx->exception, GetMagickModule(), OptionError,
1969 "For function",
"'%s', arguments don't end with '%c' at '%s'",
1970 funStr, expChLimit, SetShortExp(pfx));
1973 if (!PopOprOpenParen (pfx, pushOp)) {
1974 (void) ThrowMagickException (
1975 pfx->exception, GetMagickModule(), OptionError,
1976 "Bug: For function",
"'%s' tos not '%s' at '%s'",
1977 funStr, Operators[pushOp].str, SetShortExp(pfx));
1981 if (IsQualifier (pfx)) {
1983 if (fe == fU || fe == fV || fe == fS) {
1985 coordQual = (GetCoordQualifier (pfx, (
int) fe) == 1) ? MagickTrue : MagickFalse;
1990 ElementT * pel = &pfx->Elements[pfx->usedElements-1];
1991 if (pel->operator_index != fP) {
1992 (void) ThrowMagickException (
1993 pfx->exception, GetMagickModule(), OptionError,
1994 "Bug: For function",
"'%s' last element not 'p' at '%s'",
1995 funStr, SetShortExp(pfx));
1998 chQual = pel->channel_qual;
1999 expChLimit = (pel->is_relative) ?
']' :
'}';
2000 pfx->usedElements--;
2001 if (fe == fU) fe = fUP;
2002 else if (fe == fV) fe = fVP;
2003 else if (fe == fS) fe = fSP;
2004 funStr = Functions[fe-(int) FirstFunc].str;
2008 if ( chQual == NO_CHAN_QUAL &&
2009 (fe == fP || fe == fS || fe == fSP || fe == fU || fe == fUP || fe == fV || fe == fVP)
2012 chQual = GetChannelQualifier (pfx, (
int) fe);
2015 if (chQual == NO_CHAN_QUAL && (fe == fU || fe == fV || fe == fS)) {
2017 iaQual = GetImgAttrQualifier (pfx, (
int) fe);
2019 if (IsQualifier (pfx) && chQual == NO_CHAN_QUAL && iaQual != aNull) {
2020 chQual = GetChannelQualifier (pfx, (
int) fe);
2022 if (coordQual && iaQual != aNull) {
2023 (void) ThrowMagickException (
2024 pfx->exception, GetMagickModule(), OptionError,
2025 "For function",
"'%s', can't have qualifiers 'p' and image attribute '%s' at '%s'",
2026 funStr, pfx->token, SetShortExp(pfx));
2029 if (!coordQual && chQual == NO_CHAN_QUAL && iaQual == aNull) {
2030 (void) ThrowMagickException (
2031 pfx->exception, GetMagickModule(), OptionError,
2032 "For function",
"'%s', bad qualifier '%s' at '%s'",
2033 funStr, pfx->token, SetShortExp(pfx));
2036 if (!coordQual && chQual == CompositePixelChannel && iaQual == aNull) {
2037 (void) ThrowMagickException (
2038 pfx->exception, GetMagickModule(), OptionError,
2039 "For function",
"'%s', bad composite qualifier '%s' at '%s'",
2040 funStr, pfx->token, SetShortExp(pfx));
2044 if (chQual == HUE_CHANNEL || chQual == SAT_CHANNEL || chQual == LIGHT_CHANNEL) {
2045 pfx->NeedHsl = MagickTrue;
2047 if (iaQual >= FirstImgAttr && iaQual < aNull) {
2048 (void) ThrowMagickException (
2049 pfx->exception, GetMagickModule(), OptionError,
2050 "Can't have image attribute with HLS qualifier at",
"'%s'",
2057 if (iaQual != aNull && chQual != NO_CHAN_QUAL) {
2058 if (ImgAttrs[iaQual-(
int) FirstImgAttr].need_stats == MagickFalse) {
2059 (void) ThrowMagickException (
2060 pfx->exception, GetMagickModule(), OptionError,
2061 "Can't have image attribute ",
"'%s' with channel qualifier '%s' at '%s'",
2062 ImgAttrs[iaQual-(int) FirstImgAttr].str,
2063 pfx->token, SetShortExp(pfx));
2066 if (ChanIsVirtual (chQual)) {
2067 (void) ThrowMagickException (
2068 pfx->exception, GetMagickModule(), OptionError,
2069 "Can't have statistical image attribute ",
"'%s' with virtual channel qualifier '%s' at '%s'",
2070 ImgAttrs[iaQual-(int) FirstImgAttr].str,
2071 pfx->token, SetShortExp(pfx));
2078 pfx->Elements[ndx1].element_index = ndx2+1;
2079 }
else if (fe==fDo) {
2080 pfx->Elements[ndx0].element_index = ndx1+1;
2081 pfx->Elements[ndx1].element_index = ndx2+1;
2082 }
else if (fe==fFor) {
2083 pfx->Elements[ndx2].element_index = ndx3;
2084 }
else if (fe==fIf) {
2085 pfx->Elements[ndx1].element_index = ndx2 + 1;
2086 pfx->Elements[ndx2].element_index = ndx3;
2088 if (fe == fU && iaQual == aNull) {
2089 ElementT * pel = &pfx->Elements[pfx->usedElements-1];
2090 if (pel->type == etConstant && pel->val == 0.0) {
2091 pfx->usedElements--;
2095 (void) AddElement (pfx, (fxFltType) 0, (int) fe);
2096 if (fe == fP || fe == fU || fe == fU0 || fe == fUP ||
2097 fe == fV || fe == fVP || fe == fS || fe == fSP)
2099 ElementT * pel = &pfx->Elements[pfx->usedElements-1];
2100 pel->is_relative = (expChLimit ==
']' ? MagickTrue : MagickFalse);
2101 if (chQual >= 0) pel->channel_qual = chQual;
2102 if (iaQual != aNull && (fe == fU || fe == fV || fe == fS)) {
2104 pel->img_attr_qual = iaQual;
2109 if (pExpStart && lenExp) {
2110 ElementT * pel = &pfx->Elements[pfx->usedElements-1];
2111 pel->exp_start = pExpStart;
2112 pel->exp_len = lenExp;
2116 pfx->ContainsDebug = MagickTrue;
2121static MagickBooleanType IsStealth (
int op)
2123 return (op == fU0 || op == fUP || op == fSP || op == fVP ||
2124 (op >= FirstCont && op <= rNull) ? MagickTrue : MagickFalse
2128static MagickBooleanType GetOperand (
2129 FxInfo * pfx, MagickBooleanType * UserSymbol, MagickBooleanType * NewUserSymbol,
int * UserSymNdx,
2130 MagickBooleanType * needPopAll)
2133 *NewUserSymbol = *UserSymbol = MagickFalse;
2134 *UserSymNdx = NULL_ADDRESS;
2137 if (!*pfx->pex)
return MagickFalse;
2138 (void) GetToken (pfx);
2140 if (pfx->lenToken==0) {
2144 OperatorE op = GetLeadingOp (pfx);
2145 if (op==oOpenParen) {
2146 char chLimit =
'\0';
2147 if (!PushOperatorStack (pfx, (
int) op))
return MagickFalse;
2149 if (!TranslateExpression (pfx,
")", &chLimit, needPopAll)) {
2150 (void) ThrowMagickException (
2151 pfx->exception, GetMagickModule(), OptionError,
2152 "Empty expression in parentheses at",
"'%s'",
2156 if (chLimit !=
')') {
2157 (void) ThrowMagickException (
2158 pfx->exception, GetMagickModule(), OptionError,
2159 "'(' but no ')' at",
"'%s'",
2164 if (!PopOprOpenParen (pfx, oOpenParen)) {
2165 (void) ThrowMagickException (
2166 pfx->exception, GetMagickModule(), OptionError,
2167 "Bug: tos not '(' at",
"'%s'",
2172 }
else if (OprIsUnaryPrefix (op)) {
2173 if (!PushOperatorStack (pfx, (
int) op))
return MagickFalse;
2176 if (!*pfx->pex)
return MagickFalse;
2178 if (!GetOperand (pfx, UserSymbol, NewUserSymbol, UserSymNdx, needPopAll)) {
2179 (void) ThrowMagickException (
2180 pfx->exception, GetMagickModule(), OptionError,
2181 "After unary, bad operand at",
"'%s'",
2186 if (*NewUserSymbol) {
2187 (void) ThrowMagickException (
2188 pfx->exception, GetMagickModule(), OptionError,
2189 "After unary, NewUserSymbol at",
"'%s'",
2195 (void) AddAddressingElement (pfx, rCopyFrom, *UserSymNdx);
2196 *UserSymNdx = NULL_ADDRESS;
2198 *UserSymbol = MagickFalse;
2199 *NewUserSymbol = MagickFalse;
2202 (void) GetToken (pfx);
2204 }
else if (*pfx->pex ==
'#') {
2205 fxFltType v0=0, v1=0, v2=0;
2206 ssize_t lenToken = GetHexColour (pfx, &v0, &v1, &v2);
2208 (void) ThrowMagickException (
2209 pfx->exception, GetMagickModule(), OptionError,
2210 "Bad hex number at",
"'%s'",
2213 }
else if (lenToken > 0) {
2214 (void) AddColourElement (pfx, v0, v1, v2);
2225 fxFltType val = strtold (pfx->pex, &tailptr);
2226 if (pfx->pex != tailptr) {
2234 const char Prefixes[] =
"yzafpnum.kMGTPEZY";
2235 const char * pSi = strchr (Prefixes, *tailptr);
2236 if (pSi && *pSi !=
'.') Pow = (double) ((pSi - Prefixes) * 3 - 24);
2237 else if (*tailptr ==
'c') Pow = -2;
2238 else if (*tailptr ==
'h') Pow = 2;
2239 else if (*tailptr ==
'k') Pow = 3;
2241 if (*(++pfx->pex) ==
'i') {
2242 val *= pow (2.0, Pow/0.3);
2245 val *= pow (10.0, Pow);
2249 (void) AddElement (pfx, val, oNull);
2253 val = (fxFltType) 0;
2254 lenOptArt = GetProperty (pfx, &val);
2255 if (lenOptArt < 0)
return MagickFalse;
2256 if (lenOptArt > 0) {
2257 (void) AddElement (pfx, val, oNull);
2258 pfx->pex += lenOptArt;
2265 if (pfx->lenToken > 0) {
2270 for (ce = (ConstantE)0; ce < cNull; ce=(ConstantE) (ce+1)) {
2271 const char * ceStr = Constants[ce].str;
2272 if (LocaleCompare (ceStr, pfx->token)==0) {
2278 (void) AddElement (pfx, Constants[ce].val, oNull);
2279 pfx->pex += pfx->lenToken;
2288 for (fe = FirstFunc; fe < fNull; fe=(FunctionE) (fe+1)) {
2289 const char * feStr = Functions[fe-(int) FirstFunc].str;
2290 if (LocaleCompare (feStr, pfx->token)==0) {
2295 if (fe == fV && pfx->ImgListLen < 2) {
2296 (void) ThrowMagickException (
2297 pfx->exception, GetMagickModule(), OptionError,
2298 "Symbol 'v' but fewer than two images at",
"'%s'",
2303 if (IsStealth ((
int) fe)) {
2304 (void) ThrowMagickException (
2305 pfx->exception, GetMagickModule(), OptionError,
2306 "Function",
"'%s' not permitted at '%s'",
2307 pfx->token, SetShortExp(pfx));
2310 if (fe == fDo || fe == fFor || fe == fIf || fe == fWhile) {
2311 *needPopAll = MagickTrue;
2314 if (fe != fNull)
return (GetFunction (pfx, fe));
2320 ImgAttrE ia = GetImgAttrToken (pfx);
2323 (void) AddElement (pfx, val, (
int) ia);
2325 if (ImgAttrs[ia-(
int) FirstImgAttr].need_stats != MagickFalse) {
2326 if (IsQualifier (pfx)) {
2327 PixelChannel chQual = GetChannelQualifier (pfx, (
int) ia);
2329 if (chQual == NO_CHAN_QUAL) {
2330 (void) ThrowMagickException (
2331 pfx->exception, GetMagickModule(), OptionError,
2332 "Bad channel qualifier at",
"'%s'",
2337 pel = &pfx->Elements[pfx->usedElements-1];
2338 pel->channel_qual = chQual;
2349 for (se = FirstSym; se < sNull; se=(SymbolE) (se+1)) {
2350 const char * seStr = Symbols[se-(int) FirstSym].str;
2351 if (LocaleCompare (seStr, pfx->token)==0) {
2357 (void) AddElement (pfx, val, (
int) se);
2358 pfx->pex += pfx->lenToken;
2360 if (se==sHue || se==sSaturation || se==sLightness) pfx->NeedHsl = MagickTrue;
2368 fxFltType v0, v1, v2;
2369 ssize_t ColLen = GetConstantColour (pfx, &v0, &v1, &v2);
2370 if (ColLen < 0)
return MagickFalse;
2372 (void) AddColourElement (pfx, v0, v1, v2);
2381 const char *artifact;
2382 artifact = GetImageArtifact (pfx->image, pfx->token);
2383 if (artifact != (
const char *) NULL) {
2385 fxFltType val = strtold (artifact, &tailptr);
2386 if (pfx->token == tailptr) {
2387 (void) ThrowMagickException (
2388 pfx->exception, GetMagickModule(), OptionError,
2389 "Artifact",
"'%s' has value '%s', not a number, at '%s'",
2390 pfx->token, artifact, SetShortExp(pfx));
2393 (void) AddElement (pfx, val, oNull);
2394 pfx->pex+=pfx->lenToken;
2401 if (TokenMaybeUserSymbol (pfx)) {
2402 *UserSymbol = MagickTrue;
2403 *UserSymNdx = FindUserSymbol (pfx, pfx->token);
2404 if (*UserSymNdx == NULL_ADDRESS) {
2405 *UserSymNdx = AddUserSymbol (pfx, pfx->pex, pfx->lenToken);
2406 *NewUserSymbol = MagickTrue;
2409 pfx->pex += pfx->lenToken;
2415 (void) ThrowMagickException (
2416 pfx->exception, GetMagickModule(), OptionError,
2417 "Expected operand at",
"'%s'",
2423static inline MagickBooleanType IsRealOperator (OperatorE op)
2425 return (op < oOpenParen || op > oCloseBrace) ? MagickTrue : MagickFalse;
2428static inline MagickBooleanType ProcessTernaryOpr (
FxInfo * pfx,
TernaryT * ptern)
2433 if (pfx->usedOprStack == 0)
2435 if (pfx->OperatorStack[pfx->usedOprStack-1] == oQuery) {
2436 if (ptern->addr_query != NULL_ADDRESS) {
2437 (void) ThrowMagickException (
2438 pfx->exception, GetMagickModule(), OptionError,
2439 "Already have '?' in sub-expression at",
"'%s'",
2443 if (ptern->addr_colon != NULL_ADDRESS) {
2444 (void) ThrowMagickException (
2445 pfx->exception, GetMagickModule(), OptionError,
2446 "Already have ':' in sub-expression at",
"'%s'",
2450 pfx->usedOprStack--;
2451 ptern->addr_query = pfx->usedElements;
2452 (void) AddAddressingElement (pfx, rIfZeroGoto, NULL_ADDRESS);
2455 else if (pfx->OperatorStack[pfx->usedOprStack-1] == oColon) {
2456 if (ptern->addr_query == NULL_ADDRESS) {
2457 (void) ThrowMagickException (
2458 pfx->exception, GetMagickModule(), OptionError,
2459 "Need '?' in sub-expression at",
"'%s'",
2463 if (ptern->addr_colon != NULL_ADDRESS) {
2464 (void) ThrowMagickException (
2465 pfx->exception, GetMagickModule(), OptionError,
2466 "Already have ':' in sub-expression at",
"'%s'",
2470 pfx->usedOprStack--;
2471 ptern->addr_colon = pfx->usedElements;
2472 pfx->Elements[pfx->usedElements-1].do_push = MagickTrue;
2473 (void) AddAddressingElement (pfx, rGoto, NULL_ADDRESS);
2479static MagickBooleanType GetOperator (
2481 MagickBooleanType * Assign, MagickBooleanType * Update, MagickBooleanType * IncrDecr)
2485 MagickBooleanType DoneIt = MagickFalse;
2487 for (op = (OperatorE)0; op != oNull; op=(OperatorE) (op+1)) {
2488 const char * opStr = Operators[op].str;
2489 len = strlen(opStr);
2490 if (LocaleNCompare (opStr, pfx->pex, len)==0) {
2495 if (!IsRealOperator (op)) {
2496 (void) ThrowMagickException (
2497 pfx->exception, GetMagickModule(), OptionError,
2498 "Not a real operator at",
"'%s'",
2504 (void) ThrowMagickException (
2505 pfx->exception, GetMagickModule(), OptionError,
2506 "Expected operator at",
"'%s'",
2511 *Assign = (op==oAssign) ? MagickTrue : MagickFalse;
2512 *Update = OprInPlace ((
int) op);
2513 *IncrDecr = (op == oPlusPlus || op == oSubSub) ? MagickTrue : MagickFalse;
2520 while (pfx->usedOprStack > 0) {
2521 OperatorE top = pfx->OperatorStack[pfx->usedOprStack-1];
2522 int precTop, precNew;
2523 if (top == oOpenParen || top == oAssign || OprInPlace ((
int) top))
break;
2524 precTop = Operators[top].precedence;
2525 precNew = Operators[op].precedence;
2529 if (precTop < precNew)
break;
2530 (void) AddElement (pfx, (fxFltType) 0, (int) top);
2531 pfx->usedOprStack--;
2537 if (op==oCloseParen) {
2538 if (pfx->usedOprStack == 0) {
2539 (void) ThrowMagickException (
2540 pfx->exception, GetMagickModule(), OptionError,
2541 "Found ')' but nothing on stack at",
"'%s'",
2546 if (pfx->OperatorStack[pfx->usedOprStack-1] != oOpenParen) {
2547 (void) ThrowMagickException (
2548 pfx->exception, GetMagickModule(), OptionError,
2549 "Found ')' but no '(' on stack at",
"'%s'",
2553 pfx->usedOprStack--;
2554 DoneIt = MagickTrue;
2558 if (!PushOperatorStack (pfx, (
int) op))
return MagickFalse;
2566static MagickBooleanType ResolveTernaryAddresses (
FxInfo * pfx,
TernaryT * ptern)
2568 if (ptern->addr_query == NULL_ADDRESS && ptern->addr_colon == NULL_ADDRESS)
2571 if (ptern->addr_query != NULL_ADDRESS && ptern->addr_colon != NULL_ADDRESS) {
2572 pfx->Elements[ptern->addr_query].element_index = ptern->addr_colon + 1;
2573 pfx->Elements[ptern->addr_colon].element_index = pfx->usedElements;
2574 ptern->addr_query = NULL_ADDRESS;
2575 ptern->addr_colon = NULL_ADDRESS;
2576 }
else if (ptern->addr_query != NULL_ADDRESS) {
2577 (void) ThrowMagickException (
2578 pfx->exception, GetMagickModule(), OptionError,
2579 "'?' with no corresponding ':'",
"'%s' at '%s'",
2580 pfx->token, SetShortExp(pfx));
2582 }
else if (ptern->addr_colon != NULL_ADDRESS) {
2583 (void) ThrowMagickException (
2584 pfx->exception, GetMagickModule(), OptionError,
2585 "':' with no corresponding '?'",
"'%s' at '%s'",
2586 pfx->token, SetShortExp(pfx));
2592static MagickBooleanType TranslateExpression (
2593 FxInfo * pfx,
const char * strLimit,
char * chLimit, MagickBooleanType * needPopAll)
2597 MagickBooleanType UserSymbol, NewUserSymbol;
2598 int UserSymNdx0, UserSymNdx1;
2601 Assign = MagickFalse,
2602 Update = MagickFalse,
2603 IncrDecr = MagickFalse;
2608 ternary.addr_query = NULL_ADDRESS;
2609 ternary.addr_colon = NULL_ADDRESS;
2611 if (pfx->teDepth >= MagickMaxRecursionDepth) {
2612 (void) ThrowMagickException(pfx->exception, GetMagickModule(), OptionError,
2613 "Expression too deeply nested",
"(depth %i exceeds limit %i)",
2614 pfx->teDepth, MagickMaxRecursionDepth);
2622 StartEleNdx = pfx->usedElements-1;
2623 if (StartEleNdx < 0) StartEleNdx = 0;
2632 if (strchr(strLimit,*pfx->pex)!=NULL) {
2633 *chLimit = *pfx->pex;
2640 if (!GetOperand (pfx, &UserSymbol, &NewUserSymbol, &UserSymNdx0, needPopAll))
return MagickFalse;
2645 while (*pfx->pex && (!*strLimit || (strchr(strLimit,*pfx->pex)==NULL))) {
2646 if (!GetOperator (pfx, &Assign, &Update, &IncrDecr))
return MagickFalse;
2648 if (NewUserSymbol && !Assign) {
2649 (void) ThrowMagickException (
2650 pfx->exception, GetMagickModule(), OptionError,
2651 "Expected assignment after new UserSymbol",
"'%s' at '%s'",
2652 pfx->token, SetShortExp(pfx));
2655 if (!UserSymbol && Assign) {
2656 (void) ThrowMagickException (
2657 pfx->exception, GetMagickModule(), OptionError,
2658 "Attempted assignment to non-UserSymbol",
"'%s' at '%s'",
2659 pfx->token, SetShortExp(pfx));
2662 if (!UserSymbol && Update) {
2663 (void) ThrowMagickException (
2664 pfx->exception, GetMagickModule(), OptionError,
2665 "Attempted update to non-UserSymbol",
"'%s' at '%s'",
2666 pfx->token, SetShortExp(pfx));
2669 if (UserSymbol && (Assign || Update) && !IncrDecr) {
2671 if (!TranslateExpression (pfx, strLimit, chLimit, needPopAll))
return MagickFalse;
2672 if (!*pfx->pex)
break;
2673 if (!*strLimit)
break;
2674 if (strchr(strLimit,*chLimit)!=NULL)
break;
2676 if (UserSymbol && !Assign && !Update && UserSymNdx0 != NULL_ADDRESS) {
2678 (void) AddAddressingElement (pfx, rCopyFrom, UserSymNdx0);
2679 UserSymNdx0 = NULL_ADDRESS;
2680 pel = &pfx->Elements[pfx->usedElements-1];
2681 pel->do_push = MagickTrue;
2685 while (TopOprIsUnaryPrefix (pfx)) {
2686 OperatorE op = pfx->OperatorStack[pfx->usedOprStack-1];
2687 (void) AddElement (pfx, (fxFltType) 0, (int) op);
2688 pfx->usedOprStack--;
2692 if (!ProcessTernaryOpr (pfx, &ternary))
return MagickFalse;
2694 if (ternary.addr_colon != NULL_ADDRESS) {
2695 if (!TranslateExpression (pfx,
",);", chLimit, needPopAll))
return MagickFalse;
2699 UserSymbol = NewUserSymbol = MagickFalse;
2701 if ( (!*pfx->pex) || (*strLimit && (strchr(strLimit,*pfx->pex)!=NULL) ) )
2703 if (IncrDecr)
break;
2705 (void) ThrowMagickException (
2706 pfx->exception, GetMagickModule(), OptionError,
2707 "Expected operand after operator",
"at '%s'",
2713 (void) ThrowMagickException (
2714 pfx->exception, GetMagickModule(), OptionError,
2715 "'++' and '--' must be the final operators in an expression at",
"'%s'",
2720 if (!GetOperand (pfx, &UserSymbol, &NewUserSymbol, &UserSymNdx1, needPopAll)) {
2721 (void) ThrowMagickException (
2722 pfx->exception, GetMagickModule(), OptionError,
2723 "Expected operand at",
"'%s'",
2728 if (NewUserSymbol && !Assign) {
2729 (void) ThrowMagickException (
2730 pfx->exception, GetMagickModule(), OptionError,
2731 "NewUserSymbol",
"'%s' after non-assignment operator at '%s'",
2732 pfx->token, SetShortExp(pfx));
2735 if (UserSymbol && !NewUserSymbol) {
2736 (void) AddAddressingElement (pfx, rCopyFrom, UserSymNdx1);
2737 UserSymNdx1 = NULL_ADDRESS;
2739 UserSymNdx0 = UserSymNdx1;
2742 if (UserSymbol && !Assign && !Update && UserSymNdx0 != NULL_ADDRESS) {
2744 if (NewUserSymbol) {
2745 (void) ThrowMagickException (
2746 pfx->exception, GetMagickModule(), OptionError,
2747 "NewUserSymbol",
"'%s' needs assignment operator at '%s'",
2748 pfx->token, SetShortExp(pfx));
2751 (void) AddAddressingElement (pfx, rCopyFrom, UserSymNdx0);
2752 pel = &pfx->Elements[pfx->usedElements-1];
2753 pel->do_push = MagickTrue;
2756 if (*pfx->pex && !*chLimit && (strchr(strLimit,*pfx->pex)!=NULL)) {
2757 *chLimit = *pfx->pex;
2760 while (pfx->usedOprStack) {
2761 OperatorE op = pfx->OperatorStack[pfx->usedOprStack-1];
2762 if (op == oOpenParen || op == oOpenBracket || op == oOpenBrace) {
2765 if ( (op==oAssign && !Assign) || (OprInPlace((
int) op) && !Update) ) {
2768 pfx->usedOprStack--;
2769 (void) AddElement (pfx, (fxFltType) 0, (int) op);
2770 if (op == oAssign) {
2771 if (UserSymNdx0 < 0) {
2772 (void) ThrowMagickException (
2773 pfx->exception, GetMagickModule(), OptionError,
2774 "Assignment to unknown user symbol at",
"'%s'",
2780 pfx->usedElements--;
2781 (void) AddAddressingElement (pfx, rCopyTo, UserSymNdx0);
2783 }
else if (OprInPlace ((
int) op)) {
2784 if (UserSymNdx0 < 0) {
2785 (void) ThrowMagickException (
2786 pfx->exception, GetMagickModule(), OptionError,
2787 "Operator-in-place to unknown user symbol at",
"'%s'",
2793 pfx->Elements[pfx->usedElements-1].element_index = UserSymNdx0;
2798 if (ternary.addr_query != NULL_ADDRESS) *needPopAll = MagickTrue;
2800 (void) ResolveTernaryAddresses (pfx, &ternary);
2804 if (!pfx->teDepth && *needPopAll) {
2805 (void) AddAddressingElement (pfx, rZerStk, NULL_ADDRESS);
2806 *needPopAll = MagickFalse;
2809 if (pfx->exception->severity >= ErrorException)
2816static MagickBooleanType TranslateStatement (
FxInfo * pfx,
char * strLimit,
char * chLimit)
2818 MagickBooleanType NeedPopAll = MagickFalse;
2822 if (!*pfx->pex)
return MagickFalse;
2824 if (!TranslateExpression (pfx, strLimit, chLimit, &NeedPopAll)) {
2827 if (pfx->usedElements && *chLimit==
';') {
2832 ElementT * pel = &pfx->Elements[pfx->usedElements-1];
2833 if (pel->do_push) pel->do_push = MagickFalse;
2839static MagickBooleanType TranslateStatementList (
FxInfo * pfx,
const char * strLimit,
char * chLimit)
2841#define MAX_SLIMIT 10
2842 char sLimits[MAX_SLIMIT];
2845 if (!*pfx->pex)
return MagickFalse;
2846 (void) CopyMagickString (sLimits, strLimit, MAX_SLIMIT-1);
2848 if (strchr(strLimit,
';')==NULL)
2849 (
void) ConcatenateMagickString (sLimits,
";", MAX_SLIMIT);
2852 if (!TranslateStatement (pfx, sLimits, chLimit))
return MagickFalse;
2854 if (!*pfx->pex)
break;
2856 if (*chLimit !=
';') {
2861 if (pfx->exception->severity >= ErrorException)
2880 for (ch=0; ch <= (int) MaxPixelChannels; ch++) {
2881 cs[ch].mean *= QuantumScale;
2882 cs[ch].median *= QuantumScale;
2883 cs[ch].maxima *= QuantumScale;
2884 cs[ch].minima *= QuantumScale;
2885 cs[ch].standard_deviation *= QuantumScale;
2891static MagickBooleanType CollectStatistics (
FxInfo * pfx)
2893 Image * img = GetFirstImageInList (pfx->image);
2898 if (!pfx->statistics) {
2899 (void) ThrowMagickException (
2900 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
2901 "Statistics",
"%lu",
2902 (
unsigned long) pfx->ImgListLen);
2907 pfx->statistics[imgNum] = CollectOneImgStats (pfx, img);
2909 if (++imgNum == pfx->ImgListLen)
break;
2910 img = GetNextImageInList (img);
2911 assert (img != (
Image *) NULL);
2913 pfx->GotStats = MagickTrue;
2918static inline MagickBooleanType PushVal (
FxInfo * pfx,
fxRtT * pfxrt, fxFltType val,
int addr)
2920 if (pfxrt->usedValStack >=pfxrt->numValStack) {
2921 (void) ThrowMagickException (
2922 pfx->exception, GetMagickModule(), OptionError,
2923 "ValStack overflow at addr=",
"%i",
2928 pfxrt->ValStack[pfxrt->usedValStack++] = val;
2932static inline fxFltType PopVal (
FxInfo * pfx,
fxRtT * pfxrt,
int addr)
2934 if (pfxrt->usedValStack <= 0) {
2935 (void) ThrowMagickException (
2936 pfx->exception, GetMagickModule(), OptionError,
2937 "ValStack underflow at addr=",
"%i",
2939 return (fxFltType) 0;
2942 return pfxrt->ValStack[--pfxrt->usedValStack];
2945static inline fxFltType ImageStat (
2946 FxInfo * pfx, ssize_t ImgNum, PixelChannel channel, ImgAttrE ia)
2950 MagickBooleanType NeedRelinq = MagickFalse;
2954 (void) ThrowMagickException(pfx->exception,GetMagickModule(),
2955 OptionError,
"NoSuchImage",
"%lu",(
unsigned long) ImgNum);
2959 if (pfx->GotStats) {
2960 if ((channel < 0) || (channel > MaxPixelChannels))
2962 (void) ThrowMagickException(pfx->exception,GetMagickModule(),
2963 OptionError,
"NoSuchImageChannel",
"%i",channel);
2964 channel=(PixelChannel) 0;
2966 cs = pfx->statistics[ImgNum];
2967 }
else if (pfx->NeedStats) {
2969 if ((channel < 0) || (channel > MaxPixelChannels))
2971 (void) ThrowMagickException(pfx->exception,GetMagickModule(),
2972 OptionError,
"NoSuchImageChannel",
"%i",channel);
2973 channel=(PixelChannel) 0;
2975 cs = CollectOneImgStats (pfx, pfx->Images[ImgNum]);
2976 NeedRelinq = MagickTrue;
2981 ret = (fxFltType) GetImageDepth (pfx->Images[ImgNum], pfx->exception);
2984 ret = (fxFltType) GetBlobSize (pfx->image);
2988 ret = cs[channel].kurtosis;
2992 ret = cs[channel].maxima;
2996 ret = cs[channel].mean;
3000 ret = cs[channel].median;
3004 ret = cs[channel].minima;
3010 ret = (fxFltType) pfx->Images[ImgNum]->page.x;
3013 ret = (fxFltType) pfx->Images[ImgNum]->page.y;
3016 ret = (fxFltType) pfx->Images[ImgNum]->page.width;
3019 ret = (fxFltType) pfx->Images[ImgNum]->page.height;
3025 ret = (fxFltType) PerceptibleReciprocal (pfx->Images[ImgNum]->resolution.x)
3026 * pfx->Images[ImgNum]->columns;
3029 ret = (fxFltType) PerceptibleReciprocal (pfx->Images[ImgNum]->resolution.y)
3030 * pfx->Images[ImgNum]->rows;
3033 ret = (fxFltType) pfx->Images[ImgNum]->quality;
3039 ret = pfx->Images[ImgNum]->resolution.x;
3042 ret = pfx->Images[ImgNum]->resolution.y;
3046 ret = cs[channel].skewness;
3050 ret = cs[channel].standard_deviation;
3053 ret = (fxFltType) pfx->Images[ImgNum]->rows;
3056 ret = (fxFltType) pfx->ImgListLen;
3059 ret = (fxFltType) ImgNum;
3062 ret = (fxFltType) pfx->Images[ImgNum]->columns;
3065 ret = (fxFltType) GetImageDepth (pfx->Images[ImgNum], pfx->exception);
3068 (void) ThrowMagickException (pfx->exception,GetMagickModule(),OptionError,
3069 "Unknown ia=",
"%i",ia);
3076static inline fxFltType FxGcd (fxFltType x, fxFltType y,
const size_t depth)
3078#define FxMaxFunctionDepth 200
3081 return (FxGcd (y, x, depth+1));
3082 if ((fabs((
double) y) < 0.001) || (depth >= FxMaxFunctionDepth))
3084 return (FxGcd (y, x-y*floor((
double) (x/y)), depth+1));
3087static inline ssize_t ChkImgNum (
FxInfo * pfx, fxFltType f)
3090 ssize_t i = (ssize_t) floor ((
double) f + 0.5);
3091 if (i < 0) i += (ssize_t) pfx->ImgListLen;
3092 if (i < 0 || i >= (ssize_t) pfx->ImgListLen) {
3093 (void) ThrowMagickException (
3094 pfx->exception, GetMagickModule(), OptionError,
3095 "ImgNum",
"%lu bad for ImgListLen %lu",
3096 (
unsigned long) i, (
unsigned long) pfx->ImgListLen);
3102#define WHICH_ATTR_CHAN \
3103 (pel->channel_qual == NO_CHAN_QUAL) ? CompositePixelChannel : \
3104 (pel->channel_qual == THIS_CHANNEL) ? channel : pel->channel_qual
3106#define WHICH_NON_ATTR_CHAN \
3107 (pel->channel_qual == NO_CHAN_QUAL || \
3108 pel->channel_qual == THIS_CHANNEL || \
3109 pel->channel_qual == CompositePixelChannel \
3110 ) ? (channel == CompositePixelChannel ? RedPixelChannel: channel) \
3113static fxFltType GetHslFlt (
FxInfo * pfx, ssize_t ImgNum,
const fxFltType fx,
const fxFltType fy,
3114 PixelChannel channel)
3116 Image * img = pfx->Images[ImgNum];
3118 double red, green, blue;
3119 double hue=0, saturation=0, lightness=0;
3121 MagickBooleanType okay = MagickTrue;
3122 if(!InterpolatePixelChannel (img, pfx->Imgs[ImgNum].View, RedPixelChannel, img->interpolate,
3123 (
double) fx, (
double) fy, &red, pfx->exception)) okay = MagickFalse;
3124 if(!InterpolatePixelChannel (img, pfx->Imgs[ImgNum].View, GreenPixelChannel, img->interpolate,
3125 (
double) fx, (
double) fy, &green, pfx->exception)) okay = MagickFalse;
3126 if(!InterpolatePixelChannel (img, pfx->Imgs[ImgNum].View, BluePixelChannel, img->interpolate,
3127 (
double) fx, (
double) fy, &blue, pfx->exception)) okay = MagickFalse;
3130 (void) ThrowMagickException (
3131 pfx->exception, GetMagickModule(), OptionError,
3132 "GetHslFlt failure",
"%lu %g,%g %i", (
unsigned long) ImgNum,
3133 (
double) fx, (double) fy, channel);
3137 &hue, &saturation, &lightness);
3139 if (channel == HUE_CHANNEL)
return hue;
3140 if (channel == SAT_CHANNEL)
return saturation;
3141 if (channel == LIGHT_CHANNEL)
return lightness;
3146static fxFltType GetHslInt (
FxInfo * pfx, ssize_t ImgNum,
const ssize_t imgx,
const ssize_t imgy, PixelChannel channel)
3148 Image * img = pfx->Images[ImgNum];
3150 double hue=0, saturation=0, lightness=0;
3152 const Quantum * p = GetCacheViewVirtualPixels (pfx->Imgs[ImgNum].View, imgx, imgy, 1, 1, pfx->exception);
3153 if (p == (
const Quantum *) NULL)
3155 (void) ThrowMagickException (pfx->exception,GetMagickModule(),
3156 OptionError,
"GetHslInt failure",
"%lu %li,%li %i",(
unsigned long) ImgNum,
3157 (
long) imgx,(long) imgy,channel);
3162 GetPixelRed (img, p), GetPixelGreen (img, p), GetPixelBlue (img, p),
3163 &hue, &saturation, &lightness);
3165 if (channel == HUE_CHANNEL)
return hue;
3166 if (channel == SAT_CHANNEL)
return saturation;
3167 if (channel == LIGHT_CHANNEL)
return lightness;
3172static inline fxFltType GetIntensity (
FxInfo * pfx, ssize_t ImgNum,
const fxFltType fx,
const fxFltType fy)
3175 quantum_pixel[MaxPixelChannels];
3180 Image * img = pfx->Images[ImgNum];
3182 (void) GetPixelInfo (img, &pixelinf);
3184 if (!InterpolatePixelInfo (img, pfx->Imgs[pfx->ImgNum].View, img->interpolate,
3185 (
double) fx, (
double) fy, &pixelinf, pfx->exception))
3187 (void) ThrowMagickException (
3188 pfx->exception, GetMagickModule(), OptionError,
3189 "GetIntensity failure",
"%lu %g,%g", (
unsigned long) ImgNum,
3190 (
double) fx, (double) fy);
3193 SetPixelViaPixelInfo (img, &pixelinf, quantum_pixel);
3194 return QuantumScale * GetPixelIntensity (img, quantum_pixel);
3197static MagickBooleanType ExecuteRPN (
FxInfo * pfx,
fxRtT * pfxrt, fxFltType *result,
3198 const PixelChannel channel,
const ssize_t imgx,
const ssize_t imgy)
3200 const Quantum * p = pfxrt->thisPixel;
3201 fxFltType regA=0, regB=0, regC=0, regD=0, regE=0;
3202 Image * img = pfx->image;
3204 MagickBooleanType NeedRelinq = MagickFalse;
3205 double hue=0, saturation=0, lightness=0;
3212 if (!p) p = GetCacheViewVirtualPixels (
3213 pfx->Imgs[pfx->ImgNum].View, imgx, imgy, 1, 1, pfx->exception);
3215 if (p == (
const Quantum *) NULL)
3217 (void) ThrowMagickException (pfx->exception,GetMagickModule(),
3218 OptionError,
"Can't get virtual pixels",
"%lu %li,%li",(
unsigned long)
3219 pfx->ImgNum,(
long) imgx,(long) imgy);
3220 return(MagickFalse);
3223 if (pfx->GotStats) {
3224 cs = pfx->statistics[pfx->ImgNum];
3225 }
else if (pfx->NeedStats) {
3226 cs = CollectOneImgStats (pfx, pfx->Images[pfx->ImgNum]);
3227 NeedRelinq = MagickTrue;
3234 GetPixelRed (img, p), GetPixelGreen (img, p), GetPixelBlue (img, p),
3235 &hue, &saturation, &lightness);
3238 for (i=0; i < pfx->usedElements; i++) {
3243 (void) ThrowMagickException (
3244 pfx->exception, GetMagickModule(), OptionError,
3245 "Bad run-time address",
"%i", i);
3247 pel=&pfx->Elements[i];
3248 switch (pel->number_args) {
3252 regA = PopVal (pfx, pfxrt, i);
3255 regB = PopVal (pfx, pfxrt, i);
3256 regA = PopVal (pfx, pfxrt, i);
3259 regC = PopVal (pfx, pfxrt, i);
3260 regB = PopVal (pfx, pfxrt, i);
3261 regA = PopVal (pfx, pfxrt, i);
3264 regD = PopVal (pfx, pfxrt, i);
3265 regC = PopVal (pfx, pfxrt, i);
3266 regB = PopVal (pfx, pfxrt, i);
3267 regA = PopVal (pfx, pfxrt, i);
3270 regE = PopVal (pfx, pfxrt, i);
3271 regD = PopVal (pfx, pfxrt, i);
3272 regC = PopVal (pfx, pfxrt, i);
3273 regB = PopVal (pfx, pfxrt, i);
3274 regA = PopVal (pfx, pfxrt, i);
3277 (void) ThrowMagickException (
3278 pfx->exception, GetMagickModule(), OptionError,
3279 "Too many args:",
"%i", pel->number_args);
3283 switch (pel->operator_index) {
3285 regA = (pfxrt->UserSymVals[pel->element_index] += regA);
3288 regA = (pfxrt->UserSymVals[pel->element_index] -= regA);
3291 regA = (pfxrt->UserSymVals[pel->element_index] *= regA);
3294 regA = (pfxrt->UserSymVals[pel->element_index] *= PerceptibleReciprocal((
double)regA));
3297 regA = pfxrt->UserSymVals[pel->element_index]++;
3300 regA = pfxrt->UserSymVals[pel->element_index]--;
3312 regA *= PerceptibleReciprocal((
double)regB);
3315 regA = fmod ((
double) regA, fabs(floor((
double) regB+0.5)));
3324 if ((
size_t) (regB+0.5) >= (8*
sizeof(
size_t)))
3326 (void) ThrowMagickException ( pfx->exception, GetMagickModule(),
3327 OptionError,
"undefined shift",
"%g", (double) regB);
3328 regA = (fxFltType) 0.0;
3331 regA = (fxFltType) ((
size_t)(regA+0.5) << (
size_t)(regB+0.5));
3334 if ((
size_t) (regB+0.5) >= (8*
sizeof(
size_t)))
3336 (void) ThrowMagickException ( pfx->exception, GetMagickModule(),
3337 OptionError,
"undefined shift",
"%g", (double) regB);
3338 regA = (fxFltType) 0.0;
3341 regA = (fxFltType) ((
size_t)(regA+0.5) >> (
size_t)(regB+0.5));
3344 regA = fabs((
double) (regA-regB)) < MagickEpsilon ? 1.0 : 0.0;
3347 regA = fabs((
double) (regA-regB)) >= MagickEpsilon ? 1.0 : 0.0;
3350 regA = (regA <= regB) ? 1.0 : 0.0;
3353 regA = (regA >= regB) ? 1.0 : 0.0;
3356 regA = (regA < regB) ? 1.0 : 0.0;
3359 regA = (regA > regB) ? 1.0 : 0.0;
3362 regA = (regA<=0) ? 0.0 : (regB > 0) ? 1.0 : 0.0;
3365 regA = (regA>0) ? 1.0 : (regB > 0.0) ? 1.0 : 0.0;
3368 regA = (regA==0) ? 1.0 : 0.0;
3371 regA = (fxFltType) ((
size_t)(regA+0.5) & (
size_t)(regB+0.5));
3374 regA = (fxFltType) ((
size_t)(regA+0.5) | (
size_t)(regB+0.5));
3378 regA = (fxFltType) (~(
size_t)(regA+0.5));
3381 regA = pow ((
double) regA, (
double) regB);
3397 if (pel->type == etColourConstant) {
3398 switch (channel) {
default:
3399 case (PixelChannel) 0:
3402 case (PixelChannel) 1:
3405 case (PixelChannel) 2:
3415 regA = fabs ((
double) regA);
3417#if defined(MAGICKCORE_HAVE_ACOSH)
3419 regA = acosh ((
double) regA);
3423 regA = acos ((
double) regA);
3425#if defined(MAGICKCORE_HAVE_J1)
3427 if (regA==0) regA = 1.0;
3429 fxFltType gamma = 2.0 * j1 ((MagickPI*regA)) / (MagickPI*regA);
3430 regA = gamma * gamma;
3435 regA = (fxFltType) (((ssize_t) regA) & 0x01 ? -1.0 : 1.0);
3437#if defined(MAGICKCORE_HAVE_ASINH)
3439 regA = asinh ((
double) regA);
3443 regA = asin ((
double) regA);
3445#if defined(MAGICKCORE_HAVE_ATANH)
3447 regA = atanh ((
double) regA);
3451 regA = atan2 ((
double) regA, (
double) regB);
3454 regA = atan ((
double) regA);
3457 regA = ceil ((
double) regA);
3461 case (PixelChannel) 0:
break;
3462 case (PixelChannel) 1: regA = regB;
break;
3463 case (PixelChannel) 2: regA = regC;
break;
3464 case (PixelChannel) 3: regA = regD;
break;
3465 case (PixelChannel) 4: regA = regE;
break;
3466 default: regA = 0.0;
3470 if (regA < 0) regA = 0.0;
3471 else if (regA > 1.0) regA = 1.0;
3474 regA = cosh ((
double) regA);
3477 regA = cos ((
double) regA);
3482 (void) fprintf (stderr,
"%s[%g,%g].[%i]: %s=%.*g\n",
3483 img->filename, (
double) imgx, (double) imgy,
3484 channel, SetPtrShortExp (pfx, pel->exp_start, (
size_t) (pel->exp_len+1)),
3485 pfx->precision, (double) regA);
3488 regA = regA / (regB*(regA-1.0) + 1.0);
3490#if defined(MAGICKCORE_HAVE_ERF)
3492 regA = erf ((
double) regA);
3496 regA = exp ((
double) regA);
3499 regA = floor ((
double) regA);
3502 regA = exp((
double) (-regA*regA/2.0))/sqrt(2.0*MagickPI);
3506 regA = FxGcd (regA, regB, 0);
3509 regA = hypot ((
double) regA, (
double) regB);
3512 regA = floor ((
double) regA);
3515 regA = (fxFltType) (!!IsNaN (regA));
3517#if defined(MAGICKCORE_HAVE_J0)
3519 regA = j0 ((
double) regA);
3522#if defined(MAGICKCORE_HAVE_J1)
3524 regA = j1 ((
double) regA);
3527#if defined(MAGICKCORE_HAVE_J1)
3529 if (regA==0) regA = 1.0;
3530 else regA = 2.0 * j1 ((MagickPI*regA))/(MagickPI*regA);
3534 regA = log ((
double) regA);
3537 regA = MagickLog10((
double) regA) / log10(2.0);
3540 regA = MagickLog10 ((
double) regA);
3543 regA = (regA > regB) ? regA : regB;
3546 regA = (regA < regB) ? regA : regB;
3549 regA = regA - floor((
double) (regA*PerceptibleReciprocal((
double) regB)))*regB;
3552 regA = (fxFltType) (regA < MagickEpsilon);
3555 regA = pow ((
double) regA, (
double) regB);
3558#if defined(MAGICKCORE_OPENMP_SUPPORT)
3559 #pragma omp critical (MagickCore_ExecuteRPN)
3561 regA = GetPseudoRandomValue (pfxrt->random_info);
3565 regA = floor ((
double) regA + 0.5);
3568 regA = (regA < 0) ? -1.0 : 1.0;
3571 regA = sin ((
double) (MagickPI*regA)) / (MagickPI*regA);
3574 regA = sinh ((
double) regA);
3577 regA = sin ((
double) regA);
3580 regA = sqrt ((
double) regA);
3583 regA = 1.0 / (1.0 + exp ((
double) -regA));
3586 regA = tanh ((
double) regA);
3589 regA = tan ((
double) regA);
3592 if (regA >= 0) regA = floor ((
double) regA);
3593 else regA = ceil ((
double) regA);
3605 ssize_t ImgNum = ChkImgNum (pfx, regA);
3606 if (ImgNum < 0)
break;
3607 regA = (fxFltType) 0;
3609 Image * pimg = pfx->Images[0];
3610 if (pel->img_attr_qual == aNull) {
3611 if ((
int) pel->channel_qual < 0) {
3612 if (pel->channel_qual == NO_CHAN_QUAL || pel->channel_qual == THIS_CHANNEL) {
3613 if (pfx->ImgNum==0) {
3614 regA = QuantumScale * (double) p[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3616 const Quantum * pv = GetCacheViewVirtualPixels (
3617 pfx->Imgs[0].View, imgx, imgy, 1,1, pfx->exception);
3619 (void) ThrowMagickException (
3620 pfx->exception, GetMagickModule(), OptionError,
3621 "fU can't get cache",
"%lu", (
unsigned long) ImgNum);
3624 regA = QuantumScale * (double) pv[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3626 }
else if (pel->channel_qual == HUE_CHANNEL || pel->channel_qual == SAT_CHANNEL ||
3627 pel->channel_qual == LIGHT_CHANNEL) {
3628 regA = GetHslInt (pfx, ImgNum, imgx, imgy, pel->channel_qual);
3630 }
else if (pel->channel_qual == INTENSITY_CHANNEL) {
3631 regA = GetIntensity (pfx, 0, (
double) imgx, (
double) imgy);
3635 if (pfx->ImgNum==0) {
3636 regA = QuantumScale * (double) p[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3638 const Quantum * pv = GetCacheViewVirtualPixels (
3639 pfx->Imgs[0].View, imgx, imgy, 1,1, pfx->exception);
3641 (void) ThrowMagickException (
3642 pfx->exception, GetMagickModule(), OptionError,
3643 "fU can't get cache",
"%lu", (
unsigned long) ImgNum);
3646 regA = QuantumScale * (double) pv[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3651 regA = ImageStat (pfx, 0, WHICH_ATTR_CHAN, pel->img_attr_qual);
3655 if (pel->img_attr_qual == aNull) {
3657 if ((
int) pel->channel_qual < 0) {
3658 if (pel->channel_qual == HUE_CHANNEL || pel->channel_qual == SAT_CHANNEL ||
3659 pel->channel_qual == LIGHT_CHANNEL)
3661 regA = GetHslInt (pfx, ImgNum, imgx, imgy, pel->channel_qual);
3663 }
else if (pel->channel_qual == INTENSITY_CHANNEL)
3665 regA = GetIntensity (pfx, ImgNum, (fxFltType) imgx, (fxFltType) imgy);
3670 pv = GetCacheViewVirtualPixels (
3671 pfx->Imgs[ImgNum].View, imgx, imgy, 1,1, pfx->exception);
3673 (void) ThrowMagickException (
3674 pfx->exception, GetMagickModule(), OptionError,
3675 "fU can't get cache",
"%lu", (
unsigned long) ImgNum);
3678 regA = QuantumScale * (double)
3679 pv[pfx->Images[ImgNum]->channel_map[WHICH_NON_ATTR_CHAN].offset];
3681 regA = ImageStat (pfx, ImgNum, WHICH_ATTR_CHAN, pel->img_attr_qual);
3690 Image * pimg = pfx->Images[0];
3691 if ((
int) pel->channel_qual < 0) {
3692 if (pel->channel_qual == NO_CHAN_QUAL || pel->channel_qual == THIS_CHANNEL) {
3694 if (pfx->ImgNum==0) {
3695 regA = QuantumScale * (double) p[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3697 const Quantum * pv = GetCacheViewVirtualPixels (
3698 pfx->Imgs[0].View, imgx, imgy, 1,1, pfx->exception);
3700 (void) ThrowMagickException (
3701 pfx->exception, GetMagickModule(), OptionError,
3702 "fU0 can't get cache",
"%i", 0);
3705 regA = QuantumScale * (double) pv[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3708 }
else if (pel->channel_qual == HUE_CHANNEL || pel->channel_qual == SAT_CHANNEL ||
3709 pel->channel_qual == LIGHT_CHANNEL) {
3710 regA = GetHslInt (pfx, 0, imgx, imgy, pel->channel_qual);
3712 }
else if (pel->channel_qual == INTENSITY_CHANNEL) {
3713 regA = GetIntensity (pfx, 0, (fxFltType) imgx, (fxFltType) imgy);
3716 if (pfx->ImgNum==0) {
3717 regA = QuantumScale * (double) p[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3719 const Quantum * pv = GetCacheViewVirtualPixels (
3720 pfx->Imgs[0].View, imgx, imgy, 1,1, pfx->exception);
3722 (void) ThrowMagickException (
3723 pfx->exception, GetMagickModule(), OptionError,
3724 "fU0 can't get cache",
"%i", 0);
3727 regA = QuantumScale * (double) pv[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3734 ssize_t ImgNum = ChkImgNum (pfx, regA);
3737 if (ImgNum < 0)
break;
3739 if (pel->is_relative) {
3747 if ((
int) pel->channel_qual < 0) {
3748 if (pel->channel_qual == HUE_CHANNEL || pel->channel_qual == SAT_CHANNEL
3749 || pel->channel_qual == LIGHT_CHANNEL) {
3750 regA = GetHslFlt (pfx, ImgNum, fx, fy, pel->channel_qual);
3752 }
else if (pel->channel_qual == INTENSITY_CHANNEL) {
3753 regA = GetIntensity (pfx, ImgNum, fx, fy);
3760 Image * imUP = pfx->Images[ImgNum];
3761 if (! InterpolatePixelChannel (imUP, pfx->Imgs[ImgNum].View, WHICH_NON_ATTR_CHAN,
3762 imUP->interpolate, (
double) fx, (
double) fy, &v, pfx->exception))
3764 (void) ThrowMagickException (
3765 pfx->exception, GetMagickModule(), OptionError,
3766 "fUP can't get interpolate",
"%lu", (
unsigned long) ImgNum);
3769 regA = v * QuantumScale;
3778 if (pel->operator_index == fS) ImgNum = pfx->ImgNum;
3780 if (pel->img_attr_qual == aNull) {
3781 const Quantum * pv = GetCacheViewVirtualPixels (
3782 pfx->Imgs[ImgNum].View, imgx, imgy, 1,1, pfx->exception);
3784 (void) ThrowMagickException (
3785 pfx->exception, GetMagickModule(), OptionError,
3786 "fV can't get cache",
"%lu", (
unsigned long) ImgNum);
3790 if ((
int) pel->channel_qual < 0) {
3791 if (pel->channel_qual == HUE_CHANNEL || pel->channel_qual == SAT_CHANNEL ||
3792 pel->channel_qual == LIGHT_CHANNEL) {
3793 regA = GetHslInt (pfx, ImgNum, imgx, imgy, pel->channel_qual);
3795 }
else if (pel->channel_qual == INTENSITY_CHANNEL) {
3796 regA = GetIntensity (pfx, ImgNum, (
double) imgx, (
double) imgy);
3801 regA = QuantumScale * (double)
3802 pv[pfx->Images[ImgNum]->channel_map[WHICH_NON_ATTR_CHAN].offset];
3804 regA = ImageStat (pfx, ImgNum, WHICH_ATTR_CHAN, pel->img_attr_qual);
3814 ssize_t ImgNum = pfx->ImgNum;
3815 if (pel->operator_index == fVP) ImgNum = 1;
3816 if (pel->is_relative) {
3823 if ((
int) pel->channel_qual < 0) {
3824 if (pel->channel_qual == HUE_CHANNEL || pel->channel_qual == SAT_CHANNEL ||
3825 pel->channel_qual == LIGHT_CHANNEL) {
3826 regA = GetHslFlt (pfx, ImgNum, fx, fy, pel->channel_qual);
3828 }
else if (pel->channel_qual == INTENSITY_CHANNEL) {
3829 regA = GetIntensity (pfx, ImgNum, fx, fy);
3837 if (! InterpolatePixelChannel (pfx->Images[ImgNum], pfx->Imgs[ImgNum].View,
3838 WHICH_NON_ATTR_CHAN, pfx->Images[ImgNum]->interpolate,
3839 (
double) fx, (
double) fy, &v, pfx->exception)
3842 (void) ThrowMagickException (
3843 pfx->exception, GetMagickModule(), OptionError,
3844 "fSP or fVP can't get interp",
"%lu", (
unsigned long) ImgNum);
3847 regA = v * (fxFltType)QuantumScale;
3855 regA = (fxFltType) GetImageDepth (img, pfx->exception);
3858 regA = (fxFltType) img->extent;
3862 regA = cs[WHICH_ATTR_CHAN].kurtosis;
3866 regA = cs[WHICH_ATTR_CHAN].maxima;
3870 regA = cs[WHICH_ATTR_CHAN].mean;
3874 regA = cs[WHICH_ATTR_CHAN].median;
3878 regA = cs[WHICH_ATTR_CHAN].minima;
3883 regA = (fxFltType) img->page.x;
3886 regA = (fxFltType) img->page.y;
3889 regA = (fxFltType) img->page.width;
3892 regA = (fxFltType) img->page.height;
3897 regA = (fxFltType) PerceptibleReciprocal (img->resolution.x) * img->columns;
3900 regA = (fxFltType) PerceptibleReciprocal (img->resolution.y) * img->rows;
3903 regA = (fxFltType) img->quality;
3908 regA = (fxFltType) img->resolution.x;
3911 regA = (fxFltType) img->resolution.y;
3915 regA = cs[WHICH_ATTR_CHAN].skewness;
3919 regA = cs[WHICH_ATTR_CHAN].standard_deviation;
3922 regA = (fxFltType) img->rows;
3925 regA = (fxFltType) pfx->ImgListLen;
3928 regA = (fxFltType) pfx->ImgNum;
3931 regA = (fxFltType) img->columns;
3934 regA = (fxFltType) GetImageDepth (img, pfx->exception);
3942 regA = GetIntensity (pfx, pfx->ImgNum, (
double) imgx, (
double) imgy);
3949 regA = QuantumScale * (0.212656 * (double) GetPixelRed (img,p) +
3950 0.715158 * (double) GetPixelGreen (img,p) +
3951 0.072186 * (double) GetPixelBlue (img,p));
3957 regA = QuantumScale * (double) GetPixelAlpha (img, p);
3960 regA = QuantumScale * (double) GetPixelBlue (img, p);
3963 regA = QuantumScale * (double) GetPixelCyan (img, p);
3966 regA = QuantumScale * (double) GetPixelGreen (img, p);
3969 regA = (fxFltType) imgx;
3972 regA = (fxFltType) imgy;
3975 regA = QuantumScale * (double) GetPixelBlack (img, p);
3978 regA = QuantumScale * (double) GetPixelGreen (img, p);
3981 regA = QuantumScale * (double) GetPixelAlpha (img, p);
3984 regA = QuantumScale * (double) GetPixelRed (img, p);
3987 regA = QuantumScale * (double) GetPixelYellow (img, p);
3993 assert (pel->element_index >= 0);
3994 i = pel->element_index-1;
3997 assert (pel->element_index >= 0);
3998 i = pel->element_index-1;
3999 if (IsImageTTLExpired(img) != MagickFalse) {
4000 i = pfx->usedElements-1;
4001 (void) ThrowMagickException (pfx->exception, GetMagickModule(),
4002 ResourceLimitFatalError,
"TimeLimitExceeded",
"`%s'", img->filename);
4006 assert (pel->element_index >= 0);
4007 if (fabs((
double) regA) < MagickEpsilon) i = pel->element_index-1;
4009 case rIfNotZeroGoto:
4010 assert (pel->element_index >= 0);
4011 if (fabs((
double) regA) > MagickEpsilon) i = pel->element_index-1;
4014 assert (pel->element_index >= 0);
4015 regA = pfxrt->UserSymVals[pel->element_index];
4018 assert (pel->element_index >= 0);
4019 pfxrt->UserSymVals[pel->element_index] = regA;
4022 pfxrt->usedValStack = 0;
4028 (void) ThrowMagickException (
4029 pfx->exception, GetMagickModule(), OptionError,
4030 "pel->oprNum",
"%i '%s' not yet implemented",
4031 (int)pel->operator_index, OprStr(pel->operator_index));
4035 if (!PushVal (pfx, pfxrt, regA, i))
break;
4038 if (pfxrt->usedValStack > 0) regA = PopVal (pfx, pfxrt, 9999);
4044 if (pfx->exception->severity >= ErrorException)
4047 if (pfxrt->usedValStack != 0) {
4048 (void) ThrowMagickException (
4049 pfx->exception, GetMagickModule(), OptionError,
4050 "ValStack not empty",
"(%i)", pfxrt->usedValStack);
4059MagickPrivate MagickBooleanType FxEvaluateChannelExpression (
4061 const PixelChannel channel,
const ssize_t x,
const ssize_t y,
4065 id = GetOpenMPThreadId();
4069 assert (pfx != NULL);
4070 assert (pfx->image != NULL);
4071 assert (pfx->Images != NULL);
4072 assert (pfx->Imgs != NULL);
4073 assert (pfx->fxrts != NULL);
4075 pfx->fxrts[id].thisPixel = NULL;
4077 if (!ExecuteRPN (pfx, &pfx->fxrts[
id], &ret, channel, x, y)) {
4078 (void) ThrowMagickException (
4079 exception, GetMagickModule(), OptionError,
4080 "ExecuteRPN failed",
" ");
4084 *result = (double) ret;
4089static FxInfo *AcquireFxInfoPrivate (
const Image * images,
const char * expression,
4094 FxInfo * pfx = (
FxInfo*) AcquireCriticalMemory (
sizeof (*pfx));
4096 memset (pfx, 0,
sizeof (*pfx));
4098 if (!InitFx (pfx, images, CalcAllStats, exception)) {
4099 pfx = (
FxInfo*) RelinquishMagickMemory(pfx);
4103 if (!BuildRPN (pfx)) {
4104 (void) DeInitFx (pfx);
4105 pfx = (
FxInfo*) RelinquishMagickMemory(pfx);
4109 if ((*expression ==
'@') && (strlen(expression) > 1))
4110 pfx->expression=FileToString(expression,~0UL,exception);
4111 if (pfx->expression == (
char *) NULL)
4112 pfx->expression=ConstantString(expression);
4113 pfx->pex = (
char *) pfx->expression;
4116 if (!TranslateStatementList (pfx,
";", &chLimit)) {
4117 (void) DestroyRPN (pfx);
4118 pfx->expression = DestroyString (pfx->expression);
4120 (void) DeInitFx (pfx);
4121 pfx = (
FxInfo*) RelinquishMagickMemory(pfx);
4126 (void) ThrowMagickException (
4127 pfx->exception, GetMagickModule(), OptionError,
4128 "Translate expression depth",
"(%i) not 0",
4131 (void) DestroyRPN (pfx);
4132 pfx->expression = DestroyString (pfx->expression);
4134 (void) DeInitFx (pfx);
4135 pfx = (
FxInfo*) RelinquishMagickMemory(pfx);
4139 if (chLimit !=
'\0' && chLimit !=
';') {
4140 (void) ThrowMagickException (
4141 pfx->exception, GetMagickModule(), OptionError,
4142 "AcquireFxInfo: TranslateExpression did not exhaust input",
"(chLimit=%i) at'%s'",
4143 (int)chLimit, pfx->pex);
4145 (void) DestroyRPN (pfx);
4146 pfx->expression = DestroyString (pfx->expression);
4148 (void) DeInitFx (pfx);
4149 pfx = (
FxInfo*) RelinquishMagickMemory(pfx);
4153 if (pfx->NeedStats && pfx->runType == rtEntireImage && !pfx->statistics) {
4154 if (!CollectStatistics (pfx)) {
4155 (void) DestroyRPN (pfx);
4156 pfx->expression = DestroyString (pfx->expression);
4158 (void) DeInitFx (pfx);
4159 pfx = (
FxInfo*) RelinquishMagickMemory(pfx);
4164 if (pfx->DebugOpt) {
4165 DumpTables (stderr);
4166 DumpUserSymbols (pfx, stderr);
4167 (void) DumpRPN (pfx, stderr);
4171 size_t number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
4174 pfx->fxrts = (
fxRtT *)AcquireQuantumMemory (number_threads,
sizeof(
fxRtT));
4176 (void) ThrowMagickException (
4177 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
4179 (
unsigned long) number_threads);
4180 (void) DestroyRPN (pfx);
4181 pfx->expression = DestroyString (pfx->expression);
4183 (void) DeInitFx (pfx);
4184 pfx = (
FxInfo*) RelinquishMagickMemory(pfx);
4187 for (t=0; t < (ssize_t) number_threads; t++) {
4188 if (!AllocFxRt (pfx, &pfx->fxrts[t])) {
4189 (void) ThrowMagickException (
4190 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
4191 "AllocFxRt t=",
"%g",
4195 for (t2 = t-1; t2 >= 0; t2--) {
4196 DestroyFxRt (&pfx->fxrts[t]);
4199 pfx->fxrts = (
fxRtT *) RelinquishMagickMemory (pfx->fxrts);
4200 (void) DestroyRPN (pfx);
4201 pfx->expression = DestroyString (pfx->expression);
4203 (void) DeInitFx (pfx);
4204 pfx = (
FxInfo*) RelinquishMagickMemory(pfx);
4214 return AcquireFxInfoPrivate (images, expression, MagickFalse, exception);
4221 assert (pfx != NULL);
4222 assert (pfx->image != NULL);
4223 assert (pfx->Images != NULL);
4224 assert (pfx->Imgs != NULL);
4225 assert (pfx->fxrts != NULL);
4227 for (t=0; t < (ssize_t) GetMagickResourceLimit(ThreadResource); t++) {
4228 DestroyFxRt (&pfx->fxrts[t]);
4230 pfx->fxrts = (
fxRtT *) RelinquishMagickMemory (pfx->fxrts);
4234 pfx->expression = DestroyString (pfx->expression);
4237 (void) DeInitFx (pfx);
4239 pfx = (
FxInfo*) RelinquishMagickMemory(pfx);
4246MagickExport
Image *FxImage(
const Image *image,
const char *expression,
4249#define FxImageTag "FxNew/Image"
4270 assert(image != (
Image *) NULL);
4271 assert(image->signature == MagickCoreSignature);
4272 if (IsEventLogging() != MagickFalse)
4273 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4274 if (expression == (
const char *) NULL)
4275 return(CloneImage(image,0,0,MagickTrue,exception));
4276 fx_image=CloneImage(image,0,0,MagickTrue,exception);
4277 if (!fx_image)
return NULL;
4278 if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse) {
4279 fx_image=DestroyImage(fx_image);
4283 pfx = AcquireFxInfoPrivate (image, expression, MagickTrue, exception);
4286 fx_image=DestroyImage(fx_image);
4290 assert (pfx->image != NULL);
4291 assert (pfx->Images != NULL);
4292 assert (pfx->Imgs != NULL);
4293 assert (pfx->fxrts != NULL);
4297 image_view = AcquireVirtualCacheView (image, pfx->exception);
4298 fx_view = AcquireAuthenticCacheView (fx_image, pfx->exception);
4299#if defined(MAGICKCORE_OPENMP_SUPPORT)
4300 #pragma omp parallel for schedule(dynamic) shared(progress,status) \
4301 magick_number_threads(image,fx_image,fx_image->rows, \
4302 pfx->ContainsDebug ? 0 : 1)
4304 for (y=0; y < (ssize_t) fx_image->rows; y++)
4307 id = GetOpenMPThreadId();
4321 if (status == MagickFalse)
4323 p = GetCacheViewVirtualPixels (image_view, 0, y, image->columns, 1, pfx->exception);
4324 q = QueueCacheViewAuthenticPixels (fx_view, 0, y, fx_image->columns, 1, pfx->exception);
4325 if ((p == (
const Quantum *) NULL) || (q == (Quantum *) NULL)) {
4329 for (x=0; x < (ssize_t) fx_image->columns; x++) {
4332 pfx->fxrts[id].thisPixel = (Quantum *)p;
4334 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4336 PixelChannel channel = GetPixelChannelChannel (image, i);
4337 PixelTrait traits = GetPixelChannelTraits (image, channel);
4338 PixelTrait fx_traits = GetPixelChannelTraits (fx_image, channel);
4339 if ((traits == UndefinedPixelTrait) ||
4340 (fx_traits == UndefinedPixelTrait))
4342 if ((fx_traits & CopyPixelTrait) != 0) {
4343 SetPixelChannel (fx_image, channel, p[i], q);
4347 if (!ExecuteRPN (pfx, &pfx->fxrts[
id], &result, channel, x, y)) {
4352 q[i] = ClampToQuantum ((MagickRealType) (QuantumRange*result));
4354 p+=(ptrdiff_t) GetPixelChannels (image);
4355 q+=(ptrdiff_t) GetPixelChannels (fx_image);
4357 if (SyncCacheViewAuthenticPixels(fx_view, pfx->exception) == MagickFalse)
4359 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4364#if defined(MAGICKCORE_OPENMP_SUPPORT)
4368 proceed = SetImageProgress (image, FxImageTag, progress, image->rows);
4369 if (proceed == MagickFalse)
4374 fx_view = DestroyCacheView (fx_view);
4375 image_view = DestroyCacheView (image_view);
4379 if (pfx->DebugOpt && pfx->usedUserSymbols) {
4381 char UserSym[MagickPathExtent];
4382 fprintf (stderr,
"User symbols (%i):\n", pfx->usedUserSymbols);
4383 for (t=0; t < (int) GetMagickResourceLimit(ThreadResource); t++) {
4384 for (i = 0; i < (int) pfx->usedUserSymbols; i++) {
4385 fprintf (stderr,
"th=%i us=%i '%s': %.*Lg\n",
4386 t, i, NameOfUserSym (pfx, i, UserSym), pfx->precision, pfx->fxrts[t].UserSymVals[i]);
4391 if ((status == MagickFalse) || (pfx->exception->severity >= ErrorException))
4392 fx_image=DestroyImage(fx_image);
4394 pfx=DestroyFxInfo(pfx);