|
|
1.1 root 1: /*
2: * Copyright (c) 1984, 1985, 1986 Xerox Corp.
3: *
4: * This module reads a compressed bitmap file in the MacPaint
5: * format and outputs an RES file.
6: *
7: * For a description of RES (Raster Encoding Standard) see
8: * the booklet entitled "Raster Encoding Standard" XNS Standard 178506
9: * (June, 1985)
10: *
11: * HISTORY
12: * 21-Jul-86 Lee Moore (lee) at Xerox Webster Research Center
13: * Added the ability to generate IP masters, too.
14: *
15: * 07-Jul-86 Lee Moore (lee) at Xerox Webster Research Center
16: * Converted for use with getopt.
17: *
18: * 03-Jun-86 Lee Moore (lee) at Xerox Webster Research Center
19: * Created mp2res from readmac.c .
20: *
21: *
22: * K. Knox, 10-Dec-84 18:51:22, Created readmac.
23: */
24:
25: #include <stdio.h>
26:
27: #include "iptokens.h"
28: #include "literal.h"
29: #include "operator.h"
30:
31: #define err0 "usage: readmac filename\n"
32: #define err1 "mp2res: Could not open, %s.\n"
33:
34: #define TRUE 1
35: #define FALSE 0
36:
37: /* External procedures. */
38: extern char *malloc();
39:
40: /*
41: * Main program
42: * parse command line
43: * call ProcessData to do the work
44: */
45:
46: main(argc, argv)
47: int argc;
48: char *argv[];
49: {
50: int c;
51: int firstRow = 0,
52: lastRow = (720 - 1),
53: firstColumn = 0,
54: lastColumn = (576 - 1),
55: outputFileFD,
56: makeInterpress = FALSE;
57: extern char *optarg;
58: extern optind;
59: FILE *inputFileDesc;
60:
61: outputFileFD = 1; /* standard out */
62:
63: while ((c = getopt(argc, argv, "it:b:l:o:r:")) != EOF)
64: switch (c) {
65: case 'i':
66: makeInterpress = TRUE;
67: break;
68:
69: case 't': /* top */
70: firstRow = atoi(optarg);
71: break;
72:
73: case 'b': /* bottom */
74: lastRow = atoi(optarg);
75: break;
76:
77: case 'l': /* left */
78: firstColumn = atoi(optarg);
79: break;
80:
81: case 'o': /* output file */
82: if( (outputFileFD = creat(optarg, 0664)) < 0) {
83: fprintf(stderr, "mp2res: can't open %s for writing\n", optarg);
84: perror(optarg);
85: exit(1);
86: }
87: break;
88:
89: case 'r': /* right */
90: lastColumn = atoi(optarg);
91: break;
92:
93: default:
94: printf("ipmetrics: option '%c' not allowed\n");
95: }
96:
97: /* do we read from standard input? */
98: if (argc == optind) {
99: inputFileDesc = stdin;
100: } else {
101: /* open the MacPaint file */
102: if ((inputFileDesc = fopen(argv[optind], "r")) == NULL)
103: fprintf(stderr, err1, argv[optind]);
104: }
105:
106: if (! makeInterpress )
107: MakeRES(inputFileDesc, outputFileFD,
108: firstRow, lastRow, firstColumn, lastColumn);
109: else
110: MakeIP(inputFileDesc, outputFileFD,
111: firstRow, lastRow, firstColumn, lastColumn);
112:
113: exit(0);
114: }
115:
116:
117: /*
118: * Convert a MacPaint document to an RES file
119: * don't copy the data that the user doesn't want
120: */
121:
122: MakeRES(inputFileDesc, outputFileFD,
123: firstRow, lastRow, firstColumn, lastColumn)
124: FILE *inputFileDesc;
125: int outputFileFD,
126: firstRow, lastRow, firstColumn, lastColumn;
127: {
128: int i,
129: bufferSize;
130: int pixelsPerScanLine,
131: numberOfScanLines,
132: bytesPerScanLine;
133: unsigned char *image, *p;
134:
135: pixelsPerScanLine = lastColumn - firstColumn + 1;
136: numberOfScanLines = lastRow - firstRow + 1;
137:
138: if( pixelsPerScanLine % 32 != 0 )
139: fprintf(stderr, "internal error: %d is not a multiple of 32\n", pixelsPerScanLine);
140:
141: bytesPerScanLine = (pixelsPerScanLine + 7)/8;
142:
143: /* skip over MacPaint header */
144: for (i=0; i < 512; i++)
145: getc(inputFileDesc);
146:
147: bufferSize = bytesPerScanLine * numberOfScanLines;
148: image = (unsigned char *) malloc((unsigned) bufferSize);
149:
150: res_select(outputFileFD);
151:
152: AppendOp(OP_beginBlock);
153:
154: /* element 1: imageScale */
155: AppendRational(254L, 72L*100*100); /* assume 72 spots/inch */
156: AppendOp(OP_dup);
157: AppendInteger(2L);
158: AppendOp(OP_makevec);
159:
160: /* element 2: xDimension */
161: AppendInteger((long) pixelsPerScanLine); /* xPixels */
162:
163: /* element 3: yDimension */
164: AppendInteger((long) numberOfScanLines); /* yPixels */
165:
166: /* element 4: maskImage */
167: AppendInteger(0L); /* maskImage */
168:
169: /* element 5: colorImage */
170: AppendInteger((long) numberOfScanLines); /* yPixels */
171: AppendInteger((long) pixelsPerScanLine); /* xPixels */
172: AppendInteger(1L); /* sampelsPerPixel */
173: AppendInteger(1L); /* maxSampleValue */
174: AppendInteger(1L); /* samplesInterleaved */
175:
176: /* enter transformation to bring it to row major order */
177: AppendInteger(-90L);
178: AppendOp(OP_rotate);
179: AppendInteger(0L);
180: AppendInteger((long) numberOfScanLines);
181: AppendOp(OP_translate);
182: AppendOp(OP_concat);
183:
184: /* skip over as many rows as needed */
185: for (i = 0; i < firstRow; i++)
186: readscan(inputFileDesc, image);
187:
188: /* read the scan lines in */
189: for (i = firstRow, p = image; i <= lastRow; i++, p += bytesPerScanLine)
190: readscan(inputFileDesc, p);
191:
192: AppendPPVector(bufferSize, 1, pixelsPerScanLine, (unsigned char *) image);
193:
194: Op(makepixelarray); /* make the array */
195:
196: /* element 6: colorOperator */
197: AppendInteger(0L);
198:
199: /* element 7: image Properties */
200: AppendIdentifier("imageDescription");
201: AppendString("made from a MacPaint file");
202: Makevec(2);
203:
204: /* element 8: signature */
205: AppendInteger(13086L);
206:
207: AppendOp(OP_endBlock);
208:
209: ip_flush();
210:
211: free((char *) image);
212: }
213:
214: MakeIP(inputFileDesc, outputFileFD,
215: firstRow, lastRow, firstColumn, lastColumn)
216: FILE *inputFileDesc;
217: int outputFileFD,
218: firstRow, lastRow, firstColumn, lastColumn;
219: {
220: int i,
221: bufferSize;
222: int pixelsPerScanLine,
223: numberOfScanLines,
224: bytesPerScanLine;
225: unsigned char *image, *p;
226:
227: pixelsPerScanLine = lastColumn - firstColumn + 1;
228: numberOfScanLines = lastRow - firstRow + 1;
229:
230: if( pixelsPerScanLine % 32 != 0 )
231: fprintf(stderr, "%d is not a multiple of 32\n", pixelsPerScanLine);
232:
233: bytesPerScanLine = (pixelsPerScanLine + 7)/8;
234:
235: for (i=0; i < 512; i++)
236: getc(inputFileDesc);
237:
238: bufferSize = bytesPerScanLine * numberOfScanLines;
239: image = (unsigned char *) malloc((unsigned) bufferSize);
240:
241: ip_select(outputFileFD);
242:
243: AppendOp(OP_beginBlock);
244: AppendOp(OP_beginBody);
245: AppendOp(OP_endBody); /* end preamble */
246: AppendOp(OP_beginBody); /* page 1 (and only) */
247:
248: AppendRational(254L, 72L*100*100); /* assume 72 spots/inch */
249: AppendOp(OP_scale);
250: Translate(.5*.0254, .25*.0254);
251: AppendOp(OP_concat);
252: AppendOp(OP_concatt);
253:
254: AppendInteger((long) numberOfScanLines); /* yPixels */
255: AppendInteger((long) pixelsPerScanLine); /* xPixels */
256: AppendInteger(1L); /* sampelsPerPixel */
257: AppendInteger(1L); /* maxSampleValue */
258: AppendInteger(1L); /* samplesInterleaved */
259:
260: /* enter transformation to bring it to row major order */
261: AppendInteger(-90L);
262: AppendOp(OP_rotate);
263: AppendInteger(0L);
264: AppendInteger((long) numberOfScanLines);
265: AppendOp(OP_translate);
266: AppendOp(OP_concat);
267:
268: /* skip over as many rows as needed */
269: for (i = 0; i < firstRow; i++)
270: readscan(inputFileDesc, image);
271:
272: for (i = firstRow, p = image; i <= lastRow; i++, p += bytesPerScanLine)
273: readscan(inputFileDesc, p);
274:
275: /* output the result to the Interpress file */
276: AppendPPVector(bufferSize, 1, pixelsPerScanLine, (unsigned char *) image);
277:
278: Op(makepixelarray); /* make the array */
279:
280: /* draw a box around the pixel array */
281: AppendInteger(1L); /* unit stroke width */
282: AppendInteger(15L); /* stroke width imager variable */
283: AppendOp(OP_iset);
284: AppendRational(-1L, 2L);
285: AppendRational(-1L, 2L);
286: AppendOp(OP_moveto);
287: AppendInteger((long) (numberOfScanLines + 1));
288: AppendOp(OP_linetoy);
289: AppendInteger((long) (pixelsPerScanLine + 1));
290: AppendOp(OP_linetox);
291: AppendRational(-1L, 2L);
292: AppendOp(OP_linetoy);
293: AppendRational(-1L, 2L);
294: AppendOp(OP_linetox);
295: AppendOp(OP_maskstroke);
296:
297: /* prepare to show pixel array */
298: AppendInteger(0L);
299: AppendInteger(0L);
300: AppendOp(OP_setxy);
301: Op(maskpixel); /* show it */
302:
303: AppendOp(OP_endBody);
304: AppendOp(OP_endBlock);
305:
306: ip_flush();
307:
308: free((char *) image);
309: }
310:
311:
312: /*
313: * Read the next scan line from the MacPaint file
314: */
315:
316: readscan(inputFileDesc, image)
317: FILE *inputFileDesc;
318: unsigned char *image;
319: {
320: int in_pos, count, data_byte;
321:
322: in_pos = 0;
323:
324: while (in_pos < 72)
325: {
326: count = getc(inputFileDesc);
327:
328: if (count > 127)
329: count -= 256;
330:
331: if (count >= 0) {
332: /* run of raw bytes */
333: count++; /* # of bytes to read */
334:
335: while (count--)
336: image[in_pos++] = getc(inputFileDesc);
337: }
338: else {
339: /* run of repeated byte */
340: count = -count+1; /* repetition factor */
341: data_byte = getc(inputFileDesc); /* byte to repeat */
342:
343: while (count--)
344: image[in_pos++] = data_byte;
345: }
346: }
347: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.