-
Notifications
You must be signed in to change notification settings - Fork 92
/
reader.go
101 lines (89 loc) · 1.98 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// 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.
//go:build cgo
// +build cgo
package webp
import (
"errors"
"image"
"image/color"
"io"
"io/ioutil"
"os"
)
func LoadConfig(name string) (config image.Config, err error) {
f, err := os.Open(name)
if err != nil {
return image.Config{}, err
}
defer f.Close()
header := make([]byte, maxWebpHeaderSize)
n, err := f.Read(header)
if err != nil && err != io.EOF {
return
}
header = header[:n]
width, height, _, err := GetInfo(header)
if err != nil {
return
}
config.Width = width
config.Height = height
config.ColorModel = color.RGBAModel
return
}
func Load(name string) (m image.Image, err error) {
f, err := os.Open(name)
if err != nil {
return nil, err
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
return nil, err
}
if fi.Size() > (2 << 30) {
return nil, errors.New("webp: Load, file size is too large (> 2GB)!")
}
data := make([]byte, int(fi.Size()))
if _, err = f.Read(data); err != nil {
return nil, err
}
if m, err = DecodeRGBA(data); err != nil {
return
}
return
}
// 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)
}