浏览代码

perf: improve img bitmap fill performance

wangbei 7 年之前
父节点
当前提交
622db714a4
共有 1 个文件被更改,包括 6 次插入5 次删除
  1. 6 5
      qrcode.go

+ 6 - 5
qrcode.go

@@ -293,11 +293,11 @@ func (q *QRCode) Image(size int) image.Image {
 	// Saves a few bytes to have them in this order
 	// Saves a few bytes to have them in this order
 	p := color.Palette([]color.Color{q.BackgroundColor, q.ForegroundColor})
 	p := color.Palette([]color.Color{q.BackgroundColor, q.ForegroundColor})
 	img := image.NewPaletted(rect, p)
 	img := image.NewPaletted(rect, p)
+	bgClr := uint8(img.Palette.Index(q.BackgroundColor))
+	fgClr := uint8(img.Palette.Index(q.ForegroundColor))
 
 
-	for i := 0; i < size; i++ {
-		for j := 0; j < size; j++ {
-			img.Set(i, j, q.BackgroundColor)
-		}
+	for i := range img.Pix {
+		img.Pix[i] = bgClr
 	}
 	}
 
 
 	bitmap := q.symbol.bitmap()
 	bitmap := q.symbol.bitmap()
@@ -308,7 +308,8 @@ func (q *QRCode) Image(size int) image.Image {
 				startY := y*pixelsPerModule + offset
 				startY := y*pixelsPerModule + offset
 				for i := startX; i < startX+pixelsPerModule; i++ {
 				for i := startX; i < startX+pixelsPerModule; i++ {
 					for j := startY; j < startY+pixelsPerModule; j++ {
 					for j := startY; j < startY+pixelsPerModule; j++ {
-						img.Set(i, j, q.ForegroundColor)
+						pos := img.PixOffset(i, j)
+						img.Pix[pos] = fgClr
 					}
 					}
 				}
 				}
 			}
 			}