home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / netpbma.zip / pgm / psidtopgm.c < prev    next >
C/C++ Source or Header  |  1993-10-04  |  3KB  |  137 lines

  1. /* psidtopgm.c - convert PostScript "image" data into a portable graymap
  2. **
  3. ** Copyright (C) 1989 by Jef Poskanzer.
  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 "pgm.h"
  14.  
  15. static int gethexit ARGS(( FILE* ifp ));
  16.  
  17.  
  18. int
  19. main( argc, argv )
  20. int argc;
  21. char* argv[];
  22.     {
  23.     FILE* ifp;
  24.     gray* grayrow;
  25.     register gray* gP;
  26.     int argn, row;
  27.     register int col, val;
  28.     int maxval;
  29.     int rows, cols, bitspersample;
  30.     char* usage = "<width> <height> <bits/sample> [imagedata]";
  31.  
  32.  
  33.     pgm_init( &argc, argv );
  34.  
  35.     argn = 1;
  36.  
  37.     if ( argn + 3 > argc )
  38.     pm_usage( usage );
  39.  
  40.     cols = atoi( argv[argn++] );
  41.     rows = atoi( argv[argn++] );
  42.     bitspersample = atoi( argv[argn++] );
  43.     if ( cols <= 0 || rows <= 0 || bitspersample <= 0 )
  44.     pm_usage( usage );
  45.  
  46.     if ( argn < argc )
  47.     {
  48.     ifp = pm_openr( argv[argn] );
  49.     ++argn;
  50.     }
  51.     else
  52.     ifp = stdin;
  53.  
  54.     if ( argn != argc )
  55.     pm_usage( usage );
  56.  
  57.     maxval = pm_bitstomaxval( bitspersample );
  58.     if ( maxval > PGM_MAXMAXVAL )
  59.     pm_error(
  60.         "bits/sample is too large - try reconfiguring with PGM_BIGGRAYS" );
  61.  
  62.     pgm_writepgminit( stdout, cols, rows, (gray) maxval, 0 );
  63.     grayrow = pgm_allocrow( ( cols + 7 ) / 8 * 8 );
  64.     for ( row = 0; row < rows; ++row)
  65.     {
  66.     for ( col = 0, gP = grayrow; col < cols; )
  67.         {
  68.         val = gethexit( ifp ) << 4;
  69.         val += gethexit( ifp );
  70.         switch ( bitspersample )
  71.         {
  72.         case 1:
  73.         *gP++ = val >> 7;
  74.         *gP++ = ( val >> 6 ) & 0x1;
  75.         *gP++ = ( val >> 5 ) & 0x1;
  76.         *gP++ = ( val >> 4 ) & 0x1;
  77.         *gP++ = ( val >> 3 ) & 0x1;
  78.         *gP++ = ( val >> 2 ) & 0x1;
  79.         *gP++ = ( val >> 1 ) & 0x1;
  80.         *gP++ = val & 0x1;
  81.         col += 8;
  82.         break;
  83.  
  84.         case 2:
  85.         *gP++ = val >> 6;
  86.         *gP++ = ( val >> 4 ) & 0x3;
  87.         *gP++ = ( val >> 2 ) & 0x3;
  88.         *gP++ = val & 0x3;
  89.         col += 4;
  90.         break;
  91.  
  92.         case 4:
  93.         *gP++ = val >> 4;
  94.         *gP++ = val & 0xf;
  95.         col += 2;
  96.         break;
  97.  
  98.         case 8:
  99.         *gP++ = val;
  100.         ++col;
  101.         break;
  102.  
  103.         default:
  104.         pm_error( "bitspersample of %d not supported", bitspersample );
  105.         }
  106.         }
  107.     pgm_writepgmrow( stdout, grayrow, cols, (gray) maxval, 0 );
  108.     }
  109.     pm_close( ifp );
  110.     pm_close( stdout );
  111.  
  112.     exit( 0 );
  113.     }
  114.  
  115. static int
  116. gethexit( ifp )
  117. FILE* ifp;
  118.     {
  119.     register int i;
  120.     register char c;
  121.  
  122.     for ( ; ; )
  123.     {
  124.     i = getc( ifp );
  125.     if ( i == EOF )
  126.         pm_error( "EOF / read error" );
  127.     c = (char) i;
  128.     if ( c >= '0' && c <= '9' )
  129.         return c - '0';
  130.     else if ( c >= 'A' && c <= 'F' )
  131.         return c - 'A' + 10;
  132.     else if ( c >= 'a' && c <= 'f' )
  133.         return c - 'a' + 10;
  134.     /* Else ignore - whitespace. */
  135.     }
  136.     }
  137.