New C# port from Suraj Supekar
[zxing.git] / csharp / common / reedsolomon / ReedSolomonEncoder.cs
diff --git a/csharp/common/reedsolomon/ReedSolomonEncoder.cs b/csharp/common/reedsolomon/ReedSolomonEncoder.cs
new file mode 100755 (executable)
index 0000000..162f80e
--- /dev/null
@@ -0,0 +1,89 @@
+/*\r
+* Copyright 2008 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
+namespace com.google.zxing.common.reedsolomon\r
+{\r
+       \r
+       /// <summary> <p>Implements Reed-Solomon enbcoding, as the name implies.</p>\r
+       /// \r
+       /// </summary>\r
+       /// <author>  Sean Owen\r
+       /// </author>\r
+       /// <author>  William Rucklidge\r
+       /// </author>\r
+       /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source \r
+       /// </author>\r
+       public sealed class ReedSolomonEncoder\r
+       {\r
+               \r
+               //UPGRADE_NOTE: Final was removed from the declaration of 'field '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
+               private GF256 field;\r
+               //UPGRADE_NOTE: Final was removed from the declaration of 'cachedGenerators '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
+               private System.Collections.ArrayList cachedGenerators;\r
+               \r
+               public ReedSolomonEncoder(GF256 field)\r
+               {\r
+                       if (!GF256.QR_CODE_FIELD.Equals(field))\r
+                       {\r
+                               throw new System.ArgumentException("Only QR Code is supported at this time");\r
+                       }\r
+                       this.field = field;\r
+                       this.cachedGenerators = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10));\r
+                       cachedGenerators.Add(new GF256Poly(field, new int[]{1}));\r
+               }\r
+               \r
+               private GF256Poly buildGenerator(int degree)\r
+               {\r
+                       if (degree >= cachedGenerators.Count)\r
+                       {\r
+                               GF256Poly lastGenerator = (GF256Poly) cachedGenerators[cachedGenerators.Count - 1];\r
+                               for (int d = cachedGenerators.Count; d <= degree; d++)\r
+                               {\r
+                                       GF256Poly nextGenerator = lastGenerator.multiply(new GF256Poly(field, new int[]{1, field.exp(d - 1)}));\r
+                                       cachedGenerators.Add(nextGenerator);\r
+                                       lastGenerator = nextGenerator;\r
+                               }\r
+                       }\r
+                       return (GF256Poly) cachedGenerators[degree];\r
+               }\r
+               \r
+               public void  encode(int[] toEncode, int ecBytes)\r
+               {\r
+                       if (ecBytes == 0)\r
+                       {\r
+                               throw new System.ArgumentException("No error correction bytes");\r
+                       }\r
+                       int dataBytes = toEncode.Length - ecBytes;\r
+                       if (dataBytes <= 0)\r
+                       {\r
+                               throw new System.ArgumentException("No data bytes provided");\r
+                       }\r
+                       GF256Poly generator = buildGenerator(ecBytes);\r
+                       int[] infoCoefficients = new int[dataBytes];\r
+                       Array.Copy(toEncode, 0, infoCoefficients, 0, dataBytes);\r
+                       GF256Poly info = new GF256Poly(field, infoCoefficients);\r
+                       info = info.multiplyByMonomial(ecBytes, 1);\r
+                       GF256Poly remainder = info.divide(generator)[1];\r
+                       int[] coefficients = remainder.Coefficients;\r
+                       int numZeroCoefficients = ecBytes - coefficients.Length;\r
+                       for (int i = 0; i < numZeroCoefficients; i++)\r
+                       {\r
+                               toEncode[dataBytes + i] = 0;\r
+                       }\r
+                       Array.Copy(coefficients, 0, toEncode, dataBytes + numZeroCoefficients, coefficients.Length);\r
+               }\r
+       }\r
+}
\ No newline at end of file