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

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