forked from saily/vnc2video
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoding_zrle.go
360 lines (307 loc) · 8.73 KB
/
encoding_zrle.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package vnc2video
import (
"bytes"
"compress/zlib"
"errors"
"image/color"
"image/draw"
"io"
"vnc2video/logger"
)
type ZRLEEncoding struct {
bytes []byte
Image draw.Image
unzipper io.Reader
zippedBuff *bytes.Buffer
}
func (*ZRLEEncoding) Supported(Conn) bool {
return true
}
func (enc *ZRLEEncoding) SetTargetImage(img draw.Image) {
enc.Image = img
}
func (enc *ZRLEEncoding) Reset() error {
enc.unzipper = nil
return nil
}
func (*ZRLEEncoding) Type() EncodingType { return EncZRLE }
func (z *ZRLEEncoding) WriteTo(w io.Writer) (n int, err error) {
return w.Write(z.bytes)
}
func (enc *ZRLEEncoding) Write(c Conn, rect *Rectangle) error {
return nil
}
func IsCPixelSpecific(pf *PixelFormat) bool {
significant := int(pf.RedMax<<pf.RedShift | pf.GreenMax<<pf.GreenShift | pf.BlueMax<<pf.BlueShift)
if pf.Depth <= 24 && 32 == pf.BPP && ((significant&0x00ff000000) == 0 || (significant&0x000000ff) == 0) {
return true
}
return false
}
func CalcBytesPerCPixel(pf *PixelFormat) int {
if IsCPixelSpecific(pf) {
return 3
}
return int(pf.BPP / 8)
}
func (enc *ZRLEEncoding) Read(r Conn, rect *Rectangle) error {
logger.Tracef("reading ZRLE:%v\n", rect)
len, err := ReadUint32(r)
if err != nil {
return err
}
b, err := ReadBytes(int(len), r)
if err != nil {
return err
}
bytesBuff := bytes.NewBuffer(b)
if enc.unzipper == nil {
enc.unzipper, err = zlib.NewReader(bytesBuff)
enc.zippedBuff = bytesBuff
if err != nil {
return err
}
} else {
enc.zippedBuff.Write(b)
}
pf := r.PixelFormat()
enc.renderZRLE(rect, &pf)
return nil
}
func (enc *ZRLEEncoding) readZRLERaw(reader io.Reader, pf *PixelFormat, tx, ty, tw, th int) error {
for y := 0; y < int(th); y++ {
for x := 0; x < int(tw); x++ {
col, err := readCPixel(reader, pf)
if err != nil {
return err
}
enc.Image.Set(tx+x, ty+y, col)
}
}
return nil
}
func (enc *ZRLEEncoding) renderZRLE(rect *Rectangle, pf *PixelFormat) error {
logger.Trace("-----renderZRLE: rendering rect:", rect)
for tileOffsetY := 0; tileOffsetY < int(rect.Height); tileOffsetY += 64 {
tileHeight := Min(64, int(rect.Height)-tileOffsetY)
for tileOffsetX := 0; tileOffsetX < int(rect.Width); tileOffsetX += 64 {
tileWidth := Min(64, int(rect.Width)-tileOffsetX)
// read subencoding
subEnc, err := ReadUint8(enc.unzipper)
logger.Tracef("-----renderZRLE: rendering got tile:(%d,%d) w:%d, h:%d subEnc:%d", tileOffsetX, tileOffsetY, tileWidth, tileHeight, subEnc)
if err != nil {
logger.Errorf("renderZRLE: error while reading subencoding: %v", err)
return err
}
switch {
case subEnc == 0:
// Raw subencoding: read cpixels and paint
err = enc.readZRLERaw(enc.unzipper, pf, int(rect.X)+tileOffsetX, int(rect.Y)+tileOffsetY, tileWidth, tileHeight)
if err != nil {
logger.Errorf("renderZRLE: error while reading Raw tile: %v", err)
return err
}
case subEnc == 1:
// background color tile - just fill
color, err := readCPixel(enc.unzipper, pf)
if err != nil {
logger.Errorf("renderZRLE: error while reading CPixel for bgColor tile: %v", err)
return err
}
myRect := MakeRect(int(rect.X)+tileOffsetX, int(rect.Y)+tileOffsetY, tileWidth, tileHeight)
FillRect(enc.Image, &myRect, color)
case subEnc >= 2 && subEnc <= 16:
err = enc.handlePaletteTile(tileOffsetX, tileOffsetY, tileWidth, tileHeight, subEnc, pf, rect)
if err != nil {
return err
}
case subEnc == 128:
err = enc.handlePlainRLETile(tileOffsetX, tileOffsetY, tileWidth, tileHeight, pf, rect)
if err != nil {
return err
}
case subEnc >= 130 && subEnc <= 255:
err = enc.handlePaletteRLETile(tileOffsetX, tileOffsetY, tileWidth, tileHeight, subEnc, pf, rect)
if err != nil {
return err
}
default:
logger.Errorf("Unknown ZRLE subencoding: %v", subEnc)
}
}
}
return nil
}
func (enc *ZRLEEncoding) handlePaletteRLETile(tileOffsetX, tileOffsetY, tileWidth, tileHeight int, subEnc uint8, pf *PixelFormat, rect *Rectangle) error {
// Palette RLE
paletteSize := subEnc - 128
palette := make([]*color.RGBA, paletteSize)
var err error
// Read RLE palette
for j := 0; j < int(paletteSize); j++ {
palette[j], err = readCPixel(enc.unzipper, pf)
if err != nil {
logger.Errorf("renderZRLE: error while reading color in palette RLE subencoding: %v", err)
return err
}
}
var index uint8
runLen := 0
for y := 0; y < tileHeight; y++ {
for x := 0; x < tileWidth; x++ {
if runLen == 0 {
// Read length and index
index, err = ReadUint8(enc.unzipper)
if err != nil {
logger.Errorf("renderZRLE: error while reading length and index in palette RLE subencoding: %v", err)
//return err
}
runLen = 1
// Run is represented by index | 0x80
// Otherwise, single pixel
if (index & 0x80) != 0 {
index -= 128
runLen, err = readRunLength(enc.unzipper)
if err != nil {
logger.Errorf("handlePlainRLETile: error while reading runlength in plain RLE subencoding: %v", err)
return err
}
}
//logger.Tracef("renderZRLE: writing pixel: col=%v times=%d", palette[index], runLen)
}
// Write pixel to image
enc.Image.Set(tileOffsetX+int(rect.X)+x, tileOffsetY+int(rect.Y)+y, palette[index])
runLen--
}
}
return nil
}
func (enc *ZRLEEncoding) handlePaletteTile(tileOffsetX, tileOffsetY, tileWidth, tileHeight int, subEnc uint8, pf *PixelFormat, rect *Rectangle) error {
//subenc here is also palette size
paletteSize := subEnc
palette := make([]*color.RGBA, paletteSize)
var err error
// Read palette
for j := 0; j < int(paletteSize); j++ {
palette[j], err = readCPixel(enc.unzipper, pf)
if err != nil {
logger.Errorf("renderZRLE: error while reading CPixel for palette tile: %v", err)
return err
}
}
// Calculate index size
var indexBits, mask uint32
if paletteSize == 2 {
indexBits = 1
mask = 0x80
} else if paletteSize <= 4 {
indexBits = 2
mask = 0xC0
} else {
indexBits = 4
mask = 0xF0
}
for y := 0; y < tileHeight; y++ {
// Packing only occurs per-row
bitsAvailable := uint32(0)
buffer := uint32(0)
for x := 0; x < tileWidth; x++ {
// Buffer more bits if necessary
if bitsAvailable == 0 {
bits, err := ReadUint8(enc.unzipper)
if err != nil {
logger.Errorf("renderZRLE: error while reading first uint8 into buffer: %v", err)
return err
}
buffer = uint32(bits)
bitsAvailable = 8
}
// Read next pixel
index := (buffer & mask) >> (8 - indexBits)
buffer <<= indexBits
bitsAvailable -= indexBits
// Write pixel to image
enc.Image.Set(tileOffsetX+int(rect.X)+x, tileOffsetY+int(rect.Y)+y, palette[index])
}
}
return err
}
func (enc *ZRLEEncoding) handlePlainRLETile(tileOffsetX int, tileOffsetY int, tileWidth int, tileHeight int, pf *PixelFormat, rect *Rectangle) error {
var col *color.RGBA
var err error
runLen := 0
for y := 0; y < tileHeight; y++ {
for x := 0; x < tileWidth; x++ {
if runLen == 0 {
// Read length and color
col, err = readCPixel(enc.unzipper, pf)
if err != nil {
logger.Errorf("handlePlainRLETile: error while reading CPixel in plain RLE subencoding: %v", err)
return err
}
runLen, err = readRunLength(enc.unzipper)
if err != nil {
logger.Errorf("handlePlainRLETile: error while reading runlength in plain RLE subencoding: %v", err)
return err
}
}
// Write pixel to image
enc.Image.Set(tileOffsetX+int(rect.X)+x, tileOffsetY+int(rect.Y)+y, col)
runLen--
}
}
return err
}
func readRunLength(r io.Reader) (int, error) {
runLen := 1
addition, err := ReadUint8(r)
if err != nil {
logger.Errorf("renderZRLE: error while reading addition to runLen in plain RLE subencoding: %v", err)
return 0, err
}
runLen += int(addition)
for addition == 255 {
addition, err = ReadUint8(r)
if err != nil {
logger.Errorf("renderZRLE: error while reading addition to runLen in-loop plain RLE subencoding: %v", err)
return 0, err
}
runLen += int(addition)
}
return runLen, nil
}
// Reads cpixel color from reader
func readCPixel(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")
}
isZRLEFormat := IsCPixelSpecific(pf)
var col *color.RGBA
if isZRLEFormat {
tbytes, err := ReadBytes(3, c)
if err != nil {
return nil, err
}
if pf.BigEndian != 1 {
col = &color.RGBA{
B: uint8(tbytes[0]),
G: uint8(tbytes[1]),
R: uint8(tbytes[2]),
A: uint8(1),
}
} else {
col = &color.RGBA{
R: uint8(tbytes[0]),
G: uint8(tbytes[1]),
B: uint8(tbytes[2]),
A: uint8(1),
}
}
return col, nil
}
col, err := ReadColor(c, pf)
if err != nil {
logger.Errorf("readCPixel: Error while reading zrle: %v", err)
}
return col, nil
}