home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 309.lha / PBM_PLUS / pgm / psidtopgm.c < prev   
C/C++ Source or Header  |  1980-12-04  |  3KB  |  135 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 <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 *grayrow, *gP;
  27.     int argn, row;
  28.     register int col, val;
  29.     gray maxval;
  30.     int rows, cols, bitspersample;
  31.     char *usage = "<width> <height> <bits/sample> [imagedata]";
  32.  
  33.     pm_progname = argv[0];
  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.     ifd = pm_openr( argv[argn] );
  49.     argn++;
  50.     }
  51.     else
  52.     ifd = stdin;
  53.  
  54.     if ( argn != argc )
  55.     pm_usage( usage );
  56.  
  57.     maxval = ( 1 << bitspersample ) - 1;
  58.  
  59.     pgm_writepgminit( stdout, cols, rows, maxval );
  60.     grayrow = pgm_allocrow( ( cols + 7 ) / 8 * 8 );
  61.     for ( row = 0; row < rows; row++)
  62.     {
  63.     for ( col = 0, gP = grayrow; col < cols; )
  64.         {
  65.         val = gethexit( ifd ) << 4;
  66.         val += gethexit( ifd );
  67.         switch ( bitspersample )
  68.         {
  69.         case 1:
  70.         *gP++ = val >> 7;
  71.         *gP++ = ( val >> 6 ) & 0x1;
  72.         *gP++ = ( val >> 5 ) & 0x1;
  73.         *gP++ = ( val >> 4 ) & 0x1;
  74.         *gP++ = ( val >> 3 ) & 0x1;
  75.         *gP++ = ( val >> 2 ) & 0x1;
  76.         *gP++ = ( val >> 1 ) & 0x1;
  77.         *gP++ = val & 0x1;
  78.         col += 8;
  79.         break;
  80.  
  81.         case 2:
  82.         *gP++ = val >> 6;
  83.         *gP++ = ( val >> 4 ) & 0x3;
  84.         *gP++ = ( val >> 2 ) & 0x3;
  85.         *gP++ = val & 0x3;
  86.         col += 4;
  87.         break;
  88.  
  89.         case 4:
  90.         *gP++ = val >> 4;
  91.         *gP++ = val & 0xf;
  92.         col += 2;
  93.         break;
  94.  
  95.         case 8:
  96.         *gP++ = val;
  97.         col++;
  98.         break;
  99.  
  100.         default:
  101.         pm_error(
  102.             "bitspersample of %d not supported", bitspersample,
  103.             0,0,0,0 );
  104.         }
  105.         }
  106.     pgm_writepgmrow( stdout, grayrow, cols, maxval );
  107.     }
  108.     pm_close( ifd );
  109.  
  110.     exit( 0 );
  111.     }
  112.  
  113. int
  114. gethexit( ifd )
  115. FILE *ifd;
  116.     {
  117.     register int i;
  118.     register char c;
  119.  
  120.     for ( ; ; )
  121.     {
  122.     i = getc( ifd );
  123.     if ( i == EOF )
  124.         pm_error( "premature EOF", 0,0,0,0,0 );
  125.     c = (char) i;
  126.     if ( c >= '0' && c <= '9' )
  127.         return c - '0';
  128.     else if ( c >= 'A' && c <= 'F' )
  129.         return c - 'A' + 10;
  130.     else if ( c >= 'a' && c <= 'f' )
  131.         return c - 'a' + 10;
  132.     /* Else ignore - whitespace. */
  133.     }
  134.     }
  135.