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
|
/* curve_bend plugin for GIMP */
/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/* Revision history
* (2004/02/08) v1.3.18 hof: #133244 exit with execution error if there is
* an empty selection
* (2003/08/24) v1.3.18 hof: #119937 show busy cursor when recalculating
* preview
* (2002/09/xx) v1.1.18 mitch and gsr: clean interface
* (2000/02/16) v1.1.17b hof: added spinbuttons for rotate entry
* (2000/02/16) v1.1.17 hof: undo bugfix (#6012)
* don't call gimp_undo_push_group_end
* after gimp_displays_flush
* (1999/09/13) v1.01 hof: PDB-calls updated for gimp 1.1.9
* (1999/05/10) v1.0 hof: first public release
* (1999/04/23) v0.0 hof: coding started,
* splines and dialog parts are similar to curves.c
*/
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <glib/gstdio.h>
#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>
#include "libgimp/stdplugins-intl.h"
/* Defines */
#define PLUG_IN_PROC "plug-in-curve-bend"
#define PLUG_IN_BINARY "curve-bend"
#define PLUG_IN_ROLE "gimp-curve-bend"
#define PLUG_IN_VERSION "v1.3.18 (2003/08/26)"
#define PLUG_IN_IMAGE_TYPES "RGB*, GRAY*"
#define PLUG_IN_AUTHOR "Wolfgang Hofer (hof@hotbot.com)"
#define PLUG_IN_COPYRIGHT "Wolfgang Hofer"
#define KEY_POINTFILE "POINTFILE_CURVE_BEND"
#define KEY_POINTS "POINTS"
#define KEY_VAL_Y "VAL_Y"
#define MIDDLE 127
#define MIX_CHANNEL(a, b, m) (((a * m) + (b * (255 - m))) / 255)
#define UP_GRAPH 0x1
#define UP_PREVIEW_EXPOSE 0x4
#define UP_PREVIEW 0x8
#define UP_DRAW 0x10
#define UP_ALL 0xFF
#define GRAPH_WIDTH 256
#define GRAPH_HEIGHT 256
#define PREVIEW_SIZE_X 256
#define PREVIEW_SIZE_Y 256
#define PV_IMG_WIDTH 128
#define PV_IMG_HEIGHT 128
#define RADIUS 3
#define MIN_DISTANCE 8
#define PREVIEW_BPP 4
#define SMOOTH 0
#define GFREE 1
#define GRAPH_MASK GDK_EXPOSURE_MASK | \
GDK_POINTER_MOTION_MASK | \
GDK_POINTER_MOTION_HINT_MASK | \
GDK_ENTER_NOTIFY_MASK | \
GDK_BUTTON_PRESS_MASK | \
GDK_BUTTON_RELEASE_MASK | \
GDK_BUTTON1_MOTION_MASK
#define OUTLINE_UPPER 0
#define OUTLINE_LOWER 1
typedef struct _BenderValues BenderValues;
struct _BenderValues
{
guchar curve[2][256]; /* for curve_type freehand mode 0 <= curve <= 255 */
gdouble points[2][17][2]; /* for curve_type smooth mode 0.0 <= points <= 1.0 */
gint curve_type;
gboolean work_on_copy;
};
typedef struct _Curves Curves;
struct _Curves
{
int x, y; /* coords for last mouse click */
};
typedef struct _BenderDialog BenderDialog;
struct _BenderDialog
{
GtkWidget *shell;
GtkWidget *outline_menu;
GtkWidget *pv_widget;
GtkWidget *graph;
GtkWidget *filechooser;
GdkCursor *cursor_busy;
GimpProcedureConfig *config;
GimpDrawable *drawable;
int color;
int outline;
gint preview;
int grab_point;
int last;
int leftmost;
int rightmost;
int curve_type;
gdouble points[2][17][2]; /* 0.0 <= points <= 1.0 */
guchar curve[2][256]; /* 0 <= curve <= 255 */
gint32 *curve_ptr[2]; /* 0 <= curve_ptr <= src_drawable_width
* both arrays are allocated dynamic,
* depending on drawable width
*/
gint32 min2[2];
gint32 max2[2];
gint32 zero2[2];
gboolean show_progress;
GimpImage *preview_image;
GimpLayer *preview_layer1;
GimpLayer *preview_layer2;
gboolean run;
};
/* points Coords:
*
* 1.0 +----+----+----+----+
* | . | | |
* +--.-+--.-+----+----+
* . | . | |
* 0.5 +----+----+-.--+----+
* | | | . .
* +----+----+----+-.-.+
* | | | | |
* 0.0 +----+----+----+----+
* 0.0 0.5 1.0
*
* curve Coords:
*
* OUTLINE_UPPER OUTLINE_LOWER
*
* 255 +----+----+----+----+ 255 +----+----+----+----+
* | . | | | --- max | . | | | --- max
* +--.-+--.-+----+----+ +--.-+--.-+----+----+
* . | . | | zero ___ . | . | |
* +----+----+-.--+----+ +----+----+-.--+----+
* | | | . . --- zero | | | . .
* +----+----+----+-.-.+ ___ min +----+----+----+-.-.+ ___ min
* | | | | | | | | | |
* 0 +----+----+----+----+ 0 +----+----+----+----+
* 0 255 0 255
*/
typedef double CRMatrix[4][4];
typedef struct
{
GimpDrawable *drawable;
gint width;
gint height;
GeglBuffer *buffer;
const Babl *format;
gint x1;
gint y1;
gint x2;
gint y2;
gint index_alpha; /* 0 == no alpha, 1 == GREYA, 3 == RGBA */
gint bpp;
gint tile_width;
gint tile_height;
} t_GDRW;
typedef struct
{
gint32 y;
guchar color[4];
} t_Last;
typedef struct _Bender Bender;
typedef struct _BenderClass BenderClass;
struct _Bender
{
GimpPlugIn parent_instance;
};
struct _BenderClass
{
GimpPlugInClass parent_class;
};
#define BENDER_TYPE (bender_get_type ())
#define BENDER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), BENDER_TYPE, Bender))
GType bender_get_type (void) G_GNUC_CONST;
static GList * bender_query_procedures (GimpPlugIn *plug_in);
static GimpProcedure * bender_create_procedure (GimpPlugIn *plug_in,
const gchar *name);
static GimpValueArray * bender_run (GimpProcedure *procedure,
GimpRunMode run_mode,
GimpImage *image,
GimpDrawable **drawables,
GimpProcedureConfig *config,
gpointer run_data);
static BenderDialog * bender_new_dialog (GimpProcedure *procedure,
GimpProcedureConfig *config,
GimpDrawable *drawable);
static void bender_update (BenderDialog *,
int);
static void bender_plot_curve (BenderDialog *,
int,
int,
int,
int,
gint32,
gint32,
gint);
static void bender_calculate_curve (BenderDialog *,
gint32,
gint32,
gint);
static void bender_global_notify (GtkWidget *,
gpointer);
static void bender_type_callback (GtkWidget *,
gpointer);
static void bender_reset_callback (GtkWidget *,
gpointer);
static void bender_copy_callback (GtkWidget *,
gpointer);
static void bender_copy_inv_callback (GtkWidget *,
gpointer);
static void bender_swap_callback (GtkWidget *,
gpointer);
static void bender_preview_update (GtkWidget *,
gpointer);
static void bender_preview_update_once (GtkWidget *,
gpointer);
static void bender_load_callback (GtkWidget *,
BenderDialog *);
static void bender_save_callback (GtkWidget *,
BenderDialog *);
static gint bender_graph_events (GtkWidget *,
GdkEvent *,
BenderDialog *);
static gint bender_graph_draw (GtkWidget *,
cairo_t *,
BenderDialog *);
static void bender_CR_compose (CRMatrix,
CRMatrix,
CRMatrix);
static void bender_init_min_max (BenderDialog *,
gint32);
static BenderDialog * do_dialog (GimpProcedure *procedure,
GimpProcedureConfig *config,
GimpDrawable *drawable);
static void p_init_gdrw (t_GDRW *gdrw,
GimpDrawable *drawable,
int shadow);
static void p_end_gdrw (t_GDRW *gdrw);
static GimpLayer * p_main_bend (BenderDialog *cd,
GimpDrawable *original_drawable,
GimpProcedureConfig *config,
gint work_on_copy);
static GimpImage * p_create_pv_image (GimpDrawable *src_drawable,
GimpLayer **layer);
static void p_render_preview (BenderDialog *cd,
GimpLayer *layer);
static void p_get_pixel (t_GDRW *gdrw,
gint32 x,
gint32 y,
guchar *pixel);
static void p_put_pixel (t_GDRW *gdrw,
gint32 x,
gint32 y,
guchar *pixel);
static void p_put_mix_pixel (t_GDRW *gdrw,
gint32 x,
gint32 y,
guchar *color,
gint32 nb_curvy,
gint32 nb2_curvy,
gint32 curvy);
static void p_stretch_curves (BenderDialog *cd,
gint32 xmax,
gint32 ymax);
static void p_cd_to_bval (BenderDialog *cd,
BenderValues *bval);
static void p_cd_from_bval (BenderDialog *cd,
BenderValues *bval);
static void p_store_values (GimpProcedureConfig *config,
BenderDialog *cd);
static void p_retrieve_values (GimpProcedureConfig *config,
BenderDialog *cd);
static void p_bender_calculate_iter_curve (BenderDialog *cd,
GimpProcedureConfig *config,
gint32 xmax,
gint32 ymax);
static int p_save_pointfile (BenderDialog *cd,
const gchar *filename);
G_DEFINE_TYPE (Bender, bender, GIMP_TYPE_PLUG_IN)
GIMP_MAIN (BENDER_TYPE)
DEFINE_STD_SET_I18N
static CRMatrix CR_basis =
{
{ -0.5, 1.5, -1.5, 0.5 },
{ 1.0, -2.5, 2.0, -0.5 },
{ -0.5, 0.0, 0.5, 0.0 },
{ 0.0, 1.0, 0.0, 0.0 },
};
static int gb_debug = FALSE;
static void
bender_class_init (BenderClass *klass)
{
GimpPlugInClass *plug_in_class = GIMP_PLUG_IN_CLASS (klass);
plug_in_class->query_procedures = bender_query_procedures;
plug_in_class->create_procedure = bender_create_procedure;
plug_in_class->set_i18n = STD_SET_I18N;
}
static void
bender_init (Bender *bender)
{
}
static GList *
bender_query_procedures (GimpPlugIn *plug_in)
{
return g_list_append (NULL, g_strdup (PLUG_IN_PROC));
}
static GimpProcedure *
bender_create_procedure (GimpPlugIn *plug_in,
const gchar *name)
{
GimpProcedure *procedure = NULL;
if (! strcmp (name, PLUG_IN_PROC))
{
procedure = gimp_image_procedure_new (plug_in, name,
GIMP_PDB_PROC_TYPE_PLUGIN,
bender_run, NULL, NULL);
gimp_procedure_set_image_types (procedure, PLUG_IN_IMAGE_TYPES);
gimp_procedure_set_sensitivity_mask (procedure,
GIMP_PROCEDURE_SENSITIVE_DRAWABLE);
gimp_procedure_set_menu_label (procedure, _("_Curve Bend..."));
gimp_procedure_add_menu_path (procedure, "<Image>/Filters/Distorts");
gimp_procedure_set_documentation
(procedure,
_("Bend the image using two control curves"),
_("This plug-in bends the active layer. "
"If there is a current selection it is copied to "
"floating selection and the curve_bend distortion "
"is done on the floating selection. If "
"work_on_copy parameter is TRUE, the curve_bend "
"distortion is done on a copy of the active layer "
"(or floating selection). The upper and lower edges "
"are bent in shape of 2 spline curves. Both (upper "
"and lower) curves are determined by up to 17 points "
"or by 256 Y-Values if curve_type == 1 (freehand "
"mode). If rotation is not 0, the layer is rotated "
"before and rotated back after the bend operation. "
"This enables bending in other directions than "
"vertical. Bending usually changes the size of "
"the handled layer. This plug-in sets the offsets "
"of the handled layer to keep its center at the "
"same position."),
name);
gimp_procedure_set_attribution (procedure,
PLUG_IN_AUTHOR,
PLUG_IN_COPYRIGHT,
PLUG_IN_VERSION);
gimp_procedure_add_double_argument (procedure, "rotation",
_("Rotat_e"),
_("Direction {angle 0 to 360 degree } "
"of the bend effect"),
0, 360, 0,
G_PARAM_READWRITE);
gimp_procedure_add_boolean_argument (procedure, "smoothing",
_("Smoo_thing"),
_("Smoothing"),
TRUE,
G_PARAM_READWRITE);
gimp_procedure_add_boolean_argument (procedure, "antialias",
_("_Antialiasing"),
_("Antialias"),
TRUE,
G_PARAM_READWRITE);
gimp_procedure_add_boolean_argument (procedure, "work-on-copy",
_("Work on cop_y"),
_("Copy the drawable and bend the copy"),
FALSE,
G_PARAM_READWRITE);
gimp_procedure_add_choice_argument (procedure, "curve-type",
_("Cur_ve Type"),
_("Whether to use Smooth (17 points) or "
"Freehand (256 points) to draw the curve"),
gimp_choice_new_with_values ("smooth", SMOOTH, _("Smooth"), NULL,
"freehand", GFREE, _("Freehand"), NULL,
NULL),
"smooth",
G_PARAM_READWRITE);
gimp_procedure_add_choice_argument (procedure, "curve-border",
_("Curve for _Border"),
_("Choose the active border line to edit"),
gimp_choice_new_with_values ("upper", OUTLINE_UPPER, _("Upper"), NULL,
"lower", OUTLINE_LOWER, C_("adjective", "Lower"), NULL,
NULL),
"upper",
G_PARAM_READWRITE);
gimp_procedure_add_double_array_argument (procedure, "upper-point-x",
_("Upper point X"),
_("Array of 17 x point coords "
"{ 0.0 <= x <= 1.0 or -1 for unused point }"),
G_PARAM_READWRITE);
gimp_procedure_add_double_array_argument (procedure, "upper-point-y",
_("Upper point Y"),
_("Array of 17 y point coords "
"{ 0.0 <= y <= 1.0 or -1 for unused point }"),
G_PARAM_READWRITE);
gimp_procedure_add_double_array_argument (procedure, "lower-point-x",
_("Lower point X"),
_("Array of 17 x point coords "
"{ 0.0 <= x <= 1.0 or -1 for unused point }"),
G_PARAM_READWRITE);
gimp_procedure_add_double_array_argument (procedure, "lower-point-y",
_("Lower point Y"),
_("Array of 17 y point coords "
"{ 0.0 <= y <= 1.0 or -1 for unused point }"),
G_PARAM_READWRITE);
gimp_procedure_add_bytes_argument (procedure, "upper-val-y",
_("Upper val Y"),
_("Array of 256 y freehand coords "
"{ 0 <= y <= 255 }"),
G_PARAM_READWRITE);
gimp_procedure_add_bytes_argument (procedure, "lower-val-y",
_("Lower val Y"),
_("Array of 256 y freehand coords "
"{ 0 <= y <= 255 }"),
G_PARAM_READWRITE);
gimp_procedure_add_layer_return_value (procedure, "bent-layer",
_("Bent layer"),
_("The transformed layer"),
FALSE,
G_PARAM_READWRITE);
gimp_procedure_add_bytes_aux_argument (procedure, "settings-data",
"Settings data",
"TODO: eventually we must implement proper args for every settings",
GIMP_PARAM_READWRITE);
}
return procedure;
}
/* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX PDB_STUFF XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
static GimpItem *
p_gimp_rotate (GimpImage *image,
GimpDrawable *drawable,
gint32 interpolation,
gdouble angle_deg)
{
gdouble angle_rad;
GimpItem *rc;
angle_rad = (angle_deg * G_PI) / 180.0;
gimp_context_push ();
if (! interpolation)
gimp_context_set_interpolation (GIMP_INTERPOLATION_NONE);
gimp_context_set_transform_resize (GIMP_TRANSFORM_RESIZE_ADJUST);
rc = gimp_item_transform_rotate (GIMP_ITEM (drawable),
angle_rad,
TRUE /*auto_center*/,
-1.0 /*center_x*/,
-1.0 /*center_y*/);
gimp_context_pop ();
if (! rc)
g_printerr ("Error: gimp_drawable_transform_rotate_default call failed\n");
return rc;
}
/* ============================================================================
* p_if_selection_float_it
* ============================================================================
*/
static GimpLayer *
p_if_selection_float_it (GimpImage *image,
GimpLayer *layer)
{
if (! gimp_layer_is_floating_sel (layer))
{
GimpSelection *sel_channel;
gint32 x1, x2, y1, y2;
gint32 non_empty;
/* check and see if we have a selection mask */
sel_channel = gimp_image_get_selection (image);
gimp_selection_bounds (image, &non_empty, &x1, &y1, &x2, &y2);
if (non_empty && sel_channel)
{
const GimpDrawable *drawables[2] = { (GimpDrawable *) layer, NULL };
/* selection is TRUE, make a layer (floating selection) from
the selection */
if (gimp_edit_copy (drawables))
{
GimpDrawable **layers;
layers = gimp_edit_paste (GIMP_DRAWABLE (layer), FALSE);
/* Since we explicitly copied a single layer, there should
* be no more than a single layer in the paste as well.
*/
layer = (GimpLayer *) (layers && layers[0] ? layers[0] : NULL);
g_free (layers);
}
else
{
return NULL;
}
}
}
return layer;
}
/* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX END_PDB_STUFF XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
static GimpValueArray *
bender_run (GimpProcedure *procedure,
GimpRunMode run_mode,
GimpImage *image,
GimpDrawable **drawables,
GimpProcedureConfig *config,
gpointer run_data)
{
const gchar *env;
GimpValueArray *return_vals;
BenderDialog *cd = NULL;
GimpDrawable *active_drawable = NULL;
GimpDrawable *drawable;
GimpLayer *layer = NULL;
GimpLayer *bent_layer = NULL;
GError *error = NULL;
gegl_init (NULL, NULL);
if (gimp_core_object_array_get_length ((GObject **) drawables) != 1)
{
GError *error = NULL;
g_set_error (&error, GIMP_PLUG_IN_ERROR, 0,
_("Procedure '%s' only works with one drawable."),
PLUG_IN_PROC);
return gimp_procedure_new_return_values (procedure,
GIMP_PDB_EXECUTION_ERROR,
error);
}
else
{
drawable = drawables[0];
}
env = g_getenv ("BEND_DEBUG");
if (env != NULL)
if ((*env != 'n') && (*env != 'N')) gb_debug = 1;
if (! GIMP_IS_LAYER (drawable))
{
g_set_error (&error, 0, 0, "%s",
_("Can operate on layers only "
"(but was called on channel or mask)."));
return gimp_procedure_new_return_values (procedure,
GIMP_PDB_EXECUTION_ERROR,
error);
}
layer = GIMP_LAYER (drawable);
/* check for layermask */
if (gimp_layer_get_mask (layer))
{
g_set_error (&error, 0, 0, "%s",
_("Cannot operate on layers with masks."));
return gimp_procedure_new_return_values (procedure,
GIMP_PDB_EXECUTION_ERROR,
error);
}
/* if there is a selection, make it the floating selection layer */
active_drawable = GIMP_DRAWABLE (p_if_selection_float_it (image, layer));
if (! active_drawable)
{
/* could not float the selection because selection rectangle
* is completely empty return GIMP_PDB_EXECUTION_ERROR
*/
g_set_error (&error, 0, 0, "%s",
_("Cannot operate on empty selections."));
return gimp_procedure_new_return_values (procedure,
GIMP_PDB_EXECUTION_ERROR,
error);
}
switch (run_mode)
{
case GIMP_RUN_INTERACTIVE:
/* Get information from the dialog */
cd = do_dialog (procedure, config, active_drawable);
cd->show_progress = TRUE;
break;
case GIMP_RUN_NONINTERACTIVE:
cd = g_new (BenderDialog, 1);
cd->run = TRUE;
cd->show_progress = TRUE;
cd->drawable = active_drawable;
cd->config = config;
break;
case GIMP_RUN_WITH_LAST_VALS:
cd = g_new (BenderDialog, 1);
cd->run = TRUE;
cd->show_progress = TRUE;
cd->drawable = active_drawable;
cd->config = config;
p_retrieve_values (config, cd); /* Possibly retrieve data from a previous run */
break;
}
if (! cd)
{
return gimp_procedure_new_return_values (procedure,
GIMP_PDB_EXECUTION_ERROR,
error);
}
if (cd->run)
{
gboolean work_on_copy;
g_object_get (config,
"work-on-copy", &work_on_copy,
NULL);
gimp_image_undo_group_start (image);
bent_layer = p_main_bend (cd, cd->drawable,
config, work_on_copy);
gimp_image_undo_group_end (image);
/* Store variable states for next run */
if (run_mode == GIMP_RUN_INTERACTIVE)
p_store_values (config, cd);
}
else
{
return gimp_procedure_new_return_values (procedure,
GIMP_PDB_CANCEL,
NULL);
}
if (run_mode != GIMP_RUN_NONINTERACTIVE)
gimp_displays_flush ();
return_vals = gimp_procedure_new_return_values (procedure,
GIMP_PDB_SUCCESS,
NULL);
GIMP_VALUES_SET_LAYER (return_vals, 1, bent_layer);
return return_vals;
}
static int
p_save_pointfile (BenderDialog *cd,
const gchar *filename)
{
FILE *fp;
gint j;
fp = g_fopen(filename, "w+b");
if (! fp)
{
g_message (_("Could not open '%s' for writing: %s"),
gimp_filename_to_utf8 (filename), g_strerror (errno));
return -1;
}
fprintf (fp, "%s\n", KEY_POINTFILE);
fprintf (fp, "VERSION 1.0\n\n");
fprintf (fp, "# points for upper and lower smooth curve (0.0 <= pt <= 1.0)\n");
fprintf (fp, "# there are upto 17 points where unused points are set to -1\n");
fprintf (fp, "# UPPERX UPPERY LOWERX LOWERY\n");
fprintf (fp, "\n");
for(j = 0; j < 17; j++)
{
fprintf (fp, "%s %+.6f %+.6f %+.6f %+.6f\n", KEY_POINTS,
(float)cd->points[OUTLINE_UPPER][j][0],
(float)cd->points[OUTLINE_UPPER][j][1],
(float)cd->points[OUTLINE_LOWER][j][0],
(float)cd->points[OUTLINE_LOWER][j][1] );
}
fprintf (fp, "\n");
fprintf (fp, "# y values for upper/lower freehand curve (0 <= y <= 255) \n");
fprintf (fp, "# there must be exactly 256 y values \n");
fprintf (fp, "# UPPER_Y LOWER_Y\n");
fprintf (fp, "\n");
for (j = 0; j < 256; j++)
{
fprintf (fp, "%s %3d %3d\n", KEY_VAL_Y,
(int)cd->curve[OUTLINE_UPPER][j],
(int)cd->curve[OUTLINE_LOWER][j]);
}
fclose (fp);
return 0;
}
static int
p_load_pointfile (BenderDialog *cd,
const gchar *filename)
{
gint pi, ci, n, len;
FILE *fp;
char buff[2000];
float fux, fuy, flx, fly;
gint iuy, ily ;
fp = g_fopen(filename, "rb");
if (! fp)
{
g_message (_("Could not open '%s' for reading: %s"),
gimp_filename_to_utf8 (filename), g_strerror (errno));
return -1;
}
pi = 0;
ci = 0;
if (! fgets (buff, 2000 - 1, fp))
{
g_message (_("Error while reading '%s': %s"),
gimp_filename_to_utf8 (filename), g_strerror (errno));
fclose (fp);
return -1;
}
if (strncmp (buff, KEY_POINTFILE, strlen(KEY_POINTFILE)) == 0)
{
while (NULL != fgets (buff, 2000-1, fp))
{
len = strlen(KEY_POINTS);
if (strncmp (buff, KEY_POINTS, len) == 0)
{
n = sscanf (&buff[len],
"%f %f %f %f", &fux, &fuy, &flx, &fly);
if ((n == 4) && (pi < 17))
{
cd->points[OUTLINE_UPPER][pi][0] = fux;
cd->points[OUTLINE_UPPER][pi][1] = fuy;
cd->points[OUTLINE_LOWER][pi][0] = flx;
cd->points[OUTLINE_LOWER][pi][1] = fly;
pi++;
}
else
{
g_printf("warning: BAD points[%d] in file %s are ignored\n", pi, filename);
}
}
len = strlen (KEY_VAL_Y);
if (strncmp (buff, KEY_VAL_Y, len) == 0)
{
n = sscanf (&buff[len], "%d %d", &iuy, &ily);
if ((n == 2) && (ci < 256))
{
cd->curve[OUTLINE_UPPER][ci] = iuy;
cd->curve[OUTLINE_LOWER][ci] = ily;
ci++;
}
else
{
g_printf("warning: BAD y_vals[%d] in file %s are ignored\n", ci, filename);
}
}
}
}
fclose (fp);
return 0;
}
static void
p_cd_to_bval (BenderDialog *cd,
BenderValues *bval)
{
gint i, j;
for (i = 0; i < 2; i++)
{
for (j = 0; j < 256; j++)
{
bval->curve[i][j] = cd->curve[i][j];
}
for (j = 0; j < 17; j++)
{
bval->points[i][j][0] = cd->points[i][j][0]; /* x */
bval->points[i][j][1] = cd->points[i][j][1]; /* y */
}
}
}
static void
p_cd_from_bval (BenderDialog *cd,
BenderValues *bval)
{
gint i, j;
for (i = 0; i < 2; i++)
{
for (j = 0; j < 256; j++)
{
cd->curve[i][j] = bval->curve[i][j];
}
for (j = 0; j < 17; j++)
{
cd->points[i][j][0] = bval->points[i][j][0]; /* x */
cd->points[i][j][1] = bval->points[i][j][1]; /* y */
}
}
}
static void
p_store_values (GimpProcedureConfig *config,
BenderDialog *cd)
{
BenderValues bval;
GBytes *settings_bytes;
p_cd_to_bval (cd, &bval);
settings_bytes = g_bytes_new (&bval, sizeof (BenderValues));
g_object_set (config, "settings-data", settings_bytes, NULL);
g_bytes_unref (settings_bytes);
}
static void
p_retrieve_values (GimpProcedureConfig *config,
BenderDialog *cd)
{
GBytes *settings_bytes;
g_object_get (config, "settings-data", &settings_bytes, NULL);
if (settings_bytes != NULL && g_bytes_get_size (settings_bytes) == sizeof (BenderValues))
{
BenderValues bval;
bval = *((BenderValues *) g_bytes_get_data (settings_bytes, NULL));
p_cd_from_bval(cd, &bval);
}
g_bytes_unref (settings_bytes);
}
/* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
/* curves machinery */
/* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
static BenderDialog *
do_dialog (GimpProcedure *procedure,
GimpProcedureConfig *config,
GimpDrawable *drawable)
{
BenderDialog *cd;
/* Init GTK */
gimp_ui_init (PLUG_IN_BINARY);
/* The curve_bend dialog */
cd = bender_new_dialog (procedure, config, drawable);
/* create temporary image (with a small copy of drawable) for the preview */
cd->preview_image = p_create_pv_image (drawable,
&cd->preview_layer1);
cd->preview_layer2 = NULL;
if (! gtk_widget_get_visible (cd->shell))
gtk_widget_show (cd->shell);
bender_update (cd, UP_GRAPH | UP_DRAW | UP_PREVIEW_EXPOSE);
cd->run = gimp_procedure_dialog_run (GIMP_PROCEDURE_DIALOG (cd->shell));
gtk_widget_destroy (cd->shell);
gimp_image_delete (cd->preview_image);
cd->preview_image = NULL;
cd->preview_layer1 = NULL;
cd->preview_layer2 = NULL;
return cd;
}
/**************************/
/* Select Curves dialog */
/**************************/
static BenderDialog *
bender_new_dialog (GimpProcedure *procedure,
GimpProcedureConfig *config,
GimpDrawable *drawable)
{
BenderDialog *cd;
GtkWidget *vbox;
GtkWidget *hbox;
GtkWidget *vbox2;
GtkWidget *frame;
GtkWidget *toggle;
GtkWidget *button;
GdkDisplay *display;
gint i, j;
cd = g_new (BenderDialog, 1);
cd->config = config;
cd->preview = FALSE;
cd->filechooser = NULL;
cd->show_progress = FALSE;
cd->drawable = drawable;
cd->color = gimp_drawable_is_rgb (drawable);
cd->run = FALSE;
for (i = 0; i < 2; i++)
for (j = 0; j < 256; j++)
cd->curve[i][j] = MIDDLE;
cd->grab_point = -1;
for (i = 0; i < 2; i++)
{
for (j = 0; j < 17; j++)
{
cd->points[i][j][0] = -1;
cd->points[i][j][1] = -1;
}
cd->points[i][0][0] = 0.0; /* x */
cd->points[i][0][1] = 0.5; /* y */
cd->points[i][16][0] = 1.0; /* x */
cd->points[i][16][1] = 0.5; /* y */
}
p_retrieve_values (config, cd); /* Possibly retrieve data from a previous run */
/* The shell and main vbox */
cd->shell = gimp_procedure_dialog_new (procedure,
GIMP_PROCEDURE_CONFIG (config),
_("Curve Bend"));
gimp_dialog_set_alternative_button_order (GTK_DIALOG (cd->shell),
GTK_RESPONSE_OK,
GTK_RESPONSE_CANCEL,
-1);
gimp_window_set_transient (GTK_WINDOW (cd->shell));
/* busy cursor */
display = gtk_widget_get_display (cd->shell);
cd->cursor_busy = gdk_cursor_new_for_display (display, GDK_WATCH);
/* Left side column */
/* Config-connected widgets first */
gimp_procedure_dialog_get_label (GIMP_PROCEDURE_DIALOG (cd->shell),
"option-label", _("Options"), FALSE,
FALSE);
gimp_procedure_dialog_fill_box (GIMP_PROCEDURE_DIALOG (cd->shell),
"options-hbox", "rotation", "smoothing",
"antialias", "work-on-copy", NULL);
toggle = gimp_procedure_dialog_get_widget (GIMP_PROCEDURE_DIALOG (cd->shell),
"rotation", G_TYPE_NONE);
g_signal_connect (toggle, "value-changed",
G_CALLBACK (bender_global_notify),
cd);
toggle = gimp_procedure_dialog_get_widget (GIMP_PROCEDURE_DIALOG (cd->shell),
"smoothing", G_TYPE_NONE);
g_signal_connect (toggle, "toggled",
G_CALLBACK (bender_global_notify),
cd);
toggle = gimp_procedure_dialog_get_widget (GIMP_PROCEDURE_DIALOG (cd->shell),
"antialias", G_TYPE_NONE);
g_signal_connect (toggle, "toggled",
G_CALLBACK (bender_global_notify),
cd);
gimp_procedure_dialog_fill_frame (GIMP_PROCEDURE_DIALOG (cd->shell),
"option-frame", "option-label", FALSE,
"options-hbox");
vbox = gimp_procedure_dialog_fill_box (GIMP_PROCEDURE_DIALOG (cd->shell),
"curve-left-col", "option-frame",
NULL);
/* GUI-only widgets */
/* Preview area, top of column */
frame = gimp_frame_new (_("Preview"));
gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0);
gtk_widget_set_visible (frame, TRUE);
gtk_box_reorder_child (GTK_BOX (vbox), frame, 0);
vbox2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
gtk_container_add (GTK_CONTAINER (frame), vbox2);
gtk_widget_set_visible (vbox2, TRUE);
/* The range drawing area */
frame = gtk_frame_new (NULL);
gtk_widget_set_halign (frame, GTK_ALIGN_CENTER);
gtk_widget_set_valign (frame, GTK_ALIGN_CENTER);
gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
gtk_box_pack_start (GTK_BOX (vbox2), frame, FALSE, FALSE, 0);
gtk_widget_set_visible (frame, TRUE);
cd->pv_widget = gimp_preview_area_new ();
gtk_widget_set_size_request (cd->pv_widget,
PREVIEW_SIZE_X, PREVIEW_SIZE_Y);
gtk_container_add (GTK_CONTAINER (frame), cd->pv_widget);
gtk_widget_set_visible (cd->pv_widget, TRUE);
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
gtk_box_pack_end (GTK_BOX (vbox2), hbox, FALSE, FALSE, 0);
gtk_widget_set_visible (hbox, TRUE);
/* The preview button */
button = gtk_button_new_with_mnemonic (_("_Preview Once"));
gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
gtk_widget_set_visible (button, TRUE);
g_signal_connect (button, "clicked",
G_CALLBACK (bender_preview_update_once),
cd);
/* The preview toggle */
toggle = gtk_check_button_new_with_mnemonic (_("Automatic pre_view"));
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), cd->preview);
gtk_box_pack_start (GTK_BOX (hbox), toggle, FALSE, FALSE, 0);
gtk_widget_set_visible (toggle, TRUE);
g_signal_connect (toggle, "toggled",
G_CALLBACK (bender_preview_update),
cd);
/* The curves graph */
/* Config-connected widgets first */
gimp_procedure_dialog_get_widget (GIMP_PROCEDURE_DIALOG (cd->shell),
"curve-type", GIMP_TYPE_INT_RADIO_FRAME);
g_signal_connect (config, "notify::curve-type",
G_CALLBACK (bender_type_callback), cd);
gimp_procedure_dialog_get_widget (GIMP_PROCEDURE_DIALOG (cd->shell),
"curve-border",
GIMP_TYPE_INT_RADIO_FRAME);
hbox = gimp_procedure_dialog_fill_box (GIMP_PROCEDURE_DIALOG (cd->shell),
"curve-hbox", "curve-border",
"curve-type", NULL);
gtk_orientable_set_orientation (GTK_ORIENTABLE (hbox),
GTK_ORIENTATION_HORIZONTAL);
vbox = gimp_procedure_dialog_fill_box (GIMP_PROCEDURE_DIALOG (cd->shell),
"curve-right-col", "curve-hbox",
NULL);
gimp_procedure_dialog_get_label (GIMP_PROCEDURE_DIALOG (cd->shell),
"modify-label", _("Modify Curves"), FALSE,
FALSE);
gimp_procedure_dialog_fill_frame (GIMP_PROCEDURE_DIALOG (cd->shell),
"curve-right-frame", "modify-label", FALSE,
"curve-right-col");
/* GUI-only widgets */
cd->graph = gtk_drawing_area_new ();
gtk_widget_set_halign (cd->graph, GTK_ALIGN_CENTER);
gtk_widget_set_valign (cd->graph, GTK_ALIGN_CENTER);
gtk_widget_set_size_request (cd->graph,
GRAPH_WIDTH + RADIUS * 2,
GRAPH_HEIGHT + RADIUS * 2);
gtk_widget_set_events (cd->graph, GRAPH_MASK);
gtk_box_pack_start (GTK_BOX (vbox), cd->graph, FALSE, FALSE, 0);
gtk_widget_set_visible (cd->graph, TRUE);
g_signal_connect (cd->graph, "event",
G_CALLBACK (bender_graph_events),
cd);
g_signal_connect (cd->graph, "draw",
G_CALLBACK (bender_graph_draw),
cd);
gtk_box_reorder_child (GTK_BOX (vbox), cd->graph, 0);
/* hbox for curve options */
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 4);
gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
gtk_widget_set_visible (hbox, TRUE);
/* The Copy button */
button = gtk_button_new_with_mnemonic (_("_Copy"));
gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);
gtk_widget_set_visible (button, TRUE);
gimp_help_set_help_data (button,
_("Copy the active curve to the other border"), NULL);
g_signal_connect (button, "clicked",
G_CALLBACK (bender_copy_callback),
cd);
/* The CopyInv button */
button = gtk_button_new_with_mnemonic (_("_Mirror"));
gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);
gtk_widget_set_visible (button, TRUE);
gimp_help_set_help_data (button,
_("Mirror the active curve to the other border"),
NULL);
g_signal_connect (button, "clicked",
G_CALLBACK (bender_copy_inv_callback),
cd);
/* The Swap button */
button = gtk_button_new_with_mnemonic (_("S_wap"));
gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);
gtk_widget_set_visible (button, TRUE);
gimp_help_set_help_data (button,
_("Swap the two curves"), NULL);
g_signal_connect (button, "clicked",
G_CALLBACK (bender_swap_callback),
cd);
/* The Reset button */
button = gtk_button_new_with_mnemonic (_("_Reset"));
gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);
gtk_widget_set_visible (button, TRUE);
gimp_help_set_help_data (button,
_("Reset the active curve"), NULL);
g_signal_connect (button, "clicked",
G_CALLBACK (bender_reset_callback),
cd);
/* hbox for curve load and save */
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 4);
gtk_box_pack_end (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
gtk_widget_set_visible (hbox, TRUE);
/* The Load button */
button = gtk_button_new_with_mnemonic (_("_Open"));
gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);
gtk_widget_set_visible (button, TRUE);
gimp_help_set_help_data (button,
_("Load the curves from a file"), NULL);
g_signal_connect (button, "clicked",
G_CALLBACK (bender_load_callback),
cd);
/* The Save button */
button = gtk_button_new_with_mnemonic (_("_Save"));
gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);
gtk_widget_set_visible (button, TRUE);
gimp_help_set_help_data (button,
_("Save the curves to a file"), NULL);
g_signal_connect (button, "clicked",
G_CALLBACK (bender_save_callback),
cd);
/* Combine the two columns */
hbox = gimp_procedure_dialog_fill_box (GIMP_PROCEDURE_DIALOG (cd->shell),
"curve-columns", "curve-left-col",
"curve-right-frame", NULL);
gtk_orientable_set_orientation (GTK_ORIENTABLE (hbox),
GTK_ORIENTATION_HORIZONTAL);
gimp_procedure_dialog_fill (GIMP_PROCEDURE_DIALOG (cd->shell),
"curve-columns", NULL);
return cd;
}
static void
bender_update (BenderDialog *cd,
int update)
{
if (update & UP_PREVIEW)
{
gdk_window_set_cursor (gtk_widget_get_window (GTK_WIDGET (cd->shell)),
cd->cursor_busy);
gdk_display_flush (gtk_widget_get_display (cd->shell));
if (cd->preview_layer2)
gimp_image_remove_layer (cd->preview_image, cd->preview_layer2);
cd->preview_layer2 = p_main_bend (cd, GIMP_DRAWABLE (cd->preview_layer1),
cd->config, TRUE /* work_on_copy*/ );
p_render_preview (cd, cd->preview_layer2);
if (update & UP_DRAW)
gtk_widget_queue_draw (cd->pv_widget);
gdk_window_set_cursor (gtk_widget_get_window (GTK_WIDGET (cd->shell)),
NULL);
}
if (update & UP_PREVIEW_EXPOSE)
{
/* on expose just redraw cd->preview_layer2
* that holds the bent version of the preview (if there is one)
*/
if (! cd->preview_layer2)
cd->preview_layer2 = p_main_bend (cd, GIMP_DRAWABLE (cd->preview_layer1),
cd->config, TRUE /* work_on_copy*/ );
p_render_preview (cd, cd->preview_layer2);
if (update & UP_DRAW)
gtk_widget_queue_draw (cd->pv_widget);
}
if ((update & UP_GRAPH) && (update & UP_DRAW))
gtk_widget_queue_draw (cd->graph);
}
static void
bender_plot_curve (BenderDialog *cd,
int p1,
int p2,
int p3,
int p4,
gint32 xmax,
gint32 ymax,
gint fix255)
{
CRMatrix geometry;
CRMatrix tmp1, tmp2;
CRMatrix deltas;
double x, dx, dx2, dx3;
double y, dy, dy2, dy3;
double d, d2, d3;
int lastx, lasty;
gint32 newx, newy;
gint32 ntimes;
gint32 i;
gint outline;
outline = gimp_procedure_config_get_choice_id (GIMP_PROCEDURE_CONFIG (cd->config),
"curve-border");
/* construct the geometry matrix from the segment */
for (i = 0; i < 4; i++)
{
geometry[i][2] = 0;
geometry[i][3] = 0;
}
geometry[0][0] = (cd->points[outline][p1][0] * xmax);
geometry[1][0] = (cd->points[outline][p2][0] * xmax);
geometry[2][0] = (cd->points[outline][p3][0] * xmax);
geometry[3][0] = (cd->points[outline][p4][0] * xmax);
geometry[0][1] = (cd->points[outline][p1][1] * ymax);
geometry[1][1] = (cd->points[outline][p2][1] * ymax);
geometry[2][1] = (cd->points[outline][p3][1] * ymax);
geometry[3][1] = (cd->points[outline][p4][1] * ymax);
/* subdivide the curve ntimes (1000) times */
ntimes = 4 * xmax;
/* ntimes can be adjusted to give a finer or coarser curve */
d = 1.0 / ntimes;
d2 = d * d;
d3 = d * d * d;
/* construct a temporary matrix for determining the forward differencing deltas */
tmp2[0][0] = 0; tmp2[0][1] = 0; tmp2[0][2] = 0; tmp2[0][3] = 1;
tmp2[1][0] = d3; tmp2[1][1] = d2; tmp2[1][2] = d; tmp2[1][3] = 0;
tmp2[2][0] = 6*d3; tmp2[2][1] = 2*d2; tmp2[2][2] = 0; tmp2[2][3] = 0;
tmp2[3][0] = 6*d3; tmp2[3][1] = 0; tmp2[3][2] = 0; tmp2[3][3] = 0;
/* compose the basis and geometry matrices */
bender_CR_compose (CR_basis, geometry, tmp1);
/* compose the above results to get the deltas matrix */
bender_CR_compose (tmp2, tmp1, deltas);
/* extract the x deltas */
x = deltas[0][0];
dx = deltas[1][0];
dx2 = deltas[2][0];
dx3 = deltas[3][0];
/* extract the y deltas */
y = deltas[0][1];
dy = deltas[1][1];
dy2 = deltas[2][1];
dy3 = deltas[3][1];
lastx = CLAMP (x, 0, xmax);
lasty = CLAMP (y, 0, ymax);
if (fix255)
{
cd->curve[outline][lastx] = lasty;
}
else
{
cd->curve_ptr[outline][lastx] = lasty;
if(gb_debug) g_printf("bender_plot_curve xmax:%d ymax:%d\n",
(gint) xmax, (gint) ymax);
}
/* loop over the curve */
for (i = 0; i < ntimes; i++)
{
/* increment the x values */
x += dx;
dx += dx2;
dx2 += dx3;
/* increment the y values */
y += dy;
dy += dy2;
dy2 += dy3;
newx = CLAMP ((ROUND (x)), 0, xmax);
newy = CLAMP ((ROUND (y)), 0, ymax);
/* if this point is different than the last one...then draw it */
if ((lastx != newx) || (lasty != newy))
{
if (fix255)
{
/* use fixed array size (for the curve graph) */
cd->curve[outline][newx] = newy;
}
else
{
/* use dynamic allocated curve_ptr (for the real curve) */
cd->curve_ptr[outline][newx] = newy;
if (gb_debug) g_printf("outline: %d cX: %d cY: %d\n",
(gint) outline, (int) newx, (int) newy);
}
}
lastx = newx;
lasty = newy;
}
}
static void
bender_calculate_curve (BenderDialog *cd,
gint32 xmax,
gint32 ymax,
gint fix255)
{
gint i;
gint points[17];
gint num_pts;
gint p1, p2, p3, p4;
gint xmid;
gint yfirst, ylast;
gint curve_type;
gint outline;
curve_type = gimp_procedure_config_get_choice_id (GIMP_PROCEDURE_CONFIG (cd->config),
"curve-type");
outline = gimp_procedure_config_get_choice_id (GIMP_PROCEDURE_CONFIG (cd->config),
"curve-border");
switch (curve_type)
{
case GFREE:
break;
case SMOOTH:
/* cycle through the curves */
num_pts = 0;
for (i = 0; i < 17; i++)
if (cd->points[outline][i][0] != -1)
points[num_pts++] = i;
xmid = xmax / 2;
/* Initialize boundary curve points */
if (num_pts != 0)
{
if (fix255)
{
for (i = 0; i < (cd->points[outline][points[0]][0] * 255); i++)
cd->curve[outline][i] =
(cd->points[outline][points[0]][1] * 255);
for (i = (cd->points[outline][points[num_pts - 1]][0] * 255); i < 256; i++)
cd->curve[outline][i] =
(cd->points[outline][points[num_pts - 1]][1] * 255);
}
else
{
yfirst = cd->points[outline][points[0]][1] * ymax;
ylast = cd->points[outline][points[num_pts - 1]][1] * ymax;
for (i = 0; i < xmid; i++)
{
cd->curve_ptr[outline][i] = yfirst;
}
for (i = xmid; i <= xmax; i++)
{
cd->curve_ptr[outline][i] = ylast;
}
}
}
for (i = 0; i < num_pts - 1; i++)
{
p1 = (i == 0) ? points[i] : points[(i - 1)];
p2 = points[i];
p3 = points[(i + 1)];
p4 = (i == (num_pts - 2)) ? points[(num_pts - 1)] : points[(i + 2)];
bender_plot_curve (cd, p1, p2, p3, p4, xmax, ymax, fix255);
}
break;
}
}
static void
bender_global_notify (GtkWidget *widget,
gpointer data)
{
BenderDialog *cd;
cd = (BenderDialog *) data;
if (! cd)
return;
if (cd->preview)
bender_update (cd, UP_PREVIEW | UP_DRAW);
}
static void
bender_type_callback (GtkWidget *widget,
gpointer data)
{
BenderDialog *cd;
gint curve_type;
gint outline;
cd = g_object_get_data (G_OBJECT (widget), "cd");
if (! cd)
return;
curve_type = gimp_procedure_config_get_choice_id (GIMP_PROCEDURE_CONFIG (cd->config),
"curve-type");
outline = gimp_procedure_config_get_choice_id (GIMP_PROCEDURE_CONFIG (cd->config),
"curve-border");
if (curve_type == SMOOTH)
{
gint i;
/* pick representative points from the curve and make them control points */
for (i = 0; i <= 8; i++)
{
gint index = CLAMP ((i * 32), 0, 255);
cd->points[outline][i * 2][0] = (gdouble)index / 255.0;
cd->points[outline][i * 2][1] = (gdouble)cd->curve[outline][index] / 255.0;
}
bender_calculate_curve (cd, 255, 255, TRUE);
bender_update (cd, UP_GRAPH | UP_DRAW);
if (cd->preview)
bender_update (cd, UP_PREVIEW | UP_DRAW);
}
else
{
bender_update (cd, UP_GRAPH | UP_DRAW);
}
}
static void
bender_reset_callback (GtkWidget *widget,
gpointer client_data)
{
BenderDialog *cd = (BenderDialog *) client_data;
gint i;
gint outline;
outline = gimp_procedure_config_get_choice_id (GIMP_PROCEDURE_CONFIG (cd->config),
"curve-border");
/* Initialize the values */
for (i = 0; i < 256; i++)
cd->curve[outline][i] = MIDDLE;
cd->grab_point = -1;
for (i = 0; i < 17; i++)
{
cd->points[outline][i][0] = -1;
cd->points[outline][i][1] = -1;
}
cd->points[outline][0][0] = 0.0; /* x */
cd->points[outline][0][1] = 0.5; /* y */
cd->points[outline][16][0] = 1.0; /* x */
cd->points[outline][16][1] = 0.5; /* y */
bender_update (cd, UP_GRAPH | UP_DRAW);
if (cd->preview)
bender_update (cd, UP_PREVIEW | UP_DRAW);
}
static void
bender_copy_callback (GtkWidget *widget,
gpointer client_data)
{
BenderDialog *cd = (BenderDialog *) client_data;
gint i;
gint other;
gint outline;
outline = gimp_procedure_config_get_choice_id (GIMP_PROCEDURE_CONFIG (cd->config),
"curve-border");
other = (outline) ? 0 : 1;
for (i = 0; i < 17; i++)
{
cd->points[other][i][0] = cd->points[outline][i][0];
cd->points[other][i][1] = cd->points[outline][i][1];
}
for (i= 0; i < 256; i++)
{
cd->curve[other][i] = cd->curve[outline][i];
}
bender_update (cd, UP_GRAPH | UP_DRAW);
if (cd->preview)
bender_update (cd, UP_PREVIEW | UP_DRAW);
}
static void
bender_copy_inv_callback (GtkWidget *widget,
gpointer client_data)
{
BenderDialog *cd = (BenderDialog *) client_data;
gint i;
gint other;
gint outline;
outline = gimp_procedure_config_get_choice_id (GIMP_PROCEDURE_CONFIG (cd->config),
"curve-border");
other = (outline) ? 0 : 1;
for (i = 0; i < 17; i++)
{
cd->points[other][i][0] = cd->points[outline][i][0]; /* x */
cd->points[other][i][1] = 1.0 - cd->points[outline][i][1]; /* y */
}
for (i= 0; i < 256; i++)
{
cd->curve[other][i] = 255 - cd->curve[outline][i];
}
bender_update (cd, UP_GRAPH | UP_DRAW);
if (cd->preview)
bender_update (cd, UP_PREVIEW | UP_DRAW);
}
static void
bender_swap_callback (GtkWidget *widget,
gpointer client_data)
{
#define SWAP_VALUE(a, b, h) { h=a; a=b; b=h; }
BenderDialog *cd = (BenderDialog *) client_data;
gint i;
gint other;
gint outline;
gdouble hd;
guchar hu;
outline = gimp_procedure_config_get_choice_id (GIMP_PROCEDURE_CONFIG (cd->config),
"curve-border");
other = (outline) ? 0 : 1;
for (i = 0; i < 17; i++)
{
SWAP_VALUE(cd->points[other][i][0], cd->points[outline][i][0], hd); /* x */
SWAP_VALUE(cd->points[other][i][1], cd->points[outline][i][1], hd); /* y */
}
for (i= 0; i < 256; i++)
{
SWAP_VALUE(cd->curve[other][i], cd->curve[outline][i], hu);
}
bender_update (cd, UP_GRAPH | UP_DRAW);
if (cd->preview)
bender_update (cd, UP_PREVIEW | UP_DRAW);
}
static void
bender_preview_update (GtkWidget *widget,
gpointer data)
{
BenderDialog *cd = (BenderDialog*) data;
if (! cd)
return;
cd->preview = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
if(cd->preview)
bender_update (cd, UP_PREVIEW | UP_DRAW);
}
static void
bender_preview_update_once (GtkWidget *widget,
gpointer data)
{
BenderDialog *cd = (BenderDialog*) data;
if (! cd)
return;
bender_update (cd, UP_PREVIEW | UP_DRAW);
}
static void
p_points_save_to_file_response (GtkWidget *dialog,
gint response_id,
BenderDialog *cd)
{
if (response_id == GTK_RESPONSE_OK)
{
gchar *filename;
filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
p_save_pointfile (cd, filename);
g_free (filename);
}
gtk_widget_destroy (dialog);
}
static void
p_points_load_from_file_response (GtkWidget *dialog,
gint response_id,
BenderDialog *cd)
{
if (response_id == GTK_RESPONSE_OK)
{
gchar *filename;
filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
p_load_pointfile (cd, filename);
bender_update (cd, UP_ALL);
g_free (filename);
}
gtk_widget_destroy (dialog);
}
static void
bender_load_callback (GtkWidget *w,
BenderDialog *cd)
{
if (! cd->filechooser)
{
cd->filechooser =
gtk_file_chooser_dialog_new (_("Load Curve Points from File"),
GTK_WINDOW (gtk_widget_get_toplevel (w)),
GTK_FILE_CHOOSER_ACTION_OPEN,
_("_Cancel"), GTK_RESPONSE_CANCEL,
_("_Open"), GTK_RESPONSE_OK,
NULL);
gimp_dialog_set_alternative_button_order (GTK_DIALOG (cd->filechooser),
GTK_RESPONSE_OK,
GTK_RESPONSE_CANCEL,
-1);
gtk_dialog_set_default_response (GTK_DIALOG (cd->filechooser),
GTK_RESPONSE_OK);
g_signal_connect (cd->filechooser, "response",
G_CALLBACK (p_points_load_from_file_response),
cd);
g_signal_connect (cd->filechooser, "destroy",
G_CALLBACK (gtk_widget_destroyed),
&cd->filechooser);
}
gtk_window_present (GTK_WINDOW (cd->filechooser));
}
static void
bender_save_callback (GtkWidget *w,
BenderDialog *cd)
{
if (! cd->filechooser)
{
cd->filechooser =
gtk_file_chooser_dialog_new (_("Save Curve Points to File"),
GTK_WINDOW (gtk_widget_get_toplevel (w)),
GTK_FILE_CHOOSER_ACTION_SAVE,
_("_Cancel"), GTK_RESPONSE_CANCEL,
_("_Save"), GTK_RESPONSE_OK,
NULL);
g_signal_connect (cd->filechooser, "response",
G_CALLBACK (p_points_save_to_file_response),
cd);
g_signal_connect (cd->filechooser, "destroy",
G_CALLBACK (gtk_widget_destroyed),
&cd->filechooser);
gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (cd->filechooser),
"curve_bend.points");
}
gtk_window_present (GTK_WINDOW (cd->filechooser));
}
static gboolean
bender_graph_events (GtkWidget *widget,
GdkEvent *event,
BenderDialog *cd)
{
static GdkCursorType cursor_type = GDK_TOP_LEFT_ARROW;
GdkCursorType new_type;
GdkEventMotion *mevent;
gint i;
gint tx, ty;
gint x, y;
gint closest_point;
gint distance;
gint x1, x2, y1, y2;
gint curve_type;
gint outline;
curve_type = gimp_procedure_config_get_choice_id (GIMP_PROCEDURE_CONFIG (cd->config),
"curve-type");
outline = gimp_procedure_config_get_choice_id (GIMP_PROCEDURE_CONFIG (cd->config),
"curve-border");
new_type = GDK_X_CURSOR;
closest_point = 0;
if (! gdk_event_get_device (event))
return FALSE;
/* get the pointer position */
gdk_window_get_device_position (gtk_widget_get_window (cd->graph),
gdk_event_get_device (event),
&tx, &ty, NULL);
x = CLAMP ((tx - RADIUS), 0, 255);
y = CLAMP ((ty - RADIUS), 0, 255);
distance = G_MAXINT;
for (i = 0; i < 17; i++)
{
if (cd->points[outline][i][0] != -1)
if (abs ((int) (x - (cd->points[outline][i][0] * 255.0))) < distance)
{
distance = abs ((int) (x - (cd->points[outline][i][0] * 255.0)));
closest_point = i;
}
}
if (distance > MIN_DISTANCE)
closest_point = (x + 8) / 16;
switch (event->type)
{
case GDK_BUTTON_PRESS:
new_type = GDK_TCROSS;
switch (curve_type)
{
case SMOOTH:
/* determine the leftmost and rightmost points */
cd->leftmost = -1;
for (i = closest_point - 1; i >= 0; i--)
if (cd->points[outline][i][0] != -1)
{
cd->leftmost = (cd->points[outline][i][0] * 255.0);
break;
}
cd->rightmost = 256;
for (i = closest_point + 1; i < 17; i++)
if (cd->points[outline][i][0] != -1)
{
cd->rightmost = (cd->points[outline][i][0] * 255.0);
break;
}
cd->grab_point = closest_point;
cd->points[outline][cd->grab_point][0] = (gdouble)x / 255.0;
cd->points[outline][cd->grab_point][1] = (gdouble)(255 - y) / 255.0;
bender_calculate_curve (cd, 255, 255, TRUE);
break;
case GFREE:
cd->curve[outline][x] = 255 - y;
cd->grab_point = x;
cd->last = y;
break;
}
bender_update (cd, UP_GRAPH | UP_DRAW);
break;
case GDK_BUTTON_RELEASE:
new_type = GDK_FLEUR;
cd->grab_point = -1;
if (cd->preview)
bender_update (cd, UP_PREVIEW | UP_DRAW);
break;
case GDK_MOTION_NOTIFY:
mevent = (GdkEventMotion *) event;
if (mevent->is_hint)
{
mevent->x = tx;
mevent->y = ty;
}
switch (curve_type)
{
case SMOOTH:
/* If no point is grabbed... */
if (cd->grab_point == -1)
{
if (cd->points[outline][closest_point][0] != -1)
new_type = GDK_FLEUR;
else
new_type = GDK_TCROSS;
}
/* Else, drag the grabbed point */
else
{
new_type = GDK_TCROSS;
cd->points[outline][cd->grab_point][0] = -1;
if (x > cd->leftmost && x < cd->rightmost)
{
closest_point = (x + 8) / 16;
if (cd->points[outline][closest_point][0] == -1)
cd->grab_point = closest_point;
cd->points[outline][cd->grab_point][0] = (gdouble)x / 255.0;
cd->points[outline][cd->grab_point][1] = (gdouble)(255 - y) / 255.0;
}
bender_calculate_curve (cd, 255, 255, TRUE);
bender_update (cd, UP_GRAPH | UP_DRAW);
}
break;
case GFREE:
if (cd->grab_point != -1)
{
if (cd->grab_point > x)
{
x1 = x;
x2 = cd->grab_point;
y1 = y;
y2 = cd->last;
}
else
{
x1 = cd->grab_point;
x2 = x;
y1 = cd->last;
y2 = y;
}
if (x2 != x1)
for (i = x1; i <= x2; i++)
cd->curve[outline][i] = 255 - (y1 + ((y2 - y1) * (i - x1)) / (x2 - x1));
else
cd->curve[outline][x] = 255 - y;
cd->grab_point = x;
cd->last = y;
bender_update (cd, UP_GRAPH | UP_DRAW);
}
if (mevent->state & GDK_BUTTON1_MASK)
new_type = GDK_TCROSS;
else
new_type = GDK_PENCIL;
break;
}
if (new_type != cursor_type)
{
cursor_type = new_type;
/* change_win_cursor (gtk_widget_get_window (cd->graph), cursor_type); */
}
break;
default:
break;
}
return FALSE;
}
static gboolean
bender_graph_draw (GtkWidget *widget,
cairo_t *cr,
BenderDialog *cd)
{
GdkRGBA black = { 0.0, 0.0, 0.0, 1.0 };
GdkRGBA white = { 1.0, 1.0, 1.0, 1.0 };
GdkRGBA gray = { 0.5, 0.5, 0.5, 1.0 };
gint i;
gint other;
gint curve_type;
gint outline;
curve_type = gimp_procedure_config_get_choice_id (GIMP_PROCEDURE_CONFIG (cd->config),
"curve-type");
outline = gimp_procedure_config_get_choice_id (GIMP_PROCEDURE_CONFIG (cd->config),
"curve-border");
cairo_set_line_width (cr, 1.0);
cairo_translate (cr, 0.5, 0.5);
/* Clear the background */
gdk_cairo_set_source_rgba (cr, &white);
cairo_paint (cr);
/* Draw the grid lines */
for (i = 0; i < 5; i++)
{
cairo_move_to (cr, RADIUS, i * (GRAPH_HEIGHT / 4) + RADIUS);
cairo_line_to (cr, GRAPH_WIDTH + RADIUS, i * (GRAPH_HEIGHT / 4) + RADIUS);
cairo_move_to (cr, i * (GRAPH_WIDTH / 4) + RADIUS, RADIUS);
cairo_line_to (cr, i * (GRAPH_WIDTH / 4) + RADIUS, GRAPH_HEIGHT + RADIUS);
}
gdk_cairo_set_source_rgba (cr, &gray);
cairo_stroke (cr);
/* Draw the other curve */
other = (outline == 0) ? 1 : 0;
cairo_move_to (cr, RADIUS, 255 - cd->curve[other][0] + RADIUS);
for (i = 1; i < 256; i++)
{
cairo_line_to (cr, i + RADIUS, 255 - cd->curve[other][i] + RADIUS);
}
gdk_cairo_set_source_rgba (cr, &gray);
cairo_stroke (cr);
/* Draw the active curve */
cairo_move_to (cr, RADIUS, 255 - cd->curve[outline][0] + RADIUS);
for (i = 1; i < 256; i++)
{
cairo_line_to (cr, i + RADIUS, 255 - cd->curve[outline][i] + RADIUS);
}
/* Draw the points */
if (curve_type == SMOOTH)
{
for (i = 0; i < 17; i++)
{
if (cd->points[outline][i][0] != -1)
{
cairo_new_sub_path (cr);
cairo_arc (cr,
(cd->points[outline][i][0] * 255.0) + RADIUS,
255 - (cd->points[outline][i][1] * 255.0) + RADIUS,
RADIUS,
0, 2 * G_PI);
}
}
}
gdk_cairo_set_source_rgba (cr, &black);
cairo_stroke (cr);
return FALSE;
}
static void
bender_CR_compose (CRMatrix a,
CRMatrix b,
CRMatrix ab)
{
gint i, j;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
ab[i][j] = (a[i][0] * b[0][j] +
a[i][1] * b[1][j] +
a[i][2] * b[2][j] +
a[i][3] * b[3][j]);
}
}
}
static void
p_render_preview (BenderDialog *cd,
GimpLayer *layer)
{
guchar pixel[4];
guchar *buf, *ptr;
gint x, y;
gint ofx, ofy;
gint width, height;
t_GDRW l_gdrw;
t_GDRW *gdrw;
width = gimp_drawable_get_width (GIMP_DRAWABLE (layer));
height = gimp_drawable_get_height (GIMP_DRAWABLE (layer));
ptr = buf = g_new (guchar, PREVIEW_BPP * PREVIEW_SIZE_X * PREVIEW_SIZE_Y);
gdrw = &l_gdrw;
p_init_gdrw (gdrw, GIMP_DRAWABLE (layer), FALSE);
/* offsets to set bend layer to preview center */
ofx = (width / 2) - (PREVIEW_SIZE_X / 2);
ofy = (height / 2) - (PREVIEW_SIZE_Y / 2);
/* render preview */
for (y = 0; y < PREVIEW_SIZE_Y; y++)
{
for (x = 0; x < PREVIEW_SIZE_X; x++)
{
p_get_pixel (gdrw, x + ofx, y + ofy, &pixel[0]);
if (cd->color)
{
ptr[0] = pixel[0];
ptr[1] = pixel[1];
ptr[2] = pixel[2];
}
else
{
ptr[0] = pixel[0];
ptr[1] = pixel[0];
ptr[2] = pixel[0];
}
ptr[3] = pixel[gdrw->index_alpha];
ptr += PREVIEW_BPP;
}
}
gimp_preview_area_draw (GIMP_PREVIEW_AREA (cd->pv_widget),
0, 0, PREVIEW_SIZE_X, PREVIEW_SIZE_Y,
GIMP_RGBA_IMAGE,
buf,
PREVIEW_BPP * PREVIEW_SIZE_X);
g_free (buf);
p_end_gdrw(gdrw);
}
/* ===================================================== */
/* curve_bend worker procedures */
/* ===================================================== */
static void
p_stretch_curves (BenderDialog *cd,
gint32 xmax,
gint32 ymax)
{
gint32 x1, x2;
gdouble ya, yb;
gdouble rest;
int outline;
for (outline = 0; outline < 2; outline++)
{
for(x1 = 0; x1 <= xmax; x1++)
{
x2 = (x1 * 255) / xmax;
if ((xmax <= 255) && (x2 < 255))
{
cd->curve_ptr[outline][x1] =
ROUND ((cd->curve[outline][x2] * ymax) / 255);
}
else
{
/* interpolate */
rest = (((gdouble)x1 * 255.0) / (gdouble)xmax) - x2;
ya = cd->curve[outline][x2]; /* y of this point */
yb = cd->curve[outline][x2 +1]; /* y of next point */
cd->curve_ptr[outline][x1] =
ROUND (((ya + ((yb -ya) * rest)) * ymax) / 255);
}
}
}
}
static void
bender_init_min_max (BenderDialog *cd,
gint32 xmax)
{
int i, j;
for (i = 0; i < 2; i++)
{
cd->min2[i] = 65000;
cd->max2[i] = 0;
for (j = 0; j <= xmax; j++)
{
if(cd->curve_ptr[i][j] > cd->max2[i])
{
cd->max2[i] = cd->curve_ptr[i][j];
}
if(cd->curve_ptr[i][j] < cd->min2[i])
{
cd->min2[i] = cd->curve_ptr[i][j];
}
}
}
/* for UPPER outline : y-zero line is assumed at the min leftmost or
* rightmost point
*/
cd->zero2[OUTLINE_UPPER] = MIN (cd->curve_ptr[OUTLINE_UPPER][0],
cd->curve_ptr[OUTLINE_UPPER][xmax]);
/* for LOWER outline : y-zero line is assumed at the min leftmost or
* rightmost point
*/
cd->zero2[OUTLINE_LOWER] = MAX (cd->curve_ptr[OUTLINE_LOWER][0],
cd->curve_ptr[OUTLINE_LOWER][xmax]);
}
static gint32
p_curve_get_dy (BenderDialog *cd,
gint32 x,
gint32 drawable_width,
gint32 total_steps,
gdouble current_step)
{
/* get y values of both upper and lower curve,
* and return the iterated value in between
*/
gdouble y1, y2;
gdouble delta;
y1 = cd->zero2[OUTLINE_UPPER] - cd->curve_ptr[OUTLINE_UPPER][x];
y2 = cd->zero2[OUTLINE_LOWER] - cd->curve_ptr[OUTLINE_LOWER][x];
delta = ((double)(y2 - y1) / (double)(total_steps - 1)) * current_step;
return SIGNED_ROUND (y1 + delta);
}
static gint32
p_upper_curve_extend (BenderDialog *cd,
gint32 drawable_width,
gint32 drawable_height)
{
gint32 y1, y2;
y1 = cd->max2[OUTLINE_UPPER] - cd->zero2[OUTLINE_UPPER];
y2 = (cd->max2[OUTLINE_LOWER] - cd->zero2[OUTLINE_LOWER]) - drawable_height;
return MAX (y1, y2);
}
static gint32
p_lower_curve_extend (BenderDialog *cd,
gint32 drawable_width,
gint32 drawable_height)
{
gint32 y1, y2;
y1 = cd->zero2[OUTLINE_LOWER] - cd->min2[OUTLINE_LOWER];
y2 = (cd->zero2[OUTLINE_UPPER] - cd->min2[OUTLINE_UPPER]) - drawable_height;
return MAX (y1, y2);
}
static void
p_end_gdrw (t_GDRW *gdrw)
{
g_object_unref (gdrw->buffer);
}
static void
p_init_gdrw (t_GDRW *gdrw,
GimpDrawable *drawable,
int shadow)
{
gint w, h;
gdrw->drawable = drawable;
if (shadow)
gdrw->buffer = gimp_drawable_get_shadow_buffer (drawable);
else
gdrw->buffer = gimp_drawable_get_buffer (drawable);
gdrw->width = gimp_drawable_get_width (gdrw->drawable);
gdrw->height = gimp_drawable_get_height (gdrw->drawable);
gdrw->tile_width = gimp_tile_width ();
gdrw->tile_height = gimp_tile_height ();
if (! gimp_drawable_mask_intersect (gdrw->drawable,
&gdrw->x1, &gdrw->y1, &w, &h))
{
w = 0;
h = 0;
}
gdrw->x2 = gdrw->x1 + w;
gdrw->y2 = gdrw->y1 + h;
if (gimp_drawable_has_alpha (drawable))
gdrw->format = babl_format ("R'G'B'A u8");
else
gdrw->format = babl_format ("R'G'B' u8");
gdrw->bpp = babl_format_get_bytes_per_pixel (gdrw->format);
if (gimp_drawable_has_alpha (drawable))
{
/* index of the alpha channelbyte {1|3} */
gdrw->index_alpha = gdrw->bpp - 1;
}
else
{
gdrw->index_alpha = 0; /* there is no alpha channel */
}
}
/* get pixel value
* return light transparent black gray pixel if out of bounds
* (should occur in the previews only)
*/
static void
p_get_pixel (t_GDRW *gdrw,
gint32 x,
gint32 y,
guchar *pixel)
{
pixel[1] = 255;
pixel[3] = 255; /* simulate full visible alpha channel */
gegl_buffer_sample (gdrw->buffer, x, y, NULL, pixel, gdrw->format,
GEGL_SAMPLER_NEAREST, GEGL_ABYSS_NONE);
}
static void
p_put_pixel (t_GDRW *gdrw,
gint32 x,
gint32 y,
guchar *pixel)
{
gegl_buffer_set (gdrw->buffer, GEGL_RECTANGLE (x, y, 1, 1), 0,
gdrw->format, pixel, GEGL_AUTO_ROWSTRIDE);
}
static void
p_put_mix_pixel (t_GDRW *gdrw,
gint32 x,
gint32 y,
guchar *color,
gint32 nb_curvy,
gint32 nb2_curvy,
gint32 curvy)
{
guchar pixel[4];
guchar mixmask;
gint idx;
gint diff;
mixmask = 255 - 96;
diff = abs(nb_curvy - curvy);
if (diff == 0)
{
mixmask = 255 - 48;
diff = abs(nb2_curvy - curvy);
if (diff == 0)
{
/* last 2 neighbours were not shifted against current pixel, do not mix */
p_put_pixel(gdrw, x, y, color);
return;
}
}
/* get left neighbour pixel */
p_get_pixel(gdrw, x-1, y, &pixel[0]);
if (pixel[gdrw->index_alpha] < 10)
{
/* neighbour is (nearly or full) transparent, do not mix */
p_put_pixel(gdrw, x, y, color);
return;
}
for (idx = 0; idx < gdrw->index_alpha ; idx++)
{
/* mix in left neighbour color */
pixel[idx] = MIX_CHANNEL(color[idx], pixel[idx], mixmask);
}
pixel[gdrw->index_alpha] = color[gdrw->index_alpha];
p_put_pixel(gdrw, x, y, &pixel[0]);
}
/* ============================================================================
* p_create_pv_image
* ============================================================================
*/
static GimpImage *
p_create_pv_image (GimpDrawable *src_drawable,
GimpLayer **layer)
{
GimpImage *new_image;
guint new_width;
guint new_height;
GimpImageType type;
guint x, y;
double scale;
guchar pixel[4];
t_GDRW src_gdrw;
t_GDRW dst_gdrw;
gint src_width;
gint src_height;
src_width = gimp_drawable_get_width (src_drawable);
src_height = gimp_drawable_get_height (src_drawable);
new_image = gimp_image_new (PREVIEW_SIZE_X, PREVIEW_SIZE_Y,
gimp_image_get_base_type (gimp_item_get_image (GIMP_ITEM (src_drawable))));
gimp_image_undo_disable (new_image);
type = gimp_drawable_type (src_drawable);
if (src_height > src_width)
{
new_height = PV_IMG_HEIGHT;
new_width = (src_width * new_height) / src_height;
scale = (float) src_height / PV_IMG_HEIGHT;
}
else
{
new_width = PV_IMG_WIDTH;
new_height = (src_height * new_width) / src_width;
scale = (float) src_width / PV_IMG_WIDTH;
}
*layer = gimp_layer_new (new_image, "preview_original",
new_width, new_height,
type,
100.0, /* opacity */
0); /* mode NORMAL */
if (! gimp_drawable_has_alpha (GIMP_DRAWABLE (*layer)))
{
/* always add alpha channel */
gimp_layer_add_alpha (*layer);
}
gimp_image_insert_layer (new_image, *layer, NULL, 0);
p_init_gdrw (&src_gdrw, src_drawable, FALSE);
p_init_gdrw (&dst_gdrw, GIMP_DRAWABLE (*layer), FALSE);
for (y = 0; y < new_height; y++)
{
for (x = 0; x < new_width; x++)
{
p_get_pixel(&src_gdrw, x * scale, y * scale, &pixel[0]);
p_put_pixel(&dst_gdrw, x, y, &pixel[0]);
}
}
p_end_gdrw (&src_gdrw);
p_end_gdrw (&dst_gdrw);
return new_image;
}
/* ============================================================================
* p_add_layer
* ============================================================================
*/
static GimpLayer *
p_add_layer (gint width,
gint height,
GimpDrawable *src_drawable)
{
GimpImageType type;
GimpLayer *new_layer;
char *name;
char *name2;
gdouble opacity;
GimpLayerMode mode;
gint visible;
GimpImage *image;
gint stack_position;
image = gimp_item_get_image (GIMP_ITEM (src_drawable));
stack_position = 0; /* TODO: should be same as src_layer */
/* copy type, name, opacity and mode from src_drawable */
type = gimp_drawable_type (src_drawable);
visible = gimp_item_get_visible (GIMP_ITEM (src_drawable));
name2 = gimp_item_get_name (GIMP_ITEM (src_drawable));
name = g_strdup_printf ("%s_b", name2);
g_free (name2);
mode = gimp_layer_get_mode (GIMP_LAYER (src_drawable));
opacity = gimp_layer_get_opacity (GIMP_LAYER (src_drawable)); /* full opacity */
new_layer = gimp_layer_new (image, name,
width, height,
type,
opacity,
mode);
g_free (name);
if (! gimp_drawable_has_alpha (GIMP_DRAWABLE (new_layer)))
{
/* always add alpha channel */
gimp_layer_add_alpha (new_layer);
}
/* add the copied layer to the temp. working image */
gimp_image_insert_layer (image, new_layer, NULL, stack_position);
/* copy visibility state */
gimp_item_set_visible (GIMP_ITEM (new_layer), visible);
return new_layer;
}
/* ============================================================================
* p_bender_calculate_iter_curve
* ============================================================================
*/
static void
p_bender_calculate_iter_curve (BenderDialog *cd,
GimpProcedureConfig *config,
gint32 xmax,
gint32 ymax)
{
gint outline;
gint curve_type;
curve_type = gimp_procedure_config_get_choice_id (GIMP_PROCEDURE_CONFIG (cd->config),
"curve-type");
outline = gimp_procedure_config_get_choice_id (GIMP_PROCEDURE_CONFIG (cd->config),
"curve-border");
if (gb_debug)
g_printf ("p_bender_calculate_iter_curve NORMAL1\n");
if (curve_type == SMOOTH)
{
g_object_set (cd->config,
"curve-border", "upper",
NULL);
bender_calculate_curve (cd, xmax, ymax, FALSE);
g_object_set (cd->config,
"curve-border", "lower",
NULL);
bender_calculate_curve (cd, xmax, ymax, FALSE);
}
else
{
p_stretch_curves (cd, xmax, ymax);
}
g_object_set (config,
"curve-border", (outline == OUTLINE_UPPER) ?
"upper" : "lower",
NULL);
}
/* ============================================================================
* p_vertical_bend
* ============================================================================
*/
static void
p_vertical_bend (BenderDialog *cd,
t_GDRW *src_gdrw,
t_GDRW *dst_gdrw)
{
gint32 row, col;
gint32 first_row, first_col, last_row, last_col;
gint32 x, y;
gint32 x2, y2;
gint32 curvy, nb_curvy, nb2_curvy;
gint32 desty, othery;
gint32 miny, maxy;
gint32 sign, dy, diff;
gint32 topshift;
float progress_step;
float progress_max;
float progress;
t_Last *last_arr;
t_Last *first_arr;
guchar color[4];
guchar mixcolor[4];
gint alias_dir;
guchar mixmask;
gboolean antialias;
gboolean smoothing;
g_object_get (cd->config,
"antialias", &antialias,
"smoothing", &smoothing,
NULL);
topshift = p_upper_curve_extend (cd,
src_gdrw->width,
src_gdrw->height);
diff = curvy = nb_curvy = nb2_curvy= miny = maxy = 0;
/* allocate array of last values (one element foreach x coordinate) */
last_arr = g_new (t_Last, src_gdrw->x2);
first_arr = g_new (t_Last, src_gdrw->x2);
/* ------------------------------------------------
* foreach pixel in the SAMPLE_drawable:
* ------------------------------------------------
* the inner loops (x/y) are designed to process
* all pixels of one tile in the sample drawable, the outer loops (row/col) do step
* to the next tiles. (this was done to reduce tile swapping)
*/
first_row = src_gdrw->y1 / src_gdrw->tile_height;
last_row = (src_gdrw->y2 / src_gdrw->tile_height);
first_col = src_gdrw->x1 / src_gdrw->tile_width;
last_col = (src_gdrw->x2 / src_gdrw->tile_width);
/* init progress */
progress_max = (1 + last_row - first_row) * (1 + last_col - first_col);
progress_step = 1.0 / progress_max;
progress = 0.0;
if (cd->show_progress)
gimp_progress_init ( _("Curve Bend"));
for (row = first_row; row <= last_row; row++)
{
for (col = first_col; col <= last_col; col++)
{
if (col == first_col)
x = src_gdrw->x1;
else
x = col * src_gdrw->tile_width;
if (col == last_col)
x2 = src_gdrw->x2;
else
x2 = (col +1) * src_gdrw->tile_width;
if (cd->show_progress)
gimp_progress_update (progress += progress_step);
for( ; x < x2; x++)
{
if (row == first_row)
y = src_gdrw->y1;
else
y = row * src_gdrw->tile_height;
if (row == last_row)
y2 = src_gdrw->y2;
else
y2 = (row +1) * src_gdrw->tile_height ;
for( ; y < y2; y++)
{
/* ---------- copy SRC_PIXEL to curve position ------ */
p_get_pixel (src_gdrw, x, y, color);
curvy = p_curve_get_dy (cd, x,
(gint32)src_gdrw->width,
(gint32)src_gdrw->height,
(gdouble)y);
desty = y + topshift + curvy;
/* ----------- SMOOTHING ------------------ */
if (smoothing && (x > 0))
{
nb_curvy = p_curve_get_dy (cd, x -1,
(gint32) src_gdrw->width,
(gint32) src_gdrw->height,
(gdouble) y);
if ((nb_curvy == curvy) && (x > 1))
{
nb2_curvy = p_curve_get_dy (cd, x -2,
(gint32) src_gdrw->width,
(gint32) src_gdrw->height,
(gdouble) y);
}
else
{
nb2_curvy = nb_curvy;
}
p_put_mix_pixel (dst_gdrw, x, desty, color, nb_curvy, nb2_curvy, curvy);
}
else
{
p_put_pixel (dst_gdrw, x, desty, color);
}
/* ----------- render ANTIALIAS ------------------ */
if (antialias)
{
othery = desty;
if (y == src_gdrw->y1) /* Upper outline */
{
first_arr[x].y = curvy;
memcpy (first_arr[x].color, color,
dst_gdrw->bpp);
if (x > 0)
{
memcpy (mixcolor, first_arr[x-1].color,
dst_gdrw->bpp);
diff = abs(first_arr[x - 1].y - curvy) +1;
miny = MIN(first_arr[x - 1].y, curvy) -1;
maxy = MAX(first_arr[x - 1].y, curvy) +1;
othery = (src_gdrw->y2 -1)
+ topshift
+ p_curve_get_dy(cd, x,
(gint32)src_gdrw->width,
(gint32)src_gdrw->height,
(gdouble)(src_gdrw->y2 -1));
}
}
if (y == src_gdrw->y2 - 1) /* Lower outline */
{
if (x > 0)
{
memcpy (mixcolor, last_arr[x-1].color,
dst_gdrw->bpp);
diff = abs (last_arr[x - 1].y - curvy) +1;
maxy = MAX (last_arr[x - 1].y, curvy) +1;
miny = MIN (last_arr[x - 1].y, curvy) -1;
}
othery = (src_gdrw->y1)
+ topshift
+ p_curve_get_dy(cd, x,
(gint32)src_gdrw->width,
(gint32)src_gdrw->height,
(gdouble)(src_gdrw->y1));
}
if (desty < othery)
alias_dir = 1; /* fade to transp. with descending dy */
else if (desty > othery)
alias_dir = -1; /* fade to transp. with ascending dy */
else
alias_dir = 0; /* no antialias at curve crossing point(s) */
if (alias_dir != 0)
{
guchar alpha_lo = 20;
if (gimp_drawable_has_alpha (src_gdrw->drawable))
{
alpha_lo = MIN (20, mixcolor[src_gdrw->index_alpha]);
}
for (dy = 0; dy < diff; dy++)
{
/* iterate for fading alpha channel */
mixmask = 255 * ((gdouble)(dy + 1) / (gdouble) (diff+1));
mixcolor[dst_gdrw->index_alpha] = MIX_CHANNEL(color[dst_gdrw->index_alpha], alpha_lo, mixmask);
if(alias_dir > 0)
{
p_put_pixel (dst_gdrw, x -1, y + topshift + miny + dy, mixcolor);
}
else
{
p_put_pixel (dst_gdrw, x -1, y + topshift + (maxy - dy), mixcolor);
}
}
}
}
/* ------------------ FILL HOLES ------------------ */
if (y == src_gdrw->y1)
{
diff = 0;
sign = 1;
}
else
{
diff = last_arr[x].y - curvy;
if (diff < 0)
{
diff = 0 - diff;
sign = -1;
}
else
{
sign = 1;
}
memcpy (mixcolor, color, dst_gdrw->bpp);
}
for (dy = 1; dy <= diff; dy++)
{
/* y differs more than 1 pixel from last y in the
* destination drawable. So we have to fill the empty
* space between using a mixed color
*/
if (smoothing)
{
/* smoothing is on, so we are using a mixed color */
gulong alpha1 = last_arr[x].color[3];
gulong alpha2 = color[3];
gulong alpha;
mixmask = 255 * ((gdouble)(dy) / (gdouble)(diff+1));
alpha = alpha1 * mixmask + alpha2 * (255 - mixmask);
mixcolor[3] = alpha/255;
if (mixcolor[3])
{
mixcolor[0] = (alpha1 * mixmask * last_arr[x].color[0]
+ alpha2 * (255 - mixmask) * color[0])/alpha;
mixcolor[1] = (alpha1 * mixmask * last_arr[x].color[1]
+ alpha2 * (255 - mixmask) * color[1])/alpha;
mixcolor[2] = (alpha1 * mixmask * last_arr[x].color[2]
+ alpha2 * (255 - mixmask) * color[2])/alpha;
/*mixcolor[2] = MIX_CHANNEL(last_arr[x].color[2], color[2], mixmask);*/
}
}
else
{
/* smoothing is off, so we are using this color or
the last color */
if (dy < diff / 2)
{
memcpy (mixcolor, color,
dst_gdrw->bpp);
}
else
{
memcpy (mixcolor, last_arr[x].color,
dst_gdrw->bpp);
}
}
if (smoothing)
{
p_put_mix_pixel (dst_gdrw, x,
desty + (dy * sign),
mixcolor,
nb_curvy, nb2_curvy, curvy );
}
else
{
p_put_pixel (dst_gdrw, x,
desty + (dy * sign), mixcolor);
}
}
/* store y and color */
last_arr[x].y = curvy;
memcpy (last_arr[x].color, color, dst_gdrw->bpp);
}
}
}
}
gimp_progress_update (1.0);
g_free (last_arr);
g_free (first_arr);
}
/* ============================================================================
* p_main_bend
* ============================================================================
*/
static GimpLayer *
p_main_bend (BenderDialog *cd,
GimpDrawable *original_drawable,
GimpProcedureConfig *config,
gint work_on_copy)
{
t_GDRW src_gdrw;
t_GDRW dst_gdrw;
GimpDrawable *src_drawable;
GimpDrawable *dst_drawable;
gint src_width;
gint src_height;
gint32 dst_height;
GimpImage *image;
GimpLayer *tmp_layer;
gint32 interpolation;
gint offset_x, offset_y;
gint center_x, center_y;
gint32 xmax, ymax;
gdouble rotation;
g_object_get (config,
"smoothing", &interpolation,
"rotation", &rotation,
NULL);
image = gimp_item_get_image (GIMP_ITEM (original_drawable));
gimp_drawable_get_offsets (original_drawable, &offset_x, &offset_y);
center_x = offset_x + (gimp_drawable_get_width (original_drawable) / 2 );
center_y = offset_y + (gimp_drawable_get_height (original_drawable) / 2 );
/* always copy original_drawable to a tmp src_layer */
tmp_layer = gimp_layer_copy (GIMP_LAYER (original_drawable));
/* set layer invisible and dummyname and
* add at top of the image while working
* (for the case of undo GIMP must know,
* that the layer was part of the image)
*/
gimp_image_insert_layer (image, tmp_layer, NULL, 0);
gimp_item_set_visible (GIMP_ITEM (tmp_layer), FALSE);
gimp_item_set_name (GIMP_ITEM (tmp_layer), "curve_bend_dummylayer");
if(gb_debug) g_printf("p_main_bend tmp_layer_id %d\n",
(int) gimp_item_get_id (GIMP_ITEM (tmp_layer)));
if (rotation != 0.0)
{
if(gb_debug) g_printf("p_main_bend rotate: %f\n", (float) rotation);
p_gimp_rotate (image, GIMP_DRAWABLE (tmp_layer),
interpolation, rotation);
}
src_drawable = GIMP_DRAWABLE (tmp_layer);
src_width = gimp_drawable_get_width (GIMP_DRAWABLE (tmp_layer));
src_height = gimp_drawable_get_height (GIMP_DRAWABLE (tmp_layer));
xmax = ymax = src_width -1;
cd->curve_ptr[OUTLINE_UPPER] = g_new (gint32, 1+xmax);
cd->curve_ptr[OUTLINE_LOWER] = g_new (gint32, 1+xmax);
p_bender_calculate_iter_curve (cd, config, xmax, ymax);
bender_init_min_max (cd, xmax);
dst_height = src_height
+ p_upper_curve_extend(cd, src_width, src_height)
+ p_lower_curve_extend(cd, src_width, src_height);
if(gb_debug) g_printf("p_main_bend: dst_height:%d\n", dst_height);
if (work_on_copy)
{
dst_drawable = GIMP_DRAWABLE (p_add_layer (src_width, dst_height,
src_drawable));
if (gb_debug) g_printf("p_main_bend: DONE add layer\n");
}
else
{
/* work on the original */
gimp_layer_resize (GIMP_LAYER (original_drawable),
src_width,
dst_height,
offset_x, offset_y);
if (gb_debug) g_printf("p_main_bend: DONE layer resize\n");
if (! gimp_drawable_has_alpha (original_drawable))
{
/* always add alpha channel */
gimp_layer_add_alpha (GIMP_LAYER (original_drawable));
}
dst_drawable = original_drawable;
}
gimp_drawable_fill (dst_drawable, GIMP_FILL_TRANSPARENT);
p_init_gdrw (&src_gdrw, src_drawable, FALSE);
p_init_gdrw (&dst_gdrw, dst_drawable, FALSE);
p_vertical_bend (cd, &src_gdrw, &dst_gdrw);
if(gb_debug) g_printf("p_main_bend: DONE vertical bend\n");
p_end_gdrw (&src_gdrw);
p_end_gdrw (&dst_gdrw);
if (rotation != 0.0)
{
p_gimp_rotate (image, dst_drawable,
interpolation, (gdouble)(360.0 - rotation));
/* TODO: here we should crop dst_drawable to cut off full transparent borderpixels */
}
/* set offsets of the resulting new layer
*(center == center of original_drawable)
*/
offset_x = center_x - (gimp_drawable_get_width (dst_drawable) / 2 );
offset_y = center_y - (gimp_drawable_get_height (dst_drawable) / 2 );
gimp_layer_set_offsets (GIMP_LAYER (dst_drawable), offset_x, offset_y);
/* delete the temp layer */
gimp_image_remove_layer (image, tmp_layer);
g_free (cd->curve_ptr[OUTLINE_UPPER]);
g_free (cd->curve_ptr[OUTLINE_LOWER]);
if (gb_debug) g_printf("p_main_bend: DONE bend main\n");
return GIMP_LAYER (dst_drawable);
}
|