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

  1. /* pgmtofs.c - convert portable graymap to Usenix FaceSaver(tm) format
  2. **
  3. ** Copyright (C) 1991 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 void putinit ARGS(( int cols, int rows, int bps ));
  16. static void putitem ARGS(( void ));
  17. static void putgray ARGS(( gray g ));
  18. static void putrest ARGS(( void ));
  19.  
  20. int
  21. main( argc, argv )
  22. int argc;
  23. char* argv[];
  24.     {
  25.     FILE* ifp;
  26.     gray** grays;
  27.     register gray* gP;
  28.     int argn, rows, cols, bps, padright, row, col;
  29.     gray maxval, nmaxval;
  30.     char* usage = "[pgmfile]";
  31.  
  32.  
  33.     pgm_init( &argc, argv );
  34.  
  35.     argn = 1;
  36.  
  37.     if ( argn < argc )
  38.     {
  39.     ifp = pm_openr( argv[argn] );
  40.     ++argn;
  41.     }
  42.     else
  43.     {
  44.     ifp = stdin;
  45.     }
  46.  
  47.     if ( argn != argc )
  48.     pm_usage( usage );
  49.  
  50.     grays = pgm_readpgm( ifp, &cols, &rows, &maxval );
  51.     pm_close( ifp );
  52.  
  53.     /* Figure out bps. */
  54.     bps = pm_maxvaltobits( (int) maxval );
  55.     if ( bps > 2 && bps < 4 )
  56.     bps = 4;
  57.     else if ( bps > 4 && bps < 8 )
  58.     bps = 8;
  59.     else if ( bps > 8 )
  60.     pm_error(
  61.         "maxval of %d is too large for FaceSaver(tm)", maxval );
  62.     nmaxval = pm_bitstomaxval( bps );
  63.     
  64.     /* Compute padding to round cols * bps up to the nearest multiple of 8. */
  65.     padright = ( ( cols * bps + 7 ) / 8 ) * 8 - cols * bps;
  66.  
  67.     putinit( cols, rows, bps );
  68.     for ( row = rows - 1; row >= 0; --row )
  69.     {
  70.         for ( col = 0, gP = grays[row]; col < cols; ++col, ++gP )
  71.         {
  72.         if ( maxval != nmaxval )
  73.         *gP = (int) *gP * nmaxval / maxval;
  74.         putgray( *gP );
  75.         }
  76.     for ( col = 0; col < padright; ++col )
  77.         putgray( 0 );
  78.         }
  79.  
  80.     putrest( );
  81.  
  82.     exit( 0 );
  83.     }
  84.  
  85.  
  86. static int bitspersample, item, bitsperitem, bitshift, itemsperline, items;
  87.  
  88. static void
  89. putinit( cols, rows, bps )
  90. int cols, rows, bps;
  91.     {
  92.     printf( "FirstName: \n" );
  93.     printf( "LastName: \n" );
  94.     printf( "E-mail: \n" );
  95.     printf( "Telephone: \n" );
  96.     printf( "Company: \n" );
  97.     printf( "Address1: \n" );
  98.     printf( "Address2: \n" );
  99.     printf( "CityStateZip: \n" );
  100.     printf( "Date: \n" );
  101.     printf( "PicData: %d %d %d\n", cols, rows, bps );
  102.     printf( "Image: %d %d %d\n", cols, rows, bps );
  103.     printf( "\n" );
  104.  
  105.     bitspersample = bps;
  106.     itemsperline = items = 0;
  107.     item = 0;
  108.     bitsperitem = 0;
  109.     bitshift = 8 - bitspersample;
  110.     }
  111.  
  112. static void
  113. putitem( )
  114.     {
  115.     char* hexits = "0123456789abcdef";
  116.  
  117.     if ( itemsperline == 30 )
  118.     {
  119.     putchar( '\n' );
  120.     itemsperline = 0;
  121.     }
  122.     putchar( hexits[item >> 4] );
  123.     putchar( hexits[item & 15] );
  124.     ++itemsperline;
  125.     ++items;
  126.     item = 0;
  127.     bitsperitem = 0;
  128.     bitshift = 8 - bitspersample;
  129.     }
  130.  
  131. #if __STDC__
  132. static void
  133. putgray( gray g )
  134. #else /*__STDC__*/
  135. static void
  136. putgray( g )
  137. gray g;
  138. #endif /*__STDC__*/
  139.     {
  140.     if ( bitsperitem == 8 )
  141.     putitem( );
  142.     item += g << bitshift;
  143.     bitsperitem += bitspersample;
  144.     bitshift -= bitspersample;
  145.     }
  146.  
  147. static void
  148. putrest( )
  149.     {
  150.     if ( bitsperitem > 0 )
  151.     putitem( );
  152.     printf( "\n" );
  153.     }
  154.