webp: github.com/chai2010/webp Index | Examples | Files | Directories

package webp

import "github.com/chai2010/webp"

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

Package Files

capi.go cgo.go doc.go gen.go image.go image_color.go image_pix_slice.go reader.go rgb.go rgb48.go webp.go writer.go

Constants

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

func C_webpFree Uses

func C_webpFree(p unsafe.Pointer)

func C_webpMalloc Uses

func C_webpMalloc(size C_size_t) unsafe.Pointer

func ChannelsOf Uses

func ChannelsOf(m image.Image) int

func ColorModel Uses

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

Code:

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 Uses

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 Uses

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 Uses

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

func DecodeGrayToSize Uses

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 Uses

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

func DecodeRGBAToSize Uses

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

DecodeRGBAToSize decodes a Gray image scaled to the given dimensions.

func DepthOf Uses

func DepthOf(m image.Image) int

func Encode Uses

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

Encode writes the image m to w in WEBP format.

Code:

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

Code:

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

Code:

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

Code:

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 EncodeExactLosslessRGBA Uses

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 Uses

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

func EncodeLosslessGray Uses

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

func EncodeLosslessRGB Uses

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

func EncodeLosslessRGBA Uses

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

func EncodeRGB Uses

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

func EncodeRGBA Uses

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

func GetInfo Uses

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

Code:

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

Code:

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 Uses

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

GetMetadata return EXIF/ICCP/XMP format metadata.

func Load Uses

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

Code:

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 Uses

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

Code:

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 Uses

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

Code:

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 Uses

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

SetMetadata set EXIF/ICCP/XMP format metadata.

func SizeofImage Uses

func SizeofImage(m image.Image) int

func SizeofKind Uses

func SizeofKind(dataType reflect.Kind) int

Code:

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 Uses

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

Code:

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

type C_double Uses

type C_double C.double

type C_float Uses

type C_float C.float

type C_int Uses

type C_int C.int

func C_webpDecodeGrayToSize Uses

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 Uses

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 Uses

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 Uses

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 Uses

type C_int16_t C.int16_t

type C_int32_t Uses

type C_int32_t C.int32_t

type C_int64_t Uses

type C_int64_t C.int64_t

type C_int8_t Uses

type C_int8_t C.int8_t

type C_size_t Uses

type C_size_t C.size_t

type C_uint Uses

type C_uint C.uint

type C_uint16_t Uses

type C_uint16_t C.uint16_t

type C_uint32_t Uses

type C_uint32_t C.uint32_t

type C_uint64_t Uses

type C_uint64_t C.uint64_t

type C_uint8_t Uses

type C_uint8_t C.uint8_t

func C_webpDecodeGray Uses

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

func C_webpDecodeRGB Uses

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

func C_webpDecodeRGBA Uses

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

func C_webpEncodeGray Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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

type MemP Uses

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 Uses

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

func (MemPColor) RGBA Uses

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

type MemPImage Uses

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

func AsMemPImage Uses

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

m is MemP or image.Image

func NewMemPImage Uses

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

func NewMemPImageFrom Uses

func NewMemPImageFrom(m image.Image) *MemPImage

func (*MemPImage) AsStdImage Uses

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

func (*MemPImage) At Uses

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

func (*MemPImage) Bounds Uses

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

func (*MemPImage) Channels Uses

func (p *MemPImage) Channels() int

func (*MemPImage) Clone Uses

func (p *MemPImage) Clone() *MemPImage

func (*MemPImage) ColorModel Uses

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

func (*MemPImage) DataType Uses

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

func (*MemPImage) MemPMagic Uses

func (p *MemPImage) MemPMagic() string

func (*MemPImage) Pix Uses

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

func (*MemPImage) PixOffset Uses

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

func (*MemPImage) PixelAt Uses

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

func (*MemPImage) Set Uses

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

func (*MemPImage) SetPixel Uses

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

func (*MemPImage) StdImage Uses

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

func (*MemPImage) Stride Uses

func (p *MemPImage) Stride() int

func (*MemPImage) SubImage Uses

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

type Options Uses

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

Options are the encoding parameters.

type PixSlice Uses

type PixSlice []byte

func AsPixSilce Uses

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 Uses

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

func (PixSlice) Complex128s Uses

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

func (PixSlice) Complex64s Uses

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

func (PixSlice) Float32s Uses

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

func (PixSlice) Float64s Uses

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

func (PixSlice) Int16s Uses

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

func (PixSlice) Int32s Uses

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

func (PixSlice) Int64s Uses

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

func (PixSlice) Int8s Uses

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

func (PixSlice) SetValue Uses

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

func (PixSlice) Slice Uses

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 Uses

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

func (PixSlice) Uint16s Uses

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

func (PixSlice) Uint32s Uses

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

func (PixSlice) Uint64s Uses

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

func (PixSlice) Uint8s Uses

func (d PixSlice) Uint8s() []uint8

func (PixSlice) Value Uses

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

type RGB48Image Uses

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

func NewRGB48Image Uses

func NewRGB48Image(r image.Rectangle) *RGB48Image

NewRGB48Image returns a new RGB48Image with the given bounds.

func NewRGB48ImageFrom Uses

func NewRGB48ImageFrom(m image.Image) *RGB48Image

func (*RGB48Image) At Uses

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

func (*RGB48Image) Bounds Uses

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

func (*RGB48Image) Channels Uses

func (p *RGB48Image) Channels() int

func (*RGB48Image) ColorModel Uses

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

func (*RGB48Image) DataType Uses

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

func (*RGB48Image) MemPMagic Uses

func (p *RGB48Image) MemPMagic() string

func (*RGB48Image) Opaque Uses

func (p *RGB48Image) Opaque() bool

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

func (*RGB48Image) Pix Uses

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

func (*RGB48Image) PixOffset Uses

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 Uses

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

func (*RGB48Image) Set Uses

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

func (*RGB48Image) SetRGB48 Uses

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

func (*RGB48Image) Stride Uses

func (p *RGB48Image) Stride() int

func (*RGB48Image) SubImage Uses

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 Uses

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

func DecodeRGB Uses

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

func DecodeRGBToSize Uses

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

DecodeRGBToSize decodes an RGB image scaled to the given dimensions.

func NewRGBImage Uses

func NewRGBImage(r image.Rectangle) *RGBImage

NewRGBImage returns a new RGBImage with the given bounds.

func NewRGBImageFrom Uses

func NewRGBImageFrom(m image.Image) *RGBImage

func (*RGBImage) At Uses

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

func (*RGBImage) Bounds Uses

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

func (*RGBImage) Channels Uses

func (p *RGBImage) Channels() int

func (*RGBImage) ColorModel Uses

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

func (*RGBImage) DataType Uses

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

func (*RGBImage) MemPMagic Uses

func (p *RGBImage) MemPMagic() string

func (*RGBImage) Opaque Uses

func (p *RGBImage) Opaque() bool

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

func (*RGBImage) Pix Uses

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

func (*RGBImage) PixOffset Uses

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 Uses

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

func (*RGBImage) Set Uses

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

func (*RGBImage) SetRGB Uses

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

func (*RGBImage) Stride Uses

func (p *RGBImage) Stride() int

func (*RGBImage) SubImage Uses

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 Uses

type SizeofImager interface {
    SizeofImage() int
}

Directories

PathSynopsis
benchPackage webp_bench provides more benchmark for webp.
fuzzSee https://github.com/dvyukov/go-fuzz

Package webp imports 11 packages (graph) and is imported by 21 packages. Updated 2019-03-19. Refresh now. Tools for package owners.