Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame^] | 1 | /* Copyright (C) 2002-2005 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 | |
| 22 | using namespace rfb; |
| 23 | |
| 24 | static LogWriter vlog("Cursor"); |
| 25 | |
| 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 | |
| 35 | void Cursor::drawOutline(const Pixel& c) |
| 36 | { |
| 37 | Cursor outlined; |
| 38 | |
| 39 | // Create a mirror of the existing cursor |
| 40 | outlined.setPF(getPF()); |
| 41 | outlined.setSize(width(), height()); |
| 42 | outlined.hotspot = hotspot; |
| 43 | |
| 44 | // Clear the mirror's background to the outline colour |
| 45 | outlined.fillRect(getRect(), c); |
| 46 | |
| 47 | // Blit the existing cursor, using its mask |
| 48 | outlined.maskRect(getRect(), data, mask.buf); |
| 49 | |
| 50 | // Now just adjust the mask to add the outline. The outline pixels |
| 51 | // will already be the right colour. :) |
| 52 | int maskBytesPerRow = (width() + 7) / 8; |
| 53 | for (int y = 0; y < height(); y++) { |
| 54 | for (int byte=0; byte<maskBytesPerRow; byte++) { |
| 55 | rdr::U8 m8 = mask.buf[y*maskBytesPerRow + byte]; |
| 56 | |
| 57 | // Handle above & below outline |
| 58 | if (y > 0) m8 |= mask.buf[(y-1)*maskBytesPerRow + byte]; |
| 59 | if (y < height()-1) m8 |= mask.buf[(y+1)*maskBytesPerRow + byte]; |
| 60 | |
| 61 | // Left outline |
| 62 | m8 |= mask.buf[y*maskBytesPerRow + byte] << 1; |
| 63 | if (byte < maskBytesPerRow-1) |
| 64 | m8 |= (mask.buf[y*maskBytesPerRow + byte + 1] >> 7) & 1; |
| 65 | |
| 66 | // Right outline |
| 67 | m8 |= mask.buf[y*maskBytesPerRow + byte] >> 1; |
| 68 | if (byte > 0) |
| 69 | m8 |= (mask.buf[y*maskBytesPerRow + byte - 1] << 7) & 128; |
| 70 | |
| 71 | outlined.mask.buf[y*maskBytesPerRow + byte] = m8; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // Replace the existing cursor & mask with the new one |
| 76 | delete [] data; |
| 77 | delete [] mask.buf; |
| 78 | data = outlined.data; outlined.data = 0; |
| 79 | mask.buf = outlined.mask.buf; outlined.mask.buf = 0; |
| 80 | } |
| 81 | |
| 82 | rdr::U8* Cursor::getBitmap(Pixel* pix0, Pixel* pix1) |
| 83 | { |
| 84 | bool gotPix0 = false; |
| 85 | bool gotPix1 = false; |
| 86 | *pix0 = *pix1 = 0; |
| 87 | rdr::U8Array source(maskLen()); |
| 88 | memset(source.buf, 0, maskLen()); |
| 89 | |
| 90 | int maskBytesPerRow = (width() + 7) / 8; |
| 91 | for (int y = 0; y < height(); y++) { |
| 92 | for (int x = 0; x < width(); x++) { |
| 93 | int byte = y * maskBytesPerRow + x / 8; |
| 94 | int bit = 7 - x % 8; |
| 95 | if (mask.buf[byte] & (1 << bit)) { |
| 96 | Pixel pix=0; |
| 97 | switch (getPF().bpp) { |
| 98 | case 8: pix = ((rdr::U8*) data)[y * width() + x]; break; |
| 99 | case 16: pix = ((rdr::U16*)data)[y * width() + x]; break; |
| 100 | case 32: pix = ((rdr::U32*)data)[y * width() + x]; break; |
| 101 | } |
| 102 | if (!gotPix0 || pix == *pix0) { |
| 103 | gotPix0 = true; |
| 104 | *pix0 = pix; |
| 105 | } else if (!gotPix1 || pix == *pix1) { |
| 106 | gotPix1 = true; |
| 107 | *pix1 = pix; |
| 108 | source.buf[byte] |= (1 << bit); |
| 109 | } else { |
| 110 | // not a bitmap |
| 111 | return 0; |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | return source.takeBuf(); |
| 117 | } |
| 118 | |
| 119 | // crop() determines the "busy" rectangle for the cursor - the minimum bounding |
| 120 | // rectangle containing actual pixels. This isn't the most efficient algorithm |
| 121 | // but it's short. For sanity, we make sure that the busy rectangle always |
| 122 | // includes the hotspot (the hotspot is unsigned on the wire so otherwise it |
| 123 | // would cause problems if it was above or left of the actual pixels) |
| 124 | |
| 125 | void Cursor::crop() |
| 126 | { |
| 127 | Rect busy = getRect().intersect(Rect(hotspot.x, hotspot.y, |
| 128 | hotspot.x+1, hotspot.y+1)); |
| 129 | int maskBytesPerRow = (width() + 7) / 8; |
| 130 | int x, y; |
| 131 | for (y = 0; y < height(); y++) { |
| 132 | for (x = 0; x < width(); x++) { |
| 133 | int byte = y * maskBytesPerRow + x / 8; |
| 134 | int bit = 7 - x % 8; |
| 135 | if (mask.buf[byte] & (1 << bit)) { |
| 136 | if (x < busy.tl.x) busy.tl.x = x; |
| 137 | if (x+1 > busy.br.x) busy.br.x = x+1; |
| 138 | if (y < busy.tl.y) busy.tl.y = y; |
| 139 | if (y+1 > busy.br.y) busy.br.y = y+1; |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | if (width() == busy.width() && height() == busy.height()) return; |
| 145 | |
| 146 | vlog.debug("cropping %dx%d to %dx%d", width(), height(), |
| 147 | busy.width(), busy.height()); |
| 148 | |
| 149 | // Copy the pixel data |
| 150 | int newDataLen = busy.area() * (getPF().bpp/8); |
| 151 | rdr::U8* newData = new rdr::U8[newDataLen]; |
| 152 | getImage(newData, busy); |
| 153 | |
| 154 | // Copy the mask |
| 155 | int newMaskBytesPerRow = (busy.width()+7)/8; |
| 156 | int newMaskLen = newMaskBytesPerRow * busy.height(); |
| 157 | rdr::U8* newMask = new rdr::U8[newMaskLen]; |
| 158 | memset(newMask, 0, newMaskLen); |
| 159 | for (y = 0; y < busy.height(); y++) { |
| 160 | int newByte, newBit; |
| 161 | for (x = 0; x < busy.width(); x++) { |
| 162 | int oldByte = (y+busy.tl.y) * maskBytesPerRow + (x+busy.tl.x) / 8; |
| 163 | int oldBit = 7 - (x+busy.tl.x) % 8; |
| 164 | newByte = y * newMaskBytesPerRow + x / 8; |
| 165 | newBit = 7 - x % 8; |
| 166 | if (mask.buf[oldByte] & (1 << oldBit)) |
| 167 | newMask[newByte] |= (1 << newBit); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | // Set the size and data to the new, cropped cursor. |
| 172 | setSize(busy.width(), busy.height()); |
| 173 | hotspot = hotspot.subtract(busy.tl); |
| 174 | delete [] data; |
| 175 | delete [] mask.buf; |
| 176 | datasize = newDataLen; |
| 177 | data = newData; |
| 178 | mask.buf = newMask; |
| 179 | } |