forked from saily/vnc2video
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoding_util.go
244 lines (216 loc) · 5.84 KB
/
encoding_util.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package vnc2video
import (
"encoding/binary"
"errors"
"fmt"
"image"
"image/color"
"image/draw"
"io"
)
const (
BlockWidth = 16
BlockHeight = 16
)
type VncCanvas struct {
draw.Image
//DisplayBuff draw.Image
//WriteBuff draw.Image
imageBuffs [2]draw.Image
Cursor draw.Image
CursorMask [][]bool
CursorBackup draw.Image
CursorOffset *image.Point
CursorLocation *image.Point
DrawCursor bool
Changed map[string]bool
}
func NewVncCanvas(width, height int) *VncCanvas {
//dispImg := NewRGBImage(image.Rect(0, 0, width, height))
writeImg := NewRGBImage(image.Rect(0, 0, width, height))
canvas := VncCanvas{
Image: writeImg,
//DisplayBuff: dispImg,
//WriteBuff: writeImg,
}
return &canvas
}
func (c *VncCanvas) SetChanged(rect *Rectangle) {
if c.Changed == nil {
c.Changed = make(map[string]bool)
}
for x := int(rect.X) / BlockWidth; x*BlockWidth < int(rect.X+rect.Width); x++ {
for y := int(rect.Y) / BlockHeight; y*BlockHeight < int(rect.Y+rect.Height); y++ {
key := fmt.Sprintf("%d,%d", x, y)
//fmt.Println("setting block: ", key)
c.Changed[key] = true
}
}
}
func (c *VncCanvas) Reset(rect *Rectangle) {
c.Changed = nil
}
func (c *VncCanvas) RemoveCursor() image.Image {
if c.Cursor == nil || c.CursorLocation == nil {
return c.Image
}
if !c.DrawCursor {
return c.Image
}
rect := c.Cursor.Bounds()
loc := c.CursorLocation
img := c.Image
for y := rect.Min.Y; y < int(rect.Max.Y); y++ {
for x := rect.Min.X; x < int(rect.Max.X); x++ {
// offset := y*int(rect.Width) + x
// if bitmask[y*int(scanLine)+x/8]&(1<<uint(7-x%8)) > 0 {
col := c.CursorBackup.At(x, y)
//mask := c.CursorMask.At(x, y).(color.RGBA)
mask := c.CursorMask[x][y]
//logger.Info("Drawing Cursor: ", x, y, col, mask)
if mask {
//logger.Info("Drawing Cursor for real: ", x, y, col)
img.Set(x+loc.X-c.CursorOffset.X, y+loc.Y-c.CursorOffset.Y, col)
}
// //logger.Tracef("CursorPseudoEncoding.Read: setting pixel: (%d,%d) %v", x+int(rect.X), y+int(rect.Y), colors[offset])
// }
}
}
return img
}
// func (c *VncCanvas) SwapBuffers() {
// swapSpace := c.DisplayBuff
// c.DisplayBuff = c.WriteBuff
// c.WriteBuff = swapSpace
// c.Image = c.WriteBuff
// }
func (c *VncCanvas) PaintCursor() image.Image {
if c.Cursor == nil || c.CursorLocation == nil {
return c.Image
}
if !c.DrawCursor {
return c.Image
}
rect := c.Cursor.Bounds()
if c.CursorBackup == nil {
c.CursorBackup = image.NewRGBA(c.Cursor.Bounds())
}
loc := c.CursorLocation
img := c.Image
for y := rect.Min.Y; y < int(rect.Max.Y); y++ {
for x := rect.Min.X; x < int(rect.Max.X); x++ {
// offset := y*int(rect.Width) + x
// if bitmask[y*int(scanLine)+x/8]&(1<<uint(7-x%8)) > 0 {
col := c.Cursor.At(x, y)
//mask := c.CursorMask.At(x, y).(RGBColor)
mask := c.CursorMask[x][y]
backup := c.Image.At(x+loc.X-c.CursorOffset.X, y+loc.Y-c.CursorOffset.Y)
//c.CursorBackup.Set(x, y, backup)
//backup the previous data at this point
//logger.Info("Drawing Cursor: ", x, y, col, mask)
if mask {
c.CursorBackup.Set(x, y, backup)
//logger.Info("Drawing Cursor for real: ", x, y, col)
img.Set(x+loc.X-c.CursorOffset.X, y+loc.Y-c.CursorOffset.Y, col)
}
// //logger.Tracef("CursorPseudoEncoding.Read: setting pixel: (%d,%d) %v", x+int(rect.X), y+int(rect.Y), colors[offset])
// }
}
}
return img
}
func Min(a, b int) int {
if a < b {
return a
}
return b
}
func DrawImage(target draw.Image, imageToApply image.Image, pos image.Point) {
rect := imageToApply.Bounds()
for x := rect.Min.X; x < rect.Max.X; x++ {
for y := rect.Min.Y; y < rect.Max.Y; y++ {
target.Set(x+pos.X, y+pos.Y, imageToApply.At(x, y))
}
}
}
func FillRect(img draw.Image, rect *image.Rectangle, c color.Color) {
for x := rect.Min.X; x < rect.Max.X; x++ {
for y := rect.Min.Y; y < rect.Max.Y; y++ {
img.Set(x, y, c)
}
}
}
// Read unmarshal color from conn
func ReadColor(c io.Reader, pf *PixelFormat) (*color.RGBA, error) {
if pf.TrueColor == 0 {
return nil, errors.New("support for non true color formats was not implemented")
}
order := pf.order()
var pixel uint32
switch pf.BPP {
case 8:
var px uint8
if err := binary.Read(c, order, &px); err != nil {
return nil, err
}
pixel = uint32(px)
case 16:
var px uint16
if err := binary.Read(c, order, &px); err != nil {
return nil, err
}
pixel = uint32(px)
case 32:
var px uint32
if err := binary.Read(c, order, &px); err != nil {
return nil, err
}
pixel = uint32(px)
}
rgb := color.RGBA{
R: uint8((pixel >> pf.RedShift) & uint32(pf.RedMax)),
G: uint8((pixel >> pf.GreenShift) & uint32(pf.GreenMax)),
B: uint8((pixel >> pf.BlueShift) & uint32(pf.BlueMax)),
A: 1,
}
return &rgb, nil
}
func DecodeRaw(reader io.Reader, pf *PixelFormat, rect *Rectangle, targetImage draw.Image) error {
for y := 0; y < int(rect.Height); y++ {
for x := 0; x < int(rect.Width); x++ {
col, err := ReadColor(reader, pf)
if err != nil {
return err
}
targetImage.(draw.Image).Set(int(rect.X)+x, int(rect.Y)+y, col)
}
}
return nil
}
func ReadUint8(r io.Reader) (uint8, error) {
var myUint uint8
if err := binary.Read(r, binary.BigEndian, &myUint); err != nil {
return 0, err
}
return myUint, nil
}
func ReadUint16(r io.Reader) (uint16, error) {
var myUint uint16
if err := binary.Read(r, binary.BigEndian, &myUint); err != nil {
return 0, err
}
return myUint, nil
}
func ReadUint32(r io.Reader) (uint32, error) {
var myUint uint32
if err := binary.Read(r, binary.BigEndian, &myUint); err != nil {
return 0, err
}
return myUint, nil
}
func MakeRect(x, y, width, height int) image.Rectangle {
return image.Rectangle{Min: image.Point{X: x, Y: y}, Max: image.Point{X: x + width, Y: y + height}}
}
func MakeRectFromVncRect(rect *Rectangle) image.Rectangle {
return MakeRect(int(rect.X), int(rect.Y), int(rect.Width), int(rect.Height))
}