home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / netpbma.zip / pgm / asciitopgm.c next >
C/C++ Source or Header  |  1994-01-27  |  4KB  |  150 lines

  1. /* asciitopgm.c - read an ASCII graphics file and produce a portable graymap
  2. **
  3. ** Copyright (C) 1989 by Wilson H. Bent, Jr
  4. **
  5. ** - Based on fstopgm.c and other works which bear the following notice:
  6. ** Copyright (C) 1989 by Jef Poskanzer.
  7. **
  8. ** Permission to use, copy, modify, and distribute this software and its
  9. ** documentation for any purpose and without fee is hereby granted, provided
  10. ** that the above copyright notice appear in all copies and that both that
  11. ** copyright notice and this permission notice appear in supporting
  12. ** documentation.  This software is provided "as is" without express or
  13. ** implied warranty.
  14. */
  15.  
  16. #include "pgm.h"
  17.  
  18. static char gmap [128] = {
  19. /*00 nul-bel*/  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  20. /*08 bs -si */  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  21. /*10 dle-etb*/  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  22. /*18 can-us */  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  23. /*20 sp - ' */  0x00, 0x21, 0x1b, 0x7f, 0x70, 0x25, 0x20, 0x0a,
  24. /*28  ( - / */  0x11, 0x11, 0x2a, 0x2b, 0x0b, 0x13, 0x04, 0x10,
  25. /*30  0 - 7 */  0x30, 0x28, 0x32, 0x68, 0x39, 0x35, 0x39, 0x16,
  26. /*38  8 - ? */  0x38, 0x39, 0x14, 0x15, 0x11, 0x1c, 0x11, 0x3f,
  27. /*40  @ - G */  0x40, 0x49, 0x52, 0x18, 0x44, 0x3c, 0x38, 0x38,
  28. /*48  H - O */  0x55, 0x28, 0x2a, 0x70, 0x16, 0x7f, 0x70, 0x14,
  29. /*50  P - W */  0x60, 0x20, 0x62, 0x53, 0x1a, 0x55, 0x36, 0x57,
  30. /*58  X - _ */  0x50, 0x4c, 0x5a, 0x24, 0x10, 0x24, 0x5e, 0x13,
  31. /*60  ` - g */  0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
  32. /*68  h - o */  0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x2a,
  33. /*70  p - w */  0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
  34. /*78  x -del*/  0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
  35. };
  36.  
  37. static gray maxval = 127;
  38.  
  39. int
  40. main( argc, argv )
  41. int argc;
  42. char *argv[];
  43.     {
  44.     FILE *ifd;
  45.     register gray **grays, *gP;
  46.     int argn, row;
  47.     register int col;
  48.     register int c, i;
  49.     int rows = 0, cols = 0;
  50.     int divisor = 1;
  51.     int warned = 0;
  52.     int *obuf;
  53.     char *usage = "[-d <val>] height width [asciifile]";
  54.  
  55.     pgm_init( &argc, argv );
  56.  
  57.     argn = 1;
  58.  
  59.     if ( argc < 3 || argc > 6 )
  60.     pm_usage( usage );
  61.  
  62.     if ( argv[argn][0] == '-' )
  63.     {
  64.     if ( strcmp( argv[argn], "-d" ) == 0 )
  65.         {
  66.         if ( argc == argn + 1 )
  67.         pm_usage( usage );
  68.         if ( sscanf( argv[argn+1], "%d", &divisor ) != 1 )
  69.         pm_usage( usage );
  70.         argn += 2;
  71.         }
  72.     else
  73.         pm_usage( usage );
  74.     }
  75.  
  76.     if ( sscanf( argv[argn++], "%d", &rows ) != 1 )
  77.     pm_usage( usage );
  78.     if ( sscanf( argv[argn++], "%d", &cols ) != 1 )
  79.     pm_usage( usage );
  80.     if ( rows < 1 )
  81.     pm_error( "height is less than 1", 0,0,0,0,0 );
  82.     if ( cols < 1 )
  83.     pm_error( "width is less than 1", 0,0,0,0,0 );
  84.  
  85.     if ( argc > argn + 1 )
  86.     pm_usage( usage );
  87.  
  88.     if ( argc == argn + 1 )
  89.     ifd = pm_openr( argv[argn] );
  90.     else
  91.     ifd = stdin;
  92.  
  93.     /* Here's where the work is done:
  94.      * - Usually, the graymap value of the input char is summed into grays
  95.      * - For a 'normal' newline, the current row is adjusted by the divisor
  96.      *   and the current row is incremented
  97.      * - If the first char in the input line is a '+', then the current row
  98.      *   stays the same to allow 'overstriking'
  99.      * NOTE that we assume the user specified a sufficiently large width!
  100.      */
  101.     if ((obuf = (int *) calloc (cols, sizeof (int))) == NULL)
  102.     pm_error( "malloc error", 0,0,0,0,0 );        /* wimpy message! */
  103.     grays = pgm_allocarray( cols, rows );
  104.     row = i = 0;
  105.     while ( row < rows )
  106.     {
  107.         switch (c = getc (ifd))
  108.           {
  109.           case EOF:
  110.         goto line_done;
  111.           case '\n':
  112.         if ((c = getc (ifd)) == EOF)
  113.             goto line_done;
  114.         if (c == '+')
  115.             i = 0;
  116.         else
  117.             {
  118.         line_done:
  119.             for (i = 0; i < cols; ++i)
  120.             grays[row][i] = maxval - (obuf[i] / divisor);
  121.             bzero (obuf, cols * sizeof (int));
  122.             i = 0;
  123.             ++row;
  124.             if ( row >= rows )
  125.             break;
  126.             if (c != EOF)
  127.             obuf[i++] += gmap[c];
  128.             }
  129.         break;
  130.           default:
  131.         if (c > 0x7f)        /* !isascii(c) */
  132.             {
  133.             if (!warned)
  134.             {
  135.             fprintf (stderr, "(Warning: non-ASCII char(s) in input)\n");
  136.             warned = 1;
  137.             }
  138.             c &= 0x7f;        /* toascii(c) */
  139.             }
  140.         obuf[i++] += gmap[c];
  141.         break;
  142.           }
  143.     }
  144.     pm_close( ifd );
  145.  
  146.     pgm_writepgm( stdout, grays, cols, rows, maxval, 0 );
  147.  
  148.     exit( 0 );
  149.     }
  150.