-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcanvas.go
53 lines (41 loc) · 1.11 KB
/
canvas.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
package main
import (
"image"
"sync"
)
type Canvas struct {
Width int
Height int
Data []byte
Mutex *sync.Mutex
}
func (canvas *Canvas) FromImage(img *image.Image) {
canvas.Width = (*img).Bounds().Dx()
canvas.Height = (*img).Bounds().Dy()
canvas.Mutex.Lock()
defer canvas.Mutex.Unlock()
canvas.Data = make([]byte, canvas.Width*canvas.Height*3)
for y := 0; y < canvas.Height; y++ {
for x := 0; x < canvas.Width; x++ {
r, g, b, _ := (*img).At(x, y).RGBA()
r = r >> 8
g = g >> 8
b = b >> 8
canvas.Data[(y*canvas.Width+x)*3] = uint8(r)
canvas.Data[(y*canvas.Width+x)*3+1] = uint8(g)
canvas.Data[(y*canvas.Width+x)*3+2] = uint8(b)
}
}
}
func (canvas *Canvas) At(x, y int) (uint8, uint8, uint8) {
canvas.Mutex.Lock()
defer canvas.Mutex.Unlock()
return canvas.Data[(y*canvas.Width+x)*3], canvas.Data[(y*canvas.Width+x)*3+1], canvas.Data[(y*canvas.Width+x)*3+2]
}
func (canvas *Canvas) Set(x, y int, r, g, b uint8) {
canvas.Mutex.Lock()
defer canvas.Mutex.Unlock()
canvas.Data[(y*canvas.Width+x)*3] = r
canvas.Data[(y*canvas.Width+x)*3+1] = g
canvas.Data[(y*canvas.Width+x)*3+2] = b
}