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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* Copyright (C) 2002-2004 RealVNC Ltd. All Rights Reserved.
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. #include <string.h>
  19. #include <rfb/Cursor.h>
  20. #include <rfb/LogWriter.h>
  21. using namespace rfb;
  22. static LogWriter vlog("Cursor");
  23. void Cursor::setSize(int w, int h) {
  24. int oldMaskLen = maskLen();
  25. ManagedPixelBuffer::setSize(w, h);
  26. if (maskLen() > oldMaskLen) {
  27. delete [] mask.buf;
  28. mask.buf = new rdr::U8[maskLen()];
  29. }
  30. }
  31. void Cursor::drawOutline(const Pixel& c)
  32. {
  33. Cursor outlined;
  34. // Create a mirror of the existing cursor
  35. outlined.setPF(getPF());
  36. outlined.setSize(width(), height());
  37. outlined.hotspot = hotspot;
  38. // Clear the mirror's background to the outline colour
  39. outlined.fillRect(getRect(), c);
  40. // Blit the existing cursor, using its mask
  41. outlined.maskRect(getRect(), data, mask.buf);
  42. // Now just adjust the mask to add the outline. The outline pixels
  43. // will already be the right colour. :)
  44. int maskBytesPerRow = (width() + 7) / 8;
  45. for (int y = 0; y < height(); y++) {
  46. for (int byte=0; byte<maskBytesPerRow; byte++) {
  47. rdr::U8 m8 = mask.buf[y*maskBytesPerRow + byte];
  48. // Handle above & below outline
  49. if (y > 0) m8 |= mask.buf[(y-1)*maskBytesPerRow + byte];
  50. if (y < height()-1) m8 |= mask.buf[(y+1)*maskBytesPerRow + byte];
  51. // Left outline
  52. m8 |= mask.buf[y*maskBytesPerRow + byte] << 1;
  53. if (byte < maskBytesPerRow-1)
  54. m8 |= (mask.buf[y*maskBytesPerRow + byte + 1] >> 7) & 1;
  55. // Right outline
  56. m8 |= mask.buf[y*maskBytesPerRow + byte] >> 1;
  57. if (byte > 0)
  58. m8 |= (mask.buf[y*maskBytesPerRow + byte - 1] << 7) & 128;
  59. outlined.mask.buf[y*maskBytesPerRow + byte] = m8;
  60. }
  61. }
  62. // Replace the existing cursor & mask with the new one
  63. delete [] data;
  64. delete [] mask.buf;
  65. data = outlined.data; outlined.data = 0;
  66. mask.buf = outlined.mask.buf; outlined.mask.buf = 0;
  67. }
  68. rdr::U8* Cursor::getBitmap(Pixel* pix0, Pixel* pix1)
  69. {
  70. bool gotPix0 = false;
  71. bool gotPix1 = false;
  72. rdr::U8Array source(maskLen());
  73. memset(source.buf, 0, maskLen());
  74. int maskBytesPerRow = (width() + 7) / 8;
  75. for (int y = 0; y < height(); y++) {
  76. for (int x = 0; x < width(); x++) {
  77. int byte = y * maskBytesPerRow + x / 8;
  78. int bit = 7 - x % 8;
  79. if (mask.buf[byte] & (1 << bit)) {
  80. Pixel pix=0;
  81. switch (getPF().bpp) {
  82. case 8: pix = ((rdr::U8*) data)[y * width() + x]; break;
  83. case 16: pix = ((rdr::U16*)data)[y * width() + x]; break;
  84. case 32: pix = ((rdr::U32*)data)[y * width() + x]; break;
  85. }
  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. }
  99. }
  100. return source.takeBuf();
  101. }
  102. // crop() determines the "busy" rectangle for the cursor - the minimum bounding
  103. // rectangle containing actual pixels. This isn't the most efficient algorithm
  104. // but it's short. For sanity, we make sure that the busy rectangle always
  105. // includes the hotspot (the hotspot is unsigned on the wire so otherwise it
  106. // would cause problems if it was above or left of the actual pixels)
  107. void Cursor::crop()
  108. {
  109. Rect busy = getRect().intersect(Rect(hotspot.x, hotspot.y,
  110. hotspot.x+1, hotspot.y+1));
  111. int maskBytesPerRow = (width() + 7) / 8;
  112. int x, y;
  113. for (y = 0; y < height(); y++) {
  114. for (x = 0; x < width(); x++) {
  115. int byte = y * maskBytesPerRow + x / 8;
  116. int bit = 7 - x % 8;
  117. if (mask.buf[byte] & (1 << bit)) {
  118. if (x < busy.tl.x) busy.tl.x = x;
  119. if (x+1 > busy.br.x) busy.br.x = x+1;
  120. if (y < busy.tl.y) busy.tl.y = y;
  121. if (y+1 > busy.br.y) busy.br.y = y+1;
  122. }
  123. }
  124. }
  125. if (width() == busy.width() && height() == busy.height()) return;
  126. vlog.debug("cropping %dx%d to %dx%d", width(), height(),
  127. busy.width(), busy.height());
  128. // Copy the pixel data
  129. int newDataLen = busy.area() * (getPF().bpp/8);
  130. rdr::U8* newData = new rdr::U8[newDataLen];
  131. getImage(newData, busy);
  132. // Copy the mask
  133. int newMaskBytesPerRow = (busy.width()+7)/8;
  134. int newMaskLen = newMaskBytesPerRow * busy.height();
  135. rdr::U8* newMask = new rdr::U8[newMaskLen];
  136. memset(newMask, 0, newMaskLen);
  137. for (y = 0; y < busy.height(); y++) {
  138. int newByte, newBit;
  139. for (x = 0; x < busy.width(); x++) {
  140. int oldByte = (y+busy.tl.y) * maskBytesPerRow + (x+busy.tl.x) / 8;
  141. int oldBit = 7 - (x+busy.tl.x) % 8;
  142. newByte = y * newMaskBytesPerRow + x / 8;
  143. newBit = 7 - x % 8;
  144. if (mask.buf[oldByte] & (1 << oldBit))
  145. newMask[newByte] |= (1 << newBit);
  146. }
  147. }
  148. // Set the size and data to the new, cropped cursor.
  149. setSize(busy.width(), busy.height());
  150. hotspot = hotspot.subtract(busy.tl);
  151. delete [] data;
  152. delete [] mask.buf;
  153. datasize = newDataLen;
  154. data = newData;
  155. mask.buf = newMask;
  156. }