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.

Cursor.cxx 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2014-2017 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. #include <assert.h>
  20. #include <string.h>
  21. #include <rfb/Cursor.h>
  22. #include <rfb/LogWriter.h>
  23. #include <rfb/Exception.h>
  24. using namespace rfb;
  25. static LogWriter vlog("Cursor");
  26. Cursor::Cursor(int width, int height, const Point& hotspot,
  27. const rdr::U8* data) :
  28. width_(width), height_(height), hotspot_(hotspot)
  29. {
  30. this->data = new rdr::U8[width_*height_*4];
  31. memcpy(this->data, data, width_*height_*4);
  32. }
  33. Cursor::Cursor(const Cursor& other) :
  34. width_(other.width_), height_(other.height_),
  35. hotspot_(other.hotspot_)
  36. {
  37. data = new rdr::U8[width_*height_*4];
  38. memcpy(data, other.data, width_*height_*4);
  39. }
  40. Cursor::~Cursor()
  41. {
  42. delete [] data;
  43. }
  44. static unsigned short pow223[] = { 0, 30, 143, 355, 676, 1113, 1673,
  45. 2361, 3181, 4139, 5237, 6479, 7869,
  46. 9409, 11103, 12952, 14961, 17130,
  47. 19462, 21960, 24626, 27461, 30467,
  48. 33647, 37003, 40535, 44245, 48136,
  49. 52209, 56466, 60907, 65535 };
  50. static unsigned short ipow(unsigned short val, unsigned short lut[])
  51. {
  52. int idx = val >> (16-5);
  53. int a, b;
  54. if (val < 0x8000) {
  55. a = lut[idx];
  56. b = lut[idx+1];
  57. } else {
  58. a = lut[idx-1];
  59. b = lut[idx];
  60. }
  61. return (val & 0x7ff) * (b-a) / 0x7ff + a;
  62. }
  63. static unsigned short srgb_to_lin(unsigned char srgb)
  64. {
  65. return ipow((unsigned)srgb * 65535 / 255, pow223);
  66. }
  67. // Floyd-Steinberg dithering
  68. static void dither(int width, int height, rdr::U16* data)
  69. {
  70. for (int y = 0; y < height; y++) {
  71. for (int x_ = 0; x_ < width; x_++) {
  72. int x = (y & 1) ? (width - x_ - 1) : x_;
  73. int error;
  74. if (data[x] > 32767) {
  75. error = data[x] - 65535;
  76. data[x] = 65535;
  77. } else {
  78. error = data[x] - 0;
  79. data[x] = 0;
  80. }
  81. if (y & 1) {
  82. if (x > 0) {
  83. data[x - 1] += error * 7 / 16;
  84. }
  85. if ((y + 1) < height) {
  86. if (x > 0)
  87. data[x - 1 + width] += error * 3 / 16;
  88. data[x + width] += error * 5 / 16;
  89. if ((x + 1) < width)
  90. data[x + 1] += error * 1 / 16;
  91. }
  92. } else {
  93. if ((x + 1) < width) {
  94. data[x + 1] += error * 7 / 16;
  95. }
  96. if ((y + 1) < height) {
  97. if ((x + 1) < width)
  98. data[x + 1 + width] += error * 3 / 16;
  99. data[x + width] += error * 5 / 16;
  100. if (x > 0)
  101. data[x - 1] += error * 1 / 16;
  102. }
  103. }
  104. }
  105. data += width;
  106. }
  107. }
  108. rdr::U8* Cursor::getBitmap() const
  109. {
  110. // First step is converting to luminance
  111. rdr::U16Array luminance(width()*height());
  112. rdr::U16 *lum_ptr = luminance.buf;
  113. const rdr::U8 *data_ptr = data;
  114. for (int y = 0; y < height(); y++) {
  115. for (int x = 0; x < width(); x++) {
  116. rdr::U32 lum;
  117. // Use BT.709 coefficients for grayscale
  118. lum = 0;
  119. lum += (rdr::U32)srgb_to_lin(data_ptr[0]) * 6947; // 0.2126
  120. lum += (rdr::U32)srgb_to_lin(data_ptr[1]) * 23436; // 0.7152
  121. lum += (rdr::U32)srgb_to_lin(data_ptr[2]) * 2366; // 0.0722
  122. lum /= 32768;
  123. *lum_ptr++ = lum;
  124. data_ptr += 4;
  125. }
  126. }
  127. // Then diterhing
  128. dither(width(), height(), luminance.buf);
  129. // Then conversion to a bit mask
  130. rdr::U8Array source((width()+7)/8*height());
  131. memset(source.buf, 0, (width()+7)/8*height());
  132. int maskBytesPerRow = (width() + 7) / 8;
  133. lum_ptr = luminance.buf;
  134. data_ptr = data;
  135. for (int y = 0; y < height(); y++) {
  136. for (int x = 0; x < width(); x++) {
  137. int byte = y * maskBytesPerRow + x / 8;
  138. int bit = 7 - x % 8;
  139. if (*lum_ptr > 32767)
  140. source.buf[byte] |= (1 << bit);
  141. lum_ptr++;
  142. data_ptr += 4;
  143. }
  144. }
  145. return source.takeBuf();
  146. }
  147. rdr::U8* Cursor::getMask() const
  148. {
  149. // First step is converting to integer array
  150. rdr::U16Array alpha(width()*height());
  151. rdr::U16 *alpha_ptr = alpha.buf;
  152. const rdr::U8 *data_ptr = data;
  153. for (int y = 0; y < height(); y++) {
  154. for (int x = 0; x < width(); x++) {
  155. *alpha_ptr++ = (rdr::U32)data_ptr[3] * 65535 / 255;
  156. data_ptr += 4;
  157. }
  158. }
  159. // Then diterhing
  160. dither(width(), height(), alpha.buf);
  161. // Then conversion to a bit mask
  162. rdr::U8Array mask((width()+7)/8*height());
  163. memset(mask.buf, 0, (width()+7)/8*height());
  164. int maskBytesPerRow = (width() + 7) / 8;
  165. alpha_ptr = alpha.buf;
  166. data_ptr = data;
  167. for (int y = 0; y < height(); y++) {
  168. for (int x = 0; x < width(); x++) {
  169. int byte = y * maskBytesPerRow + x / 8;
  170. int bit = 7 - x % 8;
  171. if (*alpha_ptr > 32767)
  172. mask.buf[byte] |= (1 << bit);
  173. alpha_ptr++;
  174. data_ptr += 4;
  175. }
  176. }
  177. return mask.takeBuf();
  178. }
  179. // crop() determines the "busy" rectangle for the cursor - the minimum bounding
  180. // rectangle containing actual pixels. This isn't the most efficient algorithm
  181. // but it's short. For sanity, we make sure that the busy rectangle always
  182. // includes the hotspot (the hotspot is unsigned on the wire so otherwise it
  183. // would cause problems if it was above or left of the actual pixels)
  184. void Cursor::crop()
  185. {
  186. Rect busy = Rect(0, 0, width_, height_);
  187. busy = busy.intersect(Rect(hotspot_.x, hotspot_.y,
  188. hotspot_.x+1, hotspot_.y+1));
  189. int x, y;
  190. rdr::U8 *data_ptr = data;
  191. for (y = 0; y < height(); y++) {
  192. for (x = 0; x < width(); x++) {
  193. if (data_ptr[3] > 0) {
  194. if (x < busy.tl.x) busy.tl.x = x;
  195. if (x+1 > busy.br.x) busy.br.x = x+1;
  196. if (y < busy.tl.y) busy.tl.y = y;
  197. if (y+1 > busy.br.y) busy.br.y = y+1;
  198. }
  199. data_ptr += 4;
  200. }
  201. }
  202. if (width() == busy.width() && height() == busy.height()) return;
  203. // Copy the pixel data
  204. int newDataLen = busy.area() * 4;
  205. rdr::U8* newData = new rdr::U8[newDataLen];
  206. data_ptr = newData;
  207. for (y = busy.tl.y; y < busy.br.y; y++) {
  208. memcpy(data_ptr, data + y*width()*4 + busy.tl.x*4, busy.width()*4);
  209. data_ptr += busy.width()*4;
  210. }
  211. // Set the size and data to the new, cropped cursor.
  212. width_ = busy.width();
  213. height_ = busy.height();
  214. hotspot_ = hotspot_.subtract(busy.tl);
  215. delete [] data;
  216. data = newData;
  217. }
  218. RenderedCursor::RenderedCursor()
  219. {
  220. }
  221. const rdr::U8* RenderedCursor::getBuffer(const Rect& _r, int* stride) const
  222. {
  223. Rect r;
  224. r = _r.translate(offset.negate());
  225. if (!r.enclosed_by(buffer.getRect()))
  226. throw Exception("RenderedCursor: Invalid area requested");
  227. return buffer.getBuffer(r, stride);
  228. }
  229. void RenderedCursor::update(PixelBuffer* framebuffer,
  230. Cursor* cursor, const Point& pos)
  231. {
  232. Point rawOffset, diff;
  233. Rect clippedRect;
  234. const rdr::U8* data;
  235. int stride;
  236. assert(framebuffer);
  237. assert(cursor);
  238. format = framebuffer->getPF();
  239. width_ = framebuffer->width();
  240. height_ = framebuffer->height();
  241. rawOffset = pos.subtract(cursor->hotspot());
  242. clippedRect = Rect(0, 0, cursor->width(), cursor->height())
  243. .translate(rawOffset)
  244. .intersect(framebuffer->getRect());
  245. offset = clippedRect.tl;
  246. buffer.setPF(format);
  247. buffer.setSize(clippedRect.width(), clippedRect.height());
  248. // Bail out early to avoid pestering the framebuffer with
  249. // bogus coordinates
  250. if (clippedRect.area() == 0)
  251. return;
  252. data = framebuffer->getBuffer(buffer.getRect(offset), &stride);
  253. buffer.imageRect(buffer.getRect(), data, stride);
  254. diff = offset.subtract(rawOffset);
  255. for (int y = 0;y < buffer.height();y++) {
  256. for (int x = 0;x < buffer.width();x++) {
  257. size_t idx;
  258. rdr::U8 bg[4], fg[4];
  259. rdr::U8 rgb[3];
  260. idx = (y+diff.y)*cursor->width() + (x+diff.x);
  261. memcpy(fg, cursor->getBuffer() + idx*4, 4);
  262. if (fg[3] == 0x00)
  263. continue;
  264. else if (fg[3] == 0xff) {
  265. memcpy(rgb, fg, 3);
  266. } else {
  267. buffer.getImage(bg, Rect(x, y, x+1, y+1));
  268. format.rgbFromBuffer(rgb, bg, 1);
  269. // FIXME: Gamma aware blending
  270. for (int i = 0;i < 3;i++) {
  271. rgb[i] = (unsigned)rgb[i]*(255-fg[3])/255 +
  272. (unsigned)fg[i]*fg[3]/255;
  273. }
  274. }
  275. format.bufferFromRGB(bg, rgb, 1);
  276. buffer.imageRect(Rect(x, y, x+1, y+1), bg);
  277. }
  278. }
  279. }