added tightpng encoding

This commit is contained in:
amit bezalel 2017-07-02 22:27:20 +03:00
parent 076d8bb4cf
commit ed0dc6839c
3 changed files with 73 additions and 40 deletions

View File

@ -1,8 +1,8 @@
package common
import (
"encoding/binary"
"bytes"
"encoding/binary"
"io"
)
@ -18,22 +18,9 @@ type Encoding interface {
Read(*PixelFormat, *Rectangle, *RfbReadHelper) (Encoding, error)
}
const (
EncodingRaw = 0
EncodingCopyRect = 1
EncodingRRE = 2
EncodingCoRRE = 4
EncodingHextile = 5
EncodingZlib = 6
EncodingTight = 7
EncodingZRLE = 16
)
// EncodingType represents a known VNC encoding type.
type EncodingType int32
//go:generate stringer -type=EncodingType
const (
EncRaw EncodingType = 0
EncCopyRect EncodingType = 1
@ -80,6 +67,17 @@ const (
EncFencePseudo EncodingType = -312
EncContinuousUpdatesPseudo EncodingType = -313
EncClientRedirect EncodingType = -311
EncTightPNGBase64 EncodingType = 21 + 0x574d5600
EncTightDiffComp EncodingType = 22 + 0x574d5600
EncVMWDefineCursor EncodingType = 100 + 0x574d5600
EncVMWCursorState EncodingType = 101 + 0x574d5600
EncVMWCursorPosition EncodingType = 102 + 0x574d5600
EncVMWTypematicInfo EncodingType = 103 + 0x574d5600
EncVMWLEDState EncodingType = 104 + 0x574d5600
EncVMWServerPush2 EncodingType = 123 + 0x574d5600
EncVMWServerCaps EncodingType = 122 + 0x574d5600
EncVMWFrameStamp EncodingType = 124 + 0x574d5600
EncOffscreenCopyRect EncodingType = 126 + 0x574d5600
)
// PixelFormat describes the way a pixel is formatted for a VNC connection.
@ -98,17 +96,17 @@ type PixelFormat struct {
BlueShift uint8
}
func (format *PixelFormat) WriteTo(w io.Writer) ( error) {
func (format *PixelFormat) WriteTo(w io.Writer) error {
var buf bytes.Buffer
// Byte 1
if err := binary.Write(&buf, binary.BigEndian, format.BPP); err != nil {
return err
return err
}
// Byte 2
if err := binary.Write(&buf, binary.BigEndian, format.Depth); err != nil {
return err
return err
}
var boolByte byte
@ -120,7 +118,7 @@ func (format *PixelFormat) WriteTo(w io.Writer) ( error) {
// Byte 3 (BigEndian)
if err := binary.Write(&buf, binary.BigEndian, boolByte); err != nil {
return err
return err
}
if format.TrueColor {
@ -131,39 +129,39 @@ func (format *PixelFormat) WriteTo(w io.Writer) ( error) {
// Byte 4 (TrueColor)
if err := binary.Write(&buf, binary.BigEndian, boolByte); err != nil {
return err
return err
}
// If we have true color enabled then we have to fill in the rest of the
// structure with the color values.
if format.TrueColor {
if err := binary.Write(&buf, binary.BigEndian, format.RedMax); err != nil {
return err
return err
}
if err := binary.Write(&buf, binary.BigEndian, format.GreenMax); err != nil {
return err
return err
}
if err := binary.Write(&buf, binary.BigEndian, format.BlueMax); err != nil {
return err
return err
}
if err := binary.Write(&buf, binary.BigEndian, format.RedShift); err != nil {
return err
return err
}
if err := binary.Write(&buf, binary.BigEndian, format.GreenShift); err != nil {
return err
return err
}
if err := binary.Write(&buf, binary.BigEndian, format.BlueShift); err != nil {
return err
return err
}
}
w.Write(buf.Bytes()[0:16])
return nil
return nil
}
func NewPixelFormat(bpp uint8) *PixelFormat {

View File

@ -12,7 +12,8 @@ const (
TightExplicitFilter = 0x04
TightFill = 0x08
TightJpeg = 0x09
TightMaxSubencoding = 0x09
TightPNG = 0x10
TightFilterCopy = 0x00
TightFilterPalette = 0x01
TightFilterGradient = 0x02
@ -74,24 +75,15 @@ func (t *TightEncoding) Read(pixelFmt *common.PixelFormat, rect *common.Rectangl
//conn := &DataSource{conn: conn.c, PixelFormat: conn.PixelFormat}
//var subencoding uint8
subencoding, err := r.ReadUint8()
compctl, err := r.ReadUint8()
if err != nil {
fmt.Printf("error in handling tight encoding: %v\n", err)
return nil, err
}
fmt.Printf("bytesPixel= %d, subencoding= %d\n", bytesPixel, subencoding)
// if err := binary.Read(conn.c, binary.BigEndian, &subencoding); err != nil {
// return t, err
// }
fmt.Printf("bytesPixel= %d, subencoding= %d\n", bytesPixel, compctl)
//move it to position (remove zlib flush commands)
compType := subencoding >> 4 & 0x0F
// for stream_id := 0; stream_id < 4; stream_id++ {
// // if ((comp_ctl & 1) != 0 && tightInflaters[stream_id] != null) {
// // tightInflaters[stream_id] = null;
// // }
// subencoding >>= 1
// }
compType := compctl >> 4 & 0x0F
fmt.Printf("afterSHL:%d\n", compType)
switch compType {
@ -120,7 +112,7 @@ func (t *TightEncoding) Read(pixelFmt *common.PixelFormat, rect *common.Rectangl
fmt.Println("Compression control byte is incorrect!")
}
handleTightFilters(subencoding, pixelFmt, rect, r)
handleTightFilters(compctl, pixelFmt, rect, r)
return t, nil
}
}

43
encodings/enc-tightpng.go Normal file
View File

@ -0,0 +1,43 @@
package encodings
import (
"fmt"
"vncproxy/common"
)
type TightPngEncoding struct {
}
func (*TightPngEncoding) Type() int32 { return int32(common.EncTightPng) }
func (t *TightPngEncoding) Read(pixelFmt *common.PixelFormat, rect *common.Rectangle, r *common.RfbReadHelper) (common.Encoding, error) {
bytesPixel := calcTightBytePerPixel(pixelFmt)
//var subencoding uint8
compctl, err := r.ReadUint8()
if err != nil {
fmt.Printf("error in handling tight encoding: %v\n", err)
return nil, err
}
fmt.Printf("bytesPixel= %d, subencoding= %d\n", bytesPixel, compctl)
//move it to position (remove zlib flush commands)
compType := compctl >> 4 & 0x0F
fmt.Printf("afterSHL:%d\n", compType)
switch compType {
case TightPNG:
len, err := r.ReadCompactLen()
_, err = r.ReadBytes(len)
if err != nil {
return t, err
}
case TightFill:
r.ReadBytes(int(bytesPixel))
default:
return nil, fmt.Errorf("unknown tight compression %d", compType)
}
return t, nil
}