1 /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 * Copyright 2014-2017 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
25 using namespace rfb;
26
27 static LogWriter vlog("Cursor");
28
Cursor(int width,int height,const Point & hotspot,const rdr::U8 * data)29 Cursor::Cursor(int width, int height, const Point& hotspot,
30 const rdr::U8* data) :
31 width_(width), height_(height), hotspot_(hotspot)
32 {
33 this->data = new rdr::U8[width_*height_*4];
34 memcpy(this->data, data, width_*height_*4);
35 }
36
Cursor(const Cursor & other)37 Cursor::Cursor(const Cursor& other) :
38 width_(other.width_), height_(other.height_),
39 hotspot_(other.hotspot_)
40 {
41 data = new rdr::U8[width_*height_*4];
42 memcpy(data, other.data, width_*height_*4);
43 }
44
~Cursor()45 Cursor::~Cursor()
46 {
47 delete [] data;
48 }
49
50 static unsigned short pow223[] = { 0, 30, 143, 355, 676, 1113, 1673,
51 2361, 3181, 4139, 5237, 6479, 7869,
52 9409, 11103, 12952, 14961, 17130,
53 19462, 21960, 24626, 27461, 30467,
54 33647, 37003, 40535, 44245, 48136,
55 52209, 56466, 60907, 65535 };
56
ipow(unsigned short val,unsigned short lut[])57 static unsigned short ipow(unsigned short val, unsigned short lut[])
58 {
59 int idx = val >> (16-5);
60 int a, b;
61
62 if (val < 0x8000) {
63 a = lut[idx];
64 b = lut[idx+1];
65 } else {
66 a = lut[idx-1];
67 b = lut[idx];
68 }
69
70 return (val & 0x7ff) * (b-a) / 0x7ff + a;
71 }
72
srgb_to_lin(unsigned char srgb)73 static unsigned short srgb_to_lin(unsigned char srgb)
74 {
75 return ipow((unsigned)srgb * 65535 / 255, pow223);
76 }
77
78 // Floyd-Steinberg dithering
dither(int width,int height,rdr::S32 * data)79 static void dither(int width, int height, rdr::S32* data)
80 {
81 for (int y = 0; y < height; y++) {
82 for (int x_ = 0; x_ < width; x_++) {
83 int x = (y & 1) ? (width - x_ - 1) : x_;
84 int error;
85
86 if (data[x] > 32767) {
87 error = data[x] - 65535;
88 data[x] = 65535;
89 } else {
90 error = data[x] - 0;
91 data[x] = 0;
92 }
93
94 if (y & 1) {
95 if (x > 0) {
96 data[x - 1] += error * 7 / 16;
97 }
98 if ((y + 1) < height) {
99 if (x > 0)
100 data[x - 1 + width] += error * 3 / 16;
101 data[x + width] += error * 5 / 16;
102 if ((x + 1) < width)
103 data[x + 1] += error * 1 / 16;
104 }
105 } else {
106 if ((x + 1) < width) {
107 data[x + 1] += error * 7 / 16;
108 }
109 if ((y + 1) < height) {
110 if ((x + 1) < width)
111 data[x + 1 + width] += error * 3 / 16;
112 data[x + width] += error * 5 / 16;
113 if (x > 0)
114 data[x - 1] += error * 1 / 16;
115 }
116 }
117 }
118 data += width;
119 }
120 }
121
getBitmap() const122 rdr::U8* Cursor::getBitmap() const
123 {
124 // First step is converting to luminance
125 rdr::S32Array luminance(width()*height());
126 rdr::S32 *lum_ptr = luminance.buf;
127 const rdr::U8 *data_ptr = data;
128 for (int y = 0; y < height(); y++) {
129 for (int x = 0; x < width(); x++) {
130 rdr::S32 lum;
131
132 // Use BT.709 coefficients for grayscale
133 lum = 0;
134 lum += (rdr::U32)srgb_to_lin(data_ptr[0]) * 6947; // 0.2126
135 lum += (rdr::U32)srgb_to_lin(data_ptr[1]) * 23436; // 0.7152
136 lum += (rdr::U32)srgb_to_lin(data_ptr[2]) * 2366; // 0.0722
137 lum /= 32768;
138
139 *lum_ptr++ = lum;
140 data_ptr += 4;
141 }
142 }
143
144 // Then diterhing
145 dither(width(), height(), luminance.buf);
146
147 // Then conversion to a bit mask
148 rdr::U8Array source((width()+7)/8*height());
149 memset(source.buf, 0, (width()+7)/8*height());
150 int maskBytesPerRow = (width() + 7) / 8;
151 lum_ptr = luminance.buf;
152 data_ptr = data;
153 for (int y = 0; y < height(); y++) {
154 for (int x = 0; x < width(); x++) {
155 int byte = y * maskBytesPerRow + x / 8;
156 int bit = 7 - x % 8;
157 if (*lum_ptr > 32767)
158 source.buf[byte] |= (1 << bit);
159 lum_ptr++;
160 data_ptr += 4;
161 }
162 }
163
164 return source.takeBuf();
165 }
166
getMask() const167 rdr::U8* Cursor::getMask() const
168 {
169 // First step is converting to integer array
170 rdr::S32Array alpha(width()*height());
171 rdr::S32 *alpha_ptr = alpha.buf;
172 const rdr::U8 *data_ptr = data;
173 for (int y = 0; y < height(); y++) {
174 for (int x = 0; x < width(); x++) {
175 *alpha_ptr++ = (rdr::U32)data_ptr[3] * 65535 / 255;
176 data_ptr += 4;
177 }
178 }
179
180 // Then diterhing
181 dither(width(), height(), alpha.buf);
182
183 // Then conversion to a bit mask
184 rdr::U8Array mask((width()+7)/8*height());
185 memset(mask.buf, 0, (width()+7)/8*height());
186 int maskBytesPerRow = (width() + 7) / 8;
187 alpha_ptr = alpha.buf;
188 data_ptr = data;
189 for (int y = 0; y < height(); y++) {
190 for (int x = 0; x < width(); x++) {
191 int byte = y * maskBytesPerRow + x / 8;
192 int bit = 7 - x % 8;
193 if (*alpha_ptr > 32767)
194 mask.buf[byte] |= (1 << bit);
195 alpha_ptr++;
196 data_ptr += 4;
197 }
198 }
199
200 return mask.takeBuf();
201 }
202
203 // crop() determines the "busy" rectangle for the cursor - the minimum bounding
204 // rectangle containing actual pixels. This isn't the most efficient algorithm
205 // but it's short. For sanity, we make sure that the busy rectangle always
206 // includes the hotspot (the hotspot is unsigned on the wire so otherwise it
207 // would cause problems if it was above or left of the actual pixels)
208
crop()209 void Cursor::crop()
210 {
211 Rect busy = Rect(0, 0, width_, height_);
212 busy = busy.intersect(Rect(hotspot_.x, hotspot_.y,
213 hotspot_.x+1, hotspot_.y+1));
214 int x, y;
215 rdr::U8 *data_ptr = data;
216 for (y = 0; y < height(); y++) {
217 for (x = 0; x < width(); x++) {
218 if (data_ptr[3] > 0) {
219 if (x < busy.tl.x) busy.tl.x = x;
220 if (x+1 > busy.br.x) busy.br.x = x+1;
221 if (y < busy.tl.y) busy.tl.y = y;
222 if (y+1 > busy.br.y) busy.br.y = y+1;
223 }
224 data_ptr += 4;
225 }
226 }
227
228 if (width() == busy.width() && height() == busy.height()) return;
229
230 // Copy the pixel data
231 int newDataLen = busy.area() * 4;
232 rdr::U8* newData = new rdr::U8[newDataLen];
233 data_ptr = newData;
234 for (y = busy.tl.y; y < busy.br.y; y++) {
235 memcpy(data_ptr, data + y*width()*4 + busy.tl.x*4, busy.width()*4);
236 data_ptr += busy.width()*4;
237 }
238
239 // Set the size and data to the new, cropped cursor.
240 width_ = busy.width();
241 height_ = busy.height();
242 hotspot_ = hotspot_.subtract(busy.tl);
243 delete [] data;
244 data = newData;
245 }
246
RenderedCursor()247 RenderedCursor::RenderedCursor()
248 {
249 }
250
getBuffer(const Rect & _r,int * stride) const251 const rdr::U8* RenderedCursor::getBuffer(const Rect& _r, int* stride) const
252 {
253 Rect r;
254
255 r = _r.translate(offset.negate());
256 if (!r.enclosed_by(buffer.getRect()))
257 throw Exception("RenderedCursor: Invalid area requested");
258
259 return buffer.getBuffer(r, stride);
260 }
261
update(PixelBuffer * framebuffer,Cursor * cursor,const Point & pos)262 void RenderedCursor::update(PixelBuffer* framebuffer,
263 Cursor* cursor, const Point& pos)
264 {
265 Point rawOffset, diff;
266 Rect clippedRect;
267
268 const rdr::U8* data;
269 int stride;
270
271 assert(framebuffer);
272 assert(cursor);
273
274 format = framebuffer->getPF();
275 setSize(framebuffer->width(), framebuffer->height());
276
277 rawOffset = pos.subtract(cursor->hotspot());
278 clippedRect = Rect(0, 0, cursor->width(), cursor->height())
279 .translate(rawOffset)
280 .intersect(framebuffer->getRect());
281 offset = clippedRect.tl;
282
283 buffer.setPF(format);
284 buffer.setSize(clippedRect.width(), clippedRect.height());
285
286 // Bail out early to avoid pestering the framebuffer with
287 // bogus coordinates
288 if (clippedRect.area() == 0)
289 return;
290
291 data = framebuffer->getBuffer(buffer.getRect(offset), &stride);
292 buffer.imageRect(buffer.getRect(), data, stride);
293
294 diff = offset.subtract(rawOffset);
295 for (int y = 0;y < buffer.height();y++) {
296 for (int x = 0;x < buffer.width();x++) {
297 size_t idx;
298 rdr::U8 bg[4], fg[4];
299 rdr::U8 rgb[3];
300
301 idx = (y+diff.y)*cursor->width() + (x+diff.x);
302 memcpy(fg, cursor->getBuffer() + idx*4, 4);
303
304 if (fg[3] == 0x00)
305 continue;
306 else if (fg[3] == 0xff) {
307 memcpy(rgb, fg, 3);
308 } else {
309 buffer.getImage(bg, Rect(x, y, x+1, y+1));
310 format.rgbFromBuffer(rgb, bg, 1);
311 // FIXME: Gamma aware blending
312 for (int i = 0;i < 3;i++) {
313 rgb[i] = (unsigned)rgb[i]*(255-fg[3])/255 +
314 (unsigned)fg[i]*fg[3]/255;
315 }
316 }
317
318 format.bufferFromRGB(bg, rgb, 1);
319 buffer.imageRect(Rect(x, y, x+1, y+1), bg);
320 }
321 }
322 }
323