-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.go
108 lines (97 loc) · 1.98 KB
/
utils.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
package gosnap
import (
"bytes"
"image"
"image/color"
"image/png"
"math/big"
"golang.org/x/image/draw"
)
func grayScale(src image.Image, widthPx, heightPx int) *image.Gray {
rect := image.Rect(0, 0, widthPx, heightPx)
var gray = image.NewGray(rect)
draw.BiLinear.Scale(gray, rect, src, src.Bounds(), draw.Over, nil)
return gray
}
func grayToBigInt(img *image.Gray) *big.Int {
var (
x, y, n int
left, right uint8
r = img.Bounds()
hash = big.NewInt(0)
)
for y = r.Min.Y; y < r.Max.Y; y++ {
left = img.GrayAt(r.Min.X, y).Y
for x = r.Min.X + 1; x < r.Max.X; x++ {
right = img.GrayAt(x, y).Y
if right > left && right-left > 1 {
hash.SetBit(hash, n, 1)
}
n++
left = right
}
}
return hash
}
type Masked struct {
image.Image
Color color.Color
Rect image.Rectangle
}
func (t Masked) At(x, y int) color.Color {
if t.Rect.Min.X <= x && x < t.Rect.Max.X &&
t.Rect.Min.Y <= y && y < t.Rect.Max.Y {
return t.Color
}
return t.Image.At(x, y)
}
func encodePng(img image.Image) ([]byte, error) {
var buf bytes.Buffer
err := png.Encode(&buf, img)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func decodePng(body []byte) (image.Image, error) {
return png.Decode(bytes.NewReader(body))
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func overlay(a, b image.Image) image.Image {
var (
ab = a.Bounds()
bb = b.Bounds()
w, h = max(ab.Dx(), bb.Dx()), max(ab.Dy(), bb.Dy())
img = image.NewNRGBA(image.Rect(0, 0, w, h))
mag = color.NRGBA{R: 255, G: 0, B: 255, A: 255}
)
var (
c1, c2 color.Color
r1, g1, b1, r2, g2, b2 uint32
)
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
c1 = a.At(x, y)
c2 = b.At(x, y)
r1, g1, b1, _ = c1.RGBA()
r2, g2, b2, _ = c2.RGBA()
if (r1-r2 != 0) || (g1-g2 != 0) || (b1-b2 != 0) {
img.SetNRGBA(x, y, mag)
continue
}
img.Set(x, y, c1)
}
}
return img
}