-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
145 lines (121 loc) · 2.41 KB
/
main.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"encoding/json"
"fmt"
"image"
"image/color"
"image/png"
"os"
"path/filepath"
)
type Data struct {
Type string `json:"type"`
Width int `json:"width"`
Height int `json:"height"`
RGBA []RGBA `json:"rgba"`
}
type RGBA struct {
R uint32 `json:"r"`
G uint32 `json:"g"`
B uint32 `json:"b"`
A uint32 `json:"a"`
}
// Import PNG file
func Import(path string) (*os.File, error) {
// path
path, err := filepath.Abs(path)
if err != nil {
return nil, err
}
// if file is not png
if filepath.Ext(path) != ".png" {
return nil, fmt.Errorf("file is not png")
}
// import image
file, err := os.Open(path)
if err != nil {
return nil, err
}
return file, nil
}
// Encode PNG file to Data
func Encode(file *os.File) (Data, error) {
file.Seek(0, 0)
// Decode image
img, err := png.Decode(file)
if err != nil {
return Data{}, err
}
defer file.Close()
// data (image.Image) to rgb pixels
width, height := img.Bounds().Max.X, img.Bounds().Max.Y
data := &Data{
Type: "image/png",
Width: width,
Height: height,
RGBA: nil,
}
// Get pixels
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
r, g, b, a := img.At(x, y).RGBA()
data.RGBA = append(data.RGBA, RGBA{
R: r,
G: g,
B: b,
A: a,
})
}
}
return *data, nil
}
// Save PNG file
func (data *Data) Save(path string) error {
// Create new image from rgb pixels
newImage := image.NewRGBA(image.Rect(0, 0, data.Width, data.Height))
for y := 0; y < data.Height; y++ {
for x := 0; x < data.Width; x++ {
c := color.RGBA{
R: uint8(data.RGBA[y*data.Width+x].R),
G: uint8(data.RGBA[y*data.Width+x].G),
B: uint8(data.RGBA[y*data.Width+x].B),
A: uint8(data.RGBA[y*data.Width+x].A),
}
newImage.Set(x, y, c)
}
}
// absoulte path
path, err := filepath.Abs(path)
if err != nil {
return err
}
// *image.RGBA to png file
out, err := os.Create(path)
if err != nil {
return err
}
defer out.Close()
err = png.Encode(out, newImage)
if err != nil {
return err
}
return nil
}
// Json encode Data to json
func (data *Data) Json() (string, error) {
// return Data as json
jsonData, err := json.Marshal(data)
if err != nil {
return "", err
}
return string(jsonData), nil
}
// Json data to PNG file
func (data *Data) DecodeJson(jsonData string) error {
// json to Data
err := json.Unmarshal([]byte(jsonData), data)
if err != nil {
return err
}
return nil
}