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
29 #include "config-host.h"
34 #ifdef CONFIG_VNC_JPEG
39 #include "qemu-common.h"
44 #include "vnc-enc-tight.h"
45 #include "vnc-palette.h"
47 /* Compression level stuff. The following array contains various
48 encoder parameters for each of 10 compression levels (0..9).
49 Last three parameters correspond to JPEG quality levels (0..9). */
52 int max_rect_size, max_rect_width;
53 int mono_min_rect_size, gradient_min_rect_size;
54 int idx_zlib_level, mono_zlib_level, raw_zlib_level, gradient_zlib_level;
55 int gradient_threshold, gradient_threshold24;
56 int idx_max_colors_divisor;
57 int jpeg_quality, jpeg_threshold, jpeg_threshold24;
59 { 512, 32, 6, 65536, 0, 0, 0, 0, 0, 0, 4, 5, 10000, 23000 },
60 { 2048, 128, 6, 65536, 1, 1, 1, 0, 0, 0, 8, 10, 8000, 18000 },
61 { 6144, 256, 8, 65536, 3, 3, 2, 0, 0, 0, 24, 15, 6500, 15000 },
62 { 10240, 1024, 12, 65536, 5, 5, 3, 0, 0, 0, 32, 25, 5000, 12000 },
63 { 16384, 2048, 12, 65536, 6, 6, 4, 0, 0, 0, 32, 37, 4000, 10000 },
64 { 32768, 2048, 12, 4096, 7, 7, 5, 4, 150, 380, 32, 50, 3000, 8000 },
65 { 65536, 2048, 16, 4096, 7, 7, 6, 4, 170, 420, 48, 60, 2000, 5000 },
66 { 65536, 2048, 16, 4096, 8, 8, 7, 5, 180, 450, 64, 70, 1000, 2500 },
67 { 65536, 2048, 32, 8192, 9, 9, 8, 6, 190, 475, 64, 75, 500, 1200 },
68 { 65536, 2048, 32, 8192, 9, 9, 9, 6, 200, 500, 96, 80, 200, 500 }
72 static int tight_send_framebuffer_update(VncState *vs, int x, int y,
77 int png_zlib_level, png_filters;
78 } tight_png_conf[] = {
79 { 0, PNG_NO_FILTERS },
80 { 1, PNG_NO_FILTERS },
81 { 2, PNG_NO_FILTERS },
82 { 3, PNG_NO_FILTERS },
83 { 4, PNG_NO_FILTERS },
84 { 5, PNG_ALL_FILTERS },
85 { 6, PNG_ALL_FILTERS },
86 { 7, PNG_ALL_FILTERS },
87 { 8, PNG_ALL_FILTERS },
88 { 9, PNG_ALL_FILTERS },
91 static int send_png_rect(VncState *vs, int x, int y, int w, int h,
94 static bool tight_can_send_png_rect(VncState *vs, int w, int h)
96 if (vs->tight.type != VNC_ENCODING_TIGHT_PNG) {
100 if (ds_get_bytes_per_pixel(vs->ds) == 1 ||
101 vs->clientds.pf.bytes_per_pixel == 1) {
110 * Code to guess if given rectangle is suitable for smooth image
111 * compression (by applying "gradient" filter or JPEG coder).
115 tight_detect_smooth_image24(VncState *vs, int w, int h)
120 unsigned int stats[256];
124 unsigned char *buf = vs->tight.tight.buffer;
127 * If client is big-endian, color samples begin from the second
128 * byte (offset 1) of a 32-bit pixel value.
130 off = !!(vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG);
132 memset(stats, 0, sizeof (stats));
134 for (y = 0, x = 0; y < h && x < w;) {
135 for (d = 0; d < h - y && d < w - x - VNC_TIGHT_DETECT_SUBROW_WIDTH;
137 for (c = 0; c < 3; c++) {
138 left[c] = buf[((y+d)*w+x+d)*4+off+c] & 0xFF;
140 for (dx = 1; dx <= VNC_TIGHT_DETECT_SUBROW_WIDTH; dx++) {
141 for (c = 0; c < 3; c++) {
142 pix = buf[((y+d)*w+x+d+dx)*4+off+c] & 0xFF;
143 stats[abs(pix - left[c])]++;
158 /* 95% smooth or more ... */
159 if (stats[0] * 33 / pixels >= 95) {
164 for (c = 1; c < 8; c++) {
165 errors += stats[c] * (c * c);
166 if (stats[c] == 0 || stats[c] > stats[c-1] * 2) {
170 for (; c < 256; c++) {
171 errors += stats[c] * (c * c);
173 errors /= (pixels * 3 - stats[0]);
178 #define DEFINE_DETECT_FUNCTION(bpp) \
180 static unsigned int \
181 tight_detect_smooth_image##bpp(VncState *vs, int w, int h) { \
184 int max[3], shift[3]; \
187 unsigned int stats[256]; \
189 int sample, sum, left[3]; \
190 unsigned int errors; \
191 unsigned char *buf = vs->tight.tight.buffer; \
193 endian = ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) != \
194 (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG)); \
197 max[0] = vs->clientds.pf.rmax; \
198 max[1] = vs->clientds.pf.gmax; \
199 max[2] = vs->clientds.pf.bmax; \
200 shift[0] = vs->clientds.pf.rshift; \
201 shift[1] = vs->clientds.pf.gshift; \
202 shift[2] = vs->clientds.pf.bshift; \
204 memset(stats, 0, sizeof(stats)); \
207 while (y < h && x < w) { \
208 for (d = 0; d < h - y && \
209 d < w - x - VNC_TIGHT_DETECT_SUBROW_WIDTH; d++) { \
210 pix = ((uint##bpp##_t *)buf)[(y+d)*w+x+d]; \
212 pix = bswap##bpp(pix); \
214 for (c = 0; c < 3; c++) { \
215 left[c] = (int)(pix >> shift[c] & max[c]); \
217 for (dx = 1; dx <= VNC_TIGHT_DETECT_SUBROW_WIDTH; \
219 pix = ((uint##bpp##_t *)buf)[(y+d)*w+x+d+dx]; \
221 pix = bswap##bpp(pix); \
224 for (c = 0; c < 3; c++) { \
225 sample = (int)(pix >> shift[c] & max[c]); \
226 sum += abs(sample - left[c]); \
245 if ((stats[0] + stats[1]) * 100 / pixels >= 90) { \
250 for (c = 1; c < 8; c++) { \
251 errors += stats[c] * (c * c); \
252 if (stats[c] == 0 || stats[c] > stats[c-1] * 2) { \
256 for (; c < 256; c++) { \
257 errors += stats[c] * (c * c); \
259 errors /= (pixels - stats[0]); \
264 DEFINE_DETECT_FUNCTION(16)
265 DEFINE_DETECT_FUNCTION(32)
268 tight_detect_smooth_image(VncState *vs, int w, int h)
271 int compression = vs->tight.compression;
272 int quality = vs->tight.quality;
274 if (!vs->vd->lossy) {
278 if (ds_get_bytes_per_pixel(vs->ds) == 1 ||
279 vs->clientds.pf.bytes_per_pixel == 1 ||
280 w < VNC_TIGHT_DETECT_MIN_WIDTH || h < VNC_TIGHT_DETECT_MIN_HEIGHT) {
284 if (vs->tight.quality != (uint8_t)-1) {
285 if (w * h < VNC_TIGHT_JPEG_MIN_RECT_SIZE) {
289 if (w * h < tight_conf[compression].gradient_min_rect_size) {
294 if (vs->clientds.pf.bytes_per_pixel == 4) {
295 if (vs->tight.pixel24) {
296 errors = tight_detect_smooth_image24(vs, w, h);
297 if (vs->tight.quality != (uint8_t)-1) {
298 return (errors < tight_conf[quality].jpeg_threshold24);
300 return (errors < tight_conf[compression].gradient_threshold24);
302 errors = tight_detect_smooth_image32(vs, w, h);
305 errors = tight_detect_smooth_image16(vs, w, h);
308 return (errors < tight_conf[quality].jpeg_threshold);
310 return (errors < tight_conf[compression].gradient_threshold);
314 * Code to determine how many different colors used in rectangle.
316 #define DEFINE_FILL_PALETTE_FUNCTION(bpp) \
319 tight_fill_palette##bpp(VncState *vs, int x, int y, \
320 int max, size_t count, \
321 uint32_t *bg, uint32_t *fg, \
322 VncPalette **palette) { \
323 uint##bpp##_t *data; \
324 uint##bpp##_t c0, c1, ci; \
327 data = (uint##bpp##_t *)vs->tight.tight.buffer; \
331 while (i < count && data[i] == c0) \
345 for (i++; i < count; i++) { \
349 } else if (ci == c1) { \
356 *bg = (uint32_t)c0; \
357 *fg = (uint32_t)c1; \
359 *bg = (uint32_t)c1; \
360 *fg = (uint32_t)c0; \
369 *palette = palette_new(max, bpp); \
370 palette_put(*palette, c0); \
371 palette_put(*palette, c1); \
372 palette_put(*palette, ci); \
374 for (i++; i < count; i++) { \
375 if (data[i] == ci) { \
379 if (!palette_put(*palette, (uint32_t)ci)) { \
385 return palette_size(*palette); \
388 DEFINE_FILL_PALETTE_FUNCTION(8)
389 DEFINE_FILL_PALETTE_FUNCTION(16)
390 DEFINE_FILL_PALETTE_FUNCTION(32)
392 static int tight_fill_palette(VncState *vs, int x, int y,
393 size_t count, uint32_t *bg, uint32_t *fg,
394 VncPalette **palette)
398 max = count / tight_conf[vs->tight.compression].idx_max_colors_divisor;
400 count >= tight_conf[vs->tight.compression].mono_min_rect_size) {
407 switch(vs->clientds.pf.bytes_per_pixel) {
409 return tight_fill_palette32(vs, x, y, max, count, bg, fg, palette);
411 return tight_fill_palette16(vs, x, y, max, count, bg, fg, palette);
414 return tight_fill_palette8(vs, x, y, max, count, bg, fg, palette);
420 * Converting truecolor samples into palette indices.
422 #define DEFINE_IDX_ENCODE_FUNCTION(bpp) \
425 tight_encode_indexed_rect##bpp(uint8_t *buf, int count, \
426 VncPalette *palette) { \
427 uint##bpp##_t *src; \
432 src = (uint##bpp##_t *) buf; \
434 for (i = 0; i < count; i++) { \
438 while (i < count && *src == rgb) { \
441 idx = palette_idx(palette, rgb); \
443 * Should never happen, but don't break everything \
444 * if it does, use the first color instead \
446 if (idx == (uint8_t)-1) { \
456 DEFINE_IDX_ENCODE_FUNCTION(16)
457 DEFINE_IDX_ENCODE_FUNCTION(32)
459 #define DEFINE_MONO_ENCODE_FUNCTION(bpp) \
462 tight_encode_mono_rect##bpp(uint8_t *buf, int w, int h, \
463 uint##bpp##_t bg, uint##bpp##_t fg) { \
464 uint##bpp##_t *ptr; \
465 unsigned int value, mask; \
469 ptr = (uint##bpp##_t *) buf; \
470 aligned_width = w - w % 8; \
472 for (y = 0; y < h; y++) { \
473 for (x = 0; x < aligned_width; x += 8) { \
474 for (bg_bits = 0; bg_bits < 8; bg_bits++) { \
475 if (*ptr++ != bg) { \
479 if (bg_bits == 8) { \
483 mask = 0x80 >> bg_bits; \
485 for (bg_bits++; bg_bits < 8; bg_bits++) { \
487 if (*ptr++ != bg) { \
491 *buf++ = (uint8_t)value; \
500 for (; x < w; x++) { \
501 if (*ptr++ != bg) { \
506 *buf++ = (uint8_t)value; \
510 DEFINE_MONO_ENCODE_FUNCTION(8)
511 DEFINE_MONO_ENCODE_FUNCTION(16)
512 DEFINE_MONO_ENCODE_FUNCTION(32)
515 * ``Gradient'' filter for 24-bit color samples.
516 * Should be called only when redMax, greenMax and blueMax are 255.
517 * Color components assumed to be byte-aligned.
521 tight_filter_gradient24(VncState *vs, uint8_t *buf, int w, int h)
527 int here[3], upper[3], left[3], upperleft[3];
531 buf32 = (uint32_t *)buf;
532 memset(vs->tight.gradient.buffer, 0, w * 3 * sizeof(int));
534 if ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) ==
535 (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG)) {
536 shift[0] = vs->clientds.pf.rshift;
537 shift[1] = vs->clientds.pf.gshift;
538 shift[2] = vs->clientds.pf.bshift;
540 shift[0] = 24 - vs->clientds.pf.rshift;
541 shift[1] = 24 - vs->clientds.pf.gshift;
542 shift[2] = 24 - vs->clientds.pf.bshift;
545 for (y = 0; y < h; y++) {
546 for (c = 0; c < 3; c++) {
550 prev = (int *)vs->tight.gradient.buffer;
551 for (x = 0; x < w; x++) {
553 for (c = 0; c < 3; c++) {
554 upperleft[c] = upper[c];
557 here[c] = (int)(pix32 >> shift[c] & 0xFF);
560 prediction = left[c] + upper[c] - upperleft[c];
561 if (prediction < 0) {
563 } else if (prediction > 0xFF) {
566 *buf++ = (char)(here[c] - prediction);
574 * ``Gradient'' filter for other color depths.
577 #define DEFINE_GRADIENT_FILTER_FUNCTION(bpp) \
580 tight_filter_gradient##bpp(VncState *vs, uint##bpp##_t *buf, \
582 uint##bpp##_t pix, diff; \
585 int max[3], shift[3]; \
586 int here[3], upper[3], left[3], upperleft[3]; \
590 memset (vs->tight.gradient.buffer, 0, w * 3 * sizeof(int)); \
592 endian = ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) != \
593 (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG)); \
595 max[0] = vs->clientds.pf.rmax; \
596 max[1] = vs->clientds.pf.gmax; \
597 max[2] = vs->clientds.pf.bmax; \
598 shift[0] = vs->clientds.pf.rshift; \
599 shift[1] = vs->clientds.pf.gshift; \
600 shift[2] = vs->clientds.pf.bshift; \
602 for (y = 0; y < h; y++) { \
603 for (c = 0; c < 3; c++) { \
607 prev = (int *)vs->tight.gradient.buffer; \
608 for (x = 0; x < w; x++) { \
611 pix = bswap##bpp(pix); \
614 for (c = 0; c < 3; c++) { \
615 upperleft[c] = upper[c]; \
618 here[c] = (int)(pix >> shift[c] & max[c]); \
621 prediction = left[c] + upper[c] - upperleft[c]; \
622 if (prediction < 0) { \
624 } else if (prediction > max[c]) { \
625 prediction = max[c]; \
627 diff |= ((here[c] - prediction) & max[c]) \
631 diff = bswap##bpp(diff); \
638 DEFINE_GRADIENT_FILTER_FUNCTION(16)
639 DEFINE_GRADIENT_FILTER_FUNCTION(32)
642 * Check if a rectangle is all of the same color. If needSameColor is
643 * set to non-zero, then also check that its color equals to the
644 * *colorPtr value. The result is 1 if the test is successful, and in
645 * that case new color will be stored in *colorPtr.
648 #define DEFINE_CHECK_SOLID_FUNCTION(bpp) \
651 check_solid_tile##bpp(VncState *vs, int x, int y, int w, int h, \
652 uint32_t* color, bool samecolor) \
654 VncDisplay *vd = vs->vd; \
655 uint##bpp##_t *fbptr; \
659 fbptr = (uint##bpp##_t *) \
660 (vd->server->data + y * ds_get_linesize(vs->ds) + \
661 x * ds_get_bytes_per_pixel(vs->ds)); \
664 if (samecolor && (uint32_t)c != *color) { \
668 for (dy = 0; dy < h; dy++) { \
669 for (dx = 0; dx < w; dx++) { \
670 if (c != fbptr[dx]) { \
674 fbptr = (uint##bpp##_t *) \
675 ((uint8_t *)fbptr + ds_get_linesize(vs->ds)); \
678 *color = (uint32_t)c; \
682 DEFINE_CHECK_SOLID_FUNCTION(32)
683 DEFINE_CHECK_SOLID_FUNCTION(16)
684 DEFINE_CHECK_SOLID_FUNCTION(8)
686 static bool check_solid_tile(VncState *vs, int x, int y, int w, int h,
687 uint32_t* color, bool samecolor)
689 VncDisplay *vd = vs->vd;
691 switch(vd->server->pf.bytes_per_pixel) {
693 return check_solid_tile32(vs, x, y, w, h, color, samecolor);
695 return check_solid_tile16(vs, x, y, w, h, color, samecolor);
697 return check_solid_tile8(vs, x, y, w, h, color, samecolor);
701 static void find_best_solid_area(VncState *vs, int x, int y, int w, int h,
702 uint32_t color, int *w_ptr, int *h_ptr)
706 int w_best = 0, h_best = 0;
710 for (dy = y; dy < y + h; dy += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) {
712 dh = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, y + h - dy);
713 dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, w_prev);
715 if (!check_solid_tile(vs, x, dy, dw, dh, &color, true)) {
719 for (dx = x + dw; dx < x + w_prev;) {
720 dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, x + w_prev - dx);
722 if (!check_solid_tile(vs, dx, dy, dw, dh, &color, true)) {
729 if (w_prev * (dy + dh - y) > w_best * h_best) {
731 h_best = dy + dh - y;
739 static void extend_solid_area(VncState *vs, int x, int y, int w, int h,
740 uint32_t color, int *x_ptr, int *y_ptr,
741 int *w_ptr, int *h_ptr)
745 /* Try to extend the area upwards. */
746 for ( cy = *y_ptr - 1;
747 cy >= y && check_solid_tile(vs, *x_ptr, cy, *w_ptr, 1, &color, true);
749 *h_ptr += *y_ptr - (cy + 1);
753 for ( cy = *y_ptr + *h_ptr;
755 check_solid_tile(vs, *x_ptr, cy, *w_ptr, 1, &color, true);
757 *h_ptr += cy - (*y_ptr + *h_ptr);
759 /* ... to the left. */
760 for ( cx = *x_ptr - 1;
761 cx >= x && check_solid_tile(vs, cx, *y_ptr, 1, *h_ptr, &color, true);
763 *w_ptr += *x_ptr - (cx + 1);
766 /* ... to the right. */
767 for ( cx = *x_ptr + *w_ptr;
769 check_solid_tile(vs, cx, *y_ptr, 1, *h_ptr, &color, true);
771 *w_ptr += cx - (*x_ptr + *w_ptr);
774 static int tight_init_stream(VncState *vs, int stream_id,
775 int level, int strategy)
777 z_streamp zstream = &vs->tight.stream[stream_id];
779 if (zstream->opaque == NULL) {
782 VNC_DEBUG("VNC: TIGHT: initializing zlib stream %d\n", stream_id);
783 VNC_DEBUG("VNC: TIGHT: opaque = %p | vs = %p\n", zstream->opaque, vs);
784 zstream->zalloc = vnc_zlib_zalloc;
785 zstream->zfree = vnc_zlib_zfree;
787 err = deflateInit2(zstream, level, Z_DEFLATED, MAX_WBITS,
788 MAX_MEM_LEVEL, strategy);
791 fprintf(stderr, "VNC: error initializing zlib\n");
795 vs->tight.levels[stream_id] = level;
796 zstream->opaque = vs;
799 if (vs->tight.levels[stream_id] != level) {
800 if (deflateParams(zstream, level, strategy) != Z_OK) {
803 vs->tight.levels[stream_id] = level;
808 static void tight_send_compact_size(VncState *vs, size_t len)
812 char buf[3] = {0, 0, 0};
814 buf[bytes++] = len & 0x7F;
816 buf[bytes-1] |= 0x80;
817 buf[bytes++] = (len >> 7) & 0x7F;
819 buf[bytes-1] |= 0x80;
820 buf[bytes++] = (len >> 14) & 0xFF;
823 for (lpc = 0; lpc < bytes; lpc++) {
824 vnc_write_u8(vs, buf[lpc]);
828 static int tight_compress_data(VncState *vs, int stream_id, size_t bytes,
829 int level, int strategy)
831 z_streamp zstream = &vs->tight.stream[stream_id];
834 if (bytes < VNC_TIGHT_MIN_TO_COMPRESS) {
835 vnc_write(vs, vs->tight.tight.buffer, vs->tight.tight.offset);
839 if (tight_init_stream(vs, stream_id, level, strategy)) {
843 /* reserve memory in output buffer */
844 buffer_reserve(&vs->tight.zlib, bytes + 64);
847 zstream->next_in = vs->tight.tight.buffer;
848 zstream->avail_in = vs->tight.tight.offset;
849 zstream->next_out = vs->tight.zlib.buffer + vs->tight.zlib.offset;
850 zstream->avail_out = vs->tight.zlib.capacity - vs->tight.zlib.offset;
851 previous_out = zstream->avail_out;
852 zstream->data_type = Z_BINARY;
855 if (deflate(zstream, Z_SYNC_FLUSH) != Z_OK) {
856 fprintf(stderr, "VNC: error during tight compression\n");
860 vs->tight.zlib.offset = vs->tight.zlib.capacity - zstream->avail_out;
861 /* ...how much data has actually been produced by deflate() */
862 bytes = previous_out - zstream->avail_out;
864 tight_send_compact_size(vs, bytes);
865 vnc_write(vs, vs->tight.zlib.buffer, bytes);
867 buffer_reset(&vs->tight.zlib);
873 * Subencoding implementations.
875 static void tight_pack24(VncState *vs, uint8_t *buf, size_t count, size_t *ret)
879 int rshift, gshift, bshift;
881 buf32 = (uint32_t *)buf;
883 if ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) ==
884 (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG)) {
885 rshift = vs->clientds.pf.rshift;
886 gshift = vs->clientds.pf.gshift;
887 bshift = vs->clientds.pf.bshift;
889 rshift = 24 - vs->clientds.pf.rshift;
890 gshift = 24 - vs->clientds.pf.gshift;
891 bshift = 24 - vs->clientds.pf.bshift;
900 *buf++ = (char)(pix >> rshift);
901 *buf++ = (char)(pix >> gshift);
902 *buf++ = (char)(pix >> bshift);
906 static int send_full_color_rect(VncState *vs, int x, int y, int w, int h)
911 #ifdef CONFIG_VNC_PNG
912 if (tight_can_send_png_rect(vs, w, h)) {
913 return send_png_rect(vs, x, y, w, h, NULL);
917 vnc_write_u8(vs, stream << 4); /* no flushing, no filter */
919 if (vs->tight.pixel24) {
920 tight_pack24(vs, vs->tight.tight.buffer, w * h, &vs->tight.tight.offset);
923 bytes = vs->clientds.pf.bytes_per_pixel;
926 bytes = tight_compress_data(vs, stream, w * h * bytes,
927 tight_conf[vs->tight.compression].raw_zlib_level,
933 static int send_solid_rect(VncState *vs)
937 vnc_write_u8(vs, VNC_TIGHT_FILL << 4); /* no flushing, no filter */
939 if (vs->tight.pixel24) {
940 tight_pack24(vs, vs->tight.tight.buffer, 1, &vs->tight.tight.offset);
943 bytes = vs->clientds.pf.bytes_per_pixel;
946 vnc_write(vs, vs->tight.tight.buffer, bytes);
950 static int send_mono_rect(VncState *vs, int x, int y,
951 int w, int h, uint32_t bg, uint32_t fg)
955 int level = tight_conf[vs->tight.compression].mono_zlib_level;
957 #ifdef CONFIG_VNC_PNG
958 if (tight_can_send_png_rect(vs, w, h)) {
960 int bpp = vs->clientds.pf.bytes_per_pixel * 8;
961 VncPalette *palette = palette_new(2, bpp);
963 palette_put(palette, bg);
964 palette_put(palette, fg);
965 ret = send_png_rect(vs, x, y, w, h, palette);
966 palette_destroy(palette);
971 bytes = ((w + 7) / 8) * h;
973 vnc_write_u8(vs, (stream | VNC_TIGHT_EXPLICIT_FILTER) << 4);
974 vnc_write_u8(vs, VNC_TIGHT_FILTER_PALETTE);
977 switch(vs->clientds.pf.bytes_per_pixel) {
980 uint32_t buf[2] = {bg, fg};
981 size_t ret = sizeof (buf);
983 if (vs->tight.pixel24) {
984 tight_pack24(vs, (unsigned char*)buf, 2, &ret);
986 vnc_write(vs, buf, ret);
988 tight_encode_mono_rect32(vs->tight.tight.buffer, w, h, bg, fg);
992 vnc_write(vs, &bg, 2);
993 vnc_write(vs, &fg, 2);
994 tight_encode_mono_rect16(vs->tight.tight.buffer, w, h, bg, fg);
997 vnc_write_u8(vs, bg);
998 vnc_write_u8(vs, fg);
999 tight_encode_mono_rect8(vs->tight.tight.buffer, w, h, bg, fg);
1002 vs->tight.tight.offset = bytes;
1004 bytes = tight_compress_data(vs, stream, bytes, level, Z_DEFAULT_STRATEGY);
1005 return (bytes >= 0);
1008 struct palette_cb_priv {
1011 #ifdef CONFIG_VNC_PNG
1012 png_colorp png_palette;
1016 static void write_palette(int idx, uint32_t color, void *opaque)
1018 struct palette_cb_priv *priv = opaque;
1019 VncState *vs = priv->vs;
1020 uint32_t bytes = vs->clientds.pf.bytes_per_pixel;
1023 ((uint32_t*)priv->header)[idx] = color;
1025 ((uint16_t*)priv->header)[idx] = color;
1029 static bool send_gradient_rect(VncState *vs, int x, int y, int w, int h)
1032 int level = tight_conf[vs->tight.compression].gradient_zlib_level;
1035 if (vs->clientds.pf.bytes_per_pixel == 1)
1036 return send_full_color_rect(vs, x, y, w, h);
1038 vnc_write_u8(vs, (stream | VNC_TIGHT_EXPLICIT_FILTER) << 4);
1039 vnc_write_u8(vs, VNC_TIGHT_FILTER_GRADIENT);
1041 buffer_reserve(&vs->tight.gradient, w * 3 * sizeof (int));
1043 if (vs->tight.pixel24) {
1044 tight_filter_gradient24(vs, vs->tight.tight.buffer, w, h);
1046 } else if (vs->clientds.pf.bytes_per_pixel == 4) {
1047 tight_filter_gradient32(vs, (uint32_t *)vs->tight.tight.buffer, w, h);
1050 tight_filter_gradient16(vs, (uint16_t *)vs->tight.tight.buffer, w, h);
1054 buffer_reset(&vs->tight.gradient);
1056 bytes = w * h * bytes;
1057 vs->tight.tight.offset = bytes;
1059 bytes = tight_compress_data(vs, stream, bytes,
1061 return (bytes >= 0);
1064 static int send_palette_rect(VncState *vs, int x, int y,
1065 int w, int h, VncPalette *palette)
1068 int level = tight_conf[vs->tight.compression].idx_zlib_level;
1072 #ifdef CONFIG_VNC_PNG
1073 if (tight_can_send_png_rect(vs, w, h)) {
1074 return send_png_rect(vs, x, y, w, h, palette);
1078 colors = palette_size(palette);
1080 vnc_write_u8(vs, (stream | VNC_TIGHT_EXPLICIT_FILTER) << 4);
1081 vnc_write_u8(vs, VNC_TIGHT_FILTER_PALETTE);
1082 vnc_write_u8(vs, colors - 1);
1084 switch(vs->clientds.pf.bytes_per_pixel) {
1087 size_t old_offset, offset;
1088 uint32_t header[palette_size(palette)];
1089 struct palette_cb_priv priv = { vs, (uint8_t *)header };
1091 old_offset = vs->output.offset;
1092 palette_iter(palette, write_palette, &priv);
1093 vnc_write(vs, header, sizeof(header));
1095 if (vs->tight.pixel24) {
1096 tight_pack24(vs, vs->output.buffer + old_offset, colors, &offset);
1097 vs->output.offset = old_offset + offset;
1100 tight_encode_indexed_rect32(vs->tight.tight.buffer, w * h, palette);
1105 uint16_t header[palette_size(palette)];
1106 struct palette_cb_priv priv = { vs, (uint8_t *)header };
1108 palette_iter(palette, write_palette, &priv);
1109 vnc_write(vs, header, sizeof(header));
1110 tight_encode_indexed_rect16(vs->tight.tight.buffer, w * h, palette);
1114 return -1; /* No palette for 8bits colors */
1118 vs->tight.tight.offset = bytes;
1120 bytes = tight_compress_data(vs, stream, bytes,
1121 level, Z_DEFAULT_STRATEGY);
1122 return (bytes >= 0);
1125 #if defined(CONFIG_VNC_JPEG) || defined(CONFIG_VNC_PNG)
1126 static void rgb_prepare_row24(VncState *vs, uint8_t *dst, int x, int y,
1129 VncDisplay *vd = vs->vd;
1133 fbptr = (uint32_t *)(vd->server->data + y * ds_get_linesize(vs->ds) +
1134 x * ds_get_bytes_per_pixel(vs->ds));
1138 *dst++ = (uint8_t)(pix >> vs->ds->surface->pf.rshift);
1139 *dst++ = (uint8_t)(pix >> vs->ds->surface->pf.gshift);
1140 *dst++ = (uint8_t)(pix >> vs->ds->surface->pf.bshift);
1144 #define DEFINE_RGB_GET_ROW_FUNCTION(bpp) \
1147 rgb_prepare_row##bpp(VncState *vs, uint8_t *dst, \
1148 int x, int y, int count) \
1150 VncDisplay *vd = vs->vd; \
1151 uint##bpp##_t *fbptr; \
1152 uint##bpp##_t pix; \
1155 fbptr = (uint##bpp##_t *) \
1156 (vd->server->data + y * ds_get_linesize(vs->ds) + \
1157 x * ds_get_bytes_per_pixel(vs->ds)); \
1162 r = (int)((pix >> vs->ds->surface->pf.rshift) \
1163 & vs->ds->surface->pf.rmax); \
1164 g = (int)((pix >> vs->ds->surface->pf.gshift) \
1165 & vs->ds->surface->pf.gmax); \
1166 b = (int)((pix >> vs->ds->surface->pf.bshift) \
1167 & vs->ds->surface->pf.bmax); \
1169 *dst++ = (uint8_t)((r * 255 + vs->ds->surface->pf.rmax / 2) \
1170 / vs->ds->surface->pf.rmax); \
1171 *dst++ = (uint8_t)((g * 255 + vs->ds->surface->pf.gmax / 2) \
1172 / vs->ds->surface->pf.gmax); \
1173 *dst++ = (uint8_t)((b * 255 + vs->ds->surface->pf.bmax / 2) \
1174 / vs->ds->surface->pf.bmax); \
1178 DEFINE_RGB_GET_ROW_FUNCTION(16)
1179 DEFINE_RGB_GET_ROW_FUNCTION(32)
1181 static void rgb_prepare_row(VncState *vs, uint8_t *dst, int x, int y,
1184 if (ds_get_bytes_per_pixel(vs->ds) == 4) {
1185 if (vs->ds->surface->pf.rmax == 0xFF &&
1186 vs->ds->surface->pf.gmax == 0xFF &&
1187 vs->ds->surface->pf.bmax == 0xFF) {
1188 rgb_prepare_row24(vs, dst, x, y, count);
1190 rgb_prepare_row32(vs, dst, x, y, count);
1193 rgb_prepare_row16(vs, dst, x, y, count);
1196 #endif /* CONFIG_VNC_JPEG or CONFIG_VNC_PNG */
1199 * JPEG compression stuff.
1201 #ifdef CONFIG_VNC_JPEG
1203 * Destination manager implementation for JPEG library.
1206 /* This is called once per encoding */
1207 static void jpeg_init_destination(j_compress_ptr cinfo)
1209 VncState *vs = cinfo->client_data;
1210 Buffer *buffer = &vs->tight.jpeg;
1212 cinfo->dest->next_output_byte = (JOCTET *)buffer->buffer + buffer->offset;
1213 cinfo->dest->free_in_buffer = (size_t)(buffer->capacity - buffer->offset);
1216 /* This is called when we ran out of buffer (shouldn't happen!) */
1217 static boolean jpeg_empty_output_buffer(j_compress_ptr cinfo)
1219 VncState *vs = cinfo->client_data;
1220 Buffer *buffer = &vs->tight.jpeg;
1222 buffer->offset = buffer->capacity;
1223 buffer_reserve(buffer, 2048);
1224 jpeg_init_destination(cinfo);
1228 /* This is called when we are done processing data */
1229 static void jpeg_term_destination(j_compress_ptr cinfo)
1231 VncState *vs = cinfo->client_data;
1232 Buffer *buffer = &vs->tight.jpeg;
1234 buffer->offset = buffer->capacity - cinfo->dest->free_in_buffer;
1237 static int send_jpeg_rect(VncState *vs, int x, int y, int w, int h, int quality)
1239 struct jpeg_compress_struct cinfo;
1240 struct jpeg_error_mgr jerr;
1241 struct jpeg_destination_mgr manager;
1246 if (ds_get_bytes_per_pixel(vs->ds) == 1)
1247 return send_full_color_rect(vs, x, y, w, h);
1249 buffer_reserve(&vs->tight.jpeg, 2048);
1251 cinfo.err = jpeg_std_error(&jerr);
1252 jpeg_create_compress(&cinfo);
1254 cinfo.client_data = vs;
1255 cinfo.image_width = w;
1256 cinfo.image_height = h;
1257 cinfo.input_components = 3;
1258 cinfo.in_color_space = JCS_RGB;
1260 jpeg_set_defaults(&cinfo);
1261 jpeg_set_quality(&cinfo, quality, true);
1263 manager.init_destination = jpeg_init_destination;
1264 manager.empty_output_buffer = jpeg_empty_output_buffer;
1265 manager.term_destination = jpeg_term_destination;
1266 cinfo.dest = &manager;
1268 jpeg_start_compress(&cinfo, true);
1270 buf = qemu_malloc(w * 3);
1272 for (dy = 0; dy < h; dy++) {
1273 rgb_prepare_row(vs, buf, x, y + dy, w);
1274 jpeg_write_scanlines(&cinfo, row, 1);
1278 jpeg_finish_compress(&cinfo);
1279 jpeg_destroy_compress(&cinfo);
1281 vnc_write_u8(vs, VNC_TIGHT_JPEG << 4);
1283 tight_send_compact_size(vs, vs->tight.jpeg.offset);
1284 vnc_write(vs, vs->tight.jpeg.buffer, vs->tight.jpeg.offset);
1285 buffer_reset(&vs->tight.jpeg);
1289 #endif /* CONFIG_VNC_JPEG */
1292 * PNG compression stuff.
1294 #ifdef CONFIG_VNC_PNG
1295 static void write_png_palette(int idx, uint32_t pix, void *opaque)
1297 struct palette_cb_priv *priv = opaque;
1298 VncState *vs = priv->vs;
1299 png_colorp color = &priv->png_palette[idx];
1301 if (vs->tight.pixel24)
1303 color->red = (pix >> vs->clientds.pf.rshift) & vs->clientds.pf.rmax;
1304 color->green = (pix >> vs->clientds.pf.gshift) & vs->clientds.pf.gmax;
1305 color->blue = (pix >> vs->clientds.pf.bshift) & vs->clientds.pf.bmax;
1309 int red, green, blue;
1311 red = (pix >> vs->clientds.pf.rshift) & vs->clientds.pf.rmax;
1312 green = (pix >> vs->clientds.pf.gshift) & vs->clientds.pf.gmax;
1313 blue = (pix >> vs->clientds.pf.bshift) & vs->clientds.pf.bmax;
1314 color->red = ((red * 255 + vs->clientds.pf.rmax / 2) /
1315 vs->clientds.pf.rmax);
1316 color->green = ((green * 255 + vs->clientds.pf.gmax / 2) /
1317 vs->clientds.pf.gmax);
1318 color->blue = ((blue * 255 + vs->clientds.pf.bmax / 2) /
1319 vs->clientds.pf.bmax);
1323 static void png_write_data(png_structp png_ptr, png_bytep data,
1326 VncState *vs = png_get_io_ptr(png_ptr);
1328 buffer_reserve(&vs->tight.png, vs->tight.png.offset + length);
1329 memcpy(vs->tight.png.buffer + vs->tight.png.offset, data, length);
1331 vs->tight.png.offset += length;
1334 static void png_flush_data(png_structp png_ptr)
1338 static void *vnc_png_malloc(png_structp png_ptr, png_size_t size)
1340 return qemu_malloc(size);
1343 static void vnc_png_free(png_structp png_ptr, png_voidp ptr)
1348 static int send_png_rect(VncState *vs, int x, int y, int w, int h,
1349 VncPalette *palette)
1351 png_byte color_type;
1352 png_structp png_ptr;
1354 png_colorp png_palette = NULL;
1355 int level = tight_png_conf[vs->tight.compression].png_zlib_level;
1356 int filters = tight_png_conf[vs->tight.compression].png_filters;
1360 png_ptr = png_create_write_struct_2(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL,
1361 NULL, vnc_png_malloc, vnc_png_free);
1363 if (png_ptr == NULL)
1366 info_ptr = png_create_info_struct(png_ptr);
1368 if (info_ptr == NULL) {
1369 png_destroy_write_struct(&png_ptr, NULL);
1373 png_set_write_fn(png_ptr, (void *) vs, png_write_data, png_flush_data);
1374 png_set_compression_level(png_ptr, level);
1375 png_set_filter(png_ptr, PNG_FILTER_TYPE_DEFAULT, filters);
1378 color_type = PNG_COLOR_TYPE_PALETTE;
1380 color_type = PNG_COLOR_TYPE_RGB;
1383 png_set_IHDR(png_ptr, info_ptr, w, h,
1384 8, color_type, PNG_INTERLACE_NONE,
1385 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
1387 if (color_type == PNG_COLOR_TYPE_PALETTE) {
1388 struct palette_cb_priv priv;
1390 png_palette = png_malloc(png_ptr, sizeof(*png_palette) *
1391 palette_size(palette));
1394 priv.png_palette = png_palette;
1395 palette_iter(palette, write_png_palette, &priv);
1397 png_set_PLTE(png_ptr, info_ptr, png_palette, palette_size(palette));
1399 if (vs->clientds.pf.bytes_per_pixel == 4) {
1400 tight_encode_indexed_rect32(vs->tight.tight.buffer, w * h, palette);
1402 tight_encode_indexed_rect16(vs->tight.tight.buffer, w * h, palette);
1406 png_write_info(png_ptr, info_ptr);
1408 buffer_reserve(&vs->tight.png, 2048);
1409 buf = qemu_malloc(w * 3);
1410 for (dy = 0; dy < h; dy++)
1412 if (color_type == PNG_COLOR_TYPE_PALETTE) {
1413 memcpy(buf, vs->tight.tight.buffer + (dy * w), w);
1415 rgb_prepare_row(vs, buf, x, y + dy, w);
1417 png_write_row(png_ptr, buf);
1421 png_write_end(png_ptr, NULL);
1423 if (color_type == PNG_COLOR_TYPE_PALETTE) {
1424 png_free(png_ptr, png_palette);
1427 png_destroy_write_struct(&png_ptr, &info_ptr);
1429 vnc_write_u8(vs, VNC_TIGHT_PNG << 4);
1431 tight_send_compact_size(vs, vs->tight.png.offset);
1432 vnc_write(vs, vs->tight.png.buffer, vs->tight.png.offset);
1433 buffer_reset(&vs->tight.png);
1436 #endif /* CONFIG_VNC_PNG */
1438 static void vnc_tight_start(VncState *vs)
1440 buffer_reset(&vs->tight.tight);
1442 // make the output buffer be the zlib buffer, so we can compress it later
1443 vs->tight.tmp = vs->output;
1444 vs->output = vs->tight.tight;
1447 static void vnc_tight_stop(VncState *vs)
1449 // switch back to normal output/zlib buffers
1450 vs->tight.tight = vs->output;
1451 vs->output = vs->tight.tmp;
1454 static int send_sub_rect_nojpeg(VncState *vs, int x, int y, int w, int h,
1455 int bg, int fg, int colors, VncPalette *palette)
1460 if (tight_detect_smooth_image(vs, w, h)) {
1461 ret = send_gradient_rect(vs, x, y, w, h);
1463 ret = send_full_color_rect(vs, x, y, w, h);
1465 } else if (colors == 1) {
1466 ret = send_solid_rect(vs);
1467 } else if (colors == 2) {
1468 ret = send_mono_rect(vs, x, y, w, h, bg, fg);
1469 } else if (colors <= 256) {
1470 ret = send_palette_rect(vs, x, y, w, h, palette);
1477 #ifdef CONFIG_VNC_JPEG
1478 static int send_sub_rect_jpeg(VncState *vs, int x, int y, int w, int h,
1479 int bg, int fg, int colors,
1480 VncPalette *palette)
1485 if (tight_detect_smooth_image(vs, w, h)) {
1486 int quality = tight_conf[vs->tight.quality].jpeg_quality;
1488 ret = send_jpeg_rect(vs, x, y, w, h, quality);
1490 ret = send_full_color_rect(vs, x, y, w, h);
1492 } else if (colors == 1) {
1493 ret = send_solid_rect(vs);
1494 } else if (colors == 2) {
1495 ret = send_mono_rect(vs, x, y, w, h, bg, fg);
1496 } else if (colors <= 256) {
1498 tight_detect_smooth_image(vs, w, h)) {
1499 int quality = tight_conf[vs->tight.quality].jpeg_quality;
1501 ret = send_jpeg_rect(vs, x, y, w, h, quality);
1503 ret = send_palette_rect(vs, x, y, w, h, palette);
1512 static int send_sub_rect(VncState *vs, int x, int y, int w, int h)
1514 VncPalette *palette = NULL;
1515 uint32_t bg = 0, fg = 0;
1519 vnc_framebuffer_update(vs, x, y, w, h, vs->tight.type);
1521 vnc_tight_start(vs);
1522 vnc_raw_send_framebuffer_update(vs, x, y, w, h);
1525 colors = tight_fill_palette(vs, x, y, w * h, &fg, &bg, &palette);
1527 #ifdef CONFIG_VNC_JPEG
1528 if (vs->tight.quality != (uint8_t)-1) {
1529 ret = send_sub_rect_jpeg(vs, x, y, w, h, bg, fg, colors, palette);
1531 ret = send_sub_rect_nojpeg(vs, x, y, w, h, bg, fg, colors, palette);
1534 ret = send_sub_rect_nojpeg(vs, x, y, w, h, bg, fg, colors, palette);
1537 palette_destroy(palette);
1541 static int send_sub_rect_solid(VncState *vs, int x, int y, int w, int h)
1543 vnc_framebuffer_update(vs, x, y, w, h, vs->tight.type);
1545 vnc_tight_start(vs);
1546 vnc_raw_send_framebuffer_update(vs, x, y, w, h);
1549 return send_solid_rect(vs);
1552 static int send_rect_simple(VncState *vs, int x, int y, int w, int h)
1554 int max_size, max_width;
1555 int max_sub_width, max_sub_height;
1560 max_size = tight_conf[vs->tight.compression].max_rect_size;
1561 max_width = tight_conf[vs->tight.compression].max_rect_width;
1563 if (w > max_width || w * h > max_size) {
1564 max_sub_width = (w > max_width) ? max_width : w;
1565 max_sub_height = max_size / max_sub_width;
1567 for (dy = 0; dy < h; dy += max_sub_height) {
1568 for (dx = 0; dx < w; dx += max_width) {
1569 rw = MIN(max_sub_width, w - dx);
1570 rh = MIN(max_sub_height, h - dy);
1571 n += send_sub_rect(vs, x+dx, y+dy, rw, rh);
1575 n += send_sub_rect(vs, x, y, w, h);
1581 static int find_large_solid_color_rect(VncState *vs, int x, int y,
1582 int w, int h, int max_rows)
1587 /* Try to find large solid-color areas and send them separately. */
1589 for (dy = y; dy < y + h; dy += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) {
1591 /* If a rectangle becomes too large, send its upper part now. */
1593 if (dy - y >= max_rows) {
1594 n += send_rect_simple(vs, x, y, w, max_rows);
1599 dh = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, (y + h - dy));
1601 for (dx = x; dx < x + w; dx += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) {
1602 uint32_t color_value;
1603 int x_best, y_best, w_best, h_best;
1605 dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, (x + w - dx));
1607 if (!check_solid_tile(vs, dx, dy, dw, dh, &color_value, false)) {
1611 /* Get dimensions of solid-color area. */
1613 find_best_solid_area(vs, dx, dy, w - (dx - x), h - (dy - y),
1614 color_value, &w_best, &h_best);
1616 /* Make sure a solid rectangle is large enough
1617 (or the whole rectangle is of the same color). */
1619 if (w_best * h_best != w * h &&
1620 w_best * h_best < VNC_TIGHT_MIN_SOLID_SUBRECT_SIZE) {
1624 /* Try to extend solid rectangle to maximum size. */
1626 x_best = dx; y_best = dy;
1627 extend_solid_area(vs, x, y, w, h, color_value,
1628 &x_best, &y_best, &w_best, &h_best);
1630 /* Send rectangles at top and left to solid-color area. */
1633 n += send_rect_simple(vs, x, y, w, y_best-y);
1636 n += tight_send_framebuffer_update(vs, x, y_best,
1640 /* Send solid-color rectangle. */
1641 n += send_sub_rect_solid(vs, x_best, y_best, w_best, h_best);
1643 /* Send remaining rectangles (at right and bottom). */
1645 if (x_best + w_best != x + w) {
1646 n += tight_send_framebuffer_update(vs, x_best+w_best,
1648 w-(x_best-x)-w_best,
1651 if (y_best + h_best != y + h) {
1652 n += tight_send_framebuffer_update(vs, x, y_best+h_best,
1653 w, h-(y_best-y)-h_best);
1656 /* Return after all recursive calls are done. */
1660 return n + send_rect_simple(vs, x, y, w, h);
1663 static int tight_send_framebuffer_update(VncState *vs, int x, int y,
1668 if (vs->clientds.pf.bytes_per_pixel == 4 && vs->clientds.pf.rmax == 0xFF &&
1669 vs->clientds.pf.bmax == 0xFF && vs->clientds.pf.gmax == 0xFF) {
1670 vs->tight.pixel24 = true;
1672 vs->tight.pixel24 = false;
1675 if (w * h < VNC_TIGHT_MIN_SPLIT_RECT_SIZE)
1676 return send_rect_simple(vs, x, y, w, h);
1678 /* Calculate maximum number of rows in one non-solid rectangle. */
1680 max_rows = tight_conf[vs->tight.compression].max_rect_size;
1681 max_rows /= MIN(tight_conf[vs->tight.compression].max_rect_width, w);
1683 return find_large_solid_color_rect(vs, x, y, w, h, max_rows);
1686 int vnc_tight_send_framebuffer_update(VncState *vs, int x, int y,
1689 vs->tight.type = VNC_ENCODING_TIGHT;
1690 return tight_send_framebuffer_update(vs, x, y, w, h);
1693 int vnc_tight_png_send_framebuffer_update(VncState *vs, int x, int y,
1696 vs->tight.type = VNC_ENCODING_TIGHT_PNG;
1697 return tight_send_framebuffer_update(vs, x, y, w, h);
1700 void vnc_tight_clear(VncState *vs)
1703 for (i=0; i<ARRAY_SIZE(vs->tight.stream); i++) {
1704 if (vs->tight.stream[i].opaque) {
1705 deflateEnd(&vs->tight.stream[i]);
1709 buffer_free(&vs->tight.tight);
1710 buffer_free(&vs->tight.zlib);
1711 buffer_free(&vs->tight.gradient);
1712 #ifdef CONFIG_VNC_JPEG
1713 buffer_free(&vs->tight.jpeg);
1715 #ifdef CONFIG_VNC_PNG
1716 buffer_free(&vs->tight.png);