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