errorcorrection.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package datamatrix
  2. import (
  3. "github.com/boombuler/barcode/utils"
  4. )
  5. type errorCorrection struct {
  6. rs *utils.ReedSolomonEncoder
  7. }
  8. var ec *errorCorrection = newErrorCorrection()
  9. func newErrorCorrection() *errorCorrection {
  10. gf := utils.NewGaloisField(301, 256, 1)
  11. return &errorCorrection{utils.NewReedSolomonEncoder(gf)}
  12. }
  13. func (ec *errorCorrection) calcECC(data []byte, size *dmCodeSize) []byte {
  14. dataSize := len(data)
  15. // make some space for error correction codes
  16. data = append(data, make([]byte, size.ECCCount)...)
  17. for block := 0; block < size.BlockCount; block++ {
  18. dataCnt := size.DataCodewordsForBlock(block)
  19. buff := make([]int, dataCnt)
  20. // copy the data for the current block to buff
  21. j := 0
  22. for i := block; i < dataSize; i += size.BlockCount {
  23. buff[j] = int(data[i])
  24. j++
  25. }
  26. // calc the error correction codes
  27. ecc := ec.rs.Encode(buff, size.ErrorCorrectionCodewordsPerBlock())
  28. // and append them to the result
  29. j = 0
  30. for i := block; i < size.ErrorCorrectionCodewordsPerBlock()*size.BlockCount; i += size.BlockCount {
  31. data[dataSize+i] = byte(ecc[j])
  32. j++
  33. }
  34. }
  35. return data
  36. }