home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume11 / pbmtochar / part01 / chartopbm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-03-25  |  2.8 KB  |  115 lines

  1. /*
  2.  * chartopbm.c - read text and produce a portable bitmap
  3.  * 
  4.  * Copyright (C) 1989 Diomidis D. Spinellis.
  5.  * 
  6.  * Permission to use, copy, modify, and distribute this software and its
  7.  * documentation for any purpose and without fee is hereby granted, provided
  8.  * that the above copyright notice appear in all copies and that both that
  9.  * copyright notice and this permission notice appear in supporting
  10.  * documentation.  This software is provided "as is" without express or
  11.  * implied warranty.
  12.  * 
  13.  * Comments and additions should be sent to the author:
  14.  *
  15.  *                     Diomidis D. Spinellis
  16.  *                     1 Myrsinis Str.
  17.  *                     GR-145 62 Kifissia
  18.  *                     GREECE
  19.  *               (dds@cc.ic.ac.uk)
  20.  *
  21.  * $Header: chartopbm.c,v 1.2 89/10/06 22:41:52 dds Rel $
  22.  * 
  23.  */
  24.  
  25. #include <stdio.h>
  26. #include <ctype.h>
  27. #ifdef SYSV
  28. #include <string.h>
  29. #else                /* SYSV */
  30. #include <strings.h>
  31. #define strchr index
  32. #endif                /* SYSV */
  33. #include "pbm.h"
  34. #include "8x13.h"
  35.  
  36. #ifndef lint
  37. static char     RCSid[] = "$Header: chartopbm.c,v 1.2 89/10/06 22:41:52 dds Rel $";
  38. #endif
  39.  
  40. #define LINELEN 1024
  41.  
  42.  
  43. main(argc, argv)
  44.     int             argc;
  45.     char           *argv[];
  46. {
  47.     struct linestruct {
  48.         char                 *line;
  49.         struct linestruct *next;
  50.     }               *lines, *lp, **plp;
  51.     FILE           *ifd;
  52.     char            line[LINELEN];
  53.     register        b;
  54.     register char  *p;
  55.     char           *p1;
  56.     register bit   *bp;
  57.     register    r, c;
  58.     int             rows = 0, cols = 0;
  59.     int             len;
  60.     char           *malloc();
  61.     bit           *bitrow;
  62.  
  63.     pm_progname = argv[0];
  64.  
  65.     if (argc > 2)
  66.         pm_usage("[textfile]");
  67.  
  68.     if (argc == 2)
  69.         ifd = pm_openr(argv[1]);
  70.     else
  71.         ifd = stdin;
  72.  
  73.  
  74.     /* Read the file into a linked list of lines */
  75.     plp = &lines;
  76.     while (fgets(line, LINELEN, ifd)) {
  77.         if (!(lp = (struct linestruct *)malloc(sizeof(struct linestruct))))
  78.             pm_error("out of memory", 0, 0, 0, 0, 0);
  79.         len = strlen(line);
  80.         if (!(lp->line = malloc(len + 1)))
  81.             pm_error("out of memory", 0, 0, 0, 0, 0);
  82.         (void)strcpy(lp->line, line);
  83.         *plp = lp;
  84.         plp = &(lp->next);
  85.         rows++;
  86.         if (len > cols)
  87.             cols = len;
  88.     }
  89.     lp->next = 0;
  90.  
  91.     pm_close(ifd);
  92.  
  93.     /* Write out the PBM. */
  94.     pbm_writepbminit(stdout, cols * FCOL, rows * FROW);
  95.     bitrow = pbm_allocrow(cols * FCOL);
  96.  
  97.     for (lp = lines; lp; lp = lp->next)
  98.         for (r = 0; r < FROW; r++) {
  99.             bp = bitrow;
  100.             /* Write the line */
  101.             for (p = lp->line; *p; p++)
  102.                 if (!(p1 = strchr(patternchars, *p)))
  103.                     for (c = 0; c < FCOL; c++)
  104.                         *bp++ = PBM_WHITE;
  105.                 else
  106.                     for (b = 0x80; b != (0x80 >> FCOL); b >>= 1)
  107.                         *bp++ = (font[p1 - patternchars][r] & b) ? PBM_BLACK : PBM_WHITE;
  108.             /* Zero pad the rest of the line */
  109.             for (c = 0; c < cols - strlen(lp->line) * FCOL; c++)
  110.                 *bp++ = PBM_WHITE;
  111.             pbm_writepbmrow(stdout, bitrow, cols * FCOL);
  112.         }
  113.     exit(0);
  114. }
  115.