gowebp

Lossless WebP (VP8L) encoder in pure Go with no external runtime dependencies.
Status
- Encoder supports lossless VP8L only (no animation).
- Transform pipeline includes predictor, color transform, subtract green, and color cache.
- Near-lossless quantization is implemented for RGB (alpha left untouched).
- Optional meta Huffman groups are supported via
HuffmanGroupBits.
Current Limitations
- Transform image selection is fixed (no adaptive block analysis).
- LZ77 and cache heuristics are basic; compression is functional but not tuned.
- Huffman groups use per-tile histograms with simple deduplication (no rate optimization).
- Color cache is supported only in the main image stream (not transform images).
- No streaming API; encoder builds full bitstream in memory.
Usage
go get github.com/lEx0/gowebp
opts := webp.AutoOptions(img)
if err := webp.Encode(&buf, img, &opts); err != nil {
// handle error
}
if err := webp.Encode(&buf, img, &webp.Options{
NearLossless: 50,
ColorCacheBits: 4,
}); err != nil {
// handle error
}
LZ77 Options
LZ77Mode selects the match-finding strategy:
LZ77ModeAuto (default): current fast hashed matcher.
LZ77ModeNaive: simple O(n*window) matcher (lower memory, slower).
LZ77ModeHash: hashed ring buffer (faster, higher memory).
LZ77HashBits controls hash table size ([8..16]), default 11.
LZ77Chain controls candidates per bucket (power of two, [1..8]), default 2.
Validation
- Integration tests compare pixel output via
golang.org/x/image/webp and dwebp when available.
Benchmarks
goos: darwin
goarch: arm64
pkg: github.com/lEx0/gowebp/webp
cpu: Apple M1 Pro
BenchmarkEncodeSolid128
BenchmarkEncodeSolid128-8 6476 161514 ns/op 793248 B/op 99 allocs/op
BenchmarkEncodeSolid128WithCache
BenchmarkEncodeSolid128WithCache-8 6489 173089 ns/op 793983 B/op 95 allocs/op
BenchmarkEncodeGradient256
BenchmarkEncodeGradient256-8 10000 106664 ns/op 129658 B/op 1011 allocs/op
BenchmarkEncodeNoise64
BenchmarkEncodeNoise64-8 20358 58251 ns/op 231128 B/op 269 allocs/op
BenchmarkEncodeAlpha64
BenchmarkEncodeAlpha64-8 9765 113484 ns/op 251600 B/op 640 allocs/op
BenchmarkEncodeSolid512
BenchmarkEncodeSolid512-8 704 1655700 ns/op 12117409 B/op 97 allocs/op
Near-Lossless
NearLossless is a coarse quantization level in the range [0..100] (0 disables).
- Current step mapping is intentionally simple and may change as tuning improves.
ColorCacheBits must be in [0..11] (0 disables).
HuffmanGroupBits must be 0 or in [2..9].
Roadmap
- Rate-optimized meta Huffman groups.
- Adaptive transform images (predictor/color transform tuning).
- Streaming encoder API.