FreeRDP
Loading...
Searching...
No Matches
codec/region.c
1
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
71struct S_REGION16_DATA
72{
73 size_t nbRects;
74 RECTANGLE_16* rects;
75};
76
77void region16_init(REGION16* region)
78{
79 WINPR_ASSERT(region);
80
81 const REGION16 empty = { 0 };
82 *region = empty;
83}
84
85int region16_n_rects(const REGION16* region)
86{
87 WINPR_ASSERT(region);
88 if (!region->data)
89 return 0;
90
91 return WINPR_ASSERTING_INT_CAST(int, region->data->nbRects);
92}
93
94const RECTANGLE_16* region16_rects(const REGION16* region, UINT32* nbRects)
95{
96 if (nbRects)
97 *nbRects = 0;
98
99 if (!region)
100 return NULL;
101
102 REGION16_DATA* data = region->data;
103 if (!data)
104 return NULL;
105
106 if (nbRects)
107 *nbRects = WINPR_ASSERTING_INT_CAST(UINT32, data->nbRects);
108
109 return data->rects;
110}
111
112static INLINE RECTANGLE_16* region16_rects_noconst(REGION16* region)
113{
114 WINPR_ASSERT(region);
115
116 REGION16_DATA* data = region->data;
117
118 if (!data)
119 return NULL;
120
121 return data->rects;
122}
123
124const RECTANGLE_16* region16_extents(const REGION16* region)
125{
126 if (!region)
127 return NULL;
128
129 return &region->extents;
130}
131
132static RECTANGLE_16* region16_extents_noconst(REGION16* region)
133{
134 if (!region)
135 return NULL;
136
137 return &region->extents;
138}
139
140BOOL rectangle_is_empty(const RECTANGLE_16* rect)
141{
142 WINPR_ASSERT(rect);
143
144 /* A rectangle with width <= 0 or height <= 0 should be regarded
145 * as empty.
146 */
147 return ((rect->left >= rect->right) || (rect->top >= rect->bottom)) ? TRUE : FALSE;
148}
149
150BOOL region16_is_empty(const REGION16* region)
151{
152 WINPR_ASSERT(region);
153 if (!region->data)
154 return TRUE;
155 return (region->data->nbRects == 0);
156}
157
158BOOL rectangles_equal(const RECTANGLE_16* r1, const RECTANGLE_16* r2)
159{
160 WINPR_ASSERT(r1);
161 WINPR_ASSERT(r2);
162
163 return ((r1->left == r2->left) && (r1->top == r2->top) && (r1->right == r2->right) &&
164 (r1->bottom == r2->bottom))
165 ? TRUE
166 : FALSE;
167}
168
169BOOL rectangles_intersects(const RECTANGLE_16* r1, const RECTANGLE_16* r2)
170{
171 RECTANGLE_16 tmp = { 0 };
172 return rectangles_intersection(r1, r2, &tmp);
173}
174
175BOOL rectangles_intersection(const RECTANGLE_16* r1, const RECTANGLE_16* r2, RECTANGLE_16* dst)
176{
177 WINPR_ASSERT(r1);
178 WINPR_ASSERT(r2);
179 WINPR_ASSERT(dst);
180
181 dst->left = MAX(r1->left, r2->left);
182 dst->right = MIN(r1->right, r2->right);
183 dst->top = MAX(r1->top, r2->top);
184 dst->bottom = MIN(r1->bottom, r2->bottom);
185 return (dst->left < dst->right) && (dst->top < dst->bottom);
186}
187
188static void freeRegion(REGION16_DATA* data)
189{
190 if (data)
191 free(data->rects);
192 free(data);
193}
194
195void region16_clear(REGION16* region)
196{
197 WINPR_ASSERT(region);
198
199 freeRegion(region->data);
200 region->data = NULL;
201
202 const RECTANGLE_16 empty = { 0 };
203 region->extents = empty;
204}
205
206WINPR_ATTR_MALLOC(freeRegion, 1)
207static REGION16_DATA* allocateRegion(size_t nbItems)
208{
209 REGION16_DATA* data = calloc(1, sizeof(REGION16_DATA));
210 if (!data)
211 return NULL;
212
213 if (nbItems > 0)
214 {
215 data->rects = calloc(nbItems, sizeof(RECTANGLE_16));
216 if (!data->rects)
217 {
218 free(data);
219 return NULL;
220 }
221 }
222
223 data->nbRects = nbItems;
224 return data;
225}
226
227static inline RECTANGLE_16* nextRect(REGION16_DATA* data, size_t index)
228{
229 WINPR_ASSERT(data);
230 if (index + 1 > data->nbRects)
231 {
232 RECTANGLE_16* rects = realloc(data->rects, (index + 1) * sizeof(RECTANGLE_16));
233 if (!rects)
234 {
235 freeRegion(data);
236 return NULL;
237 }
238
239 const RECTANGLE_16 empty = { 0 };
240 rects[index] = empty;
241 data->nbRects = index + 1;
242 data->rects = rects;
243 }
244 return &data->rects[index];
245}
246
247static BOOL resizeRegion(REGION16* region, size_t nbItems)
248{
249 WINPR_ASSERT(region);
250 if (nbItems == 0)
251 {
252 freeRegion(region->data);
253 region->data = NULL;
254 return TRUE;
255 }
256
257 if (!region->data)
258 {
259 region->data = allocateRegion(nbItems);
260 return region->data != NULL;
261 }
262
263 RECTANGLE_16* rects = realloc(region->data->rects, nbItems * sizeof(RECTANGLE_16));
264 if (!rects)
265 {
266 free(region->data->rects);
267 region->data->nbRects = 0;
268 region->data->rects = NULL;
269 return FALSE;
270 }
271
272 for (size_t x = region->data->nbRects; x < nbItems; x++)
273 {
274 const RECTANGLE_16 empty = { 0 };
275 rects[x] = empty;
276 }
277 region->data->rects = rects;
278 region->data->nbRects = nbItems;
279 return TRUE;
280}
281
282static inline BOOL region16_copy_data(REGION16* dst, const REGION16* src)
283{
284 WINPR_ASSERT(dst);
285 WINPR_ASSERT(src);
286
287 freeRegion(dst->data);
288 dst->data = NULL;
289
290 if (src->data && (src->data->nbRects > 0))
291 {
292 dst->data = allocateRegion(src->data->nbRects);
293 if (!dst->data || !dst->data->rects)
294 return FALSE;
295 memcpy(dst->data->rects, src->data->rects, dst->data->nbRects * sizeof(RECTANGLE_16));
296 }
297 return TRUE;
298}
299
300BOOL region16_copy(REGION16* dst, const REGION16* src)
301{
302 if (dst == src)
303 return TRUE;
304
305 WINPR_ASSERT(dst);
306 WINPR_ASSERT(src);
307
308 dst->extents = src->extents;
309
310 return region16_copy_data(dst, src);
311}
312
313void region16_print(const REGION16* region)
314{
315 UINT32 nbRects = 0;
316 int currentBandY = -1;
317 const RECTANGLE_16* rects = region16_rects(region, &nbRects);
318
319 WLog_DBG(TAG, "nrects=%" PRIu32 "", nbRects);
320
321 for (UINT32 i = 0; i < nbRects; i++)
322 {
323 const RECTANGLE_16* rect = &rects[i];
324
325 if (rect->top != currentBandY)
326 {
327 currentBandY = rect->top;
328 WLog_DBG(TAG, "band %d: ", currentBandY);
329 }
330
331 WLog_DBG(TAG, "(%" PRIu16 ",%" PRIu16 "-%" PRIu16 ",%" PRIu16 ")", rect->left, rect->top,
332 rect->right, rect->bottom);
333 }
334}
335
336static 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{
341 WINPR_ASSERT(region);
342 WINPR_ASSERT(src);
343 WINPR_ASSERT(end);
344 WINPR_ASSERT(dstCounter);
345
346 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 if (unionRect)
373 {
374 /* items before unionRect */
375 while ((src < end) && (src->top == refY) && (src->right < unionRect->left))
376 {
377 RECTANGLE_16* dst = nextRect(region, (*dstCounter)++);
378 if (!dst)
379 return FALSE;
380 dst->top = newTop;
381 dst->bottom = newBottom;
382 dst->right = src->right;
383 dst->left = src->left;
384 src++;
385 }
386
387 /* treat items overlapping with unionRect */
388 const RECTANGLE_16* startOverlap = unionRect;
389 const RECTANGLE_16* endOverlap = unionRect;
390
391 if ((src < end) && (src->top == refY) && (src->left < unionRect->left))
392 startOverlap = src;
393
394 while ((src < end) && (src->top == refY) && (src->right < unionRect->right))
395 {
396 src++;
397 }
398
399 if ((src < end) && (src->top == refY) && (src->left < unionRect->right))
400 {
401 endOverlap = src;
402 src++;
403 }
404
405 {
406 RECTANGLE_16* dst = nextRect(region, (*dstCounter)++);
407 if (!dst)
408 return FALSE;
409 dst->bottom = newBottom;
410 dst->top = newTop;
411 dst->left = startOverlap->left;
412 dst->right = endOverlap->right;
413 }
414 }
415
416 /* treat remaining items on the same band */
417 while ((src < end) && (src->top == refY))
418 {
419 RECTANGLE_16* dst = nextRect(region, (*dstCounter)++);
420 if (!dst)
421 return FALSE;
422
423 dst->top = newTop;
424 dst->bottom = newBottom;
425 dst->right = src->right;
426 dst->left = src->left;
427 src++;
428 }
429
430 if (srcPtr)
431 *srcPtr = src;
432
433 return TRUE;
434}
435
436static RECTANGLE_16* next_band(RECTANGLE_16* band1, RECTANGLE_16* endPtr, int* nbItems)
437{
438 WINPR_ASSERT(band1);
439 WINPR_ASSERT(endPtr);
440 WINPR_ASSERT(nbItems);
441
442 UINT16 refY = band1->top;
443 *nbItems = 0;
444
445 while ((band1 < endPtr) && (band1->top == refY))
446 {
447 band1++;
448 *nbItems += 1;
449 }
450
451 return band1;
452}
453
454static BOOL band_match(const RECTANGLE_16* band1, const RECTANGLE_16* band2,
455 const RECTANGLE_16* endPtr)
456{
457 int refBand2 = band2->top;
458 const RECTANGLE_16* band2Start = band2;
459
460 while ((band1 < band2Start) && (band2 < endPtr) && (band2->top == refBand2))
461 {
462 if ((band1->left != band2->left) || (band1->right != band2->right))
463 return FALSE;
464
465 band1++;
466 band2++;
467 }
468
469 if (band1 != band2Start)
470 return FALSE;
471
472 return (band2 == endPtr) || (band2->top != refBand2);
473}
474
481static BOOL rectangle_contained_in_band(const RECTANGLE_16* band, const RECTANGLE_16* endPtr,
482 const RECTANGLE_16* rect)
483{
484 WINPR_ASSERT(band);
485 WINPR_ASSERT(endPtr);
486 WINPR_ASSERT(rect);
487
488 UINT16 refY = band->top;
489
490 if ((band->top > rect->top) || (rect->bottom > band->bottom))
491 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 while ((band < endPtr) && (band->top == refY) && (band->left <= rect->left))
497 {
498 if (rect->right <= band->right)
499 return TRUE;
500
501 band++;
502 }
503
504 return FALSE;
505}
506
507static BOOL region16_simplify_bands(REGION16* region)
508{
521 const int nbRects = region16_n_rects(region);
522 int finalNbRects = nbRects;
523
524 if (nbRects < 2)
525 return TRUE;
526
527 RECTANGLE_16* band1 = region16_rects_noconst(region);
528 RECTANGLE_16* endPtr = band1 + nbRects;
529
530 do
531 {
532 int bandItems = 0;
533 RECTANGLE_16* band2 = next_band(band1, endPtr, &bandItems);
534
535 if (band2 == endPtr)
536 break;
537
538 if ((band1->bottom == band2->top) && band_match(band1, band2, endPtr))
539 {
540 /* adjust the bottom of band1 items */
541 RECTANGLE_16* tmp = band1;
542
543 while (tmp < band2)
544 {
545 tmp->bottom = band2->bottom;
546 tmp++;
547 }
548
549 /* override band2, we don't move band1 pointer as the band after band2
550 * may be merged too */
551 const RECTANGLE_16* endBand = band2 + bandItems;
552 const size_t toMove =
553 WINPR_ASSERTING_INT_CAST(size_t, (endPtr - endBand)) * sizeof(RECTANGLE_16);
554
555 if (toMove)
556 MoveMemory(band2, endBand, toMove);
557
558 finalNbRects -= bandItems;
559 endPtr -= bandItems;
560 }
561 else
562 {
563 band1 = band2;
564 }
565 } while (TRUE);
566
567 if (finalNbRects != nbRects)
568 {
569 if (!resizeRegion(region, WINPR_ASSERTING_INT_CAST(size_t, finalNbRects)))
570 return FALSE;
571 }
572
573 return TRUE;
574}
575
576BOOL region16_union_rect(REGION16* dst, const REGION16* src, const RECTANGLE_16* rect)
577{
578 const RECTANGLE_16* nextBand = NULL;
579 UINT32 srcNbRects = 0;
580 UINT16 topInterBand = 0;
581 WINPR_ASSERT(src);
582 WINPR_ASSERT(dst);
583
584 const RECTANGLE_16* srcExtents = region16_extents(src);
585 RECTANGLE_16* dstExtents = region16_extents_noconst(dst);
586
587 const int nrSrcRects = region16_n_rects(src);
588 if (nrSrcRects == 0)
589 {
590 /* source is empty, so the union is rect */
591 dst->extents = *rect;
592
593 if (!resizeRegion(dst, 1))
594 return FALSE;
595
596 RECTANGLE_16* dstRect = region16_rects_noconst(dst);
597 WINPR_ASSERT(dstRect);
598
599 dstRect->top = rect->top;
600 dstRect->left = rect->left;
601 dstRect->right = rect->right;
602 dstRect->bottom = rect->bottom;
603 dst->data->nbRects = 1;
604 return TRUE;
605 }
606
607 REGION16_DATA* newItems = allocateRegion(WINPR_ASSERTING_INT_CAST(size_t, nrSrcRects + 1));
608
609 if (!newItems)
610 return FALSE;
611
612 UINT32 usedRects = 0;
613
614 /* adds the piece of rect that is on the top of src */
615 if (rect->top < srcExtents->top)
616 {
617 RECTANGLE_16* dstRect = nextRect(newItems, usedRects++);
618 if (!dstRect)
619 return FALSE;
620
621 dstRect->top = rect->top;
622 dstRect->left = rect->left;
623 dstRect->right = rect->right;
624 dstRect->bottom = MIN(srcExtents->top, rect->bottom);
625 }
626
627 /* treat possibly overlapping region */
628 const RECTANGLE_16* currentBand = region16_rects(src, &srcNbRects);
629 const RECTANGLE_16* endSrcRect = currentBand + srcNbRects;
630
631 while (currentBand < endSrcRect)
632 {
633 if ((currentBand->bottom <= rect->top) || (rect->bottom <= currentBand->top) ||
634 rectangle_contained_in_band(currentBand, endSrcRect, rect))
635 {
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 if (!region16_copy_band_with_union(newItems, currentBand, endSrcRect, currentBand->top,
651 currentBand->bottom, NULL, &usedRects, &nextBand))
652 return FALSE;
653 topInterBand = rect->top;
654 }
655 else
656 {
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 UINT16 mergeTop = currentBand->top;
680 UINT16 mergeBottom = currentBand->bottom;
681
682 /* test if we need a top split, case 3 and 4 */
683 if (rect->top > currentBand->top)
684 {
685 if (!region16_copy_band_with_union(newItems, currentBand, endSrcRect,
686 currentBand->top, rect->top, NULL, &usedRects,
687 &nextBand))
688 return FALSE;
689 mergeTop = rect->top;
690 }
691
692 /* do the merge zone (all cases) */
693 if (rect->bottom < currentBand->bottom)
694 mergeBottom = rect->bottom;
695
696 if (!region16_copy_band_with_union(newItems, currentBand, endSrcRect, mergeTop,
697 mergeBottom, rect, &usedRects, &nextBand))
698 return FALSE;
699
700 /* test if we need a bottom split, case 1 and 4 */
701 if (rect->bottom < currentBand->bottom)
702 {
703 if (!region16_copy_band_with_union(newItems, currentBand, endSrcRect, mergeBottom,
704 currentBand->bottom, NULL, &usedRects,
705 &nextBand))
706 return FALSE;
707 }
708
709 topInterBand = currentBand->bottom;
710 }
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 if ((nextBand < endSrcRect) && (nextBand->top != currentBand->bottom) &&
728 (rect->bottom > currentBand->bottom) && (rect->top < nextBand->top))
729 {
730 RECTANGLE_16* dstRect = nextRect(newItems, usedRects++);
731 if (!dstRect)
732 return FALSE;
733
734 dstRect->right = rect->right;
735 dstRect->left = rect->left;
736 dstRect->top = topInterBand;
737 dstRect->bottom = MIN(nextBand->top, rect->bottom);
738 }
739
740 currentBand = nextBand;
741 }
742
743 /* adds the piece of rect that is below src */
744 if (srcExtents->bottom < rect->bottom)
745 {
746 RECTANGLE_16* dstRect = nextRect(newItems, usedRects++);
747 if (!dstRect)
748 return FALSE;
749
750 dstRect->top = MAX(srcExtents->bottom, rect->top);
751 dstRect->left = rect->left;
752 dstRect->right = rect->right;
753 dstRect->bottom = rect->bottom;
754 }
755
756 dstExtents->top = MIN(rect->top, srcExtents->top);
757 dstExtents->left = MIN(rect->left, srcExtents->left);
758 dstExtents->bottom = MAX(rect->bottom, srcExtents->bottom);
759 dstExtents->right = MAX(rect->right, srcExtents->right);
760
761 newItems->nbRects = usedRects;
762 freeRegion(dst->data);
763 dst->data = newItems;
764
765 return region16_simplify_bands(dst);
766}
767
768BOOL region16_intersects_rect(const REGION16* src, const RECTANGLE_16* arg2)
769{
770 const RECTANGLE_16* endPtr = NULL;
771 UINT32 nbRects = 0;
772
773 if (!src || !src->data || !arg2)
774 return FALSE;
775
776 const RECTANGLE_16* rect = region16_rects(src, &nbRects);
777
778 if (!nbRects)
779 return FALSE;
780
781 const RECTANGLE_16* srcExtents = region16_extents(src);
782
783 if (nbRects == 1)
784 return rectangles_intersects(srcExtents, arg2);
785
786 if (!rectangles_intersects(srcExtents, arg2))
787 return FALSE;
788
789 for (endPtr = rect + nbRects; (rect < endPtr) && (arg2->bottom > rect->top); rect++)
790 {
791 if (rectangles_intersects(rect, arg2))
792 return TRUE;
793 }
794
795 return FALSE;
796}
797
798BOOL region16_intersect_rect(REGION16* dst, const REGION16* src, const RECTANGLE_16* rect)
799{
800 const RECTANGLE_16* endPtr = NULL;
801 UINT32 nbRects = 0;
802 RECTANGLE_16 common = { 0 };
803
804 WINPR_ASSERT(dst);
805 WINPR_ASSERT(src);
806
807 const RECTANGLE_16* srcPtr = region16_rects(src, &nbRects);
808
809 if (!nbRects)
810 {
811 region16_clear(dst);
812 return TRUE;
813 }
814
815 const RECTANGLE_16* srcExtents = region16_extents(src);
816
817 if (nbRects == 1)
818 {
819 BOOL intersects = rectangles_intersection(srcExtents, rect, &common);
820 region16_clear(dst);
821
822 if (intersects)
823 return region16_union_rect(dst, dst, &common);
824
825 return TRUE;
826 }
827
828 REGION16_DATA* newItems = allocateRegion(nbRects);
829
830 if (!newItems)
831 return FALSE;
832
833 RECTANGLE_16* dstPtr = newItems->rects;
834 UINT32 usedRects = 0;
835 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 for (endPtr = srcPtr + nbRects; (srcPtr < endPtr) && (rect->bottom > srcPtr->top); srcPtr++)
841 {
842 if (usedRects > nbRects)
843 {
844 freeRegion(newItems);
845 return FALSE;
846 }
847
848 if (rectangles_intersection(srcPtr, rect, &common))
849 {
850 *dstPtr = common;
851 usedRects++;
852 dstPtr++;
853
854 if (rectangle_is_empty(&newExtents))
855 {
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 newExtents = common;
861 }
862 else
863 {
864 newExtents.top = MIN(common.top, newExtents.top);
865 newExtents.left = MIN(common.left, newExtents.left);
866 newExtents.bottom = MAX(common.bottom, newExtents.bottom);
867 newExtents.right = MAX(common.right, newExtents.right);
868 }
869 }
870 }
871
872 newItems->nbRects = usedRects;
873
874 freeRegion(dst->data);
875 dst->data = newItems;
876 dst->extents = newExtents;
877 return region16_simplify_bands(dst);
878}
879
880void region16_uninit(REGION16* region)
881{
882 WINPR_ASSERT(region);
883
884 freeRegion(region->data);
885 region->data = NULL;
886}