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