Skip to content

Commit

Permalink
styl(draw): rename drawAndSave to drawTo
Browse files Browse the repository at this point in the history
  • Loading branch information
yeqown committed Nov 4, 2021
1 parent c284d29 commit 0cbd44a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 91 deletions.
97 changes: 7 additions & 90 deletions image.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"github.com/yeqown/go-qrcode/matrix"
)

// Draw image with matrix

var (
_defaultFilename = "default.jpeg"
_defaultPadding = 40
Expand All @@ -31,106 +29,25 @@ func drawAndSaveToFile(name string, m matrix.Matrix, opt *outputImageOptions) er
err = f.Close()
}(f)

return drawAndSave(f, m, opt)
return drawTo(f, m, opt)
}

// drawAndSave save image into io.Writer
func drawAndSave(w io.Writer, m matrix.Matrix, imgOpt *outputImageOptions) (err error) {
if imgOpt == nil {
imgOpt = defaultOutputImageOption()
// drawTo save image into io.Writer
func drawTo(w io.Writer, m matrix.Matrix, opt *outputImageOptions) (err error) {
if opt == nil {
opt = defaultOutputImageOption()
}

img := draw(m, imgOpt)
img := draw(m, opt)

// DONE(@yeqown): support file format specified config option
if err = imgOpt.imageEncoder.Encode(w, img); err != nil {
if err = opt.imageEncoder.Encode(w, img); err != nil {
err = fmt.Errorf("jpeg.Encode got err: %v", err)
}

return
}

//
//// draw deal QRCode's matrix to be a image.Image
//func draw(mat matrix.Matrix, opt *outputImageOptions) image.Image {
// // w as image width, h as image height
// w := mat.Width()*opt.qrBlockWidth() + 2*_defaultPadding
// h := w
// rgba := image.NewRGBA(image.Rect(0, 0, w, h))
//
// // top-bottom _defaultPadding
// for posX := 0; posX < w; posX++ {
// for posY := 0; posY < _defaultPadding; posY++ {
// rgba.Set(posX, posY, opt.backgroundColor())
// }
//
// for posY := h - _defaultPadding; posY < h; posY++ {
// rgba.Set(posX, posY, opt.backgroundColor())
// }
// }
//
// // left-right _defaultPadding
// for posY := _defaultPadding; posY < h-_defaultPadding; posY++ {
// for posX := 0; posX < _defaultPadding; posX++ {
// rgba.Set(posX, posY, opt.backgroundColor())
// }
//
// for posX := w - _defaultPadding; posX < w; posX++ {
// rgba.Set(posX, posY, opt.backgroundColor())
// }
// }
//
// ctx := &DrawContext{
// upperLeft: image.Point{}, // useless
// lowerRight: image.Point{}, // useless
// img: rgba,
// color: color.Black, // useless
// }
// shape := opt.getShape()
//
// // iterate the matrix to Draw each pixel
// mat.Iterate(matrix.ROW, func(x int, y int, v matrix.State) {
// // Draw the block
// ctx.upperLeft = image.Point{
// X: x*opt.qrBlockWidth() + _defaultPadding,
// Y: y*opt.qrBlockWidth() + _defaultPadding,
// }
// ctx.lowerRight = image.Point{
// X: (x+1)*opt.qrBlockWidth() + _defaultPadding,
// Y: (y+1)*opt.qrBlockWidth() + _defaultPadding,
// }
// ctx.color = opt.stateRGBA(v)
// // DONE(@yeqown): make this abstract to Shapes
// shape.Draw(ctx)
// })
//
// // DONE(@yeqown): add logo image
// if opt.logoImage() != nil {
// // Draw logo image into rgba
// bound := opt.logo.Bounds()
// upperLeft, lowerRight := bound.Min, bound.Max
// logoWidth, logoHeight := lowerRight.X-upperLeft.X, lowerRight.Y-upperLeft.Y
//
// if !validLogoImage(w, h, logoWidth, logoHeight) {
// log.Printf("w=%d, h=%d, logoW=%d, logoH=%d, logo is over than 1/5 of QRCode \n",
// w, h, logoWidth, logoHeight)
// goto done
// }
//
// // DONE(@yeqown): calculate the xOffset and yOffset
// // which point(xOffset, yOffset) should icon upper-left to start
// xOffset, yOffset := (w-logoWidth)/2, (h-logoHeight)/2
//
// for posX := upperLeft.X; posX < lowerRight.X; posX++ {
// for posY := upperLeft.Y; posY < lowerRight.Y; posY++ {
// rgba.Set(posX+xOffset, posY+yOffset, opt.logo.At(posX, posY))
// }
// }
// }
//done:
// return rgba
//}

// draw deal QRCode's matrix to be an image.Image. Notice that if anyone changed this function,
// please also check the function outputImageOptions.preCalculateAttribute().
func draw(mat matrix.Matrix, opt *outputImageOptions) image.Image {
Expand Down
2 changes: 1 addition & 1 deletion qrcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ func (q *QRCode) Save(saveToPath string) (err error) {
// SaveTo QRCode image into `w`(io.Writer)
func (q *QRCode) SaveTo(w io.Writer) error {
q.draw()
return drawAndSave(w, *q.mat, q.outputOption)
return drawTo(w, *q.mat, q.outputOption)
}

// Attribute pre-calculate attribute of QR Code image, before actually draw it into image.
Expand Down

0 comments on commit 0cbd44a

Please sign in to comment.