home *** CD-ROM | disk | FTP | other *** search
- /*
- * chartopbm.c - read text and produce a portable bitmap
- *
- * Copyright (C) 1989 Diomidis D. Spinellis.
- *
- * Permission to use, copy, modify, and distribute this software and its
- * documentation for any purpose and without fee is hereby granted, provided
- * that the above copyright notice appear in all copies and that both that
- * copyright notice and this permission notice appear in supporting
- * documentation. This software is provided "as is" without express or
- * implied warranty.
- *
- * Comments and additions should be sent to the author:
- *
- * Diomidis D. Spinellis
- * 1 Myrsinis Str.
- * GR-145 62 Kifissia
- * GREECE
- * (dds@cc.ic.ac.uk)
- *
- * $Header: chartopbm.c,v 1.2 89/10/06 22:41:52 dds Rel $
- *
- */
-
- #include <stdio.h>
- #include <ctype.h>
- #ifdef SYSV
- #include <string.h>
- #else /* SYSV */
- #include <strings.h>
- #define strchr index
- #endif /* SYSV */
- #include "pbm.h"
- #include "8x13.h"
-
- #ifndef lint
- static char RCSid[] = "$Header: chartopbm.c,v 1.2 89/10/06 22:41:52 dds Rel $";
- #endif
-
- #define LINELEN 1024
-
-
- main(argc, argv)
- int argc;
- char *argv[];
- {
- struct linestruct {
- char *line;
- struct linestruct *next;
- } *lines, *lp, **plp;
- FILE *ifd;
- char line[LINELEN];
- register b;
- register char *p;
- char *p1;
- register bit *bp;
- register r, c;
- int rows = 0, cols = 0;
- int len;
- char *malloc();
- bit *bitrow;
-
- pm_progname = argv[0];
-
- if (argc > 2)
- pm_usage("[textfile]");
-
- if (argc == 2)
- ifd = pm_openr(argv[1]);
- else
- ifd = stdin;
-
-
- /* Read the file into a linked list of lines */
- plp = &lines;
- while (fgets(line, LINELEN, ifd)) {
- if (!(lp = (struct linestruct *)malloc(sizeof(struct linestruct))))
- pm_error("out of memory", 0, 0, 0, 0, 0);
- len = strlen(line);
- if (!(lp->line = malloc(len + 1)))
- pm_error("out of memory", 0, 0, 0, 0, 0);
- (void)strcpy(lp->line, line);
- *plp = lp;
- plp = &(lp->next);
- rows++;
- if (len > cols)
- cols = len;
- }
- lp->next = 0;
-
- pm_close(ifd);
-
- /* Write out the PBM. */
- pbm_writepbminit(stdout, cols * FCOL, rows * FROW);
- bitrow = pbm_allocrow(cols * FCOL);
-
- for (lp = lines; lp; lp = lp->next)
- for (r = 0; r < FROW; r++) {
- bp = bitrow;
- /* Write the line */
- for (p = lp->line; *p; p++)
- if (!(p1 = strchr(patternchars, *p)))
- for (c = 0; c < FCOL; c++)
- *bp++ = PBM_WHITE;
- else
- for (b = 0x80; b != (0x80 >> FCOL); b >>= 1)
- *bp++ = (font[p1 - patternchars][r] & b) ? PBM_BLACK : PBM_WHITE;
- /* Zero pad the rest of the line */
- for (c = 0; c < cols - strlen(lp->line) * FCOL; c++)
- *bp++ = PBM_WHITE;
- pbm_writepbmrow(stdout, bitrow, cols * FCOL);
- }
- exit(0);
- }
-