1 /* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3 Copyright (C) 2009-2015 Red Hat, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 */
18 #include <config.h>
19
20 #include <sys/stat.h>
21
22 #include "spice-bitmap-utils.h"
23
24 #define RED_BITMAP_UTILS_RGB16
25 #include "spice-bitmap-utils.tmpl.c"
26 #define RED_BITMAP_UTILS_RGB24
27 #include "spice-bitmap-utils.tmpl.c"
28 #define RED_BITMAP_UTILS_RGB32
29 #include "spice-bitmap-utils.tmpl.c"
30
31 #define GRADUAL_HIGH_RGB24_TH -0.03
32 #define GRADUAL_HIGH_RGB16_TH 0
33
34 // setting a more permissive threshold for stream identification in order
35 // not to miss streams that were artificially scaled on the guest (e.g., full screen view
36 // in window media player 12). see red_stream_add_frame
37 #define GRADUAL_MEDIUM_SCORE_TH 0.002
38
39 // assumes that stride doesn't overflow
bitmap_get_graduality_level(SpiceBitmap * bitmap)40 BitmapGradualType bitmap_get_graduality_level(SpiceBitmap *bitmap)
41 {
42 double score = 0.0;
43 int num_samples = 0;
44 int num_lines;
45 double chunk_score = 0.0;
46 int chunk_num_samples = 0;
47 uint32_t x, i;
48 SpiceChunk *chunk;
49
50 chunk = bitmap->data->chunk;
51 for (i = 0; i < bitmap->data->num_chunks; i++) {
52 num_lines = chunk[i].len / bitmap->stride;
53 x = bitmap->x;
54 switch (bitmap->format) {
55 case SPICE_BITMAP_FMT_16BIT:
56 compute_lines_gradual_score_rgb16((rgb16_pixel_t *)chunk[i].data, x, num_lines,
57 &chunk_score, &chunk_num_samples);
58 break;
59 case SPICE_BITMAP_FMT_24BIT:
60 compute_lines_gradual_score_rgb24((rgb24_pixel_t *)chunk[i].data, x, num_lines,
61 &chunk_score, &chunk_num_samples);
62 break;
63 case SPICE_BITMAP_FMT_32BIT:
64 case SPICE_BITMAP_FMT_RGBA:
65 compute_lines_gradual_score_rgb32((rgb32_pixel_t *)chunk[i].data, x, num_lines,
66 &chunk_score, &chunk_num_samples);
67 break;
68 default:
69 spice_error("invalid bitmap format (not RGB) %u", bitmap->format);
70 }
71 score += chunk_score;
72 num_samples += chunk_num_samples;
73 }
74
75 spice_assert(num_samples);
76 score /= num_samples;
77
78 if (bitmap->format == SPICE_BITMAP_FMT_16BIT) {
79 if (score < GRADUAL_HIGH_RGB16_TH) {
80 return BITMAP_GRADUAL_HIGH;
81 }
82 } else {
83 if (score < GRADUAL_HIGH_RGB24_TH) {
84 return BITMAP_GRADUAL_HIGH;
85 }
86 }
87
88 if (score < GRADUAL_MEDIUM_SCORE_TH) {
89 return BITMAP_GRADUAL_MEDIUM;
90 }
91
92 return BITMAP_GRADUAL_LOW;
93 }
94
bitmap_has_extra_stride(SpiceBitmap * bitmap)95 int bitmap_has_extra_stride(SpiceBitmap *bitmap)
96 {
97 spice_assert(bitmap);
98 if (bitmap_fmt_is_rgb(bitmap->format)) {
99 return ((bitmap->x * bitmap_fmt_get_bytes_per_pixel(bitmap->format)) < bitmap->stride);
100 }
101
102 switch (bitmap->format) {
103 case SPICE_BITMAP_FMT_8BIT:
104 return (bitmap->x < bitmap->stride);
105 case SPICE_BITMAP_FMT_4BIT_BE:
106 case SPICE_BITMAP_FMT_4BIT_LE: {
107 int bytes_width = SPICE_ALIGN(bitmap->x, 2) >> 1;
108 return bytes_width < bitmap->stride;
109 }
110 case SPICE_BITMAP_FMT_1BIT_BE:
111 case SPICE_BITMAP_FMT_1BIT_LE: {
112 int bytes_width = SPICE_ALIGN(bitmap->x, 8) >> 3;
113 return bytes_width < bitmap->stride;
114 }
115 default:
116 spice_error("invalid image type %u", bitmap->format);
117 return 0;
118 }
119
120 return 0;
121 }
122
spice_bitmap_from_surface_type(uint32_t surface_format)123 int spice_bitmap_from_surface_type(uint32_t surface_format)
124 {
125 switch (surface_format) {
126 case SPICE_SURFACE_FMT_16_555:
127 return SPICE_BITMAP_FMT_16BIT;
128 case SPICE_SURFACE_FMT_32_xRGB:
129 return SPICE_BITMAP_FMT_32BIT;
130 case SPICE_SURFACE_FMT_32_ARGB:
131 return SPICE_BITMAP_FMT_RGBA;
132 case SPICE_SURFACE_FMT_8_A:
133 return SPICE_BITMAP_FMT_8BIT_A;
134 default:
135 spice_critical("Unsupported surface format");
136 }
137 return SPICE_BITMAP_FMT_INVALID;
138 }
139
140 #ifdef DUMP_BITMAP
141 #define RAM_PATH "/tmp/tmpfs"
142
put_16le(uint8_t ** ptr,uint16_t val)143 static void put_16le(uint8_t **ptr, uint16_t val)
144 {
145 **ptr = val & 0xffu;
146 (*ptr)++;
147 val >>= 8;
148 **ptr = val & 0xffu;
149 (*ptr)++;
150 }
151
put_32le(uint8_t ** ptr,uint32_t val)152 static void put_32le(uint8_t **ptr, uint32_t val)
153 {
154 put_16le(ptr, val & 0xffffu);
155 val >>= 16;
156 put_16le(ptr, val & 0xffffu);
157 }
158
159 #define WRITE(buf, size, f) do { \
160 if (fwrite(buf, 1, (size), f) != (size)) \
161 goto write_err; \
162 } while(0)
163
dump_palette(FILE * f,SpicePalette * plt)164 static bool dump_palette(FILE *f, SpicePalette* plt)
165 {
166 int i;
167 for (i = 0; i < plt->num_ents; i++) {
168 WRITE(plt->ents + i, sizeof(uint32_t), f);
169 }
170 return true;
171
172 write_err:
173 return false;
174 }
175
dump_line(FILE * f,uint8_t * line,uint16_t n_pixel_bits,int width,int row_size)176 static bool dump_line(FILE *f, uint8_t* line, uint16_t n_pixel_bits, int width, int row_size)
177 {
178 static const char zeroes[4] = { 0 };
179 int copy_bytes_size = SPICE_ALIGN(n_pixel_bits * width, 8) / 8;
180
181 WRITE(line, copy_bytes_size, f);
182 // each line should be 4 bytes aligned
183 g_return_val_if_fail(row_size - copy_bytes_size >= 0 && row_size - copy_bytes_size <= 4, false);
184 WRITE(zeroes, row_size - copy_bytes_size, f);
185 return true;
186
187 write_err:
188 return false;
189 }
190
dump_bitmap(SpiceBitmap * bitmap)191 void dump_bitmap(SpiceBitmap *bitmap)
192 {
193 static uint32_t file_id = 0;
194
195 char file_str[200];
196 int rgb = TRUE;
197 uint16_t n_pixel_bits;
198 SpicePalette *plt = NULL;
199 uint32_t id;
200 int row_size;
201 uint32_t file_size;
202 int alpha = 0;
203 uint32_t header_size = 14 + 40;
204 uint32_t bitmap_data_offset;
205 FILE *f;
206 int i, j;
207 uint8_t header[128], *ptr;
208
209 switch (bitmap->format) {
210 case SPICE_BITMAP_FMT_1BIT_BE:
211 case SPICE_BITMAP_FMT_1BIT_LE:
212 rgb = FALSE;
213 n_pixel_bits = 1;
214 break;
215 case SPICE_BITMAP_FMT_4BIT_BE:
216 case SPICE_BITMAP_FMT_4BIT_LE:
217 rgb = FALSE;
218 n_pixel_bits = 4;
219 break;
220 case SPICE_BITMAP_FMT_8BIT:
221 rgb = FALSE;
222 n_pixel_bits = 8;
223 break;
224 case SPICE_BITMAP_FMT_16BIT:
225 n_pixel_bits = 16;
226 break;
227 case SPICE_BITMAP_FMT_24BIT:
228 n_pixel_bits = 24;
229 break;
230 case SPICE_BITMAP_FMT_32BIT:
231 n_pixel_bits = 32;
232 break;
233 case SPICE_BITMAP_FMT_RGBA:
234 n_pixel_bits = 32;
235 alpha = 1;
236 break;
237 default:
238 spice_error("invalid bitmap format %u", bitmap->format);
239 return;
240 }
241
242 if (!rgb) {
243 if (!bitmap->palette) {
244 return; // dont dump masks.
245 }
246 plt = bitmap->palette;
247 }
248 row_size = (((bitmap->x * n_pixel_bits) + 31) / 32) * 4;
249 bitmap_data_offset = header_size;
250
251 if (plt) {
252 bitmap_data_offset += plt->num_ents * 4;
253 }
254 file_size = bitmap_data_offset + (bitmap->y * row_size);
255
256 id = ++file_id;
257 mkdir(RAM_PATH, 0755);
258 sprintf(file_str, "%s/%u.bmp", RAM_PATH, id);
259
260 f = fopen(file_str, "wb");
261 if (!f) {
262 spice_error("Error creating bmp");
263 return;
264 }
265
266 /* writing the bmp v3 header */
267 ptr = header;
268 put_16le(&ptr, 0x4D42); // "BM"
269 put_32le(&ptr, file_size);
270 put_16le(&ptr, alpha);
271 put_16le(&ptr, 0);
272 put_32le(&ptr, bitmap_data_offset);
273 put_32le(&ptr, header_size - 14);
274 put_32le(&ptr, bitmap->x);
275 put_32le(&ptr, bitmap->flags & SPICE_BITMAP_FLAGS_TOP_DOWN ? -bitmap->y : bitmap->y);
276
277 put_16le(&ptr, 1); // plane
278 put_16le(&ptr, n_pixel_bits);
279
280 put_32le(&ptr, 0); // compression
281
282 put_32le(&ptr, 0); //file_size - bitmap_data_offset;
283 put_32le(&ptr, 0);
284 put_32le(&ptr, 0);
285 put_32le(&ptr, !plt ? 0 : plt->num_ents); // plt entries
286 put_32le(&ptr, 0);
287
288 WRITE(header, ptr - header, f);
289
290 if (plt) {
291 if (!dump_palette(f, plt))
292 goto write_err;
293 }
294 /* writing the data */
295 for (i = 0; i < bitmap->data->num_chunks; i++) {
296 SpiceChunk *chunk = &bitmap->data->chunk[i];
297 int num_lines = chunk->len / bitmap->stride;
298 for (j = 0; j < num_lines; j++) {
299 if (!dump_line(f, chunk->data + (j * bitmap->stride), n_pixel_bits, bitmap->x, row_size))
300 goto write_err;
301 }
302 }
303 fclose(f);
304 return;
305
306 write_err:
307 fclose(f);
308 remove(file_str);
309 }
310 #endif
311