-
Notifications
You must be signed in to change notification settings - Fork 35
/
encoding_tight.go
229 lines (206 loc) · 5.78 KB
/
encoding_tight.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
package encodings
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"github.com/vprix/vncproxy/rfb"
)
var TightMinToCompress int = 12
const (
tightCompressionBasic = 0
tightCompressionFill = 0x08
tightCompressionJPEG = 0x09
tightCompressionPNG = 0x0A
)
const (
TightFilterCopy = 0
TightFilterPalette = 1
TightFilterGradient = 2
)
type TightEncoding struct {
buff *bytes.Buffer
}
func (that *TightEncoding) Supported(session rfb.ISession) bool {
return true
}
func (that *TightEncoding) Clone(data ...bool) rfb.IEncoding {
obj := &TightEncoding{}
if len(data) > 0 && data[0] {
if that.buff != nil {
obj.buff = &bytes.Buffer{}
_, _ = obj.buff.Write(that.buff.Bytes())
}
}
return obj
}
func (that *TightEncoding) Type() rfb.EncodingType {
return rfb.EncTight
}
func calcTightBytePerPixel(pf *rfb.PixelFormat) int {
bytesPerPixel := int(pf.BPP / 8)
var bytesPerPixelTight int
if 24 == pf.Depth && 32 == pf.BPP {
bytesPerPixelTight = 3
} else {
bytesPerPixelTight = bytesPerPixel
}
return bytesPerPixelTight
}
func (that *TightEncoding) Write(session rfb.ISession, rect *rfb.Rectangle) error {
if that.buff == nil {
return errors.New("ByteBuffer is nil")
}
_, err := that.buff.WriteTo(session)
that.buff.Reset()
return err
}
func (that *TightEncoding) Read(session rfb.ISession, rect *rfb.Rectangle) error {
if that.buff == nil {
that.buff = &bytes.Buffer{}
}
pf := session.Options().PixelFormat
bytesPixel := calcTightBytePerPixel(&pf)
compressionControl, err := ReadUint8(session)
if err != nil {
return nil
}
_ = binary.Write(that.buff, binary.BigEndian, compressionControl)
compType := compressionControl >> 4 & 0x0F
switch compType {
case tightCompressionFill: // 全部为紧凑压缩
bt, err := ReadBytes(bytesPixel, session)
if err != nil {
return err
}
_, _ = that.buff.Write(bt)
//jpeg紧凑压缩
case tightCompressionJPEG:
if pf.BPP == 8 {
return errors.New("Tight encoding: JPEG is not supported in 8 bpp mode. ")
}
// 获取jpeg流的长度
size, err := that.ReadCompactLen(session)
//读取jpeg流
jpegBytes, err := ReadBytes(size, session)
if err != nil {
return err
}
_, _ = that.buff.Write(jpegBytes)
default:
// 默认使用基础的压缩方式
if compType > tightCompressionJPEG {
return errors.New("Compression control byte is incorrect! ")
}
err = that.handleTightFilters(session, rect, &pf, compressionControl)
return err
}
return nil
}
// ReadCompactLen 获取动态长度
func (that *TightEncoding) ReadCompactLen(session rfb.ISession) (int, error) {
var err error
part, err := ReadUint8(session)
if err := binary.Write(that.buff, binary.BigEndian, part); err != nil {
return 0, err
}
size := uint32(part & 0x7F)
if (part & 0x80) == 0 {
return int(size), nil
}
part, err = ReadUint8(session)
if err := binary.Write(that.buff, binary.BigEndian, part); err != nil {
return 0, err
}
size |= uint32(int(part)&0x7F) << 7
if (part & 0x80) == 0 {
return int(size), nil
}
part, err = ReadUint8(session)
if err := binary.Write(that.buff, binary.BigEndian, part); err != nil {
return 0, err
}
size |= uint32(int(part)&0xFF) << 14
return int(size), err
}
// 基础压缩格式
func (that *TightEncoding) handleTightFilters(session rfb.ISession, rect *rfb.Rectangle, pf *rfb.PixelFormat, compCtl uint8) error {
var FilterIdMask uint8 = 0x40
var filterId uint8
var err error
if (compCtl & FilterIdMask) > 0 {
filterId, err = ReadUint8(session)
if err != nil {
return fmt.Errorf("error in handling tight encoding, reading filterid: %s", err.Error())
}
_ = binary.Write(that.buff, binary.BigEndian, filterId)
}
bytesPixel := calcTightBytePerPixel(pf)
lengthCurrentBPP := bytesPixel * int(rect.Width) * int(rect.Height)
switch filterId {
case TightFilterPalette:
palette, err := that.readTightPalette(session, bytesPixel)
if err != nil {
return err
}
var dataLength int
if palette == 2 {
dataLength = int(rect.Height) * ((int(rect.Width) + 7) / 8)
} else {
dataLength = int(rect.Width) * int(rect.Height)
}
err = that.ReadTightData(dataLength, session)
if err != nil {
return err
}
case TightFilterGradient:
err = that.ReadTightData(lengthCurrentBPP, session)
if err != nil {
return fmt.Errorf("handleTightFilters: error in handling tight encoding, Reading GRADIENT_FILTER: %v", err)
}
case TightFilterCopy:
err = that.ReadTightData(lengthCurrentBPP, session)
if err != nil {
return fmt.Errorf("handleTightFilters: error in handling tight encoding, Reading BASIC_FILTER: %v", err)
}
default:
return fmt.Errorf("handleTightFilters: Bad tight filter id: %d", filterId)
}
return nil
}
// 获取调色板数据
func (that *TightEncoding) readTightPalette(session rfb.ISession, bytesPixel int) (int, error) {
colorCount, err := ReadUint8(session)
if err != nil {
return 0, fmt.Errorf("handleTightFilters: error in handling tight encoding, reading TightFilterPalette: %v", err)
}
_ = binary.Write(that.buff, binary.BigEndian, colorCount)
// 注意这个地方,必须先转换为int类型,不然如果colorCount为255,+1的情况下会溢出,变成0,造成bug
numColors := int(colorCount) + 1
paletteSize := numColors * bytesPixel
paletteColorBytes, err := ReadBytes(paletteSize, session)
if err != nil {
return numColors, err
}
_, _ = that.buff.Write(paletteColorBytes)
return numColors, nil
}
func (that *TightEncoding) ReadTightData(dataSize int, session rfb.ISession) error {
if dataSize < TightMinToCompress {
b, err := ReadBytes(dataSize, session)
if err == nil {
_, _ = that.buff.Write(b)
}
return err
}
zlibDataLen, err := that.ReadCompactLen(session)
if err != nil {
return err
}
zippedBytes, err := ReadBytes(zlibDataLen, session)
if err != nil {
return err
}
_, _ = that.buff.Write(zippedBytes)
return nil
}