Skip to content

Commit

Permalink
image/gif: fix GIF encoding of sub-images.
Browse files Browse the repository at this point in the history
benchmark                    old ns/op     new ns/op     delta
BenchmarkEncode              8641055       8646829       +0.07%

Fixes golang#7792.

LGTM=r
R=r
CC=dbathgate, golang-codereviews
https://golang.org/cl/147730043
  • Loading branch information
nigeltao committed Sep 18, 2014
1 parent a2e7fd0 commit a291095
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/image/gif/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,20 @@ 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)
_, e.err = lzww.Write(pm.Pix)
if e.err != nil {
lzww.Close()
return
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.
Expand Down
23 changes: 23 additions & 0 deletions src/image/gif/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit a291095

Please sign in to comment.