1
0
mirror of https://github.com/coalaura/ffwebp.git synced 2025-07-17 22:04:35 +00:00
This commit is contained in:
Laura
2025-06-19 16:29:27 +02:00
parent 6ddf02ff19
commit 2a5bbe4fbb
5 changed files with 114 additions and 1 deletions

10
go.mod
View File

@ -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
)

6
go.sum
View File

@ -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=

View File

@ -0,0 +1,8 @@
//go:build webp || core || full
// +build webp core full
package builtins
import (
_ "github.com/coalaura/ffwebp/internal/codec/webp"
)

View File

@ -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
}

View File

@ -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,
})
}