Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

image/gif: Encode does not preserve Bounds.Min #11149

Closed
dvyukov opened this issue Jun 10, 2015 · 1 comment
Closed

image/gif: Encode does not preserve Bounds.Min #11149

dvyukov opened this issue Jun 10, 2015 · 1 comment
Assignees
Milestone

Comments

@dvyukov
Copy link
Member

@dvyukov dvyukov commented Jun 10, 2015

The following program fails:

package main

import (
    "bytes"
    "fmt"
    "image/gif"
    "reflect"
)

func main() {
    data := []byte("GIF89a\n\x0000000,\x00\x000\x00\n\x00\n\x00\x80000000\x02\b\xf01u\xb9\xfdal\x05\x00;")
    img, err := gif.Decode(bytes.NewReader(data))
    if err != nil {
        panic(err)
    }
    var w bytes.Buffer
    err = gif.Encode(&w, img, nil)
    if err != nil {
        panic(err)
    }
    img1, err := gif.Decode(&w)
    if err != nil {
        panic(err)
    }
    if !reflect.DeepEqual(img.Bounds(), img1.Bounds()) {
        fmt.Printf("img0: %#v\n", img.Bounds())
        fmt.Printf("img1: %#v\n", img1.Bounds())
        panic("bounds changed")
    }
}
img0: image.Rectangle{Min:image.Point{X:0, Y:48}, Max:image.Point{X:10, Y:58}}
img1: image.Rectangle{Min:image.Point{X:0, Y:0}, Max:image.Point{X:10, Y:10}}
panic: bounds changed

Bounds.Min should be preserved by Encode.

go version devel +b0532a9 Mon Jun 8 05:13:15 2015 +0000 linux/amd64

@ianlancetaylor ianlancetaylor added this to the Go1.5Maybe milestone Jun 10, 2015
@jeffallen
Copy link
Contributor

@jeffallen jeffallen commented Jun 18, 2015

Works as designed:

// When calling Encode instead of EncodeAll, the single-frame image is
// translated such that its top-left corner is (0, 0), so that the single
// frame completely fills the overall GIF's bounds.

This test of a round trip through DecodeAll/EncodeAll passes:

func TestRoundtrip(t *testing.T) {
data := []byte("GIF89a\n\x0000000,\x00\x000\x00\n\x00\n\x00\x80000000\x02\b\xf01u\xb9\xfdal\x05\x00;")
gif, err := DecodeAll(bytes.NewReader(data))
if err != nil {
t.Fatal(err)
}
var w bytes.Buffer
err = EncodeAll(&w, gif)
if err != nil {
t.Fatal(err)
}
gif1, err := DecodeAll(&w)
if err != nil {
t.Fatal(err)
}
if len(gif.Image) != len(gif1.Image) {
t.Fatal("wrong number of images")
}

if !reflect.DeepEqual(gif.Image[0].Bounds(), gif1.Image[0].Bounds()) {
    t.Fatal("bounds changed: %v -> %v", gif.Image[0].Bounds(), gif1.Image[0].Bounds())
}

}

@bradfitz bradfitz closed this Jun 18, 2015
@golang golang locked and limited conversation to collaborators Jun 25, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants
You can’t perform that action at this time.