vnc: tight: don't forget last pixel in tight_encode_indexed_rect
[qemu.git] / vnc-encoding-tight.c
blob7bde44db0a615e0c5893de2e88249fbbbc4e8927
1 /*
2 * QEMU VNC display driver: tight encoding
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.
8 * Copyright (C) 2010 Corentin Chary <corentin.chary@gmail.com>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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.
29 #include <stdbool.h>
31 #include "qdict.h"
32 #include "qint.h"
33 #include "vnc.h"
34 #include "vnc-encoding-tight.h"
36 /* Compression level stuff. The following array contains various
37 encoder parameters for each of 10 compression levels (0..9).
38 Last three parameters correspond to JPEG quality levels (0..9). */
40 static const struct {
41 int max_rect_size, max_rect_width;
42 int mono_min_rect_size, gradient_min_rect_size;
43 int idx_zlib_level, mono_zlib_level, raw_zlib_level, gradient_zlib_level;
44 int gradient_threshold, gradient_threshold24;
45 int idx_max_colors_divisor;
46 int jpeg_quality, jpeg_threshold, jpeg_threshold24;
47 } tight_conf[] = {
48 { 512, 32, 6, 65536, 0, 0, 0, 0, 0, 0, 4, 5, 10000, 23000 },
49 { 2048, 128, 6, 65536, 1, 1, 1, 0, 0, 0, 8, 10, 8000, 18000 },
50 { 6144, 256, 8, 65536, 3, 3, 2, 0, 0, 0, 24, 15, 6500, 15000 },
51 { 10240, 1024, 12, 65536, 5, 5, 3, 0, 0, 0, 32, 25, 5000, 12000 },
52 { 16384, 2048, 12, 65536, 6, 6, 4, 0, 0, 0, 32, 37, 4000, 10000 },
53 { 32768, 2048, 12, 4096, 7, 7, 5, 4, 150, 380, 32, 50, 3000, 8000 },
54 { 65536, 2048, 16, 4096, 7, 7, 6, 4, 170, 420, 48, 60, 2000, 5000 },
55 { 65536, 2048, 16, 4096, 8, 8, 7, 5, 180, 450, 64, 70, 1000, 2500 },
56 { 65536, 2048, 32, 8192, 9, 9, 8, 6, 190, 475, 64, 75, 500, 1200 },
57 { 65536, 2048, 32, 8192, 9, 9, 9, 6, 200, 500, 96, 80, 200, 500 }
61 * Code to determine how many different colors used in rectangle.
64 static void tight_palette_rgb2buf(uint32_t rgb, int bpp, uint8_t buf[6])
66 memset(buf, 0, 6);
68 if (bpp == 32) {
69 buf[0] = ((rgb >> 24) & 0xFF);
70 buf[1] = ((rgb >> 16) & 0xFF);
71 buf[2] = ((rgb >> 8) & 0xFF);
72 buf[3] = ((rgb >> 0) & 0xFF);
73 buf[4] = ((buf[0] & 1) == 0) << 3 | ((buf[1] & 1) == 0) << 2;
74 buf[4]|= ((buf[2] & 1) == 0) << 1 | ((buf[3] & 1) == 0) << 0;
75 buf[0] |= 1;
76 buf[1] |= 1;
77 buf[2] |= 1;
78 buf[3] |= 1;
80 if (bpp == 16) {
81 buf[0] = ((rgb >> 8) & 0xFF);
82 buf[1] = ((rgb >> 0) & 0xFF);
83 buf[2] = ((buf[0] & 1) == 0) << 1 | ((buf[1] & 1) == 0) << 0;
84 buf[0] |= 1;
85 buf[1] |= 1;
89 static uint32_t tight_palette_buf2rgb(int bpp, const uint8_t *buf)
91 uint32_t rgb = 0;
93 if (bpp == 32) {
94 rgb |= ((buf[0] & ~1) | !((buf[4] >> 3) & 1)) << 24;
95 rgb |= ((buf[1] & ~1) | !((buf[4] >> 2) & 1)) << 16;
96 rgb |= ((buf[2] & ~1) | !((buf[4] >> 1) & 1)) << 8;
97 rgb |= ((buf[3] & ~1) | !((buf[4] >> 0) & 1)) << 0;
99 if (bpp == 16) {
100 rgb |= ((buf[0] & ~1) | !((buf[2] >> 1) & 1)) << 8;
101 rgb |= ((buf[1] & ~1) | !((buf[2] >> 0) & 1)) << 0;
103 return rgb;
107 static int tight_palette_insert(QDict *palette, uint32_t rgb, int bpp, int max)
109 uint8_t key[6];
110 int idx = qdict_size(palette);
111 bool present;
113 tight_palette_rgb2buf(rgb, bpp, key);
114 present = qdict_haskey(palette, (char *)key);
115 if (idx >= max && !present) {
116 return 0;
118 if (!present) {
119 qdict_put(palette, (char *)key, qint_from_int(idx));
121 return qdict_size(palette);
124 #define DEFINE_FILL_PALETTE_FUNCTION(bpp) \
126 static int \
127 tight_fill_palette##bpp(VncState *vs, int x, int y, \
128 int max, size_t count, \
129 uint32_t *bg, uint32_t *fg, \
130 struct QDict **palette) { \
131 uint##bpp##_t *data; \
132 uint##bpp##_t c0, c1, ci; \
133 int i, n0, n1; \
135 data = (uint##bpp##_t *)vs->tight.buffer; \
137 c0 = data[0]; \
138 i = 1; \
139 while (i < count && data[i] == c0) \
140 i++; \
141 if (i >= count) { \
142 *bg = *fg = c0; \
143 return 1; \
146 if (max < 2) { \
147 return 0; \
150 n0 = i; \
151 c1 = data[i]; \
152 n1 = 0; \
153 for (i++; i < count; i++) { \
154 ci = data[i]; \
155 if (ci == c0) { \
156 n0++; \
157 } else if (ci == c1) { \
158 n1++; \
159 } else \
160 break; \
162 if (i >= count) { \
163 if (n0 > n1) { \
164 *bg = (uint32_t)c0; \
165 *fg = (uint32_t)c1; \
166 } else { \
167 *bg = (uint32_t)c1; \
168 *fg = (uint32_t)c0; \
170 return 2; \
173 if (max == 2) { \
174 return 0; \
177 *palette = qdict_new(); \
178 tight_palette_insert(*palette, c0, bpp, max); \
179 tight_palette_insert(*palette, c1, bpp, max); \
181 for (i++; i < count; i++) { \
182 if (data[i] == ci) { \
183 continue; \
184 } else { \
185 if (!tight_palette_insert(*palette, (uint32_t)ci, \
186 bpp, max)) { \
187 return 0; \
189 ci = data[i]; \
193 return qdict_size(*palette); \
196 DEFINE_FILL_PALETTE_FUNCTION(8)
197 DEFINE_FILL_PALETTE_FUNCTION(16)
198 DEFINE_FILL_PALETTE_FUNCTION(32)
200 static int tight_fill_palette(VncState *vs, int x, int y,
201 size_t count, uint32_t *bg, uint32_t *fg,
202 struct QDict **palette)
204 int max;
206 max = count / tight_conf[vs->tight_compression].idx_max_colors_divisor;
207 if (max < 2 &&
208 count >= tight_conf[vs->tight_compression].mono_min_rect_size) {
209 max = 2;
211 if (max >= 256) {
212 max = 256;
215 switch(vs->clientds.pf.bytes_per_pixel) {
216 case 4:
217 return tight_fill_palette32(vs, x, y, max, count, bg, fg, palette);
218 case 2:
219 return tight_fill_palette16(vs, x, y, max, count, bg, fg, palette);
220 default:
221 max = 2;
222 return tight_fill_palette8(vs, x, y, max, count, bg, fg, palette);
224 return 0;
227 /* Callback to dump a palette with qdict_iter
228 static void print_palette(const char *key, QObject *obj, void *opaque)
230 uint8_t idx = qint_get_int(qobject_to_qint(obj));
231 uint32_t rgb = tight_palette_buf2rgb(32, (uint8_t *)key);
233 fprintf(stderr, "%.2x ", (unsigned char)*key);
234 while (*key++)
235 fprintf(stderr, "%.2x ", (unsigned char)*key);
237 fprintf(stderr, ": idx: %x rgb: %x\n", idx, rgb);
242 * Converting truecolor samples into palette indices.
244 #define DEFINE_IDX_ENCODE_FUNCTION(bpp) \
246 static void \
247 tight_encode_indexed_rect##bpp(uint8_t *buf, int count, \
248 struct QDict *palette) { \
249 uint##bpp##_t *src; \
250 uint##bpp##_t rgb; \
251 uint8_t key[6]; \
252 int i, rep; \
253 uint8_t idx; \
255 src = (uint##bpp##_t *) buf; \
257 for (i = 0; i < count; i++) { \
258 rgb = *src++; \
259 rep = 0; \
260 while (i < count && *src == rgb) { \
261 rep++, src++, i++; \
263 tight_palette_rgb2buf(rgb, bpp, key); \
264 if (!qdict_haskey(palette, (char *)key)) { \
265 /* \
266 * Should never happen, but don't break everything \
267 * if it does, use the first color instead \
268 */ \
269 idx = 0; \
270 } else { \
271 idx = qdict_get_int(palette, (char *)key); \
273 while (rep >= 0) { \
274 *buf++ = idx; \
275 rep--; \
280 DEFINE_IDX_ENCODE_FUNCTION(16)
281 DEFINE_IDX_ENCODE_FUNCTION(32)
283 #define DEFINE_MONO_ENCODE_FUNCTION(bpp) \
285 static void \
286 tight_encode_mono_rect##bpp(uint8_t *buf, int w, int h, \
287 uint##bpp##_t bg, uint##bpp##_t fg) { \
288 uint##bpp##_t *ptr; \
289 unsigned int value, mask; \
290 int aligned_width; \
291 int x, y, bg_bits; \
293 ptr = (uint##bpp##_t *) buf; \
294 aligned_width = w - w % 8; \
296 for (y = 0; y < h; y++) { \
297 for (x = 0; x < aligned_width; x += 8) { \
298 for (bg_bits = 0; bg_bits < 8; bg_bits++) { \
299 if (*ptr++ != bg) { \
300 break; \
303 if (bg_bits == 8) { \
304 *buf++ = 0; \
305 continue; \
307 mask = 0x80 >> bg_bits; \
308 value = mask; \
309 for (bg_bits++; bg_bits < 8; bg_bits++) { \
310 mask >>= 1; \
311 if (*ptr++ != bg) { \
312 value |= mask; \
315 *buf++ = (uint8_t)value; \
318 mask = 0x80; \
319 value = 0; \
320 if (x >= w) { \
321 continue; \
324 for (; x < w; x++) { \
325 if (*ptr++ != bg) { \
326 value |= mask; \
328 mask >>= 1; \
330 *buf++ = (uint8_t)value; \
334 DEFINE_MONO_ENCODE_FUNCTION(8)
335 DEFINE_MONO_ENCODE_FUNCTION(16)
336 DEFINE_MONO_ENCODE_FUNCTION(32)
339 * Check if a rectangle is all of the same color. If needSameColor is
340 * set to non-zero, then also check that its color equals to the
341 * *colorPtr value. The result is 1 if the test is successfull, and in
342 * that case new color will be stored in *colorPtr.
345 #define DEFINE_CHECK_SOLID_FUNCTION(bpp) \
347 static bool \
348 check_solid_tile##bpp(VncState *vs, int x, int y, int w, int h, \
349 uint32_t* color, bool samecolor) \
351 VncDisplay *vd = vs->vd; \
352 uint##bpp##_t *fbptr; \
353 uint##bpp##_t c; \
354 int dx, dy; \
356 fbptr = (uint##bpp##_t *) \
357 (vd->server->data + y * ds_get_linesize(vs->ds) + \
358 x * ds_get_bytes_per_pixel(vs->ds)); \
360 c = *fbptr; \
361 if (samecolor && (uint32_t)c != *color) { \
362 return false; \
365 for (dy = 0; dy < h; dy++) { \
366 for (dx = 0; dx < w; dx++) { \
367 if (c != fbptr[dx]) { \
368 return false; \
371 fbptr = (uint##bpp##_t *) \
372 ((uint8_t *)fbptr + ds_get_linesize(vs->ds)); \
375 *color = (uint32_t)c; \
376 return true; \
379 DEFINE_CHECK_SOLID_FUNCTION(32)
380 DEFINE_CHECK_SOLID_FUNCTION(16)
381 DEFINE_CHECK_SOLID_FUNCTION(8)
383 static bool check_solid_tile(VncState *vs, int x, int y, int w, int h,
384 uint32_t* color, bool samecolor)
386 VncDisplay *vd = vs->vd;
388 switch(vd->server->pf.bytes_per_pixel) {
389 case 4:
390 return check_solid_tile32(vs, x, y, w, h, color, samecolor);
391 case 2:
392 return check_solid_tile16(vs, x, y, w, h, color, samecolor);
393 default:
394 return check_solid_tile8(vs, x, y, w, h, color, samecolor);
398 static void find_best_solid_area(VncState *vs, int x, int y, int w, int h,
399 uint32_t color, int *w_ptr, int *h_ptr)
401 int dx, dy, dw, dh;
402 int w_prev;
403 int w_best = 0, h_best = 0;
405 w_prev = w;
407 for (dy = y; dy < y + h; dy += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) {
409 dh = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, y + h - dy);
410 dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, w_prev);
412 if (!check_solid_tile(vs, x, dy, dw, dh, &color, true)) {
413 break;
416 for (dx = x + dw; dx < x + w_prev;) {
417 dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, x + w_prev - dx);
419 if (!check_solid_tile(vs, dx, dy, dw, dh, &color, true)) {
420 break;
422 dx += dw;
425 w_prev = dx - x;
426 if (w_prev * (dy + dh - y) > w_best * h_best) {
427 w_best = w_prev;
428 h_best = dy + dh - y;
432 *w_ptr = w_best;
433 *h_ptr = h_best;
436 static void extend_solid_area(VncState *vs, int x, int y, int w, int h,
437 uint32_t color, int *x_ptr, int *y_ptr,
438 int *w_ptr, int *h_ptr)
440 int cx, cy;
442 /* Try to extend the area upwards. */
443 for ( cy = *y_ptr - 1;
444 cy >= y && check_solid_tile(vs, *x_ptr, cy, *w_ptr, 1, &color, true);
445 cy-- );
446 *h_ptr += *y_ptr - (cy + 1);
447 *y_ptr = cy + 1;
449 /* ... downwards. */
450 for ( cy = *y_ptr + *h_ptr;
451 cy < y + h &&
452 check_solid_tile(vs, *x_ptr, cy, *w_ptr, 1, &color, true);
453 cy++ );
454 *h_ptr += cy - (*y_ptr + *h_ptr);
456 /* ... to the left. */
457 for ( cx = *x_ptr - 1;
458 cx >= x && check_solid_tile(vs, cx, *y_ptr, 1, *h_ptr, &color, true);
459 cx-- );
460 *w_ptr += *x_ptr - (cx + 1);
461 *x_ptr = cx + 1;
463 /* ... to the right. */
464 for ( cx = *x_ptr + *w_ptr;
465 cx < x + w &&
466 check_solid_tile(vs, cx, *y_ptr, 1, *h_ptr, &color, true);
467 cx++ );
468 *w_ptr += cx - (*x_ptr + *w_ptr);
471 static int tight_init_stream(VncState *vs, int stream_id,
472 int level, int strategy)
474 z_streamp zstream = &vs->tight_stream[stream_id];
476 if (zstream->opaque == NULL) {
477 int err;
479 VNC_DEBUG("VNC: TIGHT: initializing zlib stream %d\n", stream_id);
480 VNC_DEBUG("VNC: TIGHT: opaque = %p | vs = %p\n", zstream->opaque, vs);
481 zstream->zalloc = vnc_zlib_zalloc;
482 zstream->zfree = vnc_zlib_zfree;
484 err = deflateInit2(zstream, level, Z_DEFLATED, MAX_WBITS,
485 MAX_MEM_LEVEL, strategy);
487 if (err != Z_OK) {
488 fprintf(stderr, "VNC: error initializing zlib\n");
489 return -1;
492 vs->tight_levels[stream_id] = level;
493 zstream->opaque = vs;
496 if (vs->tight_levels[stream_id] != level) {
497 if (deflateParams(zstream, level, strategy) != Z_OK) {
498 return -1;
500 vs->tight_levels[stream_id] = level;
502 return 0;
505 static void tight_send_compact_size(VncState *vs, size_t len)
507 int lpc = 0;
508 int bytes = 0;
509 char buf[3] = {0, 0, 0};
511 buf[bytes++] = len & 0x7F;
512 if (len > 0x7F) {
513 buf[bytes-1] |= 0x80;
514 buf[bytes++] = (len >> 7) & 0x7F;
515 if (len > 0x3FFF) {
516 buf[bytes-1] |= 0x80;
517 buf[bytes++] = (len >> 14) & 0xFF;
520 for (lpc = 0; lpc < bytes; lpc++) {
521 vnc_write_u8(vs, buf[lpc]);
525 static int tight_compress_data(VncState *vs, int stream_id, size_t bytes,
526 int level, int strategy)
528 z_streamp zstream = &vs->tight_stream[stream_id];
529 int previous_out;
531 if (bytes < VNC_TIGHT_MIN_TO_COMPRESS) {
532 vnc_write(vs, vs->tight.buffer, vs->tight.offset);
533 return bytes;
536 if (tight_init_stream(vs, stream_id, level, strategy)) {
537 return -1;
540 /* reserve memory in output buffer */
541 buffer_reserve(&vs->tight_zlib, bytes + 64);
543 /* set pointers */
544 zstream->next_in = vs->tight.buffer;
545 zstream->avail_in = vs->tight.offset;
546 zstream->next_out = vs->tight_zlib.buffer + vs->tight_zlib.offset;
547 zstream->avail_out = vs->tight_zlib.capacity - vs->tight_zlib.offset;
548 zstream->data_type = Z_BINARY;
549 previous_out = zstream->total_out;
551 /* start encoding */
552 if (deflate(zstream, Z_SYNC_FLUSH) != Z_OK) {
553 fprintf(stderr, "VNC: error during tight compression\n");
554 return -1;
557 vs->tight_zlib.offset = vs->tight_zlib.capacity - zstream->avail_out;
558 bytes = zstream->total_out - previous_out;
560 tight_send_compact_size(vs, bytes);
561 vnc_write(vs, vs->tight_zlib.buffer, bytes);
563 buffer_reset(&vs->tight_zlib);
565 return bytes;
569 * Subencoding implementations.
571 static void tight_pack24(VncState *vs, uint8_t *buf, size_t count, size_t *ret)
573 uint32_t *buf32;
574 uint32_t pix;
575 int rshift, gshift, bshift;
577 buf32 = (uint32_t *)buf;
579 if ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) ==
580 (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG)) {
581 rshift = vs->clientds.pf.rshift;
582 gshift = vs->clientds.pf.gshift;
583 bshift = vs->clientds.pf.bshift;
584 } else {
585 rshift = 24 - vs->clientds.pf.rshift;
586 gshift = 24 - vs->clientds.pf.gshift;
587 bshift = 24 - vs->clientds.pf.bshift;
590 if (ret) {
591 *ret = count * 3;
594 while (count--) {
595 pix = *buf32++;
596 *buf++ = (char)(pix >> rshift);
597 *buf++ = (char)(pix >> gshift);
598 *buf++ = (char)(pix >> bshift);
602 static int send_full_color_rect(VncState *vs, int w, int h)
604 int stream = 0;
605 size_t bytes;
607 vnc_write_u8(vs, stream << 4); /* no flushing, no filter */
609 if (vs->tight_pixel24) {
610 tight_pack24(vs, vs->tight.buffer, w * h, &vs->tight.offset);
611 bytes = 3;
612 } else {
613 bytes = vs->clientds.pf.bytes_per_pixel;
616 bytes = tight_compress_data(vs, stream, w * h * bytes,
617 tight_conf[vs->tight_compression].raw_zlib_level,
618 Z_DEFAULT_STRATEGY);
620 return (bytes >= 0);
623 static int send_solid_rect(VncState *vs)
625 size_t bytes;
627 vnc_write_u8(vs, VNC_TIGHT_FILL << 4); /* no flushing, no filter */
629 if (vs->tight_pixel24) {
630 tight_pack24(vs, vs->tight.buffer, 1, &vs->tight.offset);
631 bytes = 3;
632 } else {
633 bytes = vs->clientds.pf.bytes_per_pixel;
636 vnc_write(vs, vs->tight.buffer, bytes);
637 return 1;
640 static int send_mono_rect(VncState *vs, int w, int h, uint32_t bg, uint32_t fg)
642 size_t bytes;
643 int stream = 1;
644 int level = tight_conf[vs->tight_compression].mono_zlib_level;
646 bytes = ((w + 7) / 8) * h;
648 vnc_write_u8(vs, (stream | VNC_TIGHT_EXPLICIT_FILTER) << 4);
649 vnc_write_u8(vs, VNC_TIGHT_FILTER_PALETTE);
650 vnc_write_u8(vs, 1);
652 switch(vs->clientds.pf.bytes_per_pixel) {
653 case 4:
655 uint32_t buf[2] = {bg, fg};
656 size_t ret = sizeof (buf);
658 if (vs->tight_pixel24) {
659 tight_pack24(vs, (unsigned char*)buf, 2, &ret);
661 vnc_write(vs, buf, ret);
663 tight_encode_mono_rect32(vs->tight.buffer, w, h, bg, fg);
664 break;
666 case 2:
667 vnc_write(vs, &bg, 2);
668 vnc_write(vs, &fg, 2);
669 tight_encode_mono_rect16(vs->tight.buffer, w, h, bg, fg);
670 break;
671 default:
672 vnc_write_u8(vs, bg);
673 vnc_write_u8(vs, fg);
674 tight_encode_mono_rect8(vs->tight.buffer, w, h, bg, fg);
675 break;
677 vs->tight.offset = bytes;
679 bytes = tight_compress_data(vs, stream, bytes, level, Z_DEFAULT_STRATEGY);
680 return (bytes >= 0);
683 struct palette_cb_priv {
684 VncState *vs;
685 uint8_t *header;
688 static void write_palette(const char *key, QObject *obj, void *opaque)
690 struct palette_cb_priv *priv = opaque;
691 VncState *vs = priv->vs;
692 uint32_t bytes = vs->clientds.pf.bytes_per_pixel;
693 uint8_t idx = qint_get_int(qobject_to_qint(obj));
695 if (bytes == 4) {
696 uint32_t color = tight_palette_buf2rgb(32, (uint8_t *)key);
698 ((uint32_t*)priv->header)[idx] = color;
699 } else {
700 uint16_t color = tight_palette_buf2rgb(16, (uint8_t *)key);
702 ((uint16_t*)priv->header)[idx] = color;
706 static int send_palette_rect(VncState *vs, int w, int h, struct QDict *palette)
708 int stream = 2;
709 int level = tight_conf[vs->tight_compression].idx_zlib_level;
710 int colors;
711 size_t bytes;
713 colors = qdict_size(palette);
715 vnc_write_u8(vs, (stream | VNC_TIGHT_EXPLICIT_FILTER) << 4);
716 vnc_write_u8(vs, VNC_TIGHT_FILTER_PALETTE);
717 vnc_write_u8(vs, colors - 1);
719 switch(vs->clientds.pf.bytes_per_pixel) {
720 case 4:
722 size_t old_offset, offset;
723 uint32_t header[qdict_size(palette)];
724 struct palette_cb_priv priv = { vs, (uint8_t *)header };
726 old_offset = vs->output.offset;
727 qdict_iter(palette, write_palette, &priv);
728 vnc_write(vs, header, sizeof(header));
730 if (vs->tight_pixel24) {
731 tight_pack24(vs, vs->output.buffer + old_offset, colors, &offset);
732 vs->output.offset = old_offset + offset;
735 tight_encode_indexed_rect32(vs->tight.buffer, w * h, palette);
736 break;
738 case 2:
740 uint16_t header[qdict_size(palette)];
741 struct palette_cb_priv priv = { vs, (uint8_t *)header };
743 qdict_iter(palette, write_palette, &priv);
744 vnc_write(vs, header, sizeof(header));
745 tight_encode_indexed_rect16(vs->tight.buffer, w * h, palette);
746 break;
748 default:
749 return -1; /* No palette for 8bits colors */
750 break;
752 bytes = w * h;
753 vs->tight.offset = bytes;
755 bytes = tight_compress_data(vs, stream, bytes,
756 level, Z_DEFAULT_STRATEGY);
757 return (bytes >= 0);
760 static void vnc_tight_start(VncState *vs)
762 buffer_reset(&vs->tight);
764 // make the output buffer be the zlib buffer, so we can compress it later
765 vs->tight_tmp = vs->output;
766 vs->output = vs->tight;
769 static void vnc_tight_stop(VncState *vs)
771 // switch back to normal output/zlib buffers
772 vs->tight = vs->output;
773 vs->output = vs->tight_tmp;
776 static int send_sub_rect(VncState *vs, int x, int y, int w, int h)
778 struct QDict *palette = NULL;
779 uint32_t bg = 0, fg = 0;
780 int colors;
781 int ret = 0;
783 vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_TIGHT);
785 vnc_tight_start(vs);
786 vnc_raw_send_framebuffer_update(vs, x, y, w, h);
787 vnc_tight_stop(vs);
789 colors = tight_fill_palette(vs, x, y, w * h, &fg, &bg, &palette);
791 if (colors == 0) {
792 ret = send_full_color_rect(vs, w, h);
793 } else if (colors == 1) {
794 ret = send_solid_rect(vs);
795 } else if (colors == 2) {
796 ret = send_mono_rect(vs, w, h, bg, fg);
797 } else if (colors <= 256) {
798 ret = send_palette_rect(vs, w, h, palette);
800 QDECREF(palette);
801 return ret;
804 static int send_sub_rect_solid(VncState *vs, int x, int y, int w, int h)
806 vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_TIGHT);
808 vnc_tight_start(vs);
809 vnc_raw_send_framebuffer_update(vs, x, y, w, h);
810 vnc_tight_stop(vs);
812 return send_solid_rect(vs);
815 static int send_rect_simple(VncState *vs, int x, int y, int w, int h)
817 int max_size, max_width;
818 int max_sub_width, max_sub_height;
819 int dx, dy;
820 int rw, rh;
821 int n = 0;
823 max_size = tight_conf[vs->tight_compression].max_rect_size;
824 max_width = tight_conf[vs->tight_compression].max_rect_width;
826 if (w > max_width || w * h > max_size) {
827 max_sub_width = (w > max_width) ? max_width : w;
828 max_sub_height = max_size / max_sub_width;
830 for (dy = 0; dy < h; dy += max_sub_height) {
831 for (dx = 0; dx < w; dx += max_width) {
832 rw = MIN(max_sub_width, w - dx);
833 rh = MIN(max_sub_height, h - dy);
834 n += send_sub_rect(vs, x+dx, y+dy, rw, rh);
837 } else {
838 n += send_sub_rect(vs, x, y, w, h);
841 return n;
844 static int find_large_solid_color_rect(VncState *vs, int x, int y,
845 int w, int h, int max_rows)
847 int dx, dy, dw, dh;
848 int n = 0;
850 /* Try to find large solid-color areas and send them separately. */
852 for (dy = y; dy < y + h; dy += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) {
854 /* If a rectangle becomes too large, send its upper part now. */
856 if (dy - y >= max_rows) {
857 n += send_rect_simple(vs, x, y, w, max_rows);
858 y += max_rows;
859 h -= max_rows;
862 dh = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, (y + h - dy));
864 for (dx = x; dx < x + w; dx += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) {
865 uint32_t color_value;
866 int x_best, y_best, w_best, h_best;
868 dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, (x + w - dx));
870 if (!check_solid_tile(vs, dx, dy, dw, dh, &color_value, false)) {
871 continue ;
874 /* Get dimensions of solid-color area. */
876 find_best_solid_area(vs, dx, dy, w - (dx - x), h - (dy - y),
877 color_value, &w_best, &h_best);
879 /* Make sure a solid rectangle is large enough
880 (or the whole rectangle is of the same color). */
882 if (w_best * h_best != w * h &&
883 w_best * h_best < VNC_TIGHT_MIN_SOLID_SUBRECT_SIZE) {
884 continue;
887 /* Try to extend solid rectangle to maximum size. */
889 x_best = dx; y_best = dy;
890 extend_solid_area(vs, x, y, w, h, color_value,
891 &x_best, &y_best, &w_best, &h_best);
893 /* Send rectangles at top and left to solid-color area. */
895 if (y_best != y) {
896 n += send_rect_simple(vs, x, y, w, y_best-y);
898 if (x_best != x) {
899 n += vnc_tight_send_framebuffer_update(vs, x, y_best,
900 x_best-x, h_best);
903 /* Send solid-color rectangle. */
904 n += send_sub_rect_solid(vs, x_best, y_best, w_best, h_best);
906 /* Send remaining rectangles (at right and bottom). */
908 if (x_best + w_best != x + w) {
909 n += vnc_tight_send_framebuffer_update(vs, x_best+w_best,
910 y_best,
911 w-(x_best-x)-w_best,
912 h_best);
914 if (y_best + h_best != y + h) {
915 n += vnc_tight_send_framebuffer_update(vs, x, y_best+h_best,
916 w, h-(y_best-y)-h_best);
919 /* Return after all recursive calls are done. */
920 return n;
923 return n + send_rect_simple(vs, x, y, w, h);
926 int vnc_tight_send_framebuffer_update(VncState *vs, int x, int y,
927 int w, int h)
929 int max_rows;
931 if (vs->clientds.pf.bytes_per_pixel == 4 && vs->clientds.pf.rmax == 0xFF &&
932 vs->clientds.pf.bmax == 0xFF && vs->clientds.pf.gmax == 0xFF) {
933 vs->tight_pixel24 = true;
934 } else {
935 vs->tight_pixel24 = false;
938 if (w * h < VNC_TIGHT_MIN_SPLIT_RECT_SIZE)
939 return send_rect_simple(vs, x, y, w, h);
941 /* Calculate maximum number of rows in one non-solid rectangle. */
943 max_rows = tight_conf[vs->tight_compression].max_rect_size;
944 max_rows /= MIN(tight_conf[vs->tight_compression].max_rect_width, w);
946 return find_large_solid_color_rect(vs, x, y, w, h, max_rows);
949 void vnc_tight_clear(VncState *vs)
951 int i;
952 for (i=0; i<ARRAY_SIZE(vs->tight_stream); i++) {
953 if (vs->tight_stream[i].opaque) {
954 deflateEnd(&vs->tight_stream[i]);
958 buffer_free(&vs->tight);
959 buffer_free(&vs->tight_zlib);