forked from saily/vnc2video
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
203 lines (181 loc) · 4.99 KB
/
image.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
package vnc2video
import (
"encoding/binary"
"fmt"
"image"
"vnc2video/logger"
)
//var _ draw.Drawer = (*ServerConn)(nil)
//var _ draw.Image = (*ServerConn)(nil)
// Color represents a single color in a color map.
type Color struct {
pf *PixelFormat
cm *ColorMap
cmIndex uint32 // Only valid if pf.TrueColor is false.
R, G, B uint16
}
// ColorMap represent color map
type ColorMap [256]Color
// NewColor returns a new Color object
func NewColor(pf *PixelFormat, cm *ColorMap) *Color {
return &Color{
pf: pf,
cm: cm,
}
}
// Rectangle represents a rectangle of pixel data
type Rectangle struct {
X, Y uint16
Width, Height uint16
EncType EncodingType
Enc Encoding
}
// String return string representation
func (rect *Rectangle) String() string {
return fmt.Sprintf("rect x: %d, y: %d, width: %d, height: %d, enc: %s", rect.X, rect.Y, rect.Width, rect.Height, rect.EncType)
}
// NewRectangle returns new rectangle
func NewRectangle() *Rectangle {
return &Rectangle{}
}
// Write marshal color to conn
func (clr *Color) Write(c Conn) error {
var err error
pf := c.PixelFormat()
order := pf.order()
pixel := clr.cmIndex
if clr.pf.TrueColor != 0 {
pixel = uint32(clr.R) << pf.RedShift
pixel |= uint32(clr.G) << pf.GreenShift
pixel |= uint32(clr.B) << pf.BlueShift
}
switch pf.BPP {
case 8:
err = binary.Write(c, order, byte(pixel))
case 16:
err = binary.Write(c, order, uint16(pixel))
case 32:
err = binary.Write(c, order, uint32(pixel))
}
return err
}
// Read unmarshal color from conn
func (clr *Color) Read(c Conn) error {
order := clr.pf.order()
var pixel uint32
switch clr.pf.BPP {
case 8:
var px uint8
if err := binary.Read(c, order, &px); err != nil {
return err
}
pixel = uint32(px)
case 16:
var px uint16
if err := binary.Read(c, order, &px); err != nil {
return err
}
pixel = uint32(px)
case 32:
var px uint32
if err := binary.Read(c, order, &px); err != nil {
return err
}
pixel = uint32(px)
}
if clr.pf.TrueColor != 0 {
clr.R = uint16((pixel >> clr.pf.RedShift) & uint32(clr.pf.RedMax))
clr.G = uint16((pixel >> clr.pf.GreenShift) & uint32(clr.pf.GreenMax))
clr.B = uint16((pixel >> clr.pf.BlueShift) & uint32(clr.pf.BlueMax))
} else {
*clr = clr.cm[pixel]
clr.cmIndex = pixel
}
return nil
}
func colorsToImage(x, y, width, height uint16, colors []Color) *image.RGBA64 {
rect := image.Rect(int(x), int(y), int(x+width), int(y+height))
rgba := image.NewRGBA64(rect)
a := uint16(1)
for i, color := range colors {
rgba.Pix[4*i+0] = uint8(color.R >> 8)
rgba.Pix[4*i+1] = uint8(color.R)
rgba.Pix[4*i+2] = uint8(color.G >> 8)
rgba.Pix[4*i+3] = uint8(color.G)
rgba.Pix[4*i+4] = uint8(color.B >> 8)
rgba.Pix[4*i+5] = uint8(color.B)
rgba.Pix[4*i+6] = uint8(a >> 8)
rgba.Pix[4*i+7] = uint8(a)
}
return rgba
}
// Write marshal rectangle to conn
func (rect *Rectangle) Write(c Conn) error {
var err error
if err = binary.Write(c, binary.BigEndian, rect.X); err != nil {
return err
}
if err = binary.Write(c, binary.BigEndian, rect.Y); err != nil {
return err
}
if err = binary.Write(c, binary.BigEndian, rect.Width); err != nil {
return err
}
if err = binary.Write(c, binary.BigEndian, rect.Height); err != nil {
return err
}
if err = binary.Write(c, binary.BigEndian, rect.EncType); err != nil {
return err
}
return rect.Enc.Write(c, rect)
}
// Read unmarshal rectangle from conn
func (rect *Rectangle) Read(c Conn) error {
var err error
if err = binary.Read(c, binary.BigEndian, &rect.X); err != nil {
return err
}
if err = binary.Read(c, binary.BigEndian, &rect.Y); err != nil {
return err
}
if err = binary.Read(c, binary.BigEndian, &rect.Width); err != nil {
return err
}
if err = binary.Read(c, binary.BigEndian, &rect.Height); err != nil {
return err
}
if err = binary.Read(c, binary.BigEndian, &rect.EncType); err != nil {
return err
}
logger.Debug(rect)
switch rect.EncType {
// case EncCopyRect:
// rect.Enc = &CopyRectEncoding{}
// case EncTight:
// rect.Enc = c.GetEncInstance(rect.EncType)
// case EncTightPng:
// rect.Enc = &TightPngEncoding{}
// case EncRaw:
// if strings.HasPrefix(c.Protocol(), "aten") {
// rect.Enc = &AtenHermon{}
// } else {
// rect.Enc = &RawEncoding{}
// }
case EncDesktopSizePseudo:
rect.Enc = &DesktopSizePseudoEncoding{}
case EncDesktopNamePseudo:
rect.Enc = &DesktopNamePseudoEncoding{}
// case EncXCursorPseudo:
// rect.Enc = &XCursorPseudoEncoding{}
// case EncAtenHermon:
// rect.Enc = &AtenHermon{}
default:
rect.Enc = c.GetEncInstance(rect.EncType)
if rect.Enc == nil {
return fmt.Errorf("unsupported encoding %s", rect.EncType)
}
}
return rect.Enc.Read(c, rect)
}
// Area returns the total area in pixels of the Rectangle
func (rect *Rectangle) Area() int { return int(rect.Width) * int(rect.Height) }