選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

PixelBuffer.cxx 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2014 Pierre Ossman for Cendio AB
  3. *
  4. * This is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This software is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this software; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  17. * USA.
  18. */
  19. // -=- PixelBuffer.cxx
  20. //
  21. // The PixelBuffer class encapsulates the PixelFormat and dimensions
  22. // of a block of pixel data.
  23. #include <rfb/Exception.h>
  24. #include <rfb/LogWriter.h>
  25. #include <rfb/PixelBuffer.h>
  26. using namespace rfb;
  27. using namespace rdr;
  28. static LogWriter vlog("PixelBuffer");
  29. // -=- Generic pixel buffer class
  30. PixelBuffer::PixelBuffer(const PixelFormat& pf, int w, int h)
  31. : format(pf), width_(w), height_(h) {}
  32. PixelBuffer::PixelBuffer() : width_(0), height_(0) {}
  33. PixelBuffer::~PixelBuffer() {}
  34. void
  35. PixelBuffer::getImage(void* imageBuf, const Rect& r, int outStride) {
  36. int inStride;
  37. const U8* data = getBuffer(r, &inStride);
  38. // We assume that the specified rectangle is pre-clipped to the buffer
  39. int bytesPerPixel = format.bpp/8;
  40. int inBytesPerRow = inStride * bytesPerPixel;
  41. if (!outStride) outStride = r.width();
  42. int outBytesPerRow = outStride * bytesPerPixel;
  43. int bytesPerMemCpy = r.width() * bytesPerPixel;
  44. U8* imageBufPos = (U8*)imageBuf;
  45. const U8* end = data + (inBytesPerRow * r.height());
  46. while (data < end) {
  47. memcpy(imageBufPos, data, bytesPerMemCpy);
  48. imageBufPos += outBytesPerRow;
  49. data += inBytesPerRow;
  50. }
  51. }
  52. FullFramePixelBuffer::FullFramePixelBuffer(const PixelFormat& pf, int w, int h,
  53. rdr::U8* data_, int stride_)
  54. : PixelBuffer(pf, w, h), data(data_), stride(stride_)
  55. {
  56. }
  57. FullFramePixelBuffer::FullFramePixelBuffer() : data(0) {}
  58. FullFramePixelBuffer::~FullFramePixelBuffer() {}
  59. rdr::U8* FullFramePixelBuffer::getBufferRW(const Rect& r, int* stride_)
  60. {
  61. *stride_ = stride;
  62. return &data[(r.tl.x + (r.tl.y * stride)) * format.bpp/8];
  63. }
  64. void FullFramePixelBuffer::fillRect(const Rect& r, Pixel pix) {
  65. int stride;
  66. U8 *buf, pixbuf[4];
  67. int w, h, b;
  68. buf = getBufferRW(r, &stride);
  69. w = r.width();
  70. h = r.height();
  71. b = format.bpp/8;
  72. format.bufferFromPixel(pixbuf, pix);
  73. while (h--) {
  74. int w_ = w;
  75. while (w_--) {
  76. memcpy(buf, pixbuf, b);
  77. buf += b;
  78. }
  79. buf += (stride - w) * b;
  80. }
  81. }
  82. void FullFramePixelBuffer::imageRect(const Rect& r, const void* pixels, int srcStride) {
  83. int bytesPerPixel = getPF().bpp/8;
  84. int destStride;
  85. U8* dest = getBufferRW(r, &destStride);
  86. int bytesPerDestRow = bytesPerPixel * destStride;
  87. if (!srcStride) srcStride = r.width();
  88. int bytesPerSrcRow = bytesPerPixel * srcStride;
  89. int bytesPerFill = bytesPerPixel * r.width();
  90. const U8* src = (const U8*)pixels;
  91. U8* end = dest + (bytesPerDestRow * r.height());
  92. while (dest < end) {
  93. memcpy(dest, src, bytesPerFill);
  94. dest += bytesPerDestRow;
  95. src += bytesPerSrcRow;
  96. }
  97. }
  98. void FullFramePixelBuffer::maskRect(const Rect& r, const void* pixels, const void* mask_) {
  99. Rect cr = getRect().intersect(r);
  100. if (cr.is_empty()) return;
  101. int stride;
  102. U8* data = getBufferRW(cr, &stride);
  103. U8* mask = (U8*) mask_;
  104. int w = cr.width();
  105. int h = cr.height();
  106. int bpp = getPF().bpp;
  107. int pixelStride = r.width();
  108. int maskStride = (r.width() + 7) / 8;
  109. Point offset = Point(cr.tl.x-r.tl.x, cr.tl.y-r.tl.y);
  110. mask += offset.y * maskStride;
  111. for (int y = 0; y < h; y++) {
  112. int cy = offset.y + y;
  113. for (int x = 0; x < w; x++) {
  114. int cx = offset.x + x;
  115. U8* byte = mask + (cx / 8);
  116. int bit = 7 - cx % 8;
  117. if ((*byte) & (1 << bit)) {
  118. switch (bpp) {
  119. case 8:
  120. ((U8*)data)[y * stride + x] = ((U8*)pixels)[cy * pixelStride + cx];
  121. break;
  122. case 16:
  123. ((U16*)data)[y * stride + x] = ((U16*)pixels)[cy * pixelStride + cx];
  124. break;
  125. case 32:
  126. ((U32*)data)[y * stride + x] = ((U32*)pixels)[cy * pixelStride + cx];
  127. break;
  128. }
  129. }
  130. }
  131. mask += maskStride;
  132. }
  133. }
  134. void FullFramePixelBuffer::maskRect(const Rect& r, Pixel pixel, const void* mask_) {
  135. Rect cr = getRect().intersect(r);
  136. if (cr.is_empty()) return;
  137. int stride;
  138. U8* data = getBufferRW(cr, &stride);
  139. U8* mask = (U8*) mask_;
  140. int w = cr.width();
  141. int h = cr.height();
  142. int bpp = getPF().bpp;
  143. int maskStride = (r.width() + 7) / 8;
  144. Point offset = Point(cr.tl.x-r.tl.x, cr.tl.y-r.tl.y);
  145. mask += offset.y * maskStride;
  146. for (int y = 0; y < h; y++) {
  147. for (int x = 0; x < w; x++) {
  148. int cx = offset.x + x;
  149. U8* byte = mask + (cx / 8);
  150. int bit = 7 - cx % 8;
  151. if ((*byte) & (1 << bit)) {
  152. switch (bpp) {
  153. case 8:
  154. ((U8*)data)[y * stride + x] = pixel;
  155. break;
  156. case 16:
  157. ((U16*)data)[y * stride + x] = pixel;
  158. break;
  159. case 32:
  160. ((U32*)data)[y * stride + x] = pixel;
  161. break;
  162. }
  163. }
  164. }
  165. mask += maskStride;
  166. }
  167. }
  168. void FullFramePixelBuffer::copyRect(const Rect &rect, const Point &move_by_delta) {
  169. int stride;
  170. U8* data;
  171. unsigned int bytesPerPixel, bytesPerRow, bytesPerMemCpy;
  172. Rect drect, srect = rect.translate(move_by_delta.negate());
  173. drect = rect;
  174. if (!drect.enclosed_by(getRect())) {
  175. vlog.error("Destination rect %dx%d at %d,%d exceeds framebuffer %dx%d",
  176. drect.width(), drect.height(), drect.tl.x, drect.tl.y, width_, height_);
  177. drect = drect.intersect(getRect());
  178. }
  179. if (drect.is_empty())
  180. return;
  181. srect = drect.translate(move_by_delta.negate());
  182. if (!srect.enclosed_by(getRect())) {
  183. vlog.error("Source rect %dx%d at %d,%d exceeds framebuffer %dx%d",
  184. srect.width(), srect.height(), srect.tl.x, srect.tl.y, width_, height_);
  185. srect = srect.intersect(getRect());
  186. // Need to readjust the destination now that the area has changed
  187. drect = srect.translate(move_by_delta);
  188. }
  189. if (srect.is_empty())
  190. return;
  191. data = getBufferRW(getRect(), &stride);
  192. bytesPerPixel = getPF().bpp/8;
  193. bytesPerRow = stride * bytesPerPixel;
  194. bytesPerMemCpy = drect.width() * bytesPerPixel;
  195. if (move_by_delta.y <= 0) {
  196. U8* dest = data + drect.tl.x*bytesPerPixel + drect.tl.y*bytesPerRow;
  197. U8* src = data + srect.tl.x*bytesPerPixel + srect.tl.y*bytesPerRow;
  198. for (int i=drect.tl.y; i<drect.br.y; i++) {
  199. memmove(dest, src, bytesPerMemCpy);
  200. dest += bytesPerRow;
  201. src += bytesPerRow;
  202. }
  203. } else {
  204. U8* dest = data + drect.tl.x*bytesPerPixel + (drect.br.y-1)*bytesPerRow;
  205. U8* src = data + srect.tl.x*bytesPerPixel + (srect.br.y-1)*bytesPerRow;
  206. for (int i=drect.tl.y; i<drect.br.y; i++) {
  207. memmove(dest, src, bytesPerMemCpy);
  208. dest -= bytesPerRow;
  209. src -= bytesPerRow;
  210. }
  211. }
  212. }
  213. // -=- Managed pixel buffer class
  214. // Automatically allocates enough space for the specified format & area
  215. ManagedPixelBuffer::ManagedPixelBuffer()
  216. : datasize(0)
  217. {
  218. checkDataSize();
  219. };
  220. ManagedPixelBuffer::ManagedPixelBuffer(const PixelFormat& pf, int w, int h)
  221. : FullFramePixelBuffer(pf, w, h, NULL, w), datasize(0)
  222. {
  223. checkDataSize();
  224. };
  225. ManagedPixelBuffer::~ManagedPixelBuffer() {
  226. if (data) delete [] data;
  227. };
  228. void
  229. ManagedPixelBuffer::setPF(const PixelFormat &pf) {
  230. format = pf; checkDataSize();
  231. };
  232. void
  233. ManagedPixelBuffer::setSize(int w, int h) {
  234. width_ = w; height_ = h; stride = w; checkDataSize();
  235. };
  236. inline void
  237. ManagedPixelBuffer::checkDataSize() {
  238. unsigned long new_datasize = width_ * height_ * (format.bpp/8);
  239. if (datasize < new_datasize) {
  240. vlog.debug("reallocating managed buffer (%dx%d)", width_, height_);
  241. if (data) {
  242. delete [] data;
  243. datasize = 0; data = 0;
  244. }
  245. if (new_datasize) {
  246. data = new U8[new_datasize];
  247. if (!data)
  248. throw Exception("rfb::ManagedPixelBuffer unable to allocate buffer");
  249. datasize = new_datasize;
  250. }
  251. }
  252. };