/
image.go
88 lines (80 loc) · 1.6 KB
/
image.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
package udwImage
import (
"bytes"
"image"
"image/color"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"os"
)
func MustDecodeConfigFromFile(path string) (conf image.Config) {
file, err := os.Open(path)
if err != nil {
panic(err)
}
defer file.Close()
conf, _, err = image.DecodeConfig(file)
if err != nil {
panic(err)
}
return conf
}
func MustDecodeImageFromFile(path string) (conf image.Image) {
file, err := os.Open(path)
if err != nil {
panic(err)
}
defer file.Close()
conf, _, err = image.Decode(file)
if err != nil {
panic(err)
}
return conf
}
func MustDecodeImageFromByte(content []byte) image.Image {
buf := bytes.NewBuffer(content)
img, _, err := image.Decode(buf)
if err != nil {
panic(err)
}
return img
}
type ImageConfig struct {
W int
H int
ImgExt string
}
func MustDecodeConfigFromByte(content []byte) (config ImageConfig) {
buf := bytes.NewBuffer(content)
imgConfig, imageType, err := image.DecodeConfig(buf)
if err != nil {
panic(err)
}
return ImageConfig{
W: imgConfig.Width,
H: imgConfig.Height,
ImgExt: imageType,
}
}
func DecodeConfigFromByte(content []byte) (config ImageConfig, err error) {
buf := bytes.NewBuffer(content)
imgConfig, imageType, err := image.DecodeConfig(buf)
if err != nil {
return config, err
}
return ImageConfig{
W: imgConfig.Width,
H: imgConfig.Height,
ImgExt: imageType,
}, nil
}
func GenerateOneColorImage(w int, h int, c color.Color) *image.RGBA {
img := image.NewRGBA(image.Rect(0, 0, w, h))
for x := 0; x < w; x++ {
for y := 0; y < h; y++ {
img.Set(x, y, c)
}
}
return img
}