Committed C# port from Mohamad
[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..db76156
--- /dev/null
@@ -0,0 +1,70 @@
+/*\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
+\r
+using System;\r
+using System.Collections;\r
+\r
+namespace com.google.zxing.common.reedsolomon\r
+{\r
+    public sealed class ReedSolomonEncoder\r
+    { \r
+          private GF256 Field;\r
+          private ArrayList  cachedGenerators;\r
+\r
+          public ReedSolomonEncoder(GF256 field) {\r
+            if (!GF256.QR_CODE_FIELD.Equals(field)) {\r
+              throw new ArgumentException("Only QR Code is supported at this time");\r
+            }\r
+            this.Field = field;\r
+            this.cachedGenerators = new ArrayList();\r
+            cachedGenerators.Add(new GF256Poly(field, new int[] { 1 }));\r
+          }\r
+\r
+          private GF256Poly buildGenerator(int degree) {\r
+            if (degree >= cachedGenerators.Count) {\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
+            if (ecBytes == 0) {\r
+              throw new ArgumentException("No error correction bytes");\r
+            }\r
+            int dataBytes = toEncode.Length - ecBytes;\r
+            if (dataBytes <= 0) {\r
+              throw new ArgumentException("No data bytes provided");\r
+            }\r
+            GF256Poly generator = buildGenerator(ecBytes);\r
+            int[] infoCoefficients = new int[dataBytes];\r
+            System.Array.Copy(toEncode, 0, infoCoefficients, 0, dataBytes);\r
+            GF256Poly info = new GF256Poly(this.Field, infoCoefficients);\r
+            info = info.multiplyByMonomial(ecBytes, 1);\r
+            GF256Poly remainder = info.divide(generator)[1];\r
+            int[] coefficients = remainder.getCoefficients();\r
+            int numZeroCoefficients = ecBytes - coefficients.Length;\r
+            for (int i = 0; i < numZeroCoefficients; i++) {\r
+              toEncode[dataBytes + i] = 0;\r
+            }\r
+            System.Array.Copy(coefficients, 0, toEncode, dataBytes + numZeroCoefficients, coefficients.Length);\r
+          }\r
+    \r
+    }\r
+}
\ No newline at end of file