region.c 60.2 KB
Newer Older
B
bernard.xiong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/*
 * File      : region.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rt-thread.org/license/LICENSE
 *
 * Change Logs:
 * Date           Author       Notes
 * 2009-10-16     Bernard      first version
 */
#include <rtgui/region.h>
#include <rtgui/rtgui_system.h>

/* #define good(reg) RT_ASSERT(rtgui_region16_valid(reg)) */
#define good(reg)

#define RTGUI_MIN(a,b) ((a) < (b) ? (a) : (b))
#define RTGUI_MAX(a,b) ((a) > (b) ? (a) : (b))

#define RTGUI_SHRT_MIN	(-32767-1)
#define RTGUI_SHRT_MAX	32767

#define PIXREGION_NIL(reg) ((reg)->data && !(reg)->data->numRects)

/* not a region */
#define PIXREGION_NAR(reg)	((reg)->data == &rtgui_brokendata)
#define PIXREGION_NUM_RECTS(reg) ((reg)->data ? (reg)->data->numRects : 1)
#define PIXREGION_SIZE(reg) ((reg)->data ? (reg)->data->size : 0)
#define PIXREGION_RECTS(reg) ((reg)->data ? (rtgui_rect_t *)((reg)->data + 1) \
			               : &(reg)->extents)
#define PIXREGION_BOXPTR(reg) ((rtgui_rect_t *)((reg)->data + 1))
#define PIXREGION_BOX(reg,i) (&PIXREGION_BOXPTR(reg)[i])
#define PIXREGION_TOP(reg) PIXREGION_BOX(reg, (reg)->data->numRects)
#define PIXREGION_END(reg) PIXREGION_BOX(reg, (reg)->data->numRects - 1)
#define PIXREGION_SZOF(n) (sizeof(rtgui_region_data_t) + ((n) * sizeof(rtgui_rect_t)))

rtgui_rect_t rtgui_empty_rect = {0, 0, 0, 0};
41 42
rtgui_point_t rtgui_empty_point = {0, 0};

B
bernard.xiong 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
static rtgui_region_data_t rtgui_region_emptydata = {0, 0};
static rtgui_region_data_t  rtgui_brokendata = {0, 0};

static rtgui_region_status_t rtgui_break(rtgui_region_t *pReg);

/*
 * The functions in this file implement the Region abstraction used extensively
 * throughout the X11 sample server. A Region is simply a set of disjoint
 * (non-overlapping) rectangles, plus an "extent" rectangle which is the
 * smallest single rectangle that contains all the non-overlapping rectangles.
 *
 * A Region is implemented as a "y-x-banded" array of rectangles.  This array
 * imposes two degrees of order.  First, all rectangles are sorted by top side
 * y coordinate first (y1), and then by left side x coordinate (x1).
 *
 * Furthermore, the rectangles are grouped into "bands".  Each rectangle in a
 * band has the same top y coordinate (y1), and each has the same bottom y
 * coordinate (y2).  Thus all rectangles in a band differ only in their left
 * and right side (x1 and x2).  Bands are implicit in the array of rectangles:
 * there is no separate list of band start pointers.
 *
 * The y-x band representation does not RTGUI_MINimize rectangles.  In particular,
 * if a rectangle vertically crosses a band (the rectangle has scanlines in
 * the y1 to y2 area spanned by the band), then the rectangle may be broken
 * down into two or more smaller rectangles stacked one atop the other.
 *
 *  -----------				                -----------
 *  |         |				                |         |		    band 0
 *  |         |  --------		            -----------  --------
 *  |         |  |      |  in y-x banded    |         |  |      |   band 1
 *  |         |  |      |  form is	        |         |  |      |
 *  -----------  |      |		            -----------  --------
 *               |      |				    |      |   band 2
 *               --------				    --------
 *
 * An added constraint on the rectangles is that they must cover as much
 * horizontal area as possible: no two rectangles within a band are allowed
 * to touch.
 *
 * Whenever possible, bands will be merged together to cover a greater vertical
 * distance (and thus reduce the number of rectangles). Two bands can be merged
 * only if the bottom of one touches the top of the other and they have
 * rectangles in the same places (of the same width, of course).
 *
 * Adam de Boor wrote most of the original region code.  Joel McCormack
 * substantially modified or rewrote most of the core arithmetic routines, and
 * added rtgui_region_validate in order to support several speed improvements to
 * rtgui_region_validateTree.  Bob Scheifler changed the representation to be more
 * compact when empty or a single rectangle, and did a bunch of gratuitous
 * reformatting. Carl Worth did further gratuitous reformatting while re-merging
 * the server and client region code into libpixregion.
 */

/*  true iff two Boxes overlap */
#define EXTENTCHECK(r1,r2) \
      (!( ((r1)->x2 <= (r2)->x1)  || \
          ((r1)->x1 >= (r2)->x2)  || \
          ((r1)->y2 <= (r2)->y1)  || \
          ((r1)->y1 >= (r2)->y2) ) )

/* true iff (x,y) is in Box */
#define INBOX(r,x,y) \
      ( ((r)->x2 > (x)) && \
        ((r)->x1 <= (x)) && \
        ((r)->y2 > (y)) && \
        ((r)->y1 <= (y)) )

/* true iff Box r1 contains Box r2 */
#define SUBSUMES(r1,r2) \
      ( ((r1)->x1 <= (r2)->x1) && \
        ((r1)->x2 >= (r2)->x2) && \
        ((r1)->y1 <= (r2)->y1) && \
        ((r1)->y2 >= (r2)->y2) )

117 118 119 120 121 122 123
/* true iff Box r1 and Box r2 constitute cross */
#define CROSS(r1,r2) \
	  ( ((r1)->x1 <= (r2)->x1) && \
        ((r1)->x2 >= (r2)->x2) && \
        ((r1)->y1 >= (r2)->y1) && \
        ((r1)->y2 <= (r2)->y2) )

B
bernard.xiong 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
#define allocData(n) rtgui_malloc(PIXREGION_SZOF(n))
#define freeData(reg) if ((reg)->data && (reg)->data->size) rtgui_free((reg)->data)

#define RECTALLOC_BAIL(pReg,n,bail) \
if (!(pReg)->data || (((pReg)->data->numRects + (n)) > (pReg)->data->size)) \
    if (!rtgui_rect_alloc(pReg, n)) { goto bail; }

#define RECTALLOC(pReg,n) \
if (!(pReg)->data || (((pReg)->data->numRects + (n)) > (pReg)->data->size)) \
    if (!rtgui_rect_alloc(pReg, n)) { return RTGUI_REGION_STATUS_FAILURE; }

#define ADDRECT(pNextRect,nx1,ny1,nx2,ny2)	\
{						\
    pNextRect->x1 = nx1;			\
    pNextRect->y1 = ny1;			\
    pNextRect->x2 = nx2;			\
    pNextRect->y2 = ny2;			\
    pNextRect++;				\
}

#define NEWRECT(pReg,pNextRect,nx1,ny1,nx2,ny2)			\
{									\
    if (!(pReg)->data || ((pReg)->data->numRects == (pReg)->data->size))\
    {									\
	if (!rtgui_rect_alloc(pReg, 1))					\
	    return RTGUI_REGION_STATUS_FAILURE;						\
	pNextRect = PIXREGION_TOP(pReg);					\
    }									\
    ADDRECT(pNextRect,nx1,ny1,nx2,ny2);					\
    pReg->data->numRects++;						\
    RT_ASSERT(pReg->data->numRects<=pReg->data->size);			\
}

#define DOWNSIZE(reg,numRects)						 \
if (((numRects) < ((reg)->data->size >> 1)) && ((reg)->data->size > 50)) \
{									 \
    rtgui_region_data_t * NewData;							 \
    NewData = (rtgui_region_data_t *)rt_realloc((reg)->data, PIXREGION_SZOF(numRects));	 \
    if (NewData)							 \
    {									 \
	NewData->size = (numRects);					 \
	(reg)->data = NewData;						 \
    }									 \
}

void rtgui_region_init(rtgui_region_t *region)
{
	region->extents = rtgui_empty_rect;
	region->data = &rtgui_region_emptydata;
}

void rtgui_region_init_rect(rtgui_region_t *region,
	int x, int y, unsigned int width, unsigned int height)
{
	region->extents.x1 = x;
	region->extents.y1 = y;
	region->extents.x2 = x + width;
	region->extents.y2 = y + height;
	region->data = RT_NULL;
}

B
bernard.xiong@gmail.com 已提交
185
void rtgui_region_init_with_extents(rtgui_region_t *region, const rtgui_rect_t *extents)
B
bernard.xiong 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137
{
	region->extents = *extents;
	region->data = RT_NULL;
}

void rtgui_region_fini (rtgui_region_t *region)
{
	good (region);
	freeData (region);
}

int rtgui_region_num_rects (rtgui_region_t *region)
{
	return PIXREGION_NUM_RECTS (region);
}

rtgui_rect_t *rtgui_region_rects (rtgui_region_t *region)
{
	return PIXREGION_RECTS (region);
}

static rtgui_region_status_t rtgui_break (rtgui_region_t *region)
{
	freeData (region);
	region->extents = rtgui_empty_rect;
	region->data = &rtgui_brokendata;
	return RTGUI_REGION_STATUS_FAILURE;
}

static rtgui_region_status_t rtgui_rect_alloc(rtgui_region_t * region, int n)
{
	rtgui_region_data_t *data;

	if (!region->data)
	{
		n++;
		region->data = allocData(n);
		if (!region->data) return rtgui_break (region);
		region->data->numRects = 1;
		*PIXREGION_BOXPTR(region) = region->extents;
	}
	else if (!region->data->size)
	{
		region->data = allocData(n);
		if (!region->data) return rtgui_break (region);
		region->data->numRects = 0;
	}
	else
	{
		if (n == 1)
		{
			n = region->data->numRects;
			if (n > 500) /* XXX pick numbers out of a hat */
				n = 250;
		}
		n += region->data->numRects;
		data = (rtgui_region_data_t *)rt_realloc(region->data, PIXREGION_SZOF(n));
		if (!data) return rtgui_break (region);
		region->data = data;
	}
	region->data->size = n;
	return RTGUI_REGION_STATUS_SUCCESS;
}

rtgui_region_status_t rtgui_region_copy(rtgui_region_t *dst, rtgui_region_t *src)
{
	good(dst);
	good(src);
	if (dst == src)
		return RTGUI_REGION_STATUS_SUCCESS;
	dst->extents = src->extents;
	if (!src->data || !src->data->size)
	{
		freeData(dst);
		dst->data = src->data;
		return RTGUI_REGION_STATUS_SUCCESS;
	}
	if (!dst->data || (dst->data->size < src->data->numRects))
	{
		freeData(dst);
		dst->data = allocData(src->data->numRects);
		if (!dst->data) return rtgui_break (dst);
		dst->data->size = src->data->numRects;
	}
	dst->data->numRects = src->data->numRects;
	rt_memmove((char *)PIXREGION_BOXPTR(dst),(char *)PIXREGION_BOXPTR(src),
			dst->data->numRects * sizeof(rtgui_rect_t));
	return RTGUI_REGION_STATUS_SUCCESS;
}

/*======================================================================
 *	    Generic Region Operator
 *====================================================================*/

/*-
 *-----------------------------------------------------------------------
 * rtgui_coalesce --
 *	Attempt to merge the boxes in the current band with those in the
 *	previous one.  We are guaranteed that the current band extends to
 *      the end of the rects array.  Used only by rtgui_op.
 *
 * Results:
 *	The new index for the previous band.
 *
 * Side Effects:
 *	If coalescing takes place:
 *	    - rectangles in the previous band will have their y2 fields
 *	      altered.
 *	    - region->data->numRects will be decreased.
 *
 *-----------------------------------------------------------------------
 */
rt_inline int
rtgui_coalesce (
	rtgui_region_t *region,	    	/* Region to coalesce		     */
	int	    	  	prevStart,  	/* Index of start of previous band   */
	int	    	  	curStart)   	/* Index of start of current band    */
{
	rtgui_rect_t *	pPrevBox;   	/* Current box in previous band	     */
	rtgui_rect_t *	pCurBox;    	/* Current box in current band       */
	int  	numRects;	/* Number rectangles in both bands   */
	int	y2;		/* Bottom of current band	     */
	/*
	 * Figure out how many rectangles are in the band.
	 */
	numRects = curStart - prevStart;
	RT_ASSERT(numRects == region->data->numRects - curStart);

	if (!numRects) return curStart;

	/*
	 * The bands may only be coalesced if the bottom of the previous
	 * matches the top scanline of the current.
	 */
	pPrevBox = PIXREGION_BOX(region, prevStart);
	pCurBox = PIXREGION_BOX(region, curStart);
	if (pPrevBox->y2 != pCurBox->y1) return curStart;

	/*
	 * Make sure the bands have boxes in the same places. This
	 * assumes that boxes have been added in such a way that they
	 * cover the most area possible. I.e. two boxes in a band must
	 * have some horizontal space between them.
	 */
	y2 = pCurBox->y2;

	do
	{
		if ((pPrevBox->x1 != pCurBox->x1) || (pPrevBox->x2 != pCurBox->x2))
		{
			return (curStart);
		}
		pPrevBox++;
		pCurBox++;
		numRects--;
	}
	while (numRects);

	/*
	 * The bands may be merged, so set the bottom y of each box
	 * in the previous band to the bottom y of the current band.
	 */
	numRects = curStart - prevStart;
	region->data->numRects -= numRects;
	do
	{
		pPrevBox--;
		pPrevBox->y2 = y2;
		numRects--;
	}
	while (numRects);
	return prevStart;
}

/* Quicky macro to avoid trivial reject procedure calls to rtgui_coalesce */

#define Coalesce(newReg, prevBand, curBand)				\
    if (curBand - prevBand == newReg->data->numRects - curBand) {	\
	prevBand = rtgui_coalesce(newReg, prevBand, curBand);		\
    } else {								\
	prevBand = curBand;						\
    }

/*-
 *-----------------------------------------------------------------------
 * rtgui_region_appendNonO --
 *	Handle a non-overlapping band for the union and subtract operations.
 *      Just adds the (top/bottom-clipped) rectangles into the region.
 *      Doesn't have to check for subsumption or anything.
 *
 * Results:
 *	None.
 *
 * Side Effects:
 *	region->data->numRects is incremented and the rectangles overwritten
 *	with the rectangles we're passed.
 *
 *-----------------------------------------------------------------------
 */

rt_inline rtgui_region_status_t
rtgui_region_appendNonO (
	rtgui_region_t *region,
	rtgui_rect_t *r,
	rtgui_rect_t *rEnd,
	int  	y1,
	int  	y2)
{
	rtgui_rect_t *	pNextRect;
	int	newRects;

	newRects = rEnd - r;

	RT_ASSERT(y1 < y2);
	RT_ASSERT(newRects != 0);

	/* Make sure we have enough space for all rectangles to be added */
	RECTALLOC(region, newRects);
	pNextRect = PIXREGION_TOP(region);
	region->data->numRects += newRects;
	do
	{
		RT_ASSERT(r->x1 < r->x2);
		ADDRECT(pNextRect, r->x1, y1, r->x2, y2);
		r++;
	}
	while (r != rEnd);

	return RTGUI_REGION_STATUS_SUCCESS;
}

#define FindBand(r, rBandEnd, rEnd, ry1)	\
{											\
    ry1 = r->y1;							\
    rBandEnd = r+1;							\
    while ((rBandEnd != rEnd) && (rBandEnd->y1 == ry1)) {   \
	rBandEnd++;								\
    }										\
}

#define	AppendRegions(newReg, r, rEnd)		\
{											\
    int newRects = rEnd - r;				\
    if (newRects) {							\
	RECTALLOC(newReg, newRects);			\
	rt_memmove((char *)PIXREGION_TOP(newReg),(char *)r,		\
              newRects * sizeof(rtgui_rect_t));				\
	newReg->data->numRects += newRects;		\
    }										\
}

/*-
 *-----------------------------------------------------------------------
 * rtgui_op --
 *	Apply an operation to two regions. Called by rtgui_region_union, rtgui_region_inverse,
 *	rtgui_region_subtract, rtgui_region_intersect....  Both regions MUST have at least one
 *      rectangle, and cannot be the same object.
 *
 * Results:
 *	RTGUI_REGION_STATUS_SUCCESS if successful.
 *
 * Side Effects:
 *	The new region is overwritten.
 *	pOverlap set to RTGUI_REGION_STATUS_SUCCESS if overlapFunc ever returns RTGUI_REGION_STATUS_SUCCESS.
 *
 * Notes:
 *	The idea behind this function is to view the two regions as sets.
 *	Together they cover a rectangle of area that this function divides
 *	into horizontal bands where points are covered only by one region
 *	or by both. For the first case, the nonOverlapFunc is called with
 *	each the band and the band's upper and lower extents. For the
 *	second, the overlapFunc is called to process the entire band. It
 *	is responsible for clipping the rectangles in the band, though
 *	this function provides the boundaries.
 *	At the end of each band, the new region is coalesced, if possible,
 *	to reduce the number of rectangles in the region.
 *
 *-----------------------------------------------------------------------
 */

typedef rtgui_region_status_t (*OverlapProcPtr)(
	rtgui_region_t	 *region,
	rtgui_rect_t *r1,
	rtgui_rect_t *r1End,
	rtgui_rect_t *r2,
	rtgui_rect_t *r2End,
	short    	 y1,
	short    	 y2,
	int		 *pOverlap);

static rtgui_region_status_t
rtgui_op(
	rtgui_region_t *newReg,		    /* Place to store result	     */
	rtgui_region_t *reg1,		    /* First region in operation     */
	rtgui_region_t *reg2,		    /* 2d region in operation        */
	OverlapProcPtr  overlapFunc,	/* Function to call for over-
									 * lapping bands		     */
	int	    appendNon1,				/* Append non-overlapping bands  */
	/* in region 1 ? */
	int	    appendNon2,				/* Append non-overlapping bands  */
	/* in region 2 ? */
	int	    *pOverlap)
{
	rtgui_rect_t * r1;			    /* Pointer into first region     */
	rtgui_rect_t * r2;			    /* Pointer into 2d region	     */
	rtgui_rect_t *	    r1End;		    /* End of 1st region	     */
	rtgui_rect_t *	    r2End;		    /* End of 2d region		     */
	short	    ybot;		    /* Bottom of intersection	     */
	short	    ytop;		    /* Top of intersection	     */
	rtgui_region_data_t *	    oldData;		    /* Old data for newReg	     */
	int		    prevBand;		    /* Index of start of
						     * previous band in newReg       */
	int		    curBand;		    /* Index of start of current
						     * band in newReg		     */
	rtgui_rect_t * r1BandEnd;		    /* End of current band in r1     */
	rtgui_rect_t * r2BandEnd;		    /* End of current band in r2     */
	short	    top;		    /* Top of non-overlapping band   */
	short	    bot;		    /* Bottom of non-overlapping band*/
	int    r1y1;		    /* Temps for r1->y1 and r2->y1   */
	int    r2y1;
	int		    newSize;
	int		    numRects;

	/*
	 * Break any region computed from a broken region
	 */
	if (PIXREGION_NAR (reg1) || PIXREGION_NAR(reg2))
		return rtgui_break (newReg);

	/*
	 * Initialization:
	 *	set r1, r2, r1End and r2End appropriately, save the rectangles
	 * of the destination region until the end in case it's one of
	 * the two source regions, then mark the "new" region empty, allocating
	 * another array of rectangles for it to use.
	 */

	r1 = PIXREGION_RECTS(reg1);
	newSize = PIXREGION_NUM_RECTS(reg1);
	r1End = r1 + newSize;
	numRects = PIXREGION_NUM_RECTS(reg2);
	r2 = PIXREGION_RECTS(reg2);
	r2End = r2 + numRects;
	RT_ASSERT(r1 != r1End);
	RT_ASSERT(r2 != r2End);

	oldData = (rtgui_region_data_t *)RT_NULL;
	if (((newReg == reg1) && (newSize > 1)) ||
			((newReg == reg2) && (numRects > 1)))
	{
		oldData = newReg->data;
		newReg->data = &rtgui_region_emptydata;
	}
	/* guess at new size */
	if (numRects > newSize)
		newSize = numRects;
	newSize <<= 1;
	if (!newReg->data)
		newReg->data = &rtgui_region_emptydata;
	else if (newReg->data->size)
		newReg->data->numRects = 0;
	if (newSize > newReg->data->size)
		if (!rtgui_rect_alloc(newReg, newSize))
			return RTGUI_REGION_STATUS_FAILURE;

	/*
	 * Initialize ybot.
	 * In the upcoRTGUI_MINg loop, ybot and ytop serve different functions depending
	 * on whether the band being handled is an overlapping or non-overlapping
	 * band.
	 * 	In the case of a non-overlapping band (only one of the regions
	 * has points in the band), ybot is the bottom of the most recent
	 * intersection and thus clips the top of the rectangles in that band.
	 * ytop is the top of the next intersection between the two regions and
	 * serves to clip the bottom of the rectangles in the current band.
	 *	For an overlapping band (where the two regions intersect), ytop clips
	 * the top of the rectangles of both regions and ybot clips the bottoms.
	 */

	ybot = RTGUI_MIN(r1->y1, r2->y1);

	/*
	 * prevBand serves to mark the start of the previous band so rectangles
	 * can be coalesced into larger rectangles. qv. rtgui_coalesce, above.
	 * In the beginning, there is no previous band, so prevBand == curBand
	 * (curBand is set later on, of course, but the first band will always
	 * start at index 0). prevBand and curBand must be indices because of
	 * the possible expansion, and resultant moving, of the new region's
	 * array of rectangles.
	 */
	prevBand = 0;

	do
	{
		/*
		 * This algorithm proceeds one source-band (as opposed to a
		 * destination band, which is deterRTGUI_MINed by where the two regions
		 * intersect) at a time. r1BandEnd and r2BandEnd serve to mark the
		 * rectangle after the last one in the current band for their
		 * respective regions.
		 */
		RT_ASSERT(r1 != r1End);
		RT_ASSERT(r2 != r2End);

		FindBand(r1, r1BandEnd, r1End, r1y1);
		FindBand(r2, r2BandEnd, r2End, r2y1);

		/*
		 * First handle the band that doesn't intersect, if any.
		 *
		 * Note that attention is restricted to one band in the
		 * non-intersecting region at once, so if a region has n
		 * bands between the current position and the next place it overlaps
		 * the other, this entire loop will be passed through n times.
		 */
		if (r1y1 < r2y1)
		{
			if (appendNon1)
			{
				top = RTGUI_MAX(r1y1, ybot);
				bot = RTGUI_MIN(r1->y2, r2y1);
				if (top != bot)
				{
					curBand = newReg->data->numRects;
					rtgui_region_appendNonO(newReg, r1, r1BandEnd, top, bot);
					Coalesce(newReg, prevBand, curBand);
				}
			}
			ytop = r2y1;
		}
		else if (r2y1 < r1y1)
		{
			if (appendNon2)
			{
				top = RTGUI_MAX(r2y1, ybot);
				bot = RTGUI_MIN(r2->y2, r1y1);
				if (top != bot)
				{
					curBand = newReg->data->numRects;
					rtgui_region_appendNonO(newReg, r2, r2BandEnd, top, bot);
					Coalesce(newReg, prevBand, curBand);
				}
			}
			ytop = r1y1;
		}
		else
		{
			ytop = r1y1;
		}

		/*
		 * Now see if we've hit an intersecting band. The two bands only
		 * intersect if ybot > ytop
		 */
		ybot = RTGUI_MIN(r1->y2, r2->y2);
		if (ybot > ytop)
		{
			curBand = newReg->data->numRects;
			(* overlapFunc)(newReg, r1, r1BandEnd, r2, r2BandEnd, ytop, ybot,
							pOverlap);
			Coalesce(newReg, prevBand, curBand);
		}

		/*
		 * If we've finished with a band (y2 == ybot) we skip forward
		 * in the region to the next band.
		 */
		if (r1->y2 == ybot) r1 = r1BandEnd;
		if (r2->y2 == ybot) r2 = r2BandEnd;

	}
	while (r1 != r1End && r2 != r2End);

	/*
	 * Deal with whichever region (if any) still has rectangles left.
	 *
	 * We only need to worry about banding and coalescing for the very first
	 * band left.  After that, we can just group all remaining boxes,
	 * regardless of how many bands, into one final append to the list.
	 */

	if ((r1 != r1End) && appendNon1)
	{
		/* Do first nonOverlap1Func call, which may be able to coalesce */
		FindBand(r1, r1BandEnd, r1End, r1y1);
		curBand = newReg->data->numRects;
		rtgui_region_appendNonO(newReg, r1, r1BandEnd, RTGUI_MAX(r1y1, ybot), r1->y2);
		Coalesce(newReg, prevBand, curBand);
		/* Just append the rest of the boxes  */
		AppendRegions(newReg, r1BandEnd, r1End);
	}
	else if ((r2 != r2End) && appendNon2)
	{
		/* Do first nonOverlap2Func call, which may be able to coalesce */
		FindBand(r2, r2BandEnd, r2End, r2y1);
		curBand = newReg->data->numRects;
		rtgui_region_appendNonO(newReg, r2, r2BandEnd, RTGUI_MAX(r2y1, ybot), r2->y2);
		Coalesce(newReg, prevBand, curBand);
		/* Append rest of boxes */
		AppendRegions(newReg, r2BandEnd, r2End);
	}

	if (oldData)
		rtgui_free(oldData);

	numRects = newReg->data->numRects;
	if (!numRects)
	{
		freeData(newReg);
		newReg->data = &rtgui_region_emptydata;
	}
	else if (numRects == 1)
	{
		newReg->extents = *PIXREGION_BOXPTR(newReg);
		freeData(newReg);
		newReg->data = (rtgui_region_data_t *)RT_NULL;
	}
	else
	{
		DOWNSIZE(newReg, numRects);
	}

	return RTGUI_REGION_STATUS_SUCCESS;
}

/*-
 *-----------------------------------------------------------------------
 * rtgui_set_extents --
 *	Reset the extents of a region to what they should be. Called by
 *	rtgui_region_subtract and rtgui_region_intersect as they can't figure it out along the
 *	way or do so easily, as rtgui_region_union can.
 *
 * Results:
 *	None.
 *
 * Side Effects:
 *	The region's 'extents' structure is overwritten.
 *
 *-----------------------------------------------------------------------
 */
static void
rtgui_set_extents (rtgui_region_t *region)
{
	rtgui_rect_t *box, *boxEnd;

	if (!region->data)
		return;
	if (!region->data->size)
	{
		region->extents.x2 = region->extents.x1;
		region->extents.y2 = region->extents.y1;
		return;
	}

	box = PIXREGION_BOXPTR(region);
	boxEnd = PIXREGION_END(region);

	/*
	 * Since box is the first rectangle in the region, it must have the
	 * smallest y1 and since boxEnd is the last rectangle in the region,
	 * it must have the largest y2, because of banding. Initialize x1 and
	 * x2 from  box and boxEnd, resp., as good things to initialize them
	 * to...
	 */
	region->extents.x1 = box->x1;
	region->extents.y1 = box->y1;
	region->extents.x2 = boxEnd->x2;
	region->extents.y2 = boxEnd->y2;

	RT_ASSERT(region->extents.y1 < region->extents.y2);
	while (box <= boxEnd)
	{
		if (box->x1 < region->extents.x1)
			region->extents.x1 = box->x1;
		if (box->x2 > region->extents.x2)
			region->extents.x2 = box->x2;
		box++;
	};

	RT_ASSERT(region->extents.x1 < region->extents.x2);
}

/*======================================================================
 *	    Region Intersection
 *====================================================================*/
/*-
 *-----------------------------------------------------------------------
 * rtgui_region_intersectO --
 *	Handle an overlapping band for rtgui_region_intersect.
 *
 * Results:
 *	RTGUI_REGION_STATUS_SUCCESS if successful.
 *
 * Side Effects:
 *	Rectangles may be added to the region.
 *
 *-----------------------------------------------------------------------
 */
/*ARGSUSED*/
static rtgui_region_status_t
rtgui_region_intersectO (
	rtgui_region_t *region,
	rtgui_rect_t *r1,
	rtgui_rect_t *r1End,
	rtgui_rect_t *r2,
	rtgui_rect_t *r2End,
	short		y1,
	short		y2,
	int			*pOverlap)
{
	int  	x1;
	int  	x2;
	rtgui_rect_t *	pNextRect;

	pNextRect = PIXREGION_TOP(region);

	RT_ASSERT(y1 < y2);
	RT_ASSERT(r1 != r1End && r2 != r2End);

	do
	{
		x1 = RTGUI_MAX(r1->x1, r2->x1);
		x2 = RTGUI_MIN(r1->x2, r2->x2);

		/*
		 * If there's any overlap between the two rectangles, add that
		 * overlap to the new region.
		 */
		if (x1 < x2)
			NEWRECT(region, pNextRect, x1, y1, x2, y2);

		/*
		 * Advance the pointer(s) with the leftmost right side, since the next
		 * rectangle on that list may still overlap the other region's
		 * current rectangle.
		 */
		if (r1->x2 == x2)
		{
			r1++;
		}
		if (r2->x2 == x2)
		{
			r2++;
		}
	}
	while ((r1 != r1End) && (r2 != r2End));

	return RTGUI_REGION_STATUS_SUCCESS;
}

rtgui_region_status_t
rtgui_region_intersect(rtgui_region_t *newReg,
						rtgui_region_t *reg1,
						rtgui_region_t *reg2)
{
	good(reg1);
	good(reg2);
	good(newReg);
	/* check for trivial reject */
	if (PIXREGION_NIL(reg1)  || PIXREGION_NIL(reg2) ||
			!EXTENTCHECK(&reg1->extents, &reg2->extents))
	{
		/* Covers about 20% of all cases */
		freeData(newReg);
		newReg->extents.x2 = newReg->extents.x1;
		newReg->extents.y2 = newReg->extents.y1;
		if (PIXREGION_NAR(reg1) || PIXREGION_NAR(reg2))
		{
			newReg->data = &rtgui_brokendata;
			return RTGUI_REGION_STATUS_FAILURE;
		}
		else
			newReg->data = &rtgui_region_emptydata;
	}
	else if (!reg1->data && !reg2->data)
	{
		/* Covers about 80% of cases that aren't trivially rejected */
		newReg->extents.x1 = RTGUI_MAX(reg1->extents.x1, reg2->extents.x1);
		newReg->extents.y1 = RTGUI_MAX(reg1->extents.y1, reg2->extents.y1);
		newReg->extents.x2 = RTGUI_MIN(reg1->extents.x2, reg2->extents.x2);
		newReg->extents.y2 = RTGUI_MIN(reg1->extents.y2, reg2->extents.y2);
		freeData(newReg);
		newReg->data = (rtgui_region_data_t *)RT_NULL;
	}
	else if (!reg2->data && SUBSUMES(&reg2->extents, &reg1->extents))
	{
		return rtgui_region_copy(newReg, reg1);
	}
	else if (!reg1->data && SUBSUMES(&reg1->extents, &reg2->extents))
	{
		return rtgui_region_copy(newReg, reg2);
	}
	else if (reg1 == reg2)
	{
		return rtgui_region_copy(newReg, reg1);
	}
	else
	{
		/* General purpose intersection */
		int overlap; /* result ignored */
		if (!rtgui_op(newReg, reg1, reg2, rtgui_region_intersectO, RTGUI_REGION_STATUS_FAILURE, RTGUI_REGION_STATUS_FAILURE,
					   &overlap))
			return RTGUI_REGION_STATUS_FAILURE;
		rtgui_set_extents(newReg);
	}

	good(newReg);
	return(RTGUI_REGION_STATUS_SUCCESS);
}

rtgui_region_status_t
rtgui_region_intersect_rect(rtgui_region_t *newReg,
						rtgui_region_t *reg1,
						rtgui_rect_t *rect)
{
	rtgui_region_t region;

	region.data = RT_NULL;
	region.extents.x1 = rect->x1;
	region.extents.y1 = rect->y1;
	region.extents.x2 = rect->x2;
	region.extents.y2 = rect->y2;

	return rtgui_region_intersect(newReg, reg1, &region);
}

#define MERGERECT(r)			\
{								\
    if (r->x1 <= x2) {			\
	/* Merge with current rectangle */							\
	if (r->x1 < x2) *pOverlap = RTGUI_REGION_STATUS_SUCCESS;	\
	if (x2 < r->x2) x2 = r->x2;	\
    } else {					\
	/* Add current rectangle, start new one */					\
	NEWRECT(region, pNextRect, x1, y1, x2, y2);					\
	x1 = r->x1;					\
	x2 = r->x2;					\
    }							\
    r++;						\
}

/*======================================================================
 *	    Region Union
 *====================================================================*/

/*-
 *-----------------------------------------------------------------------
 * rtgui_region_unionO --
 *	Handle an overlapping band for the union operation. Picks the
 *	left-most rectangle each time and merges it into the region.
 *
 * Results:
 *	RTGUI_REGION_STATUS_SUCCESS if successful.
 *
 * Side Effects:
 *	region is overwritten.
 *	pOverlap is set to RTGUI_REGION_STATUS_SUCCESS if any boxes overlap.
 *
 *-----------------------------------------------------------------------
 */
static rtgui_region_status_t
rtgui_region_unionO (
	rtgui_region_t *region,
	rtgui_rect_t *r1,
	rtgui_rect_t *r1End,
	rtgui_rect_t *r2,
	rtgui_rect_t *r2End,
	short	  y1,
	short	  y2,
	int		  *pOverlap)
{
	rtgui_rect_t *     pNextRect;
	int        x1;     /* left and right side of current union */
	int        x2;

	RT_ASSERT (y1 < y2);
	RT_ASSERT(r1 != r1End && r2 != r2End);

	pNextRect = PIXREGION_TOP(region);

	/* Start off current rectangle */
	if (r1->x1 < r2->x1)
	{
		x1 = r1->x1;
		x2 = r1->x2;
		r1++;
	}
	else
	{
		x1 = r2->x1;
		x2 = r2->x2;
		r2++;
	}
	while (r1 != r1End && r2 != r2End)
	{
		if (r1->x1 < r2->x1) MERGERECT(r1) else MERGERECT(r2);
	}

	/* Finish off whoever (if any) is left */
	if (r1 != r1End)
	{
		do
		{
			MERGERECT(r1);
		}
		while (r1 != r1End);
	}
	else if (r2 != r2End)
	{
		do
		{
			MERGERECT(r2);
		}
		while (r2 != r2End);
	}

	/* Add current rectangle */
	NEWRECT(region, pNextRect, x1, y1, x2, y2);

	return RTGUI_REGION_STATUS_SUCCESS;
}

/* Convenience function for perforRTGUI_MINg union of region with a single rectangle */
rtgui_region_status_t
rtgui_region_union_rect(rtgui_region_t *dest, rtgui_region_t *source, rtgui_rect_t* rect)
{
	rtgui_region_t region;

	region.data = RT_NULL;
	region.extents.x1 = rect->x1;
	region.extents.y1 = rect->y1;
	region.extents.x2 = rect->x2;
	region.extents.y2 = rect->y2;

	return rtgui_region_union (dest, source, &region);
}

rtgui_region_status_t
rtgui_region_union(rtgui_region_t *newReg, rtgui_region_t *reg1, rtgui_region_t *reg2)
{
	int overlap; /* result ignored */

	/* Return RTGUI_REGION_STATUS_SUCCESS if some overlap between reg1, reg2 */
	good(reg1);
	good(reg2);
	good(newReg);
	/*  checks all the simple cases */

	/*
	 * Region 1 and 2 are the same
	 */
	if (reg1 == reg2)
	{
		return rtgui_region_copy(newReg, reg1);
	}

	/*
	 * Region 1 is empty
	 */
	if (PIXREGION_NIL(reg1))
	{
		if (PIXREGION_NAR(reg1)) return rtgui_break (newReg);
		if (newReg != reg2)
			return rtgui_region_copy(newReg, reg2);
		return RTGUI_REGION_STATUS_SUCCESS;
	}

	/*
	 * Region 2 is empty
	 */
	if (PIXREGION_NIL(reg2))
	{
		if (PIXREGION_NAR(reg2)) return rtgui_break (newReg);
		if (newReg != reg1)
			return rtgui_region_copy(newReg, reg1);
		return RTGUI_REGION_STATUS_SUCCESS;
	}

	/*
	 * Region 1 completely subsumes region 2
	 */
	if (!reg1->data && SUBSUMES(&reg1->extents, &reg2->extents))
	{
		if (newReg != reg1)
			return rtgui_region_copy(newReg, reg1);
		return RTGUI_REGION_STATUS_SUCCESS;
	}

	/*
	 * Region 2 completely subsumes region 1
	 */
	if (!reg2->data && SUBSUMES(&reg2->extents, &reg1->extents))
	{
		if (newReg != reg2)
			return rtgui_region_copy(newReg, reg2);
		return RTGUI_REGION_STATUS_SUCCESS;
	}

	if (!rtgui_op(newReg, reg1, reg2, rtgui_region_unionO, RTGUI_REGION_STATUS_SUCCESS, RTGUI_REGION_STATUS_SUCCESS, &overlap))
		return RTGUI_REGION_STATUS_FAILURE;

	newReg->extents.x1 = RTGUI_MIN(reg1->extents.x1, reg2->extents.x1);
	newReg->extents.y1 = RTGUI_MIN(reg1->extents.y1, reg2->extents.y1);
	newReg->extents.x2 = RTGUI_MAX(reg1->extents.x2, reg2->extents.x2);
	newReg->extents.y2 = RTGUI_MAX(reg1->extents.y2, reg2->extents.y2);
	good(newReg);
	return RTGUI_REGION_STATUS_SUCCESS;
}

/*======================================================================
 *	    Batch Rectangle Union
 *====================================================================*/

/*-
 *-----------------------------------------------------------------------
 * rtgui_region_append --
 *
 *      "Append" the rgn rectangles onto the end of dstrgn, maintaining
 *      knowledge of YX-banding when it's easy.  Otherwise, dstrgn just
 *      becomes a non-y-x-banded random collection of rectangles, and not
 *      yet a true region.  After a sequence of appends, the caller must
 *      call rtgui_region_validate to ensure that a valid region is constructed.
 *
 * Results:
 *	RTGUI_REGION_STATUS_SUCCESS if successful.
 *
 * Side Effects:
 *      dstrgn is modified if rgn has rectangles.
 *
 */
rtgui_region_status_t
rtgui_region_append(rtgui_region_t *dstrgn,
					 rtgui_region_t *rgn)
{
	int numRects, dnumRects, size;
	rtgui_rect_t *new, *old;
	int prepend;

	if (PIXREGION_NAR(rgn)) return rtgui_break (dstrgn);

	if (!rgn->data && (dstrgn->data == &rtgui_region_emptydata))
	{
		dstrgn->extents = rgn->extents;
		dstrgn->data = (rtgui_region_data_t *)RT_NULL;
		return RTGUI_REGION_STATUS_SUCCESS;
	}

	numRects = PIXREGION_NUM_RECTS(rgn);
	if (!numRects)
		return RTGUI_REGION_STATUS_SUCCESS;
	prepend = RTGUI_REGION_STATUS_FAILURE;
	size = numRects;
	dnumRects = PIXREGION_NUM_RECTS(dstrgn);
	if (!dnumRects && (size < 200))
		size = 200; /* XXX pick numbers out of a hat */
	RECTALLOC(dstrgn, size);
	old = PIXREGION_RECTS(rgn);
	if (!dnumRects)
		dstrgn->extents = rgn->extents;
	else if (dstrgn->extents.x2 > dstrgn->extents.x1)
	{
		rtgui_rect_t *first, *last;

		first = old;
		last = PIXREGION_BOXPTR(dstrgn) + (dnumRects - 1);
		if ((first->y1 > last->y2) ||
				((first->y1 == last->y1) && (first->y2 == last->y2) &&
				 (first->x1 > last->x2)))
		{
			if (rgn->extents.x1 < dstrgn->extents.x1)
				dstrgn->extents.x1 = rgn->extents.x1;
			if (rgn->extents.x2 > dstrgn->extents.x2)
				dstrgn->extents.x2 = rgn->extents.x2;
			dstrgn->extents.y2 = rgn->extents.y2;
		}
		else
		{
			first = PIXREGION_BOXPTR(dstrgn);
			last = old + (numRects - 1);
			if ((first->y1 > last->y2) ||
					((first->y1 == last->y1) && (first->y2 == last->y2) &&
					 (first->x1 > last->x2)))
			{
				prepend = RTGUI_REGION_STATUS_SUCCESS;
				if (rgn->extents.x1 < dstrgn->extents.x1)
					dstrgn->extents.x1 = rgn->extents.x1;
				if (rgn->extents.x2 > dstrgn->extents.x2)
					dstrgn->extents.x2 = rgn->extents.x2;
				dstrgn->extents.y1 = rgn->extents.y1;
			}
			else
				dstrgn->extents.x2 = dstrgn->extents.x1;
		}
	}
	if (prepend)
	{
		new = PIXREGION_BOX(dstrgn, numRects);
		if (dnumRects == 1)
			*new = *PIXREGION_BOXPTR(dstrgn);
		else
			rt_memmove((char *)new,(char *)PIXREGION_BOXPTR(dstrgn),
					dnumRects * sizeof(rtgui_rect_t));
		new = PIXREGION_BOXPTR(dstrgn);
	}
	else
		new = PIXREGION_BOXPTR(dstrgn) + dnumRects;
	if (numRects == 1)
		*new = *old;
	else
		rt_memmove((char *)new, (char *)old, numRects * sizeof(rtgui_rect_t));
	dstrgn->data->numRects += numRects;
	return RTGUI_REGION_STATUS_SUCCESS;
}

#define ExchangeRects(a, b) \
{							\
    rtgui_rect_t     t;	    \
    t = rects[a];			\
    rects[a] = rects[b];    \
    rects[b] = t;			\
}

static void QuickSortRects(rtgui_rect_t rects[], int numRects)
{
	int	y1;
	int	x1;
	int        i, j;
	rtgui_rect_t *r;

	/* Always called with numRects > 1 */

	do
	{
		if (numRects == 2)
		{
			if (rects[0].y1 > rects[1].y1 ||
					(rects[0].y1 == rects[1].y1 && rects[0].x1 > rects[1].x1))
				ExchangeRects(0, 1);
			return;
		}

		/* Choose partition element, stick in location 0 */
		ExchangeRects(0, numRects >> 1);
		y1 = rects[0].y1;
		x1 = rects[0].x1;

		/* Partition array */
		i = 0;
		j = numRects;
		do
		{
			r = &(rects[i]);
			do
			{
				r++;
				i++;
			}
			while (i != numRects &&
					(r->y1 < y1 || (r->y1 == y1 && r->x1 < x1)));
			r = &(rects[j]);
			do
			{
				r--;
				j--;
			}
			while (y1 < r->y1 || (y1 == r->y1 && x1 < r->x1));
			if (i < j)
				ExchangeRects(i, j);
		}
		while (i < j);

		/* Move partition element back to middle */
		ExchangeRects(0, j);

		/* Recurse */
		if (numRects-j-1 > 1)
			QuickSortRects(&rects[j+1], numRects-j-1);
		numRects = j;
	}
	while (numRects > 1);
}

/*-
 *-----------------------------------------------------------------------
 * rtgui_region_validate --
 *
 *      Take a ``region'' which is a non-y-x-banded random collection of
 *      rectangles, and compute a nice region which is the union of all the
 *      rectangles.
 *
 * Results:
 *	RTGUI_REGION_STATUS_SUCCESS if successful.
 *
 * Side Effects:
 *      The passed-in ``region'' may be modified.
 *	pOverlap set to RTGUI_REGION_STATUS_SUCCESS if any retangles overlapped, else RTGUI_REGION_STATUS_FAILURE;
 *
 * Strategy:
 *      Step 1. Sort the rectangles into ascending order with primary key y1
 *		and secondary key x1.
 *
 *      Step 2. Split the rectangles into the RTGUI_MINimum number of proper y-x
 *		banded regions.  This may require horizontally merging
 *		rectangles, and vertically coalescing bands.  With any luck,
 *		this step in an identity transformation (ala the Box widget),
 *		or a coalescing into 1 box (ala Menus).
 *
 *	Step 3. Merge the separate regions down to a single region by calling
 *		rtgui_region_union.  Maximize the work each rtgui_region_union call does by using
 *		a binary merge.
 *
 *-----------------------------------------------------------------------
 */
rtgui_region_status_t rtgui_region_validate(rtgui_region_t * badreg,
	int *pOverlap)
{
	/* Descriptor for regions under construction  in Step 2. */
	typedef struct
	{
		rtgui_region_t   reg;
		int	    prevBand;
		int	    curBand;
	}
	RegionInfo;

	int	numRects;   /* Original numRects for badreg	    */
	RegionInfo *ri;	    /* Array of current regions		    */
	int	numRI;      /* Number of entries used in ri	    */
	int	sizeRI;	    /* Number of entries available in ri    */
	int	i;	    /* Index into rects			    */
	int	j;	    /* Index into ri			    */
	RegionInfo *rit;       /* &ri[j]				    */
	rtgui_region_t *  reg;        /* ri[j].reg			    */
	rtgui_rect_t *	box;	    /* Current box in rects		    */
	rtgui_rect_t *	riBox;      /* Last box in ri[j].reg		    */
	rtgui_region_t *  hreg;       /* ri[j_half].reg			    */
	rtgui_region_status_t ret = RTGUI_REGION_STATUS_SUCCESS;

	*pOverlap = RTGUI_REGION_STATUS_FAILURE;
	if (!badreg->data)
	{
		good(badreg);
		return RTGUI_REGION_STATUS_SUCCESS;
	}
	numRects = badreg->data->numRects;
	if (!numRects)
	{
		if (PIXREGION_NAR(badreg))
			return RTGUI_REGION_STATUS_FAILURE;
		good(badreg);
		return RTGUI_REGION_STATUS_SUCCESS;
	}
	if (badreg->extents.x1 < badreg->extents.x2)
	{
		if ((numRects) == 1)
		{
			freeData(badreg);
			badreg->data = (rtgui_region_data_t *) RT_NULL;
		}
		else
		{
			DOWNSIZE(badreg, numRects);
		}
		good(badreg);
		return RTGUI_REGION_STATUS_SUCCESS;
	}

	/* Step 1: Sort the rects array into ascending (y1, x1) order */
	QuickSortRects(PIXREGION_BOXPTR(badreg), numRects);

	/* Step 2: Scatter the sorted array into the RTGUI_MINimum number of regions */

	/* Set up the first region to be the first rectangle in badreg */
	/* Note that step 2 code will never overflow the ri[0].reg rects array */
	ri = (RegionInfo *) rtgui_malloc(4 * sizeof(RegionInfo));
	if (!ri) return rtgui_break (badreg);
	sizeRI = 4;
	numRI = 1;
	ri[0].prevBand = 0;
	ri[0].curBand = 0;
	ri[0].reg = *badreg;
	box = PIXREGION_BOXPTR(&ri[0].reg);
	ri[0].reg.extents = *box;
	ri[0].reg.data->numRects = 1;

	/* Now scatter rectangles into the RTGUI_MINimum set of valid regions.  If the
	   next rectangle to be added to a region would force an existing rectangle
	   in the region to be split up in order to maintain y-x banding, just
	   forget it.  Try the next region.  If it doesn't fit cleanly into any
	   region, make a new one. */

	for (i = numRects; --i > 0;)
	{
		box++;
		/* Look for a region to append box to */
		for (j = numRI, rit = ri; --j >= 0; rit++)
		{
			reg = &rit->reg;
			riBox = PIXREGION_END(reg);

			if (box->y1 == riBox->y1 && box->y2 == riBox->y2)
			{
				/* box is in same band as riBox.  Merge or append it */
				if (box->x1 <= riBox->x2)
				{
					/* Merge it with riBox */
					if (box->x1 < riBox->x2) *pOverlap = RTGUI_REGION_STATUS_SUCCESS;
					if (box->x2 > riBox->x2) riBox->x2 = box->x2;
				}
				else
				{
					RECTALLOC_BAIL(reg, 1, bail);
					*PIXREGION_TOP(reg) = *box;
					reg->data->numRects++;
				}
				goto NextRect;   /* So sue me */
			}
			else if (box->y1 >= riBox->y2)
			{
				/* Put box into new band */
				if (reg->extents.x2 < riBox->x2) reg->extents.x2 = riBox->x2;
				if (reg->extents.x1 > box->x1)   reg->extents.x1 = box->x1;
				Coalesce(reg, rit->prevBand, rit->curBand);
				rit->curBand = reg->data->numRects;
				RECTALLOC_BAIL(reg, 1, bail);
				*PIXREGION_TOP(reg) = *box;
				reg->data->numRects++;
				goto NextRect;
			}
			/* Well, this region was inappropriate.  Try the next one. */
		} /* for j */

		/* Uh-oh.  No regions were appropriate.  Create a new one. */
		if (sizeRI == numRI)
		{
			/* Oops, allocate space for new region information */
			sizeRI <<= 1;
			rit = (RegionInfo *) rt_realloc(ri, sizeRI * sizeof(RegionInfo));
			if (!rit)
				goto bail;
			ri = rit;
			rit = &ri[numRI];
		}
		numRI++;
		rit->prevBand = 0;
		rit->curBand = 0;
		rit->reg.extents = *box;
		rit->reg.data = (rtgui_region_data_t *)RT_NULL;
		if (!rtgui_rect_alloc(&rit->reg, (i+numRI) / numRI)) /* MUST force allocation */
			goto bail;
NextRect:
		;
	} /* for i */

	/* Make a final pass over each region in order to Coalesce and set
	   extents.x2 and extents.y2 */

	for (j = numRI, rit = ri; --j >= 0; rit++)
	{
		reg = &rit->reg;
		riBox = PIXREGION_END(reg);
		reg->extents.y2 = riBox->y2;
		if (reg->extents.x2 < riBox->x2) reg->extents.x2 = riBox->x2;
		Coalesce(reg, rit->prevBand, rit->curBand);
		if (reg->data->numRects == 1) /* keep unions happy below */
		{
			freeData(reg);
			reg->data = (rtgui_region_data_t *)RT_NULL;
		}
	}

	/* Step 3: Union all regions into a single region */
	while (numRI > 1)
	{
		int half = numRI/2;
		for (j = numRI & 1; j < (half + (numRI & 1)); j++)
		{
			reg = &ri[j].reg;
			hreg = &ri[j+half].reg;
			if (!rtgui_op(reg, reg, hreg, rtgui_region_unionO, RTGUI_REGION_STATUS_SUCCESS, RTGUI_REGION_STATUS_SUCCESS, pOverlap))
				ret = RTGUI_REGION_STATUS_FAILURE;
			if (hreg->extents.x1 < reg->extents.x1)
				reg->extents.x1 = hreg->extents.x1;
			if (hreg->extents.y1 < reg->extents.y1)
				reg->extents.y1 = hreg->extents.y1;
			if (hreg->extents.x2 > reg->extents.x2)
				reg->extents.x2 = hreg->extents.x2;
			if (hreg->extents.y2 > reg->extents.y2)
				reg->extents.y2 = hreg->extents.y2;
			freeData(hreg);
		}
		numRI -= half;
	}
	*badreg = ri[0].reg;
	rtgui_free(ri);
	good(badreg);
	return ret;

bail:
	for (i = 0; i < numRI; i++)
		freeData(&ri[i].reg);
	rtgui_free (ri);

	return rtgui_break (badreg);
}

/*======================================================================
 * 	    	  Region Subtraction
 *====================================================================*/

/*-
 *-----------------------------------------------------------------------
 * rtgui_region_subtractO --
 *	Overlapping band subtraction. x1 is the left-most point not yet
 *	checked.
 *
 * Results:
 *	RTGUI_REGION_STATUS_SUCCESS if successful.
 *
 * Side Effects:
 *	region may have rectangles added to it.
 *
 *-----------------------------------------------------------------------
 */
/*ARGSUSED*/
static rtgui_region_status_t
rtgui_region_subtractO (
	rtgui_region_t *region,
	rtgui_rect_t *r1,
	rtgui_rect_t *r1End,
	rtgui_rect_t *r2,
	rtgui_rect_t *r2End,
	short  	y1,
	short  	y2,
	int		*pOverlap)
{
	rtgui_rect_t *	pNextRect;
	int  	x1;

	x1 = r1->x1;

	RT_ASSERT(y1<y2);
	RT_ASSERT(r1 != r1End && r2 != r2End);

	pNextRect = PIXREGION_TOP(region);

	do
	{
		if (r2->x2 <= x1)
		{
			/*
			 * Subtrahend entirely to left of minuend: go to next subtrahend.
			 */
			r2++;
		}
		else if (r2->x1 <= x1)
		{
			/*
			 * Subtrahend preceeds RTGUI_MINuend: nuke left edge of minuend.
			 */
			x1 = r2->x2;
			if (x1 >= r1->x2)
			{
				/*
				 * Minuend completely covered: advance to next minuend and
				 * reset left fence to edge of new RTGUI_MINuend.
				 */
				r1++;
				if (r1 != r1End)
					x1 = r1->x1;
			}
			else
			{
				/*
				 * Subtrahend now used up since it doesn't extend beyond
				 * minuend
				 */
				r2++;
			}
		}
		else if (r2->x1 < r1->x2)
		{
			/*
			 * Left part of subtrahend covers part of minuend: add uncovered
			 * part of minuend to region and skip to next subtrahend.
			 */
			RT_ASSERT(x1<r2->x1);
			NEWRECT(region, pNextRect, x1, y1, r2->x1, y2);

			x1 = r2->x2;
			if (x1 >= r1->x2)
			{
				/*
				 * Minuend used up: advance to new...
				 */
				r1++;
				if (r1 != r1End)
					x1 = r1->x1;
			}
			else
			{
				/*
				 * Subtrahend used up
				 */
				r2++;
			}
		}
		else
		{
			/*
			 * Minuend used up: add any remaining piece before advancing.
			 */
			if (r1->x2 > x1)
				NEWRECT(region, pNextRect, x1, y1, r1->x2, y2);
			r1++;
			if (r1 != r1End)
				x1 = r1->x1;
		}
	}
	while ((r1 != r1End) && (r2 != r2End));

	/*
	 * Add remaining minuend rectangles to region.
	 */
	while (r1 != r1End)
	{
		RT_ASSERT(x1<r1->x2);
		NEWRECT(region, pNextRect, x1, y1, r1->x2, y2);
		r1++;
		if (r1 != r1End)
			x1 = r1->x1;
	}
	return RTGUI_REGION_STATUS_SUCCESS;
}

/*-
 *-----------------------------------------------------------------------
 * rtgui_region_subtract --
 *	Subtract regS from regM and leave the result in regD.
 *	S stands for subtrahend, M for minuend and D for difference.
 *
 * Results:
 *	RTGUI_REGION_STATUS_SUCCESS if successful.
 *
 * Side Effects:
 *	regD is overwritten.
 *
 *-----------------------------------------------------------------------
 */
rtgui_region_status_t
rtgui_region_subtract(rtgui_region_t *regD,
					   rtgui_region_t *regM,
					   rtgui_region_t *regS)
{
	int overlap; /* result ignored */

	good(regM);
	good(regS);
	good(regD);
	/* check for trivial rejects */
	if (PIXREGION_NIL(regM) || PIXREGION_NIL(regS) ||
			!EXTENTCHECK(&regM->extents, &regS->extents))
	{
		if (PIXREGION_NAR (regS)) return rtgui_break (regD);
		return rtgui_region_copy(regD, regM);
	}
	else if (regM == regS)
	{
		freeData(regD);
		regD->extents.x2 = regD->extents.x1;
		regD->extents.y2 = regD->extents.y1;
		regD->data = &rtgui_region_emptydata;
		return RTGUI_REGION_STATUS_SUCCESS;
	}

	/* Add those rectangles in region 1 that aren't in region 2,
	   do yucky substraction for overlaps, and
	   just throw away rectangles in region 2 that aren't in region 1 */
	if (!rtgui_op(regD, regM, regS, rtgui_region_subtractO, RTGUI_REGION_STATUS_SUCCESS, RTGUI_REGION_STATUS_FAILURE, &overlap))
		return RTGUI_REGION_STATUS_FAILURE;

	/*
	 * Can't alter RegD's extents before we call rtgui_op because
	 * it might be one of the source regions and rtgui_op depends
	 * on the extents of those regions being unaltered. Besides, this
	 * way there's no checking against rectangles that will be nuked
	 * due to coalescing, so we have to exaRTGUI_MINe fewer rectangles.
	 */
	rtgui_set_extents(regD);
	good(regD);
	return RTGUI_REGION_STATUS_SUCCESS;
}

rtgui_region_status_t
rtgui_region_subtract_rect (rtgui_region_t *regD, rtgui_region_t *regM, rtgui_rect_t* rect)
{
	rtgui_region_t region;

	region.data = RT_NULL;
	region.extents.x1 = rect->x1;
	region.extents.y1 = rect->y1;
	region.extents.x2 = rect->x2;
	region.extents.y2 = rect->y2;

	return rtgui_region_subtract(regD, regM, &region);
}

/*======================================================================
 *	    Region Inversion
 *====================================================================*/

/*-
 *-----------------------------------------------------------------------
 * rtgui_region_inverse --
 *	Take a region and a box and return a region that is everything
 *	in the box but not in the region. The careful reader will note
 *	that this is the same as subtracting the region from the box...
 *
 * Results:
 *	RTGUI_REGION_STATUS_SUCCESS.
 *
 * Side Effects:
 *	newReg is overwritten.
 *
 *-----------------------------------------------------------------------
 */
rtgui_region_status_t
rtgui_region_inverse(rtgui_region_t *newReg,       /* Destination region */
					  rtgui_region_t *reg1,         /* Region to invert */
					  rtgui_rect_t *invRect) 		/* Bounding box for inversion */
{
	rtgui_region_t	  invReg;   	/* Quick and dirty region made from the
					 * bounding box */
	int	  overlap;	/* result ignored */

	good(reg1);
	good(newReg);
	/* check for trivial rejects */
	if (PIXREGION_NIL(reg1) || !EXTENTCHECK(invRect, &reg1->extents))
	{
		if (PIXREGION_NAR(reg1)) return rtgui_break (newReg);
		newReg->extents = *invRect;
		freeData(newReg);
		newReg->data = (rtgui_region_data_t *)RT_NULL;
		return RTGUI_REGION_STATUS_SUCCESS;
	}

	/* Add those rectangles in region 1 that aren't in region 2,
	   do yucky substraction for overlaps, and
	   just throw away rectangles in region 2 that aren't in region 1 */
	invReg.extents = *invRect;
	invReg.data = (rtgui_region_data_t *)RT_NULL;
	if (!rtgui_op(newReg, &invReg, reg1, rtgui_region_subtractO, RTGUI_REGION_STATUS_SUCCESS, RTGUI_REGION_STATUS_FAILURE, &overlap))
		return RTGUI_REGION_STATUS_FAILURE;

	/*
	 * Can't alter newReg's extents before we call rtgui_op because
	 * it might be one of the source regions and rtgui_op depends
	 * on the extents of those regions being unaltered. Besides, this
	 * way there's no checking against rectangles that will be nuked
	 * due to coalescing, so we have to exaRTGUI_MINe fewer rectangles.
	 */
	rtgui_set_extents(newReg);
	good(newReg);
	return RTGUI_REGION_STATUS_SUCCESS;
}

/*
 *   RectIn(region, rect)
 *   This routine takes a pointer to a region and a pointer to a box
 *   and deterRTGUI_MINes if the box is outside/inside/partly inside the region.
 *
 *   The idea is to travel through the list of rectangles trying to cover the
 *   passed box with them. Anytime a piece of the rectangle isn't covered
 *   by a band of rectangles, partOut is set RTGUI_REGION_STATUS_SUCCESS. Any time a rectangle in
 *   the region covers part of the box, partIn is set RTGUI_REGION_STATUS_SUCCESS. The process ends
 *   when either the box has been completely covered (we reached a band that
 *   doesn't overlap the box, partIn is RTGUI_REGION_STATUS_SUCCESS and partOut is false), the
 *   box has been partially covered (partIn == partOut == RTGUI_REGION_STATUS_SUCCESS -- because of
 *   the banding, the first time this is true we know the box is only
 *   partially in the region) or is outside the region (we reached a band
 *   that doesn't overlap the box at all and partIn is false)
 */

int rtgui_region_contains_rectangle(rtgui_region_t *region, rtgui_rect_t *prect)
{
	int	x;
	int	y;
	rtgui_rect_t *     pbox;
	rtgui_rect_t *     pboxEnd;
	int			partIn, partOut;
	int			numRects;

	good(region);
	numRects = PIXREGION_NUM_RECTS(region);
	/* useful optimization */
	if (!numRects || !EXTENTCHECK(&region->extents, prect))
		return(RTGUI_REGION_OUT);

	if (numRects == 1)
	{
		/* We know that it must be rgnIN or rgnPART */
		if (SUBSUMES(&region->extents, prect))
			return(RTGUI_REGION_IN);
		else
			return(RTGUI_REGION_PART);
	}

	partOut = RTGUI_REGION_STATUS_FAILURE;
	partIn = RTGUI_REGION_STATUS_FAILURE;

	/* (x,y) starts at upper left of rect, moving to the right and down */
	x = prect->x1;
	y = prect->y1;

	/* can stop when both partOut and partIn are RTGUI_REGION_STATUS_SUCCESS, or we reach prect->y2 */
	for (pbox = PIXREGION_BOXPTR(region), pboxEnd = pbox + numRects;
			pbox != pboxEnd;
			pbox++)
	{

		if (pbox->y2 <= y)
			continue;    /* getting up to speed or skipping remainder of band */

		if (pbox->y1 > y)
		{
			partOut = RTGUI_REGION_STATUS_SUCCESS;      /* missed part of rectangle above */
			if (partIn || (pbox->y1 >= prect->y2))
				break;
			y = pbox->y1;        /* x guaranteed to be == prect->x1 */
		}

		if (pbox->x2 <= x)
			continue;            /* not far enough over yet */

		if (pbox->x1 > x)
		{
			partOut = RTGUI_REGION_STATUS_SUCCESS;      /* missed part of rectangle to left */
			if (partIn)
				break;
		}

		if (pbox->x1 < prect->x2)
		{
			partIn = RTGUI_REGION_STATUS_SUCCESS;      /* definitely overlap */
			if (partOut)
				break;
		}

		if (pbox->x2 >= prect->x2)
		{
			y = pbox->y2;        /* finished with this band */
			if (y >= prect->y2)
				break;
			x = prect->x1;       /* reset x out to left again */
		}
		else
		{
			/*
			 * Because boxes in a band are maximal width, if the first box
			 * to overlap the rectangle doesn't completely cover it in that
			 * band, the rectangle must be partially out, since some of it
			 * will be uncovered in that band. partIn will have been set true
			 * by now...
			 */
			partOut = RTGUI_REGION_STATUS_SUCCESS;
			break;
		}
	}

	return(partIn ? ((y < prect->y2) ? RTGUI_REGION_PART : RTGUI_REGION_IN) : RTGUI_REGION_OUT);
}

/* rtgui_region_translate (region, x, y)
   translates in place
*/
void rtgui_region_translate (rtgui_region_t *region, int x, int y)
{
	int x1, x2, y1, y2;
	int nbox;
	rtgui_rect_t * pbox;

	good(region);
	region->extents.x1 = x1 = region->extents.x1 + x;
	region->extents.y1 = y1 = region->extents.y1 + y;
	region->extents.x2 = x2 = region->extents.x2 + x;
	region->extents.y2 = y2 = region->extents.y2 + y;
	if (((x1 - RTGUI_SHRT_MIN)|(y1 - RTGUI_SHRT_MIN)|(RTGUI_SHRT_MAX - x2)|(RTGUI_SHRT_MAX - y2)) >= 0)
	{
		nbox = region->data->numRects;
		if (region->data && nbox)
		{
			for (pbox = PIXREGION_BOXPTR(region); nbox--; pbox++)
			{
				pbox->x1 += x;
				pbox->y1 += y;
				pbox->x2 += x;
				pbox->y2 += y;
			}
		}
		return;
	}
	if (((x2 - RTGUI_SHRT_MIN)|(y2 - RTGUI_SHRT_MIN)|(RTGUI_SHRT_MAX - x1)|(RTGUI_SHRT_MAX - y1)) <= 0)
	{
		region->extents.x2 = region->extents.x1;
		region->extents.y2 = region->extents.y1;
		freeData(region);
		region->data = &rtgui_region_emptydata;
		return;
	}
	if (x1 < RTGUI_SHRT_MIN)
		region->extents.x1 = RTGUI_SHRT_MIN;
	else if (x2 > RTGUI_SHRT_MAX)
		region->extents.x2 = RTGUI_SHRT_MAX;
	if (y1 < RTGUI_SHRT_MIN)
		region->extents.y1 = RTGUI_SHRT_MIN;
	else if (y2 > RTGUI_SHRT_MAX)
		region->extents.y2 = RTGUI_SHRT_MAX;

	nbox = region->data->numRects;
	if (region->data && nbox)
	{
		rtgui_rect_t * pboxout;

		for (pboxout = pbox = PIXREGION_BOXPTR(region); nbox--; pbox++)
		{
			pboxout->x1 = x1 = pbox->x1 + x;
			pboxout->y1 = y1 = pbox->y1 + y;
			pboxout->x2 = x2 = pbox->x2 + x;
			pboxout->y2 = y2 = pbox->y2 + y;
			if (((x2 - RTGUI_SHRT_MIN)|(y2 - RTGUI_SHRT_MIN)|
					(RTGUI_SHRT_MAX - x1)|(RTGUI_SHRT_MAX - y1)) <= 0)
			{
				region->data->numRects--;
				continue;
			}
			if (x1 < RTGUI_SHRT_MIN)
				pboxout->x1 = RTGUI_SHRT_MIN;
			else if (x2 > RTGUI_SHRT_MAX)
				pboxout->x2 = RTGUI_SHRT_MAX;
			if (y1 < RTGUI_SHRT_MIN)
				pboxout->y1 = RTGUI_SHRT_MIN;
			else if (y2 > RTGUI_SHRT_MAX)
				pboxout->y2 = RTGUI_SHRT_MAX;
			pboxout++;
		}
		if (pboxout != pbox)
		{
			if (region->data->numRects == 1)
			{
				region->extents = *PIXREGION_BOXPTR(region);
				freeData(region);
				region->data = (rtgui_region_data_t *)RT_NULL;
			}
			else
				rtgui_set_extents(region);
		}
	}
}

void rtgui_region_reset(rtgui_region_t *region, rtgui_rect_t* rect)
{
	good(region);
	freeData(region);

	rtgui_region_init_with_extents(region, rect);
}

/* box is "return" value */
int rtgui_region_contains_point(rtgui_region_t * region,
							 int x, int y,
							 rtgui_rect_t * box)
{
	rtgui_rect_t *pbox, *pboxEnd;
	int numRects;

	good(region);
	numRects = PIXREGION_NUM_RECTS(region);
	if (!numRects || !INBOX(&region->extents, x, y))
		return -RT_ERROR;

	if (numRects == 1)
	{
		*box = region->extents;
		return RT_EOK;
	}

	for (pbox = PIXREGION_BOXPTR(region), pboxEnd = pbox + numRects;
			pbox != pboxEnd;
			pbox++)
	{
		if (y >= pbox->y2)
			continue;		/* not there yet */
		if ((y < pbox->y1) || (x < pbox->x1))
			break;		/* missed it */
		if (x >= pbox->x2)
			continue;		/* not there yet */
		*box = *pbox;
		return RT_EOK;
	}

	return -RT_ERROR;
}

int rtgui_region_not_empty(rtgui_region_t * region)
{
	good(region);

	return(!PIXREGION_NIL(region));
}

void rtgui_region_empty(rtgui_region_t * region)
{
	good(region);
	freeData(region);

	region->extents = rtgui_empty_rect;
	region->data = &rtgui_region_emptydata;
}

rtgui_rect_t *rtgui_region_extents(rtgui_region_t * region)
{
	good(region);
	return(&region->extents);
}

#define ExchangeSpans(a, b)		\
{								\
    rtgui_point_t tpt;			\
    int    tw;					\
							    \
    tpt = spans[a]; spans[a] = spans[b]; spans[b] = tpt;    \
    tw = widths[a]; widths[a] = widths[b]; widths[b] = tw;  \
}

/* ||| I should apply the merge sort code to rectangle sorting above, and see
   if mapping time can be improved.  But right now I've been at work 12 hours,
   so forget it.
*/

static void QuickSortSpans(
	rtgui_point_t spans[],
	int	    widths[],
	int	    numSpans)
{
	int	    y;
	int	    i, j, m;
	rtgui_point_t *r;

	/* Always called with numSpans > 1 */
	/* Sorts only by y, doesn't bother to sort by x */

	do
	{
		if (numSpans < 9)
		{
			/* Do insertion sort */
			int yprev;

			yprev = spans[0].y;
			i = 1;
			do
			{ /* while i != numSpans */
				y = spans[i].y;
				if (yprev > y)
				{
					/* spans[i] is out of order.  Move into proper location. */
					rtgui_point_t tpt;
					int	    tw, k;

					for (j = 0; y >= spans[j].y; j++)
					{}
					tpt = spans[i];
					tw  = widths[i];
					for (k = i; k != j; k--)
					{
						spans[k] = spans[k-1];
						widths[k] = widths[k-1];
					}
					spans[j] = tpt;
					widths[j] = tw;
					y = spans[i].y;
				} /* if out of order */
				yprev = y;
				i++;
			}
			while (i != numSpans);
			return;
		}

		/* Choose partition element, stick in location 0 */
		m = numSpans / 2;
		if (spans[m].y > spans[0].y)		ExchangeSpans(m, 0);
		if (spans[m].y > spans[numSpans-1].y)   ExchangeSpans(m, numSpans-1);
		if (spans[m].y > spans[0].y)		ExchangeSpans(m, 0);
		y = spans[0].y;

		/* Partition array */
		i = 0;
		j = numSpans;
		do
		{
			r = &(spans[i]);
			do
			{
				r++;
				i++;
			}
			while (i != numSpans && r->y < y);
			r = &(spans[j]);
			do
			{
				r--;
				j--;
			}
			while (y < r->y);
			if (i < j)
				ExchangeSpans(i, j);
		}
		while (i < j);

		/* Move partition element back to middle */
		ExchangeSpans(0, j);

		/* Recurse */
		if (numSpans-j-1 > 1)
			QuickSortSpans(&spans[j+1], &widths[j+1], numSpans-j-1);
		numSpans = j;
	}
	while (numSpans > 1);
}

void rtgui_region_dump(rtgui_region_t* region)
{
	int num;
	int i;
	rtgui_rect_t * rects;

	num = PIXREGION_NUM_RECTS(region);
	rects = PIXREGION_RECTS(region);
	rt_kprintf("extents: (%d,%d) (%d,%d)\n",
		region->extents.x1, region->extents.y1,
		region->extents.x2, region->extents.y2);

	for (i = 0; i < num; i++)
	{
		rt_kprintf("box[%d]: (%d,%d) (%d,%d)\n", i,
			rects[i].x1, rects[i].y1,
			rects[i].x2, rects[i].y2);
	}
}

2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148

int rtgui_region_is_flat(rtgui_region_t* region)
{
	int num;

	num = PIXREGION_NUM_RECTS(region);
	if (num == 1) return RT_EOK;

	return -RT_ERROR;
}

B
bernard.xiong 已提交
2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222
void rtgui_rect_moveto(rtgui_rect_t *rect, int x, int y)
{
	rect->x1 += x;
	rect->x2 += x;

	rect->y1 += y;
	rect->y2 += y;
}

void rtgui_rect_moveto_align(rtgui_rect_t *rect, rtgui_rect_t *to, int align)
{
	int dw, dh;
	dw = 0;
	dh = 0;

	/* get delta width and height */
	dw = rtgui_rect_width(*rect) - rtgui_rect_width(*to);
	dh = rtgui_rect_height(*rect) - rtgui_rect_height(*to);
	if (dw < 0) dw = 0;
	if (dh < 0) dh = 0;

	/* move to insider of rect */
	rtgui_rect_moveto(to, rect->x1, rect->y1);

	/* limited the destination rect to source rect */
	if (dw == 0) to->x2 = rect->x2;
	if (dh == 0) to->y2 = rect->y2;

	/* align to right */
	if (align & RTGUI_ALIGN_RIGHT)
	{
		to->x1 += dw;
		to->x2 += dw;
	}

	/* align to bottom */
	if (align & RTGUI_ALIGN_BOTTOM)
	{
		to->y1 += dh;
		to->y2 += dh;
	}

	/* align to center horizontal */
	if (align & RTGUI_ALIGN_CENTER_HORIZONTAL)
	{
		to->x1 += dw >> 1;
		to->x2 += dw >> 1;
	}

	/* align to center vertical */
	if (align & RTGUI_ALIGN_CENTER_VERTICAL)
	{
		to->y1 += dh >> 1;
		to->y2 += dh >> 1;
	}
}

void rtgui_rect_inflate(rtgui_rect_t *rect, int d)
{
	rect->x1 -= d;
	rect->x2 += d;
	rect->y1 -= d;
	rect->y2 += d;
}

/* put the intersect of src rect and dest rect to dest */
void rtgui_rect_intersect(rtgui_rect_t *src, rtgui_rect_t *dest)
{
	if (dest->x1 < src->x1) dest->x1 = src->x1;
	if (dest->y1 < src->y1) dest->y1 = src->y1;
	if (dest->x2 > src->x2) dest->x2 = src->x2;
	if (dest->y2 > src->y2) dest->y2 = src->y2;
}

2223
int rtgui_rect_contains_point(const rtgui_rect_t *rect, int x, int y)
B
bernard.xiong 已提交
2224 2225 2226 2227 2228 2229
{
	if (INBOX(rect, x, y)) return RT_EOK;

	return -RT_ERROR;
}

2230
int rtgui_rect_is_intersect(const rtgui_rect_t *rect1, const rtgui_rect_t *rect2)
B
bernard.xiong 已提交
2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245
{
	if (INBOX(rect1, rect2->x1, rect2->y1) ||
		INBOX(rect1, rect2->x1, rect2->y2) ||
		INBOX(rect1, rect2->x2, rect2->y1) ||
		INBOX(rect1, rect2->x2, rect2->y2))
	{
		return RT_EOK;
	}
	else if (INBOX(rect2, rect1->x1, rect1->y1) ||
		INBOX(rect2, rect1->x1, rect1->y2) ||
		INBOX(rect2, rect1->x2, rect1->y1) ||
		INBOX(rect2, rect1->x2, rect1->y2))
	{
		return RT_EOK;
	}
2246 2247 2248 2249 2250 2251 2252 2253
	else if (CROSS(rect1,rect2))
	{
		return RT_EOK;
	}
	else if (CROSS(rect2,rect1))
	{
		return RT_EOK;
	}
B
bernard.xiong 已提交
2254 2255 2256

	return -RT_ERROR;
}
2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267

int rtgui_rect_is_equal(const rtgui_rect_t *rect1, const rtgui_rect_t *rect2)
{
	if (*((rt_uint32_t*)(rect1)) == *((rt_uint32_t*)(rect2)) &&
		*(((rt_uint32_t*)(rect1)) + 1) == *(((rt_uint32_t*)(rect2)) + 1))
	{
		return RT_EOK;
	}

	return -RT_ERROR;
}