mirror of
https://github.com/coalaura/ffwebp.git
synced 2025-07-17 22:04:35 +00:00
webp
This commit is contained in:
8
internal/builtins/webp.go
Normal file
8
internal/builtins/webp.go
Normal file
@ -0,0 +1,8 @@
|
||||
//go:build webp || core || full
|
||||
// +build webp core full
|
||||
|
||||
package builtins
|
||||
|
||||
import (
|
||||
_ "github.com/coalaura/ffwebp/internal/codec/webp"
|
||||
)
|
@ -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
|
||||
}
|
||||
|
||||
|
87
internal/codec/webp/webp.go
Normal file
87
internal/codec/webp/webp.go
Normal 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,
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user