Coverage Report

Created: 2026-07-16 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/libfreerdp/codec/region.c
Line
Count
Source
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 *
4
 * Copyright 2014 Thincast Technologies GmbH
5
 * Copyright 2014 Hardening <contact@hardening-consulting.com>
6
 * Copyright 2017 Armin Novak <armin.novak@thincast.com>
7
 * Copyright 2017 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 <winpr/assert.h>
23
#include <winpr/memory.h>
24
#include <freerdp/log.h>
25
#include <freerdp/codec/region.h>
26
27
#define TAG FREERDP_TAG("codec")
28
29
/*
30
 * The functions in this file implement the Region abstraction largely inspired from
31
 * pixman library. The following comment is taken from the pixman code.
32
 *
33
 * A Region is simply a set of disjoint(non-overlapping) rectangles, plus an
34
 * "extent" rectangle which is the smallest single rectangle that contains all
35
 * the non-overlapping rectangles.
36
 *
37
 * A Region is implemented as a "y-x-banded" array of rectangles.  This array
38
 * imposes two degrees of order.  First, all rectangles are sorted by top side
39
 * y coordinate first (y1), and then by left side x coordinate (x1).
40
 *
41
 * Furthermore, the rectangles are grouped into "bands".  Each rectangle in a
42
 * band has the same top y coordinate (y1), and each has the same bottom y
43
 * coordinate (y2).  Thus all rectangles in a band differ only in their left
44
 * and right side (x1 and x2).  Bands are implicit in the array of rectangles:
45
 * there is no separate list of band start pointers.
46
 *
47
 * The y-x band representation does not minimize rectangles.  In particular,
48
 * if a rectangle vertically crosses a band (the rectangle has scanlines in
49
 * the y1 to y2 area spanned by the band), then the rectangle may be broken
50
 * down into two or more smaller rectangles stacked one atop the other.
51
 *
52
 *  -----------                             -----------
53
 *  |         |                             |         |             band 0
54
 *  |         |  --------                   -----------  --------
55
 *  |         |  |      |  in y-x banded    |         |  |      |   band 1
56
 *  |         |  |      |  form is          |         |  |      |
57
 *  -----------  |      |                   -----------  --------
58
 *               |      |                                |      |   band 2
59
 *               --------                                --------
60
 *
61
 * An added constraint on the rectangles is that they must cover as much
62
 * horizontal area as possible: no two rectangles within a band are allowed
63
 * to touch.
64
 *
65
 * Whenever possible, bands will be merged together to cover a greater vertical
66
 * distance (and thus reduce the number of rectangles). Two bands can be merged
67
 * only if the bottom of one touches the top of the other and they have
68
 * rectangles in the same places (of the same width, of course).
69
 */
70
71
struct S_REGION16_DATA
72
{
73
  size_t nbRects;
74
  RECTANGLE_16* rects;
75
};
76
77
void region16_init(REGION16* region)
78
0
{
79
0
  WINPR_ASSERT(region);
80
81
0
  const REGION16 empty = WINPR_C_ARRAY_INIT;
82
0
  *region = empty;
83
0
}
84
85
int region16_n_rects(const REGION16* region)
86
0
{
87
0
  WINPR_ASSERT(region);
88
0
  if (!region->data)
89
0
    return 0;
90
91
0
  return WINPR_ASSERTING_INT_CAST(int, region->data->nbRects);
92
0
}
93
94
const RECTANGLE_16* region16_rects(const REGION16* region, UINT32* nbRects)
95
0
{
96
0
  if (nbRects)
97
0
    *nbRects = 0;
98
99
0
  if (!region)
100
0
    return nullptr;
101
102
0
  REGION16_DATA* data = region->data;
103
0
  if (!data)
104
0
    return nullptr;
105
106
0
  if (nbRects)
107
0
    *nbRects = WINPR_ASSERTING_INT_CAST(UINT32, data->nbRects);
108
109
0
  return data->rects;
110
0
}
111
112
static inline RECTANGLE_16* region16_rects_noconst(REGION16* region)
113
0
{
114
0
  WINPR_ASSERT(region);
115
116
0
  REGION16_DATA* data = region->data;
117
118
0
  if (!data)
119
0
    return nullptr;
120
121
0
  return data->rects;
122
0
}
123
124
const RECTANGLE_16* region16_extents(const REGION16* region)
125
0
{
126
0
  if (!region)
127
0
    return nullptr;
128
129
0
  return &region->extents;
130
0
}
131
132
static RECTANGLE_16* region16_extents_noconst(REGION16* region)
133
0
{
134
0
  if (!region)
135
0
    return nullptr;
136
137
0
  return &region->extents;
138
0
}
139
140
BOOL rectangle_is_empty(const RECTANGLE_16* rect)
141
0
{
142
0
  WINPR_ASSERT(rect);
143
144
  /* A rectangle with width <= 0 or height <= 0 should be regarded
145
   * as empty.
146
   */
147
0
  return ((rect->left >= rect->right) || (rect->top >= rect->bottom));
148
0
}
149
150
BOOL region16_is_empty(const REGION16* region)
151
0
{
152
0
  WINPR_ASSERT(region);
153
0
  if (!region->data)
154
0
    return TRUE;
155
0
  return (region->data->nbRects == 0);
156
0
}
157
158
BOOL rectangles_equal(const RECTANGLE_16* r1, const RECTANGLE_16* r2)
159
0
{
160
0
  WINPR_ASSERT(r1);
161
0
  WINPR_ASSERT(r2);
162
163
0
  return ((r1->left == r2->left) && (r1->top == r2->top) && (r1->right == r2->right) &&
164
0
          (r1->bottom == r2->bottom));
165
0
}
166
167
BOOL rectangles_intersects(const RECTANGLE_16* r1, const RECTANGLE_16* r2)
168
0
{
169
0
  RECTANGLE_16 tmp = WINPR_C_ARRAY_INIT;
170
0
  return rectangles_intersection(r1, r2, &tmp);
171
0
}
172
173
BOOL rectangles_intersection(const RECTANGLE_16* r1, const RECTANGLE_16* r2, RECTANGLE_16* dst)
174
0
{
175
0
  WINPR_ASSERT(r1);
176
0
  WINPR_ASSERT(r2);
177
0
  WINPR_ASSERT(dst);
178
179
0
  dst->left = MAX(r1->left, r2->left);
180
0
  dst->right = MIN(r1->right, r2->right);
181
0
  dst->top = MAX(r1->top, r2->top);
182
0
  dst->bottom = MIN(r1->bottom, r2->bottom);
183
0
  return (dst->left < dst->right) && (dst->top < dst->bottom);
184
0
}
185
186
const char* rectangle_to_string(const RECTANGLE_16* rect, char* buffer, size_t length)
187
0
{
188
0
  if (!buffer || (length < 2))
189
0
    return nullptr;
190
0
  if (!rect)
191
0
    (void)_snprintf(buffer, length - 1, "{ nullptr }");
192
0
  else
193
0
    (void)_snprintf(buffer, length - 1, "{%" PRIu16 "x%" PRIu16 "-%" PRIu16 "x%" PRIu16 "}",
194
0
                    rect->left, rect->top, rect->right, rect->bottom);
195
0
  return buffer;
196
0
}
197
198
RECTANGLE_16* rectangles_clone(const RECTANGLE_16* rects, size_t count)
199
0
{
200
0
  if (count == 0)
201
0
    return nullptr;
202
0
  if (count > SIZE_MAX / sizeof(RECTANGLE_16))
203
0
    return nullptr;
204
205
0
  WINPR_ASSERT(rects);
206
207
0
  RECTANGLE_16* clone = calloc(count, sizeof(RECTANGLE_16));
208
0
  if (!clone)
209
0
    return nullptr;
210
211
0
  memcpy(clone, rects, sizeof(RECTANGLE_16) * count);
212
0
  return clone;
213
0
}
214
215
static void freeRegion(REGION16_DATA* data)
216
0
{
217
0
  if (data)
218
0
    free(data->rects);
219
0
  free(data);
220
0
}
221
222
void region16_clear(REGION16* region)
223
0
{
224
0
  WINPR_ASSERT(region);
225
226
0
  freeRegion(region->data);
227
0
  region->data = nullptr;
228
229
0
  const RECTANGLE_16 empty = WINPR_C_ARRAY_INIT;
230
0
  region->extents = empty;
231
0
}
232
233
WINPR_ATTR_MALLOC(freeRegion, 1)
234
WINPR_ATTR_NODISCARD
235
static REGION16_DATA* allocateRegion(size_t nbItems)
236
0
{
237
0
  REGION16_DATA* data = calloc(1, sizeof(REGION16_DATA));
238
0
  if (!data)
239
0
    return nullptr;
240
241
0
  if (nbItems > 0)
242
0
  {
243
0
    data->rects = calloc(nbItems, sizeof(RECTANGLE_16));
244
0
    if (!data->rects)
245
0
    {
246
0
      free(data);
247
0
      return nullptr;
248
0
    }
249
0
  }
250
251
0
  data->nbRects = nbItems;
252
0
  return data;
253
0
}
254
255
static inline RECTANGLE_16* nextRect(REGION16_DATA* data, size_t index)
256
0
{
257
0
  WINPR_ASSERT(data);
258
0
  if (index + 1 > data->nbRects)
259
0
  {
260
0
    RECTANGLE_16* rects = realloc(data->rects, (index + 1) * sizeof(RECTANGLE_16));
261
0
    if (!rects)
262
0
    {
263
0
      freeRegion(data);
264
0
      return nullptr;
265
0
    }
266
267
0
    const RECTANGLE_16 empty = WINPR_C_ARRAY_INIT;
268
0
    rects[index] = empty;
269
0
    data->nbRects = index + 1;
270
0
    data->rects = rects;
271
0
  }
272
0
  return &data->rects[index];
273
0
}
274
275
static BOOL resizeRegion(REGION16* region, size_t nbItems)
276
0
{
277
0
  WINPR_ASSERT(region);
278
0
  if (nbItems == 0)
279
0
  {
280
0
    freeRegion(region->data);
281
0
    region->data = nullptr;
282
0
    return TRUE;
283
0
  }
284
285
0
  if (!region->data)
286
0
  {
287
0
    region->data = allocateRegion(nbItems);
288
0
    return region->data != nullptr;
289
0
  }
290
291
0
  RECTANGLE_16* rects = realloc(region->data->rects, nbItems * sizeof(RECTANGLE_16));
292
0
  if (!rects)
293
0
  {
294
0
    free(region->data->rects);
295
0
    region->data->nbRects = 0;
296
0
    region->data->rects = nullptr;
297
0
    return FALSE;
298
0
  }
299
300
0
  for (size_t x = region->data->nbRects; x < nbItems; x++)
301
0
  {
302
0
    const RECTANGLE_16 empty = WINPR_C_ARRAY_INIT;
303
0
    rects[x] = empty;
304
0
  }
305
0
  region->data->rects = rects;
306
0
  region->data->nbRects = nbItems;
307
0
  return TRUE;
308
0
}
309
310
static inline BOOL region16_copy_data(REGION16* dst, const REGION16* src)
311
0
{
312
0
  WINPR_ASSERT(dst);
313
0
  WINPR_ASSERT(src);
314
315
0
  freeRegion(dst->data);
316
0
  dst->data = nullptr;
317
318
0
  if (src->data && (src->data->nbRects > 0))
319
0
  {
320
0
    dst->data = allocateRegion(src->data->nbRects);
321
0
    if (!dst->data || !dst->data->rects)
322
0
      return FALSE;
323
0
    memcpy(dst->data->rects, src->data->rects, dst->data->nbRects * sizeof(RECTANGLE_16));
324
0
  }
325
0
  return TRUE;
326
0
}
327
328
BOOL region16_copy(REGION16* dst, const REGION16* src)
329
0
{
330
0
  if (dst == src)
331
0
    return TRUE;
332
333
0
  WINPR_ASSERT(dst);
334
0
  WINPR_ASSERT(src);
335
336
0
  dst->extents = src->extents;
337
338
0
  return region16_copy_data(dst, src);
339
0
}
340
341
void region16_print(const REGION16* region)
342
0
{
343
0
  UINT32 nbRects = 0;
344
0
  int currentBandY = -1;
345
0
  const RECTANGLE_16* rects = region16_rects(region, &nbRects);
346
347
0
  WLog_DBG(TAG, "nrects=%" PRIu32 "", nbRects);
348
349
0
  for (UINT32 i = 0; i < nbRects; i++)
350
0
  {
351
0
    const RECTANGLE_16* rect = &rects[i];
352
353
0
    if (rect->top != currentBandY)
354
0
    {
355
0
      currentBandY = rect->top;
356
0
      WLog_DBG(TAG, "band %d: ", currentBandY);
357
0
    }
358
359
0
    WLog_DBG(TAG, "(%" PRIu16 ",%" PRIu16 "-%" PRIu16 ",%" PRIu16 ")", rect->left, rect->top,
360
0
             rect->right, rect->bottom);
361
0
  }
362
0
}
363
364
char* region16_to_string(const REGION16* region)
365
0
{
366
0
  if (!region)
367
0
    return _strdup("REGION16{nullptr}");
368
369
0
  UINT32 nbRects = 0;
370
0
  const RECTANGLE_16* rects = region16_rects(region, &nbRects);
371
372
0
  char* str = nullptr;
373
0
  size_t slen = 0;
374
0
  winpr_asprintf(&str, &slen, "REGION16{nrects=%" PRIu32 " [", nbRects);
375
376
0
  UINT16 currentBandY = 0;
377
0
  for (UINT32 i = 0; i < nbRects; i++)
378
0
  {
379
0
    const RECTANGLE_16* rect = &rects[i];
380
381
0
    if (rect->top != currentBandY)
382
0
    {
383
0
      currentBandY = rect->top;
384
0
      char* tmp = nullptr;
385
0
      winpr_asprintf(&tmp, &slen, "%sband %" PRIu16 ":", str, currentBandY);
386
0
      free(str);
387
0
      str = tmp;
388
0
    }
389
390
0
    char buffer[64] = WINPR_C_ARRAY_INIT;
391
0
    char* tmp = nullptr;
392
0
    winpr_asprintf(&tmp, &slen, "%s, %s", str,
393
0
                   rectangle_to_string(rect, buffer, sizeof(buffer)));
394
0
    free(str);
395
0
    str = tmp;
396
0
  }
397
398
0
  char* tmp = nullptr;
399
0
  winpr_asprintf(&tmp, &slen, "%s]}", str);
400
0
  free(str);
401
0
  return tmp;
402
0
}
403
404
static BOOL region16_copy_band_with_union(REGION16_DATA* region, const RECTANGLE_16* src,
405
                                          const RECTANGLE_16* end, UINT16 newTop, UINT16 newBottom,
406
                                          const RECTANGLE_16* unionRect, UINT32* dstCounter,
407
                                          const RECTANGLE_16** srcPtr)
408
0
{
409
0
  WINPR_ASSERT(region);
410
0
  WINPR_ASSERT(src);
411
0
  WINPR_ASSERT(end);
412
0
  WINPR_ASSERT(dstCounter);
413
414
0
  UINT16 refY = src->top;
415
416
  /* merges a band with the given rect
417
   * Input:
418
   *                   unionRect
419
   *               |               |
420
   *               |               |
421
   * ==============+===============+================================
422
   *   |Item1|  |Item2| |Item3|  |Item4|    |Item5|            Band
423
   * ==============+===============+================================
424
   *    before     |    overlap    |          after
425
   *
426
   * Resulting band:
427
   *   +-----+  +----------------------+    +-----+
428
   *   |Item1|  |      Item2           |    |Item3|
429
   *   +-----+  +----------------------+    +-----+
430
   *
431
   *  We first copy as-is items that are before Item2, the first overlapping
432
   *  item.
433
   *  Then we find the last one that overlap unionRect to aggregate Item2, Item3
434
   *  and Item4 to create Item2.
435
   *  Finally Item5 is copied as Item3.
436
   *
437
   *  When no unionRect is provided, we skip the two first steps to just copy items
438
   */
439
440
0
  if (unionRect)
441
0
  {
442
    /* items before unionRect */
443
0
    while ((src < end) && (src->top == refY) && (src->right < unionRect->left))
444
0
    {
445
0
      RECTANGLE_16* dst = nextRect(region, (*dstCounter)++);
446
0
      if (!dst)
447
0
        return FALSE;
448
0
      dst->top = newTop;
449
0
      dst->bottom = newBottom;
450
0
      dst->right = src->right;
451
0
      dst->left = src->left;
452
0
      src++;
453
0
    }
454
455
    /* treat items overlapping with unionRect */
456
0
    const RECTANGLE_16* startOverlap = unionRect;
457
0
    const RECTANGLE_16* endOverlap = unionRect;
458
459
0
    if ((src < end) && (src->top == refY) && (src->left < unionRect->left))
460
0
      startOverlap = src;
461
462
0
    while ((src < end) && (src->top == refY) && (src->right < unionRect->right))
463
0
    {
464
0
      src++;
465
0
    }
466
467
0
    if ((src < end) && (src->top == refY) && (src->left < unionRect->right))
468
0
    {
469
0
      endOverlap = src;
470
0
      src++;
471
0
    }
472
473
0
    {
474
0
      RECTANGLE_16* dst = nextRect(region, (*dstCounter)++);
475
0
      if (!dst)
476
0
        return FALSE;
477
0
      dst->bottom = newBottom;
478
0
      dst->top = newTop;
479
0
      dst->left = startOverlap->left;
480
0
      dst->right = endOverlap->right;
481
0
    }
482
0
  }
483
484
  /* treat remaining items on the same band */
485
0
  while ((src < end) && (src->top == refY))
486
0
  {
487
0
    RECTANGLE_16* dst = nextRect(region, (*dstCounter)++);
488
0
    if (!dst)
489
0
      return FALSE;
490
491
0
    dst->top = newTop;
492
0
    dst->bottom = newBottom;
493
0
    dst->right = src->right;
494
0
    dst->left = src->left;
495
0
    src++;
496
0
  }
497
498
0
  if (srcPtr)
499
0
    *srcPtr = src;
500
501
0
  return TRUE;
502
0
}
503
504
static RECTANGLE_16* next_band(RECTANGLE_16* band1, RECTANGLE_16* endPtr, int* nbItems)
505
0
{
506
0
  WINPR_ASSERT(band1);
507
0
  WINPR_ASSERT(endPtr);
508
0
  WINPR_ASSERT(nbItems);
509
510
0
  UINT16 refY = band1->top;
511
0
  *nbItems = 0;
512
513
0
  while ((band1 < endPtr) && (band1->top == refY))
514
0
  {
515
0
    band1++;
516
0
    *nbItems += 1;
517
0
  }
518
519
0
  return band1;
520
0
}
521
522
static BOOL band_match(const RECTANGLE_16* band1, const RECTANGLE_16* band2,
523
                       const RECTANGLE_16* endPtr)
524
0
{
525
0
  int refBand2 = band2->top;
526
0
  const RECTANGLE_16* band2Start = band2;
527
528
0
  while ((band1 < band2Start) && (band2 < endPtr) && (band2->top == refBand2))
529
0
  {
530
0
    if ((band1->left != band2->left) || (band1->right != band2->right))
531
0
      return FALSE;
532
533
0
    band1++;
534
0
    band2++;
535
0
  }
536
537
0
  if (band1 != band2Start)
538
0
    return FALSE;
539
540
0
  return (band2 == endPtr) || (band2->top != refBand2);
541
0
}
542
543
/** compute if the rectangle is fully included in the band
544
 * @param band a pointer on the beginning of the band
545
 * @param endPtr end of the region
546
 * @param rect the rectangle to test
547
 * @return if rect is fully included in an item of the band
548
 */
549
static BOOL rectangle_contained_in_band(const RECTANGLE_16* band, const RECTANGLE_16* endPtr,
550
                                        const RECTANGLE_16* rect)
551
0
{
552
0
  WINPR_ASSERT(band);
553
0
  WINPR_ASSERT(endPtr);
554
0
  WINPR_ASSERT(rect);
555
556
0
  UINT16 refY = band->top;
557
558
0
  if ((band->top > rect->top) || (rect->bottom > band->bottom))
559
0
    return FALSE;
560
561
  /* note: as the band is sorted from left to right, once we've seen an item
562
   *    that is after rect->left we're sure that the result is False.
563
   */
564
0
  while ((band < endPtr) && (band->top == refY) && (band->left <= rect->left))
565
0
  {
566
0
    if (rect->right <= band->right)
567
0
      return TRUE;
568
569
0
    band++;
570
0
  }
571
572
0
  return FALSE;
573
0
}
574
575
static BOOL region16_simplify_bands(REGION16* region)
576
0
{
577
  /** Simplify consecutive bands that touch and have the same items
578
   *
579
   *  ====================          ====================
580
   *     | 1 |  | 2   |               |   |  |     |
581
   *  ====================            |   |  |     |
582
   *     | 1 |  | 2   |    ====>    | 1 |  |  2  |
583
   *  ====================            |   |  |     |
584
   *     | 1 |  | 2   |               |   |  |     |
585
   *  ====================          ====================
586
   *
587
   */
588
589
0
  const int nbRects = region16_n_rects(region);
590
0
  int finalNbRects = nbRects;
591
592
0
  if (nbRects < 2)
593
0
    return TRUE;
594
595
0
  RECTANGLE_16* band1 = region16_rects_noconst(region);
596
0
  RECTANGLE_16* endPtr = band1 + nbRects;
597
598
0
  do
599
0
  {
600
0
    int bandItems = 0;
601
0
    RECTANGLE_16* band2 = next_band(band1, endPtr, &bandItems);
602
603
0
    if (band2 == endPtr)
604
0
      break;
605
606
0
    if ((band1->bottom == band2->top) && band_match(band1, band2, endPtr))
607
0
    {
608
      /* adjust the bottom of band1 items */
609
0
      RECTANGLE_16* tmp = band1;
610
611
0
      while (tmp < band2)
612
0
      {
613
0
        tmp->bottom = band2->bottom;
614
0
        tmp++;
615
0
      }
616
617
      /* override band2, we don't move band1 pointer as the band after band2
618
       * may be merged too */
619
0
      const RECTANGLE_16* endBand = band2 + bandItems;
620
0
      const size_t toMove =
621
0
          WINPR_ASSERTING_INT_CAST(size_t, (endPtr - endBand)) * sizeof(RECTANGLE_16);
622
623
0
      if (toMove)
624
0
        MoveMemory(band2, endBand, toMove);
625
626
0
      finalNbRects -= bandItems;
627
0
      endPtr -= bandItems;
628
0
    }
629
0
    else
630
0
    {
631
0
      band1 = band2;
632
0
    }
633
0
  } while (TRUE);
634
635
0
  if (finalNbRects != nbRects)
636
0
  {
637
0
    if (!resizeRegion(region, WINPR_ASSERTING_INT_CAST(size_t, finalNbRects)))
638
0
      return FALSE;
639
0
  }
640
641
0
  return TRUE;
642
0
}
643
644
BOOL region16_union_rect(REGION16* dst, const REGION16* src, const RECTANGLE_16* rect)
645
0
{
646
0
  const RECTANGLE_16* nextBand = nullptr;
647
0
  UINT32 srcNbRects = 0;
648
0
  UINT16 topInterBand = 0;
649
0
  WINPR_ASSERT(src);
650
0
  WINPR_ASSERT(dst);
651
652
0
  const RECTANGLE_16* srcExtents = region16_extents(src);
653
0
  RECTANGLE_16* dstExtents = region16_extents_noconst(dst);
654
655
0
  const int nrSrcRects = region16_n_rects(src);
656
0
  if (nrSrcRects == 0)
657
0
  {
658
    /* source is empty, so the union is rect */
659
0
    dst->extents = *rect;
660
661
0
    if (!resizeRegion(dst, 1))
662
0
      return FALSE;
663
664
0
    RECTANGLE_16* dstRect = region16_rects_noconst(dst);
665
0
    WINPR_ASSERT(dstRect);
666
667
0
    dstRect->top = rect->top;
668
0
    dstRect->left = rect->left;
669
0
    dstRect->right = rect->right;
670
0
    dstRect->bottom = rect->bottom;
671
0
    dst->data->nbRects = 1;
672
0
    return TRUE;
673
0
  }
674
675
0
  REGION16_DATA* newItems = allocateRegion(WINPR_ASSERTING_INT_CAST(size_t, nrSrcRects + 1));
676
677
0
  if (!newItems)
678
0
    return FALSE;
679
680
0
  UINT32 usedRects = 0;
681
682
  /* adds the piece of rect that is on the top of src */
683
0
  if (rect->top < srcExtents->top)
684
0
  {
685
0
    RECTANGLE_16* dstRect = nextRect(newItems, usedRects++);
686
0
    if (!dstRect)
687
0
      return FALSE;
688
689
0
    dstRect->top = rect->top;
690
0
    dstRect->left = rect->left;
691
0
    dstRect->right = rect->right;
692
0
    dstRect->bottom = MIN(srcExtents->top, rect->bottom);
693
0
  }
694
695
  /* treat possibly overlapping region */
696
0
  const RECTANGLE_16* currentBand = region16_rects(src, &srcNbRects);
697
0
  const RECTANGLE_16* endSrcRect = currentBand + srcNbRects;
698
699
0
  while (currentBand < endSrcRect)
700
0
  {
701
0
    if ((currentBand->bottom <= rect->top) || (rect->bottom <= currentBand->top) ||
702
0
        rectangle_contained_in_band(currentBand, endSrcRect, rect))
703
0
    {
704
      /* no overlap between rect and the band, rect is totally below or totally above
705
       * the current band, or rect is already covered by an item of the band.
706
       * let's copy all the rectangles from this band
707
                  +----+
708
                  |    |   rect (case 1)
709
                  +----+
710
711
         =================
712
      band of srcRect
713
       =================
714
              +----+
715
              |    |   rect (case 2)
716
              +----+
717
      */
718
0
      if (!region16_copy_band_with_union(newItems, currentBand, endSrcRect, currentBand->top,
719
0
                                         currentBand->bottom, nullptr, &usedRects, &nextBand))
720
0
        return FALSE;
721
0
      topInterBand = rect->top;
722
0
    }
723
0
    else
724
0
    {
725
      /* rect overlaps the band:
726
                 |    |  |    |
727
      ====^=================|    |==|    |=========================== band
728
      |   top split     |    |  |    |
729
      v                 | 1  |  | 2  |
730
      ^                 |    |  |    |  +----+   +----+
731
      |   merge zone    |    |  |    |  |    |   | 4  |
732
      v                 +----+  |    |  |    |   +----+
733
      ^                         |    |  | 3  |
734
      |   bottom split          |    |  |    |
735
      ====v=========================|    |==|    |===================
736
                 |    |  |    |
737
738
       possible cases:
739
       1) no top split, merge zone then a bottom split. The band will be split
740
        in two
741
       2) not band split, only the merge zone, band merged with rect but not split
742
       3) a top split, the merge zone and no bottom split. The band will be split
743
       in two
744
       4) a top split, the merge zone and also a bottom split. The band will be
745
       split in 3, but the coalesce algorithm may merge the created bands
746
       */
747
0
      UINT16 mergeTop = currentBand->top;
748
0
      UINT16 mergeBottom = currentBand->bottom;
749
750
      /* test if we need a top split, case 3 and 4 */
751
0
      if (rect->top > currentBand->top)
752
0
      {
753
0
        if (!region16_copy_band_with_union(newItems, currentBand, endSrcRect,
754
0
                                           currentBand->top, rect->top, nullptr, &usedRects,
755
0
                                           &nextBand))
756
0
          return FALSE;
757
0
        mergeTop = rect->top;
758
0
      }
759
760
      /* do the merge zone (all cases) */
761
0
      if (rect->bottom < currentBand->bottom)
762
0
        mergeBottom = rect->bottom;
763
764
0
      if (!region16_copy_band_with_union(newItems, currentBand, endSrcRect, mergeTop,
765
0
                                         mergeBottom, rect, &usedRects, &nextBand))
766
0
        return FALSE;
767
768
      /* test if we need a bottom split, case 1 and 4 */
769
0
      if (rect->bottom < currentBand->bottom)
770
0
      {
771
0
        if (!region16_copy_band_with_union(newItems, currentBand, endSrcRect, mergeBottom,
772
0
                                           currentBand->bottom, nullptr, &usedRects,
773
0
                                           &nextBand))
774
0
          return FALSE;
775
0
      }
776
777
0
      topInterBand = currentBand->bottom;
778
0
    }
779
780
    /* test if a piece of rect should be inserted as a new band between
781
     * the current band and the next one. band n and n+1 shouldn't touch.
782
     *
783
     * ==============================================================
784
     *                                                        band n
785
     *            +------+                    +------+
786
     * ===========| rect |====================|      |===============
787
     *            |      |    +------+        |      |
788
     *            +------+    | rect |        | rect |
789
     *                        +------+        |      |
790
     * =======================================|      |================
791
     *                                        +------+         band n+1
792
     * ===============================================================
793
     *
794
     */
795
0
    if ((nextBand < endSrcRect) && (nextBand->top != currentBand->bottom) &&
796
0
        (rect->bottom > currentBand->bottom) && (rect->top < nextBand->top))
797
0
    {
798
0
      RECTANGLE_16* dstRect = nextRect(newItems, usedRects++);
799
0
      if (!dstRect)
800
0
        return FALSE;
801
802
0
      dstRect->right = rect->right;
803
0
      dstRect->left = rect->left;
804
0
      dstRect->top = topInterBand;
805
0
      dstRect->bottom = MIN(nextBand->top, rect->bottom);
806
0
    }
807
808
0
    currentBand = nextBand;
809
0
  }
810
811
  /* adds the piece of rect that is below src */
812
0
  if (srcExtents->bottom < rect->bottom)
813
0
  {
814
0
    RECTANGLE_16* dstRect = nextRect(newItems, usedRects++);
815
0
    if (!dstRect)
816
0
      return FALSE;
817
818
0
    dstRect->top = MAX(srcExtents->bottom, rect->top);
819
0
    dstRect->left = rect->left;
820
0
    dstRect->right = rect->right;
821
0
    dstRect->bottom = rect->bottom;
822
0
  }
823
824
0
  dstExtents->top = MIN(rect->top, srcExtents->top);
825
0
  dstExtents->left = MIN(rect->left, srcExtents->left);
826
0
  dstExtents->bottom = MAX(rect->bottom, srcExtents->bottom);
827
0
  dstExtents->right = MAX(rect->right, srcExtents->right);
828
829
0
  newItems->nbRects = usedRects;
830
0
  freeRegion(dst->data);
831
0
  dst->data = newItems;
832
833
0
  return region16_simplify_bands(dst);
834
0
}
835
836
BOOL region16_intersects_rect(const REGION16* src, const RECTANGLE_16* arg2)
837
0
{
838
0
  const RECTANGLE_16* endPtr = nullptr;
839
0
  UINT32 nbRects = 0;
840
841
0
  if (!src || !src->data || !arg2)
842
0
    return FALSE;
843
844
0
  const RECTANGLE_16* rect = region16_rects(src, &nbRects);
845
846
0
  if (!nbRects)
847
0
    return FALSE;
848
849
0
  const RECTANGLE_16* srcExtents = region16_extents(src);
850
851
0
  if (nbRects == 1)
852
0
    return rectangles_intersects(srcExtents, arg2);
853
854
0
  if (!rectangles_intersects(srcExtents, arg2))
855
0
    return FALSE;
856
857
0
  for (endPtr = rect + nbRects; (rect < endPtr) && (arg2->bottom > rect->top); rect++)
858
0
  {
859
0
    if (rectangles_intersects(rect, arg2))
860
0
      return TRUE;
861
0
  }
862
863
0
  return FALSE;
864
0
}
865
866
BOOL region16_intersect_rect(REGION16* dst, const REGION16* src, const RECTANGLE_16* rect)
867
0
{
868
0
  const RECTANGLE_16* endPtr = nullptr;
869
0
  UINT32 nbRects = 0;
870
0
  RECTANGLE_16 common = WINPR_C_ARRAY_INIT;
871
872
0
  WINPR_ASSERT(dst);
873
0
  WINPR_ASSERT(src);
874
875
0
  const RECTANGLE_16* srcPtr = region16_rects(src, &nbRects);
876
877
0
  if (!nbRects)
878
0
  {
879
0
    region16_clear(dst);
880
0
    return TRUE;
881
0
  }
882
883
0
  const RECTANGLE_16* srcExtents = region16_extents(src);
884
885
0
  if (nbRects == 1)
886
0
  {
887
0
    BOOL intersects = rectangles_intersection(srcExtents, rect, &common);
888
0
    region16_clear(dst);
889
890
0
    if (intersects)
891
0
      return region16_union_rect(dst, dst, &common);
892
893
0
    return TRUE;
894
0
  }
895
896
0
  REGION16_DATA* newItems = allocateRegion(nbRects);
897
898
0
  if (!newItems)
899
0
    return FALSE;
900
901
0
  RECTANGLE_16* dstPtr = newItems->rects;
902
0
  UINT32 usedRects = 0;
903
0
  RECTANGLE_16 newExtents = WINPR_C_ARRAY_INIT;
904
905
  /* accumulate intersecting rectangles, the final region16_simplify_bands() will
906
   * do all the bad job to recreate correct rectangles
907
   */
908
0
  for (endPtr = srcPtr + nbRects; (srcPtr < endPtr) && (rect->bottom > srcPtr->top); srcPtr++)
909
0
  {
910
0
    if (usedRects > nbRects)
911
0
    {
912
0
      freeRegion(newItems);
913
0
      return FALSE;
914
0
    }
915
916
0
    if (rectangles_intersection(srcPtr, rect, &common))
917
0
    {
918
0
      *dstPtr = common;
919
0
      usedRects++;
920
0
      dstPtr++;
921
922
0
      if (rectangle_is_empty(&newExtents))
923
0
      {
924
        /* Check if the existing newExtents is empty. If it is empty, use
925
         * new common directly. We do not need to check common rectangle
926
         * because the rectangles_intersection() ensures that it is not empty.
927
         */
928
0
        newExtents = common;
929
0
      }
930
0
      else
931
0
      {
932
0
        newExtents.top = MIN(common.top, newExtents.top);
933
0
        newExtents.left = MIN(common.left, newExtents.left);
934
0
        newExtents.bottom = MAX(common.bottom, newExtents.bottom);
935
0
        newExtents.right = MAX(common.right, newExtents.right);
936
0
      }
937
0
    }
938
0
  }
939
940
0
  newItems->nbRects = usedRects;
941
942
0
  freeRegion(dst->data);
943
0
  dst->data = newItems;
944
0
  dst->extents = newExtents;
945
0
  return region16_simplify_bands(dst);
946
0
}
947
948
void region16_uninit(REGION16* region)
949
0
{
950
0
  WINPR_ASSERT(region);
951
952
0
  freeRegion(region->data);
953
0
  region->data = nullptr;
954
0
}