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 | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame^] | 50 | rdr::U8* Cursor::getBitmap() const |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 51 | { |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame^] | 52 | rdr::U8Array source((width()+7)/8*height()); |
| 53 | memset(source.buf, 0, (width()+7)/8*height()); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 54 | |
| 55 | int maskBytesPerRow = (width() + 7) / 8; |
Pierre Ossman | 01cf573 | 2010-09-30 11:53:15 +0000 | [diff] [blame] | 56 | const rdr::U8 *data_ptr = data; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 57 | for (int y = 0; y < height(); y++) { |
| 58 | for (int x = 0; x < width(); x++) { |
| 59 | int byte = y * maskBytesPerRow + x / 8; |
| 60 | int bit = 7 - x % 8; |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame^] | 61 | if (data_ptr[3] >= 0x80) { |
| 62 | // Use Luma with BT.709 coefficients for grayscale |
| 63 | unsigned luma; |
| 64 | |
| 65 | luma = 0; |
| 66 | luma += (unsigned)data_ptr[0] * 13933; // 0.2126 |
| 67 | luma += (unsigned)data_ptr[1] * 46871; // 0.7152 |
| 68 | luma += (unsigned)data_ptr[2] * 4732; // 0.0722 |
| 69 | luma /= 65536; |
| 70 | |
| 71 | // Gamma compensated half intensity gray |
| 72 | if (luma > 187) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 73 | source.buf[byte] |= (1 << bit); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 74 | } |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame^] | 75 | data_ptr += 4; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 76 | } |
| 77 | } |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame^] | 78 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 79 | return source.takeBuf(); |
| 80 | } |
| 81 | |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame^] | 82 | rdr::U8* Cursor::getMask() const |
| 83 | { |
| 84 | rdr::U8Array mask((width()+7)/8*height()); |
| 85 | memset(mask.buf, 0, (width()+7)/8*height()); |
| 86 | |
| 87 | int maskBytesPerRow = (width() + 7) / 8; |
| 88 | const rdr::U8 *data_ptr = data; |
| 89 | for (int y = 0; y < height(); y++) { |
| 90 | for (int x = 0; x < width(); x++) { |
| 91 | int byte = y * maskBytesPerRow + x / 8; |
| 92 | int bit = 7 - x % 8; |
| 93 | if (data_ptr[3] >= 0x80) |
| 94 | mask.buf[byte] |= (1 << bit); |
| 95 | data_ptr += 4; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | return mask.takeBuf(); |
| 100 | } |
| 101 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 102 | // crop() determines the "busy" rectangle for the cursor - the minimum bounding |
| 103 | // rectangle containing actual pixels. This isn't the most efficient algorithm |
| 104 | // but it's short. For sanity, we make sure that the busy rectangle always |
| 105 | // includes the hotspot (the hotspot is unsigned on the wire so otherwise it |
| 106 | // would cause problems if it was above or left of the actual pixels) |
| 107 | |
| 108 | void Cursor::crop() |
| 109 | { |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame^] | 110 | Rect busy = Rect(0, 0, width_, height_); |
| 111 | busy = busy.intersect(Rect(hotspot_.x, hotspot_.y, |
| 112 | hotspot_.x+1, hotspot_.y+1)); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 113 | int x, y; |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame^] | 114 | rdr::U8 *data_ptr = data; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 115 | for (y = 0; y < height(); y++) { |
| 116 | for (x = 0; x < width(); x++) { |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame^] | 117 | if (data_ptr[3] > 0) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 118 | if (x < busy.tl.x) busy.tl.x = x; |
| 119 | if (x+1 > busy.br.x) busy.br.x = x+1; |
| 120 | if (y < busy.tl.y) busy.tl.y = y; |
| 121 | if (y+1 > busy.br.y) busy.br.y = y+1; |
| 122 | } |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame^] | 123 | data_ptr += 4; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
| 127 | if (width() == busy.width() && height() == busy.height()) return; |
| 128 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 129 | // Copy the pixel data |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame^] | 130 | int newDataLen = busy.area() * 4; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 131 | rdr::U8* newData = new rdr::U8[newDataLen]; |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame^] | 132 | data_ptr = newData; |
| 133 | for (y = busy.tl.y; y < busy.br.y; y++) { |
| 134 | memcpy(data_ptr, data + y*width()*4 + busy.tl.x*4, busy.width()*4); |
| 135 | data_ptr += busy.width()*4; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | // Set the size and data to the new, cropped cursor. |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame^] | 139 | width_ = busy.width(); |
| 140 | height_ = busy.height(); |
| 141 | hotspot_ = hotspot_.subtract(busy.tl); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 142 | delete [] data; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 143 | data = newData; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 144 | } |
Pierre Ossman | 6ea6e1a | 2014-02-12 16:33:43 +0100 | [diff] [blame] | 145 | |
| 146 | RenderedCursor::RenderedCursor() |
| 147 | { |
| 148 | } |
| 149 | |
Pierre Ossman | d4f718d | 2014-02-13 14:37:25 +0100 | [diff] [blame] | 150 | const rdr::U8* RenderedCursor::getBuffer(const Rect& _r, int* stride) const |
Pierre Ossman | 6ea6e1a | 2014-02-12 16:33:43 +0100 | [diff] [blame] | 151 | { |
| 152 | Rect r; |
| 153 | |
| 154 | r = _r.translate(offset.negate()); |
| 155 | if (!r.enclosed_by(buffer.getRect())) |
| 156 | throw Exception("RenderedCursor: Invalid area requested"); |
| 157 | |
| 158 | return buffer.getBuffer(r, stride); |
| 159 | } |
| 160 | |
| 161 | void RenderedCursor::update(PixelBuffer* framebuffer, |
| 162 | Cursor* cursor, const Point& pos) |
| 163 | { |
Pierre Ossman | 1391fc4 | 2017-01-20 15:58:44 +0100 | [diff] [blame] | 164 | Point rawOffset, diff; |
Pierre Ossman | 6ea6e1a | 2014-02-12 16:33:43 +0100 | [diff] [blame] | 165 | Rect clippedRect; |
| 166 | |
| 167 | const rdr::U8* data; |
| 168 | int stride; |
| 169 | |
| 170 | assert(framebuffer); |
| 171 | assert(cursor); |
| 172 | |
Pierre Ossman | 6ea6e1a | 2014-02-12 16:33:43 +0100 | [diff] [blame] | 173 | format = framebuffer->getPF(); |
| 174 | width_ = framebuffer->width(); |
| 175 | height_ = framebuffer->height(); |
| 176 | |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame^] | 177 | rawOffset = pos.subtract(cursor->hotspot()); |
| 178 | clippedRect = Rect(0, 0, cursor->width(), cursor->height()) |
| 179 | .translate(rawOffset) |
| 180 | .intersect(framebuffer->getRect()); |
Pierre Ossman | 6ea6e1a | 2014-02-12 16:33:43 +0100 | [diff] [blame] | 181 | offset = clippedRect.tl; |
| 182 | |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame^] | 183 | buffer.setPF(format); |
Pierre Ossman | 6ea6e1a | 2014-02-12 16:33:43 +0100 | [diff] [blame] | 184 | buffer.setSize(clippedRect.width(), clippedRect.height()); |
| 185 | |
| 186 | data = framebuffer->getBuffer(buffer.getRect(offset), &stride); |
| 187 | buffer.imageRect(buffer.getRect(), data, stride); |
| 188 | |
Pierre Ossman | 1391fc4 | 2017-01-20 15:58:44 +0100 | [diff] [blame] | 189 | diff = offset.subtract(rawOffset); |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame^] | 190 | for (int y = 0;y < buffer.height();y++) { |
| 191 | for (int x = 0;x < buffer.width();x++) { |
| 192 | size_t idx; |
| 193 | rdr::U8 bg[4], fg[4]; |
| 194 | rdr::U8 rgb[3]; |
Pierre Ossman | 1391fc4 | 2017-01-20 15:58:44 +0100 | [diff] [blame] | 195 | |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame^] | 196 | idx = (y+diff.y)*cursor->width() + (x+diff.x); |
| 197 | memcpy(fg, cursor->getBuffer() + idx*4, 4); |
| 198 | |
| 199 | if (fg[3] == 0x00) |
| 200 | continue; |
| 201 | else if (fg[3] == 0xff) { |
| 202 | memcpy(rgb, fg, 3); |
| 203 | } else { |
| 204 | buffer.getImage(bg, Rect(x, y, x+1, y+1)); |
| 205 | format.rgbFromBuffer(rgb, bg, 1); |
| 206 | // FIXME: Gamma aware blending |
| 207 | for (int i = 0;i < 3;i++) { |
| 208 | rgb[i] = (unsigned)rgb[i]*(255-fg[3])/255 + |
| 209 | (unsigned)fg[i]*fg[3]/255; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | format.bufferFromRGB(bg, rgb, 1); |
| 214 | buffer.imageRect(Rect(x, y, x+1, y+1), bg); |
| 215 | } |
| 216 | } |
Pierre Ossman | 6ea6e1a | 2014-02-12 16:33:43 +0100 | [diff] [blame] | 217 | } |