forked from boombuler/barcode
/
errorcorrection.go
45 lines (36 loc) · 1.14 KB
/
errorcorrection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package datamatrix
import (
"github.com/boombuler/barcode/utils"
)
type errorCorrection struct {
rs *utils.ReedSolomonEncoder
}
var ec *errorCorrection = newErrorCorrection()
func newErrorCorrection() *errorCorrection {
gf := utils.NewGaloisField(301, 256, 1)
return &errorCorrection{utils.NewReedSolomonEncoder(gf)}
}
func (ec *errorCorrection) calcECC(data []byte, size *dmCodeSize) []byte {
dataSize := len(data)
// make some space for error correction codes
data = append(data, make([]byte, size.ECCCount)...)
for block := 0; block < size.BlockCount; block++ {
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++
}
// 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++
}
}
return data
}