Coverage Report

Created: 2026-07-16 07:09

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
    return FALSE;
746
0
  if (planar->maxHeight < nSrcHeight)
747
0
    return FALSE;
748
749
0
  const UINT32 bpp = FreeRDPGetBytesPerPixel(DstFormat);
750
0
  if (nDstStep <= 0)
751
0
    nDstStep = nDstWidth * bpp;
752
753
0
  const BYTE* srcp = pSrcData;
754
755
0
  if (!pSrcData || (SrcSize < 1))
756
0
  {
757
0
    WLog_ERR(TAG, "Invalid argument pSrcData=%p [size=%" PRIu32 "]",
758
0
             WINPR_CXX_COMPAT_CAST(const void*, pSrcData), SrcSize);
759
0
    return FALSE;
760
0
  }
761
762
0
  if (!pDstData)
763
0
  {
764
0
    WLog_ERR(TAG, "Invalid argument pDstData=nullptr");
765
0
    return FALSE;
766
0
  }
767
768
0
  const BYTE FormatHeader = *srcp++;
769
0
  const BYTE cll = (FormatHeader & PLANAR_FORMAT_HEADER_CLL_MASK);
770
0
  const BYTE cs = (FormatHeader & PLANAR_FORMAT_HEADER_CS) != 0;
771
0
  const BYTE rle = (FormatHeader & PLANAR_FORMAT_HEADER_RLE) != 0;
772
0
  const BYTE alpha = !(FormatHeader & PLANAR_FORMAT_HEADER_NA);
773
774
0
  DstFormat = planar_invert_format(planar, alpha, DstFormat);
775
776
0
  if (alpha)
777
0
    useAlpha = FreeRDPColorHasAlpha(DstFormat);
778
779
  // WLog_INFO(TAG, "CLL: %"PRIu32" CS: %"PRIu8" RLE: %"PRIu8" ALPHA: %"PRIu8"", cll, cs, rle,
780
  // alpha);
781
782
0
  if (!cll && cs)
783
0
  {
784
0
    WLog_ERR(TAG, "Chroma subsampling requires YCoCg and does not work with RGB data");
785
0
    return FALSE; /* Chroma subsampling requires YCoCg */
786
0
  }
787
788
0
  const UINT32 subWidth = (nSrcWidth / 2) + (nSrcWidth % 2);
789
0
  const UINT32 subHeight = (nSrcHeight / 2) + (nSrcHeight % 2);
790
0
  const UINT32 planeSize = nSrcWidth * nSrcHeight;
791
0
  const UINT32 subSize = subWidth * subHeight;
792
793
0
  if (!cs)
794
0
  {
795
0
    rawSizes[0] = planeSize; /* LumaOrRedPlane */
796
0
    rawWidths[0] = nSrcWidth;
797
0
    rawHeights[0] = nSrcHeight;
798
0
    rawSizes[1] = planeSize; /* OrangeChromaOrGreenPlane */
799
0
    rawWidths[1] = nSrcWidth;
800
0
    rawHeights[1] = nSrcHeight;
801
0
    rawSizes[2] = planeSize; /* GreenChromaOrBluePlane */
802
0
    rawWidths[2] = nSrcWidth;
803
0
    rawHeights[2] = nSrcHeight;
804
0
    rawSizes[3] = planeSize; /* AlphaPlane */
805
0
    rawWidths[3] = nSrcWidth;
806
0
    rawHeights[3] = nSrcHeight;
807
0
  }
808
0
  else /* Chroma Subsampling */
809
0
  {
810
0
    rawSizes[0] = planeSize; /* LumaOrRedPlane */
811
0
    rawWidths[0] = nSrcWidth;
812
0
    rawHeights[0] = nSrcHeight;
813
0
    rawSizes[1] = subSize; /* OrangeChromaOrGreenPlane */
814
0
    rawWidths[1] = subWidth;
815
0
    rawHeights[1] = subHeight;
816
0
    rawSizes[2] = subSize; /* GreenChromaOrBluePlane */
817
0
    rawWidths[2] = subWidth;
818
0
    rawHeights[2] = subHeight;
819
0
    rawSizes[3] = planeSize; /* AlphaPlane */
820
0
    rawWidths[3] = nSrcWidth;
821
0
    rawHeights[3] = nSrcHeight;
822
0
  }
823
824
0
  const size_t diff = WINPR_ASSERTING_INT_CAST(size_t, (intptr_t)(srcp - pSrcData));
825
0
  if (SrcSize < diff)
826
0
  {
827
0
    WLog_ERR(TAG, "Size mismatch %" PRIu32 " < %" PRIuz, SrcSize, diff);
828
0
    return FALSE;
829
0
  }
830
831
0
  if (!rle) /* RAW */
832
0
  {
833
834
0
    UINT32 base = planeSize * 3;
835
0
    if (cs)
836
0
      base = planeSize + planeSize / 2;
837
838
0
    if (alpha)
839
0
    {
840
0
      if ((SrcSize - diff) < (planeSize + base))
841
0
      {
842
0
        WLog_ERR(TAG, "Alpha plane size mismatch %" PRIuz " < %" PRIu32, SrcSize - diff,
843
0
                 (planeSize + base));
844
0
        return FALSE;
845
0
      }
846
847
0
      planes[3] = srcp;                    /* AlphaPlane */
848
0
      planes[0] = planes[3] + rawSizes[3]; /* LumaOrRedPlane */
849
0
      planes[1] = planes[0] + rawSizes[0]; /* OrangeChromaOrGreenPlane */
850
0
      planes[2] = planes[1] + rawSizes[1]; /* GreenChromaOrBluePlane */
851
852
0
      if ((planes[2] + rawSizes[2]) > &pSrcData[SrcSize])
853
0
      {
854
0
        WLog_ERR(TAG, "plane size mismatch %p + %" PRIu32 " > %p",
855
0
                 WINPR_CXX_COMPAT_CAST(const void*, planes[2]), rawSizes[2],
856
0
                 WINPR_CXX_COMPAT_CAST(const void*, &pSrcData[SrcSize]));
857
0
        return FALSE;
858
0
      }
859
0
    }
860
0
    else
861
0
    {
862
0
      if ((SrcSize - diff) < base)
863
0
      {
864
0
        WLog_ERR(TAG, "plane size mismatch %" PRIuz " < %" PRIu32, SrcSize - diff, base);
865
0
        return FALSE;
866
0
      }
867
868
0
      planes[0] = srcp;                    /* LumaOrRedPlane */
869
0
      planes[1] = planes[0] + rawSizes[0]; /* OrangeChromaOrGreenPlane */
870
0
      planes[2] = planes[1] + rawSizes[1]; /* GreenChromaOrBluePlane */
871
872
0
      if ((planes[2] + rawSizes[2]) > &pSrcData[SrcSize])
873
0
      {
874
0
        WLog_ERR(TAG, "plane size mismatch %p + %" PRIu32 " > %p",
875
0
                 WINPR_CXX_COMPAT_CAST(const void*, planes[2]), rawSizes[2],
876
0
                 WINPR_CXX_COMPAT_CAST(const void*, &pSrcData[SrcSize]));
877
0
        return FALSE;
878
0
      }
879
0
    }
880
0
  }
881
0
  else /* RLE */
882
0
  {
883
0
    if (alpha)
884
0
    {
885
0
      planes[3] = srcp;
886
0
      rleSizes[3] = planar_skip_plane_rle(planes[3], (UINT32)(SrcSize - diff), rawWidths[3],
887
0
                                          rawHeights[3]); /* AlphaPlane */
888
889
0
      if (rleSizes[3] < 0)
890
0
        return FALSE;
891
892
0
      planes[0] = planes[3] + rleSizes[3];
893
0
    }
894
0
    else
895
0
      planes[0] = srcp;
896
897
0
    const size_t diff0 = WINPR_ASSERTING_INT_CAST(size_t, (intptr_t)(planes[0] - pSrcData));
898
0
    if (SrcSize < diff0)
899
0
    {
900
0
      WLog_ERR(TAG, "Size mismatch %" PRIu32 " < %" PRIuz, SrcSize, diff0);
901
0
      return FALSE;
902
0
    }
903
0
    rleSizes[0] = planar_skip_plane_rle(planes[0], (UINT32)(SrcSize - diff0), rawWidths[0],
904
0
                                        rawHeights[0]); /* RedPlane */
905
906
0
    if (rleSizes[0] < 0)
907
0
      return FALSE;
908
909
0
    planes[1] = planes[0] + rleSizes[0];
910
911
0
    const size_t diff1 = WINPR_ASSERTING_INT_CAST(size_t, (intptr_t)(planes[1] - pSrcData));
912
0
    if (SrcSize < diff1)
913
0
    {
914
0
      WLog_ERR(TAG, "Size mismatch %" PRIu32 " < %" PRIuz, SrcSize, diff1);
915
0
      return FALSE;
916
0
    }
917
0
    rleSizes[1] = planar_skip_plane_rle(planes[1], (UINT32)(SrcSize - diff1), rawWidths[1],
918
0
                                        rawHeights[1]); /* GreenPlane */
919
920
0
    if (rleSizes[1] < 1)
921
0
      return FALSE;
922
923
0
    planes[2] = planes[1] + rleSizes[1];
924
0
    const size_t diff2 = WINPR_ASSERTING_INT_CAST(size_t, (intptr_t)(planes[2] - pSrcData));
925
0
    if (SrcSize < diff2)
926
0
    {
927
0
      WLog_ERR(TAG, "Size mismatch %" PRIu32 " < %" PRIuz, SrcSize, diff);
928
0
      return FALSE;
929
0
    }
930
0
    rleSizes[2] = planar_skip_plane_rle(planes[2], (UINT32)(SrcSize - diff2), rawWidths[2],
931
0
                                        rawHeights[2]); /* BluePlane */
932
933
0
    if (rleSizes[2] < 1)
934
0
      return FALSE;
935
0
  }
936
937
0
  if (!cll) /* RGB */
938
0
  {
939
0
    UINT32 TempFormat = 0;
940
0
    BYTE* pTempData = pDstData;
941
0
    UINT32 nTempStep = nDstStep;
942
0
    UINT32 nTotalHeight = nYDst + nDstHeight;
943
944
0
    if (useAlpha)
945
0
      TempFormat = PIXEL_FORMAT_BGRA32;
946
0
    else
947
0
      TempFormat = PIXEL_FORMAT_BGRX32;
948
949
0
    TempFormat = planar_invert_format(planar, alpha, TempFormat);
950
951
0
    if ((TempFormat != DstFormat) || (nSrcWidth != nDstWidth) || (nSrcHeight != nDstHeight))
952
0
    {
953
0
      pTempData = planar->pTempData;
954
0
      nTempStep = planar->nTempStep;
955
0
      nTotalHeight = planar->maxHeight;
956
0
    }
957
958
0
    if (!rle) /* RAW */
959
0
    {
960
0
      if (!planar_decompress_planes_raw(planes, pTempData, TempFormat, nTempStep, nXDst,
961
0
                                        nYDst, nSrcWidth, nSrcHeight, vFlip, nTotalHeight))
962
0
        return FALSE;
963
964
0
      if (alpha)
965
0
        srcp += rawSizes[0] + rawSizes[1] + rawSizes[2] + rawSizes[3];
966
0
      else /* NoAlpha */
967
0
        srcp += rawSizes[0] + rawSizes[1] + rawSizes[2];
968
969
0
      if ((SrcSize - (srcp - pSrcData)) == 1)
970
0
        srcp++; /* pad */
971
0
    }
972
0
    else /* RLE */
973
0
    {
974
0
      if (nYDst + nSrcHeight > nTotalHeight)
975
0
      {
976
0
        WLog_ERR(TAG,
977
0
                 "planar plane destination Y %" PRIu32 " + height %" PRIu32
978
0
                 " exceeds totalHeight %" PRIu32,
979
0
                 nYDst, nSrcHeight, nTotalHeight);
980
0
        return FALSE;
981
0
      }
982
983
0
      if ((nXDst + nSrcWidth) * bpp > nTempStep)
984
0
      {
985
0
        WLog_ERR(TAG,
986
0
                 "planar plane destination (X %" PRIu32 " + width %" PRIu32
987
0
                 ") * bpp %" PRIu32 " exceeds stride %" PRIu32,
988
0
                 nXDst, nSrcWidth, bpp, nTempStep);
989
0
        return FALSE;
990
0
      }
991
992
0
      status = planar_decompress_plane_rle(
993
0
          planes[0], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[0]), pTempData, nTempStep,
994
0
          nXDst, nYDst, nSrcWidth, nSrcHeight, 2, vFlip); /* RedPlane */
995
996
0
      if (status < 0)
997
0
        return FALSE;
998
999
0
      status = planar_decompress_plane_rle(
1000
0
          planes[1], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[1]), pTempData, nTempStep,
1001
0
          nXDst, nYDst, nSrcWidth, nSrcHeight, 1, vFlip); /* GreenPlane */
1002
1003
0
      if (status < 0)
1004
0
        return FALSE;
1005
1006
0
      status = planar_decompress_plane_rle(
1007
0
          planes[2], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[2]), pTempData, nTempStep,
1008
0
          nXDst, nYDst, nSrcWidth, nSrcHeight, 0, vFlip); /* BluePlane */
1009
1010
0
      if (status < 0)
1011
0
        return FALSE;
1012
1013
0
      srcp += rleSizes[0] + rleSizes[1] + rleSizes[2];
1014
1015
0
      if (useAlpha)
1016
0
      {
1017
0
        status = planar_decompress_plane_rle(
1018
0
            planes[3], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[3]), pTempData,
1019
0
            nTempStep, nXDst, nYDst, nSrcWidth, nSrcHeight, 3, vFlip); /* AlphaPlane */
1020
0
      }
1021
0
      else
1022
0
        status = planar_set_plane(0xFF, pTempData, nTempStep, nXDst, nYDst, nSrcWidth,
1023
0
                                  nSrcHeight, 3, vFlip);
1024
1025
0
      if (status < 0)
1026
0
        return FALSE;
1027
1028
0
      if (alpha)
1029
0
        srcp += rleSizes[3];
1030
0
    }
1031
1032
0
    if (pTempData != pDstData)
1033
0
    {
1034
0
      if (!freerdp_image_copy_no_overlap(pDstData, DstFormat, nDstStep, nXDst, nYDst, w, h,
1035
0
                                         pTempData, TempFormat, nTempStep, nXDst, nYDst,
1036
0
                                         nullptr, FREERDP_FLIP_NONE))
1037
0
      {
1038
0
        WLog_ERR(TAG, "planar image copy failed");
1039
0
        return FALSE;
1040
0
      }
1041
0
    }
1042
0
  }
1043
0
  else /* YCoCg */
1044
0
  {
1045
0
    UINT32 TempFormat = 0;
1046
0
    BYTE* pTempData = planar->pTempData;
1047
0
    UINT32 nTempStep = planar->nTempStep;
1048
0
    UINT32 nTotalHeight = planar->maxHeight;
1049
0
    BYTE* dst = &pDstData[nXDst * FreeRDPGetBytesPerPixel(DstFormat) + nYDst * nDstStep];
1050
1051
0
    if (useAlpha)
1052
0
      TempFormat = PIXEL_FORMAT_BGRA32;
1053
0
    else
1054
0
      TempFormat = PIXEL_FORMAT_BGRX32;
1055
1056
0
    if (!pTempData)
1057
0
      return FALSE;
1058
1059
0
    if (rle) /* RLE encoded data. Decode and handle it like raw data. */
1060
0
    {
1061
0
      BYTE* rleBuffer[4] = WINPR_C_ARRAY_INIT;
1062
1063
0
      if (!planar->rlePlanesBuffer)
1064
0
        return FALSE;
1065
1066
0
      rleBuffer[3] = planar->rlePlanesBuffer;  /* AlphaPlane */
1067
0
      rleBuffer[0] = rleBuffer[3] + planeSize; /* LumaOrRedPlane */
1068
0
      rleBuffer[1] = rleBuffer[0] + planeSize; /* OrangeChromaOrGreenPlane */
1069
0
      rleBuffer[2] = rleBuffer[1] + planeSize; /* GreenChromaOrBluePlane */
1070
0
      if (useAlpha)
1071
0
      {
1072
0
        status = planar_decompress_plane_rle_only(
1073
0
            planes[3], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[3]), rleBuffer[3],
1074
0
            rawWidths[3], rawHeights[3]); /* AlphaPlane */
1075
1076
0
        if (status < 0)
1077
0
          return FALSE;
1078
0
      }
1079
1080
0
      if (alpha)
1081
0
        srcp += rleSizes[3];
1082
1083
0
      status = planar_decompress_plane_rle_only(
1084
0
          planes[0], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[0]), rleBuffer[0],
1085
0
          rawWidths[0], rawHeights[0]); /* LumaPlane */
1086
1087
0
      if (status < 0)
1088
0
        return FALSE;
1089
1090
0
      status = planar_decompress_plane_rle_only(
1091
0
          planes[1], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[1]), rleBuffer[1],
1092
0
          rawWidths[1], rawHeights[1]); /* OrangeChromaPlane */
1093
1094
0
      if (status < 0)
1095
0
        return FALSE;
1096
1097
0
      status = planar_decompress_plane_rle_only(
1098
0
          planes[2], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[2]), rleBuffer[2],
1099
0
          rawWidths[2], rawHeights[2]); /* GreenChromaPlane */
1100
1101
0
      if (status < 0)
1102
0
        return FALSE;
1103
1104
0
      planes[0] = rleBuffer[0];
1105
0
      planes[1] = rleBuffer[1];
1106
0
      planes[2] = rleBuffer[2];
1107
0
      planes[3] = rleBuffer[3];
1108
0
    }
1109
1110
    /* RAW */
1111
0
    {
1112
0
      if (cs)
1113
0
      { /* Chroma subsampling for Co and Cg:
1114
         * Each pixel contains the value that should be expanded to
1115
         * [2x,2y;2x+1,2y;2x+1,2y+1;2x;2y+1] */
1116
0
        if (!planar_subsample_expand(planes[1], rawSizes[1], nSrcWidth, nSrcHeight,
1117
0
                                     rawWidths[1], rawHeights[1], planar->deltaPlanes[0]))
1118
0
          return FALSE;
1119
1120
0
        planes[1] = planar->deltaPlanes[0];
1121
0
        rawSizes[1] = planeSize; /* OrangeChromaOrGreenPlane */
1122
0
        rawWidths[1] = nSrcWidth;
1123
0
        rawHeights[1] = nSrcHeight;
1124
1125
0
        if (!planar_subsample_expand(planes[2], rawSizes[2], nSrcWidth, nSrcHeight,
1126
0
                                     rawWidths[2], rawHeights[2], planar->deltaPlanes[1]))
1127
0
          return FALSE;
1128
1129
0
        planes[2] = planar->deltaPlanes[1];
1130
0
        rawSizes[2] = planeSize; /* GreenChromaOrBluePlane */
1131
0
        rawWidths[2] = nSrcWidth;
1132
0
        rawHeights[2] = nSrcHeight;
1133
0
      }
1134
1135
0
      if (!planar_decompress_planes_raw(planes, pTempData, TempFormat, nTempStep, nXDst,
1136
0
                                        nYDst, nSrcWidth, nSrcHeight, vFlip, nTotalHeight))
1137
0
        return FALSE;
1138
1139
0
      if (alpha)
1140
0
        srcp += rawSizes[0] + rawSizes[1] + rawSizes[2] + rawSizes[3];
1141
0
      else /* NoAlpha */
1142
0
        srcp += rawSizes[0] + rawSizes[1] + rawSizes[2];
1143
1144
0
      if ((SrcSize - (srcp - pSrcData)) == 1)
1145
0
        srcp++; /* pad */
1146
0
    }
1147
1148
0
    WINPR_ASSERT(prims->YCoCgToRGB_8u_AC4R);
1149
0
    int rc = prims->YCoCgToRGB_8u_AC4R(
1150
0
        pTempData, WINPR_ASSERTING_INT_CAST(int32_t, nTempStep), dst, DstFormat,
1151
0
        WINPR_ASSERTING_INT_CAST(int32_t, nDstStep), w, h, cll, useAlpha);
1152
0
    if (rc != PRIMITIVES_SUCCESS)
1153
0
    {
1154
0
      WLog_ERR(TAG, "YCoCgToRGB_8u_AC4R failed with %d", rc);
1155
0
      return FALSE;
1156
0
    }
1157
0
  }
1158
1159
0
  WINPR_UNUSED(srcp);
1160
0
  return TRUE;
1161
0
}
1162
1163
static inline BOOL freerdp_split_color_planes(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT planar,
1164
                                              const BYTE* WINPR_RESTRICT data, UINT32 format,
1165
                                              UINT32 width, UINT32 height, UINT32 scanline,
1166
                                              BYTE* WINPR_RESTRICT planes[4])
1167
0
{
1168
0
  WINPR_ASSERT(planar);
1169
1170
0
  if ((width > INT32_MAX) || (height > INT32_MAX) || (scanline > INT32_MAX))
1171
0
    return FALSE;
1172
1173
0
  if (scanline == 0)
1174
0
    scanline = width * FreeRDPGetBytesPerPixel(format);
1175
1176
0
  if (planar->topdown)
1177
0
  {
1178
0
    UINT32 k = 0;
1179
0
    for (UINT32 i = 0; i < height; i++)
1180
0
    {
1181
0
      const BYTE* pixel = &data[1ULL * scanline * i];
1182
1183
0
      for (UINT32 j = 0; j < width; j++)
1184
0
      {
1185
0
        const UINT32 color = FreeRDPReadColor(pixel, format);
1186
0
        pixel += FreeRDPGetBytesPerPixel(format);
1187
0
        FreeRDPSplitColor(color, format, &planes[1][k], &planes[2][k], &planes[3][k],
1188
0
                          &planes[0][k], nullptr);
1189
0
        k++;
1190
0
      }
1191
0
    }
1192
0
  }
1193
0
  else
1194
0
  {
1195
0
    UINT32 k = 0;
1196
1197
0
    for (INT64 i = (INT64)height - 1; i >= 0; i--)
1198
0
    {
1199
0
      const BYTE* pixel = &data[1ULL * scanline * (UINT32)i];
1200
1201
0
      for (UINT32 j = 0; j < width; j++)
1202
0
      {
1203
0
        const UINT32 color = FreeRDPReadColor(pixel, format);
1204
0
        pixel += FreeRDPGetBytesPerPixel(format);
1205
0
        FreeRDPSplitColor(color, format, &planes[1][k], &planes[2][k], &planes[3][k],
1206
0
                          &planes[0][k], nullptr);
1207
0
        k++;
1208
0
      }
1209
0
    }
1210
0
  }
1211
0
  return TRUE;
1212
0
}
1213
1214
static inline UINT32 freerdp_bitmap_planar_write_rle_bytes(const BYTE* WINPR_RESTRICT pInBuffer,
1215
                                                           UINT32 cRawBytes, UINT32 nRunLength,
1216
                                                           BYTE* WINPR_RESTRICT pOutBuffer,
1217
                                                           UINT32 outBufferSize)
1218
0
{
1219
0
  const BYTE* pInput = pInBuffer;
1220
0
  BYTE* pOutput = pOutBuffer;
1221
0
  BYTE controlByte = 0;
1222
0
  UINT32 nBytesToWrite = 0;
1223
1224
0
  if (!cRawBytes && !nRunLength)
1225
0
    return 0;
1226
1227
0
  if (nRunLength < 3)
1228
0
  {
1229
0
    cRawBytes += nRunLength;
1230
0
    nRunLength = 0;
1231
0
  }
1232
1233
0
  while (cRawBytes)
1234
0
  {
1235
0
    if (cRawBytes < 16)
1236
0
    {
1237
0
      if (nRunLength > 15)
1238
0
      {
1239
0
        if (nRunLength < 18)
1240
0
        {
1241
0
          controlByte = PLANAR_CONTROL_BYTE(13, cRawBytes);
1242
0
          nRunLength -= 13;
1243
0
          cRawBytes = 0;
1244
0
        }
1245
0
        else
1246
0
        {
1247
0
          controlByte = PLANAR_CONTROL_BYTE(15, cRawBytes);
1248
0
          nRunLength -= 15;
1249
0
          cRawBytes = 0;
1250
0
        }
1251
0
      }
1252
0
      else
1253
0
      {
1254
0
        controlByte = PLANAR_CONTROL_BYTE(nRunLength, cRawBytes);
1255
0
        nRunLength = 0;
1256
0
        cRawBytes = 0;
1257
0
      }
1258
0
    }
1259
0
    else
1260
0
    {
1261
0
      controlByte = PLANAR_CONTROL_BYTE(0, 15);
1262
0
      cRawBytes -= 15;
1263
0
    }
1264
1265
0
    if (outBufferSize < 1)
1266
0
      return 0;
1267
1268
0
    outBufferSize--;
1269
0
    *pOutput = controlByte;
1270
0
    pOutput++;
1271
0
    nBytesToWrite = (controlByte >> 4);
1272
1273
0
    if (nBytesToWrite)
1274
0
    {
1275
0
      if (outBufferSize < nBytesToWrite)
1276
0
        return 0;
1277
1278
0
      outBufferSize -= nBytesToWrite;
1279
0
      CopyMemory(pOutput, pInput, nBytesToWrite);
1280
0
      pOutput += nBytesToWrite;
1281
0
      pInput += nBytesToWrite;
1282
0
    }
1283
0
  }
1284
1285
0
  while (nRunLength)
1286
0
  {
1287
0
    if (nRunLength > 47)
1288
0
    {
1289
0
      if (nRunLength < 50)
1290
0
      {
1291
0
        controlByte = PLANAR_CONTROL_BYTE(2, 13);
1292
0
        nRunLength -= 45;
1293
0
      }
1294
0
      else
1295
0
      {
1296
0
        controlByte = PLANAR_CONTROL_BYTE(2, 15);
1297
0
        nRunLength -= 47;
1298
0
      }
1299
0
    }
1300
0
    else if (nRunLength > 31)
1301
0
    {
1302
0
      controlByte = PLANAR_CONTROL_BYTE(2, (nRunLength - 32));
1303
0
      nRunLength = 0;
1304
0
    }
1305
0
    else if (nRunLength > 15)
1306
0
    {
1307
0
      controlByte = PLANAR_CONTROL_BYTE(1, (nRunLength - 16));
1308
0
      nRunLength = 0;
1309
0
    }
1310
0
    else
1311
0
    {
1312
0
      controlByte = PLANAR_CONTROL_BYTE(nRunLength, 0);
1313
0
      nRunLength = 0;
1314
0
    }
1315
1316
0
    if (outBufferSize < 1)
1317
0
      return 0;
1318
1319
0
    --outBufferSize;
1320
0
    *pOutput = controlByte;
1321
0
    pOutput++;
1322
0
  }
1323
1324
0
  const intptr_t diff = (pOutput - pOutBuffer);
1325
0
  if ((diff < 0) || (diff > UINT32_MAX))
1326
0
    return 0;
1327
0
  return (UINT32)diff;
1328
0
}
1329
1330
static inline UINT32 freerdp_bitmap_planar_encode_rle_bytes(const BYTE* WINPR_RESTRICT pInBuffer,
1331
                                                            UINT32 inBufferSize,
1332
                                                            BYTE* WINPR_RESTRICT pOutBuffer,
1333
                                                            UINT32 outBufferSize)
1334
0
{
1335
0
  BYTE symbol = 0;
1336
0
  const BYTE* pInput = pInBuffer;
1337
0
  BYTE* pOutput = pOutBuffer;
1338
0
  const BYTE* pBytes = nullptr;
1339
0
  UINT32 cRawBytes = 0;
1340
0
  UINT32 nRunLength = 0;
1341
0
  UINT32 nBytesWritten = 0;
1342
0
  UINT32 nTotalBytesWritten = 0;
1343
1344
0
  if (!outBufferSize)
1345
0
    return 0;
1346
1347
0
  do
1348
0
  {
1349
0
    if (!inBufferSize)
1350
0
      break;
1351
1352
0
    const UINT32 bSymbolMatch = (symbol == *pInput) != 0;
1353
0
    symbol = *pInput;
1354
0
    pInput++;
1355
0
    inBufferSize--;
1356
1357
0
    if (nRunLength && !bSymbolMatch)
1358
0
    {
1359
0
      if (nRunLength < 3)
1360
0
      {
1361
0
        cRawBytes += nRunLength;
1362
0
        nRunLength = 0;
1363
0
      }
1364
0
      else
1365
0
      {
1366
0
        pBytes = pInput - (cRawBytes + nRunLength + 1);
1367
0
        nBytesWritten = freerdp_bitmap_planar_write_rle_bytes(pBytes, cRawBytes, nRunLength,
1368
0
                                                              pOutput, outBufferSize);
1369
0
        nRunLength = 0;
1370
1371
0
        if (!nBytesWritten || (nBytesWritten > outBufferSize))
1372
0
          return nRunLength;
1373
1374
0
        nTotalBytesWritten += nBytesWritten;
1375
0
        outBufferSize -= nBytesWritten;
1376
0
        pOutput += nBytesWritten;
1377
0
        cRawBytes = 0;
1378
0
      }
1379
0
    }
1380
1381
0
    nRunLength += bSymbolMatch;
1382
0
    cRawBytes += (!bSymbolMatch) != 0;
1383
0
  } while (outBufferSize);
1384
1385
0
  if (cRawBytes || nRunLength)
1386
0
  {
1387
0
    pBytes = pInput - (cRawBytes + nRunLength);
1388
0
    nBytesWritten = freerdp_bitmap_planar_write_rle_bytes(pBytes, cRawBytes, nRunLength,
1389
0
                                                          pOutput, outBufferSize);
1390
1391
0
    if (!nBytesWritten)
1392
0
      return 0;
1393
1394
0
    nTotalBytesWritten += nBytesWritten;
1395
0
  }
1396
1397
0
  if (inBufferSize)
1398
0
    return 0;
1399
1400
0
  return nTotalBytesWritten;
1401
0
}
1402
1403
BOOL freerdp_bitmap_planar_compress_plane_rle(const BYTE* WINPR_RESTRICT inPlane, UINT32 width,
1404
                                              UINT32 height, BYTE* WINPR_RESTRICT outPlane,
1405
                                              UINT32* WINPR_RESTRICT dstSize)
1406
0
{
1407
0
  if (!outPlane)
1408
0
    return FALSE;
1409
1410
0
  UINT32 index = 0;
1411
0
  const BYTE* pInput = inPlane;
1412
0
  BYTE* pOutput = outPlane;
1413
0
  UINT32 outBufferSize = *dstSize;
1414
0
  UINT32 nTotalBytesWritten = 0;
1415
1416
0
  while (outBufferSize > 0)
1417
0
  {
1418
0
    const UINT32 nBytesWritten =
1419
0
        freerdp_bitmap_planar_encode_rle_bytes(pInput, width, pOutput, outBufferSize);
1420
1421
0
    if ((!nBytesWritten) || (nBytesWritten > outBufferSize))
1422
0
      return FALSE;
1423
1424
0
    outBufferSize -= nBytesWritten;
1425
0
    nTotalBytesWritten += nBytesWritten;
1426
0
    pOutput += nBytesWritten;
1427
0
    pInput += width;
1428
0
    index++;
1429
1430
0
    if (index >= height)
1431
0
      break;
1432
0
  }
1433
1434
0
  *dstSize = nTotalBytesWritten;
1435
0
  return TRUE;
1436
0
}
1437
1438
static inline BOOL freerdp_bitmap_planar_compress_planes_rle(BYTE* WINPR_RESTRICT inPlanes[4],
1439
                                                             UINT32 width, UINT32 height,
1440
                                                             BYTE* WINPR_RESTRICT outPlanes,
1441
                                                             UINT32* WINPR_RESTRICT dstSizes,
1442
                                                             BOOL skipAlpha)
1443
0
{
1444
0
  UINT32 outPlanesSize = width * height * 4;
1445
1446
  /* AlphaPlane */
1447
0
  if (skipAlpha)
1448
0
  {
1449
0
    dstSizes[0] = 0;
1450
0
  }
1451
0
  else
1452
0
  {
1453
0
    dstSizes[0] = outPlanesSize;
1454
1455
0
    if (!freerdp_bitmap_planar_compress_plane_rle(inPlanes[0], width, height, outPlanes,
1456
0
                                                  &dstSizes[0]))
1457
0
      return FALSE;
1458
1459
0
    outPlanes += dstSizes[0];
1460
0
    outPlanesSize -= dstSizes[0];
1461
0
  }
1462
1463
  /* LumaOrRedPlane */
1464
0
  dstSizes[1] = outPlanesSize;
1465
1466
0
  if (!freerdp_bitmap_planar_compress_plane_rle(inPlanes[1], width, height, outPlanes,
1467
0
                                                &dstSizes[1]))
1468
0
    return FALSE;
1469
1470
0
  outPlanes += dstSizes[1];
1471
0
  outPlanesSize -= dstSizes[1];
1472
  /* OrangeChromaOrGreenPlane */
1473
0
  dstSizes[2] = outPlanesSize;
1474
1475
0
  if (!freerdp_bitmap_planar_compress_plane_rle(inPlanes[2], width, height, outPlanes,
1476
0
                                                &dstSizes[2]))
1477
0
    return FALSE;
1478
1479
0
  outPlanes += dstSizes[2];
1480
0
  outPlanesSize -= dstSizes[2];
1481
  /* GreenChromeOrBluePlane */
1482
0
  dstSizes[3] = outPlanesSize;
1483
1484
0
  return (freerdp_bitmap_planar_compress_plane_rle(inPlanes[3], width, height, outPlanes,
1485
0
                                                   &dstSizes[3]));
1486
0
}
1487
1488
BYTE* freerdp_bitmap_planar_delta_encode_plane(const BYTE* WINPR_RESTRICT inPlane, UINT32 width,
1489
                                               UINT32 height, BYTE* WINPR_RESTRICT outPlane)
1490
0
{
1491
0
  if (!outPlane)
1492
0
  {
1493
0
    if (width * height == 0)
1494
0
      return nullptr;
1495
1496
0
    outPlane = (BYTE*)calloc(height, width);
1497
0
    if (!outPlane)
1498
0
      return nullptr;
1499
0
  }
1500
1501
  // first line is copied as is
1502
0
  CopyMemory(outPlane, inPlane, width);
1503
1504
0
  for (UINT32 y = 1; y < height; y++)
1505
0
  {
1506
0
    const size_t off = 1ull * width * y;
1507
0
    BYTE* outPtr = &outPlane[off];
1508
0
    const BYTE* srcPtr = &inPlane[off];
1509
0
    const BYTE* prevLinePtr = &inPlane[off - width];
1510
0
    for (UINT32 x = 0; x < width; x++)
1511
0
    {
1512
0
      const int delta = (int)srcPtr[x] - (int)prevLinePtr[x];
1513
0
      const int s2c1i = (delta >= 0) ? delta : (INT_MAX + delta) + 1;
1514
0
      const int8_t s2c1 = WINPR_CXX_COMPAT_CAST(int8_t, s2c1i);
1515
0
      const uint32_t s2c =
1516
0
          (s2c1 >= 0) ? ((UINT32)s2c1 << 1) : (((UINT32)(~(s2c1) + 1) << 1) - 1);
1517
0
      outPtr[x] = (BYTE)s2c;
1518
0
    }
1519
0
  }
1520
1521
0
  return outPlane;
1522
0
}
1523
1524
static inline BOOL freerdp_bitmap_planar_delta_encode_planes(BYTE* WINPR_RESTRICT inPlanes[4],
1525
                                                             UINT32 width, UINT32 height,
1526
                                                             BYTE* WINPR_RESTRICT outPlanes[4])
1527
0
{
1528
0
  for (UINT32 i = 0; i < 4; i++)
1529
0
  {
1530
0
    outPlanes[i] =
1531
0
        freerdp_bitmap_planar_delta_encode_plane(inPlanes[i], width, height, outPlanes[i]);
1532
1533
0
    if (!outPlanes[i])
1534
0
      return FALSE;
1535
0
  }
1536
1537
0
  return TRUE;
1538
0
}
1539
1540
BYTE* freerdp_bitmap_compress_planar(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT context,
1541
                                     const BYTE* WINPR_RESTRICT data, UINT32 format, UINT32 width,
1542
                                     UINT32 height, UINT32 scanline, BYTE* WINPR_RESTRICT dstData,
1543
                                     UINT32* WINPR_RESTRICT pDstSize)
1544
0
{
1545
0
  UINT32 size = 0;
1546
0
  BYTE* dstp = nullptr;
1547
0
  UINT32 dstSizes[4] = WINPR_C_ARRAY_INIT;
1548
0
  BYTE FormatHeader = 0;
1549
1550
0
  if (!context || !context->rlePlanesBuffer)
1551
0
    return nullptr;
1552
1553
0
  if (context->AllowSkipAlpha)
1554
0
    FormatHeader |= PLANAR_FORMAT_HEADER_NA;
1555
1556
0
  const UINT32 planeSize = width * height;
1557
1558
0
  if (!context->AllowSkipAlpha)
1559
0
    format = planar_invert_format(context, TRUE, format);
1560
1561
0
  if (!freerdp_split_color_planes(context, data, format, width, height, scanline,
1562
0
                                  context->planes))
1563
0
    return nullptr;
1564
1565
0
  if (context->AllowRunLengthEncoding)
1566
0
  {
1567
0
    if (!freerdp_bitmap_planar_delta_encode_planes(context->planes, width, height,
1568
0
                                                   context->deltaPlanes))
1569
0
      return nullptr;
1570
1571
0
    if (!freerdp_bitmap_planar_compress_planes_rle(context->deltaPlanes, width, height,
1572
0
                                                   context->rlePlanesBuffer, dstSizes,
1573
0
                                                   context->AllowSkipAlpha))
1574
0
      return nullptr;
1575
1576
0
    {
1577
0
      uint32_t offset = 0;
1578
0
      FormatHeader |= PLANAR_FORMAT_HEADER_RLE;
1579
0
      context->rlePlanes[0] = &context->rlePlanesBuffer[offset];
1580
0
      offset += dstSizes[0];
1581
0
      context->rlePlanes[1] = &context->rlePlanesBuffer[offset];
1582
0
      offset += dstSizes[1];
1583
0
      context->rlePlanes[2] = &context->rlePlanesBuffer[offset];
1584
0
      offset += dstSizes[2];
1585
0
      context->rlePlanes[3] = &context->rlePlanesBuffer[offset];
1586
1587
#if defined(WITH_DEBUG_CODECS)
1588
      WLog_DBG(TAG,
1589
               "R: [%" PRIu32 "/%" PRIu32 "] G: [%" PRIu32 "/%" PRIu32 "] B: [%" PRIu32
1590
               " / %" PRIu32 "] ",
1591
               dstSizes[1], planeSize, dstSizes[2], planeSize, dstSizes[3], planeSize);
1592
#endif
1593
0
    }
1594
0
  }
1595
1596
0
  if (FormatHeader & PLANAR_FORMAT_HEADER_RLE)
1597
0
  {
1598
0
    if (!context->AllowRunLengthEncoding)
1599
0
      return nullptr;
1600
1601
0
    if (context->rlePlanes[0] == nullptr)
1602
0
      return nullptr;
1603
1604
0
    if (context->rlePlanes[1] == nullptr)
1605
0
      return nullptr;
1606
1607
0
    if (context->rlePlanes[2] == nullptr)
1608
0
      return nullptr;
1609
1610
0
    if (context->rlePlanes[3] == nullptr)
1611
0
      return nullptr;
1612
0
  }
1613
1614
0
  if (!dstData)
1615
0
  {
1616
0
    size = 1;
1617
1618
0
    if (!(FormatHeader & PLANAR_FORMAT_HEADER_NA))
1619
0
    {
1620
0
      if (FormatHeader & PLANAR_FORMAT_HEADER_RLE)
1621
0
        size += dstSizes[0];
1622
0
      else
1623
0
        size += planeSize;
1624
0
    }
1625
1626
0
    if (FormatHeader & PLANAR_FORMAT_HEADER_RLE)
1627
0
      size += (dstSizes[1] + dstSizes[2] + dstSizes[3]);
1628
0
    else
1629
0
      size += (planeSize * 3);
1630
1631
0
    if (!(FormatHeader & PLANAR_FORMAT_HEADER_RLE))
1632
0
      size++;
1633
1634
0
    dstData = malloc(size);
1635
1636
0
    if (!dstData)
1637
0
      return nullptr;
1638
1639
0
    *pDstSize = size;
1640
0
  }
1641
1642
0
  dstp = dstData;
1643
0
  *dstp = FormatHeader; /* FormatHeader */
1644
0
  dstp++;
1645
1646
  /* AlphaPlane */
1647
1648
0
  if (!(FormatHeader & PLANAR_FORMAT_HEADER_NA))
1649
0
  {
1650
0
    if (FormatHeader & PLANAR_FORMAT_HEADER_RLE)
1651
0
    {
1652
0
      CopyMemory(dstp, context->rlePlanes[0], dstSizes[0]); /* Alpha */
1653
0
      dstp += dstSizes[0];
1654
0
    }
1655
0
    else
1656
0
    {
1657
0
      CopyMemory(dstp, context->planes[0], planeSize); /* Alpha */
1658
0
      dstp += planeSize;
1659
0
    }
1660
0
  }
1661
1662
  /* LumaOrRedPlane */
1663
1664
0
  if (FormatHeader & PLANAR_FORMAT_HEADER_RLE)
1665
0
  {
1666
0
    CopyMemory(dstp, context->rlePlanes[1], dstSizes[1]); /* Red */
1667
0
    dstp += dstSizes[1];
1668
0
  }
1669
0
  else
1670
0
  {
1671
0
    CopyMemory(dstp, context->planes[1], planeSize); /* Red */
1672
0
    dstp += planeSize;
1673
0
  }
1674
1675
  /* OrangeChromaOrGreenPlane */
1676
1677
0
  if (FormatHeader & PLANAR_FORMAT_HEADER_RLE)
1678
0
  {
1679
0
    CopyMemory(dstp, context->rlePlanes[2], dstSizes[2]); /* Green */
1680
0
    dstp += dstSizes[2];
1681
0
  }
1682
0
  else
1683
0
  {
1684
0
    CopyMemory(dstp, context->planes[2], planeSize); /* Green */
1685
0
    dstp += planeSize;
1686
0
  }
1687
1688
  /* GreenChromeOrBluePlane */
1689
1690
0
  if (FormatHeader & PLANAR_FORMAT_HEADER_RLE)
1691
0
  {
1692
0
    CopyMemory(dstp, context->rlePlanes[3], dstSizes[3]); /* Blue */
1693
0
    dstp += dstSizes[3];
1694
0
  }
1695
0
  else
1696
0
  {
1697
0
    CopyMemory(dstp, context->planes[3], planeSize); /* Blue */
1698
0
    dstp += planeSize;
1699
0
  }
1700
1701
  /* Pad1 (1 byte) */
1702
1703
0
  if (!(FormatHeader & PLANAR_FORMAT_HEADER_RLE))
1704
0
  {
1705
0
    *dstp = 0;
1706
0
    dstp++;
1707
0
  }
1708
1709
0
  const intptr_t diff = (dstp - dstData);
1710
0
  if ((diff < 0) || (diff > UINT32_MAX))
1711
0
  {
1712
0
    free(dstData);
1713
0
    return nullptr;
1714
0
  }
1715
0
  size = (UINT32)diff;
1716
0
  *pDstSize = size;
1717
0
  return dstData;
1718
0
}
1719
1720
BOOL freerdp_bitmap_planar_context_reset(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT context,
1721
                                         UINT32 width, UINT32 height)
1722
0
{
1723
0
  if (!context)
1724
0
    return FALSE;
1725
1726
0
  context->bgr = FALSE;
1727
0
  context->maxWidth = PLANAR_ALIGN(width, 4);
1728
0
  context->maxHeight = PLANAR_ALIGN(height, 4);
1729
0
  {
1730
0
    const UINT64 tmp = (UINT64)context->maxWidth * context->maxHeight;
1731
0
    if (tmp > UINT32_MAX)
1732
0
      return FALSE;
1733
0
    context->maxPlaneSize = (UINT32)tmp;
1734
0
  }
1735
1736
0
  if (context->maxWidth > UINT32_MAX / 4)
1737
0
    return FALSE;
1738
0
  context->nTempStep = context->maxWidth * 4;
1739
1740
0
  memset((void*)context->planes, 0, sizeof(context->planes));
1741
0
  memset((void*)context->rlePlanes, 0, sizeof(context->rlePlanes));
1742
0
  memset((void*)context->deltaPlanes, 0, sizeof(context->deltaPlanes));
1743
1744
0
  if (context->maxPlaneSize > 0)
1745
0
  {
1746
0
    void* tmp = winpr_aligned_recalloc(context->planesBuffer, context->maxPlaneSize, 4, 32);
1747
0
    if (!tmp)
1748
0
      return FALSE;
1749
0
    context->planesBuffer = tmp;
1750
1751
0
    tmp = winpr_aligned_recalloc(context->pTempData, context->maxPlaneSize, 6, 32);
1752
0
    if (!tmp)
1753
0
      return FALSE;
1754
0
    context->pTempData = tmp;
1755
1756
0
    tmp = winpr_aligned_recalloc(context->deltaPlanesBuffer, context->maxPlaneSize, 4, 32);
1757
0
    if (!tmp)
1758
0
      return FALSE;
1759
0
    context->deltaPlanesBuffer = tmp;
1760
1761
0
    tmp = winpr_aligned_recalloc(context->rlePlanesBuffer, context->maxPlaneSize, 4, 32);
1762
0
    if (!tmp)
1763
0
      return FALSE;
1764
0
    context->rlePlanesBuffer = tmp;
1765
1766
0
    context->planes[0] = &context->planesBuffer[0ULL * context->maxPlaneSize];
1767
0
    context->planes[1] = &context->planesBuffer[1ULL * context->maxPlaneSize];
1768
0
    context->planes[2] = &context->planesBuffer[2ULL * context->maxPlaneSize];
1769
0
    context->planes[3] = &context->planesBuffer[3ULL * context->maxPlaneSize];
1770
0
    context->deltaPlanes[0] = &context->deltaPlanesBuffer[0ULL * context->maxPlaneSize];
1771
0
    context->deltaPlanes[1] = &context->deltaPlanesBuffer[1ULL * context->maxPlaneSize];
1772
0
    context->deltaPlanes[2] = &context->deltaPlanesBuffer[2ULL * context->maxPlaneSize];
1773
0
    context->deltaPlanes[3] = &context->deltaPlanesBuffer[3ULL * context->maxPlaneSize];
1774
0
  }
1775
0
  return TRUE;
1776
0
}
1777
1778
BITMAP_PLANAR_CONTEXT* freerdp_bitmap_planar_context_new(DWORD flags, UINT32 maxWidth,
1779
                                                         UINT32 maxHeight)
1780
0
{
1781
0
  BITMAP_PLANAR_CONTEXT* context =
1782
0
      (BITMAP_PLANAR_CONTEXT*)winpr_aligned_calloc(1, sizeof(BITMAP_PLANAR_CONTEXT), 32);
1783
1784
0
  if (!context)
1785
0
    return nullptr;
1786
1787
0
  if (flags & PLANAR_FORMAT_HEADER_NA)
1788
0
    context->AllowSkipAlpha = TRUE;
1789
1790
0
  if (flags & PLANAR_FORMAT_HEADER_RLE)
1791
0
    context->AllowRunLengthEncoding = TRUE;
1792
1793
0
  if (flags & PLANAR_FORMAT_HEADER_CS)
1794
0
    context->AllowColorSubsampling = TRUE;
1795
1796
0
  context->ColorLossLevel = flags & PLANAR_FORMAT_HEADER_CLL_MASK;
1797
1798
0
  if (context->ColorLossLevel)
1799
0
    context->AllowDynamicColorFidelity = TRUE;
1800
1801
0
  if (!freerdp_bitmap_planar_context_reset(context, maxWidth, maxHeight))
1802
0
  {
1803
0
    WINPR_PRAGMA_DIAG_PUSH
1804
0
    WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
1805
0
    freerdp_bitmap_planar_context_free(context);
1806
0
    WINPR_PRAGMA_DIAG_POP
1807
0
    return nullptr;
1808
0
  }
1809
1810
0
  return context;
1811
0
}
1812
1813
void freerdp_bitmap_planar_context_free(BITMAP_PLANAR_CONTEXT* context)
1814
0
{
1815
0
  if (!context)
1816
0
    return;
1817
1818
0
  winpr_aligned_free(context->pTempData);
1819
0
  winpr_aligned_free(context->planesBuffer);
1820
0
  winpr_aligned_free(context->deltaPlanesBuffer);
1821
0
  winpr_aligned_free(context->rlePlanesBuffer);
1822
0
  winpr_aligned_free(context);
1823
0
}
1824
1825
void freerdp_planar_switch_bgr(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT planar, BOOL bgr)
1826
0
{
1827
0
  WINPR_ASSERT(planar);
1828
0
  planar->bgr = bgr;
1829
0
}
1830
1831
void freerdp_planar_topdown_image(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT planar, BOOL topdown)
1832
0
{
1833
0
  WINPR_ASSERT(planar);
1834
0
  planar->topdown = topdown;
1835
0
}