blob: a79f0460695674cfdf44c8dd65905550d991a179 [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 Ossman6a1a0d02017-02-19 15:48:17 +010050rdr::U8* Cursor::getBitmap() const
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000051{
Pierre Ossman6a1a0d02017-02-19 15:48:17 +010052 rdr::U8Array source((width()+7)/8*height());
53 memset(source.buf, 0, (width()+7)/8*height());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000054
55 int maskBytesPerRow = (width() + 7) / 8;
Pierre Ossman01cf5732010-09-30 11:53:15 +000056 const rdr::U8 *data_ptr = data;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000057 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 Ossman6a1a0d02017-02-19 15:48:17 +010061 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 Kaplinskya2adc8d2006-05-25 05:01:55 +000073 source.buf[byte] |= (1 << bit);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000074 }
Pierre Ossman6a1a0d02017-02-19 15:48:17 +010075 data_ptr += 4;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000076 }
77 }
Pierre Ossman6a1a0d02017-02-19 15:48:17 +010078
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000079 return source.takeBuf();
80}
81
Pierre Ossman6a1a0d02017-02-19 15:48:17 +010082rdr::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 Kaplinskya2adc8d2006-05-25 05:01:55 +0000102// 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
108void Cursor::crop()
109{
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100110 Rect busy = Rect(0, 0, width_, height_);
111 busy = busy.intersect(Rect(hotspot_.x, hotspot_.y,
112 hotspot_.x+1, hotspot_.y+1));
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000113 int x, y;
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100114 rdr::U8 *data_ptr = data;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000115 for (y = 0; y < height(); y++) {
116 for (x = 0; x < width(); x++) {
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100117 if (data_ptr[3] > 0) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000118 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 Ossman6a1a0d02017-02-19 15:48:17 +0100123 data_ptr += 4;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000124 }
125 }
126
127 if (width() == busy.width() && height() == busy.height()) return;
128
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000129 // Copy the pixel data
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100130 int newDataLen = busy.area() * 4;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000131 rdr::U8* newData = new rdr::U8[newDataLen];
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100132 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 Kaplinskya2adc8d2006-05-25 05:01:55 +0000136 }
137
138 // Set the size and data to the new, cropped cursor.
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100139 width_ = busy.width();
140 height_ = busy.height();
141 hotspot_ = hotspot_.subtract(busy.tl);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000142 delete [] data;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000143 data = newData;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000144}
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +0100145
146RenderedCursor::RenderedCursor()
147{
148}
149
Pierre Ossmand4f718d2014-02-13 14:37:25 +0100150const rdr::U8* RenderedCursor::getBuffer(const Rect& _r, int* stride) const
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +0100151{
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
161void RenderedCursor::update(PixelBuffer* framebuffer,
162 Cursor* cursor, const Point& pos)
163{
Pierre Ossman1391fc42017-01-20 15:58:44 +0100164 Point rawOffset, diff;
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +0100165 Rect clippedRect;
166
167 const rdr::U8* data;
168 int stride;
169
170 assert(framebuffer);
171 assert(cursor);
172
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +0100173 format = framebuffer->getPF();
174 width_ = framebuffer->width();
175 height_ = framebuffer->height();
176
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100177 rawOffset = pos.subtract(cursor->hotspot());
178 clippedRect = Rect(0, 0, cursor->width(), cursor->height())
179 .translate(rawOffset)
180 .intersect(framebuffer->getRect());
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +0100181 offset = clippedRect.tl;
182
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100183 buffer.setPF(format);
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +0100184 buffer.setSize(clippedRect.width(), clippedRect.height());
185
186 data = framebuffer->getBuffer(buffer.getRect(offset), &stride);
187 buffer.imageRect(buffer.getRect(), data, stride);
188
Pierre Ossman1391fc42017-01-20 15:58:44 +0100189 diff = offset.subtract(rawOffset);
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100190 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 Ossman1391fc42017-01-20 15:58:44 +0100195
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100196 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 Ossman6ea6e1a2014-02-12 16:33:43 +0100217}