You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ReedSolomonEncoder.cs 2.9 kB

10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. using System.Collections.Generic;
  18. namespace ZXing.Common.ReedSolomon
  19. {
  20. /// <summary>
  21. /// Implements Reed-Solomon encoding, as the name implies.
  22. /// </summary>
  23. /// <author>Sean Owen</author>
  24. /// <author>William Rucklidge</author>
  25. public sealed class ReedSolomonEncoder
  26. {
  27. private readonly GenericGF field;
  28. private readonly IList<GenericGFPoly> cachedGenerators;
  29. public ReedSolomonEncoder(GenericGF field)
  30. {
  31. this.field = field;
  32. this.cachedGenerators = new List<GenericGFPoly>();
  33. cachedGenerators.Add(new GenericGFPoly(field, new int[] { 1 }));
  34. }
  35. private GenericGFPoly buildGenerator(int degree)
  36. {
  37. if (degree >= cachedGenerators.Count)
  38. {
  39. var lastGenerator = cachedGenerators[cachedGenerators.Count - 1];
  40. for (int d = cachedGenerators.Count; d <= degree; d++)
  41. {
  42. var nextGenerator = lastGenerator.multiply(new GenericGFPoly(field, new int[] { 1, field.exp(d - 1 + field.GeneratorBase) }));
  43. cachedGenerators.Add(nextGenerator);
  44. lastGenerator = nextGenerator;
  45. }
  46. }
  47. return cachedGenerators[degree];
  48. }
  49. public void encode(int[] toEncode, int ecBytes)
  50. {
  51. if (ecBytes == 0)
  52. {
  53. throw new ArgumentException("No error correction bytes");
  54. }
  55. var dataBytes = toEncode.Length - ecBytes;
  56. if (dataBytes <= 0)
  57. {
  58. throw new ArgumentException("No data bytes provided");
  59. }
  60. var generator = buildGenerator(ecBytes);
  61. var infoCoefficients = new int[dataBytes];
  62. Array.Copy(toEncode, 0, infoCoefficients, 0, dataBytes);
  63. var info = new GenericGFPoly(field, infoCoefficients);
  64. info = info.multiplyByMonomial(ecBytes, 1);
  65. var remainder = info.divide(generator)[1];
  66. var coefficients = remainder.Coefficients;
  67. var numZeroCoefficients = ecBytes - coefficients.Length;
  68. for (var i = 0; i < numZeroCoefficients; i++)
  69. {
  70. toEncode[dataBytes + i] = 0;
  71. }
  72. Array.Copy(coefficients, 0, toEncode, dataBytes + numZeroCoefficients, coefficients.Length);
  73. }
  74. }
  75. }