C# port, add datamatrix code
[zxing.git] / csharp / datamatrix / decoder / Decoder.cs
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 using System;\r
18 using System.Collections.Generic;\r
19 using System.Linq;\r
20 using System.Text;\r
21 using com.google.zxing.common;\r
22 using com.google.zxing.common.reedsolomon; \r
23 \r
24 namespace com.google.zxing.datamatrix.decoder\r
25 {\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 sealed class Decoder\r
34     {\r
35           private ReedSolomonDecoder rsDecoder;\r
36           public Decoder() {\r
37             rsDecoder = new ReedSolomonDecoder(GF256.DATA_MATRIX_FIELD);\r
38           }\r
39 \r
40           /**\r
41            * <p>Convenience method that can decode a Data Matrix Code represented as a 2D array of booleans.\r
42            * "true" is taken to mean a black module.</p>\r
43            *\r
44            * @param image booleans representing white/black Data Matrix Code modules\r
45            * @return text and bytes encoded within the Data Matrix Code\r
46            * @throws ReaderException if the Data Matrix Code cannot be decoded\r
47            */\r
48           public DecoderResult decode(bool[][] image) {\r
49             int dimension = image.Length;\r
50             BitMatrix bits = new BitMatrix(dimension);\r
51             for (int i = 0; i < dimension; i++) {\r
52               for (int j = 0; j < dimension; j++) {\r
53                 if (image[i][j]) {\r
54                   bits.set(i, j);\r
55                 }\r
56               }\r
57             }\r
58             return decode(bits);\r
59           }\r
60 \r
61           /**\r
62            * <p>Decodes a Data Matrix Code represented as a {@link BitMatrix}. A 1 or "true" is taken\r
63            * to mean a black module.</p>\r
64            *\r
65            * @param bits booleans representing white/black Data Matrix Code modules\r
66            * @return text and bytes encoded within the Data Matrix Code\r
67            * @throws ReaderException if the Data Matrix Code cannot be decoded\r
68            */\r
69           public DecoderResult decode(BitMatrix bits) {\r
70 \r
71             // Construct a parser and read version, error-correction level\r
72             BitMatrixParser parser = new BitMatrixParser(bits);\r
73             Version version = parser.readVersion(bits);\r
74 \r
75             // Read codewords\r
76             sbyte[] codewords = parser.readCodewords();\r
77             // Separate into data blocks\r
78             DataBlock[] dataBlocks = DataBlock.getDataBlocks(codewords, version);\r
79 \r
80             // Count total number of data bytes\r
81             int totalBytes = 0;\r
82             for (int i = 0; i < dataBlocks.Length; i++) {\r
83               totalBytes += dataBlocks[i].getNumDataCodewords();\r
84             }\r
85             sbyte[] resultBytes = new sbyte[totalBytes];\r
86             int resultOffset = 0;\r
87 \r
88             // Error-correct and copy data blocks together into a stream of bytes\r
89             for (int j = 0; j < dataBlocks.Length; j++) {\r
90               DataBlock dataBlock = dataBlocks[j];\r
91               sbyte[] codewordBytes = dataBlock.getCodewords();\r
92               int numDataCodewords = dataBlock.getNumDataCodewords();\r
93               correctErrors(codewordBytes, numDataCodewords);\r
94               for (int i = 0; i < numDataCodewords; i++) {\r
95                 resultBytes[resultOffset++] = codewordBytes[i];\r
96               }\r
97             }\r
98 \r
99             // Decode the contents of that stream of bytes\r
100             return DecodedBitStreamParser.decode(resultBytes);\r
101           }\r
102 \r
103           /**\r
104            * <p>Given data and error-correction codewords received, possibly corrupted by errors, attempts to\r
105            * correct the errors in-place using Reed-Solomon error correction.</p>\r
106            *\r
107            * @param codewordBytes data and error correction codewords\r
108            * @param numDataCodewords number of codewords that are data bytes\r
109            * @throws ReaderException if error correction fails\r
110            */\r
111           private void correctErrors(sbyte[] codewordBytes, int numDataCodewords) {\r
112             int numCodewords = codewordBytes.Length;\r
113             // First read into an array of ints\r
114             int[] codewordsInts = new int[numCodewords];\r
115             for (int i = 0; i < numCodewords; i++) {\r
116               codewordsInts[i] = codewordBytes[i] & 0xFF;\r
117             }\r
118             int numECCodewords = codewordBytes.Length - numDataCodewords;\r
119             try {\r
120               rsDecoder.decode(codewordsInts, numECCodewords);\r
121             } catch (ReedSolomonException rse) {\r
122               throw new ReaderException();\r
123             }\r
124             // Copy back into array of bytes -- only need to worry about the bytes that were data\r
125             // We don't care about errors in the error-correction codewords\r
126             for (int i = 0; i < numDataCodewords; i++) {\r
127               codewordBytes[i] = (sbyte) codewordsInts[i];\r
128             }\r
129           }\r
130 \r
131     }\r
132 }\r