Coverage Report

Created: 2025-07-18 07:08

/src/FreeRDP/libfreerdp/codec/region.c
Line
Count
Source (jump to first uncovered line)
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
14.1k
{
79
14.1k
  WINPR_ASSERT(region);
80
81
14.1k
  const REGION16 empty = { 0 };
82
14.1k
  *region = empty;
83
14.1k
}
84
85
int region16_n_rects(const REGION16* region)
86
27.0k
{
87
27.0k
  WINPR_ASSERT(region);
88
27.0k
  if (!region->data)
89
309
    return 0;
90
91
26.7k
  return WINPR_ASSERTING_INT_CAST(int, region->data->nbRects);
92
26.7k
}
93
94
const RECTANGLE_16* region16_rects(const REGION16* region, UINT32* nbRects)
95
14.2k
{
96
14.2k
  if (nbRects)
97
14.2k
    *nbRects = 0;
98
99
14.2k
  if (!region)
100
0
    return NULL;
101
102
14.2k
  REGION16_DATA* data = region->data;
103
14.2k
  if (!data)
104
903
    return NULL;
105
106
13.3k
  if (nbRects)
107
13.3k
    *nbRects = WINPR_ASSERTING_INT_CAST(UINT32, data->nbRects);
108
109
13.3k
  return data->rects;
110
13.3k
}
111
112
static INLINE RECTANGLE_16* region16_rects_noconst(REGION16* region)
113
13.0k
{
114
13.0k
  WINPR_ASSERT(region);
115
116
13.0k
  REGION16_DATA* data = region->data;
117
118
13.0k
  if (!data)
119
0
    return NULL;
120
121
13.0k
  return data->rects;
122
13.0k
}
123
124
const RECTANGLE_16* region16_extents(const REGION16* region)
125
13.6k
{
126
13.6k
  if (!region)
127
0
    return NULL;
128
129
13.6k
  return &region->extents;
130
13.6k
}
131
132
static RECTANGLE_16* region16_extents_noconst(REGION16* region)
133
13.6k
{
134
13.6k
  if (!region)
135
0
    return NULL;
136
137
13.6k
  return &region->extents;
138
13.6k
}
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)) ? TRUE : FALSE;
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
             ? TRUE
166
0
             : FALSE;
167
0
}
168
169
BOOL rectangles_intersects(const RECTANGLE_16* r1, const RECTANGLE_16* r2)
170
0
{
171
0
  RECTANGLE_16 tmp = { 0 };
172
0
  return rectangles_intersection(r1, r2, &tmp);
173
0
}
174
175
BOOL rectangles_intersection(const RECTANGLE_16* r1, const RECTANGLE_16* r2, RECTANGLE_16* dst)
176
0
{
177
0
  WINPR_ASSERT(r1);
178
0
  WINPR_ASSERT(r2);
179
0
  WINPR_ASSERT(dst);
180
181
0
  dst->left = MAX(r1->left, r2->left);
182
0
  dst->right = MIN(r1->right, r2->right);
183
0
  dst->top = MAX(r1->top, r2->top);
184
0
  dst->bottom = MIN(r1->bottom, r2->bottom);
185
0
  return (dst->left < dst->right) && (dst->top < dst->bottom);
186
0
}
187
188
static void freeRegion(REGION16_DATA* data)
189
28.4k
{
190
28.4k
  if (data)
191
13.6k
    free(data->rects);
192
28.4k
  free(data);
193
28.4k
}
194
195
void region16_clear(REGION16* region)
196
903
{
197
903
  WINPR_ASSERT(region);
198
199
903
  freeRegion(region->data);
200
903
  region->data = NULL;
201
202
903
  const RECTANGLE_16 empty = { 0 };
203
903
  region->extents = empty;
204
903
}
205
206
WINPR_ATTR_MALLOC(freeRegion, 1)
207
static REGION16_DATA* allocateRegion(size_t nbItems)
208
13.6k
{
209
13.6k
  REGION16_DATA* data = calloc(1, sizeof(REGION16_DATA));
210
13.6k
  if (!data)
211
0
    return NULL;
212
213
13.6k
  if (nbItems > 0)
214
13.6k
  {
215
13.6k
    data->rects = calloc(nbItems, sizeof(RECTANGLE_16));
216
13.6k
    if (!data->rects)
217
0
    {
218
0
      free(data);
219
0
      return NULL;
220
0
    }
221
13.6k
  }
222
223
13.6k
  data->nbRects = nbItems;
224
13.6k
  return data;
225
13.6k
}
226
227
static inline RECTANGLE_16* nextRect(REGION16_DATA* data, size_t index)
228
64.9M
{
229
64.9M
  WINPR_ASSERT(data);
230
64.9M
  if (index + 1 > data->nbRects)
231
1.01M
  {
232
1.01M
    RECTANGLE_16* rects = realloc(data->rects, (index + 1) * sizeof(RECTANGLE_16));
233
1.01M
    if (!rects)
234
0
    {
235
0
      freeRegion(data);
236
0
      return NULL;
237
0
    }
238
239
1.01M
    const RECTANGLE_16 empty = { 0 };
240
1.01M
    rects[index] = empty;
241
1.01M
    data->nbRects = index + 1;
242
1.01M
    data->rects = rects;
243
1.01M
  }
244
64.9M
  return &data->rects[index];
245
64.9M
}
246
247
static BOOL resizeRegion(REGION16* region, size_t nbItems)
248
4.04k
{
249
4.04k
  WINPR_ASSERT(region);
250
4.04k
  if (nbItems == 0)
251
0
  {
252
0
    freeRegion(region->data);
253
0
    region->data = NULL;
254
0
    return TRUE;
255
0
  }
256
257
4.04k
  if (!region->data)
258
309
  {
259
309
    region->data = allocateRegion(nbItems);
260
309
    return region->data != NULL;
261
309
  }
262
263
3.73k
  RECTANGLE_16* rects = realloc(region->data->rects, nbItems * sizeof(RECTANGLE_16));
264
3.73k
  if (!rects)
265
0
  {
266
0
    free(region->data->rects);
267
0
    region->data->nbRects = 0;
268
0
    region->data->rects = NULL;
269
0
    return FALSE;
270
0
  }
271
272
3.73k
  for (size_t x = region->data->nbRects; x < nbItems; x++)
273
0
  {
274
0
    const RECTANGLE_16 empty = { 0 };
275
0
    rects[x] = empty;
276
0
  }
277
3.73k
  region->data->rects = rects;
278
3.73k
  region->data->nbRects = nbItems;
279
3.73k
  return TRUE;
280
3.73k
}
281
282
static inline BOOL region16_copy_data(REGION16* dst, const REGION16* src)
283
0
{
284
0
  WINPR_ASSERT(dst);
285
0
  WINPR_ASSERT(src);
286
287
0
  freeRegion(dst->data);
288
0
  dst->data = NULL;
289
290
0
  if (src->data && (src->data->nbRects > 0))
291
0
  {
292
0
    dst->data = allocateRegion(src->data->nbRects);
293
0
    if (!dst->data || !dst->data->rects)
294
0
      return FALSE;
295
0
    memcpy(dst->data->rects, src->data->rects, dst->data->nbRects * sizeof(RECTANGLE_16));
296
0
  }
297
0
  return TRUE;
298
0
}
299
300
BOOL region16_copy(REGION16* dst, const REGION16* src)
301
0
{
302
0
  if (dst == src)
303
0
    return TRUE;
304
305
0
  WINPR_ASSERT(dst);
306
0
  WINPR_ASSERT(src);
307
308
0
  dst->extents = src->extents;
309
310
0
  return region16_copy_data(dst, src);
311
0
}
312
313
void region16_print(const REGION16* region)
314
903
{
315
903
  UINT32 nbRects = 0;
316
903
  int currentBandY = -1;
317
903
  const RECTANGLE_16* rects = region16_rects(region, &nbRects);
318
319
903
  WLog_DBG(TAG, "nrects=%" PRIu32 "", nbRects);
320
321
903
  for (UINT32 i = 0; i < nbRects; i++)
322
0
  {
323
0
    const RECTANGLE_16* rect = &rects[i];
324
325
0
    if (rect->top != currentBandY)
326
0
    {
327
0
      currentBandY = rect->top;
328
0
      WLog_DBG(TAG, "band %d: ", currentBandY);
329
0
    }
330
331
0
    WLog_DBG(TAG, "(%" PRIu16 ",%" PRIu16 "-%" PRIu16 ",%" PRIu16 ")", rect->left, rect->top,
332
0
             rect->right, rect->bottom);
333
0
  }
334
903
}
335
336
static BOOL region16_copy_band_with_union(REGION16_DATA* region, const RECTANGLE_16* src,
337
                                          const RECTANGLE_16* end, UINT16 newTop, UINT16 newBottom,
338
                                          const RECTANGLE_16* unionRect, UINT32* dstCounter,
339
                                          const RECTANGLE_16** srcPtr)
340
5.21M
{
341
5.21M
  WINPR_ASSERT(region);
342
5.21M
  WINPR_ASSERT(src);
343
5.21M
  WINPR_ASSERT(end);
344
5.21M
  WINPR_ASSERT(dstCounter);
345
346
5.21M
  UINT16 refY = src->top;
347
348
  /* merges a band with the given rect
349
   * Input:
350
   *                   unionRect
351
   *               |               |
352
   *               |               |
353
   * ==============+===============+================================
354
   *   |Item1|  |Item2| |Item3|  |Item4|    |Item5|            Band
355
   * ==============+===============+================================
356
   *    before     |    overlap    |          after
357
   *
358
   * Resulting band:
359
   *   +-----+  +----------------------+    +-----+
360
   *   |Item1|  |      Item2           |    |Item3|
361
   *   +-----+  +----------------------+    +-----+
362
   *
363
   *  We first copy as-is items that are before Item2, the first overlapping
364
   *  item.
365
   *  Then we find the last one that overlap unionRect to aggregate Item2, Item3
366
   *  and Item4 to create Item2.
367
   *  Finally Item5 is copied as Item3.
368
   *
369
   *  When no unionRect is provided, we skip the two first steps to just copy items
370
   */
371
372
5.21M
  if (unionRect)
373
808k
  {
374
    /* items before unionRect */
375
6.07M
    while ((src < end) && (src->top == refY) && (src->right < unionRect->left))
376
5.26M
    {
377
5.26M
      RECTANGLE_16* dst = nextRect(region, (*dstCounter)++);
378
5.26M
      if (!dst)
379
0
        return FALSE;
380
5.26M
      dst->top = newTop;
381
5.26M
      dst->bottom = newBottom;
382
5.26M
      dst->right = src->right;
383
5.26M
      dst->left = src->left;
384
5.26M
      src++;
385
5.26M
    }
386
387
    /* treat items overlapping with unionRect */
388
808k
    const RECTANGLE_16* startOverlap = unionRect;
389
808k
    const RECTANGLE_16* endOverlap = unionRect;
390
391
808k
    if ((src < end) && (src->top == refY) && (src->left < unionRect->left))
392
460k
      startOverlap = src;
393
394
1.28M
    while ((src < end) && (src->top == refY) && (src->right < unionRect->right))
395
471k
    {
396
471k
      src++;
397
471k
    }
398
399
808k
    if ((src < end) && (src->top == refY) && (src->left < unionRect->right))
400
381k
    {
401
381k
      endOverlap = src;
402
381k
      src++;
403
381k
    }
404
405
808k
    {
406
808k
      RECTANGLE_16* dst = nextRect(region, (*dstCounter)++);
407
808k
      if (!dst)
408
0
        return FALSE;
409
808k
      dst->bottom = newBottom;
410
808k
      dst->top = newTop;
411
808k
      dst->left = startOverlap->left;
412
808k
      dst->right = endOverlap->right;
413
808k
    }
414
808k
  }
415
416
  /* treat remaining items on the same band */
417
64.0M
  while ((src < end) && (src->top == refY))
418
58.8M
  {
419
58.8M
    RECTANGLE_16* dst = nextRect(region, (*dstCounter)++);
420
58.8M
    if (!dst)
421
0
      return FALSE;
422
423
58.8M
    dst->top = newTop;
424
58.8M
    dst->bottom = newBottom;
425
58.8M
    dst->right = src->right;
426
58.8M
    dst->left = src->left;
427
58.8M
    src++;
428
58.8M
  }
429
430
5.21M
  if (srcPtr)
431
5.21M
    *srcPtr = src;
432
433
5.21M
  return TRUE;
434
5.21M
}
435
436
static RECTANGLE_16* next_band(RECTANGLE_16* band1, RECTANGLE_16* endPtr, int* nbItems)
437
5.21M
{
438
5.21M
  WINPR_ASSERT(band1);
439
5.21M
  WINPR_ASSERT(endPtr);
440
5.21M
  WINPR_ASSERT(nbItems);
441
442
5.21M
  UINT16 refY = band1->top;
443
5.21M
  *nbItems = 0;
444
445
70.1M
  while ((band1 < endPtr) && (band1->top == refY))
446
64.9M
  {
447
64.9M
    band1++;
448
64.9M
    *nbItems += 1;
449
64.9M
  }
450
451
5.21M
  return band1;
452
5.21M
}
453
454
static BOOL band_match(const RECTANGLE_16* band1, const RECTANGLE_16* band2,
455
                       const RECTANGLE_16* endPtr)
456
4.88M
{
457
4.88M
  int refBand2 = band2->top;
458
4.88M
  const RECTANGLE_16* band2Start = band2;
459
460
19.3M
  while ((band1 < band2Start) && (band2 < endPtr) && (band2->top == refBand2))
461
18.6M
  {
462
18.6M
    if ((band1->left != band2->left) || (band1->right != band2->right))
463
4.21M
      return FALSE;
464
465
14.4M
    band1++;
466
14.4M
    band2++;
467
14.4M
  }
468
469
673k
  if (band1 != band2Start)
470
191k
    return FALSE;
471
472
481k
  return (band2 == endPtr) || (band2->top != refBand2);
473
673k
}
474
475
/** compute if the rectangle is fully included in the band
476
 * @param band a pointer on the beginning of the band
477
 * @param endPtr end of the region
478
 * @param rect the rectangle to test
479
 * @return if rect is fully included in an item of the band
480
 */
481
static BOOL rectangle_contained_in_band(const RECTANGLE_16* band, const RECTANGLE_16* endPtr,
482
                                        const RECTANGLE_16* rect)
483
820k
{
484
820k
  WINPR_ASSERT(band);
485
820k
  WINPR_ASSERT(endPtr);
486
820k
  WINPR_ASSERT(rect);
487
488
820k
  UINT16 refY = band->top;
489
490
820k
  if ((band->top > rect->top) || (rect->bottom > band->bottom))
491
790k
    return FALSE;
492
493
  /* note: as the band is sorted from left to right, once we've seen an item
494
   *    that is after rect->left we're sure that the result is False.
495
   */
496
53.0k
  while ((band < endPtr) && (band->top == refY) && (band->left <= rect->left))
497
34.7k
  {
498
34.7k
    if (rect->right <= band->right)
499
11.7k
      return TRUE;
500
501
23.0k
    band++;
502
23.0k
  }
503
504
18.3k
  return FALSE;
505
30.0k
}
506
507
static BOOL region16_simplify_bands(REGION16* region)
508
13.3k
{
509
  /** Simplify consecutive bands that touch and have the same items
510
   *
511
   *  ====================          ====================
512
   *     | 1 |  | 2   |               |   |  |     |
513
   *  ====================            |   |  |     |
514
   *     | 1 |  | 2   |    ====>    | 1 |  |  2  |
515
   *  ====================            |   |  |     |
516
   *     | 1 |  | 2   |               |   |  |     |
517
   *  ====================          ====================
518
   *
519
   */
520
521
13.3k
  const int nbRects = region16_n_rects(region);
522
13.3k
  int finalNbRects = nbRects;
523
524
13.3k
  if (nbRects < 2)
525
604
    return TRUE;
526
527
12.7k
  RECTANGLE_16* band1 = region16_rects_noconst(region);
528
12.7k
  RECTANGLE_16* endPtr = band1 + nbRects;
529
530
12.7k
  do
531
5.21M
  {
532
5.21M
    int bandItems = 0;
533
5.21M
    RECTANGLE_16* band2 = next_band(band1, endPtr, &bandItems);
534
535
5.21M
    if (band2 == endPtr)
536
12.7k
      break;
537
538
5.20M
    if ((band1->bottom == band2->top) && band_match(band1, band2, endPtr))
539
106k
    {
540
      /* adjust the bottom of band1 items */
541
106k
      RECTANGLE_16* tmp = band1;
542
543
643k
      while (tmp < band2)
544
536k
      {
545
536k
        tmp->bottom = band2->bottom;
546
536k
        tmp++;
547
536k
      }
548
549
      /* override band2, we don't move band1 pointer as the band after band2
550
       * may be merged too */
551
106k
      const RECTANGLE_16* endBand = band2 + bandItems;
552
106k
      const size_t toMove =
553
212k
          WINPR_ASSERTING_INT_CAST(size_t, (endPtr - endBand)) * sizeof(RECTANGLE_16);
554
555
106k
      if (toMove)
556
105k
        MoveMemory(band2, endBand, toMove);
557
558
212k
      finalNbRects -= bandItems;
559
212k
      endPtr -= bandItems;
560
212k
    }
561
5.10M
    else
562
5.10M
    {
563
5.10M
      band1 = band2;
564
5.10M
    }
565
5.20M
  } while (TRUE);
566
567
12.7k
  if (finalNbRects != nbRects)
568
3.73k
  {
569
7.46k
    if (!resizeRegion(region, WINPR_ASSERTING_INT_CAST(size_t, finalNbRects)))
570
0
      return FALSE;
571
3.73k
  }
572
573
12.7k
  return TRUE;
574
12.7k
}
575
576
BOOL region16_union_rect(REGION16* dst, const REGION16* src, const RECTANGLE_16* rect)
577
13.6k
{
578
13.6k
  const RECTANGLE_16* nextBand = NULL;
579
13.6k
  UINT32 srcNbRects = 0;
580
13.6k
  UINT16 topInterBand = 0;
581
13.6k
  WINPR_ASSERT(src);
582
13.6k
  WINPR_ASSERT(dst);
583
584
13.6k
  const RECTANGLE_16* srcExtents = region16_extents(src);
585
13.6k
  RECTANGLE_16* dstExtents = region16_extents_noconst(dst);
586
587
13.6k
  const int nrSrcRects = region16_n_rects(src);
588
13.6k
  if (nrSrcRects == 0)
589
309
  {
590
    /* source is empty, so the union is rect */
591
309
    dst->extents = *rect;
592
593
309
    if (!resizeRegion(dst, 1))
594
0
      return FALSE;
595
596
309
    RECTANGLE_16* dstRect = region16_rects_noconst(dst);
597
309
    WINPR_ASSERT(dstRect);
598
599
309
    dstRect->top = rect->top;
600
309
    dstRect->left = rect->left;
601
309
    dstRect->right = rect->right;
602
309
    dstRect->bottom = rect->bottom;
603
309
    dst->data->nbRects = 1;
604
309
    return TRUE;
605
309
  }
606
607
26.7k
  REGION16_DATA* newItems = allocateRegion(WINPR_ASSERTING_INT_CAST(size_t, nrSrcRects + 1));
608
609
13.3k
  if (!newItems)
610
0
    return FALSE;
611
612
13.3k
  UINT32 usedRects = 0;
613
614
  /* adds the piece of rect that is on the top of src */
615
13.3k
  if (rect->top < srcExtents->top)
616
564
  {
617
564
    RECTANGLE_16* dstRect = nextRect(newItems, usedRects++);
618
564
    if (!dstRect)
619
0
      return FALSE;
620
621
564
    dstRect->top = rect->top;
622
564
    dstRect->left = rect->left;
623
564
    dstRect->right = rect->right;
624
564
    dstRect->bottom = MIN(srcExtents->top, rect->bottom);
625
564
  }
626
627
  /* treat possibly overlapping region */
628
13.3k
  const RECTANGLE_16* currentBand = region16_rects(src, &srcNbRects);
629
13.3k
  const RECTANGLE_16* endSrcRect = currentBand + srcNbRects;
630
631
5.09M
  while (currentBand < endSrcRect)
632
5.08M
  {
633
5.08M
    if ((currentBand->bottom <= rect->top) || (rect->bottom <= currentBand->top) ||
634
5.08M
        rectangle_contained_in_band(currentBand, endSrcRect, rect))
635
4.27M
    {
636
      /* no overlap between rect and the band, rect is totally below or totally above
637
       * the current band, or rect is already covered by an item of the band.
638
       * let's copy all the rectangles from this band
639
                  +----+
640
                  |    |   rect (case 1)
641
                  +----+
642
643
         =================
644
      band of srcRect
645
       =================
646
              +----+
647
              |    |   rect (case 2)
648
              +----+
649
      */
650
4.27M
      if (!region16_copy_band_with_union(newItems, currentBand, endSrcRect, currentBand->top,
651
4.27M
                                         currentBand->bottom, NULL, &usedRects, &nextBand))
652
0
        return FALSE;
653
4.27M
      topInterBand = rect->top;
654
4.27M
    }
655
808k
    else
656
808k
    {
657
      /* rect overlaps the band:
658
                 |    |  |    |
659
      ====^=================|    |==|    |=========================== band
660
      |   top split     |    |  |    |
661
      v                 | 1  |  | 2  |
662
      ^                 |    |  |    |  +----+   +----+
663
      |   merge zone    |    |  |    |  |    |   | 4  |
664
      v                 +----+  |    |  |    |   +----+
665
      ^                         |    |  | 3  |
666
      |   bottom split          |    |  |    |
667
      ====v=========================|    |==|    |===================
668
                 |    |  |    |
669
670
       possible cases:
671
       1) no top split, merge zone then a bottom split. The band will be split
672
        in two
673
       2) not band split, only the merge zone, band merged with rect but not split
674
       3) a top split, the merge zone and no bottom split. The band will be split
675
       in two
676
       4) a top split, the merge zone and also a bottom split. The band will be
677
       split in 3, but the coalesce algorithm may merge the created bands
678
       */
679
808k
      UINT16 mergeTop = currentBand->top;
680
808k
      UINT16 mergeBottom = currentBand->bottom;
681
682
      /* test if we need a top split, case 3 and 4 */
683
808k
      if (rect->top > currentBand->top)
684
73.2k
      {
685
73.2k
        if (!region16_copy_band_with_union(newItems, currentBand, endSrcRect,
686
73.2k
                                           currentBand->top, rect->top, NULL, &usedRects,
687
73.2k
                                           &nextBand))
688
0
          return FALSE;
689
73.2k
        mergeTop = rect->top;
690
73.2k
      }
691
692
      /* do the merge zone (all cases) */
693
808k
      if (rect->bottom < currentBand->bottom)
694
56.3k
        mergeBottom = rect->bottom;
695
696
808k
      if (!region16_copy_band_with_union(newItems, currentBand, endSrcRect, mergeTop,
697
808k
                                         mergeBottom, rect, &usedRects, &nextBand))
698
0
        return FALSE;
699
700
      /* test if we need a bottom split, case 1 and 4 */
701
808k
      if (rect->bottom < currentBand->bottom)
702
56.3k
      {
703
56.3k
        if (!region16_copy_band_with_union(newItems, currentBand, endSrcRect, mergeBottom,
704
56.3k
                                           currentBand->bottom, NULL, &usedRects,
705
56.3k
                                           &nextBand))
706
0
          return FALSE;
707
56.3k
      }
708
709
808k
      topInterBand = currentBand->bottom;
710
808k
    }
711
712
    /* test if a piece of rect should be inserted as a new band between
713
     * the current band and the next one. band n and n+1 shouldn't touch.
714
     *
715
     * ==============================================================
716
     *                                                        band n
717
     *            +------+                    +------+
718
     * ===========| rect |====================|      |===============
719
     *            |      |    +------+        |      |
720
     *            +------+    | rect |        | rect |
721
     *                        +------+        |      |
722
     * =======================================|      |================
723
     *                                        +------+         band n+1
724
     * ===============================================================
725
     *
726
     */
727
5.08M
    if ((nextBand < endSrcRect) && (nextBand->top != currentBand->bottom) &&
728
5.08M
        (rect->bottom > currentBand->bottom) && (rect->top < nextBand->top))
729
27.4k
    {
730
27.4k
      RECTANGLE_16* dstRect = nextRect(newItems, usedRects++);
731
27.4k
      if (!dstRect)
732
0
        return FALSE;
733
734
27.4k
      dstRect->right = rect->right;
735
27.4k
      dstRect->left = rect->left;
736
27.4k
      dstRect->top = topInterBand;
737
27.4k
      dstRect->bottom = MIN(nextBand->top, rect->bottom);
738
27.4k
    }
739
740
5.08M
    currentBand = nextBand;
741
5.08M
  }
742
743
  /* adds the piece of rect that is below src */
744
13.3k
  if (srcExtents->bottom < rect->bottom)
745
719
  {
746
719
    RECTANGLE_16* dstRect = nextRect(newItems, usedRects++);
747
719
    if (!dstRect)
748
0
      return FALSE;
749
750
719
    dstRect->top = MAX(srcExtents->bottom, rect->top);
751
719
    dstRect->left = rect->left;
752
719
    dstRect->right = rect->right;
753
719
    dstRect->bottom = rect->bottom;
754
719
  }
755
756
13.3k
  dstExtents->top = MIN(rect->top, srcExtents->top);
757
13.3k
  dstExtents->left = MIN(rect->left, srcExtents->left);
758
13.3k
  dstExtents->bottom = MAX(rect->bottom, srcExtents->bottom);
759
13.3k
  dstExtents->right = MAX(rect->right, srcExtents->right);
760
761
13.3k
  newItems->nbRects = usedRects;
762
13.3k
  freeRegion(dst->data);
763
13.3k
  dst->data = newItems;
764
765
13.3k
  return region16_simplify_bands(dst);
766
13.3k
}
767
768
BOOL region16_intersects_rect(const REGION16* src, const RECTANGLE_16* arg2)
769
0
{
770
0
  const RECTANGLE_16* endPtr = NULL;
771
0
  UINT32 nbRects = 0;
772
773
0
  if (!src || !src->data || !arg2)
774
0
    return FALSE;
775
776
0
  const RECTANGLE_16* rect = region16_rects(src, &nbRects);
777
778
0
  if (!nbRects)
779
0
    return FALSE;
780
781
0
  const RECTANGLE_16* srcExtents = region16_extents(src);
782
783
0
  if (nbRects == 1)
784
0
    return rectangles_intersects(srcExtents, arg2);
785
786
0
  if (!rectangles_intersects(srcExtents, arg2))
787
0
    return FALSE;
788
789
0
  for (endPtr = rect + nbRects; (rect < endPtr) && (arg2->bottom > rect->top); rect++)
790
0
  {
791
0
    if (rectangles_intersects(rect, arg2))
792
0
      return TRUE;
793
0
  }
794
795
0
  return FALSE;
796
0
}
797
798
BOOL region16_intersect_rect(REGION16* dst, const REGION16* src, const RECTANGLE_16* rect)
799
0
{
800
0
  const RECTANGLE_16* endPtr = NULL;
801
0
  UINT32 nbRects = 0;
802
0
  RECTANGLE_16 common = { 0 };
803
804
0
  WINPR_ASSERT(dst);
805
0
  WINPR_ASSERT(src);
806
807
0
  const RECTANGLE_16* srcPtr = region16_rects(src, &nbRects);
808
809
0
  if (!nbRects)
810
0
  {
811
0
    region16_clear(dst);
812
0
    return TRUE;
813
0
  }
814
815
0
  const RECTANGLE_16* srcExtents = region16_extents(src);
816
817
0
  if (nbRects == 1)
818
0
  {
819
0
    BOOL intersects = rectangles_intersection(srcExtents, rect, &common);
820
0
    region16_clear(dst);
821
822
0
    if (intersects)
823
0
      return region16_union_rect(dst, dst, &common);
824
825
0
    return TRUE;
826
0
  }
827
828
0
  REGION16_DATA* newItems = allocateRegion(nbRects);
829
830
0
  if (!newItems)
831
0
    return FALSE;
832
833
0
  RECTANGLE_16* dstPtr = newItems->rects;
834
0
  UINT32 usedRects = 0;
835
0
  RECTANGLE_16 newExtents = { 0 };
836
837
  /* accumulate intersecting rectangles, the final region16_simplify_bands() will
838
   * do all the bad job to recreate correct rectangles
839
   */
840
0
  for (endPtr = srcPtr + nbRects; (srcPtr < endPtr) && (rect->bottom > srcPtr->top); srcPtr++)
841
0
  {
842
0
    if (usedRects > nbRects)
843
0
    {
844
0
      freeRegion(newItems);
845
0
      return FALSE;
846
0
    }
847
848
0
    if (rectangles_intersection(srcPtr, rect, &common))
849
0
    {
850
0
      *dstPtr = common;
851
0
      usedRects++;
852
0
      dstPtr++;
853
854
0
      if (rectangle_is_empty(&newExtents))
855
0
      {
856
        /* Check if the existing newExtents is empty. If it is empty, use
857
         * new common directly. We do not need to check common rectangle
858
         * because the rectangles_intersection() ensures that it is not empty.
859
         */
860
0
        newExtents = common;
861
0
      }
862
0
      else
863
0
      {
864
0
        newExtents.top = MIN(common.top, newExtents.top);
865
0
        newExtents.left = MIN(common.left, newExtents.left);
866
0
        newExtents.bottom = MAX(common.bottom, newExtents.bottom);
867
0
        newExtents.right = MAX(common.right, newExtents.right);
868
0
      }
869
0
    }
870
0
  }
871
872
0
  newItems->nbRects = usedRects;
873
874
0
  freeRegion(dst->data);
875
0
  dst->data = newItems;
876
0
  dst->extents = newExtents;
877
0
  return region16_simplify_bands(dst);
878
0
}
879
880
void region16_uninit(REGION16* region)
881
14.1k
{
882
14.1k
  WINPR_ASSERT(region);
883
884
14.1k
  freeRegion(region->data);
885
14.1k
  region->data = NULL;
886
14.1k
}