Coverage Report

Created: 2025-07-01 06:46

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