From 58981eb716d1cec8b600862b01d3443f6d041ea2 Mon Sep 17 00:00:00 2001 From: mibar-me Date: Tue, 30 Jun 2026 13:49:02 +0200 Subject: [PATCH 1/3] Add basic unanimated webp encoding and decoding support with HugoSmits86/nativewebp library --- go.mod | 1 + go.sum | 2 ++ imgio/io.go | 9 +++++++++ imgio/io_test.go | 13 +++++++++++++ 4 files changed, 25 insertions(+) diff --git a/go.mod b/go.mod index db7979b..1dbaa52 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/anthonynsimon/bild go 1.26 require ( + github.com/HugoSmits86/nativewebp v1.3.0 github.com/spf13/cobra v1.10.2 golang.org/x/image v0.38.0 ) diff --git a/go.sum b/go.sum index e53aeff..a6a0ffc 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/HugoSmits86/nativewebp v1.3.0 h1:n1egtEzSV4KwFtealr7dzdYq1wI/uj/bOQ/QcTcIyVE= +github.com/HugoSmits86/nativewebp v1.3.0/go.mod h1:YNQuWenlVmSUUASVNhTDwf4d7FwYQGbGhklC8p72Vr8= github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= diff --git a/imgio/io.go b/imgio/io.go index 0badb3e..96be876 100644 --- a/imgio/io.go +++ b/imgio/io.go @@ -8,6 +8,7 @@ import ( "io" "os" + "github.com/HugoSmits86/nativewebp" "golang.org/x/image/bmp" ) @@ -57,6 +58,14 @@ func BMPEncoder() Encoder { } } +// WEBPEncoder returns an encoder to WEBP +func WEBPEncoder(o *nativewebp.Options) Encoder { + return func(w io.Writer, img image.Image) error { + return nativewebp.Encode(w, img, o) + } +} + + // Save creates a file and writes to it an image using the provided encoder. // // Usage example: diff --git a/imgio/io_test.go b/imgio/io_test.go index 3986dc3..97e9941 100644 --- a/imgio/io_test.go +++ b/imgio/io_test.go @@ -52,6 +52,19 @@ func TestEncode(t *testing.T) { }, }, }, + { + format: "webp", + encoder: WEBPEncoder(nil), + value: &image.RGBA{ + Rect: image.Rect(0, 0, 3, 3), + Stride: 3 * 4, + Pix: []uint8{ + 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF, + 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0x80, 0x00, 0x00, 0xFF, + 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF, + }, + }, + }, } for _, c := range cases { From 16aa19899cd30870ab846c3833a9ed71aea8df46 Mon Sep 17 00:00:00 2001 From: mibar-me Date: Tue, 30 Jun 2026 17:55:34 +0200 Subject: [PATCH 2/3] apply fmt --- imgio/io.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/imgio/io.go b/imgio/io.go index 96be876..8e92a2e 100644 --- a/imgio/io.go +++ b/imgio/io.go @@ -61,11 +61,10 @@ func BMPEncoder() Encoder { // WEBPEncoder returns an encoder to WEBP func WEBPEncoder(o *nativewebp.Options) Encoder { return func(w io.Writer, img image.Image) error { - return nativewebp.Encode(w, img, o) + return nativewebp.Encode(w, img, o) } } - // Save creates a file and writes to it an image using the provided encoder. // // Usage example: From 3e0e3c667abe881226267775295f9c24d63c8e76 Mon Sep 17 00:00:00 2001 From: mibar-me Date: Tue, 30 Jun 2026 18:00:56 +0200 Subject: [PATCH 3/3] add webp extension support to helpers.go --- cmd/helpers.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/cmd/helpers.go b/cmd/helpers.go index 0f131ac..9c856f1 100644 --- a/cmd/helpers.go +++ b/cmd/helpers.go @@ -15,6 +15,7 @@ import ( var jpgExtensions = []string{".jpg", ".jpeg"} var pngExtensions = []string{".png"} var bmpExtensions = []string{".bmp"} +var webpExtensions = []string{".webp"} var ( // ErrWrongSize is thrown when the provided size string does not match the expected form. @@ -51,6 +52,12 @@ func resolveEncoder(outputfile string, defaultEncoding imgio.Encoder) imgio.Enco } } + for _, ext := range webpExtensions { + if strings.HasSuffix(lower, ext) { + return imgio.WEBPEncoder(nil) + } + } + return defaultEncoding }