2 * Copyright 2008 ZXing authors
\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
8 * http://www.apache.org/licenses/LICENSE-2.0
\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
17 namespace com.google.zxing.common.reedsolomon
\r
20 /// <summary> <p>Implements Reed-Solomon enbcoding, as the name implies.</p>
\r
23 /// <author> Sean Owen
\r
25 /// <author> William Rucklidge
\r
27 /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source
\r
29 public sealed class ReedSolomonEncoder
\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
37 public ReedSolomonEncoder(GF256 field)
\r
39 if (!GF256.QR_CODE_FIELD.Equals(field))
\r
41 throw new System.ArgumentException("Only QR Code is supported at this time");
\r
44 this.cachedGenerators = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10));
\r
45 cachedGenerators.Add(new GF256Poly(field, new int[]{1}));
\r
48 private GF256Poly buildGenerator(int degree)
\r
50 if (degree >= cachedGenerators.Count)
\r
52 GF256Poly lastGenerator = (GF256Poly) cachedGenerators[cachedGenerators.Count - 1];
\r
53 for (int d = cachedGenerators.Count; d <= degree; d++)
\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
60 return (GF256Poly) cachedGenerators[degree];
\r
63 public void encode(int[] toEncode, int ecBytes)
\r
67 throw new System.ArgumentException("No error correction bytes");
\r
69 int dataBytes = toEncode.Length - ecBytes;
\r
72 throw new System.ArgumentException("No data bytes provided");
\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
84 toEncode[dataBytes + i] = 0;
\r
86 Array.Copy(coefficients, 0, toEncode, dataBytes + numZeroCoefficients, coefficients.Length);
\r