mirror of
https://github.com/coalaura/ffwebp.git
synced 2025-07-18 06:14:34 +00:00
gif
This commit is contained in:
72
internal/codec/gif/gif.go
Normal file
72
internal/codec/gif/gif.go
Normal file
@ -0,0 +1,72 @@
|
||||
package gif
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/gif"
|
||||
"io"
|
||||
|
||||
"github.com/coalaura/ffwebp/internal/codec"
|
||||
"github.com/coalaura/ffwebp/internal/opts"
|
||||
"github.com/urfave/cli/v3"
|
||||
)
|
||||
|
||||
var numColors int
|
||||
|
||||
func init() {
|
||||
codec.Register(impl{})
|
||||
}
|
||||
|
||||
type impl struct{}
|
||||
|
||||
func (impl) Name() string {
|
||||
return "gif"
|
||||
}
|
||||
|
||||
func (impl) Extensions() []string {
|
||||
return []string{"gif"}
|
||||
}
|
||||
|
||||
func (impl) Flags(flags []cli.Flag) []cli.Flag {
|
||||
return append(flags, &cli.IntFlag{
|
||||
Name: "gif.colors",
|
||||
Usage: "GIF: Number of colors (1-256)",
|
||||
Value: 256,
|
||||
Destination: &numColors,
|
||||
Validator: func(value int) error {
|
||||
if value < 1 || value > 256 {
|
||||
return fmt.Errorf("invalid number of colors: %d", value)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (impl) Sniff(reader io.ReaderAt) (int, error) {
|
||||
magic7a := []byte("GIF87a")
|
||||
magic9a := []byte("GIF89a")
|
||||
|
||||
buf := make([]byte, 6)
|
||||
|
||||
if _, err := reader.ReadAt(buf, 0); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if bytes.Equal(buf, magic7a) || bytes.Equal(buf, magic9a) {
|
||||
return 100, nil
|
||||
}
|
||||
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func (impl) Decode(reader io.Reader) (image.Image, error) {
|
||||
return gif.Decode(reader)
|
||||
}
|
||||
|
||||
func (impl) Encode(writer io.Writer, img image.Image, options opts.Common) error {
|
||||
return gif.Encode(writer, img, &gif.Options{
|
||||
NumColors: numColors,
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user