home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 3 / Info_Mac_1994-01.iso / HyperCard / TIFFWindow1.1 / bits.h < prev    next >
Text File  |  1993-04-24  |  2KB  |  66 lines

  1. /*
  2.  * This software is copyright 1992 by Robert Morris.
  3.  * You may freely redistribute this software as shareware
  4.  * if you do so in the same form as you got it. If you find
  5.  * this software useful, please send $12 to:
  6.  *   Robert Morris
  7.  *   P.O. Box 1044
  8.  *   Harvard Square Station
  9.  *   Cambridge, MA 02238
  10.  *   ecognome@aol.com
  11.  * If you incorporate any of this software in any kind of
  12.  * commercial product, please send $2 per copy distributed
  13.  * to the above address.
  14.  */
  15.  
  16. /*
  17.  * General-purpose package for streams of bits.
  18.  * The high bit in a byte is "first" is bigendian is 1, otherwise last.
  19.  */
  20.  
  21. struct bits{
  22.     unsigned char *bits;
  23.     long nbits;
  24.     long nextbit; /* next bit to read beyond the cached bits */
  25.     long cache;  /* cache >> 1 is first cached bit. up to 31 bits here. */
  26.     long cached; /* how many bits cached in cache */
  27.     char bigendian;
  28. };
  29.  
  30. #define SeekBits(b, bitno) ((b)->nextbit = (bitno), (b)->cached = 0)
  31. #define TellBits(b) ((b)->nextbit - (b)->cached)
  32. #define TestBitsEOF(b) (TellBits(b) >= (b)->nbits)
  33. #define SetBitsLength(b, n) ((b)->nbits = (n), (b)->cached = 0)
  34.  
  35. #define ReadBit(b) ((b)->cached > 0 ? \
  36.                     ((b)->cached -= 1, ((b)->cache >>= 1), ((b)->cache & 1)) : \
  37.                     _ReadBit(b))
  38.                     
  39. #define Peek4Bits(b) (((b)->cached < 4 ? FillBitCache(b) : 0), \
  40.                       ((b)->cache >> 1) & 0xf)
  41.                       
  42. #define Read2Bits(b, dst) (((b)->cached < 2 ? FillBitCache(b) : 0), \
  43.                            (dst) = (((b)->cache >> 1) & 3), \
  44.                            (b)->cached -= 2, \
  45.                            (b)->cache >>= 2)
  46.  
  47. #define TossBits(b, n) ((b)->cached -= (n), (b)->cache >>= (n))
  48.  
  49. #define WriteBitSpan(b, bit, n) { long bitno; long byteno; \
  50.                                     bitno = (b)->nextbit & 7; \
  51.                                     if((n) <= (8-bitno)){ \
  52.                                         byteno = (b)->nextbit >> 3; \
  53.                                         (b)->bits[byteno] = (((bit)?0xff:0) >> bitno) | \
  54.                                           ((b)->bits[byteno] & (0xff00 >> bitno)); \
  55.                                         (b)->nextbit += (n); \
  56.                                     } else { \
  57.                                         _WriteBitSpan((b), (bit), (n)); \
  58.                                     } \
  59.                                    }
  60.  
  61. OSErr NewBits(struct bits *, unsigned char *data, long nbits, char bigendian, int writeable);
  62. long _ReadBit(struct bits *);
  63. OSErr WriteBits(struct bits *, long bits, long n);
  64. OSErr ScanBits(struct bits *b, long wanted);
  65. OSErr _WriteBitSpan(struct bits *b, long bit, long n);
  66. OSErr FillBitCache(struct bits *b);