webp

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2025 License: MIT Imports: 19 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

webp

Status Go Reference

Go encoder/decoder for WebP Image File Format with support for animated WebP images (decode only).

Based on libwebp compiled to WASM and used with wazero runtime (CGo-free).

The library will first try to use a dynamic/shared library (if installed) via purego and will fall back to WASM.

Build tags

  • nodynamic - do not use dynamic/shared library (use only WASM)

Benchmark

goos: linux
goarch: amd64
pkg: github.com/gen2brain/webp
cpu: 11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz

BenchmarkDecodeStd-8                             157	   7639585 ns/op	  473683 B/op	      13 allocs/op
BenchmarkDecodeWasm-8                            156	   7799653 ns/op	 2614793 B/op	     316 allocs/op
BenchmarkDecodeDynamic-8                         344	   3497863 ns/op	  943356 B/op	      58 allocs/op
BenchmarkDecodeTranspiled-8 (1)                  138	   8562133 ns/op	 1335622 B/op	      52 allocs/op
BenchmarkDecodeCGo1-8 (2)                        300	   3897300 ns/op	 1333630 B/op	      21 allocs/op
BenchmarkDecodeCGo2-8 (3)                        314	   3801195 ns/op	 1334020 B/op	      22 allocs/op

BenchmarkEncodeWasm-8                             12	  96123599 ns/op	 4857356 B/op	     298 allocs/op
BenchmarkEncodeDynamic-8                          55	  19022243 ns/op	   19888 B/op	      42 allocs/op
BenchmarkEncodeTranspiled-8 (1)                   18	  60042805 ns/op	   76104 B/op	      36 allocs/op
BenchmarkEncodeCGo1-8 (2)                         31	  32538122 ns/op	 3213497 B/op	  524294 allocs/op
BenchmarkEncodeCGo2-8 (3)                         52	  22482704 ns/op	   26043 B/op	       5 allocs/op

Documentation

Overview

Package webp implements an WEBP image decoder based on libwebp compiled to WASM.

Index

Constants

View Source
const DefaultMethod = 4

DefaultMethod is the default method encoding parameter.

View Source
const DefaultQuality = 75

DefaultQuality is the default quality encoding parameter.

Variables

View Source
var (
	ErrMemRead  = errors.New("webp: mem read failed")
	ErrMemWrite = errors.New("webp: mem write failed")
	ErrDecode   = errors.New("webp: decode failed")
	ErrEncode   = errors.New("webp: encode failed")
)

Errors .

Functions

func Decode

func Decode(r io.Reader) (image.Image, error)

Decode reads a WEBP image from r and returns it as an image.Image.

func DecodeConfig

func DecodeConfig(r io.Reader) (image.Config, error)

DecodeConfig returns the color model and dimensions of a WEBP image without decoding the entire image.

func Dynamic

func Dynamic() error

Dynamic returns error (if there was any) during opening dynamic/shared library.

func Encode

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

Encode writes the image m to w with the given options.

func Init

func Init()

Init initializes wazero runtime and compiles the module. This function does nothing if a dynamic/shared library is used and Dynamic() returns nil. There is no need to explicitly call this function, first Decode/Encode will initialize the runtime.

Types

type Options

type Options struct {
	// Quality in the range [0,100]. Quality of 100 implies Lossless. Default is 75.
	Quality int
	// Lossless indicates whether to use the lossless compression. Lossless will ignore quality.
	Lossless bool
	// Method is quality/speed trade-off (0=fast, 6=slower-better). Default is 4.
	Method int
	// Exact preserve the exact RGB values in transparent area.
	Exact bool
}

Options are the encoding parameters.

type WEBP

type WEBP struct {
	// Decoded images.
	Image []image.Image
	// Delay times, one per frame, in milliseconds.
	Delay []int
}

WEBP represents the possibly multiple images stored in a WEBP file.

func DecodeAll

func DecodeAll(r io.Reader) (*WEBP, error)

DecodeAll reads a WEBP image from r and returns the sequential frames and timing information.

Jump to

Keyboard shortcuts

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