Add jpegli

This commit is contained in:
Milan Nikolic
2025-06-13 18:42:00 +02:00
parent 84135f3304
commit 711ad2f102
9 changed files with 83 additions and 5 deletions

View File

@@ -23,6 +23,7 @@ You can also use apps like `ffplay` or `vlc`:
* `opencv` - use `OpenCV` library to access camera ([gocv](https://github.com/hybridgroup/gocv))
* `libjpeg` - build with `libjpeg` ([go-libjpeg](https://github.com/pixiv/go-libjpeg)) instead of native Go `image/jpeg`
* `jpegli` - build with `jpegli` ([go-libjpeg](https://github.com/gen2brain/jpegli)) instead of native Go `image/jpeg`
### Download

6
go.mod
View File

@@ -4,6 +4,7 @@ require (
github.com/abbot/go-http-auth v0.4.0
github.com/anthonynsimon/bild v0.14.0
github.com/gen2brain/base64 v0.0.0-20221015184129-317a5c93030c
github.com/gen2brain/jpegli v0.3.4
github.com/jamiealquiza/envy v1.1.0
github.com/korandiz/v4l v1.1.0
github.com/pbnjay/pixfont v0.0.0-20200714042608-33b744692567
@@ -16,9 +17,12 @@ require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/cobra v1.8.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tetratelabs/wazero v1.9.0 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sys v0.16.0 // indirect
)
go 1.21
go 1.23
toolchain go1.24.3

4
go.sum
View File

@@ -5,6 +5,8 @@ github.com/anthonynsimon/bild v0.14.0/go.mod h1:hcvEAyBjTW69qkKJTfpcDQ83sSZHxwOu
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/gen2brain/base64 v0.0.0-20221015184129-317a5c93030c h1:TUjjeJ2rV4KZxH6hIEi/boEQB3v6aKvwdakUJR3AwiE=
github.com/gen2brain/base64 v0.0.0-20221015184129-317a5c93030c/go.mod h1:VG58IUyxPWojCtGwqwoZ/6LLXwClu1tssqa5ktOxI9o=
github.com/gen2brain/jpegli v0.3.4 h1:wFoUHIjfPJGGeuW3r9dqy0MTT1TtvJuWf6EqfHPPGFM=
github.com/gen2brain/jpegli v0.3.4/go.mod h1:tVnF7NPyufTo8noFlW5lurUUwZW8trwBENOItzuk2BM=
github.com/hybridgroup/mjpeg v0.0.0-20140228234708-4680f319790e/go.mod h1:eagM805MRKrioHYuU7iKLUyFPVKqVV6um5DAvCkUtXs=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
@@ -22,6 +24,8 @@ github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/tetratelabs/wazero v1.9.0 h1:IcZ56OuxrtaEz8UYNRHBrUa9bYeX9oVY93KspZZBf/I=
github.com/tetratelabs/wazero v1.9.0/go.mod h1:TSbcXCfFP0L2FGkRPxHphadXPjo1T6W+CseNNY7EkjM=
gocv.io/x/gocv v0.35.0 h1:Qaxb5KdVyy8Spl4S4K0SMZ6CVmKtbfoSGQAxRD3FZlw=
gocv.io/x/gocv v0.35.0/go.mod h1:oc6FvfYqfBp99p+yOEzs9tbYF9gOrAQSeL/dyIPefJU=
golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc=

View File

@@ -1,4 +1,4 @@
//go:build !libjpeg
//go:build !libjpeg && !jpegli
// Package image.
package image

31
image/decode_jpegli.go Normal file
View File

@@ -0,0 +1,31 @@
//go:build jpegli
// Package image.
package image
import (
"image"
"io"
"github.com/gen2brain/jpegli"
)
// NewDecoder returns a new Decoder.
func NewDecoder(r io.Reader) *Decoder {
return &Decoder{r}
}
// Decoder struct.
type Decoder struct {
r io.Reader
}
// Decode decodes image from JPEG.
func (d Decoder) Decode() (image.Image, error) {
return jpegli.DecodeWithOptions(d.r, &jpegli.DecodingOptions{
DCTMethod: jpegli.DCTIFast,
FancyUpsampling: false,
BlockSmoothing: false,
ArithCode: true,
})
}

View File

@@ -23,7 +23,7 @@ type Decoder struct {
// Decode decodes image from JPEG.
func (d Decoder) Decode() (image.Image, error) {
return jpeg.Decode(d.r, &jpeg.DecoderOptions{
DCTMethod: jpeg.DCTFloat,
DCTMethod: jpeg.DCTIFast,
DisableFancyUpsampling: true,
DisableBlockSmoothing: true,
})

View File

@@ -1,4 +1,4 @@
//go:build !libjpeg
//go:build !libjpeg && !jpegli
// Package image.
package image

35
image/encode_jpegli.go Normal file
View File

@@ -0,0 +1,35 @@
//go:build jpegli
// Package image.
package image
import (
"image"
"io"
"github.com/gen2brain/jpegli"
)
// NewEncoder returns a new Encoder.
func NewEncoder(w io.Writer) *Encoder {
return &Encoder{w}
}
// Encoder struct.
type Encoder struct {
w io.Writer
}
// Encode encodes image to JPEG.
func (e Encoder) Encode(img image.Image) error {
return jpegli.Encode(e.w, img, &jpegli.EncodingOptions{
Quality: 75,
ProgressiveLevel: 0,
ChromaSubsampling: image.YCbCrSubsampleRatio420,
DCTMethod: jpegli.DCTIFast,
OptimizeCoding: false,
AdaptiveQuantization: false,
StandardQuantTables: false,
FancyDownsampling: false,
})
}

View File

@@ -23,6 +23,9 @@ type Encoder struct {
// Encode encodes image to JPEG.
func (e Encoder) Encode(img image.Image) error {
return jpeg.Encode(e.w, img, &jpeg.EncoderOptions{
Quality: 75,
Quality: 75,
DCTMethod: jpeg.DCTIFast,
ProgressiveMode: false,
OptimizeCoding: false,
})
}