-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutility.go
54 lines (44 loc) · 844 Bytes
/
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
package extractor
import (
"image"
"io"
)
type pixel struct {
R int
G int
B int
A int
}
func getPixels(file io.Reader, quality int) ([]pixel, error) {
img, _, err := image.Decode(file)
if err != nil {
return nil, err
}
bounds := img.Bounds()
width, height := bounds.Max.X, bounds.Max.Y
var pixels []pixel
for i := 0; i < width*height; i += quality {
pixel := rgbaToPixel(img.At(i%width, i/width).RGBA())
if pixel.A >= 125 {
if !(pixel.R > 250 && pixel.G > 250 && pixel.B > 250) {
pixels = append(pixels, pixel)
}
}
}
return pixels, nil
}
func rgbaToPixel(r uint32, g uint32, b uint32, a uint32) pixel {
return pixel{int(r / 257), int(g / 257), int(b / 257), int(a / 257)}
}
func min(x, y int) int {
if x < y {
return x
}
return y
}
func max(x, y int) int {
if x > y {
return x
}
return y
}