27 lines
486 B
Go
27 lines
486 B
Go
package codec
|
|
|
|
import (
|
|
"bytes"
|
|
"image"
|
|
"io"
|
|
|
|
"github.com/chai2010/webp"
|
|
)
|
|
|
|
type WebpCoder struct{}
|
|
|
|
func (coder WebpCoder) Decode(r io.Reader) (image.Image, error) {
|
|
i, err := webp.Decode(r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return i, nil
|
|
}
|
|
|
|
func (coder WebpCoder) Encode(i image.Image, quality int) (buf bytes.Buffer, err error) {
|
|
if err = webp.Encode(&buf, i, &webp.Options{Lossless: true, Quality: float32(quality)}); err != nil {
|
|
return buf, err
|
|
}
|
|
return buf, nil
|
|
}
|