vncencodezrle.cpp
4.59 KB
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
// Copyright (C) 2002 RealVNC Ltd. All Rights Reserved.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.
//
// If the source code for the program is not available from the place from
// which you received this file, check http://www.realvnc.com/ or contact
// the authors on info@realvnc.com for information on obtaining it.
#include "vncEncodeZRLE.h"
#include "rfb.h"
#include "rfbMisc.h"
#include <stdlib.h>
#include <time.h>
#include <rdr/MemOutStream.h>
#include <rdr/ZlibOutStream.h>
#define GET_IMAGE_INTO_BUF(tx,ty,tw,th,buf) \
rfb::Rect rect; \
rect.tl.x = tx; \
rect.tl.y = ty; \
rect.br.x = tx+tw; \
rect.br.y = ty+th; \
encoder->Translate(source, (BYTE*)buf, rect);
#define EXTRA_ARGS , BYTE* source, vncEncoder* encoder
#define BPP 8
#include <rfb/zrleEncode.h>
#undef BPP
#define BPP 16
#include <rfb/zrleEncode.h>
#undef BPP
#define BPP 32
#include <rfb/zrleEncode.h>
#define CPIXEL 24A
#include <rfb/zrleEncode.h>
#undef CPIXEL
#define CPIXEL 24B
#include <rfb/zrleEncode.h>
#undef CPIXEL
#undef BPP
vncEncodeZRLE::vncEncodeZRLE()
{
mos = new rdr::MemOutStream;
zos = new rdr::ZlibOutStream;
beforeBuf = new rdr::U32[rfbZRLETileWidth * rfbZRLETileHeight + 1];
}
vncEncodeZRLE::~vncEncodeZRLE()
{
delete mos;
delete zos;
delete [] beforeBuf;
}
void vncEncodeZRLE::Init()
{
vncEncoder::Init();
}
UINT vncEncodeZRLE::RequiredBuffSize(UINT width, UINT height)
{
// this is a guess - 12 bytes plus 1.5 times raw... (zlib.h says compress
// needs 12 bytes plus 1.001 times raw data but that's not quite what we give
// zlib anyway)
return (sz_rfbFramebufferUpdateRectHeader + sz_rfbZRLEHeader + 12 +
width * height * (m_remoteformat.bitsPerPixel / 8) * 3 / 2);
}
UINT vncEncodeZRLE::EncodeRect(BYTE *source, BYTE *dest, const rfb::Rect &rect)
{
int x = rect.tl.x;
int y = rect.tl.y;
int w = rect.br.x - x;
int h = rect.br.y - y;
mos->clear();
switch (m_remoteformat.bitsPerPixel) {
case 8:
zrleEncode8( x, y, w, h, mos, zos, beforeBuf, source, this);
break;
case 16:
zrleEncode16(x, y, w, h, mos, zos, beforeBuf, source, this);
break;
case 32:
bool fitsInLS3Bytes
= ((m_remoteformat.redMax << m_remoteformat.redShift) < (1<<24) &&
(m_remoteformat.greenMax << m_remoteformat.greenShift) < (1<<24) &&
(m_remoteformat.blueMax << m_remoteformat.blueShift) < (1<<24));
bool fitsInMS3Bytes = (m_remoteformat.redShift > 7 &&
m_remoteformat.greenShift > 7 &&
m_remoteformat.blueShift > 7);
if ((fitsInLS3Bytes && !m_remoteformat.bigEndian) ||
(fitsInMS3Bytes && m_remoteformat.bigEndian))
{
zrleEncode24A(x, y, w, h, mos, zos, beforeBuf, source, this);
}
else if ((fitsInLS3Bytes && m_remoteformat.bigEndian) ||
(fitsInMS3Bytes && !m_remoteformat.bigEndian))
{
zrleEncode24B(x, y, w, h, mos, zos, beforeBuf, source, this);
}
else
{
zrleEncode32(x, y, w, h, mos, zos, beforeBuf, source, this);
}
break;
}
rfbFramebufferUpdateRectHeader* surh = (rfbFramebufferUpdateRectHeader*)dest;
surh->r.x = Swap16IfLE(x-m_SWOffsetx);
surh->r.y = Swap16IfLE(y-m_SWOffsety);
surh->r.w = Swap16IfLE(w);
surh->r.h = Swap16IfLE(h);
surh->encoding = Swap32IfLE(rfbEncodingZRLE);
rfbZRLEHeader* hdr = (rfbZRLEHeader*)(dest +
sz_rfbFramebufferUpdateRectHeader);
hdr->length = Swap32IfLE(mos->length());
memcpy(dest + sz_rfbFramebufferUpdateRectHeader + sz_rfbZRLEHeader,
(rdr::U8*)mos->data(), mos->length());
return sz_rfbFramebufferUpdateRectHeader + sz_rfbZRLEHeader + mos->length();
}