Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright 1987, 1988, 1989, 1998 The Open Group |
michael@0 | 3 | * |
michael@0 | 4 | * Permission to use, copy, modify, distribute, and sell this software and its |
michael@0 | 5 | * documentation for any purpose is hereby granted without fee, provided that |
michael@0 | 6 | * the above copyright notice appear in all copies and that both that |
michael@0 | 7 | * copyright notice and this permission notice appear in supporting |
michael@0 | 8 | * documentation. |
michael@0 | 9 | * |
michael@0 | 10 | * The above copyright notice and this permission notice shall be included in |
michael@0 | 11 | * all copies or substantial portions of the Software. |
michael@0 | 12 | * |
michael@0 | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
michael@0 | 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
michael@0 | 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
michael@0 | 16 | * OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN |
michael@0 | 17 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
michael@0 | 18 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
michael@0 | 19 | * |
michael@0 | 20 | * Except as contained in this notice, the name of The Open Group shall not be |
michael@0 | 21 | * used in advertising or otherwise to promote the sale, use or other dealings |
michael@0 | 22 | * in this Software without prior written authorization from The Open Group. |
michael@0 | 23 | * |
michael@0 | 24 | * Copyright 1987, 1988, 1989 by |
michael@0 | 25 | * Digital Equipment Corporation, Maynard, Massachusetts. |
michael@0 | 26 | * |
michael@0 | 27 | * All Rights Reserved |
michael@0 | 28 | * |
michael@0 | 29 | * Permission to use, copy, modify, and distribute this software and its |
michael@0 | 30 | * documentation for any purpose and without fee is hereby granted, |
michael@0 | 31 | * provided that the above copyright notice appear in all copies and that |
michael@0 | 32 | * both that copyright notice and this permission notice appear in |
michael@0 | 33 | * supporting documentation, and that the name of Digital not be |
michael@0 | 34 | * used in advertising or publicity pertaining to distribution of the |
michael@0 | 35 | * software without specific, written prior permission. |
michael@0 | 36 | * |
michael@0 | 37 | * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING |
michael@0 | 38 | * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL |
michael@0 | 39 | * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR |
michael@0 | 40 | * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
michael@0 | 41 | * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, |
michael@0 | 42 | * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS |
michael@0 | 43 | * SOFTWARE. |
michael@0 | 44 | * |
michael@0 | 45 | * Copyright © 1998 Keith Packard |
michael@0 | 46 | * |
michael@0 | 47 | * Permission to use, copy, modify, distribute, and sell this software and its |
michael@0 | 48 | * documentation for any purpose is hereby granted without fee, provided that |
michael@0 | 49 | * the above copyright notice appear in all copies and that both that |
michael@0 | 50 | * copyright notice and this permission notice appear in supporting |
michael@0 | 51 | * documentation, and that the name of Keith Packard not be used in |
michael@0 | 52 | * advertising or publicity pertaining to distribution of the software without |
michael@0 | 53 | * specific, written prior permission. Keith Packard makes no |
michael@0 | 54 | * representations about the suitability of this software for any purpose. It |
michael@0 | 55 | * is provided "as is" without express or implied warranty. |
michael@0 | 56 | * |
michael@0 | 57 | * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
michael@0 | 58 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
michael@0 | 59 | * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
michael@0 | 60 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, |
michael@0 | 61 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
michael@0 | 62 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
michael@0 | 63 | * PERFORMANCE OF THIS SOFTWARE. |
michael@0 | 64 | */ |
michael@0 | 65 | |
michael@0 | 66 | #include <stdlib.h> |
michael@0 | 67 | #include <limits.h> |
michael@0 | 68 | #include <string.h> |
michael@0 | 69 | #include <stdio.h> |
michael@0 | 70 | #include "pixman-private.h" |
michael@0 | 71 | |
michael@0 | 72 | #define PIXREGION_NIL(reg) ((reg)->data && !(reg)->data->numRects) |
michael@0 | 73 | /* not a region */ |
michael@0 | 74 | #define PIXREGION_NAR(reg) ((reg)->data == pixman_broken_data) |
michael@0 | 75 | #define PIXREGION_NUMRECTS(reg) ((reg)->data ? (reg)->data->numRects : 1) |
michael@0 | 76 | #define PIXREGION_SIZE(reg) ((reg)->data ? (reg)->data->size : 0) |
michael@0 | 77 | #define PIXREGION_RECTS(reg) \ |
michael@0 | 78 | ((reg)->data ? (box_type_t *)((reg)->data + 1) \ |
michael@0 | 79 | : &(reg)->extents) |
michael@0 | 80 | #define PIXREGION_BOXPTR(reg) ((box_type_t *)((reg)->data + 1)) |
michael@0 | 81 | #define PIXREGION_BOX(reg, i) (&PIXREGION_BOXPTR (reg)[i]) |
michael@0 | 82 | #define PIXREGION_TOP(reg) PIXREGION_BOX (reg, (reg)->data->numRects) |
michael@0 | 83 | #define PIXREGION_END(reg) PIXREGION_BOX (reg, (reg)->data->numRects - 1) |
michael@0 | 84 | |
michael@0 | 85 | #define GOOD_RECT(rect) ((rect)->x1 < (rect)->x2 && (rect)->y1 < (rect)->y2) |
michael@0 | 86 | #define BAD_RECT(rect) ((rect)->x1 > (rect)->x2 || (rect)->y1 > (rect)->y2) |
michael@0 | 87 | |
michael@0 | 88 | #ifdef DEBUG |
michael@0 | 89 | |
michael@0 | 90 | #define GOOD(reg) \ |
michael@0 | 91 | do \ |
michael@0 | 92 | { \ |
michael@0 | 93 | if (!PREFIX (_selfcheck (reg))) \ |
michael@0 | 94 | _pixman_log_error (FUNC, "Malformed region " # reg); \ |
michael@0 | 95 | } while (0) |
michael@0 | 96 | |
michael@0 | 97 | #else |
michael@0 | 98 | |
michael@0 | 99 | #define GOOD(reg) |
michael@0 | 100 | |
michael@0 | 101 | #endif |
michael@0 | 102 | |
michael@0 | 103 | static const box_type_t PREFIX (_empty_box_) = { 0, 0, 0, 0 }; |
michael@0 | 104 | static const region_data_type_t PREFIX (_empty_data_) = { 0, 0 }; |
michael@0 | 105 | #if defined (__llvm__) && !defined (__clang__) |
michael@0 | 106 | static const volatile region_data_type_t PREFIX (_broken_data_) = { 0, 0 }; |
michael@0 | 107 | #else |
michael@0 | 108 | static const region_data_type_t PREFIX (_broken_data_) = { 0, 0 }; |
michael@0 | 109 | #endif |
michael@0 | 110 | |
michael@0 | 111 | static box_type_t *pixman_region_empty_box = |
michael@0 | 112 | (box_type_t *)&PREFIX (_empty_box_); |
michael@0 | 113 | static region_data_type_t *pixman_region_empty_data = |
michael@0 | 114 | (region_data_type_t *)&PREFIX (_empty_data_); |
michael@0 | 115 | static region_data_type_t *pixman_broken_data = |
michael@0 | 116 | (region_data_type_t *)&PREFIX (_broken_data_); |
michael@0 | 117 | |
michael@0 | 118 | static pixman_bool_t |
michael@0 | 119 | pixman_break (region_type_t *region); |
michael@0 | 120 | |
michael@0 | 121 | /* |
michael@0 | 122 | * The functions in this file implement the Region abstraction used extensively |
michael@0 | 123 | * throughout the X11 sample server. A Region is simply a set of disjoint |
michael@0 | 124 | * (non-overlapping) rectangles, plus an "extent" rectangle which is the |
michael@0 | 125 | * smallest single rectangle that contains all the non-overlapping rectangles. |
michael@0 | 126 | * |
michael@0 | 127 | * A Region is implemented as a "y-x-banded" array of rectangles. This array |
michael@0 | 128 | * imposes two degrees of order. First, all rectangles are sorted by top side |
michael@0 | 129 | * y coordinate first (y1), and then by left side x coordinate (x1). |
michael@0 | 130 | * |
michael@0 | 131 | * Furthermore, the rectangles are grouped into "bands". Each rectangle in a |
michael@0 | 132 | * band has the same top y coordinate (y1), and each has the same bottom y |
michael@0 | 133 | * coordinate (y2). Thus all rectangles in a band differ only in their left |
michael@0 | 134 | * and right side (x1 and x2). Bands are implicit in the array of rectangles: |
michael@0 | 135 | * there is no separate list of band start pointers. |
michael@0 | 136 | * |
michael@0 | 137 | * The y-x band representation does not minimize rectangles. In particular, |
michael@0 | 138 | * if a rectangle vertically crosses a band (the rectangle has scanlines in |
michael@0 | 139 | * the y1 to y2 area spanned by the band), then the rectangle may be broken |
michael@0 | 140 | * down into two or more smaller rectangles stacked one atop the other. |
michael@0 | 141 | * |
michael@0 | 142 | * ----------- ----------- |
michael@0 | 143 | * | | | | band 0 |
michael@0 | 144 | * | | -------- ----------- -------- |
michael@0 | 145 | * | | | | in y-x banded | | | | band 1 |
michael@0 | 146 | * | | | | form is | | | | |
michael@0 | 147 | * ----------- | | ----------- -------- |
michael@0 | 148 | * | | | | band 2 |
michael@0 | 149 | * -------- -------- |
michael@0 | 150 | * |
michael@0 | 151 | * An added constraint on the rectangles is that they must cover as much |
michael@0 | 152 | * horizontal area as possible: no two rectangles within a band are allowed |
michael@0 | 153 | * to touch. |
michael@0 | 154 | * |
michael@0 | 155 | * Whenever possible, bands will be merged together to cover a greater vertical |
michael@0 | 156 | * distance (and thus reduce the number of rectangles). Two bands can be merged |
michael@0 | 157 | * only if the bottom of one touches the top of the other and they have |
michael@0 | 158 | * rectangles in the same places (of the same width, of course). |
michael@0 | 159 | * |
michael@0 | 160 | * Adam de Boor wrote most of the original region code. Joel McCormack |
michael@0 | 161 | * substantially modified or rewrote most of the core arithmetic routines, and |
michael@0 | 162 | * added pixman_region_validate in order to support several speed improvements |
michael@0 | 163 | * to pixman_region_validate_tree. Bob Scheifler changed the representation |
michael@0 | 164 | * to be more compact when empty or a single rectangle, and did a bunch of |
michael@0 | 165 | * gratuitous reformatting. Carl Worth did further gratuitous reformatting |
michael@0 | 166 | * while re-merging the server and client region code into libpixregion. |
michael@0 | 167 | * Soren Sandmann did even more gratuitous reformatting. |
michael@0 | 168 | */ |
michael@0 | 169 | |
michael@0 | 170 | /* true iff two Boxes overlap */ |
michael@0 | 171 | #define EXTENTCHECK(r1, r2) \ |
michael@0 | 172 | (!( ((r1)->x2 <= (r2)->x1) || \ |
michael@0 | 173 | ((r1)->x1 >= (r2)->x2) || \ |
michael@0 | 174 | ((r1)->y2 <= (r2)->y1) || \ |
michael@0 | 175 | ((r1)->y1 >= (r2)->y2) ) ) |
michael@0 | 176 | |
michael@0 | 177 | /* true iff (x,y) is in Box */ |
michael@0 | 178 | #define INBOX(r, x, y) \ |
michael@0 | 179 | ( ((r)->x2 > x) && \ |
michael@0 | 180 | ((r)->x1 <= x) && \ |
michael@0 | 181 | ((r)->y2 > y) && \ |
michael@0 | 182 | ((r)->y1 <= y) ) |
michael@0 | 183 | |
michael@0 | 184 | /* true iff Box r1 contains Box r2 */ |
michael@0 | 185 | #define SUBSUMES(r1, r2) \ |
michael@0 | 186 | ( ((r1)->x1 <= (r2)->x1) && \ |
michael@0 | 187 | ((r1)->x2 >= (r2)->x2) && \ |
michael@0 | 188 | ((r1)->y1 <= (r2)->y1) && \ |
michael@0 | 189 | ((r1)->y2 >= (r2)->y2) ) |
michael@0 | 190 | |
michael@0 | 191 | static size_t |
michael@0 | 192 | PIXREGION_SZOF (size_t n) |
michael@0 | 193 | { |
michael@0 | 194 | size_t size = n * sizeof(box_type_t); |
michael@0 | 195 | |
michael@0 | 196 | if (n > UINT32_MAX / sizeof(box_type_t)) |
michael@0 | 197 | return 0; |
michael@0 | 198 | |
michael@0 | 199 | if (sizeof(region_data_type_t) > UINT32_MAX - size) |
michael@0 | 200 | return 0; |
michael@0 | 201 | |
michael@0 | 202 | return size + sizeof(region_data_type_t); |
michael@0 | 203 | } |
michael@0 | 204 | |
michael@0 | 205 | static region_data_type_t * |
michael@0 | 206 | alloc_data (size_t n) |
michael@0 | 207 | { |
michael@0 | 208 | size_t sz = PIXREGION_SZOF (n); |
michael@0 | 209 | |
michael@0 | 210 | if (!sz) |
michael@0 | 211 | return NULL; |
michael@0 | 212 | |
michael@0 | 213 | return malloc (sz); |
michael@0 | 214 | } |
michael@0 | 215 | |
michael@0 | 216 | #define FREE_DATA(reg) if ((reg)->data && (reg)->data->size) free ((reg)->data) |
michael@0 | 217 | |
michael@0 | 218 | #define RECTALLOC_BAIL(region, n, bail) \ |
michael@0 | 219 | do \ |
michael@0 | 220 | { \ |
michael@0 | 221 | if (!(region)->data || \ |
michael@0 | 222 | (((region)->data->numRects + (n)) > (region)->data->size)) \ |
michael@0 | 223 | { \ |
michael@0 | 224 | if (!pixman_rect_alloc (region, n)) \ |
michael@0 | 225 | goto bail; \ |
michael@0 | 226 | } \ |
michael@0 | 227 | } while (0) |
michael@0 | 228 | |
michael@0 | 229 | #define RECTALLOC(region, n) \ |
michael@0 | 230 | do \ |
michael@0 | 231 | { \ |
michael@0 | 232 | if (!(region)->data || \ |
michael@0 | 233 | (((region)->data->numRects + (n)) > (region)->data->size)) \ |
michael@0 | 234 | { \ |
michael@0 | 235 | if (!pixman_rect_alloc (region, n)) { \ |
michael@0 | 236 | return FALSE; \ |
michael@0 | 237 | } \ |
michael@0 | 238 | } \ |
michael@0 | 239 | } while (0) |
michael@0 | 240 | |
michael@0 | 241 | #define ADDRECT(next_rect, nx1, ny1, nx2, ny2) \ |
michael@0 | 242 | do \ |
michael@0 | 243 | { \ |
michael@0 | 244 | next_rect->x1 = nx1; \ |
michael@0 | 245 | next_rect->y1 = ny1; \ |
michael@0 | 246 | next_rect->x2 = nx2; \ |
michael@0 | 247 | next_rect->y2 = ny2; \ |
michael@0 | 248 | next_rect++; \ |
michael@0 | 249 | } \ |
michael@0 | 250 | while (0) |
michael@0 | 251 | |
michael@0 | 252 | #define NEWRECT(region, next_rect, nx1, ny1, nx2, ny2) \ |
michael@0 | 253 | do \ |
michael@0 | 254 | { \ |
michael@0 | 255 | if (!(region)->data || \ |
michael@0 | 256 | ((region)->data->numRects == (region)->data->size)) \ |
michael@0 | 257 | { \ |
michael@0 | 258 | if (!pixman_rect_alloc (region, 1)) \ |
michael@0 | 259 | return FALSE; \ |
michael@0 | 260 | next_rect = PIXREGION_TOP (region); \ |
michael@0 | 261 | } \ |
michael@0 | 262 | ADDRECT (next_rect, nx1, ny1, nx2, ny2); \ |
michael@0 | 263 | region->data->numRects++; \ |
michael@0 | 264 | critical_if_fail (region->data->numRects <= region->data->size); \ |
michael@0 | 265 | } while (0) |
michael@0 | 266 | |
michael@0 | 267 | #define DOWNSIZE(reg, numRects) \ |
michael@0 | 268 | do \ |
michael@0 | 269 | { \ |
michael@0 | 270 | if (((numRects) < ((reg)->data->size >> 1)) && \ |
michael@0 | 271 | ((reg)->data->size > 50)) \ |
michael@0 | 272 | { \ |
michael@0 | 273 | region_data_type_t * new_data; \ |
michael@0 | 274 | size_t data_size = PIXREGION_SZOF (numRects); \ |
michael@0 | 275 | \ |
michael@0 | 276 | if (!data_size) \ |
michael@0 | 277 | { \ |
michael@0 | 278 | new_data = NULL; \ |
michael@0 | 279 | } \ |
michael@0 | 280 | else \ |
michael@0 | 281 | { \ |
michael@0 | 282 | new_data = (region_data_type_t *) \ |
michael@0 | 283 | realloc ((reg)->data, data_size); \ |
michael@0 | 284 | } \ |
michael@0 | 285 | \ |
michael@0 | 286 | if (new_data) \ |
michael@0 | 287 | { \ |
michael@0 | 288 | new_data->size = (numRects); \ |
michael@0 | 289 | (reg)->data = new_data; \ |
michael@0 | 290 | } \ |
michael@0 | 291 | } \ |
michael@0 | 292 | } while (0) |
michael@0 | 293 | |
michael@0 | 294 | PIXMAN_EXPORT pixman_bool_t |
michael@0 | 295 | PREFIX (_equal) (region_type_t *reg1, region_type_t *reg2) |
michael@0 | 296 | { |
michael@0 | 297 | int i; |
michael@0 | 298 | box_type_t *rects1; |
michael@0 | 299 | box_type_t *rects2; |
michael@0 | 300 | |
michael@0 | 301 | if (reg1->extents.x1 != reg2->extents.x1) |
michael@0 | 302 | return FALSE; |
michael@0 | 303 | |
michael@0 | 304 | if (reg1->extents.x2 != reg2->extents.x2) |
michael@0 | 305 | return FALSE; |
michael@0 | 306 | |
michael@0 | 307 | if (reg1->extents.y1 != reg2->extents.y1) |
michael@0 | 308 | return FALSE; |
michael@0 | 309 | |
michael@0 | 310 | if (reg1->extents.y2 != reg2->extents.y2) |
michael@0 | 311 | return FALSE; |
michael@0 | 312 | |
michael@0 | 313 | if (PIXREGION_NUMRECTS (reg1) != PIXREGION_NUMRECTS (reg2)) |
michael@0 | 314 | return FALSE; |
michael@0 | 315 | |
michael@0 | 316 | rects1 = PIXREGION_RECTS (reg1); |
michael@0 | 317 | rects2 = PIXREGION_RECTS (reg2); |
michael@0 | 318 | |
michael@0 | 319 | for (i = 0; i != PIXREGION_NUMRECTS (reg1); i++) |
michael@0 | 320 | { |
michael@0 | 321 | if (rects1[i].x1 != rects2[i].x1) |
michael@0 | 322 | return FALSE; |
michael@0 | 323 | |
michael@0 | 324 | if (rects1[i].x2 != rects2[i].x2) |
michael@0 | 325 | return FALSE; |
michael@0 | 326 | |
michael@0 | 327 | if (rects1[i].y1 != rects2[i].y1) |
michael@0 | 328 | return FALSE; |
michael@0 | 329 | |
michael@0 | 330 | if (rects1[i].y2 != rects2[i].y2) |
michael@0 | 331 | return FALSE; |
michael@0 | 332 | } |
michael@0 | 333 | |
michael@0 | 334 | return TRUE; |
michael@0 | 335 | } |
michael@0 | 336 | |
michael@0 | 337 | int |
michael@0 | 338 | PREFIX (_print) (region_type_t *rgn) |
michael@0 | 339 | { |
michael@0 | 340 | int num, size; |
michael@0 | 341 | int i; |
michael@0 | 342 | box_type_t * rects; |
michael@0 | 343 | |
michael@0 | 344 | num = PIXREGION_NUMRECTS (rgn); |
michael@0 | 345 | size = PIXREGION_SIZE (rgn); |
michael@0 | 346 | rects = PIXREGION_RECTS (rgn); |
michael@0 | 347 | |
michael@0 | 348 | fprintf (stderr, "num: %d size: %d\n", num, size); |
michael@0 | 349 | fprintf (stderr, "extents: %d %d %d %d\n", |
michael@0 | 350 | rgn->extents.x1, |
michael@0 | 351 | rgn->extents.y1, |
michael@0 | 352 | rgn->extents.x2, |
michael@0 | 353 | rgn->extents.y2); |
michael@0 | 354 | |
michael@0 | 355 | for (i = 0; i < num; i++) |
michael@0 | 356 | { |
michael@0 | 357 | fprintf (stderr, "%d %d %d %d \n", |
michael@0 | 358 | rects[i].x1, rects[i].y1, rects[i].x2, rects[i].y2); |
michael@0 | 359 | } |
michael@0 | 360 | |
michael@0 | 361 | fprintf (stderr, "\n"); |
michael@0 | 362 | |
michael@0 | 363 | return(num); |
michael@0 | 364 | } |
michael@0 | 365 | |
michael@0 | 366 | |
michael@0 | 367 | PIXMAN_EXPORT void |
michael@0 | 368 | PREFIX (_init) (region_type_t *region) |
michael@0 | 369 | { |
michael@0 | 370 | region->extents = *pixman_region_empty_box; |
michael@0 | 371 | region->data = pixman_region_empty_data; |
michael@0 | 372 | } |
michael@0 | 373 | |
michael@0 | 374 | PIXMAN_EXPORT void |
michael@0 | 375 | PREFIX (_init_rect) (region_type_t * region, |
michael@0 | 376 | int x, |
michael@0 | 377 | int y, |
michael@0 | 378 | unsigned int width, |
michael@0 | 379 | unsigned int height) |
michael@0 | 380 | { |
michael@0 | 381 | region->extents.x1 = x; |
michael@0 | 382 | region->extents.y1 = y; |
michael@0 | 383 | region->extents.x2 = x + width; |
michael@0 | 384 | region->extents.y2 = y + height; |
michael@0 | 385 | |
michael@0 | 386 | if (!GOOD_RECT (®ion->extents)) |
michael@0 | 387 | { |
michael@0 | 388 | if (BAD_RECT (®ion->extents)) |
michael@0 | 389 | _pixman_log_error (FUNC, "Invalid rectangle passed"); |
michael@0 | 390 | PREFIX (_init) (region); |
michael@0 | 391 | return; |
michael@0 | 392 | } |
michael@0 | 393 | |
michael@0 | 394 | region->data = NULL; |
michael@0 | 395 | } |
michael@0 | 396 | |
michael@0 | 397 | PIXMAN_EXPORT void |
michael@0 | 398 | PREFIX (_init_with_extents) (region_type_t *region, box_type_t *extents) |
michael@0 | 399 | { |
michael@0 | 400 | if (!GOOD_RECT (extents)) |
michael@0 | 401 | { |
michael@0 | 402 | if (BAD_RECT (extents)) |
michael@0 | 403 | _pixman_log_error (FUNC, "Invalid rectangle passed"); |
michael@0 | 404 | PREFIX (_init) (region); |
michael@0 | 405 | return; |
michael@0 | 406 | } |
michael@0 | 407 | region->extents = *extents; |
michael@0 | 408 | |
michael@0 | 409 | region->data = NULL; |
michael@0 | 410 | } |
michael@0 | 411 | |
michael@0 | 412 | PIXMAN_EXPORT void |
michael@0 | 413 | PREFIX (_fini) (region_type_t *region) |
michael@0 | 414 | { |
michael@0 | 415 | GOOD (region); |
michael@0 | 416 | FREE_DATA (region); |
michael@0 | 417 | } |
michael@0 | 418 | |
michael@0 | 419 | PIXMAN_EXPORT int |
michael@0 | 420 | PREFIX (_n_rects) (region_type_t *region) |
michael@0 | 421 | { |
michael@0 | 422 | return PIXREGION_NUMRECTS (region); |
michael@0 | 423 | } |
michael@0 | 424 | |
michael@0 | 425 | PIXMAN_EXPORT box_type_t * |
michael@0 | 426 | PREFIX (_rectangles) (region_type_t *region, |
michael@0 | 427 | int *n_rects) |
michael@0 | 428 | { |
michael@0 | 429 | if (n_rects) |
michael@0 | 430 | *n_rects = PIXREGION_NUMRECTS (region); |
michael@0 | 431 | |
michael@0 | 432 | return PIXREGION_RECTS (region); |
michael@0 | 433 | } |
michael@0 | 434 | |
michael@0 | 435 | static pixman_bool_t |
michael@0 | 436 | pixman_break (region_type_t *region) |
michael@0 | 437 | { |
michael@0 | 438 | FREE_DATA (region); |
michael@0 | 439 | |
michael@0 | 440 | region->extents = *pixman_region_empty_box; |
michael@0 | 441 | region->data = pixman_broken_data; |
michael@0 | 442 | |
michael@0 | 443 | return FALSE; |
michael@0 | 444 | } |
michael@0 | 445 | |
michael@0 | 446 | static pixman_bool_t |
michael@0 | 447 | pixman_rect_alloc (region_type_t * region, |
michael@0 | 448 | int n) |
michael@0 | 449 | { |
michael@0 | 450 | region_data_type_t *data; |
michael@0 | 451 | |
michael@0 | 452 | if (!region->data) |
michael@0 | 453 | { |
michael@0 | 454 | n++; |
michael@0 | 455 | region->data = alloc_data (n); |
michael@0 | 456 | |
michael@0 | 457 | if (!region->data) |
michael@0 | 458 | return pixman_break (region); |
michael@0 | 459 | |
michael@0 | 460 | region->data->numRects = 1; |
michael@0 | 461 | *PIXREGION_BOXPTR (region) = region->extents; |
michael@0 | 462 | } |
michael@0 | 463 | else if (!region->data->size) |
michael@0 | 464 | { |
michael@0 | 465 | region->data = alloc_data (n); |
michael@0 | 466 | |
michael@0 | 467 | if (!region->data) |
michael@0 | 468 | return pixman_break (region); |
michael@0 | 469 | |
michael@0 | 470 | region->data->numRects = 0; |
michael@0 | 471 | } |
michael@0 | 472 | else |
michael@0 | 473 | { |
michael@0 | 474 | size_t data_size; |
michael@0 | 475 | |
michael@0 | 476 | if (n == 1) |
michael@0 | 477 | { |
michael@0 | 478 | n = region->data->numRects; |
michael@0 | 479 | if (n > 500) /* XXX pick numbers out of a hat */ |
michael@0 | 480 | n = 250; |
michael@0 | 481 | } |
michael@0 | 482 | |
michael@0 | 483 | n += region->data->numRects; |
michael@0 | 484 | data_size = PIXREGION_SZOF (n); |
michael@0 | 485 | |
michael@0 | 486 | if (!data_size) |
michael@0 | 487 | { |
michael@0 | 488 | data = NULL; |
michael@0 | 489 | } |
michael@0 | 490 | else |
michael@0 | 491 | { |
michael@0 | 492 | data = (region_data_type_t *) |
michael@0 | 493 | realloc (region->data, PIXREGION_SZOF (n)); |
michael@0 | 494 | } |
michael@0 | 495 | |
michael@0 | 496 | if (!data) |
michael@0 | 497 | return pixman_break (region); |
michael@0 | 498 | |
michael@0 | 499 | region->data = data; |
michael@0 | 500 | } |
michael@0 | 501 | |
michael@0 | 502 | region->data->size = n; |
michael@0 | 503 | |
michael@0 | 504 | return TRUE; |
michael@0 | 505 | } |
michael@0 | 506 | |
michael@0 | 507 | PIXMAN_EXPORT pixman_bool_t |
michael@0 | 508 | PREFIX (_copy) (region_type_t *dst, region_type_t *src) |
michael@0 | 509 | { |
michael@0 | 510 | GOOD (dst); |
michael@0 | 511 | GOOD (src); |
michael@0 | 512 | |
michael@0 | 513 | if (dst == src) |
michael@0 | 514 | return TRUE; |
michael@0 | 515 | |
michael@0 | 516 | dst->extents = src->extents; |
michael@0 | 517 | |
michael@0 | 518 | if (!src->data || !src->data->size) |
michael@0 | 519 | { |
michael@0 | 520 | FREE_DATA (dst); |
michael@0 | 521 | dst->data = src->data; |
michael@0 | 522 | return TRUE; |
michael@0 | 523 | } |
michael@0 | 524 | |
michael@0 | 525 | if (!dst->data || (dst->data->size < src->data->numRects)) |
michael@0 | 526 | { |
michael@0 | 527 | FREE_DATA (dst); |
michael@0 | 528 | |
michael@0 | 529 | dst->data = alloc_data (src->data->numRects); |
michael@0 | 530 | |
michael@0 | 531 | if (!dst->data) |
michael@0 | 532 | return pixman_break (dst); |
michael@0 | 533 | |
michael@0 | 534 | dst->data->size = src->data->numRects; |
michael@0 | 535 | } |
michael@0 | 536 | |
michael@0 | 537 | dst->data->numRects = src->data->numRects; |
michael@0 | 538 | |
michael@0 | 539 | memmove ((char *)PIXREGION_BOXPTR (dst), (char *)PIXREGION_BOXPTR (src), |
michael@0 | 540 | dst->data->numRects * sizeof(box_type_t)); |
michael@0 | 541 | |
michael@0 | 542 | return TRUE; |
michael@0 | 543 | } |
michael@0 | 544 | |
michael@0 | 545 | /*====================================================================== |
michael@0 | 546 | * Generic Region Operator |
michael@0 | 547 | *====================================================================*/ |
michael@0 | 548 | |
michael@0 | 549 | /*- |
michael@0 | 550 | *----------------------------------------------------------------------- |
michael@0 | 551 | * pixman_coalesce -- |
michael@0 | 552 | * Attempt to merge the boxes in the current band with those in the |
michael@0 | 553 | * previous one. We are guaranteed that the current band extends to |
michael@0 | 554 | * the end of the rects array. Used only by pixman_op. |
michael@0 | 555 | * |
michael@0 | 556 | * Results: |
michael@0 | 557 | * The new index for the previous band. |
michael@0 | 558 | * |
michael@0 | 559 | * Side Effects: |
michael@0 | 560 | * If coalescing takes place: |
michael@0 | 561 | * - rectangles in the previous band will have their y2 fields |
michael@0 | 562 | * altered. |
michael@0 | 563 | * - region->data->numRects will be decreased. |
michael@0 | 564 | * |
michael@0 | 565 | *----------------------------------------------------------------------- |
michael@0 | 566 | */ |
michael@0 | 567 | static inline int |
michael@0 | 568 | pixman_coalesce (region_type_t * region, /* Region to coalesce */ |
michael@0 | 569 | int prev_start, /* Index of start of previous band */ |
michael@0 | 570 | int cur_start) /* Index of start of current band */ |
michael@0 | 571 | { |
michael@0 | 572 | box_type_t *prev_box; /* Current box in previous band */ |
michael@0 | 573 | box_type_t *cur_box; /* Current box in current band */ |
michael@0 | 574 | int numRects; /* Number rectangles in both bands */ |
michael@0 | 575 | int y2; /* Bottom of current band */ |
michael@0 | 576 | |
michael@0 | 577 | /* |
michael@0 | 578 | * Figure out how many rectangles are in the band. |
michael@0 | 579 | */ |
michael@0 | 580 | numRects = cur_start - prev_start; |
michael@0 | 581 | critical_if_fail (numRects == region->data->numRects - cur_start); |
michael@0 | 582 | |
michael@0 | 583 | if (!numRects) return cur_start; |
michael@0 | 584 | |
michael@0 | 585 | /* |
michael@0 | 586 | * The bands may only be coalesced if the bottom of the previous |
michael@0 | 587 | * matches the top scanline of the current. |
michael@0 | 588 | */ |
michael@0 | 589 | prev_box = PIXREGION_BOX (region, prev_start); |
michael@0 | 590 | cur_box = PIXREGION_BOX (region, cur_start); |
michael@0 | 591 | if (prev_box->y2 != cur_box->y1) return cur_start; |
michael@0 | 592 | |
michael@0 | 593 | /* |
michael@0 | 594 | * Make sure the bands have boxes in the same places. This |
michael@0 | 595 | * assumes that boxes have been added in such a way that they |
michael@0 | 596 | * cover the most area possible. I.e. two boxes in a band must |
michael@0 | 597 | * have some horizontal space between them. |
michael@0 | 598 | */ |
michael@0 | 599 | y2 = cur_box->y2; |
michael@0 | 600 | |
michael@0 | 601 | do |
michael@0 | 602 | { |
michael@0 | 603 | if ((prev_box->x1 != cur_box->x1) || (prev_box->x2 != cur_box->x2)) |
michael@0 | 604 | return (cur_start); |
michael@0 | 605 | |
michael@0 | 606 | prev_box++; |
michael@0 | 607 | cur_box++; |
michael@0 | 608 | numRects--; |
michael@0 | 609 | } |
michael@0 | 610 | while (numRects); |
michael@0 | 611 | |
michael@0 | 612 | /* |
michael@0 | 613 | * The bands may be merged, so set the bottom y of each box |
michael@0 | 614 | * in the previous band to the bottom y of the current band. |
michael@0 | 615 | */ |
michael@0 | 616 | numRects = cur_start - prev_start; |
michael@0 | 617 | region->data->numRects -= numRects; |
michael@0 | 618 | |
michael@0 | 619 | do |
michael@0 | 620 | { |
michael@0 | 621 | prev_box--; |
michael@0 | 622 | prev_box->y2 = y2; |
michael@0 | 623 | numRects--; |
michael@0 | 624 | } |
michael@0 | 625 | while (numRects); |
michael@0 | 626 | |
michael@0 | 627 | return prev_start; |
michael@0 | 628 | } |
michael@0 | 629 | |
michael@0 | 630 | /* Quicky macro to avoid trivial reject procedure calls to pixman_coalesce */ |
michael@0 | 631 | |
michael@0 | 632 | #define COALESCE(new_reg, prev_band, cur_band) \ |
michael@0 | 633 | do \ |
michael@0 | 634 | { \ |
michael@0 | 635 | if (cur_band - prev_band == new_reg->data->numRects - cur_band) \ |
michael@0 | 636 | prev_band = pixman_coalesce (new_reg, prev_band, cur_band); \ |
michael@0 | 637 | else \ |
michael@0 | 638 | prev_band = cur_band; \ |
michael@0 | 639 | } while (0) |
michael@0 | 640 | |
michael@0 | 641 | /*- |
michael@0 | 642 | *----------------------------------------------------------------------- |
michael@0 | 643 | * pixman_region_append_non_o -- |
michael@0 | 644 | * Handle a non-overlapping band for the union and subtract operations. |
michael@0 | 645 | * Just adds the (top/bottom-clipped) rectangles into the region. |
michael@0 | 646 | * Doesn't have to check for subsumption or anything. |
michael@0 | 647 | * |
michael@0 | 648 | * Results: |
michael@0 | 649 | * None. |
michael@0 | 650 | * |
michael@0 | 651 | * Side Effects: |
michael@0 | 652 | * region->data->numRects is incremented and the rectangles overwritten |
michael@0 | 653 | * with the rectangles we're passed. |
michael@0 | 654 | * |
michael@0 | 655 | *----------------------------------------------------------------------- |
michael@0 | 656 | */ |
michael@0 | 657 | static inline pixman_bool_t |
michael@0 | 658 | pixman_region_append_non_o (region_type_t * region, |
michael@0 | 659 | box_type_t * r, |
michael@0 | 660 | box_type_t * r_end, |
michael@0 | 661 | int y1, |
michael@0 | 662 | int y2) |
michael@0 | 663 | { |
michael@0 | 664 | box_type_t *next_rect; |
michael@0 | 665 | int new_rects; |
michael@0 | 666 | |
michael@0 | 667 | new_rects = r_end - r; |
michael@0 | 668 | |
michael@0 | 669 | critical_if_fail (y1 < y2); |
michael@0 | 670 | critical_if_fail (new_rects != 0); |
michael@0 | 671 | |
michael@0 | 672 | /* Make sure we have enough space for all rectangles to be added */ |
michael@0 | 673 | RECTALLOC (region, new_rects); |
michael@0 | 674 | next_rect = PIXREGION_TOP (region); |
michael@0 | 675 | region->data->numRects += new_rects; |
michael@0 | 676 | |
michael@0 | 677 | do |
michael@0 | 678 | { |
michael@0 | 679 | critical_if_fail (r->x1 < r->x2); |
michael@0 | 680 | ADDRECT (next_rect, r->x1, y1, r->x2, y2); |
michael@0 | 681 | r++; |
michael@0 | 682 | } |
michael@0 | 683 | while (r != r_end); |
michael@0 | 684 | |
michael@0 | 685 | return TRUE; |
michael@0 | 686 | } |
michael@0 | 687 | |
michael@0 | 688 | #define FIND_BAND(r, r_band_end, r_end, ry1) \ |
michael@0 | 689 | do \ |
michael@0 | 690 | { \ |
michael@0 | 691 | ry1 = r->y1; \ |
michael@0 | 692 | r_band_end = r + 1; \ |
michael@0 | 693 | while ((r_band_end != r_end) && (r_band_end->y1 == ry1)) { \ |
michael@0 | 694 | r_band_end++; \ |
michael@0 | 695 | } \ |
michael@0 | 696 | } while (0) |
michael@0 | 697 | |
michael@0 | 698 | #define APPEND_REGIONS(new_reg, r, r_end) \ |
michael@0 | 699 | do \ |
michael@0 | 700 | { \ |
michael@0 | 701 | int new_rects; \ |
michael@0 | 702 | if ((new_rects = r_end - r)) { \ |
michael@0 | 703 | RECTALLOC_BAIL (new_reg, new_rects, bail); \ |
michael@0 | 704 | memmove ((char *)PIXREGION_TOP (new_reg), (char *)r, \ |
michael@0 | 705 | new_rects * sizeof(box_type_t)); \ |
michael@0 | 706 | new_reg->data->numRects += new_rects; \ |
michael@0 | 707 | } \ |
michael@0 | 708 | } while (0) |
michael@0 | 709 | |
michael@0 | 710 | /*- |
michael@0 | 711 | *----------------------------------------------------------------------- |
michael@0 | 712 | * pixman_op -- |
michael@0 | 713 | * Apply an operation to two regions. Called by pixman_region_union, pixman_region_inverse, |
michael@0 | 714 | * pixman_region_subtract, pixman_region_intersect.... Both regions MUST have at least one |
michael@0 | 715 | * rectangle, and cannot be the same object. |
michael@0 | 716 | * |
michael@0 | 717 | * Results: |
michael@0 | 718 | * TRUE if successful. |
michael@0 | 719 | * |
michael@0 | 720 | * Side Effects: |
michael@0 | 721 | * The new region is overwritten. |
michael@0 | 722 | * overlap set to TRUE if overlap_func ever returns TRUE. |
michael@0 | 723 | * |
michael@0 | 724 | * Notes: |
michael@0 | 725 | * The idea behind this function is to view the two regions as sets. |
michael@0 | 726 | * Together they cover a rectangle of area that this function divides |
michael@0 | 727 | * into horizontal bands where points are covered only by one region |
michael@0 | 728 | * or by both. For the first case, the non_overlap_func is called with |
michael@0 | 729 | * each the band and the band's upper and lower extents. For the |
michael@0 | 730 | * second, the overlap_func is called to process the entire band. It |
michael@0 | 731 | * is responsible for clipping the rectangles in the band, though |
michael@0 | 732 | * this function provides the boundaries. |
michael@0 | 733 | * At the end of each band, the new region is coalesced, if possible, |
michael@0 | 734 | * to reduce the number of rectangles in the region. |
michael@0 | 735 | * |
michael@0 | 736 | *----------------------------------------------------------------------- |
michael@0 | 737 | */ |
michael@0 | 738 | |
michael@0 | 739 | typedef pixman_bool_t (*overlap_proc_ptr) (region_type_t *region, |
michael@0 | 740 | box_type_t * r1, |
michael@0 | 741 | box_type_t * r1_end, |
michael@0 | 742 | box_type_t * r2, |
michael@0 | 743 | box_type_t * r2_end, |
michael@0 | 744 | int y1, |
michael@0 | 745 | int y2); |
michael@0 | 746 | |
michael@0 | 747 | static pixman_bool_t |
michael@0 | 748 | pixman_op (region_type_t * new_reg, /* Place to store result */ |
michael@0 | 749 | region_type_t * reg1, /* First region in operation */ |
michael@0 | 750 | region_type_t * reg2, /* 2d region in operation */ |
michael@0 | 751 | overlap_proc_ptr overlap_func, /* Function to call for over- |
michael@0 | 752 | * lapping bands */ |
michael@0 | 753 | int append_non1, /* Append non-overlapping bands |
michael@0 | 754 | * in region 1 ? |
michael@0 | 755 | */ |
michael@0 | 756 | int append_non2 /* Append non-overlapping bands |
michael@0 | 757 | * in region 2 ? |
michael@0 | 758 | */ |
michael@0 | 759 | ) |
michael@0 | 760 | { |
michael@0 | 761 | box_type_t *r1; /* Pointer into first region */ |
michael@0 | 762 | box_type_t *r2; /* Pointer into 2d region */ |
michael@0 | 763 | box_type_t *r1_end; /* End of 1st region */ |
michael@0 | 764 | box_type_t *r2_end; /* End of 2d region */ |
michael@0 | 765 | int ybot; /* Bottom of intersection */ |
michael@0 | 766 | int ytop; /* Top of intersection */ |
michael@0 | 767 | region_data_type_t *old_data; /* Old data for new_reg */ |
michael@0 | 768 | int prev_band; /* Index of start of |
michael@0 | 769 | * previous band in new_reg */ |
michael@0 | 770 | int cur_band; /* Index of start of current |
michael@0 | 771 | * band in new_reg */ |
michael@0 | 772 | box_type_t * r1_band_end; /* End of current band in r1 */ |
michael@0 | 773 | box_type_t * r2_band_end; /* End of current band in r2 */ |
michael@0 | 774 | int top; /* Top of non-overlapping band */ |
michael@0 | 775 | int bot; /* Bottom of non-overlapping band*/ |
michael@0 | 776 | int r1y1; /* Temps for r1->y1 and r2->y1 */ |
michael@0 | 777 | int r2y1; |
michael@0 | 778 | int new_size; |
michael@0 | 779 | int numRects; |
michael@0 | 780 | |
michael@0 | 781 | /* |
michael@0 | 782 | * Break any region computed from a broken region |
michael@0 | 783 | */ |
michael@0 | 784 | if (PIXREGION_NAR (reg1) || PIXREGION_NAR (reg2)) |
michael@0 | 785 | return pixman_break (new_reg); |
michael@0 | 786 | |
michael@0 | 787 | /* |
michael@0 | 788 | * Initialization: |
michael@0 | 789 | * set r1, r2, r1_end and r2_end appropriately, save the rectangles |
michael@0 | 790 | * of the destination region until the end in case it's one of |
michael@0 | 791 | * the two source regions, then mark the "new" region empty, allocating |
michael@0 | 792 | * another array of rectangles for it to use. |
michael@0 | 793 | */ |
michael@0 | 794 | |
michael@0 | 795 | r1 = PIXREGION_RECTS (reg1); |
michael@0 | 796 | new_size = PIXREGION_NUMRECTS (reg1); |
michael@0 | 797 | r1_end = r1 + new_size; |
michael@0 | 798 | |
michael@0 | 799 | numRects = PIXREGION_NUMRECTS (reg2); |
michael@0 | 800 | r2 = PIXREGION_RECTS (reg2); |
michael@0 | 801 | r2_end = r2 + numRects; |
michael@0 | 802 | |
michael@0 | 803 | critical_if_fail (r1 != r1_end); |
michael@0 | 804 | critical_if_fail (r2 != r2_end); |
michael@0 | 805 | |
michael@0 | 806 | old_data = (region_data_type_t *)NULL; |
michael@0 | 807 | |
michael@0 | 808 | if (((new_reg == reg1) && (new_size > 1)) || |
michael@0 | 809 | ((new_reg == reg2) && (numRects > 1))) |
michael@0 | 810 | { |
michael@0 | 811 | old_data = new_reg->data; |
michael@0 | 812 | new_reg->data = pixman_region_empty_data; |
michael@0 | 813 | } |
michael@0 | 814 | |
michael@0 | 815 | /* guess at new size */ |
michael@0 | 816 | if (numRects > new_size) |
michael@0 | 817 | new_size = numRects; |
michael@0 | 818 | |
michael@0 | 819 | new_size <<= 1; |
michael@0 | 820 | |
michael@0 | 821 | if (!new_reg->data) |
michael@0 | 822 | new_reg->data = pixman_region_empty_data; |
michael@0 | 823 | else if (new_reg->data->size) |
michael@0 | 824 | new_reg->data->numRects = 0; |
michael@0 | 825 | |
michael@0 | 826 | if (new_size > new_reg->data->size) |
michael@0 | 827 | { |
michael@0 | 828 | if (!pixman_rect_alloc (new_reg, new_size)) |
michael@0 | 829 | { |
michael@0 | 830 | free (old_data); |
michael@0 | 831 | return FALSE; |
michael@0 | 832 | } |
michael@0 | 833 | } |
michael@0 | 834 | |
michael@0 | 835 | /* |
michael@0 | 836 | * Initialize ybot. |
michael@0 | 837 | * In the upcoming loop, ybot and ytop serve different functions depending |
michael@0 | 838 | * on whether the band being handled is an overlapping or non-overlapping |
michael@0 | 839 | * band. |
michael@0 | 840 | * In the case of a non-overlapping band (only one of the regions |
michael@0 | 841 | * has points in the band), ybot is the bottom of the most recent |
michael@0 | 842 | * intersection and thus clips the top of the rectangles in that band. |
michael@0 | 843 | * ytop is the top of the next intersection between the two regions and |
michael@0 | 844 | * serves to clip the bottom of the rectangles in the current band. |
michael@0 | 845 | * For an overlapping band (where the two regions intersect), ytop clips |
michael@0 | 846 | * the top of the rectangles of both regions and ybot clips the bottoms. |
michael@0 | 847 | */ |
michael@0 | 848 | |
michael@0 | 849 | ybot = MIN (r1->y1, r2->y1); |
michael@0 | 850 | |
michael@0 | 851 | /* |
michael@0 | 852 | * prev_band serves to mark the start of the previous band so rectangles |
michael@0 | 853 | * can be coalesced into larger rectangles. qv. pixman_coalesce, above. |
michael@0 | 854 | * In the beginning, there is no previous band, so prev_band == cur_band |
michael@0 | 855 | * (cur_band is set later on, of course, but the first band will always |
michael@0 | 856 | * start at index 0). prev_band and cur_band must be indices because of |
michael@0 | 857 | * the possible expansion, and resultant moving, of the new region's |
michael@0 | 858 | * array of rectangles. |
michael@0 | 859 | */ |
michael@0 | 860 | prev_band = 0; |
michael@0 | 861 | |
michael@0 | 862 | do |
michael@0 | 863 | { |
michael@0 | 864 | /* |
michael@0 | 865 | * This algorithm proceeds one source-band (as opposed to a |
michael@0 | 866 | * destination band, which is determined by where the two regions |
michael@0 | 867 | * intersect) at a time. r1_band_end and r2_band_end serve to mark the |
michael@0 | 868 | * rectangle after the last one in the current band for their |
michael@0 | 869 | * respective regions. |
michael@0 | 870 | */ |
michael@0 | 871 | critical_if_fail (r1 != r1_end); |
michael@0 | 872 | critical_if_fail (r2 != r2_end); |
michael@0 | 873 | |
michael@0 | 874 | FIND_BAND (r1, r1_band_end, r1_end, r1y1); |
michael@0 | 875 | FIND_BAND (r2, r2_band_end, r2_end, r2y1); |
michael@0 | 876 | |
michael@0 | 877 | /* |
michael@0 | 878 | * First handle the band that doesn't intersect, if any. |
michael@0 | 879 | * |
michael@0 | 880 | * Note that attention is restricted to one band in the |
michael@0 | 881 | * non-intersecting region at once, so if a region has n |
michael@0 | 882 | * bands between the current position and the next place it overlaps |
michael@0 | 883 | * the other, this entire loop will be passed through n times. |
michael@0 | 884 | */ |
michael@0 | 885 | if (r1y1 < r2y1) |
michael@0 | 886 | { |
michael@0 | 887 | if (append_non1) |
michael@0 | 888 | { |
michael@0 | 889 | top = MAX (r1y1, ybot); |
michael@0 | 890 | bot = MIN (r1->y2, r2y1); |
michael@0 | 891 | if (top != bot) |
michael@0 | 892 | { |
michael@0 | 893 | cur_band = new_reg->data->numRects; |
michael@0 | 894 | if (!pixman_region_append_non_o (new_reg, r1, r1_band_end, top, bot)) |
michael@0 | 895 | goto bail; |
michael@0 | 896 | COALESCE (new_reg, prev_band, cur_band); |
michael@0 | 897 | } |
michael@0 | 898 | } |
michael@0 | 899 | ytop = r2y1; |
michael@0 | 900 | } |
michael@0 | 901 | else if (r2y1 < r1y1) |
michael@0 | 902 | { |
michael@0 | 903 | if (append_non2) |
michael@0 | 904 | { |
michael@0 | 905 | top = MAX (r2y1, ybot); |
michael@0 | 906 | bot = MIN (r2->y2, r1y1); |
michael@0 | 907 | |
michael@0 | 908 | if (top != bot) |
michael@0 | 909 | { |
michael@0 | 910 | cur_band = new_reg->data->numRects; |
michael@0 | 911 | |
michael@0 | 912 | if (!pixman_region_append_non_o (new_reg, r2, r2_band_end, top, bot)) |
michael@0 | 913 | goto bail; |
michael@0 | 914 | |
michael@0 | 915 | COALESCE (new_reg, prev_band, cur_band); |
michael@0 | 916 | } |
michael@0 | 917 | } |
michael@0 | 918 | ytop = r1y1; |
michael@0 | 919 | } |
michael@0 | 920 | else |
michael@0 | 921 | { |
michael@0 | 922 | ytop = r1y1; |
michael@0 | 923 | } |
michael@0 | 924 | |
michael@0 | 925 | /* |
michael@0 | 926 | * Now see if we've hit an intersecting band. The two bands only |
michael@0 | 927 | * intersect if ybot > ytop |
michael@0 | 928 | */ |
michael@0 | 929 | ybot = MIN (r1->y2, r2->y2); |
michael@0 | 930 | if (ybot > ytop) |
michael@0 | 931 | { |
michael@0 | 932 | cur_band = new_reg->data->numRects; |
michael@0 | 933 | |
michael@0 | 934 | if (!(*overlap_func)(new_reg, |
michael@0 | 935 | r1, r1_band_end, |
michael@0 | 936 | r2, r2_band_end, |
michael@0 | 937 | ytop, ybot)) |
michael@0 | 938 | { |
michael@0 | 939 | goto bail; |
michael@0 | 940 | } |
michael@0 | 941 | |
michael@0 | 942 | COALESCE (new_reg, prev_band, cur_band); |
michael@0 | 943 | } |
michael@0 | 944 | |
michael@0 | 945 | /* |
michael@0 | 946 | * If we've finished with a band (y2 == ybot) we skip forward |
michael@0 | 947 | * in the region to the next band. |
michael@0 | 948 | */ |
michael@0 | 949 | if (r1->y2 == ybot) |
michael@0 | 950 | r1 = r1_band_end; |
michael@0 | 951 | |
michael@0 | 952 | if (r2->y2 == ybot) |
michael@0 | 953 | r2 = r2_band_end; |
michael@0 | 954 | |
michael@0 | 955 | } |
michael@0 | 956 | while (r1 != r1_end && r2 != r2_end); |
michael@0 | 957 | |
michael@0 | 958 | /* |
michael@0 | 959 | * Deal with whichever region (if any) still has rectangles left. |
michael@0 | 960 | * |
michael@0 | 961 | * We only need to worry about banding and coalescing for the very first |
michael@0 | 962 | * band left. After that, we can just group all remaining boxes, |
michael@0 | 963 | * regardless of how many bands, into one final append to the list. |
michael@0 | 964 | */ |
michael@0 | 965 | |
michael@0 | 966 | if ((r1 != r1_end) && append_non1) |
michael@0 | 967 | { |
michael@0 | 968 | /* Do first non_overlap1Func call, which may be able to coalesce */ |
michael@0 | 969 | FIND_BAND (r1, r1_band_end, r1_end, r1y1); |
michael@0 | 970 | |
michael@0 | 971 | cur_band = new_reg->data->numRects; |
michael@0 | 972 | |
michael@0 | 973 | if (!pixman_region_append_non_o (new_reg, |
michael@0 | 974 | r1, r1_band_end, |
michael@0 | 975 | MAX (r1y1, ybot), r1->y2)) |
michael@0 | 976 | { |
michael@0 | 977 | goto bail; |
michael@0 | 978 | } |
michael@0 | 979 | |
michael@0 | 980 | COALESCE (new_reg, prev_band, cur_band); |
michael@0 | 981 | |
michael@0 | 982 | /* Just append the rest of the boxes */ |
michael@0 | 983 | APPEND_REGIONS (new_reg, r1_band_end, r1_end); |
michael@0 | 984 | } |
michael@0 | 985 | else if ((r2 != r2_end) && append_non2) |
michael@0 | 986 | { |
michael@0 | 987 | /* Do first non_overlap2Func call, which may be able to coalesce */ |
michael@0 | 988 | FIND_BAND (r2, r2_band_end, r2_end, r2y1); |
michael@0 | 989 | |
michael@0 | 990 | cur_band = new_reg->data->numRects; |
michael@0 | 991 | |
michael@0 | 992 | if (!pixman_region_append_non_o (new_reg, |
michael@0 | 993 | r2, r2_band_end, |
michael@0 | 994 | MAX (r2y1, ybot), r2->y2)) |
michael@0 | 995 | { |
michael@0 | 996 | goto bail; |
michael@0 | 997 | } |
michael@0 | 998 | |
michael@0 | 999 | COALESCE (new_reg, prev_band, cur_band); |
michael@0 | 1000 | |
michael@0 | 1001 | /* Append rest of boxes */ |
michael@0 | 1002 | APPEND_REGIONS (new_reg, r2_band_end, r2_end); |
michael@0 | 1003 | } |
michael@0 | 1004 | |
michael@0 | 1005 | free (old_data); |
michael@0 | 1006 | |
michael@0 | 1007 | if (!(numRects = new_reg->data->numRects)) |
michael@0 | 1008 | { |
michael@0 | 1009 | FREE_DATA (new_reg); |
michael@0 | 1010 | new_reg->data = pixman_region_empty_data; |
michael@0 | 1011 | } |
michael@0 | 1012 | else if (numRects == 1) |
michael@0 | 1013 | { |
michael@0 | 1014 | new_reg->extents = *PIXREGION_BOXPTR (new_reg); |
michael@0 | 1015 | FREE_DATA (new_reg); |
michael@0 | 1016 | new_reg->data = (region_data_type_t *)NULL; |
michael@0 | 1017 | } |
michael@0 | 1018 | else |
michael@0 | 1019 | { |
michael@0 | 1020 | DOWNSIZE (new_reg, numRects); |
michael@0 | 1021 | } |
michael@0 | 1022 | |
michael@0 | 1023 | return TRUE; |
michael@0 | 1024 | |
michael@0 | 1025 | bail: |
michael@0 | 1026 | free (old_data); |
michael@0 | 1027 | |
michael@0 | 1028 | return pixman_break (new_reg); |
michael@0 | 1029 | } |
michael@0 | 1030 | |
michael@0 | 1031 | /*- |
michael@0 | 1032 | *----------------------------------------------------------------------- |
michael@0 | 1033 | * pixman_set_extents -- |
michael@0 | 1034 | * Reset the extents of a region to what they should be. Called by |
michael@0 | 1035 | * pixman_region_subtract and pixman_region_intersect as they can't |
michael@0 | 1036 | * figure it out along the way or do so easily, as pixman_region_union can. |
michael@0 | 1037 | * |
michael@0 | 1038 | * Results: |
michael@0 | 1039 | * None. |
michael@0 | 1040 | * |
michael@0 | 1041 | * Side Effects: |
michael@0 | 1042 | * The region's 'extents' structure is overwritten. |
michael@0 | 1043 | * |
michael@0 | 1044 | *----------------------------------------------------------------------- |
michael@0 | 1045 | */ |
michael@0 | 1046 | static void |
michael@0 | 1047 | pixman_set_extents (region_type_t *region) |
michael@0 | 1048 | { |
michael@0 | 1049 | box_type_t *box, *box_end; |
michael@0 | 1050 | |
michael@0 | 1051 | if (!region->data) |
michael@0 | 1052 | return; |
michael@0 | 1053 | |
michael@0 | 1054 | if (!region->data->size) |
michael@0 | 1055 | { |
michael@0 | 1056 | region->extents.x2 = region->extents.x1; |
michael@0 | 1057 | region->extents.y2 = region->extents.y1; |
michael@0 | 1058 | return; |
michael@0 | 1059 | } |
michael@0 | 1060 | |
michael@0 | 1061 | box = PIXREGION_BOXPTR (region); |
michael@0 | 1062 | box_end = PIXREGION_END (region); |
michael@0 | 1063 | |
michael@0 | 1064 | /* |
michael@0 | 1065 | * Since box is the first rectangle in the region, it must have the |
michael@0 | 1066 | * smallest y1 and since box_end is the last rectangle in the region, |
michael@0 | 1067 | * it must have the largest y2, because of banding. Initialize x1 and |
michael@0 | 1068 | * x2 from box and box_end, resp., as good things to initialize them |
michael@0 | 1069 | * to... |
michael@0 | 1070 | */ |
michael@0 | 1071 | region->extents.x1 = box->x1; |
michael@0 | 1072 | region->extents.y1 = box->y1; |
michael@0 | 1073 | region->extents.x2 = box_end->x2; |
michael@0 | 1074 | region->extents.y2 = box_end->y2; |
michael@0 | 1075 | |
michael@0 | 1076 | critical_if_fail (region->extents.y1 < region->extents.y2); |
michael@0 | 1077 | |
michael@0 | 1078 | while (box <= box_end) |
michael@0 | 1079 | { |
michael@0 | 1080 | if (box->x1 < region->extents.x1) |
michael@0 | 1081 | region->extents.x1 = box->x1; |
michael@0 | 1082 | if (box->x2 > region->extents.x2) |
michael@0 | 1083 | region->extents.x2 = box->x2; |
michael@0 | 1084 | box++; |
michael@0 | 1085 | } |
michael@0 | 1086 | |
michael@0 | 1087 | critical_if_fail (region->extents.x1 < region->extents.x2); |
michael@0 | 1088 | } |
michael@0 | 1089 | |
michael@0 | 1090 | /*====================================================================== |
michael@0 | 1091 | * Region Intersection |
michael@0 | 1092 | *====================================================================*/ |
michael@0 | 1093 | /*- |
michael@0 | 1094 | *----------------------------------------------------------------------- |
michael@0 | 1095 | * pixman_region_intersect_o -- |
michael@0 | 1096 | * Handle an overlapping band for pixman_region_intersect. |
michael@0 | 1097 | * |
michael@0 | 1098 | * Results: |
michael@0 | 1099 | * TRUE if successful. |
michael@0 | 1100 | * |
michael@0 | 1101 | * Side Effects: |
michael@0 | 1102 | * Rectangles may be added to the region. |
michael@0 | 1103 | * |
michael@0 | 1104 | *----------------------------------------------------------------------- |
michael@0 | 1105 | */ |
michael@0 | 1106 | /*ARGSUSED*/ |
michael@0 | 1107 | static pixman_bool_t |
michael@0 | 1108 | pixman_region_intersect_o (region_type_t *region, |
michael@0 | 1109 | box_type_t * r1, |
michael@0 | 1110 | box_type_t * r1_end, |
michael@0 | 1111 | box_type_t * r2, |
michael@0 | 1112 | box_type_t * r2_end, |
michael@0 | 1113 | int y1, |
michael@0 | 1114 | int y2) |
michael@0 | 1115 | { |
michael@0 | 1116 | int x1; |
michael@0 | 1117 | int x2; |
michael@0 | 1118 | box_type_t * next_rect; |
michael@0 | 1119 | |
michael@0 | 1120 | next_rect = PIXREGION_TOP (region); |
michael@0 | 1121 | |
michael@0 | 1122 | critical_if_fail (y1 < y2); |
michael@0 | 1123 | critical_if_fail (r1 != r1_end && r2 != r2_end); |
michael@0 | 1124 | |
michael@0 | 1125 | do |
michael@0 | 1126 | { |
michael@0 | 1127 | x1 = MAX (r1->x1, r2->x1); |
michael@0 | 1128 | x2 = MIN (r1->x2, r2->x2); |
michael@0 | 1129 | |
michael@0 | 1130 | /* |
michael@0 | 1131 | * If there's any overlap between the two rectangles, add that |
michael@0 | 1132 | * overlap to the new region. |
michael@0 | 1133 | */ |
michael@0 | 1134 | if (x1 < x2) |
michael@0 | 1135 | NEWRECT (region, next_rect, x1, y1, x2, y2); |
michael@0 | 1136 | |
michael@0 | 1137 | /* |
michael@0 | 1138 | * Advance the pointer(s) with the leftmost right side, since the next |
michael@0 | 1139 | * rectangle on that list may still overlap the other region's |
michael@0 | 1140 | * current rectangle. |
michael@0 | 1141 | */ |
michael@0 | 1142 | if (r1->x2 == x2) |
michael@0 | 1143 | { |
michael@0 | 1144 | r1++; |
michael@0 | 1145 | } |
michael@0 | 1146 | if (r2->x2 == x2) |
michael@0 | 1147 | { |
michael@0 | 1148 | r2++; |
michael@0 | 1149 | } |
michael@0 | 1150 | } |
michael@0 | 1151 | while ((r1 != r1_end) && (r2 != r2_end)); |
michael@0 | 1152 | |
michael@0 | 1153 | return TRUE; |
michael@0 | 1154 | } |
michael@0 | 1155 | |
michael@0 | 1156 | PIXMAN_EXPORT pixman_bool_t |
michael@0 | 1157 | PREFIX (_intersect) (region_type_t * new_reg, |
michael@0 | 1158 | region_type_t * reg1, |
michael@0 | 1159 | region_type_t * reg2) |
michael@0 | 1160 | { |
michael@0 | 1161 | GOOD (reg1); |
michael@0 | 1162 | GOOD (reg2); |
michael@0 | 1163 | GOOD (new_reg); |
michael@0 | 1164 | |
michael@0 | 1165 | /* check for trivial reject */ |
michael@0 | 1166 | if (PIXREGION_NIL (reg1) || PIXREGION_NIL (reg2) || |
michael@0 | 1167 | !EXTENTCHECK (®1->extents, ®2->extents)) |
michael@0 | 1168 | { |
michael@0 | 1169 | /* Covers about 20% of all cases */ |
michael@0 | 1170 | FREE_DATA (new_reg); |
michael@0 | 1171 | new_reg->extents.x2 = new_reg->extents.x1; |
michael@0 | 1172 | new_reg->extents.y2 = new_reg->extents.y1; |
michael@0 | 1173 | if (PIXREGION_NAR (reg1) || PIXREGION_NAR (reg2)) |
michael@0 | 1174 | { |
michael@0 | 1175 | new_reg->data = pixman_broken_data; |
michael@0 | 1176 | return FALSE; |
michael@0 | 1177 | } |
michael@0 | 1178 | else |
michael@0 | 1179 | { |
michael@0 | 1180 | new_reg->data = pixman_region_empty_data; |
michael@0 | 1181 | } |
michael@0 | 1182 | } |
michael@0 | 1183 | else if (!reg1->data && !reg2->data) |
michael@0 | 1184 | { |
michael@0 | 1185 | /* Covers about 80% of cases that aren't trivially rejected */ |
michael@0 | 1186 | new_reg->extents.x1 = MAX (reg1->extents.x1, reg2->extents.x1); |
michael@0 | 1187 | new_reg->extents.y1 = MAX (reg1->extents.y1, reg2->extents.y1); |
michael@0 | 1188 | new_reg->extents.x2 = MIN (reg1->extents.x2, reg2->extents.x2); |
michael@0 | 1189 | new_reg->extents.y2 = MIN (reg1->extents.y2, reg2->extents.y2); |
michael@0 | 1190 | |
michael@0 | 1191 | FREE_DATA (new_reg); |
michael@0 | 1192 | |
michael@0 | 1193 | new_reg->data = (region_data_type_t *)NULL; |
michael@0 | 1194 | } |
michael@0 | 1195 | else if (!reg2->data && SUBSUMES (®2->extents, ®1->extents)) |
michael@0 | 1196 | { |
michael@0 | 1197 | return PREFIX (_copy) (new_reg, reg1); |
michael@0 | 1198 | } |
michael@0 | 1199 | else if (!reg1->data && SUBSUMES (®1->extents, ®2->extents)) |
michael@0 | 1200 | { |
michael@0 | 1201 | return PREFIX (_copy) (new_reg, reg2); |
michael@0 | 1202 | } |
michael@0 | 1203 | else if (reg1 == reg2) |
michael@0 | 1204 | { |
michael@0 | 1205 | return PREFIX (_copy) (new_reg, reg1); |
michael@0 | 1206 | } |
michael@0 | 1207 | else |
michael@0 | 1208 | { |
michael@0 | 1209 | /* General purpose intersection */ |
michael@0 | 1210 | |
michael@0 | 1211 | if (!pixman_op (new_reg, reg1, reg2, pixman_region_intersect_o, FALSE, FALSE)) |
michael@0 | 1212 | return FALSE; |
michael@0 | 1213 | |
michael@0 | 1214 | pixman_set_extents (new_reg); |
michael@0 | 1215 | } |
michael@0 | 1216 | |
michael@0 | 1217 | GOOD (new_reg); |
michael@0 | 1218 | return(TRUE); |
michael@0 | 1219 | } |
michael@0 | 1220 | |
michael@0 | 1221 | #define MERGERECT(r) \ |
michael@0 | 1222 | do \ |
michael@0 | 1223 | { \ |
michael@0 | 1224 | if (r->x1 <= x2) \ |
michael@0 | 1225 | { \ |
michael@0 | 1226 | /* Merge with current rectangle */ \ |
michael@0 | 1227 | if (x2 < r->x2) \ |
michael@0 | 1228 | x2 = r->x2; \ |
michael@0 | 1229 | } \ |
michael@0 | 1230 | else \ |
michael@0 | 1231 | { \ |
michael@0 | 1232 | /* Add current rectangle, start new one */ \ |
michael@0 | 1233 | NEWRECT (region, next_rect, x1, y1, x2, y2); \ |
michael@0 | 1234 | x1 = r->x1; \ |
michael@0 | 1235 | x2 = r->x2; \ |
michael@0 | 1236 | } \ |
michael@0 | 1237 | r++; \ |
michael@0 | 1238 | } while (0) |
michael@0 | 1239 | |
michael@0 | 1240 | /*====================================================================== |
michael@0 | 1241 | * Region Union |
michael@0 | 1242 | *====================================================================*/ |
michael@0 | 1243 | |
michael@0 | 1244 | /*- |
michael@0 | 1245 | *----------------------------------------------------------------------- |
michael@0 | 1246 | * pixman_region_union_o -- |
michael@0 | 1247 | * Handle an overlapping band for the union operation. Picks the |
michael@0 | 1248 | * left-most rectangle each time and merges it into the region. |
michael@0 | 1249 | * |
michael@0 | 1250 | * Results: |
michael@0 | 1251 | * TRUE if successful. |
michael@0 | 1252 | * |
michael@0 | 1253 | * Side Effects: |
michael@0 | 1254 | * region is overwritten. |
michael@0 | 1255 | * overlap is set to TRUE if any boxes overlap. |
michael@0 | 1256 | * |
michael@0 | 1257 | *----------------------------------------------------------------------- |
michael@0 | 1258 | */ |
michael@0 | 1259 | static pixman_bool_t |
michael@0 | 1260 | pixman_region_union_o (region_type_t *region, |
michael@0 | 1261 | box_type_t * r1, |
michael@0 | 1262 | box_type_t * r1_end, |
michael@0 | 1263 | box_type_t * r2, |
michael@0 | 1264 | box_type_t * r2_end, |
michael@0 | 1265 | int y1, |
michael@0 | 1266 | int y2) |
michael@0 | 1267 | { |
michael@0 | 1268 | box_type_t *next_rect; |
michael@0 | 1269 | int x1; /* left and right side of current union */ |
michael@0 | 1270 | int x2; |
michael@0 | 1271 | |
michael@0 | 1272 | critical_if_fail (y1 < y2); |
michael@0 | 1273 | critical_if_fail (r1 != r1_end && r2 != r2_end); |
michael@0 | 1274 | |
michael@0 | 1275 | next_rect = PIXREGION_TOP (region); |
michael@0 | 1276 | |
michael@0 | 1277 | /* Start off current rectangle */ |
michael@0 | 1278 | if (r1->x1 < r2->x1) |
michael@0 | 1279 | { |
michael@0 | 1280 | x1 = r1->x1; |
michael@0 | 1281 | x2 = r1->x2; |
michael@0 | 1282 | r1++; |
michael@0 | 1283 | } |
michael@0 | 1284 | else |
michael@0 | 1285 | { |
michael@0 | 1286 | x1 = r2->x1; |
michael@0 | 1287 | x2 = r2->x2; |
michael@0 | 1288 | r2++; |
michael@0 | 1289 | } |
michael@0 | 1290 | while (r1 != r1_end && r2 != r2_end) |
michael@0 | 1291 | { |
michael@0 | 1292 | if (r1->x1 < r2->x1) |
michael@0 | 1293 | MERGERECT (r1); |
michael@0 | 1294 | else |
michael@0 | 1295 | MERGERECT (r2); |
michael@0 | 1296 | } |
michael@0 | 1297 | |
michael@0 | 1298 | /* Finish off whoever (if any) is left */ |
michael@0 | 1299 | if (r1 != r1_end) |
michael@0 | 1300 | { |
michael@0 | 1301 | do |
michael@0 | 1302 | { |
michael@0 | 1303 | MERGERECT (r1); |
michael@0 | 1304 | } |
michael@0 | 1305 | while (r1 != r1_end); |
michael@0 | 1306 | } |
michael@0 | 1307 | else if (r2 != r2_end) |
michael@0 | 1308 | { |
michael@0 | 1309 | do |
michael@0 | 1310 | { |
michael@0 | 1311 | MERGERECT (r2); |
michael@0 | 1312 | } |
michael@0 | 1313 | while (r2 != r2_end); |
michael@0 | 1314 | } |
michael@0 | 1315 | |
michael@0 | 1316 | /* Add current rectangle */ |
michael@0 | 1317 | NEWRECT (region, next_rect, x1, y1, x2, y2); |
michael@0 | 1318 | |
michael@0 | 1319 | return TRUE; |
michael@0 | 1320 | } |
michael@0 | 1321 | |
michael@0 | 1322 | PIXMAN_EXPORT pixman_bool_t |
michael@0 | 1323 | PREFIX(_intersect_rect) (region_type_t *dest, |
michael@0 | 1324 | region_type_t *source, |
michael@0 | 1325 | int x, int y, |
michael@0 | 1326 | unsigned int width, |
michael@0 | 1327 | unsigned int height) |
michael@0 | 1328 | { |
michael@0 | 1329 | region_type_t region; |
michael@0 | 1330 | |
michael@0 | 1331 | region.data = NULL; |
michael@0 | 1332 | region.extents.x1 = x; |
michael@0 | 1333 | region.extents.y1 = y; |
michael@0 | 1334 | region.extents.x2 = x + width; |
michael@0 | 1335 | region.extents.y2 = y + height; |
michael@0 | 1336 | |
michael@0 | 1337 | return PREFIX(_intersect) (dest, source, ®ion); |
michael@0 | 1338 | } |
michael@0 | 1339 | |
michael@0 | 1340 | /* Convenience function for performing union of region with a |
michael@0 | 1341 | * single rectangle |
michael@0 | 1342 | */ |
michael@0 | 1343 | PIXMAN_EXPORT pixman_bool_t |
michael@0 | 1344 | PREFIX (_union_rect) (region_type_t *dest, |
michael@0 | 1345 | region_type_t *source, |
michael@0 | 1346 | int x, |
michael@0 | 1347 | int y, |
michael@0 | 1348 | unsigned int width, |
michael@0 | 1349 | unsigned int height) |
michael@0 | 1350 | { |
michael@0 | 1351 | region_type_t region; |
michael@0 | 1352 | |
michael@0 | 1353 | region.extents.x1 = x; |
michael@0 | 1354 | region.extents.y1 = y; |
michael@0 | 1355 | region.extents.x2 = x + width; |
michael@0 | 1356 | region.extents.y2 = y + height; |
michael@0 | 1357 | |
michael@0 | 1358 | if (!GOOD_RECT (®ion.extents)) |
michael@0 | 1359 | { |
michael@0 | 1360 | if (BAD_RECT (®ion.extents)) |
michael@0 | 1361 | _pixman_log_error (FUNC, "Invalid rectangle passed"); |
michael@0 | 1362 | return PREFIX (_copy) (dest, source); |
michael@0 | 1363 | } |
michael@0 | 1364 | |
michael@0 | 1365 | region.data = NULL; |
michael@0 | 1366 | |
michael@0 | 1367 | return PREFIX (_union) (dest, source, ®ion); |
michael@0 | 1368 | } |
michael@0 | 1369 | |
michael@0 | 1370 | PIXMAN_EXPORT pixman_bool_t |
michael@0 | 1371 | PREFIX (_union) (region_type_t *new_reg, |
michael@0 | 1372 | region_type_t *reg1, |
michael@0 | 1373 | region_type_t *reg2) |
michael@0 | 1374 | { |
michael@0 | 1375 | /* Return TRUE if some overlap |
michael@0 | 1376 | * between reg1, reg2 |
michael@0 | 1377 | */ |
michael@0 | 1378 | GOOD (reg1); |
michael@0 | 1379 | GOOD (reg2); |
michael@0 | 1380 | GOOD (new_reg); |
michael@0 | 1381 | |
michael@0 | 1382 | /* checks all the simple cases */ |
michael@0 | 1383 | |
michael@0 | 1384 | /* |
michael@0 | 1385 | * Region 1 and 2 are the same |
michael@0 | 1386 | */ |
michael@0 | 1387 | if (reg1 == reg2) |
michael@0 | 1388 | return PREFIX (_copy) (new_reg, reg1); |
michael@0 | 1389 | |
michael@0 | 1390 | /* |
michael@0 | 1391 | * Region 1 is empty |
michael@0 | 1392 | */ |
michael@0 | 1393 | if (PIXREGION_NIL (reg1)) |
michael@0 | 1394 | { |
michael@0 | 1395 | if (PIXREGION_NAR (reg1)) |
michael@0 | 1396 | return pixman_break (new_reg); |
michael@0 | 1397 | |
michael@0 | 1398 | if (new_reg != reg2) |
michael@0 | 1399 | return PREFIX (_copy) (new_reg, reg2); |
michael@0 | 1400 | |
michael@0 | 1401 | return TRUE; |
michael@0 | 1402 | } |
michael@0 | 1403 | |
michael@0 | 1404 | /* |
michael@0 | 1405 | * Region 2 is empty |
michael@0 | 1406 | */ |
michael@0 | 1407 | if (PIXREGION_NIL (reg2)) |
michael@0 | 1408 | { |
michael@0 | 1409 | if (PIXREGION_NAR (reg2)) |
michael@0 | 1410 | return pixman_break (new_reg); |
michael@0 | 1411 | |
michael@0 | 1412 | if (new_reg != reg1) |
michael@0 | 1413 | return PREFIX (_copy) (new_reg, reg1); |
michael@0 | 1414 | |
michael@0 | 1415 | return TRUE; |
michael@0 | 1416 | } |
michael@0 | 1417 | |
michael@0 | 1418 | /* |
michael@0 | 1419 | * Region 1 completely subsumes region 2 |
michael@0 | 1420 | */ |
michael@0 | 1421 | if (!reg1->data && SUBSUMES (®1->extents, ®2->extents)) |
michael@0 | 1422 | { |
michael@0 | 1423 | if (new_reg != reg1) |
michael@0 | 1424 | return PREFIX (_copy) (new_reg, reg1); |
michael@0 | 1425 | |
michael@0 | 1426 | return TRUE; |
michael@0 | 1427 | } |
michael@0 | 1428 | |
michael@0 | 1429 | /* |
michael@0 | 1430 | * Region 2 completely subsumes region 1 |
michael@0 | 1431 | */ |
michael@0 | 1432 | if (!reg2->data && SUBSUMES (®2->extents, ®1->extents)) |
michael@0 | 1433 | { |
michael@0 | 1434 | if (new_reg != reg2) |
michael@0 | 1435 | return PREFIX (_copy) (new_reg, reg2); |
michael@0 | 1436 | |
michael@0 | 1437 | return TRUE; |
michael@0 | 1438 | } |
michael@0 | 1439 | |
michael@0 | 1440 | if (!pixman_op (new_reg, reg1, reg2, pixman_region_union_o, TRUE, TRUE)) |
michael@0 | 1441 | return FALSE; |
michael@0 | 1442 | |
michael@0 | 1443 | new_reg->extents.x1 = MIN (reg1->extents.x1, reg2->extents.x1); |
michael@0 | 1444 | new_reg->extents.y1 = MIN (reg1->extents.y1, reg2->extents.y1); |
michael@0 | 1445 | new_reg->extents.x2 = MAX (reg1->extents.x2, reg2->extents.x2); |
michael@0 | 1446 | new_reg->extents.y2 = MAX (reg1->extents.y2, reg2->extents.y2); |
michael@0 | 1447 | |
michael@0 | 1448 | GOOD (new_reg); |
michael@0 | 1449 | |
michael@0 | 1450 | return TRUE; |
michael@0 | 1451 | } |
michael@0 | 1452 | |
michael@0 | 1453 | /*====================================================================== |
michael@0 | 1454 | * Batch Rectangle Union |
michael@0 | 1455 | *====================================================================*/ |
michael@0 | 1456 | |
michael@0 | 1457 | #define EXCHANGE_RECTS(a, b) \ |
michael@0 | 1458 | { \ |
michael@0 | 1459 | box_type_t t; \ |
michael@0 | 1460 | t = rects[a]; \ |
michael@0 | 1461 | rects[a] = rects[b]; \ |
michael@0 | 1462 | rects[b] = t; \ |
michael@0 | 1463 | } |
michael@0 | 1464 | |
michael@0 | 1465 | static void |
michael@0 | 1466 | quick_sort_rects ( |
michael@0 | 1467 | box_type_t rects[], |
michael@0 | 1468 | int numRects) |
michael@0 | 1469 | { |
michael@0 | 1470 | int y1; |
michael@0 | 1471 | int x1; |
michael@0 | 1472 | int i, j; |
michael@0 | 1473 | box_type_t *r; |
michael@0 | 1474 | |
michael@0 | 1475 | /* Always called with numRects > 1 */ |
michael@0 | 1476 | |
michael@0 | 1477 | do |
michael@0 | 1478 | { |
michael@0 | 1479 | if (numRects == 2) |
michael@0 | 1480 | { |
michael@0 | 1481 | if (rects[0].y1 > rects[1].y1 || |
michael@0 | 1482 | (rects[0].y1 == rects[1].y1 && rects[0].x1 > rects[1].x1)) |
michael@0 | 1483 | { |
michael@0 | 1484 | EXCHANGE_RECTS (0, 1); |
michael@0 | 1485 | } |
michael@0 | 1486 | |
michael@0 | 1487 | return; |
michael@0 | 1488 | } |
michael@0 | 1489 | |
michael@0 | 1490 | /* Choose partition element, stick in location 0 */ |
michael@0 | 1491 | EXCHANGE_RECTS (0, numRects >> 1); |
michael@0 | 1492 | y1 = rects[0].y1; |
michael@0 | 1493 | x1 = rects[0].x1; |
michael@0 | 1494 | |
michael@0 | 1495 | /* Partition array */ |
michael@0 | 1496 | i = 0; |
michael@0 | 1497 | j = numRects; |
michael@0 | 1498 | |
michael@0 | 1499 | do |
michael@0 | 1500 | { |
michael@0 | 1501 | r = &(rects[i]); |
michael@0 | 1502 | do |
michael@0 | 1503 | { |
michael@0 | 1504 | r++; |
michael@0 | 1505 | i++; |
michael@0 | 1506 | } |
michael@0 | 1507 | while (i != numRects && (r->y1 < y1 || (r->y1 == y1 && r->x1 < x1))); |
michael@0 | 1508 | |
michael@0 | 1509 | r = &(rects[j]); |
michael@0 | 1510 | do |
michael@0 | 1511 | { |
michael@0 | 1512 | r--; |
michael@0 | 1513 | j--; |
michael@0 | 1514 | } |
michael@0 | 1515 | while (y1 < r->y1 || (y1 == r->y1 && x1 < r->x1)); |
michael@0 | 1516 | |
michael@0 | 1517 | if (i < j) |
michael@0 | 1518 | EXCHANGE_RECTS (i, j); |
michael@0 | 1519 | } |
michael@0 | 1520 | while (i < j); |
michael@0 | 1521 | |
michael@0 | 1522 | /* Move partition element back to middle */ |
michael@0 | 1523 | EXCHANGE_RECTS (0, j); |
michael@0 | 1524 | |
michael@0 | 1525 | /* Recurse */ |
michael@0 | 1526 | if (numRects - j - 1 > 1) |
michael@0 | 1527 | quick_sort_rects (&rects[j + 1], numRects - j - 1); |
michael@0 | 1528 | |
michael@0 | 1529 | numRects = j; |
michael@0 | 1530 | } |
michael@0 | 1531 | while (numRects > 1); |
michael@0 | 1532 | } |
michael@0 | 1533 | |
michael@0 | 1534 | /*- |
michael@0 | 1535 | *----------------------------------------------------------------------- |
michael@0 | 1536 | * pixman_region_validate -- |
michael@0 | 1537 | * |
michael@0 | 1538 | * Take a ``region'' which is a non-y-x-banded random collection of |
michael@0 | 1539 | * rectangles, and compute a nice region which is the union of all the |
michael@0 | 1540 | * rectangles. |
michael@0 | 1541 | * |
michael@0 | 1542 | * Results: |
michael@0 | 1543 | * TRUE if successful. |
michael@0 | 1544 | * |
michael@0 | 1545 | * Side Effects: |
michael@0 | 1546 | * The passed-in ``region'' may be modified. |
michael@0 | 1547 | * overlap set to TRUE if any retangles overlapped, |
michael@0 | 1548 | * else FALSE; |
michael@0 | 1549 | * |
michael@0 | 1550 | * Strategy: |
michael@0 | 1551 | * Step 1. Sort the rectangles into ascending order with primary key y1 |
michael@0 | 1552 | * and secondary key x1. |
michael@0 | 1553 | * |
michael@0 | 1554 | * Step 2. Split the rectangles into the minimum number of proper y-x |
michael@0 | 1555 | * banded regions. This may require horizontally merging |
michael@0 | 1556 | * rectangles, and vertically coalescing bands. With any luck, |
michael@0 | 1557 | * this step in an identity transformation (ala the Box widget), |
michael@0 | 1558 | * or a coalescing into 1 box (ala Menus). |
michael@0 | 1559 | * |
michael@0 | 1560 | * Step 3. Merge the separate regions down to a single region by calling |
michael@0 | 1561 | * pixman_region_union. Maximize the work each pixman_region_union call does by using |
michael@0 | 1562 | * a binary merge. |
michael@0 | 1563 | * |
michael@0 | 1564 | *----------------------------------------------------------------------- |
michael@0 | 1565 | */ |
michael@0 | 1566 | |
michael@0 | 1567 | static pixman_bool_t |
michael@0 | 1568 | validate (region_type_t * badreg) |
michael@0 | 1569 | { |
michael@0 | 1570 | /* Descriptor for regions under construction in Step 2. */ |
michael@0 | 1571 | typedef struct |
michael@0 | 1572 | { |
michael@0 | 1573 | region_type_t reg; |
michael@0 | 1574 | int prev_band; |
michael@0 | 1575 | int cur_band; |
michael@0 | 1576 | } region_info_t; |
michael@0 | 1577 | |
michael@0 | 1578 | region_info_t stack_regions[64]; |
michael@0 | 1579 | |
michael@0 | 1580 | int numRects; /* Original numRects for badreg */ |
michael@0 | 1581 | region_info_t *ri; /* Array of current regions */ |
michael@0 | 1582 | int num_ri; /* Number of entries used in ri */ |
michael@0 | 1583 | int size_ri; /* Number of entries available in ri */ |
michael@0 | 1584 | int i; /* Index into rects */ |
michael@0 | 1585 | int j; /* Index into ri */ |
michael@0 | 1586 | region_info_t *rit; /* &ri[j] */ |
michael@0 | 1587 | region_type_t *reg; /* ri[j].reg */ |
michael@0 | 1588 | box_type_t *box; /* Current box in rects */ |
michael@0 | 1589 | box_type_t *ri_box; /* Last box in ri[j].reg */ |
michael@0 | 1590 | region_type_t *hreg; /* ri[j_half].reg */ |
michael@0 | 1591 | pixman_bool_t ret = TRUE; |
michael@0 | 1592 | |
michael@0 | 1593 | if (!badreg->data) |
michael@0 | 1594 | { |
michael@0 | 1595 | GOOD (badreg); |
michael@0 | 1596 | return TRUE; |
michael@0 | 1597 | } |
michael@0 | 1598 | |
michael@0 | 1599 | numRects = badreg->data->numRects; |
michael@0 | 1600 | if (!numRects) |
michael@0 | 1601 | { |
michael@0 | 1602 | if (PIXREGION_NAR (badreg)) |
michael@0 | 1603 | return FALSE; |
michael@0 | 1604 | GOOD (badreg); |
michael@0 | 1605 | return TRUE; |
michael@0 | 1606 | } |
michael@0 | 1607 | |
michael@0 | 1608 | if (badreg->extents.x1 < badreg->extents.x2) |
michael@0 | 1609 | { |
michael@0 | 1610 | if ((numRects) == 1) |
michael@0 | 1611 | { |
michael@0 | 1612 | FREE_DATA (badreg); |
michael@0 | 1613 | badreg->data = (region_data_type_t *) NULL; |
michael@0 | 1614 | } |
michael@0 | 1615 | else |
michael@0 | 1616 | { |
michael@0 | 1617 | DOWNSIZE (badreg, numRects); |
michael@0 | 1618 | } |
michael@0 | 1619 | |
michael@0 | 1620 | GOOD (badreg); |
michael@0 | 1621 | |
michael@0 | 1622 | return TRUE; |
michael@0 | 1623 | } |
michael@0 | 1624 | |
michael@0 | 1625 | /* Step 1: Sort the rects array into ascending (y1, x1) order */ |
michael@0 | 1626 | quick_sort_rects (PIXREGION_BOXPTR (badreg), numRects); |
michael@0 | 1627 | |
michael@0 | 1628 | /* Step 2: Scatter the sorted array into the minimum number of regions */ |
michael@0 | 1629 | |
michael@0 | 1630 | /* Set up the first region to be the first rectangle in badreg */ |
michael@0 | 1631 | /* Note that step 2 code will never overflow the ri[0].reg rects array */ |
michael@0 | 1632 | ri = stack_regions; |
michael@0 | 1633 | size_ri = sizeof (stack_regions) / sizeof (stack_regions[0]); |
michael@0 | 1634 | num_ri = 1; |
michael@0 | 1635 | ri[0].prev_band = 0; |
michael@0 | 1636 | ri[0].cur_band = 0; |
michael@0 | 1637 | ri[0].reg = *badreg; |
michael@0 | 1638 | box = PIXREGION_BOXPTR (&ri[0].reg); |
michael@0 | 1639 | ri[0].reg.extents = *box; |
michael@0 | 1640 | ri[0].reg.data->numRects = 1; |
michael@0 | 1641 | badreg->extents = *pixman_region_empty_box; |
michael@0 | 1642 | badreg->data = pixman_region_empty_data; |
michael@0 | 1643 | |
michael@0 | 1644 | /* Now scatter rectangles into the minimum set of valid regions. If the |
michael@0 | 1645 | * next rectangle to be added to a region would force an existing rectangle |
michael@0 | 1646 | * in the region to be split up in order to maintain y-x banding, just |
michael@0 | 1647 | * forget it. Try the next region. If it doesn't fit cleanly into any |
michael@0 | 1648 | * region, make a new one. |
michael@0 | 1649 | */ |
michael@0 | 1650 | |
michael@0 | 1651 | for (i = numRects; --i > 0;) |
michael@0 | 1652 | { |
michael@0 | 1653 | box++; |
michael@0 | 1654 | /* Look for a region to append box to */ |
michael@0 | 1655 | for (j = num_ri, rit = ri; --j >= 0; rit++) |
michael@0 | 1656 | { |
michael@0 | 1657 | reg = &rit->reg; |
michael@0 | 1658 | ri_box = PIXREGION_END (reg); |
michael@0 | 1659 | |
michael@0 | 1660 | if (box->y1 == ri_box->y1 && box->y2 == ri_box->y2) |
michael@0 | 1661 | { |
michael@0 | 1662 | /* box is in same band as ri_box. Merge or append it */ |
michael@0 | 1663 | if (box->x1 <= ri_box->x2) |
michael@0 | 1664 | { |
michael@0 | 1665 | /* Merge it with ri_box */ |
michael@0 | 1666 | if (box->x2 > ri_box->x2) |
michael@0 | 1667 | ri_box->x2 = box->x2; |
michael@0 | 1668 | } |
michael@0 | 1669 | else |
michael@0 | 1670 | { |
michael@0 | 1671 | RECTALLOC_BAIL (reg, 1, bail); |
michael@0 | 1672 | *PIXREGION_TOP (reg) = *box; |
michael@0 | 1673 | reg->data->numRects++; |
michael@0 | 1674 | } |
michael@0 | 1675 | |
michael@0 | 1676 | goto next_rect; /* So sue me */ |
michael@0 | 1677 | } |
michael@0 | 1678 | else if (box->y1 >= ri_box->y2) |
michael@0 | 1679 | { |
michael@0 | 1680 | /* Put box into new band */ |
michael@0 | 1681 | if (reg->extents.x2 < ri_box->x2) |
michael@0 | 1682 | reg->extents.x2 = ri_box->x2; |
michael@0 | 1683 | |
michael@0 | 1684 | if (reg->extents.x1 > box->x1) |
michael@0 | 1685 | reg->extents.x1 = box->x1; |
michael@0 | 1686 | |
michael@0 | 1687 | COALESCE (reg, rit->prev_band, rit->cur_band); |
michael@0 | 1688 | rit->cur_band = reg->data->numRects; |
michael@0 | 1689 | RECTALLOC_BAIL (reg, 1, bail); |
michael@0 | 1690 | *PIXREGION_TOP (reg) = *box; |
michael@0 | 1691 | reg->data->numRects++; |
michael@0 | 1692 | |
michael@0 | 1693 | goto next_rect; |
michael@0 | 1694 | } |
michael@0 | 1695 | /* Well, this region was inappropriate. Try the next one. */ |
michael@0 | 1696 | } /* for j */ |
michael@0 | 1697 | |
michael@0 | 1698 | /* Uh-oh. No regions were appropriate. Create a new one. */ |
michael@0 | 1699 | if (size_ri == num_ri) |
michael@0 | 1700 | { |
michael@0 | 1701 | size_t data_size; |
michael@0 | 1702 | |
michael@0 | 1703 | /* Oops, allocate space for new region information */ |
michael@0 | 1704 | size_ri <<= 1; |
michael@0 | 1705 | |
michael@0 | 1706 | data_size = size_ri * sizeof(region_info_t); |
michael@0 | 1707 | if (data_size / size_ri != sizeof(region_info_t)) |
michael@0 | 1708 | goto bail; |
michael@0 | 1709 | |
michael@0 | 1710 | if (ri == stack_regions) |
michael@0 | 1711 | { |
michael@0 | 1712 | rit = malloc (data_size); |
michael@0 | 1713 | if (!rit) |
michael@0 | 1714 | goto bail; |
michael@0 | 1715 | memcpy (rit, ri, num_ri * sizeof (region_info_t)); |
michael@0 | 1716 | } |
michael@0 | 1717 | else |
michael@0 | 1718 | { |
michael@0 | 1719 | rit = (region_info_t *) realloc (ri, data_size); |
michael@0 | 1720 | if (!rit) |
michael@0 | 1721 | goto bail; |
michael@0 | 1722 | } |
michael@0 | 1723 | ri = rit; |
michael@0 | 1724 | rit = &ri[num_ri]; |
michael@0 | 1725 | } |
michael@0 | 1726 | num_ri++; |
michael@0 | 1727 | rit->prev_band = 0; |
michael@0 | 1728 | rit->cur_band = 0; |
michael@0 | 1729 | rit->reg.extents = *box; |
michael@0 | 1730 | rit->reg.data = (region_data_type_t *)NULL; |
michael@0 | 1731 | |
michael@0 | 1732 | /* MUST force allocation */ |
michael@0 | 1733 | if (!pixman_rect_alloc (&rit->reg, (i + num_ri) / num_ri)) |
michael@0 | 1734 | goto bail; |
michael@0 | 1735 | |
michael@0 | 1736 | next_rect: ; |
michael@0 | 1737 | } /* for i */ |
michael@0 | 1738 | |
michael@0 | 1739 | /* Make a final pass over each region in order to COALESCE and set |
michael@0 | 1740 | * extents.x2 and extents.y2 |
michael@0 | 1741 | */ |
michael@0 | 1742 | for (j = num_ri, rit = ri; --j >= 0; rit++) |
michael@0 | 1743 | { |
michael@0 | 1744 | reg = &rit->reg; |
michael@0 | 1745 | ri_box = PIXREGION_END (reg); |
michael@0 | 1746 | reg->extents.y2 = ri_box->y2; |
michael@0 | 1747 | |
michael@0 | 1748 | if (reg->extents.x2 < ri_box->x2) |
michael@0 | 1749 | reg->extents.x2 = ri_box->x2; |
michael@0 | 1750 | |
michael@0 | 1751 | COALESCE (reg, rit->prev_band, rit->cur_band); |
michael@0 | 1752 | |
michael@0 | 1753 | if (reg->data->numRects == 1) /* keep unions happy below */ |
michael@0 | 1754 | { |
michael@0 | 1755 | FREE_DATA (reg); |
michael@0 | 1756 | reg->data = (region_data_type_t *)NULL; |
michael@0 | 1757 | } |
michael@0 | 1758 | } |
michael@0 | 1759 | |
michael@0 | 1760 | /* Step 3: Union all regions into a single region */ |
michael@0 | 1761 | while (num_ri > 1) |
michael@0 | 1762 | { |
michael@0 | 1763 | int half = num_ri / 2; |
michael@0 | 1764 | for (j = num_ri & 1; j < (half + (num_ri & 1)); j++) |
michael@0 | 1765 | { |
michael@0 | 1766 | reg = &ri[j].reg; |
michael@0 | 1767 | hreg = &ri[j + half].reg; |
michael@0 | 1768 | |
michael@0 | 1769 | if (!pixman_op (reg, reg, hreg, pixman_region_union_o, TRUE, TRUE)) |
michael@0 | 1770 | ret = FALSE; |
michael@0 | 1771 | |
michael@0 | 1772 | if (hreg->extents.x1 < reg->extents.x1) |
michael@0 | 1773 | reg->extents.x1 = hreg->extents.x1; |
michael@0 | 1774 | |
michael@0 | 1775 | if (hreg->extents.y1 < reg->extents.y1) |
michael@0 | 1776 | reg->extents.y1 = hreg->extents.y1; |
michael@0 | 1777 | |
michael@0 | 1778 | if (hreg->extents.x2 > reg->extents.x2) |
michael@0 | 1779 | reg->extents.x2 = hreg->extents.x2; |
michael@0 | 1780 | |
michael@0 | 1781 | if (hreg->extents.y2 > reg->extents.y2) |
michael@0 | 1782 | reg->extents.y2 = hreg->extents.y2; |
michael@0 | 1783 | |
michael@0 | 1784 | FREE_DATA (hreg); |
michael@0 | 1785 | } |
michael@0 | 1786 | |
michael@0 | 1787 | num_ri -= half; |
michael@0 | 1788 | |
michael@0 | 1789 | if (!ret) |
michael@0 | 1790 | goto bail; |
michael@0 | 1791 | } |
michael@0 | 1792 | |
michael@0 | 1793 | *badreg = ri[0].reg; |
michael@0 | 1794 | |
michael@0 | 1795 | if (ri != stack_regions) |
michael@0 | 1796 | free (ri); |
michael@0 | 1797 | |
michael@0 | 1798 | GOOD (badreg); |
michael@0 | 1799 | return ret; |
michael@0 | 1800 | |
michael@0 | 1801 | bail: |
michael@0 | 1802 | for (i = 0; i < num_ri; i++) |
michael@0 | 1803 | FREE_DATA (&ri[i].reg); |
michael@0 | 1804 | |
michael@0 | 1805 | if (ri != stack_regions) |
michael@0 | 1806 | free (ri); |
michael@0 | 1807 | |
michael@0 | 1808 | return pixman_break (badreg); |
michael@0 | 1809 | } |
michael@0 | 1810 | |
michael@0 | 1811 | /*====================================================================== |
michael@0 | 1812 | * Region Subtraction |
michael@0 | 1813 | *====================================================================*/ |
michael@0 | 1814 | |
michael@0 | 1815 | /*- |
michael@0 | 1816 | *----------------------------------------------------------------------- |
michael@0 | 1817 | * pixman_region_subtract_o -- |
michael@0 | 1818 | * Overlapping band subtraction. x1 is the left-most point not yet |
michael@0 | 1819 | * checked. |
michael@0 | 1820 | * |
michael@0 | 1821 | * Results: |
michael@0 | 1822 | * TRUE if successful. |
michael@0 | 1823 | * |
michael@0 | 1824 | * Side Effects: |
michael@0 | 1825 | * region may have rectangles added to it. |
michael@0 | 1826 | * |
michael@0 | 1827 | *----------------------------------------------------------------------- |
michael@0 | 1828 | */ |
michael@0 | 1829 | /*ARGSUSED*/ |
michael@0 | 1830 | static pixman_bool_t |
michael@0 | 1831 | pixman_region_subtract_o (region_type_t * region, |
michael@0 | 1832 | box_type_t * r1, |
michael@0 | 1833 | box_type_t * r1_end, |
michael@0 | 1834 | box_type_t * r2, |
michael@0 | 1835 | box_type_t * r2_end, |
michael@0 | 1836 | int y1, |
michael@0 | 1837 | int y2) |
michael@0 | 1838 | { |
michael@0 | 1839 | box_type_t * next_rect; |
michael@0 | 1840 | int x1; |
michael@0 | 1841 | |
michael@0 | 1842 | x1 = r1->x1; |
michael@0 | 1843 | |
michael@0 | 1844 | critical_if_fail (y1 < y2); |
michael@0 | 1845 | critical_if_fail (r1 != r1_end && r2 != r2_end); |
michael@0 | 1846 | |
michael@0 | 1847 | next_rect = PIXREGION_TOP (region); |
michael@0 | 1848 | |
michael@0 | 1849 | do |
michael@0 | 1850 | { |
michael@0 | 1851 | if (r2->x2 <= x1) |
michael@0 | 1852 | { |
michael@0 | 1853 | /* |
michael@0 | 1854 | * Subtrahend entirely to left of minuend: go to next subtrahend. |
michael@0 | 1855 | */ |
michael@0 | 1856 | r2++; |
michael@0 | 1857 | } |
michael@0 | 1858 | else if (r2->x1 <= x1) |
michael@0 | 1859 | { |
michael@0 | 1860 | /* |
michael@0 | 1861 | * Subtrahend preceeds minuend: nuke left edge of minuend. |
michael@0 | 1862 | */ |
michael@0 | 1863 | x1 = r2->x2; |
michael@0 | 1864 | if (x1 >= r1->x2) |
michael@0 | 1865 | { |
michael@0 | 1866 | /* |
michael@0 | 1867 | * Minuend completely covered: advance to next minuend and |
michael@0 | 1868 | * reset left fence to edge of new minuend. |
michael@0 | 1869 | */ |
michael@0 | 1870 | r1++; |
michael@0 | 1871 | if (r1 != r1_end) |
michael@0 | 1872 | x1 = r1->x1; |
michael@0 | 1873 | } |
michael@0 | 1874 | else |
michael@0 | 1875 | { |
michael@0 | 1876 | /* |
michael@0 | 1877 | * Subtrahend now used up since it doesn't extend beyond |
michael@0 | 1878 | * minuend |
michael@0 | 1879 | */ |
michael@0 | 1880 | r2++; |
michael@0 | 1881 | } |
michael@0 | 1882 | } |
michael@0 | 1883 | else if (r2->x1 < r1->x2) |
michael@0 | 1884 | { |
michael@0 | 1885 | /* |
michael@0 | 1886 | * Left part of subtrahend covers part of minuend: add uncovered |
michael@0 | 1887 | * part of minuend to region and skip to next subtrahend. |
michael@0 | 1888 | */ |
michael@0 | 1889 | critical_if_fail (x1 < r2->x1); |
michael@0 | 1890 | NEWRECT (region, next_rect, x1, y1, r2->x1, y2); |
michael@0 | 1891 | |
michael@0 | 1892 | x1 = r2->x2; |
michael@0 | 1893 | if (x1 >= r1->x2) |
michael@0 | 1894 | { |
michael@0 | 1895 | /* |
michael@0 | 1896 | * Minuend used up: advance to new... |
michael@0 | 1897 | */ |
michael@0 | 1898 | r1++; |
michael@0 | 1899 | if (r1 != r1_end) |
michael@0 | 1900 | x1 = r1->x1; |
michael@0 | 1901 | } |
michael@0 | 1902 | else |
michael@0 | 1903 | { |
michael@0 | 1904 | /* |
michael@0 | 1905 | * Subtrahend used up |
michael@0 | 1906 | */ |
michael@0 | 1907 | r2++; |
michael@0 | 1908 | } |
michael@0 | 1909 | } |
michael@0 | 1910 | else |
michael@0 | 1911 | { |
michael@0 | 1912 | /* |
michael@0 | 1913 | * Minuend used up: add any remaining piece before advancing. |
michael@0 | 1914 | */ |
michael@0 | 1915 | if (r1->x2 > x1) |
michael@0 | 1916 | NEWRECT (region, next_rect, x1, y1, r1->x2, y2); |
michael@0 | 1917 | |
michael@0 | 1918 | r1++; |
michael@0 | 1919 | |
michael@0 | 1920 | if (r1 != r1_end) |
michael@0 | 1921 | x1 = r1->x1; |
michael@0 | 1922 | } |
michael@0 | 1923 | } |
michael@0 | 1924 | while ((r1 != r1_end) && (r2 != r2_end)); |
michael@0 | 1925 | |
michael@0 | 1926 | /* |
michael@0 | 1927 | * Add remaining minuend rectangles to region. |
michael@0 | 1928 | */ |
michael@0 | 1929 | while (r1 != r1_end) |
michael@0 | 1930 | { |
michael@0 | 1931 | critical_if_fail (x1 < r1->x2); |
michael@0 | 1932 | |
michael@0 | 1933 | NEWRECT (region, next_rect, x1, y1, r1->x2, y2); |
michael@0 | 1934 | |
michael@0 | 1935 | r1++; |
michael@0 | 1936 | if (r1 != r1_end) |
michael@0 | 1937 | x1 = r1->x1; |
michael@0 | 1938 | } |
michael@0 | 1939 | return TRUE; |
michael@0 | 1940 | } |
michael@0 | 1941 | |
michael@0 | 1942 | /*- |
michael@0 | 1943 | *----------------------------------------------------------------------- |
michael@0 | 1944 | * pixman_region_subtract -- |
michael@0 | 1945 | * Subtract reg_s from reg_m and leave the result in reg_d. |
michael@0 | 1946 | * S stands for subtrahend, M for minuend and D for difference. |
michael@0 | 1947 | * |
michael@0 | 1948 | * Results: |
michael@0 | 1949 | * TRUE if successful. |
michael@0 | 1950 | * |
michael@0 | 1951 | * Side Effects: |
michael@0 | 1952 | * reg_d is overwritten. |
michael@0 | 1953 | * |
michael@0 | 1954 | *----------------------------------------------------------------------- |
michael@0 | 1955 | */ |
michael@0 | 1956 | PIXMAN_EXPORT pixman_bool_t |
michael@0 | 1957 | PREFIX (_subtract) (region_type_t *reg_d, |
michael@0 | 1958 | region_type_t *reg_m, |
michael@0 | 1959 | region_type_t *reg_s) |
michael@0 | 1960 | { |
michael@0 | 1961 | GOOD (reg_m); |
michael@0 | 1962 | GOOD (reg_s); |
michael@0 | 1963 | GOOD (reg_d); |
michael@0 | 1964 | |
michael@0 | 1965 | /* check for trivial rejects */ |
michael@0 | 1966 | if (PIXREGION_NIL (reg_m) || PIXREGION_NIL (reg_s) || |
michael@0 | 1967 | !EXTENTCHECK (®_m->extents, ®_s->extents)) |
michael@0 | 1968 | { |
michael@0 | 1969 | if (PIXREGION_NAR (reg_s)) |
michael@0 | 1970 | return pixman_break (reg_d); |
michael@0 | 1971 | |
michael@0 | 1972 | return PREFIX (_copy) (reg_d, reg_m); |
michael@0 | 1973 | } |
michael@0 | 1974 | else if (reg_m == reg_s) |
michael@0 | 1975 | { |
michael@0 | 1976 | FREE_DATA (reg_d); |
michael@0 | 1977 | reg_d->extents.x2 = reg_d->extents.x1; |
michael@0 | 1978 | reg_d->extents.y2 = reg_d->extents.y1; |
michael@0 | 1979 | reg_d->data = pixman_region_empty_data; |
michael@0 | 1980 | |
michael@0 | 1981 | return TRUE; |
michael@0 | 1982 | } |
michael@0 | 1983 | |
michael@0 | 1984 | /* Add those rectangles in region 1 that aren't in region 2, |
michael@0 | 1985 | do yucky substraction for overlaps, and |
michael@0 | 1986 | just throw away rectangles in region 2 that aren't in region 1 */ |
michael@0 | 1987 | if (!pixman_op (reg_d, reg_m, reg_s, pixman_region_subtract_o, TRUE, FALSE)) |
michael@0 | 1988 | return FALSE; |
michael@0 | 1989 | |
michael@0 | 1990 | /* |
michael@0 | 1991 | * Can't alter reg_d's extents before we call pixman_op because |
michael@0 | 1992 | * it might be one of the source regions and pixman_op depends |
michael@0 | 1993 | * on the extents of those regions being unaltered. Besides, this |
michael@0 | 1994 | * way there's no checking against rectangles that will be nuked |
michael@0 | 1995 | * due to coalescing, so we have to examine fewer rectangles. |
michael@0 | 1996 | */ |
michael@0 | 1997 | pixman_set_extents (reg_d); |
michael@0 | 1998 | GOOD (reg_d); |
michael@0 | 1999 | return TRUE; |
michael@0 | 2000 | } |
michael@0 | 2001 | |
michael@0 | 2002 | /*====================================================================== |
michael@0 | 2003 | * Region Inversion |
michael@0 | 2004 | *====================================================================*/ |
michael@0 | 2005 | |
michael@0 | 2006 | /*- |
michael@0 | 2007 | *----------------------------------------------------------------------- |
michael@0 | 2008 | * pixman_region_inverse -- |
michael@0 | 2009 | * Take a region and a box and return a region that is everything |
michael@0 | 2010 | * in the box but not in the region. The careful reader will note |
michael@0 | 2011 | * that this is the same as subtracting the region from the box... |
michael@0 | 2012 | * |
michael@0 | 2013 | * Results: |
michael@0 | 2014 | * TRUE. |
michael@0 | 2015 | * |
michael@0 | 2016 | * Side Effects: |
michael@0 | 2017 | * new_reg is overwritten. |
michael@0 | 2018 | * |
michael@0 | 2019 | *----------------------------------------------------------------------- |
michael@0 | 2020 | */ |
michael@0 | 2021 | PIXMAN_EXPORT pixman_bool_t |
michael@0 | 2022 | PREFIX (_inverse) (region_type_t *new_reg, /* Destination region */ |
michael@0 | 2023 | region_type_t *reg1, /* Region to invert */ |
michael@0 | 2024 | box_type_t * inv_rect) /* Bounding box for inversion */ |
michael@0 | 2025 | { |
michael@0 | 2026 | region_type_t inv_reg; /* Quick and dirty region made from the |
michael@0 | 2027 | * bounding box */ |
michael@0 | 2028 | GOOD (reg1); |
michael@0 | 2029 | GOOD (new_reg); |
michael@0 | 2030 | |
michael@0 | 2031 | /* check for trivial rejects */ |
michael@0 | 2032 | if (PIXREGION_NIL (reg1) || !EXTENTCHECK (inv_rect, ®1->extents)) |
michael@0 | 2033 | { |
michael@0 | 2034 | if (PIXREGION_NAR (reg1)) |
michael@0 | 2035 | return pixman_break (new_reg); |
michael@0 | 2036 | |
michael@0 | 2037 | new_reg->extents = *inv_rect; |
michael@0 | 2038 | FREE_DATA (new_reg); |
michael@0 | 2039 | new_reg->data = (region_data_type_t *)NULL; |
michael@0 | 2040 | |
michael@0 | 2041 | return TRUE; |
michael@0 | 2042 | } |
michael@0 | 2043 | |
michael@0 | 2044 | /* Add those rectangles in region 1 that aren't in region 2, |
michael@0 | 2045 | * do yucky substraction for overlaps, and |
michael@0 | 2046 | * just throw away rectangles in region 2 that aren't in region 1 |
michael@0 | 2047 | */ |
michael@0 | 2048 | inv_reg.extents = *inv_rect; |
michael@0 | 2049 | inv_reg.data = (region_data_type_t *)NULL; |
michael@0 | 2050 | if (!pixman_op (new_reg, &inv_reg, reg1, pixman_region_subtract_o, TRUE, FALSE)) |
michael@0 | 2051 | return FALSE; |
michael@0 | 2052 | |
michael@0 | 2053 | /* |
michael@0 | 2054 | * Can't alter new_reg's extents before we call pixman_op because |
michael@0 | 2055 | * it might be one of the source regions and pixman_op depends |
michael@0 | 2056 | * on the extents of those regions being unaltered. Besides, this |
michael@0 | 2057 | * way there's no checking against rectangles that will be nuked |
michael@0 | 2058 | * due to coalescing, so we have to examine fewer rectangles. |
michael@0 | 2059 | */ |
michael@0 | 2060 | pixman_set_extents (new_reg); |
michael@0 | 2061 | GOOD (new_reg); |
michael@0 | 2062 | return TRUE; |
michael@0 | 2063 | } |
michael@0 | 2064 | |
michael@0 | 2065 | /* In time O(log n), locate the first box whose y2 is greater than y. |
michael@0 | 2066 | * Return @end if no such box exists. |
michael@0 | 2067 | */ |
michael@0 | 2068 | static box_type_t * |
michael@0 | 2069 | find_box_for_y (box_type_t *begin, box_type_t *end, int y) |
michael@0 | 2070 | { |
michael@0 | 2071 | box_type_t *mid; |
michael@0 | 2072 | |
michael@0 | 2073 | if (end == begin) |
michael@0 | 2074 | return end; |
michael@0 | 2075 | |
michael@0 | 2076 | if (end - begin == 1) |
michael@0 | 2077 | { |
michael@0 | 2078 | if (begin->y2 > y) |
michael@0 | 2079 | return begin; |
michael@0 | 2080 | else |
michael@0 | 2081 | return end; |
michael@0 | 2082 | } |
michael@0 | 2083 | |
michael@0 | 2084 | mid = begin + (end - begin) / 2; |
michael@0 | 2085 | if (mid->y2 > y) |
michael@0 | 2086 | { |
michael@0 | 2087 | /* If no box is found in [begin, mid], the function |
michael@0 | 2088 | * will return @mid, which is then known to be the |
michael@0 | 2089 | * correct answer. |
michael@0 | 2090 | */ |
michael@0 | 2091 | return find_box_for_y (begin, mid, y); |
michael@0 | 2092 | } |
michael@0 | 2093 | else |
michael@0 | 2094 | { |
michael@0 | 2095 | return find_box_for_y (mid, end, y); |
michael@0 | 2096 | } |
michael@0 | 2097 | } |
michael@0 | 2098 | |
michael@0 | 2099 | /* |
michael@0 | 2100 | * rect_in(region, rect) |
michael@0 | 2101 | * This routine takes a pointer to a region and a pointer to a box |
michael@0 | 2102 | * and determines if the box is outside/inside/partly inside the region. |
michael@0 | 2103 | * |
michael@0 | 2104 | * The idea is to travel through the list of rectangles trying to cover the |
michael@0 | 2105 | * passed box with them. Anytime a piece of the rectangle isn't covered |
michael@0 | 2106 | * by a band of rectangles, part_out is set TRUE. Any time a rectangle in |
michael@0 | 2107 | * the region covers part of the box, part_in is set TRUE. The process ends |
michael@0 | 2108 | * when either the box has been completely covered (we reached a band that |
michael@0 | 2109 | * doesn't overlap the box, part_in is TRUE and part_out is false), the |
michael@0 | 2110 | * box has been partially covered (part_in == part_out == TRUE -- because of |
michael@0 | 2111 | * the banding, the first time this is true we know the box is only |
michael@0 | 2112 | * partially in the region) or is outside the region (we reached a band |
michael@0 | 2113 | * that doesn't overlap the box at all and part_in is false) |
michael@0 | 2114 | */ |
michael@0 | 2115 | PIXMAN_EXPORT pixman_region_overlap_t |
michael@0 | 2116 | PREFIX (_contains_rectangle) (region_type_t * region, |
michael@0 | 2117 | box_type_t * prect) |
michael@0 | 2118 | { |
michael@0 | 2119 | box_type_t * pbox; |
michael@0 | 2120 | box_type_t * pbox_end; |
michael@0 | 2121 | int part_in, part_out; |
michael@0 | 2122 | int numRects; |
michael@0 | 2123 | int x, y; |
michael@0 | 2124 | |
michael@0 | 2125 | GOOD (region); |
michael@0 | 2126 | |
michael@0 | 2127 | numRects = PIXREGION_NUMRECTS (region); |
michael@0 | 2128 | |
michael@0 | 2129 | /* useful optimization */ |
michael@0 | 2130 | if (!numRects || !EXTENTCHECK (®ion->extents, prect)) |
michael@0 | 2131 | return(PIXMAN_REGION_OUT); |
michael@0 | 2132 | |
michael@0 | 2133 | if (numRects == 1) |
michael@0 | 2134 | { |
michael@0 | 2135 | /* We know that it must be PIXMAN_REGION_IN or PIXMAN_REGION_PART */ |
michael@0 | 2136 | if (SUBSUMES (®ion->extents, prect)) |
michael@0 | 2137 | return(PIXMAN_REGION_IN); |
michael@0 | 2138 | else |
michael@0 | 2139 | return(PIXMAN_REGION_PART); |
michael@0 | 2140 | } |
michael@0 | 2141 | |
michael@0 | 2142 | part_out = FALSE; |
michael@0 | 2143 | part_in = FALSE; |
michael@0 | 2144 | |
michael@0 | 2145 | /* (x,y) starts at upper left of rect, moving to the right and down */ |
michael@0 | 2146 | x = prect->x1; |
michael@0 | 2147 | y = prect->y1; |
michael@0 | 2148 | |
michael@0 | 2149 | /* can stop when both part_out and part_in are TRUE, or we reach prect->y2 */ |
michael@0 | 2150 | for (pbox = PIXREGION_BOXPTR (region), pbox_end = pbox + numRects; |
michael@0 | 2151 | pbox != pbox_end; |
michael@0 | 2152 | pbox++) |
michael@0 | 2153 | { |
michael@0 | 2154 | /* getting up to speed or skipping remainder of band */ |
michael@0 | 2155 | if (pbox->y2 <= y) |
michael@0 | 2156 | { |
michael@0 | 2157 | if ((pbox = find_box_for_y (pbox, pbox_end, y)) == pbox_end) |
michael@0 | 2158 | break; |
michael@0 | 2159 | } |
michael@0 | 2160 | |
michael@0 | 2161 | if (pbox->y1 > y) |
michael@0 | 2162 | { |
michael@0 | 2163 | part_out = TRUE; /* missed part of rectangle above */ |
michael@0 | 2164 | if (part_in || (pbox->y1 >= prect->y2)) |
michael@0 | 2165 | break; |
michael@0 | 2166 | y = pbox->y1; /* x guaranteed to be == prect->x1 */ |
michael@0 | 2167 | } |
michael@0 | 2168 | |
michael@0 | 2169 | if (pbox->x2 <= x) |
michael@0 | 2170 | continue; /* not far enough over yet */ |
michael@0 | 2171 | |
michael@0 | 2172 | if (pbox->x1 > x) |
michael@0 | 2173 | { |
michael@0 | 2174 | part_out = TRUE; /* missed part of rectangle to left */ |
michael@0 | 2175 | if (part_in) |
michael@0 | 2176 | break; |
michael@0 | 2177 | } |
michael@0 | 2178 | |
michael@0 | 2179 | if (pbox->x1 < prect->x2) |
michael@0 | 2180 | { |
michael@0 | 2181 | part_in = TRUE; /* definitely overlap */ |
michael@0 | 2182 | if (part_out) |
michael@0 | 2183 | break; |
michael@0 | 2184 | } |
michael@0 | 2185 | |
michael@0 | 2186 | if (pbox->x2 >= prect->x2) |
michael@0 | 2187 | { |
michael@0 | 2188 | y = pbox->y2; /* finished with this band */ |
michael@0 | 2189 | if (y >= prect->y2) |
michael@0 | 2190 | break; |
michael@0 | 2191 | x = prect->x1; /* reset x out to left again */ |
michael@0 | 2192 | } |
michael@0 | 2193 | else |
michael@0 | 2194 | { |
michael@0 | 2195 | /* |
michael@0 | 2196 | * Because boxes in a band are maximal width, if the first box |
michael@0 | 2197 | * to overlap the rectangle doesn't completely cover it in that |
michael@0 | 2198 | * band, the rectangle must be partially out, since some of it |
michael@0 | 2199 | * will be uncovered in that band. part_in will have been set true |
michael@0 | 2200 | * by now... |
michael@0 | 2201 | */ |
michael@0 | 2202 | part_out = TRUE; |
michael@0 | 2203 | break; |
michael@0 | 2204 | } |
michael@0 | 2205 | } |
michael@0 | 2206 | |
michael@0 | 2207 | if (part_in) |
michael@0 | 2208 | { |
michael@0 | 2209 | if (y < prect->y2) |
michael@0 | 2210 | return PIXMAN_REGION_PART; |
michael@0 | 2211 | else |
michael@0 | 2212 | return PIXMAN_REGION_IN; |
michael@0 | 2213 | } |
michael@0 | 2214 | else |
michael@0 | 2215 | { |
michael@0 | 2216 | return PIXMAN_REGION_OUT; |
michael@0 | 2217 | } |
michael@0 | 2218 | } |
michael@0 | 2219 | |
michael@0 | 2220 | /* PREFIX(_translate) (region, x, y) |
michael@0 | 2221 | * translates in place |
michael@0 | 2222 | */ |
michael@0 | 2223 | |
michael@0 | 2224 | PIXMAN_EXPORT void |
michael@0 | 2225 | PREFIX (_translate) (region_type_t *region, int x, int y) |
michael@0 | 2226 | { |
michael@0 | 2227 | overflow_int_t x1, x2, y1, y2; |
michael@0 | 2228 | int nbox; |
michael@0 | 2229 | box_type_t * pbox; |
michael@0 | 2230 | |
michael@0 | 2231 | GOOD (region); |
michael@0 | 2232 | region->extents.x1 = x1 = region->extents.x1 + x; |
michael@0 | 2233 | region->extents.y1 = y1 = region->extents.y1 + y; |
michael@0 | 2234 | region->extents.x2 = x2 = region->extents.x2 + x; |
michael@0 | 2235 | region->extents.y2 = y2 = region->extents.y2 + y; |
michael@0 | 2236 | |
michael@0 | 2237 | if (((x1 - PIXMAN_REGION_MIN) | (y1 - PIXMAN_REGION_MIN) | (PIXMAN_REGION_MAX - x2) | (PIXMAN_REGION_MAX - y2)) >= 0) |
michael@0 | 2238 | { |
michael@0 | 2239 | if (region->data && (nbox = region->data->numRects)) |
michael@0 | 2240 | { |
michael@0 | 2241 | for (pbox = PIXREGION_BOXPTR (region); nbox--; pbox++) |
michael@0 | 2242 | { |
michael@0 | 2243 | pbox->x1 += x; |
michael@0 | 2244 | pbox->y1 += y; |
michael@0 | 2245 | pbox->x2 += x; |
michael@0 | 2246 | pbox->y2 += y; |
michael@0 | 2247 | } |
michael@0 | 2248 | } |
michael@0 | 2249 | return; |
michael@0 | 2250 | } |
michael@0 | 2251 | |
michael@0 | 2252 | if (((x2 - PIXMAN_REGION_MIN) | (y2 - PIXMAN_REGION_MIN) | (PIXMAN_REGION_MAX - x1) | (PIXMAN_REGION_MAX - y1)) <= 0) |
michael@0 | 2253 | { |
michael@0 | 2254 | region->extents.x2 = region->extents.x1; |
michael@0 | 2255 | region->extents.y2 = region->extents.y1; |
michael@0 | 2256 | FREE_DATA (region); |
michael@0 | 2257 | region->data = pixman_region_empty_data; |
michael@0 | 2258 | return; |
michael@0 | 2259 | } |
michael@0 | 2260 | |
michael@0 | 2261 | if (x1 < PIXMAN_REGION_MIN) |
michael@0 | 2262 | region->extents.x1 = PIXMAN_REGION_MIN; |
michael@0 | 2263 | else if (x2 > PIXMAN_REGION_MAX) |
michael@0 | 2264 | region->extents.x2 = PIXMAN_REGION_MAX; |
michael@0 | 2265 | |
michael@0 | 2266 | if (y1 < PIXMAN_REGION_MIN) |
michael@0 | 2267 | region->extents.y1 = PIXMAN_REGION_MIN; |
michael@0 | 2268 | else if (y2 > PIXMAN_REGION_MAX) |
michael@0 | 2269 | region->extents.y2 = PIXMAN_REGION_MAX; |
michael@0 | 2270 | |
michael@0 | 2271 | if (region->data && (nbox = region->data->numRects)) |
michael@0 | 2272 | { |
michael@0 | 2273 | box_type_t * pbox_out; |
michael@0 | 2274 | |
michael@0 | 2275 | for (pbox_out = pbox = PIXREGION_BOXPTR (region); nbox--; pbox++) |
michael@0 | 2276 | { |
michael@0 | 2277 | pbox_out->x1 = x1 = pbox->x1 + x; |
michael@0 | 2278 | pbox_out->y1 = y1 = pbox->y1 + y; |
michael@0 | 2279 | pbox_out->x2 = x2 = pbox->x2 + x; |
michael@0 | 2280 | pbox_out->y2 = y2 = pbox->y2 + y; |
michael@0 | 2281 | |
michael@0 | 2282 | if (((x2 - PIXMAN_REGION_MIN) | (y2 - PIXMAN_REGION_MIN) | |
michael@0 | 2283 | (PIXMAN_REGION_MAX - x1) | (PIXMAN_REGION_MAX - y1)) <= 0) |
michael@0 | 2284 | { |
michael@0 | 2285 | region->data->numRects--; |
michael@0 | 2286 | continue; |
michael@0 | 2287 | } |
michael@0 | 2288 | |
michael@0 | 2289 | if (x1 < PIXMAN_REGION_MIN) |
michael@0 | 2290 | pbox_out->x1 = PIXMAN_REGION_MIN; |
michael@0 | 2291 | else if (x2 > PIXMAN_REGION_MAX) |
michael@0 | 2292 | pbox_out->x2 = PIXMAN_REGION_MAX; |
michael@0 | 2293 | |
michael@0 | 2294 | if (y1 < PIXMAN_REGION_MIN) |
michael@0 | 2295 | pbox_out->y1 = PIXMAN_REGION_MIN; |
michael@0 | 2296 | else if (y2 > PIXMAN_REGION_MAX) |
michael@0 | 2297 | pbox_out->y2 = PIXMAN_REGION_MAX; |
michael@0 | 2298 | |
michael@0 | 2299 | pbox_out++; |
michael@0 | 2300 | } |
michael@0 | 2301 | |
michael@0 | 2302 | if (pbox_out != pbox) |
michael@0 | 2303 | { |
michael@0 | 2304 | if (region->data->numRects == 1) |
michael@0 | 2305 | { |
michael@0 | 2306 | region->extents = *PIXREGION_BOXPTR (region); |
michael@0 | 2307 | FREE_DATA (region); |
michael@0 | 2308 | region->data = (region_data_type_t *)NULL; |
michael@0 | 2309 | } |
michael@0 | 2310 | else |
michael@0 | 2311 | { |
michael@0 | 2312 | pixman_set_extents (region); |
michael@0 | 2313 | } |
michael@0 | 2314 | } |
michael@0 | 2315 | } |
michael@0 | 2316 | |
michael@0 | 2317 | GOOD (region); |
michael@0 | 2318 | } |
michael@0 | 2319 | |
michael@0 | 2320 | PIXMAN_EXPORT void |
michael@0 | 2321 | PREFIX (_reset) (region_type_t *region, box_type_t *box) |
michael@0 | 2322 | { |
michael@0 | 2323 | GOOD (region); |
michael@0 | 2324 | |
michael@0 | 2325 | critical_if_fail (GOOD_RECT (box)); |
michael@0 | 2326 | |
michael@0 | 2327 | region->extents = *box; |
michael@0 | 2328 | |
michael@0 | 2329 | FREE_DATA (region); |
michael@0 | 2330 | |
michael@0 | 2331 | region->data = NULL; |
michael@0 | 2332 | } |
michael@0 | 2333 | |
michael@0 | 2334 | PIXMAN_EXPORT void |
michael@0 | 2335 | PREFIX (_clear) (region_type_t *region) |
michael@0 | 2336 | { |
michael@0 | 2337 | GOOD (region); |
michael@0 | 2338 | FREE_DATA (region); |
michael@0 | 2339 | |
michael@0 | 2340 | region->extents = *pixman_region_empty_box; |
michael@0 | 2341 | region->data = pixman_region_empty_data; |
michael@0 | 2342 | } |
michael@0 | 2343 | |
michael@0 | 2344 | /* box is "return" value */ |
michael@0 | 2345 | PIXMAN_EXPORT int |
michael@0 | 2346 | PREFIX (_contains_point) (region_type_t * region, |
michael@0 | 2347 | int x, int y, |
michael@0 | 2348 | box_type_t * box) |
michael@0 | 2349 | { |
michael@0 | 2350 | box_type_t *pbox, *pbox_end; |
michael@0 | 2351 | int numRects; |
michael@0 | 2352 | |
michael@0 | 2353 | GOOD (region); |
michael@0 | 2354 | numRects = PIXREGION_NUMRECTS (region); |
michael@0 | 2355 | |
michael@0 | 2356 | if (!numRects || !INBOX (®ion->extents, x, y)) |
michael@0 | 2357 | return(FALSE); |
michael@0 | 2358 | |
michael@0 | 2359 | if (numRects == 1) |
michael@0 | 2360 | { |
michael@0 | 2361 | if (box) |
michael@0 | 2362 | *box = region->extents; |
michael@0 | 2363 | |
michael@0 | 2364 | return(TRUE); |
michael@0 | 2365 | } |
michael@0 | 2366 | |
michael@0 | 2367 | pbox = PIXREGION_BOXPTR (region); |
michael@0 | 2368 | pbox_end = pbox + numRects; |
michael@0 | 2369 | |
michael@0 | 2370 | pbox = find_box_for_y (pbox, pbox_end, y); |
michael@0 | 2371 | |
michael@0 | 2372 | for (;pbox != pbox_end; pbox++) |
michael@0 | 2373 | { |
michael@0 | 2374 | if ((y < pbox->y1) || (x < pbox->x1)) |
michael@0 | 2375 | break; /* missed it */ |
michael@0 | 2376 | |
michael@0 | 2377 | if (x >= pbox->x2) |
michael@0 | 2378 | continue; /* not there yet */ |
michael@0 | 2379 | |
michael@0 | 2380 | if (box) |
michael@0 | 2381 | *box = *pbox; |
michael@0 | 2382 | |
michael@0 | 2383 | return(TRUE); |
michael@0 | 2384 | } |
michael@0 | 2385 | |
michael@0 | 2386 | return(FALSE); |
michael@0 | 2387 | } |
michael@0 | 2388 | |
michael@0 | 2389 | PIXMAN_EXPORT int |
michael@0 | 2390 | PREFIX (_not_empty) (region_type_t * region) |
michael@0 | 2391 | { |
michael@0 | 2392 | GOOD (region); |
michael@0 | 2393 | |
michael@0 | 2394 | return(!PIXREGION_NIL (region)); |
michael@0 | 2395 | } |
michael@0 | 2396 | |
michael@0 | 2397 | PIXMAN_EXPORT box_type_t * |
michael@0 | 2398 | PREFIX (_extents) (region_type_t * region) |
michael@0 | 2399 | { |
michael@0 | 2400 | GOOD (region); |
michael@0 | 2401 | |
michael@0 | 2402 | return(®ion->extents); |
michael@0 | 2403 | } |
michael@0 | 2404 | |
michael@0 | 2405 | /* |
michael@0 | 2406 | * Clip a list of scanlines to a region. The caller has allocated the |
michael@0 | 2407 | * space. FSorted is non-zero if the scanline origins are in ascending order. |
michael@0 | 2408 | * |
michael@0 | 2409 | * returns the number of new, clipped scanlines. |
michael@0 | 2410 | */ |
michael@0 | 2411 | |
michael@0 | 2412 | PIXMAN_EXPORT pixman_bool_t |
michael@0 | 2413 | PREFIX (_selfcheck) (region_type_t *reg) |
michael@0 | 2414 | { |
michael@0 | 2415 | int i, numRects; |
michael@0 | 2416 | |
michael@0 | 2417 | if ((reg->extents.x1 > reg->extents.x2) || |
michael@0 | 2418 | (reg->extents.y1 > reg->extents.y2)) |
michael@0 | 2419 | { |
michael@0 | 2420 | return FALSE; |
michael@0 | 2421 | } |
michael@0 | 2422 | |
michael@0 | 2423 | numRects = PIXREGION_NUMRECTS (reg); |
michael@0 | 2424 | if (!numRects) |
michael@0 | 2425 | { |
michael@0 | 2426 | return ((reg->extents.x1 == reg->extents.x2) && |
michael@0 | 2427 | (reg->extents.y1 == reg->extents.y2) && |
michael@0 | 2428 | (reg->data->size || (reg->data == pixman_region_empty_data))); |
michael@0 | 2429 | } |
michael@0 | 2430 | else if (numRects == 1) |
michael@0 | 2431 | { |
michael@0 | 2432 | return (!reg->data); |
michael@0 | 2433 | } |
michael@0 | 2434 | else |
michael@0 | 2435 | { |
michael@0 | 2436 | box_type_t * pbox_p, * pbox_n; |
michael@0 | 2437 | box_type_t box; |
michael@0 | 2438 | |
michael@0 | 2439 | pbox_p = PIXREGION_RECTS (reg); |
michael@0 | 2440 | box = *pbox_p; |
michael@0 | 2441 | box.y2 = pbox_p[numRects - 1].y2; |
michael@0 | 2442 | pbox_n = pbox_p + 1; |
michael@0 | 2443 | |
michael@0 | 2444 | for (i = numRects; --i > 0; pbox_p++, pbox_n++) |
michael@0 | 2445 | { |
michael@0 | 2446 | if ((pbox_n->x1 >= pbox_n->x2) || |
michael@0 | 2447 | (pbox_n->y1 >= pbox_n->y2)) |
michael@0 | 2448 | { |
michael@0 | 2449 | return FALSE; |
michael@0 | 2450 | } |
michael@0 | 2451 | |
michael@0 | 2452 | if (pbox_n->x1 < box.x1) |
michael@0 | 2453 | box.x1 = pbox_n->x1; |
michael@0 | 2454 | |
michael@0 | 2455 | if (pbox_n->x2 > box.x2) |
michael@0 | 2456 | box.x2 = pbox_n->x2; |
michael@0 | 2457 | |
michael@0 | 2458 | if ((pbox_n->y1 < pbox_p->y1) || |
michael@0 | 2459 | ((pbox_n->y1 == pbox_p->y1) && |
michael@0 | 2460 | ((pbox_n->x1 < pbox_p->x2) || (pbox_n->y2 != pbox_p->y2)))) |
michael@0 | 2461 | { |
michael@0 | 2462 | return FALSE; |
michael@0 | 2463 | } |
michael@0 | 2464 | } |
michael@0 | 2465 | |
michael@0 | 2466 | return ((box.x1 == reg->extents.x1) && |
michael@0 | 2467 | (box.x2 == reg->extents.x2) && |
michael@0 | 2468 | (box.y1 == reg->extents.y1) && |
michael@0 | 2469 | (box.y2 == reg->extents.y2)); |
michael@0 | 2470 | } |
michael@0 | 2471 | } |
michael@0 | 2472 | |
michael@0 | 2473 | PIXMAN_EXPORT pixman_bool_t |
michael@0 | 2474 | PREFIX (_init_rects) (region_type_t *region, |
michael@0 | 2475 | const box_type_t *boxes, int count) |
michael@0 | 2476 | { |
michael@0 | 2477 | box_type_t *rects; |
michael@0 | 2478 | int displacement; |
michael@0 | 2479 | int i; |
michael@0 | 2480 | |
michael@0 | 2481 | /* if it's 1, then we just want to set the extents, so call |
michael@0 | 2482 | * the existing method. */ |
michael@0 | 2483 | if (count == 1) |
michael@0 | 2484 | { |
michael@0 | 2485 | PREFIX (_init_rect) (region, |
michael@0 | 2486 | boxes[0].x1, |
michael@0 | 2487 | boxes[0].y1, |
michael@0 | 2488 | boxes[0].x2 - boxes[0].x1, |
michael@0 | 2489 | boxes[0].y2 - boxes[0].y1); |
michael@0 | 2490 | return TRUE; |
michael@0 | 2491 | } |
michael@0 | 2492 | |
michael@0 | 2493 | PREFIX (_init) (region); |
michael@0 | 2494 | |
michael@0 | 2495 | /* if it's 0, don't call pixman_rect_alloc -- 0 rectangles is |
michael@0 | 2496 | * a special case, and causing pixman_rect_alloc would cause |
michael@0 | 2497 | * us to leak memory (because the 0-rect case should be the |
michael@0 | 2498 | * static pixman_region_empty_data data). |
michael@0 | 2499 | */ |
michael@0 | 2500 | if (count == 0) |
michael@0 | 2501 | return TRUE; |
michael@0 | 2502 | |
michael@0 | 2503 | if (!pixman_rect_alloc (region, count)) |
michael@0 | 2504 | return FALSE; |
michael@0 | 2505 | |
michael@0 | 2506 | rects = PIXREGION_RECTS (region); |
michael@0 | 2507 | |
michael@0 | 2508 | /* Copy in the rects */ |
michael@0 | 2509 | memcpy (rects, boxes, sizeof(box_type_t) * count); |
michael@0 | 2510 | region->data->numRects = count; |
michael@0 | 2511 | |
michael@0 | 2512 | /* Eliminate empty and malformed rectangles */ |
michael@0 | 2513 | displacement = 0; |
michael@0 | 2514 | |
michael@0 | 2515 | for (i = 0; i < count; ++i) |
michael@0 | 2516 | { |
michael@0 | 2517 | box_type_t *box = &rects[i]; |
michael@0 | 2518 | |
michael@0 | 2519 | if (box->x1 >= box->x2 || box->y1 >= box->y2) |
michael@0 | 2520 | displacement++; |
michael@0 | 2521 | else if (displacement) |
michael@0 | 2522 | rects[i - displacement] = rects[i]; |
michael@0 | 2523 | } |
michael@0 | 2524 | |
michael@0 | 2525 | region->data->numRects -= displacement; |
michael@0 | 2526 | |
michael@0 | 2527 | /* If eliminating empty rectangles caused there |
michael@0 | 2528 | * to be only 0 or 1 rectangles, deal with that. |
michael@0 | 2529 | */ |
michael@0 | 2530 | if (region->data->numRects == 0) |
michael@0 | 2531 | { |
michael@0 | 2532 | FREE_DATA (region); |
michael@0 | 2533 | PREFIX (_init) (region); |
michael@0 | 2534 | |
michael@0 | 2535 | return TRUE; |
michael@0 | 2536 | } |
michael@0 | 2537 | |
michael@0 | 2538 | if (region->data->numRects == 1) |
michael@0 | 2539 | { |
michael@0 | 2540 | region->extents = rects[0]; |
michael@0 | 2541 | |
michael@0 | 2542 | FREE_DATA (region); |
michael@0 | 2543 | region->data = NULL; |
michael@0 | 2544 | |
michael@0 | 2545 | GOOD (region); |
michael@0 | 2546 | |
michael@0 | 2547 | return TRUE; |
michael@0 | 2548 | } |
michael@0 | 2549 | |
michael@0 | 2550 | /* Validate */ |
michael@0 | 2551 | region->extents.x1 = region->extents.x2 = 0; |
michael@0 | 2552 | |
michael@0 | 2553 | return validate (region); |
michael@0 | 2554 | } |
michael@0 | 2555 | |
michael@0 | 2556 | #define READ(_ptr) (*(_ptr)) |
michael@0 | 2557 | |
michael@0 | 2558 | static inline box_type_t * |
michael@0 | 2559 | bitmap_addrect (region_type_t *reg, |
michael@0 | 2560 | box_type_t *r, |
michael@0 | 2561 | box_type_t **first_rect, |
michael@0 | 2562 | int rx1, int ry1, |
michael@0 | 2563 | int rx2, int ry2) |
michael@0 | 2564 | { |
michael@0 | 2565 | if ((rx1 < rx2) && (ry1 < ry2) && |
michael@0 | 2566 | (!(reg->data->numRects && |
michael@0 | 2567 | ((r-1)->y1 == ry1) && ((r-1)->y2 == ry2) && |
michael@0 | 2568 | ((r-1)->x1 <= rx1) && ((r-1)->x2 >= rx2)))) |
michael@0 | 2569 | { |
michael@0 | 2570 | if (reg->data->numRects == reg->data->size) |
michael@0 | 2571 | { |
michael@0 | 2572 | if (!pixman_rect_alloc (reg, 1)) |
michael@0 | 2573 | return NULL; |
michael@0 | 2574 | *first_rect = PIXREGION_BOXPTR(reg); |
michael@0 | 2575 | r = *first_rect + reg->data->numRects; |
michael@0 | 2576 | } |
michael@0 | 2577 | r->x1 = rx1; |
michael@0 | 2578 | r->y1 = ry1; |
michael@0 | 2579 | r->x2 = rx2; |
michael@0 | 2580 | r->y2 = ry2; |
michael@0 | 2581 | reg->data->numRects++; |
michael@0 | 2582 | if (r->x1 < reg->extents.x1) |
michael@0 | 2583 | reg->extents.x1 = r->x1; |
michael@0 | 2584 | if (r->x2 > reg->extents.x2) |
michael@0 | 2585 | reg->extents.x2 = r->x2; |
michael@0 | 2586 | r++; |
michael@0 | 2587 | } |
michael@0 | 2588 | return r; |
michael@0 | 2589 | } |
michael@0 | 2590 | |
michael@0 | 2591 | /* Convert bitmap clip mask into clipping region. |
michael@0 | 2592 | * First, goes through each line and makes boxes by noting the transitions |
michael@0 | 2593 | * from 0 to 1 and 1 to 0. |
michael@0 | 2594 | * Then it coalesces the current line with the previous if they have boxes |
michael@0 | 2595 | * at the same X coordinates. |
michael@0 | 2596 | * Stride is in number of uint32_t per line. |
michael@0 | 2597 | */ |
michael@0 | 2598 | PIXMAN_EXPORT void |
michael@0 | 2599 | PREFIX (_init_from_image) (region_type_t *region, |
michael@0 | 2600 | pixman_image_t *image) |
michael@0 | 2601 | { |
michael@0 | 2602 | uint32_t mask0 = 0xffffffff & ~SCREEN_SHIFT_RIGHT(0xffffffff, 1); |
michael@0 | 2603 | box_type_t *first_rect, *rects, *prect_line_start; |
michael@0 | 2604 | box_type_t *old_rect, *new_rect; |
michael@0 | 2605 | uint32_t *pw, w, *pw_line, *pw_line_end; |
michael@0 | 2606 | int irect_prev_start, irect_line_start; |
michael@0 | 2607 | int h, base, rx1 = 0, crects; |
michael@0 | 2608 | int ib; |
michael@0 | 2609 | pixman_bool_t in_box, same; |
michael@0 | 2610 | int width, height, stride; |
michael@0 | 2611 | |
michael@0 | 2612 | PREFIX(_init) (region); |
michael@0 | 2613 | |
michael@0 | 2614 | critical_if_fail (region->data); |
michael@0 | 2615 | |
michael@0 | 2616 | return_if_fail (image->type == BITS); |
michael@0 | 2617 | return_if_fail (image->bits.format == PIXMAN_a1); |
michael@0 | 2618 | |
michael@0 | 2619 | pw_line = pixman_image_get_data (image); |
michael@0 | 2620 | width = pixman_image_get_width (image); |
michael@0 | 2621 | height = pixman_image_get_height (image); |
michael@0 | 2622 | stride = pixman_image_get_stride (image) / 4; |
michael@0 | 2623 | |
michael@0 | 2624 | first_rect = PIXREGION_BOXPTR(region); |
michael@0 | 2625 | rects = first_rect; |
michael@0 | 2626 | |
michael@0 | 2627 | region->extents.x1 = width - 1; |
michael@0 | 2628 | region->extents.x2 = 0; |
michael@0 | 2629 | irect_prev_start = -1; |
michael@0 | 2630 | for (h = 0; h < height; h++) |
michael@0 | 2631 | { |
michael@0 | 2632 | pw = pw_line; |
michael@0 | 2633 | pw_line += stride; |
michael@0 | 2634 | irect_line_start = rects - first_rect; |
michael@0 | 2635 | |
michael@0 | 2636 | /* If the Screen left most bit of the word is set, we're starting in |
michael@0 | 2637 | * a box */ |
michael@0 | 2638 | if (READ(pw) & mask0) |
michael@0 | 2639 | { |
michael@0 | 2640 | in_box = TRUE; |
michael@0 | 2641 | rx1 = 0; |
michael@0 | 2642 | } |
michael@0 | 2643 | else |
michael@0 | 2644 | { |
michael@0 | 2645 | in_box = FALSE; |
michael@0 | 2646 | } |
michael@0 | 2647 | |
michael@0 | 2648 | /* Process all words which are fully in the pixmap */ |
michael@0 | 2649 | pw_line_end = pw + (width >> 5); |
michael@0 | 2650 | for (base = 0; pw < pw_line_end; base += 32) |
michael@0 | 2651 | { |
michael@0 | 2652 | w = READ(pw++); |
michael@0 | 2653 | if (in_box) |
michael@0 | 2654 | { |
michael@0 | 2655 | if (!~w) |
michael@0 | 2656 | continue; |
michael@0 | 2657 | } |
michael@0 | 2658 | else |
michael@0 | 2659 | { |
michael@0 | 2660 | if (!w) |
michael@0 | 2661 | continue; |
michael@0 | 2662 | } |
michael@0 | 2663 | for (ib = 0; ib < 32; ib++) |
michael@0 | 2664 | { |
michael@0 | 2665 | /* If the Screen left most bit of the word is set, we're |
michael@0 | 2666 | * starting a box */ |
michael@0 | 2667 | if (w & mask0) |
michael@0 | 2668 | { |
michael@0 | 2669 | if (!in_box) |
michael@0 | 2670 | { |
michael@0 | 2671 | rx1 = base + ib; |
michael@0 | 2672 | /* start new box */ |
michael@0 | 2673 | in_box = TRUE; |
michael@0 | 2674 | } |
michael@0 | 2675 | } |
michael@0 | 2676 | else |
michael@0 | 2677 | { |
michael@0 | 2678 | if (in_box) |
michael@0 | 2679 | { |
michael@0 | 2680 | /* end box */ |
michael@0 | 2681 | rects = bitmap_addrect (region, rects, &first_rect, |
michael@0 | 2682 | rx1, h, base + ib, h + 1); |
michael@0 | 2683 | if (rects == NULL) |
michael@0 | 2684 | goto error; |
michael@0 | 2685 | in_box = FALSE; |
michael@0 | 2686 | } |
michael@0 | 2687 | } |
michael@0 | 2688 | /* Shift the word VISUALLY left one. */ |
michael@0 | 2689 | w = SCREEN_SHIFT_LEFT(w, 1); |
michael@0 | 2690 | } |
michael@0 | 2691 | } |
michael@0 | 2692 | |
michael@0 | 2693 | if (width & 31) |
michael@0 | 2694 | { |
michael@0 | 2695 | /* Process final partial word on line */ |
michael@0 | 2696 | w = READ(pw++); |
michael@0 | 2697 | for (ib = 0; ib < (width & 31); ib++) |
michael@0 | 2698 | { |
michael@0 | 2699 | /* If the Screen left most bit of the word is set, we're |
michael@0 | 2700 | * starting a box */ |
michael@0 | 2701 | if (w & mask0) |
michael@0 | 2702 | { |
michael@0 | 2703 | if (!in_box) |
michael@0 | 2704 | { |
michael@0 | 2705 | rx1 = base + ib; |
michael@0 | 2706 | /* start new box */ |
michael@0 | 2707 | in_box = TRUE; |
michael@0 | 2708 | } |
michael@0 | 2709 | } |
michael@0 | 2710 | else |
michael@0 | 2711 | { |
michael@0 | 2712 | if (in_box) |
michael@0 | 2713 | { |
michael@0 | 2714 | /* end box */ |
michael@0 | 2715 | rects = bitmap_addrect(region, rects, &first_rect, |
michael@0 | 2716 | rx1, h, base + ib, h + 1); |
michael@0 | 2717 | if (rects == NULL) |
michael@0 | 2718 | goto error; |
michael@0 | 2719 | in_box = FALSE; |
michael@0 | 2720 | } |
michael@0 | 2721 | } |
michael@0 | 2722 | /* Shift the word VISUALLY left one. */ |
michael@0 | 2723 | w = SCREEN_SHIFT_LEFT(w, 1); |
michael@0 | 2724 | } |
michael@0 | 2725 | } |
michael@0 | 2726 | /* If scanline ended with last bit set, end the box */ |
michael@0 | 2727 | if (in_box) |
michael@0 | 2728 | { |
michael@0 | 2729 | rects = bitmap_addrect(region, rects, &first_rect, |
michael@0 | 2730 | rx1, h, base + (width & 31), h + 1); |
michael@0 | 2731 | if (rects == NULL) |
michael@0 | 2732 | goto error; |
michael@0 | 2733 | } |
michael@0 | 2734 | /* if all rectangles on this line have the same x-coords as |
michael@0 | 2735 | * those on the previous line, then add 1 to all the previous y2s and |
michael@0 | 2736 | * throw away all the rectangles from this line |
michael@0 | 2737 | */ |
michael@0 | 2738 | same = FALSE; |
michael@0 | 2739 | if (irect_prev_start != -1) |
michael@0 | 2740 | { |
michael@0 | 2741 | crects = irect_line_start - irect_prev_start; |
michael@0 | 2742 | if (crects != 0 && |
michael@0 | 2743 | crects == ((rects - first_rect) - irect_line_start)) |
michael@0 | 2744 | { |
michael@0 | 2745 | old_rect = first_rect + irect_prev_start; |
michael@0 | 2746 | new_rect = prect_line_start = first_rect + irect_line_start; |
michael@0 | 2747 | same = TRUE; |
michael@0 | 2748 | while (old_rect < prect_line_start) |
michael@0 | 2749 | { |
michael@0 | 2750 | if ((old_rect->x1 != new_rect->x1) || |
michael@0 | 2751 | (old_rect->x2 != new_rect->x2)) |
michael@0 | 2752 | { |
michael@0 | 2753 | same = FALSE; |
michael@0 | 2754 | break; |
michael@0 | 2755 | } |
michael@0 | 2756 | old_rect++; |
michael@0 | 2757 | new_rect++; |
michael@0 | 2758 | } |
michael@0 | 2759 | if (same) |
michael@0 | 2760 | { |
michael@0 | 2761 | old_rect = first_rect + irect_prev_start; |
michael@0 | 2762 | while (old_rect < prect_line_start) |
michael@0 | 2763 | { |
michael@0 | 2764 | old_rect->y2 += 1; |
michael@0 | 2765 | old_rect++; |
michael@0 | 2766 | } |
michael@0 | 2767 | rects -= crects; |
michael@0 | 2768 | region->data->numRects -= crects; |
michael@0 | 2769 | } |
michael@0 | 2770 | } |
michael@0 | 2771 | } |
michael@0 | 2772 | if(!same) |
michael@0 | 2773 | irect_prev_start = irect_line_start; |
michael@0 | 2774 | } |
michael@0 | 2775 | if (!region->data->numRects) |
michael@0 | 2776 | { |
michael@0 | 2777 | region->extents.x1 = region->extents.x2 = 0; |
michael@0 | 2778 | } |
michael@0 | 2779 | else |
michael@0 | 2780 | { |
michael@0 | 2781 | region->extents.y1 = PIXREGION_BOXPTR(region)->y1; |
michael@0 | 2782 | region->extents.y2 = PIXREGION_END(region)->y2; |
michael@0 | 2783 | if (region->data->numRects == 1) |
michael@0 | 2784 | { |
michael@0 | 2785 | free (region->data); |
michael@0 | 2786 | region->data = NULL; |
michael@0 | 2787 | } |
michael@0 | 2788 | } |
michael@0 | 2789 | |
michael@0 | 2790 | error: |
michael@0 | 2791 | return; |
michael@0 | 2792 | } |