1245f7b51SCorentin Chary /* 2245f7b51SCorentin Chary * QEMU VNC display driver: tight encoding 3245f7b51SCorentin Chary * 4245f7b51SCorentin Chary * From libvncserver/libvncserver/tight.c 5245f7b51SCorentin Chary * Copyright (C) 2000, 2001 Const Kaplinsky. All Rights Reserved. 6245f7b51SCorentin Chary * Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved. 7245f7b51SCorentin Chary * 8245f7b51SCorentin Chary * Copyright (C) 2010 Corentin Chary <corentin.chary@gmail.com> 9245f7b51SCorentin Chary * 10245f7b51SCorentin Chary * Permission is hereby granted, free of charge, to any person obtaining a copy 11245f7b51SCorentin Chary * of this software and associated documentation files (the "Software"), to deal 12245f7b51SCorentin Chary * in the Software without restriction, including without limitation the rights 13245f7b51SCorentin Chary * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14245f7b51SCorentin Chary * copies of the Software, and to permit persons to whom the Software is 15245f7b51SCorentin Chary * furnished to do so, subject to the following conditions: 16245f7b51SCorentin Chary * 17245f7b51SCorentin Chary * The above copyright notice and this permission notice shall be included in 18245f7b51SCorentin Chary * all copies or substantial portions of the Software. 19245f7b51SCorentin Chary * 20245f7b51SCorentin Chary * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21245f7b51SCorentin Chary * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22245f7b51SCorentin Chary * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 23245f7b51SCorentin Chary * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24245f7b51SCorentin Chary * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25245f7b51SCorentin Chary * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 26245f7b51SCorentin Chary * THE SOFTWARE. 27245f7b51SCorentin Chary */ 28245f7b51SCorentin Chary 29efe556adSCorentin Chary #include "config-host.h" 30245f7b51SCorentin Chary 31f26e428dSRoy Tam /* This needs to be before jpeglib.h line because of conflict with 32f26e428dSRoy Tam INT32 definitions between jmorecfg.h (included by jpeglib.h) and 33f26e428dSRoy Tam Win32 basetsd.h (included by windows.h). */ 34f26e428dSRoy Tam #include "qemu-common.h" 35f26e428dSRoy Tam 36efe556adSCorentin Chary #ifdef CONFIG_VNC_PNG 372fb0c09fSStefan Weil /* The following define is needed by pngconf.h. Otherwise it won't compile, 382fb0c09fSStefan Weil because setjmp.h was already included by qemu-common.h. */ 392fb0c09fSStefan Weil #define PNG_SKIP_SETJMP_CHECK 40efe556adSCorentin Chary #include <png.h> 41efe556adSCorentin Chary #endif 42245f7b51SCorentin Chary #ifdef CONFIG_VNC_JPEG 43245f7b51SCorentin Chary #include <stdio.h> 44245f7b51SCorentin Chary #include <jpeglib.h> 45245f7b51SCorentin Chary #endif 46245f7b51SCorentin Chary 47245f7b51SCorentin Chary #include "bswap.h" 48245f7b51SCorentin Chary #include "qint.h" 49245f7b51SCorentin Chary #include "vnc.h" 50245f7b51SCorentin Chary #include "vnc-enc-tight.h" 515136a052SCorentin Chary #include "vnc-palette.h" 52245f7b51SCorentin Chary 53245f7b51SCorentin Chary /* Compression level stuff. The following array contains various 54245f7b51SCorentin Chary encoder parameters for each of 10 compression levels (0..9). 55245f7b51SCorentin Chary Last three parameters correspond to JPEG quality levels (0..9). */ 56245f7b51SCorentin Chary 57245f7b51SCorentin Chary static const struct { 58245f7b51SCorentin Chary int max_rect_size, max_rect_width; 59245f7b51SCorentin Chary int mono_min_rect_size, gradient_min_rect_size; 60245f7b51SCorentin Chary int idx_zlib_level, mono_zlib_level, raw_zlib_level, gradient_zlib_level; 61245f7b51SCorentin Chary int gradient_threshold, gradient_threshold24; 62245f7b51SCorentin Chary int idx_max_colors_divisor; 63245f7b51SCorentin Chary int jpeg_quality, jpeg_threshold, jpeg_threshold24; 64245f7b51SCorentin Chary } tight_conf[] = { 65245f7b51SCorentin Chary { 512, 32, 6, 65536, 0, 0, 0, 0, 0, 0, 4, 5, 10000, 23000 }, 66245f7b51SCorentin Chary { 2048, 128, 6, 65536, 1, 1, 1, 0, 0, 0, 8, 10, 8000, 18000 }, 67245f7b51SCorentin Chary { 6144, 256, 8, 65536, 3, 3, 2, 0, 0, 0, 24, 15, 6500, 15000 }, 68245f7b51SCorentin Chary { 10240, 1024, 12, 65536, 5, 5, 3, 0, 0, 0, 32, 25, 5000, 12000 }, 69245f7b51SCorentin Chary { 16384, 2048, 12, 65536, 6, 6, 4, 0, 0, 0, 32, 37, 4000, 10000 }, 70245f7b51SCorentin Chary { 32768, 2048, 12, 4096, 7, 7, 5, 4, 150, 380, 32, 50, 3000, 8000 }, 71245f7b51SCorentin Chary { 65536, 2048, 16, 4096, 7, 7, 6, 4, 170, 420, 48, 60, 2000, 5000 }, 72245f7b51SCorentin Chary { 65536, 2048, 16, 4096, 8, 8, 7, 5, 180, 450, 64, 70, 1000, 2500 }, 73245f7b51SCorentin Chary { 65536, 2048, 32, 8192, 9, 9, 8, 6, 190, 475, 64, 75, 500, 1200 }, 74245f7b51SCorentin Chary { 65536, 2048, 32, 8192, 9, 9, 9, 6, 200, 500, 96, 80, 200, 500 } 75245f7b51SCorentin Chary }; 76245f7b51SCorentin Chary 77efe556adSCorentin Chary 78efe556adSCorentin Chary static int tight_send_framebuffer_update(VncState *vs, int x, int y, 79efe556adSCorentin Chary int w, int h); 80efe556adSCorentin Chary 81ce702e93SCorentin Chary #ifdef CONFIG_VNC_JPEG 82ce702e93SCorentin Chary static const struct { 83ce702e93SCorentin Chary double jpeg_freq_min; /* Don't send JPEG if the freq is bellow */ 84ce702e93SCorentin Chary double jpeg_freq_threshold; /* Always send JPEG if the freq is above */ 85ce702e93SCorentin Chary int jpeg_idx; /* Allow indexed JPEG */ 86ce702e93SCorentin Chary int jpeg_full; /* Allow full color JPEG */ 87ce702e93SCorentin Chary } tight_jpeg_conf[] = { 888cb4a6b7SCorentin Chary { 0, 8, 1, 1 }, 898cb4a6b7SCorentin Chary { 0, 8, 1, 1 }, 908cb4a6b7SCorentin Chary { 0, 8, 1, 1 }, 918cb4a6b7SCorentin Chary { 0, 8, 1, 1 }, 928cb4a6b7SCorentin Chary { 0, 10, 1, 1 }, 938cb4a6b7SCorentin Chary { 0.1, 10, 1, 1 }, 948cb4a6b7SCorentin Chary { 0.2, 10, 1, 1 }, 958cb4a6b7SCorentin Chary { 0.3, 12, 0, 0 }, 968cb4a6b7SCorentin Chary { 0.4, 14, 0, 0 }, 978cb4a6b7SCorentin Chary { 0.5, 16, 0, 0 }, 98ce702e93SCorentin Chary }; 99ce702e93SCorentin Chary #endif 100ce702e93SCorentin Chary 101efe556adSCorentin Chary #ifdef CONFIG_VNC_PNG 1023941bf6fSCorentin Chary static const struct { 1033941bf6fSCorentin Chary int png_zlib_level, png_filters; 1043941bf6fSCorentin Chary } tight_png_conf[] = { 1053941bf6fSCorentin Chary { 0, PNG_NO_FILTERS }, 1063941bf6fSCorentin Chary { 1, PNG_NO_FILTERS }, 1073941bf6fSCorentin Chary { 2, PNG_NO_FILTERS }, 1083941bf6fSCorentin Chary { 3, PNG_NO_FILTERS }, 1093941bf6fSCorentin Chary { 4, PNG_NO_FILTERS }, 1103941bf6fSCorentin Chary { 5, PNG_ALL_FILTERS }, 1113941bf6fSCorentin Chary { 6, PNG_ALL_FILTERS }, 1123941bf6fSCorentin Chary { 7, PNG_ALL_FILTERS }, 1133941bf6fSCorentin Chary { 8, PNG_ALL_FILTERS }, 1143941bf6fSCorentin Chary { 9, PNG_ALL_FILTERS }, 1153941bf6fSCorentin Chary }; 1163941bf6fSCorentin Chary 117efe556adSCorentin Chary static int send_png_rect(VncState *vs, int x, int y, int w, int h, 1185136a052SCorentin Chary VncPalette *palette); 119efe556adSCorentin Chary 120efe556adSCorentin Chary static bool tight_can_send_png_rect(VncState *vs, int w, int h) 121efe556adSCorentin Chary { 122d1af0e05SCorentin Chary if (vs->tight.type != VNC_ENCODING_TIGHT_PNG) { 123efe556adSCorentin Chary return false; 124efe556adSCorentin Chary } 125efe556adSCorentin Chary 126efe556adSCorentin Chary if (ds_get_bytes_per_pixel(vs->ds) == 1 || 127*9f64916dSGerd Hoffmann vs->client_pf.bytes_per_pixel == 1) { 128efe556adSCorentin Chary return false; 129efe556adSCorentin Chary } 130efe556adSCorentin Chary 131efe556adSCorentin Chary return true; 132efe556adSCorentin Chary } 133efe556adSCorentin Chary #endif 134efe556adSCorentin Chary 135245f7b51SCorentin Chary /* 136245f7b51SCorentin Chary * Code to guess if given rectangle is suitable for smooth image 137245f7b51SCorentin Chary * compression (by applying "gradient" filter or JPEG coder). 138245f7b51SCorentin Chary */ 139245f7b51SCorentin Chary 140249cdb42SBlue Swirl static unsigned int 141245f7b51SCorentin Chary tight_detect_smooth_image24(VncState *vs, int w, int h) 142245f7b51SCorentin Chary { 143245f7b51SCorentin Chary int off; 144245f7b51SCorentin Chary int x, y, d, dx; 145249cdb42SBlue Swirl unsigned int c; 146249cdb42SBlue Swirl unsigned int stats[256]; 147245f7b51SCorentin Chary int pixels = 0; 148245f7b51SCorentin Chary int pix, left[3]; 149249cdb42SBlue Swirl unsigned int errors; 150d1af0e05SCorentin Chary unsigned char *buf = vs->tight.tight.buffer; 151245f7b51SCorentin Chary 152245f7b51SCorentin Chary /* 153245f7b51SCorentin Chary * If client is big-endian, color samples begin from the second 154245f7b51SCorentin Chary * byte (offset 1) of a 32-bit pixel value. 155245f7b51SCorentin Chary */ 156*9f64916dSGerd Hoffmann off = vs->client_be; 157245f7b51SCorentin Chary 158245f7b51SCorentin Chary memset(stats, 0, sizeof (stats)); 159245f7b51SCorentin Chary 160245f7b51SCorentin Chary for (y = 0, x = 0; y < h && x < w;) { 161245f7b51SCorentin Chary for (d = 0; d < h - y && d < w - x - VNC_TIGHT_DETECT_SUBROW_WIDTH; 162245f7b51SCorentin Chary d++) { 163245f7b51SCorentin Chary for (c = 0; c < 3; c++) { 164245f7b51SCorentin Chary left[c] = buf[((y+d)*w+x+d)*4+off+c] & 0xFF; 165245f7b51SCorentin Chary } 166245f7b51SCorentin Chary for (dx = 1; dx <= VNC_TIGHT_DETECT_SUBROW_WIDTH; dx++) { 167245f7b51SCorentin Chary for (c = 0; c < 3; c++) { 168245f7b51SCorentin Chary pix = buf[((y+d)*w+x+d+dx)*4+off+c] & 0xFF; 169245f7b51SCorentin Chary stats[abs(pix - left[c])]++; 170245f7b51SCorentin Chary left[c] = pix; 171245f7b51SCorentin Chary } 172245f7b51SCorentin Chary pixels++; 173245f7b51SCorentin Chary } 174245f7b51SCorentin Chary } 175245f7b51SCorentin Chary if (w > h) { 176245f7b51SCorentin Chary x += h; 177245f7b51SCorentin Chary y = 0; 178245f7b51SCorentin Chary } else { 179245f7b51SCorentin Chary x = 0; 180245f7b51SCorentin Chary y += w; 181245f7b51SCorentin Chary } 182245f7b51SCorentin Chary } 183245f7b51SCorentin Chary 184245f7b51SCorentin Chary /* 95% smooth or more ... */ 185245f7b51SCorentin Chary if (stats[0] * 33 / pixels >= 95) { 186245f7b51SCorentin Chary return 0; 187245f7b51SCorentin Chary } 188245f7b51SCorentin Chary 189245f7b51SCorentin Chary errors = 0; 190245f7b51SCorentin Chary for (c = 1; c < 8; c++) { 191245f7b51SCorentin Chary errors += stats[c] * (c * c); 192245f7b51SCorentin Chary if (stats[c] == 0 || stats[c] > stats[c-1] * 2) { 193245f7b51SCorentin Chary return 0; 194245f7b51SCorentin Chary } 195245f7b51SCorentin Chary } 196245f7b51SCorentin Chary for (; c < 256; c++) { 197245f7b51SCorentin Chary errors += stats[c] * (c * c); 198245f7b51SCorentin Chary } 199245f7b51SCorentin Chary errors /= (pixels * 3 - stats[0]); 200245f7b51SCorentin Chary 201245f7b51SCorentin Chary return errors; 202245f7b51SCorentin Chary } 203245f7b51SCorentin Chary 204245f7b51SCorentin Chary #define DEFINE_DETECT_FUNCTION(bpp) \ 205245f7b51SCorentin Chary \ 206249cdb42SBlue Swirl static unsigned int \ 207245f7b51SCorentin Chary tight_detect_smooth_image##bpp(VncState *vs, int w, int h) { \ 208245f7b51SCorentin Chary bool endian; \ 209245f7b51SCorentin Chary uint##bpp##_t pix; \ 210245f7b51SCorentin Chary int max[3], shift[3]; \ 211245f7b51SCorentin Chary int x, y, d, dx; \ 212249cdb42SBlue Swirl unsigned int c; \ 213249cdb42SBlue Swirl unsigned int stats[256]; \ 214245f7b51SCorentin Chary int pixels = 0; \ 215245f7b51SCorentin Chary int sample, sum, left[3]; \ 216249cdb42SBlue Swirl unsigned int errors; \ 217d1af0e05SCorentin Chary unsigned char *buf = vs->tight.tight.buffer; \ 218245f7b51SCorentin Chary \ 219*9f64916dSGerd Hoffmann endian = 0; /* FIXME: ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) != \ 220*9f64916dSGerd Hoffmann (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG)); */ \ 221245f7b51SCorentin Chary \ 222245f7b51SCorentin Chary \ 223*9f64916dSGerd Hoffmann max[0] = vs->client_pf.rmax; \ 224*9f64916dSGerd Hoffmann max[1] = vs->client_pf.gmax; \ 225*9f64916dSGerd Hoffmann max[2] = vs->client_pf.bmax; \ 226*9f64916dSGerd Hoffmann shift[0] = vs->client_pf.rshift; \ 227*9f64916dSGerd Hoffmann shift[1] = vs->client_pf.gshift; \ 228*9f64916dSGerd Hoffmann shift[2] = vs->client_pf.bshift; \ 229245f7b51SCorentin Chary \ 230245f7b51SCorentin Chary memset(stats, 0, sizeof(stats)); \ 231245f7b51SCorentin Chary \ 232245f7b51SCorentin Chary y = 0, x = 0; \ 233245f7b51SCorentin Chary while (y < h && x < w) { \ 234245f7b51SCorentin Chary for (d = 0; d < h - y && \ 235245f7b51SCorentin Chary d < w - x - VNC_TIGHT_DETECT_SUBROW_WIDTH; d++) { \ 236245f7b51SCorentin Chary pix = ((uint##bpp##_t *)buf)[(y+d)*w+x+d]; \ 237245f7b51SCorentin Chary if (endian) { \ 238ba5e7f82SIzumi Tsutsui pix = bswap##bpp(pix); \ 239245f7b51SCorentin Chary } \ 240245f7b51SCorentin Chary for (c = 0; c < 3; c++) { \ 241245f7b51SCorentin Chary left[c] = (int)(pix >> shift[c] & max[c]); \ 242245f7b51SCorentin Chary } \ 243245f7b51SCorentin Chary for (dx = 1; dx <= VNC_TIGHT_DETECT_SUBROW_WIDTH; \ 244245f7b51SCorentin Chary dx++) { \ 245245f7b51SCorentin Chary pix = ((uint##bpp##_t *)buf)[(y+d)*w+x+d+dx]; \ 246245f7b51SCorentin Chary if (endian) { \ 247ba5e7f82SIzumi Tsutsui pix = bswap##bpp(pix); \ 248245f7b51SCorentin Chary } \ 249245f7b51SCorentin Chary sum = 0; \ 250245f7b51SCorentin Chary for (c = 0; c < 3; c++) { \ 251245f7b51SCorentin Chary sample = (int)(pix >> shift[c] & max[c]); \ 252245f7b51SCorentin Chary sum += abs(sample - left[c]); \ 253245f7b51SCorentin Chary left[c] = sample; \ 254245f7b51SCorentin Chary } \ 255245f7b51SCorentin Chary if (sum > 255) { \ 256245f7b51SCorentin Chary sum = 255; \ 257245f7b51SCorentin Chary } \ 258245f7b51SCorentin Chary stats[sum]++; \ 259245f7b51SCorentin Chary pixels++; \ 260245f7b51SCorentin Chary } \ 261245f7b51SCorentin Chary } \ 262245f7b51SCorentin Chary if (w > h) { \ 263245f7b51SCorentin Chary x += h; \ 264245f7b51SCorentin Chary y = 0; \ 265245f7b51SCorentin Chary } else { \ 266245f7b51SCorentin Chary x = 0; \ 267245f7b51SCorentin Chary y += w; \ 268245f7b51SCorentin Chary } \ 269245f7b51SCorentin Chary } \ 270245f7b51SCorentin Chary \ 271245f7b51SCorentin Chary if ((stats[0] + stats[1]) * 100 / pixels >= 90) { \ 272245f7b51SCorentin Chary return 0; \ 273245f7b51SCorentin Chary } \ 274245f7b51SCorentin Chary \ 275245f7b51SCorentin Chary errors = 0; \ 276245f7b51SCorentin Chary for (c = 1; c < 8; c++) { \ 277245f7b51SCorentin Chary errors += stats[c] * (c * c); \ 278245f7b51SCorentin Chary if (stats[c] == 0 || stats[c] > stats[c-1] * 2) { \ 279245f7b51SCorentin Chary return 0; \ 280245f7b51SCorentin Chary } \ 281245f7b51SCorentin Chary } \ 282245f7b51SCorentin Chary for (; c < 256; c++) { \ 283245f7b51SCorentin Chary errors += stats[c] * (c * c); \ 284245f7b51SCorentin Chary } \ 285245f7b51SCorentin Chary errors /= (pixels - stats[0]); \ 286245f7b51SCorentin Chary \ 287245f7b51SCorentin Chary return errors; \ 288245f7b51SCorentin Chary } 289245f7b51SCorentin Chary 290245f7b51SCorentin Chary DEFINE_DETECT_FUNCTION(16) 291245f7b51SCorentin Chary DEFINE_DETECT_FUNCTION(32) 292245f7b51SCorentin Chary 293245f7b51SCorentin Chary static int 294245f7b51SCorentin Chary tight_detect_smooth_image(VncState *vs, int w, int h) 295245f7b51SCorentin Chary { 296249cdb42SBlue Swirl unsigned int errors; 297d1af0e05SCorentin Chary int compression = vs->tight.compression; 298d1af0e05SCorentin Chary int quality = vs->tight.quality; 299245f7b51SCorentin Chary 300245f7b51SCorentin Chary if (!vs->vd->lossy) { 301245f7b51SCorentin Chary return 0; 302245f7b51SCorentin Chary } 303245f7b51SCorentin Chary 304245f7b51SCorentin Chary if (ds_get_bytes_per_pixel(vs->ds) == 1 || 305*9f64916dSGerd Hoffmann vs->client_pf.bytes_per_pixel == 1 || 306245f7b51SCorentin Chary w < VNC_TIGHT_DETECT_MIN_WIDTH || h < VNC_TIGHT_DETECT_MIN_HEIGHT) { 307245f7b51SCorentin Chary return 0; 308245f7b51SCorentin Chary } 309245f7b51SCorentin Chary 3107bccf573SBlue Swirl if (vs->tight.quality != (uint8_t)-1) { 311245f7b51SCorentin Chary if (w * h < VNC_TIGHT_JPEG_MIN_RECT_SIZE) { 312245f7b51SCorentin Chary return 0; 313245f7b51SCorentin Chary } 314245f7b51SCorentin Chary } else { 315245f7b51SCorentin Chary if (w * h < tight_conf[compression].gradient_min_rect_size) { 316245f7b51SCorentin Chary return 0; 317245f7b51SCorentin Chary } 318245f7b51SCorentin Chary } 319245f7b51SCorentin Chary 320*9f64916dSGerd Hoffmann if (vs->client_pf.bytes_per_pixel == 4) { 321d1af0e05SCorentin Chary if (vs->tight.pixel24) { 322245f7b51SCorentin Chary errors = tight_detect_smooth_image24(vs, w, h); 3237bccf573SBlue Swirl if (vs->tight.quality != (uint8_t)-1) { 324245f7b51SCorentin Chary return (errors < tight_conf[quality].jpeg_threshold24); 325245f7b51SCorentin Chary } 326245f7b51SCorentin Chary return (errors < tight_conf[compression].gradient_threshold24); 327245f7b51SCorentin Chary } else { 328245f7b51SCorentin Chary errors = tight_detect_smooth_image32(vs, w, h); 329245f7b51SCorentin Chary } 330245f7b51SCorentin Chary } else { 331245f7b51SCorentin Chary errors = tight_detect_smooth_image16(vs, w, h); 332245f7b51SCorentin Chary } 333245f7b51SCorentin Chary if (quality != -1) { 334245f7b51SCorentin Chary return (errors < tight_conf[quality].jpeg_threshold); 335245f7b51SCorentin Chary } 336245f7b51SCorentin Chary return (errors < tight_conf[compression].gradient_threshold); 337245f7b51SCorentin Chary } 338245f7b51SCorentin Chary 339245f7b51SCorentin Chary /* 340245f7b51SCorentin Chary * Code to determine how many different colors used in rectangle. 341245f7b51SCorentin Chary */ 342245f7b51SCorentin Chary #define DEFINE_FILL_PALETTE_FUNCTION(bpp) \ 343245f7b51SCorentin Chary \ 344245f7b51SCorentin Chary static int \ 345245f7b51SCorentin Chary tight_fill_palette##bpp(VncState *vs, int x, int y, \ 346245f7b51SCorentin Chary int max, size_t count, \ 347245f7b51SCorentin Chary uint32_t *bg, uint32_t *fg, \ 3485136a052SCorentin Chary VncPalette **palette) { \ 349245f7b51SCorentin Chary uint##bpp##_t *data; \ 350245f7b51SCorentin Chary uint##bpp##_t c0, c1, ci; \ 351245f7b51SCorentin Chary int i, n0, n1; \ 352245f7b51SCorentin Chary \ 353d1af0e05SCorentin Chary data = (uint##bpp##_t *)vs->tight.tight.buffer; \ 354245f7b51SCorentin Chary \ 355245f7b51SCorentin Chary c0 = data[0]; \ 356245f7b51SCorentin Chary i = 1; \ 357245f7b51SCorentin Chary while (i < count && data[i] == c0) \ 358245f7b51SCorentin Chary i++; \ 359245f7b51SCorentin Chary if (i >= count) { \ 360245f7b51SCorentin Chary *bg = *fg = c0; \ 361245f7b51SCorentin Chary return 1; \ 362245f7b51SCorentin Chary } \ 363245f7b51SCorentin Chary \ 364245f7b51SCorentin Chary if (max < 2) { \ 365245f7b51SCorentin Chary return 0; \ 366245f7b51SCorentin Chary } \ 367245f7b51SCorentin Chary \ 368245f7b51SCorentin Chary n0 = i; \ 369245f7b51SCorentin Chary c1 = data[i]; \ 370245f7b51SCorentin Chary n1 = 0; \ 371245f7b51SCorentin Chary for (i++; i < count; i++) { \ 372245f7b51SCorentin Chary ci = data[i]; \ 373245f7b51SCorentin Chary if (ci == c0) { \ 374245f7b51SCorentin Chary n0++; \ 375245f7b51SCorentin Chary } else if (ci == c1) { \ 376245f7b51SCorentin Chary n1++; \ 377245f7b51SCorentin Chary } else \ 378245f7b51SCorentin Chary break; \ 379245f7b51SCorentin Chary } \ 380245f7b51SCorentin Chary if (i >= count) { \ 381245f7b51SCorentin Chary if (n0 > n1) { \ 382245f7b51SCorentin Chary *bg = (uint32_t)c0; \ 383245f7b51SCorentin Chary *fg = (uint32_t)c1; \ 384245f7b51SCorentin Chary } else { \ 385245f7b51SCorentin Chary *bg = (uint32_t)c1; \ 386245f7b51SCorentin Chary *fg = (uint32_t)c0; \ 387245f7b51SCorentin Chary } \ 388245f7b51SCorentin Chary return 2; \ 389245f7b51SCorentin Chary } \ 390245f7b51SCorentin Chary \ 391245f7b51SCorentin Chary if (max == 2) { \ 392245f7b51SCorentin Chary return 0; \ 393245f7b51SCorentin Chary } \ 394245f7b51SCorentin Chary \ 3955136a052SCorentin Chary *palette = palette_new(max, bpp); \ 3965136a052SCorentin Chary palette_put(*palette, c0); \ 3975136a052SCorentin Chary palette_put(*palette, c1); \ 3985136a052SCorentin Chary palette_put(*palette, ci); \ 399245f7b51SCorentin Chary \ 400245f7b51SCorentin Chary for (i++; i < count; i++) { \ 401245f7b51SCorentin Chary if (data[i] == ci) { \ 402245f7b51SCorentin Chary continue; \ 403245f7b51SCorentin Chary } else { \ 4045d8efe39SCorentin Chary ci = data[i]; \ 4055136a052SCorentin Chary if (!palette_put(*palette, (uint32_t)ci)) { \ 406245f7b51SCorentin Chary return 0; \ 407245f7b51SCorentin Chary } \ 408245f7b51SCorentin Chary } \ 409245f7b51SCorentin Chary } \ 410245f7b51SCorentin Chary \ 4115136a052SCorentin Chary return palette_size(*palette); \ 412245f7b51SCorentin Chary } 413245f7b51SCorentin Chary 414245f7b51SCorentin Chary DEFINE_FILL_PALETTE_FUNCTION(8) 415245f7b51SCorentin Chary DEFINE_FILL_PALETTE_FUNCTION(16) 416245f7b51SCorentin Chary DEFINE_FILL_PALETTE_FUNCTION(32) 417245f7b51SCorentin Chary 418245f7b51SCorentin Chary static int tight_fill_palette(VncState *vs, int x, int y, 419245f7b51SCorentin Chary size_t count, uint32_t *bg, uint32_t *fg, 4205136a052SCorentin Chary VncPalette **palette) 421245f7b51SCorentin Chary { 422245f7b51SCorentin Chary int max; 423245f7b51SCorentin Chary 424d1af0e05SCorentin Chary max = count / tight_conf[vs->tight.compression].idx_max_colors_divisor; 425245f7b51SCorentin Chary if (max < 2 && 426d1af0e05SCorentin Chary count >= tight_conf[vs->tight.compression].mono_min_rect_size) { 427245f7b51SCorentin Chary max = 2; 428245f7b51SCorentin Chary } 429245f7b51SCorentin Chary if (max >= 256) { 430245f7b51SCorentin Chary max = 256; 431245f7b51SCorentin Chary } 432245f7b51SCorentin Chary 433*9f64916dSGerd Hoffmann switch (vs->client_pf.bytes_per_pixel) { 434245f7b51SCorentin Chary case 4: 435245f7b51SCorentin Chary return tight_fill_palette32(vs, x, y, max, count, bg, fg, palette); 436245f7b51SCorentin Chary case 2: 437245f7b51SCorentin Chary return tight_fill_palette16(vs, x, y, max, count, bg, fg, palette); 438245f7b51SCorentin Chary default: 439245f7b51SCorentin Chary max = 2; 440245f7b51SCorentin Chary return tight_fill_palette8(vs, x, y, max, count, bg, fg, palette); 441245f7b51SCorentin Chary } 442245f7b51SCorentin Chary return 0; 443245f7b51SCorentin Chary } 444245f7b51SCorentin Chary 445245f7b51SCorentin Chary /* 446245f7b51SCorentin Chary * Converting truecolor samples into palette indices. 447245f7b51SCorentin Chary */ 448245f7b51SCorentin Chary #define DEFINE_IDX_ENCODE_FUNCTION(bpp) \ 449245f7b51SCorentin Chary \ 450245f7b51SCorentin Chary static void \ 451245f7b51SCorentin Chary tight_encode_indexed_rect##bpp(uint8_t *buf, int count, \ 4525136a052SCorentin Chary VncPalette *palette) { \ 453245f7b51SCorentin Chary uint##bpp##_t *src; \ 454245f7b51SCorentin Chary uint##bpp##_t rgb; \ 455245f7b51SCorentin Chary int i, rep; \ 456245f7b51SCorentin Chary uint8_t idx; \ 457245f7b51SCorentin Chary \ 458245f7b51SCorentin Chary src = (uint##bpp##_t *) buf; \ 459245f7b51SCorentin Chary \ 460245f7b51SCorentin Chary for (i = 0; i < count; i++) { \ 461efe556adSCorentin Chary \ 462245f7b51SCorentin Chary rgb = *src++; \ 463245f7b51SCorentin Chary rep = 0; \ 464245f7b51SCorentin Chary while (i < count && *src == rgb) { \ 465245f7b51SCorentin Chary rep++, src++, i++; \ 466245f7b51SCorentin Chary } \ 4675136a052SCorentin Chary idx = palette_idx(palette, rgb); \ 468245f7b51SCorentin Chary /* \ 469245f7b51SCorentin Chary * Should never happen, but don't break everything \ 470245f7b51SCorentin Chary * if it does, use the first color instead \ 471245f7b51SCorentin Chary */ \ 4727bccf573SBlue Swirl if (idx == (uint8_t)-1) { \ 473245f7b51SCorentin Chary idx = 0; \ 474245f7b51SCorentin Chary } \ 475245f7b51SCorentin Chary while (rep >= 0) { \ 476245f7b51SCorentin Chary *buf++ = idx; \ 477245f7b51SCorentin Chary rep--; \ 478245f7b51SCorentin Chary } \ 479245f7b51SCorentin Chary } \ 480245f7b51SCorentin Chary } 481245f7b51SCorentin Chary 482245f7b51SCorentin Chary DEFINE_IDX_ENCODE_FUNCTION(16) 483245f7b51SCorentin Chary DEFINE_IDX_ENCODE_FUNCTION(32) 484245f7b51SCorentin Chary 485245f7b51SCorentin Chary #define DEFINE_MONO_ENCODE_FUNCTION(bpp) \ 486245f7b51SCorentin Chary \ 487245f7b51SCorentin Chary static void \ 488245f7b51SCorentin Chary tight_encode_mono_rect##bpp(uint8_t *buf, int w, int h, \ 489245f7b51SCorentin Chary uint##bpp##_t bg, uint##bpp##_t fg) { \ 490245f7b51SCorentin Chary uint##bpp##_t *ptr; \ 491245f7b51SCorentin Chary unsigned int value, mask; \ 492245f7b51SCorentin Chary int aligned_width; \ 493245f7b51SCorentin Chary int x, y, bg_bits; \ 494245f7b51SCorentin Chary \ 495245f7b51SCorentin Chary ptr = (uint##bpp##_t *) buf; \ 496245f7b51SCorentin Chary aligned_width = w - w % 8; \ 497245f7b51SCorentin Chary \ 498245f7b51SCorentin Chary for (y = 0; y < h; y++) { \ 499245f7b51SCorentin Chary for (x = 0; x < aligned_width; x += 8) { \ 500245f7b51SCorentin Chary for (bg_bits = 0; bg_bits < 8; bg_bits++) { \ 501245f7b51SCorentin Chary if (*ptr++ != bg) { \ 502245f7b51SCorentin Chary break; \ 503245f7b51SCorentin Chary } \ 504245f7b51SCorentin Chary } \ 505245f7b51SCorentin Chary if (bg_bits == 8) { \ 506245f7b51SCorentin Chary *buf++ = 0; \ 507245f7b51SCorentin Chary continue; \ 508245f7b51SCorentin Chary } \ 509245f7b51SCorentin Chary mask = 0x80 >> bg_bits; \ 510245f7b51SCorentin Chary value = mask; \ 511245f7b51SCorentin Chary for (bg_bits++; bg_bits < 8; bg_bits++) { \ 512245f7b51SCorentin Chary mask >>= 1; \ 513245f7b51SCorentin Chary if (*ptr++ != bg) { \ 514245f7b51SCorentin Chary value |= mask; \ 515245f7b51SCorentin Chary } \ 516245f7b51SCorentin Chary } \ 517245f7b51SCorentin Chary *buf++ = (uint8_t)value; \ 518245f7b51SCorentin Chary } \ 519245f7b51SCorentin Chary \ 520245f7b51SCorentin Chary mask = 0x80; \ 521245f7b51SCorentin Chary value = 0; \ 522245f7b51SCorentin Chary if (x >= w) { \ 523245f7b51SCorentin Chary continue; \ 524245f7b51SCorentin Chary } \ 525245f7b51SCorentin Chary \ 526245f7b51SCorentin Chary for (; x < w; x++) { \ 527245f7b51SCorentin Chary if (*ptr++ != bg) { \ 528245f7b51SCorentin Chary value |= mask; \ 529245f7b51SCorentin Chary } \ 530245f7b51SCorentin Chary mask >>= 1; \ 531245f7b51SCorentin Chary } \ 532245f7b51SCorentin Chary *buf++ = (uint8_t)value; \ 533245f7b51SCorentin Chary } \ 534245f7b51SCorentin Chary } 535245f7b51SCorentin Chary 536245f7b51SCorentin Chary DEFINE_MONO_ENCODE_FUNCTION(8) 537245f7b51SCorentin Chary DEFINE_MONO_ENCODE_FUNCTION(16) 538245f7b51SCorentin Chary DEFINE_MONO_ENCODE_FUNCTION(32) 539245f7b51SCorentin Chary 540245f7b51SCorentin Chary /* 541245f7b51SCorentin Chary * ``Gradient'' filter for 24-bit color samples. 542245f7b51SCorentin Chary * Should be called only when redMax, greenMax and blueMax are 255. 543245f7b51SCorentin Chary * Color components assumed to be byte-aligned. 544245f7b51SCorentin Chary */ 545245f7b51SCorentin Chary 546245f7b51SCorentin Chary static void 547245f7b51SCorentin Chary tight_filter_gradient24(VncState *vs, uint8_t *buf, int w, int h) 548245f7b51SCorentin Chary { 549245f7b51SCorentin Chary uint32_t *buf32; 550245f7b51SCorentin Chary uint32_t pix32; 551245f7b51SCorentin Chary int shift[3]; 552245f7b51SCorentin Chary int *prev; 553245f7b51SCorentin Chary int here[3], upper[3], left[3], upperleft[3]; 554245f7b51SCorentin Chary int prediction; 555245f7b51SCorentin Chary int x, y, c; 556245f7b51SCorentin Chary 557245f7b51SCorentin Chary buf32 = (uint32_t *)buf; 558d1af0e05SCorentin Chary memset(vs->tight.gradient.buffer, 0, w * 3 * sizeof(int)); 559245f7b51SCorentin Chary 560*9f64916dSGerd Hoffmann if (1 /* FIXME: (vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) == 561*9f64916dSGerd Hoffmann (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG) */) { 562*9f64916dSGerd Hoffmann shift[0] = vs->client_pf.rshift; 563*9f64916dSGerd Hoffmann shift[1] = vs->client_pf.gshift; 564*9f64916dSGerd Hoffmann shift[2] = vs->client_pf.bshift; 565245f7b51SCorentin Chary } else { 566*9f64916dSGerd Hoffmann shift[0] = 24 - vs->client_pf.rshift; 567*9f64916dSGerd Hoffmann shift[1] = 24 - vs->client_pf.gshift; 568*9f64916dSGerd Hoffmann shift[2] = 24 - vs->client_pf.bshift; 569245f7b51SCorentin Chary } 570245f7b51SCorentin Chary 571245f7b51SCorentin Chary for (y = 0; y < h; y++) { 572245f7b51SCorentin Chary for (c = 0; c < 3; c++) { 573245f7b51SCorentin Chary upper[c] = 0; 574245f7b51SCorentin Chary here[c] = 0; 575245f7b51SCorentin Chary } 576d1af0e05SCorentin Chary prev = (int *)vs->tight.gradient.buffer; 577245f7b51SCorentin Chary for (x = 0; x < w; x++) { 578245f7b51SCorentin Chary pix32 = *buf32++; 579245f7b51SCorentin Chary for (c = 0; c < 3; c++) { 580245f7b51SCorentin Chary upperleft[c] = upper[c]; 581245f7b51SCorentin Chary left[c] = here[c]; 582245f7b51SCorentin Chary upper[c] = *prev; 583245f7b51SCorentin Chary here[c] = (int)(pix32 >> shift[c] & 0xFF); 584245f7b51SCorentin Chary *prev++ = here[c]; 585245f7b51SCorentin Chary 586245f7b51SCorentin Chary prediction = left[c] + upper[c] - upperleft[c]; 587245f7b51SCorentin Chary if (prediction < 0) { 588245f7b51SCorentin Chary prediction = 0; 589245f7b51SCorentin Chary } else if (prediction > 0xFF) { 590245f7b51SCorentin Chary prediction = 0xFF; 591245f7b51SCorentin Chary } 592245f7b51SCorentin Chary *buf++ = (char)(here[c] - prediction); 593245f7b51SCorentin Chary } 594245f7b51SCorentin Chary } 595245f7b51SCorentin Chary } 596245f7b51SCorentin Chary } 597245f7b51SCorentin Chary 598245f7b51SCorentin Chary 599245f7b51SCorentin Chary /* 600245f7b51SCorentin Chary * ``Gradient'' filter for other color depths. 601245f7b51SCorentin Chary */ 602245f7b51SCorentin Chary 603245f7b51SCorentin Chary #define DEFINE_GRADIENT_FILTER_FUNCTION(bpp) \ 604245f7b51SCorentin Chary \ 605245f7b51SCorentin Chary static void \ 606245f7b51SCorentin Chary tight_filter_gradient##bpp(VncState *vs, uint##bpp##_t *buf, \ 607245f7b51SCorentin Chary int w, int h) { \ 608245f7b51SCorentin Chary uint##bpp##_t pix, diff; \ 609245f7b51SCorentin Chary bool endian; \ 610245f7b51SCorentin Chary int *prev; \ 611245f7b51SCorentin Chary int max[3], shift[3]; \ 612245f7b51SCorentin Chary int here[3], upper[3], left[3], upperleft[3]; \ 613245f7b51SCorentin Chary int prediction; \ 614245f7b51SCorentin Chary int x, y, c; \ 615245f7b51SCorentin Chary \ 616d1af0e05SCorentin Chary memset (vs->tight.gradient.buffer, 0, w * 3 * sizeof(int)); \ 617245f7b51SCorentin Chary \ 618*9f64916dSGerd Hoffmann endian = 0; /* FIXME: ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) != \ 619*9f64916dSGerd Hoffmann (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG)); */ \ 620245f7b51SCorentin Chary \ 621*9f64916dSGerd Hoffmann max[0] = vs->client_pf.rmax; \ 622*9f64916dSGerd Hoffmann max[1] = vs->client_pf.gmax; \ 623*9f64916dSGerd Hoffmann max[2] = vs->client_pf.bmax; \ 624*9f64916dSGerd Hoffmann shift[0] = vs->client_pf.rshift; \ 625*9f64916dSGerd Hoffmann shift[1] = vs->client_pf.gshift; \ 626*9f64916dSGerd Hoffmann shift[2] = vs->client_pf.bshift; \ 627245f7b51SCorentin Chary \ 628245f7b51SCorentin Chary for (y = 0; y < h; y++) { \ 629245f7b51SCorentin Chary for (c = 0; c < 3; c++) { \ 630245f7b51SCorentin Chary upper[c] = 0; \ 631245f7b51SCorentin Chary here[c] = 0; \ 632245f7b51SCorentin Chary } \ 633d1af0e05SCorentin Chary prev = (int *)vs->tight.gradient.buffer; \ 634245f7b51SCorentin Chary for (x = 0; x < w; x++) { \ 635245f7b51SCorentin Chary pix = *buf; \ 636245f7b51SCorentin Chary if (endian) { \ 637ba5e7f82SIzumi Tsutsui pix = bswap##bpp(pix); \ 638245f7b51SCorentin Chary } \ 639245f7b51SCorentin Chary diff = 0; \ 640245f7b51SCorentin Chary for (c = 0; c < 3; c++) { \ 641245f7b51SCorentin Chary upperleft[c] = upper[c]; \ 642245f7b51SCorentin Chary left[c] = here[c]; \ 643245f7b51SCorentin Chary upper[c] = *prev; \ 644245f7b51SCorentin Chary here[c] = (int)(pix >> shift[c] & max[c]); \ 645245f7b51SCorentin Chary *prev++ = here[c]; \ 646245f7b51SCorentin Chary \ 647245f7b51SCorentin Chary prediction = left[c] + upper[c] - upperleft[c]; \ 648245f7b51SCorentin Chary if (prediction < 0) { \ 649245f7b51SCorentin Chary prediction = 0; \ 650245f7b51SCorentin Chary } else if (prediction > max[c]) { \ 651245f7b51SCorentin Chary prediction = max[c]; \ 652245f7b51SCorentin Chary } \ 653245f7b51SCorentin Chary diff |= ((here[c] - prediction) & max[c]) \ 654245f7b51SCorentin Chary << shift[c]; \ 655245f7b51SCorentin Chary } \ 656245f7b51SCorentin Chary if (endian) { \ 657ba5e7f82SIzumi Tsutsui diff = bswap##bpp(diff); \ 658245f7b51SCorentin Chary } \ 659245f7b51SCorentin Chary *buf++ = diff; \ 660245f7b51SCorentin Chary } \ 661245f7b51SCorentin Chary } \ 662245f7b51SCorentin Chary } 663245f7b51SCorentin Chary 664245f7b51SCorentin Chary DEFINE_GRADIENT_FILTER_FUNCTION(16) 665245f7b51SCorentin Chary DEFINE_GRADIENT_FILTER_FUNCTION(32) 666245f7b51SCorentin Chary 667245f7b51SCorentin Chary /* 668245f7b51SCorentin Chary * Check if a rectangle is all of the same color. If needSameColor is 669245f7b51SCorentin Chary * set to non-zero, then also check that its color equals to the 670b0cd712cSStefan Weil * *colorPtr value. The result is 1 if the test is successful, and in 671245f7b51SCorentin Chary * that case new color will be stored in *colorPtr. 672245f7b51SCorentin Chary */ 673245f7b51SCorentin Chary 674245f7b51SCorentin Chary #define DEFINE_CHECK_SOLID_FUNCTION(bpp) \ 675245f7b51SCorentin Chary \ 676245f7b51SCorentin Chary static bool \ 677245f7b51SCorentin Chary check_solid_tile##bpp(VncState *vs, int x, int y, int w, int h, \ 678245f7b51SCorentin Chary uint32_t* color, bool samecolor) \ 679245f7b51SCorentin Chary { \ 680245f7b51SCorentin Chary VncDisplay *vd = vs->vd; \ 681245f7b51SCorentin Chary uint##bpp##_t *fbptr; \ 682245f7b51SCorentin Chary uint##bpp##_t c; \ 683245f7b51SCorentin Chary int dx, dy; \ 684245f7b51SCorentin Chary \ 685*9f64916dSGerd Hoffmann fbptr = vnc_server_fb_ptr(vd, x, y); \ 686245f7b51SCorentin Chary \ 687245f7b51SCorentin Chary c = *fbptr; \ 688245f7b51SCorentin Chary if (samecolor && (uint32_t)c != *color) { \ 689245f7b51SCorentin Chary return false; \ 690245f7b51SCorentin Chary } \ 691245f7b51SCorentin Chary \ 692245f7b51SCorentin Chary for (dy = 0; dy < h; dy++) { \ 693245f7b51SCorentin Chary for (dx = 0; dx < w; dx++) { \ 694245f7b51SCorentin Chary if (c != fbptr[dx]) { \ 695245f7b51SCorentin Chary return false; \ 696245f7b51SCorentin Chary } \ 697245f7b51SCorentin Chary } \ 698245f7b51SCorentin Chary fbptr = (uint##bpp##_t *) \ 699*9f64916dSGerd Hoffmann ((uint8_t *)fbptr + vnc_server_fb_stride(vd)); \ 700245f7b51SCorentin Chary } \ 701245f7b51SCorentin Chary \ 702245f7b51SCorentin Chary *color = (uint32_t)c; \ 703245f7b51SCorentin Chary return true; \ 704245f7b51SCorentin Chary } 705245f7b51SCorentin Chary 706245f7b51SCorentin Chary DEFINE_CHECK_SOLID_FUNCTION(32) 707245f7b51SCorentin Chary DEFINE_CHECK_SOLID_FUNCTION(16) 708245f7b51SCorentin Chary DEFINE_CHECK_SOLID_FUNCTION(8) 709245f7b51SCorentin Chary 710245f7b51SCorentin Chary static bool check_solid_tile(VncState *vs, int x, int y, int w, int h, 711245f7b51SCorentin Chary uint32_t* color, bool samecolor) 712245f7b51SCorentin Chary { 713*9f64916dSGerd Hoffmann switch (VNC_SERVER_FB_BYTES) { 714245f7b51SCorentin Chary case 4: 715245f7b51SCorentin Chary return check_solid_tile32(vs, x, y, w, h, color, samecolor); 716245f7b51SCorentin Chary case 2: 717245f7b51SCorentin Chary return check_solid_tile16(vs, x, y, w, h, color, samecolor); 718245f7b51SCorentin Chary default: 719245f7b51SCorentin Chary return check_solid_tile8(vs, x, y, w, h, color, samecolor); 720245f7b51SCorentin Chary } 721245f7b51SCorentin Chary } 722245f7b51SCorentin Chary 723245f7b51SCorentin Chary static void find_best_solid_area(VncState *vs, int x, int y, int w, int h, 724245f7b51SCorentin Chary uint32_t color, int *w_ptr, int *h_ptr) 725245f7b51SCorentin Chary { 726245f7b51SCorentin Chary int dx, dy, dw, dh; 727245f7b51SCorentin Chary int w_prev; 728245f7b51SCorentin Chary int w_best = 0, h_best = 0; 729245f7b51SCorentin Chary 730245f7b51SCorentin Chary w_prev = w; 731245f7b51SCorentin Chary 732245f7b51SCorentin Chary for (dy = y; dy < y + h; dy += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) { 733245f7b51SCorentin Chary 734245f7b51SCorentin Chary dh = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, y + h - dy); 735245f7b51SCorentin Chary dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, w_prev); 736245f7b51SCorentin Chary 737245f7b51SCorentin Chary if (!check_solid_tile(vs, x, dy, dw, dh, &color, true)) { 738245f7b51SCorentin Chary break; 739245f7b51SCorentin Chary } 740245f7b51SCorentin Chary 741245f7b51SCorentin Chary for (dx = x + dw; dx < x + w_prev;) { 742245f7b51SCorentin Chary dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, x + w_prev - dx); 743245f7b51SCorentin Chary 744245f7b51SCorentin Chary if (!check_solid_tile(vs, dx, dy, dw, dh, &color, true)) { 745245f7b51SCorentin Chary break; 746245f7b51SCorentin Chary } 747245f7b51SCorentin Chary dx += dw; 748245f7b51SCorentin Chary } 749245f7b51SCorentin Chary 750245f7b51SCorentin Chary w_prev = dx - x; 751245f7b51SCorentin Chary if (w_prev * (dy + dh - y) > w_best * h_best) { 752245f7b51SCorentin Chary w_best = w_prev; 753245f7b51SCorentin Chary h_best = dy + dh - y; 754245f7b51SCorentin Chary } 755245f7b51SCorentin Chary } 756245f7b51SCorentin Chary 757245f7b51SCorentin Chary *w_ptr = w_best; 758245f7b51SCorentin Chary *h_ptr = h_best; 759245f7b51SCorentin Chary } 760245f7b51SCorentin Chary 761245f7b51SCorentin Chary static void extend_solid_area(VncState *vs, int x, int y, int w, int h, 762245f7b51SCorentin Chary uint32_t color, int *x_ptr, int *y_ptr, 763245f7b51SCorentin Chary int *w_ptr, int *h_ptr) 764245f7b51SCorentin Chary { 765245f7b51SCorentin Chary int cx, cy; 766245f7b51SCorentin Chary 767245f7b51SCorentin Chary /* Try to extend the area upwards. */ 768245f7b51SCorentin Chary for ( cy = *y_ptr - 1; 769245f7b51SCorentin Chary cy >= y && check_solid_tile(vs, *x_ptr, cy, *w_ptr, 1, &color, true); 770245f7b51SCorentin Chary cy-- ); 771245f7b51SCorentin Chary *h_ptr += *y_ptr - (cy + 1); 772245f7b51SCorentin Chary *y_ptr = cy + 1; 773245f7b51SCorentin Chary 774245f7b51SCorentin Chary /* ... downwards. */ 775245f7b51SCorentin Chary for ( cy = *y_ptr + *h_ptr; 776245f7b51SCorentin Chary cy < y + h && 777245f7b51SCorentin Chary check_solid_tile(vs, *x_ptr, cy, *w_ptr, 1, &color, true); 778245f7b51SCorentin Chary cy++ ); 779245f7b51SCorentin Chary *h_ptr += cy - (*y_ptr + *h_ptr); 780245f7b51SCorentin Chary 781245f7b51SCorentin Chary /* ... to the left. */ 782245f7b51SCorentin Chary for ( cx = *x_ptr - 1; 783245f7b51SCorentin Chary cx >= x && check_solid_tile(vs, cx, *y_ptr, 1, *h_ptr, &color, true); 784245f7b51SCorentin Chary cx-- ); 785245f7b51SCorentin Chary *w_ptr += *x_ptr - (cx + 1); 786245f7b51SCorentin Chary *x_ptr = cx + 1; 787245f7b51SCorentin Chary 788245f7b51SCorentin Chary /* ... to the right. */ 789245f7b51SCorentin Chary for ( cx = *x_ptr + *w_ptr; 790245f7b51SCorentin Chary cx < x + w && 791245f7b51SCorentin Chary check_solid_tile(vs, cx, *y_ptr, 1, *h_ptr, &color, true); 792245f7b51SCorentin Chary cx++ ); 793245f7b51SCorentin Chary *w_ptr += cx - (*x_ptr + *w_ptr); 794245f7b51SCorentin Chary } 795245f7b51SCorentin Chary 796245f7b51SCorentin Chary static int tight_init_stream(VncState *vs, int stream_id, 797245f7b51SCorentin Chary int level, int strategy) 798245f7b51SCorentin Chary { 799d1af0e05SCorentin Chary z_streamp zstream = &vs->tight.stream[stream_id]; 800245f7b51SCorentin Chary 801245f7b51SCorentin Chary if (zstream->opaque == NULL) { 802245f7b51SCorentin Chary int err; 803245f7b51SCorentin Chary 804245f7b51SCorentin Chary VNC_DEBUG("VNC: TIGHT: initializing zlib stream %d\n", stream_id); 805245f7b51SCorentin Chary VNC_DEBUG("VNC: TIGHT: opaque = %p | vs = %p\n", zstream->opaque, vs); 806245f7b51SCorentin Chary zstream->zalloc = vnc_zlib_zalloc; 807245f7b51SCorentin Chary zstream->zfree = vnc_zlib_zfree; 808245f7b51SCorentin Chary 809245f7b51SCorentin Chary err = deflateInit2(zstream, level, Z_DEFLATED, MAX_WBITS, 810245f7b51SCorentin Chary MAX_MEM_LEVEL, strategy); 811245f7b51SCorentin Chary 812245f7b51SCorentin Chary if (err != Z_OK) { 813245f7b51SCorentin Chary fprintf(stderr, "VNC: error initializing zlib\n"); 814245f7b51SCorentin Chary return -1; 815245f7b51SCorentin Chary } 816245f7b51SCorentin Chary 817d1af0e05SCorentin Chary vs->tight.levels[stream_id] = level; 818245f7b51SCorentin Chary zstream->opaque = vs; 819245f7b51SCorentin Chary } 820245f7b51SCorentin Chary 821d1af0e05SCorentin Chary if (vs->tight.levels[stream_id] != level) { 822245f7b51SCorentin Chary if (deflateParams(zstream, level, strategy) != Z_OK) { 823245f7b51SCorentin Chary return -1; 824245f7b51SCorentin Chary } 825d1af0e05SCorentin Chary vs->tight.levels[stream_id] = level; 826245f7b51SCorentin Chary } 827245f7b51SCorentin Chary return 0; 828245f7b51SCorentin Chary } 829245f7b51SCorentin Chary 830245f7b51SCorentin Chary static void tight_send_compact_size(VncState *vs, size_t len) 831245f7b51SCorentin Chary { 832245f7b51SCorentin Chary int lpc = 0; 833245f7b51SCorentin Chary int bytes = 0; 834245f7b51SCorentin Chary char buf[3] = {0, 0, 0}; 835245f7b51SCorentin Chary 836245f7b51SCorentin Chary buf[bytes++] = len & 0x7F; 837245f7b51SCorentin Chary if (len > 0x7F) { 838245f7b51SCorentin Chary buf[bytes-1] |= 0x80; 839245f7b51SCorentin Chary buf[bytes++] = (len >> 7) & 0x7F; 840245f7b51SCorentin Chary if (len > 0x3FFF) { 841245f7b51SCorentin Chary buf[bytes-1] |= 0x80; 842245f7b51SCorentin Chary buf[bytes++] = (len >> 14) & 0xFF; 843245f7b51SCorentin Chary } 844245f7b51SCorentin Chary } 845245f7b51SCorentin Chary for (lpc = 0; lpc < bytes; lpc++) { 846245f7b51SCorentin Chary vnc_write_u8(vs, buf[lpc]); 847245f7b51SCorentin Chary } 848245f7b51SCorentin Chary } 849245f7b51SCorentin Chary 850245f7b51SCorentin Chary static int tight_compress_data(VncState *vs, int stream_id, size_t bytes, 851245f7b51SCorentin Chary int level, int strategy) 852245f7b51SCorentin Chary { 853d1af0e05SCorentin Chary z_streamp zstream = &vs->tight.stream[stream_id]; 854245f7b51SCorentin Chary int previous_out; 855245f7b51SCorentin Chary 856245f7b51SCorentin Chary if (bytes < VNC_TIGHT_MIN_TO_COMPRESS) { 857d1af0e05SCorentin Chary vnc_write(vs, vs->tight.tight.buffer, vs->tight.tight.offset); 858245f7b51SCorentin Chary return bytes; 859245f7b51SCorentin Chary } 860245f7b51SCorentin Chary 861245f7b51SCorentin Chary if (tight_init_stream(vs, stream_id, level, strategy)) { 862245f7b51SCorentin Chary return -1; 863245f7b51SCorentin Chary } 864245f7b51SCorentin Chary 865245f7b51SCorentin Chary /* reserve memory in output buffer */ 866d1af0e05SCorentin Chary buffer_reserve(&vs->tight.zlib, bytes + 64); 867245f7b51SCorentin Chary 868245f7b51SCorentin Chary /* set pointers */ 869d1af0e05SCorentin Chary zstream->next_in = vs->tight.tight.buffer; 870d1af0e05SCorentin Chary zstream->avail_in = vs->tight.tight.offset; 871d1af0e05SCorentin Chary zstream->next_out = vs->tight.zlib.buffer + vs->tight.zlib.offset; 872d1af0e05SCorentin Chary zstream->avail_out = vs->tight.zlib.capacity - vs->tight.zlib.offset; 8732caa9e9dSMichael Tokarev previous_out = zstream->avail_out; 874245f7b51SCorentin Chary zstream->data_type = Z_BINARY; 875245f7b51SCorentin Chary 876245f7b51SCorentin Chary /* start encoding */ 877245f7b51SCorentin Chary if (deflate(zstream, Z_SYNC_FLUSH) != Z_OK) { 878245f7b51SCorentin Chary fprintf(stderr, "VNC: error during tight compression\n"); 879245f7b51SCorentin Chary return -1; 880245f7b51SCorentin Chary } 881245f7b51SCorentin Chary 882d1af0e05SCorentin Chary vs->tight.zlib.offset = vs->tight.zlib.capacity - zstream->avail_out; 8832caa9e9dSMichael Tokarev /* ...how much data has actually been produced by deflate() */ 8842caa9e9dSMichael Tokarev bytes = previous_out - zstream->avail_out; 885245f7b51SCorentin Chary 886245f7b51SCorentin Chary tight_send_compact_size(vs, bytes); 887d1af0e05SCorentin Chary vnc_write(vs, vs->tight.zlib.buffer, bytes); 888245f7b51SCorentin Chary 889d1af0e05SCorentin Chary buffer_reset(&vs->tight.zlib); 890245f7b51SCorentin Chary 891245f7b51SCorentin Chary return bytes; 892245f7b51SCorentin Chary } 893245f7b51SCorentin Chary 894245f7b51SCorentin Chary /* 895245f7b51SCorentin Chary * Subencoding implementations. 896245f7b51SCorentin Chary */ 897245f7b51SCorentin Chary static void tight_pack24(VncState *vs, uint8_t *buf, size_t count, size_t *ret) 898245f7b51SCorentin Chary { 899245f7b51SCorentin Chary uint32_t *buf32; 900245f7b51SCorentin Chary uint32_t pix; 901245f7b51SCorentin Chary int rshift, gshift, bshift; 902245f7b51SCorentin Chary 903245f7b51SCorentin Chary buf32 = (uint32_t *)buf; 904245f7b51SCorentin Chary 905*9f64916dSGerd Hoffmann if (1 /* FIXME: (vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) == 906*9f64916dSGerd Hoffmann (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG) */) { 907*9f64916dSGerd Hoffmann rshift = vs->client_pf.rshift; 908*9f64916dSGerd Hoffmann gshift = vs->client_pf.gshift; 909*9f64916dSGerd Hoffmann bshift = vs->client_pf.bshift; 910245f7b51SCorentin Chary } else { 911*9f64916dSGerd Hoffmann rshift = 24 - vs->client_pf.rshift; 912*9f64916dSGerd Hoffmann gshift = 24 - vs->client_pf.gshift; 913*9f64916dSGerd Hoffmann bshift = 24 - vs->client_pf.bshift; 914245f7b51SCorentin Chary } 915245f7b51SCorentin Chary 916245f7b51SCorentin Chary if (ret) { 917245f7b51SCorentin Chary *ret = count * 3; 918245f7b51SCorentin Chary } 919245f7b51SCorentin Chary 920245f7b51SCorentin Chary while (count--) { 921245f7b51SCorentin Chary pix = *buf32++; 922245f7b51SCorentin Chary *buf++ = (char)(pix >> rshift); 923245f7b51SCorentin Chary *buf++ = (char)(pix >> gshift); 924245f7b51SCorentin Chary *buf++ = (char)(pix >> bshift); 925245f7b51SCorentin Chary } 926245f7b51SCorentin Chary } 927245f7b51SCorentin Chary 928efe556adSCorentin Chary static int send_full_color_rect(VncState *vs, int x, int y, int w, int h) 929245f7b51SCorentin Chary { 930245f7b51SCorentin Chary int stream = 0; 9312116eff9SJes Sorensen ssize_t bytes; 932245f7b51SCorentin Chary 933efe556adSCorentin Chary #ifdef CONFIG_VNC_PNG 934efe556adSCorentin Chary if (tight_can_send_png_rect(vs, w, h)) { 935efe556adSCorentin Chary return send_png_rect(vs, x, y, w, h, NULL); 936efe556adSCorentin Chary } 937efe556adSCorentin Chary #endif 938efe556adSCorentin Chary 939245f7b51SCorentin Chary vnc_write_u8(vs, stream << 4); /* no flushing, no filter */ 940245f7b51SCorentin Chary 941d1af0e05SCorentin Chary if (vs->tight.pixel24) { 942d1af0e05SCorentin Chary tight_pack24(vs, vs->tight.tight.buffer, w * h, &vs->tight.tight.offset); 943245f7b51SCorentin Chary bytes = 3; 944245f7b51SCorentin Chary } else { 945*9f64916dSGerd Hoffmann bytes = vs->client_pf.bytes_per_pixel; 946245f7b51SCorentin Chary } 947245f7b51SCorentin Chary 948245f7b51SCorentin Chary bytes = tight_compress_data(vs, stream, w * h * bytes, 949d1af0e05SCorentin Chary tight_conf[vs->tight.compression].raw_zlib_level, 950245f7b51SCorentin Chary Z_DEFAULT_STRATEGY); 951245f7b51SCorentin Chary 952245f7b51SCorentin Chary return (bytes >= 0); 953245f7b51SCorentin Chary } 954245f7b51SCorentin Chary 955245f7b51SCorentin Chary static int send_solid_rect(VncState *vs) 956245f7b51SCorentin Chary { 957245f7b51SCorentin Chary size_t bytes; 958245f7b51SCorentin Chary 959245f7b51SCorentin Chary vnc_write_u8(vs, VNC_TIGHT_FILL << 4); /* no flushing, no filter */ 960245f7b51SCorentin Chary 961d1af0e05SCorentin Chary if (vs->tight.pixel24) { 962d1af0e05SCorentin Chary tight_pack24(vs, vs->tight.tight.buffer, 1, &vs->tight.tight.offset); 963245f7b51SCorentin Chary bytes = 3; 964245f7b51SCorentin Chary } else { 965*9f64916dSGerd Hoffmann bytes = vs->client_pf.bytes_per_pixel; 966245f7b51SCorentin Chary } 967245f7b51SCorentin Chary 968d1af0e05SCorentin Chary vnc_write(vs, vs->tight.tight.buffer, bytes); 969245f7b51SCorentin Chary return 1; 970245f7b51SCorentin Chary } 971245f7b51SCorentin Chary 972efe556adSCorentin Chary static int send_mono_rect(VncState *vs, int x, int y, 973efe556adSCorentin Chary int w, int h, uint32_t bg, uint32_t fg) 974245f7b51SCorentin Chary { 9752116eff9SJes Sorensen ssize_t bytes; 976245f7b51SCorentin Chary int stream = 1; 977d1af0e05SCorentin Chary int level = tight_conf[vs->tight.compression].mono_zlib_level; 978245f7b51SCorentin Chary 979efe556adSCorentin Chary #ifdef CONFIG_VNC_PNG 980efe556adSCorentin Chary if (tight_can_send_png_rect(vs, w, h)) { 981efe556adSCorentin Chary int ret; 982*9f64916dSGerd Hoffmann int bpp = vs->client_pf.bytes_per_pixel * 8; 9835136a052SCorentin Chary VncPalette *palette = palette_new(2, bpp); 984efe556adSCorentin Chary 9855136a052SCorentin Chary palette_put(palette, bg); 9865136a052SCorentin Chary palette_put(palette, fg); 987efe556adSCorentin Chary ret = send_png_rect(vs, x, y, w, h, palette); 9885136a052SCorentin Chary palette_destroy(palette); 989efe556adSCorentin Chary return ret; 990efe556adSCorentin Chary } 991efe556adSCorentin Chary #endif 992efe556adSCorentin Chary 993245f7b51SCorentin Chary bytes = ((w + 7) / 8) * h; 994245f7b51SCorentin Chary 995245f7b51SCorentin Chary vnc_write_u8(vs, (stream | VNC_TIGHT_EXPLICIT_FILTER) << 4); 996245f7b51SCorentin Chary vnc_write_u8(vs, VNC_TIGHT_FILTER_PALETTE); 997245f7b51SCorentin Chary vnc_write_u8(vs, 1); 998245f7b51SCorentin Chary 999*9f64916dSGerd Hoffmann switch (vs->client_pf.bytes_per_pixel) { 1000245f7b51SCorentin Chary case 4: 1001245f7b51SCorentin Chary { 1002245f7b51SCorentin Chary uint32_t buf[2] = {bg, fg}; 1003245f7b51SCorentin Chary size_t ret = sizeof (buf); 1004245f7b51SCorentin Chary 1005d1af0e05SCorentin Chary if (vs->tight.pixel24) { 1006245f7b51SCorentin Chary tight_pack24(vs, (unsigned char*)buf, 2, &ret); 1007245f7b51SCorentin Chary } 1008245f7b51SCorentin Chary vnc_write(vs, buf, ret); 1009245f7b51SCorentin Chary 1010d1af0e05SCorentin Chary tight_encode_mono_rect32(vs->tight.tight.buffer, w, h, bg, fg); 1011245f7b51SCorentin Chary break; 1012245f7b51SCorentin Chary } 1013245f7b51SCorentin Chary case 2: 1014245f7b51SCorentin Chary vnc_write(vs, &bg, 2); 1015245f7b51SCorentin Chary vnc_write(vs, &fg, 2); 1016d1af0e05SCorentin Chary tight_encode_mono_rect16(vs->tight.tight.buffer, w, h, bg, fg); 1017245f7b51SCorentin Chary break; 1018245f7b51SCorentin Chary default: 1019245f7b51SCorentin Chary vnc_write_u8(vs, bg); 1020245f7b51SCorentin Chary vnc_write_u8(vs, fg); 1021d1af0e05SCorentin Chary tight_encode_mono_rect8(vs->tight.tight.buffer, w, h, bg, fg); 1022245f7b51SCorentin Chary break; 1023245f7b51SCorentin Chary } 1024d1af0e05SCorentin Chary vs->tight.tight.offset = bytes; 1025245f7b51SCorentin Chary 1026245f7b51SCorentin Chary bytes = tight_compress_data(vs, stream, bytes, level, Z_DEFAULT_STRATEGY); 1027245f7b51SCorentin Chary return (bytes >= 0); 1028245f7b51SCorentin Chary } 1029245f7b51SCorentin Chary 1030245f7b51SCorentin Chary struct palette_cb_priv { 1031245f7b51SCorentin Chary VncState *vs; 1032245f7b51SCorentin Chary uint8_t *header; 1033efe556adSCorentin Chary #ifdef CONFIG_VNC_PNG 1034efe556adSCorentin Chary png_colorp png_palette; 1035efe556adSCorentin Chary #endif 1036245f7b51SCorentin Chary }; 1037245f7b51SCorentin Chary 10385136a052SCorentin Chary static void write_palette(int idx, uint32_t color, void *opaque) 1039245f7b51SCorentin Chary { 1040245f7b51SCorentin Chary struct palette_cb_priv *priv = opaque; 1041245f7b51SCorentin Chary VncState *vs = priv->vs; 1042*9f64916dSGerd Hoffmann uint32_t bytes = vs->client_pf.bytes_per_pixel; 1043245f7b51SCorentin Chary 1044245f7b51SCorentin Chary if (bytes == 4) { 1045245f7b51SCorentin Chary ((uint32_t*)priv->header)[idx] = color; 1046245f7b51SCorentin Chary } else { 1047245f7b51SCorentin Chary ((uint16_t*)priv->header)[idx] = color; 1048245f7b51SCorentin Chary } 1049245f7b51SCorentin Chary } 1050245f7b51SCorentin Chary 1051efe556adSCorentin Chary static bool send_gradient_rect(VncState *vs, int x, int y, int w, int h) 1052245f7b51SCorentin Chary { 1053245f7b51SCorentin Chary int stream = 3; 1054d1af0e05SCorentin Chary int level = tight_conf[vs->tight.compression].gradient_zlib_level; 10552116eff9SJes Sorensen ssize_t bytes; 1056245f7b51SCorentin Chary 1057*9f64916dSGerd Hoffmann if (vs->client_pf.bytes_per_pixel == 1) { 1058efe556adSCorentin Chary return send_full_color_rect(vs, x, y, w, h); 1059*9f64916dSGerd Hoffmann } 1060245f7b51SCorentin Chary 1061245f7b51SCorentin Chary vnc_write_u8(vs, (stream | VNC_TIGHT_EXPLICIT_FILTER) << 4); 1062245f7b51SCorentin Chary vnc_write_u8(vs, VNC_TIGHT_FILTER_GRADIENT); 1063245f7b51SCorentin Chary 1064d1af0e05SCorentin Chary buffer_reserve(&vs->tight.gradient, w * 3 * sizeof (int)); 1065245f7b51SCorentin Chary 1066d1af0e05SCorentin Chary if (vs->tight.pixel24) { 1067d1af0e05SCorentin Chary tight_filter_gradient24(vs, vs->tight.tight.buffer, w, h); 1068245f7b51SCorentin Chary bytes = 3; 1069*9f64916dSGerd Hoffmann } else if (vs->client_pf.bytes_per_pixel == 4) { 1070d1af0e05SCorentin Chary tight_filter_gradient32(vs, (uint32_t *)vs->tight.tight.buffer, w, h); 1071245f7b51SCorentin Chary bytes = 4; 1072245f7b51SCorentin Chary } else { 1073d1af0e05SCorentin Chary tight_filter_gradient16(vs, (uint16_t *)vs->tight.tight.buffer, w, h); 1074245f7b51SCorentin Chary bytes = 2; 1075245f7b51SCorentin Chary } 1076245f7b51SCorentin Chary 1077d1af0e05SCorentin Chary buffer_reset(&vs->tight.gradient); 1078245f7b51SCorentin Chary 1079245f7b51SCorentin Chary bytes = w * h * bytes; 1080d1af0e05SCorentin Chary vs->tight.tight.offset = bytes; 1081245f7b51SCorentin Chary 1082245f7b51SCorentin Chary bytes = tight_compress_data(vs, stream, bytes, 1083245f7b51SCorentin Chary level, Z_FILTERED); 1084245f7b51SCorentin Chary return (bytes >= 0); 1085245f7b51SCorentin Chary } 1086245f7b51SCorentin Chary 1087efe556adSCorentin Chary static int send_palette_rect(VncState *vs, int x, int y, 10885136a052SCorentin Chary int w, int h, VncPalette *palette) 1089245f7b51SCorentin Chary { 1090245f7b51SCorentin Chary int stream = 2; 1091d1af0e05SCorentin Chary int level = tight_conf[vs->tight.compression].idx_zlib_level; 1092245f7b51SCorentin Chary int colors; 10932116eff9SJes Sorensen ssize_t bytes; 1094245f7b51SCorentin Chary 1095efe556adSCorentin Chary #ifdef CONFIG_VNC_PNG 1096efe556adSCorentin Chary if (tight_can_send_png_rect(vs, w, h)) { 1097efe556adSCorentin Chary return send_png_rect(vs, x, y, w, h, palette); 1098efe556adSCorentin Chary } 1099efe556adSCorentin Chary #endif 1100efe556adSCorentin Chary 11015136a052SCorentin Chary colors = palette_size(palette); 1102245f7b51SCorentin Chary 1103245f7b51SCorentin Chary vnc_write_u8(vs, (stream | VNC_TIGHT_EXPLICIT_FILTER) << 4); 1104245f7b51SCorentin Chary vnc_write_u8(vs, VNC_TIGHT_FILTER_PALETTE); 1105245f7b51SCorentin Chary vnc_write_u8(vs, colors - 1); 1106245f7b51SCorentin Chary 1107*9f64916dSGerd Hoffmann switch (vs->client_pf.bytes_per_pixel) { 1108245f7b51SCorentin Chary case 4: 1109245f7b51SCorentin Chary { 1110245f7b51SCorentin Chary size_t old_offset, offset; 11115136a052SCorentin Chary uint32_t header[palette_size(palette)]; 1112245f7b51SCorentin Chary struct palette_cb_priv priv = { vs, (uint8_t *)header }; 1113245f7b51SCorentin Chary 1114245f7b51SCorentin Chary old_offset = vs->output.offset; 11155136a052SCorentin Chary palette_iter(palette, write_palette, &priv); 1116245f7b51SCorentin Chary vnc_write(vs, header, sizeof(header)); 1117245f7b51SCorentin Chary 1118d1af0e05SCorentin Chary if (vs->tight.pixel24) { 1119245f7b51SCorentin Chary tight_pack24(vs, vs->output.buffer + old_offset, colors, &offset); 1120245f7b51SCorentin Chary vs->output.offset = old_offset + offset; 1121245f7b51SCorentin Chary } 1122245f7b51SCorentin Chary 1123d1af0e05SCorentin Chary tight_encode_indexed_rect32(vs->tight.tight.buffer, w * h, palette); 1124245f7b51SCorentin Chary break; 1125245f7b51SCorentin Chary } 1126245f7b51SCorentin Chary case 2: 1127245f7b51SCorentin Chary { 11285136a052SCorentin Chary uint16_t header[palette_size(palette)]; 1129245f7b51SCorentin Chary struct palette_cb_priv priv = { vs, (uint8_t *)header }; 1130245f7b51SCorentin Chary 11315136a052SCorentin Chary palette_iter(palette, write_palette, &priv); 1132245f7b51SCorentin Chary vnc_write(vs, header, sizeof(header)); 1133d1af0e05SCorentin Chary tight_encode_indexed_rect16(vs->tight.tight.buffer, w * h, palette); 1134245f7b51SCorentin Chary break; 1135245f7b51SCorentin Chary } 1136245f7b51SCorentin Chary default: 1137245f7b51SCorentin Chary return -1; /* No palette for 8bits colors */ 1138245f7b51SCorentin Chary break; 1139245f7b51SCorentin Chary } 1140245f7b51SCorentin Chary bytes = w * h; 1141d1af0e05SCorentin Chary vs->tight.tight.offset = bytes; 1142245f7b51SCorentin Chary 1143245f7b51SCorentin Chary bytes = tight_compress_data(vs, stream, bytes, 1144245f7b51SCorentin Chary level, Z_DEFAULT_STRATEGY); 1145245f7b51SCorentin Chary return (bytes >= 0); 1146245f7b51SCorentin Chary } 1147245f7b51SCorentin Chary 1148efe556adSCorentin Chary #if defined(CONFIG_VNC_JPEG) || defined(CONFIG_VNC_PNG) 1149efe556adSCorentin Chary static void rgb_prepare_row24(VncState *vs, uint8_t *dst, int x, int y, 1150245f7b51SCorentin Chary int count) 1151245f7b51SCorentin Chary { 1152245f7b51SCorentin Chary VncDisplay *vd = vs->vd; 1153245f7b51SCorentin Chary uint32_t *fbptr; 1154245f7b51SCorentin Chary uint32_t pix; 1155245f7b51SCorentin Chary 1156*9f64916dSGerd Hoffmann fbptr = vnc_server_fb_ptr(vd, x, y); 1157245f7b51SCorentin Chary 1158245f7b51SCorentin Chary while (count--) { 1159245f7b51SCorentin Chary pix = *fbptr++; 1160245f7b51SCorentin Chary *dst++ = (uint8_t)(pix >> vs->ds->surface->pf.rshift); 1161245f7b51SCorentin Chary *dst++ = (uint8_t)(pix >> vs->ds->surface->pf.gshift); 1162245f7b51SCorentin Chary *dst++ = (uint8_t)(pix >> vs->ds->surface->pf.bshift); 1163245f7b51SCorentin Chary } 1164245f7b51SCorentin Chary } 1165245f7b51SCorentin Chary 1166efe556adSCorentin Chary #define DEFINE_RGB_GET_ROW_FUNCTION(bpp) \ 1167245f7b51SCorentin Chary \ 1168245f7b51SCorentin Chary static void \ 1169efe556adSCorentin Chary rgb_prepare_row##bpp(VncState *vs, uint8_t *dst, \ 1170245f7b51SCorentin Chary int x, int y, int count) \ 1171245f7b51SCorentin Chary { \ 1172245f7b51SCorentin Chary VncDisplay *vd = vs->vd; \ 1173245f7b51SCorentin Chary uint##bpp##_t *fbptr; \ 1174245f7b51SCorentin Chary uint##bpp##_t pix; \ 1175245f7b51SCorentin Chary int r, g, b; \ 1176245f7b51SCorentin Chary \ 1177*9f64916dSGerd Hoffmann fbptr = vnc_server_fb_ptr(vd, x, y); \ 1178245f7b51SCorentin Chary \ 1179245f7b51SCorentin Chary while (count--) { \ 1180245f7b51SCorentin Chary pix = *fbptr++; \ 1181245f7b51SCorentin Chary \ 1182245f7b51SCorentin Chary r = (int)((pix >> vs->ds->surface->pf.rshift) \ 1183245f7b51SCorentin Chary & vs->ds->surface->pf.rmax); \ 1184245f7b51SCorentin Chary g = (int)((pix >> vs->ds->surface->pf.gshift) \ 1185245f7b51SCorentin Chary & vs->ds->surface->pf.gmax); \ 1186245f7b51SCorentin Chary b = (int)((pix >> vs->ds->surface->pf.bshift) \ 1187245f7b51SCorentin Chary & vs->ds->surface->pf.bmax); \ 1188245f7b51SCorentin Chary \ 1189245f7b51SCorentin Chary *dst++ = (uint8_t)((r * 255 + vs->ds->surface->pf.rmax / 2) \ 1190245f7b51SCorentin Chary / vs->ds->surface->pf.rmax); \ 1191245f7b51SCorentin Chary *dst++ = (uint8_t)((g * 255 + vs->ds->surface->pf.gmax / 2) \ 1192245f7b51SCorentin Chary / vs->ds->surface->pf.gmax); \ 1193245f7b51SCorentin Chary *dst++ = (uint8_t)((b * 255 + vs->ds->surface->pf.bmax / 2) \ 1194245f7b51SCorentin Chary / vs->ds->surface->pf.bmax); \ 1195245f7b51SCorentin Chary } \ 1196245f7b51SCorentin Chary } 1197245f7b51SCorentin Chary 1198efe556adSCorentin Chary DEFINE_RGB_GET_ROW_FUNCTION(16) 1199efe556adSCorentin Chary DEFINE_RGB_GET_ROW_FUNCTION(32) 1200245f7b51SCorentin Chary 1201efe556adSCorentin Chary static void rgb_prepare_row(VncState *vs, uint8_t *dst, int x, int y, 1202245f7b51SCorentin Chary int count) 1203245f7b51SCorentin Chary { 1204*9f64916dSGerd Hoffmann if (VNC_SERVER_FB_BYTES == 4) { 1205*9f64916dSGerd Hoffmann if (1) { 1206efe556adSCorentin Chary rgb_prepare_row24(vs, dst, x, y, count); 12074043a013SCorentin Chary } else { 1208efe556adSCorentin Chary rgb_prepare_row32(vs, dst, x, y, count); 12094043a013SCorentin Chary } 12104043a013SCorentin Chary } else { 1211efe556adSCorentin Chary rgb_prepare_row16(vs, dst, x, y, count); 1212245f7b51SCorentin Chary } 12134043a013SCorentin Chary } 1214efe556adSCorentin Chary #endif /* CONFIG_VNC_JPEG or CONFIG_VNC_PNG */ 1215245f7b51SCorentin Chary 1216245f7b51SCorentin Chary /* 1217efe556adSCorentin Chary * JPEG compression stuff. 1218efe556adSCorentin Chary */ 1219efe556adSCorentin Chary #ifdef CONFIG_VNC_JPEG 1220efe556adSCorentin Chary /* 1221245f7b51SCorentin Chary * Destination manager implementation for JPEG library. 1222245f7b51SCorentin Chary */ 1223245f7b51SCorentin Chary 1224245f7b51SCorentin Chary /* This is called once per encoding */ 1225245f7b51SCorentin Chary static void jpeg_init_destination(j_compress_ptr cinfo) 1226245f7b51SCorentin Chary { 1227245f7b51SCorentin Chary VncState *vs = cinfo->client_data; 1228d1af0e05SCorentin Chary Buffer *buffer = &vs->tight.jpeg; 1229245f7b51SCorentin Chary 1230245f7b51SCorentin Chary cinfo->dest->next_output_byte = (JOCTET *)buffer->buffer + buffer->offset; 1231245f7b51SCorentin Chary cinfo->dest->free_in_buffer = (size_t)(buffer->capacity - buffer->offset); 1232245f7b51SCorentin Chary } 1233245f7b51SCorentin Chary 1234245f7b51SCorentin Chary /* This is called when we ran out of buffer (shouldn't happen!) */ 1235245f7b51SCorentin Chary static boolean jpeg_empty_output_buffer(j_compress_ptr cinfo) 1236245f7b51SCorentin Chary { 1237245f7b51SCorentin Chary VncState *vs = cinfo->client_data; 1238d1af0e05SCorentin Chary Buffer *buffer = &vs->tight.jpeg; 1239245f7b51SCorentin Chary 1240245f7b51SCorentin Chary buffer->offset = buffer->capacity; 1241245f7b51SCorentin Chary buffer_reserve(buffer, 2048); 1242245f7b51SCorentin Chary jpeg_init_destination(cinfo); 1243245f7b51SCorentin Chary return TRUE; 1244245f7b51SCorentin Chary } 1245245f7b51SCorentin Chary 1246245f7b51SCorentin Chary /* This is called when we are done processing data */ 1247245f7b51SCorentin Chary static void jpeg_term_destination(j_compress_ptr cinfo) 1248245f7b51SCorentin Chary { 1249245f7b51SCorentin Chary VncState *vs = cinfo->client_data; 1250d1af0e05SCorentin Chary Buffer *buffer = &vs->tight.jpeg; 1251245f7b51SCorentin Chary 1252245f7b51SCorentin Chary buffer->offset = buffer->capacity - cinfo->dest->free_in_buffer; 1253245f7b51SCorentin Chary } 1254245f7b51SCorentin Chary 1255245f7b51SCorentin Chary static int send_jpeg_rect(VncState *vs, int x, int y, int w, int h, int quality) 1256245f7b51SCorentin Chary { 1257245f7b51SCorentin Chary struct jpeg_compress_struct cinfo; 1258245f7b51SCorentin Chary struct jpeg_error_mgr jerr; 1259245f7b51SCorentin Chary struct jpeg_destination_mgr manager; 1260245f7b51SCorentin Chary JSAMPROW row[1]; 1261245f7b51SCorentin Chary uint8_t *buf; 1262245f7b51SCorentin Chary int dy; 1263245f7b51SCorentin Chary 1264245f7b51SCorentin Chary if (ds_get_bytes_per_pixel(vs->ds) == 1) 1265efe556adSCorentin Chary return send_full_color_rect(vs, x, y, w, h); 1266245f7b51SCorentin Chary 1267d1af0e05SCorentin Chary buffer_reserve(&vs->tight.jpeg, 2048); 1268245f7b51SCorentin Chary 1269245f7b51SCorentin Chary cinfo.err = jpeg_std_error(&jerr); 1270245f7b51SCorentin Chary jpeg_create_compress(&cinfo); 1271245f7b51SCorentin Chary 1272245f7b51SCorentin Chary cinfo.client_data = vs; 1273245f7b51SCorentin Chary cinfo.image_width = w; 1274245f7b51SCorentin Chary cinfo.image_height = h; 1275245f7b51SCorentin Chary cinfo.input_components = 3; 1276245f7b51SCorentin Chary cinfo.in_color_space = JCS_RGB; 1277245f7b51SCorentin Chary 1278245f7b51SCorentin Chary jpeg_set_defaults(&cinfo); 1279245f7b51SCorentin Chary jpeg_set_quality(&cinfo, quality, true); 1280245f7b51SCorentin Chary 1281245f7b51SCorentin Chary manager.init_destination = jpeg_init_destination; 1282245f7b51SCorentin Chary manager.empty_output_buffer = jpeg_empty_output_buffer; 1283245f7b51SCorentin Chary manager.term_destination = jpeg_term_destination; 1284245f7b51SCorentin Chary cinfo.dest = &manager; 1285245f7b51SCorentin Chary 1286245f7b51SCorentin Chary jpeg_start_compress(&cinfo, true); 1287245f7b51SCorentin Chary 12887267c094SAnthony Liguori buf = g_malloc(w * 3); 1289d9c18c24SCorentin Chary row[0] = buf; 1290245f7b51SCorentin Chary for (dy = 0; dy < h; dy++) { 1291efe556adSCorentin Chary rgb_prepare_row(vs, buf, x, y + dy, w); 1292245f7b51SCorentin Chary jpeg_write_scanlines(&cinfo, row, 1); 1293245f7b51SCorentin Chary } 12947267c094SAnthony Liguori g_free(buf); 1295245f7b51SCorentin Chary 1296245f7b51SCorentin Chary jpeg_finish_compress(&cinfo); 1297245f7b51SCorentin Chary jpeg_destroy_compress(&cinfo); 1298245f7b51SCorentin Chary 1299245f7b51SCorentin Chary vnc_write_u8(vs, VNC_TIGHT_JPEG << 4); 1300245f7b51SCorentin Chary 1301d1af0e05SCorentin Chary tight_send_compact_size(vs, vs->tight.jpeg.offset); 1302d1af0e05SCorentin Chary vnc_write(vs, vs->tight.jpeg.buffer, vs->tight.jpeg.offset); 1303d1af0e05SCorentin Chary buffer_reset(&vs->tight.jpeg); 1304245f7b51SCorentin Chary 1305245f7b51SCorentin Chary return 1; 1306245f7b51SCorentin Chary } 1307245f7b51SCorentin Chary #endif /* CONFIG_VNC_JPEG */ 1308245f7b51SCorentin Chary 1309efe556adSCorentin Chary /* 1310efe556adSCorentin Chary * PNG compression stuff. 1311efe556adSCorentin Chary */ 1312efe556adSCorentin Chary #ifdef CONFIG_VNC_PNG 13135136a052SCorentin Chary static void write_png_palette(int idx, uint32_t pix, void *opaque) 1314efe556adSCorentin Chary { 1315efe556adSCorentin Chary struct palette_cb_priv *priv = opaque; 1316efe556adSCorentin Chary VncState *vs = priv->vs; 1317efe556adSCorentin Chary png_colorp color = &priv->png_palette[idx]; 1318efe556adSCorentin Chary 1319d1af0e05SCorentin Chary if (vs->tight.pixel24) 1320efe556adSCorentin Chary { 1321*9f64916dSGerd Hoffmann color->red = (pix >> vs->client_pf.rshift) & vs->client_pf.rmax; 1322*9f64916dSGerd Hoffmann color->green = (pix >> vs->client_pf.gshift) & vs->client_pf.gmax; 1323*9f64916dSGerd Hoffmann color->blue = (pix >> vs->client_pf.bshift) & vs->client_pf.bmax; 1324efe556adSCorentin Chary } 1325efe556adSCorentin Chary else 1326efe556adSCorentin Chary { 1327efe556adSCorentin Chary int red, green, blue; 1328efe556adSCorentin Chary 1329*9f64916dSGerd Hoffmann red = (pix >> vs->client_pf.rshift) & vs->client_pf.rmax; 1330*9f64916dSGerd Hoffmann green = (pix >> vs->client_pf.gshift) & vs->client_pf.gmax; 1331*9f64916dSGerd Hoffmann blue = (pix >> vs->client_pf.bshift) & vs->client_pf.bmax; 1332*9f64916dSGerd Hoffmann color->red = ((red * 255 + vs->client_pf.rmax / 2) / 1333*9f64916dSGerd Hoffmann vs->client_pf.rmax); 1334*9f64916dSGerd Hoffmann color->green = ((green * 255 + vs->client_pf.gmax / 2) / 1335*9f64916dSGerd Hoffmann vs->client_pf.gmax); 1336*9f64916dSGerd Hoffmann color->blue = ((blue * 255 + vs->client_pf.bmax / 2) / 1337*9f64916dSGerd Hoffmann vs->client_pf.bmax); 1338efe556adSCorentin Chary } 1339efe556adSCorentin Chary } 1340efe556adSCorentin Chary 1341efe556adSCorentin Chary static void png_write_data(png_structp png_ptr, png_bytep data, 1342efe556adSCorentin Chary png_size_t length) 1343efe556adSCorentin Chary { 1344efe556adSCorentin Chary VncState *vs = png_get_io_ptr(png_ptr); 1345efe556adSCorentin Chary 1346d1af0e05SCorentin Chary buffer_reserve(&vs->tight.png, vs->tight.png.offset + length); 1347d1af0e05SCorentin Chary memcpy(vs->tight.png.buffer + vs->tight.png.offset, data, length); 1348efe556adSCorentin Chary 1349d1af0e05SCorentin Chary vs->tight.png.offset += length; 1350efe556adSCorentin Chary } 1351efe556adSCorentin Chary 1352efe556adSCorentin Chary static void png_flush_data(png_structp png_ptr) 1353efe556adSCorentin Chary { 1354efe556adSCorentin Chary } 1355efe556adSCorentin Chary 1356efe556adSCorentin Chary static void *vnc_png_malloc(png_structp png_ptr, png_size_t size) 1357efe556adSCorentin Chary { 13587267c094SAnthony Liguori return g_malloc(size); 1359efe556adSCorentin Chary } 1360efe556adSCorentin Chary 1361efe556adSCorentin Chary static void vnc_png_free(png_structp png_ptr, png_voidp ptr) 1362efe556adSCorentin Chary { 13637267c094SAnthony Liguori g_free(ptr); 1364efe556adSCorentin Chary } 1365efe556adSCorentin Chary 1366efe556adSCorentin Chary static int send_png_rect(VncState *vs, int x, int y, int w, int h, 13675136a052SCorentin Chary VncPalette *palette) 1368efe556adSCorentin Chary { 1369efe556adSCorentin Chary png_byte color_type; 1370efe556adSCorentin Chary png_structp png_ptr; 1371efe556adSCorentin Chary png_infop info_ptr; 1372efe556adSCorentin Chary png_colorp png_palette = NULL; 1373d1af0e05SCorentin Chary int level = tight_png_conf[vs->tight.compression].png_zlib_level; 1374d1af0e05SCorentin Chary int filters = tight_png_conf[vs->tight.compression].png_filters; 1375efe556adSCorentin Chary uint8_t *buf; 1376efe556adSCorentin Chary int dy; 1377efe556adSCorentin Chary 1378efe556adSCorentin Chary png_ptr = png_create_write_struct_2(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL, 1379efe556adSCorentin Chary NULL, vnc_png_malloc, vnc_png_free); 1380efe556adSCorentin Chary 1381efe556adSCorentin Chary if (png_ptr == NULL) 1382efe556adSCorentin Chary return -1; 1383efe556adSCorentin Chary 1384efe556adSCorentin Chary info_ptr = png_create_info_struct(png_ptr); 1385efe556adSCorentin Chary 1386efe556adSCorentin Chary if (info_ptr == NULL) { 1387efe556adSCorentin Chary png_destroy_write_struct(&png_ptr, NULL); 1388efe556adSCorentin Chary return -1; 1389efe556adSCorentin Chary } 1390efe556adSCorentin Chary 1391efe556adSCorentin Chary png_set_write_fn(png_ptr, (void *) vs, png_write_data, png_flush_data); 1392efe556adSCorentin Chary png_set_compression_level(png_ptr, level); 13933941bf6fSCorentin Chary png_set_filter(png_ptr, PNG_FILTER_TYPE_DEFAULT, filters); 1394efe556adSCorentin Chary 1395efe556adSCorentin Chary if (palette) { 1396efe556adSCorentin Chary color_type = PNG_COLOR_TYPE_PALETTE; 1397efe556adSCorentin Chary } else { 1398efe556adSCorentin Chary color_type = PNG_COLOR_TYPE_RGB; 1399efe556adSCorentin Chary } 1400efe556adSCorentin Chary 1401efe556adSCorentin Chary png_set_IHDR(png_ptr, info_ptr, w, h, 1402efe556adSCorentin Chary 8, color_type, PNG_INTERLACE_NONE, 1403efe556adSCorentin Chary PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); 1404efe556adSCorentin Chary 1405efe556adSCorentin Chary if (color_type == PNG_COLOR_TYPE_PALETTE) { 1406efe556adSCorentin Chary struct palette_cb_priv priv; 1407efe556adSCorentin Chary 1408efe556adSCorentin Chary png_palette = png_malloc(png_ptr, sizeof(*png_palette) * 14095136a052SCorentin Chary palette_size(palette)); 1410efe556adSCorentin Chary 1411efe556adSCorentin Chary priv.vs = vs; 1412efe556adSCorentin Chary priv.png_palette = png_palette; 14135136a052SCorentin Chary palette_iter(palette, write_png_palette, &priv); 1414efe556adSCorentin Chary 14155136a052SCorentin Chary png_set_PLTE(png_ptr, info_ptr, png_palette, palette_size(palette)); 1416efe556adSCorentin Chary 1417*9f64916dSGerd Hoffmann if (vs->client_pf.bytes_per_pixel == 4) { 1418d1af0e05SCorentin Chary tight_encode_indexed_rect32(vs->tight.tight.buffer, w * h, palette); 1419efe556adSCorentin Chary } else { 1420d1af0e05SCorentin Chary tight_encode_indexed_rect16(vs->tight.tight.buffer, w * h, palette); 1421efe556adSCorentin Chary } 1422efe556adSCorentin Chary } 1423efe556adSCorentin Chary 1424efe556adSCorentin Chary png_write_info(png_ptr, info_ptr); 1425efe556adSCorentin Chary 1426d1af0e05SCorentin Chary buffer_reserve(&vs->tight.png, 2048); 14277267c094SAnthony Liguori buf = g_malloc(w * 3); 1428efe556adSCorentin Chary for (dy = 0; dy < h; dy++) 1429efe556adSCorentin Chary { 1430efe556adSCorentin Chary if (color_type == PNG_COLOR_TYPE_PALETTE) { 1431d1af0e05SCorentin Chary memcpy(buf, vs->tight.tight.buffer + (dy * w), w); 1432efe556adSCorentin Chary } else { 1433efe556adSCorentin Chary rgb_prepare_row(vs, buf, x, y + dy, w); 1434efe556adSCorentin Chary } 1435efe556adSCorentin Chary png_write_row(png_ptr, buf); 1436efe556adSCorentin Chary } 14377267c094SAnthony Liguori g_free(buf); 1438efe556adSCorentin Chary 1439efe556adSCorentin Chary png_write_end(png_ptr, NULL); 1440efe556adSCorentin Chary 1441efe556adSCorentin Chary if (color_type == PNG_COLOR_TYPE_PALETTE) { 1442efe556adSCorentin Chary png_free(png_ptr, png_palette); 1443efe556adSCorentin Chary } 1444efe556adSCorentin Chary 1445efe556adSCorentin Chary png_destroy_write_struct(&png_ptr, &info_ptr); 1446efe556adSCorentin Chary 1447efe556adSCorentin Chary vnc_write_u8(vs, VNC_TIGHT_PNG << 4); 1448efe556adSCorentin Chary 1449d1af0e05SCorentin Chary tight_send_compact_size(vs, vs->tight.png.offset); 1450d1af0e05SCorentin Chary vnc_write(vs, vs->tight.png.buffer, vs->tight.png.offset); 1451d1af0e05SCorentin Chary buffer_reset(&vs->tight.png); 1452efe556adSCorentin Chary return 1; 1453efe556adSCorentin Chary } 1454efe556adSCorentin Chary #endif /* CONFIG_VNC_PNG */ 1455efe556adSCorentin Chary 1456245f7b51SCorentin Chary static void vnc_tight_start(VncState *vs) 1457245f7b51SCorentin Chary { 1458d1af0e05SCorentin Chary buffer_reset(&vs->tight.tight); 1459245f7b51SCorentin Chary 1460245f7b51SCorentin Chary // make the output buffer be the zlib buffer, so we can compress it later 1461d1af0e05SCorentin Chary vs->tight.tmp = vs->output; 1462d1af0e05SCorentin Chary vs->output = vs->tight.tight; 1463245f7b51SCorentin Chary } 1464245f7b51SCorentin Chary 1465245f7b51SCorentin Chary static void vnc_tight_stop(VncState *vs) 1466245f7b51SCorentin Chary { 1467245f7b51SCorentin Chary // switch back to normal output/zlib buffers 1468d1af0e05SCorentin Chary vs->tight.tight = vs->output; 1469d1af0e05SCorentin Chary vs->output = vs->tight.tmp; 1470245f7b51SCorentin Chary } 1471245f7b51SCorentin Chary 147203817eb8SCorentin Chary static int send_sub_rect_nojpeg(VncState *vs, int x, int y, int w, int h, 147303817eb8SCorentin Chary int bg, int fg, int colors, VncPalette *palette) 147403817eb8SCorentin Chary { 147503817eb8SCorentin Chary int ret; 147603817eb8SCorentin Chary 147703817eb8SCorentin Chary if (colors == 0) { 147803817eb8SCorentin Chary if (tight_detect_smooth_image(vs, w, h)) { 147903817eb8SCorentin Chary ret = send_gradient_rect(vs, x, y, w, h); 148003817eb8SCorentin Chary } else { 148103817eb8SCorentin Chary ret = send_full_color_rect(vs, x, y, w, h); 148203817eb8SCorentin Chary } 148303817eb8SCorentin Chary } else if (colors == 1) { 148403817eb8SCorentin Chary ret = send_solid_rect(vs); 148503817eb8SCorentin Chary } else if (colors == 2) { 148603817eb8SCorentin Chary ret = send_mono_rect(vs, x, y, w, h, bg, fg); 148703817eb8SCorentin Chary } else if (colors <= 256) { 148803817eb8SCorentin Chary ret = send_palette_rect(vs, x, y, w, h, palette); 1489d167f9bcSBlue Swirl } else { 1490d167f9bcSBlue Swirl ret = 0; 149103817eb8SCorentin Chary } 149203817eb8SCorentin Chary return ret; 149303817eb8SCorentin Chary } 149403817eb8SCorentin Chary 149503817eb8SCorentin Chary #ifdef CONFIG_VNC_JPEG 149603817eb8SCorentin Chary static int send_sub_rect_jpeg(VncState *vs, int x, int y, int w, int h, 149703817eb8SCorentin Chary int bg, int fg, int colors, 1498ce702e93SCorentin Chary VncPalette *palette, bool force) 149903817eb8SCorentin Chary { 150003817eb8SCorentin Chary int ret; 150103817eb8SCorentin Chary 150203817eb8SCorentin Chary if (colors == 0) { 1503ce702e93SCorentin Chary if (force || (tight_jpeg_conf[vs->tight.quality].jpeg_full && 1504ce702e93SCorentin Chary tight_detect_smooth_image(vs, w, h))) { 150503817eb8SCorentin Chary int quality = tight_conf[vs->tight.quality].jpeg_quality; 150603817eb8SCorentin Chary 150703817eb8SCorentin Chary ret = send_jpeg_rect(vs, x, y, w, h, quality); 150803817eb8SCorentin Chary } else { 150903817eb8SCorentin Chary ret = send_full_color_rect(vs, x, y, w, h); 151003817eb8SCorentin Chary } 151103817eb8SCorentin Chary } else if (colors == 1) { 151203817eb8SCorentin Chary ret = send_solid_rect(vs); 151303817eb8SCorentin Chary } else if (colors == 2) { 151403817eb8SCorentin Chary ret = send_mono_rect(vs, x, y, w, h, bg, fg); 151503817eb8SCorentin Chary } else if (colors <= 256) { 1516ce702e93SCorentin Chary if (force || (colors > 96 && 1517ce702e93SCorentin Chary tight_jpeg_conf[vs->tight.quality].jpeg_idx && 1518ce702e93SCorentin Chary tight_detect_smooth_image(vs, w, h))) { 151903817eb8SCorentin Chary int quality = tight_conf[vs->tight.quality].jpeg_quality; 152003817eb8SCorentin Chary 152103817eb8SCorentin Chary ret = send_jpeg_rect(vs, x, y, w, h, quality); 152203817eb8SCorentin Chary } else { 152303817eb8SCorentin Chary ret = send_palette_rect(vs, x, y, w, h, palette); 152403817eb8SCorentin Chary } 1525ad7ee4adSBlue Swirl } else { 1526ad7ee4adSBlue Swirl ret = 0; 152703817eb8SCorentin Chary } 152803817eb8SCorentin Chary return ret; 152903817eb8SCorentin Chary } 153003817eb8SCorentin Chary #endif 153103817eb8SCorentin Chary 1532245f7b51SCorentin Chary static int send_sub_rect(VncState *vs, int x, int y, int w, int h) 1533245f7b51SCorentin Chary { 15345136a052SCorentin Chary VncPalette *palette = NULL; 1535245f7b51SCorentin Chary uint32_t bg = 0, fg = 0; 1536245f7b51SCorentin Chary int colors; 1537245f7b51SCorentin Chary int ret = 0; 1538cf76a1ceSPeter Maydell #ifdef CONFIG_VNC_JPEG 1539ce702e93SCorentin Chary bool force_jpeg = false; 1540ce702e93SCorentin Chary bool allow_jpeg = true; 1541cf76a1ceSPeter Maydell #endif 1542245f7b51SCorentin Chary 1543d1af0e05SCorentin Chary vnc_framebuffer_update(vs, x, y, w, h, vs->tight.type); 1544245f7b51SCorentin Chary 1545245f7b51SCorentin Chary vnc_tight_start(vs); 1546245f7b51SCorentin Chary vnc_raw_send_framebuffer_update(vs, x, y, w, h); 1547245f7b51SCorentin Chary vnc_tight_stop(vs); 1548245f7b51SCorentin Chary 1549ce702e93SCorentin Chary #ifdef CONFIG_VNC_JPEG 155080e0c8c3SCorentin Chary if (!vs->vd->non_adaptive && vs->tight.quality != (uint8_t)-1) { 1551ce702e93SCorentin Chary double freq = vnc_update_freq(vs, x, y, w, h); 1552ce702e93SCorentin Chary 1553ce702e93SCorentin Chary if (freq < tight_jpeg_conf[vs->tight.quality].jpeg_freq_min) { 1554ce702e93SCorentin Chary allow_jpeg = false; 1555ce702e93SCorentin Chary } 1556ce702e93SCorentin Chary if (freq >= tight_jpeg_conf[vs->tight.quality].jpeg_freq_threshold) { 1557ce702e93SCorentin Chary force_jpeg = true; 1558ce702e93SCorentin Chary vnc_sent_lossy_rect(vs, x, y, w, h); 1559ce702e93SCorentin Chary } 1560ce702e93SCorentin Chary } 1561ce702e93SCorentin Chary #endif 1562ce702e93SCorentin Chary 1563245f7b51SCorentin Chary colors = tight_fill_palette(vs, x, y, w * h, &fg, &bg, &palette); 1564245f7b51SCorentin Chary 1565245f7b51SCorentin Chary #ifdef CONFIG_VNC_JPEG 1566ce702e93SCorentin Chary if (allow_jpeg && vs->tight.quality != (uint8_t)-1) { 1567ce702e93SCorentin Chary ret = send_sub_rect_jpeg(vs, x, y, w, h, bg, fg, colors, palette, 1568ce702e93SCorentin Chary force_jpeg); 1569245f7b51SCorentin Chary } else { 157003817eb8SCorentin Chary ret = send_sub_rect_nojpeg(vs, x, y, w, h, bg, fg, colors, palette); 1571245f7b51SCorentin Chary } 1572245f7b51SCorentin Chary #else 157303817eb8SCorentin Chary ret = send_sub_rect_nojpeg(vs, x, y, w, h, bg, fg, colors, palette); 1574245f7b51SCorentin Chary #endif 157503817eb8SCorentin Chary 15765136a052SCorentin Chary palette_destroy(palette); 1577245f7b51SCorentin Chary return ret; 1578245f7b51SCorentin Chary } 1579245f7b51SCorentin Chary 1580245f7b51SCorentin Chary static int send_sub_rect_solid(VncState *vs, int x, int y, int w, int h) 1581245f7b51SCorentin Chary { 1582d1af0e05SCorentin Chary vnc_framebuffer_update(vs, x, y, w, h, vs->tight.type); 1583245f7b51SCorentin Chary 1584245f7b51SCorentin Chary vnc_tight_start(vs); 1585245f7b51SCorentin Chary vnc_raw_send_framebuffer_update(vs, x, y, w, h); 1586245f7b51SCorentin Chary vnc_tight_stop(vs); 1587245f7b51SCorentin Chary 1588245f7b51SCorentin Chary return send_solid_rect(vs); 1589245f7b51SCorentin Chary } 1590245f7b51SCorentin Chary 1591ce702e93SCorentin Chary static int send_rect_simple(VncState *vs, int x, int y, int w, int h, 1592ce702e93SCorentin Chary bool split) 1593245f7b51SCorentin Chary { 1594245f7b51SCorentin Chary int max_size, max_width; 1595245f7b51SCorentin Chary int max_sub_width, max_sub_height; 1596245f7b51SCorentin Chary int dx, dy; 1597245f7b51SCorentin Chary int rw, rh; 1598245f7b51SCorentin Chary int n = 0; 1599245f7b51SCorentin Chary 1600d1af0e05SCorentin Chary max_size = tight_conf[vs->tight.compression].max_rect_size; 1601d1af0e05SCorentin Chary max_width = tight_conf[vs->tight.compression].max_rect_width; 1602245f7b51SCorentin Chary 1603ce702e93SCorentin Chary if (split && (w > max_width || w * h > max_size)) { 1604245f7b51SCorentin Chary max_sub_width = (w > max_width) ? max_width : w; 1605245f7b51SCorentin Chary max_sub_height = max_size / max_sub_width; 1606245f7b51SCorentin Chary 1607245f7b51SCorentin Chary for (dy = 0; dy < h; dy += max_sub_height) { 1608245f7b51SCorentin Chary for (dx = 0; dx < w; dx += max_width) { 1609245f7b51SCorentin Chary rw = MIN(max_sub_width, w - dx); 1610245f7b51SCorentin Chary rh = MIN(max_sub_height, h - dy); 1611245f7b51SCorentin Chary n += send_sub_rect(vs, x+dx, y+dy, rw, rh); 1612245f7b51SCorentin Chary } 1613245f7b51SCorentin Chary } 1614245f7b51SCorentin Chary } else { 1615245f7b51SCorentin Chary n += send_sub_rect(vs, x, y, w, h); 1616245f7b51SCorentin Chary } 1617245f7b51SCorentin Chary 1618245f7b51SCorentin Chary return n; 1619245f7b51SCorentin Chary } 1620245f7b51SCorentin Chary 1621245f7b51SCorentin Chary static int find_large_solid_color_rect(VncState *vs, int x, int y, 1622245f7b51SCorentin Chary int w, int h, int max_rows) 1623245f7b51SCorentin Chary { 1624245f7b51SCorentin Chary int dx, dy, dw, dh; 1625245f7b51SCorentin Chary int n = 0; 1626245f7b51SCorentin Chary 1627245f7b51SCorentin Chary /* Try to find large solid-color areas and send them separately. */ 1628245f7b51SCorentin Chary 1629245f7b51SCorentin Chary for (dy = y; dy < y + h; dy += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) { 1630245f7b51SCorentin Chary 1631245f7b51SCorentin Chary /* If a rectangle becomes too large, send its upper part now. */ 1632245f7b51SCorentin Chary 1633245f7b51SCorentin Chary if (dy - y >= max_rows) { 1634ce702e93SCorentin Chary n += send_rect_simple(vs, x, y, w, max_rows, true); 1635245f7b51SCorentin Chary y += max_rows; 1636245f7b51SCorentin Chary h -= max_rows; 1637245f7b51SCorentin Chary } 1638245f7b51SCorentin Chary 1639245f7b51SCorentin Chary dh = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, (y + h - dy)); 1640245f7b51SCorentin Chary 1641245f7b51SCorentin Chary for (dx = x; dx < x + w; dx += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) { 1642245f7b51SCorentin Chary uint32_t color_value; 1643245f7b51SCorentin Chary int x_best, y_best, w_best, h_best; 1644245f7b51SCorentin Chary 1645245f7b51SCorentin Chary dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, (x + w - dx)); 1646245f7b51SCorentin Chary 1647245f7b51SCorentin Chary if (!check_solid_tile(vs, dx, dy, dw, dh, &color_value, false)) { 1648245f7b51SCorentin Chary continue ; 1649245f7b51SCorentin Chary } 1650245f7b51SCorentin Chary 1651245f7b51SCorentin Chary /* Get dimensions of solid-color area. */ 1652245f7b51SCorentin Chary 1653245f7b51SCorentin Chary find_best_solid_area(vs, dx, dy, w - (dx - x), h - (dy - y), 1654245f7b51SCorentin Chary color_value, &w_best, &h_best); 1655245f7b51SCorentin Chary 1656245f7b51SCorentin Chary /* Make sure a solid rectangle is large enough 1657245f7b51SCorentin Chary (or the whole rectangle is of the same color). */ 1658245f7b51SCorentin Chary 1659245f7b51SCorentin Chary if (w_best * h_best != w * h && 1660245f7b51SCorentin Chary w_best * h_best < VNC_TIGHT_MIN_SOLID_SUBRECT_SIZE) { 1661245f7b51SCorentin Chary continue; 1662245f7b51SCorentin Chary } 1663245f7b51SCorentin Chary 1664245f7b51SCorentin Chary /* Try to extend solid rectangle to maximum size. */ 1665245f7b51SCorentin Chary 1666245f7b51SCorentin Chary x_best = dx; y_best = dy; 1667245f7b51SCorentin Chary extend_solid_area(vs, x, y, w, h, color_value, 1668245f7b51SCorentin Chary &x_best, &y_best, &w_best, &h_best); 1669245f7b51SCorentin Chary 1670245f7b51SCorentin Chary /* Send rectangles at top and left to solid-color area. */ 1671245f7b51SCorentin Chary 1672245f7b51SCorentin Chary if (y_best != y) { 1673ce702e93SCorentin Chary n += send_rect_simple(vs, x, y, w, y_best-y, true); 1674245f7b51SCorentin Chary } 1675245f7b51SCorentin Chary if (x_best != x) { 1676efe556adSCorentin Chary n += tight_send_framebuffer_update(vs, x, y_best, 1677245f7b51SCorentin Chary x_best-x, h_best); 1678245f7b51SCorentin Chary } 1679245f7b51SCorentin Chary 1680245f7b51SCorentin Chary /* Send solid-color rectangle. */ 1681245f7b51SCorentin Chary n += send_sub_rect_solid(vs, x_best, y_best, w_best, h_best); 1682245f7b51SCorentin Chary 1683245f7b51SCorentin Chary /* Send remaining rectangles (at right and bottom). */ 1684245f7b51SCorentin Chary 1685245f7b51SCorentin Chary if (x_best + w_best != x + w) { 1686efe556adSCorentin Chary n += tight_send_framebuffer_update(vs, x_best+w_best, 1687245f7b51SCorentin Chary y_best, 1688245f7b51SCorentin Chary w-(x_best-x)-w_best, 1689245f7b51SCorentin Chary h_best); 1690245f7b51SCorentin Chary } 1691245f7b51SCorentin Chary if (y_best + h_best != y + h) { 1692efe556adSCorentin Chary n += tight_send_framebuffer_update(vs, x, y_best+h_best, 1693245f7b51SCorentin Chary w, h-(y_best-y)-h_best); 1694245f7b51SCorentin Chary } 1695245f7b51SCorentin Chary 1696245f7b51SCorentin Chary /* Return after all recursive calls are done. */ 1697245f7b51SCorentin Chary return n; 1698245f7b51SCorentin Chary } 1699245f7b51SCorentin Chary } 1700ce702e93SCorentin Chary return n + send_rect_simple(vs, x, y, w, h, true); 1701245f7b51SCorentin Chary } 1702245f7b51SCorentin Chary 1703efe556adSCorentin Chary static int tight_send_framebuffer_update(VncState *vs, int x, int y, 1704245f7b51SCorentin Chary int w, int h) 1705245f7b51SCorentin Chary { 1706245f7b51SCorentin Chary int max_rows; 1707245f7b51SCorentin Chary 1708*9f64916dSGerd Hoffmann if (vs->client_pf.bytes_per_pixel == 4 && vs->client_pf.rmax == 0xFF && 1709*9f64916dSGerd Hoffmann vs->client_pf.bmax == 0xFF && vs->client_pf.gmax == 0xFF) { 1710d1af0e05SCorentin Chary vs->tight.pixel24 = true; 1711245f7b51SCorentin Chary } else { 1712d1af0e05SCorentin Chary vs->tight.pixel24 = false; 1713245f7b51SCorentin Chary } 1714245f7b51SCorentin Chary 1715cf76a1ceSPeter Maydell #ifdef CONFIG_VNC_JPEG 1716368d2588SCorentin Chary if (vs->tight.quality != (uint8_t)-1) { 1717ce702e93SCorentin Chary double freq = vnc_update_freq(vs, x, y, w, h); 1718ce702e93SCorentin Chary 1719ce702e93SCorentin Chary if (freq > tight_jpeg_conf[vs->tight.quality].jpeg_freq_threshold) { 1720ce702e93SCorentin Chary return send_rect_simple(vs, x, y, w, h, false); 1721ce702e93SCorentin Chary } 1722ce702e93SCorentin Chary } 1723cf76a1ceSPeter Maydell #endif 1724ce702e93SCorentin Chary 1725ce702e93SCorentin Chary if (w * h < VNC_TIGHT_MIN_SPLIT_RECT_SIZE) { 1726ce702e93SCorentin Chary return send_rect_simple(vs, x, y, w, h, true); 1727ce702e93SCorentin Chary } 1728245f7b51SCorentin Chary 1729245f7b51SCorentin Chary /* Calculate maximum number of rows in one non-solid rectangle. */ 1730245f7b51SCorentin Chary 1731d1af0e05SCorentin Chary max_rows = tight_conf[vs->tight.compression].max_rect_size; 1732d1af0e05SCorentin Chary max_rows /= MIN(tight_conf[vs->tight.compression].max_rect_width, w); 1733245f7b51SCorentin Chary 1734245f7b51SCorentin Chary return find_large_solid_color_rect(vs, x, y, w, h, max_rows); 1735245f7b51SCorentin Chary } 1736245f7b51SCorentin Chary 1737efe556adSCorentin Chary int vnc_tight_send_framebuffer_update(VncState *vs, int x, int y, 1738efe556adSCorentin Chary int w, int h) 1739efe556adSCorentin Chary { 1740d1af0e05SCorentin Chary vs->tight.type = VNC_ENCODING_TIGHT; 1741efe556adSCorentin Chary return tight_send_framebuffer_update(vs, x, y, w, h); 1742efe556adSCorentin Chary } 1743efe556adSCorentin Chary 1744efe556adSCorentin Chary int vnc_tight_png_send_framebuffer_update(VncState *vs, int x, int y, 1745efe556adSCorentin Chary int w, int h) 1746efe556adSCorentin Chary { 1747d1af0e05SCorentin Chary vs->tight.type = VNC_ENCODING_TIGHT_PNG; 1748efe556adSCorentin Chary return tight_send_framebuffer_update(vs, x, y, w, h); 1749efe556adSCorentin Chary } 1750efe556adSCorentin Chary 1751245f7b51SCorentin Chary void vnc_tight_clear(VncState *vs) 1752245f7b51SCorentin Chary { 1753245f7b51SCorentin Chary int i; 1754d1af0e05SCorentin Chary for (i=0; i<ARRAY_SIZE(vs->tight.stream); i++) { 1755d1af0e05SCorentin Chary if (vs->tight.stream[i].opaque) { 1756d1af0e05SCorentin Chary deflateEnd(&vs->tight.stream[i]); 1757245f7b51SCorentin Chary } 1758245f7b51SCorentin Chary } 1759245f7b51SCorentin Chary 1760d1af0e05SCorentin Chary buffer_free(&vs->tight.tight); 1761d1af0e05SCorentin Chary buffer_free(&vs->tight.zlib); 1762d1af0e05SCorentin Chary buffer_free(&vs->tight.gradient); 1763245f7b51SCorentin Chary #ifdef CONFIG_VNC_JPEG 1764d1af0e05SCorentin Chary buffer_free(&vs->tight.jpeg); 1765245f7b51SCorentin Chary #endif 1766b5469b11SCorentin Chary #ifdef CONFIG_VNC_PNG 1767b5469b11SCorentin Chary buffer_free(&vs->tight.png); 1768b5469b11SCorentin Chary #endif 1769245f7b51SCorentin Chary } 1770