|
|
1.1 ! root 1: /* execute.c ! 2: * ! 3: * Copyright (c) 1984, 1985 Xerox Corp. ! 4: * ! 5: * Define the functions used in parse.c. ! 6: * ! 7: * Execute the RES file and leave the correct parameters on the stack. ! 8: * ! 9: */ ! 10: ! 11: #include <math.h> ! 12: #include <stdio.h> ! 13: #include <iptokens.h> ! 14: #include "stack.h" ! 15: ! 16: #define err0 "(%d) execute: Bad RES header, got %s\n" ! 17: #define err1 "(%d) execute: roll moveFirst > depth, %d > %d!\n" ! 18: #define err2 "(%d) execute: unknown operator subtype, got %d!\n" ! 19: #define err3 "(%d) execute: unknown operator, type=%d!\n" ! 20: #define err4 "(%d) execute: unknown sequence, type=%d, length=%d!\n" ! 21: #define err5 "(%d) execute: sequenceInsertFile not implemented!\n" ! 22: ! 23: ! 24: /* Defined elsewhere. */ ! 25: extern unsigned char **malloc(); ! 26: extern long filepos; ! 27: extern FILE *fp; ! 28: ! 29: /* Defined in this module. */ ! 30: extern unsigned char *decompressID(); ! 31: extern unsigned char *decompressOP(); ! 32: extern unsigned char *imagedata(); ! 33: extern unsigned char *popidentifier(); ! 34: extern double popdouble(); ! 35: ! 36: ! 37: /* ! 38: * Public procedures defined for "parse" module. ! 39: * ! 40: */ ! 41: ! 42: header(string) ! 43: char *string; ! 44: { ! 45: fprintf(stderr, "strings are '%s', '%s'\n", string, RES_Header); ! 46: if (strcmp(string, RES_Header) != 0) error(err0, filepos, string); ! 47: } ! 48: ! 49: op_makevec() ! 50: { ! 51: int n, depth; ! 52: unsigned char *ptr, **array; ! 53: depth = popint(); ! 54: array = malloc((depth+1)*sizeof(unsigned char *)); /* null terminated array */ ! 55: for (n=0; n < depth; n++) array[depth-n-1] = pop(0); ! 56: array[depth] = (unsigned char *) 0; ! 57: ptr = makevector(array, type_vector, subtype_general); ! 58: for (n=0; n < depth; n++) free(array[n]); ! 59: free(array); ! 60: push(ptr); ! 61: } ! 62: ! 63: op_pop() ! 64: { ! 65: free(pop(0)); ! 66: } ! 67: ! 68: op_copy() ! 69: { ! 70: int n, depth; ! 71: unsigned char *ptr, **temp1, **temp2; ! 72: depth = popint(); ! 73: temp1 = malloc(depth*sizeof(unsigned char *)); ! 74: temp2 = malloc(depth*sizeof(unsigned char *)); ! 75: for (n=0; n < depth; n++) temp1[depth-n-1] = pop(0); ! 76: for (n=0; n < depth; n++) temp2[n] = duplicate(temp1[n]); ! 77: for (n=0; n < depth; n++) push(temp1[n]); ! 78: for (n=0; n < depth; n++) push(temp2[n]); ! 79: free(temp1); ! 80: free(temp2); ! 81: } ! 82: ! 83: op_dup() ! 84: { ! 85: unsigned char *ptr, *newptr; ! 86: ptr = pop(0); ! 87: newptr = duplicate(ptr); ! 88: push(ptr); ! 89: push(newptr); ! 90: } ! 91: ! 92: op_roll() ! 93: { ! 94: int n, depth, moveFirst; ! 95: unsigned char *ptr, **temp; ! 96: moveFirst = popint(); ! 97: depth = popint(); ! 98: if (moveFirst > depth) error(err1, filepos, moveFirst, depth); ! 99: temp = malloc(depth*sizeof(unsigned char *)); ! 100: for (n=0; n < depth; n++) temp[depth-n-1] = pop(0); ! 101: for (n=moveFirst; n < depth; n++) push(temp[n]); ! 102: for (n=0; n < moveFirst; n++) push(temp[n]); ! 103: free(temp); ! 104: } ! 105: ! 106: op_exch() ! 107: { ! 108: unsigned char *temp1, *temp2; ! 109: temp1 = pop(0); ! 110: temp2 = pop(0); ! 111: push(temp1); ! 112: push(temp2); ! 113: } ! 114: ! 115: op_nop() ! 116: { ! 117: } ! 118: ! 119: op_translate() ! 120: { ! 121: double x, y; ! 122: unsigned char *ptr; ! 123: y = popdouble(); ! 124: x = popdouble(); ! 125: ptr = maketransformation(1.0, 0.0, x, 0.0, 1.0, y); ! 126: push(ptr); ! 127: } ! 128: ! 129: op_rotate() ! 130: { ! 131: double angle, cosA, sinA, pi; ! 132: unsigned char *ptr; ! 133: angle = popdouble(); ! 134: angle = 3.1415926*angle/180.; ! 135: cosA = cos(angle); ! 136: sinA = sin(angle); ! 137: ptr = maketransformation(cosA, -sinA, 0.0, sinA, cosA, 0.0); ! 138: push(ptr); ! 139: } ! 140: ! 141: op_scale() ! 142: { ! 143: unsigned char *ptr; ! 144: double s; ! 145: s = popdouble(); ! 146: ptr = maketransformation(s, 0.0, 0.0, 0.0, s, 0.0); ! 147: push(ptr); ! 148: } ! 149: ! 150: op_scale2() ! 151: { ! 152: unsigned char *ptr; ! 153: double sx, sy; ! 154: sy = popdouble(); ! 155: sx = popdouble(); ! 156: ptr = maketransformation(sx, 0.0, 0.0, 0.0, sy, 0.0); ! 157: push(ptr); ! 158: } ! 159: ! 160: op_concat() ! 161: { ! 162: double a, b, c, d, e, f; ! 163: unsigned char *ptr, *nptr, *mptr; ! 164: double *m, *n; ! 165: nptr = pop(type_transformation, 0); ! 166: mptr = pop(type_transformation, 0); ! 167: n = gettransformation(nptr); ! 168: m = gettransformation(mptr); ! 169: a = m[0]*n[0]+m[3]*n[1]; ! 170: b = m[1]*n[0]+m[4]*n[1]; ! 171: c = m[2]*n[0]+m[5]*n[1]+n[2]; ! 172: d = m[0]*n[3]+m[3]*n[4]; ! 173: e = m[1]*n[3]+m[4]*n[4]; ! 174: f = m[2]*n[3]+m[5]*n[4]+n[5]; ! 175: ptr = maketransformation(a, b, c, d, e, f); ! 176: free(mptr); ! 177: free(nptr); ! 178: free(m); ! 179: free(n); ! 180: push(ptr); ! 181: } ! 182: ! 183: op_makepixelarray() ! 184: { ! 185: int n; ! 186: unsigned char *ptr, **array; ! 187: array = malloc(9*sizeof(unsigned char *)); ! 188: array[6] = pop(type_vector, 0); /* samples */ ! 189: array[5] = pop(type_transformation, 0); /* m */ ! 190: array[4] = pop(type_number, 0); /* samplesInterleaved */ ! 191: array[3] = pop(type_number | type_vector, 0); /* maxSampleValue */ ! 192: array[2] = pop(type_number, 0); /* samplesPerPixel */ ! 193: array[1] = pop(type_number, 0); /* yPixels */ ! 194: array[0] = pop(type_number, 0); /* xPixels */ ! 195: array[7] = makeselect(getint(array[2]), ~0); /* select == all samples */ ! 196: array[8] = (unsigned char *) 0; /* null terminated list */ ! 197: ptr = makepixelarray(array); ! 198: for (n=0; n < 9; n++) free(array[n]); ! 199: free(array); ! 200: push(ptr); ! 201: } ! 202: ! 203: ! 204: op_extractpixelarray() ! 205: { ! 206: int depth; ! 207: unsigned char *oldptr, *newptr, *select, **array; ! 208: select = pop(type_vector, 0); ! 209: oldptr = pop(type_pixelarray, 0); ! 210: array = getpixelarray(oldptr); ! 211: array[7] = select; ! 212: newptr = makepixelarray(array); ! 213: free(select); ! 214: free(oldptr); ! 215: free(array); ! 216: push(newptr); ! 217: } ! 218: ! 219: op_do() ! 220: { ! 221: unsigned char *ptr, **array; ! 222: int type, subtype; ! 223: array = malloc(3*sizeof(unsigned char *)); ! 224: array[0] = pop(type_operator, 0); /* operator to do */ ! 225: array[1] = pop(type_vector, 0); /* vector argument */ ! 226: array[2] = (unsigned char *) 0; ! 227: switch (getsubtype(array[0])) ! 228: { ! 229: case subtype_decompressop: ! 230: type = type_vector; ! 231: subtype = subtype_samples; ! 232: break; ! 233: case subtype_colorop: ! 234: type = type_color; ! 235: subtype = subtype_operator; ! 236: break; ! 237: case subtype_colormodelop: ! 238: type = type_operator; ! 239: subtype = subtype_colorop; ! 240: break; ! 241: default: ! 242: error(err2, filepos, getsubtype(array[0])); ! 243: } ! 244: ptr = makevector(array, type, subtype); ! 245: free(array[0]); ! 246: free(array[1]); ! 247: free(array); ! 248: push(ptr); ! 249: } ! 250: ! 251: op_finddecompressor() ! 252: { ! 253: unsigned char *ptr, **array; ! 254: array = malloc(2*sizeof(unsigned char *)); ! 255: array[0] = popidentifier("decompressionOps/"); ! 256: array[1] = (unsigned char *) 0; ! 257: ptr = makeoperator(array, subtype_decompressop); ! 258: free(array[0]); ! 259: free(array); ! 260: push(ptr); ! 261: } ! 262: ! 263: op_makegray() ! 264: { ! 265: unsigned char *ptr, **array; ! 266: array = malloc(2*sizeof(unsigned char *)); ! 267: array[0] = pop(type_number); ! 268: array[1] = (unsigned char *) 0; ! 269: ptr = makecolor(array, subtype_value); ! 270: push(ptr); ! 271: } ! 272: ! 273: op_findcolor() ! 274: { ! 275: unsigned char *ptr, **array; ! 276: array = malloc(2*sizeof(unsigned char *)); ! 277: array[0] = popidentifier("colors/"); ! 278: array[1] = (unsigned char *) 0; ! 279: ptr = makecolor(array, subtype_name); ! 280: free(array[0]); ! 281: free(array); ! 282: push(ptr); ! 283: } ! 284: ! 285: op_findcoloroperator() ! 286: { ! 287: unsigned char *ptr, **array; ! 288: array = malloc(2*sizeof(unsigned char *)); ! 289: array[0] = popidentifier("colorOps/"); ! 290: array[1] = (unsigned char *) 0; ! 291: ptr = makeoperator(array, subtype_colorop); ! 292: free(array[0]); ! 293: free(array); ! 294: push(ptr); ! 295: } ! 296: ! 297: op_findcolormodeloperator() ! 298: { ! 299: unsigned char *ptr, **array; ! 300: array = malloc(2*sizeof(unsigned char *)); ! 301: array[0] = popidentifier("colorModelOps/"); ! 302: array[1] = (unsigned char *) 0; ! 303: ptr = makeoperator(array, subtype_colormodelop); ! 304: free(array[0]); ! 305: free(array); ! 306: push(ptr); ! 307: } ! 308: ! 309: op_beginblock() ! 310: { ! 311: } ! 312: ! 313: op_endblock() ! 314: { ! 315: } ! 316: ! 317: op_unknown(op) ! 318: int op; ! 319: { ! 320: error(err3, filepos, op); ! 321: } ! 322: ! 323: seq_comment(nbytes) ! 324: int nbytes; ! 325: { ! 326: fseek(fp, (long) nbytes, 1); ! 327: } ! 328: ! 329: seq_continued(nbytes, last) ! 330: int nbytes, last; ! 331: { ! 332: switch (last) ! 333: { ! 334: case sequenceAdaptivePixelVector: ! 335: case sequenceCompressedPixelVector: ! 336: case sequencePackedPixelVector: ! 337: case sequenceLargeVector: extendpixel(nbytes); break; ! 338: case sequenceComment: fseek(fp, (long) nbytes, 1); break; ! 339: case sequenceInteger: ! 340: case sequenceRational: extendnumber(nbytes); break; ! 341: case sequenceString: ! 342: case sequenceIdentifier: extendstring(nbytes); break; ! 343: default: error(err4, filepos, last, nbytes); ! 344: } ! 345: } ! 346: ! 347: seq_insertfile(nbytes) ! 348: int nbytes; ! 349: { ! 350: error(err5, filepos); ! 351: } ! 352: ! 353: seq_largevector(nbytes) ! 354: int nbytes; ! 355: { ! 356: int b; ! 357: long bytepos, bytelength; ! 358: unsigned char *ptr, **array; ! 359: b = getc(fp) & 0377; /* read the number of bytes/integer. */ ! 360: bytepos = ftell(fp); ! 361: bytelength = nbytes-1; ! 362: array = malloc(2*sizeof(unsigned char *)); ! 363: array[0] = makeintegers(b, bytepos, bytelength); ! 364: array[1] = (unsigned char *) 0; ! 365: ptr = makevector(array, type_vector, subtype_integers); ! 366: fseek(fp, bytelength, 1); ! 367: free(array[0]); ! 368: free(array); ! 369: push(ptr); ! 370: } ! 371: ! 372: ! 373: seq_adaptivepixel(nbytes) ! 374: int nbytes; ! 375: { ! 376: pushpixel(nbytes, "adaptive"); ! 377: } ! 378: ! 379: seq_compressedpixel(nbytes) ! 380: int nbytes; ! 381: { ! 382: pushpixel(nbytes, "compressed"); ! 383: } ! 384: ! 385: seq_packedpixel(nbytes) ! 386: int nbytes; ! 387: { ! 388: pushpixel(nbytes, "packed"); ! 389: } ! 390: ! 391: seq_identifier(nbytes) ! 392: int nbytes; ! 393: { ! 394: pushstring(nbytes, subtype_identifier); ! 395: } ! 396: ! 397: seq_string(nbytes) ! 398: int nbytes; ! 399: { ! 400: pushstring(nbytes, subtype_string); ! 401: } ! 402: ! 403: seq_unknown(type, nbytes) ! 404: int type, nbytes; ! 405: { ! 406: error(err4, filepos, type, nbytes); ! 407: } ! 408: ! 409: seq_integer(nbytes) ! 410: int nbytes; ! 411: { ! 412: pushinteger(nbytes, subtype_integer); ! 413: } ! 414: ! 415: seq_rational(nbytes) ! 416: int nbytes; ! 417: { ! 418: pushinteger(nbytes, subtype_rational); ! 419: } ! 420: ! 421: shortnum(number) ! 422: int number; ! 423: { ! 424: unsigned char value[2]; ! 425: unsigned char *ptr; ! 426: value[0] = (number >> 8) & 0377; ! 427: value[1] = number & 0377; ! 428: ptr = makenumber(2, value, subtype_integer); ! 429: push(ptr); ! 430: } ! 431: ! 432: ! 433: /* ! 434: * Private procedures to this module. ! 435: * ! 436: */ ! 437: ! 438: static pushinteger(nbytes, subtype) ! 439: int nbytes, subtype; ! 440: { ! 441: int n; ! 442: unsigned char *ptr; ! 443: unsigned char *array; ! 444: array = (unsigned char *) malloc(nbytes); ! 445: for (n=0; n < nbytes; n++) array[n] = getc(fp) & 0377; ! 446: ptr = makenumber(nbytes, array, subtype); ! 447: free(array); ! 448: push(ptr); ! 449: } ! 450: ! 451: static pushstring(nbytes, subtype) ! 452: int nbytes, subtype; ! 453: { ! 454: int n; ! 455: unsigned char *ptr; ! 456: char *string; ! 457: string = (char *) malloc(nbytes+1); ! 458: for (n=0; n < nbytes; n++) string[n] = getc(fp) & 0377; ! 459: string[nbytes] = (char) 0; ! 460: ptr = makestring(string, subtype); ! 461: free(string); ! 462: push(ptr); ! 463: } ! 464: ! 465: static pushpixel(nbytes, compression) ! 466: int nbytes; ! 467: char *compression; ! 468: { ! 469: extern unsigned char *decompressID(); ! 470: extern unsigned char *decompressOP(); ! 471: extern unsigned char *imagedata(); ! 472: unsigned char *ptr, *idvec, **array; ! 473: idvec = decompressID(compression); ! 474: array = malloc(3*sizeof(unsigned char *)); ! 475: array[0] = decompressOP(idvec); ! 476: array[1] = imagedata(nbytes); ! 477: array[2] = (unsigned char *) 0; ! 478: ptr = makevector(array, type_vector, subtype_samples); ! 479: free(idvec); ! 480: free(array[0]); ! 481: free(array[1]); ! 482: free(array); ! 483: push(ptr); ! 484: } ! 485: ! 486: static unsigned char *decompressID(compression) ! 487: char *compression; ! 488: { ! 489: unsigned char *ptr, **array; ! 490: array = malloc(3*sizeof(unsigned char *)); ! 491: array[0] = makestring("xerox", subtype_identifier); ! 492: array[1] = makestring(compression, subtype_identifier); ! 493: array[2] = (unsigned char *) 0; ! 494: ptr = makevector(array, type_vector, subtype_general); ! 495: free(array[0]); ! 496: free(array[1]); ! 497: free(array); ! 498: return(ptr); ! 499: } ! 500: ! 501: static unsigned char *decompressOP(idvec) ! 502: unsigned char *idvec; ! 503: { ! 504: unsigned char *ptr, **array; ! 505: array = malloc(2*sizeof(unsigned char *)); ! 506: array[0] = makeidentifier(idvec, "decompressionOps/"); ! 507: array[1] = (unsigned char *) 0; ! 508: ptr = makeoperator(array, subtype_decompressop); ! 509: free(array[0]); ! 510: free(array); ! 511: return(ptr); ! 512: } ! 513: ! 514: static unsigned char *imagedata(nbytes) ! 515: int nbytes; ! 516: { ! 517: long bytepos, bytelength; ! 518: unsigned char *ptr, **array; ! 519: bytepos = ftell(fp); ! 520: bytelength = nbytes; ! 521: array = malloc(2*sizeof(unsigned char *)); ! 522: array[0] = makeintegers(2, bytepos, bytelength); ! 523: array[1] = (unsigned char *) 0; ! 524: ptr = makevector(array, type_vector, subtype_integers); ! 525: fseek(fp, bytelength, 1); ! 526: free(array[0]); ! 527: free(array); ! 528: return(ptr); ! 529: } ! 530: ! 531: static popint() ! 532: { ! 533: int result; ! 534: unsigned char *ptr; ! 535: ptr = pop(type_number, 0); ! 536: result = getint(ptr); ! 537: free(ptr); ! 538: return(result); ! 539: } ! 540: ! 541: static double popdouble() ! 542: { ! 543: double result; ! 544: unsigned char *ptr; ! 545: ptr = pop(type_number, 0); ! 546: result = getdouble(ptr); ! 547: free(ptr); ! 548: return(result); ! 549: } ! 550: ! 551: static unsigned char *popidentifier(prefix) ! 552: char *prefix; /* should end with '/' character */ ! 553: { ! 554: unsigned char *ptr, *composite; ! 555: ptr = pop(type_vector, subtype_general); ! 556: composite = makeidentifier(ptr, prefix); ! 557: free(ptr); ! 558: return(composite); ! 559: } ! 560: ! 561: static extendpixel(nbytes) ! 562: int nbytes; ! 563: { ! 564: int n, depth; ! 565: long bytepos, bytelength; ! 566: unsigned char *ptr, **samplesarray, **integersarray; ! 567: unsigned char *newptr, **newarray; ! 568: ptr = pop(type_vector, subtype_samples); ! 569: samplesarray = getvector(ptr); ! 570: depth = getdepth(samplesarray[1]); ! 571: integersarray = getvector(samplesarray[1]); ! 572: newarray = malloc((depth+2)*sizeof(unsigned char *)); ! 573: for (n=0; n < depth; n++) newarray[n] = integersarray[n]; ! 574: bytepos = ftell(fp); ! 575: bytelength = nbytes; ! 576: newarray[depth] = makeintegers(2, bytepos, bytelength); ! 577: newarray[depth+1] = (unsigned char *) 0; ! 578: samplesarray[1] = makevector(newarray, type_vector, subtype_integers); ! 579: newptr = makevector(samplesarray, type_vector, subtype_samples); ! 580: fseek(fp, bytelength, 1); ! 581: free(ptr); ! 582: free(newarray[depth]); ! 583: free(samplesarray[1]); ! 584: free(samplesarray); ! 585: free(integersarray); ! 586: free(newarray); ! 587: push(newptr); ! 588: } ! 589: ! 590: static extendnumber(nbytes) ! 591: int nbytes; ! 592: { ! 593: int n, len; ! 594: unsigned char *number, *newnumber; ! 595: unsigned char *ptr, *newptr; ! 596: ptr = pop(type_number, subtype_integer | subtype_rational); ! 597: number = getnumber(ptr); ! 598: len = getnumlen(ptr); ! 599: newnumber = (unsigned char *) malloc(len+nbytes); ! 600: for (n=0; n < len; n++) newnumber[n] = number[n]; ! 601: for (n=0; n < nbytes; n++) newnumber[n+len] = getc(fp) & 0377; ! 602: newptr = makenumber(len+nbytes, newnumber, getsubtype(ptr)); ! 603: free(ptr); ! 604: free(newnumber); ! 605: push(newptr); ! 606: } ! 607: ! 608: static extendstring(nbytes) ! 609: int nbytes; ! 610: { ! 611: int n, len; ! 612: char *string, *newstring; ! 613: unsigned char *ptr, *newptr; ! 614: ptr = pop(type_string, subtype_identifier | subtype_string); ! 615: string = getstring(ptr, 0); ! 616: len = strlen(string); ! 617: newstring = (char *) malloc(len+nbytes+1); ! 618: strcpy(newstring, string); ! 619: for (n=0; n < nbytes; n++) newstring[n+len] = getc(fp) & 0377; ! 620: newstring[len+nbytes] = (char) 0; ! 621: newptr = makestring(newstring, getsubtype(ptr)); ! 622: free(ptr); ! 623: free(newstring); ! 624: push(newptr); ! 625: } ! 626: ! 627: ! 628: ! 629: ! 630: ! 631: /* Change Log ! 632: * ! 633: * K. Knox, 1-Apr-85 23:45:46, Created first version. ! 634: * K. Knox, 13-May-85 10:25:25, Fixed bugs in calls to maketransformation. ! 635: * K. Knox, 13-May-85 10:25:25, Fixed bug in angle calculation in op_rotate. ! 636: * ! 637: * ! 638: * ! 639: */ ! 640: ! 641: ! 642: ! 643:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.