xref: /qemu/ui/vnc-enc-tight.c (revision ad7ee4ad)
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 
31efe556adSCorentin Chary #ifdef CONFIG_VNC_PNG
32efe556adSCorentin Chary #include <png.h>
33efe556adSCorentin Chary #endif
34245f7b51SCorentin Chary #ifdef CONFIG_VNC_JPEG
35245f7b51SCorentin Chary #include <stdio.h>
36245f7b51SCorentin Chary #include <jpeglib.h>
37245f7b51SCorentin Chary #endif
38245f7b51SCorentin Chary 
39efe556adSCorentin Chary #include "qemu-common.h"
40efe556adSCorentin Chary 
41245f7b51SCorentin Chary #include "bswap.h"
42245f7b51SCorentin Chary #include "qint.h"
43245f7b51SCorentin Chary #include "vnc.h"
44245f7b51SCorentin Chary #include "vnc-enc-tight.h"
455136a052SCorentin Chary #include "vnc-palette.h"
46245f7b51SCorentin Chary 
47245f7b51SCorentin Chary /* Compression level stuff. The following array contains various
48245f7b51SCorentin Chary    encoder parameters for each of 10 compression levels (0..9).
49245f7b51SCorentin Chary    Last three parameters correspond to JPEG quality levels (0..9). */
50245f7b51SCorentin Chary 
51245f7b51SCorentin Chary static const struct {
52245f7b51SCorentin Chary     int max_rect_size, max_rect_width;
53245f7b51SCorentin Chary     int mono_min_rect_size, gradient_min_rect_size;
54245f7b51SCorentin Chary     int idx_zlib_level, mono_zlib_level, raw_zlib_level, gradient_zlib_level;
55245f7b51SCorentin Chary     int gradient_threshold, gradient_threshold24;
56245f7b51SCorentin Chary     int idx_max_colors_divisor;
57245f7b51SCorentin Chary     int jpeg_quality, jpeg_threshold, jpeg_threshold24;
58245f7b51SCorentin Chary } tight_conf[] = {
59245f7b51SCorentin Chary     {   512,   32,   6, 65536, 0, 0, 0, 0,   0,   0,   4,  5, 10000, 23000 },
60245f7b51SCorentin Chary     {  2048,  128,   6, 65536, 1, 1, 1, 0,   0,   0,   8, 10,  8000, 18000 },
61245f7b51SCorentin Chary     {  6144,  256,   8, 65536, 3, 3, 2, 0,   0,   0,  24, 15,  6500, 15000 },
62245f7b51SCorentin Chary     { 10240, 1024,  12, 65536, 5, 5, 3, 0,   0,   0,  32, 25,  5000, 12000 },
63245f7b51SCorentin Chary     { 16384, 2048,  12, 65536, 6, 6, 4, 0,   0,   0,  32, 37,  4000, 10000 },
64245f7b51SCorentin Chary     { 32768, 2048,  12,  4096, 7, 7, 5, 4, 150, 380,  32, 50,  3000,  8000 },
65245f7b51SCorentin Chary     { 65536, 2048,  16,  4096, 7, 7, 6, 4, 170, 420,  48, 60,  2000,  5000 },
66245f7b51SCorentin Chary     { 65536, 2048,  16,  4096, 8, 8, 7, 5, 180, 450,  64, 70,  1000,  2500 },
67245f7b51SCorentin Chary     { 65536, 2048,  32,  8192, 9, 9, 8, 6, 190, 475,  64, 75,   500,  1200 },
68245f7b51SCorentin Chary     { 65536, 2048,  32,  8192, 9, 9, 9, 6, 200, 500,  96, 80,   200,   500 }
69245f7b51SCorentin Chary };
70245f7b51SCorentin Chary 
71efe556adSCorentin Chary 
72efe556adSCorentin Chary static int tight_send_framebuffer_update(VncState *vs, int x, int y,
73efe556adSCorentin Chary                                          int w, int h);
74efe556adSCorentin Chary 
75efe556adSCorentin Chary #ifdef CONFIG_VNC_PNG
763941bf6fSCorentin Chary static const struct {
773941bf6fSCorentin Chary     int png_zlib_level, png_filters;
783941bf6fSCorentin Chary } tight_png_conf[] = {
793941bf6fSCorentin Chary     { 0, PNG_NO_FILTERS },
803941bf6fSCorentin Chary     { 1, PNG_NO_FILTERS },
813941bf6fSCorentin Chary     { 2, PNG_NO_FILTERS },
823941bf6fSCorentin Chary     { 3, PNG_NO_FILTERS },
833941bf6fSCorentin Chary     { 4, PNG_NO_FILTERS },
843941bf6fSCorentin Chary     { 5, PNG_ALL_FILTERS },
853941bf6fSCorentin Chary     { 6, PNG_ALL_FILTERS },
863941bf6fSCorentin Chary     { 7, PNG_ALL_FILTERS },
873941bf6fSCorentin Chary     { 8, PNG_ALL_FILTERS },
883941bf6fSCorentin Chary     { 9, PNG_ALL_FILTERS },
893941bf6fSCorentin Chary };
903941bf6fSCorentin Chary 
91efe556adSCorentin Chary static int send_png_rect(VncState *vs, int x, int y, int w, int h,
925136a052SCorentin Chary                          VncPalette *palette);
93efe556adSCorentin Chary 
94efe556adSCorentin Chary static bool tight_can_send_png_rect(VncState *vs, int w, int h)
95efe556adSCorentin Chary {
96d1af0e05SCorentin Chary     if (vs->tight.type != VNC_ENCODING_TIGHT_PNG) {
97efe556adSCorentin Chary         return false;
98efe556adSCorentin Chary     }
99efe556adSCorentin Chary 
100efe556adSCorentin Chary     if (ds_get_bytes_per_pixel(vs->ds) == 1 ||
101efe556adSCorentin Chary         vs->clientds.pf.bytes_per_pixel == 1) {
102efe556adSCorentin Chary         return false;
103efe556adSCorentin Chary     }
104efe556adSCorentin Chary 
105efe556adSCorentin Chary     return true;
106efe556adSCorentin Chary }
107efe556adSCorentin Chary #endif
108efe556adSCorentin Chary 
109245f7b51SCorentin Chary /*
110245f7b51SCorentin Chary  * Code to guess if given rectangle is suitable for smooth image
111245f7b51SCorentin Chary  * compression (by applying "gradient" filter or JPEG coder).
112245f7b51SCorentin Chary  */
113245f7b51SCorentin Chary 
114249cdb42SBlue Swirl static unsigned int
115245f7b51SCorentin Chary tight_detect_smooth_image24(VncState *vs, int w, int h)
116245f7b51SCorentin Chary {
117245f7b51SCorentin Chary     int off;
118245f7b51SCorentin Chary     int x, y, d, dx;
119249cdb42SBlue Swirl     unsigned int c;
120249cdb42SBlue Swirl     unsigned int stats[256];
121245f7b51SCorentin Chary     int pixels = 0;
122245f7b51SCorentin Chary     int pix, left[3];
123249cdb42SBlue Swirl     unsigned int errors;
124d1af0e05SCorentin Chary     unsigned char *buf = vs->tight.tight.buffer;
125245f7b51SCorentin Chary 
126245f7b51SCorentin Chary     /*
127245f7b51SCorentin Chary      * If client is big-endian, color samples begin from the second
128245f7b51SCorentin Chary      * byte (offset 1) of a 32-bit pixel value.
129245f7b51SCorentin Chary      */
130245f7b51SCorentin Chary     off = !!(vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG);
131245f7b51SCorentin Chary 
132245f7b51SCorentin Chary     memset(stats, 0, sizeof (stats));
133245f7b51SCorentin Chary 
134245f7b51SCorentin Chary     for (y = 0, x = 0; y < h && x < w;) {
135245f7b51SCorentin Chary         for (d = 0; d < h - y && d < w - x - VNC_TIGHT_DETECT_SUBROW_WIDTH;
136245f7b51SCorentin Chary              d++) {
137245f7b51SCorentin Chary             for (c = 0; c < 3; c++) {
138245f7b51SCorentin Chary                 left[c] = buf[((y+d)*w+x+d)*4+off+c] & 0xFF;
139245f7b51SCorentin Chary             }
140245f7b51SCorentin Chary             for (dx = 1; dx <= VNC_TIGHT_DETECT_SUBROW_WIDTH; dx++) {
141245f7b51SCorentin Chary                 for (c = 0; c < 3; c++) {
142245f7b51SCorentin Chary                     pix = buf[((y+d)*w+x+d+dx)*4+off+c] & 0xFF;
143245f7b51SCorentin Chary                     stats[abs(pix - left[c])]++;
144245f7b51SCorentin Chary                     left[c] = pix;
145245f7b51SCorentin Chary                 }
146245f7b51SCorentin Chary                 pixels++;
147245f7b51SCorentin Chary             }
148245f7b51SCorentin Chary         }
149245f7b51SCorentin Chary         if (w > h) {
150245f7b51SCorentin Chary             x += h;
151245f7b51SCorentin Chary             y = 0;
152245f7b51SCorentin Chary         } else {
153245f7b51SCorentin Chary             x = 0;
154245f7b51SCorentin Chary             y += w;
155245f7b51SCorentin Chary         }
156245f7b51SCorentin Chary     }
157245f7b51SCorentin Chary 
158245f7b51SCorentin Chary     /* 95% smooth or more ... */
159245f7b51SCorentin Chary     if (stats[0] * 33 / pixels >= 95) {
160245f7b51SCorentin Chary         return 0;
161245f7b51SCorentin Chary     }
162245f7b51SCorentin Chary 
163245f7b51SCorentin Chary     errors = 0;
164245f7b51SCorentin Chary     for (c = 1; c < 8; c++) {
165245f7b51SCorentin Chary         errors += stats[c] * (c * c);
166245f7b51SCorentin Chary         if (stats[c] == 0 || stats[c] > stats[c-1] * 2) {
167245f7b51SCorentin Chary             return 0;
168245f7b51SCorentin Chary         }
169245f7b51SCorentin Chary     }
170245f7b51SCorentin Chary     for (; c < 256; c++) {
171245f7b51SCorentin Chary         errors += stats[c] * (c * c);
172245f7b51SCorentin Chary     }
173245f7b51SCorentin Chary     errors /= (pixels * 3 - stats[0]);
174245f7b51SCorentin Chary 
175245f7b51SCorentin Chary     return errors;
176245f7b51SCorentin Chary }
177245f7b51SCorentin Chary 
178245f7b51SCorentin Chary #define DEFINE_DETECT_FUNCTION(bpp)                                     \
179245f7b51SCorentin Chary                                                                         \
180249cdb42SBlue Swirl     static unsigned int                                                 \
181245f7b51SCorentin Chary     tight_detect_smooth_image##bpp(VncState *vs, int w, int h) {        \
182245f7b51SCorentin Chary         bool endian;                                                    \
183245f7b51SCorentin Chary         uint##bpp##_t pix;                                              \
184245f7b51SCorentin Chary         int max[3], shift[3];                                           \
185245f7b51SCorentin Chary         int x, y, d, dx;                                                \
186249cdb42SBlue Swirl         unsigned int c;                                                 \
187249cdb42SBlue Swirl         unsigned int stats[256];                                        \
188245f7b51SCorentin Chary         int pixels = 0;                                                 \
189245f7b51SCorentin Chary         int sample, sum, left[3];                                       \
190249cdb42SBlue Swirl         unsigned int errors;                                            \
191d1af0e05SCorentin Chary         unsigned char *buf = vs->tight.tight.buffer;                    \
192245f7b51SCorentin Chary                                                                         \
193245f7b51SCorentin Chary         endian = ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) !=        \
194245f7b51SCorentin Chary                   (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG));     \
195245f7b51SCorentin Chary                                                                         \
196245f7b51SCorentin Chary                                                                         \
197245f7b51SCorentin Chary         max[0] = vs->clientds.pf.rmax;                                  \
198245f7b51SCorentin Chary         max[1] = vs->clientds.pf.gmax;                                  \
199245f7b51SCorentin Chary         max[2] = vs->clientds.pf.bmax;                                  \
200245f7b51SCorentin Chary         shift[0] = vs->clientds.pf.rshift;                              \
201245f7b51SCorentin Chary         shift[1] = vs->clientds.pf.gshift;                              \
202245f7b51SCorentin Chary         shift[2] = vs->clientds.pf.bshift;                              \
203245f7b51SCorentin Chary                                                                         \
204245f7b51SCorentin Chary         memset(stats, 0, sizeof(stats));                                \
205245f7b51SCorentin Chary                                                                         \
206245f7b51SCorentin Chary         y = 0, x = 0;                                                   \
207245f7b51SCorentin Chary         while (y < h && x < w) {                                        \
208245f7b51SCorentin Chary             for (d = 0; d < h - y &&                                    \
209245f7b51SCorentin Chary                      d < w - x - VNC_TIGHT_DETECT_SUBROW_WIDTH; d++) {  \
210245f7b51SCorentin Chary                 pix = ((uint##bpp##_t *)buf)[(y+d)*w+x+d];              \
211245f7b51SCorentin Chary                 if (endian) {                                           \
212245f7b51SCorentin Chary                     pix = bswap_##bpp(pix);                             \
213245f7b51SCorentin Chary                 }                                                       \
214245f7b51SCorentin Chary                 for (c = 0; c < 3; c++) {                               \
215245f7b51SCorentin Chary                     left[c] = (int)(pix >> shift[c] & max[c]);          \
216245f7b51SCorentin Chary                 }                                                       \
217245f7b51SCorentin Chary                 for (dx = 1; dx <= VNC_TIGHT_DETECT_SUBROW_WIDTH;       \
218245f7b51SCorentin Chary                      dx++) {                                            \
219245f7b51SCorentin Chary                     pix = ((uint##bpp##_t *)buf)[(y+d)*w+x+d+dx];       \
220245f7b51SCorentin Chary                     if (endian) {                                       \
221245f7b51SCorentin Chary                         pix = bswap_##bpp(pix);                         \
222245f7b51SCorentin Chary                     }                                                   \
223245f7b51SCorentin Chary                     sum = 0;                                            \
224245f7b51SCorentin Chary                     for (c = 0; c < 3; c++) {                           \
225245f7b51SCorentin Chary                         sample = (int)(pix >> shift[c] & max[c]);       \
226245f7b51SCorentin Chary                         sum += abs(sample - left[c]);                   \
227245f7b51SCorentin Chary                         left[c] = sample;                               \
228245f7b51SCorentin Chary                     }                                                   \
229245f7b51SCorentin Chary                     if (sum > 255) {                                    \
230245f7b51SCorentin Chary                         sum = 255;                                      \
231245f7b51SCorentin Chary                     }                                                   \
232245f7b51SCorentin Chary                     stats[sum]++;                                       \
233245f7b51SCorentin Chary                     pixels++;                                           \
234245f7b51SCorentin Chary                 }                                                       \
235245f7b51SCorentin Chary             }                                                           \
236245f7b51SCorentin Chary             if (w > h) {                                                \
237245f7b51SCorentin Chary                 x += h;                                                 \
238245f7b51SCorentin Chary                 y = 0;                                                  \
239245f7b51SCorentin Chary             } else {                                                    \
240245f7b51SCorentin Chary                 x = 0;                                                  \
241245f7b51SCorentin Chary                 y += w;                                                 \
242245f7b51SCorentin Chary             }                                                           \
243245f7b51SCorentin Chary         }                                                               \
244245f7b51SCorentin Chary                                                                         \
245245f7b51SCorentin Chary         if ((stats[0] + stats[1]) * 100 / pixels >= 90) {               \
246245f7b51SCorentin Chary             return 0;                                                   \
247245f7b51SCorentin Chary         }                                                               \
248245f7b51SCorentin Chary                                                                         \
249245f7b51SCorentin Chary         errors = 0;                                                     \
250245f7b51SCorentin Chary         for (c = 1; c < 8; c++) {                                       \
251245f7b51SCorentin Chary             errors += stats[c] * (c * c);                               \
252245f7b51SCorentin Chary             if (stats[c] == 0 || stats[c] > stats[c-1] * 2) {           \
253245f7b51SCorentin Chary                 return 0;                                               \
254245f7b51SCorentin Chary             }                                                           \
255245f7b51SCorentin Chary         }                                                               \
256245f7b51SCorentin Chary         for (; c < 256; c++) {                                          \
257245f7b51SCorentin Chary             errors += stats[c] * (c * c);                               \
258245f7b51SCorentin Chary         }                                                               \
259245f7b51SCorentin Chary         errors /= (pixels - stats[0]);                                  \
260245f7b51SCorentin Chary                                                                         \
261245f7b51SCorentin Chary         return errors;                                                  \
262245f7b51SCorentin Chary     }
263245f7b51SCorentin Chary 
264245f7b51SCorentin Chary DEFINE_DETECT_FUNCTION(16)
265245f7b51SCorentin Chary DEFINE_DETECT_FUNCTION(32)
266245f7b51SCorentin Chary 
267245f7b51SCorentin Chary static int
268245f7b51SCorentin Chary tight_detect_smooth_image(VncState *vs, int w, int h)
269245f7b51SCorentin Chary {
270249cdb42SBlue Swirl     unsigned int errors;
271d1af0e05SCorentin Chary     int compression = vs->tight.compression;
272d1af0e05SCorentin Chary     int quality = vs->tight.quality;
273245f7b51SCorentin Chary 
274245f7b51SCorentin Chary     if (!vs->vd->lossy) {
275245f7b51SCorentin Chary         return 0;
276245f7b51SCorentin Chary     }
277245f7b51SCorentin Chary 
278245f7b51SCorentin Chary     if (ds_get_bytes_per_pixel(vs->ds) == 1 ||
279245f7b51SCorentin Chary         vs->clientds.pf.bytes_per_pixel == 1 ||
280245f7b51SCorentin Chary         w < VNC_TIGHT_DETECT_MIN_WIDTH || h < VNC_TIGHT_DETECT_MIN_HEIGHT) {
281245f7b51SCorentin Chary         return 0;
282245f7b51SCorentin Chary     }
283245f7b51SCorentin Chary 
2847bccf573SBlue Swirl     if (vs->tight.quality != (uint8_t)-1) {
285245f7b51SCorentin Chary         if (w * h < VNC_TIGHT_JPEG_MIN_RECT_SIZE) {
286245f7b51SCorentin Chary             return 0;
287245f7b51SCorentin Chary         }
288245f7b51SCorentin Chary     } else {
289245f7b51SCorentin Chary         if (w * h < tight_conf[compression].gradient_min_rect_size) {
290245f7b51SCorentin Chary             return 0;
291245f7b51SCorentin Chary         }
292245f7b51SCorentin Chary     }
293245f7b51SCorentin Chary 
294245f7b51SCorentin Chary     if (vs->clientds.pf.bytes_per_pixel == 4) {
295d1af0e05SCorentin Chary         if (vs->tight.pixel24) {
296245f7b51SCorentin Chary             errors = tight_detect_smooth_image24(vs, w, h);
2977bccf573SBlue Swirl             if (vs->tight.quality != (uint8_t)-1) {
298245f7b51SCorentin Chary                 return (errors < tight_conf[quality].jpeg_threshold24);
299245f7b51SCorentin Chary             }
300245f7b51SCorentin Chary             return (errors < tight_conf[compression].gradient_threshold24);
301245f7b51SCorentin Chary         } else {
302245f7b51SCorentin Chary             errors = tight_detect_smooth_image32(vs, w, h);
303245f7b51SCorentin Chary         }
304245f7b51SCorentin Chary     } else {
305245f7b51SCorentin Chary         errors = tight_detect_smooth_image16(vs, w, h);
306245f7b51SCorentin Chary     }
307245f7b51SCorentin Chary     if (quality != -1) {
308245f7b51SCorentin Chary         return (errors < tight_conf[quality].jpeg_threshold);
309245f7b51SCorentin Chary     }
310245f7b51SCorentin Chary     return (errors < tight_conf[compression].gradient_threshold);
311245f7b51SCorentin Chary }
312245f7b51SCorentin Chary 
313245f7b51SCorentin Chary /*
314245f7b51SCorentin Chary  * Code to determine how many different colors used in rectangle.
315245f7b51SCorentin Chary  */
316245f7b51SCorentin Chary #define DEFINE_FILL_PALETTE_FUNCTION(bpp)                               \
317245f7b51SCorentin Chary                                                                         \
318245f7b51SCorentin Chary     static int                                                          \
319245f7b51SCorentin Chary     tight_fill_palette##bpp(VncState *vs, int x, int y,                 \
320245f7b51SCorentin Chary                             int max, size_t count,                      \
321245f7b51SCorentin Chary                             uint32_t *bg, uint32_t *fg,                 \
3225136a052SCorentin Chary                             VncPalette **palette) {                     \
323245f7b51SCorentin Chary         uint##bpp##_t *data;                                            \
324245f7b51SCorentin Chary         uint##bpp##_t c0, c1, ci;                                       \
325245f7b51SCorentin Chary         int i, n0, n1;                                                  \
326245f7b51SCorentin Chary                                                                         \
327d1af0e05SCorentin Chary         data = (uint##bpp##_t *)vs->tight.tight.buffer;                 \
328245f7b51SCorentin Chary                                                                         \
329245f7b51SCorentin Chary         c0 = data[0];                                                   \
330245f7b51SCorentin Chary         i = 1;                                                          \
331245f7b51SCorentin Chary         while (i < count && data[i] == c0)                              \
332245f7b51SCorentin Chary             i++;                                                        \
333245f7b51SCorentin Chary         if (i >= count) {                                               \
334245f7b51SCorentin Chary             *bg = *fg = c0;                                             \
335245f7b51SCorentin Chary             return 1;                                                   \
336245f7b51SCorentin Chary         }                                                               \
337245f7b51SCorentin Chary                                                                         \
338245f7b51SCorentin Chary         if (max < 2) {                                                  \
339245f7b51SCorentin Chary             return 0;                                                   \
340245f7b51SCorentin Chary         }                                                               \
341245f7b51SCorentin Chary                                                                         \
342245f7b51SCorentin Chary         n0 = i;                                                         \
343245f7b51SCorentin Chary         c1 = data[i];                                                   \
344245f7b51SCorentin Chary         n1 = 0;                                                         \
345245f7b51SCorentin Chary         for (i++; i < count; i++) {                                     \
346245f7b51SCorentin Chary             ci = data[i];                                               \
347245f7b51SCorentin Chary             if (ci == c0) {                                             \
348245f7b51SCorentin Chary                 n0++;                                                   \
349245f7b51SCorentin Chary             } else if (ci == c1) {                                      \
350245f7b51SCorentin Chary                 n1++;                                                   \
351245f7b51SCorentin Chary             } else                                                      \
352245f7b51SCorentin Chary                 break;                                                  \
353245f7b51SCorentin Chary         }                                                               \
354245f7b51SCorentin Chary         if (i >= count) {                                               \
355245f7b51SCorentin Chary             if (n0 > n1) {                                              \
356245f7b51SCorentin Chary                 *bg = (uint32_t)c0;                                     \
357245f7b51SCorentin Chary                 *fg = (uint32_t)c1;                                     \
358245f7b51SCorentin Chary             } else {                                                    \
359245f7b51SCorentin Chary                 *bg = (uint32_t)c1;                                     \
360245f7b51SCorentin Chary                 *fg = (uint32_t)c0;                                     \
361245f7b51SCorentin Chary             }                                                           \
362245f7b51SCorentin Chary             return 2;                                                   \
363245f7b51SCorentin Chary         }                                                               \
364245f7b51SCorentin Chary                                                                         \
365245f7b51SCorentin Chary         if (max == 2) {                                                 \
366245f7b51SCorentin Chary             return 0;                                                   \
367245f7b51SCorentin Chary         }                                                               \
368245f7b51SCorentin Chary                                                                         \
3695136a052SCorentin Chary         *palette = palette_new(max, bpp);                               \
3705136a052SCorentin Chary         palette_put(*palette, c0);                                      \
3715136a052SCorentin Chary         palette_put(*palette, c1);                                      \
3725136a052SCorentin Chary         palette_put(*palette, ci);                                      \
373245f7b51SCorentin Chary                                                                         \
374245f7b51SCorentin Chary         for (i++; i < count; i++) {                                     \
375245f7b51SCorentin Chary             if (data[i] == ci) {                                        \
376245f7b51SCorentin Chary                 continue;                                               \
377245f7b51SCorentin Chary             } else {                                                    \
3785d8efe39SCorentin Chary                 ci = data[i];                                           \
3795136a052SCorentin Chary                 if (!palette_put(*palette, (uint32_t)ci)) {             \
380245f7b51SCorentin Chary                     return 0;                                           \
381245f7b51SCorentin Chary                 }                                                       \
382245f7b51SCorentin Chary             }                                                           \
383245f7b51SCorentin Chary         }                                                               \
384245f7b51SCorentin Chary                                                                         \
3855136a052SCorentin Chary         return palette_size(*palette);                                  \
386245f7b51SCorentin Chary     }
387245f7b51SCorentin Chary 
388245f7b51SCorentin Chary DEFINE_FILL_PALETTE_FUNCTION(8)
389245f7b51SCorentin Chary DEFINE_FILL_PALETTE_FUNCTION(16)
390245f7b51SCorentin Chary DEFINE_FILL_PALETTE_FUNCTION(32)
391245f7b51SCorentin Chary 
392245f7b51SCorentin Chary static int tight_fill_palette(VncState *vs, int x, int y,
393245f7b51SCorentin Chary                               size_t count, uint32_t *bg, uint32_t *fg,
3945136a052SCorentin Chary                               VncPalette **palette)
395245f7b51SCorentin Chary {
396245f7b51SCorentin Chary     int max;
397245f7b51SCorentin Chary 
398d1af0e05SCorentin Chary     max = count / tight_conf[vs->tight.compression].idx_max_colors_divisor;
399245f7b51SCorentin Chary     if (max < 2 &&
400d1af0e05SCorentin Chary         count >= tight_conf[vs->tight.compression].mono_min_rect_size) {
401245f7b51SCorentin Chary         max = 2;
402245f7b51SCorentin Chary     }
403245f7b51SCorentin Chary     if (max >= 256) {
404245f7b51SCorentin Chary         max = 256;
405245f7b51SCorentin Chary     }
406245f7b51SCorentin Chary 
407245f7b51SCorentin Chary     switch(vs->clientds.pf.bytes_per_pixel) {
408245f7b51SCorentin Chary     case 4:
409245f7b51SCorentin Chary         return tight_fill_palette32(vs, x, y, max, count, bg, fg, palette);
410245f7b51SCorentin Chary     case 2:
411245f7b51SCorentin Chary         return tight_fill_palette16(vs, x, y, max, count, bg, fg, palette);
412245f7b51SCorentin Chary     default:
413245f7b51SCorentin Chary         max = 2;
414245f7b51SCorentin Chary         return tight_fill_palette8(vs, x, y, max, count, bg, fg, palette);
415245f7b51SCorentin Chary     }
416245f7b51SCorentin Chary     return 0;
417245f7b51SCorentin Chary }
418245f7b51SCorentin Chary 
419245f7b51SCorentin Chary /*
420245f7b51SCorentin Chary  * Converting truecolor samples into palette indices.
421245f7b51SCorentin Chary  */
422245f7b51SCorentin Chary #define DEFINE_IDX_ENCODE_FUNCTION(bpp)                                 \
423245f7b51SCorentin Chary                                                                         \
424245f7b51SCorentin Chary     static void                                                         \
425245f7b51SCorentin Chary     tight_encode_indexed_rect##bpp(uint8_t *buf, int count,             \
4265136a052SCorentin Chary                                    VncPalette *palette) {               \
427245f7b51SCorentin Chary         uint##bpp##_t *src;                                             \
428245f7b51SCorentin Chary         uint##bpp##_t rgb;                                              \
429245f7b51SCorentin Chary         int i, rep;                                                     \
430245f7b51SCorentin Chary         uint8_t idx;                                                    \
431245f7b51SCorentin Chary                                                                         \
432245f7b51SCorentin Chary         src = (uint##bpp##_t *) buf;                                    \
433245f7b51SCorentin Chary                                                                         \
434245f7b51SCorentin Chary         for (i = 0; i < count; i++) {                                   \
435efe556adSCorentin Chary                                                                         \
436245f7b51SCorentin Chary             rgb = *src++;                                               \
437245f7b51SCorentin Chary             rep = 0;                                                    \
438245f7b51SCorentin Chary             while (i < count && *src == rgb) {                          \
439245f7b51SCorentin Chary                 rep++, src++, i++;                                      \
440245f7b51SCorentin Chary             }                                                           \
4415136a052SCorentin Chary             idx = palette_idx(palette, rgb);                            \
442245f7b51SCorentin Chary             /*                                                          \
443245f7b51SCorentin Chary              * Should never happen, but don't break everything          \
444245f7b51SCorentin Chary              * if it does, use the first color instead                  \
445245f7b51SCorentin Chary              */                                                         \
4467bccf573SBlue Swirl             if (idx == (uint8_t)-1) {                                   \
447245f7b51SCorentin Chary                 idx = 0;                                                \
448245f7b51SCorentin Chary             }                                                           \
449245f7b51SCorentin Chary             while (rep >= 0) {                                          \
450245f7b51SCorentin Chary                 *buf++ = idx;                                           \
451245f7b51SCorentin Chary                 rep--;                                                  \
452245f7b51SCorentin Chary             }                                                           \
453245f7b51SCorentin Chary         }                                                               \
454245f7b51SCorentin Chary     }
455245f7b51SCorentin Chary 
456245f7b51SCorentin Chary DEFINE_IDX_ENCODE_FUNCTION(16)
457245f7b51SCorentin Chary DEFINE_IDX_ENCODE_FUNCTION(32)
458245f7b51SCorentin Chary 
459245f7b51SCorentin Chary #define DEFINE_MONO_ENCODE_FUNCTION(bpp)                                \
460245f7b51SCorentin Chary                                                                         \
461245f7b51SCorentin Chary     static void                                                         \
462245f7b51SCorentin Chary     tight_encode_mono_rect##bpp(uint8_t *buf, int w, int h,             \
463245f7b51SCorentin Chary                                 uint##bpp##_t bg, uint##bpp##_t fg) {   \
464245f7b51SCorentin Chary         uint##bpp##_t *ptr;                                             \
465245f7b51SCorentin Chary         unsigned int value, mask;                                       \
466245f7b51SCorentin Chary         int aligned_width;                                              \
467245f7b51SCorentin Chary         int x, y, bg_bits;                                              \
468245f7b51SCorentin Chary                                                                         \
469245f7b51SCorentin Chary         ptr = (uint##bpp##_t *) buf;                                    \
470245f7b51SCorentin Chary         aligned_width = w - w % 8;                                      \
471245f7b51SCorentin Chary                                                                         \
472245f7b51SCorentin Chary         for (y = 0; y < h; y++) {                                       \
473245f7b51SCorentin Chary             for (x = 0; x < aligned_width; x += 8) {                    \
474245f7b51SCorentin Chary                 for (bg_bits = 0; bg_bits < 8; bg_bits++) {             \
475245f7b51SCorentin Chary                     if (*ptr++ != bg) {                                 \
476245f7b51SCorentin Chary                         break;                                          \
477245f7b51SCorentin Chary                     }                                                   \
478245f7b51SCorentin Chary                 }                                                       \
479245f7b51SCorentin Chary                 if (bg_bits == 8) {                                     \
480245f7b51SCorentin Chary                     *buf++ = 0;                                         \
481245f7b51SCorentin Chary                     continue;                                           \
482245f7b51SCorentin Chary                 }                                                       \
483245f7b51SCorentin Chary                 mask = 0x80 >> bg_bits;                                 \
484245f7b51SCorentin Chary                 value = mask;                                           \
485245f7b51SCorentin Chary                 for (bg_bits++; bg_bits < 8; bg_bits++) {               \
486245f7b51SCorentin Chary                     mask >>= 1;                                         \
487245f7b51SCorentin Chary                     if (*ptr++ != bg) {                                 \
488245f7b51SCorentin Chary                         value |= mask;                                  \
489245f7b51SCorentin Chary                     }                                                   \
490245f7b51SCorentin Chary                 }                                                       \
491245f7b51SCorentin Chary                 *buf++ = (uint8_t)value;                                \
492245f7b51SCorentin Chary             }                                                           \
493245f7b51SCorentin Chary                                                                         \
494245f7b51SCorentin Chary             mask = 0x80;                                                \
495245f7b51SCorentin Chary             value = 0;                                                  \
496245f7b51SCorentin Chary             if (x >= w) {                                               \
497245f7b51SCorentin Chary                 continue;                                               \
498245f7b51SCorentin Chary             }                                                           \
499245f7b51SCorentin Chary                                                                         \
500245f7b51SCorentin Chary             for (; x < w; x++) {                                        \
501245f7b51SCorentin Chary                 if (*ptr++ != bg) {                                     \
502245f7b51SCorentin Chary                     value |= mask;                                      \
503245f7b51SCorentin Chary                 }                                                       \
504245f7b51SCorentin Chary                 mask >>= 1;                                             \
505245f7b51SCorentin Chary             }                                                           \
506245f7b51SCorentin Chary             *buf++ = (uint8_t)value;                                    \
507245f7b51SCorentin Chary         }                                                               \
508245f7b51SCorentin Chary     }
509245f7b51SCorentin Chary 
510245f7b51SCorentin Chary DEFINE_MONO_ENCODE_FUNCTION(8)
511245f7b51SCorentin Chary DEFINE_MONO_ENCODE_FUNCTION(16)
512245f7b51SCorentin Chary DEFINE_MONO_ENCODE_FUNCTION(32)
513245f7b51SCorentin Chary 
514245f7b51SCorentin Chary /*
515245f7b51SCorentin Chary  * ``Gradient'' filter for 24-bit color samples.
516245f7b51SCorentin Chary  * Should be called only when redMax, greenMax and blueMax are 255.
517245f7b51SCorentin Chary  * Color components assumed to be byte-aligned.
518245f7b51SCorentin Chary  */
519245f7b51SCorentin Chary 
520245f7b51SCorentin Chary static void
521245f7b51SCorentin Chary tight_filter_gradient24(VncState *vs, uint8_t *buf, int w, int h)
522245f7b51SCorentin Chary {
523245f7b51SCorentin Chary     uint32_t *buf32;
524245f7b51SCorentin Chary     uint32_t pix32;
525245f7b51SCorentin Chary     int shift[3];
526245f7b51SCorentin Chary     int *prev;
527245f7b51SCorentin Chary     int here[3], upper[3], left[3], upperleft[3];
528245f7b51SCorentin Chary     int prediction;
529245f7b51SCorentin Chary     int x, y, c;
530245f7b51SCorentin Chary 
531245f7b51SCorentin Chary     buf32 = (uint32_t *)buf;
532d1af0e05SCorentin Chary     memset(vs->tight.gradient.buffer, 0, w * 3 * sizeof(int));
533245f7b51SCorentin Chary 
534245f7b51SCorentin Chary     if ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) ==
535245f7b51SCorentin Chary         (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG)) {
536245f7b51SCorentin Chary         shift[0] = vs->clientds.pf.rshift;
537245f7b51SCorentin Chary         shift[1] = vs->clientds.pf.gshift;
538245f7b51SCorentin Chary         shift[2] = vs->clientds.pf.bshift;
539245f7b51SCorentin Chary     } else {
540245f7b51SCorentin Chary         shift[0] = 24 - vs->clientds.pf.rshift;
541245f7b51SCorentin Chary         shift[1] = 24 - vs->clientds.pf.gshift;
542245f7b51SCorentin Chary         shift[2] = 24 - vs->clientds.pf.bshift;
543245f7b51SCorentin Chary     }
544245f7b51SCorentin Chary 
545245f7b51SCorentin Chary     for (y = 0; y < h; y++) {
546245f7b51SCorentin Chary         for (c = 0; c < 3; c++) {
547245f7b51SCorentin Chary             upper[c] = 0;
548245f7b51SCorentin Chary             here[c] = 0;
549245f7b51SCorentin Chary         }
550d1af0e05SCorentin Chary         prev = (int *)vs->tight.gradient.buffer;
551245f7b51SCorentin Chary         for (x = 0; x < w; x++) {
552245f7b51SCorentin Chary             pix32 = *buf32++;
553245f7b51SCorentin Chary             for (c = 0; c < 3; c++) {
554245f7b51SCorentin Chary                 upperleft[c] = upper[c];
555245f7b51SCorentin Chary                 left[c] = here[c];
556245f7b51SCorentin Chary                 upper[c] = *prev;
557245f7b51SCorentin Chary                 here[c] = (int)(pix32 >> shift[c] & 0xFF);
558245f7b51SCorentin Chary                 *prev++ = here[c];
559245f7b51SCorentin Chary 
560245f7b51SCorentin Chary                 prediction = left[c] + upper[c] - upperleft[c];
561245f7b51SCorentin Chary                 if (prediction < 0) {
562245f7b51SCorentin Chary                     prediction = 0;
563245f7b51SCorentin Chary                 } else if (prediction > 0xFF) {
564245f7b51SCorentin Chary                     prediction = 0xFF;
565245f7b51SCorentin Chary                 }
566245f7b51SCorentin Chary                 *buf++ = (char)(here[c] - prediction);
567245f7b51SCorentin Chary             }
568245f7b51SCorentin Chary         }
569245f7b51SCorentin Chary     }
570245f7b51SCorentin Chary }
571245f7b51SCorentin Chary 
572245f7b51SCorentin Chary 
573245f7b51SCorentin Chary /*
574245f7b51SCorentin Chary  * ``Gradient'' filter for other color depths.
575245f7b51SCorentin Chary  */
576245f7b51SCorentin Chary 
577245f7b51SCorentin Chary #define DEFINE_GRADIENT_FILTER_FUNCTION(bpp)                            \
578245f7b51SCorentin Chary                                                                         \
579245f7b51SCorentin Chary     static void                                                         \
580245f7b51SCorentin Chary     tight_filter_gradient##bpp(VncState *vs, uint##bpp##_t *buf,        \
581245f7b51SCorentin Chary                                int w, int h) {                          \
582245f7b51SCorentin Chary         uint##bpp##_t pix, diff;                                        \
583245f7b51SCorentin Chary         bool endian;                                                    \
584245f7b51SCorentin Chary         int *prev;                                                      \
585245f7b51SCorentin Chary         int max[3], shift[3];                                           \
586245f7b51SCorentin Chary         int here[3], upper[3], left[3], upperleft[3];                   \
587245f7b51SCorentin Chary         int prediction;                                                 \
588245f7b51SCorentin Chary         int x, y, c;                                                    \
589245f7b51SCorentin Chary                                                                         \
590d1af0e05SCorentin Chary         memset (vs->tight.gradient.buffer, 0, w * 3 * sizeof(int));     \
591245f7b51SCorentin Chary                                                                         \
592245f7b51SCorentin Chary         endian = ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) !=        \
593245f7b51SCorentin Chary                   (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG));     \
594245f7b51SCorentin Chary                                                                         \
595245f7b51SCorentin Chary         max[0] = vs->clientds.pf.rmax;                                  \
596245f7b51SCorentin Chary         max[1] = vs->clientds.pf.gmax;                                  \
597245f7b51SCorentin Chary         max[2] = vs->clientds.pf.bmax;                                  \
598245f7b51SCorentin Chary         shift[0] = vs->clientds.pf.rshift;                              \
599245f7b51SCorentin Chary         shift[1] = vs->clientds.pf.gshift;                              \
600245f7b51SCorentin Chary         shift[2] = vs->clientds.pf.bshift;                              \
601245f7b51SCorentin Chary                                                                         \
602245f7b51SCorentin Chary         for (y = 0; y < h; y++) {                                       \
603245f7b51SCorentin Chary             for (c = 0; c < 3; c++) {                                   \
604245f7b51SCorentin Chary                 upper[c] = 0;                                           \
605245f7b51SCorentin Chary                 here[c] = 0;                                            \
606245f7b51SCorentin Chary             }                                                           \
607d1af0e05SCorentin Chary             prev = (int *)vs->tight.gradient.buffer;                    \
608245f7b51SCorentin Chary             for (x = 0; x < w; x++) {                                   \
609245f7b51SCorentin Chary                 pix = *buf;                                             \
610245f7b51SCorentin Chary                 if (endian) {                                           \
611245f7b51SCorentin Chary                     pix = bswap_##bpp(pix);                             \
612245f7b51SCorentin Chary                 }                                                       \
613245f7b51SCorentin Chary                 diff = 0;                                               \
614245f7b51SCorentin Chary                 for (c = 0; c < 3; c++) {                               \
615245f7b51SCorentin Chary                     upperleft[c] = upper[c];                            \
616245f7b51SCorentin Chary                     left[c] = here[c];                                  \
617245f7b51SCorentin Chary                     upper[c] = *prev;                                   \
618245f7b51SCorentin Chary                     here[c] = (int)(pix >> shift[c] & max[c]);          \
619245f7b51SCorentin Chary                     *prev++ = here[c];                                  \
620245f7b51SCorentin Chary                                                                         \
621245f7b51SCorentin Chary                     prediction = left[c] + upper[c] - upperleft[c];     \
622245f7b51SCorentin Chary                     if (prediction < 0) {                               \
623245f7b51SCorentin Chary                         prediction = 0;                                 \
624245f7b51SCorentin Chary                     } else if (prediction > max[c]) {                   \
625245f7b51SCorentin Chary                         prediction = max[c];                            \
626245f7b51SCorentin Chary                     }                                                   \
627245f7b51SCorentin Chary                     diff |= ((here[c] - prediction) & max[c])           \
628245f7b51SCorentin Chary                         << shift[c];                                    \
629245f7b51SCorentin Chary                 }                                                       \
630245f7b51SCorentin Chary                 if (endian) {                                           \
631245f7b51SCorentin Chary                     diff = bswap_##bpp(diff);                           \
632245f7b51SCorentin Chary                 }                                                       \
633245f7b51SCorentin Chary                 *buf++ = diff;                                          \
634245f7b51SCorentin Chary             }                                                           \
635245f7b51SCorentin Chary         }                                                               \
636245f7b51SCorentin Chary     }
637245f7b51SCorentin Chary 
638245f7b51SCorentin Chary DEFINE_GRADIENT_FILTER_FUNCTION(16)
639245f7b51SCorentin Chary DEFINE_GRADIENT_FILTER_FUNCTION(32)
640245f7b51SCorentin Chary 
641245f7b51SCorentin Chary /*
642245f7b51SCorentin Chary  * Check if a rectangle is all of the same color. If needSameColor is
643245f7b51SCorentin Chary  * set to non-zero, then also check that its color equals to the
644245f7b51SCorentin Chary  * *colorPtr value. The result is 1 if the test is successfull, and in
645245f7b51SCorentin Chary  * that case new color will be stored in *colorPtr.
646245f7b51SCorentin Chary  */
647245f7b51SCorentin Chary 
648245f7b51SCorentin Chary #define DEFINE_CHECK_SOLID_FUNCTION(bpp)                                \
649245f7b51SCorentin Chary                                                                         \
650245f7b51SCorentin Chary     static bool                                                         \
651245f7b51SCorentin Chary     check_solid_tile##bpp(VncState *vs, int x, int y, int w, int h,     \
652245f7b51SCorentin Chary                           uint32_t* color, bool samecolor)              \
653245f7b51SCorentin Chary     {                                                                   \
654245f7b51SCorentin Chary         VncDisplay *vd = vs->vd;                                        \
655245f7b51SCorentin Chary         uint##bpp##_t *fbptr;                                           \
656245f7b51SCorentin Chary         uint##bpp##_t c;                                                \
657245f7b51SCorentin Chary         int dx, dy;                                                     \
658245f7b51SCorentin Chary                                                                         \
659245f7b51SCorentin Chary         fbptr = (uint##bpp##_t *)                                       \
660245f7b51SCorentin Chary             (vd->server->data + y * ds_get_linesize(vs->ds) +           \
661245f7b51SCorentin Chary              x * ds_get_bytes_per_pixel(vs->ds));                       \
662245f7b51SCorentin Chary                                                                         \
663245f7b51SCorentin Chary         c = *fbptr;                                                     \
664245f7b51SCorentin Chary         if (samecolor && (uint32_t)c != *color) {                       \
665245f7b51SCorentin Chary             return false;                                               \
666245f7b51SCorentin Chary         }                                                               \
667245f7b51SCorentin Chary                                                                         \
668245f7b51SCorentin Chary         for (dy = 0; dy < h; dy++) {                                    \
669245f7b51SCorentin Chary             for (dx = 0; dx < w; dx++) {                                \
670245f7b51SCorentin Chary                 if (c != fbptr[dx]) {                                   \
671245f7b51SCorentin Chary                     return false;                                       \
672245f7b51SCorentin Chary                 }                                                       \
673245f7b51SCorentin Chary             }                                                           \
674245f7b51SCorentin Chary             fbptr = (uint##bpp##_t *)                                   \
675245f7b51SCorentin Chary                 ((uint8_t *)fbptr + ds_get_linesize(vs->ds));           \
676245f7b51SCorentin Chary         }                                                               \
677245f7b51SCorentin Chary                                                                         \
678245f7b51SCorentin Chary         *color = (uint32_t)c;                                           \
679245f7b51SCorentin Chary         return true;                                                    \
680245f7b51SCorentin Chary     }
681245f7b51SCorentin Chary 
682245f7b51SCorentin Chary DEFINE_CHECK_SOLID_FUNCTION(32)
683245f7b51SCorentin Chary DEFINE_CHECK_SOLID_FUNCTION(16)
684245f7b51SCorentin Chary DEFINE_CHECK_SOLID_FUNCTION(8)
685245f7b51SCorentin Chary 
686245f7b51SCorentin Chary static bool check_solid_tile(VncState *vs, int x, int y, int w, int h,
687245f7b51SCorentin Chary                              uint32_t* color, bool samecolor)
688245f7b51SCorentin Chary {
689245f7b51SCorentin Chary     VncDisplay *vd = vs->vd;
690245f7b51SCorentin Chary 
691245f7b51SCorentin Chary     switch(vd->server->pf.bytes_per_pixel) {
692245f7b51SCorentin Chary     case 4:
693245f7b51SCorentin Chary         return check_solid_tile32(vs, x, y, w, h, color, samecolor);
694245f7b51SCorentin Chary     case 2:
695245f7b51SCorentin Chary         return check_solid_tile16(vs, x, y, w, h, color, samecolor);
696245f7b51SCorentin Chary     default:
697245f7b51SCorentin Chary         return check_solid_tile8(vs, x, y, w, h, color, samecolor);
698245f7b51SCorentin Chary     }
699245f7b51SCorentin Chary }
700245f7b51SCorentin Chary 
701245f7b51SCorentin Chary static void find_best_solid_area(VncState *vs, int x, int y, int w, int h,
702245f7b51SCorentin Chary                                  uint32_t color, int *w_ptr, int *h_ptr)
703245f7b51SCorentin Chary {
704245f7b51SCorentin Chary     int dx, dy, dw, dh;
705245f7b51SCorentin Chary     int w_prev;
706245f7b51SCorentin Chary     int w_best = 0, h_best = 0;
707245f7b51SCorentin Chary 
708245f7b51SCorentin Chary     w_prev = w;
709245f7b51SCorentin Chary 
710245f7b51SCorentin Chary     for (dy = y; dy < y + h; dy += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) {
711245f7b51SCorentin Chary 
712245f7b51SCorentin Chary         dh = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, y + h - dy);
713245f7b51SCorentin Chary         dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, w_prev);
714245f7b51SCorentin Chary 
715245f7b51SCorentin Chary         if (!check_solid_tile(vs, x, dy, dw, dh, &color, true)) {
716245f7b51SCorentin Chary             break;
717245f7b51SCorentin Chary         }
718245f7b51SCorentin Chary 
719245f7b51SCorentin Chary         for (dx = x + dw; dx < x + w_prev;) {
720245f7b51SCorentin Chary             dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, x + w_prev - dx);
721245f7b51SCorentin Chary 
722245f7b51SCorentin Chary             if (!check_solid_tile(vs, dx, dy, dw, dh, &color, true)) {
723245f7b51SCorentin Chary                 break;
724245f7b51SCorentin Chary             }
725245f7b51SCorentin Chary             dx += dw;
726245f7b51SCorentin Chary         }
727245f7b51SCorentin Chary 
728245f7b51SCorentin Chary         w_prev = dx - x;
729245f7b51SCorentin Chary         if (w_prev * (dy + dh - y) > w_best * h_best) {
730245f7b51SCorentin Chary             w_best = w_prev;
731245f7b51SCorentin Chary             h_best = dy + dh - y;
732245f7b51SCorentin Chary         }
733245f7b51SCorentin Chary     }
734245f7b51SCorentin Chary 
735245f7b51SCorentin Chary     *w_ptr = w_best;
736245f7b51SCorentin Chary     *h_ptr = h_best;
737245f7b51SCorentin Chary }
738245f7b51SCorentin Chary 
739245f7b51SCorentin Chary static void extend_solid_area(VncState *vs, int x, int y, int w, int h,
740245f7b51SCorentin Chary                               uint32_t color, int *x_ptr, int *y_ptr,
741245f7b51SCorentin Chary                               int *w_ptr, int *h_ptr)
742245f7b51SCorentin Chary {
743245f7b51SCorentin Chary     int cx, cy;
744245f7b51SCorentin Chary 
745245f7b51SCorentin Chary     /* Try to extend the area upwards. */
746245f7b51SCorentin Chary     for ( cy = *y_ptr - 1;
747245f7b51SCorentin Chary           cy >= y && check_solid_tile(vs, *x_ptr, cy, *w_ptr, 1, &color, true);
748245f7b51SCorentin Chary           cy-- );
749245f7b51SCorentin Chary     *h_ptr += *y_ptr - (cy + 1);
750245f7b51SCorentin Chary     *y_ptr = cy + 1;
751245f7b51SCorentin Chary 
752245f7b51SCorentin Chary     /* ... downwards. */
753245f7b51SCorentin Chary     for ( cy = *y_ptr + *h_ptr;
754245f7b51SCorentin Chary           cy < y + h &&
755245f7b51SCorentin Chary               check_solid_tile(vs, *x_ptr, cy, *w_ptr, 1, &color, true);
756245f7b51SCorentin Chary           cy++ );
757245f7b51SCorentin Chary     *h_ptr += cy - (*y_ptr + *h_ptr);
758245f7b51SCorentin Chary 
759245f7b51SCorentin Chary     /* ... to the left. */
760245f7b51SCorentin Chary     for ( cx = *x_ptr - 1;
761245f7b51SCorentin Chary           cx >= x && check_solid_tile(vs, cx, *y_ptr, 1, *h_ptr, &color, true);
762245f7b51SCorentin Chary           cx-- );
763245f7b51SCorentin Chary     *w_ptr += *x_ptr - (cx + 1);
764245f7b51SCorentin Chary     *x_ptr = cx + 1;
765245f7b51SCorentin Chary 
766245f7b51SCorentin Chary     /* ... to the right. */
767245f7b51SCorentin Chary     for ( cx = *x_ptr + *w_ptr;
768245f7b51SCorentin Chary           cx < x + w &&
769245f7b51SCorentin Chary               check_solid_tile(vs, cx, *y_ptr, 1, *h_ptr, &color, true);
770245f7b51SCorentin Chary           cx++ );
771245f7b51SCorentin Chary     *w_ptr += cx - (*x_ptr + *w_ptr);
772245f7b51SCorentin Chary }
773245f7b51SCorentin Chary 
774245f7b51SCorentin Chary static int tight_init_stream(VncState *vs, int stream_id,
775245f7b51SCorentin Chary                              int level, int strategy)
776245f7b51SCorentin Chary {
777d1af0e05SCorentin Chary     z_streamp zstream = &vs->tight.stream[stream_id];
778245f7b51SCorentin Chary 
779245f7b51SCorentin Chary     if (zstream->opaque == NULL) {
780245f7b51SCorentin Chary         int err;
781245f7b51SCorentin Chary 
782245f7b51SCorentin Chary         VNC_DEBUG("VNC: TIGHT: initializing zlib stream %d\n", stream_id);
783245f7b51SCorentin Chary         VNC_DEBUG("VNC: TIGHT: opaque = %p | vs = %p\n", zstream->opaque, vs);
784245f7b51SCorentin Chary         zstream->zalloc = vnc_zlib_zalloc;
785245f7b51SCorentin Chary         zstream->zfree = vnc_zlib_zfree;
786245f7b51SCorentin Chary 
787245f7b51SCorentin Chary         err = deflateInit2(zstream, level, Z_DEFLATED, MAX_WBITS,
788245f7b51SCorentin Chary                            MAX_MEM_LEVEL, strategy);
789245f7b51SCorentin Chary 
790245f7b51SCorentin Chary         if (err != Z_OK) {
791245f7b51SCorentin Chary             fprintf(stderr, "VNC: error initializing zlib\n");
792245f7b51SCorentin Chary             return -1;
793245f7b51SCorentin Chary         }
794245f7b51SCorentin Chary 
795d1af0e05SCorentin Chary         vs->tight.levels[stream_id] = level;
796245f7b51SCorentin Chary         zstream->opaque = vs;
797245f7b51SCorentin Chary     }
798245f7b51SCorentin Chary 
799d1af0e05SCorentin Chary     if (vs->tight.levels[stream_id] != level) {
800245f7b51SCorentin Chary         if (deflateParams(zstream, level, strategy) != Z_OK) {
801245f7b51SCorentin Chary             return -1;
802245f7b51SCorentin Chary         }
803d1af0e05SCorentin Chary         vs->tight.levels[stream_id] = level;
804245f7b51SCorentin Chary     }
805245f7b51SCorentin Chary     return 0;
806245f7b51SCorentin Chary }
807245f7b51SCorentin Chary 
808245f7b51SCorentin Chary static void tight_send_compact_size(VncState *vs, size_t len)
809245f7b51SCorentin Chary {
810245f7b51SCorentin Chary     int lpc = 0;
811245f7b51SCorentin Chary     int bytes = 0;
812245f7b51SCorentin Chary     char buf[3] = {0, 0, 0};
813245f7b51SCorentin Chary 
814245f7b51SCorentin Chary     buf[bytes++] = len & 0x7F;
815245f7b51SCorentin Chary     if (len > 0x7F) {
816245f7b51SCorentin Chary         buf[bytes-1] |= 0x80;
817245f7b51SCorentin Chary         buf[bytes++] = (len >> 7) & 0x7F;
818245f7b51SCorentin Chary         if (len > 0x3FFF) {
819245f7b51SCorentin Chary             buf[bytes-1] |= 0x80;
820245f7b51SCorentin Chary             buf[bytes++] = (len >> 14) & 0xFF;
821245f7b51SCorentin Chary         }
822245f7b51SCorentin Chary     }
823245f7b51SCorentin Chary     for (lpc = 0; lpc < bytes; lpc++) {
824245f7b51SCorentin Chary         vnc_write_u8(vs, buf[lpc]);
825245f7b51SCorentin Chary     }
826245f7b51SCorentin Chary }
827245f7b51SCorentin Chary 
828245f7b51SCorentin Chary static int tight_compress_data(VncState *vs, int stream_id, size_t bytes,
829245f7b51SCorentin Chary                                int level, int strategy)
830245f7b51SCorentin Chary {
831d1af0e05SCorentin Chary     z_streamp zstream = &vs->tight.stream[stream_id];
832245f7b51SCorentin Chary     int previous_out;
833245f7b51SCorentin Chary 
834245f7b51SCorentin Chary     if (bytes < VNC_TIGHT_MIN_TO_COMPRESS) {
835d1af0e05SCorentin Chary         vnc_write(vs, vs->tight.tight.buffer, vs->tight.tight.offset);
836245f7b51SCorentin Chary         return bytes;
837245f7b51SCorentin Chary     }
838245f7b51SCorentin Chary 
839245f7b51SCorentin Chary     if (tight_init_stream(vs, stream_id, level, strategy)) {
840245f7b51SCorentin Chary         return -1;
841245f7b51SCorentin Chary     }
842245f7b51SCorentin Chary 
843245f7b51SCorentin Chary     /* reserve memory in output buffer */
844d1af0e05SCorentin Chary     buffer_reserve(&vs->tight.zlib, bytes + 64);
845245f7b51SCorentin Chary 
846245f7b51SCorentin Chary     /* set pointers */
847d1af0e05SCorentin Chary     zstream->next_in = vs->tight.tight.buffer;
848d1af0e05SCorentin Chary     zstream->avail_in = vs->tight.tight.offset;
849d1af0e05SCorentin Chary     zstream->next_out = vs->tight.zlib.buffer + vs->tight.zlib.offset;
850d1af0e05SCorentin Chary     zstream->avail_out = vs->tight.zlib.capacity - vs->tight.zlib.offset;
851245f7b51SCorentin Chary     zstream->data_type = Z_BINARY;
852245f7b51SCorentin Chary     previous_out = zstream->total_out;
853245f7b51SCorentin Chary 
854245f7b51SCorentin Chary     /* start encoding */
855245f7b51SCorentin Chary     if (deflate(zstream, Z_SYNC_FLUSH) != Z_OK) {
856245f7b51SCorentin Chary         fprintf(stderr, "VNC: error during tight compression\n");
857245f7b51SCorentin Chary         return -1;
858245f7b51SCorentin Chary     }
859245f7b51SCorentin Chary 
860d1af0e05SCorentin Chary     vs->tight.zlib.offset = vs->tight.zlib.capacity - zstream->avail_out;
861245f7b51SCorentin Chary     bytes = zstream->total_out - previous_out;
862245f7b51SCorentin Chary 
863245f7b51SCorentin Chary     tight_send_compact_size(vs, bytes);
864d1af0e05SCorentin Chary     vnc_write(vs, vs->tight.zlib.buffer, bytes);
865245f7b51SCorentin Chary 
866d1af0e05SCorentin Chary     buffer_reset(&vs->tight.zlib);
867245f7b51SCorentin Chary 
868245f7b51SCorentin Chary     return bytes;
869245f7b51SCorentin Chary }
870245f7b51SCorentin Chary 
871245f7b51SCorentin Chary /*
872245f7b51SCorentin Chary  * Subencoding implementations.
873245f7b51SCorentin Chary  */
874245f7b51SCorentin Chary static void tight_pack24(VncState *vs, uint8_t *buf, size_t count, size_t *ret)
875245f7b51SCorentin Chary {
876245f7b51SCorentin Chary     uint32_t *buf32;
877245f7b51SCorentin Chary     uint32_t pix;
878245f7b51SCorentin Chary     int rshift, gshift, bshift;
879245f7b51SCorentin Chary 
880245f7b51SCorentin Chary     buf32 = (uint32_t *)buf;
881245f7b51SCorentin Chary 
882245f7b51SCorentin Chary     if ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) ==
883245f7b51SCorentin Chary         (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG)) {
884245f7b51SCorentin Chary         rshift = vs->clientds.pf.rshift;
885245f7b51SCorentin Chary         gshift = vs->clientds.pf.gshift;
886245f7b51SCorentin Chary         bshift = vs->clientds.pf.bshift;
887245f7b51SCorentin Chary     } else {
888245f7b51SCorentin Chary         rshift = 24 - vs->clientds.pf.rshift;
889245f7b51SCorentin Chary         gshift = 24 - vs->clientds.pf.gshift;
890245f7b51SCorentin Chary         bshift = 24 - vs->clientds.pf.bshift;
891245f7b51SCorentin Chary     }
892245f7b51SCorentin Chary 
893245f7b51SCorentin Chary     if (ret) {
894245f7b51SCorentin Chary         *ret = count * 3;
895245f7b51SCorentin Chary     }
896245f7b51SCorentin Chary 
897245f7b51SCorentin Chary     while (count--) {
898245f7b51SCorentin Chary         pix = *buf32++;
899245f7b51SCorentin Chary         *buf++ = (char)(pix >> rshift);
900245f7b51SCorentin Chary         *buf++ = (char)(pix >> gshift);
901245f7b51SCorentin Chary         *buf++ = (char)(pix >> bshift);
902245f7b51SCorentin Chary     }
903245f7b51SCorentin Chary }
904245f7b51SCorentin Chary 
905efe556adSCorentin Chary static int send_full_color_rect(VncState *vs, int x, int y, int w, int h)
906245f7b51SCorentin Chary {
907245f7b51SCorentin Chary     int stream = 0;
908245f7b51SCorentin Chary     size_t bytes;
909245f7b51SCorentin Chary 
910efe556adSCorentin Chary #ifdef CONFIG_VNC_PNG
911efe556adSCorentin Chary     if (tight_can_send_png_rect(vs, w, h)) {
912efe556adSCorentin Chary         return send_png_rect(vs, x, y, w, h, NULL);
913efe556adSCorentin Chary     }
914efe556adSCorentin Chary #endif
915efe556adSCorentin Chary 
916245f7b51SCorentin Chary     vnc_write_u8(vs, stream << 4); /* no flushing, no filter */
917245f7b51SCorentin Chary 
918d1af0e05SCorentin Chary     if (vs->tight.pixel24) {
919d1af0e05SCorentin Chary         tight_pack24(vs, vs->tight.tight.buffer, w * h, &vs->tight.tight.offset);
920245f7b51SCorentin Chary         bytes = 3;
921245f7b51SCorentin Chary     } else {
922245f7b51SCorentin Chary         bytes = vs->clientds.pf.bytes_per_pixel;
923245f7b51SCorentin Chary     }
924245f7b51SCorentin Chary 
925245f7b51SCorentin Chary     bytes = tight_compress_data(vs, stream, w * h * bytes,
926d1af0e05SCorentin Chary                                 tight_conf[vs->tight.compression].raw_zlib_level,
927245f7b51SCorentin Chary                                 Z_DEFAULT_STRATEGY);
928245f7b51SCorentin Chary 
929245f7b51SCorentin Chary     return (bytes >= 0);
930245f7b51SCorentin Chary }
931245f7b51SCorentin Chary 
932245f7b51SCorentin Chary static int send_solid_rect(VncState *vs)
933245f7b51SCorentin Chary {
934245f7b51SCorentin Chary     size_t bytes;
935245f7b51SCorentin Chary 
936245f7b51SCorentin Chary     vnc_write_u8(vs, VNC_TIGHT_FILL << 4); /* no flushing, no filter */
937245f7b51SCorentin Chary 
938d1af0e05SCorentin Chary     if (vs->tight.pixel24) {
939d1af0e05SCorentin Chary         tight_pack24(vs, vs->tight.tight.buffer, 1, &vs->tight.tight.offset);
940245f7b51SCorentin Chary         bytes = 3;
941245f7b51SCorentin Chary     } else {
942245f7b51SCorentin Chary         bytes = vs->clientds.pf.bytes_per_pixel;
943245f7b51SCorentin Chary     }
944245f7b51SCorentin Chary 
945d1af0e05SCorentin Chary     vnc_write(vs, vs->tight.tight.buffer, bytes);
946245f7b51SCorentin Chary     return 1;
947245f7b51SCorentin Chary }
948245f7b51SCorentin Chary 
949efe556adSCorentin Chary static int send_mono_rect(VncState *vs, int x, int y,
950efe556adSCorentin Chary                           int w, int h, uint32_t bg, uint32_t fg)
951245f7b51SCorentin Chary {
952245f7b51SCorentin Chary     size_t bytes;
953245f7b51SCorentin Chary     int stream = 1;
954d1af0e05SCorentin Chary     int level = tight_conf[vs->tight.compression].mono_zlib_level;
955245f7b51SCorentin Chary 
956efe556adSCorentin Chary #ifdef CONFIG_VNC_PNG
957efe556adSCorentin Chary     if (tight_can_send_png_rect(vs, w, h)) {
958efe556adSCorentin Chary         int ret;
959efe556adSCorentin Chary         int bpp = vs->clientds.pf.bytes_per_pixel * 8;
9605136a052SCorentin Chary         VncPalette *palette = palette_new(2, bpp);
961efe556adSCorentin Chary 
9625136a052SCorentin Chary         palette_put(palette, bg);
9635136a052SCorentin Chary         palette_put(palette, fg);
964efe556adSCorentin Chary         ret = send_png_rect(vs, x, y, w, h, palette);
9655136a052SCorentin Chary         palette_destroy(palette);
966efe556adSCorentin Chary         return ret;
967efe556adSCorentin Chary     }
968efe556adSCorentin Chary #endif
969efe556adSCorentin Chary 
970245f7b51SCorentin Chary     bytes = ((w + 7) / 8) * h;
971245f7b51SCorentin Chary 
972245f7b51SCorentin Chary     vnc_write_u8(vs, (stream | VNC_TIGHT_EXPLICIT_FILTER) << 4);
973245f7b51SCorentin Chary     vnc_write_u8(vs, VNC_TIGHT_FILTER_PALETTE);
974245f7b51SCorentin Chary     vnc_write_u8(vs, 1);
975245f7b51SCorentin Chary 
976245f7b51SCorentin Chary     switch(vs->clientds.pf.bytes_per_pixel) {
977245f7b51SCorentin Chary     case 4:
978245f7b51SCorentin Chary     {
979245f7b51SCorentin Chary         uint32_t buf[2] = {bg, fg};
980245f7b51SCorentin Chary         size_t ret = sizeof (buf);
981245f7b51SCorentin Chary 
982d1af0e05SCorentin Chary         if (vs->tight.pixel24) {
983245f7b51SCorentin Chary             tight_pack24(vs, (unsigned char*)buf, 2, &ret);
984245f7b51SCorentin Chary         }
985245f7b51SCorentin Chary         vnc_write(vs, buf, ret);
986245f7b51SCorentin Chary 
987d1af0e05SCorentin Chary         tight_encode_mono_rect32(vs->tight.tight.buffer, w, h, bg, fg);
988245f7b51SCorentin Chary         break;
989245f7b51SCorentin Chary     }
990245f7b51SCorentin Chary     case 2:
991245f7b51SCorentin Chary         vnc_write(vs, &bg, 2);
992245f7b51SCorentin Chary         vnc_write(vs, &fg, 2);
993d1af0e05SCorentin Chary         tight_encode_mono_rect16(vs->tight.tight.buffer, w, h, bg, fg);
994245f7b51SCorentin Chary         break;
995245f7b51SCorentin Chary     default:
996245f7b51SCorentin Chary         vnc_write_u8(vs, bg);
997245f7b51SCorentin Chary         vnc_write_u8(vs, fg);
998d1af0e05SCorentin Chary         tight_encode_mono_rect8(vs->tight.tight.buffer, w, h, bg, fg);
999245f7b51SCorentin Chary         break;
1000245f7b51SCorentin Chary     }
1001d1af0e05SCorentin Chary     vs->tight.tight.offset = bytes;
1002245f7b51SCorentin Chary 
1003245f7b51SCorentin Chary     bytes = tight_compress_data(vs, stream, bytes, level, Z_DEFAULT_STRATEGY);
1004245f7b51SCorentin Chary     return (bytes >= 0);
1005245f7b51SCorentin Chary }
1006245f7b51SCorentin Chary 
1007245f7b51SCorentin Chary struct palette_cb_priv {
1008245f7b51SCorentin Chary     VncState *vs;
1009245f7b51SCorentin Chary     uint8_t *header;
1010efe556adSCorentin Chary #ifdef CONFIG_VNC_PNG
1011efe556adSCorentin Chary     png_colorp png_palette;
1012efe556adSCorentin Chary #endif
1013245f7b51SCorentin Chary };
1014245f7b51SCorentin Chary 
10155136a052SCorentin Chary static void write_palette(int idx, uint32_t color, void *opaque)
1016245f7b51SCorentin Chary {
1017245f7b51SCorentin Chary     struct palette_cb_priv *priv = opaque;
1018245f7b51SCorentin Chary     VncState *vs = priv->vs;
1019245f7b51SCorentin Chary     uint32_t bytes = vs->clientds.pf.bytes_per_pixel;
1020245f7b51SCorentin Chary 
1021245f7b51SCorentin Chary     if (bytes == 4) {
1022245f7b51SCorentin Chary         ((uint32_t*)priv->header)[idx] = color;
1023245f7b51SCorentin Chary     } else {
1024245f7b51SCorentin Chary         ((uint16_t*)priv->header)[idx] = color;
1025245f7b51SCorentin Chary     }
1026245f7b51SCorentin Chary }
1027245f7b51SCorentin Chary 
1028efe556adSCorentin Chary static bool send_gradient_rect(VncState *vs, int x, int y, int w, int h)
1029245f7b51SCorentin Chary {
1030245f7b51SCorentin Chary     int stream = 3;
1031d1af0e05SCorentin Chary     int level = tight_conf[vs->tight.compression].gradient_zlib_level;
1032245f7b51SCorentin Chary     size_t bytes;
1033245f7b51SCorentin Chary 
1034245f7b51SCorentin Chary     if (vs->clientds.pf.bytes_per_pixel == 1)
1035efe556adSCorentin Chary         return send_full_color_rect(vs, x, y, w, h);
1036245f7b51SCorentin Chary 
1037245f7b51SCorentin Chary     vnc_write_u8(vs, (stream | VNC_TIGHT_EXPLICIT_FILTER) << 4);
1038245f7b51SCorentin Chary     vnc_write_u8(vs, VNC_TIGHT_FILTER_GRADIENT);
1039245f7b51SCorentin Chary 
1040d1af0e05SCorentin Chary     buffer_reserve(&vs->tight.gradient, w * 3 * sizeof (int));
1041245f7b51SCorentin Chary 
1042d1af0e05SCorentin Chary     if (vs->tight.pixel24) {
1043d1af0e05SCorentin Chary         tight_filter_gradient24(vs, vs->tight.tight.buffer, w, h);
1044245f7b51SCorentin Chary         bytes = 3;
1045245f7b51SCorentin Chary     } else if (vs->clientds.pf.bytes_per_pixel == 4) {
1046d1af0e05SCorentin Chary         tight_filter_gradient32(vs, (uint32_t *)vs->tight.tight.buffer, w, h);
1047245f7b51SCorentin Chary         bytes = 4;
1048245f7b51SCorentin Chary     } else {
1049d1af0e05SCorentin Chary         tight_filter_gradient16(vs, (uint16_t *)vs->tight.tight.buffer, w, h);
1050245f7b51SCorentin Chary         bytes = 2;
1051245f7b51SCorentin Chary     }
1052245f7b51SCorentin Chary 
1053d1af0e05SCorentin Chary     buffer_reset(&vs->tight.gradient);
1054245f7b51SCorentin Chary 
1055245f7b51SCorentin Chary     bytes = w * h * bytes;
1056d1af0e05SCorentin Chary     vs->tight.tight.offset = bytes;
1057245f7b51SCorentin Chary 
1058245f7b51SCorentin Chary     bytes = tight_compress_data(vs, stream, bytes,
1059245f7b51SCorentin Chary                                 level, Z_FILTERED);
1060245f7b51SCorentin Chary     return (bytes >= 0);
1061245f7b51SCorentin Chary }
1062245f7b51SCorentin Chary 
1063efe556adSCorentin Chary static int send_palette_rect(VncState *vs, int x, int y,
10645136a052SCorentin Chary                              int w, int h, VncPalette *palette)
1065245f7b51SCorentin Chary {
1066245f7b51SCorentin Chary     int stream = 2;
1067d1af0e05SCorentin Chary     int level = tight_conf[vs->tight.compression].idx_zlib_level;
1068245f7b51SCorentin Chary     int colors;
1069245f7b51SCorentin Chary     size_t bytes;
1070245f7b51SCorentin Chary 
1071efe556adSCorentin Chary #ifdef CONFIG_VNC_PNG
1072efe556adSCorentin Chary     if (tight_can_send_png_rect(vs, w, h)) {
1073efe556adSCorentin Chary         return send_png_rect(vs, x, y, w, h, palette);
1074efe556adSCorentin Chary     }
1075efe556adSCorentin Chary #endif
1076efe556adSCorentin Chary 
10775136a052SCorentin Chary     colors = palette_size(palette);
1078245f7b51SCorentin Chary 
1079245f7b51SCorentin Chary     vnc_write_u8(vs, (stream | VNC_TIGHT_EXPLICIT_FILTER) << 4);
1080245f7b51SCorentin Chary     vnc_write_u8(vs, VNC_TIGHT_FILTER_PALETTE);
1081245f7b51SCorentin Chary     vnc_write_u8(vs, colors - 1);
1082245f7b51SCorentin Chary 
1083245f7b51SCorentin Chary     switch(vs->clientds.pf.bytes_per_pixel) {
1084245f7b51SCorentin Chary     case 4:
1085245f7b51SCorentin Chary     {
1086245f7b51SCorentin Chary         size_t old_offset, offset;
10875136a052SCorentin Chary         uint32_t header[palette_size(palette)];
1088245f7b51SCorentin Chary         struct palette_cb_priv priv = { vs, (uint8_t *)header };
1089245f7b51SCorentin Chary 
1090245f7b51SCorentin Chary         old_offset = vs->output.offset;
10915136a052SCorentin Chary         palette_iter(palette, write_palette, &priv);
1092245f7b51SCorentin Chary         vnc_write(vs, header, sizeof(header));
1093245f7b51SCorentin Chary 
1094d1af0e05SCorentin Chary         if (vs->tight.pixel24) {
1095245f7b51SCorentin Chary             tight_pack24(vs, vs->output.buffer + old_offset, colors, &offset);
1096245f7b51SCorentin Chary             vs->output.offset = old_offset + offset;
1097245f7b51SCorentin Chary         }
1098245f7b51SCorentin Chary 
1099d1af0e05SCorentin Chary         tight_encode_indexed_rect32(vs->tight.tight.buffer, w * h, palette);
1100245f7b51SCorentin Chary         break;
1101245f7b51SCorentin Chary     }
1102245f7b51SCorentin Chary     case 2:
1103245f7b51SCorentin Chary     {
11045136a052SCorentin Chary         uint16_t header[palette_size(palette)];
1105245f7b51SCorentin Chary         struct palette_cb_priv priv = { vs, (uint8_t *)header };
1106245f7b51SCorentin Chary 
11075136a052SCorentin Chary         palette_iter(palette, write_palette, &priv);
1108245f7b51SCorentin Chary         vnc_write(vs, header, sizeof(header));
1109d1af0e05SCorentin Chary         tight_encode_indexed_rect16(vs->tight.tight.buffer, w * h, palette);
1110245f7b51SCorentin Chary         break;
1111245f7b51SCorentin Chary     }
1112245f7b51SCorentin Chary     default:
1113245f7b51SCorentin Chary         return -1; /* No palette for 8bits colors */
1114245f7b51SCorentin Chary         break;
1115245f7b51SCorentin Chary     }
1116245f7b51SCorentin Chary     bytes = w * h;
1117d1af0e05SCorentin Chary     vs->tight.tight.offset = bytes;
1118245f7b51SCorentin Chary 
1119245f7b51SCorentin Chary     bytes = tight_compress_data(vs, stream, bytes,
1120245f7b51SCorentin Chary                                 level, Z_DEFAULT_STRATEGY);
1121245f7b51SCorentin Chary     return (bytes >= 0);
1122245f7b51SCorentin Chary }
1123245f7b51SCorentin Chary 
1124efe556adSCorentin Chary #if defined(CONFIG_VNC_JPEG) || defined(CONFIG_VNC_PNG)
1125efe556adSCorentin Chary static void rgb_prepare_row24(VncState *vs, uint8_t *dst, int x, int y,
1126245f7b51SCorentin Chary                               int count)
1127245f7b51SCorentin Chary {
1128245f7b51SCorentin Chary     VncDisplay *vd = vs->vd;
1129245f7b51SCorentin Chary     uint32_t *fbptr;
1130245f7b51SCorentin Chary     uint32_t pix;
1131245f7b51SCorentin Chary 
1132245f7b51SCorentin Chary     fbptr = (uint32_t *)(vd->server->data + y * ds_get_linesize(vs->ds) +
1133245f7b51SCorentin Chary                          x * ds_get_bytes_per_pixel(vs->ds));
1134245f7b51SCorentin Chary 
1135245f7b51SCorentin Chary     while (count--) {
1136245f7b51SCorentin Chary         pix = *fbptr++;
1137245f7b51SCorentin Chary         *dst++ = (uint8_t)(pix >> vs->ds->surface->pf.rshift);
1138245f7b51SCorentin Chary         *dst++ = (uint8_t)(pix >> vs->ds->surface->pf.gshift);
1139245f7b51SCorentin Chary         *dst++ = (uint8_t)(pix >> vs->ds->surface->pf.bshift);
1140245f7b51SCorentin Chary     }
1141245f7b51SCorentin Chary }
1142245f7b51SCorentin Chary 
1143efe556adSCorentin Chary #define DEFINE_RGB_GET_ROW_FUNCTION(bpp)                                \
1144245f7b51SCorentin Chary                                                                         \
1145245f7b51SCorentin Chary     static void                                                         \
1146efe556adSCorentin Chary     rgb_prepare_row##bpp(VncState *vs, uint8_t *dst,                    \
1147245f7b51SCorentin Chary                          int x, int y, int count)                       \
1148245f7b51SCorentin Chary     {                                                                   \
1149245f7b51SCorentin Chary         VncDisplay *vd = vs->vd;                                        \
1150245f7b51SCorentin Chary         uint##bpp##_t *fbptr;                                           \
1151245f7b51SCorentin Chary         uint##bpp##_t pix;                                              \
1152245f7b51SCorentin Chary         int r, g, b;                                                    \
1153245f7b51SCorentin Chary                                                                         \
1154245f7b51SCorentin Chary         fbptr = (uint##bpp##_t *)                                       \
1155245f7b51SCorentin Chary             (vd->server->data + y * ds_get_linesize(vs->ds) +           \
1156245f7b51SCorentin Chary              x * ds_get_bytes_per_pixel(vs->ds));                       \
1157245f7b51SCorentin Chary                                                                         \
1158245f7b51SCorentin Chary         while (count--) {                                               \
1159245f7b51SCorentin Chary             pix = *fbptr++;                                             \
1160245f7b51SCorentin Chary                                                                         \
1161245f7b51SCorentin Chary             r = (int)((pix >> vs->ds->surface->pf.rshift)               \
1162245f7b51SCorentin Chary                       & vs->ds->surface->pf.rmax);                      \
1163245f7b51SCorentin Chary             g = (int)((pix >> vs->ds->surface->pf.gshift)               \
1164245f7b51SCorentin Chary                       & vs->ds->surface->pf.gmax);                      \
1165245f7b51SCorentin Chary             b = (int)((pix >> vs->ds->surface->pf.bshift)               \
1166245f7b51SCorentin Chary                       & vs->ds->surface->pf.bmax);                      \
1167245f7b51SCorentin Chary                                                                         \
1168245f7b51SCorentin Chary             *dst++ = (uint8_t)((r * 255 + vs->ds->surface->pf.rmax / 2) \
1169245f7b51SCorentin Chary                                / vs->ds->surface->pf.rmax);             \
1170245f7b51SCorentin Chary             *dst++ = (uint8_t)((g * 255 + vs->ds->surface->pf.gmax / 2) \
1171245f7b51SCorentin Chary                                / vs->ds->surface->pf.gmax);             \
1172245f7b51SCorentin Chary             *dst++ = (uint8_t)((b * 255 + vs->ds->surface->pf.bmax / 2) \
1173245f7b51SCorentin Chary                                / vs->ds->surface->pf.bmax);             \
1174245f7b51SCorentin Chary         }                                                               \
1175245f7b51SCorentin Chary     }
1176245f7b51SCorentin Chary 
1177efe556adSCorentin Chary DEFINE_RGB_GET_ROW_FUNCTION(16)
1178efe556adSCorentin Chary DEFINE_RGB_GET_ROW_FUNCTION(32)
1179245f7b51SCorentin Chary 
1180efe556adSCorentin Chary static void rgb_prepare_row(VncState *vs, uint8_t *dst, int x, int y,
1181245f7b51SCorentin Chary                             int count)
1182245f7b51SCorentin Chary {
11834043a013SCorentin Chary     if (ds_get_bytes_per_pixel(vs->ds) == 4) {
11844043a013SCorentin Chary         if (vs->ds->surface->pf.rmax == 0xFF &&
11854043a013SCorentin Chary             vs->ds->surface->pf.gmax == 0xFF &&
11864043a013SCorentin Chary             vs->ds->surface->pf.bmax == 0xFF) {
1187efe556adSCorentin Chary             rgb_prepare_row24(vs, dst, x, y, count);
11884043a013SCorentin Chary         } else {
1189efe556adSCorentin Chary             rgb_prepare_row32(vs, dst, x, y, count);
11904043a013SCorentin Chary         }
11914043a013SCorentin Chary     } else {
1192efe556adSCorentin Chary         rgb_prepare_row16(vs, dst, x, y, count);
1193245f7b51SCorentin Chary     }
11944043a013SCorentin Chary }
1195efe556adSCorentin Chary #endif /* CONFIG_VNC_JPEG or CONFIG_VNC_PNG */
1196245f7b51SCorentin Chary 
1197245f7b51SCorentin Chary /*
1198efe556adSCorentin Chary  * JPEG compression stuff.
1199efe556adSCorentin Chary  */
1200efe556adSCorentin Chary #ifdef CONFIG_VNC_JPEG
1201efe556adSCorentin Chary /*
1202245f7b51SCorentin Chary  * Destination manager implementation for JPEG library.
1203245f7b51SCorentin Chary  */
1204245f7b51SCorentin Chary 
1205245f7b51SCorentin Chary /* This is called once per encoding */
1206245f7b51SCorentin Chary static void jpeg_init_destination(j_compress_ptr cinfo)
1207245f7b51SCorentin Chary {
1208245f7b51SCorentin Chary     VncState *vs = cinfo->client_data;
1209d1af0e05SCorentin Chary     Buffer *buffer = &vs->tight.jpeg;
1210245f7b51SCorentin Chary 
1211245f7b51SCorentin Chary     cinfo->dest->next_output_byte = (JOCTET *)buffer->buffer + buffer->offset;
1212245f7b51SCorentin Chary     cinfo->dest->free_in_buffer = (size_t)(buffer->capacity - buffer->offset);
1213245f7b51SCorentin Chary }
1214245f7b51SCorentin Chary 
1215245f7b51SCorentin Chary /* This is called when we ran out of buffer (shouldn't happen!) */
1216245f7b51SCorentin Chary static boolean jpeg_empty_output_buffer(j_compress_ptr cinfo)
1217245f7b51SCorentin Chary {
1218245f7b51SCorentin Chary     VncState *vs = cinfo->client_data;
1219d1af0e05SCorentin Chary     Buffer *buffer = &vs->tight.jpeg;
1220245f7b51SCorentin Chary 
1221245f7b51SCorentin Chary     buffer->offset = buffer->capacity;
1222245f7b51SCorentin Chary     buffer_reserve(buffer, 2048);
1223245f7b51SCorentin Chary     jpeg_init_destination(cinfo);
1224245f7b51SCorentin Chary     return TRUE;
1225245f7b51SCorentin Chary }
1226245f7b51SCorentin Chary 
1227245f7b51SCorentin Chary /* This is called when we are done processing data */
1228245f7b51SCorentin Chary static void jpeg_term_destination(j_compress_ptr cinfo)
1229245f7b51SCorentin Chary {
1230245f7b51SCorentin Chary     VncState *vs = cinfo->client_data;
1231d1af0e05SCorentin Chary     Buffer *buffer = &vs->tight.jpeg;
1232245f7b51SCorentin Chary 
1233245f7b51SCorentin Chary     buffer->offset = buffer->capacity - cinfo->dest->free_in_buffer;
1234245f7b51SCorentin Chary }
1235245f7b51SCorentin Chary 
1236245f7b51SCorentin Chary static int send_jpeg_rect(VncState *vs, int x, int y, int w, int h, int quality)
1237245f7b51SCorentin Chary {
1238245f7b51SCorentin Chary     struct jpeg_compress_struct cinfo;
1239245f7b51SCorentin Chary     struct jpeg_error_mgr jerr;
1240245f7b51SCorentin Chary     struct jpeg_destination_mgr manager;
1241245f7b51SCorentin Chary     JSAMPROW row[1];
1242245f7b51SCorentin Chary     uint8_t *buf;
1243245f7b51SCorentin Chary     int dy;
1244245f7b51SCorentin Chary 
1245245f7b51SCorentin Chary     if (ds_get_bytes_per_pixel(vs->ds) == 1)
1246efe556adSCorentin Chary         return send_full_color_rect(vs, x, y, w, h);
1247245f7b51SCorentin Chary 
1248d1af0e05SCorentin Chary     buffer_reserve(&vs->tight.jpeg, 2048);
1249245f7b51SCorentin Chary 
1250245f7b51SCorentin Chary     cinfo.err = jpeg_std_error(&jerr);
1251245f7b51SCorentin Chary     jpeg_create_compress(&cinfo);
1252245f7b51SCorentin Chary 
1253245f7b51SCorentin Chary     cinfo.client_data = vs;
1254245f7b51SCorentin Chary     cinfo.image_width = w;
1255245f7b51SCorentin Chary     cinfo.image_height = h;
1256245f7b51SCorentin Chary     cinfo.input_components = 3;
1257245f7b51SCorentin Chary     cinfo.in_color_space = JCS_RGB;
1258245f7b51SCorentin Chary 
1259245f7b51SCorentin Chary     jpeg_set_defaults(&cinfo);
1260245f7b51SCorentin Chary     jpeg_set_quality(&cinfo, quality, true);
1261245f7b51SCorentin Chary 
1262245f7b51SCorentin Chary     manager.init_destination = jpeg_init_destination;
1263245f7b51SCorentin Chary     manager.empty_output_buffer = jpeg_empty_output_buffer;
1264245f7b51SCorentin Chary     manager.term_destination = jpeg_term_destination;
1265245f7b51SCorentin Chary     cinfo.dest = &manager;
1266245f7b51SCorentin Chary 
1267245f7b51SCorentin Chary     jpeg_start_compress(&cinfo, true);
1268245f7b51SCorentin Chary 
1269d9c18c24SCorentin Chary     buf = qemu_malloc(w * 3);
1270d9c18c24SCorentin Chary     row[0] = buf;
1271245f7b51SCorentin Chary     for (dy = 0; dy < h; dy++) {
1272efe556adSCorentin Chary         rgb_prepare_row(vs, buf, x, y + dy, w);
1273245f7b51SCorentin Chary         jpeg_write_scanlines(&cinfo, row, 1);
1274245f7b51SCorentin Chary     }
1275d9c18c24SCorentin Chary     qemu_free(buf);
1276245f7b51SCorentin Chary 
1277245f7b51SCorentin Chary     jpeg_finish_compress(&cinfo);
1278245f7b51SCorentin Chary     jpeg_destroy_compress(&cinfo);
1279245f7b51SCorentin Chary 
1280245f7b51SCorentin Chary     vnc_write_u8(vs, VNC_TIGHT_JPEG << 4);
1281245f7b51SCorentin Chary 
1282d1af0e05SCorentin Chary     tight_send_compact_size(vs, vs->tight.jpeg.offset);
1283d1af0e05SCorentin Chary     vnc_write(vs, vs->tight.jpeg.buffer, vs->tight.jpeg.offset);
1284d1af0e05SCorentin Chary     buffer_reset(&vs->tight.jpeg);
1285245f7b51SCorentin Chary 
1286245f7b51SCorentin Chary     return 1;
1287245f7b51SCorentin Chary }
1288245f7b51SCorentin Chary #endif /* CONFIG_VNC_JPEG */
1289245f7b51SCorentin Chary 
1290efe556adSCorentin Chary /*
1291efe556adSCorentin Chary  * PNG compression stuff.
1292efe556adSCorentin Chary  */
1293efe556adSCorentin Chary #ifdef CONFIG_VNC_PNG
12945136a052SCorentin Chary static void write_png_palette(int idx, uint32_t pix, void *opaque)
1295efe556adSCorentin Chary {
1296efe556adSCorentin Chary     struct palette_cb_priv *priv = opaque;
1297efe556adSCorentin Chary     VncState *vs = priv->vs;
1298efe556adSCorentin Chary     png_colorp color = &priv->png_palette[idx];
1299efe556adSCorentin Chary 
1300d1af0e05SCorentin Chary     if (vs->tight.pixel24)
1301efe556adSCorentin Chary     {
1302efe556adSCorentin Chary         color->red = (pix >> vs->clientds.pf.rshift) & vs->clientds.pf.rmax;
1303efe556adSCorentin Chary         color->green = (pix >> vs->clientds.pf.gshift) & vs->clientds.pf.gmax;
1304efe556adSCorentin Chary         color->blue = (pix >> vs->clientds.pf.bshift) & vs->clientds.pf.bmax;
1305efe556adSCorentin Chary     }
1306efe556adSCorentin Chary     else
1307efe556adSCorentin Chary     {
1308efe556adSCorentin Chary         int red, green, blue;
1309efe556adSCorentin Chary 
1310efe556adSCorentin Chary         red = (pix >> vs->clientds.pf.rshift) & vs->clientds.pf.rmax;
1311efe556adSCorentin Chary         green = (pix >> vs->clientds.pf.gshift) & vs->clientds.pf.gmax;
1312efe556adSCorentin Chary         blue = (pix >> vs->clientds.pf.bshift) & vs->clientds.pf.bmax;
1313efe556adSCorentin Chary         color->red = ((red * 255 + vs->clientds.pf.rmax / 2) /
1314efe556adSCorentin Chary                       vs->clientds.pf.rmax);
1315efe556adSCorentin Chary         color->green = ((green * 255 + vs->clientds.pf.gmax / 2) /
1316efe556adSCorentin Chary                         vs->clientds.pf.gmax);
1317efe556adSCorentin Chary         color->blue = ((blue * 255 + vs->clientds.pf.bmax / 2) /
1318efe556adSCorentin Chary                        vs->clientds.pf.bmax);
1319efe556adSCorentin Chary     }
1320efe556adSCorentin Chary }
1321efe556adSCorentin Chary 
1322efe556adSCorentin Chary static void png_write_data(png_structp png_ptr, png_bytep data,
1323efe556adSCorentin Chary                            png_size_t length)
1324efe556adSCorentin Chary {
1325efe556adSCorentin Chary     VncState *vs = png_get_io_ptr(png_ptr);
1326efe556adSCorentin Chary 
1327d1af0e05SCorentin Chary     buffer_reserve(&vs->tight.png, vs->tight.png.offset + length);
1328d1af0e05SCorentin Chary     memcpy(vs->tight.png.buffer + vs->tight.png.offset, data, length);
1329efe556adSCorentin Chary 
1330d1af0e05SCorentin Chary     vs->tight.png.offset += length;
1331efe556adSCorentin Chary }
1332efe556adSCorentin Chary 
1333efe556adSCorentin Chary static void png_flush_data(png_structp png_ptr)
1334efe556adSCorentin Chary {
1335efe556adSCorentin Chary }
1336efe556adSCorentin Chary 
1337efe556adSCorentin Chary static void *vnc_png_malloc(png_structp png_ptr, png_size_t size)
1338efe556adSCorentin Chary {
1339efe556adSCorentin Chary     return qemu_malloc(size);
1340efe556adSCorentin Chary }
1341efe556adSCorentin Chary 
1342efe556adSCorentin Chary static void vnc_png_free(png_structp png_ptr, png_voidp ptr)
1343efe556adSCorentin Chary {
1344efe556adSCorentin Chary     qemu_free(ptr);
1345efe556adSCorentin Chary }
1346efe556adSCorentin Chary 
1347efe556adSCorentin Chary static int send_png_rect(VncState *vs, int x, int y, int w, int h,
13485136a052SCorentin Chary                          VncPalette *palette)
1349efe556adSCorentin Chary {
1350efe556adSCorentin Chary     png_byte color_type;
1351efe556adSCorentin Chary     png_structp png_ptr;
1352efe556adSCorentin Chary     png_infop info_ptr;
1353efe556adSCorentin Chary     png_colorp png_palette = NULL;
1354efe556adSCorentin Chary     size_t offset;
1355d1af0e05SCorentin Chary     int level = tight_png_conf[vs->tight.compression].png_zlib_level;
1356d1af0e05SCorentin Chary     int filters = tight_png_conf[vs->tight.compression].png_filters;
1357efe556adSCorentin Chary     uint8_t *buf;
1358efe556adSCorentin Chary     int dy;
1359efe556adSCorentin Chary 
1360efe556adSCorentin Chary     png_ptr = png_create_write_struct_2(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL,
1361efe556adSCorentin Chary                                         NULL, vnc_png_malloc, vnc_png_free);
1362efe556adSCorentin Chary 
1363efe556adSCorentin Chary     if (png_ptr == NULL)
1364efe556adSCorentin Chary         return -1;
1365efe556adSCorentin Chary 
1366efe556adSCorentin Chary     info_ptr = png_create_info_struct(png_ptr);
1367efe556adSCorentin Chary 
1368efe556adSCorentin Chary     if (info_ptr == NULL) {
1369efe556adSCorentin Chary         png_destroy_write_struct(&png_ptr, NULL);
1370efe556adSCorentin Chary         return -1;
1371efe556adSCorentin Chary     }
1372efe556adSCorentin Chary 
1373efe556adSCorentin Chary     png_set_write_fn(png_ptr, (void *) vs, png_write_data, png_flush_data);
1374efe556adSCorentin Chary     png_set_compression_level(png_ptr, level);
13753941bf6fSCorentin Chary     png_set_filter(png_ptr, PNG_FILTER_TYPE_DEFAULT, filters);
1376efe556adSCorentin Chary 
1377efe556adSCorentin Chary     if (palette) {
1378efe556adSCorentin Chary         color_type = PNG_COLOR_TYPE_PALETTE;
1379efe556adSCorentin Chary     } else {
1380efe556adSCorentin Chary         color_type = PNG_COLOR_TYPE_RGB;
1381efe556adSCorentin Chary     }
1382efe556adSCorentin Chary 
1383efe556adSCorentin Chary     png_set_IHDR(png_ptr, info_ptr, w, h,
1384efe556adSCorentin Chary                  8, color_type, PNG_INTERLACE_NONE,
1385efe556adSCorentin Chary                  PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
1386efe556adSCorentin Chary 
1387efe556adSCorentin Chary     if (color_type == PNG_COLOR_TYPE_PALETTE) {
1388efe556adSCorentin Chary         struct palette_cb_priv priv;
1389efe556adSCorentin Chary 
1390efe556adSCorentin Chary         png_palette = png_malloc(png_ptr, sizeof(*png_palette) *
13915136a052SCorentin Chary                                  palette_size(palette));
1392efe556adSCorentin Chary 
1393efe556adSCorentin Chary         priv.vs = vs;
1394efe556adSCorentin Chary         priv.png_palette = png_palette;
13955136a052SCorentin Chary         palette_iter(palette, write_png_palette, &priv);
1396efe556adSCorentin Chary 
13975136a052SCorentin Chary         png_set_PLTE(png_ptr, info_ptr, png_palette, palette_size(palette));
1398efe556adSCorentin Chary 
1399d1af0e05SCorentin Chary         offset = vs->tight.tight.offset;
1400efe556adSCorentin Chary         if (vs->clientds.pf.bytes_per_pixel == 4) {
1401d1af0e05SCorentin Chary             tight_encode_indexed_rect32(vs->tight.tight.buffer, w * h, palette);
1402efe556adSCorentin Chary         } else {
1403d1af0e05SCorentin Chary             tight_encode_indexed_rect16(vs->tight.tight.buffer, w * h, palette);
1404efe556adSCorentin Chary         }
1405efe556adSCorentin Chary     }
1406efe556adSCorentin Chary 
1407efe556adSCorentin Chary     png_write_info(png_ptr, info_ptr);
1408efe556adSCorentin Chary 
1409d1af0e05SCorentin Chary     buffer_reserve(&vs->tight.png, 2048);
1410efe556adSCorentin Chary     buf = qemu_malloc(w * 3);
1411efe556adSCorentin Chary     for (dy = 0; dy < h; dy++)
1412efe556adSCorentin Chary     {
1413efe556adSCorentin Chary         if (color_type == PNG_COLOR_TYPE_PALETTE) {
1414d1af0e05SCorentin Chary             memcpy(buf, vs->tight.tight.buffer + (dy * w), w);
1415efe556adSCorentin Chary         } else {
1416efe556adSCorentin Chary             rgb_prepare_row(vs, buf, x, y + dy, w);
1417efe556adSCorentin Chary         }
1418efe556adSCorentin Chary         png_write_row(png_ptr, buf);
1419efe556adSCorentin Chary     }
1420efe556adSCorentin Chary     qemu_free(buf);
1421efe556adSCorentin Chary 
1422efe556adSCorentin Chary     png_write_end(png_ptr, NULL);
1423efe556adSCorentin Chary 
1424efe556adSCorentin Chary     if (color_type == PNG_COLOR_TYPE_PALETTE) {
1425efe556adSCorentin Chary         png_free(png_ptr, png_palette);
1426efe556adSCorentin Chary     }
1427efe556adSCorentin Chary 
1428efe556adSCorentin Chary     png_destroy_write_struct(&png_ptr, &info_ptr);
1429efe556adSCorentin Chary 
1430efe556adSCorentin Chary     vnc_write_u8(vs, VNC_TIGHT_PNG << 4);
1431efe556adSCorentin Chary 
1432d1af0e05SCorentin Chary     tight_send_compact_size(vs, vs->tight.png.offset);
1433d1af0e05SCorentin Chary     vnc_write(vs, vs->tight.png.buffer, vs->tight.png.offset);
1434d1af0e05SCorentin Chary     buffer_reset(&vs->tight.png);
1435efe556adSCorentin Chary     return 1;
1436efe556adSCorentin Chary }
1437efe556adSCorentin Chary #endif /* CONFIG_VNC_PNG */
1438efe556adSCorentin Chary 
1439245f7b51SCorentin Chary static void vnc_tight_start(VncState *vs)
1440245f7b51SCorentin Chary {
1441d1af0e05SCorentin Chary     buffer_reset(&vs->tight.tight);
1442245f7b51SCorentin Chary 
1443245f7b51SCorentin Chary     // make the output buffer be the zlib buffer, so we can compress it later
1444d1af0e05SCorentin Chary     vs->tight.tmp = vs->output;
1445d1af0e05SCorentin Chary     vs->output = vs->tight.tight;
1446245f7b51SCorentin Chary }
1447245f7b51SCorentin Chary 
1448245f7b51SCorentin Chary static void vnc_tight_stop(VncState *vs)
1449245f7b51SCorentin Chary {
1450245f7b51SCorentin Chary     // switch back to normal output/zlib buffers
1451d1af0e05SCorentin Chary     vs->tight.tight = vs->output;
1452d1af0e05SCorentin Chary     vs->output = vs->tight.tmp;
1453245f7b51SCorentin Chary }
1454245f7b51SCorentin Chary 
145503817eb8SCorentin Chary static int send_sub_rect_nojpeg(VncState *vs, int x, int y, int w, int h,
145603817eb8SCorentin Chary                                 int bg, int fg, int colors, VncPalette *palette)
145703817eb8SCorentin Chary {
145803817eb8SCorentin Chary     int ret;
145903817eb8SCorentin Chary 
146003817eb8SCorentin Chary     if (colors == 0) {
146103817eb8SCorentin Chary         if (tight_detect_smooth_image(vs, w, h)) {
146203817eb8SCorentin Chary             ret = send_gradient_rect(vs, x, y, w, h);
146303817eb8SCorentin Chary         } else {
146403817eb8SCorentin Chary             ret = send_full_color_rect(vs, x, y, w, h);
146503817eb8SCorentin Chary         }
146603817eb8SCorentin Chary     } else if (colors == 1) {
146703817eb8SCorentin Chary         ret = send_solid_rect(vs);
146803817eb8SCorentin Chary     } else if (colors == 2) {
146903817eb8SCorentin Chary         ret = send_mono_rect(vs, x, y, w, h, bg, fg);
147003817eb8SCorentin Chary     } else if (colors <= 256) {
147103817eb8SCorentin Chary         ret = send_palette_rect(vs, x, y, w, h, palette);
1472d167f9bcSBlue Swirl     } else {
1473d167f9bcSBlue Swirl         ret = 0;
147403817eb8SCorentin Chary     }
147503817eb8SCorentin Chary     return ret;
147603817eb8SCorentin Chary }
147703817eb8SCorentin Chary 
147803817eb8SCorentin Chary #ifdef CONFIG_VNC_JPEG
147903817eb8SCorentin Chary static int send_sub_rect_jpeg(VncState *vs, int x, int y, int w, int h,
148003817eb8SCorentin Chary                               int bg, int fg, int colors,
148103817eb8SCorentin Chary                               VncPalette *palette)
148203817eb8SCorentin Chary {
148303817eb8SCorentin Chary     int ret;
148403817eb8SCorentin Chary 
148503817eb8SCorentin Chary     if (colors == 0) {
148603817eb8SCorentin Chary         if (tight_detect_smooth_image(vs, w, h)) {
148703817eb8SCorentin Chary             int quality = tight_conf[vs->tight.quality].jpeg_quality;
148803817eb8SCorentin Chary 
148903817eb8SCorentin Chary             ret = send_jpeg_rect(vs, x, y, w, h, quality);
149003817eb8SCorentin Chary         } else {
149103817eb8SCorentin Chary             ret = send_full_color_rect(vs, x, y, w, h);
149203817eb8SCorentin Chary         }
149303817eb8SCorentin Chary     } else if (colors == 1) {
149403817eb8SCorentin Chary         ret = send_solid_rect(vs);
149503817eb8SCorentin Chary     } else if (colors == 2) {
149603817eb8SCorentin Chary         ret = send_mono_rect(vs, x, y, w, h, bg, fg);
149703817eb8SCorentin Chary     } else if (colors <= 256) {
149803817eb8SCorentin Chary         if (colors > 96 &&
149903817eb8SCorentin Chary             tight_detect_smooth_image(vs, w, h)) {
150003817eb8SCorentin Chary             int quality = tight_conf[vs->tight.quality].jpeg_quality;
150103817eb8SCorentin Chary 
150203817eb8SCorentin Chary             ret = send_jpeg_rect(vs, x, y, w, h, quality);
150303817eb8SCorentin Chary         } else {
150403817eb8SCorentin Chary             ret = send_palette_rect(vs, x, y, w, h, palette);
150503817eb8SCorentin Chary         }
1506*ad7ee4adSBlue Swirl     } else {
1507*ad7ee4adSBlue Swirl         ret = 0;
150803817eb8SCorentin Chary     }
150903817eb8SCorentin Chary     return ret;
151003817eb8SCorentin Chary }
151103817eb8SCorentin Chary #endif
151203817eb8SCorentin Chary 
1513245f7b51SCorentin Chary static int send_sub_rect(VncState *vs, int x, int y, int w, int h)
1514245f7b51SCorentin Chary {
15155136a052SCorentin Chary     VncPalette *palette = NULL;
1516245f7b51SCorentin Chary     uint32_t bg = 0, fg = 0;
1517245f7b51SCorentin Chary     int colors;
1518245f7b51SCorentin Chary     int ret = 0;
1519245f7b51SCorentin Chary 
1520d1af0e05SCorentin Chary     vnc_framebuffer_update(vs, x, y, w, h, vs->tight.type);
1521245f7b51SCorentin Chary 
1522245f7b51SCorentin Chary     vnc_tight_start(vs);
1523245f7b51SCorentin Chary     vnc_raw_send_framebuffer_update(vs, x, y, w, h);
1524245f7b51SCorentin Chary     vnc_tight_stop(vs);
1525245f7b51SCorentin Chary 
1526245f7b51SCorentin Chary     colors = tight_fill_palette(vs, x, y, w * h, &fg, &bg, &palette);
1527245f7b51SCorentin Chary 
1528245f7b51SCorentin Chary #ifdef CONFIG_VNC_JPEG
1529aee474ebSBlue Swirl     if (vs->tight.quality != (uint8_t)-1) {
153003817eb8SCorentin Chary         ret = send_sub_rect_jpeg(vs, x, y, w, h, bg, fg, colors, palette);
1531245f7b51SCorentin Chary     } else {
153203817eb8SCorentin Chary         ret = send_sub_rect_nojpeg(vs, x, y, w, h, bg, fg, colors, palette);
1533245f7b51SCorentin Chary     }
1534245f7b51SCorentin Chary #else
153503817eb8SCorentin Chary     ret = send_sub_rect_nojpeg(vs, x, y, w, h, bg, fg, colors, palette);
1536245f7b51SCorentin Chary #endif
153703817eb8SCorentin Chary 
15385136a052SCorentin Chary     palette_destroy(palette);
1539245f7b51SCorentin Chary     return ret;
1540245f7b51SCorentin Chary }
1541245f7b51SCorentin Chary 
1542245f7b51SCorentin Chary static int send_sub_rect_solid(VncState *vs, int x, int y, int w, int h)
1543245f7b51SCorentin Chary {
1544d1af0e05SCorentin Chary     vnc_framebuffer_update(vs, x, y, w, h, vs->tight.type);
1545245f7b51SCorentin Chary 
1546245f7b51SCorentin Chary     vnc_tight_start(vs);
1547245f7b51SCorentin Chary     vnc_raw_send_framebuffer_update(vs, x, y, w, h);
1548245f7b51SCorentin Chary     vnc_tight_stop(vs);
1549245f7b51SCorentin Chary 
1550245f7b51SCorentin Chary     return send_solid_rect(vs);
1551245f7b51SCorentin Chary }
1552245f7b51SCorentin Chary 
1553245f7b51SCorentin Chary static int send_rect_simple(VncState *vs, int x, int y, int w, int h)
1554245f7b51SCorentin Chary {
1555245f7b51SCorentin Chary     int max_size, max_width;
1556245f7b51SCorentin Chary     int max_sub_width, max_sub_height;
1557245f7b51SCorentin Chary     int dx, dy;
1558245f7b51SCorentin Chary     int rw, rh;
1559245f7b51SCorentin Chary     int n = 0;
1560245f7b51SCorentin Chary 
1561d1af0e05SCorentin Chary     max_size = tight_conf[vs->tight.compression].max_rect_size;
1562d1af0e05SCorentin Chary     max_width = tight_conf[vs->tight.compression].max_rect_width;
1563245f7b51SCorentin Chary 
1564245f7b51SCorentin Chary     if (w > max_width || w * h > max_size) {
1565245f7b51SCorentin Chary         max_sub_width = (w > max_width) ? max_width : w;
1566245f7b51SCorentin Chary         max_sub_height = max_size / max_sub_width;
1567245f7b51SCorentin Chary 
1568245f7b51SCorentin Chary         for (dy = 0; dy < h; dy += max_sub_height) {
1569245f7b51SCorentin Chary             for (dx = 0; dx < w; dx += max_width) {
1570245f7b51SCorentin Chary                 rw = MIN(max_sub_width, w - dx);
1571245f7b51SCorentin Chary                 rh = MIN(max_sub_height, h - dy);
1572245f7b51SCorentin Chary                 n += send_sub_rect(vs, x+dx, y+dy, rw, rh);
1573245f7b51SCorentin Chary             }
1574245f7b51SCorentin Chary         }
1575245f7b51SCorentin Chary     } else {
1576245f7b51SCorentin Chary         n += send_sub_rect(vs, x, y, w, h);
1577245f7b51SCorentin Chary     }
1578245f7b51SCorentin Chary 
1579245f7b51SCorentin Chary     return n;
1580245f7b51SCorentin Chary }
1581245f7b51SCorentin Chary 
1582245f7b51SCorentin Chary static int find_large_solid_color_rect(VncState *vs, int x, int y,
1583245f7b51SCorentin Chary                                        int w, int h, int max_rows)
1584245f7b51SCorentin Chary {
1585245f7b51SCorentin Chary     int dx, dy, dw, dh;
1586245f7b51SCorentin Chary     int n = 0;
1587245f7b51SCorentin Chary 
1588245f7b51SCorentin Chary     /* Try to find large solid-color areas and send them separately. */
1589245f7b51SCorentin Chary 
1590245f7b51SCorentin Chary     for (dy = y; dy < y + h; dy += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) {
1591245f7b51SCorentin Chary 
1592245f7b51SCorentin Chary         /* If a rectangle becomes too large, send its upper part now. */
1593245f7b51SCorentin Chary 
1594245f7b51SCorentin Chary         if (dy - y >= max_rows) {
1595245f7b51SCorentin Chary             n += send_rect_simple(vs, x, y, w, max_rows);
1596245f7b51SCorentin Chary             y += max_rows;
1597245f7b51SCorentin Chary             h -= max_rows;
1598245f7b51SCorentin Chary         }
1599245f7b51SCorentin Chary 
1600245f7b51SCorentin Chary         dh = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, (y + h - dy));
1601245f7b51SCorentin Chary 
1602245f7b51SCorentin Chary         for (dx = x; dx < x + w; dx += VNC_TIGHT_MAX_SPLIT_TILE_SIZE) {
1603245f7b51SCorentin Chary             uint32_t color_value;
1604245f7b51SCorentin Chary             int x_best, y_best, w_best, h_best;
1605245f7b51SCorentin Chary 
1606245f7b51SCorentin Chary             dw = MIN(VNC_TIGHT_MAX_SPLIT_TILE_SIZE, (x + w - dx));
1607245f7b51SCorentin Chary 
1608245f7b51SCorentin Chary             if (!check_solid_tile(vs, dx, dy, dw, dh, &color_value, false)) {
1609245f7b51SCorentin Chary                 continue ;
1610245f7b51SCorentin Chary             }
1611245f7b51SCorentin Chary 
1612245f7b51SCorentin Chary             /* Get dimensions of solid-color area. */
1613245f7b51SCorentin Chary 
1614245f7b51SCorentin Chary             find_best_solid_area(vs, dx, dy, w - (dx - x), h - (dy - y),
1615245f7b51SCorentin Chary                                  color_value, &w_best, &h_best);
1616245f7b51SCorentin Chary 
1617245f7b51SCorentin Chary             /* Make sure a solid rectangle is large enough
1618245f7b51SCorentin Chary                (or the whole rectangle is of the same color). */
1619245f7b51SCorentin Chary 
1620245f7b51SCorentin Chary             if (w_best * h_best != w * h &&
1621245f7b51SCorentin Chary                 w_best * h_best < VNC_TIGHT_MIN_SOLID_SUBRECT_SIZE) {
1622245f7b51SCorentin Chary                 continue;
1623245f7b51SCorentin Chary             }
1624245f7b51SCorentin Chary 
1625245f7b51SCorentin Chary             /* Try to extend solid rectangle to maximum size. */
1626245f7b51SCorentin Chary 
1627245f7b51SCorentin Chary             x_best = dx; y_best = dy;
1628245f7b51SCorentin Chary             extend_solid_area(vs, x, y, w, h, color_value,
1629245f7b51SCorentin Chary                               &x_best, &y_best, &w_best, &h_best);
1630245f7b51SCorentin Chary 
1631245f7b51SCorentin Chary             /* Send rectangles at top and left to solid-color area. */
1632245f7b51SCorentin Chary 
1633245f7b51SCorentin Chary             if (y_best != y) {
1634245f7b51SCorentin Chary                 n += send_rect_simple(vs, x, y, w, y_best-y);
1635245f7b51SCorentin Chary             }
1636245f7b51SCorentin Chary             if (x_best != x) {
1637efe556adSCorentin Chary                 n += tight_send_framebuffer_update(vs, x, y_best,
1638245f7b51SCorentin Chary                                                    x_best-x, h_best);
1639245f7b51SCorentin Chary             }
1640245f7b51SCorentin Chary 
1641245f7b51SCorentin Chary             /* Send solid-color rectangle. */
1642245f7b51SCorentin Chary             n += send_sub_rect_solid(vs, x_best, y_best, w_best, h_best);
1643245f7b51SCorentin Chary 
1644245f7b51SCorentin Chary             /* Send remaining rectangles (at right and bottom). */
1645245f7b51SCorentin Chary 
1646245f7b51SCorentin Chary             if (x_best + w_best != x + w) {
1647efe556adSCorentin Chary                 n += tight_send_framebuffer_update(vs, x_best+w_best,
1648245f7b51SCorentin Chary                                                    y_best,
1649245f7b51SCorentin Chary                                                    w-(x_best-x)-w_best,
1650245f7b51SCorentin Chary                                                    h_best);
1651245f7b51SCorentin Chary             }
1652245f7b51SCorentin Chary             if (y_best + h_best != y + h) {
1653efe556adSCorentin Chary                 n += tight_send_framebuffer_update(vs, x, y_best+h_best,
1654245f7b51SCorentin Chary                                                    w, h-(y_best-y)-h_best);
1655245f7b51SCorentin Chary             }
1656245f7b51SCorentin Chary 
1657245f7b51SCorentin Chary             /* Return after all recursive calls are done. */
1658245f7b51SCorentin Chary             return n;
1659245f7b51SCorentin Chary         }
1660245f7b51SCorentin Chary     }
1661245f7b51SCorentin Chary     return n + send_rect_simple(vs, x, y, w, h);
1662245f7b51SCorentin Chary }
1663245f7b51SCorentin Chary 
1664efe556adSCorentin Chary static int tight_send_framebuffer_update(VncState *vs, int x, int y,
1665245f7b51SCorentin Chary                                          int w, int h)
1666245f7b51SCorentin Chary {
1667245f7b51SCorentin Chary     int max_rows;
1668245f7b51SCorentin Chary 
1669245f7b51SCorentin Chary     if (vs->clientds.pf.bytes_per_pixel == 4 && vs->clientds.pf.rmax == 0xFF &&
1670245f7b51SCorentin Chary         vs->clientds.pf.bmax == 0xFF && vs->clientds.pf.gmax == 0xFF) {
1671d1af0e05SCorentin Chary         vs->tight.pixel24 = true;
1672245f7b51SCorentin Chary     } else {
1673d1af0e05SCorentin Chary         vs->tight.pixel24 = false;
1674245f7b51SCorentin Chary     }
1675245f7b51SCorentin Chary 
1676245f7b51SCorentin Chary     if (w * h < VNC_TIGHT_MIN_SPLIT_RECT_SIZE)
1677245f7b51SCorentin Chary         return send_rect_simple(vs, x, y, w, h);
1678245f7b51SCorentin Chary 
1679245f7b51SCorentin Chary     /* Calculate maximum number of rows in one non-solid rectangle. */
1680245f7b51SCorentin Chary 
1681d1af0e05SCorentin Chary     max_rows = tight_conf[vs->tight.compression].max_rect_size;
1682d1af0e05SCorentin Chary     max_rows /= MIN(tight_conf[vs->tight.compression].max_rect_width, w);
1683245f7b51SCorentin Chary 
1684245f7b51SCorentin Chary     return find_large_solid_color_rect(vs, x, y, w, h, max_rows);
1685245f7b51SCorentin Chary }
1686245f7b51SCorentin Chary 
1687efe556adSCorentin Chary int vnc_tight_send_framebuffer_update(VncState *vs, int x, int y,
1688efe556adSCorentin Chary                                       int w, int h)
1689efe556adSCorentin Chary {
1690d1af0e05SCorentin Chary     vs->tight.type = VNC_ENCODING_TIGHT;
1691efe556adSCorentin Chary     return tight_send_framebuffer_update(vs, x, y, w, h);
1692efe556adSCorentin Chary }
1693efe556adSCorentin Chary 
1694efe556adSCorentin Chary int vnc_tight_png_send_framebuffer_update(VncState *vs, int x, int y,
1695efe556adSCorentin Chary                                           int w, int h)
1696efe556adSCorentin Chary {
1697d1af0e05SCorentin Chary     vs->tight.type = VNC_ENCODING_TIGHT_PNG;
1698efe556adSCorentin Chary     return tight_send_framebuffer_update(vs, x, y, w, h);
1699efe556adSCorentin Chary }
1700efe556adSCorentin Chary 
1701245f7b51SCorentin Chary void vnc_tight_clear(VncState *vs)
1702245f7b51SCorentin Chary {
1703245f7b51SCorentin Chary     int i;
1704d1af0e05SCorentin Chary     for (i=0; i<ARRAY_SIZE(vs->tight.stream); i++) {
1705d1af0e05SCorentin Chary         if (vs->tight.stream[i].opaque) {
1706d1af0e05SCorentin Chary             deflateEnd(&vs->tight.stream[i]);
1707245f7b51SCorentin Chary         }
1708245f7b51SCorentin Chary     }
1709245f7b51SCorentin Chary 
1710d1af0e05SCorentin Chary     buffer_free(&vs->tight.tight);
1711d1af0e05SCorentin Chary     buffer_free(&vs->tight.zlib);
1712d1af0e05SCorentin Chary     buffer_free(&vs->tight.gradient);
1713245f7b51SCorentin Chary #ifdef CONFIG_VNC_JPEG
1714d1af0e05SCorentin Chary     buffer_free(&vs->tight.jpeg);
1715245f7b51SCorentin Chary #endif
1716b5469b11SCorentin Chary #ifdef CONFIG_VNC_PNG
1717b5469b11SCorentin Chary     buffer_free(&vs->tight.png);
1718b5469b11SCorentin Chary #endif
1719245f7b51SCorentin Chary }
1720