home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3430 / lptopgm.c < prev   
Encoding:
C/C++ Source or Header  |  1991-05-28  |  3.3 KB  |  166 lines

  1. /* lptppgm.c - convert line printer picture into a portable graymap
  2. **
  3. ** Copyright (C) 1990 by Ken Yap.
  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. #include <stdio.h>
  14. #include "pgm.h"
  15. #ifdef SYSV
  16. #include <string.h>
  17. #else /*SYSV*/
  18. #include <strings.h>
  19. #endif /*SYSV*/
  20.  
  21. #define    TEMPLATE    "/tmp/lpXXXXXX"
  22.  
  23. char    grayvals[128] = {
  24.      0,  0,  0,  0,  0,  0,  0,  0,
  25.      0,  0,  0,  0,  0,  0,  0,  0,
  26.      0,  0,  0,  0,  0,  0,  0,  0,
  27.      0,  0,  0,  0,  0,  0,  0,  0,
  28.      0,  6,  6, 22, 19, 17, 15,  3,
  29.     11, 11, 11,  9,  6,  5,  4, 10,
  30.     16, 11, 15, 14, 18, 15, 15, 12,
  31.     16, 15,  8, 10,  7,  8,  7,  9,
  32.     19, 19, 21, 15, 20, 20, 17, 18,
  33.     24, 16, 14, 20, 15, 24, 25, 20,
  34.     18, 23, 20, 17, 16, 19, 17, 21,
  35.     18, 17, 18, 15, 10, 15,  5,  5,
  36.      3, 13, 17, 10, 17, 13, 15, 18,
  37.     19, 13, 12, 16, 14, 18, 16, 12,
  38.     18, 18, 13, 12, 12, 15, 12, 17,
  39.     14, 17, 14, 13, 10, 13,  7,  0
  40. };
  41.  
  42. FILE *prepass(ifd, cols, rows)
  43. FILE *ifd;
  44. int *cols, *rows;
  45. {
  46.     register int c, col;
  47.     register FILE *tfd;
  48.     char tempname[sizeof(TEMPLATE)];
  49.     char *mktemp();
  50.  
  51.     (void)strcpy(tempname, TEMPLATE);
  52.     (void)mktemp(tempname);
  53.     if ((tfd = fopen(tempname, "w+")) == NULL)
  54.         return (NULL);
  55.     (void)unlink(tempname);
  56.     *rows = 0;
  57.     *cols = 0;
  58.     col = 0;
  59.     while ((c = getc(ifd)) != EOF)
  60.     {
  61.         putc(c, tfd);
  62.         c &= 0xff;
  63.         switch (c)
  64.         {
  65.         case '\b':
  66.             if (cols > 0)
  67.                 --cols;
  68.             break;
  69.         case '\n':
  70.             ++*rows;
  71.             if (col > *cols)
  72.                 *cols = col;
  73.             col = 0;
  74.             break;
  75.         case '\r':
  76.             if (col > *cols)
  77.                 *cols = col;
  78.             col = 0;
  79.             break;
  80.         default:
  81.             if (c >= ' ' && c <= '~')
  82.                 ++col;
  83.             break;
  84.         }
  85.     }
  86.     (void)fclose(ifd);
  87.     (void)fseek(tfd, 0L, 0);
  88.     return (tfd);
  89. }
  90.  
  91. postpass(ifd, cols, rows)
  92. FILE *ifd;
  93. int cols, rows;
  94. {
  95.     register int c, maxval, i;
  96.     register gray *grayrow, *gP;
  97.  
  98.     maxval = 255;
  99.     if (maxval > PGM_MAXMAXVAL)
  100.         pm_error("maxval of 255 is too large - try recompiling with a larger gray type");
  101.     pgm_writepgminit(stdout, cols, rows, (gray)maxval);
  102.     gP = grayrow = pgm_allocrow(cols);
  103.     /* clear out row */
  104.     for (i = 0, gP = grayrow; i < cols; ++i)
  105.         *gP++ = (gray)0;
  106.     while ((c = getc(ifd)) != EOF)
  107.     {
  108.         c &= 0xff;
  109.         switch (c)
  110.         {
  111.         case '\b':
  112.             if (gP != grayrow)
  113.                 --gP;
  114.             break;
  115.         case '\n':
  116.             pgm_writepgmrow(stdout, grayrow, cols, (gray)maxval);
  117.             /* clear out row */
  118.             for (i = 0, gP = grayrow; i < cols; ++i)
  119.                 *gP++ = (gray)0;
  120.             gP = grayrow;
  121.             break;
  122.         case '\r':
  123.             gP = grayrow;
  124.             break;
  125.         default:
  126.             if (c >= ' ' && c <= '~')
  127.             {
  128.                 *gP += grayvals[c];
  129.                 if (*gP > maxval)
  130.                     *gP = maxval;
  131.                 ++gP;
  132.             }
  133.             break;
  134.         }
  135.     }
  136.     pm_close(ifd);
  137. }
  138.  
  139. main(argc, argv)
  140. int argc;
  141. char *argv[];
  142. {
  143.     register FILE *ifd;
  144.     register int argn;
  145.     int cols, rows;
  146.     char *usage = "[lppic]";
  147.  
  148.     pm_progname = argv[0];
  149.     argn = 1;
  150.     if (argn < argc)
  151.     {
  152.         ifd = pm_openr(argv[argn]);
  153.         argn++;
  154.     }
  155.     else
  156.         ifd = stdin;
  157.     if (argn != argc)
  158.         pm_usage(usage);
  159.  
  160.     if ((ifd = prepass(ifd, &cols, &rows)) == NULL)
  161.         pm_error("problems reading line printer picture");
  162.     (void)fprintf(stderr, "%d columns, %d rows\n", cols, rows);
  163.     postpass(ifd, cols, rows);
  164.     exit(0);
  165. }
  166.