barcode/qr/errorcorrection.go

48 lines
1.1 KiB
Go
Raw Normal View History

2013-12-11 13:31:11 +00:00
package qr
import (
"github.com/boombuler/barcode/utils"
)
type errorCorrection struct {
fld *utils.GaloisField
polynomes []*utils.GFPoly
2013-12-11 13:31:11 +00:00
}
2014-08-11 08:34:45 +00:00
var ec = newGF()
2013-12-11 13:31:11 +00:00
func newGF() *errorCorrection {
fld := utils.NewGaloisField(285)
2013-12-11 13:31:11 +00:00
return &errorCorrection{fld,
[]*utils.GFPoly{
utils.NewGFPoly(fld, []byte{1}),
},
}
}
2013-12-11 13:31:11 +00:00
func (ec *errorCorrection) getPolynomial(degree int) *utils.GFPoly {
if degree >= len(ec.polynomes) {
last := ec.polynomes[len(ec.polynomes)-1]
for d := len(ec.polynomes); d <= degree; d++ {
next := last.Multiply(utils.NewGFPoly(ec.fld, []byte{1, byte(ec.fld.ALogTbl[d-1])}))
ec.polynomes = append(ec.polynomes, next)
last = next
2013-12-11 13:31:11 +00:00
}
}
return ec.polynomes[degree]
2013-12-11 13:31:11 +00:00
}
func (ec *errorCorrection) calcECC(data []byte, eccCount byte) []byte {
generator := ec.getPolynomial(int(eccCount))
info := utils.NewGFPoly(ec.fld, data)
info = info.MultByMonominal(int(eccCount), 1)
_, remainder := info.Divide(generator)
2013-12-11 13:31:11 +00:00
result := make([]byte, eccCount)
numZero := int(eccCount) - len(remainder.Coefficients)
copy(result[numZero:], remainder.Coefficients)
return result
2013-12-11 13:31:11 +00:00
}