summaryrefslogtreecommitdiffstats
path: root/common/rfb/PixelBuffer.cxx
blob: 60957a25dbed854f15591ce3a880d5402ddffef1 (plain)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/* Copyright (C) 2002-2005 RealVNC Ltd.  All Rights Reserved.
 * 
 * This 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 software 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 software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 * USA.
 */

// -=- PixelBuffer.cxx
//
// The PixelBuffer class encapsulates the PixelFormat and dimensions
// of a block of pixel data.

#include <rfb/Exception.h>
#include <rfb/LogWriter.h>
#include <rfb/PixelBuffer.h>

using namespace rfb;
using namespace rdr;

static LogWriter vlog("PixelBuffer");


// -=- Generic pixel buffer class

PixelBuffer::PixelBuffer(const PixelFormat& pf, int w, int h, ColourMap* cm)
  : format(pf), width_(w), height_(h), colourmap(cm) {}
PixelBuffer::PixelBuffer() : width_(0), height_(0), colourmap(0) {}

PixelBuffer::~PixelBuffer() {}


void PixelBuffer::setPF(const PixelFormat &pf) {format = pf;}
const PixelFormat& PixelBuffer::getPF() const {return format;}
ColourMap* PixelBuffer::getColourMap() const {return colourmap;}


void
PixelBuffer::getImage(void* imageBuf, const Rect& r, int outStride) {
  int inStride;
  const U8* data = getPixelsR(r, &inStride);
  // We assume that the specified rectangle is pre-clipped to the buffer
  int bytesPerPixel = format.bpp/8;
  int inBytesPerRow = inStride * bytesPerPixel;
  if (!outStride) outStride = r.width();
  int outBytesPerRow = outStride * bytesPerPixel;
  int bytesPerMemCpy = r.width() * bytesPerPixel;
  U8* imageBufPos = (U8*)imageBuf;
  const U8* end = data + (inBytesPerRow * r.height());
  while (data < end) {
    memcpy(imageBufPos, data, bytesPerMemCpy);
    imageBufPos += outBytesPerRow;
    data += inBytesPerRow;
  }
}

/* ***
Pixel PixelBuffer::getPixel(const Point& p) {
  int stride;
  Rect r = Rect(p.x, p.y, p.x+1, p.y+1);
  switch(format.bpp) {
  case 8: return *((rdr::U8*)getDataAt(r, &stride));
  case 16: return *((rdr::U16*)getDataAt(r, &stride));
  case 32: return *((rdr::U32*)getDataAt(r, &stride));
  default: return 0;
  };
}
*/


static void fillRect8(U8 *buf, int stride, const Rect& r, Pixel pix)
{
  U8* ptr = buf;
  int w = r.width(), h = r.height();

  while (h > 0) {
    memset(ptr, pix, w);
    ptr += stride;
    h--;
  }
}

static void fillRect16(U8 *buf, int stride, const Rect& r, Pixel pix)
{
  U16* ptr = (U16 *)buf;
  int w = r.width(), h = r.height(), wBytes = w * 2;

  while (w > 0) {
    *ptr++ = pix;  w--;
  }
  h--;

  ptr = (U16 *)buf;

  while (h > 0) {
    U16 *oldptr = ptr;
    memcpy(ptr += stride, oldptr, wBytes);
    h--;
  }
}

static void fillRect32(U8 *buf, int stride, const Rect& r, Pixel pix)
{
  U32* ptr = (U32 *)buf;
  int w = r.width(), h = r.height(), wBytes = w * 4;

  while (w > 0) {
    *ptr++ = pix;  w--;
  }
  h--;

  ptr = (U32 *)buf;

  while (h > 0) {
    U32 *oldptr = ptr;
    memcpy(ptr += stride, oldptr, wBytes);
    h--;
  }
}


FullFramePixelBuffer::FullFramePixelBuffer(const PixelFormat& pf, int w, int h,
                                           rdr::U8* data_, ColourMap* cm)
  : PixelBuffer(pf, w, h, cm), data(data_)
{
  // Called again to configure the fill function
  setPF(pf);
}

FullFramePixelBuffer::FullFramePixelBuffer() : data(0) {}

FullFramePixelBuffer::~FullFramePixelBuffer() {}


void FullFramePixelBuffer::setPF(const PixelFormat &pf) {
  // We have this as a separate method for ManagedPixelBuffer's
  // sake. Direct users of FullFramePixelBuffer aren't allowed
  // to call it.

  PixelBuffer::setPF(pf);

  switch(pf.bpp) {
  case 8:
    fillRectFn = fillRect8;
    break;
  case 16:
    fillRectFn = fillRect16;
    break;
  case 32:
    fillRectFn = fillRect32;
    break;
  default:
    throw Exception("rfb::FullFramePixelBuffer - Unsupported pixel format");
  }
}


int FullFramePixelBuffer::getStride() const { return width(); }

rdr::U8* FullFramePixelBuffer::getPixelsRW(const Rect& r, int* stride)
{
  *stride = getStride();
  return &data[(r.tl.x + (r.tl.y * *stride)) * format.bpp/8];
}


void FullFramePixelBuffer::fillRect(const Rect& r, Pixel pix) {
  int stride;
  U8 *buf = getPixelsRW(r, &stride);
  fillRectFn(buf, stride, r, pix);
}

void FullFramePixelBuffer::imageRect(const Rect& r, const void* pixels, int srcStride) {
  int bytesPerPixel = getPF().bpp/8;
  int destStride;
  U8* dest = getPixelsRW(r, &destStride);
  int bytesPerDestRow = bytesPerPixel * destStride;
  if (!srcStride) srcStride = r.width();
  int bytesPerSrcRow = bytesPerPixel * srcStride;
  int bytesPerFill = bytesPerPixel * r.width();
  const U8* src = (const U8*)pixels;
  U8* end = dest + (bytesPerDestRow * r.height());
  while (dest < end) {
    memcpy(dest, src, bytesPerFill);
    dest += bytesPerDestRow;
    src += bytesPerSrcRow;
  }
}

void FullFramePixelBuffer::maskRect(const Rect& r, const void* pixels, const void* mask_) {
  Rect cr = getRect().intersect(r);
  if (cr.is_empty()) return;
  int stride;
  U8* data = getPixelsRW(cr, &stride);
  U8* mask = (U8*) mask_;
  int w = cr.width();
  int h = cr.height();
  int bpp = getPF().bpp;
  int pixelStride = r.width();
  int maskStride = (r.width() + 7) / 8;

  Point offset = Point(cr.tl.x-r.tl.x, cr.tl.y-r.tl.y);
  mask += offset.y * maskStride;
  for (int y = 0; y < h; y++) {
    int cy = offset.y + y;
    for (int x = 0; x < w; x++) {
      int cx = offset.x + x;
      U8* byte = mask + (cx / 8);
      int bit = 7 - cx % 8;
      if ((*byte) & (1 << bit)) {
        switch (bpp) {
        case 8:
          ((U8*)data)[y * stride + x] = ((U8*)pixels)[cy * pixelStride + cx];
          break;
        case 16:
          ((U16*)data)[y * stride + x] = ((U16*)pixels)[cy * pixelStride + cx];
          break;
        case 32:
          ((U32*)data)[y * stride + x] = ((U32*)pixels)[cy * pixelStride + cx];
          break;
        }
      }
    }
    mask += maskStride;
  }
}

void FullFramePixelBuffer::maskRect(const Rect& r, Pixel pixel, const void* mask_) {
  Rect cr = getRect().intersect(r);
  if (cr.is_empty()) return;
  int stride;
  U8* data = getPixelsRW(cr, &stride);
  U8* mask = (U8*) mask_;
  int w = cr.width();
  int h = cr.height();
  int bpp = getPF().bpp;
  int maskStride = (r.width() + 7) / 8;

  Point offset = Point(cr.tl.x-r.tl.x, cr.tl.y-r.tl.y);
  mask += offset.y * maskStride;
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      int cx = offset.x + x;
      U8* byte = mask + (cx / 8);
      int bit = 7 - cx % 8;
      if ((*byte) & (1 << bit)) {
        switch (bpp) {
        case 8:
          ((U8*)data)[y * stride + x] = pixel;
          break;
        case 16:
          ((U16*)data)[y * stride + x] = pixel;
          break;
        case 32:
          ((U32*)data)[y * stride + x] = pixel;
          break;
        }
      }
    }
    mask += maskStride;
  }
}

void FullFramePixelBuffer::copyRect(const Rect &rect, const Point &move_by_delta) {
  int stride;
  U8* data;
  unsigned int bytesPerPixel, bytesPerRow, bytesPerMemCpy;
  Rect drect, srect = rect.translate(move_by_delta.negate());

  drect = rect;
  if (!drect.enclosed_by(getRect())) {
    vlog.error("Destination rect %dx%d at %d,%d exceeds framebuffer %dx%d",
               drect.width(), drect.height(), drect.tl.x, drect.tl.y, width_, height_);
    drect = drect.intersect(getRect());
  }

  if (drect.is_empty())
    return;

  srect = drect.translate(move_by_delta.negate());
  if (!srect.enclosed_by(getRect())) {
    vlog.error("Source rect %dx%d at %d,%d exceeds framebuffer %dx%d",
               srect.width(), srect.height(), srect.tl.x, srect.tl.y, width_, height_);
    srect = srect.intersect(getRect());
    // Need to readjust the destination now that the area has changed
    drect = srect.translate(move_by_delta);
  }

  if (srect.is_empty())
    return;

  data = getPixelsRW(getRect(), &stride);
  bytesPerPixel = getPF().bpp/8;
  bytesPerRow = stride * bytesPerPixel;
  bytesPerMemCpy = drect.width() * bytesPerPixel;
  if (move_by_delta.y <= 0) {
    U8* dest = data + drect.tl.x*bytesPerPixel + drect.tl.y*bytesPerRow;
    U8* src = data + srect.tl.x*bytesPerPixel + srect.tl.y*bytesPerRow;
    for (int i=drect.tl.y; i<drect.br.y; i++) {
      memmove(dest, src, bytesPerMemCpy);
      dest += bytesPerRow;
      src += bytesPerRow;
    }
  } else {
    U8* dest = data + drect.tl.x*bytesPerPixel + (drect.br.y-1)*bytesPerRow;
    U8* src = data + srect.tl.x*bytesPerPixel + (srect.br.y-1)*bytesPerRow;
    for (int i=drect.tl.y; i<drect.br.y; i++) {
      memmove(dest, src, bytesPerMemCpy);
      dest -= bytesPerRow;
      src -= bytesPerRow;
    }
  }
}


// -=- Managed pixel buffer class
// Automatically allocates enough space for the specified format & area

ManagedPixelBuffer::ManagedPixelBuffer()
  : datasize(0), own_colourmap(false)
{
  checkDataSize();
};

ManagedPixelBuffer::ManagedPixelBuffer(const PixelFormat& pf, int w, int h)
  : FullFramePixelBuffer(pf, w, h, 0, 0), datasize(0), own_colourmap(false)
{
  checkDataSize();
};

ManagedPixelBuffer::~ManagedPixelBuffer() {
  if (data) delete [] data;
  if (colourmap && own_colourmap) delete colourmap;
};


void
ManagedPixelBuffer::setPF(const PixelFormat &pf) {
  FullFramePixelBuffer::setPF(pf); checkDataSize();
};
void
ManagedPixelBuffer::setSize(int w, int h) {
  width_ = w; height_ = h; checkDataSize();
};


void
ManagedPixelBuffer::setColourMap(ColourMap* cm, bool own_cm) {
  if (colourmap && own_colourmap) delete colourmap;
  colourmap = cm;
  own_colourmap = own_cm;
}

inline void
ManagedPixelBuffer::checkDataSize() {
  unsigned long new_datasize = width_ * height_ * (format.bpp/8);
  if (datasize < new_datasize) {
    vlog.debug("reallocating managed buffer (%dx%d)", width_, height_);
    if (data) {
      delete [] data;
      datasize = 0; data = 0;
    }
    if (new_datasize) {
      data = new U8[new_datasize];
      if (!data)
        throw Exception("rfb::ManagedPixelBuffer unable to allocate buffer");
      datasize = new_datasize;
    }
  }
};