sources/huffman/bitreader.h

changeset 76
6de6d9a64ebd
parent 75
5f8a03274d75
child 77
32ef969adeed
equal deleted inserted replaced
75:5f8a03274d75 76:6de6d9a64ebd
1 /**
2 * skulltag::BitReader class - Allows reading arbitrary bit lengths of data.
3 * Version 1 - Revsion 0
4 *
5 * Copyright 2009 Timothy Landers
6 * email: code.vortexcortex@gmail.com
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
27 #ifndef _BIT_READER_VERSION
28 #define _BIT_READER_VERSION 1
29
30 /** Prevents naming convention problems via encapsulation. */
31 namespace skulltag { // scope limitation
32
33 /** BitReader - Allows reading of varying amounts of bits from a char buffer. <br>
34 * Very usefull for inputting variable bit length encodings such as Huffman. */
35
36 class BitReader {
37 int bufferBits; /**< intermediary buffer of bits. */
38 int bitsUsed; /**< number of bits used in the buffer. */
39 unsigned char const * currentByte; /**< position in memory the next char will be read. */
40 int bytesAvailable; /**< amount of available space left in the input buffer in bytes. Excludes the contents of bufferBits. */
41 int bitsAvailable; /**< amount of available space left in the input buffer in bits. Includes the contents of bufferBits. */
42 int maximumBytes; /**< total amount of bytes that can be read from the input buffer. */
43 static int mask[]; /**< maps a number of bits to a bit mask containing as many bits. */
44 static int intSize; /**< number of chars in an int. */
45 static int intBitSize; /**< number of bits in an ing. */
46 public:
47
48 /** Creates a new BitReader. */
49 BitReader();
50
51 /** Creates a new BitReader.
52 * @param input Source of data that bits will be read from.
53 * @param max Maximum number of chars to read. */
54 BitReader( unsigned char const * input, int const &max );
55
56 /** Sets the input buffer that bytes will be read from.
57 * @param input Source of data that bits will be read from.
58 * @param max Maximum number of chars to input.
59 * @return true if successful, false if an error occurs. */
60 bool inputBuffer( unsigned char const * input, int const &max );
61
62 /** Fetches a specified number of bits and stores them in an int.
63 * @param bits destination of the retrieved bits. <br>
64 * The bits will be stored in the least significant portion of the int.
65 * @param count the number of bits to fetch.
66 * @return the number of bits read -- may not be equal to the amount requested. */
67 int get( int &bits, int const &count );
68
69 /** @return Amount of bits that can be read from this BitReader. */
70 int availableBits();
71
72 private:
73
74 /** Fills the internal bit buffer.
75 * @return true if successful, false if an error occurs. */
76 bool fill();
77
78 /** Initializes this BitReader. */
79 void init();
80
81 };
82
83 }
84 #endif
85

mercurial