Began porting the QR Code encoder from ChartServer to Java/ZXing. Some important...
[zxing.git] / core / src / com / google / zxing / qrcode / encoder / BitVector.java
1 /*
2  * Copyright 2008 ZXing authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.google.zxing.qrcode.encoder;
18
19 /**
20  * @author satorux@google.com (Satoru Takabayashi) - creator
21  * @author dswitkin@google.com (Daniel Switkin) - ported from C++
22  */
23 public final class BitVector {
24
25   private int size_;
26   private String bytes_;
27
28   public BitVector() {
29     size_ = 0;
30   }
31
32   // Return the bit value at "index".
33   public int at(final int index) {
34     Debug.DCHECK_LE(0, index);
35     Debug.DCHECK_LT(index, size_);
36     final uint8 byte = bytes_.at(index / 8);
37     return (byte >> (7 - (index % 8))) & 1;
38   }
39
40   // Return the number of bits in the bit vector.
41   public int size() {
42     return size_;
43   }
44
45   // Return the number of bytes in the bit vector.
46   public int num_bytes() {
47     return size_ / 8;
48   }
49
50   // Append one bit to the bit vector.
51   public void AppendBit(final int bit) {
52     Debug.DCHECK(bit == 0 || bit == 1);
53     final int num_bits_in_last_byte = size_ % 8;
54     // We'll expand bytes_ if we don't have bits in the last byte.
55     if (num_bits_in_last_byte == 0) {
56       bytes_.push_back(0);
57     }
58     // Modify the last byte.
59     bytes_[bytes_.size() - 1] |= (bit << (7 - num_bits_in_last_byte));
60     ++size_;
61   }
62
63   // Append "num_bits" bits in "value" to the bit vector.
64   // REQUIRES: 0<= num_bits <= 32.
65   //
66   // Examples:
67   // - AppendBits(0x00, 1) adds 0.
68   // - AppendBits(0x00, 4) adds 0000.
69   // - AppendBits(0xff, 8) adds 11111111.
70   public void AppendBits(final uint32 value, final int num_bits) {
71     Debug.DCHECK(num_bits >= 0 && num_bits <= 32);
72     int num_bits_left = num_bits;
73     while (num_bits_left > 0) {
74       // Optimization for byte-oriented appending.
75       if (size_ % 8 == 0 && num_bits_left >= 8) {
76         final uint8 byte = (value >> (num_bits_left - 8)) & 0xff;
77         bytes_.push_back(byte);
78         size_ += 8;
79         num_bits_left -= 8;
80       } else {
81         final int bit = (value >> (num_bits_left - 1)) & 1;
82         AppendBit(bit);
83         --num_bits_left;
84       }
85     }
86   }
87
88   // Append "bytes".
89   public void AppendBytes(final StringPiece &bytes) {
90     for (int i = 0; i < bytes.size(); ++i) {
91       AppendBits(bytes[i], 8);
92     }
93   }
94
95   // Append "bits".
96   public void AppendBitVector(final BitVector &bits) {
97     for (int i = 0; i < bits.size(); ++i) {
98       AppendBit(bits.at(i));
99     }
100   }
101
102   // Modify the bit vector by XOR'ing with "other"
103   public void XOR(final BitVector &other) {
104     Debug.DCHECK_EQ(size_, other.size());
105     for (int i = 0; i < bytes_.size(); ++i) {
106       // The last byte could be incomplete (i.e. not have 8 bits in
107       // it) but there is no problem since 0 XOR 0 == 0.
108       bytes_[i] ^= other.ToString()[i];
109     }
110   }
111
112   // Return the content of the bit vector as String.
113   public final String &ToString() {
114     return bytes_;
115   }
116
117   // Return String like "01110111" for debugging.
118   public String ToASCII() {
119     String result;
120     result.reserve(size_);
121     for (int i = 0; i < size_; ++i) {
122       if (at(i) == 0) {
123         result.append("0");
124       } else if (at(i) == 1) {
125         result.append("1");
126       } else {
127         Debug.DCHECK(false);
128       }
129     }
130     return result;
131   }
132
133 }