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 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. #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. void Cursor::setSize(int w, int h) {
  27. int oldMaskLen = maskLen();
  28. ManagedPixelBuffer::setSize(w, h);
  29. if (maskLen() > oldMaskLen) {
  30. delete [] mask.buf;
  31. mask.buf = new rdr::U8[maskLen()];
  32. }
  33. }
  34. void Cursor::drawOutline(const Pixel& c)
  35. {
  36. Cursor outlined;
  37. // Create a mirror of the existing cursor
  38. outlined.setPF(getPF());
  39. outlined.setSize(width(), height());
  40. outlined.hotspot = hotspot;
  41. // Clear the mirror's background to the outline colour
  42. outlined.fillRect(getRect(), c);
  43. // Blit the existing cursor, using its mask
  44. outlined.maskRect(getRect(), data, mask.buf);
  45. // Now just adjust the mask to add the outline. The outline pixels
  46. // will already be the right colour. :)
  47. int maskBytesPerRow = (width() + 7) / 8;
  48. for (int y = 0; y < height(); y++) {
  49. for (int byte=0; byte<maskBytesPerRow; byte++) {
  50. rdr::U8 m8 = mask.buf[y*maskBytesPerRow + byte];
  51. // Handle above & below outline
  52. if (y > 0) m8 |= mask.buf[(y-1)*maskBytesPerRow + byte];
  53. if (y < height()-1) m8 |= mask.buf[(y+1)*maskBytesPerRow + byte];
  54. // Left outline
  55. m8 |= mask.buf[y*maskBytesPerRow + byte] << 1;
  56. if (byte < maskBytesPerRow-1)
  57. m8 |= (mask.buf[y*maskBytesPerRow + byte + 1] >> 7) & 1;
  58. // Right outline
  59. m8 |= mask.buf[y*maskBytesPerRow + byte] >> 1;
  60. if (byte > 0)
  61. m8 |= (mask.buf[y*maskBytesPerRow + byte - 1] << 7) & 128;
  62. outlined.mask.buf[y*maskBytesPerRow + byte] = m8;
  63. }
  64. }
  65. // Replace the existing cursor & mask with the new one
  66. delete [] data;
  67. delete [] mask.buf;
  68. data = outlined.data; outlined.data = 0;
  69. mask.buf = outlined.mask.buf; outlined.mask.buf = 0;
  70. }
  71. rdr::U8* Cursor::getBitmap(Pixel* pix0, Pixel* pix1) const
  72. {
  73. bool gotPix0 = false;
  74. bool gotPix1 = false;
  75. *pix0 = *pix1 = 0;
  76. rdr::U8Array source(maskLen());
  77. memset(source.buf, 0, maskLen());
  78. int maskBytesPerRow = (width() + 7) / 8;
  79. const rdr::U8 *data_ptr = data;
  80. for (int y = 0; y < height(); y++) {
  81. for (int x = 0; x < width(); x++) {
  82. int byte = y * maskBytesPerRow + x / 8;
  83. int bit = 7 - x % 8;
  84. if (mask.buf[byte] & (1 << bit)) {
  85. Pixel pix = getPF().pixelFromBuffer(data_ptr);
  86. if (!gotPix0 || pix == *pix0) {
  87. gotPix0 = true;
  88. *pix0 = pix;
  89. } else if (!gotPix1 || pix == *pix1) {
  90. gotPix1 = true;
  91. *pix1 = pix;
  92. source.buf[byte] |= (1 << bit);
  93. } else {
  94. // not a bitmap
  95. return 0;
  96. }
  97. }
  98. data_ptr += getPF().bpp/8;
  99. }
  100. }
  101. return source.takeBuf();
  102. }
  103. // crop() determines the "busy" rectangle for the cursor - the minimum bounding
  104. // rectangle containing actual pixels. This isn't the most efficient algorithm
  105. // but it's short. For sanity, we make sure that the busy rectangle always
  106. // includes the hotspot (the hotspot is unsigned on the wire so otherwise it
  107. // would cause problems if it was above or left of the actual pixels)
  108. void Cursor::crop()
  109. {
  110. Rect busy = getRect().intersect(Rect(hotspot.x, hotspot.y,
  111. hotspot.x+1, hotspot.y+1));
  112. int maskBytesPerRow = (width() + 7) / 8;
  113. int x, y;
  114. for (y = 0; y < height(); y++) {
  115. for (x = 0; x < width(); x++) {
  116. int byte = y * maskBytesPerRow + x / 8;
  117. int bit = 7 - x % 8;
  118. if (mask.buf[byte] & (1 << bit)) {
  119. if (x < busy.tl.x) busy.tl.x = x;
  120. if (x+1 > busy.br.x) busy.br.x = x+1;
  121. if (y < busy.tl.y) busy.tl.y = y;
  122. if (y+1 > busy.br.y) busy.br.y = y+1;
  123. }
  124. }
  125. }
  126. if (width() == busy.width() && height() == busy.height()) return;
  127. vlog.debug("cropping %dx%d to %dx%d", width(), height(),
  128. busy.width(), busy.height());
  129. // Copy the pixel data
  130. int newDataLen = busy.area() * (getPF().bpp/8);
  131. rdr::U8* newData = new rdr::U8[newDataLen];
  132. getImage(newData, busy);
  133. // Copy the mask
  134. int newMaskBytesPerRow = (busy.width()+7)/8;
  135. int newMaskLen = newMaskBytesPerRow * busy.height();
  136. rdr::U8* newMask = new rdr::U8[newMaskLen];
  137. memset(newMask, 0, newMaskLen);
  138. for (y = 0; y < busy.height(); y++) {
  139. int newByte, newBit;
  140. for (x = 0; x < busy.width(); x++) {
  141. int oldByte = (y+busy.tl.y) * maskBytesPerRow + (x+busy.tl.x) / 8;
  142. int oldBit = 7 - (x+busy.tl.x) % 8;
  143. newByte = y * newMaskBytesPerRow + x / 8;
  144. newBit = 7 - x % 8;
  145. if (mask.buf[oldByte] & (1 << oldBit))
  146. newMask[newByte] |= (1 << newBit);
  147. }
  148. }
  149. // Set the size and data to the new, cropped cursor.
  150. setSize(busy.width(), busy.height());
  151. hotspot = hotspot.subtract(busy.tl);
  152. delete [] data;
  153. delete [] mask.buf;
  154. datasize = newDataLen;
  155. data = newData;
  156. mask.buf = newMask;
  157. }
  158. RenderedCursor::RenderedCursor()
  159. {
  160. }
  161. const rdr::U8* RenderedCursor::getBuffer(const Rect& _r, int* stride) const
  162. {
  163. Rect r;
  164. r = _r.translate(offset.negate());
  165. if (!r.enclosed_by(buffer.getRect()))
  166. throw Exception("RenderedCursor: Invalid area requested");
  167. return buffer.getBuffer(r, stride);
  168. }
  169. void RenderedCursor::update(PixelBuffer* framebuffer,
  170. Cursor* cursor, const Point& pos)
  171. {
  172. Point rawOffset;
  173. Rect clippedRect;
  174. const rdr::U8* data;
  175. int stride;
  176. assert(framebuffer);
  177. assert(cursor);
  178. if (!framebuffer->getPF().equal(cursor->getPF()))
  179. throw Exception("RenderedCursor: Trying to render cursor on incompatible frame buffer");
  180. format = framebuffer->getPF();
  181. width_ = framebuffer->width();
  182. height_ = framebuffer->height();
  183. rawOffset = pos.subtract(cursor->hotspot);
  184. clippedRect = cursor->getRect(rawOffset).intersect(framebuffer->getRect());
  185. offset = clippedRect.tl;
  186. buffer.setPF(cursor->getPF());
  187. buffer.setSize(clippedRect.width(), clippedRect.height());
  188. data = framebuffer->getBuffer(buffer.getRect(offset), &stride);
  189. buffer.imageRect(buffer.getRect(), data, stride);
  190. data = cursor->getBuffer(cursor->getRect(), &stride);
  191. buffer.maskRect(cursor->getRect(rawOffset.subtract(offset)),
  192. data, cursor->mask.buf);
  193. }