mirror of
https://github.com/coalaura/ffwebp.git
synced 2025-09-07 21:45:31 +00:00
jpegxl
This commit is contained in:
1
go.mod
1
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
|
||||
|
2
go.sum
2
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=
|
||||
|
8
internal/builtins/jpegxl.go
Normal file
8
internal/builtins/jpegxl.go
Normal file
@@ -0,0 +1,8 @@
|
||||
//go:build jpegxl || full
|
||||
// +build jpegxl full
|
||||
|
||||
package builtins
|
||||
|
||||
import (
|
||||
_ "github.com/coalaura/ffwebp/internal/codec/jpegxl"
|
||||
)
|
91
internal/codec/jpegxl/jpegxl.go
Normal file
91
internal/codec/jpegxl/jpegxl.go
Normal file
@@ -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,
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user