]> git.proxmox.com Git - qemu.git/blame - ui/vnc-enc-tight.c
Fix MinGW compilation when --enable-vnc-jpeg is specified
[qemu.git] / ui / vnc-enc-tight.c
CommitLineData
380282b0
CC
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
efe556ad 29#include "config-host.h"
2f6f5c7a 30
f26e428d
RT
31/* This needs to be before jpeglib.h line because of conflict with
32 INT32 definitions between jmorecfg.h (included by jpeglib.h) and
33 Win32 basetsd.h (included by windows.h). */
34#include "qemu-common.h"
35
efe556ad
CC
36#ifdef CONFIG_VNC_PNG
37#include <png.h>
38#endif
2f6f5c7a
CC
39#ifdef CONFIG_VNC_JPEG
40#include <stdio.h>
41#include <jpeglib.h>
42#endif
43
44#include "bswap.h"
aa7d73fd 45#include "qint.h"
380282b0 46#include "vnc.h"
245f7b51 47#include "vnc-enc-tight.h"
5136a052 48#include "vnc-palette.h"
380282b0
CC
49
50/* Compression level stuff. The following array contains various
51 encoder parameters for each of 10 compression levels (0..9).
52 Last three parameters correspond to JPEG quality levels (0..9). */
53
54static const struct {
55 int max_rect_size, max_rect_width;
56 int mono_min_rect_size, gradient_min_rect_size;
57 int idx_zlib_level, mono_zlib_level, raw_zlib_level, gradient_zlib_level;
58 int gradient_threshold, gradient_threshold24;
59 int idx_max_colors_divisor;
60 int jpeg_quality, jpeg_threshold, jpeg_threshold24;
61} tight_conf[] = {
62 { 512, 32, 6, 65536, 0, 0, 0, 0, 0, 0, 4, 5, 10000, 23000 },
63 { 2048, 128, 6, 65536, 1, 1, 1, 0, 0, 0, 8, 10, 8000, 18000 },
64 { 6144, 256, 8, 65536, 3, 3, 2, 0, 0, 0, 24, 15, 6500, 15000 },
65 { 10240, 1024, 12, 65536, 5, 5, 3, 0, 0, 0, 32, 25, 5000, 12000 },
66 { 16384, 2048, 12, 65536, 6, 6, 4, 0, 0, 0, 32, 37, 4000, 10000 },
67 { 32768, 2048, 12, 4096, 7, 7, 5, 4, 150, 380, 32, 50, 3000, 8000 },
68 { 65536, 2048, 16, 4096, 7, 7, 6, 4, 170, 420, 48, 60, 2000, 5000 },
69 { 65536, 2048, 16, 4096, 8, 8, 7, 5, 180, 450, 64, 70, 1000, 2500 },
70 { 65536, 2048, 32, 8192, 9, 9, 8, 6, 190, 475, 64, 75, 500, 1200 },
71 { 65536, 2048, 32, 8192, 9, 9, 9, 6, 200, 500, 96, 80, 200, 500 }
72};
73
efe556ad
CC
74
75static int tight_send_framebuffer_update(VncState *vs, int x, int y,
76 int w, int h);
77
ce702e93
CC
78#ifdef CONFIG_VNC_JPEG
79static const struct {
80 double jpeg_freq_min; /* Don't send JPEG if the freq is bellow */
81 double jpeg_freq_threshold; /* Always send JPEG if the freq is above */
82 int jpeg_idx; /* Allow indexed JPEG */
83 int jpeg_full; /* Allow full color JPEG */
84} tight_jpeg_conf[] = {
8cb4a6b7
CC
85 { 0, 8, 1, 1 },
86 { 0, 8, 1, 1 },
87 { 0, 8, 1, 1 },
88 { 0, 8, 1, 1 },
89 { 0, 10, 1, 1 },
90 { 0.1, 10, 1, 1 },
91 { 0.2, 10, 1, 1 },
92 { 0.3, 12, 0, 0 },
93 { 0.4, 14, 0, 0 },
94 { 0.5, 16, 0, 0 },
ce702e93
CC
95};
96#endif
97
efe556ad 98#ifdef CONFIG_VNC_PNG
3941bf6f
CC
99static const struct {
100 int png_zlib_level, png_filters;
101} tight_png_conf[] = {
102 { 0, PNG_NO_FILTERS },
103 { 1, PNG_NO_FILTERS },
104 { 2, PNG_NO_FILTERS },
105 { 3, PNG_NO_FILTERS },
106 { 4, PNG_NO_FILTERS },
107 { 5, PNG_ALL_FILTERS },
108 { 6, PNG_ALL_FILTERS },
109 { 7, PNG_ALL_FILTERS },
110 { 8, PNG_ALL_FILTERS },
111 { 9, PNG_ALL_FILTERS },
112};
113
efe556ad 114static int send_png_rect(VncState *vs, int x, int y, int w, int h,
5136a052 115 VncPalette *palette);
efe556ad
CC
116
117static bool tight_can_send_png_rect(VncState *vs, int w, int h)
118{
d1af0e05 119 if (vs->tight.type != VNC_ENCODING_TIGHT_PNG) {
efe556ad
CC
120 return false;
121 }
122
123 if (ds_get_bytes_per_pixel(vs->ds) == 1 ||
124 vs->clientds.pf.bytes_per_pixel == 1) {
125 return false;
126 }
127
128 return true;
129}
130#endif
131
2f6f5c7a
CC
132/*
133 * Code to guess if given rectangle is suitable for smooth image
134 * compression (by applying "gradient" filter or JPEG coder).
135 */
136
249cdb42 137static unsigned int
2f6f5c7a
CC
138tight_detect_smooth_image24(VncState *vs, int w, int h)
139{
140 int off;
141 int x, y, d, dx;
249cdb42
BS
142 unsigned int c;
143 unsigned int stats[256];
2f6f5c7a
CC
144 int pixels = 0;
145 int pix, left[3];
249cdb42 146 unsigned int errors;
d1af0e05 147 unsigned char *buf = vs->tight.tight.buffer;
2f6f5c7a
CC
148
149 /*
150 * If client is big-endian, color samples begin from the second
151 * byte (offset 1) of a 32-bit pixel value.
152 */
153 off = !!(vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG);
154
155 memset(stats, 0, sizeof (stats));
156
157 for (y = 0, x = 0; y < h && x < w;) {
158 for (d = 0; d < h - y && d < w - x - VNC_TIGHT_DETECT_SUBROW_WIDTH;
159 d++) {
160 for (c = 0; c < 3; c++) {
161 left[c] = buf[((y+d)*w+x+d)*4+off+c] & 0xFF;
162 }
163 for (dx = 1; dx <= VNC_TIGHT_DETECT_SUBROW_WIDTH; dx++) {
164 for (c = 0; c < 3; c++) {
165 pix = buf[((y+d)*w+x+d+dx)*4+off+c] & 0xFF;
166 stats[abs(pix - left[c])]++;
167 left[c] = pix;
168 }
169 pixels++;
170 }
171 }
172 if (w > h) {
173 x += h;
174 y = 0;
175 } else {
176 x = 0;
177 y += w;
178 }
179 }
180
181 /* 95% smooth or more ... */
182 if (stats[0] * 33 / pixels >= 95) {
183 return 0;
184 }
185
186 errors = 0;
187 for (c = 1; c < 8; c++) {
188 errors += stats[c] * (c * c);
189 if (stats[c] == 0 || stats[c] > stats[c-1] * 2) {
190 return 0;
191 }
192 }
193 for (; c < 256; c++) {
194 errors += stats[c] * (c * c);
195 }
196 errors /= (pixels * 3 - stats[0]);
197
198 return errors;
199}
200
201#define DEFINE_DETECT_FUNCTION(bpp) \
202 \
249cdb42 203 static unsigned int \
2f6f5c7a
CC
204 tight_detect_smooth_image##bpp(VncState *vs, int w, int h) { \
205 bool endian; \
206 uint##bpp##_t pix; \
207 int max[3], shift[3]; \
208 int x, y, d, dx; \
249cdb42
BS
209 unsigned int c; \
210 unsigned int stats[256]; \
2f6f5c7a
CC
211 int pixels = 0; \
212 int sample, sum, left[3]; \
249cdb42 213 unsigned int errors; \
d1af0e05 214 unsigned char *buf = vs->tight.tight.buffer; \
2f6f5c7a
CC
215 \
216 endian = ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) != \
217 (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG)); \
218 \
219 \
220 max[0] = vs->clientds.pf.rmax; \
221 max[1] = vs->clientds.pf.gmax; \
222 max[2] = vs->clientds.pf.bmax; \
223 shift[0] = vs->clientds.pf.rshift; \
224 shift[1] = vs->clientds.pf.gshift; \
225 shift[2] = vs->clientds.pf.bshift; \
226 \
227 memset(stats, 0, sizeof(stats)); \
228 \
229 y = 0, x = 0; \
230 while (y < h && x < w) { \
231 for (d = 0; d < h - y && \
232 d < w - x - VNC_TIGHT_DETECT_SUBROW_WIDTH; d++) { \
233 pix = ((uint##bpp##_t *)buf)[(y+d)*w+x+d]; \
234 if (endian) { \
ba5e7f82 235 pix = bswap##bpp(pix); \
2f6f5c7a
CC
236 } \
237 for (c = 0; c < 3; c++) { \
238 left[c] = (int)(pix >> shift[c] & max[c]); \
239 } \
240 for (dx = 1; dx <= VNC_TIGHT_DETECT_SUBROW_WIDTH; \
241 dx++) { \
242 pix = ((uint##bpp##_t *)buf)[(y+d)*w+x+d+dx]; \
243 if (endian) { \
ba5e7f82 244 pix = bswap##bpp(pix); \
2f6f5c7a
CC
245 } \
246 sum = 0; \
247 for (c = 0; c < 3; c++) { \
248 sample = (int)(pix >> shift[c] & max[c]); \
249 sum += abs(sample - left[c]); \
250 left[c] = sample; \
251 } \
252 if (sum > 255) { \
253 sum = 255; \
254 } \
255 stats[sum]++; \
256 pixels++; \
257 } \
258 } \
259 if (w > h) { \
260 x += h; \
261 y = 0; \
262 } else { \
263 x = 0; \
264 y += w; \
265 } \
266 } \
267 \
268 if ((stats[0] + stats[1]) * 100 / pixels >= 90) { \
269 return 0; \
270 } \
271 \
272 errors = 0; \
273 for (c = 1; c < 8; c++) { \
274 errors += stats[c] * (c * c); \
275 if (stats[c] == 0 || stats[c] > stats[c-1] * 2) { \
276 return 0; \
277 } \
278 } \
279 for (; c < 256; c++) { \
280 errors += stats[c] * (c * c); \
281 } \
282 errors /= (pixels - stats[0]); \
283 \
284 return errors; \
285 }
286
287DEFINE_DETECT_FUNCTION(16)
288DEFINE_DETECT_FUNCTION(32)
289
290static int
291tight_detect_smooth_image(VncState *vs, int w, int h)
292{
249cdb42 293 unsigned int errors;
d1af0e05
CC
294 int compression = vs->tight.compression;
295 int quality = vs->tight.quality;
2f6f5c7a 296
6f9c78c1
CC
297 if (!vs->vd->lossy) {
298 return 0;
299 }
300
2f6f5c7a
CC
301 if (ds_get_bytes_per_pixel(vs->ds) == 1 ||
302 vs->clientds.pf.bytes_per_pixel == 1 ||
303 w < VNC_TIGHT_DETECT_MIN_WIDTH || h < VNC_TIGHT_DETECT_MIN_HEIGHT) {
304 return 0;
305 }
306
7bccf573 307 if (vs->tight.quality != (uint8_t)-1) {
2f6f5c7a
CC
308 if (w * h < VNC_TIGHT_JPEG_MIN_RECT_SIZE) {
309 return 0;
310 }
311 } else {
312 if (w * h < tight_conf[compression].gradient_min_rect_size) {
313 return 0;
314 }
315 }
316
317 if (vs->clientds.pf.bytes_per_pixel == 4) {
d1af0e05 318 if (vs->tight.pixel24) {
2f6f5c7a 319 errors = tight_detect_smooth_image24(vs, w, h);
7bccf573 320 if (vs->tight.quality != (uint8_t)-1) {
2f6f5c7a
CC
321 return (errors < tight_conf[quality].jpeg_threshold24);
322 }
323 return (errors < tight_conf[compression].gradient_threshold24);
324 } else {
325 errors = tight_detect_smooth_image32(vs, w, h);
326 }
327 } else {
328 errors = tight_detect_smooth_image16(vs, w, h);
329 }
330 if (quality != -1) {
331 return (errors < tight_conf[quality].jpeg_threshold);
332 }
333 return (errors < tight_conf[compression].gradient_threshold);
334}
335
aa7d73fd
CC
336/*
337 * Code to determine how many different colors used in rectangle.
338 */
aa7d73fd
CC
339#define DEFINE_FILL_PALETTE_FUNCTION(bpp) \
340 \
341 static int \
342 tight_fill_palette##bpp(VncState *vs, int x, int y, \
343 int max, size_t count, \
344 uint32_t *bg, uint32_t *fg, \
5136a052 345 VncPalette **palette) { \
aa7d73fd
CC
346 uint##bpp##_t *data; \
347 uint##bpp##_t c0, c1, ci; \
348 int i, n0, n1; \
349 \
d1af0e05 350 data = (uint##bpp##_t *)vs->tight.tight.buffer; \
aa7d73fd
CC
351 \
352 c0 = data[0]; \
353 i = 1; \
354 while (i < count && data[i] == c0) \
355 i++; \
356 if (i >= count) { \
357 *bg = *fg = c0; \
358 return 1; \
359 } \
360 \
361 if (max < 2) { \
362 return 0; \
363 } \
364 \
365 n0 = i; \
366 c1 = data[i]; \
367 n1 = 0; \
368 for (i++; i < count; i++) { \
369 ci = data[i]; \
370 if (ci == c0) { \
371 n0++; \
372 } else if (ci == c1) { \
373 n1++; \
374 } else \
375 break; \
376 } \
377 if (i >= count) { \
378 if (n0 > n1) { \
379 *bg = (uint32_t)c0; \
380 *fg = (uint32_t)c1; \
381 } else { \
382 *bg = (uint32_t)c1; \
383 *fg = (uint32_t)c0; \
384 } \
385 return 2; \
386 } \
387 \
388 if (max == 2) { \
389 return 0; \
390 } \
391 \
5136a052
CC
392 *palette = palette_new(max, bpp); \
393 palette_put(*palette, c0); \
394 palette_put(*palette, c1); \
395 palette_put(*palette, ci); \
aa7d73fd
CC
396 \
397 for (i++; i < count; i++) { \
398 if (data[i] == ci) { \
399 continue; \
400 } else { \
5d8efe39 401 ci = data[i]; \
5136a052 402 if (!palette_put(*palette, (uint32_t)ci)) { \
aa7d73fd
CC
403 return 0; \
404 } \
aa7d73fd
CC
405 } \
406 } \
407 \
5136a052 408 return palette_size(*palette); \
aa7d73fd
CC
409 }
410
411DEFINE_FILL_PALETTE_FUNCTION(8)
412DEFINE_FILL_PALETTE_FUNCTION(16)
413DEFINE_FILL_PALETTE_FUNCTION(32)
414
415static int tight_fill_palette(VncState *vs, int x, int y,
416 size_t count, uint32_t *bg, uint32_t *fg,
5136a052 417 VncPalette **palette)
aa7d73fd
CC
418{
419 int max;
420
d1af0e05 421 max = count / tight_conf[vs->tight.compression].idx_max_colors_divisor;
aa7d73fd 422 if (max < 2 &&
d1af0e05 423 count >= tight_conf[vs->tight.compression].mono_min_rect_size) {
aa7d73fd
CC
424 max = 2;
425 }
426 if (max >= 256) {
427 max = 256;
428 }
429
430 switch(vs->clientds.pf.bytes_per_pixel) {
431 case 4:
432 return tight_fill_palette32(vs, x, y, max, count, bg, fg, palette);
433 case 2:
434 return tight_fill_palette16(vs, x, y, max, count, bg, fg, palette);
435 default:
436 max = 2;
437 return tight_fill_palette8(vs, x, y, max, count, bg, fg, palette);
438 }
439 return 0;
440}
441
aa7d73fd
CC
442/*
443 * Converting truecolor samples into palette indices.
444 */
445#define DEFINE_IDX_ENCODE_FUNCTION(bpp) \
446 \
447 static void \
448 tight_encode_indexed_rect##bpp(uint8_t *buf, int count, \
5136a052 449 VncPalette *palette) { \
aa7d73fd
CC
450 uint##bpp##_t *src; \
451 uint##bpp##_t rgb; \
54d43eac 452 int i, rep; \
aa7d73fd
CC
453 uint8_t idx; \
454 \
455 src = (uint##bpp##_t *) buf; \
456 \
54d43eac 457 for (i = 0; i < count; i++) { \
efe556ad 458 \
aa7d73fd
CC
459 rgb = *src++; \
460 rep = 0; \
54d43eac
CC
461 while (i < count && *src == rgb) { \
462 rep++, src++, i++; \
aa7d73fd 463 } \
5136a052
CC
464 idx = palette_idx(palette, rgb); \
465 /* \
466 * Should never happen, but don't break everything \
467 * if it does, use the first color instead \
468 */ \
7bccf573 469 if (idx == (uint8_t)-1) { \
aa7d73fd 470 idx = 0; \
aa7d73fd
CC
471 } \
472 while (rep >= 0) { \
473 *buf++ = idx; \
474 rep--; \
475 } \
476 } \
477 }
478
479DEFINE_IDX_ENCODE_FUNCTION(16)
480DEFINE_IDX_ENCODE_FUNCTION(32)
481
482#define DEFINE_MONO_ENCODE_FUNCTION(bpp) \
483 \
484 static void \
485 tight_encode_mono_rect##bpp(uint8_t *buf, int w, int h, \
486 uint##bpp##_t bg, uint##bpp##_t fg) { \
487 uint##bpp##_t *ptr; \
488 unsigned int value, mask; \
489 int aligned_width; \
490 int x, y, bg_bits; \
491 \
492 ptr = (uint##bpp##_t *) buf; \
493 aligned_width = w - w % 8; \
494 \
495 for (y = 0; y < h; y++) { \
496 for (x = 0; x < aligned_width; x += 8) { \
497 for (bg_bits = 0; bg_bits < 8; bg_bits++) { \
498 if (*ptr++ != bg) { \
499 break; \
500 } \
501 } \
502 if (bg_bits == 8) { \
503 *buf++ = 0; \
504 continue; \
505 } \
506 mask = 0x80 >> bg_bits; \
507 value = mask; \
508 for (bg_bits++; bg_bits < 8; bg_bits++) { \
509 mask >>= 1; \
510 if (*ptr++ != bg) { \
511 value |= mask; \
512 } \
513 } \
514 *buf++ = (uint8_t)value; \
515 } \
516 \
517 mask = 0x80; \
518 value = 0; \
519 if (x >= w) { \
520 continue; \
521 } \
522 \
523 for (; x < w; x++) { \
524 if (*ptr++ != bg) { \
525 value |= mask; \
526 } \
527 mask >>= 1; \
528 } \
529 *buf++ = (uint8_t)value; \
530 } \
531 }
532
533DEFINE_MONO_ENCODE_FUNCTION(8)
534DEFINE_MONO_ENCODE_FUNCTION(16)
535DEFINE_MONO_ENCODE_FUNCTION(32)
536
2f6f5c7a
CC
537/*
538 * ``Gradient'' filter for 24-bit color samples.
539 * Should be called only when redMax, greenMax and blueMax are 255.
540 * Color components assumed to be byte-aligned.
541 */
542
543static void
544tight_filter_gradient24(VncState *vs, uint8_t *buf, int w, int h)
545{
546 uint32_t *buf32;
547 uint32_t pix32;
548 int shift[3];
549 int *prev;
550 int here[3], upper[3], left[3], upperleft[3];
551 int prediction;
552 int x, y, c;
553
554 buf32 = (uint32_t *)buf;
d1af0e05 555 memset(vs->tight.gradient.buffer, 0, w * 3 * sizeof(int));
2f6f5c7a
CC
556
557 if ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) ==
558 (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG)) {
559 shift[0] = vs->clientds.pf.rshift;
560 shift[1] = vs->clientds.pf.gshift;
561 shift[2] = vs->clientds.pf.bshift;
562 } else {
563 shift[0] = 24 - vs->clientds.pf.rshift;
564 shift[1] = 24 - vs->clientds.pf.gshift;
565 shift[2] = 24 - vs->clientds.pf.bshift;
566 }
567
568 for (y = 0; y < h; y++) {
569 for (c = 0; c < 3; c++) {
570 upper[c] = 0;
571 here[c] = 0;
572 }
d1af0e05 573 prev = (int *)vs->tight.gradient.buffer;
2f6f5c7a
CC
574 for (x = 0; x < w; x++) {
575 pix32 = *buf32++;
576 for (c = 0; c < 3; c++) {
577 upperleft[c] = upper[c];
578 left[c] = here[c];
579 upper[c] = *prev;
580 here[c] = (int)(pix32 >> shift[c] & 0xFF);
581 *prev++ = here[c];
582
583 prediction = left[c] + upper[c] - upperleft[c];
584 if (prediction < 0) {
585 prediction = 0;
586 } else if (prediction > 0xFF) {
587 prediction = 0xFF;
588 }
589 *buf++ = (char)(here[c] - prediction);
590 }
591 }
592 }
593}
594
595
596/*
597 * ``Gradient'' filter for other color depths.
598 */
599
600#define DEFINE_GRADIENT_FILTER_FUNCTION(bpp) \
601 \
602 static void \
603 tight_filter_gradient##bpp(VncState *vs, uint##bpp##_t *buf, \
604 int w, int h) { \
605 uint##bpp##_t pix, diff; \
606 bool endian; \
607 int *prev; \
608 int max[3], shift[3]; \
609 int here[3], upper[3], left[3], upperleft[3]; \
610 int prediction; \
611 int x, y, c; \
612 \
d1af0e05 613 memset (vs->tight.gradient.buffer, 0, w * 3 * sizeof(int)); \
2f6f5c7a
CC
614 \
615 endian = ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) != \
616 (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG)); \
617 \
618 max[0] = vs->clientds.pf.rmax; \
619 max[1] = vs->clientds.pf.gmax; \
620 max[2] = vs->clientds.pf.bmax; \
621 shift[0] = vs->clientds.pf.rshift; \
622 shift[1] = vs->clientds.pf.gshift; \
623 shift[2] = vs->clientds.pf.bshift; \
624 \
625 for (y = 0; y < h; y++) { \
626 for (c = 0; c < 3; c++) { \
627 upper[c] = 0; \
628 here[c] = 0; \
629 } \
d1af0e05 630 prev = (int *)vs->tight.gradient.buffer; \
2f6f5c7a
CC
631 for (x = 0; x < w; x++) { \
632 pix = *buf; \
633 if (endian) { \
ba5e7f82 634 pix = bswap##bpp(pix); \
2f6f5c7a
CC
635 } \
636 diff = 0; \
637 for (c = 0; c < 3; c++) { \
638 upperleft[c] = upper[c]; \
639 left[c] = here[c]; \
640 upper[c] = *prev; \
641 here[c] = (int)(pix >> shift[c] & max[c]); \
642 *prev++ = here[c]; \
643 \
644 prediction = left[c] + upper[c] - upperleft[c]; \
645 if (prediction < 0) { \
646 prediction = 0; \
647 } else if (prediction > max[c]) { \
648 prediction = max[c]; \
649 } \
650 diff |= ((here[c] - prediction) & max[c]) \
651 << shift[c]; \
652 } \
653 if (endian) { \
ba5e7f82 654 diff = bswap##bpp(diff); \
2f6f5c7a
CC
655 } \
656 *buf++ = diff; \
657 } \
658 } \
659 }
660
661DEFINE_GRADIENT_FILTER_FUNCTION(16)
662DEFINE_GRADIENT_FILTER_FUNCTION(32)
663
b4bea3f2
CC
664/*
665 * Check if a rectangle is all of the same color. If needSameColor is
666 * set to non-zero, then also check that its color equals to the
b0cd712c 667 * *colorPtr value. The result is 1 if the test is successful, and in
b4bea3f2
CC
668 * that case new color will be stored in *colorPtr.
669 */
670
671#define DEFINE_CHECK_SOLID_FUNCTION(bpp) \
672 \
673 static bool \
674 check_solid_tile##bpp(VncState *vs, int x, int y, int w, int h, \
675 uint32_t* color, bool samecolor) \
676 { \
677 VncDisplay *vd = vs->vd; \
678 uint##bpp##_t *fbptr; \
679 uint##bpp##_t c; \
680 int dx, dy; \
681 \
682 fbptr = (uint##bpp##_t *) \
683 (vd->server->data + y * ds_get_linesize(vs->ds) + \
684 x * ds_get_bytes_per_pixel(vs->ds)); \
685 \
686 c = *fbptr; \
687 if (samecolor && (uint32_t)c != *color) { \
688 return false; \
689 } \
690 \
691 for (dy = 0; dy < h; dy++) { \
692 for (dx = 0; dx < w; dx++) { \
693 if (c != fbptr[dx]) { \
694 return false; \
695 } \
696 } \
697 fbptr = (uint##bpp##_t *) \
698 ((uint8_t *)fbptr + ds_get_linesize(vs->ds)); \
699 } \
700 \
701 *color = (uint32_t)c; \
702 return true; \
703 }
704
705DEFINE_CHECK_SOLID_FUNCTION(32)
706DEFINE_CHECK_SOLID_FUNCTION(16)
707DEFINE_CHECK_SOLID_FUNCTION(8)
708
709static bool check_solid_tile(VncState *vs, int x, int y, int w, int h,
710 uint32_t* color, bool samecolor)
711{
712 VncDisplay *vd = vs->vd;
713
714 switch(vd->server->pf.bytes_per_pixel) {
715 case 4:
716 return check_solid_tile32(vs, x, y, w, h, color, samecolor);
717 case 2:
718 return check_solid_tile16(vs, x, y, w, h, color, samecolor);
719 default:
720 return check_solid_tile8(vs, x, y, w, h, color, samecolor);
721 }
722}
723
724static void find_best_solid_area(VncState *vs, int x, int y, int w, int h,
725 uint32_t color, int *w_ptr, int *h_ptr)
726{
727 int dx, dy, dw, dh;
728 int w_prev;
729 int w_best = 0, h_best = 0;
730
731 w_prev = w;
732
733 for (dy = y; dy < y + h; dy += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) {
734
735 dh = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, y + h - dy);
736 dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, w_prev);
737
738 if (!check_solid_tile(vs, x, dy, dw, dh, &color, true)) {
739 break;
740 }
741
742 for (dx = x + dw; dx < x + w_prev;) {
743 dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, x + w_prev - dx);
744
745 if (!check_solid_tile(vs, dx, dy, dw, dh, &color, true)) {
746 break;
747 }
748 dx += dw;
749 }
750
751 w_prev = dx - x;
752 if (w_prev * (dy + dh - y) > w_best * h_best) {
753 w_best = w_prev;
754 h_best = dy + dh - y;
755 }
756 }
757
758 *w_ptr = w_best;
759 *h_ptr = h_best;
760}
761
762static void extend_solid_area(VncState *vs, int x, int y, int w, int h,
763 uint32_t color, int *x_ptr, int *y_ptr,
764 int *w_ptr, int *h_ptr)
765{
766 int cx, cy;
767
768 /* Try to extend the area upwards. */
769 for ( cy = *y_ptr - 1;
770 cy >= y && check_solid_tile(vs, *x_ptr, cy, *w_ptr, 1, &color, true);
771 cy-- );
772 *h_ptr += *y_ptr - (cy + 1);
773 *y_ptr = cy + 1;
774
775 /* ... downwards. */
776 for ( cy = *y_ptr + *h_ptr;
777 cy < y + h &&
778 check_solid_tile(vs, *x_ptr, cy, *w_ptr, 1, &color, true);
779 cy++ );
780 *h_ptr += cy - (*y_ptr + *h_ptr);
781
782 /* ... to the left. */
783 for ( cx = *x_ptr - 1;
784 cx >= x && check_solid_tile(vs, cx, *y_ptr, 1, *h_ptr, &color, true);
785 cx-- );
786 *w_ptr += *x_ptr - (cx + 1);
787 *x_ptr = cx + 1;
788
789 /* ... to the right. */
790 for ( cx = *x_ptr + *w_ptr;
791 cx < x + w &&
792 check_solid_tile(vs, cx, *y_ptr, 1, *h_ptr, &color, true);
793 cx++ );
794 *w_ptr += cx - (*x_ptr + *w_ptr);
795}
796
380282b0
CC
797static int tight_init_stream(VncState *vs, int stream_id,
798 int level, int strategy)
799{
d1af0e05 800 z_streamp zstream = &vs->tight.stream[stream_id];
380282b0
CC
801
802 if (zstream->opaque == NULL) {
803 int err;
804
805 VNC_DEBUG("VNC: TIGHT: initializing zlib stream %d\n", stream_id);
806 VNC_DEBUG("VNC: TIGHT: opaque = %p | vs = %p\n", zstream->opaque, vs);
807 zstream->zalloc = vnc_zlib_zalloc;
808 zstream->zfree = vnc_zlib_zfree;
809
810 err = deflateInit2(zstream, level, Z_DEFLATED, MAX_WBITS,
811 MAX_MEM_LEVEL, strategy);
812
813 if (err != Z_OK) {
814 fprintf(stderr, "VNC: error initializing zlib\n");
815 return -1;
816 }
817
d1af0e05 818 vs->tight.levels[stream_id] = level;
380282b0
CC
819 zstream->opaque = vs;
820 }
821
d1af0e05 822 if (vs->tight.levels[stream_id] != level) {
380282b0
CC
823 if (deflateParams(zstream, level, strategy) != Z_OK) {
824 return -1;
825 }
d1af0e05 826 vs->tight.levels[stream_id] = level;
380282b0
CC
827 }
828 return 0;
829}
830
831static void tight_send_compact_size(VncState *vs, size_t len)
832{
833 int lpc = 0;
834 int bytes = 0;
835 char buf[3] = {0, 0, 0};
836
837 buf[bytes++] = len & 0x7F;
838 if (len > 0x7F) {
839 buf[bytes-1] |= 0x80;
840 buf[bytes++] = (len >> 7) & 0x7F;
841 if (len > 0x3FFF) {
842 buf[bytes-1] |= 0x80;
843 buf[bytes++] = (len >> 14) & 0xFF;
844 }
845 }
b4bea3f2 846 for (lpc = 0; lpc < bytes; lpc++) {
380282b0
CC
847 vnc_write_u8(vs, buf[lpc]);
848 }
849}
850
851static int tight_compress_data(VncState *vs, int stream_id, size_t bytes,
852 int level, int strategy)
853{
d1af0e05 854 z_streamp zstream = &vs->tight.stream[stream_id];
380282b0
CC
855 int previous_out;
856
857 if (bytes < VNC_TIGHT_MIN_TO_COMPRESS) {
d1af0e05 858 vnc_write(vs, vs->tight.tight.buffer, vs->tight.tight.offset);
380282b0
CC
859 return bytes;
860 }
861
862 if (tight_init_stream(vs, stream_id, level, strategy)) {
863 return -1;
864 }
865
866 /* reserve memory in output buffer */
d1af0e05 867 buffer_reserve(&vs->tight.zlib, bytes + 64);
380282b0
CC
868
869 /* set pointers */
d1af0e05
CC
870 zstream->next_in = vs->tight.tight.buffer;
871 zstream->avail_in = vs->tight.tight.offset;
872 zstream->next_out = vs->tight.zlib.buffer + vs->tight.zlib.offset;
873 zstream->avail_out = vs->tight.zlib.capacity - vs->tight.zlib.offset;
2caa9e9d 874 previous_out = zstream->avail_out;
380282b0 875 zstream->data_type = Z_BINARY;
380282b0
CC
876
877 /* start encoding */
878 if (deflate(zstream, Z_SYNC_FLUSH) != Z_OK) {
879 fprintf(stderr, "VNC: error during tight compression\n");
880 return -1;
881 }
882
d1af0e05 883 vs->tight.zlib.offset = vs->tight.zlib.capacity - zstream->avail_out;
2caa9e9d
MT
884 /* ...how much data has actually been produced by deflate() */
885 bytes = previous_out - zstream->avail_out;
380282b0
CC
886
887 tight_send_compact_size(vs, bytes);
d1af0e05 888 vnc_write(vs, vs->tight.zlib.buffer, bytes);
380282b0 889
d1af0e05 890 buffer_reset(&vs->tight.zlib);
380282b0
CC
891
892 return bytes;
893}
894
895/*
896 * Subencoding implementations.
897 */
aa7d73fd 898static void tight_pack24(VncState *vs, uint8_t *buf, size_t count, size_t *ret)
380282b0 899{
380282b0
CC
900 uint32_t *buf32;
901 uint32_t pix;
902 int rshift, gshift, bshift;
903
380282b0
CC
904 buf32 = (uint32_t *)buf;
905
906 if ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) ==
907 (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG)) {
908 rshift = vs->clientds.pf.rshift;
909 gshift = vs->clientds.pf.gshift;
910 bshift = vs->clientds.pf.bshift;
911 } else {
912 rshift = 24 - vs->clientds.pf.rshift;
913 gshift = 24 - vs->clientds.pf.gshift;
914 bshift = 24 - vs->clientds.pf.bshift;
915 }
916
aa7d73fd
CC
917 if (ret) {
918 *ret = count * 3;
919 }
380282b0
CC
920
921 while (count--) {
922 pix = *buf32++;
923 *buf++ = (char)(pix >> rshift);
924 *buf++ = (char)(pix >> gshift);
925 *buf++ = (char)(pix >> bshift);
926 }
927}
928
efe556ad 929static int send_full_color_rect(VncState *vs, int x, int y, int w, int h)
380282b0
CC
930{
931 int stream = 0;
2116eff9 932 ssize_t bytes;
380282b0 933
efe556ad
CC
934#ifdef CONFIG_VNC_PNG
935 if (tight_can_send_png_rect(vs, w, h)) {
936 return send_png_rect(vs, x, y, w, h, NULL);
937 }
938#endif
939
380282b0
CC
940 vnc_write_u8(vs, stream << 4); /* no flushing, no filter */
941
d1af0e05
CC
942 if (vs->tight.pixel24) {
943 tight_pack24(vs, vs->tight.tight.buffer, w * h, &vs->tight.tight.offset);
380282b0
CC
944 bytes = 3;
945 } else {
946 bytes = vs->clientds.pf.bytes_per_pixel;
947 }
948
949 bytes = tight_compress_data(vs, stream, w * h * bytes,
d1af0e05 950 tight_conf[vs->tight.compression].raw_zlib_level,
380282b0
CC
951 Z_DEFAULT_STRATEGY);
952
953 return (bytes >= 0);
954}
955
b4bea3f2
CC
956static int send_solid_rect(VncState *vs)
957{
958 size_t bytes;
959
960 vnc_write_u8(vs, VNC_TIGHT_FILL << 4); /* no flushing, no filter */
961
d1af0e05
CC
962 if (vs->tight.pixel24) {
963 tight_pack24(vs, vs->tight.tight.buffer, 1, &vs->tight.tight.offset);
b4bea3f2
CC
964 bytes = 3;
965 } else {
966 bytes = vs->clientds.pf.bytes_per_pixel;
967 }
968
d1af0e05 969 vnc_write(vs, vs->tight.tight.buffer, bytes);
b4bea3f2
CC
970 return 1;
971}
972
efe556ad
CC
973static int send_mono_rect(VncState *vs, int x, int y,
974 int w, int h, uint32_t bg, uint32_t fg)
aa7d73fd 975{
2116eff9 976 ssize_t bytes;
aa7d73fd 977 int stream = 1;
d1af0e05 978 int level = tight_conf[vs->tight.compression].mono_zlib_level;
aa7d73fd 979
efe556ad
CC
980#ifdef CONFIG_VNC_PNG
981 if (tight_can_send_png_rect(vs, w, h)) {
982 int ret;
efe556ad 983 int bpp = vs->clientds.pf.bytes_per_pixel * 8;
5136a052 984 VncPalette *palette = palette_new(2, bpp);
efe556ad 985
5136a052
CC
986 palette_put(palette, bg);
987 palette_put(palette, fg);
efe556ad 988 ret = send_png_rect(vs, x, y, w, h, palette);
5136a052 989 palette_destroy(palette);
efe556ad
CC
990 return ret;
991 }
992#endif
993
aa7d73fd
CC
994 bytes = ((w + 7) / 8) * h;
995
996 vnc_write_u8(vs, (stream | VNC_TIGHT_EXPLICIT_FILTER) << 4);
997 vnc_write_u8(vs, VNC_TIGHT_FILTER_PALETTE);
998 vnc_write_u8(vs, 1);
999
1000 switch(vs->clientds.pf.bytes_per_pixel) {
1001 case 4:
1002 {
1003 uint32_t buf[2] = {bg, fg};
1004 size_t ret = sizeof (buf);
1005
d1af0e05 1006 if (vs->tight.pixel24) {
aa7d73fd
CC
1007 tight_pack24(vs, (unsigned char*)buf, 2, &ret);
1008 }
1009 vnc_write(vs, buf, ret);
1010
d1af0e05 1011 tight_encode_mono_rect32(vs->tight.tight.buffer, w, h, bg, fg);
aa7d73fd
CC
1012 break;
1013 }
1014 case 2:
1015 vnc_write(vs, &bg, 2);
1016 vnc_write(vs, &fg, 2);
d1af0e05 1017 tight_encode_mono_rect16(vs->tight.tight.buffer, w, h, bg, fg);
aa7d73fd
CC
1018 break;
1019 default:
1020 vnc_write_u8(vs, bg);
1021 vnc_write_u8(vs, fg);
d1af0e05 1022 tight_encode_mono_rect8(vs->tight.tight.buffer, w, h, bg, fg);
aa7d73fd
CC
1023 break;
1024 }
d1af0e05 1025 vs->tight.tight.offset = bytes;
aa7d73fd
CC
1026
1027 bytes = tight_compress_data(vs, stream, bytes, level, Z_DEFAULT_STRATEGY);
1028 return (bytes >= 0);
1029}
1030
1031struct palette_cb_priv {
1032 VncState *vs;
1033 uint8_t *header;
efe556ad
CC
1034#ifdef CONFIG_VNC_PNG
1035 png_colorp png_palette;
1036#endif
aa7d73fd
CC
1037};
1038
5136a052 1039static void write_palette(int idx, uint32_t color, void *opaque)
aa7d73fd
CC
1040{
1041 struct palette_cb_priv *priv = opaque;
1042 VncState *vs = priv->vs;
1043 uint32_t bytes = vs->clientds.pf.bytes_per_pixel;
aa7d73fd
CC
1044
1045 if (bytes == 4) {
aa7d73fd
CC
1046 ((uint32_t*)priv->header)[idx] = color;
1047 } else {
aa7d73fd
CC
1048 ((uint16_t*)priv->header)[idx] = color;
1049 }
1050}
1051
efe556ad 1052static bool send_gradient_rect(VncState *vs, int x, int y, int w, int h)
2f6f5c7a
CC
1053{
1054 int stream = 3;
d1af0e05 1055 int level = tight_conf[vs->tight.compression].gradient_zlib_level;
2116eff9 1056 ssize_t bytes;
2f6f5c7a
CC
1057
1058 if (vs->clientds.pf.bytes_per_pixel == 1)
efe556ad 1059 return send_full_color_rect(vs, x, y, w, h);
2f6f5c7a
CC
1060
1061 vnc_write_u8(vs, (stream | VNC_TIGHT_EXPLICIT_FILTER) << 4);
1062 vnc_write_u8(vs, VNC_TIGHT_FILTER_GRADIENT);
1063
d1af0e05 1064 buffer_reserve(&vs->tight.gradient, w * 3 * sizeof (int));
2f6f5c7a 1065
d1af0e05
CC
1066 if (vs->tight.pixel24) {
1067 tight_filter_gradient24(vs, vs->tight.tight.buffer, w, h);
2f6f5c7a
CC
1068 bytes = 3;
1069 } else if (vs->clientds.pf.bytes_per_pixel == 4) {
d1af0e05 1070 tight_filter_gradient32(vs, (uint32_t *)vs->tight.tight.buffer, w, h);
2f6f5c7a
CC
1071 bytes = 4;
1072 } else {
d1af0e05 1073 tight_filter_gradient16(vs, (uint16_t *)vs->tight.tight.buffer, w, h);
2f6f5c7a
CC
1074 bytes = 2;
1075 }
1076
d1af0e05 1077 buffer_reset(&vs->tight.gradient);
2f6f5c7a
CC
1078
1079 bytes = w * h * bytes;
d1af0e05 1080 vs->tight.tight.offset = bytes;
2f6f5c7a
CC
1081
1082 bytes = tight_compress_data(vs, stream, bytes,
1083 level, Z_FILTERED);
1084 return (bytes >= 0);
1085}
1086
efe556ad 1087static int send_palette_rect(VncState *vs, int x, int y,
5136a052 1088 int w, int h, VncPalette *palette)
aa7d73fd
CC
1089{
1090 int stream = 2;
d1af0e05 1091 int level = tight_conf[vs->tight.compression].idx_zlib_level;
aa7d73fd 1092 int colors;
2116eff9 1093 ssize_t bytes;
aa7d73fd 1094
efe556ad
CC
1095#ifdef CONFIG_VNC_PNG
1096 if (tight_can_send_png_rect(vs, w, h)) {
1097 return send_png_rect(vs, x, y, w, h, palette);
1098 }
1099#endif
1100
5136a052 1101 colors = palette_size(palette);
aa7d73fd
CC
1102
1103 vnc_write_u8(vs, (stream | VNC_TIGHT_EXPLICIT_FILTER) << 4);
1104 vnc_write_u8(vs, VNC_TIGHT_FILTER_PALETTE);
1105 vnc_write_u8(vs, colors - 1);
1106
1107 switch(vs->clientds.pf.bytes_per_pixel) {
1108 case 4:
1109 {
1110 size_t old_offset, offset;
5136a052 1111 uint32_t header[palette_size(palette)];
aa7d73fd
CC
1112 struct palette_cb_priv priv = { vs, (uint8_t *)header };
1113
1114 old_offset = vs->output.offset;
5136a052 1115 palette_iter(palette, write_palette, &priv);
aa7d73fd
CC
1116 vnc_write(vs, header, sizeof(header));
1117
d1af0e05 1118 if (vs->tight.pixel24) {
aa7d73fd
CC
1119 tight_pack24(vs, vs->output.buffer + old_offset, colors, &offset);
1120 vs->output.offset = old_offset + offset;
1121 }
1122
d1af0e05 1123 tight_encode_indexed_rect32(vs->tight.tight.buffer, w * h, palette);
aa7d73fd
CC
1124 break;
1125 }
1126 case 2:
1127 {
5136a052 1128 uint16_t header[palette_size(palette)];
aa7d73fd
CC
1129 struct palette_cb_priv priv = { vs, (uint8_t *)header };
1130
5136a052 1131 palette_iter(palette, write_palette, &priv);
aa7d73fd 1132 vnc_write(vs, header, sizeof(header));
d1af0e05 1133 tight_encode_indexed_rect16(vs->tight.tight.buffer, w * h, palette);
aa7d73fd
CC
1134 break;
1135 }
1136 default:
1137 return -1; /* No palette for 8bits colors */
1138 break;
1139 }
1140 bytes = w * h;
d1af0e05 1141 vs->tight.tight.offset = bytes;
aa7d73fd
CC
1142
1143 bytes = tight_compress_data(vs, stream, bytes,
1144 level, Z_DEFAULT_STRATEGY);
1145 return (bytes >= 0);
1146}
1147
efe556ad
CC
1148#if defined(CONFIG_VNC_JPEG) || defined(CONFIG_VNC_PNG)
1149static void rgb_prepare_row24(VncState *vs, uint8_t *dst, int x, int y,
1150 int count)
2f6f5c7a
CC
1151{
1152 VncDisplay *vd = vs->vd;
1153 uint32_t *fbptr;
1154 uint32_t pix;
1155
1156 fbptr = (uint32_t *)(vd->server->data + y * ds_get_linesize(vs->ds) +
1157 x * ds_get_bytes_per_pixel(vs->ds));
1158
1159 while (count--) {
1160 pix = *fbptr++;
1161 *dst++ = (uint8_t)(pix >> vs->ds->surface->pf.rshift);
1162 *dst++ = (uint8_t)(pix >> vs->ds->surface->pf.gshift);
1163 *dst++ = (uint8_t)(pix >> vs->ds->surface->pf.bshift);
1164 }
1165}
1166
efe556ad 1167#define DEFINE_RGB_GET_ROW_FUNCTION(bpp) \
2f6f5c7a
CC
1168 \
1169 static void \
efe556ad
CC
1170 rgb_prepare_row##bpp(VncState *vs, uint8_t *dst, \
1171 int x, int y, int count) \
2f6f5c7a
CC
1172 { \
1173 VncDisplay *vd = vs->vd; \
1174 uint##bpp##_t *fbptr; \
1175 uint##bpp##_t pix; \
1176 int r, g, b; \
1177 \
1178 fbptr = (uint##bpp##_t *) \
1179 (vd->server->data + y * ds_get_linesize(vs->ds) + \
1180 x * ds_get_bytes_per_pixel(vs->ds)); \
1181 \
1182 while (count--) { \
1183 pix = *fbptr++; \
1184 \
1185 r = (int)((pix >> vs->ds->surface->pf.rshift) \
1186 & vs->ds->surface->pf.rmax); \
1187 g = (int)((pix >> vs->ds->surface->pf.gshift) \
1188 & vs->ds->surface->pf.gmax); \
1189 b = (int)((pix >> vs->ds->surface->pf.bshift) \
1190 & vs->ds->surface->pf.bmax); \
1191 \
1192 *dst++ = (uint8_t)((r * 255 + vs->ds->surface->pf.rmax / 2) \
1193 / vs->ds->surface->pf.rmax); \
1194 *dst++ = (uint8_t)((g * 255 + vs->ds->surface->pf.gmax / 2) \
1195 / vs->ds->surface->pf.gmax); \
1196 *dst++ = (uint8_t)((b * 255 + vs->ds->surface->pf.bmax / 2) \
1197 / vs->ds->surface->pf.bmax); \
1198 } \
1199 }
1200
efe556ad
CC
1201DEFINE_RGB_GET_ROW_FUNCTION(16)
1202DEFINE_RGB_GET_ROW_FUNCTION(32)
2f6f5c7a 1203
efe556ad
CC
1204static void rgb_prepare_row(VncState *vs, uint8_t *dst, int x, int y,
1205 int count)
2f6f5c7a 1206{
4043a013
CC
1207 if (ds_get_bytes_per_pixel(vs->ds) == 4) {
1208 if (vs->ds->surface->pf.rmax == 0xFF &&
1209 vs->ds->surface->pf.gmax == 0xFF &&
1210 vs->ds->surface->pf.bmax == 0xFF) {
1211 rgb_prepare_row24(vs, dst, x, y, count);
1212 } else {
1213 rgb_prepare_row32(vs, dst, x, y, count);
1214 }
1215 } else {
efe556ad 1216 rgb_prepare_row16(vs, dst, x, y, count);
4043a013 1217 }
2f6f5c7a 1218}
efe556ad 1219#endif /* CONFIG_VNC_JPEG or CONFIG_VNC_PNG */
2f6f5c7a 1220
efe556ad
CC
1221/*
1222 * JPEG compression stuff.
1223 */
1224#ifdef CONFIG_VNC_JPEG
2f6f5c7a
CC
1225/*
1226 * Destination manager implementation for JPEG library.
1227 */
1228
1229/* This is called once per encoding */
1230static void jpeg_init_destination(j_compress_ptr cinfo)
1231{
1232 VncState *vs = cinfo->client_data;
d1af0e05 1233 Buffer *buffer = &vs->tight.jpeg;
2f6f5c7a
CC
1234
1235 cinfo->dest->next_output_byte = (JOCTET *)buffer->buffer + buffer->offset;
1236 cinfo->dest->free_in_buffer = (size_t)(buffer->capacity - buffer->offset);
1237}
1238
1239/* This is called when we ran out of buffer (shouldn't happen!) */
1240static boolean jpeg_empty_output_buffer(j_compress_ptr cinfo)
1241{
1242 VncState *vs = cinfo->client_data;
d1af0e05 1243 Buffer *buffer = &vs->tight.jpeg;
2f6f5c7a
CC
1244
1245 buffer->offset = buffer->capacity;
1246 buffer_reserve(buffer, 2048);
1247 jpeg_init_destination(cinfo);
1248 return TRUE;
1249}
1250
1251/* This is called when we are done processing data */
1252static void jpeg_term_destination(j_compress_ptr cinfo)
1253{
1254 VncState *vs = cinfo->client_data;
d1af0e05 1255 Buffer *buffer = &vs->tight.jpeg;
2f6f5c7a
CC
1256
1257 buffer->offset = buffer->capacity - cinfo->dest->free_in_buffer;
1258}
1259
1260static int send_jpeg_rect(VncState *vs, int x, int y, int w, int h, int quality)
1261{
1262 struct jpeg_compress_struct cinfo;
1263 struct jpeg_error_mgr jerr;
1264 struct jpeg_destination_mgr manager;
1265 JSAMPROW row[1];
1266 uint8_t *buf;
1267 int dy;
1268
1269 if (ds_get_bytes_per_pixel(vs->ds) == 1)
efe556ad 1270 return send_full_color_rect(vs, x, y, w, h);
2f6f5c7a 1271
d1af0e05 1272 buffer_reserve(&vs->tight.jpeg, 2048);
2f6f5c7a
CC
1273
1274 cinfo.err = jpeg_std_error(&jerr);
1275 jpeg_create_compress(&cinfo);
1276
1277 cinfo.client_data = vs;
1278 cinfo.image_width = w;
1279 cinfo.image_height = h;
1280 cinfo.input_components = 3;
1281 cinfo.in_color_space = JCS_RGB;
1282
1283 jpeg_set_defaults(&cinfo);
1284 jpeg_set_quality(&cinfo, quality, true);
1285
1286 manager.init_destination = jpeg_init_destination;
1287 manager.empty_output_buffer = jpeg_empty_output_buffer;
1288 manager.term_destination = jpeg_term_destination;
1289 cinfo.dest = &manager;
1290
1291 jpeg_start_compress(&cinfo, true);
1292
d9c18c24
CC
1293 buf = qemu_malloc(w * 3);
1294 row[0] = buf;
2f6f5c7a 1295 for (dy = 0; dy < h; dy++) {
efe556ad 1296 rgb_prepare_row(vs, buf, x, y + dy, w);
2f6f5c7a
CC
1297 jpeg_write_scanlines(&cinfo, row, 1);
1298 }
d9c18c24 1299 qemu_free(buf);
2f6f5c7a
CC
1300
1301 jpeg_finish_compress(&cinfo);
1302 jpeg_destroy_compress(&cinfo);
1303
1304 vnc_write_u8(vs, VNC_TIGHT_JPEG << 4);
1305
d1af0e05
CC
1306 tight_send_compact_size(vs, vs->tight.jpeg.offset);
1307 vnc_write(vs, vs->tight.jpeg.buffer, vs->tight.jpeg.offset);
1308 buffer_reset(&vs->tight.jpeg);
2f6f5c7a
CC
1309
1310 return 1;
1311}
1312#endif /* CONFIG_VNC_JPEG */
1313
efe556ad
CC
1314/*
1315 * PNG compression stuff.
1316 */
1317#ifdef CONFIG_VNC_PNG
5136a052 1318static void write_png_palette(int idx, uint32_t pix, void *opaque)
efe556ad
CC
1319{
1320 struct palette_cb_priv *priv = opaque;
1321 VncState *vs = priv->vs;
efe556ad 1322 png_colorp color = &priv->png_palette[idx];
efe556ad 1323
d1af0e05 1324 if (vs->tight.pixel24)
efe556ad
CC
1325 {
1326 color->red = (pix >> vs->clientds.pf.rshift) & vs->clientds.pf.rmax;
1327 color->green = (pix >> vs->clientds.pf.gshift) & vs->clientds.pf.gmax;
1328 color->blue = (pix >> vs->clientds.pf.bshift) & vs->clientds.pf.bmax;
1329 }
1330 else
1331 {
1332 int red, green, blue;
1333
1334 red = (pix >> vs->clientds.pf.rshift) & vs->clientds.pf.rmax;
1335 green = (pix >> vs->clientds.pf.gshift) & vs->clientds.pf.gmax;
1336 blue = (pix >> vs->clientds.pf.bshift) & vs->clientds.pf.bmax;
1337 color->red = ((red * 255 + vs->clientds.pf.rmax / 2) /
1338 vs->clientds.pf.rmax);
1339 color->green = ((green * 255 + vs->clientds.pf.gmax / 2) /
1340 vs->clientds.pf.gmax);
1341 color->blue = ((blue * 255 + vs->clientds.pf.bmax / 2) /
1342 vs->clientds.pf.bmax);
1343 }
1344}
1345
1346static void png_write_data(png_structp png_ptr, png_bytep data,
1347 png_size_t length)
1348{
1349 VncState *vs = png_get_io_ptr(png_ptr);
1350
d1af0e05
CC
1351 buffer_reserve(&vs->tight.png, vs->tight.png.offset + length);
1352 memcpy(vs->tight.png.buffer + vs->tight.png.offset, data, length);
efe556ad 1353
d1af0e05 1354 vs->tight.png.offset += length;
efe556ad
CC
1355}
1356
1357static void png_flush_data(png_structp png_ptr)
1358{
1359}
1360
1361static void *vnc_png_malloc(png_structp png_ptr, png_size_t size)
1362{
1363 return qemu_malloc(size);
1364}
1365
1366static void vnc_png_free(png_structp png_ptr, png_voidp ptr)
1367{
1368 qemu_free(ptr);
1369}
1370
1371static int send_png_rect(VncState *vs, int x, int y, int w, int h,
5136a052 1372 VncPalette *palette)
efe556ad
CC
1373{
1374 png_byte color_type;
1375 png_structp png_ptr;
1376 png_infop info_ptr;
1377 png_colorp png_palette = NULL;
d1af0e05
CC
1378 int level = tight_png_conf[vs->tight.compression].png_zlib_level;
1379 int filters = tight_png_conf[vs->tight.compression].png_filters;
efe556ad
CC
1380 uint8_t *buf;
1381 int dy;
1382
1383 png_ptr = png_create_write_struct_2(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL,
1384 NULL, vnc_png_malloc, vnc_png_free);
1385
1386 if (png_ptr == NULL)
1387 return -1;
1388
1389 info_ptr = png_create_info_struct(png_ptr);
1390
1391 if (info_ptr == NULL) {
1392 png_destroy_write_struct(&png_ptr, NULL);
1393 return -1;
1394 }
1395
1396 png_set_write_fn(png_ptr, (void *) vs, png_write_data, png_flush_data);
1397 png_set_compression_level(png_ptr, level);
3941bf6f 1398 png_set_filter(png_ptr, PNG_FILTER_TYPE_DEFAULT, filters);
efe556ad
CC
1399
1400 if (palette) {
1401 color_type = PNG_COLOR_TYPE_PALETTE;
1402 } else {
1403 color_type = PNG_COLOR_TYPE_RGB;
1404 }
1405
1406 png_set_IHDR(png_ptr, info_ptr, w, h,
1407 8, color_type, PNG_INTERLACE_NONE,
1408 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
1409
1410 if (color_type == PNG_COLOR_TYPE_PALETTE) {
1411 struct palette_cb_priv priv;
1412
1413 png_palette = png_malloc(png_ptr, sizeof(*png_palette) *
5136a052 1414 palette_size(palette));
efe556ad
CC
1415
1416 priv.vs = vs;
1417 priv.png_palette = png_palette;
5136a052 1418 palette_iter(palette, write_png_palette, &priv);
efe556ad 1419
5136a052 1420 png_set_PLTE(png_ptr, info_ptr, png_palette, palette_size(palette));
efe556ad 1421
efe556ad 1422 if (vs->clientds.pf.bytes_per_pixel == 4) {
d1af0e05 1423 tight_encode_indexed_rect32(vs->tight.tight.buffer, w * h, palette);
efe556ad 1424 } else {
d1af0e05 1425 tight_encode_indexed_rect16(vs->tight.tight.buffer, w * h, palette);
efe556ad
CC
1426 }
1427 }
1428
1429 png_write_info(png_ptr, info_ptr);
1430
d1af0e05 1431 buffer_reserve(&vs->tight.png, 2048);
efe556ad
CC
1432 buf = qemu_malloc(w * 3);
1433 for (dy = 0; dy < h; dy++)
1434 {
1435 if (color_type == PNG_COLOR_TYPE_PALETTE) {
d1af0e05 1436 memcpy(buf, vs->tight.tight.buffer + (dy * w), w);
efe556ad
CC
1437 } else {
1438 rgb_prepare_row(vs, buf, x, y + dy, w);
1439 }
1440 png_write_row(png_ptr, buf);
1441 }
1442 qemu_free(buf);
1443
1444 png_write_end(png_ptr, NULL);
1445
1446 if (color_type == PNG_COLOR_TYPE_PALETTE) {
1447 png_free(png_ptr, png_palette);
1448 }
1449
1450 png_destroy_write_struct(&png_ptr, &info_ptr);
1451
1452 vnc_write_u8(vs, VNC_TIGHT_PNG << 4);
1453
d1af0e05
CC
1454 tight_send_compact_size(vs, vs->tight.png.offset);
1455 vnc_write(vs, vs->tight.png.buffer, vs->tight.png.offset);
1456 buffer_reset(&vs->tight.png);
efe556ad
CC
1457 return 1;
1458}
1459#endif /* CONFIG_VNC_PNG */
1460
380282b0
CC
1461static void vnc_tight_start(VncState *vs)
1462{
d1af0e05 1463 buffer_reset(&vs->tight.tight);
380282b0
CC
1464
1465 // make the output buffer be the zlib buffer, so we can compress it later
d1af0e05
CC
1466 vs->tight.tmp = vs->output;
1467 vs->output = vs->tight.tight;
380282b0
CC
1468}
1469
1470static void vnc_tight_stop(VncState *vs)
1471{
1472 // switch back to normal output/zlib buffers
d1af0e05
CC
1473 vs->tight.tight = vs->output;
1474 vs->output = vs->tight.tmp;
380282b0
CC
1475}
1476
03817eb8
CC
1477static int send_sub_rect_nojpeg(VncState *vs, int x, int y, int w, int h,
1478 int bg, int fg, int colors, VncPalette *palette)
380282b0 1479{
03817eb8 1480 int ret;
380282b0 1481
03817eb8
CC
1482 if (colors == 0) {
1483 if (tight_detect_smooth_image(vs, w, h)) {
1484 ret = send_gradient_rect(vs, x, y, w, h);
1485 } else {
1486 ret = send_full_color_rect(vs, x, y, w, h);
1487 }
1488 } else if (colors == 1) {
1489 ret = send_solid_rect(vs);
1490 } else if (colors == 2) {
1491 ret = send_mono_rect(vs, x, y, w, h, bg, fg);
1492 } else if (colors <= 256) {
1493 ret = send_palette_rect(vs, x, y, w, h, palette);
d167f9bc
BS
1494 } else {
1495 ret = 0;
03817eb8
CC
1496 }
1497 return ret;
1498}
380282b0 1499
03817eb8
CC
1500#ifdef CONFIG_VNC_JPEG
1501static int send_sub_rect_jpeg(VncState *vs, int x, int y, int w, int h,
1502 int bg, int fg, int colors,
ce702e93 1503 VncPalette *palette, bool force)
03817eb8
CC
1504{
1505 int ret;
aa7d73fd
CC
1506
1507 if (colors == 0) {
ce702e93
CC
1508 if (force || (tight_jpeg_conf[vs->tight.quality].jpeg_full &&
1509 tight_detect_smooth_image(vs, w, h))) {
03817eb8 1510 int quality = tight_conf[vs->tight.quality].jpeg_quality;
2f6f5c7a 1511
03817eb8 1512 ret = send_jpeg_rect(vs, x, y, w, h, quality);
2f6f5c7a 1513 } else {
efe556ad 1514 ret = send_full_color_rect(vs, x, y, w, h);
2f6f5c7a 1515 }
aa7d73fd
CC
1516 } else if (colors == 1) {
1517 ret = send_solid_rect(vs);
1518 } else if (colors == 2) {
efe556ad 1519 ret = send_mono_rect(vs, x, y, w, h, bg, fg);
aa7d73fd 1520 } else if (colors <= 256) {
ce702e93
CC
1521 if (force || (colors > 96 &&
1522 tight_jpeg_conf[vs->tight.quality].jpeg_idx &&
1523 tight_detect_smooth_image(vs, w, h))) {
d1af0e05 1524 int quality = tight_conf[vs->tight.quality].jpeg_quality;
2f6f5c7a
CC
1525
1526 ret = send_jpeg_rect(vs, x, y, w, h, quality);
1527 } else {
efe556ad 1528 ret = send_palette_rect(vs, x, y, w, h, palette);
2f6f5c7a 1529 }
ad7ee4ad
BS
1530 } else {
1531 ret = 0;
03817eb8
CC
1532 }
1533 return ret;
1534}
2f6f5c7a 1535#endif
03817eb8
CC
1536
1537static int send_sub_rect(VncState *vs, int x, int y, int w, int h)
1538{
1539 VncPalette *palette = NULL;
1540 uint32_t bg = 0, fg = 0;
1541 int colors;
1542 int ret = 0;
cf76a1ce 1543#ifdef CONFIG_VNC_JPEG
ce702e93
CC
1544 bool force_jpeg = false;
1545 bool allow_jpeg = true;
cf76a1ce 1546#endif
03817eb8
CC
1547
1548 vnc_framebuffer_update(vs, x, y, w, h, vs->tight.type);
1549
1550 vnc_tight_start(vs);
1551 vnc_raw_send_framebuffer_update(vs, x, y, w, h);
1552 vnc_tight_stop(vs);
1553
ce702e93 1554#ifdef CONFIG_VNC_JPEG
80e0c8c3 1555 if (!vs->vd->non_adaptive && vs->tight.quality != (uint8_t)-1) {
ce702e93
CC
1556 double freq = vnc_update_freq(vs, x, y, w, h);
1557
1558 if (freq < tight_jpeg_conf[vs->tight.quality].jpeg_freq_min) {
1559 allow_jpeg = false;
1560 }
1561 if (freq >= tight_jpeg_conf[vs->tight.quality].jpeg_freq_threshold) {
1562 force_jpeg = true;
1563 vnc_sent_lossy_rect(vs, x, y, w, h);
1564 }
1565 }
1566#endif
1567
03817eb8
CC
1568 colors = tight_fill_palette(vs, x, y, w * h, &fg, &bg, &palette);
1569
1570#ifdef CONFIG_VNC_JPEG
ce702e93
CC
1571 if (allow_jpeg && vs->tight.quality != (uint8_t)-1) {
1572 ret = send_sub_rect_jpeg(vs, x, y, w, h, bg, fg, colors, palette,
1573 force_jpeg);
03817eb8
CC
1574 } else {
1575 ret = send_sub_rect_nojpeg(vs, x, y, w, h, bg, fg, colors, palette);
aa7d73fd 1576 }
03817eb8
CC
1577#else
1578 ret = send_sub_rect_nojpeg(vs, x, y, w, h, bg, fg, colors, palette);
1579#endif
1580
5136a052 1581 palette_destroy(palette);
aa7d73fd 1582 return ret;
380282b0
CC
1583}
1584
b4bea3f2
CC
1585static int send_sub_rect_solid(VncState *vs, int x, int y, int w, int h)
1586{
d1af0e05 1587 vnc_framebuffer_update(vs, x, y, w, h, vs->tight.type);
b4bea3f2
CC
1588
1589 vnc_tight_start(vs);
1590 vnc_raw_send_framebuffer_update(vs, x, y, w, h);
1591 vnc_tight_stop(vs);
1592
1593 return send_solid_rect(vs);
1594}
1595
ce702e93
CC
1596static int send_rect_simple(VncState *vs, int x, int y, int w, int h,
1597 bool split)
380282b0
CC
1598{
1599 int max_size, max_width;
1600 int max_sub_width, max_sub_height;
1601 int dx, dy;
1602 int rw, rh;
1603 int n = 0;
1604
d1af0e05
CC
1605 max_size = tight_conf[vs->tight.compression].max_rect_size;
1606 max_width = tight_conf[vs->tight.compression].max_rect_width;
380282b0 1607
ce702e93 1608 if (split && (w > max_width || w * h > max_size)) {
380282b0
CC
1609 max_sub_width = (w > max_width) ? max_width : w;
1610 max_sub_height = max_size / max_sub_width;
1611
1612 for (dy = 0; dy < h; dy += max_sub_height) {
1613 for (dx = 0; dx < w; dx += max_width) {
1614 rw = MIN(max_sub_width, w - dx);
1615 rh = MIN(max_sub_height, h - dy);
1616 n += send_sub_rect(vs, x+dx, y+dy, rw, rh);
1617 }
1618 }
1619 } else {
1620 n += send_sub_rect(vs, x, y, w, h);
1621 }
1622
1623 return n;
1624}
1625
b4bea3f2
CC
1626static int find_large_solid_color_rect(VncState *vs, int x, int y,
1627 int w, int h, int max_rows)
1628{
1629 int dx, dy, dw, dh;
1630 int n = 0;
1631
1632 /* Try to find large solid-color areas and send them separately. */
1633
1634 for (dy = y; dy < y + h; dy += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) {
1635
1636 /* If a rectangle becomes too large, send its upper part now. */
1637
1638 if (dy - y >= max_rows) {
ce702e93 1639 n += send_rect_simple(vs, x, y, w, max_rows, true);
b4bea3f2
CC
1640 y += max_rows;
1641 h -= max_rows;
1642 }
1643
1644 dh = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, (y + h - dy));
1645
1646 for (dx = x; dx < x + w; dx += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) {
1647 uint32_t color_value;
1648 int x_best, y_best, w_best, h_best;
1649
1650 dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, (x + w - dx));
1651
1652 if (!check_solid_tile(vs, dx, dy, dw, dh, &color_value, false)) {
1653 continue ;
1654 }
1655
1656 /* Get dimensions of solid-color area. */
1657
1658 find_best_solid_area(vs, dx, dy, w - (dx - x), h - (dy - y),
1659 color_value, &w_best, &h_best);
1660
1661 /* Make sure a solid rectangle is large enough
1662 (or the whole rectangle is of the same color). */
1663
1664 if (w_best * h_best != w * h &&
1665 w_best * h_best < VNC_TIGHT_MIN_SOLID_SUBRECT_SIZE) {
1666 continue;
1667 }
1668
1669 /* Try to extend solid rectangle to maximum size. */
1670
1671 x_best = dx; y_best = dy;
1672 extend_solid_area(vs, x, y, w, h, color_value,
1673 &x_best, &y_best, &w_best, &h_best);
1674
1675 /* Send rectangles at top and left to solid-color area. */
1676
1677 if (y_best != y) {
ce702e93 1678 n += send_rect_simple(vs, x, y, w, y_best-y, true);
b4bea3f2
CC
1679 }
1680 if (x_best != x) {
efe556ad
CC
1681 n += tight_send_framebuffer_update(vs, x, y_best,
1682 x_best-x, h_best);
b4bea3f2
CC
1683 }
1684
1685 /* Send solid-color rectangle. */
1686 n += send_sub_rect_solid(vs, x_best, y_best, w_best, h_best);
1687
1688 /* Send remaining rectangles (at right and bottom). */
1689
1690 if (x_best + w_best != x + w) {
efe556ad
CC
1691 n += tight_send_framebuffer_update(vs, x_best+w_best,
1692 y_best,
1693 w-(x_best-x)-w_best,
1694 h_best);
b4bea3f2
CC
1695 }
1696 if (y_best + h_best != y + h) {
efe556ad
CC
1697 n += tight_send_framebuffer_update(vs, x, y_best+h_best,
1698 w, h-(y_best-y)-h_best);
b4bea3f2
CC
1699 }
1700
1701 /* Return after all recursive calls are done. */
1702 return n;
1703 }
1704 }
ce702e93 1705 return n + send_rect_simple(vs, x, y, w, h, true);
b4bea3f2
CC
1706}
1707
efe556ad
CC
1708static int tight_send_framebuffer_update(VncState *vs, int x, int y,
1709 int w, int h)
380282b0 1710{
b4bea3f2
CC
1711 int max_rows;
1712
380282b0
CC
1713 if (vs->clientds.pf.bytes_per_pixel == 4 && vs->clientds.pf.rmax == 0xFF &&
1714 vs->clientds.pf.bmax == 0xFF && vs->clientds.pf.gmax == 0xFF) {
d1af0e05 1715 vs->tight.pixel24 = true;
380282b0 1716 } else {
d1af0e05 1717 vs->tight.pixel24 = false;
380282b0
CC
1718 }
1719
cf76a1ce 1720#ifdef CONFIG_VNC_JPEG
368d2588 1721 if (vs->tight.quality != (uint8_t)-1) {
ce702e93
CC
1722 double freq = vnc_update_freq(vs, x, y, w, h);
1723
1724 if (freq > tight_jpeg_conf[vs->tight.quality].jpeg_freq_threshold) {
1725 return send_rect_simple(vs, x, y, w, h, false);
1726 }
1727 }
cf76a1ce 1728#endif
ce702e93
CC
1729
1730 if (w * h < VNC_TIGHT_MIN_SPLIT_RECT_SIZE) {
1731 return send_rect_simple(vs, x, y, w, h, true);
1732 }
b4bea3f2
CC
1733
1734 /* Calculate maximum number of rows in one non-solid rectangle. */
1735
d1af0e05
CC
1736 max_rows = tight_conf[vs->tight.compression].max_rect_size;
1737 max_rows /= MIN(tight_conf[vs->tight.compression].max_rect_width, w);
b4bea3f2
CC
1738
1739 return find_large_solid_color_rect(vs, x, y, w, h, max_rows);
380282b0
CC
1740}
1741
efe556ad
CC
1742int vnc_tight_send_framebuffer_update(VncState *vs, int x, int y,
1743 int w, int h)
1744{
d1af0e05 1745 vs->tight.type = VNC_ENCODING_TIGHT;
efe556ad
CC
1746 return tight_send_framebuffer_update(vs, x, y, w, h);
1747}
1748
1749int vnc_tight_png_send_framebuffer_update(VncState *vs, int x, int y,
1750 int w, int h)
1751{
d1af0e05 1752 vs->tight.type = VNC_ENCODING_TIGHT_PNG;
efe556ad
CC
1753 return tight_send_framebuffer_update(vs, x, y, w, h);
1754}
1755
380282b0
CC
1756void vnc_tight_clear(VncState *vs)
1757{
1758 int i;
d1af0e05
CC
1759 for (i=0; i<ARRAY_SIZE(vs->tight.stream); i++) {
1760 if (vs->tight.stream[i].opaque) {
1761 deflateEnd(&vs->tight.stream[i]);
380282b0
CC
1762 }
1763 }
1764
d1af0e05
CC
1765 buffer_free(&vs->tight.tight);
1766 buffer_free(&vs->tight.zlib);
1767 buffer_free(&vs->tight.gradient);
2f6f5c7a 1768#ifdef CONFIG_VNC_JPEG
d1af0e05 1769 buffer_free(&vs->tight.jpeg);
2f6f5c7a 1770#endif
b5469b11
CC
1771#ifdef CONFIG_VNC_PNG
1772 buffer_free(&vs->tight.png);
1773#endif
380282b0 1774}