This repository has been archived by the owner on Jan 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverter.go
82 lines (65 loc) · 1.46 KB
/
converter.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
package uitest
import (
"errors"
"image"
"image/color"
"sync"
"github.com/mitchellh/go-vnc"
)
var (
// Use this map to add other encodings that the converter can understand
TypeColors = make(map[int32]func(vnc.Encoding) []vnc.Color)
// I don't know this encoding.
ErrUnknownEncoding = errors.New("uitest: unknown encoding")
)
func init() {
rawencode := &vnc.RawEncoding{}
TypeColors[rawencode.Type()] = func(v vnc.Encoding) []vnc.Color {
r := v.(*vnc.RawEncoding)
return r.Colors
}
}
type Converter struct {
sync.Mutex
conn *vnc.ClientConn
lastimg *image.NRGBA64
}
func NewConverter(conn *vnc.ClientConn) *Converter {
return &Converter{
conn: conn,
lastimg: image.NewNRGBA64(image.Rect(0, 0, int(conn.FrameBufferWidth), int(conn.FrameBufferHeight))),
}
}
func (c *Converter) Process(m *vnc.FramebufferUpdateMessage) error {
c.Lock()
defer c.Unlock()
for _, v := range m.Rectangles {
f, ok := TypeColors[v.Enc.Type()]
if !ok {
return ErrUnknownEncoding
}
colors := f(v.Enc)
for y := v.Y; y < v.Height; y++ {
for x := v.X; x < v.Width; x++ {
vcolor := colors[x+y]
c.lastimg.SetNRGBA64(int(x), int(y), color.NRGBA64{
R: vcolor.R,
G: vcolor.G,
B: vcolor.B,
A: 255,
})
}
}
}
return nil
}
func (c *Converter) Image() image.Image {
c.Lock()
defer c.Unlock()
// copy the image
copy := image.NewNRGBA64(c.lastimg.Rect)
for k, v := range c.lastimg.Pix {
copy.Pix[k] = v
}
return copy
}