You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PixelBuffer.cxx 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. // -=- PixelBuffer.cxx
  19. //
  20. // The PixelBuffer class encapsulates the PixelFormat and dimensions
  21. // of a block of pixel data.
  22. #include <rfb/Exception.h>
  23. #include <rfb/LogWriter.h>
  24. #include <rfb/PixelBuffer.h>
  25. using namespace rfb;
  26. using namespace rdr;
  27. static LogWriter vlog("PixelBuffer");
  28. // -=- Generic pixel buffer class
  29. PixelBuffer::PixelBuffer(const PixelFormat& pf, int w, int h, ColourMap* cm)
  30. : format(pf), width_(w), height_(h), colourmap(cm) {}
  31. PixelBuffer::PixelBuffer() : width_(0), height_(0), colourmap(0) {}
  32. PixelBuffer::~PixelBuffer() {}
  33. void PixelBuffer::setPF(const PixelFormat &pf) {format = pf;}
  34. const PixelFormat& PixelBuffer::getPF() const {return format;}
  35. ColourMap* PixelBuffer::getColourMap() const {return colourmap;}
  36. void
  37. PixelBuffer::getImage(void* imageBuf, const Rect& r, int outStride) {
  38. int inStride;
  39. const U8* data = getBuffer(r, &inStride);
  40. // We assume that the specified rectangle is pre-clipped to the buffer
  41. int bytesPerPixel = format.bpp/8;
  42. int inBytesPerRow = inStride * bytesPerPixel;
  43. if (!outStride) outStride = r.width();
  44. int outBytesPerRow = outStride * bytesPerPixel;
  45. int bytesPerMemCpy = r.width() * bytesPerPixel;
  46. U8* imageBufPos = (U8*)imageBuf;
  47. const U8* end = data + (inBytesPerRow * r.height());
  48. while (data < end) {
  49. memcpy(imageBufPos, data, bytesPerMemCpy);
  50. imageBufPos += outBytesPerRow;
  51. data += inBytesPerRow;
  52. }
  53. }
  54. static void fillRect8(U8 *buf, int stride, const Rect& r, Pixel pix)
  55. {
  56. U8* ptr = buf;
  57. int w = r.width(), h = r.height();
  58. while (h > 0) {
  59. memset(ptr, pix, w);
  60. ptr += stride;
  61. h--;
  62. }
  63. }
  64. static void fillRect16(U8 *buf, int stride, const Rect& r, Pixel pix)
  65. {
  66. U16* ptr = (U16 *)buf;
  67. int w = r.width(), h = r.height(), wBytes = w * 2;
  68. while (w > 0) {
  69. *ptr++ = pix; w--;
  70. }
  71. h--;
  72. ptr = (U16 *)buf;
  73. while (h > 0) {
  74. U16 *oldptr = ptr;
  75. memcpy(ptr += stride, oldptr, wBytes);
  76. h--;
  77. }
  78. }
  79. static void fillRect32(U8 *buf, int stride, const Rect& r, Pixel pix)
  80. {
  81. U32* ptr = (U32 *)buf;
  82. int w = r.width(), h = r.height(), wBytes = w * 4;
  83. while (w > 0) {
  84. *ptr++ = pix; w--;
  85. }
  86. h--;
  87. ptr = (U32 *)buf;
  88. while (h > 0) {
  89. U32 *oldptr = ptr;
  90. memcpy(ptr += stride, oldptr, wBytes);
  91. h--;
  92. }
  93. }
  94. FullFramePixelBuffer::FullFramePixelBuffer(const PixelFormat& pf, int w, int h,
  95. rdr::U8* data_, ColourMap* cm)
  96. : PixelBuffer(pf, w, h, cm), data(data_)
  97. {
  98. // Called again to configure the fill function
  99. setPF(pf);
  100. }
  101. FullFramePixelBuffer::FullFramePixelBuffer() : data(0) {}
  102. FullFramePixelBuffer::~FullFramePixelBuffer() {}
  103. void FullFramePixelBuffer::setPF(const PixelFormat &pf) {
  104. // We have this as a separate method for ManagedPixelBuffer's
  105. // sake. Direct users of FullFramePixelBuffer aren't allowed
  106. // to call it.
  107. PixelBuffer::setPF(pf);
  108. switch(pf.bpp) {
  109. case 8:
  110. fillRectFn = fillRect8;
  111. break;
  112. case 16:
  113. fillRectFn = fillRect16;
  114. break;
  115. case 32:
  116. fillRectFn = fillRect32;
  117. break;
  118. default:
  119. throw Exception("rfb::FullFramePixelBuffer - Unsupported pixel format");
  120. }
  121. }
  122. int FullFramePixelBuffer::getStride() const { return width(); }
  123. rdr::U8* FullFramePixelBuffer::getBufferRW(const Rect& r, int* stride)
  124. {
  125. *stride = getStride();
  126. return &data[(r.tl.x + (r.tl.y * *stride)) * format.bpp/8];
  127. }
  128. void FullFramePixelBuffer::fillRect(const Rect& r, Pixel pix) {
  129. int stride;
  130. U8 *buf = getBufferRW(r, &stride);
  131. fillRectFn(buf, stride, r, pix);
  132. }
  133. void FullFramePixelBuffer::imageRect(const Rect& r, const void* pixels, int srcStride) {
  134. int bytesPerPixel = getPF().bpp/8;
  135. int destStride;
  136. U8* dest = getBufferRW(r, &destStride);
  137. int bytesPerDestRow = bytesPerPixel * destStride;
  138. if (!srcStride) srcStride = r.width();
  139. int bytesPerSrcRow = bytesPerPixel * srcStride;
  140. int bytesPerFill = bytesPerPixel * r.width();
  141. const U8* src = (const U8*)pixels;
  142. U8* end = dest + (bytesPerDestRow * r.height());
  143. while (dest < end) {
  144. memcpy(dest, src, bytesPerFill);
  145. dest += bytesPerDestRow;
  146. src += bytesPerSrcRow;
  147. }
  148. }
  149. void FullFramePixelBuffer::maskRect(const Rect& r, const void* pixels, const void* mask_) {
  150. Rect cr = getRect().intersect(r);
  151. if (cr.is_empty()) return;
  152. int stride;
  153. U8* data = getBufferRW(cr, &stride);
  154. U8* mask = (U8*) mask_;
  155. int w = cr.width();
  156. int h = cr.height();
  157. int bpp = getPF().bpp;
  158. int pixelStride = r.width();
  159. int maskStride = (r.width() + 7) / 8;
  160. Point offset = Point(cr.tl.x-r.tl.x, cr.tl.y-r.tl.y);
  161. mask += offset.y * maskStride;
  162. for (int y = 0; y < h; y++) {
  163. int cy = offset.y + y;
  164. for (int x = 0; x < w; x++) {
  165. int cx = offset.x + x;
  166. U8* byte = mask + (cx / 8);
  167. int bit = 7 - cx % 8;
  168. if ((*byte) & (1 << bit)) {
  169. switch (bpp) {
  170. case 8:
  171. ((U8*)data)[y * stride + x] = ((U8*)pixels)[cy * pixelStride + cx];
  172. break;
  173. case 16:
  174. ((U16*)data)[y * stride + x] = ((U16*)pixels)[cy * pixelStride + cx];
  175. break;
  176. case 32:
  177. ((U32*)data)[y * stride + x] = ((U32*)pixels)[cy * pixelStride + cx];
  178. break;
  179. }
  180. }
  181. }
  182. mask += maskStride;
  183. }
  184. }
  185. void FullFramePixelBuffer::maskRect(const Rect& r, Pixel pixel, const void* mask_) {
  186. Rect cr = getRect().intersect(r);
  187. if (cr.is_empty()) return;
  188. int stride;
  189. U8* data = getBufferRW(cr, &stride);
  190. U8* mask = (U8*) mask_;
  191. int w = cr.width();
  192. int h = cr.height();
  193. int bpp = getPF().bpp;
  194. int maskStride = (r.width() + 7) / 8;
  195. Point offset = Point(cr.tl.x-r.tl.x, cr.tl.y-r.tl.y);
  196. mask += offset.y * maskStride;
  197. for (int y = 0; y < h; y++) {
  198. for (int x = 0; x < w; x++) {
  199. int cx = offset.x + x;
  200. U8* byte = mask + (cx / 8);
  201. int bit = 7 - cx % 8;
  202. if ((*byte) & (1 << bit)) {
  203. switch (bpp) {
  204. case 8:
  205. ((U8*)data)[y * stride + x] = pixel;
  206. break;
  207. case 16:
  208. ((U16*)data)[y * stride + x] = pixel;
  209. break;
  210. case 32:
  211. ((U32*)data)[y * stride + x] = pixel;
  212. break;
  213. }
  214. }
  215. }
  216. mask += maskStride;
  217. }
  218. }
  219. void FullFramePixelBuffer::copyRect(const Rect &rect, const Point &move_by_delta) {
  220. int stride;
  221. U8* data;
  222. unsigned int bytesPerPixel, bytesPerRow, bytesPerMemCpy;
  223. Rect drect, srect = rect.translate(move_by_delta.negate());
  224. drect = rect;
  225. if (!drect.enclosed_by(getRect())) {
  226. vlog.error("Destination rect %dx%d at %d,%d exceeds framebuffer %dx%d",
  227. drect.width(), drect.height(), drect.tl.x, drect.tl.y, width_, height_);
  228. drect = drect.intersect(getRect());
  229. }
  230. if (drect.is_empty())
  231. return;
  232. srect = drect.translate(move_by_delta.negate());
  233. if (!srect.enclosed_by(getRect())) {
  234. vlog.error("Source rect %dx%d at %d,%d exceeds framebuffer %dx%d",
  235. srect.width(), srect.height(), srect.tl.x, srect.tl.y, width_, height_);
  236. srect = srect.intersect(getRect());
  237. // Need to readjust the destination now that the area has changed
  238. drect = srect.translate(move_by_delta);
  239. }
  240. if (srect.is_empty())
  241. return;
  242. data = getBufferRW(getRect(), &stride);
  243. bytesPerPixel = getPF().bpp/8;
  244. bytesPerRow = stride * bytesPerPixel;
  245. bytesPerMemCpy = drect.width() * bytesPerPixel;
  246. if (move_by_delta.y <= 0) {
  247. U8* dest = data + drect.tl.x*bytesPerPixel + drect.tl.y*bytesPerRow;
  248. U8* src = data + srect.tl.x*bytesPerPixel + srect.tl.y*bytesPerRow;
  249. for (int i=drect.tl.y; i<drect.br.y; i++) {
  250. memmove(dest, src, bytesPerMemCpy);
  251. dest += bytesPerRow;
  252. src += bytesPerRow;
  253. }
  254. } else {
  255. U8* dest = data + drect.tl.x*bytesPerPixel + (drect.br.y-1)*bytesPerRow;
  256. U8* src = data + srect.tl.x*bytesPerPixel + (srect.br.y-1)*bytesPerRow;
  257. for (int i=drect.tl.y; i<drect.br.y; i++) {
  258. memmove(dest, src, bytesPerMemCpy);
  259. dest -= bytesPerRow;
  260. src -= bytesPerRow;
  261. }
  262. }
  263. }
  264. // -=- Managed pixel buffer class
  265. // Automatically allocates enough space for the specified format & area
  266. ManagedPixelBuffer::ManagedPixelBuffer()
  267. : datasize(0), own_colourmap(false)
  268. {
  269. checkDataSize();
  270. };
  271. ManagedPixelBuffer::ManagedPixelBuffer(const PixelFormat& pf, int w, int h)
  272. : FullFramePixelBuffer(pf, w, h, 0, 0), datasize(0), own_colourmap(false)
  273. {
  274. checkDataSize();
  275. };
  276. ManagedPixelBuffer::~ManagedPixelBuffer() {
  277. if (data) delete [] data;
  278. if (colourmap && own_colourmap) delete colourmap;
  279. };
  280. void
  281. ManagedPixelBuffer::setPF(const PixelFormat &pf) {
  282. FullFramePixelBuffer::setPF(pf); checkDataSize();
  283. };
  284. void
  285. ManagedPixelBuffer::setSize(int w, int h) {
  286. width_ = w; height_ = h; checkDataSize();
  287. };
  288. void
  289. ManagedPixelBuffer::setColourMap(ColourMap* cm, bool own_cm) {
  290. if (colourmap && own_colourmap) delete colourmap;
  291. colourmap = cm;
  292. own_colourmap = own_cm;
  293. }
  294. inline void
  295. ManagedPixelBuffer::checkDataSize() {
  296. unsigned long new_datasize = width_ * height_ * (format.bpp/8);
  297. if (datasize < new_datasize) {
  298. vlog.debug("reallocating managed buffer (%dx%d)", width_, height_);
  299. if (data) {
  300. delete [] data;
  301. datasize = 0; data = 0;
  302. }
  303. if (new_datasize) {
  304. data = new U8[new_datasize];
  305. if (!data)
  306. throw Exception("rfb::ManagedPixelBuffer unable to allocate buffer");
  307. datasize = new_datasize;
  308. }
  309. }
  310. };