Coverage Report

Created: 2026-01-16 07:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/libfreerdp/codec/planar.c
Line
Count
Source
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * RDP6 Planar Codec
4
 *
5
 * Copyright 2013 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
 * Copyright 2016 Armin Novak <armin.novak@thincast.com>
7
 * Copyright 2016 Thincast Technologies GmbH
8
 *
9
 * Licensed under the Apache License, Version 2.0 (the "License");
10
 * you may not use this file except in compliance with the License.
11
 * You may obtain a copy of the License at
12
 *
13
 *     http://www.apache.org/licenses/LICENSE-2.0
14
 *
15
 * Unless required by applicable law or agreed to in writing, software
16
 * distributed under the License is distributed on an "AS IS" BASIS,
17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
 * See the License for the specific language governing permissions and
19
 * limitations under the License.
20
 */
21
22
#include <freerdp/config.h>
23
24
#include <winpr/crt.h>
25
#include <winpr/wtypes.h>
26
#include <winpr/assert.h>
27
#include <winpr/cast.h>
28
#include <winpr/print.h>
29
30
#include <freerdp/primitives.h>
31
#include <freerdp/log.h>
32
#include <freerdp/codec/bitmap.h>
33
#include <freerdp/codec/planar.h>
34
35
#define TAG FREERDP_TAG("codec")
36
37
#define PLANAR_ALIGN(val, align) \
38
10.7k
  ((val) % (align) == 0) ? (val) : ((val) + (align) - (val) % (align))
39
40
typedef struct
41
{
42
  /**
43
   * controlByte:
44
   * [0-3]: nRunLength
45
   * [4-7]: cRawBytes
46
   */
47
  BYTE controlByte;
48
  BYTE* rawValues;
49
} RDP6_RLE_SEGMENT;
50
51
typedef struct
52
{
53
  UINT32 cSegments;
54
  RDP6_RLE_SEGMENT* segments;
55
} RDP6_RLE_SEGMENTS;
56
57
typedef struct
58
{
59
  /**
60
   * formatHeader:
61
   * [0-2]: Color Loss Level (CLL)
62
   *  [3] : Chroma Subsampling (CS)
63
   *  [4] : Run Length Encoding (RLE)
64
   *  [5] : No Alpha (NA)
65
   * [6-7]: Reserved
66
   */
67
  BYTE formatHeader;
68
} RDP6_BITMAP_STREAM;
69
70
struct S_BITMAP_PLANAR_CONTEXT
71
{
72
  UINT32 maxWidth;
73
  UINT32 maxHeight;
74
  UINT32 maxPlaneSize;
75
76
  BOOL AllowSkipAlpha;
77
  BOOL AllowRunLengthEncoding;
78
  BOOL AllowColorSubsampling;
79
  BOOL AllowDynamicColorFidelity;
80
81
  UINT32 ColorLossLevel;
82
83
  BYTE* planes[4];
84
  BYTE* planesBuffer;
85
86
  BYTE* deltaPlanes[4];
87
  BYTE* deltaPlanesBuffer;
88
89
  BYTE* rlePlanes[4];
90
  BYTE* rlePlanesBuffer;
91
92
  BYTE* pTempData;
93
  UINT32 nTempStep;
94
95
  BOOL bgr;
96
  BOOL topdown;
97
};
98
99
static inline BYTE PLANAR_CONTROL_BYTE(UINT32 nRunLength, UINT32 cRawBytes)
100
0
{
101
0
  return WINPR_ASSERTING_INT_CAST(UINT8, ((nRunLength & 0x0F) | ((cRawBytes & 0x0F) << 4)));
102
0
}
103
104
static inline BYTE PLANAR_CONTROL_BYTE_RUN_LENGTH(UINT32 controlByte)
105
17.1M
{
106
17.1M
  return (controlByte & 0x0F);
107
17.1M
}
108
static inline BYTE PLANAR_CONTROL_BYTE_RAW_BYTES(UINT32 controlByte)
109
17.1M
{
110
17.1M
  return ((controlByte >> 4) & 0x0F);
111
17.1M
}
112
113
static inline UINT32 planar_invert_format(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT planar, BOOL alpha,
114
                                          UINT32 DstFormat)
115
11.3k
{
116
11.3k
  WINPR_ASSERT(planar);
117
11.3k
  if (planar->bgr && alpha)
118
0
  {
119
0
    switch (DstFormat)
120
0
    {
121
0
      case PIXEL_FORMAT_ARGB32:
122
0
        DstFormat = PIXEL_FORMAT_ABGR32;
123
0
        break;
124
0
      case PIXEL_FORMAT_XRGB32:
125
0
        DstFormat = PIXEL_FORMAT_XBGR32;
126
0
        break;
127
0
      case PIXEL_FORMAT_ABGR32:
128
0
        DstFormat = PIXEL_FORMAT_ARGB32;
129
0
        break;
130
0
      case PIXEL_FORMAT_XBGR32:
131
0
        DstFormat = PIXEL_FORMAT_XRGB32;
132
0
        break;
133
0
      case PIXEL_FORMAT_BGRA32:
134
0
        DstFormat = PIXEL_FORMAT_RGBA32;
135
0
        break;
136
0
      case PIXEL_FORMAT_BGRX32:
137
0
        DstFormat = PIXEL_FORMAT_RGBX32;
138
0
        break;
139
0
      case PIXEL_FORMAT_RGBA32:
140
0
        DstFormat = PIXEL_FORMAT_BGRA32;
141
0
        break;
142
0
      case PIXEL_FORMAT_RGBX32:
143
0
        DstFormat = PIXEL_FORMAT_BGRX32;
144
0
        break;
145
0
      case PIXEL_FORMAT_RGB24:
146
0
        DstFormat = PIXEL_FORMAT_BGR24;
147
0
        break;
148
0
      case PIXEL_FORMAT_BGR24:
149
0
        DstFormat = PIXEL_FORMAT_RGB24;
150
0
        break;
151
0
      case PIXEL_FORMAT_RGB16:
152
0
        DstFormat = PIXEL_FORMAT_BGR16;
153
0
        break;
154
0
      case PIXEL_FORMAT_BGR16:
155
0
        DstFormat = PIXEL_FORMAT_RGB16;
156
0
        break;
157
0
      case PIXEL_FORMAT_ARGB15:
158
0
        DstFormat = PIXEL_FORMAT_ABGR15;
159
0
        break;
160
0
      case PIXEL_FORMAT_RGB15:
161
0
        DstFormat = PIXEL_FORMAT_BGR15;
162
0
        break;
163
0
      case PIXEL_FORMAT_ABGR15:
164
0
        DstFormat = PIXEL_FORMAT_ARGB15;
165
0
        break;
166
0
      case PIXEL_FORMAT_BGR15:
167
0
        DstFormat = PIXEL_FORMAT_RGB15;
168
0
        break;
169
0
      default:
170
0
        break;
171
0
    }
172
0
  }
173
11.3k
  return DstFormat;
174
11.3k
}
175
176
static inline BOOL freerdp_bitmap_planar_compress_plane_rle(const BYTE* WINPR_RESTRICT inPlane,
177
                                                            UINT32 width, UINT32 height,
178
                                                            BYTE* WINPR_RESTRICT outPlane,
179
                                                            UINT32* WINPR_RESTRICT dstSize);
180
static inline BYTE* freerdp_bitmap_planar_delta_encode_plane(const BYTE* WINPR_RESTRICT inPlane,
181
                                                             UINT32 width, UINT32 height,
182
                                                             BYTE* WINPR_RESTRICT outPlane);
183
184
static inline INT32 planar_skip_plane_rle(const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcSize,
185
                                          UINT32 nWidth, UINT32 nHeight)
186
2.96k
{
187
2.96k
  UINT32 used = 0;
188
189
2.96k
  WINPR_ASSERT(pSrcData);
190
191
43.4k
  for (UINT32 y = 0; y < nHeight; y++)
192
42.4k
  {
193
12.0M
    for (UINT32 x = 0; x < nWidth;)
194
12.0M
    {
195
12.0M
      if (used >= SrcSize)
196
434
      {
197
434
        WLog_ERR(TAG, "planar plane used %" PRIu32 " exceeds SrcSize %" PRIu32, used,
198
434
                 SrcSize);
199
434
        return -1;
200
434
      }
201
202
12.0M
      const uint8_t controlByte = pSrcData[used++];
203
12.0M
      uint32_t nRunLength = PLANAR_CONTROL_BYTE_RUN_LENGTH(controlByte);
204
12.0M
      uint32_t cRawBytes = PLANAR_CONTROL_BYTE_RAW_BYTES(controlByte);
205
206
12.0M
      if (nRunLength == 1)
207
20.3k
      {
208
20.3k
        nRunLength = cRawBytes + 16;
209
20.3k
        cRawBytes = 0;
210
20.3k
      }
211
12.0M
      else if (nRunLength == 2)
212
32.1k
      {
213
32.1k
        nRunLength = cRawBytes + 32;
214
32.1k
        cRawBytes = 0;
215
32.1k
      }
216
217
12.0M
      used += cRawBytes;
218
12.0M
      x += cRawBytes;
219
12.0M
      x += nRunLength;
220
221
12.0M
      if (x > nWidth)
222
807
      {
223
807
        WLog_ERR(TAG, "planar plane x %" PRIu32 " exceeds width %" PRIu32, x, nWidth);
224
807
        return -1;
225
807
      }
226
227
12.0M
      if (used > SrcSize)
228
726
      {
229
726
        WLog_ERR(TAG, "planar plane used %" PRIu32 " exceeds SrcSize %" PRIu32, used,
230
726
                 INT32_MAX);
231
726
        return -1;
232
726
      }
233
12.0M
    }
234
42.4k
  }
235
236
998
  if (used > INT32_MAX)
237
0
  {
238
0
    WLog_ERR(TAG, "planar plane used %" PRIu32 " exceeds SrcSize %" PRIu32, used, SrcSize);
239
0
    return -1;
240
0
  }
241
998
  return (INT32)used;
242
998
}
243
244
static inline UINT8 clamp(INT16 val)
245
855k
{
246
855k
  return (UINT8)val;
247
855k
}
248
249
static inline INT32 planar_decompress_plane_rle_only(const BYTE* WINPR_RESTRICT pSrcData,
250
                                                     UINT32 SrcSize, BYTE* WINPR_RESTRICT pDstData,
251
                                                     UINT32 nWidth, UINT32 nHeight)
252
567
{
253
567
  BYTE* previousScanline = NULL;
254
567
  const BYTE* srcp = pSrcData;
255
256
567
  WINPR_ASSERT(nHeight <= INT32_MAX);
257
567
  WINPR_ASSERT(nWidth <= INT32_MAX);
258
259
14.4k
  for (UINT32 y = 0; y < nHeight; y++)
260
13.8k
  {
261
13.8k
    BYTE* dstp = &pDstData[1ULL * (y)*nWidth];
262
13.8k
    INT16 pixel = 0;
263
13.8k
    BYTE* currentScanline = dstp;
264
265
5.09M
    for (UINT32 x = 0; x < nWidth;)
266
5.08M
    {
267
5.08M
      BYTE controlByte = *srcp;
268
5.08M
      srcp++;
269
270
5.08M
      if ((srcp - pSrcData) > SrcSize * 1ll)
271
0
      {
272
0
        WLog_ERR(TAG, "error reading input buffer");
273
0
        return -1;
274
0
      }
275
276
5.08M
      UINT32 nRunLength = PLANAR_CONTROL_BYTE_RUN_LENGTH(controlByte);
277
5.08M
      UINT32 cRawBytes = PLANAR_CONTROL_BYTE_RAW_BYTES(controlByte);
278
279
5.08M
      if (nRunLength == 1)
280
5.38k
      {
281
5.38k
        nRunLength = cRawBytes + 16;
282
5.38k
        cRawBytes = 0;
283
5.38k
      }
284
5.07M
      else if (nRunLength == 2)
285
6.49k
      {
286
6.49k
        nRunLength = cRawBytes + 32;
287
6.49k
        cRawBytes = 0;
288
6.49k
      }
289
290
5.08M
      if (((dstp + (cRawBytes + nRunLength)) - currentScanline) > nWidth * 1ll)
291
0
      {
292
0
        WLog_ERR(TAG, "too many pixels in scanline");
293
0
        return -1;
294
0
      }
295
296
5.08M
      if (!previousScanline)
297
2.38k
      {
298
        /* first scanline, absolute values */
299
4.10k
        while (cRawBytes > 0)
300
1.72k
        {
301
1.72k
          pixel = *srcp;
302
1.72k
          srcp++;
303
1.72k
          *dstp = clamp(pixel);
304
1.72k
          dstp++;
305
1.72k
          x++;
306
1.72k
          cRawBytes--;
307
1.72k
        }
308
309
14.5k
        while (nRunLength > 0)
310
12.1k
        {
311
12.1k
          *dstp = clamp(pixel);
312
12.1k
          dstp++;
313
12.1k
          x++;
314
12.1k
          nRunLength--;
315
12.1k
        }
316
2.38k
      }
317
5.08M
      else
318
5.08M
      {
319
        /* delta values relative to previous scanline */
320
5.12M
        while (cRawBytes > 0)
321
45.2k
        {
322
45.2k
          UINT8 deltaValue = *srcp;
323
45.2k
          srcp++;
324
325
45.2k
          if (deltaValue & 1)
326
5.44k
          {
327
5.44k
            deltaValue = deltaValue >> 1;
328
5.44k
            deltaValue = deltaValue + 1;
329
10.8k
            pixel = WINPR_ASSERTING_INT_CAST(int16_t, -1 * (int16_t)deltaValue);
330
10.8k
          }
331
39.7k
          else
332
39.7k
          {
333
39.7k
            deltaValue = deltaValue >> 1;
334
79.5k
            pixel = WINPR_ASSERTING_INT_CAST(INT16, deltaValue);
335
79.5k
          }
336
337
45.2k
          const INT16 delta =
338
90.4k
              WINPR_ASSERTING_INT_CAST(int16_t, previousScanline[x] + pixel);
339
90.4k
          *dstp = clamp(delta);
340
90.4k
          dstp++;
341
90.4k
          x++;
342
90.4k
          cRawBytes--;
343
90.4k
        }
344
345
5.41M
        while (nRunLength > 0)
346
338k
        {
347
338k
          const INT16 deltaValue =
348
677k
              WINPR_ASSERTING_INT_CAST(int16_t, previousScanline[x] + pixel);
349
677k
          *dstp = clamp(deltaValue);
350
677k
          dstp++;
351
677k
          x++;
352
677k
          nRunLength--;
353
677k
        }
354
5.08M
      }
355
5.08M
    }
356
357
13.8k
    previousScanline = currentScanline;
358
13.8k
  }
359
360
567
  return (INT32)(srcp - pSrcData);
361
567
}
362
363
static inline INT32 planar_decompress_plane_rle(const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcSize,
364
                                                BYTE* WINPR_RESTRICT pDstData, UINT32 nDstStep,
365
                                                UINT32 nXDst, UINT32 nYDst, UINT32 nWidth,
366
                                                UINT32 nHeight, UINT32 nChannel, BOOL vFlip)
367
342
{
368
342
  INT32 beg = 0;
369
342
  INT32 end = 0;
370
342
  INT32 inc = 0;
371
342
  BYTE* previousScanline = NULL;
372
342
  const BYTE* srcp = pSrcData;
373
374
342
  WINPR_ASSERT(nHeight <= INT32_MAX);
375
342
  WINPR_ASSERT(nWidth <= INT32_MAX);
376
342
  WINPR_ASSERT(nDstStep <= INT32_MAX);
377
378
342
  if (vFlip)
379
0
  {
380
0
    beg = (INT32)nHeight - 1;
381
0
    end = -1;
382
0
    inc = -1;
383
0
  }
384
342
  else
385
342
  {
386
342
    beg = 0;
387
342
    end = (INT32)nHeight;
388
342
    inc = 1;
389
342
  }
390
391
12.5k
  for (INT32 y = beg; y != end; y += inc)
392
12.1k
  {
393
12.1k
    const intptr_t off = ((1LL * nYDst + y) * nDstStep) + (4LL * nXDst) + nChannel * 1LL;
394
12.1k
    BYTE* dstp = &pDstData[off];
395
12.1k
    BYTE* currentScanline = dstp;
396
12.1k
    INT16 pixel = 0;
397
398
62.0k
    for (INT32 x = 0; x < (INT32)nWidth;)
399
49.8k
    {
400
49.8k
      const BYTE controlByte = *srcp;
401
49.8k
      srcp++;
402
403
49.8k
      if ((srcp - pSrcData) > SrcSize * 1ll)
404
0
      {
405
0
        WLog_ERR(TAG, "error reading input buffer");
406
0
        return -1;
407
0
      }
408
409
49.8k
      UINT32 nRunLength = PLANAR_CONTROL_BYTE_RUN_LENGTH(controlByte);
410
49.8k
      UINT32 cRawBytes = PLANAR_CONTROL_BYTE_RAW_BYTES(controlByte);
411
412
49.8k
      if (nRunLength == 1)
413
5.71k
      {
414
5.71k
        nRunLength = cRawBytes + 16;
415
5.71k
        cRawBytes = 0;
416
5.71k
      }
417
44.1k
      else if (nRunLength == 2)
418
9.75k
      {
419
9.75k
        nRunLength = cRawBytes + 32;
420
9.75k
        cRawBytes = 0;
421
9.75k
      }
422
423
49.8k
      if (((dstp + (cRawBytes + nRunLength)) - currentScanline) > nWidth * 4ll)
424
0
      {
425
0
        WLog_ERR(TAG, "too many pixels in scanline");
426
0
        return -1;
427
0
      }
428
429
49.8k
      if (!previousScanline)
430
1.76k
      {
431
        /* first scanline, absolute values */
432
2.66k
        while (cRawBytes > 0)
433
893
        {
434
893
          pixel = *srcp;
435
893
          srcp++;
436
1.78k
          *dstp = WINPR_ASSERTING_INT_CAST(BYTE, pixel);
437
1.78k
          dstp += 4;
438
1.78k
          x++;
439
1.78k
          cRawBytes--;
440
1.78k
        }
441
442
13.0k
        while (nRunLength > 0)
443
11.2k
        {
444
22.5k
          *dstp = WINPR_ASSERTING_INT_CAST(BYTE, pixel);
445
22.5k
          dstp += 4;
446
22.5k
          x++;
447
22.5k
          nRunLength--;
448
22.5k
        }
449
1.76k
      }
450
48.1k
      else
451
48.1k
      {
452
        /* delta values relative to previous scanline */
453
82.2k
        while (cRawBytes > 0)
454
34.1k
        {
455
34.1k
          BYTE deltaValue = *srcp;
456
34.1k
          srcp++;
457
458
34.1k
          if (deltaValue & 1)
459
834
          {
460
834
            deltaValue = deltaValue >> 1;
461
834
            deltaValue = deltaValue + 1;
462
1.66k
            pixel = WINPR_ASSERTING_INT_CAST(int16_t, -deltaValue);
463
1.66k
          }
464
33.2k
          else
465
33.2k
          {
466
33.2k
            deltaValue = deltaValue >> 1;
467
33.2k
            pixel = deltaValue;
468
33.2k
          }
469
470
34.1k
          const INT16 delta =
471
68.2k
              WINPR_ASSERTING_INT_CAST(int16_t, previousScanline[4LL * x] + pixel);
472
68.2k
          *dstp = clamp(delta);
473
68.2k
          dstp += 4;
474
68.2k
          x++;
475
68.2k
          cRawBytes--;
476
68.2k
        }
477
478
471k
        while (nRunLength > 0)
479
423k
        {
480
423k
          const INT16 deltaValue =
481
847k
              WINPR_ASSERTING_INT_CAST(int16_t, pixel + previousScanline[4LL * x]);
482
847k
          *dstp = clamp(deltaValue);
483
847k
          dstp += 4;
484
847k
          x++;
485
847k
          nRunLength--;
486
847k
        }
487
48.1k
      }
488
49.8k
    }
489
490
12.1k
    previousScanline = currentScanline;
491
12.1k
  }
492
493
342
  return (INT32)(srcp - pSrcData);
494
342
}
495
496
static inline INT32 planar_set_plane(BYTE bValue, BYTE* pDstData, UINT32 nDstStep, UINT32 nXDst,
497
                                     UINT32 nYDst, UINT32 nWidth, UINT32 nHeight, UINT32 nChannel,
498
                                     BOOL vFlip)
499
114
{
500
114
  INT32 beg = 0;
501
114
  INT32 end = (INT32)nHeight;
502
114
  INT32 inc = 1;
503
504
114
  WINPR_ASSERT(nHeight <= INT32_MAX);
505
114
  WINPR_ASSERT(nWidth <= INT32_MAX);
506
114
  WINPR_ASSERT(nDstStep <= INT32_MAX);
507
508
114
  if (vFlip)
509
0
  {
510
0
    beg = (INT32)nHeight - 1;
511
0
    end = -1;
512
0
    inc = -1;
513
0
  }
514
515
4.17k
  for (INT32 y = beg; y != end; y += inc)
516
4.06k
  {
517
4.06k
    const intptr_t off = ((1LL * nYDst + y) * nDstStep) + (4LL * nXDst) + nChannel * 1LL;
518
4.06k
    BYTE* dstp = &pDstData[off];
519
520
160k
    for (INT32 x = 0; x < (INT32)nWidth; ++x)
521
156k
    {
522
156k
      *dstp = bValue;
523
156k
      dstp += 4;
524
156k
    }
525
4.06k
  }
526
527
114
  return 0;
528
114
}
529
530
static inline BOOL writeLine(BYTE** WINPR_RESTRICT ppRgba, UINT32 DstFormat, UINT32 width,
531
                             const BYTE** WINPR_RESTRICT ppR, const BYTE** WINPR_RESTRICT ppG,
532
                             const BYTE** WINPR_RESTRICT ppB, const BYTE** WINPR_RESTRICT ppA)
533
44.8k
{
534
44.8k
  WINPR_ASSERT(ppRgba);
535
44.8k
  WINPR_ASSERT(ppR);
536
44.8k
  WINPR_ASSERT(ppG);
537
44.8k
  WINPR_ASSERT(ppB);
538
539
44.8k
  switch (DstFormat)
540
44.8k
  {
541
0
    case PIXEL_FORMAT_BGRA32:
542
0
      for (UINT32 x = 0; x < width; x++)
543
0
      {
544
0
        *(*ppRgba)++ = *(*ppB)++;
545
0
        *(*ppRgba)++ = *(*ppG)++;
546
0
        *(*ppRgba)++ = *(*ppR)++;
547
0
        *(*ppRgba)++ = *(*ppA)++;
548
0
      }
549
550
0
      return TRUE;
551
552
44.8k
    case PIXEL_FORMAT_BGRX32:
553
2.22M
      for (UINT32 x = 0; x < width; x++)
554
2.18M
      {
555
2.18M
        *(*ppRgba)++ = *(*ppB)++;
556
2.18M
        *(*ppRgba)++ = *(*ppG)++;
557
2.18M
        *(*ppRgba)++ = *(*ppR)++;
558
2.18M
        *(*ppRgba)++ = 0xFF;
559
2.18M
      }
560
561
44.8k
      return TRUE;
562
563
0
    default:
564
0
      if (ppA)
565
0
      {
566
0
        for (UINT32 x = 0; x < width; x++)
567
0
        {
568
0
          BYTE alpha = *(*ppA)++;
569
0
          UINT32 color =
570
0
              FreeRDPGetColor(DstFormat, *(*ppR)++, *(*ppG)++, *(*ppB)++, alpha);
571
0
          FreeRDPWriteColor(*ppRgba, DstFormat, color);
572
0
          *ppRgba += FreeRDPGetBytesPerPixel(DstFormat);
573
0
        }
574
0
      }
575
0
      else
576
0
      {
577
0
        const BYTE alpha = 0xFF;
578
579
0
        for (UINT32 x = 0; x < width; x++)
580
0
        {
581
0
          UINT32 color =
582
0
              FreeRDPGetColor(DstFormat, *(*ppR)++, *(*ppG)++, *(*ppB)++, alpha);
583
0
          FreeRDPWriteColor(*ppRgba, DstFormat, color);
584
0
          *ppRgba += FreeRDPGetBytesPerPixel(DstFormat);
585
0
        }
586
0
      }
587
588
0
      return TRUE;
589
44.8k
  }
590
44.8k
}
591
592
static inline BOOL planar_decompress_planes_raw(const BYTE* WINPR_RESTRICT pSrcData[4],
593
                                                BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat,
594
                                                UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
595
                                                UINT32 nWidth, UINT32 nHeight, BOOL vFlip,
596
                                                UINT32 totalHeight)
597
1.03k
{
598
1.03k
  INT32 beg = 0;
599
1.03k
  INT32 end = 0;
600
1.03k
  INT32 inc = 0;
601
1.03k
  const BYTE* pR = pSrcData[0];
602
1.03k
  const BYTE* pG = pSrcData[1];
603
1.03k
  const BYTE* pB = pSrcData[2];
604
1.03k
  const BYTE* pA = pSrcData[3];
605
1.03k
  const UINT32 bpp = FreeRDPGetBytesPerPixel(DstFormat);
606
607
1.03k
  if (vFlip)
608
0
  {
609
0
    beg = WINPR_ASSERTING_INT_CAST(int32_t, nHeight - 1);
610
0
    end = -1;
611
0
    inc = -1;
612
0
  }
613
1.03k
  else
614
1.03k
  {
615
1.03k
    beg = 0;
616
2.07k
    end = WINPR_ASSERTING_INT_CAST(int32_t, nHeight);
617
2.07k
    inc = 1;
618
2.07k
  }
619
620
1.03k
  if (nYDst + nHeight > totalHeight)
621
0
  {
622
0
    WLog_ERR(TAG,
623
0
             "planar plane destination Y %" PRIu32 " + height %" PRIu32
624
0
             " exceeds totalHeight %" PRIu32,
625
0
             nYDst, nHeight, totalHeight);
626
0
    return FALSE;
627
0
  }
628
629
1.03k
  if ((nXDst + nWidth) * bpp > nDstStep)
630
0
  {
631
0
    WLog_ERR(TAG,
632
0
             "planar plane destination (X %" PRIu32 " + width %" PRIu32 ") * bpp %" PRIu32
633
0
             " exceeds stride %" PRIu32,
634
0
             nXDst, nWidth, bpp, nDstStep);
635
0
    return FALSE;
636
0
  }
637
638
45.9k
  for (INT32 y = beg; y != end; y += inc)
639
44.8k
  {
640
44.8k
    BYTE* pRGB = NULL;
641
642
44.8k
    if (y > WINPR_ASSERTING_INT_CAST(INT64, nHeight))
643
0
    {
644
0
      WLog_ERR(TAG, "planar plane destination Y %" PRId32 " exceeds height %" PRIu32, y,
645
0
               nHeight);
646
0
      return FALSE;
647
0
    }
648
649
44.8k
    const intptr_t off = ((1LL * nYDst + y) * nDstStep) + (1LL * nXDst * bpp);
650
44.8k
    pRGB = &pDstData[off];
651
652
44.8k
    if (!writeLine(&pRGB, DstFormat, nWidth, &pR, &pG, &pB, &pA))
653
0
      return FALSE;
654
44.8k
  }
655
656
1.03k
  return TRUE;
657
1.03k
}
658
659
static BOOL planar_subsample_expand(const BYTE* WINPR_RESTRICT plane, size_t planeLength,
660
                                    UINT32 nWidth, UINT32 nHeight, UINT32 nPlaneWidth,
661
                                    UINT32 nPlaneHeight, BYTE* WINPR_RESTRICT deltaPlane)
662
382
{
663
382
  size_t pos = 0;
664
382
  WINPR_UNUSED(planeLength);
665
666
382
  WINPR_ASSERT(plane);
667
382
  WINPR_ASSERT(deltaPlane);
668
669
382
  if (nWidth > nPlaneWidth * 2)
670
0
  {
671
0
    WLog_ERR(TAG, "planar subsample width %" PRIu32 " > PlaneWidth %" PRIu32 " * 2", nWidth,
672
0
             nPlaneWidth);
673
0
    return FALSE;
674
0
  }
675
676
382
  if (nHeight > nPlaneHeight * 2)
677
0
  {
678
0
    WLog_ERR(TAG, "planar subsample height %" PRIu32 " > PlaneHeight %" PRIu32 " * 2", nHeight,
679
0
             nPlaneHeight);
680
0
    return FALSE;
681
0
  }
682
683
14.0k
  for (size_t y = 0; y < nHeight; y++)
684
13.6k
  {
685
13.6k
    const BYTE* src = plane + y / 2 * nPlaneWidth;
686
687
539k
    for (UINT32 x = 0; x < nWidth; x++)
688
526k
    {
689
526k
      deltaPlane[pos++] = src[x / 2];
690
526k
    }
691
13.6k
  }
692
693
382
  return TRUE;
694
382
}
695
696
#if !defined(WITHOUT_FREERDP_3x_DEPRECATED)
697
BOOL planar_decompress(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT planar,
698
                       const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcSize, UINT32 nSrcWidth,
699
                       UINT32 nSrcHeight, BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat,
700
                       UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst, UINT32 nDstWidth,
701
                       UINT32 nDstHeight, BOOL vFlip)
702
10.7k
{
703
10.7k
  return freerdp_bitmap_decompress_planar(planar, pSrcData, SrcSize, nSrcWidth, nSrcHeight,
704
10.7k
                                          pDstData, DstFormat, nDstStep, nXDst, nYDst, nDstWidth,
705
10.7k
                                          nDstHeight, vFlip);
706
10.7k
}
707
#endif
708
709
BOOL freerdp_bitmap_decompress_planar(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT planar,
710
                                      const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcSize,
711
                                      UINT32 nSrcWidth, UINT32 nSrcHeight,
712
                                      BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat,
713
                                      UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst, UINT32 nDstWidth,
714
                                      UINT32 nDstHeight, BOOL vFlip)
715
10.7k
{
716
10.7k
  BOOL useAlpha = FALSE;
717
10.7k
  INT32 status = 0;
718
10.7k
  INT32 rleSizes[4] = { 0, 0, 0, 0 };
719
10.7k
  UINT32 rawSizes[4] = { 0 };
720
10.7k
  UINT32 rawWidths[4] = { 0 };
721
10.7k
  UINT32 rawHeights[4] = { 0 };
722
10.7k
  const BYTE* planes[4] = { 0 };
723
10.7k
  const UINT32 w = MIN(nSrcWidth, nDstWidth);
724
10.7k
  const UINT32 h = MIN(nSrcHeight, nDstHeight);
725
10.7k
  const primitives_t* prims = primitives_get();
726
727
10.7k
  WINPR_ASSERT(planar);
728
10.7k
  WINPR_ASSERT(prims);
729
730
10.7k
  if (planar->maxWidth < nSrcWidth)
731
0
    return FALSE;
732
10.7k
  if (planar->maxHeight < nSrcHeight)
733
0
    return FALSE;
734
735
10.7k
  if (nDstStep <= 0)
736
10.7k
    nDstStep = nDstWidth * FreeRDPGetBytesPerPixel(DstFormat);
737
738
10.7k
  const BYTE* srcp = pSrcData;
739
740
10.7k
  if (!pSrcData)
741
0
  {
742
0
    WLog_ERR(TAG, "Invalid argument pSrcData=NULL");
743
0
    return FALSE;
744
0
  }
745
746
10.7k
  if (!pDstData)
747
0
  {
748
0
    WLog_ERR(TAG, "Invalid argument pDstData=NULL");
749
0
    return FALSE;
750
0
  }
751
752
10.7k
  const BYTE FormatHeader = *srcp++;
753
10.7k
  const BYTE cll = (FormatHeader & PLANAR_FORMAT_HEADER_CLL_MASK);
754
10.7k
  const BYTE cs = (FormatHeader & PLANAR_FORMAT_HEADER_CS) ? TRUE : FALSE;
755
10.7k
  const BYTE rle = (FormatHeader & PLANAR_FORMAT_HEADER_RLE) ? TRUE : FALSE;
756
10.7k
  const BYTE alpha = (FormatHeader & PLANAR_FORMAT_HEADER_NA) ? FALSE : TRUE;
757
758
10.7k
  DstFormat = planar_invert_format(planar, alpha, DstFormat);
759
760
10.7k
  if (alpha)
761
5.47k
    useAlpha = FreeRDPColorHasAlpha(DstFormat);
762
763
  // WLog_INFO(TAG, "CLL: %"PRIu32" CS: %"PRIu8" RLE: %"PRIu8" ALPHA: %"PRIu8"", cll, cs, rle,
764
  // alpha);
765
766
10.7k
  if (!cll && cs)
767
288
  {
768
288
    WLog_ERR(TAG, "Chroma subsampling requires YCoCg and does not work with RGB data");
769
288
    return FALSE; /* Chroma subsampling requires YCoCg */
770
288
  }
771
772
10.4k
  const UINT32 subWidth = (nSrcWidth / 2) + (nSrcWidth % 2);
773
10.4k
  const UINT32 subHeight = (nSrcHeight / 2) + (nSrcHeight % 2);
774
10.4k
  const UINT32 planeSize = nSrcWidth * nSrcHeight;
775
10.4k
  const UINT32 subSize = subWidth * subHeight;
776
777
10.4k
  if (!cs)
778
8.98k
  {
779
8.98k
    rawSizes[0] = planeSize; /* LumaOrRedPlane */
780
8.98k
    rawWidths[0] = nSrcWidth;
781
8.98k
    rawHeights[0] = nSrcHeight;
782
8.98k
    rawSizes[1] = planeSize; /* OrangeChromaOrGreenPlane */
783
8.98k
    rawWidths[1] = nSrcWidth;
784
8.98k
    rawHeights[1] = nSrcHeight;
785
8.98k
    rawSizes[2] = planeSize; /* GreenChromaOrBluePlane */
786
8.98k
    rawWidths[2] = nSrcWidth;
787
8.98k
    rawHeights[2] = nSrcHeight;
788
8.98k
    rawSizes[3] = planeSize; /* AlphaPlane */
789
8.98k
    rawWidths[3] = nSrcWidth;
790
8.98k
    rawHeights[3] = nSrcHeight;
791
8.98k
  }
792
1.48k
  else /* Chroma Subsampling */
793
1.48k
  {
794
1.48k
    rawSizes[0] = planeSize; /* LumaOrRedPlane */
795
1.48k
    rawWidths[0] = nSrcWidth;
796
1.48k
    rawHeights[0] = nSrcHeight;
797
1.48k
    rawSizes[1] = subSize; /* OrangeChromaOrGreenPlane */
798
1.48k
    rawWidths[1] = subWidth;
799
1.48k
    rawHeights[1] = subHeight;
800
1.48k
    rawSizes[2] = subSize; /* GreenChromaOrBluePlane */
801
1.48k
    rawWidths[2] = subWidth;
802
1.48k
    rawHeights[2] = subHeight;
803
1.48k
    rawSizes[3] = planeSize; /* AlphaPlane */
804
1.48k
    rawWidths[3] = nSrcWidth;
805
1.48k
    rawHeights[3] = nSrcHeight;
806
1.48k
  }
807
808
20.9k
  const size_t diff = WINPR_ASSERTING_INT_CAST(size_t, (intptr_t)(srcp - pSrcData));
809
20.9k
  if (SrcSize < diff)
810
0
  {
811
0
    WLog_ERR(TAG, "Size mismatch %" PRIu32 " < %" PRIuz, SrcSize, diff);
812
0
    return FALSE;
813
0
  }
814
815
10.4k
  if (!rle) /* RAW */
816
8.20k
  {
817
818
8.20k
    UINT32 base = planeSize * 3;
819
8.20k
    if (cs)
820
668
      base = planeSize + planeSize / 2;
821
822
8.20k
    if (alpha)
823
4.95k
    {
824
4.95k
      if ((SrcSize - diff) < (planeSize + base))
825
4.47k
      {
826
4.47k
        WLog_ERR(TAG, "Alpha plane size mismatch %" PRIuz " < %" PRIu32, SrcSize - diff,
827
4.47k
                 (planeSize + base));
828
4.47k
        return FALSE;
829
4.47k
      }
830
831
479
      planes[3] = srcp;                    /* AlphaPlane */
832
479
      planes[0] = planes[3] + rawSizes[3]; /* LumaOrRedPlane */
833
479
      planes[1] = planes[0] + rawSizes[0]; /* OrangeChromaOrGreenPlane */
834
479
      planes[2] = planes[1] + rawSizes[1]; /* GreenChromaOrBluePlane */
835
836
479
      if ((planes[2] + rawSizes[2]) > &pSrcData[SrcSize])
837
0
      {
838
0
        WLog_ERR(TAG, "plane size mismatch %p + %" PRIu32 " > %p", planes[2], rawSizes[2],
839
0
                 &pSrcData[SrcSize]);
840
0
        return FALSE;
841
0
      }
842
479
    }
843
3.25k
    else
844
3.25k
    {
845
3.25k
      if ((SrcSize - diff) < base)
846
2.88k
      {
847
2.88k
        WLog_ERR(TAG, "plane size mismatch %" PRIu32 " < %" PRIu32, SrcSize - diff, base);
848
2.88k
        return FALSE;
849
2.88k
      }
850
851
370
      planes[0] = srcp;                    /* LumaOrRedPlane */
852
370
      planes[1] = planes[0] + rawSizes[0]; /* OrangeChromaOrGreenPlane */
853
370
      planes[2] = planes[1] + rawSizes[1]; /* GreenChromaOrBluePlane */
854
855
370
      if ((planes[2] + rawSizes[2]) > &pSrcData[SrcSize])
856
0
      {
857
0
        WLog_ERR(TAG, "plane size mismatch %p + %" PRIu32 " > %p", planes[2], rawSizes[2],
858
0
                 &pSrcData[SrcSize]);
859
0
        return FALSE;
860
0
      }
861
370
    }
862
8.20k
  }
863
2.27k
  else /* RLE */
864
2.27k
  {
865
2.27k
    if (alpha)
866
412
    {
867
412
      planes[3] = srcp;
868
412
      rleSizes[3] = planar_skip_plane_rle(planes[3], (UINT32)(SrcSize - diff), rawWidths[3],
869
412
                                          rawHeights[3]); /* AlphaPlane */
870
871
412
      if (rleSizes[3] < 0)
872
390
        return FALSE;
873
874
22
      planes[0] = planes[3] + rleSizes[3];
875
22
    }
876
1.85k
    else
877
1.85k
      planes[0] = srcp;
878
879
3.76k
    const size_t diff0 = WINPR_ASSERTING_INT_CAST(size_t, (intptr_t)(planes[0] - pSrcData));
880
3.76k
    if (SrcSize < diff0)
881
0
    {
882
0
      WLog_ERR(TAG, "Size mismatch %" PRIu32 " < %" PRIuz, SrcSize, diff0);
883
0
      return FALSE;
884
0
    }
885
1.88k
    rleSizes[0] = planar_skip_plane_rle(planes[0], (UINT32)(SrcSize - diff0), rawWidths[0],
886
1.88k
                                        rawHeights[0]); /* RedPlane */
887
888
1.88k
    if (rleSizes[0] < 0)
889
1.53k
      return FALSE;
890
891
350
    planes[1] = planes[0] + rleSizes[0];
892
893
700
    const size_t diff1 = WINPR_ASSERTING_INT_CAST(size_t, (intptr_t)(planes[1] - pSrcData));
894
700
    if (SrcSize < diff1)
895
0
    {
896
0
      WLog_ERR(TAG, "Size mismatch %" PRIu32 " < %" PRIuz, SrcSize, diff1);
897
0
      return FALSE;
898
0
    }
899
350
    rleSizes[1] = planar_skip_plane_rle(planes[1], (UINT32)(SrcSize - diff1), rawWidths[1],
900
350
                                        rawHeights[1]); /* GreenPlane */
901
902
350
    if (rleSizes[1] < 1)
903
27
      return FALSE;
904
905
323
    planes[2] = planes[1] + rleSizes[1];
906
646
    const size_t diff2 = WINPR_ASSERTING_INT_CAST(size_t, (intptr_t)(planes[2] - pSrcData));
907
646
    if (SrcSize < diff2)
908
0
    {
909
0
      WLog_ERR(TAG, "Size mismatch %" PRIu32 " < %" PRIuz, SrcSize, diff);
910
0
      return FALSE;
911
0
    }
912
323
    rleSizes[2] = planar_skip_plane_rle(planes[2], (UINT32)(SrcSize - diff2), rawWidths[2],
913
323
                                        rawHeights[2]); /* BluePlane */
914
915
323
    if (rleSizes[2] < 1)
916
20
      return FALSE;
917
323
  }
918
919
1.15k
  if (!cll) /* RGB */
920
626
  {
921
626
    UINT32 TempFormat = 0;
922
626
    BYTE* pTempData = pDstData;
923
626
    UINT32 nTempStep = nDstStep;
924
626
    UINT32 nTotalHeight = nYDst + nDstHeight;
925
926
626
    if (useAlpha)
927
0
      TempFormat = PIXEL_FORMAT_BGRA32;
928
626
    else
929
626
      TempFormat = PIXEL_FORMAT_BGRX32;
930
931
626
    TempFormat = planar_invert_format(planar, alpha, TempFormat);
932
933
626
    if ((TempFormat != DstFormat) || (nSrcWidth != nDstWidth) || (nSrcHeight != nDstHeight))
934
626
    {
935
626
      pTempData = planar->pTempData;
936
626
      nTempStep = planar->nTempStep;
937
626
      nTotalHeight = planar->maxHeight;
938
626
    }
939
940
626
    if (!rle) /* RAW */
941
512
    {
942
512
      if (!planar_decompress_planes_raw(planes, pTempData, TempFormat, nTempStep, nXDst,
943
512
                                        nYDst, nSrcWidth, nSrcHeight, vFlip, nTotalHeight))
944
0
        return FALSE;
945
946
512
      if (alpha)
947
286
        srcp += rawSizes[0] + rawSizes[1] + rawSizes[2] + rawSizes[3];
948
226
      else /* NoAlpha */
949
226
        srcp += rawSizes[0] + rawSizes[1] + rawSizes[2];
950
951
512
      if ((SrcSize - (srcp - pSrcData)) == 1)
952
0
        srcp++; /* pad */
953
512
    }
954
114
    else /* RLE */
955
114
    {
956
114
      status = planar_decompress_plane_rle(
957
228
          planes[0], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[0]), pTempData, nTempStep,
958
228
          nXDst, nYDst, nSrcWidth, nSrcHeight, 2, vFlip); /* RedPlane */
959
960
228
      if (status < 0)
961
0
        return FALSE;
962
963
114
      status = planar_decompress_plane_rle(
964
228
          planes[1], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[1]), pTempData, nTempStep,
965
228
          nXDst, nYDst, nSrcWidth, nSrcHeight, 1, vFlip); /* GreenPlane */
966
967
228
      if (status < 0)
968
0
        return FALSE;
969
970
114
      status = planar_decompress_plane_rle(
971
228
          planes[2], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[2]), pTempData, nTempStep,
972
228
          nXDst, nYDst, nSrcWidth, nSrcHeight, 0, vFlip); /* BluePlane */
973
974
228
      if (status < 0)
975
0
        return FALSE;
976
977
114
      srcp += rleSizes[0] + rleSizes[1] + rleSizes[2];
978
979
114
      if (useAlpha)
980
0
      {
981
0
        status = planar_decompress_plane_rle(
982
0
            planes[3], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[3]), pTempData,
983
0
            nTempStep, nXDst, nYDst, nSrcWidth, nSrcHeight, 3, vFlip); /* AlphaPlane */
984
0
      }
985
114
      else
986
114
        status = planar_set_plane(0xFF, pTempData, nTempStep, nXDst, nYDst, nSrcWidth,
987
114
                                  nSrcHeight, 3, vFlip);
988
989
114
      if (status < 0)
990
0
        return FALSE;
991
992
114
      if (alpha)
993
0
        srcp += rleSizes[3];
994
114
    }
995
996
626
    if (pTempData != pDstData)
997
626
    {
998
626
      if (!freerdp_image_copy_no_overlap(pDstData, DstFormat, nDstStep, nXDst, nYDst, w, h,
999
626
                                         pTempData, TempFormat, nTempStep, nXDst, nYDst, NULL,
1000
626
                                         FREERDP_FLIP_NONE))
1001
0
      {
1002
0
        WLog_ERR(TAG, "planar image copy failed");
1003
0
        return FALSE;
1004
0
      }
1005
626
    }
1006
626
  }
1007
526
  else /* YCoCg */
1008
526
  {
1009
526
    UINT32 TempFormat = 0;
1010
526
    BYTE* pTempData = planar->pTempData;
1011
526
    UINT32 nTempStep = planar->nTempStep;
1012
526
    UINT32 nTotalHeight = planar->maxHeight;
1013
526
    BYTE* dst = &pDstData[nXDst * FreeRDPGetBytesPerPixel(DstFormat) + nYDst * nDstStep];
1014
1015
526
    if (useAlpha)
1016
0
      TempFormat = PIXEL_FORMAT_BGRA32;
1017
526
    else
1018
526
      TempFormat = PIXEL_FORMAT_BGRX32;
1019
1020
526
    if (!pTempData)
1021
0
      return FALSE;
1022
1023
526
    if (rle) /* RLE encoded data. Decode and handle it like raw data. */
1024
189
    {
1025
189
      BYTE* rleBuffer[4] = { 0 };
1026
1027
189
      if (!planar->rlePlanesBuffer)
1028
0
        return FALSE;
1029
1030
189
      rleBuffer[3] = planar->rlePlanesBuffer;  /* AlphaPlane */
1031
189
      rleBuffer[0] = rleBuffer[3] + planeSize; /* LumaOrRedPlane */
1032
189
      rleBuffer[1] = rleBuffer[0] + planeSize; /* OrangeChromaOrGreenPlane */
1033
189
      rleBuffer[2] = rleBuffer[1] + planeSize; /* GreenChromaOrBluePlane */
1034
189
      if (useAlpha)
1035
0
      {
1036
0
        status = planar_decompress_plane_rle_only(
1037
0
            planes[3], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[3]), rleBuffer[3],
1038
0
            rawWidths[3], rawHeights[3]); /* AlphaPlane */
1039
1040
0
        if (status < 0)
1041
0
          return FALSE;
1042
0
      }
1043
1044
189
      if (alpha)
1045
5
        srcp += rleSizes[3];
1046
1047
189
      status = planar_decompress_plane_rle_only(
1048
378
          planes[0], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[0]), rleBuffer[0],
1049
378
          rawWidths[0], rawHeights[0]); /* LumaPlane */
1050
1051
378
      if (status < 0)
1052
0
        return FALSE;
1053
1054
189
      status = planar_decompress_plane_rle_only(
1055
378
          planes[1], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[1]), rleBuffer[1],
1056
378
          rawWidths[1], rawHeights[1]); /* OrangeChromaPlane */
1057
1058
378
      if (status < 0)
1059
0
        return FALSE;
1060
1061
189
      status = planar_decompress_plane_rle_only(
1062
378
          planes[2], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[2]), rleBuffer[2],
1063
378
          rawWidths[2], rawHeights[2]); /* GreenChromaPlane */
1064
1065
378
      if (status < 0)
1066
0
        return FALSE;
1067
1068
189
      planes[0] = rleBuffer[0];
1069
189
      planes[1] = rleBuffer[1];
1070
189
      planes[2] = rleBuffer[2];
1071
189
      planes[3] = rleBuffer[3];
1072
189
    }
1073
1074
    /* RAW */
1075
526
    {
1076
526
      if (cs)
1077
191
      { /* Chroma subsampling for Co and Cg:
1078
         * Each pixel contains the value that should be expanded to
1079
         * [2x,2y;2x+1,2y;2x+1,2y+1;2x;2y+1] */
1080
191
        if (!planar_subsample_expand(planes[1], rawSizes[1], nSrcWidth, nSrcHeight,
1081
191
                                     rawWidths[1], rawHeights[1], planar->deltaPlanes[0]))
1082
0
          return FALSE;
1083
1084
191
        planes[1] = planar->deltaPlanes[0];
1085
191
        rawSizes[1] = planeSize; /* OrangeChromaOrGreenPlane */
1086
191
        rawWidths[1] = nSrcWidth;
1087
191
        rawHeights[1] = nSrcHeight;
1088
1089
191
        if (!planar_subsample_expand(planes[2], rawSizes[2], nSrcWidth, nSrcHeight,
1090
191
                                     rawWidths[2], rawHeights[2], planar->deltaPlanes[1]))
1091
0
          return FALSE;
1092
1093
191
        planes[2] = planar->deltaPlanes[1];
1094
191
        rawSizes[2] = planeSize; /* GreenChromaOrBluePlane */
1095
191
        rawWidths[2] = nSrcWidth;
1096
191
        rawHeights[2] = nSrcHeight;
1097
191
      }
1098
1099
526
      if (!planar_decompress_planes_raw(planes, pTempData, TempFormat, nTempStep, nXDst,
1100
526
                                        nYDst, nSrcWidth, nSrcHeight, vFlip, nTotalHeight))
1101
0
        return FALSE;
1102
1103
526
      if (alpha)
1104
198
        srcp += rawSizes[0] + rawSizes[1] + rawSizes[2] + rawSizes[3];
1105
328
      else /* NoAlpha */
1106
328
        srcp += rawSizes[0] + rawSizes[1] + rawSizes[2];
1107
1108
526
      if ((SrcSize - (srcp - pSrcData)) == 1)
1109
0
        srcp++; /* pad */
1110
526
    }
1111
1112
526
    WINPR_ASSERT(prims->YCoCgToRGB_8u_AC4R);
1113
526
    int rc = prims->YCoCgToRGB_8u_AC4R(
1114
1.05k
        pTempData, WINPR_ASSERTING_INT_CAST(int32_t, nTempStep), dst, DstFormat,
1115
1.05k
        WINPR_ASSERTING_INT_CAST(int32_t, nDstStep), w, h, cll, useAlpha);
1116
1.05k
    if (rc != PRIMITIVES_SUCCESS)
1117
0
    {
1118
0
      WLog_ERR(TAG, "YCoCgToRGB_8u_AC4R failed with %d", rc);
1119
0
      return FALSE;
1120
0
    }
1121
1.05k
  }
1122
1123
1.15k
  WINPR_UNUSED(srcp);
1124
1.15k
  return TRUE;
1125
1.15k
}
1126
1127
static inline BOOL freerdp_split_color_planes(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT planar,
1128
                                              const BYTE* WINPR_RESTRICT data, UINT32 format,
1129
                                              UINT32 width, UINT32 height, UINT32 scanline,
1130
                                              BYTE* WINPR_RESTRICT planes[4])
1131
0
{
1132
0
  WINPR_ASSERT(planar);
1133
1134
0
  if ((width > INT32_MAX) || (height > INT32_MAX) || (scanline > INT32_MAX))
1135
0
    return FALSE;
1136
1137
0
  if (scanline == 0)
1138
0
    scanline = width * FreeRDPGetBytesPerPixel(format);
1139
1140
0
  if (planar->topdown)
1141
0
  {
1142
0
    UINT32 k = 0;
1143
0
    for (UINT32 i = 0; i < height; i++)
1144
0
    {
1145
0
      const BYTE* pixel = &data[1ULL * scanline * i];
1146
1147
0
      for (UINT32 j = 0; j < width; j++)
1148
0
      {
1149
0
        const UINT32 color = FreeRDPReadColor(pixel, format);
1150
0
        pixel += FreeRDPGetBytesPerPixel(format);
1151
0
        FreeRDPSplitColor(color, format, &planes[1][k], &planes[2][k], &planes[3][k],
1152
0
                          &planes[0][k], NULL);
1153
0
        k++;
1154
0
      }
1155
0
    }
1156
0
  }
1157
0
  else
1158
0
  {
1159
0
    UINT32 k = 0;
1160
1161
0
    for (INT64 i = (INT64)height - 1; i >= 0; i--)
1162
0
    {
1163
0
      const BYTE* pixel = &data[1ULL * scanline * (UINT32)i];
1164
1165
0
      for (UINT32 j = 0; j < width; j++)
1166
0
      {
1167
0
        const UINT32 color = FreeRDPReadColor(pixel, format);
1168
0
        pixel += FreeRDPGetBytesPerPixel(format);
1169
0
        FreeRDPSplitColor(color, format, &planes[1][k], &planes[2][k], &planes[3][k],
1170
0
                          &planes[0][k], NULL);
1171
0
        k++;
1172
0
      }
1173
0
    }
1174
0
  }
1175
0
  return TRUE;
1176
0
}
1177
1178
static inline UINT32 freerdp_bitmap_planar_write_rle_bytes(const BYTE* WINPR_RESTRICT pInBuffer,
1179
                                                           UINT32 cRawBytes, UINT32 nRunLength,
1180
                                                           BYTE* WINPR_RESTRICT pOutBuffer,
1181
                                                           UINT32 outBufferSize)
1182
0
{
1183
0
  const BYTE* pInput = pInBuffer;
1184
0
  BYTE* pOutput = pOutBuffer;
1185
0
  BYTE controlByte = 0;
1186
0
  UINT32 nBytesToWrite = 0;
1187
1188
0
  if (!cRawBytes && !nRunLength)
1189
0
    return 0;
1190
1191
0
  if (nRunLength < 3)
1192
0
  {
1193
0
    cRawBytes += nRunLength;
1194
0
    nRunLength = 0;
1195
0
  }
1196
1197
0
  while (cRawBytes)
1198
0
  {
1199
0
    if (cRawBytes < 16)
1200
0
    {
1201
0
      if (nRunLength > 15)
1202
0
      {
1203
0
        if (nRunLength < 18)
1204
0
        {
1205
0
          controlByte = PLANAR_CONTROL_BYTE(13, cRawBytes);
1206
0
          nRunLength -= 13;
1207
0
          cRawBytes = 0;
1208
0
        }
1209
0
        else
1210
0
        {
1211
0
          controlByte = PLANAR_CONTROL_BYTE(15, cRawBytes);
1212
0
          nRunLength -= 15;
1213
0
          cRawBytes = 0;
1214
0
        }
1215
0
      }
1216
0
      else
1217
0
      {
1218
0
        controlByte = PLANAR_CONTROL_BYTE(nRunLength, cRawBytes);
1219
0
        nRunLength = 0;
1220
0
        cRawBytes = 0;
1221
0
      }
1222
0
    }
1223
0
    else
1224
0
    {
1225
0
      controlByte = PLANAR_CONTROL_BYTE(0, 15);
1226
0
      cRawBytes -= 15;
1227
0
    }
1228
1229
0
    if (outBufferSize < 1)
1230
0
      return 0;
1231
1232
0
    outBufferSize--;
1233
0
    *pOutput = controlByte;
1234
0
    pOutput++;
1235
0
    nBytesToWrite = (controlByte >> 4);
1236
1237
0
    if (nBytesToWrite)
1238
0
    {
1239
0
      if (outBufferSize < nBytesToWrite)
1240
0
        return 0;
1241
1242
0
      outBufferSize -= nBytesToWrite;
1243
0
      CopyMemory(pOutput, pInput, nBytesToWrite);
1244
0
      pOutput += nBytesToWrite;
1245
0
      pInput += nBytesToWrite;
1246
0
    }
1247
0
  }
1248
1249
0
  while (nRunLength)
1250
0
  {
1251
0
    if (nRunLength > 47)
1252
0
    {
1253
0
      if (nRunLength < 50)
1254
0
      {
1255
0
        controlByte = PLANAR_CONTROL_BYTE(2, 13);
1256
0
        nRunLength -= 45;
1257
0
      }
1258
0
      else
1259
0
      {
1260
0
        controlByte = PLANAR_CONTROL_BYTE(2, 15);
1261
0
        nRunLength -= 47;
1262
0
      }
1263
0
    }
1264
0
    else if (nRunLength > 31)
1265
0
    {
1266
0
      controlByte = PLANAR_CONTROL_BYTE(2, (nRunLength - 32));
1267
0
      nRunLength = 0;
1268
0
    }
1269
0
    else if (nRunLength > 15)
1270
0
    {
1271
0
      controlByte = PLANAR_CONTROL_BYTE(1, (nRunLength - 16));
1272
0
      nRunLength = 0;
1273
0
    }
1274
0
    else
1275
0
    {
1276
0
      controlByte = PLANAR_CONTROL_BYTE(nRunLength, 0);
1277
0
      nRunLength = 0;
1278
0
    }
1279
1280
0
    if (outBufferSize < 1)
1281
0
      return 0;
1282
1283
0
    --outBufferSize;
1284
0
    *pOutput = controlByte;
1285
0
    pOutput++;
1286
0
  }
1287
1288
0
  const intptr_t diff = (pOutput - pOutBuffer);
1289
0
  if ((diff < 0) || (diff > UINT32_MAX))
1290
0
    return 0;
1291
0
  return (UINT32)diff;
1292
0
}
1293
1294
static inline UINT32 freerdp_bitmap_planar_encode_rle_bytes(const BYTE* WINPR_RESTRICT pInBuffer,
1295
                                                            UINT32 inBufferSize,
1296
                                                            BYTE* WINPR_RESTRICT pOutBuffer,
1297
                                                            UINT32 outBufferSize)
1298
0
{
1299
0
  BYTE symbol = 0;
1300
0
  const BYTE* pInput = pInBuffer;
1301
0
  BYTE* pOutput = pOutBuffer;
1302
0
  const BYTE* pBytes = NULL;
1303
0
  UINT32 cRawBytes = 0;
1304
0
  UINT32 nRunLength = 0;
1305
0
  UINT32 nBytesWritten = 0;
1306
0
  UINT32 nTotalBytesWritten = 0;
1307
1308
0
  if (!outBufferSize)
1309
0
    return 0;
1310
1311
0
  do
1312
0
  {
1313
0
    if (!inBufferSize)
1314
0
      break;
1315
1316
0
    const UINT32 bSymbolMatch = (symbol == *pInput) ? TRUE : FALSE;
1317
0
    symbol = *pInput;
1318
0
    pInput++;
1319
0
    inBufferSize--;
1320
1321
0
    if (nRunLength && !bSymbolMatch)
1322
0
    {
1323
0
      if (nRunLength < 3)
1324
0
      {
1325
0
        cRawBytes += nRunLength;
1326
0
        nRunLength = 0;
1327
0
      }
1328
0
      else
1329
0
      {
1330
0
        pBytes = pInput - (cRawBytes + nRunLength + 1);
1331
0
        nBytesWritten = freerdp_bitmap_planar_write_rle_bytes(pBytes, cRawBytes, nRunLength,
1332
0
                                                              pOutput, outBufferSize);
1333
0
        nRunLength = 0;
1334
1335
0
        if (!nBytesWritten || (nBytesWritten > outBufferSize))
1336
0
          return nRunLength;
1337
1338
0
        nTotalBytesWritten += nBytesWritten;
1339
0
        outBufferSize -= nBytesWritten;
1340
0
        pOutput += nBytesWritten;
1341
0
        cRawBytes = 0;
1342
0
      }
1343
0
    }
1344
1345
0
    nRunLength += bSymbolMatch;
1346
0
    cRawBytes += (!bSymbolMatch) ? TRUE : FALSE;
1347
0
  } while (outBufferSize);
1348
1349
0
  if (cRawBytes || nRunLength)
1350
0
  {
1351
0
    pBytes = pInput - (cRawBytes + nRunLength);
1352
0
    nBytesWritten = freerdp_bitmap_planar_write_rle_bytes(pBytes, cRawBytes, nRunLength,
1353
0
                                                          pOutput, outBufferSize);
1354
1355
0
    if (!nBytesWritten)
1356
0
      return 0;
1357
1358
0
    nTotalBytesWritten += nBytesWritten;
1359
0
  }
1360
1361
0
  if (inBufferSize)
1362
0
    return 0;
1363
1364
0
  return nTotalBytesWritten;
1365
0
}
1366
1367
BOOL freerdp_bitmap_planar_compress_plane_rle(const BYTE* WINPR_RESTRICT inPlane, UINT32 width,
1368
                                              UINT32 height, BYTE* WINPR_RESTRICT outPlane,
1369
                                              UINT32* WINPR_RESTRICT dstSize)
1370
0
{
1371
0
  if (!outPlane)
1372
0
    return FALSE;
1373
1374
0
  UINT32 index = 0;
1375
0
  const BYTE* pInput = inPlane;
1376
0
  BYTE* pOutput = outPlane;
1377
0
  UINT32 outBufferSize = *dstSize;
1378
0
  UINT32 nTotalBytesWritten = 0;
1379
1380
0
  while (outBufferSize > 0)
1381
0
  {
1382
0
    const UINT32 nBytesWritten =
1383
0
        freerdp_bitmap_planar_encode_rle_bytes(pInput, width, pOutput, outBufferSize);
1384
1385
0
    if ((!nBytesWritten) || (nBytesWritten > outBufferSize))
1386
0
      return FALSE;
1387
1388
0
    outBufferSize -= nBytesWritten;
1389
0
    nTotalBytesWritten += nBytesWritten;
1390
0
    pOutput += nBytesWritten;
1391
0
    pInput += width;
1392
0
    index++;
1393
1394
0
    if (index >= height)
1395
0
      break;
1396
0
  }
1397
1398
0
  *dstSize = nTotalBytesWritten;
1399
0
  return TRUE;
1400
0
}
1401
1402
static inline BOOL freerdp_bitmap_planar_compress_planes_rle(BYTE* WINPR_RESTRICT inPlanes[4],
1403
                                                             UINT32 width, UINT32 height,
1404
                                                             BYTE* WINPR_RESTRICT outPlanes,
1405
                                                             UINT32* WINPR_RESTRICT dstSizes,
1406
                                                             BOOL skipAlpha)
1407
0
{
1408
0
  UINT32 outPlanesSize = width * height * 4;
1409
1410
  /* AlphaPlane */
1411
0
  if (skipAlpha)
1412
0
  {
1413
0
    dstSizes[0] = 0;
1414
0
  }
1415
0
  else
1416
0
  {
1417
0
    dstSizes[0] = outPlanesSize;
1418
1419
0
    if (!freerdp_bitmap_planar_compress_plane_rle(inPlanes[0], width, height, outPlanes,
1420
0
                                                  &dstSizes[0]))
1421
0
      return FALSE;
1422
1423
0
    outPlanes += dstSizes[0];
1424
0
    outPlanesSize -= dstSizes[0];
1425
0
  }
1426
1427
  /* LumaOrRedPlane */
1428
0
  dstSizes[1] = outPlanesSize;
1429
1430
0
  if (!freerdp_bitmap_planar_compress_plane_rle(inPlanes[1], width, height, outPlanes,
1431
0
                                                &dstSizes[1]))
1432
0
    return FALSE;
1433
1434
0
  outPlanes += dstSizes[1];
1435
0
  outPlanesSize -= dstSizes[1];
1436
  /* OrangeChromaOrGreenPlane */
1437
0
  dstSizes[2] = outPlanesSize;
1438
1439
0
  if (!freerdp_bitmap_planar_compress_plane_rle(inPlanes[2], width, height, outPlanes,
1440
0
                                                &dstSizes[2]))
1441
0
    return FALSE;
1442
1443
0
  outPlanes += dstSizes[2];
1444
0
  outPlanesSize -= dstSizes[2];
1445
  /* GreenChromeOrBluePlane */
1446
0
  dstSizes[3] = outPlanesSize;
1447
1448
0
  if (!freerdp_bitmap_planar_compress_plane_rle(inPlanes[3], width, height, outPlanes,
1449
0
                                                &dstSizes[3]))
1450
0
    return FALSE;
1451
1452
0
  return TRUE;
1453
0
}
1454
1455
BYTE* freerdp_bitmap_planar_delta_encode_plane(const BYTE* WINPR_RESTRICT inPlane, UINT32 width,
1456
                                               UINT32 height, BYTE* WINPR_RESTRICT outPlane)
1457
0
{
1458
0
  if (!outPlane)
1459
0
  {
1460
0
    if (width * height == 0)
1461
0
      return NULL;
1462
1463
0
    outPlane = (BYTE*)calloc(height, width);
1464
0
    if (!outPlane)
1465
0
      return NULL;
1466
0
  }
1467
1468
  // first line is copied as is
1469
0
  CopyMemory(outPlane, inPlane, width);
1470
1471
0
  for (UINT32 y = 1; y < height; y++)
1472
0
  {
1473
0
    const size_t off = 1ull * width * y;
1474
0
    BYTE* outPtr = &outPlane[off];
1475
0
    const BYTE* srcPtr = &inPlane[off];
1476
0
    const BYTE* prevLinePtr = &inPlane[off - width];
1477
0
    for (UINT32 x = 0; x < width; x++)
1478
0
    {
1479
0
      const int delta = (int)srcPtr[x] - (int)prevLinePtr[x];
1480
0
      const int s2c1i = (delta >= 0) ? delta : (INT_MAX + delta) + 1;
1481
0
      const int8_t s2c1 = WINPR_CXX_COMPAT_CAST(int8_t, s2c1i);
1482
0
      const uint32_t s2c =
1483
0
          (s2c1 >= 0) ? ((UINT32)s2c1 << 1) : (((UINT32)(~(s2c1) + 1) << 1) - 1);
1484
0
      outPtr[x] = (BYTE)s2c;
1485
0
    }
1486
0
  }
1487
1488
0
  return outPlane;
1489
0
}
1490
1491
static inline BOOL freerdp_bitmap_planar_delta_encode_planes(BYTE* WINPR_RESTRICT inPlanes[4],
1492
                                                             UINT32 width, UINT32 height,
1493
                                                             BYTE* WINPR_RESTRICT outPlanes[4])
1494
0
{
1495
0
  for (UINT32 i = 0; i < 4; i++)
1496
0
  {
1497
0
    outPlanes[i] =
1498
0
        freerdp_bitmap_planar_delta_encode_plane(inPlanes[i], width, height, outPlanes[i]);
1499
1500
0
    if (!outPlanes[i])
1501
0
      return FALSE;
1502
0
  }
1503
1504
0
  return TRUE;
1505
0
}
1506
1507
BYTE* freerdp_bitmap_compress_planar(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT context,
1508
                                     const BYTE* WINPR_RESTRICT data, UINT32 format, UINT32 width,
1509
                                     UINT32 height, UINT32 scanline, BYTE* WINPR_RESTRICT dstData,
1510
                                     UINT32* WINPR_RESTRICT pDstSize)
1511
0
{
1512
0
  UINT32 size = 0;
1513
0
  BYTE* dstp = NULL;
1514
0
  UINT32 dstSizes[4] = { 0 };
1515
0
  BYTE FormatHeader = 0;
1516
1517
0
  if (!context || !context->rlePlanesBuffer)
1518
0
    return NULL;
1519
1520
0
  if (context->AllowSkipAlpha)
1521
0
    FormatHeader |= PLANAR_FORMAT_HEADER_NA;
1522
1523
0
  const UINT32 planeSize = width * height;
1524
1525
0
  if (!context->AllowSkipAlpha)
1526
0
    format = planar_invert_format(context, TRUE, format);
1527
1528
0
  if (!freerdp_split_color_planes(context, data, format, width, height, scanline,
1529
0
                                  context->planes))
1530
0
    return NULL;
1531
1532
0
  if (context->AllowRunLengthEncoding)
1533
0
  {
1534
0
    if (!freerdp_bitmap_planar_delta_encode_planes(context->planes, width, height,
1535
0
                                                   context->deltaPlanes))
1536
0
      return NULL;
1537
1538
0
    if (!freerdp_bitmap_planar_compress_planes_rle(context->deltaPlanes, width, height,
1539
0
                                                   context->rlePlanesBuffer, dstSizes,
1540
0
                                                   context->AllowSkipAlpha))
1541
0
      return NULL;
1542
1543
0
    {
1544
0
      uint32_t offset = 0;
1545
0
      FormatHeader |= PLANAR_FORMAT_HEADER_RLE;
1546
0
      context->rlePlanes[0] = &context->rlePlanesBuffer[offset];
1547
0
      offset += dstSizes[0];
1548
0
      context->rlePlanes[1] = &context->rlePlanesBuffer[offset];
1549
0
      offset += dstSizes[1];
1550
0
      context->rlePlanes[2] = &context->rlePlanesBuffer[offset];
1551
0
      offset += dstSizes[2];
1552
0
      context->rlePlanes[3] = &context->rlePlanesBuffer[offset];
1553
1554
#if defined(WITH_DEBUG_CODECS)
1555
      WLog_DBG(TAG,
1556
               "R: [%" PRIu32 "/%" PRIu32 "] G: [%" PRIu32 "/%" PRIu32 "] B: [%" PRIu32
1557
               " / %" PRIu32 "] ",
1558
               dstSizes[1], planeSize, dstSizes[2], planeSize, dstSizes[3], planeSize);
1559
#endif
1560
0
    }
1561
0
  }
1562
1563
0
  if (FormatHeader & PLANAR_FORMAT_HEADER_RLE)
1564
0
  {
1565
0
    if (!context->AllowRunLengthEncoding)
1566
0
      return NULL;
1567
1568
0
    if (context->rlePlanes[0] == NULL)
1569
0
      return NULL;
1570
1571
0
    if (context->rlePlanes[1] == NULL)
1572
0
      return NULL;
1573
1574
0
    if (context->rlePlanes[2] == NULL)
1575
0
      return NULL;
1576
1577
0
    if (context->rlePlanes[3] == NULL)
1578
0
      return NULL;
1579
0
  }
1580
1581
0
  if (!dstData)
1582
0
  {
1583
0
    size = 1;
1584
1585
0
    if (!(FormatHeader & PLANAR_FORMAT_HEADER_NA))
1586
0
    {
1587
0
      if (FormatHeader & PLANAR_FORMAT_HEADER_RLE)
1588
0
        size += dstSizes[0];
1589
0
      else
1590
0
        size += planeSize;
1591
0
    }
1592
1593
0
    if (FormatHeader & PLANAR_FORMAT_HEADER_RLE)
1594
0
      size += (dstSizes[1] + dstSizes[2] + dstSizes[3]);
1595
0
    else
1596
0
      size += (planeSize * 3);
1597
1598
0
    if (!(FormatHeader & PLANAR_FORMAT_HEADER_RLE))
1599
0
      size++;
1600
1601
0
    dstData = malloc(size);
1602
1603
0
    if (!dstData)
1604
0
      return NULL;
1605
1606
0
    *pDstSize = size;
1607
0
  }
1608
1609
0
  dstp = dstData;
1610
0
  *dstp = FormatHeader; /* FormatHeader */
1611
0
  dstp++;
1612
1613
  /* AlphaPlane */
1614
1615
0
  if (!(FormatHeader & PLANAR_FORMAT_HEADER_NA))
1616
0
  {
1617
0
    if (FormatHeader & PLANAR_FORMAT_HEADER_RLE)
1618
0
    {
1619
0
      CopyMemory(dstp, context->rlePlanes[0], dstSizes[0]); /* Alpha */
1620
0
      dstp += dstSizes[0];
1621
0
    }
1622
0
    else
1623
0
    {
1624
0
      CopyMemory(dstp, context->planes[0], planeSize); /* Alpha */
1625
0
      dstp += planeSize;
1626
0
    }
1627
0
  }
1628
1629
  /* LumaOrRedPlane */
1630
1631
0
  if (FormatHeader & PLANAR_FORMAT_HEADER_RLE)
1632
0
  {
1633
0
    CopyMemory(dstp, context->rlePlanes[1], dstSizes[1]); /* Red */
1634
0
    dstp += dstSizes[1];
1635
0
  }
1636
0
  else
1637
0
  {
1638
0
    CopyMemory(dstp, context->planes[1], planeSize); /* Red */
1639
0
    dstp += planeSize;
1640
0
  }
1641
1642
  /* OrangeChromaOrGreenPlane */
1643
1644
0
  if (FormatHeader & PLANAR_FORMAT_HEADER_RLE)
1645
0
  {
1646
0
    CopyMemory(dstp, context->rlePlanes[2], dstSizes[2]); /* Green */
1647
0
    dstp += dstSizes[2];
1648
0
  }
1649
0
  else
1650
0
  {
1651
0
    CopyMemory(dstp, context->planes[2], planeSize); /* Green */
1652
0
    dstp += planeSize;
1653
0
  }
1654
1655
  /* GreenChromeOrBluePlane */
1656
1657
0
  if (FormatHeader & PLANAR_FORMAT_HEADER_RLE)
1658
0
  {
1659
0
    CopyMemory(dstp, context->rlePlanes[3], dstSizes[3]); /* Blue */
1660
0
    dstp += dstSizes[3];
1661
0
  }
1662
0
  else
1663
0
  {
1664
0
    CopyMemory(dstp, context->planes[3], planeSize); /* Blue */
1665
0
    dstp += planeSize;
1666
0
  }
1667
1668
  /* Pad1 (1 byte) */
1669
1670
0
  if (!(FormatHeader & PLANAR_FORMAT_HEADER_RLE))
1671
0
  {
1672
0
    *dstp = 0;
1673
0
    dstp++;
1674
0
  }
1675
1676
0
  const intptr_t diff = (dstp - dstData);
1677
0
  if ((diff < 0) || (diff > UINT32_MAX))
1678
0
  {
1679
0
    free(dstData);
1680
0
    return NULL;
1681
0
  }
1682
0
  size = (UINT32)diff;
1683
0
  *pDstSize = size;
1684
0
  return dstData;
1685
0
}
1686
1687
BOOL freerdp_bitmap_planar_context_reset(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT context,
1688
                                         UINT32 width, UINT32 height)
1689
5.37k
{
1690
5.37k
  if (!context)
1691
0
    return FALSE;
1692
1693
5.37k
  context->bgr = FALSE;
1694
5.37k
  context->maxWidth = PLANAR_ALIGN(width, 4);
1695
5.37k
  context->maxHeight = PLANAR_ALIGN(height, 4);
1696
5.37k
  {
1697
5.37k
    const UINT64 tmp = (UINT64)context->maxWidth * context->maxHeight;
1698
5.37k
    if (tmp > UINT32_MAX)
1699
0
      return FALSE;
1700
5.37k
    context->maxPlaneSize = (UINT32)tmp;
1701
5.37k
  }
1702
1703
5.37k
  if (context->maxWidth > UINT32_MAX / 4)
1704
0
    return FALSE;
1705
5.37k
  context->nTempStep = context->maxWidth * 4;
1706
1707
5.37k
  memset((void*)context->planes, 0, sizeof(context->planes));
1708
5.37k
  memset((void*)context->rlePlanes, 0, sizeof(context->rlePlanes));
1709
5.37k
  memset((void*)context->deltaPlanes, 0, sizeof(context->deltaPlanes));
1710
1711
5.37k
  if (context->maxPlaneSize > 0)
1712
5.37k
  {
1713
5.37k
    void* tmp = winpr_aligned_recalloc(context->planesBuffer, context->maxPlaneSize, 4, 32);
1714
5.37k
    if (!tmp)
1715
0
      return FALSE;
1716
5.37k
    context->planesBuffer = tmp;
1717
1718
5.37k
    tmp = winpr_aligned_recalloc(context->pTempData, context->maxPlaneSize, 6, 32);
1719
5.37k
    if (!tmp)
1720
0
      return FALSE;
1721
5.37k
    context->pTempData = tmp;
1722
1723
5.37k
    tmp = winpr_aligned_recalloc(context->deltaPlanesBuffer, context->maxPlaneSize, 4, 32);
1724
5.37k
    if (!tmp)
1725
0
      return FALSE;
1726
5.37k
    context->deltaPlanesBuffer = tmp;
1727
1728
5.37k
    tmp = winpr_aligned_recalloc(context->rlePlanesBuffer, context->maxPlaneSize, 4, 32);
1729
5.37k
    if (!tmp)
1730
0
      return FALSE;
1731
5.37k
    context->rlePlanesBuffer = tmp;
1732
1733
5.37k
    context->planes[0] = &context->planesBuffer[0ULL * context->maxPlaneSize];
1734
5.37k
    context->planes[1] = &context->planesBuffer[1ULL * context->maxPlaneSize];
1735
5.37k
    context->planes[2] = &context->planesBuffer[2ULL * context->maxPlaneSize];
1736
5.37k
    context->planes[3] = &context->planesBuffer[3ULL * context->maxPlaneSize];
1737
5.37k
    context->deltaPlanes[0] = &context->deltaPlanesBuffer[0ULL * context->maxPlaneSize];
1738
5.37k
    context->deltaPlanes[1] = &context->deltaPlanesBuffer[1ULL * context->maxPlaneSize];
1739
5.37k
    context->deltaPlanes[2] = &context->deltaPlanesBuffer[2ULL * context->maxPlaneSize];
1740
5.37k
    context->deltaPlanes[3] = &context->deltaPlanesBuffer[3ULL * context->maxPlaneSize];
1741
5.37k
  }
1742
5.37k
  return TRUE;
1743
5.37k
}
1744
1745
BITMAP_PLANAR_CONTEXT* freerdp_bitmap_planar_context_new(DWORD flags, UINT32 maxWidth,
1746
                                                         UINT32 maxHeight)
1747
5.37k
{
1748
5.37k
  BITMAP_PLANAR_CONTEXT* context =
1749
5.37k
      (BITMAP_PLANAR_CONTEXT*)winpr_aligned_calloc(1, sizeof(BITMAP_PLANAR_CONTEXT), 32);
1750
1751
5.37k
  if (!context)
1752
0
    return NULL;
1753
1754
5.37k
  if (flags & PLANAR_FORMAT_HEADER_NA)
1755
5.37k
    context->AllowSkipAlpha = TRUE;
1756
1757
5.37k
  if (flags & PLANAR_FORMAT_HEADER_RLE)
1758
5.37k
    context->AllowRunLengthEncoding = TRUE;
1759
1760
5.37k
  if (flags & PLANAR_FORMAT_HEADER_CS)
1761
0
    context->AllowColorSubsampling = TRUE;
1762
1763
5.37k
  context->ColorLossLevel = flags & PLANAR_FORMAT_HEADER_CLL_MASK;
1764
1765
5.37k
  if (context->ColorLossLevel)
1766
0
    context->AllowDynamicColorFidelity = TRUE;
1767
1768
5.37k
  if (!freerdp_bitmap_planar_context_reset(context, maxWidth, maxHeight))
1769
0
  {
1770
0
    WINPR_PRAGMA_DIAG_PUSH
1771
0
    WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
1772
0
    freerdp_bitmap_planar_context_free(context);
1773
0
    WINPR_PRAGMA_DIAG_POP
1774
0
    return NULL;
1775
0
  }
1776
1777
5.37k
  return context;
1778
5.37k
}
1779
1780
void freerdp_bitmap_planar_context_free(BITMAP_PLANAR_CONTEXT* context)
1781
5.37k
{
1782
5.37k
  if (!context)
1783
0
    return;
1784
1785
5.37k
  winpr_aligned_free(context->pTempData);
1786
5.37k
  winpr_aligned_free(context->planesBuffer);
1787
5.37k
  winpr_aligned_free(context->deltaPlanesBuffer);
1788
5.37k
  winpr_aligned_free(context->rlePlanesBuffer);
1789
5.37k
  winpr_aligned_free(context);
1790
5.37k
}
1791
1792
void freerdp_planar_switch_bgr(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT planar, BOOL bgr)
1793
0
{
1794
0
  WINPR_ASSERT(planar);
1795
0
  planar->bgr = bgr;
1796
0
}
1797
1798
void freerdp_planar_topdown_image(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT planar, BOOL topdown)
1799
0
{
1800
0
  WINPR_ASSERT(planar);
1801
0
  planar->topdown = topdown;
1802
0
}