Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 2 | * Copyright 2014-2017 Pierre Ossman for Cendio AB |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 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 | */ |
Pierre Ossman | 6ea6e1a | 2014-02-12 16:33:43 +0100 | [diff] [blame] | 19 | #include <assert.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 20 | #include <string.h> |
| 21 | #include <rfb/Cursor.h> |
| 22 | #include <rfb/LogWriter.h> |
Pierre Ossman | 6ea6e1a | 2014-02-12 16:33:43 +0100 | [diff] [blame] | 23 | #include <rfb/Exception.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace rfb; |
| 26 | |
| 27 | static LogWriter vlog("Cursor"); |
| 28 | |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 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); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 37 | Cursor::Cursor(const Cursor& other) : |
| 38 | width_(other.width_), height_(other.height_), |
| 39 | hotspot_(other.hotspot_) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 40 | { |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 41 | data = new rdr::U8[width_*height_*4]; |
| 42 | memcpy(data, other.data, width_*height_*4); |
| 43 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 44 | |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 45 | Cursor::~Cursor() |
| 46 | { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 47 | delete [] data; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Pierre Ossman | 6b75e77 | 2017-02-21 12:58:01 +0100 | [diff] [blame] | 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 | |
| 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 | |
| 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 |
| 79 | static 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 Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 122 | rdr::U8* Cursor::getBitmap() const |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 123 | { |
Pierre Ossman | 6b75e77 | 2017-02-21 12:58:01 +0100 | [diff] [blame] | 124 | // 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 Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 146 | rdr::U8Array source((width()+7)/8*height()); |
| 147 | memset(source.buf, 0, (width()+7)/8*height()); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 148 | int maskBytesPerRow = (width() + 7) / 8; |
Pierre Ossman | 6b75e77 | 2017-02-21 12:58:01 +0100 | [diff] [blame] | 149 | lum_ptr = luminance; |
| 150 | data_ptr = data; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 151 | 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 Ossman | 6b75e77 | 2017-02-21 12:58:01 +0100 | [diff] [blame] | 155 | if (*lum_ptr > 32767) |
| 156 | source.buf[byte] |= (1 << bit); |
| 157 | lum_ptr++; |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 158 | data_ptr += 4; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 159 | } |
| 160 | } |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 161 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 162 | return source.takeBuf(); |
| 163 | } |
| 164 | |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 165 | rdr::U8* Cursor::getMask() const |
| 166 | { |
Pierre Ossman | 6b75e77 | 2017-02-21 12:58:01 +0100 | [diff] [blame] | 167 | // 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 Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 183 | rdr::U8Array mask((width()+7)/8*height()); |
| 184 | memset(mask.buf, 0, (width()+7)/8*height()); |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 185 | int maskBytesPerRow = (width() + 7) / 8; |
Pierre Ossman | 6b75e77 | 2017-02-21 12:58:01 +0100 | [diff] [blame] | 186 | alpha_ptr = alpha; |
| 187 | data_ptr = data; |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 188 | 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 Ossman | 6b75e77 | 2017-02-21 12:58:01 +0100 | [diff] [blame] | 192 | if (*alpha_ptr > 32767) |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 193 | mask.buf[byte] |= (1 << bit); |
Pierre Ossman | 6b75e77 | 2017-02-21 12:58:01 +0100 | [diff] [blame] | 194 | alpha_ptr++; |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 195 | data_ptr += 4; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | return mask.takeBuf(); |
| 200 | } |
| 201 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 202 | // 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 | |
| 208 | void Cursor::crop() |
| 209 | { |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 210 | Rect busy = Rect(0, 0, width_, height_); |
| 211 | busy = busy.intersect(Rect(hotspot_.x, hotspot_.y, |
| 212 | hotspot_.x+1, hotspot_.y+1)); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 213 | int x, y; |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 214 | rdr::U8 *data_ptr = data; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 215 | for (y = 0; y < height(); y++) { |
| 216 | for (x = 0; x < width(); x++) { |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 217 | if (data_ptr[3] > 0) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 218 | 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 Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 223 | data_ptr += 4; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
| 227 | if (width() == busy.width() && height() == busy.height()) return; |
| 228 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 229 | // Copy the pixel data |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 230 | int newDataLen = busy.area() * 4; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 231 | rdr::U8* newData = new rdr::U8[newDataLen]; |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 232 | 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 Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | // Set the size and data to the new, cropped cursor. |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 239 | width_ = busy.width(); |
| 240 | height_ = busy.height(); |
| 241 | hotspot_ = hotspot_.subtract(busy.tl); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 242 | delete [] data; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 243 | data = newData; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 244 | } |
Pierre Ossman | 6ea6e1a | 2014-02-12 16:33:43 +0100 | [diff] [blame] | 245 | |
| 246 | RenderedCursor::RenderedCursor() |
| 247 | { |
| 248 | } |
| 249 | |
Pierre Ossman | d4f718d | 2014-02-13 14:37:25 +0100 | [diff] [blame] | 250 | const rdr::U8* RenderedCursor::getBuffer(const Rect& _r, int* stride) const |
Pierre Ossman | 6ea6e1a | 2014-02-12 16:33:43 +0100 | [diff] [blame] | 251 | { |
| 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 | |
| 261 | void RenderedCursor::update(PixelBuffer* framebuffer, |
| 262 | Cursor* cursor, const Point& pos) |
| 263 | { |
Pierre Ossman | 1391fc4 | 2017-01-20 15:58:44 +0100 | [diff] [blame] | 264 | Point rawOffset, diff; |
Pierre Ossman | 6ea6e1a | 2014-02-12 16:33:43 +0100 | [diff] [blame] | 265 | Rect clippedRect; |
| 266 | |
| 267 | const rdr::U8* data; |
| 268 | int stride; |
| 269 | |
| 270 | assert(framebuffer); |
| 271 | assert(cursor); |
| 272 | |
Pierre Ossman | 6ea6e1a | 2014-02-12 16:33:43 +0100 | [diff] [blame] | 273 | format = framebuffer->getPF(); |
| 274 | width_ = framebuffer->width(); |
| 275 | height_ = framebuffer->height(); |
| 276 | |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 277 | rawOffset = pos.subtract(cursor->hotspot()); |
| 278 | clippedRect = Rect(0, 0, cursor->width(), cursor->height()) |
| 279 | .translate(rawOffset) |
| 280 | .intersect(framebuffer->getRect()); |
Pierre Ossman | 6ea6e1a | 2014-02-12 16:33:43 +0100 | [diff] [blame] | 281 | offset = clippedRect.tl; |
| 282 | |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 283 | buffer.setPF(format); |
Pierre Ossman | 6ea6e1a | 2014-02-12 16:33:43 +0100 | [diff] [blame] | 284 | buffer.setSize(clippedRect.width(), clippedRect.height()); |
| 285 | |
Pierre Ossman | 28055e2 | 2017-02-23 13:12:54 +0100 | [diff] [blame] | 286 | // Bail out early to avoid pestering the framebuffer with |
| 287 | // bogus coordinates |
| 288 | if (clippedRect.area() == 0) |
| 289 | return; |
| 290 | |
Pierre Ossman | 6ea6e1a | 2014-02-12 16:33:43 +0100 | [diff] [blame] | 291 | data = framebuffer->getBuffer(buffer.getRect(offset), &stride); |
| 292 | buffer.imageRect(buffer.getRect(), data, stride); |
| 293 | |
Pierre Ossman | 1391fc4 | 2017-01-20 15:58:44 +0100 | [diff] [blame] | 294 | diff = offset.subtract(rawOffset); |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 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]; |
Pierre Ossman | 1391fc4 | 2017-01-20 15:58:44 +0100 | [diff] [blame] | 300 | |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 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 | } |
Pierre Ossman | 6ea6e1a | 2014-02-12 16:33:43 +0100 | [diff] [blame] | 322 | } |