Coverage Report

Created: 2023-09-25 06:56

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