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 = { 0 };
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 == 0)
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 == 0)
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 = ((0x3 << 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 = ((0x3 << 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 = ((0x4 << 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_SetPosition(in_data, 0);
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 = ((0x4 << 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_SetPosition(in_data, 0);
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 = ((0x1 << 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 = ((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/*****************************************************************************/
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 = ((0x2 << 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 = ((0x2 << 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 start_line, wStream* WINPR_RESTRICT temp_s,
455 UINT32 e)
456{
457 uint8_t fom_mask[8192] = { 0 }; /* 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 = { 0 };
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 const char* line = start + 4ULL * width * start_line;
470 const char* last_line = NULL;
471
472 while ((line >= start) && (out_count < 32768))
473 {
474 size_t i = Stream_GetPosition(s) + 3ULL * count;
475
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))
481 {
482 break;
483 }
484
485 out_count += end * 3;
486
487 for (UINT32 j = 0; j < end; j++)
488 {
489 /* read next pixel */
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);
492
493 if (!TEST_FILL)
494 {
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)
498 {
499 if (counts.fill_count > count)
500 return -1;
501
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);
506 }
507
508 counts.fill_count = 0;
509 }
510
511 if (!TEST_MIX)
512 {
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)
516 {
517 if (counts.mix_count > count)
518 return -1;
519
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);
524 }
525
526 counts.mix_count = 0;
527 }
528
529 if (!(TEST_COLOR))
530 {
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)
535 {
536 if (counts.color_count > count)
537 return -1;
538
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);
543 }
544
545 counts.color_count = 0;
546 }
547
548 if (!test_bicolor(&counts))
549 {
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)
554 {
555 if ((counts.bicolor_count % 2) != 0)
556 counts.bicolor_count--;
557
558 if (counts.bicolor_count > count)
559 return -1;
560
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);
566 }
567
568 counts.bicolor_count = 0;
569 bicolor1 = last_pixel;
570 bicolor2 = pixel;
571 counts.bicolor_spin = FALSE;
572 }
573
574 if (!(TEST_FOM))
575 {
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)
580 {
581 if (counts.fom_count > count)
582 return -1;
583
584 count -= counts.fom_count;
585 count = out_copy_count_3(count, s, temp_s);
586 counts.fom_count =
587 out_from_count_3(counts.fom_count, s, fom_mask, counts.fom_mask_len);
588 reset_counts(&counts);
589 }
590
591 counts.fom_count = 0;
592 counts.fom_mask_len = 0;
593 }
594
595 if (TEST_FILL)
596 {
597 counts.fill_count++;
598 }
599
600 if (TEST_MIX)
601 {
602 counts.mix_count++;
603 }
604
605 if (TEST_COLOR)
606 {
607 counts.color_count++;
608 }
609
610 if (test_bicolor(&counts))
611 {
612 counts.bicolor_spin = !counts.bicolor_spin;
613 counts.bicolor_count++;
614 }
615
616 if (TEST_FOM)
617 {
618 if ((counts.fom_count % 8) == 0)
619 {
620 fom_mask[counts.fom_mask_len] = 0;
621 counts.fom_mask_len++;
622 }
623
624 if (pixel == (ypixel ^ mix))
625 {
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;
629 }
630
631 counts.fom_count++;
632 }
633
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);
637 count++;
638 last_pixel = pixel;
639 last_ypixel = ypixel;
640 }
641
642 /* can't take fix, mix, or fom past first line */
643 if (last_line == 0)
644 {
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)
648 {
649 if (counts.fill_count > count)
650 return -1;
651
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);
656 }
657
658 counts.fill_count = 0;
659
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)
663 {
664 if (counts.mix_count > count)
665 return -1;
666
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);
671 }
672
673 counts.mix_count = 0;
674
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)
678 {
679 if (counts.fom_count > count)
680 return -1;
681
682 count -= counts.fom_count;
683 count = out_copy_count_3(count, s, temp_s);
684 counts.fom_count =
685 out_from_count_3(counts.fom_count, s, fom_mask, counts.fom_mask_len);
686 reset_counts(&counts);
687 }
688
689 counts.fom_count = 0;
690 counts.fom_mask_len = 0;
691 }
692
693 last_line = line;
694 line = line - 4ULL * width;
695 start_line--;
696 lines_sent++;
697 }
698
699 Stream_SetPosition(temp_s, 0);
700
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)
704 {
705 if (counts.fill_count > count)
706 return -1;
707
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);
711 }
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)
715 {
716 if (counts.mix_count > count)
717 return -1;
718
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);
722 }
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)
726 {
727 if (counts.color_count > count)
728 return -1;
729
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);
733 }
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)
737 {
738 if ((counts.bicolor_count % 2) != 0)
739 counts.bicolor_count--;
740
741 if (counts.bicolor_count > count)
742 return -1;
743
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);
747
748 if (counts.bicolor_count > count)
749 return -1;
750
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);
754 }
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)
758 {
759 if (counts.fom_count > count)
760 return -1;
761
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);
765 }
766 else
767 {
768 (void)out_copy_count_3(count, s, temp_s);
769 }
770
771 return lines_sent;
772}
773
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)
779{
780 uint8_t fom_mask[8192] = { 0 }; /* good for up to 64K bitmap */
781 SSIZE_T lines_sent = 0;
782 UINT16 count = 0;
783 UINT16 last_pixel = 0;
784 UINT16 last_ypixel = 0;
785 struct count counts = { 0 };
786 UINT16 bicolor1 = 0;
787 UINT16 bicolor2 = 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;
794
795 while ((line >= start) && (out_count < 32768))
796 {
797 size_t i = Stream_GetPosition(s) + 2ULL * count;
798
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))
804 {
805 break;
806 }
807
808 out_count += end * 2;
809
810 for (UINT32 j = 0; j < end; j++)
811 {
812 /* read next pixel */
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);
815
816 if (!TEST_FILL)
817 {
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)
821 {
822 if (counts.fill_count > count)
823 return -1;
824
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);
829 }
830
831 counts.fill_count = 0;
832 }
833
834 if (!TEST_MIX)
835 {
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)
839 {
840 if (counts.mix_count > count)
841 return -1;
842
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);
847 }
848
849 counts.mix_count = 0;
850 }
851
852 if (!(TEST_COLOR))
853 {
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)
858 {
859 if (counts.color_count > count)
860 return -1;
861
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);
866 }
867
868 counts.color_count = 0;
869 }
870
871 if (!test_bicolor(&counts))
872 {
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))
877 {
878 if ((counts.bicolor_count % 2) != 0)
879 counts.bicolor_count--;
880
881 if (counts.bicolor_count > count)
882 return -1;
883
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);
889 }
890
891 counts.bicolor_count = 0;
892 bicolor1 = last_pixel;
893 bicolor2 = pixel;
894 counts.bicolor_spin = FALSE;
895 }
896
897 if (!(TEST_FOM))
898 {
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)
903 {
904 if (counts.fom_count > count)
905 return -1;
906
907 count -= counts.fom_count;
908 count = out_copy_count_2(count, s, temp_s);
909 counts.fom_count =
910 out_from_count_2(counts.fom_count, s, fom_mask, counts.fom_mask_len);
911 reset_counts(&counts);
912 }
913
914 counts.fom_count = 0;
915 counts.fom_mask_len = 0;
916 }
917
918 if (TEST_FILL)
919 {
920 counts.fill_count++;
921 }
922
923 if (TEST_MIX)
924 {
925 counts.mix_count++;
926 }
927
928 if (TEST_COLOR)
929 {
930 counts.color_count++;
931 }
932
933 if (test_bicolor(&counts))
934 {
935 counts.bicolor_spin = !counts.bicolor_spin;
936 counts.bicolor_count++;
937 }
938
939 if (TEST_FOM)
940 {
941 if ((counts.fom_count % 8) == 0)
942 {
943 fom_mask[counts.fom_mask_len] = 0;
944 counts.fom_mask_len++;
945 }
946
947 if (pixel == (ypixel ^ mix))
948 {
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;
952 }
953
954 counts.fom_count++;
955 }
956
957 Stream_Write_UINT16(temp_s, pixel);
958 count++;
959 last_pixel = pixel;
960 last_ypixel = ypixel;
961 }
962
963 /* can't take fix, mix, or fom past first line */
964 if (last_line == 0)
965 {
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)
969 {
970 if (counts.fill_count > count)
971 return -1;
972
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);
977 }
978
979 counts.fill_count = 0;
980
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)
984 {
985 if (counts.mix_count > count)
986 return -1;
987
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);
992 }
993
994 counts.mix_count = 0;
995
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)
999 {
1000 if (counts.fom_count > count)
1001 return -1;
1002
1003 count -= counts.fom_count;
1004 count = out_copy_count_2(count, s, temp_s);
1005 counts.fom_count =
1006 out_from_count_2(counts.fom_count, s, fom_mask, counts.fom_mask_len);
1007 reset_counts(&counts);
1008 }
1009
1010 counts.fom_count = 0;
1011 counts.fom_mask_len = 0;
1012 }
1013
1014 last_line = line;
1015 line = line - 2ULL * width;
1016 start_line--;
1017 lines_sent++;
1018 }
1019
1020 Stream_SetPosition(temp_s, 0);
1021
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)
1025 {
1026 if (counts.fill_count > count)
1027 return -1;
1028
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);
1032 }
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)
1036 {
1037 if (counts.mix_count > count)
1038 return -1;
1039
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);
1043 }
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)
1047 {
1048 if (counts.color_count > count)
1049 return -1;
1050
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);
1054 }
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)
1058 {
1059 if ((counts.bicolor_count % 2) != 0)
1060 counts.bicolor_count--;
1061
1062 if (counts.bicolor_count > count)
1063 return -1;
1064
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);
1068
1069 if (counts.bicolor_count > count)
1070 return -1;
1071
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);
1075 }
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)
1079 {
1080 if (counts.fom_count > count)
1081 return -1;
1082
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);
1086 }
1087 else
1088 {
1089 (void)out_copy_count_2(count, s, temp_s);
1090 }
1091
1092 return lines_sent;
1093}
1094
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)
1098{
1099 Stream_SetPosition(temp_s, 0);
1100
1101 switch (bpp)
1102 {
1103 case 15:
1104 case 16:
1105 return freerdp_bitmap_compress_16(srcData, width, height, s, bpp, byte_limit,
1106 start_line, temp_s, e);
1107
1108 case 24:
1109 return freerdp_bitmap_compress_24(srcData, width, height, s, byte_limit, start_line,
1110 temp_s, e);
1111
1112 default:
1113 return -1;
1114 }
1115}