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 using ReaderException = com.google.zxing.ReaderException;
\r
18 using BitMatrix = com.google.zxing.common.BitMatrix;
\r
19 namespace com.google.zxing.datamatrix.decoder
\r
22 /// <author> bbrown@google.com (Brian Brown)
\r
24 /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source
\r
26 sealed class BitMatrixParser
\r
29 //UPGRADE_NOTE: Final was removed from the declaration of 'mappingBitMatrix '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
\r
30 private BitMatrix mappingBitMatrix;
\r
31 //UPGRADE_NOTE: Final was removed from the declaration of 'readMappingMatrix '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
\r
32 private BitMatrix readMappingMatrix;
\r
33 //UPGRADE_NOTE: Final was removed from the declaration of 'version '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
\r
34 private Version version;
\r
36 /// <param name="bitMatrix">{@link BitMatrix} to parse
\r
38 /// <throws> ReaderException if dimension is < 10 or > 144 or not 0 mod 2 </throws>
\r
39 internal BitMatrixParser(BitMatrix bitMatrix)
\r
41 int dimension = bitMatrix.Dimension;
\r
42 if (dimension < 10 || dimension > 144 || (dimension & 0x01) != 0)
\r
44 throw ReaderException.Instance;
\r
47 version = readVersion(bitMatrix);
\r
48 this.mappingBitMatrix = extractDataRegion(bitMatrix);
\r
49 // TODO(bbrown): Make this work for rectangular symbols
\r
50 this.readMappingMatrix = new BitMatrix(this.mappingBitMatrix.Dimension);
\r
53 /// <summary> <p>Creates the version object based on the dimension of the original bit matrix from
\r
54 /// the datamatrix code.</p>
\r
56 /// <p>See ISO 16022:2006 Table 7 - ECC 200 symbol attributes</p>
\r
59 /// <param name="bitMatrix">Original {@link BitMatrix} including alignment patterns
\r
61 /// <returns> {@link Version} encapsulating the Data Matrix Code's "version"
\r
63 /// <throws> ReaderException if the dimensions of the mapping matrix are not valid </throws>
\r
64 /// <summary> Data Matrix dimensions.
\r
66 internal Version readVersion(BitMatrix bitMatrix)
\r
69 if (version != null)
\r
74 // TODO(bbrown): make this work for rectangular dimensions as well.
\r
75 int numRows = bitMatrix.Dimension;
\r
76 int numColumns = numRows;
\r
78 return Version.getVersionForDimensions(numRows, numColumns);
\r
81 /// <summary> <p>Reads the bits in the {@link BitMatrix} representing the mapping matrix (No alignment patterns)
\r
82 /// in the correct order in order to reconstitute the codewords bytes contained within the
\r
83 /// Data Matrix Code.</p>
\r
86 /// <returns> bytes encoded within the Data Matrix Code
\r
88 /// <throws> ReaderException if the exact number of bytes expected is not read </throws>
\r
89 internal sbyte[] readCodewords()
\r
92 sbyte[] result = new sbyte[version.TotalCodewords];
\r
93 int resultOffset = 0;
\r
97 // TODO(bbrown): Data Matrix can be rectangular, assuming square for now
\r
98 int numRows = mappingBitMatrix.Dimension;
\r
99 int numColumns = numRows;
\r
101 bool corner1Read = false;
\r
102 bool corner2Read = false;
\r
103 bool corner3Read = false;
\r
104 bool corner4Read = false;
\r
106 // Read all of the codewords
\r
109 // Check the four corner cases
\r
110 if ((row == numRows) && (column == 0) && !corner1Read)
\r
112 result[resultOffset++] = (sbyte) readCorner1(numRows, numColumns);
\r
115 corner1Read = true;
\r
117 else if ((row == numRows - 2) && (column == 0) && ((numColumns & 0x03) != 0) && !corner2Read)
\r
119 result[resultOffset++] = (sbyte) readCorner2(numRows, numColumns);
\r
122 corner2Read = true;
\r
124 else if ((row == numRows + 4) && (column == 2) && ((numColumns & 0x07) == 0) && !corner3Read)
\r
126 result[resultOffset++] = (sbyte) readCorner3(numRows, numColumns);
\r
129 corner3Read = true;
\r
131 else if ((row == numRows - 2) && (column == 0) && ((numColumns & 0x07) == 4) && !corner4Read)
\r
133 result[resultOffset++] = (sbyte) readCorner4(numRows, numColumns);
\r
136 corner4Read = true;
\r
140 // Sweep upward diagonally to the right
\r
143 if ((row < numRows) && (column >= 0) && !readMappingMatrix.get_Renamed(column, row))
\r
145 result[resultOffset++] = (sbyte) readUtah(row, column, numRows, numColumns);
\r
150 while ((row >= 0) && (column < numColumns));
\r
154 // Sweep downward diagonally to the left
\r
157 if ((row >= 0) && (column < numColumns) && !readMappingMatrix.get_Renamed(column, row))
\r
159 result[resultOffset++] = (sbyte) readUtah(row, column, numRows, numColumns);
\r
164 while ((row < numRows) && (column >= 0));
\r
169 while ((row < numRows) || (column < numColumns));
\r
171 if (resultOffset != version.TotalCodewords)
\r
173 throw ReaderException.Instance;
\r
178 /// <summary> <p>Reads a bit of the mapping matrix accounting for boundary wrapping.</p>
\r
181 /// <param name="row">Row to read in the mapping matrix
\r
183 /// <param name="column">Column to read in the mapping matrix
\r
185 /// <param name="numRows">Number of rows in the mapping matrix
\r
187 /// <param name="numColumns">Number of columns in the mapping matrix
\r
189 /// <returns> value of the given bit in the mapping matrix
\r
191 internal bool readModule(int row, int column, int numRows, int numColumns)
\r
193 // Adjust the row and column indices based on boundary wrapping
\r
197 column += 4 - ((numRows + 4) & 0x07);
\r
201 column += numColumns;
\r
202 row += 4 - ((numColumns + 4) & 0x07);
\r
204 readMappingMatrix.set_Renamed(column, row);
\r
205 return mappingBitMatrix.get_Renamed(column, row);
\r
208 /// <summary> <p>Reads the 8 bits of the standard Utah-shaped pattern.</p>
\r
210 /// <p>See ISO 16022:2006, 5.8.1 Figure 6</p>
\r
213 /// <param name="row">Current row in the mapping matrix, anchored at the 8th bit (LSB) of the pattern
\r
215 /// <param name="column">Current column in the mapping matrix, anchored at the 8th bit (LSB) of the pattern
\r
217 /// <param name="numRows">Number of rows in the mapping matrix
\r
219 /// <param name="numColumns">Number of columns in the mapping matrix
\r
221 /// <returns> byte from the utah shape
\r
223 internal int readUtah(int row, int column, int numRows, int numColumns)
\r
225 int currentByte = 0;
\r
226 if (readModule(row - 2, column - 2, numRows, numColumns))
\r
231 if (readModule(row - 2, column - 1, numRows, numColumns))
\r
236 if (readModule(row - 1, column - 2, numRows, numColumns))
\r
241 if (readModule(row - 1, column - 1, numRows, numColumns))
\r
246 if (readModule(row - 1, column, numRows, numColumns))
\r
251 if (readModule(row, column - 2, numRows, numColumns))
\r
256 if (readModule(row, column - 1, numRows, numColumns))
\r
261 if (readModule(row, column, numRows, numColumns))
\r
265 return currentByte;
\r
268 /// <summary> <p>Reads the 8 bits of the special corner condition 1.</p>
\r
270 /// <p>See ISO 16022:2006, Figure F.3</p>
\r
273 /// <param name="numRows">Number of rows in the mapping matrix
\r
275 /// <param name="numColumns">Number of columns in the mapping matrix
\r
277 /// <returns> byte from the Corner condition 1
\r
279 internal int readCorner1(int numRows, int numColumns)
\r
281 int currentByte = 0;
\r
282 if (readModule(numRows - 1, 0, numRows, numColumns))
\r
287 if (readModule(numRows - 1, 1, numRows, numColumns))
\r
292 if (readModule(numRows - 1, 2, numRows, numColumns))
\r
297 if (readModule(0, numColumns - 2, numRows, numColumns))
\r
302 if (readModule(0, numColumns - 1, numRows, numColumns))
\r
307 if (readModule(1, numColumns - 1, numRows, numColumns))
\r
312 if (readModule(2, numColumns - 1, numRows, numColumns))
\r
317 if (readModule(3, numColumns - 1, numRows, numColumns))
\r
321 return currentByte;
\r
324 /// <summary> <p>Reads the 8 bits of the special corner condition 2.</p>
\r
326 /// <p>See ISO 16022:2006, Figure F.4</p>
\r
329 /// <param name="numRows">Number of rows in the mapping matrix
\r
331 /// <param name="numColumns">Number of columns in the mapping matrix
\r
333 /// <returns> byte from the Corner condition 2
\r
335 internal int readCorner2(int numRows, int numColumns)
\r
337 int currentByte = 0;
\r
338 if (readModule(numRows - 3, 0, numRows, numColumns))
\r
343 if (readModule(numRows - 2, 0, numRows, numColumns))
\r
348 if (readModule(numRows - 1, 0, numRows, numColumns))
\r
353 if (readModule(0, numColumns - 4, numRows, numColumns))
\r
358 if (readModule(0, numColumns - 3, numRows, numColumns))
\r
363 if (readModule(0, numColumns - 2, numRows, numColumns))
\r
368 if (readModule(0, numColumns - 1, numRows, numColumns))
\r
373 if (readModule(1, numColumns - 1, numRows, numColumns))
\r
377 return currentByte;
\r
380 /// <summary> <p>Reads the 8 bits of the special corner condition 3.</p>
\r
382 /// <p>See ISO 16022:2006, Figure F.5</p>
\r
385 /// <param name="numRows">Number of rows in the mapping matrix
\r
387 /// <param name="numColumns">Number of columns in the mapping matrix
\r
389 /// <returns> byte from the Corner condition 3
\r
391 internal int readCorner3(int numRows, int numColumns)
\r
393 int currentByte = 0;
\r
394 if (readModule(numRows - 1, 0, numRows, numColumns))
\r
399 if (readModule(numRows - 1, numColumns - 1, numRows, numColumns))
\r
404 if (readModule(0, numColumns - 3, numRows, numColumns))
\r
409 if (readModule(0, numColumns - 2, numRows, numColumns))
\r
414 if (readModule(0, numColumns - 1, numRows, numColumns))
\r
419 if (readModule(1, numColumns - 3, numRows, numColumns))
\r
424 if (readModule(1, numColumns - 2, numRows, numColumns))
\r
429 if (readModule(1, numColumns - 1, numRows, numColumns))
\r
433 return currentByte;
\r
436 /// <summary> <p>Reads the 8 bits of the special corner condition 4.</p>
\r
438 /// <p>See ISO 16022:2006, Figure F.6</p>
\r
441 /// <param name="numRows">Number of rows in the mapping matrix
\r
443 /// <param name="numColumns">Number of columns in the mapping matrix
\r
445 /// <returns> byte from the Corner condition 4
\r
447 internal int readCorner4(int numRows, int numColumns)
\r
449 int currentByte = 0;
\r
450 if (readModule(numRows - 3, 0, numRows, numColumns))
\r
455 if (readModule(numRows - 2, 0, numRows, numColumns))
\r
460 if (readModule(numRows - 1, 0, numRows, numColumns))
\r
465 if (readModule(0, numColumns - 2, numRows, numColumns))
\r
470 if (readModule(0, numColumns - 1, numRows, numColumns))
\r
475 if (readModule(1, numColumns - 1, numRows, numColumns))
\r
480 if (readModule(2, numColumns - 1, numRows, numColumns))
\r
485 if (readModule(3, numColumns - 1, numRows, numColumns))
\r
489 return currentByte;
\r
492 /// <summary> <p>Extracts the data region from a {@link BitMatrix} that contains
\r
493 /// alignment patterns.</p>
\r
496 /// <param name="bitMatrix">Original {@link BitMatrix} with alignment patterns
\r
498 /// <returns> BitMatrix that has the alignment patterns removed
\r
500 internal BitMatrix extractDataRegion(BitMatrix bitMatrix)
\r
502 int symbolSizeRows = version.SymbolSizeRows;
\r
503 int symbolSizeColumns = version.SymbolSizeColumns;
\r
505 // TODO(bbrown): Make this work with rectangular codes
\r
506 if (bitMatrix.Dimension != symbolSizeRows)
\r
508 throw new System.ArgumentException("Dimension of bitMarix must match the version size");
\r
511 int dataRegionSizeRows = version.DataRegionSizeRows;
\r
512 int dataRegionSizeColumns = version.DataRegionSizeColumns;
\r
514 int numDataRegionsRow = symbolSizeRows / dataRegionSizeRows;
\r
515 int numDataRegionsColumn = symbolSizeColumns / dataRegionSizeColumns;
\r
517 int sizeDataRegionRow = numDataRegionsRow * dataRegionSizeRows;
\r
518 //int sizeDataRegionColumn = numDataRegionsColumn * dataRegionSizeColumns;
\r
520 // TODO(bbrown): Make this work with rectangular codes
\r
521 BitMatrix bitMatrixWithoutAlignment = new BitMatrix(sizeDataRegionRow);
\r
522 for (int dataRegionRow = 0; dataRegionRow < numDataRegionsRow; ++dataRegionRow)
\r
524 int dataRegionRowOffset = dataRegionRow * dataRegionSizeRows;
\r
525 for (int dataRegionColumn = 0; dataRegionColumn < numDataRegionsColumn; ++dataRegionColumn)
\r
527 int dataRegionColumnOffset = dataRegionColumn * dataRegionSizeColumns;
\r
528 for (int i = 0; i < dataRegionSizeRows; ++i)
\r
530 int readRowOffset = dataRegionRow * (dataRegionSizeRows + 2) + 1 + i;
\r
531 int writeRowOffset = dataRegionRowOffset + i;
\r
532 for (int j = 0; j < dataRegionSizeColumns; ++j)
\r
534 int readColumnOffset = dataRegionColumn * (dataRegionSizeColumns + 2) + 1 + j;
\r
535 if (bitMatrix.get_Renamed(readColumnOffset, readRowOffset))
\r
537 int writeColumnOffset = dataRegionColumnOffset + j;
\r
538 bitMatrixWithoutAlignment.set_Renamed(writeColumnOffset, writeRowOffset);
\r
544 return bitMatrixWithoutAlignment;
\r