FreeRDP
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 
28 static 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 
34 static 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 /*****************************************************************************/
42 static 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 /*****************************************************************************/
54 static 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 */
67 static 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 */
99 static 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 */
135 static 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 */
168 static 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 */
201 static 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 */
235 static 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 */
273 static 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 */
301 static 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 */
328 static 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 */
356 static 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 */
385 static 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) */
417 static 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 
467 static INLINE SSIZE_T freerdp_bitmap_compress_24(const void* WINPR_RESTRICT srcData, UINT32 width,
468  UINT32 height, wStream* WINPR_RESTRICT s,
469  UINT32 byte_limit, UINT32 start_line,
470  wStream* WINPR_RESTRICT temp_s, UINT32 e)
471 {
472  int8_t fom_mask[8192] = { 0 }; /* good for up to 64K bitmap */
473  SSIZE_T lines_sent = 0;
474  UINT16 count = 0;
475  UINT16 color_count = 0;
476  UINT32 last_pixel = 0;
477  UINT32 last_ypixel = 0;
478  UINT16 bicolor_count = 0;
479  UINT32 bicolor1 = 0;
480  UINT32 bicolor2 = 0;
481  BOOL bicolor_spin = FALSE;
482  UINT32 end = width + e;
483  UINT32 out_count = end * 3;
484  UINT16 fill_count = 0;
485  UINT16 mix_count = 0;
486  const UINT32 mix = 0xFFFFFF;
487  UINT16 fom_count = 0;
488  size_t fom_mask_len = 0;
489  const char* start = (const char*)srcData;
490  const char* line = start + 4ULL * width * start_line;
491  const char* last_line = NULL;
492 
493  while ((line >= start) && (out_count < 32768))
494  {
495  size_t i = Stream_GetPosition(s) + 3ULL * count;
496 
497  if ((i - (3ULL * color_count) >= byte_limit) &&
498  (i - (3ULL * bicolor_count) >= byte_limit) && (i - (3ULL * fill_count) >= byte_limit) &&
499  (i - (3ULL * mix_count) >= byte_limit) && (i - (3ULL * fom_count) >= byte_limit))
500  {
501  break;
502  }
503 
504  out_count += end * 3;
505 
506  for (UINT32 j = 0; j < end; j++)
507  {
508  /* read next pixel */
509  const UINT32 pixel = IN_PIXEL32(line, j, 0, width, last_pixel);
510  const UINT32 ypixel = IN_PIXEL32(last_line, j, 0, width, last_ypixel);
511 
512  if (!TEST_FILL)
513  {
514  if (fill_count > 3 && fill_count >= color_count && fill_count >= bicolor_count &&
515  fill_count >= mix_count && fill_count >= fom_count)
516  {
517  if (fill_count > count)
518  return -1;
519 
520  count -= fill_count;
521  OUT_COPY_COUNT3(count, s, temp_s);
522  OUT_FILL_COUNT3(fill_count, s);
523  RESET_COUNTS;
524  }
525 
526  fill_count = 0;
527  }
528 
529  if (!TEST_MIX)
530  {
531  if (mix_count > 3 && mix_count >= fill_count && mix_count >= bicolor_count &&
532  mix_count >= color_count && mix_count >= fom_count)
533  {
534  if (mix_count > count)
535  return -1;
536 
537  count -= mix_count;
538  OUT_COPY_COUNT3(count, s, temp_s);
539  OUT_MIX_COUNT3(mix_count, s);
540  RESET_COUNTS;
541  }
542 
543  mix_count = 0;
544  }
545 
546  if (!(TEST_COLOR))
547  {
548  if (color_count > 3 && color_count >= fill_count && color_count >= bicolor_count &&
549  color_count >= mix_count && color_count >= fom_count)
550  {
551  if (color_count > count)
552  return -1;
553 
554  count -= color_count;
555  OUT_COPY_COUNT3(count, s, temp_s);
556  OUT_COLOR_COUNT3(color_count, s, last_pixel);
557  RESET_COUNTS;
558  }
559 
560  color_count = 0;
561  }
562 
563  if (!TEST_BICOLOR)
564  {
565  if (bicolor_count > 3 && bicolor_count >= fill_count &&
566  bicolor_count >= color_count && bicolor_count >= mix_count &&
567  bicolor_count >= fom_count)
568  {
569  if ((bicolor_count % 2) != 0)
570  bicolor_count--;
571 
572  if (bicolor_count > count)
573  return -1;
574 
575  count -= bicolor_count;
576  OUT_COPY_COUNT3(count, s, temp_s);
577  OUT_BICOLOR_COUNT3(bicolor_count, s, bicolor2, bicolor1);
578  RESET_COUNTS;
579  }
580 
581  bicolor_count = 0;
582  bicolor1 = last_pixel;
583  bicolor2 = pixel;
584  bicolor_spin = FALSE;
585  }
586 
587  if (!(TEST_FOM))
588  {
589  if (fom_count > 3 && fom_count >= fill_count && fom_count >= color_count &&
590  fom_count >= mix_count && fom_count >= bicolor_count)
591  {
592  if (fom_count > count)
593  return -1;
594 
595  count -= fom_count;
596  OUT_COPY_COUNT3(count, s, temp_s);
597  OUT_FOM_COUNT3(fom_count, s, fom_mask, fom_mask_len);
598  RESET_COUNTS;
599  }
600 
601  fom_count = 0;
602  fom_mask_len = 0;
603  }
604 
605  if (TEST_FILL)
606  {
607  fill_count++;
608  }
609 
610  if (TEST_MIX)
611  {
612  mix_count++;
613  }
614 
615  if (TEST_COLOR)
616  {
617  color_count++;
618  }
619 
620  if (TEST_BICOLOR)
621  {
622  bicolor_spin = !bicolor_spin;
623  bicolor_count++;
624  }
625 
626  if (TEST_FOM)
627  {
628  if ((fom_count % 8) == 0)
629  {
630  fom_mask[fom_mask_len] = 0;
631  fom_mask_len++;
632  }
633 
634  if (pixel == (ypixel ^ mix))
635  {
636  fom_mask[fom_mask_len - 1] |=
637  WINPR_ASSERTING_INT_CAST(int8_t, (1 << (fom_count % 8)));
638  }
639 
640  fom_count++;
641  }
642 
643  Stream_Write_UINT8(temp_s, pixel & 0xff);
644  Stream_Write_UINT8(temp_s, (pixel >> 8) & 0xff);
645  Stream_Write_UINT8(temp_s, (pixel >> 16) & 0xff);
646  count++;
647  last_pixel = pixel;
648  last_ypixel = ypixel;
649  }
650 
651  /* can't take fix, mix, or fom past first line */
652  if (last_line == 0)
653  {
654  if (fill_count > 3 && fill_count >= color_count && fill_count >= bicolor_count &&
655  fill_count >= mix_count && fill_count >= fom_count)
656  {
657  if (fill_count > count)
658  return -1;
659 
660  count -= fill_count;
661  OUT_COPY_COUNT3(count, s, temp_s);
662  OUT_FILL_COUNT3(fill_count, s);
663  RESET_COUNTS;
664  }
665 
666  fill_count = 0;
667 
668  if (mix_count > 3 && mix_count >= fill_count && mix_count >= bicolor_count &&
669  mix_count >= color_count && mix_count >= fom_count)
670  {
671  if (mix_count > count)
672  return -1;
673 
674  count -= mix_count;
675  OUT_COPY_COUNT3(count, s, temp_s);
676  OUT_MIX_COUNT3(mix_count, s);
677  RESET_COUNTS;
678  }
679 
680  mix_count = 0;
681 
682  if (fom_count > 3 && fom_count >= fill_count && fom_count >= color_count &&
683  fom_count >= mix_count && fom_count >= bicolor_count)
684  {
685  if (fom_count > count)
686  return -1;
687 
688  count -= fom_count;
689  OUT_COPY_COUNT3(count, s, temp_s);
690  OUT_FOM_COUNT3(fom_count, s, fom_mask, fom_mask_len);
691  RESET_COUNTS;
692  }
693 
694  fom_count = 0;
695  fom_mask_len = 0;
696  }
697 
698  last_line = line;
699  line = line - 4ULL * width;
700  start_line--;
701  lines_sent++;
702  }
703 
704  Stream_SetPosition(temp_s, 0);
705 
706  if (fill_count > 3 && fill_count >= color_count && fill_count >= bicolor_count &&
707  fill_count >= mix_count && fill_count >= fom_count)
708  {
709  if (fill_count > count)
710  return -1;
711 
712  count -= fill_count;
713  OUT_COPY_COUNT3(count, s, temp_s);
714  OUT_FILL_COUNT3(fill_count, s);
715  }
716  else if (mix_count > 3 && mix_count >= color_count && mix_count >= bicolor_count &&
717  mix_count >= fill_count && mix_count >= fom_count)
718  {
719  if (mix_count > count)
720  return -1;
721 
722  count -= mix_count;
723  OUT_COPY_COUNT3(count, s, temp_s);
724  OUT_MIX_COUNT3(mix_count, s);
725  }
726  else if (color_count > 3 && color_count >= mix_count && color_count >= bicolor_count &&
727  color_count >= fill_count && color_count >= fom_count)
728  {
729  if (color_count > count)
730  return -1;
731 
732  count -= color_count;
733  OUT_COPY_COUNT3(count, s, temp_s);
734  OUT_COLOR_COUNT3(color_count, s, last_pixel);
735  }
736  else if (bicolor_count > 3 && bicolor_count >= mix_count && bicolor_count >= color_count &&
737  bicolor_count >= fill_count && bicolor_count >= fom_count)
738  {
739  if ((bicolor_count % 2) != 0)
740  bicolor_count--;
741 
742  if (bicolor_count > count)
743  return -1;
744 
745  count -= bicolor_count;
746  OUT_COPY_COUNT3(count, s, temp_s);
747  OUT_BICOLOR_COUNT3(bicolor_count, s, bicolor2, bicolor1);
748 
749  if (bicolor_count > count)
750  return -1;
751 
752  count -= bicolor_count;
753  OUT_COPY_COUNT3(count, s, temp_s);
754  OUT_BICOLOR_COUNT3(bicolor_count, s, bicolor1, bicolor2);
755  }
756  else if (fom_count > 3 && fom_count >= mix_count && fom_count >= color_count &&
757  fom_count >= fill_count && fom_count >= bicolor_count)
758  {
759  if (fom_count > count)
760  return -1;
761 
762  count -= fom_count;
763  OUT_COPY_COUNT3(count, s, temp_s);
764  OUT_FOM_COUNT3(fom_count, s, fom_mask, fom_mask_len);
765  }
766  else
767  {
768  OUT_COPY_COUNT3(count, s, temp_s);
769  }
770 
771  return lines_sent;
772 }
773 
774 static INLINE SSIZE_T freerdp_bitmap_compress_16(const void* WINPR_RESTRICT srcData, UINT32 width,
775  UINT32 height, wStream* WINPR_RESTRICT s,
776  UINT32 bpp, UINT32 byte_limit, UINT32 start_line,
777  wStream* WINPR_RESTRICT temp_s, UINT32 e)
778 {
779  int8_t fom_mask[8192] = { 0 }; /* good for up to 64K bitmap */
780  SSIZE_T lines_sent = 0;
781  UINT16 count = 0;
782  UINT16 color_count = 0;
783  UINT16 last_pixel = 0;
784  UINT16 last_ypixel = 0;
785  UINT16 bicolor_count = 0;
786  UINT16 bicolor1 = 0;
787  UINT16 bicolor2 = 0;
788  BOOL bicolor_spin = FALSE;
789  UINT32 end = width + e;
790  UINT32 out_count = end * 2;
791  UINT16 fill_count = 0;
792  UINT16 mix_count = 0;
793  const UINT32 mix = (bpp == 15) ? 0xBA1F : 0xFFFF;
794  UINT16 fom_count = 0;
795  size_t fom_mask_len = 0;
796  const char* start = (const char*)srcData;
797  const char* line = start + 2ULL * width * start_line;
798  const char* last_line = NULL;
799 
800  while ((line >= start) && (out_count < 32768))
801  {
802  size_t i = Stream_GetPosition(s) + 2ULL * count;
803 
804  if ((i - (2ULL * color_count) >= byte_limit) &&
805  (i - (2ULL * bicolor_count) >= byte_limit) && (i - (2ULL * fill_count) >= byte_limit) &&
806  (i - (2ULL * mix_count) >= byte_limit) && (i - (2ULL * fom_count) >= byte_limit))
807  {
808  break;
809  }
810 
811  out_count += end * 2;
812 
813  for (UINT32 j = 0; j < end; j++)
814  {
815  /* read next pixel */
816  const UINT16 pixel = IN_PIXEL16(line, j, 0, width, last_pixel);
817  const UINT16 ypixel = IN_PIXEL16(last_line, j, 0, width, last_ypixel);
818 
819  if (!TEST_FILL)
820  {
821  if (fill_count > 3 && fill_count >= color_count && fill_count >= bicolor_count &&
822  fill_count >= mix_count && fill_count >= fom_count)
823  {
824  if (fill_count > count)
825  return -1;
826 
827  count -= fill_count;
828  OUT_COPY_COUNT2(count, s, temp_s);
829  OUT_FILL_COUNT2(fill_count, s);
830  RESET_COUNTS;
831  }
832 
833  fill_count = 0;
834  }
835 
836  if (!TEST_MIX)
837  {
838  if (mix_count > 3 && mix_count >= fill_count && mix_count >= bicolor_count &&
839  mix_count >= color_count && mix_count >= fom_count)
840  {
841  if (mix_count > count)
842  return -1;
843 
844  count -= mix_count;
845  OUT_COPY_COUNT2(count, s, temp_s);
846  OUT_MIX_COUNT2(mix_count, s);
847  RESET_COUNTS;
848  }
849 
850  mix_count = 0;
851  }
852 
853  if (!(TEST_COLOR))
854  {
855  if (color_count > 3 && color_count >= fill_count && color_count >= bicolor_count &&
856  color_count >= mix_count && color_count >= fom_count)
857  {
858  if (color_count > count)
859  return -1;
860 
861  count -= color_count;
862  OUT_COPY_COUNT2(count, s, temp_s);
863  OUT_COLOR_COUNT2(color_count, s, last_pixel);
864  RESET_COUNTS;
865  }
866 
867  color_count = 0;
868  }
869 
870  if (!TEST_BICOLOR)
871  {
872  if ((bicolor_count > 3) && (bicolor_count >= fill_count) &&
873  (bicolor_count >= color_count) && (bicolor_count >= mix_count) &&
874  (bicolor_count >= fom_count))
875  {
876  if ((bicolor_count % 2) != 0)
877  bicolor_count--;
878 
879  if (bicolor_count > count)
880  return -1;
881 
882  count -= bicolor_count;
883  OUT_COPY_COUNT2(count, s, temp_s);
884  OUT_BICOLOR_COUNT2(bicolor_count, s, bicolor2, bicolor1);
885  RESET_COUNTS;
886  }
887 
888  bicolor_count = 0;
889  bicolor1 = last_pixel;
890  bicolor2 = pixel;
891  bicolor_spin = FALSE;
892  }
893 
894  if (!(TEST_FOM))
895  {
896  if (fom_count > 3 && fom_count >= fill_count && fom_count >= color_count &&
897  fom_count >= mix_count && fom_count >= bicolor_count)
898  {
899  if (fom_count > count)
900  return -1;
901 
902  count -= fom_count;
903  OUT_COPY_COUNT2(count, s, temp_s);
904  OUT_FOM_COUNT2(fom_count, s, fom_mask, fom_mask_len);
905  RESET_COUNTS;
906  }
907 
908  fom_count = 0;
909  fom_mask_len = 0;
910  }
911 
912  if (TEST_FILL)
913  {
914  fill_count++;
915  }
916 
917  if (TEST_MIX)
918  {
919  mix_count++;
920  }
921 
922  if (TEST_COLOR)
923  {
924  color_count++;
925  }
926 
927  if (TEST_BICOLOR)
928  {
929  bicolor_spin = !bicolor_spin;
930  bicolor_count++;
931  }
932 
933  if (TEST_FOM)
934  {
935  if ((fom_count % 8) == 0)
936  {
937  fom_mask[fom_mask_len] = 0;
938  fom_mask_len++;
939  }
940 
941  if (pixel == (ypixel ^ mix))
942  {
943  fom_mask[fom_mask_len - 1] |=
944  WINPR_ASSERTING_INT_CAST(int8_t, (1 << (fom_count % 8)));
945  }
946 
947  fom_count++;
948  }
949 
950  Stream_Write_UINT16(temp_s, pixel);
951  count++;
952  last_pixel = pixel;
953  last_ypixel = ypixel;
954  }
955 
956  /* can't take fix, mix, or fom past first line */
957  if (last_line == 0)
958  {
959  if (fill_count > 3 && fill_count >= color_count && fill_count >= bicolor_count &&
960  fill_count >= mix_count && fill_count >= fom_count)
961  {
962  if (fill_count > count)
963  return -1;
964 
965  count -= fill_count;
966  OUT_COPY_COUNT2(count, s, temp_s);
967  OUT_FILL_COUNT2(fill_count, s);
968  RESET_COUNTS;
969  }
970 
971  fill_count = 0;
972 
973  if (mix_count > 3 && mix_count >= fill_count && mix_count >= bicolor_count &&
974  mix_count >= color_count && mix_count >= fom_count)
975  {
976  if (mix_count > count)
977  return -1;
978 
979  count -= mix_count;
980  OUT_COPY_COUNT2(count, s, temp_s);
981  OUT_MIX_COUNT2(mix_count, s);
982  RESET_COUNTS;
983  }
984 
985  mix_count = 0;
986 
987  if (fom_count > 3 && fom_count >= fill_count && fom_count >= color_count &&
988  fom_count >= mix_count && fom_count >= bicolor_count)
989  {
990  if (fom_count > count)
991  return -1;
992 
993  count -= fom_count;
994  OUT_COPY_COUNT2(count, s, temp_s);
995  OUT_FOM_COUNT2(fom_count, s, fom_mask, fom_mask_len);
996  RESET_COUNTS;
997  }
998 
999  fom_count = 0;
1000  fom_mask_len = 0;
1001  }
1002 
1003  last_line = line;
1004  line = line - 2ULL * width;
1005  start_line--;
1006  lines_sent++;
1007  }
1008 
1009  Stream_SetPosition(temp_s, 0);
1010 
1011  if (fill_count > 3 && fill_count >= color_count && fill_count >= bicolor_count &&
1012  fill_count >= mix_count && fill_count >= fom_count)
1013  {
1014  if (fill_count > count)
1015  return -1;
1016 
1017  count -= fill_count;
1018  OUT_COPY_COUNT2(count, s, temp_s);
1019  OUT_FILL_COUNT2(fill_count, s);
1020  }
1021  else if (mix_count > 3 && mix_count >= color_count && mix_count >= bicolor_count &&
1022  mix_count >= fill_count && mix_count >= fom_count)
1023  {
1024  if (mix_count > count)
1025  return -1;
1026 
1027  count -= mix_count;
1028  OUT_COPY_COUNT2(count, s, temp_s);
1029  OUT_MIX_COUNT2(mix_count, s);
1030  }
1031  else if (color_count > 3 && color_count >= mix_count && color_count >= bicolor_count &&
1032  color_count >= fill_count && color_count >= fom_count)
1033  {
1034  if (color_count > count)
1035  return -1;
1036 
1037  count -= color_count;
1038  OUT_COPY_COUNT2(count, s, temp_s);
1039  OUT_COLOR_COUNT2(color_count, s, last_pixel);
1040  }
1041  else if (bicolor_count > 3 && bicolor_count >= mix_count && bicolor_count >= color_count &&
1042  bicolor_count >= fill_count && bicolor_count >= fom_count)
1043  {
1044  if ((bicolor_count % 2) != 0)
1045  bicolor_count--;
1046 
1047  if (bicolor_count > count)
1048  return -1;
1049 
1050  count -= bicolor_count;
1051  OUT_COPY_COUNT2(count, s, temp_s);
1052  OUT_BICOLOR_COUNT2(bicolor_count, s, bicolor2, bicolor1);
1053 
1054  if (bicolor_count > count)
1055  return -1;
1056 
1057  count -= bicolor_count;
1058  OUT_COPY_COUNT2(count, s, temp_s);
1059  OUT_BICOLOR_COUNT2(bicolor_count, s, bicolor1, bicolor2);
1060  }
1061  else if (fom_count > 3 && fom_count >= mix_count && fom_count >= color_count &&
1062  fom_count >= fill_count && fom_count >= bicolor_count)
1063  {
1064  if (fom_count > count)
1065  return -1;
1066 
1067  count -= fom_count;
1068  OUT_COPY_COUNT2(count, s, temp_s);
1069  OUT_FOM_COUNT2(fom_count, s, fom_mask, fom_mask_len);
1070  }
1071  else
1072  {
1073  OUT_COPY_COUNT2(count, s, temp_s);
1074  }
1075 
1076  return lines_sent;
1077 }
1078 
1079 SSIZE_T freerdp_bitmap_compress(const void* WINPR_RESTRICT srcData, UINT32 width, UINT32 height,
1080  wStream* WINPR_RESTRICT s, UINT32 bpp, UINT32 byte_limit,
1081  UINT32 start_line, wStream* WINPR_RESTRICT temp_s, UINT32 e)
1082 {
1083  Stream_SetPosition(temp_s, 0);
1084 
1085  switch (bpp)
1086  {
1087  case 15:
1088  case 16:
1089  return freerdp_bitmap_compress_16(srcData, width, height, s, bpp, byte_limit,
1090  start_line, temp_s, e);
1091 
1092  case 24:
1093  return freerdp_bitmap_compress_24(srcData, width, height, s, byte_limit, start_line,
1094  temp_s, e);
1095 
1096  default:
1097  return -1;
1098  }
1099 }