Debian Codegraph @

converter/pbm/pktopbm.c

raw
1 /*
2 pktopbm, adapted from "pktopx in C by Tomas Rokicki" by AJCD 1/8/90
3 1998-09-22: jcn <[email protected]>
4 - lots of bugfixes:
5 * always read x/y offset bytes (3x)
6 * reset bmx, bmy to defaults for each char
7 * fix bitmap y placement of dynamically packed char
8 * skip char early if no output file allocated
9 - added debug output
10
11 compile with: cc -lpbm -o pktopbm pktopbm.c
12 */
13
14 #include <stdbool.h>
15 #include <stdio.h>
16 #include <string.h>
17
18 #include "pm_c_util.h"
19 #include "nstring.h"
20 #include "mallocvar.h"
21 #include "shhopt.h"
22 #include "pbm.h"
23
24 #define NAMELENGTH 80
25 #define MAXROWWIDTH 3200
26 #define MAXPKCHAR 256
27
28 struct CmdlineInfo {
29 /* All the information the user supplied in the command line,
30 in a form easy for the program to use.
31 */
32 const char * inputFileNm;
33 unsigned int outputFileCt;
34 /* The number of output files */
35 const char * outputFileNm[MAXPKCHAR];
36 /* The output file name, in order */
37 unsigned int character;
38 unsigned int xSpec;
39 unsigned int x;
40 unsigned int ySpec;
41 unsigned int y;
42 unsigned int debug;
43 };
44
45
46
47 static void
48 parseCommandLine(int argc, const char ** argv,
49 struct CmdlineInfo * const cmdlineP) {
50 /*----------------------------------------------------------------------------
51 Note that the file spec strings we return are stored in the storage that
52 was passed to us as the argv array.
53 -----------------------------------------------------------------------------*/
54 optEntry * option_def;
55 optStruct3 opt;
56
57 unsigned int option_def_index;
58 unsigned int characterSpec;
59 unsigned int firstOutputArgNm;
60
61 MALLOCARRAY_NOFAIL(option_def, 100);
62
63 option_def_index = 0; /* incremented by OPTENTRY */
64 OPTENT3(0, "character", OPT_UINT, &cmdlineP->character,
65 &characterSpec, 0);
66 OPTENT3(0, "x", OPT_UINT, &cmdlineP->x,
67 &cmdlineP->xSpec, 0);
68 OPTENT3(0, "X", OPT_UINT, &cmdlineP->x,
69 &cmdlineP->xSpec, 0);
70 OPTENT3(0, "y", OPT_UINT, &cmdlineP->y,
71 &cmdlineP->ySpec, 0);
72 OPTENT3(0, "Y", OPT_UINT, &cmdlineP->y,
73 &cmdlineP->ySpec, 0);
74 OPTENT3(0, "debug", OPT_UINT, NULL,
75 &cmdlineP->debug, 0);
76
77 opt.opt_table = option_def;
78 opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */
79 opt.allowNegNum = FALSE; /* We may have parms that are negative numbers */
80
81 pm_optParseOptions4(&argc, argv, opt, sizeof(opt), 0);
82 /* Uses and sets argc, argv, and some of *cmdlineP and others. */
83
84 if (characterSpec) {
85 if (cmdlineP->character >= MAXPKCHAR)
86 pm_error("Character number (-character) must be in range 0 to %u",
87 MAXPKCHAR-1);
88 } else
89 cmdlineP->character = 0;
90
91 if (argc-1 < 1) {
92 cmdlineP->inputFileNm = "-";
93 firstOutputArgNm = 1;
94 } else {
95 cmdlineP->inputFileNm = argv[1];
96 firstOutputArgNm = 2;
97 }
98
99 cmdlineP->outputFileCt = 0; /* initial value */
100 {
101 unsigned int argn;
102 bool stdoutUsed;
103 for (argn = firstOutputArgNm, stdoutUsed = false;
104 argn < argc;
105 ++argn) {
106 if (cmdlineP->outputFileCt >= MAXPKCHAR)
107 pm_error("You may not specify more than %u output files.",
108 MAXPKCHAR);
109 cmdlineP->outputFileNm[cmdlineP->outputFileCt++] = argv[argn];
110 if (streq(argv[argn], "-")) {
111 if (stdoutUsed)
112 pm_error("You cannot specify Standard Output ('-') "
113 "for more than one output file");
114 stdoutUsed = true;
115 }
116 }
117 }
118 if (cmdlineP->outputFileCt < 1)
119 cmdlineP->outputFileNm[cmdlineP->outputFileCt++] = "-";
120
121 if (cmdlineP->character + cmdlineP->outputFileCt >= MAXPKCHAR)
122 pm_error("Number of output files (%u) "
123 "plus -character value (%u) exceeds "
124 "the maximum number of characters is a PK font file (%u)",
125 cmdlineP->character, cmdlineP->outputFileCt, MAXPKCHAR);
126 }
127
128
129
130 typedef unsigned char eightbits ;
131
132 static FILE * ifP;
133 static unsigned int pkLoc;
134 static const char * fileName[MAXPKCHAR];
135 static int dynf ;
136 static eightbits inputByte ;
137 static eightbits bitWeight ;
138 static int repeatCount ;
139 static int flagByte ;
140 static int debug = 0;
141
142 #define dprintf(s,d) if (debug) printf(s,d)
143 #define dprintf0(s) if (debug) printf(s)
144
145 static eightbits
146 pkByte() {
147 /*----------------------------------------------------------------------------
148 Get a byte from the PK file
149 -----------------------------------------------------------------------------*/
150 ++pkLoc;
151
152 return getc(ifP);
153 }
154
155
156
157 static int
158 get16() {
159 /*----------------------------------------------------------------------------
160 Get a 16-bit half word from the PK file
161 -----------------------------------------------------------------------------*/
162 int const a = pkByte() ;
163
164 return (a<<8) + pkByte();
165 }
166
167
168
169 static int
170 get32() {
171 /*----------------------------------------------------------------------------
172 Get a 32-bit word from the PK file
173 -----------------------------------------------------------------------------*/
174 int a;
175
176 a = get16(); /* initial value */
177
178 if (a > 32767)
179 a -= 65536;
180
181 return (a << 16) + get16();
182 }
183
184
185
186 static int
187 getNybble() {
188 /*----------------------------------------------------------------------------
189 Get a nibble from current input byte, or new byte if no current byte
190 -----------------------------------------------------------------------------*/
191 eightbits temp;
192
193 if (bitWeight == 0) {
194 inputByte = pkByte() ;
195 bitWeight = 16 ;
196 }
197 temp = inputByte / bitWeight ;
198 inputByte -= temp * bitWeight ;
199 bitWeight >>= 4 ;
200
201 return temp;
202 }
203
204
205
206 static bool
207 getBit() {
208 /*----------------------------------------------------------------------------
209 Get a bit from the current input byte, or a new byte if no current byte
210 -----------------------------------------------------------------------------*/
211 bool temp ;
212
213 bitWeight >>= 1 ;
214 if (bitWeight == 0) {
215 inputByte = pkByte();
216 bitWeight = 128 ;
217 }
218 temp = (inputByte >= bitWeight);
219 if (temp)
220 inputByte -= bitWeight;
221
222 return temp;
223 }
224
225
226
227 static int
228 pkPackedNum() {
229 /*----------------------------------------------------------------------------
230 Unpack a dynamically packed number. dynf is dynamic packing threshold
231 -----------------------------------------------------------------------------*/
232 int i, j ;
233
234 i = getNybble() ;
235
236 if (i == 0) { /* large run count, >= 3 nibbles */
237 do {
238 j = getNybble() ; /* count extra nibbles */
239 i++ ;
240 } while (j == 0) ;
241 while (i > 0) {
242 j = (j<<4) + getNybble() ; /* add extra nibbles */
243 i-- ;
244 }
245 return (j - 15 +((13 - dynf)<<4) + dynf) ;
246 } else if (i <= dynf) return (i) ; /* number > 0 and <= dynf */
247 else if (i < 14) return (((i - dynf - 1)<<4) + getNybble() + dynf + 1) ;
248 else {
249 if (i == 14)
250 repeatCount = pkPackedNum() ; /* get repeat count */
251 else
252 repeatCount = 1 ; /* value 15 indicates repeat count 1 */
253
254 return pkPackedNum();
255 }
256 }
257
258
259
260 static void
261 skipSpecials() {
262 /*----------------------------------------------------------------------------
263 Skip specials in PK files, inserted by Metafont or some other program
264 -----------------------------------------------------------------------------*/
265 do {
266 flagByte = pkByte() ;
267 if (flagByte >= 240)
268 switch(flagByte) {
269 case 240: /* specials of size 1-4 bytes */
270 case 241:
271 case 242:
272 case 243: {
273 int i, j;
274
275 i = 0 ;
276 for (j = 240 ; j <= flagByte ; ++j)
277 i = (i<<8) + pkByte() ;
278 for (j = 1 ; j <= i ; ++j)
279 pkByte() ; /* ignore special */
280 } break ;
281 case 244: /* no-op, parameters to specials */
282 get32() ;
283 case 245: /* start of postamble */
284 case 246: /* no-op */
285 break ;
286 case 247: /* pre-amble in wrong place */
287 case 248:
288 case 249:
289 case 250:
290 case 251:
291 case 252:
292 case 253:
293 case 254:
294 case 255:
295 pm_error("unexpected flag byte %d", flagByte) ;
296 }
297 } while (!(flagByte < 240 || flagByte == 245)) ;
298 }
299
300
301
302 static void
303 ignoreChar(int const car,
304 unsigned int const endOfPacket) {
305 /*----------------------------------------------------------------------------
306 ignore character packet
307 -----------------------------------------------------------------------------*/
308 while (pkLoc != endOfPacket)
309 pkByte();
310
311 if (car < 0 || car >= MAXPKCHAR)
312 pm_message("Character %d out of range", car) ;
313
314 skipSpecials() ;
315 }
316
317
318
319 static void
320 readHeader() {
321 /*----------------------------------------------------------------------------
322 Read the header of the input file.
323
324 Surprisingly, nothing in the header is useful to this program, so we're
325 just reading past it and doing some validation.
326
327 We read through the first flag byte and update the global variable
328 'flagByte'.
329 -----------------------------------------------------------------------------*/
330 unsigned int commentSz;
331 unsigned int i;
332
333 if (pkByte() != 247)
334 pm_error("bad PK file (pre command missing)") ;
335
336 if (pkByte() != 89)
337 pm_error("wrong version of packed file") ;
338
339 commentSz = pkByte() ; /* get header comment size */
340
341 for (i = 1 ; i <= commentSz ; ++i)
342 pkByte() ; /* ignore header comment */
343
344 get32() ; /* ignore designsize */
345 get32() ; /* ignore checksum */
346 if (get32() != get32()) /* h & v pixels per point */
347 pm_message("Warning: aspect ratio not 1:1") ;
348
349 skipSpecials();
350 }
351
352
353
354 static void
355 readCharacterHeader(int * const carP,
356 unsigned int * const endOfPacketP,
357 bool * const mustIgnoreP,
358 int * const cheightP,
359 int * const cwidthP,
360 int * const xoffsP,
361 int * const yoffsP,
362 bool * const turnonP) {
363
364 int packetLength;
365 /* character packet length field value */
366 int cheight, cwidth;
367 /* bounding box height, width field values */
368 int xoffs=0, yoffs=0;
369 bool turnon ;
370 int x;
371
372 dynf = (flagByte >> 4); /* get dynamic packing value */
373 flagByte &= 15;
374 turnon = (flagByte >= 8) ; /* black or white initially? */
375 if (turnon)
376 flagByte &= 7; /* long or short form */
377
378 if (flagByte == 7) { /* long form preamble */
379 packetLength = get32();
380 *carP = get32(); /* character number */
381
382 dprintf0("flagByte7\n");
383 dprintf("car: %d\n", *carP);
384 get32(); /* ignore tfmwidth */
385 x=get32(); /* ignore horiz escapement */
386 dprintf("horiz esc %d\n", x);
387 x=get32(); /* ignore vert escapement */
388 dprintf("vert esc %d\n", x);
389 cwidth = get32(); /* bounding box width */
390 cheight = get32(); /* bounding box height */
391 dprintf("cwidth %d\n", cwidth);
392 dprintf("cheight %d\n", cheight);
393 if (cwidth < 0 || cheight < 0 ||
394 cwidth > 65535 || cheight > 65535) {
395 *mustIgnoreP = true;
396 } else {
397 *mustIgnoreP = false;
398 xoffs = get32() ; /* horiz offset */
399 yoffs = get32() ; /* vert offset */
400 dprintf ("xoffs %d\n", xoffs);
401 dprintf ("yoffs %d\n", yoffs);
402 }
403 } else if (flagByte > 3) { /* extended short form */
404 packetLength = ((flagByte - 4)<<16) + get16() ;
405
406 *carP = pkByte() ; /* char number */
407
408 *mustIgnoreP = false;
409
410 dprintf0("flagByte>3\n");
411 dprintf("car: %d\n", *carP);
412 pkByte(); /* ignore tfmwidth (3 bytes) */
413 get16(); /* ignore tfmwidth (3 bytes) */
414 get16(); /* ignore horiz escapement */
415 cwidth = get16(); /* bounding box width */
416 cheight = get16(); /* bounding box height */
417 dprintf("cwidth %d\n", cwidth);
418 dprintf("cheight %d\n", cheight);
419 xoffs = get16(); /* horiz offset */
420 if (xoffs >= 32768)
421 xoffs -= 65536;
422 yoffs = get16(); /* vert offset */
423 if (yoffs >= 32768)
424 yoffs -= 65536;
425 dprintf("xoffs %d\n", xoffs);
426 dprintf("yoffs %d\n", yoffs);
427 } else { /* short form preamble */
428 packetLength = (flagByte << 8) + pkByte();
429 *carP = pkByte(); /* char number */
430
431 *mustIgnoreP = false;
432
433 dprintf0("flagByte<=3\n");
434 dprintf("car: %d\n", *carP);
435 pkByte(); /* ignore tfmwidth (3 bytes) */
436 get16(); /* ignore tfmwidth (3 bytes) */
437 x = pkByte() ; /* ignore horiz escapement */
438 dprintf("horiz esc %d\n", x);
439 cwidth = pkByte(); /* bounding box width */
440 cheight = pkByte() ; /* bounding box height */
441 dprintf("cwidth %d\n", cwidth);
442 dprintf("cheight %d\n", cheight);
443 xoffs = pkByte(); /* horiz offset */
444 if (xoffs >= 128)
445 xoffs -=256;
446 yoffs = pkByte(); /* vert offset */
447 if (yoffs >= 128)
448 yoffs -= 256;
449 dprintf("xoffs %d\n", xoffs);
450 dprintf("yoffs %d\n", yoffs);
451 }
452 if (packetLength < 0)
453 pm_error("Invalid character header - negative packet length");
454 if (packetLength > UINT_MAX - pkLoc)
455 pm_error("Invalid character header - excessive packet length");
456
457 *endOfPacketP = packetLength + pkLoc;
458
459 *cheightP = cheight;
460 *cwidthP = cwidth;
461 *xoffsP = xoffs;
462 *yoffsP = yoffs;
463 *turnonP = turnon;
464 }
465
466
467
468 static void
469 readOneCharacter(bool const bmxOverrideSpec,
470 int const bmxOverride,
471 bool const bmyOverrideSpec,
472 int const bmyOverride,
473 unsigned int * const carP,
474 bool * const mustIgnoreP,
475 bit *** const bitmapP,
476 unsigned int * const bmxP,
477 unsigned int * const bmyP) {
478
479 int car;
480 unsigned int endOfPacket;
481 bool mustIgnore;
482 int cheight, cwidth;
483 int xoffs, yoffs;
484 bool turnon;
485 bit row[MAXROWWIDTH+1];
486
487 readCharacterHeader(&car, &endOfPacket, &mustIgnore,
488 &cheight, &cwidth, &xoffs, &yoffs, &turnon);
489
490 *carP = car;
491
492 if (mustIgnore || !fileName[car]) {
493 /* Ignore this character in the font */
494 ignoreChar(car, endOfPacket);
495 *mustIgnoreP = true;
496 } else {
497 bit ** bitmap;
498 unsigned int i;
499
500 int const bmx = bmxOverrideSpec ? bmxOverride : cwidth;
501 int const bmy = bmyOverrideSpec ? bmyOverride : cheight;
502
503 *mustIgnoreP = false;
504
505 bitmap = pbm_allocarray(bmx, bmy);
506
507 bitWeight = 0 ;
508 for (i = 0 ; i < bmy ; ++i) {
509 /* make it blank */
510 unsigned int j;
511
512 for (j = 0 ; j < bmx ; ++j)
513 bitmap[i][j] = PBM_WHITE;
514 }
515 if (dynf == 14) { /* bitmapped character */
516 dprintf("bmy: %d\n ", bmy);
517 dprintf("y: %d\n ", bmy - yoffs - 1);
518 for (i = 0 ; i < cheight ; ++i) {
519 unsigned int const yi = i + (bmy - yoffs - 1);
520 unsigned int j;
521 for (j = 0 ; j < cwidth ; ++j) {
522 unsigned int const xj = j - xoffs;
523 if (getBit() && 0 <= xj && xj < bmx && 0 <= yi && yi < bmy)
524 bitmap[yi][xj] = PBM_BLACK ;
525 }
526 }
527 } else { /* dynamically packed char */
528 int rowsleft = cheight ;
529 int hbit = cwidth ;
530 int rp = 0;
531 repeatCount = 0 ;
532 dprintf("bmy: %d\n ", bmy);
533 dprintf("y: %d\n", cheight-rowsleft+(bmy-2*yoffs-1));
534 while (rowsleft > 0) {
535 int count = pkPackedNum() ; /* get current color count */
536 while (count > 0) {
537 if (count < hbit) { /* doesn't extend past row */
538 hbit -= count ;
539 while (count--)
540 row[rp++] = turnon ? PBM_BLACK : PBM_WHITE;
541 } else { /* reaches end of row */
542 count -= hbit;
543 while (hbit--)
544 row[rp++] = turnon ? PBM_BLACK : PBM_WHITE;
545 for (i = 0; i <= repeatCount; i++) { /* fill row */
546 unsigned int const yi = i + cheight - rowsleft - 1;
547 if (0 <= yi && yi < bmy) {
548 unsigned int j;
549 for (j = 0; j < cwidth; j++) {
550 unsigned int const xj= j - xoffs;
551 if (0 <= xj && xj < bmx)
552 bitmap[yi][xj] = row[j] ;
553 }
554 }
555 }
556 rowsleft -= repeatCount + 1;
557 repeatCount = rp = 0;
558 hbit = cwidth;
559 }
560 }
561 turnon = !turnon;
562 }
563 if (rowsleft != 0 || hbit != cwidth)
564 pm_error("bad pk file (more bits than required)") ;
565 }
566 if (endOfPacket != pkLoc)
567 pm_error("bad pk file (bad packet length)") ;
568 *bitmapP = bitmap;
569 *bmxP = bmx;
570 *bmyP = bmy;
571 }
572 }
573
574
575
576 static void
577 generatePbmFile(const char * const fileNm,
578 bit ** const bitmap,
579 unsigned int const cols,
580 unsigned int const rows) {
581
582 FILE * ofP;
583
584 ofP = pm_openw(fileNm);
585
586 pbm_writepbm(ofP, bitmap, cols, rows, 0);
587
588 pm_close(ofP);
589 }
590
591
592
593 static void
594 warnMissingCodePoint() {
595
596 unsigned int car;
597
598 for (car = 0; car < MAXPKCHAR; ++car) {
599 if (fileName[car])
600 pm_message("Warning: No character in position %d (file %s).",
601 car, fileName[car]) ;
602 }
603 }
604
605
606
607 int
608 main(int argc, const char ** argv) {
609
610 struct CmdlineInfo cmdline;
611 unsigned int i;
612
613 pm_proginit(&argc, argv);
614
615 parseCommandLine(argc, argv, &cmdline);
616
617 debug = cmdline.debug;
618
619 for (i = 0; i < cmdline.character; ++i)
620 fileName[i] = NULL;
621 for (i = 0; i < cmdline.outputFileCt; ++i)
622 fileName[cmdline.character + i] = cmdline.outputFileNm[i];
623 for (i = cmdline.character + cmdline.outputFileCt;
624 i < MAXPKCHAR;
625 ++i)
626 fileName[i] = NULL;
627
628 ifP = pm_openr(cmdline.inputFileNm);
629
630 pkLoc = 0;
631
632 readHeader();
633
634 while (flagByte != 245) { /* not at postamble */
635
636 unsigned int car;
637 bool mustIgnore;
638 bit ** bitmap;
639 unsigned int cols, rows;
640
641 readOneCharacter(!!cmdline.xSpec, cmdline.x,
642 !!cmdline.ySpec, cmdline.y,
643 &car, &mustIgnore, &bitmap, &cols, &rows);
644
645 if (!mustIgnore) {
646 generatePbmFile(fileName[car], bitmap, cols, rows);
647
648 pbm_freearray(bitmap, rows) ;
649 }
650
651 fileName[car] = NULL;
652
653 skipSpecials();
654 }
655
656 while (!feof(ifP))
657 pkByte() ; /* skip trailing junk */
658
659 pm_close(ifP);
660
661 warnMissingCodePoint();
662
663 pm_message("%u bytes read from packed file.", pkLoc);
664
665 return 0;
666 }
667
668
669