blob: ffbe955ca950264039468f4b01b73db43de8e5b2 [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
79static void dither(int width, int height, int* 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
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
125 int luminance[width()*height()];
126 int *lum_ptr = luminance;
127 const rdr::U8 *data_ptr = data;
128 for (int y = 0; y < height(); y++) {
129 for (int x = 0; x < width(); x++) {
130 // Use BT.709 coefficients for grayscale
131 *lum_ptr = 0;
132 *lum_ptr += (int)srgb_to_lin(data_ptr[0]) * 6947; // 0.2126
133 *lum_ptr += (int)srgb_to_lin(data_ptr[1]) * 23436; // 0.7152
134 *lum_ptr += (int)srgb_to_lin(data_ptr[2]) * 2366; // 0.0722
135 *lum_ptr /= 32768;
136
137 lum_ptr++;
138 data_ptr += 4;
139 }
140 }
141
142 // Then diterhing
143 dither(width(), height(), luminance);
144
145 // Then conversion to a bit mask
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100146 rdr::U8Array source((width()+7)/8*height());
147 memset(source.buf, 0, (width()+7)/8*height());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000148 int maskBytesPerRow = (width() + 7) / 8;
Pierre Ossman6b75e772017-02-21 12:58:01 +0100149 lum_ptr = luminance;
150 data_ptr = data;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000151 for (int y = 0; y < height(); y++) {
152 for (int x = 0; x < width(); x++) {
153 int byte = y * maskBytesPerRow + x / 8;
154 int bit = 7 - x % 8;
Pierre Ossman6b75e772017-02-21 12:58:01 +0100155 if (*lum_ptr > 32767)
156 source.buf[byte] |= (1 << bit);
157 lum_ptr++;
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100158 data_ptr += 4;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000159 }
160 }
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100161
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000162 return source.takeBuf();
163}
164
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100165rdr::U8* Cursor::getMask() const
166{
Pierre Ossman6b75e772017-02-21 12:58:01 +0100167 // First step is converting to integer array
168 int alpha[width()*height()];
169 int *alpha_ptr = alpha;
170 const rdr::U8 *data_ptr = data;
171 for (int y = 0; y < height(); y++) {
172 for (int x = 0; x < width(); x++) {
173 *alpha_ptr = (int)data_ptr[3] * 65535 / 255;
174 alpha_ptr++;
175 data_ptr += 4;
176 }
177 }
178
179 // Then diterhing
180 dither(width(), height(), alpha);
181
182 // Then conversion to a bit mask
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100183 rdr::U8Array mask((width()+7)/8*height());
184 memset(mask.buf, 0, (width()+7)/8*height());
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100185 int maskBytesPerRow = (width() + 7) / 8;
Pierre Ossman6b75e772017-02-21 12:58:01 +0100186 alpha_ptr = alpha;
187 data_ptr = data;
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100188 for (int y = 0; y < height(); y++) {
189 for (int x = 0; x < width(); x++) {
190 int byte = y * maskBytesPerRow + x / 8;
191 int bit = 7 - x % 8;
Pierre Ossman6b75e772017-02-21 12:58:01 +0100192 if (*alpha_ptr > 32767)
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100193 mask.buf[byte] |= (1 << bit);
Pierre Ossman6b75e772017-02-21 12:58:01 +0100194 alpha_ptr++;
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100195 data_ptr += 4;
196 }
197 }
198
199 return mask.takeBuf();
200}
201
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000202// crop() determines the "busy" rectangle for the cursor - the minimum bounding
203// rectangle containing actual pixels. This isn't the most efficient algorithm
204// but it's short. For sanity, we make sure that the busy rectangle always
205// includes the hotspot (the hotspot is unsigned on the wire so otherwise it
206// would cause problems if it was above or left of the actual pixels)
207
208void Cursor::crop()
209{
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100210 Rect busy = Rect(0, 0, width_, height_);
211 busy = busy.intersect(Rect(hotspot_.x, hotspot_.y,
212 hotspot_.x+1, hotspot_.y+1));
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000213 int x, y;
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100214 rdr::U8 *data_ptr = data;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000215 for (y = 0; y < height(); y++) {
216 for (x = 0; x < width(); x++) {
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100217 if (data_ptr[3] > 0) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000218 if (x < busy.tl.x) busy.tl.x = x;
219 if (x+1 > busy.br.x) busy.br.x = x+1;
220 if (y < busy.tl.y) busy.tl.y = y;
221 if (y+1 > busy.br.y) busy.br.y = y+1;
222 }
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100223 data_ptr += 4;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000224 }
225 }
226
227 if (width() == busy.width() && height() == busy.height()) return;
228
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000229 // Copy the pixel data
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100230 int newDataLen = busy.area() * 4;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000231 rdr::U8* newData = new rdr::U8[newDataLen];
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100232 data_ptr = newData;
233 for (y = busy.tl.y; y < busy.br.y; y++) {
234 memcpy(data_ptr, data + y*width()*4 + busy.tl.x*4, busy.width()*4);
235 data_ptr += busy.width()*4;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000236 }
237
238 // Set the size and data to the new, cropped cursor.
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100239 width_ = busy.width();
240 height_ = busy.height();
241 hotspot_ = hotspot_.subtract(busy.tl);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000242 delete [] data;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000243 data = newData;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000244}
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +0100245
246RenderedCursor::RenderedCursor()
247{
248}
249
Pierre Ossmand4f718d2014-02-13 14:37:25 +0100250const rdr::U8* RenderedCursor::getBuffer(const Rect& _r, int* stride) const
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +0100251{
252 Rect r;
253
254 r = _r.translate(offset.negate());
255 if (!r.enclosed_by(buffer.getRect()))
256 throw Exception("RenderedCursor: Invalid area requested");
257
258 return buffer.getBuffer(r, stride);
259}
260
261void RenderedCursor::update(PixelBuffer* framebuffer,
262 Cursor* cursor, const Point& pos)
263{
Pierre Ossman1391fc42017-01-20 15:58:44 +0100264 Point rawOffset, diff;
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +0100265 Rect clippedRect;
266
267 const rdr::U8* data;
268 int stride;
269
270 assert(framebuffer);
271 assert(cursor);
272
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +0100273 format = framebuffer->getPF();
274 width_ = framebuffer->width();
275 height_ = framebuffer->height();
276
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100277 rawOffset = pos.subtract(cursor->hotspot());
278 clippedRect = Rect(0, 0, cursor->width(), cursor->height())
279 .translate(rawOffset)
280 .intersect(framebuffer->getRect());
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +0100281 offset = clippedRect.tl;
282
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100283 buffer.setPF(format);
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +0100284 buffer.setSize(clippedRect.width(), clippedRect.height());
285
286 data = framebuffer->getBuffer(buffer.getRect(offset), &stride);
287 buffer.imageRect(buffer.getRect(), data, stride);
288
Pierre Ossman1391fc42017-01-20 15:58:44 +0100289 diff = offset.subtract(rawOffset);
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100290 for (int y = 0;y < buffer.height();y++) {
291 for (int x = 0;x < buffer.width();x++) {
292 size_t idx;
293 rdr::U8 bg[4], fg[4];
294 rdr::U8 rgb[3];
Pierre Ossman1391fc42017-01-20 15:58:44 +0100295
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100296 idx = (y+diff.y)*cursor->width() + (x+diff.x);
297 memcpy(fg, cursor->getBuffer() + idx*4, 4);
298
299 if (fg[3] == 0x00)
300 continue;
301 else if (fg[3] == 0xff) {
302 memcpy(rgb, fg, 3);
303 } else {
304 buffer.getImage(bg, Rect(x, y, x+1, y+1));
305 format.rgbFromBuffer(rgb, bg, 1);
306 // FIXME: Gamma aware blending
307 for (int i = 0;i < 3;i++) {
308 rgb[i] = (unsigned)rgb[i]*(255-fg[3])/255 +
309 (unsigned)fg[i]*fg[3]/255;
310 }
311 }
312
313 format.bufferFromRGB(bg, rgb, 1);
314 buffer.imageRect(Rect(x, y, x+1, y+1), bg);
315 }
316 }
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +0100317}