2
* Copyright (C) 2002 RealVNC Ltd. All Rights Reserved.
3
* Copyright (C) 2003 Sun Microsystems, Inc.
5
* This is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This software is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this software; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
24
* Routines to implement Zlib Run-length Encoding (ZRLE).
28
#include <zrleoutstream.h>
31
#define GET_IMAGE_INTO_BUF(tx,ty,tw,th,buf) \
32
{ char *fbptr = (cl->screen->frameBuffer \
33
+ (cl->screen->paddedWidthInBytes * ty) \
34
+ (tx * (cl->screen->bitsPerPixel / 8))); \
36
(*cl->translateFn)(cl->translateLookupTable, &cl->screen->rfbServerFormat,\
37
&cl->format, fbptr, (char*)buf, \
38
cl->screen->paddedWidthInBytes, tw, th); }
40
#define EXTRA_ARGS , rfbClientPtr cl
43
#include <zrleencodetemplate.c>
46
#include <zrleencodetemplate.c>
49
#include <zrleencodetemplate.c>
51
#include <zrleencodetemplate.c>
54
#include <zrleencodetemplate.c>
60
* zrleBeforeBuf contains pixel data in the client's format. It must be at
61
* least one pixel bigger than the largest tile of pixel data, since the
62
* ZRLE encoding algorithm writes to the position one past the end of the pixel
66
static char zrleBeforeBuf[rfbZRLETileWidth * rfbZRLETileHeight * 4 + 4];
71
* rfbSendRectEncodingZRLE - send a given rectangle using ZRLE encoding.
75
rfbBool rfbSendRectEncodingZRLE(rfbClientPtr cl, int x, int y, int w, int h)
78
rfbFramebufferUpdateRectHeader rect;
83
cl->zrleData = zrleOutStreamNew();
85
zos->in.ptr = zos->in.start;
86
zos->out.ptr = zos->out.start;
88
switch (cl->format.bitsPerPixel) {
91
zrleEncode8( x, y, w, h, zos, zrleBeforeBuf, cl);
95
zrleEncode16(x, y, w, h, zos, zrleBeforeBuf, cl);
99
rfbBool fitsInLS3Bytes
100
= ((cl->format.redMax << cl->format.redShift) < (1<<24) &&
101
(cl->format.greenMax << cl->format.greenShift) < (1<<24) &&
102
(cl->format.blueMax << cl->format.blueShift) < (1<<24));
104
rfbBool fitsInMS3Bytes = (cl->format.redShift > 7 &&
105
cl->format.greenShift > 7 &&
106
cl->format.blueShift > 7);
108
if ((fitsInLS3Bytes && !cl->format.bigEndian) ||
109
(fitsInMS3Bytes && cl->format.bigEndian))
111
zrleEncode24A(x, y, w, h, zos, zrleBeforeBuf, cl);
113
else if ((fitsInLS3Bytes && cl->format.bigEndian) ||
114
(fitsInMS3Bytes && !cl->format.bigEndian))
116
zrleEncode24B(x, y, w, h, zos, zrleBeforeBuf, cl);
120
zrleEncode32(x, y, w, h, zos, zrleBeforeBuf, cl);
126
cl->rfbRectanglesSent[rfbEncodingZRLE]++;
127
cl->rfbBytesSent[rfbEncodingZRLE] += (sz_rfbFramebufferUpdateRectHeader
128
+ sz_rfbZRLEHeader + ZRLE_BUFFER_LENGTH(&zos->out));
130
if (cl->ublen + sz_rfbFramebufferUpdateRectHeader + sz_rfbZRLEHeader
133
if (!rfbSendUpdateBuf(cl))
137
rect.r.x = Swap16IfLE(x);
138
rect.r.y = Swap16IfLE(y);
139
rect.r.w = Swap16IfLE(w);
140
rect.r.h = Swap16IfLE(h);
141
rect.encoding = Swap32IfLE(rfbEncodingZRLE);
143
memcpy(cl->updateBuf+cl->ublen, (char *)&rect,
144
sz_rfbFramebufferUpdateRectHeader);
145
cl->ublen += sz_rfbFramebufferUpdateRectHeader;
147
hdr.length = Swap32IfLE(ZRLE_BUFFER_LENGTH(&zos->out));
149
memcpy(cl->updateBuf+cl->ublen, (char *)&hdr, sz_rfbZRLEHeader);
150
cl->ublen += sz_rfbZRLEHeader;
152
/* copy into updateBuf and send from there. Maybe should send directly? */
154
for (i = 0; i < ZRLE_BUFFER_LENGTH(&zos->out);) {
156
int bytesToCopy = UPDATE_BUF_SIZE - cl->ublen;
158
if (i + bytesToCopy > ZRLE_BUFFER_LENGTH(&zos->out)) {
159
bytesToCopy = ZRLE_BUFFER_LENGTH(&zos->out) - i;
162
memcpy(cl->updateBuf+cl->ublen, (uint8_t*)zos->out.start + i, bytesToCopy);
164
cl->ublen += bytesToCopy;
167
if (cl->ublen == UPDATE_BUF_SIZE) {
168
if (!rfbSendUpdateBuf(cl))
177
void FreeZrleData(rfbClientPtr cl)
180
zrleOutStreamFree(cl->zrleData);