forked from chai2010/bpg
/
reader.go
58 lines (53 loc) · 1.22 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
// Copyright 2015 <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 bpg
import (
"image"
"image/color"
"io"
"io/ioutil"
)
// DecodeConfig returns the color model and dimensions of a BPG image without
// decoding the entire image.
func DecodeConfig(r io.Reader) (config image.Config, err error) {
header := make([]byte, maxHeaderSize)
n, err := r.Read(header)
if err != nil && err != io.EOF {
return
}
header, err = header[:n], nil
info, err := DecodeInfo(header)
if err != nil {
return
}
config.Width = int(info.Width)
config.Height = int(info.Height)
if info.BitDepth == 8 {
config.ColorModel = color.RGBAModel
} else {
config.ColorModel = color.RGBA64Model
}
return
}
// Decode reads a BPG 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
}
info, err := DecodeInfo(data)
if err != nil {
return
}
if info.BitDepth == 8 {
m, err = DecodeRGBA32(data)
return
} else {
m, err = DecodeRGBA64(data)
return
}
}
func init() {
image.RegisterFormat("bpg", headerMagic, Decode, DecodeConfig)
}