sparc64: fix udiv and sdiv insns
[qemu/aliguori-queue.git] / vnc-encoding-tight.c
blobe8604a85a9285f84eeee82223f1e30723ebe24be
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); \
180 tight_palette_insert(*palette, ci, bpp, max); \
182 for (i++; i < count; i++) { \
183 if (data[i] == ci) { \
184 continue; \
185 } else { \
186 if (!tight_palette_insert(*palette, (uint32_t)ci, \
187 bpp, max)) { \
188 return 0; \
190 ci = data[i]; \
194 return qdict_size(*palette); \
197 DEFINE_FILL_PALETTE_FUNCTION(8)
198 DEFINE_FILL_PALETTE_FUNCTION(16)
199 DEFINE_FILL_PALETTE_FUNCTION(32)
201 static int tight_fill_palette(VncState *vs, int x, int y,
202 size_t count, uint32_t *bg, uint32_t *fg,
203 struct QDict **palette)
205 int max;
207 max = count / tight_conf[vs->tight_compression].idx_max_colors_divisor;
208 if (max < 2 &&
209 count >= tight_conf[vs->tight_compression].mono_min_rect_size) {
210 max = 2;
212 if (max >= 256) {
213 max = 256;
216 switch(vs->clientds.pf.bytes_per_pixel) {
217 case 4:
218 return tight_fill_palette32(vs, x, y, max, count, bg, fg, palette);
219 case 2:
220 return tight_fill_palette16(vs, x, y, max, count, bg, fg, palette);
221 default:
222 max = 2;
223 return tight_fill_palette8(vs, x, y, max, count, bg, fg, palette);
225 return 0;
228 /* Callback to dump a palette with qdict_iter
229 static void print_palette(const char *key, QObject *obj, void *opaque)
231 uint8_t idx = qint_get_int(qobject_to_qint(obj));
232 uint32_t rgb = tight_palette_buf2rgb(32, (uint8_t *)key);
234 fprintf(stderr, "%.2x ", (unsigned char)*key);
235 while (*key++)
236 fprintf(stderr, "%.2x ", (unsigned char)*key);
238 fprintf(stderr, ": idx: %x rgb: %x\n", idx, rgb);
243 * Converting truecolor samples into palette indices.
245 #define DEFINE_IDX_ENCODE_FUNCTION(bpp) \
247 static void \
248 tight_encode_indexed_rect##bpp(uint8_t *buf, int count, \
249 struct QDict *palette) { \
250 uint##bpp##_t *src; \
251 uint##bpp##_t rgb; \
252 uint8_t key[6]; \
253 int i, rep; \
254 uint8_t idx; \
256 src = (uint##bpp##_t *) buf; \
258 for (i = 0; i < count; i++) { \
259 rgb = *src++; \
260 rep = 0; \
261 while (i < count && *src == rgb) { \
262 rep++, src++, i++; \
264 tight_palette_rgb2buf(rgb, bpp, key); \
265 if (!qdict_haskey(palette, (char *)key)) { \
266 /* \
267 * Should never happen, but don't break everything \
268 * if it does, use the first color instead \
269 */ \
270 idx = 0; \
271 } else { \
272 idx = qdict_get_int(palette, (char *)key); \
274 while (rep >= 0) { \
275 *buf++ = idx; \
276 rep--; \
281 DEFINE_IDX_ENCODE_FUNCTION(16)
282 DEFINE_IDX_ENCODE_FUNCTION(32)
284 #define DEFINE_MONO_ENCODE_FUNCTION(bpp) \
286 static void \
287 tight_encode_mono_rect##bpp(uint8_t *buf, int w, int h, \
288 uint##bpp##_t bg, uint##bpp##_t fg) { \
289 uint##bpp##_t *ptr; \
290 unsigned int value, mask; \
291 int aligned_width; \
292 int x, y, bg_bits; \
294 ptr = (uint##bpp##_t *) buf; \
295 aligned_width = w - w % 8; \
297 for (y = 0; y < h; y++) { \
298 for (x = 0; x < aligned_width; x += 8) { \
299 for (bg_bits = 0; bg_bits < 8; bg_bits++) { \
300 if (*ptr++ != bg) { \
301 break; \
304 if (bg_bits == 8) { \
305 *buf++ = 0; \
306 continue; \
308 mask = 0x80 >> bg_bits; \
309 value = mask; \
310 for (bg_bits++; bg_bits < 8; bg_bits++) { \
311 mask >>= 1; \
312 if (*ptr++ != bg) { \
313 value |= mask; \
316 *buf++ = (uint8_t)value; \
319 mask = 0x80; \
320 value = 0; \
321 if (x >= w) { \
322 continue; \
325 for (; x < w; x++) { \
326 if (*ptr++ != bg) { \
327 value |= mask; \
329 mask >>= 1; \
331 *buf++ = (uint8_t)value; \
335 DEFINE_MONO_ENCODE_FUNCTION(8)
336 DEFINE_MONO_ENCODE_FUNCTION(16)
337 DEFINE_MONO_ENCODE_FUNCTION(32)
340 * Check if a rectangle is all of the same color. If needSameColor is
341 * set to non-zero, then also check that its color equals to the
342 * *colorPtr value. The result is 1 if the test is successfull, and in
343 * that case new color will be stored in *colorPtr.
346 #define DEFINE_CHECK_SOLID_FUNCTION(bpp) \
348 static bool \
349 check_solid_tile##bpp(VncState *vs, int x, int y, int w, int h, \
350 uint32_t* color, bool samecolor) \
352 VncDisplay *vd = vs->vd; \
353 uint##bpp##_t *fbptr; \
354 uint##bpp##_t c; \
355 int dx, dy; \
357 fbptr = (uint##bpp##_t *) \
358 (vd->server->data + y * ds_get_linesize(vs->ds) + \
359 x * ds_get_bytes_per_pixel(vs->ds)); \
361 c = *fbptr; \
362 if (samecolor && (uint32_t)c != *color) { \
363 return false; \
366 for (dy = 0; dy < h; dy++) { \
367 for (dx = 0; dx < w; dx++) { \
368 if (c != fbptr[dx]) { \
369 return false; \
372 fbptr = (uint##bpp##_t *) \
373 ((uint8_t *)fbptr + ds_get_linesize(vs->ds)); \
376 *color = (uint32_t)c; \
377 return true; \
380 DEFINE_CHECK_SOLID_FUNCTION(32)
381 DEFINE_CHECK_SOLID_FUNCTION(16)
382 DEFINE_CHECK_SOLID_FUNCTION(8)
384 static bool check_solid_tile(VncState *vs, int x, int y, int w, int h,
385 uint32_t* color, bool samecolor)
387 VncDisplay *vd = vs->vd;
389 switch(vd->server->pf.bytes_per_pixel) {
390 case 4:
391 return check_solid_tile32(vs, x, y, w, h, color, samecolor);
392 case 2:
393 return check_solid_tile16(vs, x, y, w, h, color, samecolor);
394 default:
395 return check_solid_tile8(vs, x, y, w, h, color, samecolor);
399 static void find_best_solid_area(VncState *vs, int x, int y, int w, int h,
400 uint32_t color, int *w_ptr, int *h_ptr)
402 int dx, dy, dw, dh;
403 int w_prev;
404 int w_best = 0, h_best = 0;
406 w_prev = w;
408 for (dy = y; dy < y + h; dy += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) {
410 dh = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, y + h - dy);
411 dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, w_prev);
413 if (!check_solid_tile(vs, x, dy, dw, dh, &color, true)) {
414 break;
417 for (dx = x + dw; dx < x + w_prev;) {
418 dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, x + w_prev - dx);
420 if (!check_solid_tile(vs, dx, dy, dw, dh, &color, true)) {
421 break;
423 dx += dw;
426 w_prev = dx - x;
427 if (w_prev * (dy + dh - y) > w_best * h_best) {
428 w_best = w_prev;
429 h_best = dy + dh - y;
433 *w_ptr = w_best;
434 *h_ptr = h_best;
437 static void extend_solid_area(VncState *vs, int x, int y, int w, int h,
438 uint32_t color, int *x_ptr, int *y_ptr,
439 int *w_ptr, int *h_ptr)
441 int cx, cy;
443 /* Try to extend the area upwards. */
444 for ( cy = *y_ptr - 1;
445 cy >= y && check_solid_tile(vs, *x_ptr, cy, *w_ptr, 1, &color, true);
446 cy-- );
447 *h_ptr += *y_ptr - (cy + 1);
448 *y_ptr = cy + 1;
450 /* ... downwards. */
451 for ( cy = *y_ptr + *h_ptr;
452 cy < y + h &&
453 check_solid_tile(vs, *x_ptr, cy, *w_ptr, 1, &color, true);
454 cy++ );
455 *h_ptr += cy - (*y_ptr + *h_ptr);
457 /* ... to the left. */
458 for ( cx = *x_ptr - 1;
459 cx >= x && check_solid_tile(vs, cx, *y_ptr, 1, *h_ptr, &color, true);
460 cx-- );
461 *w_ptr += *x_ptr - (cx + 1);
462 *x_ptr = cx + 1;
464 /* ... to the right. */
465 for ( cx = *x_ptr + *w_ptr;
466 cx < x + w &&
467 check_solid_tile(vs, cx, *y_ptr, 1, *h_ptr, &color, true);
468 cx++ );
469 *w_ptr += cx - (*x_ptr + *w_ptr);
472 static int tight_init_stream(VncState *vs, int stream_id,
473 int level, int strategy)
475 z_streamp zstream = &vs->tight_stream[stream_id];
477 if (zstream->opaque == NULL) {
478 int err;
480 VNC_DEBUG("VNC: TIGHT: initializing zlib stream %d\n", stream_id);
481 VNC_DEBUG("VNC: TIGHT: opaque = %p | vs = %p\n", zstream->opaque, vs);
482 zstream->zalloc = vnc_zlib_zalloc;
483 zstream->zfree = vnc_zlib_zfree;
485 err = deflateInit2(zstream, level, Z_DEFLATED, MAX_WBITS,
486 MAX_MEM_LEVEL, strategy);
488 if (err != Z_OK) {
489 fprintf(stderr, "VNC: error initializing zlib\n");
490 return -1;
493 vs->tight_levels[stream_id] = level;
494 zstream->opaque = vs;
497 if (vs->tight_levels[stream_id] != level) {
498 if (deflateParams(zstream, level, strategy) != Z_OK) {
499 return -1;
501 vs->tight_levels[stream_id] = level;
503 return 0;
506 static void tight_send_compact_size(VncState *vs, size_t len)
508 int lpc = 0;
509 int bytes = 0;
510 char buf[3] = {0, 0, 0};
512 buf[bytes++] = len & 0x7F;
513 if (len > 0x7F) {
514 buf[bytes-1] |= 0x80;
515 buf[bytes++] = (len >> 7) & 0x7F;
516 if (len > 0x3FFF) {
517 buf[bytes-1] |= 0x80;
518 buf[bytes++] = (len >> 14) & 0xFF;
521 for (lpc = 0; lpc < bytes; lpc++) {
522 vnc_write_u8(vs, buf[lpc]);
526 static int tight_compress_data(VncState *vs, int stream_id, size_t bytes,
527 int level, int strategy)
529 z_streamp zstream = &vs->tight_stream[stream_id];
530 int previous_out;
532 if (bytes < VNC_TIGHT_MIN_TO_COMPRESS) {
533 vnc_write(vs, vs->tight.buffer, vs->tight.offset);
534 return bytes;
537 if (tight_init_stream(vs, stream_id, level, strategy)) {
538 return -1;
541 /* reserve memory in output buffer */
542 buffer_reserve(&vs->tight_zlib, bytes + 64);
544 /* set pointers */
545 zstream->next_in = vs->tight.buffer;
546 zstream->avail_in = vs->tight.offset;
547 zstream->next_out = vs->tight_zlib.buffer + vs->tight_zlib.offset;
548 zstream->avail_out = vs->tight_zlib.capacity - vs->tight_zlib.offset;
549 zstream->data_type = Z_BINARY;
550 previous_out = zstream->total_out;
552 /* start encoding */
553 if (deflate(zstream, Z_SYNC_FLUSH) != Z_OK) {
554 fprintf(stderr, "VNC: error during tight compression\n");
555 return -1;
558 vs->tight_zlib.offset = vs->tight_zlib.capacity - zstream->avail_out;
559 bytes = zstream->total_out - previous_out;
561 tight_send_compact_size(vs, bytes);
562 vnc_write(vs, vs->tight_zlib.buffer, bytes);
564 buffer_reset(&vs->tight_zlib);
566 return bytes;
570 * Subencoding implementations.
572 static void tight_pack24(VncState *vs, uint8_t *buf, size_t count, size_t *ret)
574 uint32_t *buf32;
575 uint32_t pix;
576 int rshift, gshift, bshift;
578 buf32 = (uint32_t *)buf;
580 if ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) ==
581 (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG)) {
582 rshift = vs->clientds.pf.rshift;
583 gshift = vs->clientds.pf.gshift;
584 bshift = vs->clientds.pf.bshift;
585 } else {
586 rshift = 24 - vs->clientds.pf.rshift;
587 gshift = 24 - vs->clientds.pf.gshift;
588 bshift = 24 - vs->clientds.pf.bshift;
591 if (ret) {
592 *ret = count * 3;
595 while (count--) {
596 pix = *buf32++;
597 *buf++ = (char)(pix >> rshift);
598 *buf++ = (char)(pix >> gshift);
599 *buf++ = (char)(pix >> bshift);
603 static int send_full_color_rect(VncState *vs, int w, int h)
605 int stream = 0;
606 size_t bytes;
608 vnc_write_u8(vs, stream << 4); /* no flushing, no filter */
610 if (vs->tight_pixel24) {
611 tight_pack24(vs, vs->tight.buffer, w * h, &vs->tight.offset);
612 bytes = 3;
613 } else {
614 bytes = vs->clientds.pf.bytes_per_pixel;
617 bytes = tight_compress_data(vs, stream, w * h * bytes,
618 tight_conf[vs->tight_compression].raw_zlib_level,
619 Z_DEFAULT_STRATEGY);
621 return (bytes >= 0);
624 static int send_solid_rect(VncState *vs)
626 size_t bytes;
628 vnc_write_u8(vs, VNC_TIGHT_FILL << 4); /* no flushing, no filter */
630 if (vs->tight_pixel24) {
631 tight_pack24(vs, vs->tight.buffer, 1, &vs->tight.offset);
632 bytes = 3;
633 } else {
634 bytes = vs->clientds.pf.bytes_per_pixel;
637 vnc_write(vs, vs->tight.buffer, bytes);
638 return 1;
641 static int send_mono_rect(VncState *vs, int w, int h, uint32_t bg, uint32_t fg)
643 size_t bytes;
644 int stream = 1;
645 int level = tight_conf[vs->tight_compression].mono_zlib_level;
647 bytes = ((w + 7) / 8) * h;
649 vnc_write_u8(vs, (stream | VNC_TIGHT_EXPLICIT_FILTER) << 4);
650 vnc_write_u8(vs, VNC_TIGHT_FILTER_PALETTE);
651 vnc_write_u8(vs, 1);
653 switch(vs->clientds.pf.bytes_per_pixel) {
654 case 4:
656 uint32_t buf[2] = {bg, fg};
657 size_t ret = sizeof (buf);
659 if (vs->tight_pixel24) {
660 tight_pack24(vs, (unsigned char*)buf, 2, &ret);
662 vnc_write(vs, buf, ret);
664 tight_encode_mono_rect32(vs->tight.buffer, w, h, bg, fg);
665 break;
667 case 2:
668 vnc_write(vs, &bg, 2);
669 vnc_write(vs, &fg, 2);
670 tight_encode_mono_rect16(vs->tight.buffer, w, h, bg, fg);
671 break;
672 default:
673 vnc_write_u8(vs, bg);
674 vnc_write_u8(vs, fg);
675 tight_encode_mono_rect8(vs->tight.buffer, w, h, bg, fg);
676 break;
678 vs->tight.offset = bytes;
680 bytes = tight_compress_data(vs, stream, bytes, level, Z_DEFAULT_STRATEGY);
681 return (bytes >= 0);
684 struct palette_cb_priv {
685 VncState *vs;
686 uint8_t *header;
689 static void write_palette(const char *key, QObject *obj, void *opaque)
691 struct palette_cb_priv *priv = opaque;
692 VncState *vs = priv->vs;
693 uint32_t bytes = vs->clientds.pf.bytes_per_pixel;
694 uint8_t idx = qint_get_int(qobject_to_qint(obj));
696 if (bytes == 4) {
697 uint32_t color = tight_palette_buf2rgb(32, (uint8_t *)key);
699 ((uint32_t*)priv->header)[idx] = color;
700 } else {
701 uint16_t color = tight_palette_buf2rgb(16, (uint8_t *)key);
703 ((uint16_t*)priv->header)[idx] = color;
707 static int send_palette_rect(VncState *vs, int w, int h, struct QDict *palette)
709 int stream = 2;
710 int level = tight_conf[vs->tight_compression].idx_zlib_level;
711 int colors;
712 size_t bytes;
714 colors = qdict_size(palette);
716 vnc_write_u8(vs, (stream | VNC_TIGHT_EXPLICIT_FILTER) << 4);
717 vnc_write_u8(vs, VNC_TIGHT_FILTER_PALETTE);
718 vnc_write_u8(vs, colors - 1);
720 switch(vs->clientds.pf.bytes_per_pixel) {
721 case 4:
723 size_t old_offset, offset;
724 uint32_t header[qdict_size(palette)];
725 struct palette_cb_priv priv = { vs, (uint8_t *)header };
727 old_offset = vs->output.offset;
728 qdict_iter(palette, write_palette, &priv);
729 vnc_write(vs, header, sizeof(header));
731 if (vs->tight_pixel24) {
732 tight_pack24(vs, vs->output.buffer + old_offset, colors, &offset);
733 vs->output.offset = old_offset + offset;
736 tight_encode_indexed_rect32(vs->tight.buffer, w * h, palette);
737 break;
739 case 2:
741 uint16_t header[qdict_size(palette)];
742 struct palette_cb_priv priv = { vs, (uint8_t *)header };
744 qdict_iter(palette, write_palette, &priv);
745 vnc_write(vs, header, sizeof(header));
746 tight_encode_indexed_rect16(vs->tight.buffer, w * h, palette);
747 break;
749 default:
750 return -1; /* No palette for 8bits colors */
751 break;
753 bytes = w * h;
754 vs->tight.offset = bytes;
756 bytes = tight_compress_data(vs, stream, bytes,
757 level, Z_DEFAULT_STRATEGY);
758 return (bytes >= 0);
761 static void vnc_tight_start(VncState *vs)
763 buffer_reset(&vs->tight);
765 // make the output buffer be the zlib buffer, so we can compress it later
766 vs->tight_tmp = vs->output;
767 vs->output = vs->tight;
770 static void vnc_tight_stop(VncState *vs)
772 // switch back to normal output/zlib buffers
773 vs->tight = vs->output;
774 vs->output = vs->tight_tmp;
777 static int send_sub_rect(VncState *vs, int x, int y, int w, int h)
779 struct QDict *palette = NULL;
780 uint32_t bg = 0, fg = 0;
781 int colors;
782 int ret = 0;
784 vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_TIGHT);
786 vnc_tight_start(vs);
787 vnc_raw_send_framebuffer_update(vs, x, y, w, h);
788 vnc_tight_stop(vs);
790 colors = tight_fill_palette(vs, x, y, w * h, &fg, &bg, &palette);
792 if (colors == 0) {
793 ret = send_full_color_rect(vs, w, h);
794 } else if (colors == 1) {
795 ret = send_solid_rect(vs);
796 } else if (colors == 2) {
797 ret = send_mono_rect(vs, w, h, bg, fg);
798 } else if (colors <= 256) {
799 ret = send_palette_rect(vs, w, h, palette);
801 QDECREF(palette);
802 return ret;
805 static int send_sub_rect_solid(VncState *vs, int x, int y, int w, int h)
807 vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_TIGHT);
809 vnc_tight_start(vs);
810 vnc_raw_send_framebuffer_update(vs, x, y, w, h);
811 vnc_tight_stop(vs);
813 return send_solid_rect(vs);
816 static int send_rect_simple(VncState *vs, int x, int y, int w, int h)
818 int max_size, max_width;
819 int max_sub_width, max_sub_height;
820 int dx, dy;
821 int rw, rh;
822 int n = 0;
824 max_size = tight_conf[vs->tight_compression].max_rect_size;
825 max_width = tight_conf[vs->tight_compression].max_rect_width;
827 if (w > max_width || w * h > max_size) {
828 max_sub_width = (w > max_width) ? max_width : w;
829 max_sub_height = max_size / max_sub_width;
831 for (dy = 0; dy < h; dy += max_sub_height) {
832 for (dx = 0; dx < w; dx += max_width) {
833 rw = MIN(max_sub_width, w - dx);
834 rh = MIN(max_sub_height, h - dy);
835 n += send_sub_rect(vs, x+dx, y+dy, rw, rh);
838 } else {
839 n += send_sub_rect(vs, x, y, w, h);
842 return n;
845 static int find_large_solid_color_rect(VncState *vs, int x, int y,
846 int w, int h, int max_rows)
848 int dx, dy, dw, dh;
849 int n = 0;
851 /* Try to find large solid-color areas and send them separately. */
853 for (dy = y; dy < y + h; dy += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) {
855 /* If a rectangle becomes too large, send its upper part now. */
857 if (dy - y >= max_rows) {
858 n += send_rect_simple(vs, x, y, w, max_rows);
859 y += max_rows;
860 h -= max_rows;
863 dh = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, (y + h - dy));
865 for (dx = x; dx < x + w; dx += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) {
866 uint32_t color_value;
867 int x_best, y_best, w_best, h_best;
869 dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, (x + w - dx));
871 if (!check_solid_tile(vs, dx, dy, dw, dh, &color_value, false)) {
872 continue ;
875 /* Get dimensions of solid-color area. */
877 find_best_solid_area(vs, dx, dy, w - (dx - x), h - (dy - y),
878 color_value, &w_best, &h_best);
880 /* Make sure a solid rectangle is large enough
881 (or the whole rectangle is of the same color). */
883 if (w_best * h_best != w * h &&
884 w_best * h_best < VNC_TIGHT_MIN_SOLID_SUBRECT_SIZE) {
885 continue;
888 /* Try to extend solid rectangle to maximum size. */
890 x_best = dx; y_best = dy;
891 extend_solid_area(vs, x, y, w, h, color_value,
892 &x_best, &y_best, &w_best, &h_best);
894 /* Send rectangles at top and left to solid-color area. */
896 if (y_best != y) {
897 n += send_rect_simple(vs, x, y, w, y_best-y);
899 if (x_best != x) {
900 n += vnc_tight_send_framebuffer_update(vs, x, y_best,
901 x_best-x, h_best);
904 /* Send solid-color rectangle. */
905 n += send_sub_rect_solid(vs, x_best, y_best, w_best, h_best);
907 /* Send remaining rectangles (at right and bottom). */
909 if (x_best + w_best != x + w) {
910 n += vnc_tight_send_framebuffer_update(vs, x_best+w_best,
911 y_best,
912 w-(x_best-x)-w_best,
913 h_best);
915 if (y_best + h_best != y + h) {
916 n += vnc_tight_send_framebuffer_update(vs, x, y_best+h_best,
917 w, h-(y_best-y)-h_best);
920 /* Return after all recursive calls are done. */
921 return n;
924 return n + send_rect_simple(vs, x, y, w, h);
927 int vnc_tight_send_framebuffer_update(VncState *vs, int x, int y,
928 int w, int h)
930 int max_rows;
932 if (vs->clientds.pf.bytes_per_pixel == 4 && vs->clientds.pf.rmax == 0xFF &&
933 vs->clientds.pf.bmax == 0xFF && vs->clientds.pf.gmax == 0xFF) {
934 vs->tight_pixel24 = true;
935 } else {
936 vs->tight_pixel24 = false;
939 if (w * h < VNC_TIGHT_MIN_SPLIT_RECT_SIZE)
940 return send_rect_simple(vs, x, y, w, h);
942 /* Calculate maximum number of rows in one non-solid rectangle. */
944 max_rows = tight_conf[vs->tight_compression].max_rect_size;
945 max_rows /= MIN(tight_conf[vs->tight_compression].max_rect_width, w);
947 return find_large_solid_color_rect(vs, x, y, w, h, max_rows);
950 void vnc_tight_clear(VncState *vs)
952 int i;
953 for (i=0; i<ARRAY_SIZE(vs->tight_stream); i++) {
954 if (vs->tight_stream[i].opaque) {
955 deflateEnd(&vs->tight_stream[i]);
959 buffer_free(&vs->tight);
960 buffer_free(&vs->tight_zlib);