Coverage Report

Created: 2026-10-15 06:26

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