Coverage Report

Created: 2026-01-17 07:16

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