Skip to content

Commit

Permalink
refactor(qrcode): add export API for matrix visit function and types
Browse files Browse the repository at this point in the history
  • Loading branch information
yeqown committed Feb 16, 2022
1 parent 671d008 commit ff47931
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 36 deletions.
2 changes: 1 addition & 1 deletion debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func debugDrawTo(w io.Writer, mat Matrix) error {
// background
rectangle(0, 0, width, height, img, color.Gray16{Y: 0xff12})

mat.Iterate(IterDirection_COLUMN, func(x int, y int, v qrvalue) {
mat.iter(IterDirection_COLUMN, func(x int, y int, v qrvalue) {
sx := x*blockWidth + padding
sy := y*blockWidth + padding
es := (x+1)*blockWidth + padding
Expand Down
2 changes: 1 addition & 1 deletion mask.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (m *mask) masking() {
panic("impossible panic, contact maintainer plz")
}

m.mat.Iterate(IterDirection_COLUMN, func(x, y int, s qrvalue) {
m.mat.iter(IterDirection_COLUMN, func(x, y int, s qrvalue) {
// skip the function modules
if v, _ := m.mat.at(x, y); v.qrtype() != QRType_INIT {
_ = m.mat.set(x, y, QRValue_INIT_V0)
Expand Down
2 changes: 1 addition & 1 deletion mask_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestMask(t *testing.T) {
require.NoError(t, err)

var stateInitCnt int
qrc.mat.Iterate(IterDirection_COLUMN, func(x, y int, s qrvalue) {
qrc.mat.iter(IterDirection_COLUMN, func(x, y int, s qrvalue) {
if s.qrtype() == QRType_INIT {
stateInitCnt++
}
Expand Down
22 changes: 12 additions & 10 deletions matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (m *Matrix) init() {

// print to stdout
func (m *Matrix) print() {
m.Iterate(IterDirection_ROW, func(x, y int, s qrvalue) {
m.iter(IterDirection_ROW, func(x, y int, s qrvalue) {
fmt.Printf("%s ", s)
if (x + 1) == m.width {
fmt.Println()
Expand Down Expand Up @@ -96,7 +96,7 @@ func (m *Matrix) set(w, h int, c qrvalue) error {
return nil
}

// at state qrbool from matrix with position {x, y}
// at state qrvalue from matrix with position {x, y}
func (m *Matrix) at(w, h int) (qrvalue, error) {
if w >= m.width || w < 0 {
return QRValue_INIT_V0, ErrorOutRangeOfW
Expand All @@ -120,26 +120,28 @@ const (

// Iterate the Matrix with loop direction IterDirection_ROW major or IterDirection_COLUMN major.
// IterDirection_COLUMN is recommended.
func (m *Matrix) Iterate(dir iterDirection, f func(int, int, qrvalue)) {
func (m *Matrix) Iterate(direction iterDirection, fn func(x, y int, s QRValue)) {
m.iter(direction, fn)
}

func (m *Matrix) iter(dir iterDirection, visitFn func(x int, y int, v qrvalue)) {
// row direction first
if dir == IterDirection_ROW {
for h := 0; h < m.height; h++ {
for w := 0; w < m.width; w++ {
f(w, h, m.mat[w][h])
visitFn(w, h, m.mat[w][h])
}
}
return
}

// column direction first
if dir == IterDirection_COLUMN {
for w := 0; w < m.width; w++ {
for h := 0; h < m.height; h++ {
f(w, h, m.mat[w][h])
}
for w := 0; w < m.width; w++ {
for h := 0; h < m.height; h++ {
visitFn(w, h, m.mat[w][h])
}
return
}
return
}

// Row return a row of matrix, cur should be y dimension.
Expand Down
32 changes: 11 additions & 21 deletions matrix_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package qrcode

import (
"reflect"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -41,25 +40,16 @@ func TestMatrix_Copy(t *testing.T) {

// change origin
_ = m1.set(2, 2, QRValue_DATA_V1)
assert.Equal(t, m2, got)

// compare copy of the matrix
if !reflect.DeepEqual(got, m2) {
t.Errorf("Matrix.Copy() = %v, want %v", got, m2)
t.Fail()
}

if s, err := m1.at(2, 2); err != nil {
t.Errorf("Matrix.at() = %v, want %v", QRValue_DATA_V1, s)
t.Fail()
} else if s != QRValue_DATA_V1 {
t.Errorf("Matrix.Copy() = %v, want %v", QRValue_DATA_V1, s)
t.Fail()
}
s, err := m1.at(2, 2)
assert.NoError(t, err)
assert.Equal(t, QRValue_DATA_V1, s)

if s, _ := got.at(2, 2); s != QRValue_DATA_V1 {
t.Errorf("Matrix.Copy() = %v, want %v", QRValue_DATA_V1, s)
t.Fail()
}
s, err = got.at(2, 2)
assert.NoError(t, err)
assert.NotEqual(t, QRValue_DATA_V1, s)
assert.Equal(t, QRValue_INIT_V0, s)
}

//func Test_stateSliceMatched(t *testing.T) {
Expand Down Expand Up @@ -99,7 +89,7 @@ func TestMatrix_Copy(t *testing.T) {
//}

// go test -run=NONE -bench Benchmark_Iterate -count 10 > old.txt
// after change to `m.Iterate(IterDirection_COLUMN, rowIteration)`
// after change to `m.iter(IterDirection_COLUMN, rowIteration)`
// go test -run=NONE -bench Benchmark_Iterate -count 10 > new.txt
// benchstat old.txt new.txt
func Benchmark_Iterate(b *testing.B) {
Expand All @@ -119,8 +109,8 @@ func Benchmark_Iterate(b *testing.B) {
}

for i := 0; i < b.N; i++ {
//m.Iterate(IterDirection_ROW, rowIteration)
m.Iterate(IterDirection_COLUMN, rowIteration)
//m.iter(IterDirection_ROW, rowIteration)
m.iter(IterDirection_COLUMN, rowIteration)
}
}

Expand Down
12 changes: 12 additions & 0 deletions matrix_type.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package qrcode

type QRType = qrtype

// qrtype
type qrtype uint8

Expand Down Expand Up @@ -43,6 +45,16 @@ func (s qrtype) String() string {
return "?"
}

type QRValue = qrvalue

func (v QRValue) Type() qrtype {
return v.qrtype()
}

func (v QRValue) IsSet() bool {
return v.qrbool()
}

// qrvalue represents the value of the matrix, it is composed of the qrtype(7bits) and the value(1bits).
// such as: 0b0000,0011 (QRValue_DATA_V1) represents the qrtype is QRType_DATA and the value is 1.
type qrvalue uint8
Expand Down
2 changes: 1 addition & 1 deletion qrcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ func (q *QRCode) masking() {

// all mask patter and check the maskScore choose the lowest mask result
func (q *QRCode) xorMask(m *Matrix, mask *mask) {
mask.mat.Iterate(IterDirection_COLUMN, func(x, y int, v qrvalue) {
mask.mat.iter(IterDirection_COLUMN, func(x, y int, v qrvalue) {
// skip the empty place
if v.qrtype() == QRType_INIT {
return
Expand Down
2 changes: 1 addition & 1 deletion utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package qrcode
// samestate judge two matrix qrtype is same with binary semantic.
// QRValue_DATA_V0/QRType_INIT only equal to QRValue_DATA_V0, other state are equal to each other.
func samestate(s1, s2 qrvalue) bool {
return s1.qrbool() && s2.qrbool()
return s1.qrbool() == s2.qrbool()
}

func abs(x int) int {
Expand Down

0 comments on commit ff47931

Please sign in to comment.