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

  1. /* fstopgm.c - read a Usenix FaceSaver(tm) file and produce 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. int
  18. main( argc, argv )
  19. int argc;
  20. char *argv[];
  21.     {
  22.     FILE *ifp;
  23.     register gray **grays, *gP;
  24.     int argn, row;
  25.     register int col;
  26.     int maxval;
  27.     int rows = 0, cols = 0, depth = 0, xrows = 0, xcols = 0, xdepth = 0;
  28. #define STRSIZE 1000
  29.     char buf[STRSIZE], firstname[STRSIZE], lastname[STRSIZE], email[STRSIZE];
  30.  
  31.  
  32.     pgm_init( &argc, argv );
  33.  
  34.     argn = 1;
  35.  
  36.     if ( argn < argc )
  37.     {
  38.     ifp = pm_openr( argv[argn] );
  39.     argn++;
  40.     }
  41.     else
  42.     ifp = stdin;
  43.  
  44.     if ( argn != argc )
  45.     pm_usage( "[fsfile]" );
  46.  
  47.     /* Read the FaceSaver(tm) header. */
  48.     for ( ; ; )
  49.     {
  50.     if ( fgets( buf, STRSIZE, ifp ) == (char *) 0 )
  51.         pm_error( "error reading header" );
  52.  
  53.     /* Blank line ends header. */
  54.     if ( strlen( buf ) == 1 )
  55.         break;
  56.  
  57.     if ( sscanf( buf, "FirstName: %[^\n]", firstname ) == 1 )
  58.         ;
  59.     else if ( sscanf( buf, "LastName: %[^\n]", lastname ) == 1 )
  60.         ;
  61.     else if ( sscanf( buf, "E-mail: %[^\n]", email ) == 1 )
  62.         ;
  63.     else if ( sscanf( buf, "PicData: %d %d %d\n",
  64.               &cols, &rows, &depth ) == 3 )
  65.         {
  66.         if ( depth != 8 )
  67.         pm_error(
  68.             "can't handle 'PicData' depth other than 8" );
  69.         }
  70.     else if ( sscanf( buf, "Image: %d %d %d\n",
  71.               &xcols, &xrows, &xdepth ) == 3 )
  72.         {
  73.         if ( xdepth != 8 )
  74.         pm_error(
  75.             "can't handle 'Image' depth other than 8" );
  76.         }
  77.     }
  78.     if ( cols <= 0 || rows <= 0 )
  79.     pm_error( "invalid header" );
  80.     maxval = pm_bitstomaxval( depth );
  81.     if ( maxval > PGM_MAXMAXVAL )
  82.     pm_error( "depth is too large - try reconfiguring with PGM_BIGGRAYS" );
  83.     if ( xcols != 0 && xrows != 0 && ( xcols != cols || xrows != rows ) )
  84.     {
  85.     float rowratio, colratio;
  86.  
  87.     rowratio = (float) xrows / (float) rows;
  88.     colratio = (float) xcols / (float) cols;
  89.     pm_message(
  90.         "warning, non-square pixels; to fix do a 'pnmscale -%cscale %g'",
  91.         rowratio > colratio ? 'y' : 'x',
  92.         rowratio > colratio ? rowratio / colratio : colratio / rowratio );
  93.     }
  94.  
  95.     /* Now read the hex bits. */
  96.     grays = pgm_allocarray( cols, rows );
  97.     for ( row = rows - 1; row >= 0; row--)
  98.     {
  99.     for ( col = 0, gP = grays[row]; col < cols; col++, gP++ )
  100.         {
  101.         *gP = gethexit( ifp ) << 4;
  102.         *gP += gethexit( ifp );
  103.         }
  104.     }
  105.     pm_close( ifp );
  106.  
  107.     /* And write out the graymap. */
  108.     pgm_writepgm( stdout, grays, cols, rows, (gray) maxval, 0 );
  109.     pm_close( stdout );
  110.  
  111.     exit( 0 );
  112.     }
  113.  
  114. static int
  115. gethexit( ifp )
  116. FILE *ifp;
  117.     {
  118.     register int i;
  119.     register char c;
  120.  
  121.     for ( ; ; )
  122.     {
  123.     i = getc( ifp );
  124.     if ( i == EOF )
  125.         pm_error( "EOF / read error" );
  126.     c = (char) i;
  127.     if ( c >= '0' && c <= '9' )
  128.         return c - '0';
  129.     else if ( c >= 'A' && c <= 'F' )
  130.         return c - 'A' + 10;
  131.     else if ( c >= 'a' && c <= 'f' )
  132.         return c - 'a' + 10;
  133.     /* Else ignore - whitespace. */
  134.     }
  135.     }
  136.