FreeRDP
Loading...
Searching...
No Matches
codec/bitmap.c
1
20#include <winpr/assert.h>
21#include <winpr/cast.h>
22
23#include <freerdp/config.h>
24
25#include <freerdp/codec/bitmap.h>
26#include <freerdp/codec/planar.h>
27
28static INLINE UINT16 GETPIXEL16(const void* WINPR_RESTRICT d, UINT32 x, UINT32 y, UINT32 w)
29{
30 const BYTE* WINPR_RESTRICT src = (const BYTE*)d + ((y * w + x) * sizeof(UINT16));
31 return WINPR_ASSERTING_INT_CAST(UINT16, ((UINT16)src[1] << 8) | (UINT16)src[0]);
32}
33
34static INLINE UINT32 GETPIXEL32(const void* WINPR_RESTRICT d, UINT32 x, UINT32 y, UINT32 w)
35{
36 const BYTE* WINPR_RESTRICT src = (const BYTE*)d + ((y * w + x) * sizeof(UINT32));
37 return (((UINT32)src[3]) << 24) | (((UINT32)src[2]) << 16) | (((UINT32)src[1]) << 8) |
38 (src[0] & 0xFF);
39}
40
41/*****************************************************************************/
42static INLINE UINT16 IN_PIXEL16(const void* WINPR_RESTRICT in_ptr, UINT32 in_x, UINT32 in_y,
43 UINT32 in_w, UINT16 in_last_pixel)
44{
45 if (in_ptr == 0)
46 return 0;
47 else if (in_x < in_w)
48 return GETPIXEL16(in_ptr, in_x, in_y, in_w);
49 else
50 return in_last_pixel;
51}
52
53/*****************************************************************************/
54static INLINE UINT32 IN_PIXEL32(const void* WINPR_RESTRICT in_ptr, UINT32 in_x, UINT32 in_y,
55 UINT32 in_w, UINT32 in_last_pixel)
56{
57 if (in_ptr == 0)
58 return 0;
59 else if (in_x < in_w)
60 return GETPIXEL32(in_ptr, in_x, in_y, in_w);
61 else
62 return in_last_pixel;
63}
64
65/*****************************************************************************/
66/* color */
67static INLINE UINT16 out_color_count_2(UINT16 in_count, wStream* WINPR_RESTRICT in_s,
68 UINT16 in_data)
69{
70 if (in_count > 0)
71 {
72 if (in_count < 32)
73 {
74 const BYTE temp = ((0x3 << 5) | in_count) & 0xFF;
75 Stream_Write_UINT8(in_s, temp);
76 }
77 else if (in_count < 256 + 32)
78 {
79 const BYTE temp = (in_count - 32) & 0xFF;
80 Stream_Write_UINT8(in_s, 0x60);
81 Stream_Write_UINT8(in_s, temp);
82 }
83 else
84 {
85 Stream_Write_UINT8(in_s, 0xf3);
86 Stream_Write_UINT16(in_s, in_count);
87 }
88
89 Stream_Write_UINT16(in_s, in_data);
90 }
91
92 return 0;
93}
94#define OUT_COLOR_COUNT2(in_count, in_s, in_data) \
95 in_count = out_color_count_2(in_count, in_s, in_data)
96
97/*****************************************************************************/
98/* color */
99static INLINE UINT16 out_color_count_3(UINT16 in_count, wStream* WINPR_RESTRICT in_s,
100 UINT32 in_data)
101{
102 if (in_count > 0)
103 {
104 if (in_count < 32)
105 {
106 const BYTE temp = ((0x3 << 5) | in_count) & 0xFF;
107 Stream_Write_UINT8(in_s, temp);
108 }
109 else if (in_count < 256 + 32)
110 {
111 const BYTE temp = (in_count - 32) & 0xFF;
112 Stream_Write_UINT8(in_s, 0x60);
113 Stream_Write_UINT8(in_s, temp);
114 }
115 else
116 {
117 Stream_Write_UINT8(in_s, 0xf3);
118 Stream_Write_UINT16(in_s, in_count);
119 }
120
121 Stream_Write_UINT8(in_s, in_data & 0xFF);
122
123 Stream_Write_UINT8(in_s, (in_data >> 8) & 0xFF);
124 Stream_Write_UINT8(in_s, (in_data >> 16) & 0xFF);
125 }
126
127 return 0;
128}
129
130#define OUT_COLOR_COUNT3(in_count, in_s, in_data) \
131 in_count = out_color_count_3(in_count, in_s, in_data)
132
133/*****************************************************************************/
134/* copy */
135static INLINE UINT16 out_copy_count_2(UINT16 in_count, wStream* WINPR_RESTRICT in_s,
136 wStream* WINPR_RESTRICT in_data)
137
138{
139 if (in_count > 0)
140 {
141 if (in_count < 32)
142 {
143 const BYTE temp = ((0x4 << 5) | in_count) & 0xFF;
144 Stream_Write_UINT8(in_s, temp);
145 }
146 else if (in_count < 256 + 32)
147 {
148 const BYTE temp = (in_count - 32) & 0xFF;
149 Stream_Write_UINT8(in_s, 0x80);
150 Stream_Write_UINT8(in_s, temp);
151 }
152 else
153 {
154 Stream_Write_UINT8(in_s, 0xf4);
155 Stream_Write_UINT16(in_s, in_count);
156 }
157
158 Stream_Write(in_s, Stream_Buffer(in_data), 2ULL * in_count);
159 }
160
161 Stream_SetPosition(in_data, 0);
162 return 0;
163}
164#define OUT_COPY_COUNT2(in_count, in_s, in_data) \
165 in_count = out_copy_count_2(in_count, in_s, in_data)
166/*****************************************************************************/
167/* copy */
168static INLINE UINT16 out_copy_count_3(UINT16 in_count, wStream* WINPR_RESTRICT in_s,
169 wStream* WINPR_RESTRICT in_data)
170{
171 if (in_count > 0)
172 {
173 if (in_count < 32)
174 {
175 const BYTE temp = ((0x4 << 5) | in_count) & 0xFF;
176 Stream_Write_UINT8(in_s, temp);
177 }
178 else if (in_count < 256 + 32)
179 {
180 const BYTE temp = (in_count - 32) & 0xFF;
181 Stream_Write_UINT8(in_s, 0x80);
182 Stream_Write_UINT8(in_s, temp);
183 }
184 else
185 {
186 Stream_Write_UINT8(in_s, 0xf4);
187 Stream_Write_UINT16(in_s, in_count);
188 }
189
190 Stream_Write(in_s, Stream_Pointer(in_data), 3ULL * in_count);
191 }
192
193 Stream_SetPosition(in_data, 0);
194 return 0;
195}
196#define OUT_COPY_COUNT3(in_count, in_s, in_data) \
197 in_count = out_copy_count_3(in_count, in_s, in_data)
198
199/*****************************************************************************/
200/* bicolor */
201static INLINE UINT16 out_bicolor_count_2(UINT16 in_count, wStream* WINPR_RESTRICT in_s,
202 UINT16 in_color1, UINT16 in_color2)
203{
204 if (in_count > 0)
205 {
206 if (in_count / 2 < 16)
207 {
208 const BYTE temp = ((0xe << 4) | (in_count / 2)) & 0xFF;
209 Stream_Write_UINT8(in_s, temp);
210 }
211 else if (in_count / 2 < 256 + 16)
212 {
213 const BYTE temp = (in_count / 2 - 16) & 0xFF;
214 Stream_Write_UINT8(in_s, 0xe0);
215 Stream_Write_UINT8(in_s, temp);
216 }
217 else
218 {
219 Stream_Write_UINT8(in_s, 0xf8);
220 Stream_Write_UINT16(in_s, in_count / 2);
221 }
222
223 Stream_Write_UINT16(in_s, in_color1);
224 Stream_Write_UINT16(in_s, in_color2);
225 }
226
227 return 0;
228}
229
230#define OUT_BICOLOR_COUNT2(in_count, in_s, in_color1, in_color2) \
231 in_count = out_bicolor_count_2(in_count, in_s, in_color1, in_color2)
232
233/*****************************************************************************/
234/* bicolor */
235static INLINE UINT16 out_bicolor_count_3(UINT16 in_count, wStream* WINPR_RESTRICT in_s,
236 UINT32 in_color1, UINT32 in_color2)
237{
238 if (in_count > 0)
239 {
240 if (in_count / 2 < 16)
241 {
242 const BYTE temp = ((0xe << 4) | (in_count / 2)) & 0xFF;
243 Stream_Write_UINT8(in_s, temp);
244 }
245 else if (in_count / 2 < 256 + 16)
246 {
247 const BYTE temp = (in_count / 2 - 16) & 0xFF;
248 Stream_Write_UINT8(in_s, 0xe0);
249 Stream_Write_UINT8(in_s, temp);
250 }
251 else
252 {
253 Stream_Write_UINT8(in_s, 0xf8);
254 Stream_Write_UINT16(in_s, in_count / 2);
255 }
256
257 Stream_Write_UINT8(in_s, in_color1 & 0xFF);
258 Stream_Write_UINT8(in_s, (in_color1 >> 8) & 0xFF);
259 Stream_Write_UINT8(in_s, (in_color1 >> 16) & 0xFF);
260 Stream_Write_UINT8(in_s, in_color2 & 0xFF);
261 Stream_Write_UINT8(in_s, (in_color2 >> 8) & 0xFF);
262 Stream_Write_UINT8(in_s, (in_color2 >> 16) & 0xFF);
263 }
264
265 return 0;
266}
267
268#define OUT_BICOLOR_COUNT3(in_count, in_s, in_color1, in_color2) \
269 in_count = out_bicolor_count_3(in_count, in_s, in_color1, in_color2)
270
271/*****************************************************************************/
272/* fill */
273static INLINE UINT16 out_fill_count_2(UINT16 in_count, wStream* WINPR_RESTRICT in_s)
274{
275 if (in_count > 0)
276 {
277 if (in_count < 32)
278 {
279 Stream_Write_UINT8(in_s, in_count & 0xFF);
280 }
281 else if (in_count < 256 + 32)
282 {
283 const BYTE temp = (in_count - 32) & 0xFF;
284 Stream_Write_UINT8(in_s, 0x0);
285 Stream_Write_UINT8(in_s, temp);
286 }
287 else
288 {
289 Stream_Write_UINT8(in_s, 0xf0);
290 Stream_Write_UINT16(in_s, in_count);
291 }
292 }
293
294 return 0;
295}
296
297#define OUT_FILL_COUNT2(in_count, in_s) in_count = out_fill_count_2(in_count, in_s)
298
299/*****************************************************************************/
300/* fill */
301static INLINE UINT16 out_fill_count_3(UINT16 in_count, wStream* WINPR_RESTRICT in_s)
302{
303 if (in_count > 0)
304 {
305 if (in_count < 32)
306 {
307 Stream_Write_UINT8(in_s, in_count & 0xFF);
308 }
309 else if (in_count < 256 + 32)
310 {
311 const BYTE temp = (in_count - 32) & 0xFF;
312 Stream_Write_UINT8(in_s, 0x0);
313 Stream_Write_UINT8(in_s, temp);
314 }
315 else
316 {
317 Stream_Write_UINT8(in_s, 0xf0);
318 Stream_Write_UINT16(in_s, in_count);
319 }
320 }
321
322 return 0;
323}
324#define OUT_FILL_COUNT3(in_count, in_s) in_count = out_fill_count_3(in_count, in_s)
325
326/*****************************************************************************/
327/* mix */
328static INLINE UINT16 out_mix_count_2(UINT16 in_count, wStream* WINPR_RESTRICT in_s)
329{
330 if (in_count > 0)
331 {
332 if (in_count < 32)
333 {
334 const BYTE temp = ((0x1 << 5) | in_count) & 0xFF;
335 Stream_Write_UINT8(in_s, temp);
336 }
337 else if (in_count < 256 + 32)
338 {
339 const BYTE temp = (in_count - 32) & 0xFF;
340 Stream_Write_UINT8(in_s, 0x20);
341 Stream_Write_UINT8(in_s, temp);
342 }
343 else
344 {
345 Stream_Write_UINT8(in_s, 0xf1);
346 Stream_Write_UINT16(in_s, in_count);
347 }
348 }
349
350 return 0;
351}
352#define OUT_MIX_COUNT2(in_count, in_s) in_count = out_mix_count_2(in_count, in_s)
353
354/*****************************************************************************/
355/* mix */
356static INLINE UINT16 out_mix_count_3(UINT16 in_count, wStream* WINPR_RESTRICT in_s)
357{
358 if (in_count > 0)
359 {
360 if (in_count < 32)
361 {
362 const BYTE temp = ((0x1 << 5) | in_count) & 0xFF;
363 Stream_Write_UINT8(in_s, temp);
364 }
365 else if (in_count < 256 + 32)
366 {
367 const BYTE temp = (in_count - 32) & 0xFF;
368 Stream_Write_UINT8(in_s, 0x20);
369 Stream_Write_UINT8(in_s, temp);
370 }
371 else
372 {
373 Stream_Write_UINT8(in_s, 0xf1);
374 Stream_Write_UINT16(in_s, in_count);
375 }
376 }
377
378 return 0;
379}
380
381#define OUT_MIX_COUNT3(in_count, in_s) in_count = out_mix_count_3(in_count, in_s)
382
383/*****************************************************************************/
384/* fom */
385static INLINE UINT16 out_from_count_2(UINT16 in_count, wStream* WINPR_RESTRICT in_s,
386 const int8_t* WINPR_RESTRICT in_mask, size_t in_mask_len)
387{
388 if (in_count > 0)
389 {
390 if ((in_count % 8) == 0 && in_count < 249)
391 {
392 const BYTE temp = ((0x2 << 5) | (in_count / 8)) & 0xFF;
393 Stream_Write_UINT8(in_s, temp);
394 }
395 else if (in_count < 256)
396 {
397 const BYTE temp = (in_count - 1) & 0xFF;
398 Stream_Write_UINT8(in_s, 0x40);
399 Stream_Write_UINT8(in_s, temp);
400 }
401 else
402 {
403 Stream_Write_UINT8(in_s, 0xf2);
404 Stream_Write_UINT16(in_s, in_count);
405 }
406
407 Stream_Write(in_s, in_mask, in_mask_len);
408 }
409
410 return 0;
411}
412#define OUT_FOM_COUNT2(in_count, in_s, in_mask, in_mask_len) \
413 in_count = out_from_count_2(in_count, in_s, in_mask, in_mask_len)
414
415/*****************************************************************************/
416/* fill or mix (fom) */
417static INLINE UINT16 out_from_count_3(UINT16 in_count, wStream* WINPR_RESTRICT in_s,
418 const int8_t* WINPR_RESTRICT in_mask, size_t in_mask_len)
419{
420 if (in_count > 0)
421 {
422 if ((in_count % 8) == 0 && in_count < 249)
423 {
424 const BYTE temp = ((0x2 << 5) | (in_count / 8)) & 0xFF;
425 Stream_Write_UINT8(in_s, temp);
426 }
427 else if (in_count < 256)
428 {
429 const BYTE temp = (in_count - 1) & 0xFF;
430 Stream_Write_UINT8(in_s, 0x40);
431 Stream_Write_UINT8(in_s, temp);
432 }
433 else
434 {
435 Stream_Write_UINT8(in_s, 0xf2);
436 Stream_Write_UINT16(in_s, in_count);
437 }
438
439 Stream_Write(in_s, in_mask, in_mask_len);
440 }
441
442 return 0;
443}
444#define OUT_FOM_COUNT3(in_count, in_s, in_mask, in_mask_len) \
445 in_count = out_from_count_3(in_count, in_s, in_mask, in_mask_len)
446
447#define TEST_FILL ((last_line == 0 && pixel == 0) || (last_line != 0 && pixel == ypixel))
448#define TEST_MIX ((last_line == 0 && pixel == mix) || (last_line != 0 && pixel == (ypixel ^ mix)))
449#define TEST_FOM TEST_FILL || TEST_MIX
450#define TEST_COLOR pixel == last_pixel
451#define TEST_BICOLOR \
452 ((pixel != last_pixel) && \
453 ((!bicolor_spin && (pixel == bicolor1) && (last_pixel == bicolor2)) || \
454 (bicolor_spin && (pixel == bicolor2) && (last_pixel == bicolor1))))
455#define RESET_COUNTS \
456 do \
457 { \
458 bicolor_count = 0; \
459 fill_count = 0; \
460 color_count = 0; \
461 mix_count = 0; \
462 fom_count = 0; \
463 fom_mask_len = 0; \
464 bicolor_spin = FALSE; \
465 } while (0)
466
467static INLINE SSIZE_T freerdp_bitmap_compress_24(const void* WINPR_RESTRICT srcData, UINT32 width,
468 WINPR_ATTR_UNUSED UINT32 height,
469 wStream* WINPR_RESTRICT s, UINT32 byte_limit,
470 UINT32 start_line, wStream* WINPR_RESTRICT temp_s,
471 UINT32 e)
472{
473 int8_t fom_mask[8192] = { 0 }; /* good for up to 64K bitmap */
474 SSIZE_T lines_sent = 0;
475 UINT16 count = 0;
476 UINT16 color_count = 0;
477 UINT32 last_pixel = 0;
478 UINT32 last_ypixel = 0;
479 UINT16 bicolor_count = 0;
480 UINT32 bicolor1 = 0;
481 UINT32 bicolor2 = 0;
482 BOOL bicolor_spin = FALSE;
483 UINT32 end = width + e;
484 UINT32 out_count = end * 3;
485 UINT16 fill_count = 0;
486 UINT16 mix_count = 0;
487 const UINT32 mix = 0xFFFFFF;
488 UINT16 fom_count = 0;
489 size_t fom_mask_len = 0;
490 const char* start = (const char*)srcData;
491 const char* line = start + 4ULL * width * start_line;
492 const char* last_line = NULL;
493
494 while ((line >= start) && (out_count < 32768))
495 {
496 size_t i = Stream_GetPosition(s) + 3ULL * count;
497
498 if ((i - (3ULL * color_count) >= byte_limit) &&
499 (i - (3ULL * bicolor_count) >= byte_limit) && (i - (3ULL * fill_count) >= byte_limit) &&
500 (i - (3ULL * mix_count) >= byte_limit) && (i - (3ULL * fom_count) >= byte_limit))
501 {
502 break;
503 }
504
505 out_count += end * 3;
506
507 for (UINT32 j = 0; j < end; j++)
508 {
509 /* read next pixel */
510 const UINT32 pixel = IN_PIXEL32(line, j, 0, width, last_pixel);
511 const UINT32 ypixel = IN_PIXEL32(last_line, j, 0, width, last_ypixel);
512
513 if (!TEST_FILL)
514 {
515 if (fill_count > 3 && fill_count >= color_count && fill_count >= bicolor_count &&
516 fill_count >= mix_count && fill_count >= fom_count)
517 {
518 if (fill_count > count)
519 return -1;
520
521 count -= fill_count;
522 OUT_COPY_COUNT3(count, s, temp_s);
523 OUT_FILL_COUNT3(fill_count, s);
524 RESET_COUNTS;
525 }
526
527 fill_count = 0;
528 }
529
530 if (!TEST_MIX)
531 {
532 if (mix_count > 3 && mix_count >= fill_count && mix_count >= bicolor_count &&
533 mix_count >= color_count && mix_count >= fom_count)
534 {
535 if (mix_count > count)
536 return -1;
537
538 count -= mix_count;
539 OUT_COPY_COUNT3(count, s, temp_s);
540 OUT_MIX_COUNT3(mix_count, s);
541 RESET_COUNTS;
542 }
543
544 mix_count = 0;
545 }
546
547 if (!(TEST_COLOR))
548 {
549 if (color_count > 3 && color_count >= fill_count && color_count >= bicolor_count &&
550 color_count >= mix_count && color_count >= fom_count)
551 {
552 if (color_count > count)
553 return -1;
554
555 count -= color_count;
556 OUT_COPY_COUNT3(count, s, temp_s);
557 OUT_COLOR_COUNT3(color_count, s, last_pixel);
558 RESET_COUNTS;
559 }
560
561 color_count = 0;
562 }
563
564 if (!TEST_BICOLOR)
565 {
566 if (bicolor_count > 3 && bicolor_count >= fill_count &&
567 bicolor_count >= color_count && bicolor_count >= mix_count &&
568 bicolor_count >= fom_count)
569 {
570 if ((bicolor_count % 2) != 0)
571 bicolor_count--;
572
573 if (bicolor_count > count)
574 return -1;
575
576 count -= bicolor_count;
577 OUT_COPY_COUNT3(count, s, temp_s);
578 OUT_BICOLOR_COUNT3(bicolor_count, s, bicolor2, bicolor1);
579 RESET_COUNTS;
580 }
581
582 bicolor_count = 0;
583 bicolor1 = last_pixel;
584 bicolor2 = pixel;
585 bicolor_spin = FALSE;
586 }
587
588 if (!(TEST_FOM))
589 {
590 if (fom_count > 3 && fom_count >= fill_count && fom_count >= color_count &&
591 fom_count >= mix_count && fom_count >= bicolor_count)
592 {
593 if (fom_count > count)
594 return -1;
595
596 count -= fom_count;
597 OUT_COPY_COUNT3(count, s, temp_s);
598 OUT_FOM_COUNT3(fom_count, s, fom_mask, fom_mask_len);
599 RESET_COUNTS;
600 }
601
602 fom_count = 0;
603 fom_mask_len = 0;
604 }
605
606 if (TEST_FILL)
607 {
608 fill_count++;
609 }
610
611 if (TEST_MIX)
612 {
613 mix_count++;
614 }
615
616 if (TEST_COLOR)
617 {
618 color_count++;
619 }
620
621 if (TEST_BICOLOR)
622 {
623 bicolor_spin = !bicolor_spin;
624 bicolor_count++;
625 }
626
627 if (TEST_FOM)
628 {
629 if ((fom_count % 8) == 0)
630 {
631 fom_mask[fom_mask_len] = 0;
632 fom_mask_len++;
633 }
634
635 if (pixel == (ypixel ^ mix))
636 {
637 const int tmp = (1 << (fom_count % 8));
638 const int val = fom_mask[fom_mask_len - 1] | tmp;
639 const int8_t ival = WINPR_ASSERTING_INT_CAST(int8_t, val);
640 fom_mask[fom_mask_len - 1] = ival;
641 }
642
643 fom_count++;
644 }
645
646 Stream_Write_UINT8(temp_s, pixel & 0xff);
647 Stream_Write_UINT8(temp_s, (pixel >> 8) & 0xff);
648 Stream_Write_UINT8(temp_s, (pixel >> 16) & 0xff);
649 count++;
650 last_pixel = pixel;
651 last_ypixel = ypixel;
652 }
653
654 /* can't take fix, mix, or fom past first line */
655 if (last_line == 0)
656 {
657 if (fill_count > 3 && fill_count >= color_count && fill_count >= bicolor_count &&
658 fill_count >= mix_count && fill_count >= fom_count)
659 {
660 if (fill_count > count)
661 return -1;
662
663 count -= fill_count;
664 OUT_COPY_COUNT3(count, s, temp_s);
665 OUT_FILL_COUNT3(fill_count, s);
666 RESET_COUNTS;
667 }
668
669 fill_count = 0;
670
671 if (mix_count > 3 && mix_count >= fill_count && mix_count >= bicolor_count &&
672 mix_count >= color_count && mix_count >= fom_count)
673 {
674 if (mix_count > count)
675 return -1;
676
677 count -= mix_count;
678 OUT_COPY_COUNT3(count, s, temp_s);
679 OUT_MIX_COUNT3(mix_count, s);
680 RESET_COUNTS;
681 }
682
683 mix_count = 0;
684
685 if (fom_count > 3 && fom_count >= fill_count && fom_count >= color_count &&
686 fom_count >= mix_count && fom_count >= bicolor_count)
687 {
688 if (fom_count > count)
689 return -1;
690
691 count -= fom_count;
692 OUT_COPY_COUNT3(count, s, temp_s);
693 OUT_FOM_COUNT3(fom_count, s, fom_mask, fom_mask_len);
694 RESET_COUNTS;
695 }
696
697 fom_count = 0;
698 fom_mask_len = 0;
699 }
700
701 last_line = line;
702 line = line - 4ULL * width;
703 start_line--;
704 lines_sent++;
705 }
706
707 Stream_SetPosition(temp_s, 0);
708
709 if (fill_count > 3 && fill_count >= color_count && fill_count >= bicolor_count &&
710 fill_count >= mix_count && fill_count >= fom_count)
711 {
712 if (fill_count > count)
713 return -1;
714
715 count -= fill_count;
716 OUT_COPY_COUNT3(count, s, temp_s);
717 OUT_FILL_COUNT3(fill_count, s);
718 }
719 else if (mix_count > 3 && mix_count >= color_count && mix_count >= bicolor_count &&
720 mix_count >= fill_count && mix_count >= fom_count)
721 {
722 if (mix_count > count)
723 return -1;
724
725 count -= mix_count;
726 OUT_COPY_COUNT3(count, s, temp_s);
727 OUT_MIX_COUNT3(mix_count, s);
728 }
729 else if (color_count > 3 && color_count >= mix_count && color_count >= bicolor_count &&
730 color_count >= fill_count && color_count >= fom_count)
731 {
732 if (color_count > count)
733 return -1;
734
735 count -= color_count;
736 OUT_COPY_COUNT3(count, s, temp_s);
737 OUT_COLOR_COUNT3(color_count, s, last_pixel);
738 }
739 else if (bicolor_count > 3 && bicolor_count >= mix_count && bicolor_count >= color_count &&
740 bicolor_count >= fill_count && bicolor_count >= fom_count)
741 {
742 if ((bicolor_count % 2) != 0)
743 bicolor_count--;
744
745 if (bicolor_count > count)
746 return -1;
747
748 count -= bicolor_count;
749 OUT_COPY_COUNT3(count, s, temp_s);
750 OUT_BICOLOR_COUNT3(bicolor_count, s, bicolor2, bicolor1);
751
752 if (bicolor_count > count)
753 return -1;
754
755 count -= bicolor_count;
756 OUT_COPY_COUNT3(count, s, temp_s);
757 OUT_BICOLOR_COUNT3(bicolor_count, s, bicolor1, bicolor2);
758 }
759 else if (fom_count > 3 && fom_count >= mix_count && fom_count >= color_count &&
760 fom_count >= fill_count && fom_count >= bicolor_count)
761 {
762 if (fom_count > count)
763 return -1;
764
765 count -= fom_count;
766 OUT_COPY_COUNT3(count, s, temp_s);
767 OUT_FOM_COUNT3(fom_count, s, fom_mask, fom_mask_len);
768 }
769 else
770 {
771 OUT_COPY_COUNT3(count, s, temp_s);
772 }
773
774 return lines_sent;
775}
776
777static INLINE SSIZE_T freerdp_bitmap_compress_16(const void* WINPR_RESTRICT srcData, UINT32 width,
778 WINPR_ATTR_UNUSED UINT32 height,
779 wStream* WINPR_RESTRICT s, UINT32 bpp,
780 UINT32 byte_limit, UINT32 start_line,
781 wStream* WINPR_RESTRICT temp_s, UINT32 e)
782{
783 int8_t fom_mask[8192] = { 0 }; /* good for up to 64K bitmap */
784 SSIZE_T lines_sent = 0;
785 UINT16 count = 0;
786 UINT16 color_count = 0;
787 UINT16 last_pixel = 0;
788 UINT16 last_ypixel = 0;
789 UINT16 bicolor_count = 0;
790 UINT16 bicolor1 = 0;
791 UINT16 bicolor2 = 0;
792 BOOL bicolor_spin = FALSE;
793 UINT32 end = width + e;
794 UINT32 out_count = end * 2;
795 UINT16 fill_count = 0;
796 UINT16 mix_count = 0;
797 const UINT32 mix = (bpp == 15) ? 0xBA1F : 0xFFFF;
798 UINT16 fom_count = 0;
799 size_t fom_mask_len = 0;
800 const char* start = (const char*)srcData;
801 const char* line = start + 2ULL * width * start_line;
802 const char* last_line = NULL;
803
804 while ((line >= start) && (out_count < 32768))
805 {
806 size_t i = Stream_GetPosition(s) + 2ULL * count;
807
808 if ((i - (2ULL * color_count) >= byte_limit) &&
809 (i - (2ULL * bicolor_count) >= byte_limit) && (i - (2ULL * fill_count) >= byte_limit) &&
810 (i - (2ULL * mix_count) >= byte_limit) && (i - (2ULL * fom_count) >= byte_limit))
811 {
812 break;
813 }
814
815 out_count += end * 2;
816
817 for (UINT32 j = 0; j < end; j++)
818 {
819 /* read next pixel */
820 const UINT16 pixel = IN_PIXEL16(line, j, 0, width, last_pixel);
821 const UINT16 ypixel = IN_PIXEL16(last_line, j, 0, width, last_ypixel);
822
823 if (!TEST_FILL)
824 {
825 if (fill_count > 3 && fill_count >= color_count && fill_count >= bicolor_count &&
826 fill_count >= mix_count && fill_count >= fom_count)
827 {
828 if (fill_count > count)
829 return -1;
830
831 count -= fill_count;
832 OUT_COPY_COUNT2(count, s, temp_s);
833 OUT_FILL_COUNT2(fill_count, s);
834 RESET_COUNTS;
835 }
836
837 fill_count = 0;
838 }
839
840 if (!TEST_MIX)
841 {
842 if (mix_count > 3 && mix_count >= fill_count && mix_count >= bicolor_count &&
843 mix_count >= color_count && mix_count >= fom_count)
844 {
845 if (mix_count > count)
846 return -1;
847
848 count -= mix_count;
849 OUT_COPY_COUNT2(count, s, temp_s);
850 OUT_MIX_COUNT2(mix_count, s);
851 RESET_COUNTS;
852 }
853
854 mix_count = 0;
855 }
856
857 if (!(TEST_COLOR))
858 {
859 if (color_count > 3 && color_count >= fill_count && color_count >= bicolor_count &&
860 color_count >= mix_count && color_count >= fom_count)
861 {
862 if (color_count > count)
863 return -1;
864
865 count -= color_count;
866 OUT_COPY_COUNT2(count, s, temp_s);
867 OUT_COLOR_COUNT2(color_count, s, last_pixel);
868 RESET_COUNTS;
869 }
870
871 color_count = 0;
872 }
873
874 if (!TEST_BICOLOR)
875 {
876 if ((bicolor_count > 3) && (bicolor_count >= fill_count) &&
877 (bicolor_count >= color_count) && (bicolor_count >= mix_count) &&
878 (bicolor_count >= fom_count))
879 {
880 if ((bicolor_count % 2) != 0)
881 bicolor_count--;
882
883 if (bicolor_count > count)
884 return -1;
885
886 count -= bicolor_count;
887 OUT_COPY_COUNT2(count, s, temp_s);
888 OUT_BICOLOR_COUNT2(bicolor_count, s, bicolor2, bicolor1);
889 RESET_COUNTS;
890 }
891
892 bicolor_count = 0;
893 bicolor1 = last_pixel;
894 bicolor2 = pixel;
895 bicolor_spin = FALSE;
896 }
897
898 if (!(TEST_FOM))
899 {
900 if (fom_count > 3 && fom_count >= fill_count && fom_count >= color_count &&
901 fom_count >= mix_count && fom_count >= bicolor_count)
902 {
903 if (fom_count > count)
904 return -1;
905
906 count -= fom_count;
907 OUT_COPY_COUNT2(count, s, temp_s);
908 OUT_FOM_COUNT2(fom_count, s, fom_mask, fom_mask_len);
909 RESET_COUNTS;
910 }
911
912 fom_count = 0;
913 fom_mask_len = 0;
914 }
915
916 if (TEST_FILL)
917 {
918 fill_count++;
919 }
920
921 if (TEST_MIX)
922 {
923 mix_count++;
924 }
925
926 if (TEST_COLOR)
927 {
928 color_count++;
929 }
930
931 if (TEST_BICOLOR)
932 {
933 bicolor_spin = !bicolor_spin;
934 bicolor_count++;
935 }
936
937 if (TEST_FOM)
938 {
939 if ((fom_count % 8) == 0)
940 {
941 fom_mask[fom_mask_len] = 0;
942 fom_mask_len++;
943 }
944
945 if (pixel == (ypixel ^ mix))
946 {
947 const int tmp = (1 << (fom_count % 8));
948 const int val = fom_mask[fom_mask_len - 1] | tmp;
949 const int8_t ival = WINPR_ASSERTING_INT_CAST(int8_t, val);
950 fom_mask[fom_mask_len - 1] = ival;
951 }
952
953 fom_count++;
954 }
955
956 Stream_Write_UINT16(temp_s, pixel);
957 count++;
958 last_pixel = pixel;
959 last_ypixel = ypixel;
960 }
961
962 /* can't take fix, mix, or fom past first line */
963 if (last_line == 0)
964 {
965 if (fill_count > 3 && fill_count >= color_count && fill_count >= bicolor_count &&
966 fill_count >= mix_count && fill_count >= fom_count)
967 {
968 if (fill_count > count)
969 return -1;
970
971 count -= fill_count;
972 OUT_COPY_COUNT2(count, s, temp_s);
973 OUT_FILL_COUNT2(fill_count, s);
974 RESET_COUNTS;
975 }
976
977 fill_count = 0;
978
979 if (mix_count > 3 && mix_count >= fill_count && mix_count >= bicolor_count &&
980 mix_count >= color_count && mix_count >= fom_count)
981 {
982 if (mix_count > count)
983 return -1;
984
985 count -= mix_count;
986 OUT_COPY_COUNT2(count, s, temp_s);
987 OUT_MIX_COUNT2(mix_count, s);
988 RESET_COUNTS;
989 }
990
991 mix_count = 0;
992
993 if (fom_count > 3 && fom_count >= fill_count && fom_count >= color_count &&
994 fom_count >= mix_count && fom_count >= bicolor_count)
995 {
996 if (fom_count > count)
997 return -1;
998
999 count -= fom_count;
1000 OUT_COPY_COUNT2(count, s, temp_s);
1001 OUT_FOM_COUNT2(fom_count, s, fom_mask, fom_mask_len);
1002 RESET_COUNTS;
1003 }
1004
1005 fom_count = 0;
1006 fom_mask_len = 0;
1007 }
1008
1009 last_line = line;
1010 line = line - 2ULL * width;
1011 start_line--;
1012 lines_sent++;
1013 }
1014
1015 Stream_SetPosition(temp_s, 0);
1016
1017 if (fill_count > 3 && fill_count >= color_count && fill_count >= bicolor_count &&
1018 fill_count >= mix_count && fill_count >= fom_count)
1019 {
1020 if (fill_count > count)
1021 return -1;
1022
1023 count -= fill_count;
1024 OUT_COPY_COUNT2(count, s, temp_s);
1025 OUT_FILL_COUNT2(fill_count, s);
1026 }
1027 else if (mix_count > 3 && mix_count >= color_count && mix_count >= bicolor_count &&
1028 mix_count >= fill_count && mix_count >= fom_count)
1029 {
1030 if (mix_count > count)
1031 return -1;
1032
1033 count -= mix_count;
1034 OUT_COPY_COUNT2(count, s, temp_s);
1035 OUT_MIX_COUNT2(mix_count, s);
1036 }
1037 else if (color_count > 3 && color_count >= mix_count && color_count >= bicolor_count &&
1038 color_count >= fill_count && color_count >= fom_count)
1039 {
1040 if (color_count > count)
1041 return -1;
1042
1043 count -= color_count;
1044 OUT_COPY_COUNT2(count, s, temp_s);
1045 OUT_COLOR_COUNT2(color_count, s, last_pixel);
1046 }
1047 else if (bicolor_count > 3 && bicolor_count >= mix_count && bicolor_count >= color_count &&
1048 bicolor_count >= fill_count && bicolor_count >= fom_count)
1049 {
1050 if ((bicolor_count % 2) != 0)
1051 bicolor_count--;
1052
1053 if (bicolor_count > count)
1054 return -1;
1055
1056 count -= bicolor_count;
1057 OUT_COPY_COUNT2(count, s, temp_s);
1058 OUT_BICOLOR_COUNT2(bicolor_count, s, bicolor2, bicolor1);
1059
1060 if (bicolor_count > count)
1061 return -1;
1062
1063 count -= bicolor_count;
1064 OUT_COPY_COUNT2(count, s, temp_s);
1065 OUT_BICOLOR_COUNT2(bicolor_count, s, bicolor1, bicolor2);
1066 }
1067 else if (fom_count > 3 && fom_count >= mix_count && fom_count >= color_count &&
1068 fom_count >= fill_count && fom_count >= bicolor_count)
1069 {
1070 if (fom_count > count)
1071 return -1;
1072
1073 count -= fom_count;
1074 OUT_COPY_COUNT2(count, s, temp_s);
1075 OUT_FOM_COUNT2(fom_count, s, fom_mask, fom_mask_len);
1076 }
1077 else
1078 {
1079 OUT_COPY_COUNT2(count, s, temp_s);
1080 }
1081
1082 return lines_sent;
1083}
1084
1085SSIZE_T freerdp_bitmap_compress(const void* WINPR_RESTRICT srcData, UINT32 width, UINT32 height,
1086 wStream* WINPR_RESTRICT s, UINT32 bpp, UINT32 byte_limit,
1087 UINT32 start_line, wStream* WINPR_RESTRICT temp_s, UINT32 e)
1088{
1089 Stream_SetPosition(temp_s, 0);
1090
1091 switch (bpp)
1092 {
1093 case 15:
1094 case 16:
1095 return freerdp_bitmap_compress_16(srcData, width, height, s, bpp, byte_limit,
1096 start_line, temp_s, e);
1097
1098 case 24:
1099 return freerdp_bitmap_compress_24(srcData, width, height, s, byte_limit, start_line,
1100 temp_s, e);
1101
1102 default:
1103 return -1;
1104 }
1105}