1 //========================================================================
5 // Copyright 2001-2003 Glyph & Cog, LLC
7 //========================================================================
11 #ifdef USE_GCC_PRAGMAS
12 #pragma implementation
27 //------------------------------------------------------------------------
29 //------------------------------------------------------------------------
31 Function::Function() {
34 Function::~Function() {
37 Function *Function::parse(Object *funcObj) {
43 if (funcObj->isStream()) {
44 dict = funcObj->streamGetDict();
45 } else if (funcObj->isDict()) {
46 dict = funcObj->getDict();
47 } else if (funcObj->isName("Identity")) {
48 return new IdentityFunction();
50 error(-1, "Expected function dictionary or stream");
54 if (!dict->lookup("FunctionType", &obj1)->isInt()) {
55 error(-1, "Function type is missing or wrong type");
59 funcType = obj1.getInt();
63 func = new SampledFunction(funcObj, dict);
64 } else if (funcType == 2) {
65 func = new ExponentialFunction(funcObj, dict);
66 } else if (funcType == 3) {
67 func = new StitchingFunction(funcObj, dict);
68 } else if (funcType == 4) {
69 func = new PostScriptFunction(funcObj, dict);
71 error(-1, "Unimplemented function type (%d)", funcType);
82 GBool Function::init(Dict *dict) {
87 if (!dict->lookup("Domain", &obj1)->isArray()) {
88 error(-1, "Function is missing domain");
91 m = obj1.arrayGetLength() / 2;
92 if (m > funcMaxInputs) {
93 error(-1, "Functions with more than %d inputs are unsupported",
97 for (i = 0; i < m; ++i) {
98 obj1.arrayGet(2*i, &obj2);
100 error(-1, "Illegal value in function domain array");
103 domain[i][0] = obj2.getNum();
105 obj1.arrayGet(2*i+1, &obj2);
107 error(-1, "Illegal value in function domain array");
110 domain[i][1] = obj2.getNum();
118 if (dict->lookup("Range", &obj1)->isArray()) {
120 n = obj1.arrayGetLength() / 2;
121 if (n > funcMaxOutputs) {
122 error(-1, "Functions with more than %d outputs are unsupported",
126 for (i = 0; i < n; ++i) {
127 obj1.arrayGet(2*i, &obj2);
129 error(-1, "Illegal value in function range array");
132 range[i][0] = obj2.getNum();
134 obj1.arrayGet(2*i+1, &obj2);
136 error(-1, "Illegal value in function range array");
139 range[i][1] = obj2.getNum();
154 //------------------------------------------------------------------------
156 //------------------------------------------------------------------------
158 IdentityFunction::IdentityFunction() {
161 // fill these in with arbitrary values just in case they get used
165 for (i = 0; i < funcMaxInputs; ++i) {
172 IdentityFunction::~IdentityFunction() {
175 void IdentityFunction::transform(double *in, double *out) {
178 for (i = 0; i < funcMaxOutputs; ++i) {
183 //------------------------------------------------------------------------
185 //------------------------------------------------------------------------
187 SampledFunction::SampledFunction(Object *funcObj, Dict *dict) {
189 int nSamples, sampleBits;
200 //----- initialize the generic stuff
205 error(-1, "Type 0 function is missing range");
209 //----- get the stream
210 if (!funcObj->isStream()) {
211 error(-1, "Type 0 function isn't a stream");
214 str = funcObj->getStream();
217 if (!dict->lookup("Size", &obj1)->isArray() ||
218 obj1.arrayGetLength() != m) {
219 error(-1, "Function has missing or invalid size array");
222 for (i = 0; i < m; ++i) {
223 obj1.arrayGet(i, &obj2);
225 error(-1, "Illegal value in function size array");
228 sampleSize[i] = obj2.getInt();
233 //----- BitsPerSample
234 if (!dict->lookup("BitsPerSample", &obj1)->isInt()) {
235 error(-1, "Function has missing or invalid BitsPerSample");
238 sampleBits = obj1.getInt();
239 sampleMul = 1.0 / (double)((1 << sampleBits) - 1);
243 if (dict->lookup("Encode", &obj1)->isArray() &&
244 obj1.arrayGetLength() == 2*m) {
245 for (i = 0; i < m; ++i) {
246 obj1.arrayGet(2*i, &obj2);
248 error(-1, "Illegal value in function encode array");
251 encode[i][0] = obj2.getNum();
253 obj1.arrayGet(2*i+1, &obj2);
255 error(-1, "Illegal value in function encode array");
258 encode[i][1] = obj2.getNum();
262 for (i = 0; i < m; ++i) {
264 encode[i][1] = sampleSize[i] - 1;
270 if (dict->lookup("Decode", &obj1)->isArray() &&
271 obj1.arrayGetLength() == 2*n) {
272 for (i = 0; i < n; ++i) {
273 obj1.arrayGet(2*i, &obj2);
275 error(-1, "Illegal value in function decode array");
278 decode[i][0] = obj2.getNum();
280 obj1.arrayGet(2*i+1, &obj2);
282 error(-1, "Illegal value in function decode array");
285 decode[i][1] = obj2.getNum();
289 for (i = 0; i < n; ++i) {
290 decode[i][0] = range[i][0];
291 decode[i][1] = range[i][1];
298 for (i = 0; i < m; ++i)
299 nSamples *= sampleSize[i];
300 samples = (double *)gmalloc(nSamples * sizeof(double));
303 bitMask = (1 << sampleBits) - 1;
305 for (i = 0; i < nSamples; ++i) {
306 if (sampleBits == 8) {
308 } else if (sampleBits == 16) {
310 s = (s << 8) + str->getChar();
311 } else if (sampleBits == 32) {
313 s = (s << 8) + str->getChar();
314 s = (s << 8) + str->getChar();
315 s = (s << 8) + str->getChar();
317 while (bits < sampleBits) {
318 buf = (buf << 8) | (str->getChar() & 0xff);
321 s = (buf >> (bits - sampleBits)) & bitMask;
324 samples[i] = (double)s * sampleMul;
339 SampledFunction::~SampledFunction() {
345 SampledFunction::SampledFunction(SampledFunction *func) {
348 memcpy(this, func, sizeof(SampledFunction));
351 for (i = 0; i < m; ++i) {
352 nSamples *= sampleSize[i];
354 samples = (double *)gmalloc(nSamples * sizeof(double));
355 memcpy(samples, func->samples, nSamples * sizeof(double));
358 void SampledFunction::transform(double *in, double *out) {
360 int e[2][funcMaxInputs];
361 double efrac[funcMaxInputs];
362 double s0[1 << funcMaxInputs], s1[1 << funcMaxInputs];
365 // map input values into sample array
366 for (i = 0; i < m; ++i) {
367 x = ((in[i] - domain[i][0]) / (domain[i][1] - domain[i][0])) *
368 (encode[i][1] - encode[i][0]) + encode[i][0];
371 } else if (x > sampleSize[i] - 1) {
372 x = sampleSize[i] - 1;
374 e[0][i] = (int)floor(x);
375 e[1][i] = (int)ceil(x);
376 efrac[i] = x - e[0][i];
379 // for each output, do m-linear interpolation
380 for (i = 0; i < n; ++i) {
382 // pull 2^m values out of the sample array
383 for (j = 0; j < (1<<m); ++j) {
384 idx = e[j & 1][m - 1];
385 for (k = m - 2; k >= 0; --k) {
386 idx = idx * sampleSize[k] + e[(j >> k) & 1][k];
389 s0[j] = samples[idx];
392 // do m sets of interpolations
393 for (j = 0; j < m; ++j) {
394 for (k = 0; k < (1 << (m - j)); k += 2) {
395 s1[k >> 1] = (1 - efrac[j]) * s0[k] + efrac[j] * s0[k+1];
397 memcpy(s0, s1, (1 << (m - j - 1)) * sizeof(double));
400 // map output value to range
401 out[i] = s0[0] * (decode[i][1] - decode[i][0]) + decode[i][0];
402 if (out[i] < range[i][0]) {
403 out[i] = range[i][0];
404 } else if (out[i] > range[i][1]) {
405 out[i] = range[i][1];
410 //------------------------------------------------------------------------
411 // ExponentialFunction
412 //------------------------------------------------------------------------
414 ExponentialFunction::ExponentialFunction(Object *funcObj, Dict *dict) {
420 //----- initialize the generic stuff
425 error(-1, "Exponential function with more than one input");
430 if (dict->lookup("C0", &obj1)->isArray()) {
431 if (hasRange && obj1.arrayGetLength() != n) {
432 error(-1, "Function's C0 array is wrong length");
435 n = obj1.arrayGetLength();
436 for (i = 0; i < n; ++i) {
437 obj1.arrayGet(i, &obj2);
439 error(-1, "Illegal value in function C0 array");
442 c0[i] = obj2.getNum();
446 if (hasRange && n != 1) {
447 error(-1, "Function's C0 array is wrong length");
456 if (dict->lookup("C1", &obj1)->isArray()) {
457 if (obj1.arrayGetLength() != n) {
458 error(-1, "Function's C1 array is wrong length");
461 for (i = 0; i < n; ++i) {
462 obj1.arrayGet(i, &obj2);
464 error(-1, "Illegal value in function C1 array");
467 c1[i] = obj2.getNum();
472 error(-1, "Function's C1 array is wrong length");
480 if (!dict->lookup("N", &obj1)->isNum()) {
481 error(-1, "Function has missing or invalid N");
498 ExponentialFunction::~ExponentialFunction() {
501 ExponentialFunction::ExponentialFunction(ExponentialFunction *func) {
502 memcpy(this, func, sizeof(ExponentialFunction));
505 void ExponentialFunction::transform(double *in, double *out) {
509 if (in[0] < domain[0][0]) {
511 } else if (in[0] > domain[0][1]) {
516 for (i = 0; i < n; ++i) {
517 out[i] = c0[i] + pow(x, e) * (c1[i] - c0[i]);
519 if (out[i] < range[i][0]) {
520 out[i] = range[i][0];
521 } else if (out[i] > range[i][1]) {
522 out[i] = range[i][1];
529 //------------------------------------------------------------------------
531 //------------------------------------------------------------------------
533 StitchingFunction::StitchingFunction(Object *funcObj, Dict *dict) {
542 //----- initialize the generic stuff
547 error(-1, "Stitching function with more than one input");
552 if (!dict->lookup("Functions", &obj1)->isArray()) {
553 error(-1, "Missing 'Functions' entry in stitching function");
556 k = obj1.arrayGetLength();
557 funcs = (Function **)gmalloc(k * sizeof(Function *));
558 bounds = (double *)gmalloc((k + 1) * sizeof(double));
559 encode = (double *)gmalloc(2 * k * sizeof(double));
560 for (i = 0; i < k; ++i) {
563 for (i = 0; i < k; ++i) {
564 if (!(funcs[i] = Function::parse(obj1.arrayGet(i, &obj2)))) {
567 if (i > 0 && (funcs[i]->getInputSize() != 1 ||
568 funcs[i]->getOutputSize() != funcs[0]->getOutputSize())) {
569 error(-1, "Incompatible subfunctions in stitching function");
577 if (!dict->lookup("Bounds", &obj1)->isArray() ||
578 obj1.arrayGetLength() != k - 1) {
579 error(-1, "Missing or invalid 'Bounds' entry in stitching function");
582 bounds[0] = domain[0][0];
583 for (i = 1; i < k; ++i) {
584 if (!obj1.arrayGet(i - 1, &obj2)->isNum()) {
585 error(-1, "Invalid type in 'Bounds' array in stitching function");
588 bounds[i] = obj2.getNum();
591 bounds[k] = domain[0][1];
595 if (!dict->lookup("Encode", &obj1)->isArray() ||
596 obj1.arrayGetLength() != 2 * k) {
597 error(-1, "Missing or invalid 'Encode' entry in stitching function");
600 for (i = 0; i < 2 * k; ++i) {
601 if (!obj1.arrayGet(i, &obj2)->isNum()) {
602 error(-1, "Invalid type in 'Encode' array in stitching function");
605 encode[i] = obj2.getNum();
619 StitchingFunction::StitchingFunction(StitchingFunction *func) {
621 funcs = (Function **)gmalloc(k * sizeof(Function *));
622 memcpy(funcs, func->funcs, k * sizeof(Function *));
623 bounds = (double *)gmalloc((k + 1) * sizeof(double));
624 memcpy(bounds, func->bounds, (k + 1) * sizeof(double));
625 encode = (double *)gmalloc(2 * k * sizeof(double));
626 memcpy(encode, func->encode, 2 * k * sizeof(double));
630 StitchingFunction::~StitchingFunction() {
633 for (i = 0; i < k; ++i) {
643 void StitchingFunction::transform(double *in, double *out) {
647 if (in[0] < domain[0][0]) {
649 } else if (in[0] > domain[0][1]) {
654 for (i = 0; i < k - 1; ++i) {
655 if (x < bounds[i+1]) {
659 x = encode[2*i] + ((x - bounds[i]) / (bounds[i+1] - bounds[i])) *
660 (encode[2*i+1] - encode[2*i]);
661 funcs[i]->transform(&x, out);
664 //------------------------------------------------------------------------
665 // PostScriptFunction
666 //------------------------------------------------------------------------
714 // Note: 'if' and 'ifelse' are parsed separately.
715 // The rest are listed here in alphabetical order.
716 // The index in this table is equivalent to the entry in PSOp.
717 char *psOpNames[] = {
760 #define nPSOps (sizeof(psOpNames) / sizeof(char *))
770 // In the code array, 'if'/'ifelse' operators take up three slots
771 // plus space for the code in the subclause(s).
773 // +---------------------------------+
774 // | psOperator: psOpIf / psOpIfelse |
775 // +---------------------------------+
776 // | psBlock: ptr=<A> |
777 // +---------------------------------+
778 // | psBlock: ptr=<B> |
779 // +---------------------------------+
782 // | psOperator: psOpReturn |
783 // +---------------------------------+
784 // <A> | else clause |
786 // | psOperator: psOpReturn |
787 // +---------------------------------+
790 // For 'if', pointer <A> is present in the code stream but unused.
795 GBool booln; // boolean (stack only)
796 int intg; // integer (stack and code)
797 double real; // real (stack and code)
798 PSOp op; // operator (code only)
799 int blk; // if/ifelse block pointer (code only)
803 #define psStackSize 100
808 PSStack() { sp = psStackSize; }
809 void pushBool(GBool booln);
810 void pushInt(int intg);
811 void pushReal(double real);
815 GBool empty() { return sp == psStackSize; }
816 GBool topIsInt() { return sp < psStackSize && stack[sp].type == psInt; }
817 GBool topTwoAreInts()
818 { return sp < psStackSize - 1 &&
819 stack[sp].type == psInt &&
820 stack[sp+1].type == psInt; }
821 GBool topIsReal() { return sp < psStackSize && stack[sp].type == psReal; }
822 GBool topTwoAreNums()
823 { return sp < psStackSize - 1 &&
824 (stack[sp].type == psInt || stack[sp].type == psReal) &&
825 (stack[sp+1].type == psInt || stack[sp+1].type == psReal); }
827 void roll(int n, int j);
833 GBool checkOverflow(int n = 1);
834 GBool checkUnderflow();
835 GBool checkType(PSObjectType t1, PSObjectType t2);
837 PSObject stack[psStackSize];
841 GBool PSStack::checkOverflow(int n) {
843 error(-1, "Stack overflow in PostScript function");
849 GBool PSStack::checkUnderflow() {
850 if (sp == psStackSize) {
851 error(-1, "Stack underflow in PostScript function");
857 GBool PSStack::checkType(PSObjectType t1, PSObjectType t2) {
858 if (stack[sp].type != t1 && stack[sp].type != t2) {
859 error(-1, "Type mismatch in PostScript function");
865 void PSStack::pushBool(GBool booln) {
866 if (checkOverflow()) {
867 stack[--sp].type = psBool;
868 stack[sp].booln = booln;
872 void PSStack::pushInt(int intg) {
873 if (checkOverflow()) {
874 stack[--sp].type = psInt;
875 stack[sp].intg = intg;
879 void PSStack::pushReal(double real) {
880 if (checkOverflow()) {
881 stack[--sp].type = psReal;
882 stack[sp].real = real;
886 GBool PSStack::popBool() {
887 if (checkUnderflow() && checkType(psBool, psBool)) {
888 return stack[sp++].booln;
893 int PSStack::popInt() {
894 if (checkUnderflow() && checkType(psInt, psInt)) {
895 return stack[sp++].intg;
900 double PSStack::popNum() {
903 if (checkUnderflow() && checkType(psInt, psReal)) {
904 ret = (stack[sp].type == psInt) ? (double)stack[sp].intg : stack[sp].real;
911 void PSStack::copy(int n) {
914 if (!checkOverflow(n)) {
917 for (i = sp + n - 1; i <= sp; ++i) {
918 stack[i - n] = stack[i];
923 void PSStack::roll(int n, int j) {
935 if (n <= 0 || j == 0) {
938 for (i = 0; i < j; ++i) {
940 for (k = sp; k < sp + n - 1; ++k) {
941 stack[k] = stack[k+1];
943 stack[sp + n - 1] = obj;
947 void PSStack::index(int i) {
948 if (!checkOverflow()) {
952 stack[sp] = stack[sp + 1 + i];
955 void PSStack::pop() {
956 if (!checkUnderflow()) {
962 PostScriptFunction::PostScriptFunction(Object *funcObj, Dict *dict) {
971 //----- initialize the generic stuff
976 error(-1, "Type 4 function is missing range");
980 //----- get the stream
981 if (!funcObj->isStream()) {
982 error(-1, "Type 4 function isn't a stream");
985 str = funcObj->getStream();
987 //----- parse the function
989 if (!(tok = getToken(str)) || tok->cmp("{")) {
990 error(-1, "Expected '{' at start of PostScript function");
998 if (!parseCode(str, &codePtr)) {
1011 PostScriptFunction::PostScriptFunction(PostScriptFunction *func) {
1012 memcpy(this, func, sizeof(PostScriptFunction));
1013 code = (PSObject *)gmalloc(codeSize * sizeof(PSObject));
1014 memcpy(code, func->code, codeSize * sizeof(PSObject));
1017 PostScriptFunction::~PostScriptFunction() {
1021 void PostScriptFunction::transform(double *in, double *out) {
1025 stack = new PSStack();
1026 for (i = 0; i < m; ++i) {
1027 //~ may need to check for integers here
1028 stack->pushReal(in[i]);
1031 for (i = n - 1; i >= 0; --i) {
1032 out[i] = stack->popNum();
1033 if (out[i] < range[i][0]) {
1034 out[i] = range[i][0];
1035 } else if (out[i] > range[i][1]) {
1036 out[i] = range[i][1];
1039 // if (!stack->empty()) {
1040 // error(-1, "Extra values on stack at end of PostScript function");
1045 GBool PostScriptFunction::parseCode(Stream *str, int *codePtr) {
1053 if (!(tok = getToken(str))) {
1054 error(-1, "Unexpected end of PostScript function stream");
1057 p = tok->getCString();
1058 if (isdigit(*p) || *p == '.' || *p == '-') {
1060 for (++p; *p; ++p) {
1066 resizeCode(*codePtr);
1068 code[*codePtr].type = psReal;
1070 char *theLocale = setlocale(LC_NUMERIC, "C");
1071 code[*codePtr].real = atof(tok->getCString());
1072 setlocale(LC_NUMERIC, theLocale);
1075 code[*codePtr].type = psInt;
1076 code[*codePtr].intg = atoi(tok->getCString());
1080 } else if (!tok->cmp("{")) {
1084 resizeCode(opPtr + 2);
1085 if (!parseCode(str, codePtr)) {
1088 if (!(tok = getToken(str))) {
1089 error(-1, "Unexpected end of PostScript function stream");
1092 if (!tok->cmp("{")) {
1094 if (!parseCode(str, codePtr)) {
1098 if (!(tok = getToken(str))) {
1099 error(-1, "Unexpected end of PostScript function stream");
1105 if (!tok->cmp("if")) {
1107 error(-1, "Got 'if' operator with two blocks in PostScript function");
1110 code[opPtr].type = psOperator;
1111 code[opPtr].op = psOpIf;
1112 code[opPtr+2].type = psBlock;
1113 code[opPtr+2].blk = *codePtr;
1114 } else if (!tok->cmp("ifelse")) {
1116 error(-1, "Got 'ifelse' operator with one blocks in PostScript function");
1119 code[opPtr].type = psOperator;
1120 code[opPtr].op = psOpIfelse;
1121 code[opPtr+1].type = psBlock;
1122 code[opPtr+1].blk = elsePtr;
1123 code[opPtr+2].type = psBlock;
1124 code[opPtr+2].blk = *codePtr;
1126 error(-1, "Expected if/ifelse operator in PostScript function");
1131 } else if (!tok->cmp("}")) {
1133 resizeCode(*codePtr);
1134 code[*codePtr].type = psOperator;
1135 code[*codePtr].op = psOpReturn;
1141 // invariant: psOpNames[a] < tok < psOpNames[b]
1144 cmp = tok->cmp(psOpNames[mid]);
1147 } else if (cmp < 0) {
1154 error(-1, "Unknown operator '%s' in PostScript function",
1160 resizeCode(*codePtr);
1161 code[*codePtr].type = psOperator;
1162 code[*codePtr].op = (PSOp)a;
1169 GString *PostScriptFunction::getToken(Stream *str) {
1176 } while (c != EOF && isspace(c));
1177 if (c == '{' || c == '}') {
1179 } else if (isdigit(c) || c == '.' || c == '-') {
1182 c = str->lookChar();
1183 if (c == EOF || !(isdigit(c) || c == '.' || c == '-')) {
1191 c = str->lookChar();
1192 if (c == EOF || !isalnum(c)) {
1201 void PostScriptFunction::resizeCode(int newSize) {
1202 if (newSize >= codeSize) {
1204 code = (PSObject *)grealloc(code, codeSize * sizeof(PSObject));
1208 void PostScriptFunction::exec(PSStack *stack, int codePtr) {
1214 switch (code[codePtr].type) {
1216 stack->pushInt(code[codePtr++].intg);
1219 stack->pushReal(code[codePtr++].real);
1222 switch (code[codePtr++].op) {
1224 if (stack->topIsInt()) {
1225 stack->pushInt(abs(stack->popInt()));
1227 stack->pushReal(fabs(stack->popNum()));
1231 if (stack->topTwoAreInts()) {
1232 i2 = stack->popInt();
1233 i1 = stack->popInt();
1234 stack->pushInt(i1 + i2);
1236 r2 = stack->popNum();
1237 r1 = stack->popNum();
1238 stack->pushReal(r1 + r2);
1242 if (stack->topTwoAreInts()) {
1243 i2 = stack->popInt();
1244 i1 = stack->popInt();
1245 stack->pushInt(i1 & i2);
1247 b2 = stack->popBool();
1248 b1 = stack->popBool();
1249 stack->pushReal(b1 && b2);
1253 r2 = stack->popNum();
1254 r1 = stack->popNum();
1255 stack->pushReal(atan2(r1, r2));
1258 i2 = stack->popInt();
1259 i1 = stack->popInt();
1261 stack->pushInt(i1 << i2);
1262 } else if (i2 < 0) {
1263 stack->pushInt((int)((Guint)i1 >> i2));
1269 if (!stack->topIsInt()) {
1270 stack->pushReal(ceil(stack->popNum()));
1274 stack->copy(stack->popInt());
1277 stack->pushReal(cos(stack->popNum()));
1280 if (!stack->topIsInt()) {
1281 stack->pushInt((int)stack->popNum());
1285 if (!stack->topIsReal()) {
1286 stack->pushReal(stack->popNum());
1290 r2 = stack->popNum();
1291 r1 = stack->popNum();
1292 stack->pushReal(r1 / r2);
1298 if (stack->topTwoAreInts()) {
1299 i2 = stack->popInt();
1300 i1 = stack->popInt();
1301 stack->pushBool(i1 == i2);
1302 } else if (stack->topTwoAreNums()) {
1303 r2 = stack->popNum();
1304 r1 = stack->popNum();
1305 stack->pushBool(r1 == r2);
1307 b2 = stack->popBool();
1308 b1 = stack->popBool();
1309 stack->pushBool(b1 == b2);
1316 r2 = stack->popInt();
1317 r1 = stack->popInt();
1318 stack->pushReal(pow(r1, r2));
1321 stack->pushBool(gFalse);
1324 if (!stack->topIsInt()) {
1325 stack->pushReal(floor(stack->popNum()));
1329 if (stack->topTwoAreInts()) {
1330 i2 = stack->popInt();
1331 i1 = stack->popInt();
1332 stack->pushBool(i1 >= i2);
1334 r2 = stack->popNum();
1335 r1 = stack->popNum();
1336 stack->pushBool(r1 >= r2);
1340 if (stack->topTwoAreInts()) {
1341 i2 = stack->popInt();
1342 i1 = stack->popInt();
1343 stack->pushBool(i1 > i2);
1345 r2 = stack->popNum();
1346 r1 = stack->popNum();
1347 stack->pushBool(r1 > r2);
1351 i2 = stack->popInt();
1352 i1 = stack->popInt();
1353 stack->pushInt(i1 / i2);
1356 stack->index(stack->popInt());
1359 if (stack->topTwoAreInts()) {
1360 i2 = stack->popInt();
1361 i1 = stack->popInt();
1362 stack->pushBool(i1 <= i2);
1364 r2 = stack->popNum();
1365 r1 = stack->popNum();
1366 stack->pushBool(r1 <= r2);
1370 stack->pushReal(log(stack->popNum()));
1373 stack->pushReal(log10(stack->popNum()));
1376 if (stack->topTwoAreInts()) {
1377 i2 = stack->popInt();
1378 i1 = stack->popInt();
1379 stack->pushBool(i1 < i2);
1381 r2 = stack->popNum();
1382 r1 = stack->popNum();
1383 stack->pushBool(r1 < r2);
1387 i2 = stack->popInt();
1388 i1 = stack->popInt();
1389 stack->pushInt(i1 % i2);
1392 if (stack->topTwoAreInts()) {
1393 i2 = stack->popInt();
1394 i1 = stack->popInt();
1395 //~ should check for out-of-range, and push a real instead
1396 stack->pushInt(i1 * i2);
1398 r2 = stack->popNum();
1399 r1 = stack->popNum();
1400 stack->pushReal(r1 * r2);
1404 if (stack->topTwoAreInts()) {
1405 i2 = stack->popInt();
1406 i1 = stack->popInt();
1407 stack->pushBool(i1 != i2);
1408 } else if (stack->topTwoAreNums()) {
1409 r2 = stack->popNum();
1410 r1 = stack->popNum();
1411 stack->pushBool(r1 != r2);
1413 b2 = stack->popBool();
1414 b1 = stack->popBool();
1415 stack->pushBool(b1 != b2);
1419 if (stack->topIsInt()) {
1420 stack->pushInt(-stack->popInt());
1422 stack->pushReal(-stack->popNum());
1426 if (stack->topIsInt()) {
1427 stack->pushInt(~stack->popInt());
1429 stack->pushReal(!stack->popBool());
1433 if (stack->topTwoAreInts()) {
1434 i2 = stack->popInt();
1435 i1 = stack->popInt();
1436 stack->pushInt(i1 | i2);
1438 b2 = stack->popBool();
1439 b1 = stack->popBool();
1440 stack->pushReal(b1 || b2);
1447 i2 = stack->popInt();
1448 i1 = stack->popInt();
1449 stack->roll(i1, i2);
1452 if (!stack->topIsInt()) {
1453 r1 = stack->popNum();
1454 stack->pushReal((r1 >= 0) ? floor(r1 + 0.5) : ceil(r1 - 0.5));
1458 stack->pushReal(cos(stack->popNum()));
1461 stack->pushReal(sqrt(stack->popNum()));
1464 if (stack->topTwoAreInts()) {
1465 i2 = stack->popInt();
1466 i1 = stack->popInt();
1467 stack->pushInt(i1 - i2);
1469 r2 = stack->popNum();
1470 r1 = stack->popNum();
1471 stack->pushReal(r1 - r2);
1475 stack->pushBool(gTrue);
1478 if (!stack->topIsInt()) {
1479 r1 = stack->popNum();
1480 stack->pushReal((r1 >= 0) ? floor(r1) : ceil(r1));
1484 if (stack->topTwoAreInts()) {
1485 i2 = stack->popInt();
1486 i1 = stack->popInt();
1487 stack->pushInt(i1 ^ i2);
1489 b2 = stack->popBool();
1490 b1 = stack->popBool();
1491 stack->pushReal(b1 ^ b2);
1495 b1 = stack->popBool();
1497 exec(stack, codePtr + 2);
1499 codePtr = code[codePtr + 1].blk;
1502 b1 = stack->popBool();
1504 exec(stack, codePtr + 2);
1506 exec(stack, code[codePtr].blk);
1508 codePtr = code[codePtr + 1].blk;
1515 error(-1, "Internal: bad object in PostScript function code");