Skip to content

Commit 622db71

Browse files
author
wangbei
committed
perf: improve img bitmap fill performance
1 parent bcdd5e3 commit 622db71

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

qrcode.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -293,11 +293,11 @@ func (q *QRCode) Image(size int) image.Image {
293293
// Saves a few bytes to have them in this order
294294
p := color.Palette([]color.Color{q.BackgroundColor, q.ForegroundColor})
295295
img := image.NewPaletted(rect, p)
296+
bgClr := uint8(img.Palette.Index(q.BackgroundColor))
297+
fgClr := uint8(img.Palette.Index(q.ForegroundColor))
296298

297-
for i := 0; i < size; i++ {
298-
for j := 0; j < size; j++ {
299-
img.Set(i, j, q.BackgroundColor)
300-
}
299+
for i := range img.Pix {
300+
img.Pix[i] = bgClr
301301
}
302302

303303
bitmap := q.symbol.bitmap()
@@ -308,7 +308,8 @@ func (q *QRCode) Image(size int) image.Image {
308308
startY := y*pixelsPerModule + offset
309309
for i := startX; i < startX+pixelsPerModule; i++ {
310310
for j := startY; j < startY+pixelsPerModule; j++ {
311-
img.Set(i, j, q.ForegroundColor)
311+
pos := img.PixOffset(i, j)
312+
img.Pix[pos] = fgClr
312313
}
313314
}
314315
}

0 commit comments

Comments
 (0)