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
|
/* gap_morph_exec.c
* 2004.02.12 hof (Wolfgang Hofer)
* layer morphing worker procedures
*
* Note:
* using multiple workpoint sets is an unfinshed feature and does not work yet.
* (this feature was intended for morphing between 2 videos, where each handled video frame
* can have its own workpoint set. but his is no practical solution since creating of the
* workpoint files is too much manual work)
*
*/
/* The GIMP -- an 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 2 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/* revision history:
* gimp 2.0.2b; 2004/07/28 hof: added p_mix_layers
* gimp 2.0.2b; 2004/07/17 hof: p_pixel_warp_core: changed workpoint selection (both FAST and Quality strategy)
* gimp 2.0.2a; 2004/04/07 hof: created
*/
#include "config.h"
/* SYTEM (UNIX) includes */
#include "math.h"
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <glib/gstdio.h>
/* GIMP includes */
#include "gtk/gtk.h"
#include "libgimp/gimp.h"
/* GAP includes */
#include "gap_libgapbase.h"
#include "gap-intl.h"
#include "gap_image.h"
#include "gap_morph_main.h"
#include "gap_morph_exec.h"
#include "gap_layer_copy.h"
/* GAP_MORPH_USE_SIMPLE_PIXEL_WARP is used for development test only
* (gives bad quality results, you should NOT enable this option)
*/
#undef GAP_MORPH_USE_SIMPLE_PIXEL_WARP
//#define GAP_MORPH_USE_SIMPLE_PIXEL_WARP
#define GAP_MORPH_NEAR_SQR_RADIUS 64
#define GAP_MORPH_NEAR_RADIUS 4
#define GAP_MORPH_FAR_RADIUS 40
#define GAP_MORPH_TOL_FACT_NEAR 3.0
#define GAP_MORPH_TOL_FACT_FAR 1.5
#define GAP_MORPH_8_SEKTORS 8
#define GAP_MORPH_TOL_FAKTOR 2.25
#define GAP_MORPH_MIN_TOL_FAKTOR 4.5
#define GAP_MORPH_XY_REL_FAKTOR 5
#define MIX_VALUE(factor, a, b) ((a * (1.0 - factor)) + (b * factor))
typedef struct GapMorphExeLayerstack
{
gint32 *src_layers;
gint src_nlayers;
gint32 *dst_layers;
gint dst_nlayers;
gint src1_idx;
gint src2_idx;
gint dst1_idx;
gint dst2_idx;
gint32 available_wp_sets;
GapMorphWarpCoreAPI **tab_wp_sets;
} GapMorphExeLayerstack;
extern int gap_debug;
static inline gdouble p_get_tolerance(gdouble dist);
static void p_clip_gimp_pixel_fetcher_get_pixel(GimpPixelFetcher *pft
,GimpDrawable *drawable
,gint xi
,gint yi
,guchar *pixel
);
static void p_bilinear_get_pixel(GimpPixelFetcher *pft
, GimpDrawable *drawable
, gdouble needx
, gdouble needy
, guchar *dest
, gint dst_bpp
);
static gdouble p_linear_advance(gdouble total_steps
,gdouble current_step
,gdouble val_from
,gdouble val_to
);
static inline gint p_calc_sector(GapMorphWorkPoint *wp, gdouble dx, gdouble dy);
static inline gint p_calc_angle_core(GapMorphWorkPoint *wp, gdouble dx, gdouble dy, gint num_sectors);
static gint p_calc_angle(GapMorphWorkPoint *wp, gint32 in_x, gint32 in_y);
static void p_pixel_warp_core(GapMorphWarpCoreAPI *wcap
, gint32 in_x
, gint32 in_y
, gdouble *out_pick_x
, gdouble *out_pick_y
);
static void p_pixel_warp_pick(GapMorphWarpCoreAPI *wcap
, gint32 in_x
, gint32 in_y
, gint set_idx
, gdouble *out_pick_x
, gdouble *out_pick_y
);
static void p_pixel_warp_multipick(GapMorphWarpCoreAPI *wcap_1
, GapMorphWarpCoreAPI *wcap_2
, gdouble wp_mix_factor
, gint32 in_x
, gint32 in_y
, gdouble *out_pick_x
, gdouble *out_pick_y
);
GapMorphWorkPoint * p_calculate_work_point_movement(gdouble total_steps
,gdouble current_step
,GapMorphWorkPoint *master_list
,gint32 src_layer_id
,gint32 dst_layer_id
,gint32 curr_width
,gint32 curr_height
,gboolean forward_move
);
static gint32 p_get_tween_steps_and_layerstacks(GapMorphGlobalParams *mgpp, GapMorphExeLayerstack *mlayers);
#define GAP_MORPH_WORKPOINT_FILE_HEADER "GAP-MORPH workpoint file"
typedef struct GapMorphExePickCache
{
gboolean valid;
gdouble xx;
gdouble yy;
gdouble pick_x;
gdouble pick_y;
} GapMorphExePickCache;
#define GAP_MORPH_PCK_CACHE_SIZE 32
static GapMorphExePickCache gloabl_pick_cache[GAP_MORPH_PCK_CACHE_SIZE][2];
static gint gloabl_pick_cache_idx[2];
/* ---------------------------------
* p_get_tolerance
* ---------------------------------
*/
static inline gdouble
p_get_tolerance(gdouble dist)
{
gdouble factor;
if(dist <= GAP_MORPH_NEAR_RADIUS)
{
return(GAP_MORPH_TOL_FACT_NEAR);
}
if(dist >= GAP_MORPH_FAR_RADIUS)
{
return(GAP_MORPH_TOL_FACT_FAR);
}
factor = (dist - GAP_MORPH_NEAR_RADIUS) / GAP_MORPH_FAR_RADIUS;
return( (GAP_MORPH_TOL_FACT_NEAR * (1 -factor))
+ (GAP_MORPH_TOL_FACT_FAR * factor)
);
} /* end p_get_tolerance */
static void
p_error_message_with_filename(GimpRunMode run_mode, const char *msg_fmt, const char *filename)
{
char *l_msg;
l_msg = g_strdup_printf(msg_fmt, filename);
printf("%s RUN_MODE:%d\n", l_msg, (int)run_mode);
if(run_mode != GIMP_RUN_NONINTERACTIVE)
{
g_message("%s", l_msg);
}
g_free(l_msg);
}
/* ---------------------------------
* gap_moprh_exec_save_workpointfile
* ---------------------------------
*/
gboolean
gap_moprh_exec_save_workpointfile(const char *filename
, GapMorphGUIParams *mgup
)
{
FILE *l_fp;
GapMorphWorkPoint *wp;
gint32 src_layer_width;
gint32 src_layer_height;
gint32 dst_layer_width;
gint32 dst_layer_height;
guchar l_red, l_green, l_blue;
if(filename == NULL) return -1;
l_fp = g_fopen(filename, "w+");
if(l_fp != NULL)
{
src_layer_width = gimp_drawable_width(mgup->mgpp->osrc_layer_id);
src_layer_height = gimp_drawable_height(mgup->mgpp->osrc_layer_id);
dst_layer_width = gimp_drawable_width(mgup->mgpp->fdst_layer_id);
dst_layer_height = gimp_drawable_height(mgup->mgpp->fdst_layer_id);
fprintf(l_fp, "%s\n", GAP_MORPH_WORKPOINT_FILE_HEADER);
fprintf(l_fp, "LAYER-SIZES: %d %d %d %d # src_width src_height dst_width dst_height\n"
,(int)src_layer_width
,(int)src_layer_height
,(int)dst_layer_width
,(int)dst_layer_height
);
fprintf(l_fp, "TWEEN-STEPS: %d\n"
,(int)mgup->mgpp->tween_steps
);
fprintf(l_fp, "AFFECT-RADIUS:");
gap_base_fprintf_gdouble(l_fp, mgup->mgpp->affect_radius, 4, 2, " ");
fprintf(l_fp, " # pixels\n");
fprintf(l_fp, "INTENSITY:");
if(mgup->mgpp->use_gravity)
{
gap_base_fprintf_gdouble(l_fp, mgup->mgpp->gravity_intensity, 2, 3, " ");
fprintf(l_fp, "\n");
}
else
{
fprintf(l_fp, "0 # OFF\n");
}
if(mgup->mgpp->use_quality_wp_selection)
{
fprintf(l_fp, "QUALITY-WP-SELECT: 1 # TRUE\n");
}
else
{
fprintf(l_fp, "QUALITY-WP-SELECT: 0 # FALSE (faster but lower quality)\n");
}
gimp_rgb_get_uchar (&mgup->pointcolor, &l_red, &l_green, &l_blue);
fprintf(l_fp, "POINTCOLOR: %d %d %d\n"
,(int)l_red
,(int)l_green
,(int)l_blue
);
gimp_rgb_get_uchar (&mgup->curr_pointcolor, &l_red, &l_green, &l_blue);
fprintf(l_fp, "CURR-POINTCOLOR: %d %d %d\n"
,(int)l_red
,(int)l_green
,(int)l_blue
);
fprintf(l_fp, "# WP: src_x src_y dst_x dst_y\n");
for(wp = mgup->mgpp->master_wp_list; wp != NULL; wp = (GapMorphWorkPoint *)wp->next)
{
fprintf(l_fp, "WP: ");
gap_base_fprintf_gdouble(l_fp, wp->osrc_x, 4, 3, " ");
gap_base_fprintf_gdouble(l_fp, wp->osrc_y, 4, 3, " ");
gap_base_fprintf_gdouble(l_fp, wp->fdst_x, 4, 3, " ");
gap_base_fprintf_gdouble(l_fp, wp->fdst_y, 4, 3, " ");
fprintf(l_fp, "\n"); /* terminate the output line */
}
fclose(l_fp);
return (TRUE);
}
return (FALSE);
} /* end gap_moprh_exec_save_workpointfile */
/* ----------------------------------
* p_load_workpointfile
* ----------------------------------
* return root pointer to the loaded workpoint list on SUCCESS
* return NULL if load failed.
*/
GapMorphWorkPoint *
p_load_workpointfile(const char *filename
, gint32 src_layer_id
, gint32 dst_layer_id
, gint32 *tween_steps
, gdouble *affect_radius
, gdouble *gravity_intensity
, gboolean *use_gravity
, gboolean *use_quality_wp_selection
, GimpRunMode run_mode
)
{
#define POINT_REC_MAX 512
#define MAX_NUMVALUES_PER_LINE 17
FILE *l_fp;
GapMorphWorkPoint *wp;
GapMorphWorkPoint *wp_list;
gint32 src_layer_width;
gint32 src_layer_height;
gint32 dst_layer_width;
gint32 dst_layer_height;
gdouble src_scale_x;
gdouble src_scale_y;
gdouble dst_scale_x;
gdouble dst_scale_y;
char l_buff[POINT_REC_MAX +1 ];
char *l_ptr;
gint l_cnt;
gdouble l_farr[MAX_NUMVALUES_PER_LINE];
src_layer_width = gimp_drawable_width(src_layer_id);
src_layer_height = gimp_drawable_height(src_layer_id);
dst_layer_width = gimp_drawable_width(dst_layer_id);
dst_layer_height = gimp_drawable_height(dst_layer_id);
src_scale_x = 1.0;
src_scale_y = 1.0;
dst_scale_x = 1.0;
dst_scale_y = 1.0;
wp_list = NULL;
wp = NULL;
if(filename == NULL) return(NULL);
*use_gravity = FALSE;
*use_quality_wp_selection = FALSE;
l_fp = g_fopen(filename, "r");
if(l_fp != NULL)
{
gint l_len;
fgets (l_buff, POINT_REC_MAX, l_fp);
l_len = strlen(GAP_MORPH_WORKPOINT_FILE_HEADER);
if(strncmp(l_buff, GAP_MORPH_WORKPOINT_FILE_HEADER, l_len) != 0)
{
p_error_message_with_filename(run_mode, _("File: %s\n ==>is no workpointfile (header is missing)"), filename);
fclose(l_fp);
return (NULL);
}
while (NULL != fgets (l_buff, POINT_REC_MAX, l_fp))
{
/* skip leading blanks */
l_ptr = l_buff;
while(*l_ptr == ' ') { l_ptr++; }
/* check if line empty or comment only (starts with '#') */
if((*l_ptr != '#') && (*l_ptr != '\n') && (*l_ptr != '\0'))
{
l_len = strlen("LAYER-SIZES:");
if(strncmp(l_ptr, "LAYER-SIZES:", l_len) == 0)
{
l_ptr += l_len;
l_cnt = gap_base_sscan_flt_numbers(l_ptr, &l_farr[0], MAX_NUMVALUES_PER_LINE);
if(l_cnt >= 4)
{
src_scale_x = MAX(1,src_layer_width) / MAX(1,l_farr[0]);
src_scale_y = MAX(1,src_layer_height) / MAX(1,l_farr[1]);
dst_scale_x = MAX(1,dst_layer_width) / MAX(1,l_farr[2]);
dst_scale_y = MAX(1,dst_layer_height) / MAX(1,l_farr[3]);
}
else
{
p_error_message_with_filename(run_mode, _("file: %s\n ==> is corrupted (LAYER-SIZES: record requires 4 numbers)")
, filename);
fclose(l_fp);
return (NULL);
}
}
l_len = strlen("TWEEN-STEPS:");
if(strncmp(l_ptr, "TWEEN-STEPS:", l_len) == 0)
{
l_ptr += l_len;
l_cnt = gap_base_sscan_flt_numbers(l_ptr, &l_farr[0], MAX_NUMVALUES_PER_LINE);
if(l_cnt >= 1)
{
*tween_steps = MAX(1,l_farr[0]);
}
else
{
p_error_message_with_filename(run_mode, _("file: %s\n ==> is corrupted (TWEEN-STEPS record requires 1 number)")
, filename);
fclose(l_fp);
return (NULL);
}
}
l_len = strlen("AFFECT-RADIUS:");
if(strncmp(l_ptr, "AFFECT-RADIUS:", l_len) == 0)
{
l_ptr += l_len;
l_cnt = gap_base_sscan_flt_numbers(l_ptr, &l_farr[0], MAX_NUMVALUES_PER_LINE);
if(l_cnt >= 1)
{
*affect_radius = MAX(0,l_farr[0]);
}
else
{
p_error_message_with_filename(run_mode, _("file: %s ==> is corrupted (AFFECT-RADIUS record requires 1 number)")
, filename);
fclose(l_fp);
return (NULL);
}
}
l_len = strlen("INTENSITY:");
if(strncmp(l_ptr, "INTENSITY:", l_len) == 0)
{
l_ptr += l_len;
l_cnt = gap_base_sscan_flt_numbers(l_ptr, &l_farr[0], MAX_NUMVALUES_PER_LINE);
if(l_cnt >= 1)
{
*gravity_intensity = MAX(0,l_farr[0]);
if(*gravity_intensity > 0.0)
{
*use_gravity = TRUE;
}
}
else
{
p_error_message_with_filename(run_mode, _("file: %s\n ==>is corrupted (INTENSITY record requires 1 number)")
, filename);
fclose(l_fp);
return (NULL);
}
}
l_len = strlen("QUALITY-WP-SELECT:");
if(strncmp(l_ptr, "QUALITY-WP-SELECT:", l_len) == 0)
{
l_ptr += l_len;
l_cnt = gap_base_sscan_flt_numbers(l_ptr, &l_farr[0], MAX_NUMVALUES_PER_LINE);
if(l_cnt >= 1)
{
*use_quality_wp_selection = FALSE;
if(l_farr[0] != 0)
{
*use_quality_wp_selection = TRUE;
}
}
else
{
p_error_message_with_filename(run_mode, _("file: %s\n ==>is corrupted (QUALITY-WP-SELECT record requires 1 number)")
, filename);
fclose(l_fp);
return (NULL);
}
}
l_len = strlen("WP:");
if(strncmp(l_ptr, "WP:", l_len) == 0)
{
l_ptr += l_len;
l_cnt = gap_base_sscan_flt_numbers(l_ptr, &l_farr[0], MAX_NUMVALUES_PER_LINE);
if(l_cnt >= 4)
{
wp = g_new(GapMorphWorkPoint, 1);
/* scale the loaded workpoints
* (to fit the current layer)
*/
wp->osrc_x = l_farr[0] * src_scale_x;
wp->osrc_y = l_farr[1] * src_scale_y;
wp->fdst_x = l_farr[2] * dst_scale_x;
wp->fdst_y = l_farr[3] * dst_scale_y;
wp->src_x = wp->osrc_x;
wp->src_y = wp->osrc_y;
wp->dst_x = wp->fdst_x;
wp->dst_y = wp->fdst_y;
wp->next = wp_list;
wp_list = wp;
}
else
{
p_error_message_with_filename(run_mode, _("file: %s\n ==> is corrupted (WP: record requires 4 numbers)")
, filename);
fclose(l_fp);
return (NULL);
}
}
}
}
fclose(l_fp);
}
return (wp_list);
} /* end p_load_workpointfile */
/* ----------------------------------
* gap_moprh_exec_load_workpointfile
* ----------------------------------
* return root pointer to the loaded workpoint list on SUCCESS
* return NULL if load failed.
*/
GapMorphWorkPoint *
gap_moprh_exec_load_workpointfile(const char *filename
, GapMorphGUIParams *mgup
)
{
GapMorphWorkPoint *wp_list;
wp_list = p_load_workpointfile(filename
,mgup->mgpp->osrc_layer_id
,mgup->mgpp->fdst_layer_id
,&mgup->mgpp->tween_steps
,&mgup->mgpp->affect_radius
,&mgup->mgpp->gravity_intensity
,&mgup->mgpp->use_gravity
,&mgup->mgpp->use_quality_wp_selection
,mgup->mgpp->run_mode
);
return(wp_list);
} /* end gap_moprh_exec_load_workpointfile */
/* ---------------------------------
* p_load_workpoint_set
* ---------------------------------
* load workpoint set from file.
* in case file not exists or other loading troubles
* use the master workpoint list as the default workpoint set.
*/
static GapMorphWarpCoreAPI*
p_load_workpoint_set(const char *filename
,GapMorphGlobalParams *mgpp
,GapMorphExeLayerstack *mlayers
)
{
GapMorphWarpCoreAPI *wps;
gint32 tween_steps;
wps = g_new(GapMorphWarpCoreAPI ,1);
wps->wp_list = p_load_workpointfile(filename
,mgpp->osrc_layer_id
,mgpp->fdst_layer_id
,&tween_steps
,&wps->affect_radius
,&wps->gravity_intensity
,&wps->use_gravity
,&wps->use_quality_wp_selection
,mgpp->run_mode
);
if(wps->wp_list == NULL)
{
wps->wp_list = mgpp->master_wp_list;
wps->affect_radius = mgpp->affect_radius;
wps->gravity_intensity = mgpp->gravity_intensity;
wps->use_gravity = mgpp->use_gravity;
wps->use_quality_wp_selection = mgpp->use_quality_wp_selection;
}
return(wps);
} /* end p_load_workpoint_set */
/* ---------------------------------
* p_build_wp_set_table
* ---------------------------------
*/
static void
p_build_wp_set_table(GapMorphGlobalParams *mgpp
,GapMorphExeLayerstack *mlayers
)
{
long lower_num;
long upper_num;
gchar *lower_basename;
gchar *upper_basename;
gchar *lower_ext;
gchar *upper_ext;
lower_basename = gap_lib_alloc_basename(mgpp->workpoint_file_lower
,&lower_num);
upper_basename = gap_lib_alloc_basename(mgpp->workpoint_file_upper
,&upper_num);
lower_ext = gap_lib_alloc_extension(mgpp->workpoint_file_lower);
upper_ext = gap_lib_alloc_extension(mgpp->workpoint_file_upper);
if((lower_num != upper_num)
&& (strcmp(lower_basename, upper_basename) == 0)
&& (strcmp(lower_ext, upper_ext) == 0))
{
gchar *wp_filename;
gint ii;
gint wp_idx;
if(gap_debug)
{
printf("p_build_wp_set_table: using multiple workpoint sets\n"
" lower_basename:%s\n"
" upper_basename:%s\n lower_num:%d upper_num:%d\n"
,lower_basename
,upper_basename
,(int)lower_num
,(int)upper_num
);
}
mlayers->available_wp_sets = 0;
for(ii=MIN(lower_num, upper_num); ii <= MAX(upper_num, lower_num); ii++)
{
wp_filename = g_strdup_printf("%s%02d%s"
,lower_basename
,(int)ii
,lower_ext
);
if(g_file_test(wp_filename, G_FILE_TEST_EXISTS))
{
mlayers->available_wp_sets++;
}
g_free(wp_filename);
}
mlayers->tab_wp_sets = g_new(GapMorphWarpCoreAPI*, mlayers->available_wp_sets);
wp_idx = 0;
for(ii=MIN(lower_num, upper_num); ii <= MAX(upper_num, lower_num); ii++)
{
wp_filename = g_strdup_printf("%s%02d%s"
,lower_basename
,(int)ii
,lower_ext
);
if(g_file_test(wp_filename, G_FILE_TEST_EXISTS))
{
if(wp_idx < mlayers->available_wp_sets)
{
if(gap_debug)
{
printf("p_build_wp_set_table: LOADING workpoint file: %s\n", wp_filename);
}
mlayers->tab_wp_sets[wp_idx] = p_load_workpoint_set(wp_filename, mgpp, mlayers);
}
wp_idx++;
}
g_free(wp_filename);
}
}
else
{
/* create 2 workpoint sets from upper and lower file */
if(gap_debug)
{
printf("p_build_wp_set_table: create 2 workpoint sets from upper and lower file\n lower:%s\n upper:%s\n"
,mgpp->workpoint_file_lower
,mgpp->workpoint_file_upper
);
}
mlayers->available_wp_sets = 2;
mlayers->tab_wp_sets = g_new(GapMorphWarpCoreAPI*, mlayers->available_wp_sets);
mlayers->tab_wp_sets[0] = p_load_workpoint_set(mgpp->workpoint_file_lower, mgpp, mlayers);
mlayers->tab_wp_sets[1] = p_load_workpoint_set(mgpp->workpoint_file_upper, mgpp, mlayers);
}
g_free(lower_basename);
g_free(upper_basename);
g_free(lower_ext);
g_free(upper_ext);
} /* end p_build_wp_set_table */
/* ---------------------------------
* gap_morph_exec_find_dst_drawable
* ---------------------------------
* search for a 2.nd layer in image_id
* that has same size as the input layer_id
* (prefere layer on top of the input layer_id)
*/
gint32
gap_morph_exec_find_dst_drawable(gint32 image_id, gint32 layer_id)
{
gint l_width;
gint l_height;
gint l_ii;
gint l_nlayers;
gint32 *l_layers_list;
gint32 l_ret_layer_id;
l_ret_layer_id = -1;
l_width = gimp_drawable_width(layer_id);
l_height = gimp_drawable_height(layer_id);
l_layers_list = gimp_image_get_layers(image_id, &l_nlayers);
if(l_layers_list == NULL)
{
return (-1);
}
for(l_ii=0; l_ii < l_nlayers; l_ii++)
{
if(l_layers_list[l_ii] != layer_id)
{
if((gimp_drawable_width(l_layers_list[l_ii]) == l_width)
&& (gimp_drawable_height(l_layers_list[l_ii]) == l_height))
{
l_ret_layer_id = l_layers_list[l_ii];
break;
}
if(l_ret_layer_id == -1)
{
l_ret_layer_id = l_layers_list[l_ii];
}
}
}
g_free (l_layers_list);
return(l_ret_layer_id);
} /* end gap_morph_exec_find_dst_drawable */
/* -----------------------------------
* p_clip_gimp_pixel_fetcher_get_pixel
* -----------------------------------
*/
static void
p_clip_gimp_pixel_fetcher_get_pixel(GimpPixelFetcher *pft
,GimpDrawable *drawable
,gint xi
,gint yi
,guchar *pixel
)
{
if((xi >= 0)
&& (yi >= 0)
&& (xi < (gint)drawable->width)
&& (yi < (gint)drawable->height))
{
gimp_pixel_fetcher_get_pixel (pft, xi, yi, pixel);
}
else
{
/* deliver full transparent black pixel when out of bounds */
pixel[0] = 0;
pixel[1] = 0;
pixel[2] = 0;
pixel[3] = 0;
}
} /* end p_clip_gimp_pixel_fetcher_get_pixel */
/* ------------------------
* p_bilinear_get_pixel
* ------------------------
* pixel picking at gdouble needx/needy koords
* picks 4 pixels at nearest integer koordinates
* and calculate the value by bilinear interpolation.
*/
static void
p_bilinear_get_pixel(GimpPixelFetcher *pft
,GimpDrawable *drawable
, gdouble needx
, gdouble needy
, guchar *dest
, gint dst_bpp
)
{
guchar pixel[4][4];
guchar values[4];
guchar val;
gint xi, yi;
guint k;
if (needx >= 0.0)
xi = (int) needx;
else
xi = -((int) -needx + 1);
if (needy >= 0.0)
yi = (int) needy;
else
yi = -((int) -needy + 1);
p_clip_gimp_pixel_fetcher_get_pixel (pft, drawable, xi, yi, pixel[0]);
p_clip_gimp_pixel_fetcher_get_pixel (pft, drawable, xi + 1, yi, pixel[1]);
p_clip_gimp_pixel_fetcher_get_pixel (pft, drawable, xi, yi + 1, pixel[2]);
p_clip_gimp_pixel_fetcher_get_pixel (pft, drawable, xi + 1, yi + 1, pixel[3]);
for (k = 0; k < drawable->bpp; k++)
{
values[0] = pixel[0][k];
values[1] = pixel[1][k];
values[2] = pixel[2][k];
values[3] = pixel[3][k];
val = gimp_bilinear_8 (needx, needy, values);
*dest++ = val;
}
} /* end p_bilinear_get_pixel */
/* ---------------------------------
* p_linear_advance
* ---------------------------------
*/
static gdouble
p_linear_advance(gdouble total_steps
,gdouble current_step
,gdouble val_from
,gdouble val_to
)
{
gdouble delta;
if(total_steps < 1) return(val_from);
delta = ((val_to - val_from) / total_steps) * (total_steps - current_step);
return (val_to - delta);
} /* end p_linear_advance */
/* ----------------------------------
* gap_morph_exec_free_workpoint_list
* ----------------------------------
*/
void
gap_morph_exec_free_workpoint_list(GapMorphWorkPoint **wp_list)
{
if(wp_list)
{
GapMorphWorkPoint *wp;
GapMorphWorkPoint *wp_next;
wp_next = NULL;
for(wp = *wp_list; wp != NULL; wp = wp_next)
{
wp_next = wp->next;
g_free(wp);
}
*wp_list = NULL;
}
} /* end gap_morph_exec_free_workpoint_list */
/* ---------------------------------
* p_calc_sector
* ---------------------------------
* return sector index (an integer val 0 upto 7)
* (is much faster than calculating the angle, but restricted to 8 sectors)
*/
static inline gint
p_calc_sector(GapMorphWorkPoint *wp, gdouble dx, gdouble dy)
{
if(dx >= 0)
{
if(dy >= 0)
{
if(dx >= dy)
{
return(3); /* 91 - 135 degree */
}
return(2); /* 136 - 180 degree */
}
if(dx >= abs(dy))
{
return(4); /* 181 - 225 degree */
}
return(5); /* 226 - 270 degree */
}
if(dy >= 0)
{
if(abs(dx) >= dy)
{
return(0); /* 0 - 45 degree */
}
return(1); /* 46 - 90 degree */
}
if(abs(dx) >= abs(dy))
{
return(7); /* 316 - 360 degree */
}
return(6); /* 271 - 315 degree */
} /* end p_calc_sector */
/* ---------------------------------
* p_calc_angle_core
* ---------------------------------
* calculate the angle and return as sector (rounded to sectorsize)
*/
static inline gint
p_calc_angle_core(GapMorphWorkPoint *wp, gdouble dx, gdouble dy, gint num_sectors)
{
gdouble angle_rad;
gdouble sector;
if(dx == 0)
{
if(dy < 0)
{
wp->angle_deg = 270;
}
else
{
if(dy > 0)
{
wp->angle_deg = 90;
}
else
{
wp->angle_deg = 0;
}
}
}
else
{
gdouble adx;
gdouble ady;
adx = abs(dx);
ady = abs(dy);
angle_rad = atan(ady / adx);
/* angle_rad = atan(abs(dy) / abs(dx)); */ /* this version crashes sometimes, dont know why ? */
wp->angle_deg = 180.0 * (angle_rad / G_PI);
if(dx > 0)
{
if(dy < 0)
{
wp->angle_deg = 360 - wp->angle_deg;
}
}
else
{
if(dy > 0)
{
wp->angle_deg = 180 - wp->angle_deg;
}
else
{
wp->angle_deg += 180;
}
}
}
sector = (wp->angle_deg / 360.0) * num_sectors;
return(CLAMP((gint)sector, 0 , (num_sectors-1)));
} /* end p_calc_angle_core */
/* ---------------------------------
* p_calc_angle
* ---------------------------------
* calculate the angle (rounded to sectorsize) and distance
* between in_x/in_y and the workpoint
*/
static gint
p_calc_angle(GapMorphWorkPoint *wp, gint32 in_x, gint32 in_y)
{
gdouble dx;
gdouble dy;
gdouble adx;
gdouble ady;
if(wp)
{
dx = in_x - wp->dst_x;
dy = in_y - wp->dst_y;
wp->sqr_dist = (dx * dx ) + (dy * dy);
wp->dist = sqrt(wp->sqr_dist);
adx = abs(dx);
ady = abs(dy);
if(adx > ady)
{
wp->xy_relation = (gint)(0.5 + (GAP_MORPH_XY_REL_FAKTOR * (ady / adx)));
}
else
{
if(dy != 0)
{
wp->xy_relation = (gint)(0.5 + (GAP_MORPH_XY_REL_FAKTOR * (adx / ady)));
}
else
{
wp->xy_relation = 0;
}
}
return(p_calc_angle_core(wp, dx, dy, GAP_MORPH_8_SEKTORS));
}
return (0);
} /* end p_calc_angle */
/* ---------------------------------
* p_pixel_warp_core
* ---------------------------------
* the movement vektor for in_x/in_y is calculated by mixing
* the movement vektors of the selected workpoints,
* weighted by invers distances of the workpoint to in_x/in_y
*
* there are 2 strategies (FAST / QUALITY) for selecting relevant
* workpoints.
* FAST strategy uses just the nearest workpoints in 8 sektors,
* QUALITY looks for all workpoints per sektor within a tolerance radius
* but discards points with similar angle and greater distance
*
* in case there is no workpoint available
* the pixel is picked by simply scaling in_x/in_y to ssrc koords
*
*/
static void
p_pixel_warp_core(GapMorphWarpCoreAPI *wcap
, gint32 in_x
, gint32 in_y
, gdouble *out_pick_x
, gdouble *out_pick_y
)
{
register GapMorphWorkPoint *wp;
GapMorphWorkPoint *wp_sek;
GapMorphWorkPoint *wp_sektor_tab[GAP_MORPH_8_SEKTORS];
gdouble tab_tol[GAP_MORPH_8_SEKTORS];
/* index tables to access neigbour sectors */
static gint tab_l1_idx[GAP_MORPH_8_SEKTORS] = {7,0,1,2,3,4,5,6};
static gint tab_l2_idx[GAP_MORPH_8_SEKTORS] = {6,7,0,1,2,3,4,5};
static gint tab_r1_idx[GAP_MORPH_8_SEKTORS] = {1,2,3,4,5,6,7,0};
static gint tab_r2_idx[GAP_MORPH_8_SEKTORS] = {2,3,4,5,6,7,0,1};
gdouble vektor_x; /* movement vektor for the current point at in_x/in_y */
gdouble vektor_y;
gdouble new_vektor_x;
gdouble new_vektor_y;
gdouble weight;
gdouble sum_inv_dist;
gboolean direct_hit_flag;
gint32 sek_idx;
gdouble min_sqr_dist;
gdouble limit_sqr_dist;
gdouble dx;
gdouble dy;
gdouble adx;
gdouble ady;
/* reset sektor tab */
for(sek_idx=0; sek_idx < GAP_MORPH_8_SEKTORS; sek_idx++)
{
wp_sektor_tab[sek_idx] = NULL;
}
vektor_x = 0;
vektor_y = 0;
direct_hit_flag = FALSE;
min_sqr_dist = wcap->sqr_affect_radius;
/* loop1 calculate square distance,
* check for direct hits
* and build sector table (nearest workpoints foreach sector
*/
for(wp=wcap->wp_list; wp != NULL; wp = (GapMorphWorkPoint *)wp->next)
{
dx = in_x - wp->dst_x;
dy = in_y - wp->dst_y;
wp->sqr_dist = (dx * dx ) + (dy * dy);
wp->sek_idx = 0;
if(wp->sqr_dist <= wcap->sqr_affect_radius)
{
if(wp->sqr_dist < 1)
{
new_vektor_x = (wp->src_x - wp->dst_x);
new_vektor_y = (wp->src_y - wp->dst_y);
if(direct_hit_flag)
{
vektor_x = (new_vektor_x + vektor_x) / 2.0;
vektor_y = (new_vektor_y + vektor_y) / 2.0;
}
else
{
vektor_x = new_vektor_x;
vektor_y = new_vektor_y;
}
direct_hit_flag = TRUE;
}
else
{
wp->is_alive = TRUE;
/* sektor index 8 sectors */
sek_idx = p_calc_sector(wp, dx, dy);
wp->sek_idx = sek_idx;
if((wp_sek = wp_sektor_tab[sek_idx]) != NULL)
{
/* there is already a workpoint for this sektor */
if(wp->sqr_dist <= wp_sek->sqr_dist)
{
/* set current workpoint as the new nearest point for this sektor */
wp_sektor_tab[sek_idx] = wp;
}
else
{
continue;
}
}
else
{
/* found 1.st workpint for this sektor */
wp_sektor_tab[sek_idx] = wp;
}
wp->next_sek = NULL; /* init empty sektors list */
if(wp->sqr_dist < min_sqr_dist)
{
min_sqr_dist = wp->sqr_dist;
}
}
}
}
if(!direct_hit_flag)
{
GapMorphWorkPoint *wp_selected_list;
sum_inv_dist = 0;
wp_selected_list = NULL;
/* calculate sector sqr_distance limit table for the 8 sektors */
if(min_sqr_dist <= GAP_MORPH_NEAR_SQR_RADIUS)
{
limit_sqr_dist = min_sqr_dist * GAP_MORPH_MIN_TOL_FAKTOR;
for(sek_idx = 0; sek_idx < GAP_MORPH_8_SEKTORS; sek_idx++)
{
tab_tol[sek_idx] = limit_sqr_dist;
}
}
else
{
limit_sqr_dist = wcap->sqr_affect_radius;
for(sek_idx = 0; sek_idx < GAP_MORPH_8_SEKTORS; sek_idx++)
{
wp_sek = wp_sektor_tab[sek_idx];
if(wp_sek)
{
GapMorphWorkPoint *wp_l_sek; /* left neigbour sektor */
GapMorphWorkPoint *wp_r_sek; /* right neigbour sektor */
gdouble l_tol;
gdouble r_tol;
/* xy_relation is the representation of the angle
* (is used to compare for similar angle in steps 10tiems finer than the 8 sektors.
* and is faster to calculate than the real angle)
*/
adx = abs(in_x - wp_sek->dst_x);
ady = abs(in_y - wp_sek->dst_y);
if(adx > ady)
{
wp_sek->xy_relation = (gint)(0.5 + (GAP_MORPH_XY_REL_FAKTOR * (ady / adx)));
}
else
{
if(ady != 0)
{
wp_sek->xy_relation = (gint)(0.5 + (GAP_MORPH_XY_REL_FAKTOR * (adx / ady)));
}
else
{
wp_sek->xy_relation = 0;
}
}
wp_l_sek = wp_sektor_tab[tab_l1_idx[sek_idx]];
wp_r_sek = wp_sektor_tab[tab_r1_idx[sek_idx]];
if(wp_l_sek == NULL)
{
wp_l_sek = wp_sektor_tab[tab_l2_idx[sek_idx]];
}
if(wp_r_sek == NULL)
{
wp_r_sek = wp_sektor_tab[tab_r2_idx[sek_idx]];
}
if(wp_l_sek)
{
if(wp_r_sek)
{
l_tol = wp_l_sek->sqr_dist * GAP_MORPH_TOL_FAKTOR;
r_tol = wp_r_sek->sqr_dist * GAP_MORPH_TOL_FAKTOR;
tab_tol[sek_idx] = MIN(MAX(l_tol, r_tol), (wp_sek->sqr_dist * GAP_MORPH_TOL_FAKTOR));
}
else
{
tab_tol[sek_idx] = wp_sek->sqr_dist * GAP_MORPH_TOL_FAKTOR;
}
}
else
{
tab_tol[sek_idx] = wp_sek->sqr_dist * GAP_MORPH_TOL_FAKTOR;
}
}
else
{
tab_tol[sek_idx] = limit_sqr_dist;
}
}
}
if(wcap->use_quality_wp_selection)
{
/* using the QUALITY workpoint selection strategy:
* select all points per sector within the sektor specific tolerance
* but discard those points that have same angle as another nearer point
* in the same sektor
*/
for(wp=wcap->wp_list; wp != NULL; wp = (GapMorphWorkPoint *)wp->next)
{
sek_idx = CLAMP(wp->sek_idx, 0, (GAP_MORPH_8_SEKTORS -1));
if(wp->sqr_dist <= tab_tol[sek_idx])
{
gboolean sel_flag;
sel_flag = TRUE;
/* xy_relation is the representation of the angle
* (is used to compare for similar angle in steps 10tiems finer than the 8 sektors.
* and is faster to calculate than the real angle)
*/
adx = abs(in_x - wp->dst_x);
ady = abs(in_y - wp->dst_y);
if(adx > ady)
{
wp->xy_relation = (gint)(0.5 + (GAP_MORPH_XY_REL_FAKTOR * (ady / adx)));
}
else
{
if(ady != 0)
{
wp->xy_relation = (gint)(0.5 + (GAP_MORPH_XY_REL_FAKTOR * (adx / ady)));
}
else
{
wp->xy_relation = 0;
}
}
for(wp_sek = wp_sektor_tab[sek_idx]; wp_sek != NULL; wp_sek = (GapMorphWorkPoint *)wp_sek->next_sek)
{
if(wp_sek == wp)
{
sel_flag = FALSE; /* dont add the same point twice to the sektors list */
continue;
}
if((wp_sek->is_alive)
&& (wp->xy_relation == wp_sek->xy_relation)) /* check for (nearly) same angle */
{
if(wp->sqr_dist < wp_sek->sqr_dist)
{
/* discard the (farer) point from the sektor list by flag
* (is faster than remove the list element)
*/
wp_sek->is_alive = FALSE;
}
else
{
sel_flag = FALSE;
}
break;
}
}
if(sel_flag)
{
/* add wp to the sektor list */
wp->next_sek = wp_sektor_tab[sek_idx];
wp_sektor_tab[sek_idx] = wp;
}
}
}
}
/* build selection list by merging all 8 sektor sublists where workpoint is still alive
* for the FAST strategy each sektor sublist ha s just one (the nearest) or zero elements
* the QUALITY strategy can have more points per sektor within the sektor specific tolerance.
*/
for(sek_idx = 0; sek_idx < GAP_MORPH_8_SEKTORS; sek_idx++)
{
for(wp=wp_sektor_tab[sek_idx]; wp != NULL; wp = (GapMorphWorkPoint *)wp->next_sek)
{
if((wp->is_alive)
&& (wp->sqr_dist <= tab_tol[sek_idx]))
{
wp->dist = sqrt(wp->sqr_dist);
if(wcap->printf_flag)
{
/* this is a debug feature for tuning the warp algorithm */
printf("sek: %02d, ang: %.2f, dist:%.3f, sqr_dist: %.3f TAB_TOL:%.3f SELECTED\n"
,(int)sek_idx
,(float)wp->angle_deg
,(float)wp->dist
,(float)wp->sqr_dist
,(float)tab_tol[sek_idx]
);
}
/* add point to selection list */
wp->next_selected = wp_selected_list;
wp_selected_list = wp;
wp->inv_dist = 1.0 / wp->dist;
sum_inv_dist += wp->inv_dist;
}
}
} /* end for(sek_idx..) */
/* build result vektor as weightened mix of all selected workpoint vektors */
for(wp = wp_selected_list; wp != NULL; wp = wp->next_selected)
{
wp->gravity = 1.0;
if(wcap->use_gravity)
{
wp->gravity -= pow((MIN(wp->sqr_dist,wcap->sqr_affect_radius) / wcap->sqr_affect_radius) , wcap->gravity_intensity);
}
new_vektor_x = (wp->src_x - wp->dst_x);
new_vektor_y = (wp->src_y - wp->dst_y);
weight = (wp->inv_dist / sum_inv_dist) * wp->gravity;
vektor_x += (new_vektor_x * weight);
vektor_y += (new_vektor_y * weight);
if(wcap->printf_flag)
{
wp->warp_weight = weight;
printf("wp->src_x/y: %.3f / %.3f wp->dst_x/y: %.3f / %.3f sek_idx: %d xy_rel:%d dist:%.3f weight: %.7f vek: %.3f / %.3f \n"
, (float)wp->src_x
, (float)wp->src_y
, (float)wp->dst_x
, (float)wp->dst_y
, (int) wp->sek_idx
, (int) wp->xy_relation
, (float)wp->dist
, (float)weight
, (float)vektor_x
, (float)vektor_y
);
}
}
} /* end direct_hit_flag */
*out_pick_x = (in_x + vektor_x) * wcap->scale_x;
*out_pick_y = (in_y + vektor_y) * wcap->scale_y;
} /* end p_pixel_warp_core */
/* -----------------------------------
* gap_morph_exec_get_warp_pick_koords
* -----------------------------------
* this procedure is used for the show option (a debug feature in the GUI)
* it is not used for the rendering, but should give an idea
* how selecting of relevant workpoints will work and set
* the weight values for all workpoints for later display in the GUI
* This was the major quality tuning tool for the development of the warp
* algorithm
*/
void
gap_morph_exec_get_warp_pick_koords(GapMorphWorkPoint *wp_list
, gint32 in_x
, gint32 in_y
, gdouble scale_x
, gdouble scale_y
, gboolean use_quality_wp_selection
, gboolean use_gravity
, gdouble gravity_intensity
, gdouble affect_radius
, gdouble *out_pick_x
, gdouble *out_pick_y
)
{
GapMorphWarpCoreAPI wcap_struct;
GapMorphWarpCoreAPI *wcap;
GapMorphWorkPoint *wp;
gdouble pick_x;
gdouble pick_y;
gdouble sum_pick_x;
gdouble sum_pick_y;
gint xx;
gint yy;
gint nn;
wcap = &wcap_struct;
wcap->sqr_affect_radius = affect_radius * affect_radius;
wcap->wp_list = wp_list;
wcap->scale_x = scale_x;
wcap->scale_y = scale_y;
wcap->use_gravity = use_gravity;
wcap->gravity_intensity = gravity_intensity;
wcap->printf_flag = TRUE;
wcap->use_quality_wp_selection = use_quality_wp_selection;
for(wp=wp_list; wp != NULL; wp = (GapMorphWorkPoint *)wp->next)
{
wp->warp_weight = -1; /* for debug display in warp_info_label */
wp->dist = p_calc_angle(wp, in_x, in_y); /* for debug display in warp_info_label */
if(wp->dist < 1)
{
wp->warp_weight = -4; /* for debug display in warp_info_label */
}
}
printf("==== scale: %.2f / %.2f in_x:%02d in_y:%02d == \n"
, (float)scale_x
, (float)scale_y
, (int)in_x
, (int)in_y
);
nn = 0;
sum_pick_x = 0;
sum_pick_y = 0;
#ifdef GAP_MORPH_USE_SIMPLE_PIXEL_WARP
p_pixel_warp_core(wcap
,in_x
,in_y
,&pick_x
,&pick_y
);
#else
for(xx=MAX(0,in_x -3); xx <= in_x +3; xx+=3)
{
wcap->printf_flag = FALSE;
for(yy=MAX(0,in_y -3); yy <= in_y +3; yy+=3)
{
p_pixel_warp_core(wcap
,xx
,yy
,&pick_x
,&pick_y
);
sum_pick_x += pick_x;
sum_pick_y += pick_y;
nn++;
}
}
pick_x = sum_pick_x / (gdouble)nn;
pick_y = sum_pick_y / (gdouble)nn;
/* this call is for reset wp->angel and other things to the center pick point */
wcap->printf_flag = TRUE;
p_pixel_warp_core(wcap
,in_x
,in_y
,out_pick_x
,out_pick_y
);
#endif
*out_pick_x = pick_x;
*out_pick_y = pick_y;
printf("pick_x/y : %.3f / %.3f\n"
, (float)*out_pick_x
, (float)*out_pick_y
);
} /* end gap_morph_exec_get_warp_pick_koords */
/* ---------------------------------
* p_pixel_warp_pick
* ---------------------------------
*/
static void
p_pixel_warp_pick(GapMorphWarpCoreAPI *wcap
, gint32 in_x
, gint32 in_y
, gint set_idx
, gdouble *out_pick_x
, gdouble *out_pick_y
)
{
gdouble pick_x;
gdouble pick_y;
gdouble sum_pick_x;
gdouble sum_pick_y;
gint xx;
gint yy;
gint nn;
GapMorphExePickCache *pcp;
#ifdef GAP_MORPH_USE_SIMPLE_PIXEL_WARP
p_pixel_warp_core(wcap
,in_x
,in_y
,out_pick_x
,out_pick_y
);
#else
/* the implementation of the p_pixel_warp_core
* produces unwanted hard edges at those places where
* the selection of relevant near workpoints
* switches to another set of workpoints with
* signifikant different movement settings.
* To comensate this unwanted effect, we do multiple picks
* and use the average pick koordinates.
* We use gloabl_pick_cache to reduce the effective
* number of p_pixel_warp_core calls
*/
nn = 0;
sum_pick_x = 0;
sum_pick_y = 0;
for(xx=MAX(0,in_x -3); xx <= in_x +3; xx+=3)
{
for(yy=MAX(0,in_y -3); yy <= in_y +3; yy+=3)
{
gint ii;
for(ii=0; ii < GAP_MORPH_PCK_CACHE_SIZE; ii++)
{
pcp = &gloabl_pick_cache[ii][set_idx];
if ((pcp->valid)
&& (pcp->xx == xx)
&& (pcp->yy == yy))
{
pick_x = pcp->pick_x;
pick_y = pcp->pick_y;
/* printf("** CACHE HIT\n"); */
break;
}
}
if(ii == GAP_MORPH_PCK_CACHE_SIZE)
{
/* printf("** must do pixel warp\n"); */
p_pixel_warp_core(wcap
,xx
,yy
,&pick_x
,&pick_y
);
/* save pick koords in cache table */
pcp = &gloabl_pick_cache[gloabl_pick_cache_idx[set_idx]][set_idx];
pcp->xx = xx;
pcp->yy = yy;
pcp->pick_x = pick_x;
pcp->pick_y = pick_y;
pcp->valid = TRUE;
gloabl_pick_cache_idx[set_idx]++;
if(gloabl_pick_cache_idx[set_idx] >= GAP_MORPH_PCK_CACHE_SIZE)
{
gloabl_pick_cache_idx[set_idx] = 0;
}
}
sum_pick_x += (pick_x - ((in_x - xx) * wcap->scale_x));
sum_pick_y += (pick_y - ((in_y - yy) * wcap->scale_y));
nn++;
}
}
*out_pick_x = sum_pick_x / (gdouble)nn;
*out_pick_y = sum_pick_y / (gdouble)nn;
#endif
} /* end p_pixel_warp_pick */
/* ---------------------------------
* p_pixel_warp_multipick
* ---------------------------------
*/
static void
p_pixel_warp_multipick(GapMorphWarpCoreAPI *wcap_1
,GapMorphWarpCoreAPI *wcap_2
,gdouble wp_mix_factor
, gint32 in_x
, gint32 in_y
, gdouble *out_pick_x
, gdouble *out_pick_y
)
{
gdouble pick_x_1;
gdouble pick_y_1;
gdouble pick_x_2;
gdouble pick_y_2;
pick_x_1 = 0;
pick_y_1 = 0;
pick_x_2 = 0;
pick_y_2 = 0;
if(wp_mix_factor != 0.0)
{
p_pixel_warp_pick(wcap_1
, in_x
, in_y
, 0 /* set_idx */
, &pick_x_1
, &pick_y_1
);
}
if(wp_mix_factor != 1.0)
{
p_pixel_warp_pick(wcap_1
, in_x
, in_y
, 1 /* set_idx */
, &pick_x_2
, &pick_y_2
);
}
*out_pick_x = (pick_x_1 * wp_mix_factor) + (pick_x_2 * (1 - wp_mix_factor));
*out_pick_y = (pick_y_1 * wp_mix_factor) + (pick_y_2 * (1 - wp_mix_factor));
} /* end p_pixel_warp_multipick */
/* ---------------------------------
* p_calculate_work_point_movement
* ---------------------------------
* return the newly created workpoint list
* the list contains duplicates of the input workpoint master_list
* with transformed point koordinates, according to current movement
* step and scaling.
*
* forward_move == TRUE:
* dup->fdest_x,y := master->fdest_x,y
* dup->orsc_x,y := master->orsc_x,y
* dup->src_x,y := scale (master->osrc_x,y)
* dup->dst_x,y := linear_advance(master->orsc_x,y --> master->fdest_x,y)
*
* forward_move == FALSE:
* dup->fdest_x,y := master->osrc_x,y
* dup->orsc_x,y := master->fdest_x,y
* dup->src_x,y := scale (master->fdest_x,y)
* dup->dst_x,y := linear_advance(master->fdest_x,y --> master->orsc_x,y)
*
*/
GapMorphWorkPoint *
p_calculate_work_point_movement(gdouble total_steps
,gdouble current_step
,GapMorphWorkPoint *master_list
,gint32 src_layer_id
,gint32 dst_layer_id
,gint32 curr_width
,gint32 curr_height
,gboolean forward_move
)
{
GapMorphWorkPoint *wp_list;
GapMorphWorkPoint *wp_elem_orig;
GapMorphWorkPoint *wp_elem_dup;
GapMorphWorkPoint *wp_elem_prev;
gdouble scalex;
gdouble scaley;
if(gap_debug) printf ("p_calculate_work_point_movement: START\n");
if(forward_move)
{
scalex = (gdouble)curr_width / (gdouble)gimp_drawable_width(src_layer_id);
scaley = (gdouble)curr_height / (gdouble)gimp_drawable_height(src_layer_id);
}
else
{
scalex = (gdouble)curr_width / (gdouble)gimp_drawable_width(dst_layer_id);
scaley = (gdouble)curr_height / (gdouble)gimp_drawable_height(dst_layer_id);
}
wp_list = NULL;
wp_elem_prev = NULL;
for(wp_elem_orig = master_list; wp_elem_orig != NULL; wp_elem_orig = (GapMorphWorkPoint *)wp_elem_orig->next)
{
wp_elem_dup = g_new(GapMorphWorkPoint, 1);
wp_elem_dup->next = NULL;
if(forward_move)
{
wp_elem_dup->fdst_x = wp_elem_orig->fdst_x;
wp_elem_dup->fdst_y = wp_elem_orig->fdst_y;
wp_elem_dup->osrc_x = wp_elem_orig->osrc_x;
wp_elem_dup->osrc_y = wp_elem_orig->osrc_y;
wp_elem_dup->dst_x = p_linear_advance((gdouble)total_steps
,(gdouble)current_step
,(gdouble)wp_elem_orig->osrc_x
,(gdouble)wp_elem_orig->fdst_x
);
wp_elem_dup->dst_y = p_linear_advance((gdouble)total_steps
,(gdouble)current_step
,(gdouble)wp_elem_orig->osrc_y
,(gdouble)wp_elem_orig->fdst_y
);
wp_elem_dup->src_x = scalex * wp_elem_orig->osrc_x;
wp_elem_dup->src_y = scaley * wp_elem_orig->osrc_y;
}
else
{
wp_elem_dup->fdst_x = wp_elem_orig->osrc_x;
wp_elem_dup->fdst_y = wp_elem_orig->osrc_y;
wp_elem_dup->osrc_x = wp_elem_orig->fdst_x;
wp_elem_dup->osrc_y = wp_elem_orig->fdst_y;
wp_elem_dup->dst_x = p_linear_advance((gdouble)total_steps
,(gdouble)(total_steps - current_step)
,(gdouble)wp_elem_orig->fdst_x
,(gdouble)wp_elem_orig->osrc_x
);
wp_elem_dup->dst_y = p_linear_advance((gdouble)total_steps
,(gdouble)(total_steps - current_step)
,(gdouble)wp_elem_orig->fdst_y
,(gdouble)wp_elem_orig->osrc_y
);
wp_elem_dup->src_x = scalex * wp_elem_orig->fdst_x;
wp_elem_dup->src_y = scaley * wp_elem_orig->fdst_y;
}
if(gap_debug)
{
printf("WP_DUP: osrc: %03.2f / %03.2f fdst: %03.2f / %03.2f src: %03.2f / %03.2f dst: %03.2f / %03.2f\n"
,(float)wp_elem_dup->osrc_x
,(float)wp_elem_dup->osrc_y
,(float)wp_elem_dup->fdst_x
,(float)wp_elem_dup->fdst_y
,(float)wp_elem_dup->src_x
,(float)wp_elem_dup->src_y
,(float)wp_elem_dup->dst_x
,(float)wp_elem_dup->dst_y
);
}
if(wp_elem_prev == NULL)
{
wp_list = wp_elem_dup;
}
else
{
wp_elem_prev->next = wp_elem_dup;
}
wp_elem_prev = wp_elem_dup;
}
return(wp_list);
} /* end p_calculate_work_point_movement */
/* ---------------------------------
* p_layer_warp_move
* ---------------------------------
*/
static void
p_layer_warp_move (GapMorphWorkPoint *wp_list_1
,GapMorphWorkPoint *wp_list_2
, gint32 src_layer_id
, gint32 dst_layer_id
, GapMorphGlobalParams *mgpp
, GapMorphWarpCoreAPI *p1
, GapMorphWarpCoreAPI *p2
, gdouble wp_mix_factor
)
{
GapMorphWarpCoreAPI wcap_struct_1;
GapMorphWarpCoreAPI wcap_struct_2;
GapMorphWarpCoreAPI *wcap_1;
GapMorphWarpCoreAPI *wcap_2;
GimpDrawable *src_drawable;
GimpDrawable *dst_drawable;
GimpPixelFetcher *src_pixfet;
GimpPixelRgn dstPR;
gpointer pr;
guchar *pixel_ptr;
guint32 l_row;
guint32 l_col;
gdouble scale_x;
gdouble scale_y;
gdouble l_max_progress;
gdouble l_progress;
gint ii;
gint set_idx;
wcap_1 = &wcap_struct_1;
wcap_2 = &wcap_struct_2;
wcap_1->wp_list = wp_list_1;
wcap_2->wp_list = wp_list_2;
wcap_1->sqr_affect_radius = mgpp->affect_radius * mgpp->affect_radius;
wcap_1->use_gravity = mgpp->use_gravity;
wcap_1->gravity_intensity = mgpp->gravity_intensity;
if(p1)
{
wcap_1->sqr_affect_radius = p1->affect_radius * p1->affect_radius;
wcap_1->use_gravity = p1->use_gravity;
wcap_1->gravity_intensity = p1->gravity_intensity;
}
wcap_2->sqr_affect_radius = mgpp->affect_radius * mgpp->affect_radius;
wcap_2->use_gravity = mgpp->use_gravity;
wcap_2->gravity_intensity = mgpp->gravity_intensity;
if(p2)
{
wcap_2->sqr_affect_radius = p2->sqr_affect_radius * p2->sqr_affect_radius;
wcap_2->use_gravity = p2->use_gravity;
wcap_2->gravity_intensity = p2->gravity_intensity;
}
wcap_1->printf_flag = FALSE;
wcap_2->printf_flag = FALSE;
wcap_1->use_quality_wp_selection = mgpp->use_quality_wp_selection;
wcap_2->use_quality_wp_selection = mgpp->use_quality_wp_selection;
/* clear the global cache for picking koordinates */
for(set_idx=0; set_idx < 2; set_idx++)
{
for(ii=0; ii < GAP_MORPH_PCK_CACHE_SIZE; ii++)
{
gloabl_pick_cache[ii][set_idx].valid = FALSE;
}
gloabl_pick_cache_idx[set_idx] = 0;
}
src_drawable = gimp_drawable_get (src_layer_id);
dst_drawable = gimp_drawable_get (dst_layer_id);
if(src_drawable == NULL) { return; }
if(dst_drawable == NULL) { return; }
if(src_drawable->bpp != dst_drawable->bpp) { return; }
if((src_drawable->bpp != 4) && (src_drawable->bpp != 2))
{
return;
}
/* if src and dst size not equal: caluclate scaling factors x/y */
scale_x = src_drawable->width / MAX(1,dst_drawable->width);
scale_y = src_drawable->height / MAX(1,dst_drawable->height);
wcap_1->scale_x = scale_x;
wcap_2->scale_x = scale_x;
wcap_1->scale_y = scale_y;
wcap_2->scale_y = scale_y;
/* init Pixel Fetcher for source drawable */
src_pixfet = gimp_pixel_fetcher_new (src_drawable, FALSE /*shadow*/);
if(src_drawable == NULL) { return; }
/* init Pixel Region */
gimp_pixel_rgn_init (&dstPR, dst_drawable
, 0
, 0
, dst_drawable->width
, dst_drawable->height
, TRUE /* dirty */
, TRUE /* shadow */
);
l_max_progress = dst_drawable->width * dst_drawable->height;
l_progress = 0;
for (pr = gimp_pixel_rgns_register (1, &dstPR);
pr != NULL;
pr = gimp_pixel_rgns_process (pr))
{
guint x;
guint y;
guchar *dest;
dest = dstPR.data;
for (y = 0; y < dstPR.h; y++)
{
pixel_ptr = dest;
l_row = dstPR.y + y;
for (x = 0; x < dstPR.w; x++)
{
gdouble pick_x;
gdouble pick_y;
l_col = dstPR.x + x;
if(mgpp->have_workpointsets)
{
/* pick based on 2 sets of workpoints */
p_pixel_warp_multipick(wcap_1 /// XXXXX list1
, wcap_2 /// XXXXX list2
, wp_mix_factor
, l_col
, l_row
, &pick_x
, &pick_y
);
}
else
{
/* pick based on a single set of workpoints */
p_pixel_warp_pick(wcap_1
, l_col
, l_row
, 0 /* set_idx */
, &pick_x
, &pick_y
);
}
p_bilinear_get_pixel (src_pixfet
,src_drawable
,pick_x
,pick_y
,pixel_ptr
,dst_drawable->bpp);
pixel_ptr += dstPR.bpp;
}
dest += dstPR.rowstride;
}
if(mgpp->do_progress)
{
gdouble l_total_progress;
l_progress += (dstPR.w * dstPR.h);
l_total_progress = mgpp->master_progress
+ (mgpp->layer_progress_step * (l_progress /l_max_progress));
/*
* if)gap_debug)
* {
* printf("Progress: mgpp->master_progress:%f mgpp->layer_progress_step:%f l_total_progress:%f\n"
* ,(float)mgpp->master_progress
* ,(float)mgpp->layer_progress_step
* ,(float)l_total_progress
* );
* }
*/
gimp_progress_update(l_total_progress);
}
}
gimp_drawable_flush (dst_drawable);
gimp_drawable_merge_shadow (dst_drawable->drawable_id, TRUE);
gimp_drawable_update (dst_layer_id
, 0
, 0
, dst_drawable->width
, dst_drawable->height
);
gimp_pixel_fetcher_destroy (src_pixfet);
gimp_drawable_detach(src_drawable);
gimp_drawable_detach(dst_drawable);
} /* end p_layer_warp_move */
/* ---------------------------------
* p_mix_layers
* ---------------------------------
* create a layer by mixing bg_layer_id and top_layer_id
*/
static gint32
p_mix_layers (gint32 curr_image_id
,gint32 bg_layer_id
,gint32 top_layer_id
,gint32 curr_width
,gint32 curr_height
,gdouble curr_mix_factor /* 0.0 <= mix <= 1.0 */
)
{
gint32 dst_layer_id;
GimpDrawable *top_drawable;
GimpDrawable *bg_drawable;
GimpDrawable *dst_drawable;
GimpPixelRgn dstPR;
GimpPixelRgn topPR;
GimpPixelRgn bgPR;
gpointer pr;
if(gap_debug)
{
printf("p_mix_layers START curr_mix_factor: %f\n"
,(float)curr_mix_factor
);
}
top_drawable = gimp_drawable_get (top_layer_id);
bg_drawable = gimp_drawable_get (bg_layer_id);
if(top_drawable == NULL) { return (-1); }
if(bg_drawable == NULL) { return (-1); }
if(top_drawable->bpp < 3)
{
dst_layer_id = gimp_layer_new(curr_image_id, "morph_tween"
, curr_width
, curr_height
, GIMP_GRAYA_IMAGE
, 100.0 /* full opaque */
, GIMP_NORMAL_MODE
);
}
else
{
dst_layer_id = gimp_layer_new(curr_image_id, "morph_tween"
, curr_width
, curr_height
, GIMP_RGBA_IMAGE
, 100.0 /* full opaque */
, GIMP_NORMAL_MODE
);
}
gimp_image_add_layer(curr_image_id, dst_layer_id, 0);
dst_drawable = gimp_drawable_get (dst_layer_id);
if(dst_drawable == NULL) { return (-1); }
if(gap_debug)
{
printf("p_mix_layers dst_layer_id: %d bpp:%d top_layer_id:%d bpp:%d bg_layer_id:%d bpp:%d\n"
,(int)dst_layer_id
,(int)dst_drawable->bpp
,(int)top_layer_id
,(int)top_drawable->bpp
,(int)bg_layer_id
,(int)bg_drawable->bpp
);
}
/* init Pixel Region */
gimp_pixel_rgn_init (&dstPR, dst_drawable
, 0
, 0
, dst_drawable->width
, dst_drawable->height
, TRUE /* dirty */
, TRUE /* shadow */
);
gimp_pixel_rgn_init (&topPR, top_drawable
, 0
, 0
, top_drawable->width
, top_drawable->height
, FALSE /* dirty */
, FALSE /* shadow */
);
gimp_pixel_rgn_init (&bgPR, bg_drawable
, 0
, 0
, bg_drawable->width
, bg_drawable->height
, FALSE /* dirty */
, FALSE /* shadow */
);
for (pr = gimp_pixel_rgns_register (3, &dstPR, &topPR, &bgPR);
pr != NULL;
pr = gimp_pixel_rgns_process (pr))
{
guint x;
guint y;
guchar *dest;
guchar *top;
guchar *bg;
guchar *pixel_ptr;
guchar *top_ptr;
guchar *bg_ptr;
dest = dstPR.data;
top = topPR.data;
bg = bgPR.data;
for (y = 0; y < dstPR.h; y++)
{
pixel_ptr = dest;
top_ptr = top;
bg_ptr = bg;
for (x = 0; x < dstPR.rowstride ; x++)
{
gdouble val;
val = MIX_VALUE(curr_mix_factor, (gdouble)(*bg_ptr), (gdouble)(*top_ptr));
*pixel_ptr = (guchar)val;
pixel_ptr++;
top_ptr++;
bg_ptr++;
}
dest += dstPR.rowstride;
top += topPR.rowstride;
bg += bgPR.rowstride;
}
}
gimp_drawable_flush (dst_drawable);
gimp_drawable_merge_shadow (dst_drawable->drawable_id, TRUE);
gimp_drawable_update (dst_layer_id
, 0
, 0
, dst_drawable->width
, dst_drawable->height
);
gimp_drawable_detach(bg_drawable);
gimp_drawable_detach(top_drawable);
gimp_drawable_detach(dst_drawable);
return(dst_layer_id);
} /* end p_mix_layers */
/* ---------------------------------
* p_create_morph_tween_frame
* ---------------------------------
* return the newly created tween morphed layer
*/
static gint32
p_create_morph_tween_frame(gint32 total_steps
,gint32 current_step
,GapMorphGlobalParams *mgpp
,GapMorphExeLayerstack *mlayers
)
{
GimpDrawable *dst_drawable;
gint32 curr_image_id; /* a temp image of current layer size */
gint32 bg_layer_id;
gint32 top_layer_id;
gint32 merged_layer_id;
gint32 curr_width;
gint32 curr_height;
gdouble curr_opacity;
GapMorphWorkPoint *curr_wp_list_1;
GapMorphWorkPoint *curr_wp_list_2;
GapMorphWarpCoreAPI *wp_set_1;
GapMorphWarpCoreAPI *wp_set_2;
gint32 src_layer_id;
gint32 dst_layer_id;
gboolean forward_move;
gdouble wp_mix_factor;
forward_move = TRUE;
src_layer_id = mgpp->osrc_layer_id;
dst_layer_id = mgpp->fdst_layer_id;
wp_set_1 = NULL;
wp_set_2 = NULL;
wp_mix_factor = 1.0;
if(gap_debug)
{
printf("p_create_morph_tween_frame START total_steps:%d current_step:%d\n"
" mgpp->create_tween_layers:%d\n"
" mgpp->have_workpointsets:%d\n"
" mlayers->available_wp_sets:%d\n"
" mgpp->render_mode:%d\n"
,(int)total_steps
,(int)current_step
,(int)mgpp->create_tween_layers
,(int)mgpp->have_workpointsets
,(int)mlayers->available_wp_sets
,(int)mgpp->render_mode
);
}
if(mgpp->create_tween_layers)
{
/* size of the new frame */
curr_width = p_linear_advance((gdouble)total_steps
,(gdouble)current_step
,(gdouble)gimp_drawable_width(src_layer_id)
,(gdouble)gimp_drawable_width(dst_layer_id)
);
curr_height = p_linear_advance((gdouble)total_steps
,(gdouble)current_step
,(gdouble)gimp_drawable_height(src_layer_id)
,(gdouble)gimp_drawable_height(dst_layer_id)
);
}
else
{
gint ii;
ii = mlayers->dst1_idx - (current_step -1);
/* operate on existing destination layers */
dst_layer_id = mlayers->dst_layers[ii];
curr_width = gimp_drawable_width(dst_layer_id);
curr_height = gimp_drawable_height(dst_layer_id);
ii = p_linear_advance((gdouble)total_steps
,(gdouble)current_step
,(gdouble)mlayers->src1_idx
,(gdouble)mlayers->src2_idx
);
/* in this case also operate with changing source layers (if there are more than one) */
src_layer_id = mlayers->src_layers[ii];
}
if((mgpp->have_workpointsets)
&& (mlayers->available_wp_sets > 1))
{
gint wps_idx;
gint wps_idx_2;
gdouble wp_stepspeed;
gdouble ref;
wp_stepspeed = (gdouble)(mlayers->available_wp_sets -1) / (gdouble)MIN((total_steps -1),1);
ref = (gdouble)((gdouble)(current_step -1) * wp_stepspeed);
wps_idx = (gint)ref;
wps_idx_2 = MIN(wps_idx+1, (mlayers->available_wp_sets -1));
wp_set_1 = mlayers->tab_wp_sets[wps_idx];
wp_set_2 = mlayers->tab_wp_sets[wps_idx_2];
wp_mix_factor = ref - (gdouble)wps_idx;
}
dst_drawable = gimp_drawable_get (dst_layer_id);
if(gap_debug)
{
printf("p_create_morph_tween_frame dst_layer_id:%d, bpp:%d\n"
,(int)dst_layer_id
,(int)dst_drawable->bpp
);
}
/* add empty BG layer */
if(dst_drawable->bpp < 3)
{
/* create the tween frame image */
curr_image_id = gimp_image_new(curr_width, curr_height, GIMP_GRAY);
bg_layer_id = gimp_layer_new(curr_image_id, "bg_morph_layer"
, curr_width
, curr_height
, GIMP_GRAYA_IMAGE
, 100.0 /* full opaque */
, GIMP_NORMAL_MODE
);
}
else
{
/* create the tween frame image */
curr_image_id = gimp_image_new(curr_width, curr_height, GIMP_RGB);
bg_layer_id = gimp_layer_new(curr_image_id, "bg_morph_layer"
, curr_width
, curr_height
, GIMP_RGBA_IMAGE
, 100.0 /* full opaque */
, GIMP_NORMAL_MODE
);
}
gimp_image_add_layer(curr_image_id, bg_layer_id, 0);
/* add empty TOP layer */
curr_opacity = p_linear_advance((gdouble)total_steps
,(gdouble)current_step
,(gdouble)0.0
,(gdouble)100.0
);
if(dst_drawable->bpp < 3)
{
top_layer_id = gimp_layer_new(curr_image_id, "top_morph_layer"
, curr_width
, curr_height
, GIMP_GRAYA_IMAGE
, curr_opacity
, GIMP_NORMAL_MODE
);
}
else
{
top_layer_id = gimp_layer_new(curr_image_id, "top_morph_layer"
, curr_width
, curr_height
, GIMP_RGBA_IMAGE
, curr_opacity
, GIMP_NORMAL_MODE
);
}
gimp_image_add_layer(curr_image_id, top_layer_id, 0);
if(!mgpp->render_mode == GAP_MORPH_RENDER_MODE_WARP)
{
if((wp_set_1) && (wp_set_2))
{
/* multi workpoint settings for src_layer */
curr_wp_list_1 = p_calculate_work_point_movement((gdouble)total_steps
,(gdouble)current_step
,wp_set_1->wp_list
,mgpp->osrc_layer_id
,mgpp->fdst_layer_id
,curr_width
,curr_height
,forward_move
);
curr_wp_list_2 = p_calculate_work_point_movement((gdouble)total_steps
,(gdouble)current_step
,wp_set_2->wp_list
,mgpp->osrc_layer_id
,mgpp->fdst_layer_id
,curr_width
,curr_height
,forward_move
);
/* warp the BG layer */
p_layer_warp_move ( curr_wp_list_1
, curr_wp_list_2
, src_layer_id
, bg_layer_id
, mgpp
, wp_set_1
, wp_set_2
, wp_mix_factor
);
gap_morph_exec_free_workpoint_list(&curr_wp_list_1);
gap_morph_exec_free_workpoint_list(&curr_wp_list_2);
}
else
{
/* simple workpoint settings for src_layer */
curr_wp_list_1 = p_calculate_work_point_movement((gdouble)total_steps
,(gdouble)current_step
,mgpp->master_wp_list
,mgpp->osrc_layer_id
,mgpp->fdst_layer_id
,curr_width
,curr_height
,forward_move
);
/* warp the BG layer */
p_layer_warp_move ( curr_wp_list_1
, NULL
, src_layer_id
, bg_layer_id
, mgpp
, NULL
, NULL
, wp_mix_factor
);
gap_morph_exec_free_workpoint_list(&curr_wp_list_1);
}
forward_move = FALSE;
mgpp->master_progress += mgpp->layer_progress_step;
}
if((wp_set_1) && (wp_set_2))
{
/* milti workpoint settings for sc_layer */
curr_wp_list_1 = p_calculate_work_point_movement((gdouble)total_steps
,(gdouble)current_step
,wp_set_1->wp_list
,mgpp->osrc_layer_id
,mgpp->fdst_layer_id
,curr_width
,curr_height
,forward_move
);
curr_wp_list_2 = p_calculate_work_point_movement((gdouble)total_steps
,(gdouble)current_step
,wp_set_2->wp_list
,mgpp->osrc_layer_id
,mgpp->fdst_layer_id
,curr_width
,curr_height
,forward_move
);
/* warp the TOP layer */
p_layer_warp_move ( curr_wp_list_1
, curr_wp_list_2
, dst_layer_id
, top_layer_id
, mgpp
, wp_set_1
, wp_set_2
, wp_mix_factor
);
gap_morph_exec_free_workpoint_list(&curr_wp_list_1);
gap_morph_exec_free_workpoint_list(&curr_wp_list_2);
}
else
{
/* simple workpoint settings for sc_layer */
curr_wp_list_1 = p_calculate_work_point_movement((gdouble)total_steps
,(gdouble)current_step
,mgpp->master_wp_list
,mgpp->osrc_layer_id
,mgpp->fdst_layer_id
,curr_width
,curr_height
,forward_move
);
/* warp the TOP layer */
p_layer_warp_move ( curr_wp_list_1
, NULL
, dst_layer_id
, top_layer_id
, mgpp
, NULL
, NULL
, wp_mix_factor
);
gap_morph_exec_free_workpoint_list(&curr_wp_list_1);
}
gimp_drawable_detach(dst_drawable);
if(mgpp->render_mode == GAP_MORPH_RENDER_MODE_WARP)
{
/* for warp only mode all is done at this point */
return(top_layer_id);
}
/* merge BG and TOP Layer (does mix opacity according to current step) */
// merged_layer_id =
// gap_image_merge_visible_layers(curr_image_id, GIMP_EXPAND_AS_NECESSARY);
merged_layer_id =
p_mix_layers(curr_image_id
,bg_layer_id
,top_layer_id
,curr_width
,curr_height
,(gdouble)(curr_opacity / 100.0)
);
/* DEBUG code: show duplicate of the temporary tween image */
if(FALSE)
{
gint32 dup_id;
dup_id = gimp_image_duplicate(curr_image_id);
gimp_display_new (dup_id);
}
return(merged_layer_id);
} /* end p_create_morph_tween_frame */
/* ---------------------------------
* p_get_tween_steps_and_layerstacks
* ---------------------------------
* get layerstack for both src and dst
*
* return the constraint tween_steps value
* when operating on existing destination layers
*/
static gint32
p_get_tween_steps_and_layerstacks(GapMorphGlobalParams *mgpp, GapMorphExeLayerstack *mlayers)
{
gint32 src_image_id;
gint32 dst_image_id;
gint32 tween_steps;
gint32 ii;
tween_steps = 0;
mlayers->src_nlayers = 0;
mlayers->src_layers = NULL;
mlayers->dst_nlayers = 0;
mlayers->dst_layers = NULL;
mlayers->src1_idx = -1;
mlayers->src2_idx = -1;
mlayers->dst1_idx = -1;
mlayers->dst2_idx = -1;
src_image_id = -1;
dst_image_id = -1;
if(gap_debug)
{
printf("p_get_tween_steps_and_layerstacks: START\n");
}
if(mgpp->osrc_layer_id >= 0)
{
src_image_id = gimp_drawable_get_image(mgpp->osrc_layer_id);
mlayers->src_layers = gimp_image_get_layers (src_image_id
,&mlayers->src_nlayers
);
for(ii=0; ii < mlayers->src_nlayers; ii++)
{
if(gap_debug)
{
printf("src: id[%d]: %d\n", (int)ii, (int)mlayers->src_layers[ii] );
}
if(mlayers->src_layers[ii] == mgpp->osrc_layer_id)
{
mlayers->src1_idx = ii; /* src at 1.st step */
mlayers->src2_idx = ii; /* src at last step */
break;
}
}
}
if(mgpp->fdst_layer_id >= 0)
{
tween_steps = mgpp->tween_steps;
dst_image_id = gimp_drawable_get_image(mgpp->fdst_layer_id);
mlayers->dst_layers = gimp_image_get_layers (dst_image_id
,&mlayers->dst_nlayers
);
for(ii=0; ii < mlayers->dst_nlayers; ii++)
{
if(gap_debug) printf("dst: id[%d]: %d\n", (int)ii, (int)mlayers->dst_layers[ii] );
if(mlayers->dst_layers[ii] == mgpp->fdst_layer_id)
{
mlayers->dst2_idx = ii; /* dst at last step */
break;
}
}
}
if(mgpp->create_tween_layers)
{
mlayers->dst1_idx = mlayers->dst2_idx + (tween_steps -1);
if(gap_debug)
{
printf("p_get_tween_steps_and_layerstacks: create_tween_layers RETURN tween_steps:%d\n"
,(int)tween_steps
);
}
return(tween_steps);
}
if(mgpp->render_mode == GAP_MORPH_RENDER_MODE_WARP)
{
if(dst_image_id == src_image_id)
{
if(mlayers->dst2_idx > mlayers->src1_idx)
{
gint32 tmp;
/* source layer is above the destination layer
* in this case swap src with dst layer
*/
tmp = mgpp->fdst_layer_id;
mgpp->fdst_layer_id = mgpp->osrc_layer_id;
mgpp->osrc_layer_id = tmp;
tmp = mlayers->dst2_idx;
mlayers->dst2_idx = mlayers->src1_idx;
mlayers->src1_idx = tmp;
mlayers->src2_idx = tmp;
}
tween_steps = 1 + (mlayers->src1_idx - mlayers->dst2_idx );
}
else
{
tween_steps = MIN(tween_steps, (mlayers->dst_nlayers - mlayers->dst2_idx) );
}
}
mlayers->dst1_idx = mlayers->dst2_idx + (tween_steps -1);
if(mgpp->render_mode == GAP_MORPH_RENDER_MODE_WARP)
{
mgpp->osrc_layer_id = mlayers->dst_layers[mlayers->dst1_idx];
}
mlayers->src1_idx = MIN((mlayers->src2_idx + (tween_steps -1)) , (mlayers->src_nlayers -1));
if(gap_debug)
{
printf("p_get_tween_steps_and_layerstacks: create_tween_layers END, RETURN tween_steps:%d\n"
,(int)tween_steps
);
}
return(tween_steps);
} /* end p_get_tween_steps_and_layerstacks */
/* ---------------------------------
* gap_morph_execute
* ---------------------------------
* layer morphing master procedure
*/
gint32
gap_morph_execute(GapMorphGlobalParams *mgpp)
{
gint32 total_steps;
gint32 current_step;
gint32 last_step;
gint32 new_layer_id;
gint32 cp_layer_id;
gint32 dst_image_id;
gint32 dst_stack_position;
GapMorphExeLayerstack mlayers_struct;
GapMorphExeLayerstack *mlayers;
mlayers = &mlayers_struct;
mlayers->tab_wp_sets = NULL;
mlayers->available_wp_sets = 0;
mgpp->tween_steps = p_get_tween_steps_and_layerstacks(mgpp, mlayers);
if(mgpp->have_workpointsets)
{
/* check for available workpointfile sets and load them.
* (note: this also handles the case when working with a single workpointfile
* where the same workpoint file is used both as upper and lower set)
*/
p_build_wp_set_table(mgpp ,mlayers);
}
if(gap_debug) printf("gap_morph_execute: START src_layer:%d dst_layer:%d tween_steps:%d\n"
,(int)mgpp->osrc_layer_id
,(int)mgpp->fdst_layer_id
,(int)mgpp->tween_steps
);
if(mgpp->osrc_layer_id == mgpp->fdst_layer_id)
{
/* for identical layers we do only warp
* (fading identical images makes no sense, force warp only mode)
*/
mgpp->render_mode = GAP_MORPH_RENDER_MODE_WARP;
}
if(mgpp->do_progress)
{
if(mgpp->render_mode == GAP_MORPH_RENDER_MODE_MORPH)
{
gimp_progress_init(_("creating morph tween layers..."));
}
else
{
gimp_progress_init(_("creating warp tween layers..."));
}
}
cp_layer_id = -1;
dst_image_id = gimp_drawable_get_image(mgpp->fdst_layer_id);
dst_stack_position = 1;
/* findout stackposition (where to insert new tween layer(s)
* in the destination image.
* (one below the mgpp->fdst_layer_id Layer)
* where 0 is on top of stack
*/
{
gint ii;
gint l_nlayers;
gint32 *l_layers_list;
l_layers_list = gimp_image_get_layers(dst_image_id, &l_nlayers);
for(ii=0; ii < l_nlayers; ii++)
{
if(l_layers_list[ii] == mgpp->fdst_layer_id)
{
dst_stack_position = ii +1;
break;
}
}
if(l_layers_list)
{
g_free (l_layers_list);
}
}
last_step = mgpp->tween_steps;
total_steps = 1 + mgpp->tween_steps;
mgpp->master_progress = 0.0;
mgpp->layer_progress_step = 0.5 / (gdouble)(MAX(1, last_step));
if(mgpp->render_mode == GAP_MORPH_RENDER_MODE_WARP)
{
/* warp_only mode (equal layer)
* must do one extra step to render the final distortion step
* (in morph mode this makes no sense, because this extra step
* would deliver the destination layer again as result)
*/
last_step = mgpp->tween_steps;
total_steps = mgpp->tween_steps;
mgpp->layer_progress_step = 1.0 / (gdouble)(MAX(1, last_step));
}
for(current_step=1; current_step <= last_step; current_step++)
{
new_layer_id = p_create_morph_tween_frame(total_steps
,current_step
,mgpp
,mlayers
);
if(new_layer_id < 0)
{
continue;
break;
}
else
{
gint32 tween_image_id;
gint l_src_offset_x, l_src_offset_y; /* layeroffsets as they were in src_image */
if(mgpp->create_tween_layers)
{
cp_layer_id = gap_layer_copy_to_dest_image(dst_image_id
,new_layer_id
,100.0 /* Opacity */
,0 /* NORMAL */
,&l_src_offset_x
,&l_src_offset_y
);
gimp_image_add_layer(dst_image_id
, cp_layer_id
, dst_stack_position
);
}
else
{
gint ii;
/* replace content of current dst_layer[ii] by content of new_layer_id */
ii = mlayers->dst1_idx - (current_step -1);
gap_layer_copy_content(mlayers->dst_layers[ii], new_layer_id);
}
tween_image_id = gimp_drawable_get_image(new_layer_id);
gimp_image_delete(tween_image_id);
}
if(mgpp->do_progress)
{
mgpp->master_progress = (gdouble)current_step / (gdouble)(MAX(1, last_step));
gimp_progress_update(mgpp->master_progress);
}
}
return(cp_layer_id);
} /* end gap_morph_execute */
/* ----------------------------------
* p_create_simple_fade_tween_frame
* ----------------------------------
*
* return the newly created tween morphed layer
*
*/
static gint32
p_create_simple_fade_tween_frame(gint32 total_steps
,gint32 current_step
,GapMorphGlobalParams *mgpp
)
{
gint32 curr_image_id; /* a temp image of current layer size */
gint32 bg_layer_id;
gint32 top_layer_id;
gint32 merged_layer_id;
gint32 curr_width;
gint32 curr_height;
gdouble curr_opacity;
gint32 src_layer_id;
gint32 dst_layer_id;
GimpImageType l_src_type;
src_layer_id = mgpp->osrc_layer_id;
dst_layer_id = mgpp->fdst_layer_id;
/* check size of the new frame */
curr_width = gimp_drawable_width(src_layer_id);
if (curr_width != gimp_drawable_width(dst_layer_id))
{
return -1;
}
curr_height = gimp_drawable_height(src_layer_id);
if (curr_height != gimp_drawable_height(dst_layer_id))
{
return -1;
}
l_src_type = gimp_drawable_type(src_layer_id);
if (l_src_type != gimp_drawable_type(dst_layer_id))
{
return -1;
}
curr_image_id = -1;
switch(l_src_type)
{
case GIMP_RGB_IMAGE: /* 0 */
case GIMP_RGBA_IMAGE: /* 1 */
curr_image_id = gimp_image_new(curr_width, curr_height, GIMP_RGB);
break;
case GIMP_GRAY_IMAGE: /* 2 */
case GIMP_GRAYA_IMAGE: /* 3 */
curr_image_id = gimp_image_new(curr_width, curr_height, GIMP_GRAY);
break;
case GIMP_INDEXED_IMAGE: /* 4 */
case GIMP_INDEXEDA_IMAGE: /* 5 */
return -1;
break;
}
bg_layer_id = gap_layer_copy_to_image (curr_image_id, mgpp->osrc_layer_id);
top_layer_id = gap_layer_copy_to_image (curr_image_id, mgpp->fdst_layer_id);
/* merge BG and TOP Layer (does mix opacity according to current step) */
curr_opacity = p_linear_advance((gdouble)total_steps
,(gdouble)current_step
,(gdouble)0.0
,(gdouble)100.0
);
merged_layer_id =
p_mix_layers(curr_image_id
,bg_layer_id
,top_layer_id
,curr_width
,curr_height
,(gdouble)(curr_opacity / 100.0)
);
mgpp->master_progress += mgpp->layer_progress_step;
/* DEBUG code: show duplicate of the temporary tween image */
if(FALSE)
{
gint32 dup_id;
dup_id = gimp_image_duplicate(curr_image_id);
gimp_display_new (dup_id);
}
return(merged_layer_id);
} /* end p_create_simple_fade_tween_frame */
/* ----------------------------------
* gap_morph_render_one_of_n_tweens
* ----------------------------------
* This procedure creates only one tween (a scene between 2 video frames)
* according to the specified total_steps and current_step parameters.
* the created tween is part of a new created image
* that contians the tween layer.
*
* return the newly created tween morphed layer
*
*/
gint32
gap_morph_render_one_of_n_tweens(GapMorphGlobalParams *mgpp, gdouble total_steps, gdouble current_step)
{
gint32 new_layer_id;
GapMorphExeLayerstack mlayers_struct;
GapMorphExeLayerstack *mlayers;
mlayers = &mlayers_struct;
mlayers->tab_wp_sets = NULL;
mlayers->available_wp_sets = 0;
new_layer_id = -1;
p_get_tween_steps_and_layerstacks(mgpp, mlayers);
mgpp->create_tween_layers = TRUE;
/* check if workpoint filename is available */
if(mgpp->workpoint_file_lower)
{
if(*mgpp->workpoint_file_lower != '\0')
{
/* load a single workpointfile
* (note: we use general procedure p_build_wp_set_table and handle the single workpointfile
* as mix of upper and lower set that both refere to the same single workpointfile)
*/
p_build_wp_set_table(mgpp ,mlayers);
mgpp->have_workpointsets = FALSE;
mlayers->available_wp_sets = 0;
}
}
/* overrule the number of steps with the explicite parameter value */
mgpp->tween_steps = total_steps;
if(mgpp->do_simple_fade)
{
/* we have no workpoints available
* in this case make an attempt with a fast fade operation
* (but restricted to frames of same size and type)
*/
new_layer_id = p_create_simple_fade_tween_frame(total_steps
,current_step
,mgpp
);
}
if( new_layer_id < 0)
{
if(gap_debug)
{
printf("calling the full morph algorithm total_steps:%f current_step:%f\n"
,(float)total_steps
,(float)current_step
);
}
new_layer_id = p_create_morph_tween_frame(total_steps
,current_step
,mgpp
,mlayers
);
}
return (gap_image_merge_to_specified_layer(new_layer_id, GIMP_CLIP_TO_IMAGE));
} /* end gap_morph_render_one_of_n_tweens */
/* ----------------------------------
* gap_morph_render_one_tween
* ----------------------------------
* This procedure creates only one tween (a scene between 2 video frames)
* according to the specified mix factor (mgpp->tween_mix_factor)
* the created tween is part of a new created image
* that contians the tween layer.
*
* return the newly created tween morphed layer
*
*/
gint32
gap_morph_render_one_tween(GapMorphGlobalParams *mgpp)
{
gint32 new_layer_id;
gdouble total_steps;
gdouble current_step;
total_steps = 1.0 * 100000;
current_step = CLAMP(mgpp->tween_mix_factor, 0.0, 1.0) * 100000;
new_layer_id = gap_morph_render_one_of_n_tweens(mgpp, total_steps, current_step);
return (new_layer_id);
} /* end gap_morph_render_one_tween */
/* ----------------------------------
* gap_morph_render_frame_tweens
* ----------------------------------
*
* return the newly created tween morphed layer
*
*/
gint32
gap_morph_render_frame_tweens(GapAnimInfo *ainfo_ptr, GapMorphGlobalParams *mgpp)
{
gint32 l_tween_layer_id;
gint32 l_tween_frame_nr;
gint32 l_rc;
gdouble total_steps;
l_tween_layer_id = -1;
total_steps = (mgpp->range_to - mgpp->range_from);
mgpp->tween_steps = (mgpp->range_to - mgpp->range_from); /// ??? - 1;
mgpp->master_progress = 0.0;
mgpp->layer_progress_step = 0.5 / (gdouble)(MAX(1.0, (total_steps -1.0)));
if(mgpp->do_progress)
{
gimp_progress_init(_("creating morph tween frames..."));
}
if (mgpp->tween_steps > 0)
{
gint32 l_tmp_image_id;
if(ainfo_ptr->new_filename != NULL)
{
g_free(ainfo_ptr->new_filename);
}
ainfo_ptr->new_filename = gap_lib_alloc_fname(ainfo_ptr->basename,
mgpp->range_to,
ainfo_ptr->extension);
if(ainfo_ptr->new_filename == NULL)
{
printf("could not create frame filename for frameNr:%d\n", (int)mgpp->range_to);
return -1;
}
if(!g_file_test(ainfo_ptr->new_filename, G_FILE_TEST_EXISTS))
{
printf("target frame does not exist, name: %s\n", ainfo_ptr->new_filename);
if (mgpp->run_mode != GIMP_RUN_NONINTERACTIVE)
{
g_message(_("target frame does not exist, name: %s"), ainfo_ptr->new_filename);
}
return -1;
}
/* load current frame */
l_tmp_image_id = gap_lib_load_image(ainfo_ptr->new_filename);
if(gap_debug)
{
printf("gap_morph_render_frame_tweens to frame: %s l_tmp_image_id:%d RUN_MODE:%d\n"
,ainfo_ptr->new_filename
,(int)l_tmp_image_id
,(int)mgpp->run_mode
);
}
mgpp->osrc_layer_id = gap_image_merge_visible_layers(gimp_image_duplicate(mgpp->image_id), GIMP_CLIP_TO_IMAGE);
mgpp->fdst_layer_id = gap_image_merge_visible_layers(l_tmp_image_id, GIMP_CLIP_TO_IMAGE);
if(gap_debug)
{
printf("gap_morph_render_frame_tweens osrc_layer_id:%d fdst_layer_id:%d \n"
,(int)mgpp->osrc_layer_id
,(int)mgpp->fdst_layer_id
);
}
for (l_tween_frame_nr = mgpp->range_from +1; l_tween_frame_nr < mgpp->range_to; l_tween_frame_nr++)
{
gint32 l_tween_tmp_image_id;
gdouble l_current_step;
mgpp->master_progress = 2.0 * mgpp->layer_progress_step * (l_tween_frame_nr - (mgpp->range_from +1));
if(l_tween_layer_id >= 0)
{
/* delete the previous handled tween frame image in memory. (that is already saved to disk
* we keep only the last one opened)
*/
gap_image_delete_immediate(gimp_drawable_get_image(l_tween_layer_id));
}
l_current_step = l_tween_frame_nr - mgpp->range_from;
if(ainfo_ptr->new_filename != NULL)
{
g_free(ainfo_ptr->new_filename);
}
ainfo_ptr->new_filename = gap_lib_alloc_fname(ainfo_ptr->basename,
l_tween_frame_nr,
ainfo_ptr->extension);
if(mgpp->do_progress)
{
char *l_msg;
l_msg = g_strdup_printf(_("creating morph tween frame: %d"), (int)l_tween_frame_nr);
gimp_progress_init(l_msg);
g_free(l_msg);
}
if(gap_debug)
{
printf("gap_morph_render_frame_tweens creating tween:%s\n"
,ainfo_ptr->new_filename
);
}
if (mgpp->overwrite_flag != TRUE)
{
if(g_file_test(ainfo_ptr->new_filename, G_FILE_TEST_EXISTS))
{
l_tween_layer_id = -1;
p_error_message_with_filename(mgpp->run_mode, _("file: %s already exists"), ainfo_ptr->new_filename);
break;
}
}
l_tween_layer_id = gap_morph_render_one_of_n_tweens(mgpp, total_steps, l_current_step);
l_tween_tmp_image_id = gimp_drawable_get_image(l_tween_layer_id);
if(gap_debug)
{
printf("gap_morph_render_frame_tweens saving tween:%s :%d\n"
,ainfo_ptr->new_filename
,(int)l_tween_tmp_image_id
);
}
l_rc = gap_lib_save_named_frame(l_tween_tmp_image_id, ainfo_ptr->new_filename);
if(ainfo_ptr->new_filename != NULL)
{
g_free(ainfo_ptr->new_filename);
ainfo_ptr->new_filename = NULL;
}
if (l_rc < 0)
{
l_tween_layer_id = -1;
p_error_message_with_filename(mgpp->run_mode, _("file: %s save failed"), ainfo_ptr->new_filename);
break;
}
}
gap_image_delete_immediate(gimp_drawable_get_image(mgpp->osrc_layer_id));
gap_image_delete_immediate(gimp_drawable_get_image(mgpp->fdst_layer_id));
}
return(l_tween_layer_id);
} /* end gap_morph_render_frame_tweens */
|