From 6c75477d4267b449652c0f05602824b9a105caf0 Mon Sep 17 00:00:00 2001 From: Laura Date: Mon, 11 Aug 2025 02:55:07 +0200 Subject: [PATCH] jpegxl --- go.mod | 1 + go.sum | 2 + internal/builtins/jpegxl.go | 8 +++ internal/codec/jpegxl/jpegxl.go | 91 +++++++++++++++++++++++++++++++++ 4 files changed, 102 insertions(+) create mode 100644 internal/builtins/jpegxl.go create mode 100644 internal/codec/jpegxl/jpegxl.go diff --git a/go.mod b/go.mod index ade2632..c6d878b 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require github.com/urfave/cli/v3 v3.3.8 require ( github.com/gen2brain/avif v0.4.4 + github.com/gen2brain/jpegxl v0.4.5 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 e182d1b..32f245f 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,8 @@ github.com/ebitengine/purego v0.8.3 h1:K+0AjQp63JEZTEMZiwsI9g0+hAMNohwUOtY0RPGex 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/jpegxl v0.4.5 h1:TWpVEn5xkIfsswzkjHBArd0Cc9AE0tbjBSoa0jDsrbo= +github.com/gen2brain/jpegxl v0.4.5/go.mod h1:4kWYJ18xCEuO2vzocYdGpeqNJ990/Gjy3uLMg5TBN6I= 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/jpegxl.go b/internal/builtins/jpegxl.go new file mode 100644 index 0000000..feacdab --- /dev/null +++ b/internal/builtins/jpegxl.go @@ -0,0 +1,8 @@ +//go:build jpegxl || full +// +build jpegxl full + +package builtins + +import ( + _ "github.com/coalaura/ffwebp/internal/codec/jpegxl" +) diff --git a/internal/codec/jpegxl/jpegxl.go b/internal/codec/jpegxl/jpegxl.go new file mode 100644 index 0000000..911a311 --- /dev/null +++ b/internal/codec/jpegxl/jpegxl.go @@ -0,0 +1,91 @@ +package jpegxl + +import ( + "bytes" + "fmt" + "image" + "io" + + jxl "github.com/gen2brain/jpegxl" + + "github.com/coalaura/ffwebp/internal/codec" + "github.com/coalaura/ffwebp/internal/logx" + "github.com/coalaura/ffwebp/internal/opts" + "github.com/urfave/cli/v3" +) + +var ( + effort int +) + +func init() { + codec.Register(impl{}) +} + +type impl struct{} + +func (impl) String() string { + return "jpegxl" +} + +func (impl) Extensions() []string { + return []string{"jxl"} +} + +func (impl) Flags(flags []cli.Flag) []cli.Flag { + return append(flags, + &cli.IntFlag{ + Name: "jpegxl.effort", + Usage: "JPEG XL: encode effort (1=fast .. 10=slow). Default 7", + Value: 7, + Destination: &effort, + Validator: func(value int) error { + if value < 1 || value > 10 { + return fmt.Errorf("invalid jpegxl.effort: %d", value) + } + return nil + }, + }, + ) +} + +func (impl) Sniff(reader io.ReaderAt) (int, []byte, error) { + containerMagic := []byte{ + 0x00, 0x00, 0x00, 0x0C, + 'j', 'x', 'l', ' ', + 0x0D, 0x0A, 0x87, 0x0A, + } + + buf := make([]byte, 12) + + if _, err := reader.ReadAt(buf, 0); err != nil { + if err == io.EOF { + return 0, nil, nil + } + + return 0, nil, err + } + + if bytes.Equal(buf, containerMagic) { + return 100, containerMagic, nil + } + + if buf[0] == 0xFF && buf[1] == 0x0A { + return 100, buf[:2], nil + } + + return 0, nil, nil +} + +func (impl) Decode(reader io.Reader) (image.Image, error) { + return jxl.Decode(reader) +} + +func (impl) Encode(writer io.Writer, img image.Image, options opts.Common) error { + logx.Printf("jpegxl: quality=%d effort=%d\n", options.Quality, effort) + + return jxl.Encode(writer, img, jxl.Options{ + Quality: options.Quality, + Effort: effort, + }) +}