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) {
385 for (k = m - 1; 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) {
623 funcs = (Function **)gmalloc(k * sizeof(Function *));
624 for (i = 0; i < k; ++i) {
625 funcs[i] = func->funcs[i]->copy();
627 bounds = (double *)gmalloc((k + 1) * sizeof(double));
628 memcpy(bounds, func->bounds, (k + 1) * sizeof(double));
629 encode = (double *)gmalloc(2 * k * sizeof(double));
630 memcpy(encode, func->encode, 2 * k * sizeof(double));
634 StitchingFunction::~StitchingFunction() {
638 for (i = 0; i < k; ++i) {
649 void StitchingFunction::transform(double *in, double *out) {
653 if (in[0] < domain[0][0]) {
655 } else if (in[0] > domain[0][1]) {
660 for (i = 0; i < k - 1; ++i) {
661 if (x < bounds[i+1]) {
665 x = encode[2*i] + ((x - bounds[i]) / (bounds[i+1] - bounds[i])) *
666 (encode[2*i+1] - encode[2*i]);
667 funcs[i]->transform(&x, out);
670 //------------------------------------------------------------------------
671 // PostScriptFunction
672 //------------------------------------------------------------------------
720 // Note: 'if' and 'ifelse' are parsed separately.
721 // The rest are listed here in alphabetical order.
722 // The index in this table is equivalent to the entry in PSOp.
723 char *psOpNames[] = {
766 #define nPSOps (sizeof(psOpNames) / sizeof(char *))
776 // In the code array, 'if'/'ifelse' operators take up three slots
777 // plus space for the code in the subclause(s).
779 // +---------------------------------+
780 // | psOperator: psOpIf / psOpIfelse |
781 // +---------------------------------+
782 // | psBlock: ptr=<A> |
783 // +---------------------------------+
784 // | psBlock: ptr=<B> |
785 // +---------------------------------+
788 // | psOperator: psOpReturn |
789 // +---------------------------------+
790 // <A> | else clause |
792 // | psOperator: psOpReturn |
793 // +---------------------------------+
796 // For 'if', pointer <A> is present in the code stream but unused.
801 GBool booln; // boolean (stack only)
802 int intg; // integer (stack and code)
803 double real; // real (stack and code)
804 PSOp op; // operator (code only)
805 int blk; // if/ifelse block pointer (code only)
809 #define psStackSize 100
814 PSStack() { sp = psStackSize; }
815 void pushBool(GBool booln);
816 void pushInt(int intg);
817 void pushReal(double real);
821 GBool empty() { return sp == psStackSize; }
822 GBool topIsInt() { return sp < psStackSize && stack[sp].type == psInt; }
823 GBool topTwoAreInts()
824 { return sp < psStackSize - 1 &&
825 stack[sp].type == psInt &&
826 stack[sp+1].type == psInt; }
827 GBool topIsReal() { return sp < psStackSize && stack[sp].type == psReal; }
828 GBool topTwoAreNums()
829 { return sp < psStackSize - 1 &&
830 (stack[sp].type == psInt || stack[sp].type == psReal) &&
831 (stack[sp+1].type == psInt || stack[sp+1].type == psReal); }
833 void roll(int n, int j);
839 GBool checkOverflow(int n = 1);
840 GBool checkUnderflow();
841 GBool checkType(PSObjectType t1, PSObjectType t2);
843 PSObject stack[psStackSize];
847 GBool PSStack::checkOverflow(int n) {
849 error(-1, "Stack overflow in PostScript function");
855 GBool PSStack::checkUnderflow() {
856 if (sp == psStackSize) {
857 error(-1, "Stack underflow in PostScript function");
863 GBool PSStack::checkType(PSObjectType t1, PSObjectType t2) {
864 if (stack[sp].type != t1 && stack[sp].type != t2) {
865 error(-1, "Type mismatch in PostScript function");
871 void PSStack::pushBool(GBool booln) {
872 if (checkOverflow()) {
873 stack[--sp].type = psBool;
874 stack[sp].booln = booln;
878 void PSStack::pushInt(int intg) {
879 if (checkOverflow()) {
880 stack[--sp].type = psInt;
881 stack[sp].intg = intg;
885 void PSStack::pushReal(double real) {
886 if (checkOverflow()) {
887 stack[--sp].type = psReal;
888 stack[sp].real = real;
892 GBool PSStack::popBool() {
893 if (checkUnderflow() && checkType(psBool, psBool)) {
894 return stack[sp++].booln;
899 int PSStack::popInt() {
900 if (checkUnderflow() && checkType(psInt, psInt)) {
901 return stack[sp++].intg;
906 double PSStack::popNum() {
909 if (checkUnderflow() && checkType(psInt, psReal)) {
910 ret = (stack[sp].type == psInt) ? (double)stack[sp].intg : stack[sp].real;
917 void PSStack::copy(int n) {
920 if (!checkOverflow(n)) {
923 for (i = sp + n - 1; i <= sp; ++i) {
924 stack[i - n] = stack[i];
929 void PSStack::roll(int n, int j) {
941 if (n <= 0 || j == 0) {
944 for (i = 0; i < j; ++i) {
946 for (k = sp; k < sp + n - 1; ++k) {
947 stack[k] = stack[k+1];
949 stack[sp + n - 1] = obj;
953 void PSStack::index(int i) {
954 if (!checkOverflow()) {
958 stack[sp] = stack[sp + 1 + i];
961 void PSStack::pop() {
962 if (!checkUnderflow()) {
968 PostScriptFunction::PostScriptFunction(Object *funcObj, Dict *dict) {
977 //----- initialize the generic stuff
982 error(-1, "Type 4 function is missing range");
986 //----- get the stream
987 if (!funcObj->isStream()) {
988 error(-1, "Type 4 function isn't a stream");
991 str = funcObj->getStream();
993 //----- parse the function
995 if (!(tok = getToken(str)) || tok->cmp("{")) {
996 error(-1, "Expected '{' at start of PostScript function");
1004 if (!parseCode(str, &codePtr)) {
1017 PostScriptFunction::PostScriptFunction(PostScriptFunction *func) {
1018 memcpy(this, func, sizeof(PostScriptFunction));
1019 code = (PSObject *)gmalloc(codeSize * sizeof(PSObject));
1020 memcpy(code, func->code, codeSize * sizeof(PSObject));
1023 PostScriptFunction::~PostScriptFunction() {
1027 void PostScriptFunction::transform(double *in, double *out) {
1031 stack = new PSStack();
1032 for (i = 0; i < m; ++i) {
1033 //~ may need to check for integers here
1034 stack->pushReal(in[i]);
1037 for (i = n - 1; i >= 0; --i) {
1038 out[i] = stack->popNum();
1039 if (out[i] < range[i][0]) {
1040 out[i] = range[i][0];
1041 } else if (out[i] > range[i][1]) {
1042 out[i] = range[i][1];
1045 // if (!stack->empty()) {
1046 // error(-1, "Extra values on stack at end of PostScript function");
1051 GBool PostScriptFunction::parseCode(Stream *str, int *codePtr) {
1059 if (!(tok = getToken(str))) {
1060 error(-1, "Unexpected end of PostScript function stream");
1063 p = tok->getCString();
1064 if (isdigit(*p) || *p == '.' || *p == '-') {
1066 for (++p; *p; ++p) {
1072 resizeCode(*codePtr);
1074 code[*codePtr].type = psReal;
1076 char *theLocale = setlocale(LC_NUMERIC, "C");
1077 code[*codePtr].real = atof(tok->getCString());
1078 setlocale(LC_NUMERIC, theLocale);
1081 code[*codePtr].type = psInt;
1082 code[*codePtr].intg = atoi(tok->getCString());
1086 } else if (!tok->cmp("{")) {
1090 resizeCode(opPtr + 2);
1091 if (!parseCode(str, codePtr)) {
1094 if (!(tok = getToken(str))) {
1095 error(-1, "Unexpected end of PostScript function stream");
1098 if (!tok->cmp("{")) {
1100 if (!parseCode(str, codePtr)) {
1104 if (!(tok = getToken(str))) {
1105 error(-1, "Unexpected end of PostScript function stream");
1111 if (!tok->cmp("if")) {
1113 error(-1, "Got 'if' operator with two blocks in PostScript function");
1116 code[opPtr].type = psOperator;
1117 code[opPtr].op = psOpIf;
1118 code[opPtr+2].type = psBlock;
1119 code[opPtr+2].blk = *codePtr;
1120 } else if (!tok->cmp("ifelse")) {
1122 error(-1, "Got 'ifelse' operator with one blocks in PostScript function");
1125 code[opPtr].type = psOperator;
1126 code[opPtr].op = psOpIfelse;
1127 code[opPtr+1].type = psBlock;
1128 code[opPtr+1].blk = elsePtr;
1129 code[opPtr+2].type = psBlock;
1130 code[opPtr+2].blk = *codePtr;
1132 error(-1, "Expected if/ifelse operator in PostScript function");
1137 } else if (!tok->cmp("}")) {
1139 resizeCode(*codePtr);
1140 code[*codePtr].type = psOperator;
1141 code[*codePtr].op = psOpReturn;
1147 // invariant: psOpNames[a] < tok < psOpNames[b]
1150 cmp = tok->cmp(psOpNames[mid]);
1153 } else if (cmp < 0) {
1160 error(-1, "Unknown operator '%s' in PostScript function",
1166 resizeCode(*codePtr);
1167 code[*codePtr].type = psOperator;
1168 code[*codePtr].op = (PSOp)a;
1175 GString *PostScriptFunction::getToken(Stream *str) {
1182 } while (c != EOF && isspace(c));
1183 if (c == '{' || c == '}') {
1185 } else if (isdigit(c) || c == '.' || c == '-') {
1188 c = str->lookChar();
1189 if (c == EOF || !(isdigit(c) || c == '.' || c == '-')) {
1197 c = str->lookChar();
1198 if (c == EOF || !isalnum(c)) {
1207 void PostScriptFunction::resizeCode(int newSize) {
1208 if (newSize >= codeSize) {
1210 code = (PSObject *)grealloc(code, codeSize * sizeof(PSObject));
1214 void PostScriptFunction::exec(PSStack *stack, int codePtr) {
1220 switch (code[codePtr].type) {
1222 stack->pushInt(code[codePtr++].intg);
1225 stack->pushReal(code[codePtr++].real);
1228 switch (code[codePtr++].op) {
1230 if (stack->topIsInt()) {
1231 stack->pushInt(abs(stack->popInt()));
1233 stack->pushReal(fabs(stack->popNum()));
1237 if (stack->topTwoAreInts()) {
1238 i2 = stack->popInt();
1239 i1 = stack->popInt();
1240 stack->pushInt(i1 + i2);
1242 r2 = stack->popNum();
1243 r1 = stack->popNum();
1244 stack->pushReal(r1 + r2);
1248 if (stack->topTwoAreInts()) {
1249 i2 = stack->popInt();
1250 i1 = stack->popInt();
1251 stack->pushInt(i1 & i2);
1253 b2 = stack->popBool();
1254 b1 = stack->popBool();
1255 stack->pushBool(b1 && b2);
1259 r2 = stack->popNum();
1260 r1 = stack->popNum();
1261 stack->pushReal(atan2(r1, r2));
1264 i2 = stack->popInt();
1265 i1 = stack->popInt();
1267 stack->pushInt(i1 << i2);
1268 } else if (i2 < 0) {
1269 stack->pushInt((int)((Guint)i1 >> i2));
1275 if (!stack->topIsInt()) {
1276 stack->pushReal(ceil(stack->popNum()));
1280 stack->copy(stack->popInt());
1283 stack->pushReal(cos(stack->popNum()));
1286 if (!stack->topIsInt()) {
1287 stack->pushInt((int)stack->popNum());
1291 if (!stack->topIsReal()) {
1292 stack->pushReal(stack->popNum());
1296 r2 = stack->popNum();
1297 r1 = stack->popNum();
1298 stack->pushReal(r1 / r2);
1304 if (stack->topTwoAreInts()) {
1305 i2 = stack->popInt();
1306 i1 = stack->popInt();
1307 stack->pushBool(i1 == i2);
1308 } else if (stack->topTwoAreNums()) {
1309 r2 = stack->popNum();
1310 r1 = stack->popNum();
1311 stack->pushBool(r1 == r2);
1313 b2 = stack->popBool();
1314 b1 = stack->popBool();
1315 stack->pushBool(b1 == b2);
1322 r2 = stack->popNum();
1323 r1 = stack->popNum();
1324 stack->pushReal(pow(r1, r2));
1327 stack->pushBool(gFalse);
1330 if (!stack->topIsInt()) {
1331 stack->pushReal(floor(stack->popNum()));
1335 if (stack->topTwoAreInts()) {
1336 i2 = stack->popInt();
1337 i1 = stack->popInt();
1338 stack->pushBool(i1 >= i2);
1340 r2 = stack->popNum();
1341 r1 = stack->popNum();
1342 stack->pushBool(r1 >= r2);
1346 if (stack->topTwoAreInts()) {
1347 i2 = stack->popInt();
1348 i1 = stack->popInt();
1349 stack->pushBool(i1 > i2);
1351 r2 = stack->popNum();
1352 r1 = stack->popNum();
1353 stack->pushBool(r1 > r2);
1357 i2 = stack->popInt();
1358 i1 = stack->popInt();
1359 stack->pushInt(i1 / i2);
1362 stack->index(stack->popInt());
1365 if (stack->topTwoAreInts()) {
1366 i2 = stack->popInt();
1367 i1 = stack->popInt();
1368 stack->pushBool(i1 <= i2);
1370 r2 = stack->popNum();
1371 r1 = stack->popNum();
1372 stack->pushBool(r1 <= r2);
1376 stack->pushReal(log(stack->popNum()));
1379 stack->pushReal(log10(stack->popNum()));
1382 if (stack->topTwoAreInts()) {
1383 i2 = stack->popInt();
1384 i1 = stack->popInt();
1385 stack->pushBool(i1 < i2);
1387 r2 = stack->popNum();
1388 r1 = stack->popNum();
1389 stack->pushBool(r1 < r2);
1393 i2 = stack->popInt();
1394 i1 = stack->popInt();
1395 stack->pushInt(i1 % i2);
1398 if (stack->topTwoAreInts()) {
1399 i2 = stack->popInt();
1400 i1 = stack->popInt();
1401 //~ should check for out-of-range, and push a real instead
1402 stack->pushInt(i1 * i2);
1404 r2 = stack->popNum();
1405 r1 = stack->popNum();
1406 stack->pushReal(r1 * r2);
1410 if (stack->topTwoAreInts()) {
1411 i2 = stack->popInt();
1412 i1 = stack->popInt();
1413 stack->pushBool(i1 != i2);
1414 } else if (stack->topTwoAreNums()) {
1415 r2 = stack->popNum();
1416 r1 = stack->popNum();
1417 stack->pushBool(r1 != r2);
1419 b2 = stack->popBool();
1420 b1 = stack->popBool();
1421 stack->pushBool(b1 != b2);
1425 if (stack->topIsInt()) {
1426 stack->pushInt(-stack->popInt());
1428 stack->pushReal(-stack->popNum());
1432 if (stack->topIsInt()) {
1433 stack->pushInt(~stack->popInt());
1435 stack->pushBool(!stack->popBool());
1439 if (stack->topTwoAreInts()) {
1440 i2 = stack->popInt();
1441 i1 = stack->popInt();
1442 stack->pushInt(i1 | i2);
1444 b2 = stack->popBool();
1445 b1 = stack->popBool();
1446 stack->pushBool(b1 || b2);
1453 i2 = stack->popInt();
1454 i1 = stack->popInt();
1455 stack->roll(i1, i2);
1458 if (!stack->topIsInt()) {
1459 r1 = stack->popNum();
1460 stack->pushReal((r1 >= 0) ? floor(r1 + 0.5) : ceil(r1 - 0.5));
1464 stack->pushReal(sin(stack->popNum()));
1467 stack->pushReal(sqrt(stack->popNum()));
1470 if (stack->topTwoAreInts()) {
1471 i2 = stack->popInt();
1472 i1 = stack->popInt();
1473 stack->pushInt(i1 - i2);
1475 r2 = stack->popNum();
1476 r1 = stack->popNum();
1477 stack->pushReal(r1 - r2);
1481 stack->pushBool(gTrue);
1484 if (!stack->topIsInt()) {
1485 r1 = stack->popNum();
1486 stack->pushReal((r1 >= 0) ? floor(r1) : ceil(r1));
1490 if (stack->topTwoAreInts()) {
1491 i2 = stack->popInt();
1492 i1 = stack->popInt();
1493 stack->pushInt(i1 ^ i2);
1495 b2 = stack->popBool();
1496 b1 = stack->popBool();
1497 stack->pushBool(b1 ^ b2);
1501 b1 = stack->popBool();
1503 exec(stack, codePtr + 2);
1505 codePtr = code[codePtr + 1].blk;
1508 b1 = stack->popBool();
1510 exec(stack, codePtr + 2);
1512 exec(stack, code[codePtr].blk);
1514 codePtr = code[codePtr + 1].blk;
1521 error(-1, "Internal: bad object in PostScript function code");