/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 | 0 | { |
79 | 0 | WINPR_ASSERT(region); |
80 | | |
81 | 0 | const REGION16 empty = { 0 }; |
82 | 0 | *region = empty; |
83 | 0 | } |
84 | | |
85 | | int region16_n_rects(const REGION16* region) |
86 | 0 | { |
87 | 0 | WINPR_ASSERT(region); |
88 | 0 | if (!region->data) |
89 | 0 | return 0; |
90 | | |
91 | 0 | return WINPR_ASSERTING_INT_CAST(int, region->data->nbRects); |
92 | 0 | } |
93 | | |
94 | | const RECTANGLE_16* region16_rects(const REGION16* region, UINT32* nbRects) |
95 | 0 | { |
96 | 0 | if (nbRects) |
97 | 0 | *nbRects = 0; |
98 | |
|
99 | 0 | if (!region) |
100 | 0 | return NULL; |
101 | | |
102 | 0 | REGION16_DATA* data = region->data; |
103 | 0 | if (!data) |
104 | 0 | return NULL; |
105 | | |
106 | 0 | if (nbRects) |
107 | 0 | *nbRects = WINPR_ASSERTING_INT_CAST(UINT32, data->nbRects); |
108 | | |
109 | 0 | return data->rects; |
110 | 0 | } |
111 | | |
112 | | static INLINE RECTANGLE_16* region16_rects_noconst(REGION16* region) |
113 | 0 | { |
114 | 0 | WINPR_ASSERT(region); |
115 | | |
116 | 0 | REGION16_DATA* data = region->data; |
117 | |
|
118 | 0 | if (!data) |
119 | 0 | return NULL; |
120 | | |
121 | 0 | return data->rects; |
122 | 0 | } |
123 | | |
124 | | const RECTANGLE_16* region16_extents(const REGION16* region) |
125 | 0 | { |
126 | 0 | if (!region) |
127 | 0 | return NULL; |
128 | | |
129 | 0 | return ®ion->extents; |
130 | 0 | } |
131 | | |
132 | | static RECTANGLE_16* region16_extents_noconst(REGION16* region) |
133 | 0 | { |
134 | 0 | if (!region) |
135 | 0 | return NULL; |
136 | | |
137 | 0 | return ®ion->extents; |
138 | 0 | } |
139 | | |
140 | | BOOL rectangle_is_empty(const RECTANGLE_16* rect) |
141 | 0 | { |
142 | 0 | WINPR_ASSERT(rect); |
143 | | |
144 | | /* A rectangle with width <= 0 or height <= 0 should be regarded |
145 | | * as empty. |
146 | | */ |
147 | 0 | return ((rect->left >= rect->right) || (rect->top >= rect->bottom)) ? 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 | 0 | { |
190 | 0 | if (data) |
191 | 0 | free(data->rects); |
192 | 0 | free(data); |
193 | 0 | } |
194 | | |
195 | | void region16_clear(REGION16* region) |
196 | 0 | { |
197 | 0 | WINPR_ASSERT(region); |
198 | | |
199 | 0 | freeRegion(region->data); |
200 | 0 | region->data = NULL; |
201 | |
|
202 | 0 | const RECTANGLE_16 empty = { 0 }; |
203 | 0 | region->extents = empty; |
204 | 0 | } |
205 | | |
206 | | WINPR_ATTR_MALLOC(freeRegion, 1) |
207 | | static REGION16_DATA* allocateRegion(size_t nbItems) |
208 | 0 | { |
209 | 0 | REGION16_DATA* data = calloc(1, sizeof(REGION16_DATA)); |
210 | 0 | if (!data) |
211 | 0 | return NULL; |
212 | | |
213 | 0 | if (nbItems > 0) |
214 | 0 | { |
215 | 0 | data->rects = calloc(nbItems, sizeof(RECTANGLE_16)); |
216 | 0 | if (!data->rects) |
217 | 0 | { |
218 | 0 | free(data); |
219 | 0 | return NULL; |
220 | 0 | } |
221 | 0 | } |
222 | | |
223 | 0 | data->nbRects = nbItems; |
224 | 0 | return data; |
225 | 0 | } |
226 | | |
227 | | static inline RECTANGLE_16* nextRect(REGION16_DATA* data, size_t index) |
228 | 0 | { |
229 | 0 | WINPR_ASSERT(data); |
230 | 0 | if (index + 1 > data->nbRects) |
231 | 0 | { |
232 | 0 | RECTANGLE_16* rects = realloc(data->rects, (index + 1) * sizeof(RECTANGLE_16)); |
233 | 0 | if (!rects) |
234 | 0 | { |
235 | 0 | freeRegion(data); |
236 | 0 | return NULL; |
237 | 0 | } |
238 | | |
239 | 0 | const RECTANGLE_16 empty = { 0 }; |
240 | 0 | rects[index] = empty; |
241 | 0 | data->nbRects = index + 1; |
242 | 0 | data->rects = rects; |
243 | 0 | } |
244 | 0 | return &data->rects[index]; |
245 | 0 | } |
246 | | |
247 | | static BOOL resizeRegion(REGION16* region, size_t nbItems) |
248 | 0 | { |
249 | 0 | WINPR_ASSERT(region); |
250 | 0 | if (nbItems == 0) |
251 | 0 | { |
252 | 0 | freeRegion(region->data); |
253 | 0 | region->data = NULL; |
254 | 0 | return TRUE; |
255 | 0 | } |
256 | | |
257 | 0 | if (!region->data) |
258 | 0 | { |
259 | 0 | region->data = allocateRegion(nbItems); |
260 | 0 | return region->data != NULL; |
261 | 0 | } |
262 | | |
263 | 0 | RECTANGLE_16* rects = realloc(region->data->rects, nbItems * sizeof(RECTANGLE_16)); |
264 | 0 | 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 | 0 | 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 | 0 | region->data->rects = rects; |
278 | 0 | region->data->nbRects = nbItems; |
279 | 0 | return TRUE; |
280 | 0 | } |
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 | 0 | { |
315 | 0 | UINT32 nbRects = 0; |
316 | 0 | int currentBandY = -1; |
317 | 0 | const RECTANGLE_16* rects = region16_rects(region, &nbRects); |
318 | |
|
319 | 0 | WLog_DBG(TAG, "nrects=%" PRIu32 "", nbRects); |
320 | |
|
321 | 0 | 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 | 0 | } |
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 | 0 | { |
341 | 0 | WINPR_ASSERT(region); |
342 | 0 | WINPR_ASSERT(src); |
343 | 0 | WINPR_ASSERT(end); |
344 | 0 | WINPR_ASSERT(dstCounter); |
345 | | |
346 | 0 | 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 | 0 | if (unionRect) |
373 | 0 | { |
374 | | /* items before unionRect */ |
375 | 0 | while ((src < end) && (src->top == refY) && (src->right < unionRect->left)) |
376 | 0 | { |
377 | 0 | RECTANGLE_16* dst = nextRect(region, (*dstCounter)++); |
378 | 0 | if (!dst) |
379 | 0 | return FALSE; |
380 | 0 | dst->top = newTop; |
381 | 0 | dst->bottom = newBottom; |
382 | 0 | dst->right = src->right; |
383 | 0 | dst->left = src->left; |
384 | 0 | src++; |
385 | 0 | } |
386 | | |
387 | | /* treat items overlapping with unionRect */ |
388 | 0 | const RECTANGLE_16* startOverlap = unionRect; |
389 | 0 | const RECTANGLE_16* endOverlap = unionRect; |
390 | |
|
391 | 0 | if ((src < end) && (src->top == refY) && (src->left < unionRect->left)) |
392 | 0 | startOverlap = src; |
393 | |
|
394 | 0 | while ((src < end) && (src->top == refY) && (src->right < unionRect->right)) |
395 | 0 | { |
396 | 0 | src++; |
397 | 0 | } |
398 | |
|
399 | 0 | if ((src < end) && (src->top == refY) && (src->left < unionRect->right)) |
400 | 0 | { |
401 | 0 | endOverlap = src; |
402 | 0 | src++; |
403 | 0 | } |
404 | |
|
405 | 0 | { |
406 | 0 | RECTANGLE_16* dst = nextRect(region, (*dstCounter)++); |
407 | 0 | if (!dst) |
408 | 0 | return FALSE; |
409 | 0 | dst->bottom = newBottom; |
410 | 0 | dst->top = newTop; |
411 | 0 | dst->left = startOverlap->left; |
412 | 0 | dst->right = endOverlap->right; |
413 | 0 | } |
414 | 0 | } |
415 | | |
416 | | /* treat remaining items on the same band */ |
417 | 0 | while ((src < end) && (src->top == refY)) |
418 | 0 | { |
419 | 0 | RECTANGLE_16* dst = nextRect(region, (*dstCounter)++); |
420 | 0 | if (!dst) |
421 | 0 | return FALSE; |
422 | | |
423 | 0 | dst->top = newTop; |
424 | 0 | dst->bottom = newBottom; |
425 | 0 | dst->right = src->right; |
426 | 0 | dst->left = src->left; |
427 | 0 | src++; |
428 | 0 | } |
429 | | |
430 | 0 | if (srcPtr) |
431 | 0 | *srcPtr = src; |
432 | |
|
433 | 0 | return TRUE; |
434 | 0 | } |
435 | | |
436 | | static RECTANGLE_16* next_band(RECTANGLE_16* band1, RECTANGLE_16* endPtr, int* nbItems) |
437 | 0 | { |
438 | 0 | WINPR_ASSERT(band1); |
439 | 0 | WINPR_ASSERT(endPtr); |
440 | 0 | WINPR_ASSERT(nbItems); |
441 | | |
442 | 0 | UINT16 refY = band1->top; |
443 | 0 | *nbItems = 0; |
444 | |
|
445 | 0 | while ((band1 < endPtr) && (band1->top == refY)) |
446 | 0 | { |
447 | 0 | band1++; |
448 | 0 | *nbItems += 1; |
449 | 0 | } |
450 | |
|
451 | 0 | return band1; |
452 | 0 | } |
453 | | |
454 | | static BOOL band_match(const RECTANGLE_16* band1, const RECTANGLE_16* band2, |
455 | | const RECTANGLE_16* endPtr) |
456 | 0 | { |
457 | 0 | int refBand2 = band2->top; |
458 | 0 | const RECTANGLE_16* band2Start = band2; |
459 | |
|
460 | 0 | while ((band1 < band2Start) && (band2 < endPtr) && (band2->top == refBand2)) |
461 | 0 | { |
462 | 0 | if ((band1->left != band2->left) || (band1->right != band2->right)) |
463 | 0 | return FALSE; |
464 | | |
465 | 0 | band1++; |
466 | 0 | band2++; |
467 | 0 | } |
468 | | |
469 | 0 | if (band1 != band2Start) |
470 | 0 | return FALSE; |
471 | | |
472 | 0 | return (band2 == endPtr) || (band2->top != refBand2); |
473 | 0 | } |
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 | 0 | { |
484 | 0 | WINPR_ASSERT(band); |
485 | 0 | WINPR_ASSERT(endPtr); |
486 | 0 | WINPR_ASSERT(rect); |
487 | | |
488 | 0 | UINT16 refY = band->top; |
489 | |
|
490 | 0 | if ((band->top > rect->top) || (rect->bottom > band->bottom)) |
491 | 0 | 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 | 0 | while ((band < endPtr) && (band->top == refY) && (band->left <= rect->left)) |
497 | 0 | { |
498 | 0 | if (rect->right <= band->right) |
499 | 0 | return TRUE; |
500 | | |
501 | 0 | band++; |
502 | 0 | } |
503 | | |
504 | 0 | return FALSE; |
505 | 0 | } |
506 | | |
507 | | static BOOL region16_simplify_bands(REGION16* region) |
508 | 0 | { |
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 | 0 | const int nbRects = region16_n_rects(region); |
522 | 0 | int finalNbRects = nbRects; |
523 | |
|
524 | 0 | if (nbRects < 2) |
525 | 0 | return TRUE; |
526 | | |
527 | 0 | RECTANGLE_16* band1 = region16_rects_noconst(region); |
528 | 0 | RECTANGLE_16* endPtr = band1 + nbRects; |
529 | |
|
530 | 0 | do |
531 | 0 | { |
532 | 0 | int bandItems = 0; |
533 | 0 | RECTANGLE_16* band2 = next_band(band1, endPtr, &bandItems); |
534 | |
|
535 | 0 | if (band2 == endPtr) |
536 | 0 | break; |
537 | | |
538 | 0 | if ((band1->bottom == band2->top) && band_match(band1, band2, endPtr)) |
539 | 0 | { |
540 | | /* adjust the bottom of band1 items */ |
541 | 0 | RECTANGLE_16* tmp = band1; |
542 | |
|
543 | 0 | while (tmp < band2) |
544 | 0 | { |
545 | 0 | tmp->bottom = band2->bottom; |
546 | 0 | tmp++; |
547 | 0 | } |
548 | | |
549 | | /* override band2, we don't move band1 pointer as the band after band2 |
550 | | * may be merged too */ |
551 | 0 | const RECTANGLE_16* endBand = band2 + bandItems; |
552 | 0 | const size_t toMove = |
553 | 0 | WINPR_ASSERTING_INT_CAST(size_t, (endPtr - endBand)) * sizeof(RECTANGLE_16); |
554 | | |
555 | 0 | if (toMove) |
556 | 0 | MoveMemory(band2, endBand, toMove); |
557 | |
|
558 | 0 | finalNbRects -= bandItems; |
559 | 0 | endPtr -= bandItems; |
560 | 0 | } |
561 | 0 | else |
562 | 0 | { |
563 | 0 | band1 = band2; |
564 | 0 | } |
565 | 0 | } while (TRUE); |
566 | | |
567 | 0 | if (finalNbRects != nbRects) |
568 | 0 | { |
569 | 0 | if (!resizeRegion(region, WINPR_ASSERTING_INT_CAST(size_t, finalNbRects))) |
570 | 0 | return FALSE; |
571 | 0 | } |
572 | | |
573 | 0 | return TRUE; |
574 | 0 | } |
575 | | |
576 | | BOOL region16_union_rect(REGION16* dst, const REGION16* src, const RECTANGLE_16* rect) |
577 | 0 | { |
578 | 0 | const RECTANGLE_16* nextBand = NULL; |
579 | 0 | UINT32 srcNbRects = 0; |
580 | 0 | UINT16 topInterBand = 0; |
581 | 0 | WINPR_ASSERT(src); |
582 | 0 | WINPR_ASSERT(dst); |
583 | | |
584 | 0 | const RECTANGLE_16* srcExtents = region16_extents(src); |
585 | 0 | RECTANGLE_16* dstExtents = region16_extents_noconst(dst); |
586 | |
|
587 | 0 | const int nrSrcRects = region16_n_rects(src); |
588 | 0 | if (nrSrcRects == 0) |
589 | 0 | { |
590 | | /* source is empty, so the union is rect */ |
591 | 0 | dst->extents = *rect; |
592 | |
|
593 | 0 | if (!resizeRegion(dst, 1)) |
594 | 0 | return FALSE; |
595 | | |
596 | 0 | RECTANGLE_16* dstRect = region16_rects_noconst(dst); |
597 | 0 | WINPR_ASSERT(dstRect); |
598 | | |
599 | 0 | dstRect->top = rect->top; |
600 | 0 | dstRect->left = rect->left; |
601 | 0 | dstRect->right = rect->right; |
602 | 0 | dstRect->bottom = rect->bottom; |
603 | 0 | dst->data->nbRects = 1; |
604 | 0 | return TRUE; |
605 | 0 | } |
606 | | |
607 | 0 | REGION16_DATA* newItems = allocateRegion(WINPR_ASSERTING_INT_CAST(size_t, nrSrcRects + 1)); |
608 | | |
609 | 0 | if (!newItems) |
610 | 0 | return FALSE; |
611 | | |
612 | 0 | UINT32 usedRects = 0; |
613 | | |
614 | | /* adds the piece of rect that is on the top of src */ |
615 | 0 | if (rect->top < srcExtents->top) |
616 | 0 | { |
617 | 0 | RECTANGLE_16* dstRect = nextRect(newItems, usedRects++); |
618 | 0 | if (!dstRect) |
619 | 0 | return FALSE; |
620 | | |
621 | 0 | dstRect->top = rect->top; |
622 | 0 | dstRect->left = rect->left; |
623 | 0 | dstRect->right = rect->right; |
624 | 0 | dstRect->bottom = MIN(srcExtents->top, rect->bottom); |
625 | 0 | } |
626 | | |
627 | | /* treat possibly overlapping region */ |
628 | 0 | const RECTANGLE_16* currentBand = region16_rects(src, &srcNbRects); |
629 | 0 | const RECTANGLE_16* endSrcRect = currentBand + srcNbRects; |
630 | |
|
631 | 0 | while (currentBand < endSrcRect) |
632 | 0 | { |
633 | 0 | if ((currentBand->bottom <= rect->top) || (rect->bottom <= currentBand->top) || |
634 | 0 | rectangle_contained_in_band(currentBand, endSrcRect, rect)) |
635 | 0 | { |
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 | 0 | if (!region16_copy_band_with_union(newItems, currentBand, endSrcRect, currentBand->top, |
651 | 0 | currentBand->bottom, NULL, &usedRects, &nextBand)) |
652 | 0 | return FALSE; |
653 | 0 | topInterBand = rect->top; |
654 | 0 | } |
655 | 0 | else |
656 | 0 | { |
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 | 0 | UINT16 mergeTop = currentBand->top; |
680 | 0 | UINT16 mergeBottom = currentBand->bottom; |
681 | | |
682 | | /* test if we need a top split, case 3 and 4 */ |
683 | 0 | if (rect->top > currentBand->top) |
684 | 0 | { |
685 | 0 | if (!region16_copy_band_with_union(newItems, currentBand, endSrcRect, |
686 | 0 | currentBand->top, rect->top, NULL, &usedRects, |
687 | 0 | &nextBand)) |
688 | 0 | return FALSE; |
689 | 0 | mergeTop = rect->top; |
690 | 0 | } |
691 | | |
692 | | /* do the merge zone (all cases) */ |
693 | 0 | if (rect->bottom < currentBand->bottom) |
694 | 0 | mergeBottom = rect->bottom; |
695 | |
|
696 | 0 | if (!region16_copy_band_with_union(newItems, currentBand, endSrcRect, mergeTop, |
697 | 0 | mergeBottom, rect, &usedRects, &nextBand)) |
698 | 0 | return FALSE; |
699 | | |
700 | | /* test if we need a bottom split, case 1 and 4 */ |
701 | 0 | if (rect->bottom < currentBand->bottom) |
702 | 0 | { |
703 | 0 | if (!region16_copy_band_with_union(newItems, currentBand, endSrcRect, mergeBottom, |
704 | 0 | currentBand->bottom, NULL, &usedRects, |
705 | 0 | &nextBand)) |
706 | 0 | return FALSE; |
707 | 0 | } |
708 | | |
709 | 0 | topInterBand = currentBand->bottom; |
710 | 0 | } |
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 | 0 | if ((nextBand < endSrcRect) && (nextBand->top != currentBand->bottom) && |
728 | 0 | (rect->bottom > currentBand->bottom) && (rect->top < nextBand->top)) |
729 | 0 | { |
730 | 0 | RECTANGLE_16* dstRect = nextRect(newItems, usedRects++); |
731 | 0 | if (!dstRect) |
732 | 0 | return FALSE; |
733 | | |
734 | 0 | dstRect->right = rect->right; |
735 | 0 | dstRect->left = rect->left; |
736 | 0 | dstRect->top = topInterBand; |
737 | 0 | dstRect->bottom = MIN(nextBand->top, rect->bottom); |
738 | 0 | } |
739 | | |
740 | 0 | currentBand = nextBand; |
741 | 0 | } |
742 | | |
743 | | /* adds the piece of rect that is below src */ |
744 | 0 | if (srcExtents->bottom < rect->bottom) |
745 | 0 | { |
746 | 0 | RECTANGLE_16* dstRect = nextRect(newItems, usedRects++); |
747 | 0 | if (!dstRect) |
748 | 0 | return FALSE; |
749 | | |
750 | 0 | dstRect->top = MAX(srcExtents->bottom, rect->top); |
751 | 0 | dstRect->left = rect->left; |
752 | 0 | dstRect->right = rect->right; |
753 | 0 | dstRect->bottom = rect->bottom; |
754 | 0 | } |
755 | | |
756 | 0 | dstExtents->top = MIN(rect->top, srcExtents->top); |
757 | 0 | dstExtents->left = MIN(rect->left, srcExtents->left); |
758 | 0 | dstExtents->bottom = MAX(rect->bottom, srcExtents->bottom); |
759 | 0 | dstExtents->right = MAX(rect->right, srcExtents->right); |
760 | |
|
761 | 0 | newItems->nbRects = usedRects; |
762 | 0 | freeRegion(dst->data); |
763 | 0 | dst->data = newItems; |
764 | |
|
765 | 0 | return region16_simplify_bands(dst); |
766 | 0 | } |
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 | 0 | { |
882 | 0 | WINPR_ASSERT(region); |
883 | | |
884 | 0 | freeRegion(region->data); |
885 | 0 | region->data = NULL; |
886 | 0 | } |