2 * Copyright 2007 ZXing authors
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 package com.google.zxing.datamatrix.decoder;
\r
19 import com.google.zxing.FormatException;
\r
20 import com.google.zxing.common.BitMatrix;
\r
23 * @author bbrown@google.com (Brian Brown)
\r
25 final class BitMatrixParser {
\r
27 private final BitMatrix mappingBitMatrix;
\r
28 private final BitMatrix readMappingMatrix;
\r
29 private final Version version;
\r
32 * @param bitMatrix {@link BitMatrix} to parse
\r
33 * @throws FormatException if dimension is < 8 or > 144 or not 0 mod 2
\r
35 BitMatrixParser(BitMatrix bitMatrix) throws FormatException {
\r
36 int dimension = bitMatrix.getHeight();
\r
37 if (dimension < 8 || dimension > 144 || (dimension & 0x01) != 0) {
\r
38 throw FormatException.getFormatInstance();
\r
41 version = readVersion(bitMatrix);
\r
42 this.mappingBitMatrix = extractDataRegion(bitMatrix);
\r
43 this.readMappingMatrix = new BitMatrix(this.mappingBitMatrix.getWidth(), this.mappingBitMatrix.getHeight());
\r
47 * <p>Creates the version object based on the dimension of the original bit matrix from
\r
48 * the datamatrix code.</p>
\r
50 * <p>See ISO 16022:2006 Table 7 - ECC 200 symbol attributes</p>
\r
52 * @param bitMatrix Original {@link BitMatrix} including alignment patterns
\r
53 * @return {@link Version} encapsulating the Data Matrix Code's "version"
\r
54 * @throws FormatException if the dimensions of the mapping matrix are not valid
\r
55 * Data Matrix dimensions.
\r
57 Version readVersion(BitMatrix bitMatrix) throws FormatException {
\r
59 if (version != null) {
\r
63 int numRows = bitMatrix.getHeight();
\r
64 int numColumns = bitMatrix.getWidth();
\r
66 return Version.getVersionForDimensions(numRows, numColumns);
\r
70 * <p>Reads the bits in the {@link BitMatrix} representing the mapping matrix (No alignment patterns)
\r
71 * in the correct order in order to reconstitute the codewords bytes contained within the
\r
72 * Data Matrix Code.</p>
\r
74 * @return bytes encoded within the Data Matrix Code
\r
75 * @throws FormatException if the exact number of bytes expected is not read
\r
77 byte[] readCodewords() throws FormatException {
\r
79 byte[] result = new byte[version.getTotalCodewords()];
\r
80 int resultOffset = 0;
\r
85 int numRows = mappingBitMatrix.getHeight();
\r
86 int numColumns = mappingBitMatrix.getWidth();
\r
88 boolean corner1Read = false;
\r
89 boolean corner2Read = false;
\r
90 boolean corner3Read = false;
\r
91 boolean corner4Read = false;
\r
93 // Read all of the codewords
\r
95 // Check the four corner cases
\r
96 if ((row == numRows) && (column == 0) && !corner1Read) {
\r
97 result[resultOffset++] = (byte) readCorner1(numRows, numColumns);
\r
100 corner1Read = true;
\r
101 } else if ((row == numRows-2) && (column == 0) && ((numColumns & 0x03) != 0) && !corner2Read) {
\r
102 result[resultOffset++] = (byte) readCorner2(numRows, numColumns);
\r
105 corner2Read = true;
\r
106 } else if ((row == numRows+4) && (column == 2) && ((numColumns & 0x07) == 0) && !corner3Read) {
\r
107 result[resultOffset++] = (byte) readCorner3(numRows, numColumns);
\r
110 corner3Read = true;
\r
111 } else if ((row == numRows-2) && (column == 0) && ((numColumns & 0x07) == 4) && !corner4Read) {
\r
112 result[resultOffset++] = (byte) readCorner4(numRows, numColumns);
\r
115 corner4Read = true;
\r
117 // Sweep upward diagonally to the right
\r
119 if ((row < numRows) && (column >= 0) && !readMappingMatrix.get(column, row)) {
\r
120 result[resultOffset++] = (byte) readUtah(row, column, numRows, numColumns);
\r
124 } while ((row >= 0) && (column < numColumns));
\r
128 // Sweep downward diagonally to the left
\r
130 if ((row >= 0) && (column < numColumns) && !readMappingMatrix.get(column, row)) {
\r
131 result[resultOffset++] = (byte) readUtah(row, column, numRows, numColumns);
\r
135 } while ((row < numRows) && (column >= 0));
\r
139 } while ((row < numRows) || (column < numColumns));
\r
141 if (resultOffset != version.getTotalCodewords()) {
\r
142 throw FormatException.getFormatInstance();
\r
148 * <p>Reads a bit of the mapping matrix accounting for boundary wrapping.</p>
\r
150 * @param row Row to read in the mapping matrix
\r
151 * @param column Column to read in the mapping matrix
\r
152 * @param numRows Number of rows in the mapping matrix
\r
153 * @param numColumns Number of columns in the mapping matrix
\r
154 * @return value of the given bit in the mapping matrix
\r
156 boolean readModule(int row, int column, int numRows, int numColumns) {
\r
157 // Adjust the row and column indices based on boundary wrapping
\r
160 column += 4 - ((numRows + 4) & 0x07);
\r
163 column += numColumns;
\r
164 row += 4 - ((numColumns + 4) & 0x07);
\r
166 readMappingMatrix.set(column, row);
\r
167 return mappingBitMatrix.get(column, row);
\r
171 * <p>Reads the 8 bits of the standard Utah-shaped pattern.</p>
\r
173 * <p>See ISO 16022:2006, 5.8.1 Figure 6</p>
\r
175 * @param row Current row in the mapping matrix, anchored at the 8th bit (LSB) of the pattern
\r
176 * @param column Current column in the mapping matrix, anchored at the 8th bit (LSB) of the pattern
\r
177 * @param numRows Number of rows in the mapping matrix
\r
178 * @param numColumns Number of columns in the mapping matrix
\r
179 * @return byte from the utah shape
\r
181 int readUtah(int row, int column, int numRows, int numColumns) {
\r
182 int currentByte = 0;
\r
183 if (readModule(row - 2, column - 2, numRows, numColumns)) {
\r
187 if (readModule(row - 2, column - 1, numRows, numColumns)) {
\r
191 if (readModule(row - 1, column - 2, numRows, numColumns)) {
\r
195 if (readModule(row - 1, column - 1, numRows, numColumns)) {
\r
199 if (readModule(row - 1, column, numRows, numColumns)) {
\r
203 if (readModule(row, column - 2, numRows, numColumns)) {
\r
207 if (readModule(row, column - 1, numRows, numColumns)) {
\r
211 if (readModule(row, column, numRows, numColumns)) {
\r
214 return currentByte;
\r
218 * <p>Reads the 8 bits of the special corner condition 1.</p>
\r
220 * <p>See ISO 16022:2006, Figure F.3</p>
\r
222 * @param numRows Number of rows in the mapping matrix
\r
223 * @param numColumns Number of columns in the mapping matrix
\r
224 * @return byte from the Corner condition 1
\r
226 int readCorner1(int numRows, int numColumns) {
\r
227 int currentByte = 0;
\r
228 if (readModule(numRows - 1, 0, numRows, numColumns)) {
\r
232 if (readModule(numRows - 1, 1, numRows, numColumns)) {
\r
236 if (readModule(numRows - 1, 2, numRows, numColumns)) {
\r
240 if (readModule(0, numColumns - 2, numRows, numColumns)) {
\r
244 if (readModule(0, numColumns - 1, numRows, numColumns)) {
\r
248 if (readModule(1, numColumns - 1, numRows, numColumns)) {
\r
252 if (readModule(2, numColumns - 1, numRows, numColumns)) {
\r
256 if (readModule(3, numColumns - 1, numRows, numColumns)) {
\r
259 return currentByte;
\r
263 * <p>Reads the 8 bits of the special corner condition 2.</p>
\r
265 * <p>See ISO 16022:2006, Figure F.4</p>
\r
267 * @param numRows Number of rows in the mapping matrix
\r
268 * @param numColumns Number of columns in the mapping matrix
\r
269 * @return byte from the Corner condition 2
\r
271 int readCorner2(int numRows, int numColumns) {
\r
272 int currentByte = 0;
\r
273 if (readModule(numRows - 3, 0, numRows, numColumns)) {
\r
277 if (readModule(numRows - 2, 0, numRows, numColumns)) {
\r
281 if (readModule(numRows - 1, 0, numRows, numColumns)) {
\r
285 if (readModule(0, numColumns - 4, numRows, numColumns)) {
\r
289 if (readModule(0, numColumns - 3, numRows, numColumns)) {
\r
293 if (readModule(0, numColumns - 2, numRows, numColumns)) {
\r
297 if (readModule(0, numColumns - 1, numRows, numColumns)) {
\r
301 if (readModule(1, numColumns - 1, numRows, numColumns)) {
\r
304 return currentByte;
\r
308 * <p>Reads the 8 bits of the special corner condition 3.</p>
\r
310 * <p>See ISO 16022:2006, Figure F.5</p>
\r
312 * @param numRows Number of rows in the mapping matrix
\r
313 * @param numColumns Number of columns in the mapping matrix
\r
314 * @return byte from the Corner condition 3
\r
316 int readCorner3(int numRows, int numColumns) {
\r
317 int currentByte = 0;
\r
318 if (readModule(numRows - 1, 0, numRows, numColumns)) {
\r
322 if (readModule(numRows - 1, numColumns - 1, numRows, numColumns)) {
\r
326 if (readModule(0, numColumns - 3, numRows, numColumns)) {
\r
330 if (readModule(0, numColumns - 2, numRows, numColumns)) {
\r
334 if (readModule(0, numColumns - 1, numRows, numColumns)) {
\r
338 if (readModule(1, numColumns - 3, numRows, numColumns)) {
\r
342 if (readModule(1, numColumns - 2, numRows, numColumns)) {
\r
346 if (readModule(1, numColumns - 1, numRows, numColumns)) {
\r
349 return currentByte;
\r
353 * <p>Reads the 8 bits of the special corner condition 4.</p>
\r
355 * <p>See ISO 16022:2006, Figure F.6</p>
\r
357 * @param numRows Number of rows in the mapping matrix
\r
358 * @param numColumns Number of columns in the mapping matrix
\r
359 * @return byte from the Corner condition 4
\r
361 int readCorner4(int numRows, int numColumns) {
\r
362 int currentByte = 0;
\r
363 if (readModule(numRows - 3, 0, numRows, numColumns)) {
\r
367 if (readModule(numRows - 2, 0, numRows, numColumns)) {
\r
371 if (readModule(numRows - 1, 0, numRows, numColumns)) {
\r
375 if (readModule(0, numColumns - 2, numRows, numColumns)) {
\r
379 if (readModule(0, numColumns - 1, numRows, numColumns)) {
\r
383 if (readModule(1, numColumns - 1, numRows, numColumns)) {
\r
387 if (readModule(2, numColumns - 1, numRows, numColumns)) {
\r
391 if (readModule(3, numColumns - 1, numRows, numColumns)) {
\r
394 return currentByte;
\r
398 * <p>Extracts the data region from a {@link BitMatrix} that contains
\r
399 * alignment patterns.</p>
\r
401 * @param bitMatrix Original {@link BitMatrix} with alignment patterns
\r
402 * @return BitMatrix that has the alignment patterns removed
\r
404 BitMatrix extractDataRegion(BitMatrix bitMatrix) {
\r
405 int symbolSizeRows = version.getSymbolSizeRows();
\r
406 int symbolSizeColumns = version.getSymbolSizeColumns();
\r
408 if (bitMatrix.getHeight() != symbolSizeRows) {
\r
409 throw new IllegalArgumentException("Dimension of bitMarix must match the version size");
\r
412 int dataRegionSizeRows = version.getDataRegionSizeRows();
\r
413 int dataRegionSizeColumns = version.getDataRegionSizeColumns();
\r
415 int numDataRegionsRow = symbolSizeRows / dataRegionSizeRows;
\r
416 int numDataRegionsColumn = symbolSizeColumns / dataRegionSizeColumns;
\r
418 int sizeDataRegionRow = numDataRegionsRow * dataRegionSizeRows;
\r
419 int sizeDataRegionColumn = numDataRegionsColumn * dataRegionSizeColumns;
\r
421 BitMatrix bitMatrixWithoutAlignment = new BitMatrix(sizeDataRegionColumn, sizeDataRegionRow);
\r
422 for (int dataRegionRow = 0; dataRegionRow < numDataRegionsRow; ++dataRegionRow) {
\r
423 int dataRegionRowOffset = dataRegionRow * dataRegionSizeRows;
\r
424 for (int dataRegionColumn = 0; dataRegionColumn < numDataRegionsColumn; ++dataRegionColumn) {
\r
425 int dataRegionColumnOffset = dataRegionColumn * dataRegionSizeColumns;
\r
426 for (int i = 0; i < dataRegionSizeRows; ++i) {
\r
427 int readRowOffset = dataRegionRow * (dataRegionSizeRows + 2) + 1 + i;
\r
428 int writeRowOffset = dataRegionRowOffset + i;
\r
429 for (int j = 0; j < dataRegionSizeColumns; ++j) {
\r
430 int readColumnOffset = dataRegionColumn * (dataRegionSizeColumns + 2) + 1 + j;
\r
431 if (bitMatrix.get(readColumnOffset, readRowOffset)) {
\r
432 int writeColumnOffset = dataRegionColumnOffset + j;
\r
433 bitMatrixWithoutAlignment.set(writeColumnOffset, writeRowOffset);
\r
439 return bitMatrixWithoutAlignment;
\r