home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume5 / pbm3 / part4 / libpbm4.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-03  |  2.9 KB  |  151 lines

  1. /* libpbm4.c - pbm utility library part 4
  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 <stdio.h>
  14. #include "pbm.h"
  15. #include "libpbm.h"
  16.  
  17.  
  18. static int version, item, bitsinitem, bitshift;
  19. static int bitspercount, count, repeatbits, repeatindex;
  20. static bit repeat, repeatbuf[MAX_REPEATBITS];
  21. static FILE *cbm_file;
  22.  
  23.  
  24. static
  25. cbm_getinit( file, colsP, rowsP )
  26. FILE *file;
  27. int *colsP, *rowsP;
  28.     {
  29.     int magic2;
  30.  
  31.     if ( getc( file ) != CBM_MAGIC1 )
  32.     {
  33.     fprintf( stderr, "Bad first magic number.\n" );
  34.     exit( 1 );
  35.     }
  36.     if ( ( magic2 = getc( file ) ) != OCBM_MAGIC2 && magic2 != NCBM_MAGIC2 )
  37.     {
  38.     fprintf( stderr, "Bad second magic number.\n" );
  39.     exit( 1 );
  40.     }
  41.  
  42.     if ( magic2 == OCBM_MAGIC2 )
  43.     version = VERSION_OLDCBM;
  44.     else
  45.     {
  46.     version = getc( file );
  47.     switch ( version )
  48.         {
  49.         case VERSION_RUNLEN:
  50.         bitspercount = getc( file );
  51.         repeatbits = getc( file );
  52.         count = 0;
  53.         break;
  54.  
  55.         default:
  56.         fprintf( stderr, "Unknown cbm version %d.\n", version );
  57.         exit( 1 );
  58.         }
  59.     }
  60.  
  61.     *colsP = getc( file ) << 8;
  62.     *colsP += getc( file );
  63.     *rowsP = getc( file ) << 8;
  64.     *rowsP += getc( file );
  65.  
  66.     bitsinitem = 8;
  67.     cbm_file = file;
  68.     }
  69.  
  70. static bit
  71. cbm_getbit( )
  72.     {
  73.     bit b;
  74.  
  75.     if ( bitsinitem == 8 )
  76.     {
  77.     item = getc( cbm_file );
  78.     bitsinitem = 0;
  79.     bitshift = 7;
  80.     }
  81.     bitsinitem++;
  82.     b = ( item >> bitshift) & 1;
  83.     bitshift--;
  84.     return b;
  85.     }
  86.  
  87. static bit
  88. cbm_getrlbit( )
  89.     {
  90.     int i;
  91.  
  92.     if ( count == 0 )
  93.     {
  94.     repeat = cbm_getbit( );
  95.  
  96.     for ( i = 0; i < bitspercount; i++ )
  97.         count = ( count << 1 ) + cbm_getbit( );
  98.     if ( count == 0 )
  99.         {
  100.         fprintf( stderr, "Error - zero count in cbm file.\n" );
  101.         exit( 1 );
  102.         }
  103.  
  104.     if ( repeat )
  105.         {
  106.         for ( i = 0; i < repeatbits; i++ )
  107.         repeatbuf[i] = cbm_getbit( );
  108.         repeatindex = repeatbits - 1;
  109.         }
  110.     }
  111.  
  112.     count--;
  113.     if ( repeat )
  114.     {
  115.     repeatindex = ( repeatindex + 1 ) % repeatbits;
  116.     return repeatbuf[repeatindex];
  117.     }
  118.     else
  119.     return cbm_getbit( );
  120.     }
  121.  
  122. bit **
  123. pbm_readcbm( file, colsP, rowsP )
  124. FILE *file;
  125. int *colsP, *rowsP;
  126.     {
  127.     bit **bits;
  128.     int row, col;
  129.  
  130.     cbm_getinit( file, colsP, rowsP );
  131.  
  132.     bits = pbm_allocarray( *colsP, *rowsP );
  133.  
  134.     switch ( version )
  135.     {
  136.     case VERSION_OLDCBM:
  137.     for ( row = 0; row < *rowsP; row++ )
  138.         for ( col = 0; col < *colsP; col++ )
  139.         bits[row][col] = cbm_getbit( );
  140.     break;
  141.  
  142.     case VERSION_RUNLEN:
  143.     for ( row = 0; row < *rowsP; row++ )
  144.         for ( col = 0; col < *colsP; col++ )
  145.         bits[row][col] = cbm_getrlbit( );
  146.     break;
  147.     }
  148.  
  149.     return bits;
  150.     }
  151.