Skip to content

Commit

Permalink
Merge pull request #10 from yeqown/docs/cancel-useless-exported-api
Browse files Browse the repository at this point in the history
Docs/cancel useless exported api
  • Loading branch information
yeqown authored Oct 16, 2020
2 parents 616a6b3 + ddb46bf commit f42e02b
Show file tree
Hide file tree
Showing 24 changed files with 350 additions and 339 deletions.
96 changes: 48 additions & 48 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,80 +9,80 @@ import (
"github.com/yeqown/reedsolomon/binary"
)

// EncMode ...
type EncMode uint
// encMode ...
type encMode uint

const (
// EncModeNone mode ...
EncModeNone EncMode = 1 << iota
// EncModeNumeric mode ...
EncModeNumeric
// EncModeAlphanumeric mode ...
EncModeAlphanumeric
// EncModeByte mode ...
EncModeByte
// EncModeJP mode ...
EncModeJP
// encModeNone mode ...
encModeNone encMode = 1 << iota
// encModeNumeric mode ...
encModeNumeric
// encModeAlphanumeric mode ...
encModeAlphanumeric
// encModeByte mode ...
encModeByte
// encModeJP mode ...
encModeJP
)

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

// GetEncModeName ...
func GetEncModeName(mode EncMode) string {
// getEncModeName ...
func getEncModeName(mode encMode) string {
switch mode {
case EncModeNone:
case encModeNone:
return "none"
case EncModeNumeric:
case encModeNumeric:
return "numeric"
case EncModeAlphanumeric:
case encModeAlphanumeric:
return "alphanumeric"
case EncModeByte:
case encModeByte:
return "byte"
case EncModeJP:
case encModeJP:
return "japan"
default:
return "unknown"
}
}

// getEncodeModeIndicator ...
func getEncodeModeIndicator(mode EncMode) *binary.Binary {
func getEncodeModeIndicator(mode encMode) *binary.Binary {
switch mode {
case EncModeNumeric:
case encModeNumeric:
return binary.New(false, false, false, true)
case EncModeAlphanumeric:
case encModeAlphanumeric:
return binary.New(false, false, true, false)
case EncModeByte:
case encModeByte:
return binary.New(false, true, false, false)
case EncModeJP:
case encModeJP:
return binary.New(true, false, false, false)
default:
panic("no indicator")
}
}

// Encoder ... data to bit stream ...
type Encoder struct {
// encoder ... data to bit stream ...
type encoder struct {
// self init
dst *binary.Binary
data []byte // raw input data

// initial params
mode EncMode // encode mode
ecLv ECLevel // error correction level
mode encMode // encode mode
ecLv ecLevel // error correction level

// self load
version Version // QR version ref
version version // QR version ref
}

// Encode ...
// 1. encode raw data into bitset
// 2. append _defaultPadding data
//
func (e *Encoder) Encode(byts []byte) (*binary.Binary, error) {
func (e *encoder) Encode(byts []byte) (*binary.Binary, error) {
e.dst = binary.New()
e.data = byts

Expand All @@ -94,13 +94,13 @@ func (e *Encoder) Encode(byts []byte) (*binary.Binary, error) {

// encode data with specified mode
switch e.mode {
case EncModeNumeric:
case encModeNumeric:
e.encodeNumeric()
case EncModeAlphanumeric:
case encModeAlphanumeric:
e.encodeAlphanumeric()
case EncModeByte:
case encModeByte:
e.encodeByte()
case EncModeJP:
case encModeJP:
panic("this has not been finished")
}

Expand All @@ -111,7 +111,7 @@ func (e *Encoder) Encode(byts []byte) (*binary.Binary, error) {
}

// 0001b mode indicator
func (e *Encoder) encodeNumeric() {
func (e *encoder) encodeNumeric() {
if e.dst == nil {
log.Println("e.dst is nil")
return
Expand All @@ -132,7 +132,7 @@ func (e *Encoder) encodeNumeric() {
}

// 0010b mode indicator
func (e *Encoder) encodeAlphanumeric() {
func (e *encoder) encodeAlphanumeric() {
if e.dst == nil {
log.Println("e.dst is nil")
return
Expand All @@ -156,7 +156,7 @@ func (e *Encoder) encodeAlphanumeric() {
}

// 0100b mode indicator
func (e *Encoder) encodeByte() {
func (e *encoder) encodeByte() {
if e.dst == nil {
log.Println("e.dst is nil")
return
Expand All @@ -167,7 +167,7 @@ func (e *Encoder) encodeByte() {
}

// Break Up into 8-bit Codewords and Add Pad Bytes if Necessary
func (e *Encoder) breakUpInto8bit() {
func (e *encoder) breakUpInto8bit() {
// fill ending code (max 4bit)
// depends on max capacity of current version and EC level
maxCap := e.version.NumTotalCodewrods() * 8
Expand Down Expand Up @@ -219,7 +219,7 @@ var charCountMap = map[string]int{
}

// charCountBits
func (e *Encoder) charCountBits() int {
func (e *encoder) charCountBits() int {
var lv int
if v := e.version.Ver; v <= 9 {
lv = 9
Expand All @@ -228,7 +228,7 @@ func (e *Encoder) charCountBits() int {
} else {
lv = 40
}
pos := fmt.Sprintf("%d_%s", lv, GetEncModeName(e.mode))
pos := fmt.Sprintf("%d_%s", lv, getEncModeName(e.mode))
return charCountMap[pos]
}

Expand Down Expand Up @@ -274,25 +274,25 @@ type analyzeEncFunc func(byte) bool
// 如果输入字符串只包含数字(0-9),请使用数字编码模式。
// 在数字编码模式不适用的情况下,如果可以在字符索引表的左列中找到输入字符串中的所有字符,请使用字符编码模式。注意:小写字母不能使用字符编码模式。
// 在字符编码模式不适用的情况下,如果字符可以在ISO-8859-1字符集中找到,则使用字节编码模式。
func anlayzeMode(raw []byte) EncMode {
func anlayzeMode(raw []byte) encMode {
var (
analyFunc analyzeEncFunc = analyzeNum
encMode = EncModeNumeric
encMode = encModeNumeric
)
// check
for _, byt := range raw {
switch encMode {
case EncModeNumeric:
case encModeNumeric:
if !analyFunc(byt) {
encMode = EncModeAlphanumeric
encMode = encModeAlphanumeric
analyFunc = analyzeAlphaNum
}
case EncModeAlphanumeric:
case encModeAlphanumeric:
if !analyFunc(byt) {
encMode = EncModeByte
encMode = encModeByte
}
case EncModeByte:
return EncModeByte
case encModeByte:
return encModeByte
}
}
return encMode
Expand Down
26 changes: 13 additions & 13 deletions encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
// }

func TestEncodeNum(t *testing.T) {
enc := Encoder{
enc := encoder{
ecLv: Low,
mode: EncModeNumeric,
mode: encModeNumeric,
version: loadVersion(1, Low),
}

Expand All @@ -24,9 +24,9 @@ func TestEncodeNum(t *testing.T) {
}

func TestEncodeAlphanum(t *testing.T) {
enc := Encoder{
enc := encoder{
ecLv: Low,
mode: EncModeAlphanumeric,
mode: encModeAlphanumeric,
version: loadVersion(1, Low),
}

Expand All @@ -39,9 +39,9 @@ func TestEncodeAlphanum(t *testing.T) {
}

func TestEncodeByte(t *testing.T) {
enc := Encoder{
enc := encoder{
ecLv: Quart,
mode: EncModeByte,
mode: encModeByte,
version: loadVersion(5, Quart),
}

Expand Down Expand Up @@ -158,37 +158,37 @@ func Test_anlayzeMode(t *testing.T) {
tests := []struct {
name string
args args
want EncMode
want encMode
}{
{
name: "case 0",
args: args{raw: []byte("123120899231")},
want: EncModeNumeric,
want: encModeNumeric,
},
{
name: "case 1",
args: args{raw: []byte(":/1231H208*99231FBJO")},
want: EncModeAlphanumeric,
want: encModeAlphanumeric,
},
{
name: "case 2",
args: args{raw: []byte("hahah1298312hG&^FBJO@jhgG*")},
want: EncModeByte,
want: encModeByte,
},
{
name: "case 3",
args: args{raw: []byte("JKAHDOIANKQOIHCMJKASJ")},
want: EncModeAlphanumeric,
want: encModeAlphanumeric,
},
{
name: "case 4",
args: args{raw: []byte("https://baidu.com?keyword=_JSO==GA")},
want: EncModeByte,
want: encModeByte,
},
{
name: "case 5",
args: args{raw: []byte("这是汉字也应该是EncModeByte")},
want: EncModeByte,
want: encModeByte,
},
}
for _, tt := range tests {
Expand Down
59 changes: 1 addition & 58 deletions image.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package qrcode
import (
"fmt"
"image"
"image/color"
"image/jpeg"
"io"
"log"
Expand Down Expand Up @@ -47,9 +46,6 @@ func drawAndSave(w io.Writer, m matrix.Matrix, opt *outputImageOptions) (err err

// draw deal QRCode's matrix to be a image.Image
func draw(mat matrix.Matrix, opt *outputImageOptions) image.Image {
_stateToRGBA[matrix.StateFalse] = opt.backgroundColor()
_stateToRGBA[matrix.StateTrue] = opt.foregroundColor()

// w as image width, h as image height
w := mat.Width()*opt.qrBlockWidth() + 2*_defaultPadding
h := w
Expand Down Expand Up @@ -88,7 +84,7 @@ func draw(mat matrix.Matrix, opt *outputImageOptions) image.Image {
// TODO(@yeqown): make this abstract to Shape
for posX := xStart; posX < xEnd; posX++ {
for posY := yStart; posY < yEnd; posY++ {
rgba.Set(posX, posY, stateRGBA(v))
rgba.Set(posX, posY, opt.stateRGBA(v))
}
}
})
Expand Down Expand Up @@ -123,56 +119,3 @@ done:
func validLogoImage(qrWidth, qrHeight, logoWidth, logoHeight int) bool {
return qrWidth >= 5*logoWidth && qrHeight >= 5*logoHeight
}

var (
// _stateToRGBA state map tp color.Gray16
_stateToRGBA = map[matrix.State]color.Color{
matrix.StateFalse: hexToRGBA("#1aa6b7"),
matrix.StateTrue: hexToRGBA("#01c5c4"),
// matrix.StateInit: hexToRGBA("#1aa6b7"),
// matrix.StateVersion: hexToRGBA("#444444"),
// matrix.StateFormat: hexToRGBA("#555555"),
}

// _defaultStateColor default color of undefined matrix.State
// it shouldn't be used.
_defaultStateColor = hexToRGBA("#ff414d")
)

// hexToRGBA convert hex string into color.RGBA
func hexToRGBA(s string) color.RGBA {
c := color.RGBA{
R: 0,
G: 0,
B: 0,
A: 0xff,
}

var err error
switch len(s) {
case 7:
_, err = fmt.Sscanf(s, "#%02x%02x%02x", &c.R, &c.G, &c.B)
case 4:
_, err = fmt.Sscanf(s, "#%1x%1x%1x", &c.R, &c.G, &c.B)
// Double the hex digits:
c.R *= 17
c.G *= 17
c.B *= 17
default:
err = fmt.Errorf("invalid length, must be 7 or 4")
}
if err != nil {
panic(err)
}

return c
}

// stateRGBA get color.Color by value State
func stateRGBA(v matrix.State) color.Color {
if v, ok := _stateToRGBA[v]; ok {
return v
}

return _defaultStateColor
}
Loading

0 comments on commit f42e02b

Please sign in to comment.