2014-01-31 20:02:50 +04:00
|
|
|
/**
|
2014-05-22 23:56:47 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2014-01-31 20:02:50 +04:00
|
|
|
*
|
2014-05-22 23:56:47 +04:00
|
|
|
* Copyright 2014 Thincast Technologies GmbH
|
|
|
|
* Copyright 2014 Hardening <contact@hardening-consulting.com>
|
2014-01-31 20:02:50 +04:00
|
|
|
*
|
2014-05-22 23:56:47 +04:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2014-01-31 20:02:50 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <winpr/memory.h>
|
2014-09-12 16:36:29 +04:00
|
|
|
#include <freerdp/log.h>
|
2014-02-05 18:52:29 +04:00
|
|
|
#include <freerdp/codec/region.h>
|
2014-01-31 20:02:50 +04:00
|
|
|
|
2014-09-12 16:36:29 +04:00
|
|
|
#define TAG FREERDP_TAG("codec")
|
|
|
|
|
2014-01-31 20:02:50 +04:00
|
|
|
/*
|
|
|
|
* The functions in this file implement the Region abstraction largely inspired from
|
|
|
|
* pixman library. The following comment is taken from the pixman code.
|
|
|
|
*
|
|
|
|
* A Region is simply a set of disjoint(non-overlapping) rectangles, plus an
|
|
|
|
* "extent" rectangle which is the smallest single rectangle that contains all
|
|
|
|
* the non-overlapping rectangles.
|
|
|
|
*
|
|
|
|
* A Region is implemented as a "y-x-banded" array of rectangles. This array
|
|
|
|
* imposes two degrees of order. First, all rectangles are sorted by top side
|
|
|
|
* y coordinate first (y1), and then by left side x coordinate (x1).
|
|
|
|
*
|
|
|
|
* Furthermore, the rectangles are grouped into "bands". Each rectangle in a
|
|
|
|
* band has the same top y coordinate (y1), and each has the same bottom y
|
|
|
|
* coordinate (y2). Thus all rectangles in a band differ only in their left
|
|
|
|
* and right side (x1 and x2). Bands are implicit in the array of rectangles:
|
|
|
|
* there is no separate list of band start pointers.
|
|
|
|
*
|
|
|
|
* The y-x band representation does not minimize rectangles. In particular,
|
|
|
|
* if a rectangle vertically crosses a band (the rectangle has scanlines in
|
|
|
|
* the y1 to y2 area spanned by the band), then the rectangle may be broken
|
|
|
|
* down into two or more smaller rectangles stacked one atop the other.
|
|
|
|
*
|
|
|
|
* ----------- -----------
|
|
|
|
* | | | | band 0
|
|
|
|
* | | -------- ----------- --------
|
|
|
|
* | | | | in y-x banded | | | | band 1
|
|
|
|
* | | | | form is | | | |
|
|
|
|
* ----------- | | ----------- --------
|
|
|
|
* | | | | band 2
|
|
|
|
* -------- --------
|
|
|
|
*
|
|
|
|
* An added constraint on the rectangles is that they must cover as much
|
|
|
|
* horizontal area as possible: no two rectangles within a band are allowed
|
|
|
|
* to touch.
|
|
|
|
*
|
|
|
|
* Whenever possible, bands will be merged together to cover a greater vertical
|
|
|
|
* distance (and thus reduce the number of rectangles). Two bands can be merged
|
|
|
|
* only if the bottom of one touches the top of the other and they have
|
|
|
|
* rectangles in the same places (of the same width, of course).
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct _REGION16_DATA {
|
|
|
|
long size;
|
|
|
|
long nbRects;
|
|
|
|
};
|
|
|
|
|
|
|
|
static REGION16_DATA empty_region = { 0, 0 };
|
|
|
|
|
|
|
|
void region16_init(REGION16 *region)
|
|
|
|
{
|
|
|
|
assert(region);
|
|
|
|
|
|
|
|
ZeroMemory(region, sizeof(REGION16));
|
|
|
|
region->data = &empty_region;
|
|
|
|
}
|
|
|
|
|
|
|
|
int region16_n_rects(const REGION16 *region)
|
|
|
|
{
|
|
|
|
assert(region);
|
|
|
|
assert(region->data);
|
|
|
|
|
|
|
|
return region->data->nbRects;
|
|
|
|
}
|
|
|
|
|
2016-04-05 18:07:45 +03:00
|
|
|
const RECTANGLE_16 *region16_rects(const REGION16 *region, UINT32 *nbRects)
|
2014-01-31 20:02:50 +04:00
|
|
|
{
|
|
|
|
REGION16_DATA *data;
|
|
|
|
|
|
|
|
assert(region);
|
|
|
|
|
|
|
|
data = region->data;
|
|
|
|
if (!data)
|
|
|
|
{
|
|
|
|
if (nbRects)
|
|
|
|
*nbRects = 0;
|
2016-02-12 03:34:48 +03:00
|
|
|
return NULL;
|
2014-01-31 20:02:50 +04:00
|
|
|
}
|
|
|
|
|
2016-02-12 03:34:48 +03:00
|
|
|
if (nbRects)
|
|
|
|
*nbRects = data->nbRects;
|
2014-01-31 20:02:50 +04:00
|
|
|
return (RECTANGLE_16 *)(data + 1);
|
|
|
|
}
|
|
|
|
|
2014-02-16 06:57:33 +04:00
|
|
|
static INLINE RECTANGLE_16 *region16_rects_noconst(REGION16 *region)
|
2014-01-31 20:02:50 +04:00
|
|
|
{
|
2014-06-20 20:35:21 +04:00
|
|
|
REGION16_DATA* data;
|
2014-01-31 20:02:50 +04:00
|
|
|
|
|
|
|
data = region->data;
|
2014-06-20 20:35:21 +04:00
|
|
|
|
2014-01-31 20:02:50 +04:00
|
|
|
if (!data)
|
2014-06-20 20:35:21 +04:00
|
|
|
return NULL;
|
2014-01-31 20:02:50 +04:00
|
|
|
|
2014-06-20 20:35:21 +04:00
|
|
|
return (RECTANGLE_16*)(&data[1]);
|
2014-01-31 20:02:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
const RECTANGLE_16 *region16_extents(const REGION16 *region)
|
|
|
|
{
|
|
|
|
return ®ion->extents;
|
|
|
|
}
|
|
|
|
|
|
|
|
static RECTANGLE_16 *region16_extents_noconst(REGION16 *region)
|
|
|
|
{
|
|
|
|
return ®ion->extents;
|
|
|
|
}
|
|
|
|
|
2014-09-11 00:27:24 +04:00
|
|
|
BOOL rectangle_is_empty(const RECTANGLE_16 *rect)
|
|
|
|
{
|
2015-03-20 19:04:30 +03:00
|
|
|
/* A rectangle with width = 0 or height = 0 should be regarded
|
2015-03-20 18:40:48 +03:00
|
|
|
* as empty.
|
|
|
|
*/
|
2015-03-20 19:04:30 +03:00
|
|
|
return ((rect->left == rect->right) || (rect->top == rect->bottom)) ? TRUE : FALSE;
|
2014-09-11 00:27:24 +04:00
|
|
|
}
|
|
|
|
|
2014-01-31 20:02:50 +04:00
|
|
|
BOOL region16_is_empty(const REGION16 *region)
|
|
|
|
{
|
|
|
|
assert(region);
|
|
|
|
assert(region->data);
|
|
|
|
|
|
|
|
return (region->data->nbRects == 0);
|
|
|
|
}
|
|
|
|
|
2014-09-19 06:18:58 +04:00
|
|
|
BOOL rectangles_equal(const RECTANGLE_16 *r1, const RECTANGLE_16 *r2)
|
|
|
|
{
|
|
|
|
return ((r1->left == r2->left) && (r1->top == r2->top) &&
|
|
|
|
(r1->right == r2->right) && (r1->bottom == r2->bottom)) ? TRUE : FALSE;
|
|
|
|
}
|
|
|
|
|
2014-01-31 20:02:50 +04:00
|
|
|
BOOL rectangles_intersects(const RECTANGLE_16 *r1, const RECTANGLE_16 *r2)
|
|
|
|
{
|
|
|
|
RECTANGLE_16 tmp;
|
|
|
|
return rectangles_intersection(r1, r2, &tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL rectangles_intersection(const RECTANGLE_16 *r1, const RECTANGLE_16 *r2,
|
|
|
|
RECTANGLE_16 *dst)
|
|
|
|
{
|
|
|
|
dst->left = MAX(r1->left, r2->left);
|
|
|
|
dst->right = MIN(r1->right, r2->right);
|
|
|
|
dst->top = MAX(r1->top, r2->top);
|
|
|
|
dst->bottom = MIN(r1->bottom, r2->bottom);
|
|
|
|
|
|
|
|
return (dst->left < dst->right) && (dst->top < dst->bottom);
|
|
|
|
}
|
|
|
|
|
|
|
|
void region16_clear(REGION16 *region)
|
|
|
|
{
|
|
|
|
assert(region);
|
|
|
|
assert(region->data);
|
|
|
|
|
|
|
|
if (region->data->size)
|
|
|
|
free(region->data);
|
2014-06-20 20:35:21 +04:00
|
|
|
|
2014-01-31 20:02:50 +04:00
|
|
|
region->data = &empty_region;
|
|
|
|
|
|
|
|
ZeroMemory(®ion->extents, sizeof(region->extents));
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:35:21 +04:00
|
|
|
static INLINE REGION16_DATA* allocateRegion(long nbItems)
|
2014-01-31 20:02:50 +04:00
|
|
|
{
|
2014-06-20 20:35:21 +04:00
|
|
|
long allocSize = sizeof(REGION16_DATA) + (nbItems * sizeof(RECTANGLE_16));
|
|
|
|
|
|
|
|
REGION16_DATA* ret = (REGION16_DATA*) malloc(allocSize);
|
|
|
|
|
2014-01-31 20:02:50 +04:00
|
|
|
if (!ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret->size = allocSize;
|
|
|
|
ret->nbRects = nbItems;
|
2014-06-20 20:35:21 +04:00
|
|
|
|
2014-01-31 20:02:50 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL region16_copy(REGION16 *dst, const REGION16 *src)
|
|
|
|
{
|
|
|
|
assert(dst);
|
|
|
|
assert(dst->data);
|
|
|
|
assert(src);
|
|
|
|
assert(src->data);
|
|
|
|
|
|
|
|
if (dst == src)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
dst->extents = src->extents;
|
2014-06-20 20:35:21 +04:00
|
|
|
|
2014-01-31 20:02:50 +04:00
|
|
|
if (dst->data->size)
|
|
|
|
free(dst->data);
|
|
|
|
|
|
|
|
if (!src->data->size)
|
|
|
|
{
|
|
|
|
dst->data = &empty_region;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dst->data = allocateRegion(src->data->nbRects);
|
2014-06-20 20:35:21 +04:00
|
|
|
|
2014-01-31 20:02:50 +04:00
|
|
|
if (!dst->data)
|
|
|
|
return FALSE;
|
|
|
|
|
2014-06-20 20:35:21 +04:00
|
|
|
CopyMemory(dst->data, src->data, src->data->size);
|
2014-01-31 20:02:50 +04:00
|
|
|
}
|
2014-06-20 20:35:21 +04:00
|
|
|
|
2014-01-31 20:02:50 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void region16_print(const REGION16 *region)
|
|
|
|
{
|
|
|
|
const RECTANGLE_16 *rects;
|
2016-04-05 18:07:45 +03:00
|
|
|
UINT32 nbRects, i;
|
2014-01-31 20:02:50 +04:00
|
|
|
int currentBandY = -1;
|
|
|
|
|
|
|
|
rects = region16_rects(region, &nbRects);
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_DBG(TAG, "nrects=%d", nbRects);
|
2014-01-31 20:02:50 +04:00
|
|
|
|
|
|
|
for (i = 0; i < nbRects; i++, rects++)
|
|
|
|
{
|
|
|
|
if (rects->top != currentBandY)
|
|
|
|
{
|
|
|
|
currentBandY = rects->top;
|
2015-01-20 13:09:54 +03:00
|
|
|
WLog_DBG(TAG, "band %d: ", currentBandY);
|
2014-01-31 20:02:50 +04:00
|
|
|
}
|
|
|
|
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_DBG(TAG, "(%d,%d-%d,%d)", rects->left, rects->top, rects->right, rects->bottom);
|
2014-01-31 20:02:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-05 18:07:45 +03:00
|
|
|
static void region16_copy_band_with_union(RECTANGLE_16 *dst,
|
2014-01-31 20:02:50 +04:00
|
|
|
const RECTANGLE_16 *src, const RECTANGLE_16 *end,
|
|
|
|
UINT16 newTop, UINT16 newBottom,
|
|
|
|
const RECTANGLE_16 *unionRect,
|
2016-04-05 18:07:45 +03:00
|
|
|
UINT32 *dstCounter,
|
2014-01-31 20:02:50 +04:00
|
|
|
const RECTANGLE_16 **srcPtr, RECTANGLE_16 **dstPtr)
|
|
|
|
{
|
|
|
|
UINT16 refY = src->top;
|
|
|
|
const RECTANGLE_16 *startOverlap, *endOverlap;
|
|
|
|
|
|
|
|
/* merges a band with the given rect
|
|
|
|
* Input:
|
|
|
|
* unionRect
|
|
|
|
* | |
|
|
|
|
* | |
|
|
|
|
* ==============+===============+================================
|
|
|
|
* |Item1| |Item2| |Item3| |Item4| |Item5| Band
|
|
|
|
* ==============+===============+================================
|
|
|
|
* before | overlap | after
|
|
|
|
*
|
|
|
|
* Resulting band:
|
|
|
|
* +-----+ +----------------------+ +-----+
|
|
|
|
* |Item1| | Item2 | |Item3|
|
|
|
|
* +-----+ +----------------------+ +-----+
|
|
|
|
*
|
|
|
|
* We first copy as-is items that are before Item2, the first overlapping
|
|
|
|
* item.
|
|
|
|
* Then we find the last one that overlap unionRect to agregate Item2, Item3
|
|
|
|
* and Item4 to create Item2.
|
|
|
|
* Finally Item5 is copied as Item3.
|
|
|
|
*
|
|
|
|
* When no unionRect is provided, we skip the two first steps to just copy items
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (unionRect)
|
|
|
|
{
|
|
|
|
/* items before unionRect */
|
|
|
|
while ((src < end) && (src->top == refY) && (src->right < unionRect->left))
|
|
|
|
{
|
|
|
|
dst->top = newTop;
|
|
|
|
dst->bottom = newBottom;
|
|
|
|
dst->right = src->right;
|
|
|
|
dst->left = src->left;
|
|
|
|
|
|
|
|
src++; dst++; *dstCounter += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* treat items overlapping with unionRect */
|
|
|
|
startOverlap = unionRect;
|
|
|
|
endOverlap = unionRect;
|
|
|
|
|
|
|
|
if ((src < end) && (src->top == refY) && (src->left < unionRect->left))
|
|
|
|
startOverlap = src;
|
|
|
|
|
|
|
|
while ((src < end) && (src->top == refY) && (src->right < unionRect->right))
|
|
|
|
{
|
|
|
|
src++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((src < end) && (src->top == refY) && (src->left < unionRect->right))
|
|
|
|
{
|
|
|
|
endOverlap = src;
|
|
|
|
src++;
|
|
|
|
}
|
|
|
|
|
|
|
|
dst->bottom = newBottom;
|
|
|
|
dst->top = newTop;
|
|
|
|
dst->left = startOverlap->left;
|
|
|
|
dst->right = endOverlap->right;
|
|
|
|
dst++; *dstCounter += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* treat remaining items on the same band */
|
|
|
|
while ((src < end) && (src->top == refY))
|
|
|
|
{
|
|
|
|
dst->top = newTop;
|
|
|
|
dst->bottom = newBottom;
|
|
|
|
dst->right = src->right;
|
|
|
|
dst->left = src->left;
|
|
|
|
|
|
|
|
src++; dst++; *dstCounter += 1;
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:35:21 +04:00
|
|
|
if (srcPtr)
|
2014-01-31 20:02:50 +04:00
|
|
|
*srcPtr = src;
|
2014-06-20 20:35:21 +04:00
|
|
|
|
2014-01-31 20:02:50 +04:00
|
|
|
*dstPtr = dst;
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:35:21 +04:00
|
|
|
static RECTANGLE_16* next_band(RECTANGLE_16* band1, RECTANGLE_16* endPtr, int* nbItems)
|
2014-01-31 20:02:50 +04:00
|
|
|
{
|
|
|
|
UINT16 refY = band1->top;
|
|
|
|
|
|
|
|
*nbItems = 0;
|
2014-06-20 20:35:21 +04:00
|
|
|
|
|
|
|
while((band1 < endPtr) && (band1->top == refY))
|
|
|
|
{
|
2014-01-31 20:02:50 +04:00
|
|
|
band1++;
|
|
|
|
*nbItems += 1;
|
|
|
|
}
|
2014-06-20 20:35:21 +04:00
|
|
|
|
2014-01-31 20:02:50 +04:00
|
|
|
return band1;
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:35:21 +04:00
|
|
|
static BOOL band_match(const RECTANGLE_16* band1, const RECTANGLE_16* band2, RECTANGLE_16* endPtr)
|
2014-01-31 20:02:50 +04:00
|
|
|
{
|
|
|
|
int refBand2 = band2->top;
|
2014-06-20 20:35:21 +04:00
|
|
|
const RECTANGLE_16* band2Start = band2;
|
2014-01-31 20:02:50 +04:00
|
|
|
|
2014-06-20 20:35:21 +04:00
|
|
|
while ((band1 < band2Start) && (band2 < endPtr) && (band2->top == refBand2))
|
|
|
|
{
|
2014-01-31 20:02:50 +04:00
|
|
|
if ((band1->left != band2->left) || (band1->right != band2->right))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
band1++;
|
|
|
|
band2++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (band1 != band2Start)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return (band2 == endPtr) || (band2->top != refBand2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** compute if the rectangle is fully included in the band
|
|
|
|
* @param band a pointer on the beginning of the band
|
|
|
|
* @param endPtr end of the region
|
|
|
|
* @param rect the rectangle to test
|
|
|
|
* @return if rect is fully included in an item of the band
|
|
|
|
*/
|
|
|
|
static BOOL rectangle_contained_in_band(const RECTANGLE_16 *band, const RECTANGLE_16 *endPtr,
|
|
|
|
const RECTANGLE_16 *rect)
|
|
|
|
{
|
|
|
|
UINT16 refY = band->top;
|
|
|
|
|
|
|
|
if ((band->top > rect->top) || (rect->bottom > band->bottom))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* note: as the band is sorted from left to right, once we've seen an item
|
|
|
|
* that is after rect->left we're sure that the result is False.
|
|
|
|
*/
|
|
|
|
while( (band < endPtr) && (band->top == refY) && (band->left <= rect->left))
|
|
|
|
{
|
|
|
|
if (rect->right <= band->right)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
band++;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL region16_simplify_bands(REGION16 *region)
|
|
|
|
{
|
2014-02-05 18:52:29 +04:00
|
|
|
/** Simplify consecutive bands that touch and have the same items
|
2014-01-31 20:02:50 +04:00
|
|
|
*
|
|
|
|
* ==================== ====================
|
|
|
|
* | 1 | | 2 | | | | |
|
|
|
|
* ==================== | | | |
|
|
|
|
* | 1 | | 2 | ====> | 1 | | 2 |
|
|
|
|
* ==================== | | | |
|
|
|
|
* | 1 | | 2 | | | | |
|
|
|
|
* ==================== ====================
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
RECTANGLE_16 *band1, *band2, *endPtr, *endBand, *tmp;
|
|
|
|
int nbRects, finalNbRects;
|
|
|
|
int bandItems, toMove;
|
|
|
|
|
|
|
|
finalNbRects = nbRects = region16_n_rects(region);
|
2014-06-20 20:35:21 +04:00
|
|
|
|
2014-01-31 20:02:50 +04:00
|
|
|
if (nbRects < 2)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
band1 = region16_rects_noconst(region);
|
|
|
|
endPtr = band1 + nbRects;
|
|
|
|
|
2014-06-20 20:35:21 +04:00
|
|
|
do
|
|
|
|
{
|
2014-01-31 20:02:50 +04:00
|
|
|
band2 = next_band(band1, endPtr, &bandItems);
|
2014-06-20 20:35:21 +04:00
|
|
|
|
2014-01-31 20:02:50 +04:00
|
|
|
if (band2 == endPtr)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if ((band1->bottom == band2->top) && band_match(band1, band2, endPtr))
|
|
|
|
{
|
|
|
|
/* adjust the bottom of band1 items */
|
|
|
|
tmp = band1;
|
|
|
|
while (tmp < band2)
|
|
|
|
{
|
|
|
|
tmp->bottom = band2->bottom;
|
|
|
|
tmp++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* override band2, we don't move band1 pointer as the band after band2
|
|
|
|
* may be merged too */
|
|
|
|
endBand = band2 + bandItems;
|
|
|
|
toMove = (endPtr - endBand) * sizeof(RECTANGLE_16);
|
2014-06-20 20:35:21 +04:00
|
|
|
|
2014-01-31 20:02:50 +04:00
|
|
|
if (toMove)
|
2014-06-20 20:35:21 +04:00
|
|
|
MoveMemory(band2, endBand, toMove);
|
|
|
|
|
2014-01-31 20:02:50 +04:00
|
|
|
finalNbRects -= bandItems;
|
|
|
|
endPtr -= bandItems;
|
2014-06-20 20:35:21 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-01-31 20:02:50 +04:00
|
|
|
band1 = band2;
|
|
|
|
}
|
2014-06-20 20:35:21 +04:00
|
|
|
}
|
|
|
|
while(TRUE);
|
2014-01-31 20:02:50 +04:00
|
|
|
|
|
|
|
if (finalNbRects != nbRects)
|
|
|
|
{
|
2014-06-20 20:35:21 +04:00
|
|
|
int allocSize = sizeof(REGION16_DATA) + (finalNbRects * sizeof(RECTANGLE_16));
|
2014-01-31 20:02:50 +04:00
|
|
|
region->data = realloc(region->data, allocSize);
|
2014-06-20 20:35:21 +04:00
|
|
|
|
2014-02-05 18:52:29 +04:00
|
|
|
if (!region->data)
|
|
|
|
{
|
|
|
|
region->data = &empty_region;
|
|
|
|
return FALSE;
|
|
|
|
}
|
2014-06-20 20:35:21 +04:00
|
|
|
|
2014-01-31 20:02:50 +04:00
|
|
|
region->data->nbRects = finalNbRects;
|
|
|
|
region->data->size = allocSize;
|
|
|
|
}
|
2014-06-20 20:35:21 +04:00
|
|
|
|
2014-01-31 20:02:50 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL region16_union_rect(REGION16 *dst, const REGION16 *src, const RECTANGLE_16 *rect)
|
|
|
|
{
|
2014-06-20 20:35:21 +04:00
|
|
|
const RECTANGLE_16* srcExtents;
|
|
|
|
RECTANGLE_16* dstExtents;
|
2014-01-31 20:02:50 +04:00
|
|
|
const RECTANGLE_16 *currentBand, *endSrcRect, *nextBand;
|
2014-06-20 20:35:21 +04:00
|
|
|
REGION16_DATA* newItems = NULL;
|
|
|
|
RECTANGLE_16* dstRect = NULL;
|
2016-04-05 18:07:45 +03:00
|
|
|
UINT32 usedRects, srcNbRects;
|
2014-01-31 20:02:50 +04:00
|
|
|
UINT16 topInterBand;
|
|
|
|
|
|
|
|
assert(src);
|
|
|
|
assert(src->data);
|
|
|
|
assert(dst);
|
|
|
|
|
|
|
|
srcExtents = region16_extents(src);
|
|
|
|
dstExtents = region16_extents_noconst(dst);
|
|
|
|
|
|
|
|
if (!region16_n_rects(src))
|
|
|
|
{
|
|
|
|
/* source is empty, so the union is rect */
|
|
|
|
dst->extents = *rect;
|
|
|
|
dst->data = allocateRegion(1);
|
2014-06-20 20:35:21 +04:00
|
|
|
|
2014-01-31 20:02:50 +04:00
|
|
|
if (!dst->data)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
dstRect = region16_rects_noconst(dst);
|
2014-06-20 20:35:21 +04:00
|
|
|
|
|
|
|
dstRect->top = rect->top;
|
|
|
|
dstRect->left = rect->left;
|
|
|
|
dstRect->right = rect->right;
|
|
|
|
dstRect->bottom = rect->bottom;
|
|
|
|
|
2014-01-31 20:02:50 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:35:21 +04:00
|
|
|
newItems = allocateRegion((1 + region16_n_rects(src)) * 4);
|
|
|
|
|
2014-01-31 20:02:50 +04:00
|
|
|
if (!newItems)
|
|
|
|
return FALSE;
|
|
|
|
|
2014-06-20 20:35:21 +04:00
|
|
|
dstRect = (RECTANGLE_16*) (&newItems[1]);
|
2014-01-31 20:02:50 +04:00
|
|
|
usedRects = 0;
|
|
|
|
|
|
|
|
/* adds the piece of rect that is on the top of src */
|
|
|
|
if (rect->top < srcExtents->top)
|
|
|
|
{
|
|
|
|
dstRect->top = rect->top;
|
|
|
|
dstRect->left = rect->left;
|
|
|
|
dstRect->right = rect->right;
|
2015-09-30 22:14:43 +03:00
|
|
|
dstRect->bottom = MIN(srcExtents->top, rect->bottom);
|
2014-01-31 20:02:50 +04:00
|
|
|
|
|
|
|
usedRects++;
|
|
|
|
dstRect++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* treat possibly overlapping region */
|
|
|
|
currentBand = region16_rects(src, &srcNbRects);
|
|
|
|
endSrcRect = currentBand + srcNbRects;
|
|
|
|
|
|
|
|
while (currentBand < endSrcRect)
|
|
|
|
{
|
|
|
|
if ((currentBand->bottom <= rect->top) || (rect->bottom <= currentBand->top) ||
|
2014-06-20 20:35:21 +04:00
|
|
|
rectangle_contained_in_band(currentBand, endSrcRect, rect))
|
2014-01-31 20:02:50 +04:00
|
|
|
{
|
|
|
|
/* no overlap between rect and the band, rect is totally below or totally above
|
|
|
|
* the current band, or rect is already covered by an item of the band.
|
|
|
|
* let's copy all the rectangles from this band
|
|
|
|
+----+
|
|
|
|
| | rect (case 1)
|
|
|
|
+----+
|
|
|
|
|
|
|
|
=================
|
2016-04-05 18:07:45 +03:00
|
|
|
band of srcRect
|
2014-01-31 20:02:50 +04:00
|
|
|
=================
|
|
|
|
+----+
|
|
|
|
| | rect (case 2)
|
|
|
|
+----+
|
|
|
|
*/
|
|
|
|
region16_copy_band_with_union(dstRect,
|
|
|
|
currentBand, endSrcRect,
|
|
|
|
currentBand->top, currentBand->bottom,
|
|
|
|
NULL, &usedRects,
|
|
|
|
&nextBand, &dstRect);
|
|
|
|
topInterBand = rect->top;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
|
|
|
|
/* rect overlaps the band:
|
2016-04-05 18:07:45 +03:00
|
|
|
| | | |
|
|
|
|
====^=================| |==| |=========================== band
|
|
|
|
| top split | | | |
|
|
|
|
v | 1 | | 2 |
|
|
|
|
^ | | | | +----+ +----+
|
|
|
|
| merge zone | | | | | | | 4 |
|
|
|
|
v +----+ | | | | +----+
|
|
|
|
^ | | | 3 |
|
|
|
|
| bottom split | | | |
|
|
|
|
====v=========================| |==| |===================
|
|
|
|
| | | |
|
2014-01-31 20:02:50 +04:00
|
|
|
|
|
|
|
possible cases:
|
|
|
|
1) no top split, merge zone then a bottom split. The band will be splitted
|
|
|
|
in two
|
|
|
|
2) not band split, only the merge zone, band merged with rect but not splitted
|
|
|
|
3) a top split, the merge zone and no bottom split. The band will be split
|
|
|
|
in two
|
|
|
|
4) a top split, the merge zone and also a bottom split. The band will be
|
|
|
|
splitted in 3, but the coalesce algorithm may merge the created bands
|
|
|
|
*/
|
|
|
|
UINT16 mergeTop = currentBand->top;
|
|
|
|
UINT16 mergeBottom = currentBand->bottom;
|
|
|
|
|
|
|
|
/* test if we need a top split, case 3 and 4 */
|
|
|
|
if (rect->top > currentBand->top)
|
|
|
|
{
|
|
|
|
region16_copy_band_with_union(dstRect,
|
|
|
|
currentBand, endSrcRect,
|
|
|
|
currentBand->top, rect->top,
|
|
|
|
NULL, &usedRects,
|
|
|
|
&nextBand, &dstRect);
|
|
|
|
mergeTop = rect->top;
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:35:21 +04:00
|
|
|
/* do the merge zone (all cases) */
|
2014-01-31 20:02:50 +04:00
|
|
|
if (rect->bottom < currentBand->bottom)
|
|
|
|
mergeBottom = rect->bottom;
|
2014-06-20 20:35:21 +04:00
|
|
|
|
2014-01-31 20:02:50 +04:00
|
|
|
region16_copy_band_with_union(dstRect,
|
|
|
|
currentBand, endSrcRect,
|
|
|
|
mergeTop, mergeBottom,
|
|
|
|
rect, &usedRects,
|
|
|
|
&nextBand, &dstRect);
|
|
|
|
|
|
|
|
/* test if we need a bottom split, case 1 and 4 */
|
|
|
|
if (rect->bottom < currentBand->bottom)
|
|
|
|
{
|
|
|
|
region16_copy_band_with_union(dstRect,
|
|
|
|
currentBand, endSrcRect,
|
|
|
|
mergeBottom, currentBand->bottom,
|
|
|
|
NULL, &usedRects,
|
|
|
|
&nextBand, &dstRect);
|
|
|
|
}
|
|
|
|
topInterBand = currentBand->bottom;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* test if a piece of rect should be inserted as a new band between
|
|
|
|
* the current band and the next one. band n and n+1 shouldn't touch.
|
|
|
|
*
|
|
|
|
* ==============================================================
|
|
|
|
* band n
|
|
|
|
* +------+ +------+
|
|
|
|
* ===========| rect |====================| |===============
|
|
|
|
* | | +------+ | |
|
|
|
|
* +------+ | rect | | rect |
|
|
|
|
* +------+ | |
|
|
|
|
* =======================================| |================
|
|
|
|
* +------+ band n+1
|
|
|
|
* ===============================================================
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
if ((nextBand < endSrcRect) && (nextBand->top != currentBand->bottom) &&
|
|
|
|
(rect->bottom > currentBand->bottom) && (rect->top < nextBand->top))
|
|
|
|
{
|
|
|
|
dstRect->right = rect->right;
|
|
|
|
dstRect->left = rect->left;
|
|
|
|
dstRect->top = topInterBand;
|
|
|
|
dstRect->bottom = MIN(nextBand->top, rect->bottom);
|
|
|
|
dstRect++; usedRects++;
|
|
|
|
}
|
|
|
|
|
|
|
|
currentBand = nextBand;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* adds the piece of rect that is below src */
|
|
|
|
if (srcExtents->bottom < rect->bottom)
|
|
|
|
{
|
|
|
|
dstRect->top = MAX(srcExtents->bottom, rect->top);
|
|
|
|
dstRect->left = rect->left;
|
|
|
|
dstRect->right = rect->right;
|
|
|
|
dstRect->bottom = rect->bottom;
|
|
|
|
|
|
|
|
usedRects++;
|
|
|
|
dstRect++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((src == dst) && (src->data->size))
|
|
|
|
free(src->data);
|
|
|
|
|
|
|
|
dstExtents->top = MIN(rect->top, srcExtents->top);
|
|
|
|
dstExtents->left = MIN(rect->left, srcExtents->left);
|
|
|
|
dstExtents->bottom = MAX(rect->bottom, srcExtents->bottom);
|
|
|
|
dstExtents->right = MAX(rect->right, srcExtents->right);
|
|
|
|
|
2014-06-20 20:35:21 +04:00
|
|
|
newItems->size = sizeof(REGION16_DATA) + (usedRects * sizeof(RECTANGLE_16));
|
2014-01-31 20:02:50 +04:00
|
|
|
dst->data = realloc(newItems, newItems->size);
|
2014-06-20 20:35:21 +04:00
|
|
|
|
2014-01-31 20:02:50 +04:00
|
|
|
if (!dst->data)
|
|
|
|
{
|
|
|
|
free(newItems);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2014-06-20 20:35:21 +04:00
|
|
|
|
2014-01-31 20:02:50 +04:00
|
|
|
dst->data->nbRects = usedRects;
|
|
|
|
|
|
|
|
return region16_simplify_bands(dst);
|
|
|
|
}
|
|
|
|
|
2014-02-16 06:57:33 +04:00
|
|
|
BOOL region16_intersects_rect(const REGION16 *src, const RECTANGLE_16 *arg2)
|
|
|
|
{
|
|
|
|
const RECTANGLE_16 *rect, *endPtr, *srcExtents;
|
2016-04-05 18:07:45 +03:00
|
|
|
UINT32 nbRects;
|
2014-02-16 06:57:33 +04:00
|
|
|
|
2014-01-31 20:02:50 +04:00
|
|
|
assert(src);
|
|
|
|
assert(src->data);
|
|
|
|
|
|
|
|
rect = region16_rects(src, &nbRects);
|
2014-06-20 20:35:21 +04:00
|
|
|
|
2014-01-31 20:02:50 +04:00
|
|
|
if (!nbRects)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
srcExtents = region16_extents(src);
|
2014-06-20 20:35:21 +04:00
|
|
|
|
2014-01-31 20:02:50 +04:00
|
|
|
if (nbRects == 1)
|
|
|
|
return rectangles_intersects(srcExtents, arg2);
|
|
|
|
|
|
|
|
if (!rectangles_intersects(srcExtents, arg2))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
endPtr = rect + nbRects;
|
|
|
|
|
2014-02-07 19:43:48 +04:00
|
|
|
for (endPtr = rect + nbRects; (rect < endPtr) && (arg2->bottom > rect->top); rect++)
|
2014-01-31 20:02:50 +04:00
|
|
|
{
|
|
|
|
if (rectangles_intersects(rect, arg2))
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL region16_intersect_rect(REGION16 *dst, const REGION16 *src, const RECTANGLE_16 *rect)
|
|
|
|
{
|
|
|
|
REGION16_DATA *newItems;
|
|
|
|
const RECTANGLE_16 *srcPtr, *endPtr, *srcExtents;
|
|
|
|
RECTANGLE_16 *dstPtr;
|
2016-04-05 18:07:45 +03:00
|
|
|
UINT32 nbRects, usedRects;
|
2014-01-31 20:02:50 +04:00
|
|
|
RECTANGLE_16 common, newExtents;
|
|
|
|
|
2014-02-16 06:57:33 +04:00
|
|
|
assert(src);
|
|
|
|
assert(src->data);
|
|
|
|
|
2014-01-31 20:02:50 +04:00
|
|
|
srcPtr = region16_rects(src, &nbRects);
|
2014-06-20 20:35:21 +04:00
|
|
|
|
2014-01-31 20:02:50 +04:00
|
|
|
if (!nbRects)
|
|
|
|
{
|
|
|
|
region16_clear(dst);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
srcExtents = region16_extents(src);
|
2014-06-20 20:35:21 +04:00
|
|
|
|
2014-01-31 20:02:50 +04:00
|
|
|
if (nbRects == 1)
|
|
|
|
{
|
|
|
|
BOOL intersects = rectangles_intersection(srcExtents, rect, &common);
|
|
|
|
|
|
|
|
region16_clear(dst);
|
2014-06-20 20:35:21 +04:00
|
|
|
|
2014-01-31 20:02:50 +04:00
|
|
|
if (intersects)
|
|
|
|
return region16_union_rect(dst, dst, &common);
|
2014-06-20 20:35:21 +04:00
|
|
|
|
2014-01-31 20:02:50 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
newItems = allocateRegion(nbRects);
|
2014-06-20 20:35:21 +04:00
|
|
|
|
2014-01-31 20:02:50 +04:00
|
|
|
if (!newItems)
|
|
|
|
return FALSE;
|
2014-06-20 20:35:21 +04:00
|
|
|
|
|
|
|
dstPtr = (RECTANGLE_16*) (&newItems[1]);
|
2014-01-31 20:02:50 +04:00
|
|
|
usedRects = 0;
|
|
|
|
ZeroMemory(&newExtents, sizeof(newExtents));
|
|
|
|
|
|
|
|
/* accumulate intersecting rectangles, the final region16_simplify_bands() will
|
|
|
|
* do all the bad job to recreate correct rectangles
|
|
|
|
*/
|
2014-06-20 20:35:21 +04:00
|
|
|
for (endPtr = srcPtr + nbRects; (srcPtr < endPtr) && (rect->bottom > srcPtr->top); srcPtr++)
|
2014-01-31 20:02:50 +04:00
|
|
|
{
|
|
|
|
if (rectangles_intersection(srcPtr, rect, &common))
|
|
|
|
{
|
|
|
|
*dstPtr = common;
|
|
|
|
usedRects++;
|
|
|
|
dstPtr++;
|
|
|
|
|
2015-03-20 18:40:48 +03:00
|
|
|
if (rectangle_is_empty(&newExtents))
|
|
|
|
{
|
2016-04-05 18:07:45 +03:00
|
|
|
/* Check if the existing newExtents is empty. If it is empty, use
|
|
|
|
* new common directly. We do not need to check common rectangle
|
2015-03-20 18:40:48 +03:00
|
|
|
* because the rectangles_intersection() ensures that it is not empty.
|
|
|
|
*/
|
|
|
|
newExtents = common;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newExtents.top = MIN(common.top, newExtents.top);
|
|
|
|
newExtents.left = MIN(common.left, newExtents.left);
|
|
|
|
newExtents.bottom = MAX(common.bottom, newExtents.bottom);
|
|
|
|
newExtents.right = MAX(common.right, newExtents.right);
|
|
|
|
}
|
2014-01-31 20:02:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
newItems->nbRects = usedRects;
|
2014-06-20 20:35:21 +04:00
|
|
|
newItems->size = sizeof(REGION16_DATA) + (usedRects * sizeof(RECTANGLE_16));
|
2014-01-31 20:02:50 +04:00
|
|
|
|
|
|
|
if (dst->data->size)
|
|
|
|
free(dst->data);
|
|
|
|
|
|
|
|
dst->data = realloc(newItems, newItems->size);
|
2014-06-20 20:35:21 +04:00
|
|
|
|
|
|
|
if (!dst->data)
|
|
|
|
{
|
2014-04-26 08:16:34 +04:00
|
|
|
free(newItems);
|
2014-01-31 20:02:50 +04:00
|
|
|
return FALSE;
|
2014-04-26 08:16:34 +04:00
|
|
|
}
|
2014-01-31 20:02:50 +04:00
|
|
|
|
|
|
|
dst->extents = newExtents;
|
|
|
|
return region16_simplify_bands(dst);
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:35:21 +04:00
|
|
|
void region16_uninit(REGION16 *region)
|
|
|
|
{
|
2014-01-31 20:02:50 +04:00
|
|
|
assert(region);
|
|
|
|
|
2015-02-05 23:10:24 +03:00
|
|
|
if (region->data)
|
|
|
|
{
|
|
|
|
if (region->data->size)
|
|
|
|
free(region->data);
|
2014-01-31 20:02:50 +04:00
|
|
|
|
2015-02-05 23:10:24 +03:00
|
|
|
region->data = NULL;
|
|
|
|
}
|
2014-06-20 20:35:21 +04:00
|
|
|
}
|