mirror of
https://github.com/amitbet/vnc2video.git
synced 2025-05-08 23:36:21 +00:00
added zlib encoding (tested)
This commit is contained in:
parent
5cd902341d
commit
b858291681
@ -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
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user