Skip to content

Commit

Permalink
Feat/replace reedsolomon (#4)
Browse files Browse the repository at this point in the history
* feat: replace reedsolomon ~

* reedsolomon version release v1.0.0
  • Loading branch information
yeqown authored Nov 30, 2018
1 parent a41571f commit 4608e38
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 37 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
testdata/
/testdata/
vendor/
draft/
# example/
Expand All @@ -7,4 +7,5 @@ default.jpeg
# *.json
*.log
.DS_store
js/
js/
default.jpeg
22 changes: 11 additions & 11 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
"log"

"github.com/skip2/go-qrcode/bitset"
"github.com/yeqown/reedsolomon/binary"
)

// EncMode ...
Expand All @@ -26,8 +26,8 @@ const (
)

var (
paddingByte1 = bitset.NewFromBase2String("11101100")
paddingByte2 = bitset.NewFromBase2String("00010001")
paddingByte1, _ = binary.NewFromBinaryString("11101100")
paddingByte2, _ = binary.NewFromBinaryString("00010001")
)

// GetEncModeName ...
Expand All @@ -49,16 +49,16 @@ func GetEncModeName(mode EncMode) string {
}

// getEncodeModeIndicator ...
func getEncodeModeIndicator(mode EncMode) *bitset.Bitset {
func getEncodeModeIndicator(mode EncMode) *binary.Binary {
switch mode {
case EncModeNumeric:
return bitset.New(false, false, false, true)
return binary.New(false, false, false, true)
case EncModeAlphanumeric:
return bitset.New(false, false, true, false)
return binary.New(false, false, true, false)
case EncModeByte:
return bitset.New(false, true, false, false)
return binary.New(false, true, false, false)
case EncModeJP:
return bitset.New(true, false, false, false)
return binary.New(true, false, false, false)
default:
panic("no indicator")
}
Expand All @@ -67,7 +67,7 @@ func getEncodeModeIndicator(mode EncMode) *bitset.Bitset {
// Encoder ... data to bit stream ...
type Encoder struct {
// self init
dst *bitset.Bitset
dst *binary.Binary
data []byte // raw input data

// initial params
Expand All @@ -82,8 +82,8 @@ type Encoder struct {
// 1. encode raw data into bitset
// 2. append padding data
//
func (e *Encoder) Encode(byts []byte) (*bitset.Bitset, error) {
e.dst = bitset.New()
func (e *Encoder) Encode(byts []byte) (*binary.Binary, error) {
e.dst = binary.New()
e.data = byts

// appedn mode indicator symbol
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/yeqown/go-qrcode

require github.com/skip2/go-qrcode v0.0.0-20171229120447-cf5f9fa2f0d8
require github.com/yeqown/reedsolomon v1.0.0
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github.com/skip2/go-qrcode v0.0.0-20171229120447-cf5f9fa2f0d8 h1:5C4yAeYifeRO+7z2/H2kxL8tJZE9ZE9LpxK6YUZPByo=
github.com/skip2/go-qrcode v0.0.0-20171229120447-cf5f9fa2f0d8/go.mod h1:PLPIyL7ikehBD1OAjmKKiOEhbvWyHGaNDjquXMcYABo=
github.com/yeqown/reedsolomon v1.0.0 h1:x1h/Ej/uJnNu8jaX7GLHBWmZKCAWjEJTetkqaabr4B0=
github.com/yeqown/reedsolomon v1.0.0/go.mod h1:P76zpcn2TCuL0ul1Fso373qHRc69LKwAw/Iy6g1WiiM=
43 changes: 30 additions & 13 deletions qrcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ import (
"sync"
"time"

"github.com/skip2/go-qrcode/bitset"
"github.com/skip2/go-qrcode/reedsolomon"
// "github.com/skip2/go-qrcode/bitset"
// "github.com/skip2/go-qrcode/reedsolomon"

"github.com/yeqown/go-qrcode/matrix"
"github.com/yeqown/reedsolomon"
"github.com/yeqown/reedsolomon/binary"
)

var (
Expand Down Expand Up @@ -61,9 +64,9 @@ type QRCode struct {
content string // input text content
rawData []byte // raw Data to transfer

dataBSet *bitset.Bitset // final data bit stream of encode data
dataBSet *binary.Binary // final data bit stream of encode data
mat *matrix.Matrix // matrix grid to store final bitmap
ecBSet *bitset.Bitset // final error correction bitset
ecBSet *binary.Binary // final error correction bitset

v Version // version means the size
ver int // version num
Expand Down Expand Up @@ -152,7 +155,7 @@ func (q *QRCode) analyze() error {
// https://www.thonky.com/qr-code-tutorial/data-encoding
func (q *QRCode) dataEncoding() (blocks []dataBlock, err error) {
var (
bset *bitset.Bitset
bset *binary.Binary
)
bset, err = q.encoder.Encode(q.rawData)
if err != nil {
Expand All @@ -169,7 +172,10 @@ func (q *QRCode) dataEncoding() (blocks []dataBlock, err error) {
start = end
end = start + g.NumDataCodewords*8

blocks[blockID].Data = bset.Substr(start, end)
blocks[blockID].Data, err = bset.Subset(start, end)
if err != nil {
panic(err)
}
blocks[blockID].StartOffset = end - start
blocks[blockID].NumECBlock = g.ECBlockwordsPerBlock

Expand All @@ -182,14 +188,14 @@ func (q *QRCode) dataEncoding() (blocks []dataBlock, err error) {

// dataBlock ...
type dataBlock struct {
Data *bitset.Bitset
Data *binary.Binary
StartOffset int // length
NumECBlock int // error correction codewrods num per data block
}

// ecBlock ...
type ecBlock struct {
Data *bitset.Bitset
Data *binary.Binary
// StartOffset int // length
}

Expand All @@ -201,7 +207,10 @@ func (q *QRCode) errorCorrectionEncoding(dataBlocks []dataBlock) (blocks []ecBlo
for idx, b := range dataBlocks {
debugLogf("numOfECBlock: %d", b.NumECBlock)
bset := reedsolomon.Encode(b.Data, b.NumECBlock)
blocks[idx].Data = bset.Substr(b.StartOffset, bset.Len())
blocks[idx].Data, err = bset.Subset(b.StartOffset, bset.Len())
if err != nil {
panic(err)
}
// blocks[idx].StartOffset = b.StartOffset
}
return
Expand Down Expand Up @@ -229,10 +238,10 @@ func (q *QRCode) arrarngeBits(dataBlocks []dataBlock, ecBlocks []ecBlock) {

// check if bitsets initialized, or initial them
if q.dataBSet == nil {
q.dataBSet = bitset.New()
q.dataBSet = binary.New()
}
if q.ecBSet == nil {
q.ecBSet = bitset.New()
q.ecBSet = binary.New()
}

for !endFlag {
Expand All @@ -243,7 +252,11 @@ func (q *QRCode) arrarngeBits(dataBlocks []dataBlock, ecBlocks []ecBlock) {
overflowCnt++
continue
}
q.dataBSet.Append(block.Data.Substr(start, end))
subBin, err := block.Data.Subset(start, end)
if err != nil {
panic(err)
}
q.dataBSet.Append(subBin)
debugLogf("arrange data blocks info: start: %d, end: %d, len: %d, overflowCnt: %d, curIdx: %d",
start, end, block.Data.Len(), overflowCnt, curIdx,
)
Expand All @@ -269,7 +282,11 @@ func (q *QRCode) arrarngeBits(dataBlocks []dataBlock, ecBlocks []ecBlock) {
overflowCnt++
continue
}
q.ecBSet.Append(block.Data.Substr(start, end))
subBin, err := block.Data.Subset(start, end)
if err != nil {
panic(err)
}
q.ecBSet.Append(subBin)
}
curIdx++
// loop finish check
Expand Down
11 changes: 6 additions & 5 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"errors"
"log"

"github.com/skip2/go-qrcode/bitset"
// "github.com/skip2/go-qrcode/bitset"
"github.com/yeqown/reedsolomon/binary"
)

// ECLevel error correction level
Expand Down Expand Up @@ -179,20 +180,20 @@ func (v Version) TotalNumBlocks() int {
}

// VerInfo Version info bitset
func (v Version) verInfo() *bitset.Bitset {
func (v Version) verInfo() *binary.Binary {
if v.Ver < 7 {
return nil
}

result := bitset.New()
result := binary.New()
result.AppendUint32(versionBitSequence[v.Ver], verInfoBitsNum)

return result
}

// formatInfo returns the 15-bit Format Information value for a QR
// code.
func (v Version) formatInfo(maskPattern int) *bitset.Bitset {
func (v Version) formatInfo(maskPattern int) *binary.Binary {
formatID := 0

switch v.ECLevel {
Expand All @@ -213,7 +214,7 @@ func (v Version) formatInfo(maskPattern int) *bitset.Bitset {
}

formatID |= maskPattern & 0x7
result := bitset.New()
result := binary.New()
result.AppendUint32(formatBitSequence[formatID].regular, formatInfoBitsNum)
return result
}
Expand Down
6 changes: 3 additions & 3 deletions version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"reflect"
"testing"

"github.com/skip2/go-qrcode/bitset"
"github.com/yeqown/reedsolomon/binary"
)

// func Test_load(t *testing.T) {
Expand Down Expand Up @@ -135,7 +135,7 @@ func TestVersion_verInfo(t *testing.T) {
tests := []struct {
name string
fields fields
want *bitset.Bitset
want *binary.Binary
}{
// TODO: Add test cases.
}
Expand Down Expand Up @@ -170,7 +170,7 @@ func TestVersion_formatInfo(t *testing.T) {
name string
fields fields
args args
want *bitset.Bitset
want *binary.Binary
}{
// TODO: Add test cases.
}
Expand Down

0 comments on commit 4608e38

Please sign in to comment.