20#include <winpr/assert.h>
21#include <winpr/cast.h>
23#include <freerdp/config.h>
25#include <freerdp/codec/bitmap.h>
26#include <freerdp/codec/planar.h>
30 ALIGN64 UINT16 bicolor_count;
31 ALIGN64 UINT16 fill_count;
32 ALIGN64 UINT16 color_count;
33 ALIGN64 UINT16 mix_count;
34 ALIGN64 UINT16 fom_count;
35 ALIGN64
size_t fom_mask_len;
36 ALIGN64 BOOL bicolor_spin;
39static inline void reset_counts(
struct count* counts)
41 const struct count empty = { 0 };
46static inline UINT16 GETPIXEL16(
const void* WINPR_RESTRICT d, UINT32 x, UINT32 y, UINT32 w)
48 const BYTE* WINPR_RESTRICT src = (
const BYTE*)d + ((y * w + x) *
sizeof(UINT16));
49 return WINPR_ASSERTING_INT_CAST(UINT16, ((UINT16)src[1] << 8) | (UINT16)src[0]);
52static inline UINT32 GETPIXEL32(
const void* WINPR_RESTRICT d, UINT32 x, UINT32 y, UINT32 w)
54 const BYTE* WINPR_RESTRICT src = (
const BYTE*)d + ((y * w + x) *
sizeof(UINT32));
55 return (((UINT32)src[3]) << 24) | (((UINT32)src[2]) << 16) | (((UINT32)src[1]) << 8) |
60static inline UINT16 IN_PIXEL16(
const void* WINPR_RESTRICT in_ptr, UINT32 in_x, UINT32 in_y,
61 UINT32 in_w, UINT16 in_last_pixel)
66 return GETPIXEL16(in_ptr, in_x, in_y, in_w);
72static inline UINT32 IN_PIXEL32(
const void* WINPR_RESTRICT in_ptr, UINT32 in_x, UINT32 in_y,
73 UINT32 in_w, UINT32 in_last_pixel)
78 return GETPIXEL32(in_ptr, in_x, in_y, in_w);
85static inline UINT16 out_color_count_2(UINT16 in_count,
wStream* WINPR_RESTRICT in_s,
92 const BYTE temp = ((0x3 << 5) | in_count) & 0xFF;
93 Stream_Write_UINT8(in_s, temp);
95 else if (in_count < 256 + 32)
97 const BYTE temp = (in_count - 32) & 0xFF;
98 Stream_Write_UINT8(in_s, 0x60);
99 Stream_Write_UINT8(in_s, temp);
103 Stream_Write_UINT8(in_s, 0xf3);
104 Stream_Write_UINT16(in_s, in_count);
107 Stream_Write_UINT16(in_s, in_data);
115static inline UINT16 out_color_count_3(UINT16 in_count,
wStream* WINPR_RESTRICT in_s,
122 const BYTE temp = ((0x3 << 5) | in_count) & 0xFF;
123 Stream_Write_UINT8(in_s, temp);
125 else if (in_count < 256 + 32)
127 const BYTE temp = (in_count - 32) & 0xFF;
128 Stream_Write_UINT8(in_s, 0x60);
129 Stream_Write_UINT8(in_s, temp);
133 Stream_Write_UINT8(in_s, 0xf3);
134 Stream_Write_UINT16(in_s, in_count);
137 Stream_Write_UINT8(in_s, in_data & 0xFF);
139 Stream_Write_UINT8(in_s, (in_data >> 8) & 0xFF);
140 Stream_Write_UINT8(in_s, (in_data >> 16) & 0xFF);
148static inline UINT16 out_copy_count_2(UINT16 in_count,
wStream* WINPR_RESTRICT in_s,
149 wStream* WINPR_RESTRICT in_data)
156 const BYTE temp = ((0x4 << 5) | in_count) & 0xFF;
157 Stream_Write_UINT8(in_s, temp);
159 else if (in_count < 256 + 32)
161 const BYTE temp = (in_count - 32) & 0xFF;
162 Stream_Write_UINT8(in_s, 0x80);
163 Stream_Write_UINT8(in_s, temp);
167 Stream_Write_UINT8(in_s, 0xf4);
168 Stream_Write_UINT16(in_s, in_count);
171 Stream_Write(in_s, Stream_Buffer(in_data), 2ULL * in_count);
174 Stream_SetPosition(in_data, 0);
180static inline UINT16 out_copy_count_3(UINT16 in_count,
wStream* WINPR_RESTRICT in_s,
181 wStream* WINPR_RESTRICT in_data)
187 const BYTE temp = ((0x4 << 5) | in_count) & 0xFF;
188 Stream_Write_UINT8(in_s, temp);
190 else if (in_count < 256 + 32)
192 const BYTE temp = (in_count - 32) & 0xFF;
193 Stream_Write_UINT8(in_s, 0x80);
194 Stream_Write_UINT8(in_s, temp);
198 Stream_Write_UINT8(in_s, 0xf4);
199 Stream_Write_UINT16(in_s, in_count);
202 Stream_Write(in_s, Stream_Pointer(in_data), 3ULL * in_count);
205 Stream_SetPosition(in_data, 0);
211static inline UINT16 out_bicolor_count_2(UINT16 in_count,
wStream* WINPR_RESTRICT in_s,
212 UINT16 in_color1, UINT16 in_color2)
216 if (in_count / 2 < 16)
218 const BYTE temp = ((0xe << 4) | (in_count / 2)) & 0xFF;
219 Stream_Write_UINT8(in_s, temp);
221 else if (in_count / 2 < 256 + 16)
223 const BYTE temp = (in_count / 2 - 16) & 0xFF;
224 Stream_Write_UINT8(in_s, 0xe0);
225 Stream_Write_UINT8(in_s, temp);
229 Stream_Write_UINT8(in_s, 0xf8);
230 Stream_Write_UINT16(in_s, in_count / 2);
233 Stream_Write_UINT16(in_s, in_color1);
234 Stream_Write_UINT16(in_s, in_color2);
242static inline UINT16 out_bicolor_count_3(UINT16 in_count,
wStream* WINPR_RESTRICT in_s,
243 UINT32 in_color1, UINT32 in_color2)
247 if (in_count / 2 < 16)
249 const BYTE temp = ((0xe << 4) | (in_count / 2)) & 0xFF;
250 Stream_Write_UINT8(in_s, temp);
252 else if (in_count / 2 < 256 + 16)
254 const BYTE temp = (in_count / 2 - 16) & 0xFF;
255 Stream_Write_UINT8(in_s, 0xe0);
256 Stream_Write_UINT8(in_s, temp);
260 Stream_Write_UINT8(in_s, 0xf8);
261 Stream_Write_UINT16(in_s, in_count / 2);
264 Stream_Write_UINT8(in_s, in_color1 & 0xFF);
265 Stream_Write_UINT8(in_s, (in_color1 >> 8) & 0xFF);
266 Stream_Write_UINT8(in_s, (in_color1 >> 16) & 0xFF);
267 Stream_Write_UINT8(in_s, in_color2 & 0xFF);
268 Stream_Write_UINT8(in_s, (in_color2 >> 8) & 0xFF);
269 Stream_Write_UINT8(in_s, (in_color2 >> 16) & 0xFF);
277static inline UINT16 out_fill_count_2(UINT16 in_count,
wStream* WINPR_RESTRICT in_s)
283 Stream_Write_UINT8(in_s, in_count & 0xFF);
285 else if (in_count < 256 + 32)
287 const BYTE temp = (in_count - 32) & 0xFF;
288 Stream_Write_UINT8(in_s, 0x0);
289 Stream_Write_UINT8(in_s, temp);
293 Stream_Write_UINT8(in_s, 0xf0);
294 Stream_Write_UINT16(in_s, in_count);
303static inline UINT16 out_fill_count_3(UINT16 in_count,
wStream* WINPR_RESTRICT in_s)
309 Stream_Write_UINT8(in_s, in_count & 0xFF);
311 else if (in_count < 256 + 32)
313 const BYTE temp = (in_count - 32) & 0xFF;
314 Stream_Write_UINT8(in_s, 0x0);
315 Stream_Write_UINT8(in_s, temp);
319 Stream_Write_UINT8(in_s, 0xf0);
320 Stream_Write_UINT16(in_s, in_count);
329static inline UINT16 out_counts_mix_count_2(UINT16 in_count,
wStream* WINPR_RESTRICT in_s)
335 const BYTE temp = ((0x1 << 5) | in_count) & 0xFF;
336 Stream_Write_UINT8(in_s, temp);
338 else if (in_count < 256 + 32)
340 const BYTE temp = (in_count - 32) & 0xFF;
341 Stream_Write_UINT8(in_s, 0x20);
342 Stream_Write_UINT8(in_s, temp);
346 Stream_Write_UINT8(in_s, 0xf1);
347 Stream_Write_UINT16(in_s, in_count);
356static inline UINT16 out_counts_mix_count_3(UINT16 in_count,
wStream* WINPR_RESTRICT in_s)
362 const BYTE temp = ((0x1 << 5) | in_count) & 0xFF;
363 Stream_Write_UINT8(in_s, temp);
365 else if (in_count < 256 + 32)
367 const BYTE temp = (in_count - 32) & 0xFF;
368 Stream_Write_UINT8(in_s, 0x20);
369 Stream_Write_UINT8(in_s, temp);
373 Stream_Write_UINT8(in_s, 0xf1);
374 Stream_Write_UINT16(in_s, in_count);
383static inline UINT16 out_from_count_2(UINT16 in_count,
wStream* WINPR_RESTRICT in_s,
384 const uint8_t* WINPR_RESTRICT in_mask,
size_t in_mask_len)
388 if ((in_count % 8) == 0 && in_count < 249)
390 const BYTE temp = ((0x2 << 5) | (in_count / 8)) & 0xFF;
391 Stream_Write_UINT8(in_s, temp);
393 else if (in_count < 256)
395 const BYTE temp = (in_count - 1) & 0xFF;
396 Stream_Write_UINT8(in_s, 0x40);
397 Stream_Write_UINT8(in_s, temp);
401 Stream_Write_UINT8(in_s, 0xf2);
402 Stream_Write_UINT16(in_s, in_count);
405 Stream_Write(in_s, in_mask, in_mask_len);
413static inline UINT16 out_from_count_3(UINT16 in_count,
wStream* WINPR_RESTRICT in_s,
414 const uint8_t* WINPR_RESTRICT in_mask,
size_t in_mask_len)
418 if ((in_count % 8) == 0 && in_count < 249)
420 const BYTE temp = ((0x2 << 5) | (in_count / 8)) & 0xFF;
421 Stream_Write_UINT8(in_s, temp);
423 else if (in_count < 256)
425 const BYTE temp = (in_count - 1) & 0xFF;
426 Stream_Write_UINT8(in_s, 0x40);
427 Stream_Write_UINT8(in_s, temp);
431 Stream_Write_UINT8(in_s, 0xf2);
432 Stream_Write_UINT16(in_s, in_count);
435 Stream_Write(in_s, in_mask, in_mask_len);
441#define TEST_FILL ((last_line == 0 && pixel == 0) || (last_line != 0 && pixel == ypixel))
442#define TEST_MIX ((last_line == 0 && pixel == mix) || (last_line != 0 && pixel == (ypixel ^ mix)))
443#define TEST_FOM TEST_FILL || TEST_MIX
444#define TEST_COLOR pixel == last_pixel
446#define test_bicolor(counts) \
447 ((pixel != last_pixel) && \
448 ((!(counts)->bicolor_spin && (pixel == bicolor1) && (last_pixel == bicolor2)) || \
449 ((counts)->bicolor_spin && (pixel == bicolor2) && (last_pixel == bicolor1))))
451static inline SSIZE_T freerdp_bitmap_compress_24(
const void* WINPR_RESTRICT srcData, UINT32 width,
452 WINPR_ATTR_UNUSED UINT32 height,
453 wStream* WINPR_RESTRICT s, UINT32 byte_limit,
454 UINT32 start_line,
wStream* WINPR_RESTRICT temp_s,
457 uint8_t fom_mask[8192] = { 0 };
458 SSIZE_T lines_sent = 0;
460 UINT32 last_pixel = 0;
461 UINT32 last_ypixel = 0;
462 struct count counts = { 0 };
465 UINT32 end = width + e;
466 UINT32 out_count = end * 3;
467 const UINT32 mix = 0xFFFFFF;
468 const char* start = (
const char*)srcData;
469 const char* line = start + 4ULL * width * start_line;
470 const char* last_line = NULL;
472 while ((line >= start) && (out_count < 32768))
474 size_t i = Stream_GetPosition(s) + 3ULL * count;
476 if ((i - (3ULL * counts.color_count) >= byte_limit) &&
477 (i - (3ULL * counts.bicolor_count) >= byte_limit) &&
478 (i - (3ULL * counts.fill_count) >= byte_limit) &&
479 (i - (3ULL * counts.mix_count) >= byte_limit) &&
480 (i - (3ULL * counts.fom_count) >= byte_limit))
485 out_count += end * 3;
487 for (UINT32 j = 0; j < end; j++)
490 const UINT32 pixel = IN_PIXEL32(line, j, 0, width, last_pixel);
491 const UINT32 ypixel = IN_PIXEL32(last_line, j, 0, width, last_ypixel);
495 if (counts.fill_count > 3 && counts.fill_count >= counts.color_count &&
496 counts.fill_count >= counts.bicolor_count &&
497 counts.fill_count >= counts.mix_count && counts.fill_count >= counts.fom_count)
499 if (counts.fill_count > count)
502 count -= counts.fill_count;
503 count = out_copy_count_3(count, s, temp_s);
504 counts.fill_count = out_fill_count_3(counts.fill_count, s);
505 reset_counts(&counts);
508 counts.fill_count = 0;
513 if (counts.mix_count > 3 && counts.mix_count >= counts.fill_count &&
514 counts.mix_count >= counts.bicolor_count &&
515 counts.mix_count >= counts.color_count && counts.mix_count >= counts.fom_count)
517 if (counts.mix_count > count)
520 count -= counts.mix_count;
521 count = out_copy_count_3(count, s, temp_s);
522 counts.mix_count = out_counts_mix_count_3(counts.mix_count, s);
523 reset_counts(&counts);
526 counts.mix_count = 0;
531 if (counts.color_count > 3 && counts.color_count >= counts.fill_count &&
532 counts.color_count >= counts.bicolor_count &&
533 counts.color_count >= counts.mix_count &&
534 counts.color_count >= counts.fom_count)
536 if (counts.color_count > count)
539 count -= counts.color_count;
540 count = out_copy_count_3(count, s, temp_s);
541 counts.color_count = out_color_count_3(counts.color_count, s, last_pixel);
542 reset_counts(&counts);
545 counts.color_count = 0;
548 if (!test_bicolor(&counts))
550 if (counts.bicolor_count > 3 && counts.bicolor_count >= counts.fill_count &&
551 counts.bicolor_count >= counts.color_count &&
552 counts.bicolor_count >= counts.mix_count &&
553 counts.bicolor_count >= counts.fom_count)
555 if ((counts.bicolor_count % 2) != 0)
556 counts.bicolor_count--;
558 if (counts.bicolor_count > count)
561 count -= counts.bicolor_count;
562 count = out_copy_count_3(count, s, temp_s);
563 counts.bicolor_count =
564 out_bicolor_count_3(counts.bicolor_count, s, bicolor2, bicolor1);
565 reset_counts(&counts);
568 counts.bicolor_count = 0;
569 bicolor1 = last_pixel;
571 counts.bicolor_spin = FALSE;
576 if (counts.fom_count > 3 && counts.fom_count >= counts.fill_count &&
577 counts.fom_count >= counts.color_count &&
578 counts.fom_count >= counts.mix_count &&
579 counts.fom_count >= counts.bicolor_count)
581 if (counts.fom_count > count)
584 count -= counts.fom_count;
585 count = out_copy_count_3(count, s, temp_s);
587 out_from_count_3(counts.fom_count, s, fom_mask, counts.fom_mask_len);
588 reset_counts(&counts);
591 counts.fom_count = 0;
592 counts.fom_mask_len = 0;
607 counts.color_count++;
610 if (test_bicolor(&counts))
612 counts.bicolor_spin = !counts.bicolor_spin;
613 counts.bicolor_count++;
618 if ((counts.fom_count % 8) == 0)
620 fom_mask[counts.fom_mask_len] = 0;
621 counts.fom_mask_len++;
624 if (pixel == (ypixel ^ mix))
626 const uint8_t tmp = (1 << (counts.fom_count % 8)) & 0xFF;
627 const uint8_t val = fom_mask[counts.fom_mask_len - 1] | tmp;
628 fom_mask[counts.fom_mask_len - 1] = val;
634 Stream_Write_UINT8(temp_s, pixel & 0xff);
635 Stream_Write_UINT8(temp_s, (pixel >> 8) & 0xff);
636 Stream_Write_UINT8(temp_s, (pixel >> 16) & 0xff);
639 last_ypixel = ypixel;
645 if (counts.fill_count > 3 && counts.fill_count >= counts.color_count &&
646 counts.fill_count >= counts.bicolor_count &&
647 counts.fill_count >= counts.mix_count && counts.fill_count >= counts.fom_count)
649 if (counts.fill_count > count)
652 count -= counts.fill_count;
653 count = out_copy_count_3(count, s, temp_s);
654 counts.fill_count = out_fill_count_3(counts.fill_count, s);
655 reset_counts(&counts);
658 counts.fill_count = 0;
660 if (counts.mix_count > 3 && counts.mix_count >= counts.fill_count &&
661 counts.mix_count >= counts.bicolor_count &&
662 counts.mix_count >= counts.color_count && counts.mix_count >= counts.fom_count)
664 if (counts.mix_count > count)
667 count -= counts.mix_count;
668 count = out_copy_count_3(count, s, temp_s);
669 counts.mix_count = out_counts_mix_count_3(counts.mix_count, s);
670 reset_counts(&counts);
673 counts.mix_count = 0;
675 if (counts.fom_count > 3 && counts.fom_count >= counts.fill_count &&
676 counts.fom_count >= counts.color_count && counts.fom_count >= counts.mix_count &&
677 counts.fom_count >= counts.bicolor_count)
679 if (counts.fom_count > count)
682 count -= counts.fom_count;
683 count = out_copy_count_3(count, s, temp_s);
685 out_from_count_3(counts.fom_count, s, fom_mask, counts.fom_mask_len);
686 reset_counts(&counts);
689 counts.fom_count = 0;
690 counts.fom_mask_len = 0;
694 line = line - 4ULL * width;
699 Stream_SetPosition(temp_s, 0);
701 if (counts.fill_count > 3 && counts.fill_count >= counts.color_count &&
702 counts.fill_count >= counts.bicolor_count && counts.fill_count >= counts.mix_count &&
703 counts.fill_count >= counts.fom_count)
705 if (counts.fill_count > count)
708 count -= counts.fill_count;
709 (void)out_copy_count_3(count, s, temp_s);
710 counts.fill_count = out_fill_count_3(counts.fill_count, s);
712 else if (counts.mix_count > 3 && counts.mix_count >= counts.color_count &&
713 counts.mix_count >= counts.bicolor_count && counts.mix_count >= counts.fill_count &&
714 counts.mix_count >= counts.fom_count)
716 if (counts.mix_count > count)
719 count -= counts.mix_count;
720 (void)out_copy_count_3(count, s, temp_s);
721 counts.mix_count = out_counts_mix_count_3(counts.mix_count, s);
723 else if (counts.color_count > 3 && counts.color_count >= counts.mix_count &&
724 counts.color_count >= counts.bicolor_count &&
725 counts.color_count >= counts.fill_count && counts.color_count >= counts.fom_count)
727 if (counts.color_count > count)
730 count -= counts.color_count;
731 (void)out_copy_count_3(count, s, temp_s);
732 counts.color_count = out_color_count_3(counts.color_count, s, last_pixel);
734 else if (counts.bicolor_count > 3 && counts.bicolor_count >= counts.mix_count &&
735 counts.bicolor_count >= counts.color_count &&
736 counts.bicolor_count >= counts.fill_count && counts.bicolor_count >= counts.fom_count)
738 if ((counts.bicolor_count % 2) != 0)
739 counts.bicolor_count--;
741 if (counts.bicolor_count > count)
744 count -= counts.bicolor_count;
745 count = out_copy_count_3(count, s, temp_s);
746 counts.bicolor_count = out_bicolor_count_3(counts.bicolor_count, s, bicolor2, bicolor1);
748 if (counts.bicolor_count > count)
751 count -= counts.bicolor_count;
752 (void)out_copy_count_3(count, s, temp_s);
753 counts.bicolor_count = out_bicolor_count_3(counts.bicolor_count, s, bicolor1, bicolor2);
755 else if (counts.fom_count > 3 && counts.fom_count >= counts.mix_count &&
756 counts.fom_count >= counts.color_count && counts.fom_count >= counts.fill_count &&
757 counts.fom_count >= counts.bicolor_count)
759 if (counts.fom_count > count)
762 count -= counts.fom_count;
763 (void)out_copy_count_3(count, s, temp_s);
764 counts.fom_count = out_from_count_3(counts.fom_count, s, fom_mask, counts.fom_mask_len);
768 (void)out_copy_count_3(count, s, temp_s);
774static inline SSIZE_T freerdp_bitmap_compress_16(
const void* WINPR_RESTRICT srcData, UINT32 width,
775 WINPR_ATTR_UNUSED UINT32 height,
776 wStream* WINPR_RESTRICT s, UINT32 bpp,
777 UINT32 byte_limit, UINT32 start_line,
778 wStream* WINPR_RESTRICT temp_s, UINT32 e)
780 uint8_t fom_mask[8192] = { 0 };
781 SSIZE_T lines_sent = 0;
783 UINT16 last_pixel = 0;
784 UINT16 last_ypixel = 0;
785 struct count counts = { 0 };
788 UINT32 end = width + e;
789 UINT32 out_count = end * 2;
790 const UINT32 mix = (bpp == 15) ? 0xBA1F : 0xFFFF;
791 const char* start = (
const char*)srcData;
792 const char* line = start + 2ULL * width * start_line;
793 const char* last_line = NULL;
795 while ((line >= start) && (out_count < 32768))
797 size_t i = Stream_GetPosition(s) + 2ULL * count;
799 if ((i - (2ULL * counts.color_count) >= byte_limit) &&
800 (i - (2ULL * counts.bicolor_count) >= byte_limit) &&
801 (i - (2ULL * counts.fill_count) >= byte_limit) &&
802 (i - (2ULL * counts.mix_count) >= byte_limit) &&
803 (i - (2ULL * counts.fom_count) >= byte_limit))
808 out_count += end * 2;
810 for (UINT32 j = 0; j < end; j++)
813 const UINT16 pixel = IN_PIXEL16(line, j, 0, width, last_pixel);
814 const UINT16 ypixel = IN_PIXEL16(last_line, j, 0, width, last_ypixel);
818 if (counts.fill_count > 3 && counts.fill_count >= counts.color_count &&
819 counts.fill_count >= counts.bicolor_count &&
820 counts.fill_count >= counts.mix_count && counts.fill_count >= counts.fom_count)
822 if (counts.fill_count > count)
825 count -= counts.fill_count;
826 count = out_copy_count_2(count, s, temp_s);
827 counts.fill_count = out_fill_count_2(counts.fill_count, s);
828 reset_counts(&counts);
831 counts.fill_count = 0;
836 if (counts.mix_count > 3 && counts.mix_count >= counts.fill_count &&
837 counts.mix_count >= counts.bicolor_count &&
838 counts.mix_count >= counts.color_count && counts.mix_count >= counts.fom_count)
840 if (counts.mix_count > count)
843 count -= counts.mix_count;
844 count = out_copy_count_2(count, s, temp_s);
845 counts.mix_count = out_counts_mix_count_2(counts.mix_count, s);
846 reset_counts(&counts);
849 counts.mix_count = 0;
854 if (counts.color_count > 3 && counts.color_count >= counts.fill_count &&
855 counts.color_count >= counts.bicolor_count &&
856 counts.color_count >= counts.mix_count &&
857 counts.color_count >= counts.fom_count)
859 if (counts.color_count > count)
862 count -= counts.color_count;
863 count = out_copy_count_2(count, s, temp_s);
864 counts.color_count = out_color_count_2(counts.color_count, s, last_pixel);
865 reset_counts(&counts);
868 counts.color_count = 0;
871 if (!test_bicolor(&counts))
873 if ((counts.bicolor_count > 3) && (counts.bicolor_count >= counts.fill_count) &&
874 (counts.bicolor_count >= counts.color_count) &&
875 (counts.bicolor_count >= counts.mix_count) &&
876 (counts.bicolor_count >= counts.fom_count))
878 if ((counts.bicolor_count % 2) != 0)
879 counts.bicolor_count--;
881 if (counts.bicolor_count > count)
884 count -= counts.bicolor_count;
885 count = out_copy_count_2(count, s, temp_s);
886 counts.bicolor_count =
887 out_bicolor_count_2(counts.bicolor_count, s, bicolor2, bicolor1);
888 reset_counts(&counts);
891 counts.bicolor_count = 0;
892 bicolor1 = last_pixel;
894 counts.bicolor_spin = FALSE;
899 if (counts.fom_count > 3 && counts.fom_count >= counts.fill_count &&
900 counts.fom_count >= counts.color_count &&
901 counts.fom_count >= counts.mix_count &&
902 counts.fom_count >= counts.bicolor_count)
904 if (counts.fom_count > count)
907 count -= counts.fom_count;
908 count = out_copy_count_2(count, s, temp_s);
910 out_from_count_2(counts.fom_count, s, fom_mask, counts.fom_mask_len);
911 reset_counts(&counts);
914 counts.fom_count = 0;
915 counts.fom_mask_len = 0;
930 counts.color_count++;
933 if (test_bicolor(&counts))
935 counts.bicolor_spin = !counts.bicolor_spin;
936 counts.bicolor_count++;
941 if ((counts.fom_count % 8) == 0)
943 fom_mask[counts.fom_mask_len] = 0;
944 counts.fom_mask_len++;
947 if (pixel == (ypixel ^ mix))
949 const uint8_t tmp = (1 << (counts.fom_count % 8)) & 0xFF;
950 const uint8_t val = fom_mask[counts.fom_mask_len - 1] | tmp;
951 fom_mask[counts.fom_mask_len - 1] = val;
957 Stream_Write_UINT16(temp_s, pixel);
960 last_ypixel = ypixel;
966 if (counts.fill_count > 3 && counts.fill_count >= counts.color_count &&
967 counts.fill_count >= counts.bicolor_count &&
968 counts.fill_count >= counts.mix_count && counts.fill_count >= counts.fom_count)
970 if (counts.fill_count > count)
973 count -= counts.fill_count;
974 count = out_copy_count_2(count, s, temp_s);
975 counts.fill_count = out_fill_count_2(counts.fill_count, s);
976 reset_counts(&counts);
979 counts.fill_count = 0;
981 if (counts.mix_count > 3 && counts.mix_count >= counts.fill_count &&
982 counts.mix_count >= counts.bicolor_count &&
983 counts.mix_count >= counts.color_count && counts.mix_count >= counts.fom_count)
985 if (counts.mix_count > count)
988 count -= counts.mix_count;
989 count = out_copy_count_2(count, s, temp_s);
990 counts.mix_count = out_counts_mix_count_2(counts.mix_count, s);
991 reset_counts(&counts);
994 counts.mix_count = 0;
996 if (counts.fom_count > 3 && counts.fom_count >= counts.fill_count &&
997 counts.fom_count >= counts.color_count && counts.fom_count >= counts.mix_count &&
998 counts.fom_count >= counts.bicolor_count)
1000 if (counts.fom_count > count)
1003 count -= counts.fom_count;
1004 count = out_copy_count_2(count, s, temp_s);
1006 out_from_count_2(counts.fom_count, s, fom_mask, counts.fom_mask_len);
1007 reset_counts(&counts);
1010 counts.fom_count = 0;
1011 counts.fom_mask_len = 0;
1015 line = line - 2ULL * width;
1020 Stream_SetPosition(temp_s, 0);
1022 if (counts.fill_count > 3 && counts.fill_count >= counts.color_count &&
1023 counts.fill_count >= counts.bicolor_count && counts.fill_count >= counts.mix_count &&
1024 counts.fill_count >= counts.fom_count)
1026 if (counts.fill_count > count)
1029 count -= counts.fill_count;
1030 (void)out_copy_count_2(count, s, temp_s);
1031 counts.fill_count = out_fill_count_2(counts.fill_count, s);
1033 else if (counts.mix_count > 3 && counts.mix_count >= counts.color_count &&
1034 counts.mix_count >= counts.bicolor_count && counts.mix_count >= counts.fill_count &&
1035 counts.mix_count >= counts.fom_count)
1037 if (counts.mix_count > count)
1040 count -= counts.mix_count;
1041 (void)out_copy_count_2(count, s, temp_s);
1042 counts.mix_count = out_counts_mix_count_2(counts.mix_count, s);
1044 else if (counts.color_count > 3 && counts.color_count >= counts.mix_count &&
1045 counts.color_count >= counts.bicolor_count &&
1046 counts.color_count >= counts.fill_count && counts.color_count >= counts.fom_count)
1048 if (counts.color_count > count)
1051 count -= counts.color_count;
1052 (void)out_copy_count_2(count, s, temp_s);
1053 counts.color_count = out_color_count_2(counts.color_count, s, last_pixel);
1055 else if (counts.bicolor_count > 3 && counts.bicolor_count >= counts.mix_count &&
1056 counts.bicolor_count >= counts.color_count &&
1057 counts.bicolor_count >= counts.fill_count && counts.bicolor_count >= counts.fom_count)
1059 if ((counts.bicolor_count % 2) != 0)
1060 counts.bicolor_count--;
1062 if (counts.bicolor_count > count)
1065 count -= counts.bicolor_count;
1066 count = out_copy_count_2(count, s, temp_s);
1067 counts.bicolor_count = out_bicolor_count_2(counts.bicolor_count, s, bicolor2, bicolor1);
1069 if (counts.bicolor_count > count)
1072 count -= counts.bicolor_count;
1073 (void)out_copy_count_2(count, s, temp_s);
1074 counts.bicolor_count = out_bicolor_count_2(counts.bicolor_count, s, bicolor1, bicolor2);
1076 else if (counts.fom_count > 3 && counts.fom_count >= counts.mix_count &&
1077 counts.fom_count >= counts.color_count && counts.fom_count >= counts.fill_count &&
1078 counts.fom_count >= counts.bicolor_count)
1080 if (counts.fom_count > count)
1083 count -= counts.fom_count;
1084 (void)out_copy_count_2(count, s, temp_s);
1085 counts.fom_count = out_from_count_2(counts.fom_count, s, fom_mask, counts.fom_mask_len);
1089 (void)out_copy_count_2(count, s, temp_s);
1095SSIZE_T freerdp_bitmap_compress(
const void* WINPR_RESTRICT srcData, UINT32 width, UINT32 height,
1096 wStream* WINPR_RESTRICT s, UINT32 bpp, UINT32 byte_limit,
1097 UINT32 start_line,
wStream* WINPR_RESTRICT temp_s, UINT32 e)
1099 Stream_SetPosition(temp_s, 0);
1105 return freerdp_bitmap_compress_16(srcData, width, height, s, bpp, byte_limit,
1106 start_line, temp_s, e);
1109 return freerdp_bitmap_compress_24(srcData, width, height, s, byte_limit, start_line,