nativewebp

package module
v0.9.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 11, 2025 License: MIT Imports: 11 Imported by: 20

README

Codecov Coverage Go Reference

Native WebP for Go

This is a native WebP encoder written entirely in Go, with no dependencies on libwebp or other external libraries. Designed for performance and efficiency, this encoder generates smaller files than the standard Go PNG encoder and is approximately 50% faster in execution.

Currently, the encoder supports only WebP lossless images (VP8L).

Benchmark

We conducted a quick benchmark to showcase file size reduction and encoding performance. Using an image from Google’s WebP Lossless and Alpha Gallery, we compared the results of our nativewebp encoder with the standard PNG decoder.

PNG encoder nativeWebP encoder reduction

file size 121kb 105kb 13% smaller
encoding time 14170403 ns/op 5389776 ns/op 62% faster

file size 48kb 38kb 21% smaller
encoding time 10662832 ns/op 3760902 ns/op 65% faster

file size 238 215 10% smaller
encoding time 30952147 ns/op 16371708 ns/op 47% faster

file size 53kb 43kb 19% smaller
encoding time 4511737 ns/op 2181801 ns/op 52% faster

file size 140kb 137kb 2% smaller
encoding time 11045284 ns/op 4850678 ns/op 56% faster

image source: https://developers.google.com/speed/webp/gallery2

Installation

To install the nativewebp package, use the following command:

go get github.com/HugoSmits86/nativewebp

Usage

Here’s a simple example of how to encode an image:

file, err := os.Create(name)
if err != nil {
  log.Fatalf("Error creating file %s: %v", name, err)
}
defer file.Close()

err = nativewebp.Encode(file, img, nil)
if err != nil {
  log.Fatalf("Error encoding image to WebP: %v", err)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Encode

func Encode(w io.Writer, img image.Image, o *Options) error

Encode writes the provided image.Image to the specified io.Writer in WebP VP8L format.

This function supports VP8L (lossless WebP) encoding and can handle color-indexed images when img is provided as image.Paletted.

Parameters:

w   - The destination writer where the encoded WebP image will be written.
img - The input image to be encoded.
o   - Pointer to Options containing encoding settings; currently unused but reserved
      for future enhancements such as adjusting compression levels.

Returns:

An error if encoding fails or writing to the io.Writer encounters an issue.

Types

type Options added in v0.9.3

type Options struct {
}

Options holds future configuration settings (e.g., compression levels)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL