mirror of
https://github.com/talgo-cloud/talgo-libwebp.git
synced 2026-03-09 07:28:20 -07:00
Add documentation and tests for animation
This commit is contained in:
parent
7430f2cbfa
commit
fb5d8a6b3d
4 changed files with 112 additions and 3 deletions
2
Makefile
2
Makefile
|
|
@ -16,7 +16,7 @@ $(libwebp_so):
|
||||||
&& wget http://downloads.webmproject.org/releases/webp/libwebp-$(LIBWEBP_VERSION).tar.gz \
|
&& wget http://downloads.webmproject.org/releases/webp/libwebp-$(LIBWEBP_VERSION).tar.gz \
|
||||||
&& tar xf libwebp-$(LIBWEBP_VERSION).tar.gz \
|
&& tar xf libwebp-$(LIBWEBP_VERSION).tar.gz \
|
||||||
&& cd libwebp-$(LIBWEBP_VERSION) \
|
&& cd libwebp-$(LIBWEBP_VERSION) \
|
||||||
&& ./configure --prefix=$(LIBWEBP_PREFIX) \
|
&& ./configure --prefix=$(LIBWEBP_PREFIX) --enable-libwebpmux \
|
||||||
&& make \
|
&& make \
|
||||||
&& make install
|
&& make install
|
||||||
|
|
||||||
|
|
|
||||||
60
README.md
60
README.md
|
|
@ -8,7 +8,7 @@ A implementation of Go binding for [libwebp](https://developers.google.com/speed
|
||||||
|
|
||||||
## Dependencies
|
## Dependencies
|
||||||
|
|
||||||
- libwebp 0.4, 0.5
|
- libwebp 0.4, 0.5+, compiled with `--enable-libwebpmux`
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
|
|
@ -76,10 +76,66 @@ func main() {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### 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
|
## TODO
|
||||||
|
|
||||||
- Incremental decoding API
|
- Incremental decoding API
|
||||||
- Container API (Animation)
|
- Animation decoding
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
package webp
|
package webp
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
#cgo LDFLAGS: -lwebpmux
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <webp/encode.h>
|
#include <webp/encode.h>
|
||||||
|
|
|
||||||
51
webp/anim_encode_test.go
Normal file
51
webp/anim_encode_test.go
Normal file
|
|
@ -0,0 +1,51 @@
|
||||||
|
package webp_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"image"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/harukasan/go-libwebp/test/util"
|
||||||
|
"github.com/harukasan/go-libwebp/webp"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestEncodeAnimation(t *testing.T) {
|
||||||
|
data := util.ReadFile("cosmos.webp")
|
||||||
|
aWebP, err := webp.DecodeRGBA(data, &webp.DecoderOptions{})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Got Error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
img := []image.Image{
|
||||||
|
util.ReadPNG("butterfly.png"),
|
||||||
|
util.ReadPNG("checkerboard.png"),
|
||||||
|
util.ReadPNG("yellow-rose-3.png"),
|
||||||
|
aWebP,
|
||||||
|
}
|
||||||
|
|
||||||
|
width, height := 24, 24
|
||||||
|
anim, err := webp.NewAnimationEncoder(width, height, 0, 0)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("initializing decoder: %v", err)
|
||||||
|
}
|
||||||
|
defer anim.Close()
|
||||||
|
|
||||||
|
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 {
|
||||||
|
t.Errorf("adding frame %d: %v", i, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
buf, err := anim.Assemble()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("assembling animation: %v", err)
|
||||||
|
}
|
||||||
|
if len(buf) == 0 {
|
||||||
|
t.Errorf("assembled animation is empty")
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue