blob: d7b536deaec89bb0a754dff3ea86a480f6501722 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Pierre Ossman6a1a0d02017-02-19 15:48:17 +01002 * Copyright 2014-2017 Pierre Ossman for Cendio AB
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00003 *
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 */
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +010019#include <assert.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000020#include <string.h>
21#include <rfb/Cursor.h>
22#include <rfb/LogWriter.h>
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +010023#include <rfb/Exception.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000024
25using namespace rfb;
26
27static LogWriter vlog("Cursor");
28
Pierre Ossman6a1a0d02017-02-19 15:48:17 +010029Cursor::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);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000035}
36
Pierre Ossman6a1a0d02017-02-19 15:48:17 +010037Cursor::Cursor(const Cursor& other) :
38 width_(other.width_), height_(other.height_),
39 hotspot_(other.hotspot_)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000040{
Pierre Ossman6a1a0d02017-02-19 15:48:17 +010041 data = new rdr::U8[width_*height_*4];
42 memcpy(data, other.data, width_*height_*4);
43}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000044
Pierre Ossman6a1a0d02017-02-19 15:48:17 +010045Cursor::~Cursor()
46{
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000047 delete [] data;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000048}
49
Pierre Ossman6b75e772017-02-21 12:58:01 +010050static 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
57static 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
73static unsigned short srgb_to_lin(unsigned char srgb)
74{
75 return ipow((unsigned)srgb * 65535 / 255, pow223);
76}
77
78// Floyd-Steinberg dithering
Pierre Ossman6c38a082018-11-23 16:12:17 +010079static void dither(int width, int height, rdr::S32* data)
Pierre Ossman6b75e772017-02-21 12:58:01 +010080{
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
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100122rdr::U8* Cursor::getBitmap() const
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000123{
Pierre Ossman6b75e772017-02-21 12:58:01 +0100124 // First step is converting to luminance
Pierre Ossman6c38a082018-11-23 16:12:17 +0100125 rdr::S32Array luminance(width()*height());
126 rdr::S32 *lum_ptr = luminance.buf;
Pierre Ossman6b75e772017-02-21 12:58:01 +0100127 const rdr::U8 *data_ptr = data;
128 for (int y = 0; y < height(); y++) {
129 for (int x = 0; x < width(); x++) {
Pierre Ossman6c38a082018-11-23 16:12:17 +0100130 rdr::S32 lum;
Pierre Ossman6b75e772017-02-21 12:58:01 +0100131
Pierre Ossmanbeb59a42018-11-07 21:36:05 +0100132 // 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;
Pierre Ossman6b75e772017-02-21 12:58:01 +0100140 data_ptr += 4;
141 }
142 }
143
144 // Then diterhing
Pierre Ossmanbeb59a42018-11-07 21:36:05 +0100145 dither(width(), height(), luminance.buf);
Pierre Ossman6b75e772017-02-21 12:58:01 +0100146
147 // Then conversion to a bit mask
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100148 rdr::U8Array source((width()+7)/8*height());
149 memset(source.buf, 0, (width()+7)/8*height());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000150 int maskBytesPerRow = (width() + 7) / 8;
Pierre Ossmanbeb59a42018-11-07 21:36:05 +0100151 lum_ptr = luminance.buf;
Pierre Ossman6b75e772017-02-21 12:58:01 +0100152 data_ptr = data;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000153 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;
Pierre Ossman6b75e772017-02-21 12:58:01 +0100157 if (*lum_ptr > 32767)
158 source.buf[byte] |= (1 << bit);
159 lum_ptr++;
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100160 data_ptr += 4;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000161 }
162 }
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100163
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000164 return source.takeBuf();
165}
166
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100167rdr::U8* Cursor::getMask() const
168{
Pierre Ossman6b75e772017-02-21 12:58:01 +0100169 // First step is converting to integer array
Pierre Ossman6c38a082018-11-23 16:12:17 +0100170 rdr::S32Array alpha(width()*height());
171 rdr::S32 *alpha_ptr = alpha.buf;
Pierre Ossman6b75e772017-02-21 12:58:01 +0100172 const rdr::U8 *data_ptr = data;
173 for (int y = 0; y < height(); y++) {
174 for (int x = 0; x < width(); x++) {
Pierre Ossmanbeb59a42018-11-07 21:36:05 +0100175 *alpha_ptr++ = (rdr::U32)data_ptr[3] * 65535 / 255;
Pierre Ossman6b75e772017-02-21 12:58:01 +0100176 data_ptr += 4;
177 }
178 }
179
180 // Then diterhing
Pierre Ossmanbeb59a42018-11-07 21:36:05 +0100181 dither(width(), height(), alpha.buf);
Pierre Ossman6b75e772017-02-21 12:58:01 +0100182
183 // Then conversion to a bit mask
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100184 rdr::U8Array mask((width()+7)/8*height());
185 memset(mask.buf, 0, (width()+7)/8*height());
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100186 int maskBytesPerRow = (width() + 7) / 8;
Pierre Ossmanbeb59a42018-11-07 21:36:05 +0100187 alpha_ptr = alpha.buf;
Pierre Ossman6b75e772017-02-21 12:58:01 +0100188 data_ptr = data;
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100189 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;
Pierre Ossman6b75e772017-02-21 12:58:01 +0100193 if (*alpha_ptr > 32767)
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100194 mask.buf[byte] |= (1 << bit);
Pierre Ossman6b75e772017-02-21 12:58:01 +0100195 alpha_ptr++;
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100196 data_ptr += 4;
197 }
198 }
199
200 return mask.takeBuf();
201}
202
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000203// 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
209void Cursor::crop()
210{
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100211 Rect busy = Rect(0, 0, width_, height_);
212 busy = busy.intersect(Rect(hotspot_.x, hotspot_.y,
213 hotspot_.x+1, hotspot_.y+1));
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000214 int x, y;
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100215 rdr::U8 *data_ptr = data;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000216 for (y = 0; y < height(); y++) {
217 for (x = 0; x < width(); x++) {
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100218 if (data_ptr[3] > 0) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000219 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 }
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100224 data_ptr += 4;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000225 }
226 }
227
228 if (width() == busy.width() && height() == busy.height()) return;
229
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000230 // Copy the pixel data
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100231 int newDataLen = busy.area() * 4;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000232 rdr::U8* newData = new rdr::U8[newDataLen];
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100233 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;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000237 }
238
239 // Set the size and data to the new, cropped cursor.
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100240 width_ = busy.width();
241 height_ = busy.height();
242 hotspot_ = hotspot_.subtract(busy.tl);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000243 delete [] data;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000244 data = newData;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000245}
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +0100246
247RenderedCursor::RenderedCursor()
248{
249}
250
Pierre Ossmand4f718d2014-02-13 14:37:25 +0100251const rdr::U8* RenderedCursor::getBuffer(const Rect& _r, int* stride) const
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +0100252{
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
262void RenderedCursor::update(PixelBuffer* framebuffer,
263 Cursor* cursor, const Point& pos)
264{
Pierre Ossman1391fc42017-01-20 15:58:44 +0100265 Point rawOffset, diff;
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +0100266 Rect clippedRect;
267
268 const rdr::U8* data;
269 int stride;
270
271 assert(framebuffer);
272 assert(cursor);
273
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +0100274 format = framebuffer->getPF();
275 width_ = framebuffer->width();
276 height_ = framebuffer->height();
277
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100278 rawOffset = pos.subtract(cursor->hotspot());
279 clippedRect = Rect(0, 0, cursor->width(), cursor->height())
280 .translate(rawOffset)
281 .intersect(framebuffer->getRect());
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +0100282 offset = clippedRect.tl;
283
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100284 buffer.setPF(format);
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +0100285 buffer.setSize(clippedRect.width(), clippedRect.height());
286
Pierre Ossman28055e22017-02-23 13:12:54 +0100287 // Bail out early to avoid pestering the framebuffer with
288 // bogus coordinates
289 if (clippedRect.area() == 0)
290 return;
291
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +0100292 data = framebuffer->getBuffer(buffer.getRect(offset), &stride);
293 buffer.imageRect(buffer.getRect(), data, stride);
294
Pierre Ossman1391fc42017-01-20 15:58:44 +0100295 diff = offset.subtract(rawOffset);
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100296 for (int y = 0;y < buffer.height();y++) {
297 for (int x = 0;x < buffer.width();x++) {
298 size_t idx;
299 rdr::U8 bg[4], fg[4];
300 rdr::U8 rgb[3];
Pierre Ossman1391fc42017-01-20 15:58:44 +0100301
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100302 idx = (y+diff.y)*cursor->width() + (x+diff.x);
303 memcpy(fg, cursor->getBuffer() + idx*4, 4);
304
305 if (fg[3] == 0x00)
306 continue;
307 else if (fg[3] == 0xff) {
308 memcpy(rgb, fg, 3);
309 } else {
310 buffer.getImage(bg, Rect(x, y, x+1, y+1));
311 format.rgbFromBuffer(rgb, bg, 1);
312 // FIXME: Gamma aware blending
313 for (int i = 0;i < 3;i++) {
314 rgb[i] = (unsigned)rgb[i]*(255-fg[3])/255 +
315 (unsigned)fg[i]*fg[3]/255;
316 }
317 }
318
319 format.bufferFromRGB(bg, rgb, 1);
320 buffer.imageRect(Rect(x, y, x+1, y+1), bg);
321 }
322 }
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +0100323}