/
png.go
58 lines (51 loc) · 1.13 KB
/
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package udwImage
import (
"bytes"
"github.com/tachyon-protocol/udw/udwFile"
"image"
"image/png"
"os"
)
func PngDecodeConfigFromFile(path string) (conf image.Config, err error) {
file, err := os.Open(path)
if err != nil {
return
}
defer file.Close()
return png.DecodeConfig(file)
}
func MustPngDecodeConfigFromFile(path string) (conf image.Config) {
conf, err := PngDecodeConfigFromFile(path)
if err != nil {
panic(err)
}
return conf
}
func MustPngDecodeConfigFromBytes(content []byte) (conf image.Config) {
buf := bytes.NewBuffer(content)
conf, err := png.DecodeConfig(buf)
if err != nil {
panic(err)
}
return conf
}
func MustPngEncodeFromGoImageToBytes(img image.Image) []byte {
buf := bytes.Buffer{}
err := png.Encode(&buf, img)
if err != nil {
panic(err)
}
return buf.Bytes()
}
func MustPngEncodeFromGoImageToFile(img image.Image, inPath string) {
content := MustPngEncodeFromGoImageToBytes(img)
udwFile.MustWriteFileWithMkdir(inPath, content)
}
func MustPngDecodeFromBuf(content []byte) image.Image {
buf := bytes.NewBuffer(content)
img, err := png.Decode(buf)
if err != nil {
panic(err)
}
return img
}