~ubuntu-branches/debian/lenny/italc/lenny

« back to all changes in this revision

Viewing changes to common/ivs/libvncserver/rre.c

  • Committer: Bazaar Package Importer
  • Author(s): Patrick Winnertz
  • Date: 2008-06-17 13:46:54 UTC
  • mfrom: (1.2.1 upstream) (4.1.1 gutsy)
  • Revision ID: james.westby@ubuntu.com-20080617134654-cl0gi4u524cv1ici
Tags: 1:1.0.9~rc3-1
* Package new upstream version
  - upstream ported the code to qt4.4 (Closes: #481974)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * rre.c
3
 
 *
4
 
 * Routines to implement Rise-and-Run-length Encoding (RRE).  This
5
 
 * code is based on krw's original javatel rfbserver.
6
 
 */
7
 
 
8
 
/*
9
 
 *  OSXvnc Copyright (C) 2001 Dan McGuirk <mcguirk@incompleteness.net>.
10
 
 *  Original Xvnc code Copyright (C) 1999 AT&T Laboratories Cambridge.  
11
 
 *  All Rights Reserved.
12
 
 *
13
 
 *  This is free software; you can redistribute it and/or modify
14
 
 *  it under the terms of the GNU General Public License as published by
15
 
 *  the Free Software Foundation; either version 2 of the License, or
16
 
 *  (at your option) any later version.
17
 
 *
18
 
 *  This software is distributed in the hope that it will be useful,
19
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 
 *  GNU General Public License for more details.
22
 
 *
23
 
 *  You should have received a copy of the GNU General Public License
24
 
 *  along with this software; if not, write to the Free Software
25
 
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
26
 
 *  USA.
27
 
 */
28
 
 
29
 
#include <rfb/rfb.h>
30
 
 
31
 
/*
32
 
 * rreBeforeBuf contains pixel data in the client's format.
33
 
 * rreAfterBuf contains the RRE encoded version.  If the RRE encoded version is
34
 
 * larger than the raw data or if it exceeds rreAfterBufSize then
35
 
 * raw encoding is used instead.
36
 
 */
37
 
 
38
 
static int rreBeforeBufSize = 0;
39
 
static char *rreBeforeBuf = NULL;
40
 
 
41
 
static int rreAfterBufSize = 0;
42
 
static char *rreAfterBuf = NULL;
43
 
static int rreAfterBufLen;
44
 
 
45
 
static int subrectEncode8(uint8_t *data, int w, int h);
46
 
static int subrectEncode16(uint16_t *data, int w, int h);
47
 
static int subrectEncode32(uint32_t *data, int w, int h);
48
 
static uint32_t getBgColour(char *data, int size, int bpp);
49
 
 
50
 
 
51
 
/*
52
 
 * rfbSendRectEncodingRRE - send a given rectangle using RRE encoding.
53
 
 */
54
 
 
55
 
rfbBool
56
 
rfbSendRectEncodingRRE(cl, x, y, w, h)
57
 
    rfbClientPtr cl;
58
 
    int x, y, w, h;
59
 
{
60
 
    rfbFramebufferUpdateRectHeader rect;
61
 
    rfbRREHeader hdr;
62
 
    int nSubrects;
63
 
    int i;
64
 
    char *fbptr = (cl->screen->frameBuffer + (cl->screen->paddedWidthInBytes * y)
65
 
                   + (x * (cl->screen->bitsPerPixel / 8)));
66
 
 
67
 
    int maxRawSize = (cl->screen->width * cl->screen->height
68
 
                      * (cl->format.bitsPerPixel / 8));
69
 
 
70
 
    if (rreBeforeBufSize < maxRawSize) {
71
 
        rreBeforeBufSize = maxRawSize;
72
 
        if (rreBeforeBuf == NULL)
73
 
            rreBeforeBuf = (char *)malloc(rreBeforeBufSize);
74
 
        else
75
 
            rreBeforeBuf = (char *)realloc(rreBeforeBuf, rreBeforeBufSize);
76
 
    }
77
 
 
78
 
    if (rreAfterBufSize < maxRawSize) {
79
 
        rreAfterBufSize = maxRawSize;
80
 
        if (rreAfterBuf == NULL)
81
 
            rreAfterBuf = (char *)malloc(rreAfterBufSize);
82
 
        else
83
 
            rreAfterBuf = (char *)realloc(rreAfterBuf, rreAfterBufSize);
84
 
    }
85
 
 
86
 
    (*cl->translateFn)(cl->translateLookupTable,
87
 
                       &(cl->screen->rfbServerFormat),
88
 
                       &cl->format, fbptr, rreBeforeBuf,
89
 
                       cl->screen->paddedWidthInBytes, w, h);
90
 
 
91
 
    switch (cl->format.bitsPerPixel) {
92
 
    case 8:
93
 
        nSubrects = subrectEncode8((uint8_t *)rreBeforeBuf, w, h);
94
 
        break;
95
 
    case 16:
96
 
        nSubrects = subrectEncode16((uint16_t *)rreBeforeBuf, w, h);
97
 
        break;
98
 
    case 32:
99
 
        nSubrects = subrectEncode32((uint32_t *)rreBeforeBuf, w, h);
100
 
        break;
101
 
    default:
102
 
        rfbLog("getBgColour: bpp %d?\n",cl->format.bitsPerPixel);
103
 
        return FALSE;
104
 
    }
105
 
        
106
 
    if (nSubrects < 0) {
107
 
 
108
 
        /* RRE encoding was too large, use raw */
109
 
 
110
 
        return rfbSendRectEncodingRaw(cl, x, y, w, h);
111
 
    }
112
 
 
113
 
    cl->rfbRectanglesSent[rfbEncodingRRE]++;
114
 
    cl->rfbBytesSent[rfbEncodingRRE] += (sz_rfbFramebufferUpdateRectHeader
115
 
                                         + sz_rfbRREHeader + rreAfterBufLen);
116
 
 
117
 
    if (cl->ublen + sz_rfbFramebufferUpdateRectHeader + sz_rfbRREHeader
118
 
        > UPDATE_BUF_SIZE)
119
 
    {
120
 
        if (!rfbSendUpdateBuf(cl))
121
 
            return FALSE;
122
 
    }
123
 
 
124
 
    rect.r.x = Swap16IfLE(x);
125
 
    rect.r.y = Swap16IfLE(y);
126
 
    rect.r.w = Swap16IfLE(w);
127
 
    rect.r.h = Swap16IfLE(h);
128
 
    rect.encoding = Swap32IfLE(rfbEncodingRRE);
129
 
 
130
 
    memcpy(&cl->updateBuf[cl->ublen], (char *)&rect,
131
 
           sz_rfbFramebufferUpdateRectHeader);
132
 
    cl->ublen += sz_rfbFramebufferUpdateRectHeader;
133
 
 
134
 
    hdr.nSubrects = Swap32IfLE(nSubrects);
135
 
 
136
 
    memcpy(&cl->updateBuf[cl->ublen], (char *)&hdr, sz_rfbRREHeader);
137
 
    cl->ublen += sz_rfbRREHeader;
138
 
 
139
 
    for (i = 0; i < rreAfterBufLen;) {
140
 
 
141
 
        int bytesToCopy = UPDATE_BUF_SIZE - cl->ublen;
142
 
 
143
 
        if (i + bytesToCopy > rreAfterBufLen) {
144
 
            bytesToCopy = rreAfterBufLen - i;
145
 
        }
146
 
 
147
 
        memcpy(&cl->updateBuf[cl->ublen], &rreAfterBuf[i], bytesToCopy);
148
 
 
149
 
        cl->ublen += bytesToCopy;
150
 
        i += bytesToCopy;
151
 
 
152
 
        if (cl->ublen == UPDATE_BUF_SIZE) {
153
 
            if (!rfbSendUpdateBuf(cl))
154
 
                return FALSE;
155
 
        }
156
 
    }
157
 
 
158
 
    return TRUE;
159
 
}
160
 
 
161
 
 
162
 
 
163
 
/*
164
 
 * subrectEncode() encodes the given multicoloured rectangle as a background 
165
 
 * colour overwritten by single-coloured rectangles.  It returns the number 
166
 
 * of subrectangles in the encoded buffer, or -1 if subrect encoding won't
167
 
 * fit in the buffer.  It puts the encoded rectangles in rreAfterBuf.  The
168
 
 * single-colour rectangle partition is not optimal, but does find the biggest
169
 
 * horizontal or vertical rectangle top-left anchored to each consecutive 
170
 
 * coordinate position.
171
 
 *
172
 
 * The coding scheme is simply [<bgcolour><subrect><subrect>...] where each 
173
 
 * <subrect> is [<colour><x><y><w><h>].
174
 
 */
175
 
 
176
 
#define DEFINE_SUBRECT_ENCODE(bpp)                                            \
177
 
static int                                                                    \
178
 
subrectEncode##bpp(data,w,h)                                                  \
179
 
    uint##bpp##_t *data;                                                          \
180
 
    int w;                                                                    \
181
 
    int h;                                                                    \
182
 
{                                                                             \
183
 
    uint##bpp##_t cl;                                                             \
184
 
    rfbRectangle subrect;                                                     \
185
 
    int x,y;                                                                  \
186
 
    int i,j;                                                                  \
187
 
    int hx=0,hy,vx=0,vy;                                                      \
188
 
    int hyflag;                                                               \
189
 
    uint##bpp##_t *seg;                                                           \
190
 
    uint##bpp##_t *line;                                                          \
191
 
    int hw,hh,vw,vh;                                                          \
192
 
    int thex,they,thew,theh;                                                  \
193
 
    int numsubs = 0;                                                          \
194
 
    int newLen;                                                               \
195
 
    uint##bpp##_t bg = (uint##bpp##_t)getBgColour((char*)data,w*h,bpp);               \
196
 
                                                                              \
197
 
    *((uint##bpp##_t*)rreAfterBuf) = bg;                                          \
198
 
                                                                              \
199
 
    rreAfterBufLen = (bpp/8);                                                 \
200
 
                                                                              \
201
 
    for (y=0; y<h; y++) {                                                     \
202
 
      line = data+(y*w);                                                      \
203
 
      for (x=0; x<w; x++) {                                                   \
204
 
        if (line[x] != bg) {                                                  \
205
 
          cl = line[x];                                                       \
206
 
          hy = y-1;                                                           \
207
 
          hyflag = 1;                                                         \
208
 
          for (j=y; j<h; j++) {                                               \
209
 
            seg = data+(j*w);                                                 \
210
 
            if (seg[x] != cl) {break;}                                        \
211
 
            i = x;                                                            \
212
 
            while ((seg[i] == cl) && (i < w)) i += 1;                         \
213
 
            i -= 1;                                                           \
214
 
            if (j == y) vx = hx = i;                                          \
215
 
            if (i < vx) vx = i;                                               \
216
 
            if ((hyflag > 0) && (i >= hx)) {hy += 1;} else {hyflag = 0;}      \
217
 
          }                                                                   \
218
 
          vy = j-1;                                                           \
219
 
                                                                              \
220
 
          /*  We now have two possible subrects: (x,y,hx,hy) and (x,y,vx,vy)  \
221
 
           *  We'll choose the bigger of the two.                             \
222
 
           */                                                                 \
223
 
          hw = hx-x+1;                                                        \
224
 
          hh = hy-y+1;                                                        \
225
 
          vw = vx-x+1;                                                        \
226
 
          vh = vy-y+1;                                                        \
227
 
                                                                              \
228
 
          thex = x;                                                           \
229
 
          they = y;                                                           \
230
 
                                                                              \
231
 
          if ((hw*hh) > (vw*vh)) {                                            \
232
 
            thew = hw;                                                        \
233
 
            theh = hh;                                                        \
234
 
          } else {                                                            \
235
 
            thew = vw;                                                        \
236
 
            theh = vh;                                                        \
237
 
          }                                                                   \
238
 
                                                                              \
239
 
          subrect.x = Swap16IfLE(thex);                                       \
240
 
          subrect.y = Swap16IfLE(they);                                       \
241
 
          subrect.w = Swap16IfLE(thew);                                       \
242
 
          subrect.h = Swap16IfLE(theh);                                       \
243
 
                                                                              \
244
 
          newLen = rreAfterBufLen + (bpp/8) + sz_rfbRectangle;                \
245
 
          if ((newLen > (w * h * (bpp/8))) || (newLen > rreAfterBufSize))     \
246
 
            return -1;                                                        \
247
 
                                                                              \
248
 
          numsubs += 1;                                                       \
249
 
          *((uint##bpp##_t*)(rreAfterBuf + rreAfterBufLen)) = cl;                 \
250
 
          rreAfterBufLen += (bpp/8);                                          \
251
 
          memcpy(&rreAfterBuf[rreAfterBufLen],&subrect,sz_rfbRectangle);      \
252
 
          rreAfterBufLen += sz_rfbRectangle;                                  \
253
 
                                                                              \
254
 
          /*                                                                  \
255
 
           * Now mark the subrect as done.                                    \
256
 
           */                                                                 \
257
 
          for (j=they; j < (they+theh); j++) {                                \
258
 
            for (i=thex; i < (thex+thew); i++) {                              \
259
 
              data[j*w+i] = bg;                                               \
260
 
            }                                                                 \
261
 
          }                                                                   \
262
 
        }                                                                     \
263
 
      }                                                                       \
264
 
    }                                                                         \
265
 
                                                                              \
266
 
    return numsubs;                                                           \
267
 
}
268
 
 
269
 
DEFINE_SUBRECT_ENCODE(8)
270
 
DEFINE_SUBRECT_ENCODE(16)
271
 
DEFINE_SUBRECT_ENCODE(32)
272
 
 
273
 
 
274
 
/*
275
 
 * getBgColour() gets the most prevalent colour in a byte array.
276
 
 */
277
 
static uint32_t
278
 
getBgColour(data,size,bpp)
279
 
    char *data;
280
 
    int size;
281
 
    int bpp;
282
 
{
283
 
    
284
 
#define NUMCLRS 256
285
 
  
286
 
  static int counts[NUMCLRS];
287
 
  int i,j,k;
288
 
 
289
 
  int maxcount = 0;
290
 
  uint8_t maxclr = 0;
291
 
 
292
 
  if (bpp != 8) {
293
 
    if (bpp == 16) {
294
 
      return ((uint16_t *)data)[0];
295
 
    } else if (bpp == 32) {
296
 
      return ((uint32_t *)data)[0];
297
 
    } else {
298
 
      rfbLog("getBgColour: bpp %d?\n",bpp);
299
 
      return 0;
300
 
    }
301
 
  }
302
 
 
303
 
  for (i=0; i<NUMCLRS; i++) {
304
 
    counts[i] = 0;
305
 
  }
306
 
 
307
 
  for (j=0; j<size; j++) {
308
 
    k = (int)(((uint8_t *)data)[j]);
309
 
    if (k >= NUMCLRS) {
310
 
      rfbErr("getBgColour: unusual colour = %d\n", k);
311
 
      return 0;
312
 
    }
313
 
    counts[k] += 1;
314
 
    if (counts[k] > maxcount) {
315
 
      maxcount = counts[k];
316
 
      maxclr = ((uint8_t *)data)[j];
317
 
    }
318
 
  }
319
 
  
320
 
  return maxclr;
321
 
}