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 · 3 comments · May be fixed by #46813

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
Contributor

@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.

Loading

@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
@nigeltao
Copy link
Contributor

@nigeltao nigeltao commented Oct 29, 2020

Sounds like a reasonable behavior change, although I don't have much spare time to work on this.

Loading

chrisgillis added a commit to chrisgillis/go that referenced this issue Jun 18, 2021
The existing decode() method offers the ability to keep just one
frame of the GIF image, however it will read and decompress all
subsequent frames regardless.

Fixes golang#41142
@gopherbot
Copy link

@gopherbot gopherbot commented Jun 18, 2021

Change https://golang.org/cl/329329 mentions this issue: image/gif: avoid decoding past the first frame in decode()

Loading

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.

4 participants