Coverage Report

Created: 2025-10-10 06:46

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
11.4k
  ((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
28.0M
{
106
28.0M
  return (controlByte & 0x0F);
107
28.0M
}
108
static inline BYTE PLANAR_CONTROL_BYTE_RAW_BYTES(UINT32 controlByte)
109
28.0M
{
110
28.0M
  return ((controlByte >> 4) & 0x0F);
111
28.0M
}
112
113
static inline UINT32 planar_invert_format(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT planar, BOOL alpha,
114
                                          UINT32 DstFormat)
115
12.2k
{
116
12.2k
  WINPR_ASSERT(planar);
117
12.2k
  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
12.2k
  return DstFormat;
174
12.2k
}
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
3.39k
{
187
3.39k
  UINT32 used = 0;
188
189
3.39k
  WINPR_ASSERT(pSrcData);
190
191
54.4k
  for (UINT32 y = 0; y < nHeight; y++)
192
53.2k
  {
193
19.8M
    for (UINT32 x = 0; x < nWidth;)
194
19.7M
    {
195
19.7M
      if (used >= SrcSize)
196
507
      {
197
507
        WLog_ERR(TAG, "planar plane used %" PRIu32 " exceeds SrcSize %" PRIu32, used,
198
507
                 SrcSize);
199
507
        return -1;
200
507
      }
201
202
19.7M
      const uint8_t controlByte = pSrcData[used++];
203
19.7M
      uint32_t nRunLength = PLANAR_CONTROL_BYTE_RUN_LENGTH(controlByte);
204
19.7M
      uint32_t cRawBytes = PLANAR_CONTROL_BYTE_RAW_BYTES(controlByte);
205
206
19.7M
      if (nRunLength == 1)
207
17.1k
      {
208
17.1k
        nRunLength = cRawBytes + 16;
209
17.1k
        cRawBytes = 0;
210
17.1k
      }
211
19.7M
      else if (nRunLength == 2)
212
41.9k
      {
213
41.9k
        nRunLength = cRawBytes + 32;
214
41.9k
        cRawBytes = 0;
215
41.9k
      }
216
217
19.7M
      used += cRawBytes;
218
19.7M
      x += cRawBytes;
219
19.7M
      x += nRunLength;
220
221
19.7M
      if (x > nWidth)
222
899
      {
223
899
        WLog_ERR(TAG, "planar plane x %" PRIu32 " exceeds width %" PRIu32, x, nWidth);
224
899
        return -1;
225
899
      }
226
227
19.7M
      if (used > SrcSize)
228
809
      {
229
809
        WLog_ERR(TAG, "planar plane used %" PRIu32 " exceeds SrcSize %" PRIu32, used,
230
809
                 INT32_MAX);
231
809
        return -1;
232
809
      }
233
19.7M
    }
234
53.2k
  }
235
236
1.18k
  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
1.18k
  return (INT32)used;
242
1.18k
}
243
244
static inline UINT8 clamp(INT16 val)
245
1.14M
{
246
1.14M
  return (UINT8)val;
247
1.14M
}
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
612
{
253
612
  BYTE* previousScanline = NULL;
254
612
  const BYTE* srcp = pSrcData;
255
256
612
  WINPR_ASSERT(nHeight <= INT32_MAX);
257
612
  WINPR_ASSERT(nWidth <= INT32_MAX);
258
259
17.7k
  for (UINT32 y = 0; y < nHeight; y++)
260
17.1k
  {
261
17.1k
    BYTE* dstp = &pDstData[1ULL * (y)*nWidth];
262
17.1k
    INT16 pixel = 0;
263
17.1k
    BYTE* currentScanline = dstp;
264
265
6.02M
    for (UINT32 x = 0; x < nWidth;)
266
6.00M
    {
267
6.00M
      BYTE controlByte = *srcp;
268
6.00M
      srcp++;
269
270
6.00M
      if ((srcp - pSrcData) > SrcSize * 1ll)
271
0
      {
272
0
        WLog_ERR(TAG, "error reading input buffer");
273
0
        return -1;
274
0
      }
275
276
6.00M
      UINT32 nRunLength = PLANAR_CONTROL_BYTE_RUN_LENGTH(controlByte);
277
6.00M
      UINT32 cRawBytes = PLANAR_CONTROL_BYTE_RAW_BYTES(controlByte);
278
279
6.00M
      if (nRunLength == 1)
280
5.47k
      {
281
5.47k
        nRunLength = cRawBytes + 16;
282
5.47k
        cRawBytes = 0;
283
5.47k
      }
284
6.00M
      else if (nRunLength == 2)
285
8.88k
      {
286
8.88k
        nRunLength = cRawBytes + 32;
287
8.88k
        cRawBytes = 0;
288
8.88k
      }
289
290
6.00M
      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
6.00M
      if (!previousScanline)
297
4.98k
      {
298
        /* first scanline, absolute values */
299
8.44k
        while (cRawBytes > 0)
300
3.45k
        {
301
3.45k
          pixel = *srcp;
302
3.45k
          srcp++;
303
3.45k
          *dstp = clamp(pixel);
304
3.45k
          dstp++;
305
3.45k
          x++;
306
3.45k
          cRawBytes--;
307
3.45k
        }
308
309
18.6k
        while (nRunLength > 0)
310
13.6k
        {
311
13.6k
          *dstp = clamp(pixel);
312
13.6k
          dstp++;
313
13.6k
          x++;
314
13.6k
          nRunLength--;
315
13.6k
        }
316
4.98k
      }
317
6.00M
      else
318
6.00M
      {
319
        /* delta values relative to previous scanline */
320
6.16M
        while (cRawBytes > 0)
321
165k
        {
322
165k
          UINT8 deltaValue = *srcp;
323
165k
          srcp++;
324
325
165k
          if (deltaValue & 1)
326
2.94k
          {
327
2.94k
            deltaValue = deltaValue >> 1;
328
2.94k
            deltaValue = deltaValue + 1;
329
5.89k
            pixel = WINPR_ASSERTING_INT_CAST(int16_t, -1 * (int16_t)deltaValue);
330
5.89k
          }
331
162k
          else
332
162k
          {
333
162k
            deltaValue = deltaValue >> 1;
334
325k
            pixel = WINPR_ASSERTING_INT_CAST(INT16, deltaValue);
335
325k
          }
336
337
165k
          const INT16 delta =
338
331k
              WINPR_ASSERTING_INT_CAST(int16_t, previousScanline[x] + pixel);
339
331k
          *dstp = clamp(delta);
340
331k
          dstp++;
341
331k
          x++;
342
331k
          cRawBytes--;
343
331k
        }
344
345
6.39M
        while (nRunLength > 0)
346
396k
        {
347
396k
          const INT16 deltaValue =
348
792k
              WINPR_ASSERTING_INT_CAST(int16_t, previousScanline[x] + pixel);
349
792k
          *dstp = clamp(deltaValue);
350
792k
          dstp++;
351
792k
          x++;
352
792k
          nRunLength--;
353
792k
        }
354
6.00M
      }
355
6.00M
    }
356
357
17.1k
    previousScanline = currentScanline;
358
17.1k
  }
359
360
612
  return (INT32)(srcp - pSrcData);
361
612
}
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
441
{
368
441
  INT32 beg = 0;
369
441
  INT32 end = 0;
370
441
  INT32 inc = 0;
371
441
  BYTE* previousScanline = NULL;
372
441
  const BYTE* srcp = pSrcData;
373
374
441
  WINPR_ASSERT(nHeight <= INT32_MAX);
375
441
  WINPR_ASSERT(nWidth <= INT32_MAX);
376
441
  WINPR_ASSERT(nDstStep <= INT32_MAX);
377
378
441
  if (vFlip)
379
0
  {
380
0
    beg = (INT32)nHeight - 1;
381
0
    end = -1;
382
0
    inc = -1;
383
0
  }
384
441
  else
385
441
  {
386
441
    beg = 0;
387
441
    end = (INT32)nHeight;
388
441
    inc = 1;
389
441
  }
390
391
15.8k
  for (INT32 y = beg; y != end; y += inc)
392
15.4k
  {
393
15.4k
    const intptr_t off = ((1LL * nYDst + y) * nDstStep) + (4LL * nXDst) + nChannel * 1LL;
394
15.4k
    BYTE* dstp = &pDstData[off];
395
15.4k
    BYTE* currentScanline = dstp;
396
15.4k
    INT16 pixel = 0;
397
398
2.31M
    for (INT32 x = 0; x < (INT32)nWidth;)
399
2.30M
    {
400
2.30M
      const BYTE controlByte = *srcp;
401
2.30M
      srcp++;
402
403
2.30M
      if ((srcp - pSrcData) > SrcSize * 1ll)
404
0
      {
405
0
        WLog_ERR(TAG, "error reading input buffer");
406
0
        return -1;
407
0
      }
408
409
2.30M
      UINT32 nRunLength = PLANAR_CONTROL_BYTE_RUN_LENGTH(controlByte);
410
2.30M
      UINT32 cRawBytes = PLANAR_CONTROL_BYTE_RAW_BYTES(controlByte);
411
412
2.30M
      if (nRunLength == 1)
413
2.00k
      {
414
2.00k
        nRunLength = cRawBytes + 16;
415
2.00k
        cRawBytes = 0;
416
2.00k
      }
417
2.29M
      else if (nRunLength == 2)
418
12.6k
      {
419
12.6k
        nRunLength = cRawBytes + 32;
420
12.6k
        cRawBytes = 0;
421
12.6k
      }
422
423
2.30M
      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
2.30M
      if (!previousScanline)
430
3.42k
      {
431
        /* first scanline, absolute values */
432
5.97k
        while (cRawBytes > 0)
433
2.55k
        {
434
2.55k
          pixel = *srcp;
435
2.55k
          srcp++;
436
5.10k
          *dstp = WINPR_ASSERTING_INT_CAST(BYTE, pixel);
437
5.10k
          dstp += 4;
438
5.10k
          x++;
439
5.10k
          cRawBytes--;
440
5.10k
        }
441
442
16.3k
        while (nRunLength > 0)
443
12.9k
        {
444
25.8k
          *dstp = WINPR_ASSERTING_INT_CAST(BYTE, pixel);
445
25.8k
          dstp += 4;
446
25.8k
          x++;
447
25.8k
          nRunLength--;
448
25.8k
        }
449
3.42k
      }
450
2.29M
      else
451
2.29M
      {
452
        /* delta values relative to previous scanline */
453
2.37M
        while (cRawBytes > 0)
454
73.9k
        {
455
73.9k
          BYTE deltaValue = *srcp;
456
73.9k
          srcp++;
457
458
73.9k
          if (deltaValue & 1)
459
7.86k
          {
460
7.86k
            deltaValue = deltaValue >> 1;
461
7.86k
            deltaValue = deltaValue + 1;
462
15.7k
            pixel = WINPR_ASSERTING_INT_CAST(int16_t, -deltaValue);
463
15.7k
          }
464
66.0k
          else
465
66.0k
          {
466
66.0k
            deltaValue = deltaValue >> 1;
467
66.0k
            pixel = deltaValue;
468
66.0k
          }
469
470
73.9k
          const INT16 delta =
471
147k
              WINPR_ASSERTING_INT_CAST(int16_t, previousScanline[4LL * x] + pixel);
472
147k
          *dstp = clamp(delta);
473
147k
          dstp += 4;
474
147k
          x++;
475
147k
          cRawBytes--;
476
147k
        }
477
478
2.78M
        while (nRunLength > 0)
479
491k
        {
480
491k
          const INT16 deltaValue =
481
982k
              WINPR_ASSERTING_INT_CAST(int16_t, pixel + previousScanline[4LL * x]);
482
982k
          *dstp = clamp(deltaValue);
483
982k
          dstp += 4;
484
982k
          x++;
485
982k
          nRunLength--;
486
982k
        }
487
2.29M
      }
488
2.30M
    }
489
490
15.4k
    previousScanline = currentScanline;
491
15.4k
  }
492
493
441
  return (INT32)(srcp - pSrcData);
494
441
}
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
147
{
500
147
  INT32 beg = 0;
501
147
  INT32 end = (INT32)nHeight;
502
147
  INT32 inc = 1;
503
504
147
  WINPR_ASSERT(nHeight <= INT32_MAX);
505
147
  WINPR_ASSERT(nWidth <= INT32_MAX);
506
147
  WINPR_ASSERT(nDstStep <= INT32_MAX);
507
508
147
  if (vFlip)
509
0
  {
510
0
    beg = (INT32)nHeight - 1;
511
0
    end = -1;
512
0
    inc = -1;
513
0
  }
514
515
5.29k
  for (INT32 y = beg; y != end; y += inc)
516
5.15k
  {
517
5.15k
    const intptr_t off = ((1LL * nYDst + y) * nDstStep) + (4LL * nXDst) + nChannel * 1LL;
518
5.15k
    BYTE* dstp = &pDstData[off];
519
520
198k
    for (INT32 x = 0; x < (INT32)nWidth; ++x)
521
193k
    {
522
193k
      *dstp = bValue;
523
193k
      dstp += 4;
524
193k
    }
525
5.15k
  }
526
527
147
  return 0;
528
147
}
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
53.0k
{
534
53.0k
  WINPR_ASSERT(ppRgba);
535
53.0k
  WINPR_ASSERT(ppR);
536
53.0k
  WINPR_ASSERT(ppG);
537
53.0k
  WINPR_ASSERT(ppB);
538
539
53.0k
  switch (DstFormat)
540
53.0k
  {
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
53.0k
    case PIXEL_FORMAT_BGRX32:
553
2.64M
      for (UINT32 x = 0; x < width; x++)
554
2.59M
      {
555
2.59M
        *(*ppRgba)++ = *(*ppB)++;
556
2.59M
        *(*ppRgba)++ = *(*ppG)++;
557
2.59M
        *(*ppRgba)++ = *(*ppR)++;
558
2.59M
        *(*ppRgba)++ = 0xFF;
559
2.59M
      }
560
561
53.0k
      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
53.0k
  }
590
53.0k
}
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.22k
{
598
1.22k
  INT32 beg = 0;
599
1.22k
  INT32 end = 0;
600
1.22k
  INT32 inc = 0;
601
1.22k
  const BYTE* pR = pSrcData[0];
602
1.22k
  const BYTE* pG = pSrcData[1];
603
1.22k
  const BYTE* pB = pSrcData[2];
604
1.22k
  const BYTE* pA = pSrcData[3];
605
1.22k
  const UINT32 bpp = FreeRDPGetBytesPerPixel(DstFormat);
606
607
1.22k
  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.22k
  else
614
1.22k
  {
615
1.22k
    beg = 0;
616
2.44k
    end = WINPR_ASSERTING_INT_CAST(int32_t, nHeight);
617
2.44k
    inc = 1;
618
2.44k
  }
619
620
1.22k
  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.22k
  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
54.2k
  for (INT32 y = beg; y != end; y += inc)
639
53.0k
  {
640
53.0k
    BYTE* pRGB = NULL;
641
642
53.0k
    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
53.0k
    const intptr_t off = ((1LL * nYDst + y) * nDstStep) + (1LL * nXDst * bpp);
650
53.0k
    pRGB = &pDstData[off];
651
652
53.0k
    if (!writeLine(&pRGB, DstFormat, nWidth, &pR, &pG, &pB, &pA))
653
0
      return FALSE;
654
53.0k
  }
655
656
1.22k
  return TRUE;
657
1.22k
}
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
374
{
663
374
  size_t pos = 0;
664
374
  WINPR_UNUSED(planeLength);
665
666
374
  WINPR_ASSERT(plane);
667
374
  WINPR_ASSERT(deltaPlane);
668
669
374
  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
374
  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
13.7k
  for (size_t y = 0; y < nHeight; y++)
684
13.3k
  {
685
13.3k
    const BYTE* src = plane + y / 2 * nPlaneWidth;
686
687
531k
    for (UINT32 x = 0; x < nWidth; x++)
688
518k
    {
689
518k
      deltaPlane[pos++] = src[x / 2];
690
518k
    }
691
13.3k
  }
692
693
374
  return TRUE;
694
374
}
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
11.4k
{
703
11.4k
  return freerdp_bitmap_decompress_planar(planar, pSrcData, SrcSize, nSrcWidth, nSrcHeight,
704
11.4k
                                          pDstData, DstFormat, nDstStep, nXDst, nYDst, nDstWidth,
705
11.4k
                                          nDstHeight, vFlip);
706
11.4k
}
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
11.4k
{
716
11.4k
  BOOL useAlpha = FALSE;
717
11.4k
  INT32 status = 0;
718
11.4k
  INT32 rleSizes[4] = { 0, 0, 0, 0 };
719
11.4k
  UINT32 rawSizes[4] = { 0 };
720
11.4k
  UINT32 rawWidths[4] = { 0 };
721
11.4k
  UINT32 rawHeights[4] = { 0 };
722
11.4k
  const BYTE* planes[4] = { 0 };
723
11.4k
  const UINT32 w = MIN(nSrcWidth, nDstWidth);
724
11.4k
  const UINT32 h = MIN(nSrcHeight, nDstHeight);
725
11.4k
  const primitives_t* prims = primitives_get();
726
727
11.4k
  WINPR_ASSERT(planar);
728
11.4k
  WINPR_ASSERT(prims);
729
730
11.4k
  if (nDstStep <= 0)
731
11.4k
    nDstStep = nDstWidth * FreeRDPGetBytesPerPixel(DstFormat);
732
733
11.4k
  const BYTE* srcp = pSrcData;
734
735
11.4k
  if (!pSrcData)
736
0
  {
737
0
    WLog_ERR(TAG, "Invalid argument pSrcData=NULL");
738
0
    return FALSE;
739
0
  }
740
741
11.4k
  if (!pDstData)
742
0
  {
743
0
    WLog_ERR(TAG, "Invalid argument pDstData=NULL");
744
0
    return FALSE;
745
0
  }
746
747
11.4k
  const BYTE FormatHeader = *srcp++;
748
11.4k
  const BYTE cll = (FormatHeader & PLANAR_FORMAT_HEADER_CLL_MASK);
749
11.4k
  const BYTE cs = (FormatHeader & PLANAR_FORMAT_HEADER_CS) ? TRUE : FALSE;
750
11.4k
  const BYTE rle = (FormatHeader & PLANAR_FORMAT_HEADER_RLE) ? TRUE : FALSE;
751
11.4k
  const BYTE alpha = (FormatHeader & PLANAR_FORMAT_HEADER_NA) ? FALSE : TRUE;
752
753
11.4k
  DstFormat = planar_invert_format(planar, alpha, DstFormat);
754
755
11.4k
  if (alpha)
756
6.10k
    useAlpha = FreeRDPColorHasAlpha(DstFormat);
757
758
  // WLog_INFO(TAG, "CLL: %"PRIu32" CS: %"PRIu8" RLE: %"PRIu8" ALPHA: %"PRIu8"", cll, cs, rle,
759
  // alpha);
760
761
11.4k
  if (!cll && cs)
762
276
  {
763
276
    WLog_ERR(TAG, "Chroma subsampling requires YCoCg and does not work with RGB data");
764
276
    return FALSE; /* Chroma subsampling requires YCoCg */
765
276
  }
766
767
11.2k
  const UINT32 subWidth = (nSrcWidth / 2) + (nSrcWidth % 2);
768
11.2k
  const UINT32 subHeight = (nSrcHeight / 2) + (nSrcHeight % 2);
769
11.2k
  const UINT32 planeSize = nSrcWidth * nSrcHeight;
770
11.2k
  const UINT32 subSize = subWidth * subHeight;
771
772
11.2k
  if (!cs)
773
9.72k
  {
774
9.72k
    rawSizes[0] = planeSize; /* LumaOrRedPlane */
775
9.72k
    rawWidths[0] = nSrcWidth;
776
9.72k
    rawHeights[0] = nSrcHeight;
777
9.72k
    rawSizes[1] = planeSize; /* OrangeChromaOrGreenPlane */
778
9.72k
    rawWidths[1] = nSrcWidth;
779
9.72k
    rawHeights[1] = nSrcHeight;
780
9.72k
    rawSizes[2] = planeSize; /* GreenChromaOrBluePlane */
781
9.72k
    rawWidths[2] = nSrcWidth;
782
9.72k
    rawHeights[2] = nSrcHeight;
783
9.72k
    rawSizes[3] = planeSize; /* AlphaPlane */
784
9.72k
    rawWidths[3] = nSrcWidth;
785
9.72k
    rawHeights[3] = nSrcHeight;
786
9.72k
  }
787
1.48k
  else /* Chroma Subsampling */
788
1.48k
  {
789
1.48k
    rawSizes[0] = planeSize; /* LumaOrRedPlane */
790
1.48k
    rawWidths[0] = nSrcWidth;
791
1.48k
    rawHeights[0] = nSrcHeight;
792
1.48k
    rawSizes[1] = subSize; /* OrangeChromaOrGreenPlane */
793
1.48k
    rawWidths[1] = subWidth;
794
1.48k
    rawHeights[1] = subHeight;
795
1.48k
    rawSizes[2] = subSize; /* GreenChromaOrBluePlane */
796
1.48k
    rawWidths[2] = subWidth;
797
1.48k
    rawHeights[2] = subHeight;
798
1.48k
    rawSizes[3] = planeSize; /* AlphaPlane */
799
1.48k
    rawWidths[3] = nSrcWidth;
800
1.48k
    rawHeights[3] = nSrcHeight;
801
1.48k
  }
802
803
22.4k
  const size_t diff = WINPR_ASSERTING_INT_CAST(size_t, (intptr_t)(srcp - pSrcData));
804
22.4k
  if (SrcSize < diff)
805
0
  {
806
0
    WLog_ERR(TAG, "Size mismatch %" PRIu32 " < %" PRIuz, SrcSize, diff);
807
0
    return FALSE;
808
0
  }
809
810
11.2k
  if (!rle) /* RAW */
811
8.64k
  {
812
813
8.64k
    UINT32 base = planeSize * 3;
814
8.64k
    if (cs)
815
654
      base = planeSize + planeSize / 2;
816
817
8.64k
    if (alpha)
818
5.49k
    {
819
5.49k
      if ((SrcSize - diff) < (planeSize + base))
820
4.88k
      {
821
4.88k
        WLog_ERR(TAG, "Alpha plane size mismatch %" PRIuz " < %" PRIu32, SrcSize - diff,
822
4.88k
                 (planeSize + base));
823
4.88k
        return FALSE;
824
4.88k
      }
825
826
607
      planes[3] = srcp;                    /* AlphaPlane */
827
607
      planes[0] = planes[3] + rawSizes[3]; /* LumaOrRedPlane */
828
607
      planes[1] = planes[0] + rawSizes[0]; /* OrangeChromaOrGreenPlane */
829
607
      planes[2] = planes[1] + rawSizes[1]; /* GreenChromaOrBluePlane */
830
831
607
      if ((planes[2] + rawSizes[2]) > &pSrcData[SrcSize])
832
0
      {
833
0
        WLog_ERR(TAG, "plane size mismatch %p + %" PRIu32 " > %p", planes[2], rawSizes[2],
834
0
                 &pSrcData[SrcSize]);
835
0
        return FALSE;
836
0
      }
837
607
    }
838
3.15k
    else
839
3.15k
    {
840
3.15k
      if ((SrcSize - diff) < base)
841
2.74k
      {
842
2.74k
        WLog_ERR(TAG, "plane size mismatch %" PRIu32 " < %" PRIu32, SrcSize - diff, base);
843
2.74k
        return FALSE;
844
2.74k
      }
845
846
409
      planes[0] = srcp;                    /* LumaOrRedPlane */
847
409
      planes[1] = planes[0] + rawSizes[0]; /* OrangeChromaOrGreenPlane */
848
409
      planes[2] = planes[1] + rawSizes[1]; /* GreenChromaOrBluePlane */
849
850
409
      if ((planes[2] + rawSizes[2]) > &pSrcData[SrcSize])
851
0
      {
852
0
        WLog_ERR(TAG, "plane size mismatch %p + %" PRIu32 " > %p", planes[2], rawSizes[2],
853
0
                 &pSrcData[SrcSize]);
854
0
        return FALSE;
855
0
      }
856
409
    }
857
8.64k
  }
858
2.56k
  else /* RLE */
859
2.56k
  {
860
2.56k
    if (alpha)
861
476
    {
862
476
      planes[3] = srcp;
863
476
      rleSizes[3] = planar_skip_plane_rle(planes[3], (UINT32)(SrcSize - diff), rawWidths[3],
864
476
                                          rawHeights[3]); /* AlphaPlane */
865
866
476
      if (rleSizes[3] < 0)
867
446
        return FALSE;
868
869
30
      planes[0] = planes[3] + rleSizes[3];
870
30
    }
871
2.09k
    else
872
2.09k
      planes[0] = srcp;
873
874
4.24k
    const size_t diff0 = WINPR_ASSERTING_INT_CAST(size_t, (intptr_t)(planes[0] - pSrcData));
875
4.24k
    if (SrcSize < diff0)
876
0
    {
877
0
      WLog_ERR(TAG, "Size mismatch %" PRIu32 " < %" PRIuz, SrcSize, diff0);
878
0
      return FALSE;
879
0
    }
880
2.12k
    rleSizes[0] = planar_skip_plane_rle(planes[0], (UINT32)(SrcSize - diff0), rawWidths[0],
881
2.12k
                                        rawHeights[0]); /* RedPlane */
882
883
2.12k
    if (rleSizes[0] < 0)
884
1.69k
      return FALSE;
885
886
422
    planes[1] = planes[0] + rleSizes[0];
887
888
844
    const size_t diff1 = WINPR_ASSERTING_INT_CAST(size_t, (intptr_t)(planes[1] - pSrcData));
889
844
    if (SrcSize < diff1)
890
0
    {
891
0
      WLog_ERR(TAG, "Size mismatch %" PRIu32 " < %" PRIuz, SrcSize, diff1);
892
0
      return FALSE;
893
0
    }
894
422
    rleSizes[1] = planar_skip_plane_rle(planes[1], (UINT32)(SrcSize - diff1), rawWidths[1],
895
422
                                        rawHeights[1]); /* GreenPlane */
896
897
422
    if (rleSizes[1] < 1)
898
44
      return FALSE;
899
900
378
    planes[2] = planes[1] + rleSizes[1];
901
756
    const size_t diff2 = WINPR_ASSERTING_INT_CAST(size_t, (intptr_t)(planes[2] - pSrcData));
902
756
    if (SrcSize < diff2)
903
0
    {
904
0
      WLog_ERR(TAG, "Size mismatch %" PRIu32 " < %" PRIuz, SrcSize, diff);
905
0
      return FALSE;
906
0
    }
907
378
    rleSizes[2] = planar_skip_plane_rle(planes[2], (UINT32)(SrcSize - diff2), rawWidths[2],
908
378
                                        rawHeights[2]); /* BluePlane */
909
910
378
    if (rleSizes[2] < 1)
911
27
      return FALSE;
912
378
  }
913
914
1.36k
  if (!cll) /* RGB */
915
749
  {
916
749
    UINT32 TempFormat = 0;
917
749
    BYTE* pTempData = pDstData;
918
749
    UINT32 nTempStep = nDstStep;
919
749
    UINT32 nTotalHeight = nYDst + nDstHeight;
920
921
749
    if (useAlpha)
922
0
      TempFormat = PIXEL_FORMAT_BGRA32;
923
749
    else
924
749
      TempFormat = PIXEL_FORMAT_BGRX32;
925
926
749
    TempFormat = planar_invert_format(planar, alpha, TempFormat);
927
928
749
    if ((TempFormat != DstFormat) || (nSrcWidth != nDstWidth) || (nSrcHeight != nDstHeight))
929
749
    {
930
749
      pTempData = planar->pTempData;
931
749
      nTempStep = planar->nTempStep;
932
749
      nTotalHeight = planar->maxHeight;
933
749
    }
934
935
749
    if (!rle) /* RAW */
936
602
    {
937
602
      if (!planar_decompress_planes_raw(planes, pTempData, TempFormat, nTempStep, nXDst,
938
602
                                        nYDst, nSrcWidth, nSrcHeight, vFlip, nTotalHeight))
939
0
        return FALSE;
940
941
602
      if (alpha)
942
330
        srcp += rawSizes[0] + rawSizes[1] + rawSizes[2] + rawSizes[3];
943
272
      else /* NoAlpha */
944
272
        srcp += rawSizes[0] + rawSizes[1] + rawSizes[2];
945
946
602
      if ((SrcSize - (srcp - pSrcData)) == 1)
947
0
        srcp++; /* pad */
948
602
    }
949
147
    else /* RLE */
950
147
    {
951
147
      status = planar_decompress_plane_rle(
952
294
          planes[0], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[0]), pTempData, nTempStep,
953
294
          nXDst, nYDst, nSrcWidth, nSrcHeight, 2, vFlip); /* RedPlane */
954
955
294
      if (status < 0)
956
0
        return FALSE;
957
958
147
      status = planar_decompress_plane_rle(
959
294
          planes[1], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[1]), pTempData, nTempStep,
960
294
          nXDst, nYDst, nSrcWidth, nSrcHeight, 1, vFlip); /* GreenPlane */
961
962
294
      if (status < 0)
963
0
        return FALSE;
964
965
147
      status = planar_decompress_plane_rle(
966
294
          planes[2], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[2]), pTempData, nTempStep,
967
294
          nXDst, nYDst, nSrcWidth, nSrcHeight, 0, vFlip); /* BluePlane */
968
969
294
      if (status < 0)
970
0
        return FALSE;
971
972
147
      srcp += rleSizes[0] + rleSizes[1] + rleSizes[2];
973
974
147
      if (useAlpha)
975
0
      {
976
0
        status = planar_decompress_plane_rle(
977
0
            planes[3], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[3]), pTempData,
978
0
            nTempStep, nXDst, nYDst, nSrcWidth, nSrcHeight, 3, vFlip); /* AlphaPlane */
979
0
      }
980
147
      else
981
147
        status = planar_set_plane(0xFF, pTempData, nTempStep, nXDst, nYDst, nSrcWidth,
982
147
                                  nSrcHeight, 3, vFlip);
983
984
147
      if (status < 0)
985
0
        return FALSE;
986
987
147
      if (alpha)
988
0
        srcp += rleSizes[3];
989
147
    }
990
991
749
    if (pTempData != pDstData)
992
749
    {
993
749
      if (!freerdp_image_copy_no_overlap(pDstData, DstFormat, nDstStep, nXDst, nYDst, w, h,
994
749
                                         pTempData, TempFormat, nTempStep, nXDst, nYDst, NULL,
995
749
                                         FREERDP_FLIP_NONE))
996
0
      {
997
0
        WLog_ERR(TAG, "planar image copy failed");
998
0
        return FALSE;
999
0
      }
1000
749
    }
1001
749
  }
1002
618
  else /* YCoCg */
1003
618
  {
1004
618
    UINT32 TempFormat = 0;
1005
618
    BYTE* pTempData = planar->pTempData;
1006
618
    UINT32 nTempStep = planar->nTempStep;
1007
618
    UINT32 nTotalHeight = planar->maxHeight;
1008
618
    BYTE* dst = &pDstData[nXDst * FreeRDPGetBytesPerPixel(DstFormat) + nYDst * nDstStep];
1009
1010
618
    if (useAlpha)
1011
0
      TempFormat = PIXEL_FORMAT_BGRA32;
1012
618
    else
1013
618
      TempFormat = PIXEL_FORMAT_BGRX32;
1014
1015
618
    if (!pTempData)
1016
0
      return FALSE;
1017
1018
618
    if (rle) /* RLE encoded data. Decode and handle it like raw data. */
1019
204
    {
1020
204
      BYTE* rleBuffer[4] = { 0 };
1021
1022
204
      if (!planar->rlePlanesBuffer)
1023
0
        return FALSE;
1024
1025
204
      rleBuffer[3] = planar->rlePlanesBuffer;  /* AlphaPlane */
1026
204
      rleBuffer[0] = rleBuffer[3] + planeSize; /* LumaOrRedPlane */
1027
204
      rleBuffer[1] = rleBuffer[0] + planeSize; /* OrangeChromaOrGreenPlane */
1028
204
      rleBuffer[2] = rleBuffer[1] + planeSize; /* GreenChromaOrBluePlane */
1029
204
      if (useAlpha)
1030
0
      {
1031
0
        status = planar_decompress_plane_rle_only(
1032
0
            planes[3], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[3]), rleBuffer[3],
1033
0
            rawWidths[3], rawHeights[3]); /* AlphaPlane */
1034
1035
0
        if (status < 0)
1036
0
          return FALSE;
1037
0
      }
1038
1039
204
      if (alpha)
1040
7
        srcp += rleSizes[3];
1041
1042
204
      status = planar_decompress_plane_rle_only(
1043
408
          planes[0], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[0]), rleBuffer[0],
1044
408
          rawWidths[0], rawHeights[0]); /* LumaPlane */
1045
1046
408
      if (status < 0)
1047
0
        return FALSE;
1048
1049
204
      status = planar_decompress_plane_rle_only(
1050
408
          planes[1], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[1]), rleBuffer[1],
1051
408
          rawWidths[1], rawHeights[1]); /* OrangeChromaPlane */
1052
1053
408
      if (status < 0)
1054
0
        return FALSE;
1055
1056
204
      status = planar_decompress_plane_rle_only(
1057
408
          planes[2], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[2]), rleBuffer[2],
1058
408
          rawWidths[2], rawHeights[2]); /* GreenChromaPlane */
1059
1060
408
      if (status < 0)
1061
0
        return FALSE;
1062
1063
204
      planes[0] = rleBuffer[0];
1064
204
      planes[1] = rleBuffer[1];
1065
204
      planes[2] = rleBuffer[2];
1066
204
      planes[3] = rleBuffer[3];
1067
204
    }
1068
1069
    /* RAW */
1070
618
    {
1071
618
      if (cs)
1072
187
      { /* Chroma subsampling for Co and Cg:
1073
         * Each pixel contains the value that should be expanded to
1074
         * [2x,2y;2x+1,2y;2x+1,2y+1;2x;2y+1] */
1075
187
        if (!planar_subsample_expand(planes[1], rawSizes[1], nSrcWidth, nSrcHeight,
1076
187
                                     rawWidths[1], rawHeights[1], planar->deltaPlanes[0]))
1077
0
          return FALSE;
1078
1079
187
        planes[1] = planar->deltaPlanes[0];
1080
187
        rawSizes[1] = planeSize; /* OrangeChromaOrGreenPlane */
1081
187
        rawWidths[1] = nSrcWidth;
1082
187
        rawHeights[1] = nSrcHeight;
1083
1084
187
        if (!planar_subsample_expand(planes[2], rawSizes[2], nSrcWidth, nSrcHeight,
1085
187
                                     rawWidths[2], rawHeights[2], planar->deltaPlanes[1]))
1086
0
          return FALSE;
1087
1088
187
        planes[2] = planar->deltaPlanes[1];
1089
187
        rawSizes[2] = planeSize; /* GreenChromaOrBluePlane */
1090
187
        rawWidths[2] = nSrcWidth;
1091
187
        rawHeights[2] = nSrcHeight;
1092
187
      }
1093
1094
618
      if (!planar_decompress_planes_raw(planes, pTempData, TempFormat, nTempStep, nXDst,
1095
618
                                        nYDst, nSrcWidth, nSrcHeight, vFlip, nTotalHeight))
1096
0
        return FALSE;
1097
1098
618
      if (alpha)
1099
284
        srcp += rawSizes[0] + rawSizes[1] + rawSizes[2] + rawSizes[3];
1100
334
      else /* NoAlpha */
1101
334
        srcp += rawSizes[0] + rawSizes[1] + rawSizes[2];
1102
1103
618
      if ((SrcSize - (srcp - pSrcData)) == 1)
1104
0
        srcp++; /* pad */
1105
618
    }
1106
1107
618
    WINPR_ASSERT(prims->YCoCgToRGB_8u_AC4R);
1108
618
    int rc = prims->YCoCgToRGB_8u_AC4R(
1109
1.23k
        pTempData, WINPR_ASSERTING_INT_CAST(int32_t, nTempStep), dst, DstFormat,
1110
1.23k
        WINPR_ASSERTING_INT_CAST(int32_t, nDstStep), w, h, cll, useAlpha);
1111
1.23k
    if (rc != PRIMITIVES_SUCCESS)
1112
0
    {
1113
0
      WLog_ERR(TAG, "YCoCgToRGB_8u_AC4R failed with %d", rc);
1114
0
      return FALSE;
1115
0
    }
1116
1.23k
  }
1117
1118
1.36k
  WINPR_UNUSED(srcp);
1119
1.36k
  return TRUE;
1120
1.36k
}
1121
1122
static inline BOOL freerdp_split_color_planes(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT planar,
1123
                                              const BYTE* WINPR_RESTRICT data, UINT32 format,
1124
                                              UINT32 width, UINT32 height, UINT32 scanline,
1125
                                              BYTE* WINPR_RESTRICT planes[4])
1126
0
{
1127
0
  WINPR_ASSERT(planar);
1128
1129
0
  if ((width > INT32_MAX) || (height > INT32_MAX) || (scanline > INT32_MAX))
1130
0
    return FALSE;
1131
1132
0
  if (scanline == 0)
1133
0
    scanline = width * FreeRDPGetBytesPerPixel(format);
1134
1135
0
  if (planar->topdown)
1136
0
  {
1137
0
    UINT32 k = 0;
1138
0
    for (UINT32 i = 0; i < height; i++)
1139
0
    {
1140
0
      const BYTE* pixel = &data[1ULL * scanline * i];
1141
1142
0
      for (UINT32 j = 0; j < width; j++)
1143
0
      {
1144
0
        const UINT32 color = FreeRDPReadColor(pixel, format);
1145
0
        pixel += FreeRDPGetBytesPerPixel(format);
1146
0
        FreeRDPSplitColor(color, format, &planes[1][k], &planes[2][k], &planes[3][k],
1147
0
                          &planes[0][k], NULL);
1148
0
        k++;
1149
0
      }
1150
0
    }
1151
0
  }
1152
0
  else
1153
0
  {
1154
0
    UINT32 k = 0;
1155
1156
0
    for (INT64 i = (INT64)height - 1; i >= 0; i--)
1157
0
    {
1158
0
      const BYTE* pixel = &data[1ULL * scanline * (UINT32)i];
1159
1160
0
      for (UINT32 j = 0; j < width; j++)
1161
0
      {
1162
0
        const UINT32 color = FreeRDPReadColor(pixel, format);
1163
0
        pixel += FreeRDPGetBytesPerPixel(format);
1164
0
        FreeRDPSplitColor(color, format, &planes[1][k], &planes[2][k], &planes[3][k],
1165
0
                          &planes[0][k], NULL);
1166
0
        k++;
1167
0
      }
1168
0
    }
1169
0
  }
1170
0
  return TRUE;
1171
0
}
1172
1173
static inline UINT32 freerdp_bitmap_planar_write_rle_bytes(const BYTE* WINPR_RESTRICT pInBuffer,
1174
                                                           UINT32 cRawBytes, UINT32 nRunLength,
1175
                                                           BYTE* WINPR_RESTRICT pOutBuffer,
1176
                                                           UINT32 outBufferSize)
1177
0
{
1178
0
  const BYTE* pInput = pInBuffer;
1179
0
  BYTE* pOutput = pOutBuffer;
1180
0
  BYTE controlByte = 0;
1181
0
  UINT32 nBytesToWrite = 0;
1182
1183
0
  if (!cRawBytes && !nRunLength)
1184
0
    return 0;
1185
1186
0
  if (nRunLength < 3)
1187
0
  {
1188
0
    cRawBytes += nRunLength;
1189
0
    nRunLength = 0;
1190
0
  }
1191
1192
0
  while (cRawBytes)
1193
0
  {
1194
0
    if (cRawBytes < 16)
1195
0
    {
1196
0
      if (nRunLength > 15)
1197
0
      {
1198
0
        if (nRunLength < 18)
1199
0
        {
1200
0
          controlByte = PLANAR_CONTROL_BYTE(13, cRawBytes);
1201
0
          nRunLength -= 13;
1202
0
          cRawBytes = 0;
1203
0
        }
1204
0
        else
1205
0
        {
1206
0
          controlByte = PLANAR_CONTROL_BYTE(15, cRawBytes);
1207
0
          nRunLength -= 15;
1208
0
          cRawBytes = 0;
1209
0
        }
1210
0
      }
1211
0
      else
1212
0
      {
1213
0
        controlByte = PLANAR_CONTROL_BYTE(nRunLength, cRawBytes);
1214
0
        nRunLength = 0;
1215
0
        cRawBytes = 0;
1216
0
      }
1217
0
    }
1218
0
    else
1219
0
    {
1220
0
      controlByte = PLANAR_CONTROL_BYTE(0, 15);
1221
0
      cRawBytes -= 15;
1222
0
    }
1223
1224
0
    if (outBufferSize < 1)
1225
0
      return 0;
1226
1227
0
    outBufferSize--;
1228
0
    *pOutput = controlByte;
1229
0
    pOutput++;
1230
0
    nBytesToWrite = (controlByte >> 4);
1231
1232
0
    if (nBytesToWrite)
1233
0
    {
1234
0
      if (outBufferSize < nBytesToWrite)
1235
0
        return 0;
1236
1237
0
      outBufferSize -= nBytesToWrite;
1238
0
      CopyMemory(pOutput, pInput, nBytesToWrite);
1239
0
      pOutput += nBytesToWrite;
1240
0
      pInput += nBytesToWrite;
1241
0
    }
1242
0
  }
1243
1244
0
  while (nRunLength)
1245
0
  {
1246
0
    if (nRunLength > 47)
1247
0
    {
1248
0
      if (nRunLength < 50)
1249
0
      {
1250
0
        controlByte = PLANAR_CONTROL_BYTE(2, 13);
1251
0
        nRunLength -= 45;
1252
0
      }
1253
0
      else
1254
0
      {
1255
0
        controlByte = PLANAR_CONTROL_BYTE(2, 15);
1256
0
        nRunLength -= 47;
1257
0
      }
1258
0
    }
1259
0
    else if (nRunLength > 31)
1260
0
    {
1261
0
      controlByte = PLANAR_CONTROL_BYTE(2, (nRunLength - 32));
1262
0
      nRunLength = 0;
1263
0
    }
1264
0
    else if (nRunLength > 15)
1265
0
    {
1266
0
      controlByte = PLANAR_CONTROL_BYTE(1, (nRunLength - 16));
1267
0
      nRunLength = 0;
1268
0
    }
1269
0
    else
1270
0
    {
1271
0
      controlByte = PLANAR_CONTROL_BYTE(nRunLength, 0);
1272
0
      nRunLength = 0;
1273
0
    }
1274
1275
0
    if (outBufferSize < 1)
1276
0
      return 0;
1277
1278
0
    --outBufferSize;
1279
0
    *pOutput = controlByte;
1280
0
    pOutput++;
1281
0
  }
1282
1283
0
  const intptr_t diff = (pOutput - pOutBuffer);
1284
0
  if ((diff < 0) || (diff > UINT32_MAX))
1285
0
    return 0;
1286
0
  return (UINT32)diff;
1287
0
}
1288
1289
static inline UINT32 freerdp_bitmap_planar_encode_rle_bytes(const BYTE* WINPR_RESTRICT pInBuffer,
1290
                                                            UINT32 inBufferSize,
1291
                                                            BYTE* WINPR_RESTRICT pOutBuffer,
1292
                                                            UINT32 outBufferSize)
1293
0
{
1294
0
  BYTE symbol = 0;
1295
0
  const BYTE* pInput = pInBuffer;
1296
0
  BYTE* pOutput = pOutBuffer;
1297
0
  const BYTE* pBytes = NULL;
1298
0
  UINT32 cRawBytes = 0;
1299
0
  UINT32 nRunLength = 0;
1300
0
  UINT32 nBytesWritten = 0;
1301
0
  UINT32 nTotalBytesWritten = 0;
1302
1303
0
  if (!outBufferSize)
1304
0
    return 0;
1305
1306
0
  do
1307
0
  {
1308
0
    if (!inBufferSize)
1309
0
      break;
1310
1311
0
    const UINT32 bSymbolMatch = (symbol == *pInput) ? TRUE : FALSE;
1312
0
    symbol = *pInput;
1313
0
    pInput++;
1314
0
    inBufferSize--;
1315
1316
0
    if (nRunLength && !bSymbolMatch)
1317
0
    {
1318
0
      if (nRunLength < 3)
1319
0
      {
1320
0
        cRawBytes += nRunLength;
1321
0
        nRunLength = 0;
1322
0
      }
1323
0
      else
1324
0
      {
1325
0
        pBytes = pInput - (cRawBytes + nRunLength + 1);
1326
0
        nBytesWritten = freerdp_bitmap_planar_write_rle_bytes(pBytes, cRawBytes, nRunLength,
1327
0
                                                              pOutput, outBufferSize);
1328
0
        nRunLength = 0;
1329
1330
0
        if (!nBytesWritten || (nBytesWritten > outBufferSize))
1331
0
          return nRunLength;
1332
1333
0
        nTotalBytesWritten += nBytesWritten;
1334
0
        outBufferSize -= nBytesWritten;
1335
0
        pOutput += nBytesWritten;
1336
0
        cRawBytes = 0;
1337
0
      }
1338
0
    }
1339
1340
0
    nRunLength += bSymbolMatch;
1341
0
    cRawBytes += (!bSymbolMatch) ? TRUE : FALSE;
1342
0
  } while (outBufferSize);
1343
1344
0
  if (cRawBytes || nRunLength)
1345
0
  {
1346
0
    pBytes = pInput - (cRawBytes + nRunLength);
1347
0
    nBytesWritten = freerdp_bitmap_planar_write_rle_bytes(pBytes, cRawBytes, nRunLength,
1348
0
                                                          pOutput, outBufferSize);
1349
1350
0
    if (!nBytesWritten)
1351
0
      return 0;
1352
1353
0
    nTotalBytesWritten += nBytesWritten;
1354
0
  }
1355
1356
0
  if (inBufferSize)
1357
0
    return 0;
1358
1359
0
  return nTotalBytesWritten;
1360
0
}
1361
1362
BOOL freerdp_bitmap_planar_compress_plane_rle(const BYTE* WINPR_RESTRICT inPlane, UINT32 width,
1363
                                              UINT32 height, BYTE* WINPR_RESTRICT outPlane,
1364
                                              UINT32* WINPR_RESTRICT dstSize)
1365
0
{
1366
0
  if (!outPlane)
1367
0
    return FALSE;
1368
1369
0
  UINT32 index = 0;
1370
0
  const BYTE* pInput = inPlane;
1371
0
  BYTE* pOutput = outPlane;
1372
0
  UINT32 outBufferSize = *dstSize;
1373
0
  UINT32 nTotalBytesWritten = 0;
1374
1375
0
  while (outBufferSize > 0)
1376
0
  {
1377
0
    const UINT32 nBytesWritten =
1378
0
        freerdp_bitmap_planar_encode_rle_bytes(pInput, width, pOutput, outBufferSize);
1379
1380
0
    if ((!nBytesWritten) || (nBytesWritten > outBufferSize))
1381
0
      return FALSE;
1382
1383
0
    outBufferSize -= nBytesWritten;
1384
0
    nTotalBytesWritten += nBytesWritten;
1385
0
    pOutput += nBytesWritten;
1386
0
    pInput += width;
1387
0
    index++;
1388
1389
0
    if (index >= height)
1390
0
      break;
1391
0
  }
1392
1393
0
  *dstSize = nTotalBytesWritten;
1394
0
  return TRUE;
1395
0
}
1396
1397
static inline BOOL freerdp_bitmap_planar_compress_planes_rle(BYTE* WINPR_RESTRICT inPlanes[4],
1398
                                                             UINT32 width, UINT32 height,
1399
                                                             BYTE* WINPR_RESTRICT outPlanes,
1400
                                                             UINT32* WINPR_RESTRICT dstSizes,
1401
                                                             BOOL skipAlpha)
1402
0
{
1403
0
  UINT32 outPlanesSize = width * height * 4;
1404
1405
  /* AlphaPlane */
1406
0
  if (skipAlpha)
1407
0
  {
1408
0
    dstSizes[0] = 0;
1409
0
  }
1410
0
  else
1411
0
  {
1412
0
    dstSizes[0] = outPlanesSize;
1413
1414
0
    if (!freerdp_bitmap_planar_compress_plane_rle(inPlanes[0], width, height, outPlanes,
1415
0
                                                  &dstSizes[0]))
1416
0
      return FALSE;
1417
1418
0
    outPlanes += dstSizes[0];
1419
0
    outPlanesSize -= dstSizes[0];
1420
0
  }
1421
1422
  /* LumaOrRedPlane */
1423
0
  dstSizes[1] = outPlanesSize;
1424
1425
0
  if (!freerdp_bitmap_planar_compress_plane_rle(inPlanes[1], width, height, outPlanes,
1426
0
                                                &dstSizes[1]))
1427
0
    return FALSE;
1428
1429
0
  outPlanes += dstSizes[1];
1430
0
  outPlanesSize -= dstSizes[1];
1431
  /* OrangeChromaOrGreenPlane */
1432
0
  dstSizes[2] = outPlanesSize;
1433
1434
0
  if (!freerdp_bitmap_planar_compress_plane_rle(inPlanes[2], width, height, outPlanes,
1435
0
                                                &dstSizes[2]))
1436
0
    return FALSE;
1437
1438
0
  outPlanes += dstSizes[2];
1439
0
  outPlanesSize -= dstSizes[2];
1440
  /* GreenChromeOrBluePlane */
1441
0
  dstSizes[3] = outPlanesSize;
1442
1443
0
  if (!freerdp_bitmap_planar_compress_plane_rle(inPlanes[3], width, height, outPlanes,
1444
0
                                                &dstSizes[3]))
1445
0
    return FALSE;
1446
1447
0
  return TRUE;
1448
0
}
1449
1450
BYTE* freerdp_bitmap_planar_delta_encode_plane(const BYTE* WINPR_RESTRICT inPlane, UINT32 width,
1451
                                               UINT32 height, BYTE* WINPR_RESTRICT outPlane)
1452
0
{
1453
0
  if (!outPlane)
1454
0
  {
1455
0
    if (width * height == 0)
1456
0
      return NULL;
1457
1458
0
    outPlane = (BYTE*)calloc(height, width);
1459
0
    if (!outPlane)
1460
0
      return NULL;
1461
0
  }
1462
1463
  // first line is copied as is
1464
0
  CopyMemory(outPlane, inPlane, width);
1465
1466
0
  for (UINT32 y = 1; y < height; y++)
1467
0
  {
1468
0
    const size_t off = 1ull * width * y;
1469
0
    BYTE* outPtr = &outPlane[off];
1470
0
    const BYTE* srcPtr = &inPlane[off];
1471
0
    const BYTE* prevLinePtr = &inPlane[off - width];
1472
0
    for (UINT32 x = 0; x < width; x++)
1473
0
    {
1474
0
      const int delta = (int)srcPtr[x] - (int)prevLinePtr[x];
1475
0
      const int s2c1i = (delta >= 0) ? delta : (INT_MAX + delta) + 1;
1476
0
      const int8_t s2c1 = WINPR_CXX_COMPAT_CAST(int8_t, s2c1i);
1477
0
      const uint32_t s2c =
1478
0
          (s2c1 >= 0) ? ((UINT32)s2c1 << 1) : (((UINT32)(~(s2c1) + 1) << 1) - 1);
1479
0
      outPtr[x] = (BYTE)s2c;
1480
0
    }
1481
0
  }
1482
1483
0
  return outPlane;
1484
0
}
1485
1486
static inline BOOL freerdp_bitmap_planar_delta_encode_planes(BYTE* WINPR_RESTRICT inPlanes[4],
1487
                                                             UINT32 width, UINT32 height,
1488
                                                             BYTE* WINPR_RESTRICT outPlanes[4])
1489
0
{
1490
0
  for (UINT32 i = 0; i < 4; i++)
1491
0
  {
1492
0
    outPlanes[i] =
1493
0
        freerdp_bitmap_planar_delta_encode_plane(inPlanes[i], width, height, outPlanes[i]);
1494
1495
0
    if (!outPlanes[i])
1496
0
      return FALSE;
1497
0
  }
1498
1499
0
  return TRUE;
1500
0
}
1501
1502
BYTE* freerdp_bitmap_compress_planar(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT context,
1503
                                     const BYTE* WINPR_RESTRICT data, UINT32 format, UINT32 width,
1504
                                     UINT32 height, UINT32 scanline, BYTE* WINPR_RESTRICT dstData,
1505
                                     UINT32* WINPR_RESTRICT pDstSize)
1506
0
{
1507
0
  UINT32 size = 0;
1508
0
  BYTE* dstp = NULL;
1509
0
  UINT32 dstSizes[4] = { 0 };
1510
0
  BYTE FormatHeader = 0;
1511
1512
0
  if (!context || !context->rlePlanesBuffer)
1513
0
    return NULL;
1514
1515
0
  if (context->AllowSkipAlpha)
1516
0
    FormatHeader |= PLANAR_FORMAT_HEADER_NA;
1517
1518
0
  const UINT32 planeSize = width * height;
1519
1520
0
  if (!context->AllowSkipAlpha)
1521
0
    format = planar_invert_format(context, TRUE, format);
1522
1523
0
  if (!freerdp_split_color_planes(context, data, format, width, height, scanline,
1524
0
                                  context->planes))
1525
0
    return NULL;
1526
1527
0
  if (context->AllowRunLengthEncoding)
1528
0
  {
1529
0
    if (!freerdp_bitmap_planar_delta_encode_planes(context->planes, width, height,
1530
0
                                                   context->deltaPlanes))
1531
0
      return NULL;
1532
1533
0
    if (!freerdp_bitmap_planar_compress_planes_rle(context->deltaPlanes, width, height,
1534
0
                                                   context->rlePlanesBuffer, dstSizes,
1535
0
                                                   context->AllowSkipAlpha))
1536
0
      return NULL;
1537
1538
0
    {
1539
0
      uint32_t offset = 0;
1540
0
      FormatHeader |= PLANAR_FORMAT_HEADER_RLE;
1541
0
      context->rlePlanes[0] = &context->rlePlanesBuffer[offset];
1542
0
      offset += dstSizes[0];
1543
0
      context->rlePlanes[1] = &context->rlePlanesBuffer[offset];
1544
0
      offset += dstSizes[1];
1545
0
      context->rlePlanes[2] = &context->rlePlanesBuffer[offset];
1546
0
      offset += dstSizes[2];
1547
0
      context->rlePlanes[3] = &context->rlePlanesBuffer[offset];
1548
1549
#if defined(WITH_DEBUG_CODECS)
1550
      WLog_DBG(TAG,
1551
               "R: [%" PRIu32 "/%" PRIu32 "] G: [%" PRIu32 "/%" PRIu32 "] B: [%" PRIu32
1552
               " / %" PRIu32 "] ",
1553
               dstSizes[1], planeSize, dstSizes[2], planeSize, dstSizes[3], planeSize);
1554
#endif
1555
0
    }
1556
0
  }
1557
1558
0
  if (FormatHeader & PLANAR_FORMAT_HEADER_RLE)
1559
0
  {
1560
0
    if (!context->AllowRunLengthEncoding)
1561
0
      return NULL;
1562
1563
0
    if (context->rlePlanes[0] == NULL)
1564
0
      return NULL;
1565
1566
0
    if (context->rlePlanes[1] == NULL)
1567
0
      return NULL;
1568
1569
0
    if (context->rlePlanes[2] == NULL)
1570
0
      return NULL;
1571
1572
0
    if (context->rlePlanes[3] == NULL)
1573
0
      return NULL;
1574
0
  }
1575
1576
0
  if (!dstData)
1577
0
  {
1578
0
    size = 1;
1579
1580
0
    if (!(FormatHeader & PLANAR_FORMAT_HEADER_NA))
1581
0
    {
1582
0
      if (FormatHeader & PLANAR_FORMAT_HEADER_RLE)
1583
0
        size += dstSizes[0];
1584
0
      else
1585
0
        size += planeSize;
1586
0
    }
1587
1588
0
    if (FormatHeader & PLANAR_FORMAT_HEADER_RLE)
1589
0
      size += (dstSizes[1] + dstSizes[2] + dstSizes[3]);
1590
0
    else
1591
0
      size += (planeSize * 3);
1592
1593
0
    if (!(FormatHeader & PLANAR_FORMAT_HEADER_RLE))
1594
0
      size++;
1595
1596
0
    dstData = malloc(size);
1597
1598
0
    if (!dstData)
1599
0
      return NULL;
1600
1601
0
    *pDstSize = size;
1602
0
  }
1603
1604
0
  dstp = dstData;
1605
0
  *dstp = FormatHeader; /* FormatHeader */
1606
0
  dstp++;
1607
1608
  /* AlphaPlane */
1609
1610
0
  if (!(FormatHeader & PLANAR_FORMAT_HEADER_NA))
1611
0
  {
1612
0
    if (FormatHeader & PLANAR_FORMAT_HEADER_RLE)
1613
0
    {
1614
0
      CopyMemory(dstp, context->rlePlanes[0], dstSizes[0]); /* Alpha */
1615
0
      dstp += dstSizes[0];
1616
0
    }
1617
0
    else
1618
0
    {
1619
0
      CopyMemory(dstp, context->planes[0], planeSize); /* Alpha */
1620
0
      dstp += planeSize;
1621
0
    }
1622
0
  }
1623
1624
  /* LumaOrRedPlane */
1625
1626
0
  if (FormatHeader & PLANAR_FORMAT_HEADER_RLE)
1627
0
  {
1628
0
    CopyMemory(dstp, context->rlePlanes[1], dstSizes[1]); /* Red */
1629
0
    dstp += dstSizes[1];
1630
0
  }
1631
0
  else
1632
0
  {
1633
0
    CopyMemory(dstp, context->planes[1], planeSize); /* Red */
1634
0
    dstp += planeSize;
1635
0
  }
1636
1637
  /* OrangeChromaOrGreenPlane */
1638
1639
0
  if (FormatHeader & PLANAR_FORMAT_HEADER_RLE)
1640
0
  {
1641
0
    CopyMemory(dstp, context->rlePlanes[2], dstSizes[2]); /* Green */
1642
0
    dstp += dstSizes[2];
1643
0
  }
1644
0
  else
1645
0
  {
1646
0
    CopyMemory(dstp, context->planes[2], planeSize); /* Green */
1647
0
    dstp += planeSize;
1648
0
  }
1649
1650
  /* GreenChromeOrBluePlane */
1651
1652
0
  if (FormatHeader & PLANAR_FORMAT_HEADER_RLE)
1653
0
  {
1654
0
    CopyMemory(dstp, context->rlePlanes[3], dstSizes[3]); /* Blue */
1655
0
    dstp += dstSizes[3];
1656
0
  }
1657
0
  else
1658
0
  {
1659
0
    CopyMemory(dstp, context->planes[3], planeSize); /* Blue */
1660
0
    dstp += planeSize;
1661
0
  }
1662
1663
  /* Pad1 (1 byte) */
1664
1665
0
  if (!(FormatHeader & PLANAR_FORMAT_HEADER_RLE))
1666
0
  {
1667
0
    *dstp = 0;
1668
0
    dstp++;
1669
0
  }
1670
1671
0
  const intptr_t diff = (dstp - dstData);
1672
0
  if ((diff < 0) || (diff > UINT32_MAX))
1673
0
  {
1674
0
    free(dstData);
1675
0
    return NULL;
1676
0
  }
1677
0
  size = (UINT32)diff;
1678
0
  *pDstSize = size;
1679
0
  return dstData;
1680
0
}
1681
1682
BOOL freerdp_bitmap_planar_context_reset(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT context,
1683
                                         UINT32 width, UINT32 height)
1684
5.74k
{
1685
5.74k
  if (!context)
1686
0
    return FALSE;
1687
1688
5.74k
  context->bgr = FALSE;
1689
5.74k
  context->maxWidth = PLANAR_ALIGN(width, 4);
1690
5.74k
  context->maxHeight = PLANAR_ALIGN(height, 4);
1691
5.74k
  {
1692
5.74k
    const UINT64 tmp = (UINT64)context->maxWidth * context->maxHeight;
1693
5.74k
    if (tmp > UINT32_MAX)
1694
0
      return FALSE;
1695
5.74k
    context->maxPlaneSize = (UINT32)tmp;
1696
5.74k
  }
1697
1698
5.74k
  if (context->maxWidth > UINT32_MAX / 4)
1699
0
    return FALSE;
1700
5.74k
  context->nTempStep = context->maxWidth * 4;
1701
1702
5.74k
  memset((void*)context->planes, 0, sizeof(context->planes));
1703
5.74k
  memset((void*)context->rlePlanes, 0, sizeof(context->rlePlanes));
1704
5.74k
  memset((void*)context->deltaPlanes, 0, sizeof(context->deltaPlanes));
1705
1706
5.74k
  if (context->maxPlaneSize > 0)
1707
5.74k
  {
1708
5.74k
    void* tmp = winpr_aligned_recalloc(context->planesBuffer, context->maxPlaneSize, 4, 32);
1709
5.74k
    if (!tmp)
1710
0
      return FALSE;
1711
5.74k
    context->planesBuffer = tmp;
1712
1713
5.74k
    tmp = winpr_aligned_recalloc(context->pTempData, context->maxPlaneSize, 6, 32);
1714
5.74k
    if (!tmp)
1715
0
      return FALSE;
1716
5.74k
    context->pTempData = tmp;
1717
1718
5.74k
    tmp = winpr_aligned_recalloc(context->deltaPlanesBuffer, context->maxPlaneSize, 4, 32);
1719
5.74k
    if (!tmp)
1720
0
      return FALSE;
1721
5.74k
    context->deltaPlanesBuffer = tmp;
1722
1723
5.74k
    tmp = winpr_aligned_recalloc(context->rlePlanesBuffer, context->maxPlaneSize, 4, 32);
1724
5.74k
    if (!tmp)
1725
0
      return FALSE;
1726
5.74k
    context->rlePlanesBuffer = tmp;
1727
1728
5.74k
    context->planes[0] = &context->planesBuffer[0ULL * context->maxPlaneSize];
1729
5.74k
    context->planes[1] = &context->planesBuffer[1ULL * context->maxPlaneSize];
1730
5.74k
    context->planes[2] = &context->planesBuffer[2ULL * context->maxPlaneSize];
1731
5.74k
    context->planes[3] = &context->planesBuffer[3ULL * context->maxPlaneSize];
1732
5.74k
    context->deltaPlanes[0] = &context->deltaPlanesBuffer[0ULL * context->maxPlaneSize];
1733
5.74k
    context->deltaPlanes[1] = &context->deltaPlanesBuffer[1ULL * context->maxPlaneSize];
1734
5.74k
    context->deltaPlanes[2] = &context->deltaPlanesBuffer[2ULL * context->maxPlaneSize];
1735
5.74k
    context->deltaPlanes[3] = &context->deltaPlanesBuffer[3ULL * context->maxPlaneSize];
1736
5.74k
  }
1737
5.74k
  return TRUE;
1738
5.74k
}
1739
1740
BITMAP_PLANAR_CONTEXT* freerdp_bitmap_planar_context_new(DWORD flags, UINT32 maxWidth,
1741
                                                         UINT32 maxHeight)
1742
5.74k
{
1743
5.74k
  BITMAP_PLANAR_CONTEXT* context =
1744
5.74k
      (BITMAP_PLANAR_CONTEXT*)winpr_aligned_calloc(1, sizeof(BITMAP_PLANAR_CONTEXT), 32);
1745
1746
5.74k
  if (!context)
1747
0
    return NULL;
1748
1749
5.74k
  if (flags & PLANAR_FORMAT_HEADER_NA)
1750
5.74k
    context->AllowSkipAlpha = TRUE;
1751
1752
5.74k
  if (flags & PLANAR_FORMAT_HEADER_RLE)
1753
5.74k
    context->AllowRunLengthEncoding = TRUE;
1754
1755
5.74k
  if (flags & PLANAR_FORMAT_HEADER_CS)
1756
0
    context->AllowColorSubsampling = TRUE;
1757
1758
5.74k
  context->ColorLossLevel = flags & PLANAR_FORMAT_HEADER_CLL_MASK;
1759
1760
5.74k
  if (context->ColorLossLevel)
1761
0
    context->AllowDynamicColorFidelity = TRUE;
1762
1763
5.74k
  if (!freerdp_bitmap_planar_context_reset(context, maxWidth, maxHeight))
1764
0
  {
1765
0
    WINPR_PRAGMA_DIAG_PUSH
1766
0
    WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
1767
0
    freerdp_bitmap_planar_context_free(context);
1768
0
    WINPR_PRAGMA_DIAG_POP
1769
0
    return NULL;
1770
0
  }
1771
1772
5.74k
  return context;
1773
5.74k
}
1774
1775
void freerdp_bitmap_planar_context_free(BITMAP_PLANAR_CONTEXT* context)
1776
5.74k
{
1777
5.74k
  if (!context)
1778
0
    return;
1779
1780
5.74k
  winpr_aligned_free(context->pTempData);
1781
5.74k
  winpr_aligned_free(context->planesBuffer);
1782
5.74k
  winpr_aligned_free(context->deltaPlanesBuffer);
1783
5.74k
  winpr_aligned_free(context->rlePlanesBuffer);
1784
5.74k
  winpr_aligned_free(context);
1785
5.74k
}
1786
1787
void freerdp_planar_switch_bgr(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT planar, BOOL bgr)
1788
0
{
1789
0
  WINPR_ASSERT(planar);
1790
0
  planar->bgr = bgr;
1791
0
}
1792
1793
void freerdp_planar_topdown_image(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT planar, BOOL topdown)
1794
0
{
1795
0
  WINPR_ASSERT(planar);
1796
0
  planar->topdown = topdown;
1797
0
}