4 * Routines to implement Rise-and-Run-length Encoding (RRE). This
5 * code is based on krw's original javatel rfbserver.
9 * Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
11 * This is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This software is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this software; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
31 * rreBeforeBuf contains pixel data in the client's format.
32 * rreAfterBuf contains the RRE encoded version. If the RRE encoded version is
33 * larger than the raw data or if it exceeds rreAfterBufSize then
34 * raw encoding is used instead.
37 static int rreBeforeBufSize = 0;
38 static char *rreBeforeBuf = NULL;
40 static int rreAfterBufSize = 0;
41 static char *rreAfterBuf = NULL;
42 static int rreAfterBufLen;
44 static int subrectEncode8(CARD8 *data, int w, int h);
45 static int subrectEncode16(CARD16 *data, int w, int h);
46 static int subrectEncode32(CARD32 *data, int w, int h);
47 static CARD32 getBgColour(char *data, int size, int bpp);
51 * rfbSendRectEncodingRRE - send a given rectangle using RRE encoding.
55 rfbSendRectEncodingRRE(cl, x, y, w, h)
59 rfbFramebufferUpdateRectHeader rect;
63 char *fbptr = (rfbScreen.pfbMemory + (rfbScreen.paddedWidthInBytes * y)
64 + (x * (rfbScreen.bitsPerPixel / 8)));
66 int maxRawSize = (rfbScreen.width * rfbScreen.height
67 * (cl->format.bitsPerPixel / 8));
69 if (rreBeforeBufSize < maxRawSize) {
70 rreBeforeBufSize = maxRawSize;
71 if (rreBeforeBuf == NULL)
72 rreBeforeBuf = (char *)xalloc(rreBeforeBufSize);
74 rreBeforeBuf = (char *)xrealloc(rreBeforeBuf, rreBeforeBufSize);
77 if (rreAfterBufSize < maxRawSize) {
78 rreAfterBufSize = maxRawSize;
79 if (rreAfterBuf == NULL)
80 rreAfterBuf = (char *)xalloc(rreAfterBufSize);
82 rreAfterBuf = (char *)xrealloc(rreAfterBuf, rreAfterBufSize);
85 (*cl->translateFn)(cl->translateLookupTable, &rfbServerFormat,
86 &cl->format, fbptr, rreBeforeBuf,
87 rfbScreen.paddedWidthInBytes, w, h);
89 switch (cl->format.bitsPerPixel) {
91 nSubrects = subrectEncode8((CARD8 *)rreBeforeBuf, w, h);
94 nSubrects = subrectEncode16((CARD16 *)rreBeforeBuf, w, h);
97 nSubrects = subrectEncode32((CARD32 *)rreBeforeBuf, w, h);
100 rfbLog("getBgColour: bpp %d?\n",cl->format.bitsPerPixel);
106 /* RRE encoding was too large, use raw */
108 return rfbSendRectEncodingRaw(cl, x, y, w, h);
111 cl->rfbRectanglesSent[rfbEncodingRRE]++;
112 cl->rfbBytesSent[rfbEncodingRRE] += (sz_rfbFramebufferUpdateRectHeader
113 + sz_rfbRREHeader + rreAfterBufLen);
115 if (ublen + sz_rfbFramebufferUpdateRectHeader + sz_rfbRREHeader
118 if (!rfbSendUpdateBuf(cl))
122 rect.r.x = Swap16IfLE(x);
123 rect.r.y = Swap16IfLE(y);
124 rect.r.w = Swap16IfLE(w);
125 rect.r.h = Swap16IfLE(h);
126 rect.encoding = Swap32IfLE(rfbEncodingRRE);
128 memcpy(&updateBuf[ublen], (char *)&rect,
129 sz_rfbFramebufferUpdateRectHeader);
130 ublen += sz_rfbFramebufferUpdateRectHeader;
132 hdr.nSubrects = Swap32IfLE(nSubrects);
134 memcpy(&updateBuf[ublen], (char *)&hdr, sz_rfbRREHeader);
135 ublen += sz_rfbRREHeader;
137 for (i = 0; i < rreAfterBufLen;) {
139 int bytesToCopy = UPDATE_BUF_SIZE - ublen;
141 if (i + bytesToCopy > rreAfterBufLen) {
142 bytesToCopy = rreAfterBufLen - i;
145 memcpy(&updateBuf[ublen], &rreAfterBuf[i], bytesToCopy);
147 ublen += bytesToCopy;
150 if (ublen == UPDATE_BUF_SIZE) {
151 if (!rfbSendUpdateBuf(cl))
162 * subrectEncode() encodes the given multicoloured rectangle as a background
163 * colour overwritten by single-coloured rectangles. It returns the number
164 * of subrectangles in the encoded buffer, or -1 if subrect encoding won't
165 * fit in the buffer. It puts the encoded rectangles in rreAfterBuf. The
166 * single-colour rectangle partition is not optimal, but does find the biggest
167 * horizontal or vertical rectangle top-left anchored to each consecutive
168 * coordinate position.
170 * The coding scheme is simply [<bgcolour><subrect><subrect>...] where each
171 * <subrect> is [<colour><x><y><w><h>].
174 #define DEFINE_SUBRECT_ENCODE(bpp) \
176 subrectEncode##bpp(data,w,h) \
182 rfbRectangle subrect; \
185 int hx=0,hy,vx=0,vy; \
190 int thex,they,thew,theh; \
193 CARD##bpp bg = (CARD##bpp)getBgColour((char*)data,w*h,bpp); \
195 *((CARD##bpp*)rreAfterBuf) = bg; \
197 rreAfterBufLen = (bpp/8); \
199 for (y=0; y<h; y++) { \
201 for (x=0; x<w; x++) { \
202 if (line[x] != bg) { \
206 for (j=y; j<h; j++) { \
208 if (seg[x] != cl) {break;} \
210 while ((seg[i] == cl) && (i < w)) i += 1; \
212 if (j == y) vx = hx = i; \
213 if (i < vx) vx = i; \
214 if ((hyflag > 0) && (i >= hx)) {hy += 1;} else {hyflag = 0;} \
218 /* We now have two possible subrects: (x,y,hx,hy) and (x,y,vx,vy) \
219 * We'll choose the bigger of the two. \
229 if ((hw*hh) > (vw*vh)) { \
237 subrect.x = Swap16IfLE(thex); \
238 subrect.y = Swap16IfLE(they); \
239 subrect.w = Swap16IfLE(thew); \
240 subrect.h = Swap16IfLE(theh); \
242 newLen = rreAfterBufLen + (bpp/8) + sz_rfbRectangle; \
243 if ((newLen > (w * h * (bpp/8))) || (newLen > rreAfterBufSize)) \
247 *((CARD##bpp*)(rreAfterBuf + rreAfterBufLen)) = cl; \
248 rreAfterBufLen += (bpp/8); \
249 memcpy(&rreAfterBuf[rreAfterBufLen],&subrect,sz_rfbRectangle); \
250 rreAfterBufLen += sz_rfbRectangle; \
253 * Now mark the subrect as done. \
255 for (j=they; j < (they+theh); j++) { \
256 for (i=thex; i < (thex+thew); i++) { \
267 DEFINE_SUBRECT_ENCODE(8)
268 DEFINE_SUBRECT_ENCODE(16)
269 DEFINE_SUBRECT_ENCODE(32)
273 * getBgColour() gets the most prevalent colour in a byte array.
276 getBgColour(data,size,bpp)
284 static int counts[NUMCLRS];
292 return ((CARD16 *)data)[0];
293 } else if (bpp == 32) {
294 return ((CARD32 *)data)[0];
296 rfbLog("getBgColour: bpp %d?\n",bpp);
301 for (i=0; i<NUMCLRS; i++) {
305 for (j=0; j<size; j++) {
306 k = (int)(((CARD8 *)data)[j]);
308 rfbLog("getBgColour: unusual colour = %d\n", k);
312 if (counts[k] > maxcount) {
313 maxcount = counts[k];
314 maxclr = ((CARD8 *)data)[j];