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