Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
| 2 | * |
| 3 | * This is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This software is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this software; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 16 | * USA. |
| 17 | */ |
| 18 | |
| 19 | // -=- PixelBuffer.cxx |
| 20 | // |
| 21 | // The PixelBuffer class encapsulates the PixelFormat and dimensions |
| 22 | // of a block of pixel data. |
| 23 | |
| 24 | #include <rfb/Exception.h> |
| 25 | #include <rfb/LogWriter.h> |
| 26 | #include <rfb/PixelBuffer.h> |
| 27 | |
| 28 | using namespace rfb; |
| 29 | using namespace rdr; |
| 30 | |
| 31 | static LogWriter vlog("PixelBuffer"); |
| 32 | |
| 33 | |
| 34 | // -=- Generic pixel buffer class |
| 35 | |
| 36 | PixelBuffer::PixelBuffer(const PixelFormat& pf, int w, int h, ColourMap* cm) |
| 37 | : format(pf), width_(w), height_(h), colourmap(cm) {} |
| 38 | PixelBuffer::PixelBuffer() : width_(0), height_(0), colourmap(0) {} |
| 39 | |
| 40 | PixelBuffer::~PixelBuffer() {} |
| 41 | |
| 42 | |
| 43 | void PixelBuffer::setPF(const PixelFormat &pf) {format = pf;} |
| 44 | const PixelFormat& PixelBuffer::getPF() const {return format;} |
| 45 | ColourMap* PixelBuffer::getColourMap() const {return colourmap;} |
| 46 | |
| 47 | |
| 48 | void |
| 49 | PixelBuffer::getImage(void* imageBuf, const Rect& r, int outStride) { |
| 50 | int inStride; |
| 51 | const U8* data = getPixelsR(r, &inStride); |
| 52 | // We assume that the specified rectangle is pre-clipped to the buffer |
| 53 | int bytesPerPixel = format.bpp/8; |
| 54 | int inBytesPerRow = inStride * bytesPerPixel; |
| 55 | if (!outStride) outStride = r.width(); |
| 56 | int outBytesPerRow = outStride * bytesPerPixel; |
| 57 | int bytesPerMemCpy = r.width() * bytesPerPixel; |
| 58 | U8* imageBufPos = (U8*)imageBuf; |
| 59 | const U8* end = data + (inBytesPerRow * r.height()); |
| 60 | while (data < end) { |
| 61 | memcpy(imageBufPos, data, bytesPerMemCpy); |
| 62 | imageBufPos += outBytesPerRow; |
| 63 | data += inBytesPerRow; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /* *** |
| 68 | Pixel PixelBuffer::getPixel(const Point& p) { |
| 69 | int stride; |
| 70 | Rect r = Rect(p.x, p.y, p.x+1, p.y+1); |
| 71 | switch(format.bpp) { |
| 72 | case 8: return *((rdr::U8*)getDataAt(r, &stride)); |
| 73 | case 16: return *((rdr::U16*)getDataAt(r, &stride)); |
| 74 | case 32: return *((rdr::U32*)getDataAt(r, &stride)); |
| 75 | default: return 0; |
| 76 | }; |
| 77 | } |
| 78 | */ |
| 79 | |
| 80 | |
DRC | 4f24c1a | 2011-11-03 23:56:10 +0000 | [diff] [blame] | 81 | static void fillRect8(U8 *buf, int stride, const Rect& r, Pixel pix) |
| 82 | { |
| 83 | U8* ptr = buf; |
| 84 | int w = r.width(), h = r.height(); |
| 85 | |
| 86 | while (h > 0) { |
| 87 | memset(ptr, pix, w); |
| 88 | ptr += stride; |
| 89 | h--; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | static void fillRect16(U8 *buf, int stride, const Rect& r, Pixel pix) |
| 94 | { |
| 95 | U16* ptr = (U16 *)buf; |
| 96 | int w = r.width(), h = r.height(), wBytes = w * 2; |
| 97 | |
| 98 | while (w > 0) { |
| 99 | *ptr++ = pix; w--; |
| 100 | } |
| 101 | h--; |
| 102 | |
| 103 | ptr = (U16 *)buf; |
| 104 | |
| 105 | while (h > 0) { |
| 106 | U16 *oldptr = ptr; |
| 107 | memcpy(ptr += stride, oldptr, wBytes); |
| 108 | h--; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | static void fillRect32(U8 *buf, int stride, const Rect& r, Pixel pix) |
| 113 | { |
| 114 | U32* ptr = (U32 *)buf; |
| 115 | int w = r.width(), h = r.height(), wBytes = w * 4; |
| 116 | |
| 117 | while (w > 0) { |
| 118 | *ptr++ = pix; w--; |
| 119 | } |
| 120 | h--; |
| 121 | |
| 122 | ptr = (U32 *)buf; |
| 123 | |
| 124 | while (h > 0) { |
| 125 | U32 *oldptr = ptr; |
| 126 | memcpy(ptr += stride, oldptr, wBytes); |
| 127 | h--; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 132 | FullFramePixelBuffer::FullFramePixelBuffer(const PixelFormat& pf, int w, int h, |
| 133 | rdr::U8* data_, ColourMap* cm) |
| 134 | : PixelBuffer(pf, w, h, cm), data(data_) |
| 135 | { |
DRC | 4f24c1a | 2011-11-03 23:56:10 +0000 | [diff] [blame] | 136 | switch(pf.bpp) { |
| 137 | case 8: |
| 138 | fillRectFn = fillRect8; |
| 139 | break; |
| 140 | case 16: |
| 141 | fillRectFn = fillRect16; |
| 142 | break; |
| 143 | case 32: |
| 144 | fillRectFn = fillRect32; |
| 145 | break; |
| 146 | default: |
| 147 | throw Exception("rfb::FullFramePixelBuffer - Unsupported pixel format"); |
| 148 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | FullFramePixelBuffer::FullFramePixelBuffer() : data(0) {} |
| 152 | |
| 153 | FullFramePixelBuffer::~FullFramePixelBuffer() {} |
| 154 | |
| 155 | |
| 156 | int FullFramePixelBuffer::getStride() const { return width(); } |
| 157 | |
| 158 | rdr::U8* FullFramePixelBuffer::getPixelsRW(const Rect& r, int* stride) |
| 159 | { |
| 160 | *stride = getStride(); |
| 161 | return &data[(r.tl.x + (r.tl.y * *stride)) * format.bpp/8]; |
| 162 | } |
| 163 | |
| 164 | |
| 165 | void FullFramePixelBuffer::fillRect(const Rect& r, Pixel pix) { |
| 166 | int stride; |
DRC | 4f24c1a | 2011-11-03 23:56:10 +0000 | [diff] [blame] | 167 | U8 *buf = getPixelsRW(r, &stride); |
| 168 | fillRectFn(buf, stride, r, pix); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | void FullFramePixelBuffer::imageRect(const Rect& r, const void* pixels, int srcStride) { |
| 172 | int bytesPerPixel = getPF().bpp/8; |
| 173 | int destStride; |
| 174 | U8* dest = getPixelsRW(r, &destStride); |
| 175 | int bytesPerDestRow = bytesPerPixel * destStride; |
| 176 | if (!srcStride) srcStride = r.width(); |
| 177 | int bytesPerSrcRow = bytesPerPixel * srcStride; |
| 178 | int bytesPerFill = bytesPerPixel * r.width(); |
| 179 | const U8* src = (const U8*)pixels; |
| 180 | U8* end = dest + (bytesPerDestRow * r.height()); |
| 181 | while (dest < end) { |
| 182 | memcpy(dest, src, bytesPerFill); |
| 183 | dest += bytesPerDestRow; |
| 184 | src += bytesPerSrcRow; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | void FullFramePixelBuffer::maskRect(const Rect& r, const void* pixels, const void* mask_) { |
| 189 | Rect cr = getRect().intersect(r); |
| 190 | if (cr.is_empty()) return; |
| 191 | int stride; |
| 192 | U8* data = getPixelsRW(cr, &stride); |
| 193 | U8* mask = (U8*) mask_; |
| 194 | int w = cr.width(); |
| 195 | int h = cr.height(); |
| 196 | int bpp = getPF().bpp; |
| 197 | int pixelStride = r.width(); |
| 198 | int maskStride = (r.width() + 7) / 8; |
| 199 | |
| 200 | Point offset = Point(cr.tl.x-r.tl.x, cr.tl.y-r.tl.y); |
| 201 | mask += offset.y * maskStride; |
| 202 | for (int y = 0; y < h; y++) { |
| 203 | int cy = offset.y + y; |
| 204 | for (int x = 0; x < w; x++) { |
| 205 | int cx = offset.x + x; |
| 206 | U8* byte = mask + (cx / 8); |
| 207 | int bit = 7 - cx % 8; |
| 208 | if ((*byte) & (1 << bit)) { |
| 209 | switch (bpp) { |
| 210 | case 8: |
| 211 | ((U8*)data)[y * stride + x] = ((U8*)pixels)[cy * pixelStride + cx]; |
| 212 | break; |
| 213 | case 16: |
| 214 | ((U16*)data)[y * stride + x] = ((U16*)pixels)[cy * pixelStride + cx]; |
| 215 | break; |
| 216 | case 32: |
| 217 | ((U32*)data)[y * stride + x] = ((U32*)pixels)[cy * pixelStride + cx]; |
| 218 | break; |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | mask += maskStride; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | void FullFramePixelBuffer::maskRect(const Rect& r, Pixel pixel, const void* mask_) { |
| 227 | Rect cr = getRect().intersect(r); |
| 228 | if (cr.is_empty()) return; |
| 229 | int stride; |
| 230 | U8* data = getPixelsRW(cr, &stride); |
| 231 | U8* mask = (U8*) mask_; |
| 232 | int w = cr.width(); |
| 233 | int h = cr.height(); |
| 234 | int bpp = getPF().bpp; |
| 235 | int maskStride = (r.width() + 7) / 8; |
| 236 | |
| 237 | Point offset = Point(cr.tl.x-r.tl.x, cr.tl.y-r.tl.y); |
| 238 | mask += offset.y * maskStride; |
| 239 | for (int y = 0; y < h; y++) { |
| 240 | for (int x = 0; x < w; x++) { |
| 241 | int cx = offset.x + x; |
| 242 | U8* byte = mask + (cx / 8); |
| 243 | int bit = 7 - cx % 8; |
| 244 | if ((*byte) & (1 << bit)) { |
| 245 | switch (bpp) { |
| 246 | case 8: |
| 247 | ((U8*)data)[y * stride + x] = pixel; |
| 248 | break; |
| 249 | case 16: |
| 250 | ((U16*)data)[y * stride + x] = pixel; |
| 251 | break; |
| 252 | case 32: |
| 253 | ((U32*)data)[y * stride + x] = pixel; |
| 254 | break; |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | mask += maskStride; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | void FullFramePixelBuffer::copyRect(const Rect &rect, const Point &move_by_delta) { |
| 263 | int stride; |
Pierre Ossman | 45de20d | 2010-12-21 15:53:42 +0000 | [diff] [blame] | 264 | U8* data; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 265 | unsigned int bytesPerPixel, bytesPerRow, bytesPerMemCpy; |
Pierre Ossman | 45de20d | 2010-12-21 15:53:42 +0000 | [diff] [blame] | 266 | Rect drect, srect = rect.translate(move_by_delta.negate()); |
| 267 | |
| 268 | drect = rect; |
| 269 | if (!drect.enclosed_by(getRect())) { |
| 270 | vlog.error("Destination rect %dx%d at %d,%d exceeds framebuffer %dx%d", |
| 271 | drect.width(), drect.height(), drect.tl.x, drect.tl.y, width_, height_); |
| 272 | drect = drect.intersect(getRect()); |
| 273 | } |
| 274 | |
| 275 | if (drect.is_empty()) |
| 276 | return; |
| 277 | |
| 278 | srect = drect.translate(move_by_delta.negate()); |
| 279 | if (!srect.enclosed_by(getRect())) { |
| 280 | vlog.error("Source rect %dx%d at %d,%d exceeds framebuffer %dx%d", |
| 281 | srect.width(), srect.height(), srect.tl.x, srect.tl.y, width_, height_); |
| 282 | srect = srect.intersect(getRect()); |
| 283 | // Need to readjust the destination now that the area has changed |
| 284 | drect = srect.translate(move_by_delta); |
| 285 | } |
| 286 | |
| 287 | if (srect.is_empty()) |
| 288 | return; |
| 289 | |
| 290 | data = getPixelsRW(getRect(), &stride); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 291 | bytesPerPixel = getPF().bpp/8; |
| 292 | bytesPerRow = stride * bytesPerPixel; |
Pierre Ossman | 45de20d | 2010-12-21 15:53:42 +0000 | [diff] [blame] | 293 | bytesPerMemCpy = drect.width() * bytesPerPixel; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 294 | if (move_by_delta.y <= 0) { |
Pierre Ossman | 45de20d | 2010-12-21 15:53:42 +0000 | [diff] [blame] | 295 | U8* dest = data + drect.tl.x*bytesPerPixel + drect.tl.y*bytesPerRow; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 296 | U8* src = data + srect.tl.x*bytesPerPixel + srect.tl.y*bytesPerRow; |
Pierre Ossman | 45de20d | 2010-12-21 15:53:42 +0000 | [diff] [blame] | 297 | for (int i=drect.tl.y; i<drect.br.y; i++) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 298 | memmove(dest, src, bytesPerMemCpy); |
| 299 | dest += bytesPerRow; |
| 300 | src += bytesPerRow; |
| 301 | } |
| 302 | } else { |
Pierre Ossman | 45de20d | 2010-12-21 15:53:42 +0000 | [diff] [blame] | 303 | U8* dest = data + drect.tl.x*bytesPerPixel + (drect.br.y-1)*bytesPerRow; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 304 | U8* src = data + srect.tl.x*bytesPerPixel + (srect.br.y-1)*bytesPerRow; |
Pierre Ossman | 45de20d | 2010-12-21 15:53:42 +0000 | [diff] [blame] | 305 | for (int i=drect.tl.y; i<drect.br.y; i++) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 306 | memmove(dest, src, bytesPerMemCpy); |
| 307 | dest -= bytesPerRow; |
| 308 | src -= bytesPerRow; |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | |
| 314 | // -=- Managed pixel buffer class |
| 315 | // Automatically allocates enough space for the specified format & area |
| 316 | |
| 317 | ManagedPixelBuffer::ManagedPixelBuffer() |
| 318 | : datasize(0), own_colourmap(false) |
| 319 | { |
| 320 | checkDataSize(); |
| 321 | }; |
| 322 | |
| 323 | ManagedPixelBuffer::ManagedPixelBuffer(const PixelFormat& pf, int w, int h) |
| 324 | : FullFramePixelBuffer(pf, w, h, 0, 0), datasize(0), own_colourmap(false) |
| 325 | { |
| 326 | checkDataSize(); |
| 327 | }; |
| 328 | |
| 329 | ManagedPixelBuffer::~ManagedPixelBuffer() { |
| 330 | if (data) delete [] data; |
| 331 | if (colourmap && own_colourmap) delete colourmap; |
| 332 | }; |
| 333 | |
| 334 | |
| 335 | void |
| 336 | ManagedPixelBuffer::setPF(const PixelFormat &pf) { |
| 337 | format = pf; checkDataSize(); |
| 338 | }; |
| 339 | void |
| 340 | ManagedPixelBuffer::setSize(int w, int h) { |
| 341 | width_ = w; height_ = h; checkDataSize(); |
| 342 | }; |
| 343 | |
| 344 | |
| 345 | void |
| 346 | ManagedPixelBuffer::setColourMap(ColourMap* cm, bool own_cm) { |
| 347 | if (colourmap && own_colourmap) delete colourmap; |
| 348 | colourmap = cm; |
| 349 | own_colourmap = own_cm; |
| 350 | } |
| 351 | |
| 352 | inline void |
| 353 | ManagedPixelBuffer::checkDataSize() { |
| 354 | unsigned long new_datasize = width_ * height_ * (format.bpp/8); |
| 355 | if (datasize < new_datasize) { |
| 356 | vlog.debug("reallocating managed buffer (%dx%d)", width_, height_); |
| 357 | if (data) { |
| 358 | delete [] data; |
| 359 | datasize = 0; data = 0; |
| 360 | } |
| 361 | if (new_datasize) { |
| 362 | data = new U8[new_datasize]; |
| 363 | if (!data) |
| 364 | throw Exception("rfb::ManagedPixelBuffer unable to allocate buffer"); |
| 365 | datasize = new_datasize; |
| 366 | } |
| 367 | } |
| 368 | }; |