davidbyttow / govips

A lightning fast image processing and resizing library for Go
MIT License
1.3k stars 200 forks source link

how to set webp's loop count if the original picture is a gif with GIF87a type? #381

Open t00350320 opened 1 year ago

t00350320 commented 1 year ago

We have a task to transfer a gif(GIF87a, gif1.zip) to webp, here is our govips demo code:

func main() {
        vips.Startup(nil)
        defer vips.Shutdown()
        p:=vips.NewImportParams()
        p.NumPages.Set(-1)
        imagePath := "gif1.gif"
        buf, err := ioutil.ReadFile(imagePath)  
        imageref,err := vips.LoadImageFromBuffer(buf,p)
        if err !=nil { 
          fmt.Println(err)
        }
        checkError(err)
        //imageref.RemoveMetadata()

        ep := vips.NewDefaultWEBPExportParams()
        image1bytes, _, err := imageref.Export(ep)
        err = ioutil.WriteFile("output.webp", image1bytes, 0644)
        checkError(err)

}

in order to get a thin image, we default use "RemoveMetadata()" before Export to webp, the output webp's loop count is 0 , so the webp loop forever.

Chunk ANIM at offset     30, length     14
  Background color:(ARGB) ff ff ff ff
  Loop count      : 0

if we remove "RemoveMetadata()" , the output is 1, the webp loop once

Chunk ANIM at offset     30, length     14
  Background color:(ARGB) ff ff ff ff
  Loop count      : 1

So,webp "Loop count:" parameter differs with the one that execute "RemoveMetadata()" , two doubts here: a:) why RemoveMetadata() affects "Loop count : 0" parameter? b:) how to set webp's loop count if the original picture is a gif with GIF87a type? We also tested the normal GIF89a type(gif2.zip) picture with loop 2, it works well when transfered to webp.

FYI: gif may has two types: GIF87a, GIF89a. While 87a has no reference to loop count and 89a's loop count is set near "0x31d". For example, 89a type gif (gif2.zip), “31D: 00 00 # Loop forever". gif89a doc,http://www.vurdalakov.net/misc/gif/netscape-looping-application-extension

t00350320 commented 1 year ago

gif1.zip

t00350320 commented 1 year ago

gif2.zip