-
Notifications
You must be signed in to change notification settings - Fork 8
/
utility.go
276 lines (236 loc) · 6.7 KB
/
utility.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* D3pixelbot - Custom client, recorder and bot for pixel drawing games
Copyright (C) 2019 David Vogel
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
package main
import (
"bytes"
"encoding/json"
"fmt"
"image"
"image/color"
"io/ioutil"
"net/http"
"time"
)
var myClient = &http.Client{Timeout: 10 * time.Second}
func getJSON(url string, target interface{}) error {
r, err := myClient.Get(url)
if err != nil {
return err
}
defer r.Body.Close()
return json.NewDecoder(r.Body).Decode(target)
}
func postJSON(url string, origin string, structure interface{}) (statusCode int, headers http.Header, bodyString []byte, err error) {
jsonStr, err := json.Marshal(structure)
if err != nil {
return 0, nil, nil, err
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Origin", origin)
resp, err := myClient.Do(req)
if err != nil {
return 0, nil, nil, err
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
return resp.StatusCode, resp.Header, body, nil
}
// Integer division that rounds to the next integer towards negative infinity
func divideFloor(a, b int) int {
temp := a / b
if ((a ^ b) < 0) && (a%b != 0) {
return temp - 1
}
return temp
}
// Integer division that rounds to the next integer towards positive infinity
func divideCeil(a, b int) int {
temp := a / b
if ((a ^ b) >= 0) && (a%b != 0) {
return temp + 1
}
return temp
}
// Returns if two palettes are equal
func isPaletteEqual(pal1, pal2 color.Palette) bool {
if len(pal1) != len(pal2) {
return false
}
for k, col := range pal1 {
r1, g1, b1, a1 := col.RGBA()
r2, g2, b2, a2 := pal2[k].RGBA()
if r1 != r2 || g1 != g2 || b1 != b2 || a1 != a2 {
return false
}
}
return true
}
// Creates a copy of an image
func copyImage(img image.Image) (image.Image, error) {
switch img := img.(type) {
case *image.RGBA:
imgCopy := &image.RGBA{
Pix: make([]uint8, len(img.Pix)),
Stride: img.Stride,
Rect: img.Rect,
}
copy(imgCopy.Pix, img.Pix)
return imgCopy, nil
case *image.Paletted:
imgCopy := &image.Paletted{
Pix: make([]uint8, len(img.Pix)),
Stride: img.Stride,
Rect: img.Rect,
Palette: make(color.Palette, len(img.Palette)),
}
copy(imgCopy.Pix, img.Pix)
copy(imgCopy.Palette, img.Palette)
return imgCopy, nil
case *image.Rectangle:
imgCopy := &image.Rectangle{
Min: img.Min,
Max: img.Max,
}
return imgCopy, nil
}
return nil, fmt.Errorf("Incompatible image type %T", img)
}
// Creates a copy of an image, without copying data outside that image rectangle.
// This is useful to reduce the memory footprint of subimages.
func copyImageReduced(img image.Image) (image.Image, error) {
switch img := img.(type) {
case *image.RGBA:
rect := img.Rect
stride := rect.Dx() * 4
imgCopy := &image.RGBA{
Pix: make([]uint8, rect.Dx()*stride),
Stride: stride,
Rect: rect,
}
for iy := 0; iy < rect.Dy(); iy++ {
copy(imgCopy.Pix[iy*stride:iy*stride+stride], img.Pix[iy*img.Stride:iy*img.Stride+stride])
}
return imgCopy, nil
case *image.Paletted:
rect := img.Rect
stride := rect.Dx()
imgCopy := &image.Paletted{
Pix: make([]uint8, rect.Dx()*stride),
Stride: stride,
Rect: rect,
Palette: make(color.Palette, len(img.Palette)),
}
for iy := 0; iy < rect.Dy(); iy++ {
copy(imgCopy.Pix[iy*stride:iy*stride+stride], img.Pix[iy*img.Stride:iy*img.Stride+stride])
}
copy(imgCopy.Palette, img.Palette)
return imgCopy, nil
}
return nil, fmt.Errorf("Incompatible image type %T", img)
}
// Returns the part of the image that is seen by rect.
// Pixels are shared between the original and sub image.
func subImage(img image.Image, rect image.Rectangle) (image.Image, error) {
switch img := img.(type) {
case *image.RGBA:
return img.SubImage(rect), nil
case *image.Paletted:
return img.SubImage(rect), nil
}
return nil, fmt.Errorf("Incompatible image type %T", img)
}
// Returns whether two images are the same.
//
// Will return false in the following cases, even if the image data looks the same from the outside:
// - Image types differ
// - Palettes differ
// - Image type not supported (Only Paletted and RGBA yet)
//
// Can compare:
// - Subimages
func compareImages(a, b image.Image) bool {
switch a := a.(type) {
case *image.Paletted:
b, ok := b.(*image.Paletted)
if !ok {
return false
}
if !a.Rect.Eq(b.Rect) {
return false
}
if !isPaletteEqual(a.Palette, b.Palette) {
return false
}
aStride, bStride := a.Stride, b.Stride
width := a.Rect.Dx() // = b.Rect.Dx()
for iy := 0; iy < a.Rect.Dy(); iy++ {
// Compare line by line and respect the stride
if !bytes.Equal(a.Pix[iy*aStride:iy*aStride+width], b.Pix[iy*bStride:iy*bStride+width]) {
return false
}
}
return true
case *image.RGBA:
b, ok := b.(*image.RGBA)
if !ok {
return false
}
if !a.Rect.Eq(b.Rect) {
return false
}
aStride, bStride := a.Stride, b.Stride
width := a.Rect.Dx() * 4 // = b.Rect.Dx() * 4
for iy := 0; iy < a.Rect.Dy(); iy++ {
// Compare line by line and respect the stride
if !bytes.Equal(a.Pix[iy*aStride:iy*aStride+width], b.Pix[iy*bStride:iy*bStride+width]) {
return false
}
}
return true
}
return false
}
// Converts any image to an BGRA array
func imageToBGRAArray(img image.Image) []byte {
rect := img.Bounds()
switch img := img.(type) {
case *image.RGBA:
array := make([]uint8, rect.Dx()*rect.Dy()*4)
stride := rect.Dx() * 4
for iy := 0; iy < rect.Dy(); iy++ {
copy(array[iy*stride:iy*stride+stride], img.Pix[iy*img.Stride:iy*img.Stride+stride])
for i := iy * stride; i <= iy*stride+stride-4; i += 4 {
array[i], array[i+2] = array[i+2], array[i] // Swap R and B
}
}
return array
default:
array := make([]byte, rect.Dx()*rect.Dy()*4)
i := 0
for iy := rect.Min.Y; iy < rect.Max.Y; iy++ {
for ix := rect.Min.X; ix < rect.Max.X; ix++ {
r, g, b, a := img.At(ix, iy).RGBA() // Returns 16 bit per channel
array[i] = byte(b >> 8)
i++
array[i] = byte(g >> 8)
i++
array[i] = byte(r >> 8)
i++
array[i] = byte(a >> 8)
i++
}
}
return array
}
}