forked from chai2010/webp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.go
47 lines (42 loc) · 1.04 KB
/
reader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright 2014 <chaishushan{AT}gmail.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package webp
import (
"image"
"image/color"
"io"
"io/ioutil"
)
// DecodeConfig returns the color model and dimensions of a WEBP image without
// decoding the entire image.
func DecodeConfig(r io.Reader) (config image.Config, err error) {
header := make([]byte, maxWebpHeaderSize)
n, err := r.Read(header)
if err != nil && err != io.EOF {
return
}
header, err = header[:n], nil
width, height, _, err := GetInfo(header)
if err != nil {
return
}
config.Width = width
config.Height = height
config.ColorModel = color.RGBAModel
return
}
// Decode reads a WEBP image from r and returns it as an image.Image.
func Decode(r io.Reader) (m image.Image, err error) {
data, err := ioutil.ReadAll(r)
if err != nil {
return
}
if m, err = DecodeRGBA(data); err != nil {
return
}
return
}
func init() {
image.RegisterFormat("webp", "RIFF????WEBPVP8", Decode, DecodeConfig)
}