forked from saily/vnc2video
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoding_rre.go
109 lines (87 loc) · 2.05 KB
/
encoding_rre.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
package vnc2video
import (
"encoding/binary"
"image/draw"
"io"
//"image/draw"
)
type RREEncoding struct {
//Colors []Color
numSubRects uint32
backgroundColor []byte
subRectData []byte
Image draw.Image
}
func (*RREEncoding) Supported(Conn) bool {
return true
}
func (enc *RREEncoding) SetTargetImage(img draw.Image) {
enc.Image = img
}
func (enc *RREEncoding) Reset() error {
return nil
}
func (*RREEncoding) Type() EncodingType { return EncRRE }
func (enc *RREEncoding) Write(c Conn, rect *Rectangle) error {
return nil
}
func (z *RREEncoding) WriteTo(w io.Writer) (n int, err error) {
binary.Write(w, binary.BigEndian, z.numSubRects)
if err != nil {
return 0, err
}
w.Write(z.backgroundColor)
if err != nil {
return 0, err
}
w.Write(z.subRectData)
if err != nil {
return 0, err
}
b := len(z.backgroundColor) + len(z.subRectData) + 4
return b, nil
}
func (enc *RREEncoding) Read(r Conn, rect *Rectangle) error {
//func (z *RREEncoding) Read(pixelFmt *PixelFormat, rect *Rectangle, r io.Reader) (Encoding, error) {
pf := r.PixelFormat()
//bytesPerPixel := int(pf.BPP / 8)
var numOfSubrectangles uint32
if err := binary.Read(r, binary.BigEndian, &numOfSubrectangles); err != nil {
return err
}
var err error
enc.numSubRects = numOfSubrectangles
//read whole-rect background color
bgColor, err := ReadColor(r, &pf)
if err != nil {
return err
}
imgRect := MakeRectFromVncRect(rect)
FillRect(enc.Image, &imgRect, bgColor)
//read all individual rects (color=bytesPerPixel + x=16b + y=16b + w=16b + h=16b)
for i := 0; i < int(numOfSubrectangles); i++ {
color, err := ReadColor(r, &pf)
if err != nil {
return err
}
x, err := ReadUint16(r)
if err != nil {
return err
}
y, err := ReadUint16(r)
if err != nil {
return err
}
width, err := ReadUint16(r)
if err != nil {
return err
}
height, err := ReadUint16(r)
if err != nil {
return err
}
subRect := MakeRect(int(rect.X+x), int(rect.Y+y), int(width), int(height))
FillRect(enc.Image, &subRect, color)
}
return nil
}