1 parent
3dbe04fe04
commit
d039b6bd02
14 files changed
+642
-160
No files matched your search
+14
-39
@@ -2,53 +2,28 @@ package qr
|
||||
|
||||
import (
|
||||
"github.com/boombuler/barcode/utils"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type errorCorrection struct {
|
||||
fld *utils.GaloisField
|
||||
|
||||
m *sync.Mutex
|
||||
polynomes []*utils.GFPoly
|
||||
rs *utils.ReedSolomonEncoder
|
||||
}
|
||||
|
||||
var ec = newGF()
|
||||
var ec = newErrorCorrection()
|
||||
|
||||
func newGF() *errorCorrection {
|
||||
fld := utils.NewGaloisField(285)
|
||||
|
||||
return &errorCorrection{fld,
|
||||
new(sync.Mutex),
|
||||
[]*utils.GFPoly{
|
||||
utils.NewGFPoly(fld, []byte{1}),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (ec *errorCorrection) getPolynomial(degree int) *utils.GFPoly {
|
||||
ec.m.Lock()
|
||||
defer ec.m.Unlock()
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
return ec.polynomes[degree]
|
||||
func newErrorCorrection() *errorCorrection {
|
||||
fld := utils.NewGaloisField(285, 256, 0)
|
||||
return &errorCorrection{utils.NewReedSolomonEncoder(fld)}
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
result := make([]byte, eccCount)
|
||||
numZero := int(eccCount) - len(remainder.Coefficients)
|
||||
copy(result[numZero:], remainder.Coefficients)
|
||||
dataInts := make([]int, len(data))
|
||||
for i := 0; i < len(data); i++ {
|
||||
dataInts[i] = int(data[i])
|
||||
}
|
||||
res := ec.rs.Encode(dataInts, int(eccCount))
|
||||
result := make([]byte, len(res))
|
||||
for i := 0; i < len(res); i++ {
|
||||
result[i] = byte(res[i])
|
||||
}
|
||||
return result
|
||||
}
|
||||
@@ -5,19 +5,6 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_LogTables(t *testing.T) {
|
||||
for i := 1; i <= 255; i++ {
|
||||
tmp := ec.fld.LogTbl[i]
|
||||
if i != ec.fld.ALogTbl[tmp] {
|
||||
t.Errorf("Invalid LogTables: %d", i)
|
||||
}
|
||||
}
|
||||
|
||||
if ec.fld.ALogTbl[11] != 232 || ec.fld.ALogTbl[87] != 127 || ec.fld.ALogTbl[225] != 36 {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func Test_ErrorCorrection(t *testing.T) {
|
||||
doTest := func(b []byte, ecc []byte) {
|
||||
cnt := byte(len(ecc))
|
||||
|
||||
Reference in new issue
Block a user