webp

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2022 License: BSD-3-Clause Imports: 12 Imported by: 0

README


webp

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

Build Status GoDoc

Benchmark

Install

Install GCC or MinGW (download here) at first, and then run these commands:

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

Example

This is a simple example:

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"log"

	"github.com/chai2010/webp"
)

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

	// Load file data
	if data, err = ioutil.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))
	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 = ioutil.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)
if err != nil {
	log.Fatal(err)
}

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

Notes

Change the libwebp to fast method:

internal/libwebp/src/enc/config.c
WebPConfigInitInternal
config->method = 0; // 4;

BUGS

Report bugs to chaishushan@gmail.com.

Thanks!

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/chai2010/webp`
  2. `go run hello.go`

Examples

This is a simple example:

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"log"

	"github.com/chai2010/webp"
)

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

	// Load file data
	if data, err = ioutil.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))
	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 = ioutil.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)
if err != nil {
	log.Fatal(err)
}

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

BUGS

Report bugs to <chaishushan@gmail.com>.

Thanks!

Index

Examples

Constants

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

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

func ColorModel

func ColorModel(channels int, dataType reflect.Kind) color.Model
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) (m image.Image, err error)

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

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) (m *image.Gray, err error)

func DecodeGrayToSize

func DecodeGrayToSize(data []byte, width, height int) (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.

func DecodeRGBA

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

func DecodeRGBAToSize

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

DecodeRGBAToSize decodes a Gray image scaled to the given dimensions.

func DepthOf

func DepthOf(m image.Image) int

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()
Output:

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()
Output:

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()
Output:

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()
Output:

func EncodeExactLosslessRGBA

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

EncodeExactLosslessRGBA Encode lossless RGB mode with exact. exact: preserve RGB values in transparent area.

func EncodeGray

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

func EncodeLosslessGray

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

func EncodeLosslessRGB

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

func EncodeLosslessRGBA

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

func EncodeRGB

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

func EncodeRGBA

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

func GetInfo

func GetInfo(data []byte) (width, height int, hasAlpha bool, err error)
Example
data := xLoadData("1_webp_a.webp")

width, height, hasAlpha, 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)
Output:

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

width, height, hasAlpha, 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)
Output:

width: 150
height: 103
hasAlpha: false

func GetMetadata

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

GetMetadata return EXIF/ICCP/XMP format metadata.

func Load

func Load(name string) (m image.Image, err error)
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)
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)
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)
}
Output:

func SetMetadata

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

SetMetadata set EXIF/ICCP/XMP format metadata.

func SizeofImage

func SizeofImage(m image.Image) int

func SizeofKind

func SizeofKind(dataType reflect.Kind) int
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
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

Types

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,
) 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,
) 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,
) 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_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_int8_t

type C_int8_t C.int8_t

type C_size_t

type C_size_t C.size_t

type C_uint

type C_uint C.uint

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 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,
	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,
	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,
	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,
	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,
	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,
	output_size *C_size_t,
) *C_uint8_t

type ColorModelInterface

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

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 Image Spec (Native Endian), see https://github.com/chai2010/image.

type MemPColor

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

func (MemPColor) RGBA

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

type MemPImage

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

func AsMemPImage

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

m is MemP or image.Image

func NewMemPImage

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

func NewMemPImageFrom

func NewMemPImageFrom(m image.Image) *MemPImage

func (*MemPImage) AsStdImage

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

func (*MemPImage) At

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

func (*MemPImage) Bounds

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

func (*MemPImage) Channels

func (p *MemPImage) Channels() int

func (*MemPImage) Clone

func (p *MemPImage) Clone() *MemPImage

func (*MemPImage) ColorModel

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

func (*MemPImage) DataType

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

func (*MemPImage) MemPMagic

func (p *MemPImage) MemPMagic() string

func (*MemPImage) Pix

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

func (*MemPImage) PixOffset

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

func (*MemPImage) PixelAt

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

func (*MemPImage) Set

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

func (*MemPImage) SetPixel

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

func (*MemPImage) StdImage

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

func (*MemPImage) Stride

func (p *MemPImage) Stride() int

func (*MemPImage) SubImage

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

type Options

type Options struct {
	Lossless bool
	Quality  float32 // 0 ~ 100
	Exact    bool    // Preserve RGB values in transparent area.
}

Options are the encoding parameters.

type PixSlice

type PixSlice []byte

func AsPixSilce

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

AsPixSilce convert a normal slice to byte slice.

Convert []X to []byte:

x := make([]X, xLen)
y := AsPixSilce(x)

func (PixSlice) Bytes

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

func (PixSlice) Complex128s

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

func (PixSlice) Complex64s

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

func (PixSlice) Float32s

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

func (PixSlice) Float64s

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

func (PixSlice) Int16s

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

func (PixSlice) Int32s

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

func (PixSlice) Int64s

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

func (PixSlice) Int8s

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

func (PixSlice) SetValue

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

func (PixSlice) Slice

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

Slice convert a normal slice to new type slice.

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)

func (PixSlice) Uint16s

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

func (PixSlice) Uint32s

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

func (PixSlice) Uint64s

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

func (PixSlice) Uint8s

func (d PixSlice) Uint8s() []uint8

func (PixSlice) Value

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

type RGB48Image

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

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

func (*RGB48Image) At

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

func (*RGB48Image) Bounds

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

func (*RGB48Image) Channels

func (p *RGB48Image) Channels() int

func (*RGB48Image) ColorModel

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

func (*RGB48Image) DataType

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

func (*RGB48Image) MemPMagic

func (p *RGB48Image) MemPMagic() 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

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

func (*RGB48Image) Set

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

func (*RGB48Image) SetRGB48

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

func (*RGB48Image) Stride

func (p *RGB48Image) Stride() int

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
}

func DecodeRGB

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

func DecodeRGBToSize

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

DecodeRGBToSize decodes an RGB image scaled to the given dimensions.

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

func (*RGBImage) At

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

func (*RGBImage) Bounds

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

func (*RGBImage) Channels

func (p *RGBImage) Channels() int

func (*RGBImage) ColorModel

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

func (*RGBImage) DataType

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

func (*RGBImage) MemPMagic

func (p *RGBImage) MemPMagic() 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

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

func (*RGBImage) Set

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

func (*RGBImage) SetRGB

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

func (*RGBImage) Stride

func (p *RGBImage) Stride() int

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
}

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

Jump to

Keyboard shortcuts

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