home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / NETWORK / netpbm_src.lzh / NETPBM / PBM / libpbm2.c < prev    next >
Text File  |  1996-11-24  |  3KB  |  138 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. #ifndef _OSK
  28.     while ( ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' );
  29. #else
  30.     while ( ch == ' ' || ch == '\t' || ch == '\n' || ch == '\l' );
  31. #endif
  32.     if ( ch != '0' && ch != '1' )
  33.     pm_error( "junk in file where bits should be" );
  34.  
  35.     return ( ch == '1' ) ? 1 : 0;
  36.     }
  37.  
  38. int
  39. pbm_readmagicnumber( file )
  40.     FILE* file;
  41.     {
  42.     int ich1, ich2;
  43.  
  44.     ich1 = getc( file );
  45.     if ( ich1 == EOF )
  46.     pm_error( "EOF / read error reading magic number" );
  47.     ich2 = getc( file );
  48.     if ( ich2 == EOF )
  49.     pm_error( "EOF / read error reading magic number" );
  50.     return ich1 * 256 + ich2;
  51.     }
  52.  
  53. void
  54. pbm_readpbminitrest( file, colsP, rowsP )
  55.     FILE* file;
  56.     int* colsP;
  57.     int* rowsP;
  58.     {
  59.     /* Read size. */
  60.     *colsP = pbm_getint( file );
  61.     *rowsP = pbm_getint( file );
  62.     }
  63.  
  64. void
  65. pbm_readpbminit( file, colsP, rowsP, formatP )
  66.     FILE* file;
  67.     int* colsP;
  68.     int* rowsP;
  69.     int* formatP;
  70.     {
  71.     /* Check magic number. */
  72.     *formatP = pbm_readmagicnumber( file );
  73.     switch ( PBM_FORMAT_TYPE(*formatP) )
  74.     {
  75.         case PBM_TYPE:
  76.     pbm_readpbminitrest( file, colsP, rowsP );
  77.     break;
  78.  
  79.     default:
  80.     pm_error( "bad magic number - not a pbm file" );
  81.     }
  82.     }
  83.  
  84. void
  85. pbm_readpbmrow( file, bitrow, cols, format )
  86.     FILE* file;
  87.     bit* bitrow;
  88.     int cols, format;
  89.     {
  90.     register int col, bitshift;
  91.     register unsigned char item;
  92.     register bit* bP;
  93.  
  94.     switch ( format )
  95.     {
  96.     case PBM_FORMAT:
  97.     for ( col = 0, bP = bitrow; col < cols; ++col, ++bP )
  98.         *bP = pbm_getbit( file );
  99.     break;
  100.  
  101.     case RPBM_FORMAT:
  102.     bitshift = -1;
  103.     for ( col = 0, bP = bitrow; col < cols; ++col, ++bP )
  104.         {
  105.         if ( bitshift == -1 )
  106.         {
  107.         item = pbm_getrawbyte( file );
  108.         bitshift = 7;
  109.         }
  110.         *bP = ( item >> bitshift ) & 1;
  111.         --bitshift;
  112.         }
  113.     break;
  114.  
  115.     default:
  116.     pm_error( "can't happen" );
  117.     }
  118.     }
  119.  
  120. bit**
  121. pbm_readpbm( file, colsP, rowsP )
  122.     FILE* file;
  123.     int* colsP;
  124.     int* rowsP;
  125.     {
  126.     register bit** bits;
  127.     int format, row;
  128.  
  129.     pbm_readpbminit( file, colsP, rowsP, &format );
  130.  
  131.     bits = pbm_allocarray( *colsP, *rowsP );
  132.  
  133.     for ( row = 0; row < *rowsP; ++row )
  134.     pbm_readpbmrow( file, bits[row], *colsP, format );
  135.  
  136.     return bits;
  137.     }
  138.