-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathencoding_tight.go
131 lines (107 loc) · 2.65 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
package rfb
import (
"encoding/binary"
"fmt"
)
//go:generate stringer -type=TightCompression
type TightCompression uint8
const (
TightCompressionBasic TightCompression = 0
TightCompressionFill TightCompression = 8
TightCompressionJPEG TightCompression = 9
TightCompressionPNG TightCompression = 10
)
//go:generate stringer -type=TightFilter
type TightFilter uint8
const (
TightFilterCopy TightFilter = 0
TightFilterPalette TightFilter = 1
TightFilterGradient TightFilter = 2
)
type TightEncoding struct{}
func (*TightEncoding) Supported(Conn) bool {
return true
}
func (*TightEncoding) Type() EncodingType { return EncTight }
func (enc *TightEncoding) Write(c Conn, rect *Rectangle) error {
return nil
}
func (enc *TightEncoding) Read(c Conn, rect *Rectangle) error {
return nil
}
type TightCC struct {
Compression TightCompression
Filter TightFilter
}
func readTightCC(c Conn) (*TightCC, error) {
var ccb uint8 // compression control byte
if err := binary.Read(c, binary.BigEndian, &ccb); err != nil {
return nil, err
}
cmp := TightCompression(ccb >> 4)
switch cmp {
case TightCompressionBasic:
return &TightCC{TightCompressionBasic, TightFilterCopy}, nil
case TightCompressionFill:
return &TightCC{TightCompressionFill, TightFilterCopy}, nil
case TightCompressionPNG:
return &TightCC{TightCompressionPNG, TightFilterCopy}, nil
}
return nil, fmt.Errorf("unknown tight compression %d", cmp)
}
func writeTightCC(c Conn, tcc *TightCC) error {
var ccb uint8 // compression control byte
switch tcc.Compression {
case TightCompressionFill:
ccb = setBit(ccb, 7)
case TightCompressionJPEG:
ccb = setBit(ccb, 7)
ccb = setBit(ccb, 4)
case TightCompressionPNG:
ccb = setBit(ccb, 7)
ccb = setBit(ccb, 5)
}
return binary.Write(c, binary.BigEndian, ccb)
}
type TightPixel struct {
R uint8
G uint8
B uint8
}
func writeTightLength(c Conn, l int) error {
var buf []uint8
buf = append(buf, uint8(l&0x7F))
if l > 0x7F {
buf[0] |= 0x80
buf = append(buf, uint8((l>>7)&0x7F))
if l > 0x3FFF {
buf[1] |= 0x80
buf = append(buf, uint8((l>>14)&0xFF))
}
}
return binary.Write(c, binary.BigEndian, buf)
}
func readTightLength(c Conn) (int, error) {
var length int
var err error
var b uint8
if err = binary.Read(c, binary.BigEndian, &b); err != nil {
return 0, err
}
length = int(b) & 0x7F
if (b & 0x80) == 0 {
return length, nil
}
if err = binary.Read(c, binary.BigEndian, &b); err != nil {
return 0, err
}
length |= (int(b) & 0x7F) << 7
if (b & 0x80) == 0 {
return length, nil
}
if err = binary.Read(c, binary.BigEndian, &b); err != nil {
return 0, err
}
length |= (int(b) & 0xFF) << 14
return length, nil
}