From b8582916814ed18aaaaea8e258ec574fcc1e7cd4 Mon Sep 17 00:00:00 2001 From: amit bezalel Date: Sat, 20 Jan 2018 12:08:17 +0200 Subject: [PATCH] added zlib encoding (tested) --- encoding_copyrect.go | 17 +++++++++++- encoding_raw.go | 32 ++++------------------ encoding_util.go | 15 +++++++++++ encoding_zlib.go | 60 ++++++++++++++++++++++++++++++++---------- example/client/main.go | 10 +++++-- 5 files changed, 90 insertions(+), 44 deletions(-) diff --git a/encoding_copyrect.go b/encoding_copyrect.go index 0179611..0d960a5 100644 --- a/encoding_copyrect.go +++ b/encoding_copyrect.go @@ -1,9 +1,14 @@ package vnc2video -import "encoding/binary" +import ( + "encoding/binary" + "image" + "image/draw" +) type CopyRectEncoding struct { SX, SY uint16 + Image draw.Image } func (*CopyRectEncoding) Supported(Conn) bool { @@ -21,6 +26,16 @@ func (enc *CopyRectEncoding) Read(c Conn, rect *Rectangle) error { if err := binary.Read(c, binary.BigEndian, &enc.SY); err != nil { return err } + cpyIm := image.NewRGBA(image.Rectangle{Min: image.Point{0, 0}, Max: image.Point{int(rect.Width), int(rect.Height)}}) + for x := 0; x < int(rect.Width); x++ { + for y := 0; x < int(rect.Height); y++ { + col := enc.Image.At(x+int(enc.SX), y+int(enc.SY)) + cpyIm.Set(x, y, col) + } + } + + draw.Draw(enc.Image, enc.Image.Bounds(), cpyIm, image.Point{int(rect.X), int(rect.Y)}, draw.Src) + return nil } diff --git a/encoding_raw.go b/encoding_raw.go index a1613d4..0191f7e 100644 --- a/encoding_raw.go +++ b/encoding_raw.go @@ -1,11 +1,11 @@ package vnc2video -import "image" -import "image/draw" -import "image/color" +import ( + "image/draw" +) type RawEncoding struct { - Image image.Image + Image draw.Image //Colors []Color } @@ -19,12 +19,6 @@ func (*RawEncoding) Reset() error { func (enc *RawEncoding) Write(c Conn, rect *Rectangle) error { var err error - // - //for _, clr := range enc.Colors { - // if err = clr.Write(c); err != nil { - // return err - // } - //} return err } @@ -35,25 +29,9 @@ func (enc *RawEncoding) SetTargetImage(img draw.Image) { // Read implements the Encoding interface. func (enc *RawEncoding) Read(c Conn, rect *Rectangle) error { pf := c.PixelFormat() - cm := c.ColorMap() - //colors := make([]Color, rect.Area()) - for y := 0; y < int(rect.Height); y++ { - for x := 0; x < int(rect.Width); x++ { - c1 := NewColor(&pf, &cm) - if err := c1.Read(c); err != nil { - return err - } + DecodeRaw(c, &pf, rect, enc.Image) - c2:=color.RGBA{R:uint8(c1.R),G:uint8(c1.G),B:uint8(c1.B),A:1} - //c3 := color.RGBAModel.Convert(c2) - - enc.Image.(draw.Image).Set(int(rect.X)+x,int(rect.Y)+y,c2) - //colors[int(y)*int(rect.Width)+int(x)] = *color - } - } - - //enc.Colors = colors return nil } diff --git a/encoding_util.go b/encoding_util.go index 521318d..692e370 100644 --- a/encoding_util.go +++ b/encoding_util.go @@ -55,3 +55,18 @@ func ReadColor(c io.Reader, pf *PixelFormat) (*color.RGBA, error) { return &rgb, nil } + +func DecodeRaw(reader io.Reader, pf *PixelFormat, rect *Rectangle, targetImage draw.Image) error { + for y := 0; y < int(rect.Height); y++ { + for x := 0; x < int(rect.Width); x++ { + col, err := ReadColor(reader, pf) + if err != nil { + return err + } + + targetImage.(draw.Image).Set(int(rect.X)+x, int(rect.Y)+y, col) + } + } + + return nil +} diff --git a/encoding_zlib.go b/encoding_zlib.go index 41f7488..9e12abc 100644 --- a/encoding_zlib.go +++ b/encoding_zlib.go @@ -2,37 +2,69 @@ package vnc2video import ( "bytes" - "encoding/binary" + "compress/zlib" + "image/draw" "io" ) type ZLibEncoding struct { - bytes []byte + Image draw.Image + unzipper io.Reader + zippedBuff *bytes.Buffer } -func (z *ZLibEncoding) Type() int32 { - return 6 +func (*ZLibEncoding) Type() EncodingType { + return EncZlib } -func (z *ZLibEncoding) WriteTo(w io.Writer) (n int, err error) { - return w.Write(z.bytes) + +func (enc *ZLibEncoding) WriteTo(w io.Writer) (n int, err error) { + return 0, nil } -func (z *ZLibEncoding) Read(r Conn, rect *Rectangle) error { + +func (enc *ZLibEncoding) Write(c Conn, rect *Rectangle) error { + return nil +} + +func (enc *ZLibEncoding) SetTargetImage(img draw.Image) { + enc.Image = img +} + +func (*ZLibEncoding) Supported(Conn) bool { + return true +} +func (enc *ZLibEncoding) Reset() error { + enc.unzipper = nil + return nil +} + +func (enc *ZLibEncoding) Read(r Conn, rect *Rectangle) error { //func (z *ZLibEncoding) Read(pixelFmt *PixelFormat, rect *Rectangle, r io.Reader) (Encoding, error) { //conn := RfbReadHelper{Reader:r} //conn := &DataSource{conn: conn.c, PixelFormat: conn.PixelFormat} - //bytesPerPixel := c.PixelFormat.BPP / 8 - bytes := &bytes.Buffer{} - len, err := ReadUint32(r) + pf := r.PixelFormat() + //bytesPerPixel := r.PixelFormat().BPP / 8 + //bytesBuff := &bytes.Buffer{} + zippedLen, err := ReadUint32(r) if err != nil { return err } - binary.Write(bytes, binary.BigEndian, len) - _, err = ReadBytes(int(len), r) + b, err := ReadBytes(int(zippedLen), r) if err != nil { return err } - //StoreBytes(bytes, bts) - z.bytes = bytes.Bytes() + 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) + } + DecodeRaw(enc.unzipper, &pf, rect, enc.Image) + return nil } diff --git a/example/client/main.go b/example/client/main.go index fd94c4a..ebee29a 100644 --- a/example/client/main.go +++ b/example/client/main.go @@ -39,7 +39,8 @@ func main() { &vnc.RawEncoding{}, //&vnc.TightEncoding{}, &vnc.HextileEncoding{}, - //&vnc.CursorPseudoEncoding{}, + &vnc.CursorPseudoEncoding{}, + &vnc.CursorPosPseudoEncoding{}, }, ErrorCh: errorCh, } @@ -76,7 +77,12 @@ func main() { logger.Debugf("connected to: %s", os.Args[1]) defer cc.Close() - cc.SetEncodings([]vnc.EncodingType{vnc.EncHextile}) + cc.SetEncodings([]vnc.EncodingType{ + vnc.EncCursorPseudo, + vnc.EncPointerPosPseudo, + //vnc.EncTight, + vnc.EncHextile, + }) //rect := image.Rect(0, 0, int(cc.Width()), int(cc.Height())) //screenImage := image.NewRGBA64(rect) // Process messages coming in on the ServerMessage channel.