golang / go

The Go programming language

Home Page:https://go.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

image/gif: LoopCount changes on round trip

dvyukov opened this issue · comments

The following program:

package main

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

func main() {
    data := []byte("GIF89a000\x00000,0\x00\x00\x00\n\x00" +
        "\n\x00\x80000000\x02\b\xf01u\xb9\xfdal\x05\x00;")
    img, err := gif.DecodeAll(bytes.NewReader(data))
    if err != nil {
        return
    }
    w := new(bytes.Buffer)
    err = gif.EncodeAll(w, img)
    if err != nil {
        panic(err)
    }
    img1, err := gif.DecodeAll(w)
    if err != nil {
        panic(err)
    }
    fmt.Printf("LoopCount: %v -> %v\n", img.LoopCount, img1.LoopCount)
}

prints:

LoopCount: 0 -> -1

Image should be preserved after Encode/Decode round trip. A more consistent behavior would be to:

  • return error from first Decode for LoopCount=0
  • encode image with LoopCount=0
  • patch LoopCount from 0 to -1 in Decode

Can't find anything definitive in the GIF spec on this.

go version devel +514014c Thu Jun 18 15:54:35 2015 +0200 linux/amd64

Well, LoopCount of 0 loops like a reasonable value (don't loop). So I think we need to encode it as 0.

Agreed that it should encode to 0 as a sane default, but FYI a LoopCount of 0 implies that the image should loop infinitely as opposed to not looping at all. (http://www.vurdalakov.net/misc/gif/netscape-looping-application-extension)

Rather simplistic fix proposed here: https://go-review.googlesource.com/#/c/11256/