Debian Codegraph @

converter/pgm/asciitopgm.c

raw
1 /* asciitopgm.c - read an ASCII graphics file and produce a PGM
2 **
3 ** Copyright (C) 1989 by Wilson H. Bent, Jr
4 **
5 ** - Based on fstopgm.c and other works which bear the following notice:
6 ** Copyright (C) 1989 by Jef Poskanzer.
7 **
8 ** Permission to use, copy, modify, and distribute this software and its
9 ** documentation for any purpose and without fee is hereby granted, provided
10 ** that the above copyright notice appear in all copies and that both that
11 ** copyright notice and this permission notice appear in supporting
12 ** documentation. This software is provided "as is" without express or
13 ** implied warranty.
14 */
15
16 #include <string.h>
17
18 #include "pm_c_util.h"
19 #include "mallocvar.h"
20 #include "nstring.h"
21 #include "shhopt.h"
22 #include "pgm.h"
23
24
25
26 struct CmdlineInfo {
27 /* All the information the user supplied in the command line,
28 in a form easy for the program to use.
29 */
30 const char * inputFileNm; /* File name of input file */
31 unsigned int divisor;
32 unsigned int rows; /* height of output image in pixels */
33 unsigned int cols; /* width of output image in pixels */
34 };
35
36
37
38 static void
39 parseSizeParm(const char * const sizeString,
40 const char * const description,
41 unsigned int * const sizeP) {
42
43 char * endptr;
44 long int sizeLong;
45
46 sizeLong = strtol(sizeString, &endptr, 10);
47 if (strlen(sizeString) > 0 && *endptr != '\0')
48 pm_error("%s argument not an integer: '%s'",
49 description, sizeString);
50 else if (sizeLong > INT_MAX - 2)
51 pm_error("%s argument is too large "
52 "for computations: %ld",
53 description, sizeLong);
54 else if (sizeLong <= 0)
55 pm_error("%s argument is not positive: %ld",
56 description, sizeLong);
57 else
58 *sizeP = (unsigned int) sizeLong;
59 }
60
61
62
63 static void
64 parseCommandLine(int argc, const char ** argv,
65 struct CmdlineInfo * const cmdlineP) {
66 /*----------------------------------------------------------------------------
67 Note that the file spec array we return is stored in the storage that
68 was passed to us as the argv array.
69 -----------------------------------------------------------------------------*/
70 optEntry * option_def;
71 /* Instructions to OptParseOptions3 on how to parse our options.
72 */
73 optStruct3 opt;
74
75 unsigned int option_def_index;
76 unsigned int divisorSpec;
77
78 MALLOCARRAY_NOFAIL(option_def, 100);
79
80 option_def_index = 0; /* incremented by OPTENTRY */
81 OPTENT3(0, "divisor", OPT_UINT, &cmdlineP->divisor, &divisorSpec, 0);
82
83 opt.opt_table = option_def;
84 opt.short_allowed = false; /* We have no short (old-fashioned) options */
85 opt.allowNegNum = false; /* We have no parms that are negative numbers */
86
87 pm_optParseOptions4(&argc, argv, opt, sizeof(opt), 0);
88 /* Uses and sets argc, argv, and some of *cmdlineP and others. */
89
90 if (!divisorSpec)
91 cmdlineP->divisor = 1;
92 else {
93 if (cmdlineP->divisor == 0)
94 pm_error("Divisor cannot be zero");
95 }
96
97 if (argc-1 < 2)
98 pm_error("Not enough arguments. Need at least height and width "
99 "of output image, in pixels");
100 else {
101 parseSizeParm(argv[1], "height", &cmdlineP->rows);
102 parseSizeParm(argv[2], "width", &cmdlineP->cols);
103
104 if (argc-1 < 3)
105 cmdlineP->inputFileNm = "-";
106 else {
107 cmdlineP->inputFileNm = argv[3];
108
109 if (argc-1 > 3) {
110 pm_error("Too many arguments: %u. Only possible "
111 "non-option arguments are "
112 "height, width, and input file name", argc-1);
113 }
114 }
115 }
116
117 free(option_def);
118 }
119
120
121
122 static unsigned int const gmap [128] = {
123 /*00 nul-bel*/ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
124 /*08 bs -si */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
125 /*10 dle-etb*/ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
126 /*18 can-us */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
127 /*20 sp - ' */ 0x00, 0x21, 0x1b, 0x7f, 0x70, 0x25, 0x20, 0x0a,
128 /*28 ( - / */ 0x11, 0x11, 0x2a, 0x2b, 0x0b, 0x13, 0x04, 0x10,
129 /*30 0 - 7 */ 0x30, 0x28, 0x32, 0x68, 0x39, 0x35, 0x39, 0x16,
130 /*38 8 - ? */ 0x38, 0x39, 0x14, 0x15, 0x11, 0x1c, 0x11, 0x3f,
131 /*40 @ - G */ 0x40, 0x49, 0x52, 0x18, 0x44, 0x3c, 0x38, 0x38,
132 /*48 H - O */ 0x55, 0x28, 0x2a, 0x70, 0x16, 0x7f, 0x70, 0x14,
133 /*50 P - W */ 0x60, 0x20, 0x62, 0x53, 0x1a, 0x55, 0x36, 0x57,
134 /*58 X - _ */ 0x50, 0x4c, 0x5a, 0x24, 0x10, 0x24, 0x5e, 0x13,
135 /*60 ` - g */ 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
136 /*68 h - o */ 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x2a,
137 /*70 p - w */ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
138 /*78 x -del*/ 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
139 };
140
141 static gray const maxval = 127;
142
143
144
145 static void
146 zeroObuf(unsigned int * const obuf,
147 unsigned int const cols) {
148
149 unsigned int col;
150 for (col = 0; col < cols; ++col)
151 obuf[col] = 0;
152 }
153
154
155
156 static void
157 convertRowToPgm(const unsigned int * const obuf,
158 unsigned int const cols,
159 gray const maxval,
160 gray * const grayrow) {
161 /*----------------------------------------------------------------------------
162 Convert the row in obuf[], which is 'cols' columns wide, to PGM
163 in grayrow[] with maxval 'maxval'.
164
165 The values in 'obuf' are _darkness_ values; 0 is white; 'maxval' is black.
166 -----------------------------------------------------------------------------*/
167 unsigned int col;
168
169 for (col = 0; col < cols; ++col)
170 grayrow[col] = maxval - MIN(maxval, obuf[col]);
171 }
172
173
174
175 static bool warnedNonAscii;
176
177 static unsigned int
178 darknessOfChar(char const c) {
179
180 unsigned char asciifiedC;
181
182 if (c & 0x80) { /* !isascii(c) */
183 if (!warnedNonAscii) {
184 pm_message("Warning: non-ASCII char(s) in input");
185 warnedNonAscii = true;
186 }
187 asciifiedC = c & 0x7f; /* toascii(c) */
188 } else
189 asciifiedC = c;
190
191 return gmap[asciifiedC];
192 }
193
194
195
196 static void
197 convertAsciiToPgm(FILE * const ifP,
198 unsigned int const cols,
199 unsigned int const rows,
200 unsigned int const divisor,
201 gray const maxval,
202 gray ** const grays) {
203
204 unsigned int * obuf;
205 /* The current row, in darkness values (0 is white; 127 is maximum
206 black)
207 */
208 unsigned int outRow;
209 unsigned int outCursor;
210 bool beginningOfImage;
211 bool beginningOfLine;
212 bool warnedTrunc;
213 bool eof;
214
215 MALLOCARRAY(obuf, cols);
216 if (obuf == NULL)
217 pm_error("Unable to allocate memory for %u columns", cols);
218
219 zeroObuf(obuf, cols);
220
221 warnedNonAscii = false;
222 warnedTrunc = false;
223 outCursor = 0;
224 beginningOfImage = true;
225 beginningOfLine = true;
226 for (eof = false, outRow = 0; outRow < rows && !eof; ) {
227 int c;
228
229 c = getc(ifP);
230
231 if (c == EOF)
232 eof = true;
233 else {
234 if (beginningOfLine) {
235 if (c == '+') {
236 /* + at start of line means rest of line
237 overstrikes previous
238 */
239 c = getc(ifP);
240 if (c == EOF)
241 eof = true;
242 } else {
243 if (!beginningOfImage) {
244 /* Output previous line, move to next */
245
246 convertRowToPgm(obuf, cols, maxval, grays[outRow]);
247
248 zeroObuf(obuf, cols);
249
250 ++outRow;
251 }
252 }
253 outCursor = 0;
254 beginningOfLine = false;
255 }
256 if (!eof) {
257 if (c == '\n')
258 beginningOfLine = true;
259 else {
260 if (outRow < rows && outCursor < cols)
261 obuf[outCursor++] += darknessOfChar(c) / divisor;
262 else {
263 if (!warnedTrunc) {
264 pm_message("Warning: "
265 "truncating image to %u columns "
266 "x %u rows", cols, rows);
267 warnedTrunc = true;
268 }
269 }
270 }
271 }
272 }
273 beginningOfImage = false;
274 }
275 while (outRow < rows) {
276 /* Output previous line, move to next */
277
278 convertRowToPgm(obuf, cols, maxval, grays[outRow]);
279
280 zeroObuf(obuf, cols);
281
282 ++outRow;
283 }
284 free(obuf);
285 }
286
287
288
289 int
290 main(int argc, const char ** argv) {
291
292 struct CmdlineInfo cmdline;
293 FILE * ifP;
294 gray ** grays;
295
296 pm_proginit(&argc, argv);
297
298 parseCommandLine(argc, argv, &cmdline);
299
300 ifP = pm_openr(cmdline.inputFileNm);
301
302 grays = pgm_allocarray(cmdline.cols, cmdline.rows);
303
304 convertAsciiToPgm(ifP, cmdline.cols, cmdline.rows, cmdline.divisor,
305 maxval, grays);
306
307 pm_close(ifP);
308
309 pgm_writepgm(stdout, grays, cmdline.cols, cmdline.rows, maxval, 0);
310
311 pgm_freearray(grays, cmdline.rows);
312
313 return 0;
314 }
315
316