Browse Source

Merge pull request #20 from warbaby/img_set_performance

Improve img bitmap fill performance
tfh 7 years ago
parent
commit
dc11ecdae0
1 changed files with 3 additions and 7 deletions
  1. 3 7
      qrcode.go

+ 3 - 7
qrcode.go

@@ -293,12 +293,7 @@ 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)
-
-	for i := 0; i < size; i++ {
-		for j := 0; j < size; j++ {
-			img.Set(i, j, q.BackgroundColor)
-		}
-	}
+	fgClr := uint8(img.Palette.Index(q.ForegroundColor))
 
 
 	bitmap := q.symbol.bitmap()
 	bitmap := q.symbol.bitmap()
 	for y, row := range bitmap {
 	for y, row := range bitmap {
@@ -308,7 +303,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
 					}
 					}
 				}
 				}
 			}
 			}