C++ port: add changes to HybridBinarizer
[zxing.git] / cpp / core / src / zxing / common / BitSource.h
1 #ifndef __BIT_SOURCE_H__
2 #define __BIT_SOURCE_H__
3
4 /*
5  *  BitSource.h
6  *  zxing
7  *
8  *  Copyright 2010 ZXing authors All rights reserved.
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */
22
23 #include <zxing/common/Array.h>
24
25 namespace zxing {
26 /**
27  * <p>This provides an easy abstraction to read bits at a time from a sequence of bytes, where the
28  * number of bits read is not often a multiple of 8.</p>
29  *
30  * <p>This class is not thread-safe.</p>
31  *
32  * @author srowen@google.com (Sean Owen)
33  * @author christian.brunschen@gmail.com (Christian Brunschen)
34  */
35 class BitSource : public Counted {
36   typedef unsigned char byte;
37 private:
38   ArrayRef<byte> bytes_;
39   int byteOffset_;
40   int bitOffset_;
41 public:
42   /**
43    * @param bytes bytes from which this will read bits. Bits will be read from the first byte first.
44    * Bits are read within a byte from most-significant to least-significant bit.
45    */
46   BitSource(ArrayRef<byte> &bytes) :
47       bytes_(bytes), byteOffset_(0), bitOffset_(0) {
48   }
49
50
51   /**
52    * @param numBits number of bits to read
53    * @return int representing the bits read. The bits will appear as the least-significant
54    *         bits of the int
55    * @throws IllegalArgumentException if numBits isn't in [1,32]
56    */
57   int readBits(int numBits);
58
59   /**
60    * @return number of bits that can be read successfully
61    */
62   int available();
63 };
64
65 }
66
67 #endif // __BIT_SOURCE_H__