Committed C# port from Mohamad
[zxing.git] / csharp / common / reedsolomon / ReedSolomonEncoder.cs
1 /*\r
2 * Licensed under the Apache License, Version 2.0 (the "License");\r
3 * you may not use this file except in compliance with the License.\r
4 * You may obtain a copy of the License at\r
5 *\r
6 *      http://www.apache.org/licenses/LICENSE-2.0\r
7 *\r
8 * Unless required by applicable law or agreed to in writing, software\r
9 * distributed under the License is distributed on an "AS IS" BASIS,\r
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
11 * See the License for the specific language governing permissions and\r
12 * limitations under the License.\r
13 */\r
14 \r
15 using System;\r
16 using System.Collections;\r
17 \r
18 namespace com.google.zxing.common.reedsolomon\r
19 {\r
20     public sealed class ReedSolomonEncoder\r
21     { \r
22           private GF256 Field;\r
23           private ArrayList  cachedGenerators;\r
24 \r
25           public ReedSolomonEncoder(GF256 field) {\r
26             if (!GF256.QR_CODE_FIELD.Equals(field)) {\r
27               throw new ArgumentException("Only QR Code is supported at this time");\r
28             }\r
29             this.Field = field;\r
30             this.cachedGenerators = new ArrayList();\r
31             cachedGenerators.Add(new GF256Poly(field, new int[] { 1 }));\r
32           }\r
33 \r
34           private GF256Poly buildGenerator(int degree) {\r
35             if (degree >= cachedGenerators.Count) {\r
36                 GF256Poly lastGenerator = (GF256Poly)cachedGenerators[(cachedGenerators.Count - 1)];\r
37               for (int d = cachedGenerators.Count; d <= degree; d++)\r
38               {\r
39                   GF256Poly nextGenerator = lastGenerator.multiply(new GF256Poly(Field, new int[] { 1, Field.exp(d - 1) }));\r
40                 cachedGenerators.Add(nextGenerator);\r
41                 lastGenerator = nextGenerator;\r
42               }\r
43             }\r
44             return (GF256Poly) cachedGenerators[(degree)];    \r
45           }\r
46 \r
47           public void encode(int[] toEncode, int ecBytes) {\r
48             if (ecBytes == 0) {\r
49               throw new ArgumentException("No error correction bytes");\r
50             }\r
51             int dataBytes = toEncode.Length - ecBytes;\r
52             if (dataBytes <= 0) {\r
53               throw new ArgumentException("No data bytes provided");\r
54             }\r
55             GF256Poly generator = buildGenerator(ecBytes);\r
56             int[] infoCoefficients = new int[dataBytes];\r
57             System.Array.Copy(toEncode, 0, infoCoefficients, 0, dataBytes);\r
58             GF256Poly info = new GF256Poly(this.Field, infoCoefficients);\r
59             info = info.multiplyByMonomial(ecBytes, 1);\r
60             GF256Poly remainder = info.divide(generator)[1];\r
61             int[] coefficients = remainder.getCoefficients();\r
62             int numZeroCoefficients = ecBytes - coefficients.Length;\r
63             for (int i = 0; i < numZeroCoefficients; i++) {\r
64               toEncode[dataBytes + i] = 0;\r
65             }\r
66             System.Array.Copy(coefficients, 0, toEncode, dataBytes + numZeroCoefficients, coefficients.Length);\r
67           }\r
68     \r
69     }\r
70 }