2 * Copyright 2008 ZXing authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.qrcode.encoder;
20 * JAVAPORT: This should be combined with BitArray in the future, although that class is not yet
21 * dynamically resizeable. This implementation is reasonable but there is a lot of function calling
22 * in loops I'd like to get rid of.
24 * @author satorux@google.com (Satoru Takabayashi) - creator
25 * @author dswitkin@google.com (Daniel Switkin) - ported from C++
27 public final class BitVector {
29 private int sizeInBits;
30 private int bytePosition;
33 // For efficiency, start out with some room to work.
34 private static final int DEFAULT_SIZE_IN_BITS = 32 * 8;
37 sizeInBits = DEFAULT_SIZE_IN_BITS;
39 array = new byte[DEFAULT_SIZE_IN_BITS / 8];
42 // Return the bit value at "index".
43 public int at(final int index) {
44 Debug.DCHECK_LE(0, index);
45 Debug.DCHECK_LT(index, sizeInBits);
46 final int value = array[index / 8];
47 return (value >> (7 - (index % 8))) & 1;
50 // Return the number of bits in the bit vector.
55 // Return the number of bytes in the bit vector.
56 public int num_bytes() {
57 return sizeInBits / 8;
60 // Append one bit to the bit vector.
61 public void AppendBit(final int bit) {
62 Debug.DCHECK(bit == 0 || bit == 1);
63 final int num_bits_in_last_byte = sizeInBits % 8;
64 // We'll expand array if we don't have bits in the last byte.
65 if (num_bits_in_last_byte == 0) {
68 // Modify the last byte.
69 array[array.length - 1] |= (bit << (7 - num_bits_in_last_byte));
73 // Append "num_bits" bits in "value" to the bit vector.
74 // REQUIRES: 0<= num_bits <= 32.
77 // - AppendBits(0x00, 1) adds 0.
78 // - AppendBits(0x00, 4) adds 0000.
79 // - AppendBits(0xff, 8) adds 11111111.
80 public void AppendBits(final int value, final int num_bits) {
81 Debug.DCHECK(num_bits >= 0 && num_bits <= 32);
82 int num_bits_left = num_bits;
83 while (num_bits_left > 0) {
84 // Optimization for byte-oriented appending.
85 if (sizeInBits % 8 == 0 && num_bits_left >= 8) {
86 final int newByte = (value >> (num_bits_left - 8)) & 0xff;
91 final int bit = (value >> (num_bits_left - 1)) & 1;
100 // JAVAPORT: Uncomment and implement when a substitute for StringPiece is chosen.
101 // public void AppendBytes(final StringPiece stringPiece) {
102 // for (int i = 0; i < stringPiece.size(); ++i) {
103 // AppendBits(stringPiece[i], 8);
108 public void AppendBitVector(final BitVector bits) {
109 int size = bits.size();
110 for (int i = 0; i < size; ++i) {
111 AppendBit(bits.at(i));
115 // Modify the bit vector by XOR'ing with "other"
116 public void XOR(final BitVector other) {
117 Debug.DCHECK_EQ(sizeInBits, other.size());
118 for (int i = 0; i < array.length; ++i) {
119 // The last byte could be incomplete (i.e. not have 8 bits in
120 // it) but there is no problem since 0 XOR 0 == 0.
121 array[i] ^= other.array[i];
125 // Return String like "01110111" for debugging.
126 public String toString() {
127 StringBuffer result = new StringBuffer(sizeInBits);
128 for (int i = 0; i < sizeInBits; ++i) {
131 } else if (at(i) == 1) {
137 return result.toString();
140 // Add a new byte to the end, possibly reallocating and doubling the size of the array if we've
142 private void appendByte(int value) {
143 if (bytePosition >= array.length) {
144 byte[] newArray = new byte[array.length * 2];
145 System.arraycopy(array, 0, newArray, 0, array.length);
148 array[bytePosition] = (byte) value;