mirror of
https://github.com/zxing/zxing.git
synced 2025-01-13 04:07:27 -08:00
Made Java naming changes to BitVector.
git-svn-id: https://zxing.googlecode.com/svn/trunk@757 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
afdc4508b7
commit
98b34ae187
|
@ -52,12 +52,12 @@ public final class BitVector {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the number of bytes in the bit vector.
|
// Return the number of bytes in the bit vector.
|
||||||
public int num_bytes() {
|
public int sizeInBytes() {
|
||||||
return (sizeInBits + 7) >> 3;
|
return (sizeInBits + 7) >> 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Append one bit to the bit vector.
|
// Append one bit to the bit vector.
|
||||||
public void AppendBit(final int bit) {
|
public void appendBit(final int bit) {
|
||||||
if (!(bit == 0 || bit == 1)) {
|
if (!(bit == 0 || bit == 1)) {
|
||||||
throw new IllegalArgumentException("Bad bit");
|
throw new IllegalArgumentException("Bad bit");
|
||||||
}
|
}
|
||||||
|
@ -76,10 +76,10 @@ public final class BitVector {
|
||||||
// REQUIRES: 0<= num_bits <= 32.
|
// REQUIRES: 0<= num_bits <= 32.
|
||||||
//
|
//
|
||||||
// Examples:
|
// Examples:
|
||||||
// - AppendBits(0x00, 1) adds 0.
|
// - appendBits(0x00, 1) adds 0.
|
||||||
// - AppendBits(0x00, 4) adds 0000.
|
// - appendBits(0x00, 4) adds 0000.
|
||||||
// - AppendBits(0xff, 8) adds 11111111.
|
// - appendBits(0xff, 8) adds 11111111.
|
||||||
public void AppendBits(final int value, final int num_bits) {
|
public void appendBits(final int value, final int num_bits) {
|
||||||
if (num_bits < 0 || num_bits > 32) {
|
if (num_bits < 0 || num_bits > 32) {
|
||||||
throw new IllegalArgumentException("Num bits must be between 0 and 32");
|
throw new IllegalArgumentException("Num bits must be between 0 and 32");
|
||||||
}
|
}
|
||||||
|
@ -92,22 +92,22 @@ public final class BitVector {
|
||||||
num_bits_left -= 8;
|
num_bits_left -= 8;
|
||||||
} else {
|
} else {
|
||||||
final int bit = (value >> (num_bits_left - 1)) & 1;
|
final int bit = (value >> (num_bits_left - 1)) & 1;
|
||||||
AppendBit(bit);
|
appendBit(bit);
|
||||||
--num_bits_left;
|
--num_bits_left;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Append "bits".
|
// Append "bits".
|
||||||
public void AppendBitVector(final BitVector bits) {
|
public void appendBitVector(final BitVector bits) {
|
||||||
int size = bits.size();
|
int size = bits.size();
|
||||||
for (int i = 0; i < size; ++i) {
|
for (int i = 0; i < size; ++i) {
|
||||||
AppendBit(bits.at(i));
|
appendBit(bits.at(i));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Modify the bit vector by XOR'ing with "other"
|
// Modify the bit vector by XOR'ing with "other"
|
||||||
public void XOR(final BitVector other) {
|
public void xor(final BitVector other) {
|
||||||
if (sizeInBits != other.size()) {
|
if (sizeInBits != other.size()) {
|
||||||
throw new IllegalArgumentException("BitVector sizes don't match");
|
throw new IllegalArgumentException("BitVector sizes don't match");
|
||||||
}
|
}
|
||||||
|
|
|
@ -134,14 +134,14 @@ public final class Encoder {
|
||||||
BitVector data_bits = new BitVector();
|
BitVector data_bits = new BitVector();
|
||||||
AppendBytes(bytes, mode, data_bits);
|
AppendBytes(bytes, mode, data_bits);
|
||||||
// Step 3: Initialize QR code that can contain "data_bits".
|
// Step 3: Initialize QR code that can contain "data_bits".
|
||||||
final int num_input_bytes = data_bits.num_bytes();
|
final int num_input_bytes = data_bits.sizeInBytes();
|
||||||
InitQRCode(num_input_bytes, ec_level, mode, qr_code);
|
InitQRCode(num_input_bytes, ec_level, mode, qr_code);
|
||||||
|
|
||||||
// Step 4: Build another bit vector that contains header and data.
|
// Step 4: Build another bit vector that contains header and data.
|
||||||
BitVector header_and_data_bits = new BitVector();
|
BitVector header_and_data_bits = new BitVector();
|
||||||
AppendModeInfo(qr_code.mode(), header_and_data_bits);
|
AppendModeInfo(qr_code.mode(), header_and_data_bits);
|
||||||
AppendLengthInfo(bytes.size(), qr_code.version(), qr_code.mode(), header_and_data_bits);
|
AppendLengthInfo(bytes.size(), qr_code.version(), qr_code.mode(), header_and_data_bits);
|
||||||
header_and_data_bits.AppendBitVector(data_bits);
|
header_and_data_bits.appendBitVector(data_bits);
|
||||||
|
|
||||||
// Step 5: Terminate the bits properly.
|
// Step 5: Terminate the bits properly.
|
||||||
TerminateBits(qr_code.num_data_bytes(), header_and_data_bits);
|
TerminateBits(qr_code.num_data_bytes(), header_and_data_bits);
|
||||||
|
@ -280,14 +280,14 @@ public final class Encoder {
|
||||||
}
|
}
|
||||||
// Append termination bits. See 8.4.8 of JISX0510:2004 (p.24) for details.
|
// Append termination bits. See 8.4.8 of JISX0510:2004 (p.24) for details.
|
||||||
for (int i = 0; i < 4 && bits.size() < capacity; ++i) {
|
for (int i = 0; i < 4 && bits.size() < capacity; ++i) {
|
||||||
bits.AppendBit(0);
|
bits.appendBit(0);
|
||||||
}
|
}
|
||||||
final int num_bits_in_last_byte = bits.size() % 8;
|
final int num_bits_in_last_byte = bits.size() % 8;
|
||||||
// If the last byte isn't 8-bit aligned, we'll add padding bits.
|
// If the last byte isn't 8-bit aligned, we'll add padding bits.
|
||||||
if (num_bits_in_last_byte > 0) {
|
if (num_bits_in_last_byte > 0) {
|
||||||
final int num_padding_bits = 8 - num_bits_in_last_byte;
|
final int num_padding_bits = 8 - num_bits_in_last_byte;
|
||||||
for (int i = 0; i < num_padding_bits; ++i) {
|
for (int i = 0; i < num_padding_bits; ++i) {
|
||||||
bits.AppendBit(0);
|
bits.appendBit(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Should be 8-bit aligned here.
|
// Should be 8-bit aligned here.
|
||||||
|
@ -295,12 +295,12 @@ public final class Encoder {
|
||||||
throw new WriterException("Number of bits is not a multiple of 8");
|
throw new WriterException("Number of bits is not a multiple of 8");
|
||||||
}
|
}
|
||||||
// If we have more space, we'll fill the space with padding patterns defined in 8.4.9 (p.24).
|
// If we have more space, we'll fill the space with padding patterns defined in 8.4.9 (p.24).
|
||||||
final int num_padding_bytes = num_data_bytes - bits.num_bytes();
|
final int num_padding_bytes = num_data_bytes - bits.sizeInBytes();
|
||||||
for (int i = 0; i < num_padding_bytes; ++i) {
|
for (int i = 0; i < num_padding_bytes; ++i) {
|
||||||
if (i % 2 == 0) {
|
if (i % 2 == 0) {
|
||||||
bits.AppendBits(0xec, 8);
|
bits.appendBits(0xec, 8);
|
||||||
} else {
|
} else {
|
||||||
bits.AppendBits(0x11, 8);
|
bits.appendBits(0x11, 8);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (bits.size() != capacity) {
|
if (bits.size() != capacity) {
|
||||||
|
@ -369,7 +369,7 @@ public final class Encoder {
|
||||||
int num_data_bytes, int num_rs_blocks, BitVector result) throws WriterException {
|
int num_data_bytes, int num_rs_blocks, BitVector result) throws WriterException {
|
||||||
|
|
||||||
// "bits" must have "num_data_bytes" bytes of data.
|
// "bits" must have "num_data_bytes" bytes of data.
|
||||||
if (bits.num_bytes() != num_data_bytes) {
|
if (bits.sizeInBytes() != num_data_bytes) {
|
||||||
throw new WriterException("Number of bits and data bytes does not match");
|
throw new WriterException("Number of bits and data bytes does not match");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -407,7 +407,7 @@ public final class Encoder {
|
||||||
for (int j = 0; j < blocks.size(); ++j) {
|
for (int j = 0; j < blocks.size(); ++j) {
|
||||||
final ByteArray data_bytes = ((BlockPair) blocks.elementAt(j)).getDataBytes();
|
final ByteArray data_bytes = ((BlockPair) blocks.elementAt(j)).getDataBytes();
|
||||||
if (i < data_bytes.size()) {
|
if (i < data_bytes.size()) {
|
||||||
result.AppendBits(data_bytes.at(i), 8);
|
result.appendBits(data_bytes.at(i), 8);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -416,12 +416,12 @@ public final class Encoder {
|
||||||
for (int j = 0; j < blocks.size(); ++j) {
|
for (int j = 0; j < blocks.size(); ++j) {
|
||||||
final ByteArray ec_bytes = ((BlockPair) blocks.elementAt(j)).getErrorCorrectionBytes();
|
final ByteArray ec_bytes = ((BlockPair) blocks.elementAt(j)).getErrorCorrectionBytes();
|
||||||
if (i < ec_bytes.size()) {
|
if (i < ec_bytes.size()) {
|
||||||
result.AppendBits(ec_bytes.at(i), 8);
|
result.appendBits(ec_bytes.at(i), 8);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (num_total_bytes != result.num_bytes()) { // Should be same.
|
if (num_total_bytes != result.sizeInBytes()) { // Should be same.
|
||||||
throw new WriterException("Interleaving error: " + num_total_bytes + " and " + result.num_bytes() +
|
throw new WriterException("Interleaving error: " + num_total_bytes + " and " + result.sizeInBytes() +
|
||||||
" differ.");
|
" differ.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -445,7 +445,7 @@ public final class Encoder {
|
||||||
// false.
|
// false.
|
||||||
static void AppendModeInfo(int mode, BitVector bits) throws WriterException {
|
static void AppendModeInfo(int mode, BitVector bits) throws WriterException {
|
||||||
final int code = QRCode.GetModeCode(mode);
|
final int code = QRCode.GetModeCode(mode);
|
||||||
bits.AppendBits(code, 4);
|
bits.appendBits(code, 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -465,7 +465,7 @@ public final class Encoder {
|
||||||
if (num_letters > ((1 << num_bits) - 1)) {
|
if (num_letters > ((1 << num_bits) - 1)) {
|
||||||
throw new WriterException(num_letters + "is bigger than" + ((1 << num_bits) - 1));
|
throw new WriterException(num_letters + "is bigger than" + ((1 << num_bits) - 1));
|
||||||
}
|
}
|
||||||
bits.AppendBits(num_letters, num_bits);
|
bits.appendBits(num_letters, num_bits);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Append "bytes" in "mode" mode (encoding) into "bits". On success, store the result in "bits"
|
// Append "bytes" in "mode" mode (encoding) into "bits". On success, store the result in "bits"
|
||||||
|
@ -505,16 +505,16 @@ public final class Encoder {
|
||||||
// Encode three numeric letters in ten bits.
|
// Encode three numeric letters in ten bits.
|
||||||
final int num2 = bytes.at(i + 1) - '0';
|
final int num2 = bytes.at(i + 1) - '0';
|
||||||
final int num3 = bytes.at(i + 2) - '0';
|
final int num3 = bytes.at(i + 2) - '0';
|
||||||
bits.AppendBits(num1 * 100 + num2 * 10 + num3, 10);
|
bits.appendBits(num1 * 100 + num2 * 10 + num3, 10);
|
||||||
i += 3;
|
i += 3;
|
||||||
} else if (i + 1 < bytes.size()) {
|
} else if (i + 1 < bytes.size()) {
|
||||||
// Encode two numeric letters in seven bits.
|
// Encode two numeric letters in seven bits.
|
||||||
final int num2 = bytes.at(i + 1) - '0';
|
final int num2 = bytes.at(i + 1) - '0';
|
||||||
bits.AppendBits(num1 * 10 + num2, 7);
|
bits.appendBits(num1 * 10 + num2, 7);
|
||||||
i += 2;
|
i += 2;
|
||||||
} else {
|
} else {
|
||||||
// Encode one numeric letter in four bits.
|
// Encode one numeric letter in four bits.
|
||||||
bits.AppendBits(num1, 4);
|
bits.appendBits(num1, 4);
|
||||||
++i;
|
++i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -534,11 +534,11 @@ public final class Encoder {
|
||||||
throw new WriterException();
|
throw new WriterException();
|
||||||
}
|
}
|
||||||
// Encode two alphanumeric letters in 11 bits.
|
// Encode two alphanumeric letters in 11 bits.
|
||||||
bits.AppendBits(code1 * 45 + code2, 11);
|
bits.appendBits(code1 * 45 + code2, 11);
|
||||||
i += 2;
|
i += 2;
|
||||||
} else {
|
} else {
|
||||||
// Encode one alphanumeric letter in six bits.
|
// Encode one alphanumeric letter in six bits.
|
||||||
bits.AppendBits(code1, 6);
|
bits.appendBits(code1, 6);
|
||||||
++i;
|
++i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -548,7 +548,7 @@ public final class Encoder {
|
||||||
// "bits" and return true. On error, return false.
|
// "bits" and return true. On error, return false.
|
||||||
static void Append8BitBytes(final ByteArray bytes, BitVector bits) {
|
static void Append8BitBytes(final ByteArray bytes, BitVector bits) {
|
||||||
for (int i = 0; i < bytes.size(); ++i) {
|
for (int i = 0; i < bytes.size(); ++i) {
|
||||||
bits.AppendBits(bytes.at(i), 8);
|
bits.appendBits(bytes.at(i), 8);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -574,7 +574,7 @@ public final class Encoder {
|
||||||
throw new WriterException("Invalid byte sequence: " + bytes);
|
throw new WriterException("Invalid byte sequence: " + bytes);
|
||||||
}
|
}
|
||||||
final int encoded = ((subtracted >> 8) * 0xc0) + (subtracted & 0xff);
|
final int encoded = ((subtracted >> 8) * 0xc0) + (subtracted & 0xff);
|
||||||
bits.AppendBits(encoded, 13);
|
bits.appendBits(encoded, 13);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -326,14 +326,14 @@ public final class MatrixUtil {
|
||||||
throw new WriterException("Invalid mask pattern");
|
throw new WriterException("Invalid mask pattern");
|
||||||
}
|
}
|
||||||
final int type_info = (ec_code << 3) | mask_pattern;
|
final int type_info = (ec_code << 3) | mask_pattern;
|
||||||
bits.AppendBits(type_info, 5);
|
bits.appendBits(type_info, 5);
|
||||||
|
|
||||||
final int bch_code = MatrixUtil.CalculateBCHCode(type_info, kTypeInfoPoly);
|
final int bch_code = MatrixUtil.CalculateBCHCode(type_info, kTypeInfoPoly);
|
||||||
bits.AppendBits(bch_code, 10);
|
bits.appendBits(bch_code, 10);
|
||||||
|
|
||||||
BitVector mask_bits = new BitVector();
|
BitVector mask_bits = new BitVector();
|
||||||
mask_bits.AppendBits(kTypeInfoMaskPattern, 15);
|
mask_bits.appendBits(kTypeInfoMaskPattern, 15);
|
||||||
bits.XOR(mask_bits);
|
bits.xor(mask_bits);
|
||||||
|
|
||||||
if (bits.size() != 15) { // Just in case.
|
if (bits.size() != 15) { // Just in case.
|
||||||
throw new WriterException("should not happen but we got: " + bits.size());
|
throw new WriterException("should not happen but we got: " + bits.size());
|
||||||
|
@ -343,9 +343,9 @@ public final class MatrixUtil {
|
||||||
// Make bit vector of version information. On success, store the result in "bits" and return true.
|
// Make bit vector of version information. On success, store the result in "bits" and return true.
|
||||||
// On error, return false. See 8.10 of JISX0510:2004 (p.45) for details.
|
// On error, return false. See 8.10 of JISX0510:2004 (p.45) for details.
|
||||||
public static void MakeVersionInfoBits(int version, BitVector bits) throws WriterException {
|
public static void MakeVersionInfoBits(int version, BitVector bits) throws WriterException {
|
||||||
bits.AppendBits(version, 6);
|
bits.appendBits(version, 6);
|
||||||
final int bch_code = MatrixUtil.CalculateBCHCode(version, kVersionInfoPoly);
|
final int bch_code = MatrixUtil.CalculateBCHCode(version, kVersionInfoPoly);
|
||||||
bits.AppendBits(bch_code, 12);
|
bits.appendBits(bch_code, 12);
|
||||||
|
|
||||||
if (bits.size() != 18) { // Just in case.
|
if (bits.size() != 18) { // Just in case.
|
||||||
throw new WriterException("should not happen but we got: " + bits.size());
|
throw new WriterException("should not happen but we got: " + bits.size());
|
||||||
|
|
|
@ -30,46 +30,46 @@ public class BitVectorTestCase extends TestCase {
|
||||||
|
|
||||||
public void testAppendBit() {
|
public void testAppendBit() {
|
||||||
BitVector v = new BitVector();
|
BitVector v = new BitVector();
|
||||||
assertEquals(0, v.num_bytes());
|
assertEquals(0, v.sizeInBytes());
|
||||||
// 1
|
// 1
|
||||||
v.AppendBit(1);
|
v.appendBit(1);
|
||||||
assertEquals(1, v.size());
|
assertEquals(1, v.size());
|
||||||
assertEquals(0x80, getUnsignedByte(v, 0));
|
assertEquals(0x80, getUnsignedByte(v, 0));
|
||||||
// 10
|
// 10
|
||||||
v.AppendBit(0);
|
v.appendBit(0);
|
||||||
assertEquals(2, v.size());
|
assertEquals(2, v.size());
|
||||||
assertEquals(0x80, getUnsignedByte(v, 0));
|
assertEquals(0x80, getUnsignedByte(v, 0));
|
||||||
// 101
|
// 101
|
||||||
v.AppendBit(1);
|
v.appendBit(1);
|
||||||
assertEquals(3, v.size());
|
assertEquals(3, v.size());
|
||||||
assertEquals(0xa0, getUnsignedByte(v, 0));
|
assertEquals(0xa0, getUnsignedByte(v, 0));
|
||||||
// 1010
|
// 1010
|
||||||
v.AppendBit(0);
|
v.appendBit(0);
|
||||||
assertEquals(4, v.size());
|
assertEquals(4, v.size());
|
||||||
assertEquals(0xa0, getUnsignedByte(v, 0));
|
assertEquals(0xa0, getUnsignedByte(v, 0));
|
||||||
// 10101
|
// 10101
|
||||||
v.AppendBit(1);
|
v.appendBit(1);
|
||||||
assertEquals(5, v.size());
|
assertEquals(5, v.size());
|
||||||
assertEquals(0xa8, getUnsignedByte(v, 0));
|
assertEquals(0xa8, getUnsignedByte(v, 0));
|
||||||
// 101010
|
// 101010
|
||||||
v.AppendBit(0);
|
v.appendBit(0);
|
||||||
assertEquals(6, v.size());
|
assertEquals(6, v.size());
|
||||||
assertEquals(0xa8, getUnsignedByte(v, 0));
|
assertEquals(0xa8, getUnsignedByte(v, 0));
|
||||||
// 1010101
|
// 1010101
|
||||||
v.AppendBit(1);
|
v.appendBit(1);
|
||||||
assertEquals(7, v.size());
|
assertEquals(7, v.size());
|
||||||
assertEquals(0xaa, getUnsignedByte(v, 0));
|
assertEquals(0xaa, getUnsignedByte(v, 0));
|
||||||
// 10101010
|
// 10101010
|
||||||
v.AppendBit(0);
|
v.appendBit(0);
|
||||||
assertEquals(8, v.size());
|
assertEquals(8, v.size());
|
||||||
assertEquals(0xaa, getUnsignedByte(v, 0));
|
assertEquals(0xaa, getUnsignedByte(v, 0));
|
||||||
// 10101010 1
|
// 10101010 1
|
||||||
v.AppendBit(1);
|
v.appendBit(1);
|
||||||
assertEquals(9, v.size());
|
assertEquals(9, v.size());
|
||||||
assertEquals(0xaa, getUnsignedByte(v, 0));
|
assertEquals(0xaa, getUnsignedByte(v, 0));
|
||||||
assertEquals(0x80, getUnsignedByte(v, 1));
|
assertEquals(0x80, getUnsignedByte(v, 1));
|
||||||
// 10101010 10
|
// 10101010 10
|
||||||
v.AppendBit(0);
|
v.appendBit(0);
|
||||||
assertEquals(10, v.size());
|
assertEquals(10, v.size());
|
||||||
assertEquals(0xaa, getUnsignedByte(v, 0));
|
assertEquals(0xaa, getUnsignedByte(v, 0));
|
||||||
assertEquals(0x80, getUnsignedByte(v, 1));
|
assertEquals(0x80, getUnsignedByte(v, 1));
|
||||||
|
@ -78,19 +78,19 @@ public class BitVectorTestCase extends TestCase {
|
||||||
public void testAppendBits() {
|
public void testAppendBits() {
|
||||||
{
|
{
|
||||||
BitVector v = new BitVector();
|
BitVector v = new BitVector();
|
||||||
v.AppendBits(0x1, 1);
|
v.appendBits(0x1, 1);
|
||||||
assertEquals(1, v.size());
|
assertEquals(1, v.size());
|
||||||
assertEquals(0x80, getUnsignedByte(v, 0));
|
assertEquals(0x80, getUnsignedByte(v, 0));
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
BitVector v = new BitVector();
|
BitVector v = new BitVector();
|
||||||
v.AppendBits(0xff, 8);
|
v.appendBits(0xff, 8);
|
||||||
assertEquals(8, v.size());
|
assertEquals(8, v.size());
|
||||||
assertEquals(0xff, getUnsignedByte(v, 0));
|
assertEquals(0xff, getUnsignedByte(v, 0));
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
BitVector v = new BitVector();
|
BitVector v = new BitVector();
|
||||||
v.AppendBits(0xff7, 12);
|
v.appendBits(0xff7, 12);
|
||||||
assertEquals(12, v.size());
|
assertEquals(12, v.size());
|
||||||
assertEquals(0xff, getUnsignedByte(v, 0));
|
assertEquals(0xff, getUnsignedByte(v, 0));
|
||||||
assertEquals(0x70, getUnsignedByte(v, 1));
|
assertEquals(0x70, getUnsignedByte(v, 1));
|
||||||
|
@ -99,25 +99,25 @@ public class BitVectorTestCase extends TestCase {
|
||||||
|
|
||||||
public void testNumBytes() {
|
public void testNumBytes() {
|
||||||
BitVector v = new BitVector();
|
BitVector v = new BitVector();
|
||||||
assertEquals(0, v.num_bytes());
|
assertEquals(0, v.sizeInBytes());
|
||||||
v.AppendBit(0);
|
v.appendBit(0);
|
||||||
// 1 bit was added in the vector, so 1 byte should be consumed.
|
// 1 bit was added in the vector, so 1 byte should be consumed.
|
||||||
assertEquals(1, v.num_bytes());
|
assertEquals(1, v.sizeInBytes());
|
||||||
v.AppendBits(0, 7);
|
v.appendBits(0, 7);
|
||||||
assertEquals(1, v.num_bytes());
|
assertEquals(1, v.sizeInBytes());
|
||||||
v.AppendBits(0, 8);
|
v.appendBits(0, 8);
|
||||||
assertEquals(2, v.num_bytes());
|
assertEquals(2, v.sizeInBytes());
|
||||||
v.AppendBits(0, 1);
|
v.appendBits(0, 1);
|
||||||
// We now have 17 bits, so 3 bytes should be consumed.
|
// We now have 17 bits, so 3 bytes should be consumed.
|
||||||
assertEquals(3, v.num_bytes());
|
assertEquals(3, v.sizeInBytes());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testAppendBitVector() {
|
public void testAppendBitVector() {
|
||||||
BitVector v1 = new BitVector();
|
BitVector v1 = new BitVector();
|
||||||
v1.AppendBits(0xbe, 8);
|
v1.appendBits(0xbe, 8);
|
||||||
BitVector v2 = new BitVector();
|
BitVector v2 = new BitVector();
|
||||||
v2.AppendBits(0xef, 8);
|
v2.appendBits(0xef, 8);
|
||||||
v1.AppendBitVector(v2);
|
v1.appendBitVector(v2);
|
||||||
// beef = 1011 1110 1110 1111
|
// beef = 1011 1110 1110 1111
|
||||||
assertEquals("1011111011101111", v1.toString());
|
assertEquals("1011111011101111", v1.toString());
|
||||||
}
|
}
|
||||||
|
@ -125,10 +125,10 @@ public class BitVectorTestCase extends TestCase {
|
||||||
public void testXOR() {
|
public void testXOR() {
|
||||||
{
|
{
|
||||||
BitVector v1 = new BitVector();
|
BitVector v1 = new BitVector();
|
||||||
v1.AppendBits(0x5555aaaa, 32);
|
v1.appendBits(0x5555aaaa, 32);
|
||||||
BitVector v2 = new BitVector();
|
BitVector v2 = new BitVector();
|
||||||
v2.AppendBits(0xaaaa5555, 32);
|
v2.appendBits(0xaaaa5555, 32);
|
||||||
v1.XOR(v2);
|
v1.xor(v2);
|
||||||
assertEquals(0xff, getUnsignedByte(v1, 0));
|
assertEquals(0xff, getUnsignedByte(v1, 0));
|
||||||
assertEquals(0xff, getUnsignedByte(v1, 1));
|
assertEquals(0xff, getUnsignedByte(v1, 1));
|
||||||
assertEquals(0xff, getUnsignedByte(v1, 2));
|
assertEquals(0xff, getUnsignedByte(v1, 2));
|
||||||
|
@ -136,17 +136,17 @@ public class BitVectorTestCase extends TestCase {
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
BitVector v1 = new BitVector();
|
BitVector v1 = new BitVector();
|
||||||
v1.AppendBits(0x2a, 7); // 010 1010
|
v1.appendBits(0x2a, 7); // 010 1010
|
||||||
BitVector v2 = new BitVector();
|
BitVector v2 = new BitVector();
|
||||||
v2.AppendBits(0x55, 7); // 101 0101
|
v2.appendBits(0x55, 7); // 101 0101
|
||||||
v1.XOR(v2);
|
v1.xor(v2);
|
||||||
assertEquals(0xfe, getUnsignedByte(v1, 0)); // 1111 1110
|
assertEquals(0xfe, getUnsignedByte(v1, 0)); // 1111 1110
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testAt() {
|
public void testAt() {
|
||||||
BitVector v = new BitVector();
|
BitVector v = new BitVector();
|
||||||
v.AppendBits(0xdead, 16); // 1101 1110 1010 1101
|
v.appendBits(0xdead, 16); // 1101 1110 1010 1101
|
||||||
assertEquals(1, v.at(0));
|
assertEquals(1, v.at(0));
|
||||||
assertEquals(1, v.at(1));
|
assertEquals(1, v.at(1));
|
||||||
assertEquals(0, v.at(2));
|
assertEquals(0, v.at(2));
|
||||||
|
@ -170,7 +170,7 @@ public class BitVectorTestCase extends TestCase {
|
||||||
|
|
||||||
public void testToString() {
|
public void testToString() {
|
||||||
BitVector v = new BitVector();
|
BitVector v = new BitVector();
|
||||||
v.AppendBits(0xdead, 16); // 1101 1110 1010 1101
|
v.appendBits(0xdead, 16); // 1101 1110 1010 1101
|
||||||
assertEquals("1101111010101101", v.toString());
|
assertEquals("1101111010101101", v.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -237,19 +237,19 @@ public final class EncoderTestCase extends TestCase {
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
BitVector v = new BitVector();
|
BitVector v = new BitVector();
|
||||||
v.AppendBits(0, 3); // Append 000
|
v.appendBits(0, 3); // Append 000
|
||||||
Encoder.TerminateBits(1, v);
|
Encoder.TerminateBits(1, v);
|
||||||
assertEquals("00000000", v.toString());
|
assertEquals("00000000", v.toString());
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
BitVector v = new BitVector();
|
BitVector v = new BitVector();
|
||||||
v.AppendBits(0, 5); // Append 00000
|
v.appendBits(0, 5); // Append 00000
|
||||||
Encoder.TerminateBits(1, v);
|
Encoder.TerminateBits(1, v);
|
||||||
assertEquals("00000000", v.toString());
|
assertEquals("00000000", v.toString());
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
BitVector v = new BitVector();
|
BitVector v = new BitVector();
|
||||||
v.AppendBits(0, 8); // Append 00000000
|
v.appendBits(0, 8); // Append 00000000
|
||||||
Encoder.TerminateBits(1, v);
|
Encoder.TerminateBits(1, v);
|
||||||
assertEquals("00000000", v.toString());
|
assertEquals("00000000", v.toString());
|
||||||
}
|
}
|
||||||
|
@ -260,7 +260,7 @@ public final class EncoderTestCase extends TestCase {
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
BitVector v = new BitVector();
|
BitVector v = new BitVector();
|
||||||
v.AppendBits(0, 1); // Append 0
|
v.appendBits(0, 1); // Append 0
|
||||||
Encoder.TerminateBits(3, v);
|
Encoder.TerminateBits(3, v);
|
||||||
assertEquals("000000001110110000010001", v.toString());
|
assertEquals("000000001110110000010001", v.toString());
|
||||||
}
|
}
|
||||||
|
@ -307,7 +307,7 @@ public final class EncoderTestCase extends TestCase {
|
||||||
final byte[] data_bytes = {32, 65, (byte)205, 69, 41, (byte)220, 46, (byte)128, (byte)236};
|
final byte[] data_bytes = {32, 65, (byte)205, 69, 41, (byte)220, 46, (byte)128, (byte)236};
|
||||||
BitVector in = new BitVector();
|
BitVector in = new BitVector();
|
||||||
for (byte data_byte: data_bytes) {
|
for (byte data_byte: data_bytes) {
|
||||||
in.AppendBits(data_byte, 8);
|
in.appendBits(data_byte, 8);
|
||||||
}
|
}
|
||||||
BitVector out = new BitVector();
|
BitVector out = new BitVector();
|
||||||
Encoder.InterleaveWithECBytes(in, 26, 9, 1, out);
|
Encoder.InterleaveWithECBytes(in, 26, 9, 1, out);
|
||||||
|
@ -318,9 +318,9 @@ public final class EncoderTestCase extends TestCase {
|
||||||
42, (byte)159, 74, (byte)221, (byte)244, (byte)169, (byte)239, (byte)150, (byte)138, 70,
|
42, (byte)159, 74, (byte)221, (byte)244, (byte)169, (byte)239, (byte)150, (byte)138, 70,
|
||||||
(byte)237, 85, (byte)224, 96, 74, (byte)219, 61,
|
(byte)237, 85, (byte)224, 96, 74, (byte)219, 61,
|
||||||
};
|
};
|
||||||
assertEquals(expected.length, out.num_bytes());
|
assertEquals(expected.length, out.sizeInBytes());
|
||||||
final byte[] out_array = out.getArray();
|
final byte[] out_array = out.getArray();
|
||||||
// Can't use Arrays.equals(), because out_array may be longer than out.num_bytes()
|
// Can't use Arrays.equals(), because out_array may be longer than out.sizeInBytes()
|
||||||
for (int x = 0; x < expected.length; x++) {
|
for (int x = 0; x < expected.length; x++) {
|
||||||
assertEquals(expected[x], out_array[x]);
|
assertEquals(expected[x], out_array[x]);
|
||||||
}
|
}
|
||||||
|
@ -337,7 +337,7 @@ public final class EncoderTestCase extends TestCase {
|
||||||
};
|
};
|
||||||
BitVector in = new BitVector();
|
BitVector in = new BitVector();
|
||||||
for (byte data_byte: data_bytes) {
|
for (byte data_byte: data_bytes) {
|
||||||
in.AppendBits(data_byte, 8);
|
in.appendBits(data_byte, 8);
|
||||||
}
|
}
|
||||||
BitVector out = new BitVector();
|
BitVector out = new BitVector();
|
||||||
Encoder.InterleaveWithECBytes(in, 134, 62, 4, out);
|
Encoder.InterleaveWithECBytes(in, 134, 62, 4, out);
|
||||||
|
@ -358,7 +358,7 @@ public final class EncoderTestCase extends TestCase {
|
||||||
(byte)140, 61, (byte)179, (byte)154, (byte)214, (byte)138, (byte)147, 87, 27, 96, 77, 47,
|
(byte)140, 61, (byte)179, (byte)154, (byte)214, (byte)138, (byte)147, 87, 27, 96, 77, 47,
|
||||||
(byte)187, 49, (byte)156, (byte)214,
|
(byte)187, 49, (byte)156, (byte)214,
|
||||||
};
|
};
|
||||||
assertEquals(expected.length, out.num_bytes());
|
assertEquals(expected.length, out.sizeInBytes());
|
||||||
final byte[] out_array = out.getArray();
|
final byte[] out_array = out.getArray();
|
||||||
for (int x = 0; x < expected.length; x++) {
|
for (int x = 0; x < expected.length; x++) {
|
||||||
assertEquals(expected[x], out_array[x]);
|
assertEquals(expected[x], out_array[x]);
|
||||||
|
@ -609,10 +609,10 @@ public final class EncoderTestCase extends TestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testBugInBitVectorNumBytes() throws WriterException {
|
public void testBugInBitVectorNumBytes() throws WriterException {
|
||||||
// There was a bug in BitVector::num_bytes() that caused it to return a
|
// There was a bug in BitVector::sizeInBytes() that caused it to return a
|
||||||
// smaller-by-one value (ex. 1465 instead of 1466) if the number of bits
|
// smaller-by-one value (ex. 1465 instead of 1466) if the number of bits
|
||||||
// in the vector is not 8-bit aligned. In QRCodeEncoder::InitQRCode(),
|
// in the vector is not 8-bit aligned. In QRCodeEncoder::InitQRCode(),
|
||||||
// BitVector::num_bytes() is used for finding the smallest QR Code
|
// BitVector::sizeInBytes() is used for finding the smallest QR Code
|
||||||
// version that can fit the given data. Hence there were corner cases
|
// version that can fit the given data. Hence there were corner cases
|
||||||
// where we chose a wrong QR Code version that cannot fit the given
|
// where we chose a wrong QR Code version that cannot fit the given
|
||||||
// data. Note that the issue did not occur with MODE_8BIT_BYTE, as the
|
// data. Note that the issue did not occur with MODE_8BIT_BYTE, as the
|
||||||
|
@ -631,7 +631,7 @@ public final class EncoderTestCase extends TestCase {
|
||||||
// - 1828 - 360 = 1468
|
// - 1828 - 360 = 1468
|
||||||
// - In InitQRCode(), 3 bytes are reserved for a header. Hence 1465 bytes
|
// - In InitQRCode(), 3 bytes are reserved for a header. Hence 1465 bytes
|
||||||
// (1468 -3) are left for data.
|
// (1468 -3) are left for data.
|
||||||
// - Because of the bug in BitVector::num_bytes(), InitQRCode() determines
|
// - Because of the bug in BitVector::sizeInBytes(), InitQRCode() determines
|
||||||
// the given data can fit in 1465 bytes, despite it needs 1466 bytes.
|
// the given data can fit in 1465 bytes, despite it needs 1466 bytes.
|
||||||
// - Hence QRCodeEncoder::Encode() failed and returned false.
|
// - Hence QRCodeEncoder::Encode() failed and returned false.
|
||||||
// - To be precise, it needs 11727 + 4 (mode info) + 14 (length info) =
|
// - To be precise, it needs 11727 + 4 (mode info) + 14 (length info) =
|
||||||
|
|
|
@ -238,7 +238,7 @@ public final class MatrixUtilTestCase extends TestCase {
|
||||||
70, 237, 85, 224, 96, 74, 219 , 61};
|
70, 237, 85, 224, 96, 74, 219 , 61};
|
||||||
BitVector bits = new BitVector();
|
BitVector bits = new BitVector();
|
||||||
for (char c: bytes) {
|
for (char c: bytes) {
|
||||||
bits.AppendBits(c, 8);
|
bits.appendBits(c, 8);
|
||||||
}
|
}
|
||||||
ByteMatrix matrix = new ByteMatrix(21, 21);
|
ByteMatrix matrix = new ByteMatrix(21, 21);
|
||||||
MatrixUtil.BuildMatrix(bits,
|
MatrixUtil.BuildMatrix(bits,
|
||||||
|
|
Loading…
Reference in a new issue