1
0
mirror of https://github.com/golang/go synced 2024-11-23 08:10:03 -07:00

image/gif: fix GIF encoding of sub-images.

benchmark                    old ns/op     new ns/op     delta
BenchmarkEncode              8641055       8646829       +0.07%

Fixes #7792.

LGTM=r
R=r
CC=dbathgate, golang-codereviews
https://golang.org/cl/147730043
This commit is contained in:
Nigel Tao 2014-09-18 12:43:01 +10:00
parent a2e7fd0fc1
commit a2910958a2
2 changed files with 37 additions and 4 deletions

View File

@ -233,11 +233,21 @@ func (e *encoder) writeImageBlock(pm *image.Paletted, delay int) {
e.writeByte(uint8(litWidth)) // LZW Minimum Code Size.
lzww := lzw.NewWriter(blockWriter{e: e}, lzw.LSB, litWidth)
if dx := b.Dx(); dx == pm.Stride {
_, e.err = lzww.Write(pm.Pix)
if e.err != nil {
lzww.Close()
return
}
} else {
for i, y := 0, b.Min.Y; y < b.Max.Y; i, y = i+pm.Stride, y+1 {
_, e.err = lzww.Write(pm.Pix[i : i+dx])
if e.err != nil {
lzww.Close()
return
}
}
}
lzww.Close()
e.writeByte(0x00) // Block Terminator.
}

View File

@ -102,6 +102,29 @@ func TestWriter(t *testing.T) {
}
}
func TestSubImage(t *testing.T) {
m0, err := readImg("../testdata/video-001.gif")
if err != nil {
t.Fatalf("readImg: %v", err)
}
m0 = m0.(*image.Paletted).SubImage(image.Rect(0, 0, 50, 30))
var buf bytes.Buffer
err = Encode(&buf, m0, nil)
if err != nil {
t.Fatalf("Encode: %v", err)
}
m1, err := Decode(&buf)
if err != nil {
t.Fatalf("Decode: %v", err)
}
if m0.Bounds() != m1.Bounds() {
t.Fatalf("bounds differ: %v and %v", m0.Bounds(), m1.Bounds())
}
if averageDelta(m0, m1) != 0 {
t.Fatalf("images differ")
}
}
var frames = []string{
"../testdata/video-001.gif",
"../testdata/video-005.gray.gif",