X-Git-Url: http://git.rot13.org/?p=zxing.git;a=blobdiff_plain;f=core%2Fsrc%2Fcom%2Fgoogle%2Fzxing%2Fdatamatrix%2Fdecoder%2FBitMatrixParser.java;h=45ffd2d04bdcf48c688c8848682135c8b512f4b4;hp=f54e9c1489c61ff2c7e99fe9285168854b48f725;hb=d0920ceb07da310eb8e10f6a11d5590bcb3184ea;hpb=93dc44adb7dbf108975ec09d061229b80f8cc834 diff --git a/core/src/com/google/zxing/datamatrix/decoder/BitMatrixParser.java b/core/src/com/google/zxing/datamatrix/decoder/BitMatrixParser.java index f54e9c14..45ffd2d0 100644 --- a/core/src/com/google/zxing/datamatrix/decoder/BitMatrixParser.java +++ b/core/src/com/google/zxing/datamatrix/decoder/BitMatrixParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2007 Google Inc. + * 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. @@ -16,7 +16,7 @@ package com.google.zxing.datamatrix.decoder; -import com.google.zxing.ReaderException; +import com.google.zxing.FormatException; import com.google.zxing.common.BitMatrix; /** @@ -26,23 +26,25 @@ final class BitMatrixParser { private final BitMatrix mappingBitMatrix; private final BitMatrix readMappingMatrix; - private Version version; -// private FormatInformation parsedFormatInfo; + private final Version version; /** * @param bitMatrix {@link BitMatrix} to parse - * @throws ReaderException if dimension is < 10 or > 144 or not 0 mod 2 + * @throws FormatException if dimension is < 8 or > 144 or not 0 mod 2 */ - BitMatrixParser(BitMatrix bitMatrix) throws ReaderException { - int dimension = bitMatrix.getDimension(); - if (dimension < 10 || dimension > 144 || (dimension & 0x01) != 0) { - throw new ReaderException("Invalid dimension (" + dimension + ") Must be 0 mod 2 and >= 10 and <= 144"); + BitMatrixParser(BitMatrix bitMatrix) throws FormatException { + int dimension = bitMatrix.getHeight(); + if (dimension < 8 || dimension > 144 || (dimension & 0x01) != 0) { + throw FormatException.getFormatInstance(); } version = readVersion(bitMatrix); this.mappingBitMatrix = extractDataRegion(bitMatrix); - // TODO(bbrown): Make this work for rectangular symbols - this.readMappingMatrix = new BitMatrix(this.mappingBitMatrix.getDimension()); + this.readMappingMatrix = new BitMatrix(this.mappingBitMatrix.getWidth(), this.mappingBitMatrix.getHeight()); + } + + Version getVersion() { + return version; } /** @@ -53,19 +55,12 @@ final class BitMatrixParser { * * @param bitMatrix Original {@link BitMatrix} including alignment patterns * @return {@link Version} encapsulating the Data Matrix Code's "version" - * @throws ReaderException if the dimensions of the mapping matrix are not valid + * @throws FormatException if the dimensions of the mapping matrix are not valid * Data Matrix dimensions. */ - Version readVersion(BitMatrix bitMatrix) throws ReaderException { - - if (version != null) { - return version; - } - - // TODO(bbrown): make this work for rectangular dimensions as well. - int numRows = bitMatrix.getDimension(); - int numColumns = numRows; - + private static Version readVersion(BitMatrix bitMatrix) throws FormatException { + int numRows = bitMatrix.getHeight(); + int numColumns = bitMatrix.getWidth(); return Version.getVersionForDimensions(numRows, numColumns); } @@ -75,18 +70,18 @@ final class BitMatrixParser { * Data Matrix Code.

* * @return bytes encoded within the Data Matrix Code - * @throws ReaderException if the exact number of bytes expected is not read + * @throws FormatException if the exact number of bytes expected is not read */ - byte[] readCodewords() throws ReaderException { + byte[] readCodewords() throws FormatException { byte[] result = new byte[version.getTotalCodewords()]; int resultOffset = 0; int row = 4; int column = 0; - // TODO(bbrown): Data Matrix can be rectangular, assuming square for now - int numRows = mappingBitMatrix.getDimension(); - int numColumns = numRows; + + int numRows = mappingBitMatrix.getHeight(); + int numColumns = mappingBitMatrix.getWidth(); boolean corner1Read = false; boolean corner2Read = false; @@ -98,49 +93,57 @@ final class BitMatrixParser { // Check the four corner cases if ((row == numRows) && (column == 0) && !corner1Read) { result[resultOffset++] = (byte) readCorner1(numRows, numColumns); - row -= 2; column +=2; + row -= 2; + column +=2; corner1Read = true; } else if ((row == numRows-2) && (column == 0) && ((numColumns & 0x03) != 0) && !corner2Read) { result[resultOffset++] = (byte) readCorner2(numRows, numColumns); - row -= 2; column +=2; + row -= 2; + column +=2; corner2Read = true; } else if ((row == numRows+4) && (column == 2) && ((numColumns & 0x07) == 0) && !corner3Read) { result[resultOffset++] = (byte) readCorner3(numRows, numColumns); - row -= 2; column +=2; + row -= 2; + column +=2; corner3Read = true; } else if ((row == numRows-2) && (column == 0) && ((numColumns & 0x07) == 4) && !corner4Read) { result[resultOffset++] = (byte) readCorner4(numRows, numColumns); - row -= 2; column +=2; + row -= 2; + column +=2; corner4Read = true; } else { // Sweep upward diagonally to the right do { - if ((row < numRows) && (column >= 0) && !readMappingMatrix.get(row, column)) { + if ((row < numRows) && (column >= 0) && !readMappingMatrix.get(column, row)) { result[resultOffset++] = (byte) readUtah(row, column, numRows, numColumns); } - row -= 2; column +=2; + row -= 2; + column +=2; } while ((row >= 0) && (column < numColumns)); - row += 1; column +=3; + row += 1; + column +=3; - // Sweep downward giagonally to the left + // Sweep downward diagonally to the left do { - if ((row >= 0) && (column < numColumns) && !readMappingMatrix.get(row, column)) { + if ((row >= 0) && (column < numColumns) && !readMappingMatrix.get(column, row)) { result[resultOffset++] = (byte) readUtah(row, column, numRows, numColumns); } - row += 2; column -=2; + row += 2; + column -=2; } while ((row < numRows) && (column >= 0)); - row += 3; column +=1; + row += 3; + column +=1; } } while ((row < numRows) || (column < numColumns)); if (resultOffset != version.getTotalCodewords()) { - throw new ReaderException("Did not read all codewords"); + throw FormatException.getFormatInstance(); } return result; } /** - *

Reads a bit of the mapping matrix accounting for boundry wrapping.

+ *

Reads a bit of the mapping matrix accounting for boundary wrapping.

* * @param row Row to read in the mapping matrix * @param column Column to read in the mapping matrix @@ -149,7 +152,7 @@ final class BitMatrixParser { * @return value of the given bit in the mapping matrix */ boolean readModule(int row, int column, int numRows, int numColumns) { - // Adjust the row and column indicies based on boundry wrapping + // Adjust the row and column indices based on boundary wrapping if (row < 0) { row += numRows; column += 4 - ((numRows + 4) & 0x07); @@ -158,12 +161,12 @@ final class BitMatrixParser { column += numColumns; row += 4 - ((numColumns + 4) & 0x07); } - readMappingMatrix.set(row, column); - return mappingBitMatrix.get(row, column); + readMappingMatrix.set(column, row); + return mappingBitMatrix.get(column, row); } /** - *

Reads the 8 bits of the standard utah shaped pattern.

+ *

Reads the 8 bits of the standard Utah-shaped pattern.

* *

See ISO 16022:2006, 5.8.1 Figure 6

* @@ -400,8 +403,7 @@ final class BitMatrixParser { int symbolSizeRows = version.getSymbolSizeRows(); int symbolSizeColumns = version.getSymbolSizeColumns(); - // TODO(bbrown): Make this work with rectangular codes - if (bitMatrix.getDimension() != symbolSizeRows) { + if (bitMatrix.getHeight() != symbolSizeRows) { throw new IllegalArgumentException("Dimension of bitMarix must match the version size"); } @@ -412,27 +414,26 @@ final class BitMatrixParser { int numDataRegionsColumn = symbolSizeColumns / dataRegionSizeColumns; int sizeDataRegionRow = numDataRegionsRow * dataRegionSizeRows; - //int sizeDataRegionColumn = numDataRegionsColumn * dataRegionSizeColumns; + int sizeDataRegionColumn = numDataRegionsColumn * dataRegionSizeColumns; - // TODO(bbrown): Make this work with rectangular codes - BitMatrix bitMatrixWithoutAlignment = new BitMatrix(sizeDataRegionRow); + BitMatrix bitMatrixWithoutAlignment = new BitMatrix(sizeDataRegionColumn, sizeDataRegionRow); for (int dataRegionRow = 0; dataRegionRow < numDataRegionsRow; ++dataRegionRow) { + int dataRegionRowOffset = dataRegionRow * dataRegionSizeRows; for (int dataRegionColumn = 0; dataRegionColumn < numDataRegionsColumn; ++dataRegionColumn) { + int dataRegionColumnOffset = dataRegionColumn * dataRegionSizeColumns; for (int i = 0; i < dataRegionSizeRows; ++i) { + int readRowOffset = dataRegionRow * (dataRegionSizeRows + 2) + 1 + i; + int writeRowOffset = dataRegionRowOffset + i; for (int j = 0; j < dataRegionSizeColumns; ++j) { - int readRowOffset = dataRegionRow * (dataRegionSizeRows + 2) + 1 + i; int readColumnOffset = dataRegionColumn * (dataRegionSizeColumns + 2) + 1 + j; - int writeRowOffset = dataRegionRow * dataRegionSizeRows + i; - int writeColumnOffset = dataRegionColumn * dataRegionSizeColumns + j; - - if (bitMatrix.get(readRowOffset, readColumnOffset)) { - bitMatrixWithoutAlignment.set(writeRowOffset, writeColumnOffset); + if (bitMatrix.get(readColumnOffset, readRowOffset)) { + int writeColumnOffset = dataRegionColumnOffset + j; + bitMatrixWithoutAlignment.set(writeColumnOffset, writeRowOffset); } } } } } - return bitMatrixWithoutAlignment; }