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

Closed
kawaiiDango opened this issue Aug 31, 2020 · 4 comments · May be fixed by #46813
Labels
FeatureRequest FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@kawaiiDango
Copy link

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

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 FeatureRequest NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Aug 31, 2020
@dmitshur dmitshur added this to the Backlog milestone Aug 31, 2020
@nigeltao
Copy link
Contributor

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

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

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

@NathanBaulch
Copy link

FWIW, I needed this along with a few other more advanced animated gif features (encoding gifs one frame at a time, comment metadata, etc) so I forked image/gif and implemented them here: https://github.com/NathanBaulch/gifx

@dmitshur dmitshur added NeedsFix The path to resolution is known, but the work has not been done. and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels May 20, 2022
@dmitshur dmitshur modified the milestones: Backlog, Go1.20 Aug 22, 2022
@golang golang locked and limited conversation to collaborators Aug 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FeatureRequest FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants