New C# port from Suraj Supekar
[zxing.git] / csharp / datamatrix / decoder / Decoder.cs
diff --git a/csharp/datamatrix/decoder/Decoder.cs b/csharp/datamatrix/decoder/Decoder.cs
new file mode 100755 (executable)
index 0000000..4901ceb
--- /dev/null
@@ -0,0 +1,153 @@
+/*\r
+* Copyright 2007 ZXing authors\r
+*\r
+* Licensed under the Apache License, Version 2.0 (the "License");\r
+* you may not use this file except in compliance with the License.\r
+* You may obtain a copy of the License at\r
+*\r
+*      http://www.apache.org/licenses/LICENSE-2.0\r
+*\r
+* Unless required by applicable law or agreed to in writing, software\r
+* distributed under the License is distributed on an "AS IS" BASIS,\r
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+* See the License for the specific language governing permissions and\r
+* limitations under the License.\r
+*/\r
+using System;\r
+using ReaderException = com.google.zxing.ReaderException;\r
+using BitMatrix = com.google.zxing.common.BitMatrix;\r
+using DecoderResult = com.google.zxing.common.DecoderResult;\r
+using GF256 = com.google.zxing.common.reedsolomon.GF256;\r
+using ReedSolomonDecoder = com.google.zxing.common.reedsolomon.ReedSolomonDecoder;\r
+using ReedSolomonException = com.google.zxing.common.reedsolomon.ReedSolomonException;\r
+namespace com.google.zxing.datamatrix.decoder\r
+{\r
+       \r
+       /// <summary> <p>The main class which implements Data Matrix Code decoding -- as opposed to locating and extracting\r
+       /// the Data Matrix Code from an image.</p>\r
+       /// \r
+       /// </summary>\r
+       /// <author>  bbrown@google.com (Brian Brown)\r
+       /// </author>\r
+       /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source \r
+       /// </author>\r
+       public sealed class Decoder\r
+       {\r
+               \r
+               //UPGRADE_NOTE: Final was removed from the declaration of 'rsDecoder '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
+               private ReedSolomonDecoder rsDecoder;\r
+               \r
+               public Decoder()\r
+               {\r
+                       rsDecoder = new ReedSolomonDecoder(GF256.DATA_MATRIX_FIELD);\r
+               }\r
+               \r
+               /// <summary> <p>Convenience method that can decode a Data Matrix Code represented as a 2D array of booleans.\r
+               /// "true" is taken to mean a black module.</p>\r
+               /// \r
+               /// </summary>\r
+               /// <param name="image">booleans representing white/black Data Matrix Code modules\r
+               /// </param>\r
+               /// <returns> text and bytes encoded within the Data Matrix Code\r
+               /// </returns>\r
+               /// <throws>  ReaderException if the Data Matrix Code cannot be decoded </throws>\r
+               public DecoderResult decode(bool[][] image)\r
+               {\r
+                       int dimension = image.Length;\r
+                       BitMatrix bits = new BitMatrix(dimension);\r
+                       for (int i = 0; i < dimension; i++)\r
+                       {\r
+                               for (int j = 0; j < dimension; j++)\r
+                               {\r
+                                       if (image[i][j])\r
+                                       {\r
+                                               bits.set_Renamed(j, i);\r
+                                       }\r
+                               }\r
+                       }\r
+                       return decode(bits);\r
+               }\r
+               \r
+               /// <summary> <p>Decodes a Data Matrix Code represented as a {@link BitMatrix}. A 1 or "true" is taken\r
+               /// to mean a black module.</p>\r
+               /// \r
+               /// </summary>\r
+               /// <param name="bits">booleans representing white/black Data Matrix Code modules\r
+               /// </param>\r
+               /// <returns> text and bytes encoded within the Data Matrix Code\r
+               /// </returns>\r
+               /// <throws>  ReaderException if the Data Matrix Code cannot be decoded </throws>\r
+               public DecoderResult decode(BitMatrix bits)\r
+               {\r
+                       \r
+                       // Construct a parser and read version, error-correction level\r
+                       BitMatrixParser parser = new BitMatrixParser(bits);\r
+                       Version version = parser.readVersion(bits);\r
+                       \r
+                       // Read codewords\r
+                       sbyte[] codewords = parser.readCodewords();\r
+                       // Separate into data blocks\r
+                       DataBlock[] dataBlocks = DataBlock.getDataBlocks(codewords, version);\r
+                       \r
+                       // Count total number of data bytes\r
+                       int totalBytes = 0;\r
+                       for (int i = 0; i < dataBlocks.Length; i++)\r
+                       {\r
+                               totalBytes += dataBlocks[i].NumDataCodewords;\r
+                       }\r
+                       sbyte[] resultBytes = new sbyte[totalBytes];\r
+                       int resultOffset = 0;\r
+                       \r
+                       // Error-correct and copy data blocks together into a stream of bytes\r
+                       for (int j = 0; j < dataBlocks.Length; j++)\r
+                       {\r
+                               DataBlock dataBlock = dataBlocks[j];\r
+                               sbyte[] codewordBytes = dataBlock.Codewords;\r
+                               int numDataCodewords = dataBlock.NumDataCodewords;\r
+                               correctErrors(codewordBytes, numDataCodewords);\r
+                               for (int i = 0; i < numDataCodewords; i++)\r
+                               {\r
+                                       resultBytes[resultOffset++] = codewordBytes[i];\r
+                               }\r
+                       }\r
+                       \r
+                       // Decode the contents of that stream of bytes\r
+                       return DecodedBitStreamParser.decode(resultBytes);\r
+               }\r
+               \r
+               /// <summary> <p>Given data and error-correction codewords received, possibly corrupted by errors, attempts to\r
+               /// correct the errors in-place using Reed-Solomon error correction.</p>\r
+               /// \r
+               /// </summary>\r
+               /// <param name="codewordBytes">data and error correction codewords\r
+               /// </param>\r
+               /// <param name="numDataCodewords">number of codewords that are data bytes\r
+               /// </param>\r
+               /// <throws>  ReaderException if error correction fails </throws>\r
+               private void  correctErrors(sbyte[] codewordBytes, int numDataCodewords)\r
+               {\r
+                       int numCodewords = codewordBytes.Length;\r
+                       // First read into an array of ints\r
+                       int[] codewordsInts = new int[numCodewords];\r
+                       for (int i = 0; i < numCodewords; i++)\r
+                       {\r
+                               codewordsInts[i] = codewordBytes[i] & 0xFF;\r
+                       }\r
+                       int numECCodewords = codewordBytes.Length - numDataCodewords;\r
+                       try\r
+                       {\r
+                               rsDecoder.decode(codewordsInts, numECCodewords);\r
+                       }\r
+                       catch (ReedSolomonException rse)\r
+                       {\r
+                               throw ReaderException.Instance;\r
+                       }\r
+                       // Copy back into array of bytes -- only need to worry about the bytes that were data\r
+                       // We don't care about errors in the error-correction codewords\r
+                       for (int i = 0; i < numDataCodewords; i++)\r
+                       {\r
+                               codewordBytes[i] = (sbyte) codewordsInts[i];\r
+                       }\r
+               }\r
+       }\r
+}
\ No newline at end of file