ReedSolomonEncoder.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright 2008 ZXing authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. using System;
  17. namespace FastReport.Barcode.QRCode
  18. {
  19. /*/// <summary> <p>Implements Reed-Solomon enbcoding, as the name implies.</p>
  20. ///
  21. /// </summary>
  22. /// <author> Sean Owen
  23. /// </author>
  24. /// <author> William Rucklidge
  25. /// </author>
  26. /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source
  27. /// </author>*/
  28. internal sealed class ReedSolomonEncoder
  29. {
  30. //UPGRADE_NOTE: Final was removed from the declaration of 'field '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
  31. private GF256 field;
  32. //UPGRADE_NOTE: Final was removed from the declaration of 'cachedGenerators '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
  33. private System.Collections.ArrayList cachedGenerators;
  34. public ReedSolomonEncoder(GF256 field)
  35. {
  36. if (!GF256.QR_CODE_FIELD.Equals(field))
  37. {
  38. throw new System.ArgumentException("Only QR Code is supported at this time");
  39. }
  40. this.field = field;
  41. this.cachedGenerators = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10));
  42. cachedGenerators.Add(new GF256Poly(field, new int[]{1}));
  43. }
  44. private GF256Poly buildGenerator(int degree)
  45. {
  46. if (degree >= cachedGenerators.Count)
  47. {
  48. GF256Poly lastGenerator = (GF256Poly) cachedGenerators[cachedGenerators.Count - 1];
  49. for (int d = cachedGenerators.Count; d <= degree; d++)
  50. {
  51. GF256Poly nextGenerator = lastGenerator.multiply(new GF256Poly(field, new int[]{1, field.exp(d - 1)}));
  52. cachedGenerators.Add(nextGenerator);
  53. lastGenerator = nextGenerator;
  54. }
  55. }
  56. return (GF256Poly) cachedGenerators[degree];
  57. }
  58. public void encode(int[] toEncode, int ecBytes)
  59. {
  60. if (ecBytes == 0)
  61. {
  62. throw new System.ArgumentException("No error correction bytes");
  63. }
  64. int dataBytes = toEncode.Length - ecBytes;
  65. if (dataBytes <= 0)
  66. {
  67. throw new System.ArgumentException("No data bytes provided");
  68. }
  69. GF256Poly generator = buildGenerator(ecBytes);
  70. int[] infoCoefficients = new int[dataBytes];
  71. Array.Copy(toEncode, 0, infoCoefficients, 0, dataBytes);
  72. GF256Poly info = new GF256Poly(field, infoCoefficients);
  73. info = info.multiplyByMonomial(ecBytes, 1);
  74. GF256Poly remainder = info.divide(generator)[1];
  75. int[] coefficients = remainder.Coefficients;
  76. int numZeroCoefficients = ecBytes - coefficients.Length;
  77. for (int i = 0; i < numZeroCoefficients; i++)
  78. {
  79. toEncode[dataBytes + i] = 0;
  80. }
  81. Array.Copy(coefficients, 0, toEncode, dataBytes + numZeroCoefficients, coefficients.Length);
  82. }
  83. }
  84. }