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, MagickPathExtent);
877 (void) CopyMagickString(pfx->ShortExp + MaxLen,
"...", 4);
880 p = strchr(pfx->ShortExp,
'\n');
881 if (p) (void) CopyMagickString(p,
"...", 4);
883 p = strchr(pfx->ShortExp,
'\r');
884 if (p) (void) CopyMagickString(p,
"...", 4);
887 return pfx->ShortExp;
890static char * SetShortExp (
FxInfo * pfx)
892 return SetPtrShortExp (pfx, pfx->pex, MaxTokenLen-1);
895static int FindUserSymbol (
FxInfo * pfx,
char * name)
902 lenName = strlen (name);
903 for (i=0; i < pfx->usedUserSymbols; i++) {
905 if (lenName == pus->len && LocaleNCompare (name, pus->pex, lenName)==0)
break;
907 if (i == pfx->usedUserSymbols)
return NULL_ADDRESS;
911static MagickBooleanType ExtendUserSymbols (
FxInfo * pfx)
913 pfx->numUserSymbols = (int) ceil (pfx->numUserSymbols * (1 + TableExtend));
914 pfx->UserSymbols = (
UserSymbolT*) ResizeMagickMemory (pfx->UserSymbols, (
size_t) pfx->numUserSymbols *
sizeof(
UserSymbolT));
915 if (!pfx->UserSymbols) {
916 (void) ThrowMagickException (
917 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
919 pfx->numUserSymbols);
926static int AddUserSymbol (
FxInfo * pfx,
char * pex,
size_t len)
929 if (++pfx->usedUserSymbols >= pfx->numUserSymbols) {
930 if (!ExtendUserSymbols (pfx))
return -1;
932 pus = &pfx->UserSymbols[pfx->usedUserSymbols-1];
936 return pfx->usedUserSymbols-1;
939static void DumpTables (FILE * fh)
943 for (i=0; i <= rNull; i++) {
944 const char * str =
"";
945 if ( i < oNull) str = Operators[i].str;
946 if (i >= (
int) FirstFunc && i < fNull) str = Functions[i-(int) FirstFunc].str;
947 if (i >= (
int) FirstImgAttr && i < aNull) str = ImgAttrs[i-(
int) FirstImgAttr].str;
948 if (i >= (
int) FirstSym && i < sNull) str = Symbols[i-(
int) FirstSym].str;
949 if (i >= (
int) FirstCont && i < rNull) str = Controls[i-(
int) FirstCont].str;
950 if (i==0 ) fprintf (stderr,
"Operators:\n ");
951 else if (i==oNull) fprintf (stderr,
"\nFunctions:\n ");
952 else if (i==fNull) fprintf (stderr,
"\nImage attributes:\n ");
953 else if (i==aNull) fprintf (stderr,
"\nSymbols:\n ");
954 else if (i==sNull) fprintf (stderr,
"\nControls:\n ");
955 fprintf (fh,
" %s", str);
960static char * NameOfUserSym (
FxInfo * pfx,
int ndx,
char * buf)
963 assert (ndx >= 0 && ndx < pfx->usedUserSymbols);
964 pus = &pfx->UserSymbols[ndx];
965 (void) CopyMagickString (buf, pus->pex, pus->len+1);
969static void DumpUserSymbols (
FxInfo * pfx, FILE * fh)
971 char UserSym[MagickPathExtent];
973 fprintf (fh,
"UserSymbols (%i)\n", pfx->usedUserSymbols);
974 for (i=0; i < pfx->usedUserSymbols; i++) {
975 fprintf (fh,
" %i: '%s'\n", i, NameOfUserSym (pfx, i, UserSym));
979static MagickBooleanType BuildRPN (
FxInfo * pfx)
981 pfx->numUserSymbols = InitNumUserSymbols;
982 pfx->usedUserSymbols = 0;
983 pfx->UserSymbols = (
UserSymbolT*) AcquireMagickMemory ((
size_t) pfx->numUserSymbols *
sizeof(
UserSymbolT));
984 if (!pfx->UserSymbols) {
985 (void) ThrowMagickException (
986 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
988 pfx->numUserSymbols);
992 pfx->numElements = RpnInit;
993 pfx->usedElements = 0;
994 pfx->Elements = NULL;
996 pfx->Elements = (
ElementT*) AcquireMagickMemory ((
size_t) pfx->numElements *
sizeof(
ElementT));
998 if (!pfx->Elements) {
999 (void) ThrowMagickException (
1000 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
1006 pfx->usedOprStack = 0;
1007 pfx->maxUsedOprStack = 0;
1008 pfx->numOprStack = InitNumOprStack;
1009 pfx->OperatorStack = (OperatorE*) AcquireMagickMemory ((
size_t) pfx->numOprStack *
sizeof(OperatorE));
1010 if (!pfx->OperatorStack) {
1011 (void) ThrowMagickException (
1012 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
1013 "OperatorStack",
"%i",
1021static MagickBooleanType AllocFxRt (
FxInfo * pfx,
fxRtT * pfxrt)
1025 pfxrt->random_info = AcquireRandomInfo ();
1026 pfxrt->thisPixel = NULL;
1028 nRnd = 20 + 10 * (int) GetPseudoRandomValue (pfxrt->random_info);
1029 for (i=0; i < nRnd; i++) (
void) GetPseudoRandomValue (pfxrt->random_info);;
1031 pfxrt->usedValStack = 0;
1032 pfxrt->numValStack = 2 * pfx->maxUsedOprStack;
1033 if (pfxrt->numValStack < MinValStackSize) pfxrt->numValStack = MinValStackSize;
1034 pfxrt->ValStack = (fxFltType*) AcquireMagickMemory ((
size_t) pfxrt->numValStack *
sizeof(fxFltType));
1035 if (!pfxrt->ValStack) {
1036 (void) ThrowMagickException (
1037 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
1039 pfxrt->numValStack);
1043 pfxrt->UserSymVals = NULL;
1045 if (pfx->usedUserSymbols) {
1046 pfxrt->UserSymVals = (fxFltType*) AcquireMagickMemory ((
size_t) pfx->usedUserSymbols *
sizeof(fxFltType));
1047 if (!pfxrt->UserSymVals) {
1048 (void) ThrowMagickException (
1049 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
1050 "UserSymVals",
"%i",
1051 pfx->usedUserSymbols);
1054 for (i = 0; i < pfx->usedUserSymbols; i++) pfxrt->UserSymVals[i] = (fxFltType) 0;
1060static MagickBooleanType ExtendRPN (
FxInfo * pfx)
1062 pfx->numElements = (int) ceil (pfx->numElements * (1 + TableExtend));
1063 pfx->Elements = (
ElementT*) ResizeMagickMemory (pfx->Elements, (
size_t) pfx->numElements *
sizeof(
ElementT));
1064 if (!pfx->Elements) {
1065 (void) ThrowMagickException (
1066 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
1074static inline MagickBooleanType OprInPlace (
int op)
1076 return (op >= oAddEq && op <= oSubSub ? MagickTrue : MagickFalse);
1079static const char * OprStr (
int oprNum)
1082 if (oprNum < 0) str =
"bad OprStr";
1083 else if (oprNum <= oNull) str = Operators[oprNum].str;
1084 else if (oprNum <= fNull) str = Functions[oprNum-(int) FirstFunc].str;
1085 else if (oprNum <= aNull) str = ImgAttrs[oprNum-(int) FirstImgAttr].str;
1086 else if (oprNum <= sNull) str = Symbols[oprNum-(int) FirstSym].str;
1087 else if (oprNum <= rNull) str = Controls[oprNum-(int) FirstCont].str;
1094static MagickBooleanType DumpRPN (
FxInfo * pfx, FILE * fh)
1098 fprintf (fh,
"DumpRPN:");
1099 fprintf (fh,
" numElements=%i", pfx->numElements);
1100 fprintf (fh,
" usedElements=%i", pfx->usedElements);
1101 fprintf (fh,
" maxUsedOprStack=%i", pfx->maxUsedOprStack);
1102 fprintf (fh,
" ImgListLen=%g", (
double) pfx->ImgListLen);
1103 fprintf (fh,
" NeedStats=%s", pfx->NeedStats ?
"yes" :
"no");
1104 fprintf (fh,
" GotStats=%s", pfx->GotStats ?
"yes" :
"no");
1105 fprintf (fh,
" NeedHsl=%s\n", pfx->NeedHsl ?
"yes" :
"no");
1106 if (pfx->runType==rtEntireImage) fprintf (stderr,
"EntireImage");
1107 else if (pfx->runType==rtCornerOnly) fprintf (stderr,
"CornerOnly");
1111 for (i=0; i < pfx->usedElements; i++) {
1112 ElementT * pel = &pfx->Elements[i];
1113 pel->number_dest = 0;
1115 for (i=0; i < pfx->usedElements; i++) {
1116 ElementT * pel = &pfx->Elements[i];
1117 if (pel->operator_index == rGoto || pel->operator_index == rGotoChk || pel->operator_index == rIfZeroGoto || pel->operator_index == rIfNotZeroGoto) {
1118 if (pel->element_index >= 0 && pel->element_index < pfx->numElements) {
1119 ElementT * pelDest = &pfx->Elements[pel->element_index];
1120 pelDest->number_dest++;
1124 for (i=0; i < pfx->usedElements; i++) {
1125 char UserSym[MagickPathExtent];
1127 ElementT * pel = &pfx->Elements[i];
1128 const char * str = OprStr (pel->operator_index);
1129 const char *sRelAbs =
"";
1131 if (pel->operator_index == fP || pel->operator_index == fUP || pel->operator_index == fVP || pel->operator_index == fSP)
1132 sRelAbs = pel->is_relative ?
"[]" :
"{}";
1134 if (pel->type == etColourConstant)
1135 fprintf (fh,
" %i: %s vals=%.*Lg,%.*Lg,%.*Lg '%s%s' nArgs=%i ndx=%i %s",
1136 i, sElementTypes[pel->type],
1137 pfx->precision, pel->val, pfx->precision, pel->val1, pfx->precision, pel->val2,
1138 str, sRelAbs, pel->number_args, pel->element_index,
1139 pel->do_push ?
"push" :
"NO push");
1141 fprintf (fh,
" %i: %s val=%.*Lg '%s%s' nArgs=%i ndx=%i %s",
1142 i, sElementTypes[pel->type], pfx->precision, pel->val, str, sRelAbs,
1143 pel->number_args, pel->element_index,
1144 pel->do_push ?
"push" :
"NO push");
1146 if (pel->img_attr_qual != aNull)
1147 fprintf (fh,
" ia=%s", OprStr((
int) pel->img_attr_qual));
1149 if (pel->channel_qual != NO_CHAN_QUAL) {
1150 if (pel->channel_qual == THIS_CHANNEL) fprintf (stderr,
" ch=this");
1151 else fprintf (stderr,
" ch=%i", pel->channel_qual);
1154 if (pel->operator_index == rCopyTo) {
1155 fprintf (fh,
" CopyTo ==> %s", NameOfUserSym (pfx, pel->element_index, UserSym));
1156 }
else if (pel->operator_index == rCopyFrom) {
1157 fprintf (fh,
" CopyFrom <== %s", NameOfUserSym (pfx, pel->element_index, UserSym));
1158 }
else if (OprInPlace (pel->operator_index)) {
1159 fprintf (fh,
" <==> %s", NameOfUserSym (pfx, pel->element_index, UserSym));
1161 if (pel->number_dest > 0) fprintf (fh,
" <==dest(%i)", pel->number_dest);
1167static void DestroyRPN (
FxInfo * pfx)
1169 pfx->numOprStack = 0;
1170 pfx->usedOprStack = 0;
1171 if (pfx->OperatorStack) pfx->OperatorStack = (OperatorE*) RelinquishMagickMemory (pfx->OperatorStack);
1173 pfx->numElements = 0;
1174 pfx->usedElements = 0;
1175 if (pfx->Elements) pfx->Elements = (
ElementT*) RelinquishMagickMemory (pfx->Elements);
1177 pfx->usedUserSymbols = 0;
1178 if (pfx->UserSymbols) pfx->UserSymbols = (
UserSymbolT*) RelinquishMagickMemory (pfx->UserSymbols);
1181static void DestroyFxRt (
fxRtT * pfxrt)
1183 pfxrt->usedValStack = 0;
1184 if (pfxrt->ValStack) pfxrt->ValStack = (fxFltType*) RelinquishMagickMemory (pfxrt->ValStack);
1185 if (pfxrt->UserSymVals) pfxrt->UserSymVals = (fxFltType*) RelinquishMagickMemory (pfxrt->UserSymVals);
1187 pfxrt->random_info = DestroyRandomInfo (pfxrt->random_info);
1190static size_t GetToken (
FxInfo * pfx)
1201 char * p = pfx->pex;
1205 if (!isalpha((
int)*p))
return 0;
1212 if (LocaleNCompare (p,
"icc-", 4) == 0) {
1215 while (isalpha ((
int)*p)) { len++; p++; }
1216 }
else if (LocaleNCompare (p,
"device-", 7) == 0) {
1219 while (isalpha ((
int)*p)) { len++; p++; }
1221 while (isalpha ((
int)*p)) { len++; p++; }
1222 if (*p ==
'_') { len++; p++; }
1223 while (isalpha ((
int)*p)) { len++; p++; }
1224 while (isdigit ((
int)*p)) { len++; p++; }
1226 if (len >= MaxTokenLen) {
1227 (void) ThrowMagickException (
1228 pfx->exception, GetMagickModule(), OptionError,
1229 "GetToken: too long",
"%g at '%s'",
1230 (double) len, SetShortExp(pfx));
1234 (void) CopyMagickString (pfx->token, pfx->pex, (len+1<MaxTokenLen)?len+1:MaxTokenLen);
1237 pfx->lenToken = strlen (pfx->token);
1241static MagickBooleanType TokenMaybeUserSymbol (
FxInfo * pfx)
1243 char * p = pfx->token;
1246 if (!isalpha ((
int)*p++))
return MagickFalse;
1249 if (i < 2)
return MagickFalse;
1253static MagickBooleanType AddElement (
FxInfo * pfx, fxFltType val,
int oprNum)
1257 assert (oprNum <= rNull);
1259 if (++pfx->usedElements >= pfx->numElements) {
1260 if (!ExtendRPN (pfx))
return MagickFalse;
1263 pel = &pfx->Elements[pfx->usedElements-1];
1264 pel->type = TypeOfOpr (oprNum);
1266 pel->val1 = (fxFltType) 0;
1267 pel->val2 = (fxFltType) 0;
1268 pel->operator_index = oprNum;
1269 pel->do_push = MagickTrue;
1270 pel->element_index = 0;
1271 pel->channel_qual = NO_CHAN_QUAL;
1272 pel->img_attr_qual = aNull;
1273 pel->number_dest = 0;
1274 pel->exp_start = NULL;
1277 if (oprNum <= oNull) pel->number_args = Operators[oprNum].number_args;
1278 else if (oprNum <= fNull) pel->number_args = Functions[oprNum-(int) FirstFunc].number_args;
1279 else if (oprNum <= aNull) pel->number_args = 0;
1280 else if (oprNum <= sNull) pel->number_args = 0;
1281 else pel->number_args = Controls[oprNum-(int) FirstCont].number_args;
1286static MagickBooleanType AddAddressingElement (
FxInfo * pfx,
int oprNum,
int EleNdx)
1289 if (!AddElement (pfx, (fxFltType) 0, oprNum))
return MagickFalse;
1290 pel = &pfx->Elements[pfx->usedElements-1];
1291 pel->element_index = EleNdx;
1292 if (oprNum == rGoto || oprNum == rGotoChk || oprNum == rIfZeroGoto || oprNum == rIfNotZeroGoto
1293 || oprNum == rZerStk)
1295 pel->do_push = MagickFalse;
1305static MagickBooleanType AddColourElement (
FxInfo * pfx, fxFltType val0, fxFltType val1, fxFltType val2)
1308 if (!AddElement (pfx, val0, oNull))
return MagickFalse;
1309 pel = &pfx->Elements[pfx->usedElements-1];
1312 pel->type = etColourConstant;
1316static inline void SkipSpaces (
FxInfo * pfx)
1318 while (isspace ((
int)*pfx->pex)) pfx->pex++;
1321static inline char PeekChar (
FxInfo * pfx)
1327static inline MagickBooleanType PeekStr (
FxInfo * pfx,
const char * str)
1331 return (LocaleNCompare (pfx->pex, str, strlen(str))==0 ? MagickTrue : MagickFalse);
1334static MagickBooleanType ExpectChar (
FxInfo * pfx,
char c)
1336 if (PeekChar (pfx) != c) {
1337 (void) ThrowMagickException (
1338 pfx->exception, GetMagickModule(), OptionError,
1339 "Expected char",
"'%c' at '%s'", c, SetShortExp (pfx));
1346static int MaybeXYWH (
FxInfo * pfx, ImgAttrE * pop)
1353 if (*pop != aPage && *pop != aPrintsize && *pop != aRes)
return 0;
1355 if (PeekChar (pfx) !=
'.')
return 0;
1357 if (!ExpectChar (pfx,
'.'))
return 0;
1359 (void) GetToken (pfx);
1360 if (LocaleCompare (
"x", pfx->token)==0) ret=1;
1361 else if (LocaleCompare (
"y", pfx->token)==0) ret=2;
1362 else if (LocaleCompare (
"width", pfx->token)==0) ret=3;
1363 else if (LocaleCompare (
"height", pfx->token)==0) ret=4;
1366 (void) ThrowMagickException (
1367 pfx->exception, GetMagickModule(), OptionError,
1368 "Invalid 'x' or 'y' or 'width' or 'height' token=",
"'%s' at '%s'",
1369 pfx->token, SetShortExp(pfx));
1371 if (*pop == aPage) (*pop) = (ImgAttrE) ((
int) *pop + ret);
1374 (void) ThrowMagickException (
1375 pfx->exception, GetMagickModule(), OptionError,
1376 "Invalid 'width' or 'height' token=",
"'%s' at '%s'",
1377 pfx->token, SetShortExp(pfx));
1379 (*pop) = (ImgAttrE) ((
int) *pop + ret);
1382 pfx->pex+=pfx->lenToken;
1387static MagickBooleanType ExtendOperatorStack (
FxInfo * pfx)
1389 pfx->numOprStack = (int) ceil (pfx->numOprStack * (1 + TableExtend));
1390 pfx->OperatorStack = (OperatorE*) ResizeMagickMemory (pfx->OperatorStack, (
size_t) pfx->numOprStack *
sizeof(OperatorE));
1391 if (!pfx->OperatorStack) {
1392 (void) ThrowMagickException (
1393 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
1401static MagickBooleanType PushOperatorStack (
FxInfo * pfx,
int op)
1403 if (++pfx->usedOprStack >= pfx->numOprStack) {
1404 if (!ExtendOperatorStack (pfx))
1407 pfx->OperatorStack[pfx->usedOprStack-1] = (OperatorE) op;
1409 if (pfx->maxUsedOprStack < pfx->usedOprStack)
1410 pfx->maxUsedOprStack = pfx->usedOprStack;
1414static OperatorE GetLeadingOp (
FxInfo * pfx)
1416 OperatorE op = oNull;
1418 if (*pfx->pex ==
'-') op = oUnaryMinus;
1419 else if (*pfx->pex ==
'+') op = oUnaryPlus;
1420 else if (*pfx->pex ==
'~') op = oBitNot;
1421 else if (*pfx->pex ==
'!') op = oLogNot;
1422 else if (*pfx->pex ==
'(') op = oOpenParen;
1427static inline MagickBooleanType OprIsUnaryPrefix (OperatorE op)
1429 return (op == oUnaryMinus || op == oUnaryPlus || op == oBitNot || op == oLogNot ? MagickTrue : MagickFalse);
1432static MagickBooleanType TopOprIsUnaryPrefix (
FxInfo * pfx)
1434 if (!pfx->usedOprStack)
return MagickFalse;
1436 return OprIsUnaryPrefix (pfx->OperatorStack[pfx->usedOprStack-1]);
1439static MagickBooleanType PopOprOpenParen (
FxInfo * pfx, OperatorE op)
1442 if (!pfx->usedOprStack)
return MagickFalse;
1444 if (pfx->OperatorStack[pfx->usedOprStack-1] != op)
return MagickFalse;
1446 pfx->usedOprStack--;
1451static int GetCoordQualifier (
FxInfo * pfx,
int op)
1455 if (op != fU && op != fV && op != fS)
return -1;
1457 (void) GetToken (pfx);
1459 if (pfx->lenToken != 1) {
1462 if (*pfx->token !=
'p' && *pfx->token !=
'P')
return -1;
1463 if (!GetFunction (pfx, fP))
return -1;
1468static PixelChannel GetChannelQualifier (
FxInfo * pfx,
int op)
1470 if (op == fU || op == fV || op == fP ||
1471 op == fUP || op == fVP ||
1472 op == fS || (op >= (
int) FirstImgAttr && op <= aNull)
1475 const ChannelT * pch = &Channels[0];
1476 (void) GetToken (pfx);
1479 if (LocaleCompare (pch->str, pfx->token)==0) {
1481 if (op >= (
int) FirstImgAttr && op <= (int) ((OperatorE)aNull) &&
1482 ChanIsVirtual (pch->pixel_channel)
1485 (void) ThrowMagickException (
1486 pfx->exception, GetMagickModule(), OptionError,
1487 "Can't have image attribute with channel qualifier at",
"'%s' at '%s'",
1488 pfx->token, SetShortExp(pfx));
1489 return NO_CHAN_QUAL;
1492 pfx->pex += pfx->lenToken;
1493 return pch->pixel_channel;
1498 return NO_CHAN_QUAL;
1501static ImgAttrE GetImgAttrToken (
FxInfo * pfx)
1503 ImgAttrE ia = aNull;
1505 for (ia = FirstImgAttr; ia < aNull; ia=(ImgAttrE) (ia+1)) {
1506 iaStr = ImgAttrs[ia-(int) FirstImgAttr].str;
1507 if (LocaleCompare (iaStr, pfx->token)==0) {
1508 pfx->pex += strlen(pfx->token);
1509 if (ImgAttrs[ia-(
int) FirstImgAttr].need_stats != MagickFalse) pfx->NeedStats = MagickTrue;
1510 MaybeXYWH (pfx, &ia);
1515 if (ia == aPage || ia == aPrintsize || ia == aRes) {
1516 (void) ThrowMagickException (
1517 pfx->exception, GetMagickModule(), OptionError,
1518 "Attribute",
"'%s' needs qualifier at '%s'",
1519 iaStr, SetShortExp(pfx));
1525static ImgAttrE GetImgAttrQualifier (
FxInfo * pfx,
int op)
1527 ImgAttrE ia = aNull;
1528 if (op == (OperatorE)fU || op == (OperatorE)fV || op == (OperatorE)fP || op == (OperatorE)fS) {
1529 (void) GetToken (pfx);
1530 if (pfx->lenToken == 0) {
1533 ia = GetImgAttrToken (pfx);
1538static MagickBooleanType IsQualifier (
FxInfo * pfx)
1540 if (PeekChar (pfx) ==
'.') {
1547static ssize_t GetProperty (
FxInfo * pfx, fxFltType *val)
1553 if (PeekStr (pfx,
"%[")) {
1556 char sProperty [MagickPathExtent];
1557 char * p = pfx->pex + 2;
1561 if (*p ==
'[') level++;
1562 else if (*p ==
']') {
1563 if (level == 0)
break;
1568 if (!*p || level != 0) {
1569 (void) ThrowMagickException (
1570 pfx->exception, GetMagickModule(), OptionError,
1571 "After '%[' expected ']' at",
"'%s'",
1576 len = (size_t) (p - pfx->pex + 1);
1577 if (len > MaxTokenLen) {
1578 (void) ThrowMagickException (
1579 pfx->exception, GetMagickModule(), OptionError,
1580 "Too much text between '%[' and ']' at",
"'%s'",
1585 (void) CopyMagickString (sProperty, pfx->pex, len+1);
1586 sProperty[len] =
'\0';
1590 text = InterpretImageProperties (pfx->image->image_info, pfx->image,
1591 sProperty, pfx->exception);
1592 if (!text || !*text) {
1593 text = DestroyString(text);
1594 (void) ThrowMagickException (
1595 pfx->exception, GetMagickModule(), OptionError,
1596 "Unknown property",
"'%s' at '%s'",
1597 sProperty, SetShortExp(pfx));
1601 *val = strtold (text, &tailptr);
1602 if (text == tailptr) {
1603 text = DestroyString(text);
1604 (void) ThrowMagickException (
1605 pfx->exception, GetMagickModule(), OptionError,
1606 "Property",
"'%s' text '%s' is not a number at '%s'",
1607 sProperty, text, SetShortExp(pfx));
1611 text = DestroyString(text);
1613 return ((ssize_t) len);
1619static inline ssize_t GetConstantColour (
FxInfo * pfx, fxFltType *v0, fxFltType *v1, fxFltType *v2)
1630 *dummy_exception = AcquireExceptionInfo ();
1640 char ColSp[MagickPathExtent];
1641 (void) CopyMagickString (ColSp, pfx->token, MaxTokenLen);
1642 p = ColSp + pfx->lenToken - 1;
1643 if (*p ==
'a' || *p ==
'A') *p =
'\0';
1645 (void) GetPixelInfo (pfx->image, &colour);
1649 IsGray = (LocaleCompare (ColSp,
"gray") == 0) ? MagickTrue : MagickFalse;
1650 IsIcc = (LocaleCompare (ColSp,
"icc-color") == 0) ? MagickTrue : MagickFalse;
1651 IsDev = (LocaleNCompare (ColSp,
"device-", 7) == 0) ? MagickTrue : MagickFalse;
1655 if (!QueryColorCompliance (pfx->token, AllCompliance, &colour, dummy_exception) || IsGray) {
1656 ssize_t type = ParseCommandOption (MagickColorspaceOptions, MagickFalse, ColSp);
1657 if (type >= 0 || IsIcc || IsDev) {
1658 char * q = pfx->pex + pfx->lenToken;
1659 while (isspace((
int) ((
unsigned char) *q))) q++;
1662 char sFunc[MagickPathExtent];
1663 while (*q && *q !=
')') q++;
1665 (void) ThrowMagickException (
1666 pfx->exception, GetMagickModule(), OptionError,
1667 "constant color missing ')'",
"at '%s'",
1669 dummy_exception = DestroyExceptionInfo (dummy_exception);
1672 lenfun = (size_t) (q - pfx->pex + 1);
1673 if (lenfun > MaxTokenLen) {
1674 (void) ThrowMagickException (
1675 pfx->exception, GetMagickModule(), OptionError,
1676 "lenfun too long",
"'%lu' at '%s'",
1677 (
unsigned long) lenfun, SetShortExp(pfx));
1678 dummy_exception = DestroyExceptionInfo (dummy_exception);
1681 (void) CopyMagickString (sFunc, pfx->pex, lenfun+1);
1682 if (QueryColorCompliance (sFunc, AllCompliance, &colour, dummy_exception)) {
1683 *v0 = QuantumScale*colour.red;
1684 *v1 = QuantumScale*colour.green;
1685 *v2 = QuantumScale*colour.blue;
1686 dummy_exception = DestroyExceptionInfo (dummy_exception);
1687 return (ssize_t)lenfun;
1690 (void) ThrowMagickException (
1691 pfx->exception, GetMagickModule(), OptionError,
1692 "colorspace but not a valid color with '(...)' at",
"'%s'",
1694 dummy_exception = DestroyExceptionInfo (dummy_exception);
1699 dummy_exception = DestroyExceptionInfo (dummy_exception);
1704 *v0 = QuantumScale*colour.red;
1705 *v1 = QuantumScale*colour.green;
1706 *v2 = QuantumScale*colour.blue;
1708 dummy_exception = DestroyExceptionInfo (dummy_exception);
1709 return (ssize_t)strlen (pfx->token);
1712static inline ssize_t GetHexColour (
FxInfo * pfx, fxFltType *v0, fxFltType *v1, fxFltType *v2)
1721 if (*pfx->pex !=
'#')
return 0;
1725 while (isxdigit ((
int)*p)) p++;
1726 if (isalpha ((
int)*p)) {
1727 (void) ThrowMagickException (
1728 pfx->exception, GetMagickModule(), OptionError,
1729 "Bad hex number at",
"'%s'",
1734 len = (size_t) (p - pfx->pex);
1735 if (len < 1)
return 0;
1736 if (len >= MaxTokenLen) {
1737 (void) ThrowMagickException (
1738 pfx->exception, GetMagickModule(), OptionError,
1739 "Hex colour too long at",
"'%s'",
1743 (void) CopyMagickString (pfx->token, pfx->pex, len+1);
1745 (void) GetPixelInfo (pfx->image, &colour);
1747 if (!QueryColorCompliance (pfx->token, AllCompliance, &colour, pfx->exception)) {
1748 (void) ThrowMagickException (
1749 pfx->exception, GetMagickModule(), OptionError,
1750 "QueryColorCompliance rejected",
"'%s' at '%s'",
1751 pfx->token, SetShortExp(pfx));
1755 *v0 = QuantumScale*colour.red;
1756 *v1 = QuantumScale*colour.green;
1757 *v2 = QuantumScale*colour.blue;
1759 return (ssize_t) len;
1762static MagickBooleanType GetFunction (
FxInfo * pfx, FunctionE fe)
1766 const char * funStr = Functions[fe-(int) FirstFunc].str;
1767 int nArgs = Functions[fe-(int) FirstFunc].number_args;
1769 char expChLimit =
')';
1770 const char *strLimit =
",)";
1771 OperatorE pushOp = oOpenParen;
1778 int ndx0 = NULL_ADDRESS, ndx1 = NULL_ADDRESS, ndx2 = NULL_ADDRESS, ndx3 = NULL_ADDRESS;
1780 MagickBooleanType coordQual = MagickFalse;
1781 PixelChannel chQual = NO_CHAN_QUAL;
1782 ImgAttrE iaQual = aNull;
1784 pfx->pex += pfx->lenToken;
1787 char p = PeekChar (pfx);
1789 (void) ExpectChar (pfx,
'{');
1790 pushOp = oOpenBrace;
1794 }
else if (p==
'[') {
1795 (void) ExpectChar (pfx,
'[');
1796 pushOp = oOpenBracket;
1805 }
else if (fe == fU) {
1806 char p = PeekChar (pfx);
1808 (void) ExpectChar (pfx,
'[');
1809 pushOp = oOpenBracket;
1818 }
else if (fe == fV || fe == fS) {
1820 pushOp = oOpenBracket;
1824 if (!ExpectChar (pfx,
'('))
return MagickFalse;
1826 if (!PushOperatorStack (pfx, (
int) pushOp))
return MagickFalse;
1828 pExpStart = pfx->pex;
1829 ndx0 = pfx->usedElements;
1831 (void) AddAddressingElement (pfx, rGoto, NULL_ADDRESS);
1835 if (TranslateStatementList (pfx, strLimit, &chLimit)) {
1839 (void) ThrowMagickException (
1840 pfx->exception, GetMagickModule(), OptionError,
1841 "For function",
"'%s' expected ')' at '%s'",
1842 funStr, SetShortExp(pfx));
1846 if (!chLimit)
break;
1847 if (fe == fP || fe == fS|| fe == fIf) {
1848 (void) AddElement (pfx, (fxFltType) 0, oNull);
1853 if (strchr (strLimit, chLimit)==NULL) {
1854 (void) ThrowMagickException (
1855 pfx->exception, GetMagickModule(), OptionError,
1856 "For function",
"'%s' expected one of '%s' after expression but found '%c' at '%s'",
1857 funStr, strLimit, chLimit ? chLimit :
' ', SetShortExp(pfx));
1866 if (ndx1 != NULL_ADDRESS) {
1867 (void) ThrowMagickException (
1868 pfx->exception, GetMagickModule(), OptionError,
1869 "For function",
"'%s' required argument is missing at '%s'",
1870 funStr, SetShortExp(pfx));
1873 ndx1 = pfx->usedElements;
1874 if (fe==fWhile || fe==fIf) {
1875 (void) AddAddressingElement (pfx, rIfZeroGoto, NULL_ADDRESS);
1876 }
else if (fe==fDo) {
1877 (void) AddAddressingElement (pfx, rIfZeroGoto, NULL_ADDRESS);
1878 }
else if (fe==fFor) {
1879 pfx->Elements[pfx->usedElements-1].do_push = MagickFalse;
1883 if (ndx2 != NULL_ADDRESS) {
1884 (void) ThrowMagickException (
1885 pfx->exception, GetMagickModule(), OptionError,
1886 "For function",
"'%s' required argument is missing at '%s'",
1887 funStr, SetShortExp(pfx));
1890 ndx2 = pfx->usedElements;
1892 pfx->Elements[pfx->usedElements-1].do_push = MagickFalse;
1893 (void) AddAddressingElement (pfx, rGotoChk, ndx0);
1894 }
else if (fe==fDo) {
1895 pfx->Elements[pfx->usedElements-1].do_push = MagickFalse;
1896 (void) AddAddressingElement (pfx, rGotoChk, ndx0 + 1);
1897 }
else if (fe==fFor) {
1898 (void) AddAddressingElement (pfx, rIfZeroGoto, NULL_ADDRESS);
1899 pfx->Elements[pfx->usedElements-1].do_push = MagickTrue;
1900 (void) AddAddressingElement (pfx, rZerStk, NULL_ADDRESS);
1901 }
else if (fe==fIf) {
1902 (void) AddAddressingElement (pfx, rGoto, NULL_ADDRESS);
1906 if (ndx3 != NULL_ADDRESS) {
1907 (void) ThrowMagickException (
1908 pfx->exception, GetMagickModule(), OptionError,
1909 "For function",
"'%s' required argument is missing at '%s'",
1910 funStr, SetShortExp(pfx));
1914 pfx->Elements[pfx->usedElements-1].do_push = MagickFalse;
1915 (void) AddAddressingElement (pfx, rGotoChk, ndx1);
1917 ndx3 = pfx->usedElements;
1922 if (chLimit == expChLimit) {
1923 lenExp = (size_t) (pfx->pex - pExpStart - 1);
1927 if (chLimit && chLimit != expChLimit && chLimit !=
',' ) {
1928 (void) ThrowMagickException (
1929 pfx->exception, GetMagickModule(), OptionError,
1930 "For function",
"'%s' expected '%c', found '%c' at '%s'",
1931 funStr, expChLimit, chLimit ? chLimit :
' ', SetShortExp(pfx));
1935 if (fe == fP || fe == fS || fe == fU || fe == fChannel) {
1936 while (FndArgs < Functions[fe-(
int) FirstFunc].number_args) {
1937 (void) AddElement (pfx, (fxFltType) 0, oNull);
1942 if (FndArgs > Functions[fe-(
int) FirstFunc].number_args)
1945 (void) ThrowMagickException (
1946 pfx->exception, GetMagickModule(), OptionError,
1947 "For function",
"'%s' expected up to %i arguments, found '%i' at '%s'",
1948 funStr, Functions[fe-(int) FirstFunc].number_args, FndArgs, SetShortExp(pfx));
1950 (void) ThrowMagickException (
1951 pfx->exception, GetMagickModule(), OptionError,
1952 "For function",
"'%s' expected %i arguments, found '%i' at '%s'",
1953 funStr, Functions[fe-(int) FirstFunc].number_args, FndArgs, SetShortExp(pfx));
1957 if (FndArgs < Functions[fe-(
int) FirstFunc].number_args) {
1958 (void) ThrowMagickException (
1959 pfx->exception, GetMagickModule(), OptionError,
1960 "For function",
"'%s' expected %i arguments, found too few (%i) at '%s'",
1961 funStr, Functions[fe-(int) FirstFunc].number_args, FndArgs, SetShortExp(pfx));
1964 if (fe != fS && fe != fV && FndArgs == 0 && Functions[fe-(
int) FirstFunc].number_args == 0) {
1966 chLimit = expChLimit;
1967 if (!ExpectChar (pfx,
')'))
return MagickFalse;
1970 if (chLimit != expChLimit) {
1971 (void) ThrowMagickException (
1972 pfx->exception, GetMagickModule(), OptionError,
1973 "For function",
"'%s', arguments don't end with '%c' at '%s'",
1974 funStr, expChLimit, SetShortExp(pfx));
1977 if (!PopOprOpenParen (pfx, pushOp)) {
1978 (void) ThrowMagickException (
1979 pfx->exception, GetMagickModule(), OptionError,
1980 "Bug: For function",
"'%s' tos not '%s' at '%s'",
1981 funStr, Operators[pushOp].str, SetShortExp(pfx));
1985 if (IsQualifier (pfx)) {
1987 if (fe == fU || fe == fV || fe == fS) {
1989 coordQual = (GetCoordQualifier (pfx, (
int) fe) == 1) ? MagickTrue : MagickFalse;
1994 ElementT * pel = &pfx->Elements[pfx->usedElements-1];
1995 if (pel->operator_index != fP) {
1996 (void) ThrowMagickException (
1997 pfx->exception, GetMagickModule(), OptionError,
1998 "Bug: For function",
"'%s' last element not 'p' at '%s'",
1999 funStr, SetShortExp(pfx));
2002 chQual = pel->channel_qual;
2003 expChLimit = (pel->is_relative) ?
']' :
'}';
2004 pfx->usedElements--;
2005 if (fe == fU) fe = fUP;
2006 else if (fe == fV) fe = fVP;
2007 else if (fe == fS) fe = fSP;
2008 funStr = Functions[fe-(int) FirstFunc].str;
2012 if ( chQual == NO_CHAN_QUAL &&
2013 (fe == fP || fe == fS || fe == fSP || fe == fU || fe == fUP || fe == fV || fe == fVP)
2016 chQual = GetChannelQualifier (pfx, (
int) fe);
2019 if (chQual == NO_CHAN_QUAL && (fe == fU || fe == fV || fe == fS)) {
2021 iaQual = GetImgAttrQualifier (pfx, (
int) fe);
2023 if (IsQualifier (pfx) && chQual == NO_CHAN_QUAL && iaQual != aNull) {
2024 chQual = GetChannelQualifier (pfx, (
int) fe);
2026 if (coordQual && iaQual != aNull) {
2027 (void) ThrowMagickException (
2028 pfx->exception, GetMagickModule(), OptionError,
2029 "For function",
"'%s', can't have qualifiers 'p' and image attribute '%s' at '%s'",
2030 funStr, pfx->token, SetShortExp(pfx));
2033 if (!coordQual && chQual == NO_CHAN_QUAL && iaQual == aNull) {
2034 (void) ThrowMagickException (
2035 pfx->exception, GetMagickModule(), OptionError,
2036 "For function",
"'%s', bad qualifier '%s' at '%s'",
2037 funStr, pfx->token, SetShortExp(pfx));
2040 if (!coordQual && chQual == CompositePixelChannel && iaQual == aNull) {
2041 (void) ThrowMagickException (
2042 pfx->exception, GetMagickModule(), OptionError,
2043 "For function",
"'%s', bad composite qualifier '%s' at '%s'",
2044 funStr, pfx->token, SetShortExp(pfx));
2048 if (chQual == HUE_CHANNEL || chQual == SAT_CHANNEL || chQual == LIGHT_CHANNEL) {
2049 pfx->NeedHsl = MagickTrue;
2051 if (iaQual >= FirstImgAttr && iaQual < aNull) {
2052 (void) ThrowMagickException (
2053 pfx->exception, GetMagickModule(), OptionError,
2054 "Can't have image attribute with HLS qualifier at",
"'%s'",
2061 if (iaQual != aNull && chQual != NO_CHAN_QUAL) {
2062 if (ImgAttrs[iaQual-(
int) FirstImgAttr].need_stats == MagickFalse) {
2063 (void) ThrowMagickException (
2064 pfx->exception, GetMagickModule(), OptionError,
2065 "Can't have image attribute ",
"'%s' with channel qualifier '%s' at '%s'",
2066 ImgAttrs[iaQual-(int) FirstImgAttr].str,
2067 pfx->token, SetShortExp(pfx));
2070 if (ChanIsVirtual (chQual)) {
2071 (void) ThrowMagickException (
2072 pfx->exception, GetMagickModule(), OptionError,
2073 "Can't have statistical image attribute ",
"'%s' with virtual channel qualifier '%s' at '%s'",
2074 ImgAttrs[iaQual-(int) FirstImgAttr].str,
2075 pfx->token, SetShortExp(pfx));
2082 pfx->Elements[ndx1].element_index = ndx2+1;
2083 }
else if (fe==fDo) {
2084 pfx->Elements[ndx0].element_index = ndx1+1;
2085 pfx->Elements[ndx1].element_index = ndx2+1;
2086 }
else if (fe==fFor) {
2087 pfx->Elements[ndx2].element_index = ndx3;
2088 }
else if (fe==fIf) {
2089 pfx->Elements[ndx1].element_index = ndx2 + 1;
2090 pfx->Elements[ndx2].element_index = ndx3;
2092 if (fe == fU && iaQual == aNull) {
2093 ElementT * pel = &pfx->Elements[pfx->usedElements-1];
2094 if (pel->type == etConstant && pel->val == 0.0) {
2095 pfx->usedElements--;
2099 (void) AddElement (pfx, (fxFltType) 0, (int) fe);
2100 if (fe == fP || fe == fU || fe == fU0 || fe == fUP ||
2101 fe == fV || fe == fVP || fe == fS || fe == fSP)
2103 ElementT * pel = &pfx->Elements[pfx->usedElements-1];
2104 pel->is_relative = (expChLimit ==
']' ? MagickTrue : MagickFalse);
2105 if (chQual >= 0) pel->channel_qual = chQual;
2106 if (iaQual != aNull && (fe == fU || fe == fV || fe == fS)) {
2108 pel->img_attr_qual = iaQual;
2113 if (pExpStart && lenExp) {
2114 ElementT * pel = &pfx->Elements[pfx->usedElements-1];
2115 pel->exp_start = pExpStart;
2116 pel->exp_len = lenExp;
2120 pfx->ContainsDebug = MagickTrue;
2125static MagickBooleanType IsStealth (
int op)
2127 return (op == fU0 || op == fUP || op == fSP || op == fVP ||
2128 (op >= FirstCont && op <= rNull) ? MagickTrue : MagickFalse
2132static MagickBooleanType GetOperand (
2133 FxInfo * pfx, MagickBooleanType * UserSymbol, MagickBooleanType * NewUserSymbol,
int * UserSymNdx,
2134 MagickBooleanType * needPopAll)
2137 *NewUserSymbol = *UserSymbol = MagickFalse;
2138 *UserSymNdx = NULL_ADDRESS;
2141 if (!*pfx->pex)
return MagickFalse;
2142 (void) GetToken (pfx);
2144 if (pfx->lenToken==0) {
2148 OperatorE op = GetLeadingOp (pfx);
2149 if (op==oOpenParen) {
2150 char chLimit =
'\0';
2151 if (!PushOperatorStack (pfx, (
int) op))
return MagickFalse;
2153 if (!TranslateExpression (pfx,
")", &chLimit, needPopAll)) {
2154 (void) ThrowMagickException (
2155 pfx->exception, GetMagickModule(), OptionError,
2156 "Empty expression in parentheses at",
"'%s'",
2160 if (chLimit !=
')') {
2161 (void) ThrowMagickException (
2162 pfx->exception, GetMagickModule(), OptionError,
2163 "'(' but no ')' at",
"'%s'",
2168 if (!PopOprOpenParen (pfx, oOpenParen)) {
2169 (void) ThrowMagickException (
2170 pfx->exception, GetMagickModule(), OptionError,
2171 "Bug: tos not '(' at",
"'%s'",
2176 }
else if (OprIsUnaryPrefix (op)) {
2177 MagickBooleanType operand_ok;
2178 if (!PushOperatorStack (pfx, (
int) op))
return MagickFalse;
2181 if (!*pfx->pex)
return MagickFalse;
2182 if (pfx->teDepth >= MagickMaxRecursionDepth) {
2183 (void) ThrowMagickException (
2184 pfx->exception, GetMagickModule(), OptionError,
2185 "Expression too deeply nested",
"(depth %i exceeds limit %i)",
2186 pfx->teDepth, MagickMaxRecursionDepth);
2190 operand_ok=GetOperand (pfx, UserSymbol, NewUserSymbol, UserSymNdx, needPopAll);
2193 (void) ThrowMagickException (
2194 pfx->exception, GetMagickModule(), OptionError,
2195 "After unary, bad operand at",
"'%s'",
2200 if (*NewUserSymbol) {
2201 (void) ThrowMagickException (
2202 pfx->exception, GetMagickModule(), OptionError,
2203 "After unary, NewUserSymbol at",
"'%s'",
2209 (void) AddAddressingElement (pfx, rCopyFrom, *UserSymNdx);
2210 *UserSymNdx = NULL_ADDRESS;
2212 *UserSymbol = MagickFalse;
2213 *NewUserSymbol = MagickFalse;
2216 (void) GetToken (pfx);
2218 }
else if (*pfx->pex ==
'#') {
2219 fxFltType v0=0, v1=0, v2=0;
2220 ssize_t lenToken = GetHexColour (pfx, &v0, &v1, &v2);
2222 (void) ThrowMagickException (
2223 pfx->exception, GetMagickModule(), OptionError,
2224 "Bad hex number at",
"'%s'",
2227 }
else if (lenToken > 0) {
2228 (void) AddColourElement (pfx, v0, v1, v2);
2239 fxFltType val = strtold (pfx->pex, &tailptr);
2240 if (pfx->pex != tailptr) {
2248 const char Prefixes[] =
"yzafpnum.kMGTPEZY";
2249 const char * pSi = strchr (Prefixes, *tailptr);
2250 if (pSi && *pSi !=
'.') Pow = (double) ((pSi - Prefixes) * 3 - 24);
2251 else if (*tailptr ==
'c') Pow = -2;
2252 else if (*tailptr ==
'h') Pow = 2;
2253 else if (*tailptr ==
'k') Pow = 3;
2255 if (*(++pfx->pex) ==
'i') {
2256 val *= pow (2.0, Pow/0.3);
2259 val *= pow (10.0, Pow);
2263 (void) AddElement (pfx, val, oNull);
2267 val = (fxFltType) 0;
2268 lenOptArt = GetProperty (pfx, &val);
2269 if (lenOptArt < 0)
return MagickFalse;
2270 if (lenOptArt > 0) {
2271 (void) AddElement (pfx, val, oNull);
2272 pfx->pex += lenOptArt;
2279 if (pfx->lenToken > 0) {
2284 for (ce = (ConstantE)0; ce < cNull; ce=(ConstantE) (ce+1)) {
2285 const char * ceStr = Constants[ce].str;
2286 if (LocaleCompare (ceStr, pfx->token)==0) {
2292 (void) AddElement (pfx, Constants[ce].val, oNull);
2293 pfx->pex += pfx->lenToken;
2302 for (fe = FirstFunc; fe < fNull; fe=(FunctionE) (fe+1)) {
2303 const char * feStr = Functions[fe-(int) FirstFunc].str;
2304 if (LocaleCompare (feStr, pfx->token)==0) {
2309 if (fe == fV && pfx->ImgListLen < 2) {
2310 (void) ThrowMagickException (
2311 pfx->exception, GetMagickModule(), OptionError,
2312 "Symbol 'v' but fewer than two images at",
"'%s'",
2317 if (IsStealth ((
int) fe)) {
2318 (void) ThrowMagickException (
2319 pfx->exception, GetMagickModule(), OptionError,
2320 "Function",
"'%s' not permitted at '%s'",
2321 pfx->token, SetShortExp(pfx));
2324 if (fe == fDo || fe == fFor || fe == fIf || fe == fWhile) {
2325 *needPopAll = MagickTrue;
2328 if (fe != fNull)
return (GetFunction (pfx, fe));
2334 ImgAttrE ia = GetImgAttrToken (pfx);
2337 (void) AddElement (pfx, val, (
int) ia);
2339 if (ImgAttrs[ia-(
int) FirstImgAttr].need_stats != MagickFalse) {
2340 if (IsQualifier (pfx)) {
2341 PixelChannel chQual = GetChannelQualifier (pfx, (
int) ia);
2343 if (chQual == NO_CHAN_QUAL) {
2344 (void) ThrowMagickException (
2345 pfx->exception, GetMagickModule(), OptionError,
2346 "Bad channel qualifier at",
"'%s'",
2351 pel = &pfx->Elements[pfx->usedElements-1];
2352 pel->channel_qual = chQual;
2363 for (se = FirstSym; se < sNull; se=(SymbolE) (se+1)) {
2364 const char * seStr = Symbols[se-(int) FirstSym].str;
2365 if (LocaleCompare (seStr, pfx->token)==0) {
2371 (void) AddElement (pfx, val, (
int) se);
2372 pfx->pex += pfx->lenToken;
2374 if (se==sHue || se==sSaturation || se==sLightness) pfx->NeedHsl = MagickTrue;
2382 fxFltType v0, v1, v2;
2383 ssize_t ColLen = GetConstantColour (pfx, &v0, &v1, &v2);
2384 if (ColLen < 0)
return MagickFalse;
2386 (void) AddColourElement (pfx, v0, v1, v2);
2395 const char *artifact;
2396 artifact = GetImageArtifact (pfx->image, pfx->token);
2397 if (artifact != (
const char *) NULL) {
2399 fxFltType val = strtold (artifact, &tailptr);
2400 if (pfx->token == tailptr) {
2401 (void) ThrowMagickException (
2402 pfx->exception, GetMagickModule(), OptionError,
2403 "Artifact",
"'%s' has value '%s', not a number, at '%s'",
2404 pfx->token, artifact, SetShortExp(pfx));
2407 (void) AddElement (pfx, val, oNull);
2408 pfx->pex+=pfx->lenToken;
2415 if (TokenMaybeUserSymbol (pfx)) {
2416 *UserSymbol = MagickTrue;
2417 *UserSymNdx = FindUserSymbol (pfx, pfx->token);
2418 if (*UserSymNdx == NULL_ADDRESS) {
2419 *UserSymNdx = AddUserSymbol (pfx, pfx->pex, pfx->lenToken);
2420 *NewUserSymbol = MagickTrue;
2423 pfx->pex += pfx->lenToken;
2429 (void) ThrowMagickException (
2430 pfx->exception, GetMagickModule(), OptionError,
2431 "Expected operand at",
"'%s'",
2437static inline MagickBooleanType IsRealOperator (OperatorE op)
2439 return (op < oOpenParen || op > oCloseBrace) ? MagickTrue : MagickFalse;
2442static inline MagickBooleanType ProcessTernaryOpr (
FxInfo * pfx,
TernaryT * ptern)
2447 if (pfx->usedOprStack == 0)
2449 if (pfx->OperatorStack[pfx->usedOprStack-1] == oQuery) {
2450 if (ptern->addr_query != NULL_ADDRESS) {
2451 (void) ThrowMagickException (
2452 pfx->exception, GetMagickModule(), OptionError,
2453 "Already have '?' in sub-expression at",
"'%s'",
2457 if (ptern->addr_colon != NULL_ADDRESS) {
2458 (void) ThrowMagickException (
2459 pfx->exception, GetMagickModule(), OptionError,
2460 "Already have ':' in sub-expression at",
"'%s'",
2464 pfx->usedOprStack--;
2465 ptern->addr_query = pfx->usedElements;
2466 (void) AddAddressingElement (pfx, rIfZeroGoto, NULL_ADDRESS);
2469 else if (pfx->OperatorStack[pfx->usedOprStack-1] == oColon) {
2470 if (ptern->addr_query == NULL_ADDRESS) {
2471 (void) ThrowMagickException (
2472 pfx->exception, GetMagickModule(), OptionError,
2473 "Need '?' in sub-expression at",
"'%s'",
2477 if (ptern->addr_colon != NULL_ADDRESS) {
2478 (void) ThrowMagickException (
2479 pfx->exception, GetMagickModule(), OptionError,
2480 "Already have ':' in sub-expression at",
"'%s'",
2484 pfx->usedOprStack--;
2485 ptern->addr_colon = pfx->usedElements;
2486 pfx->Elements[pfx->usedElements-1].do_push = MagickTrue;
2487 (void) AddAddressingElement (pfx, rGoto, NULL_ADDRESS);
2493static MagickBooleanType GetOperator (
2495 MagickBooleanType * Assign, MagickBooleanType * Update, MagickBooleanType * IncrDecr)
2499 MagickBooleanType DoneIt = MagickFalse;
2501 for (op = (OperatorE)0; op != oNull; op=(OperatorE) (op+1)) {
2502 const char * opStr = Operators[op].str;
2503 len = strlen(opStr);
2504 if (LocaleNCompare (opStr, pfx->pex, len)==0) {
2509 if (!IsRealOperator (op)) {
2510 (void) ThrowMagickException (
2511 pfx->exception, GetMagickModule(), OptionError,
2512 "Not a real operator at",
"'%s'",
2518 (void) ThrowMagickException (
2519 pfx->exception, GetMagickModule(), OptionError,
2520 "Expected operator at",
"'%s'",
2525 *Assign = (op==oAssign) ? MagickTrue : MagickFalse;
2526 *Update = OprInPlace ((
int) op);
2527 *IncrDecr = (op == oPlusPlus || op == oSubSub) ? MagickTrue : MagickFalse;
2534 while (pfx->usedOprStack > 0) {
2535 OperatorE top = pfx->OperatorStack[pfx->usedOprStack-1];
2536 int precTop, precNew;
2537 if (top == oOpenParen || top == oAssign || OprInPlace ((
int) top))
break;
2538 precTop = Operators[top].precedence;
2539 precNew = Operators[op].precedence;
2543 if (precTop < precNew)
break;
2544 (void) AddElement (pfx, (fxFltType) 0, (int) top);
2545 pfx->usedOprStack--;
2551 if (op==oCloseParen) {
2552 if (pfx->usedOprStack == 0) {
2553 (void) ThrowMagickException (
2554 pfx->exception, GetMagickModule(), OptionError,
2555 "Found ')' but nothing on stack at",
"'%s'",
2560 if (pfx->OperatorStack[pfx->usedOprStack-1] != oOpenParen) {
2561 (void) ThrowMagickException (
2562 pfx->exception, GetMagickModule(), OptionError,
2563 "Found ')' but no '(' on stack at",
"'%s'",
2567 pfx->usedOprStack--;
2568 DoneIt = MagickTrue;
2572 if (!PushOperatorStack (pfx, (
int) op))
return MagickFalse;
2580static MagickBooleanType ResolveTernaryAddresses (
FxInfo * pfx,
TernaryT * ptern)
2582 if (ptern->addr_query == NULL_ADDRESS && ptern->addr_colon == NULL_ADDRESS)
2585 if (ptern->addr_query != NULL_ADDRESS && ptern->addr_colon != NULL_ADDRESS) {
2586 pfx->Elements[ptern->addr_query].element_index = ptern->addr_colon + 1;
2587 pfx->Elements[ptern->addr_colon].element_index = pfx->usedElements;
2588 ptern->addr_query = NULL_ADDRESS;
2589 ptern->addr_colon = NULL_ADDRESS;
2590 }
else if (ptern->addr_query != NULL_ADDRESS) {
2591 (void) ThrowMagickException (
2592 pfx->exception, GetMagickModule(), OptionError,
2593 "'?' with no corresponding ':'",
"'%s' at '%s'",
2594 pfx->token, SetShortExp(pfx));
2596 }
else if (ptern->addr_colon != NULL_ADDRESS) {
2597 (void) ThrowMagickException (
2598 pfx->exception, GetMagickModule(), OptionError,
2599 "':' with no corresponding '?'",
"'%s' at '%s'",
2600 pfx->token, SetShortExp(pfx));
2606static MagickBooleanType TranslateExpression (
2607 FxInfo * pfx,
const char * strLimit,
char * chLimit, MagickBooleanType * needPopAll)
2611 MagickBooleanType UserSymbol, NewUserSymbol;
2612 int UserSymNdx0, UserSymNdx1;
2615 Assign = MagickFalse,
2616 Update = MagickFalse,
2617 IncrDecr = MagickFalse;
2622 ternary.addr_query = NULL_ADDRESS;
2623 ternary.addr_colon = NULL_ADDRESS;
2625 if (pfx->teDepth >= MagickMaxRecursionDepth) {
2626 (void) ThrowMagickException(pfx->exception, GetMagickModule(), OptionError,
2627 "Expression too deeply nested",
"(depth %i exceeds limit %i)",
2628 pfx->teDepth, MagickMaxRecursionDepth);
2636 StartEleNdx = pfx->usedElements-1;
2637 if (StartEleNdx < 0) StartEleNdx = 0;
2646 if (strchr(strLimit,*pfx->pex)!=NULL) {
2647 *chLimit = *pfx->pex;
2654 if (!GetOperand (pfx, &UserSymbol, &NewUserSymbol, &UserSymNdx0, needPopAll))
return MagickFalse;
2659 while (*pfx->pex && (!*strLimit || (strchr(strLimit,*pfx->pex)==NULL))) {
2660 if (!GetOperator (pfx, &Assign, &Update, &IncrDecr))
return MagickFalse;
2662 if (NewUserSymbol && !Assign) {
2663 (void) ThrowMagickException (
2664 pfx->exception, GetMagickModule(), OptionError,
2665 "Expected assignment after new UserSymbol",
"'%s' at '%s'",
2666 pfx->token, SetShortExp(pfx));
2669 if (!UserSymbol && Assign) {
2670 (void) ThrowMagickException (
2671 pfx->exception, GetMagickModule(), OptionError,
2672 "Attempted assignment to non-UserSymbol",
"'%s' at '%s'",
2673 pfx->token, SetShortExp(pfx));
2676 if (!UserSymbol && Update) {
2677 (void) ThrowMagickException (
2678 pfx->exception, GetMagickModule(), OptionError,
2679 "Attempted update to non-UserSymbol",
"'%s' at '%s'",
2680 pfx->token, SetShortExp(pfx));
2683 if (UserSymbol && (Assign || Update) && !IncrDecr) {
2685 if (!TranslateExpression (pfx, strLimit, chLimit, needPopAll))
return MagickFalse;
2686 if (!*pfx->pex)
break;
2687 if (!*strLimit)
break;
2688 if (strchr(strLimit,*chLimit)!=NULL)
break;
2690 if (UserSymbol && !Assign && !Update && UserSymNdx0 != NULL_ADDRESS) {
2692 (void) AddAddressingElement (pfx, rCopyFrom, UserSymNdx0);
2693 UserSymNdx0 = NULL_ADDRESS;
2694 pel = &pfx->Elements[pfx->usedElements-1];
2695 pel->do_push = MagickTrue;
2699 while (TopOprIsUnaryPrefix (pfx)) {
2700 OperatorE op = pfx->OperatorStack[pfx->usedOprStack-1];
2701 (void) AddElement (pfx, (fxFltType) 0, (int) op);
2702 pfx->usedOprStack--;
2706 if (!ProcessTernaryOpr (pfx, &ternary))
return MagickFalse;
2708 if (ternary.addr_colon != NULL_ADDRESS) {
2709 if (!TranslateExpression (pfx,
",);", chLimit, needPopAll))
return MagickFalse;
2713 UserSymbol = NewUserSymbol = MagickFalse;
2715 if ( (!*pfx->pex) || (*strLimit && (strchr(strLimit,*pfx->pex)!=NULL) ) )
2717 if (IncrDecr)
break;
2719 (void) ThrowMagickException (
2720 pfx->exception, GetMagickModule(), OptionError,
2721 "Expected operand after operator",
"at '%s'",
2727 (void) ThrowMagickException (
2728 pfx->exception, GetMagickModule(), OptionError,
2729 "'++' and '--' must be the final operators in an expression at",
"'%s'",
2734 if (!GetOperand (pfx, &UserSymbol, &NewUserSymbol, &UserSymNdx1, needPopAll)) {
2735 (void) ThrowMagickException (
2736 pfx->exception, GetMagickModule(), OptionError,
2737 "Expected operand at",
"'%s'",
2742 if (NewUserSymbol && !Assign) {
2743 (void) ThrowMagickException (
2744 pfx->exception, GetMagickModule(), OptionError,
2745 "NewUserSymbol",
"'%s' after non-assignment operator at '%s'",
2746 pfx->token, SetShortExp(pfx));
2749 if (UserSymbol && !NewUserSymbol) {
2750 (void) AddAddressingElement (pfx, rCopyFrom, UserSymNdx1);
2751 UserSymNdx1 = NULL_ADDRESS;
2753 UserSymNdx0 = UserSymNdx1;
2756 if (UserSymbol && !Assign && !Update && UserSymNdx0 != NULL_ADDRESS) {
2758 if (NewUserSymbol) {
2759 (void) ThrowMagickException (
2760 pfx->exception, GetMagickModule(), OptionError,
2761 "NewUserSymbol",
"'%s' needs assignment operator at '%s'",
2762 pfx->token, SetShortExp(pfx));
2765 (void) AddAddressingElement (pfx, rCopyFrom, UserSymNdx0);
2766 pel = &pfx->Elements[pfx->usedElements-1];
2767 pel->do_push = MagickTrue;
2770 if (*pfx->pex && !*chLimit && (strchr(strLimit,*pfx->pex)!=NULL)) {
2771 *chLimit = *pfx->pex;
2774 while (pfx->usedOprStack) {
2775 OperatorE op = pfx->OperatorStack[pfx->usedOprStack-1];
2776 if (op == oOpenParen || op == oOpenBracket || op == oOpenBrace) {
2779 if ( (op==oAssign && !Assign) || (OprInPlace((
int) op) && !Update) ) {
2782 pfx->usedOprStack--;
2783 (void) AddElement (pfx, (fxFltType) 0, (int) op);
2784 if (op == oAssign) {
2785 if (UserSymNdx0 < 0) {
2786 (void) ThrowMagickException (
2787 pfx->exception, GetMagickModule(), OptionError,
2788 "Assignment to unknown user symbol at",
"'%s'",
2794 pfx->usedElements--;
2795 (void) AddAddressingElement (pfx, rCopyTo, UserSymNdx0);
2797 }
else if (OprInPlace ((
int) op)) {
2798 if (UserSymNdx0 < 0) {
2799 (void) ThrowMagickException (
2800 pfx->exception, GetMagickModule(), OptionError,
2801 "Operator-in-place to unknown user symbol at",
"'%s'",
2807 pfx->Elements[pfx->usedElements-1].element_index = UserSymNdx0;
2812 if (ternary.addr_query != NULL_ADDRESS) *needPopAll = MagickTrue;
2814 (void) ResolveTernaryAddresses (pfx, &ternary);
2818 if (!pfx->teDepth && *needPopAll) {
2819 (void) AddAddressingElement (pfx, rZerStk, NULL_ADDRESS);
2820 *needPopAll = MagickFalse;
2823 if (pfx->exception->severity >= ErrorException)
2830static MagickBooleanType TranslateStatement (
FxInfo * pfx,
char * strLimit,
char * chLimit)
2832 MagickBooleanType NeedPopAll = MagickFalse;
2836 if (!*pfx->pex)
return MagickFalse;
2838 if (!TranslateExpression (pfx, strLimit, chLimit, &NeedPopAll)) {
2841 if (pfx->usedElements && *chLimit==
';') {
2846 ElementT * pel = &pfx->Elements[pfx->usedElements-1];
2847 if (pel->do_push) pel->do_push = MagickFalse;
2853static MagickBooleanType TranslateStatementList (
FxInfo * pfx,
const char * strLimit,
char * chLimit)
2855#define MAX_SLIMIT 10
2856 char sLimits[MAX_SLIMIT];
2859 if (!*pfx->pex)
return MagickFalse;
2860 (void) CopyMagickString (sLimits, strLimit, MAX_SLIMIT-1);
2862 if (strchr(strLimit,
';')==NULL)
2863 (
void) ConcatenateMagickString (sLimits,
";", MAX_SLIMIT);
2866 if (!TranslateStatement (pfx, sLimits, chLimit))
return MagickFalse;
2868 if (!*pfx->pex)
break;
2870 if (*chLimit !=
';') {
2875 if (pfx->exception->severity >= ErrorException)
2894 for (ch=0; ch <= (int) MaxPixelChannels; ch++) {
2895 cs[ch].mean *= QuantumScale;
2896 cs[ch].median *= QuantumScale;
2897 cs[ch].maxima *= QuantumScale;
2898 cs[ch].minima *= QuantumScale;
2899 cs[ch].standard_deviation *= QuantumScale;
2905static MagickBooleanType CollectStatistics (
FxInfo * pfx)
2907 Image * img = GetFirstImageInList (pfx->image);
2912 if (!pfx->statistics) {
2913 (void) ThrowMagickException (
2914 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
2915 "Statistics",
"%lu",
2916 (
unsigned long) pfx->ImgListLen);
2921 pfx->statistics[imgNum] = CollectOneImgStats (pfx, img);
2923 if (++imgNum == pfx->ImgListLen)
break;
2924 img = GetNextImageInList (img);
2925 assert (img != (
Image *) NULL);
2927 pfx->GotStats = MagickTrue;
2932static inline MagickBooleanType PushVal (
FxInfo * pfx,
fxRtT * pfxrt, fxFltType val,
int addr)
2934 if (pfxrt->usedValStack >=pfxrt->numValStack) {
2935 (void) ThrowMagickException (
2936 pfx->exception, GetMagickModule(), OptionError,
2937 "ValStack overflow at addr=",
"%i",
2942 pfxrt->ValStack[pfxrt->usedValStack++] = val;
2946static inline fxFltType PopVal (
FxInfo * pfx,
fxRtT * pfxrt,
int addr)
2948 if (pfxrt->usedValStack <= 0) {
2949 (void) ThrowMagickException (
2950 pfx->exception, GetMagickModule(), OptionError,
2951 "ValStack underflow at addr=",
"%i",
2953 return (fxFltType) 0;
2956 return pfxrt->ValStack[--pfxrt->usedValStack];
2959static inline fxFltType ImageStat (
2960 FxInfo * pfx, ssize_t ImgNum, PixelChannel channel, ImgAttrE ia)
2964 MagickBooleanType NeedRelinq = MagickFalse;
2968 (void) ThrowMagickException(pfx->exception,GetMagickModule(),
2969 OptionError,
"NoSuchImage",
"%lu",(
unsigned long) ImgNum);
2973 if (pfx->GotStats) {
2974 if ((channel < 0) || (channel > MaxPixelChannels))
2976 (void) ThrowMagickException(pfx->exception,GetMagickModule(),
2977 OptionError,
"NoSuchImageChannel",
"%i",channel);
2978 channel=(PixelChannel) 0;
2980 cs = pfx->statistics[ImgNum];
2981 }
else if (pfx->NeedStats) {
2983 if ((channel < 0) || (channel > MaxPixelChannels))
2985 (void) ThrowMagickException(pfx->exception,GetMagickModule(),
2986 OptionError,
"NoSuchImageChannel",
"%i",channel);
2987 channel=(PixelChannel) 0;
2989 cs = CollectOneImgStats (pfx, pfx->Images[ImgNum]);
2990 NeedRelinq = MagickTrue;
2995 ret = (fxFltType) GetImageDepth (pfx->Images[ImgNum], pfx->exception);
2998 ret = (fxFltType) GetBlobSize (pfx->image);
3002 ret = cs[channel].kurtosis;
3006 ret = cs[channel].maxima;
3010 ret = cs[channel].mean;
3014 ret = cs[channel].median;
3018 ret = cs[channel].minima;
3024 ret = (fxFltType) pfx->Images[ImgNum]->page.x;
3027 ret = (fxFltType) pfx->Images[ImgNum]->page.y;
3030 ret = (fxFltType) pfx->Images[ImgNum]->page.width;
3033 ret = (fxFltType) pfx->Images[ImgNum]->page.height;
3039 ret = (fxFltType) PerceptibleReciprocal (pfx->Images[ImgNum]->resolution.x)
3040 * pfx->Images[ImgNum]->columns;
3043 ret = (fxFltType) PerceptibleReciprocal (pfx->Images[ImgNum]->resolution.y)
3044 * pfx->Images[ImgNum]->rows;
3047 ret = (fxFltType) pfx->Images[ImgNum]->quality;
3053 ret = pfx->Images[ImgNum]->resolution.x;
3056 ret = pfx->Images[ImgNum]->resolution.y;
3060 ret = cs[channel].skewness;
3064 ret = cs[channel].standard_deviation;
3067 ret = (fxFltType) pfx->Images[ImgNum]->rows;
3070 ret = (fxFltType) pfx->ImgListLen;
3073 ret = (fxFltType) ImgNum;
3076 ret = (fxFltType) pfx->Images[ImgNum]->columns;
3079 ret = (fxFltType) GetImageDepth (pfx->Images[ImgNum], pfx->exception);
3082 (void) ThrowMagickException (pfx->exception,GetMagickModule(),OptionError,
3083 "Unknown ia=",
"%i",ia);
3090static inline fxFltType FxGcd (fxFltType x, fxFltType y,
const size_t depth)
3092#define FxMaxFunctionDepth 200
3095 return (FxGcd (y, x, depth+1));
3096 if ((fabs((
double) y) < 0.001) || (depth >= FxMaxFunctionDepth))
3098 return (FxGcd (y, x-y*floor((
double) (x/y)), depth+1));
3101static inline ssize_t ChkImgNum (
FxInfo * pfx, fxFltType f)
3104 ssize_t i = (ssize_t) floor ((
double) f + 0.5);
3105 if (i < 0) i += (ssize_t) pfx->ImgListLen;
3106 if (i < 0 || i >= (ssize_t) pfx->ImgListLen) {
3107 (void) ThrowMagickException (
3108 pfx->exception, GetMagickModule(), OptionError,
3109 "ImgNum",
"%lu bad for ImgListLen %lu",
3110 (
unsigned long) i, (
unsigned long) pfx->ImgListLen);
3116#define WHICH_ATTR_CHAN \
3117 (pel->channel_qual == NO_CHAN_QUAL) ? CompositePixelChannel : \
3118 (pel->channel_qual == THIS_CHANNEL) ? channel : pel->channel_qual
3120#define WHICH_NON_ATTR_CHAN \
3121 (pel->channel_qual == NO_CHAN_QUAL || \
3122 pel->channel_qual == THIS_CHANNEL || \
3123 pel->channel_qual == CompositePixelChannel \
3124 ) ? (channel == CompositePixelChannel ? RedPixelChannel: channel) \
3127static fxFltType GetHslFlt (
FxInfo * pfx, ssize_t ImgNum,
const fxFltType fx,
const fxFltType fy,
3128 PixelChannel channel)
3130 Image * img = pfx->Images[ImgNum];
3132 double red, green, blue;
3133 double hue=0, saturation=0, lightness=0;
3135 MagickBooleanType okay = MagickTrue;
3136 if(!InterpolatePixelChannel (img, pfx->Imgs[ImgNum].View, RedPixelChannel, img->interpolate,
3137 (
double) fx, (
double) fy, &red, pfx->exception)) okay = MagickFalse;
3138 if(!InterpolatePixelChannel (img, pfx->Imgs[ImgNum].View, GreenPixelChannel, img->interpolate,
3139 (
double) fx, (
double) fy, &green, pfx->exception)) okay = MagickFalse;
3140 if(!InterpolatePixelChannel (img, pfx->Imgs[ImgNum].View, BluePixelChannel, img->interpolate,
3141 (
double) fx, (
double) fy, &blue, pfx->exception)) okay = MagickFalse;
3144 (void) ThrowMagickException (
3145 pfx->exception, GetMagickModule(), OptionError,
3146 "GetHslFlt failure",
"%lu %g,%g %i", (
unsigned long) ImgNum,
3147 (
double) fx, (double) fy, channel);
3151 &hue, &saturation, &lightness);
3153 if (channel == HUE_CHANNEL)
return hue;
3154 if (channel == SAT_CHANNEL)
return saturation;
3155 if (channel == LIGHT_CHANNEL)
return lightness;
3160static fxFltType GetHslInt (
FxInfo * pfx, ssize_t ImgNum,
const ssize_t imgx,
const ssize_t imgy, PixelChannel channel)
3162 Image * img = pfx->Images[ImgNum];
3164 double hue=0, saturation=0, lightness=0;
3166 const Quantum * p = GetCacheViewVirtualPixels (pfx->Imgs[ImgNum].View, imgx, imgy, 1, 1, pfx->exception);
3167 if (p == (
const Quantum *) NULL)
3169 (void) ThrowMagickException (pfx->exception,GetMagickModule(),
3170 OptionError,
"GetHslInt failure",
"%lu %li,%li %i",(
unsigned long) ImgNum,
3171 (
long) imgx,(long) imgy,channel);
3176 GetPixelRed (img, p), GetPixelGreen (img, p), GetPixelBlue (img, p),
3177 &hue, &saturation, &lightness);
3179 if (channel == HUE_CHANNEL)
return hue;
3180 if (channel == SAT_CHANNEL)
return saturation;
3181 if (channel == LIGHT_CHANNEL)
return lightness;
3186static inline fxFltType GetIntensity (
FxInfo * pfx, ssize_t ImgNum,
const fxFltType fx,
const fxFltType fy)
3189 quantum_pixel[MaxPixelChannels];
3194 Image * img = pfx->Images[ImgNum];
3196 (void) GetPixelInfo (img, &pixelinf);
3198 if (!InterpolatePixelInfo (img, pfx->Imgs[pfx->ImgNum].View, img->interpolate,
3199 (
double) fx, (
double) fy, &pixelinf, pfx->exception))
3201 (void) ThrowMagickException (
3202 pfx->exception, GetMagickModule(), OptionError,
3203 "GetIntensity failure",
"%lu %g,%g", (
unsigned long) ImgNum,
3204 (
double) fx, (double) fy);
3207 SetPixelViaPixelInfo (img, &pixelinf, quantum_pixel);
3208 return QuantumScale * GetPixelIntensity (img, quantum_pixel);
3211static MagickBooleanType ExecuteRPN (
FxInfo * pfx,
fxRtT * pfxrt, fxFltType *result,
3212 const PixelChannel channel,
const ssize_t imgx,
const ssize_t imgy)
3214 const Quantum * p = pfxrt->thisPixel;
3215 fxFltType regA=0, regB=0, regC=0, regD=0, regE=0;
3216 Image * img = pfx->image;
3218 MagickBooleanType NeedRelinq = MagickFalse;
3219 double hue=0, saturation=0, lightness=0;
3226 if (!p) p = GetCacheViewVirtualPixels (
3227 pfx->Imgs[pfx->ImgNum].View, imgx, imgy, 1, 1, pfx->exception);
3229 if (p == (
const Quantum *) NULL)
3231 (void) ThrowMagickException (pfx->exception,GetMagickModule(),
3232 OptionError,
"Can't get virtual pixels",
"%lu %li,%li",(
unsigned long)
3233 pfx->ImgNum,(
long) imgx,(long) imgy);
3234 return(MagickFalse);
3237 if (pfx->GotStats) {
3238 cs = pfx->statistics[pfx->ImgNum];
3239 }
else if (pfx->NeedStats) {
3240 cs = CollectOneImgStats (pfx, pfx->Images[pfx->ImgNum]);
3241 NeedRelinq = MagickTrue;
3248 GetPixelRed (img, p), GetPixelGreen (img, p), GetPixelBlue (img, p),
3249 &hue, &saturation, &lightness);
3252 for (i=0; i < pfx->usedElements; i++) {
3257 (void) ThrowMagickException (
3258 pfx->exception, GetMagickModule(), OptionError,
3259 "Bad run-time address",
"%i", i);
3261 pel=&pfx->Elements[i];
3262 switch (pel->number_args) {
3266 regA = PopVal (pfx, pfxrt, i);
3269 regB = PopVal (pfx, pfxrt, i);
3270 regA = PopVal (pfx, pfxrt, i);
3273 regC = PopVal (pfx, pfxrt, i);
3274 regB = PopVal (pfx, pfxrt, i);
3275 regA = PopVal (pfx, pfxrt, i);
3278 regD = PopVal (pfx, pfxrt, i);
3279 regC = PopVal (pfx, pfxrt, i);
3280 regB = PopVal (pfx, pfxrt, i);
3281 regA = PopVal (pfx, pfxrt, i);
3284 regE = PopVal (pfx, pfxrt, i);
3285 regD = PopVal (pfx, pfxrt, i);
3286 regC = PopVal (pfx, pfxrt, i);
3287 regB = PopVal (pfx, pfxrt, i);
3288 regA = PopVal (pfx, pfxrt, i);
3291 (void) ThrowMagickException (
3292 pfx->exception, GetMagickModule(), OptionError,
3293 "Too many args:",
"%i", pel->number_args);
3297 switch (pel->operator_index) {
3299 regA = (pfxrt->UserSymVals[pel->element_index] += regA);
3302 regA = (pfxrt->UserSymVals[pel->element_index] -= regA);
3305 regA = (pfxrt->UserSymVals[pel->element_index] *= regA);
3308 regA = (pfxrt->UserSymVals[pel->element_index] *= PerceptibleReciprocal((
double)regA));
3311 regA = pfxrt->UserSymVals[pel->element_index]++;
3314 regA = pfxrt->UserSymVals[pel->element_index]--;
3326 regA *= PerceptibleReciprocal((
double)regB);
3329 regA = fmod ((
double) regA, fabs(floor((
double) regB+0.5)));
3338 if ((
size_t) (regB+0.5) >= (8*
sizeof(
size_t)))
3340 (void) ThrowMagickException ( pfx->exception, GetMagickModule(),
3341 OptionError,
"undefined shift",
"%g", (double) regB);
3342 regA = (fxFltType) 0.0;
3345 regA = (fxFltType) ((
size_t)(regA+0.5) << (
size_t)(regB+0.5));
3348 if ((
size_t) (regB+0.5) >= (8*
sizeof(
size_t)))
3350 (void) ThrowMagickException ( pfx->exception, GetMagickModule(),
3351 OptionError,
"undefined shift",
"%g", (double) regB);
3352 regA = (fxFltType) 0.0;
3355 regA = (fxFltType) ((
size_t)(regA+0.5) >> (
size_t)(regB+0.5));
3358 regA = fabs((
double) (regA-regB)) < MagickEpsilon ? 1.0 : 0.0;
3361 regA = fabs((
double) (regA-regB)) >= MagickEpsilon ? 1.0 : 0.0;
3364 regA = (regA <= regB) ? 1.0 : 0.0;
3367 regA = (regA >= regB) ? 1.0 : 0.0;
3370 regA = (regA < regB) ? 1.0 : 0.0;
3373 regA = (regA > regB) ? 1.0 : 0.0;
3376 regA = (regA<=0) ? 0.0 : (regB > 0) ? 1.0 : 0.0;
3379 regA = (regA>0) ? 1.0 : (regB > 0.0) ? 1.0 : 0.0;
3382 regA = (regA==0) ? 1.0 : 0.0;
3385 regA = (fxFltType) ((
size_t)(regA+0.5) & (
size_t)(regB+0.5));
3388 regA = (fxFltType) ((
size_t)(regA+0.5) | (
size_t)(regB+0.5));
3392 regA = (fxFltType) (~(
size_t)(regA+0.5));
3395 regA = pow ((
double) regA, (
double) regB);
3411 if (pel->type == etColourConstant) {
3412 switch (channel) {
default:
3413 case (PixelChannel) 0:
3416 case (PixelChannel) 1:
3419 case (PixelChannel) 2:
3429 regA = fabs ((
double) regA);
3431#if defined(MAGICKCORE_HAVE_ACOSH)
3433 regA = acosh ((
double) regA);
3437 regA = acos ((
double) regA);
3439#if defined(MAGICKCORE_HAVE_J1)
3441 if (regA==0) regA = 1.0;
3443 fxFltType gamma = 2.0 * j1 ((MagickPI*regA)) / (MagickPI*regA);
3444 regA = gamma * gamma;
3449 regA = (fxFltType) (((ssize_t) regA) & 0x01 ? -1.0 : 1.0);
3451#if defined(MAGICKCORE_HAVE_ASINH)
3453 regA = asinh ((
double) regA);
3457 regA = asin ((
double) regA);
3459#if defined(MAGICKCORE_HAVE_ATANH)
3461 regA = atanh ((
double) regA);
3465 regA = atan2 ((
double) regA, (
double) regB);
3468 regA = atan ((
double) regA);
3471 regA = ceil ((
double) regA);
3475 case (PixelChannel) 0:
break;
3476 case (PixelChannel) 1: regA = regB;
break;
3477 case (PixelChannel) 2: regA = regC;
break;
3478 case (PixelChannel) 3: regA = regD;
break;
3479 case (PixelChannel) 4: regA = regE;
break;
3480 default: regA = 0.0;
3484 if (regA < 0) regA = 0.0;
3485 else if (regA > 1.0) regA = 1.0;
3488 regA = cosh ((
double) regA);
3491 regA = cos ((
double) regA);
3496 (void) fprintf (stderr,
"%s[%g,%g].[%i]: %s=%.*g\n",
3497 img->filename, (
double) imgx, (double) imgy,
3498 channel, SetPtrShortExp (pfx, pel->exp_start, (
size_t) (pel->exp_len+1)),
3499 pfx->precision, (double) regA);
3502 regA = regA / (regB*(regA-1.0) + 1.0);
3504#if defined(MAGICKCORE_HAVE_ERF)
3506 regA = erf ((
double) regA);
3510 regA = exp ((
double) regA);
3513 regA = floor ((
double) regA);
3516 regA = exp((
double) (-regA*regA/2.0))/sqrt(2.0*MagickPI);
3520 regA = FxGcd (regA, regB, 0);
3523 regA = hypot ((
double) regA, (
double) regB);
3526 regA = floor ((
double) regA);
3529 regA = (fxFltType) (!!IsNaN (regA));
3531#if defined(MAGICKCORE_HAVE_J0)
3533 regA = j0 ((
double) regA);
3536#if defined(MAGICKCORE_HAVE_J1)
3538 regA = j1 ((
double) regA);
3541#if defined(MAGICKCORE_HAVE_J1)
3543 if (regA==0) regA = 1.0;
3544 else regA = 2.0 * j1 ((MagickPI*regA))/(MagickPI*regA);
3548 regA = log ((
double) regA);
3551 regA = MagickLog10((
double) regA) / log10(2.0);
3554 regA = MagickLog10 ((
double) regA);
3557 regA = (regA > regB) ? regA : regB;
3560 regA = (regA < regB) ? regA : regB;
3563 regA = regA - floor((
double) (regA*PerceptibleReciprocal((
double) regB)))*regB;
3566 regA = (fxFltType) (regA < MagickEpsilon);
3569 regA = pow ((
double) regA, (
double) regB);
3572#if defined(MAGICKCORE_OPENMP_SUPPORT)
3573 #pragma omp critical (MagickCore_ExecuteRPN)
3575 regA = GetPseudoRandomValue (pfxrt->random_info);
3579 regA = floor ((
double) regA + 0.5);
3582 regA = (regA < 0) ? -1.0 : 1.0;
3585 regA = sin ((
double) (MagickPI*regA)) / (MagickPI*regA);
3588 regA = sinh ((
double) regA);
3591 regA = sin ((
double) regA);
3594 regA = sqrt ((
double) regA);
3597 regA = 1.0 / (1.0 + exp ((
double) -regA));
3600 regA = tanh ((
double) regA);
3603 regA = tan ((
double) regA);
3606 if (regA >= 0) regA = floor ((
double) regA);
3607 else regA = ceil ((
double) regA);
3619 ssize_t ImgNum = ChkImgNum (pfx, regA);
3620 if (ImgNum < 0)
break;
3621 regA = (fxFltType) 0;
3623 Image * pimg = pfx->Images[0];
3624 if (pel->img_attr_qual == aNull) {
3625 if ((
int) pel->channel_qual < 0) {
3626 if (pel->channel_qual == NO_CHAN_QUAL || pel->channel_qual == THIS_CHANNEL) {
3627 if (pfx->ImgNum==0) {
3628 regA = QuantumScale * (double) p[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3630 const Quantum * pv = GetCacheViewVirtualPixels (
3631 pfx->Imgs[0].View, imgx, imgy, 1,1, pfx->exception);
3633 (void) ThrowMagickException (
3634 pfx->exception, GetMagickModule(), OptionError,
3635 "fU can't get cache",
"%lu", (
unsigned long) ImgNum);
3638 regA = QuantumScale * (double) pv[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3640 }
else if (pel->channel_qual == HUE_CHANNEL || pel->channel_qual == SAT_CHANNEL ||
3641 pel->channel_qual == LIGHT_CHANNEL) {
3642 regA = GetHslInt (pfx, ImgNum, imgx, imgy, pel->channel_qual);
3644 }
else if (pel->channel_qual == INTENSITY_CHANNEL) {
3645 regA = GetIntensity (pfx, 0, (
double) imgx, (
double) imgy);
3649 if (pfx->ImgNum==0) {
3650 regA = QuantumScale * (double) p[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3652 const Quantum * pv = GetCacheViewVirtualPixels (
3653 pfx->Imgs[0].View, imgx, imgy, 1,1, pfx->exception);
3655 (void) ThrowMagickException (
3656 pfx->exception, GetMagickModule(), OptionError,
3657 "fU can't get cache",
"%lu", (
unsigned long) ImgNum);
3660 regA = QuantumScale * (double) pv[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3665 regA = ImageStat (pfx, 0, WHICH_ATTR_CHAN, pel->img_attr_qual);
3669 if (pel->img_attr_qual == aNull) {
3671 if ((
int) pel->channel_qual < 0) {
3672 if (pel->channel_qual == HUE_CHANNEL || pel->channel_qual == SAT_CHANNEL ||
3673 pel->channel_qual == LIGHT_CHANNEL)
3675 regA = GetHslInt (pfx, ImgNum, imgx, imgy, pel->channel_qual);
3677 }
else if (pel->channel_qual == INTENSITY_CHANNEL)
3679 regA = GetIntensity (pfx, ImgNum, (fxFltType) imgx, (fxFltType) imgy);
3684 pv = GetCacheViewVirtualPixels (
3685 pfx->Imgs[ImgNum].View, imgx, imgy, 1,1, pfx->exception);
3687 (void) ThrowMagickException (
3688 pfx->exception, GetMagickModule(), OptionError,
3689 "fU can't get cache",
"%lu", (
unsigned long) ImgNum);
3692 regA = QuantumScale * (double)
3693 pv[pfx->Images[ImgNum]->channel_map[WHICH_NON_ATTR_CHAN].offset];
3695 regA = ImageStat (pfx, ImgNum, WHICH_ATTR_CHAN, pel->img_attr_qual);
3704 Image * pimg = pfx->Images[0];
3705 if ((
int) pel->channel_qual < 0) {
3706 if (pel->channel_qual == NO_CHAN_QUAL || pel->channel_qual == THIS_CHANNEL) {
3708 if (pfx->ImgNum==0) {
3709 regA = QuantumScale * (double) p[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3711 const Quantum * pv = GetCacheViewVirtualPixels (
3712 pfx->Imgs[0].View, imgx, imgy, 1,1, pfx->exception);
3714 (void) ThrowMagickException (
3715 pfx->exception, GetMagickModule(), OptionError,
3716 "fU0 can't get cache",
"%i", 0);
3719 regA = QuantumScale * (double) pv[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3722 }
else if (pel->channel_qual == HUE_CHANNEL || pel->channel_qual == SAT_CHANNEL ||
3723 pel->channel_qual == LIGHT_CHANNEL) {
3724 regA = GetHslInt (pfx, 0, imgx, imgy, pel->channel_qual);
3726 }
else if (pel->channel_qual == INTENSITY_CHANNEL) {
3727 regA = GetIntensity (pfx, 0, (fxFltType) imgx, (fxFltType) imgy);
3730 if (pfx->ImgNum==0) {
3731 regA = QuantumScale * (double) p[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3733 const Quantum * pv = GetCacheViewVirtualPixels (
3734 pfx->Imgs[0].View, imgx, imgy, 1,1, pfx->exception);
3736 (void) ThrowMagickException (
3737 pfx->exception, GetMagickModule(), OptionError,
3738 "fU0 can't get cache",
"%i", 0);
3741 regA = QuantumScale * (double) pv[pimg->channel_map[WHICH_NON_ATTR_CHAN].offset];
3748 ssize_t ImgNum = ChkImgNum (pfx, regA);
3751 if (ImgNum < 0)
break;
3753 if (pel->is_relative) {
3761 if ((
int) pel->channel_qual < 0) {
3762 if (pel->channel_qual == HUE_CHANNEL || pel->channel_qual == SAT_CHANNEL
3763 || pel->channel_qual == LIGHT_CHANNEL) {
3764 regA = GetHslFlt (pfx, ImgNum, fx, fy, pel->channel_qual);
3766 }
else if (pel->channel_qual == INTENSITY_CHANNEL) {
3767 regA = GetIntensity (pfx, ImgNum, fx, fy);
3774 Image * imUP = pfx->Images[ImgNum];
3775 if (! InterpolatePixelChannel (imUP, pfx->Imgs[ImgNum].View, WHICH_NON_ATTR_CHAN,
3776 imUP->interpolate, (
double) fx, (
double) fy, &v, pfx->exception))
3778 (void) ThrowMagickException (
3779 pfx->exception, GetMagickModule(), OptionError,
3780 "fUP can't get interpolate",
"%lu", (
unsigned long) ImgNum);
3783 regA = v * QuantumScale;
3792 if (pel->operator_index == fS) ImgNum = pfx->ImgNum;
3794 if (pel->img_attr_qual == aNull) {
3795 const Quantum * pv = GetCacheViewVirtualPixels (
3796 pfx->Imgs[ImgNum].View, imgx, imgy, 1,1, pfx->exception);
3798 (void) ThrowMagickException (
3799 pfx->exception, GetMagickModule(), OptionError,
3800 "fV can't get cache",
"%lu", (
unsigned long) ImgNum);
3804 if ((
int) pel->channel_qual < 0) {
3805 if (pel->channel_qual == HUE_CHANNEL || pel->channel_qual == SAT_CHANNEL ||
3806 pel->channel_qual == LIGHT_CHANNEL) {
3807 regA = GetHslInt (pfx, ImgNum, imgx, imgy, pel->channel_qual);
3809 }
else if (pel->channel_qual == INTENSITY_CHANNEL) {
3810 regA = GetIntensity (pfx, ImgNum, (
double) imgx, (
double) imgy);
3815 regA = QuantumScale * (double)
3816 pv[pfx->Images[ImgNum]->channel_map[WHICH_NON_ATTR_CHAN].offset];
3818 regA = ImageStat (pfx, ImgNum, WHICH_ATTR_CHAN, pel->img_attr_qual);
3828 ssize_t ImgNum = pfx->ImgNum;
3829 if (pel->operator_index == fVP) ImgNum = 1;
3830 if (pel->is_relative) {
3837 if ((
int) pel->channel_qual < 0) {
3838 if (pel->channel_qual == HUE_CHANNEL || pel->channel_qual == SAT_CHANNEL ||
3839 pel->channel_qual == LIGHT_CHANNEL) {
3840 regA = GetHslFlt (pfx, ImgNum, fx, fy, pel->channel_qual);
3842 }
else if (pel->channel_qual == INTENSITY_CHANNEL) {
3843 regA = GetIntensity (pfx, ImgNum, fx, fy);
3851 if (! InterpolatePixelChannel (pfx->Images[ImgNum], pfx->Imgs[ImgNum].View,
3852 WHICH_NON_ATTR_CHAN, pfx->Images[ImgNum]->interpolate,
3853 (
double) fx, (
double) fy, &v, pfx->exception)
3856 (void) ThrowMagickException (
3857 pfx->exception, GetMagickModule(), OptionError,
3858 "fSP or fVP can't get interp",
"%lu", (
unsigned long) ImgNum);
3861 regA = v * (fxFltType)QuantumScale;
3869 regA = (fxFltType) GetImageDepth (img, pfx->exception);
3872 regA = (fxFltType) img->extent;
3876 regA = cs[WHICH_ATTR_CHAN].kurtosis;
3880 regA = cs[WHICH_ATTR_CHAN].maxima;
3884 regA = cs[WHICH_ATTR_CHAN].mean;
3888 regA = cs[WHICH_ATTR_CHAN].median;
3892 regA = cs[WHICH_ATTR_CHAN].minima;
3897 regA = (fxFltType) img->page.x;
3900 regA = (fxFltType) img->page.y;
3903 regA = (fxFltType) img->page.width;
3906 regA = (fxFltType) img->page.height;
3911 regA = (fxFltType) PerceptibleReciprocal (img->resolution.x) * img->columns;
3914 regA = (fxFltType) PerceptibleReciprocal (img->resolution.y) * img->rows;
3917 regA = (fxFltType) img->quality;
3922 regA = (fxFltType) img->resolution.x;
3925 regA = (fxFltType) img->resolution.y;
3929 regA = cs[WHICH_ATTR_CHAN].skewness;
3933 regA = cs[WHICH_ATTR_CHAN].standard_deviation;
3936 regA = (fxFltType) img->rows;
3939 regA = (fxFltType) pfx->ImgListLen;
3942 regA = (fxFltType) pfx->ImgNum;
3945 regA = (fxFltType) img->columns;
3948 regA = (fxFltType) GetImageDepth (img, pfx->exception);
3956 regA = GetIntensity (pfx, pfx->ImgNum, (
double) imgx, (
double) imgy);
3963 regA = QuantumScale * (0.212656 * (double) GetPixelRed (img,p) +
3964 0.715158 * (double) GetPixelGreen (img,p) +
3965 0.072186 * (double) GetPixelBlue (img,p));
3971 regA = QuantumScale * (double) GetPixelAlpha (img, p);
3974 regA = QuantumScale * (double) GetPixelBlue (img, p);
3977 regA = QuantumScale * (double) GetPixelCyan (img, p);
3980 regA = QuantumScale * (double) GetPixelGreen (img, p);
3983 regA = (fxFltType) imgx;
3986 regA = (fxFltType) imgy;
3989 regA = QuantumScale * (double) GetPixelBlack (img, p);
3992 regA = QuantumScale * (double) GetPixelGreen (img, p);
3995 regA = QuantumScale * (double) GetPixelAlpha (img, p);
3998 regA = QuantumScale * (double) GetPixelRed (img, p);
4001 regA = QuantumScale * (double) GetPixelYellow (img, p);
4007 assert (pel->element_index >= 0);
4008 i = pel->element_index-1;
4011 assert (pel->element_index >= 0);
4012 i = pel->element_index-1;
4013 if (IsImageTTLExpired(img) != MagickFalse) {
4014 i = pfx->usedElements-1;
4015 (void) ThrowMagickException (pfx->exception, GetMagickModule(),
4016 ResourceLimitFatalError,
"TimeLimitExceeded",
"`%s'", img->filename);
4020 assert (pel->element_index >= 0);
4021 if (fabs((
double) regA) < MagickEpsilon) i = pel->element_index-1;
4023 case rIfNotZeroGoto:
4024 assert (pel->element_index >= 0);
4025 if (fabs((
double) regA) > MagickEpsilon) i = pel->element_index-1;
4028 assert (pel->element_index >= 0);
4029 regA = pfxrt->UserSymVals[pel->element_index];
4032 assert (pel->element_index >= 0);
4033 pfxrt->UserSymVals[pel->element_index] = regA;
4036 pfxrt->usedValStack = 0;
4042 (void) ThrowMagickException (
4043 pfx->exception, GetMagickModule(), OptionError,
4044 "pel->oprNum",
"%i '%s' not yet implemented",
4045 (int)pel->operator_index, OprStr(pel->operator_index));
4049 if (!PushVal (pfx, pfxrt, regA, i))
break;
4052 if (pfxrt->usedValStack > 0) regA = PopVal (pfx, pfxrt, 9999);
4058 if (pfx->exception->severity >= ErrorException)
4061 if (pfxrt->usedValStack != 0) {
4062 (void) ThrowMagickException (
4063 pfx->exception, GetMagickModule(), OptionError,
4064 "ValStack not empty",
"(%i)", pfxrt->usedValStack);
4073MagickPrivate MagickBooleanType FxEvaluateChannelExpression (
4075 const PixelChannel channel,
const ssize_t x,
const ssize_t y,
4079 id = GetOpenMPThreadId();
4083 assert (pfx != NULL);
4084 assert (pfx->image != NULL);
4085 assert (pfx->Images != NULL);
4086 assert (pfx->Imgs != NULL);
4087 assert (pfx->fxrts != NULL);
4089 pfx->fxrts[id].thisPixel = NULL;
4091 if (!ExecuteRPN (pfx, &pfx->fxrts[
id], &ret, channel, x, y)) {
4092 (void) ThrowMagickException (
4093 exception, GetMagickModule(), OptionError,
4094 "ExecuteRPN failed",
" ");
4098 *result = (double) ret;
4103static FxInfo *AcquireFxInfoPrivate (
const Image * images,
const char * expression,
4108 FxInfo * pfx = (
FxInfo*) AcquireCriticalMemory (
sizeof (*pfx));
4110 memset (pfx, 0,
sizeof (*pfx));
4112 if (!InitFx (pfx, images, CalcAllStats, exception)) {
4113 pfx = (
FxInfo*) RelinquishMagickMemory(pfx);
4117 if (!BuildRPN (pfx)) {
4118 (void) DeInitFx (pfx);
4119 pfx = (
FxInfo*) RelinquishMagickMemory(pfx);
4123 if ((*expression ==
'@') && (strlen(expression) > 1))
4124 pfx->expression=FileToString(expression,~0UL,exception);
4125 if (pfx->expression == (
char *) NULL)
4126 pfx->expression=ConstantString(expression);
4127 pfx->pex = (
char *) pfx->expression;
4130 if (!TranslateStatementList (pfx,
";", &chLimit)) {
4131 (void) DestroyRPN (pfx);
4132 pfx->expression = DestroyString (pfx->expression);
4134 (void) DeInitFx (pfx);
4135 pfx = (
FxInfo*) RelinquishMagickMemory(pfx);
4140 (void) ThrowMagickException (
4141 pfx->exception, GetMagickModule(), OptionError,
4142 "Translate expression depth",
"(%i) not 0",
4145 (void) DestroyRPN (pfx);
4146 pfx->expression = DestroyString (pfx->expression);
4148 (void) DeInitFx (pfx);
4149 pfx = (
FxInfo*) RelinquishMagickMemory(pfx);
4153 if (chLimit !=
'\0' && chLimit !=
';') {
4154 (void) ThrowMagickException (
4155 pfx->exception, GetMagickModule(), OptionError,
4156 "AcquireFxInfo: TranslateExpression did not exhaust input",
"(chLimit=%i) at'%s'",
4157 (int)chLimit, pfx->pex);
4159 (void) DestroyRPN (pfx);
4160 pfx->expression = DestroyString (pfx->expression);
4162 (void) DeInitFx (pfx);
4163 pfx = (
FxInfo*) RelinquishMagickMemory(pfx);
4167 if (pfx->NeedStats && pfx->runType == rtEntireImage && !pfx->statistics) {
4168 if (!CollectStatistics (pfx)) {
4169 (void) DestroyRPN (pfx);
4170 pfx->expression = DestroyString (pfx->expression);
4172 (void) DeInitFx (pfx);
4173 pfx = (
FxInfo*) RelinquishMagickMemory(pfx);
4178 if (pfx->DebugOpt) {
4179 DumpTables (stderr);
4180 DumpUserSymbols (pfx, stderr);
4181 (void) DumpRPN (pfx, stderr);
4185 size_t number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
4188 pfx->fxrts = (
fxRtT *)AcquireQuantumMemory (number_threads,
sizeof(
fxRtT));
4190 (void) ThrowMagickException (
4191 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
4193 (
unsigned long) number_threads);
4194 (void) DestroyRPN (pfx);
4195 pfx->expression = DestroyString (pfx->expression);
4197 (void) DeInitFx (pfx);
4198 pfx = (
FxInfo*) RelinquishMagickMemory(pfx);
4201 for (t=0; t < (ssize_t) number_threads; t++) {
4202 if (!AllocFxRt (pfx, &pfx->fxrts[t])) {
4203 (void) ThrowMagickException (
4204 pfx->exception, GetMagickModule(), ResourceLimitFatalError,
4205 "AllocFxRt t=",
"%g",
4209 for (t2 = t-1; t2 >= 0; t2--) {
4210 DestroyFxRt (&pfx->fxrts[t]);
4213 pfx->fxrts = (
fxRtT *) RelinquishMagickMemory (pfx->fxrts);
4214 (void) DestroyRPN (pfx);
4215 pfx->expression = DestroyString (pfx->expression);
4217 (void) DeInitFx (pfx);
4218 pfx = (
FxInfo*) RelinquishMagickMemory(pfx);
4228 return AcquireFxInfoPrivate (images, expression, MagickFalse, exception);
4235 assert (pfx != NULL);
4236 assert (pfx->image != NULL);
4237 assert (pfx->Images != NULL);
4238 assert (pfx->Imgs != NULL);
4239 assert (pfx->fxrts != NULL);
4241 for (t=0; t < (ssize_t) GetMagickResourceLimit(ThreadResource); t++) {
4242 DestroyFxRt (&pfx->fxrts[t]);
4244 pfx->fxrts = (
fxRtT *) RelinquishMagickMemory (pfx->fxrts);
4248 pfx->expression = DestroyString (pfx->expression);
4251 (void) DeInitFx (pfx);
4253 pfx = (
FxInfo*) RelinquishMagickMemory(pfx);
4260MagickExport
Image *FxImage(
const Image *image,
const char *expression,
4263#define FxImageTag "FxNew/Image"
4284 assert(image != (
Image *) NULL);
4285 assert(image->signature == MagickCoreSignature);
4286 if (IsEventLogging() != MagickFalse)
4287 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4288 if (expression == (
const char *) NULL)
4289 return(CloneImage(image,0,0,MagickTrue,exception));
4290 fx_image=CloneImage(image,0,0,MagickTrue,exception);
4291 if (!fx_image)
return NULL;
4292 if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse) {
4293 fx_image=DestroyImage(fx_image);
4297 pfx = AcquireFxInfoPrivate (image, expression, MagickTrue, exception);
4300 fx_image=DestroyImage(fx_image);
4304 assert (pfx->image != NULL);
4305 assert (pfx->Images != NULL);
4306 assert (pfx->Imgs != NULL);
4307 assert (pfx->fxrts != NULL);
4311 image_view = AcquireVirtualCacheView (image, pfx->exception);
4312 fx_view = AcquireAuthenticCacheView (fx_image, pfx->exception);
4313#if defined(MAGICKCORE_OPENMP_SUPPORT)
4314 #pragma omp parallel for schedule(dynamic) shared(progress,status) \
4315 magick_number_threads(image,fx_image,fx_image->rows, \
4316 pfx->ContainsDebug ? 0 : 1)
4318 for (y=0; y < (ssize_t) fx_image->rows; y++)
4321 id = GetOpenMPThreadId();
4335 if (status == MagickFalse)
4337 p = GetCacheViewVirtualPixels (image_view, 0, y, image->columns, 1, pfx->exception);
4338 q = QueueCacheViewAuthenticPixels (fx_view, 0, y, fx_image->columns, 1, pfx->exception);
4339 if ((p == (
const Quantum *) NULL) || (q == (Quantum *) NULL)) {
4343 for (x=0; x < (ssize_t) fx_image->columns; x++) {
4346 pfx->fxrts[id].thisPixel = (Quantum *)p;
4348 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4350 PixelChannel channel = GetPixelChannelChannel (image, i);
4351 PixelTrait traits = GetPixelChannelTraits (image, channel);
4352 PixelTrait fx_traits = GetPixelChannelTraits (fx_image, channel);
4353 if ((traits == UndefinedPixelTrait) ||
4354 (fx_traits == UndefinedPixelTrait))
4356 if ((fx_traits & CopyPixelTrait) != 0) {
4357 SetPixelChannel (fx_image, channel, p[i], q);
4361 if (!ExecuteRPN (pfx, &pfx->fxrts[
id], &result, channel, x, y)) {
4366 q[i] = ClampToQuantum ((MagickRealType) (QuantumRange*result));
4368 p+=(ptrdiff_t) GetPixelChannels (image);
4369 q+=(ptrdiff_t) GetPixelChannels (fx_image);
4371 if (SyncCacheViewAuthenticPixels(fx_view, pfx->exception) == MagickFalse)
4373 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4378#if defined(MAGICKCORE_OPENMP_SUPPORT)
4382 proceed = SetImageProgress (image, FxImageTag, progress, image->rows);
4383 if (proceed == MagickFalse)
4388 fx_view = DestroyCacheView (fx_view);
4389 image_view = DestroyCacheView (image_view);
4393 if (pfx->DebugOpt && pfx->usedUserSymbols) {
4395 char UserSym[MagickPathExtent];
4396 fprintf (stderr,
"User symbols (%i):\n", pfx->usedUserSymbols);
4397 for (t=0; t < (int) GetMagickResourceLimit(ThreadResource); t++) {
4398 for (i = 0; i < (int) pfx->usedUserSymbols; i++) {
4399 fprintf (stderr,
"th=%i us=%i '%s': %.*Lg\n",
4400 t, i, NameOfUserSym (pfx, i, UserSym), pfx->precision, pfx->fxrts[t].UserSymVals[i]);
4405 if ((status == MagickFalse) || (pfx->exception->severity >= ErrorException))
4406 fx_image=DestroyImage(fx_image);
4408 pfx=DestroyFxInfo(pfx);