Miscellaneous tweaks from inspection

git-svn-id: https://zxing.googlecode.com/svn/trunk@2244 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2012-03-31 12:05:30 +00:00
parent b1dfc23418
commit cc204e1bde
9 changed files with 158 additions and 164 deletions

View file

@ -36,13 +36,14 @@ public class GlobalHistogramBinarizer extends Binarizer {
private static final int LUMINANCE_BITS = 5; private static final int LUMINANCE_BITS = 5;
private static final int LUMINANCE_SHIFT = 8 - LUMINANCE_BITS; private static final int LUMINANCE_SHIFT = 8 - LUMINANCE_BITS;
private static final int LUMINANCE_BUCKETS = 1 << LUMINANCE_BITS; private static final int LUMINANCE_BUCKETS = 1 << LUMINANCE_BITS;
private static final byte[] EMPTY = new byte[0];
private byte[] luminances; private byte[] luminances;
private final int[] buckets; private final int[] buckets;
public GlobalHistogramBinarizer(LuminanceSource source) { public GlobalHistogramBinarizer(LuminanceSource source) {
super(source); super(source);
luminances = new byte[0]; luminances = EMPTY;
buckets = new int[LUMINANCE_BUCKETS]; buckets = new int[LUMINANCE_BUCKETS];
} }

View file

@ -103,8 +103,8 @@ final class GenericGFPoly {
if (a == 1) { if (a == 1) {
// Just the sum of the coefficients // Just the sum of the coefficients
int result = 0; int result = 0;
for (int i = 0; i < size; i++) { for (int coefficient : coefficients) {
result = GenericGF.addOrSubtract(result, coefficients[i]); result = GenericGF.addOrSubtract(result, coefficient);
} }
return result; return result;
} }

View file

@ -84,8 +84,8 @@ public final class Decoder {
// Count total number of data bytes // Count total number of data bytes
int totalBytes = 0; int totalBytes = 0;
for (int i = 0; i < dataBlocksCount; i++) { for (DataBlock db : dataBlocks) {
totalBytes += dataBlocks[i].getNumDataCodewords(); totalBytes += db.getNumDataCodewords();
} }
byte[] resultBytes = new byte[totalBytes]; byte[] resultBytes = new byte[totalBytes];

View file

@ -100,12 +100,7 @@ public final class Version {
throw FormatException.getFormatInstance(); throw FormatException.getFormatInstance();
} }
// TODO(bbrown): This is doing a linear search through the array of versions. for (Version version : VERSIONS) {
// If we interleave the rectangular versions with the square versions we could
// do a binary search.
int numVersions = VERSIONS.length;
for (int i = 0; i < numVersions; ++i){
Version version = VERSIONS[i];
if (version.symbolSizeRows == numRows && version.symbolSizeColumns == numColumns) { if (version.symbolSizeRows == numRows && version.symbolSizeColumns == numColumns) {
return version; return version;
} }

View file

@ -213,8 +213,7 @@ public final class Code39Reader extends OneDReader {
int wideCounters; int wideCounters;
do { do {
int minCounter = Integer.MAX_VALUE; int minCounter = Integer.MAX_VALUE;
for (int i = 0; i < numCounters; i++) { for (int counter : counters) {
int counter = counters[i];
if (counter < minCounter && counter > maxNarrowCounter) { if (counter < minCounter && counter > maxNarrowCounter) {
minCounter = counter; minCounter = counter;
} }

View file

@ -147,8 +147,8 @@ public final class Code93Reader extends OneDReader {
private static int toPattern(int[] counters) { private static int toPattern(int[] counters) {
int max = counters.length; int max = counters.length;
int sum = 0; int sum = 0;
for (int i = 0; i < max; i++) { for (int counter : counters) {
sum += counters[i]; sum += counter;
} }
int pattern = 0; int pattern = 0;
for (int i = 0; i < max; i++) { for (int i = 0; i < max; i++) {

View file

@ -64,8 +64,8 @@ public final class RSSUtils {
public static int getRSSvalue(int[] widths, int maxWidth, boolean noNarrow) { public static int getRSSvalue(int[] widths, int maxWidth, boolean noNarrow) {
int elements = widths.length; int elements = widths.length;
int n = 0; int n = 0;
for (int i = 0; i < elements; i++) { for (int width : widths) {
n += widths[i]; n += width;
} }
int val = 0; int val = 0;
int narrowMask = 0; int narrowMask = 0;

View file

@ -55,9 +55,11 @@ public final class MatrixUtilTestCase extends Assert {
} }
@Test @Test
public void testEmbedBasicPatterns() throws WriterException { public void testEmbedBasicPatterns1() throws WriterException {
{
// Version 1. // Version 1.
ByteMatrix matrix = new ByteMatrix(21, 21);
MatrixUtil.clearMatrix(matrix);
MatrixUtil.embedBasicPatterns(1, matrix);
String expected = String expected =
" 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1\n" + " 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1\n" +
" 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1\n" + " 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1\n" +
@ -80,14 +82,16 @@ public final class MatrixUtilTestCase extends Assert {
" 1 0 1 1 1 0 1 0 \n" + " 1 0 1 1 1 0 1 0 \n" +
" 1 0 0 0 0 0 1 0 \n" + " 1 0 0 0 0 0 1 0 \n" +
" 1 1 1 1 1 1 1 0 \n"; " 1 1 1 1 1 1 1 0 \n";
ByteMatrix matrix = new ByteMatrix(21, 21);
MatrixUtil.clearMatrix(matrix);
MatrixUtil.embedBasicPatterns(1, matrix);
assertEquals(expected, matrix.toString()); assertEquals(expected, matrix.toString());
} }
{
@Test
public void testEmbedBasicPatterns2() throws WriterException {
// Version 2. Position adjustment pattern should apppear at right // Version 2. Position adjustment pattern should apppear at right
// bottom corner. // bottom corner.
ByteMatrix matrix = new ByteMatrix(25, 25);
MatrixUtil.clearMatrix(matrix);
MatrixUtil.embedBasicPatterns(2, matrix);
String expected = String expected =
" 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1\n" + " 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1\n" +
" 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1\n" + " 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1\n" +
@ -114,16 +118,15 @@ public final class MatrixUtilTestCase extends Assert {
" 1 0 1 1 1 0 1 0 \n" + " 1 0 1 1 1 0 1 0 \n" +
" 1 0 0 0 0 0 1 0 \n" + " 1 0 0 0 0 0 1 0 \n" +
" 1 1 1 1 1 1 1 0 \n"; " 1 1 1 1 1 1 1 0 \n";
ByteMatrix matrix = new ByteMatrix(25, 25);
MatrixUtil.clearMatrix(matrix);
MatrixUtil.embedBasicPatterns(2, matrix);
assertEquals(expected, matrix.toString()); assertEquals(expected, matrix.toString());
} }
}
@Test @Test
public void testEmbedTypeInfo() throws WriterException { public void testEmbedTypeInfo() throws WriterException {
// Type info bits = 100000011001110. // Type info bits = 100000011001110.
ByteMatrix matrix = new ByteMatrix(21, 21);
MatrixUtil.clearMatrix(matrix);
MatrixUtil.embedTypeInfo(ErrorCorrectionLevel.M, 5, matrix);
String expected = String expected =
" 0 \n" + " 0 \n" +
" 1 \n" + " 1 \n" +
@ -146,15 +149,17 @@ public final class MatrixUtilTestCase extends Assert {
" 0 \n" + " 0 \n" +
" 0 \n" + " 0 \n" +
" 1 \n"; " 1 \n";
ByteMatrix matrix = new ByteMatrix(21, 21);
MatrixUtil.clearMatrix(matrix);
MatrixUtil.embedTypeInfo(ErrorCorrectionLevel.M, 5, matrix);
assertEquals(expected, matrix.toString()); assertEquals(expected, matrix.toString());
} }
@Test @Test
public void testEmbedVersionInfo() throws WriterException { public void testEmbedVersionInfo() throws WriterException {
// Version info bits = 000111 110010 010100 // Version info bits = 000111 110010 010100
// Actually, version 7 QR Code has 45x45 matrix but we use 21x21 here
// since 45x45 matrix is too big to depict.
ByteMatrix matrix = new ByteMatrix(21, 21);
MatrixUtil.clearMatrix(matrix);
MatrixUtil.maybeEmbedVersionInfo(7, matrix);
String expected = String expected =
" 0 0 1 \n" + " 0 0 1 \n" +
" 0 1 0 \n" + " 0 1 0 \n" +
@ -177,17 +182,17 @@ public final class MatrixUtilTestCase extends Assert {
" \n" + " \n" +
" \n" + " \n" +
" \n"; " \n";
// Actually, version 7 QR Code has 45x45 matrix but we use 21x21 here
// since 45x45 matrix is too big to depict.
ByteMatrix matrix = new ByteMatrix(21, 21);
MatrixUtil.clearMatrix(matrix);
MatrixUtil.maybeEmbedVersionInfo(7, matrix);
assertEquals(expected, matrix.toString()); assertEquals(expected, matrix.toString());
} }
@Test @Test
public void testEmbedDataBits() throws WriterException { public void testEmbedDataBits() throws WriterException {
// Cells other than basic patterns should be filled with zero. // Cells other than basic patterns should be filled with zero.
BitArray bits = new BitArray();
ByteMatrix matrix = new ByteMatrix(21, 21);
MatrixUtil.clearMatrix(matrix);
MatrixUtil.embedBasicPatterns(1, matrix);
MatrixUtil.embedDataBits(bits, -1, matrix);
String expected = String expected =
" 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1\n" + " 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1\n" +
" 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1\n" + " 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1\n" +
@ -210,17 +215,25 @@ public final class MatrixUtilTestCase extends Assert {
" 1 0 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" + " 1 0 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" + " 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n"; " 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n";
BitArray bits = new BitArray();
ByteMatrix matrix = new ByteMatrix(21, 21);
MatrixUtil.clearMatrix(matrix);
MatrixUtil.embedBasicPatterns(1, matrix);
MatrixUtil.embedDataBits(bits, -1, matrix);
assertEquals(expected, matrix.toString()); assertEquals(expected, matrix.toString());
} }
@Test @Test
public void testBuildMatrix() throws WriterException { public void testBuildMatrix() throws WriterException {
// From http://www.swetake.com/qr/qr7.html // From http://www.swetake.com/qr/qr7.html
char[] bytes = {32, 65, 205, 69, 41, 220, 46, 128, 236,
42, 159, 74, 221, 244, 169, 239, 150, 138,
70, 237, 85, 224, 96, 74, 219 , 61};
BitArray bits = new BitArray();
for (char c: bytes) {
bits.appendBits(c, 8);
}
ByteMatrix matrix = new ByteMatrix(21, 21);
MatrixUtil.buildMatrix(bits,
ErrorCorrectionLevel.H,
1, // Version 1
3, // Mask pattern 3
matrix);
String expected = String expected =
" 1 1 1 1 1 1 1 0 0 1 1 0 0 0 1 1 1 1 1 1 1\n" + " 1 1 1 1 1 1 1 0 0 1 1 0 0 0 1 1 1 1 1 1 1\n" +
" 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1\n" + " 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1\n" +
@ -243,19 +256,6 @@ public final class MatrixUtilTestCase extends Assert {
" 1 0 1 1 1 0 1 0 1 1 1 1 0 0 0 0 1 1 1 0 0\n" + " 1 0 1 1 1 0 1 0 1 1 1 1 0 0 0 0 1 1 1 0 0\n" +
" 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0\n" + " 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0\n" +
" 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 0 1 0 0 1 0\n"; " 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 0 1 0 0 1 0\n";
char[] bytes = {32, 65, 205, 69, 41, 220, 46, 128, 236,
42, 159, 74, 221, 244, 169, 239, 150, 138,
70, 237, 85, 224, 96, 74, 219 , 61};
BitArray bits = new BitArray();
for (char c: bytes) {
bits.appendBits(c, 8);
}
ByteMatrix matrix = new ByteMatrix(21, 21);
MatrixUtil.buildMatrix(bits,
ErrorCorrectionLevel.H,
1, // Version 1
3, // Mask pattern 3
matrix);
assertEquals(expected, matrix.toString()); assertEquals(expected, matrix.toString());
} }

View file

@ -83,8 +83,7 @@ public final class QRCodeTestCase extends Assert {
} }
@Test @Test
public void testToString() { public void testToString1() {
{
QRCode qrCode = new QRCode(); QRCode qrCode = new QRCode();
String expected = String expected =
"<<\n" + "<<\n" +
@ -101,9 +100,28 @@ public final class QRCodeTestCase extends Assert {
">>\n"; ">>\n";
assertEquals(expected, qrCode.toString()); assertEquals(expected, qrCode.toString());
} }
{
String expected = @Test
"<<\n" + public void testToString2() {
QRCode qrCode = new QRCode();
qrCode.setMode(Mode.BYTE);
qrCode.setECLevel(ErrorCorrectionLevel.H);
qrCode.setVersion(1);
qrCode.setMatrixWidth(21);
qrCode.setMaskPattern(3);
qrCode.setNumTotalBytes(26);
qrCode.setNumDataBytes(9);
qrCode.setNumECBytes(17);
qrCode.setNumRSBlocks(1);
ByteMatrix matrix = new ByteMatrix(21, 21);
for (int y = 0; y < 21; ++y) {
for (int x = 0; x < 21; ++x) {
matrix.set(x, y, (y + x) % 2);
}
}
qrCode.setMatrix(matrix);
assertTrue(qrCode.isValid());
String expected = "<<\n" +
" mode: BYTE\n" + " mode: BYTE\n" +
" ecLevel: H\n" + " ecLevel: H\n" +
" version: 1\n" + " version: 1\n" +
@ -136,27 +154,8 @@ public final class QRCodeTestCase extends Assert {
" 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" + " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
" 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" + " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
">>\n"; ">>\n";
QRCode qrCode = new QRCode();
qrCode.setMode(Mode.BYTE);
qrCode.setECLevel(ErrorCorrectionLevel.H);
qrCode.setVersion(1);
qrCode.setMatrixWidth(21);
qrCode.setMaskPattern(3);
qrCode.setNumTotalBytes(26);
qrCode.setNumDataBytes(9);
qrCode.setNumECBytes(17);
qrCode.setNumRSBlocks(1);
ByteMatrix matrix = new ByteMatrix(21, 21);
for (int y = 0; y < 21; ++y) {
for (int x = 0; x < 21; ++x) {
matrix.set(x, y, (y + x) % 2);
}
}
qrCode.setMatrix(matrix);
assertTrue(qrCode.isValid());
assertEquals(expected, qrCode.toString()); assertEquals(expected, qrCode.toString());
} }
}
@Test @Test
public void testIsValidMaskPattern() { public void testIsValidMaskPattern() {