22#include <winpr/assert.h>
23#include <winpr/memory.h>
24#include <freerdp/log.h>
25#include <freerdp/codec/region.h>
27#define TAG FREERDP_TAG("codec")
81 const REGION16 empty = WINPR_C_ARRAY_INIT;
85int region16_n_rects(
const REGION16* region)
91 return WINPR_ASSERTING_INT_CAST(
int, region->data->nbRects);
102 REGION16_DATA* data = region->data;
107 *nbRects = WINPR_ASSERTING_INT_CAST(UINT32, data->nbRects);
114 WINPR_ASSERT(region);
116 REGION16_DATA* data = region->data;
129 return ®ion->extents;
137 return ®ion->extents;
147 return ((rect->left >= rect->right) || (rect->top >= rect->bottom));
150BOOL region16_is_empty(
const REGION16* region)
152 WINPR_ASSERT(region);
155 return (region->data->nbRects == 0);
163 return ((r1->left == r2->left) && (r1->top == r2->top) && (r1->right == r2->right) &&
164 (r1->bottom == r2->bottom));
170 return rectangles_intersection(r1, r2, &tmp);
179 dst->left = MAX(r1->left, r2->left);
180 dst->right = MIN(r1->right, r2->right);
181 dst->top = MAX(r1->top, r2->top);
182 dst->bottom = MIN(r1->bottom, r2->bottom);
183 return (dst->left < dst->right) && (dst->top < dst->bottom);
186static void freeRegion(REGION16_DATA* data)
193void region16_clear(
REGION16* region)
195 WINPR_ASSERT(region);
197 freeRegion(region->data);
198 region->data =
nullptr;
201 region->extents = empty;
204WINPR_ATTR_MALLOC(freeRegion, 1)
206static REGION16_DATA* allocateRegion(
size_t nbItems)
208 REGION16_DATA* data = calloc(1,
sizeof(REGION16_DATA));
222 data->nbRects = nbItems;
226static inline RECTANGLE_16* nextRect(REGION16_DATA* data,
size_t index)
229 if (index + 1 > data->nbRects)
239 rects[index] = empty;
240 data->nbRects = index + 1;
243 return &data->rects[index];
246static BOOL resizeRegion(
REGION16* region,
size_t nbItems)
248 WINPR_ASSERT(region);
251 freeRegion(region->data);
252 region->data =
nullptr;
258 region->data = allocateRegion(nbItems);
259 return region->data !=
nullptr;
265 free(region->data->rects);
266 region->data->nbRects = 0;
267 region->data->rects =
nullptr;
271 for (
size_t x = region->data->nbRects; x < nbItems; x++)
276 region->data->rects = rects;
277 region->data->nbRects = nbItems;
286 freeRegion(dst->data);
289 if (src->data && (src->data->nbRects > 0))
291 dst->data = allocateRegion(src->data->nbRects);
292 if (!dst->data || !dst->data->rects)
294 memcpy(dst->data->rects, src->data->rects, dst->data->nbRects *
sizeof(
RECTANGLE_16));
307 dst->extents = src->extents;
309 return region16_copy_data(dst, src);
312void region16_print(
const REGION16* region)
315 int currentBandY = -1;
316 const RECTANGLE_16* rects = region16_rects(region, &nbRects);
318 WLog_DBG(TAG,
"nrects=%" PRIu32
"", nbRects);
320 for (UINT32 i = 0; i < nbRects; i++)
324 if (rect->top != currentBandY)
326 currentBandY = rect->top;
327 WLog_DBG(TAG,
"band %d: ", currentBandY);
330 WLog_DBG(TAG,
"(%" PRIu16
",%" PRIu16
"-%" PRIu16
",%" PRIu16
")", rect->left, rect->top,
331 rect->right, rect->bottom);
335static BOOL region16_copy_band_with_union(REGION16_DATA* region,
const RECTANGLE_16* src,
336 const RECTANGLE_16* end, UINT16 newTop, UINT16 newBottom,
340 WINPR_ASSERT(region);
343 WINPR_ASSERT(dstCounter);
345 UINT16 refY = src->top;
374 while ((src < end) && (src->top == refY) && (src->right < unionRect->left))
380 dst->bottom = newBottom;
381 dst->right = src->right;
382 dst->left = src->left;
390 if ((src < end) && (src->top == refY) && (src->left < unionRect->left))
393 while ((src < end) && (src->top == refY) && (src->right < unionRect->right))
398 if ((src < end) && (src->top == refY) && (src->left < unionRect->right))
408 dst->bottom = newBottom;
410 dst->left = startOverlap->left;
411 dst->right = endOverlap->right;
416 while ((src < end) && (src->top == refY))
423 dst->bottom = newBottom;
424 dst->right = src->right;
425 dst->left = src->left;
438 WINPR_ASSERT(endPtr);
439 WINPR_ASSERT(nbItems);
441 UINT16 refY = band1->top;
444 while ((band1 < endPtr) && (band1->top == refY))
456 int refBand2 = band2->top;
459 while ((band1 < band2Start) && (band2 < endPtr) && (band2->top == refBand2))
461 if ((band1->left != band2->left) || (band1->right != band2->right))
468 if (band1 != band2Start)
471 return (band2 == endPtr) || (band2->top != refBand2);
484 WINPR_ASSERT(endPtr);
487 UINT16 refY = band->top;
489 if ((band->top > rect->top) || (rect->bottom > band->bottom))
495 while ((band < endPtr) && (band->top == refY) && (band->left <= rect->left))
497 if (rect->right <= band->right)
506static BOOL region16_simplify_bands(
REGION16* region)
520 const int nbRects = region16_n_rects(region);
521 int finalNbRects = nbRects;
532 RECTANGLE_16* band2 = next_band(band1, endPtr, &bandItems);
537 if ((band1->bottom == band2->top) && band_match(band1, band2, endPtr))
544 tmp->bottom = band2->bottom;
551 const size_t toMove =
552 WINPR_ASSERTING_INT_CAST(
size_t, (endPtr - endBand)) *
sizeof(
RECTANGLE_16);
555 MoveMemory(band2, endBand, toMove);
557 finalNbRects -= bandItems;
566 if (finalNbRects != nbRects)
568 if (!resizeRegion(region, WINPR_ASSERTING_INT_CAST(
size_t, finalNbRects)))
578 UINT32 srcNbRects = 0;
579 UINT16 topInterBand = 0;
584 RECTANGLE_16* dstExtents = region16_extents_noconst(dst);
586 const int nrSrcRects = region16_n_rects(src);
590 dst->extents = *rect;
592 if (!resizeRegion(dst, 1))
596 WINPR_ASSERT(dstRect);
598 dstRect->top = rect->top;
599 dstRect->left = rect->left;
600 dstRect->right = rect->right;
601 dstRect->bottom = rect->bottom;
602 dst->data->nbRects = 1;
606 REGION16_DATA* newItems = allocateRegion(WINPR_ASSERTING_INT_CAST(
size_t, nrSrcRects + 1));
611 UINT32 usedRects = 0;
614 if (rect->top < srcExtents->top)
616 RECTANGLE_16* dstRect = nextRect(newItems, usedRects++);
620 dstRect->top = rect->top;
621 dstRect->left = rect->left;
622 dstRect->right = rect->right;
623 dstRect->bottom = MIN(srcExtents->top, rect->bottom);
627 const RECTANGLE_16* currentBand = region16_rects(src, &srcNbRects);
628 const RECTANGLE_16* endSrcRect = currentBand + srcNbRects;
630 while (currentBand < endSrcRect)
632 if ((currentBand->bottom <= rect->top) || (rect->bottom <= currentBand->top) ||
633 rectangle_contained_in_band(currentBand, endSrcRect, rect))
649 if (!region16_copy_band_with_union(newItems, currentBand, endSrcRect, currentBand->top,
650 currentBand->bottom,
nullptr, &usedRects, &nextBand))
652 topInterBand = rect->top;
678 UINT16 mergeTop = currentBand->top;
679 UINT16 mergeBottom = currentBand->bottom;
682 if (rect->top > currentBand->top)
684 if (!region16_copy_band_with_union(newItems, currentBand, endSrcRect,
685 currentBand->top, rect->top,
nullptr, &usedRects,
688 mergeTop = rect->top;
692 if (rect->bottom < currentBand->bottom)
693 mergeBottom = rect->bottom;
695 if (!region16_copy_band_with_union(newItems, currentBand, endSrcRect, mergeTop,
696 mergeBottom, rect, &usedRects, &nextBand))
700 if (rect->bottom < currentBand->bottom)
702 if (!region16_copy_band_with_union(newItems, currentBand, endSrcRect, mergeBottom,
703 currentBand->bottom,
nullptr, &usedRects,
708 topInterBand = currentBand->bottom;
726 if ((nextBand < endSrcRect) && (nextBand->top != currentBand->bottom) &&
727 (rect->bottom > currentBand->bottom) && (rect->top < nextBand->top))
729 RECTANGLE_16* dstRect = nextRect(newItems, usedRects++);
733 dstRect->right = rect->right;
734 dstRect->left = rect->left;
735 dstRect->top = topInterBand;
736 dstRect->bottom = MIN(nextBand->top, rect->bottom);
739 currentBand = nextBand;
743 if (srcExtents->bottom < rect->bottom)
745 RECTANGLE_16* dstRect = nextRect(newItems, usedRects++);
749 dstRect->top = MAX(srcExtents->bottom, rect->top);
750 dstRect->left = rect->left;
751 dstRect->right = rect->right;
752 dstRect->bottom = rect->bottom;
755 dstExtents->top = MIN(rect->top, srcExtents->top);
756 dstExtents->left = MIN(rect->left, srcExtents->left);
757 dstExtents->bottom = MAX(rect->bottom, srcExtents->bottom);
758 dstExtents->right = MAX(rect->right, srcExtents->right);
760 newItems->nbRects = usedRects;
761 freeRegion(dst->data);
762 dst->data = newItems;
764 return region16_simplify_bands(dst);
772 if (!src || !src->data || !arg2)
775 const RECTANGLE_16* rect = region16_rects(src, &nbRects);
783 return rectangles_intersects(srcExtents, arg2);
785 if (!rectangles_intersects(srcExtents, arg2))
788 for (endPtr = rect + nbRects; (rect < endPtr) && (arg2->bottom > rect->top); rect++)
790 if (rectangles_intersects(rect, arg2))
806 const RECTANGLE_16* srcPtr = region16_rects(src, &nbRects);
818 BOOL intersects = rectangles_intersection(srcExtents, rect, &common);
822 return region16_union_rect(dst, dst, &common);
827 REGION16_DATA* newItems = allocateRegion(nbRects);
833 UINT32 usedRects = 0;
839 for (endPtr = srcPtr + nbRects; (srcPtr < endPtr) && (rect->bottom > srcPtr->top); srcPtr++)
841 if (usedRects > nbRects)
843 freeRegion(newItems);
847 if (rectangles_intersection(srcPtr, rect, &common))
853 if (rectangle_is_empty(&newExtents))
863 newExtents.top = MIN(common.top, newExtents.top);
864 newExtents.left = MIN(common.left, newExtents.left);
865 newExtents.bottom = MAX(common.bottom, newExtents.bottom);
866 newExtents.right = MAX(common.right, newExtents.right);
871 newItems->nbRects = usedRects;
873 freeRegion(dst->data);
874 dst->data = newItems;
875 dst->extents = newExtents;
876 return region16_simplify_bands(dst);
879void region16_uninit(
REGION16* region)
881 WINPR_ASSERT(region);
883 freeRegion(region->data);
884 region->data =
nullptr;