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: Decode reads the entire animated gif image, even though it returns only the first frame (while DecodeAll exists to read and return all frames) #41142

Open
kawaiiDango opened this issue Aug 31, 2020 · 1 comment

Comments

@kawaiiDango
Copy link

@kawaiiDango kawaiiDango commented Aug 31, 2020

What version of Go are you using (go version)?

1.15

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\arn\AppData\Local\go-build
set GOENV=C:\Users\arn\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\arn\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\arn\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=c:\go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=c:\go\pkg\tool\windows_amd64
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\arn\AppData\Local\Temp\go-build610476046=/tmp/go-build -gno-record-gcc-switches

What did you do?

func test() *image.Image {
	resp, _ := httpClient.Get("https://media.giphy.com/media/URdTwRx7YNcoefXsoi/giphy.gif")
	defer resp.Body.Close()

	fmt.Println("decode started")
	img, err := gif.Decode(resp.Body)
	if err != nil {
		fmt.Println(err)
		panic("gif error")
	}
	fmt.Println("decoded")
	return &img
}

What did you expect to see?

It should stop reading from the stream and return as soon as it is done decoding the first frame. Saves a lot of time and bandwidth for large animated gifs from the network.

What did you see instead?

reads the entire stream of an animated gif (4.5 MB in this case), taking over a minute for large gifs on a slow connection in this case

@dmitshur
Copy link
Member

@dmitshur dmitshur commented Aug 31, 2020

There is a DecodeAll function that reads a GIF image and returns all relevant information. The Decode function returns only the first frame and an error, however it still reads everything.

If I understand correctly, this a feature request to change the behavior of Decode so it stops short, in contrast to DecodeAll.

However, it would mean Decode would no longer reports an error if there is a problem reading something past the first frame.

/cc @nigeltao @robpike per owners.

@dmitshur dmitshur changed the title image/gif: gif.Decode() reads the entire stream of an animated gif even if it should only return the first frame image/gif: Decode reads the entire animated gif image, even though it returns only the first frame (while DecodeAll exists to read and return all frames) Aug 31, 2020
@dmitshur dmitshur added this to the Backlog milestone Aug 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

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