"Split" ReaderException into subclasses to enable more useful error reporting
[zxing.git] / core / src / com / google / zxing / datamatrix / decoder / Decoder.java
1 /*\r
2  * Copyright 2007 ZXing authors\r
3  *\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
7  *\r
8  *      http://www.apache.org/licenses/LICENSE-2.0\r
9  *\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
15  */\r
16 \r
17 package com.google.zxing.datamatrix.decoder;\r
18 \r
19 import com.google.zxing.ChecksumException;\r
20 import com.google.zxing.FormatException;\r
21 import com.google.zxing.common.BitMatrix;\r
22 import com.google.zxing.common.DecoderResult;\r
23 import com.google.zxing.common.reedsolomon.GF256;\r
24 import com.google.zxing.common.reedsolomon.ReedSolomonDecoder;\r
25 import com.google.zxing.common.reedsolomon.ReedSolomonException;\r
26 \r
27 /**\r
28  * <p>The main class which implements Data Matrix Code decoding -- as opposed to locating and extracting\r
29  * the Data Matrix Code from an image.</p>\r
30  *\r
31  * @author bbrown@google.com (Brian Brown)\r
32  */\r
33 public final class Decoder {\r
34 \r
35   private final ReedSolomonDecoder rsDecoder;\r
36 \r
37   public Decoder() {\r
38     rsDecoder = new ReedSolomonDecoder(GF256.DATA_MATRIX_FIELD);\r
39   }\r
40 \r
41   /**\r
42    * <p>Convenience method that can decode a Data Matrix Code represented as a 2D array of booleans.\r
43    * "true" is taken to mean a black module.</p>\r
44    *\r
45    * @param image booleans representing white/black Data Matrix Code modules\r
46    * @return text and bytes encoded within the Data Matrix Code\r
47    * @throws FormatException if the Data Matrix Code cannot be decoded\r
48    * @throws ChecksumException if error correction fails\r
49    */\r
50   public DecoderResult decode(boolean[][] image) throws FormatException, ChecksumException {\r
51     int dimension = image.length;\r
52     BitMatrix bits = new BitMatrix(dimension);\r
53     for (int i = 0; i < dimension; i++) {\r
54       for (int j = 0; j < dimension; j++) {\r
55         if (image[i][j]) {\r
56           bits.set(j, i);\r
57         }\r
58       }\r
59     }\r
60     return decode(bits);\r
61   }\r
62 \r
63   /**\r
64    * <p>Decodes a Data Matrix Code represented as a {@link BitMatrix}. A 1 or "true" is taken\r
65    * to mean a black module.</p>\r
66    *\r
67    * @param bits booleans representing white/black Data Matrix Code modules\r
68    * @return text and bytes encoded within the Data Matrix Code\r
69    * @throws FormatException if the Data Matrix Code cannot be decoded\r
70    * @throws ChecksumException if error correction fails\r
71    */\r
72   public DecoderResult decode(BitMatrix bits) throws FormatException, ChecksumException {\r
73 \r
74     // Construct a parser and read version, error-correction level\r
75     BitMatrixParser parser = new BitMatrixParser(bits);\r
76     Version version = parser.readVersion(bits);\r
77 \r
78     // Read codewords\r
79     byte[] codewords = parser.readCodewords();\r
80     // Separate into data blocks\r
81     DataBlock[] dataBlocks = DataBlock.getDataBlocks(codewords, version);\r
82 \r
83     // Count total number of data bytes\r
84     int totalBytes = 0;\r
85     for (int i = 0; i < dataBlocks.length; i++) {\r
86       totalBytes += dataBlocks[i].getNumDataCodewords();\r
87     }\r
88     byte[] resultBytes = new byte[totalBytes];\r
89     int resultOffset = 0;\r
90 \r
91     // Error-correct and copy data blocks together into a stream of bytes\r
92     for (int j = 0; j < dataBlocks.length; j++) {\r
93       DataBlock dataBlock = dataBlocks[j];\r
94       byte[] codewordBytes = dataBlock.getCodewords();\r
95       int numDataCodewords = dataBlock.getNumDataCodewords();\r
96       correctErrors(codewordBytes, numDataCodewords);\r
97       for (int i = 0; i < numDataCodewords; i++) {\r
98         resultBytes[resultOffset++] = codewordBytes[i];\r
99       }\r
100     }\r
101 \r
102     // Decode the contents of that stream of bytes\r
103     return DecodedBitStreamParser.decode(resultBytes);\r
104   }\r
105 \r
106   /**\r
107    * <p>Given data and error-correction codewords received, possibly corrupted by errors, attempts to\r
108    * correct the errors in-place using Reed-Solomon error correction.</p>\r
109    *\r
110    * @param codewordBytes data and error correction codewords\r
111    * @param numDataCodewords number of codewords that are data bytes\r
112    * @throws ChecksumException if error correction fails\r
113    */\r
114   private void correctErrors(byte[] codewordBytes, int numDataCodewords) throws ChecksumException {\r
115     int numCodewords = codewordBytes.length;\r
116     // First read into an array of ints\r
117     int[] codewordsInts = new int[numCodewords];\r
118     for (int i = 0; i < numCodewords; i++) {\r
119       codewordsInts[i] = codewordBytes[i] & 0xFF;\r
120     }\r
121     int numECCodewords = codewordBytes.length - numDataCodewords;\r
122     try {\r
123       rsDecoder.decode(codewordsInts, numECCodewords);\r
124     } catch (ReedSolomonException rse) {\r
125       throw ChecksumException.getChecksumInstance();\r
126     }\r
127     // Copy back into array of bytes -- only need to worry about the bytes that were data\r
128     // We don't care about errors in the error-correction codewords\r
129     for (int i = 0; i < numDataCodewords; i++) {\r
130       codewordBytes[i] = (byte) codewordsInts[i];\r
131     }\r
132   }\r
133 \r
134 }\r