You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
146 lines
2.8 KiB
146 lines
2.8 KiB
go-libwebp
|
|
==========
|
|
|
|
[](https://travis-ci.org/harukasan/go-libwebp)
|
|
[](https://godoc.org/github.com/harukasan/go-libwebp/webp)
|
|
|
|
A implementation of Go binding for [libwebp](https://developers.google.com/speed/webp/docs/api).
|
|
|
|
## Dependencies
|
|
|
|
- libwebp 0.4, 0.5+, compiled with `--enable-libwebpmux`
|
|
|
|
## Usage
|
|
|
|
The [examples](./examples) directory contains example codes and images.
|
|
|
|
### Decoding WebP into image.RGBA
|
|
|
|
```
|
|
package main
|
|
|
|
import (
|
|
"github.com/harukasan/go-libwebp/test/util"
|
|
"github.com/harukasan/go-libwebp/webp"
|
|
)
|
|
|
|
func main() {
|
|
var err error
|
|
|
|
// Read binary data
|
|
data := util.ReadFile("cosmos.webp")
|
|
|
|
// Decode
|
|
options := &webp.DecoderOptions{}
|
|
img, err := webp.DecodeRGBA(data, options)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
util.WritePNG(img, "encoded_cosmos.png")
|
|
}
|
|
```
|
|
|
|
You can set more decoding options such as cropping, flipping and scaling.
|
|
|
|
### Encoding WebP from image.RGBA
|
|
|
|
```
|
|
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"image"
|
|
|
|
"github.com/harukasan/go-libwebp/test/util"
|
|
"github.com/harukasan/go-libwebp/webp"
|
|
)
|
|
|
|
func main() {
|
|
img := util.ReadPNG("cosmos.png")
|
|
|
|
// Create file and buffered writer
|
|
io := util.CreateFile("encoded_cosmos.webp")
|
|
w := bufio.NewWriter(io)
|
|
defer func() {
|
|
w.Flush()
|
|
io.Close()
|
|
}()
|
|
|
|
config := webp.ConfigPreset(webp.PresetDefault, 90)
|
|
|
|
// Encode into WebP
|
|
if err := webp.EncodeRGBA(w, img.(*image.RGBA), config); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
```
|
|
|
|
|
|
### Encoding animations from a series of frames
|
|
|
|
```
|
|
package main
|
|
|
|
import (
|
|
"image"
|
|
"time"
|
|
|
|
"github.com/harukasan/go-libwebp/test/util"
|
|
"github.com/harukasan/go-libwebp/webp"
|
|
)
|
|
|
|
func main() {
|
|
// Get some frames
|
|
img := []image.Image{
|
|
util.ReadPNG("butterfly.png"),
|
|
util.ReadPNG("checkerboard.png"),
|
|
util.ReadPNG("yellow-rose-3.png"),
|
|
}
|
|
|
|
// Initialize the animation encoder
|
|
width, height := 24, 24
|
|
anim, err := webp.NewAnimationEncoder(width, height, 0, 0)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer anim.Close()
|
|
|
|
// Add each frame to the animation
|
|
for i, im := range img {
|
|
// all frames of an animation must have the same dimensions
|
|
cropped := im.(interface {
|
|
SubImage(r image.Rectangle) image.Image
|
|
}).SubImage(image.Rect(0, 0, width, height))
|
|
|
|
if err := anim.AddFrame(cropped, 100*time.Millisecond); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// Assemble the final animation
|
|
buf, err := anim.Assemble()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
// Write to disk
|
|
f := util.CreateFile("animation.webp")
|
|
defer f.Close()
|
|
f.Write(buf)
|
|
}
|
|
|
|
```
|
|
|
|
## TODO
|
|
|
|
- Incremental decoding API
|
|
- Animation decoding
|
|
|
|
## License
|
|
|
|
Copyright (c) 2016 MICHII Shunsuke. All rights reserved.
|
|
|
|
This library is released under The BSD 2-Clause License.
|
|
See [LICENSE](./LICENSE).
|