1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328 8329 8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8432 8433 8434 8435 8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446 8447 8448 8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542 8543 8544 8545 8546 8547 8548 8549 8550 8551 8552 8553 8554 8555 8556 8557 8558 8559 8560 8561 8562 8563 8564 8565 8566 8567 8568 8569 8570 8571 8572 8573 8574 8575 8576 8577 8578 8579 8580 8581 8582 8583 8584 8585 8586 8587 8588 8589 8590 8591 8592 8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610 8611 8612 8613 8614 8615 8616 8617 8618 8619 8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634 8635 8636 8637 8638 8639 8640 8641 8642 8643 8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661 8662 8663 8664 8665 8666 8667 8668 8669 8670 8671 8672 8673 8674 8675 8676 8677 8678 8679 8680 8681 8682 8683 8684 8685 8686 8687 8688 8689 8690 8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705 8706 8707 8708 8709 8710 8711 8712 8713 8714 8715 8716 8717 8718 8719 8720 8721 8722 8723 8724 8725 8726 8727 8728 8729 8730 8731 8732 8733 8734 8735 8736 8737 8738 8739 8740 8741 8742 8743 8744 8745 8746 8747 8748 8749 8750 8751 8752 8753 8754 8755 8756 8757 8758 8759 8760 8761 8762 8763 8764 8765 8766 8767 8768 8769 8770 8771 8772 8773 8774 8775 8776 8777 8778 8779 8780 8781 8782 8783 8784 8785 8786 8787 8788 8789 8790 8791 8792 8793 8794 8795 8796 8797 8798 8799 8800 8801 8802 8803 8804 8805 8806 8807 8808 8809 8810 8811 8812 8813 8814 8815 8816 8817 8818 8819 8820 8821 8822 8823 8824 8825 8826 8827 8828 8829 8830 8831 8832 8833 8834 8835 8836 8837 8838 8839 8840 8841 8842 8843 8844 8845 8846 8847 8848 8849 8850 8851 8852 8853 8854 8855 8856 8857 8858 8859 8860 8861 8862 8863 8864 8865 8866 8867 8868 8869 8870 8871 8872 8873 8874 8875 8876 8877 8878 8879 8880 8881 8882 8883 8884 8885 8886 8887 8888 8889 8890 8891 8892 8893 8894 8895 8896 8897 8898 8899 8900 8901 8902 8903 8904 8905 8906 8907 8908 8909 8910 8911 8912 8913 8914 8915 8916 8917 8918 8919 8920 8921 8922 8923 8924 8925 8926 8927 8928 8929 8930 8931 8932 8933 8934 8935 8936 8937 8938 8939 8940 8941 8942 8943 8944 8945 8946 8947 8948 8949 8950 8951 8952 8953 8954 8955 8956 8957 8958 8959 8960 8961 8962 8963 8964 8965 8966 8967 8968 8969 8970 8971 8972 8973 8974 8975 8976 8977 8978 8979 8980 8981 8982 8983 8984 8985 8986 8987 8988 8989 8990 8991 8992 8993 8994 8995 8996 8997 8998 8999 9000 9001 9002 9003 9004 9005 9006 9007 9008 9009 9010 9011 9012 9013 9014 9015 9016 9017 9018 9019 9020 9021 9022 9023 9024 9025 9026 9027 9028 9029 9030 9031 9032 9033 9034 9035 9036 9037 9038 9039 9040 9041 9042 9043 9044 9045 9046 9047 9048 9049 9050 9051 9052 9053 9054 9055 9056 9057 9058 9059 9060 9061 9062 9063 9064 9065 9066 9067 9068 9069 9070 9071 9072 9073 9074 9075 9076 9077 9078 9079 9080 9081 9082 9083 9084 9085 9086 9087 9088 9089 9090 9091 9092 9093 9094 9095 9096 9097 9098 9099 9100 9101 9102 9103 9104 9105 9106 9107 9108 9109 9110 9111 9112 9113 9114 9115 9116 9117 9118 9119 9120 9121 9122 9123 9124 9125 9126 9127 9128 9129 9130 9131 9132 9133 9134 9135 9136 9137 9138 9139 9140 9141 9142 9143 9144 9145 9146 9147 9148 9149 9150 9151 9152 9153 9154 9155 9156 9157 9158 9159 9160 9161 9162 9163 9164 9165 9166 9167 9168 9169 9170 9171 9172 9173 9174 9175 9176 9177 9178 9179 9180 9181 9182 9183 9184 9185 9186 9187 9188 9189 9190 9191 9192 9193 9194 9195 9196 9197 9198 9199 9200 9201 9202 9203 9204 9205 9206 9207 9208 9209 9210 9211 9212 9213 9214 9215 9216 9217 9218 9219 9220 9221 9222 9223 9224 9225 9226 9227 9228 9229 9230 9231 9232 9233 9234 9235 9236 9237 9238 9239 9240 9241 9242 9243 9244 9245 9246 9247 9248 9249 9250 9251 9252 9253 9254 9255 9256 9257 9258 9259 9260 9261 9262 9263 9264 9265 9266 9267 9268 9269 9270 9271 9272 9273 9274 9275 9276 9277 9278 9279 9280 9281 9282 9283 9284 9285 9286 9287 9288 9289 9290 9291 9292 9293 9294 9295 9296 9297 9298 9299 9300 9301 9302 9303 9304 9305 9306 9307 9308 9309 9310 9311 9312 9313 9314 9315 9316 9317 9318 9319 9320 9321 9322 9323 9324 9325 9326 9327 9328 9329 9330 9331 9332 9333 9334 9335 9336 9337 9338 9339 9340 9341 9342 9343 9344 9345 9346 9347 9348 9349 9350 9351 9352 9353 9354 9355 9356 9357 9358 9359 9360 9361 9362 9363 9364 9365 9366 9367 9368 9369 9370 9371 9372 9373 9374 9375 9376 9377 9378 9379 9380 9381 9382 9383 9384 9385 9386 9387 9388 9389 9390 9391 9392 9393 9394 9395 9396 9397 9398 9399 9400 9401 9402 9403 9404 9405 9406 9407 9408 9409 9410 9411 9412 9413 9414 9415 9416 9417 9418 9419 9420 9421 9422 9423 9424 9425 9426 9427 9428 9429 9430 9431 9432 9433 9434 9435 9436 9437 9438 9439 9440 9441 9442 9443 9444 9445 9446 9447 9448 9449 9450 9451 9452 9453 9454 9455 9456 9457 9458 9459 9460 9461 9462 9463 9464 9465 9466 9467 9468 9469 9470 9471 9472 9473 9474 9475 9476 9477 9478 9479 9480 9481 9482 9483 9484 9485 9486 9487 9488 9489 9490 9491 9492 9493 9494 9495 9496 9497 9498 9499 9500 9501 9502 9503 9504 9505 9506 9507 9508 9509 9510 9511 9512 9513 9514 9515 9516 9517 9518 9519 9520 9521 9522 9523 9524 9525 9526 9527 9528 9529 9530 9531 9532 9533 9534 9535 9536 9537 9538 9539 9540 9541 9542 9543 9544 9545 9546 9547 9548 9549 9550 9551 9552 9553 9554 9555 9556 9557 9558 9559 9560 9561 9562 9563 9564 9565 9566 9567 9568 9569 9570 9571 9572 9573 9574 9575 9576 9577 9578 9579 9580 9581 9582 9583 9584 9585 9586 9587 9588 9589 9590 9591 9592 9593 9594 9595 9596 9597 9598 9599 9600 9601 9602 9603 9604 9605 9606 9607 9608 9609 9610 9611 9612 9613 9614 9615 9616 9617 9618 9619 9620 9621 9622 9623 9624 9625 9626 9627 9628 9629 9630 9631 9632 9633 9634 9635 9636 9637 9638 9639 9640 9641 9642 9643 9644 9645 9646 9647 9648 9649 9650
|
/********************************************************************************
Fotocx - edit photos and manage collections
Copyright 2007-2024 Michael Cornelison
source code URL: https://kornelix.net
contact: mkornelix@gmail.com
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. See https://www.gnu.org/licenses
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.
*********************************************************************************
Fotocx image edit - Edit menu functions
get_edit_pixels get pixels from image, area, mouse
m_rotate rotate or level an image
m_upright upright a rotated image
m_crop crop image margins
m_retouch brightness, contrast, color ...
blackbodyRGB convert deg. K to RGB factors
m_rescale rescale image
m_margins add margins around image
m_markup draw on image image: text, line, box, oval
m_draw_text draw text on image
m_draw_line draw line/arrow on image
m_draw_box draw box on image
m_draw_oval draw oval on image
m_color_mode convert: B&W, color, sepia, positive, negative
m_paint_edits set up edit via mouse painting
m_paint_image paint on the image with a color
RGB_chooser select RGB color or use color chooser file
HSL_chooser select HSL color
m_paint_transp paint more or less transparency on an image
m_area_fill fill selected areas or transparent areas
m_copy_in_image copy pixels within one image
m_copy_from_image copy pixels from source image to target image
m_copy_from_image_slave copy source pixels to shared memory
m_copy_prior_edit copy pixels from prior edits in current image
m_template template for new edit functions
*********************************************************************************/
#define EX extern // enable extern declarations
#include "fotocx.h" // (variables in fotocx.h are refs)
/********************************************************************************
get_edit_pixels() function
Feed pixels to edit function, one at a time
modes of operation:
Fpaintedits = 0, sa_stat != sa_stat_fini
return all pixels with blend = 1
sa_stat == sa_stat_fini
return all pixels within selected area
Fpaintedits = 1
return all pixela within mouse radius Mradius
blend calculation for pixel at Pradius from mouse position:
blend = MCpower + (MEpower - MCpower) * Pradius / Mradius
where MCpower/MEpower is edit power at center/edge of mouse circle
and Pradius/Mradius = pixel/circle radius
initial call:
get_edit_pixels_init(Nthreads,margin)
subsequent calls (pixel processing loop)
stat = get_edit_pixels(int thread, int &px, int &py, float &blend)
stat = 0 next pixel is returned
1 finished, no more pixels
****/
namespace get_edit_pixels_names
{
int Nt;
int FTF[Xsmp];
int pxlo, pxhi, pylo, pyhi;
float Cpow, Epow;
}
// initialization for returning pixels to the edit threads
void get_edit_pixels_init(int Nthreads, int margin)
{
using namespace get_edit_pixels_names;
int cc;
if (! CEF) zappcrash("no edit function active");
if (! E3pxm) zappcrash("no E3pxm");
cc = NSMP * sizeof(int); // set first time flags for all threads
memset(FTF,1,cc);
if (! Nthreads) return; // re-initz. same area
Nt = Nthreads; // edit thread count
if (Fpaintedits) // paint_edit() is active,
{ // process pixels in mouse circle
Cpow = 0.01 * MCpower;
Epow = 0.01 * MEpower;
pxlo = Mxposn - Mradius;
if (pxlo < 0) pxlo = 0;
pxlo += margin;
pxhi = Mxposn + Mradius;
if (pxhi > Eww) pxhi = Eww;
pxhi -= margin;
pylo = Myposn - Mradius;
if (pylo < 0) pylo = 0;
pylo += margin;
pyhi = Myposn + Mradius;
if (pyhi > Ehh) pyhi = Ehh;
pyhi -= margin;
return;
}
if (sa_stat == sa_stat_fini) // select area is valid and enabled,
{ // process pixels within area
pxlo = sa_minx + margin;
pxhi = sa_maxx - margin;
pylo = sa_miny + margin;
pyhi = sa_maxy - margin;
return;
}
pxlo = pylo = margin; // process entire image
pxhi = Eww - margin;
pyhi = Ehh - margin;
editpixels_pxlo = pxlo; // initz. externals
editpixels_pxhi = pxhi;
editpixels_pylo = pylo;
editpixels_pyhi = pyhi;
return;
}
// return next pixel for edit mode: full image, area, mouse paint
int get_edit_pixels(int thread, int &px, int &py, float &blend)
{
using namespace get_edit_pixels_names;
int ii, dist;
float Pradius;
if (Fpaintedits) // edit within mouse circle
{
if (FTF[thread])
{
FTF[thread] = 0;
px = pxlo; // initial px/py
py = pylo + thread;
if (py >= pyhi) return 1; // fini
}
while (true)
{
px++; // next px/py
if (px >= pxhi) {
px = pxlo;
py += Nt;
if (py >= pyhi) return 1; // end return
}
if (sa_stat == sa_stat_fini) { // select area active
ii = Eww * py + px;
dist = sa_pixmap[ii];
if (! dist) continue; // ignore pixels outside area
}
Pradius = (px - Mxposn) * (px - Mxposn); // get (px,py) discance from mouse center
Pradius += (py - Myposn) * (py - Myposn);
Pradius = sqrtf(Pradius);
if (Pradius < Mradius) break; // pixel within mouse radius, keep
}
blend = Cpow + (Epow - Cpow) * Pradius / Mradius; // Cpow to Epow for center to edge pixel
blend = blend * blend; // quadratic scale
if (Mbutton > 2) blend = -blend; // left/right button == paint/erase edit
return 0;
}
if (sa_stat == sa_stat_fini) // edit within select area
{
if (FTF[thread])
{
FTF[thread] = 0;
px = pxlo; // initial px/py
py = pylo + thread;
if (py >= pyhi) return 1; // fini
}
while (true)
{
px++; // next px/py
if (px >= pxhi) {
px = pxlo;
py += Nt;
if (py >= pyhi) return 1; // fini
}
ii = Eww * py + px; // blend = 0/1 for pixel out/in area
dist = sa_pixmap[ii];
if (! dist) continue; // ignore pixels outside area
blend = 1;
return 0;
}
}
blend = 1; // full image edit
if (FTF[thread])
{
FTF[thread] = 0;
px = pxlo; // initial px/py
py = pylo + thread;
if (py < pyhi) return 0;
return 1; // fini
}
px++; // next px/py
if (px < pxhi) return 0;
px = pxlo;
py += Nt;
if (py < pyhi) return 0;
return 1; // fini
}
/********************************************************************************/
// rotate or level an image
namespace rotate_names
{
editfunc EFrotateimage;
zdialog *zd;
double rotate_goal, rotate_angle; // target and actual rotation
int mirror; // mirror: horizontal or vertical
int Ftimer1;
double Ftimer2;
int timerfunc(void *);
int dialog_event(zdialog *zd, ch *event);
void mousefunc();
void KBfunc(int key);
void rotate_final();
void rotate_func(int fast);
void drawlines(cairo_t *cr);
}
// menu function
void m_rotate(GtkWidget *, ch *menu)
{
using namespace rotate_names;
ch *rotate_message = "Drag on image to level image.\n"
"Click on image for level lines.";
F1_help_topic = "rotate";
Plog(1,"m_rotate \n");
EFrotateimage.menufunc = m_rotate; // menu function
EFrotateimage.menuname = "Rotate";
EFrotateimage.mousefunc = mousefunc;
EFrotateimage.FprevReq = 1; // use preview (small) size
if (! edit_setup(EFrotateimage)) return; // setup edit
PXM_addalpha(E0pxm);
PXM_addalpha(E1pxm);
PXM_addalpha(E3pxm);
rotate_goal = rotate_angle = 0; // initially no rotation
/***
______________________________________
| |
| Drag on image to level image. |
| Click on image for level lines. |
| |
| Rotate: [-90°] [+90°] [180°] [ 0 ] |
| Mirror: [] horizontal [] vertical |
| [Auto Level] [Auto Upright] |
| |
| [Crop] [OK] [X] |
|______________________________________|
***/
zd = zdialog_new("Rotate Image",Mwin,"Crop","OK"," X ",null);
EFrotateimage.zd = zd;
zdialog_add_widget(zd,"label","labrotate","dialog",rotate_message,"space=3");
zdialog_add_widget(zd,"hbox","hbrot","dialog",0,"space=3");
zdialog_add_widget(zd,"label","labrotate","hbrot","Rotate:","space=5");
zdialog_add_widget(zd,"imagebutt","-90","hbrot","rotate-left.png","size=32|space=5");
zdialog_add_widget(zd,"imagebutt","+90","hbrot","rotate-right.png","size=32|space=5");
zdialog_add_widget(zd,"button","180","hbrot","180","space=5");
zdialog_add_widget(zd,"zspin","degrees","hbrot","-180|180|0.1|0","space=5");
zdialog_add_widget(zd,"hbox","hbmir","dialog",0,"space=3");
zdialog_add_widget(zd,"label","labmir","hbmir","Mirror:","space=5");
zdialog_add_widget(zd,"zbutton","horz","hbmir","horizontal","space=5");
zdialog_add_widget(zd,"zbutton","vert","hbmir","vertical","space=5");
zdialog_add_widget(zd,"hbox","hbauto","dialog",0,"space=3");
zdialog_add_widget(zd,"button","level","hbauto","Auto Level","space=10");
zdialog_add_widget(zd,"button","upright","hbauto","Auto Upright","space=8");
zdialog_add_ttip(zd,"level","level based on EXIF roll angle");
zdialog_add_ttip(zd,"upright","upright based on EXIF orientation");
takeMouse(mousefunc,dragcursor); // connect mouse function
Fzoom = 0;
zdialog_run(zd,dialog_event,"save"); // run dialog - parallel
g_timeout_add(100,timerfunc,0); // start rotate watchdog
Ftimer1 = 1; // rotate() active
Ftimer2 = 0; // rotate fix pending
return;
}
// watchdog timer function - watch for fast low-quality rotates
// and fix them when rotation has apparently ended.
int rotate_names::timerfunc(void *)
{
if (! Ftimer1) return 0; // trim_rotate() ended
if (! Ftimer2) return 1; // nothing pending
if (get_seconds() - Ftimer2 < 1.0) return 1; // < 1 sec. since last rotate
Ftimer2 = 0; // reset pending flag
rotate_angle = 0;
rotate_func(0); // high quality rotate
return 1;
}
// dialog event and completion callback function
int rotate_names::dialog_event(zdialog *zd, ch *event)
{
using namespace rotate_names;
int angle = 0, Fcrop = 0;
double fangle;
ch *metakey[1];
ch *ppv[1];
if (strmatch(event,"done")) zd->zstat = 2; // apply and quit
if (strmatch(event,"cancel")) zd->zstat = 3; // cancel
if (strmatch(event,"undo")) // 'undo' button was used
{
Ftimer1 = Ftimer2 = 0; // stop watchdog timer
Ntoplines = 0; // remove leveling lines
edit_reset();
zdialog_stuff(zd,"degrees",0);
rotate_goal = rotate_angle = 0;
return 1;
}
if (strmatch(event,"focus")) {
takeMouse(mousefunc,dragcursor); // re-connect mouse function
return 1;
}
if (zd->zstat) // dialog complete
{
Ftimer1 = Ftimer2 = 0; // stop watchdog timer
Ntoplines = 0; // remove leveling lines
if (zd->zstat == 1 || zd->zstat == 2) // [crop] or [OK]
{
if (zd->zstat == 1) Fcrop = 1; // remember if crop
if (rotate_goal == 0) { // no rotate was done
edit_cancel(0);
if (Fcrop) m_crop(0,0); // [crop] was pressed
return 1;
}
edit_fullsize(); // get full size image
rotate_angle = 0; // reset to no rotation
rotate_func(0); // final rotate, high quality
fangle = fabs(rotate_angle);
if (fangle != 90 && fangle != 180) { // small auto sharpen if not 90/180
sharp_AC(30,20);
Plog(1,"small auto sharpen done \n");
}
Fupright = Flevel = 1; // mark uprighted (for metadata update)
edit_addhist("angle:%.1f",rotate_angle); // edit parms > edit hist
CEF->Fmods++; // finish edit
edit_done(0);
if (Fcrop) m_crop(0,"auto"); // [crop] was pressed
return 1;
}
edit_cancel(0); // cancel
return 1;
}
if (zstrstr("+90 -90 180",event)) // [-90] [+90] [180] button
{
if (strmatch(event,"+90")) // 90 deg. CW
rotate_goal += 90;
if (strmatch(event,"-90")) // 90 deg. CCW
rotate_goal -= 90;
if (strmatch(event,"180"))
rotate_goal += 180;
if (rotate_goal > 180) rotate_goal -= 360; // keep within -180 to +180 deg.
if (rotate_goal < -180) rotate_goal += 360;
if (fabs(rotate_goal) < 0.01) rotate_goal = 0; // = 0 within my precision
zdialog_stuff(zd,"degrees",rotate_goal);
rotate_func(0); // high quality rotate
Fupright = Flevel = 1; // mark uprighted (for metadata update)
}
if (strmatch(event,"degrees")) // manual input degrees
{
zdialog_fetch(zd,"degrees",rotate_goal);
if (rotate_goal > 180) rotate_goal -= 360; // keep within -180 to +180 deg.
if (rotate_goal < -180) rotate_goal += 360;
if (fabs(rotate_goal) < 0.01) rotate_goal = 0; // = 0 within my precision
zdialog_stuff(zd,"degrees",rotate_goal);
rotate_func(1); // fast low quality rotate
Fupright = Flevel = 1; // mark uprighted (for metadata update)
}
if (strmatch(event,"level")) // auto level using EXIF RollAngle
{
metakey[0] = (ch *) meta_rollangle_key;
meta_get1(curr_file,metakey,ppv,1);
if (! ppv[0]) {
zmessageACK(Mwin,"no metadata level data");
return 1;
}
rotate_goal = atofz(ppv[0]);
zfree(ppv[0]);
rotate_func(0); // high quality rotate
Fupright = Flevel = 1; // mark uprighted (for metadata update)
}
if (strstr("horz vert",event)) // horizontal or vertical mirror
{
if (strmatch(event,"horz")) mirror = 1;
if (strmatch(event,"vert")) mirror = 2;
PXM_free(E3pxm);
E3pxm = PXM_upright(E0pxm,0,mirror); // mirror image
Eww = E3pxm->ww;
Ehh = E3pxm->hh;
Fupright = Flevel = 1; // mark uprighted (for metadata update)
CEF->Fmods++;
Fpaintnow();
edit_done(0); // do not restart m_rotate()
}
if (strmatch(event,"upright")) // auto upright button
{
metakey[0] = (ch *) meta_orientation_key;
meta_get1(curr_file,metakey,ppv,1); // get metadata Orientation
if (! ppv[0] || *ppv[0] <= '1') {
zmessageACK(Mwin,"no EXIF orientation data");
return 1;
}
if (*ppv[0] == '2') { angle = 0; mirror = 1; } // horizontal mirror
if (*ppv[0] == '3') { angle = 180; mirror = 0; } // rotate 180
if (*ppv[0] == '4') { angle = 0; mirror = 2; } // vertical mirror
if (*ppv[0] == '5') { angle = 90; mirror = 1; } // rotate 90 + horizontal mirror
if (*ppv[0] == '6') { angle = 90; mirror = 0; } // rotate 90
if (*ppv[0] == '7') { angle = 270; mirror = 1; } // rotate 270 + horizontal mirror
if (*ppv[0] == '8') { angle = 270; mirror = 0; } // rotate 270
zfree(ppv[0]);
PXM_free(E3pxm);
E3pxm = PXM_upright(E0pxm,angle,mirror); // do rotate and mirro
Eww = E3pxm->ww;
Ehh = E3pxm->hh;
Fupright = Flevel = 1; // mark uprighted (for metadata update)
CEF->Fmods++;
Fpaintnow();
edit_done(0); // do not restart m_rotate()
}
return 1;
}
// rotate image mouse function
void rotate_names::mousefunc()
{
using namespace rotate_names;
int mpx, mpy, xdrag, ydrag;
int cx;
zdialog *zd = EFrotateimage.zd;
mpx = mpy = xdrag = ydrag = 0; // avoid gcc warnings
if (LMclick) // add vertical and horizontal
{ // lines at click position
LMclick = 0;
Ntoplines = 2;
toplines[0].x1 = Mxclick;
toplines[0].y1 = 0;
toplines[0].x2 = Mxclick;
toplines[0].y2 = Ehh-1;
toplines[0].type = 3; // black/white pair
toplines[1].x1 = 0;
toplines[1].y1 = Myclick;
toplines[1].x2 = Eww-1;
toplines[1].y2 = Myclick;
toplines[1].type = 3;
Fpaint2();
return;
}
if (RMclick) // remove the lines
{
RMclick = 0;
Ntoplines = 0;
Fpaint2();
return;
}
if (Mxdrag || Mydrag) // drag underway
{
mpx = Mxdrag;
mpy = Mydrag;
xdrag = Mxdrag - Mxdown;
ydrag = Mydrag - Mydown;
Mxdown = Mxdrag; // reset drag origin
Mydown = Mydrag;
Mxdrag = Mydrag = 0;
}
cx = Eww / 2; // image center
if (mpx > cx) rotate_goal += 30.0 * ydrag / Eww;
else rotate_goal -= 30.0 * ydrag / Eww;
if (rotate_goal > 180) rotate_goal -= 360; // keep within -180 to +180 deg.
if (rotate_goal < -180) rotate_goal += 360;
if (fabs(rotate_goal) < 0.01) rotate_goal = 0; // = 0 within my precision
zdialog_stuff(zd,"degrees",rotate_goal); // update dialog
rotate_func(1); // fast low quality rotate
return;
}
// rotate function
// E3 = E1 with rotation
// fast: 0 = slow / high quality
// 1 = fast / low quality
void rotate_names::rotate_func(int fast)
{
using namespace rotate_names;
zdialog *zd = EFrotateimage.zd;
static int Fbusy = 0;
if (rotate_goal == rotate_angle) return;
if (Fbusy) return; // stop re-entry (mouse event)
Fbusy = 1;
if (fabs(rotate_goal) < 0.01) { // insignificant rotation
zdialog_stuff(zd,"degrees",0.0);
PXM_free(E3pxm); // E1 >> E3
E3pxm = PXM_copy(E1pxm);
rotate_goal = 0.0;
}
else {
PXM_free(E3pxm);
E3pxm = PXM_rotate(E1pxm,rotate_goal,fast); // rotate (fast or high quality)
}
Eww = E3pxm->ww;
Ehh = E3pxm->hh;
rotate_angle = rotate_goal;
if (fast) Ftimer2 = get_seconds(); // flag watchdog timer to come back
else Ftimer2 = 0;
CEF->Fmods++;
Fpaintnow();
Fbusy = 0;
return;
}
/********************************************************************************/
// auto upright a rotated image
// menu function - not an edit function
void m_upright(GtkWidget *, ch *menu)
{
ch *metakey[1] = { meta_orientation_key };
ch orient, *ppv[1];
ch *pp, *newfile, *delfile;
int err, angle, mirror;
F1_help_topic = "upright";
Plog(1,"m_upright \n");
if (! curr_file) {
zmessageACK(Mwin,"no current file");
return;
}
meta_get1(curr_file,(ch **) metakey,ppv,1); // get metadata Orientation
if (! ppv[0]) {
zmessageACK(Mwin,"no EXIF orientation data");
return;
}
orient = *ppv[0];
zfree(ppv[0]);
if (orient < '1' || orient > '8') {
zmessageACK(Mwin,"invalid EXIF orientation - use Rotate function");
return;
}
angle = mirror = 0;
if (orient == '1') { angle = 0; mirror = 0; } // do nothing 24.50
if (orient == '2') { angle = 0; mirror = 1; } // horizontal mirror
if (orient == '3') { angle = 180; mirror = 0; } // rotate 180
if (orient == '4') { angle = 0; mirror = 2; } // vertical mirror
if (orient == '5') { angle = 90; mirror = 1; } // rotate 90 + horizontal mirror
if (orient == '6') { angle = 90; mirror = 0; } // rotate 90
if (orient == '7') { angle = 270; mirror = 1; } // rotate 270 + horizontal mirror
if (orient == '8') { angle = 270; mirror = 0; } // rotate 270
err = f_open(curr_file);
if (err) return;
E0pxm = PXM_load(curr_file,1); // load poss. 16-bit image
if (! E0pxm) return;
E3pxm = PXM_upright(E0pxm,angle,mirror); // do rotate/mirror
PXM_free(E0pxm);
E0pxm = E3pxm;
E3pxm = 0;
if (strcasestr("jp2 heic avif webp",curr_file_type)) { // save these types as .jpg
newfile = zstrdup(curr_file,"batch_upright",16);
delfile = zstrdup(curr_file,"batch_upright");
pp = strrchr(newfile,'.');
strcpy(pp,"-upright.jpg");
Fupright = 1; // mark uprighted (for metadata update)
f_save(newfile,"jpg",8,0,1); // make .jpg duplicate
f_open(newfile); // show uprighted file
remove(delfile);
zfree(newfile);
zfree(delfile);
}
else {
Fupright = 1; // mark uprighted (for metadata update)
f_save(curr_file,curr_file_type,curr_file_bpc,0,1); // replace file
f_open(curr_file); // force full open 24.40
}
gallery(curr_file,"init",-1);
return;
}
/********************************************************************************/
// crop an image (trim margins)
//
// fotocx.h
// int cropx1, cropy1, cropx2, cropy2; // crop rectangle (calling function)
// ch *cropsizes[10]; // previous crop sizes "NNNNxNNNN"
// ch *cropbuttons[5]; // crop dialog button labels
// ch *cropratios[5]; // corresponding aspect ratios
namespace crop_names
{
editfunc EFcrop;
zdialog *zd;
int pcropx1, pcropy1, pcropx2, pcropy2; // prior crop rectangle
int cropww, crophh; // crop rectangle width and height
int Rlock; // lock width/height ratio
int Rauto; // margin confine with ratio locked
int E0ww, E0hh; // full size image
int Fcorner, Fside, Fcenter;
int KBcorner, KBside;
double cropR; // crop ratio, width/height
double Zratio; // full / preview size >= 1.0
int dialog_event(zdialog *zd, ch *event);
void prevcrop_menu_event(GtkWidget *, ch *menu);
void update_prev_cropsize();
void mousefunc();
void KBfunc(int key);
void crop_limits();
void crop_darkmargins();
void crop_final();
void drawlines(cairo_t *cr);
void autocrop_max();
void autocrop_ratio();
void crop_customize();
}
// menu function
void m_crop(GtkWidget *, ch *menu) // more accurate control of crop size
{
using namespace crop_names;
int ii, xmargin, ymargin;
ch *crop_message = "Drag margin box middle or edges";
ch text[20];
F1_help_topic = "crop";
Plog(1,"m_crop \n");
EFcrop.menufunc = m_crop; // menu function
EFcrop.menuname = "Crop";
EFcrop.mousefunc = mousefunc;
EFcrop.FprevReq = 1; // use preview (small) size
if (! edit_setup(EFcrop)) return; // setup edit
PXM_addalpha(E0pxm);
PXM_addalpha(E1pxm);
PXM_addalpha(E3pxm);
E0ww = E0pxm->ww; // full-size image dimensions
E0hh = E0pxm->hh; // (preview size = Eww/Ehh)
Zratio = 1.0 * (E0ww + E0hh) / (Eww + Ehh); // full / preview size >= 1.0
if (E9pxm) PXM_free(E9pxm); // E9 invalid
E9pxm = 0;
cropww = 0.8 * E0ww; // initial crop rectangle, 80% image size
crophh = 0.8 * E0hh;
xmargin = 0.5 * (E0ww - cropww); // set balanced margins
ymargin = 0.5 * (E0hh - crophh);
cropx1 = xmargin;
cropx2 = E0ww - xmargin;
cropy1 = ymargin;
cropy2 = E0hh - ymargin;
cropR = 1.0 * cropww / crophh; // crop ratio = width/height
pcropx1 = 0; // set prior crop rectangle
pcropy1 = 0; // = 100% of image
pcropx2 = E0ww;
pcropy2 = E0hh;
Rlock = Rauto = 0;
/***
____________________________________________
| |
| Drag margin box middle or edges |
| |
| Auto crop: [Max] [Prev] |
| Set Width: [____] Height: [____] |
| W/H Ratio: 1.778 [x] Lock [x] Auto |
| Set Ratio: [5:4] [4:3] [8:5] [16:9] [2:1] |
| [Invert] [Customize] |
| |
| [Rotate] [OK] [X] |
|____________________________________________|
***/
zd = zdialog_new("Crop Image",Mwin,"Rotate","OK"," X ",null);
EFcrop.zd = zd;
zdialog_add_widget(zd,"label","labcrop","dialog",crop_message,"space=5");
zdialog_add_widget(zd,"hbox","hbcrop","dialog",0,"space=3");
zdialog_add_widget(zd,"label","labauto","hbcrop","Auto crop:","space=5");
zdialog_add_widget(zd,"button","max","hbcrop","Max","space=8");
zdialog_add_widget(zd,"button","prev","hbcrop","Prev","space=8");
zdialog_add_widget(zd,"hbox","hbwh","dialog",0,"space=3");
zdialog_add_widget(zd,"label","labW","hbwh","Set Width:","space=5");
zdialog_add_widget(zd,"zspin","width","hbwh","20|30000|1|1000"); // fotocx.h limits
zdialog_add_widget(zd,"label","space","hbwh",0,"space=5");
zdialog_add_widget(zd,"label","labH","hbwh","Height:","space=5");
zdialog_add_widget(zd,"zspin","height","hbwh","20|30000|1|600");
zdialog_add_widget(zd,"hbox","hbrat","dialog",0,"space=3");
zdialog_add_widget(zd,"label","labR","hbrat","W/H Ratio:","space=5");
zdialog_add_widget(zd,"label","cropR","hbrat","1.667");
zdialog_add_widget(zd,"check","Rlock","hbrat","Lock","space=15");
zdialog_add_widget(zd,"check","Rauto","hbrat","Auto","space=12");
zdialog_add_widget(zd,"hbox","hbsr1","dialog",0,"space=3");
zdialog_add_widget(zd,"label","labsr1","hbsr1","Set Ratio:","space=5");
for (ii = 0; ii < 5; ii++)
zdialog_add_widget(zd,"button",cropbuttons[ii],"hbsr1",cropbuttons[ii],"space=5");
zdialog_add_widget(zd,"hbox","hbsr2","dialog",0,"space=3");
zdialog_add_widget(zd,"label","space","hbsr2",0,"space=50");
zdialog_add_widget(zd,"button","invert","hbsr2","Invert","space=5");
zdialog_add_widget(zd,"button","custom","hbsr2","Customize","space=5");
zdialog_add_ttip(zd,"max","set maximum crop");
zdialog_add_ttip(zd,"prev","use previous input");
zdialog_add_ttip(zd,"Rlock","lock width/height ratio");
zdialog_add_ttip(zd,"Rauto","confine within transparent margins");
zd = EFcrop.zd;
zdialog_load_inputs(zd); // restore all prior inputs
zdialog_stuff(zd,"width",cropww); // stuff width, height, ratio as
zdialog_stuff(zd,"height",crophh); // pre-calculated for this image
snprintf(text,20,"%.3f ",cropR);
zdialog_stuff(zd,"cropR",text);
zdialog_stuff(zd,"Rlock",0); // no lock ratio
zdialog_stuff(zd,"Rauto",0); // no margin confine
takeMouse(mousefunc,dragcursor); // connect mouse function
Fzoom = 0;
crop_darkmargins();
zdialog_run(zd,dialog_event,"save"); // run dialog - parallel
if (menu && strmatch(menu,"auto")) // crop after rotate
zdialog_send_event(zd,"max");
return;
}
// dialog event and completion callback function
int crop_names::dialog_event(zdialog *zd, ch *event)
{
using namespace crop_names;
static GtkWidget *popmenu = 0;
static int flip = 0;
int ii, Frotate = 0;
int width, height, delta;
double r1, r2, Fratio = 0;
ch text[20];
ch *pp;
if (strmatch(event,"done")) zd->zstat = 2; // apply and quit
if (strmatch(event,"cancel")) zd->zstat = 3; // cancel
if (strmatch(event,"focus")) {
takeMouse(mousefunc,dragcursor); // re-connect mouse function
return 1;
}
if (zd->zstat) // dialog complete
{
if (zd->zstat == 1 || zd->zstat == 2) // [rotate] or [OK]
{
if (zd->zstat == 1) Frotate = 1; // remember if [rotate]
draw_toplines(2,0); // erase crop rectangle
erase_topcircles(); // erase center circle
edit_fullsize(); // get full size image (Eww/Ehh)
Zratio = 1.0;
crop_final(); // rotate/crop final image
update_prev_cropsize(); // update prev. crop size memory
edit_addhist("width:%d height:%d",cropww,crophh); // edit parms > edit hist
CEF->Fmods++; // finish edit
edit_done(0);
if (Frotate) m_rotate(0,0); // start rotate function
return 1;
}
draw_toplines(2,0); // cancel - erase crop rectangle
erase_topcircles(); // erase center circle
edit_cancel(0);
return 1;
}
zdialog_fetch(zd,"width",width); // full image scale, E0
zdialog_fetch(zd,"height",height);
zdialog_fetch(zd,"Rlock",Rlock); // get w/h ratio lock, on/off
zdialog_fetch(zd,"Rauto",Rauto); // get margin confine lock, on/off
if (strmatch(event,"Rlock") && ! Rlock) { // if Rlock off, set Rauto off
Rauto = 0;
zdialog_stuff(zd,"Rauto",0);
}
if (strmatch(event,"Rauto") && Rauto) { // if Rauto, set Rlock
Rlock = 1;
zdialog_stuff(zd,"Rlock",1);
autocrop_ratio(); // find max crop within margins
crop_darkmargins();
return 1;
}
Fcorner = Fside = KBcorner = KBside = 0; // no mouse or KB action underway
if (strmatch(event,"max")) // auto crop, max within margins
{
zdialog_stuff(zd,"Rlock",0); // set no ratio lock
zdialog_stuff(zd,"Rauto",0); // set no margin confine
Rlock = Rauto = 0;
autocrop_max(); // do max auto-crop
zdialog_stuff(zd,"width",cropww); // update dialog values
zdialog_stuff(zd,"height",crophh);
cropR = 1.0 * cropww / crophh; // new w/h ratio
snprintf(text,20,"%.3f ",cropR);
zdialog_stuff(zd,"cropR",text);
crop_darkmargins(); // show crop area in image
return 1;
}
if (strmatch(event,"prev")) // [Prev] button - get prev. crop size
{
if (popmenu) gtk_widget_destroy(popmenu);
popmenu = create_popmenu(); // make popup menu with crop sizes
for (int ii = 0; ii < 10; ii++)
if (cropsizes[ii])
add_popmenu_item(popmenu,cropsizes[ii],prevcrop_menu_event,0,0);
popup_menu(Mwin,popmenu);
return 1;
}
if (zstrstr("width height",event)) // width or height input
{
zdialog_stuff(zd,"Rauto",0); // set no margin confine
Rauto = 0;
if (width < 20) width = 20;
if (width > E0ww) width = E0ww;
if (height < 20) height = 20;
if (height > E0hh) height = E0hh;
if (Rlock) // w/h ratio lock
{
if (strmatch(event,"width"))
height = width / cropR + 0.5;
if (strmatch(event,"height"))
width = height * cropR + 0.5;
if (width > E0ww) {
width = E0ww;
height = width / cropR + 0.5;
}
if (height > E0hh) {
height = E0hh;
width = height * cropR + 0.5;
}
if (width > E0ww) width = E0ww; // rounding could put it over limit
if (height > E0hh) height = E0hh;
}
zdialog_stuff(zd,"width",width); // adjusted for ratio lock
zdialog_stuff(zd,"height",height);
flip = 1 - flip; // alternates 0, 1, 0, 1 ...
delta = width - cropww; // change in width
if (delta > 0) { // increased width
cropx1 = cropx1 - delta / 2; // left and right sides equally
cropx2 = cropx2 + delta / 2;
if (delta % 2) { // if increase is odd
cropx1 = cropx1 - flip; // add 1 alternatively to each side
cropx2 = cropx2 + 1 - flip;
}
if (cropx1 < 0) { // add balance to upper limit
cropx2 = cropx2 - cropx1;
cropx1 = 0;
}
if (cropx2 > E0ww) { // add balance to lower limit
cropx1 = cropx1 - cropx2 + E0ww;
cropx2 = E0ww;
}
}
if (delta < 0) { // decreased width
cropx1 = cropx1 - delta / 2;
cropx2 = cropx2 + delta / 2;
if (delta % 2) {
cropx1 = cropx1 + flip;
cropx2 = cropx2 - 1 + flip;
}
}
if (cropx1 < 0) cropx1 = 0; // keep within limits
if (cropx2 > E0ww) cropx2 = E0ww; // use ww not ww-1
cropww = cropx2 - cropx1;
delta = height - crophh; // change in height
if (delta > 0) { // increased height
cropy1 = cropy1 - delta / 2; // top and bottom sides equally
cropy2 = cropy2 + delta / 2;
if (delta % 2) { // if increase is odd
cropy1 = cropy1 - flip; // add 1 alternatively to each side
cropy2 = cropy2 + 1 - flip;
}
if (cropy1 < 0) {
cropy2 = cropy2 - cropy1;
cropy1 = 0;
}
if (cropy2 > E0hh) {
cropy1 = cropy1 - cropy2 + E0hh;
cropy2 = E0hh;
}
}
if (delta < 0) { // decreased height
cropy1 = cropy1 - delta / 2;
cropy2 = cropy2 + delta / 2;
if (delta % 2) {
cropy1 = cropy1 + flip;
cropy2 = cropy2 - 1 + flip;
}
}
if (cropy1 < 0) cropy1 = 0; // keep within limits
if (cropy2 > E0hh) cropy2 = E0hh; // use hh not hh-1
crophh = cropy2 - cropy1;
if (! Rlock) {
cropR = 1.0 * width / height; // new w/h ratio
snprintf(text,20,"%.3f ",cropR);
zdialog_stuff(zd,"cropR",text);
}
crop_darkmargins(); // show crop area in image
return 1;
}
if (! Rlock) cropR = 1.0 * width / height;
snprintf(text,20,"%.3f ",cropR); // update ratio
zdialog_stuff(zd,"cropR",text);
for (ii = 0; ii < 5; ii++) // crop ratio buttons
if (strmatch(event,cropbuttons[ii])) break;
if (ii < 5) {
r1 = r2 = Fratio = 0; // ratio button pressed
pp = substring(cropratios[ii],':',1);
if (pp) r1 = atofz(pp);
pp = substring(cropratios[ii],':',2);
if (pp) r2 = atofz(pp);
if (r1 > 0 && r2 > 0) Fratio = r1/r2;
if (Fratio < 0.1 || Fratio > 10) Fratio = 1.0;
zdialog_stuff(zd,"Rlock",1); // assume lock is wanted
Rlock = 1;
}
if (strmatch(event,"invert")) // [invert] ratio button
Fratio = 1.0 * crophh / cropww;
if (Fratio) // w/h ratio was changed
{
cropR = Fratio;
if (cropx2 - cropx1 > cropy2 - cropy1) // adjust smaller dimension
cropy2 = cropy1 + (cropx2 - cropx1) / cropR + 0.5; // round
else
cropx2 = cropx1 + (cropy2 - cropy1) * cropR + 0.5;
if (cropx2 > E0ww) { // if off the right edge,
cropx2 = E0ww; // adjust height
cropy2 = cropy1 + (cropx2 - cropx1) / cropR + 0.5;
}
if (cropy2 > E0hh) { // if off the bottom edge,
cropy2 = E0hh; // adjust width
cropx2 = cropx1 + (cropy2 - cropy1) * cropR + 0.5;
}
cropww = cropx2 - cropx1; // new rectangle dimensions
crophh = cropy2 - cropy1;
if (cropww > crophh) crophh = cropww / cropR + 0.5;
else cropww = crophh * cropR + 0.5;
zdialog_stuff(zd,"width",cropww); // stuff width, height, ratio
zdialog_stuff(zd,"height",crophh);
snprintf(text,20,"%.3f ",cropR);
zdialog_stuff(zd,"cropR",text);
crop_darkmargins(); // update crop area in image
return 1;
}
if (strmatch(event,"custom")) { // [customize] button
draw_toplines(2,0); // erase crop rectangle
erase_topcircles(); // erase center circle
edit_cancel(0); // cancel edit
crop_customize(); // customize dialog
m_crop(0,0); // restart function
return 1;
}
return 1;
}
// crop size popup menu event function
void crop_names::prevcrop_menu_event(GtkWidget *, ch *menu)
{
using namespace crop_names;
int nn, width, height;
zdialog *zd;
nn = sscanf(menu,"%dx%d",&width,&height); // chosen crop size, "NNNNxNNNN"
if (nn != 2) return;
zd = EFcrop.zd;
zdialog_stuff(zd,"width",width); // update dialog
zdialog_stuff(zd,"height",height);
zdialog_send_event(zd,"width");
zdialog_stuff(zd,"Rlock",0); // set no ratio lock
zdialog_stuff(zd,"Rauto",0); // set no margin confine
Rlock = Rauto = 0;
return;
}
// update memory of previously used crop sizes, for future use
void crop_names::update_prev_cropsize()
{
using namespace crop_names;
int ii;
ch mysize[20];
snprintf(mysize,20,"%dx%d",cropww,crophh); // this crop size used, "NNNNxNNNN"
for (ii = 0; ii < 10; ii++) {
if (! cropsizes[ii]) break; // check past sizes list for same size
if (strmatch(cropsizes[ii],mysize)) break;
}
if (ii < 10 && cropsizes[ii]) { // found in list
zfree(cropsizes[ii]); // remove from list
for (ii++; ii < 10; ii++) // pack down remaining entries
cropsizes[ii-1] = cropsizes[ii];
cropsizes[9] = 0; // last = null
}
if (cropsizes[9]) zfree(cropsizes[9]); // if entry 9 exists, discard
for (ii = 9; ii > 0; ii--) // move entries 0-8 to 1-9
cropsizes[ii] = cropsizes[ii-1];
cropsizes[0] = zstrdup(mysize,"crop"); // entry 0 = this size
return;
}
// mouse function - move crop rectangle via mouse drags
void crop_names::mousefunc()
{
using namespace crop_names;
int mpx, mpy, xdrag, ydrag;
int d1, d2, d3, d4;
int dc, ds, dx, dy, dz;
int cx, cy, close;
float fdc;
mpx = mpy = xdrag = ydrag = 0; // avoid gcc warnings
if (! LMclick && ! RMclick && ! Mxdrag && ! Mydrag) { // no click or drag underway
Fcorner = Fside = Fcenter = 0;
return;
}
if (Mxdrag || Mydrag) // continue drag underway
{
mpx = Mxdrag;
mpy = Mydrag;
xdrag = Mxdrag - Mxdown;
ydrag = Mydrag - Mydown;
Mxdown = Mxdrag; // reset drag origin
Mydown = Mydrag;
Mxdrag = Mydrag = 0;
xdrag = xdrag * Zratio; // scale mouse drag
ydrag = ydrag * Zratio; // to full image size
if (Rlock && Rauto) { // move crop rectangle within margins
cropx1 += xdrag; // and keep locked w/h ratio
cropx2 += xdrag;
cropy1 += ydrag;
cropy2 += ydrag;
autocrop_ratio();
goto crop_finish;
}
}
mpx = mpx * Zratio + 0.5; // scale mouse position
mpy = mpy * Zratio + 0.5; // to full image size
if (Fcorner) // continue drag corner
{
if (Fcorner == 1) { cropx1 = mpx; cropy1 = mpy; }
if (Fcorner == 2) { cropx2 = mpx; cropy1 = mpy; }
if (Fcorner == 3) { cropx2 = mpx; cropy2 = mpy; }
if (Fcorner == 4) { cropx1 = mpx; cropy2 = mpy; }
goto crop_finish;
}
if (Fside) // continue drag side
{
if (Fside == 1) cropx1 = mpx;
if (Fside == 2) cropy1 = mpy;
if (Fside == 3) cropx2 = mpx;
if (Fside == 4) cropy2 = mpy;
goto crop_finish;
}
if (Fcenter) // continue drag entire crop rectangle
{
cropx1 += xdrag;
cropx2 += xdrag;
cropy1 += ydrag;
cropy2 += ydrag;
if (cropx1 < 0) cropx1 = 0;
if (cropy1 < 0) cropy1 = 0;
if (cropx2 > E0ww) cropx2 = E0ww;
if (cropy2 > E0hh) cropy2 = E0hh;
goto crop_finish;
}
if (E0ww > E0hh) close = 0.05 * E0ww; // mouse capture threshold
else close = 0.05 * E0hh; // = 5% of image size
dx = mpx - cropx1; // find closest corner/side to mouse
dy = mpy - cropy1;
d1 = sqrt(dx*dx + dy*dy); // distance from NW corner
dx = mpx - cropx2;
dy = mpy - cropy1;
d2 = sqrt(dx*dx + dy*dy); // NE
dx = mpx - cropx2;
dy = mpy - cropy2;
d3 = sqrt(dx*dx + dy*dy); // SE
dx = mpx - cropx1;
dy = mpy - cropy2;
d4 = sqrt(dx*dx + dy*dy); // SW
Fcorner = 1; // find closest corner to mouse
dc = d1; // NW
if (d2 < dc) { Fcorner = 2; dc = d2; } // NE
if (d3 < dc) { Fcorner = 3; dc = d3; } // SE
if (d4 < dc) { Fcorner = 4; dc = d4; } // SW
d1 = abs(mpx - cropx1); // distance from left side
d2 = abs(mpy - cropy1); // top
d3 = abs(mpx - cropx2); // right
d4 = abs(mpy - cropy2); // bottom
Fside = 1; // find closest side to mouse
ds = d1; // left
if (d2 < ds) { Fside = 2; ds = d2; } // top
if (d3 < ds) { Fside = 3; ds = d3; } // right
if (d4 < ds) { Fside = 4; ds = d4; } // bottom
cx = (cropx1 + cropx2) * 0.5; // center of crop rectangle
cy = (cropy1 + cropy2) * 0.5;
dx = mpx - cx; // get mouse distance from 24.10
dy = mpy - cy; // center of crop rectangle
dz = sqrtf(dx * dx + dy * dy);
if (dz < dc && dz < ds) { // if center closer, drag center
Fcorner = Fside = KBcorner = KBside = 0;
if (dz > close) return; // not close enough
Fcenter = 1;
goto crop_finish;
}
Fcenter = 0; // side or corner is closer
fdc = (cropx2 - cropx1 + cropy2 - cropy1); // crop rectangle size
fdc = 1.0 * dc / fdc; // corner distance / rectangle size 24.20
if (fdc < 0.05) Fside = 0; // if near corner, use corner
else Fcorner = 0; // else use closest side
if (Fcorner == 1) { cropx1 = mpx; cropy1 = mpy; } // move this corner to mouse
if (Fcorner == 2) { cropx2 = mpx; cropy1 = mpy; }
if (Fcorner == 3) { cropx2 = mpx; cropy2 = mpy; }
if (Fcorner == 4) { cropx1 = mpx; cropy2 = mpy; }
if (Fside == 1) cropx1 = mpx; // move this side to mouse
if (Fside == 2) cropy1 = mpy;
if (Fside == 3) cropx2 = mpx;
if (Fside == 4) cropy2 = mpy;
KBcorner = Fcorner; // save last corner/side moved
KBside = Fside;
crop_finish:
crop_limits(); // check margin limits and adjust if req.
crop_darkmargins(); // show crop area in image
return;
}
// Keyboard function
// KB arrow keys tweak the last selected corner or side
void crop_names::KBfunc(int key)
{
using namespace crop_names;
int 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;
if (KBcorner == 1) { // NW
cropx1 += xstep;
cropy1 += ystep;
}
if (KBcorner == 2) { // NE
cropx2 += xstep;
cropy1 += ystep;
}
if (KBcorner == 3) { // SE
cropx2 += xstep;
cropy2 += ystep;
}
if (KBcorner == 4) { // SW
cropx1 += xstep;
cropy2 += ystep;
}
if (KBside == 1) cropx1 += xstep; // left
if (KBside == 2) cropy1 += ystep; // top
if (KBside == 3) cropx2 += xstep; // right
if (KBside == 4) cropy2 += ystep; // bottom
crop_limits(); // check margin limits and adjust if req.
crop_darkmargins(); // show crop area in image
return;
}
// check new margins for sanity and enforce locked aspect ratio
void crop_names::crop_limits()
{
using namespace crop_names;
int chop;
int ww, hh, ww2, hh2;
float drr;
ch text[20];
zdialog *zd = EFcrop.zd;
if (cropx1 > cropx2-10) cropx1 = cropx2-10; // sanity limits
if (cropy1 > cropy2-10) cropy1 = cropy2-10;
if (cropx1 < 0) { cropx2 -= cropx1; cropx1 = 0; } // 24.40
if (cropy1 < 0) { cropy2 -= cropy1; cropy1 = 0; }
if (cropx2 > E0ww) { cropx1 -= (cropx2 - E0ww); cropx2 = E0ww; }
if (cropy2 > E0hh) { cropy1 -= (cropy2 - E0hh); cropy2 = E0hh; }
if (Rlock && Fcorner) { // w/h ratio locked
if (Fcorner < 3)
cropy1 = cropy2 - 1.0 * (cropx2 - cropx1) / cropR + 0.5; // round
else
cropy2 = cropy1 + 1.0 * (cropx2 - cropx1) / cropR + 0.5;
}
if (Rlock && Fside) {
ww = cropx2 - cropx1;
hh = cropy2 - cropy1;
ww2 = ww;
hh2 = hh;
if (Fside == 1 || Fside == 3) hh2 = ww / cropR + 0.5;
else ww2 = hh * cropR + 0.5;
ww2 = (ww2 - ww) / 2;
hh2 = (hh2 - hh) / 2;
cropx1 -= ww2;
cropx2 += ww2;
cropy1 -= hh2;
cropy2 += hh2;
}
chop = 0;
if (cropx1 < 0) { // look for off the edge
cropx1 = 0; // after corner move
chop = 1;
}
if (cropx2 > E0ww) {
cropx2 = E0ww;
chop = 2;
}
if (cropy1 < 0) {
cropy1 = 0;
chop = 3;
}
if (cropy2 > E0hh) {
cropy2 = E0hh;
chop = 4;
}
if (Rlock && chop) { // keep ratio if off edge
if (chop < 3)
cropy2 = cropy1 + 1.0 * (cropx2 - cropx1) / cropR + 0.5; // round
else
cropx2 = cropx1 + 1.0 * (cropy2 - cropy1) * cropR + 0.5;
}
if (cropx1 > cropx2-10) cropx1 = cropx2-10; // sanity limits
if (cropy1 > cropy2-10) cropy1 = cropy2-10;
if (cropx1 < 0) cropx1 = 0; // keep within visible area
if (cropx2 > E0ww) cropx2 = E0ww;
if (cropy1 < 0) cropy1 = 0;
if (cropy2 > E0hh) cropy2 = E0hh;
cropww = cropx2 - cropx1; // new rectangle dimensions
crophh = cropy2 - cropy1;
drr = 1.0 * cropww / crophh; // new w/h ratio
if (! Rlock) cropR = drr;
zdialog_stuff(zd,"width",cropww); // stuff width, height, ratio
zdialog_stuff(zd,"height",crophh);
snprintf(text,20,"%.3f ",cropR);
zdialog_stuff(zd,"cropR",text);
return;
}
// Darken image pixels outside of current crop margins.
// messy logic: update pixmaps only for changed pixels to increase speed
void crop_names::crop_darkmargins()
{
using namespace crop_names;
static int Fbusy = 0;
int zx1, zx2, zy1, zy2; // crop rectangle, reduced image scale
int ox1, oy1, ox2, oy2; // outer crop rectangle
int nx1, ny1, nx2, ny2; // inner crop rectangle
int px, py;
float *pix1, *pix3;
int nc = E1pxm->nc, pcc = nc * sizeof(float);
if (Fbusy++) return; // prevent re-entrance
if (cropx1 < 0) cropx1 = 0; // keep within visible area
if (cropx2 > E0ww) cropx2 = E0ww;
if (cropy1 < 0) cropy1 = 0;
if (cropy2 > E0hh) cropy2 = E0hh;
if (pcropx1 < cropx1) ox1 = pcropx1; // outer rectangle
else ox1 = cropx1;
if (pcropx2 > cropx2) ox2 = pcropx2;
else ox2 = cropx2;
if (pcropy1 < cropy1) oy1 = pcropy1;
else oy1 = cropy1;
if (pcropy2 > cropy2) oy2 = pcropy2;
else oy2 = cropy2;
if (pcropx1 > cropx1) nx1 = pcropx1; // inner rectangle
else nx1 = cropx1;
if (pcropx2 < cropx2) nx2 = pcropx2;
else nx2 = cropx2;
if (pcropy1 > cropy1) ny1 = pcropy1;
else ny1 = cropy1;
if (pcropy2 < cropy2) ny2 = pcropy2;
else ny2 = cropy2;
ox1 /= Zratio; // outer rectangle, reduced image scale
oy1 /= Zratio;
ox2 /= Zratio;
oy2 /= Zratio;
nx1 /= Zratio; // inner rectangle
ny1 /= Zratio;
nx2 /= Zratio;
ny2 /= Zratio;
ox1 -= 2; // expand outer rectangle
oy1 -= 2;
ox2 += 2;
oy2 += 2;
nx1 += 2; // reduce inner rectangle
ny1 += 2;
nx2 -= 2;
ny2 -= 2;
if (ox1 < 0) ox1 = 0;
if (oy1 < 0) oy1 = 0;
if (ox2 > Eww) ox2 = Eww;
if (oy2 > Ehh) oy2 = Ehh;
if (nx1 < ox1) nx1 = ox1;
if (ny1 < oy1) ny1 = oy1;
if (nx2 > ox2) nx2 = ox2;
if (ny2 > oy2) ny2 = oy2;
if (nx2 < nx1) nx2 = nx1;
if (ny2 < ny1) ny2 = ny1;
if (! E9pxm) E9pxm = PXM_copy(E3pxm); // E9 = original source image (reduced)
zx1 = cropx1 / Zratio + 0.5; // crop rectangle for reduced image
zy1 = cropy1 / Zratio + 0.5;
zx2 = cropx2 / Zratio + 0.5;
zy2 = cropy2 / Zratio + 0.5;
if (zx2 > Eww) zx2 = Eww;
if (zy2 > Ehh) zy2 = Ehh;
for (py = oy1; py < ny1; py++) // top band of pixels
for (px = ox1; px < ox2; px++)
{
pix1 = PXMpix(E9pxm,px,py);
pix3 = PXMpix(E3pxm,px,py);
if (px < zx1 || px >= zx2 || py < zy1 || py >= zy2)
{
pix3[0] = pix1[0] / 2; // outside crop margins
pix3[1] = pix1[1] / 2; // 50% brightness
pix3[2] = pix1[2] / 2;
}
else memcpy(pix3,pix1,pcc); // inside, full brightness
}
for (py = oy1; py < oy2; py++) // right band
for (px = nx2; px < ox2; px++)
{
pix1 = PXMpix(E9pxm,px,py);
pix3 = PXMpix(E3pxm,px,py);
if (px < zx1 || px >= zx2 || py < zy1 || py >= zy2)
{
pix3[0] = pix1[0] / 2;
pix3[1] = pix1[1] / 2;
pix3[2] = pix1[2] / 2;
}
else memcpy(pix3,pix1,pcc);
}
for (py = ny2; py < oy2; py++) // bottom band
for (px = ox1; px < ox2; px++)
{
pix1 = PXMpix(E9pxm,px,py);
pix3 = PXMpix(E3pxm,px,py);
if (px < zx1 || px >= zx2 || py < zy1 || py >= zy2)
{
pix3[0] = pix1[0] / 2;
pix3[1] = pix1[1] / 2;
pix3[2] = pix1[2] / 2;
}
else memcpy(pix3,pix1,pcc);
}
for (py = oy1; py < oy2; py++) // left band
for (px = ox1; px < nx1; px++)
{
pix1 = PXMpix(E9pxm,px,py);
pix3 = PXMpix(E3pxm,px,py);
if (px < zx1 || px >= zx2 || py < zy1 || py >= zy2)
{
pix3[0] = pix1[0] / 2;
pix3[1] = pix1[1] / 2;
pix3[2] = pix1[2] / 2;
}
else memcpy(pix3,pix1,pcc);
}
cairo_t *cr = draw_context_create(gdkwin,draw_context);
Fpaint3(ox1,oy1,ox2-ox1,ny1-oy1,cr); // 4 updated rectangles
Fpaint3(nx2,oy1,ox2-nx2,oy2-oy1,cr);
Fpaint3(ox1,ny2,ox2-ox1,oy2-ny2,cr);
Fpaint3(ox1,oy1,nx1-ox1,oy2-oy1,cr);
drawlines(cr); // draw crop margin lines
draw_context_destroy(draw_context);
if (ox1 == 0 || oy1 == 0 || ox2 == Eww || oy2 == Ehh) // insure edges are refreshed
Fpaint2();
pcropx1 = cropx1; // prior crop rectangle
pcropy1 = cropy1; // = current crop rectangle
pcropx2 = cropx2;
pcropy2 = cropy2;
Fbusy = 0;
return;
}
// draw lines on image: crop rectangle, center circle
void crop_names::drawlines(cairo_t *cr)
{
using namespace crop_names;
int cx, cy, rad, rad2;
int zx1, zx2, zy1, zy2; // crop rectangle, reduced image scale
zx1 = cropx1 / Zratio + 0.5; // crop rectangle for reduced image
zy1 = cropy1 / Zratio + 0.5;
zx2 = cropx2 / Zratio + 0.5;
zy2 = cropy2 / Zratio + 0.5;
if (zx2 > Eww) zx2 = Eww;
if (zy2 > Ehh) zy2 = Ehh;
Ntoplines = 4; // outline crop rectangle
toplines[0].x1 = zx1;
toplines[0].y1 = zy1;
toplines[0].x2 = zx2;
toplines[0].y2 = zy1;
toplines[0].type = 2; // white lines
toplines[1].x1 = zx2;
toplines[1].y1 = zy1;
toplines[1].x2 = zx2;
toplines[1].y2 = zy2;
toplines[1].type = 2;
toplines[2].x1 = zx2;
toplines[2].y1 = zy2;
toplines[2].x2 = zx1;
toplines[2].y2 = zy2;
toplines[2].type = 2;
toplines[3].x1 = zx1;
toplines[3].y1 = zy2;
toplines[3].x2 = zx1;
toplines[3].y2 = zy1;
toplines[3].type = 2;
draw_toplines(1,cr); // draw crop rectangle and guidelines
erase_topcircles(); // erase center circle
rad = 15 / Mscale; // repaint center area
rad2 = 2 * rad + 1;
cx = (pcropx1 + pcropx2) * 0.5 / Zratio;
cy = (pcropy1 + pcropy2) * 0.5 / Zratio;
Fpaint3(cx-rad,cy-rad,rad2,rad2,cr);
cx = (cropx1 + cropx2) * 0.5 / Zratio; // draw center circle
cy = (cropy1 + cropy2) * 0.5 / Zratio;
add_topcircle(cx,cy,10);
draw_topcircles(cr);
return;
}
// final crop - cut margins off
// E3 is the full size input image, maybe rotated
// E3 is the output image from user crop rectangle
void crop_names::crop_final()
{
using namespace crop_names;
int px1, py1, px2, py2;
float *pix3, *pix9;
int nc = E3pxm->nc, pcc = nc * sizeof(float);
int ww, hh, nn;
ww = cropww; // save final width/height in user dialog
hh = crophh;
if (ww < 2 || hh < 2) return; // 24.40
nn = ww - (cropx2 - cropx1); // adjust crop rectangle to hit
cropx1 -= nn/2; // target width and height
if (cropx1 < 0) cropx1 = 0;
cropx2 = cropx1 + ww;
if (cropx2 > Eww) cropx2 = Eww;
nn = hh - (cropy2 - cropy1);
cropy1 -= nn/2;
if (cropy1 < 0) cropy1 = 0;
cropy2 = cropy1 + hh;
if (cropy2 > Ehh) cropy2 = Ehh;
if (E9pxm) PXM_free(E9pxm);
E9pxm = PXM_make(ww,hh,nc); // new pixmap with requested size
for (py1 = cropy1; py1 < cropy2; py1++) // copy E3 (rotated) to new size
for (px1 = cropx1; px1 < cropx2; px1++)
{
px2 = px1 - cropx1;
py2 = py1 - cropy1;
pix3 = PXMpix(E3pxm,px1,py1);
pix9 = PXMpix(E9pxm,px2,py2);
memcpy(pix9,pix3,pcc);
}
PXM_free(E3pxm);
E3pxm = E9pxm;
E9pxm = 0;
Eww = E3pxm->ww; // update E3 dimensions
Ehh = E3pxm->hh;
CEF->Fmods++;
CEF->Fsaved = 0;
Fpaint2();
return;
}
// auto-crop image - set max. crop rectangle to exclude transparent regions
void crop_names::autocrop_max()
{
using namespace crop_names;
PXM *pxm = 0;
int px1, py1, px2, py2;
int qx1, qy1, qx2, qy2;
int qx, qy, step1, step2;
int area1, area2, Fgrow = 0;
float *ppix;
if (! E3pxm) return; // no edit image
if (! E0pxm) return;
pxm = E0pxm;
if (pxm->nc < 4) { // no alpha channel
cropx1 = 0;
cropx2 = pxm->ww; // set max. dimensions
cropy1 = 0;
cropy2 = pxm->hh;
cropww = pxm->ww;
crophh = pxm->hh;
return;
}
for (px1 = 0; px1 < pxm->ww; px1++) { // check image edges for transparency
ppix = PXMpix(pxm,px1,0);
if (ppix[3] < 200) break;
ppix = PXMpix(pxm,px1,pxm->hh-1);
if (ppix[3] < 200) break;
}
for (py1 = 0; py1 < pxm->hh; py1++) {
ppix = PXMpix(pxm,0,py1);
if (ppix[3] < 200) break;
ppix = PXMpix(pxm,pxm->ww-1,py1);
if (ppix[3] < 200) break;
}
if (px1 == pxm->ww && py1 == pxm->hh) { // no edge transparencies found
cropx1 = 0;
cropx2 = pxm->ww; // set max. dimensions
cropy1 = 0;
cropy2 = pxm->hh;
cropww = pxm->ww;
crophh = pxm->hh;
return;
}
px1 = 0.4 * pxm->ww; // select small rectangle in the middle
py1 = 0.4 * pxm->hh;
px2 = 0.6 * pxm->ww;
py2 = 0.6 * pxm->hh;
step1 = 0.02 * (pxm->ww + pxm->hh); // start with big search steps
if (step1 < 2) step1 = 2;
step2 = 0.2 * step1;
if (step2 < 1) step2 = 1;
while (true)
{
while (true)
{
Fgrow = 0;
area1 = (px2 - px1) * (py2 - py1); // area of current selection rectangle
area2 = area1;
for (qx1 = px1-step1; qx1 <= px1+step1; qx1 += step2) // loop, vary NW and SE corners +/-
for (qy1 = py1-step1; qy1 <= py1+step1; qy1 += step2)
for (qx2 = px2-step1; qx2 <= px2+step1; qx2 += step2)
for (qy2 = py2-step1; qy2 <= py2+step1; qy2 += step2)
{
if (qx1 < 0) continue; // check image limits
if (qy1 < 0) continue;
if (qx1 > 0.5 * pxm->ww) continue;
if (qy1 > 0.5 * pxm->hh) continue;
if (qx2 > pxm->ww-1) continue;
if (qy2 > pxm->hh-1) continue;
if (qx2 < 0.5 * pxm->ww) continue;
if (qy2 < 0.5 * pxm->hh) continue;
ppix = PXMpix(pxm,qx1,qy1); // check 4 corners are not
if (ppix[3] < 200) continue; // in transparent zones
ppix = PXMpix(pxm,qx2,qy1);
if (ppix[3] < 200) continue;
ppix = PXMpix(pxm,qx2,qy2);
if (ppix[3] < 200) continue;
ppix = PXMpix(pxm,qx1,qy2);
if (ppix[3] < 200) continue;
area2 = (qx2 - qx1) * (qy2 - qy1); // look for larger enclosed area
if (area2 <= area1) continue;
for (qx = qx1; qx < qx2; qx++) { // check top/bottom sides not intersect
ppix = PXMpix(pxm,qx,qy1); // transparent zones
if (ppix[3] < 200) break;
ppix = PXMpix(pxm,qx,qy2);
if (ppix[3] < 200) break;
}
if (qx < qx2) continue;
for (qy = qy1; qy < qy2; qy++) { // also left/right sides
ppix = PXMpix(pxm,qx1,qy);
if (ppix[3] < 200) break;
ppix = PXMpix(pxm,qx2,qy);
if (ppix[3] < 200) break;
}
if (qy < qy2) continue;
area1 = area2;
Fgrow = 1; // successfully grew the rectangle
px1 = qx1; // new bigger rectangle coordinates
py1 = qy1; // for the next search iteration
px2 = qx2;
py2 = qy2;
}
if (! Fgrow) break;
}
if (step2 == 1) break; // done
step1 = 0.7 * step1; // reduce search step size
if (step1 < 2) step1 = 2;
step2 = 0.2 * step1;
if (step2 < 1) step2 = 1;
}
cropx1 = px1; // set parameters for crop function
cropx2 = px2;
cropy1 = py1;
cropy2 = py2;
cropww = px2 - px1;
crophh = py2 - py1;
return;
}
// auto-crop image - set crop rectangle to exclude transparent regions
// keep current box center and keep w/h aspect ratio
void crop_names::autocrop_ratio()
{
using namespace crop_names;
PXM *pxm = 0;
int cx, cy, ww, hh;
int px1, py1, px2, py2;
int ppx1, ppy1, ppx2, ppy2;
float *ppix;
if (! E0pxm) return; // no full size image
if (! E3pxm) return; // no edit image
pxm = E3pxm;
if (pxm->nc < 4) return; // no alpha channel
cx = (cropx1 + cropx2) * 0.5 / Zratio; // center of current crop rectangle
cy = (cropy1 + cropy2) * 0.5 / Zratio;
px1 = cx - 0.1 * pxm->ww; // start with small rectangle at cx, cy
px2 = cx + 0.1 * pxm->ww;
if (px1 < 0) px1 = 0;
if (px2 > pxm->ww-1) px2 = pxm->ww-1;
ww = px2 - px1; // width
hh = ww / cropR; // height for width
py1 = cy - hh / 2.0 + 0.5; // set height
py2 = cy + hh / 2.0 + 0.5;
if (py1 < 0) py1 = 0;
if (py2 > pxm->hh-1) py2 = pxm->hh-1;
ppx1 = px1; // remember prior values
ppy1 = py1;
ppx2 = px2;
ppy2 = py2;
while (true)
{
px1--; px2++; // expand width
if (px1 < 0) px1 = 0;
if (px2 > pxm->ww-1) px2 = pxm->ww-1;
ww = px2 - px1; // width
hh = ww / cropR; // height for width
py1 = cy - hh / 2.0 + 0.5;
py2 = cy + hh / 2.0 + 0.5;
if (py1 < 0) py1 = 0;
if (py2 > pxm->hh-1) py2 = pxm->hh-1;
if ((px1 + px2) / 2 > cx) { px1--; px2--; } // center tracks new width, height
if ((px1 + px2) / 2 < cx) { px1++; px2++; }
if ((py1 + py2) / 2 > cy) { py1--; py2--; }
if ((py1 + py2) / 2 < cy) { py1++; py2++; }
if (px1 < 0) { px1 = 0; break; } // check image limits
if (py1 < 0) { py1 = 0; break; }
if (px2 > pxm->ww-1) { px2 = pxm->ww-1; break; }
if (py2 > pxm->hh-1) { py2 = pxm->hh-1; break; }
ppix = PXMpix(pxm,px1,py1); // check 4 corners are not
if (ppix[3] < 200) break; // in transparent zones
ppix = PXMpix(pxm,px2,py1);
if (ppix[3] < 200) break;
ppix = PXMpix(pxm,px2,py2);
if (ppix[3] < 200) break;
ppix = PXMpix(pxm,px1,py2);
if (ppix[3] < 200) break;
if (px1 == ppx1 && py1 == ppy1 && px2 == ppx2 && py2 == ppy2) break; // if no change, break out
ppx1 = px1; // remember each set of values
ppy1 = py1;
ppx2 = px2;
ppy2 = py2;
}
cropx1 = px1; // set parameters for crop function
cropy1 = py1;
cropx2 = px2;
cropy2 = py2;
cropx1 *= Zratio; // scale to full image size
cropy1 *= Zratio;
cropx2 *= Zratio;
cropy2 *= Zratio;
if (cropx2 > E0ww) cropx2 = E0ww;
if (cropy2 > E0hh) cropy2 = E0hh;
cropww = cropx2 - cropx1;
crophh = cropy2 - cropy1;
return;
}
// dialog to set custom crop button names and corresponding ratios
void crop_names::crop_customize()
{
using namespace crop_names;
ch text[20], blab[8], rlab[8];
float r1, r2, ratio;
ch *pp;
int ii, zstat;
/***
___________________________________________________________
| crop Buttons |
| |
| label [ 5:4 ] [ 4:3 ] [ 8:5 ] [ 16:9 ] [ 2:1 ] [ gold ] |
| ratio [ 5:4 ] [ 4:3 ] [ 8:5 ] [ 16:9 ] [ 2:1 ] [1.62:1] |
| |
| [OK] [X] |
|___________________________________________________________|
***/
zdialog *zd = zdialog_new("Crop Buttons",Mwin,"OK"," X ",null);
zdialog_add_widget(zd,"hbox","hbb","dialog",0,"homog|space=3");
zdialog_add_widget(zd,"hbox","hbr","dialog",0,"homog|space=3");
zdialog_add_widget(zd,"label","label","hbb","label","space=3");
zdialog_add_widget(zd,"label","ratio","hbr","ratio","space=3");
strcpy(blab,"butt-0");
strcpy(rlab,"ratio-0");
for (ii = 0; ii < 5; ii++) // add current cropbuttons to dialog
{
blab[5] = '0' + ii;
rlab[6] = '0' + ii;
zdialog_add_widget(zd,"zentry",blab,"hbb",cropbuttons[ii],"size=4");
zdialog_add_widget(zd,"zentry",rlab,"hbr",cropratios[ii],"size=4");
}
zdialog_set_modal(zd);
zdialog_run(zd,0,"mouse"); // run dialog
zstat = zdialog_wait(zd); // wait for complete
if (zstat == 1) // done
{
for (ii = 0; ii < 5; ii++) // get new button names
{
blab[5] = '0' + ii;
zdialog_fetch(zd,blab,text,12);
strTrim2(text);
if (! *text) continue;
zstrcopy(cropbuttons[ii],text,"crop buttons");
}
for (ii = 0; ii < 5; ii++) // get new ratios
{
rlab[6] = '0' + ii;
zdialog_fetch(zd,rlab,text,12);
strTrim2(text);
r1 = r2 = ratio = 0;
pp = substring(text,':',1);
if (! pp) continue;
r1 = atofz(pp);
pp = substring(text,':',2);
if (! pp) continue;
r2 = atofz(pp);
if (r1 > 0 && r2 > 0) ratio = r1/r2;
if (ratio < 0.1 || ratio > 10) continue;
zstrcopy(cropratios[ii],text,"crop buttons");
}
}
zdialog_free(zd); // kill dialog
save_params(); // save parameters
return;
}
/********************************************************************************/
// Retouch
// Adjust all aspects of brightness, contrast, color:
// Black level, white balance, brightness, contrast, color temperature, color saturation
namespace retouch_names
{
editfunc EFretouch;
float blackR, blackG, blackB; // black level RGB values
float Rbscale, Gbscale, Bbscale; // scaled RGB factors - black clip
float wbalR, wbalG, wbalB; // white balance RGB factors
int Fblack; // flag, black level was set
int sampWB, sampBL; // sample size, % pixels
int spotWB, spotBL; // spot white balance or black level active
float tempR, tempG, tempB; // illumination temperature RGB factors
float combR, combG, combB; // combined RGB factors
float brightness; // brightness input, -1 ... +1
float contrast; // contrast input, -1 ... +1
float colorsat; // saturation input, -1 ... +1 = saturated
int colortemp; // illumination temp. input, 1K ... 8K
int Fapply; // flag, apply dialog controls to image
float whitebalance[4] = { 1.0, 1.0, 1.0, 5000 }; // white balance RGB factors, deg. K
}
// menu function
void m_retouch(GtkWidget *, ch *menu)
{
using namespace retouch_names;
void retouch_mousefunc();
int retouch_dialog_event(zdialog* zd, ch *event);
void retouch_curvedit(int spc);
void * retouch_thread(void *);
GtkWidget *drawwin_scale;
F1_help_topic = "retouch";
Plog(1,"m_retouch \n");
EFretouch.menuname = "Retouch";
EFretouch.menufunc = m_retouch;
EFretouch.FprevReq = 1; // use preview
EFretouch.Farea = 2; // select area usable
EFretouch.Fpaintedits = 1; // use with paint edits OK
EFretouch.Fscript = 1; // scripting supported
EFretouch.threadfunc = retouch_thread;
EFretouch.mousefunc = retouch_mousefunc;
if (! edit_setup(EFretouch)) return; // setup edit
/***
_______________________________________________
| Retouch |
| ___________________________________________ |
| | | |
| | | | // 5 curves are maintained:
| | | | // 0: current display curve
| | | | // 1: curve for overall brightness
| | curve edit area | | // 2, 3, 4: red, green, blue
| | | |
| | | |
| | | |
| |___________________________________________| |
| |___________________________________________| | // brightness scale: black to white stripe
| (o) all (o) red (o) green (o) blue | // select curve to display
| |
| [_] Auto black level [__] sample % | // auto black level
| [_] Auto white balance [__] sample % | // auto white balance
| [x] Click dark spot for black level | // click dark spot
| [x] Click gray spot for white balance | // click gray spot
| [_] Click for RGB histogram | // show RGB histogram in popup window
| |
| Brightness =============[]================= | // brightness
| Contrast ===============[]=============== | // contrast
| Saturation =================[]============ | // saturation B/W ... saturated
| Temperature ===============[]============== | // illumination temperature
| |
| [Reset] [Prev] [OK] [X] |
|_______________________________________________|
***/
zdialog *zd = zdialog_new("Retouch",Mwin,"Reset","Prev","OK"," X ",null);
EFretouch.zd = zd;
zdialog_add_widget(zd,"frame","frameH","dialog",0,"expand"); // edit-curve
zdialog_add_widget(zd,"frame","frameB","dialog"); // black to white brightness scale
zdialog_add_widget(zd,"hbox","hbrgb","dialog"); // (o) all (o) red (o) green (o) blue
zdialog_add_widget(zd,"radio","all","hbrgb","All","space=5");
zdialog_add_widget(zd,"radio","red","hbrgb","Red","space=3");
zdialog_add_widget(zd,"radio","green","hbrgb","Green","space=3");
zdialog_add_widget(zd,"radio","blue","hbrgb","Blue","space=3");
zdialog_add_widget(zd,"hbox","hbabl","dialog"); // black level
zdialog_add_widget(zd,"zbutton","autoBL","hbabl","Auto black level","space=3");
zdialog_add_widget(zd,"label","space","hbabl",0,"space=5");
zdialog_add_widget(zd,"zspin","sampBL","hbabl","1|50|1|1","space=3|size=3");
zdialog_add_widget(zd,"label","labsamp","hbabl","sample %");
zdialog_add_widget(zd,"hbox","hbawb","dialog"); // white balance
zdialog_add_widget(zd,"zbutton","autoWB","hbawb","Auto white balance","space=3");
zdialog_add_widget(zd,"label","space","hbawb",0,"space=5");
zdialog_add_widget(zd,"zspin","sampWB","hbawb","1|50|1|1","space=3|size=3");
zdialog_add_widget(zd,"label","labsamp","hbawb","sample %");
zdialog_add_widget(zd,"hbox","hbwb","dialog"); // click dark spot
zdialog_add_widget(zd,"check","spotWB","hbwb","Click gray spot for white balance","space=3");
zdialog_add_widget(zd,"hbox","hbbl","dialog"); // click gray spot
zdialog_add_widget(zd,"check","spotBL","hbbl","Click dark spot for black level","space=3");
zdialog_add_widget(zd,"hbox","hbhist","dialog"); // click RGB histogram
zdialog_add_widget(zd,"zbutton","hist","hbhist","Click for RGB histogram","space=3");
zdialog_add_widget(zd,"hbox","hbcolor","dialog",0,"space=8");
zdialog_add_widget(zd,"label","space","hbcolor",0,"space=1");
zdialog_add_widget(zd,"vbox","vbcolor1","hbcolor",0,"homog");
zdialog_add_widget(zd,"label","space","hbcolor",0,"space=1");
zdialog_add_widget(zd,"vbox","vbcolor2","hbcolor",0,"homog|expand");
zdialog_add_widget(zd,"label","labrite","vbcolor1","Brightness");
zdialog_add_widget(zd,"label","labcont","vbcolor1","Contrast");
zdialog_add_widget(zd,"label","labsat1","vbcolor1","Saturation");
zdialog_add_widget(zd,"label","labtemp1","vbcolor1","Temperature");
zdialog_add_widget(zd,"hscale2","brightness","vbcolor2","-1.0|1.0|0.01|0");
zdialog_add_widget(zd,"hscale2","contrast","vbcolor2","-1.0|1.0|0.01|0");
zdialog_add_widget(zd,"hscale2","colorsat","vbcolor2","-1.0|1.0|0.01|0");
zdialog_add_widget(zd,"hscale2","colortemp","vbcolor2","2000|8000|1|5000");
zdialog_add_ttip(zd,"Prev","use previous input");
GtkWidget *frameH = zdialog_gtkwidget(zd,"frameH"); // setup edit curves
spldat *sd = splcurve_init(frameH,retouch_curvedit);
EFretouch.sd = sd;
sd->Nscale = 1; // horizontal line, neutral value
sd->xscale[0][0] = 0.00;
sd->yscale[0][0] = 0.50;
sd->xscale[1][0] = 1.00;
sd->yscale[1][0] = 0.50;
for (int ii = 0; ii < 4; ii++) // loop curves 0-3
{
sd->nap[ii] = 3; // initial curves are neutral
sd->vert[ii] = 0;
sd->fact[ii] = 0;
sd->apx[ii][0] = 0.01; // horizontal lines
sd->apy[ii][0] = 0.50;
sd->apx[ii][1] = 0.50;
sd->apy[ii][1] = 0.50; // curve 0 = overall brightness
sd->apx[ii][2] = 0.99;
sd->apy[ii][2] = 0.50; // curve 1/2/3 = R/G/B adjustment
splcurve_generate(sd,ii);
sd->mod[ii] = 0; // mark curve unmodified
}
sd->Nspc = 4; // 4 curves
sd->fact[0] = 1; // curve 0 active
zdialog_stuff(zd,"all",1); // stuff default selection, all
GtkWidget *frameB = zdialog_gtkwidget(zd,"frameB"); // setup brightness scale drawing area
drawwin_scale = gtk_drawing_area_new();
gtk_container_add(GTK_CONTAINER(frameB),drawwin_scale);
gtk_widget_set_size_request(drawwin_scale,100,12);
G_SIGNAL(drawwin_scale,"draw",brightness_scale,0);
wbalR = wbalG = wbalB = 1.0; // neutral white balance
colortemp = 5000; // neutral illumination temp.
blackbodyRGB(colortemp,tempR,tempG,tempB); // neutral temp. RGB factors
combR = combG = combB = 1.0; // neutral combined RGB factors
blackR = blackG = blackB = 0.0; // zero black point
Fblack = 0;
zdialog_stuff(zd,"spotWB",0); // reset mouse click status
zdialog_stuff(zd,"spotBL",0);
spotWB = spotBL = 0;
brightness = 0; // neutral brightness
contrast = 0; // neutral contrast
colorsat = 0; // neutral saturation
Fapply = 0;
zdialog_resize(zd,350,450);
zdialog_run(zd,retouch_dialog_event,"save"); // run dialog - parallel
return;
}
// dialog event and completion callback function
int retouch_dialog_event(zdialog *zd, ch *event)
{
using namespace retouch_names;
void retouch_mousefunc();
void retouch_autoWB();
void retouch_autoBL();
void retouch_mousefunc();
spldat *sd = EFretouch.sd;
float bright0, dbrite, cont0, dcont;
float dx, dy;
int ii;
Fapply = 0;
if (strmatch(event,"done")) zd->zstat = 3; // apply and quit
if (strmatch(event,"cancel")) zd->zstat = 4;
if (strmatch(event,"apply")) Fapply = 1; // from script
if (strmatch(event,"fullsize")) { // from select area
edit_fullsize(); // get full size image
thread_signal();
thread_wait(); // required for paint edits
return 1;
}
if (zd->zstat == 1) // [reset]
{
zd->zstat = 0; // keep dialog active
zdialog_stuff(zd,"brightness",0); // neutral brightness
zdialog_stuff(zd,"contrast",0); // neutral contrast
brightness = contrast = 0;
zdialog_stuff(zd,"colorsat",0); // neutral saturation
colorsat = 0;
for (int ii = 0; ii < 4; ii++) // loop brightness curves 0-3
{
sd->nap[ii] = 3; // all curves are neutral
sd->vert[ii] = 0;
sd->fact[ii] = 0;
sd->apx[ii][0] = 0.01;
sd->apy[ii][0] = 0.50;
sd->apx[ii][1] = 0.50;
sd->apy[ii][1] = 0.50;
sd->apx[ii][2] = 0.99;
sd->apy[ii][2] = 0.50;
splcurve_generate(sd,ii);
sd->mod[ii] = 0; // mark curve unmodified
}
sd->fact[0] = 1; // active curve is 'all'
gtk_widget_queue_draw(sd->drawarea); // redraw curves
zdialog_stuff(zd,"all",1);
zdialog_stuff(zd,"red",0);
zdialog_stuff(zd,"green",0);
zdialog_stuff(zd,"blue",0);
zdialog_stuff(zd,"spotWB",0);
zdialog_stuff(zd,"spotBL",0);
wbalR = wbalG = wbalB = 1.0; // neutral white balance
colortemp = 5000; // neutral illumination temp.
zdialog_stuff(zd,"colortemp",colortemp);
blackbodyRGB(colortemp,tempR,tempG,tempB); // neutral temp. RGB factors
combR = combG = combB = 1.0; // neutral combined RGB factors
blackR = blackG = blackB = 0.0; // zero black point
Fblack = 0;
edit_reset();
return 1;
}
if (zd->zstat == 2) // [prev] restore previous settings
{
zd->zstat = 0; // keep dialog active
zdialog_load_prev_widgets(zd,sd,"retouch");
wbalR = whitebalance[0]; // use previous white balance settings
wbalG = whitebalance[1];
wbalB = whitebalance[2];
colortemp = whitebalance[3];
zdialog_stuff(zd,"colortemp",colortemp);
Fapply = 1; // trigger apply event
}
if (zd->zstat == 3) // [OK]
{
freeMouse();
if (CEF->Fmods) {
edit_fullsize(); // get full size image
thread_signal(); // apply changes
zdialog_save_last_widgets(zd,sd,"retouch"); // save settings
whitebalance[0] = wbalR; // save settings for next time
whitebalance[1] = wbalG;
whitebalance[2] = wbalB;
whitebalance[3] = colortemp;
edit_addhist("br:%.2f con:%.2f sat:%.2f temp:%d", // edit params > edit hist
brightness,contrast,colorsat,colortemp);
edit_done(0); // complete edit
}
else edit_cancel(0); // no changes made
return 1;
}
if (zd->zstat < 0 || zd->zstat > 3) { // cancel
edit_cancel(0);
return 1;
}
if (strmatch(event,"autoWB")) { // auto white balance
zdialog_fetch(zd,"sampWB",sampWB); // % brightest pixels to sample
zdialog_stuff(zd,"spotWB",0);
zdialog_stuff(zd,"spotBL",0);
spotWB = spotBL = 0;
retouch_autoWB();
}
if (strmatch(event,"autoBL")) { // auto black level
zdialog_fetch(zd,"sampBL",sampBL); // % darkest pixels to sample
zdialog_stuff(zd,"spotWB",0);
zdialog_stuff(zd,"spotBL",0);
spotWB = spotBL = 0;
retouch_autoBL();
}
if (strmatch(event,"spotWB")) { // spot white balance checkbox
zdialog_fetch(zd,"spotWB",spotWB);
if (spotWB) {
zdialog_stuff(zd,"spotBL",0);
spotBL = 0;
}
}
if (strmatch(event,"spotBL")) { // spot black level checkbox
zdialog_fetch(zd,"spotBL",spotBL);
if (spotBL) {
zdialog_stuff(zd,"spotWB",0);
spotWB = 0;
}
}
if (zstrstr("spotWB spotBL",event)) { // spot white balance or black level
if (spotWB || spotBL) // if either is on,
takeMouse(retouch_mousefunc,dragcursor); // connect mouse function
else freeMouse(); // both off: free mouse
return 1;
}
if (strmatch(event,"hist")) m_RGB_hist(0,0); // popup RGB histogram window
if (zstrstr("all red green blue",event)) // new choice of curve
{
zdialog_fetch(zd,event,ii);
if (! ii) return 0; // button OFF event, wait for ON event
for (ii = 0; ii < 4; ii++) sd->fact[ii] = 0;
ii = strmatchV(event,"all","red","green","blue",null);
ii = ii-1; // new active curve: 0, 1, 2, 3
sd->fact[ii] = 1;
splcurve_generate(sd,ii); // regenerate curve
gtk_widget_queue_draw(sd->drawarea); // draw curve
}
if (strmatch(event,"brightness")) // brightness slider, 0 ... 1
{
bright0 = brightness;
zdialog_fetch(zd,"brightness",brightness);
dbrite = brightness - bright0; // change in brightness, + -
zdialog_stuff(zd,"all",1); // active curve is "all"
sd->fact[0] = 1;
for (ii = 1; ii < 4; ii++)
sd->fact[ii] = 0;
for (ii = 0; ii < sd->nap[0]; ii++) // update curve 0 nodes from slider
{
dy = sd->apy[0][ii] + 0.5 * dbrite; // increment brightness
if (dy < 0) dy = 0;
if (dy > 1) dy = 1;
sd->apy[0][ii] = dy;
}
splcurve_generate(sd,0); // regenerate curve 0
gtk_widget_queue_draw(sd->drawarea); // draw curve 0
}
if (strmatch(event,"contrast")) // contrast slider, 0 ... 1
{
cont0 = contrast;
zdialog_fetch(zd,"contrast",contrast);
dcont = contrast - cont0; // change in contrast, + -
zdialog_stuff(zd,"all",1); // active curve is "all"
sd->fact[0] = 1;
for (ii = 1; ii < 4; ii++)
sd->fact[ii] = 0;
for (ii = 0; ii < sd->nap[0]; ii++) // update curve 0 nodes from slider
{
dx = sd->apx[0][ii]; // 0 ... 0.5 ... 1
if (dx < 0.0 || dx >= 1.0) continue;
dy = dcont * (dx - 0.5); // -0.5 ... 0 ... +0.5 * dcont
dy += sd->apy[0][ii];
if (dy < 0) dy = 0;
if (dy > 1) dy = 1;
sd->apy[0][ii] = dy;
}
splcurve_generate(sd,0); // regenerate curve 0
gtk_widget_queue_draw(sd->drawarea); // draw curve 0
}
if (strmatch(event,"paint")) Fapply = 1; // from paint_edits()
if (zstrstr("brightness contrast colortemp colorsat " // detect change
"autoWB autoBL load",event)) Fapply = 1;
if (! Fapply) return 1; // wait for change
zdialog_fetch(zd,"colortemp",colortemp); // get illumination temp. setting
blackbodyRGB(colortemp,tempR,tempG,tempB); // generate temp. adjustments
zdialog_fetch(zd,"brightness",brightness); // get brightness setting
zdialog_fetch(zd,"contrast",contrast); // get contrast setting
zdialog_fetch(zd,"colorsat",colorsat); // get saturation setting
thread_signal(); // update the image
return 1;
}
// this function is called when a curve is edited
void retouch_curvedit(int spc)
{
using namespace retouch_names;
thread_signal();
return;
}
// use brightest pixels to estimate RGB values of illumination
// and use these to adjust image RGB values for neutral illumination
void retouch_autoWB()
{
using namespace retouch_names;
int pixR[256], pixG[256], pixB[256]; // pixel counts per RGB value 0-255
int samplesize; // sample size, pixel count
int px, py, ii, dist;
int sampR, sampG, sampB;
double sumR, sumG, sumB;
float meanR, meanG, meanB;
float *pix;
for (ii = 0; ii < 256; ii++)
pixR[ii] = pixG[ii] = pixB[ii] = 0;
for (py = 0; py < Ehh; py++) // scan image file, get pixel counts
for (px = 0; px < Eww; px++) // per RGB value 0-255
{
if (sa_stat == sa_stat_fini) { // select area active
ii = py * Eww + px;
dist = sa_pixmap[ii]; // distance from edge
if (! dist) continue; // pixel outside area
}
pix = PXMpix(E1pxm,px,py);
ii = pix[0]; // pixel R value 0-255
pixR[ii]++; // accumulate pixel count for R value
ii = pix[1];
pixG[ii]++;
ii = pix[2];
pixB[ii]++;
}
samplesize = Eww * Ehh; // image pixel count
if (sa_stat == sa_stat_fini) samplesize = sa_Npixel; // use area count
samplesize = samplesize * 0.01 * sampWB; // % sample size >> sample pixel count
sampR = sampG = sampB = 0;
sumR = sumG = sumB = 0;
for (ii = 254; ii > 0; ii--) // sample brightest % pixels
{ // (avoid blowout value 255)
if (sampR < samplesize) {
sampR += pixR[ii]; // pixels with R value ii
sumR += ii * pixR[ii]; // sum product
}
if (sampG < samplesize) {
sampG += pixG[ii];
sumG += ii * pixG[ii];
}
if (sampB < samplesize) {
sampB += pixB[ii];
sumB += ii * pixB[ii];
}
}
meanR = sumR / sampR; // mean RGB for brightest % pixels
meanG = sumG / sampG;
meanB = sumB / sampB;
wbalR = meanG / meanR; // = 1.0 if all RGB are equal
wbalG = meanG / meanG; // <1/>1 if RGB should be less/more
wbalB = meanG / meanB;
return;
}
// use darkest pixels to estimate black point and reduce image RGB values
void retouch_autoBL()
{
using namespace retouch_names;
int pixR[256], pixG[256], pixB[256]; // pixel counts per RGB value 0-255
int samplesize; // sample size, pixel count
int px, py, ii, dist;
int sampR, sampG, sampB;
double sumR, sumG, sumB;
float meanR, meanG, meanB;
float *pix;
for (ii = 0; ii < 256; ii++)
pixR[ii] = pixG[ii] = pixB[ii] = 0;
for (py = 0; py < Ehh; py++) // scan image file, get pixel counts
for (px = 0; px < Eww; px++) // per RGB value 0-255
{
if (sa_stat == sa_stat_fini) { // select area active
ii = py * Eww + px;
dist = sa_pixmap[ii]; // distance from edge
if (! dist) continue; // pixel outside area
}
pix = PXMpix(E1pxm,px,py);
ii = pix[0]; // pixel R value 0-255
pixR[ii]++; // accumulate pixel count for R value
ii = pix[1];
pixG[ii]++;
ii = pix[2];
pixB[ii]++;
}
samplesize = Eww * Ehh; // image pixel count
if (sa_stat == sa_stat_fini) samplesize = sa_Npixel; // use area count
samplesize = samplesize * 0.01 * sampBL; // % sample size >> sample pixel count
sampR = sampG = sampB = 0;
sumR = sumG = sumB = 0;
for (ii = 0; ii < 100; ii++) // sample darkest % pixels
{
if (sampR < samplesize) {
sampR += pixR[ii]; // pixels with R value ii
sumR += ii * pixR[ii]; // sum product
}
if (sampG < samplesize) {
sampG += pixG[ii];
sumG += ii * pixG[ii];
}
if (sampB < samplesize) {
sampB += pixB[ii];
sumB += ii * pixB[ii];
}
}
meanR = sumR / sampR; // mean RGB for darkest % pixels
meanG = sumG / sampG;
meanB = sumB / sampB;
blackR = meanR; // set black RGB levels
blackG = meanG;
blackB = meanB;
Fblack = 1;
return;
}
// get nominal white color or black point from mouse click position
void retouch_mousefunc() // mouse function
{
using namespace retouch_names;
int px, py, dx, dy;
float red, green, blue;
float *ppix;
ch mousetext[60];
if (! LMclick) return;
LMclick = 0;
px = Mxclick; // mouse click position
py = Myclick;
if (px < 2) px = 2; // pull back from edge
if (px > Eww-3) px = Eww-3;
if (py < 2) py = 2;
if (py > Ehh-3) py = Ehh-3;
red = green = blue = 0;
for (dy = -1; dy <= 1; dy++) // 3x3 block around mouse position
for (dx = -1; dx <= 1; dx++)
{
ppix = PXMpix(E1pxm,px+dx,py+dy); // input image
red += ppix[0];
green += ppix[1];
blue += ppix[2];
}
red = red / 9.0; // mean RGB levels
green = green / 9.0;
blue = blue / 9.0;
snprintf(mousetext,60,"3x3 pixels RGB: %.0f %.0f %.0f \n",red,green,blue);
poptext_mouse(mousetext,10,10,0,3);
if (spotWB &&
(red<5 || red>250 || green<5 || green>250 || blue<5 || blue>250)) { // refuse unscalable spot
zmessageACK(Mwin,"choose a better spot");
return;
}
if (spotWB) { // click pixel is new gray/white
wbalR = green / red; // = 1.0 if all RGB are equal
wbalG = green / green; // <1/>1 if RGB should be less/more
wbalB = green / blue;
thread_signal(); // trigger image update
}
if (spotBL) { // click pixel is new black point
blackR = red;
blackG = green;
blackB = blue;
Fblack = 1;
thread_signal(); // update image
}
return;
}
// thread function
void * retouch_thread(void *arg)
{
using namespace retouch_names;
void * retouch_wthread(void *);
if (Fpaintedits && ! Mdrag) return 0; // if mouse paint, must be drag
combR = wbalR * tempR / 256.0; // combine white balance colors
combG = wbalG * tempG / 256.0; // and illumination temperature.
combB = wbalB * tempB / 256.0; // <1/>1 if RGB should be less/more
Rbscale = Gbscale = Bbscale = 1.0;
if (Fblack) { // if RGB black points defined,
Rbscale = 256.0 / (256.0 - blackR); // rescale for full brightness
Gbscale = 256.0 / (256.0 - blackG);
Bbscale = 256.0 / (256.0 - blackB);
}
get_edit_pixels_init(NSMP,0); // initz. pixel loop
do_wthreads(retouch_wthread,NSMP); // do working threads
CEF->Fmods++; // image3 modified
CEF->Fsaved = 0;
if (! Fpaintedits) Fpaint2(); // update window
return 0;
}
void * retouch_wthread(void *arg) // worker thread function
{
using namespace retouch_names;
int index = *((int *) arg);
int ii, px, py, Fend;
float *pix1, *pix3;
float R1, G1, B1, R3, G3, B3, R9, G9, B9;
float pixbrite, ff, F1, F2;
float blend, coeff = 1000.0 / 256.0;
spldat *sd = EFretouch.sd;
while (true)
{
Fend = get_edit_pixels(index,px,py,blend);
if (Fend) return 0;
if (blend == 0) continue;
pix1 = PXMpix(E1pxm,px,py); // input pixel
pix3 = PXMpix(E3pxm,px,py); // output pixel
R1 = R9 = pix1[0]; // input image RGB values
G1 = G9 = pix1[1];
B1 = B9 = pix1[2];
R3 = pix3[0]; // current output image RGB
G3 = pix3[1];
B3 = pix3[2];
// apply RGB black points to input RGB values
if (Fblack) {
R9 = R9 - blackR; // clip black value at low end
R9 = R9 * Rbscale; // rescale so that high end = 256
if (R9 < 0) R9 = 0;
G9 = G9 - blackG;
G9 = G9 * Gbscale;
if (G9 < 0) G9 = 0;
B9 = B9 - blackB;
B9 = B9 * Bbscale;
if (B9 < 0) B9 = 0;
}
// apply white balance and temperature color shifts // dampen as RGB values approach 256
if (combR > 1) {
F1 = (256 - R9) / 256; // R1 = 0...256 >> F1 = 1...0
F2 = F1 * combR + 1 - F1; // F2 = combR ... 1
R9 = F2 * R9; // R9 is adjusted R1
}
else R9 = combR * R9;
if (combG > 1) { // same for G9 and B9
F1 = (256 - G9) / 256;
F2 = F1 * combG + 1 - F1;
G9 = F2 * G9;
}
else G9 = combG * G9;
if (combB > 1) {
F1 = (256 - B9) / 256;
F2 = F1 * combB + 1 - F1;
B9 = F2 * B9;
}
else B9 = combB * B9;
// apply saturation color shift
if (colorsat != 0) {
pixbrite = 0.333 * (R9 + G9 + B9); // pixel brightness, 0 to 255.9
R9 = R9 + colorsat * (R9 - pixbrite); // colorsat is -1 ... +1
G9 = G9 + colorsat * (G9 - pixbrite);
B9 = B9 + colorsat * (B9 - pixbrite);
}
// apply brightness/contrast curves
if (sd->mod[0]) // curve was modified
{
pixbrite = R9; // use max. RGB value
if (G9 > pixbrite) pixbrite = G9;
if (B9 > pixbrite) pixbrite = B9;
ii = coeff * pixbrite; // "all" curve index 0-999
if (ii < 0) ii = 0;
if (ii > 999) ii = 999;
ff = 2.0 * sd->yval[0][ii]; // 0.0 - 2.0
R9 = ff * R9;
G9 = ff * G9;
B9 = ff * B9;
}
if (sd->mod[1])
{
ii = coeff * R9; // additional RGB curve adjustments
if (ii < 0) ii = 0;
if (ii > 999) ii = 999;
R9 = R9 * (0.5 + sd->yval[1][ii]); // 0.5 - 1.5
}
if (sd->mod[2])
{
ii = coeff * G9;
if (ii < 0) ii = 0;
if (ii > 999) ii = 999;
G9 = G9 * (0.5 + sd->yval[2][ii]);
}
if (sd->mod[3])
{
ii = coeff * B9;
if (ii < 0) ii = 0;
if (ii > 999) ii = 999;
B9 = B9 * (0.5 + sd->yval[3][ii]);
}
RGBfix(R9,G9,B9);
if (Fpaintedits) // gradual edit within mouse circle
{
if (blend > 0)
{
R3 = blend * R9 + (1-blend) * R3; // increase edit
G3 = blend * G9 + (1-blend) * G3;
B3 = blend * B9 + (1-blend) * B3;
}
else if (blend < 0)
{
R3 = -blend * R1 + (1+blend) * R3; // decrease edit
G3 = -blend * G1 + (1+blend) * G3;
B3 = -blend * B1 + (1+blend) * B3;
}
}
else // full edit for image or area
{
R3 = blend * R9 + (1-blend) * R1; // increase edit
G3 = blend * G9 + (1-blend) * G1;
B3 = blend * B9 + (1-blend) * B1;
}
pix3[0] = R3; // set output pixel
pix3[1] = G3;
pix3[2] = B3;
}
return 0;
}
/********************************************************************************/
// Return relative RGB illumination values for a light source
// having a given input temperature of 1000-10000 deg. K
// 5000 K is neutral: all returned factors relative to 1.0
void blackbodyRGB(int K, float &R, float &G, float &B)
{
float kk[19] = { 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5, 8.0, 8.5, 9.0, 9.5, 10.0 };
float r1[19] = { 255, 255, 255, 255, 255, 255, 255, 255, 254, 250, 242, 231, 220, 211, 204, 197, 192, 188, 184 };
float g1[19] = { 060, 148, 193, 216, 232, 242, 249, 252, 254, 254, 251, 245, 239, 233, 228, 224, 220, 217, 215 };
float b1[19] = { 000, 010, 056, 112, 158, 192, 219, 241, 253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 };
static int ftf = 1;
static float r2[10000], g2[10000], b2[10000];
if (ftf) { // initialize spline curves
spline1(19,kk,r1);
for (int T = 1000; T < 10000; T++)
r2[T] = spline2(0.001 * T);
spline1(19,kk,g1);
for (int T = 1000; T < 10000; T++)
g2[T] = spline2(0.001 * T);
spline1(19,kk,b1);
for (int T = 1000; T < 10000; T++)
b2[T] = spline2(0.001 * T);
ftf = 0;
}
if (K < 1000 || K > 9999) zappcrash("blackbody bad K: %dK",K);
R = r2[K];
G = g2[K];
B = b2[K];
return;
}
/********************************************************************************/
// Rescale image
namespace rescale_names
{
editfunc EFrescale;
int width, height; // new image size
int orgwidth, orgheight; // original image size
int pctwidth, pctheight; // percent of original size
int Fnewwidth, Fnewheight; // flags, width or height was changed
int Flockratio; // flag, W/H ratio is locked
float WHratio;
}
void m_rescale(GtkWidget *, ch *menu)
{
using namespace rescale_names;
int rescale_dialog_event(zdialog *zd, ch *event);
void * rescale_thread(void *);
F1_help_topic = "rescale";
Plog(1,"m_rescale \n");
ch rtext[12];
EFrescale.menuname = "Rescale";
EFrescale.menufunc = m_rescale;
EFrescale.Fscript = 1; // script supported
EFrescale.threadfunc = rescale_thread; // thread function
if (! edit_setup(EFrescale)) return; // setup edit
/****
__________________________________________________
| |
| Pixels Percent |
| Width [_____] [_____] [Previous] |
| Height [_____] [_____] |
| |
| [_] 3/1 [_] 2/1 [_] 3/2 [_] 1/1 |
| [_] 3/4 [_] 2/3 [_] 1/2 [_] 1/3 [_] 1/4 |
| |
| W/H Ratio: 1.333 Lock [_] |
| [OK] [X] |
|__________________________________________________|
****/
zdialog *zd = zdialog_new("Rescale Image",Mwin,"OK"," X ",null);
EFrescale.zd = zd;
zdialog_add_widget(zd,"hbox","hb1","dialog",0,"space=3");
zdialog_add_widget(zd,"vbox","vb11","hb1",0,"homog|space=5");
zdialog_add_widget(zd,"vbox","vb12","hb1",0,"homog|space=5");
zdialog_add_widget(zd,"vbox","vb13","hb1",0,"homog|space=5");
zdialog_add_widget(zd,"vbox","vb14","hb1",0,"homog|space=10");
zdialog_add_widget(zd,"label","placeholder","vb11",0);
zdialog_add_widget(zd,"label","labw","vb11","Width");
zdialog_add_widget(zd,"label","labh","vb11","Height");
zdialog_add_widget(zd,"label","labpix","vb12","Pixels");
zdialog_add_widget(zd,"zspin","width","vb12","10|50000|1|200","size=6"); // fotocx.h limits
zdialog_add_widget(zd,"zspin","height","vb12","10|50000|1|200","size=6");
zdialog_add_widget(zd,"label","labpct","vb13","Percent");
zdialog_add_widget(zd,"zspin","pctwidth","vb13","1|999|1|100");
zdialog_add_widget(zd,"zspin","pctheight","vb13","1|999|1|100");
zdialog_add_widget(zd,"button","prev","vb14","Previous");
zdialog_add_widget(zd,"hbox","hb2","dialog",0,"space=5");
zdialog_add_widget(zd,"label","space","hb2",0);
zdialog_add_widget(zd,"check","3/1","hb2","3/1");
zdialog_add_widget(zd,"check","2/1","hb2","2/1");
zdialog_add_widget(zd,"check","3/2","hb2","3/2");
zdialog_add_widget(zd,"check","1/1","hb2","1/1");
zdialog_add_widget(zd,"hbox","hb3","dialog",0,"space=5");
zdialog_add_widget(zd,"label","space","hb3",0);
zdialog_add_widget(zd,"check","3/4","hb3","3/4");
zdialog_add_widget(zd,"check","2/3","hb3","2/3");
zdialog_add_widget(zd,"check","1/2","hb3","1/2");
zdialog_add_widget(zd,"check","1/3","hb3","1/3");
zdialog_add_widget(zd,"check","1/4","hb3","1/4");
zdialog_add_widget(zd,"hbox","hb4","dialog",0,"space=5");
zdialog_add_widget(zd,"label","labratio","hb4","W/H Ratio:","space=5");
zdialog_add_widget(zd,"label","WHratio","hb4","1.6667");
zdialog_add_widget(zd,"check","Flockratio","hb4","Lock","space=10");
zdialog_add_ttip(zd,"prev","use previous input");
orgwidth = E1pxm->ww; // original width, height
orgheight = E1pxm->hh;
width = orgwidth; // = initial width, height
height = orgheight;
WHratio = 1.0 * width / height; // initial W/H ratio
pctwidth = pctheight = 100;
Fnewwidth = Fnewheight = 0;
Flockratio = 1; // W/H ratio initially locked
zdialog_stuff(zd,"width",width);
zdialog_stuff(zd,"height",height);
zdialog_stuff(zd,"pctwidth",pctwidth);
zdialog_stuff(zd,"pctheight",pctheight);
snprintf(rtext,12,"%.3f",WHratio);
zdialog_stuff(zd,"WHratio",rtext);
zdialog_stuff(zd,"Flockratio",1);
zdialog_stuff(zd,"3/1",0);
zdialog_stuff(zd,"2/1",0);
zdialog_stuff(zd,"3/2",0);
zdialog_stuff(zd,"1/1",1); // begin with full size
zdialog_stuff(zd,"3/4",0);
zdialog_stuff(zd,"2/3",0);
zdialog_stuff(zd,"1/2",0);
zdialog_stuff(zd,"1/3",0);
zdialog_stuff(zd,"1/4",0);
zdialog_run(zd,rescale_dialog_event,"save"); // run dialog
return;
}
// dialog event and completion callback function
int rescale_dialog_event(zdialog *zd, ch *event)
{
using namespace rescale_names;
int nn;
double memreq, memavail;
ch rtext[12];
int pixcc = 4 * sizeof(float); // pixel size, RGBA
if (strmatch(event,"done")) zd->zstat = 1; // apply and quit
if (strmatch(event,"cancel")) zd->zstat = 2; // cancel
if (strmatch(event,"apply")) { // from script
zdialog_fetch(zd,"width",width);
zdialog_fetch(zd,"height",height);
thread_signal();
return 1;
}
if (zd->zstat) // dialog complete
{
if (zd->zstat == 1) { // done
editrescale[0] = width; // remember size used
editrescale[1] = height;
thread_signal();
edit_addhist("width:%d height:%d",width,height); // edit params > edit hist
edit_done(0);
}
else edit_cancel(0); // cancel or kill
Fzoom = 0;
return 1;
}
if (strmatch(event,"focus")) return 1; // ignore focus
Fnewwidth = Fnewheight = 0; // reset flags
if (strmatch(event,"pctwidth")) { // % width input
zdialog_fetch(zd,"pctwidth",nn);
if (nn != pctwidth) {
width = nn / 100.0 * orgwidth + 0.5;
Fnewwidth = 1; // note changed
}
}
if (strmatch(event,"pctheight")) { // same for height
zdialog_fetch(zd,"pctheight",nn);
if (nn != pctheight) {
height = nn / 100.0 * orgheight + 0.5;
Fnewheight = 1;
}
}
if (strmatch(event,"width")) { // width input
zdialog_fetch(zd,"width",nn);
if (nn != width) {
width = nn; // set new width
Fnewwidth = 1; // note change
}
}
if (strmatch(event,"height")) { // same for height
zdialog_fetch(zd,"height",nn);
if (nn != height) {
height = nn;
Fnewheight = 1;
}
}
if (Fnewwidth || Fnewheight) { // width or height was set directly
zdialog_stuff(zd,"3/1",0); // uncheck all preset sizes
zdialog_stuff(zd,"2/1",0);
zdialog_stuff(zd,"3/2",0);
zdialog_stuff(zd,"1/1",0);
zdialog_stuff(zd,"3/4",0);
zdialog_stuff(zd,"2/3",0);
zdialog_stuff(zd,"1/2",0);
zdialog_stuff(zd,"1/3",0);
zdialog_stuff(zd,"1/4",0);
}
if (zstrstr("3/1 2/1 3/2 1/1 3/4 2/3 1/2 1/3 1/4",event)) { // a ratio button was selected
zdialog_stuff(zd,"3/1",0); // uncheck all preset sizes
zdialog_stuff(zd,"2/1",0);
zdialog_stuff(zd,"3/2",0);
zdialog_stuff(zd,"1/1",0);
zdialog_stuff(zd,"3/4",0);
zdialog_stuff(zd,"2/3",0);
zdialog_stuff(zd,"1/2",0);
zdialog_stuff(zd,"1/3",0);
zdialog_stuff(zd,"1/4",0);
zdialog_stuff(zd,event,1); // set selected button on
if (strmatch(event,"3/1")) {
width = 3 * orgwidth;
height = 3 * orgheight;
}
if (strmatch(event,"2/1")) {
width = 2 * orgwidth;
height = 2 * orgheight;
}
if (strmatch(event,"3/2")) {
width = 1.5 * orgwidth;
height = 1.5 * orgheight;
}
if (strmatch(event,"1/1")) {
width = orgwidth;
height = orgheight;
}
if (strmatch(event,"3/4")) {
width = 0.75 * orgwidth;
height = 0.75 * orgheight;
}
if (strmatch(event,"2/3")) {
width = 0.66667 * orgwidth;
height = 0.66667 * orgheight;
}
if (strmatch(event,"1/2")) {
width = 0.5 * orgwidth;
height = 0.5 * orgheight;
}
if (strmatch(event,"1/3")) {
width = 0.33333 * orgwidth;
height = 0.33333 * orgheight;
}
if (strmatch(event,"1/4")) {
width = 0.25 * orgwidth;
height = 0.25 * orgheight;
}
WHratio = 1.0 * orgwidth / orgheight; // new W/H ratio
Flockratio = 1; // lock W/H ratio
}
if (strmatch("prev",event)) { // Previous size button checked
zdialog_stuff(zd,"3/1",0); // uncheck all preset sizes
zdialog_stuff(zd,"2/1",0);
zdialog_stuff(zd,"3/2",0);
zdialog_stuff(zd,"1/1",0);
zdialog_stuff(zd,"3/4",0);
zdialog_stuff(zd,"2/3",0);
zdialog_stuff(zd,"1/2",0);
zdialog_stuff(zd,"1/3",0);
zdialog_stuff(zd,"1/4",0);
width = editrescale[0]; // set previous size
height = editrescale[1];
WHratio = 1.0 * width / height; // new W/H ratio
Flockratio = 1; // lock W/H ratio
}
if (strmatch(event,"Flockratio")) // toggle W/H ratio lock
zdialog_fetch(zd,"Flockratio",Flockratio);
if (Flockratio) { // if W/H ratio locked,
if (Fnewwidth) height = width / WHratio + 0.5; // force same W/H ratio
if (Fnewheight) width = height * WHratio + 0.5; // round
}
if (width < 10) { // enforce lower limits 24.10
width = 10;
height = width / WHratio + 0.5;
}
if (height < 10) {
height = 10;
width = height * WHratio + 0.5;
}
if (! edit_wwhhOK(width,height)) return 1; // enforce image size limits
if (! Flockratio) WHratio = 1.0 * width / height; // track actual W/H ratio
pctwidth = 100.0 * width / orgwidth + 0.5; // final % size
pctheight = 100.0 * height / orgheight + 0.5;
zdialog_stuff(zd,"width",width); // update all zdialog data
zdialog_stuff(zd,"height",height);
zdialog_stuff(zd,"pctwidth",pctwidth);
zdialog_stuff(zd,"pctheight",pctheight);
zdialog_stuff(zd,"Flockratio",Flockratio);
snprintf(rtext,12,"%.3f",WHratio);
zdialog_stuff(zd,"WHratio",rtext);
memreq = 1.0 * width * height * pixcc; // memory for rescaled image
memreq = memreq / 1024 / 1024; // peak required memory, MB
memavail = availmemory();
if (memreq > memavail - 1000) { // 1 GB margin
zmessageACK(Mwin,"insufficient memory, cannot proceed");
edit_cancel(0);
return 1;
}
thread_signal(); // update image, no wait for idle
return 1;
}
// do the rescale job
void * rescale_thread(void *)
{
using namespace rescale_names;
PXM *pxmtemp;
pxmtemp = PXM_rescale(E1pxm,width,height); // rescale the edit image
PXM_free(E3pxm);
E3pxm = pxmtemp;
Eww = E3pxm->ww;
Ehh = E3pxm->hh;
CEF->Fmods++;
CEF->Fsaved = 0;
Fpaint2();
return 0;
}
/********************************************************************************/
// Add Margins to an image
namespace margins_names
{
editfunc EFmargins;
int Eww1, Ehh1;
int Eww2, Ehh2;
int top, bott, left, right;
int red, green, blue, alpha;
}
void m_margins(GtkWidget *, ch *menu)
{
using namespace margins_names;
int margins_dialog_event(zdialog *zd, ch *event);
void * margins_thread(void *);
F1_help_topic = "margins";
Plog(1,"m_margins \n");
EFmargins.menuname = "Margins";
EFmargins.menufunc = m_margins;
EFmargins.FprevReq = 1; // use preview (small) size
EFmargins.Fscript = 1; // script supported
EFmargins.threadfunc = margins_thread; // thread function
if (! edit_setup(EFmargins)) return; // setup edit
Eww1 = E1pxm->ww; // input image dimensions
Ehh1 = E1pxm->hh;
PXM_addalpha(E1pxm);
/****
________________________________
| Add Margins |
| |
| Top [____] All+-1 [__] |
| Left [____] All+-10 [__] |
| Right [____] Color [###] |
| Bottom [____] Alpha [___] |
| |
| [OK] [X] |
|________________________________|
****/
zdialog *zd = zdialog_new("Add Margins",Mwin,"OK"," X ",null);
EFmargins.zd = zd;
zdialog_add_widget(zd,"hbox","hb1","dialog",0,"space=3");
zdialog_add_widget(zd,"vbox","vb1","hb1",0,"homog|space=5");
zdialog_add_widget(zd,"vbox","vb2","hb1",0,"homog|space=5");
zdialog_add_widget(zd,"label","space","hb1"," ");
zdialog_add_widget(zd,"vbox","vb3","hb1",0,"homog|space=5");
zdialog_add_widget(zd,"vbox","vb4","hb1",0,"homog|space=5");
zdialog_add_widget(zd,"label","Ltop","vb1","Top");
zdialog_add_widget(zd,"label","Lleft","vb1","Left");
zdialog_add_widget(zd,"label","Lright","vb1","Right");
zdialog_add_widget(zd,"label","Lbott","vb1","Bottom");
zdialog_add_widget(zd,"zspin","top","vb2","0|999|1|0");
zdialog_add_widget(zd,"zspin","left","vb2","0|999|1|0");
zdialog_add_widget(zd,"zspin","right","vb2","0|999|1|0");
zdialog_add_widget(zd,"zspin","bott","vb2","0|999|1|0");
zdialog_add_widget(zd,"label","Lall1","vb3","All±1");
zdialog_add_widget(zd,"label","Lall10","vb3","All±10");
zdialog_add_widget(zd,"label","Lcolor","vb3","Color");
zdialog_add_widget(zd,"label","Lalpha","vb3","Alpha");
zdialog_add_widget(zd,"zspin","all±1","vb4","-1|+1|1|0");
zdialog_add_widget(zd,"zspin","all±10","vb4","-10|+10|10|0");
zdialog_add_widget(zd,"colorbutt","color","vb4","255|255|255");
zdialog_add_widget(zd,"zspin","alpha","vb4","0|255|1|255");
zdialog_run(zd,margins_dialog_event,"save"); // run dialog
return;
}
// dialog event and completion callback function
int margins_dialog_event(zdialog *zd, ch *event)
{
using namespace margins_names;
void margins_final();
int addall;
ch color[20];
ch *pp;
if (strmatch(event,"focus")) return 1; // ignore focus
if (strmatch(event,"done")) zd->zstat = 1; // apply and quit
if (strmatch(event,"cancel")) zd->zstat = 2; // cancel
if (zd->zstat) // dialog complete
{
if (zd->zstat == 1) // done
{
edit_fullsize(); // get full size image
PXM_addalpha(E1pxm);
margins_final(); // rotate/crop final image
edit_addhist("%d %d %d %d",top,bott,left,right); // edit params > edit hist
edit_done(0);
}
else edit_cancel(0); // cancel or kill
return 1;
}
if (strmatch(event,"all±1")) {
zdialog_fetch(zd,"all±1",addall);
zdialog_stuff(zd,"all±1",0);
}
if (strmatch(event,"all±10")) {
zdialog_fetch(zd,"all±10",addall);
zdialog_stuff(zd,"all±10",0);
}
if (addall)
{
zdialog_fetch(zd,"top",top);
top += addall;
zdialog_stuff(zd,"top",top);
zdialog_fetch(zd,"bott",bott);
bott += addall;
zdialog_stuff(zd,"bott",bott);
zdialog_fetch(zd,"left",left);
left += addall;
zdialog_stuff(zd,"left",left);
zdialog_fetch(zd,"right",right);
right += addall;
zdialog_stuff(zd,"right",right);
addall = 0;
}
zdialog_fetch(zd,"top",top); // get margin sizes
zdialog_fetch(zd,"bott",bott);
zdialog_fetch(zd,"left",left);
zdialog_fetch(zd,"right",right);
zdialog_fetch(zd,"alpha",alpha); // margin alpha channel
zdialog_fetch(zd,"color",color,20); // margin color from color button
pp = substring(color,"|",1);
if (pp) red = atoi(pp);
pp = substring(color,"|",2);
if (pp) green = atoi(pp);
pp = substring(color,"|",3);
if (pp) blue = atoi(pp);
thread_signal(); // update image, no wait
return 1;
}
// do the margins job
void * margins_thread(void *)
{
using namespace margins_names;
PXM *pxmtemp;
int leftx, topx;
int cc, ii;
float pix1[4], *pix2;
topx = top; // guaranteed constant values
leftx = left;
pix1[0] = red; // margin color and alpha
pix1[1] = green;
pix1[2] = blue;
pix1[3] = alpha;
Eww2 = Eww1 + right + leftx; // output image size
Ehh2 = Ehh1 + topx + bott;
pxmtemp = PXM_make(Eww2,Ehh2,4); // output image with alpha channel
pix2 = pxmtemp->pixels;
cc = 4 * sizeof(float);
for (ii = 0; ii < Eww2 * Ehh2 * 4; ii += 4) // fill output image with margin color
memcpy(pix2+ii,pix1,cc);
PXM_copy_area(E1pxm,pxmtemp,0,0,leftx,topx,Eww1,Ehh1); // copy input image into output
PXM_free(E3pxm);
E3pxm = pxmtemp;
Eww = E3pxm->ww;
Ehh = E3pxm->hh;
CEF->Fmods++;
CEF->Fsaved = 0;
Fpaint2();
return 0;
}
void margins_final()
{
using namespace margins_names;
PXM *pxmtemp;
int cc, ii;
float pix1[4], *pix2;
float R;
pix1[0] = red; // margin color and alpha
pix1[1] = green;
pix1[2] = blue;
pix1[3] = alpha;
if (E1pxm->ww == Eww1) return; // preview image is full size image
R = 1.0 * (Eww2 + Ehh2) / (Eww1 + Ehh1); // size with margins / size without
top *= R + 0.5; // rescale full size margins
bott *= R + 0.5;
left *= R + 0.5;
right *= R + 0.5;
Eww1 = E1pxm->ww; // full size input image
Ehh1 = E1pxm->hh;
Eww2 = Eww1 + right + left; // full size output image
Ehh2 = Ehh1 + top + bott;
pxmtemp = PXM_make(Eww2,Ehh2,4); // output image with alpha channel
pix2 = pxmtemp->pixels;
cc = 4 * sizeof(float);
for (ii = 0; ii < Eww2 * Ehh2 * 4; ii += 4) // fill output image with margin color
memcpy(pix2+ii,pix1,cc);
PXM_copy_area(E1pxm,pxmtemp,0,0,left,top,Eww1,Ehh1); // copy input image into output
PXM_free(E3pxm);
E3pxm = pxmtemp;
Eww = E3pxm->ww;
Ehh = E3pxm->hh;
CEF->Fmods++;
CEF->Fsaved = 0;
Fpaint2();
return;
}
/********************************************************************************/
// draw on image: text, line/arrow, box, oval
void m_markup(GtkWidget *widget, ch *menu)
{
int markup_dialog_event(zdialog *zd, ch *event);
zdialog *zd;
F1_help_topic = "markup";
Plog(1,"m_markup \n");
/***
___________________________________
| Image Markup |
| |
| [_] Draw text on image |
| [_] Draw line or arrow on image |
| [_] Draw box on image |
| [_] Draw oval on image |
| |
| [X] |
|___________________________________|
***/
zd = zdialog_new("Image Markup",Mwin," X ",null);
zdialog_add_widget(zd,"check","text","dialog","Draw text on image");
zdialog_add_widget(zd,"check","line","dialog","Draw line or arrow on image");
zdialog_add_widget(zd,"check","box","dialog","Draw box on image");
zdialog_add_widget(zd,"check","oval","dialog","Draw oval on image");
zdialog_stuff(zd,"text",0);
zdialog_stuff(zd,"line",0);
zdialog_stuff(zd,"box",0);
zdialog_stuff(zd,"oval",0);
zdialog_run(zd,markup_dialog_event,"save");
return;
}
// dialog event and completion function
int markup_dialog_event(zdialog *zd, ch *event)
{
if (zd->zstat) {
zdialog_free(zd);
return 1;
}
if (strmatch(event,"text")) m_draw_text(0,0);
if (strmatch(event,"line")) m_draw_line(0,0);
if (strmatch(event,"box")) m_draw_box(0,0);
if (strmatch(event,"oval")) m_draw_oval(0,0);
if (zstrstr("text line box oval",event)) zdialog_free(zd);
return 1;
}
/********************************************************************************/
// add text on top of the image
namespace drawtext_names
{
textattr_t attr; // text attributes and image
ch file[1000] = ""; // file for write_text data
ch metakey[60] = "";
int Tpx, Tpy; // text position on image
int textpresent; // flag, text present on image
int dialog_event(zdialog *zd, ch *event); // dialog event function
void mousefunc(); // mouse event function
void write(int mode); // write text on image
editfunc EFdrawtext;
}
// menu function
void m_draw_text(GtkWidget *, ch *menu)
{
using namespace drawtext_names;
ch *title = "Draw text on image";
ch *tip = "Enter text, click/drag on image, right click to remove";
Plog(1,"m_draw_text \n");
EFdrawtext.menufunc = m_draw_text;
EFdrawtext.menuname = "Markup Text";
EFdrawtext.Farea = 1; // select area ignored
EFdrawtext.mousefunc = mousefunc;
if (! edit_setup(EFdrawtext)) return; // setup edit
/***
_________________________________________________________________
| Draw text on image |
| |
| Enter text, click/drag on image, right click to remove. |
| |
| Text [_______________________________________________________] | text
| Use metadata key [___________________________________] [Fetch] | metakey "Fetch"
| [Font] [FreeSans_________] Size [ 44|v] | "Font" fontname fontsize
| |
| Color Transp. Width Angle |
| text [###] [____] [____] | txcolor txtransp txangle
| backing [###] [____] | bgcolor bgtransp
| outline [###] [____] [___] | tocolor totransp towidth
| shadow [###] [____] [___] [____] | shcolor shtransp shwidth shangle
| |
| emboss radius [____] depth [____] |
| |
| Use settings file [Open] [Save] | "Open" "Save"
| |
| [Clear] [Apply] [X] |
|_________________________________________________________________|
[Clear] clear text and metadata key
[Apply] edit_done, restart dialog
[X] edit_cancel
***/
zdialog *zd = zdialog_new(title,Mwin,"Clear","Apply"," X ",null); // simplify
EFdrawtext.zd = zd;
EFdrawtext.mousefunc = mousefunc;
EFdrawtext.menufunc = m_draw_text; // allow restart
zdialog_add_widget(zd,"label","tip","dialog",tip,"space=5");
zdialog_add_widget(zd,"hbox","hbtext","dialog",0,"space=2");
zdialog_add_widget(zd,"label","labtext","hbtext","Text","space=5");
zdialog_add_widget(zd,"zedit","text","hbtext","text","expand|wrap");
zdialog_add_widget(zd,"hbox","hbmeta","dialog",0,"space=2");
zdialog_add_widget(zd,"label","labmeta","hbmeta","Use metadata key","space=5");
zdialog_add_widget(zd,"zentry","metakey","hbmeta",0,"space=2|expand");
zdialog_add_widget(zd,"button","Fetch","hbmeta","Fetch");
zdialog_add_widget(zd,"hbox","hbfont","dialog",0,"space=2");
zdialog_add_widget(zd,"button","Font","hbfont","Font");
zdialog_add_widget(zd,"zentry","fontname","hbfont","FreeSans","space=2|size=20");
zdialog_add_widget(zd,"label","space","hbfont",0,"space=10");
zdialog_add_widget(zd,"label","labfsize","hbfont","Size");
zdialog_add_widget(zd,"zspin","fontsize","hbfont","8|500|1|40","space=3");
zdialog_add_widget(zd,"hsep","sep1","dialog",0,"space=5");
zdialog_add_widget(zd,"hbox","hbattr","dialog",0,"space=3");
zdialog_add_widget(zd,"vbox","vbattr1","hbattr",0,"homog|space=5");
zdialog_add_widget(zd,"vbox","vbattr2","hbattr",0,"homog|space=5");
zdialog_add_widget(zd,"vbox","vbattr3","hbattr",0,"homog|space=5");
zdialog_add_widget(zd,"vbox","vbattr4","hbattr",0,"homog|space=5");
zdialog_add_widget(zd,"vbox","vbattr5","hbattr",0,"homog|space=5");
zdialog_add_widget(zd,"label","space","vbattr1");
zdialog_add_widget(zd,"label","labtext","vbattr1","text");
zdialog_add_widget(zd,"label","labback","vbattr1","backing");
zdialog_add_widget(zd,"label","laboutln","vbattr1","outline");
zdialog_add_widget(zd,"label","labshadow","vbattr1","shadow");
zdialog_add_widget(zd,"label","labcol","vbattr2","Color");
zdialog_add_widget(zd,"colorbutt","txcolor","vbattr2","0|0|0");
zdialog_add_widget(zd,"colorbutt","bgcolor","vbattr2","255|255|255");
zdialog_add_widget(zd,"colorbutt","tocolor","vbattr2","255|0|0");
zdialog_add_widget(zd,"colorbutt","shcolor","vbattr2","255|0|0");
zdialog_add_widget(zd,"label","labtran","vbattr3","Transp.");
zdialog_add_widget(zd,"zspin","txtransp","vbattr3","0|100|1|0");
zdialog_add_widget(zd,"zspin","bgtransp","vbattr3","0|100|1|0");
zdialog_add_widget(zd,"zspin","totransp","vbattr3","0|100|1|0");
zdialog_add_widget(zd,"zspin","shtransp","vbattr3","0|100|1|0");
zdialog_add_widget(zd,"label","labw","vbattr4","Width");
zdialog_add_widget(zd,"label","space","vbattr4");
zdialog_add_widget(zd,"label","space","vbattr4");
zdialog_add_widget(zd,"zspin","towidth","vbattr4","0|30|1|0");
zdialog_add_widget(zd,"zspin","shwidth","vbattr4","0|50|1|0");
zdialog_add_widget(zd,"label","labw","vbattr5","Angle");
zdialog_add_widget(zd,"zspin","txangle","vbattr5","-360|360|0.5|0");
zdialog_add_widget(zd,"label","space","vbattr5");
zdialog_add_widget(zd,"label","space","vbattr5");
zdialog_add_widget(zd,"zspin","shangle","vbattr5","-360|360|1|0");
zdialog_add_widget(zd,"hsep","sep2","dialog",0,"space=5");
zdialog_add_widget(zd,"hbox","hbemboss","dialog",0,"space=3");
zdialog_add_widget(zd,"label","labemboss","hbemboss","emboss","space=5");
zdialog_add_widget(zd,"label","labradius","hbemboss","radius","space=5");
zdialog_add_widget(zd,"zspin","radius","hbemboss","0|10|1|0");
zdialog_add_widget(zd,"label","labdepth","hbemboss","depth","space=5");
zdialog_add_widget(zd,"zspin","depth","hbemboss","0|20|1|0");
zdialog_add_widget(zd,"hbox","hbfile","dialog",0,"space=3");
zdialog_add_widget(zd,"label","labfile","hbfile","Use settings file","space=5");
zdialog_add_widget(zd,"button","Open","hbfile","Open","space=5");
zdialog_add_widget(zd,"button","Save","hbfile","Save","space=5");
zdialog_add_ttip(zd,"Clear","clear text");
zdialog_add_ttip(zd,"Apply","commit edit");
zdialog_load_inputs(zd); // restore prior inputs
memset(&attr,0,sizeof(attr));
zdialog_fetch(zd,"text",attr.text,1000); // get defaults or prior inputs
zdialog_fetch(zd,"fontname",attr.font,80);
zdialog_fetch(zd,"fontsize",attr.size);
zdialog_fetch(zd,"txcolor",attr.color[0],20);
zdialog_fetch(zd,"txtransp",attr.transp[0]);
zdialog_fetch(zd,"txangle",attr.angle);
zdialog_fetch(zd,"bgcolor",attr.color[1],20);
zdialog_fetch(zd,"bgtransp",attr.transp[1]);
zdialog_fetch(zd,"tocolor",attr.color[2],20);
zdialog_fetch(zd,"totransp",attr.transp[2]);
zdialog_fetch(zd,"towidth",attr.towidth);
zdialog_fetch(zd,"shcolor",attr.color[3],20);
zdialog_fetch(zd,"shtransp",attr.transp[3]);
zdialog_fetch(zd,"shwidth",attr.shwidth);
zdialog_fetch(zd,"shangle",attr.shangle);
zdialog_fetch(zd,"radius",attr.emboss[0]);
zdialog_fetch(zd,"depth",attr.emboss[1]);
zdialog_fetch(zd,"metakey",metakey,60);
gentext(&attr); // initial text
takeMouse(mousefunc,dragcursor); // connect mouse function
textpresent = 0; // no text on image yet
zdialog_run(zd,dialog_event,"save"); // run dialog, parallel
if (*metakey) zdialog_send_event(zd,"Fetch"); // metadata key active, get text
return;
}
// dialog event and completion callback function
int drawtext_names::dialog_event(zdialog *zd, ch *event)
{
using namespace drawtext_names;
GtkWidget *font_dialog;
ch font[100]; // font name and size
int size;
ch *pp;
ch *keyname[1];
ch *keyvals[1];
if (strmatch(event,"done")) zd->zstat = 6; // apply and quit
if (strmatch(event,"cancel")) zd->zstat = 7; // cancel
if (zd->zstat)
{
if (zd->zstat == 1) { // [clear]
*attr.text = 0;
zdialog_stuff(zd,"text","");
zd->zstat = 0; // keep dialog active
write(2); // erase text on image
return 1;
}
if (zd->zstat == 2) { // [Apply]
if (textpresent) edit_done(0); // save mods
else edit_cancel(0);
m_draw_text(0,0); // restart text dialog
return 1;
}
edit_cancel(0); // cancel
m_markup(0,0); // restart main dialog
return 1;
}
if (strmatch(event,"focus")) { // toggle mouse capture
takeMouse(mousefunc,dragcursor); // connect mouse function
return 1;
}
if (strmatch(event,"Open")) // load zdialog fields from a file
{
load_text(zd);
zdialog_fetch(zd,"text",attr.text,1000); // get all zdialog fields
zdialog_fetch(zd,"fontname",attr.font,80);
zdialog_fetch(zd,"fontsize",attr.size);
zdialog_fetch(zd,"txcolor",attr.color[0],20);
zdialog_fetch(zd,"txtransp",attr.transp[0]);
zdialog_fetch(zd,"txangle",attr.angle);
zdialog_fetch(zd,"bgcolor",attr.color[1],20);
zdialog_fetch(zd,"bgtransp",attr.transp[1]);
zdialog_fetch(zd,"tocolor",attr.color[2],20);
zdialog_fetch(zd,"totransp",attr.transp[2]);
zdialog_fetch(zd,"towidth",attr.towidth);
zdialog_fetch(zd,"shcolor",attr.color[3],20);
zdialog_fetch(zd,"shtransp",attr.transp[3]);
zdialog_fetch(zd,"shwidth",attr.shwidth);
zdialog_fetch(zd,"shangle",attr.shangle);
zdialog_fetch(zd,"radius",attr.emboss[0]);
zdialog_fetch(zd,"depth",attr.emboss[1]);
}
if (strmatch(event,"Save")) { // save zdialog fields to file
save_text(zd);
return 1;
}
if (strmatch(event,"Fetch")) { // load text from metadata keyname
zdialog_fetch(zd,"metakey",metakey,60);
if (*metakey < ' ') return 1;
keyname[0] = metakey;
meta_get1(curr_file,keyname,keyvals,1);
if (! keyvals[0]) return 1;
if (strlen(keyvals[0]) > 999) keyvals[0][999] = 0;
repl_1str(keyvals[0],attr.text,999,"\\n","\n"); // replace "\n" with newlines
zfree(keyvals[0]);
zdialog_stuff(zd,"text",attr.text); // stuff dialog with metadata
}
if (strmatch(event,"text")) // get text from dialog
zdialog_fetch(zd,"text",attr.text,1000);
if (strmatch(event,"Font")) { // select new font
snprintf(font,100,"%s %d",attr.font,attr.size);
font_dialog = gtk_font_chooser_dialog_new("select font",MWIN);
gtk_font_chooser_set_font(GTK_FONT_CHOOSER(font_dialog),font);
gtk_dialog_run(GTK_DIALOG(font_dialog));
pp = gtk_font_chooser_get_font(GTK_FONT_CHOOSER(font_dialog));
gtk_widget_destroy(font_dialog);
if (pp) { // should have "fontname nn"
strncpy0(font,pp,100);
g_free(pp);
pp = font + strlen(font);
while (*pp != ' ') pp--;
if (pp > font) {
size = atoi(pp);
if (size < 8) size = 8;
zdialog_stuff(zd,"fontsize",size);
attr.size = size;
*pp = 0;
strncpy0(attr.font,font,80); // get fontname = new font name
zdialog_stuff(zd,"fontname",font);
}
}
}
if (strmatch(event,"fontsize")) // new font size
zdialog_fetch(zd,"fontsize",attr.size);
if (strmatch(event,"txangle"))
zdialog_fetch(zd,"txangle",attr.angle);
if (strmatch(event,"txcolor")) // foreground (text) color
zdialog_fetch(zd,"txcolor",attr.color[0],20);
if (strmatch(event,"bgcolor")) // background color
zdialog_fetch(zd,"bgcolor",attr.color[1],20);
if (strmatch(event,"tocolor")) // text outline color
zdialog_fetch(zd,"tocolor",attr.color[2],20);
if (strmatch(event,"shcolor")) // text shadow color
zdialog_fetch(zd,"shcolor",attr.color[3],20);
if (strmatch(event,"txtransp")) // foreground transparency
zdialog_fetch(zd,"txtransp",attr.transp[0]);
if (strmatch(event,"bgtransp")) // background transparency
zdialog_fetch(zd,"bgtransp",attr.transp[1]);
if (strmatch(event,"totransp")) // text outline transparency
zdialog_fetch(zd,"totransp",attr.transp[2]);
if (strmatch(event,"shtransp")) // text shadow transparency
zdialog_fetch(zd,"shtransp",attr.transp[3]);
if (strmatch(event,"towidth")) // text outline width
zdialog_fetch(zd,"towidth",attr.towidth);
if (strmatch(event,"shwidth")) // text shadow width
zdialog_fetch(zd,"shwidth",attr.shwidth);
if (strmatch(event,"shangle")) // text shadow angle
zdialog_fetch(zd,"shangle",attr.shangle);
if (strmatch(event,"radius")) // text emboss radius
zdialog_fetch(zd,"radius",attr.emboss[0]);
if (strmatch(event,"depth")) // text emboss depth
zdialog_fetch(zd,"depth",attr.emboss[1]);
gentext(&attr); // build text image from text and attributes
if (textpresent) write(1); // update text on image
return 1;
}
// mouse function, set position for text on image
void drawtext_names::mousefunc()
{
using namespace drawtext_names;
if (LMclick) { // left mouse click
Tpx = Mxclick; // new text position on image
Tpy = Myclick;
write(1); // erase old, write new text on image
}
if (RMclick) { // right mouse click
write(2); // erase old text, if any
Tpx = Tpy = -1;
}
if (Mxdrag || Mydrag) // mouse dragged
{
Tpx = Mxdrag; // new text position on image
Tpy = Mydrag;
write(1); // erase old, write new text on image
}
LMclick = RMclick = Mxdrag = Mydrag = 0;
return;
}
// write text on image at designated location
// mode: 1 erase old and write to new position
// 2 erase old and write nothing
void drawtext_names::write(int mode)
{
using namespace drawtext_names;
if (! E1pxm) return; // can happen with wild key bashing
float *pix1, *pix3;
uint8 *pixT;
int px1, py1, px3, py3, done;
float e3part, Ot, Om, Ob;
static int orgx1, orgy1, ww1, hh1; // old text image overlap rectangle
int orgx2, orgy2, ww2, hh2; // new overlap rectangle
int nc = E1pxm->nc, pcc = nc * sizeof(float);
int rad, depth, dx, dy, rgb;
float kernel[23][23]; // emboss radius <= 10
float R, sumpix,*pixN;
PXM *E9pxm;
cairo_t *cr = draw_context_create(gdkwin,draw_context);
if (textpresent)
{
for (py3 = orgy1; py3 < orgy1 + hh1; py3++) // erase prior text image
for (px3 = orgx1; px3 < orgx1 + ww1; px3++) // replace E3 pixels with E1 pixels
{ // in prior overlap rectangle
if (px3 < 0 || px3 >= Eww) continue;
if (py3 < 0 || py3 >= Ehh) continue;
pix1 = PXMpix(E1pxm,px3,py3);
pix3 = PXMpix(E3pxm,px3,py3);
memcpy(pix3,pix1,pcc);
}
}
done = 0;
if (mode == 2) done = 1; // erase only
if (! *attr.text) done = 2; // no text defined
if (Tpx < 0 && Tpy < 0) done = 3; // no position defined
if (done) {
if (textpresent) {
Fpaint3(orgx1,orgy1,ww1,hh1,cr); // update window to erase old text
textpresent = 0; // mark no text present
CEF->Fmods--;
}
draw_context_destroy(draw_context);
return;
}
ww2 = attr.pxb_text->ww; // text image size
hh2 = attr.pxb_text->hh;
if (Tpx >= Eww) Tpx = Eww-1; // if off image, pull back within
if (Tpy >= Ehh) Tpy = Ehh-1;
orgx2 = Tpx - ww2/2; // copy-to image3 location
orgy2 = Tpy - hh2/2;
if (orgx2 < 0) orgx2 = 0;
if (orgy2 < 0) orgy2 = 0;
if (orgx2 + ww2 > Eww) ww2 = Eww - orgx2;
if (orgy2 + hh2 > Ehh) hh2 = Ehh - orgy2;
for (py1 = 0; py1 < hh2; py1++) // loop all pixels in text image
for (px1 = 0; px1 < ww2; px1++)
{
px3 = orgx2 + px1; // copy-to image3 pixel
py3 = orgy2 + py1;
if (px3 < 0 || px3 >= Eww) continue; // omit parts beyond edges
if (py3 < 0 || py3 >= Ehh) continue;
pixT = PXBpix(attr.pxb_text,px1,py1); // copy-from text pixel
pix3 = PXMpix(E3pxm,px3,py3); // copy-to image pixel
e3part = pixT[3] / 256.0; // text image transparency
pix3[0] = pixT[0] + e3part * pix3[0]; // combine text part + image part
pix3[1] = pixT[1] + e3part * pix3[1];
pix3[2] = pixT[2] + e3part * pix3[2];
if (nc > 3) {
Ot = (1.0 - e3part); // text opacity
Om = pix3[3] / 256.0; // image opacity
Ob = 1.0 - (1.0 - Ot) * (1.0 - Om); // combined opacity
pix3[3] = 255.0 * Ob;
}
}
rad = attr.emboss[0]; // emboss text pixels in image
depth = attr.emboss[1];
if (rad && depth)
{
E9pxm = PXM_copy_area(E3pxm,orgx2,orgy2,ww2,hh2);
R = 0.1 * depth / (rad * rad + 1);
for (dy = -rad; dy <= rad; dy++) // build kernel with radius and depth
for (dx = -rad; dx <= rad; dx++)
kernel[dx+rad][dy+rad] = R * (dx + dy);
kernel[rad][rad] = 1; // kernel center cell = 1
for (py1 = 0; py1 < hh2; py1++) // loop all pixels in text image
for (px1 = 0; px1 < ww2; px1++)
{
pixT = PXBpix(attr.pxb_text,px1,py1); // text pixel
e3part = pixT[3] / 256.0; // transparency
if (e3part > 0.99) continue; // omit transparent areas
px3 = orgx2 + px1; // image3 pixel
py3 = orgy2 + py1;
if (px3 < 0 || px3 >= Eww) continue; // omit parts beyond edges
if (py3 < 0 || py3 >= Ehh) continue;
pix3 = PXMpix(E3pxm,px3,py3); // text in image pixel
for (rgb = 0; rgb < 3; rgb++)
{
sumpix = 0;
for (dy = -rad; dy <= rad; dy++) // loop surrounding block of pixels
for (dx = -rad; dx <= rad; dx++)
{
if (px1+dx < 0 || px1+dx >= ww2) continue;
if (py1+dy < 0 || py1+dy >= hh2) continue;
pixN = PXMpix(E9pxm,px1+dx,py1+dy);
R = kernel[dx+rad][dy+rad];
sumpix += R * pixN[rgb];
}
if (sumpix < 0) sumpix = 0;
if (sumpix > 255) sumpix = 255;
pix3[rgb] = sumpix;
}
}
PXM_free(E9pxm);
}
if (textpresent) {
Fpaint3(orgx1,orgy1,ww1,hh1,cr); // update window to erase old text
textpresent = 0;
CEF->Fmods--;
}
// (updates together to reduce flicker)
Fpaint3(orgx2,orgy2,ww2,hh2,cr); // update window for new text
draw_context_destroy(draw_context);
textpresent = 1; // mark text is present
CEF->Fmods++; // increase mod count
CEF->Fsaved = 0;
orgx1 = orgx2; // remember overlap rectangle
orgy1 = orgy2; // for next call
ww1 = ww2;
hh1 = hh2;
return;
}
// load text and attributes into dialog from a file
void load_text(zdialog *zd)
{
FILE *fid;
int err, nn;
ch *pp, *pp2, *file, buff[1200];
ch *dialogtitle = "load text data from a file";
textattr_t attr;
file = zgetfile(dialogtitle,MWIN,"file",markup_folder); // get input file from user
if (! file) return;
fid = fopen(file,"r"); // open for read
if (! fid) {
zmessageACK(Mwin,strerror(errno));
zfree(file);
return;
}
pp = fgets_trim(buff,1200,fid); // read text string
if (! pp) goto badfile;
if (! strmatchN(pp,"text string: ",13)) goto badfile;
pp += 13;
if (strlen(pp) < 2) goto badfile;
repl_Nstrs(pp,attr.text,999,"\\n","\n",null); // replace "\n" with newline char.
pp = fgets_trim(buff,1200,fid); // read font and size
if (! pp) goto badfile;
if (! strmatchN(pp,"font: ",6)) goto badfile;
pp += 6;
strTrim(pp);
pp2 = (ch *) zstrstr(pp,"size: ");
if (! pp2) goto badfile;
*pp2 = 0;
pp2 += 6;
strncpy0(attr.font,pp,80);
attr.size = atoi(pp2);
if (attr.size < 8) goto badfile;
pp = fgets_trim(buff,1200,fid); // read text attributes
if (! pp) goto badfile;
nn = sscanf(pp,"attributes: %f %s %s %s %s %d %d %d %d %d %d %d",
&attr.angle, attr.color[0], attr.color[1], attr.color[2], attr.color[3],
&attr.transp[0], &attr.transp[1], &attr.transp[2], &attr.transp[3],
&attr.towidth, &attr.shwidth, &attr.shangle);
if (nn != 12) goto badfile;
err = fclose(fid);
if (err) {
zmessageACK(Mwin,strerror(errno));
zfree(file);
return;
}
zdialog_stuff(zd,"text",attr.text); // stuff zdialog fields
zdialog_stuff(zd,"fontname",attr.font);
zdialog_stuff(zd,"fontsize",attr.size);
zdialog_stuff(zd,"txangle",attr.angle);
zdialog_stuff(zd,"txcolor",attr.color[0]);
zdialog_stuff(zd,"bgcolor",attr.color[1]);
zdialog_stuff(zd,"tocolor",attr.color[2]);
zdialog_stuff(zd,"shcolor",attr.color[3]);
zdialog_stuff(zd,"txtransp",attr.transp[0]);
zdialog_stuff(zd,"bgtransp",attr.transp[1]);
zdialog_stuff(zd,"totransp",attr.transp[2]);
zdialog_stuff(zd,"shtransp",attr.transp[3]);
zdialog_stuff(zd,"towidth",attr.towidth);
zdialog_stuff(zd,"shwidth",attr.shwidth);
zdialog_stuff(zd,"shangle",attr.shangle);
return;
badfile: // project file had a problem
fclose(fid);
zmessageACK(Mwin,"text file is defective");
Plog(0,"buff: %s\n",buff);
}
// save text and attributes from dialog to a file
void save_text(zdialog *zd)
{
ch *dialogtitle = "save text data to a file";
FILE *fid;
ch *file, text2[1200];
textattr_t attr;
file = zgetfile(dialogtitle,MWIN,"save",markup_folder); // get output file from user
if (! file) return;
fid = fopen(file,"w"); // open for write
if (! fid) {
zmessageACK(Mwin,strerror(errno));
zfree(file);
return;
}
zdialog_fetch(zd,"text",attr.text,1000); // get text and attributes from zdialog
zdialog_fetch(zd,"fontname",attr.font,80);
zdialog_fetch(zd,"fontsize",attr.size);
zdialog_fetch(zd,"txangle",attr.angle);
zdialog_fetch(zd,"txcolor",attr.color[0],20);
zdialog_fetch(zd,"bgcolor",attr.color[1],20);
zdialog_fetch(zd,"tocolor",attr.color[2],20);
zdialog_fetch(zd,"shcolor",attr.color[3],20);
zdialog_fetch(zd,"txtransp",attr.transp[0]);
zdialog_fetch(zd,"bgtransp",attr.transp[1]);
zdialog_fetch(zd,"totransp",attr.transp[2]);
zdialog_fetch(zd,"shtransp",attr.transp[3]);
zdialog_fetch(zd,"towidth",attr.towidth);
zdialog_fetch(zd,"shwidth",attr.shwidth);
zdialog_fetch(zd,"shangle",attr.shangle);
repl_Nstrs(attr.text,text2,1200,"\n","\\n",null); // replace newlines with "\n"
fprintf(fid,"text string: %s\n",text2);
fprintf(fid,"font: %s size: %d \n",attr.font, attr.size);
fprintf(fid,"attributes: %.4f %s %s %s %s %d %d %d %d %d %d %d \n",
attr.angle, attr.color[0], attr.color[1], attr.color[2], attr.color[3],
attr.transp[0], attr.transp[1], attr.transp[2], attr.transp[3],
attr.towidth, attr.shwidth, attr.shangle);
fclose(fid);
return;
}
/********************************************************************************
Create a graphic image with text, any color, any font, any angle.
Add outline and shadow colors. Apply transparencies.
struct textattr_t // attributes for gentext() function
ch text[1000]; // text to generate image from
ch font[80]; // font name
int size; // font size
int tww, thh; // generated image size, unrotated
float angle; // text angle, degrees
float sinT, cosT; // trig funcs for text angle
ch color[4][20]; // text, backing, outline, shadow "R|G|B"
int transp[4]; // corresponding transparencies 0-255
int towidth; // outline width, pixels
int shwidth; // shadow width
int shangle; // shadow angle -180...+180
PXB *pxb_text; // output image with text/outline/shadow
************************************/
int gentext(textattr_t *attr)
{
PXB * gentext_outline(textattr_t *, PXB *);
PXB * gentext_shadow(textattr_t *, PXB *);
PangoFontDescription *pfont;
PangoLayout *playout;
cairo_surface_t *surface;
cairo_t *cr;
float angle;
int txred, txgreen, txblue;
int bgred, bggreen, bgblue;
int tored, togreen, toblue;
int shred, shgreen, shblue;
float txtransp, bgtransp, totransp, shtransp;
PXB *pxb_temp1, *pxb_temp2;
uint8 *cairo_data, *cpix;
uint8 *pix1, *pix2;
int px, py, ww, hh;
ch font[100]; // font name and size
int red, green, blue;
float txpart, topart, shpart, bgpart;
if (! *attr->text) strcpy(attr->text," "); // no text
if (attr->pxb_text) PXB_free(attr->pxb_text);
snprintf(font,100,"%s %d",attr->font,attr->size); // font name and size together
angle = attr->angle; // text angle, degrees
attr->sinT = sin(angle/57.296); // trig funcs for text angle
attr->cosT = cos(angle/57.296);
txred = atoi(substring(attr->color[0],'|',1)); // get text foreground color
txgreen = atoi(substring(attr->color[0],'|',2));
txblue = atoi(substring(attr->color[0],'|',3));
bgred = atoi(substring(attr->color[1],'|',1)); // get text background color
bggreen = atoi(substring(attr->color[1],'|',2));
bgblue = atoi(substring(attr->color[1],'|',3));
tored = atoi(substring(attr->color[2],'|',1)); // get text outline color
togreen = atoi(substring(attr->color[2],'|',2));
toblue = atoi(substring(attr->color[2],'|',3));
shred = atoi(substring(attr->color[3],'|',1)); // get text shadow color
shgreen = atoi(substring(attr->color[3],'|',2));
shblue = atoi(substring(attr->color[3],'|',3));
txtransp = 0.01 * attr->transp[0]; // get transparencies
bgtransp = 0.01 * attr->transp[1]; // text, background, outline, shadow
totransp = 0.01 * attr->transp[2];
shtransp = 0.01 * attr->transp[3];
pfont = pango_font_description_from_string(font); // make layout with text
playout = gtk_widget_create_pango_layout(Fdrawin,null); // Fdrawin instead of Cdrawin
if (! playout) zappcrash("gentext(): cannot create pango layout");
pango_layout_set_font_description(playout,pfont);
pango_layout_set_text(playout,attr->text,-1);
pango_layout_get_pixel_size(playout,&ww,&hh);
ww += 2 + 0.2 * attr->size; // compensate bad font metrics
hh += 2 + 0.1 * attr->size;
attr->tww = ww; // save text image size before rotate
attr->thh = hh;
surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24,ww,hh); // cairo output image
cr = cairo_create(surface);
pango_cairo_show_layout(cr,playout); // write text layout to image
cairo_data = cairo_image_surface_get_data(surface); // get text image pixels
pxb_temp1 = PXB_make(ww+5,hh,3); // create PXB
for (py = 0; py < hh; py++) // copy text image to PXB
for (px = 0; px < ww; px++)
{
cpix = cairo_data + 4 * (ww * py + px); // pango output is monocolor
pix2 = PXBpix(pxb_temp1,px+4,py); // small pad before text
pix2[0] = cpix[3]; // use red [0] for text intensity
pix2[1] = pix2[2] = 0;
}
pango_font_description_free(pfont); // free resources
g_object_unref(playout);
cairo_destroy(cr);
cairo_surface_destroy(surface);
pxb_temp2 = gentext_outline(attr,pxb_temp1); // add text outline if any
if (pxb_temp2) { // using green [1] for outline intensity
PXB_free(pxb_temp1);
pxb_temp1 = pxb_temp2;
}
pxb_temp2 = gentext_shadow(attr,pxb_temp1); // add text shadow color if any
if (pxb_temp2) { // using blue [2] for shadow intensity
PXB_free(pxb_temp1);
pxb_temp1 = pxb_temp2;
}
if (fabsf(angle) > 0.1) { // rotate text if wanted
pxb_temp2 = PXB_rotate(pxb_temp1,angle);
PXB_free(pxb_temp1);
pxb_temp1 = pxb_temp2;
}
ww = pxb_temp1->ww; // text image input PXB
hh = pxb_temp1->hh;
pxb_temp2 = PXB_make(ww,hh,4); // text image output PXB
for (py = 0; py < hh; py++) // loop all pixels in text image
for (px = 0; px < ww; px++)
{
pix1 = PXBpix(pxb_temp1,px,py); // copy-from pixel (text + outline + shadow)
pix2 = PXBpix(pxb_temp2,px,py); // copy-to pixel
txpart = pix1[0] / 256.0; // text opacity 0-1
topart = pix1[1] / 256.0; // outline
shpart = pix1[2] / 256.0; // shadow
bgpart = (1.0 - txpart - topart - shpart); // background
txpart = txpart * (1.0 - txtransp); // reduce for transparencies
topart = topart * (1.0 - totransp);
shpart = shpart * (1.0 - shtransp);
bgpart = bgpart * (1.0 - bgtransp);
red = txpart * txred + topart * tored + shpart * shred + bgpart * bgred;
green = txpart * txgreen + topart * togreen + shpart * shgreen + bgpart * bggreen;
blue = txpart * txblue + topart * toblue + shpart * shblue + bgpart * bgblue;
pix2[0] = red; // output total red, green, blue
pix2[1] = green;
pix2[2] = blue;
pix2[3] = 255 * (1.0 - txpart - topart - shpart - bgpart); // image part visible through text
}
PXB_free(pxb_temp1);
attr->pxb_text = pxb_temp2;
return 0;
}
// add an outline color to the text character edges
// red color [0] is original monocolor text
// use green color [1] for added outline
PXB * gentext_outline(textattr_t *attr, PXB *pxb1)
{
PXB *pxb2;
int toww, ww1, hh1, ww2, hh2;
int px, py, dx, dy;
uint8 *pix1, *pix2;
float theta;
toww = attr->towidth; // text outline color width
if (toww == 0) return 0; // zero
ww1 = pxb1->ww; // input PXB dimensions
hh1 = pxb1->hh;
ww2 = ww1 + toww * 2; // add margins for outline width
hh2 = hh1 + toww * 2;
pxb2 = PXB_make(ww2,hh2,3); // output PXB
for (py = 0; py < hh1; py++) // copy text pixels to outline pixels
for (px = 0; px < ww1; px++) // displaced by outline width
{
pix1 = PXBpix(pxb1,px,py);
pix2 = PXBpix(pxb2,px+toww,py+toww);
pix2[0] = pix1[0];
}
theta = 0.7 / toww;
for (theta = 0; theta < 6.3; theta += 0.7/toww) // displace outline pixels in all directions
{
dx = roundf(toww * sinf(theta));
dy = roundf(toww * cosf(theta));
for (py = 0; py < hh1; py++)
for (px = 0; px < ww1; px++)
{
pix1 = PXBpix(pxb1,px,py);
pix2 = PXBpix(pxb2,px+toww+dx,py+toww+dy);
if (pix2[1] < pix1[0] - pix2[0]) // compare text to outline brightness
pix2[1] = pix1[0] - pix2[0]; // brighter part is outline pixel
}
}
return pxb2; // pix2[0] / pix2[1] = text / outline
}
// add a shadow to the text character edges
// red color [0] is original monocolor text intensity
// green color [1] is added outline if any
// use blue color [2] for added shadow
PXB * gentext_shadow(textattr_t *attr, PXB *pxb1)
{
PXB *pxb2;
int shww, ww1, hh1, ww2, hh2;
int px, py, dx, dy;
uint8 *pix1, *pix2;
float theta;
shww = attr->shwidth; // text shadow width
if (shww == 0) return 0; // zero
ww1 = pxb1->ww; // input PXB dimensions
hh1 = pxb1->hh;
ww2 = ww1 + shww * 2; // add margins for shadow width
hh2 = hh1 + shww * 2;
pxb2 = PXB_make(ww2,hh2,3); // output PXB
for (py = 0; py < hh1; py++) // copy text pixels to shadow pixels
for (px = 0; px < ww1; px++) // displaced by shadow width
{
pix1 = PXBpix(pxb1,px,py);
pix2 = PXBpix(pxb2,px+shww,py+shww);
pix2[0] = pix1[0];
pix2[1] = pix1[1];
}
theta = (90 - attr->shangle) / 57.3; // degrees to radians, 0 = to the right
dx = roundf(shww * sinf(theta));
dy = roundf(shww * cosf(theta));
for (py = 0; py < hh1; py++) // displace text by shadow width
for (px = 0; px < ww1; px++)
{
pix1 = PXBpix(pxb1,px,py);
pix2 = PXBpix(pxb2,px+shww+dx,py+shww+dy);
if (pix2[2] < pix1[0] + pix1[1] - pix2[0] - pix2[1]) // compare text+outline to shadow pixels
pix2[2] = pix1[0] + pix1[1] - pix2[0] - pix2[1]; // brighter part is shadow pixel
}
return pxb2; // pix2[0] / pix2[1] / pix2[2]
} // = text / outline / shadow brightness
/********************************************************************************/
// draw a line or arrow on top of the image
namespace drawline_names
{
lineattr_t attr; // line/arrow attributes and image
int mpx, mpy; // mouse position on image
int linepresent; // flag, line present on image
int orgx1, orgy1, ww1, hh1; // old line image overlap rectangle
int orgx2, orgy2, ww2, hh2; // new overlap rectangle
zdialog *zd;
int dialog_event(zdialog *zd, ch *event); // dialog event function
void mousefunc(); // mouse event function
void write(int mode); // write line on image
editfunc EFdrawline;
}
void m_draw_line(GtkWidget *, ch *menu)
{
using namespace drawline_names;
ch *intro = "Enter line or arrow properties in dialog, \n"
"click/drag on image, right click to remove";
Plog(1,"m_draw_line \n");
EFdrawline.menufunc = m_draw_line;
EFdrawline.menuname = "Markup Line";
EFdrawline.Farea = 1; // select area ignored
EFdrawline.mousefunc = mousefunc;
if (! edit_setup(EFdrawline)) return; // setup edit
/***
________________________________________________
| Draw line or arrow on image |
| |
| Enter line or arrow properties in dialog, |
| click/drag on image, right click to remove. |
| |
| Line length [____] width [____] |
| Arrow head [x] left [x] right |
| |
| Color Transp. Width Angle |
| line [###] [____] [___] | lncolor lntransp lnangle
| backing [###] [____] | bgcolor bgtransp
| outline [###] [____] [___] | tocolor totransp towidth
| shadow [###] [____] [___] [___] | shcolor shtransp shwidth shangle
| |
| [Apply] [OK] [X] |
|________________________________________________|
***/
zd = zdialog_new("Draw line or arrow on image",Mwin,"Apply","OK"," X ",null);
EFdrawline.zd = zd;
EFdrawline.mousefunc = mousefunc;
EFdrawline.menufunc = m_draw_line; // allow restart
zdialog_add_widget(zd,"label","intro","dialog",intro,"space=3");
zdialog_add_widget(zd,"hbox","hbline","dialog",0,"space=3");
zdialog_add_widget(zd,"label","lablength","hbline","Line length","space=5");
zdialog_add_widget(zd,"zspin","length","hbline","2|9999|1|20");
zdialog_add_widget(zd,"label","space","hbline",0,"space=10");
zdialog_add_widget(zd,"label","labwidth","hbline","Width","space=5");
zdialog_add_widget(zd,"zspin","width","hbline","1|99|1|2");
zdialog_add_widget(zd,"hbox","hbarrow","dialog",0,"space=3");
zdialog_add_widget(zd,"label","labarrow","hbarrow","Arrow head","space=5");
zdialog_add_widget(zd,"check","larrow","hbarrow","Left");
zdialog_add_widget(zd,"label","space","hbarrow",0,"space=10");
zdialog_add_widget(zd,"check","rarrow","hbarrow","Right");
zdialog_add_widget(zd,"hbox","hbcol","dialog");
zdialog_add_widget(zd,"vbox","vbcol1","hbcol",0,"homog|space=5");
zdialog_add_widget(zd,"vbox","vbcol2","hbcol",0,"homog|space=5");
zdialog_add_widget(zd,"vbox","vbcol3","hbcol",0,"homog|space=5");
zdialog_add_widget(zd,"vbox","vbcol4","hbcol",0,"homog|space=5");
zdialog_add_widget(zd,"vbox","vbcol5","hbcol",0,"homog|space=5");
zdialog_add_widget(zd,"label","space","vbcol1");
zdialog_add_widget(zd,"label","labline","vbcol1","line");
zdialog_add_widget(zd,"label","labback","vbcol1","backing");
zdialog_add_widget(zd,"label","laboutln","vbcol1","outline");
zdialog_add_widget(zd,"label","labshadow","vbcol1","shadow");
zdialog_add_widget(zd,"label","labcol","vbcol2","Color");
zdialog_add_widget(zd,"colorbutt","lncolor","vbcol2","0|0|0");
zdialog_add_widget(zd,"colorbutt","bgcolor","vbcol2","255|255|255");
zdialog_add_widget(zd,"colorbutt","tocolor","vbcol2","255|0|0");
zdialog_add_widget(zd,"colorbutt","shcolor","vbcol2","255|0|0");
zdialog_add_widget(zd,"label","labcol","vbcol3","Transp.");
zdialog_add_widget(zd,"zspin","lntransp","vbcol3","0|100|1|0");
zdialog_add_widget(zd,"zspin","bgtransp","vbcol3","0|100|1|0");
zdialog_add_widget(zd,"zspin","totransp","vbcol3","0|100|1|0");
zdialog_add_widget(zd,"zspin","shtransp","vbcol3","0|100|1|0");
zdialog_add_widget(zd,"label","labw","vbcol4","Width");
zdialog_add_widget(zd,"label","space","vbcol4");
zdialog_add_widget(zd,"label","space","vbcol4");
zdialog_add_widget(zd,"zspin","towidth","vbcol4","0|30|1|0");
zdialog_add_widget(zd,"zspin","shwidth","vbcol4","0|50|1|0");
zdialog_add_widget(zd,"label","labw","vbcol5","Angle");
zdialog_add_widget(zd,"zspin","lnangle","vbcol5","-360|360|0.1|0");
zdialog_add_widget(zd,"label","space","vbcol5");
zdialog_add_widget(zd,"label","space","vbcol5");
zdialog_add_widget(zd,"zspin","shangle","vbcol5","-360|360|1|0");
zdialog_add_ttip(zd,"Apply","fix line/arrow in layout \n start new line/arrow");
zdialog_load_inputs(zd); // restore prior inputs
memset(&attr,0,sizeof(attr));
zdialog_fetch(zd,"length",attr.length); // get defaults or prior inputs
zdialog_fetch(zd,"width",attr.width);
zdialog_fetch(zd,"larrow",attr.larrow);
zdialog_fetch(zd,"rarrow",attr.rarrow);
zdialog_fetch(zd,"lnangle",attr.angle);
zdialog_fetch(zd,"lncolor",attr.color[0],20);
zdialog_fetch(zd,"bgcolor",attr.color[1],20);
zdialog_fetch(zd,"tocolor",attr.color[2],20);
zdialog_fetch(zd,"shcolor",attr.color[3],20);
zdialog_fetch(zd,"lntransp",attr.transp[0]);
zdialog_fetch(zd,"bgtransp",attr.transp[1]);
zdialog_fetch(zd,"totransp",attr.transp[2]);
zdialog_fetch(zd,"shtransp",attr.transp[3]);
zdialog_fetch(zd,"towidth",attr.towidth);
zdialog_fetch(zd,"shwidth",attr.shwidth);
zdialog_fetch(zd,"shangle",attr.shangle);
genline(&attr); // generate initial line
takeMouse(mousefunc,dragcursor); // connect mouse function
linepresent = 0; // no line on image yet
mpx = mpy = -1; // no position defined yet
zdialog_run(zd,dialog_event,"save"); // run dialog, parallel
return;
}
// dialog event and completion callback function
int drawline_names::dialog_event(zdialog *zd, ch *event)
{
using namespace drawline_names;
if (strmatch(event,"done")) zd->zstat = 2; // apply and quit
if (strmatch(event,"cancel")) zd->zstat = 3; // cancel
if (zd->zstat)
{
if (zd->zstat == 1) { // Apply, commit present line to image
zd->zstat = 0;
write(1);
linepresent = 0; // (no old line to erase)
mpx = mpy = -1;
PXM_free(E1pxm); // copy edits in E3 to source image E1
E1pxm = PXM_copy(E3pxm);
Fpaintnow(); // update image synchronous
return 1;
}
if (zd->zstat == 2 && CEF->Fmods) edit_done(0); // Done, complete pending edit
else edit_cancel(0); // Cancel or kill
if (attr.pxb_line) PXB_free(attr.pxb_line);
m_markup(0,0); // restore main markup dialog
return 1;
}
if (strmatch(event,"focus")) { // toggle mouse capture
takeMouse(mousefunc,dragcursor); // connect mouse function
return 1;
}
if (strmatch(event,"length")) // line length
zdialog_fetch(zd,"length",attr.length);
if (strmatch(event,"width")) // line width
zdialog_fetch(zd,"width",attr.width);
if (strmatch(event,"larrow")) // left arrow head
zdialog_fetch(zd,"larrow",attr.larrow);
if (strmatch(event,"rarrow")) // right arrow head
zdialog_fetch(zd,"rarrow",attr.rarrow);
if (strmatch(event,"lnangle")) // line angle
zdialog_fetch(zd,"lnangle",attr.angle);
if (strmatch(event,"lncolor")) // foreground (line) color
zdialog_fetch(zd,"lncolor",attr.color[0],20);
if (strmatch(event,"bgcolor")) // background color
zdialog_fetch(zd,"bgcolor",attr.color[1],20);
if (strmatch(event,"tocolor")) // line outline color
zdialog_fetch(zd,"tocolor",attr.color[2],20);
if (strmatch(event,"shcolor")) // line shadow color
zdialog_fetch(zd,"shcolor",attr.color[3],20);
if (strmatch(event,"lntransp")) // foreground transparency
zdialog_fetch(zd,"lntransp",attr.transp[0]);
if (strmatch(event,"bgtransp")) // background transparency
zdialog_fetch(zd,"bgtransp",attr.transp[1]);
if (strmatch(event,"totransp")) // line outline transparency
zdialog_fetch(zd,"totransp",attr.transp[2]);
if (strmatch(event,"shtransp")) // line shadow transparency
zdialog_fetch(zd,"shtransp",attr.transp[3]);
if (strmatch(event,"towidth")) // line outline width
zdialog_fetch(zd,"towidth",attr.towidth);
if (strmatch(event,"shwidth")) // line shadow width
zdialog_fetch(zd,"shwidth",attr.shwidth);
if (strmatch(event,"shangle")) // line shadow angle
zdialog_fetch(zd,"shangle",attr.shangle);
genline(&attr); // build line image from attributes
write(1); // write on image
return 1;
}
// mouse function, set new position for line on image
void drawline_names::mousefunc()
{
using namespace drawline_names;
float ax1, ay1, ax2, ay2, ax3, ay3; // line/arrow end points, mid point
float angle, rad, l2;
float amx, amy;
float d1, d2, d3, sinv;
if (RMclick) { // right mouse click
write(2); // erase old line, if any
mpx = mpy = -1;
LMclick = RMclick = Mxdrag = Mydrag = 0;
return;
}
if (LMclick + Mxdrag + Mydrag == 0) return;
if (LMclick) {
mpx = Mxclick; // new line position on image
mpy = Myclick;
}
else {
mpx = Mxdrag;
mpy = Mydrag;
}
LMclick = RMclick = Mxdrag = Mydrag = 0;
if (! linepresent) {
orgx2 = mpx;
orgy2 = mpy;
write(1);
return;
}
// move the closest line endpoint to the mouse position and leave the other endpoint fixed
angle = attr.angle;
rad = -angle / 57.296;
l2 = attr.length / 2.0;
ww2 = attr.pxb_line->ww; // line image buffer
hh2 = attr.pxb_line->hh;
amx = ww2 / 2.0; // line midpoint within line image
amy = hh2 / 2.0;
ax1 = amx - l2 * cosf(rad) + 0.5; // line end points
ay1 = amy + l2 * sinf(rad) + 0.5;
ax2 = amx + l2 * cosf(rad) + 0.5;
ay2 = amy - l2 * sinf(rad) + 0.5;
ax3 = (ax1 + ax2) / 2; // line mid point
ay3 = (ay1 + ay2) / 2;
d1 = (mpx-ax1-orgx1) * (mpx-ax1-orgx1) + (mpy-ay1-orgy1) * (mpy-ay1-orgy1);
d2 = (mpx-ax2-orgx1) * (mpx-ax2-orgx1) + (mpy-ay2-orgy1) * (mpy-ay2-orgy1);
d3 = (mpx-amx-orgx1) * (mpx-amx-orgx1) + (mpy-amy-orgy1) * (mpy-amy-orgy1);
d1 = sqrtf(d1); // mouse - end point distances
d2 = sqrtf(d2);
d3 = sqrtf(d3); // mouse - mid point distance
if (d1 < d2 && d1 < d3) { // move ax1/ay1 end to mouse
ax2 += orgx1;
ay2 += orgy1;
ax1 = mpx;
ay1 = mpy;
attr.length = d2 + 0.5;
sinv = (ay1-ay2) / d2;
if (sinv > 1.0) sinv = 1.0;
if (sinv < -1.0) sinv = -1.0;
rad = asinf(sinv);
angle = -57.296 * rad;
if (mpx > ax2) angle = -180 - angle;
}
else if (d2 < d1 && d2 < d3) { // move ax2/ay2 end to mouse
ax1 += orgx1;
ay1 += orgy1;
ax2 = mpx;
ay2 = mpy;
attr.length = d1 + 0.5;
sinv = (ay1-ay2) / d1;
if (sinv > 1.0) sinv = 1.0;
if (sinv < -1.0) sinv = -1.0;
rad = asinf(sinv);
angle = -57.296 * rad;
if (mpx < ax1) angle = -180 - angle;
}
else { // move entire arrow unchanged
ax1 += (mpx - ax3);
ay1 += (mpy - ay3);
ax2 += (mpx - ax3);
ay2 += (mpy - ay3);
}
if (angle < -180) angle += 360;
if (angle > 180) angle -= 360;
attr.angle = angle;
genline(&attr);
ww2 = attr.pxb_line->ww;
hh2 = attr.pxb_line->hh;
amx = (ax1 + ax2) / 2.0;
amy = (ay1 + ay2) / 2.0;
orgx2 = amx - ww2 / 2.0;
orgy2 = amy - hh2 / 2.0;
write(1);
zdialog_stuff(zd,"lnangle",attr.angle);
zdialog_stuff(zd,"length",attr.length);
return;
}
// write line on image at designated location
// mode: 1 erase old and write to new position
// 2 erase old and write nothing
void drawline_names::write(int mode)
{
using namespace drawline_names;
float *pix1, *pix3;
uint8 *pixL;
int px1, py1, px3, py3, done;
float e3part, Ot, Om, Ob;
int nc = E1pxm->nc, pcc = nc * sizeof(float);
cairo_t *cr = draw_context_create(gdkwin,draw_context);
if (linepresent)
{
for (py3 = orgy1; py3 < orgy1 + hh1; py3++) // erase prior line image
for (px3 = orgx1; px3 < orgx1 + ww1; px3++) // replace E3 pixels with E1 pixels
{ // in prior overlap rectangle
if (px3 < 0 || px3 >= Eww) continue;
if (py3 < 0 || py3 >= Ehh) continue;
pix1 = PXMpix(E1pxm,px3,py3);
pix3 = PXMpix(E3pxm,px3,py3);
memcpy(pix3,pix1,pcc);
}
}
done = 0;
if (mode == 2) done = 1; // erase only
if (mpx < 0 && mpy < 0) done = 1; // no position defined
if (done) {
if (linepresent) {
Fpaint3(orgx1,orgy1,ww1,hh1,cr); // update window to erase old line
linepresent = 0; // mark no line present
}
draw_context_destroy(draw_context);
return;
}
ww2 = attr.pxb_line->ww; // line image buffer
hh2 = attr.pxb_line->hh;
for (py1 = 0; py1 < hh2; py1++) // loop all pixels in line image
for (px1 = 0; px1 < ww2; px1++)
{
px3 = orgx2 + px1; // copy-to image3 pixel
py3 = orgy2 + py1;
if (px3 < 0 || px3 >= Eww) continue; // omit parts beyond edges
if (py3 < 0 || py3 >= Ehh) continue;
pixL = PXBpix(attr.pxb_line,px1,py1); // copy-from line pixel
pix3 = PXMpix(E3pxm,px3,py3); // copy-to image pixel
e3part = pixL[3] / 256.0; // line image transparency
pix3[0] = pixL[0] + e3part * pix3[0]; // combine line part + image part
pix3[1] = pixL[1] + e3part * pix3[1];
pix3[2] = pixL[2] + e3part * pix3[2];
if (nc > 3) {
Ot = (1.0 - e3part); // line opacity
Om = pix3[3] / 256.0; // image opacity
Ob = 1.0 - (1.0 - Ot) * (1.0 - Om); // combined opacity
pix3[3] = 255.0 * Ob;
}
}
if (linepresent) {
Fpaint3(orgx1,orgy1,ww1,hh1,cr); // update window to erase old line
linepresent = 0;
}
// (updates together to reduce flicker)
Fpaint3(orgx2,orgy2,ww2,hh2,cr); // update window for new line
draw_context_destroy(draw_context);
CEF->Fmods++;
CEF->Fsaved = 0;
linepresent = 1; // mark line is present
orgx1 = orgx2; // remember overlap rectangle
orgy1 = orgy2; // for next call
ww1 = ww2;
hh1 = hh2;
return;
}
/********************************************************************************/
// Create a graphic image of a line or arrow, any color,
// any size, any angle. Add outline and shadow colors.
int genline(lineattr_t *attr)
{
PXB * genline_outline(lineattr_t *, PXB *);
PXB * genline_shadow(lineattr_t *, PXB *);
cairo_surface_t *surface;
cairo_t *cr;
float angle;
int lnred, lngreen, lnblue;
int bgred, bggreen, bgblue;
int tored, togreen, toblue;
int shred, shgreen, shblue;
float lntransp, bgtransp, totransp, shtransp;
PXB *pxb_temp1, *pxb_temp2;
uint8 *cairo_data, *cpix;
uint8 *pix1, *pix2;
float length, width;
int px, py, ww, hh;
int red, green, blue;
float lnpart, topart, shpart, bgpart;
if (attr->pxb_line) PXB_free(attr->pxb_line);
angle = attr->angle; // line angle, degrees
attr->sinT = sin(angle/57.296); // trig funcs for line angle
attr->cosT = cos(angle/57.296);
lnred = atoi(substring(attr->color[0],'|',1)); // get line foreground color
lngreen = atoi(substring(attr->color[0],'|',2));
lnblue = atoi(substring(attr->color[0],'|',3));
bgred = atoi(substring(attr->color[1],'|',1)); // get line background color
bggreen = atoi(substring(attr->color[1],'|',2));
bgblue = atoi(substring(attr->color[1],'|',3));
tored = atoi(substring(attr->color[2],'|',1)); // get line outline color
togreen = atoi(substring(attr->color[2],'|',2));
toblue = atoi(substring(attr->color[2],'|',3));
shred = atoi(substring(attr->color[3],'|',1)); // get line shadow color
shgreen = atoi(substring(attr->color[3],'|',2));
shblue = atoi(substring(attr->color[3],'|',3));
lntransp = 0.01 * attr->transp[0]; // get transparencies
bgtransp = 0.01 * attr->transp[1]; // line, background, outline, shadow
totransp = 0.01 * attr->transp[2];
shtransp = 0.01 * attr->transp[3];
length = attr->length; // line dimensions
width = attr->width;
ww = length + 20; // create cairo surface
hh = width + 20; // with margins all around
if (attr->larrow || attr->rarrow) // wider if arrow head used
hh += width * 4;
attr->lww = ww;
attr->lhh = hh;
surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24,ww,hh);
cr = cairo_create(surface);
cairo_set_antialias(cr,CAIRO_ANTIALIAS_BEST);
cairo_set_line_width(cr,width);
cairo_set_line_cap(cr,CAIRO_LINE_CAP_ROUND);
cairo_move_to(cr, 10, hh/2.0); // draw line in middle of surface
cairo_line_to(cr, length+10, hh/2.0);
if (attr->larrow) { // add arrow heads if req.
cairo_move_to(cr, 10, hh/2.0);
cairo_line_to(cr, 10 + 2 * width, hh/2.0 - 2 * width);
cairo_move_to(cr, 10, hh/2.0);
cairo_line_to(cr, 10 + 2 * width, hh/2.0 + 2 * width);
}
if (attr->rarrow) {
cairo_move_to(cr, length+10, hh/2.0);
cairo_line_to(cr, length+10 - 2 * width, hh/2.0 - 2 * width);
cairo_move_to(cr, length+10, hh/2.0);
cairo_line_to(cr, length+10 - 2 * width, hh/2.0 + 2 * width);
}
cairo_stroke(cr);
cairo_data = cairo_image_surface_get_data(surface); // cairo image pixels
pxb_temp1 = PXB_make(ww,hh,3); // create PXB
for (py = 0; py < hh; py++) // copy image to PXB
for (px = 0; px < ww; px++)
{
cpix = cairo_data + 4 * (ww * py + px); // pango output is monocolor
pix2 = PXBpix(pxb_temp1,px,py);
pix2[0] = cpix[3]; // use red [0] for line intensity
pix2[1] = pix2[2] = 0;
}
cairo_destroy(cr); // free resources
cairo_surface_destroy(surface);
pxb_temp2 = genline_outline(attr,pxb_temp1); // add line outline if any
if (pxb_temp2) { // using green [1] for outline intensity
PXB_free(pxb_temp1);
pxb_temp1 = pxb_temp2;
}
pxb_temp2 = genline_shadow(attr,pxb_temp1); // add line shadow color if any
if (pxb_temp2) { // using blue [2] for shadow intensity
PXB_free(pxb_temp1);
pxb_temp1 = pxb_temp2;
}
if (fabsf(angle) > 0.1) { // rotate line if wanted
pxb_temp2 = PXB_rotate(pxb_temp1,angle);
PXB_free(pxb_temp1);
pxb_temp1 = pxb_temp2;
}
ww = pxb_temp1->ww; // line image input PXB
hh = pxb_temp1->hh;
pxb_temp2 = PXB_make(ww,hh,4); // line image output PXB
for (py = 0; py < hh; py++) // loop all pixels in line image
for (px = 0; px < ww; px++)
{
pix1 = PXBpix(pxb_temp1,px,py); // copy-from pixel (line + outline + shadow)
pix2 = PXBpix(pxb_temp2,px,py); // copy-to pixel
lnpart = pix1[0] / 256.0;
topart = pix1[1] / 256.0;
shpart = pix1[2] / 256.0;
bgpart = (1.0 - lnpart - topart - shpart);
lnpart = lnpart * (1.0 - lntransp);
topart = topart * (1.0 - totransp);
shpart = shpart * (1.0 - shtransp);
bgpart = bgpart * (1.0 - bgtransp);
red = lnpart * lnred + topart * tored + shpart * shred + bgpart * bgred;
green = lnpart * lngreen + topart * togreen + shpart * shgreen + bgpart * bggreen;
blue = lnpart * lnblue + topart * toblue + shpart * shblue + bgpart * bgblue;
pix2[0] = red; // output total red, green blue
pix2[1] = green;
pix2[2] = blue;
pix2[3] = 255 * (1.0 - lnpart - topart - shpart - bgpart); // image part visible through line
}
PXB_free(pxb_temp1);
attr->pxb_line = pxb_temp2;
return 0;
}
// add an outline color to the line edges
// red color [0] is original monocolor line
// use green color [1] for added outline
PXB * genline_outline(lineattr_t *attr, PXB *pxb1)
{
PXB *pxb2;
int toww, ww1, hh1, ww2, hh2;
int px, py, dx, dy;
uint8 *pix1, *pix2;
float theta;
toww = attr->towidth; // line outline color width
if (toww == 0) return 0; // zero
ww1 = pxb1->ww; // input PXB dimensions
hh1 = pxb1->hh;
ww2 = ww1 + toww * 2; // add margins for outline width
hh2 = hh1 + toww * 2;
pxb2 = PXB_make(ww2,hh2,3); // output PXB
for (py = 0; py < hh1; py++) // copy line pixels to outline pixels
for (px = 0; px < ww1; px++) // displaced by outline width
{
pix1 = PXBpix(pxb1,px,py);
pix2 = PXBpix(pxb2,px+toww,py+toww);
pix2[0] = pix1[0];
}
theta = 0.7 / toww;
for (theta = 0; theta < 6.3; theta += 0.7/toww) // displace outline pixels in all directions
{
dx = roundf(toww * sinf(theta));
dy = roundf(toww * cosf(theta));
for (py = 0; py < hh1; py++)
for (px = 0; px < ww1; px++)
{
pix1 = PXBpix(pxb1,px,py);
pix2 = PXBpix(pxb2,px+toww+dx,py+toww+dy);
if (pix2[1] < pix1[0] - pix2[0]) // compare line to outline brightness
pix2[1] = pix1[0] - pix2[0]; // brighter part is outline pixel
}
}
return pxb2; // pix2[0] / pix2[1] = line / outline
}
// add a shadow to the line edges
// red color [0] is original monocolor line intensity
// green color [1] is added outline if any
// use blue color [2] for added shadow
PXB * genline_shadow(lineattr_t *attr, PXB *pxb1)
{
PXB *pxb2;
int shww, ww1, hh1, ww2, hh2;
int px, py, dx, dy;
uint8 *pix1, *pix2;
float theta;
shww = attr->shwidth; // line shadow width
if (shww == 0) return 0; // zero
ww1 = pxb1->ww; // input PXB dimensions
hh1 = pxb1->hh;
ww2 = ww1 + shww * 2; // add margins for shadow width
hh2 = hh1 + shww * 2;
pxb2 = PXB_make(ww2,hh2,3); // output PXB
for (py = 0; py < hh1; py++) // copy line pixels to shadow pixels
for (px = 0; px < ww1; px++) // displaced by shadow width
{
pix1 = PXBpix(pxb1,px,py);
pix2 = PXBpix(pxb2,px+shww,py+shww);
pix2[0] = pix1[0];
pix2[1] = pix1[1];
}
theta = (90 - attr->shangle) / 57.3; // degrees to radians, 0 = to the right
dx = roundf(shww * sinf(theta));
dy = roundf(shww * cosf(theta));
for (py = 0; py < hh1; py++) // displace line by shadow width
for (px = 0; px < ww1; px++)
{
pix1 = PXBpix(pxb1,px,py);
pix2 = PXBpix(pxb2,px+shww+dx,py+shww+dy);
if (pix2[2] < pix1[0] + pix1[1] - pix2[0] - pix2[1]) // compare line+outline to shadow pixels
pix2[2] = pix1[0] + pix1[1] - pix2[0] - pix2[1]; // brighter part is shadow pixel
}
return pxb2; // pix2[0] / pix2[1] / pix2[2]
} // = line / outline / shadow brightness
/********************************************************************************/
// draw a box around an area drag-selected with the mouse
namespace drawbox_names
{
editfunc EFdrawbox;
int RGB[3]; // box line color
int width; // box line width
int bx1, by1, bx2, by2; // box NW and SE corners
}
// menu function
void m_draw_box(GtkWidget *, ch *menu)
{
using namespace drawbox_names;
int drawbox_dialog_event(zdialog *zd, ch *event);
void drawbox_mousefunc();
Plog(1,"m_draw_box \n");
zdialog *zd;
ch *tip = "drag mouse to draw box \n"
"shift + drag center to move box \n"
"shift + drag edge to move edge";
EFdrawbox.menufunc = m_draw_box;
EFdrawbox.menuname = "Markup Box";
EFdrawbox.Farea = 1; // select area ignored
EFdrawbox.mousefunc = drawbox_mousefunc;
if (! edit_setup(EFdrawbox)) return;
/***
___________________________________
| Draw box on image |
| drag mouse to draw box |
| shift + drag center to move box |
| shift + drag edge to move edge |
| |
| line color [###] line width [__] |
| |
| [Apply] [OK] [X] |
|___________________________________|
***/
zd = zdialog_new("Draw box on image",Mwin,"Apply","OK"," X ",null);
EFdrawbox.zd = zd;
zdialog_add_widget(zd,"label","labtip","dialog",tip,"space=3");
zdialog_add_widget(zd,"hbox","space","dialog",0,"space=5");
zdialog_add_widget(zd,"hbox","hbline","dialog");
zdialog_add_widget(zd,"label","labrgb","hbline","line color","space=3");
zdialog_add_widget(zd,"colorbutt","RGB","hbline","255|0|0","space=3");
zdialog_add_widget(zd,"label","space","hbline",0,"space=5");
zdialog_add_widget(zd,"label","labwidth","hbline","line width","space=3");
zdialog_add_widget(zd,"zspin","width","hbline","1|10|1|1","space=3");
zdialog_load_inputs(zd);
zdialog_run(zd,drawbox_dialog_event,"save");
zdialog_send_event(zd,"init");
takeMouse(drawbox_mousefunc,dragcursor);
bx1 = by1 = bx2 = by2 = 0; // no box yet
return;
}
// dialog event and completion function
int drawbox_dialog_event(zdialog *zd, ch *event)
{
using namespace drawbox_names;
ch color[20];
ch *pp;
if (strmatch(event,"done")) zd->zstat = 2; // apply and quit
if (strmatch(event,"cancel")) zd->zstat = 3; // cancel
if (zd->zstat)
{
if (zd->zstat == 1) { // apply
zd->zstat = 0;
PXM_free(E1pxm); // copy edits in E3 to source image E1
E1pxm = PXM_copy(E3pxm);
Fpaintnow(); // update image synchronous
return 1;
}
if (zd->zstat == 2) edit_done(0); // commit edit
else edit_cancel(0); // discard edit
m_markup(0,0); // restart main markup dialog
return 1;
}
if (zstrstr("init RGB width",event)) // set new line color and width
{
zdialog_fetch(zd,"RGB",color,19);
pp = substring(color,"|",1);
if (pp) RGB[0] = atoi(pp);
pp = substring(color,"|",2);
if (pp) RGB[1] = atoi(pp);
pp = substring(color,"|",3);
if (pp) RGB[2] = atoi(pp);
zdialog_fetch(zd,"width",width);
}
return 1;
}
// mouse function
// record drag start and current mouse position
// draw box with opposing corners at these positions
void drawbox_mousefunc()
{
using namespace drawbox_names;
void drawline(int x1, int y1, int x2, int y2, int RGB[3], int width);
int mx, my, dx, dy, bump;
int cx, cy, rx, ry, rr;
int min, dt, db, dl, dr;
if (Mxdrag || Mydrag) // drag underway
{
PXM_copy(E1pxm,E3pxm); // erase prior
if (KBshiftkey && bx1) // shift key, move prior box
{
dx = Mxdrag - Mxdown; // new drag displacement
dy = Mydrag - Mydown;
Mxdown = Mxdrag; // reset drag
Mydown = Mydrag;
Mxdrag = Mydrag = 0;
mx = Mxdown; // mouse position
my = Mydown;
cx = (bx1 + bx2) / 2; // box center
cy = (by1 + by2) / 2;
rx = mx - cx; // get mouse distance from center
ry = my - cy;
rr = sqrt(rx*rx + ry*ry);
dt = abs(my - by1); // get mouse distance from each edge
db = abs(my - by2); // top, bottom, left, right
dl = abs(mx - bx1);
dr = abs(mx - bx2);
min = dt; // find minimum edge distance
if (db < min) min = db;
if (dl < min) min = dl;
if (dr < min) min = dr;
if (rr < min) { // mouse is closer to center
bx1 += dx; // move unchanged box
by1 += dy;
bx2 += dx;
by2 += dy;
}
else {
if (min == dt) by1 = my; // move line closest to mouse
if (min == db) by2 = my;
if (min == dl) bx1 = mx;
if (min == dr) bx2 = mx;
}
}
else { // no shift key: create new box
bx1 = Mxdown;
by1 = Mydown;
bx2 = Mxdrag;
by2 = Mydrag;
Mxdrag = Mydrag = 0;
}
bump = 1; // fill last pixel
if (by2 < by1) bump = -1; // (bx2,by2) at NW corner of pixel
drawline(bx1,by1,bx2,by1,RGB,width);
drawline(bx1,by2,bx2,by2,RGB,width);
drawline(bx1,by1,bx1,by2,RGB,width);
drawline(bx2,by1,bx2,by2+bump,RGB,width);
CEF->Fmods++;
CEF->Fsaved = 0;
Fpaint2();
}
return;
}
/********************************************************************************/
// draw an oval around an area drag-selected with the mouse
namespace drawoval_names
{
editfunc EFdrawoval;
int RGB[3]; // oval line color
int width; // oval line width
int type = 1; // 1/2 = circle/oval
int mx1, my1, mx2, my2; // mouse drag coordinates
float exa, exb; // ellipse axes
float angle; // ellipse angle
int Fupdate;
}
// menu function
void m_draw_oval(GtkWidget *, ch *menu)
{
using namespace drawoval_names;
int drawoval_dialog_event(zdialog *zd, ch *event);
void drawoval_mousefunc();
Plog(1,"m_draw_oval \n");
zdialog *zd;
ch *tip = "drag mouse down/right to draw oval \n"
"shift + drag center to move oval \n"
"shift + drag lower right edge to change";
EFdrawoval.menufunc = m_draw_oval;
EFdrawoval.menuname = "Markup Oval";
EFdrawoval.Farea = 1; // select area ignored
EFdrawoval.mousefunc = drawoval_mousefunc;
if (! edit_setup(EFdrawoval)) return;
/***
_________________________________________
| Draw oval on image |
| drag mouse down/right to draw oval |
| shift + drag center to move oval |
| shift + drag lower right edge to change |
| |
| line color [###] line width [__] |
| [x] circle [x] oval angle [__] |
| |
| [Apply] [OK] [X] |
|_________________________________________|
***/
zd = zdialog_new("Draw oval on image",Mwin,"Apply","OK"," X ",null);
EFdrawoval.zd = zd;
zdialog_add_widget(zd,"label","labtip","dialog",tip,"space=3");
zdialog_add_widget(zd,"hbox","space","dialog",0,"space=5");
zdialog_add_widget(zd,"hbox","hbline","dialog");
zdialog_add_widget(zd,"label","labline","hbline","line color","space=3");
zdialog_add_widget(zd,"colorbutt","RGB","hbline","255|0|0","space=3");
zdialog_add_widget(zd,"label","space","hbline",0,"space=5");
zdialog_add_widget(zd,"label","labwidth","hbline","line width","space=3");
zdialog_add_widget(zd,"zspin","width","hbline","1|10|1|1","space=3");
zdialog_add_widget(zd,"hbox","hbco","dialog");
zdialog_add_widget(zd,"check","circle","hbco","circle","space=8");
zdialog_add_widget(zd,"label","space","hbco",0,"space=10");
zdialog_add_widget(zd,"check","oval","hbco","oval","space=3");
zdialog_add_widget(zd,"label","labangle","hbco","angle","space=3");
zdialog_add_widget(zd,"zspin","angle","hbco","0|90|1|0");
zdialog_load_inputs(zd);
zdialog_fetch(zd,"angle",angle);
zdialog_run(zd,drawoval_dialog_event,"save");
zdialog_send_event(zd,"init");
takeMouse(drawoval_mousefunc,dragcursor);
mx1 = my1 = 0; // no oval yet
Fupdate = 0;
return;
}
// dialog event and completion function
int drawoval_dialog_event(zdialog *zd, ch *event)
{
using namespace drawoval_names;
void drawoval_mousefunc();
ch color[20];
ch *pp;
if (strmatch(event,"done")) zd->zstat = 2; // apply and quit
if (strmatch(event,"cancel")) zd->zstat = 3; // cancel
if (zd->zstat)
{
if (zd->zstat == 1) { // apply
zd->zstat = 0;
PXM_free(E1pxm); // copy edits in E3 to source image E1
E1pxm = PXM_copy(E3pxm);
Fpaintnow(); // update image synchronous
return 1;
}
if (zd->zstat == 2) edit_done(0); // commit edit
else edit_cancel(0); // discard edit
m_markup(0,0); // restart main markup dialog
return 1;
}
if (zstrstr("init RGB width",event)) // set line color
{
zdialog_fetch(zd,"RGB",color,19);
pp = substring(color,"|",1);
if (pp) RGB[0] = atoi(pp);
pp = substring(color,"|",2);
if (pp) RGB[1] = atoi(pp);
pp = substring(color,"|",3);
if (pp) RGB[2] = atoi(pp);
zdialog_fetch(zd,"width",width); // line width
zdialog_stuff(zd,"oval",0);
zdialog_stuff(zd,"circle",0);
if (type == 1) zdialog_stuff(zd,"circle",1);
if (type == 2) zdialog_stuff(zd,"oval",1);
if (zstrstr("RGB width",event)) Fupdate = 1;
}
if (zstrstr("oval circle",event)) {
zdialog_stuff(zd,"oval",0);
zdialog_stuff(zd,"circle",0);
zdialog_stuff(zd,event,1);
if (strmatch(event,"oval")) type = 2;
else type = 1;
Fupdate = 1;
}
if (strmatch(event,"angle")) {
zdialog_fetch(zd,"angle",angle);
Fupdate = 1;
}
if (Fupdate) drawoval_mousefunc();
return 1;
}
// mouse function
// record drag start and current mouse position
// draw oval (ellipse) with major/minor axes from drag X/Y distances.
void drawoval_mousefunc()
{
using namespace drawoval_names;
void * drawoval_draw(void *arg);
int dx, dy, rx, ry, rr;
if (! Mxdrag && ! Mydrag && ! Fupdate) return;
Fupdate = 0;
if (Mxdrag || Mydrag) // drag underway
{
if (KBshiftkey && mx1) // shift key: move prior oval
{
dx = Mxdrag - Mxdown; // new drag displacement
dy = Mydrag - Mydown;
Mxdown = Mxdrag; // reset drag
Mydown = Mydrag;
Mxdrag = Mydrag = 0;
rx = mx1 - Mxdown; // get mouse distance from prior center
ry = my1 - Mydown;
rr = sqrtf(rx*rx + ry*ry);
if (rr < 0.6 * exa || rr < 0.6 * exb) { // small, move unchanged oval
mx1 += dx; // move center
my1 += dy;
}
mx2 += dx; // move edge (change size)
my2 += dy;
}
else { // no shift key: create new oval
mx1 = Mxdown;
my1 = Mydown;
mx2 = Mxdrag;
my2 = Mydrag;
Mxdrag = Mydrag = 0;
}
exa = abs(mx2 - mx1); // ellipse constants from
exb = abs(my2 - my1); // enclosing rectangle
}
drawoval_draw(0); // faster than thread
return;
}
// drawoval thread function - runs parallel with GTK stuff
void * drawoval_draw(void *arg)
{
using namespace drawoval_names;
void rotatepoint(float T, int cx, int cy, int &px, int &py);
void drawpoint(int px, int py, int RGB[3], int W);
int px, py;
float a2, b2;
float x, y, x2, y2, cx, cy;
float T;
PXM_copy(E1pxm,E3pxm); // erase prior
if (type == 1) // make a circle
{
if (exa > exb) exb = exa; // ellipse axes are equal
else exa = exb;
a2 = exa * exa;
b2 = exb * exb;
cx = mx1; // center at drag origin
cy = my1;
for (y = -exb; y < exb; y++) // step through y values
{
y2 = y * y;
x2 = a2 * (1 - y2 / b2);
x = sqrtf(x2); // corresp. x values, + and -
py = y + cy;
px = cx - x + 0.5;
drawpoint(px,py,RGB,width); // draw 2 points on ellipse
px = cx + x + 0.5;
drawpoint(px,py,RGB,width);
}
for (x = -exa; x < exa; x++) // step through x values
{
x2 = x * x;
y2 = b2 * (1 - x2 / a2);
y = sqrtf(y2); // corresp. y values, + and -
px = cx + x;
py = cy - y + 0.5;
drawpoint(px,py,RGB,width); // draw 2 points on ellipse
py = cy + y + 0.5;
drawpoint(px,py,RGB,width);
}
}
if (type == 2) // make an ellipse
{
a2 = exa * exa;
b2 = exb * exb;
cx = mx1; // center at drag origin
cy = my1;
T = PI * angle / 180;
for (y = -exb; y < exb; y++) // step through y values
{
y2 = y * y;
x2 = a2 * (1 - y2 / b2);
x = sqrtf(x2); // corresp. x values, + and -
px = cx - x + 0.5;
py = y + cy;
rotatepoint(T,cx,cy,px,py);
drawpoint(px,py,RGB,width); // draw 2 points on ellipse
px = cx + x + 0.5;
py = y + cy;
rotatepoint(T,cx,cy,px,py);
drawpoint(px,py,RGB,width);
}
for (x = -exa; x < exa; x++) // step through x values
{
x2 = x * x;
y2 = b2 * (1 - x2 / a2);
y = sqrtf(y2); // corresp. y values, + and -
px = cx + x;
py = cy - y + 0.5;
rotatepoint(T,cx,cy,px,py);
drawpoint(px,py,RGB,width); // draw 2 points on ellipse
px = cx + x;
py = cy + y + 0.5;
rotatepoint(T,cx,cy,px,py);
drawpoint(px,py,RGB,width);
}
}
CEF->Fmods++;
CEF->Fsaved = 0;
Fpaint2();
return 0;
}
/********************************************************************************/
// rotate a point (px,py) around point (cx,cy) by angle T
void rotatepoint(float T, int cx, int cy, int &px, int &py)
{
float R, T2;
R = sqrtf((px-cx)*(px-cx) + (py-cy)*(py-cy));
T2 = acosf((px-cx)/R);
if (py < cy) T2 = - T2;
T2 += T;
px = cx + R * cosf(T2);
py = cy + R * sinf(T2);
return;
}
/********************************************************************************/
// draw a line from pixel (x1,y1) to (x2,y2) with given pixel width.
// color is RGB[3], range 0-255.
void drawline(int x1, int y1, int x2, int y2, int RGB[3], int W)
{
void drawpoint(int px, int py, int RGB[3], int W);
float M;
int px, py, inc;
if (abs(x1-x2) > abs(y1-y2)) // line more horizontal
{
M = 1.0 * (y2-y1) / (x2-x1);
inc = 1;
if (x2 < x1) inc = -1;
for (px = x1; px != x2; px += inc) { // loop points in line center
py = y1 + M * (px-x1);
drawpoint(px,py,RGB,W); // color center + neighbor points
}
}
else
{
M = 1.0 * (x2-x1) / (y2-y1); // line more vertical
inc = 1;
if (y2 < y1) inc = -1;
for (py = y1; py != y2; py += inc) {
px = x1 + M * (py-y1);
drawpoint(px,py,RGB,W);
}
}
return;
}
// draw one 'point' of line (point size = line width)
void drawpoint(int px, int py, int RGB[3], int W)
{
int qx, qy, w1, w2;
float *pix3;
w1 = W / 2;
w2 = w1;
if (!(W & 1)) w2 -= 1;
for (qy = py-w1; qy <= py+w2; qy++) // loop pixels overlapping point
for (qx = px-w1; qx <= px+w2; qx++)
{
if (qx < 0 || qx > Eww-1) continue;
if (qy < 0 || qy > Ehh-1) continue;
pix3 = PXMpix(E3pxm,qx,qy);
pix3[0] = RGB[0];
pix3[1] = RGB[1];
pix3[2] = RGB[2];
}
return;
}
/********************************************************************************/
// make a black & white or color positive or negative, or sepia image
namespace colormode_names
{
editfunc EFcolormode;
int mode;
float mblend;
}
// menu function
void m_color_mode(GtkWidget *, ch *menu)
{
using namespace colormode_names;
int colormode_dialog_event(zdialog *zd, ch *event);
void * colormode_thread(void *);
F1_help_topic = "color mode";
Plog(1,"m_color_mode \n");
EFcolormode.menuname = "Color Mode";
EFcolormode.menufunc = m_color_mode;
EFcolormode.threadfunc = colormode_thread;
EFcolormode.FprevReq = 1; // use preview
EFcolormode.Farea = 2; // select area usable
EFcolormode.Fpaintedits = 1; // allow paint edits
EFcolormode.Fscript = 1; // scripting supported
if (! edit_setup(EFcolormode)) return; // setup edit
/***
_____________________________
| Color Mode |
| |
| [_] reset | // check boxes
| [_] black/white positive |
| [_] black/white negative |
| [_] color negative |
| [_] RGB -> GBR |
| [_] RGB -> BRG |
| [_] sepia |
| |
| 0% ===========[]==== 100% |
| |
| [OK] [X] |
|_____________________________|
***/
zdialog *zd = zdialog_new("Color Mode",Mwin,"OK"," X ",null);
EFcolormode.zd = zd;
zdialog_add_widget(zd,"check","reset","dialog","reset");
zdialog_add_widget(zd,"check","b&wpos","dialog","black/white positive");
zdialog_add_widget(zd,"check","b&wneg","dialog","black/white negative");
zdialog_add_widget(zd,"check","colneg","dialog","color negative");
zdialog_add_widget(zd,"check","rgb-gbr","dialog","RGB -> GBR");
zdialog_add_widget(zd,"check","rgb-brg","dialog","RGB -> BRG");
zdialog_add_widget(zd,"check","sepia","dialog","sepia");
zdialog_add_widget(zd,"hbox","hbmblend","dialog");
zdialog_add_widget(zd,"label","lab0","hbmblend","0%","space=5");
zdialog_add_widget(zd,"hscale","mblend","hbmblend","0.0|1.0|0.01|1.0","expand");
zdialog_add_widget(zd,"label","lab100","hbmblend","100%","space=5");
zdialog_resize(zd,200,0);
zdialog_run(zd,colormode_dialog_event,"save"); // run dialog - parallel
return;
}
// dialog event and completion callback function
int colormode_dialog_event(zdialog *zd, ch *event)
{
using namespace colormode_names;
if (strmatch(event,"done")) zd->zstat = 1; // from edit_setup() or f_save()
if (strmatch(event,"cancel")) zd->zstat = 2; // from f_open()
if (strmatch(event,"fullsize")) { // from select area
edit_fullsize(); // get full size image
thread_signal();
thread_wait(); // required for paint edits
return 1;
}
if (zd->zstat)
{
if (zd->zstat == 1) {
edit_fullsize(); // get full size image
thread_signal();
edit_addhist("mode:%d blend:%.2f",mode,mblend); // edit params > edit hist
edit_done(0); // commit edit
}
else edit_cancel(0); // discard edit
return 1;
}
if (strmatch(event,"focus")) return 1;
if (zstrstr("reset b&wpos b&wneg colneg rgb-gbr rgb-brg sepia",event))
{
zdialog_stuff(zd,"reset",0);
zdialog_stuff(zd,"b&wpos",0);
zdialog_stuff(zd,"b&wneg",0);
zdialog_stuff(zd,"colneg",0);
zdialog_stuff(zd,"rgb-gbr",0);
zdialog_stuff(zd,"rgb-brg",0);
zdialog_stuff(zd,"sepia",0);
zdialog_stuff(zd,event,1);
if (strmatch(event,"reset")) mode = 0;
if (strmatch(event,"b&wpos")) mode = 1;
if (strmatch(event,"b&wneg")) mode = 2;
if (strmatch(event,"colneg")) mode = 3;
if (strmatch(event,"rgb-gbr")) mode = 4;
if (strmatch(event,"rgb-brg")) mode = 5;
if (strmatch(event,"sepia")) mode = 6;
}
zdialog_fetch(zd,"mblend",mblend);
if (mode == 0) {
edit_reset();
return 1;
}
thread_signal();
return 1;
}
// thread function
void * colormode_thread(void *)
{
using namespace colormode_names;
void * colormode_wthread(void *arg);
if (Fpaintedits && ! Mdrag) return 0; // if mouse paint, must be drag
get_edit_pixels_init(NSMP,0); // initz. pixel loop
do_wthreads(colormode_wthread,NSMP); // worker threads
CEF->Fmods++; // image modified
CEF->Fsaved = 0; // not saved
if (! Fpaintedits) Fpaint2(); // update window
return 0;
}
// worker thread functions
void * colormode_wthread(void *arg)
{
using namespace colormode_names;
int index = *((int *) (arg));
int px, py, Fend;
float R1, G1, B1, R3, G3, B3, R9, G9, B9;
float *pix1, *pix3;
float brite, ff1, ff2, blend;
while (true)
{
Fend = get_edit_pixels(index,px,py,blend);
if (Fend) break;
if (blend == 0) continue;
pix1 = PXMpix(E1pxm,px,py); // input pixel
pix3 = PXMpix(E3pxm,px,py); // output pixel
R1 = pix1[0];
G1 = pix1[1];
B1 = pix1[2];
R3 = R9 = pix3[0];
G3 = G9 = pix3[1];
B3 = B9 = pix3[2];
switch (mode)
{
case 1: { // black and white positive
R9 = G9 = B9 = 0.333 * (R1 + G1 + B1);
break;
}
case 2: { // black and white negative
R9 = G9 = B9 = 255.9 - 0.333 * (R1 + G1 + B1);
break;
}
case 3: { // color negative
R9 = 255.9 - R1;
G9 = 255.9 - G1;
B9 = 255.9 - B1;
break;
}
case 4: { // RGB - GBR
R9 = G1;
G9 = B1;
B9 = R1;
break;
}
case 5: { // RGB - BRG
R9 = B1;
G9 = R1;
B9 = G1;
break;
}
case 6: { // sepia
brite = R1;
if (G1 > brite) brite = G1; // max. color level
if (B1 > brite) brite = B1;
brite = 0.2 * brite + 0.2666 * (R1 + G1 + B1); // brightness, 0.0 ... 255.9
brite = brite * 0.003906; // 0.0 ... 1.0
ff1 = 1.0 - 0.7 * brite; // sepia part, 1.0 ... 0.3
ff2 = 1.0 - ff1; // B & W part, 0.0 ... 0.7
R9 = ff1 * 255.0 + ff2 * 256; // combine max. sepia with white
G9 = ff1 * 150.0 + ff2 * 256;
B9 = ff1 * 46.0 + ff2 * 256;
brite = 0.8333 * (brite + 0.2); // add brightness at low end
R9 = R9 * brite; // output = combined color * brightness
G9 = G9 * brite;
B9 = B9 * brite;
break;
}
}
RGBfix(R9,G9,B9); // prevent underflow/overflow
if (mblend < 1.0) { // image blend slider
ff1 = mblend;
ff2 = 1.0 - ff1;
R9 = ff1 * R9 + ff2 * R1; // blend input and edited images
G9 = ff1 * G9 + ff2 * G1;
B9 = ff1 * B9 + ff2 * B1;
}
if (Fpaintedits) // mouse paint edit
{
if (blend > 0)
{ // increase edit
R3 = blend * R9 + (1-blend) * R3;
G3 = blend * G9 + (1-blend) * G3;
B3 = blend * B9 + (1-blend) * B3;
}
else if (blend < 0)
{ // decrease edit
R3 = -blend * R1 + (1+blend) * R3;
G3 = -blend * G1 + (1+blend) * G3;
B3 = -blend * B1 + (1+blend) * B3;
}
}
else // full image edit
{
R3 = blend * R9 + (1-blend) * R1;
G3 = blend * G9 + (1-blend) * G1;
B3 = blend * B9 + (1-blend) * B1;
}
pix3[0] = R3;
pix3[1] = G3;
pix3[2] = B3;
}
return 0;
}
/********************************************************************************/
// Current edit function is applied to areas painted with the mouse.
// Mouse can be weak or strong, and edits are applied incrementally.
void m_paint_edits(GtkWidget *, ch *) // menu function
{
int paint_edits_dialog_event(zdialog *, ch *event); // dialog event function
void paint_edits_mousefunc(); // mouse function
ch *title = "Paint Edits";
F1_help_topic = "paint edits";
Plog(1,"m_paint_edits \n");
if (Nfuncbusy) {
Plog(0,"paint edits: function busy \n");
return;
}
if (! CEF) { // edit func must be active
zmessageACK(Mwin,"Edit function must be active");
return;
}
if (! CEF->Fpaintedits) {
zmessageACK(Mwin,"Edit function can not use Paint Edits");
return;
}
if (zd_paintedits) {
zmessageACK(Mwin,"Paint Edits already active");
return;
}
if (CEF->Fpreview)
zdialog_send_event(CEF->zd,"fullsize"); // use full-size image
/***
______________________________________
| Press F1 for help |
| |
| Mouse Radius [____] |
| Power: Center [____] Edge [____] |
| |
| [OK] |
|______________________________________|
***/
zd_paintedits = zdialog_new(title,Mwin,"OK",null);
zdialog *zd = zd_paintedits;
zdialog_add_widget(zd,"label","labhelp1","dialog","Press F1 for help","space=5");
zdialog_add_widget(zd,"hbox","hbr","dialog",0,"space=3");
zdialog_add_widget(zd,"label","labr","hbr","Mouse Radius","space=5");
zdialog_add_widget(zd,"zspin","radius","hbr","2|500|1|50");
zdialog_add_widget(zd,"hbox","hbt","dialog",0,"space=3");
zdialog_add_widget(zd,"label","labtc","hbt","Power: " "Center","space=5");
zdialog_add_widget(zd,"zspin","center","hbt","0|100|1|50");
zdialog_add_widget(zd,"label","labte","hbt","Edge","space=5");
zdialog_add_widget(zd,"zspin","edge","hbt","0|100|1|0");
Mradius = 50;
MCpower = 50;
MEpower = 0;
zdialog_load_inputs(zd);
zdialog_fetch(zd,"radius",Mradius);
zdialog_fetch(zd,"center",MCpower);
zdialog_fetch(zd,"edge",MEpower);
zdialog_run(zd,paint_edits_dialog_event,"save"); // run dialog - parallel
Fpaintedits = 1; // activate mouse paint mode
return;
}
// dialog event and completion function
int paint_edits_dialog_event(zdialog *zd, ch *event)
{
void paint_edits_mousefunc(); // mouse function
if (strmatch(event,"focus")) // toggle mouse capture
{
if (CEF && CEF->Fpaintedits) // check edit function OK
takeMouse(paint_edits_mousefunc,0);
else { // no edit function active
freeMouse();
return 1;
}
}
if (strmatch(event,"done")) zd->zstat = 1; // from edit_done()
if (strmatch(event,"cancel")) zd->zstat = 2; // from edit_cancel()
if (zd->zstat) // done or cancel
{
if (CEF) zdialog_send_event(CEF->zd,"done"); // complete edit
zdialog_free(zd); // kill dialog
zd_paintedits = 0;
Fpaintedits = 0; // paint_edits not active
freeMouse(); // disconnect mouse
return 1;
}
if (strmatch(event,"radius"))
zdialog_fetch(zd,"radius",Mradius); // set mouse radius
if (strmatch(event,"center"))
zdialog_fetch(zd,"center",MCpower); // set mouse center power 0-100
if (strmatch(event,"edge"))
zdialog_fetch(zd,"edge",MEpower); // set mouse edge power 0-100
return 1;
}
// mouse function - adjust edit strength for areas within mouse radius
// "edge distance" is increased for more strength, decreased for less
void paint_edits_mousefunc()
{
if (! CEF || ! CEF->Fpaintedits) { // no active edit
Fpaintedits = 0;
freeMouse(); // disconnect mouse
return;
}
draw_mousecircle(Mxposn,Myposn,Mradius,0,0); // show mouse selection circle
if (! Mdrag) return; // wait for drag event
Mxdrag = Mydrag = 0; // prevent image drag
zdialog_send_event(CEF->zd,"paint"); // do edit function within mouse
Fpaintmouse(Mxposn,Myposn,Mradius); // update drawing window
draw_mousecircle(Mxposn,Myposn,Mradius,0,0); // refresh mouse selection circle
return;
}
/********************************************************************************/
// Pixel paint function - paint individual pixels with the mouse.
// The mouse circle paints a selected color.
namespace paint_image_names
{
int paint_dialog_event(zdialog* zd, ch *event);
void paint_mousefunc();
void paint_dopixels(int px, int py); // update pixel block
void paint_savepixB(int px, int py); // save pixel block for poss. undo
void paint_undolastB(); // undo last pixel block, free memory
void paint_freefirstB(); // free memory for first pixel block
void paint_freeallB(); // free memory for all pixel blocks
uint8 RGB[3] = { 0, 0, 0 }; // color to paint
int mode; // 1/2 = paint / erase
int Mrad; // mouse radius
int Fptran = 0; // flag, paint over transparent areas
int Fdrag = 0; // flag, mouse drags image
int nc, ac; // no. channels, alpha channel
float kernel[202][202]; // Mrad <= 100
int64 maxmem = (int64) 4000 * MEGA; // max. pixel block memory (4 GB)
int64 totmem; // pixB memory allocated
int maxpixB = 10000; // max. pixel blocks
int totpixB = 0; // total pixel blocks
int pixBseq = 0; // last pixel block sequence no.
typedef struct { // pixel block before edit
int seq; // block sequence no.
uint16 px, py; // center pixel (radius org.)
uint16 radius; // radius of pixel block
float pixel[][4]; // array of pixel[npix][4]
} pixBmem_t;
pixBmem_t **pixBmem = 0; // *pixBmem_t[]
int pixBmem_cc = 12; // all except pixel array + pad
int pcc4 = 4 * sizeof(float); // pixel cc: RGBA = 4 channels
editfunc EFpaint;
}
// menu function
void m_paint_image(GtkWidget *, ch *menu) // separate paint and copy
{
using namespace paint_image_names;
ch *mess1 = "shift + left click: pick color from image \n"
"left drag: paint color on image \n" // remove click actions
"right drag: restore original image";
ch *dash = " \xcc\xb6 ";
F1_help_topic = "paint image";
Plog(1,"m_paint_image \n");
EFpaint.menufunc = m_paint_image;
EFpaint.menuname = "Paint Image";
EFpaint.Farea = 2; // select area usable
EFpaint.mousefunc = paint_mousefunc; // mouse function
if (! edit_setup(EFpaint)) return; // setup edit
/****
__________________________________________________
| Paint on Image |
| |
| shift + left click: pick color from image |
| left drag: paint color on image |
| right drag: restore original image |
| |
| paint color [######] [palette] [HSL] |
| |
| brush size NN ============[]============= |
| opacity center NN ==================[]======= |
| opacity edge NN ====[]===================== |
| |
| (o) paint (o) erase [undo last] [undo all] |
| [x] include transparent areas |
| [x] drag image zoom image [+] [-] |
| |
| [OK] [X] |
|__________________________________________________|
****/
zdialog *zd = zdialog_new("Paint on Image",Mwin,"OK"," X ",null);
EFpaint.zd = zd;
zdialog_add_widget(zd,"label","labm","dialog",mess1,"space=5");
zdialog_add_widget(zd,"hbox","hbp","dialog",0,"space=3");
zdialog_add_widget(zd,"label","labp","hbp","paint color","space=5");
zdialog_add_widget(zd,"colorbutt","colorbutt","hbp","255|0|0");
zdialog_add_widget(zd,"label","space","hbp",0,"space=10");
zdialog_add_widget(zd,"button","palette","hbp","palette","space=10");
zdialog_add_widget(zd,"button","HSL","hbp","HSL");
zdialog_add_widget(zd,"hbox","hbbru","dialog",0,"space=5");
zdialog_add_widget(zd,"vbox","vbbru1","hbbru",0,"homog|space=1");
zdialog_add_widget(zd,"vbox","vbbru2","hbbru",0,"homog|space=1");
zdialog_add_widget(zd,"vbox","vbbru3","hbbru",0,"homog|expand|space=1");
zdialog_add_widget(zd,"label","labbr","vbbru1","brush size");
zdialog_add_widget(zd,"label","laboc","vbbru1","opacity center");
zdialog_add_widget(zd,"label","laboe","vbbru1","opacity edge");
zdialog_add_widget(zd,"label","labbrNN","vbbru2","NN");
zdialog_add_widget(zd,"label","labocNN","vbbru2","NNN");
zdialog_add_widget(zd,"label","laboeNN","vbbru2","NNN");
zdialog_add_widget(zd,"hscale","Mrad","vbbru3","1|100|1|20","expand");
zdialog_add_widget(zd,"hscale","opccent","vbbru3","1|100|1|50","expand");
zdialog_add_widget(zd,"hscale","opcedge","vbbru3","0|100|1|100","expand");
zdialog_add_widget(zd,"hbox","hbp","dialog",0,"space=3");
zdialog_add_widget(zd,"radio","paint","hbp","paint","space=3");
zdialog_add_widget(zd,"radio","erase","hbp","erase");
zdialog_add_widget(zd,"button","undlast","hbp","Undo Last","space=5");
zdialog_add_widget(zd,"button","undall","hbp","Undo All");
zdialog_add_widget(zd,"hbox","hbt","dialog",0,"space=3");
zdialog_add_widget(zd,"check","Fptran","hbt","include transparent areas","space=3");
zdialog_add_widget(zd,"hbox","hbd","dialog");
zdialog_add_widget(zd,"check","Fdrag","hbd","drag image","space=3");
zdialog_add_widget(zd,"label","space","hbd",0,"space=10");
zdialog_add_widget(zd,"label","labzoom","hbd","zoom image","space=3");
zdialog_add_widget(zd,"button","zoom+","hbd"," + ","space=3");
zdialog_add_widget(zd,"button","zoom-","hbd",dash,"space=3");
zdialog_rescale(zd,"Mrad",1,2,100); // stretch scales at sensitive end
zdialog_rescale(zd,"opccent",1,2,100);
zdialog_rescale(zd,"opcedge",0,1,100);
zdialog_load_inputs(zd); // preload prior user inputs
zdialog_stuff(zd,"Fptran",0); // initialize
zdialog_stuff(zd,"paint",1);
zdialog_stuff(zd,"erase",0);
zdialog_stuff(zd,"Fdrag",0);
mode = 1;
Fdrag = 0;
zdialog_run(zd,paint_dialog_event,"save"); // run dialog, parallel
zdialog_send_event(zd,"colorbutt"); // initialize paint color
zdialog_send_event(zd,"Mrad"); // get kernel initialized
zdialog_fetch(zd,"Fptran",Fptran); // paint over transparent areas
totmem = 0; // memory used
pixBmem = 0; // pixel block memory
totpixB = 0; // pixel blocks
pixBseq = 0;
ac = 0;
nc = E1pxm->nc; // channels, RGBA
if (nc > 3) ac = 1; // alpha channel present
takeMouse(paint_mousefunc,drawcursor); // connect mouse function
return;
}
// dialog event and completion callback function
int paint_image_names::paint_dialog_event(zdialog *zd, ch *event)
{
using namespace paint_image_names;
ch *pp;
ch color[20], text[20];
float opccent, opcedge; // center and edge opacities
int paint, radius, dx, dy, err;
float rad, kern;
if (strmatch(event,"done")) zd->zstat = 1; // from edit_setup() or f_save()
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
paint_freeallB(); // free pixel block memory
return 1;
}
draw_mousecircle(0,0,0,1,0); // erase mouse circle
if (strmatch(event,"focus")) // toggle mouse capture
takeMouse(paint_mousefunc,drawcursor);
if (strmatch(event,"colorbutt"))
{
zdialog_fetch(zd,"colorbutt",color,19); // get paint color from color button
pp = substring(color,"|",1);
if (pp) RGB[0] = atoi(pp);
pp = substring(color,"|",2);
if (pp) RGB[1] = atoi(pp);
pp = substring(color,"|",3);
if (pp) RGB[2] = atoi(pp);
}
if (strmatch(event,"palette"))
err = RGB_chooser(zd,"colorbutt",RGB); // select color from palette
if (strmatch(event,"HSL")) {
err = HSL_chooser(zd,"colorbutt",RGB); // select color from palette
if (err) return 1;
snprintf(color,20,"%d|%d|%d",RGB[0],RGB[1],RGB[2]);
zdialog_stuff(zd,"colorbutt",color);
}
if (zstrstr("Mrad opccent opcedge",event))
{
zdialog_fetch(zd,"Mrad",Mrad); // get new brush attributes
zdialog_fetch(zd,"opccent",opccent);
zdialog_fetch(zd,"opcedge",opcedge);
sprintf(text,"%3d",Mrad); // stuff corresp. number values
zdialog_stuff(zd,"labbrNN",text);
sprintf(text,"%3.0f",opccent);
zdialog_stuff(zd,"labocNN",text);
sprintf(text,"%3.0f",opcedge);
zdialog_stuff(zd,"laboeNN",text);
opccent = 0.01 * opccent; // opacity 0 ... 1
opcedge = 0.01 * opcedge;
opccent = pow(opccent,2.0); // change response curve
opcedge = opccent * opcedge; // edge relative to center
radius = Mrad;
for (dy = -radius; dy <= radius; dy++) // build kernel
for (dx = -radius; dx <= radius; dx++)
{
rad = sqrt(dx*dx + dy*dy);
kern = (radius - rad) / radius; // center ... edge >> 1 ... 0
kern = kern * (opccent - opcedge) + opcedge; // opacity center ... edge
if (rad > radius) kern = 0; // beyond radius, within square
if (kern < 0) kern = 0;
if (kern > 1) kern = 1;
kernel[dx+radius][dy+radius] = kern;
}
}
if (strmatch(event,"undlast")) // undo last edit (click or drag)
paint_undolastB();
if (strmatch(event,"undall")) { // undo all edits
edit_reset();
paint_freeallB();
}
if (strmatch(event,"Fptran")) // flag, paint over transparency
zdialog_fetch(zd,"Fptran",Fptran);
if (zstrstr("paint erase",event)) { // set paint or erase mode
zdialog_fetch(zd,"paint",paint);
if (paint) mode = 1;
else mode = 2;
}
if (strmatch(event,"Fdrag")) // mouse drags image
zdialog_fetch(zd,"Fdrag",Fdrag);
if (strmatch(event,"zoom+")) m_zoom(0,"in"); // zoom image in or out
if (strmatch(event,"zoom-")) m_zoom(0,"out");
return 1;
}
// mouse function
void paint_image_names::paint_mousefunc() // no action on clicks
{
using namespace paint_image_names;
static int pmxdown = 0, pmydown = 0;
static int pmxdrag = 0, pmydrag = 0;
ch color[20];
float *pix3;
zdialog *zd = EFpaint.zd;
if (Fdrag) return; // pass through to main()
if (LMclick && KBshiftkey) // shift + left mouse click
{
LMclick = 0;
pix3 = PXMpix(E3pxm,Mxclick,Myclick); // pick new color from image
RGB[0] = pix3[0];
RGB[1] = pix3[1];
RGB[2] = pix3[2];
snprintf(color,19,"%d|%d|%d",RGB[0],RGB[1],RGB[2]);
if (zd) zdialog_stuff(zd,"colorbutt",color);
return;
}
if (Mxdrag || Mydrag) // drag in progress
{
if (Mxdown != pmxdown || Mydown != pmydown) { // new drag
pixBseq++; // new undo seq. no.
pmxdown = Mxdown;
pmydown = Mydown;
}
if (Mxdrag == pmxdrag && Mydrag == pmydrag) { // no movement
Mxdrag = Mydrag = 0;
return;
}
pmxdrag = Mxdrag;
pmydrag = Mydrag;
paint_dopixels(Mxdrag,Mydrag); // do 1 block of pixels
}
draw_mousecircle(Mxposn,Myposn,Mrad,0,0); // draw mouse circle
Mxdrag = Mydrag = LMclick = 0;
return;
}
// paint or erase 1 block of pixels within mouse radius of px, py
void paint_image_names::paint_dopixels(int px, int py)
{
using namespace paint_image_names;
float *pix1, *pix3;
int radius, dx, dy, qx, qy;
int ii, ww, hh, dist = 0;
int pot = ac * Fptran; // paint over transparent areas
float red, green, blue;
float kern;
cairo_t *cr = draw_context_create(gdkwin,draw_context);
draw_mousecircle(0,0,0,1,cr); // erase mouse circle
ww = Eww;
hh = Ehh;
paint_savepixB(px,py); // save pixels for poss. undo
red = RGB[0]; // paint color
green = RGB[1];
blue = RGB[2];
radius = Mrad;
if (mode == 1) { // paint
CEF->Fmods++;
CEF->Fsaved = 0;
}
for (dy = -radius; dy <= radius; dy++) // loop surrounding block of pixels
for (dx = -radius; dx <= radius; dx++)
{
qx = px + dx;
qy = py + dy;
if (qx < 0 || qx > ww-1) continue;
if (qy < 0 || qy > hh-1) continue;
if (sa_stat == sa_stat_fini) { // select area active
ii = qy * ww + qx;
dist = sa_pixmap[ii];
if (! dist) continue; // pixel is outside area
}
kern = kernel[dx+radius][dy+radius]; // mouse opacities
if (kern == 0) continue; // outside mouse radius
pix1 = PXMpix(E1pxm,qx,qy); // source image pixel
pix3 = PXMpix(E3pxm,qx,qy); // edited image pixel
if (mode == 1 && Mbutton < 2) // paint
{
kern = kern * (1.0 + 50.0 * wacom_pressure); // wacom
if (kern > 1.0) kern = 1.0;
pix3[0] = kern * red + (1.0 - kern) * pix3[0]; // overpaints accumulate
pix3[1] = kern * green + (1.0 - kern) * pix3[1];
pix3[2] = kern * blue + (1.0 - kern) * pix3[2];
if (pot) pix3[3] = kern * 255.0 + (1.0 - kern) * pix3[3]; // overpaint transparent area
}
if (mode == 2 || Mbutton >= 2) // erase
{
kern = kern * (2.0 + 50.0 * wacom_pressure); // wacom
if (kern > 1.0) kern = 1.0;
if (kern > 1) kern = 1;
pix3[0] = kern * pix1[0] + (1.0 - kern) * pix3[0]; // gradual erase
pix3[1] = kern * pix1[1] + (1.0 - kern) * pix3[1];
pix3[2] = kern * pix1[2] + (1.0 - kern) * pix3[2];
if (pot) pix3[3] = kern * pix1[3] + (1.0 - kern) * pix3[3];
}
}
px = px - radius - 1; // repaint modified area
py = py - radius - 1;
ww = 2 * radius + 3;
Fpaint3(px,py,ww,ww,cr);
draw_context_destroy(draw_context);
return;
}
// save 1 block of pixels for possible undo
void paint_image_names::paint_savepixB(int px, int py)
{
using namespace paint_image_names;
int cc, npix, radius, dx, dy;
float *pix3;
pixBmem_t *paintsave1;
if (! pixBmem) { // first time
cc = maxpixB * sizeof(void *);
pixBmem = (pixBmem_t **) zmalloc(cc,"paint image");
totpixB = 0;
totmem = 0;
}
if (totmem > maxmem || totpixB == maxpixB) // free memory for oldest updates
while (totmem > 0.7 * maxmem || totpixB > 0.7 * maxpixB)
paint_freefirstB();
radius = Mrad;
npix = 0;
for (dy = -radius; dy <= radius; dy++) // count pixels in block
for (dx = -radius; dx <= radius; dx++)
{
if (px + dx < 0 || px + dx > Eww-1) continue;
if (py + dy < 0 || py + dy > Ehh-1) continue;
npix++;
}
cc = npix * pcc4 + pixBmem_cc;
paintsave1 = (pixBmem_t *) zmalloc(cc,"paint image"); // allocate memory for block
pixBmem[totpixB] = paintsave1;
totpixB += 1;
totmem += cc;
paintsave1->seq = pixBseq; // save pixel block poop
paintsave1->px = px;
paintsave1->py = py;
paintsave1->radius = radius;
npix = 0;
for (dy = -radius; dy <= radius; dy++) // save pixels in block
for (dx = -radius; dx <= radius; dx++)
{
if (px + dx < 0 || px + dx > Eww-1) continue;
if (py + dy < 0 || py + dy > Ehh-1) continue;
pix3 = PXMpix(E3pxm,(px+dx),(py+dy)); // edited image pixel
paintsave1->pixel[npix][0] = pix3[0];
paintsave1->pixel[npix][1] = pix3[1];
paintsave1->pixel[npix][2] = pix3[2];
if (ac) paintsave1->pixel[npix][3] = pix3[3];
npix++;
}
return;
}
// undo last pixel block (newest edit) and free memory
void paint_image_names::paint_undolastB()
{
using namespace paint_image_names;
int ii, cc, npix, radius;
int ww, px, py, dx, dy;
float *pix3;
pixBmem_t *paintsave1;
for (ii = totpixB-1; ii >= 0; ii--)
{
paintsave1 = pixBmem[ii];
if (paintsave1->seq != pixBseq) break;
px = paintsave1->px;
py = paintsave1->py;
radius = paintsave1->radius;
npix = 0;
for (dy = -radius; dy <= radius; dy++)
for (dx = -radius; dx <= radius; dx++)
{
if (px + dx < 0 || px + dx > Eww-1) continue;
if (py + dy < 0 || py + dy > Ehh-1) continue;
pix3 = PXMpix(E3pxm,(px+dx),(py+dy));
pix3[0] = paintsave1->pixel[npix][0];
pix3[1] = paintsave1->pixel[npix][1];
pix3[2] = paintsave1->pixel[npix][2];
if (ac) pix3[3] = paintsave1->pixel[npix][3];
npix++;
}
px = px - radius - 1;
py = py - radius - 1;
ww = 2 * radius + 3;
Fpaint3(px,py,ww,ww,0);
zfree(paintsave1);
pixBmem[ii] = 0;
cc = npix * pcc4 + pixBmem_cc;
totmem -= cc;
totpixB--;
}
if (pixBseq > 0) --pixBseq;
return;
}
// free memory for first pixel block (oldest edit)
void paint_image_names::paint_freefirstB()
{
using namespace paint_image_names;
int firstseq;
int ii, jj, cc, npix, radius;
int px, py, dx, dy;
pixBmem_t *paintsave1;
if (! totpixB) return;
firstseq = pixBmem[0]->seq;
for (ii = 0; ii < totpixB; ii++)
{
paintsave1 = pixBmem[ii];
if (paintsave1->seq != firstseq) break;
px = paintsave1->px;
py = paintsave1->py;
radius = paintsave1->radius;
npix = 0;
for (dy = -radius; dy <= radius; dy++)
for (dx = -radius; dx <= radius; dx++)
{
if (px + dx < 0 || px + dx > Eww-1) continue;
if (py + dy < 0 || py + dy > Ehh-1) continue;
npix++;
}
zfree(paintsave1);
pixBmem[ii] = 0;
cc = npix * pcc4 + pixBmem_cc;
totmem -= cc;
}
for (jj = 0; ii < totpixB; jj++, ii++)
pixBmem[jj] = pixBmem[ii];
totpixB = jj;
return;
}
// free all pixel block memory
void paint_image_names::paint_freeallB()
{
using namespace paint_image_names;
int ii;
pixBmem_t *paintsave1;
for (ii = totpixB-1; ii >= 0; ii--)
{
paintsave1 = pixBmem[ii];
zfree(paintsave1);
}
if (pixBmem) zfree(pixBmem);
pixBmem = 0;
pixBseq = 0;
totpixB = 0;
totmem = 0;
return;
}
/********************************************************************************/
// Select a color from a color chooser image file.
// Returns color button in callers zdialog.
// Returns selected RGB color in prgb[3] argument.
namespace RGB_chooser_names
{
// ch *RGB_chooser_file; // defined in fotocx.h
ch color[20];
zdialog *pzdialog;
ch *pcolorbutt;
uint8 *pRGB;
PIXBUF *pixbuf = 0;
GtkWidget *frame, *drawarea;
}
int RGB_chooser(zdialog *pzd, ch *pbutt, uint8 prgb[3])
{
using namespace RGB_chooser_names;
int RGB_chooser_dialog_event(zdialog *zd, ch *event); // dialog events function
int RGB_chooser_draw(GtkWidget *window, cairo_t *cr); // window draw function
int RGB_chooser_mouse(GtkWidget *window, GdkEventButton *); // window mouse button event
pzdialog = pzd; // copy args from caller
pcolorbutt = pbutt;
pRGB = prgb;
if (! RGB_chooser_file || ! regfile(RGB_chooser_file)) { // default color chooser file
RGB_chooser_file = (ch *) zmalloc(200,"color chooser");
snprintf(RGB_chooser_file,200,"%s/CIE1931.jpg",get_zdatadir()); // 24.20
}
/***
____________________________________________
| Color Chooser |
| |
| click on desired color |
| ________________________________________ |
| | | |
| | | |
| | | |
| | image | |
| | | |
| | | |
| | | |
| | | |
| |________________________________________| |
| |
| [_______________________________] [Browse] |
| |
| [X] |
|____________________________________________|
***/
zdialog *zd = zdialog_new("Color Chooser",Mwin," X ",null);
zdialog_add_widget(zd,"label","labclick","dialog","click on desired color");
zdialog_add_widget(zd,"frame","frame","dialog");
frame = zdialog_gtkwidget(zd,"frame");
drawarea = gtk_drawing_area_new();
gtk_widget_set_size_request(drawarea,-1,200);
gtk_container_add(GTK_CONTAINER(frame),drawarea);
zdialog_add_widget(zd,"hbox","hbfile","dialog",0,"space=3");
zdialog_add_widget(zd,"zentry","file","hbfile",0,"space=3|expand");
zdialog_add_widget(zd,"button","browse","hbfile","Browse","space=3");
zdialog_stuff(zd,"file",RGB_chooser_file);
G_SIGNAL(drawarea,"draw",RGB_chooser_draw,0);
G_SIGNAL(drawarea,"button-press-event",RGB_chooser_mouse,0);
gtk_widget_add_events(drawarea,GDK_BUTTON_PRESS_MASK);
zdialog_resize(zd,300,0);
zdialog_run(zd,RGB_chooser_dialog_event,"save");
return 0;
}
// dialog event and completion function
int RGB_chooser_dialog_event(zdialog *zd, ch *event)
{
using namespace RGB_chooser_names;
ch *pp;
if (strmatch(event,"browse"))
{
pp = navi::galleryname;
if (! pp) pp = topfolders[0];
pp = select_files1(pp);
if (! pp) return 1;
zdialog_stuff(zd,"file",pp);
if (RGB_chooser_file) zfree(RGB_chooser_file);
RGB_chooser_file = pp;
gtk_widget_queue_draw(drawarea);
}
if (zd->zstat) zdialog_free(zd);
return 1;
}
// color chooser window draw function
int RGB_chooser_draw(GtkWidget *widget, cairo_t *cr)
{
using namespace RGB_chooser_names;
PIXBUF *pixbuf1;
GError *gerror = 0;
GdkWindow *gdkwin;
int ww1, hh1, ww2, hh2;
if (*RGB_chooser_file != '/') return 1; // load last color chooser file
pixbuf1 = gdk_pixbuf_new_from_file_at_size(RGB_chooser_file,500,500,&gerror);
if (! pixbuf1) {
Plog(0,"pixbuf error: %s \n",gerror->message); // popup message >> draw event loop
return 1;
}
ww1 = gdk_pixbuf_get_width(pixbuf1); // image dimensions
hh1 = gdk_pixbuf_get_height(pixbuf1);
gdkwin = gtk_widget_get_window(widget); // set drawing area to match
ww2 = gdk_window_get_width(gdkwin); // aspect ratio
hh2 = ww2 * hh1 / ww1;
gtk_widget_set_size_request(widget,-1,hh2);
if (pixbuf) g_object_unref(pixbuf);
pixbuf = gdk_pixbuf_scale_simple(pixbuf1,ww2,hh2,BILINEAR);
g_object_unref(pixbuf1);
if (! pixbuf) return 1;
gdk_cairo_set_source_pixbuf(cr,pixbuf,0,0); // draw image
cairo_paint(cr);
return 1;
}
// color chooser mouse click function
int RGB_chooser_mouse(GtkWidget *widget, GdkEventButton *event)
{
using namespace RGB_chooser_names;
int mx, my, rs, nc;
uint8 *pixels, *pix1;
if (! pixbuf) return 1;
rs = gdk_pixbuf_get_rowstride(pixbuf);
nc = gdk_pixbuf_get_n_channels(pixbuf);
pixels = gdk_pixbuf_get_pixels(pixbuf);
mx = event->x;
my = event->y;
pix1 = pixels + my * rs + mx * nc;
snprintf(color,20,"%d|%d|%d",pix1[0],pix1[1],pix1[2]); // update caller's color button
zdialog_stuff(pzdialog,pcolorbutt,color);
pRGB[0] = pix1[0]; // update caller's paint color
pRGB[1] = pix1[1];
pRGB[2] = pix1[2];
return 1;
}
/********************************************************************************/
// HSL color chooser function
// Returns color button in callers zdialog.
// Returns selected RGB color in prgb[3] argument.
namespace HSL_chooser_names
{
zdialog *HSLzdialog;
GtkWidget *RGBframe, *RGBcolor;
GtkWidget *Hframe, *Hscale;
zdialog *pzdialog;
ch *pcolorbutt;
uint8 *pRGB;
float H, S, L; // chosen HSL color
float R, G, B; // corresp. RGB color
}
int HSL_chooser(zdialog *pzd, ch *pbutt, uint8 prgb[3])
{
using namespace HSL_chooser_names;
void HSL_chooser_RGB(GtkWidget *drawarea, cairo_t *cr, int *);
void HSL_chooser_Hscale(GtkWidget *drawarea, cairo_t *cr, int *);
int HSL_chooser_dialog_event(zdialog *zd, ch *event);
pzdialog = pzd; // copy args from caller
pcolorbutt = pbutt;
pRGB = prgb;
/***
________________________________________________
| |
| [#######] [##############################] |
| Color Hue ================[]============== |
| Saturation =====================[]========= |
| Lightness ===========[]=================== |
| |
| [X] |
|________________________________________________|
***/
zdialog *zd = zdialog_new("Adjust HSL",Mwin," X ",null);
HSLzdialog = zd;
zdialog_add_widget(zd,"hbox","hb2","dialog");
zdialog_add_widget(zd,"vbox","vb1","hb2",0,"homog|space=0");
zdialog_add_widget(zd,"vbox","vb2","hb2",0,"homog|expand|space=0");
zdialog_add_widget(zd,"frame","RGBframe","vb1",0,"space=1"); // drawing area for RGB color
RGBframe = zdialog_gtkwidget(zd,"RGBframe");
RGBcolor = gtk_drawing_area_new();
gtk_container_add(GTK_CONTAINER(RGBframe),RGBcolor);
gtk_widget_set_size_request(RGBcolor,0,16);
G_SIGNAL(RGBcolor,"draw",HSL_chooser_RGB,0);
zdialog_add_widget(zd,"frame","Hframe","vb2",0,"space=1"); // drawing area for hue scale
Hframe = zdialog_gtkwidget(zd,"Hframe");
Hscale = gtk_drawing_area_new();
gtk_container_add(GTK_CONTAINER(Hframe),Hscale);
gtk_widget_set_size_request(Hscale,200,16);
G_SIGNAL(Hscale,"draw",HSL_chooser_Hscale,0);
zdialog_add_widget(zd,"label","labhue","vb1","Color Hue");
zdialog_add_widget(zd,"label","labsat","vb1","Saturation");
zdialog_add_widget(zd,"label","lablgt","vb1","Lightness");
zdialog_add_widget(zd,"hscale","H","vb2","0|359.9|0.1|180","expand");
zdialog_add_widget(zd,"hscale","S","vb2","0|1|0.001|0.5","expand");
zdialog_add_widget(zd,"hscale","L","vb2","0|1|0.001|0.5","expand");
H = 180; // chosen HSL color = not set
S = 0.5;
L = 0.5;
zdialog_run(zd,HSL_chooser_dialog_event,"save"); // run dialog - parallel
return 1;
}
// Paint RGBcolor drawing area with RGB color from chosen HSL color
void HSL_chooser_RGB(GtkWidget *drawarea, cairo_t *cr, int *)
{
using namespace HSL_chooser_names;
int ww, hh;
int r, g, b;
ch color[20];
ww = gtk_widget_get_allocated_width(drawarea); // drawing area size
hh = gtk_widget_get_allocated_height(drawarea);
HSLtoRGB(H,S,L,R,G,B); // RGB color from chosen HSL
cairo_set_source_rgb(cr,R,G,B);
cairo_rectangle(cr,0,0,ww-1,hh-1);
cairo_fill(cr);
r = 255 * R;
g = 255 * G;
b = 255 * B;
snprintf(color,20,"%d|%d|%d",r,g,b); // update caller's color button
zdialog_stuff(pzdialog,pcolorbutt,color);
pRGB[0] = r; // update caller's paint color
pRGB[1] = g;
pRGB[2] = b;
return;
}
// Paint Hscale drawing area with all hue values in a horizontal scale
void HSL_chooser_Hscale(GtkWidget *drawarea, cairo_t *cr, int *)
{
using namespace HSL_chooser_names;
int px, ww, hh;
float H, S, L, R, G, B;
ww = gtk_widget_get_allocated_width(drawarea); // drawing area size
hh = gtk_widget_get_allocated_height(drawarea);
S = L = 0.5;
for (px = 0; px < ww; px++) // paint hue color scale
{
H = 360 * px / ww;
HSLtoRGB(H,S,L,R,G,B);
cairo_set_source_rgb(cr,R,G,B);
cairo_move_to(cr,px,0);
cairo_line_to(cr,px,hh-1);
cairo_stroke(cr);
}
return;
}
// HSL dialog event and completion function
int HSL_chooser_dialog_event(zdialog *zd, ch *event) // HSL dialog event function
{
using namespace HSL_chooser_names;
if (zd->zstat) { // zdialog complete
zdialog_free(zd);
freeMouse();
HSLzdialog = 0;
return 1;
}
if (zstrstr("H S L",event)) { // HSL inputs changed
zdialog_fetch(zd,"H",H);
zdialog_fetch(zd,"S",S);
zdialog_fetch(zd,"L",L);
gtk_widget_queue_draw(RGBcolor); // draw corresp. RGB color
}
return 1;
}
/********************************************************************************/
// Paint transparency function - paint pixel alpha channel with the mouse.
namespace paint_transp_names
{
int mode = 1; // 1/2 = more/less transparency
int Fgrad = 1; // gradual or instant transparency
int Mrad; // mouse radius
float kernel[400][400]; // radius <= 199
editfunc EFpaintransp;
}
// menu function
void m_paint_transp(GtkWidget *, ch *menu)
{
using namespace paint_transp_names;
int paint_transp_dialog_event(zdialog* zd, ch *event);
void paint_transp_mousefunc();
ch *mess1 = "left drag: add transparency \n"
"right drag: add opacity";
F1_help_topic = "paint transp";
Plog(1,"m_paint_transp \n");
EFpaintransp.menufunc = m_paint_transp;
EFpaintransp.menuname = "Paint Transp";
EFpaintransp.Farea = 2; // select area usable
EFpaintransp.mousefunc = paint_transp_mousefunc; // mouse function
if (! edit_setup(EFpaintransp)) return; // setup edit
PXM_addalpha(E1pxm); // add an alpha channel if req.
PXM_addalpha(E3pxm);
Fpaintnow();
/***
________________________________
| Paint transparency |
| |
| left drag: add transparency |
| right drag: add opacity |
| |
| paint radius [____] |
| power center [____] |
| power edge [____] |
| [x] gradual paint |
| |
| [OK] [X] |
|________________________________|
***/
zdialog *zd = zdialog_new("Paint Transparency",Mwin,"OK"," X ",null);
EFpaintransp.zd = zd;
zdialog_add_widget(zd,"hbox","hbr","dialog",0,"space=3");
zdialog_add_widget(zd,"label","labm","dialog",mess1,"space=5");
zdialog_add_widget(zd,"hbox","hbbri","dialog",0,"space=5");
zdialog_add_widget(zd,"vbox","vbbr1","hbbri",0,"homog|space=5");
zdialog_add_widget(zd,"vbox","vbbr2","hbbri",0,"homog|space=5");
zdialog_add_widget(zd,"label","labbr","vbbr1","paint radius");
zdialog_add_widget(zd,"label","labsc","vbbr1","power center");
zdialog_add_widget(zd,"label","labse","vbbr1","power edge");
zdialog_add_widget(zd,"zspin","radius","vbbr2","1|199|1|30");
zdialog_add_widget(zd,"zspin","stcent","vbbr2","0|100|1|95");
zdialog_add_widget(zd,"zspin","stedge","vbbr2","0|100|1|100");
zdialog_add_widget(zd,"hbox","hb4","dialog",0,"space=3");
zdialog_add_widget(zd,"check","Fgrad","hb4","gradual paint","space=5");
zdialog_stuff(zd,"Fgrad",1);
zdialog_load_inputs(zd); // preload prior user inputs
zdialog_run(zd,paint_transp_dialog_event,"save"); // run dialog, parallel
zdialog_send_event(zd,"radius"); // get kernel initialized
zdialog_fetch(zd,"Fgrad",Fgrad); // instant/gradual paint
mode = 1; // start with paint mode
takeMouse(paint_transp_mousefunc,drawcursor); // connect mouse function
return;
}
// dialog event and completion callback function
int paint_transp_dialog_event(zdialog *zd, ch *event)
{
using namespace paint_transp_names;
void paint_transp_mousefunc();
int radius, dx, dy;
float rad, kern, stcent, stedge;
if (strmatch(event,"done")) zd->zstat = 1; // from edit_setup() or f_save()
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 (strmatch(event,"focus")) // toggle mouse capture
takeMouse(paint_transp_mousefunc,drawcursor);
if (zstrstr("radius stcent stedge",event)) // get new brush attributes
{
zdialog_fetch(zd,"radius",Mrad); // mouse radius
zdialog_fetch(zd,"stcent",stcent); // center transparency
zdialog_fetch(zd,"stedge",stedge); // edge transparency
stcent = 0.01 * stcent; // scale 0 ... 1
stedge = 0.01 * stedge;
radius = Mrad;
for (dy = -radius; dy <= radius; dy++) // build kernel
for (dx = -radius; dx <= radius; dx++)
{
rad = sqrt(dx*dx + dy*dy);
kern = (radius - rad) / radius; // center ... edge >> 1 ... 0
kern = kern * (stcent - stedge) + stedge; // power center ... edge
if (kern < 0) kern = 0;
if (kern > 1) kern = 1;
if (rad > radius) kern = 2; // beyond radius, within square
kernel[dx+radius][dy+radius] = kern;
}
}
if (strmatch(event,"Fgrad")) // flag, gradual overpaints
zdialog_fetch(zd,"Fgrad",Fgrad);
return 1;
}
// pixel paint mouse function
void paint_transp_mousefunc()
{
using namespace paint_transp_names;
void paint_transp_dopixels(int px, int py);
static int pmxdown = 0, pmydown = 0;
int px, py;
if (LMclick || RMclick)
{
if (LMclick) mode = 1; // left click, paint
if (RMclick) mode = 2; // right click, erase
px = Mxclick;
py = Myclick;
paint_transp_dopixels(px,py); // do 1 block of pixels
}
else if (Mxdrag || Mydrag) // drag in progress
{
if (Mbutton == 1) mode = 1; // left drag, paint
if (Mbutton == 3) mode = 2; // right drag, erase
px = Mxdrag;
py = Mydrag;
if (Mxdown != pmxdown || Mydown != pmydown) { // new drag
pmxdown = Mxdown;
pmydown = Mydown;
}
paint_transp_dopixels(px,py); // do 1 block of pixels
LMclick = RMclick = Mxdrag = Mydrag = 0;
return;
}
LMclick = RMclick = Mxdrag = Mydrag = 0;
draw_mousecircle(Mxposn,Myposn,Mrad,0,0); // draw mouse circle
return;
}
// paint or erase 1 block of pixels within mouse radius of px, py
void paint_transp_dopixels(int px, int py)
{
using namespace paint_transp_names;
float *pix3;
int radius, dx, dy, qx, qy;
int ii, rr, dist = 0;
float kern;
cairo_t *cr = draw_context_create(gdkwin,draw_context);
radius = Mrad;
for (dy = -radius; dy <= radius; dy++) // loop surrounding block of pixels
for (dx = -radius; dx <= radius; dx++)
{
qx = px + dx;
qy = py + dy;
if (qx < 0 || qx > Eww-1) continue;
if (qy < 0 || qy > Ehh-1) continue;
if (sa_stat == sa_stat_fini) { // select area active
ii = qy * Eww + qx;
dist = sa_pixmap[ii];
if (! dist) continue; // pixel is outside area
}
kern = kernel[dx+radius][dy+radius]; // mouse transparencies
if (kern > 1) continue; // outside mouse radius
pix3 = PXMpix(E3pxm,qx,qy); // edited image pixel
if (mode == 1) { // add transparency
if (Fgrad) {
pix3[3] -= 2 * kern; // accumulate transparency
if (pix3[3] < 0) pix3[3] = 0;
}
else pix3[3] = 0; // instant
}
if (mode == 2) { // add opacity
if (Fgrad) {
pix3[3] += 2 * kern; // accumulate
if (pix3[3] > 255.0) pix3[3] = 255.0;
}
else pix3[3] = 255.0; // instant
}
}
CEF->Fmods++;
CEF->Fsaved = 0;
px = px - radius - 1; // repaint modified area
py = py - radius - 1;
rr = 2 * radius + 3;
Fpaint3(px,py,rr,rr,cr);
draw_mousecircle(Mxposn,Myposn,Mrad,0,cr); // draw mouse circle
draw_context_destroy(draw_context);
return;
}
/********************************************************************************/
// fill selected areas or transparent areas with selected color
namespace areafill_names
{
editfunc EFareafill;
int Fareas, Ftransp; // flags, fill select areas, fill transp. areas
float mblend; // 0 - 1.0 input/output blend
uint8 RGB[3] = { 0, 0, 0 }; // fill color
}
// menu function
void m_area_fill(GtkWidget *, ch *menu) // 25.50
{
using namespace areafill_names;
int areafill_dialog_event(zdialog *zd, ch *event);
void * areafill_thread(void *);
ch color[20];
F1_help_topic = "area fill";
Plog(1,"m_area_fill \n");
EFareafill.menuname = "Area Fill";
EFareafill.menufunc = m_area_fill;
EFareafill.threadfunc = areafill_thread;
EFareafill.Farea = 2; // select area usable
EFareafill.Fscript = 1; // scripting supported
if (! edit_setup(EFareafill)) return; // setup edit
PXM_addalpha(E1pxm);
PXM_addalpha(E3pxm);
/***
________________________________
| Area Fill |
| |
| color [####] [palette] [HSL] |
| |
| [_] fill selected areas |
| [_] fill transparent areas |
| |
| Blend 0% ======[]======= 100% |
| |
| [OK] [X] |
|________________________________|
***/
zdialog *zd = zdialog_new("Area Fill",Mwin,"OK"," X ",null);
EFareafill.zd = zd;
zdialog_add_widget(zd,"hbox","hbp","dialog",0,"space=3");
zdialog_add_widget(zd,"label","labp","hbp","color","space=5");
zdialog_add_widget(zd,"colorbutt","colorbutt","hbp","0|0|0");
zdialog_add_widget(zd,"button","palette","hbp","palette","space=10");
zdialog_add_widget(zd,"button","HSL","hbp","HSL");
zdialog_add_widget(zd,"check","Fareas","dialog","fill selected areas");
zdialog_add_widget(zd,"check","Ftransp","dialog","fill transparent areas");
zdialog_add_widget(zd,"hbox","hbmblend","dialog",0,"space=3");
zdialog_add_widget(zd,"label","lab0","hbmblend","Blend 0%","space=5");
zdialog_add_widget(zd,"hscale","mblend","hbmblend","0.0|1.0|0.01|1.0","expand");
zdialog_add_widget(zd,"label","lab100","hbmblend","100%","space=5");
snprintf(color,20,"%d|%d|%d",RGB[0],RGB[1],RGB[2]); // initz. current color
zdialog_stuff(zd,"colorbutt",color);
zdialog_resize(zd,200,0);
zdialog_run(zd,areafill_dialog_event,"save"); // run dialog - parallel
return;
}
// dialog event and completion callback function
int areafill_dialog_event(zdialog *zd, ch *event)
{
using namespace areafill_names;
int err;
ch *pp, color[20];
if (strmatch(event,"done")) zd->zstat = 1; // from edit_setup() or f_save()
if (strmatch(event,"cancel")) zd->zstat = 2; // from f_open()
if (zd->zstat)
{
if (zd->zstat == 1) // [OK]
{
edit_addhist("areas:%d transp:%d mblend:%.2f", // edit params > edit hist
Fareas,Ftransp,mblend);
edit_done(0); // commit edit
}
else edit_cancel(0); // discard edit
return 1;
}
if (strmatch(event,"focus")) return 1;
if (strmatch(event,"colorbutt"))
{
zdialog_fetch(zd,"colorbutt",color,19); // get paint color from color button
pp = substring(color,"|",1);
if (pp) RGB[0] = atoi(pp);
pp = substring(color,"|",2);
if (pp) RGB[1] = atoi(pp);
pp = substring(color,"|",3);
if (pp) RGB[2] = atoi(pp);
}
if (strmatch(event,"palette"))
err = RGB_chooser(zd,"colorbutt",RGB); // select color from palette
if (strmatch(event,"HSL")) {
err = HSL_chooser(zd,"colorbutt",RGB); // select color from palette
if (err) return 1;
snprintf(color,20,"%d|%d|%d",RGB[0],RGB[1],RGB[2]);
zdialog_stuff(zd,"colorbutt",color);
}
if (strstr("Fareas Ftransp mblend",event))
{
zdialog_fetch(zd,"Fareas",Fareas);
zdialog_fetch(zd,"Ftransp",Ftransp);
zdialog_fetch(zd,"mblend",mblend);
thread_signal();
}
return 1;
}
// thread function
void * areafill_thread(void *)
{
using namespace areafill_names;
void * areafill_wthread(void *arg);
if (! Fareas && ! Ftransp) return 0; // nothing to do
do_wthreads(areafill_wthread,NSMP); // worker threads
CEF->Fmods++; // image modified
CEF->Fsaved = 0; // not saved
Fpaint2();
return 0;
}
// worker thread functions
void * areafill_wthread(void *arg)
{
using namespace areafill_names;
int index = *((int *) (arg));
int px, py;
int dist, Fdopixel;
float R1, G1, B1, R3, G3, B3;
float *pix1, *pix3;
float ff1, ff2;
for (py = index; py < Ehh; py += NSMP)
for (px = 0; px < Eww; px++)
{
pix1 = PXMpix(E1pxm,px,py); // input pixel
pix3 = PXMpix(E3pxm,px,py); // output pixel
Fdopixel = 0;
if (Fareas && sa_stat == sa_stat_fini) // selected areas fill wanted
{
int ii = py * Eww + px;
dist = sa_pixmap[ii]; // distance from edge
if (dist) Fdopixel = 1; // pixel inside area
}
if (Ftransp && pix1[3] < 250) Fdopixel = 1; // transparent areas fill wanted
if (! Fdopixel) continue;
R1 = pix1[0]; // input pixel color
G1 = pix1[1];
B1 = pix1[2];
R3 = RGB[0]; // fill color
G3 = RGB[1];
B3 = RGB[2];
if (pix1[3] < 250) // pixel has transparency
{
ff1 = (255 - pix1[3]) / 255.0; // fill color part
ff2 = 1.0 - ff1; // input pixel part
R3 = ff1 * R3 + ff2 * R1; // blend input and fill color
G3 = ff1 * G3 + ff2 * G1;
B3 = ff1 * B3 + ff2 * B1;
}
if (mblend < 1.0) // image blend slider
{
ff1 = mblend;
ff2 = 1.0 - ff1;
R3 = ff1 * R3 + ff2 * R1; // blend input and fill color
G3 = ff1 * G3 + ff2 * G1;
B3 = ff1 * B3 + ff2 * B1;
}
pix3[0] = R3;
pix3[1] = G3;
pix3[2] = B3;
pix3[3] = 255;
}
return 0;
}
/********************************************************************************/
// Copy in Image function.
// Copy from one image area to another with variable opacity.
namespace copy_in_image
{
int dialog_event(zdialog* zd, ch *event);
void mousefunc();
void dopixels(int px, int py); // update pixel block
void savepixB(int px, int py); // save pixel block for poss. undo
void undolastB(); // undo last pixel block, free memory
void freefirstB(); // free memory for first pixel block
void freeallB(); // free memory for all pixel blocks
int mode; // 1/2 = paint / erase
int Mrad; // mouse radius
int xorg1, yorg1; // source pixel origin
int xorg2, yorg2; // dest pixel origin
float kernel[402][402]; // radius <= 200
int Fptran = 0; // flag, paint over transparent areas
int nc, ac; // no. channels, alpha channel
int64 maxmem = (int64) 4000 * MEGA; // max. pixel block memory
int64 totmem; // pixB memory allocated
int maxpixB = 10000; // max. pixel blocks
int totpixB = 0; // total pixel blocks
int pixBseq = 0; // last pixel block sequence no.
typedef struct { // pixel block before edit
int seq; // block sequence no.
uint16 px, py; // center pixel (radius org.)
uint16 radius; // radius of pixel block
float pixel[][4]; // array of pixel[npix][4]
} pixBmem_t;
pixBmem_t **pixBmem = 0; // *pixBmem_t[]
int pixBmem_cc = 12; // all except pixel array + pad
int pcc4 = 4 * sizeof(float); // pixel cc: RGBA = 4 channels
editfunc EFcopyinimage;
}
// menu function
void m_copy_in_image(GtkWidget *, ch *menu) // separate paint and copy
{
using namespace copy_in_image;
ch *mess1 = "left click: set image source location \n"
"right click: set corresponding destination";
ch *mess2 = "left drag: copy image to mouse position \n"
"right drag: restore original image";
F1_help_topic = "copy in image";
Plog(1,"m_copy_in_image \n");
EFcopyinimage.menufunc = m_copy_in_image;
EFcopyinimage.menuname = "Copy in Image";
EFcopyinimage.Farea = 2; // select area usable
EFcopyinimage.mousefunc = mousefunc; // mouse function
if (! edit_setup(EFcopyinimage)) return; // setup edit
/***
_______________________________________________
| Copy Within Image |
| |
| left click: set image source location |
| right click: set corresponding destination |
| |
| left drag: copy image to mouse position |
| right drag: restore original image |
| |
| brush size [____] [undo last] |
| opacity center [____] [undo all] |
| opacity edge [____] |
| |
| [x] paint transparent areas |
| |
| [OK] [X] |
|_______________________________________________|
***/
zdialog *zd = zdialog_new("Copy Within Image",Mwin,"OK"," X ",null);
EFcopyinimage.zd = zd;
zdialog_add_widget(zd,"hbox","hbm1","dialog",0,"space=3");
zdialog_add_widget(zd,"label","labm","hbm1",mess1,"space=3");
zdialog_add_widget(zd,"hbox","hbm2","dialog",0,"space=3");
zdialog_add_widget(zd,"label","labm","hbm2",mess2,"space=3");
zdialog_add_widget(zd,"hsep","sep1","dialog",0,"space=5");
zdialog_add_widget(zd,"hbox","hbbri","dialog",0,"space=5");
zdialog_add_widget(zd,"vbox","vbbr1","hbbri",0,"homog|space=5");
zdialog_add_widget(zd,"vbox","vbbr2","hbbri",0,"homog|space=5");
zdialog_add_widget(zd,"vbox","space","hbbri",0,"space=10");
zdialog_add_widget(zd,"vbox","vbbr3","hbbri",0,"space=10");
zdialog_add_widget(zd,"label","labbr","vbbr1","brush size");
zdialog_add_widget(zd,"label","labtc","vbbr1","opacity center");
zdialog_add_widget(zd,"label","labte","vbbr1","opacity edge");
zdialog_add_widget(zd,"zspin","Mrad","vbbr2","1|200|1|30");
zdialog_add_widget(zd,"zspin","opccent","vbbr2","1|100|1|30");
zdialog_add_widget(zd,"zspin","opcedge","vbbr2","0|100|1|0");
zdialog_add_widget(zd,"button","undlast","vbbr3","Undo Last");
zdialog_add_widget(zd,"button","undall","vbbr3","Undo All");
zdialog_add_widget(zd,"hbox","hb4","dialog",0,"space=3");
zdialog_add_widget(zd,"check","Fptran","hb4","paint over transparent areas","space=5");
zdialog_load_inputs(zd); // preload prior user inputs
zdialog_run(zd,dialog_event,"save"); // run dialog, parallel
zdialog_fetch(zd,"Fptran",Fptran); // paint over transparent areas
zdialog_send_event(zd,"Mrad"); // get kernel initialized
totmem = 0; // memory used
pixBmem = 0; // pixel block memory
totpixB = 0; // pixel blocks
pixBseq = 0;
xorg1 = yorg1 = -1; // no source pixel origin
xorg2 = yorg2 = -1; // no dest. pixel origin
ac = 0;
nc = E1pxm->nc; // channels, RGBA
if (nc > 3) ac = 1; // alpha channel present
takeMouse(mousefunc,drawcursor); // connect mouse function
return;
}
// dialog event and completion callback function
int copy_in_image::dialog_event(zdialog *zd, ch *event)
{
using namespace copy_in_image;
int radius, dx, dy;
float rad, kern, opccent, opcedge;
if (strmatch(event,"done")) zd->zstat = 1; // from edit_setup() or f_save()
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
freeallB(); // free pixel block memory
return 1;
}
draw_mousecircle(0,0,0,1,0); // erase mouse circle
draw_mousecircle2(0,0,0,1,0); // erase source tracking circle
if (strmatch(event,"focus")) // toggle mouse capture
takeMouse(mousefunc,drawcursor);
if (zstrstr("Mrad opccent opcedge",event)) // get new brush attributes
{
zdialog_fetch(zd,"Mrad",Mrad);
zdialog_fetch(zd,"opccent",opccent);
zdialog_fetch(zd,"opcedge",opcedge);
opccent = 0.01 * opccent; // scale 0 ... 1
opcedge = 0.01 * opcedge;
opccent = pow(opccent,2); // change response curve
opcedge = opccent * opcedge; // edge relative to center
radius = Mrad;
for (dy = -radius; dy <= radius; dy++) // build kernel
for (dx = -radius; dx <= radius; dx++)
{
rad = sqrt(dx*dx + dy*dy);
kern = (radius - rad) / radius; // center ... edge >> 1 ... 0
kern = kern * (opccent - opcedge) + opcedge; // opacity center ... edge
if (rad > radius) kern = 0; // beyond radius, within square
if (kern < 0) kern = 0;
if (kern > 1) kern = 1;
kernel[dx+radius][dy+radius] = kern;
}
}
if (strmatch(event,"undlast")) // undo last edit (click or drag)
undolastB();
if (strmatch(event,"undall")) { // undo all edits
edit_reset();
freeallB();
}
if (strmatch(event,"Fptran")) // flag, paint over transparency
zdialog_fetch(zd,"Fptran",Fptran);
return 1;
}
// pixel paint mouse function
void copy_in_image::mousefunc() // improve source/destination
{ // mouse tracking
using namespace copy_in_image;
static int pmxdown = 0, pmydown = 0;
int px, py;
if (LMclick) {
xorg1 = Mxclick; // new source pixel origin
yorg1 = Myclick;
LMclick = 0;
}
if (RMclick) { // new corresp. destination pixel
xorg2 = Mxclick;
yorg2 = Myclick;
RMclick = 0;
}
if ((Mxdrag || Mydrag) && xorg1 > 0 && xorg2 > 0) // drag in progress
{
if (Mbutton == 1) mode = 1; // left drag, paint
if (Mbutton == 3) mode = 2; // right drag, erase
if (Mxdown != pmxdown || Mydown != pmydown) // new drag
{
pmxdown = Mxdown;
pmydown = Mydown;
pixBseq++; // new undo seq. no.
}
px = Mxdrag;
py = Mydrag;
dopixels(px,py); // do 1 block of pixels
}
else mode = 0;
draw_mousecircle(Mxposn,Myposn,Mrad,0,0); // draw mouse circle
if (xorg1 > 0 && xorg2 > 0 && mode != 2) { // 2nd circle tracks source pixels
px = xorg1 + Mxposn - xorg2;
py = yorg1 + Myposn - yorg2;
if (px > 0 && px < Eww-1 && py > 0 && py < Ehh-1)
draw_mousecircle2(px,py,Mrad,0,0);
}
else draw_mousecircle2(0,0,0,1,0); // erase 2nd circle
LMclick = RMclick = Mxdrag = Mydrag = 0;
return;
}
// paint or erase 1 block of pixels within mouse radius of px, py
void copy_in_image::dopixels(int px, int py)
{
using namespace copy_in_image;
float *pix1, *pix3;
int radius, dx, dy, qx, qy, sx, sy;
int ii, ww, hh, dist = 0;
int pot = ac * Fptran; // paint over transparent areas
float kern;
if (xorg1 < 0 || yorg1 < 0) return; // no source pixel origin
if (xorg2 < 0 || yorg2 < 0) return; // no dest. pixel origin
cairo_t *cr = draw_context_create(gdkwin,draw_context);
draw_mousecircle(0,0,0,1,cr); // erase mouse circle
draw_mousecircle2(0,0,0,1,cr); // erase source tracking circle
ww = Eww;
hh = Ehh;
savepixB(px,py); // save pixels for poss. undo
radius = Mrad;
if (mode == 1) {
CEF->Fmods++;
CEF->Fsaved = 0;
}
for (dy = -radius; dy <= radius; dy++) // loop surrounding block of pixels
for (dx = -radius; dx <= radius; dx++)
{
qx = px + dx;
qy = py + dy;
if (qx < 0 || qx > ww-1) continue;
if (qy < 0 || qy > hh-1) continue;
if (sa_stat == sa_stat_fini) { // select area active
ii = qy * ww + qx;
dist = sa_pixmap[ii];
if (! dist) continue; // pixel is outside area
}
kern = kernel[dx+radius][dy+radius]; // mouse opacities
if (kern == 0) continue; // outside mouse radius
pix1 = PXMpix(E1pxm,qx,qy); // source image pixel
pix3 = PXMpix(E3pxm,qx,qy); // edited image pixel
if (mode == 1) // paint
{
sx = xorg1 + qx - xorg2; // source image pixel
sy = yorg1 + qy - yorg2;
if (sx < 0) sx = 0;
if (sx > ww-1) sx = ww-1;
if (sy < 0) sy = 0;
if (sy > hh-1) sy = hh-1;
pix1 = PXMpix(E1pxm,sx,sy); // source image pixel at location
kern = 0.3 * kern;
pix3[0] = kern * pix1[0] + (1.0 - kern) * pix3[0]; // overpaints accumulate
pix3[1] = kern * pix1[1] + (1.0 - kern) * pix3[1];
pix3[2] = kern * pix1[2] + (1.0 - kern) * pix3[2];
if (pot) pix3[3] = kern * pix1[3] + (1.0 - kern) * pix3[3]; // overpaint transparent area
}
if (mode == 2) // erase
{
pix3[0] = kern * pix1[0] + (1.0 - kern) * pix3[0]; // gradual erase
pix3[1] = kern * pix1[1] + (1.0 - kern) * pix3[1];
pix3[2] = kern * pix1[2] + (1.0 - kern) * pix3[2];
if (pot) pix3[3] = kern * pix1[3] + (1.0 - kern) * pix3[3];
}
}
px = px - radius - 1; // repaint modified area
py = py - radius - 1;
ww = 2 * radius + 3;
Fpaint3(px,py,ww,ww,cr);
draw_context_destroy(draw_context);
return;
}
// save 1 block of pixels for possible undo
void copy_in_image::savepixB(int px, int py)
{
using namespace copy_in_image;
int cc, npix, radius, dx, dy;
float *pix3;
pixBmem_t *save1B;
if (! pixBmem) { // first time
cc = maxpixB * sizeof(void *);
pixBmem = (pixBmem_t **) zmalloc(cc,"copy in image");
totpixB = 0;
totmem = 0;
}
if (totmem > maxmem || totpixB == maxpixB) // free memory for oldest updates
while (totmem > 0.7 * maxmem || totpixB > 0.7 * maxpixB)
freefirstB();
radius = Mrad;
npix = 0;
for (dy = -radius; dy <= radius; dy++) // count pixels in block
for (dx = -radius; dx <= radius; dx++)
{
if (px + dx < 0 || px + dx > Eww-1) continue;
if (py + dy < 0 || py + dy > Ehh-1) continue;
npix++;
}
cc = npix * pcc4 + pixBmem_cc;
save1B = (pixBmem_t *) zmalloc(cc,"copy in image"); // allocate memory for block
pixBmem[totpixB] = save1B;
totpixB += 1;
totmem += cc;
save1B->seq = pixBseq; // save pixel block poop
save1B->px = px;
save1B->py = py;
save1B->radius = radius;
npix = 0;
for (dy = -radius; dy <= radius; dy++) // save pixels in block
for (dx = -radius; dx <= radius; dx++)
{
if (px + dx < 0 || px + dx > Eww-1) continue;
if (py + dy < 0 || py + dy > Ehh-1) continue;
pix3 = PXMpix(E3pxm,(px+dx),(py+dy)); // edited image pixel
save1B->pixel[npix][0] = pix3[0];
save1B->pixel[npix][1] = pix3[1];
save1B->pixel[npix][2] = pix3[2];
if (ac) save1B->pixel[npix][3] = pix3[3];
npix++;
}
return;
}
// undo last pixel block (newest edit) and free memory
void copy_in_image::undolastB()
{
using namespace copy_in_image;
int ii, cc, npix, radius;
int ww, px, py, dx, dy;
float *pix3;
pixBmem_t *save1B;
for (ii = totpixB-1; ii >= 0; ii--)
{
save1B = pixBmem[ii];
if (save1B->seq != pixBseq) break;
px = save1B->px;
py = save1B->py;
radius = save1B->radius;
npix = 0;
for (dy = -radius; dy <= radius; dy++)
for (dx = -radius; dx <= radius; dx++)
{
if (px + dx < 0 || px + dx > Eww-1) continue;
if (py + dy < 0 || py + dy > Ehh-1) continue;
pix3 = PXMpix(E3pxm,(px+dx),(py+dy));
pix3[0] = save1B->pixel[npix][0];
pix3[1] = save1B->pixel[npix][1];
pix3[2] = save1B->pixel[npix][2];
if (ac) pix3[3] = save1B->pixel[npix][3];
npix++;
}
px = px - radius - 1;
py = py - radius - 1;
ww = 2 * radius + 3;
Fpaint3(px,py,ww,ww,0);
zfree(save1B);
pixBmem[ii] = 0;
cc = npix * pcc4 + pixBmem_cc;
totmem -= cc;
totpixB--;
}
if (pixBseq > 0) --pixBseq;
return;
}
// free memory for first pixel block (oldest edit)
void copy_in_image::freefirstB()
{
using namespace copy_in_image;
int firstseq;
int ii, jj, cc, npix, radius;
int px, py, dx, dy;
pixBmem_t *save1B;
if (! totpixB) return;
firstseq = pixBmem[0]->seq;
for (ii = 0; ii < totpixB; ii++)
{
save1B = pixBmem[ii];
if (save1B->seq != firstseq) break;
px = save1B->px;
py = save1B->py;
radius = save1B->radius;
npix = 0;
for (dy = -radius; dy <= radius; dy++)
for (dx = -radius; dx <= radius; dx++)
{
if (px + dx < 0 || px + dx > Eww-1) continue;
if (py + dy < 0 || py + dy > Ehh-1) continue;
npix++;
}
zfree(save1B);
pixBmem[ii] = 0;
cc = npix * pcc4 + pixBmem_cc;
totmem -= cc;
}
for (jj = 0; ii < totpixB; jj++, ii++)
pixBmem[jj] = pixBmem[ii];
totpixB = jj;
return;
}
// free all pixel block memory
void copy_in_image::freeallB()
{
using namespace copy_in_image;
int ii;
pixBmem_t *save1B;
for (ii = totpixB-1; ii >= 0; ii--)
{
save1B = pixBmem[ii];
zfree(save1B);
}
if (pixBmem) zfree(pixBmem);
pixBmem = 0;
pixBseq = 0;
totpixB = 0;
totmem = 0;
return;
}
/********************************************************************************/
// Copy From Image function.
// 'Paint' a target image with pixels from a different source image.
namespace copy_from_image
{
int dialog_event(zdialog* zd, ch *event);
void mousefunc(); // generate mouse circle
void dopixels(); // copy pixel block to target
void save_pixblock(); // save pixel block for poss. undo
void undo_lastblock(); // undo last pixel block, free memory
void free_firstblock(); // free memory for first pixel block
void free_allblocks(); // free memory for all pixel blocks
editfunc EFcopyfromimage;
int mode; // 1/2 = paint / erase
int Fptran; // flag, paint over transparent areas
int mpxC, mpyC; // center of pixel copy area
int mpx, mpy; // center of moving mouse circle
int mrad; // radius of pixel copy area
typedef struct { // shared memory data
int killsource; // source image process should exit
int mpxC, mpyC; // center of pixel copy area (dest image)
int mpxS, mpyS; // center of pixel copy area (source image)
int mpx, mpy; // mouse drag position
int mrad; // mouse radius
float Fscale; // source image scale factor
int Freq; // source/target coord. flag
float pixels[402*402*4]; // pixel block, max. mrad = 200
} mmap_data_t;
mmap_data_t *mmap_data; // shared memory pointer
float kernel[402][402]; // mrad <= 200
int64 maxmem = (int64) 4000 * MEGA; // max. pixel block memory
int64 totmem; // pixblock memory allocated
int maxpixblock = 10000; // max. pixel blocks
int totpixblock = 0; // total pixel blocks
int pixblockseq = 0; // last pixel block sequence no.
typedef struct { // saved pixel block before edit
int seq; // block sequence no.
uint16 mpx, mpy; // center pixel (mrad org.)
uint16 mrad; // radius of pixel block
float pixels[][4]; // array of pixels, rows x cols
} pixblockmem_t;
pixblockmem_t **pixblockmem;
int pixblockmem_cc = 12; // all except pixel array + pad
}
// menu function
void m_copy_from_image(GtkWidget *, ch *menu) // split source/target processes
{
using namespace copy_from_image;
int err, fd;
size_t cc;
F1_help_topic = "copy from image";
Plog(1,"m_copy_from_image \n");
fd = shm_open("/fotocx_copy_from_image",O_RDWR+O_CREAT,0600); // identify memory mapped region
if (fd < 0) {
zmessageACK(Mwin,"shm_open() failure: %s",strerror(errno));
return;
}
cc = sizeof(mmap_data_t);
err = ftruncate(fd,cc);
if (err) {
zmessageACK(Mwin,"ftruncate() failure: %s",strerror(errno));
return;
}
mmap_data = (mmap_data_t *) mmap(0,cc,PROT_WRITE,MAP_SHARED,fd,0); // create memory mapped region
if (mmap_data == (void *) -1) {
zmessageACK(Mwin,"mmap() failure: %s",strerror(errno));
return;
}
memset(mmap_data,0,cc);
mpxC = mpyC = 0; // default pixel copy area selected
mrad = 50; // default mouse radius
mmap_data->Fscale = 1.0; // default source image scale factor
EFcopyfromimage.menufunc = m_copy_from_image; // start edit function for target image
EFcopyfromimage.menuname = "Copy From Image";
EFcopyfromimage.Farea = 2; // select area usable
EFcopyfromimage.mousefunc = mousefunc; // mouse function
if (! edit_setup(EFcopyfromimage)) return;
PXM_addalpha(E1pxm);
PXM_addalpha(E3pxm);
cc = maxpixblock * sizeof(void *);
pixblockmem = (pixblockmem_t **) zmalloc(cc,"copy from image"); // saved pixel blocks list
totmem = 0; // memory used
totpixblock = 0; // pixel blocks
pixblockseq = 0;
/****
__________________________________________________
| Copy From Image |
| |
| shift + left click: synchronize copy position |
| left drag: copy source image to mouse |
| right drag: restore original image |
| |
| source image scale [____] |
| |
| brush size [____] [undo last] |
| opacity center [____] [undo all] |
| opacity edge [____] |
| |
| [x] paint over transparent areas |
| |
| [OK] [X] |
|__________________________________________________|
****/
ch *mess1 = "shift + left click: synchronize copy position \n"
"left drag: copy source image to mouse \n"
"right drag: restore original image";
zdialog *zd = zdialog_new("Copy From Image",Mwin,"OK"," X ",null);
EFcopyfromimage.zd = zd;
zdialog_add_widget(zd,"hbox","hbr","dialog",0,"space=3");
zdialog_add_widget(zd,"label","labm","dialog",mess1,"space=5");
zdialog_add_widget(zd,"hbox","hbsc","dialog",0,"space=5");
zdialog_add_widget(zd,"label","labsc","hbsc","source image scale","space=3");
zdialog_add_widget(zd,"zspin","scale","hbsc","0.2|5.0|0.01|1.0","space=3");
zdialog_add_widget(zd,"hbox","hbbri","dialog",0,"space=3");
zdialog_add_widget(zd,"vbox","vbbr1","hbbri",0,"homog|space=3");
zdialog_add_widget(zd,"vbox","vbbr2","hbbri",0,"homog|space=3");
zdialog_add_widget(zd,"vbox","space","hbbri",0,"space=10");
zdialog_add_widget(zd,"vbox","vbbr3","hbbri",0,"space=10");
zdialog_add_widget(zd,"label","labbr","vbbr1","brush size");
zdialog_add_widget(zd,"label","labtc","vbbr1","opacity center");
zdialog_add_widget(zd,"label","labte","vbbr1","opacity edge");
zdialog_add_widget(zd,"zspin","mrad","vbbr2","1|200|1|50");
zdialog_add_widget(zd,"zspin","opccent","vbbr2","1|100|1|10");
zdialog_add_widget(zd,"zspin","opcedge","vbbr2","0|100|1|0");
zdialog_add_widget(zd,"button","undlast","vbbr3","Undo Last");
zdialog_add_widget(zd,"button","undall","vbbr3","Undo All");
zdialog_add_widget(zd,"hbox","hb4","dialog",0,"space=3");
zdialog_add_widget(zd,"check","Fptran","hb4","paint over transparent areas","space=5");
zdialog_load_inputs(zd); // preload prior user inputs
zdialog_run(zd,dialog_event,"save"); // run dialog, parallel
zdialog_fetch(zd,"Fptran",Fptran); // paint over transparent areas
zdialog_fetch(zd,"scale",mmap_data->Fscale);
zdialog_send_event(zd,"mrad"); // get kernel initialized
takeMouse(mousefunc,drawcursor); // connect mouse function
save_params(); // communicate current file
new_session("-x1 -m \"Copy From Image Slave\" "); // slave process for source image
return;
}
// dialog event and completion callback function
int copy_from_image::dialog_event(zdialog *zd, ch *event)
{
using namespace copy_from_image;
int dx, dy;
float rad, kern, opccent, opcedge;
if (strmatch(event,"done")) zd->zstat = 1; // from edit_setup() or f_save()
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
free_allblocks(); // free pixel block memory
zfree(pixblockmem);
mmap_data->killsource = 1; // make source image process exit
return 1;
}
if (strmatch(event,"focus")) // toggle mouse capture
takeMouse(mousefunc,drawcursor);
if (strmatch(event,"scale")) // source image scale change
zdialog_fetch(zd,"scale",mmap_data->Fscale);
if (zstrstr("mrad opccent opcedge",event)) // get new brush attributes
{
zdialog_fetch(zd,"mrad",mrad);
zdialog_fetch(zd,"opccent",opccent);
zdialog_fetch(zd,"opcedge",opcedge);
opccent = 0.01 * opccent; // scale 0 ... 1
opcedge = 0.01 * opcedge;
opccent = pow(opccent,2); // change response curve
opcedge = opccent * opcedge; // edge relative to center
for (dy = -mrad; dy <= mrad; dy++) // rebuild kernel
for (dx = -mrad; dx <= mrad; dx++)
{
rad = sqrt(dx*dx + dy*dy);
kern = (mrad - rad) / mrad; // center ... edge >> 1 ... 0
kern = kern * (opccent - opcedge) + opcedge; // opacity center ... edge
if (rad > mrad) kern = 0; // beyond mrad, within square
if (kern < 0) kern = 0;
if (kern > 1) kern = 1;
kernel[dx+mrad][dy+mrad] = kern;
}
}
if (strmatch(event,"undlast")) // undo last edit (click or drag)
undo_lastblock();
if (strmatch(event,"undall")) { // undo all edits
edit_reset();
free_allblocks();
}
if (strmatch(event,"Fptran")) // flag, paint over transparency
zdialog_fetch(zd,"Fptran",Fptran);
return 1;
}
// mouse function
void copy_from_image::mousefunc()
{
using namespace copy_from_image;
static int pmxdown = 0, pmydown = 0;
static int pmpx, pmpy;
mode = 0;
mpx = Mxposn;
mpy = Myposn;
mmap_data->mpx = mpx; // inform source image,
mmap_data->mpy = mpy; // mouse position,
mmap_data->mrad = mrad; // mouse radius
if (LMclick && KBshiftkey) // shift + left mouse click
{
Mxdown = Mxclick; // click position
Mydown = Myclick;
mmap_data->mpxC = Mxdown; // inform source image,
mmap_data->mpyC = Mydown; // new mouse drag start
pixblockseq++; // new undo seq. no.
mode = 1;
if (! mmap_data->Freq) { // request pixel block from source
mmap_data->Freq = 1;
pmpx = mpx; // save mouse position
pmpy = mpy;
}
}
else if (Mxdrag || Mydrag) // drag in progress
{
if (Mbutton == 1) mode = 1; // left drag, paint
if (Mbutton == 3) mode = 2; // right drag, erase
if (Mxdown != pmxdown || Mydown != pmydown) { // new drag
pmxdown = Mxdown;
pmydown = Mydown;
if (mode == 1) pixblockseq++; // new undo seq. no.
}
if (mode == 1 && ! mmap_data->Freq) { // request pixel block from source
mmap_data->Freq = 1;
pmpx = mpx; // save mouse position
pmpy = mpy;
}
}
if (mode == 1 && mmap_data->Freq == 2) { // new pixel block from source avail.
mpx = pmpx; // synch mouse position
mpy = pmpy;
dopixels(); // paint pixel block on target image
mmap_data->Freq = 0; // ready for next
}
if (mode == 2) dopixels(); // erase target image
draw_mousecircle(0,0,0,1,0); // erase mouse circle
draw_mousecircle(mpx,mpy,mrad,0,0); // draw mouse circle
LMclick = RMclick = Mxdrag = Mydrag = 0;
return;
}
// paint or erase 1 block of pixels within mouse radius of mpx, mpy
void copy_from_image::dopixels()
{
using namespace copy_from_image;
float *pix1, *pix2, *pix3, pixF[4];
int dx, dy, qx, qy, px, py;
int ii, ww, hh, rs, dist = 0;
int pot = Fptran; // paint over transparent areas
float kern, alpha1, alpha2;
ww = Eww;
hh = Ehh;
rs = 2 * mrad + 1; // pixel block row stride
save_pixblock(); // save pixels for poss. undo
for (dy = -mrad; dy <= mrad; dy++) // loop surrounding block of pixels
for (dx = -mrad; dx <= mrad; dx++)
{
qx = mpx + dx;
qy = mpy + dy;
if (qx < 0 || qx > ww-1) continue;
if (qy < 0 || qy > hh-1) continue;
if (sa_stat == sa_stat_fini) { // select area active
ii = qy * ww + qx;
dist = sa_pixmap[ii];
if (! dist) continue; // pixel is outside area
}
kern = kernel[dx+mrad][dy+mrad]; // mouse opacities
if (kern == 0) continue; // outside mouse radius
pix1 = PXMpix(E1pxm,qx,qy); // input target image pixel
ii = (dy + mrad) * rs + (dx + mrad); // source image pixel >> target
pix2 = mmap_data->pixels + ii * 4;
pix3 = PXMpix(E3pxm,qx,qy); // output target image pixel
if (mode == 1) // paint
{
if (pot) alpha1 = 1; // paint over transparent areas
else alpha1 = 0.00392 * pix1[3]; // input target image opacity
alpha2 = 0.00392 * pix2[3]; // source image opacity
if (alpha2 < 0.99) {
pixF[0] = pix2[0] * alpha2 + pix1[0] * (1 - alpha2); // construct output target pixel
pixF[1] = pix2[1] * alpha2 + pix1[1] * (1 - alpha2); // = source image pixel
pixF[2] = pix2[2] * alpha2 + pix1[2] * (1 - alpha2); // + target image pixel
pixF[3] = pix2[3] * alpha2 + pix1[3] * (1 - alpha2);
}
else {
pixF[0] = pix2[0]; // (no transparency case)
pixF[1] = pix2[1];
pixF[2] = pix2[2];
pixF[3] = pix2[3];
}
if (alpha1 < 0.99) {
pixF[0] = pixF[0] * alpha1; // apply target image opacity
pixF[1] = pixF[1] * alpha1;
pixF[2] = pixF[2] * alpha1;
pixF[3] = pixF[3] * alpha1;
}
kern = 0.2 * kern; // output target image changes
pix3[0] = kern * pixF[0] + (1 - kern) * pix3[0]; // gradually to source image
pix3[1] = kern * pixF[1] + (1 - kern) * pix3[1];
pix3[2] = kern * pixF[2] + (1 - kern) * pix3[2];
pix3[3] = kern * pixF[3] + (1 - kern) * pix3[3];
}
if (mode == 2) // erase
{
kern = 0.4 * kern;
pix3[0] = kern * pix1[0] + (1.0 - kern) * pix3[0]; // gradual erase
pix3[1] = kern * pix1[1] + (1.0 - kern) * pix3[1];
pix3[2] = kern * pix1[2] + (1.0 - kern) * pix3[2];
pix3[3] = kern * pix1[3] + (1.0 - kern) * pix3[3];
}
}
if (mode == 1) {
CEF->Fmods++;
CEF->Fsaved = 0;
}
px = mpx - mrad - 1; // repaint modified area
py = mpy - mrad - 1;
ww = 2 * mrad + 3;
Fpaint3(px,py,ww,ww,0);
return;
}
// save 1 block of pixels for possible undo
void copy_from_image::save_pixblock()
{
using namespace copy_from_image;
int cc, npix, dx, dy;
int pcc = 4 * sizeof(float);
float *pix3;
pixblockmem_t *save1B;
if (totmem > maxmem || totpixblock == maxpixblock) // free memory for oldest updates
while (totmem > 0.7 * maxmem || totpixblock > 0.7 * maxpixblock)
free_firstblock();
npix = 0;
for (dy = -mrad; dy <= mrad; dy++) // count pixels in block
for (dx = -mrad; dx <= mrad; dx++)
{
if (mpx + dx < 0 || mpx + dx > Eww-1) continue;
if (mpy + dy < 0 || mpy + dy > Ehh-1) continue;
npix++;
}
cc = npix * pcc + pixblockmem_cc;
save1B = (pixblockmem_t *) zmalloc(cc,"copy from image"); // allocate memory for block
pixblockmem[totpixblock] = save1B;
totpixblock += 1;
totmem += cc;
save1B->seq = pixblockseq; // save pixel block poop
save1B->mpx = mpx;
save1B->mpy = mpy;
save1B->mrad = mrad;
npix = 0;
for (dy = -mrad; dy <= mrad; dy++) // save pixels in block
for (dx = -mrad; dx <= mrad; dx++)
{
if (mpx + dx < 0 || mpx + dx > Eww-1) continue;
if (mpy + dy < 0 || mpy + dy > Ehh-1) continue;
pix3 = PXMpix(E3pxm,(mpx+dx),(mpy+dy)); // edited image pixel
memcpy(save1B->pixels[npix],pix3,pcc);
npix++;
}
return;
}
// undo last pixel block (newest edit) and free memory
void copy_from_image::undo_lastblock()
{
using namespace copy_from_image;
int ii, cc, npix;
int ww, px, py, dx, dy;
int pcc = 4 * sizeof(float);
float *pix3;
pixblockmem_t *save1B;
for (ii = totpixblock-1; ii >= 0; ii--)
{
save1B = pixblockmem[ii];
if (save1B->seq != pixblockseq) break;
px = save1B->mpx;
py = save1B->mpy;
mrad = save1B->mrad;
npix = 0;
for (dy = -mrad; dy <= mrad; dy++)
for (dx = -mrad; dx <= mrad; dx++)
{
if (px + dx < 0 || px + dx > Eww-1) continue;
if (py + dy < 0 || py + dy > Ehh-1) continue;
pix3 = PXMpix(E3pxm,(px+dx),(py+dy));
memcpy(pix3,save1B->pixels[npix],pcc);
npix++;
}
px = px - mrad - 1;
py = py - mrad - 1;
ww = 2 * mrad + 3;
Fpaint3(px,py,ww,ww,0);
zfree(save1B);
pixblockmem[ii] = 0;
cc = npix * pcc + pixblockmem_cc;
totmem -= cc;
totpixblock--;
}
if (pixblockseq > 0) --pixblockseq;
return;
}
// free memory for first pixel block (oldest edit)
void copy_from_image::free_firstblock()
{
using namespace copy_from_image;
int firstseq;
int ii, jj, cc, npix;
int px, py, dx, dy;
int pcc = 4 * sizeof(float);
pixblockmem_t *save1B;
if (! totpixblock) return;
firstseq = pixblockmem[0]->seq;
for (ii = 0; ii < totpixblock; ii++)
{
save1B = pixblockmem[ii];
if (save1B->seq != firstseq) break;
px = save1B->mpx;
py = save1B->mpy;
mrad = save1B->mrad;
npix = 0;
for (dy = -mrad; dy <= mrad; dy++)
for (dx = -mrad; dx <= mrad; dx++)
{
if (px + dx < 0 || px + dx > Eww-1) continue;
if (py + dy < 0 || py + dy > Ehh-1) continue;
npix++;
}
zfree(save1B);
pixblockmem[ii] = 0;
cc = npix * pcc + pixblockmem_cc;
totmem -= cc;
}
for (jj = 0; ii < totpixblock; jj++, ii++)
pixblockmem[jj] = pixblockmem[ii];
totpixblock = jj;
return;
}
// free all pixel block memory
void copy_from_image::free_allblocks()
{
using namespace copy_from_image;
int ii;
pixblockmem_t *save1B;
for (ii = totpixblock-1; ii >= 0; ii--)
{
save1B = pixblockmem[ii];
zfree(save1B);
}
pixblockseq = 0;
totpixblock = 0;
totmem = 0;
return;
}
/********************************************************************************/
// Copy From Image slave process - copy pixels from source image into shared memory.
// Started by m_copy_from_image() as a separate session to view the source image.
namespace copy_from_image_slave
{
void mousefunc();
void dopixels(); // update pixel block
PXM *pxm_1x = 0, *pxm_scaled = 0;
int mpxC, mpyC; // center of pixel copy area
int mpx, mpy; // center of moving mouse circle
int mrad; // radius of pixel copy area
float Fscale = 1;
typedef struct { // shared memory data
int killsource; // source image process should exit
int mpxC, mpyC; // center of pixel copy area (dest image)
int mpxS, mpyS; // center of pixel copy area (source image)
int mpx, mpy; // mouse drag position
int mrad; // mouse radius
float Fscale; // source image scale factor
int Freq; // source/target coord. flag
float pixels[402*402*4]; // pixel block, max. mrad = 200
} mmap_data_t;
mmap_data_t *mmap_data; // shared memory pointer
}
// menu function
void m_copy_from_image_slave(GtkWidget *, ch *) // split source/target processes
{
using namespace copy_from_image_slave;
int err, fd;
int pmpx = 0, pmpy = 0, pmrad = 0;
size_t cc;
F1_help_topic = "copy from image";
Plog(1,"m_copy_from_image_slave \n");
fd = shm_open("/fotocx_copy_from_image",O_RDWR+O_CREAT,0600); // identify memory mapped region
if (fd < 0) {
zmessageACK(Mwin,"shm_open() failure: %s",strerror(errno));
return;
}
cc = sizeof(mmap_data_t);
err = ftruncate(fd,cc);
if (err) {
zmessageACK(Mwin,"ftruncate() failure: %s",strerror(errno));
return;
}
mmap_data = (mmap_data_t *) mmap(0,cc,PROT_WRITE,MAP_SHARED,fd,0); // create memory mapped region
if (mmap_data == (void *) -1) {
zmessageACK(Mwin,"mmap() failure: %s",strerror(errno));
return;
}
ch *Pcurr_file = 0;
int Frefresh = 1;
int ww, hh;
int mpx2, mpy2, mrad2;
mrad = 50;
while (true)
{
zmainloop();
if (! curr_file) { // 24.60
zmainsleep(1);
continue;
}
if (mmap_data->killsource) goto done; // target image process is done
if (! Pcurr_file || ! strmatch(curr_file,Pcurr_file)) { // detect file change
zstrcopy(Pcurr_file,curr_file,"copy from image");
Frefresh = 1;
}
if (CEF) { // source image edit, wait until done
Frefresh = 1;
continue;
}
if (mmap_data->Fscale != Fscale) Frefresh = 1;
if (Frefresh)
{
Plog(1,"m_copy_from_image_slave() source image: %s \n",curr_file);
if (pxm_1x) PXM_free(pxm_1x); // refresh edited image
if (E0pxm) pxm_1x = PXM_copy(E0pxm);
else pxm_1x = PXM_load(curr_file,1); // load new image
if (! pxm_1x) goto fail;
Fscale = mmap_data->Fscale;
ww = pxm_1x->ww * Fscale;
hh = pxm_1x->hh * Fscale;
if (pxm_scaled) PXM_free(pxm_scaled);
pxm_scaled = PXM_rescale(pxm_1x,ww,hh);
if (! pxm_scaled) goto fail;
PXM_addalpha(pxm_scaled);
takeMouse(mousefunc,targcursor); // connect mouse function
Frefresh = 0;
}
mpx = mpxC + mmap_data->mpx - mmap_data->mpxC; // convert target copy area
mpy = mpyC + mmap_data->mpy - mmap_data->mpyC; // to source copy area
mrad = mmap_data->mrad;
if (mpx != pmpx || mpy != pmpy || mrad != pmrad) { // mouse circle changed
pmpx = mpx;
pmpy = mpy;
pmrad = mrad;
mpx2 = mpx / Fscale;
mpy2 = mpy / Fscale;
mrad2 = mrad / Fscale;
gdk_window_freeze_updates(gdkwin);
draw_mousecircle(0,0,0,1,0); // erase mouse circle
draw_mousecircle(mpx2,mpy2,mrad2,0,0); // draw mouse circle
gdk_window_thaw_updates(gdkwin);
zmainloop();
}
if (mmap_data->Freq == 1) { // pixels wanted from target image
dopixels(); // copy pixel block to shared memory
mmap_data->Freq = 2; // pixel block available
}
}
fail:
zmessageACK(Mwin,"source image failure (scale too big?)");
done:
if (CEF) edit_cancel(0);
Plog(1,"m_copy_from_image_slave() exit \n");
quitxx();
}
// mouse function
void copy_from_image_slave::mousefunc()
{
using namespace copy_from_image_slave;
if (LMclick && KBshiftkey) { // shift + left mouse click
LMclick = 0;
mmap_data->mpxS = Mxclick; // source image mouse center
mmap_data->mpyS = Myclick;
mpxC = Mxclick * Fscale; // source pixel copy area
mpyC = Myclick * Fscale;
}
return;
}
// copy block of pixels within mouse circle to shared memory
void copy_from_image_slave::dopixels()
{
using namespace copy_from_image_slave;
float *pix0, *pix1;
float pixnull[4] = { 0, 0, 0, 0 }; // black, alpha = 0
int dx, dy, qx, qy;
int ii, ww, hh, rs;
int pcc = 4 * sizeof(float);
ww = pxm_scaled->ww;
hh = pxm_scaled->hh;
rs = 2 * mrad + 1; // pixel block row stride
for (dy = -mrad; dy <= mrad; dy++) // loop surrounding block of pixels
for (dx = -mrad; dx <= mrad; dx++)
{
qx = mpx + dx;
qy = mpy + dy;
if (qx < 0 || qx > ww-1 || qy < 0 || qy > hh-1) pix0 = pixnull; // pixel outside source image
else pix0 = PXMpix(pxm_scaled,qx,qy); // source image pixel
ii = (dy + mrad) * rs + (dx + mrad);
pix1 = mmap_data->pixels + ii * 4; // to shared memory
memcpy(pix1,pix0,pcc);
}
return;
}
/********************************************************************************/
// Copy pixels from prior edit steps of current image.
// Mouse can be weak or strong, and edits are removed incrementally.
// E3 image (target) is incrementally replaced from chosen source image
// (one of the previous edit steps for the current image).
namespace copy_prior_edit_names
{
int radius;
int cpower;
int epower;
editfunc EFcopy_prior_edit;
}
// menu function
void m_copy_prior_edit(GtkWidget *, ch *menu) // menu function
{
using namespace copy_prior_edit_names;
int copy_prior_edit_dialog_event(zdialog *, ch *event); // dialog event function
void copy_prior_edit_mousefunc(); // mouse function
zdialog *zd;
ch text[40];
F1_help_topic = "copy prior edit";
Plog(1,"m_copy_prior_edit \n");
if (CEF) {
zmessageACK(Mwin,"finish current edit first");
return;
}
if (URS_pos < 1) {
zmessageACK(Mwin,"no prior edit");
return;
}
if (! E0pxm) {
zmessageACK(Mwin,"no current edit image");
return;
}
EFcopy_prior_edit.menufunc = m_copy_prior_edit; // start new edit
EFcopy_prior_edit.menuname = "Copy Prior Edit";
EFcopy_prior_edit.Farea = 1; // ignore area
EFcopy_prior_edit.mousefunc = copy_prior_edit_mousefunc;
if (! edit_setup(EFcopy_prior_edit)) return; // initz. edit
/***
______________________________________
| Copy Prior Edit |
| |
| prior edit step [_______________|v] |
| mouse radius [____] |
| power: center [____] edge [____] |
| |
| [OK] [X] |
|______________________________________|
***/
zd = zdialog_new("Copy Prior Edit",Mwin,"OK"," X ",null);
CEF->zd = zd;
zdialog_add_widget(zd,"hbox","hbstep","dialog",0,"space=3");
zdialog_add_widget(zd,"label","labstep","hbstep","prior edit step","space=5");
zdialog_add_widget(zd,"combo","editstep","hbstep");
zdialog_add_widget(zd,"hbox","hbr","dialog",0,"space=3");
zdialog_add_widget(zd,"label","labr","hbr","Mouse Radius","space=5");
zdialog_add_widget(zd,"zspin","radius","hbr","2|500|1|200");
zdialog_add_widget(zd,"hbox","hbt","dialog",0,"space=3");
zdialog_add_widget(zd,"label","labtc","hbt","power: center","space=5");
zdialog_add_widget(zd,"zspin","center","hbt","0|100|1|50");
zdialog_add_widget(zd,"label","labte","hbt","Edge","space=5");
zdialog_add_widget(zd,"zspin","edge","hbt","0|100|1|0");
for (int ii = 0; ii <= URS_pos; ii++) { // prior edit steps > dropdown list
snprintf(text,40,"%d %s",ii,URS_menu[ii]); // (includes current image, URS_pos 0)
zdialog_stuff(zd,"editstep",text);
}
E9pxm = PXM_copy(E1pxm); // initial source image (copy from)
radius = 100;
cpower = 50;
epower = 0;
zdialog_run(zd,copy_prior_edit_dialog_event,"save"); // run dialog - parallel
return;
}
// dialog event and completion function
int copy_prior_edit_dialog_event(zdialog *zd, ch *event)
{
using namespace copy_prior_edit_names;
void copy_prior_edit_mousefunc();
ch text[40];
int nn;
if (strmatch(event,"done")) zd->zstat = 1; // apply and quit
if (strmatch(event,"cancel")) zd->zstat = 2; // cancel
if (zd->zstat) // done or cancel
{
if (zd->zstat == 1) edit_done(0); // complete edit
else edit_cancel(0);
freeMouse(); // disconnect mouse function
return 1;
}
if (strmatch(event,"focus")) // toggle mouse capture
takeMouse(copy_prior_edit_mousefunc,0);
if (strmatch(event,"radius"))
zdialog_fetch(zd,"radius",radius); // mouse radius
if (strmatch(event,"center"))
zdialog_fetch(zd,"center",cpower); // mouse center power
if (strmatch(event,"edge"))
zdialog_fetch(zd,"edge",epower); // mouse edge power
if (strmatch(event,"editstep")) // select prior edit step
{
zdialog_fetch(zd,"editstep",text,40); // "nn menuname"
nn = atoi(text);
if (nn < 0 || nn > URS_max) return 1;
if (E9pxm) PXM_free(E9pxm);
E9pxm = load_undo(nn); // load prior edit output image
if (! E9pxm) return 1;
if (E9pxm->ww != Eww || E9pxm->hh != Ehh) {
zmessageACK(Mwin,"Image dimensions have changed \n"
"Copy Prior Edit cannot be used");
PXM_free(E9pxm); // free memory
E9pxm = 0;
}
}
return 1;
}
// mouse function
void copy_prior_edit_mousefunc()
{
using namespace copy_prior_edit_names;
int px, py, ww, rx, ry, radius2;
float rad, rad2, power, F1;
float R0, G0, B0, R3, G3, B3, R9, G9, B9;
float *pix0, *pix3, *pix9;
if (! CEF || ! E9pxm) return; // no source image
radius2 = radius * radius;
draw_mousecircle(Mxposn,Myposn,radius,0,0); // show mouse selection circle
if (! Mxdrag && ! Mydrag) return; // wait for drag event
Mxdrag = Mydrag = 0; // neutralize drag
for (rx = -radius; rx <= radius; rx++) // loop every pixel in radius
for (ry = -radius; ry <= radius; ry++)
{
rad2 = rx * rx + ry * ry;
if (rad2 > radius2) continue; // outside radius
px = Mxposn + rx;
py = Myposn + ry;
if (px < 0 || px > Eww-1) continue; // off the image edge
if (py < 0 || py > Ehh-1) continue;
rad = sqrt(rad2);
power = cpower + rad / radius * (epower - cpower); // power at pixel radius, 0 - 100
F1 = 0.0002 * power; // scale 0 - 0.02
pix0 = PXMpix(E0pxm,px,py); // original image pixel
pix3 = PXMpix(E3pxm,px,py); // current (painted) pixel
pix9 = PXMpix(E9pxm,px,py); // source image pixel
R0 = pix0[0]; // original pixel RGB
G0 = pix0[1];
B0 = pix0[2];
R3 = pix3[0]; // current pixel RGB
G3 = pix3[1];
B3 = pix3[2];
R9 = pix9[0]; // source pixel RGB
G9 = pix9[1];
B9 = pix9[2];
if (Mbutton == 1) { // left mouse, move to source image
R3 += F1 * (R9 - R0); // (paint)
G3 += F1 * (G9 - G0);
B3 += F1 * (B9 - B0);
}
else if (Mbutton == 3) { // right mouse, move to original image
R3 += F1 * (R0 - R9); // (unpaint)
G3 += F1 * (G0 - G9);
B3 += F1 * (B0 - B9);
}
if (R0 > R9) { // limit R3 range to R0 ... R1
if (R3 < R9) R3 = R9;
else if (R3 > R0) R3 = R0;
}
else {
if (R3 < R0) R3 = R0;
else if (R3 > R9) R3 = R9;
}
if (G0 > G9) { // G3 range
if (G3 < G9) G3 = G9;
else if (G3 > G0) G3 = G0;
}
else {
if (G3 < G0) G3 = G0;
else if (G3 > G9) G3 = G9;
}
if (B0 > B9) { // B3 range
if (B3 < B9) B3 = B9;
else if (B3 > B0) B3 = B0;
}
else {
if (B3 < B0) B3 = B0;
else if (B3 > B9) B3 = B9;
}
pix3[0] = R3;
pix3[1] = G3;
pix3[2] = B3;
}
px = Mxposn - radius - 1; // repaint modified area
py = Myposn - radius - 1;
ww = 2 * radius + 3;
Fpaint3(px,py,ww,ww,0);
draw_mousecircle(Mxposn,Myposn,radius,0,0); // show mouse selection circle
CEF->Fmods++;
CEF->Fsaved = 0;
return;
}
/********************************************************************************/
// minimal edit function to use as a template for coding new functions
namespace template_names
{
editfunc EFtemplate;
float userinput; // user input value, -1 ... +1
}
// menu function
void m_template(GtkWidget *, ch *menu)
{
using namespace template_names;
int template_dialog_event(zdialog* zd, ch *event);
void template_curvedit(int spc);
void * template_thread(void *);
GtkWidget *drawwin_scale; // drawing window for brightness scale
F1_help_topic = "edit template";
Plog(1,"m_template \n");
EFtemplate.menuname = "Template";
EFtemplate.menufunc = m_template;
EFtemplate.FprevReq = 1; // use preview
EFtemplate.Farea = 2; // select area usable
EFtemplate.Fpaintedits = 1; // use with paint edits OK
EFtemplate.Fscript = 1; // scripting supported
EFtemplate.threadfunc = template_thread;
if (! edit_setup(EFtemplate)) return; // setup edit
/***
_______________________________________________
| Template |
| ___________________________________________ |
| | | |
| | curve edit area | |
| | | |
| | ----------------------------------------- | | // editable function curve
| | | |
| | | |
| |___________________________________________| |
| |___________________________________________| | // black to white brightness scale
| |
| input =================[]================== | // user input value
| |
| [Reset] [OK] [X] |
|_______________________________________________|
***/
zdialog *zd = zdialog_new("template",Mwin,"Reset","OK"," X ",null);
EFtemplate.zd = zd;
zdialog_add_widget(zd,"frame","frameH","dialog",0,"expand"); // edit-curve
zdialog_add_widget(zd,"frame","frameB","dialog"); // brightness scale
zdialog_add_widget(zd,"hbox","hbsat","dialog",0,"space=5|expand");
zdialog_add_widget(zd,"label","labsat","hbsat","template");
zdialog_add_widget(zd,"hscale","userinput","hbsat","-1.0|1.0|0.01|0.0","expand");
GtkWidget *frameH = zdialog_gtkwidget(zd,"frameH"); // setup edit curves
spldat *sd = splcurve_init(frameH,template_curvedit);
EFtemplate.sd = sd;
sd->Nscale = 1; // horizontal line, neutral value
sd->xscale[0][0] = 0.00;
sd->yscale[0][0] = 0.50;
sd->xscale[1][0] = 1.00;
sd->yscale[1][0] = 0.50;
sd->nap[0] = 3; // initial curve is neutral
sd->vert[0] = 0;
sd->apx[0][0] = 0.01; // horizontal line
sd->apy[0][0] = 0.50;
sd->apx[0][1] = 0.50;
sd->apy[0][1] = 0.50; // curve 0 = overall brightness
sd->apx[0][2] = 0.99;
sd->apy[0][2] = 0.50;
splcurve_generate(sd,0);
sd->mod[0] = 0; // mark curve unmodified
sd->Nspc = 1; // 1 curve
sd->fact[0] = 1; // curve 0 active
GtkWidget *frameB = zdialog_gtkwidget(zd,"frameB"); // setup drawing area
drawwin_scale = gtk_drawing_area_new();
gtk_container_add(GTK_CONTAINER(frameB),drawwin_scale);
gtk_widget_set_size_request(drawwin_scale,100,12);
G_SIGNAL(drawwin_scale,"draw",brightness_scale,0);
userinput = 0; // neutral value
zdialog_resize(zd,350,300);
zdialog_run(zd,template_dialog_event,"save"); // run dialog - parallel
return;
}
// dialog event and completion callback function
int template_dialog_event(zdialog *zd, ch *event)
{
using namespace template_names;
spldat *sd = EFtemplate.sd;
float input0, dinput, dy;
float Fapply = 0;
int ii;
if (strmatch(event,"done")) zd->zstat = 2; // apply and quit
if (strmatch(event,"cancel")) zd->zstat = 3; // cancel
if (strmatch(event,"apply")) Fapply = 1; // from script
if (strmatch(event,"paint")) Fapply = 1; // mouse paint
if (strmatch(event,"fullsize")) { // from select area
edit_fullsize(); // get full size image
thread_signal();
thread_wait(); // required for paint edits
return 1;
}
if (zd->zstat == 1) // [reset]
{
zd->zstat = 0; // keep dialog active
zdialog_stuff(zd,"userinput",0); // neutral value
userinput = 0;
sd->nap[0] = 3; // curve is neutral
sd->vert[0] = 0;
sd->apx[0][0] = 0.01;
sd->apy[0][0] = 0.50;
sd->apx[0][1] = 0.50;
sd->apy[0][1] = 0.50;
sd->apx[0][2] = 0.99;
sd->apy[0][2] = 0.50;
splcurve_generate(sd,0);
sd->mod[0] = 0; // mark curve unmodified
gtk_widget_queue_draw(sd->drawarea); // redraw curves
edit_reset();
return 1;
}
if (zd->zstat == 2) // [OK]
{
freeMouse(); // free mouse if curve edit active
if (CEF->Fmods) {
edit_fullsize(); // get full size image
thread_signal(); // apply changes
edit_done(0); // complete edit
}
else edit_cancel(0); // no change
return 1;
}
if (zd->zstat < 0 || zd->zstat > 2) { // cancel
edit_cancel(0);
return 1;
}
if (strmatch(event,"userinput")) // input slider -1 ... +1
{
input0 = userinput; // old value
zdialog_fetch(zd,"userinput",userinput); // new value
dinput = userinput - input0; // change, + -
for (ii = 0; ii < sd->nap[0]; ii++) // update curve 0 nodes from input value
{
dy = sd->apy[0][ii] + 0.5 * dinput;
if (dy < 0) dy = 0;
if (dy > 1) dy = 1;
sd->apy[0][ii] = dy;
}
splcurve_generate(sd,0); // regenerate curve 0
gtk_widget_queue_draw(sd->drawarea); // draw curve 0
Fapply = 1;
}
if (Fapply) thread_signal(); // update the image
return 1;
}
// this function is called when the curve is edited
void template_curvedit(int spc)
{
using namespace template_names;
thread_signal();
return;
}
// thread function
void * template_thread(void *arg)
{
using namespace template_names;
void * template_wthread(void *);
if (Fpaintedits && ! Mdrag) return 0; // if mouse paint, must be drag
get_edit_pixels_init(NSMP,0); // initz. pixel loop
do_wthreads(template_wthread,NSMP);
CEF->Fmods++; // image3 modified
CEF->Fsaved = 0;
if (! Fpaintedits) Fpaint2(); // update window
return 0;
}
void * template_wthread(void *arg) // worker thread function
{
using namespace template_names;
int index = *((int *) arg);
int ii, Fend, px, py;
float *pix1, *pix3;
float R1, G1, B1, R3, G3, B3, R9, G9, B9;
float pixbrite, userinput;
float coeff = 1000.0 / 256.0;
float blend;
spldat *sd = EFtemplate.sd;
while (true) // loop all edit pixels
{
Fend = get_edit_pixels(index,px,py,blend);
if (Fend) break;
if (blend == 0) continue;
pix1 = PXMpix(E1pxm,px,py); // input pixel
pix3 = PXMpix(E3pxm,px,py); // output pixel
R1 = R9 = pix1[0]; // input RGB values
G1 = G9 = pix1[1];
B1 = B9 = pix1[2];
R3 = pix3[0]; // current output RGB
G3 = pix3[1];
B3 = pix3[2];
if (sd->mod[0]) // curve was modified
{
pixbrite = 0.333 * (R3 + G3 + B3); // pixel brightness, 0 to 255.9
ii = coeff * pixbrite; // curve index 0-999
if (ii < 0) ii = 0;
if (ii > 999) ii = 999;
userinput = 2.0 * sd->yval[0][ii] - 1.0; // -1 ... 0 ... +1
R9 = R9 + userinput * (R9 - pixbrite); // new output RGB
G9 = G9 + userinput * (G9 - pixbrite);
B9 = B9 + userinput * (B9 - pixbrite);
}
RGBfix(R9,G9,B9); // clamp values within 0-255
if (Fpaintedits) // gradual edit within mouse circle
{
if (blend > 0)
{
R3 = blend * R9 + (1-blend) * R3; // increase edit
G3 = blend * G9 + (1-blend) * G3;
B3 = blend * B9 + (1-blend) * B3;
}
else if (blend < 0)
{
R3 = -blend * R1 + (1+blend) * R3; // decrease edit
G3 = -blend * G1 + (1+blend) * G3;
B3 = -blend * B1 + (1+blend) * B3;
}
}
else // full edit for image or select area
{
R3 = blend * R9 + (1-blend) * R1; // output = blend of input and output
G3 = blend * G9 + (1-blend) * G1; // (select area edge blending)
B3 = blend * B9 + (1-blend) * B1;
}
pix3[0] = R3; // set output pixel
pix3[1] = G3;
pix3[2] = B3;
}
return 0;
}
|