blob: 2d077c98d058e9f9b0b1925eaaaded306ddc38a3 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* 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
22using namespace rfb;
23
24static LogWriter vlog("Cursor");
25
26void 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
35void 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
82rdr::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;
Pierre Ossman01cf5732010-09-30 11:53:15 +000091 const rdr::U8 *data_ptr = data;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000092 for (int y = 0; y < height(); y++) {
93 for (int x = 0; x < width(); x++) {
94 int byte = y * maskBytesPerRow + x / 8;
95 int bit = 7 - x % 8;
96 if (mask.buf[byte] & (1 << bit)) {
Pierre Ossman01cf5732010-09-30 11:53:15 +000097 Pixel pix = getPF().pixelFromBuffer(data_ptr);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000098 if (!gotPix0 || pix == *pix0) {
99 gotPix0 = true;
100 *pix0 = pix;
101 } else if (!gotPix1 || pix == *pix1) {
102 gotPix1 = true;
103 *pix1 = pix;
104 source.buf[byte] |= (1 << bit);
105 } else {
106 // not a bitmap
107 return 0;
108 }
109 }
Pierre Ossman01cf5732010-09-30 11:53:15 +0000110 data_ptr += getPF().bpp/8;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000111 }
112 }
113 return source.takeBuf();
114}
115
116// crop() determines the "busy" rectangle for the cursor - the minimum bounding
117// rectangle containing actual pixels. This isn't the most efficient algorithm
118// but it's short. For sanity, we make sure that the busy rectangle always
119// includes the hotspot (the hotspot is unsigned on the wire so otherwise it
120// would cause problems if it was above or left of the actual pixels)
121
122void Cursor::crop()
123{
124 Rect busy = getRect().intersect(Rect(hotspot.x, hotspot.y,
125 hotspot.x+1, hotspot.y+1));
126 int maskBytesPerRow = (width() + 7) / 8;
127 int x, y;
128 for (y = 0; y < height(); y++) {
129 for (x = 0; x < width(); x++) {
130 int byte = y * maskBytesPerRow + x / 8;
131 int bit = 7 - x % 8;
132 if (mask.buf[byte] & (1 << bit)) {
133 if (x < busy.tl.x) busy.tl.x = x;
134 if (x+1 > busy.br.x) busy.br.x = x+1;
135 if (y < busy.tl.y) busy.tl.y = y;
136 if (y+1 > busy.br.y) busy.br.y = y+1;
137 }
138 }
139 }
140
141 if (width() == busy.width() && height() == busy.height()) return;
142
143 vlog.debug("cropping %dx%d to %dx%d", width(), height(),
144 busy.width(), busy.height());
145
146 // Copy the pixel data
147 int newDataLen = busy.area() * (getPF().bpp/8);
148 rdr::U8* newData = new rdr::U8[newDataLen];
149 getImage(newData, busy);
150
151 // Copy the mask
152 int newMaskBytesPerRow = (busy.width()+7)/8;
153 int newMaskLen = newMaskBytesPerRow * busy.height();
154 rdr::U8* newMask = new rdr::U8[newMaskLen];
155 memset(newMask, 0, newMaskLen);
156 for (y = 0; y < busy.height(); y++) {
157 int newByte, newBit;
158 for (x = 0; x < busy.width(); x++) {
159 int oldByte = (y+busy.tl.y) * maskBytesPerRow + (x+busy.tl.x) / 8;
160 int oldBit = 7 - (x+busy.tl.x) % 8;
161 newByte = y * newMaskBytesPerRow + x / 8;
162 newBit = 7 - x % 8;
163 if (mask.buf[oldByte] & (1 << oldBit))
164 newMask[newByte] |= (1 << newBit);
165 }
166 }
167
168 // Set the size and data to the new, cropped cursor.
169 setSize(busy.width(), busy.height());
170 hotspot = hotspot.subtract(busy.tl);
171 delete [] data;
172 delete [] mask.buf;
173 datasize = newDataLen;
174 data = newData;
175 mask.buf = newMask;
176}