webp

package module
v1.6.4 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2026 License: BSD-3-Clause Imports: 13 Imported by: 0

README

webp

██╗    ██╗███████╗██████╗ ██████╗
██║    ██║██╔════╝██╔══██╗██╔══██╗
██║ █╗ ██║█████╗  ██████╔╝██████╔╝
██║███╗██║██╔══╝  ██╔══██╗██╔═══╝
╚███╔███╔╝███████╗██████╔╝██║
 ╚══╝╚══╝ ╚══════╝╚═════╝ ╚═╝

Build Status GoDoc GitHub release license

Why this fork

This fork adds a few practical improvements over the original repository:

  • libwebp 1.6.0 instead of 1.4.0
  • Animated WebP support (DecodeAll/EncodeAll)
  • Full WebP config parsing (has_alpha, has_animation and format via DecodeConfigEx)
  • More exposed WebP encoding options (method, auto_filter, etc.)

Benchmark

Install

Install Zig first (recommended, cross-platform, export CC="zig cc" && export CXX="zig c++"), and then run these commands:

  1. go get github.com/coalaura/webp
  2. go run hello.go

Example

This is a simple example:

package main

import (
	"bytes"
	"fmt"
	"log"
	"os"

	"github.com/coalaura/webp"
)

func main() {
	var buf bytes.Buffer
	var width, height int
	var data []byte
	var err error

	// Load file data
	if data, err = os.ReadFile("./testdata/1_webp_ll.webp"); err != nil {
		log.Println(err)
	}

	// GetInfo
	if width, height, _, _, _, err = webp.GetInfo(data); err != nil {
		log.Println(err)
	}
	fmt.Printf("width = %d, height = %d\n", width, height)

	// GetMetadata
	if metadata, err := webp.GetMetadata(data, "ICCP"); err != nil {
		fmt.Printf("Metadata: err = %v\n", err)
	} else {
		fmt.Printf("Metadata: %s\n", string(metadata))
	}

	// Decode webp
	m, err := webp.Decode(bytes.NewReader(data), nil)
	if err != nil {
		log.Println(err)
	}

	// Encode lossless webp
	if err = webp.Encode(&buf, m, &webp.Options{Lossless: true}); err != nil {
		log.Println(err)
	}
	if err = os.WriteFile("output.webp", buf.Bytes(), 0666); err != nil {
		log.Println(err)
	}
    
    fmt.Println("Save output.webp ok")
}

Decode and Encode as RGB format:

m, err := webp.DecodeRGB(data, nil)
if err != nil {
	log.Fatal(err)
}

data, err := webp.EncodeRGB(m)
if err != nil {
	log.Fatal(err)
}

Documentation

Overview

Package webp implements a decoder and encoder for WEBP images.

WEBP is defined at: https://developers.google.com/speed/webp/docs/riff_container

Install

Install `GCC` or `MinGW` (http://tdm-gcc.tdragon.net/download) at first, and then run these commands:

  1. `go get github.com/coalaura/webp`
  2. `go run hello.go`

Examples

This is a simple example:

package main

import (
	"bytes"
	"fmt"
	"os"
	"log"

	"github.com/coalaura/webp"
)

func main() {
	var buf bytes.Buffer
	var width, height int
	var data []byte
	var err error

	// Load file data
	if data, err = os.ReadFile("./testdata/1_webp_ll.webp"); err != nil {
		log.Fatal(err)
	}

	// GetInfo
	if width, height, _, err = webp.GetInfo(data); err != nil {
		log.Fatal(err)
	}
	fmt.Printf("width = %d, height = %d\n", width, height)

	// GetMetadata
	if metadata, err := webp.GetMetadata(data, "ICCP"); err != nil {
		fmt.Printf("Metadata: err = %v\n", err)
	} else {
		fmt.Printf("Metadata: %s\n", string(metadata))
	}

	// Decode webp
	m, err := webp.Decode(bytes.NewReader(data), nil)
	if err != nil {
		log.Fatal(err)
	}

	// Encode lossless webp
	if err = webp.Encode(&buf, m, &webp.Options{Lossless: true}); err != nil {
		log.Fatal(err)
	}
	if err = os.WriteFile("output.webp", buf.Bytes(), 0666); err != nil {
		log.Fatal(err)
	}
	fmt.Printf("Save output.webp ok\n")
}

Decode and Encode as RGB format:

m, err := webp.DecodeRGB(data, nil)
if err != nil {
	log.Fatal(err)
}

data, err := webp.EncodeRGB(m, 90.0, 0)
if err != nil {
	log.Fatal(err)
}

BUGS

Report bugs to <[email protected]>.

Thanks!

Index

Examples

Constants

View Source
const DefaultAlphaQuality = 100

DefaultAlphaQuality is the default alpha plane quality used by Encode.

View Source
const DefaultMethod = 4

DefaultMethod is the default encoding method used by Encode.

View Source
const DefaultQuality = 90

DefaultQuality is the default lossy quality used by Encode.

View Source
const (
	MemPMagic = "MemP" // See https://github.com/chai2010/image
)

MemPMagic is the magic string used by MemP images.

View Source
const (
	WEBP_DECODER_ABI_VERSION = 0x0210 // MAJOR(8b) + MINOR(8b)
)

WEBP_DECODER_ABI_VERSION is the libwebp decoder ABI version.

Variables

This section is empty.

Functions

func C_webpFree

func C_webpFree(p unsafe.Pointer)

func C_webpMalloc

func C_webpMalloc(size C_size_t) unsafe.Pointer

func ChannelsOf

func ChannelsOf(m image.Image) int

ChannelsOf returns the number of color channels in m.

func ColorModel

func ColorModel(channels int, dataType reflect.Kind) color.Model

ColorModel returns a color.Model for the given channels and data type.

Example
rgba := color.RGBA{R: 101, G: 102, B: 103, A: 104}
c := ColorModel(4, reflect.Uint8).Convert(rgba).(MemPColor)
fmt.Printf("c = %v\n", c)
Output:

c = {4 uint8 [101 102 103 104]}

func Decode

func Decode(r io.Reader, opt *DecodeOptions) (m image.Image, err error)

Decode reads a WEBP image from r and returns it as an image.Image. If opt is nil, defaults are used.

func DecodeConfig

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

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

func DecodeGray

func DecodeGray(data []byte, opt *DecodeOptions) (m *image.Gray, err error)

DecodeGray decodes a WebP payload into a Gray image. If opt is nil, defaults are used.

func DecodeGrayToSize

func DecodeGrayToSize(data []byte, width, height int, opt *DecodeOptions) (m *image.Gray, err error)

DecodeGrayToSize decodes a Gray image scaled to the given dimensions. For large images, the DecodeXXXToSize methods are significantly faster and require less memory compared to decoding a full-size image and then resizing it. If opt is nil, defaults are used.

func DecodeRGBA

func DecodeRGBA(data []byte, opt *DecodeOptions) (m *image.RGBA, err error)

DecodeRGBA decodes a WebP payload into an RGBA image. If opt is nil, defaults are used.

func DecodeRGBAToSize

func DecodeRGBAToSize(data []byte, width, height int, opt *DecodeOptions) (m *image.RGBA, err error)

DecodeRGBAToSize decodes an RGBA image scaled to the given dimensions. If opt is nil, defaults are used.

func DepthOf

func DepthOf(m image.Image) int

DepthOf returns the per-channel bit depth of m.

func Encode

func Encode(w io.Writer, m image.Image, opt *Options) (err error)

Encode writes the image m to w in WEBP format.

Example
m, err := Load("./testdata/1_webp_ll.webp")
if err != nil {
	log.Fatal(err)
}

var buf bytes.Buffer
if err := Encode(&buf, m, nil); err != nil {
	log.Fatal(err)
}
_ = buf.Bytes()
Example (Lossless)
m, err := Load("./testdata/1_webp_ll.webp")
if err != nil {
	log.Fatal(err)
}

var buf bytes.Buffer
if err := Encode(&buf, m, &Options{Lossless: true}); err != nil {
	log.Fatal(err)
}
_ = buf.Bytes()
Example (Rgb)
rgb := NewRGBImage(image.Rect(0, 0, 400, 300))

var buf bytes.Buffer
if err := Encode(&buf, rgb, nil); err != nil {
	log.Fatal(err)
}
_ = buf.Bytes()
Example (Rgb48MemP)
rgb48 := NewMemPImage(image.Rect(0, 0, 400, 300), 3, reflect.Uint16)

var buf bytes.Buffer
if err := Encode(&buf, rgb48, nil); err != nil {
	log.Fatal(err)
}
_ = buf.Bytes()

func EncodeAll added in v1.6.2

func EncodeAll(w io.Writer, anim *Animation, opt *Options) error

EncodeAll writes all frames in anim to w as an animated WEBP. Delay values are in milliseconds. If opt is nil, defaults are used.

func EncodeExactLosslessRGBA

func EncodeExactLosslessRGBA(m image.Image, method int) (data []byte, err error)

EncodeExactLosslessRGBA encodes lossless RGBA WebP and preserves RGB values in transparent areas.

func EncodeGray

func EncodeGray(m image.Image, quality float32, method int) (data []byte, err error)

EncodeGray encodes an image as lossy grayscale WebP.

func EncodeLosslessGray

func EncodeLosslessGray(m image.Image, method int) (data []byte, err error)

EncodeLosslessGray encodes an image as lossless grayscale WebP.

func EncodeLosslessRGB

func EncodeLosslessRGB(m image.Image, method int) (data []byte, err error)

EncodeLosslessRGB encodes an image as lossless RGB WebP.

func EncodeLosslessRGBA

func EncodeLosslessRGBA(m image.Image, method int) (data []byte, err error)

EncodeLosslessRGBA encodes an image as lossless RGBA WebP.

func EncodeRGB

func EncodeRGB(m image.Image, quality float32, method int) (data []byte, err error)

EncodeRGB encodes an image as lossy RGB WebP.

func EncodeRGBA

func EncodeRGBA(m image.Image, quality float32, method int) (data []byte, err error)

EncodeRGBA encodes an image as lossy RGBA WebP.

func GetInfo

func GetInfo(data []byte) (width, height int, hasAlpha bool, hasAnimation bool, format int, err error)

GetInfo returns image dimensions and WebP bitstream flags.

Example
data := xLoadData("1_webp_a.webp")

width, height, hasAlpha, hasAnimation, format, err := GetInfo(data)
if err != nil {
	log.Fatal(err)
}

fmt.Printf("width: %v\n", width)
fmt.Printf("height: %v\n", height)
fmt.Printf("hasAlpha: %v\n", hasAlpha)
fmt.Printf("hasAnimation: %v\n", hasAnimation)
fmt.Printf("format: %v\n", format)
Output:

width: 400
height: 301
hasAlpha: true
hasAnimation: false
format: 0
Example (NoAlpha)
data := xLoadData("video-001.webp")

width, height, hasAlpha, hasAnimation, format, err := GetInfo(data)
if err != nil {
	log.Fatal(err)
}

fmt.Printf("width: %v\n", width)
fmt.Printf("height: %v\n", height)
fmt.Printf("hasAlpha: %v\n", hasAlpha)
fmt.Printf("hasAnimation: %v\n", hasAnimation)
fmt.Printf("format: %v\n", format)
Output:

width: 150
height: 103
hasAlpha: false
hasAnimation: false
format: 1

func GetMetadata

func GetMetadata(data []byte, format string) (metadata []byte, err error)

GetMetadata returns EXIF/ICCP/XMP metadata from a WebP payload.

func Load

func Load(name string) (m image.Image, err error)

Load reads a WebP file from disk and returns it as an RGBA image.

Example
m, err := Load("./testdata/1_webp_ll.webp")
if err != nil {
	log.Fatal(err)
}
fmt.Printf("Bounds = %v\n", m.Bounds())
Output:

Bounds = (0,0)-(400,301)

func LoadConfig

func LoadConfig(name string) (config image.Config, err error)

LoadConfig reads a file header and returns image.Config metadata.

Example
cfg, err := LoadConfig("./testdata/1_webp_ll.webp")
if err != nil {
	log.Fatal(err)
}
fmt.Printf("Width = %d\n", cfg.Width)
fmt.Printf("Height = %d\n", cfg.Height)
Output:

Width = 400
Height = 301

func Save

func Save(name string, m image.Image, opt *Options) (err error)

Save encodes m as WebP and writes it to the named file.

Example
tmpname := "z_test_ExampleSave.webp"
defer os.Remove(tmpname)

gray := NewMemPImage(image.Rect(0, 0, 400, 300), 1, reflect.Uint8)
if err := Save(tmpname, gray, &Options{Quality: 75}); err != nil {
	log.Fatal(err)
}

func SetMetadata

func SetMetadata(data, metadata []byte, format string) (newData []byte, err error)

SetMetadata writes EXIF/ICCP/XMP metadata into a WebP payload.

func SizeofImage

func SizeofImage(m image.Image) int

SizeofImage returns an approximate in-memory size for m.

func SizeofKind

func SizeofKind(dataType reflect.Kind) int

SizeofKind returns the size in bytes for the provided kind.

Example
fmt.Printf("%v = %v\n", reflect.Uint8, SizeofKind(reflect.Uint8))
fmt.Printf("%v = %v\n", reflect.Uint16, SizeofKind(reflect.Uint16))
fmt.Printf("%v = %v\n", reflect.Uint32, SizeofKind(reflect.Uint32))
fmt.Printf("%v = %v\n", reflect.Float32, SizeofKind(reflect.Float32))
fmt.Printf("%v = %v\n", reflect.Float64, SizeofKind(reflect.Float64))
Output:

uint8 = 1
uint16 = 2
uint32 = 4
float32 = 4
float64 = 8

func SizeofPixel

func SizeofPixel(channels int, dataType reflect.Kind) int

SizeofPixel returns the size in bytes for a pixel of the given shape.

Example
fmt.Printf("sizeof(gray) = %d\n", SizeofPixel(1, reflect.Uint8))
fmt.Printf("sizeof(gray16) = %d\n", SizeofPixel(1, reflect.Uint16))
fmt.Printf("sizeof(rgb) = %d\n", SizeofPixel(3, reflect.Uint8))
fmt.Printf("sizeof(rgb48) = %d\n", SizeofPixel(3, reflect.Uint16))
fmt.Printf("sizeof(rgba) = %d\n", SizeofPixel(4, reflect.Uint8))
fmt.Printf("sizeof(rgba64) = %d\n", SizeofPixel(4, reflect.Uint16))
fmt.Printf("sizeof(float32) = %d\n", SizeofPixel(1, reflect.Float32))
Output:

sizeof(gray) = 1
sizeof(gray16) = 2
sizeof(rgb) = 3
sizeof(rgb48) = 6
sizeof(rgba) = 4
sizeof(rgba64) = 8
sizeof(float32) = 4

func WebPGetDecoderVersion

func WebPGetDecoderVersion() uint

WebPGetDecoderVersion returns the decoder version packed in hex using 8 bits each for major/minor/revision.

func WebPGetInfo

func WebPGetInfo(data []byte) (width, height int, ok bool)

WebPGetInfo returns width and height from a WebP header and reports whether the header is valid.

Types

type Animation added in v1.6.2

type Animation struct {
	Image      []image.Image
	Delay      []int
	LoopCount  int
	Background color.RGBA
}

Animation represents an animated WebP image. Delay values are in milliseconds.

func DecodeAll added in v1.6.2

func DecodeAll(r io.Reader, opt *DecodeOptions) (*Animation, error)

DecodeAll reads a WEBP image from r and returns all frames. Delay values are in milliseconds. If opt is nil, defaults are used.

type C_double

type C_double C.double

type C_float

type C_float C.float

type C_int

type C_int C.int

func C_webpDecodeGrayToSize

func C_webpDecodeGrayToSize(
	data *C_uint8_t, data_size C_size_t,
	width C_int, height C_int, outStride C_int,
	out *C_uint8_t, use_threads C_int,
) C_int

func C_webpDecodeRGBAToSize

func C_webpDecodeRGBAToSize(
	data *C_uint8_t, data_size C_size_t,
	width C_int, height C_int, outStride C_int,
	out *C_uint8_t, use_threads C_int,
) C_int

func C_webpDecodeRGBToSize

func C_webpDecodeRGBToSize(
	data *C_uint8_t, data_size C_size_t,
	width C_int, height C_int, outStride C_int,
	out *C_uint8_t, use_threads C_int,
) C_int

func C_webpGetInfo

func C_webpGetInfo(
	data *C_uint8_t, data_size C_size_t,
	width *C_int, height *C_int,
	has_alpha *C_int,
) C_int

type C_int8_t

type C_int8_t C.int8_t

type C_int16_t

type C_int16_t C.int16_t

type C_int32_t

type C_int32_t C.int32_t

type C_int64_t

type C_int64_t C.int64_t

type C_size_t

type C_size_t C.size_t

type C_uint

type C_uint C.uint

type C_uint8_t

type C_uint8_t C.uint8_t

func C_webpDecodeGray

func C_webpDecodeGray(
	data *C_uint8_t, data_size C_size_t,
	width *C_int, height *C_int,
) *C_uint8_t

func C_webpDecodeRGB

func C_webpDecodeRGB(
	data *C_uint8_t, data_size C_size_t,
	width *C_int, height *C_int,
) *C_uint8_t

func C_webpDecodeRGBA

func C_webpDecodeRGBA(
	data *C_uint8_t, data_size C_size_t,
	width *C_int, height *C_int,
) *C_uint8_t

func C_webpEncodeGray

func C_webpEncodeGray(
	pix *C_uint8_t,
	width C_int, height C_int, stride C_int,
	quality_factor C_float,
	method C_int,
	target_size C_int,
	alpha_quality C_int,
	auto_filter C_int,
	thread_level C_int,
	output_size *C_size_t,
) *C_uint8_t

func C_webpEncodeLosslessGray

func C_webpEncodeLosslessGray(
	pix *C_uint8_t,
	width C_int, height C_int, stride C_int,
	method C_int,
	target_size C_int,
	alpha_quality C_int,
	auto_filter C_int,
	thread_level C_int,
	output_size *C_size_t,
) *C_uint8_t

func C_webpEncodeLosslessRGB

func C_webpEncodeLosslessRGB(
	pix *C_uint8_t,
	width C_int, height C_int, stride C_int,
	method C_int,
	target_size C_int,
	alpha_quality C_int,
	auto_filter C_int,
	thread_level C_int,
	output_size *C_size_t,
) *C_uint8_t

func C_webpEncodeLosslessRGBA

func C_webpEncodeLosslessRGBA(
	exact C_int,
	pix *C_uint8_t,
	width C_int, height C_int, stride C_int,
	method C_int,
	target_size C_int,
	alpha_quality C_int,
	auto_filter C_int,
	thread_level C_int,
	output_size *C_size_t,
) *C_uint8_t

func C_webpEncodeRGB

func C_webpEncodeRGB(
	pix *C_uint8_t,
	width C_int, height C_int, stride C_int,
	quality_factor C_float,
	method C_int,
	target_size C_int,
	alpha_quality C_int,
	auto_filter C_int,
	thread_level C_int,
	output_size *C_size_t,
) *C_uint8_t

func C_webpEncodeRGBA

func C_webpEncodeRGBA(
	pix *C_uint8_t,
	width C_int, height C_int, stride C_int,
	quality_factor C_float,
	method C_int,
	target_size C_int,
	alpha_quality C_int,
	auto_filter C_int,
	thread_level C_int,
	output_size *C_size_t,
) *C_uint8_t

type C_uint16_t

type C_uint16_t C.uint16_t

type C_uint32_t

type C_uint32_t C.uint32_t

type C_uint64_t

type C_uint64_t C.uint64_t

type ColorModelInterface

type ColorModelInterface interface {
	Channels() int
	DataType() reflect.Kind
}

ColorModelInterface exposes channel count and data type for a color model.

type Config added in v1.6.2

type Config struct {
	ColorModel   color.Model
	Width        int  // Width in pixels, as read from the bitstream.
	Height       int  // Height in pixels, as read from the bitstream.
	HasAlpha     bool // True if the bitstream contains an alpha channel.
	HasAnimation bool // True if the bitstream is an animation.
	Format       int  // 0 = undefined (/mixed), 1 = lossy, 2 = lossless
}

Config is like image.Config with alpha and animation metadata.

func DecodeConfigEx added in v1.6.2

func DecodeConfigEx(r io.Reader) (config Config, err error)

DecodeConfigEx returns the color model, dimensions and animation flag of a WEBP image without decoding the entire image.

func LoadConfigEx added in v1.6.2

func LoadConfigEx(name string) (config Config, err error)

LoadConfigEx reads a file header and returns Config metadata.

type DecodeOptions added in v1.6.3

type DecodeOptions struct {
	UseThreads bool // Enable libwebp multi-threading (default true)
}

DecodeOptions controls WebP decoding behavior. A nil options value uses defaults.

type MemP

type MemP interface {
	MemPMagic() string
	Bounds() image.Rectangle
	Channels() int
	DataType() reflect.Kind
	Pix() []byte // PixSilce type

	// Stride is the Pix stride (in bytes, must align with SizeofKind(p.DataType))
	// between vertically adjacent pixels.
	Stride() int
}

MemP defines a native-endian packed pixel format. See https://github.com/chai2010/image.

type MemPColor

type MemPColor struct {
	Channels int
	DataType reflect.Kind
	Pix      PixSlice
}

MemPColor is a packed color value for MemP images.

func (MemPColor) RGBA

func (c MemPColor) RGBA() (r, g, b, a uint32)

RGBA implements the color.Color interface.

type MemPImage

type MemPImage struct {
	XMemPMagic string // MemP
	XRect      image.Rectangle
	XChannels  int
	XDataType  reflect.Kind
	XPix       PixSlice
	XStride    int
}

MemPImage stores pixels in a packed native-endian layout.

func AsMemPImage

func AsMemPImage(m interface{}) (p *MemPImage, ok bool)

AsMemPImage wraps supported images as MemPImage without copying when possible. m may be a MemP implementation or a standard image type.

func NewMemPImage

func NewMemPImage(r image.Rectangle, channels int, dataType reflect.Kind) *MemPImage

NewMemPImage allocates a MemPImage with the given bounds, channels, and data type.

func NewMemPImageFrom

func NewMemPImageFrom(m image.Image) *MemPImage

NewMemPImageFrom converts m into a MemPImage.

func (*MemPImage) AsStdImage

func (p *MemPImage) AsStdImage() (m image.Image, ok bool)

AsStdImage returns a standard library image if p can be represented directly.

func (*MemPImage) At

func (p *MemPImage) At(x, y int) color.Color

At returns the color of the pixel at (x, y).

func (*MemPImage) Bounds

func (p *MemPImage) Bounds() image.Rectangle

Bounds returns the image bounds.

func (*MemPImage) Channels

func (p *MemPImage) Channels() int

Channels returns the number of channels.

func (*MemPImage) Clone

func (p *MemPImage) Clone() *MemPImage

Clone returns a deep copy of p.

func (*MemPImage) ColorModel

func (p *MemPImage) ColorModel() color.Model

ColorModel returns the image's color model.

func (*MemPImage) DataType

func (p *MemPImage) DataType() reflect.Kind

DataType returns the underlying element kind.

func (*MemPImage) MemPMagic

func (p *MemPImage) MemPMagic() string

MemPMagic returns the MemP magic string.

func (*MemPImage) Pix

func (p *MemPImage) Pix() []byte

Pix returns the raw pixel buffer.

func (*MemPImage) PixOffset

func (p *MemPImage) PixOffset(x, y int) int

PixOffset returns the index of the first pixel byte at (x, y).

func (*MemPImage) PixelAt

func (p *MemPImage) PixelAt(x, y int) []byte

PixelAt returns a view of the pixel data at (x, y).

func (*MemPImage) Set

func (p *MemPImage) Set(x, y int, c color.Color)

Set sets the pixel at (x, y) to c.

func (*MemPImage) SetPixel

func (p *MemPImage) SetPixel(x, y int, c []byte)

SetPixel copies raw pixel bytes into the pixel at (x, y).

func (*MemPImage) StdImage

func (p *MemPImage) StdImage() image.Image

StdImage converts p into a standard library image.

func (*MemPImage) Stride

func (p *MemPImage) Stride() int

Stride returns the byte stride between rows.

func (*MemPImage) SubImage

func (p *MemPImage) SubImage(r image.Rectangle) image.Image

SubImage returns an image representing the portion of p visible through r. The returned value shares pixels with the original image.

type Options

type Options struct {
	Lossless     bool
	Quality      float32 // 0 ~ 100
	TargetSize   int     // Target byte size (overrides Quality)
	AlphaQuality int     // Alpha plane quality 0-100 (default 100)
	AutoFilter   bool    // Auto-adjust filter for each image
	Exact        bool    // Preserve RGB values in transparent area.
	Method       int     // Quality/speed trade-off (0=fast, 6=slower-better)
	UseThreads   bool    // Enable libwebp multi-threading (default true)
}

Options are the encoding parameters.

type PixSlice

type PixSlice []byte

PixSlice is a byte-backed view over pixel data.

func AsPixSilce

func AsPixSilce(slice interface{}) (d PixSlice)

AsPixSilce converts a slice to a PixSlice view.

Convert []X to []byte:

x := make([]X, xLen)
y := AsPixSilce(x)
Example
a := []int32{101, 102, 103}
b := AsPixSilce(a)

b.Int32s()[0] = 12345
b.SetValue(1, reflect.Int32, 1002)
b.SetValue(2, reflect.Int32, 1003.5)
fmt.Printf("len(b) = %d\n", len(b))
fmt.Printf("b.Int32s() = %v\n", b.Int32s())
Output:

len(b) = 12
b.Int32s() = [12345 1002 1003]
Example (SwapEndian)
rgba64 := image.NewRGBA64(image.Rect(0, 0, 1, 1))
rgba64.SetRGBA64(0, 0, color.RGBA64{
	R: 0x0102,
	G: 0x0304,
	B: 0x0506,
	A: 0x0708,
})

// pix is big-endian format
fmt.Printf("big-endian: %v\n", rgba64.Pix)

AsPixSilce(rgba64.Pix).SwapEndian(reflect.Uint16)
fmt.Printf("little-endian: %v\n", rgba64.Pix)
Output:

big-endian: [1 2 3 4 5 6 7 8]
little-endian: [2 1 4 3 6 5 8 7]

func (PixSlice) Bytes

func (d PixSlice) Bytes() (v []byte)

Bytes returns the data as a byte slice.

func (PixSlice) Complex64s

func (d PixSlice) Complex64s() (v []complex64)

Complex64s reinterprets the data as []complex64.

func (PixSlice) Complex128s

func (d PixSlice) Complex128s() (v []complex128)

Complex128s reinterprets the data as []complex128.

func (PixSlice) Float32s

func (d PixSlice) Float32s() (v []float32)

Float32s reinterprets the data as []float32.

func (PixSlice) Float64s

func (d PixSlice) Float64s() (v []float64)

Float64s reinterprets the data as []float64.

func (PixSlice) Int8s

func (d PixSlice) Int8s() (v []int8)

Int8s reinterprets the data as []int8.

func (PixSlice) Int16s

func (d PixSlice) Int16s() (v []int16)

Int16s reinterprets the data as []int16.

func (PixSlice) Int32s

func (d PixSlice) Int32s() (v []int32)

Int32s reinterprets the data as []int32.

func (PixSlice) Int64s

func (d PixSlice) Int64s() (v []int64)

Int64s reinterprets the data as []int64.

func (PixSlice) SetValue

func (d PixSlice) SetValue(i int, dataType reflect.Kind, v float64)

SetValue writes the i-th element from v for the given kind.

func (PixSlice) Slice

func (d PixSlice) Slice(newSliceType reflect.Type) interface{}

Slice converts PixSlice to a slice of the provided type.

Convert []byte to []Y:

x := make([]byte, xLen)
y := PixSlice(x).Slice(reflect.TypeOf([]Y(nil))).([]Y)

func (PixSlice) SwapEndian

func (d PixSlice) SwapEndian(dataType reflect.Kind)

SwapEndian swaps byte order for elements of the provided kind.

func (PixSlice) Uint8s

func (d PixSlice) Uint8s() []uint8

Uint8s reinterprets the data as []uint8.

func (PixSlice) Uint16s

func (d PixSlice) Uint16s() (v []uint16)

Uint16s reinterprets the data as []uint16.

func (PixSlice) Uint32s

func (d PixSlice) Uint32s() (v []uint32)

Uint32s reinterprets the data as []uint32.

func (PixSlice) Uint64s

func (d PixSlice) Uint64s() (v []uint64)

Uint64s reinterprets the data as []uint64.

func (PixSlice) Value

func (d PixSlice) Value(i int, dataType reflect.Kind) float64

Value reads the i-th element as float64 for the given kind.

type RGB48Image

type RGB48Image struct {
	XPix    []uint8 // XPix use Native Endian (same as MemP) !!!
	XStride int
	XRect   image.Rectangle
}

RGB48Image stores an RGB image with 16-bit channels in native endian order.

func NewRGB48Image

func NewRGB48Image(r image.Rectangle) *RGB48Image

NewRGB48Image returns a new RGB48Image with the given bounds.

func NewRGB48ImageFrom

func NewRGB48ImageFrom(m image.Image) *RGB48Image

NewRGB48ImageFrom converts m into an RGB48Image.

func (*RGB48Image) At

func (p *RGB48Image) At(x, y int) color.Color

At returns the color of the pixel at (x, y).

func (*RGB48Image) Bounds

func (p *RGB48Image) Bounds() image.Rectangle

Bounds returns the image bounds.

func (*RGB48Image) Channels

func (p *RGB48Image) Channels() int

Channels returns the number of channels.

func (*RGB48Image) ColorModel

func (p *RGB48Image) ColorModel() color.Model

ColorModel returns the image's color model.

func (*RGB48Image) DataType

func (p *RGB48Image) DataType() reflect.Kind

DataType returns the underlying element kind.

func (*RGB48Image) MemPMagic

func (p *RGB48Image) MemPMagic() string

MemPMagic returns the MemP magic string.

func (*RGB48Image) Opaque

func (p *RGB48Image) Opaque() bool

Opaque scans the entire image and reports whether it is fully opaque.

func (*RGB48Image) Pix

func (p *RGB48Image) Pix() []byte

Pix returns the raw pixel buffer.

func (*RGB48Image) PixOffset

func (p *RGB48Image) PixOffset(x, y int) int

PixOffset returns the index of the first element of XPix that corresponds to the pixel at (x, y).

func (*RGB48Image) RGB48At

func (p *RGB48Image) RGB48At(x, y int) [3]uint16

RGB48At returns the RGB values at (x, y).

func (*RGB48Image) Set

func (p *RGB48Image) Set(x, y int, c color.Color)

Set sets the pixel at (x, y) to c.

func (*RGB48Image) SetRGB48

func (p *RGB48Image) SetRGB48(x, y int, c [3]uint16)

SetRGB48 sets the pixel at (x, y) to an RGB triplet.

func (*RGB48Image) Stride

func (p *RGB48Image) Stride() int

Stride returns the byte stride between rows.

func (*RGB48Image) SubImage

func (p *RGB48Image) SubImage(r image.Rectangle) image.Image

SubImage returns an image representing the portion of the image p visible through r. The returned value shares pixels with the original image.

type RGBImage

type RGBImage struct {
	XPix    []uint8
	XStride int
	XRect   image.Rectangle
}

RGBImage stores an RGB image in packed 8-bit format.

func DecodeRGB

func DecodeRGB(data []byte, opt *DecodeOptions) (m *RGBImage, err error)

DecodeRGB decodes a WebP payload into an RGBImage. If opt is nil, defaults are used.

func DecodeRGBToSize

func DecodeRGBToSize(data []byte, width, height int, opt *DecodeOptions) (m *RGBImage, err error)

DecodeRGBToSize decodes an RGB image scaled to the given dimensions. If opt is nil, defaults are used.

func NewRGBImage

func NewRGBImage(r image.Rectangle) *RGBImage

NewRGBImage returns a new RGBImage with the given bounds.

func NewRGBImageFrom

func NewRGBImageFrom(m image.Image) *RGBImage

NewRGBImageFrom converts m into an RGBImage.

func (*RGBImage) At

func (p *RGBImage) At(x, y int) color.Color

At returns the color of the pixel at (x, y).

func (*RGBImage) Bounds

func (p *RGBImage) Bounds() image.Rectangle

Bounds returns the image bounds.

func (*RGBImage) Channels

func (p *RGBImage) Channels() int

Channels returns the number of channels.

func (*RGBImage) ColorModel

func (p *RGBImage) ColorModel() color.Model

ColorModel returns the image's color model.

func (*RGBImage) DataType

func (p *RGBImage) DataType() reflect.Kind

DataType returns the underlying element kind.

func (*RGBImage) MemPMagic

func (p *RGBImage) MemPMagic() string

MemPMagic returns the MemP magic string.

func (*RGBImage) Opaque

func (p *RGBImage) Opaque() bool

Opaque scans the entire image and reports whether it is fully opaque.

func (*RGBImage) Pix

func (p *RGBImage) Pix() []byte

Pix returns the raw pixel buffer.

func (*RGBImage) PixOffset

func (p *RGBImage) PixOffset(x, y int) int

PixOffset returns the index of the first element of Pix that corresponds to the pixel at (x, y).

func (*RGBImage) RGBAt

func (p *RGBImage) RGBAt(x, y int) [3]uint8

RGBAt returns the RGB values at (x, y).

func (*RGBImage) Set

func (p *RGBImage) Set(x, y int, c color.Color)

Set sets the pixel at (x, y) to c.

func (*RGBImage) SetRGB

func (p *RGBImage) SetRGB(x, y int, c [3]uint8)

SetRGB sets the pixel at (x, y) to an RGB triplet.

func (*RGBImage) Stride

func (p *RGBImage) Stride() int

Stride returns the byte stride between rows.

func (*RGBImage) SubImage

func (p *RGBImage) SubImage(r image.Rectangle) image.Image

SubImage returns an image representing the portion of the image p visible through r. The returned value shares pixels with the original image.

type SizeofImager

type SizeofImager interface {
	SizeofImage() int
}

SizeofImager reports the approximate size in bytes of an image.

Directories

Path Synopsis
Package webp_bench provides more benchmark for webp.
Package webp_bench provides more benchmark for webp.
See https://github.com/dvyukov/go-fuzz
See https://github.com/dvyukov/go-fuzz
internal

Jump to

Keyboard shortcuts

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