New C# port from Suraj Supekar
[zxing.git] / csharp / common / reedsolomon / ReedSolomonEncoder.cs
1 /*\r
2 * Copyright 2008 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 using System;\r
17 namespace com.google.zxing.common.reedsolomon\r
18 {\r
19         \r
20         /// <summary> <p>Implements Reed-Solomon enbcoding, as the name implies.</p>\r
21         /// \r
22         /// </summary>\r
23         /// <author>  Sean Owen\r
24         /// </author>\r
25         /// <author>  William Rucklidge\r
26         /// </author>\r
27         /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source \r
28         /// </author>\r
29         public sealed class ReedSolomonEncoder\r
30         {\r
31                 \r
32                 //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
33                 private GF256 field;\r
34                 //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
35                 private System.Collections.ArrayList cachedGenerators;\r
36                 \r
37                 public ReedSolomonEncoder(GF256 field)\r
38                 {\r
39                         if (!GF256.QR_CODE_FIELD.Equals(field))\r
40                         {\r
41                                 throw new System.ArgumentException("Only QR Code is supported at this time");\r
42                         }\r
43                         this.field = field;\r
44                         this.cachedGenerators = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10));\r
45                         cachedGenerators.Add(new GF256Poly(field, new int[]{1}));\r
46                 }\r
47                 \r
48                 private GF256Poly buildGenerator(int degree)\r
49                 {\r
50                         if (degree >= cachedGenerators.Count)\r
51                         {\r
52                                 GF256Poly lastGenerator = (GF256Poly) cachedGenerators[cachedGenerators.Count - 1];\r
53                                 for (int d = cachedGenerators.Count; d <= degree; d++)\r
54                                 {\r
55                                         GF256Poly nextGenerator = lastGenerator.multiply(new GF256Poly(field, new int[]{1, field.exp(d - 1)}));\r
56                                         cachedGenerators.Add(nextGenerator);\r
57                                         lastGenerator = nextGenerator;\r
58                                 }\r
59                         }\r
60                         return (GF256Poly) cachedGenerators[degree];\r
61                 }\r
62                 \r
63                 public void  encode(int[] toEncode, int ecBytes)\r
64                 {\r
65                         if (ecBytes == 0)\r
66                         {\r
67                                 throw new System.ArgumentException("No error correction bytes");\r
68                         }\r
69                         int dataBytes = toEncode.Length - ecBytes;\r
70                         if (dataBytes <= 0)\r
71                         {\r
72                                 throw new System.ArgumentException("No data bytes provided");\r
73                         }\r
74                         GF256Poly generator = buildGenerator(ecBytes);\r
75                         int[] infoCoefficients = new int[dataBytes];\r
76                         Array.Copy(toEncode, 0, infoCoefficients, 0, dataBytes);\r
77                         GF256Poly info = new GF256Poly(field, infoCoefficients);\r
78                         info = info.multiplyByMonomial(ecBytes, 1);\r
79                         GF256Poly remainder = info.divide(generator)[1];\r
80                         int[] coefficients = remainder.Coefficients;\r
81                         int numZeroCoefficients = ecBytes - coefficients.Length;\r
82                         for (int i = 0; i < numZeroCoefficients; i++)\r
83                         {\r
84                                 toEncode[dataBytes + i] = 0;\r
85                         }\r
86                         Array.Copy(coefficients, 0, toEncode, dataBytes + numZeroCoefficients, coefficients.Length);\r
87                 }\r
88         }\r
89 }