1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471
|
/********************************************************************************
Fotoxx edit photos and manage collections
Copyright 2007-2016 Michael Cornelison
Source URL: http://kornelix.net
Contact: kornelix@posteo.de
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 http://www.gnu.org/licenses/.
*********************************************************************************
Fotoxx image edit - warp menu functions
m_unbend straighten curvature added by pano or improve perspective
m_perspective select a tetragon and convert into a rectangle
m_warp_area select an area and warp interior with mouse drags
m_unwarp_CU select a face in a close-up photo, remove distortion
m_warp_curved warp an image or area using a curved transform
m_warp_linear warp an image using a linear transform
m_warp_affine warp an image using an affine transform
m_flatbook flatten a photographed book page
m_sphere spherical image projection, variable radius
m_selective_rescale rescale while leaving selected areas unchanged
m_waves distort an image with a wave pattern
*********************************************************************************/
#define EX extern // enable extern declarations
#include "fotoxx.h" // (variables in fotoxx.h are refs)
/********************************************************************************/
// unbend an image
// straighten curvature added by pano or improve perspective
float unbend_lin_horz, unbend_lin_vert; // unbend values from dialog
float unbend_cur_horz, unbend_cur_vert;
float unbend_x1, unbend_x2, unbend_y1, unbend_y2; // unbend axes scaled 0 to 1
int unbend_hx1, unbend_hy1, unbend_hx2, unbend_hy2;
int unbend_vx1, unbend_vy1, unbend_vx2, unbend_vy2;
editfunc EFunbend;
// menu function
void m_unbend(GtkWidget *, cchar *) // overhauled
{
int unbend_dialog_event(zdialog* zd, cchar *event);
void * unbend_thread(void *);
void unbend_mousefunc();
F1_help_topic = "unbend_image";
EFunbend.funcname = "unbend";
EFunbend.FprevReq = 1; // use preview
EFunbend.Frestart = 1; // restart allowed
EFunbend.threadfunc = unbend_thread; // thread function
EFunbend.mousefunc = unbend_mousefunc; // mouse function
if (! edit_setup(EFunbend)) return; // setup edit
PXM_addalpha(E0pxm); // 15.09
PXM_addalpha(E1pxm);
PXM_addalpha(E3pxm);
/*** // 16.06
_________________________________________
| Unbend |
| ____ ____ |
| | | [____|-+] | | [____|-+] | splinvert spcurvert
| |____| |____| |
| |
| ____ ____ |
| | | [____|-+] | | [____|-+] | splinhorz spcurhorz
| |____| |____| |
| |
| [ grid ] |
| [done] [cancel] |
|_________________________________________|
***/
zdialog *zd = zdialog_new(ZTX("Unbend"),Mwin,Bdone,Bcancel,null);
EFunbend.zd = zd;
zdialog_add_widget(zd,"hbox","hb1","dialog",0,"space=3");
zdialog_add_widget(zd,"icon","VL","hb1","unbend vert linear.png","size=64|space=5");
zdialog_add_widget(zd,"spin","splinvert","hb1","-99|99|1|0");
zdialog_add_widget(zd,"label","space","hb1",0,"space=10");
zdialog_add_widget(zd,"icon","VC","hb1","unbend vert curved.png","size=64|space=5");
zdialog_add_widget(zd,"spin","spcurvert","hb1","-99|99|1|0");
zdialog_add_widget(zd,"hbox","hb2","dialog",0,"space=3");
zdialog_add_widget(zd,"icon","HL","hb2","unbend horz linear.png","size=64|space=5");
zdialog_add_widget(zd,"spin","splinhorz","hb2","-99|99|1|0");
zdialog_add_widget(zd,"label","space","hb2",0,"space=10");
zdialog_add_widget(zd,"icon","HC","hb2","unbend horz curved.png","size=64|space=5");
zdialog_add_widget(zd,"spin","spcurhorz","hb2","-99|99|1|0");
zdialog_add_widget(zd,"hbox","hb3","dialog",0,"space=5");
zdialog_add_widget(zd,"button","grid","hb3",Bgrid,"space=10");
unbend_x1 = unbend_x2 = unbend_y1 = unbend_y2 = 0.5; // initial axes thru image middle
unbend_lin_horz = unbend_lin_vert = 0;
unbend_cur_horz = unbend_cur_vert = 0;
currgrid = 2; // use unbend grid
takeMouse(unbend_mousefunc,dragcursor); // connect mouse function
signal_thread();
zdialog_run(zd,unbend_dialog_event,"save"); // run dialog, parallel
return;
}
// dialog event and completion callback function
int unbend_dialog_event(zdialog *zd, cchar *event)
{
if (strmatch(event,"done")) zd->zstat = 1; // from edit_setup() or f_save()
if (strmatch(event,"enter")) zd->zstat = 1; // KB input
if (strmatch(event,"cancel")) zd->zstat = 2; // from f_open()
if (zd->zstat) // dialog complete
{
currgrid = 0; // restore normal grid settings 15.09
if (zd->zstat == 1)
{ // done
CEF->Fmods = 0;
if (unbend_cur_vert || unbend_cur_horz || // image3 modified
unbend_lin_vert || unbend_lin_horz) {
CEF->Fmods = 1;
CEF->Fsaved = 0; // done
}
edit_fullsize(); // get full size image
signal_thread();
edit_done(0); // commit edit
}
else edit_cancel(0); // discard edit
draw_toplines(2); // erase axes-lines
return 1;
}
if (strstr(event,"splinvert")) { // get new unbend value
zdialog_fetch(zd,"splinvert",unbend_lin_vert);
signal_thread(); // trigger thread
}
if (strstr(event,"splinhorz")) {
zdialog_fetch(zd,"splinhorz",unbend_lin_horz);
signal_thread();
}
if (strstr(event,"spcurvert")) {
zdialog_fetch(zd,"spcurvert",unbend_cur_vert);
signal_thread();
}
if (strstr(event,"spcurhorz")) {
zdialog_fetch(zd,"spcurhorz",unbend_cur_horz);
signal_thread();
}
if (strmatch(event,"grid")) m_gridlines(0,"grid 2"); // grid settings dialog
return 1;
}
// unbend mouse function // adjustable axes
void unbend_mousefunc()
{
cchar *close;
float dist1, dist2;
float mpx = 0, mpy = 0;
if (LMclick) { // left mouse click
mpx = Mxclick;
mpy = Myclick;
}
if (Mxdrag || Mydrag) { // mouse dragged
mpx = Mxdrag;
mpy = Mydrag;
}
if (! mpx && ! mpy) return;
mpx = 1.0 * mpx / E3pxm->ww; // scale mouse position 0 to 1
mpy = 1.0 * mpy / E3pxm->hh;
if (mpx < 0.2 || mpx > 0.8 ) { // check reasonable position
if (mpy < 0.1 || mpy > 0.9) return;
}
else if (mpy < 0.2 || mpy > 0.8) {
if (mpx < 0.1 || mpx > 0.9) return;
}
else return;
close = "?"; // find closest axis end-point
dist1 = 2;
dist2 = mpx * mpx + (mpy-unbend_y1) * (mpy-unbend_y1);
if (dist2 < dist1) {
dist1 = dist2;
close = "left";
}
dist2 = (1-mpx) * (1-mpx) + (mpy-unbend_y2) * (mpy-unbend_y2);
if (dist2 < dist1) {
dist1 = dist2;
close = "right";
}
dist2 = (mpx-unbend_x1) * (mpx-unbend_x1) + mpy * mpy;
if (dist2 < dist1) {
dist1 = dist2;
close = "top";
}
dist2 = (mpx-unbend_x2) * (mpx-unbend_x2) + (1-mpy) * (1-mpy);
if (dist2 < dist1) {
dist1 = dist2;
close = "bottom";
}
if (strmatch(close,"left")) unbend_y1 = mpy; // set new axis end-point
if (strmatch(close,"right")) unbend_y2 = mpy;
if (strmatch(close,"top")) unbend_x1 = mpx;
if (strmatch(close,"bottom")) unbend_x2 = mpx;
signal_thread(); // trigger thread
LMclick = Mxdrag = Mydrag = 0;
return;
}
// unbend thread function
void * unbend_thread(void *arg)
{
void * unbend_wthread(void *);
while (true)
{
thread_idle_loop(); // wait for work or exit request
unbend_hx1 = 0; // scale axes to E3ww/hh
unbend_hy1 = unbend_y1 * E3pxm->hh;
unbend_hx2 = E3pxm->ww;
unbend_hy2 = unbend_y2 * E3pxm->hh;
unbend_vx1 = unbend_x1 * E3pxm->ww;
unbend_vy1 = 0;
unbend_vx2 = unbend_x2 * E3pxm->ww;
unbend_vy2 = E3pxm->hh;
Ntoplines = 2;
toplines[0].x1 = unbend_hx1; // lines on window
toplines[0].y1 = unbend_hy1;
toplines[0].x2 = unbend_hx2;
toplines[0].y2 = unbend_hy2;
toplines[0].type = 2;
toplines[1].x1 = unbend_vx1;
toplines[1].y1 = unbend_vy1;
toplines[1].x2 = unbend_vx2;
toplines[1].y2 = unbend_vy2;
toplines[1].type = 2;
for (int ii = 0; ii < NWT; ii++) // start worker threads
start_wthread(unbend_wthread,&Nval[ii]);
wait_wthreads(); // wait for completion
Fpaint2(); // update window
}
return 0; // not executed, stop g++ warning
}
void * unbend_wthread(void *arg) // worker thread function
{
int index = *((int *) arg);
int vstat, px3, py3, cx3, cy3;
float dispx, dispy, dispx2, dispy2;
float px1, py1, vx1, vx2, hy1, hy2;
float curvert, curhorz, linvert, linhorz;
float vpix[4], *pix3;
int nc = E1pxm->nc, pcc = nc * sizeof(float);
curvert = int(unbend_cur_vert * 0.01 * E3pxm->hh); // -0.99 to +0.99
curhorz = int(unbend_cur_horz * 0.01 * E3pxm->ww);
linvert = int(unbend_lin_vert * 0.0013 * E3pxm->hh); // -0.13 to +0.13
linhorz = int(unbend_lin_horz * 0.0013 * E3pxm->ww);
vx1 = unbend_vx1;
vx2 = unbend_vx2;
hy1 = unbend_hy1;
hy2 = unbend_hy2;
for (py3 = index; py3 < E3pxm->hh; py3 += NWT) // step through F3 pixels
for (px3 = 0; px3 < E3pxm->ww; px3++)
{
cx3 = vx1 + (vx2 - vx1) * py3 / E3pxm->hh; // center of unbend
cy3 = hy1 + (hy2 - hy1) * px3 / E3pxm->ww;
dispx = 2.0 * (px3 - cx3) / E3pxm->ww; // -1.0 .. 0.0 .. +1.0 (roughly)
dispy = 2.0 * (py3 - cy3) / E3pxm->hh; // -1.0 .. 0.0 .. +1.0
dispx2 = -cosf(0.8 * dispx) + 1; // curved
dispy2 = -cosf(0.8 * dispy) + 1;
pix3 = PXMpix(E3pxm,px3,py3); // output pixel
px1 = px3; // input pixel = output
py1 = py3;
px1 += dispx * dispy * linhorz; // move input pixel
py1 += dispy * dispx * linvert;
px1 -= dispx * dispy2 * curhorz; // change sign 16.06
py1 -= dispy * dispx2 * curvert;
vstat = vpixel(E1pxm,px1,py1,vpix);
if (vstat) memcpy(pix3,vpix,pcc);
else memset(pix3,0,pcc);
}
exit_wthread();
return 0; // not executed, avoid gcc warning
}
/********************************************************************************/
// Convert a selected tetragon area into a rectangle, converting the
// rest of the image to match and keeping straight lines straight.
namespace perspective
{
int PSP_pixel[4][2]; // last 0-4 pixels clicked
int PSP_npix; // count of pixels
char PSP_pixlab[4][4] = { " A ", " B ", " C ", " D " };
int PSP_corner = 0;
editfunc EFperspective;
int dialog_event(zdialog *zd, cchar *event);
void mousefunc(void);
void warpfunc(void);
void KBfunc(int key);
}
// menu function
void m_perspective(GtkWidget *, cchar *)
{
using namespace perspective;
cchar *PSP_message = ZTX(
" Click the four corners of a tetragon area. Press [apply]. \n"
" The image is warped to make the tetragon into a rectangle.");
F1_help_topic = "fix_perspective";
EFperspective.menufunc = m_perspective;
EFperspective.funcname = "perspective";
EFperspective.Frestart = 1; // restart allowed
EFperspective.mousefunc = mousefunc; // mouse function
if (! edit_setup(EFperspective)) return; // setup edit
PXM_addalpha(E0pxm); // 15.09
PXM_addalpha(E1pxm);
PXM_addalpha(E3pxm);
PSP_npix = 0; // no pixels yet
zdialog *zd = zdialog_new(ZTX("Perspective Correction"),Mwin,Bapply,Breset,Btrim,Bdone,null);
zdialog_add_widget(zd,"label","lab1","dialog",PSP_message,"space=3");
EFperspective.zd = zd;
zdialog_run(zd,dialog_event,"save"); // run dialog, parallel
takeMouse(mousefunc,dragcursor); // connect mouse function
return;
}
// dialog completion callback function
int perspective::dialog_event(zdialog *zd, cchar *event)
{
using namespace perspective;
int ii, px, py;
int minx, maxx, miny, maxy;
if (strmatch(event,"done")) zd->zstat = 3; // from edit_setup() or f_save()
if (strmatch(event,"enter")) zd->zstat = 3; // KB input
if (strmatch(event,"cancel")) zd->zstat = 4; // from f_open()
if (strmatch(event,"focus")) // toggle mouse capture 12.01
takeMouse(mousefunc,dragcursor);
if (! zd->zstat) return 1; // wait for completion
if (zd->zstat == 1) // apply
{
erase_toptext(102); // erase points
warpfunc(); // do the warp
zd->zstat = 0; // keep dialog active
}
else if (zd->zstat == 2) // reset
{
edit_reset();
zd->zstat = 0;
for (ii = 0; ii < PSP_npix; ii++) // show pixel labels on image
{
px = PSP_pixel[ii][0];
py = PSP_pixel[ii][1];
add_toptext(102,px,py,PSP_pixlab[ii],"Sans 8");
}
Fpaint2();
}
else if (zd->zstat == 3) // trim 15.12
{
if (PSP_npix < 4) { // wait for 4 corners
zd->zstat = 0;
return 1;
}
erase_toptext(102); // erase labels
warpfunc(); // do the warp
edit_done(0);
minx = miny = 99999; // find trim limits
maxx = maxy = 0;
for (ii = 0; ii < 4; ii++) {
if (PSP_pixel[ii][0] < minx) minx = PSP_pixel[ii][0];
if (PSP_pixel[ii][0] > maxx) maxx = PSP_pixel[ii][0];
if (PSP_pixel[ii][1] < miny) miny = PSP_pixel[ii][1];
if (PSP_pixel[ii][1] > maxy) maxy = PSP_pixel[ii][1];
}
trimrect[0] = minx; // set parameters for trim function
trimrect[2] = maxx;
trimrect[1] = miny;
trimrect[3] = maxy;
m_trimrotate(0,"keep");
}
else if (zd->zstat == 4) { // done
erase_toptext(102); // erase labels
edit_done(0); // commit edit
}
else { // cancel
erase_toptext(102); // erase labels
edit_cancel(0); // discard edit
}
return 1;
}
// mouse function - click on 4 corners of tetragon
void perspective::mousefunc(void)
{
using namespace perspective;
int ii, minii, jj, px, py;
float dist, distx, disty, mindist;
if (LMclick) // left click
{
for (ii = 0; ii < PSP_npix; ii++) // check if very near a previous corner
{
if (abs(PSP_pixel[ii][0] - Mxclick) < 0.07 * E3pxm->ww
&& abs(PSP_pixel[ii][1] - Myclick) < 0.07 * E3pxm->hh)
{
PSP_pixel[ii][0] = Mxclick; // yes, set new corner position
PSP_pixel[ii][1] = Myclick;
PSP_corner = ii;
goto showcorners;
}
}
if (PSP_npix < 4) // if < 4 corners, add a new one
{
ii = PSP_npix; // next corner to fill
PSP_pixel[ii][0] = Mxclick; // save newest corner position
PSP_pixel[ii][1] = Myclick;
PSP_corner = ii;
PSP_npix++;
goto showcorners;
}
mindist = 99999; // all 4 corners already specified
minii = -1;
for (ii = 0; ii < 4; ii++) // find closest corner to click position
{
distx = (Mxclick - PSP_pixel[ii][0]);
disty = (Myclick - PSP_pixel[ii][1]);
dist = sqrt(distx*distx + disty*disty);
if (dist < mindist) {
mindist = dist;
minii = ii;
}
}
if (minii >= 0) { // set new corner position
ii = minii;
PSP_pixel[ii][0] = Mxclick;
PSP_pixel[ii][1] = Myclick;
PSP_corner = ii;
goto showcorners;
}
}
else if (RMclick) // right click
{
mindist = 99999;
minii = -1;
for (ii = 0; ii < PSP_npix; ii++) // find closest corner to click position
{
distx = (Mxclick - PSP_pixel[ii][0]);
disty = (Myclick - PSP_pixel[ii][1]);
dist = sqrt(distx*distx + disty*disty);
if (dist < mindist) {
mindist = dist;
minii = ii;
}
}
if (minii >= 0) { // replace deleted corner with
ii = minii; // last corner
jj = PSP_npix - 1;
PSP_pixel[ii][0] = PSP_pixel[jj][0];
PSP_pixel[ii][1] = PSP_pixel[jj][1];
PSP_corner = ii;
--PSP_npix; // reduce corner count
goto showcorners;
}
}
showcorners: // show corner labels on image
erase_toptext(102);
for (ii = 0; ii < PSP_npix; ii++)
{
px = PSP_pixel[ii][0];
py = PSP_pixel[ii][1];
add_toptext(102,px,py,PSP_pixlab[ii],"Sans 8");
}
LMclick = RMclick = 0;
Fpaint2();
return;
}
// Keyboard function
// KB arrow keys tweak position of last touched tetragon corner.
void perspective::KBfunc(int key)
{
using namespace perspective;
int ii, xstep, ystep;
xstep = ystep = 0;
if (key == GDK_KEY_Left) xstep = -1;
if (key == GDK_KEY_Right) xstep = +1;
if (key == GDK_KEY_Up) ystep = -1;
if (key == GDK_KEY_Down) ystep = +1;
ii = PSP_corner;
if (ii < 0 || ii > 3) return; // last corner touched, 0-3
PSP_pixel[ii][0] += xstep;
PSP_pixel[ii][1] += ystep;
mousefunc();
return;
}
// perspective warp function - make input tetragon into a rectangle
void perspective::warpfunc(void)
{
using namespace perspective;
int ii, jj, tempx, tempy, vstat;
float px3, py3, trpx[4], trpy[4];
float sqpx0, sqpy0, sqpx1, sqpy1, sqpx2, sqpy2, sqpx3, sqpy3;
float cdx0, cdy0, cdx1, cdy1, cdx2, cdy2, cdx3, cdy3;
float px1, py1, dispx, dispy, sqww, sqhh;
float f0, f1, f2, f3;
float vpix1[4], *pix3;
int nc = E1pxm->nc, pcc = nc * sizeof(float);
if (PSP_npix != 4) {
zmessageACK(Mwin,ZTX("must have 4 corners"));
return;
}
for (ii = 0; ii < 4; ii++) { // get 4 selected tetragon points
trpx[ii] = PSP_pixel[ii][0];
trpy[ii] = PSP_pixel[ii][1];
}
// sort 4 points in clockwise order NW, NE, SE, SW
for (ii = 0; ii < 4; ii++) { // sort top to bottom (y order)
for (jj = ii; jj < 4; jj++) {
if (trpy[jj] < trpy[ii]) {
tempx = trpx[ii];
tempy = trpy[ii];
trpx[ii] = trpx[jj];
trpy[ii] = trpy[jj];
trpx[jj] = tempx;
trpy[jj] = tempy;
}
}
}
if (trpx[1] < trpx[0]) { // sort upper two left, right
tempx = trpx[0];
tempy = trpy[0];
trpx[0] = trpx[1];
trpy[0] = trpy[1];
trpx[1] = tempx;
trpy[1] = tempy;
}
if (trpx[2] < trpx[3]) { // sort lower two right, left
tempx = trpx[2];
tempy = trpy[2];
trpx[2] = trpx[3];
trpy[2] = trpy[3];
trpx[3] = tempx;
trpy[3] = tempy;
}
if (trpx[0] < trpx[3]) sqpx0 = sqpx3 = trpx[0]; // rectangle enclosing tetragon
else sqpx0 = sqpx3 = trpx[3];
if (trpx[1] > trpx[2]) sqpx1 = sqpx2 = trpx[1];
else sqpx1 = sqpx2 = trpx[2];
if (trpy[0] < trpy[1]) sqpy0 = sqpy1 = trpy[0];
else sqpy0 = sqpy1 = trpy[1];
if (trpy[2] > trpy[3]) sqpy2 = sqpy3 = trpy[2];
else sqpy2 = sqpy3 = trpy[3];
/***
sqpx0 = sqpx3 = 0.5 * (trpx[0] + trpx[3]); // rectangle bisecting tetragon sides
sqpx1 = sqpx2 = 0.5 * (trpx[1] + trpx[2]);
sqpy0 = sqpy1 = 0.5 * (trpy[0] + trpy[1]);
sqpy2 = sqpy3 = 0.5 * (trpy[2] + trpy[3]);
***/
cdx0 = sqpx0 - trpx[0]; // displavement of tetragon corner
cdy0 = sqpy0 - trpy[0]; // to corresponding rectangle corner
cdx1 = sqpx1 - trpx[1];
cdy1 = sqpy1 - trpy[1];
cdx2 = sqpx2 - trpx[2];
cdy2 = sqpy2 - trpy[2];
cdx3 = sqpx3 - trpx[3];
cdy3 = sqpy3 - trpy[3];
sqww = 1.0 / (sqpx1 - sqpx0); // rectangle width and height
sqhh = 1.0 / (sqpy3 - sqpy0);
for (py3 = 0; py3 < E3pxm->hh; py3++) // loop all output pixels
for (px3 = 0; px3 < E3pxm->ww; px3++)
{
f0 = (1.0 - (px3 - sqpx0) * sqww) * (1.0 - (py3 - sqpy0) * sqhh);
f1 = (px3 - sqpx0) * sqww * (1.0 - (py3 - sqpy0) * sqhh);
f2 = (px3 - sqpx0) * sqww * (py3 - sqpy0) * sqhh;
f3 = (1.0 - (px3 - sqpx0) * sqww) * (py3 - sqpy0) * sqhh;
dispx = cdx0 * f0 + cdx1 * f1 + cdx2 * f2 + cdx3 * f3;
dispy = cdy0 * f0 + cdy1 * f1 + cdy2 * f2 + cdy3 * f3;
px1 = px3 - dispx; // input virtual pixel for px3/py3
py1 = py3 - dispy;
pix3 = PXMpix(E3pxm,int(px3),int(py3)); // output pixel
vstat = vpixel(E1pxm,px1,py1,vpix1); // output pixel = input virtual pixel
if (vstat) memcpy(pix3,vpix1,pcc);
else memset(pix3,0,pcc); // voided pixel
}
CEF->Fmods++; // image is modified
CEF->Fsaved = 0;
Fpaint2(); // update window
return;
}
/********************************************************************************/
// warp/distort area - select image area and pull with mouse
float *WarpAx, *WarpAy; // memory for pixel warp vectors
int WarpAcc, WarpAnew;
int WarpA_started;
int WarpA_areanumber;
editfunc EFwarpA;
void WarpA_init();
void WarpA_warpfunc(float mdx, float mdy, float mdw, float mdh, int acc);
void WarpA_warpfunc2(float mdx, float mdy, float mdw, float mdh, int acc);
void WarpA_mousefunc();
void WarpA_expand();
void WarpA_edgeblend();
// menu function
void m_warp_area(GtkWidget *, cchar *)
{
int WarpA_dialog_event(zdialog *zd, cchar *event);
cchar *WarpA_message = ZTX(
" Select an area to warp using select area function. \n"
" Press [start warp] and pull area with mouse. \n"
" Make multiple mouse pulls until satisfied. \n"
" When finished, select another area or press [done].");
F1_help_topic = "warp_area";
EFwarpA.menufunc = m_warp_area;
EFwarpA.funcname = "warp-area";
EFwarpA.Farea = 2; // select area usable
EFwarpA.mousefunc = WarpA_mousefunc; // mouse function
if (! edit_setup(EFwarpA)) return; // setup edit
zdialog *zd = zdialog_new(ZTX("Warp area"),Mwin,Bdone,Bcancel,null);
EFwarpA.zd = zd;
zdialog_add_widget(zd,"label","lab1","dialog",WarpA_message,"space=3");
zdialog_add_widget(zd,"hbox","hb1","dialog",0,"space=5");
zdialog_add_widget(zd,"button","start","hb1",ZTX("start warp"),"space=5");
zdialog_add_widget(zd,"button","reset","hb1",Breset,"space=5");
WarpAcc = E3pxm->ww * E3pxm->hh * sizeof(float);
WarpAx = (float *) zmalloc(WarpAcc); // get memory for pixel warp vectors
WarpAy = (float *) zmalloc(WarpAcc);
memset(WarpAx,0,WarpAcc);
memset(WarpAy,0,WarpAcc);
WarpA_started = 0;
WarpA_areanumber = 0;
WarpAnew = 0;
zdialog_run(zd,WarpA_dialog_event,"save"); // run dialog, parallel
return;
}
// dialog event and completion callback function
int WarpA_dialog_event(zdialog * zd, cchar *event)
{
int init = 0;
if (strmatch(event,"done")) zd->zstat = 1; // from edit_setup() or f_save()
if (strmatch(event,"enter")) zd->zstat = 1; // KB input
if (strmatch(event,"cancel")) zd->zstat = 2; // from f_open()
if (zd->zstat)
{
if (zd->zstat == 1) edit_done(0); // commit edit
else edit_cancel(0); // discard edit
zfree(WarpAx); // release memory
zfree(WarpAy);
return 1;
}
if (! sa_validate()) init = 1; // area invalid for curr. image file
if (sa_stat != 3 || sa_mode == mode_image) // no select area active
init = 1;
if (WarpA_started && WarpA_areanumber != areanumber) // select area changed
init = 1;
if (init) {
memset(WarpAx,0,WarpAcc);
memset(WarpAy,0,WarpAcc);
WarpA_started = 0;
WarpA_areanumber = 0;
return 1;
}
if (strmatch(event,"start")) // start warp
{
if (sa_stat != 3 || sa_mode == mode_image) { // no select area active
zmessageACK(Mwin,ZTX("no active Select Area"));
return 1;
}
sa_edgecalc(); // calculate area edge distances
takeMouse(WarpA_mousefunc,dragcursor); // connect mouse function
WarpA_started = 1;
WarpA_areanumber = areanumber;
}
if (strmatch(event,"focus")) // toggle mouse capture
takeMouse(WarpA_mousefunc,dragcursor);
if (strmatch(event,"reset")) { // undo all warps
edit_reset();
memset(WarpAx,0,WarpAcc); // clear warp vectors
memset(WarpAy,0,WarpAcc);
}
if (strmatch(event,"blendwidth"))
WarpA_edgeblend();
return 1;
}
// warp mouse function
void WarpA_mousefunc()
{
static float mdx, mdy, mdw, mdh;
static int warped = 0;
if (! WarpA_started) return;
if (Mxdrag || Mydrag) // mouse drag underway
{
mdx = Mxdown; // drag origin, image coordinates
mdy = Mydown;
mdw = Mxdrag - Mxdown; // drag increment
mdh = Mydrag - Mydown;
WarpA_warpfunc(mdx,mdy,mdw,mdh,0); // warp image
warped = 1;
Mxdrag = Mydrag = 0;
return;
}
else if (warped)
{
warped = 0;
WarpA_warpfunc(mdx,mdy,mdw,mdh,1); // drag done, update warp vectors
if (WarpAnew) WarpA_expand();
}
return;
}
// warp image and accumulate warp memory
// mdx/y = mouse initial position
// mdw/h = mouse drag vector
void WarpA_warpfunc(float mdx, float mdy, float mdw, float mdh, int acc)
{
int ii, px, py, ww, hh, vstat;
float ddx, ddy, dpe, dpm1, dpm2, dpm;
float mag, dispx, dispy;
float vpix[4], *pix3;
int nc = E1pxm->nc, pcc = nc * sizeof(float);
if (sa_stat != 3) return; // area erased
ww = E1pxm->ww;
hh = E1pxm->hh;
ii = mdy * ww + mdx;
if (! sa_pixmap[ii]) { // if mouse drag outside select area
WarpA_warpfunc2(mdx,mdy,mdw,mdh,acc); // then use alternate function
return;
}
for (py = sa_miny; py < sa_maxy; py++) // loop all pixels in area
for (px = sa_minx; px < sa_maxx; px++)
{
ii = py * ww + px;
dpe = sa_pixmap[ii]; // distance from area edge
if (dpe < 1) continue; // outside area
dpe -= 1; // rebase edge = 0
ddx = (px - mdx); // pixel distance to mouse position
ddy = (py - mdy); // before drag
dpm1 = sqrt(ddx*ddx + ddy*ddy);
ddx -= mdw; // pixel distance to mouse position
ddy -= mdh; // after drag
dpm2 = sqrt(ddx*ddx + ddy*ddy);
dpm = 0.5 * (dpm1 + dpm2); // mean
mag = (dpe / (dpm + dpe)); // 1...0 for pixel at mouse...edge
dispx = -mdw * mag; // pixel movement from drag movement
dispy = -mdh * mag;
dispx += WarpAx[ii]; // add this warp to prior
dispy += WarpAy[ii];
if (acc) { // mouse drag done,
WarpAx[ii] = dispx; // accumulate warp memory
WarpAy[ii] = dispy;
continue;
}
vstat = vpixel(E1pxm,px+dispx,py+dispy,vpix); // input virtual pixel
if (vstat) {
pix3 = PXMpix(E3pxm,px,py); // output pixel
memcpy(pix3,vpix,pcc);
}
}
ww = sa_maxx - sa_minx; // update window
hh = sa_maxy - sa_miny;
Fpaint3(sa_minx,sa_miny,ww,hh);
CEF->Fmods++;
CEF->Fsaved = 0;
return;
}
// warp function when mouse is dragged outside select area
void WarpA_warpfunc2(float mdx, float mdy, float mdw, float mdh, int acc)
{
int ii, jj, px, py, dpx, dpy, dpe;
int xlo, xhi, ylo, yhi;
int ww, hh, vstat;
float ddx, ddy, dpm1, dpm2, dpm;
float mag, dispx, dispy;
float mdmax, mdmax2;
float vpix[4], *pix1, *pix3;
int nc = E1pxm->nc, pcc = nc * sizeof(float);
#define distance(px1,py1,px2,py2) \
sqrtf((px2-px1)*(px2-px1)+(py2-py1)*(py2-py1))
WarpAnew = 1; // note select area will expand
ww = E1pxm->ww;
hh = E1pxm->hh;
ylo = sa_miny; // select area enclosing rectangle
yhi = sa_maxy;
xlo = sa_minx;
xhi = sa_maxx;
if (mdy < ylo) ylo = mdy - 200; // extend area to mouse posn + more
if (mdy > yhi) yhi = mdy + 200;
if (mdx < xlo) xlo = mdx - 200;
if (mdx > xhi) xhi = mdx + 200;
if (ylo < 0) ylo = 0;
if (yhi > hh) yhi = hh;
if (xlo < 0) xlo = 0;
if (xhi > ww) xhi = ww;
mdmax = distance(mdx,mdy,xlo,ylo); // find max. mouse distance from
mdmax2 = distance(mdx,mdy,xhi,ylo); // enclosing rectangle corners
if (mdmax2 > mdmax) mdmax = mdmax2;
mdmax2 = distance(mdx,mdy,xhi,yhi);
if (mdmax2 > mdmax) mdmax = mdmax2;
mdmax2 = distance(mdx,mdy,xlo,yhi);
if (mdmax2 > mdmax) mdmax = mdmax2;
for (py = ylo; py < yhi; py++) // loop all pixels in enclosing rectangle
for (px = xlo; px < xhi; px++)
{
ddx = (px - mdx); // pixel distance to mouse initial position
ddy = (py - mdy);
dpm1 = sqrt(ddx*ddx + ddy*ddy);
ddx -= mdw; // pixel distance to mouse curr. position
ddy -= mdh;
dpm2 = sqrt(ddx*ddx + ddy*ddy);
dpm = 0.5 * (dpm1 + dpm2); // mean
mag = 1.0 - dpm / mdmax; // 1...0 for pixel-mouse 0...mdmax
dispx = -mdw * mag;
dispy = -mdh * mag; // pixel movement from drag movement
ii = py * ww + px;
dispx += WarpAx[ii]; // add this warp to prior
dispy += WarpAy[ii];
dpx = px + dispx; // source virtual pixel
dpy = py + dispy; // (nearest real pixel)
if (dpx < 0) dpx = 0;
if (dpx > ww-1) dpx = ww-1;
if (dpy < 0) dpy = 0;
if (dpy > hh-1) dpy = hh-1;
jj = dpy * ww + dpx;
dpe = sa_pixmap[jj]; // distance from area edge
if (dpe < 1) // outside area
dispx = dispy = 0;
if (acc) { // mouse drag done,
WarpAx[ii] = dispx; // accumulate warp memory
WarpAy[ii] = dispy;
continue;
}
if (dpe) { // source pixel inside area
vstat = vpixel(E1pxm,px+dispx,py+dispy,vpix); // = input virtual pixel
if (vstat) {
pix3 = PXMpix(E3pxm,px,py); // output pixel
memcpy(pix3,vpix,pcc);
continue;
}
}
pix1 = PXMpix(E1pxm,px,py); // pixel is unchanged
pix3 = PXMpix(E3pxm,px,py);
memcpy(pix3,pix1,pcc);
}
ww = xhi - xlo; // update window
hh = yhi - ylo;
Fpaint3(xlo,ylo,ww,hh);
CEF->Fmods++;
CEF->Fsaved = 0;
return;
}
// expand select area if pulled outside the original bounds
void WarpA_expand()
{
int ii, px, py, ww, hh;
int blend = sa_blend;
ww = E1pxm->ww;
hh = E1pxm->hh;
for (py = 0; py < hh; py++) // loop all pixels
for (px = 0; px < ww; px++)
{
ii = py * ww + px; // find pixels that have moved
if (! WarpAx[ii] && ! WarpAy[ii]) continue;
sa_pixmap[ii] = 1; // mark within area
}
sa_map_pixels();
sa_finish_auto();
sa_edgecalc(); // recalculate edge distances
sa_blend = blend;
WarpA_edgeblend();
WarpA_areanumber = areanumber;
WarpAnew = 0;
return;
}
// blend area edges according to sa_blend
void WarpA_edgeblend()
{
int ii, px, py, ww, hh, dist, vstat;
float *pix1, *pix3, vpix[4];
float dold, dnew, dispx, dispy;
if (! sa_blend) return;
ww = E1pxm->ww;
hh = E1pxm->hh;
for (py = 0; py < hh; py++) // loop output pixels
for (px = 0; px < ww; px++)
{
ii = py * ww + px; // blend changes at edge
dist = sa_pixmap[ii];
if (! dist) continue; // outside area
if (dist > sa_blend) continue; // beyond blend area
dispx = WarpAx[ii]; // warp
dispy = WarpAy[ii];
vstat = vpixel(E1pxm,px+dispx,py+dispy,vpix); // input virtual pixel (new)
if (! vstat) continue;
pix1 = PXMpix(E1pxm,px,py); // input pixel (old)
pix3 = PXMpix(E3pxm,px,py); // output pixel
dnew = sa_blendfunc(dist); // 16.08
dold = 1.0 - dnew;
pix3[0] = dnew * vpix[0] + dold * pix1[0];
pix3[1] = dnew * vpix[1] + dold * pix1[1];
pix3[2] = dnew * vpix[2] + dold * pix1[2];
}
ww = sa_maxx - sa_minx; // update window
hh = sa_maxy - sa_miny;
Fpaint3(sa_minx,sa_miny,ww,hh);
return;
}
/********************************************************************************/
// Unwarp closeup face photo - shrink magnified areas closest to camera
int unwarpCU_started;
int unwarpCU_areanumber;
float unwarpCU_warpval;
int unwarpCU_cx, unwarpCU_cy;
editfunc EFunwarpCU;
int unwarpCU_dialog_event(zdialog *zd, cchar *event);
void unwarpCU_warpfunc();
void unwarpCU_mousefunc();
// menu function
void m_unwarp_closeup(GtkWidget *, cchar *) // 16.11
{
cchar *unwarpCU_message = ZTX(
" Use Select Area to select a face. \n"
" Click on the tip of the nose. \n"
" Move the slider. \n");
F1_help_topic = "unwarp_closeup";
EFunwarpCU.menufunc = m_unwarp_closeup;
EFunwarpCU.funcname = "unwarp-closeup";
EFunwarpCU.Farea = 2; // select area usable
EFunwarpCU.mousefunc = unwarpCU_mousefunc; // mouse function
if (! edit_setup(EFunwarpCU)) return; // setup edit
/***
______________________________________
| Unwarp Closeup |
| |
| Use Select Area to select a face. |
| Click on the tip of the nose. |
| Move the slider. |
| |
| [===============[]================] |
| |
| [done] [cancel] |
|______________________________________|
***/
zdialog *zd = zdialog_new(ZTX("Unwarp Closeup"),Mwin,Bdone,Bcancel,null);
EFunwarpCU.zd = zd;
zdialog_add_widget(zd,"label","lab1","dialog",unwarpCU_message,"space=3");
zdialog_add_widget(zd,"hbox","hbw","dialog",0,"space=5");
zdialog_add_widget(zd,"hscale","warpval","hbw","0.0|1.0|0.01|0.0","space=5|expand");
takeMouse(unwarpCU_mousefunc,dragcursor); // connect mouse function
unwarpCU_started = 0;
unwarpCU_areanumber = 0;
zdialog_run(zd,unwarpCU_dialog_event,"save"); // run dialog, parallel
return;
}
// dialog event and completion callback function
int unwarpCU_dialog_event(zdialog * zd, cchar *event)
{
int init = 0;
if (strmatch(event,"done")) zd->zstat = 1; // from edit_setup() or f_save()
if (strmatch(event,"enter")) zd->zstat = 1; // KB input
if (strmatch(event,"cancel")) zd->zstat = 2; // from f_open()
if (zd->zstat)
{
if (zd->zstat == 1) edit_done(0); // commit edit
else edit_cancel(0); // discard edit
return 1;
}
if (! sa_validate()) init = 1; // area invalid for curr. image file
if (sa_stat != 3 || sa_mode == mode_image) // no select area active
init = 1;
if (unwarpCU_started && unwarpCU_areanumber != areanumber) // select area changed
init = 1;
if (init) {
unwarpCU_started = 0;
unwarpCU_areanumber = 0;
return 1;
}
if (strmatch(event,"focus")) // reconnect mouse
takeMouse(unwarpCU_mousefunc,dragcursor);
if (strmatch(event,"warpval")) { // slider movement
zdialog_fetch(zd,"warpval",unwarpCU_warpval);
unwarpCU_warpfunc();
}
return 1;
}
// mouse function
void unwarpCU_mousefunc()
{
if (LMclick) {
unwarpCU_cx = Mxclick; // capture central point
unwarpCU_cy = Myclick;
}
else if (Mxdrag || Mydrag) {
unwarpCU_cx = Mxdrag;
unwarpCU_cy = Mydrag;
}
else return;
LMclick = Mxdrag = Mydrag = 0;
if (sa_stat != 3 || sa_mode == mode_image) { // no select area active
zmessageACK(Mwin,ZTX("no active Select Area"));
unwarpCU_started = 0;
Mdrag = 0;
return;
}
unwarpCU_started = 1; // unwarp can proceed
unwarpCU_areanumber = areanumber;
sa_edgecalc(); // calculate area edge distances
unwarpCU_warpfunc();
return;
}
// warp image according to slider position
void unwarpCU_warpfunc()
{
int ii, px, py, ww, vstat;
float hsize, vsize, hpos, vpos;
float warpval = unwarpCU_warpval;
float ed, cx, cy, dx, dy;
float vpix[4], *pix3;
int nc = E1pxm->nc, pcc = nc * sizeof(float);
if (sa_stat != 3) return; // area erased
hsize = sa_maxx - sa_minx; // area size
vsize = sa_maxy - sa_miny;
cx = unwarpCU_cx; // unwarp center (nose tip)
cy = unwarpCU_cy;
ww = E1pxm->ww;
for (py = sa_miny; py < sa_maxy; py++) // loop all pixels in area
for (px = sa_minx; px < sa_maxx; px++)
{
ii = py * ww + px;
if (sa_pixmap[ii] < 1) continue; // pixel outside area
hpos = 2.0 * (px - cx) / hsize; // horizontal pixel position, -1 ... +1
vpos = 2.0 * (py - cy) / vsize; // vertical pixel position, -1 ... +1
ed = sa_pixmap[ii]; // pixel edge distance
hpos = hpos * ed / hsize; // scale pixel position
vpos = vpos * ed / vsize;
dx = sinf(PI * hpos); // pixel displacement, -1 ... +1
dy = sinf(PI * vpos);
dx = dx * 0.1 * warpval * hsize; // pixel displacement, -10% ... +10%
dy = dy * 0.1 * warpval * vsize;
vstat = vpixel(E1pxm,px+dx,py+dy,vpix); // input virtual pixel
if (vstat) {
pix3 = PXMpix(E3pxm,px,py); // output pixel
memcpy(pix3,vpix,pcc);
}
}
Fpaint3(sa_minx,sa_miny,hsize,vsize);
CEF->Fmods++;
CEF->Fsaved = 0;
return;
}
/********************************************************************************/
// warp/distort whole image with a curved transform
// fix perspective problems (e.g. curved walls, leaning buildings)
namespace warpC_names
{
float *WarpCx, *WarpCy; // memory of all dragged pixels
float WarpCmem[5][100]; // undo memory, last 100 drags
int NWarpC; // WarpCmem count
int WarpCdrag;
int E3ww, E3hh;
float $mdx, $mdy, $mdw, $mdh; // warpC_warpfunc() and warpC_wthread()
float $D, $span; // use these $ args
int $acc;
editfunc EFwarpC;
int WarpC_dialog_event(zdialog *zd, cchar *event);
void WarpC_warpfunc();
void WarpC_mousefunc(void);
void * WarpC_wthread(void *arg);
}
// menu function
void m_warp_curved(GtkWidget *, cchar *)
{
using namespace warpC_names;
cchar *WarpC_message = ZTX(
" Pull an image position using the mouse. \n"
" Make multiple mouse pulls until satisfied. \n"
" When finished, press [done].");
int px, py, ii;
F1_help_topic = "warp_curved";
EFwarpC.menufunc = m_warp_curved;
EFwarpC.funcname = "warp-curved";
EFwarpC.FprevReq = 1; // use preview
EFwarpC.mousefunc = WarpC_mousefunc; // mouse function
if (! edit_setup(EFwarpC)) return; // setup edit
PXM_addalpha(E0pxm); // 15.09
PXM_addalpha(E1pxm);
PXM_addalpha(E3pxm);
zdialog *zd = zdialog_new(ZTX("Warp curved"),Mwin,Bdone,Bcancel,null);
EFwarpC.zd = zd;
zdialog_add_widget(zd,"label","lab1","dialog",WarpC_message,"space=3");
zdialog_add_widget(zd,"hbox","hb1","dialog",0,"space=8");
zdialog_add_widget(zd,"button","undolast","hb1",Bundolast,"space=8");
zdialog_add_widget(zd,"button","undoall","hb1",Bundoall,"space=2");
zdialog_add_widget(zd,"button","grid","hb1",Bgrid,"space=15");
zdialog_add_widget(zd,"hbox","hb2","dialog",0,"space=4");
zdialog_add_widget(zd,"label","lab2","hb2",ZTX("warp span"),"space=8");
zdialog_add_widget(zd,"spin","span","hb2","0.00|1.0|0.01|0.1","space=1");
currgrid = 3; // use warp-curved grid
NWarpC = WarpCdrag = 0; // no drag data
int cc = E3pxm->ww * E3pxm->hh * sizeof(float);
WarpCx = (float *) zmalloc(cc); // get memory for pixel displacements
WarpCy = (float *) zmalloc(cc);
for (py = 0; py < E3pxm->hh; py++) // no pixel displacements
for (px = 0; px < E3pxm->ww; px++)
{
ii = py * E3pxm->ww + px;
WarpCx[ii] = WarpCy[ii] = 0.0;
}
E3ww = E3pxm->ww; // preview dimensions
E3hh = E3pxm->hh;
zdialog_restore_inputs(zd); // restore previous inputs
zdialog_fetch(zd,"span",$span); // save span value
zdialog_run(zd,WarpC_dialog_event,"save"); // run dialog, parallel
takeMouse(WarpC_mousefunc,dragcursor); // connect mouse function
return;
}
// dialog event and completion callback function
int warpC_names::WarpC_dialog_event(zdialog * zd, cchar *event)
{
using namespace warpC_names;
int px, py, ii;
int fpx, fpy, epx, epy, vstat;
float scale, dispx, dispy;
float vpix[4], *pix3;
int nc = E1pxm->nc, pcc = nc * sizeof(float);
if (strmatch(event,"done")) zd->zstat = 1; // from edit_setup() or f_save()
if (strmatch(event,"enter")) zd->zstat = 1; // KB input
if (strmatch(event,"cancel")) zd->zstat = 2; // from f_open()
if (zd->zstat) goto complete;
if (strmatch(event,"undolast"))
{
if (NWarpC == 1) event = "undoall";
else if (NWarpC) { // undo most recent drag
ii = --NWarpC;
$mdx = WarpCmem[0][ii];
$mdy = WarpCmem[1][ii];
$mdw = -WarpCmem[2][ii];
$mdh = -WarpCmem[3][ii];
$span = WarpCmem[4][ii];
zdialog_stuff(zd,"span",$span);
$acc = 0;
WarpC_warpfunc(); // undrag image
$acc = 1;
WarpC_warpfunc(); // undrag memory
}
}
if (strmatch(event,"undoall")) // undo all drags
{
NWarpC = 0; // erase undo memory
for (py = 0; py < E3pxm->hh; py++) // reset pixel displacements
for (px = 0; px < E3pxm->ww; px++)
{
ii = py * E3pxm->ww + px;
WarpCx[ii] = WarpCy[ii] = 0.0;
}
edit_reset(); // restore image 1
}
if (strmatch(event,"grid")) m_gridlines(0,"grid 3"); // grid settings dialog
if (strmatch(event,"span"))
zdialog_fetch(zd,"span",$span);
return 1;
complete:
currgrid = 0; // restore normal grid settings
if (zd->zstat)
{
if (NWarpC == 0) zd->zstat = 2; // no warps, cancel
if (zd->zstat == 1) // done
{
edit_fullsize(); // get full size image
scale = 1.0 * (E3pxm->ww + E3pxm->hh) / (E3ww + E3hh);
for (fpy = 0; fpy < E3pxm->hh; fpy++) // scale net pixel displacements
for (fpx = 0; fpx < E3pxm->ww; fpx++) // to full image size
{
epx = E3ww * fpx / E3pxm->ww;
epy = E3hh * fpy / E3pxm->hh;
ii = epy * E3ww + epx;
dispx = WarpCx[ii] * scale;
dispy = WarpCy[ii] * scale;
vstat = vpixel(E1pxm,fpx+dispx,fpy+dispy,vpix); // input virtual pixel
pix3 = PXMpix(E3pxm,fpx,fpy); // output pixel
if (vstat) memcpy(pix3,vpix,pcc);
else memset(pix3,0,pcc); // voided pixel
}
signal_thread();
edit_done(0); // commit edit
}
else edit_cancel(0); // discard edit
zfree(WarpCx); // release memory
zfree(WarpCy);
}
return 1;
}
// WarpC mouse function
void warpC_names::WarpC_mousefunc(void)
{
using namespace warpC_names;
int ii;
if (Mxdrag || Mydrag) // mouse drag underway
{
$mdx = Mxdown; // drag origin
$mdy = Mydown;
$mdw = Mxdrag - Mxdown; // drag increment
$mdh = Mydrag - Mydown;
$acc = 0;
WarpC_warpfunc(); // drag image
WarpCdrag = 1;
Mxdrag = Mydrag = 0;
return;
}
else if (WarpCdrag)
{
WarpCdrag = 0;
$acc = 1;
WarpC_warpfunc(); // drag done, add to memory
if (NWarpC == 100) // if full, throw away oldest
{
NWarpC = 99;
for (ii = 0; ii < NWarpC; ii++)
{
WarpCmem[0][ii] = WarpCmem[0][ii+1];
WarpCmem[1][ii] = WarpCmem[1][ii+1];
WarpCmem[2][ii] = WarpCmem[2][ii+1];
WarpCmem[3][ii] = WarpCmem[3][ii+1];
WarpCmem[4][ii] = WarpCmem[4][ii+1];
}
}
ii = NWarpC;
WarpCmem[0][ii] = $mdx; // save drag for undo
WarpCmem[1][ii] = $mdy;
WarpCmem[2][ii] = $mdw;
WarpCmem[3][ii] = $mdh;
WarpCmem[4][ii] = $span;
NWarpC++;
}
return;
}
// warp image and accumulate warp memory
// mouse at (mx,my) is moved (mw,mh) pixels
void warpC_names::WarpC_warpfunc()
{
using namespace warpC_names;
float D, d1, d2, d3, d4;
d1 = ($mdx-0) * ($mdx-0) + ($mdy-0) * ($mdy-0); // distance, mouse to 4 corners
d2 = (E3pxm->ww-$mdx) * (E3pxm->ww-$mdx) + ($mdy-0) * ($mdy-0);
d3 = (E3pxm->ww-$mdx) * (E3pxm->ww-$mdx) + (E3pxm->hh-$mdy) * (E3pxm->hh-$mdy);
d4 = ($mdx-0) * ($mdx-0) + (E3pxm->hh-$mdy) * (E3pxm->hh-$mdy);
D = d1;
if (d2 > D) D = d2; // find greatest corner distance
if (d3 > D) D = d3;
if (d4 > D) D = d4;
$D = D * $span;
for (int ii = 0; ii < NWT; ii++) // start worker threads
start_wthread(WarpC_wthread,&Nval[ii]);
wait_wthreads(); // wait for completion
CEF->Fmods++;
CEF->Fsaved = 0;
Fpaint2(); // update window
return;
}
// working thread to process the pixels
void * warpC_names::WarpC_wthread(void *arg)
{
using namespace warpC_names;
int index = *((int *) arg);
int ii, px, py, vstat;
float d, mag, dispx, dispy;
float vpix[4], *pix3;
int nc = E1pxm->nc, pcc = nc * sizeof(float);
for (py = index; py < E3pxm->hh; py += NWT) // process all pixels
for (px = 0; px < E3pxm->ww; px++)
{
d = (px-$mdx)*(px-$mdx) + (py-$mdy)*(py-$mdy);
mag = (1.0 - d / $D);
if (mag < 0) continue;
mag = mag * mag; // faster than pow(mag,4);
mag = mag * mag;
dispx = -$mdw * mag; // displacement = drag * mag
dispy = -$mdh * mag;
ii = py * E3pxm->ww + px;
if ($acc) { // drag done, accumulate drag sum
WarpCx[ii] += dispx;
WarpCy[ii] += dispy;
continue;
}
dispx += WarpCx[ii]; // add this drag to prior sum
dispy += WarpCy[ii];
vstat = vpixel(E1pxm,px+dispx,py+dispy,vpix); // input virtual pixel
pix3 = PXMpix(E3pxm,px,py); // output pixel
if (vstat) memcpy(pix3,vpix,pcc);
else memset(pix3,0,pcc); // voided pixel
}
exit_wthread(); // exit thread
return 0; // not executed, avoid gcc warning
}
/********************************************************************************/
// warp/distort whole image with a linear transform (almost)
// fix perspective problems (e.g. leaning buildings)
namespace warpL_names
{
float *WarpLx, *WarpLy; // memory of all dragged pixels
float WarpLmem[4][100]; // undo memory, last 100 drags
int NWarpL; // WarpLmem count
int WarpLdrag;
int E3ww, E3hh;
float $mdx, $mdy, $mdw, $mdh; // warpL_warpfunc() and warpL_wthread()
int $D, $Dx, $Dy, $acc; // use these $ args
editfunc EFwarpL;
int WarpL_dialog_event(zdialog *zd, cchar *event);
void WarpL_mousefunc(void);
void WarpL_warpfunc();
void * WarpL_wthread(void *arg);
}
// menu function
void m_warp_linear(GtkWidget *, cchar *)
{
using namespace warpL_names;
cchar *WarpL_message = ZTX(
" Pull an image position using the mouse. \n"
" Make multiple mouse pulls until satisfied. \n"
" When finished, press [done].");
int px, py, ii;
F1_help_topic = "warp_linear";
EFwarpL.menufunc = m_warp_linear;
EFwarpL.funcname = "warp-linear";
EFwarpL.FprevReq = 1; // use preview
EFwarpL.mousefunc = WarpL_mousefunc; // mouse function
if (! edit_setup(EFwarpL)) return; // setup edit
PXM_addalpha(E0pxm); // 15.09
PXM_addalpha(E1pxm);
PXM_addalpha(E3pxm);
zdialog *zd = zdialog_new(ZTX("Warp linear"),Mwin,Bdone,Bcancel,null);
EFwarpL.zd = zd;
zdialog_add_widget(zd,"label","lab1","dialog",WarpL_message,"space=3");
zdialog_add_widget(zd,"hbox","hb1","dialog",0,"space=10");
zdialog_add_widget(zd,"button","undolast","hb1",Bundolast,"space=5");
zdialog_add_widget(zd,"button","undoall","hb1",Bundoall,"space=5");
zdialog_add_widget(zd,"hbox","hb2","dialog",0,"space=6");
zdialog_add_widget(zd,"button","grid","hb2",Bgrid,"space=10");
currgrid = 4; // use warp-linear grid
NWarpL = WarpLdrag = 0; // no drag data
int cc = E3pxm->ww * E3pxm->hh * sizeof(float);
WarpLx = (float *) zmalloc(cc); // get memory for pixel displacements
WarpLy = (float *) zmalloc(cc);
for (py = 0; py < E3pxm->hh; py++) // no pixel displacements
for (px = 0; px < E3pxm->ww; px++)
{
ii = py * E3pxm->ww + px;
WarpLx[ii] = WarpLy[ii] = 0.0;
}
E3ww = E3pxm->ww; // preview dimensions
E3hh = E3pxm->hh;
zdialog_run(zd,WarpL_dialog_event,"save"); // run dialog, parallel
takeMouse(WarpL_mousefunc,dragcursor); // connect mouse function
return;
}
// dialog event and completion callback function
int warpL_names::WarpL_dialog_event(zdialog * zd, cchar *event)
{
using namespace warpL_names;
int px, py, ii;
int fpx, fpy, epx, epy, vstat;
float scale, dispx, dispy;
float vpix[4], *pix3;
int nc = E1pxm->nc, pcc = nc * sizeof(float);
if (strmatch(event,"done")) zd->zstat = 1; // from edit_setup() or f_save()
if (strmatch(event,"enter")) zd->zstat = 1; // KB input
if (strmatch(event,"cancel")) zd->zstat = 2; // from f_open()
if (zd->zstat) goto complete;
if (strmatch(event,"undolast"))
{
if (NWarpL == 1) event = "undoall";
else if (NWarpL) { // undo most recent drag
ii = --NWarpL;
$mdx = WarpLmem[0][ii];
$mdy = WarpLmem[1][ii];
$mdw = -WarpLmem[2][ii];
$mdh = -WarpLmem[3][ii];
$acc = 0;
WarpL_warpfunc(); // undrag image
$acc = 1;
WarpL_warpfunc(); // undrag memory
}
}
if (strmatch(event,"undoall")) // undo all drags
{
NWarpL = 0; // erase undo memory
for (py = 0; py < E3pxm->hh; py++) // reset pixel displacements
for (px = 0; px < E3pxm->ww; px++)
{
ii = py * E3pxm->ww + px;
WarpLx[ii] = WarpLy[ii] = 0.0;
}
edit_reset(); // restore image 1
}
if (strmatch(event,"grid")) m_gridlines(0,"grid 4"); // grid settings dialog
return 1;
complete:
currgrid = 0; // restore normal grid settings
if (zd->zstat != 1 || NWarpL == 0) // cancel or no warps made
edit_cancel(0);
else
{
edit_fullsize(); // get full-size E1/E3
scale = 1.0 * (E3pxm->ww + E3pxm->hh) / (E3ww + E3hh);
for (fpy = 0; fpy < E3pxm->hh; fpy++) // scale net pixel displacements
for (fpx = 0; fpx < E3pxm->ww; fpx++) // to full image size
{
epx = E3ww * fpx / E3pxm->ww;
epy = E3hh * fpy / E3pxm->hh;
ii = epy * E3ww + epx;
dispx = WarpLx[ii] * scale;
dispy = WarpLy[ii] * scale;
vstat = vpixel(E1pxm,fpx+dispx,fpy+dispy,vpix); // input virtual pixel
pix3 = PXMpix(E3pxm,fpx,fpy); // output pixel
if (vstat) memcpy(pix3,vpix,pcc);
else memset(pix3,0,pcc); // voided pixel
}
signal_thread();
edit_done(0);
}
zfree(WarpLx); // release memory
zfree(WarpLy);
return 1;
}
// WarpL mouse function
void warpL_names::WarpL_mousefunc(void)
{
using namespace warpL_names;
int ii;
if (Mxdrag || Mydrag) // mouse drag underway
{
$mdx = Mxdown; // drag origin, window coordinates
$mdy = Mydown;
$mdw = Mxdrag - Mxdown; // drag increment
$mdh = Mydrag - Mydown;
$acc = 0;
WarpL_warpfunc(); // drag image
WarpLdrag = 1;
Mxdrag = Mydrag = 0;
return;
}
else if (WarpLdrag)
{
WarpLdrag = 0;
$acc = 1;
WarpL_warpfunc(); // drag done, add to memory
if (NWarpL == 100) // if full, throw away oldest
{
NWarpL = 99;
for (ii = 0; ii < NWarpL; ii++)
{
WarpLmem[0][ii] = WarpLmem[0][ii+1];
WarpLmem[1][ii] = WarpLmem[1][ii+1];
WarpLmem[2][ii] = WarpLmem[2][ii+1];
WarpLmem[3][ii] = WarpLmem[3][ii+1];
}
}
ii = NWarpL;
WarpLmem[0][ii] = $mdx; // save drag for undo
WarpLmem[1][ii] = $mdy;
WarpLmem[2][ii] = $mdw;
WarpLmem[3][ii] = $mdh;
NWarpL++;
}
return;
}
// warp image and accumulate warp memory
// mouse at ($mdx,$mdy) is moved ($mdw,$mdh) pixels
void warpL_names::WarpL_warpfunc()
{
using namespace warpL_names;
float d1, d2, d3, d4;
d1 = ($mdx-0) * ($mdx-0) + ($mdy-0) * ($mdy-0); // distance, mouse to 4 corners
d2 = (E3pxm->ww-$mdx) * (E3pxm->ww-$mdx) + ($mdy-0) * ($mdy-0);
d3 = (E3pxm->ww-$mdx) * (E3pxm->ww-$mdx) + (E3pxm->hh-$mdy) * (E3pxm->hh-$mdy);
d4 = ($mdx-0) * ($mdx-0) + (E3pxm->hh-$mdy) * (E3pxm->hh-$mdy);
$D = d1;
if (d2 > $D) $D = d2; // find greatest corner distance
if (d3 > $D) $D = d3;
if (d4 > $D) $D = d4;
if ($D == d1) { // NW corner
$D = 1;
$Dx = $mdx; // x/y distance, mouse to edges
$Dy = $mdy;
}
if ($D == d2) { // NE
$D = 2;
$Dx = E3ww - $mdx;
$Dy = $mdy;
}
if ($D == d3) { // SE
$D = 3;
$Dx = E3ww - $mdx;
$Dy = E3hh - $mdy;
}
if ($D == d4) { // SW
$D = 4;
$Dx = $mdx;
$Dy = E3hh - $mdy;
}
for (int ii = 0; ii < NWT; ii++) // start worker threads
start_wthread(WarpL_wthread,&Nval[ii]);
wait_wthreads(); // wait for completion
CEF->Fmods++;
CEF->Fsaved = 0;
Fpaint2(); // update window
return;
}
// working thread to process the pixels
void * warpL_names::WarpL_wthread(void *arg) // algorithm change
{
using namespace warpL_names;
int index = *((int *) arg);
int ii, px, py, vstat;
float dx, dy, mag, dispx, dispy;
float vpix[4], *pix3;
int nc = E1pxm->nc, pcc = nc * sizeof(float);
for (py = index; py < E3pxm->hh; py += NWT) // process all pixels
for (px = 0; px < E3pxm->ww; px++)
{
if ($D == 1) {
dx = $mdx - px; // x/y distance, pixel to mouse
dy = $mdy - py;
}
else if ($D == 2) {
dx = px - $mdx;
dy = $mdy - py;
}
else if ($D == 3) {
dx = px - $mdx;
dy = py - $mdy;
}
else /* $D == 4 */ {
dx = $mdx - px;
dy = py - $mdy;
}
mag = (1.0 - dx / $Dx) * (1.0 - dy / $Dy); // pixel movement / mouse drag
dispx = -$mdw * mag; // displacement = drag * mag
dispy = -$mdh * mag;
ii = py * E3pxm->ww + px;
if ($acc) { // drag done, accumulate drag sum
WarpLx[ii] += dispx;
WarpLy[ii] += dispy;
continue;
}
dispx += WarpLx[ii]; // add this drag to prior sum
dispy += WarpLy[ii];
vstat = vpixel(E1pxm,px+dispx,py+dispy,vpix); // input virtual pixel
pix3 = PXMpix(E3pxm,px,py); // output pixel
if (vstat) memcpy(pix3,vpix,pcc);
else memset(pix3,0,pcc); // voided pixel
}
exit_wthread(); // exit thread
return 0; // not executed, avoid gcc warning
}
/********************************************************************************/
// warp/distort whole image using affine transform
// (straight lines remain straight)
float WarpF_old[3][2]; // 3 original image points
float WarpF_new[3][2]; // corresponding warped points
float WarpF_coeff[6]; // transform coefficients
float WarpF_Icoeff[6]; // inverse transform coefficients
int WarpF_ftf; // first time flag
editfunc EFwarpF;
void WarpF_warpfunc(); // image warp function
void WarpF_mousefunc(void);
void WarpF_affine(float po[3][2], float pn[3][2], float coeff[6]); // compute affine transform coefficients
void WarpF_invert(float coeff[6], float Icoeff[6]); // compute reverse transform coefficients
// menu function
void m_warp_affine(GtkWidget *, cchar *)
{
int WarpF_dialog_event(zdialog *zd, cchar *event);
cchar *WarpF_message = ZTX(
" Pull on an image corner using the mouse. \n"
" Make multiple mouse pulls until satisfied. \n"
" When finished, press [done].");
F1_help_topic = "warp_affine";
EFwarpF.menufunc = m_warp_affine;
EFwarpF.funcname = "warp-affine";
EFwarpF.FprevReq = 1; // use preview
EFwarpF.mousefunc = WarpF_mousefunc; // mouse function
if (! edit_setup(EFwarpF)) return; // setup edit
PXM_addalpha(E0pxm); // 15.09
PXM_addalpha(E1pxm);
PXM_addalpha(E3pxm);
zdialog *zd = zdialog_new(ZTX("Warp affine"),Mwin,Bdone,Bcancel,null);
EFwarpF.zd = zd;
zdialog_add_widget(zd,"label","lab1","dialog",WarpF_message,"space=3");
zdialog_add_widget(zd,"hbox","hb2","dialog",0,"space=6");
zdialog_add_widget(zd,"button","grid","hb2",Bgrid,"space=10");
currgrid = 5; // use warp-affine grid
WarpF_ftf = 1; // 1st warp flag
zdialog_run(zd,WarpF_dialog_event,"save"); // run dialog, parallel
takeMouse(WarpF_mousefunc,dragcursor); // connect mouse function
return;
}
// dialog event and completion callback function
int WarpF_dialog_event(zdialog *zd, cchar *event)
{
float scale;
int ww, hh;
if (strmatch(event,"done")) zd->zstat = 1; // from edit_setup() or f_save()
if (strmatch(event,"enter")) zd->zstat = 1; // KB input
if (strmatch(event,"cancel")) zd->zstat = 2; // from f_open()
if (strmatch(event,"grid")) m_gridlines(0,"grid 5"); // grid settings dialog
if (! zd->zstat) return 1; // wait for completion
currgrid = 0; // restore normal grid settings
if (zd->zstat != 1 || ! CEF->Fmods) {
edit_cancel(0);
return 1;
}
ww = E3pxm->ww; // preview image dimensions
hh = E3pxm->hh;
edit_fullsize(); // get full-size images
scale = 1.0 * (E3pxm->ww + E3pxm->hh) / (ww + hh); // preview to full-size scale factor
WarpF_old[0][0] = WarpF_old[0][0] * scale; // re-scale new and old points
WarpF_old[0][1] = WarpF_old[0][1] * scale;
WarpF_old[1][0] = WarpF_old[1][0] * scale;
WarpF_old[1][1] = WarpF_old[1][1] * scale;
WarpF_old[2][0] = WarpF_old[2][0] * scale;
WarpF_old[2][1] = WarpF_old[2][1] * scale;
WarpF_new[0][0] = WarpF_new[0][0] * scale;
WarpF_new[0][1] = WarpF_new[0][1] * scale;
WarpF_new[1][0] = WarpF_new[1][0] * scale;
WarpF_new[1][1] = WarpF_new[1][1] * scale;
WarpF_new[2][0] = WarpF_new[2][0] * scale;
WarpF_new[2][1] = WarpF_new[2][1] * scale;
WarpF_warpfunc(); // warp full-size image
edit_done(0);
return 1;
}
// WarpF mouse function
void WarpF_mousefunc(void)
{
int mdx1, mdy1, mdx2, mdy2;
float x1o, y1o, x2o, y2o, x3o, y3o;
float x1n, y1n, x2n, y2n, x3n, y3n;
float a, b, c, d, e, f;
if (! Mxdrag && ! Mydrag) return;
mdx1 = Mxdown; // mouse drag origin
mdy1 = Mydown;
mdx2 = Mxdrag; // mouse drag position
mdy2 = Mydrag;
Mxdown = Mxdrag; // reset origin for next time
Mydown = Mydrag;
x1n = mdx1; // point 1 = drag origin
y1n = mdy1;
x2n = E3pxm->ww - x1n; // point 2 = mirror of point1
y2n = E3pxm->hh - y1n;
x3n = E3pxm->ww * (y2n / E3pxm->hh);
y3n = E3pxm->hh * (1.0 - (x2n / E3pxm->ww));
if (WarpF_ftf) // first warp
{
WarpF_ftf = 0;
x1o = x1n; // old = current positions
y1o = y1n;
x2o = x2n;
y2o = y2n;
x3o = x3n;
y3o = y3n;
}
else
{
WarpF_invert(WarpF_coeff,WarpF_Icoeff); // get inverse coefficients
a = WarpF_Icoeff[0];
b = WarpF_Icoeff[1];
c = WarpF_Icoeff[2];
d = WarpF_Icoeff[3];
e = WarpF_Icoeff[4];
f = WarpF_Icoeff[5];
x1o = a * x1n + b * y1n + c; // compute old from current positions
y1o = d * x1n + e * y1n + f;
x2o = a * x2n + b * y2n + c;
y2o = d * x2n + e * y2n + f;
x3o = a * x3n + b * y3n + c;
y3o = d * x3n + e * y3n + f;
}
WarpF_old[0][0] = x1o; // set up 3 old points and corresponding
WarpF_old[0][1] = y1o; // new points for affine translation
WarpF_old[1][0] = x2o;
WarpF_old[1][1] = y2o;
WarpF_old[2][0] = x3o;
WarpF_old[2][1] = y3o;
x1n = mdx2; // point 1 new position = drag position
y1n = mdy2;
x2n = E3pxm->ww - x1n; // point 2 new = mirror of point1 new
y2n = E3pxm->hh - y1n;
WarpF_new[0][0] = x1n; // 3 new points
WarpF_new[0][1] = y1n;
WarpF_new[1][0] = x2n;
WarpF_new[1][1] = y2n;
WarpF_new[2][0] = x3n;
WarpF_new[2][1] = y3n;
WarpF_warpfunc(); // do the warp
Mxdrag = Mydrag = 0;
return;
}
// warp image and accumulate warp memory
void WarpF_warpfunc()
{
void * WarpF_wthread(void *);
WarpF_affine(WarpF_old, WarpF_new, WarpF_coeff); // get coefficients for forward transform
WarpF_invert(WarpF_coeff, WarpF_Icoeff); // get coefficients for reverse transform
for (int ii = 0; ii < NWT; ii++) // start worker threads
start_wthread(WarpF_wthread,&Nval[ii]);
wait_wthreads(); // wait for completion
CEF->Fmods++;
CEF->Fsaved = 0;
Fpaint2(); // update window
return;
}
// working thread to process the pixels
void * WarpF_wthread(void *arg)
{
int index = *((int *) arg);
float a, b, c, d, e, f;
int px3, py3, vstat;
float px1, py1;
float vpix1[4], *pix3;
int nc = E1pxm->nc, pcc = nc * sizeof(float);
a = WarpF_Icoeff[0]; // coefficients to map output pixels
b = WarpF_Icoeff[1]; // to corresponding input pixels
c = WarpF_Icoeff[2];
d = WarpF_Icoeff[3];
e = WarpF_Icoeff[4];
f = WarpF_Icoeff[5];
for (py3 = index; py3 < E3pxm->hh; py3 += NWT) // process all pixels
for (px3 = 0; px3 < E3pxm->ww; px3++)
{
px1 = a * px3 + b * py3 + c; // corresponding input pixel
py1 = d * px3 + e * py3 + f;
vstat = vpixel(E1pxm,px1,py1,vpix1); // input virtual pixel
pix3 = PXMpix(E3pxm,px3,py3); // output pixel
if (vstat) memcpy(pix3,vpix1,pcc);
else memset(pix3,0,pcc); // voided pixel
}
exit_wthread(); // exit thread
return 0; // not executed, avoid gcc warning
}
/********************************************************************************
Compute affine transformation of an image (warp image).
Given 3 new (warped) positions for 3 image points, derive the
coefficients of the translation function to warp the entire image.
Inputs:
pold[3][2] (x,y) coordinates for 3 points in original image
pnew[3][2] (x,y) coordinates for same points in warped image
Output:
coeff[6] coefficients of translation function which can be used
to convert all image points to their warped positions
If coeff[6] = (a, b, c, d, e, f) then the following formula
can be used to convert an image point to its warped position:
Xnew = a * Xold + b * Yold + c
Ynew = d * Xold + e * Yold + f
*********************************************************************************/
void WarpF_affine(float pold[3][2], float pnew[3][2], float coeff[6])
{
float x11, y11, x12, y12, x13, y13; // original points
float x21, y21, x22, y22, x23, y23; // moved points
float a, b, c, d, e, f; // coefficients
float A1, A2, B1, B2, C1, C2;
x11 = pold[0][0];
y11 = pold[0][1];
x12 = pold[1][0];
y12 = pold[1][1];
x13 = pold[2][0];
y13 = pold[2][1];
x21 = pnew[0][0];
y21 = pnew[0][1];
x22 = pnew[1][0];
y22 = pnew[1][1];
x23 = pnew[2][0];
y23 = pnew[2][1];
A1 = x11 - x12;
A2 = x12 - x13;
B1 = y11 - y12;
B2 = y12 - y13;
C1 = x21 - x22;
C2 = x22 - x23;
a = (B1 * C2 - B2 * C1) / (A2 * B1 - A1 * B2);
b = (A1 * C2 - A2 * C1) / (A1 * B2 - A2 * B1);
c = x23 - a * x13 - b * y13;
C1 = y21 - y22;
C2 = y22 - y23;
d = (B1 * C2 - B2 * C1) / (A2 * B1 - A1 * B2);
e = (A1 * C2 - A2 * C1) / (A1 * B2 - A2 * B1);
f = y23 - d * x13 - e * y13;
coeff[0] = a;
coeff[1] = b;
coeff[2] = c;
coeff[3] = d;
coeff[4] = e;
coeff[5] = f;
return;
}
/********************************************************************************
Invert affine transform
Input:
coeff[6] coefficients of translation function to convert
image points to their warped positions
Output:
Icoeff[6] coefficients of translation function to convert
warped image points to their original positions
If Icoeff[6] = (a, b, c, d, e, f) then the following formula can be
used to translate a warped image point to its original position:
Xold = a * Xnew + b * Ynew + c
Yold = d * Xnew + e * Ynew + f
*********************************************************************************/
void WarpF_invert(float coeff[6], float Icoeff[6])
{
float a, b, c, d, e, f, Z;
a = coeff[0];
b = coeff[1];
c = coeff[2];
d = coeff[3];
e = coeff[4];
f = coeff[5];
Z = 1.0 / (a * e - b * d);
Icoeff[0] = e * Z;
Icoeff[1] = - b * Z;
Icoeff[2] = Z * (b * f - c * e);
Icoeff[3] = - d * Z;
Icoeff[4] = a * Z;
Icoeff[5] = Z * (c * d - a * f);
return;
}
/********************************************************************************/
// Flatten a photographed book page.
// Compensate for page curvature at the center binding.
editfunc EFflatbook; // edit function data
namespace flatbook {
int Tmx[20], Tmy[20], Bmx[20], Bmy[20]; // top/bottom mouse click points
int Tnm = 0, Bnm = 0; // top/bottom click points counts
int Tbase, Bbase; // top/bottom points, low values
int ww, hh; // image dimensions
int E3warped; // flag, E3 image is warped
double *Tfy, *Bfy; // derived top/bottom y-shifts [ww]
double *Tfx, *Bfx; // derived top/bottom x-shifts [ww]
double Tstretch, Bstretch; // top/bottom x-shift stretch factors
}
// menu function
void m_flatbook(GtkWidget *, const char *)
{
using namespace flatbook;
int flatbook_dialog_event(zdialog* zd, const char *event);
void * flatbook_thread(void *);
void flatbook_mousefunc();
void flatbook_draw();
cchar *title = ZTX("Flatten Book Page Photo");
cchar *guide = ZTX("Trim image to isolate one page. \n"
"Map top and bottom edges with \n"
"4+ mouse clicks, then flatten: ");
cchar *stretch = ZTX("Stretch curved-down surfaces:");
zdialog *zd;
F1_help_topic = "flatten_book";
EFflatbook.menufunc = m_flatbook;
EFflatbook.funcname = "flatbook"; // func name, no preview, no area
EFflatbook.threadfunc = flatbook_thread;
EFflatbook.mousefunc = flatbook_mousefunc;
if (! edit_setup(EFflatbook)) return; // setup edit
int cc = E1pxm->ww * sizeof(double); // allocate memory
Tfy = (double *) zmalloc(cc);
Bfy = (double *) zmalloc(cc);
Tfx = (double *) zmalloc(cc);
Bfx = (double *) zmalloc(cc);
if (ww != E1pxm->ww) Tnm = Bnm = 0; // clear prior points if image
if (hh != E1pxm->hh) Tnm = Bnm = 0; // size is different
ww = E1pxm->ww; // set image size
hh = E1pxm->hh;
E3warped = 0; // no E3 warp yet
/***
_________________________________
| Flatten Book Page Photo |
| |
| Trim image to isolate one page. |
| Map top and bottom edges with |
| 4+ mouse clicks, then flatten: |
| [clear] [flatten] [undo] |
| |
| Stretch curved-down surfaces: |
| Top: ========[]============== |
| Bottom: =========[]========== |
| |
| [done] [cancel] |
|_________________________________|
***/
zd = zdialog_new(title,Mwin,Bdone,Bcancel,null); // flatbook dialog
EFflatbook.zd = zd;
zdialog_add_widget(zd,"hbox","hbg","dialog",0,"space=3");
zdialog_add_widget(zd,"label","labg","hbg",guide,"space=8");
zdialog_add_widget(zd,"hbox","hbf","dialog");
zdialog_add_widget(zd,"button","clear","hbf",Bclear,"space=10");
zdialog_add_widget(zd,"button","flatten","hbf",Bflatten,"space=10");
zdialog_add_widget(zd,"button","undo","hbf",Bundo,"space=10");
zdialog_add_widget(zd,"hbox","space","dialog",0,"space=5");
zdialog_add_widget(zd,"hbox","hbs1","dialog");
zdialog_add_widget(zd,"label","labs1","hbs1",stretch,"space=8");
zdialog_add_widget(zd,"hbox","hbs2","dialog");
zdialog_add_widget(zd,"label","labs2","hbs2",ZTX("Top:"),"space=8");
zdialog_add_widget(zd,"hscale","top","hbs2","1|30|0.01|1","expand|space=5");
zdialog_add_widget(zd,"hbox","hbs3","dialog");
zdialog_add_widget(zd,"label","labs3","hbs3",ZTX("Bottom:"),"space=8");
zdialog_add_widget(zd,"hscale","bottom","hbs3","1|30|0.01|1","expand|space=5");
zdialog_restore_inputs(zd); // restore prior inputs
zdialog_resize(zd,300,0);
zdialog_run(zd,flatbook_dialog_event,"save"); // run dialog - parallel
takeMouse(flatbook_mousefunc,dragcursor); // connect mouse function
if (Tnm + Bnm > 0) flatbook_draw();
return;
}
// flatbook dialog event and completion function
int flatbook_dialog_event(zdialog *zd, const char *event) // flatbook dialog event function
{
using namespace flatbook;
void flatbook_draw();
if (strmatch(event,"done")) zd->zstat = 1; // from edit_setup() or f_save()
if (strmatch(event,"enter")) zd->zstat = 1; // KB input
if (strmatch(event,"cancel")) zd->zstat = 2; // from f_open()
if (zd->zstat) {
zfree(Tfy); // free memory
zfree(Bfy);
zfree(Tfx);
zfree(Bfx);
if (zd->zstat == 1) edit_done(0); // commit edit
else edit_cancel(0); // cancel or destroy
return 1;
}
if (strmatch(event,"clear")) { // clear all mouse points
Tnm = Bnm = 0;
edit_undo();
flatbook_draw();
}
if (strstr("flatten top bottom",event)) {
if (Tnm < 4 || Bnm < 4) return 1;
zdialog_fetch(zd,"top",Tstretch);
zdialog_fetch(zd,"bottom",Bstretch);
signal_thread(); // trigger update thread
}
if (strmatch(event,"undo")) {
edit_undo();
E3warped = 0;
flatbook_draw();
}
if (strmatch(event,"line_color")) flatbook_draw(); // refresh lines after window redraw
return 1;
}
// flatbook mouse function
void flatbook_mousefunc()
{
using namespace flatbook;
void flatbook_draw();
int ii, jj, mx, my, dist;
int Fadd = 0, close;
if (! (LMclick || RMclick || Mxdrag || Mydrag)) return; // ignore mouse movement
if (E3warped) return; // E3 image warped, ignore mouse
if (LMclick || RMclick) { // left or right mouse click
mx = Mxclick;
my = Myclick;
if (LMclick) Fadd = 1;
LMclick = RMclick = 0;
}
else { // mouse drag
mx = Mxdrag;
my = Mydrag;
Mxdrag = Mydrag = 0;
Fadd = 1;
}
close = 0.01 * ww; // 1% of image size
if (my < hh/2) {
for (ii = 0; ii < Tnm; ii++) { // compare mouse position with
dist = abs(mx-Tmx[ii]); // existing top points
if (dist < close) {
Tnm--; // delete any too close
for (jj = ii--; jj < Tnm; jj++) {
Tmx[jj] = Tmx[jj+1];
Tmy[jj] = Tmy[jj+1];
}
}
}
}
else {
for (ii = 0; ii < Bnm; ii++) { // same for bottom points
dist = abs(mx-Bmx[ii]);
if (dist < close) {
Bnm--;
for (jj = ii--; jj < Bnm; jj++) {
Bmx[jj] = Bmx[jj+1];
Bmy[jj] = Bmy[jj+1];
}
}
}
}
if (Fadd) // add new mouse position
{
if (my < hh/2) { // add to top points
if (Tnm == 20) return;
for (ii = 0; ii < Tnm; ii++)
if (mx < Tmx[ii]) break;
for (jj = Tnm; jj > ii; jj--) {
Tmx[jj] = Tmx[jj-1];
Tmy[jj] = Tmy[jj-1];
}
Tmx[ii] = mx;
Tmy[ii] = my;
Tnm++;
}
else { // add to bottom points
if (Bnm == 20) return;
for (ii = 0; ii < Bnm; ii++)
if (mx < Bmx[ii]) break;
for (jj = Bnm; jj > ii; jj--) {
Bmx[jj] = Bmx[jj-1];
Bmy[jj] = Bmy[jj-1];
}
Bmx[ii] = mx;
Bmy[ii] = my;
Bnm++;
}
}
edit_undo(); // erase and redraw
flatbook_draw();
return;
}
// generate spline curves from mouse points and draw on the image
void flatbook_draw()
{
using namespace flatbook;
float Tnx[20], Tny[20], Bnx[20], Bny[20]; // top/bottom spline nodes
float *ppix3;
int topmax, topmin, bottmax, bottmin; // limits of image update areas
int ii, px, py, qx, qy, npq;
topmax = topmin = bottmax = bottmin = 0;
npq = 3; // pixel block for drawing nodes
if (Tnm > 0) topmax = topmin = Tmy[0];
for (ii = 0; ii < Tnm; ii++) // draw top mouse points
{
px = Tmx[ii];
py = Tmy[ii];
for (qy = py-npq; qy <= py+npq; qy++)
for (qx = px-npq; qx <= px+npq; qx++) {
if (qx < 0 || qx > ww-1) continue;
if (qy < 0 || qy > hh-1) continue;
ppix3 = PXMpix(E3pxm,qx,qy);
ppix3[0] = LINE_COLOR[0];
ppix3[1] = LINE_COLOR[1];
ppix3[2] = LINE_COLOR[2];
if (qy > topmax) topmax = qy;
if (qy < topmin) topmin = qy;
}
}
if (Bnm > 0) bottmax = bottmin = Bmy[0];
for (ii = 0; ii < Bnm; ii++) // draw bottom mouse points
{
px = Bmx[ii];
py = Bmy[ii];
for (qy = py-npq; qy <= py+npq; qy++)
for (qx = px-npq; qx <= px+npq; qx++) {
if (qx < 0 || qx > ww-1) continue;
if (qy < 0 || qy > hh-1) continue;
ppix3 = PXMpix(E3pxm,qx,qy);
ppix3[0] = LINE_COLOR[0];
ppix3[1] = LINE_COLOR[1];
ppix3[2] = LINE_COLOR[2];
if (qy > bottmax) bottmax = qy;
if (qy < bottmin) bottmin = qy;
}
}
if (Tnm > 3) // top mouse points
{
Tbase = Tmy[0]; // find lowest top point
for (ii = 0; ii < Tnm; ii++)
if (Tmy[ii] < Tbase) Tbase = Tmy[ii];
for (ii = 0; ii < Tnm; ii++) { // copy top points to spline nodes
Tnx[ii] = Tmx[ii]; // and convert to 0 base
Tny[ii] = Tmy[ii] - Tbase;
}
spline1(Tnm,Tnx,Tny); // generate spline curve
for (px = 0; px < ww; px++) // generate top curve y shifts
Tfy[px] = spline2(px);
for (px = 0; px < ww; px++) { // draw top curve over image
py = Tfy[px] + Tbase;
if (py < 0 || py > hh-1) continue;
ppix3 = PXMpix(E3pxm,px,py);
ppix3[0] = LINE_COLOR[0];
ppix3[1] = LINE_COLOR[1];
ppix3[2] = LINE_COLOR[2];
if (py < topmin) topmin = py;
if (py > topmax) topmax = py;
}
}
if (Bnm > 3) // bottom mouse points
{
Bbase = Bmy[0]; // find lowest bottom point
for (ii = 0; ii < Bnm; ii++)
if (Bmy[ii] < Bbase) Bbase = Bmy[ii];
for (ii = 0; ii < Bnm; ii++) { // copy bottom points to spline nodes
Bnx[ii] = Bmx[ii]; // and convert to 0 base
Bny[ii] = Bmy[ii] - Bbase;
}
spline1(Bnm,Bnx,Bny); // generate spline curve
for (px = 0; px < ww; px++) // generate bottom curve y shifts
Bfy[px] = spline2(px);
for (px = 0; px < ww; px++) { // draw bottom curve over image
py = Bfy[px] + Bbase;
if (py < 0 || py > hh-1) continue; // 15.09
ppix3 = PXMpix(E3pxm,px,py);
ppix3[0] = LINE_COLOR[0];
ppix3[1] = LINE_COLOR[1];
ppix3[2] = LINE_COLOR[2];
if (py < bottmin) bottmin = py;
if (py > bottmax) bottmax = py;
}
}
if (Tnm) Fpaint3(0,topmin,ww,topmax-topmin); // paint modified areas
if (Bnm) Fpaint3(0,bottmin,ww,bottmax-bottmin);
if (Tnm + Bnm) CEF->Fmods++; // necessary
CEF->Fsaved = 0;
return;
}
// flatbook thread function - warp image based on input parameters
void * flatbook_thread(void *)
{
using namespace flatbook;
void * flatbook_wthread(void *arg); // worker thread
int px;
double Tslope, Bslope, slope, Rt, Rb;
while (true)
{
thread_idle_loop();
E3warped = 1; // flag, E3 image is warped
Tfx[0] = Bfx[0] = 0;
for (px = 1; px < ww; px++) // loop page width
{
Tslope = fabs(Tfy[px] - Tfy[px-1]); // slope of top function
Bslope = fabs(Bfy[px] - Bfy[px-1]); // slope of bottom function
slope = Tslope * Tstretch; // mean slope, stretched 15.09
slope = cos(atan(slope));
Tfx[px] = Tfx[px-1] + slope; // resulting pixel x-shift
slope = Bslope * Bstretch;
slope = cos(atan(slope));
Bfx[px] = Bfx[px-1] + slope;
}
Rt = (ww-1) / Tfx[ww-1]; // make overall width the same
Rb = (ww-1) / Bfx[ww-1];
for (px = 0; px < ww; px++) {
Tfx[px] = Rt * Tfx[px]; // (using separate loops causes
Bfx[px] = Rb * Bfx[px]; // GCC optimization bug)
}
for (int ii = 0; ii < NWT; ii++) // start worker threads
start_wthread(flatbook_wthread,&Nval[ii]);
wait_wthreads(); // wait for completion
CEF->Fmods++; // image is modified
CEF->Fsaved = 0;
Fpaint2(); // update window
}
return 0;
}
void * flatbook_wthread(void *arg)
{
using namespace flatbook;
int index = *((int *) (arg));
int px, py, vstat;
double Rt, Rb;
float *pix3, vpix[4], vpx, vpy;
int nc = E1pxm->nc, pcc = nc * sizeof(float);
for (py = index; py < hh; py += NWT) // loop from top to bottom
{
Rt = 1.0 - 1.0 * py / hh; // Rt from 1 to 0 - top weight
Rb = 1.0 - Rt; // Rb from 0 to 1 - bottom weight
for (px = 0; px < ww; px++) // loop page width
{
pix3 = PXMpix(E3pxm,px,py); // output pixel
vpx = Rt * Tfx[px] + Rb * Bfx[px]; // source pixel for (px,py)
vpy = py + Rt * Tfy[int(vpx)] + Rb * Bfy[int(vpx)];
vstat = vpixel(E1pxm,vpx,vpy,vpix); // get source pixel
if (vstat) memcpy(pix3,vpix,pcc);
else memset(pix3,0,pcc); // off page, void pixel
}
}
exit_wthread();
return 0;
}
/********************************************************************************/
// project image on to a sphere with adjustable radius (flatness)
namespace sphere_names
{
int E3ww, E3hh; // image dimensions
int Xcen, Ycen, Dia; // center and diameter of sphere
float flatten = 0; // flatten parameter
float magnify = 1; // magnify parameter
editfunc EFsphere; // edit function data
}
// menu function
void m_sphere(GtkWidget *, const char *) // 15.11
{
using namespace sphere_names;
int sphere_dialog_event(zdialog* zd, const char *event);
void sphere_mousefunc(void);
void * sphere_thread(void *);
cchar *title = ZTX("Spherical Projection");
F1_help_topic = "sphere";
EFsphere.menufunc = m_sphere;
EFsphere.funcname = "sphere"; // function name
EFsphere.FprevReq = 1; // use preview edit mode
EFsphere.mousefunc = sphere_mousefunc; // mouse function
EFsphere.threadfunc = sphere_thread; // thread function
if (! edit_setup(EFsphere)) return; // setup edit
PXM_addalpha(E0pxm);
PXM_addalpha(E1pxm);
PXM_addalpha(E3pxm);
E3ww = E3pxm->ww;
E3hh = E3pxm->hh;
Xcen = E3ww / 2; // set dialog defaults
Ycen = E3hh / 2;
Dia = E3ww;
if (E3hh < E3ww) Dia = E3hh;
flatten = 0;
magnify = 1;
/***
_____________________________
| Spherical Projection |
| |
| Flatten ======[]========== |
| Magnify ==========[]====== |
| |
| [done] [cancel] |
|_____________________________|
***/
zdialog *zd = zdialog_new(title,Mwin,Bdone,Bcancel,null); // sphere dialog
CEF->zd = zd;
zdialog_add_widget(zd,"hbox","hbflat","dialog");
zdialog_add_widget(zd,"label","labflat","hbflat",Bflatten,"space=5");
zdialog_add_widget(zd,"hscale","flatten","hbflat","0.0|0.999|0.001|0.0","expand");
zdialog_add_widget(zd,"hbox","hbmag","dialog");
zdialog_add_widget(zd,"label","labmag","hbmag",ZTX("Magnify"),"space=5");
zdialog_add_widget(zd,"hscale","magnify","hbmag","1.0|2.0|0.001|1.0","expand");
zdialog_resize(zd,250,0);
zdialog_run(zd,sphere_dialog_event,"save"); // run dialog - parallel
takeMouse(sphere_mousefunc,0); // connect mouse function
signal_thread(); // trigger update thread
return;
}
// sphere dialog event and completion function
int sphere_dialog_event(zdialog *zd, const char *event)
{
using namespace sphere_names;
float scale;
if (strmatch(event,"done")) zd->zstat = 1; // from edit_setup() or f_save()
if (strmatch(event,"enter")) zd->zstat = 1; // KB input
if (strmatch(event,"cancel")) zd->zstat = 2; // from f_open()
if (zd->zstat)
{
if (zd->zstat == 1) // done
{
edit_fullsize(); // get full size image
scale = 1.0 * (E3pxm->ww + E3pxm->hh) / (E3ww + E3hh); // scale up the parameters
E3ww = E3pxm->ww;
E3hh = E3pxm->hh;
Xcen = scale * Xcen;
Ycen = scale * Ycen;
Dia = scale * Dia;
signal_thread(); // recalculate image
edit_done(0); // commit edit
}
else edit_cancel(0); // discard edit
return 1;
}
if (strmatch(event,"flatten")) {
zdialog_fetch(zd,"flatten",flatten);
signal_thread(); // trigger update thread
}
if (strmatch(event,"magnify")) {
zdialog_fetch(zd,"magnify",magnify);
signal_thread(); // trigger update thread
}
return 1;
}
// mouse function - get new center from mouse click or drag
void sphere_mousefunc(void)
{
using namespace sphere_names;
int edist;
if (! LMclick && ! Mdrag) return;
LMclick = 0;
if (Mxposn < 0.1 * E3ww || Mxposn > 0.9 * E3ww) return; // ignore if near image edge
if (Myposn < 0.1 * E3hh || Myposn > 0.9 * E3hh) return;
Xcen = Mxposn; // new center
Ycen = Myposn;
edist = Xcen; // find nearest edge distance
if (Ycen < edist) edist = Ycen;
if (E3ww - Xcen < edist) edist = E3ww - Xcen;
if (E3hh - Ycen < edist) edist = E3hh - Ycen;
Dia = 2 * edist; // new sphere diameter
signal_thread();
return;
}
// thread function - multiple working threads to update image
void * sphere_thread(void *)
{
void * sphere_wthread(void *arg); // worker thread
while (true)
{
thread_idle_loop(); // wait for work or exit request
for (int ii = 0; ii < NWT; ii++) // start worker threads
start_wthread(sphere_wthread,&Nval[ii]);
wait_wthreads(); // wait for completion
CEF->Fmods++; // image modified
CEF->Fsaved = 0; // not saved
Fpaint2(); // update window
}
return 0; // not executed, stop warning
}
void * sphere_wthread(void *arg) // worker thread function
{
using namespace sphere_names;
int index = *((int *) (arg));
int px3, py3, dx, dy, vstat;
float px1, py1, *pix3, vpix[4];
float s1, s2, D, T;
int nc = E1pxm->nc, pcc = nc * sizeof(float);
D = Dia / (1.0 - flatten);
for (py3 = index; py3 < E3hh; py3 += NWT) // loop output pixels
for (px3 = 0; px3 < E3ww; px3++)
{
pix3 = PXMpix(E3pxm,px3,py3); // output pixel
dx = px3 - Xcen;
dy = py3 - Ycen;
s1 = sqrtf(dx*dx + dy*dy); // dist. from center to output pixel
T = s1 * PI / D / magnify; // sine of subtended angle
if (T > 1.0) {
memset(pix3,0,pcc); // pixel = black, alpha = 0
continue;
}
s2 = D / PI * asinf(T); // corresp. dist. on sphere
px1 = Xcen + dx * s2 / s1; // input v.pixel
py1 = Ycen + dy * s2 / s1;
vstat = vpixel(E1pxm,px1,py1,vpix);
if (vstat) memcpy(pix3,vpix,pcc);
else memset(pix3,0,pcc); // pixel = black, alpha = 0
}
exit_wthread(); // exit thread
return 0; // not executed, avoid gcc warning
}
/********************************************************************************/
// Rescale an image while leaving selected areas unchanged.
namespace SLrescale_names
{
editfunc EFslrescale;
int Fsetups = 0;
int dragx, dragy;
int E3ww, E3hh;
char *sqrow, *sqcol;
int Nsqrow, Nsqcol;
int *npx, *npy;
int dialog_event(zdialog *zd, cchar *event);
void setups();
void cleanups();
void mousefunc();
void warpfunc();
void *warpthread(void *);
}
// menu function
void m_selective_rescale(GtkWidget *, cchar *) // 16.05
{
using namespace SLrescale_names;
cchar *message = ZTX(" Select areas to remain unchanged. \n"
" Pull image from upper left corner. \n"
" When finished, press [done].");
F1_help_topic = "selective_rescale";
EFslrescale.menufunc = m_selective_rescale;
EFslrescale.funcname = "selective rescale";
EFslrescale.Farea = 2; // select area usable
EFslrescale.mousefunc = mousefunc; // mouse function
if (! edit_setup(EFslrescale)) return; // setup edit
PXM_addalpha(E0pxm);
PXM_addalpha(E1pxm);
PXM_addalpha(E3pxm);
zdialog *zd = zdialog_new(ZTX("Selective Rescale"),Mwin,Bproceed,Bdone,Bcancel,null);
EFslrescale.zd = zd;
zdialog_add_widget(zd,"label","lab1","dialog",message,"space=3");
zdialog_run(zd,dialog_event,"save"); // run dialog, parallel
return;
}
// dialog event and completion callback function
int SLrescale_names::dialog_event(zdialog * zd, cchar *event)
{
using namespace SLrescale_names;
if (strmatch(event,"done")) zd->zstat = 2; // from edit_setup() or f_save()
if (strmatch(event,"enter")) zd->zstat = 2; // KB input
if (strmatch(event,"cancel")) zd->zstat = 3; // from f_open()
if (! zd->zstat) return 1; // wait for completion
if (zd->zstat == 1) { // [proceed]
zd->zstat = 0; // keep dialog active
if (sa_stat != 3)
zmessageACK(Mwin,ZTX("select areas first"));
else setups(); // start drag and warp
return 1;
}
if (zd->zstat != 2 || dragx + dragy == 0) { // [cancel] or no change
edit_cancel(0);
cleanups();
return 1;
}
edit_done(0); // [done]
cleanups();
m_trimrotate(0,"auto"); // set up automatic trim
return 1;
}
// do setups based on select area data
void SLrescale_names::setups()
{
int ii, spx, spy, sum;
cleanups(); // free prior if any
dragx = dragy = 0; // no drag data
E3ww = E3pxm->ww; // image dimensions
E3hh = E3pxm->hh;
sqrow = (char *) zmalloc(E3hh); // maps squishable rows/cols
sqcol = (char *) zmalloc(E3ww);
memset(sqrow,1,E3hh); // mark all rows/cols squishable
memset(sqcol,1,E3ww);
for (spy = 0; spy < E3hh; spy++) // loop all source pixels
for (spx = 0; spx < E3ww; spx++)
{
ii = spy * E3ww + spx; // pixel within area?
if (sa_pixmap[ii]) sqrow[spy] = sqcol[spx] = 0; // mark row/col non-squishable
}
Nsqrow = Nsqcol = 0;
for (spy = 0; spy < E3hh; spy++) // count total squishable rows/cols
Nsqrow += sqrow[spy];
for (spx = 0; spx < E3ww; spx++)
Nsqcol += sqcol[spx];
npx = (int *) zmalloc(E3ww * sizeof(int)); // count of squishable rows/cols
npy = (int *) zmalloc(E3hh * sizeof(int)); // predeeding a given row/col
for (sum = spx = 0; spx < E3ww; spx++)
{
if (sqcol[spx]) sum++;
npx[spx] = sum;
}
for (sum = spy = 0; spy < E3hh; spy++)
{
if (sqrow[spy]) sum++;
npy[spy] = sum; // squishable rows < spy
}
Fsetups = 1;
sa_unselect(); // delete area
takeMouse(mousefunc,dragcursor); // connect mouse function
return;
}
// free allocated memory
void SLrescale_names::cleanups()
{
if (! Fsetups) return;
Fsetups = 0;
zfree(sqrow);
zfree(sqcol);
zfree(npx);
zfree(npy);
return;
}
// mouse function
void SLrescale_names::mousefunc()
{
using namespace SLrescale_names;
float R;
if (Mxdrag || Mydrag) // mouse drag underway
{
R = 1.0 * Mxdown / E3ww; // ignore drag not from NW corner
if (R > 0.2) return;
R = 1.0 * Mydown / E3hh;
if (R > 0.2) return;
dragx = Mxdrag - Mxdown; // drag amount
dragy = Mydrag - Mydown;
warpfunc(); // drag image
Mxdrag = Mydrag = 0;
}
return;
}
// warp function
void SLrescale_names::warpfunc()
{
using namespace SLrescale_names;
for (int ii = 0; ii < NWT; ii++) // start worker threads
start_wthread(warpthread,&Nval[ii]);
wait_wthreads(); // wait for completion
CEF->Fmods++;
CEF->Fsaved = 0;
Fpaint2(); // update window
return;
}
// warp thread
void * SLrescale_names::warpthread(void *arg)
{
using namespace SLrescale_names;
int index = *((int *) (arg));
int spx, spy, dpx, dpy;
float Rx, Ry;
float *spix, *dpix;
int nc = E1pxm->nc, pcc = nc * sizeof(float);
for (spy = index; spy < E3hh; spy += NWT) // loop all source pixels
for (spx = 0; spx < E3ww; spx++)
{
if (spx < dragx || spy < dragy) { // pixels < dragx/dragy:
spix = PXMpix(E3pxm,spx,spy); // black, transparent
memset(spix,0,pcc);
}
Rx = 1.0 * npx[spx] / Nsqcol; // squishable pixel ratios, 0 - 1.0
Ry = 1.0 * npy[spy] / Nsqrow;
dpx = spx + dragx * (1.0 - Rx); // destination pixel
dpy = spy + dragy * (1.0 - Ry);
if (dpx < 0 || dpx > E3ww-1) continue; // necessary, why? ///////////
if (dpy < 0 || dpy > E3hh-1) continue;
dpix = PXMpix(E3pxm,dpx,dpy); // source pixel >> destination pixel
spix = PXMpix(E1pxm,spx,spy);
memcpy(dpix,spix,pcc);
}
exit_wthread();
return 0;
}
/********************************************************************************/
// make waves menu function
// distort the image with a wave pattern
// independent horizontal and vertical wavelengths
// variance option: make waves more or less irregular
// perspective option: wavelengths lengthen going down
namespace waves_names
{
editfunc EFwaves;
int ww, hh; // image dimensions
int WLV, WLH; // vertical and horizontal wavelengths
int AMPV, AMPH; // vertical and horizontal amplitudes
int VARV, VARH; // vertical and horizontal variance
int PERSP; // perspective 0-100
}
// menu function
void m_waves(GtkWidget *, const char *)
{
using namespace waves_names;
int waves_dialog_event(zdialog* zd, const char *event);
void * waves_thread(void *);
F1_help_topic = "make_waves";
EFwaves.menufunc = m_waves;
EFwaves.funcname = "make_waves";
EFwaves.Farea = 2; // select area usable
EFwaves.threadfunc = waves_thread;
if (! edit_setup(EFwaves)) return;
/***
___________________________________
| Make waves |
| |
| Horizontal Vertical |
| wavelength [___|-+] [___|-+] |
| amplitude [___|-+] [___|-+] |
| variance [___|-+] [___|-+] |
| |
| perspective [___|-+] |
| |
| [apply] [done] [cancel] |
|___________________________________|
***/
ww = E3pxm->ww;
hh = E3pxm->hh;
zdialog *zd = zdialog_new(ZTX("Make Waves"),Mwin,Bapply,Bdone,Bcancel,null);
EFwaves.zd = zd;
zdialog_add_widget(zd,"hbox","hbw","dialog",0,"space=3");
zdialog_add_widget(zd,"vbox","vbw1","hbw",0,"space=3");
zdialog_add_widget(zd,"vbox","vbw2","hbw",0,"space=3");
zdialog_add_widget(zd,"vbox","vbw3","hbw",0,"space=3");
zdialog_add_widget(zd,"label","space","vbw1"," ","space=1");
zdialog_add_widget(zd,"label","labwl","vbw1",ZTX("wavelength"),"expand");
zdialog_add_widget(zd,"label","labamp","vbw1",ZTX("amplitude"),"expand");
zdialog_add_widget(zd,"label","labamp","vbw1",ZTX("variance"),"expand");
zdialog_add_widget(zd,"label","labh","vbw2",ZTX("horizontal"),"space=1");
zdialog_add_widget(zd,"spin","wlh","vbw2","3|500|1|50","expand");
zdialog_add_widget(zd,"spin","amph","vbw2","0|100|1|20","expand");
zdialog_add_widget(zd,"spin","varh","vbw2","0|100|1|20","expand");
zdialog_add_widget(zd,"label","labh","vbw3",ZTX("vertical"),"space=1");
zdialog_add_widget(zd,"spin","wlv","vbw3","3|500|1|50","expand");
zdialog_add_widget(zd,"spin","ampv","vbw3","0|100|1|20","expand");
zdialog_add_widget(zd,"spin","varv","vbw3","0|100|1|20","expand");
zdialog_add_widget(zd,"hsep","sepp","dialog",0,"space=3");
zdialog_add_widget(zd,"hbox","hbp","dialog",0,"space=2");
zdialog_add_widget(zd,"label","labp","hbp",ZTX("perspective"),"space=3");
zdialog_add_widget(zd,"spin","persp","hbp","0|100|1|0","space=5");
zdialog_restore_inputs(zd); // restore previous inputs
zdialog_run(zd,waves_dialog_event,"save"); // run dialog - parallel
return;
}
// waves dialog event and completion function
int waves_dialog_event(zdialog *zd, const char *event) // waves dialog event function
{
using namespace waves_names;
if (strmatch(event,"done")) zd->zstat = 2; // from edit_setup() or f_save()
if (strmatch(event,"enter")) zd->zstat = 2; // KB input
if (strmatch(event,"cancel")) zd->zstat = 3; // from f_open()
if (zd->zstat)
{
if (zd->zstat == 1) { // apply
zd->zstat = 0; // keep dialog active
edit_reset();
zdialog_fetch(zd,"wlv",WLV); // get user inputs
zdialog_fetch(zd,"wlh",WLH);
zdialog_fetch(zd,"ampv",AMPV);
zdialog_fetch(zd,"amph",AMPH);
zdialog_fetch(zd,"varv",VARV);
zdialog_fetch(zd,"varh",VARH);
zdialog_fetch(zd,"persp",PERSP);
signal_thread(); // calculate
return 1;
}
if (zd->zstat == 2) edit_done(0); // commit edit
else edit_cancel(0); // discard edit
}
return 1;
}
// thread function - multiple working threads to update image
void * waves_thread(void *)
{
using namespace waves_names;
void * waves_wthread(void *);
while (true)
{
thread_idle_loop(); // wait for work or exit request
for (int ii = 0; ii < NWT; ii++) // start worker threads
start_wthread(waves_wthread,&Nval[ii]);
wait_wthreads(); // wait for completion
CEF->Fmods++; // image modified
CEF->Fsaved = 0; // not saved
Fpaint2(); // update window
}
return 0; // not executed, stop warning
}
void * waves_wthread(void *arg) // working threads
{
using namespace waves_names;
int index = *((int *) (arg));
float wlv, wlh, ampv, amph, varv, varh, persp;
int py, px, pylo, pyhi;
int ii, edist = 0;
float dx, dy, *pix1, *pix3, vpix[4];
float f1, f2, ffv, ffh, red, green, blue;
wlv = WLV; // vertical and horizontal wavelengths
wlh = WLH;
ampv = 0.01 * AMPV * wlv; // vertical and horizoneal amplitudes
amph = 0.01 * AMPH * wlh;
varv = 0.01 * VARV; // vertical and horizontal variance
varh = 0.01 * VARH;
persp = 0.01 * PERSP; // perspective 0 to 1.0
pylo = 0; // py range
pyhi = hh;
if (sa_stat == 3) {
pylo = sa_miny - wlv; // rescale to select area height
if (pylo < 0) pylo = 0;
pyhi = sa_maxy + wlv;
if (pyhi > hh) pyhi = hh;
}
for (py = pylo + index; py < pyhi; py += NWT)
{
if (persp == 0) ffv = ffh = 1.0;
else {
ffv = 2.0 * persp * (py - pylo) / (pyhi - pylo); // 0 ... 2.0
ffv = 1.0 + ffv * ffv; // 1 ... 5.0
ffh = sqrtf(ffv); // 1 ... 2.23
}
dx = ampv * sin(2.0 * PI * py / (ffv * wlv));
if (varv > 0)
dx += ampv * varv * sin(2.0 * PI * py / (1.37 * ffv * wlv)); // make irregular wave
for (px = 0; px < ww; px++)
{
if (sa_stat == 3) { // select area active
ii = py * ww + px;
edist = sa_pixmap[ii]; // distance from edge
if (! edist) continue; // pixel outside area
}
dy = amph * sin(2.0 * PI * px / (ffh * wlh));
if (varh > 0)
dy += amph * varh * sin(2.0 * PI * px / (1.37 * ffh * wlh)); // make irregular wave
vpixel(E1pxm,px+dx,py+dy,vpix); // source pixel, displaced
red = vpix[0];
green = vpix[1];
blue = vpix[2];
if (sa_stat == 3 && edist < sa_blend) { // blend edges
f1 = sa_blendfunc(edist); // 16.08
f2 = 1.0 - f1;
pix1 = PXMpix(E1pxm,px,py);
red = f1 * red + f2 * pix1[0];
green = f1 * green + f2 * pix1[1];
blue = f1 * blue + f2 * pix1[2];
}
pix3 = PXMpix(E3pxm,px,py); // destination pixel
pix3[0] = red;
pix3[1] = green;
pix3[2] = blue;
}
}
exit_wthread(); // exit thread
return 0; // not executed, stop gcc warning
}
|