blob: b50d9255bfd1df9b2c8c657c190b983fe24512b1 [file] [log] [blame]
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +00001/* 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
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 rdr::U8Array source(maskLen());
87 memset(source.buf, 0, maskLen());
88
89 int maskBytesPerRow = (width() + 7) / 8;
90 for (int y = 0; y < height(); y++) {
91 for (int x = 0; x < width(); x++) {
92 int byte = y * maskBytesPerRow + x / 8;
93 int bit = 7 - x % 8;
94 if (mask.buf[byte] & (1 << bit)) {
95 Pixel pix=0;
96 switch (getPF().bpp) {
97 case 8: pix = ((rdr::U8*) data)[y * width() + x]; break;
98 case 16: pix = ((rdr::U16*)data)[y * width() + x]; break;
99 case 32: pix = ((rdr::U32*)data)[y * width() + x]; break;
100 }
101 if (!gotPix0 || pix == *pix0) {
102 gotPix0 = true;
103 *pix0 = pix;
104 } else if (!gotPix1 || pix == *pix1) {
105 gotPix1 = true;
106 *pix1 = pix;
107 source.buf[byte] |= (1 << bit);
108 } else {
109 // not a bitmap
110 return 0;
111 }
112 }
113 }
114 }
115 return source.takeBuf();
116}
117
118// crop() determines the "busy" rectangle for the cursor - the minimum bounding
119// rectangle containing actual pixels. This isn't the most efficient algorithm
120// but it's short. For sanity, we make sure that the busy rectangle always
121// includes the hotspot (the hotspot is unsigned on the wire so otherwise it
122// would cause problems if it was above or left of the actual pixels)
123
124void Cursor::crop()
125{
126 Rect busy = getRect().intersect(Rect(hotspot.x, hotspot.y,
127 hotspot.x+1, hotspot.y+1));
128 int maskBytesPerRow = (width() + 7) / 8;
129 int x, y;
130 for (y = 0; y < height(); y++) {
131 for (x = 0; x < width(); x++) {
132 int byte = y * maskBytesPerRow + x / 8;
133 int bit = 7 - x % 8;
134 if (mask.buf[byte] & (1 << bit)) {
135 if (x < busy.tl.x) busy.tl.x = x;
136 if (x+1 > busy.br.x) busy.br.x = x+1;
137 if (y < busy.tl.y) busy.tl.y = y;
138 if (y+1 > busy.br.y) busy.br.y = y+1;
139 }
140 }
141 }
142
143 if (width() == busy.width() && height() == busy.height()) return;
144
145 vlog.debug("cropping %dx%d to %dx%d", width(), height(),
146 busy.width(), busy.height());
147
148 // Copy the pixel data
149 int newDataLen = busy.area() * (getPF().bpp/8);
150 rdr::U8* newData = new rdr::U8[newDataLen];
151 getImage(newData, busy);
152
153 // Copy the mask
154 int newMaskBytesPerRow = (busy.width()+7)/8;
155 int newMaskLen = newMaskBytesPerRow * busy.height();
156 rdr::U8* newMask = new rdr::U8[newMaskLen];
157 memset(newMask, 0, newMaskLen);
158 for (y = 0; y < busy.height(); y++) {
159 int newByte, newBit;
160 for (x = 0; x < busy.width(); x++) {
161 int oldByte = (y+busy.tl.y) * maskBytesPerRow + (x+busy.tl.x) / 8;
162 int oldBit = 7 - (x+busy.tl.x) % 8;
163 newByte = y * newMaskBytesPerRow + x / 8;
164 newBit = 7 - x % 8;
165 if (mask.buf[oldByte] & (1 << oldBit))
166 newMask[newByte] |= (1 << newBit);
167 }
168 }
169
170 // Set the size and data to the new, cropped cursor.
171 setSize(busy.width(), busy.height());
172 hotspot = hotspot.subtract(busy.tl);
173 delete [] data;
174 delete [] mask.buf;
175 datasize = newDataLen;
176 data = newData;
177 mask.buf = newMask;
178}