Coverage Report

Created: 2024-05-20 06:11

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