Debian Codegraph @

converter/pbm/atktopbm.c

raw
1 /* atktopbm.c - convert Andrew Toolkit raster object to portable bitmap
2 **
3 ** Copyright (C) 1991 by Bill Janssen
4 **
5 ** Permission to use, copy, modify, and distribute this software and its
6 ** documentation for any purpose and without fee is hereby granted, provided
7 ** that the above copyright notice appear in all copies and that both that
8 ** copyright notice and this permission notice appear in supporting
9 ** documentation. This software is provided "as is" without express or
10 ** implied warranty.
11 */
12
13 #include <stdio.h>
14 #include <string.h>
15 #include <sys/types.h>
16
17 #include "nstring.h"
18 #include "pbm.h"
19 #include "mallocvar.h"
20
21
22 /* readatkraster
23 **
24 ** Routine for reading rasters in .raster form. (BE2 rasters version 2.)
25 */
26
27 /* codes for data stream */
28 #define WHITEZERO 'f'
29 #define WHITETWENTY 'z'
30 #define BLACKZERO 'F'
31 #define BLACKTWENTY 'Z'
32 #define OTHERZERO 0x1F
33
34 #define WHITEBYTE 0x00
35 #define BLACKBYTE 0xFF
36
37 /* error codes (copied from $ANDREW/atk/basics/common/dataobj.ch) */
38 /* return values from Read */
39 #define dataobject_NOREADERROR 0
40 #define dataobject_PREMATUREEOF 1
41 #define dataobject_NOTBE2DATASTREAM 2 /* backward compatibility */
42 #define dataobject_NOTATKDATASTREAM 2 /* preferred version */
43 #define dataobject_MISSINGENDDATAMARKER 3
44 #define dataobject_OBJECTCREATIONFAILED 4
45 #define dataobject_BADFORMAT 5
46
47 /* ReadRow(file, row, length)
48 ** Reads from 'file' the encoding of bytes to fill in 'row'. Row will be
49 ** truncated or padded (with WHITE) to exactly 'length' bytes.
50 **
51 ** Returns the code that terminated the row. This may be
52 ** '|' correct end of line
53 ** '\0' if the length was satisfied (before a terminator)
54 ** EOF if the file ended
55 ** '\' '{' other recognized ends.
56 ** The '|' is the expected end and pads the row with WHITE.
57 ** The '\' and '{' are error conditions and may indicate the
58 ** beginning of some other portion of the data stream.
59 ** If the terminator is '\' or '{', it is left at the front of the input.
60 ** '|' is gobbled up.
61 */
62
63 /* macros to generate case entries for switch statement */
64 #define CASE1(v) case v
65 #define CASE4(v) case v: case (v)+1: case (v)+2: case(v)+3
66 #define CASE6(v) CASE4(v): case ((v)+4): case ((v)+5)
67 #define CASE8(v) CASE4(v): CASE4((v)+4)
68
69
70
71 static long
72 ReadRow(FILE * const file,
73 unsigned char * const row,
74 long const length) {
75 /*----------------------------------------------------------------------------
76 'file' is where to get them from.
77 'row' is where to put bytes.
78 'length' is how many bytes in row must be filled.
79
80 Return the delimiter that marks the end of the row, or EOF if EOF marks
81 the end of the row, or NUL in some cases.
82 -----------------------------------------------------------------------------*/
83 /* Each input character is processed by the central loop. There are
84 ** some input codes which require two or three characters for
85 ** completion; these are handled by advancing the state machine.
86 ** Errors are not processed; instead the state machine is reset
87 ** to the Ready state whenever a character unacceptable to the
88 ** current state is read.
89 */
90 enum StateCode {
91 Ready,
92 /* any input code is allowed */
93 HexDigitPending,
94 /* have seen the first of a hex digit pair */
95 RepeatPending,
96 /* repeat code has been seen: must be followed by two hex digits
97 */
98 RepeatAndDigit
99 /* have seen repeat code and its first following digit */
100 };
101
102 enum StateCode InputState; /* current state */
103 int c; /* the current input character */
104 long repeatcount; /* current repeat value */
105 long hexval; /* current hex value */
106 long pendinghex; /* the first of a pair of hex characters */
107 int lengthRemaining;
108 unsigned char * cursor;
109
110 /* We cannot exit when length becomes zero because we need to check
111 ** to see if a row ending character follows. Thus length is checked
112 ** only when we get a data generating byte. If length then is
113 ** zero, we ungetc the byte.
114 */
115
116 repeatcount = 0; /* initial value */
117 pendinghex = 0; /* initial value */
118
119 lengthRemaining = length; /* initial value */
120 cursor = row; /* initial value */
121 InputState = Ready; /* initial value */
122
123 while ((c=getc(file)) != EOF) switch (c) {
124
125 CASE8(0x0):
126 CASE8(0x8):
127 CASE8(0x10):
128 CASE8(0x18):
129 CASE1(' '):
130 /* control characters and space are legal and ignored */
131 break;
132 CASE1(0x40): /* '@' */
133 CASE1(0x5B): /* '[' */
134 CASE4(0x5D): /* ']' '^' '_' '`' */
135 CASE4(0x7D): /* '}' '~' DEL 0x80 */
136 default: /* all above 0x80 */
137 /* error code: Ignored at present. Reset InputState. */
138 InputState = Ready;
139 break;
140
141 CASE1(0x7B): /* '{' */
142 CASE1(0x5C): /* '\\' */
143 /* illegal end of line: exit anyway */
144 ungetc(c, file); /* retain terminator in stream */
145 /* DROP THROUGH */
146 CASE1(0x7C): /* '|' */
147 /* legal end of row: may have to pad */
148 while (lengthRemaining-- > 0)
149 *cursor++ = WHITEBYTE;
150 return c;
151
152 CASE1(0x21):
153 CASE6(0x22):
154 CASE8(0x28):
155 /* punctuation characters: repeat byte given by two
156 ** succeeding hex chars
157 */
158 if (lengthRemaining <= 0) {
159 ungetc(c, file);
160 return('\0');
161 }
162 repeatcount = c - OTHERZERO;
163 InputState = RepeatPending;
164 break;
165
166 CASE8(0x30):
167 CASE8(0x38):
168 /* digit (or following punctuation) - hex digit */
169 hexval = c - 0x30;
170 goto hexdigit;
171 CASE6(0x41):
172 /* A ... F - hex digit */
173 hexval = c - (0x41 - 0xA);
174 goto hexdigit;
175 CASE6(0x61):
176 /* a ... f - hex digit */
177 hexval = c - (0x61 - 0xA);
178 goto hexdigit;
179
180 CASE8(0x67):
181 CASE8(0x6F):
182 CASE4(0x77):
183 /* g ... z - multiple WHITE bytes */
184 if (lengthRemaining <= 0) {
185 ungetc(c, file);
186 return('\0');
187 }
188 repeatcount = c - WHITEZERO;
189 hexval = WHITEBYTE;
190 goto store;
191 CASE8(0x47):
192 CASE8(0x4F):
193 CASE4(0x57):
194 /* G ... Z - multiple BLACK bytes */
195 if (lengthRemaining <= 0) {
196 ungetc(c, file);
197 return('\0');
198 }
199 repeatcount = c - BLACKZERO;
200 hexval = BLACKBYTE;
201 goto store;
202
203 hexdigit:
204 /* process a hex digit. Use InputState to determine
205 what to do with it. */
206 if (lengthRemaining <= 0) {
207 ungetc(c, file);
208 return('\0');
209 }
210 switch(InputState) {
211 case Ready:
212 InputState = HexDigitPending;
213 pendinghex = hexval << 4;
214 break;
215 case HexDigitPending:
216 hexval |= pendinghex;
217 repeatcount = 1;
218 goto store;
219 case RepeatPending:
220 InputState = RepeatAndDigit;
221 pendinghex = hexval << 4;
222 break;
223 case RepeatAndDigit:
224 hexval |= pendinghex;
225 goto store;
226 }
227 break;
228
229 store:
230 /* generate byte(s) into the output row
231 Use repeatcount, depending on state. */
232 if (lengthRemaining < repeatcount)
233 /* reduce repeat count if it would exceed
234 available space */
235 repeatcount = lengthRemaining;
236 lengthRemaining -= repeatcount; /* do this before repeatcount-- */
237 while (repeatcount-- > 0)
238 *cursor++ = hexval;
239 InputState = Ready;
240 break;
241
242 } /* end of while( - )switch( - ) */
243 return EOF;
244 }
245
246
247
248 #undef CASE1
249 #undef CASE4
250 #undef CASE6
251 #undef CASE8
252
253
254
255 static void
256 ReadATKRaster(FILE * const ifP) {
257
258 int row; /* count rows; byte length of row */
259 int version;
260 char keyword[6];
261 int discardid;
262 int objectid; /* id read for the incoming pixel image */
263 long tc; /* temp */
264 int width, height; /* dimensions of image */
265 bit * bitrow;
266
267 if (fscanf(ifP, "\\begindata{raster,%d", &discardid) != 1
268 || getc(ifP) != '}' || getc(ifP) != '\n')
269 pm_error ("input file not Andrew raster object");
270
271 fscanf(ifP, " %d ", &version);
272 if (version < 2)
273 pm_error ("version too old to parse");
274
275 {
276 unsigned int options;
277 long xscale, yscale;
278 long xoffset, yoffset, subwidth, subheight;
279 /* ignore all these features: */
280 fscanf(ifP, " %u %ld %ld %ld %ld %ld %ld",
281 &options, &xscale, &yscale, &xoffset,
282 &yoffset, &subwidth, &subheight);
283 }
284 /* scan to end of line in case this is actually something beyond V2 */
285 while (((tc=getc(ifP)) != '\n') && (tc != '\\') && (tc != EOF)) {}
286
287 /* read the keyword */
288 fscanf(ifP, " %5s", keyword);
289 if (!streq(keyword, "bits"))
290 pm_error ("keyword is not 'bits'!");
291
292 fscanf(ifP, " %d %d %d ", &objectid, &width, &height);
293
294 if (width < 1 || height < 1 || width > 1000000 || height > 1000000)
295 pm_error("bad width or height");
296 /* Note: Whether these values are upper limits set by the author of
297 the original version of this program, or come from the official
298 file format specification is unknown.
299 */
300
301 pbm_writepbminit(stdout, width, height, 0);
302 bitrow = pbm_allocrow_packed(width);
303
304 for (row = 0; row < height; ++row) {
305 unsigned int const rowlen = (width + 7) / 8;
306 long const nextChar = ReadRow(ifP, bitrow, rowlen);
307
308 switch (nextChar) {
309 case '|':
310 pbm_writepbmrow_packed(stdout, bitrow, width, 0);
311 break;
312 case EOF:
313 pm_error("premature EOF");
314 break;
315 default:
316 pm_error("bad format");
317 }
318 }
319
320 pbm_freerow_packed(bitrow);
321
322 while (! feof(ifP) && getc(ifP) != '\\') {}; /* scan for \enddata */
323
324 if (fscanf(ifP, "enddata{raster,%d", &discardid) != 1
325 || getc(ifP) != '}' || getc(ifP) != '\n')
326 pm_error("missing end-of-object marker");
327 }
328
329
330
331 int
332 main(int argc, const char ** argv) {
333
334 FILE * ifP;
335
336 pm_proginit(&argc, argv);
337
338 if (argc-1 < 1)
339 ifP = stdin;
340 else {
341 ifP = pm_openr(argv[1]);
342
343 if (argc-1 > 1)
344 pm_error("Too many arguments. The only possible argument is "
345 "the input file name");
346 }
347
348 ReadATKRaster(ifP);
349
350 pm_close(ifP);
351
352 pm_close(stdout);
353
354 return 0;
355 }
356
357
358