1
0
mirror of https://github.com/golang/go synced 2024-11-19 02:34:44 -07:00

image/png: speed up paletted encoding ~25%

Avoids a lot of redundant bounds checks.

R=nigeltao, rsc
CC=golang-dev
https://golang.org/cl/2678041
This commit is contained in:
Brad Fitzpatrick 2010-10-27 22:48:18 +11:00 committed by Nigel Tao
parent d86ab015f7
commit ad7d24ac4b
2 changed files with 18 additions and 3 deletions

View File

@ -311,9 +311,8 @@ func writeImage(w io.Writer, m image.Image, cb int) os.Error {
cr[0][3*x+3] = uint8(b >> 8)
}
case cbP8:
for x := b.Min.X; x < b.Max.X; x++ {
cr[0][x+1] = paletted.ColorIndexAt(x, y)
}
rowOffset := y * paletted.Stride
copy(cr[0][b.Min.X+1:], paletted.Pix[rowOffset+b.Min.X:rowOffset+b.Max.X])
case cbTCA8:
// Convert from image.Image (which is alpha-premultiplied) to PNG's non-alpha-premultiplied.
for x := b.Min.X; x < b.Max.X; x++ {

View File

@ -5,6 +5,7 @@
package png
import (
"bytes"
"fmt"
"image"
"io"
@ -68,3 +69,18 @@ func TestWriter(t *testing.T) {
}
}
}
func BenchmarkEncodePaletted(b *testing.B) {
b.StopTimer()
img := image.NewPaletted(640, 480,
[]image.Color{
image.RGBAColor{0, 0, 0, 255},
image.RGBAColor{255, 255, 255, 255},
})
b.StartTimer()
buffer := new(bytes.Buffer)
for i := 0; i < b.N; i++ {
buffer.Reset()
Encode(buffer, img)
}
}