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