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 41 42 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 117 118 119 120 121 122 123 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 185 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 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 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 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505
|
/*
* GDI region objects. Shamelessly ripped out from the X11 distribution
* Thanks for the nice licence.
*
* Copyright 1993, 1994, 1995 Alexandre Julliard
* Modifications and additions: Copyright 1998 Huw Davies
*
*/
/************************************************************************
Copyright (c) 1987, 1988 X Consortium
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of the X Consortium shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from the X Consortium.
Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts.
All Rights Reserved
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name of Digital not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.
************************************************************************/
/*
* The functions in this file implement the Region abstraction, similar to one
* used in the X11 sample server. A Region is simply an area, as the name
* implies, and is implemented as a "y-x-banded" array of rectangles. To
* explain: Each Region is made up of a certain number of rectangles sorted
* by y coordinate first, and then by x coordinate.
*
* Furthermore, the rectangles are banded such that every rectangle with a
* given upper-left y coordinate (y1) will have the same lower-right y
* coordinate (y2) and vice versa. If a rectangle has scanlines in a band, it
* will span the entire vertical distance of the band. This means that some
* areas that could be merged into a taller rectangle will be represented as
* several shorter rectangles to account for shorter rectangles to its left
* or right but within its "vertical scope".
*
* An added constraint on the rectangles is that they must cover as much
* horizontal area as possible. E.g. no two rectangles in 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). This maintains
* the y-x-banding that's so nice to have...
*/
#include <stdio.h>
#include "region.h"
#include "debug.h"
#include "heap.h"
#include "dc.h"
typedef void (*voidProcp)();
/* Note the parameter order is different from the X11 equivalents */
static void REGION_CopyRegion(WINEREGION *d, WINEREGION *s);
static void REGION_IntersectRegion(WINEREGION *d, WINEREGION *s1, WINEREGION *s2);
static void REGION_UnionRegion(WINEREGION *d, WINEREGION *s1, WINEREGION *s2);
static void REGION_SubtractRegion(WINEREGION *d, WINEREGION *s1, WINEREGION *s2);
static void REGION_XorRegion(WINEREGION *d, WINEREGION *s1, WINEREGION *s2);
static void REGION_UnionRectWithRegion(const RECT32 *rect, WINEREGION *rgn);
/***********************************************************************
* REGION_DumpRegion
* Outputs the contents of a WINEREGION
*/
static void REGION_DumpRegion(WINEREGION *pReg)
{
RECT32 *pRect, *pRectEnd = pReg->rects + pReg->numRects;
TRACE(region, "Region %p: %d,%d - %d,%d %d rects\n", pReg,
pReg->extents.left, pReg->extents.top,
pReg->extents.right, pReg->extents.bottom, pReg->numRects);
for(pRect = pReg->rects; pRect < pRectEnd; pRect++)
TRACE(region, "\t%d,%d - %d,%d\n", pRect->left, pRect->top,
pRect->right, pRect->bottom);
return;
}
/***********************************************************************
* REGION_AllocWineRegion
* Create a new empty WINEREGION.
*/
static WINEREGION *REGION_AllocWineRegion( void )
{
WINEREGION *pReg;
if (!(pReg = HeapAlloc(SystemHeap, 0, sizeof( WINEREGION ))))
return NULL;
if (!(pReg->rects = HeapAlloc(SystemHeap, 0, sizeof( RECT32 ))))
{
HeapFree(SystemHeap, 0, pReg);
return NULL;
}
pReg->size = 1;
EMPTY_REGION(pReg);
return pReg;
}
/***********************************************************************
* REGION_CreateRegion
* Create a new empty region.
*/
static HRGN32 REGION_CreateRegion(void)
{
HRGN32 hrgn;
RGNOBJ *obj;
if(!(hrgn = GDI_AllocObject( sizeof(RGNOBJ), REGION_MAGIC )))
return 0;
obj = (RGNOBJ *) GDI_HEAP_LOCK( hrgn );
if(!(obj->rgn = REGION_AllocWineRegion())) {
GDI_FreeObject( hrgn );
return 0;
}
GDI_HEAP_UNLOCK( hrgn );
return hrgn;
}
/***********************************************************************
* REGION_DestroyWineRegion
*/
static void REGION_DestroyWineRegion( WINEREGION* pReg )
{
HeapFree( SystemHeap, 0, pReg->rects );
HeapFree( SystemHeap, 0, pReg );
return;
}
/***********************************************************************
* REGION_DeleteObject
*/
BOOL32 REGION_DeleteObject( HRGN32 hrgn, RGNOBJ * obj )
{
TRACE(region, " %04x\n", hrgn );
REGION_DestroyWineRegion( obj->rgn );
return GDI_FreeObject( hrgn );
}
/***********************************************************************
* OffsetRgn16 (GDI.101)
*/
INT16 WINAPI OffsetRgn16( HRGN16 hrgn, INT16 x, INT16 y )
{
return OffsetRgn32( hrgn, x, y );
}
/***********************************************************************
* OffsetRgn32 (GDI32.256)
*/
INT32 WINAPI OffsetRgn32( HRGN32 hrgn, INT32 x, INT32 y )
{
RGNOBJ * obj = (RGNOBJ *) GDI_GetObjPtr( hrgn, REGION_MAGIC );
if (obj)
{
INT32 ret;
int nbox = obj->rgn->numRects;
RECT32 *pbox = obj->rgn->rects;
TRACE(region, " %04x %d,%d\n", hrgn, x, y );
if(nbox && (x || y)) {
while(nbox--) {
pbox->left += x;
pbox->right += x;
pbox->top += y;
pbox->bottom += y;
pbox++;
}
obj->rgn->extents.left += x;
obj->rgn->extents.right += x;
obj->rgn->extents.top += y;
obj->rgn->extents.bottom += y;
}
ret = obj->rgn->type;
GDI_HEAP_UNLOCK( hrgn );
return ret;
}
return ERROR;
}
/***********************************************************************
* GetRgnBox16 (GDI.134)
*/
INT16 WINAPI GetRgnBox16( HRGN16 hrgn, LPRECT16 rect )
{
RECT32 r;
INT16 ret = (INT16)GetRgnBox32( hrgn, &r );
CONV_RECT32TO16( &r, rect );
return ret;
}
/***********************************************************************
* GetRgnBox32 (GDI32.219)
*/
INT32 WINAPI GetRgnBox32( HRGN32 hrgn, LPRECT32 rect )
{
RGNOBJ * obj = (RGNOBJ *) GDI_GetObjPtr( hrgn, REGION_MAGIC );
if (obj)
{
INT32 ret;
TRACE(region, " %04x\n", hrgn );
rect->left = obj->rgn->extents.left;
rect->top = obj->rgn->extents.top;
rect->right = obj->rgn->extents.right;
rect->bottom = obj->rgn->extents.bottom;
ret = obj->rgn->type;
GDI_HEAP_UNLOCK(hrgn);
return ret;
}
return ERROR;
}
/***********************************************************************
* CreateRectRgn16 (GDI.64)
*/
HRGN16 WINAPI CreateRectRgn16(INT16 left, INT16 top, INT16 right, INT16 bottom)
{
return (HRGN16)CreateRectRgn32( left, top, right, bottom );
}
/***********************************************************************
* CreateRectRgn32 (GDI32.59)
*/
HRGN32 WINAPI CreateRectRgn32(INT32 left, INT32 top, INT32 right, INT32 bottom)
{
HRGN32 hrgn;
if (!(hrgn = REGION_CreateRegion()))
return 0;
TRACE(region, " \n");
SetRectRgn32(hrgn, left, top, right, bottom);
return hrgn;
}
/***********************************************************************
* CreateRectRgnIndirect16 (GDI.65)
*/
HRGN16 WINAPI CreateRectRgnIndirect16( const RECT16* rect )
{
return CreateRectRgn32( rect->left, rect->top, rect->right, rect->bottom );
}
/***********************************************************************
* CreateRectRgnIndirect32 (GDI32.60)
*/
HRGN32 WINAPI CreateRectRgnIndirect32( const RECT32* rect )
{
return CreateRectRgn32( rect->left, rect->top, rect->right, rect->bottom );
}
/***********************************************************************
* SetRectRgn16 (GDI.172)
*/
VOID WINAPI SetRectRgn16( HRGN16 hrgn, INT16 left, INT16 top,
INT16 right, INT16 bottom )
{
SetRectRgn32( hrgn, left, top, right, bottom );
}
/***********************************************************************
* SetRectRgn32 (GDI32.332)
*/
VOID WINAPI SetRectRgn32( HRGN32 hrgn, INT32 left, INT32 top,
INT32 right, INT32 bottom )
{
RGNOBJ * obj;
TRACE(region, " %04x %d,%d-%d,%d\n",
hrgn, left, top, right, bottom );
if (!(obj = (RGNOBJ *) GDI_GetObjPtr( hrgn, REGION_MAGIC ))) return;
if ((right > left) && (bottom > top))
{
obj->rgn->rects->left = obj->rgn->extents.left = left;
obj->rgn->rects->top = obj->rgn->extents.top = top;
obj->rgn->rects->right = obj->rgn->extents.right = right;
obj->rgn->rects->bottom = obj->rgn->extents.bottom = bottom;
obj->rgn->numRects = 1;
obj->rgn->type = SIMPLEREGION;
}
else
EMPTY_REGION(obj->rgn);
GDI_HEAP_UNLOCK( hrgn );
}
/***********************************************************************
* CreateRoundRectRgn16 (GDI.444)
*/
HRGN16 WINAPI CreateRoundRectRgn16( INT16 left, INT16 top,
INT16 right, INT16 bottom,
INT16 ellipse_width, INT16 ellipse_height )
{
return (HRGN16)CreateRoundRectRgn32( left, top, right, bottom,
ellipse_width, ellipse_height );
}
/***********************************************************************
* CreateRoundRectRgn32 (GDI32.61)
*/
HRGN32 WINAPI CreateRoundRectRgn32( INT32 left, INT32 top,
INT32 right, INT32 bottom,
INT32 ellipse_width, INT32 ellipse_height )
{
RGNOBJ * obj;
HRGN32 hrgn;
int asq, bsq, d, xd, yd;
RECT32 rect;
/* Check if we can do a normal rectangle instead */
if ((right <= left) || (bottom <= top) ||
(ellipse_width <= 0) || (ellipse_height <= 0))
return CreateRectRgn32( left, top, right, bottom );
/* Create region */
if (!(hrgn = REGION_CreateRegion())) return 0;
obj = (RGNOBJ *) GDI_HEAP_LOCK( hrgn );
TRACE(region,"(%d,%d-%d,%d %dx%d): ret=%04x\n",
left, top, right, bottom, ellipse_width, ellipse_height, hrgn );
/* Check parameters */
if (ellipse_width > right-left) ellipse_width = right-left;
if (ellipse_height > bottom-top) ellipse_height = bottom-top;
/* Ellipse algorithm, based on an article by K. Porter */
/* in DDJ Graphics Programming Column, 8/89 */
asq = ellipse_width * ellipse_width / 4; /* a^2 */
bsq = ellipse_height * ellipse_height / 4; /* b^2 */
d = bsq - asq * ellipse_height / 2 + asq / 4; /* b^2 - a^2b + a^2/4 */
xd = 0;
yd = asq * ellipse_height; /* 2a^2b */
rect.left = left + ellipse_width / 2;
rect.right = right - ellipse_width;
/* Loop to draw first half of quadrant */
while (xd < yd)
{
if (d > 0) /* if nearest pixel is toward the center */
{
/* move toward center */
rect.top = top++;
rect.bottom = rect.top + 1;
REGION_UnionRectWithRegion( &rect, obj->rgn );
rect.top = --bottom;
rect.bottom = rect.top + 1;
REGION_UnionRectWithRegion( &rect, obj->rgn );
yd -= 2*asq;
d -= yd;
}
rect.left--; /* next horiz point */
rect.right++;
xd += 2*bsq;
d += bsq + xd;
}
/* Loop to draw second half of quadrant */
d += (3 * (asq-bsq) / 2 - (xd+yd)) / 2;
while (yd >= 0)
{
/* next vertical point */
rect.top = top++;
rect.bottom = rect.top + 1;
REGION_UnionRectWithRegion( &rect, obj->rgn );
rect.top = --bottom;
rect.bottom = rect.top + 1;
REGION_UnionRectWithRegion( &rect, obj->rgn );
if (d < 0) /* if nearest pixel is outside ellipse */
{
rect.left--; /* move away from center */
rect.right++;
xd += 2*bsq;
d += xd;
}
yd -= 2*asq;
d += asq - yd;
}
/* Add the inside rectangle */
if (top <= bottom)
{
rect.top = top;
rect.bottom = bottom;
REGION_UnionRectWithRegion( &rect, obj->rgn );
}
obj->rgn->type = SIMPLEREGION; /* FIXME? */
GDI_HEAP_UNLOCK( hrgn );
return hrgn;
}
/***********************************************************************
* CreateEllipticRgn16 (GDI.54)
*/
HRGN16 WINAPI CreateEllipticRgn16( INT16 left, INT16 top,
INT16 right, INT16 bottom )
{
return (HRGN16)CreateRoundRectRgn32( left, top, right, bottom,
right-left, bottom-top );
}
/***********************************************************************
* CreateEllipticRgn32 (GDI32.39)
*/
HRGN32 WINAPI CreateEllipticRgn32( INT32 left, INT32 top,
INT32 right, INT32 bottom )
{
return CreateRoundRectRgn32( left, top, right, bottom,
right-left, bottom-top );
}
/***********************************************************************
* CreateEllipticRgnIndirect16 (GDI.55)
*/
HRGN16 WINAPI CreateEllipticRgnIndirect16( const RECT16 *rect )
{
return CreateRoundRectRgn32( rect->left, rect->top, rect->right,
rect->bottom, rect->right - rect->left,
rect->bottom - rect->top );
}
/***********************************************************************
* CreateEllipticRgnIndirect32 (GDI32.40)
*/
HRGN32 WINAPI CreateEllipticRgnIndirect32( const RECT32 *rect )
{
return CreateRoundRectRgn32( rect->left, rect->top, rect->right,
rect->bottom, rect->right - rect->left,
rect->bottom - rect->top );
}
/***********************************************************************
* GetRegionData (GDI32.217)
*
*/
DWORD WINAPI GetRegionData(HRGN32 hrgn, DWORD count, LPRGNDATA rgndata)
{
DWORD size;
RGNOBJ *obj = (RGNOBJ *) GDI_GetObjPtr( hrgn, REGION_MAGIC );
TRACE(region, " %04x count = %ld, rgndata = %p\n",
hrgn, count, rgndata);
if(!obj) return 0;
size = obj->rgn->numRects * sizeof(RECT32);
if(count < (size + sizeof(RGNDATAHEADER)) || rgndata == NULL)
{
GDI_HEAP_UNLOCK( hrgn );
return size + sizeof(RGNDATAHEADER);
}
rgndata->rdh.dwSize = sizeof(RGNDATAHEADER);
rgndata->rdh.iType = RDH_RECTANGLES;
rgndata->rdh.nCount = obj->rgn->numRects;
rgndata->rdh.nRgnSize = size;
rgndata->rdh.rcBound.left = obj->rgn->extents.left;
rgndata->rdh.rcBound.top = obj->rgn->extents.top;
rgndata->rdh.rcBound.right = obj->rgn->extents.right;
rgndata->rdh.rcBound.bottom = obj->rgn->extents.bottom;
memcpy( rgndata->Buffer, obj->rgn->rects, size );
GDI_HEAP_UNLOCK( hrgn );
return 1;
}
/***********************************************************************
* ExtCreateRegion (GDI32.94)
*
*/
HRGN32 WINAPI ExtCreateRegion( XFORM *lpXform, DWORD dwCount, RGNDATA *rgndata)
{
HRGN32 hrgn = CreateRectRgn32(0, 0, 0, 0);
RGNOBJ *obj = (RGNOBJ *) GDI_GetObjPtr( hrgn, REGION_MAGIC );
RECT32 *pCurRect, *pEndRect;
TRACE(region, " %p %ld %p. Returning %04x\n",
lpXform, dwCount, rgndata, hrgn);
if(!hrgn)
{
fprintf(stderr, "ExtCreateRegion can't create a region!\n");
return 0;
}
if(lpXform)
fprintf(stderr, "ExtCreateRegion: Xform not implemented - ignoring\n");
if(rgndata->rdh.iType != RDH_RECTANGLES)
{
fprintf(stderr, "ExtCreateRegion: type not RDH_RECTANGLES\n");
GDI_HEAP_UNLOCK( hrgn );
DeleteObject32( hrgn );
return 0;
}
pEndRect = (RECT32 *)rgndata->Buffer + rgndata->rdh.nCount;
for(pCurRect = (RECT32 *)rgndata->Buffer; pCurRect < pEndRect; pCurRect++)
REGION_UnionRectWithRegion( pCurRect, obj->rgn );
GDI_HEAP_UNLOCK( hrgn );
return hrgn;
}
/***********************************************************************
* PtInRegion16 (GDI.161)
*/
BOOL16 WINAPI PtInRegion16( HRGN16 hrgn, INT16 x, INT16 y )
{
return PtInRegion32( hrgn, x, y );
}
/***********************************************************************
* PtInRegion32 (GDI32.278)
*/
BOOL32 WINAPI PtInRegion32( HRGN32 hrgn, INT32 x, INT32 y )
{
RGNOBJ * obj;
if ((obj = (RGNOBJ *) GDI_GetObjPtr( hrgn, REGION_MAGIC )))
{
BOOL32 ret = FALSE;
int i;
if (obj->rgn->numRects > 0 && INRECT(obj->rgn->extents, x, y))
for (i = 0; i < obj->rgn->numRects; i++)
if (INRECT (obj->rgn->rects[i], x, y))
ret = TRUE;
GDI_HEAP_UNLOCK( hrgn );
return ret;
}
return FALSE;
}
/***********************************************************************
* RectInRegion16 (GDI.181)
*/
BOOL16 WINAPI RectInRegion16( HRGN16 hrgn, const RECT16 *rect )
{
RECT32 r32;
CONV_RECT16TO32(rect, &r32);
return (BOOL16)RectInRegion32(hrgn, &r32);
}
/***********************************************************************
* RectInRegion32 (GDI32.281)
*
* Returns TRUE if rect is at least partly inside hrgn
*/
BOOL32 WINAPI RectInRegion32( HRGN32 hrgn, const RECT32 *rect )
{
RGNOBJ * obj;
if ((obj = (RGNOBJ *) GDI_GetObjPtr( hrgn, REGION_MAGIC )))
{
RECT32 *pCurRect, *pRectEnd;
BOOL32 ret = FALSE;
/* this is (just) a useful optimization */
if ((obj->rgn->numRects > 0) && EXTENTCHECK(&obj->rgn->extents,
rect))
{
for (pCurRect = obj->rgn->rects, pRectEnd = pCurRect +
obj->rgn->numRects; pCurRect < pRectEnd; pCurRect++)
{
if (pCurRect->bottom <= rect->top)
continue; /* not far enough down yet */
if (pCurRect->top >= rect->bottom) {
ret = FALSE; /* too far down */
break;
}
if (pCurRect->right <= rect->left)
continue; /* not far enough over yet */
if (pCurRect->left >= rect->right) {
ret = FALSE; /* too far over */
break;
}
ret = TRUE;
break;
}
}
GDI_HEAP_UNLOCK(hrgn);
return ret;
}
return FALSE;
}
/***********************************************************************
* EqualRgn16 (GDI.72)
*/
BOOL16 WINAPI EqualRgn16( HRGN16 rgn1, HRGN16 rgn2 )
{
return EqualRgn32( rgn1, rgn2 );
}
/***********************************************************************
* EqualRgn32 (GDI32.90)
*/
BOOL32 WINAPI EqualRgn32( HRGN32 hrgn1, HRGN32 hrgn2 )
{
RGNOBJ *obj1, *obj2;
BOOL32 ret = FALSE;
if ((obj1 = (RGNOBJ *) GDI_GetObjPtr( hrgn1, REGION_MAGIC )))
{
if ((obj2 = (RGNOBJ *) GDI_GetObjPtr( hrgn2, REGION_MAGIC )))
{
int i;
ret = TRUE;
if ( obj1->rgn->numRects != obj2->rgn->numRects ) ret = FALSE;
else if ( obj1->rgn->numRects == 0 ) ret = TRUE;
else if ( !EqualRect32(&obj1->rgn->extents, &obj2->rgn->extents) )
ret = FALSE;
else for( i = 0; i < obj1->rgn->numRects; i++ ) {
if (!EqualRect32(obj1->rgn->rects + i, obj2->rgn->rects + i)) {
ret = FALSE;
break;
}
}
GDI_HEAP_UNLOCK(hrgn2);
}
GDI_HEAP_UNLOCK(hrgn1);
}
return ret;
}
/***********************************************************************
* REGION_UnionRectWithRegion
* Adds a rectangle to a WINEREGION
* See below for REGION_UnionRectWithRgn
*/
static void REGION_UnionRectWithRegion(const RECT32 *rect, WINEREGION *rgn)
{
WINEREGION region;
region.rects = ®ion.extents;
region.numRects = 1;
region.size = 1;
region.type = SIMPLEREGION;
CopyRect32(&(region.extents), rect);
REGION_UnionRegion(rgn, rgn, ®ion);
return;
}
/***********************************************************************
* REGION_UnionRectWithRgn
* Adds a rectangle to a HRGN32
* A helper used by scroll.c
*/
BOOL32 REGION_UnionRectWithRgn( HRGN32 hrgn, const RECT32 *lpRect )
{
RGNOBJ *obj = (RGNOBJ *) GDI_HEAP_LOCK( hrgn );
if(!obj) return FALSE;
REGION_UnionRectWithRegion( lpRect, obj->rgn );
GDI_HEAP_UNLOCK(hrgn);
return TRUE;
}
/***********************************************************************
* REGION_CreateFrameRgn
*
* Create a region that is a frame around another region.
* Expand all rectangles by +/- x and y, then subtract original region.
*/
BOOL32 REGION_FrameRgn( HRGN32 hDest, HRGN32 hSrc, INT32 x, INT32 y )
{
BOOL32 bRet;
RGNOBJ *srcObj = (RGNOBJ*) GDI_GetObjPtr( hSrc, REGION_MAGIC );
if (srcObj->rgn->numRects != 0)
{
RGNOBJ* destObj = (RGNOBJ*) GDI_GetObjPtr( hDest, REGION_MAGIC );
RECT32 *pRect, *pEndRect;
RECT32 tempRect;
EMPTY_REGION( destObj->rgn );
pEndRect = srcObj->rgn->rects + srcObj->rgn->numRects;
for(pRect = srcObj->rgn->rects; pRect < pEndRect; pRect++)
{
tempRect.left = pRect->left - x;
tempRect.top = pRect->top - y;
tempRect.right = pRect->right + x;
tempRect.bottom = pRect->bottom + y;
REGION_UnionRectWithRegion( &tempRect, destObj->rgn );
}
REGION_SubtractRegion( destObj->rgn, destObj->rgn, srcObj->rgn );
GDI_HEAP_UNLOCK ( hDest );
bRet = TRUE;
}
else
bRet = FALSE;
GDI_HEAP_UNLOCK( hSrc );
return bRet;
}
/***********************************************************************
* REGION_LPTODP
*
* Convert region to device co-ords for the supplied dc.
* Used by X11DRV_PaintRgn.
*/
BOOL32 REGION_LPTODP( HDC32 hdc, HRGN32 hDest, HRGN32 hSrc )
{
RECT32 *pCurRect, *pEndRect;
RGNOBJ *srcObj, *destObj;
DC * dc = DC_GetDCPtr( hdc );
RECT32 tmpRect;
TRACE(region, " hdc=%04x dest=%04x src=%04x\n",
hdc, hDest, hSrc) ;
if (dc->w.MapMode == MM_TEXT) /* Requires only a translation */
{
if( CombineRgn32( hDest, hSrc, 0, RGN_COPY ) == ERROR ) return FALSE;
OffsetRgn32( hDest, dc->vportOrgX - dc->wndOrgX,
dc->vportOrgY - dc->wndOrgY );
return TRUE;
}
if(!( srcObj = (RGNOBJ *) GDI_GetObjPtr( hSrc, REGION_MAGIC) ))
return FALSE;
if(!( destObj = (RGNOBJ *) GDI_GetObjPtr( hDest, REGION_MAGIC) ))
{
GDI_HEAP_UNLOCK( hSrc );
return FALSE;
}
EMPTY_REGION( destObj->rgn );
pEndRect = srcObj->rgn->rects + srcObj->rgn->numRects;
for(pCurRect = srcObj->rgn->rects; pCurRect < pEndRect; pCurRect++)
{
tmpRect = *pCurRect;
tmpRect.left = XLPTODP( dc, tmpRect.left );
tmpRect.top = YLPTODP( dc, tmpRect.top );
tmpRect.right = XLPTODP( dc, tmpRect.right );
tmpRect.bottom = YLPTODP( dc, tmpRect.bottom );
REGION_UnionRectWithRegion( &tmpRect, destObj->rgn );
}
GDI_HEAP_UNLOCK( hDest );
GDI_HEAP_UNLOCK( hSrc );
return TRUE;
}
/***********************************************************************
* CombineRgn16 (GDI.451)
*/
INT16 WINAPI CombineRgn16(HRGN16 hDest, HRGN16 hSrc1, HRGN16 hSrc2, INT16 mode)
{
return (INT16)CombineRgn32( hDest, hSrc1, hSrc2, mode );
}
/***********************************************************************
* CombineRgn32 (GDI32.19)
*
* Note: The behavior is correct even if src and dest regions are the same.
*/
INT32 WINAPI CombineRgn32(HRGN32 hDest, HRGN32 hSrc1, HRGN32 hSrc2, INT32 mode)
{
RGNOBJ *destObj = (RGNOBJ *) GDI_GetObjPtr( hDest, REGION_MAGIC);
INT32 result = ERROR;
TRACE(region, " %04x,%04x -> %04x mode=%x\n",
hSrc1, hSrc2, hDest, mode );
if (destObj)
{
RGNOBJ *src1Obj = (RGNOBJ *) GDI_GetObjPtr( hSrc1, REGION_MAGIC);
if (src1Obj)
{
TRACE(region, "dump:\n");
if(TRACE_ON(region))
REGION_DumpRegion(src1Obj->rgn);
if (mode == RGN_COPY)
{
REGION_CopyRegion( destObj->rgn, src1Obj->rgn );
result = destObj->rgn->type;
}
else
{
RGNOBJ *src2Obj = (RGNOBJ *) GDI_GetObjPtr( hSrc2, REGION_MAGIC);
if (src2Obj)
{
TRACE(region, "dump:\n");
if(TRACE_ON(region))
REGION_DumpRegion(src2Obj->rgn);
switch (mode)
{
case RGN_AND:
REGION_IntersectRegion( destObj->rgn, src1Obj->rgn, src2Obj->rgn);
break;
case RGN_OR:
REGION_UnionRegion( destObj->rgn, src1Obj->rgn, src2Obj->rgn );
break;
case RGN_XOR:
REGION_XorRegion( destObj->rgn, src1Obj->rgn, src2Obj->rgn );
break;
case RGN_DIFF:
REGION_SubtractRegion( destObj->rgn, src1Obj->rgn, src2Obj->rgn );
break;
}
result = destObj->rgn->type;
GDI_HEAP_UNLOCK( hSrc2 );
}
}
GDI_HEAP_UNLOCK( hSrc1 );
}
TRACE(region, "dump:\n");
if(TRACE_ON(region))
REGION_DumpRegion(destObj->rgn);
GDI_HEAP_UNLOCK( hDest );
}
return result;
}
/***********************************************************************
* REGION_SetExtents
* Re-calculate the extents of a region
*/
static void REGION_SetExtents (WINEREGION *pReg)
{
RECT32 *pRect, *pRectEnd, *pExtents;
if (pReg->numRects == 0)
{
pReg->extents.left = 0;
pReg->extents.top = 0;
pReg->extents.right = 0;
pReg->extents.bottom = 0;
return;
}
pExtents = &pReg->extents;
pRect = pReg->rects;
pRectEnd = &pRect[pReg->numRects - 1];
/*
* Since pRect is the first rectangle in the region, it must have the
* smallest top and since pRectEnd is the last rectangle in the region,
* it must have the largest bottom, because of banding. Initialize left and
* right from pRect and pRectEnd, resp., as good things to initialize them
* to...
*/
pExtents->left = pRect->left;
pExtents->top = pRect->top;
pExtents->right = pRectEnd->right;
pExtents->bottom = pRectEnd->bottom;
while (pRect <= pRectEnd)
{
if (pRect->left < pExtents->left)
pExtents->left = pRect->left;
if (pRect->right > pExtents->right)
pExtents->right = pRect->right;
pRect++;
}
}
/***********************************************************************
* REGION_CopyRegion
*/
static void REGION_CopyRegion(WINEREGION *dst, WINEREGION *src)
{
if (dst != src) /* don't want to copy to itself */
{
if (dst->size < src->numRects)
{
if (! (dst->rects = HeapReAlloc( SystemHeap, 0, dst->rects,
src->numRects * sizeof(RECT32) )))
return;
dst->size = src->numRects;
}
dst->numRects = src->numRects;
dst->extents.left = src->extents.left;
dst->extents.top = src->extents.top;
dst->extents.right = src->extents.right;
dst->extents.bottom = src->extents.bottom;
dst->type = src->type;
memcpy((char *) dst->rects, (char *) src->rects,
(int) (src->numRects * sizeof(RECT32)));
}
return;
}
/***********************************************************************
* REGION_Coalesce
*
* Attempt to merge the rects in the current band with those in the
* previous one. Used only by REGION_RegionOp.
*
* Results:
* The new index for the previous band.
*
* Side Effects:
* If coalescing takes place:
* - rectangles in the previous band will have their bottom fields
* altered.
* - pReg->numRects will be decreased.
*
*/
static INT32 REGION_Coalesce (
WINEREGION *pReg, /* Region to coalesce */
INT32 prevStart, /* Index of start of previous band */
INT32 curStart /* Index of start of current band */
) {
RECT32 *pPrevRect; /* Current rect in previous band */
RECT32 *pCurRect; /* Current rect in current band */
RECT32 *pRegEnd; /* End of region */
INT32 curNumRects; /* Number of rectangles in current band */
INT32 prevNumRects; /* Number of rectangles in previous band */
INT32 bandtop; /* top coordinate for current band */
pRegEnd = &pReg->rects[pReg->numRects];
pPrevRect = &pReg->rects[prevStart];
prevNumRects = curStart - prevStart;
/*
* Figure out how many rectangles are in the current band. Have to do
* this because multiple bands could have been added in REGION_RegionOp
* at the end when one region has been exhausted.
*/
pCurRect = &pReg->rects[curStart];
bandtop = pCurRect->top;
for (curNumRects = 0;
(pCurRect != pRegEnd) && (pCurRect->top == bandtop);
curNumRects++)
{
pCurRect++;
}
if (pCurRect != pRegEnd)
{
/*
* If more than one band was added, we have to find the start
* of the last band added so the next coalescing job can start
* at the right place... (given when multiple bands are added,
* this may be pointless -- see above).
*/
pRegEnd--;
while (pRegEnd[-1].top == pRegEnd->top)
{
pRegEnd--;
}
curStart = pRegEnd - pReg->rects;
pRegEnd = pReg->rects + pReg->numRects;
}
if ((curNumRects == prevNumRects) && (curNumRects != 0)) {
pCurRect -= curNumRects;
/*
* The bands may only be coalesced if the bottom of the previous
* matches the top scanline of the current.
*/
if (pPrevRect->bottom == pCurRect->top)
{
/*
* Make sure the bands have rects in the same places. This
* assumes that rects have been added in such a way that they
* cover the most area possible. I.e. two rects in a band must
* have some horizontal space between them.
*/
do
{
if ((pPrevRect->left != pCurRect->left) ||
(pPrevRect->right != pCurRect->right))
{
/*
* The bands don't line up so they can't be coalesced.
*/
return (curStart);
}
pPrevRect++;
pCurRect++;
prevNumRects -= 1;
} while (prevNumRects != 0);
pReg->numRects -= curNumRects;
pCurRect -= curNumRects;
pPrevRect -= curNumRects;
/*
* The bands may be merged, so set the bottom of each rect
* in the previous band to that of the corresponding rect in
* the current band.
*/
do
{
pPrevRect->bottom = pCurRect->bottom;
pPrevRect++;
pCurRect++;
curNumRects -= 1;
} while (curNumRects != 0);
/*
* If only one band was added to the region, we have to backup
* curStart to the start of the previous band.
*
* If more than one band was added to the region, copy the
* other bands down. The assumption here is that the other bands
* came from the same region as the current one and no further
* coalescing can be done on them since it's all been done
* already... curStart is already in the right place.
*/
if (pCurRect == pRegEnd)
{
curStart = prevStart;
}
else
{
do
{
*pPrevRect++ = *pCurRect++;
} while (pCurRect != pRegEnd);
}
}
}
return (curStart);
}
/***********************************************************************
* REGION_RegionOp
*
* Apply an operation to two regions. Called by REGION_Union,
* REGION_Inverse, REGION_Subtract, REGION_Intersect...
*
* Results:
* None.
*
* Side Effects:
* The new region is overwritten.
*
* 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.
*
*/
static void REGION_RegionOp(
WINEREGION *newReg, /* Place to store result */
WINEREGION *reg1, /* First region in operation */
WINEREGION *reg2, /* 2nd region in operation */
void (*overlapFunc)(), /* Function to call for over-lapping bands */
void (*nonOverlap1Func)(), /* Function to call for non-overlapping bands in region 1 */
void (*nonOverlap2Func)() /* Function to call for non-overlapping bands in region 2 */
) {
RECT32 *r1; /* Pointer into first region */
RECT32 *r2; /* Pointer into 2d region */
RECT32 *r1End; /* End of 1st region */
RECT32 *r2End; /* End of 2d region */
INT32 ybot; /* Bottom of intersection */
INT32 ytop; /* Top of intersection */
RECT32 *oldRects; /* Old rects for newReg */
INT32 prevBand; /* Index of start of
* previous band in newReg */
INT32 curBand; /* Index of start of current
* band in newReg */
RECT32 *r1BandEnd; /* End of current band in r1 */
RECT32 *r2BandEnd; /* End of current band in r2 */
INT32 top; /* Top of non-overlapping band */
INT32 bot; /* Bottom of non-overlapping band */
/*
* Initialization:
* set r1, r2, r1End and r2End appropriately, preserve the important
* parts 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 = reg1->rects;
r2 = reg2->rects;
r1End = r1 + reg1->numRects;
r2End = r2 + reg2->numRects;
oldRects = newReg->rects;
EMPTY_REGION(newReg);
/*
* Allocate a reasonable number of rectangles for the new region. The idea
* is to allocate enough so the individual functions don't need to
* reallocate and copy the array, which is time consuming, yet we don't
* have to worry about using too much memory. I hope to be able to
* nuke the Xrealloc() at the end of this function eventually.
*/
newReg->size = MAX(reg1->numRects,reg2->numRects) * 2;
if (! (newReg->rects = HeapAlloc( SystemHeap, 0,
sizeof(RECT32) * newReg->size )))
{
newReg->size = 0;
return;
}
/*
* Initialize ybot and ytop.
* In the upcoming 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.
*/
if (reg1->extents.top < reg2->extents.top)
ybot = reg1->extents.top;
else
ybot = reg2->extents.top;
/*
* prevBand serves to mark the start of the previous band so rectangles
* can be coalesced into larger rectangles. qv. miCoalesce, 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
{
curBand = newReg->numRects;
/*
* This algorithm proceeds one source-band (as opposed to a
* destination band, which is determined 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.
*/
r1BandEnd = r1;
while ((r1BandEnd != r1End) && (r1BandEnd->top == r1->top))
{
r1BandEnd++;
}
r2BandEnd = r2;
while ((r2BandEnd != r2End) && (r2BandEnd->top == r2->top))
{
r2BandEnd++;
}
/*
* 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 (r1->top < r2->top)
{
top = MAX(r1->top,ybot);
bot = MIN(r1->bottom,r2->top);
if ((top != bot) && (nonOverlap1Func != (void (*)())NULL))
{
(* nonOverlap1Func) (newReg, r1, r1BandEnd, top, bot);
}
ytop = r2->top;
}
else if (r2->top < r1->top)
{
top = MAX(r2->top,ybot);
bot = MIN(r2->bottom,r1->top);
if ((top != bot) && (nonOverlap2Func != (void (*)())NULL))
{
(* nonOverlap2Func) (newReg, r2, r2BandEnd, top, bot);
}
ytop = r1->top;
}
else
{
ytop = r1->top;
}
/*
* If any rectangles got added to the region, try and coalesce them
* with rectangles from the previous band. Note we could just do
* this test in miCoalesce, but some machines incur a not
* inconsiderable cost for function calls, so...
*/
if (newReg->numRects != curBand)
{
prevBand = REGION_Coalesce (newReg, prevBand, curBand);
}
/*
* Now see if we've hit an intersecting band. The two bands only
* intersect if ybot > ytop
*/
ybot = MIN(r1->bottom, r2->bottom);
curBand = newReg->numRects;
if (ybot > ytop)
{
(* overlapFunc) (newReg, r1, r1BandEnd, r2, r2BandEnd, ytop, ybot);
}
if (newReg->numRects != curBand)
{
prevBand = REGION_Coalesce (newReg, prevBand, curBand);
}
/*
* If we've finished with a band (bottom == ybot) we skip forward
* in the region to the next band.
*/
if (r1->bottom == ybot)
{
r1 = r1BandEnd;
}
if (r2->bottom == ybot)
{
r2 = r2BandEnd;
}
} while ((r1 != r1End) && (r2 != r2End));
/*
* Deal with whichever region still has rectangles left.
*/
curBand = newReg->numRects;
if (r1 != r1End)
{
if (nonOverlap1Func != (void (*)())NULL)
{
do
{
r1BandEnd = r1;
while ((r1BandEnd < r1End) && (r1BandEnd->top == r1->top))
{
r1BandEnd++;
}
(* nonOverlap1Func) (newReg, r1, r1BandEnd,
MAX(r1->top,ybot), r1->bottom);
r1 = r1BandEnd;
} while (r1 != r1End);
}
}
else if ((r2 != r2End) && (nonOverlap2Func != (void (*)())NULL))
{
do
{
r2BandEnd = r2;
while ((r2BandEnd < r2End) && (r2BandEnd->top == r2->top))
{
r2BandEnd++;
}
(* nonOverlap2Func) (newReg, r2, r2BandEnd,
MAX(r2->top,ybot), r2->bottom);
r2 = r2BandEnd;
} while (r2 != r2End);
}
if (newReg->numRects != curBand)
{
(void) REGION_Coalesce (newReg, prevBand, curBand);
}
/*
* A bit of cleanup. To keep regions from growing without bound,
* we shrink the array of rectangles to match the new number of
* rectangles in the region. This never goes to 0, however...
*
* Only do this stuff if the number of rectangles allocated is more than
* twice the number of rectangles in the region (a simple optimization...).
*/
if (newReg->numRects < (newReg->size >> 1))
{
if (REGION_NOT_EMPTY(newReg))
{
RECT32 *prev_rects = newReg->rects;
newReg->size = newReg->numRects;
newReg->rects = HeapReAlloc( SystemHeap, 0, newReg->rects,
sizeof(RECT32) * newReg->size );
if (! newReg->rects)
newReg->rects = prev_rects;
}
else
{
/*
* No point in doing the extra work involved in an Xrealloc if
* the region is empty
*/
newReg->size = 1;
HeapFree( SystemHeap, 0, newReg->rects );
newReg->rects = HeapAlloc( SystemHeap, 0, sizeof(RECT32) );
}
}
HeapFree( SystemHeap, 0, oldRects );
return;
}
/***********************************************************************
* Region Intersection
***********************************************************************/
/***********************************************************************
* REGION_IntersectO
*
* Handle an overlapping band for REGION_Intersect.
*
* Results:
* None.
*
* Side Effects:
* Rectangles may be added to the region.
*
*/
static void REGION_IntersectO(WINEREGION *pReg, RECT32 *r1, RECT32 *r1End,
RECT32 *r2, RECT32 *r2End, INT32 top, INT32 bottom)
{
INT32 left, right;
RECT32 *pNextRect;
pNextRect = &pReg->rects[pReg->numRects];
while ((r1 != r1End) && (r2 != r2End))
{
left = MAX(r1->left, r2->left);
right = MIN(r1->right, r2->right);
/*
* If there's any overlap between the two rectangles, add that
* overlap to the new region.
* There's no need to check for subsumption because the only way
* such a need could arise is if some region has two rectangles
* right next to each other. Since that should never happen...
*/
if (left < right)
{
MEMCHECK(pReg, pNextRect, pReg->rects);
pNextRect->left = left;
pNextRect->top = top;
pNextRect->right = right;
pNextRect->bottom = bottom;
pReg->numRects += 1;
pNextRect++;
}
/*
* Need to advance the pointers. Shift the one that extends
* to the right the least, since the other still has a chance to
* overlap with that region's next rectangle, if you see what I mean.
*/
if (r1->right < r2->right)
{
r1++;
}
else if (r2->right < r1->right)
{
r2++;
}
else
{
r1++;
r2++;
}
}
return;
}
/***********************************************************************
* REGION_IntersectRegion
*/
static void REGION_IntersectRegion(WINEREGION *newReg, WINEREGION *reg1,
WINEREGION *reg2)
{
/* check for trivial reject */
if ( (!(reg1->numRects)) || (!(reg2->numRects)) ||
(!EXTENTCHECK(®1->extents, ®2->extents)))
newReg->numRects = 0;
else
REGION_RegionOp (newReg, reg1, reg2,
(voidProcp) REGION_IntersectO, (voidProcp) NULL, (voidProcp) NULL);
/*
* Can't alter newReg's extents before we call miRegionOp because
* it might be one of the source regions and miRegionOp depends
* on the extents of those regions being the same. Besides, this
* way there's no checking against rectangles that will be nuked
* due to coalescing, so we have to examine fewer rectangles.
*/
REGION_SetExtents(newReg);
newReg->type = (newReg->numRects) ? COMPLEXREGION : NULLREGION ;
return;
}
/***********************************************************************
* Region Union
***********************************************************************/
/***********************************************************************
* REGION_UnionNonO
*
* Handle a non-overlapping band for the union operation. Just
* Adds the rectangles into the region. Doesn't have to check for
* subsumption or anything.
*
* Results:
* None.
*
* Side Effects:
* pReg->numRects is incremented and the final rectangles overwritten
* with the rectangles we're passed.
*
*/
static void REGION_UnionNonO (WINEREGION *pReg, RECT32 *r, RECT32 *rEnd,
INT32 top, INT32 bottom)
{
RECT32 *pNextRect;
pNextRect = &pReg->rects[pReg->numRects];
while (r != rEnd)
{
MEMCHECK(pReg, pNextRect, pReg->rects);
pNextRect->left = r->left;
pNextRect->top = top;
pNextRect->right = r->right;
pNextRect->bottom = bottom;
pReg->numRects += 1;
pNextRect++;
r++;
}
return;
}
/***********************************************************************
* REGION_UnionO
*
* Handle an overlapping band for the union operation. Picks the
* left-most rectangle each time and merges it into the region.
*
* Results:
* None.
*
* Side Effects:
* Rectangles are overwritten in pReg->rects and pReg->numRects will
* be changed.
*
*/
static void REGION_UnionO (WINEREGION *pReg, RECT32 *r1, RECT32 *r1End,
RECT32 *r2, RECT32 *r2End, INT32 top, INT32 bottom)
{
RECT32 *pNextRect;
pNextRect = &pReg->rects[pReg->numRects];
#define MERGERECT(r) \
if ((pReg->numRects != 0) && \
(pNextRect[-1].top == top) && \
(pNextRect[-1].bottom == bottom) && \
(pNextRect[-1].right >= r->left)) \
{ \
if (pNextRect[-1].right < r->right) \
{ \
pNextRect[-1].right = r->right; \
} \
} \
else \
{ \
MEMCHECK(pReg, pNextRect, pReg->rects); \
pNextRect->top = top; \
pNextRect->bottom = bottom; \
pNextRect->left = r->left; \
pNextRect->right = r->right; \
pReg->numRects += 1; \
pNextRect += 1; \
} \
r++;
while ((r1 != r1End) && (r2 != r2End))
{
if (r1->left < r2->left)
{
MERGERECT(r1);
}
else
{
MERGERECT(r2);
}
}
if (r1 != r1End)
{
do
{
MERGERECT(r1);
} while (r1 != r1End);
}
else while (r2 != r2End)
{
MERGERECT(r2);
}
return;
}
/***********************************************************************
* REGION_UnionRegion
*/
static void REGION_UnionRegion(WINEREGION *newReg, WINEREGION *reg1,
WINEREGION *reg2)
{
/* checks all the simple cases */
/*
* Region 1 and 2 are the same or region 1 is empty
*/
if ( (reg1 == reg2) || (!(reg1->numRects)) )
{
if (newReg != reg2)
REGION_CopyRegion(newReg, reg2);
return;
}
/*
* if nothing to union (region 2 empty)
*/
if (!(reg2->numRects))
{
if (newReg != reg1)
REGION_CopyRegion(newReg, reg1);
return;
}
/*
* Region 1 completely subsumes region 2
*/
if ((reg1->numRects == 1) &&
(reg1->extents.left <= reg2->extents.left) &&
(reg1->extents.top <= reg2->extents.top) &&
(reg1->extents.right >= reg2->extents.right) &&
(reg1->extents.bottom >= reg2->extents.bottom))
{
if (newReg != reg1)
REGION_CopyRegion(newReg, reg1);
return;
}
/*
* Region 2 completely subsumes region 1
*/
if ((reg2->numRects == 1) &&
(reg2->extents.left <= reg1->extents.left) &&
(reg2->extents.top <= reg1->extents.top) &&
(reg2->extents.right >= reg1->extents.right) &&
(reg2->extents.bottom >= reg1->extents.bottom))
{
if (newReg != reg2)
REGION_CopyRegion(newReg, reg2);
return;
}
REGION_RegionOp (newReg, reg1, reg2, (voidProcp) REGION_UnionO,
(voidProcp) REGION_UnionNonO, (voidProcp) REGION_UnionNonO);
newReg->extents.left = MIN(reg1->extents.left, reg2->extents.left);
newReg->extents.top = MIN(reg1->extents.top, reg2->extents.top);
newReg->extents.right = MAX(reg1->extents.right, reg2->extents.right);
newReg->extents.bottom = MAX(reg1->extents.bottom, reg2->extents.bottom);
newReg->type = (newReg->numRects) ? COMPLEXREGION : NULLREGION ;
return;
}
/***********************************************************************
* Region Subtraction
***********************************************************************/
/***********************************************************************
* REGION_SubtractNonO1
*
* Deal with non-overlapping band for subtraction. Any parts from
* region 2 we discard. Anything from region 1 we add to the region.
*
* Results:
* None.
*
* Side Effects:
* pReg may be affected.
*
*/
static void REGION_SubtractNonO1 (WINEREGION *pReg, RECT32 *r, RECT32 *rEnd,
INT32 top, INT32 bottom)
{
RECT32 *pNextRect;
pNextRect = &pReg->rects[pReg->numRects];
while (r != rEnd)
{
MEMCHECK(pReg, pNextRect, pReg->rects);
pNextRect->left = r->left;
pNextRect->top = top;
pNextRect->right = r->right;
pNextRect->bottom = bottom;
pReg->numRects += 1;
pNextRect++;
r++;
}
return;
}
/***********************************************************************
* REGION_SubtractO
*
* Overlapping band subtraction. x1 is the left-most point not yet
* checked.
*
* Results:
* None.
*
* Side Effects:
* pReg may have rectangles added to it.
*
*/
static void REGION_SubtractO (WINEREGION *pReg, RECT32 *r1, RECT32 *r1End,
RECT32 *r2, RECT32 *r2End, INT32 top, INT32 bottom)
{
RECT32 *pNextRect;
INT32 left;
left = r1->left;
pNextRect = &pReg->rects[pReg->numRects];
while ((r1 != r1End) && (r2 != r2End))
{
if (r2->right <= left)
{
/*
* Subtrahend missed the boat: go to next subtrahend.
*/
r2++;
}
else if (r2->left <= left)
{
/*
* Subtrahend preceeds minuend: nuke left edge of minuend.
*/
left = r2->right;
if (left >= r1->right)
{
/*
* Minuend completely covered: advance to next minuend and
* reset left fence to edge of new minuend.
*/
r1++;
if (r1 != r1End)
left = r1->left;
}
else
{
/*
* Subtrahend now used up since it doesn't extend beyond
* minuend
*/
r2++;
}
}
else if (r2->left < r1->right)
{
/*
* Left part of subtrahend covers part of minuend: add uncovered
* part of minuend to region and skip to next subtrahend.
*/
MEMCHECK(pReg, pNextRect, pReg->rects);
pNextRect->left = left;
pNextRect->top = top;
pNextRect->right = r2->left;
pNextRect->bottom = bottom;
pReg->numRects += 1;
pNextRect++;
left = r2->right;
if (left >= r1->right)
{
/*
* Minuend used up: advance to new...
*/
r1++;
if (r1 != r1End)
left = r1->left;
}
else
{
/*
* Subtrahend used up
*/
r2++;
}
}
else
{
/*
* Minuend used up: add any remaining piece before advancing.
*/
if (r1->right > left)
{
MEMCHECK(pReg, pNextRect, pReg->rects);
pNextRect->left = left;
pNextRect->top = top;
pNextRect->right = r1->right;
pNextRect->bottom = bottom;
pReg->numRects += 1;
pNextRect++;
}
r1++;
left = r1->left;
}
}
/*
* Add remaining minuend rectangles to region.
*/
while (r1 != r1End)
{
MEMCHECK(pReg, pNextRect, pReg->rects);
pNextRect->left = left;
pNextRect->top = top;
pNextRect->right = r1->right;
pNextRect->bottom = bottom;
pReg->numRects += 1;
pNextRect++;
r1++;
if (r1 != r1End)
{
left = r1->left;
}
}
return;
}
/***********************************************************************
* REGION_SubtractRegion
*
* Subtract regS from regM and leave the result in regD.
* S stands for subtrahend, M for minuend and D for difference.
*
* Results:
* TRUE.
*
* Side Effects:
* regD is overwritten.
*
*/
static void REGION_SubtractRegion(WINEREGION *regD, WINEREGION *regM,
WINEREGION *regS )
{
/* check for trivial reject */
if ( (!(regM->numRects)) || (!(regS->numRects)) ||
(!EXTENTCHECK(®M->extents, ®S->extents)) )
{
REGION_CopyRegion(regD, regM);
return;
}
REGION_RegionOp (regD, regM, regS, (voidProcp) REGION_SubtractO,
(voidProcp) REGION_SubtractNonO1, (voidProcp) NULL);
/*
* Can't alter newReg's extents before we call miRegionOp because
* it might be one of the source regions and miRegionOp depends
* on the extents of those regions being the unaltered. Besides, this
* way there's no checking against rectangles that will be nuked
* due to coalescing, so we have to examine fewer rectangles.
*/
REGION_SetExtents (regD);
regD->type = (regD->numRects) ? COMPLEXREGION : NULLREGION ;
return;
}
/***********************************************************************
* REGION_XorRegion
*/
static void REGION_XorRegion(WINEREGION *dr, WINEREGION *sra,
WINEREGION *srb)
{
WINEREGION *tra, *trb;
if ((! (tra = REGION_AllocWineRegion())) ||
(! (trb = REGION_AllocWineRegion())))
return;
REGION_SubtractRegion(tra,sra,srb);
REGION_SubtractRegion(trb,srb,sra);
REGION_UnionRegion(dr,tra,trb);
REGION_DestroyWineRegion(tra);
REGION_DestroyWineRegion(trb);
return;
}
/**************************************************************************
*
* Poly Regions
*
*************************************************************************/
#define LARGE_COORDINATE 0x7fffffff /* FIXME */
#define SMALL_COORDINATE 0x80000000
/***********************************************************************
* REGION_InsertEdgeInET
*
* Insert the given edge into the edge table.
* First we must find the correct bucket in the
* Edge table, then find the right slot in the
* bucket. Finally, we can insert it.
*
*/
static void REGION_InsertEdgeInET(EdgeTable *ET, EdgeTableEntry *ETE,
INT32 scanline, ScanLineListBlock **SLLBlock, INT32 *iSLLBlock)
{
EdgeTableEntry *start, *prev;
ScanLineList *pSLL, *pPrevSLL;
ScanLineListBlock *tmpSLLBlock;
/*
* find the right bucket to put the edge into
*/
pPrevSLL = &ET->scanlines;
pSLL = pPrevSLL->next;
while (pSLL && (pSLL->scanline < scanline))
{
pPrevSLL = pSLL;
pSLL = pSLL->next;
}
/*
* reassign pSLL (pointer to ScanLineList) if necessary
*/
if ((!pSLL) || (pSLL->scanline > scanline))
{
if (*iSLLBlock > SLLSPERBLOCK-1)
{
tmpSLLBlock = HeapAlloc( SystemHeap, 0, sizeof(ScanLineListBlock));
if(!tmpSLLBlock)
{
fprintf(stderr, "REGION_InsertEdgeInET(): Can't alloc SLLB\n");
return;
}
(*SLLBlock)->next = tmpSLLBlock;
tmpSLLBlock->next = (ScanLineListBlock *)NULL;
*SLLBlock = tmpSLLBlock;
*iSLLBlock = 0;
}
pSLL = &((*SLLBlock)->SLLs[(*iSLLBlock)++]);
pSLL->next = pPrevSLL->next;
pSLL->edgelist = (EdgeTableEntry *)NULL;
pPrevSLL->next = pSLL;
}
pSLL->scanline = scanline;
/*
* now insert the edge in the right bucket
*/
prev = (EdgeTableEntry *)NULL;
start = pSLL->edgelist;
while (start && (start->bres.minor_axis < ETE->bres.minor_axis))
{
prev = start;
start = start->next;
}
ETE->next = start;
if (prev)
prev->next = ETE;
else
pSLL->edgelist = ETE;
}
/***********************************************************************
* REGION_CreateEdgeTable
*
* This routine creates the edge table for
* scan converting polygons.
* The Edge Table (ET) looks like:
*
* EdgeTable
* --------
* | ymax | ScanLineLists
* |scanline|-->------------>-------------->...
* -------- |scanline| |scanline|
* |edgelist| |edgelist|
* --------- ---------
* | |
* | |
* V V
* list of ETEs list of ETEs
*
* where ETE is an EdgeTableEntry data structure,
* and there is one ScanLineList per scanline at
* which an edge is initially entered.
*
*/
static void REGION_CreateETandAET(const INT32 *Count, INT32 nbpolygons,
const POINT32 *pts, EdgeTable *ET, EdgeTableEntry *AET,
EdgeTableEntry *pETEs, ScanLineListBlock *pSLLBlock)
{
const POINT32 *top, *bottom;
const POINT32 *PrevPt, *CurrPt, *EndPt;
INT32 poly, count;
int iSLLBlock = 0;
int dy;
/*
* initialize the Active Edge Table
*/
AET->next = (EdgeTableEntry *)NULL;
AET->back = (EdgeTableEntry *)NULL;
AET->nextWETE = (EdgeTableEntry *)NULL;
AET->bres.minor_axis = SMALL_COORDINATE;
/*
* initialize the Edge Table.
*/
ET->scanlines.next = (ScanLineList *)NULL;
ET->ymax = SMALL_COORDINATE;
ET->ymin = LARGE_COORDINATE;
pSLLBlock->next = (ScanLineListBlock *)NULL;
EndPt = pts - 1;
for(poly = 0; poly < nbpolygons; poly++)
{
count = Count[poly];
EndPt += count;
if(count < 2)
continue;
PrevPt = EndPt;
/*
* for each vertex in the array of points.
* In this loop we are dealing with two vertices at
* a time -- these make up one edge of the polygon.
*/
while (count--)
{
CurrPt = pts++;
/*
* find out which point is above and which is below.
*/
if (PrevPt->y > CurrPt->y)
{
bottom = PrevPt, top = CurrPt;
pETEs->ClockWise = 0;
}
else
{
bottom = CurrPt, top = PrevPt;
pETEs->ClockWise = 1;
}
/*
* don't add horizontal edges to the Edge table.
*/
if (bottom->y != top->y)
{
pETEs->ymax = bottom->y-1;
/* -1 so we don't get last scanline */
/*
* initialize integer edge algorithm
*/
dy = bottom->y - top->y;
BRESINITPGONSTRUCT(dy, top->x, bottom->x, pETEs->bres);
REGION_InsertEdgeInET(ET, pETEs, top->y, &pSLLBlock,
&iSLLBlock);
if (PrevPt->y > ET->ymax)
ET->ymax = PrevPt->y;
if (PrevPt->y < ET->ymin)
ET->ymin = PrevPt->y;
pETEs++;
}
PrevPt = CurrPt;
}
}
}
/***********************************************************************
* REGION_loadAET
*
* This routine moves EdgeTableEntries from the
* EdgeTable into the Active Edge Table,
* leaving them sorted by smaller x coordinate.
*
*/
static void REGION_loadAET(EdgeTableEntry *AET, EdgeTableEntry *ETEs)
{
EdgeTableEntry *pPrevAET;
EdgeTableEntry *tmp;
pPrevAET = AET;
AET = AET->next;
while (ETEs)
{
while (AET && (AET->bres.minor_axis < ETEs->bres.minor_axis))
{
pPrevAET = AET;
AET = AET->next;
}
tmp = ETEs->next;
ETEs->next = AET;
if (AET)
AET->back = ETEs;
ETEs->back = pPrevAET;
pPrevAET->next = ETEs;
pPrevAET = ETEs;
ETEs = tmp;
}
}
/***********************************************************************
* REGION_computeWAET
*
* This routine links the AET by the
* nextWETE (winding EdgeTableEntry) link for
* use by the winding number rule. The final
* Active Edge Table (AET) might look something
* like:
*
* AET
* ---------- --------- ---------
* |ymax | |ymax | |ymax |
* | ... | |... | |... |
* |next |->|next |->|next |->...
* |nextWETE| |nextWETE| |nextWETE|
* --------- --------- ^--------
* | | |
* V-------------------> V---> ...
*
*/
static void REGION_computeWAET(EdgeTableEntry *AET)
{
register EdgeTableEntry *pWETE;
register int inside = 1;
register int isInside = 0;
AET->nextWETE = (EdgeTableEntry *)NULL;
pWETE = AET;
AET = AET->next;
while (AET)
{
if (AET->ClockWise)
isInside++;
else
isInside--;
if ((!inside && !isInside) ||
( inside && isInside))
{
pWETE->nextWETE = AET;
pWETE = AET;
inside = !inside;
}
AET = AET->next;
}
pWETE->nextWETE = (EdgeTableEntry *)NULL;
}
/***********************************************************************
* REGION_InsertionSort
*
* Just a simple insertion sort using
* pointers and back pointers to sort the Active
* Edge Table.
*
*/
static BOOL32 REGION_InsertionSort(EdgeTableEntry *AET)
{
EdgeTableEntry *pETEchase;
EdgeTableEntry *pETEinsert;
EdgeTableEntry *pETEchaseBackTMP;
BOOL32 changed = FALSE;
AET = AET->next;
while (AET)
{
pETEinsert = AET;
pETEchase = AET;
while (pETEchase->back->bres.minor_axis > AET->bres.minor_axis)
pETEchase = pETEchase->back;
AET = AET->next;
if (pETEchase != pETEinsert)
{
pETEchaseBackTMP = pETEchase->back;
pETEinsert->back->next = AET;
if (AET)
AET->back = pETEinsert->back;
pETEinsert->next = pETEchase;
pETEchase->back->next = pETEinsert;
pETEchase->back = pETEinsert;
pETEinsert->back = pETEchaseBackTMP;
changed = TRUE;
}
}
return changed;
}
/***********************************************************************
* REGION_FreeStorage
*
* Clean up our act.
*/
static void REGION_FreeStorage(ScanLineListBlock *pSLLBlock)
{
ScanLineListBlock *tmpSLLBlock;
while (pSLLBlock)
{
tmpSLLBlock = pSLLBlock->next;
HeapFree( SystemHeap, 0, pSLLBlock );
pSLLBlock = tmpSLLBlock;
}
}
/***********************************************************************
* REGION_PtsToRegion
*
* Create an array of rectangles from a list of points.
*/
static int REGION_PtsToRegion(int numFullPtBlocks, int iCurPtBlock,
POINTBLOCK *FirstPtBlock, WINEREGION *reg)
{
RECT32 *rects;
POINT32 *pts;
POINTBLOCK *CurPtBlock;
int i;
RECT32 *extents;
INT32 numRects;
extents = ®->extents;
numRects = ((numFullPtBlocks * NUMPTSTOBUFFER) + iCurPtBlock) >> 1;
if (!(reg->rects = HeapReAlloc( SystemHeap, 0, reg->rects,
sizeof(RECT32) * numRects )))
return(0);
reg->size = numRects;
CurPtBlock = FirstPtBlock;
rects = reg->rects - 1;
numRects = 0;
extents->left = LARGE_COORDINATE, extents->right = SMALL_COORDINATE;
for ( ; numFullPtBlocks >= 0; numFullPtBlocks--) {
/* the loop uses 2 points per iteration */
i = NUMPTSTOBUFFER >> 1;
if (!numFullPtBlocks)
i = iCurPtBlock >> 1;
for (pts = CurPtBlock->pts; i--; pts += 2) {
if (pts->x == pts[1].x)
continue;
if (numRects && pts->x == rects->left && pts->y == rects->bottom &&
pts[1].x == rects->right &&
(numRects == 1 || rects[-1].top != rects->top) &&
(i && pts[2].y > pts[1].y)) {
rects->bottom = pts[1].y + 1;
continue;
}
numRects++;
rects++;
rects->left = pts->x; rects->top = pts->y;
rects->right = pts[1].x; rects->bottom = pts[1].y + 1;
if (rects->left < extents->left)
extents->left = rects->left;
if (rects->right > extents->right)
extents->right = rects->right;
}
CurPtBlock = CurPtBlock->next;
}
if (numRects) {
extents->top = reg->rects->top;
extents->bottom = rects->bottom;
} else {
extents->left = 0;
extents->top = 0;
extents->right = 0;
extents->bottom = 0;
}
reg->numRects = numRects;
return(TRUE);
}
/***********************************************************************
* CreatePolyPolygonRgn32 (GDI32.57)
*/
HRGN32 WINAPI CreatePolyPolygonRgn32(const POINT32 *Pts, const INT32 *Count,
INT32 nbpolygons, INT32 mode)
{
HRGN32 hrgn;
RGNOBJ *obj;
WINEREGION *region;
register EdgeTableEntry *pAET; /* Active Edge Table */
register INT32 y; /* current scanline */
register int iPts = 0; /* number of pts in buffer */
register EdgeTableEntry *pWETE; /* Winding Edge Table Entry*/
register ScanLineList *pSLL; /* current scanLineList */
register POINT32 *pts; /* output buffer */
EdgeTableEntry *pPrevAET; /* ptr to previous AET */
EdgeTable ET; /* header node for ET */
EdgeTableEntry AET; /* header node for AET */
EdgeTableEntry *pETEs; /* EdgeTableEntries pool */
ScanLineListBlock SLLBlock; /* header for scanlinelist */
int fixWAET = FALSE;
POINTBLOCK FirstPtBlock, *curPtBlock; /* PtBlock buffers */
POINTBLOCK *tmpPtBlock;
int numFullPtBlocks = 0;
INT32 poly, total;
if(!(hrgn = REGION_CreateRegion()))
return 0;
obj = (RGNOBJ *) GDI_GetObjPtr( hrgn, REGION_MAGIC );
region = obj->rgn;
/* special case a rectangle */
if (((nbpolygons == 1) && ((*Count == 4) ||
((*Count == 5) && (Pts[4].x == Pts[0].x) && (Pts[4].y == Pts[0].y)))) &&
(((Pts[0].y == Pts[1].y) &&
(Pts[1].x == Pts[2].x) &&
(Pts[2].y == Pts[3].y) &&
(Pts[3].x == Pts[0].x)) ||
((Pts[0].x == Pts[1].x) &&
(Pts[1].y == Pts[2].y) &&
(Pts[2].x == Pts[3].x) &&
(Pts[3].y == Pts[0].y))))
{
SetRectRgn32( hrgn, MIN(Pts[0].x, Pts[2].x), MIN(Pts[0].y, Pts[2].y),
MAX(Pts[0].x, Pts[2].x), MAX(Pts[0].y, Pts[2].y) );
GDI_HEAP_UNLOCK( hrgn );
return hrgn;
}
for(poly = total = 0; poly < nbpolygons; poly++)
total += Count[poly];
if (! (pETEs = HeapAlloc( SystemHeap, 0, sizeof(EdgeTableEntry) * total )))
{
REGION_DeleteObject( hrgn, obj );
return 0;
}
pts = FirstPtBlock.pts;
REGION_CreateETandAET(Count, nbpolygons, Pts, &ET, &AET, pETEs, &SLLBlock);
pSLL = ET.scanlines.next;
curPtBlock = &FirstPtBlock;
if (mode != WINDING) {
/*
* for each scanline
*/
for (y = ET.ymin; y < ET.ymax; y++) {
/*
* Add a new edge to the active edge table when we
* get to the next edge.
*/
if (pSLL != NULL && y == pSLL->scanline) {
REGION_loadAET(&AET, pSLL->edgelist);
pSLL = pSLL->next;
}
pPrevAET = &AET;
pAET = AET.next;
/*
* for each active edge
*/
while (pAET) {
pts->x = pAET->bres.minor_axis, pts->y = y;
pts++, iPts++;
/*
* send out the buffer
*/
if (iPts == NUMPTSTOBUFFER) {
tmpPtBlock = HeapAlloc( SystemHeap, 0, sizeof(POINTBLOCK));
if(!tmpPtBlock) {
fprintf(stderr,
"CreatePolyPolygonRgn(): can't alloc tPB\n");
return 0;
}
curPtBlock->next = tmpPtBlock;
curPtBlock = tmpPtBlock;
pts = curPtBlock->pts;
numFullPtBlocks++;
iPts = 0;
}
EVALUATEEDGEEVENODD(pAET, pPrevAET, y);
}
REGION_InsertionSort(&AET);
}
}
else {
/*
* for each scanline
*/
for (y = ET.ymin; y < ET.ymax; y++) {
/*
* Add a new edge to the active edge table when we
* get to the next edge.
*/
if (pSLL != NULL && y == pSLL->scanline) {
REGION_loadAET(&AET, pSLL->edgelist);
REGION_computeWAET(&AET);
pSLL = pSLL->next;
}
pPrevAET = &AET;
pAET = AET.next;
pWETE = pAET;
/*
* for each active edge
*/
while (pAET) {
/*
* add to the buffer only those edges that
* are in the Winding active edge table.
*/
if (pWETE == pAET) {
pts->x = pAET->bres.minor_axis, pts->y = y;
pts++, iPts++;
/*
* send out the buffer
*/
if (iPts == NUMPTSTOBUFFER) {
tmpPtBlock = HeapAlloc( SystemHeap, 0,
sizeof(POINTBLOCK) );
if(!tmpPtBlock) {
fprintf(stderr,
"CreatePolyPolygonRgn(): can't alloc tPB\n");
return 0;
}
curPtBlock->next = tmpPtBlock;
curPtBlock = tmpPtBlock;
pts = curPtBlock->pts;
numFullPtBlocks++; iPts = 0;
}
pWETE = pWETE->nextWETE;
}
EVALUATEEDGEWINDING(pAET, pPrevAET, y, fixWAET);
}
/*
* recompute the winding active edge table if
* we just resorted or have exited an edge.
*/
if (REGION_InsertionSort(&AET) || fixWAET) {
REGION_computeWAET(&AET);
fixWAET = FALSE;
}
}
}
REGION_FreeStorage(SLLBlock.next);
REGION_PtsToRegion(numFullPtBlocks, iPts, &FirstPtBlock, region);
for (curPtBlock = FirstPtBlock.next; --numFullPtBlocks >= 0;) {
tmpPtBlock = curPtBlock->next;
HeapFree( SystemHeap, 0, curPtBlock );
curPtBlock = tmpPtBlock;
}
HeapFree( SystemHeap, 0, pETEs );
GDI_HEAP_UNLOCK( hrgn );
return hrgn;
}
/***********************************************************************
* CreatePolygonRgn16 (GDI.63)
*/
HRGN16 WINAPI CreatePolygonRgn16( const POINT16 * points, INT16 count,
INT16 mode )
{
return CreatePolyPolygonRgn16( points, &count, 1, mode );
}
/***********************************************************************
* CreatePolyPolygonRgn16 (GDI.451)
*/
HRGN16 WINAPI CreatePolyPolygonRgn16( const POINT16 *points,
const INT16 *count, INT16 nbpolygons, INT16 mode )
{
HRGN32 hrgn;
int i, npts = 0;
INT32 *count32;
POINT32 *points32;
for (i = 0; i < nbpolygons; i++)
npts += count[i];
points32 = HeapAlloc( SystemHeap, 0, npts * sizeof(POINT32) );
for (i = 0; i < npts; i++)
CONV_POINT16TO32( &(points[i]), &(points32[i]) );
count32 = HeapAlloc( SystemHeap, 0, nbpolygons * sizeof(INT32) );
for (i = 0; i < nbpolygons; i++)
count32[i] = count[i];
hrgn = CreatePolyPolygonRgn32( points32, count32, nbpolygons, mode );
HeapFree( SystemHeap, 0, count32 );
HeapFree( SystemHeap, 0, points32 );
return hrgn;
}
/***********************************************************************
* CreatePolygonRgn32 (GDI32.58)
*/
HRGN32 WINAPI CreatePolygonRgn32( const POINT32 *points, INT32 count,
INT32 mode )
{
return CreatePolyPolygonRgn32( points, &count, 1, mode );
}
|