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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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) const
  36. {
  37. int inStride;
  38. const U8* data;
  39. int bytesPerPixel, inBytesPerRow, outBytesPerRow, bytesPerMemCpy;
  40. U8* imageBufPos;
  41. const U8* end;
  42. if (!r.enclosed_by(getRect()))
  43. throw rfb::Exception("Source rect %dx%d at %d,%d exceeds framebuffer %dx%d",
  44. r.width(), r.height(),
  45. r.tl.x, r.tl.y, width_, height_);
  46. data = getBuffer(r, &inStride);
  47. bytesPerPixel = format.bpp/8;
  48. inBytesPerRow = inStride * bytesPerPixel;
  49. if (!outStride)
  50. outStride = r.width();
  51. outBytesPerRow = outStride * bytesPerPixel;
  52. bytesPerMemCpy = r.width() * bytesPerPixel;
  53. imageBufPos = (U8*)imageBuf;
  54. end = data + (inBytesPerRow * r.height());
  55. while (data < end) {
  56. memcpy(imageBufPos, data, bytesPerMemCpy);
  57. imageBufPos += outBytesPerRow;
  58. data += inBytesPerRow;
  59. }
  60. }
  61. void PixelBuffer::getImage(const PixelFormat& pf, void* imageBuf,
  62. const Rect& r, int stride) const
  63. {
  64. const rdr::U8* srcBuffer;
  65. int srcStride;
  66. if (format.equal(pf)) {
  67. getImage(imageBuf, r, stride);
  68. return;
  69. }
  70. if (!r.enclosed_by(getRect()))
  71. throw rfb::Exception("Source rect %dx%d at %d,%d exceeds framebuffer %dx%d",
  72. r.width(), r.height(),
  73. r.tl.x, r.tl.y, width_, height_);
  74. if (stride == 0)
  75. stride = r.width();
  76. srcBuffer = getBuffer(r, &srcStride);
  77. pf.bufferFromBuffer((U8*)imageBuf, format, srcBuffer, r.width(), r.height(),
  78. stride, srcStride);
  79. }
  80. // -=- Modifiable generic pixel buffer class
  81. ModifiablePixelBuffer::ModifiablePixelBuffer(const PixelFormat& pf,
  82. int w, int h)
  83. : PixelBuffer(pf, w, h)
  84. {
  85. }
  86. ModifiablePixelBuffer::ModifiablePixelBuffer()
  87. {
  88. }
  89. ModifiablePixelBuffer::~ModifiablePixelBuffer()
  90. {
  91. }
  92. void ModifiablePixelBuffer::fillRect(const Rect& r, const void* pix)
  93. {
  94. int stride;
  95. U8 *buf;
  96. int w, h, b;
  97. if (!r.enclosed_by(getRect()))
  98. throw rfb::Exception("Destination rect %dx%d at %d,%d exceeds framebuffer %dx%d",
  99. r.width(), r.height(), r.tl.x, r.tl.y, width_, height_);
  100. w = r.width();
  101. h = r.height();
  102. b = format.bpp/8;
  103. if (h == 0)
  104. return;
  105. buf = getBufferRW(r, &stride);
  106. if (b == 1) {
  107. while (h--) {
  108. memset(buf, *(const U8*)pix, w);
  109. buf += stride * b;
  110. }
  111. } else {
  112. U8 *start;
  113. int w1;
  114. start = buf;
  115. w1 = w;
  116. while (w1--) {
  117. memcpy(buf, pix, b);
  118. buf += b;
  119. }
  120. buf += (stride - w) * b;
  121. h--;
  122. while (h--) {
  123. memcpy(buf, start, w * b);
  124. buf += stride * b;
  125. }
  126. }
  127. commitBufferRW(r);
  128. }
  129. void ModifiablePixelBuffer::imageRect(const Rect& r,
  130. const void* pixels, int srcStride)
  131. {
  132. U8* dest;
  133. int destStride;
  134. int bytesPerPixel, bytesPerDestRow, bytesPerSrcRow, bytesPerFill;
  135. const U8* src;
  136. U8* end;
  137. if (!r.enclosed_by(getRect()))
  138. throw rfb::Exception("Destination rect %dx%d at %d,%d exceeds framebuffer %dx%d",
  139. r.width(), r.height(),
  140. r.tl.x, r.tl.y, width_, height_);
  141. bytesPerPixel = getPF().bpp/8;
  142. dest = getBufferRW(r, &destStride);
  143. bytesPerDestRow = bytesPerPixel * destStride;
  144. if (!srcStride)
  145. srcStride = r.width();
  146. bytesPerSrcRow = bytesPerPixel * srcStride;
  147. bytesPerFill = bytesPerPixel * r.width();
  148. src = (const U8*)pixels;
  149. end = dest + (bytesPerDestRow * r.height());
  150. while (dest < end) {
  151. memcpy(dest, src, bytesPerFill);
  152. dest += bytesPerDestRow;
  153. src += bytesPerSrcRow;
  154. }
  155. commitBufferRW(r);
  156. }
  157. void ModifiablePixelBuffer::copyRect(const Rect &rect,
  158. const Point &move_by_delta)
  159. {
  160. int srcStride, dstStride;
  161. int bytesPerPixel;
  162. const U8* srcData;
  163. U8* dstData;
  164. Rect drect, srect;
  165. drect = rect;
  166. if (!drect.enclosed_by(getRect()))
  167. throw rfb::Exception("Destination rect %dx%d at %d,%d exceeds framebuffer %dx%d",
  168. drect.width(), drect.height(),
  169. drect.tl.x, drect.tl.y, width_, height_);
  170. srect = drect.translate(move_by_delta.negate());
  171. if (!srect.enclosed_by(getRect()))
  172. throw rfb::Exception("Source rect %dx%d at %d,%d exceeds framebuffer %dx%d",
  173. srect.width(), srect.height(),
  174. srect.tl.x, srect.tl.y, width_, height_);
  175. bytesPerPixel = format.bpp/8;
  176. srcData = getBuffer(srect, &srcStride);
  177. dstData = getBufferRW(drect, &dstStride);
  178. if (move_by_delta.y == 0) {
  179. // Possible overlap. Be careful and use memmove().
  180. int h = drect.height();
  181. while (h--) {
  182. memmove(dstData, srcData, drect.width() * bytesPerPixel);
  183. dstData += dstStride * bytesPerPixel;
  184. srcData += srcStride * bytesPerPixel;
  185. }
  186. } else if (move_by_delta.y < 0) {
  187. // The data shifted upwards. Copy from top to bottom.
  188. int h = drect.height();
  189. while (h--) {
  190. memcpy(dstData, srcData, drect.width() * bytesPerPixel);
  191. dstData += dstStride * bytesPerPixel;
  192. srcData += srcStride * bytesPerPixel;
  193. }
  194. } else {
  195. // The data shifted downwards. Copy from bottom to top.
  196. int h = drect.height();
  197. dstData += (h-1) * dstStride * bytesPerPixel;
  198. srcData += (h-1) * srcStride * bytesPerPixel;
  199. while (h--) {
  200. memcpy(dstData, srcData, drect.width() * bytesPerPixel);
  201. dstData -= dstStride * bytesPerPixel;
  202. srcData -= srcStride * bytesPerPixel;
  203. }
  204. }
  205. commitBufferRW(drect);
  206. }
  207. void ModifiablePixelBuffer::fillRect(const PixelFormat& pf, const Rect &dest,
  208. const void* pix)
  209. {
  210. rdr::U8 buf[4];
  211. format.bufferFromBuffer(buf, pf, (const rdr::U8*)pix, 1);
  212. fillRect(dest, buf);
  213. }
  214. void ModifiablePixelBuffer::imageRect(const PixelFormat& pf, const Rect &dest,
  215. const void* pixels, int stride)
  216. {
  217. rdr::U8* dstBuffer;
  218. int dstStride;
  219. if (!dest.enclosed_by(getRect()))
  220. throw rfb::Exception("Destination rect %dx%d at %d,%d exceeds framebuffer %dx%d",
  221. dest.width(), dest.height(),
  222. dest.tl.x, dest.tl.y, width_, height_);
  223. if (stride == 0)
  224. stride = dest.width();
  225. dstBuffer = getBufferRW(dest, &dstStride);
  226. format.bufferFromBuffer(dstBuffer, pf, (const rdr::U8*)pixels,
  227. dest.width(), dest.height(),
  228. dstStride, stride);
  229. commitBufferRW(dest);
  230. }
  231. // -=- Simple pixel buffer with a continuous block of memory
  232. FullFramePixelBuffer::FullFramePixelBuffer(const PixelFormat& pf, int w, int h,
  233. rdr::U8* data_, int stride_)
  234. : ModifiablePixelBuffer(pf, w, h), data(data_), stride(stride_)
  235. {
  236. }
  237. FullFramePixelBuffer::FullFramePixelBuffer() : data(0) {}
  238. FullFramePixelBuffer::~FullFramePixelBuffer() {}
  239. rdr::U8* FullFramePixelBuffer::getBufferRW(const Rect& r, int* stride_)
  240. {
  241. if (!r.enclosed_by(getRect()))
  242. throw rfb::Exception("Pixel buffer request %dx%d at %d,%d exceeds framebuffer %dx%d",
  243. r.width(), r.height(),
  244. r.tl.x, r.tl.y, width_, height_);
  245. *stride_ = stride;
  246. return &data[(r.tl.x + (r.tl.y * stride)) * (format.bpp/8)];
  247. }
  248. void FullFramePixelBuffer::commitBufferRW(const Rect& r)
  249. {
  250. }
  251. const rdr::U8* FullFramePixelBuffer::getBuffer(const Rect& r, int* stride_) const
  252. {
  253. if (!r.enclosed_by(getRect()))
  254. throw rfb::Exception("Pixel buffer request %dx%d at %d,%d exceeds framebuffer %dx%d",
  255. r.width(), r.height(),
  256. r.tl.x, r.tl.y, width_, height_);
  257. *stride_ = stride;
  258. return &data[(r.tl.x + (r.tl.y * stride)) * (format.bpp/8)];
  259. }
  260. // -=- Managed pixel buffer class
  261. // Automatically allocates enough space for the specified format & area
  262. ManagedPixelBuffer::ManagedPixelBuffer()
  263. : datasize(0)
  264. {
  265. checkDataSize();
  266. };
  267. ManagedPixelBuffer::ManagedPixelBuffer(const PixelFormat& pf, int w, int h)
  268. : FullFramePixelBuffer(pf, w, h, NULL, w), datasize(0)
  269. {
  270. checkDataSize();
  271. };
  272. ManagedPixelBuffer::~ManagedPixelBuffer() {
  273. if (data) delete [] data;
  274. };
  275. void
  276. ManagedPixelBuffer::setPF(const PixelFormat &pf) {
  277. format = pf; checkDataSize();
  278. };
  279. void
  280. ManagedPixelBuffer::setSize(int w, int h) {
  281. width_ = w; height_ = h; stride = w; checkDataSize();
  282. };
  283. inline void
  284. ManagedPixelBuffer::checkDataSize() {
  285. unsigned long new_datasize = width_ * height_ * (format.bpp/8);
  286. if (datasize < new_datasize) {
  287. if (data) {
  288. delete [] data;
  289. datasize = 0; data = 0;
  290. }
  291. if (new_datasize) {
  292. data = new U8[new_datasize];
  293. if (!data)
  294. throw Exception("rfb::ManagedPixelBuffer unable to allocate buffer");
  295. datasize = new_datasize;
  296. }
  297. }
  298. };