|
1 |
/*
|
|
2 |
* QEMU VNC display driver: tight encoding
|
|
3 |
*
|
|
4 |
* From libvncserver/libvncserver/tight.c
|
|
5 |
* Copyright (C) 2000, 2001 Const Kaplinsky. All Rights Reserved.
|
|
6 |
* Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
|
|
7 |
*
|
|
8 |
* Copyright (C) 2010 Corentin Chary <corentin.chary@gmail.com>
|
|
9 |
*
|
|
10 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11 |
* of this software and associated documentation files (the "Software"), to deal
|
|
12 |
* in the Software without restriction, including without limitation the rights
|
|
13 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14 |
* copies of the Software, and to permit persons to whom the Software is
|
|
15 |
* furnished to do so, subject to the following conditions:
|
|
16 |
*
|
|
17 |
* The above copyright notice and this permission notice shall be included in
|
|
18 |
* all copies or substantial portions of the Software.
|
|
19 |
*
|
|
20 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
23 |
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
26 |
* THE SOFTWARE.
|
|
27 |
*/
|
|
28 |
|
|
29 |
#include "qemu-common.h"
|
|
30 |
|
|
31 |
#ifdef CONFIG_VNC_JPEG
|
|
32 |
#include <stdio.h>
|
|
33 |
#include <jpeglib.h>
|
|
34 |
#endif
|
|
35 |
|
|
36 |
#include "bswap.h"
|
|
37 |
#include "qdict.h"
|
|
38 |
#include "qint.h"
|
|
39 |
#include "vnc.h"
|
|
40 |
#include "vnc-enc-tight.h"
|
|
41 |
|
|
42 |
/* Compression level stuff. The following array contains various
|
|
43 |
encoder parameters for each of 10 compression levels (0..9).
|
|
44 |
Last three parameters correspond to JPEG quality levels (0..9). */
|
|
45 |
|
|
46 |
static const struct {
|
|
47 |
int max_rect_size, max_rect_width;
|
|
48 |
int mono_min_rect_size, gradient_min_rect_size;
|
|
49 |
int idx_zlib_level, mono_zlib_level, raw_zlib_level, gradient_zlib_level;
|
|
50 |
int gradient_threshold, gradient_threshold24;
|
|
51 |
int idx_max_colors_divisor;
|
|
52 |
int jpeg_quality, jpeg_threshold, jpeg_threshold24;
|
|
53 |
} tight_conf[] = {
|
|
54 |
{ 512, 32, 6, 65536, 0, 0, 0, 0, 0, 0, 4, 5, 10000, 23000 },
|
|
55 |
{ 2048, 128, 6, 65536, 1, 1, 1, 0, 0, 0, 8, 10, 8000, 18000 },
|
|
56 |
{ 6144, 256, 8, 65536, 3, 3, 2, 0, 0, 0, 24, 15, 6500, 15000 },
|
|
57 |
{ 10240, 1024, 12, 65536, 5, 5, 3, 0, 0, 0, 32, 25, 5000, 12000 },
|
|
58 |
{ 16384, 2048, 12, 65536, 6, 6, 4, 0, 0, 0, 32, 37, 4000, 10000 },
|
|
59 |
{ 32768, 2048, 12, 4096, 7, 7, 5, 4, 150, 380, 32, 50, 3000, 8000 },
|
|
60 |
{ 65536, 2048, 16, 4096, 7, 7, 6, 4, 170, 420, 48, 60, 2000, 5000 },
|
|
61 |
{ 65536, 2048, 16, 4096, 8, 8, 7, 5, 180, 450, 64, 70, 1000, 2500 },
|
|
62 |
{ 65536, 2048, 32, 8192, 9, 9, 8, 6, 190, 475, 64, 75, 500, 1200 },
|
|
63 |
{ 65536, 2048, 32, 8192, 9, 9, 9, 6, 200, 500, 96, 80, 200, 500 }
|
|
64 |
};
|
|
65 |
|
|
66 |
/*
|
|
67 |
* Code to guess if given rectangle is suitable for smooth image
|
|
68 |
* compression (by applying "gradient" filter or JPEG coder).
|
|
69 |
*/
|
|
70 |
|
|
71 |
static uint
|
|
72 |
tight_detect_smooth_image24(VncState *vs, int w, int h)
|
|
73 |
{
|
|
74 |
int off;
|
|
75 |
int x, y, d, dx;
|
|
76 |
uint c;
|
|
77 |
uint stats[256];
|
|
78 |
int pixels = 0;
|
|
79 |
int pix, left[3];
|
|
80 |
uint errors;
|
|
81 |
unsigned char *buf = vs->tight.buffer;
|
|
82 |
|
|
83 |
/*
|
|
84 |
* If client is big-endian, color samples begin from the second
|
|
85 |
* byte (offset 1) of a 32-bit pixel value.
|
|
86 |
*/
|
|
87 |
off = !!(vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG);
|
|
88 |
|
|
89 |
memset(stats, 0, sizeof (stats));
|
|
90 |
|
|
91 |
for (y = 0, x = 0; y < h && x < w;) {
|
|
92 |
for (d = 0; d < h - y && d < w - x - VNC_TIGHT_DETECT_SUBROW_WIDTH;
|
|
93 |
d++) {
|
|
94 |
for (c = 0; c < 3; c++) {
|
|
95 |
left[c] = buf[((y+d)*w+x+d)*4+off+c] & 0xFF;
|
|
96 |
}
|
|
97 |
for (dx = 1; dx <= VNC_TIGHT_DETECT_SUBROW_WIDTH; dx++) {
|
|
98 |
for (c = 0; c < 3; c++) {
|
|
99 |
pix = buf[((y+d)*w+x+d+dx)*4+off+c] & 0xFF;
|
|
100 |
stats[abs(pix - left[c])]++;
|
|
101 |
left[c] = pix;
|
|
102 |
}
|
|
103 |
pixels++;
|
|
104 |
}
|
|
105 |
}
|
|
106 |
if (w > h) {
|
|
107 |
x += h;
|
|
108 |
y = 0;
|
|
109 |
} else {
|
|
110 |
x = 0;
|
|
111 |
y += w;
|
|
112 |
}
|
|
113 |
}
|
|
114 |
|
|
115 |
/* 95% smooth or more ... */
|
|
116 |
if (stats[0] * 33 / pixels >= 95) {
|
|
117 |
return 0;
|
|
118 |
}
|
|
119 |
|
|
120 |
errors = 0;
|
|
121 |
for (c = 1; c < 8; c++) {
|
|
122 |
errors += stats[c] * (c * c);
|
|
123 |
if (stats[c] == 0 || stats[c] > stats[c-1] * 2) {
|
|
124 |
return 0;
|
|
125 |
}
|
|
126 |
}
|
|
127 |
for (; c < 256; c++) {
|
|
128 |
errors += stats[c] * (c * c);
|
|
129 |
}
|
|
130 |
errors /= (pixels * 3 - stats[0]);
|
|
131 |
|
|
132 |
return errors;
|
|
133 |
}
|
|
134 |
|
|
135 |
#define DEFINE_DETECT_FUNCTION(bpp) \
|
|
136 |
\
|
|
137 |
static uint \
|
|
138 |
tight_detect_smooth_image##bpp(VncState *vs, int w, int h) { \
|
|
139 |
bool endian; \
|
|
140 |
uint##bpp##_t pix; \
|
|
141 |
int max[3], shift[3]; \
|
|
142 |
int x, y, d, dx; \
|
|
143 |
uint c; \
|
|
144 |
uint stats[256]; \
|
|
145 |
int pixels = 0; \
|
|
146 |
int sample, sum, left[3]; \
|
|
147 |
uint errors; \
|
|
148 |
unsigned char *buf = vs->tight.buffer; \
|
|
149 |
\
|
|
150 |
endian = ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) != \
|
|
151 |
(vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG)); \
|
|
152 |
\
|
|
153 |
\
|
|
154 |
max[0] = vs->clientds.pf.rmax; \
|
|
155 |
max[1] = vs->clientds.pf.gmax; \
|
|
156 |
max[2] = vs->clientds.pf.bmax; \
|
|
157 |
shift[0] = vs->clientds.pf.rshift; \
|
|
158 |
shift[1] = vs->clientds.pf.gshift; \
|
|
159 |
shift[2] = vs->clientds.pf.bshift; \
|
|
160 |
\
|
|
161 |
memset(stats, 0, sizeof(stats)); \
|
|
162 |
\
|
|
163 |
y = 0, x = 0; \
|
|
164 |
while (y < h && x < w) { \
|
|
165 |
for (d = 0; d < h - y && \
|
|
166 |
d < w - x - VNC_TIGHT_DETECT_SUBROW_WIDTH; d++) { \
|
|
167 |
pix = ((uint##bpp##_t *)buf)[(y+d)*w+x+d]; \
|
|
168 |
if (endian) { \
|
|
169 |
pix = bswap_##bpp(pix); \
|
|
170 |
} \
|
|
171 |
for (c = 0; c < 3; c++) { \
|
|
172 |
left[c] = (int)(pix >> shift[c] & max[c]); \
|
|
173 |
} \
|
|
174 |
for (dx = 1; dx <= VNC_TIGHT_DETECT_SUBROW_WIDTH; \
|
|
175 |
dx++) { \
|
|
176 |
pix = ((uint##bpp##_t *)buf)[(y+d)*w+x+d+dx]; \
|
|
177 |
if (endian) { \
|
|
178 |
pix = bswap_##bpp(pix); \
|
|
179 |
} \
|
|
180 |
sum = 0; \
|
|
181 |
for (c = 0; c < 3; c++) { \
|
|
182 |
sample = (int)(pix >> shift[c] & max[c]); \
|
|
183 |
sum += abs(sample - left[c]); \
|
|
184 |
left[c] = sample; \
|
|
185 |
} \
|
|
186 |
if (sum > 255) { \
|
|
187 |
sum = 255; \
|
|
188 |
} \
|
|
189 |
stats[sum]++; \
|
|
190 |
pixels++; \
|
|
191 |
} \
|
|
192 |
} \
|
|
193 |
if (w > h) { \
|
|
194 |
x += h; \
|
|
195 |
y = 0; \
|
|
196 |
} else { \
|
|
197 |
x = 0; \
|
|
198 |
y += w; \
|
|
199 |
} \
|
|
200 |
} \
|
|
201 |
\
|
|
202 |
if ((stats[0] + stats[1]) * 100 / pixels >= 90) { \
|
|
203 |
return 0; \
|
|
204 |
} \
|
|
205 |
\
|
|
206 |
errors = 0; \
|
|
207 |
for (c = 1; c < 8; c++) { \
|
|
208 |
errors += stats[c] * (c * c); \
|
|
209 |
if (stats[c] == 0 || stats[c] > stats[c-1] * 2) { \
|
|
210 |
return 0; \
|
|
211 |
} \
|
|
212 |
} \
|
|
213 |
for (; c < 256; c++) { \
|
|
214 |
errors += stats[c] * (c * c); \
|
|
215 |
} \
|
|
216 |
errors /= (pixels - stats[0]); \
|
|
217 |
\
|
|
218 |
return errors; \
|
|
219 |
}
|
|
220 |
|
|
221 |
DEFINE_DETECT_FUNCTION(16)
|
|
222 |
DEFINE_DETECT_FUNCTION(32)
|
|
223 |
|
|
224 |
static int
|
|
225 |
tight_detect_smooth_image(VncState *vs, int w, int h)
|
|
226 |
{
|
|
227 |
uint errors;
|
|
228 |
int compression = vs->tight_compression;
|
|
229 |
int quality = vs->tight_quality;
|
|
230 |
|
|
231 |
if (!vs->vd->lossy) {
|
|
232 |
return 0;
|
|
233 |
}
|
|
234 |
|
|
235 |
if (ds_get_bytes_per_pixel(vs->ds) == 1 ||
|
|
236 |
vs->clientds.pf.bytes_per_pixel == 1 ||
|
|
237 |
w < VNC_TIGHT_DETECT_MIN_WIDTH || h < VNC_TIGHT_DETECT_MIN_HEIGHT) {
|
|
238 |
return 0;
|
|
239 |
}
|
|
240 |
|
|
241 |
if (vs->tight_quality != -1) {
|
|
242 |
if (w * h < VNC_TIGHT_JPEG_MIN_RECT_SIZE) {
|
|
243 |
return 0;
|
|
244 |
}
|
|
245 |
} else {
|
|
246 |
if (w * h < tight_conf[compression].gradient_min_rect_size) {
|
|
247 |
return 0;
|
|
248 |
}
|
|
249 |
}
|
|
250 |
|
|
251 |
if (vs->clientds.pf.bytes_per_pixel == 4) {
|
|
252 |
if (vs->tight_pixel24) {
|
|
253 |
errors = tight_detect_smooth_image24(vs, w, h);
|
|
254 |
if (vs->tight_quality != -1) {
|
|
255 |
return (errors < tight_conf[quality].jpeg_threshold24);
|
|
256 |
}
|
|
257 |
return (errors < tight_conf[compression].gradient_threshold24);
|
|
258 |
} else {
|
|
259 |
errors = tight_detect_smooth_image32(vs, w, h);
|
|
260 |
}
|
|
261 |
} else {
|
|
262 |
errors = tight_detect_smooth_image16(vs, w, h);
|
|
263 |
}
|
|
264 |
if (quality != -1) {
|
|
265 |
return (errors < tight_conf[quality].jpeg_threshold);
|
|
266 |
}
|
|
267 |
return (errors < tight_conf[compression].gradient_threshold);
|
|
268 |
}
|
|
269 |
|
|
270 |
/*
|
|
271 |
* Code to determine how many different colors used in rectangle.
|
|
272 |
*/
|
|
273 |
|
|
274 |
static void tight_palette_rgb2buf(uint32_t rgb, int bpp, uint8_t buf[6])
|
|
275 |
{
|
|
276 |
memset(buf, 0, 6);
|
|
277 |
|
|
278 |
if (bpp == 32) {
|
|
279 |
buf[0] = ((rgb >> 24) & 0xFF);
|
|
280 |
buf[1] = ((rgb >> 16) & 0xFF);
|
|
281 |
buf[2] = ((rgb >> 8) & 0xFF);
|
|
282 |
buf[3] = ((rgb >> 0) & 0xFF);
|
|
283 |
buf[4] = ((buf[0] & 1) == 0) << 3 | ((buf[1] & 1) == 0) << 2;
|
|
284 |
buf[4]|= ((buf[2] & 1) == 0) << 1 | ((buf[3] & 1) == 0) << 0;
|
|
285 |
buf[0] |= 1;
|
|
286 |
buf[1] |= 1;
|
|
287 |
buf[2] |= 1;
|
|
288 |
buf[3] |= 1;
|
|
289 |
}
|
|
290 |
if (bpp == 16) {
|
|
291 |
buf[0] = ((rgb >> 8) & 0xFF);
|
|
292 |
buf[1] = ((rgb >> 0) & 0xFF);
|
|
293 |
buf[2] = ((buf[0] & 1) == 0) << 1 | ((buf[1] & 1) == 0) << 0;
|
|
294 |
buf[0] |= 1;
|
|
295 |
buf[1] |= 1;
|
|
296 |
}
|
|
297 |
}
|
|
298 |
|
|
299 |
static uint32_t tight_palette_buf2rgb(int bpp, const uint8_t *buf)
|
|
300 |
{
|
|
301 |
uint32_t rgb = 0;
|
|
302 |
|
|
303 |
if (bpp == 32) {
|
|
304 |
rgb |= ((buf[0] & ~1) | !((buf[4] >> 3) & 1)) << 24;
|
|
305 |
rgb |= ((buf[1] & ~1) | !((buf[4] >> 2) & 1)) << 16;
|
|
306 |
rgb |= ((buf[2] & ~1) | !((buf[4] >> 1) & 1)) << 8;
|
|
307 |
rgb |= ((buf[3] & ~1) | !((buf[4] >> 0) & 1)) << 0;
|
|
308 |
}
|
|
309 |
if (bpp == 16) {
|
|
310 |
rgb |= ((buf[0] & ~1) | !((buf[2] >> 1) & 1)) << 8;
|
|
311 |
rgb |= ((buf[1] & ~1) | !((buf[2] >> 0) & 1)) << 0;
|
|
312 |
}
|
|
313 |
return rgb;
|
|
314 |
}
|
|
315 |
|
|
316 |
|
|
317 |
static int tight_palette_insert(QDict *palette, uint32_t rgb, int bpp, int max)
|
|
318 |
{
|
|
319 |
uint8_t key[6];
|
|
320 |
int idx = qdict_size(palette);
|
|
321 |
bool present;
|
|
322 |
|
|
323 |
tight_palette_rgb2buf(rgb, bpp, key);
|
|
324 |
present = qdict_haskey(palette, (char *)key);
|
|
325 |
if (idx >= max && !present) {
|
|
326 |
return 0;
|
|
327 |
}
|
|
328 |
if (!present) {
|
|
329 |
qdict_put(palette, (char *)key, qint_from_int(idx));
|
|
330 |
}
|
|
331 |
return qdict_size(palette);
|
|
332 |
}
|
|
333 |
|
|
334 |
#define DEFINE_FILL_PALETTE_FUNCTION(bpp) \
|
|
335 |
\
|
|
336 |
static int \
|
|
337 |
tight_fill_palette##bpp(VncState *vs, int x, int y, \
|
|
338 |
int max, size_t count, \
|
|
339 |
uint32_t *bg, uint32_t *fg, \
|
|
340 |
struct QDict **palette) { \
|
|
341 |
uint##bpp##_t *data; \
|
|
342 |
uint##bpp##_t c0, c1, ci; \
|
|
343 |
int i, n0, n1; \
|
|
344 |
\
|
|
345 |
data = (uint##bpp##_t *)vs->tight.buffer; \
|
|
346 |
\
|
|
347 |
c0 = data[0]; \
|
|
348 |
i = 1; \
|
|
349 |
while (i < count && data[i] == c0) \
|
|
350 |
i++; \
|
|
351 |
if (i >= count) { \
|
|
352 |
*bg = *fg = c0; \
|
|
353 |
return 1; \
|
|
354 |
} \
|
|
355 |
\
|
|
356 |
if (max < 2) { \
|
|
357 |
return 0; \
|
|
358 |
} \
|
|
359 |
\
|
|
360 |
n0 = i; \
|
|
361 |
c1 = data[i]; \
|
|
362 |
n1 = 0; \
|
|
363 |
for (i++; i < count; i++) { \
|
|
364 |
ci = data[i]; \
|
|
365 |
if (ci == c0) { \
|
|
366 |
n0++; \
|
|
367 |
} else if (ci == c1) { \
|
|
368 |
n1++; \
|
|
369 |
} else \
|
|
370 |
break; \
|
|
371 |
} \
|
|
372 |
if (i >= count) { \
|
|
373 |
if (n0 > n1) { \
|
|
374 |
*bg = (uint32_t)c0; \
|
|
375 |
*fg = (uint32_t)c1; \
|
|
376 |
} else { \
|
|
377 |
*bg = (uint32_t)c1; \
|
|
378 |
*fg = (uint32_t)c0; \
|
|
379 |
} \
|
|
380 |
return 2; \
|
|
381 |
} \
|
|
382 |
\
|
|
383 |
if (max == 2) { \
|
|
384 |
return 0; \
|
|
385 |
} \
|
|
386 |
\
|
|
387 |
*palette = qdict_new(); \
|
|
388 |
tight_palette_insert(*palette, c0, bpp, max); \
|
|
389 |
tight_palette_insert(*palette, c1, bpp, max); \
|
|
390 |
tight_palette_insert(*palette, ci, bpp, max); \
|
|
391 |
\
|
|
392 |
for (i++; i < count; i++) { \
|
|
393 |
if (data[i] == ci) { \
|
|
394 |
continue; \
|
|
395 |
} else { \
|
|
396 |
if (!tight_palette_insert(*palette, (uint32_t)ci, \
|
|
397 |
bpp, max)) { \
|
|
398 |
return 0; \
|
|
399 |
} \
|
|
400 |
ci = data[i]; \
|
|
401 |
} \
|
|
402 |
} \
|
|
403 |
\
|
|
404 |
return qdict_size(*palette); \
|
|
405 |
}
|
|
406 |
|
|
407 |
DEFINE_FILL_PALETTE_FUNCTION(8)
|
|
408 |
DEFINE_FILL_PALETTE_FUNCTION(16)
|
|
409 |
DEFINE_FILL_PALETTE_FUNCTION(32)
|
|
410 |
|
|
411 |
static int tight_fill_palette(VncState *vs, int x, int y,
|
|
412 |
size_t count, uint32_t *bg, uint32_t *fg,
|
|
413 |
struct QDict **palette)
|
|
414 |
{
|
|
415 |
int max;
|
|
416 |
|
|
417 |
max = count / tight_conf[vs->tight_compression].idx_max_colors_divisor;
|
|
418 |
if (max < 2 &&
|
|
419 |
count >= tight_conf[vs->tight_compression].mono_min_rect_size) {
|
|
420 |
max = 2;
|
|
421 |
}
|
|
422 |
if (max >= 256) {
|
|
423 |
max = 256;
|
|
424 |
}
|
|
425 |
|
|
426 |
switch(vs->clientds.pf.bytes_per_pixel) {
|
|
427 |
case 4:
|
|
428 |
return tight_fill_palette32(vs, x, y, max, count, bg, fg, palette);
|
|
429 |
case 2:
|
|
430 |
return tight_fill_palette16(vs, x, y, max, count, bg, fg, palette);
|
|
431 |
default:
|
|
432 |
max = 2;
|
|
433 |
return tight_fill_palette8(vs, x, y, max, count, bg, fg, palette);
|
|
434 |
}
|
|
435 |
return 0;
|
|
436 |
}
|
|
437 |
|
|
438 |
/* Callback to dump a palette with qdict_iter
|
|
439 |
static void print_palette(const char *key, QObject *obj, void *opaque)
|
|
440 |
{
|
|
441 |
uint8_t idx = qint_get_int(qobject_to_qint(obj));
|
|
442 |
uint32_t rgb = tight_palette_buf2rgb(32, (uint8_t *)key);
|
|
443 |
|
|
444 |
fprintf(stderr, "%.2x ", (unsigned char)*key);
|
|
445 |
while (*key++)
|
|
446 |
fprintf(stderr, "%.2x ", (unsigned char)*key);
|
|
447 |
|
|
448 |
fprintf(stderr, ": idx: %x rgb: %x\n", idx, rgb);
|
|
449 |
}
|
|
450 |
*/
|
|
451 |
|
|
452 |
/*
|
|
453 |
* Converting truecolor samples into palette indices.
|
|
454 |
*/
|
|
455 |
#define DEFINE_IDX_ENCODE_FUNCTION(bpp) \
|
|
456 |
\
|
|
457 |
static void \
|
|
458 |
tight_encode_indexed_rect##bpp(uint8_t *buf, int count, \
|
|
459 |
struct QDict *palette) { \
|
|
460 |
uint##bpp##_t *src; \
|
|
461 |
uint##bpp##_t rgb; \
|
|
462 |
uint8_t key[6]; \
|
|
463 |
int i, rep; \
|
|
464 |
uint8_t idx; \
|
|
465 |
\
|
|
466 |
src = (uint##bpp##_t *) buf; \
|
|
467 |
\
|
|
468 |
for (i = 0; i < count; i++) { \
|
|
469 |
rgb = *src++; \
|
|
470 |
rep = 0; \
|
|
471 |
while (i < count && *src == rgb) { \
|
|
472 |
rep++, src++, i++; \
|
|
473 |
} \
|
|
474 |
tight_palette_rgb2buf(rgb, bpp, key); \
|
|
475 |
if (!qdict_haskey(palette, (char *)key)) { \
|
|
476 |
/* \
|
|
477 |
* Should never happen, but don't break everything \
|
|
478 |
* if it does, use the first color instead \
|
|
479 |
*/ \
|
|
480 |
idx = 0; \
|
|
481 |
} else { \
|
|
482 |
idx = qdict_get_int(palette, (char *)key); \
|
|
483 |
} \
|
|
484 |
while (rep >= 0) { \
|
|
485 |
*buf++ = idx; \
|
|
486 |
rep--; \
|
|
487 |
} \
|
|
488 |
} \
|
|
489 |
}
|
|
490 |
|
|
491 |
DEFINE_IDX_ENCODE_FUNCTION(16)
|
|
492 |
DEFINE_IDX_ENCODE_FUNCTION(32)
|
|
493 |
|
|
494 |
#define DEFINE_MONO_ENCODE_FUNCTION(bpp) \
|
|
495 |
\
|
|
496 |
static void \
|
|
497 |
tight_encode_mono_rect##bpp(uint8_t *buf, int w, int h, \
|
|
498 |
uint##bpp##_t bg, uint##bpp##_t fg) { \
|
|
499 |
uint##bpp##_t *ptr; \
|
|
500 |
unsigned int value, mask; \
|
|
501 |
int aligned_width; \
|
|
502 |
int x, y, bg_bits; \
|
|
503 |
\
|
|
504 |
ptr = (uint##bpp##_t *) buf; \
|
|
505 |
aligned_width = w - w % 8; \
|
|
506 |
\
|
|
507 |
for (y = 0; y < h; y++) { \
|
|
508 |
for (x = 0; x < aligned_width; x += 8) { \
|
|
509 |
for (bg_bits = 0; bg_bits < 8; bg_bits++) { \
|
|
510 |
if (*ptr++ != bg) { \
|
|
511 |
break; \
|
|
512 |
} \
|
|
513 |
} \
|
|
514 |
if (bg_bits == 8) { \
|
|
515 |
*buf++ = 0; \
|
|
516 |
continue; \
|
|
517 |
} \
|
|
518 |
mask = 0x80 >> bg_bits; \
|
|
519 |
value = mask; \
|
|
520 |
for (bg_bits++; bg_bits < 8; bg_bits++) { \
|
|
521 |
mask >>= 1; \
|
|
522 |
if (*ptr++ != bg) { \
|
|
523 |
value |= mask; \
|
|
524 |
} \
|
|
525 |
} \
|
|
526 |
*buf++ = (uint8_t)value; \
|
|
527 |
} \
|
|
528 |
\
|
|
529 |
mask = 0x80; \
|
|
530 |
value = 0; \
|
|
531 |
if (x >= w) { \
|
|
532 |
continue; \
|
|
533 |
} \
|
|
534 |
\
|
|
535 |
for (; x < w; x++) { \
|
|
536 |
if (*ptr++ != bg) { \
|
|
537 |
value |= mask; \
|
|
538 |
} \
|
|
539 |
mask >>= 1; \
|
|
540 |
} \
|
|
541 |
*buf++ = (uint8_t)value; \
|
|
542 |
} \
|
|
543 |
}
|
|
544 |
|
|
545 |
DEFINE_MONO_ENCODE_FUNCTION(8)
|
|
546 |
DEFINE_MONO_ENCODE_FUNCTION(16)
|
|
547 |
DEFINE_MONO_ENCODE_FUNCTION(32)
|
|
548 |
|
|
549 |
/*
|
|
550 |
* ``Gradient'' filter for 24-bit color samples.
|
|
551 |
* Should be called only when redMax, greenMax and blueMax are 255.
|
|
552 |
* Color components assumed to be byte-aligned.
|
|
553 |
*/
|
|
554 |
|
|
555 |
static void
|
|
556 |
tight_filter_gradient24(VncState *vs, uint8_t *buf, int w, int h)
|
|
557 |
{
|
|
558 |
uint32_t *buf32;
|
|
559 |
uint32_t pix32;
|
|
560 |
int shift[3];
|
|
561 |
int *prev;
|
|
562 |
int here[3], upper[3], left[3], upperleft[3];
|
|
563 |
int prediction;
|
|
564 |
int x, y, c;
|
|
565 |
|
|
566 |
buf32 = (uint32_t *)buf;
|
|
567 |
memset(vs->tight_gradient.buffer, 0, w * 3 * sizeof(int));
|
|
568 |
|
|
569 |
if ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) ==
|
|
570 |
(vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG)) {
|
|
571 |
shift[0] = vs->clientds.pf.rshift;
|
|
572 |
shift[1] = vs->clientds.pf.gshift;
|
|
573 |
shift[2] = vs->clientds.pf.bshift;
|
|
574 |
} else {
|
|
575 |
shift[0] = 24 - vs->clientds.pf.rshift;
|
|
576 |
shift[1] = 24 - vs->clientds.pf.gshift;
|
|
577 |
shift[2] = 24 - vs->clientds.pf.bshift;
|
|
578 |
}
|
|
579 |
|
|
580 |
for (y = 0; y < h; y++) {
|
|
581 |
for (c = 0; c < 3; c++) {
|
|
582 |
upper[c] = 0;
|
|
583 |
here[c] = 0;
|
|
584 |
}
|
|
585 |
prev = (int *)vs->tight_gradient.buffer;
|
|
586 |
for (x = 0; x < w; x++) {
|
|
587 |
pix32 = *buf32++;
|
|
588 |
for (c = 0; c < 3; c++) {
|
|
589 |
upperleft[c] = upper[c];
|
|
590 |
left[c] = here[c];
|
|
591 |
upper[c] = *prev;
|
|
592 |
here[c] = (int)(pix32 >> shift[c] & 0xFF);
|
|
593 |
*prev++ = here[c];
|
|
594 |
|
|
595 |
prediction = left[c] + upper[c] - upperleft[c];
|
|
596 |
if (prediction < 0) {
|
|
597 |
prediction = 0;
|
|
598 |
} else if (prediction > 0xFF) {
|
|
599 |
prediction = 0xFF;
|
|
600 |
}
|
|
601 |
*buf++ = (char)(here[c] - prediction);
|
|
602 |
}
|
|
603 |
}
|
|
604 |
}
|
|
605 |
}
|
|
606 |
|
|
607 |
|
|
608 |
/*
|
|
609 |
* ``Gradient'' filter for other color depths.
|
|
610 |
*/
|
|
611 |
|
|
612 |
#define DEFINE_GRADIENT_FILTER_FUNCTION(bpp) \
|
|
613 |
\
|
|
614 |
static void \
|
|
615 |
tight_filter_gradient##bpp(VncState *vs, uint##bpp##_t *buf, \
|
|
616 |
int w, int h) { \
|
|
617 |
uint##bpp##_t pix, diff; \
|
|
618 |
bool endian; \
|
|
619 |
int *prev; \
|
|
620 |
int max[3], shift[3]; \
|
|
621 |
int here[3], upper[3], left[3], upperleft[3]; \
|
|
622 |
int prediction; \
|
|
623 |
int x, y, c; \
|
|
624 |
\
|
|
625 |
memset (vs->tight_gradient.buffer, 0, w * 3 * sizeof(int)); \
|
|
626 |
\
|
|
627 |
endian = ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) != \
|
|
628 |
(vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG)); \
|
|
629 |
\
|
|
630 |
max[0] = vs->clientds.pf.rmax; \
|
|
631 |
max[1] = vs->clientds.pf.gmax; \
|
|
632 |
max[2] = vs->clientds.pf.bmax; \
|
|
633 |
shift[0] = vs->clientds.pf.rshift; \
|
|
634 |
shift[1] = vs->clientds.pf.gshift; \
|
|
635 |
shift[2] = vs->clientds.pf.bshift; \
|
|
636 |
\
|
|
637 |
for (y = 0; y < h; y++) { \
|
|
638 |
for (c = 0; c < 3; c++) { \
|
|
639 |
upper[c] = 0; \
|
|
640 |
here[c] = 0; \
|
|
641 |
} \
|
|
642 |
prev = (int *)vs->tight_gradient.buffer; \
|
|
643 |
for (x = 0; x < w; x++) { \
|
|
644 |
pix = *buf; \
|
|
645 |
if (endian) { \
|
|
646 |
pix = bswap_##bpp(pix); \
|
|
647 |
} \
|
|
648 |
diff = 0; \
|
|
649 |
for (c = 0; c < 3; c++) { \
|
|
650 |
upperleft[c] = upper[c]; \
|
|
651 |
left[c] = here[c]; \
|
|
652 |
upper[c] = *prev; \
|
|
653 |
here[c] = (int)(pix >> shift[c] & max[c]); \
|
|
654 |
*prev++ = here[c]; \
|
|
655 |
\
|
|
656 |
prediction = left[c] + upper[c] - upperleft[c]; \
|
|
657 |
if (prediction < 0) { \
|
|
658 |
prediction = 0; \
|
|
659 |
} else if (prediction > max[c]) { \
|
|
660 |
prediction = max[c]; \
|
|
661 |
} \
|
|
662 |
diff |= ((here[c] - prediction) & max[c]) \
|
|
663 |
<< shift[c]; \
|
|
664 |
} \
|
|
665 |
if (endian) { \
|
|
666 |
diff = bswap_##bpp(diff); \
|
|
667 |
} \
|
|
668 |
*buf++ = diff; \
|
|
669 |
} \
|
|
670 |
} \
|
|
671 |
}
|
|
672 |
|
|
673 |
DEFINE_GRADIENT_FILTER_FUNCTION(16)
|
|
674 |
DEFINE_GRADIENT_FILTER_FUNCTION(32)
|
|
675 |
|
|
676 |
/*
|
|
677 |
* Check if a rectangle is all of the same color. If needSameColor is
|
|
678 |
* set to non-zero, then also check that its color equals to the
|
|
679 |
* *colorPtr value. The result is 1 if the test is successfull, and in
|
|
680 |
* that case new color will be stored in *colorPtr.
|
|
681 |
*/
|
|
682 |
|
|
683 |
#define DEFINE_CHECK_SOLID_FUNCTION(bpp) \
|
|
684 |
\
|
|
685 |
static bool \
|
|
686 |
check_solid_tile##bpp(VncState *vs, int x, int y, int w, int h, \
|
|
687 |
uint32_t* color, bool samecolor) \
|
|
688 |
{ \
|
|
689 |
VncDisplay *vd = vs->vd; \
|
|
690 |
uint##bpp##_t *fbptr; \
|
|
691 |
uint##bpp##_t c; \
|
|
692 |
int dx, dy; \
|
|
693 |
\
|
|
694 |
fbptr = (uint##bpp##_t *) \
|
|
695 |
(vd->server->data + y * ds_get_linesize(vs->ds) + \
|
|
696 |
x * ds_get_bytes_per_pixel(vs->ds)); \
|
|
697 |
\
|
|
698 |
c = *fbptr; \
|
|
699 |
if (samecolor && (uint32_t)c != *color) { \
|
|
700 |
return false; \
|
|
701 |
} \
|
|
702 |
\
|
|
703 |
for (dy = 0; dy < h; dy++) { \
|
|
704 |
for (dx = 0; dx < w; dx++) { \
|
|
705 |
if (c != fbptr[dx]) { \
|
|
706 |
return false; \
|
|
707 |
} \
|
|
708 |
} \
|
|
709 |
fbptr = (uint##bpp##_t *) \
|
|
710 |
((uint8_t *)fbptr + ds_get_linesize(vs->ds)); \
|
|
711 |
} \
|
|
712 |
\
|
|
713 |
*color = (uint32_t)c; \
|
|
714 |
return true; \
|
|
715 |
}
|
|
716 |
|
|
717 |
DEFINE_CHECK_SOLID_FUNCTION(32)
|
|
718 |
DEFINE_CHECK_SOLID_FUNCTION(16)
|
|
719 |
DEFINE_CHECK_SOLID_FUNCTION(8)
|
|
720 |
|
|
721 |
static bool check_solid_tile(VncState *vs, int x, int y, int w, int h,
|
|
722 |
uint32_t* color, bool samecolor)
|
|
723 |
{
|
|
724 |
VncDisplay *vd = vs->vd;
|
|
725 |
|
|
726 |
switch(vd->server->pf.bytes_per_pixel) {
|
|
727 |
case 4:
|
|
728 |
return check_solid_tile32(vs, x, y, w, h, color, samecolor);
|
|
729 |
case 2:
|
|
730 |
return check_solid_tile16(vs, x, y, w, h, color, samecolor);
|
|
731 |
default:
|
|
732 |
return check_solid_tile8(vs, x, y, w, h, color, samecolor);
|
|
733 |
}
|
|
734 |
}
|
|
735 |
|
|
736 |
static void find_best_solid_area(VncState *vs, int x, int y, int w, int h,
|
|
737 |
uint32_t color, int *w_ptr, int *h_ptr)
|
|
738 |
{
|
|
739 |
int dx, dy, dw, dh;
|
|
740 |
int w_prev;
|
|
741 |
int w_best = 0, h_best = 0;
|
|
742 |
|
|
743 |
w_prev = w;
|
|
744 |
|
|
745 |
for (dy = y; dy < y + h; dy += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) {
|
|
746 |
|
|
747 |
dh = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, y + h - dy);
|
|
748 |
dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, w_prev);
|
|
749 |
|
|
750 |
if (!check_solid_tile(vs, x, dy, dw, dh, &color, true)) {
|
|
751 |
break;
|
|
752 |
}
|
|
753 |
|
|
754 |
for (dx = x + dw; dx < x + w_prev;) {
|
|
755 |
dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, x + w_prev - dx);
|
|
756 |
|
|
757 |
if (!check_solid_tile(vs, dx, dy, dw, dh, &color, true)) {
|
|
758 |
break;
|
|
759 |
}
|
|
760 |
dx += dw;
|
|
761 |
}
|
|
762 |
|
|
763 |
w_prev = dx - x;
|
|
764 |
if (w_prev * (dy + dh - y) > w_best * h_best) {
|
|
765 |
w_best = w_prev;
|
|
766 |
h_best = dy + dh - y;
|
|
767 |
}
|
|
768 |
}
|
|
769 |
|
|
770 |
*w_ptr = w_best;
|
|
771 |
*h_ptr = h_best;
|
|
772 |
}
|
|
773 |
|
|
774 |
static void extend_solid_area(VncState *vs, int x, int y, int w, int h,
|
|
775 |
uint32_t color, int *x_ptr, int *y_ptr,
|
|
776 |
int *w_ptr, int *h_ptr)
|
|
777 |
{
|
|
778 |
int cx, cy;
|
|
779 |
|
|
780 |
/* Try to extend the area upwards. */
|
|
781 |
for ( cy = *y_ptr - 1;
|
|
782 |
cy >= y && check_solid_tile(vs, *x_ptr, cy, *w_ptr, 1, &color, true);
|
|
783 |
cy-- );
|
|
784 |
*h_ptr += *y_ptr - (cy + 1);
|
|
785 |
*y_ptr = cy + 1;
|
|
786 |
|
|
787 |
/* ... downwards. */
|
|
788 |
for ( cy = *y_ptr + *h_ptr;
|
|
789 |
cy < y + h &&
|
|
790 |
check_solid_tile(vs, *x_ptr, cy, *w_ptr, 1, &color, true);
|
|
791 |
cy++ );
|
|
792 |
*h_ptr += cy - (*y_ptr + *h_ptr);
|
|
793 |
|
|
794 |
/* ... to the left. */
|
|
795 |
for ( cx = *x_ptr - 1;
|
|
796 |
cx >= x && check_solid_tile(vs, cx, *y_ptr, 1, *h_ptr, &color, true);
|
|
797 |
cx-- );
|
|
798 |
*w_ptr += *x_ptr - (cx + 1);
|
|
799 |
*x_ptr = cx + 1;
|
|
800 |
|
|
801 |
/* ... to the right. */
|
|
802 |
for ( cx = *x_ptr + *w_ptr;
|
|
803 |
cx < x + w &&
|
|
804 |
check_solid_tile(vs, cx, *y_ptr, 1, *h_ptr, &color, true);
|
|
805 |
cx++ );
|
|
806 |
*w_ptr += cx - (*x_ptr + *w_ptr);
|
|
807 |
}
|
|
808 |
|
|
809 |
static int tight_init_stream(VncState *vs, int stream_id,
|
|
810 |
int level, int strategy)
|
|
811 |
{
|
|
812 |
z_streamp zstream = &vs->tight_stream[stream_id];
|
|
813 |
|
|
814 |
if (zstream->opaque == NULL) {
|
|
815 |
int err;
|
|
816 |
|
|
817 |
VNC_DEBUG("VNC: TIGHT: initializing zlib stream %d\n", stream_id);
|
|
818 |
VNC_DEBUG("VNC: TIGHT: opaque = %p | vs = %p\n", zstream->opaque, vs);
|
|
819 |
zstream->zalloc = vnc_zlib_zalloc;
|
|
820 |
zstream->zfree = vnc_zlib_zfree;
|
|
821 |
|
|
822 |
err = deflateInit2(zstream, level, Z_DEFLATED, MAX_WBITS,
|
|
823 |
MAX_MEM_LEVEL, strategy);
|
|
824 |
|
|
825 |
if (err != Z_OK) {
|
|
826 |
fprintf(stderr, "VNC: error initializing zlib\n");
|
|
827 |
return -1;
|
|
828 |
}
|
|
829 |
|
|
830 |
vs->tight_levels[stream_id] = level;
|
|
831 |
zstream->opaque = vs;
|
|
832 |
}
|
|
833 |
|
|
834 |
if (vs->tight_levels[stream_id] != level) {
|
|
835 |
if (deflateParams(zstream, level, strategy) != Z_OK) {
|
|
836 |
return -1;
|
|
837 |
}
|
|
838 |
vs->tight_levels[stream_id] = level;
|
|
839 |
}
|
|
840 |
return 0;
|
|
841 |
}
|
|
842 |
|
|
843 |
static void tight_send_compact_size(VncState *vs, size_t len)
|
|
844 |
{
|
|
845 |
int lpc = 0;
|
|
846 |
int bytes = 0;
|
|
847 |
char buf[3] = {0, 0, 0};
|
|
848 |
|
|
849 |
buf[bytes++] = len & 0x7F;
|
|
850 |
if (len > 0x7F) {
|
|
851 |
buf[bytes-1] |= 0x80;
|
|
852 |
buf[bytes++] = (len >> 7) & 0x7F;
|
|
853 |
if (len > 0x3FFF) {
|
|
854 |
buf[bytes-1] |= 0x80;
|
|
855 |
buf[bytes++] = (len >> 14) & 0xFF;
|
|
856 |
}
|
|
857 |
}
|
|
858 |
for (lpc = 0; lpc < bytes; lpc++) {
|
|
859 |
vnc_write_u8(vs, buf[lpc]);
|
|
860 |
}
|
|
861 |
}
|
|
862 |
|
|
863 |
static int tight_compress_data(VncState *vs, int stream_id, size_t bytes,
|
|
864 |
int level, int strategy)
|
|
865 |
{
|
|
866 |
z_streamp zstream = &vs->tight_stream[stream_id];
|
|
867 |
int previous_out;
|
|
868 |
|
|
869 |
if (bytes < VNC_TIGHT_MIN_TO_COMPRESS) {
|
|
870 |
vnc_write(vs, vs->tight.buffer, vs->tight.offset);
|
|
871 |
return bytes;
|
|
872 |
}
|
|
873 |
|
|
874 |
if (tight_init_stream(vs, stream_id, level, strategy)) {
|
|
875 |
return -1;
|
|
876 |
}
|
|
877 |
|
|
878 |
/* reserve memory in output buffer */
|
|
879 |
buffer_reserve(&vs->tight_zlib, bytes + 64);
|
|
880 |
|
|
881 |
/* set pointers */
|
|
882 |
zstream->next_in = vs->tight.buffer;
|
|
883 |
zstream->avail_in = vs->tight.offset;
|
|
884 |
zstream->next_out = vs->tight_zlib.buffer + vs->tight_zlib.offset;
|
|
885 |
zstream->avail_out = vs->tight_zlib.capacity - vs->tight_zlib.offset;
|
|
886 |
zstream->data_type = Z_BINARY;
|
|
887 |
previous_out = zstream->total_out;
|
|
888 |
|
|
889 |
/* start encoding */
|
|
890 |
if (deflate(zstream, Z_SYNC_FLUSH) != Z_OK) {
|
|
891 |
fprintf(stderr, "VNC: error during tight compression\n");
|
|
892 |
return -1;
|
|
893 |
}
|
|
894 |
|
|
895 |
vs->tight_zlib.offset = vs->tight_zlib.capacity - zstream->avail_out;
|
|
896 |
bytes = zstream->total_out - previous_out;
|
|
897 |
|
|
898 |
tight_send_compact_size(vs, bytes);
|
|
899 |
vnc_write(vs, vs->tight_zlib.buffer, bytes);
|
|
900 |
|
|
901 |
buffer_reset(&vs->tight_zlib);
|
|
902 |
|
|
903 |
return bytes;
|
|
904 |
}
|
|
905 |
|
|
906 |
/*
|
|
907 |
* Subencoding implementations.
|
|
908 |
*/
|
|
909 |
static void tight_pack24(VncState *vs, uint8_t *buf, size_t count, size_t *ret)
|
|
910 |
{
|
|
911 |
uint32_t *buf32;
|
|
912 |
uint32_t pix;
|
|
913 |
int rshift, gshift, bshift;
|
|
914 |
|
|
915 |
buf32 = (uint32_t *)buf;
|
|
916 |
|
|
917 |
if ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) ==
|
|
918 |
(vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG)) {
|
|
919 |
rshift = vs->clientds.pf.rshift;
|
|
920 |
gshift = vs->clientds.pf.gshift;
|
|
921 |
bshift = vs->clientds.pf.bshift;
|
|
922 |
} else {
|
|
923 |
rshift = 24 - vs->clientds.pf.rshift;
|
|
924 |
gshift = 24 - vs->clientds.pf.gshift;
|
|
925 |
bshift = 24 - vs->clientds.pf.bshift;
|
|
926 |
}
|
|
927 |
|
|
928 |
if (ret) {
|
|
929 |
*ret = count * 3;
|
|
930 |
}
|
|
931 |
|
|
932 |
while (count--) {
|
|
933 |
pix = *buf32++;
|
|
934 |
*buf++ = (char)(pix >> rshift);
|
|
935 |
*buf++ = (char)(pix >> gshift);
|
|
936 |
*buf++ = (char)(pix >> bshift);
|
|
937 |
}
|
|
938 |
}
|
|
939 |
|
|
940 |
static int send_full_color_rect(VncState *vs, int w, int h)
|
|
941 |
{
|
|
942 |
int stream = 0;
|
|
943 |
size_t bytes;
|
|
944 |
|
|
945 |
vnc_write_u8(vs, stream << 4); /* no flushing, no filter */
|
|
946 |
|
|
947 |
if (vs->tight_pixel24) {
|
|
948 |
tight_pack24(vs, vs->tight.buffer, w * h, &vs->tight.offset);
|
|
949 |
bytes = 3;
|
|
950 |
} else {
|
|
951 |
bytes = vs->clientds.pf.bytes_per_pixel;
|
|
952 |
}
|
|
953 |
|
|
954 |
bytes = tight_compress_data(vs, stream, w * h * bytes,
|
|
955 |
tight_conf[vs->tight_compression].raw_zlib_level,
|
|
956 |
Z_DEFAULT_STRATEGY);
|
|
957 |
|
|
958 |
return (bytes >= 0);
|
|
959 |
}
|
|
960 |
|
|
961 |
static int send_solid_rect(VncState *vs)
|
|
962 |
{
|
|
963 |
size_t bytes;
|
|
964 |
|
|
965 |
vnc_write_u8(vs, VNC_TIGHT_FILL << 4); /* no flushing, no filter */
|
|
966 |
|
|
967 |
if (vs->tight_pixel24) {
|
|
968 |
tight_pack24(vs, vs->tight.buffer, 1, &vs->tight.offset);
|
|
969 |
bytes = 3;
|
|
970 |
} else {
|
|
971 |
bytes = vs->clientds.pf.bytes_per_pixel;
|
|
972 |
}
|
|
973 |
|
|
974 |
vnc_write(vs, vs->tight.buffer, bytes);
|
|
975 |
return 1;
|
|
976 |
}
|
|
977 |
|
|
978 |
static int send_mono_rect(VncState *vs, int w, int h, uint32_t bg, uint32_t fg)
|
|
979 |
{
|
|
980 |
size_t bytes;
|
|
981 |
int stream = 1;
|
|
982 |
int level = tight_conf[vs->tight_compression].mono_zlib_level;
|
|
983 |
|
|
984 |
bytes = ((w + 7) / 8) * h;
|
|
985 |
|
|
986 |
vnc_write_u8(vs, (stream | VNC_TIGHT_EXPLICIT_FILTER) << 4);
|
|
987 |
vnc_write_u8(vs, VNC_TIGHT_FILTER_PALETTE);
|
|
988 |
vnc_write_u8(vs, 1);
|
|
989 |
|
|
990 |
switch(vs->clientds.pf.bytes_per_pixel) {
|
|
991 |
case 4:
|
|
992 |
{
|
|
993 |
uint32_t buf[2] = {bg, fg};
|
|
994 |
size_t ret = sizeof (buf);
|
|
995 |
|
|
996 |
if (vs->tight_pixel24) {
|
|
997 |
tight_pack24(vs, (unsigned char*)buf, 2, &ret);
|
|
998 |
}
|
|
999 |
vnc_write(vs, buf, ret);
|
|
1000 |
|
|
1001 |
tight_encode_mono_rect32(vs->tight.buffer, w, h, bg, fg);
|
|
1002 |
break;
|
|
1003 |
}
|
|
1004 |
case 2:
|
|
1005 |
vnc_write(vs, &bg, 2);
|
|
1006 |
vnc_write(vs, &fg, 2);
|
|
1007 |
tight_encode_mono_rect16(vs->tight.buffer, w, h, bg, fg);
|
|
1008 |
break;
|
|
1009 |
default:
|
|
1010 |
vnc_write_u8(vs, bg);
|
|
1011 |
vnc_write_u8(vs, fg);
|
|
1012 |
tight_encode_mono_rect8(vs->tight.buffer, w, h, bg, fg);
|
|
1013 |
break;
|
|
1014 |
}
|
|
1015 |
vs->tight.offset = bytes;
|
|
1016 |
|
|
1017 |
bytes = tight_compress_data(vs, stream, bytes, level, Z_DEFAULT_STRATEGY);
|
|
1018 |
return (bytes >= 0);
|
|
1019 |
}
|
|
1020 |
|
|
1021 |
struct palette_cb_priv {
|
|
1022 |
VncState *vs;
|
|
1023 |
uint8_t *header;
|
|
1024 |
};
|
|
1025 |
|
|
1026 |
static void write_palette(const char *key, QObject *obj, void *opaque)
|
|
1027 |
{
|
|
1028 |
struct palette_cb_priv *priv = opaque;
|
|
1029 |
VncState *vs = priv->vs;
|
|
1030 |
uint32_t bytes = vs->clientds.pf.bytes_per_pixel;
|
|
1031 |
uint8_t idx = qint_get_int(qobject_to_qint(obj));
|
|
1032 |
|
|
1033 |
if (bytes == 4) {
|
|
1034 |
uint32_t color = tight_palette_buf2rgb(32, (uint8_t *)key);
|
|
1035 |
|
|
1036 |
((uint32_t*)priv->header)[idx] = color;
|
|
1037 |
} else {
|
|
1038 |
uint16_t color = tight_palette_buf2rgb(16, (uint8_t *)key);
|
|
1039 |
|
|
1040 |
((uint16_t*)priv->header)[idx] = color;
|
|
1041 |
}
|
|
1042 |
}
|
|
1043 |
|
|
1044 |
static bool send_gradient_rect(VncState *vs, int w, int h)
|
|
1045 |
{
|
|
1046 |
int stream = 3;
|
|
1047 |
int level = tight_conf[vs->tight_compression].gradient_zlib_level;
|
|
1048 |
size_t bytes;
|
|
1049 |
|
|
1050 |
if (vs->clientds.pf.bytes_per_pixel == 1)
|
|
1051 |
return send_full_color_rect(vs, w, h);
|
|
1052 |
|
|
1053 |
vnc_write_u8(vs, (stream | VNC_TIGHT_EXPLICIT_FILTER) << 4);
|
|
1054 |
vnc_write_u8(vs, VNC_TIGHT_FILTER_GRADIENT);
|
|
1055 |
|
|
1056 |
buffer_reserve(&vs->tight_gradient, w * 3 * sizeof (int));
|
|
1057 |
|
|
1058 |
if (vs->tight_pixel24) {
|
|
1059 |
tight_filter_gradient24(vs, vs->tight.buffer, w, h);
|
|
1060 |
bytes = 3;
|
|
1061 |
} else if (vs->clientds.pf.bytes_per_pixel == 4) {
|
|
1062 |
tight_filter_gradient32(vs, (uint32_t *)vs->tight.buffer, w, h);
|
|
1063 |
bytes = 4;
|
|
1064 |
} else {
|
|
1065 |
tight_filter_gradient16(vs, (uint16_t *)vs->tight.buffer, w, h);
|
|
1066 |
bytes = 2;
|
|
1067 |
}
|
|
1068 |
|
|
1069 |
buffer_reset(&vs->tight_gradient);
|
|
1070 |
|
|
1071 |
bytes = w * h * bytes;
|
|
1072 |
vs->tight.offset = bytes;
|
|
1073 |
|
|
1074 |
bytes = tight_compress_data(vs, stream, bytes,
|
|
1075 |
level, Z_FILTERED);
|
|
1076 |
return (bytes >= 0);
|
|
1077 |
}
|
|
1078 |
|
|
1079 |
static int send_palette_rect(VncState *vs, int w, int h, struct QDict *palette)
|
|
1080 |
{
|
|
1081 |
int stream = 2;
|
|
1082 |
int level = tight_conf[vs->tight_compression].idx_zlib_level;
|
|
1083 |
int colors;
|
|
1084 |
size_t bytes;
|
|
1085 |
|
|
1086 |
colors = qdict_size(palette);
|
|
1087 |
|
|
1088 |
vnc_write_u8(vs, (stream | VNC_TIGHT_EXPLICIT_FILTER) << 4);
|
|
1089 |
vnc_write_u8(vs, VNC_TIGHT_FILTER_PALETTE);
|
|
1090 |
vnc_write_u8(vs, colors - 1);
|
|
1091 |
|
|
1092 |
switch(vs->clientds.pf.bytes_per_pixel) {
|
|
1093 |
case 4:
|
|
1094 |
{
|
|
1095 |
size_t old_offset, offset;
|
|
1096 |
uint32_t header[qdict_size(palette)];
|
|
1097 |
struct palette_cb_priv priv = { vs, (uint8_t *)header };
|
|
1098 |
|
|
1099 |
old_offset = vs->output.offset;
|
|
1100 |
qdict_iter(palette, write_palette, &priv);
|
|
1101 |
vnc_write(vs, header, sizeof(header));
|
|
1102 |
|
|
1103 |
if (vs->tight_pixel24) {
|
|
1104 |
tight_pack24(vs, vs->output.buffer + old_offset, colors, &offset);
|
|
1105 |
vs->output.offset = old_offset + offset;
|
|
1106 |
}
|
|
1107 |
|
|
1108 |
tight_encode_indexed_rect32(vs->tight.buffer, w * h, palette);
|
|
1109 |
break;
|
|
1110 |
}
|
|
1111 |
case 2:
|
|
1112 |
{
|
|
1113 |
uint16_t header[qdict_size(palette)];
|
|
1114 |
struct palette_cb_priv priv = { vs, (uint8_t *)header };
|
|
1115 |
|
|
1116 |
qdict_iter(palette, write_palette, &priv);
|
|
1117 |
vnc_write(vs, header, sizeof(header));
|
|
1118 |
tight_encode_indexed_rect16(vs->tight.buffer, w * h, palette);
|
|
1119 |
break;
|
|
1120 |
}
|
|
1121 |
default:
|
|
1122 |
return -1; /* No palette for 8bits colors */
|
|
1123 |
break;
|
|
1124 |
}
|
|
1125 |
bytes = w * h;
|
|
1126 |
vs->tight.offset = bytes;
|
|
1127 |
|
|
1128 |
bytes = tight_compress_data(vs, stream, bytes,
|
|
1129 |
level, Z_DEFAULT_STRATEGY);
|