diff --git a/.gitignore b/.gitignore index 334fc90..ccd315c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ bin example.* test.* -*.exe \ No newline at end of file +*.exe +ffwebp \ No newline at end of file diff --git a/cmd/ffwebp/main.go b/cmd/ffwebp/main.go index ab8d10e..b01ae35 100644 --- a/cmd/ffwebp/main.go +++ b/cmd/ffwebp/main.go @@ -80,7 +80,7 @@ func main() { } if err := app.Run(context.Background(), os.Args); err != nil { - logx.Errorf("fatal: %v", err) + logx.Errorf("fatal: %v\n", err) } } @@ -112,6 +112,13 @@ func run(_ context.Context, cmd *cli.Command) error { logx.Printf("reading input from \n") } + sniffed, reader, err := codec.Sniff(reader) + if err != nil { + return err + } + + logx.Printf("sniffed codec: %s (%q)\n", sniffed.Codec, sniffed) + if output = cmd.String("output"); output != "-" { logx.Printf("opening output file %q\n", filepath.ToSlash(output)) @@ -132,13 +139,6 @@ func run(_ context.Context, cmd *cli.Command) error { common.FillDefaults() - sniffed, reader, err := codec.Sniff(reader) - if err != nil { - return err - } - - logx.Printf("sniffed codec: %s (%q)\n", sniffed.Codec, sniffed) - oCodec, err := codec.Detect(output, cmd.String("codec")) if err != nil { return err diff --git a/go.mod b/go.mod index 597970f..ade2632 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.24.2 require github.com/urfave/cli/v3 v3.3.8 require ( + github.com/gen2brain/avif v0.4.4 github.com/gen2brain/webp v0.5.5 github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 diff --git a/go.sum b/go.sum index d4bf5c5..e182d1b 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ 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/avif v0.4.4 h1:Ga/ss7qcWWQm2bxFpnjYjhJsNfZrWs5RsyklgFjKRSE= +github.com/gen2brain/avif v0.4.4/go.mod h1:/XCaJcjZraQwKVhpu9aEd9aLOssYOawLvhMBtmHVGqk= 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/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ= diff --git a/internal/builtins/avif.go b/internal/builtins/avif.go new file mode 100644 index 0000000..0ae1546 --- /dev/null +++ b/internal/builtins/avif.go @@ -0,0 +1,8 @@ +//go:build avif || core || full +// +build avif core full + +package builtins + +import ( + _ "github.com/coalaura/ffwebp/internal/codec/avif" +) diff --git a/internal/codec/avif/avif.go b/internal/codec/avif/avif.go new file mode 100644 index 0000000..60bb6ea --- /dev/null +++ b/internal/codec/avif/avif.go @@ -0,0 +1,119 @@ +package avif + +import ( + "bytes" + "fmt" + "image" + "io" + + "github.com/gen2brain/avif" + + "github.com/coalaura/ffwebp/internal/codec" + "github.com/coalaura/ffwebp/internal/logx" + "github.com/coalaura/ffwebp/internal/opts" + "github.com/urfave/cli/v3" +) + +var ( + quality int + qualityA int + speed int + chroma int +) + +func init() { + codec.Register(impl{}) +} + +type impl struct{} + +func (impl) String() string { + return "avif" +} + +func (impl) Extensions() []string { + return []string{"avif"} +} + +func (impl) Flags(flags []cli.Flag) []cli.Flag { + return append(flags, + &cli.IntFlag{ + Name: "avif.quality-alpha", + Usage: "AVIF: alpha channel quality in range [0-100]", + Value: 60, + Destination: &qualityA, + Validator: func(v int) error { + if v < 0 || v > 100 { + return fmt.Errorf("invalid avif.quality-alpha: %d", v) + } + return nil + }, + }, + &cli.IntFlag{ + Name: "avif.speed", + Usage: "AVIF: encoding speed in range [0-10] (0=slowest/best)", + Value: 6, + Destination: &speed, + Validator: func(v int) error { + if v < 0 || v > 10 { + return fmt.Errorf("invalid avif.speed: %d", v) + } + return nil + }, + }, + &cli.IntFlag{ + Name: "avif.chroma", + Usage: "AVIF: chroma subsampling (444=best, 422, 420=smallest)", + Value: 444, + Destination: &chroma, + Validator: func(v int) error { + if v != 444 && v != 422 && v != 420 { + return fmt.Errorf("invalid avif.chroma: %d", v) + } + return nil + }, + }, + ) +} + +func (impl) Sniff(reader io.ReaderAt) (int, []byte, error) { + buf := make([]byte, 12) + + if _, err := reader.ReadAt(buf, 0); err != nil { + return 0, nil, err + } + + if bytes.Equal(buf[4:12], []byte("ftypavif")) { + return 100, buf[:12], nil + } + + return 0, nil, nil +} + +func (impl) Decode(reader io.Reader) (image.Image, error) { + return avif.Decode(reader) +} + +func (impl) Encode(writer io.Writer, img image.Image, options opts.Common) error { + logx.Printf("avif: quality=%d, quality-alpha=%d, speed=%d, chroma=%d\n", options.Quality, qualityA, speed, chroma) + + return avif.Encode(writer, img, avif.Options{ + Quality: options.Quality, + QualityAlpha: qualityA, + Speed: speed, + ChromaSubsampling: chromaSubsampling(chroma), + }) +} + +func chromaSubsampling(c int) image.YCbCrSubsampleRatio { + switch c { + case 444: + return image.YCbCrSubsampleRatio444 + case 422: + return image.YCbCrSubsampleRatio422 + case 420: + return image.YCbCrSubsampleRatio420 + default: + return image.YCbCrSubsampleRatio444 + } +} diff --git a/internal/codec/detect.go b/internal/codec/detect.go index 217f010..9b0402e 100644 --- a/internal/codec/detect.go +++ b/internal/codec/detect.go @@ -61,7 +61,7 @@ func Sniff(reader io.Reader) (*Sniffed, io.Reader, error) { } if guess == nil { - return nil, nil, errors.New("unknown format") + return nil, nil, errors.New("unknown input format") } return &Sniffed{ diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..3a9ddca --- /dev/null +++ b/run.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +echo "Rebuilding..." +go build -tags full -o ffwebp ./cmd/ffwebp + +chmod +x ffwebp + +./ffwebp $*