2 // Copyright (C) 2002 RealVNC Ltd. All Rights Reserved.
4 // This is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
9 // This software is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this software; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
23 // Routines to implement Zlib Run-length Encoding (ZRLE).
30 #include <rdr/MemOutStream.h>
31 #include <rdr/ZlibOutStream.h>
34 #define GET_IMAGE_INTO_BUF(tx,ty,tw,th,buf) \
35 char *fbptr = (rfbScreen.pfbMemory + (rfbScreen.paddedWidthInBytes * ty) \
36 + (tx * (rfbScreen.bitsPerPixel / 8))); \
38 (*cl->translateFn)(cl->translateLookupTable, &rfbServerFormat, \
39 &cl->format, fbptr, (char*)buf, \
40 rfbScreen.paddedWidthInBytes, tw, th);
42 #define EXTRA_ARGS , rfbClientPtr cl
45 #include <rfb/zrleEncode.h>
48 #include <rfb/zrleEncode.h>
51 #include <rfb/zrleEncode.h>
53 #include <rfb/zrleEncode.h>
56 #include <rfb/zrleEncode.h>
62 * zrleBeforeBuf contains pixel data in the client's format. It must be at
63 * least one pixel bigger than the largest tile of pixel data, since the
64 * ZRLE encoding algorithm writes to the position one past the end of the pixel
68 static char zrleBeforeBuf[rfbZRLETileWidth * rfbZRLETileHeight * 4 + 4];
70 static rdr::MemOutStream mos;
74 * rfbSendRectEncodingZRLE - send a given rectangle using ZRLE encoding.
78 Bool rfbSendRectEncodingZRLE(rfbClientPtr cl, int x, int y, int w, int h)
80 if (!cl->zrleData) cl->zrleData = new rdr::ZlibOutStream;
81 rdr::ZlibOutStream* zos = (rdr::ZlibOutStream*)cl->zrleData;
84 switch (cl->format.bitsPerPixel) {
87 zrleEncode8( x, y, w, h, &mos, zos, zrleBeforeBuf, cl);
91 zrleEncode16(x, y, w, h, &mos, zos, zrleBeforeBuf, cl);
96 = ((cl->format.redMax << cl->format.redShift) < (1<<24) &&
97 (cl->format.greenMax << cl->format.greenShift) < (1<<24) &&
98 (cl->format.blueMax << cl->format.blueShift) < (1<<24));
100 bool fitsInMS3Bytes = (cl->format.redShift > 7 &&
101 cl->format.greenShift > 7 &&
102 cl->format.blueShift > 7);
104 if ((fitsInLS3Bytes && !cl->format.bigEndian) ||
105 (fitsInMS3Bytes && cl->format.bigEndian))
107 zrleEncode24A(x, y, w, h, &mos, zos, zrleBeforeBuf, cl);
109 else if ((fitsInLS3Bytes && cl->format.bigEndian) ||
110 (fitsInMS3Bytes && !cl->format.bigEndian))
112 zrleEncode24B(x, y, w, h, &mos, zos, zrleBeforeBuf, cl);
116 zrleEncode32(x, y, w, h, &mos, zos, zrleBeforeBuf, cl);
121 cl->rfbRectanglesSent[rfbEncodingZRLE]++;
122 cl->rfbBytesSent[rfbEncodingZRLE] += (sz_rfbFramebufferUpdateRectHeader
123 + sz_rfbZRLEHeader + mos.length());
125 if (ublen + sz_rfbFramebufferUpdateRectHeader + sz_rfbZRLEHeader
128 if (!rfbSendUpdateBuf(cl))
132 rfbFramebufferUpdateRectHeader rect;
133 rect.r.x = Swap16IfLE(x);
134 rect.r.y = Swap16IfLE(y);
135 rect.r.w = Swap16IfLE(w);
136 rect.r.h = Swap16IfLE(h);
137 rect.encoding = Swap32IfLE(rfbEncodingZRLE);
139 memcpy(&updateBuf[ublen], (char *)&rect,
140 sz_rfbFramebufferUpdateRectHeader);
141 ublen += sz_rfbFramebufferUpdateRectHeader;
145 hdr.length = Swap32IfLE(mos.length());
147 memcpy(&updateBuf[ublen], (char *)&hdr, sz_rfbZRLEHeader);
148 ublen += sz_rfbZRLEHeader;
150 // copy into updateBuf and send from there. Maybe should send directly?
152 for (int i = 0; i < mos.length();) {
154 int bytesToCopy = UPDATE_BUF_SIZE - ublen;
156 if (i + bytesToCopy > mos.length()) {
157 bytesToCopy = mos.length() - i;
160 memcpy(&updateBuf[ublen], (CARD8*)mos.data() + i, bytesToCopy);
162 ublen += bytesToCopy;
165 if (ublen == UPDATE_BUF_SIZE) {
166 if (!rfbSendUpdateBuf(cl))
175 void FreeZrleData(rfbClientPtr cl)
177 delete (rdr::ZlibOutStream*)cl->zrleData;