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

  1. /* libpbm2.c - pbm utility library part 2
  2. **
  3. ** Copyright (C) 1988 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 "pbm.h"
  14. #include "libpbm.h"
  15.  
  16. static bit pbm_getbit ARGS((FILE *));
  17. static bit
  18. pbm_getbit( file )
  19.     FILE* file;
  20.     {
  21.     register char ch;
  22.  
  23.     do
  24.     {
  25.     ch = pbm_getc( file );
  26.     }
  27.     while ( ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' );
  28.  
  29.     if ( ch != '0' && ch != '1' )
  30.     pm_error( "junk in file where bits should be" );
  31.  
  32.     return ( ch == '1' ) ? 1 : 0;
  33.     }
  34.  
  35. int
  36. pbm_readmagicnumber( file )
  37.     FILE* file;
  38.     {
  39.     int ich1, ich2;
  40.  
  41.     ich1 = getc( file );
  42.     if ( ich1 == EOF )
  43.     pm_error( "EOF / read error reading magic number" );
  44.     ich2 = getc( file );
  45.     if ( ich2 == EOF )
  46.     pm_error( "EOF / read error reading magic number" );
  47.     return ich1 * 256 + ich2;
  48.     }
  49.  
  50. void
  51. pbm_readpbminitrest( file, colsP, rowsP )
  52.     FILE* file;
  53.     int* colsP;
  54.     int* rowsP;
  55.     {
  56.     /* Read size. */
  57.     *colsP = pbm_getint( file );
  58.     *rowsP = pbm_getint( file );
  59.     }
  60.  
  61. void
  62. pbm_readpbminit( file, colsP, rowsP, formatP )
  63.     FILE* file;
  64.     int* colsP;
  65.     int* rowsP;
  66.     int* formatP;
  67.     {
  68.     /* Check magic number. */
  69.     *formatP = pbm_readmagicnumber( file );
  70.     switch ( PBM_FORMAT_TYPE(*formatP) )
  71.     {
  72.         case PBM_TYPE:
  73.     pbm_readpbminitrest( file, colsP, rowsP );
  74.     break;
  75.  
  76.     default:
  77.     pm_error( "bad magic number - not a pbm file" );
  78.     }
  79.     }
  80.  
  81. void
  82. pbm_readpbmrow( file, bitrow, cols, format )
  83.     FILE* file;
  84.     bit* bitrow;
  85.     int cols, format;
  86.     {
  87.     register int col, bitshift;
  88.     register unsigned char item;
  89.     register bit* bP;
  90.  
  91.     switch ( format )
  92.     {
  93.     case PBM_FORMAT:
  94.     for ( col = 0, bP = bitrow; col < cols; ++col, ++bP )
  95.         *bP = pbm_getbit( file );
  96.     break;
  97.  
  98.     case RPBM_FORMAT:
  99.     bitshift = -1;
  100.     for ( col = 0, bP = bitrow; col < cols; ++col, ++bP )
  101.         {
  102.         if ( bitshift == -1 )
  103.         {
  104.         item = pbm_getrawbyte( file );
  105.         bitshift = 7;
  106.         }
  107.         *bP = ( item >> bitshift ) & 1;
  108.         --bitshift;
  109.         }
  110.     break;
  111.  
  112.     default:
  113.     pm_error( "can't happen" );
  114.     }
  115.     }
  116.  
  117. bit**
  118. pbm_readpbm( file, colsP, rowsP )
  119.     FILE* file;
  120.     int* colsP;
  121.     int* rowsP;
  122.     {
  123.     register bit** bits;
  124.     int format, row;
  125.  
  126.     pbm_readpbminit( file, colsP, rowsP, &format );
  127.  
  128.     bits = pbm_allocarray( *colsP, *rowsP );
  129.  
  130.     for ( row = 0; row < *rowsP; ++row )
  131.     pbm_readpbmrow( file, bits[row], *colsP, format );
  132.  
  133.     return bits;
  134.     }
  135.