Commit ad7d24ac authored by Brad Fitzpatrick's avatar Brad Fitzpatrick Committed by Nigel Tao

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
parent d86ab015
...@@ -311,9 +311,8 @@ func writeImage(w io.Writer, m image.Image, cb int) os.Error { ...@@ -311,9 +311,8 @@ func writeImage(w io.Writer, m image.Image, cb int) os.Error {
cr[0][3*x+3] = uint8(b >> 8) cr[0][3*x+3] = uint8(b >> 8)
} }
case cbP8: case cbP8:
for x := b.Min.X; x < b.Max.X; x++ { rowOffset := y * paletted.Stride
cr[0][x+1] = paletted.ColorIndexAt(x, y) copy(cr[0][b.Min.X+1:], paletted.Pix[rowOffset+b.Min.X:rowOffset+b.Max.X])
}
case cbTCA8: case cbTCA8:
// Convert from image.Image (which is alpha-premultiplied) to PNG's non-alpha-premultiplied. // Convert from image.Image (which is alpha-premultiplied) to PNG's non-alpha-premultiplied.
for x := b.Min.X; x < b.Max.X; x++ { for x := b.Min.X; x < b.Max.X; x++ {
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
package png package png
import ( import (
"bytes"
"fmt" "fmt"
"image" "image"
"io" "io"
...@@ -68,3 +69,18 @@ func TestWriter(t *testing.T) { ...@@ -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)
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment