Debian Codegraph @

converter/pbm/pbmtoxbm.c

raw
1 /* pbmtoxbm.c - read a PBM image and produce an X11/X10 bitmap file
2 **
3 ** Copyright (C) 1988 by Jef Poskanzer.
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 /* 2006.10 (afu)
14 Changed bitrow from plain to raw, read function from pbm_readpbmrow() to
15 pbm_readpbmrow_packed(). Retired bitwise transformation functions.
16
17 Output function putitem rewritten to handle both X10 and X11.
18
19 Added -name option. There is no check for the string thus given.
20
21 */
22
23 #define _DEFAULT_SOURCE 1 /* New name for SVID & BSD source defines */
24 #define _BSD_SOURCE 1 /* Make sure strdup() is in string.h */
25 #define _XOPEN_SOURCE 500 /* Make sure strdup() is in string.h */
26
27 #include <assert.h>
28 #include <string.h>
29
30 #include "pm_c_util.h"
31 #include "pbm.h"
32 #include "shhopt.h"
33 #include "mallocvar.h"
34 #include "bitreverse.h"
35 #include "nstring.h"
36
37
38 enum XbmVersion { X10, X11 };
39
40 struct CmdlineInfo {
41 /* All the information the user supplied in the command line,
42 in a form easy for the program to use.
43 */
44 const char * inputFileName;
45 const char * name;
46 enum XbmVersion xbmVersion;
47 };
48
49 static void
50 parseCommandLine(int argc,
51 const char ** argv,
52 struct CmdlineInfo *cmdlineP ) {
53 /*----------------------------------------------------------------------------
54 Parse program command line described in Unix standard form by argc
55 and argv. Return the information in the options as *cmdlineP.
56
57 If command line is internally inconsistent (invalid options, etc.),
58 issue error message to stderr and abort program.
59
60 Note that the strings we return are stored in the storage that
61 was passed to us as the argv array. We also trash *argv.
62 -----------------------------------------------------------------------------*/
63 optEntry * option_def;
64 optStruct3 opt;
65 unsigned int option_def_index;
66 unsigned int x10, x11, nameSpec;
67 MALLOCARRAY_NOFAIL(option_def, 100);
68
69 option_def_index = 0; /* incremented by OPTENT3 */
70
71 OPTENT3(0, "name", OPT_STRING, &cmdlineP->name, &nameSpec, 0);
72 OPTENT3(0, "x10" , OPT_FLAG, NULL, &x10, 0);
73 OPTENT3(0, "x11" , OPT_FLAG, NULL, &x11, 0);
74
75 opt.opt_table = option_def;
76 opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */
77 opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */
78
79 pm_optParseOptions4(&argc, argv, opt, sizeof(opt), 0);
80 /* Uses and sets argc, argv, and some of *cmdlineP and others. */
81
82 if (!nameSpec)
83 cmdlineP->name = NULL;
84 else if (strlen(cmdlineP->name) > 56)
85 pm_error("Image name too long: %d chars. (max 56)",
86 (unsigned)strlen(cmdlineP->name));
87 else if (!ISALPHA(cmdlineP->name[0]) && cmdlineP->name[0] !='_')
88 pm_error("Image name '%s' starts with non-alphabet character.",
89 cmdlineP->name);
90 else {
91 unsigned int i;
92 for (i = 0 ; i < strlen(cmdlineP->name); ++i)
93 if (!ISALNUM(cmdlineP->name[i]) && cmdlineP->name[i] != '_')
94 pm_error("Image name '%s' contains invalid character (%c).",
95 cmdlineP->name, cmdlineP->name[i]);
96 }
97
98 if (x10 && x11)
99 pm_error("You can't specify both -x10 and -x11");
100 else if (x10)
101 cmdlineP->xbmVersion = X10;
102 else
103 cmdlineP->xbmVersion = X11;
104
105 if (argc-1 < 1)
106 cmdlineP->inputFileName = "-";
107 else {
108 cmdlineP->inputFileName = argv[1];
109
110 if (argc-1 > 1)
111 pm_error("Program takes zero or one argument (filename). You "
112 "specified %u", argc-1);
113 }
114 free(option_def);
115 }
116
117
118
119 static void
120 generateName(char const filenameArg[],
121 const char ** const nameP) {
122 /*----------------------------------------------------------------------------
123 Generate a name for the image to put in the bitmap file. Derive it from
124 the filename argument filenameArg[] and return it as a null-terminated
125 string in newly malloc'ed space at *nameP.
126
127 We take the part of the name after the rightmost slash
128 (i.e. filename without the directory path part), stopping before
129 any period. We convert any punctuation to underscores.
130
131 If the argument is "-", meaning standard input, we return the name
132 "noname". Also, if the argument is null or ends in a slash, we
133 return "noname".
134 -----------------------------------------------------------------------------*/
135 if (streq(filenameArg, "-"))
136 *nameP = strdup("noname");
137 else {
138 int nameIndex, argIndex;
139 /* indices into the input and output buffers */
140
141 /* Start just after the rightmost slash, or at beginning if no slash */
142 if (strrchr(filenameArg, '/') == 0)
143 argIndex = 0;
144 else argIndex = strrchr(filenameArg, '/') - filenameArg + 1;
145
146 if (filenameArg[argIndex] == '\0')
147 *nameP = strdup("noname");
148 else {
149 char * name;
150 nameIndex = 0; /* Start at beginning of name buffer */
151
152 name = malloc(strlen(filenameArg));
153
154 while (filenameArg[argIndex] != '\0'
155 && filenameArg[argIndex] != '.') {
156 const char filenameChar = filenameArg[argIndex++];
157 name[nameIndex++] =
158 ISALNUM(filenameChar) ? filenameChar : '_';
159 }
160 name[nameIndex] = '\0';
161 *nameP = name;
162 }
163 }
164 }
165
166
167
168 typedef struct {
169 unsigned short int buff[22];
170 int cnt; /* takes values 0 to 15 (x11) or 21 (x10) */
171 enum XbmVersion version;
172 } ItemWriter;
173
174 static ItemWriter itemWriter;
175
176
177
178 static void
179 putitemX10(unsigned char const item) {
180
181 if (itemWriter.cnt == 22) {
182 /* Buffer is full. Write out one line. */
183 int rc;
184 rc = printf(" 0x%02x%02x,0x%02x%02x,0x%02x%02x,0x%02x%02x,"
185 "0x%02x%02x,0x%02x%02x,0x%02x%02x,0x%02x%02x,"
186 "0x%02x%02x,0x%02x%02x,0x%02x%02x,\n",
187 itemWriter.buff[ 1], itemWriter.buff[ 0],
188 itemWriter.buff[ 3], itemWriter.buff[ 2],
189 itemWriter.buff[ 5], itemWriter.buff[ 4],
190 itemWriter.buff[ 7], itemWriter.buff[ 6],
191 itemWriter.buff[ 9], itemWriter.buff[ 8],
192 itemWriter.buff[11], itemWriter.buff[10],
193 itemWriter.buff[13], itemWriter.buff[12],
194 itemWriter.buff[15], itemWriter.buff[14],
195 itemWriter.buff[17], itemWriter.buff[16],
196 itemWriter.buff[19], itemWriter.buff[18],
197 itemWriter.buff[21], itemWriter.buff[20]
198 );
199
200 if (rc < 0)
201 pm_error("Error writing X10 bitmap raster item. "
202 "printf() failed with errno %d (%s)",
203 errno, strerror(errno));
204
205 itemWriter.cnt = 0;
206 }
207 itemWriter.buff[itemWriter.cnt++] = bitreverse[item];
208 }
209
210
211
212 static void
213 putitemX11(unsigned char const item) {
214
215 if (itemWriter.cnt == 15 ) {
216 /* Buffer is full. Write out one line. */
217 int rc;
218 rc = printf(" 0x%02x,0x%02x,0x%02x,0x%02x,"
219 "0x%02x,0x%02x,0x%02x,0x%02x,"
220 "0x%02x,0x%02x,0x%02x,0x%02x,"
221 "0x%02x,0x%02x,0x%02x,\n",
222 itemWriter.buff[ 0], itemWriter.buff[ 1],
223 itemWriter.buff[ 2], itemWriter.buff[ 3],
224 itemWriter.buff[ 4], itemWriter.buff[ 5],
225 itemWriter.buff[ 6], itemWriter.buff[ 7],
226 itemWriter.buff[ 8], itemWriter.buff[ 9],
227 itemWriter.buff[10], itemWriter.buff[11],
228 itemWriter.buff[12], itemWriter.buff[13],
229 itemWriter.buff[14]
230 );
231 if (rc < 0)
232 pm_error("Error writing X11 bitmap raster item. "
233 "printf() failed with errno %d (%s)",
234 errno, strerror(errno));
235
236 itemWriter.cnt = 0;
237 }
238 itemWriter.buff[itemWriter.cnt++] = bitreverse[item];
239 }
240
241
242
243 static void
244 putitem(unsigned char const item) {
245
246 switch (itemWriter.version) {
247 case X10: putitemX10(item); break;
248 case X11: putitemX11(item); break;
249 }
250 }
251
252
253
254 static void
255 puttermX10(void) {
256
257 unsigned int i;
258
259 assert(itemWriter.cnt % 2 == 0);
260
261 for (i = 0; i < itemWriter.cnt; i += 2) {
262 int rc;
263
264 assert(i + 1 < itemWriter.cnt);
265
266 rc = printf("%s0x%02x%02x%s",
267 (i == 0) ? " " : "",
268 itemWriter.buff[i+1],
269 itemWriter.buff[i],
270 (i + 2 >= itemWriter.cnt) ? "" : ",");
271 if (rc < 0)
272 pm_error("Error writing Item %u at end of X10 bitmap raster. "
273 "printf() failed with errno %d (%s)",
274 i, errno, strerror(errno));
275 }
276 }
277
278
279
280 static void
281 puttermX11(void) {
282
283 unsigned int i;
284
285 for (i = 0; i < itemWriter.cnt; ++i) {
286 int rc;
287
288 rc = printf("%s0x%02x%s",
289 (i == 0) ? " " : "",
290 itemWriter.buff[i],
291 (i + 1 >= itemWriter.cnt) ? "" : ",");
292
293 if (rc < 0)
294 pm_error("Error writing Item %u at end of X11 bitmap raster. "
295 "printf() failed with errno %d (%s)",
296 i, errno, strerror(errno));
297 }
298 }
299
300
301
302 static void
303 putinit(enum XbmVersion const xbmVersion) {
304
305 itemWriter.cnt = 0;
306 itemWriter.version = xbmVersion;
307 }
308
309
310
311 static void
312 putterm(void) {
313
314 switch (itemWriter.version) {
315 case X10: puttermX10(); break;
316 case X11: puttermX11(); break;
317 }
318
319 {
320 int rc;
321
322 rc = printf("};\n");
323
324 if (rc < 0)
325 pm_error("Error writing end of X11 bitmap raster. "
326 "printf() failed with errno %d (%s)",
327 errno, strerror(errno));
328 }
329 }
330
331
332
333 static void
334 writeXbmHeader(enum XbmVersion const xbmVersion,
335 const char * const name,
336 unsigned int const width,
337 unsigned int const height,
338 FILE * const ofP) {
339
340 printf("#define %s_width %u\n", name, width);
341 printf("#define %s_height %u\n", name, height);
342 printf("static %s %s_bits[] = {\n",
343 xbmVersion == X10 ? "short" : "char",
344 name);
345 }
346
347
348
349 static void
350 convertRaster(FILE * const ifP,
351 unsigned int const cols,
352 unsigned int const rows,
353 int const format,
354 FILE * const ofP,
355 enum XbmVersion const xbmVersion) {
356
357 unsigned int const bitsPerUnit = xbmVersion == X10 ? 16 : 8;
358 unsigned int const padright = ROUNDUP(cols, bitsPerUnit) - cols;
359 /* Amount of padding to round cols up to the nearest multiple of
360 8 (if x11) or 16 (if x10).
361 */
362 unsigned int const bitrowBytes = (cols + padright) / 8;
363
364 unsigned char * bitrow;
365 unsigned int row;
366
367 if (cols > UINT_MAX - bitsPerUnit)
368 pm_error("Image is too wide (%u columns) for computations",
369 cols);
370
371 putinit(xbmVersion);
372
373 bitrow = pbm_allocrow_packed(cols + padright);
374
375 for (row = 0; row < rows; ++row) {
376 unsigned int i;
377
378 pbm_readpbmrow_packed(ifP, bitrow, cols, format);
379 pbm_cleanrowend_packed(bitrow, cols);
380
381 if (padright >= 8) {
382 assert(bitrowBytes > 0);
383 bitrow[bitrowBytes-1] = 0x00;
384 }
385 for (i = 0; i < bitrowBytes; ++i)
386 putitem(bitrow[i]);
387 }
388
389 putterm();
390
391 pbm_freerow(bitrow);
392 }
393
394
395
396 int
397 main(int argc,
398 const char ** argv) {
399
400 struct CmdlineInfo cmdline;
401 FILE * ifP;
402 int rows, cols, format;
403 const char * name;
404
405 pm_proginit(&argc, argv);
406
407 parseCommandLine(argc, argv, &cmdline);
408 if (cmdline.name == NULL)
409 generateName(cmdline.inputFileName, &name);
410 else
411 name = strdup(cmdline.name);
412
413 ifP = pm_openr(cmdline.inputFileName);
414
415 pbm_readpbminit(ifP, &cols, &rows, &format);
416
417 writeXbmHeader(cmdline.xbmVersion, name, cols, rows, stdout);
418
419 convertRaster(ifP, cols, rows, format, stdout, cmdline.xbmVersion);
420
421 pm_strfree(name);
422 pm_close(ifP);
423
424 return 0;
425 }
426
427
428