home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 309.lha / PBM_PLUS / pgm / fstopgm.c < prev    next >
C/C++ Source or Header  |  1980-12-04  |  3KB  |  137 lines

  1. /* fstopgm.c - read a Usenix FaceSaver 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 <stdio.h>
  14. #ifdef    SYSV
  15. #include <string.h>
  16. #else    SYSV
  17. #include <strings.h>
  18. #endif    SYSV
  19. #include "pgm.h"
  20.  
  21. main( argc, argv )
  22. int argc;
  23. char *argv[];
  24.     {
  25.     FILE *ifd;
  26.     register gray **grays, *gP;
  27.     int argn, row;
  28.     register int col;
  29.     gray maxval;
  30.     int rows = 0, cols = 0, depth = 0, xrows = 0, xcols = 0, xdepth = 0;
  31. #define STRSIZE 1000
  32.     char buf[STRSIZE], firstname[STRSIZE], lastname[STRSIZE], email[STRSIZE];
  33.     int gethexit();
  34.  
  35.     pm_progname = argv[0];
  36.  
  37.     argn = 1;
  38.  
  39.     if ( argn < argc )
  40.     {
  41.     ifd = pm_openr( argv[argn] );
  42.     argn++;
  43.     }
  44.     else
  45.     ifd = stdin;
  46.  
  47.     if ( argn != argc )
  48.     pm_usage( "[fsfile]" );
  49.  
  50.     /* Read the FaceSaver header. */
  51.     for ( ; ; )
  52.     {
  53.     if ( fgets( buf, STRSIZE, ifd ) == (char *) 0 )
  54.         pm_error( "error reading header", 0,0,0,0,0 );
  55.  
  56.     /* Blank line ends header. */
  57.     if ( strlen( buf ) == 1 )
  58.         break;
  59.  
  60.     if ( sscanf( buf, "FirstName: %s\n", firstname ) == 1 )
  61.         ;
  62.     else if ( sscanf( buf, "LastName: %s\n", lastname ) == 1 )
  63.         ;
  64.     else if ( sscanf( buf, "E-mail: %s\n", email ) == 1 )
  65.         ;
  66.     else if ( sscanf( buf, "PicData: %d %d %d\n",
  67.               &cols, &rows, &depth ) == 3 )
  68.         {
  69.         if ( depth != 8 )
  70.         pm_error(
  71.             "can't handle 'PicData' depth other than 8", 0,0,0,0,0 );
  72.         }
  73.     else if ( sscanf( buf, "Image: %d %d %d\n",
  74.               &xcols, &xrows, &xdepth ) == 3 )
  75.         {
  76.         if ( xdepth != 8 )
  77.         pm_error(
  78.             "can't handle 'Image' depth other than 8", 0,0,0,0,0 );
  79.         }
  80.     }
  81.     if ( cols <= 0 || rows <= 0 )
  82.     pm_error( "invalid header", 0,0,0,0,0 );
  83.     maxval = 255;
  84.     if ( xcols != 0 && xrows != 0 && ( xcols != cols || xrows != rows ) )
  85.     {
  86.     float rowratio, colratio;
  87.  
  88.     rowratio = (float) xrows / (float) rows;
  89.     colratio = (float) xcols / (float) cols;
  90.     fprintf(
  91.         stderr,
  92.         "(Warning: non-square pixels; to fix do a 'ppmscale -%cscale %g | ppmtopgm'.)\n",
  93.         rowratio > colratio ? 'y' : 'x',
  94.         rowratio > colratio ? rowratio / colratio : colratio / rowratio );
  95.     }
  96.  
  97.     /* Now read the hex bits. */
  98.     grays = pgm_allocarray( cols, rows );
  99.     for ( row = rows - 1; row >= 0; row--)
  100.     {
  101.     for ( col = 0, gP = grays[row]; col < cols; col++, gP++ )
  102.         {
  103.         *gP = gethexit( ifd ) << 4;
  104.         *gP += gethexit( ifd );
  105.         }
  106.     }
  107.     pm_close( ifd );
  108.  
  109.     /* And write out the graymap. */
  110.     pgm_writepgm( stdout, grays, cols, rows, maxval );
  111.  
  112.     exit( 0 );
  113.     }
  114.  
  115. int
  116. gethexit( ifd )
  117. FILE *ifd;
  118.     {
  119.     register int i;
  120.     register char c;
  121.  
  122.     for ( ; ; )
  123.     {
  124.     i = getc( ifd );
  125.     if ( i == EOF )
  126.         pm_error( "premature EOF", 0,0,0,0,0 );
  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.