errorcorrection.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package qr
  2. import (
  3. "github.com/boombuler/barcode/utils"
  4. )
  5. type errorCorrection struct {
  6. fld *utils.GaloisField
  7. polynomes []*utils.GFPoly
  8. }
  9. var ec = newGF()
  10. func newGF() *errorCorrection {
  11. fld := utils.NewGaloisField(285)
  12. return &errorCorrection{fld,
  13. []*utils.GFPoly{
  14. utils.NewGFPoly(fld, []byte{1}),
  15. },
  16. }
  17. }
  18. func (ec *errorCorrection) getPolynomial(degree int) *utils.GFPoly {
  19. if degree >= len(ec.polynomes) {
  20. last := ec.polynomes[len(ec.polynomes)-1]
  21. for d := len(ec.polynomes); d <= degree; d++ {
  22. next := last.Multiply(utils.NewGFPoly(ec.fld, []byte{1, byte(ec.fld.ALogTbl[d-1])}))
  23. ec.polynomes = append(ec.polynomes, next)
  24. last = next
  25. }
  26. }
  27. return ec.polynomes[degree]
  28. }
  29. func (ec *errorCorrection) calcECC(data []byte, eccCount byte) []byte {
  30. generator := ec.getPolynomial(int(eccCount))
  31. info := utils.NewGFPoly(ec.fld, data)
  32. info = info.MultByMonominal(int(eccCount), 1)
  33. _, remainder := info.Divide(generator)
  34. result := make([]byte, eccCount)
  35. numZero := int(eccCount) - len(remainder.Coefficients)
  36. copy(result[numZero:], remainder.Coefficients)
  37. return result
  38. }