2018-07-27 16:55:06 +03:00
|
|
|
package rfb
|
2017-06-12 14:11:23 +03:00
|
|
|
|
|
|
|
import (
|
2017-06-13 01:52:07 +03:00
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
2017-06-12 14:11:23 +03:00
|
|
|
"image"
|
2017-07-04 14:24:41 +03:00
|
|
|
"strings"
|
2017-06-12 14:11:23 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
//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
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// ColorMap represent color map
|
2017-06-12 14:11:23 +03:00
|
|
|
type ColorMap [256]Color
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// NewColor returns a new Color object
|
2017-06-12 14:11:23 +03:00
|
|
|
func NewColor(pf *PixelFormat, cm *ColorMap) *Color {
|
|
|
|
return &Color{
|
|
|
|
pf: pf,
|
|
|
|
cm: cm,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Rectangle represents a rectangle of pixel data
|
2017-06-12 14:11:23 +03:00
|
|
|
type Rectangle struct {
|
|
|
|
X, Y uint16
|
|
|
|
Width, Height uint16
|
2017-06-13 01:52:07 +03:00
|
|
|
EncType EncodingType
|
2017-06-12 14:11:23 +03:00
|
|
|
Enc Encoding
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// String return string representation
|
2017-06-30 16:13:01 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// NewRectangle returns new rectangle
|
2017-06-13 01:52:07 +03:00
|
|
|
func NewRectangle() *Rectangle {
|
|
|
|
return &Rectangle{}
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Write marshal color to conn
|
2017-06-20 12:32:09 +03:00
|
|
|
func (clr *Color) Write(c Conn) error {
|
|
|
|
var err error
|
2017-07-14 02:01:24 +03:00
|
|
|
pf := c.PixelFormat()
|
|
|
|
order := pf.order()
|
2017-06-20 12:32:09 +03:00
|
|
|
pixel := clr.cmIndex
|
2017-07-10 23:20:35 +03:00
|
|
|
if clr.pf.TrueColor != 0 {
|
2017-07-14 02:01:24 +03:00
|
|
|
pixel = uint32(clr.R) << pf.RedShift
|
|
|
|
pixel |= uint32(clr.G) << pf.GreenShift
|
|
|
|
pixel |= uint32(clr.B) << pf.BlueShift
|
2017-06-20 12:32:09 +03:00
|
|
|
}
|
|
|
|
|
2017-07-14 02:01:24 +03:00
|
|
|
switch pf.BPP {
|
2017-06-20 12:32:09 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Read unmarshal color from conn
|
2017-06-20 12:32:09 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-07-10 23:20:35 +03:00
|
|
|
if clr.pf.TrueColor != 0 {
|
2017-06-20 12:32:09 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-06-12 14:11:23 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Write marshal rectangle to conn
|
|
|
|
func (rect *Rectangle) Write(c Conn) error {
|
2017-06-13 16:20:35 +03:00
|
|
|
var err error
|
2017-07-04 01:36:10 +03:00
|
|
|
|
|
|
|
if err = binary.Write(c, binary.BigEndian, rect.X); err != nil {
|
2017-06-13 01:52:07 +03:00
|
|
|
return err
|
|
|
|
}
|
2017-07-04 01:36:10 +03:00
|
|
|
if err = binary.Write(c, binary.BigEndian, rect.Y); err != nil {
|
2017-06-13 01:52:07 +03:00
|
|
|
return err
|
|
|
|
}
|
2017-07-04 01:36:10 +03:00
|
|
|
if err = binary.Write(c, binary.BigEndian, rect.Width); err != nil {
|
2017-06-13 01:52:07 +03:00
|
|
|
return err
|
|
|
|
}
|
2017-07-04 01:36:10 +03:00
|
|
|
if err = binary.Write(c, binary.BigEndian, rect.Height); err != nil {
|
2017-06-13 01:52:07 +03:00
|
|
|
return err
|
|
|
|
}
|
2017-07-04 01:36:10 +03:00
|
|
|
if err = binary.Write(c, binary.BigEndian, rect.EncType); err != nil {
|
2017-06-13 01:52:07 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
return rect.Enc.Write(c, rect)
|
2017-06-12 14:11:23 +03:00
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Read unmarshal rectangle from conn
|
|
|
|
func (rect *Rectangle) Read(c Conn) error {
|
2017-06-13 01:52:07 +03:00
|
|
|
var err error
|
2017-07-04 01:36:10 +03:00
|
|
|
|
|
|
|
if err = binary.Read(c, binary.BigEndian, &rect.X); err != nil {
|
2017-06-13 01:52:07 +03:00
|
|
|
return err
|
|
|
|
}
|
2017-07-04 01:36:10 +03:00
|
|
|
if err = binary.Read(c, binary.BigEndian, &rect.Y); err != nil {
|
2017-06-13 01:52:07 +03:00
|
|
|
return err
|
|
|
|
}
|
2017-07-04 01:36:10 +03:00
|
|
|
if err = binary.Read(c, binary.BigEndian, &rect.Width); err != nil {
|
2017-06-13 01:52:07 +03:00
|
|
|
return err
|
|
|
|
}
|
2017-07-04 01:36:10 +03:00
|
|
|
if err = binary.Read(c, binary.BigEndian, &rect.Height); err != nil {
|
2017-06-13 01:52:07 +03:00
|
|
|
return err
|
|
|
|
}
|
2017-07-04 01:36:10 +03:00
|
|
|
if err = binary.Read(c, binary.BigEndian, &rect.EncType); err != nil {
|
2017-06-13 01:52:07 +03:00
|
|
|
return err
|
|
|
|
}
|
2017-07-04 01:36:10 +03:00
|
|
|
|
|
|
|
switch rect.EncType {
|
2017-06-18 19:42:41 +03:00
|
|
|
case EncCopyRect:
|
2017-07-04 01:36:10 +03:00
|
|
|
rect.Enc = &CopyRectEncoding{}
|
2017-06-18 19:42:41 +03:00
|
|
|
case EncTight:
|
2017-07-04 01:36:10 +03:00
|
|
|
rect.Enc = &TightEncoding{}
|
2017-06-18 19:42:41 +03:00
|
|
|
case EncTightPng:
|
2017-07-04 01:36:10 +03:00
|
|
|
rect.Enc = &TightPngEncoding{}
|
2017-06-13 01:52:07 +03:00
|
|
|
case EncRaw:
|
2017-07-04 14:24:41 +03:00
|
|
|
if strings.HasPrefix(c.Protocol(), "aten") {
|
2017-07-04 01:36:10 +03:00
|
|
|
rect.Enc = &AtenHermon{}
|
2017-06-30 16:13:01 +03:00
|
|
|
} else {
|
2017-07-04 01:36:10 +03:00
|
|
|
rect.Enc = &RawEncoding{}
|
2017-06-30 16:13:01 +03:00
|
|
|
}
|
2017-06-30 00:39:52 +03:00
|
|
|
case EncDesktopSizePseudo:
|
2017-07-04 01:36:10 +03:00
|
|
|
rect.Enc = &DesktopSizePseudoEncoding{}
|
2017-06-30 00:39:52 +03:00
|
|
|
case EncDesktopNamePseudo:
|
2017-07-04 01:36:10 +03:00
|
|
|
rect.Enc = &DesktopNamePseudoEncoding{}
|
2017-06-30 00:39:52 +03:00
|
|
|
case EncXCursorPseudo:
|
2017-07-04 01:36:10 +03:00
|
|
|
rect.Enc = &XCursorPseudoEncoding{}
|
2017-06-30 16:13:01 +03:00
|
|
|
case EncAtenHermon:
|
2017-07-04 01:36:10 +03:00
|
|
|
rect.Enc = &AtenHermon{}
|
2017-06-18 19:42:41 +03:00
|
|
|
default:
|
2017-07-04 01:36:10 +03:00
|
|
|
return fmt.Errorf("unsupported encoding %s", rect.EncType)
|
2017-06-13 01:52:07 +03:00
|
|
|
}
|
2017-07-04 01:36:10 +03:00
|
|
|
|
|
|
|
return rect.Enc.Read(c, rect)
|
2017-06-12 14:11:23 +03:00
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Area returns the total area in pixels of the Rectangle
|
|
|
|
func (rect *Rectangle) Area() int { return int(rect.Width) * int(rect.Height) }
|