Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 3998 lines (3544 sloc) 95.481 kb
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 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998
#define PERL_NO_GET_CONTEXT
#ifdef __cplusplus
extern "C" {
#endif
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#define NEED_newRV_noinc
#define NEED_sv_2pv_nolen
#define NEED_sv_2pvbyte
#include "ppport.h"
#ifdef __cplusplus
}
#endif

#define i_int_hlines_testing() 1

#include "imager.h"
#include "feat.h"
#include "dynaload.h"
#include "regmach.h"
#include "imextdef.h"
#include "imextpltypes.h"
#include <float.h>

#if i_int_hlines_testing()
#include "imageri.h"
#endif

#include "imperl.h"

/* used to represent channel lists parameters */
typedef struct i_channel_list_tag {
  int *channels;
  int count;
} i_channel_list;

typedef struct {
  size_t count;
  const i_sample_t *samples;
} i_sample_list;

typedef struct {
  size_t count;
  const i_fsample_t *samples;
} i_fsample_list;

/*

Allocate memory that will be discarded when mortals are discarded.

*/

static void *
malloc_temp(pTHX_ size_t size) {
  SV *sv = sv_2mortal(newSV(size));

  return SvPVX(sv);
}

/* These functions are all shared - then comes platform dependant code */
static int getstr(void *hv_t,char *key,char **store) {
  dTHX;
  SV** svpp;
  HV* hv=(HV*)hv_t;

  mm_log((1,"getstr(hv_t %p, key %s, store %p)\n",hv_t,key,store));

  if ( !hv_exists(hv,key,strlen(key)) ) return 0;

  svpp=hv_fetch(hv, key, strlen(key), 0);
  *store=SvPV(*svpp, PL_na );

  return 1;
}

static int getint(void *hv_t,char *key,int *store) {
  dTHX;
  SV** svpp;
  HV* hv=(HV*)hv_t;

  mm_log((1,"getint(hv_t %p, key %s, store %p)\n",hv_t,key,store));

  if ( !hv_exists(hv,key,strlen(key)) ) return 0;

  svpp=hv_fetch(hv, key, strlen(key), 0);
  *store=(int)SvIV(*svpp);
  return 1;
}

static int getdouble(void *hv_t,char* key,double *store) {
  dTHX;
  SV** svpp;
  HV* hv=(HV*)hv_t;

  mm_log((1,"getdouble(hv_t %p, key %s, store %p)\n",hv_t,key,store));

  if ( !hv_exists(hv,key,strlen(key)) ) return 0;
  svpp=hv_fetch(hv, key, strlen(key), 0);
  *store=(double)SvNV(*svpp);
  return 1;
}

static int getvoid(void *hv_t,char* key,void **store) {
  dTHX;
  SV** svpp;
  HV* hv=(HV*)hv_t;

  mm_log((1,"getvoid(hv_t %p, key %s, store %p)\n",hv_t,key,store));

  if ( !hv_exists(hv,key,strlen(key)) ) return 0;

  svpp=hv_fetch(hv, key, strlen(key), 0);
  *store = INT2PTR(void*, SvIV(*svpp));

  return 1;
}

static int getobj(void *hv_t,char *key,char *type,void **store) {
  dTHX;
  SV** svpp;
  HV* hv=(HV*)hv_t;

  mm_log((1,"getobj(hv_t %p, key %s,type %s, store %p)\n",hv_t,key,type,store));

  if ( !hv_exists(hv,key,strlen(key)) ) return 0;

  svpp=hv_fetch(hv, key, strlen(key), 0);

  if (sv_derived_from(*svpp,type)) {
    IV tmp = SvIV((SV*)SvRV(*svpp));
    *store = INT2PTR(void*, tmp);
  } else {
    mm_log((1,"getobj: key exists in hash but is not of correct type"));
    return 0;
  }

  return 1;
}

UTIL_table_t i_UTIL_table={getstr,getint,getdouble,getvoid,getobj};

void my_SvREFCNT_dec(void *p) {
  dTHX;
  SvREFCNT_dec((SV*)p);
}


static void
i_log_entry(char *string, int level) {
  mm_log((level, "%s", string));
}

static SV *
make_i_color_sv(pTHX_ const i_color *c) {
  SV *sv;
  i_color *col = mymalloc(sizeof(i_color));
  *col = *c;
  sv = sv_newmortal();
  sv_setref_pv(sv, "Imager::Color", (void *)col);

  return sv;
}

#define CBDATA_BUFSIZE 8192

struct cbdata {
  /* the SVs we use to call back to Perl */
  SV *writecb;
  SV *readcb;
  SV *seekcb;
  SV *closecb;
};

static ssize_t
call_reader(struct cbdata *cbd, void *buf, size_t size,
            size_t maxread) {
  dTHX;
  int count;
  int result;
  SV *data;
  dSP;

  if (!SvOK(cbd->readcb)) {
    mm_log((1, "read callback called but no readcb supplied\n"));
    i_push_error(0, "read callback called but no readcb supplied");
    return -1;
  }

  ENTER;
  SAVETMPS;
  EXTEND(SP, 2);
  PUSHMARK(SP);
  PUSHs(sv_2mortal(newSViv(size)));
  PUSHs(sv_2mortal(newSViv(maxread)));
  PUTBACK;

  count = perl_call_sv(cbd->readcb, G_SCALAR);

  SPAGAIN;

  if (count != 1)
    croak("Result of perl_call_sv(..., G_SCALAR) != 1");

  data = POPs;

  if (SvOK(data)) {
    STRLEN len;
    char *ptr = SvPVbyte(data, len);
    if (len > maxread)
      croak("Too much data returned in reader callback (wanted %d, got %d, expected %d)",
      (int)size, (int)len, (int)maxread);
    
    memcpy(buf, ptr, len);
    result = len;
  }
  else {
    result = -1;
  }

  PUTBACK;
  FREETMPS;
  LEAVE;

  return result;
}

static off_t
io_seeker(void *p, off_t offset, int whence) {
  dTHX;
  struct cbdata *cbd = p;
  int count;
  off_t result;
  dSP;

  if (!SvOK(cbd->seekcb)) {
    mm_log((1, "seek callback called but no seekcb supplied\n"));
    i_push_error(0, "seek callback called but no seekcb supplied");
    return -1;
  }

  ENTER;
  SAVETMPS;
  EXTEND(SP, 2);
  PUSHMARK(SP);
  PUSHs(sv_2mortal(newSViv(offset)));
  PUSHs(sv_2mortal(newSViv(whence)));
  PUTBACK;

  count = perl_call_sv(cbd->seekcb, G_SCALAR);

  SPAGAIN;

  if (count != 1)
    croak("Result of perl_call_sv(..., G_SCALAR) != 1");

  result = POPi;

  PUTBACK;
  FREETMPS;
  LEAVE;

  return result;
}

static ssize_t
io_writer(void *p, void const *data, size_t size) {
  dTHX;
  struct cbdata *cbd = p;
  I32 count;
  SV *sv;
  dSP;
  bool success;

  if (!SvOK(cbd->writecb)) {
    mm_log((1, "write callback called but no writecb supplied\n"));
    i_push_error(0, "write callback called but no writecb supplied");
    return -1;
  }

  ENTER;
  SAVETMPS;
  EXTEND(SP, 1);
  PUSHMARK(SP);
  PUSHs(sv_2mortal(newSVpv((char *)data, size)));
  PUTBACK;

  count = perl_call_sv(cbd->writecb, G_SCALAR);

  SPAGAIN;
  if (count != 1)
    croak("Result of perl_call_sv(..., G_SCALAR) != 1");

  sv = POPs;
  success = SvTRUE(sv);


  PUTBACK;
  FREETMPS;
  LEAVE;

  return success ? size : -1;
}

static ssize_t
io_reader(void *p, void *data, size_t size) {
  struct cbdata *cbd = p;

  return call_reader(cbd, data, size, size);
}

static int io_closer(void *p) {
  dTHX;
  struct cbdata *cbd = p;
  int success = 1;

  if (SvOK(cbd->closecb)) {
    dSP;
    I32 count;
    SV *sv;

    ENTER;
    SAVETMPS;
    PUSHMARK(SP);
    PUTBACK;

    count = perl_call_sv(cbd->closecb, G_SCALAR);

    SPAGAIN;
    
    sv = POPs;
    success = SvTRUE(sv);

    PUTBACK;
    FREETMPS;
    LEAVE;
  }

  return success ? 0 : -1;
}

static void io_destroyer(void *p) {
  dTHX;
  struct cbdata *cbd = p;

  SvREFCNT_dec(cbd->writecb);
  SvREFCNT_dec(cbd->readcb);
  SvREFCNT_dec(cbd->seekcb);
  SvREFCNT_dec(cbd->closecb);
  myfree(cbd);
}

static i_io_glue_t *
do_io_new_buffer(pTHX_ SV *data_sv) {
  const char *data;
  STRLEN length;

  data = SvPVbyte(data_sv, length);
  SvREFCNT_inc(data_sv);
  return io_new_buffer(data, length, my_SvREFCNT_dec, data_sv);
}

static const char *
describe_sv(SV *sv) {
  if (SvOK(sv)) {
    if (SvROK(sv)) {
      svtype type = SvTYPE(SvRV(sv));
      switch (type) {
      case SVt_PVCV: return "CV";
      case SVt_PVGV: return "GV";
      case SVt_PVLV: return "LV";
      default: return "some reference";
      }
    }
    else {
      return "non-reference scalar";
    }
  }
  else {
    return "undef";
  }
}

static i_io_glue_t *
do_io_new_cb(pTHX_ SV *writecb, SV *readcb, SV *seekcb, SV *closecb) {
  struct cbdata *cbd;

  cbd = mymalloc(sizeof(struct cbdata));
  cbd->writecb = newSVsv(writecb);
  cbd->readcb = newSVsv(readcb);
  cbd->seekcb = newSVsv(seekcb);
  cbd->closecb = newSVsv(closecb);

  mm_log((1, "do_io_new_cb(writecb %p (%s), readcb %p (%s), seekcb %p (%s), closecb %p (%s))\n", writecb, describe_sv(writecb), readcb, describe_sv(readcb), seekcb, describe_sv(seekcb), closecb, describe_sv(closecb)));

  return io_new_cb(cbd, io_reader, io_writer, io_seeker, io_closer,
io_destroyer);
}

struct value_name {
  char *name;
  int value;
};
static int lookup_name(struct value_name *names, int count, char *name, int def_value)
{
  int i;
  for (i = 0; i < count; ++i)
    if (strEQ(names[i].name, name))
      return names[i].value;

  return def_value;
}
static struct value_name transp_names[] =
{
  { "none", tr_none },
  { "threshold", tr_threshold },
  { "errdiff", tr_errdiff },
  { "ordered", tr_ordered, },
};

static struct value_name make_color_names[] =
{
  { "none", mc_none, },
  { "webmap", mc_web_map, },
  { "addi", mc_addi, },
  { "mediancut", mc_median_cut, },
  { "mono", mc_mono, },
  { "monochrome", mc_mono, },
  { "gray", mc_gray, },
  { "gray4", mc_gray4, },
  { "gray16", mc_gray16, },
};

static struct value_name translate_names[] =
{
  { "giflib", pt_giflib, },
  { "closest", pt_closest, },
  { "perturb", pt_perturb, },
  { "errdiff", pt_errdiff, },
};

static struct value_name errdiff_names[] =
{
  { "floyd", ed_floyd, },
  { "jarvis", ed_jarvis, },
  { "stucki", ed_stucki, },
  { "custom", ed_custom, },
};

static struct value_name orddith_names[] =
{
  { "random", od_random, },
  { "dot8", od_dot8, },
  { "dot4", od_dot4, },
  { "hline", od_hline, },
  { "vline", od_vline, },
  { "/line", od_slashline, },
  { "slashline", od_slashline, },
  { "\\line", od_backline, },
  { "backline", od_backline, },
  { "tiny", od_tiny, },
  { "custom", od_custom, },
};

/* look through the hash for quantization options */
static void
ip_handle_quant_opts(pTHX_ i_quantize *quant, HV *hv)
{
  /*** POSSIBLY BROKEN: do I need to unref the SV from hv_fetch ***/
  SV **sv;
  int i;
  STRLEN len;
  char *str;

  quant->mc_colors = mymalloc(quant->mc_size * sizeof(i_color));

  sv = hv_fetch(hv, "transp", 6, 0);
  if (sv && *sv && (str = SvPV(*sv, len))) {
    quant->transp =
      lookup_name(transp_names, sizeof(transp_names)/sizeof(*transp_names),
str, tr_none);
    if (quant->transp != tr_none) {
      quant->tr_threshold = 127;
      sv = hv_fetch(hv, "tr_threshold", 12, 0);
      if (sv && *sv)
quant->tr_threshold = SvIV(*sv);
    }
    if (quant->transp == tr_errdiff) {
      sv = hv_fetch(hv, "tr_errdiff", 10, 0);
      if (sv && *sv && (str = SvPV(*sv, len)))
quant->tr_errdiff = lookup_name(errdiff_names, sizeof(errdiff_names)/sizeof(*errdiff_names), str, ed_floyd);
    }
    if (quant->transp == tr_ordered) {
      quant->tr_orddith = od_tiny;
      sv = hv_fetch(hv, "tr_orddith", 10, 0);
      if (sv && *sv && (str = SvPV(*sv, len)))
quant->tr_orddith = lookup_name(orddith_names, sizeof(orddith_names)/sizeof(*orddith_names), str, od_random);

      if (quant->tr_orddith == od_custom) {
sv = hv_fetch(hv, "tr_map", 6, 0);
if (sv && *sv && SvTYPE(SvRV(*sv)) == SVt_PVAV) {
AV *av = (AV*)SvRV(*sv);
len = av_len(av) + 1;
if (len > sizeof(quant->tr_custom))
len = sizeof(quant->tr_custom);
for (i = 0; i < len; ++i) {
SV **sv2 = av_fetch(av, i, 0);
if (sv2 && *sv2) {
quant->tr_custom[i] = SvIV(*sv2);
}
}
while (i < sizeof(quant->tr_custom))
quant->tr_custom[i++] = 0;
}
      }
    }
  }
  quant->make_colors = mc_median_cut;
  sv = hv_fetch(hv, "make_colors", 11, 0);
  if (sv && *sv && (str = SvPV(*sv, len))) {
    quant->make_colors =
      lookup_name(make_color_names, sizeof(make_color_names)/sizeof(*make_color_names), str, mc_median_cut);
  }
  sv = hv_fetch(hv, "colors", 6, 0);
  if (sv && *sv && SvROK(*sv) && SvTYPE(SvRV(*sv)) == SVt_PVAV) {
    /* needs to be an array of Imager::Color
note that the caller allocates the mc_color array and sets mc_size
to it's size */
    AV *av = (AV *)SvRV(*sv);
    quant->mc_count = av_len(av)+1;
    if (quant->mc_count > quant->mc_size)
      quant->mc_count = quant->mc_size;
    for (i = 0; i < quant->mc_count; ++i) {
      SV **sv1 = av_fetch(av, i, 0);
      if (sv1 && *sv1 && SvROK(*sv1) && sv_derived_from(*sv1, "Imager::Color")) {
i_color *col = INT2PTR(i_color *, SvIV((SV*)SvRV(*sv1)));
quant->mc_colors[i] = *col;
      }
    }
  }
  sv = hv_fetch(hv, "max_colors", 10, 0);
  if (sv && *sv) {
    i = SvIV(*sv);
    if (i <= quant->mc_size && i >= quant->mc_count)
      quant->mc_size = i;
  }

  quant->translate = pt_closest;
  sv = hv_fetch(hv, "translate", 9, 0);
  if (sv && *sv && (str = SvPV(*sv, len))) {
    quant->translate = lookup_name(translate_names, sizeof(translate_names)/sizeof(*translate_names), str, pt_closest);
  }
  sv = hv_fetch(hv, "errdiff", 7, 0);
  if (sv && *sv && (str = SvPV(*sv, len))) {
    quant->errdiff = lookup_name(errdiff_names, sizeof(errdiff_names)/sizeof(*errdiff_names), str, ed_floyd);
  }
  if (quant->translate == pt_errdiff && quant->errdiff == ed_custom) {
    /* get the error diffusion map */
    sv = hv_fetch(hv, "errdiff_width", 13, 0);
    if (sv && *sv)
      quant->ed_width = SvIV(*sv);
    sv = hv_fetch(hv, "errdiff_height", 14, 0);
    if (sv && *sv)
      quant->ed_height = SvIV(*sv);
    sv = hv_fetch(hv, "errdiff_orig", 12, 0);
    if (sv && *sv)
      quant->ed_orig = SvIV(*sv);
    if (quant->ed_width > 0 && quant->ed_height > 0) {
      int sum = 0;
      quant->ed_map = mymalloc(sizeof(int)*quant->ed_width*quant->ed_height);
      sv = hv_fetch(hv, "errdiff_map", 11, 0);
      if (sv && *sv && SvROK(*sv) && SvTYPE(SvRV(*sv)) == SVt_PVAV) {
AV *av = (AV*)SvRV(*sv);
len = av_len(av) + 1;
if (len > quant->ed_width * quant->ed_height)
len = quant->ed_width * quant->ed_height;
for (i = 0; i < len; ++i) {
SV **sv2 = av_fetch(av, i, 0);
if (sv2 && *sv2) {
quant->ed_map[i] = SvIV(*sv2);
sum += quant->ed_map[i];
}
}
      }
      if (!sum) {
/* broken map */
myfree(quant->ed_map);
quant->ed_map = 0;
quant->errdiff = ed_floyd;
      }
    }
  }
  sv = hv_fetch(hv, "perturb", 7, 0);
  if (sv && *sv)
    quant->perturb = SvIV(*sv);
}

static void
ip_cleanup_quant_opts(pTHX_ i_quantize *quant) {
  myfree(quant->mc_colors);
  if (quant->ed_map)
    myfree(quant->ed_map);
}

/* copies the color map from the hv into the colors member of the HV */
static void
ip_copy_colors_back(pTHX_ HV *hv, i_quantize *quant) {
  SV **sv;
  AV *av;
  int i;
  SV *work;

  sv = hv_fetch(hv, "colors", 6, 0);
  if (!sv || !*sv || !SvROK(*sv) || SvTYPE(SvRV(*sv)) != SVt_PVAV) {
    /* nothing to do */
    return;
  }

  av = (AV *)SvRV(*sv);
  av_clear(av);
  av_extend(av, quant->mc_count+1);
  for (i = 0; i < quant->mc_count; ++i) {
    i_color *in = quant->mc_colors+i;
    Imager__Color c = ICL_new_internal(in->rgb.r, in->rgb.g, in->rgb.b, 255);
    work = sv_newmortal();
    sv_setref_pv(work, "Imager::Color", (void *)c);
    SvREFCNT_inc(work);
    av_push(av, work);
  }
}

/* loads the segments of a fountain fill into an array */
static i_fountain_seg *
load_fount_segs(pTHX_ AV *asegs, int *count) {
  /* Each element of segs must contain:
[ start, middle, end, c0, c1, segtype, colortrans ]
start, middle, end are doubles from 0 to 1
c0, c1 are Imager::Color::Float or Imager::Color objects
segtype, colortrans are ints
*/
  int i, j;
  AV *aseg;
  i_fountain_seg *segs;
  double work[3];
  int worki[2];

  *count = av_len(asegs)+1;
  if (*count < 1)
    croak("i_fountain must have at least one segment");
  segs = mymalloc(sizeof(i_fountain_seg) * *count);
  for(i = 0; i < *count; i++) {
    SV **sv1 = av_fetch(asegs, i, 0);
    if (!sv1 || !*sv1 || !SvROK(*sv1)
        || SvTYPE(SvRV(*sv1)) != SVt_PVAV) {
      myfree(segs);
      croak("i_fountain: segs must be an arrayref of arrayrefs");
    }
    aseg = (AV *)SvRV(*sv1);
    if (av_len(aseg) != 7-1) {
      myfree(segs);
      croak("i_fountain: a segment must have 7 members");
    }
    for (j = 0; j < 3; ++j) {
      SV **sv2 = av_fetch(aseg, j, 0);
      if (!sv2 || !*sv2) {
        myfree(segs);
        croak("i_fountain: XS error");
      }
      work[j] = SvNV(*sv2);
    }
    segs[i].start = work[0];
    segs[i].middle = work[1];
    segs[i].end = work[2];
    for (j = 0; j < 2; ++j) {
      SV **sv3 = av_fetch(aseg, 3+j, 0);
      if (!sv3 || !*sv3 || !SvROK(*sv3) ||
          (!sv_derived_from(*sv3, "Imager::Color")
           && !sv_derived_from(*sv3, "Imager::Color::Float"))) {
        myfree(segs);
        croak("i_fountain: segs must contain colors in elements 3 and 4");
      }
      if (sv_derived_from(*sv3, "Imager::Color::Float")) {
        segs[i].c[j] = *INT2PTR(i_fcolor *, SvIV((SV *)SvRV(*sv3)));
      }
      else {
        i_color c = *INT2PTR(i_color *, SvIV((SV *)SvRV(*sv3)));
        int ch;
        for (ch = 0; ch < MAXCHANNELS; ++ch) {
          segs[i].c[j].channel[ch] = c.channel[ch] / 255.0;
        }
      }
    }
    for (j = 0; j < 2; ++j) {
      SV **sv2 = av_fetch(aseg, j+5, 0);
      if (!sv2 || !*sv2) {
        myfree(segs);
        croak("i_fountain: XS error");
      }
      worki[j] = SvIV(*sv2);
    }
    segs[i].type = worki[0];
    segs[i].color = worki[1];
  }

  return segs;
}

/* validates the indexes supplied to i_ppal

i_ppal() doesn't do that for speed, but I'm not comfortable doing that
for calls from perl.

*/
static void
validate_i_ppal(i_img *im, i_palidx const *indexes, int count) {
  int color_count = i_colorcount(im);
  int i;

  if (color_count == -1)
    croak("i_plin() called on direct color image");
  
  for (i = 0; i < count; ++i) {
    if (indexes[i] >= color_count) {
      croak("i_plin() called with out of range color index %d (max %d)",
        indexes[i], color_count-1);
    }
  }
}


/* I don't think ICLF_* names belong at the C interface
this makes the XS code think we have them, to let us avoid
putting function bodies in the XS code
*/
#define ICLF_new_internal(r, g, b, a) i_fcolor_new((r), (g), (b), (a))
#define ICLF_DESTROY(cl) i_fcolor_destroy(cl)

#ifdef IMAGER_LOG
#define i_log_enabled() 1
#else
#define i_log_enabled() 0
#endif

#if i_int_hlines_testing()

typedef i_int_hlines *Imager__Internal__Hlines;

static i_int_hlines *
i_int_hlines_new(i_img_dim start_y, i_img_dim count_y, i_img_dim start_x, i_img_dim count_x) {
  i_int_hlines *result = mymalloc(sizeof(i_int_hlines));
  i_int_init_hlines(result, start_y, count_y, start_x, count_x);

  return result;
}

static i_int_hlines *
i_int_hlines_new_img(i_img *im) {
  i_int_hlines *result = mymalloc(sizeof(i_int_hlines));
  i_int_init_hlines_img(result, im);

  return result;
}

static void
i_int_hlines_DESTROY(i_int_hlines *hlines) {
  i_int_hlines_destroy(hlines);
  myfree(hlines);
}

#define i_int_hlines_CLONE_SKIP(cls) 1

static int seg_compare(const void *vleft, const void *vright) {
  const i_int_hline_seg *left = vleft;
  const i_int_hline_seg *right = vright;

  return left->minx - right->minx;
}

static SV *
i_int_hlines_dump(i_int_hlines *hlines) {
  dTHX;
  SV *dump = newSVpvf("start_y: %" i_DF " limit_y: %" i_DF " start_x: %" i_DF " limit_x: %" i_DF"\n",
i_DFc(hlines->start_y), i_DFc(hlines->limit_y), i_DFc(hlines->start_x), i_DFc(hlines->limit_x));
  i_img_dim y;
  
  for (y = hlines->start_y; y < hlines->limit_y; ++y) {
    i_int_hline_entry *entry = hlines->entries[y-hlines->start_y];
    if (entry) {
      int i;
      /* sort the segments, if any */
      if (entry->count)
        qsort(entry->segs, entry->count, sizeof(i_int_hline_seg), seg_compare);

      sv_catpvf(dump, " %" i_DF " (%" i_DF "):", i_DFc(y), i_DFc(entry->count));
      for (i = 0; i < entry->count; ++i) {
        sv_catpvf(dump, " [%" i_DF ", %" i_DF ")", i_DFc(entry->segs[i].minx),
                  i_DFc(entry->segs[i].x_limit));
      }
      sv_catpv(dump, "\n");
    }
  }

  return dump;
}

#endif

static off_t
i_sv_off_t(pTHX_ SV *sv) {
#if LSEEKSIZE > IVSIZE
  return (off_t)SvNV(sv);
#else
  return (off_t)SvIV(sv);
#endif
}

static SV *
i_new_sv_off_t(pTHX_ off_t off) {
#if LSEEKSIZE > IVSIZE
  return newSVnv(off);
#else
  return newSViv(off);
#endif
}

static im_pl_ext_funcs im_perl_funcs =
{
  IMAGER_PL_API_VERSION,
  IMAGER_PL_API_LEVEL,
  ip_handle_quant_opts,
  ip_cleanup_quant_opts,
  ip_copy_colors_back
};

#define PERL_PL_SET_GLOBAL_CALLBACKS \
sv_setiv(get_sv(PERL_PL_FUNCTION_TABLE_NAME, 1), PTR2IV(&im_perl_funcs));

#define IIM_new i_img_8_new
#define IIM_DESTROY i_img_destroy

#ifdef IMEXIF_ENABLE
#define i_exif_enabled() 1
#else
#define i_exif_enabled() 0
#endif

/* trying to use more C style names, map them here */
#define i_io_DESTROY(ig) io_glue_destroy(ig)

#define i_img_get_width(im) ((im)->xsize)
#define i_img_get_height(im) ((im)->ysize)

#define i_img_epsilonf() (DBL_EPSILON * 4)

/* avoid some xsubpp strangeness */
#define NEWLINE '\n'

MODULE = Imager PACKAGE = Imager::Color PREFIX = ICL_

Imager::Color
ICL_new_internal(r,g,b,a)
               unsigned char r
               unsigned char g
               unsigned char b
               unsigned char a

void
ICL_DESTROY(cl)
               Imager::Color cl


void
ICL_set_internal(cl,r,g,b,a)
               Imager::Color cl
               unsigned char r
               unsigned char g
               unsigned char b
               unsigned char a
PPCODE:
ICL_set_internal(cl, r, g, b, a);
EXTEND(SP, 1);
PUSHs(ST(0));

void
ICL_info(cl)
               Imager::Color cl


void
ICL_rgba(cl)
Imager::Color cl
PPCODE:
EXTEND(SP, 4);
PUSHs(sv_2mortal(newSVnv(cl->rgba.r)));
PUSHs(sv_2mortal(newSVnv(cl->rgba.g)));
PUSHs(sv_2mortal(newSVnv(cl->rgba.b)));
PUSHs(sv_2mortal(newSVnv(cl->rgba.a)));

Imager::Color
i_hsv_to_rgb(c)
        Imager::Color c
      CODE:
        RETVAL = mymalloc(sizeof(i_color));
        *RETVAL = *c;
        i_hsv_to_rgb(RETVAL);
      OUTPUT:
        RETVAL
        
Imager::Color
i_rgb_to_hsv(c)
        Imager::Color c
      CODE:
        RETVAL = mymalloc(sizeof(i_color));
        *RETVAL = *c;
        i_rgb_to_hsv(RETVAL);
      OUTPUT:
        RETVAL
        


MODULE = Imager PACKAGE = Imager::Color::Float PREFIX=ICLF_

Imager::Color::Float
ICLF_new_internal(r, g, b, a)
        double r
        double g
        double b
        double a

void
ICLF_DESTROY(cl)
        Imager::Color::Float cl

void
ICLF_rgba(cl)
        Imager::Color::Float cl
      PREINIT:
        int ch;
      PPCODE:
        EXTEND(SP, MAXCHANNELS);
        for (ch = 0; ch < MAXCHANNELS; ++ch) {
        /* printf("%d: %g\n", ch, cl->channel[ch]); */
          PUSHs(sv_2mortal(newSVnv(cl->channel[ch])));
        }

void
ICLF_set_internal(cl,r,g,b,a)
        Imager::Color::Float cl
        double r
        double g
        double b
        double a
      PPCODE:
        cl->rgba.r = r;
        cl->rgba.g = g;
        cl->rgba.b = b;
        cl->rgba.a = a;
        EXTEND(SP, 1);
        PUSHs(ST(0));

Imager::Color::Float
i_hsv_to_rgb(c)
        Imager::Color::Float c
      CODE:
        RETVAL = mymalloc(sizeof(i_fcolor));
        *RETVAL = *c;
        i_hsv_to_rgbf(RETVAL);
      OUTPUT:
        RETVAL
        
Imager::Color::Float
i_rgb_to_hsv(c)
        Imager::Color::Float c
      CODE:
        RETVAL = mymalloc(sizeof(i_fcolor));
        *RETVAL = *c;
        i_rgb_to_hsvf(RETVAL);
      OUTPUT:
        RETVAL

MODULE = Imager PACKAGE = Imager::ImgRaw PREFIX = IIM_

Imager::ImgRaw
IIM_new(x,y,ch)
               i_img_dim x
i_img_dim y
int ch

void
IIM_DESTROY(im)
               Imager::ImgRaw im



MODULE = Imager PACKAGE = Imager

PROTOTYPES: ENABLE


Imager::IO
io_new_fd(fd)
                         int fd

Imager::IO
io_new_bufchain()


Imager::IO
io_new_buffer(data_sv)
SV *data_sv
CODE:
RETVAL = do_io_new_buffer(aTHX_ data_sv);
        OUTPUT:
          RETVAL

Imager::IO
io_new_cb(writecb, readcb, seekcb, closecb, maxwrite = CBDATA_BUFSIZE)
        SV *writecb;
        SV *readcb;
        SV *seekcb;
        SV *closecb;
        int maxwrite;
      CODE:
        RETVAL = do_io_new_cb(aTHX_ writecb, readcb, seekcb, closecb);
      OUTPUT:
        RETVAL

SV *
io_slurp(ig)
        Imager::IO ig
PREINIT:
unsigned char* data;
size_t tlength;
CODE:
  data = NULL;
              tlength = io_slurp(ig, &data);
              RETVAL = newSVpv((char *)data,tlength);
              myfree(data);
OUTPUT:
RETVAL


undef_int
i_set_image_file_limits(width, height, bytes)
i_img_dim width
i_img_dim height
size_t bytes

void
i_get_image_file_limits()
      PREINIT:
        i_img_dim width, height;
size_t bytes;
      PPCODE:
        if (i_get_image_file_limits(&width, &height, &bytes)) {
EXTEND(SP, 3);
          PUSHs(sv_2mortal(newSViv(width)));
          PUSHs(sv_2mortal(newSViv(height)));
          PUSHs(sv_2mortal(newSVuv(bytes)));
        }

bool
i_int_check_image_file_limits(width, height, channels, sample_size)
i_img_dim width
i_img_dim height
int channels
size_t sample_size
  PROTOTYPE: DISABLE

MODULE = Imager PACKAGE = Imager::IO PREFIX = io_

Imager::IO
io_new_fd(class, fd)
int fd
    CODE:
RETVAL = io_new_fd(fd);
    OUTPUT:
RETVAL

Imager::IO
io_new_buffer(class, data_sv)
SV *data_sv
    CODE:
        RETVAL = do_io_new_buffer(aTHX_ data_sv);
    OUTPUT:
        RETVAL

Imager::IO
io_new_cb(class, writecb, readcb, seekcb, closecb)
        SV *writecb;
        SV *readcb;
        SV *seekcb;
        SV *closecb;
    CODE:
        RETVAL = do_io_new_cb(aTHX_ writecb, readcb, seekcb, closecb);
    OUTPUT:
        RETVAL

Imager::IO
io_new_bufchain(class)
    CODE:
RETVAL = io_new_bufchain();
    OUTPUT:
        RETVAL

SV *
io_slurp(class, ig)
        Imager::IO ig
    PREINIT:
unsigned char* data;
size_t tlength;
    CODE:
data = NULL;
tlength = io_slurp(ig, &data);
RETVAL = newSVpv((char *)data,tlength);
myfree(data);
    OUTPUT:
RETVAL

MODULE = Imager PACKAGE = Imager::IO PREFIX = i_io_

IV
i_io_raw_write(ig, data_sv)
Imager::IO ig
SV *data_sv
      PREINIT:
        void *data;
STRLEN size;
      CODE:
#ifdef SvUTF8
        if (SvUTF8(data_sv)) {
data_sv = sv_2mortal(newSVsv(data_sv));
          /* yes, we want this to croak() if the SV can't be downgraded */
sv_utf8_downgrade(data_sv, FALSE);
}
#endif
data = SvPV(data_sv, size);
        RETVAL = i_io_raw_write(ig, data, size);
      OUTPUT:
RETVAL

void
i_io_raw_read(ig, buffer_sv, size)
Imager::IO ig
SV *buffer_sv
IV size
      PREINIT:
        void *buffer;
ssize_t result;
      PPCODE:
        if (size <= 0)
croak("size negative in call to i_io_raw_read()");
        /* prevent an undefined value warning if they supplied an
undef buffer.
Orginally conditional on !SvOK(), but this will prevent the
downgrade from croaking */
sv_setpvn(buffer_sv, "", 0);
#ifdef SvUTF8
if (SvUTF8(buffer_sv))
          sv_utf8_downgrade(buffer_sv, FALSE);
#endif
buffer = SvGROW(buffer_sv, size+1);
        result = i_io_raw_read(ig, buffer, size);
        if (result >= 0) {
SvCUR_set(buffer_sv, result);
*SvEND(buffer_sv) = '\0';
SvPOK_only(buffer_sv);
EXTEND(SP, 1);
PUSHs(sv_2mortal(newSViv(result)));
}
ST(1) = buffer_sv;
SvSETMAGIC(ST(1));

void
i_io_raw_read2(ig, size)
Imager::IO ig
IV size
      PREINIT:
SV *buffer_sv;
        void *buffer;
ssize_t result;
      PPCODE:
        if (size <= 0)
croak("size negative in call to i_io_read2()");
buffer_sv = newSV(size);
buffer = SvGROW(buffer_sv, size+1);
        result = i_io_raw_read(ig, buffer, size);
        if (result >= 0) {
SvCUR_set(buffer_sv, result);
*SvEND(buffer_sv) = '\0';
SvPOK_only(buffer_sv);
EXTEND(SP, 1);
PUSHs(sv_2mortal(buffer_sv));
}
else {
          /* discard it */
SvREFCNT_dec(buffer_sv);
        }

off_t
i_io_raw_seek(ig, position, whence)
Imager::IO ig
off_t position
int whence

int
i_io_raw_close(ig)
Imager::IO ig

void
i_io_DESTROY(ig)
        Imager::IO ig

int
i_io_CLONE_SKIP(...)
    CODE:
        (void)items; /* avoid unused warning for XS variable */
RETVAL = 1;
    OUTPUT:
RETVAL

int
i_io_getc(ig)
Imager::IO ig

int
i_io_putc(ig, c)
Imager::IO ig
        int c

int
i_io_close(ig)
Imager::IO ig

int
i_io_flush(ig)
Imager::IO ig

int
i_io_peekc(ig)
Imager::IO ig

int
i_io_seek(ig, off, whence)
Imager::IO ig
off_t off
        int whence

void
i_io_peekn(ig, size)
Imager::IO ig
STRLEN size
      PREINIT:
SV *buffer_sv;
        void *buffer;
ssize_t result;
      PPCODE:
buffer_sv = newSV(size+1);
buffer = SvGROW(buffer_sv, size+1);
        result = i_io_peekn(ig, buffer, size);
        if (result >= 0) {
SvCUR_set(buffer_sv, result);
*SvEND(buffer_sv) = '\0';
SvPOK_only(buffer_sv);
EXTEND(SP, 1);
PUSHs(sv_2mortal(buffer_sv));
}
else {
          /* discard it */
SvREFCNT_dec(buffer_sv);
        }

void
i_io_read(ig, buffer_sv, size)
Imager::IO ig
SV *buffer_sv
IV size
      PREINIT:
        void *buffer;
ssize_t result;
      PPCODE:
        if (size <= 0)
croak("size negative in call to i_io_read()");
        /* prevent an undefined value warning if they supplied an
undef buffer.
Orginally conditional on !SvOK(), but this will prevent the
downgrade from croaking */
sv_setpvn(buffer_sv, "", 0);
#ifdef SvUTF8
if (SvUTF8(buffer_sv))
          sv_utf8_downgrade(buffer_sv, FALSE);
#endif
buffer = SvGROW(buffer_sv, size+1);
        result = i_io_read(ig, buffer, size);
        if (result >= 0) {
SvCUR_set(buffer_sv, result);
*SvEND(buffer_sv) = '\0';
SvPOK_only(buffer_sv);
EXTEND(SP, 1);
PUSHs(sv_2mortal(newSViv(result)));
}
ST(1) = buffer_sv;
SvSETMAGIC(ST(1));

void
i_io_read2(ig, size)
Imager::IO ig
STRLEN size
      PREINIT:
SV *buffer_sv;
        void *buffer;
ssize_t result;
      PPCODE:
        if (size == 0)
croak("size zero in call to read2()");
buffer_sv = newSV(size);
buffer = SvGROW(buffer_sv, size+1);
        result = i_io_read(ig, buffer, size);
        if (result > 0) {
SvCUR_set(buffer_sv, result);
*SvEND(buffer_sv) = '\0';
SvPOK_only(buffer_sv);
EXTEND(SP, 1);
PUSHs(sv_2mortal(buffer_sv));
}
else {
          /* discard it */
SvREFCNT_dec(buffer_sv);
        }

void
i_io_gets(ig, size = 8192, eol = NEWLINE)
Imager::IO ig
STRLEN size
int eol
      PREINIT:
SV *buffer_sv;
        void *buffer;
ssize_t result;
      PPCODE:
        if (size < 2)
croak("size too small in call to gets()");
buffer_sv = sv_2mortal(newSV(size+1));
buffer = SvPVX(buffer_sv);
        result = i_io_gets(ig, buffer, size+1, eol);
        if (result > 0) {
SvCUR_set(buffer_sv, result);
*SvEND(buffer_sv) = '\0';
SvPOK_only(buffer_sv);
EXTEND(SP, 1);
PUSHs(buffer_sv);
}

IV
i_io_write(ig, data_sv)
Imager::IO ig
SV *data_sv
      PREINIT:
        void *data;
STRLEN size;
      CODE:
#ifdef SvUTF8
        if (SvUTF8(data_sv)) {
data_sv = sv_2mortal(newSVsv(data_sv));
          /* yes, we want this to croak() if the SV can't be downgraded */
sv_utf8_downgrade(data_sv, FALSE);
}
#endif
data = SvPV(data_sv, size);
        RETVAL = i_io_write(ig, data, size);
      OUTPUT:
RETVAL

void
i_io_dump(ig, flags = I_IO_DUMP_DEFAULT)
Imager::IO ig
int flags

bool
i_io_set_buffered(ig, flag = 1)
Imager::IO ig
int flag

bool
i_io_is_buffered(ig)
Imager::IO ig

bool
i_io_eof(ig)
Imager::IO ig

bool
i_io_error(ig)
Imager::IO ig

MODULE = Imager PACKAGE = Imager

PROTOTYPES: ENABLE

void
i_list_formats()
PREINIT:
char* item;
int i;
PPCODE:
i=0;
while( (item=i_format_list[i++]) != NULL ) {
EXTEND(SP, 1);
PUSHs(sv_2mortal(newSVpv(item,0)));
}

Imager::ImgRaw
i_sametype(im, x, y)
    Imager::ImgRaw im
               i_img_dim x
               i_img_dim y

Imager::ImgRaw
i_sametype_chans(im, x, y, channels)
    Imager::ImgRaw im
               i_img_dim x
               i_img_dim y
               int channels

int
i_init_log(name_sv,level)
SV* name_sv
int level
PREINIT:
const char *name = SvOK(name_sv) ? SvPV_nolen(name_sv) : NULL;
CODE:
RETVAL = i_init_log(name, level);
OUTPUT:
RETVAL

void
i_log_entry(string,level)
char* string
int level

int
i_log_enabled()

void
i_img_info(im)
    Imager::ImgRaw im
PREINIT:
i_img_dim info[4];
PPCODE:
    i_img_info(im,info);
               EXTEND(SP, 4);
               PUSHs(sv_2mortal(newSViv(info[0])));
               PUSHs(sv_2mortal(newSViv(info[1])));
               PUSHs(sv_2mortal(newSViv(info[2])));
               PUSHs(sv_2mortal(newSViv(info[3])));




void
i_img_setmask(im,ch_mask)
    Imager::ImgRaw im
int ch_mask

int
i_img_getmask(im)
    Imager::ImgRaw im

int
i_img_getchannels(im)
    Imager::ImgRaw im

void
i_img_getdata(im)
    Imager::ImgRaw im
             PPCODE:
EXTEND(SP, 1);
               PUSHs(im->idata ?
sv_2mortal(newSVpv((char *)im->idata, im->bytes))
: &PL_sv_undef);

IV
i_img_get_width(im)
    Imager::ImgRaw im

IV
i_img_get_height(im)
    Imager::ImgRaw im


void
i_img_is_monochrome(im)
Imager::ImgRaw im
      PREINIT:
int zero_is_white;
int result;
      PPCODE:
result = i_img_is_monochrome(im, &zero_is_white);
if (result) {
if (GIMME_V == G_ARRAY) {
EXTEND(SP, 2);
PUSHs(&PL_sv_yes);
PUSHs(sv_2mortal(newSViv(zero_is_white)));
  }
else {
EXTEND(SP, 1);
PUSHs(&PL_sv_yes);
}
}

void
i_line(im,x1,y1,x2,y2,val,endp)
    Imager::ImgRaw im
i_img_dim x1
i_img_dim y1
i_img_dim x2
i_img_dim y2
     Imager::Color val
int endp

void
i_line_aa(im,x1,y1,x2,y2,val,endp)
    Imager::ImgRaw im
i_img_dim x1
i_img_dim y1
i_img_dim x2
i_img_dim y2
     Imager::Color val
int endp

void
i_box(im,x1,y1,x2,y2,val)
    Imager::ImgRaw im
i_img_dim x1
i_img_dim y1
i_img_dim x2
i_img_dim y2
     Imager::Color val

void
i_box_filled(im,x1,y1,x2,y2,val)
    Imager::ImgRaw im
i_img_dim x1
i_img_dim y1
i_img_dim x2
i_img_dim y2
Imager::Color val

int
i_box_filledf(im,x1,y1,x2,y2,val)
    Imager::ImgRaw im
i_img_dim x1
i_img_dim y1
i_img_dim x2
i_img_dim y2
Imager::Color::Float val

void
i_box_cfill(im,x1,y1,x2,y2,fill)
    Imager::ImgRaw im
i_img_dim x1
i_img_dim y1
i_img_dim x2
i_img_dim y2
Imager::FillHandle fill

void
i_arc(im,x,y,rad,d1,d2,val)
    Imager::ImgRaw im
i_img_dim x
i_img_dim y
             double rad
             double d1
             double d2
Imager::Color val

void
i_arc_aa(im,x,y,rad,d1,d2,val)
    Imager::ImgRaw im
double x
double y
            double rad
            double d1
            double d2
Imager::Color val

void
i_arc_cfill(im,x,y,rad,d1,d2,fill)
    Imager::ImgRaw im
i_img_dim x
i_img_dim y
             double rad
             double d1
             double d2
Imager::FillHandle fill

void
i_arc_aa_cfill(im,x,y,rad,d1,d2,fill)
    Imager::ImgRaw im
double x
double y
            double rad
            double d1
            double d2
Imager::FillHandle fill


void
i_circle_aa(im,x,y,rad,val)
    Imager::ImgRaw im
double x
double y
             double rad
Imager::Color val

int
i_circle_out(im,x,y,rad,val)
    Imager::ImgRaw im
i_img_dim x
i_img_dim y
             i_img_dim rad
Imager::Color val

int
i_circle_out_aa(im,x,y,rad,val)
    Imager::ImgRaw im
i_img_dim x
i_img_dim y
             i_img_dim rad
Imager::Color val

int
i_arc_out(im,x,y,rad,d1,d2,val)
    Imager::ImgRaw im
i_img_dim x
i_img_dim y
             i_img_dim rad
double d1
double d2
Imager::Color val

int
i_arc_out_aa(im,x,y,rad,d1,d2,val)
    Imager::ImgRaw im
i_img_dim x
i_img_dim y
             i_img_dim rad
double d1
double d2
Imager::Color val


void
i_bezier_multi(im,xc,yc,val)
    Imager::ImgRaw im
             Imager::Color val
PREINIT:
double *x,*y;
int len;
AV *av1;
AV *av2;
SV *sv1;
SV *sv2;
int i;
PPCODE:
ICL_info(val);
if (!SvROK(ST(1))) croak("Imager: Parameter 1 to i_bezier_multi must be a reference to an array\n");
if (SvTYPE(SvRV(ST(1))) != SVt_PVAV) croak("Imager: Parameter 1 to i_bezier_multi must be a reference to an array\n");
if (!SvROK(ST(2))) croak("Imager: Parameter 2 to i_bezier_multi must be a reference to an array\n");
if (SvTYPE(SvRV(ST(2))) != SVt_PVAV) croak("Imager: Parameter 2 to i_bezier_multi must be a reference to an array\n");
av1=(AV*)SvRV(ST(1));
av2=(AV*)SvRV(ST(2));
if (av_len(av1) != av_len(av2)) croak("Imager: x and y arrays to i_bezier_multi must be equal length\n");
len=av_len(av1)+1;
x=mymalloc( len*sizeof(double) );
y=mymalloc( len*sizeof(double) );
for(i=0;i<len;i++) {
sv1=(*(av_fetch(av1,i,0)));
sv2=(*(av_fetch(av2,i,0)));
x[i]=(double)SvNV(sv1);
y[i]=(double)SvNV(sv2);
}
             i_bezier_multi(im,len,x,y,val);
             myfree(x);
             myfree(y);


int
i_poly_aa(im,xc,yc,val)
    Imager::ImgRaw im
             Imager::Color val
PREINIT:
double *x,*y;
int len;
AV *av1;
AV *av2;
SV *sv1;
SV *sv2;
int i;
CODE:
ICL_info(val);
if (!SvROK(ST(1))) croak("Imager: Parameter 1 to i_poly_aa must be a reference to an array\n");
if (SvTYPE(SvRV(ST(1))) != SVt_PVAV) croak("Imager: Parameter 1 to i_poly_aa must be a reference to an array\n");
if (!SvROK(ST(2))) croak("Imager: Parameter 1 to i_poly_aa must be a reference to an array\n");
if (SvTYPE(SvRV(ST(2))) != SVt_PVAV) croak("Imager: Parameter 1 to i_poly_aa must be a reference to an array\n");
av1=(AV*)SvRV(ST(1));
av2=(AV*)SvRV(ST(2));
if (av_len(av1) != av_len(av2)) croak("Imager: x and y arrays to i_poly_aa must be equal length\n");
len=av_len(av1)+1;
x=mymalloc( len*sizeof(double) );
y=mymalloc( len*sizeof(double) );
for(i=0;i<len;i++) {
sv1=(*(av_fetch(av1,i,0)));
sv2=(*(av_fetch(av2,i,0)));
x[i]=(double)SvNV(sv1);
y[i]=(double)SvNV(sv2);
}
             RETVAL = i_poly_aa(im,len,x,y,val);
             myfree(x);
             myfree(y);
OUTPUT:
RETVAL

int
i_poly_aa_cfill(im,xc,yc,fill)
    Imager::ImgRaw im
     Imager::FillHandle fill
PREINIT:
double *x,*y;
int len;
AV *av1;
AV *av2;
SV *sv1;
SV *sv2;
int i;
CODE:
if (!SvROK(ST(1))) croak("Imager: Parameter 1 to i_poly_aa_cfill must be a reference to an array\n");
if (SvTYPE(SvRV(ST(1))) != SVt_PVAV) croak("Imager: Parameter 1 to i_poly_aa_cfill must be a reference to an array\n");
if (!SvROK(ST(2))) croak("Imager: Parameter 1 to i_poly_aa_cfill must be a reference to an array\n");
if (SvTYPE(SvRV(ST(2))) != SVt_PVAV) croak("Imager: Parameter 1 to i_poly_aa_cfill must be a reference to an array\n");
av1=(AV*)SvRV(ST(1));
av2=(AV*)SvRV(ST(2));
if (av_len(av1) != av_len(av2)) croak("Imager: x and y arrays to i_poly_aa_cfill must be equal length\n");
len=av_len(av1)+1;
x=mymalloc( len*sizeof(double) );
y=mymalloc( len*sizeof(double) );
for(i=0;i<len;i++) {
sv1=(*(av_fetch(av1,i,0)));
sv2=(*(av_fetch(av2,i,0)));
x[i]=(double)SvNV(sv1);
y[i]=(double)SvNV(sv2);
}
             RETVAL = i_poly_aa_cfill(im,len,x,y,fill);
             myfree(x);
             myfree(y);
OUTPUT:
RETVAL



undef_int
i_flood_fill(im,seedx,seedy,dcol)
    Imager::ImgRaw im
i_img_dim seedx
i_img_dim seedy
     Imager::Color dcol

undef_int
i_flood_cfill(im,seedx,seedy,fill)
    Imager::ImgRaw im
i_img_dim seedx
i_img_dim seedy
     Imager::FillHandle fill

undef_int
i_flood_fill_border(im,seedx,seedy,dcol, border)
    Imager::ImgRaw im
i_img_dim seedx
i_img_dim seedy
     Imager::Color dcol
     Imager::Color border

undef_int
i_flood_cfill_border(im,seedx,seedy,fill, border)
    Imager::ImgRaw im
i_img_dim seedx
i_img_dim seedy
     Imager::FillHandle fill
     Imager::Color border


void
i_copyto(im,src,x1,y1,x2,y2,tx,ty)
    Imager::ImgRaw im
    Imager::ImgRaw src
i_img_dim x1
i_img_dim y1
i_img_dim x2
i_img_dim y2
i_img_dim tx
i_img_dim ty


void
i_copyto_trans(im,src,x1,y1,x2,y2,tx,ty,trans)
    Imager::ImgRaw im
    Imager::ImgRaw src
i_img_dim x1
i_img_dim y1
i_img_dim x2
i_img_dim y2
i_img_dim tx
i_img_dim ty
     Imager::Color trans

Imager::ImgRaw
i_copy(src)
    Imager::ImgRaw src


undef_int
i_rubthru(im,src,tx,ty,src_minx,src_miny,src_maxx,src_maxy)
    Imager::ImgRaw im
    Imager::ImgRaw src
i_img_dim tx
i_img_dim ty
i_img_dim src_minx
i_img_dim src_miny
i_img_dim src_maxx
i_img_dim src_maxy

undef_int
i_compose(out, src, out_left, out_top, src_left, src_top, width, height, combine = ic_normal, opacity = 0.0)
    Imager::ImgRaw out
    Imager::ImgRaw src
i_img_dim out_left
  i_img_dim out_top
i_img_dim src_left
i_img_dim src_top
i_img_dim width
i_img_dim height
int combine
double opacity

undef_int
i_compose_mask(out, src, mask, out_left, out_top, src_left, src_top, mask_left, mask_top, width, height, combine = ic_normal, opacity = 0.0)
    Imager::ImgRaw out
    Imager::ImgRaw src
    Imager::ImgRaw mask
i_img_dim out_left
  i_img_dim out_top
i_img_dim src_left
i_img_dim src_top
i_img_dim mask_left
i_img_dim mask_top
i_img_dim width
i_img_dim height
int combine
double opacity

Imager::ImgRaw
i_combine(src_av, channels_av = NULL)
AV *src_av
AV *channels_av
  PREINIT:
i_img **imgs = NULL;
STRLEN in_count;
int *channels = NULL;
int i;
SV **psv;
IV tmp;
  CODE:
in_count = av_len(src_av) + 1;
if (in_count > 0) {
imgs = mymalloc(sizeof(i_img*) * in_count);
channels = mymalloc(sizeof(int) * in_count);
for (i = 0; i < in_count; ++i) {
psv = av_fetch(src_av, i, 0);
if (!psv || !*psv || !sv_derived_from(*psv, "Imager::ImgRaw")) {
myfree(imgs);
myfree(channels);
croak("imgs must contain only images");
}
tmp = SvIV((SV*)SvRV(*psv));
imgs[i] = INT2PTR(i_img*, tmp);
if (channels_av &&
(psv = av_fetch(channels_av, i, 0)) != NULL &&
*psv) {
channels[i] = SvIV(*psv);
}
else {
channels[i] = 0;
}
}
}
RETVAL = i_combine(imgs, channels, in_count);
myfree(imgs);
myfree(channels);
  OUTPUT:
RETVAL

undef_int
i_flipxy(im, direction)
    Imager::ImgRaw im
int direction

Imager::ImgRaw
i_rotate90(im, degrees)
    Imager::ImgRaw im
               int degrees

Imager::ImgRaw
i_rotate_exact(im, amount, ...)
    Imager::ImgRaw im
            double amount
      PREINIT:
i_color *backp = NULL;
i_fcolor *fbackp = NULL;
int i;
SV * sv1;
      CODE:
/* extract the bg colors if any */
/* yes, this is kind of strange */
for (i = 2; i < items; ++i) {
          sv1 = ST(i);
          if (sv_derived_from(sv1, "Imager::Color")) {
IV tmp = SvIV((SV*)SvRV(sv1));
backp = INT2PTR(i_color *, tmp);
}
else if (sv_derived_from(sv1, "Imager::Color::Float")) {
IV tmp = SvIV((SV*)SvRV(sv1));
fbackp = INT2PTR(i_fcolor *, tmp);
}
}
RETVAL = i_rotate_exact_bg(im, amount, backp, fbackp);
      OUTPUT:
RETVAL

Imager::ImgRaw
i_matrix_transform(im, xsize, ysize, matrix, ...)
    Imager::ImgRaw im
               i_img_dim xsize
               i_img_dim ysize
      PREINIT:
        double matrix[9];
        AV *av;
        IV len;
        SV *sv1;
        int i;
i_color *backp = NULL;
i_fcolor *fbackp = NULL;
      CODE:
        if (!SvROK(ST(3)) || SvTYPE(SvRV(ST(3))) != SVt_PVAV)
          croak("i_matrix_transform: parameter 4 must be an array ref\n");
av=(AV*)SvRV(ST(3));
len=av_len(av)+1;
        if (len > 9)
          len = 9;
        for (i = 0; i < len; ++i) {
sv1=(*(av_fetch(av,i,0)));
matrix[i] = SvNV(sv1);
        }
        for (; i < 9; ++i)
          matrix[i] = 0;
/* extract the bg colors if any */
/* yes, this is kind of strange */
for (i = 4; i < items; ++i) {
          sv1 = ST(i);
          if (sv_derived_from(sv1, "Imager::Color")) {
IV tmp = SvIV((SV*)SvRV(sv1));
backp = INT2PTR(i_color *, tmp);
}
else if (sv_derived_from(sv1, "Imager::Color::Float")) {
IV tmp = SvIV((SV*)SvRV(sv1));
fbackp = INT2PTR(i_fcolor *, tmp);
}
}
        RETVAL = i_matrix_transform_bg(im, xsize, ysize, matrix, backp, fbackp);
      OUTPUT:
        RETVAL

undef_int
i_gaussian(im,stdev)
    Imager::ImgRaw im
double stdev

void
i_unsharp_mask(im,stdev,scale)
    Imager::ImgRaw im
double stdev
             double scale

int
i_conv(im,coef)
Imager::ImgRaw im
AV *coef
     PREINIT:
double* c_coef;
int len;
SV* sv1;
int i;
    CODE:
len = av_len(coef) + 1;
c_coef=mymalloc( len * sizeof(double) );
for(i = 0; i < len; i++) {
sv1 = (*(av_fetch(coef, i, 0)));
c_coef[i] = (double)SvNV(sv1);
}
RETVAL = i_conv(im, c_coef, len);
myfree(c_coef);
    OUTPUT:
RETVAL

Imager::ImgRaw
i_convert(src, avmain)
    Imager::ImgRaw src
    AV *avmain
PREINIT:
     double *coeff;
int outchan;
int inchan;
          SV **temp;
          AV *avsub;
int len;
int i, j;
        CODE:
outchan = av_len(avmain)+1;
          /* find the biggest */
          inchan = 0;
for (j=0; j < outchan; ++j) {
temp = av_fetch(avmain, j, 0);
if (temp && SvROK(*temp) && SvTYPE(SvRV(*temp)) == SVt_PVAV) {
avsub = (AV*)SvRV(*temp);
len = av_len(avsub)+1;
if (len > inchan)
inchan = len;
}
else {
i_push_errorf(0, "invalid matrix: element %d is not an array ref", j);
XSRETURN(0);
}
          }
          coeff = mymalloc(sizeof(double) * outchan * inchan);
for (j = 0; j < outchan; ++j) {
avsub = (AV*)SvRV(*av_fetch(avmain, j, 0));
len = av_len(avsub)+1;
for (i = 0; i < len; ++i) {
temp = av_fetch(avsub, i, 0);
if (temp)
coeff[i+j*inchan] = SvNV(*temp);
else
coeff[i+j*inchan] = 0;
}
while (i < inchan)
coeff[i++ + j*inchan] = 0;
}
RETVAL = i_convert(src, coeff, outchan, inchan);
          myfree(coeff);
OUTPUT:
RETVAL


void
i_map(im, pmaps)
    Imager::ImgRaw im
PREINIT:
unsigned int mask = 0;
AV *avmain;
AV *avsub;
          SV **temp;
int len;
int i, j;
unsigned char (*maps)[256];
        CODE:
if (!SvROK(ST(1)) || SvTYPE(SvRV(ST(1))) != SVt_PVAV)
croak("i_map: parameter 2 must be an arrayref\n");
          avmain = (AV*)SvRV(ST(1));
len = av_len(avmain)+1;
if (im->channels < len) len = im->channels;

maps = mymalloc( len * sizeof(unsigned char [256]) );

for (j=0; j<len ; j++) {
temp = av_fetch(avmain, j, 0);
if (temp && SvROK(*temp) && (SvTYPE(SvRV(*temp)) == SVt_PVAV) ) {
avsub = (AV*)SvRV(*temp);
if(av_len(avsub) != 255) continue;
mask |= 1<<j;
              for (i=0; i<256 ; i++) {
int val;
temp = av_fetch(avsub, i, 0);
val = temp ? SvIV(*temp) : 0;
if (val<0) val = 0;
if (val>255) val = 255;
maps[j][i] = val;
}
            }
          }
          i_map(im, maps, mask);
myfree(maps);



float
i_img_diff(im1,im2)
    Imager::ImgRaw im1
    Imager::ImgRaw im2

double
i_img_diffd(im1,im2)
    Imager::ImgRaw im1
    Imager::ImgRaw im2

int
i_img_samef(im1, im2, epsilon = i_img_epsilonf(), what=NULL)
    Imager::ImgRaw im1
    Imager::ImgRaw im2
    double epsilon
    const char *what

double
i_img_epsilonf()

bool
_is_color_object(sv)
SV* sv
    CODE:
        SvGETMAGIC(sv);
        RETVAL = SvOK(sv) && SvROK(sv) &&
(sv_derived_from(sv, "Imager::Color")
          || sv_derived_from(sv, "Imager::Color::Float"));
    OUTPUT:
        RETVAL

#ifdef HAVE_LIBTT


Imager::Font::TT
i_tt_new(fontname)
char* fontname


MODULE = Imager PACKAGE = Imager::Font::TT PREFIX=TT_

#define TT_DESTROY(handle) i_tt_destroy(handle)

void
TT_DESTROY(handle)
     Imager::Font::TT handle

int
TT_CLONE_SKIP(...)
    CODE:
        (void)items; /* avoid unused warning */
        RETVAL = 1;
    OUTPUT:
        RETVAL


MODULE = Imager PACKAGE = Imager


undef_int
i_tt_text(handle,im,xb,yb,cl,points,str_sv,len_ignored,smooth,utf8,align=1)
  Imager::Font::TT handle
    Imager::ImgRaw im
i_img_dim xb
i_img_dim yb
     Imager::Color cl
             double points
SV * str_sv
int smooth
               int utf8
               int align
             PREINIT:
               char *str;
               STRLEN len;
             CODE:
#ifdef SvUTF8
               if (SvUTF8(str_sv))
                 utf8 = 1;
#endif
               str = SvPV(str_sv, len);
               RETVAL = i_tt_text(handle, im, xb, yb, cl, points, str,
                                  len, smooth, utf8, align);
             OUTPUT:
               RETVAL


undef_int
i_tt_cp(handle,im,xb,yb,channel,points,str_sv,len_ignored,smooth,utf8,align=1)
  Imager::Font::TT handle
    Imager::ImgRaw im
i_img_dim xb
i_img_dim yb
int channel
             double points
SV * str_sv
int smooth
               int utf8
               int align
             PREINIT:
               char *str;
               STRLEN len;
             CODE:
#ifdef SvUTF8
               if (SvUTF8(str_sv))
                 utf8 = 1;
#endif
               str = SvPV(str_sv, len);
               RETVAL = i_tt_cp(handle, im, xb, yb, channel, points, str, len,
                                smooth, utf8, align);
             OUTPUT:
                RETVAL


void
i_tt_bbox(handle,point,str_sv,len_ignored, utf8)
  Imager::Font::TT handle
double point
SV* str_sv
               int utf8
PREINIT:
i_img_dim cords[BOUNDING_BOX_COUNT];
int rc;
               char * str;
               STRLEN len;
               int i;
PPCODE:
#ifdef SvUTF8
               if (SvUTF8(ST(2)))
                 utf8 = 1;
#endif
               str = SvPV(str_sv, len);
   if ((rc=i_tt_bbox(handle,point,str,len,cords, utf8))) {
                 EXTEND(SP, rc);
                 for (i = 0; i < rc; ++i) {
                   PUSHs(sv_2mortal(newSViv(cords[i])));
                 }
               }

void
i_tt_has_chars(handle, text_sv, utf8)
        Imager::Font::TT handle
        SV *text_sv
        int utf8
      PREINIT:
        char const *text;
        STRLEN len;
        char *work;
        size_t count;
        size_t i;
      PPCODE:
#ifdef SvUTF8
        if (SvUTF8(text_sv))
          utf8 = 1;
#endif
        text = SvPV(text_sv, len);
        work = mymalloc(len);
        count = i_tt_has_chars(handle, text, len, utf8, work);
        if (GIMME_V == G_ARRAY) {
          EXTEND(SP, count);
          for (i = 0; i < count; ++i) {
            PUSHs(boolSV(work[i]));
          }
        }
        else {
          EXTEND(SP, 1);
          PUSHs(sv_2mortal(newSVpv(work, count)));
        }
        myfree(work);

void
i_tt_dump_names(handle)
        Imager::Font::TT handle

void
i_tt_face_name(handle)
        Imager::Font::TT handle
      PREINIT:
        char name[255];
        size_t len;
      PPCODE:
        len = i_tt_face_name(handle, name, sizeof(name));
        if (len) {
          EXTEND(SP, 1);
          PUSHs(sv_2mortal(newSVpv(name, len-1)));
        }

void
i_tt_glyph_name(handle, text_sv, utf8 = 0)
        Imager::Font::TT handle
        SV *text_sv
        int utf8
      PREINIT:
        char const *text;
        STRLEN work_len;
        size_t len;
        size_t outsize;
        char name[255];
      PPCODE:
#ifdef SvUTF8
        if (SvUTF8(text_sv))
          utf8 = 1;
#endif
        text = SvPV(text_sv, work_len);
        len = work_len;
        while (len) {
          unsigned long ch;
          if (utf8) {
            ch = i_utf8_advance(&text, &len);
            if (ch == ~0UL) {
              i_push_error(0, "invalid UTF8 character");
              break;
            }
          }
          else {
            ch = *text++;
            --len;
          }
          EXTEND(SP, 1);
          if ((outsize = i_tt_glyph_name(handle, ch, name, sizeof(name))) != 0) {
            PUSHs(sv_2mortal(newSVpv(name, 0)));
          }
          else {
            PUSHs(&PL_sv_undef);
          }
        }

#endif

const char *
i_test_format_probe(ig, length)
        Imager::IO ig
int length

Imager::ImgRaw
i_readpnm_wiol(ig, allow_incomplete)
        Imager::IO ig
int allow_incomplete


void
i_readpnm_multi_wiol(ig, allow_incomplete)
        Imager::IO ig
int allow_incomplete
      PREINIT:
        i_img **imgs;
        int count=0;
        int i;
      PPCODE:
        imgs = i_readpnm_multi_wiol(ig, &count, allow_incomplete);
        if (imgs) {
          EXTEND(SP, count);
          for (i = 0; i < count; ++i) {
            SV *sv = sv_newmortal();
            sv_setref_pv(sv, "Imager::ImgRaw", (void *)imgs[i]);
            PUSHs(sv);
          }
          myfree(imgs);
        }

undef_int
i_writeppm_wiol(im, ig)
    Imager::ImgRaw im
        Imager::IO ig





Imager::ImgRaw
i_readraw_wiol(ig,x,y,datachannels,storechannels,intrl)
        Imager::IO ig
i_img_dim x
i_img_dim y
int datachannels
int storechannels
int intrl

undef_int
i_writeraw_wiol(im,ig)
    Imager::ImgRaw im
        Imager::IO ig

undef_int
i_writebmp_wiol(im,ig)
    Imager::ImgRaw im
        Imager::IO ig

Imager::ImgRaw
i_readbmp_wiol(ig, allow_incomplete=0)
        Imager::IO ig
        int allow_incomplete


undef_int
i_writetga_wiol(im,ig, wierdpack, compress, idstring)
    Imager::ImgRaw im
        Imager::IO ig
               int wierdpack
               int compress
              char* idstring
            PREINIT:
                int idlen;
CODE:
                idlen = SvCUR(ST(4));
                RETVAL = i_writetga_wiol(im, ig, wierdpack, compress, idstring, idlen);
                OUTPUT:
                RETVAL


Imager::ImgRaw
i_readtga_wiol(ig, length)
        Imager::IO ig
               int length




Imager::ImgRaw
i_scaleaxis(im,Value,Axis)
    Imager::ImgRaw im
             double Value
int Axis

Imager::ImgRaw
i_scale_nn(im,scx,scy)
    Imager::ImgRaw im
             double scx
             double scy

Imager::ImgRaw
i_scale_mixing(im, width, height)
    Imager::ImgRaw im
i_img_dim width
i_img_dim height

Imager::ImgRaw
i_haar(im)
    Imager::ImgRaw im

int
i_count_colors(im,maxc)
    Imager::ImgRaw im
               int maxc

void
i_get_anonymous_color_histo(im, maxc = 0x40000000)
   Imager::ImgRaw im
   int maxc
    PREINIT:
        int i;
        unsigned int * col_usage = NULL;
        int col_cnt;
    PPCODE:
col_cnt = i_get_anonymous_color_histo(im, &col_usage, maxc);
        EXTEND(SP, col_cnt);
        for (i = 0; i < col_cnt; i++) {
            PUSHs(sv_2mortal(newSViv( col_usage[i])));
        }
        myfree(col_usage);
        XSRETURN(col_cnt);


void
i_transform(im,opx,opy,parm)
    Imager::ImgRaw im
             PREINIT:
             double* parm;
             int *opx;
             int *opy;
             int opxl;
             int opyl;
             int parmlen;
             AV* av;
             SV* sv1;
             int i;
i_img *result;
             PPCODE:
             if (!SvROK(ST(1))) croak("Imager: Parameter 1 must be a reference to an array\n");
             if (!SvROK(ST(2))) croak("Imager: Parameter 2 must be a reference to an array\n");
             if (!SvROK(ST(3))) croak("Imager: Parameter 3 must be a reference to an array\n");
             if (SvTYPE(SvRV(ST(1))) != SVt_PVAV) croak("Imager: Parameter 1 must be a reference to an array\n");
             if (SvTYPE(SvRV(ST(2))) != SVt_PVAV) croak("Imager: Parameter 2 must be a reference to an array\n");
             if (SvTYPE(SvRV(ST(3))) != SVt_PVAV) croak("Imager: Parameter 3 must be a reference to an array\n");
             av=(AV*)SvRV(ST(1));
             opxl=av_len(av)+1;
             opx=mymalloc( opxl*sizeof(int) );
             for(i=0;i<opxl;i++) {
               sv1=(*(av_fetch(av,i,0)));
               opx[i]=(int)SvIV(sv1);
             }
             av=(AV*)SvRV(ST(2));
             opyl=av_len(av)+1;
             opy=mymalloc( opyl*sizeof(int) );
             for(i=0;i<opyl;i++) {
               sv1=(*(av_fetch(av,i,0)));
               opy[i]=(int)SvIV(sv1);
             }
             av=(AV*)SvRV(ST(3));
             parmlen=av_len(av)+1;
             parm=mymalloc( parmlen*sizeof(double) );
             for(i=0;i<parmlen;i++) { /* FIXME: Bug? */
               sv1=(*(av_fetch(av,i,0)));
               parm[i]=(double)SvNV(sv1);
             }
             result=i_transform(im,opx,opxl,opy,opyl,parm,parmlen);
             myfree(parm);
             myfree(opy);
             myfree(opx);
  if (result) {
SV *result_sv = sv_newmortal();
EXTEND(SP, 1);
sv_setref_pv(result_sv, "Imager::ImgRaw", (void*)result);
PUSHs(result_sv);
}

void
i_transform2(sv_width,sv_height,channels,sv_ops,av_n_regs,av_c_regs,av_in_imgs)
SV *sv_width
SV *sv_height
SV *sv_ops
AV *av_n_regs
AV *av_c_regs
AV *av_in_imgs
int channels
PREINIT:
             i_img_dim width;
             i_img_dim height;
struct rm_op *ops;
STRLEN ops_len;
int ops_count;
             double *n_regs;
             int n_regs_count;
             i_color *c_regs;
int c_regs_count;
             int in_imgs_count;
             i_img **in_imgs;
             SV *sv1;
             IV tmp;
int i;
i_img *result;
             PPCODE:

             in_imgs_count = av_len(av_in_imgs)+1;
for (i = 0; i < in_imgs_count; ++i) {
sv1 = *av_fetch(av_in_imgs, i, 0);
if (!sv_derived_from(sv1, "Imager::ImgRaw")) {
croak("sv_in_img must contain only images");
}
}
             if (in_imgs_count > 0) {
               in_imgs = mymalloc(in_imgs_count*sizeof(i_img*));
               for (i = 0; i < in_imgs_count; ++i) {
sv1 = *av_fetch(av_in_imgs,i,0);
if (!sv_derived_from(sv1, "Imager::ImgRaw")) {
croak("Parameter 5 must contain only images");
}
                 tmp = SvIV((SV*)SvRV(sv1));
in_imgs[i] = INT2PTR(i_img*, tmp);
}
}
             else {
/* no input images */
in_imgs = NULL;
             }
             /* default the output size from the first input if possible */
             if (SvOK(sv_width))
width = SvIV(sv_width);
             else if (in_imgs_count)
width = in_imgs[0]->xsize;
             else
croak("No output image width supplied");

             if (SvOK(sv_height))
height = SvIV(sv_height);
             else if (in_imgs_count)
height = in_imgs[0]->ysize;
             else
croak("No output image height supplied");

ops = (struct rm_op *)SvPV(sv_ops, ops_len);
             if (ops_len % sizeof(struct rm_op))
croak("Imager: Parameter 3 must be a bitmap of regops\n");
ops_count = ops_len / sizeof(struct rm_op);

n_regs_count = av_len(av_n_regs)+1;
             n_regs = mymalloc(n_regs_count * sizeof(double));
for (i = 0; i < n_regs_count; ++i) {
sv1 = *av_fetch(av_n_regs,i,0);
if (SvOK(sv1))
n_regs[i] = SvNV(sv1);
}
             c_regs_count = av_len(av_c_regs)+1;
             c_regs = mymalloc(c_regs_count * sizeof(i_color));
             /* I don't bother initializing the colou?r registers */

result=i_transform2(width, height, channels, ops, ops_count,
n_regs, n_regs_count,
c_regs, c_regs_count, in_imgs, in_imgs_count);
if (in_imgs)
myfree(in_imgs);
             myfree(n_regs);
myfree(c_regs);
  if (result) {
SV *result_sv = sv_newmortal();
EXTEND(SP, 1);
sv_setref_pv(result_sv, "Imager::ImgRaw", (void*)result);
PUSHs(result_sv);
}


void
i_contrast(im,intensity)
    Imager::ImgRaw im
             float intensity

void
i_hardinvert(im)
    Imager::ImgRaw im

void
i_hardinvertall(im)
    Imager::ImgRaw im

void
i_noise(im,amount,type)
    Imager::ImgRaw im
             float amount
     unsigned char type

void
i_bumpmap(im,bump,channel,light_x,light_y,strength)
    Imager::ImgRaw im
    Imager::ImgRaw bump
               int channel
         i_img_dim light_x
         i_img_dim light_y
         i_img_dim strength


void
i_bumpmap_complex(im,bump,channel,tx,ty,Lx,Ly,Lz,cd,cs,n,Ia,Il,Is)
    Imager::ImgRaw im
    Imager::ImgRaw bump
               int channel
               i_img_dim tx
               i_img_dim ty
             double Lx
             double Ly
             double Lz
             float cd
             float cs
             float n
     Imager::Color Ia
     Imager::Color Il
     Imager::Color Is



void
i_postlevels(im,levels)
    Imager::ImgRaw im
             int levels

void
i_mosaic(im,size)
    Imager::ImgRaw im
         i_img_dim size

void
i_watermark(im,wmark,tx,ty,pixdiff)
    Imager::ImgRaw im
    Imager::ImgRaw wmark
               i_img_dim tx
               i_img_dim ty
               int pixdiff


void
i_autolevels(im,lsat,usat,skew)
    Imager::ImgRaw im
             float lsat
             float usat
             float skew

void
i_radnoise(im,xo,yo,rscale,ascale)
    Imager::ImgRaw im
             float xo
             float yo
             float rscale
             float ascale

void
i_turbnoise(im, xo, yo, scale)
    Imager::ImgRaw im
             float xo
             float yo
             float scale


void
i_gradgen(im, ...)
    Imager::ImgRaw im
      PREINIT:
int num;
i_img_dim *xo;
i_img_dim *yo;
        i_color *ival;
int dmeasure;
int i;
SV *sv;
AV *axx;
AV *ayy;
AV *ac;
      CODE:
if (items != 5)
croak("Usage: i_gradgen(im, xo, yo, ival, dmeasure)");
if (!SvROK(ST(1)) || ! SvTYPE(SvRV(ST(1))))
croak("i_gradgen: Second argument must be an array ref");
if (!SvROK(ST(2)) || ! SvTYPE(SvRV(ST(2))))
croak("i_gradgen: Third argument must be an array ref");
if (!SvROK(ST(3)) || ! SvTYPE(SvRV(ST(3))))
croak("i_gradgen: Fourth argument must be an array ref");
axx = (AV *)SvRV(ST(1));
ayy = (AV *)SvRV(ST(2));
ac = (AV *)SvRV(ST(3));
dmeasure = (int)SvIV(ST(4));

        num = av_len(axx) < av_len(ayy) ? av_len(axx) : av_len(ayy);
num = num <= av_len(ac) ? num : av_len(ac);
num++;
if (num < 2) croak("Usage: i_gradgen array refs must have more than 1 entry each");
xo = mymalloc( sizeof(i_img_dim) * num );
yo = mymalloc( sizeof(i_img_dim) * num );
ival = mymalloc( sizeof(i_color) * num );
for(i = 0; i<num; i++) {
xo[i] = (i_img_dim)SvIV(* av_fetch(axx, i, 0));
yo[i] = (i_img_dim)SvIV(* av_fetch(ayy, i, 0));
          sv = *av_fetch(ac, i, 0);
if ( !sv_derived_from(sv, "Imager::Color") ) {
free(axx); free(ayy); free(ac);
            croak("i_gradgen: Element of fourth argument is not derived from Imager::Color");
}
ival[i] = *INT2PTR(i_color *, SvIV((SV *)SvRV(sv)));
}
        i_gradgen(im, num, xo, yo, ival, dmeasure);
        myfree(xo);
        myfree(yo);
        myfree(ival);

Imager::ImgRaw
i_diff_image(im, im2, mindist=0)
    Imager::ImgRaw im
    Imager::ImgRaw im2
            double mindist

undef_int
i_fountain(im, xa, ya, xb, yb, type, repeat, combine, super_sample, ssample_param, segs)
    Imager::ImgRaw im
            double xa
            double ya
            double xb
            double yb
               int type
               int repeat
               int combine
               int super_sample
            double ssample_param
      PREINIT:
        AV *asegs;
        int count;
        i_fountain_seg *segs;
      CODE:
if (!SvROK(ST(10)) || ! SvTYPE(SvRV(ST(10))))
croak("i_fountain: argument 11 must be an array ref");
        
asegs = (AV *)SvRV(ST(10));
        segs = load_fount_segs(aTHX_ asegs, &count);
        RETVAL = i_fountain(im, xa, ya, xb, yb, type, repeat, combine,
                            super_sample, ssample_param, count, segs);
        myfree(segs);
      OUTPUT:
        RETVAL

Imager::FillHandle
i_new_fill_fount(xa, ya, xb, yb, type, repeat, combine, super_sample, ssample_param, segs)
            double xa
            double ya
            double xb
            double yb
               int type
               int repeat
               int combine
               int super_sample
            double ssample_param
      PREINIT:
        AV *asegs;
        int count;
        i_fountain_seg *segs;
      CODE:
if (!SvROK(ST(9)) || ! SvTYPE(SvRV(ST(9))))
croak("i_fountain: argument 11 must be an array ref");
        
asegs = (AV *)SvRV(ST(9));
        segs = load_fount_segs(aTHX_ asegs, &count);
        RETVAL = i_new_fill_fount(xa, ya, xb, yb, type, repeat, combine,
                                  super_sample, ssample_param, count, segs);
        myfree(segs);
      OUTPUT:
        RETVAL

Imager::FillHandle
i_new_fill_opacity(other_fill, alpha_mult)
    Imager::FillHandle other_fill
    double alpha_mult

void
i_errors()
      PREINIT:
        i_errmsg *errors;
int i;
AV *av;
SV *sv;
      PPCODE:
errors = i_errors();
i = 0;
while (errors[i].msg) {
av = newAV();
sv = newSVpv(errors[i].msg, strlen(errors[i].msg));
if (!av_store(av, 0, sv)) {
SvREFCNT_dec(sv);
}
sv = newSViv(errors[i].code);
if (!av_store(av, 1, sv)) {
SvREFCNT_dec(sv);
}
PUSHs(sv_2mortal(newRV_noinc((SV*)av)));
++i;
}

void
i_clear_error()

void
i_push_error(code, msg)
int code
const char *msg

undef_int
i_nearest_color(im, ...)
    Imager::ImgRaw im
      PREINIT:
int num;
i_img_dim *xo;
i_img_dim *yo;
        i_color *ival;
int dmeasure;
int i;
SV *sv;
AV *axx;
AV *ayy;
AV *ac;
      CODE:
if (items != 5)
croak("Usage: i_nearest_color(im, xo, yo, ival, dmeasure)");
if (!SvROK(ST(1)) || ! SvTYPE(SvRV(ST(1))))
croak("i_nearest_color: Second argument must be an array ref");
if (!SvROK(ST(2)) || ! SvTYPE(SvRV(ST(2))))
croak("i_nearest_color: Third argument must be an array ref");
if (!SvROK(ST(3)) || ! SvTYPE(SvRV(ST(3))))
croak("i_nearest_color: Fourth argument must be an array ref");
axx = (AV *)SvRV(ST(1));
ayy = (AV *)SvRV(ST(2));
ac = (AV *)SvRV(ST(3));
dmeasure = (int)SvIV(ST(4));

        num = av_len(axx) < av_len(ayy) ? av_len(axx) : av_len(ayy);
num = num <= av_len(ac) ? num : av_len(ac);
num++;
if (num < 2) croak("Usage: i_nearest_color array refs must have more than 1 entry each");
xo = mymalloc( sizeof(i_img_dim) * num );
yo = mymalloc( sizeof(i_img_dim) * num );
ival = mymalloc( sizeof(i_color) * num );
for(i = 0; i<num; i++) {
xo[i] = (i_img_dim)SvIV(* av_fetch(axx, i, 0));
yo[i] = (i_img_dim)SvIV(* av_fetch(ayy, i, 0));
          sv = *av_fetch(ac, i, 0);
if ( !sv_derived_from(sv, "Imager::Color") ) {
free(axx); free(ayy); free(ac);
            croak("i_nearest_color: Element of fourth argument is not derived from Imager::Color");
}
ival[i] = *INT2PTR(i_color *, SvIV((SV *)SvRV(sv)));
}
        RETVAL = i_nearest_color(im, num, xo, yo, ival, dmeasure);
      OUTPUT:
        RETVAL

void
malloc_state()

void
DSO_open(filename)
             char* filename
PREINIT:
void *rc;
char *evstr;
PPCODE:
rc=DSO_open(filename,&evstr);
               if (rc!=NULL) {
                 if (evstr!=NULL) {
                   EXTEND(SP,2);
                   PUSHs(sv_2mortal(newSViv(PTR2IV(rc))));
                   PUSHs(sv_2mortal(newSVpvn(evstr, strlen(evstr))));
                 } else {
                   EXTEND(SP,1);
                   PUSHs(sv_2mortal(newSViv(PTR2IV(rc))));
                 }
               }


undef_int
DSO_close(dso_handle)
             void* dso_handle

void
DSO_funclist(dso_handle_v)
             void* dso_handle_v
PREINIT:
int i;
DSO_handle *dso_handle;
func_ptr *functions;
PPCODE:
dso_handle=(DSO_handle*)dso_handle_v;
functions = DSO_funclist(dso_handle);
i=0;
while( functions[i].name != NULL) {
EXTEND(SP,1);
PUSHs(sv_2mortal(newSVpv(functions[i].name,0)));
EXTEND(SP,1);
PUSHs(sv_2mortal(newSVpv(functions[i++].pcode,0)));
}

void
DSO_call(handle,func_index,hv)
void* handle
int func_index
PREINIT:
HV* hv;
PPCODE:
if (!SvROK(ST(2))) croak("Imager: Parameter 2 must be a reference to a hash\n");
hv=(HV*)SvRV(ST(2));
if (SvTYPE(hv)!=SVt_PVHV) croak("Imager: Parameter 2 must be a reference to a hash\n");
DSO_call( (DSO_handle *)handle,func_index,hv);

SV *
i_get_pixel(im, x, y)
Imager::ImgRaw im
i_img_dim x
i_img_dim y;
      PREINIT:
        i_color *color;
      CODE:
color = (i_color *)mymalloc(sizeof(i_color));
if (i_gpix(im, x, y, color) == 0) {
          RETVAL = NEWSV(0, 0);
          sv_setref_pv(RETVAL, "Imager::Color", (void *)color);
        }
        else {
          myfree(color);
          RETVAL = &PL_sv_undef;
        }
      OUTPUT:
        RETVAL
        

int
i_ppix(im, x, y, cl)
        Imager::ImgRaw im
        i_img_dim x
        i_img_dim y
        Imager::Color cl

Imager::ImgRaw
i_img_pal_new(x, y, channels, maxpal)
i_img_dim x
        i_img_dim y
        int channels
int maxpal

Imager::ImgRaw
i_img_to_pal(src, quant)
        Imager::ImgRaw src
      PREINIT:
        HV *hv;
        i_quantize quant;
      CODE:
        if (!SvROK(ST(1)) || ! SvTYPE(SvRV(ST(1))))
          croak("i_img_to_pal: second argument must be a hash ref");
        hv = (HV *)SvRV(ST(1));
        memset(&quant, 0, sizeof(quant));
quant.version = 1;
        quant.mc_size = 256;
ip_handle_quant_opts(aTHX_ &quant, hv);
        RETVAL = i_img_to_pal(src, &quant);
        if (RETVAL) {
          ip_copy_colors_back(aTHX_ hv, &quant);
        }
ip_cleanup_quant_opts(aTHX_ &quant);
      OUTPUT:
        RETVAL

Imager::ImgRaw
i_img_to_rgb(src)
        Imager::ImgRaw src

void
i_img_make_palette(HV *quant_hv, ...)
      PREINIT:
        size_t count = items - 1;
i_quantize quant;
i_img **imgs = NULL;
ssize_t i;
      PPCODE:
        if (count <= 0)
croak("Please supply at least one image (%d)", (int)count);
        imgs = mymalloc(sizeof(i_img *) * count);
for (i = 0; i < count; ++i) {
SV *img_sv = ST(i + 1);
if (SvROK(img_sv) && sv_derived_from(img_sv, "Imager::ImgRaw")) {
imgs[i] = INT2PTR(i_img *, SvIV((SV*)SvRV(img_sv)));
}
else {
myfree(imgs);
croak("Image %d is not an image object", (int)i+1);
          }
}
        memset(&quant, 0, sizeof(quant));
quant.version = 1;
quant.mc_size = 256;
        ip_handle_quant_opts(aTHX_ &quant, quant_hv);
i_quant_makemap(&quant, imgs, count);
EXTEND(SP, quant.mc_count);
for (i = 0; i < quant.mc_count; ++i) {
SV *sv_c = make_i_color_sv(aTHX_ quant.mc_colors + i);
PUSHs(sv_c);
}
  ip_cleanup_quant_opts(aTHX_ &quant);


void
i_gpal(im, l, r, y)
        Imager::ImgRaw im
        i_img_dim l
        i_img_dim r
        i_img_dim y
      PREINIT:
        i_palidx *work;
        int count, i;
      PPCODE:
        if (l < r) {
          work = mymalloc((r-l) * sizeof(i_palidx));
          count = i_gpal(im, l, r, y, work);
          if (GIMME_V == G_ARRAY) {
            EXTEND(SP, count);
            for (i = 0; i < count; ++i) {
              PUSHs(sv_2mortal(newSViv(work[i])));
            }
          }
          else {
            EXTEND(SP, 1);
            PUSHs(sv_2mortal(newSVpv((char *)work, count * sizeof(i_palidx))));
          }
          myfree(work);
        }
        else {
          if (GIMME_V != G_ARRAY) {
            EXTEND(SP, 1);
            PUSHs(&PL_sv_undef);
          }
        }

int
i_ppal(im, l, y, ...)
        Imager::ImgRaw im
        i_img_dim l
        i_img_dim y
      PREINIT:
        i_palidx *work;
        i_img_dim i;
      CODE:
        if (items > 3) {
          work = malloc_temp(aTHX_ sizeof(i_palidx) * (items-3));
          for (i=0; i < items-3; ++i) {
            work[i] = SvIV(ST(i+3));
          }
          validate_i_ppal(im, work, items - 3);
          RETVAL = i_ppal(im, l, l+items-3, y, work);
        }
        else {
          RETVAL = 0;
        }
      OUTPUT:
        RETVAL

int
i_ppal_p(im, l, y, data)
        Imager::ImgRaw im
        i_img_dim l
        i_img_dim y
        SV *data
      PREINIT:
        i_palidx const *work;
        STRLEN len;
      CODE:
        work = (i_palidx const *)SvPV(data, len);
        len /= sizeof(i_palidx);
        if (len > 0) {
          validate_i_ppal(im, work, len);
          RETVAL = i_ppal(im, l, l+len, y, work);
        }
        else {
          RETVAL = 0;
        }
      OUTPUT:
        RETVAL

SV *
i_addcolors(im, ...)
        Imager::ImgRaw im
      PREINIT:
        int index;
        i_color *colors;
        int i;
      CODE:
        if (items < 2)
          croak("i_addcolors: no colors to add");
        colors = mymalloc((items-1) * sizeof(i_color));
        for (i=0; i < items-1; ++i) {
          if (sv_isobject(ST(i+1))
              && sv_derived_from(ST(i+1), "Imager::Color")) {
            IV tmp = SvIV((SV *)SvRV(ST(i+1)));
            colors[i] = *INT2PTR(i_color *, tmp);
          }
          else {
            myfree(colors);
            croak("i_addcolor: pixels must be Imager::Color objects");
          }
        }
        index = i_addcolors(im, colors, items-1);
        myfree(colors);
        if (index == 0) {
          RETVAL = newSVpv("0 but true", 0);
        }
        else if (index == -1) {
          RETVAL = &PL_sv_undef;
        }
        else {
          RETVAL = newSViv(index);
        }
      OUTPUT:
        RETVAL

undef_int
i_setcolors(im, index, ...)
        Imager::ImgRaw im
        int index
      PREINIT:
        i_color *colors;
        int i;
      CODE:
        if (items < 3)
          croak("i_setcolors: no colors to add");
        colors = mymalloc((items-2) * sizeof(i_color));
        for (i=0; i < items-2; ++i) {
          if (sv_isobject(ST(i+2))
              && sv_derived_from(ST(i+2), "Imager::Color")) {
            IV tmp = SvIV((SV *)SvRV(ST(i+2)));
            colors[i] = *INT2PTR(i_color *, tmp);
          }
          else {
            myfree(colors);
            croak("i_setcolors: pixels must be Imager::Color objects");
          }
        }
        RETVAL = i_setcolors(im, index, colors, items-2);
        myfree(colors);
      OUTPUT:
RETVAL

void
i_getcolors(im, index, ...)
        Imager::ImgRaw im
        int index
      PREINIT:
        i_color *colors;
        int count = 1;
        int i;
      PPCODE:
        if (items > 3)
          croak("i_getcolors: too many arguments");
        if (items == 3)
          count = SvIV(ST(2));
        if (count < 1)
          croak("i_getcolors: count must be positive");
        colors = mymalloc(sizeof(i_color) * count);
        if (i_getcolors(im, index, colors, count)) {
          for (i = 0; i < count; ++i) {
            SV *sv = make_i_color_sv(aTHX_ colors+i);
            PUSHs(sv);
          }
        }
        myfree(colors);


undef_neg_int
i_colorcount(im)
        Imager::ImgRaw im

undef_neg_int
i_maxcolors(im)
        Imager::ImgRaw im

SV *
i_findcolor(im, color)
        Imager::ImgRaw im
        Imager::Color color
      PREINIT:
        i_palidx index;
      CODE:
        if (i_findcolor(im, color, &index)) {
          RETVAL = newSViv(index);
        }
        else {
          RETVAL = &PL_sv_undef;
        }
      OUTPUT:
        RETVAL

int
i_img_bits(im)
        Imager::ImgRaw im

int
i_img_type(im)
        Imager::ImgRaw im

int
i_img_virtual(im)
        Imager::ImgRaw im

void
i_gsamp(im, l, r, y, channels)
        Imager::ImgRaw im
        i_img_dim l
        i_img_dim r
        i_img_dim y
        i_channel_list channels
      PREINIT:
        i_sample_t *data;
        i_img_dim count, i;
      PPCODE:
        if (l < r) {
          data = mymalloc(sizeof(i_sample_t) * (r-l) * channels.count); /* XXX: memleak? */
          count = i_gsamp(im, l, r, y, data, channels.channels, channels.count);
          if (GIMME_V == G_ARRAY) {
            EXTEND(SP, count);
            for (i = 0; i < count; ++i)
              PUSHs(sv_2mortal(newSViv(data[i])));
          }
          else {
            EXTEND(SP, 1);
            PUSHs(sv_2mortal(newSVpv((char *)data, count * sizeof(i_sample_t))));
          }
myfree(data);
        }
        else {
          if (GIMME_V != G_ARRAY) {
            EXTEND(SP, 1);
            PUSHs(&PL_sv_undef);
          }
        }

undef_neg_int
i_gsamp_bits(im, l, r, y, bits, target, offset, channels)
        Imager::ImgRaw im
        i_img_dim l
        i_img_dim r
        i_img_dim y
int bits
AV *target
STRLEN offset
        i_channel_list channels
      PREINIT:
        unsigned *data;
        i_img_dim count, i;
      CODE:
i_clear_error();
        if (items < 8)
          croak("No channel numbers supplied to g_samp()");
        if (l < r) {
          data = mymalloc(sizeof(unsigned) * (r-l) * channels.count);
          count = i_gsamp_bits(im, l, r, y, data, channels.channels, channels.count, bits);
for (i = 0; i < count; ++i) {
av_store(target, i+offset, newSVuv(data[i]));
}
myfree(data);
RETVAL = count;
        }
        else {
RETVAL = 0;
        }
      OUTPUT:
RETVAL

undef_neg_int
i_psamp_bits(im, l, y, bits, channels, data_av, data_offset = 0, pixel_count = -1)
        Imager::ImgRaw im
        i_img_dim l
        i_img_dim y
int bits
i_channel_list channels
AV *data_av
        i_img_dim data_offset
        i_img_dim pixel_count
      PREINIT:
STRLEN data_count;
size_t data_used;
unsigned *data;
ptrdiff_t i;
      CODE:
i_clear_error();

data_count = av_len(data_av) + 1;
if (data_offset < 0) {
croak("data_offset must be non-negative");
}
if (data_offset > data_count) {
croak("data_offset greater than number of samples supplied");
        }
if (pixel_count == -1 ||
data_offset + pixel_count * channels.count > data_count) {
pixel_count = (data_count - data_offset) / channels.count;
}

data_used = pixel_count * channels.count;
data = mymalloc(sizeof(unsigned) * data_count);
for (i = 0; i < data_used; ++i)
data[i] = SvUV(*av_fetch(data_av, data_offset + i, 0));

RETVAL = i_psamp_bits(im, l, l + pixel_count, y, data, channels.channels,
channels.count, bits);

if (data)
myfree(data);
      OUTPUT:
RETVAL

undef_neg_int
i_psamp(im, x, y, channels, data, offset = 0, width = -1)
Imager::ImgRaw im
i_img_dim x
i_img_dim y
i_channel_list channels
        i_sample_list data
i_img_dim offset
i_img_dim width
    PREINIT:
i_img_dim r;
    CODE:
i_clear_error();
if (offset < 0) {
i_push_error(0, "offset must be non-negative");
XSRETURN_UNDEF;
}
if (offset > 0) {
if (offset > data.count) {
i_push_error(0, "offset greater than number of samples supplied");
XSRETURN_UNDEF;
}
data.samples += offset;
data.count -= offset;
}
if (width == -1 ||
width * channels.count > data.count) {
width = data.count / channels.count;
        }
r = x + width;
RETVAL = i_psamp(im, x, r, y, data.samples, channels.channels, channels.count);
    OUTPUT:
RETVAL

undef_neg_int
i_psampf(im, x, y, channels, data, offset = 0, width = -1)
Imager::ImgRaw im
i_img_dim x
i_img_dim y
i_channel_list channels
        i_fsample_list data
i_img_dim offset
i_img_dim width
    PREINIT:
i_img_dim r;
    CODE:
i_clear_error();
if (offset < 0) {
i_push_error(0, "offset must be non-negative");
XSRETURN_UNDEF;
}
if (offset > 0) {
if (offset > data.count) {
i_push_error(0, "offset greater than number of samples supplied");
XSRETURN_UNDEF;
}
data.samples += offset;
data.count -= offset;
}
if (width == -1 ||
width * channels.count > data.count) {
width = data.count / channels.count;
        }
r = x + width;
RETVAL = i_psampf(im, x, r, y, data.samples, channels.channels, channels.count);
    OUTPUT:
RETVAL

Imager::ImgRaw
i_img_masked_new(targ, mask, x, y, w, h)
        Imager::ImgRaw targ
        i_img_dim x
        i_img_dim y
        i_img_dim w
        i_img_dim h
      PREINIT:
        i_img *mask;
      CODE:
        if (SvOK(ST(1))) {
          if (!sv_isobject(ST(1))
              || !sv_derived_from(ST(1), "Imager::ImgRaw")) {
            croak("i_img_masked_new: parameter 2 must undef or an image");
          }
          mask = INT2PTR(i_img *, SvIV((SV *)SvRV(ST(1))));
        }
        else
          mask = NULL;
        RETVAL = i_img_masked_new(targ, mask, x, y, w, h);
      OUTPUT:
        RETVAL

int
i_plin(im, l, y, ...)
        Imager::ImgRaw im
        i_img_dim l
        i_img_dim y
      PREINIT:
        i_color *work;
        STRLEN i;
        STRLEN len;
        size_t count;
      CODE:
        if (items > 3) {
          if (items == 4 && SvOK(ST(3)) && !SvROK(ST(3))) {
/* supplied as a byte string */
            work = (i_color *)SvPV(ST(3), len);
            count = len / sizeof(i_color);
if (count * sizeof(i_color) != len) {
              croak("i_plin: length of scalar argument must be multiple of sizeof i_color");
            }
            RETVAL = i_plin(im, l, l+count, y, work);
          }
else {
            work = mymalloc(sizeof(i_color) * (items-3));
            for (i=0; i < items-3; ++i) {
              if (sv_isobject(ST(i+3))
                  && sv_derived_from(ST(i+3), "Imager::Color")) {
                IV tmp = SvIV((SV *)SvRV(ST(i+3)));
                work[i] = *INT2PTR(i_color *, tmp);
              }
              else {
                myfree(work);
                croak("i_plin: pixels must be Imager::Color objects");
              }
            }
            RETVAL = i_plin(im, l, l+items-3, y, work);
            myfree(work);
          }
        }
        else {
          RETVAL = 0;
        }
      OUTPUT:
        RETVAL

int
i_ppixf(im, x, y, cl)
        Imager::ImgRaw im
        i_img_dim x
        i_img_dim y
        Imager::Color::Float cl

void
i_gsampf(im, l, r, y, channels)
        Imager::ImgRaw im
        i_img_dim l
        i_img_dim r
        i_img_dim y
i_channel_list channels
      PREINIT:
        i_fsample_t *data;
        i_img_dim count, i;
      PPCODE:
        if (l < r) {
          data = mymalloc(sizeof(i_fsample_t) * (r-l) * channels.count);
          count = i_gsampf(im, l, r, y, data, channels.channels, channels.count);
          if (GIMME_V == G_ARRAY) {
            EXTEND(SP, count);
            for (i = 0; i < count; ++i)
              PUSHs(sv_2mortal(newSVnv(data[i])));
          }
          else {
            EXTEND(SP, 1);
            PUSHs(sv_2mortal(newSVpv((void *)data, count * sizeof(i_fsample_t))));
          }
          myfree(data);
        }
        else {
          if (GIMME_V != G_ARRAY) {
            EXTEND(SP, 1);
            PUSHs(&PL_sv_undef);
          }
        }

int
i_plinf(im, l, y, ...)
        Imager::ImgRaw im
        i_img_dim l
        i_img_dim y
      PREINIT:
        i_fcolor *work;
        i_img_dim i;
        STRLEN len;
        size_t count;
      CODE:
        if (items > 3) {
          if (items == 4 && SvOK(ST(3)) && !SvROK(ST(3))) {
/* supplied as a byte string */
            work = (i_fcolor *)SvPV(ST(3), len);
            count = len / sizeof(i_fcolor);
if (count * sizeof(i_fcolor) != len) {
              croak("i_plin: length of scalar argument must be multiple of sizeof i_fcolor");
            }
            RETVAL = i_plinf(im, l, l+count, y, work);
          }
else {
            work = mymalloc(sizeof(i_fcolor) * (items-3));
            for (i=0; i < items-3; ++i) {
              if (sv_isobject(ST(i+3))
                  && sv_derived_from(ST(i+3), "Imager::Color::Float")) {
                IV tmp = SvIV((SV *)SvRV(ST(i+3)));
                work[i] = *INT2PTR(i_fcolor *, tmp);
              }
              else {
                myfree(work);
                croak("i_plinf: pixels must be Imager::Color::Float objects");
              }
            }
            /**(char *)0 = 1;*/
            RETVAL = i_plinf(im, l, l+items-3, y, work);
            myfree(work);
          }
        }
        else {
          RETVAL = 0;
        }
      OUTPUT:
        RETVAL

SV *
i_gpixf(im, x, y)
Imager::ImgRaw im
i_img_dim x
i_img_dim y;
      PREINIT:
        i_fcolor *color;
      CODE:
color = (i_fcolor *)mymalloc(sizeof(i_fcolor));
if (i_gpixf(im, x, y, color) == 0) {
          RETVAL = NEWSV(0,0);
          sv_setref_pv(RETVAL, "Imager::Color::Float", (void *)color);
        }
        else {
          myfree(color);
          RETVAL = &PL_sv_undef;
        }
      OUTPUT:
        RETVAL

void
i_glin(im, l, r, y)
        Imager::ImgRaw im
        i_img_dim l
        i_img_dim r
        i_img_dim y
      PREINIT:
        i_color *vals;
        i_img_dim count, i;
      PPCODE:
        if (l < r) {
          vals = mymalloc((r-l) * sizeof(i_color));
          memset(vals, 0, (r-l) * sizeof(i_color));
          count = i_glin(im, l, r, y, vals);
if (GIMME_V == G_ARRAY) {
            EXTEND(SP, count);
            for (i = 0; i < count; ++i) {
              SV *sv = make_i_color_sv(aTHX_ vals+i);
              PUSHs(sv);
            }
          }
          else if (count) {
EXTEND(SP, 1);
PUSHs(sv_2mortal(newSVpv((void *)vals, count * sizeof(i_color))));
          }
          myfree(vals);
        }

void
i_glinf(im, l, r, y)
        Imager::ImgRaw im
        i_img_dim l
        i_img_dim r
        i_img_dim y
      PREINIT:
        i_fcolor *vals;
        i_img_dim count, i;
        i_fcolor zero;
      PPCODE:
for (i = 0; i < MAXCHANNELS; ++i)
zero.channel[i] = 0;
        if (l < r) {
          vals = mymalloc((r-l) * sizeof(i_fcolor));
          for (i = 0; i < r-l; ++i)
vals[i] = zero;
          count = i_glinf(im, l, r, y, vals);
          if (GIMME_V == G_ARRAY) {
            EXTEND(SP, count);
            for (i = 0; i < count; ++i) {
              SV *sv;
              i_fcolor *col = mymalloc(sizeof(i_fcolor));
              *col = vals[i];
              sv = sv_newmortal();
              sv_setref_pv(sv, "Imager::Color::Float", (void *)col);
              PUSHs(sv);
            }
          }
          else if (count) {
            EXTEND(SP, 1);
            PUSHs(sv_2mortal(newSVpv((void *)vals, count * sizeof(i_fcolor))));
          }
          myfree(vals);
        }

Imager::ImgRaw
i_img_8_new(x, y, ch)
        i_img_dim x
        i_img_dim y
        int ch

Imager::ImgRaw
i_img_16_new(x, y, ch)
        i_img_dim x
        i_img_dim y
        int ch

Imager::ImgRaw
i_img_to_rgb16(im)
       Imager::ImgRaw im

Imager::ImgRaw
i_img_double_new(x, y, ch)
        i_img_dim x
        i_img_dim y
        int ch

Imager::ImgRaw
i_img_to_drgb(im)
       Imager::ImgRaw im

undef_int
i_tags_addn(im, name, code, idata)
        Imager::ImgRaw im
        int code
        int idata
      PREINIT:
        char *name;
        STRLEN len;
      CODE:
        if (SvOK(ST(1)))
          name = SvPV(ST(1), len);
        else
          name = NULL;
        RETVAL = i_tags_addn(&im->tags, name, code, idata);
      OUTPUT:
        RETVAL

undef_int
i_tags_add(im, name, code, data, idata)
        Imager::ImgRaw im
        int code
        int idata
      PREINIT:
        char *name;
        char *data;
        STRLEN len;
      CODE:
        if (SvOK(ST(1)))
          name = SvPV(ST(1), len);
        else
          name = NULL;
        if (SvOK(ST(3)))
          data = SvPV(ST(3), len);
        else {
          data = NULL;
          len = 0;
        }
        RETVAL = i_tags_add(&im->tags, name, code, data, len, idata);
      OUTPUT:
        RETVAL

SV *
i_tags_find(im, name, start)
        Imager::ImgRaw im
        char *name
        int start
      PREINIT:
        int entry;
      CODE:
        if (i_tags_find(&im->tags, name, start, &entry)) {
          if (entry == 0)
            RETVAL = newSVpv("0 but true", 0);
          else
            RETVAL = newSViv(entry);
        } else {
          RETVAL = &PL_sv_undef;
        }
      OUTPUT:
        RETVAL

SV *
i_tags_findn(im, code, start)
        Imager::ImgRaw im
        int code
        int start
      PREINIT:
        int entry;
      CODE:
        if (i_tags_findn(&im->tags, code, start, &entry)) {
          if (entry == 0)
            RETVAL = newSVpv("0 but true", 0);
          else
            RETVAL = newSViv(entry);
        }
        else {
          RETVAL = &PL_sv_undef;
        }
      OUTPUT:
        RETVAL

int
i_tags_delete(im, entry)
        Imager::ImgRaw im
        int entry
      CODE:
        RETVAL = i_tags_delete(&im->tags, entry);
      OUTPUT:
        RETVAL

int
i_tags_delbyname(im, name)
        Imager::ImgRaw im
        char * name
      CODE:
        RETVAL = i_tags_delbyname(&im->tags, name);
      OUTPUT:
        RETVAL

int
i_tags_delbycode(im, code)
        Imager::ImgRaw im
        int code
      CODE:
        RETVAL = i_tags_delbycode(&im->tags, code);
      OUTPUT:
        RETVAL

void
i_tags_get(im, index)
        Imager::ImgRaw im
        int index
      PPCODE:
        if (index >= 0 && index < im->tags.count) {
          i_img_tag *entry = im->tags.tags + index;
          EXTEND(SP, 5);
        
          if (entry->name) {
            PUSHs(sv_2mortal(newSVpv(entry->name, 0)));
          }
          else {
            PUSHs(sv_2mortal(newSViv(entry->code)));
          }
          if (entry->data) {
            PUSHs(sv_2mortal(newSVpvn(entry->data, entry->size)));
          }
          else {
            PUSHs(sv_2mortal(newSViv(entry->idata)));
          }
        }

void
i_tags_get_string(im, what_sv)
        Imager::ImgRaw im
        SV *what_sv
      PREINIT:
        char const *name = NULL;
        int code;
        char buffer[200];
      PPCODE:
        if (SvIOK(what_sv)) {
          code = SvIV(what_sv);
          name = NULL;
        }
        else {
          name = SvPV_nolen(what_sv);
          code = 0;
        }
        if (i_tags_get_string(&im->tags, name, code, buffer, sizeof(buffer))) {
          EXTEND(SP, 1);
          PUSHs(sv_2mortal(newSVpv(buffer, 0)));
        }

int
i_tags_count(im)
        Imager::ImgRaw im
      CODE:
        RETVAL = im->tags.count;
      OUTPUT:
        RETVAL



MODULE = Imager PACKAGE = Imager::FillHandle PREFIX=IFILL_

void
IFILL_DESTROY(fill)
        Imager::FillHandle fill

int
IFILL_CLONE_SKIP(...)
    CODE:
        (void)items; /* avoid unused warning for XS variable */
        RETVAL = 1;
    OUTPUT:
        RETVAL

MODULE = Imager PACKAGE = Imager

Imager::FillHandle
i_new_fill_solid(cl, combine)
        Imager::Color cl
        int combine

Imager::FillHandle
i_new_fill_solidf(cl, combine)
        Imager::Color::Float cl
        int combine

Imager::FillHandle
i_new_fill_hatch(fg, bg, combine, hatch, cust_hatch, dx, dy)
        Imager::Color fg
        Imager::Color bg
        int combine
        int hatch
        i_img_dim dx
        i_img_dim dy
      PREINIT:
        unsigned char *cust_hatch;
        STRLEN len;
      CODE:
        if (SvOK(ST(4))) {
          cust_hatch = (unsigned char *)SvPV(ST(4), len);
        }
        else
          cust_hatch = NULL;
        RETVAL = i_new_fill_hatch(fg, bg, combine, hatch, cust_hatch, dx, dy);
      OUTPUT:
        RETVAL

Imager::FillHandle
i_new_fill_hatchf(fg, bg, combine, hatch, cust_hatch, dx, dy)
        Imager::Color::Float fg
        Imager::Color::Float bg
        int combine
        int hatch
        i_img_dim dx
        i_img_dim dy
      PREINIT:
        unsigned char *cust_hatch;
        STRLEN len;
      CODE:
        if (SvOK(ST(4))) {
          cust_hatch = (unsigned char *)SvPV(ST(4), len);
        }
        else
          cust_hatch = NULL;
        RETVAL = i_new_fill_hatchf(fg, bg, combine, hatch, cust_hatch, dx, dy);
      OUTPUT:
        RETVAL

Imager::FillHandle
i_new_fill_image(src, matrix, xoff, yoff, combine)
        Imager::ImgRaw src
        i_img_dim xoff
        i_img_dim yoff
        int combine
      PREINIT:
        double matrix[9];
        double *matrixp;
        AV *av;
        IV len;
        SV *sv1;
        int i;
      CODE:
        if (!SvOK(ST(1))) {
          matrixp = NULL;
        }
        else {
          if (!SvROK(ST(1)) || SvTYPE(SvRV(ST(1))) != SVt_PVAV)
            croak("i_new_fill_image: parameter must be an arrayref");
av=(AV*)SvRV(ST(1));
len=av_len(av)+1;
          if (len > 9)
            len = 9;
          for (i = 0; i < len; ++i) {
sv1=(*(av_fetch(av,i,0)));
matrix[i] = SvNV(sv1);
          }
          for (; i < 9; ++i)
            matrix[i] = 0;
          matrixp = matrix;
        }
        RETVAL = i_new_fill_image(src, matrixp, xoff, yoff, combine);
      OUTPUT:
        RETVAL

MODULE = Imager PACKAGE = Imager::Internal::Hlines PREFIX=i_int_hlines_

# this class is only exposed for testing

int
i_int_hlines_testing()

#if i_int_hlines_testing()

Imager::Internal::Hlines
i_int_hlines_new(start_y, count_y, start_x, count_x)
i_img_dim start_y
int count_y
i_img_dim start_x
int count_x

Imager::Internal::Hlines
i_int_hlines_new_img(im)
Imager::ImgRaw im

void
i_int_hlines_add(hlines, y, minx, width)
Imager::Internal::Hlines hlines
i_img_dim y
i_img_dim minx
i_img_dim width

void
i_int_hlines_DESTROY(hlines)
Imager::Internal::Hlines hlines

SV *
i_int_hlines_dump(hlines)
Imager::Internal::Hlines hlines

int
i_int_hlines_CLONE_SKIP(cls)

#endif

BOOT:
        PERL_SET_GLOBAL_CALLBACKS;
PERL_PL_SET_GLOBAL_CALLBACKS;
Something went wrong with that request. Please try again.