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 |
Pierre Ossman | 6c38a08 | 2018-11-23 16:12:17 +0100 | [diff] [blame] | 79 | static void dither(int width, int height, rdr::S32* data) |
Pierre Ossman | 6b75e77 | 2017-02-21 12:58:01 +0100 | [diff] [blame] | 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 |
Pierre Ossman | 6c38a08 | 2018-11-23 16:12:17 +0100 | [diff] [blame] | 125 | rdr::S32Array luminance(width()*height()); |
| 126 | rdr::S32 *lum_ptr = luminance.buf; |
Pierre Ossman | 6b75e77 | 2017-02-21 12:58:01 +0100 | [diff] [blame] | 127 | const rdr::U8 *data_ptr = data; |
| 128 | for (int y = 0; y < height(); y++) { |
| 129 | for (int x = 0; x < width(); x++) { |
Pierre Ossman | 6c38a08 | 2018-11-23 16:12:17 +0100 | [diff] [blame] | 130 | rdr::S32 lum; |
Pierre Ossman | 6b75e77 | 2017-02-21 12:58:01 +0100 | [diff] [blame] | 131 | |
Pierre Ossman | beb59a4 | 2018-11-07 21:36:05 +0100 | [diff] [blame] | 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; |
Pierre Ossman | 6b75e77 | 2017-02-21 12:58:01 +0100 | [diff] [blame] | 140 | data_ptr += 4; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // Then diterhing |
Pierre Ossman | beb59a4 | 2018-11-07 21:36:05 +0100 | [diff] [blame] | 145 | dither(width(), height(), luminance.buf); |
Pierre Ossman | 6b75e77 | 2017-02-21 12:58:01 +0100 | [diff] [blame] | 146 | |
| 147 | // Then conversion to a bit mask |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 148 | rdr::U8Array source((width()+7)/8*height()); |
| 149 | memset(source.buf, 0, (width()+7)/8*height()); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 150 | int maskBytesPerRow = (width() + 7) / 8; |
Pierre Ossman | beb59a4 | 2018-11-07 21:36:05 +0100 | [diff] [blame] | 151 | lum_ptr = luminance.buf; |
Pierre Ossman | 6b75e77 | 2017-02-21 12:58:01 +0100 | [diff] [blame] | 152 | data_ptr = data; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 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; |
Pierre Ossman | 6b75e77 | 2017-02-21 12:58:01 +0100 | [diff] [blame] | 157 | if (*lum_ptr > 32767) |
| 158 | source.buf[byte] |= (1 << bit); |
| 159 | lum_ptr++; |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 160 | data_ptr += 4; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 161 | } |
| 162 | } |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 163 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 164 | return source.takeBuf(); |
| 165 | } |
| 166 | |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 167 | rdr::U8* Cursor::getMask() const |
| 168 | { |
Pierre Ossman | 6b75e77 | 2017-02-21 12:58:01 +0100 | [diff] [blame] | 169 | // First step is converting to integer array |
Pierre Ossman | 6c38a08 | 2018-11-23 16:12:17 +0100 | [diff] [blame] | 170 | rdr::S32Array alpha(width()*height()); |
| 171 | rdr::S32 *alpha_ptr = alpha.buf; |
Pierre Ossman | 6b75e77 | 2017-02-21 12:58:01 +0100 | [diff] [blame] | 172 | const rdr::U8 *data_ptr = data; |
| 173 | for (int y = 0; y < height(); y++) { |
| 174 | for (int x = 0; x < width(); x++) { |
Pierre Ossman | beb59a4 | 2018-11-07 21:36:05 +0100 | [diff] [blame] | 175 | *alpha_ptr++ = (rdr::U32)data_ptr[3] * 65535 / 255; |
Pierre Ossman | 6b75e77 | 2017-02-21 12:58:01 +0100 | [diff] [blame] | 176 | data_ptr += 4; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | // Then diterhing |
Pierre Ossman | beb59a4 | 2018-11-07 21:36:05 +0100 | [diff] [blame] | 181 | dither(width(), height(), alpha.buf); |
Pierre Ossman | 6b75e77 | 2017-02-21 12:58:01 +0100 | [diff] [blame] | 182 | |
| 183 | // Then conversion to a bit mask |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 184 | rdr::U8Array mask((width()+7)/8*height()); |
| 185 | memset(mask.buf, 0, (width()+7)/8*height()); |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 186 | int maskBytesPerRow = (width() + 7) / 8; |
Pierre Ossman | beb59a4 | 2018-11-07 21:36:05 +0100 | [diff] [blame] | 187 | alpha_ptr = alpha.buf; |
Pierre Ossman | 6b75e77 | 2017-02-21 12:58:01 +0100 | [diff] [blame] | 188 | data_ptr = data; |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 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; |
Pierre Ossman | 6b75e77 | 2017-02-21 12:58:01 +0100 | [diff] [blame] | 193 | if (*alpha_ptr > 32767) |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 194 | mask.buf[byte] |= (1 << bit); |
Pierre Ossman | 6b75e77 | 2017-02-21 12:58:01 +0100 | [diff] [blame] | 195 | alpha_ptr++; |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 196 | data_ptr += 4; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | return mask.takeBuf(); |
| 201 | } |
| 202 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 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 | |
| 209 | void Cursor::crop() |
| 210 | { |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 211 | Rect busy = Rect(0, 0, width_, height_); |
| 212 | busy = busy.intersect(Rect(hotspot_.x, hotspot_.y, |
| 213 | hotspot_.x+1, hotspot_.y+1)); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 214 | int x, y; |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 215 | rdr::U8 *data_ptr = data; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 216 | for (y = 0; y < height(); y++) { |
| 217 | for (x = 0; x < width(); x++) { |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 218 | if (data_ptr[3] > 0) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 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 | } |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 224 | data_ptr += 4; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
| 228 | if (width() == busy.width() && height() == busy.height()) return; |
| 229 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 230 | // Copy the pixel data |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 231 | int newDataLen = busy.area() * 4; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 232 | rdr::U8* newData = new rdr::U8[newDataLen]; |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 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; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | // Set the size and data to the new, cropped cursor. |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 240 | width_ = busy.width(); |
| 241 | height_ = busy.height(); |
| 242 | hotspot_ = hotspot_.subtract(busy.tl); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 243 | delete [] data; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 244 | data = newData; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 245 | } |
Pierre Ossman | 6ea6e1a | 2014-02-12 16:33:43 +0100 | [diff] [blame] | 246 | |
| 247 | RenderedCursor::RenderedCursor() |
| 248 | { |
| 249 | } |
| 250 | |
Pierre Ossman | d4f718d | 2014-02-13 14:37:25 +0100 | [diff] [blame] | 251 | const rdr::U8* RenderedCursor::getBuffer(const Rect& _r, int* stride) const |
Pierre Ossman | 6ea6e1a | 2014-02-12 16:33:43 +0100 | [diff] [blame] | 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 | |
| 262 | void RenderedCursor::update(PixelBuffer* framebuffer, |
| 263 | Cursor* cursor, const Point& pos) |
| 264 | { |
Pierre Ossman | 1391fc4 | 2017-01-20 15:58:44 +0100 | [diff] [blame] | 265 | Point rawOffset, diff; |
Pierre Ossman | 6ea6e1a | 2014-02-12 16:33:43 +0100 | [diff] [blame] | 266 | Rect clippedRect; |
| 267 | |
| 268 | const rdr::U8* data; |
| 269 | int stride; |
| 270 | |
| 271 | assert(framebuffer); |
| 272 | assert(cursor); |
| 273 | |
Pierre Ossman | 6ea6e1a | 2014-02-12 16:33:43 +0100 | [diff] [blame] | 274 | format = framebuffer->getPF(); |
| 275 | width_ = framebuffer->width(); |
| 276 | height_ = framebuffer->height(); |
| 277 | |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 278 | rawOffset = pos.subtract(cursor->hotspot()); |
| 279 | clippedRect = Rect(0, 0, cursor->width(), cursor->height()) |
| 280 | .translate(rawOffset) |
| 281 | .intersect(framebuffer->getRect()); |
Pierre Ossman | 6ea6e1a | 2014-02-12 16:33:43 +0100 | [diff] [blame] | 282 | offset = clippedRect.tl; |
| 283 | |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 284 | buffer.setPF(format); |
Pierre Ossman | 6ea6e1a | 2014-02-12 16:33:43 +0100 | [diff] [blame] | 285 | buffer.setSize(clippedRect.width(), clippedRect.height()); |
| 286 | |
Pierre Ossman | 28055e2 | 2017-02-23 13:12:54 +0100 | [diff] [blame] | 287 | // Bail out early to avoid pestering the framebuffer with |
| 288 | // bogus coordinates |
| 289 | if (clippedRect.area() == 0) |
| 290 | return; |
| 291 | |
Pierre Ossman | 6ea6e1a | 2014-02-12 16:33:43 +0100 | [diff] [blame] | 292 | data = framebuffer->getBuffer(buffer.getRect(offset), &stride); |
| 293 | buffer.imageRect(buffer.getRect(), data, stride); |
| 294 | |
Pierre Ossman | 1391fc4 | 2017-01-20 15:58:44 +0100 | [diff] [blame] | 295 | diff = offset.subtract(rawOffset); |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 296 | 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 Ossman | 1391fc4 | 2017-01-20 15:58:44 +0100 | [diff] [blame] | 301 | |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 302 | 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 Ossman | 6ea6e1a | 2014-02-12 16:33:43 +0100 | [diff] [blame] | 323 | } |