initial commit

This commit is contained in:
harukasan 2015-01-09 12:51:34 +09:00
commit 5af6d93b3b
26 changed files with 1165 additions and 0 deletions

5
examples/README.md Normal file
View file

@ -0,0 +1,5 @@
Example Codes and Images
========================
- [decode/decode.go](./decode/decode.go) -- a example code to decoding WebP image into image.RGBA.
- [encode/encode.go](./encode/encode.go) -- a example code to encoding and writing image.RGBA into WebP file.

22
examples/decode/decode.go Normal file
View file

@ -0,0 +1,22 @@
package main
import (
"github.com/harukasan/go-libwebp/examples/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")
}

32
examples/encode/encode.go Normal file
View file

@ -0,0 +1,32 @@
package main
import (
"bufio"
"image"
"github.com/harukasan/go-libwebp/examples/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.Config{
Preset: webp.PresetDefault,
Quality: 90,
Method: 6,
}
// Encode into WebP
if err := webp.EncodeRGBA(w, img.(*image.RGBA), config); err != nil {
panic(err)
}
}

111
examples/images/README.md Normal file
View file

@ -0,0 +1,111 @@
Example Images
==============
This directory contains example WebP encoded files and PNG source files.
## Image Credits
### Photos by Author
These images are taken by author.
These photos are licensed under the Creative Commons Attribution 3.0.
You can also use these images under the same as [go-libwebp's license](../LICENSE).
#### cosmos.png
The cosmos taken in Nokono-shima (Nokono Island), Fukuoka, JAPAN.
![cosmos.png](cosmos.png)
WebP file is generated by following command:
```sh
$ cwebp -q 90 cosmos.png -o cosmos.webp
Saving file 'cosmos.webp'
File: cosmos.png
Dimension: 1024 x 768
Output: 76954 bytes Y-U-V-All-PSNR 45.87 47.63 48.10 46.44 dB
block count: intra4: 2972
intra16: 100 (-> 3.26%)
skipped block: 1 (0.03%)
bytes used: header: 249 (0.3%)
mode-partition: 15161 (19.7%)
Residuals bytes |segment 1|segment 2|segment 3|segment 4| total
macroblocks: | 0%| 11%| 33%| 54%| 3072
quantizer: | 12 | 11 | 9 | 8 |
filter level: | 4 | 2 | 2 | 4 |
```
#### butterfly.png
The butterfly taken in Ishigaki-jima (Ishigaki Island) in Okinawa, JAPAN.
![butterfly.png](butterfly.png)
WebP file is generated by following command:
```sh
$ cwebp -q 90 butterfly.png -o butterfly.webp
Saving file 'butterfly.webp'
File: butterfly.png
Dimension: 1024 x 768
Output: 79198 bytes Y-U-V-All-PSNR 45.50 48.71 49.90 46.44 dB
block count: intra4: 2775
intra16: 297 (-> 9.67%)
skipped block: 0 (0.00%)
bytes used: header: 383 (0.5%)
mode-partition: 13227 (16.7%)
Residuals bytes |segment 1|segment 2|segment 3|segment 4| total
macroblocks: | 1%| 7%| 14%| 75%| 3072
quantizer: | 12 | 12 | 10 | 8 |
filter level: | 4 | 3 | 2 | 6 |
```
#### kinkaku.png
Kinkaku taken in Kyoto, JAPAN.
![kinkaku.png](kinkaku.png)
WebP file is generated by following command:
```sh
$ cwebp -q 90 kinkaku.png -o kinkaku.webp
Saving file 'kinkaku.webp'
File: kinkaku.png
Dimension: 1024 x 768
Output: 186300 bytes Y-U-V-All-PSNR 43.86 47.25 48.26 44.81 dB
block count: intra4: 2775
intra16: 297 (-> 9.67%)
skipped block: 243 (7.91%)
bytes used: header: 425 (0.2%)
mode-partition: 16737 (9.0%)
Residuals bytes |segment 1|segment 2|segment 3|segment 4| total
macroblocks: | 2%| 28%| 40%| 28%| 3072
quantizer: | 12 | 11 | 9 | 6 |
filter level: | 4 | 2 | 2 | 1 |
```
### From Google WebP Gallery
These images picked up from [WebP Gallery](https://developers.google.com/speed/webp/gallery) in Google Developers.
#### yellow-rose-3.png
"Free Stock Photo in High Resolution - Yellow Rose 3 - Flowers" by Jon Sullivan
This image is clipped by Google and licensed under [Creative Commons Attribution 3.0](https://creativecommons.org/licenses/by/3.0/). The Source of this image is in the public domain.
<div style="background:url('./checkerboard.png')">
<img title="yellow-rose-3.png" src="yellow-rose-3.png">
</div>
#### fizyplankton.png
"baby tux for my user page" by Fizyplankton.
This image file is in the public domain.
<div style="background:url('./checkerboard.png')">
<img title="fizyplankton.png" src="fizyplankton.png">
</div>

Binary file not shown.

After

Width:  |  Height:  |  Size: 997 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 77 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

BIN
examples/images/cosmos.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 964 KiB

BIN
examples/images/cosmos.webp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 75 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

BIN
examples/images/kinkaku.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 182 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 118 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

0
examples/out/.keep Normal file
View file

80
examples/util/util.go Normal file
View file

@ -0,0 +1,80 @@
// Package util contains utility code for demosntration of go-libwebp.
package util
import (
"bufio"
"image"
"image/png"
"io"
"io/ioutil"
"os"
"path/filepath"
)
// GetExFilePath returns the path of specified example file.
func GetExFilePath(name string) string {
return filepath.Join(os.Getenv("GOPATH"), "src/github.com/harukasan/go-libwebp/examples/images", name)
}
// GetOutFilePath returns the path of specified out file.
func GetOutFilePath(name string) string {
return filepath.Join(os.Getenv("GOPATH"), "src/github.com/harukasan/go-libwebp/examples/out", name)
}
// OpenFile opens specified example file
func OpenFile(name string) (io io.Reader) {
io, err := os.Open(GetExFilePath(name))
if err != nil {
panic(err)
}
return
}
// ReadFile reads and returns data bytes of specified example file.
func ReadFile(name string) (data []byte) {
data, err := ioutil.ReadFile(GetExFilePath(name))
if err != nil {
panic(err)
}
return
}
// CreateFile opens specified example file
func CreateFile(name string) (f *os.File) {
f, err := os.Create(GetOutFilePath(name))
if err != nil {
panic(err)
}
return
}
// WritePNG encodes and writes image into PNG file.
func WritePNG(img image.Image, name string) {
f, err := os.Create(GetOutFilePath(name))
if err != nil {
panic(err)
}
b := bufio.NewWriter(f)
defer func() {
b.Flush()
f.Close()
}()
if err := png.Encode(b, img); err != nil {
panic(err)
}
return
}
// ReadPNG reads and decodes png data into image.Image
func ReadPNG(name string) (img image.Image) {
io, err := os.Open(GetExFilePath(name))
if err != nil {
panic(err)
}
img, err = png.Decode(io)
if err != nil {
panic(err)
}
return
}