mirror of
https://github.com/starovoid/rxing.git
synced 2026-07-26 12:22:34 +00:00
enum exceptions
This commit is contained in:
258
src/common/BitArrayTestCase.java
Normal file
258
src/common/BitArrayTestCase.java
Normal file
@@ -0,0 +1,258 @@
|
||||
/*
|
||||
* Copyright 2007 ZXing authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.google.zxing.common;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* @author Sean Owen
|
||||
*/
|
||||
public final class BitArrayTestCase extends Assert {
|
||||
|
||||
@Test
|
||||
public void testGetSet() {
|
||||
BitArray array = new BitArray(33);
|
||||
for (int i = 0; i < 33; i++) {
|
||||
assertFalse(array.get(i));
|
||||
array.set(i);
|
||||
assertTrue(array.get(i));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNextSet1() {
|
||||
BitArray array = new BitArray(32);
|
||||
for (int i = 0; i < array.getSize(); i++) {
|
||||
assertEquals(String.valueOf(i), 32, array.getNextSet(i));
|
||||
}
|
||||
array = new BitArray(33);
|
||||
for (int i = 0; i < array.getSize(); i++) {
|
||||
assertEquals(String.valueOf(i), 33, array.getNextSet(i));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNextSet2() {
|
||||
BitArray array = new BitArray(33);
|
||||
array.set(31);
|
||||
for (int i = 0; i < array.getSize(); i++) {
|
||||
assertEquals(String.valueOf(i), i <= 31 ? 31 : 33, array.getNextSet(i));
|
||||
}
|
||||
array = new BitArray(33);
|
||||
array.set(32);
|
||||
for (int i = 0; i < array.getSize(); i++) {
|
||||
assertEquals(String.valueOf(i), 32, array.getNextSet(i));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNextSet3() {
|
||||
BitArray array = new BitArray(63);
|
||||
array.set(31);
|
||||
array.set(32);
|
||||
for (int i = 0; i < array.getSize(); i++) {
|
||||
int expected;
|
||||
if (i <= 31) {
|
||||
expected = 31;
|
||||
} else if (i == 32) {
|
||||
expected = 32;
|
||||
} else {
|
||||
expected = 63;
|
||||
}
|
||||
assertEquals(String.valueOf(i), expected, array.getNextSet(i));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNextSet4() {
|
||||
BitArray array = new BitArray(63);
|
||||
array.set(33);
|
||||
array.set(40);
|
||||
for (int i = 0; i < array.getSize(); i++) {
|
||||
int expected;
|
||||
if (i <= 33) {
|
||||
expected = 33;
|
||||
} else if (i <= 40) {
|
||||
expected = 40;
|
||||
} else {
|
||||
expected = 63;
|
||||
}
|
||||
assertEquals(String.valueOf(i), expected, array.getNextSet(i));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNextSet5() {
|
||||
Random r = new Random(0xDEADBEEF);
|
||||
for (int i = 0; i < 10; i++) {
|
||||
BitArray array = new BitArray(1 + r.nextInt(100));
|
||||
int numSet = r.nextInt(20);
|
||||
for (int j = 0; j < numSet; j++) {
|
||||
array.set(r.nextInt(array.getSize()));
|
||||
}
|
||||
int numQueries = r.nextInt(20);
|
||||
for (int j = 0; j < numQueries; j++) {
|
||||
int query = r.nextInt(array.getSize());
|
||||
int expected = query;
|
||||
while (expected < array.getSize() && !array.get(expected)) {
|
||||
expected++;
|
||||
}
|
||||
int actual = array.getNextSet(query);
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testSetBulk() {
|
||||
BitArray array = new BitArray(64);
|
||||
array.setBulk(32, 0xFFFF0000);
|
||||
for (int i = 0; i < 48; i++) {
|
||||
assertFalse(array.get(i));
|
||||
}
|
||||
for (int i = 48; i < 64; i++) {
|
||||
assertTrue(array.get(i));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetRange() {
|
||||
BitArray array = new BitArray(64);
|
||||
array.setRange(28, 36);
|
||||
assertFalse(array.get(27));
|
||||
for (int i = 28; i < 36; i++) {
|
||||
assertTrue(array.get(i));
|
||||
}
|
||||
assertFalse(array.get(36));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClear() {
|
||||
BitArray array = new BitArray(32);
|
||||
for (int i = 0; i < 32; i++) {
|
||||
array.set(i);
|
||||
}
|
||||
array.clear();
|
||||
for (int i = 0; i < 32; i++) {
|
||||
assertFalse(array.get(i));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFlip() {
|
||||
BitArray array = new BitArray(32);
|
||||
assertFalse(array.get(5));
|
||||
array.flip(5);
|
||||
assertTrue(array.get(5));
|
||||
array.flip(5);
|
||||
assertFalse(array.get(5));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetArray() {
|
||||
BitArray array = new BitArray(64);
|
||||
array.set(0);
|
||||
array.set(63);
|
||||
int[] ints = array.getBitArray();
|
||||
assertEquals(1, ints[0]);
|
||||
assertEquals(Integer.MIN_VALUE, ints[1]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsRange() {
|
||||
BitArray array = new BitArray(64);
|
||||
assertTrue(array.isRange(0, 64, false));
|
||||
assertFalse(array.isRange(0, 64, true));
|
||||
array.set(32);
|
||||
assertTrue(array.isRange(32, 33, true));
|
||||
array.set(31);
|
||||
assertTrue(array.isRange(31, 33, true));
|
||||
array.set(34);
|
||||
assertFalse(array.isRange(31, 35, true));
|
||||
for (int i = 0; i < 31; i++) {
|
||||
array.set(i);
|
||||
}
|
||||
assertTrue(array.isRange(0, 33, true));
|
||||
for (int i = 33; i < 64; i++) {
|
||||
array.set(i);
|
||||
}
|
||||
assertTrue(array.isRange(0, 64, true));
|
||||
assertFalse(array.isRange(0, 64, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void reverseAlgorithmTest() {
|
||||
int[] oldBits = {128, 256, 512, 6453324, 50934953};
|
||||
for (int size = 1; size < 160; size++) {
|
||||
int[] newBitsOriginal = reverseOriginal(oldBits.clone(), size);
|
||||
BitArray newBitArray = new BitArray(oldBits.clone(), size);
|
||||
newBitArray.reverse();
|
||||
int[] newBitsNew = newBitArray.getBitArray();
|
||||
assertTrue(arraysAreEqual(newBitsOriginal, newBitsNew, size / 32 + 1));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClone() {
|
||||
BitArray array = new BitArray(32);
|
||||
array.clone().set(0);
|
||||
assertFalse(array.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEquals() {
|
||||
BitArray a = new BitArray(32);
|
||||
BitArray b = new BitArray(32);
|
||||
assertEquals(a, b);
|
||||
assertEquals(a.hashCode(), b.hashCode());
|
||||
assertNotEquals(a, new BitArray(31));
|
||||
a.set(16);
|
||||
assertNotEquals(a, b);
|
||||
assertNotEquals(a.hashCode(), b.hashCode());
|
||||
b.set(16);
|
||||
assertEquals(a, b);
|
||||
assertEquals(a.hashCode(), b.hashCode());
|
||||
}
|
||||
|
||||
private static int[] reverseOriginal(int[] oldBits, int size) {
|
||||
int[] newBits = new int[oldBits.length];
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (bitSet(oldBits, size - i - 1)) {
|
||||
newBits[i / 32] |= 1 << (i & 0x1F);
|
||||
}
|
||||
}
|
||||
return newBits;
|
||||
}
|
||||
|
||||
private static boolean bitSet(int[] bits, int i) {
|
||||
return (bits[i / 32] & (1 << (i & 0x1F))) != 0;
|
||||
}
|
||||
|
||||
private static boolean arraysAreEqual(int[] left, int[] right, int size) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (left[i] != right[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
347
src/common/BitMatrixTestCase.java
Normal file
347
src/common/BitMatrixTestCase.java
Normal file
@@ -0,0 +1,347 @@
|
||||
/*
|
||||
* Copyright 2007 ZXing authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.google.zxing.common;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* @author Sean Owen
|
||||
* @author dswitkin@google.com (Daniel Switkin)
|
||||
*/
|
||||
public final class BitMatrixTestCase extends Assert {
|
||||
|
||||
private static final int[] BIT_MATRIX_POINTS = { 1, 2, 2, 0, 3, 1 };
|
||||
|
||||
@Test
|
||||
public void testGetSet() {
|
||||
BitMatrix matrix = new BitMatrix(33);
|
||||
assertEquals(33, matrix.getHeight());
|
||||
for (int y = 0; y < 33; y++) {
|
||||
for (int x = 0; x < 33; x++) {
|
||||
if (y * x % 3 == 0) {
|
||||
matrix.set(x, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int y = 0; y < 33; y++) {
|
||||
for (int x = 0; x < 33; x++) {
|
||||
assertEquals(y * x % 3 == 0, matrix.get(x, y));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetRegion() {
|
||||
BitMatrix matrix = new BitMatrix(5);
|
||||
matrix.setRegion(1, 1, 3, 3);
|
||||
for (int y = 0; y < 5; y++) {
|
||||
for (int x = 0; x < 5; x++) {
|
||||
assertEquals(y >= 1 && y <= 3 && x >= 1 && x <= 3, matrix.get(x, y));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEnclosing() {
|
||||
BitMatrix matrix = new BitMatrix(5);
|
||||
assertNull(matrix.getEnclosingRectangle());
|
||||
matrix.setRegion(1, 1, 1, 1);
|
||||
assertArrayEquals(new int[] { 1, 1, 1, 1 }, matrix.getEnclosingRectangle());
|
||||
matrix.setRegion(1, 1, 3, 2);
|
||||
assertArrayEquals(new int[] { 1, 1, 3, 2 }, matrix.getEnclosingRectangle());
|
||||
matrix.setRegion(0, 0, 5, 5);
|
||||
assertArrayEquals(new int[] { 0, 0, 5, 5 }, matrix.getEnclosingRectangle());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnBit() {
|
||||
BitMatrix matrix = new BitMatrix(5);
|
||||
assertNull(matrix.getTopLeftOnBit());
|
||||
assertNull(matrix.getBottomRightOnBit());
|
||||
matrix.setRegion(1, 1, 1, 1);
|
||||
assertArrayEquals(new int[] { 1, 1 }, matrix.getTopLeftOnBit());
|
||||
assertArrayEquals(new int[] { 1, 1 }, matrix.getBottomRightOnBit());
|
||||
matrix.setRegion(1, 1, 3, 2);
|
||||
assertArrayEquals(new int[] { 1, 1 }, matrix.getTopLeftOnBit());
|
||||
assertArrayEquals(new int[] { 3, 2 }, matrix.getBottomRightOnBit());
|
||||
matrix.setRegion(0, 0, 5, 5);
|
||||
assertArrayEquals(new int[] { 0, 0 }, matrix.getTopLeftOnBit());
|
||||
assertArrayEquals(new int[] { 4, 4 }, matrix.getBottomRightOnBit());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRectangularMatrix() {
|
||||
BitMatrix matrix = new BitMatrix(75, 20);
|
||||
assertEquals(75, matrix.getWidth());
|
||||
assertEquals(20, matrix.getHeight());
|
||||
matrix.set(10, 0);
|
||||
matrix.set(11, 1);
|
||||
matrix.set(50, 2);
|
||||
matrix.set(51, 3);
|
||||
matrix.flip(74, 4);
|
||||
matrix.flip(0, 5);
|
||||
|
||||
// Should all be on
|
||||
assertTrue(matrix.get(10, 0));
|
||||
assertTrue(matrix.get(11, 1));
|
||||
assertTrue(matrix.get(50, 2));
|
||||
assertTrue(matrix.get(51, 3));
|
||||
assertTrue(matrix.get(74, 4));
|
||||
assertTrue(matrix.get(0, 5));
|
||||
|
||||
// Flip a couple back off
|
||||
matrix.flip(50, 2);
|
||||
matrix.flip(51, 3);
|
||||
assertFalse(matrix.get(50, 2));
|
||||
assertFalse(matrix.get(51, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRectangularSetRegion() {
|
||||
BitMatrix matrix = new BitMatrix(320, 240);
|
||||
assertEquals(320, matrix.getWidth());
|
||||
assertEquals(240, matrix.getHeight());
|
||||
matrix.setRegion(105, 22, 80, 12);
|
||||
|
||||
// Only bits in the region should be on
|
||||
for (int y = 0; y < 240; y++) {
|
||||
for (int x = 0; x < 320; x++) {
|
||||
assertEquals(y >= 22 && y < 34 && x >= 105 && x < 185, matrix.get(x, y));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetRow() {
|
||||
BitMatrix matrix = new BitMatrix(102, 5);
|
||||
for (int x = 0; x < 102; x++) {
|
||||
if ((x & 0x03) == 0) {
|
||||
matrix.set(x, 2);
|
||||
}
|
||||
}
|
||||
|
||||
// Should allocate
|
||||
BitArray array = matrix.getRow(2, null);
|
||||
assertEquals(102, array.getSize());
|
||||
|
||||
// Should reallocate
|
||||
BitArray array2 = new BitArray(60);
|
||||
array2 = matrix.getRow(2, array2);
|
||||
assertEquals(102, array2.getSize());
|
||||
|
||||
// Should use provided object, with original BitArray size
|
||||
BitArray array3 = new BitArray(200);
|
||||
array3 = matrix.getRow(2, array3);
|
||||
assertEquals(200, array3.getSize());
|
||||
|
||||
for (int x = 0; x < 102; x++) {
|
||||
boolean on = (x & 0x03) == 0;
|
||||
assertEquals(on, array.get(x));
|
||||
assertEquals(on, array2.get(x));
|
||||
assertEquals(on, array3.get(x));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRotate90Simple() {
|
||||
BitMatrix matrix = new BitMatrix(3, 3);
|
||||
matrix.set(0, 0);
|
||||
matrix.set(0, 1);
|
||||
matrix.set(1, 2);
|
||||
matrix.set(2, 1);
|
||||
|
||||
matrix.rotate90();
|
||||
|
||||
assertTrue(matrix.get(0, 2));
|
||||
assertTrue(matrix.get(1, 2));
|
||||
assertTrue(matrix.get(2, 1));
|
||||
assertTrue(matrix.get(1, 0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRotate180Simple() {
|
||||
BitMatrix matrix = new BitMatrix(3, 3);
|
||||
matrix.set(0, 0);
|
||||
matrix.set(0, 1);
|
||||
matrix.set(1, 2);
|
||||
matrix.set(2, 1);
|
||||
|
||||
matrix.rotate180();
|
||||
|
||||
assertTrue(matrix.get(2, 2));
|
||||
assertTrue(matrix.get(2, 1));
|
||||
assertTrue(matrix.get(1, 0));
|
||||
assertTrue(matrix.get(0, 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRotate180() {
|
||||
testRotate180(7, 4);
|
||||
testRotate180(7, 5);
|
||||
testRotate180(8, 4);
|
||||
testRotate180(8, 5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParse() {
|
||||
BitMatrix emptyMatrix = new BitMatrix(3, 3);
|
||||
BitMatrix fullMatrix = new BitMatrix(3, 3);
|
||||
fullMatrix.setRegion(0, 0, 3, 3);
|
||||
BitMatrix centerMatrix = new BitMatrix(3, 3);
|
||||
centerMatrix.setRegion(1, 1, 1, 1);
|
||||
BitMatrix emptyMatrix24 = new BitMatrix(2, 4);
|
||||
|
||||
assertEquals(emptyMatrix, BitMatrix.parse(" \n \n \n", "x", " "));
|
||||
assertEquals(emptyMatrix, BitMatrix.parse(" \n \r\r\n \n\r", "x", " "));
|
||||
assertEquals(emptyMatrix, BitMatrix.parse(" \n \n ", "x", " "));
|
||||
|
||||
assertEquals(fullMatrix, BitMatrix.parse("xxx\nxxx\nxxx\n", "x", " "));
|
||||
|
||||
assertEquals(centerMatrix, BitMatrix.parse(" \n x \n \n", "x", " "));
|
||||
assertEquals(centerMatrix, BitMatrix.parse(" \n x \n \n", "x ", " "));
|
||||
try {
|
||||
assertEquals(centerMatrix, BitMatrix.parse(" \n xy\n \n", "x", " "));
|
||||
fail();
|
||||
} catch (IllegalArgumentException ex) {
|
||||
// good
|
||||
}
|
||||
|
||||
assertEquals(emptyMatrix24, BitMatrix.parse(" \n \n \n \n", "x", " "));
|
||||
|
||||
assertEquals(centerMatrix, BitMatrix.parse(centerMatrix.toString("x", "."), "x", "."));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseBoolean() {
|
||||
BitMatrix emptyMatrix = new BitMatrix(3, 3);
|
||||
BitMatrix fullMatrix = new BitMatrix(3, 3);
|
||||
fullMatrix.setRegion(0, 0, 3, 3);
|
||||
BitMatrix centerMatrix = new BitMatrix(3, 3);
|
||||
centerMatrix.setRegion(1, 1, 1, 1);
|
||||
BitMatrix emptyMatrix24 = new BitMatrix(2, 4);
|
||||
|
||||
boolean[][] matrix = new boolean[3][3];
|
||||
assertEquals(emptyMatrix, BitMatrix.parse(matrix));
|
||||
matrix[1][1] = true;
|
||||
assertEquals(centerMatrix, BitMatrix.parse(matrix));
|
||||
for (boolean[] arr : matrix) {
|
||||
Arrays.fill(arr, true);
|
||||
}
|
||||
assertEquals(fullMatrix, BitMatrix.parse(matrix));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnset() {
|
||||
BitMatrix emptyMatrix = new BitMatrix(3, 3);
|
||||
BitMatrix matrix = emptyMatrix.clone();
|
||||
matrix.set(1, 1);
|
||||
assertNotEquals(emptyMatrix, matrix);
|
||||
matrix.unset(1, 1);
|
||||
assertEquals(emptyMatrix, matrix);
|
||||
matrix.unset(1, 1);
|
||||
assertEquals(emptyMatrix, matrix);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testXOR() {
|
||||
BitMatrix emptyMatrix = new BitMatrix(3, 3);
|
||||
BitMatrix fullMatrix = new BitMatrix(3, 3);
|
||||
fullMatrix.setRegion(0, 0, 3, 3);
|
||||
BitMatrix centerMatrix = new BitMatrix(3, 3);
|
||||
centerMatrix.setRegion(1, 1, 1, 1);
|
||||
BitMatrix invertedCenterMatrix = fullMatrix.clone();
|
||||
invertedCenterMatrix.unset(1, 1);
|
||||
BitMatrix badMatrix = new BitMatrix(4, 4);
|
||||
|
||||
testXOR(emptyMatrix, emptyMatrix, emptyMatrix);
|
||||
testXOR(emptyMatrix, centerMatrix, centerMatrix);
|
||||
testXOR(emptyMatrix, fullMatrix, fullMatrix);
|
||||
|
||||
testXOR(centerMatrix, emptyMatrix, centerMatrix);
|
||||
testXOR(centerMatrix, centerMatrix, emptyMatrix);
|
||||
testXOR(centerMatrix, fullMatrix, invertedCenterMatrix);
|
||||
|
||||
testXOR(invertedCenterMatrix, emptyMatrix, invertedCenterMatrix);
|
||||
testXOR(invertedCenterMatrix, centerMatrix, fullMatrix);
|
||||
testXOR(invertedCenterMatrix, fullMatrix, centerMatrix);
|
||||
|
||||
testXOR(fullMatrix, emptyMatrix, fullMatrix);
|
||||
testXOR(fullMatrix, centerMatrix, invertedCenterMatrix);
|
||||
testXOR(fullMatrix, fullMatrix, emptyMatrix);
|
||||
|
||||
try {
|
||||
emptyMatrix.clone().xor(badMatrix);
|
||||
fail();
|
||||
} catch (IllegalArgumentException ex) {
|
||||
// good
|
||||
}
|
||||
|
||||
try {
|
||||
badMatrix.clone().xor(emptyMatrix);
|
||||
fail();
|
||||
} catch (IllegalArgumentException ex) {
|
||||
// good
|
||||
}
|
||||
}
|
||||
|
||||
public static String matrixToString(BitMatrix result) {
|
||||
assertEquals(1, result.getHeight());
|
||||
StringBuilder builder = new StringBuilder(result.getWidth());
|
||||
for (int i = 0; i < result.getWidth(); i++) {
|
||||
builder.append(result.get(i, 0) ? '1' : '0');
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
private static void testXOR(BitMatrix dataMatrix, BitMatrix flipMatrix, BitMatrix expectedMatrix) {
|
||||
BitMatrix matrix = dataMatrix.clone();
|
||||
matrix.xor(flipMatrix);
|
||||
assertEquals(expectedMatrix, matrix);
|
||||
}
|
||||
|
||||
private static void testRotate180(int width, int height) {
|
||||
BitMatrix input = getInput(width, height);
|
||||
input.rotate180();
|
||||
BitMatrix expected = getExpected(width, height);
|
||||
|
||||
for (int y = 0; y < height; y++) {
|
||||
for (int x = 0; x < width; x++) {
|
||||
assertEquals("(" + x + ',' + y + ')', expected.get(x, y), input.get(x, y));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static BitMatrix getExpected(int width, int height) {
|
||||
BitMatrix result = new BitMatrix(width, height);
|
||||
for (int i = 0; i < BIT_MATRIX_POINTS.length; i += 2) {
|
||||
result.set(width - 1 - BIT_MATRIX_POINTS[i], height - 1 - BIT_MATRIX_POINTS[i + 1]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static BitMatrix getInput(int width, int height) {
|
||||
BitMatrix result = new BitMatrix(width, height);
|
||||
for (int i = 0; i < BIT_MATRIX_POINTS.length; i += 2) {
|
||||
result.set(BIT_MATRIX_POINTS[i], BIT_MATRIX_POINTS[i + 1]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
65
src/common/BitSourceBuilder.java
Normal file
65
src/common/BitSourceBuilder.java
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright 2008 ZXing authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.google.zxing.common;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
/**
|
||||
* Class that lets one easily build an array of bytes by appending bits at a time.
|
||||
*
|
||||
* @author Sean Owen
|
||||
*/
|
||||
public final class BitSourceBuilder {
|
||||
|
||||
private final ByteArrayOutputStream output;
|
||||
private int nextByte;
|
||||
private int bitsLeftInNextByte;
|
||||
|
||||
public BitSourceBuilder() {
|
||||
output = new ByteArrayOutputStream();
|
||||
nextByte = 0;
|
||||
bitsLeftInNextByte = 8;
|
||||
}
|
||||
|
||||
public void write(int value, int numBits) {
|
||||
if (numBits <= bitsLeftInNextByte) {
|
||||
nextByte <<= numBits;
|
||||
nextByte |= value;
|
||||
bitsLeftInNextByte -= numBits;
|
||||
if (bitsLeftInNextByte == 0) {
|
||||
output.write(nextByte);
|
||||
nextByte = 0;
|
||||
bitsLeftInNextByte = 8;
|
||||
}
|
||||
} else {
|
||||
int bitsToWriteNow = bitsLeftInNextByte;
|
||||
int numRestOfBits = numBits - bitsToWriteNow;
|
||||
int mask = 0xFF >> (8 - bitsToWriteNow);
|
||||
int valueToWriteNow = (value >>> numRestOfBits) & mask;
|
||||
write(valueToWriteNow, bitsToWriteNow);
|
||||
write(value, numRestOfBits);
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] toByteArray() {
|
||||
if (bitsLeftInNextByte < 8) {
|
||||
write(0, bitsLeftInNextByte);
|
||||
}
|
||||
return output.toByteArray();
|
||||
}
|
||||
|
||||
}
|
||||
117
src/common/StringUtilsTestCase.java
Normal file
117
src/common/StringUtilsTestCase.java
Normal file
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
* Copyright 2012 ZXing authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.google.zxing.common;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* Tests {@link StringUtils}.
|
||||
*/
|
||||
public final class StringUtilsTestCase extends Assert {
|
||||
|
||||
@Test
|
||||
public void testRandom() {
|
||||
Random r = new Random(1234L);
|
||||
byte[] bytes = new byte[1000];
|
||||
r.nextBytes(bytes);
|
||||
assertEquals(Charset.defaultCharset(), StringUtils.guessCharset(bytes, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShortShiftJIS1() {
|
||||
// 金魚
|
||||
doTest(new byte[] { (byte) 0x8b, (byte) 0xe0, (byte) 0x8b, (byte) 0x9b, }, StringUtils.SHIFT_JIS_CHARSET, "SJIS");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShortISO885911() {
|
||||
// båd
|
||||
doTest(new byte[] { (byte) 0x62, (byte) 0xe5, (byte) 0x64, }, StandardCharsets.ISO_8859_1, "ISO8859_1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShortUTF81() {
|
||||
// Español
|
||||
doTest(new byte[] { (byte) 0x45, (byte) 0x73, (byte) 0x70, (byte) 0x61, (byte) 0xc3,
|
||||
(byte) 0xb1, (byte) 0x6f, (byte) 0x6c },
|
||||
StandardCharsets.UTF_8, "UTF8");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMixedShiftJIS1() {
|
||||
// Hello 金!
|
||||
doTest(new byte[] { (byte) 0x48, (byte) 0x65, (byte) 0x6c, (byte) 0x6c, (byte) 0x6f,
|
||||
(byte) 0x20, (byte) 0x8b, (byte) 0xe0, (byte) 0x21, },
|
||||
StringUtils.SHIFT_JIS_CHARSET, "SJIS");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUTF16BE() {
|
||||
// 调压柜
|
||||
doTest(new byte[] { (byte) 0xFE, (byte) 0xFF, (byte) 0x8c, (byte) 0x03, (byte) 0x53, (byte) 0x8b,
|
||||
(byte) 0x67, (byte) 0xdc, },
|
||||
StandardCharsets.UTF_16,
|
||||
StandardCharsets.UTF_16.name());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUTF16LE() {
|
||||
// 调压柜
|
||||
doTest(new byte[] { (byte) 0xFF, (byte) 0xFE, (byte) 0x03, (byte) 0x8c, (byte) 0x8b, (byte) 0x53,
|
||||
(byte) 0xdc, (byte) 0x67, },
|
||||
StandardCharsets.UTF_16,
|
||||
StandardCharsets.UTF_16.name());
|
||||
}
|
||||
|
||||
private static void doTest(byte[] bytes, Charset charset, String encoding) {
|
||||
Charset guessedCharset = StringUtils.guessCharset(bytes, null);
|
||||
String guessedEncoding = StringUtils.guessEncoding(bytes, null);
|
||||
assertEquals(charset, guessedCharset);
|
||||
assertEquals(encoding, guessedEncoding);
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility for printing out a string in given encoding as a Java statement, since it's better
|
||||
* to write that into the Java source file rather than risk character encoding issues in the
|
||||
* source file itself.
|
||||
*
|
||||
* @param args command line arguments
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
String text = args[0];
|
||||
Charset charset = Charset.forName(args[1]);
|
||||
StringBuilder declaration = new StringBuilder();
|
||||
declaration.append("new byte[] { ");
|
||||
for (byte b : text.getBytes(charset)) {
|
||||
declaration.append("(byte) 0x");
|
||||
int value = b & 0xFF;
|
||||
if (value < 0x10) {
|
||||
declaration.append('0');
|
||||
}
|
||||
declaration.append(Integer.toHexString(value));
|
||||
declaration.append(", ");
|
||||
}
|
||||
declaration.append('}');
|
||||
System.out.println(declaration);
|
||||
}
|
||||
|
||||
}
|
||||
58
src/common/TestResult.java
Normal file
58
src/common/TestResult.java
Normal file
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright 2008 ZXing authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.google.zxing.common;
|
||||
|
||||
/**
|
||||
* Encapsulates the result of one test over a batch of black-box images.
|
||||
*/
|
||||
public final class TestRXingResult {
|
||||
|
||||
private final int mustPassCount;
|
||||
private final int tryHarderCount;
|
||||
private final int maxMisreads;
|
||||
private final int maxTryHarderMisreads;
|
||||
private final float rotation;
|
||||
|
||||
public TestRXingResult(int mustPassCount, int tryHarderCount, int maxMisreads, int maxTryHarderMisreads, float rotation) {
|
||||
this.mustPassCount = mustPassCount;
|
||||
this.tryHarderCount = tryHarderCount;
|
||||
this.maxMisreads = maxMisreads;
|
||||
this.maxTryHarderMisreads = maxTryHarderMisreads;
|
||||
this.rotation = rotation;
|
||||
}
|
||||
|
||||
public int getMustPassCount() {
|
||||
return mustPassCount;
|
||||
}
|
||||
|
||||
public int getTryHarderCount() {
|
||||
return tryHarderCount;
|
||||
}
|
||||
|
||||
public int getMaxMisreads() {
|
||||
return maxMisreads;
|
||||
}
|
||||
|
||||
public int getMaxTryHarderMisreads() {
|
||||
return maxTryHarderMisreads;
|
||||
}
|
||||
|
||||
public float getRotation() {
|
||||
return rotation;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
pub mod MathUtils;
|
||||
use crate::common::BitMatrix;
|
||||
use crate::{NotFoundException, RXingResultPoint};
|
||||
use crate::{Exceptions, RXingResultPoint};
|
||||
|
||||
/*
|
||||
* Copyright 2009 ZXing authors
|
||||
@@ -49,7 +49,7 @@ impl MonochromeRectangleDetector {
|
||||
* third, the rightmost
|
||||
* @throws NotFoundException if no Data Matrix Code can be found
|
||||
*/
|
||||
pub fn detect(&self) -> Result<Vec<RXingResultPoint>, NotFoundException> {
|
||||
pub fn detect(&self) -> Result<Vec<RXingResultPoint>, Exceptions> {
|
||||
let height = self.image.getHeight() as i32;
|
||||
let width = self.image.getWidth() as i32;
|
||||
let halfHeight= height / 2;
|
||||
@@ -155,7 +155,7 @@ impl MonochromeRectangleDetector {
|
||||
top: i32,
|
||||
bottom: i32,
|
||||
maxWhiteRun: i32,
|
||||
) -> Result<RXingResultPoint, NotFoundException> {
|
||||
) -> Result<RXingResultPoint, Exceptions> {
|
||||
let mut lastRange_z: Option<Vec<i32>> = None;
|
||||
let mut y: i32 = centerY;
|
||||
let mut x: i32 = centerX;
|
||||
@@ -212,13 +212,13 @@ impl MonochromeRectangleDetector {
|
||||
}
|
||||
}
|
||||
}}else {
|
||||
return Err(NotFoundException {});
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
}
|
||||
lastRange_z = range;
|
||||
y += deltaY;
|
||||
x += deltaX
|
||||
}
|
||||
return Err(NotFoundException {});
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -354,7 +354,7 @@ pub struct WhiteRectangleDetector {
|
||||
}
|
||||
|
||||
impl WhiteRectangleDetector {
|
||||
pub fn new_from_image(image: &BitMatrix) -> Result<Self, NotFoundException> {
|
||||
pub fn new_from_image(image: &BitMatrix) -> Result<Self, Exceptions> {
|
||||
Self::new(
|
||||
image,
|
||||
INIT_SIZE,
|
||||
@@ -375,7 +375,7 @@ impl WhiteRectangleDetector {
|
||||
initSize: i32,
|
||||
x: i32,
|
||||
y: i32,
|
||||
) -> Result<Self, NotFoundException> {
|
||||
) -> Result<Self, Exceptions> {
|
||||
|
||||
let halfsize = initSize / 2;
|
||||
|
||||
@@ -389,7 +389,7 @@ impl WhiteRectangleDetector {
|
||||
|| downInit >= image.getHeight() as i32
|
||||
|| rightInit >= image.getWidth() as i32
|
||||
{
|
||||
return Err(NotFoundException {});
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
}
|
||||
|
||||
Ok(Self{
|
||||
@@ -417,7 +417,7 @@ impl WhiteRectangleDetector {
|
||||
* leftmost and the third, the rightmost
|
||||
* @throws NotFoundException if no Data Matrix Code can be found
|
||||
*/
|
||||
pub fn detect(&self) -> Result<Vec<RXingResultPoint>, NotFoundException> {
|
||||
pub fn detect(&self) -> Result<Vec<RXingResultPoint>, Exceptions> {
|
||||
let mut left: i32 = self.leftInit;
|
||||
let mut right: i32 = self.rightInit;
|
||||
let mut up: i32 = self.upInit;
|
||||
@@ -532,7 +532,7 @@ impl WhiteRectangleDetector {
|
||||
}
|
||||
|
||||
if z.is_none() {
|
||||
return Err(NotFoundException {});
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
}
|
||||
|
||||
let mut t: Option<RXingResultPoint> = None;
|
||||
@@ -550,7 +550,7 @@ impl WhiteRectangleDetector {
|
||||
}
|
||||
|
||||
if t.is_none() {
|
||||
return Err(NotFoundException {});
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
}
|
||||
|
||||
let mut x: Option<RXingResultPoint> = None;
|
||||
@@ -568,7 +568,7 @@ impl WhiteRectangleDetector {
|
||||
}
|
||||
|
||||
if x.is_none() {
|
||||
return Err(NotFoundException {});
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
}
|
||||
|
||||
let mut y: Option<RXingResultPoint> = None;
|
||||
@@ -586,12 +586,12 @@ impl WhiteRectangleDetector {
|
||||
}
|
||||
|
||||
if y.is_none() {
|
||||
return Err(NotFoundException {});
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
}
|
||||
|
||||
return Ok(self.centerEdges(&y.unwrap(), &z.unwrap(), &x.unwrap(), &t.unwrap()));
|
||||
} else {
|
||||
return Err(NotFoundException {});
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ use std::cmp;
|
||||
use std::collections::HashMap;
|
||||
use std::fmt;
|
||||
|
||||
use crate::exceptions::IllegalArgumentException;
|
||||
use crate::Exceptions;
|
||||
use crate::DecodeHintType;
|
||||
use crate::RXingResultPoint;
|
||||
use encoding::Encoding;
|
||||
@@ -440,11 +440,11 @@ impl BitArray {
|
||||
* @param start start of range, inclusive.
|
||||
* @param end end of range, exclusive
|
||||
*/
|
||||
pub fn setRange(&mut self, start: usize, end: usize) -> Result<(), IllegalArgumentException> {
|
||||
pub fn setRange(&mut self, start: usize, end: usize) -> Result<(), Exceptions> {
|
||||
let mut end = end;
|
||||
if end < start || start < 0 || end > self.size {
|
||||
return Err(IllegalArgumentException::new(
|
||||
"end < start || start < 0 || end > self.size",
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
"end < start || start < 0 || end > self.size".to_owned(),
|
||||
));
|
||||
}
|
||||
if end == start {
|
||||
@@ -489,11 +489,11 @@ impl BitArray {
|
||||
start: usize,
|
||||
end: usize,
|
||||
value: bool,
|
||||
) -> Result<bool, IllegalArgumentException> {
|
||||
) -> Result<bool, Exceptions> {
|
||||
let mut end = end;
|
||||
if end < start || start < 0 || end > self.size {
|
||||
return Err(IllegalArgumentException::new(
|
||||
"end < start || start < 0 || end > self.size",
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
"end < start || start < 0 || end > self.size".to_owned(),
|
||||
));
|
||||
}
|
||||
if end == start {
|
||||
@@ -538,10 +538,10 @@ impl BitArray {
|
||||
&mut self,
|
||||
value: u32,
|
||||
numBits: usize,
|
||||
) -> Result<(), IllegalArgumentException> {
|
||||
) -> Result<(), Exceptions> {
|
||||
if numBits < 0 || numBits > 32 {
|
||||
return Err(IllegalArgumentException::new(
|
||||
"Num bits must be between 0 and 32",
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
"Num bits must be between 0 and 32".to_owned(),
|
||||
));
|
||||
}
|
||||
let mut nextSize = self.size;
|
||||
@@ -566,9 +566,9 @@ impl BitArray {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn xor(&mut self, other: &BitArray) -> Result<(), IllegalArgumentException> {
|
||||
pub fn xor(&mut self, other: &BitArray) -> Result<(), Exceptions> {
|
||||
if self.size != other.size {
|
||||
return Err(IllegalArgumentException::new("Sizes don't match"));
|
||||
return Err(Exceptions::IllegalArgumentException("Sizes don't match".to_owned()));
|
||||
}
|
||||
for i in 0..self.bits.len() {
|
||||
//for (int i = 0; i < bits.length; i++) {
|
||||
@@ -785,10 +785,10 @@ impl BitMatrix {
|
||||
* @param width bit matrix width
|
||||
* @param height bit matrix height
|
||||
*/
|
||||
pub fn new(width: u32, height: u32) -> Result<Self, IllegalArgumentException> {
|
||||
pub fn new(width: u32, height: u32) -> Result<Self, Exceptions> {
|
||||
if width < 1 || height < 1 {
|
||||
return Err(IllegalArgumentException::new(
|
||||
"Both dimensions must be greater than 0",
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
"Both dimensions must be greater than 0".to_owned(),
|
||||
));
|
||||
}
|
||||
Ok(Self {
|
||||
@@ -839,7 +839,7 @@ impl BitMatrix {
|
||||
stringRepresentation: &str,
|
||||
setString: &str,
|
||||
unsetString: &str,
|
||||
) -> Result<Self, IllegalArgumentException> {
|
||||
) -> Result<Self, Exceptions> {
|
||||
// cannot pass nulls in rust
|
||||
// if (stringRepresentation == null) {
|
||||
// throw new IllegalArgumentException();
|
||||
@@ -862,7 +862,7 @@ impl BitMatrix {
|
||||
first_run = false;
|
||||
rowLength = bitsPos - rowStartPos;
|
||||
} else if bitsPos - rowStartPos != rowLength {
|
||||
return Err(IllegalArgumentException::new("row lengths do not match"));
|
||||
return Err(Exceptions::IllegalArgumentException("row lengths do not match".to_owned()));
|
||||
}
|
||||
rowStartPos = bitsPos;
|
||||
nRows += 1;
|
||||
@@ -877,7 +877,7 @@ impl BitMatrix {
|
||||
bits[bitsPos] = false;
|
||||
bitsPos += 1;
|
||||
} else {
|
||||
return Err(IllegalArgumentException::new(&format!(
|
||||
return Err(Exceptions::IllegalArgumentException(format!(
|
||||
"illegal character encountered: {}",
|
||||
stringRepresentation[pos..].to_owned()
|
||||
)));
|
||||
@@ -891,7 +891,7 @@ impl BitMatrix {
|
||||
first_run = false;
|
||||
rowLength = bitsPos - rowStartPos;
|
||||
} else if bitsPos - rowStartPos != rowLength {
|
||||
return Err(IllegalArgumentException::new("row lengths do not match"));
|
||||
return Err(Exceptions::IllegalArgumentException("row lengths do not match".to_owned()));
|
||||
}
|
||||
nRows += 1;
|
||||
}
|
||||
@@ -965,10 +965,10 @@ impl BitMatrix {
|
||||
*
|
||||
* @param mask XOR mask
|
||||
*/
|
||||
pub fn xor(&mut self, mask: &BitMatrix) -> Result<(), IllegalArgumentException> {
|
||||
pub fn xor(&mut self, mask: &BitMatrix) -> Result<(), Exceptions> {
|
||||
if self.width != mask.width || self.height != mask.height || self.rowSize != mask.rowSize {
|
||||
return Err(IllegalArgumentException::new(
|
||||
"input matrix dimensions do not match",
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
"input matrix dimensions do not match".to_owned(),
|
||||
));
|
||||
}
|
||||
let rowArray = BitArray::with_size(self.width as usize);
|
||||
@@ -1010,22 +1010,22 @@ impl BitMatrix {
|
||||
top: u32,
|
||||
width: u32,
|
||||
height: u32,
|
||||
) -> Result<(), IllegalArgumentException> {
|
||||
) -> Result<(), Exceptions> {
|
||||
if top < 0 || left < 0 {
|
||||
return Err(IllegalArgumentException::new(
|
||||
"Left and top must be nonnegative",
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
"Left and top must be nonnegative".to_owned(),
|
||||
));
|
||||
}
|
||||
if height < 1 || width < 1 {
|
||||
return Err(IllegalArgumentException::new(
|
||||
"Height and width must be at least 1",
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
"Height and width must be at least 1".to_owned(),
|
||||
));
|
||||
}
|
||||
let right = left + width;
|
||||
let bottom = top + height;
|
||||
if bottom > self.height || right > self.width {
|
||||
return Err(IllegalArgumentException::new(
|
||||
"The region must fit inside the matrix",
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
"The region must fit inside the matrix".to_owned(),
|
||||
));
|
||||
}
|
||||
for y in top..bottom {
|
||||
@@ -1081,7 +1081,7 @@ impl BitMatrix {
|
||||
*
|
||||
* @param degrees number of degrees to rotate through counter-clockwise (0, 90, 180, 270)
|
||||
*/
|
||||
pub fn rotate(&mut self, degrees: u32) -> Result<(), IllegalArgumentException> {
|
||||
pub fn rotate(&mut self, degrees: u32) -> Result<(), Exceptions> {
|
||||
match degrees % 360 {
|
||||
0 => Ok(()),
|
||||
90 => {
|
||||
@@ -1097,8 +1097,8 @@ impl BitMatrix {
|
||||
self.rotate180();
|
||||
Ok(())
|
||||
}
|
||||
_ => Err(IllegalArgumentException::new(
|
||||
"degrees must be a multiple of 0, 90, 180, or 270",
|
||||
_ => Err(Exceptions::IllegalArgumentException(
|
||||
"degrees must be a multiple of 0, 90, 180, or 270".to_owned(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
@@ -1516,9 +1516,9 @@ impl BitSource {
|
||||
* bits of the int
|
||||
* @throws IllegalArgumentException if numBits isn't in [1,32] or more than is available
|
||||
*/
|
||||
pub fn readBits(&mut self, numBits: usize) -> Result<u32, IllegalArgumentException> {
|
||||
pub fn readBits(&mut self, numBits: usize) -> Result<u32, Exceptions> {
|
||||
if numBits < 1 || numBits > 32 || numBits > self.available() {
|
||||
return Err(IllegalArgumentException::new(&numBits.to_string()));
|
||||
return Err(Exceptions::IllegalArgumentException(numBits.to_string()));
|
||||
}
|
||||
|
||||
let mut result = 0;
|
||||
|
||||
@@ -30,7 +30,7 @@ use super::{GenericGF, GenericGFPoly};
|
||||
#[test]
|
||||
fn testPolynomialString() {
|
||||
let FIELD = super::get_predefined_genericgf(super::PredefinedGenericGF::QrCodeField256);
|
||||
let fz = super::GenericGFPoly::new(FIELD.clone(), &vec![0;0]).unwrap();
|
||||
let fz = super::GenericGFPoly::new(FIELD.clone(), &vec![0; 0]).unwrap();
|
||||
|
||||
assert_eq!("0", fz.getZero().to_string());
|
||||
assert_eq!("-1", FIELD.buildMonomial(0, -1).to_string());
|
||||
@@ -43,7 +43,7 @@ fn testPolynomialString() {
|
||||
#[test]
|
||||
fn testZero() {
|
||||
let FIELD = super::get_predefined_genericgf(super::PredefinedGenericGF::QrCodeField256);
|
||||
let fz = super::GenericGFPoly::new(FIELD.clone(), &vec![0;0]).unwrap();
|
||||
let fz = super::GenericGFPoly::new(FIELD.clone(), &vec![0; 0]).unwrap();
|
||||
|
||||
assert_eq!(fz.getZero(), FIELD.buildMonomial(1, 0));
|
||||
assert_eq!(
|
||||
|
||||
@@ -466,7 +466,7 @@ fn testDecoder(field: &GenericGF, dataWords: &Vec<i32>, ecWords: &Vec<i32>) {
|
||||
// fail only if maxErrors exceeded
|
||||
assert!(
|
||||
i > maxErrors,
|
||||
"Decode in {} ({},{}) failed at {} errors: {}",
|
||||
"Decode in {} ({},{}) failed at {} errors: {:#?}",
|
||||
field,
|
||||
dataWords.len(),
|
||||
ecWords.len(),
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use std::fmt;
|
||||
|
||||
use crate::exceptions::*;
|
||||
use crate::Exceptions;
|
||||
use std::hash::Hash;
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -8,49 +8,6 @@ mod GenericGFPolyTestCase;
|
||||
#[cfg(test)]
|
||||
mod ReedSolomonTestCase;
|
||||
|
||||
/*
|
||||
* Copyrigh&t 2007 ZXing authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "&License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
//package com.google.zxing.common.reedsolomon;
|
||||
|
||||
/**
|
||||
* <p>Thrown when an exception occurs during Reed-Solomon decoding, such as when
|
||||
* there are too many errors to correct.</p>
|
||||
*
|
||||
* @author Sean Owen
|
||||
*/
|
||||
#[derive(Debug)]
|
||||
pub struct ReedSolomonException {
|
||||
message: String,
|
||||
}
|
||||
|
||||
impl ReedSolomonException {
|
||||
pub fn new(message: &str) -> Self {
|
||||
Self {
|
||||
message: message.to_owned(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for ReedSolomonException {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}", self.message)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Copyright 2007 ZXing authors
|
||||
*
|
||||
@@ -222,9 +179,9 @@ impl GenericGF {
|
||||
/**
|
||||
* @return base 2 log of a in GF(size)
|
||||
*/
|
||||
pub fn log(&self, a: i32) -> Result<i32, IllegalArgumentException> {
|
||||
pub fn log(&self, a: i32) -> Result<i32, Exceptions> {
|
||||
if a == 0 {
|
||||
return Err(IllegalArgumentException::new(""));
|
||||
return Err(Exceptions::IllegalArgumentException("".to_owned()));
|
||||
}
|
||||
let pos: usize = a.try_into().unwrap();
|
||||
return Ok(self.logTable[pos]);
|
||||
@@ -233,9 +190,9 @@ impl GenericGF {
|
||||
/**
|
||||
* @return multiplicative inverse of a
|
||||
*/
|
||||
pub fn inverse(&self, a: i32) -> Result<i32, ArithmeticException> {
|
||||
pub fn inverse(&self, a: i32) -> Result<i32, Exceptions> {
|
||||
if a == 0 {
|
||||
return Err(ArithmeticException::new(""));
|
||||
return Err(Exceptions::ArithmeticException("".to_owned()));
|
||||
}
|
||||
let log_t_loc: usize = a.try_into().unwrap();
|
||||
let loc: usize = ((self.size as i32) - self.logTable[log_t_loc] - 1)
|
||||
@@ -327,9 +284,9 @@ impl GenericGFPoly {
|
||||
pub fn new(
|
||||
field: GenericGF,
|
||||
coefficients: &Vec<i32>,
|
||||
) -> Result<Self, IllegalArgumentException> {
|
||||
) -> Result<Self, Exceptions> {
|
||||
if coefficients.len() == 0 {
|
||||
return Err(IllegalArgumentException::new(""));
|
||||
return Err(Exceptions::IllegalArgumentException("".to_owned()));
|
||||
}
|
||||
Ok(Self {
|
||||
field: field,
|
||||
@@ -422,10 +379,10 @@ impl GenericGFPoly {
|
||||
pub fn addOrSubtract(
|
||||
&self,
|
||||
other: &GenericGFPoly,
|
||||
) -> Result<GenericGFPoly, IllegalArgumentException> {
|
||||
) -> Result<GenericGFPoly, Exceptions> {
|
||||
if self.field != other.field {
|
||||
return Err(IllegalArgumentException::new(
|
||||
"GenericGFPolys do not have same GenericGF field",
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
"GenericGFPolys do not have same GenericGF field".to_owned(),
|
||||
));
|
||||
}
|
||||
if self.isZero() {
|
||||
@@ -463,11 +420,11 @@ impl GenericGFPoly {
|
||||
pub fn multiply(
|
||||
&self,
|
||||
other: &GenericGFPoly,
|
||||
) -> Result<GenericGFPoly, IllegalArgumentException> {
|
||||
) -> Result<GenericGFPoly, Exceptions> {
|
||||
if self.field != other.field {
|
||||
//if (!field.equals(other.field)) {
|
||||
return Err(IllegalArgumentException::new(
|
||||
"GenericGFPolys do not have same GenericGF field",
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
"GenericGFPolys do not have same GenericGF field".to_owned(),
|
||||
));
|
||||
}
|
||||
if self.isZero() || other.isZero() {
|
||||
@@ -529,9 +486,9 @@ impl GenericGFPoly {
|
||||
&self,
|
||||
degree: usize,
|
||||
coefficient: i32,
|
||||
) -> Result<GenericGFPoly, IllegalArgumentException> {
|
||||
) -> Result<GenericGFPoly, Exceptions> {
|
||||
if degree < 0 {
|
||||
return Err(IllegalArgumentException::new(""));
|
||||
return Err(Exceptions::IllegalArgumentException("".to_owned()));
|
||||
}
|
||||
if coefficient == 0 {
|
||||
return Ok(self.getZero());
|
||||
@@ -548,15 +505,15 @@ impl GenericGFPoly {
|
||||
pub fn divide(
|
||||
&self,
|
||||
other: &GenericGFPoly,
|
||||
) -> Result<Vec<GenericGFPoly>, IllegalArgumentException> {
|
||||
) -> Result<Vec<GenericGFPoly>, Exceptions> {
|
||||
if self.field != other.field {
|
||||
//if (!field.equals(other.field)) {
|
||||
return Err(IllegalArgumentException::new(
|
||||
"GenericGFPolys do not have same GenericGF field",
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
"GenericGFPolys do not have same GenericGF field".to_owned(),
|
||||
));
|
||||
}
|
||||
if other.isZero() {
|
||||
return Err(IllegalArgumentException::new("Divide by 0"));
|
||||
return Err(Exceptions::IllegalArgumentException("Divide by 0".to_owned()));
|
||||
}
|
||||
|
||||
let mut quotient = self.getZero();
|
||||
@@ -565,7 +522,7 @@ impl GenericGFPoly {
|
||||
let denominatorLeadingTerm = other.getCoefficient(other.getDegree());
|
||||
let inverseDenominatorLeadingTerm = match self.field.inverse(denominatorLeadingTerm) {
|
||||
Ok(val) => val,
|
||||
Err(issue) => return Err(IllegalArgumentException::new("arithmetic issue")),
|
||||
Err(issue) => return Err(Exceptions::IllegalArgumentException("arithmetic issue".to_owned())),
|
||||
};
|
||||
|
||||
while remainder.getDegree() >= other.getDegree() && !remainder.isZero() {
|
||||
@@ -690,7 +647,7 @@ impl ReedSolomonDecoder {
|
||||
* @param twoS number of error-correction codewords available
|
||||
* @throws ReedSolomonException if decoding fails for any reason
|
||||
*/
|
||||
pub fn decode(&self, received: &mut Vec<i32>, twoS: i32) -> Result<(), ReedSolomonException> {
|
||||
pub fn decode(&self, received: &mut Vec<i32>, twoS: i32) -> Result<(), Exceptions> {
|
||||
let poly = GenericGFPoly::new(self.field.clone(), received).unwrap();
|
||||
let mut syndromeCoefficients = Vec::with_capacity(twoS.try_into().unwrap());
|
||||
let mut noError = true;
|
||||
@@ -713,7 +670,7 @@ impl ReedSolomonDecoder {
|
||||
}
|
||||
let syndrome = match GenericGFPoly::new(self.field.clone(), &syndromeCoefficients) {
|
||||
Ok(res) => res,
|
||||
Err(fail) => return Err(ReedSolomonException::new("IllegalArgumentException")),
|
||||
Err(fail) => return Err(Exceptions::ReedSolomonException("IllegalArgumentException".to_owned())),
|
||||
};
|
||||
let sigmaOmega = self.runEuclideanAlgorithm(
|
||||
&self.field.buildMonomial(twoS.try_into().unwrap(), 1),
|
||||
@@ -730,10 +687,10 @@ impl ReedSolomonDecoder {
|
||||
- 1
|
||||
- match self.field.log(errorLocations[i].try_into().unwrap()) {
|
||||
Ok(size) => size as usize,
|
||||
Err(err) => return Err(ReedSolomonException::new("IllegalArgumentException")),
|
||||
Err(err) => return Err(Exceptions::ReedSolomonException("IllegalArgumentException".to_owned())),
|
||||
};
|
||||
if position < 0 {
|
||||
return Err(ReedSolomonException::new("Bad error location"));
|
||||
return Err(Exceptions::ReedSolomonException("Bad error location".to_owned()));
|
||||
}
|
||||
received[position] = GenericGF::addOrSubtract(received[position], errorMagnitudes[i]);
|
||||
}
|
||||
@@ -745,7 +702,7 @@ impl ReedSolomonDecoder {
|
||||
a: &GenericGFPoly,
|
||||
b: &GenericGFPoly,
|
||||
R: usize,
|
||||
) -> Result<Vec<GenericGFPoly>, ReedSolomonException> {
|
||||
) -> Result<Vec<GenericGFPoly>, Exceptions> {
|
||||
// Assume a's degree is >= b's
|
||||
let mut a = a.clone();
|
||||
let mut b = b.clone();
|
||||
@@ -772,14 +729,14 @@ impl ReedSolomonDecoder {
|
||||
// Divide rLastLast by rLast, with quotient in q and remainder in r
|
||||
if rLast.isZero() {
|
||||
// Oops, Euclidean algorithm already terminated?
|
||||
return Err(ReedSolomonException::new("r_{i-1} was zero"));
|
||||
return Err(Exceptions::ReedSolomonException("r_{i-1} was zero".to_owned()));
|
||||
}
|
||||
r = rLastLast;
|
||||
let mut q = r.getZero();
|
||||
let denominatorLeadingTerm = rLast.getCoefficient(rLast.getDegree());
|
||||
let dltInverse = match self.field.inverse(denominatorLeadingTerm) {
|
||||
Ok(inv) => inv,
|
||||
Err(err) => return Err(ReedSolomonException::new("ArithmetricException")),
|
||||
Err(err) => return Err(Exceptions::ReedSolomonException("ArithmetricException".to_owned())),
|
||||
};
|
||||
while r.getDegree() >= rLast.getDegree() && !r.isZero() {
|
||||
let degreeDiff = r.getDegree() - rLast.getDegree();
|
||||
@@ -788,29 +745,29 @@ impl ReedSolomonDecoder {
|
||||
.multiply(r.getCoefficient(r.getDegree()), dltInverse);
|
||||
q = match q.addOrSubtract(&self.field.buildMonomial(degreeDiff, scale)) {
|
||||
Ok(res) => res,
|
||||
Err(err) => return Err(ReedSolomonException::new("IllegalArgumentException")),
|
||||
Err(err) => return Err(Exceptions::ReedSolomonException("IllegalArgumentException".to_owned())),
|
||||
};
|
||||
r = match r.addOrSubtract(&match rLast.multiplyByMonomial(degreeDiff, scale) {
|
||||
Ok(res) => res,
|
||||
Err(err) => return Err(ReedSolomonException::new("IllegalArgumentException")),
|
||||
Err(err) => return Err(Exceptions::ReedSolomonException("IllegalArgumentException".to_owned())),
|
||||
}) {
|
||||
Ok(res) => res,
|
||||
Err(err) => return Err(ReedSolomonException::new("IllegalArgumentException")),
|
||||
Err(err) => return Err(Exceptions::ReedSolomonException("IllegalArgumentException".to_owned())),
|
||||
};
|
||||
}
|
||||
|
||||
t = match (match q.multiply(&tLast) {
|
||||
Ok(res) => res,
|
||||
Err(err) => return Err(ReedSolomonException::new("IllegalArgumentException")),
|
||||
Err(err) => return Err(Exceptions::ReedSolomonException("IllegalArgumentException".to_owned())),
|
||||
})
|
||||
.addOrSubtract(&tLastLast)
|
||||
{
|
||||
Ok(res) => res,
|
||||
Err(err) => return Err(ReedSolomonException::new("IllegalArgumentException")),
|
||||
Err(err) => return Err(Exceptions::ReedSolomonException("IllegalArgumentException".to_owned())),
|
||||
};
|
||||
|
||||
if r.getDegree() >= rLast.getDegree() {
|
||||
return Err(ReedSolomonException::new(&format!(
|
||||
return Err(Exceptions::ReedSolomonException(format!(
|
||||
"Division algorithm failed to reduce polynomial? r: {}, rLast: {}",
|
||||
r, rLast
|
||||
)));
|
||||
@@ -819,12 +776,12 @@ impl ReedSolomonDecoder {
|
||||
|
||||
let sigmaTildeAtZero = t.getCoefficient(0);
|
||||
if sigmaTildeAtZero == 0 {
|
||||
return Err(ReedSolomonException::new("sigmaTilde(0) was zero"));
|
||||
return Err(Exceptions::ReedSolomonException("sigmaTilde(0) was zero".to_owned()));
|
||||
}
|
||||
|
||||
let inverse = match self.field.inverse(sigmaTildeAtZero) {
|
||||
Ok(res) => res,
|
||||
Err(err) => return Err(ReedSolomonException::new("ArithmetricException")),
|
||||
Err(err) => return Err(Exceptions::ReedSolomonException("ArithmetricException".to_owned())),
|
||||
};
|
||||
let sigma = t.multiply_with_scalar(inverse);
|
||||
let omega = r.multiply_with_scalar(inverse);
|
||||
@@ -834,7 +791,7 @@ impl ReedSolomonDecoder {
|
||||
fn findErrorLocations(
|
||||
&self,
|
||||
errorLocator: &GenericGFPoly,
|
||||
) -> Result<Vec<usize>, ReedSolomonException> {
|
||||
) -> Result<Vec<usize>, Exceptions> {
|
||||
// This is a direct application of Chien's search
|
||||
let numErrors = errorLocator.getDegree();
|
||||
if numErrors == 1 {
|
||||
@@ -852,14 +809,14 @@ impl ReedSolomonDecoder {
|
||||
if errorLocator.evaluateAt(i) == 0 {
|
||||
result[e] = match self.field.inverse(i.try_into().unwrap()) {
|
||||
Ok(res) => res.try_into().unwrap(),
|
||||
Err(err) => return Err(ReedSolomonException::new("ArithmetricException")),
|
||||
Err(err) => return Err(Exceptions::ReedSolomonException("ArithmetricException".to_owned())),
|
||||
};
|
||||
e += 1;
|
||||
}
|
||||
}
|
||||
if e != numErrors {
|
||||
return Err(ReedSolomonException::new(
|
||||
"Error locator degree does not match number of roots",
|
||||
return Err(Exceptions::ReedSolomonException(
|
||||
"Error locator degree does not match number of roots".to_owned(),
|
||||
));
|
||||
}
|
||||
return Ok(result);
|
||||
@@ -985,13 +942,13 @@ impl ReedSolomonEncoder {
|
||||
&mut self,
|
||||
toEncode: &mut Vec<i32>,
|
||||
ecBytes: usize,
|
||||
) -> Result<(), IllegalArgumentException> {
|
||||
) -> Result<(), Exceptions> {
|
||||
if ecBytes == 0 {
|
||||
return Err(IllegalArgumentException::new("No error correction bytes"));
|
||||
return Err(Exceptions::IllegalArgumentException("No error correction bytes".to_owned()));
|
||||
}
|
||||
let dataBytes = toEncode.len() - ecBytes;
|
||||
if dataBytes <= 0 {
|
||||
return Err(IllegalArgumentException::new("No data bytes provided"));
|
||||
return Err(Exceptions::IllegalArgumentException("No data bytes provided".to_owned()));
|
||||
}
|
||||
let fld = self.field.clone();
|
||||
let generator = self.buildGenerator(ecBytes);
|
||||
|
||||
Reference in New Issue
Block a user