home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / dkbtrace / pbmplus / source / pgm / pgmtofs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-06  |  3.5 KB  |  155 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. void
  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.     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.     {
  43.     ifp = stdin;
  44.     }
  45.  
  46.     if ( argn != argc )
  47.     pm_usage( usage );
  48.  
  49.     grays = pgm_readpgm( ifp, &cols, &rows, &maxval );
  50.     pm_close( ifp );
  51.  
  52.     /* Figure out bps. */
  53.     bps = pm_maxvaltobits( (int) maxval );
  54.     if ( bps > 2 && bps < 4 )
  55.     bps = 4;
  56.     else if ( bps > 4 && bps < 8 )
  57.     bps = 8;
  58.     else if ( bps > 8 )
  59.     pm_error(
  60.         "maxval of %d is too large for FaceSaver(tm)", maxval );
  61.     nmaxval = pm_bitstomaxval( bps );
  62.     
  63.     /* Compute padding to round cols * bps up to the nearest multiple of 8. */
  64.     padright = ( ( cols * bps + 7 ) / 8 ) * 8 - cols * bps;
  65.  
  66.     putinit( cols, rows, bps );
  67.     for ( row = rows - 1; row >= 0; --row )
  68.     {
  69.         for ( col = 0, gP = grays[row]; col < cols; ++col, ++gP )
  70.         {
  71.         if ( maxval != nmaxval )
  72.         *gP = (int) *gP * nmaxval / maxval;
  73.         putgray( *gP );
  74.         }
  75.     for ( col = 0; col < padright; ++col )
  76.         putgray( 0 );
  77.         }
  78.  
  79.     putrest( );
  80.  
  81.     pm_close (stdout);
  82.  
  83.     exit( 0 );
  84.     }
  85.  
  86.  
  87. static int bitspersample, item, bitsperitem, bitshift, itemsperline, items;
  88.  
  89. static void
  90. putinit( cols, rows, bps )
  91. int cols, rows, bps;
  92.     {
  93.     fprintf (stdout,  "FirstName: \n" );
  94.     fprintf (stdout,  "LastName: \n" );
  95.     fprintf (stdout,  "E-mail: \n" );
  96.     fprintf (stdout,  "Telephone: \n" );
  97.     fprintf (stdout,  "Company: \n" );
  98.     fprintf (stdout,  "Address1: \n" );
  99.     fprintf (stdout,  "Address2: \n" );
  100.     fprintf (stdout,  "CityStateZip: \n" );
  101.     fprintf (stdout,  "Date: \n" );
  102.     fprintf (stdout,  "PicData: %d %d %d\n", cols, rows, bps );
  103.     fprintf (stdout,  "Image: %d %d %d\n", cols, rows, bps );
  104.     fprintf (stdout,  "\n" );
  105.  
  106.     bitspersample = bps;
  107.     itemsperline = items = 0;
  108.     item = 0;
  109.     bitsperitem = 0;
  110.     bitshift = 8 - bitspersample;
  111.     }
  112.  
  113. static void
  114. putitem( )
  115.     {
  116.     char* hexits = "0123456789abcdef";
  117.  
  118.     if ( itemsperline == 30 )
  119.     {
  120.     putchar( '\n' );
  121.     itemsperline = 0;
  122.     }
  123.     putchar( hexits[item >> 4] );
  124.     putchar( hexits[item & 15] );
  125.     ++itemsperline;
  126.     ++items;
  127.     item = 0;
  128.     bitsperitem = 0;
  129.     bitshift = 8 - bitspersample;
  130.     }
  131.  
  132. #if __STDC__
  133. static void
  134. putgray( gray g )
  135. #else /*__STDC__*/
  136. static void
  137. putgray( g )
  138. gray g;
  139. #endif /*__STDC__*/
  140.     {
  141.     if ( bitsperitem == 8 )
  142.     putitem( );
  143.     item += g << bitshift;
  144.     bitsperitem += bitspersample;
  145.     bitshift -= bitspersample;
  146.     }
  147.  
  148. static void
  149. putrest( )
  150.     {
  151.     if ( bitsperitem > 0 )
  152.     putitem( );
  153.     fprintf (stdout,  "\n" );
  154.     }
  155.