-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathpng.go
39 lines (29 loc) · 752 Bytes
/
png.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
package fastimage
import (
"bytes"
"encoding/binary"
"errors"
)
type imagePNG struct{}
func (p imagePNG) Type() ImageType {
return PNG
}
func (p imagePNG) Detect(buffer []byte) bool {
firstTwoBytes := buffer[:2]
return bytes.Equal(firstTwoBytes, []byte{0x89, 0x50})
}
func (p imagePNG) GetSize(buffer []byte) (*ImageSize, error) {
if len(buffer) < 25 {
return nil, errors.New("Insufficient data")
}
imageSize := ImageSize{}
slice := buffer[16 : 16+8]
widthBuffer := bytes.NewReader(slice[:4])
binary.Read(widthBuffer, binary.BigEndian, &imageSize.Width)
heightBuffer := bytes.NewReader(slice[4:8])
binary.Read(heightBuffer, binary.BigEndian, &imageSize.Height)
return &imageSize, nil
}
func init() {
register(&imagePNG{})
}