barcode/datamatrix/errorcorrection.go

46 lines
1.1 KiB
Go
Raw Normal View History

2013-12-12 19:36:57 +00:00
package datamatrix
import (
2025-01-03 19:41:46 +00:00
"git.bbr-dev.info/brajkovic/barcode/utils"
)
2013-12-12 19:36:57 +00:00
type errorCorrection struct {
rs *utils.ReedSolomonEncoder
2013-12-12 19:36:57 +00:00
}
var ec *errorCorrection = newErrorCorrection()
2013-12-12 19:36:57 +00:00
func newErrorCorrection() *errorCorrection {
gf := utils.NewGaloisField(301, 256, 1)
return &errorCorrection{utils.NewReedSolomonEncoder(gf)}
2013-12-12 19:36:57 +00:00
}
func (ec *errorCorrection) calcECC(data []byte, size *dmCodeSize) []byte {
2013-12-12 19:36:57 +00:00
dataSize := len(data)
// make some space for error correction codes
data = append(data, make([]byte, size.ECCCount)...)
2013-12-12 19:36:57 +00:00
for block := 0; block < size.BlockCount; block++ {
2016-07-18 16:56:27 +00:00
dataCnt := size.DataCodewordsForBlock(block)
buff := make([]int, dataCnt)
// copy the data for the current block to buff
j := 0
for i := block; i < dataSize; i += size.BlockCount {
buff[j] = int(data[i])
j++
2013-12-12 19:36:57 +00:00
}
// calc the error correction codes
ecc := ec.rs.Encode(buff, size.ErrorCorrectionCodewordsPerBlock())
// and append them to the result
j = 0
for i := block; i < size.ErrorCorrectionCodewordsPerBlock()*size.BlockCount; i += size.BlockCount {
data[dataSize+i] = byte(ecc[j])
j++
2013-12-12 19:36:57 +00:00
}
}
return data
}