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
|
/********************************************************************************
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 - functions for pixmap memory images
PXM_make create PXM pixmap RGB[A] float
PXM_free free PXM pixmap memory
PXM_audit audit PXM pixmap for errors
PXM_clear clear PXM pixmap white/opaque or black/transparent
PXM_addalpha add alhpa channel to PXM pixmap
PXM_subalpha remove alhpa channel from PXM pixmap
PXM_applyalpha apply alpha channel to RGB values
PXM_copy copy (duplicate) PXM pixmap
PXM_copy_area copy area from one PXM pixmap to another
PXM_add_margin add or remove margins from PXM edges
PXM_rescale copy and rescale PXM pixmap
PXM_rotate copy and rotate PXM pixmap
PXM_upright rotate and/or mirror PXM pixmap
PXM_blur blur PXM pixmap image using specified blur radius
PXM_rgbdepth set PXM pixmap image color depth 1-16 bits/color
PXB_make create PXB pixmap RGB[A] uint8
PXB_free free PXB pixmap memory
PXB_clear clear PXB pixmap to zeros
PXB_addalpha add alhpa channel to PXB pixmap
PXB_subalpha remove alpha channel from PXB pixmap
PXB_copy copy (duplicate) PXB pixmap
PXB_copy_area copy area from one PXB into another
PXB_subpxb create new PXB from section of existing PXB
PXB_half rescale PXB pixmap to 1/2 size
PXB_rescale rescale PXB pixmap - ww/hh independent
PXB_rescale_fast fast rescale PXB pixmap - ww/hh independent
PXB_rescale rescale PXB pixmap - max. ww/hh, preserve ratio
PXB_rescale_fast fast rescale PXB pixmap - max. ww/hh, preserve ratio
PXB_rotate rotate PXB pixmap, any angle
vpixel get virtual pixel at any PXM or PXB image location (float)
PXM_PXB_copy copy PXM pixmap to PXB pixmap
PXM_PXB_update copy/convert PXM pixmap area to PXB pixmap area
PXB_PXB_update copy/rescale input PXB pixmap area to output PXB area
PXB_load load an image file into a PXB pixmap (8-bit RGB)
PXM_load load an image file into a PXM pixmap (float RGB)
PXB_save save a PXB pixmap to a file with (8 bit RGB)
PXM_save save a PXM pixmap to a file with (8/16 bit RGB)
JPG_PXB_load load a .jpg file into a PXB pixmap (8-bit RGB)
JPG_PXM_load load a .jpg file into a PXM pixmap (float RGB)
PXB_JPG_save save a PXB pixmap to a .jpg file (8-bit RGB)
PXM_JPG_save save a PXM pixmap to a .jpg file (8-bit RGB)
TIFF_PXB_load load a .tif file into a PXB pixmap (8-bit RGB)
TIFF_PXM_load load a .tif file into a PXM pixmap (float RGB)
PXB_TIFF_save save a PXB pixmap to a .tif file (8/16-bit RGB)
PXM_TIFF_save save a PXM pixmap to a .tif file (8/16-bit RGB)
PNG_PXB_load load a .png file into a PXB pixmap (8-bit RGB)
PNG_PXM_load load a .png file into a PXM pixmap (float RGB)
PXB_PNG_save save a PXB pixmap to a .png file (8/16-bit RGB)
PXM_PNG_save save a PXM pixmap to a .png file (8/16-bit RGB)
HEIC_PXB_load load a .heic/.avif file into a PXB pixmap (8-bit RGB)
HEIC_PXM_load load a .heic/.avif file into a PXM pixmap (float RGB)
PXB_HEIC_save save a PXB pixmap to a .heic/.avif file (8/16-bit RGB)
PXM_HEIC_save save a PXM pixmap to a .heic/.avif file (8/16-bit RGB)
JP2_PXB_load load a .jp2 file into a PXB pixmap (8-bit RGB)
JP2_PXM_load load a .jp2 file into a PXM pixmap (float RGB)
PXB_JP2_save save a PXB pixmap to a .jp2 file (8/16-bit RGB)
PXM_JP2_save save a PXM pixmap to a .jp2 file (8/16-bit RGB)
WEBP_PXB_load load a .webp file into a PXB pixmap (8-bit RGB)
WEBP_PXM_load load a .webp file into a PXM pixmap (float RGB)
PXB_WEBP_save save a PXB pixmap to a .webp file (8/16-bit RGB)
PXM_WEBP_save save a PXM pixmap to a .webp file (8/16-bit RGB)
ANY_PXB_load load other image file into a PXB pixmap (8-bit RGB)
ANY_PXM_load load other image file into a PXM pixmap (float RGB)
RAW_PXB_load load a RAW file into a PXB pixmap (8-bit RGB)
RAW_PXM_load load a RAW file into a PXM pixmap (float RGB)
RAW_thumb_pxb get PXB for thumbnail file from RAW file embedded image
raw_match_embed_color use RAW file embedded image for image color balance
pixelvert convert pixel format and channel count
*********************************************************************************/
#define EX extern // disable extern declarations
#include "fotocx.h" // (variables in fotocx.h are refs)
using namespace zfuncs;
/*********************************************************************************
PXM pixmap functions - RGB[A] float pixel map
pixel RGB values may range from 0.0 to 255.99
*********************************************************************************/
// calculate image size in memory and check < 4 GB
// arguments are image width, height, no. colors, color size (char=1 or float=4)
// returns uint size (< 4 GB) or 0 if calculation > 4 GB
// pz is pixel RGB size (1/2/4 bytes for int8/int16/float)
int64 imagesize(int ww, int hh, int nc, int pz) // >4 GB allowed
{
int64 z64 = 1, size;
if (ww > wwhh_limit1) {
zmessageACK(Mwin,"image width too large to edit: %d \n",ww);
quitxx();
}
if (hh > wwhh_limit1) {
zmessageACK(Mwin,"image height too large to edit: %d \n",hh);
quitxx();
}
size = z64 * ww * hh;
if (size > wwhh_limit2) {
size = size / MEGA;
zmessageACK(Mwin,"image size too large to edit: %lld megapixels \n",size);
quitxx();
}
return z64 * ww * hh * nc * pz; // insure 64-bit arithmetic
}
// initialize PXM pixmap - allocate memory
PXM * PXM_make(int ww, int hh, int nc)
{
int64 cc64;
PXM *pxm = (PXM *) zmalloc(sizeof(PXM),"PXM");
strcpy(pxm->wmi,"pxmpix");
pxm->ww = ww;
pxm->hh = hh;
pxm->nc = nc; // nc = 3/4 for no/alpha channel
pxm->rs = ww * nc; // bytes = rs * sizeof(float)
cc64 = imagesize(ww,hh,nc,sizeof(float));
pxm->pixels = (float *) zmalloc(cc64,"PXM");
return pxm;
}
// free PXM pixmap
void PXM_free(PXM *&pxm)
{
if (! pxm) return;
if (! strmatch(pxm->wmi,"pxmpix"))
zappcrash("PXM_free(), bad PXM %s",pxm->wmi);
strcpy(pxm->wmi,"xxxxxx");
zfree(pxm->pixels);
zfree(pxm);
pxm = 0;
return;
}
// audit the contents of a PXM pixmap
// detect NAN and check RGB >= 0.0 and RGB < 256.0
namespace PXM_audit_names
{
int Pww, Phh;
int err1, err2, err3;
PXM *pxm;
}
void PXM_audit(PXM *pxmx)
{
using namespace PXM_audit_names;
void * PXM_audit_thread(void *index);
Plog(2,"PXM_audit \n");
pxm = pxmx;
Pww = pxm->ww;
Phh = pxm->hh;
err1 = err2 = err3 = 0;
do_wthreads(PXM_audit_thread,NSMP);
if (err1) Plog(0,"PXM_audit(): corrected %d RGB = NAN \n",err1);
if (err2) Plog(0,"PXM_audit(): corrected %d RGB < 0 \n",err2);
if (err3) Plog(0,"PXM_audit(): corrected %d RGB >= 256.0 \n",err3);
return;
}
void * PXM_audit_thread(void *arg)
{
using namespace PXM_audit_names;
int index = *((int *) arg);
int px, py;
float *pix;
for (py = index; py < Phh; py += NSMP)
for (px = 0; px < Pww; px++)
{
pix = PXMpix(pxm,px,py);
for (int ii = 0; ii < 3; ii++)
{
if (isnan(pix[ii])) {
err1++;
pix[ii] = 0;
continue;
}
if (pix[ii] < 0) {
err2++;
pix[ii] = 0;
continue;
}
if (pix[ii] >= 256.0) {
err3++;
pix[ii] = 255.9;
continue;
}
}
}
return 0;
}
// clear a PXM pixmap to white/opaque or black/transparent
void PXM_clear(PXM *pxm, int BW)
{
int px, py, ii;
float *pix, rgba;
if (BW) rgba = 255.0;
else rgba = 0;
for (py = 0; py < pxm->hh; py++)
for (px = 0; px < pxm->ww; px++)
{
pix = PXMpix(pxm,px,py);
for (ii = 0; ii < pxm->nc; ii++)
pix[ii] = rgba;
}
return;
}
// add an alpha channel to a PXM pixmap (RGB >> RGBA)
// does nothing if 4 channels already
void PXM_addalpha(PXM *pxm)
{
int ww, hh, nc1, nc2, ii;
float *pixels1, *pixels2, *pix1, *pix2;
uint cc;
int64 cc64;
ww = pxm->ww;
hh = pxm->hh;
nc1 = pxm->nc;
if (nc1 > 3) return;
pixels1 = pxm->pixels;
nc2 = nc1 + 1;
cc64 = imagesize(ww,hh,nc2,sizeof(float));
pixels2 = (float *) zmalloc(cc64,"PXM");
cc = nc1 * sizeof(float);
pix1 = pixels1;
pix2 = pixels2;
for (ii = 0; ii < ww * hh; ii++) {
memcpy(pix2,pix1,cc);
pix2[nc1] = 255.0; // 100% opaque = 255
pix1 += nc1;
pix2 += nc2;
}
zfree(pixels1);
pxm->nc = nc2;
pxm->rs = ww * nc2; // bytes = rs * sizeof(float)
pxm->pixels = pixels2;
return;
}
// remove alpha channel from a PXM pixmap (RGBA >> RGB)
// does nothing if no alpha channel present
void PXM_subalpha(PXM *pxm)
{
int ww, hh, nc1, nc2, ii;
float *pixels1, *pixels2, *pix1, *pix2;
uint cc;
int64 cc64;
ww = pxm->ww;
hh = pxm->hh;
nc1 = pxm->nc;
if (nc1 < 4) return;
pixels1 = pxm->pixels;
nc2 = 3;
cc64 = imagesize(ww,hh,nc2,sizeof(float));
pixels2 = (float *) zmalloc(cc64,"PXM");
cc = nc2 * sizeof(float);
pix1 = pixels1;
pix2 = pixels2;
for (ii = 0; ii < ww * hh; ii++) {
memcpy(pix2,pix1,cc);
pix1 += nc1;
pix2 += nc2;
}
zfree(pixels1);
pxm->nc = nc2;
pxm->rs = ww * nc2;
pxm->pixels = pixels2;
return;
}
// apply alpha channel to RGB values and remove alpha channel
// RGBout = RGBin * alpha/255
void PXM_applyalpha(PXM *pxm)
{
int ww, hh, nc1, nc2, ii;
float *pixels1, *pixels2, *pix1, *pix2;
float F;
int64 cc64;
ww = pxm->ww;
hh = pxm->hh;
nc1 = pxm->nc;
if (nc1 < 4) return;
pixels1 = pxm->pixels;
nc2 = 3;
cc64 = imagesize(ww,hh,nc2,sizeof(float));
pixels2 = (float *) zmalloc(cc64,"PXM");
pix1 = pixels1;
pix2 = pixels2;
for (ii = 0; ii < ww * hh; ii++)
{
F = 0.0039 * pix1[3]; // alpha / 255
pix2[0] = F * pix1[0];
pix2[1] = F * pix1[1];
pix2[2] = F * pix1[2];
pix1 += nc1;
pix2 += nc2;
}
zfree(pixels1);
pxm->nc = nc2;
pxm->rs = ww * nc2;
pxm->pixels = pixels2;
return;
}
// create a copy of a PXM pixmap
PXM * PXM_copy(PXM *pxm1)
{
int ww, hh, nc;
int64 cc64;
PXM *pxm2;
ww = pxm1->ww;
hh = pxm1->hh;
nc = pxm1->nc;
pxm2 = PXM_make(ww,hh,nc);
cc64 = imagesize(ww,hh,nc,sizeof(float));
memcpy(pxm2->pixels,pxm1->pixels,cc64);
return pxm2;
}
// copy one PXM pixmap to another PXM of equal size
// pxm1 >> pxm2
void PXM_copy(PXM *pxm1, PXM *pxm2)
{
int ww, hh, nc;
int64 cc64;
ww = pxm1->ww;
hh = pxm1->hh;
nc = pxm1->nc;
if (pxm2->ww != ww) zappcrash("PXM_copy() not same ww/hh");
if (pxm2->hh != hh) zappcrash("PXM_copy() not same ww/hh");
if (pxm2->nc != nc) zappcrash("PXM_copy() not same nc");
cc64 = imagesize(ww,hh,nc,sizeof(float));
memcpy(pxm2->pixels,pxm1->pixels,cc64);
return;
}
// create a copy of a PXM pixmap rectangular area
PXM * PXM_copy_area(PXM *pxm1, int orgx, int orgy, int ww2, int hh2)
{
float *pix1, *pix2;
PXM *pxm2 = 0;
int px1, py1, px2, py2;
int nc, pcc;
nc = pxm1->nc;
pcc = nc * sizeof(float);
pxm2 = PXM_make(ww2,hh2,nc);
for (py1 = orgy, py2 = 0; py2 < hh2; py1++, py2++)
{
pix1 = PXMpix(pxm1,orgx,py1);
pix2 = PXMpix(pxm2,0,py2);
for (px1 = orgx, px2 = 0; px2 < ww2; px1++, px2++)
{
memcpy(pix2,pix1,pcc);
pix1 += nc;
pix2 += nc;
}
}
return pxm2;
}
// copy a rectangular area from one PXM image into another - same origin
void PXM_copy_area(PXM *pxm1, PXM *pxm2, int orgx, int orgy, int ww, int hh)
{
float *pix1, *pix2;
int py, nc, cc;
nc = pxm1->nc;
cc = nc * ww * sizeof(float);
for (py = orgy; py < orgy + hh; py++)
{
pix1 = PXMpix(pxm1,orgx,py);
pix2 = PXMpix(pxm2,orgx,py);
memcpy(pix2,pix1,cc);
}
return;
}
// copy a rectangular area from one PXM image into another - different origin
void PXM_copy_area(PXM *pxm1, PXM *pxm2, int orgx1, int orgy1, int orgx2, int orgy2, int ww, int hh)
{
float *pix1, *pix2;
int py, nc, cc;
nc = pxm1->nc;
cc = nc * ww * sizeof(float);
for (py = 0; py < hh; py++)
{
pix1 = PXMpix(pxm1,orgx1,orgy1+py);
pix2 = PXMpix(pxm2,orgx2,orgy2+py);
memcpy(pix2,pix1,cc);
}
return;
}
/********************************************************************************
Rescale PXM pixmap to new width and height.
The scale ratios may be different for width and height.
Method is better than bilinear:
For all input pixels [p] having some overlap area with an output pixel:
output pixel = sum ( overlap[p] * input[p] ) / sum ( overlap[p] )
NOT THREAD SAFE
*********************************************************************************/
namespace pxmrescale {
float *ppix1, *ppix2;
int ww1, hh1, ww2, hh2, nc;
int *px1L, *py1L;
float *pxmap, *pymap;
int maxmapx, maxmapy;
}
PXM * PXM_rescale(PXM *pxm1, int ww, int hh)
{
using namespace pxmrescale;
void * pxm_rescale_thread(void *arg);
PXM *pxm2;
int px1, py1, px2, py2;
int pxl, pyl, pxm, pym, ii;
float scalex, scaley;
float px1a, py1a, px1b, py1b;
float fx, fy;
int64 cc64;
ww1 = pxm1->ww; // input PXM
hh1 = pxm1->hh;
nc = pxm1->nc;
ppix1 = pxm1->pixels;
pxm2 = PXM_make(ww,hh,nc); // output PXM
ww2 = ww;
hh2 = hh;
ppix2 = pxm2->pixels;
cc64 = imagesize(ww2,hh2,nc,sizeof(float));
memset(ppix2, 0, cc64); // clear output pixmap
scalex = 1.0 * ww1 / ww2; // compute x and y scales
scaley = 1.0 * hh1 / hh2;
if (scalex <= 1) maxmapx = 2; // compute max input pixels
else maxmapx = scalex + 2; // mapping into output pixels
maxmapx += 1; // for both dimensions
if (scaley <= 1) maxmapy = 2; // (pixels may not be square)
else maxmapy = scaley + 2;
maxmapy += 1; // (extra entry for -1 flag)
pymap = (float *) zmalloc(hh2 * maxmapy * sizeof(float),"PXM"); // maps overlap of < maxmap input
pxmap = (float *) zmalloc(ww2 * maxmapx * sizeof(float),"PXM"); // pixels per output pixel
py1L = (int *) zmalloc(hh2 * sizeof(int),"PXM"); // maps first (lowest) input pixel
px1L = (int *) zmalloc(ww2 * sizeof(int),"PXM"); // per output pixel
for (py2 = 0; py2 < hh2; py2++) // loop output y-pixels
{
py1a = py2 * scaley; // corresponding input y-pixels
py1b = py1a + scaley;
if (py1b >= hh1) py1b = hh1 - 0.001; // fix precision limitation
pyl = py1a;
py1L[py2] = pyl; // 1st overlapping input pixel
for (py1 = pyl, pym = 0; py1 < py1b; py1++, pym++) // loop overlapping input pixels
{
if (py1 < py1a) { // compute amount of overlap
if (py1+1 < py1b) fy = py1+1 - py1a; // 0.0 to 1.0
else fy = scaley;
}
else if (py1+1 > py1b) fy = py1b - py1;
else fy = 1;
ii = py2 * maxmapy + pym; // save it
pymap[ii] = 0.999 * fy / scaley;
}
ii = py2 * maxmapy + pym; // set an end marker after
pymap[ii] = -1; // last overlapping pixel
}
for (px2 = 0; px2 < ww2; px2++) // do same for x-pixels
{
px1a = px2 * scalex;
px1b = px1a + scalex;
if (px1b >= ww1) px1b = ww1 - 0.001;
pxl = px1a;
px1L[px2] = pxl;
for (px1 = pxl, pxm = 0; px1 < px1b; px1++, pxm++)
{
if (px1 < px1a) {
if (px1+1 < px1b) fx = px1+1 - px1a;
else fx = scalex;
}
else if (px1+1 > px1b) fx = px1b - px1;
else fx = 1;
ii = px2 * maxmapx + pxm;
pxmap[ii] = 0.999 * fx / scalex;
}
ii = px2 * maxmapx + pxm;
pxmap[ii] = -1;
}
do_wthreads(pxm_rescale_thread,NSMP);
zfree(px1L);
zfree(py1L);
zfree(pxmap);
zfree(pymap);
return pxm2;
}
void * pxm_rescale_thread(void *arg) // worker thread function
{
using namespace pxmrescale;
int index = *((int *) arg);
int px1, py1, px2, py2;
int pxl, pyl, pxm, pym, ii;
float *pixel1, *pixel2;
float fx, fy, ftot;
float chan[6];
int pcc = nc * sizeof(float);
int64 z64 = 1;
for (py2 = index; py2 < hh2; py2 += NSMP) // loop output y-pixels
{
pyl = py1L[py2]; // corresp. 1st input y-pixel
for (px2 = 0; px2 < ww2; px2++) // loop output x-pixels
{
pxl = px1L[px2]; // corresp. 1st input x-pixel
memset(chan,0,pcc); // initz. output pixel
for (py1 = pyl, pym = 0; ; py1++, pym++) // loop overlapping input y-pixels
{
ii = py2 * maxmapy + pym; // get y-overlap
fy = pymap[ii];
if (fy < 0) break; // no more pixels
for (px1 = pxl, pxm = 0; ; px1++, pxm++) // loop overlapping input x-pixels
{
ii = px2 * maxmapx + pxm; // get x-overlap
fx = pxmap[ii];
if (fx < 0) break; // no more pixels
ftot = fx * fy; // area overlap = x * y overlap
pixel1 = ppix1 + (z64 * py1 * ww1 + px1) * nc;
for (ii = 0; ii < nc; ii++)
chan[ii] += pixel1[ii] * ftot; // add input pixel * overlap
}
}
pixel2 = ppix2 + (z64 * py2 * ww2 + px2) * nc; // save output pixel
memcpy(pixel2,chan,pcc);
}
}
return 0;
}
/********************************************************************************
PXM *pxm2 = PXM_rotate(PXM *pxm1, float angle)
Rotate PXM pixmap through an arbitrary angle (degrees).
The returned image has the same size as the original, but the
pixmap size is increased to accommodate the rotated image.
(e.g. a 100x100 image rotated 45 deg. needs a 142x142 pixmap).
The space added around the rotated image is black (RGB 0,0,0).
Angle is in degrees. Positive direction is clockwise.
Speed is about 28 million pixels/sec/thread for a 3.3 GHz CPU.
Loss of resolution is less than 1 pixel.
NOT THREAD SAFE
*********************************************************************************/
namespace pxmrotate {
float *ppix1, *ppix2;
int ww1, hh1, ww2, hh2, nc;
float angle;
}
PXM * PXM_rotate(PXM *pxm1, float anglex, int fast) // fast option
{
using namespace pxmrotate;
PXM *PXM_rotate90(PXM *pxm, int angle);
void *PXM_rotate_thread(void *);
void *PXM_rotate_thread_fast(void *);
PXM *pxm2;
ww1 = pxm1->ww; // input PXM
hh1 = pxm1->hh;
nc = pxm1->nc;
ppix1 = pxm1->pixels;
angle = anglex;
while (angle < -180) angle += 360; // normalize, -180 to +180
while (angle > 180) angle -= 360;
if (angle >= -180.0 && angle < -179.99) // use lossless version for angles
return PXM_rotate90(pxm1,180); // of -180, -90, 0, 90, 180
if (angle > -90.01 && angle < -89.99)
return PXM_rotate90(pxm1,-90);
if (angle > -0.01 && angle < 0.01)
return PXM_copy(pxm1);
if (angle > 89.99 && angle < 90.01)
return PXM_rotate90(pxm1,90);
if (angle > 179.99 && angle <= 180.0)
return PXM_rotate90(pxm1,180);
angle = angle * PI / 180; // radians, -PI to +PI
ww2 = ww1*fabsf(cosf(angle)) + hh1*fabsf(sinf(angle)); // rectangle containing rotated image
hh2 = ww1*fabsf(sinf(angle)) + hh1*fabsf(cosf(angle));
pxm2 = PXM_make(ww2,hh2,nc); // output PXM
ppix2 = pxm2->pixels;
if (fast) do_wthreads(PXM_rotate_thread_fast,NSMP);
else do_wthreads(PXM_rotate_thread,NSMP);
return pxm2;
}
void * PXM_rotate_thread(void *arg)
{
using namespace pxmrotate;
int index = *((int *) (arg));
int px2, py2, px0, py0;
float *pix0, *pix1, *pix2, *pix3;
float px1, py1;
float f0, f1, f2, f3, pixN[6];
float a, b, ww15, hh15, ww25, hh25;
int pcc = nc * sizeof(float);
int64 z64 = 1;
ww15 = 0.5 * ww1;
hh15 = 0.5 * hh1;
ww25 = 0.5 * ww2;
hh25 = 0.5 * hh2;
a = cosf(angle);
b = sinf(angle);
for (py2 = index; py2 < hh2; py2 += NSMP) // loop through output pixels
for (px2 = 0; px2 < ww2; px2++)
{
px1 = a * (px2 - ww25) + b * (py2 - hh25) + ww15; // (px1,py1) = corresponding
py1 = -b * (px2 - ww25) + a * (py2 - hh25) + hh15; // point within input pixels
px0 = px1; // pixel containing (px1,py1)
py0 = py1;
if (px0 < 0 || px0 > ww1-2 || py0 < 0 || py0 > hh1-2) { // if outside input pixel array
pix2 = ppix2 + (z64 * py2 * ww2 + px2) * nc; // output is black
memset(pix2,0,pcc);
continue;
}
pix0 = ppix1 + (z64 * py0 * ww1 + px0) * nc; // 4 input pixels based at (px0,py0)
pix1 = pix0 + ww1 * nc;
pix2 = pix0 + nc;
pix3 = pix1 + nc;
f0 = (px0+1 - px1) * (py0+1 - py1); // overlap of (px1,py1)
f1 = (px0+1 - px1) * (py1 - py0); // in each of the 4 pixels
f2 = (px1 - px0) * (py0+1 - py1);
f3 = (px1 - px0) * (py1 - py0);
for (int ii = 0; ii < nc; ii++) // sum the weighted inputs
pixN[ii] = f0 * pix0[ii] + f1 * pix1[ii]
+ f2 * pix2[ii] + f3 * pix3[ii];
pix2 = ppix2 + (z64 * py2 * ww2 + px2) * nc; // output pixel
memcpy(pix2,pixN,pcc);
}
return 0;
}
void * PXM_rotate_thread_fast(void *arg)
{
using namespace pxmrotate;
int index = *((int *) (arg));
int px2, py2, px0, py0;
float *pix0, *pix2;
float px1, py1;
float a, b, ww15, hh15, ww25, hh25;
int pcc = nc * sizeof(float);
int z64 = 1;
ww15 = 0.5 * ww1;
hh15 = 0.5 * hh1;
ww25 = 0.5 * ww2;
hh25 = 0.5 * hh2;
a = cosf(angle);
b = sinf(angle);
for (py2 = index; py2 < hh2; py2 += NSMP) // loop through output pixels
for (px2 = 0; px2 < ww2; px2++)
{
px1 = a * (px2 - ww25) + b * (py2 - hh25) + ww15; // (px1,py1) = corresponding
py1 = -b * (px2 - ww25) + a * (py2 - hh25) + hh15; // point within input pixels
px0 = px1; // pixel containing (px1,py1)
py0 = py1;
if (px0 < 0 || px0 > ww1-2 || py0 < 0 || py0 > hh1-2) { // if outside input pixel array
pix2 = ppix2 + (z64 * py2 * ww2 + px2) * nc; // output is black
memset(pix2,0,pcc);
continue;
}
pix0 = ppix1 + (z64 * py0 * ww1 + px0) * nc; // input pixel
pix2 = ppix2 + (z64 * py2 * ww2 + px2) * nc; // output pixel
memcpy(pix2,pix0,pcc);
}
return 0;
}
PXM * PXM_rotate90(PXM *pxm1, int angle) // angle = -90, 90, 180
{
using namespace pxmrotate;
int px1, py1, px2, py2, nc, pcc;
float *pix1, *pix2;
PXM *pxm2;
int z64 = 1;
if (angle == 0) return PXM_copy(pxm1);
ww1 = pxm1->ww; // input PXM
hh1 = pxm1->hh;
nc = pxm1->nc;
pcc = nc * sizeof(float);
if (angle == -90) {
ww2 = hh1;
hh2 = ww1;
}
else if (angle == 90) {
ww2 = hh1;
hh2 = ww1;
}
else if (angle == 180) {
ww2 = ww1;
hh2 = hh1;
}
else zappcrash("PXM_rotate2() bad angle %d",angle);
pxm2 = PXM_make(ww2,hh2,nc); // output PXM
ppix2 = pxm2->pixels;
for (py1 = 0; py1 < hh1; py1++) // loop all input pixels
for (px1 = 0; px1 < ww1; px1++)
{
if (angle == -90) {
px2 = py1;
py2 = hh2 - px1 - 1;
}
else if (angle == 90) {
px2 = ww2 - py1 - 1;
py2 = px1;
}
else /* angle = 180 */ {
px2 = ww2 - px1 - 1;
py2 = hh2 - py1 - 1;
}
pix1 = ppix1 + (z64 * py1 * ww1 + px1) * nc;
pix2 = ppix2 + (z64 * py2 * ww2 + px2) * nc;
memcpy(pix2,pix1,pcc);
}
return pxm2;
}
/********************************************************************************
PXM *pxm2 = PXM_upright(PXM *pxm1, int angle, int mirror)
Rotate a PXM pixmap 0 or 90 or 180 or 270 degrees,
and mirror the image horizontally or vertically.
angle angle of clockwise rotation: 0/90/180/270.
mirror 0/1/2 = no/horizontal/vertical mirror.
(mirror is done after rotation)
The returned image has the same size as the original,
but length and width are reversed if rotation = 90 or 270.
If angle = 0 and mirror = 0, a copy of the input image is returned.
There is no loss of resolution.
NOT THREAD SAFE
*********************************************************************************/
PXM * PXM_upright(PXM *pxm1, int angle, int mirror)
{
PXM *pxm2, *pxm3;
float *pix2, *pix3;
int px, py;
int pcc = pxm1->nc * sizeof(float); // pixel cc
if (angle != 0 && angle != 90 && angle != 180 && angle != 270)
zappcrash("PXM_upright() angle: %d",angle);
if (mirror != 0 && mirror != 1 && mirror != 2)
zappcrash("PXM_upright() mirror: %d",mirror);
pxm2 = PXM_rotate(pxm1,angle);
if (! mirror) return pxm2;
if (mirror == 1) // horizontal mirror
{
pxm3 = PXM_copy(pxm2);
for (py = 0; py < pxm2->hh; py++)
for (px = 0; px < pxm2->ww; px++)
{
pix2 = PXMpix(pxm2,px,py);
pix3 = PXMpix(pxm3,pxm2->ww-1-px,py);
memcpy(pix3,pix2,pcc);
}
PXM_free(pxm2);
return pxm3;
}
if (mirror == 2) // vertical mirror
{
pxm3 = PXM_copy(pxm2);
for (py = 0; py < pxm2->hh; py++)
for (px = 0; px < pxm2->ww; px++)
{
pix2 = PXMpix(pxm2,px,py);
pix3 = PXMpix(pxm3,px,pxm2->hh-1-py);
memcpy(pix3,pix2,pcc);
}
PXM_free(pxm2);
return pxm3;
}
return 0; // should not happen
}
/********************************************************************************/
// blur a PXM pixmap image using specified blur radius
namespace PXM_blur_names
{
int rad;
PXM *pxm_input;
PXM *pxm_temp;
int Pww, Phh;
float blur_weight[1000]; // blur radius limit 999
}
// callable function
void PXM_blur(PXM *pxm, int radius)
{
using namespace PXM_blur_names;
void * PXM_blur_wthread1(void *);
void * PXM_blur_wthread2(void *);
int ii;
float w, wsum;
if (radius < 1) return;
if (radius > 999) zappcrash("PXM_blur() radius: %d",radius);
rad = radius;
Pww = pxm->ww; // image dimensions
Phh = pxm->hh;
pxm_input = pxm; // input image
pxm_temp = PXM_copy(pxm_input); // intermediate image
wsum = 0;
for (ii = 0; ii <= rad; ii++) // set pixel weight per distance
{ // example, rad = 10
w = 1.0 - (ii + 0.25) / rad; // dist: 0 1 2 3 5 7 9
w = w * w; // weight: .95 .77 .60 .46 .23 .08 .01
blur_weight[ii] = w;
wsum += w;
}
for (ii = 0; ii <= rad; ii++) // make weights sum to 1.0
blur_weight[ii] = blur_weight[ii] / wsum;
do_wthreads(PXM_blur_wthread1,NSMP); // worker threads 1
do_wthreads(PXM_blur_wthread2,NSMP); // worker threads 2
PXM_free(pxm_temp);
return;
}
// worker thread 1
void * PXM_blur_wthread1(void *arg)
{
using namespace PXM_blur_names;
int index = *((int *) arg);
int ii, dist = 0;
int px, py, qy;
int ylo, yhi;
float R9, G9, B9, w1, w2;
float *pix1, *pix2;
for (py = index; py < Phh; py += NSMP) // loop all pixels
for (px = 0; px < Pww; px++)
{
if (Fescape) return 0;
if (sa_stat == sa_stat_fini) { // select area active
ii = py * Pww + px;
if (! sa_pixmap[ii]) continue; // outside pixel
}
pix1 = PXMpix(pxm_input,px,py); // input pixel
pix2 = PXMpix(pxm_temp,px,py); // output pixel - intermediate image
ylo = py - rad; // get column of input pixel
if (ylo < 0) ylo = 0;
yhi = py + rad;
if (yhi > Phh-1) yhi = Phh - 1;
R9 = G9 = B9 = 0;
w2 = 0;
for (qy = ylo; qy <= yhi; qy++) // loop pixels in same column
{
if (sa_stat == sa_stat_fini) {
ii = qy * Pww + px; // don't use pixels outside area
dist = sa_pixmap[ii];
if (! dist) continue;
}
w1 = blur_weight[abs(qy-py)]; // weight based on radius
pix1 = PXMpix(E3pxm,px,qy);
R9 += w1 * pix1[0]; // accumulate RGB * weight
G9 += w1 * pix1[1];
B9 += w1 * pix1[2];
w2 += w1; // accumulate weights
}
R9 = R9 / w2; // normalize
G9 = G9 / w2;
B9 = B9 / w2;
pix2[0] = R9; // weighted average
pix2[1] = G9;
pix2[2] = B9;
}
return 0;
}
// worker thread 2
void * PXM_blur_wthread2(void *arg)
{
using namespace PXM_blur_names;
int index = *((int *) arg);
int ii, dist = 0;
int px, py, qx;
int xlo, xhi;
float R9, G9, B9;
float w1, w2;
float *pix2, *pix3;
for (py = index; py < Phh; py += NSMP) // loop all pixels
for (px = 0; px < Pww; px++)
{
if (Fescape) return 0;
if (sa_stat == sa_stat_fini) { // select area active
ii = py * Pww + px;
if (! sa_pixmap[ii]) continue; // outside pixel
}
pix3 = PXMpix(pxm_input,px,py); // output pixel
xlo = px - rad; // get row of input pixel
if (xlo < 0) xlo = 0;
xhi = px + rad;
if (xhi > Pww-1) xhi = Pww - 1;
R9 = G9 = B9 = 0;
w2 = 0;
for (qx = xlo; qx <= xhi; qx++) // loop pixels in same row
{
if (sa_stat == sa_stat_fini) {
ii = py * Pww + qx; // don't use pixels outside area
dist = sa_pixmap[ii];
if (! dist) continue;
}
pix2 = PXMpix(pxm_temp,qx,py); // intermediate image, input pixel
w1 = blur_weight[abs(qx-px)]; // weight based on radius
R9 += w1 * pix2[0]; // accumulate RGB * weight
G9 += w1 * pix2[1];
B9 += w1 * pix2[2];
w2 += w1; // accumulate weights
}
R9 = R9 / w2; // normalize
G9 = G9 / w2;
B9 = B9 / w2;
RGBfix(R9,G9,B9);
pix3[0] = R9; // set output pixel
pix3[1] = G9;
pix3[2] = B9;
}
return 0;
}
/********************************************************************************/
// set PXM pixmap image color depth to 1-16 bits per RGB color
// alpha channel is not affected
void PXM_rgbdepth(PXM *pxm, int color_depth)
{
int ii, px, py, rgb;
uint16 m1, m2, val1, val3;
float fmag, *pix1, *pix3;
int Pww, Phh;
Pww = pxm->ww;
Phh = pxm->hh;
m1 = 0xFFFF << (16 - color_depth); // 5 > 1111100000000000
m2 = 0x8000 >> color_depth; // 5 > 0000010000000000
fmag = 65535.0 / m1; // full brightness range
for (py = 0; py < Phh; py++) // loop all pixels
for (px = 0; px < Pww; px++)
{
if (Fescape) break;
if (sa_stat == sa_stat_fini) { // select area active
ii = py * Pww + px;
if (! sa_pixmap[ii]) continue; // outside pixel
}
pix1 = PXMpix(E3pxm,px,py); // input pixel
pix3 = PXMpix(E3pxm,px,py); // output pixel
for (rgb = 0; rgb < 3; rgb++)
{
val1 = 256 * pix1[rgb];
if (val1 < m1) val3 = (val1 + m2) & m1;
else val3 = m1;
val3 = val3 * fmag;
pix3[rgb] = val3 / 256;
}
}
return;
}
/********************************************************************************
PXB pixmap functions - RGB[A] uint8 pixel map and PIXBUF wrapper
*********************************************************************************/
// Create PXB pixmap with pixels cleared to zero
// nc = 3/4 for RGB/RGBA image
PXB * PXB_make(int ww, int hh, int nc)
{
int rs, ac;
int64 cc64;
uint8 *pixels;
if (nc != 3 && nc != 4) zappcrash("PXB_make() nc: %d",nc);
PXB *pxb = (PXB *) zmalloc(sizeof(PXB),"PXB");
strcpy(pxb->wmi,"pxbpix");
pxb->ww = ww;
pxb->hh = hh;
pxb->nc = nc;
rs = ww * nc;
pxb->rs = rs;
cc64 = imagesize(ww,hh,nc,1);
pixels = (uint8 *) zmalloc(cc64,"PXB");
pxb->pixels = pixels;
ac = nc - 3; // alpha channel 0/1
pxb->pixbuf = gdk_pixbuf_new_from_data(pixels,GDKRGB,ac,8,ww,hh,rs,0,0); // make congruent pixbuf
if (! pxb->pixbuf) {
zmessageACK(Mwin,"PXB_make(): pixbuf creation failure");
quitxx();
}
return pxb;
}
// Free PXB pixmap - release memory
void PXB_free(PXB *&pxb)
{
if (! pxb) return;
if (! strmatch(pxb->wmi,"pxbpix"))
zappcrash("PXB_free(), bad PXB");
strcpy(pxb->wmi,"xxxxxx");
g_object_unref(pxb->pixbuf);
zfree(pxb->pixels);
zfree(pxb);
pxb = 0;
return;
}
// clear PXB pixmap to zeros (black)
void PXB_clear(PXB * pxb)
{
int64 cc64 = imagesize(pxb->ww,pxb->hh,pxb->nc,1);
memset(pxb->pixels,0,cc64);
return;
}
// add an alpha channel to a PXB pixmap
int PXB_addalpha(PXB *pxb)
{
uint8 *pixels1, *pixels2, *pix1, *pix2;
int ww, hh, nc, rs;
int64 cc64;
nc = pxb->nc;
if (nc == 4) return 0;
if (nc != 3) zappcrash("PXB_addalpha() nc: %d",nc);
ww = pxb->ww;
hh = pxb->hh;
pixels1 = pxb->pixels;
cc64 = imagesize(ww,hh,4,1);
pixels2 = (uint8 *) zmalloc(cc64,"PXB");
pix1 = pixels1;
pix2 = pixels2;
for (int ii = 0; ii < ww * hh; ii++)
{
memcpy(pix2,pix1,3);
pix2[3] = 255; // opacity = max.
pix1 += 3;
pix2 += 4;
}
zfree(pixels1);
pxb->pixels = pixels2;
pxb->nc = 4;
rs = ww * 4;
pxb->rs = rs;
g_object_unref(pxb->pixbuf);
pxb->pixbuf = gdk_pixbuf_new_from_data(pixels2,GDKRGB,1,8,ww,hh,rs,0,0);
if (! pxb->pixbuf) {
zmessageACK(Mwin,"PXB_addalpha(): pixbuf creation failure");
quitxx();
}
return 0;
}
// remove an alpha channel from a PXB pixmap
// return: 0 = OK, +N = error
int PXB_subalpha(PXB *pxb)
{
uint8 *pixels1, *pixels2, *pix1, *pix2;
int ww, hh, nc, rs;
int64 cc64;
nc = pxb->nc;
if (nc == 3) return 0;
if (nc != 4) zappcrash("PXB_subalpha() nc: %d",nc);
ww = pxb->ww;
hh = pxb->hh;
pixels1 = pxb->pixels;
cc64 = imagesize(ww,hh,3,1);
pixels2 = (uint8 *) zmalloc(cc64,"PXB");
pix1 = pixels1;
pix2 = pixels2;
for (int ii = 0; ii < ww * hh; ii++)
{
memcpy(pix2,pix1,3);
pix1 += 4;
pix2 += 3;
}
zfree(pixels1);
pxb->pixels = pixels2;
pxb->nc = 3;
rs = ww * 3;
pxb->rs = rs;
g_object_unref(pxb->pixbuf);
pxb->pixbuf = gdk_pixbuf_new_from_data(pixels2,GDKRGB,0,8,ww,hh,rs,0,0);
if (! pxb->pixbuf) {
zmessageACK(Mwin,"PXB_subalpha(): pixbuf creation failure");
quitxx();
}
return 0;
}
// Copy a PXB pixmap to a new PXB pixmap
PXB * PXB_copy(PXB *pxb1)
{
int ww, hh, nc;
int64 cc64;
ww = pxb1->ww;
hh = pxb1->hh;
nc = pxb1->nc;
cc64 = imagesize(ww,hh,nc,1);
PXB *pxb2 = PXB_make(ww,hh,nc);
memcpy(pxb2->pixels,pxb1->pixels,cc64);
return pxb2;
}
// copy an area from an input PXB to an output PXB
// input area (px1, py1, ww, hh) copied to output area at (px2, py2)
void PXB_copy_area(PXB *pxb1, int px1, int py1, int ww, int hh, PXB *pxb2, int px2, int py2)
{
int row1, row2;
int64 z64 = 1;
if (px1 < 0) goto overflow;
if (px1 + ww > pxb1->ww) goto overflow;
if (py1 < 0) goto overflow;
if (py1 + hh > pxb1->hh) goto overflow;
if (px2 < 0) goto overflow;
if (px2 + ww > pxb2->ww) goto overflow;
if (py2 < 0) goto overflow;
if (py2 + hh > pxb2->hh) goto overflow;
if (pxb1->nc != pxb2->nc) zappcrash("PXB_copy_area(): NC unequal");
for (row1 = py1, row2 = py2; row1 < py1 + hh; row1++, row2++)
memcpy(pxb2->pixels + z64 * row2 * pxb2->rs + px2 * pxb2->nc,
pxb1->pixels + z64 * row1 * pxb1->rs + px1 * pxb1->nc, ww * pxb1->nc);
return;
overflow:
zappcrash("PXB_copy_area() overflow: input: %d %d %d %d "
"output: %d %d",px1,py1,ww,hh,px2,py2);
}
// create a new PXB pixmap from a subsection of an existing PXB
PXB * PXB_subpxb(PXB *pxb1, int px1, int py1, int ww, int hh)
{
PXB *pxb2 = PXB_make(ww,hh,pxb1->nc);
PXB_copy_area(pxb1,px1,py1,ww,hh,pxb2,0,0);
return pxb2;
}
/********************************************************************************/
// rescale a PXB pixmap image to 1/2 size using NSMP threads
// if rows or columns are odd, the last one is omitted
typedef struct {
uint8 *pixels1, *pixels2;
int ww1, hh1, rs1, nc1;
int ww2, hh2, rs2, nc2;
}
pxb_half_data_t;
PXB * PXB_half(PXB *pxb1)
{
void * pxb_half_thread(void *arg);
pxb_half_data_t P;
PXB *pxb2;
P.ww1 = pxb1->ww; // input image data
P.hh1 = pxb1->hh;
P.rs1 = pxb1->rs;
P.nc1 = pxb1->nc;
P.pixels1 = pxb1->pixels;
P.ww2 = P.ww1 / 2; // output image data
P.hh2 = P.hh1 / 2;
P.nc2 = P.nc1;
pxb2 = PXB_make(P.ww2,P.hh2,P.nc2); // output PXB
P.pixels2 = pxb2->pixels;
P.rs2 = pxb2->rs;
void *args[Xsmp][2];
pthread_t tid[Xsmp];
for (int ii = 0; ii < NSMP; ii++) {
args[ii][0] = &P;
args[ii][1] = &Nval[ii];
tid[ii] = start_Jthread(pxb_half_thread,args[ii]);
}
for (int ii = 0; ii < NSMP; ii++)
wait_Jthread(tid[ii]);
return pxb2;
}
void * pxb_half_thread(void *arg)
{
int row, col, ch1, nc, rgb;
uint8 *pix1a, *pix1b, *pix2;
int64 z64 = 1;
void ** args = (void **) arg;
pxb_half_data_t *P = (pxb_half_data_t *) args[0];
int index = * ((int *) args[1]);
nc = P->nc1;
for (row = index; row < P->hh2; row += NSMP) // loop output rows
{
pix1a = P->pixels1 + z64 * P->rs1 * row * 2; // 2 input rows
pix1b = pix1a + P->rs1;
pix2 = P->pixels2 + z64 * P->rs2 * row; // output row
for (col = 0; col < P->ww2 * nc; col += nc) // output col
{
for (ch1 = 0; ch1 < nc; ch1++) // loop channels
{
rgb = pix1a[ch1] + pix1a[ch1+nc] + pix1b[ch1] + pix1b[ch1+nc]; // 4 input RGB[A] values
pix2[ch1] = rgb / 4; // --> 1 output RGB[A]
}
pix1a += 2 * nc;
pix1b += 2 * nc;
pix2 += nc;
}
}
return 0;
}
/********************************************************************************
Rescale PXB pixmap image to new width and height.
The scale ratios may be different for width and height.
Method is better than bilinear:
For all input pixels [p] having some overlap area (0-1) with an output pixel:
output pixel = sum over p ( overlap(p) * value(p) )
*********************************************************************************/
typedef struct {
uint8 *pixels1;
uint8 *pixels2;
int ww1, hh1, rs1;
int ww2, hh2, rs2;
int nc1, nc2;
int *px1L, *py1L;
float *pxmap, *pymap;
int maxmapx, maxmapy;
}
pxb_rescale_data_t;
PXB * PXB_rescale(PXB *pxb1, int ww, int hh)
{
void * pxb_rescale_thread(void *arg);
pxb_rescale_data_t P;
PXB *pxb2;
int px1, py1, px2, py2;
int pxl, pyl, pxm, pym, ii;
float scalex, scaley;
float px1a, py1a, px1b, py1b;
float fx, fy;
int Fhalf = 0;
if (ww <= pxb1->ww/2 && hh <= pxb1->hh/2) { // if scaling down to < 1/2 size,
pxb1 = PXB_half(pxb1); // use fast 1/2 scale down first
Fhalf = 1;
}
P.ww1 = pxb1->ww;
P.hh1 = pxb1->hh;
P.nc1 = pxb1->nc;
P.rs1 = pxb1->rs;
P.pixels1 = pxb1->pixels;
pxb2 = PXB_make(ww,hh,P.nc1); // output PXB
P.ww2 = ww; // output image data
P.hh2 = hh;
P.rs2 = pxb2->rs;
P.nc2 = P.nc1;
P.pixels2 = pxb2->pixels;
scalex = 1.0 * P.ww1 / P.ww2; // compute x and y scales
scaley = 1.0 * P.hh1 / P.hh2;
if (scalex <= 1) P.maxmapx = 2; // compute max input pixels
else P.maxmapx = scalex + 2; // mapping into output pixels
P.maxmapx += 1; // for both dimensions
if (scaley <= 1) P.maxmapy = 2; // (pixels may not be square)
else P.maxmapy = scaley + 2;
P.maxmapy += 1; // (extra entry for -1 flag)
P.pymap = (float *) zmalloc(P.hh2 * P.maxmapy * sizeof(float),"PXB"); // maps overlap of < maxmap input
P.pxmap = (float *) zmalloc(P.ww2 * P.maxmapx * sizeof(float),"PXB"); // pixels per output pixel
P.py1L = (int *) zmalloc(P.hh2 * sizeof(int),"PXB"); // maps first (lowest) input pixel
P.px1L = (int *) zmalloc(P.ww2 * sizeof(int),"PXB"); // per output pixel
for (py2 = 0; py2 < P.hh2; py2++) // loop output y-pixels
{
py1a = py2 * scaley; // corresponding input y-pixels
py1b = py1a + scaley;
if (py1b >= P.hh1) py1b = P.hh1 - 0.001; // fix precision limitation
pyl = py1a;
P.py1L[py2] = pyl; // 1st overlapping input pixel
for (py1 = pyl, pym = 0; py1 < py1b; py1++, pym++) // loop overlapping input pixels
{
if (py1 < py1a) { // compute amount of overlap
if (py1+1 < py1b) fy = py1+1 - py1a; // 0.0 to 1.0
else fy = scaley;
}
else if (py1+1 > py1b) fy = py1b - py1;
else fy = 1;
ii = py2 * P.maxmapy + pym; // save it
P.pymap[ii] = 0.9999 * fy / scaley;
}
ii = py2 * P.maxmapy + pym; // set an end marker after
P.pymap[ii] = -1; // last overlapping pixel
}
for (px2 = 0; px2 < P.ww2; px2++) // do same for x-pixels
{
px1a = px2 * scalex;
px1b = px1a + scalex;
if (px1b >= P.ww1) px1b = P.ww1 - 0.001;
pxl = px1a;
P.px1L[px2] = pxl;
for (px1 = pxl, pxm = 0; px1 < px1b; px1++, pxm++)
{
if (px1 < px1a) {
if (px1+1 < px1b) fx = px1+1 - px1a;
else fx = scalex;
}
else if (px1+1 > px1b) fx = px1b - px1;
else fx = 1;
ii = px2 * P.maxmapx + pxm;
P.pxmap[ii] = 0.9999 * fx / scalex;
}
ii = px2 * P.maxmapx + pxm;
P.pxmap[ii] = -1;
}
void *args[Xsmp][2];
pthread_t tid[Xsmp];
for (ii = 0; ii < NSMP; ii++) {
args[ii][0] = &P;
args[ii][1] = &Nval[ii];
tid[ii] = start_Jthread(pxb_rescale_thread,args[ii]);
}
for (ii = 0; ii < NSMP; ii++)
wait_Jthread(tid[ii]);
zfree(P.px1L);
zfree(P.py1L);
zfree(P.pxmap);
zfree(P.pymap);
if (Fhalf) PXB_free(pxb1);
return pxb2;
}
void * pxb_rescale_thread(void *arg) // worker thread function
{
int px1, py1, px2, py2;
int pxl, pyl, pxm, pym, ii;
uint8 *pix1, *pix2;
float fx, fy, ftot;
float red, green, blue, alpha;
int64 z64 = 1;
void ** args = (void **) arg;
pxb_rescale_data_t *P = (pxb_rescale_data_t *) args[0];
int index = * ((int *) args[1]);
for (py2 = index; py2 < P->hh2; py2 += NSMP) // loop output y-pixels
{
pyl = P->py1L[py2]; // corresp. 1st input y-pixel
for (px2 = 0; px2 < P->ww2; px2++) // loop output x-pixels
{
pxl = P->px1L[px2]; // corresp. 1st input x-pixel
red = green = blue = alpha = 0; // initz. output pixel
for (py1 = pyl, pym = 0; ; py1++, pym++) // loop overlapping input y-pixels
{
ii = py2 * P->maxmapy + pym; // get y-overlap
fy = P->pymap[ii];
if (fy < 0) break; // no more pixels
for (px1 = pxl, pxm = 0; ; px1++, pxm++) // loop overlapping input x-pixels
{
ii = px2 * P->maxmapx + pxm; // get x-overlap
fx = P->pxmap[ii];
if (fx < 0) break; // no more pixels
ftot = fx * fy; // area overlap = x * y overlap
pix1 = P->pixels1 + z64 * py1 * P->rs1 + px1 * P->nc1;
red += pix1[0] * ftot; // add input pixel * overlap
green += pix1[1] * ftot;
blue += pix1[2] * ftot;
if (P->nc1 == 4) alpha += pix1[3] * ftot;
}
pix2 = P->pixels2 + z64 * py2 * P->rs2 + px2 * P->nc2; // save output pixel
pix2[0] = red;
pix2[1] = green;
pix2[2] = blue;
if (P->nc2 == 4) pix2[3] = alpha;
}
}
}
return 0;
}
/********************************************************************************/
// fast PXB pixmap rescale using 'half binomial' interpolation
// works best when the rescale ratio is between 0.5 and 2.0
typedef struct {
int ww1, hh1, ww2, hh2, rs1, rs2, nc1, nc2;
uint8 *pixels1, *pixels2;
float xscale, yscale;
}
pxb_rescale_fast_data_t;
PXB * PXB_rescale_fast(PXB *pxb1, int ww, int hh)
{
void * pxb_rescale_fast_thread(void *arg);
pxb_rescale_fast_data_t P;
PXB *pxb2;
P.ww1 = pxb1->ww; // input image data
P.hh1 = pxb1->hh;
P.rs1 = pxb1->rs;
P.nc1 = pxb1->nc;
P.pixels1 = pxb1->pixels;
P.ww2 = ww; // output image data
P.hh2 = hh;
P.nc2 = P.nc1;
pxb2 = PXB_make(P.ww2,P.hh2,P.nc2);
P.rs2 = pxb2->rs;
P.pixels2 = pxb2->pixels;
P.xscale = 1.0 * (P.ww1-1) / P.ww2;
P.yscale = 1.0 * (P.hh1-1) / P.hh2;
void *args[Xsmp][2];
pthread_t tid[Xsmp];
for (int ii = 0; ii < NSMP; ii++) { // start working threads
args[ii][0] = &P;
args[ii][1] = &Nval[ii];
tid[ii] = start_Jthread(pxb_rescale_fast_thread,args[ii]);
}
for (int ii = 0; ii < NSMP; ii++) // wait for all done
wait_Jthread(tid[ii]);
return pxb2;
}
void * pxb_rescale_fast_thread(void *arg)
{
int px1, py1, px2, py2;
uint8 *pix10, *pix11, *pix2;
int64 z64 = 1;
void ** args = (void **) arg;
int index = * ((int *) args[1]);
// set_cpu_affinity(index); // stay on same cpu if possible
pxb_rescale_fast_data_t *P = (pxb_rescale_fast_data_t *) args[0];
for (py2 = index; py2 < P->hh2; py2 += NSMP) // loop output pixels
for (px2 = 0; px2 < P->ww2; px2++)
{
px1 = px2 * P->xscale; // input pixel corresponding
py1 = py2 * P->yscale; // to output pixel
pix10 = P->pixels1 + z64 * py1 * P->rs1 + px1 * P->nc1; // input pixel RGB data
pix11 = pix10 + P->nc1; // next pixel right
pix2 = P->pixels2 + z64 * py2 * P->rs2 + px2 * P->nc2; // output pixel
pix2[0] = (pix10[0] + pix11[0]) / 2;
pix2[1] = (pix10[1] + pix11[1]) / 2;
pix2[2] = (pix10[2] + pix11[2]) / 2;
}
return 0;
}
/********************************************************************************/
// rescale PXB to given max. width or height, preserving aspect ratio
// same as PXB_rescale() but one size argument instead of width and height
PXB * PXB_rescale(PXB *pxb1, int size)
{
int ww1, hh1, ww2, hh2;
PXB *pxb2;
ww1 = pxb1->ww;
hh1 = pxb1->hh;
if (ww1 >= hh1) {
ww2 = size;
hh2 = 1.0 * size * hh1 / ww1 + 0.5;
}
else {
hh2 = size;
ww2 = 1.0 * size * ww1 / hh1 + 0.5;
}
if (ww2 < 1 || hh2 < 1) { // happens - invalid jpeg file
Plog(0,"PXB rescale failed: %d %d \n",ww2,hh2);
return 0;
}
pxb2 = PXB_rescale(pxb1,ww2,hh2);
return pxb2;
}
/********************************************************************************/
// fast rescale PXB to given max. width or height, preserving aspect ratio
// same as PXB_rescale_fast() but one size argument instead of width and height
PXB * PXB_rescale_fast(PXB *pxb1, int size)
{
int ww1, hh1, ww2, hh2;
PXB *pxb2;
ww1 = pxb1->ww;
hh1 = pxb1->hh;
if (ww1 >= hh1) {
ww2 = size;
hh2 = 1.0 * size * hh1 / ww1 + 0.5;
}
else {
hh2 = size;
ww2 = 1.0 * size * ww1 / hh1 + 0.5;
}
pxb2 = PXB_rescale_fast(pxb1,ww2,hh2);
return pxb2;
}
/********************************************************************************
PXB * PXB_rotate(PXB *pxb, float angle)
Rotate PXB pixmap image through any angle (degrees).
The returned pixmap has the same size as the original, but the
pixmap envelope is increased to accommodate the rotated original
(e.g. a 100x100 pixmap rotated 45 deg. needs a 142x142 pixmap).
Pixels added around the rotated pixmap have RGB[A] = 0.
Angle is in degrees. Positive direction is clockwise.
PXB must have 3 or 4 channels (RGB or RGBA).
Loss of resolution is about 1/2 pixel.
Algorithm:
create output PXB big enough for rotated input PXB
compute coefficients for affine transform
loop all output pixels (px2,py2)
get corresp. input pixel (px1,py1) using affine transform
if outside of PXB pixmap
output pixel = black
continue
for 4 input pixels based at (px0,py0) = (int(px1),int(py1))
compute overlap (0 to 1) with (px1,py1)
sum RGB values * overlap
output aggregate RGB to pixel (px2,py2)
*********************************************************************************/
PXB * PXB_rotate(PXB *pxb1, float angle)
{
PXB *pxb2;
int nc, ac;
int ww1, hh1, rs1, ww2, hh2, rs2;
int px2, py2, px0, py0;
float px1, py1;
float f0, f1, f2, f3, red, green, blue, alpha = 0;
float a, b, d, e, ww15, hh15, ww25, hh25;
uch *ppix1, *ppix2, *pix0, *pix1, *pix2, *pix3;
int64 z64 = 1;
ww1 = pxb1->ww;
hh1 = pxb1->hh;
nc = pxb1->nc;
ac = 0;
if (nc == 4) ac = 1; // has alpha channel
rs1 = ww1 * nc;
while (angle < -180) angle += 360; // normalize, -180 to +180
while (angle > 180) angle -= 360;
angle = angle * PI / 180; // radians, -PI to +PI
if (fabsf(angle) < 0.001) {
pxb2 = PXB_copy(pxb1); // angle is zero within my precision
return pxb2;
}
ww2 = int(ww1*fabsf(cosf(angle)) + hh1*fabsf(sinf(angle))); // rectangle containing rotated pixmap
hh2 = int(ww1*fabsf(sinf(angle)) + hh1*fabsf(cosf(angle)));
pxb2 = PXB_make(ww2,hh2,nc); // create output pxb2
rs2 = ww2 * nc;
ppix1 = pxb1->pixels;
ppix2 = pxb2->pixels;
ww15 = 0.5 * ww1;
hh15 = 0.5 * hh1;
ww25 = 0.5 * ww2;
hh25 = 0.5 * hh2;
a = cosf(angle); // affine transform coefficients
b = sinf(angle);
d = - sinf(angle);
e = cosf(angle);
for (py2 = 0; py2 < hh2; py2++) // loop through output pixels
for (px2 = 0; px2 < ww2; px2++)
{
px1 = a * (px2 - ww25) + b * (py2 - hh25) + ww15; // (px1,py1) = corresponding
py1 = d * (px2 - ww25) + e * (py2 - hh25) + hh15; // point within input pixels
px0 = int(px1); // pixel containing (px1,py1)
py0 = int(py1);
if (px0 < 0 || px0 >= ww1-1 || py0 < 0 || py0 >= hh1-1) // if outside input pixel array,
continue; // leave black
pix0 = ppix1 + z64 * py0 * rs1 + px0 * nc; // 4 input pixels based at (px0,py0)
pix1 = pix0 + rs1;
pix2 = pix0 + nc;
pix3 = pix1 + nc;
f0 = (px0+1 - px1) * (py0+1 - py1); // overlap of (px1,py1)
f1 = (px0+1 - px1) * (py1 - py0); // in each of the 4 pixels
f2 = (px1 - px0) * (py0+1 - py1);
f3 = (px1 - px0) * (py1 - py0);
red = f0 * pix0[0] + f1 * pix1[0] + f2 * pix2[0] + f3 * pix3[0]; // sum the weighted inputs
green = f0 * pix0[1] + f1 * pix1[1] + f2 * pix2[1] + f3 * pix3[1];
blue = f0 * pix0[2] + f1 * pix1[2] + f2 * pix2[2] + f3 * pix3[2];
if (ac) alpha = f0 * pix0[3] + f1 * pix1[3] + f2 * pix2[3] + f3 * pix3[3];
pix2 = ppix2 + z64 * py2 * rs2 + px2 * nc; // output pixel
pix2[0] = int(red);
pix2[1] = int(green);
pix2[2] = int(blue);
if (ac) pix2[3] = int(alpha);
}
return pxb2;
}
/********************************************************************************/
// Get a virtual pixel at location (px,py) (real) in a PXB pixmap.
// Get the overlapping integer pixels and build a composite.
// Output vpix is uint8[4] supplied by caller.
// Returns 1 if OK, 0 if px/py out of limits for pxm.
int vpixel(PXB *pxb, float px, float py, uint8 *vpix)
{
int ww, hh, rs, nc, ac, px0, py0;
uint8 *pix0, *pix1, *pix2, *pix3;
float f0, f1, f2, f3;
ww = pxb->ww;
hh = pxb->hh;
rs = pxb->rs;
nc = pxb->nc;
ac = nc - 3;
px0 = px; // integer pixel containing (px,py)
py0 = py;
if (px0 < 0 || py0 < 0) return 0;
if (px0 > ww-2 || py0 > hh-2) return 0;
f0 = (px0+1 - px) * (py0+1 - py); // overlap of (px,py)
f1 = (px0+1 - px) * (py - py0); // in each of the 4 pixels
f2 = (px - px0) * (py0+1 - py);
f3 = (px - px0) * (py - py0);
pix0 = PXBpix(pxb,px0,py0); // pixel (px0,py0)
pix1 = pix0 + rs; // (px0,py0+1)
pix2 = pix0 + nc; // (px0+1,py0)
pix3 = pix1 + nc; // (px0+1,py0+1)
vpix[0] = f0 * pix0[0] + f1 * pix1[0] + f2 * pix2[0] + f3 * pix3[0]; // sum the weighted inputs
vpix[1] = f0 * pix0[1] + f1 * pix1[1] + f2 * pix2[1] + f3 * pix3[1];
vpix[2] = f0 * pix0[2] + f1 * pix1[2] + f2 * pix2[2] + f3 * pix3[2];
if (ac) vpix[3] = f0 * pix0[3] + f1 * pix1[3] + f2 * pix2[3] + f3 * pix3[3];
return 1;
}
// Get a virtual pixel at location (px,py) (real) in a PXM pixmap.
// Get the overlapping float pixels and build a composite.
// Output vpix is float[4] supplied by caller.
// Returns 1 if OK, 0 if px/py out of limits for pxm.
int vpixel(PXM *pxm, float px, float py, float *vpix)
{
int ww, hh, nc, ac, px0, py0;
float *pix0, *pix1, *pix2, *pix3;
float f0, f1, f2, f3;
ww = pxm->ww;
hh = pxm->hh;
nc = pxm->nc;
ac = nc - 3;
px0 = px; // integer pixel containing (px,py)
py0 = py;
if (px0 < 1 || py0 < 1) return 0; // off-image or edge pixel
if (px0 > ww-2 || py0 > hh-2) return 0;
f0 = (px0+1 - px) * (py0+1 - py); // overlap of (px,py)
f1 = (px0+1 - px) * (py - py0); // in each of the 4 pixels
f2 = (px - px0) * (py0+1 - py);
f3 = (px - px0) * (py - py0);
pix0 = PXMpix(pxm,px0,py0); // pixel (px0,py0)
pix1 = pix0 + ww * nc; // (px0,py0+1)
pix2 = pix0 + nc; // (px0+1,py0)
pix3 = pix1 + nc; // (px0+1,py0+1)
vpix[0] = f0 * pix0[0] + f1 * pix1[0] + f2 * pix2[0] + f3 * pix3[0]; // sum the weighted inputs
vpix[1] = f0 * pix0[1] + f1 * pix1[1] + f2 * pix2[1] + f3 * pix3[1];
vpix[2] = f0 * pix0[2] + f1 * pix1[2] + f2 * pix2[2] + f3 * pix3[2];
if (ac) // alpha channel
vpix[3] = f0 * pix0[3] + f1 * pix1[3] + f2 * pix2[3] + f3 * pix3[3];
return 1;
}
/********************************************************************************/
// Copy a PXM pixmap image (RGB float) to a PXB pixmap image (RGB uint8).
// NOT THREAD SAFE
namespace PXM_PXB_copy_names
{
PXM *pxm1;
PXB *pxb2;
int ww, hh, nc;
}
PXB * PXM_PXB_copy(PXM *pxm)
{
using namespace PXM_PXB_copy_names;
void * PXM_PXB_copy_thread(void *);
ww = pxm->ww;
hh = pxm->hh;
nc = pxm->nc;
pxm1 = pxm;
pxb2 = PXB_make(ww,hh,nc);
do_wthreads(PXM_PXB_copy_thread,NSMP);
return pxb2;
}
void * PXM_PXB_copy_thread(void *arg) // speedup with threads
{
using namespace PXM_PXB_copy_names;
int index = *((int *) arg);
int px, py;
float *pix1;
uint8 *pix2;
for (py = index; py < hh; py += NSMP)
{
pix1 = PXMpix(pxm1,0,py);
pix2 = PXBpix(pxb2,0,py);
for (px = 0; px < ww; px++)
{
pix2[0] = pix1[0];
pix2[1] = pix1[1];
pix2[2] = pix1[2];
if (nc == 4) pix2[3] = pix1[3];
pix1 += nc;
pix2 += nc;
}
}
return 0;
}
// Update a PXB section from an updated PXM section.
// PXM and PXB must have the same dimensions.
// px3, py3, ww3, hh3: modified section within pxm1 to propagate to pxb2;
void PXM_PXB_update(PXM *pxm1, PXB *pxb2, int px3, int py3, int ww3, int hh3)
{
float *pix1;
uint8 *pix2;
int px, py;
int lox, hix, loy, hiy;
int nc1, nc2, ac;
if (pxm1->ww + pxm1->hh != pxb2->ww + pxb2->hh) {
Plog(0,"PXM_PXB_update() call error %d %d %d %d \n",
pxm1->ww, pxm1->hh, pxb2->ww, pxb2->hh);
quitxx();
}
lox = px3;
hix = px3 + ww3;
if (lox < 0) lox = 0;
if (hix > pxb2->ww) hix = pxb2->ww;
loy = py3;
hiy = py3 + hh3;
if (loy < 0) loy = 0;
if (hiy > pxb2->hh) hiy = pxb2->hh;
nc1 = pxm1->nc;
nc2 = pxb2->nc;
if (nc1 > 3 && nc2 > 3) ac = 1; // alpha channel
else ac = 0;
for (py = loy; py < hiy; py++)
{
pix1 = PXMpix(pxm1,lox,py);
pix2 = PXBpix(pxb2,lox,py);
for (px = lox; px < hix; px++)
{
pix2[0] = pix1[0]; // 0.0 - 255.99 >> 0 - 255
pix2[1] = pix1[1];
pix2[2] = pix1[2];
if (ac) pix2[3] = pix1[3];
pix1 += nc1;
pix2 += nc2;
}
}
return;
}
// Update an output PXB section from a corresponding input PXB section.
// The two PXBs represent the same image at different scales (width/height).
// Input pxb1 must be larger or equal to output pxb2.
// px3, py3, ww3, hh3: modified section within pxb1 to propagate to pxb2;
void PXB_PXB_update(PXB *pxb1, PXB *pxb2, int px3, int py3, int ww3, int hh3)
{
static int pww1 = 0, phh1 = 0, pww2 = 0, phh2 = 0;
static int *px1L = 0, *py1L = 0;
static float scalex = 1, scaley = 1;
static float *pxmap = 0, *pymap = 0;
static int maxmapx = 0, maxmapy = 0;
uint8 *ppix1, *ppix2;
int ww1, hh1, ww2, hh2, rs1, rs2, nc1, nc2, ac;
int px1, py1, px2, py2;
int pxl, pyl, pxm, pym, ii;
float px1a, py1a, px1b, py1b;
float fx, fy, ftot;
uint8 *pixel1, *pixel2;
float red, green, blue, alpha;
int lox, hix, loy, hiy;
int64 z64 = 1;
ww1 = pxb1->ww;
hh1 = pxb1->hh;
rs1 = pxb1->rs;
nc1 = pxb1->nc;
ppix1 = pxb1->pixels;
ww2 = pxb2->ww;
hh2 = pxb2->hh;
rs2 = pxb2->rs;
nc2 = pxb2->nc;
ppix2 = pxb2->pixels;
if (nc1 > 3 && nc2 > 3) ac = 1; // alpha channel
else ac = 0;
if (ww1 == pww1 && hh1 == phh1 && ww2 == pww2 && hh2 == phh2) // if the sizes are the same as before,
goto copy_pixels; // the pixel mapping math can be avoided.
pww1 = ww1;
phh1 = hh1;
pww2 = ww2;
phh2 = hh2;
if (px1L) { // unless this is the first call,
zfree(px1L); // free prior map memory
zfree(py1L);
zfree(pxmap);
zfree(pymap);
}
scalex = 1.0 * ww1 / ww2; // compute x and y scales
scaley = 1.0 * hh1 / hh2;
if (scalex <= 1) maxmapx = 2; // compute max input pixels
else maxmapx = scalex + 2; // mapping into output pixels
maxmapx += 1; // for both dimensions
if (scaley <= 1) maxmapy = 2; // (pixels may not be square)
else maxmapy = scaley + 2;
maxmapy += 1; // (extra entry for -1 flag)
pymap = (float *) zmalloc(hh2 * maxmapy * sizeof(float),"PXB_PXB_upd"); // maps overlap of < maxmap input
pxmap = (float *) zmalloc(ww2 * maxmapx * sizeof(float),"PXB_PXB_upd"); // pixels per output pixel
py1L = (int *) zmalloc(hh2 * sizeof(int),"PXB_PXB_upd"); // maps first (lowest) input pixel
px1L = (int *) zmalloc(ww2 * sizeof(int),"PXB_PXB_upd"); // per output pixel
for (py2 = 0; py2 < hh2; py2++) // loop output y-pixels
{
py1a = py2 * scaley; // corresponding input y-pixels
py1b = py1a + scaley;
if (py1b >= hh1) py1b = hh1 - 0.001; // fix precision limitation
pyl = py1a;
py1L[py2] = pyl; // 1st overlapping input pixel
for (py1 = pyl, pym = 0; py1 < py1b; py1++, pym++) // loop overlapping input pixels
{
if (py1 < py1a) { // compute amount of overlap
if (py1+1 < py1b) fy = py1+1 - py1a; // 0.0 to 1.0
else fy = scaley;
}
else if (py1+1 > py1b) fy = py1b - py1;
else fy = 1;
ii = py2 * maxmapy + pym; // save it
pymap[ii] = 0.9999 * fy / scaley;
}
ii = py2 * maxmapy + pym; // set an end marker after
pymap[ii] = -1; // last overlapping pixel
}
for (px2 = 0; px2 < ww2; px2++) // do same for x-pixels
{
px1a = px2 * scalex;
px1b = px1a + scalex;
if (px1b >= ww1) px1b = ww1 - 0.001;
pxl = px1a;
px1L[px2] = pxl;
for (px1 = pxl, pxm = 0; px1 < px1b; px1++, pxm++)
{
if (px1 < px1a) {
if (px1+1 < px1b) fx = px1+1 - px1a;
else fx = scalex;
}
else if (px1+1 > px1b) fx = px1b - px1;
else fx = 1;
ii = px2 * maxmapx + pxm;
pxmap[ii] = 0.9999 * fx / scalex;
}
ii = px2 * maxmapx + pxm;
pxmap[ii] = -1;
}
copy_pixels:
px3 = px3 / scalex; // convert input area to output area
py3 = py3 / scaley;
ww3 = ww3 / scalex + 2;
hh3 = hh3 / scaley + 2;
lox = px3;
hix = px3 + ww3;
if (lox < 0) lox = 0;
if (hix > ww2) hix = ww2;
loy = py3;
hiy = py3 + hh3;
if (loy < 0) loy = 0;
if (hiy > hh2) hiy = hh2;
for (py2 = loy; py2 < hiy; py2++) // loop output y-pixels
{
pyl = py1L[py2]; // corresp. 1st input y-pixel
for (px2 = lox; px2 < hix; px2++) // loop output x-pixels
{
pxl = px1L[px2]; // corresp. 1st input x-pixel
red = green = blue = alpha = 0; // initz. output pixel
for (py1 = pyl, pym = 0; ; py1++, pym++) // loop overlapping input y-pixels
{
ii = py2 * maxmapy + pym; // get y-overlap
fy = pymap[ii];
if (fy < 0) break; // no more pixels
for (px1 = pxl, pxm = 0; ; px1++, pxm++) // loop overlapping input x-pixels
{
ii = px2 * maxmapx + pxm; // get x-overlap
fx = pxmap[ii];
if (fx < 0) break; // no more pixels
ftot = fx * fy; // area overlap = x * y overlap
pixel1 = ppix1 + z64 * py1 * rs1 + px1 * nc1;
red += pixel1[0] * ftot; // add input pixel * overlap
green += pixel1[1] * ftot;
blue += pixel1[2] * ftot;
if (ac) alpha += pixel1[3] * ftot;
}
}
pixel2 = ppix2 + z64 * py2 * rs2 + px2 * nc2; // save output pixel
pixel2[0] = red; // 0.0 - 255.996 >> 0 - 255
pixel2[1] = green;
pixel2[2] = blue;
if (ac) pixel2[3] = alpha;
}
}
return;
}
/********************************************************************************
primary image file read and write functions
PXB pixmap <--> file 8-bit RGB[A]
PXM pixmap <--> file float RGB[A]
*********************************************************************************/
// Load an image file into a PXB pixmap (8 bit color).
// Also sets the following file scope variables:
// f_load_type = "jpg" "tif" "png" "RAW" "video" "other"
// f_load_bpc = disk file bits per color = 8/16
// f_load_size = disk file size
// If Fack is true, failure will cause a popup ACK dialog
// Do not call from a thread with Fack true.
PXB * PXB_load(ch *file, int Fack)
{
int err;
FTYPE ftype;
ch *file2 = 0;
ch framefile[200];
ch pngfile[XFCC];
ch *pext = 0;
PXB *pxb = 0;
STATB statB;
pthread_t tid;
ch *key = "bitspersample";
ch *kdata[1];
Funcbusy(+1,1);
static int Vftf = 1;
ch *ffmpegmess = "video files not supported (install ffmpeg)";
if (! regfile(file,&statB)) {
Plog(0,"%s %s \n","file not found",file);
goto errret;
}
f_load_size = statB.st_size;
f_load_bpc = 8; // default
ftype = image_file_type(file);
if (ftype != IMAGE && ftype != RAW && ftype != VIDEO) {
Plog(0,"file type not supported: %s \n",file);
goto errret;
}
pext = strrchr(file,'.'); // file .ext
if (! pext) pext = "";
if (ftype == VIDEO) // video file - extract frame 1
{
if (! Ffmpeg) {
if (Vftf) { Vftf = 0; zmessageACK(Mwin,ffmpegmess); }
goto errret;
}
tid = pthread_self();
snprintf(framefile,200,"%s/framefile-%ld.jpg",temp_folder,tid); // unique frame file per thread
// (index function, thumbnail creation)
file2 = zescape_quotes(file); // escape embedded quotes
err = zshell(0,"ffmpeg -ss 1 -i \"%s\" -v 8 -frames 1 -y %s", // ffmpeg command to get frame file
file2, framefile);
zfree(file2);
if (err && err != 69) { // srmount error, ignore
Plog(0,"ffmpeg cannot get video frame: %s %s \n",file,strerror(err));
goto errret;
}
pxb = ANY_PXB_load(framefile);
if (! pxb) goto errret;
remove(framefile);
strcpy(f_load_type,"video");
f_load_bpc_raw = 0;
}
if (ftype == IMAGE)
{
if (strcasestr(".jpg .jpeg",pext)) {
pxb = JPG_PXB_load(file);
if (pxb) strcpy(f_load_type,"jpg");
else {
pxb = PNG_PXB_load(file); // unreadable .jpg file 24.30
if (pxb) { // check if really a .png file
Plog(0,"file: %s is a PNG file \n",file);
strcpy(pngfile,file);
strcat(pngfile,".png");
zshell("log","mv -T \"%s\" \"%s\" ",file,pngfile);
pxb = PXB_load(pngfile,Fack);
Funcbusy(-1);
return pxb;
}
}
}
else if (strcasestr(".tif .tiff",pext)) {
pxb = TIFF_PXB_load(file);
strcpy(f_load_type,"tif");
}
else if (strcasestr(".png",pext)) {
pxb = PNG_PXB_load(file);
strcpy(f_load_type,"png");
}
else if (strcasestr(".heic",pext)) { // heif-gdk-pixbuf in U22.04
pxb = HEIC_PXB_load(file);
strcpy(f_load_type,"heic");
}
else if (strcasestr(".avif",pext)) { // libavif-gdk-pixbuf in U22.04
pxb = HEIC_PXB_load(file);
strcpy(f_load_type,"avif");
}
else if (strcasestr(".jp2",pext)) {
pxb = JP2_PXB_load(file);
strcpy(f_load_type,"jp2");
}
else if (strcasestr(".webp",pext)) { // webp-pixbuf-loader in U22.04
pxb = WEBP_PXB_load(file);
strcpy(f_load_type,"webp");
}
else {
pxb = ANY_PXB_load(file);
strcpy(f_load_type,"other");
}
f_load_bpc_raw = 0;
}
if (ftype == RAW)
{
pxb = RAW_PXB_load(file,Fraw_match_embed);
strcpy(f_load_type,"RAW");
err = meta_get1(file,(ch **) &key,kdata,1);
if (! err && *kdata) {
f_load_bpc_raw = atoi(*kdata);
zfree(*kdata);
}
}
if (pxb) {
Funcbusy(-1);
return pxb;
}
errret:
strcpy(f_load_type,"none");
f_load_bpc = f_load_size = 0;
if (Fack) zmessageACK(Mwin,"file error: %s (see log file)",file);
Funcbusy(-1);
return 0;
}
/********************************************************************************/
// Load an image file into a PXM pixmap (float color).
// Also sets the following file scope variables:
// f_load_type = "jpg" "tif" "png" "RAW" "other"
// f_load_bpc = disk file bits per color = 8/16
// f_load_size = disk file size
// If Fack is true, failure will cause a popup ACK dialog
// Do not call from a thread with Fack true.
PXM * PXM_load(ch *file, int Fack)
{
int err;
FTYPE ftype;
ch *pext;
PXM *pxm = 0;
STATB statB;
ch *key = "bitspersample";
ch *kdata[1];
Funcbusy(+1,1);
if (! regfile(file,&statB)) {
Plog(0,"%s %s \n","file not found",file);
goto errret;
}
f_load_size = statB.st_size;
f_load_bpc = 8; // default
ftype = image_file_type(file);
if (ftype != IMAGE && ftype != RAW) {
Plog(0,"file type not supported: %s \n",file);
goto errret;
}
pext = strrchr(file,'.'); // file .ext
if (! pext) pext = "";
if (ftype == IMAGE)
{
if (strcasestr(".jpg .jpeg",pext)) {
pxm = JPG_PXM_load(file);
strcpy(f_load_type,"jpg");
}
else if (strcasestr(".tif .tiff",pext)) {
pxm = TIFF_PXM_load(file);
strcpy(f_load_type,"tif");
}
else if (strcasestr(".png",pext)) {
pxm = PNG_PXM_load(file);
strcpy(f_load_type,"png");
}
else if (strcasestr(".heic",pext)) {
pxm = HEIC_PXM_load(file);
strcpy(f_load_type,"heic");
}
else if (strcasestr(".avif",pext)) {
pxm = HEIC_PXM_load(file);
strcpy(f_load_type,"avif");
}
else if (strcasestr(".jp2",pext)) {
pxm = JP2_PXM_load(file);
strcpy(f_load_type,"jp2");
}
else if (strcasestr(".webp",pext)) {
pxm = WEBP_PXM_load(file);
strcpy(f_load_type,"webp");
}
else {
pxm = ANY_PXM_load(file);
strcpy(f_load_type,"other");
}
f_load_bpc_raw = 0;
}
if (ftype == RAW)
{
pxm = RAW_PXM_load(file,Fraw_match_embed);
strcpy(f_load_type,"RAW");
err = meta_get1(file,(ch **) &key,kdata,1);
if (! err && *kdata) {
f_load_bpc_raw = atoi(*kdata);
zfree(*kdata);
}
}
if (pxm) {
Funcbusy(-1);
return pxm;
}
errret:
strcpy(f_load_type,"none");
f_load_bpc = f_load_size = 0;
if (Fack) zmessageACK(Mwin,"file error: %s (see log file)",file);
Funcbusy(-1);
return 0;
}
/********************************************************************************/
// Save a PXB pixmap to a specified file, bits/color, and quality level.
// If output file is JPEG, bpc is 8 and quality is used for compression level.
// If output file is TIFF or PNG, bpc may be 8 or 16 and quality is ignored.
// If Fack is true, failure will cause a popup ACK dialog.
// Return 0 if OK, +N if error
int PXB_save(PXB *pxb, ch *file, int bpc, int quality, int Fack)
{
int err;
ch *pext;
Funcbusy(+1,1);
pext = strrchr(file,'/');
if (! pext) pext = file;
pext = strrchr(pext,'.');
if (! pext) pext = "";
if (strcasestr(".tif .tiff",pext))
err = PXB_TIFF_save(pxb,file,bpc);
else if (strcasestr(".png",pext))
err = PXB_PNG_save(pxb,file,bpc);
else if (strcasestr(".heic",pext))
err = PXB_HEIC_save(pxb,file);
else if (strcasestr(".avif",pext))
err = PXB_HEIC_save(pxb,file);
else if (strcasestr(".jp2",pext))
err = PXB_JP2_save(pxb,file);
else if (strcasestr(".webp",pext))
err = PXB_WEBP_save(pxb,file);
else err = PXB_JPG_save(pxb,file,quality);
if (! err) {
Funcbusy(-1);
return 0;
}
if (Fack) zmessageACK(Mwin,"file error: %s (see log file)",file);
Funcbusy(-1);
return err;
}
/********************************************************************************/
// Save a PXM pixmap to a specified file, bits/color, and quality level.
// If output file is JPEG, bpc is 8 and quality is used for compression level.
// If output file is TIFF or PNG, bpc may be 8 or 16 and quality is ignored.
// If Fack is true, failure will cause a popup ACK dialog.
// Return 0 if OK, +N if error
int PXM_save(PXM *pxm, ch *file, int bpc, int quality, int Fack)
{
int err;
ch *pext;
Funcbusy(+1,1);
pext = strrchr(file,'/');
if (! pext) pext = file;
pext = strrchr(pext,'.');
if (! pext) pext = "";
if (strcasestr(".tif .tiff",pext))
err = PXM_TIFF_save(pxm,file,bpc);
else if (strcasestr(".png",pext))
err = PXM_PNG_save(pxm,file,bpc);
else if (strcasestr(".heic",pext))
err = PXM_HEIC_save(pxm,file);
else if (strcasestr(".avif",pext))
err = PXM_HEIC_save(pxm,file);
else if (strcasestr(".jp2",pext))
err = PXM_JP2_save(pxm,file);
else if (strcasestr(".webp",pext))
err = PXM_WEBP_save(pxm,file);
else err = PXM_JPG_save(pxm,file,quality);
if (! err) {
Funcbusy(-1);
return 0;
}
if (Fack) zmessageACK(Mwin,"file error: %s (see log file)",file);
Funcbusy(-1);
return err;
}
/********************************************************************************
JPG file read and write functions
*********************************************************************************/
#pragma GCC push_options // stop GCC from removing necessary code
#pragma GCC optimize ("O1") // (all calls to jpeg_xxxx functions)
// JPEG error handler function
typedef struct libjpeg_error_mgr *libjpeg_error_ptr;
struct libjpeg_error_mgr {
struct jpeg_error_mgr pub;
jmp_buf setjmp_buffer;
};
void libjpeg_error_exit (j_common_ptr cinfo)
{
libjpeg_error_ptr errptr = (libjpeg_error_ptr) cinfo->err;
// (*cinfo->err->output_message) (cinfo); // suppress secondary message
longjmp(errptr->setjmp_buffer, 1);
}
// Load PXB pixmap from JPG file using JPG library.
// set size = 0 to load a full image size
// set size = N to load an image with width and height <= size
PXB * JPG_PXB_load(ch *file, int size)
{
struct jpeg_decompress_struct cinfo;
struct libjpeg_error_mgr jerr;
FILE *fid = 0;
uint8 **row_pointers = 0;
uint8 *pixels = 0;
uint8 *buffer;
int ww, hh, num, nc1, nc2, row, nb;
uint cc;
int64 cc64;
PXB *pxb = 0, *pxb2 = 0;
if (! regfile(file)) goto errret; // no print error
fid = fopen(file,"r");
if (! fid) goto errret;
cinfo.err = jpeg_std_error(&jerr.pub); // replace standard error handler
jerr.pub.error_exit = libjpeg_error_exit; // which exits on error
if (setjmp(jerr.setjmp_buffer)) {
jpeg_destroy_decompress(&cinfo);
goto errret;
}
jpeg_create_decompress(&cinfo); // initz. for read
jpeg_stdio_src(&cinfo,fid);
jpeg_read_header(&cinfo,TRUE);
ww = cinfo.image_width; // image size
hh = cinfo.image_height;
if (size) // used for thumbnails
{
if (ww >= hh) { // calculate num for actual size
num = 16 * size / ww; // minimally >= target size
if (ww * num / 16 < size) num++;
}
else {
num = 16 * size / hh;
if (hh * num / 16 < size) num++;
}
if (num == 0) num = 1;
if (num < 16) {
cinfo.scale_num = num; // target size = num/16 * full size
cinfo.scale_denom = 16;
}
}
jpeg_start_decompress(&cinfo);
nb = cinfo.rec_outbuf_height; // rows per read
nc1 = cinfo.output_components; // color channels
if (nc1 != 3) Plog(2,"JPEG file has %d channels: %s \n",nc1,file); // try anyway
ww = cinfo.output_width; // actual output dimensions
hh = cinfo.output_height;
cc64 = imagesize(ww,hh,nc1,1);
pixels = (uint8 *) zmalloc(cc64,"JPG_PXB_load"); // allocate memory for RGB image
cc = hh * sizeof(void *); // allocate row pointers
row_pointers = (uint8 **) zmalloc(cc,"JPG_PXB_load");
for (row = 0; row < hh; row++) // initz. row pointers
row_pointers[row] = pixels + (uint)(row) * ww * nc1; // GCC bug 2018, now fixed
while (cinfo.output_scanline < cinfo.output_height) { // read rows
row = cinfo.output_scanline;
buffer = row_pointers[row];
jpeg_read_scanlines(&cinfo,&buffer,nb);
}
jpeg_finish_decompress(&cinfo); // wrap-up
jpeg_destroy_decompress(&cinfo);
fclose(fid);
zfree(row_pointers);
row_pointers = 0;
nc2 = 3; // RGB output even if monocolor input
pxb = PXB_make(ww,hh,nc2); // output PXB, no alpha channel
pixelvert(pixels,pxb->pixels,ww,hh,nc1,nc2); // convert pixel data
zfree(pixels);
pixels = 0;
if (size) // make final exact size
{
if (ww >= hh) {
hh = hh * size / ww;
ww = size;
}
else {
ww = ww * size / hh;
hh = size;
}
pxb2 = PXB_rescale(pxb,ww,hh);
PXB_free(pxb);
pxb = pxb2;
}
f_load_bpc = 8;
return pxb;
errret:
if (errno) Plog(0,"error: %s \n",strerror(errno));
Plog(0,"jpeg file error: %s \n",file);
if (fid && fileno(fid) != -1) fclose(fid);
if (row_pointers) zfree(row_pointers);
if (pixels) zfree(pixels);
return 0;
}
// Load PXM pixmap from JPG file using JPG library.
PXM * JPG_PXM_load(ch *file)
{
struct jpeg_decompress_struct cinfo;
struct libjpeg_error_mgr jerr;
FILE *fid = 0;
uint8 **row_pointers = 0;
uint8 *pixels = 0;
uint8 *buffer;
int ww, hh, nc1, nc2, row, nb;
uint cc;
int64 cc64;
PXM *pxm = 0;
if (! regfile(file)) goto errret; // no print error
fid = fopen(file,"r");
if (! fid) goto errret;
cinfo.err = jpeg_std_error(&jerr.pub); // replace standard error handler
jerr.pub.error_exit = libjpeg_error_exit; // which exits on error
if (setjmp(jerr.setjmp_buffer)) {
jpeg_destroy_decompress(&cinfo);
goto errret;
}
jpeg_create_decompress(&cinfo); // initz. for read
jpeg_stdio_src(&cinfo,fid);
jpeg_read_header(&cinfo,TRUE);
ww = cinfo.image_width; // image size
hh = cinfo.image_height;
jpeg_start_decompress(&cinfo);
nb = cinfo.rec_outbuf_height; // rows per read
nc1 = cinfo.output_components; // color channels
if (nc1 != 3) Plog(0,"JPEG file has %d channels: %s \n",nc1,file); // try anyway
ww = cinfo.output_width; // actual output dimensions
hh = cinfo.output_height;
cc64 = imagesize(ww,hh,nc1,1);
pixels = (uint8 *) zmalloc(cc64,"JPG_PXM_load"); // allocate memory for RGB image
cc = hh * sizeof(void *); // allocate row pointers
row_pointers = (uint8 **) zmalloc(cc,"JPG_PXM_load");
for (row = 0; row < hh; row++) // initz. row pointers
row_pointers[row] = pixels + (uint)(row) * ww * nc1; // GCC bug 2018, now fixed
while (cinfo.output_scanline < cinfo.output_height) { // read rows
row = cinfo.output_scanline;
buffer = row_pointers[row];
jpeg_read_scanlines(&cinfo,&buffer,nb);
}
jpeg_finish_decompress(&cinfo); // wrap-up
jpeg_destroy_decompress(&cinfo);
fclose(fid);
zfree(row_pointers);
row_pointers = 0;
nc2 = 3;
pxm = PXM_make(ww,hh,nc2); // output PXM, no alpha channel
pixelvert(pixels,pxm->pixels,ww,hh,nc1,nc2); // convert pixel data
PXM_audit(pxm); // audit/repair
zfree(pixels);
pixels = 0;
f_load_bpc = 8;
return pxm;
errret:
if (errno) Plog(0,"error: %s \n",strerror(errno));
Plog(0,"jpeg file error: %s \n",file);
if (fid && fileno(fid) != -1) fclose(fid);
if (row_pointers) zfree(row_pointers);
if (pixels) zfree(pixels);
return 0;
}
// Write PXB pixmap to JPG file using JPG library.
// quality is compression quality 0-100
// returns 0 if OK, +N if error.
int PXB_JPG_save(PXB *pxb, ch *file, int quality)
{
struct jpeg_compress_struct cinfo;
struct libjpeg_error_mgr jerr;
FILE *fid = 0;
int ww, hh, nc1, nc2, row, nn;
uint cc;
int64 cc64;
uint8 **row_pointers = 0;
uint8 *pixels1, *pixels2 = 0;
fid = fopen(file,"w");
if (! fid) goto errret;
cinfo.err = jpeg_std_error(&jerr.pub); // replace standard error handler
jerr.pub.error_exit = libjpeg_error_exit; // which exits on error
if (setjmp(jerr.setjmp_buffer)) {
jpeg_destroy_compress(&cinfo);
goto errret;
}
jpeg_create_compress(&cinfo); // initz. for write
jpeg_stdio_dest(&cinfo,fid);
ww = pxb->ww; // image dimensions
hh = pxb->hh;
nc1 = pxb->nc; // input channels (3/4)
nc2 = 3; // output channels
pixels1 = pxb->pixels; // input pixels
cc64 = imagesize(ww,hh,nc2,1);
pixels2 = (uint8 *) zmalloc(cc64,"PXB_JPG_save");
pixelvert(pixels1,pixels2,ww,hh,nc1,nc2); // convert pixel data
cinfo.image_width = ww;
cinfo.image_height = hh;
cinfo.input_components = nc2;
cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfo); // default compression parameters
jpeg_set_quality(&cinfo,quality,TRUE); // compression quality 0-100
jpeg_start_compress(&cinfo,TRUE);
cc = hh * sizeof(void *); // allocate row pointers
row_pointers = (uint8 **) zmalloc(cc,"PXB_JPG_save");
for (row = 0; row < hh; row++) // initz. row pointers
row_pointers[row] = pixels2 + (uint)(row) * ww * nc2; // GCC bug 2018, now fixed
nn = jpeg_write_scanlines(&cinfo,row_pointers,hh); // write rows
if (nn != hh) goto errret;
jpeg_finish_compress(&cinfo); // wrap-up
jpeg_destroy_compress(&cinfo);
fclose(fid);
zfree(row_pointers);
zfree(pixels2);
row_pointers = 0;
pixels2 = 0;
return 0;
errret:
if (errno) Plog(0,"error: %s \n",strerror(errno));
Plog(0,"jpeg file error: %s \n",file);
if (fid && fileno(fid) != -1) fclose(fid);
if (row_pointers) zfree(row_pointers);
if (pixels2) zfree(pixels2);
return 3;
}
// Write PXM pixmap to JPG file using JPG library.
// quality is compression quality 0-100
// returns 0 if OK, +N if error.
int PXM_JPG_save(PXM *pxm, ch *file, int quality)
{
struct jpeg_compress_struct cinfo;
struct libjpeg_error_mgr jerr;
FILE *fid = 0;
int ww, hh, nc1, nc2, row, nn;
uint cc;
int64 cc64;
uint8 **row_pointers = 0;
uint8 *pixels2 = 0;
float *pixels1;
fid = fopen(file,"w");
if (! fid) goto errret;
cinfo.err = jpeg_std_error(&jerr.pub); // replace standard error handler
jerr.pub.error_exit = libjpeg_error_exit; // which exits on error
if (setjmp(jerr.setjmp_buffer)) {
jpeg_destroy_compress(&cinfo);
goto errret;
}
jpeg_create_compress(&cinfo); // initz. for write
jpeg_stdio_dest(&cinfo,fid);
ww = pxm->ww; // image dimensions
hh = pxm->hh;
nc1 = pxm->nc; // 3 or 4 channels RGB[A]
nc2 = 3;
pixels1 = pxm->pixels; // input float *
cc64 = imagesize(ww,hh,nc2,1);
pixels2 = (uint8 *) zmalloc(cc64,"PXM_JPG_save"); // output uint8 *
pixelvert(pixels1,pixels2,ww,hh,nc1,nc2); // convert pixel data
cinfo.image_width = ww;
cinfo.image_height = hh;
cinfo.input_components = nc2; // RGB
cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfo); // default compression parameters
jpeg_set_quality(&cinfo,quality,TRUE); // compression quality 0-100
jpeg_start_compress(&cinfo,TRUE);
cc = hh * sizeof(void *); // allocate row pointers
row_pointers = (uint8 **) zmalloc(cc,"PXM_JPG_save");
for (row = 0; row < hh; row++) // initz. row pointers
row_pointers[row] = pixels2 + (uint)(row) * ww * nc2; // GCC bug 2018, now fixed
nn = jpeg_write_scanlines(&cinfo,row_pointers,hh); // write rows
if (nn != hh) goto errret;
jpeg_finish_compress(&cinfo); // wrap-up
jpeg_destroy_compress(&cinfo);
fclose(fid);
zfree(row_pointers);
zfree(pixels2);
row_pointers = 0;
pixels2 = 0;
return 0;
errret:
if (errno) Plog(0,"error: %s \n",strerror(errno));
Plog(0,"jpeg file error: %s \n",file);
if (fid && fileno(fid) != -1) fclose(fid);
if (row_pointers) zfree(row_pointers);
if (pixels2) zfree(pixels2);
return 2;
}
/********************************************************************************/
#pragma GCC pop_options // revert to normal optimization
/********************************************************************************
TIFF file read and write functions
*********************************************************************************/
// Intercept TIFF warning messages (many)
void tiffwarninghandler(cch *module, cch *format, va_list ap)
{
return; // stop flood of crap
ch message[200];
vsnprintf(message,199,format,ap);
Plog(0,"TIFF warning: %s %s \n",module,message);
return;
}
// Load PXB pixmap from TIFF file using TIFF library.
// Native TIFF file bits/color >> f_load_bpc
PXB * TIFF_PXB_load(ch *file)
{
static int ftf = 1;
TIFF *tiff;
PXB *pxb;
ch *tiffbuff;
uint16 bpc, nc1, nc2, pcfg; // pcfg int -> uint16
uint ww, hh, rs1, rps, rwb, stb, nst;
uint row, strip, cc;
int64 buffsz1, buffsz2, buffsz;
int64 buffbytes, buffbits;
int64 z64 = 1;
if (! regfile(file)) goto errret; // no print error
if (ftf) {
TIFFSetWarningHandler(tiffwarninghandler); // intercept TIFF warning messages
ftf = 0;
}
tiff = TIFFOpen(file,"r");
if (! tiff) {
Plog(0,"tiff file error: %s %s \n",file,strerror(errno));
goto errret;
}
TIFFGetFieldDefaulted(tiff, TIFFTAG_IMAGEWIDTH, &ww); // width
TIFFGetFieldDefaulted(tiff, TIFFTAG_IMAGELENGTH, &hh); // height
TIFFGetFieldDefaulted(tiff, TIFFTAG_BITSPERSAMPLE, &bpc); // bits per color, 1/8/16
TIFFGetFieldDefaulted(tiff, TIFFTAG_SAMPLESPERPIXEL, &nc1); // no. channels (colors), 1/2/3/4
TIFFGetFieldDefaulted(tiff, TIFFTAG_ROWSPERSTRIP, &rps); // rows per strip
TIFFGetFieldDefaulted(tiff, TIFFTAG_PLANARCONFIG, &pcfg); // 1 = contiguous, 2 = planes
rwb = TIFFScanlineSize(tiff); // scanline size
stb = TIFFStripSize(tiff); // strip size (ww)
nst = TIFFNumberOfStrips(tiff); // number of strips (hh)
if (pcfg != 1 || nc1 > 4) {
Plog(0,"tiff format not supported: %s pcfg: %d nc1: %d \n",file,pcfg,nc1);
TIFFClose(tiff);
goto errret;
}
if (bpc != 8 && bpc != 16) { // bits/color
Plog(0,"tiff format not supported: %s bpc: %d \n",file,bpc);
TIFFClose(tiff);
goto errret;
}
if (nst == 0 || stb == 0) { // R.Nolde tiff integrity checks
Plog(0,"tiff format not supported: %s nst: %d stb: %d \n",file,nst,stb);
TIFFClose(tiff);
goto errret;
}
buffbytes = ((uint64)(stb)) * nst;
if (stb != buffbytes / nst) {
Plog(0,"tiff buffer check fail: %s %d %d \n",file,stb,nst);
TIFFClose(tiff);
goto errret;
}
buffbits = ((uint64)(ww)) * hh * nc1 * bpc;
if (hh != buffbits / ww / nc1 / bpc) {
Plog(0,"tiff buffer check fail: %s %d %d %d %d \n",file,ww,hh,nc1,bpc);
TIFFClose(tiff);
goto errret;
}
rs1 = ww * nc1; // row stride
if (bpc == 16) rs1 = rs1 * 2;
buffsz1 = z64 * stb * nst; // alternate size calculations
buffsz2 = z64 * rs1 * hh;
buffsz = buffsz1;
if (buffsz2 > buffsz) buffsz = buffsz2; // use the larger
if (! buffsz) goto errret;
buffsz += rwb; // extra row, R.Nolde
tiffbuff = (ch *) zmalloc(buffsz,"TIFF_PXB_load"); // allocate buffer
for (strip = 0; strip < nst; strip++) // read strips
{
row = strip * rps; // 1st row in strip
cc = TIFFReadEncodedStrip(tiff,strip,tiffbuff+row*rs1,stb);
if (cc > 0) continue;
if (cc == 0) break;
TIFFClose(tiff); // failed
zfree(tiffbuff);
return ANY_PXB_load(file); // fallback to pixbuf loader
}
TIFFClose(tiff);
if (nc1 == 2 || nc1 == 4) nc2 = 4; // alpha channel present
else nc2 = 3;
pxb = PXB_make(ww,hh,nc2); // create output PXB
if (bpc == 8) pixelvert((uint8 *) tiffbuff,pxb->pixels,ww,hh,nc1,nc2); // convert pixel data
if (bpc == 16) pixelvert((uint16 *) tiffbuff,pxb->pixels,ww,hh,nc1,nc2);
zfree(tiffbuff);
f_load_bpc = bpc;
return pxb;
errret:
return 0;
}
// Load PXM pixmap from TIFF file using TIFF library.
// Native TIFF file bits/color >> f_load_bpc
PXM * TIFF_PXM_load(ch *file)
{
static int ftf = 1;
TIFF *tiff;
PXM *pxm;
ch *tiffbuff;
uint16 bpc, nc1, nc2, pcfg; // pcfg int -> uint16
uint ww, hh, rs1, rps, rwb, stb, nst;
uint row, strip, cc;
int64 buffsz1, buffsz2, buffsz;
int64 buffbytes, buffbits;
int64 z64 = 1;
if (! regfile(file)) goto errret; // no print error
if (ftf) {
TIFFSetWarningHandler(tiffwarninghandler); // intercept TIFF warning messages
ftf = 0;
}
tiff = TIFFOpen(file,"r");
if (! tiff) {
Plog(0,"tiff file error: %s %s \n",file,strerror(errno));
goto errret;
}
TIFFGetFieldDefaulted(tiff, TIFFTAG_IMAGEWIDTH, &ww); // width
TIFFGetFieldDefaulted(tiff, TIFFTAG_IMAGELENGTH, &hh); // height
TIFFGetFieldDefaulted(tiff, TIFFTAG_BITSPERSAMPLE, &bpc); // bits per color, 1/8/16
TIFFGetFieldDefaulted(tiff, TIFFTAG_SAMPLESPERPIXEL, &nc1); // no. channels (colors), 1/2/3/4
TIFFGetFieldDefaulted(tiff, TIFFTAG_ROWSPERSTRIP, &rps); // rows per strip
TIFFGetFieldDefaulted(tiff, TIFFTAG_PLANARCONFIG, &pcfg); // 1 = contiguous, 2 = planes
rwb = TIFFScanlineSize(tiff); // scanline size
stb = TIFFStripSize(tiff); // strip size (ww)
nst = TIFFNumberOfStrips(tiff); // number of strips (hh)
if (pcfg != 1 || nc1 > 4) {
Plog(0,"tiff format not supported: %s pcfg: %d nc1: %d \n",file,pcfg,nc1);
TIFFClose(tiff);
goto errret;
}
if (bpc != 8 && bpc != 16) { // bits/color
Plog(0,"tiff format not supported: %s bpc: %d \n",file,bpc);
TIFFClose(tiff);
goto errret;
}
if (nst == 0 || stb == 0) { // R.Nolde tiff integrity checks
Plog(0,"tiff format not supported: %s nst: %d stb: %d \n",file,nst,stb);
TIFFClose(tiff);
goto errret;
}
buffbytes = ((uint64)(stb)) * nst;
if (stb != buffbytes / nst) {
Plog(0,"tiff buffer check fail: %s %d %d \n",file,stb,nst);
TIFFClose(tiff);
goto errret;
}
buffbits = ((uint64)(ww)) * hh * nc1 * bpc;
if (hh != buffbits / ww / nc1 / bpc) {
Plog(0,"tiff buffer check fail: %s %d %d %d %d \n",file,ww,hh,nc1,bpc);
TIFFClose(tiff);
goto errret;
}
rs1 = ww * nc1; // row stride
if (bpc == 16) rs1 = rs1 * 2;
buffsz1 = z64 * stb * nst; // alternate size calculations
buffsz2 = z64 * rs1 * hh;
buffsz = buffsz1;
if (buffsz2 > buffsz) buffsz = buffsz2; // use the larger
if (! buffsz) goto errret;
buffsz += rwb; // extra row, R.Nolde
tiffbuff = (ch *) zmalloc(buffsz,"TIFF_PXM_load"); // allocate buffer
for (strip = 0; strip < nst; strip++) // read strips
{
row = strip * rps; // 1st row in strip
cc = TIFFReadEncodedStrip(tiff,strip,tiffbuff+row*rs1,stb);
if (cc > 0) continue;
if (cc == 0) break;
TIFFClose(tiff); // failed
zfree(tiffbuff);
return ANY_PXM_load(file); // fallback to pixbuf loader
}
TIFFClose(tiff);
if (nc1 == 2 || nc1 == 4) nc2 = 4; // alpha channel present
else nc2 = 3;
pxm = PXM_make(ww,hh,nc2); // create output PXM
if (bpc == 8) pixelvert((uint8 *) tiffbuff,pxm->pixels,ww,hh,nc1,nc2); // convert pixel data
if (bpc == 16) pixelvert((uint16 *) tiffbuff,pxm->pixels,ww,hh,nc1,nc2);
zfree(tiffbuff);
f_load_bpc = bpc;
PXM_audit(pxm); // audit/repair
return pxm;
errret:
return 0;
}
// Write PXB pixmap to TIFF file using TIFF library.
// TIFF file bpc is 8 or 16.
// Returns 0 if OK, +N if error.
int PXB_TIFF_save(PXB *pxb, ch *file, int bpc)
{
static int ftf = 1;
TIFF *tiff;
ch *tiffbuff;
int tiffstat = 0;
int ww, hh, row; // int not uint
int nc1, nc2, rs2, pm = 2, pc = 1, rps = 1;
int comp = tiff_comp_method;
int64 cc64, z64 = 1;
uint16 es[1];
if (ftf) {
TIFFSetWarningHandler(tiffwarninghandler); // intercept TIFF warning messages
ftf = 0;
}
tiff = TIFFOpen(file,"w");
if (! tiff) {
Plog(0,"file error: %s %s \n",file,strerror(errno));
goto errret;
}
ww = pxb->ww;
hh = pxb->hh;
nc1 = pxb->nc;
nc2 = nc1;
rs2 = ww * nc2; // output row stride
if (bpc == 16) rs2 = rs2 * 2;
cc64 = z64 * rs2 * hh;
tiffbuff = (ch *) zmalloc(cc64,"PXB_TIFF_save");
TIFFSetField(tiff, TIFFTAG_IMAGEWIDTH, ww);
TIFFSetField(tiff, TIFFTAG_IMAGELENGTH, hh); // Nolde
TIFFSetField(tiff, TIFFTAG_BITSPERSAMPLE, bpc);
TIFFSetField(tiff, TIFFTAG_SAMPLESPERPIXEL, nc2);
TIFFSetField(tiff, TIFFTAG_PHOTOMETRIC, pm);
TIFFSetField(tiff, TIFFTAG_PLANARCONFIG, pc);
TIFFSetField(tiff, TIFFTAG_COMPRESSION, comp);
TIFFSetField(tiff, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); // Nolde
if (nc2 == 2 || nc2 == 4) { // extra channel is alpha
es[0] = EXTRASAMPLE_ASSOCALPHA;
TIFFSetField(tiff,TIFFTAG_EXTRASAMPLES,1,&es);
}
if (rs2 > 10240) rps = 1; // R.Nolde
else rps = 10240 / rs2;
TIFFSetField(tiff,TIFFTAG_ROWSPERSTRIP,rps);
if (bpc == 8) pixelvert(pxb->pixels,(uint8 *) tiffbuff,ww,hh,nc1,nc2);
if (bpc == 16) pixelvert(pxb->pixels,(uint16 *) tiffbuff,ww,hh,nc1,nc2);
for (row = 0; row < hh; row++)
{
tiffstat = TIFFWriteScanline(tiff,tiffbuff+row*rs2,row,0);
if (tiffstat != 1) break;
}
TIFFClose(tiff);
zfree(tiffbuff);
if (tiffstat == 1) return 0;
errret:
return 1;
}
// Write PXM pixmap to TIFF file using TIFF library.
// TIFF file bpc is 8 or 16.
// Returns 0 if OK, +N if error.
int PXM_TIFF_save(PXM *pxm, ch *file, int bpc)
{
static int ftf = 1;
TIFF *tiff;
ch *tiffbuff;
int tiffstat = 0;
int ww, hh, row; // int not uint
int nc1, nc2, rs2, pm = 2, pc = 1, rps = 1;
int comp = tiff_comp_method;
int64 cc64, z64 = 1;
uint16 es[1];
if (ftf) {
TIFFSetWarningHandler(tiffwarninghandler); // intercept TIFF warning messages
ftf = 0;
}
tiff = TIFFOpen(file,"w");
if (! tiff) {
Plog(0,"file error: %s %s \n",file,strerror(errno));
goto errret;
}
ww = pxm->ww;
hh = pxm->hh;
nc1 = pxm->nc;
nc2 = nc1;
rs2 = ww * nc2; // output row stride
if (bpc == 16) rs2 = rs2 * 2;
cc64 = z64 * rs2 * hh;
tiffbuff = (ch *) zmalloc(cc64,"PXM_TIFF_save");
TIFFSetField(tiff, TIFFTAG_IMAGEWIDTH, ww);
TIFFSetField(tiff, TIFFTAG_IMAGELENGTH, hh); // Nolde
TIFFSetField(tiff, TIFFTAG_BITSPERSAMPLE, bpc);
TIFFSetField(tiff, TIFFTAG_SAMPLESPERPIXEL, nc2);
TIFFSetField(tiff, TIFFTAG_PHOTOMETRIC, pm);
TIFFSetField(tiff, TIFFTAG_PLANARCONFIG, pc);
TIFFSetField(tiff, TIFFTAG_COMPRESSION, comp);
TIFFSetField(tiff, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); // Nolde
if (nc2 == 2 || nc2 == 4) { // extra channel is alpha
es[0] = EXTRASAMPLE_ASSOCALPHA;
TIFFSetField(tiff,TIFFTAG_EXTRASAMPLES,1,&es);
}
if (rs2 > 10240) rps = 1; // R.Nolde
else rps = 10240 / rs2;
TIFFSetField(tiff,TIFFTAG_ROWSPERSTRIP,rps);
if (bpc == 8) pixelvert(pxm->pixels,(uint8 *) tiffbuff,ww,hh,nc1,nc2);
if (bpc == 16) pixelvert(pxm->pixels,(uint16 *) tiffbuff,ww,hh,nc1,nc2);
for (row = 0; row < hh; row++)
{
tiffstat = TIFFWriteScanline(tiff,tiffbuff+row*rs2,row,0);
if (tiffstat != 1) break;
}
TIFFClose(tiff);
zfree(tiffbuff);
if (tiffstat == 1) return 0;
errret:
return 1;
}
/********************************************************************************
PNG file read and write functions
*********************************************************************************/
// Load PXB pixmap from PNG file using PNG library.
// Native PNG file bits/color >> f_load_bpc
PXB * PNG_PXB_load(ch *file)
{
png_structp pngstruct = 0;
png_infop pnginfo = 0;
FILE *fid = 0;
int ww, hh, bpc, nc1, nc2, rs1, row, colortype;
uch *pngbuff, **pngrows;
PXB *pxb;
int64 cc64;
if (! regfile(file)) goto errret; // no print error
fid = fopen(file,"r"); // open file
if (! fid) {
Plog(0,"file error: %s %s \n",file,strerror(errno));
goto errret;
}
pngstruct = png_create_read_struct(PNG_LIBPNG_VER_STRING,0,0,0); // create png structs
if (! pngstruct) goto errret;
pnginfo = png_create_info_struct(pngstruct);
if (! pnginfo) goto errret;
if (setjmp(png_jmpbuf(pngstruct))) goto errret; // setup error handling
png_init_io(pngstruct,fid); // read png file
png_read_png(pngstruct,pnginfo,PNG_TRANSFORM_SWAP_ENDIAN,0);
fclose(fid);
ww = png_get_image_width(pngstruct,pnginfo); // width
hh = png_get_image_height(pngstruct,pnginfo); // height
bpc = png_get_bit_depth(pngstruct,pnginfo); // bits per color (channel)
nc1 = png_get_channels(pngstruct,pnginfo); // no. channels
colortype = png_get_color_type(pngstruct,pnginfo); // color type
pngrows = png_get_rows(pngstruct,pnginfo); // array of png row pointers
if (colortype == PNG_COLOR_TYPE_GRAY || colortype == PNG_COLOR_TYPE_RGB)
nc2 = 3;
else if (colortype == PNG_COLOR_TYPE_GRAY_ALPHA || colortype == PNG_COLOR_TYPE_RGB_ALPHA)
nc2 = 4;
else {
png_destroy_read_struct(&pngstruct,&pnginfo,0);
return ANY_PXB_load(file); // fallback to pixbuf loader
}
if (bpc != 8 && bpc != 16) { // not 8 or 16 bits per channel
png_destroy_read_struct(&pngstruct,&pnginfo,0);
return ANY_PXB_load(file); // use standard pixbuf loader
}
if (nc1 > 4) { // not gray (+alpha) or RGB (+alpha)
png_destroy_read_struct(&pngstruct,&pnginfo,0);
return ANY_PXB_load(file); // use standard pixbuf loader
}
pxb = PXB_make(ww,hh,nc2); // create output PXB
rs1 = ww * nc1; // png input row stride
if (bpc == 16) rs1 = rs1 * 2;
cc64 = imagesize(ww,hh,nc1,1);
if (bpc == 16) cc64 = imagesize(ww,hh,nc1,2);
pngbuff = (uch *) zmalloc(cc64,"PNG_PXB_load"); // png file input buffer
for (row = 0; row < hh; row++) // get all png rows
memcpy(pngbuff+row*rs1,pngrows[row],rs1);
png_destroy_read_struct(&pngstruct,&pnginfo,0); // release memory
if (bpc == 8) pixelvert((uint8 *) pngbuff,pxb->pixels,ww,hh,nc1,nc2); // convert pixel data
if (bpc == 16) pixelvert((uint16 *) pngbuff,pxb->pixels,ww,hh,nc1,nc2);
zfree(pngbuff);
f_load_bpc = bpc;
return pxb;
errret:
if (fid && fileno(fid) != -1) fclose(fid);
return 0;
}
// Load PXM pixmap from PNG file using PNG library.
// Native PNG file bits/color >> f_load_bpc
PXM * PNG_PXM_load(ch *file)
{
png_structp pngstruct = 0;
png_infop pnginfo = 0;
FILE *fid = 0;
int ww, hh, bpc, nc1, nc2, rs1, row, colortype;
uch *pngbuff, **pngrows;
PXM *pxm;
int64 cc64;
if (! regfile(file)) goto errret; // no print error
fid = fopen(file,"r"); // open file
if (! fid) {
Plog(0,"file error: %s %s \n",file,strerror(errno));
goto errret;
}
pngstruct = png_create_read_struct(PNG_LIBPNG_VER_STRING,0,0,0); // create png structs
if (! pngstruct) goto errret;
pnginfo = png_create_info_struct(pngstruct);
if (! pnginfo) goto errret;
if (setjmp(png_jmpbuf(pngstruct))) goto errret; // setup error handling
png_init_io(pngstruct,fid); // read png file
png_read_png(pngstruct,pnginfo,PNG_TRANSFORM_SWAP_ENDIAN,0);
fclose(fid);
ww = png_get_image_width(pngstruct,pnginfo); // width
hh = png_get_image_height(pngstruct,pnginfo); // height
bpc = png_get_bit_depth(pngstruct,pnginfo); // bits per color (channel)
nc1 = png_get_channels(pngstruct,pnginfo); // no. channels
colortype = png_get_color_type(pngstruct,pnginfo); // color type
pngrows = png_get_rows(pngstruct,pnginfo); // array of png row pointers
if (colortype == PNG_COLOR_TYPE_GRAY || colortype == PNG_COLOR_TYPE_RGB)
nc2 = 3;
else if (colortype == PNG_COLOR_TYPE_GRAY_ALPHA || colortype == PNG_COLOR_TYPE_RGB_ALPHA)
nc2 = 4;
else {
png_destroy_read_struct(&pngstruct,&pnginfo,0);
return ANY_PXM_load(file); // fallback to pixbuf loader
}
if (bpc != 8 && bpc != 16) {
png_destroy_read_struct(&pngstruct,&pnginfo,0);
return ANY_PXM_load(file); // use standard pixbuf loader
}
if (nc1 > 4) { // not gray (+alpha) or RGB (+alpha)
png_destroy_read_struct(&pngstruct,&pnginfo,0);
return ANY_PXM_load(file); // use standard pixbuf loader
}
pxm = PXM_make(ww,hh,nc2); // create PXM
rs1 = ww * nc1; // png input row stride
if (bpc == 16) rs1 = rs1 * 2;
cc64 = imagesize(ww,hh,nc1,1);
if (bpc == 16) cc64 = imagesize(ww,hh,nc1,2);
pngbuff = (uch *) zmalloc(cc64,"PNG_PXM_load"); // png file input buffer
for (row = 0; row < hh; row++) // get all png rows
memcpy(pngbuff+row*rs1,pngrows[row],rs1);
png_destroy_read_struct(&pngstruct,&pnginfo,0); // release memory
if (bpc == 8) pixelvert((uint8 *) pngbuff,pxm->pixels,ww,hh,nc1,nc2); // convert pixel data
if (bpc == 16) pixelvert((uint16 *) pngbuff,pxm->pixels,ww,hh,nc1,nc2);
PXM_audit(pxm); // audit/repair
zfree(pngbuff);
f_load_bpc = bpc;
return pxm;
errret:
if (fid && fileno(fid) != -1) fclose(fid);
return 0;
}
// Write PXB pixmap to PNG file using PNG library.
// File bpc is 8 or 16.
// returns 0 if OK, +N if error.
int PXB_PNG_save(PXB *pxb, ch *file, int bpc)
{
png_structp pngstruct;
png_infop pnginfo;
FILE *fid = 0;
uch *pngbuff, **pngrows;
int ww, hh, nc1, nc2, rs2, row;
int64 cc64;
if (bpc != 8 && bpc != 16) {
Plog(0,"PNG BPC not 8/16: %s",file);
goto errret;
}
fid = fopen(file,"w"); // open output file
if (! fid) {
Plog(0,"file error: %s %s \n",file,strerror(errno));
goto errret;
}
ww = pxb->ww;
hh = pxb->hh;
nc1 = pxb->nc;
nc2 = nc1;
pngstruct = png_create_write_struct(PNG_LIBPNG_VER_STRING,0,0,0); // create png structs
if (! pngstruct) goto errret;
pnginfo = png_create_info_struct(pngstruct);
if (! pnginfo) goto errret;
if (setjmp(png_jmpbuf(pngstruct))) goto errret; // setup error handling
Funcbusy(+1,1); // may take a while 24.10
png_init_io(pngstruct,fid); // initz. for writing file
png_set_compression_level(pngstruct,1); // fast
if (nc1 == 4)
png_set_IHDR(pngstruct,pnginfo,ww,hh,bpc,PNG_COLOR_TYPE_RGB_ALPHA,0,0,0);
else
png_set_IHDR(pngstruct,pnginfo,ww,hh,bpc,PNG_COLOR_TYPE_RGB,0,0,0);
rs2 = ww * nc2; // png row length
if (bpc == 16) rs2 = rs2 * 2;
cc64 = imagesize(ww,hh,nc2,1);
if (bpc == 16) cc64 = imagesize(ww,hh,nc2,2);
pngbuff = (uch *) zmalloc(cc64,"PXB_PNG_save"); // allocate png file data
pngrows = (uch **) zmalloc(hh * sizeof(ch *),"PXB_PNG_save"); // allocate png row pointers
png_set_rows(pngstruct,pnginfo,pngrows);
for (row = 0; row < hh; row++) // set row pointers to row data
pngrows[row] = pngbuff + row * rs2;
if (bpc == 8) pixelvert(pxb->pixels,(uint8 *) pngbuff,ww,hh,nc1,nc2); // convert pixel data
if (bpc == 16) pixelvert(pxb->pixels,(uint16 *) pngbuff,ww,hh,nc1,nc2);
png_write_png(pngstruct,pnginfo,PNG_TRANSFORM_SWAP_ENDIAN,0); // write the file
fclose(fid);
png_destroy_write_struct(&pngstruct,&pnginfo); // release memory
zfree(pngbuff);
zfree(pngrows);
Funcbusy(-1); // 24.10
return 0;
errret:
if (fid && fileno(fid) != -1) fclose(fid);
return 2;
}
// Write PXM pixmap to PNG file using PNG library.
// File bpc is 8 or 16.
// returns 0 if OK, +N if error.
int PXM_PNG_save(PXM *pxm, ch *file, int bpc)
{
png_structp pngstruct;
png_infop pnginfo;
FILE *fid = 0;
uch *pngbuff, **pngrows;
int ww, hh, nc1, nc2, rs2, row;
int cc64;
if (bpc != 8 && bpc != 16) {
Plog(0,"PNG BPC not 8/16: %s",file);
goto errret;
}
fid = fopen(file,"w"); // open output file
if (! fid) {
Plog(0,"file error: %s %s \n",file,strerror(errno));
goto errret;
}
ww = pxm->ww;
hh = pxm->hh;
nc1 = pxm->nc;
nc2 = nc1;
pngstruct = png_create_write_struct(PNG_LIBPNG_VER_STRING,0,0,0); // create png structs
if (! pngstruct) goto errret;
pnginfo = png_create_info_struct(pngstruct);
if (! pnginfo) goto errret;
if (setjmp(png_jmpbuf(pngstruct))) goto errret; // setup error handling
Funcbusy(+1,1); // may take a while 24.10
png_init_io(pngstruct,fid); // initz. for writing file
png_set_compression_level(pngstruct,1); // fast
if (nc1 == 4)
png_set_IHDR(pngstruct,pnginfo,ww,hh,bpc,PNG_COLOR_TYPE_RGB_ALPHA,0,0,0);
else
png_set_IHDR(pngstruct,pnginfo,ww,hh,bpc,PNG_COLOR_TYPE_RGB,0,0,0);
rs2 = ww * nc2; // png row length
if (bpc == 16) rs2 = rs2 * 2;
cc64 = imagesize(ww,hh,nc2,1);
if (bpc == 16) cc64 = imagesize(ww,hh,nc2,2);
pngbuff = (uch *) zmalloc(cc64,"PXM_PNG_save"); // allocate png file data
pngrows = (uch **) zmalloc(hh * sizeof(ch *),"PXM_PNG_save"); // allocate png row pointers
png_set_rows(pngstruct,pnginfo,pngrows);
for (row = 0; row < hh; row++) // set row pointers to row data
pngrows[row] = pngbuff + row * rs2;
if (bpc == 8) pixelvert(pxm->pixels,(uint8 *) pngbuff,ww,hh,nc1,nc2); // convert pixel data
if (bpc == 16) pixelvert(pxm->pixels,(uint16 *) pngbuff,ww,hh,nc1,nc2);
png_write_png(pngstruct,pnginfo,PNG_TRANSFORM_SWAP_ENDIAN,0); // write the file
fclose(fid);
png_destroy_write_struct(&pngstruct,&pnginfo); // release memory
zfree(pngbuff);
zfree(pngrows);
Funcbusy(-1); // 24.10
return 0;
errret:
if (fid && fileno(fid) != -1) fclose(fid);
return 2;
}
/********************************************************************************
HEIC file read functions
(write functions are not implemented)
*********************************************************************************/
// Load PXB pixmap from HEIC file using HEIC library (heif-convert).
// Also works for AVIF files (.avif)
// Crutch: convert .heic file to .jpg and load .jpg file.
// f_load_bpc = 8
PXB * HEIC_PXB_load(ch *file, int size)
{
ch *jpegfile = 0, *jpegfix = 0;
ch *pp;
ch *f1, *f2;
PXB *pxb;
static int ftf = 1;
ch *installmess = ".heic/.avif files not supported (install heif_convert)";
if (! Fheif) {
if (ftf) { ftf = 0; zmessageACK(Mwin,installmess); }
return 0;
}
if (! regfile(file)) return 0; // no print error
jpegfile = zstrdup(file,"HEIC_PXB_load",8); // file.heic >> file.jpg
pp = strrchr(jpegfile,'.');
if (! pp) goto errret;
strcpy(pp,".jpg");
f1 = zescape_quotes(file);
f2 = zescape_quotes(jpegfile);
zshell(0,"heif-convert -q %d \"%s\" \"%s\" >/dev/null ", // convert to .jpg
jpeg_def_quality,f1,f2);
zfree(f1);
zfree(f2);
if (! regfile(jpegfile)) {
jpegfix = zstrdup(jpegfile,"HEIC_PXB_load",8); // failed, may have multiple output files
pp = strrchr(jpegfix,'.'); // file-1.jpg, file-2.jpg, etc.
strcpy(pp,"-1.jpg");
if (! regfile(jpegfix)) goto errret;
rename(jpegfix,jpegfile); // if file-1.jpg, rename to file.jpg
zfree(jpegfix); // (file-2.jpg etc. remain)
}
pxb = JPG_PXB_load(jpegfile,size); // make PXB from jpeg file
remove(jpegfile);
zfree(jpegfile);
return pxb;
errret:
if (jpegfile) remove(jpegfile);
if (jpegfile) zfree(jpegfile);
if (jpegfix) zfree(jpegfix);
return 0;
}
// Load PXM pixmap from HEIC file using HEIC library (heif-convert).
// Also works for AVIF files (.avif)
// Crutch: convert .heic file to .jpg and load .jpg file.
// f_load_bpc = 8
PXM * HEIC_PXM_load(ch *file)
{
ch *jpegfile = 0, *jpegfix = 0;
ch *pp;
ch *f1, *f2;
PXM *pxm;
static int ftf = 1;
ch *installmess = ".heic/.avif files not supported (install heif_convert)";
if (! Fheif) {
if (ftf) { ftf = 0; zmessageACK(Mwin,installmess); }
return 0;
}
if (! regfile(file)) return 0; // no print error
jpegfile = zstrdup(file,"HEIC_PXM_load",8);
pp = strrchr(jpegfile,'.');
if (! pp) goto errret;
strcpy(pp,".jpg");
f1 = zescape_quotes(file);
f2 = zescape_quotes(jpegfile);
zshell(0,"heif-convert -q %d \"%s\" \"%s\" >/dev/null ", // convert to .jpg
jpeg_def_quality,f1,f2);
zfree(f1);
zfree(f2);
if (! regfile(jpegfile)) {
jpegfix = zstrdup(jpegfile,"HEIC_PXM_load",8);
pp = strrchr(jpegfix,'.');
strcpy(pp,"-1.jpg");
if (! regfile(jpegfix)) goto errret;
rename(jpegfix,jpegfile);
zfree(jpegfix);
}
pxm = JPG_PXM_load(jpegfile);
remove(jpegfile);
zfree(jpegfile);
return pxm;
errret:
if (jpegfile) remove(jpegfile);
if (jpegfile) zfree(jpegfile);
if (jpegfix) zfree(jpegfix);
return 0;
}
int PXB_HEIC_save(PXB *pxb, ch *file)
{
zmessageACK(Mwin,"save to .heic/.avif file not supported");
return 0;
}
int PXM_HEIC_save(PXM *pxm, ch *file)
{
zmessageACK(Mwin,"save to .heic/.avif file not supported");
return 0;
}
/********************************************************************************
JP2 file read functions
(write functions are not implemented)
*********************************************************************************/
// Load PXB pixmap from JP2 file using jpeg2000 utility (opj_decompress).
// Crutch: convert .jp2 file to .tif and load .tif file.
// f_load_bpc = 8
PXB * JP2_PXB_load(ch *file)
{
int err;
ch *tiffile = 0;
ch *pp;
ch *f1, *f2;
PXB *pxb;
static int ftf = 1;
ch *installmess = ".jp2 files not supported (install opj_decompress)";
if (! Fjp2) {
if (ftf) { ftf = 0; zmessageACK(Mwin,installmess); }
return 0;
}
if (! Fjp2) {
zmessageACK(Mwin,"JP2 files not supported");
return 0;
}
if (! regfile(file)) return 0; // no print error
tiffile = zstrdup(file,"JP2_PXB_load",8); // file.jp2 >> file.tif
pp = strrchr(tiffile,'.');
if (! pp) goto errret;
strcpy(pp,".tif");
f1 = zescape_quotes(file);
f2 = zescape_quotes(tiffile);
err = zshell(0,"opj_decompress -i \"%s\" -o \"%s\" >/dev/null 2>1",f1,f2);
zfree(f1);
zfree(f2);
if (err) goto errret;
if (! regfile(tiffile)) goto errret;
pxb = TIFF_PXB_load(tiffile); // make PXB from tif file
remove(tiffile);
zfree(tiffile);
return pxb;
errret:
if (tiffile) remove(tiffile);
if (tiffile) zfree(tiffile);
return 0;
}
// Load PXM pixmap from JP2 file using jpeg2000 utility (opj_decompress).
// Crutch: convert .jp2 file to .tif and load .tif file.
// f_load_bpc = 8 or 16, depending on .jp2 file
PXM * JP2_PXM_load(ch *file)
{
int err;
ch *tiffile = 0;
ch *f1, *f2;
ch *pp;
PXM *pxm = 0;
static int ftf = 1;
ch *installmess = ".jp2 files not supported (install opj_decompress)";
if (! Fjp2) {
if (ftf) { ftf = 0; zmessageACK(Mwin,installmess); }
return 0;
}
if (! regfile(file)) return 0; // no print error
tiffile = zstrdup(file,"JP2_PXM_load",8); // file.jp2 >> file.tif
pp = strrchr(tiffile,'.');
if (! pp) goto errret;
strcpy(pp,".tif");
f1 = zescape_quotes(file);
f2 = zescape_quotes(tiffile);
err = zshell(0,"opj_decompress -i \"%s\" -o \"%s\" >/dev/null 2>1",f1,f2);
zfree(f1);
zfree(f2);
if (err) goto errret;
if (! regfile(tiffile)) goto errret;
pxm = TIFF_PXM_load(tiffile); // make PXM from tif file
remove(tiffile);
zfree(tiffile);
return pxm;
errret:
if (tiffile) remove(tiffile);
if (tiffile) zfree(tiffile);
return 0;
}
int PXB_JP2_save(PXB *pxb, ch *file)
{
zmessageACK(Mwin,"save to .jp2 file not supported");
return 0;
}
int PXM_JP2_save(PXM *pxm, ch *file)
{
zmessageACK(Mwin,"save to .jp2 file not supported");
return 0;
}
/********************************************************************************
WEBP file read functions
(write functions are not implemented)
*********************************************************************************/
// Load PXB pixmap from WEBP file using dwebp utility (webp package).
// Crutch: convert .webp file to .tif and load .tif file.
// f_load_bpc = 8
PXB * WEBP_PXB_load(ch *file)
{
int err;
ch *tiffile = 0;
ch *f1, *f2;
ch *pp;
PXB *pxb;
static int ftf = 1;
ch *installmess = ".webp files not supported (install dwebp)";
if (! Fwebp) {
if (ftf) { ftf = 0; zmessageACK(Mwin,installmess); }
return 0;
}
if (! regfile(file)) return 0; // no print error
tiffile = zstrdup(file,"WEBP_PXB_load",8); // file.webp >> file.tif
pp = strrchr(tiffile,'.');
if (! pp) goto errret;
strcpy(pp,".tif");
f1 = zescape_quotes(file);
f2 = zescape_quotes(tiffile);
err = zshell(0,"dwebp -quiet -tiff \"%s\" -o \"%s\" ",file,tiffile);
zfree(f1);
zfree(f2);
if (err) goto errret;
if (! regfile(tiffile)) goto errret;
pxb = TIFF_PXB_load(tiffile); // make PXB from tif file
remove(tiffile);
zfree(tiffile);
return pxb;
errret:
if (tiffile) remove(tiffile);
if (tiffile) zfree(tiffile);
return 0;
}
// Load PXM pixmap from WEBP file using jpeg2000 utility (opj_decompress).
// Crutch: convert .webp file to .tif and load .tif file.
// f_load_bpc = 8 or 16, depending on .webp file
PXM * WEBP_PXM_load(ch *file)
{
int err;
ch *tiffile = 0;
ch *f1, *f2;
ch *pp;
PXM *pxm = 0;
static int ftf = 1;
ch *installmess = ".webp files not supported (install dwebp)";
if (! Fwebp) {
if (ftf) { ftf = 0; zmessageACK(Mwin,installmess); }
return 0;
}
if (! regfile(file)) return 0; // no print error
tiffile = zstrdup(file,"WEBP_PXM_load",8); // file.webp >> file.tif
pp = strrchr(tiffile,'.');
if (! pp) goto errret;
strcpy(pp,".tif");
f1 = zescape_quotes(file);
f2 = zescape_quotes(tiffile);
err = zshell(0,"dwebp -quiet -tiff \"%s\" -o \"%s\" ",file,tiffile);
zfree(f1);
zfree(f2);
if (err) goto errret;
if (! regfile(tiffile)) goto errret;
pxm = TIFF_PXM_load(tiffile); // make PXM from tif file
remove(tiffile);
zfree(tiffile);
return pxm;
errret:
if (tiffile) remove(tiffile);
if (tiffile) zfree(tiffile);
return 0;
}
int PXB_WEBP_save(PXB *pxb, ch *file)
{
zmessageACK(Mwin,"save to .webp file not supported");
return 0;
}
int PXM_WEBP_save(PXM *pxm, ch *file)
{
zmessageACK(Mwin,"save to .webp file not supported");
return 0;
}
/********************************************************************************
Other file types read and write functions (via gdk_pixbuf library)
*********************************************************************************/
// Load PXB pixmap from other file types using the pixbuf library.
// bpc = 8.
PXB * ANY_PXB_load(ch *file)
{
GError *gerror = 0;
PXB *pxb;
PIXBUF *pixbuf;
int ww, hh, px, py, nc1, nc2, ac, rs;
uint8 *pixels1, *pix1;
uint8 *pixels2, *pix2;
if (! regfile(file)) goto errret; // no print error
pixbuf = gdk_pixbuf_new_from_file(file,&gerror);
if (! pixbuf) goto errret;
ww = gdk_pixbuf_get_width(pixbuf);
hh = gdk_pixbuf_get_height(pixbuf);
nc1 = gdk_pixbuf_get_n_channels(pixbuf);
ac = gdk_pixbuf_get_has_alpha(pixbuf);
rs = gdk_pixbuf_get_rowstride(pixbuf);
pixels1 = gdk_pixbuf_get_pixels(pixbuf);
nc2 = 3 + ac;
pxb = PXB_make(ww,hh,nc2);
pixels2 = pxb->pixels;
for (py = 0; py < hh; py++)
{
pix1 = pixels1 + rs * py;
pix2 = pixels2 + py * pxb->rs;
if (nc1 >= 3)
{
for (px = 0; px < ww; px++)
{
memcpy(pix2,pix1,3);
if (nc1 == 4) pix2[3] = pix1[3]; // uint8 > unit8
pix1 += nc1;
pix2 += nc2;
}
}
else
{
for (px = 0; px < ww; px++)
{
pix2[0] = pix2[1] = pix2[2] = pix1[0];
if (nc1 == 2) pix2[3] = pix1[1];
pix1 += nc1;
pix2 += nc2;
}
}
}
g_object_unref(pixbuf);
return pxb;
errret:
Plog(0,"pixbuf file read error: %s \n",file);
if (gerror) Plog(0,"%s \n",gerror->message);
return 0;
}
// Load PXM pixmap from other file types using the pixbuf library.
// bpc = 8.
PXM * ANY_PXM_load(ch *file)
{
GError *gerror = 0;
PXM *pxm;
PIXBUF *pixbuf;
int ww, hh, px, py, nc1, nc2, ac, rs;
uint8 *pixels1, *pix1;
float *pixels2, *pix2;
if (! regfile(file)) goto errret; // no print error
pixbuf = gdk_pixbuf_new_from_file(file,&gerror);
if (! pixbuf) goto errret;
ww = gdk_pixbuf_get_width(pixbuf);
hh = gdk_pixbuf_get_height(pixbuf);
nc1 = gdk_pixbuf_get_n_channels(pixbuf);
ac = gdk_pixbuf_get_has_alpha(pixbuf);
rs = gdk_pixbuf_get_rowstride(pixbuf);
pixels1 = gdk_pixbuf_get_pixels(pixbuf);
nc2 = 3 + ac;
pxm = PXM_make(ww,hh,nc2);
pixels2 = pxm->pixels;
for (py = 0; py < hh; py++)
{
pix1 = pixels1 + rs * py;
pix2 = pixels2 + py * ww * nc2;
if (nc1 >= 3)
{
for (px = 0; px < ww; px++)
{
pix2[0] = pix1[0]; // uint8 > float
pix2[1] = pix1[1];
pix2[2] = pix1[2];
if (nc1 == 4) pix2[3] = pix1[3];
pix1 += nc1;
pix2 += nc2;
}
}
else
{
for (px = 0; px < ww; px++)
{
pix2[0] = pix2[1] = pix2[2] = pix1[0];
if (ac) pix2[3] = pix1[1];
pix1 += nc1;
pix2 += nc2;
}
}
}
g_object_unref(pixbuf);
PXM_audit(pxm); // audit/repair
return pxm;
errret:
Plog(0,"pixbuf file read error: %s \n",file);
if (gerror) Plog(0,"%s \n",gerror->message);
return 0;
}
/********************************************************************************
RAW file read functions.
There are no write functions.
*********************************************************************************/
int raw_match_embed_color(ch *rawfile, PXM *pxm);
// RAW file to PXB pixmap (pixbuf, 8-bit color)
PXB * RAW_PXB_load(ch *rawfile, int matchthumb)
{
PXB *pxb = 0;
PXM *pxm = 0;
pxm = RAW_PXM_load(rawfile,matchthumb);
if (! pxm) return 0;
pxb = PXM_PXB_copy(pxm);
PXM_free(pxm);
return pxb;
}
// RAW file to PXM pixmap (float color)
PXM * RAW_PXM_load(ch *rawfile, int matchthumb)
{
int err;
PXM *pxm = 0;
ch *pp, *tiffile;
ch command[200];
Funcbusy(+1,1);
strncpy0(command,raw_loader_command,200); // get RAW loader command
strTrim(command);
pp = zescape_quotes(rawfile); // escape quotes in raw file name
err = zshell("log",command,pp); // convert to rawfile.tiff
zfree(pp);
if (err) {
Funcbusy(-1);
return 0;
}
tiffile = zstrdup(rawfile,"RAW_PXM_load",8); // tiff file name = rawfile.tiff
pp = strrchr(tiffile,'/');
pp = strrchr(pp,'.');
if (! pp) pp = tiffile + strlen(tiffile);
strcpy(pp,".tiff");
if (! regfile(tiffile)) strcpy(pp,".tif");
pxm = TIFF_PXM_load(tiffile);
remove(tiffile);
zfree(tiffile);
if (matchthumb) raw_match_embed_color(rawfile,pxm);
Funcbusy(-1);
return pxm;
}
// RAW file to PXB pixmap (pixbuf, 8-bit color, dcraw)
// use 1/2 size output file for faster thumbnail creation
PXB * RAW_PXB_load_dcraw_half(ch *rawfile)
{
int err;
ch *tiffile, *pp;
PXB *pxb;
ch *command = "dcraw -w -T -h \"%s\" "; // -h (half size)
Funcbusy(+1,1);
pp = zescape_quotes(rawfile); // make tiff file from raw file
err = zshell(0,command,pp);
zfree(pp);
if (err) {
Funcbusy(-1);
return 0;
}
tiffile = zstrdup(rawfile,"RAW_PXB_load",8); // tiff file name = rawfile.tiff
pp = strrchr(tiffile,'/');
if (! pp) pp = tiffile;
pp = strrchr(pp,'.');
if (! pp) pp = tiffile + strlen(tiffile);
strcpy(pp,".tiff");
if (! regfile(tiffile)) strcpy(pp,".tif");
pxb = TIFF_PXB_load(tiffile); // make raw PXB from tiff file
remove(tiffile);
zfree(tiffile);
Funcbusy(-1);
return pxb;
}
// Create 512x512 PXB for thumbnail from the embedded image in a RAW file
PXB * RAW_thumb_pxb(ch *rawfile)
{
ch *thumbfile = 0, *pp;
int err, ww, hh, angle = 0;
PXB *Tpxb = 0, *Rpxb = 0;
ch *metakey[1] = { meta_orientation_key };
ch *metadata[1] = { 0 }, orientation = 0;
pp = zescape_quotes(rawfile);
err = zshell(0,"dcraw -e \"%s\" ",pp); // get .jpg or .ppm embedded image
zfree(pp);
if (err) goto getraw; // none there
thumbfile = zstrdup(rawfile,"RAW_thumb_pxb",12); // rawfile.thumb.jpg (or .ppm)
pp = strrchr(thumbfile,'/');
pp = strrchr(pp,'.');
if (! pp) pp = thumbfile + strlen(thumbfile);
strcpy(pp,".thumb.jpg");
if (regfile(thumbfile)) // make thumb PXB
Tpxb = JPG_PXB_load(thumbfile);
else {
strcpy(pp+6,".ppm");
Tpxb = ANY_PXB_load(thumbfile);
}
if (! Tpxb) { // embedded image not usable
remove(thumbfile);
zfree(thumbfile);
goto getraw;
}
meta_get1(thumbfile,metakey,metadata,1); // get thumb image rotation status
if (metadata[0]) orientation = *metadata[0];
else { // no data
meta_get1(rawfile,metakey,metadata,1); // get raw image rotation status
if (metadata[0]) orientation = *metadata[0];
}
remove(thumbfile);
zfree(thumbfile);
ww = Tpxb->ww;
hh = Tpxb->hh;
if (ww < 512 && hh < 512) { // embedded image too small
PXB_free(Tpxb);
goto getraw;
}
Rpxb = PXB_rescale(Tpxb,512); // rescale to 512 pixels
PXB_free(Tpxb);
Tpxb = Rpxb;
if (orientation) {
if (orientation == '6') angle = +90; // rotate clockwise 90 deg.
else if (orientation == '8') angle = -90; // counterclockwise 90 deg.
else if (orientation == '3') angle = 180; // 180 deg.
if (angle) {
Rpxb = PXB_rotate(Tpxb,angle); // rotate pixbuf to upright
PXB_free(Tpxb);
Tpxb = Rpxb;
}
}
return Tpxb;
getraw:
Rpxb = RAW_PXB_load_dcraw_half(rawfile); // extract 1/2 size tiff file
if (! Rpxb) return 0;
Tpxb = PXB_rescale(Rpxb,512); // rescale to 512
PXB_free(Rpxb);
return Tpxb;
}
// Use the RAW file embedded thumbnail as a template for RAW image color balance.
// Input PXM RGB values are revised to match thumbnail histogram.
// returns: 0 = OK, +N = error (PXM not changed).
int raw_match_embed_color(ch *rawfile, PXM *Rpxm)
{
PXB *Tpxb;
ch *thumbfile, *pp;
int ii, jj, kk, err, rgb, step;
int Tpix, Rpix, Tpix3[3], Rpix3[3];
int Tdist[3][1024], Rdist[3][1024], Rval[3][1024];
int Rsum, Tsum, Rbin, Tbin;
uint8 *Tpixel;
float *Rpixel;
pp = zescape_quotes(rawfile); // get embedded thumbnail file
err = zshell(0,"dcraw -e \"%s\" ",pp); // from raw file
zfree(pp);
if (err) return 1;
thumbfile = zstrdup(rawfile,"raw_match_thumb",12); // rawfile.thumb.jpg
pp = strrchr(thumbfile,'/');
pp = strrchr(pp,'.');
if (! pp) pp = thumbfile + strlen(thumbfile);
strcpy(pp,".thumb.jpg");
if (regfile(thumbfile)) Tpxb = JPG_PXB_load(thumbfile); // make thumb PXB
else {
strcpy(pp+6,".ppm");
Tpxb = ANY_PXB_load(thumbfile);
}
remove(thumbfile);
zfree(thumbfile);
if (! Tpxb) return 2;
if (Tpxb->nc < 3) return 3; // thumbnail not RGB
Tpix = Tpxb->ww * Tpxb->hh; // total thumb PXB pixels
Rpix = Rpxm->ww * Rpxm->hh; // total raw PXM pixels
memset(Tdist,0,3*1024*sizeof(int)); // clear thumb distribution
for (ii = 0; ii < Tpix; ii++) // loop thumb pixels
for (rgb = 0; rgb < 3; rgb++)
{
Tpixel = Tpxb->pixels + ii * 3; // build thumb brightness distribution
jj = 4 * Tpixel[rgb]; // for each RGB channel
Tdist[rgb][jj]++;
Tdist[rgb][jj+1] = Tdist[rgb][jj+2] = Tdist[rgb][jj+3] = Tdist[rgb][jj]; // duplicate 4x for 1024 bins
}
for (ii = 0; ii < 255; ii++) // ramp each set of 4 duplicates
for (rgb = 0; rgb < 3; rgb++) // 10 10 10 10 20 20 20 20 40 ...
{ // >> 10 12 15 17 20 25 30 35 40 ...
jj = 4 * ii;
step = Tdist[rgb][jj+4] - Tdist[rgb][jj];
for (kk = 1; kk < 4; kk++)
Tdist[rgb][jj+kk] += kk * step / 4;
}
memset(Rdist,0,3*1024*sizeof(int)); // clear raw distribution
for (ii = 0; ii < Rpix; ii++) // loop raw pixels
for (rgb = 0; rgb < 3; rgb++)
{
Rpixel = Rpxm->pixels + ii * 3; // build raw brightness distribution
jj = 4 * Rpixel[rgb]; // 0.0 - 255.99 >> bins 0 - 1023
if (jj > 1023) jj = 1023;
Rdist[rgb][jj]++;
}
for (rgb = 0; rgb < 3; rgb++)
Tpix3[rgb] = Rpix3[rgb] = 0;
for (ii = 0; ii < 1024; ii++) // get distribution totals
for (rgb = 0; rgb < 3; rgb++)
{
Tpix3[rgb] += Tdist[rgb][ii];
Rpix3[rgb] += Rdist[rgb][ii];
}
for (ii = 0; ii < 1024; ii++) // make Tdist totals = Rdist totals
for (rgb = 0; rgb < 3; rgb++)
Tdist[rgb][ii] *= 1.0 * Rpix3[rgb] / Tpix3[rgb];
for (rgb = 0; rgb < 3; rgb++) // low to high
{
for (Rsum = 0, Rbin = 0; Rbin < 1024; Rbin++)
{
Rsum += Rdist[rgb][Rbin];
for (Tsum = 0, Tbin = 0; Tbin < 1024; Tbin++)
{
Tsum += Tdist[rgb][Tbin];
if (Tsum > Rsum) break;
}
if (Tbin == 256) Tbin = 255;
Rval[rgb][Rbin] = Tbin;
}
}
for (rgb = 0; rgb < 3; rgb++) // high to low
{
for (Rsum = 0, Rbin = 1023; Rbin >= 0; Rbin--)
{
Rsum += Rdist[rgb][Rbin];
for (Tsum = 0, Tbin = 1023; Tbin >= 0; Tbin--)
{
Tsum += Tdist[rgb][Tbin];
if (Tsum > Rsum) break;
}
if (Tbin < 0) Tbin = 0;
Rval[rgb][Rbin] = 0.5 * (Rval[rgb][Rbin] + Tbin); // average two distributions
}
}
for (ii = 0; ii < Rpix; ii++) // loop raw pixels
{
Rpixel = Rpxm->pixels + ii * 3;
for (rgb = 0; rgb < 3; rgb++) // loop RGB color
{
Rbin = 4 * Rpixel[rgb]; // old brightness
Rpixel[rgb] = 0.25 * Rval[rgb][Rbin]; // set new brightness
}
}
return 0;
}
/********************************************************************************
pixel format conversion functions
input pixel buffer: uint8/uint16/float with 1/2/3/4 channels
output pixel buffer: uint8/uint16/float with 3/4 channels
row stride is assumed to have no padding at row ends and no
shortening of the last row (not compatible with GDK pixbufs)
*********************************************************************************/
// copy from uint8 buffer to uint8 buffer
void pixelvert(uint8 *buff1, uint8 *buff2, int ww, int hh, int nc1, int nc2)
{
uint8 *pix1;
uint8 *pix2;
uint rs1 = ww * nc1, rs2 = ww * nc2;
int row, col;
float F;
Funcbusy(+1);
for (row = 0; row < hh; row++)
{
pix1 = buff1 + row * rs1;
pix2 = buff2 + row * rs2;
if (nc1 == 1 && nc2 == 3)
{
for (col = 0; col < ww; col++)
{
memset(pix2,pix1[0],3);
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 1 && nc2 == 4)
{
for (col = 0; col < ww; col++)
{
memset(pix2,pix1[0],3);
pix2[3] = 255;
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 2 && nc2 == 3)
{
for (col = 0; col < ww; col++)
{
memset(pix2,pix1[0],3);
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 2 && nc2 == 4)
{
for (col = 0; col < ww; col++)
{
memset(pix2,pix1[0],3);
pix2[3] = pix1[1];
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 3 && nc2 == 3)
{
for (col = 0; col < ww; col++)
{
memcpy(pix2,pix1,3);
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 3 && nc2 == 4)
{
for (col = 0; col < ww; col++)
{
memcpy(pix2,pix1,3);
pix2[3] = 255;
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 4 && nc2 == 3)
{
for (col = 0; col < ww; col++)
{
if (pix1[3] < 255) { // apply alpha channel
F = 0.003906 * pix1[3];
pix2[0] = F * pix1[0];
pix2[1] = F * pix1[1];
pix2[2] = F * pix1[2];
}
else memcpy(pix2,pix1,3);
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 4 && nc2 == 4)
{
for (col = 0; col < ww; col++)
{
memcpy(pix2,pix1,4);
pix1 += nc1;
pix2 += nc2;
}
}
}
Funcbusy(-1);
return;
}
// copy from uint8 buffer to uint16 buffer
void pixelvert(uint8 *buff1, uint16 *buff2, int ww, int hh, int nc1, int nc2)
{
uint8 *pix1;
uint16 *pix2;
int rs1 = ww * nc1, rs2 = ww * nc2;
int row, col;
float F;
Funcbusy(+1);
for (row = 0; row < hh; row++)
{
pix1 = buff1 + row * rs1;
pix2 = buff2 + row * rs2;
if (nc1 == 1 && nc2 == 3)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix2[1] = pix2[2] = pix1[0] * 256;
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 1 && nc2 == 4)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix2[1] = pix2[2] = pix1[0] * 256;
pix2[3] = 255 * 256;
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 2 && nc2 == 3)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix2[1] = pix2[2] = pix1[0] * 256;
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 2 && nc2 == 4)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix2[1] = pix2[2] = pix1[0] * 256;
pix2[3] = pix1[1] * 256;
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 3 && nc2 == 3)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix1[0] * 256;
pix2[1] = pix1[1] * 256;
pix2[2] = pix1[2] * 256;
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 3 && nc2 == 4)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix1[0] * 256;
pix2[1] = pix1[1] * 256;
pix2[2] = pix1[2] * 256;
pix2[3] = 255 * 256;
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 4 && nc2 == 3)
{
for (col = 0; col < ww; col++)
{
if (pix1[3] < 255) { // apply alpha channel 24.10
F = 0.003906 * pix1[3] * 256;
pix2[0] = pix1[0] * F;
pix2[1] = pix1[1] * F;
pix2[2] = pix1[2] * F;
}
else {
pix2[0] = pix1[0] * 256;
pix2[1] = pix1[1] * 256;
pix2[2] = pix1[2] * 256;
}
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 4 && nc2 == 4)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix1[0] * 256;
pix2[1] = pix1[1] * 256;
pix2[2] = pix1[2] * 256;
pix2[3] = pix1[3] * 256;
pix1 += nc1;
pix2 += nc2;
}
}
}
Funcbusy(-1);
return;
}
// copy from uint8 buffer to float buffer
void pixelvert(uint8 *buff1, float *buff2, int ww, int hh, int nc1, int nc2)
{
uint8 *pix1;
float *pix2;
int rs1 = ww * nc1, rs2 = ww * nc2;
int row, col;
float F;
Funcbusy(+1);
for (row = 0; row < hh; row++)
{
pix1 = buff1 + row * rs1;
pix2 = buff2 + row * rs2;
if (nc1 == 1 && nc2 == 3)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix2[1] = pix2[2] = pix1[0];
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 1 && nc2 == 4)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix2[1] = pix2[2] = pix1[0];
pix2[3] = 255;
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 2 && nc2 == 3)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix2[1] = pix2[2] = pix1[0];
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 2 && nc2 == 4)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix2[1] = pix2[2] = pix1[0];
pix2[3] = pix1[1];
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 3 && nc2 == 3)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix1[0];
pix2[1] = pix1[1];
pix2[2] = pix1[2];
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 3 && nc2 == 4)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix1[0];
pix2[1] = pix1[1];
pix2[2] = pix1[2];
pix2[3] = 255;
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 4 && nc2 == 3)
{
for (col = 0; col < ww; col++)
{
if (pix1[3] < 255) { // apply alpha channel 24.10
F = 0.003906 * pix1[3];
pix2[0] = F * pix1[0];
pix2[1] = F * pix1[1];
pix2[2] = F * pix1[2];
}
else {
pix2[0] = pix1[0];
pix2[1] = pix1[1];
pix2[2] = pix1[2];
}
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 4 && nc2 == 4)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix1[0];
pix2[1] = pix1[1];
pix2[2] = pix1[2];
pix2[3] = pix1[3];
pix1 += nc1;
pix2 += nc2;
}
}
}
Funcbusy(-1);
return;
}
// copy from uint16 buffer to uint8 buffer
void pixelvert(uint16 *buff1, uint8 *buff2, int ww, int hh, int nc1, int nc2)
{
uint16 *pix1;
uint8 *pix2;
int rs1 = ww * nc1, rs2 = ww * nc2;
int row, col;
float F;
Funcbusy(+1);
for (row = 0; row < hh; row++)
{
pix1 = buff1 + row * rs1;
pix2 = buff2 + row * rs2;
if (nc1 == 1 && nc2 == 3)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix2[1] = pix2[2] = pix1[0] >> 8;
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 1 && nc2 == 4)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix2[1] = pix2[2] = pix1[0] >> 8;
pix2[3] = 255;
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 2 && nc2 == 3)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix2[1] = pix2[2] = pix1[0] >> 8;
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 2 && nc2 == 4)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix2[1] = pix2[2] = pix1[0] >> 8;
pix2[3] = pix1[1] >> 8;
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 3 && nc2 == 3)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix1[0] >> 8;
pix2[1] = pix1[1] >> 8;
pix2[2] = pix1[2] >> 8;
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 3 && nc2 == 4)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix1[0] >> 8;
pix2[1] = pix1[1] >> 8;
pix2[2] = pix1[2] >> 8;
pix2[3] = 255;
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 4 && nc2 == 3)
{
for (col = 0; col < ww; col++)
{
if (pix1[3] < 65500) { // apply alpha channel 24.10
F = 0.000015258 * pix1[3];
pix2[0] = (pix1[0] >> 8) * F;
pix2[1] = (pix1[1] >> 8) * F;
pix2[2] = (pix1[2] >> 8) * F;
}
else {
pix2[0] = pix1[0] >> 8;
pix2[1] = pix1[1] >> 8;
pix2[2] = pix1[2] >> 8;
}
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 4 && nc2 == 4)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix1[0] >> 8;
pix2[1] = pix1[1] >> 8;
pix2[2] = pix1[2] >> 8;
pix2[3] = pix1[3] >> 8;
pix1 += nc1;
pix2 += nc2;
}
}
}
Funcbusy(-1);
return;
}
// copy from uint16 buffer to uint16 buffer
void pixelvert(uint16 *buff1, uint16 *buff2, int ww, int hh, int nc1, int nc2)
{
uint16 *pix1;
uint16 *pix2;
int rs1 = ww * nc1, rs2 = ww * nc2;
int row, col;
float F;
Funcbusy(+1);
for (row = 0; row < hh; row++)
{
pix1 = buff1 + row * rs1;
pix2 = buff2 + row * rs2;
if (nc1 == 1 && nc2 == 3)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix2[1] = pix2[2] = pix1[0];
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 1 && nc2 == 4)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix2[1] = pix2[2] = pix1[0];
pix2[3] = 255;
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 2 && nc2 == 3)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix2[1] = pix2[2] = pix1[0];
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 2 && nc2 == 4)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix2[1] = pix2[2] = pix1[0];
pix2[3] = pix1[1];
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 3 && nc2 == 3)
{
for (col = 0; col < ww; col++)
{
memcpy(pix2,pix1,6);
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 3 && nc2 == 4)
{
for (col = 0; col < ww; col++)
{
memcpy(pix2,pix1,6);
pix2[3] = 255 * 256;
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 4 && nc2 == 3)
{
for (col = 0; col < ww; col++)
{
if (pix1[3] < 65500) { // apply alpha channel 24.10
F = pix1[3] * 0.000015258;
pix2[0] = F * pix1[0];
pix2[1] = F * pix1[1];
pix2[2] = F * pix1[2];
}
else memcpy(pix2,pix1,6);
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 4 && nc2 == 4)
{
for (col = 0; col < ww; col++)
{
memcpy(pix2,pix1,8);
pix1 += nc1;
pix2 += nc2;
}
}
}
Funcbusy(-1);
return;
}
// copy from uint16 buffer to float buffer
void pixelvert(uint16 *buff1, float *buff2, int ww, int hh, int nc1, int nc2)
{
uint16 *pix1;
float *pix2;
int rs1 = ww * nc1, rs2 = ww * nc2;
int row, col;
float F;
Funcbusy(+1);
for (row = 0; row < hh; row++)
{
pix1 = buff1 + row * rs1;
pix2 = buff2 + row * rs2;
if (nc1 == 1 && nc2 == 3)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix2[1] = pix2[2] = pix1[0] * 0.003906;
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 1 && nc2 == 4)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix2[1] = pix2[2] = pix1[0] * 0.003906;
pix2[3] = 255;
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 2 && nc2 == 3)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix2[1] = pix2[2] = pix1[0] * 0.003906;
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 2 && nc2 == 4)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix2[1] = pix2[2] = pix1[0] * 0.003906;
pix2[3] = pix1[1] * 0.003906;
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 3 && nc2 == 3)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix1[0] * 0.003906;
pix2[1] = pix1[1] * 0.003906;
pix2[2] = pix1[2] * 0.003906;
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 3 && nc2 == 4)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix1[0] * 0.003906;
pix2[1] = pix1[1] * 0.003906;
pix2[2] = pix1[2] * 0.003906;
pix2[3] = 255;
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 4 && nc2 == 3)
{
for (col = 0; col < ww; col++)
{
if (pix1[3] < 65500) { // apply alpha channel 24.10
F = pix1[3] * 0.000015258 * 0.003906;
pix2[0] = pix1[0] * F;
pix2[1] = pix1[1] * F;
pix2[2] = pix1[2] * F;
}
else {
pix2[0] = pix1[0] * 0.003906;
pix2[1] = pix1[1] * 0.003906;
pix2[2] = pix1[2] * 0.003906;
}
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 4 && nc2 == 4)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix1[0] * 0.003906;
pix2[1] = pix1[1] * 0.003906;
pix2[2] = pix1[2] * 0.003906;
pix2[3] = pix1[3] * 0.003906;
pix1 += nc1;
pix2 += nc2;
}
}
}
Funcbusy(-1);
return;
}
// copy from float buffer to uint8 buffer
void pixelvert(float *buff1, uint8 *buff2, int ww, int hh, int nc1, int nc2)
{
float *pix1;
uint8 *pix2;
int rs1 = ww * nc1, rs2 = ww * nc2;
int row, col;
float F;
Funcbusy(+1);
for (row = 0; row < hh; row++)
{
pix1 = buff1 + row * rs1;
pix2 = buff2 + row * rs2;
if (nc1 == 1 && nc2 == 3)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix2[1] = pix2[2] = pix1[0];
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 1 && nc2 == 4)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix2[1] = pix2[2] = pix1[0];
pix2[3] = 255;
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 2 && nc2 == 3)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix2[1] = pix2[2] = pix1[0];
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 2 && nc2 == 4)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix2[1] = pix2[2] = pix1[0];
pix2[3] = pix1[1];
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 3 && nc2 == 3)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix1[0];
pix2[1] = pix1[1];
pix2[2] = pix1[2];
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 3 && nc2 == 4)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix1[0];
pix2[1] = pix1[1];
pix2[2] = pix1[2];
pix2[3] = 255;
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 4 && nc2 == 3)
{
for (col = 0; col < ww; col++)
{
if (pix1[3] < 255) { // apply alpha channel 24.10
F = 0.003906 * pix1[3];
pix2[0] = F * pix1[0];
pix2[1] = F * pix1[1];
pix2[2] = F * pix1[2];
}
else {
pix2[0] = pix1[0];
pix2[1] = pix1[1];
pix2[2] = pix1[2];
}
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 4 && nc2 == 4)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix1[0];
pix2[1] = pix1[1];
pix2[2] = pix1[2];
pix2[3] = pix1[3];
pix1 += nc1;
pix2 += nc2;
}
}
}
Funcbusy(-1);
return;
}
// copy from float buffer to uint16 buffer
void pixelvert(float *buff1, uint16 *buff2, int ww, int hh, int nc1, int nc2)
{
float *pix1;
uint16 *pix2;
int rs1 = ww * nc1, rs2 = ww * nc2;
int row, col;
float F;
Funcbusy(+1);
for (row = 0; row < hh; row++)
{
pix1 = buff1 + row * rs1;
pix2 = buff2 + row * rs2;
if (nc1 == 1 && nc2 == 3)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix2[1] = pix2[2] = pix1[0] * 255.9;
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 1 && nc2 == 4)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix2[1] = pix2[2] = pix1[0] * 255.9;
pix2[3] = 255 * 256;
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 2 && nc2 == 3)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix2[1] = pix2[2] = pix1[0] * 255.9;
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 2 && nc2 == 4)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix2[1] = pix2[2] = pix1[0] * 255.9;
pix2[3] = pix1[1] * 255.9;
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 3 && nc2 == 3)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix1[0] * 255.9;
pix2[1] = pix1[1] * 255.9;
pix2[2] = pix1[2] * 255.9;
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 3 && nc2 == 4)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix1[0] * 255.9;
pix2[1] = pix1[1] * 255.9;
pix2[2] = pix1[2] * 255.9;
pix2[3] = 255 * 256;
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 4 && nc2 == 3)
{
for (col = 0; col < ww; col++)
{
if (pix1[3] < 255) { // apply alpha channel 24.10
F = 0.003906 * pix1[3] * 255.9;
pix2[0] = F * pix1[0];
pix2[1] = F * pix1[1];
pix2[2] = F * pix1[2];
}
else {
pix2[0] = pix1[0] * 255.9;
pix2[1] = pix1[1] * 255.9;
pix2[2] = pix1[2] * 255.9;
}
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 4 && nc2 == 4)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix1[0] * 255.9;
pix2[1] = pix1[1] * 255.9;
pix2[2] = pix1[2] * 255.9;
pix2[3] = pix1[3] * 255.9;
pix1 += nc1;
pix2 += nc2;
}
}
}
Funcbusy(-1);
return;
}
// copy from float buffer to float buffer
void pixelvert(float *buff1, float *buff2, int ww, int hh, int nc1, int nc2)
{
float *pix1;
float *pix2;
int rs1 = ww * nc1, rs2 = ww * nc2;
int row, col;
float F;
Funcbusy(+1);
for (row = 0; row < hh; row++)
{
pix1 = buff1 + row * rs1;
pix2 = buff2 + row * rs2;
if (nc1 == 1 && nc2 == 3)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix2[1] = pix2[2] = pix1[0];
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 1 && nc2 == 4)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix2[1] = pix2[2] = pix1[0];
pix2[3] = 255;
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 2 && nc2 == 3)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix2[1] = pix2[2] = pix1[0];
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 2 && nc2 == 4)
{
for (col = 0; col < ww; col++)
{
pix2[0] = pix2[1] = pix2[2] = pix1[0];
pix2[3] = pix1[1];
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 3 && nc2 == 3)
{
for (col = 0; col < ww; col++)
{
memcpy(pix2, pix1, 3 * sizeof(float));
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 3 && nc2 == 4)
{
for (col = 0; col < ww; col++)
{
memcpy(pix2, pix1, 3 * sizeof(float));
pix2[3] = 255;
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 4 && nc2 == 3)
{
for (col = 0; col < ww; col++)
{
if (pix1[3] < 255) { // apply alpha channel 24.10
F = 0.003906 * pix1[3];
pix2[0] = F * pix1[0];
pix2[1] = F * pix1[1];
pix2[2] = F * pix1[2];
}
else memcpy(pix2, pix1, 3 * sizeof(float));
pix1 += nc1;
pix2 += nc2;
}
}
if (nc1 == 4 && nc2 == 4)
{
for (col = 0; col < ww; col++)
{
memcpy(pix2, pix1, 4 * sizeof(float));
pix1 += nc1;
pix2 += nc2;
}
}
}
Funcbusy(-1);
return;
}
|