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