vnc-enc-tight.c 66.1 KB
Newer Older
C
Corentin Chary 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * QEMU VNC display driver: tight encoding
 *
 * From libvncserver/libvncserver/tight.c
 * Copyright (C) 2000, 2001 Const Kaplinsky.  All Rights Reserved.
 * Copyright (C) 1999 AT&T Laboratories Cambridge.  All Rights Reserved.
 *
 * Copyright (C) 2010 Corentin Chary <corentin.chary@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

C
Corentin Chary 已提交
29
#include "config-host.h"
30

C
Corentin Chary 已提交
31 32 33
#ifdef CONFIG_VNC_PNG
#include <png.h>
#endif
34 35 36 37 38
#ifdef CONFIG_VNC_JPEG
#include <stdio.h>
#include <jpeglib.h>
#endif

C
Corentin Chary 已提交
39 40
#include "qemu-common.h"

41
#include "bswap.h"
42
#include "qint.h"
C
Corentin Chary 已提交
43
#include "vnc.h"
44
#include "vnc-enc-tight.h"
45
#include "vnc-palette.h"
C
Corentin Chary 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

/* Compression level stuff. The following array contains various
   encoder parameters for each of 10 compression levels (0..9).
   Last three parameters correspond to JPEG quality levels (0..9). */

static const struct {
    int max_rect_size, max_rect_width;
    int mono_min_rect_size, gradient_min_rect_size;
    int idx_zlib_level, mono_zlib_level, raw_zlib_level, gradient_zlib_level;
    int gradient_threshold, gradient_threshold24;
    int idx_max_colors_divisor;
    int jpeg_quality, jpeg_threshold, jpeg_threshold24;
} tight_conf[] = {
    {   512,   32,   6, 65536, 0, 0, 0, 0,   0,   0,   4,  5, 10000, 23000 },
    {  2048,  128,   6, 65536, 1, 1, 1, 0,   0,   0,   8, 10,  8000, 18000 },
    {  6144,  256,   8, 65536, 3, 3, 2, 0,   0,   0,  24, 15,  6500, 15000 },
    { 10240, 1024,  12, 65536, 5, 5, 3, 0,   0,   0,  32, 25,  5000, 12000 },
    { 16384, 2048,  12, 65536, 6, 6, 4, 0,   0,   0,  32, 37,  4000, 10000 },
    { 32768, 2048,  12,  4096, 7, 7, 5, 4, 150, 380,  32, 50,  3000,  8000 },
    { 65536, 2048,  16,  4096, 7, 7, 6, 4, 170, 420,  48, 60,  2000,  5000 },
    { 65536, 2048,  16,  4096, 8, 8, 7, 5, 180, 450,  64, 70,  1000,  2500 },
    { 65536, 2048,  32,  8192, 9, 9, 8, 6, 190, 475,  64, 75,   500,  1200 },
    { 65536, 2048,  32,  8192, 9, 9, 9, 6, 200, 500,  96, 80,   200,   500 }
};

C
Corentin Chary 已提交
71 72 73 74

static int tight_send_framebuffer_update(VncState *vs, int x, int y,
                                         int w, int h);

75 76 77 78 79 80 81
#ifdef CONFIG_VNC_JPEG
static const struct {
    double jpeg_freq_min;       /* Don't send JPEG if the freq is bellow */
    double jpeg_freq_threshold; /* Always send JPEG if the freq is above */
    int jpeg_idx;               /* Allow indexed JPEG */
    int jpeg_full;              /* Allow full color JPEG */
} tight_jpeg_conf[] = {
82 83 84 85 86 87 88 89 90 91
    { 0,   8,  1, 1 },
    { 0,   8,  1, 1 },
    { 0,   8,  1, 1 },
    { 0,   8,  1, 1 },
    { 0,   10, 1, 1 },
    { 0.1, 10, 1, 1 },
    { 0.2, 10, 1, 1 },
    { 0.3, 12, 0, 0 },
    { 0.4, 14, 0, 0 },
    { 0.5, 16, 0, 0 },
92 93 94
};
#endif

C
Corentin Chary 已提交
95
#ifdef CONFIG_VNC_PNG
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
static const struct {
    int png_zlib_level, png_filters;
} tight_png_conf[] = {
    { 0, PNG_NO_FILTERS },
    { 1, PNG_NO_FILTERS },
    { 2, PNG_NO_FILTERS },
    { 3, PNG_NO_FILTERS },
    { 4, PNG_NO_FILTERS },
    { 5, PNG_ALL_FILTERS },
    { 6, PNG_ALL_FILTERS },
    { 7, PNG_ALL_FILTERS },
    { 8, PNG_ALL_FILTERS },
    { 9, PNG_ALL_FILTERS },
};

C
Corentin Chary 已提交
111
static int send_png_rect(VncState *vs, int x, int y, int w, int h,
112
                         VncPalette *palette);
C
Corentin Chary 已提交
113 114 115

static bool tight_can_send_png_rect(VncState *vs, int w, int h)
{
116
    if (vs->tight.type != VNC_ENCODING_TIGHT_PNG) {
C
Corentin Chary 已提交
117 118 119 120 121 122 123 124 125 126 127 128
        return false;
    }

    if (ds_get_bytes_per_pixel(vs->ds) == 1 ||
        vs->clientds.pf.bytes_per_pixel == 1) {
        return false;
    }

    return true;
}
#endif

129 130 131 132 133
/*
 * Code to guess if given rectangle is suitable for smooth image
 * compression (by applying "gradient" filter or JPEG coder).
 */

B
Blue Swirl 已提交
134
static unsigned int
135 136 137 138
tight_detect_smooth_image24(VncState *vs, int w, int h)
{
    int off;
    int x, y, d, dx;
B
Blue Swirl 已提交
139 140
    unsigned int c;
    unsigned int stats[256];
141 142
    int pixels = 0;
    int pix, left[3];
B
Blue Swirl 已提交
143
    unsigned int errors;
144
    unsigned char *buf = vs->tight.tight.buffer;
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199

    /*
     * If client is big-endian, color samples begin from the second
     * byte (offset 1) of a 32-bit pixel value.
     */
    off = !!(vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG);

    memset(stats, 0, sizeof (stats));

    for (y = 0, x = 0; y < h && x < w;) {
        for (d = 0; d < h - y && d < w - x - VNC_TIGHT_DETECT_SUBROW_WIDTH;
             d++) {
            for (c = 0; c < 3; c++) {
                left[c] = buf[((y+d)*w+x+d)*4+off+c] & 0xFF;
            }
            for (dx = 1; dx <= VNC_TIGHT_DETECT_SUBROW_WIDTH; dx++) {
                for (c = 0; c < 3; c++) {
                    pix = buf[((y+d)*w+x+d+dx)*4+off+c] & 0xFF;
                    stats[abs(pix - left[c])]++;
                    left[c] = pix;
                }
                pixels++;
            }
        }
        if (w > h) {
            x += h;
            y = 0;
        } else {
            x = 0;
            y += w;
        }
    }

    /* 95% smooth or more ... */
    if (stats[0] * 33 / pixels >= 95) {
        return 0;
    }

    errors = 0;
    for (c = 1; c < 8; c++) {
        errors += stats[c] * (c * c);
        if (stats[c] == 0 || stats[c] > stats[c-1] * 2) {
            return 0;
        }
    }
    for (; c < 256; c++) {
        errors += stats[c] * (c * c);
    }
    errors /= (pixels * 3 - stats[0]);

    return errors;
}

#define DEFINE_DETECT_FUNCTION(bpp)                                     \
                                                                        \
B
Blue Swirl 已提交
200
    static unsigned int                                                 \
201 202 203 204 205
    tight_detect_smooth_image##bpp(VncState *vs, int w, int h) {        \
        bool endian;                                                    \
        uint##bpp##_t pix;                                              \
        int max[3], shift[3];                                           \
        int x, y, d, dx;                                                \
B
Blue Swirl 已提交
206 207
        unsigned int c;                                                 \
        unsigned int stats[256];                                        \
208 209
        int pixels = 0;                                                 \
        int sample, sum, left[3];                                       \
B
Blue Swirl 已提交
210
        unsigned int errors;                                            \
211
        unsigned char *buf = vs->tight.tight.buffer;                    \
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
                                                                        \
        endian = ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) !=        \
                  (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG));     \
                                                                        \
                                                                        \
        max[0] = vs->clientds.pf.rmax;                                  \
        max[1] = vs->clientds.pf.gmax;                                  \
        max[2] = vs->clientds.pf.bmax;                                  \
        shift[0] = vs->clientds.pf.rshift;                              \
        shift[1] = vs->clientds.pf.gshift;                              \
        shift[2] = vs->clientds.pf.bshift;                              \
                                                                        \
        memset(stats, 0, sizeof(stats));                                \
                                                                        \
        y = 0, x = 0;                                                   \
        while (y < h && x < w) {                                        \
            for (d = 0; d < h - y &&                                    \
                     d < w - x - VNC_TIGHT_DETECT_SUBROW_WIDTH; d++) {  \
                pix = ((uint##bpp##_t *)buf)[(y+d)*w+x+d];              \
                if (endian) {                                           \
232
                    pix = bswap##bpp(pix);                              \
233 234 235 236 237 238 239 240
                }                                                       \
                for (c = 0; c < 3; c++) {                               \
                    left[c] = (int)(pix >> shift[c] & max[c]);          \
                }                                                       \
                for (dx = 1; dx <= VNC_TIGHT_DETECT_SUBROW_WIDTH;       \
                     dx++) {                                            \
                    pix = ((uint##bpp##_t *)buf)[(y+d)*w+x+d+dx];       \
                    if (endian) {                                       \
241
                        pix = bswap##bpp(pix);                          \
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
                    }                                                   \
                    sum = 0;                                            \
                    for (c = 0; c < 3; c++) {                           \
                        sample = (int)(pix >> shift[c] & max[c]);       \
                        sum += abs(sample - left[c]);                   \
                        left[c] = sample;                               \
                    }                                                   \
                    if (sum > 255) {                                    \
                        sum = 255;                                      \
                    }                                                   \
                    stats[sum]++;                                       \
                    pixels++;                                           \
                }                                                       \
            }                                                           \
            if (w > h) {                                                \
                x += h;                                                 \
                y = 0;                                                  \
            } else {                                                    \
                x = 0;                                                  \
                y += w;                                                 \
            }                                                           \
        }                                                               \
                                                                        \
        if ((stats[0] + stats[1]) * 100 / pixels >= 90) {               \
            return 0;                                                   \
        }                                                               \
                                                                        \
        errors = 0;                                                     \
        for (c = 1; c < 8; c++) {                                       \
            errors += stats[c] * (c * c);                               \
            if (stats[c] == 0 || stats[c] > stats[c-1] * 2) {           \
                return 0;                                               \
            }                                                           \
        }                                                               \
        for (; c < 256; c++) {                                          \
            errors += stats[c] * (c * c);                               \
        }                                                               \
        errors /= (pixels - stats[0]);                                  \
                                                                        \
        return errors;                                                  \
    }

DEFINE_DETECT_FUNCTION(16)
DEFINE_DETECT_FUNCTION(32)

static int
tight_detect_smooth_image(VncState *vs, int w, int h)
{
B
Blue Swirl 已提交
290
    unsigned int errors;
291 292
    int compression = vs->tight.compression;
    int quality = vs->tight.quality;
293

C
Corentin Chary 已提交
294 295 296 297
    if (!vs->vd->lossy) {
        return 0;
    }

298 299 300 301 302 303
    if (ds_get_bytes_per_pixel(vs->ds) == 1 ||
        vs->clientds.pf.bytes_per_pixel == 1 ||
        w < VNC_TIGHT_DETECT_MIN_WIDTH || h < VNC_TIGHT_DETECT_MIN_HEIGHT) {
        return 0;
    }

304
    if (vs->tight.quality != (uint8_t)-1) {
305 306 307 308 309 310 311 312 313 314
        if (w * h < VNC_TIGHT_JPEG_MIN_RECT_SIZE) {
            return 0;
        }
    } else {
        if (w * h < tight_conf[compression].gradient_min_rect_size) {
            return 0;
        }
    }

    if (vs->clientds.pf.bytes_per_pixel == 4) {
315
        if (vs->tight.pixel24) {
316
            errors = tight_detect_smooth_image24(vs, w, h);
317
            if (vs->tight.quality != (uint8_t)-1) {
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
                return (errors < tight_conf[quality].jpeg_threshold24);
            }
            return (errors < tight_conf[compression].gradient_threshold24);
        } else {
            errors = tight_detect_smooth_image32(vs, w, h);
        }
    } else {
        errors = tight_detect_smooth_image16(vs, w, h);
    }
    if (quality != -1) {
        return (errors < tight_conf[quality].jpeg_threshold);
    }
    return (errors < tight_conf[compression].gradient_threshold);
}

333 334 335 336 337 338 339 340 341
/*
 * Code to determine how many different colors used in rectangle.
 */
#define DEFINE_FILL_PALETTE_FUNCTION(bpp)                               \
                                                                        \
    static int                                                          \
    tight_fill_palette##bpp(VncState *vs, int x, int y,                 \
                            int max, size_t count,                      \
                            uint32_t *bg, uint32_t *fg,                 \
342
                            VncPalette **palette) {                     \
343 344 345 346
        uint##bpp##_t *data;                                            \
        uint##bpp##_t c0, c1, ci;                                       \
        int i, n0, n1;                                                  \
                                                                        \
347
        data = (uint##bpp##_t *)vs->tight.tight.buffer;                 \
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
                                                                        \
        c0 = data[0];                                                   \
        i = 1;                                                          \
        while (i < count && data[i] == c0)                              \
            i++;                                                        \
        if (i >= count) {                                               \
            *bg = *fg = c0;                                             \
            return 1;                                                   \
        }                                                               \
                                                                        \
        if (max < 2) {                                                  \
            return 0;                                                   \
        }                                                               \
                                                                        \
        n0 = i;                                                         \
        c1 = data[i];                                                   \
        n1 = 0;                                                         \
        for (i++; i < count; i++) {                                     \
            ci = data[i];                                               \
            if (ci == c0) {                                             \
                n0++;                                                   \
            } else if (ci == c1) {                                      \
                n1++;                                                   \
            } else                                                      \
                break;                                                  \
        }                                                               \
        if (i >= count) {                                               \
            if (n0 > n1) {                                              \
                *bg = (uint32_t)c0;                                     \
                *fg = (uint32_t)c1;                                     \
            } else {                                                    \
                *bg = (uint32_t)c1;                                     \
                *fg = (uint32_t)c0;                                     \
            }                                                           \
            return 2;                                                   \
        }                                                               \
                                                                        \
        if (max == 2) {                                                 \
            return 0;                                                   \
        }                                                               \
                                                                        \
389 390 391 392
        *palette = palette_new(max, bpp);                               \
        palette_put(*palette, c0);                                      \
        palette_put(*palette, c1);                                      \
        palette_put(*palette, ci);                                      \
393 394 395 396 397
                                                                        \
        for (i++; i < count; i++) {                                     \
            if (data[i] == ci) {                                        \
                continue;                                               \
            } else {                                                    \
398
                ci = data[i];                                           \
399
                if (!palette_put(*palette, (uint32_t)ci)) {             \
400 401 402 403 404
                    return 0;                                           \
                }                                                       \
            }                                                           \
        }                                                               \
                                                                        \
405
        return palette_size(*palette);                                  \
406 407 408 409 410 411 412 413
    }

DEFINE_FILL_PALETTE_FUNCTION(8)
DEFINE_FILL_PALETTE_FUNCTION(16)
DEFINE_FILL_PALETTE_FUNCTION(32)

static int tight_fill_palette(VncState *vs, int x, int y,
                              size_t count, uint32_t *bg, uint32_t *fg,
414
                              VncPalette **palette)
415 416 417
{
    int max;

418
    max = count / tight_conf[vs->tight.compression].idx_max_colors_divisor;
419
    if (max < 2 &&
420
        count >= tight_conf[vs->tight.compression].mono_min_rect_size) {
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
        max = 2;
    }
    if (max >= 256) {
        max = 256;
    }

    switch(vs->clientds.pf.bytes_per_pixel) {
    case 4:
        return tight_fill_palette32(vs, x, y, max, count, bg, fg, palette);
    case 2:
        return tight_fill_palette16(vs, x, y, max, count, bg, fg, palette);
    default:
        max = 2;
        return tight_fill_palette8(vs, x, y, max, count, bg, fg, palette);
    }
    return 0;
}

/*
 * Converting truecolor samples into palette indices.
 */
#define DEFINE_IDX_ENCODE_FUNCTION(bpp)                                 \
                                                                        \
    static void                                                         \
    tight_encode_indexed_rect##bpp(uint8_t *buf, int count,             \
446
                                   VncPalette *palette) {               \
447 448
        uint##bpp##_t *src;                                             \
        uint##bpp##_t rgb;                                              \
449
        int i, rep;                                                     \
450 451 452 453
        uint8_t idx;                                                    \
                                                                        \
        src = (uint##bpp##_t *) buf;                                    \
                                                                        \
454
        for (i = 0; i < count; i++) {                                   \
C
Corentin Chary 已提交
455
                                                                        \
456 457
            rgb = *src++;                                               \
            rep = 0;                                                    \
458 459
            while (i < count && *src == rgb) {                          \
                rep++, src++, i++;                                      \
460
            }                                                           \
461 462 463 464 465
            idx = palette_idx(palette, rgb);                            \
            /*                                                          \
             * Should never happen, but don't break everything          \
             * if it does, use the first color instead                  \
             */                                                         \
466
            if (idx == (uint8_t)-1) {                                   \
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
                idx = 0;                                                \
            }                                                           \
            while (rep >= 0) {                                          \
                *buf++ = idx;                                           \
                rep--;                                                  \
            }                                                           \
        }                                                               \
    }

DEFINE_IDX_ENCODE_FUNCTION(16)
DEFINE_IDX_ENCODE_FUNCTION(32)

#define DEFINE_MONO_ENCODE_FUNCTION(bpp)                                \
                                                                        \
    static void                                                         \
    tight_encode_mono_rect##bpp(uint8_t *buf, int w, int h,             \
                                uint##bpp##_t bg, uint##bpp##_t fg) {   \
        uint##bpp##_t *ptr;                                             \
        unsigned int value, mask;                                       \
        int aligned_width;                                              \
        int x, y, bg_bits;                                              \
                                                                        \
        ptr = (uint##bpp##_t *) buf;                                    \
        aligned_width = w - w % 8;                                      \
                                                                        \
        for (y = 0; y < h; y++) {                                       \
            for (x = 0; x < aligned_width; x += 8) {                    \
                for (bg_bits = 0; bg_bits < 8; bg_bits++) {             \
                    if (*ptr++ != bg) {                                 \
                        break;                                          \
                    }                                                   \
                }                                                       \
                if (bg_bits == 8) {                                     \
                    *buf++ = 0;                                         \
                    continue;                                           \
                }                                                       \
                mask = 0x80 >> bg_bits;                                 \
                value = mask;                                           \
                for (bg_bits++; bg_bits < 8; bg_bits++) {               \
                    mask >>= 1;                                         \
                    if (*ptr++ != bg) {                                 \
                        value |= mask;                                  \
                    }                                                   \
                }                                                       \
                *buf++ = (uint8_t)value;                                \
            }                                                           \
                                                                        \
            mask = 0x80;                                                \
            value = 0;                                                  \
            if (x >= w) {                                               \
                continue;                                               \
            }                                                           \
                                                                        \
            for (; x < w; x++) {                                        \
                if (*ptr++ != bg) {                                     \
                    value |= mask;                                      \
                }                                                       \
                mask >>= 1;                                             \
            }                                                           \
            *buf++ = (uint8_t)value;                                    \
        }                                                               \
    }

DEFINE_MONO_ENCODE_FUNCTION(8)
DEFINE_MONO_ENCODE_FUNCTION(16)
DEFINE_MONO_ENCODE_FUNCTION(32)

534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
/*
 * ``Gradient'' filter for 24-bit color samples.
 * Should be called only when redMax, greenMax and blueMax are 255.
 * Color components assumed to be byte-aligned.
 */

static void
tight_filter_gradient24(VncState *vs, uint8_t *buf, int w, int h)
{
    uint32_t *buf32;
    uint32_t pix32;
    int shift[3];
    int *prev;
    int here[3], upper[3], left[3], upperleft[3];
    int prediction;
    int x, y, c;

    buf32 = (uint32_t *)buf;
552
    memset(vs->tight.gradient.buffer, 0, w * 3 * sizeof(int));
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569

    if ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) ==
        (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG)) {
        shift[0] = vs->clientds.pf.rshift;
        shift[1] = vs->clientds.pf.gshift;
        shift[2] = vs->clientds.pf.bshift;
    } else {
        shift[0] = 24 - vs->clientds.pf.rshift;
        shift[1] = 24 - vs->clientds.pf.gshift;
        shift[2] = 24 - vs->clientds.pf.bshift;
    }

    for (y = 0; y < h; y++) {
        for (c = 0; c < 3; c++) {
            upper[c] = 0;
            here[c] = 0;
        }
570
        prev = (int *)vs->tight.gradient.buffer;
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
        for (x = 0; x < w; x++) {
            pix32 = *buf32++;
            for (c = 0; c < 3; c++) {
                upperleft[c] = upper[c];
                left[c] = here[c];
                upper[c] = *prev;
                here[c] = (int)(pix32 >> shift[c] & 0xFF);
                *prev++ = here[c];

                prediction = left[c] + upper[c] - upperleft[c];
                if (prediction < 0) {
                    prediction = 0;
                } else if (prediction > 0xFF) {
                    prediction = 0xFF;
                }
                *buf++ = (char)(here[c] - prediction);
            }
        }
    }
}


/*
 * ``Gradient'' filter for other color depths.
 */

#define DEFINE_GRADIENT_FILTER_FUNCTION(bpp)                            \
                                                                        \
    static void                                                         \
    tight_filter_gradient##bpp(VncState *vs, uint##bpp##_t *buf,        \
                               int w, int h) {                          \
        uint##bpp##_t pix, diff;                                        \
        bool endian;                                                    \
        int *prev;                                                      \
        int max[3], shift[3];                                           \
        int here[3], upper[3], left[3], upperleft[3];                   \
        int prediction;                                                 \
        int x, y, c;                                                    \
                                                                        \
610
        memset (vs->tight.gradient.buffer, 0, w * 3 * sizeof(int));     \
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
                                                                        \
        endian = ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) !=        \
                  (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG));     \
                                                                        \
        max[0] = vs->clientds.pf.rmax;                                  \
        max[1] = vs->clientds.pf.gmax;                                  \
        max[2] = vs->clientds.pf.bmax;                                  \
        shift[0] = vs->clientds.pf.rshift;                              \
        shift[1] = vs->clientds.pf.gshift;                              \
        shift[2] = vs->clientds.pf.bshift;                              \
                                                                        \
        for (y = 0; y < h; y++) {                                       \
            for (c = 0; c < 3; c++) {                                   \
                upper[c] = 0;                                           \
                here[c] = 0;                                            \
            }                                                           \
627
            prev = (int *)vs->tight.gradient.buffer;                    \
628 629 630
            for (x = 0; x < w; x++) {                                   \
                pix = *buf;                                             \
                if (endian) {                                           \
631
                    pix = bswap##bpp(pix);                              \
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
                }                                                       \
                diff = 0;                                               \
                for (c = 0; c < 3; c++) {                               \
                    upperleft[c] = upper[c];                            \
                    left[c] = here[c];                                  \
                    upper[c] = *prev;                                   \
                    here[c] = (int)(pix >> shift[c] & max[c]);          \
                    *prev++ = here[c];                                  \
                                                                        \
                    prediction = left[c] + upper[c] - upperleft[c];     \
                    if (prediction < 0) {                               \
                        prediction = 0;                                 \
                    } else if (prediction > max[c]) {                   \
                        prediction = max[c];                            \
                    }                                                   \
                    diff |= ((here[c] - prediction) & max[c])           \
                        << shift[c];                                    \
                }                                                       \
                if (endian) {                                           \
651
                    diff = bswap##bpp(diff);                            \
652 653 654 655 656 657 658 659 660
                }                                                       \
                *buf++ = diff;                                          \
            }                                                           \
        }                                                               \
    }

DEFINE_GRADIENT_FILTER_FUNCTION(16)
DEFINE_GRADIENT_FILTER_FUNCTION(32)

661 662 663
/*
 * Check if a rectangle is all of the same color. If needSameColor is
 * set to non-zero, then also check that its color equals to the
S
Stefan Weil 已提交
664
 * *colorPtr value. The result is 1 if the test is successful, and in
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
 * that case new color will be stored in *colorPtr.
 */

#define DEFINE_CHECK_SOLID_FUNCTION(bpp)                                \
                                                                        \
    static bool                                                         \
    check_solid_tile##bpp(VncState *vs, int x, int y, int w, int h,     \
                          uint32_t* color, bool samecolor)              \
    {                                                                   \
        VncDisplay *vd = vs->vd;                                        \
        uint##bpp##_t *fbptr;                                           \
        uint##bpp##_t c;                                                \
        int dx, dy;                                                     \
                                                                        \
        fbptr = (uint##bpp##_t *)                                       \
            (vd->server->data + y * ds_get_linesize(vs->ds) +           \
             x * ds_get_bytes_per_pixel(vs->ds));                       \
                                                                        \
        c = *fbptr;                                                     \
        if (samecolor && (uint32_t)c != *color) {                       \
            return false;                                               \
        }                                                               \
                                                                        \
        for (dy = 0; dy < h; dy++) {                                    \
            for (dx = 0; dx < w; dx++) {                                \
                if (c != fbptr[dx]) {                                   \
                    return false;                                       \
                }                                                       \
            }                                                           \
            fbptr = (uint##bpp##_t *)                                   \
                ((uint8_t *)fbptr + ds_get_linesize(vs->ds));           \
        }                                                               \
                                                                        \
        *color = (uint32_t)c;                                           \
        return true;                                                    \
    }

DEFINE_CHECK_SOLID_FUNCTION(32)
DEFINE_CHECK_SOLID_FUNCTION(16)
DEFINE_CHECK_SOLID_FUNCTION(8)

static bool check_solid_tile(VncState *vs, int x, int y, int w, int h,
                             uint32_t* color, bool samecolor)
{
    VncDisplay *vd = vs->vd;

    switch(vd->server->pf.bytes_per_pixel) {
    case 4:
        return check_solid_tile32(vs, x, y, w, h, color, samecolor);
    case 2:
        return check_solid_tile16(vs, x, y, w, h, color, samecolor);
    default:
        return check_solid_tile8(vs, x, y, w, h, color, samecolor);
    }
}

static void find_best_solid_area(VncState *vs, int x, int y, int w, int h,
                                 uint32_t color, int *w_ptr, int *h_ptr)
{
    int dx, dy, dw, dh;
    int w_prev;
    int w_best = 0, h_best = 0;

    w_prev = w;

    for (dy = y; dy < y + h; dy += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) {

        dh = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, y + h - dy);
        dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, w_prev);

        if (!check_solid_tile(vs, x, dy, dw, dh, &color, true)) {
            break;
        }

        for (dx = x + dw; dx < x + w_prev;) {
            dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, x + w_prev - dx);

            if (!check_solid_tile(vs, dx, dy, dw, dh, &color, true)) {
                break;
            }
            dx += dw;
        }

        w_prev = dx - x;
        if (w_prev * (dy + dh - y) > w_best * h_best) {
            w_best = w_prev;
            h_best = dy + dh - y;
        }
    }

    *w_ptr = w_best;
    *h_ptr = h_best;
}

static void extend_solid_area(VncState *vs, int x, int y, int w, int h,
                              uint32_t color, int *x_ptr, int *y_ptr,
                              int *w_ptr, int *h_ptr)
{
    int cx, cy;

    /* Try to extend the area upwards. */
    for ( cy = *y_ptr - 1;
          cy >= y && check_solid_tile(vs, *x_ptr, cy, *w_ptr, 1, &color, true);
          cy-- );
    *h_ptr += *y_ptr - (cy + 1);
    *y_ptr = cy + 1;

    /* ... downwards. */
    for ( cy = *y_ptr + *h_ptr;
          cy < y + h &&
              check_solid_tile(vs, *x_ptr, cy, *w_ptr, 1, &color, true);
          cy++ );
    *h_ptr += cy - (*y_ptr + *h_ptr);

    /* ... to the left. */
    for ( cx = *x_ptr - 1;
          cx >= x && check_solid_tile(vs, cx, *y_ptr, 1, *h_ptr, &color, true);
          cx-- );
    *w_ptr += *x_ptr - (cx + 1);
    *x_ptr = cx + 1;

    /* ... to the right. */
    for ( cx = *x_ptr + *w_ptr;
          cx < x + w &&
              check_solid_tile(vs, cx, *y_ptr, 1, *h_ptr, &color, true);
          cx++ );
    *w_ptr += cx - (*x_ptr + *w_ptr);
}

C
Corentin Chary 已提交
794 795 796
static int tight_init_stream(VncState *vs, int stream_id,
                             int level, int strategy)
{
797
    z_streamp zstream = &vs->tight.stream[stream_id];
C
Corentin Chary 已提交
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814

    if (zstream->opaque == NULL) {
        int err;

        VNC_DEBUG("VNC: TIGHT: initializing zlib stream %d\n", stream_id);
        VNC_DEBUG("VNC: TIGHT: opaque = %p | vs = %p\n", zstream->opaque, vs);
        zstream->zalloc = vnc_zlib_zalloc;
        zstream->zfree = vnc_zlib_zfree;

        err = deflateInit2(zstream, level, Z_DEFLATED, MAX_WBITS,
                           MAX_MEM_LEVEL, strategy);

        if (err != Z_OK) {
            fprintf(stderr, "VNC: error initializing zlib\n");
            return -1;
        }

815
        vs->tight.levels[stream_id] = level;
C
Corentin Chary 已提交
816 817 818
        zstream->opaque = vs;
    }

819
    if (vs->tight.levels[stream_id] != level) {
C
Corentin Chary 已提交
820 821 822
        if (deflateParams(zstream, level, strategy) != Z_OK) {
            return -1;
        }
823
        vs->tight.levels[stream_id] = level;
C
Corentin Chary 已提交
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
    }
    return 0;
}

static void tight_send_compact_size(VncState *vs, size_t len)
{
    int lpc = 0;
    int bytes = 0;
    char buf[3] = {0, 0, 0};

    buf[bytes++] = len & 0x7F;
    if (len > 0x7F) {
        buf[bytes-1] |= 0x80;
        buf[bytes++] = (len >> 7) & 0x7F;
        if (len > 0x3FFF) {
            buf[bytes-1] |= 0x80;
            buf[bytes++] = (len >> 14) & 0xFF;
        }
    }
843
    for (lpc = 0; lpc < bytes; lpc++) {
C
Corentin Chary 已提交
844 845 846 847 848 849 850
        vnc_write_u8(vs, buf[lpc]);
    }
}

static int tight_compress_data(VncState *vs, int stream_id, size_t bytes,
                               int level, int strategy)
{
851
    z_streamp zstream = &vs->tight.stream[stream_id];
C
Corentin Chary 已提交
852 853 854
    int previous_out;

    if (bytes < VNC_TIGHT_MIN_TO_COMPRESS) {
855
        vnc_write(vs, vs->tight.tight.buffer, vs->tight.tight.offset);
C
Corentin Chary 已提交
856 857 858 859 860 861 862 863
        return bytes;
    }

    if (tight_init_stream(vs, stream_id, level, strategy)) {
        return -1;
    }

    /* reserve memory in output buffer */
864
    buffer_reserve(&vs->tight.zlib, bytes + 64);
C
Corentin Chary 已提交
865 866

    /* set pointers */
867 868 869 870
    zstream->next_in = vs->tight.tight.buffer;
    zstream->avail_in = vs->tight.tight.offset;
    zstream->next_out = vs->tight.zlib.buffer + vs->tight.zlib.offset;
    zstream->avail_out = vs->tight.zlib.capacity - vs->tight.zlib.offset;
C
Corentin Chary 已提交
871 872 873 874 875 876 877 878 879
    zstream->data_type = Z_BINARY;
    previous_out = zstream->total_out;

    /* start encoding */
    if (deflate(zstream, Z_SYNC_FLUSH) != Z_OK) {
        fprintf(stderr, "VNC: error during tight compression\n");
        return -1;
    }

880
    vs->tight.zlib.offset = vs->tight.zlib.capacity - zstream->avail_out;
C
Corentin Chary 已提交
881 882 883
    bytes = zstream->total_out - previous_out;

    tight_send_compact_size(vs, bytes);
884
    vnc_write(vs, vs->tight.zlib.buffer, bytes);
C
Corentin Chary 已提交
885

886
    buffer_reset(&vs->tight.zlib);
C
Corentin Chary 已提交
887 888 889 890 891 892 893

    return bytes;
}

/*
 * Subencoding implementations.
 */
894
static void tight_pack24(VncState *vs, uint8_t *buf, size_t count, size_t *ret)
C
Corentin Chary 已提交
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
{
    uint32_t *buf32;
    uint32_t pix;
    int rshift, gshift, bshift;

    buf32 = (uint32_t *)buf;

    if ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) ==
        (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG)) {
        rshift = vs->clientds.pf.rshift;
        gshift = vs->clientds.pf.gshift;
        bshift = vs->clientds.pf.bshift;
    } else {
        rshift = 24 - vs->clientds.pf.rshift;
        gshift = 24 - vs->clientds.pf.gshift;
        bshift = 24 - vs->clientds.pf.bshift;
    }

913 914 915
    if (ret) {
        *ret = count * 3;
    }
C
Corentin Chary 已提交
916 917 918 919 920 921 922 923 924

    while (count--) {
        pix = *buf32++;
        *buf++ = (char)(pix >> rshift);
        *buf++ = (char)(pix >> gshift);
        *buf++ = (char)(pix >> bshift);
    }
}

C
Corentin Chary 已提交
925
static int send_full_color_rect(VncState *vs, int x, int y, int w, int h)
C
Corentin Chary 已提交
926 927
{
    int stream = 0;
928
    ssize_t bytes;
C
Corentin Chary 已提交
929

C
Corentin Chary 已提交
930 931 932 933 934 935
#ifdef CONFIG_VNC_PNG
    if (tight_can_send_png_rect(vs, w, h)) {
        return send_png_rect(vs, x, y, w, h, NULL);
    }
#endif

C
Corentin Chary 已提交
936 937
    vnc_write_u8(vs, stream << 4); /* no flushing, no filter */

938 939
    if (vs->tight.pixel24) {
        tight_pack24(vs, vs->tight.tight.buffer, w * h, &vs->tight.tight.offset);
C
Corentin Chary 已提交
940 941 942 943 944 945
        bytes = 3;
    } else {
        bytes = vs->clientds.pf.bytes_per_pixel;
    }

    bytes = tight_compress_data(vs, stream, w * h * bytes,
946
                                tight_conf[vs->tight.compression].raw_zlib_level,
C
Corentin Chary 已提交
947 948 949 950 951
                                Z_DEFAULT_STRATEGY);

    return (bytes >= 0);
}

952 953 954 955 956 957
static int send_solid_rect(VncState *vs)
{
    size_t bytes;

    vnc_write_u8(vs, VNC_TIGHT_FILL << 4); /* no flushing, no filter */

958 959
    if (vs->tight.pixel24) {
        tight_pack24(vs, vs->tight.tight.buffer, 1, &vs->tight.tight.offset);
960 961 962 963 964
        bytes = 3;
    } else {
        bytes = vs->clientds.pf.bytes_per_pixel;
    }

965
    vnc_write(vs, vs->tight.tight.buffer, bytes);
966 967 968
    return 1;
}

C
Corentin Chary 已提交
969 970
static int send_mono_rect(VncState *vs, int x, int y,
                          int w, int h, uint32_t bg, uint32_t fg)
971
{
972
    ssize_t bytes;
973
    int stream = 1;
974
    int level = tight_conf[vs->tight.compression].mono_zlib_level;
975

C
Corentin Chary 已提交
976 977 978 979
#ifdef CONFIG_VNC_PNG
    if (tight_can_send_png_rect(vs, w, h)) {
        int ret;
        int bpp = vs->clientds.pf.bytes_per_pixel * 8;
980
        VncPalette *palette = palette_new(2, bpp);
C
Corentin Chary 已提交
981

982 983
        palette_put(palette, bg);
        palette_put(palette, fg);
C
Corentin Chary 已提交
984
        ret = send_png_rect(vs, x, y, w, h, palette);
985
        palette_destroy(palette);
C
Corentin Chary 已提交
986 987 988 989
        return ret;
    }
#endif

990 991 992 993 994 995 996 997 998 999 1000 1001
    bytes = ((w + 7) / 8) * h;

    vnc_write_u8(vs, (stream | VNC_TIGHT_EXPLICIT_FILTER) << 4);
    vnc_write_u8(vs, VNC_TIGHT_FILTER_PALETTE);
    vnc_write_u8(vs, 1);

    switch(vs->clientds.pf.bytes_per_pixel) {
    case 4:
    {
        uint32_t buf[2] = {bg, fg};
        size_t ret = sizeof (buf);

1002
        if (vs->tight.pixel24) {
1003 1004 1005 1006
            tight_pack24(vs, (unsigned char*)buf, 2, &ret);
        }
        vnc_write(vs, buf, ret);

1007
        tight_encode_mono_rect32(vs->tight.tight.buffer, w, h, bg, fg);
1008 1009 1010 1011 1012
        break;
    }
    case 2:
        vnc_write(vs, &bg, 2);
        vnc_write(vs, &fg, 2);
1013
        tight_encode_mono_rect16(vs->tight.tight.buffer, w, h, bg, fg);
1014 1015 1016 1017
        break;
    default:
        vnc_write_u8(vs, bg);
        vnc_write_u8(vs, fg);
1018
        tight_encode_mono_rect8(vs->tight.tight.buffer, w, h, bg, fg);
1019 1020
        break;
    }
1021
    vs->tight.tight.offset = bytes;
1022 1023 1024 1025 1026 1027 1028 1029

    bytes = tight_compress_data(vs, stream, bytes, level, Z_DEFAULT_STRATEGY);
    return (bytes >= 0);
}

struct palette_cb_priv {
    VncState *vs;
    uint8_t *header;
C
Corentin Chary 已提交
1030 1031 1032
#ifdef CONFIG_VNC_PNG
    png_colorp png_palette;
#endif
1033 1034
};

1035
static void write_palette(int idx, uint32_t color, void *opaque)
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
{
    struct palette_cb_priv *priv = opaque;
    VncState *vs = priv->vs;
    uint32_t bytes = vs->clientds.pf.bytes_per_pixel;

    if (bytes == 4) {
        ((uint32_t*)priv->header)[idx] = color;
    } else {
        ((uint16_t*)priv->header)[idx] = color;
    }
}

C
Corentin Chary 已提交
1048
static bool send_gradient_rect(VncState *vs, int x, int y, int w, int h)
1049 1050
{
    int stream = 3;
1051
    int level = tight_conf[vs->tight.compression].gradient_zlib_level;
1052
    ssize_t bytes;
1053 1054

    if (vs->clientds.pf.bytes_per_pixel == 1)
C
Corentin Chary 已提交
1055
        return send_full_color_rect(vs, x, y, w, h);
1056 1057 1058 1059

    vnc_write_u8(vs, (stream | VNC_TIGHT_EXPLICIT_FILTER) << 4);
    vnc_write_u8(vs, VNC_TIGHT_FILTER_GRADIENT);

1060
    buffer_reserve(&vs->tight.gradient, w * 3 * sizeof (int));
1061

1062 1063
    if (vs->tight.pixel24) {
        tight_filter_gradient24(vs, vs->tight.tight.buffer, w, h);
1064 1065
        bytes = 3;
    } else if (vs->clientds.pf.bytes_per_pixel == 4) {
1066
        tight_filter_gradient32(vs, (uint32_t *)vs->tight.tight.buffer, w, h);
1067 1068
        bytes = 4;
    } else {
1069
        tight_filter_gradient16(vs, (uint16_t *)vs->tight.tight.buffer, w, h);
1070 1071 1072
        bytes = 2;
    }

1073
    buffer_reset(&vs->tight.gradient);
1074 1075

    bytes = w * h * bytes;
1076
    vs->tight.tight.offset = bytes;
1077 1078 1079 1080 1081 1082

    bytes = tight_compress_data(vs, stream, bytes,
                                level, Z_FILTERED);
    return (bytes >= 0);
}

C
Corentin Chary 已提交
1083
static int send_palette_rect(VncState *vs, int x, int y,
1084
                             int w, int h, VncPalette *palette)
1085 1086
{
    int stream = 2;
1087
    int level = tight_conf[vs->tight.compression].idx_zlib_level;
1088
    int colors;
1089
    ssize_t bytes;
1090

C
Corentin Chary 已提交
1091 1092 1093 1094 1095 1096
#ifdef CONFIG_VNC_PNG
    if (tight_can_send_png_rect(vs, w, h)) {
        return send_png_rect(vs, x, y, w, h, palette);
    }
#endif

1097
    colors = palette_size(palette);
1098 1099 1100 1101 1102 1103 1104 1105 1106

    vnc_write_u8(vs, (stream | VNC_TIGHT_EXPLICIT_FILTER) << 4);
    vnc_write_u8(vs, VNC_TIGHT_FILTER_PALETTE);
    vnc_write_u8(vs, colors - 1);

    switch(vs->clientds.pf.bytes_per_pixel) {
    case 4:
    {
        size_t old_offset, offset;
1107
        uint32_t header[palette_size(palette)];
1108 1109 1110
        struct palette_cb_priv priv = { vs, (uint8_t *)header };

        old_offset = vs->output.offset;
1111
        palette_iter(palette, write_palette, &priv);
1112 1113
        vnc_write(vs, header, sizeof(header));

1114
        if (vs->tight.pixel24) {
1115 1116 1117 1118
            tight_pack24(vs, vs->output.buffer + old_offset, colors, &offset);
            vs->output.offset = old_offset + offset;
        }

1119
        tight_encode_indexed_rect32(vs->tight.tight.buffer, w * h, palette);
1120 1121 1122 1123
        break;
    }
    case 2:
    {
1124
        uint16_t header[palette_size(palette)];
1125 1126
        struct palette_cb_priv priv = { vs, (uint8_t *)header };

1127
        palette_iter(palette, write_palette, &priv);
1128
        vnc_write(vs, header, sizeof(header));
1129
        tight_encode_indexed_rect16(vs->tight.tight.buffer, w * h, palette);
1130 1131 1132 1133 1134 1135 1136
        break;
    }
    default:
        return -1; /* No palette for 8bits colors */
        break;
    }
    bytes = w * h;
1137
    vs->tight.tight.offset = bytes;
1138 1139 1140 1141 1142 1143

    bytes = tight_compress_data(vs, stream, bytes,
                                level, Z_DEFAULT_STRATEGY);
    return (bytes >= 0);
}

C
Corentin Chary 已提交
1144 1145 1146
#if defined(CONFIG_VNC_JPEG) || defined(CONFIG_VNC_PNG)
static void rgb_prepare_row24(VncState *vs, uint8_t *dst, int x, int y,
                              int count)
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
{
    VncDisplay *vd = vs->vd;
    uint32_t *fbptr;
    uint32_t pix;

    fbptr = (uint32_t *)(vd->server->data + y * ds_get_linesize(vs->ds) +
                         x * ds_get_bytes_per_pixel(vs->ds));

    while (count--) {
        pix = *fbptr++;
        *dst++ = (uint8_t)(pix >> vs->ds->surface->pf.rshift);
        *dst++ = (uint8_t)(pix >> vs->ds->surface->pf.gshift);
        *dst++ = (uint8_t)(pix >> vs->ds->surface->pf.bshift);
    }
}

C
Corentin Chary 已提交
1163
#define DEFINE_RGB_GET_ROW_FUNCTION(bpp)                                \
1164 1165
                                                                        \
    static void                                                         \
C
Corentin Chary 已提交
1166 1167
    rgb_prepare_row##bpp(VncState *vs, uint8_t *dst,                    \
                         int x, int y, int count)                       \
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
    {                                                                   \
        VncDisplay *vd = vs->vd;                                        \
        uint##bpp##_t *fbptr;                                           \
        uint##bpp##_t pix;                                              \
        int r, g, b;                                                    \
                                                                        \
        fbptr = (uint##bpp##_t *)                                       \
            (vd->server->data + y * ds_get_linesize(vs->ds) +           \
             x * ds_get_bytes_per_pixel(vs->ds));                       \
                                                                        \
        while (count--) {                                               \
            pix = *fbptr++;                                             \
                                                                        \
            r = (int)((pix >> vs->ds->surface->pf.rshift)               \
                      & vs->ds->surface->pf.rmax);                      \
            g = (int)((pix >> vs->ds->surface->pf.gshift)               \
                      & vs->ds->surface->pf.gmax);                      \
            b = (int)((pix >> vs->ds->surface->pf.bshift)               \
                      & vs->ds->surface->pf.bmax);                      \
                                                                        \
            *dst++ = (uint8_t)((r * 255 + vs->ds->surface->pf.rmax / 2) \
                               / vs->ds->surface->pf.rmax);             \
            *dst++ = (uint8_t)((g * 255 + vs->ds->surface->pf.gmax / 2) \
                               / vs->ds->surface->pf.gmax);             \
            *dst++ = (uint8_t)((b * 255 + vs->ds->surface->pf.bmax / 2) \
                               / vs->ds->surface->pf.bmax);             \
        }                                                               \
    }

C
Corentin Chary 已提交
1197 1198
DEFINE_RGB_GET_ROW_FUNCTION(16)
DEFINE_RGB_GET_ROW_FUNCTION(32)
1199

C
Corentin Chary 已提交
1200 1201
static void rgb_prepare_row(VncState *vs, uint8_t *dst, int x, int y,
                            int count)
1202
{
C
Corentin Chary 已提交
1203 1204 1205 1206 1207 1208 1209 1210 1211
    if (ds_get_bytes_per_pixel(vs->ds) == 4) {
        if (vs->ds->surface->pf.rmax == 0xFF &&
            vs->ds->surface->pf.gmax == 0xFF &&
            vs->ds->surface->pf.bmax == 0xFF) {
            rgb_prepare_row24(vs, dst, x, y, count);
        } else {
            rgb_prepare_row32(vs, dst, x, y, count);
        }
    } else {
C
Corentin Chary 已提交
1212
        rgb_prepare_row16(vs, dst, x, y, count);
C
Corentin Chary 已提交
1213
    }
1214
}
C
Corentin Chary 已提交
1215
#endif /* CONFIG_VNC_JPEG or CONFIG_VNC_PNG */
1216

C
Corentin Chary 已提交
1217 1218 1219 1220
/*
 * JPEG compression stuff.
 */
#ifdef CONFIG_VNC_JPEG
1221 1222 1223 1224 1225 1226 1227 1228
/*
 * Destination manager implementation for JPEG library.
 */

/* This is called once per encoding */
static void jpeg_init_destination(j_compress_ptr cinfo)
{
    VncState *vs = cinfo->client_data;
1229
    Buffer *buffer = &vs->tight.jpeg;
1230 1231 1232 1233 1234 1235 1236 1237 1238

    cinfo->dest->next_output_byte = (JOCTET *)buffer->buffer + buffer->offset;
    cinfo->dest->free_in_buffer = (size_t)(buffer->capacity - buffer->offset);
}

/* This is called when we ran out of buffer (shouldn't happen!) */
static boolean jpeg_empty_output_buffer(j_compress_ptr cinfo)
{
    VncState *vs = cinfo->client_data;
1239
    Buffer *buffer = &vs->tight.jpeg;
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250

    buffer->offset = buffer->capacity;
    buffer_reserve(buffer, 2048);
    jpeg_init_destination(cinfo);
    return TRUE;
}

/* This is called when we are done processing data */
static void jpeg_term_destination(j_compress_ptr cinfo)
{
    VncState *vs = cinfo->client_data;
1251
    Buffer *buffer = &vs->tight.jpeg;
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265

    buffer->offset = buffer->capacity - cinfo->dest->free_in_buffer;
}

static int send_jpeg_rect(VncState *vs, int x, int y, int w, int h, int quality)
{
    struct jpeg_compress_struct cinfo;
    struct jpeg_error_mgr jerr;
    struct jpeg_destination_mgr manager;
    JSAMPROW row[1];
    uint8_t *buf;
    int dy;

    if (ds_get_bytes_per_pixel(vs->ds) == 1)
C
Corentin Chary 已提交
1266
        return send_full_color_rect(vs, x, y, w, h);
1267

1268
    buffer_reserve(&vs->tight.jpeg, 2048);
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288

    cinfo.err = jpeg_std_error(&jerr);
    jpeg_create_compress(&cinfo);

    cinfo.client_data = vs;
    cinfo.image_width = w;
    cinfo.image_height = h;
    cinfo.input_components = 3;
    cinfo.in_color_space = JCS_RGB;

    jpeg_set_defaults(&cinfo);
    jpeg_set_quality(&cinfo, quality, true);

    manager.init_destination = jpeg_init_destination;
    manager.empty_output_buffer = jpeg_empty_output_buffer;
    manager.term_destination = jpeg_term_destination;
    cinfo.dest = &manager;

    jpeg_start_compress(&cinfo, true);

1289 1290
    buf = qemu_malloc(w * 3);
    row[0] = buf;
1291
    for (dy = 0; dy < h; dy++) {
C
Corentin Chary 已提交
1292
        rgb_prepare_row(vs, buf, x, y + dy, w);
1293 1294
        jpeg_write_scanlines(&cinfo, row, 1);
    }
1295
    qemu_free(buf);
1296 1297 1298 1299 1300 1301

    jpeg_finish_compress(&cinfo);
    jpeg_destroy_compress(&cinfo);

    vnc_write_u8(vs, VNC_TIGHT_JPEG << 4);

1302 1303 1304
    tight_send_compact_size(vs, vs->tight.jpeg.offset);
    vnc_write(vs, vs->tight.jpeg.buffer, vs->tight.jpeg.offset);
    buffer_reset(&vs->tight.jpeg);
1305 1306 1307 1308 1309

    return 1;
}
#endif /* CONFIG_VNC_JPEG */

C
Corentin Chary 已提交
1310 1311 1312 1313
/*
 * PNG compression stuff.
 */
#ifdef CONFIG_VNC_PNG
1314
static void write_png_palette(int idx, uint32_t pix, void *opaque)
C
Corentin Chary 已提交
1315 1316 1317 1318 1319
{
    struct palette_cb_priv *priv = opaque;
    VncState *vs = priv->vs;
    png_colorp color = &priv->png_palette[idx];

1320
    if (vs->tight.pixel24)
C
Corentin Chary 已提交
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
    {
        color->red = (pix >> vs->clientds.pf.rshift) & vs->clientds.pf.rmax;
        color->green = (pix >> vs->clientds.pf.gshift) & vs->clientds.pf.gmax;
        color->blue = (pix >> vs->clientds.pf.bshift) & vs->clientds.pf.bmax;
    }
    else
    {
        int red, green, blue;

        red = (pix >> vs->clientds.pf.rshift) & vs->clientds.pf.rmax;
        green = (pix >> vs->clientds.pf.gshift) & vs->clientds.pf.gmax;
        blue = (pix >> vs->clientds.pf.bshift) & vs->clientds.pf.bmax;
        color->red = ((red * 255 + vs->clientds.pf.rmax / 2) /
                      vs->clientds.pf.rmax);
        color->green = ((green * 255 + vs->clientds.pf.gmax / 2) /
                        vs->clientds.pf.gmax);
        color->blue = ((blue * 255 + vs->clientds.pf.bmax / 2) /
                       vs->clientds.pf.bmax);
    }
}

static void png_write_data(png_structp png_ptr, png_bytep data,
                           png_size_t length)
{
    VncState *vs = png_get_io_ptr(png_ptr);

1347 1348
    buffer_reserve(&vs->tight.png, vs->tight.png.offset + length);
    memcpy(vs->tight.png.buffer + vs->tight.png.offset, data, length);
C
Corentin Chary 已提交
1349

1350
    vs->tight.png.offset += length;
C
Corentin Chary 已提交
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
}

static void png_flush_data(png_structp png_ptr)
{
}

static void *vnc_png_malloc(png_structp png_ptr, png_size_t size)
{
    return qemu_malloc(size);
}

static void vnc_png_free(png_structp png_ptr, png_voidp ptr)
{
    qemu_free(ptr);
}

static int send_png_rect(VncState *vs, int x, int y, int w, int h,
1368
                         VncPalette *palette)
C
Corentin Chary 已提交
1369 1370 1371 1372 1373
{
    png_byte color_type;
    png_structp png_ptr;
    png_infop info_ptr;
    png_colorp png_palette = NULL;
1374 1375
    int level = tight_png_conf[vs->tight.compression].png_zlib_level;
    int filters = tight_png_conf[vs->tight.compression].png_filters;
C
Corentin Chary 已提交
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
    uint8_t *buf;
    int dy;

    png_ptr = png_create_write_struct_2(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL,
                                        NULL, vnc_png_malloc, vnc_png_free);

    if (png_ptr == NULL)
        return -1;

    info_ptr = png_create_info_struct(png_ptr);

    if (info_ptr == NULL) {
        png_destroy_write_struct(&png_ptr, NULL);
        return -1;
    }

    png_set_write_fn(png_ptr, (void *) vs, png_write_data, png_flush_data);
    png_set_compression_level(png_ptr, level);
1394
    png_set_filter(png_ptr, PNG_FILTER_TYPE_DEFAULT, filters);
C
Corentin Chary 已提交
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409

    if (palette) {
        color_type = PNG_COLOR_TYPE_PALETTE;
    } else {
        color_type = PNG_COLOR_TYPE_RGB;
    }

    png_set_IHDR(png_ptr, info_ptr, w, h,
                 8, color_type, PNG_INTERLACE_NONE,
                 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);

    if (color_type == PNG_COLOR_TYPE_PALETTE) {
        struct palette_cb_priv priv;

        png_palette = png_malloc(png_ptr, sizeof(*png_palette) *
1410
                                 palette_size(palette));
C
Corentin Chary 已提交
1411 1412 1413

        priv.vs = vs;
        priv.png_palette = png_palette;
1414
        palette_iter(palette, write_png_palette, &priv);
C
Corentin Chary 已提交
1415

1416
        png_set_PLTE(png_ptr, info_ptr, png_palette, palette_size(palette));
C
Corentin Chary 已提交
1417 1418

        if (vs->clientds.pf.bytes_per_pixel == 4) {
1419
            tight_encode_indexed_rect32(vs->tight.tight.buffer, w * h, palette);
C
Corentin Chary 已提交
1420
        } else {
1421
            tight_encode_indexed_rect16(vs->tight.tight.buffer, w * h, palette);
C
Corentin Chary 已提交
1422 1423 1424 1425 1426
        }
    }

    png_write_info(png_ptr, info_ptr);

1427
    buffer_reserve(&vs->tight.png, 2048);
C
Corentin Chary 已提交
1428 1429 1430 1431
    buf = qemu_malloc(w * 3);
    for (dy = 0; dy < h; dy++)
    {
        if (color_type == PNG_COLOR_TYPE_PALETTE) {
1432
            memcpy(buf, vs->tight.tight.buffer + (dy * w), w);
C
Corentin Chary 已提交
1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449
        } else {
            rgb_prepare_row(vs, buf, x, y + dy, w);
        }
        png_write_row(png_ptr, buf);
    }
    qemu_free(buf);

    png_write_end(png_ptr, NULL);

    if (color_type == PNG_COLOR_TYPE_PALETTE) {
        png_free(png_ptr, png_palette);
    }

    png_destroy_write_struct(&png_ptr, &info_ptr);

    vnc_write_u8(vs, VNC_TIGHT_PNG << 4);

1450 1451 1452
    tight_send_compact_size(vs, vs->tight.png.offset);
    vnc_write(vs, vs->tight.png.buffer, vs->tight.png.offset);
    buffer_reset(&vs->tight.png);
C
Corentin Chary 已提交
1453 1454 1455 1456
    return 1;
}
#endif /* CONFIG_VNC_PNG */

C
Corentin Chary 已提交
1457 1458
static void vnc_tight_start(VncState *vs)
{
1459
    buffer_reset(&vs->tight.tight);
C
Corentin Chary 已提交
1460 1461

    // make the output buffer be the zlib buffer, so we can compress it later
1462 1463
    vs->tight.tmp = vs->output;
    vs->output = vs->tight.tight;
C
Corentin Chary 已提交
1464 1465 1466 1467 1468
}

static void vnc_tight_stop(VncState *vs)
{
    // switch back to normal output/zlib buffers
1469 1470
    vs->tight.tight = vs->output;
    vs->output = vs->tight.tmp;
C
Corentin Chary 已提交
1471 1472
}

C
Corentin Chary 已提交
1473 1474
static int send_sub_rect_nojpeg(VncState *vs, int x, int y, int w, int h,
                                int bg, int fg, int colors, VncPalette *palette)
C
Corentin Chary 已提交
1475
{
C
Corentin Chary 已提交
1476
    int ret;
C
Corentin Chary 已提交
1477

C
Corentin Chary 已提交
1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489
    if (colors == 0) {
        if (tight_detect_smooth_image(vs, w, h)) {
            ret = send_gradient_rect(vs, x, y, w, h);
        } else {
            ret = send_full_color_rect(vs, x, y, w, h);
        }
    } else if (colors == 1) {
        ret = send_solid_rect(vs);
    } else if (colors == 2) {
        ret = send_mono_rect(vs, x, y, w, h, bg, fg);
    } else if (colors <= 256) {
        ret = send_palette_rect(vs, x, y, w, h, palette);
B
Blue Swirl 已提交
1490 1491
    } else {
        ret = 0;
C
Corentin Chary 已提交
1492 1493 1494
    }
    return ret;
}
C
Corentin Chary 已提交
1495

C
Corentin Chary 已提交
1496 1497 1498
#ifdef CONFIG_VNC_JPEG
static int send_sub_rect_jpeg(VncState *vs, int x, int y, int w, int h,
                              int bg, int fg, int colors,
1499
                              VncPalette *palette, bool force)
C
Corentin Chary 已提交
1500 1501
{
    int ret;
1502 1503

    if (colors == 0) {
1504 1505
        if (force || (tight_jpeg_conf[vs->tight.quality].jpeg_full &&
                      tight_detect_smooth_image(vs, w, h))) {
C
Corentin Chary 已提交
1506
            int quality = tight_conf[vs->tight.quality].jpeg_quality;
1507

C
Corentin Chary 已提交
1508
            ret = send_jpeg_rect(vs, x, y, w, h, quality);
1509
        } else {
C
Corentin Chary 已提交
1510
            ret = send_full_color_rect(vs, x, y, w, h);
1511
        }
1512 1513 1514
    } else if (colors == 1) {
        ret = send_solid_rect(vs);
    } else if (colors == 2) {
C
Corentin Chary 已提交
1515
        ret = send_mono_rect(vs, x, y, w, h, bg, fg);
1516
    } else if (colors <= 256) {
1517 1518 1519
        if (force || (colors > 96 &&
                      tight_jpeg_conf[vs->tight.quality].jpeg_idx &&
                      tight_detect_smooth_image(vs, w, h))) {
1520
            int quality = tight_conf[vs->tight.quality].jpeg_quality;
1521 1522 1523

            ret = send_jpeg_rect(vs, x, y, w, h, quality);
        } else {
C
Corentin Chary 已提交
1524
            ret = send_palette_rect(vs, x, y, w, h, palette);
1525
        }
B
Blue Swirl 已提交
1526 1527
    } else {
        ret = 0;
C
Corentin Chary 已提交
1528 1529 1530
    }
    return ret;
}
1531
#endif
C
Corentin Chary 已提交
1532 1533 1534 1535 1536 1537 1538

static int send_sub_rect(VncState *vs, int x, int y, int w, int h)
{
    VncPalette *palette = NULL;
    uint32_t bg = 0, fg = 0;
    int colors;
    int ret = 0;
1539
#ifdef CONFIG_VNC_JPEG
1540 1541
    bool force_jpeg = false;
    bool allow_jpeg = true;
1542
#endif
C
Corentin Chary 已提交
1543 1544 1545 1546 1547 1548 1549

    vnc_framebuffer_update(vs, x, y, w, h, vs->tight.type);

    vnc_tight_start(vs);
    vnc_raw_send_framebuffer_update(vs, x, y, w, h);
    vnc_tight_stop(vs);

1550
#ifdef CONFIG_VNC_JPEG
C
Corentin Chary 已提交
1551
    if (!vs->vd->non_adaptive && vs->tight.quality != (uint8_t)-1) {
1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563
        double freq = vnc_update_freq(vs, x, y, w, h);

        if (freq < tight_jpeg_conf[vs->tight.quality].jpeg_freq_min) {
            allow_jpeg = false;
        }
        if (freq >= tight_jpeg_conf[vs->tight.quality].jpeg_freq_threshold) {
            force_jpeg = true;
            vnc_sent_lossy_rect(vs, x, y, w, h);
        }
    }
#endif

C
Corentin Chary 已提交
1564 1565 1566
    colors = tight_fill_palette(vs, x, y, w * h, &fg, &bg, &palette);

#ifdef CONFIG_VNC_JPEG
1567 1568 1569
    if (allow_jpeg && vs->tight.quality != (uint8_t)-1) {
        ret = send_sub_rect_jpeg(vs, x, y, w, h, bg, fg, colors, palette,
                                 force_jpeg);
C
Corentin Chary 已提交
1570 1571
    } else {
        ret = send_sub_rect_nojpeg(vs, x, y, w, h, bg, fg, colors, palette);
1572
    }
C
Corentin Chary 已提交
1573 1574 1575 1576
#else
    ret = send_sub_rect_nojpeg(vs, x, y, w, h, bg, fg, colors, palette);
#endif

1577
    palette_destroy(palette);
1578
    return ret;
C
Corentin Chary 已提交
1579 1580
}

1581 1582
static int send_sub_rect_solid(VncState *vs, int x, int y, int w, int h)
{
1583
    vnc_framebuffer_update(vs, x, y, w, h, vs->tight.type);
1584 1585 1586 1587 1588 1589 1590 1591

    vnc_tight_start(vs);
    vnc_raw_send_framebuffer_update(vs, x, y, w, h);
    vnc_tight_stop(vs);

    return send_solid_rect(vs);
}

1592 1593
static int send_rect_simple(VncState *vs, int x, int y, int w, int h,
                            bool split)
C
Corentin Chary 已提交
1594 1595 1596 1597 1598 1599 1600
{
    int max_size, max_width;
    int max_sub_width, max_sub_height;
    int dx, dy;
    int rw, rh;
    int n = 0;

1601 1602
    max_size = tight_conf[vs->tight.compression].max_rect_size;
    max_width = tight_conf[vs->tight.compression].max_rect_width;
C
Corentin Chary 已提交
1603

1604
    if (split && (w > max_width || w * h > max_size)) {
C
Corentin Chary 已提交
1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621
        max_sub_width = (w > max_width) ? max_width : w;
        max_sub_height = max_size / max_sub_width;

        for (dy = 0; dy < h; dy += max_sub_height) {
            for (dx = 0; dx < w; dx += max_width) {
                rw = MIN(max_sub_width, w - dx);
                rh = MIN(max_sub_height, h - dy);
                n += send_sub_rect(vs, x+dx, y+dy, rw, rh);
            }
        }
    } else {
        n += send_sub_rect(vs, x, y, w, h);
    }

    return n;
}

1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634
static int find_large_solid_color_rect(VncState *vs, int x, int y,
                                       int w, int h, int max_rows)
{
    int dx, dy, dw, dh;
    int n = 0;

    /* Try to find large solid-color areas and send them separately. */

    for (dy = y; dy < y + h; dy += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) {

        /* If a rectangle becomes too large, send its upper part now. */

        if (dy - y >= max_rows) {
1635
            n += send_rect_simple(vs, x, y, w, max_rows, true);
1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673
            y += max_rows;
            h -= max_rows;
        }

        dh = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, (y + h - dy));

        for (dx = x; dx < x + w; dx += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) {
            uint32_t color_value;
            int x_best, y_best, w_best, h_best;

            dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, (x + w - dx));

            if (!check_solid_tile(vs, dx, dy, dw, dh, &color_value, false)) {
                continue ;
            }

            /* Get dimensions of solid-color area. */

            find_best_solid_area(vs, dx, dy, w - (dx - x), h - (dy - y),
                                 color_value, &w_best, &h_best);

            /* Make sure a solid rectangle is large enough
               (or the whole rectangle is of the same color). */

            if (w_best * h_best != w * h &&
                w_best * h_best < VNC_TIGHT_MIN_SOLID_SUBRECT_SIZE) {
                continue;
            }

            /* Try to extend solid rectangle to maximum size. */

            x_best = dx; y_best = dy;
            extend_solid_area(vs, x, y, w, h, color_value,
                              &x_best, &y_best, &w_best, &h_best);

            /* Send rectangles at top and left to solid-color area. */

            if (y_best != y) {
1674
                n += send_rect_simple(vs, x, y, w, y_best-y, true);
1675 1676
            }
            if (x_best != x) {
C
Corentin Chary 已提交
1677 1678
                n += tight_send_framebuffer_update(vs, x, y_best,
                                                   x_best-x, h_best);
1679 1680 1681 1682 1683 1684 1685 1686
            }

            /* Send solid-color rectangle. */
            n += send_sub_rect_solid(vs, x_best, y_best, w_best, h_best);

            /* Send remaining rectangles (at right and bottom). */

            if (x_best + w_best != x + w) {
C
Corentin Chary 已提交
1687 1688 1689 1690
                n += tight_send_framebuffer_update(vs, x_best+w_best,
                                                   y_best,
                                                   w-(x_best-x)-w_best,
                                                   h_best);
1691 1692
            }
            if (y_best + h_best != y + h) {
C
Corentin Chary 已提交
1693 1694
                n += tight_send_framebuffer_update(vs, x, y_best+h_best,
                                                   w, h-(y_best-y)-h_best);
1695 1696 1697 1698 1699 1700
            }

            /* Return after all recursive calls are done. */
            return n;
        }
    }
1701
    return n + send_rect_simple(vs, x, y, w, h, true);
1702 1703
}

C
Corentin Chary 已提交
1704 1705
static int tight_send_framebuffer_update(VncState *vs, int x, int y,
                                         int w, int h)
C
Corentin Chary 已提交
1706
{
1707 1708
    int max_rows;

C
Corentin Chary 已提交
1709 1710
    if (vs->clientds.pf.bytes_per_pixel == 4 && vs->clientds.pf.rmax == 0xFF &&
        vs->clientds.pf.bmax == 0xFF && vs->clientds.pf.gmax == 0xFF) {
1711
        vs->tight.pixel24 = true;
C
Corentin Chary 已提交
1712
    } else {
1713
        vs->tight.pixel24 = false;
C
Corentin Chary 已提交
1714 1715
    }

1716
#ifdef CONFIG_VNC_JPEG
1717
    if (vs->tight.quality != (uint8_t)-1) {
1718 1719 1720 1721 1722 1723
        double freq = vnc_update_freq(vs, x, y, w, h);

        if (freq > tight_jpeg_conf[vs->tight.quality].jpeg_freq_threshold) {
            return send_rect_simple(vs, x, y, w, h, false);
        }
    }
1724
#endif
1725 1726 1727 1728

    if (w * h < VNC_TIGHT_MIN_SPLIT_RECT_SIZE) {
        return send_rect_simple(vs, x, y, w, h, true);
    }
1729 1730 1731

    /* Calculate maximum number of rows in one non-solid rectangle. */

1732 1733
    max_rows = tight_conf[vs->tight.compression].max_rect_size;
    max_rows /= MIN(tight_conf[vs->tight.compression].max_rect_width, w);
1734 1735

    return find_large_solid_color_rect(vs, x, y, w, h, max_rows);
C
Corentin Chary 已提交
1736 1737
}

C
Corentin Chary 已提交
1738 1739 1740
int vnc_tight_send_framebuffer_update(VncState *vs, int x, int y,
                                      int w, int h)
{
1741
    vs->tight.type = VNC_ENCODING_TIGHT;
C
Corentin Chary 已提交
1742 1743 1744 1745 1746 1747
    return tight_send_framebuffer_update(vs, x, y, w, h);
}

int vnc_tight_png_send_framebuffer_update(VncState *vs, int x, int y,
                                          int w, int h)
{
1748
    vs->tight.type = VNC_ENCODING_TIGHT_PNG;
C
Corentin Chary 已提交
1749 1750 1751
    return tight_send_framebuffer_update(vs, x, y, w, h);
}

C
Corentin Chary 已提交
1752 1753 1754
void vnc_tight_clear(VncState *vs)
{
    int i;
1755 1756 1757
    for (i=0; i<ARRAY_SIZE(vs->tight.stream); i++) {
        if (vs->tight.stream[i].opaque) {
            deflateEnd(&vs->tight.stream[i]);
C
Corentin Chary 已提交
1758 1759 1760
        }
    }

1761 1762 1763
    buffer_free(&vs->tight.tight);
    buffer_free(&vs->tight.zlib);
    buffer_free(&vs->tight.gradient);
1764
#ifdef CONFIG_VNC_JPEG
1765
    buffer_free(&vs->tight.jpeg);
1766
#endif
C
Corentin Chary 已提交
1767 1768 1769
#ifdef CONFIG_VNC_PNG
    buffer_free(&vs->tight.png);
#endif
C
Corentin Chary 已提交
1770
}