home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / netpbma.zip / pgm / libpgm1.c < prev    next >
C/C++ Source or Header  |  1996-11-10  |  4KB  |  150 lines

  1. /* libpgm1.c - pgm utility library part 1
  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. #include "libpgm.h"
  15. #include "pbm.h"
  16. #include "libpbm.h"
  17.  
  18. void
  19. pgm_init( argcP, argv )
  20.     int* argcP;
  21.     char* argv[];
  22.     {
  23.     pbm_init( argcP, argv );
  24.     }
  25.  
  26. void
  27. pgm_readpgminitrest( file, colsP, rowsP, maxvalP )
  28.     FILE* file;
  29.     int* colsP;
  30.     int* rowsP;
  31.     gray* maxvalP;
  32.     {
  33.     int maxval;
  34.  
  35.     /* Read size. */
  36.     *colsP = pbm_getint( file );
  37.     *rowsP = pbm_getint( file );
  38.  
  39.     /* Read maxval. */
  40.     maxval = pbm_getint( file );
  41.     if ( maxval > PGM_MAXMAXVAL )
  42.         pm_error( "maxval is too large - try reconfiguring with PGM_BIGGRAYS" );
  43.     *maxvalP = maxval;
  44.     }
  45.  
  46. gray pgm_pbmmaxval = 1;
  47.  
  48. void
  49. pgm_readpgminit( file, colsP, rowsP, maxvalP, formatP )
  50.     FILE* file;
  51.     int* colsP;
  52.     int* rowsP;
  53.     int* formatP;
  54.     gray* maxvalP;
  55.     {
  56.     /* Check magic number. */
  57.     *formatP = pbm_readmagicnumber( file );
  58.     switch ( PGM_FORMAT_TYPE(*formatP) )
  59.         {
  60.         case PGM_TYPE:
  61.         pgm_readpgminitrest( file, colsP, rowsP, maxvalP );
  62.         break;
  63.  
  64.         case PBM_TYPE:
  65.         pbm_readpbminitrest( file, colsP, rowsP );
  66.         *maxvalP = pgm_pbmmaxval;
  67.         break;
  68.  
  69.         default:
  70.         pm_error( "bad magic number - not a pgm or pbm file" );
  71.         }
  72.     }
  73.  
  74. #if __STDC__
  75. void
  76. pgm_readpgmrow( FILE* file, gray* grayrow, int cols, gray maxval, int format )
  77. #else /*__STDC__*/
  78. void
  79. pgm_readpgmrow( file, grayrow, cols, maxval, format )
  80.     FILE* file;
  81.     gray* grayrow;
  82.     int cols;
  83.     gray maxval;
  84.     int format;
  85. #endif /*__STDC__*/
  86.     {
  87.     register int col;
  88.     register gray* gP;
  89.     bit* bitrow;
  90.     register bit* bP;
  91.  
  92.     switch ( format )
  93.         {
  94.         case PGM_FORMAT:
  95.         for ( col = 0, gP = grayrow; col < cols; ++col, ++gP )
  96.             {
  97.             *gP = pbm_getint( file );
  98. #ifdef DEBUG
  99.             if ( *gP > maxval )
  100.                 pm_error( "value out of bounds (%u > %u)", *gP, maxval );
  101. #endif /*DEBUG*/
  102.             }
  103.         break;
  104.  
  105.         case RPGM_FORMAT:
  106.         for ( col = 0, gP = grayrow; col < cols; ++col, ++gP )
  107.             {
  108.             *gP = pbm_getrawbyte( file );
  109. #ifdef DEBUG
  110.             if ( *gP > maxval )
  111.                 pm_error( "value out of bounds (%u > %u)", *gP, maxval );
  112. #endif /*DEBUG*/
  113.             }
  114.         break;
  115.  
  116.         case PBM_FORMAT:
  117.         case RPBM_FORMAT:
  118.         bitrow = pbm_allocrow( cols );
  119.         pbm_readpbmrow( file, bitrow, cols, format );
  120.         for ( col = 0, gP = grayrow, bP = bitrow; col < cols; ++col, ++gP, ++bP )
  121.             *gP = ( *bP == PBM_WHITE ) ? maxval : 0;
  122.         pbm_freerow( bitrow );
  123.         break;
  124.  
  125.         default:
  126.         pm_error( "can't happen" );
  127.         }
  128.     }
  129.  
  130. gray**
  131. pgm_readpgm( file, colsP, rowsP, maxvalP )
  132.     FILE* file;
  133.     int* colsP;
  134.     int* rowsP;
  135.     gray* maxvalP;
  136.     {
  137.     gray** grays;
  138.     int row;
  139.     int format;
  140.  
  141.     pgm_readpgminit( file, colsP, rowsP, maxvalP, &format );
  142.  
  143.     grays = pgm_allocarray( *colsP, *rowsP );
  144.  
  145.     for ( row = 0; row < *rowsP; ++row )
  146.         pgm_readpgmrow( file, grays[row], *colsP, *maxvalP, format );
  147.  
  148.     return grays;
  149.     }
  150.