From 2a5bbe4fbbf8b5471bc3052051eed5477c308fd1 Mon Sep 17 00:00:00 2001 From: Laura Date: Thu, 19 Jun 2025 16:29:27 +0200 Subject: [PATCH] webp --- go.mod | 10 ++++- go.sum | 6 +++ internal/builtins/webp.go | 8 ++++ internal/codec/detect.go | 4 ++ internal/codec/webp/webp.go | 87 +++++++++++++++++++++++++++++++++++++ 5 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 internal/builtins/webp.go create mode 100644 internal/codec/webp/webp.go diff --git a/go.mod b/go.mod index 818f443..fbc8673 100644 --- a/go.mod +++ b/go.mod @@ -4,4 +4,12 @@ go 1.24.2 require github.com/urfave/cli/v3 v3.3.8 -require golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 +require ( + github.com/gen2brain/webp v0.5.5 + golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 +) + +require ( + github.com/ebitengine/purego v0.8.3 // indirect + github.com/tetratelabs/wazero v1.9.0 // indirect +) diff --git a/go.sum b/go.sum index eaef168..1c0b020 100644 --- a/go.sum +++ b/go.sum @@ -1,9 +1,15 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/ebitengine/purego v0.8.3 h1:K+0AjQp63JEZTEMZiwsI9g0+hAMNohwUOtY0RPGexmc= +github.com/ebitengine/purego v0.8.3/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ= +github.com/gen2brain/webp v0.5.5 h1:MvQR75yIPU/9nSqYT5h13k4URaJK3gf9tgz/ksRbyEg= +github.com/gen2brain/webp v0.5.5/go.mod h1:xOSMzp4aROt2KFW++9qcK/RBTOVC2S9tJG66ip/9Oc0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/tetratelabs/wazero v1.9.0 h1:IcZ56OuxrtaEz8UYNRHBrUa9bYeX9oVY93KspZZBf/I= +github.com/tetratelabs/wazero v1.9.0/go.mod h1:TSbcXCfFP0L2FGkRPxHphadXPjo1T6W+CseNNY7EkjM= github.com/urfave/cli/v3 v3.3.8 h1:BzolUExliMdet9NlJ/u4m5vHSotJ3PzEqSAZ1oPMa/E= github.com/urfave/cli/v3 v3.3.8/go.mod h1:FJSKtM/9AiiTOJL4fJ6TbMUkxBXn7GO9guZqoZtpYpo= golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 h1:hVwzHzIUGRjiF7EcUjqNxk3NCfkPxbDKRdnNE1Rpg0U= diff --git a/internal/builtins/webp.go b/internal/builtins/webp.go new file mode 100644 index 0000000..31d202c --- /dev/null +++ b/internal/builtins/webp.go @@ -0,0 +1,8 @@ +//go:build webp || core || full +// +build webp core full + +package builtins + +import ( + _ "github.com/coalaura/ffwebp/internal/codec/webp" +) diff --git a/internal/codec/detect.go b/internal/codec/detect.go index 577323f..217f010 100644 --- a/internal/codec/detect.go +++ b/internal/codec/detect.go @@ -46,6 +46,10 @@ func Sniff(reader io.Reader) (*Sniffed, io.Reader, error) { for _, codec := range codecs { confidence, header, err := codec.Sniff(ra) if err != nil { + if errors.Is(err, io.EOF) { + continue + } + return nil, nil, err } diff --git a/internal/codec/webp/webp.go b/internal/codec/webp/webp.go new file mode 100644 index 0000000..e4ca401 --- /dev/null +++ b/internal/codec/webp/webp.go @@ -0,0 +1,87 @@ +package bmp + +import ( + "bytes" + "fmt" + "image" + "io" + + "github.com/gen2brain/webp" + + "github.com/coalaura/ffwebp/internal/codec" + "github.com/coalaura/ffwebp/internal/logx" + "github.com/coalaura/ffwebp/internal/opts" + "github.com/urfave/cli/v3" +) + +var ( + method int + exact bool +) + +func init() { + codec.Register(impl{}) +} + +type impl struct{} + +func (impl) String() string { + return "webp" +} + +func (impl) Extensions() []string { + return []string{"webp"} +} + +func (impl) Flags(flags []cli.Flag) []cli.Flag { + return append(flags, + &cli.IntFlag{ + Name: "webp.method", + Usage: "WebP: quality/speed trade-off (0=fast, 6=slower-better)", + Value: 4, + Destination: &method, + Validator: func(v int) error { + if v < 0 || v > 6 { + return fmt.Errorf("invalid webp.method: %d (must be 0-6)", v) + } + + return nil + }, + }, + &cli.BoolFlag{ + Name: "webp.exact", + Usage: "WebP: preserve exact RGB values in transparent areas", + Value: false, + Destination: &exact, + }, + ) +} + +func (impl) Sniff(rd io.ReaderAt) (int, []byte, error) { + buf := make([]byte, 12) + + if _, err := rd.ReadAt(buf, 0); err != nil { + return 0, nil, err + } + + if bytes.Equal(buf[0:4], []byte("RIFF")) && bytes.Equal(buf[8:12], []byte("WEBP")) { + return 100, buf[:12], nil + } + + return 0, nil, nil +} + +func (impl) Decode(reader io.Reader) (image.Image, error) { + return webp.Decode(reader) +} + +func (impl) Encode(writer io.Writer, img image.Image, options opts.Common) error { + logx.Printf("webp: quality=%d lossless=%t method=%d exact=%t\n", options.Quality, options.Lossless, method, exact) + + return webp.Encode(writer, img, webp.Options{ + Quality: options.Quality, + Lossless: options.Lossless, + Method: method, + Exact: exact, + }) +}