nativewebp

package module
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2024 License: MIT Imports: 9 Imported by: 20

README

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 40% 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. The tests were performed on a 2023 Mac mini equipped with an Apple M2 chip and 16GB of RAM.


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

PNG encoder nativeWebP encoder reduction
file size 54kb 50kb 7% smaller
encoding time 1950541 ns/op 5651958 ns/op 65% faster

Installation

To install the nativewebp package, use the following command:

go get github.com/yourusername/native-webp-encoder

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)
if err != nil {
  log.Fatalf("Error encoding image to WebP: %v", err)
}

Documentation

Index

Constants

View Source
const (
	TransformPredict  = Transform(0)
	TransformSubGreen = Transform(2)
)

Variables

This section is empty.

Functions

func ApplyPredictTransform

func ApplyPredictTransform(pixels []color.NRGBA, width, height int) (int, []color.NRGBA)

func ApplySubtractGreenTransform

func ApplySubtractGreenTransform(pixels []color.NRGBA)

func Encode

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

func WriteBitStreamData

func WriteBitStreamData(w *BitWriter, img image.Image, colorCacheBits int, transforms [4]bool) error

func WriteImageData

func WriteImageData(w *BitWriter, pixels []color.NRGBA, isRecursive bool, colorCacheBits int)

Types

type BitWriter

type BitWriter struct {
	Buffer        *bytes.Buffer
	BitBuffer     uint64
	BitBufferSize int
}

func (*BitWriter) AlignByte

func (w *BitWriter) AlignByte()

func (*BitWriter) WriteBits

func (w *BitWriter) WriteBits(value uint64, n int)

func (*BitWriter) WriteCode

func (w *BitWriter) WriteCode(code HuffmanCode)

type HuffmanCode

type HuffmanCode struct {
	Symbol int
	Bits   int
	Depth  int
}

type Node

type Node struct {
	IsBranch    bool
	Weight      int
	Symbol      int
	BranchLeft  *Node
	BranchRight *Node
}

type NodeHeap

type NodeHeap []*Node

func (NodeHeap) Len

func (h NodeHeap) Len() int

func (NodeHeap) Less

func (h NodeHeap) Less(i, j int) bool

func (*NodeHeap) Pop

func (h *NodeHeap) Pop() interface{}

func (*NodeHeap) Push

func (h *NodeHeap) Push(x interface{})

func (NodeHeap) Swap

func (h NodeHeap) Swap(i, j int)

type Transform

type Transform int

Jump to

Keyboard shortcuts

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