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
|
/* $Id: cpl_image_basic.c,v 1.208 2013-07-24 09:45:53 llundin Exp $
*
* This file is part of the ESO Common Pipeline Library
* Copyright (C) 2001-2008 European Southern Observatory
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* $Author: llundin $
* $Date: 2013-07-24 09:45:53 $
* $Revision: 1.208 $
* $Name: not supported by cvs2svn $
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
/*-----------------------------------------------------------------------------
Includes
-----------------------------------------------------------------------------*/
#include <complex.h>
#include "cpl_image_basic_impl.h"
#include "cpl_image_io_impl.h"
#include "cpl_memory.h"
#include "cpl_vector.h"
#include "cpl_image_stats.h"
#include "cpl_stats_impl.h"
#include "cpl_image_bpm.h"
#include "cpl_image_iqe.h"
#include "cpl_mask_impl.h"
#include "cpl_tools.h"
#include "cpl_errorstate.h"
#include "cpl_error_impl.h"
#include "cpl_math_const.h"
#include "cpl_image_defs.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include <complex.h>
#include <assert.h>
#include <errno.h>
/* for SIZE_MAX, intptr_t */
#include <stdint.h>
/*-----------------------------------------------------------------------------
Defines
-----------------------------------------------------------------------------*/
#define CPL_IMAGE_BASIC_ASSIGN 1
#define CPL_IMAGE_BASIC_ASSIGN_LOCAL 2
#define CPL_IMAGE_BASIC_THRESHOLD 3
#define CPL_IMAGE_BASIC_ABS 4
#define CPL_IMAGE_BASIC_AVERAGE 5
#define CPL_IMAGE_BASIC_SUBMIN 6
#define CPL_IMAGE_BASIC_EXTRACT 7
#define CPL_IMAGE_BASIC_EXTRACTROW 8
#define CPL_IMAGE_BASIC_EXTRACTCOL 9
#define CPL_IMAGE_BASIC_COLLAPSE_MEDIAN 11
#define CPL_IMAGE_BASIC_ROTATE_INT_LOCAL 12
#define CPL_IMAGE_BASIC_FLIP_LOCAL 15
#define CPL_IMAGE_BASIC_MOVE_PIXELS 16
#define CPL_IMAGE_BASIC_OP_SCALAR 21
#define CPL_IMAGE_BASIC_SQRT 22
#define CPL_IMAGE_BASIC_DECLARE 23
#define CPL_IMAGE_BASIC_OPERATE 24
#define CPL_IMAGE_BASIC_DIVIDE 25
#define CPL_IMAGE_BASIC_OPERATE_LOCAL 26
#define CPL_IMAGE_BASIC_DIVIDE_LOCAL 27
#define CPL_IMAGE_BASIC_HYPOT 28
#define CPL_IMAGE_ADDITION(a,b,c) a = (b) + (c)
#define CPL_IMAGE_ADDITIONASSIGN(a,b) a += (b)
#define CPL_IMAGE_SUBTRACTION(a,b,c) a = (b) - (c)
#define CPL_IMAGE_SUBTRACTIONASSIGN(a,b) a -= (b)
#define CPL_IMAGE_MULTIPLICATION(a,b,c) a = (b) * (c)
#define CPL_IMAGE_MULTIPLICATIONASSIGN(a,b) a *= (b)
#define CPL_IMAGE_DIVISION(a,b,c) a = (b) / (c)
#define CPL_IMAGE_DIVISIONASSIGN(a,b) a /= (b)
#define CPL_IMAGE_MINABS(a,b,c) a = CPL_MATH_ABS1(b) < CPL_MATH_ABS2(c) ? (b) : (c)
/*-----------------------------------------------------------------------------
Private Function prototypes
-----------------------------------------------------------------------------*/
static double cpl_vector_get_noise(const cpl_vector *, cpl_size);
static double cpl_vector_get_fwhm(const cpl_vector *, cpl_size, double);
static cpl_error_code cpl_fft(double *, double *, const unsigned *, int, int);
/* Declare and define the 8 hypot functions */
#define CPL_OPERATION CPL_IMAGE_BASIC_HYPOT
#define CPL_TYPE_T1 CPL_TYPE_FLOAT
#define CPL_TYPE1 float
#define CPL_TYPE_T2 CPL_TYPE_FLOAT
#define CPL_TYPE_T3 CPL_TYPE_FLOAT
#define CPL_TYPE2 float
#define CPL_TYPE3 float
#define CPL_HYPOT hypotf
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T2
#undef CPL_TYPE_T3
#undef CPL_TYPE2
#undef CPL_TYPE3
#undef CPL_HYPOT
#define CPL_TYPE_T2 CPL_TYPE_FLOAT
#define CPL_TYPE_T3 CPL_TYPE_DOUBLE
#define CPL_TYPE2 float
#define CPL_TYPE3 double
#define CPL_HYPOT hypot
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T2
#undef CPL_TYPE_T3
#undef CPL_TYPE2
#undef CPL_TYPE3
#define CPL_TYPE_T2 CPL_TYPE_DOUBLE
#define CPL_TYPE_T3 CPL_TYPE_FLOAT
#define CPL_TYPE2 double
#define CPL_TYPE3 float
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T2
#undef CPL_TYPE_T3
#undef CPL_TYPE2
#undef CPL_TYPE3
#define CPL_TYPE_T2 CPL_TYPE_DOUBLE
#define CPL_TYPE_T3 CPL_TYPE_DOUBLE
#define CPL_TYPE2 double
#define CPL_TYPE3 double
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T2
#undef CPL_TYPE_T3
#undef CPL_TYPE2
#undef CPL_TYPE3
#undef CPL_TYPE_T1
#undef CPL_TYPE1
#define CPL_TYPE_T1 CPL_TYPE_DOUBLE
#define CPL_TYPE1 double
#undef CPL_HYPOT
#define CPL_TYPE_T2 CPL_TYPE_FLOAT
#define CPL_TYPE_T3 CPL_TYPE_FLOAT
#define CPL_TYPE2 float
#define CPL_TYPE3 float
#define CPL_HYPOT hypotf
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T2
#undef CPL_TYPE_T3
#undef CPL_TYPE2
#undef CPL_TYPE3
#undef CPL_HYPOT
#define CPL_TYPE_T2 CPL_TYPE_FLOAT
#define CPL_TYPE_T3 CPL_TYPE_DOUBLE
#define CPL_TYPE2 float
#define CPL_TYPE3 double
#define CPL_HYPOT hypot
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T2
#undef CPL_TYPE_T3
#undef CPL_TYPE2
#undef CPL_TYPE3
#define CPL_TYPE_T2 CPL_TYPE_DOUBLE
#define CPL_TYPE_T3 CPL_TYPE_FLOAT
#define CPL_TYPE2 double
#define CPL_TYPE3 float
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T2
#undef CPL_TYPE_T3
#undef CPL_TYPE2
#undef CPL_TYPE3
#define CPL_TYPE_T2 CPL_TYPE_DOUBLE
#define CPL_TYPE_T3 CPL_TYPE_DOUBLE
#define CPL_TYPE2 double
#define CPL_TYPE3 double
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T2
#undef CPL_TYPE_T3
#undef CPL_TYPE2
#undef CPL_TYPE3
#undef CPL_TYPE_T1
#undef CPL_TYPE1
#undef CPL_HYPOT
#undef CPL_OPERATION
/* Declare and define the C-type dependent functions */
#define CPL_OPERATION CPL_IMAGE_BASIC_DECLARE
#define CPL_CLASS CPL_CLASS_DOUBLE
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_FLOAT
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_INT
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#undef CPL_OPERATION
#ifdef __SSE3__
#include <pmmintrin.h>
#endif
#ifdef __SSE2__
#include <xmmintrin.h>
#ifdef __clang__
# define cpl_m_from_int64 (__m64)
# else
# define cpl_m_from_int64 _m_from_int64
# endif
#endif
#if defined __SSE2__ || defined __SSE3__
#if defined __SSE3__
#define CPL_MM_ADDSUB_PS(a, b) _mm_addsub_ps(a, b)
#define CPL_MM_ADDSUB_PD(a, b) _mm_addsub_pd(a, b)
#else
/* faster than multiplying with 1,-1,1,-1 */
#define CPL_MM_ADDSUB_PS(a, b) \
_mm_add_ps(a, _mm_xor_ps(b, (__m128)_mm_set_epi32(0x0u, 0x80000000u, \
0x0u, 0x80000000u)))
#define CPL_MM_ADDSUB_PD(a, b) \
_mm_add_pd(a, _mm_xor_pd(b, (__m128d)_mm_set_epi64(cpl_m_from_int64(0x0llu), \
cpl_m_from_int64(0x8000000000000000llu))))
#endif
static cpl_error_code cpl_image_multiply_fcomplex_sse_(cpl_image *,
const cpl_image *)
CPL_ATTR_NONNULL;
static cpl_error_code cpl_image_multiply_dcomplex_sse_(cpl_image *,
const cpl_image *)
CPL_ATTR_NONNULL;
#endif
/*-----------------------------------------------------------------------------
Function codes
-----------------------------------------------------------------------------*/
#define CPL_OPERATION CPL_IMAGE_BASIC_ASSIGN
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Add two images.
@param image1 first operand
@param image2 second operand
@return 1 newly allocated image or NULL on error
Creates a new image, being the result of the operation, and returns it to
the caller. The returned image must be deallocated using cpl_image_delete().
The function supports images with different types among CPL_TYPE_INT,
CPL_TYPE_FLOAT and CPL_TYPE_DOUBLE. The returned image type is the one of the
first passed image.
The bad pixels map of the result is the union of the bad pixels maps of
the input images.
Possible #_cpl_error_code_ set in this function:
- CPL_ERROR_NULL_INPUT if an input pointer is NULL
- CPL_ERROR_INCOMPATIBLE_INPUT if the input images have different sizes
- CPL_ERROR_TYPE_MISMATCH if the second input image has complex type
while the first one does not
*/
/*----------------------------------------------------------------------------*/
cpl_image * cpl_image_add_create(
const cpl_image * image1,
const cpl_image * image2)
{
#define CPL_OPERATOR CPL_IMAGE_ADDITION
#include "cpl_image_basic_body.h"
#undef CPL_OPERATOR
}
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Subtract two images.
@param image1 first operand
@param image2 second operand
@return 1 newly allocated image or NULL on error
@see cpl_image_add_create()
*/
/*----------------------------------------------------------------------------*/
cpl_image * cpl_image_subtract_create(
const cpl_image * image1,
const cpl_image * image2)
{
#define CPL_OPERATOR CPL_IMAGE_SUBTRACTION
#include "cpl_image_basic_body.h"
#undef CPL_OPERATOR
}
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Multiply two images.
@param image1 first operand
@param image2 second operand
@return 1 newly allocated image or NULL on error
@see cpl_image_add_create()
*/
/*----------------------------------------------------------------------------*/
cpl_image * cpl_image_multiply_create(
const cpl_image * image1,
const cpl_image * image2)
{
#define CPL_OPERATOR CPL_IMAGE_MULTIPLICATION
#include "cpl_image_basic_body.h"
#undef CPL_OPERATOR
}
/*----------------------------------------------------------------------------*/
/**
@internal
@ingroup cpl_image
@brief Create the minimum of two images.
@param image1 first operand
@param image2 second operand
@return 1 newly allocated image or NULL on error
@see cpl_image_add_create()
@note For each pixel position the new value is the one with the
absolute minimum
*/
/*----------------------------------------------------------------------------*/
cpl_image * cpl_image_min_create(
const cpl_image * image1,
const cpl_image * image2)
{
#define CPL_OPERATOR CPL_IMAGE_MINABS
#include "cpl_image_basic_body.h"
#undef CPL_OPERATOR
}
#undef CPL_OPERATION
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Divide two images.
@param image1 first operand
@param image2 second operand
@return 1 newly allocated image or NULL on error
@see cpl_image_add_create()
@see cpl_image_divide()
The result of division with a zero-valued pixel is marked as a bad
pixel.
Possible #_cpl_error_code_ set in this function:
- CPL_ERROR_NULL_INPUT if an input pointer is NULL
- CPL_ERROR_INCOMPATIBLE_INPUT if the input images have different sizes
- CPL_ERROR_TYPE_MISMATCH if the second input image has complex type
while the first one does not
- CPL_ERROR_DIVISION_BY_ZERO is all pixels in the divisor are zero
*/
/*----------------------------------------------------------------------------*/
cpl_image * cpl_image_divide_create(const cpl_image * image1,
const cpl_image * image2)
{
#define CPL_OPERATION CPL_IMAGE_BASIC_DIVIDE
cpl_image * self;
cpl_mask * zeros;
cpl_binary * pzeros;
cpl_size nzero = 0;
cpl_ensure(image1 != NULL, CPL_ERROR_NULL_INPUT, NULL);
cpl_ensure(image2 != NULL, CPL_ERROR_NULL_INPUT, NULL);
cpl_ensure(image1->nx == image2->nx, CPL_ERROR_INCOMPATIBLE_INPUT, NULL);
cpl_ensure(image1->ny == image2->ny, CPL_ERROR_INCOMPATIBLE_INPUT, NULL);
/* Create the map of zero-divisors */
zeros = cpl_mask_new(image2->nx, image2->ny);
pzeros = cpl_mask_get_data(zeros);
/* Switch on the first passed image type */
switch (image1->type) {
case CPL_TYPE_INT: {
const int * p1 = (const int *)image1->pixels;
int * pout = (int *)cpl_malloc(image1->nx * image1->ny *
sizeof(*pout));
/* Switch on the second passed image type */
switch (image2->type) {
#define CPL_TYPE_T CPL_TYPE_INT
#define CPL_TYPE int
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T
#undef CPL_TYPE
#define CPL_TYPE_T CPL_TYPE_FLOAT
#define CPL_TYPE float
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T
#undef CPL_TYPE
#define CPL_TYPE_T CPL_TYPE_DOUBLE
#define CPL_TYPE double
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T
#undef CPL_TYPE
default:
cpl_free(pout);
(void)cpl_error_set_(CPL_ERROR_TYPE_MISMATCH);
pout = NULL;
}
self = pout ? cpl_image_wrap_int(image1->nx, image1->ny, pout) : NULL;
break;
}
case CPL_TYPE_FLOAT: {
const float * p1 = (const float *)image1->pixels;
float * pout = (float *)cpl_malloc(image1->nx * image1->ny *
sizeof(*pout));
/* Switch on the second passed image type */
switch (image2->type) {
#define CPL_TYPE_T CPL_TYPE_INT
#define CPL_TYPE int
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T
#undef CPL_TYPE
#define CPL_TYPE_T CPL_TYPE_FLOAT
#define CPL_TYPE float
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T
#undef CPL_TYPE
#define CPL_TYPE_T CPL_TYPE_DOUBLE
#define CPL_TYPE double
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T
#undef CPL_TYPE
default:
cpl_free(pout);
(void)cpl_error_set_(CPL_ERROR_TYPE_MISMATCH);
pout = NULL;
}
self = pout ? cpl_image_wrap_float(image1->nx, image1->ny, pout) : NULL;
break;
}
case CPL_TYPE_DOUBLE: {
const double * p1 = (const double *)image1->pixels;
double * pout = (double *)cpl_malloc(image1->nx * image1->ny *
sizeof(*pout));
/* Switch on the second passed image type */
switch (image2->type) {
#define CPL_TYPE_T CPL_TYPE_INT
#define CPL_TYPE int
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T
#undef CPL_TYPE
#define CPL_TYPE_T CPL_TYPE_FLOAT
#define CPL_TYPE float
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T
#undef CPL_TYPE
#define CPL_TYPE_T CPL_TYPE_DOUBLE
#define CPL_TYPE double
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T
#undef CPL_TYPE
default:
cpl_free(pout);
(void)cpl_error_set_(CPL_ERROR_TYPE_MISMATCH);
pout = NULL;
}
self = pout ? cpl_image_wrap_double(image1->nx, image1->ny, pout) : NULL;
break;
}
case CPL_TYPE_FLOAT_COMPLEX: {
const float complex * p1 = (const float complex *)image1->pixels;
float complex * pout = (float complex *)cpl_malloc(image1->nx *
image1->ny *
sizeof(*pout));
/* Switch on the second passed image type */
switch (image2->type) {
#define CPL_TYPE_T CPL_TYPE_INT
#define CPL_TYPE int
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T
#undef CPL_TYPE
#define CPL_TYPE_T CPL_TYPE_FLOAT
#define CPL_TYPE float
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T
#undef CPL_TYPE
#define CPL_TYPE_T CPL_TYPE_DOUBLE
#define CPL_TYPE double
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T
#undef CPL_TYPE
#define CPL_TYPE_T CPL_TYPE_FLOAT_COMPLEX
#define CPL_TYPE float complex
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T
#undef CPL_TYPE
#define CPL_TYPE_T CPL_TYPE_DOUBLE_COMPLEX
#define CPL_TYPE double complex
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T
#undef CPL_TYPE
default:
cpl_free(pout);
(void)cpl_error_set_(CPL_ERROR_INVALID_TYPE);
pout = NULL;
}
self = pout ? cpl_image_wrap_float_complex(image1->nx, image1->ny, pout)
: NULL;
break;
}
case CPL_TYPE_DOUBLE_COMPLEX: {
const double complex * p1 = (const double complex *)image1->pixels;
double complex * pout = (double complex *)cpl_malloc(image1->nx *
image1->ny *
sizeof(*pout));
/* Switch on the second passed image type */
switch (image2->type) {
#define CPL_TYPE_T CPL_TYPE_INT
#define CPL_TYPE int
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T
#undef CPL_TYPE
#define CPL_TYPE_T CPL_TYPE_FLOAT
#define CPL_TYPE float
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T
#undef CPL_TYPE
#define CPL_TYPE_T CPL_TYPE_DOUBLE
#define CPL_TYPE double
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T
#undef CPL_TYPE
#define CPL_TYPE_T CPL_TYPE_FLOAT_COMPLEX
#define CPL_TYPE float complex
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T
#undef CPL_TYPE
#define CPL_TYPE_T CPL_TYPE_DOUBLE_COMPLEX
#define CPL_TYPE double complex
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T
#undef CPL_TYPE
default:
cpl_free(pout);
(void)cpl_error_set_(CPL_ERROR_INVALID_TYPE);
pout = NULL;
}
self = pout ? cpl_image_wrap_double_complex(image1->nx, image1->ny,
pout) : NULL;
break;
}
default:
(void)cpl_error_set_(CPL_ERROR_INVALID_TYPE);
self = NULL;
}
if (nzero == image1->nx * image1->ny) {
cpl_image_delete(self);
(void)cpl_error_set_(CPL_ERROR_DIVISION_BY_ZERO);
self = NULL;
}
if (self == NULL) {
cpl_mask_delete(zeros);
} else {
/* Handle bad pixels map */
if (image1->bpm == NULL && image2->bpm == NULL) {
self->bpm = NULL;
} else if (image1->bpm == NULL) {
self->bpm = cpl_mask_duplicate(image2->bpm);
} else if (image2->bpm == NULL) {
self->bpm = cpl_mask_duplicate(image1->bpm);
} else {
self->bpm = cpl_mask_duplicate(image1->bpm);
cpl_mask_or(self->bpm, image2->bpm);
}
/* Handle division by zero in the BPM */
if (nzero != 0) {
if (self->bpm == NULL) {
self->bpm = zeros;
} else {
cpl_mask_or(self->bpm, zeros);
cpl_mask_delete(zeros);
}
} else {
cpl_mask_delete(zeros);
}
if (image1->type != CPL_TYPE_INT && image2->type != CPL_TYPE_INT) {
cpl_tools_add_flops( image1->nx * image1->ny - nzero);
}
}
return self;
#undef CPL_OPERATION
}
#define CPL_OPERATION CPL_IMAGE_BASIC_ASSIGN_LOCAL
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Add two images, store the result in the first image.
@param im1 first operand.
@param im2 second operand.
@return the #_cpl_error_code_ or CPL_ERROR_NONE
The first input image is modified to contain the result of the operation.
The bad pixel map of the first image becomes the union of the bad pixel
maps of the input images.
Possible #_cpl_error_code_ set in this function:
- CPL_ERROR_NULL_INPUT if an input pointer is NULL
- CPL_ERROR_INCOMPATIBLE_INPUT if the input images have different sizes
- CPL_ERROR_TYPE_MISMATCH if the second input image has complex type
while the first one does not
*/
/*----------------------------------------------------------------------------*/
cpl_error_code cpl_image_add(
cpl_image * im1,
const cpl_image * im2)
{
#define CPL_OPERATOR CPL_IMAGE_ADDITIONASSIGN
#include "cpl_image_basic_body.h"
#undef CPL_OPERATOR
}
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Subtract two images, store the result in the first image.
@param im1 first operand.
@param im2 second operand.
@return the #_cpl_error_code_ or CPL_ERROR_NONE
@see cpl_image_add()
*/
/*----------------------------------------------------------------------------*/
cpl_error_code cpl_image_subtract(
cpl_image * im1,
const cpl_image * im2)
{
#define CPL_OPERATOR CPL_IMAGE_SUBTRACTIONASSIGN
#include "cpl_image_basic_body.h"
#undef CPL_OPERATOR
}
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Multiply two images, store the result in the first image.
@param im1 first operand.
@param im2 second operand.
@return the #_cpl_error_code_ or CPL_ERROR_NONE
@see cpl_image_add()
*/
/*----------------------------------------------------------------------------*/
cpl_error_code cpl_image_multiply(
cpl_image * im1,
const cpl_image * im2)
{
/* Faster version of code generated with gcc -ffast-math */
/* (NaNs and other float specials are no longer IEEE compliant) */
#if (defined __SSE3__ || defined __SSE2__)
cpl_ensure_code(im1 != NULL, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(im2 != NULL, CPL_ERROR_NULL_INPUT);
if (im1->type == CPL_TYPE_FLOAT_COMPLEX &&
im2->type == CPL_TYPE_FLOAT_COMPLEX) {
cpl_ensure_code(im1->nx == im2->nx, CPL_ERROR_INCOMPATIBLE_INPUT);
cpl_ensure_code(im1->ny == im2->ny, CPL_ERROR_INCOMPATIBLE_INPUT);
cpl_image_multiply_fcomplex_sse_(im1, im2);
return CPL_ERROR_NONE;
}
else if (im1->type == CPL_TYPE_DOUBLE_COMPLEX &&
im2->type == CPL_TYPE_DOUBLE_COMPLEX) {
cpl_ensure_code(im1->nx == im2->nx, CPL_ERROR_INCOMPATIBLE_INPUT);
cpl_ensure_code(im1->ny == im2->ny, CPL_ERROR_INCOMPATIBLE_INPUT);
cpl_image_multiply_dcomplex_sse_(im1, im2);
return CPL_ERROR_NONE;
}
else
#endif
{
#define CPL_OPERATOR CPL_IMAGE_MULTIPLICATIONASSIGN
#include "cpl_image_basic_body.h"
#undef CPL_OPERATOR
}
}
#undef CPL_OPERATION
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Divide two images, store the result in the first image.
@param im1 first operand.
@param im2 second operand.
@return the #_cpl_error_code_ or CPL_ERROR_NONE
@see cpl_image_add()
@note
The result of division with a zero-valued pixel is marked as a bad
pixel.
Possible #_cpl_error_code_ set in this function:
- CPL_ERROR_NULL_INPUT if an input pointer is NULL
- CPL_ERROR_INCOMPATIBLE_INPUT if the input images have different sizes
- CPL_ERROR_TYPE_MISMATCH if the second input image has complex type
while the first one does not
- CPL_ERROR_DIVISION_BY_ZERO is all pixels in the divisor are zero
*/
/*----------------------------------------------------------------------------*/
cpl_error_code cpl_image_divide(cpl_image * im1,
const cpl_image * im2)
{
#define CPL_OPERATION CPL_IMAGE_BASIC_DIVIDE_LOCAL
cpl_mask * zeros;
cpl_binary * pzeros;
cpl_size nzero = 0;
cpl_ensure_code(im1 != NULL, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(im2 != NULL, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(im1->nx == im2->nx, CPL_ERROR_INCOMPATIBLE_INPUT);
cpl_ensure_code(im1->ny == im2->ny, CPL_ERROR_INCOMPATIBLE_INPUT);
assert( im1->pixels );
assert( im2->pixels );
/* Create the zeros map */
/* Do not modify im1->bpm now, in case of failure below */
if (im1->bpm != NULL) {
zeros = im1->bpm;
} else if (im2->bpm != NULL) {
zeros = cpl_mask_duplicate(im2->bpm);
} else {
zeros = cpl_mask_new(im1->nx, im1->ny);
}
pzeros = cpl_mask_get_data(zeros);
/* Switch on the first passed image type */
switch (im1->type) {
case CPL_TYPE_INT: {
int * p1 = (int *)im1->pixels;
/* Switch on the second passed image type */
switch (im2->type) {
#define CPL_TYPE_T CPL_TYPE_INT
#define CPL_TYPE int
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T
#undef CPL_TYPE
#define CPL_TYPE_T CPL_TYPE_FLOAT
#define CPL_TYPE float
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T
#undef CPL_TYPE
#define CPL_TYPE_T CPL_TYPE_DOUBLE
#define CPL_TYPE double
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T
#undef CPL_TYPE
default:
if (zeros != im1->bpm) cpl_mask_delete(zeros);
return cpl_error_set_(CPL_ERROR_TYPE_MISMATCH);
}
break;
}
case CPL_TYPE_FLOAT: {
float * p1 = (float *)im1->pixels;
/* Switch on the second passed image type */
switch (im2->type) {
#define CPL_TYPE_T CPL_TYPE_INT
#define CPL_TYPE int
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T
#undef CPL_TYPE
#define CPL_TYPE_T CPL_TYPE_FLOAT
#define CPL_TYPE float
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T
#undef CPL_TYPE
#define CPL_TYPE_T CPL_TYPE_DOUBLE
#define CPL_TYPE double
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T
#undef CPL_TYPE
default:
if (zeros != im1->bpm) cpl_mask_delete(zeros);
return cpl_error_set_(CPL_ERROR_TYPE_MISMATCH);
}
break;
}
case CPL_TYPE_DOUBLE: {
double * p1 = (double *)im1->pixels;
/* Switch on the second passed image type */
switch (im2->type) {
#define CPL_TYPE_T CPL_TYPE_INT
#define CPL_TYPE int
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T
#undef CPL_TYPE
#define CPL_TYPE_T CPL_TYPE_FLOAT
#define CPL_TYPE float
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T
#undef CPL_TYPE
#define CPL_TYPE_T CPL_TYPE_DOUBLE
#define CPL_TYPE double
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T
#undef CPL_TYPE
default:
if (zeros != im1->bpm) cpl_mask_delete(zeros);
return cpl_error_set_(CPL_ERROR_TYPE_MISMATCH);
}
break;
}
case CPL_TYPE_FLOAT_COMPLEX: {
float complex * p1 = (float complex *)im1->pixels;
/* Switch on the second passed image type */
switch (im2->type) {
#define CPL_TYPE_T CPL_TYPE_INT
#define CPL_TYPE int
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T
#undef CPL_TYPE
#define CPL_TYPE_T CPL_TYPE_FLOAT
#define CPL_TYPE float
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T
#undef CPL_TYPE
#define CPL_TYPE_T CPL_TYPE_DOUBLE
#define CPL_TYPE double
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T
#undef CPL_TYPE
#define CPL_TYPE_T CPL_TYPE_FLOAT_COMPLEX
#define CPL_TYPE float complex
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T
#undef CPL_TYPE
#define CPL_TYPE_T CPL_TYPE_DOUBLE_COMPLEX
#define CPL_TYPE double complex
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T
#undef CPL_TYPE
default:
if (zeros != im1->bpm) cpl_mask_delete(zeros);
return cpl_error_set_(CPL_ERROR_INVALID_TYPE);
}
break;
}
case CPL_TYPE_DOUBLE_COMPLEX: {
double complex * p1 = (double complex *)im1->pixels;
/* Switch on the second passed image type */
switch (im2->type) {
#define CPL_TYPE_T CPL_TYPE_INT
#define CPL_TYPE int
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T
#undef CPL_TYPE
#define CPL_TYPE_T CPL_TYPE_FLOAT
#define CPL_TYPE float
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T
#undef CPL_TYPE
#define CPL_TYPE_T CPL_TYPE_DOUBLE
#define CPL_TYPE double
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T
#undef CPL_TYPE
#define CPL_TYPE_T CPL_TYPE_FLOAT_COMPLEX
#define CPL_TYPE float complex
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T
#undef CPL_TYPE
#define CPL_TYPE_T CPL_TYPE_DOUBLE_COMPLEX
#define CPL_TYPE double complex
#include "cpl_image_basic_body.h"
#undef CPL_TYPE_T
#undef CPL_TYPE
default:
if (zeros != im1->bpm) cpl_mask_delete(zeros);
return cpl_error_set_(CPL_ERROR_INVALID_TYPE);
}
break;
}
default:
if (zeros != im1->bpm) cpl_mask_delete(zeros);
return cpl_error_set_(CPL_ERROR_INVALID_TYPE);
}
if (nzero == im1->nx * im1->ny) {
if (zeros != im1->bpm) cpl_mask_delete(zeros);
return cpl_error_set_(CPL_ERROR_DIVISION_BY_ZERO);
}
if (im1->type != CPL_TYPE_INT && im2->type != CPL_TYPE_INT) {
cpl_tools_add_flops( im1->nx * im1->ny - nzero);
}
/* Handle bad pixels map */
if (im1->bpm != NULL && im2->bpm != NULL && im1->bpm != im2->bpm) {
/* assert( im1->bpm == zeros ); */
cpl_mask_or(im1->bpm, im2->bpm);
} else if (im1->bpm != zeros) {
/*
assert( im1->bpm == NULL );
assert( zeros != NULL );
*/
if (im2->bpm != NULL || nzero > 0) {
im1->bpm = zeros;
} else {
cpl_mask_delete(zeros);
}
}
return CPL_ERROR_NONE;
}
#undef CPL_OPERATION
#define CPL_OPERATION CPL_IMAGE_BASIC_OP_SCALAR
#define CPL_OPERATOR CPL_IMAGE_ADDITIONASSIGN
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Elementwise addition of a scalar to an image
@param self Image to be modified in place.
@param scalar Number to add
@return CPL_ERROR_NONE or the relevant the #_cpl_error_code_ on error
Modifies the image by adding a number to each of its pixels.
The operation is always performed in double precision, with a final
cast of the result to the image pixel type.
Possible #_cpl_error_code_ set in this function:
- CPL_ERROR_NULL_INPUT if an input pointer is NULL
*/
/*----------------------------------------------------------------------------*/
cpl_error_code cpl_image_add_scalar(cpl_image * self,
double scalar)
{
cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
/* Switch on image type */
switch (cpl_image_get_type(self)) {
#define CPL_CLASS CPL_CLASS_DOUBLE
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_FLOAT
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_INT
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_DOUBLE_COMPLEX
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_FLOAT_COMPLEX
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
default:
return cpl_error_set_(CPL_ERROR_INVALID_TYPE);
}
return CPL_ERROR_NONE;
}
#undef CPL_OPERATOR
#define CPL_OPERATOR CPL_IMAGE_SUBTRACTIONASSIGN
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Elementwise subtraction of a scalar from an image
@param self Image to be modified in place.
@param scalar Number to subtract
@return CPL_ERROR_NONE or the relevant the #_cpl_error_code_ on error
@see cpl_image_add_scalar()
*/
/*----------------------------------------------------------------------------*/
cpl_error_code cpl_image_subtract_scalar(cpl_image * self,
double scalar)
{
cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
/* Switch on image type */
switch (cpl_image_get_type(self)) {
#define CPL_CLASS CPL_CLASS_DOUBLE
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_FLOAT
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_INT
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_DOUBLE_COMPLEX
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_FLOAT_COMPLEX
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
default:
return cpl_error_set_(CPL_ERROR_INVALID_TYPE);
}
return CPL_ERROR_NONE;
}
#undef CPL_OPERATOR
#define CPL_OPERATOR CPL_IMAGE_MULTIPLICATIONASSIGN
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Elementwise multiplication of an image with a scalar
@param self Image to be modified in place.
@param scalar Number to multiply with
@return CPL_ERROR_NONE or the relevant the #_cpl_error_code_ on error
@see cpl_image_add_scalar()
*/
/*----------------------------------------------------------------------------*/
cpl_error_code cpl_image_multiply_scalar(cpl_image * self,
double scalar)
{
cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
/* Switch on image type */
switch (cpl_image_get_type(self)) {
#define CPL_CLASS CPL_CLASS_DOUBLE
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_FLOAT
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_INT
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_DOUBLE_COMPLEX
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_FLOAT_COMPLEX
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
default:
return cpl_error_set_(CPL_ERROR_INVALID_TYPE);
}
return CPL_ERROR_NONE;
}
#undef CPL_OPERATOR
#define CPL_OPERATOR CPL_IMAGE_DIVISIONASSIGN
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Elementwise division of an image with a scalar
@param self Image to be modified in place.
@param scalar Non-zero number to divide with
@return CPL_ERROR_NONE or the relevant the #_cpl_error_code_ on error
@see cpl_image_add_scalar()
Modifies the image by dividing each of its pixels with a number.
If the scalar is zero the image is not modified and an error is returned.
Possible #_cpl_error_code_ set in this function:
- CPL_ERROR_NULL_INPUT if an input pointer is NULL
- CPL_ERROR_DIVISION_BY_ZERO a division by 0 occurs
*/
/*----------------------------------------------------------------------------*/
cpl_error_code cpl_image_divide_scalar(cpl_image * self,
double scalar)
{
cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(scalar != 0.0, CPL_ERROR_DIVISION_BY_ZERO);
/* Switch on image type */
switch (cpl_image_get_type(self)) {
#define CPL_CLASS CPL_CLASS_DOUBLE
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_FLOAT
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_INT
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_DOUBLE_COMPLEX
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_FLOAT_COMPLEX
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
default:
return cpl_error_set_(CPL_ERROR_INVALID_TYPE);
}
return CPL_ERROR_NONE;
}
#undef CPL_OPERATOR
#undef CPL_OPERATION
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Compute the elementwise logarithm of the image.
@param self Image to be modified in place.
@param base Base of the logarithm.
@return CPL_ERROR_NONE or the relevant the #_cpl_error_code_ on error
Modifies the image by computing the base-scalar logarithm of each of its
pixels.
Images can be of type CPL_TYPE_INT, CPL_TYPE_FLOAT or CPL_TYPE_DOUBLE.
Pixels for which the logarithm is not defined are
rejected and set to zero.
Possible #_cpl_error_code_ set in this function:
- CPL_ERROR_NULL_INPUT if an input pointer is NULL
- CPL_ERROR_INVALID_TYPE if the passed image type is not supported
- CPL_ERROR_ILLEGAL_INPUT if base is non-positive
- CPL_ERROR_DIVISION_BY_ZERO if the base equals 1
*/
/*----------------------------------------------------------------------------*/
cpl_error_code cpl_image_logarithm(cpl_image * self, double base)
{
cpl_error_code error;
cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
/* Switch on image type */
switch (cpl_image_get_type(self)) {
case CPL_TYPE_INT:
error = cpl_image_logarithm_int(self, base);
break;
case CPL_TYPE_FLOAT:
error = cpl_image_logarithm_float(self, base);
break;
case CPL_TYPE_DOUBLE:
error = cpl_image_logarithm_double(self, base);
break;
default:
return cpl_error_set_message_(CPL_ERROR_INVALID_TYPE, "type='%s'. "
"base=%g", cpl_type_get_name
(cpl_image_get_type(self)), base);
}
return error ? cpl_error_set_where_() : CPL_ERROR_NONE;
}
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Compute the elementwise exponential of the image.
@param self Image to be modified in place.
@param base Base of the exponential.
@return CPL_ERROR_NONE or the relevant the #_cpl_error_code_ on error
Modifies the image by computing the base-scalar exponential of each of its
pixels.
Images can be of type CPL_TYPE_INT, CPL_TYPE_FLOAT or CPL_TYPE_DOUBLE.
Pixels for which the power of the given base is not defined are
rejected and set to zero.
Possible #_cpl_error_code_ set in this function:
- CPL_ERROR_NULL_INPUT if an input pointer is NULL
- CPL_ERROR_INVALID_TYPE if the passed image type is not supported
*/
/*----------------------------------------------------------------------------*/
cpl_error_code cpl_image_exponential(cpl_image * self, double base)
{
cpl_error_code error;
cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
/* Switch on image type */
switch (cpl_image_get_type(self)) {
case CPL_TYPE_INT:
error = cpl_image_exponential_int(self, base);
break;
case CPL_TYPE_FLOAT:
error = cpl_image_exponential_float(self, base);
break;
case CPL_TYPE_DOUBLE:
error = cpl_image_exponential_double(self, base);
break;
default:
return cpl_error_set_message_(CPL_ERROR_INVALID_TYPE, "type='%s'. "
"base=%g", cpl_type_get_name
(cpl_image_get_type(self)), base);
}
return error ? cpl_error_set_where_() : CPL_ERROR_NONE;
}
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Compute the elementwise power of the image.
@param self Image to be modified in place.
@param exponent Scalar exponent.
@return CPL_ERROR_NONE or the relevant the #_cpl_error_code_ on error
Modifies the image by lifting each of its pixels to exponent.
Images can be of type CPL_TYPE_INT, CPL_TYPE_FLOAT or CPL_TYPE_DOUBLE.
Pixels for which the power to the given exponent is not defined are
rejected and set to zero.
Possible #_cpl_error_code_ set in this function:
- CPL_ERROR_NULL_INPUT if an input pointer is NULL
- CPL_ERROR_INVALID_TYPE if the passed image type is not supported
*/
/*----------------------------------------------------------------------------*/
cpl_error_code cpl_image_power(cpl_image * self, double exponent)
{
cpl_error_code error;
cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
/* Switch on image type */
switch (cpl_image_get_type(self)) {
case CPL_TYPE_INT:
error = cpl_image_power_int(self, exponent);
break;
case CPL_TYPE_FLOAT:
error = cpl_image_power_float(self, exponent);
break;
case CPL_TYPE_DOUBLE:
error = cpl_image_power_double(self, exponent);
break;
default:
return cpl_error_set_message_(CPL_ERROR_INVALID_TYPE, "type='%s'. "
"exponent=%g", cpl_type_get_name
(cpl_image_get_type(self)), exponent);
}
return error ? cpl_error_set_where_() : CPL_ERROR_NONE;
}
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief The bit-wise and of two images with integer pixels
@param self Pre-allocated image to hold the result
@param first First operand, or NULL for an in-place operation
@param second Second operand
@return CPL_ERROR_NONE or the relevant the #_cpl_error_code_ on error
@note CPL_TYPE_INT is required
@see cpl_mask_and() for the equivalent logical operation
Possible #_cpl_error_code_ set in this function:
- CPL_ERROR_NULL_INPUT if an input pointer is NULL
- CPL_ERROR_INCOMPATIBLE_INPUT if the images have different sizes
- CPL_ERROR_INVALID_TYPE if the passed image type is as required
*/
/*----------------------------------------------------------------------------*/
cpl_error_code cpl_image_and(cpl_image * self,
const cpl_image * first,
const cpl_image * second)
{
cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(second != NULL, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(self->nx == second->nx, CPL_ERROR_INCOMPATIBLE_INPUT);
cpl_ensure_code(self->ny == second->ny, CPL_ERROR_INCOMPATIBLE_INPUT);
if (cpl_image_get_type(self) != CPL_TYPE_INT) {
return cpl_error_set_message_(CPL_ERROR_INVALID_TYPE,
"self-type='%s', not int",
cpl_type_get_name
(cpl_image_get_type(self)));
}
if (first != NULL) {
cpl_ensure_code(self->nx == first->nx, CPL_ERROR_INCOMPATIBLE_INPUT);
cpl_ensure_code(self->ny == first->ny, CPL_ERROR_INCOMPATIBLE_INPUT);
if (cpl_image_get_type(first) != CPL_TYPE_INT) {
return cpl_error_set_message_(CPL_ERROR_INVALID_TYPE,
"first-type='%s', not int",
cpl_type_get_name
(cpl_image_get_type(first)));
}
}
if (cpl_image_get_type(second) != CPL_TYPE_INT) {
return cpl_error_set_message_(CPL_ERROR_INVALID_TYPE,
"second-type='%s', not int",
cpl_type_get_name
(cpl_image_get_type(second)));
}
/* Cannot fail now */
/* Update the output bad pixel map */
cpl_image_or_mask(self, first, second);
cpl_mask_and_((cpl_binary*)self->pixels,
first ? (const cpl_binary*)first->pixels : NULL,
(const cpl_binary*)second->pixels,
cpl_type_get_sizeof(CPL_TYPE_INT)
*(size_t)(self->nx * self->ny));
return CPL_ERROR_NONE;
}
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief The bit-wise or of two images with integer pixels
@param self Pre-allocated image to hold the result
@param first First operand, or NULL for an in-place operation
@param second Second operand
@return CPL_ERROR_NONE or the relevant the #_cpl_error_code_ on error
@note CPL_TYPE_INT is required
@see cpl_mask_or() for the equivalent logical operation
Possible #_cpl_error_code_ set in this function:
- CPL_ERROR_NULL_INPUT if an input pointer is NULL
- CPL_ERROR_INCOMPATIBLE_INPUT if the images have different sizes
- CPL_ERROR_INVALID_TYPE if the passed image type is as required
*/
/*----------------------------------------------------------------------------*/
cpl_error_code cpl_image_or(cpl_image * self,
const cpl_image * first,
const cpl_image * second)
{
cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(second != NULL, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(self->nx == second->nx, CPL_ERROR_INCOMPATIBLE_INPUT);
cpl_ensure_code(self->ny == second->ny, CPL_ERROR_INCOMPATIBLE_INPUT);
if (cpl_image_get_type(self) != CPL_TYPE_INT) {
return cpl_error_set_message_(CPL_ERROR_INVALID_TYPE,
"self-type='%s', not int",
cpl_type_get_name
(cpl_image_get_type(self)));
}
if (first != NULL) {
cpl_ensure_code(self->nx == first->nx, CPL_ERROR_INCOMPATIBLE_INPUT);
cpl_ensure_code(self->ny == first->ny, CPL_ERROR_INCOMPATIBLE_INPUT);
if (cpl_image_get_type(first) != CPL_TYPE_INT) {
return cpl_error_set_message_(CPL_ERROR_INVALID_TYPE,
"first-type='%s', not int",
cpl_type_get_name
(cpl_image_get_type(first)));
}
}
if (cpl_image_get_type(second) != CPL_TYPE_INT) {
return cpl_error_set_message_(CPL_ERROR_INVALID_TYPE,
"second-type='%s', not int",
cpl_type_get_name
(cpl_image_get_type(second)));
}
/* Cannot fail now */
/* Update the output bad pixel map */
cpl_image_or_mask(self, first, second);
cpl_mask_or_((cpl_binary*)self->pixels,
first ? (const cpl_binary*)first->pixels : NULL,
(const cpl_binary*)second->pixels,
cpl_type_get_sizeof(CPL_TYPE_INT)
*(size_t)(self->nx * self->ny));
return CPL_ERROR_NONE;
}
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief The bit-wise xor of two images with integer pixels
@param self Pre-allocated image to hold the result
@param first First operand, or NULL for an in-place operation
@param second Second operand
@return CPL_ERROR_NONE or the relevant the #_cpl_error_code_ on error
@note CPL_TYPE_INT is required
@see cpl_mask_xor() for the equivalent logical operation
Possible #_cpl_error_code_ set in this function:
- CPL_ERROR_NULL_INPUT if an input pointer is NULL
- CPL_ERROR_INCOMPATIBLE_INPUT if the images have different sizes
- CPL_ERROR_INVALID_TYPE if the passed image type is as required
*/
/*----------------------------------------------------------------------------*/
cpl_error_code cpl_image_xor(cpl_image * self,
const cpl_image * first,
const cpl_image * second)
{
cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(second != NULL, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(self->nx == second->nx, CPL_ERROR_INCOMPATIBLE_INPUT);
cpl_ensure_code(self->ny == second->ny, CPL_ERROR_INCOMPATIBLE_INPUT);
if (cpl_image_get_type(self) != CPL_TYPE_INT) {
return cpl_error_set_message_(CPL_ERROR_INVALID_TYPE,
"self-type='%s', not int",
cpl_type_get_name
(cpl_image_get_type(self)));
}
if (first != NULL) {
cpl_ensure_code(self->nx == first->nx, CPL_ERROR_INCOMPATIBLE_INPUT);
cpl_ensure_code(self->ny == first->ny, CPL_ERROR_INCOMPATIBLE_INPUT);
if (cpl_image_get_type(first) != CPL_TYPE_INT) {
return cpl_error_set_message_(CPL_ERROR_INVALID_TYPE,
"first-type='%s', not int",
cpl_type_get_name
(cpl_image_get_type(first)));
}
}
if (cpl_image_get_type(second) != CPL_TYPE_INT) {
return cpl_error_set_message_(CPL_ERROR_INVALID_TYPE,
"second-type='%s', not int",
cpl_type_get_name
(cpl_image_get_type(second)));
}
/* Cannot fail now */
/* Update the output bad pixel map */
cpl_image_or_mask(self, first, second);
cpl_mask_xor_((cpl_binary*)self->pixels,
first ? (const cpl_binary*)first->pixels : NULL,
(const cpl_binary*)second->pixels,
cpl_type_get_sizeof(CPL_TYPE_INT)
*(size_t)(self->nx * self->ny));
return CPL_ERROR_NONE;
}
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief The bit-wise complement (not) of an image with integer pixels
@param self Pre-allocated image to hold the result
@param first First operand, or NULL for an in-place operation
@return CPL_ERROR_NONE or the relevant the #_cpl_error_code_ on error
@note CPL_TYPE_INT is required
@see cpl_mask_not() for the equivalent logical operation
Possible #_cpl_error_code_ set in this function:
- CPL_ERROR_NULL_INPUT if an input pointer is NULL
- CPL_ERROR_INCOMPATIBLE_INPUT if the images have different sizes
- CPL_ERROR_INVALID_TYPE if the passed image type is as required
*/
/*----------------------------------------------------------------------------*/
cpl_error_code cpl_image_not(cpl_image * self,
const cpl_image * first)
{
cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
if (cpl_image_get_type(self) != CPL_TYPE_INT) {
return cpl_error_set_message_(CPL_ERROR_INVALID_TYPE,
"self-type='%s', not int",
cpl_type_get_name
(cpl_image_get_type(self)));
}
if (first != NULL) {
cpl_ensure_code(self->nx == first->nx, CPL_ERROR_INCOMPATIBLE_INPUT);
cpl_ensure_code(self->ny == first->ny, CPL_ERROR_INCOMPATIBLE_INPUT);
if (cpl_image_get_type(first) != CPL_TYPE_INT) {
return cpl_error_set_message_(CPL_ERROR_INVALID_TYPE,
"first-type='%s', not int",
cpl_type_get_name
(cpl_image_get_type(first)));
}
}
/* Cannot fail now */
/* Update the output bad pixel map */
cpl_image_or_mask_unary(self, first);
cpl_mask_xor_scalar((cpl_binary*)self->pixels,
first ? (const cpl_binary*)first->pixels : NULL,
(cpl_bitmask)-1,
cpl_type_get_sizeof(CPL_TYPE_INT)
*(size_t)(self->nx * self->ny));
return CPL_ERROR_NONE;
}
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief The bit-wise and of a scalar and an image with integer pixels
@param self Pre-allocated image to hold the result
@param first First operand, or NULL for an in-place operation
@param second Second operand (scalar)
@return CPL_ERROR_NONE or the relevant the #_cpl_error_code_ on error
@note CPL_TYPE_INT is required
Possible #_cpl_error_code_ set in this function:
- CPL_ERROR_NULL_INPUT if an input pointer is NULL
- CPL_ERROR_INCOMPATIBLE_INPUT if the images have different sizes
- CPL_ERROR_INVALID_TYPE if the passed image type is as required
*/
/*----------------------------------------------------------------------------*/
cpl_error_code cpl_image_and_scalar(cpl_image * self,
const cpl_image * first,
cpl_bitmask second)
{
cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
if (cpl_image_get_type(self) != CPL_TYPE_INT) {
return cpl_error_set_message_(CPL_ERROR_INVALID_TYPE,
"self-type='%s', not int",
cpl_type_get_name
(cpl_image_get_type(self)));
}
if (first != NULL) {
cpl_ensure_code(self->nx == first->nx, CPL_ERROR_INCOMPATIBLE_INPUT);
cpl_ensure_code(self->ny == first->ny, CPL_ERROR_INCOMPATIBLE_INPUT);
if (cpl_image_get_type(first) != CPL_TYPE_INT) {
return cpl_error_set_message_(CPL_ERROR_INVALID_TYPE,
"first-type='%s', not int",
cpl_type_get_name
(cpl_image_get_type(first)));
}
}
/* Cannot fail now */
/* Update the output bad pixel map */
cpl_image_or_mask_unary(self, first);
cpl_mask_and_scalar((cpl_binary*)self->pixels,
first ? (const cpl_binary*)first->pixels : NULL,
(second & (uint32_t)-1) | (second << 32),
cpl_type_get_sizeof(CPL_TYPE_INT)
*(size_t)(self->nx * self->ny));
return CPL_ERROR_NONE;
}
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief The bit-wise or of a scalar and an image with integer pixels
@param self Pre-allocated image to hold the result
@param first First operand, or NULL for an in-place operation
@param second Second operand (scalar)
@return CPL_ERROR_NONE or the relevant the #_cpl_error_code_ on error
@note CPL_TYPE_INT is required
Possible #_cpl_error_code_ set in this function:
- CPL_ERROR_NULL_INPUT if an input pointer is NULL
- CPL_ERROR_INCOMPATIBLE_INPUT if the images have different sizes
- CPL_ERROR_INVALID_TYPE if the passed image type is as required
*/
/*----------------------------------------------------------------------------*/
cpl_error_code cpl_image_or_scalar(cpl_image * self,
const cpl_image * first,
cpl_bitmask second)
{
cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
if (cpl_image_get_type(self) != CPL_TYPE_INT) {
return cpl_error_set_message_(CPL_ERROR_INVALID_TYPE,
"self-type='%s', not int",
cpl_type_get_name
(cpl_image_get_type(self)));
}
if (first != NULL) {
cpl_ensure_code(self->nx == first->nx, CPL_ERROR_INCOMPATIBLE_INPUT);
cpl_ensure_code(self->ny == first->ny, CPL_ERROR_INCOMPATIBLE_INPUT);
if (cpl_image_get_type(first) != CPL_TYPE_INT) {
return cpl_error_set_message_(CPL_ERROR_INVALID_TYPE,
"first-type='%s', not int",
cpl_type_get_name
(cpl_image_get_type(first)));
}
}
/* Cannot fail now */
/* Update the output bad pixel map */
cpl_image_or_mask_unary(self, first);
cpl_mask_or_scalar((cpl_binary*)self->pixels,
first ? (const cpl_binary*)first->pixels : NULL,
(second & (uint32_t)-1) | (second << 32),
cpl_type_get_sizeof(CPL_TYPE_INT)
*(size_t)(self->nx * self->ny));
return CPL_ERROR_NONE;
}
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief The bit-wise xor of a scalar and an image with integer pixels
@param self Pre-allocated image to hold the result
@param first First operand, or NULL for an in-place operation
@param second Second operand (scalar)
@return CPL_ERROR_NONE or the relevant the #_cpl_error_code_ on error
@note CPL_TYPE_INT is required
Possible #_cpl_error_code_ set in this function:
- CPL_ERROR_NULL_INPUT if an input pointer is NULL
- CPL_ERROR_INCOMPATIBLE_INPUT if the images have different sizes
- CPL_ERROR_INVALID_TYPE if the passed image type is as required
*/
/*----------------------------------------------------------------------------*/
cpl_error_code cpl_image_xor_scalar(cpl_image * self,
const cpl_image * first,
cpl_bitmask second)
{
cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
if (cpl_image_get_type(self) != CPL_TYPE_INT) {
return cpl_error_set_message_(CPL_ERROR_INVALID_TYPE,
"self-type='%s', not int",
cpl_type_get_name
(cpl_image_get_type(self)));
}
if (first != NULL) {
cpl_ensure_code(self->nx == first->nx, CPL_ERROR_INCOMPATIBLE_INPUT);
cpl_ensure_code(self->ny == first->ny, CPL_ERROR_INCOMPATIBLE_INPUT);
if (cpl_image_get_type(first) != CPL_TYPE_INT) {
return cpl_error_set_message_(CPL_ERROR_INVALID_TYPE,
"first-type='%s', not int",
cpl_type_get_name
(cpl_image_get_type(first)));
}
}
/* Cannot fail now */
/* Update the output bad pixel map */
cpl_image_or_mask_unary(self, first);
cpl_mask_xor_scalar((cpl_binary*)self->pixels,
first ? (const cpl_binary*)first->pixels : NULL,
(second & (uint32_t)-1) | (second << 32),
cpl_type_get_sizeof(CPL_TYPE_INT)
*(size_t)(self->nx * self->ny));
return CPL_ERROR_NONE;
}
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief The pixel-wise Euclidean distance function of the images
@param self Pre-allocated image to hold the result
@param first First operand, or NULL for an in-place operation
@param second Second operand
@return CPL_ERROR_NONE or the relevant the #_cpl_error_code_ on error
The Euclidean distance function is useful for gaussian error propagation
on addition/subtraction operations.
For pixel values a and b the Euclidean distance c is defined as:
$$c = sqrt{a^2 + b^2}$$
first may be NULL, in this case the distance is computed in-place on self
using second as the other operand.
Images can be of type CPL_TYPE_FLOAT or CPL_TYPE_DOUBLE.
If both input operands are of type CPL_TYPE_FLOAT the distance is computed
in single precision (using hypotf()), otherwise in double precision
(using hypot()).
Possible #_cpl_error_code_ set in this function:
- CPL_ERROR_NULL_INPUT if an input pointer is NULL
- CPL_ERROR_INCOMPATIBLE_INPUT if the images have different sizes
- CPL_ERROR_INVALID_TYPE if the passed image type is not supported
*/
/*----------------------------------------------------------------------------*/
cpl_error_code cpl_image_hypot(cpl_image * self,
const cpl_image * first,
const cpl_image * second)
{
cpl_error_code error;
/* Only used to determine the type of the first hypot operand */
const cpl_image * myfirst = first ? first : second;
cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(second != NULL, CPL_ERROR_NULL_INPUT);
if (cpl_image_get_type(self) != CPL_TYPE_DOUBLE &&
cpl_image_get_type(self) != CPL_TYPE_FLOAT) {
return cpl_error_set_message_(CPL_ERROR_INVALID_TYPE,
"self-type='%s'", cpl_type_get_name
(cpl_image_get_type(self)));
}
if (first != NULL && cpl_image_get_type(first) != CPL_TYPE_DOUBLE &&
cpl_image_get_type(first) != CPL_TYPE_FLOAT) {
return cpl_error_set_message_(CPL_ERROR_INVALID_TYPE,
"first-type='%s'", cpl_type_get_name
(cpl_image_get_type(first)));
}
if (cpl_image_get_type(second) != CPL_TYPE_DOUBLE &&
cpl_image_get_type(second) != CPL_TYPE_FLOAT) {
return cpl_error_set_message_(CPL_ERROR_INVALID_TYPE,
"second-type='%s'", cpl_type_get_name
(cpl_image_get_type(second)));
}
if (first != NULL) {
cpl_ensure_code(self->nx == first->nx, CPL_ERROR_INCOMPATIBLE_INPUT);
cpl_ensure_code(self->ny == first->ny, CPL_ERROR_INCOMPATIBLE_INPUT);
}
cpl_ensure_code(self->nx == second->nx, CPL_ERROR_INCOMPATIBLE_INPUT);
cpl_ensure_code(self->ny == second->ny, CPL_ERROR_INCOMPATIBLE_INPUT);
/* Switch on image type */
switch ((cpl_image_get_type(self) == CPL_TYPE_FLOAT ? 4 : 0) +
(cpl_image_get_type(myfirst) == CPL_TYPE_FLOAT ? 2 : 0) +
(cpl_image_get_type(second) == CPL_TYPE_FLOAT ? 1 : 0)) {
case 7: /* float, float, float */
error = cpl_image_hypot_float_float_float(self, first, second);
break;
case 6: /* float, float, double */
error = cpl_image_hypot_float_float_double(self, first, second);
break;
case 5: /* float, double, float */
error = cpl_image_hypot_float_double_float(self, first, second);
break;
case 4: /* float, double, double */
error = cpl_image_hypot_float_double_double(self, first, second);
break;
case 3: /* double, float, float */
error = cpl_image_hypot_double_float_float(self, first, second);
break;
case 2: /* double, float, double */
error = cpl_image_hypot_double_float_double(self, first, second);
break;
case 1: /* double, double, float */
error = cpl_image_hypot_double_double_float(self, first, second);
break;
default: /* double, double, double */
error = cpl_image_hypot_double_double_double(self, first, second);
break;
}
return error ? cpl_error_set_where_() : CPL_ERROR_NONE;
}
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Normalise pixels in an image.
@param image Image operand.
@param mode Normalisation mode.
@return CPL_ERROR_NONE, or the relevant #_cpl_error_code_ on error.
Normalises an image according to a given criterion.
Possible normalisations are:
- CPL_NORM_SCALE sets the pixel interval to [0,1].
- CPL_NORM_MEAN sets the mean value to 1.
- CPL_NORM_FLUX sets the flux to 1.
- CPL_NORM_ABSFLUX sets the absolute flux to 1.
Possible #_cpl_error_code_ set in this function:
- CPL_ERROR_NULL_INPUT if an input pointer is NULL
*/
/*----------------------------------------------------------------------------*/
cpl_error_code cpl_image_normalise(
cpl_image * image,
cpl_norm mode)
{
double scale;
cpl_ensure_code(image, CPL_ERROR_NULL_INPUT);
switch (mode) {
case CPL_NORM_SCALE: {
cpl_stats stats;
if (cpl_stats_fill_from_image(&stats, image, CPL_STATS_MIN
| CPL_STATS_MAX)) {
return cpl_error_set_where_();
}
scale = cpl_stats_get_max(&stats) - cpl_stats_get_min(&stats);
if (scale > 0 &&
cpl_image_subtract_scalar(image, cpl_stats_get_min(&stats))) {
return cpl_error_set_where_();
}
break;
}
case CPL_NORM_MEAN: {
scale = cpl_image_get_mean(image);
break;
}
case CPL_NORM_FLUX: {
scale = cpl_image_get_flux(image);
break;
}
case CPL_NORM_ABSFLUX: {
scale = cpl_image_get_absflux(image);
break;
}
default:
/* This case can only be reached if cpl_norm is extended in error */
return cpl_error_set_(CPL_ERROR_INVALID_TYPE);
}
cpl_ensure_code( !cpl_image_divide_scalar(image, scale),
cpl_error_get_code());
return CPL_ERROR_NONE;
}
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Create a new normalised image from an existing image.
@param image_in Image operand.
@param mode Normalisation mode.
@return 1 newly allocated image or NULL on error
@see cpl_image_normalise
Stores the result in a newly allocated image and returns it.
The returned image must be deallocated using cpl_image_delete().
- CPL_ERROR_NULL_INPUT if an input pointer is NULL
*/
/*----------------------------------------------------------------------------*/
cpl_image * cpl_image_normalise_create(
const cpl_image * image_in,
cpl_norm mode)
{
cpl_image * image_out;
cpl_ensure(image_in, CPL_ERROR_NULL_INPUT, NULL);
image_out = cpl_image_duplicate(image_in);
if (cpl_image_normalise(image_out, mode)) {
cpl_image_delete(image_out);
image_out = NULL;
(void)cpl_error_set_where_();
}
return image_out;
}
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Create a new image by elementwise addition of a scalar to an image
@param image Image to add
@param addend Number to add
@return 1 newly allocated image or NULL in case of an error
@see cpl_image_add_scalar
Creates a new image, being the result of the operation, and returns it to
the caller. The returned image must be deallocated using cpl_image_delete().
The function supports images with different types among CPL_TYPE_INT,
CPL_TYPE_FLOAT and CPL_TYPE_DOUBLE. The type of the created image is that of
the passed image.
*/
/*----------------------------------------------------------------------------*/
cpl_image * cpl_image_add_scalar_create(
const cpl_image * image,
double addend)
{
cpl_image * result = cpl_image_duplicate(image);
cpl_ensure(result, cpl_error_get_code(), NULL);
if (cpl_image_add_scalar(result, addend)) {
cpl_image_delete(result);
result = NULL;
(void)cpl_error_set_where_();
}
return result;
}
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Create an image by elementwise subtraction of a scalar from an image
@param image Image to be subtracted from
@param subtrahend Number to subtract
@return 1 newly allocated image or NULL in case of an error
@see cpl_image_subtract_scalar
@see cpl_image_add_scalar_create
*/
/*----------------------------------------------------------------------------*/
cpl_image * cpl_image_subtract_scalar_create(
const cpl_image * image,
double subtrahend)
{
cpl_image * result = cpl_image_duplicate(image);
cpl_ensure(result, cpl_error_get_code(), NULL);
if (cpl_image_subtract_scalar(result, subtrahend)) {
cpl_image_delete(result);
result = NULL;
(void)cpl_error_set_where_();
}
return result;
}
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Create a new image by multiplication of a scalar and an image
@param image Image to be multiplied
@param factor Number to multiply with
@return 1 newly allocated image or NULL in case of an error
@see cpl_image_multiply_scalar
@see cpl_image_add_scalar_create
*/
/*----------------------------------------------------------------------------*/
cpl_image * cpl_image_multiply_scalar_create(
const cpl_image * image,
double factor)
{
cpl_image * result = cpl_image_duplicate(image);
cpl_ensure(result, cpl_error_get_code(), NULL);
if (cpl_image_multiply_scalar(result, factor)) {
cpl_image_delete(result);
result = NULL;
(void)cpl_error_set_where_();
}
return result;
}
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Create a new image by elementwise division of an image with a scalar
@param image Image to divide
@param divisor Non-zero number to divide with
@return 1 newly allocated image or NULL in case of an error
@see cpl_image_divide_scalar
@see cpl_image_add_scalar_create
*/
/*----------------------------------------------------------------------------*/
cpl_image * cpl_image_divide_scalar_create(
const cpl_image * image,
double divisor)
{
cpl_image * result = cpl_image_duplicate(image);
cpl_ensure(result, cpl_error_get_code(), NULL);
if (cpl_image_divide_scalar(result, divisor)) {
cpl_image_delete(result);
result = NULL;
(void)cpl_error_set_where_();
}
return result;
}
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Create a new image by taking the elementwise logarithm of an image
@param image Image to take logarithm of
@param base Base of the logarithm.
@return 1 newly allocated image or NULL in case of an error
@see cpl_image_logarithm
@see cpl_image_add_scalar_create
*/
/*----------------------------------------------------------------------------*/
cpl_image * cpl_image_logarithm_create(
const cpl_image * image,
double base)
{
cpl_image * result = cpl_image_duplicate(image);
cpl_ensure(result, cpl_error_get_code(), NULL);
if (cpl_image_logarithm(result, base)) {
cpl_image_delete(result);
result = NULL;
(void)cpl_error_set_where_();
}
return result;
}
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Create a new image by elementwise exponentiation of an image
@param image Image to exponentiate
@param base Base of the exponential
@return 1 newly allocated image or NULL in case of an error
@see cpl_image_logarithm
@see cpl_image_add_scalar_create
*/
/*----------------------------------------------------------------------------*/
cpl_image * cpl_image_exponential_create(
const cpl_image * image,
double base)
{
cpl_image * result = cpl_image_duplicate(image);
cpl_ensure(result, cpl_error_get_code(), NULL);
if (cpl_image_exponential(result, base)) {
cpl_image_delete(result);
result = NULL;
(void)cpl_error_set_where_();
}
return result;
}
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Create a new image by elementwise raising of an image to a power
@param image Image to raise to a power
@param exponent scalar exponent
@return 1 newly allocated image or NULL in case of an error
@see cpl_image_power
@see cpl_image_add_scalar_create
*/
/*----------------------------------------------------------------------------*/
cpl_image * cpl_image_power_create(
const cpl_image * image,
double exponent)
{
cpl_image * result = cpl_image_duplicate(image);
cpl_ensure(result, cpl_error_get_code(), NULL);
if (cpl_image_power(result, exponent)) {
cpl_image_delete(result);
result = NULL;
(void)cpl_error_set_where_();
}
return result;
}
#define CPL_OPERATION CPL_IMAGE_BASIC_THRESHOLD
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Threshold an image to a given interval.
@param image_in Image to threshold.
@param lo_cut Lower bound.
@param hi_cut Higher bound.
@param assign_lo_cut Value to assign to pixels below low bound.
@param assign_hi_cut Value to assign to pixels above high bound.
@return the #_cpl_error_code_ or CPL_ERROR_NONE
Pixels outside of the provided interval are assigned the given values.
Use FLT_MIN and FLT_MAX for floating point images and DBL_MIN and DBL_MAX
for double images for the lo_cut and hi_cut to avoid any pixel
replacement.
Images can be of type CPL_TYPE_INT, CPL_TYPE_FLOAT or CPL_TYPE_DOUBLE.
lo_cut must be smaller than or equal to hi_cut.
Possible #_cpl_error_code_ set in this function:
- CPL_ERROR_NULL_INPUT if an input pointer is NULL
- CPL_ERROR_INVALID_TYPE if the passed image type is not supported
- CPL_ERROR_ILLEGAL_INPUT if lo_cut is greater than hi_cut
*/
/*----------------------------------------------------------------------------*/
cpl_error_code cpl_image_threshold(
cpl_image * image_in,
double lo_cut,
double hi_cut,
double assign_lo_cut,
double assign_hi_cut)
{
cpl_ensure_code(image_in != NULL, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(lo_cut <= hi_cut, CPL_ERROR_ILLEGAL_INPUT);
/* Switch on image type */
switch (image_in->type) {
#define CPL_CLASS CPL_CLASS_DOUBLE
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_FLOAT
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_INT
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
default:
return cpl_error_set_(CPL_ERROR_INVALID_TYPE);
}
return CPL_ERROR_NONE;
}
#undef CPL_OPERATION
#define CPL_OPERATION CPL_IMAGE_BASIC_ABS
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Take the absolute value of an image.
@param image Image to be modified in place
@return CPL_ERROR_NONE or the relevant the #_cpl_error_code_ on error
Set each pixel to its absolute value.
Possible #_cpl_error_code_ set in this function:
- CPL_ERROR_NULL_INPUT if an input pointer is NULL
*/
/*----------------------------------------------------------------------------*/
cpl_error_code cpl_image_abs(cpl_image * image)
{
/* Check entries */
cpl_ensure_code(image, CPL_ERROR_NULL_INPUT);
/* Switch on image type */
switch (image->type) {
#define CPL_CLASS CPL_CLASS_DOUBLE
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_FLOAT
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_INT
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
default:
return cpl_error_set_(CPL_ERROR_INVALID_TYPE);
}
return CPL_ERROR_NONE;
}
#undef CPL_OPERATION
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Take the absolute value of an image.
@param image_in Image operand.
@return 1 newly allocated image or NULL on error
@see cpl_image_abs
For each pixel, out = abs(in). The returned image must be deallocated using
cpl_image_delete().
*/
/*----------------------------------------------------------------------------*/
cpl_image * cpl_image_abs_create(const cpl_image * image_in)
{
cpl_image * result = cpl_image_duplicate(image_in);
cpl_ensure(result, cpl_error_get_code(), NULL);
if (cpl_image_abs(result)) {
cpl_image_delete(result);
result = NULL;
(void)cpl_error_set_where_();
}
return result;
}
#define CPL_OPERATION CPL_IMAGE_BASIC_AVERAGE
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Build the average of two images.
@param image_1 First image operand.
@param image_2 Second image operand.
@return 1 newly allocated image or NULL on error
Builds the average of two images and returns a newly allocated image,
to be deallocated using cpl_image_delete(). The average is arithmetic, i.e.
outpix=(pix1+pix2)/2
Images can be of type CPL_TYPE_INT, CPL_TYPE_FLOAT or CPL_TYPE_DOUBLE.
Possible #_cpl_error_code_ set in this function:
- CPL_ERROR_NULL_INPUT if an input pointer is NULL
- CPL_ERROR_INVALID_TYPE if the passed image type is not supported
*/
/*----------------------------------------------------------------------------*/
cpl_image * cpl_image_average_create(
const cpl_image * image_1,
const cpl_image * image_2)
{
cpl_image * image_out;
int * pii2;
float * pfi2;
double * pdi2;
/* Check entries */
cpl_ensure(image_1 && image_2, CPL_ERROR_NULL_INPUT, NULL);
/* Input data images shall have the same sizes */
cpl_ensure(image_1->nx == image_2->nx && image_1->ny == image_2->ny,
CPL_ERROR_ILLEGAL_INPUT, NULL);
/* Switch on first passed image type */
switch (image_1->type) {
#define CPL_CLASS CPL_CLASS_DOUBLE
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_FLOAT
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_INT
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
default:
(void)cpl_error_set_(CPL_ERROR_INVALID_TYPE);
return NULL;
}
/* Handle bad pixels map */
if (image_1->bpm == NULL && image_2->bpm == NULL) {
image_out->bpm = NULL;
} else if (image_1->bpm == NULL) {
image_out->bpm = cpl_mask_duplicate(image_2->bpm);
} else if (image_2->bpm == NULL) {
image_out->bpm = cpl_mask_duplicate(image_1->bpm);
} else {
image_out->bpm = cpl_mask_duplicate(image_1->bpm);
cpl_mask_or(image_out->bpm, image_2->bpm);
}
return image_out;
}
#undef CPL_OPERATION
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Collapse an image region along its rows or columns.
@param self Image to collapse.
@param llx lower left x coord.
@param lly lower left y coord
@param urx upper right x coord
@param ury upper right y coord
@param direction Collapsing direction.
@return a newly allocated image or NULL on error
@see cpl_image_collapse_create()
llx, lly, urx, ury are the image region coordinates in FITS convention.
Those specified bounds are included in the collapsed region.
Possible #_cpl_error_code_ set in this function:
- CPL_ERROR_NULL_INPUT if an input pointer is NULL
- CPL_ERROR_ILLEGAL_INPUT if the specified window is not valid
*/
/*----------------------------------------------------------------------------*/
cpl_image * cpl_image_collapse_window_create(const cpl_image * self,
cpl_size llx,
cpl_size lly,
cpl_size urx,
cpl_size ury,
int direction)
{
cpl_image * other;
/* Switch on image type */
switch (cpl_image_get_type(self)) {
case CPL_TYPE_DOUBLE:
other = cpl_image_collapse_window_create_double(self, llx, lly, urx,
ury, direction);
break;
case CPL_TYPE_FLOAT:
other = cpl_image_collapse_window_create_float(self, llx, lly, urx,
ury, direction);
break;
case CPL_TYPE_INT:
other = cpl_image_collapse_window_create_int(self, llx, lly, urx,
ury, direction);
break;
default:
/* NULL input will be go here, after having set a CPL error */
other = NULL;
}
/* Propagate error, if any */
if (other == NULL) (void)cpl_error_set_where_();
return other;
}
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Collapse an image along its rows or columns.
@param self Input image to collapse.
@param direction Collapsing direction.
@return 1 newly allocated 1D image or NULL on error
On success the function returns a 1D image, created by adding up all pixels
on the same row or column.
@verbatim
Collapse along y (sum of rows):
p7 p8 p9 Input image is a 3x3 image containing 9 pixels.
p4 p5 p6 The output is an image containing one row with
p1 p2 p3 3 pixels A, B, C, where:
----------
A B C A = p1+p4+p7
B = p2+p5+p8
C = p3+p6+p9
If p7 is a bad pixel, A = (p1+p4)*3/2.
If p1, p4, p7 are bad, A is flagged as bad.
@endverbatim
Provide the collapsing direction as an int. Give 0 to collapse along y
(sum of rows) and get an image with a single row in output, or give 1
to collapse along x (sum of columns) to get an image with a single
column in output.
Only the good pixels are collapsed.
Images can be of type CPL_TYPE_INT, CPL_TYPE_FLOAT or CPL_TYPE_DOUBLE.
The returned image must be deallocated using cpl_image_delete().
Possible #_cpl_error_code_ set in this function:
- CPL_ERROR_NULL_INPUT if an input pointer is NULL
- CPL_ERROR_INVALID_TYPE if the passed image type is not supported
*/
/*----------------------------------------------------------------------------*/
cpl_image * cpl_image_collapse_create(const cpl_image * self,
int direction)
{
cpl_image * other
= cpl_image_collapse_window_create(self, 1, 1,
cpl_image_get_size_x(self),
cpl_image_get_size_y(self),
direction);
/* Propagate error, if any */
cpl_ensure(other != NULL, cpl_error_get_code(), NULL);
return other;
}
#define CPL_OPERATION CPL_IMAGE_BASIC_COLLAPSE_MEDIAN
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Collapse an image along its rows or columns, with filtering.
@param self Input image to collapse.
@param direction Collapsing direction.
@param drop_ll Ignore this many lower rows/leftmost columns
@param drop_ur Ignore this many upper rows/rightmost columns
@return 1 newly allocated image having 1 row or 1 column or NULL on error
@see cpl_image_collapse_create()
The collapsing direction is defined as for cpl_image_collapse_create().
For each output pixel, the median of the corresponding non-ignored pixels
is computed. A combination of bad pixels and drop parameters can cause a
median value in the output image to be undefined. Such pixels will be
flagged as bad and set to zero.
If the output would contain only bad pixels an error is set.
Images can be of type CPL_TYPE_INT, CPL_TYPE_FLOAT or CPL_TYPE_DOUBLE.
The returned image must be deallocated using cpl_image_delete().
Possible #_cpl_error_code_ set in this function:
- CPL_ERROR_NULL_INPUT if an input pointer is NULL
- CPL_ERROR_ILLEGAL_INPUT if a rejection parameter is negative,
or if the sum of ignored pixels is bigger than the image size
in the collapsing direction
- CPL_ERROR_INVALID_TYPE if the passed image type is not supported
- CPL_ERROR_DATA_NOT_FOUND if the output image would have only bad pixels
*/
/*----------------------------------------------------------------------------*/
cpl_image * cpl_image_collapse_median_create(const cpl_image * self,
int direction,
cpl_size drop_ll,
cpl_size drop_ur)
{
cpl_image * other = NULL;
const cpl_size ndrop = drop_ll + drop_ur;
cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
cpl_ensure(drop_ll >= 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
cpl_ensure(drop_ur >= 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
if (direction == 0) {
cpl_ensure(ndrop < self->ny, CPL_ERROR_ILLEGAL_INPUT, NULL);
} else if (direction == 1) {
cpl_ensure(ndrop < self->nx, CPL_ERROR_ILLEGAL_INPUT, NULL);
} else {
(void)cpl_error_set_(CPL_ERROR_ILLEGAL_INPUT);
return NULL;
}
/* Switch on image type */
switch (self->type) {
#define CPL_CLASS CPL_CLASS_DOUBLE
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_FLOAT
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_INT
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
default:
(void)cpl_error_set_(CPL_ERROR_INVALID_TYPE);
}
return other;
}
#undef CPL_OPERATION
#define CPL_OPERATION CPL_IMAGE_BASIC_EXTRACT
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Extract a rectangular zone from an image into another image.
@param in Input image
@param llx Lower left X coordinate
@param lly Lower left Y coordinate
@param urx Upper right X coordinate
@param ury Upper right Y coordinate
@return 1 newly allocated image or NULL on error
@note The returned image must be deallocated using cpl_image_delete()
The input coordinates define the extracted region by giving the coordinates
of the lower left and upper right corners (inclusive).
Coordinates must be provided in the FITS convention: lower left
corner of the image is at (1,1), x increasing from left to right,
y increasing from bottom to top.
Images can be of type CPL_TYPE_INT, CPL_TYPE_FLOAT or CPL_TYPE_DOUBLE.
Possible #_cpl_error_code_ set in this function:
- CPL_ERROR_NULL_INPUT if an input pointer is NULL
- CPL_ERROR_ILLEGAL_INPUT if the window coordinates are not valid
- CPL_ERROR_INVALID_TYPE if the passed image type is not supported
*/
/*----------------------------------------------------------------------------*/
cpl_image * cpl_image_extract(const cpl_image * in,
cpl_size llx,
cpl_size lly,
cpl_size urx,
cpl_size ury)
{
cpl_image * self = NULL;
cpl_ensure(in != NULL, CPL_ERROR_NULL_INPUT, NULL);
cpl_ensure(llx >= 1, CPL_ERROR_ILLEGAL_INPUT, NULL);
cpl_ensure(llx <= urx, CPL_ERROR_ILLEGAL_INPUT, NULL);
cpl_ensure(urx <= in->nx, CPL_ERROR_ILLEGAL_INPUT, NULL);
cpl_ensure(lly >= 1, CPL_ERROR_ILLEGAL_INPUT, NULL);
cpl_ensure(lly <= ury, CPL_ERROR_ILLEGAL_INPUT, NULL);
cpl_ensure(ury <= in->ny, CPL_ERROR_ILLEGAL_INPUT, NULL);
/* Switch on image type */
switch (in->type) {
#define CPL_CLASS CPL_CLASS_DOUBLE
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_FLOAT
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_INT
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_DOUBLE_COMPLEX
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_FLOAT_COMPLEX
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
default:
/* It is an error in CPL to enter here */
(void)cpl_error_set_(CPL_ERROR_INVALID_TYPE);
}
if (self != NULL) {
/* Bad pixels handling */
self->bpm = in->bpm == NULL ? NULL
: cpl_mask_extract(in->bpm, llx, lly, urx, ury);
}
return self;
}
#undef CPL_OPERATION
#define CPL_OPERATION CPL_IMAGE_BASIC_EXTRACTROW
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Extract a row from an image
@param image_in Input image
@param pos Position of the row (1 for the bottom one)
@return 1 newly allocated cpl_vector or NULL on error
Images can be of type CPL_TYPE_INT, CPL_TYPE_FLOAT or CPL_TYPE_DOUBLE.
The returned vector must be deallocated using cpl_vector_delete().
The bad pixels map is not taken into account in this function.
Possible #_cpl_error_code_ set in this function:
- CPL_ERROR_NULL_INPUT if an input pointer is NULL
- CPL_ERROR_ILLEGAL_INPUT if pos is not valid
- CPL_ERROR_INVALID_TYPE if the passed image type is not supported
*/
/*----------------------------------------------------------------------------*/
cpl_vector * cpl_vector_new_from_image_row(const cpl_image * image_in,
cpl_size pos)
{
cpl_vector * out;
double * out_data;
/* Test entries */
cpl_ensure(image_in, CPL_ERROR_NULL_INPUT, NULL);
cpl_ensure(pos>=1 && pos<=image_in->ny, CPL_ERROR_ILLEGAL_INPUT,NULL);
/* Allocate output vector */
out = cpl_vector_new(image_in->nx);
out_data = cpl_vector_get_data(out);
/* Switch on image type */
switch (image_in->type) {
#define CPL_CLASS CPL_CLASS_DOUBLE
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_FLOAT
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_INT
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
default:
cpl_vector_delete(out);
out = NULL;
(void)cpl_error_set_(CPL_ERROR_INVALID_TYPE);
}
return out;
}
#undef CPL_OPERATION
#define CPL_OPERATION CPL_IMAGE_BASIC_EXTRACTCOL
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Extract a column from an image
@param image_in Input image
@param pos Position of the column (1 for the left one)
@return 1 newly allocated cpl_vector or NULL on error
Images can be of type CPL_TYPE_INT, CPL_TYPE_FLOAT or CPL_TYPE_DOUBLE.
The returned vector must be deallocated using cpl_vector_delete().
The bad pixels map is not taken into account in this function.
Possible #_cpl_error_code_ set in this function:
- CPL_ERROR_NULL_INPUT if an input pointer is NULL
- CPL_ERROR_ILLEGAL_INPUT if pos is not valid
- CPL_ERROR_INVALID_TYPE if the passed image type is not supported
*/
/*----------------------------------------------------------------------------*/
cpl_vector * cpl_vector_new_from_image_column(const cpl_image * image_in,
cpl_size pos)
{
cpl_vector * out;
double * out_data;
/* Check entries */
cpl_ensure(image_in != NULL, CPL_ERROR_NULL_INPUT, NULL);
cpl_ensure(pos >= 1, CPL_ERROR_ILLEGAL_INPUT, NULL);
cpl_ensure(pos <= image_in->nx, CPL_ERROR_ILLEGAL_INPUT, NULL);
/* Allocate output vector */
out = cpl_vector_new(image_in->ny);
out_data = cpl_vector_get_data(out);
/* Switch on image type */
switch (image_in->type) {
#define CPL_CLASS CPL_CLASS_DOUBLE
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_FLOAT
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_INT
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
default:
cpl_vector_delete(out);
out = NULL;
(void)cpl_error_set_(CPL_ERROR_INVALID_TYPE);
}
return out;
}
#undef CPL_OPERATION
#define CPL_OPERATION CPL_IMAGE_BASIC_ROTATE_INT_LOCAL
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Rotate an image by a multiple of 90 degrees clockwise.
@param self The image to rotate in place.
@param rot The multiple: -1 is a rotation of 90 deg counterclockwise.
@return CPL_ERROR_NONE on success, otherwise the relevant #_cpl_error_code_
@note The dimension of a rectangular image is changed.
Images can be of type CPL_TYPE_INT, CPL_TYPE_FLOAT or CPL_TYPE_DOUBLE.
The definition of the rotation relies on the FITS convention:
The lower left corner of the image is at (1,1), x increasing from left to
right, y increasing from bottom to top.
For rotations of +90 or -90 degrees on rectangular non-1D-images,
the pixel buffer is temporarily duplicated.
rot may be any integer value, its modulo 4 determines the rotation:
- -3 to turn 270 degrees counterclockwise.
- -2 to turn 180 degrees counterclockwise.
- -1 to turn 90 degrees counterclockwise.
- 0 to not turn
- +1 to turn 90 degrees clockwise (same as -3)
- +2 to turn 180 degrees clockwise (same as -2).
- +3 to turn 270 degrees clockwise (same as -1).
Possible #_cpl_error_code_ set in this function:
- CPL_ERROR_NULL_INPUT if an input pointer is NULL
- CPL_ERROR_INVALID_TYPE if the passed image type is not supported
*/
/*----------------------------------------------------------------------------*/
cpl_error_code cpl_image_turn(cpl_image * self, int rot)
{
cpl_ensure_code(self, CPL_ERROR_NULL_INPUT);
rot %= 4;
if (rot < 0) rot += 4;
/* rot is 0, 1, 2 or 3. */
/* Rotate the bad pixel map */
if (rot != 0 && self->bpm != NULL) cpl_mask_turn(self->bpm, rot);
/* Switch on the image type */
switch (self->type) {
#define CPL_CLASS CPL_CLASS_DOUBLE
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_FLOAT
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_INT
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
default:
/* It is a bug in CPL to reach this point */
return cpl_error_set_(CPL_ERROR_INVALID_TYPE);
}
return CPL_ERROR_NONE;
}
#undef CPL_OPERATION
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Shift an image by integer offsets
@param self The image to shift in place
@param dx The shift in X
@param dy The shift in Y
@return the #_cpl_error_code_ or CPL_ERROR_NONE
The new zones (in the result image) where no new value is computed are set
to 0 and flagged as bad pixels.
The shift values have to be valid:
-nx < dx < nx and -ny < dy < ny
Possible #_cpl_error_code_ set in this function:
- CPL_ERROR_NULL_INPUT if an input pointer is NULL
- CPL_ERROR_ILLEGAL_INPUT if the requested shift is bigger than the
image size
*/
/*----------------------------------------------------------------------------*/
cpl_error_code cpl_image_shift(cpl_image * self,
cpl_size dx,
cpl_size dy)
{
cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
if (dx != 0 || dy != 0) {
/* Rejected pixels are set to a zero-bit-pattern */
if (cpl_tools_shift_window(self->pixels,
cpl_type_get_sizeof(self->type),
self->nx, self->ny, 0, dx, dy)) {
return cpl_error_set_where_();
}
/* Shift the bad pixel map */
if (self->bpm == NULL) self->bpm = cpl_mask_new(self->nx, self->ny);
/* Cannot fail now */
(void)cpl_mask_shift(self->bpm, dx, dy);
}
return CPL_ERROR_NONE;
}
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Copy one image into another
@param im1 the image in which im2 is inserted
@param im2 the inserted image
@param xpos the x pixel position in im1 where the lower left pixel of
im2 should go (from 1 to the x size of im1)
@param ypos the y pixel position in im1 where the lower left pixel of
im2 should go (from 1 to the y size of im1)
@return CPL_ERROR_NONE or the relevant #_cpl_error_code_ on error.
@note The two pixel buffers may not overlap
(xpos, ypos) must be a valid position in im1. If im2 is bigger than the place
left in im1, the part that falls outside of im1 is simply ignored, an no
error is raised.
The bad pixels are inherited from im2 in the concerned im1 zone.
The two input images must be of the same type, namely one of
CPL_TYPE_INT, CPL_TYPE_FLOAT, CPL_TYPE_DOUBLE.
Possible #_cpl_error_code_ set in this function:
- CPL_ERROR_TYPE_MISMATCH if the input images are of different types
- CPL_ERROR_INVALID_TYPE if the passed image type is not supported
- CPL_ERROR_ACCESS_OUT_OF_RANGE if xpos or ypos are outside the
specified range
*/
/*----------------------------------------------------------------------------*/
cpl_error_code cpl_image_copy(cpl_image * im1,
const cpl_image * im2,
cpl_size xpos,
cpl_size ypos)
{
/* FIXME: Support overlapping pixel buffers ? */
/* FIXME: Need to do pointer arithmetic */
char * pim1 = (char*)cpl_image_get_data(im1);
const cpl_size nx1 = cpl_image_get_size_x(im1);
const cpl_size ny1 = cpl_image_get_size_y(im1);
const cpl_size nx2 = cpl_image_get_size_x(im2);
const cpl_size ny2 = cpl_image_get_size_y(im2);
/* Define the zone to modify in im1: xpos, ypos, urx, ury */
const cpl_size urx = CX_MIN(nx1, nx2 + xpos - 1);
const cpl_size ury = CX_MIN(ny1, ny2 + ypos - 1);
const size_t pixsz = cpl_type_get_sizeof(cpl_image_get_type(im1));
const size_t linesz = (size_t)(urx - (xpos-1)) * pixsz;
/* Check entries */
cpl_ensure_code(im1 != NULL, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(im2 != NULL, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(xpos >= 1, CPL_ERROR_ACCESS_OUT_OF_RANGE);
cpl_ensure_code(xpos <= nx1, CPL_ERROR_ACCESS_OUT_OF_RANGE);
cpl_ensure_code(ypos >= 1, CPL_ERROR_ACCESS_OUT_OF_RANGE);
cpl_ensure_code(ypos <= ny1, CPL_ERROR_ACCESS_OUT_OF_RANGE);
cpl_ensure_code(im1->type == im2->type, CPL_ERROR_TYPE_MISMATCH);
pim1 += (ypos - 1) * nx1 * pixsz;
if (xpos == 1 && urx == nx1 && nx1 == nx2) {
/* The zone consists of whole lines in both in1 and in2 */
memcpy(pim1, im2->pixels, (ury - (ypos - 1)) * linesz);
} else {
/* FIXME: Need to do pointer arithmetic */
const char * pim2 = (const char*)im2->pixels;
const size_t sz1 = (size_t)nx1 * pixsz;
const size_t sz2 = (size_t)nx2 * pixsz;
cpl_size j;
pim1 += (size_t)(xpos - 1) * pixsz;
/* Loop on the zone */
for (j = ypos - 1; j < ury; j++, pim1 += sz1, pim2 += sz2) {
memcpy(pim1, pim2, linesz);
}
}
/* Handle the bad pixels */
if (im1->bpm != NULL || im2->bpm != NULL) {
cpl_mask * bpm2;
if (im1->bpm == NULL) im1->bpm = cpl_mask_new(im1->nx, im1->ny);
bpm2 = im2->bpm ? im2->bpm : cpl_mask_new(im2->nx, im2->ny);
cpl_mask_copy(im1->bpm, bpm2, xpos, ypos);
if (bpm2 != im2->bpm) cpl_mask_delete(bpm2);
/* FIXME: im1->bpm may be empty... */
}
return CPL_ERROR_NONE;
}
#define CPL_OPERATION CPL_IMAGE_BASIC_FLIP_LOCAL
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Flip an image on a given mirror line.
@param im the image to flip.
@param angle mirror line in polar coord. is theta = (PI/4) * angle
@return the #_cpl_error_code_ or CPL_ERROR_NONE
This function operates locally on the pixel buffer.
angle can take one of the following values:
- 0 (theta=0) to flip the image around the horizontal
- 1 (theta=pi/4) to flip the image around y=x
- 2 (theta=pi/2) to flip the image around the vertical
- 3 (theta=3pi/4) to flip the image around y=-x
Images can be of type CPL_TYPE_INT, CPL_TYPE_FLOAT or CPL_TYPE_DOUBLE.
Possible #_cpl_error_code_ set in this function:
- CPL_ERROR_NULL_INPUT if an input pointer is NULL
- CPL_ERROR_ILLEGAL_INPUT if the angle is different from the allowed values
- CPL_ERROR_INVALID_TYPE if the passed image type is not supported
*/
/*----------------------------------------------------------------------------*/
cpl_error_code cpl_image_flip(
cpl_image * im,
int angle)
{
/* Check entries */
cpl_ensure_code(im != NULL, CPL_ERROR_NULL_INPUT);
/* Switch on the image type */
switch (im->type) {
#define CPL_CLASS CPL_CLASS_DOUBLE
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_FLOAT
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_INT
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
default:
return cpl_error_set_(CPL_ERROR_INVALID_TYPE);
}
/* Flip the bad pixel map */
if (im->bpm != NULL) cpl_mask_flip(im->bpm, angle);
return CPL_ERROR_NONE;
}
#undef CPL_OPERATION
#define CPL_OPERATION CPL_IMAGE_BASIC_MOVE_PIXELS
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Reorganize the pixels in an image
@param im the image to reorganize
@param nb_cut the number of cut in x and y
@param new_pos array with the nb_cut^2 new positions
@return the #_cpl_error_code_ or CPL_ERROR_NONE
nb_cut^2 defines in how many tiles the images will be moved. Each tile will
then be moved to an other place defined in new_pos. 1 will leave the image
unchanged, 2 is used to move the quadrants, etc..
new_pos contains nb_cut^2 values between 1 and nb_cut^2.
The zones positions are counted from the lower left part of the image.
It is not allowed to move two tiles to the same position (the relation
between th new tiles positions and the initial position is bijective !).
The image x and y sizes have to be multiples of nb_cut.
@verbatim
Example:
16 17 18 6 5 4
13 14 15 3 2 1
10 11 12 ----> 12 11 10
7 8 9 9 8 7
4 5 6 18 17 16
1 2 3 15 14 13
image 3x6 cpl_image_move(image, 3, new_pos);
with new_pos = {9,8,7,6,5,4,3,2,1};
@endverbatim
The bad pixels are moved accordingly.
Possible #_cpl_error_code_ set in this function:
- CPL_ERROR_NULL_INPUT if an input pointer is NULL
- CPL_ERROR_ILLEGAL_INPUT if nb_cut is not strictly positive or cannot
divide one of the image sizes or if the new_pos array specifies to
move two tiles to the same position.
- CPL_ERROR_INVALID_TYPE if the passed image type is not supported
*/
/*----------------------------------------------------------------------------*/
cpl_error_code cpl_image_move(cpl_image * im,
cpl_size nb_cut,
const cpl_size * new_pos)
{
cpl_size test_sum;
cpl_size tile_sz_x, tile_sz_y;
cpl_size tile_x, tile_y;
cpl_size npos, opos;
cpl_size i, j, k, l;
/* Check entries */
cpl_ensure_code(im, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(new_pos, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(nb_cut > 0, CPL_ERROR_ILLEGAL_INPUT);
cpl_ensure_code(im->nx % nb_cut == 0, CPL_ERROR_ILLEGAL_INPUT);
cpl_ensure_code(im->ny % nb_cut == 0, CPL_ERROR_ILLEGAL_INPUT);
/* Test that new_pos takes all values between 1 and nb_cut*nb_cut */
/* The test here is not strict, but should be sufficient */
test_sum = 0;
for (i=0; i<nb_cut*nb_cut; i++) test_sum += new_pos[i];
cpl_ensure_code(test_sum==nb_cut*nb_cut*(nb_cut*nb_cut+1)/2,
CPL_ERROR_ILLEGAL_INPUT);
/* Initialize */
tile_sz_x = im->nx / nb_cut;
tile_sz_y = im->ny / nb_cut;
/* Switch on the image type */
switch (im->type) {
#define CPL_CLASS CPL_CLASS_DOUBLE
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_FLOAT
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_INT
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_DOUBLE_COMPLEX
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_FLOAT_COMPLEX
#include "cpl_image_basic_body.h"
#undef CPL_CLASS
default:
return cpl_error_set_(CPL_ERROR_INVALID_TYPE);
}
/* Handle the bad pixels */
if (im->bpm != NULL) cpl_mask_move(im->bpm, nb_cut, new_pos);
return CPL_ERROR_NONE;
}
#undef CPL_OPERATION
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Apply a gaussian fit on an image sub window
@param im the input image
@param xpos the x position of the center (1 for the first pixel)
@param ypos the y position of the center (1 for the first pixel)
@param size the window size in pixels, at least 4
@param norm the norm of the gaussian or NULL
@param xcen the x center of the gaussian or NULL
@param ycen the y center of the gaussian or NULL
@param sig_x the semi-major axis of the gaussian or NULL
@param sig_y the semi-minor axis of the gaussian or NULL
@param fwhm_x the FHHM in x or NULL
@param fwhm_y the FHHM in y or NULL
@return the #_cpl_error_code_ or CPL_ERROR_NONE
@see cpl_fit_image_gaussian()
@see cpl_image_iqe()
@deprecated If you need a 2D gaussian fit please
use the function @em cpl_fit_image_gaussian(). Please note that
on CPL versions earlier than 5.1.0 this function was wrongly
documented: the parameters @em sig_x and @em sig_y were defined
as "the sigma in x (or y) of the gaussian", while actually they
returned the semi-major and semi-minor axes of the gaussian distribution
at 1-sigma. <b><i>PLEASE NOTE THAT IF YOU USED THIS FUNCTION FOR DETERMINING
THE SPREAD OF A DISTRIBUTION ALONG THE X DIRECTION, THIS WAS VERY
LIKELY OVERESTIMATED</i></b> (because @em sig_x was always assigned the
semi-major axis of the distribution ignoring the rotation),
<b><i>WHILE THE SPREAD ALONG THE Y DIRECTION WOULD BE UNDERESTIMATED</i></b>.
In addition to that, even with circular distributions this function
may lead to an underestimation of @em sig_x and @em sig_y (up to 25%
underestimation in the case of noiseless data with a box 4 times the
sigma, 1% underestimation in the case of noiseless data with a box 7
times the sigma). This latter problem is related to the function
@em cpl_image_iqe().
This function is only acceptable for determining the position of a peak.
*/
/*----------------------------------------------------------------------------*/
cpl_error_code cpl_image_fit_gaussian(
const cpl_image * im,
cpl_size xpos,
cpl_size ypos,
cpl_size size,
double * norm,
double * xcen,
double * ycen,
double * sig_x,
double * sig_y,
double * fwhm_x,
double * fwhm_y)
{
const cpl_image * im_use;
cpl_image * im_cast = NULL;
cpl_size llx, lly, urx, ury;
cpl_bivector * stats;
double * pstats;
/* Check entries */
cpl_ensure_code(im != NULL, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(xpos >= 1, CPL_ERROR_ILLEGAL_INPUT);
cpl_ensure_code(ypos >= 1, CPL_ERROR_ILLEGAL_INPUT);
cpl_ensure_code(xpos <= im->nx, CPL_ERROR_ILLEGAL_INPUT);
cpl_ensure_code(ypos <= im->ny, CPL_ERROR_ILLEGAL_INPUT);
cpl_ensure_code(size >= 1, CPL_ERROR_ILLEGAL_INPUT);
cpl_ensure_code(size < im->nx, CPL_ERROR_ILLEGAL_INPUT);
cpl_ensure_code(size < im->ny, CPL_ERROR_ILLEGAL_INPUT);
/* Extraction zone */
llx = xpos - size / 2;
lly = ypos - size / 2;
urx = xpos + size / 2;
ury = ypos + size / 2;
if (llx < 1) llx = 1;
if (lly < 1) lly = 1;
if (urx > im->nx) urx = im->nx;
if (ury > im->ny) ury = im->ny;
/* Convert the image to FLOAT, if needed */
im_use = cpl_image_get_type(im) == CPL_TYPE_FLOAT ? im :
(im_cast = cpl_image_cast(im, CPL_TYPE_FLOAT));
/* Call cpl_image_iqe */
stats = cpl_image_iqe(im_use, llx, lly, urx, ury);
cpl_image_delete(im_cast);
if (stats == NULL) return cpl_error_set_where_();
/* Write the results */
pstats = cpl_bivector_get_x_data(stats);
if (xcen) *xcen = pstats[0];
if (ycen) *ycen = pstats[1];
if (fwhm_x) *fwhm_x = pstats[2];
if (fwhm_y) *fwhm_y = pstats[3];
if (sig_x) *sig_x = pstats[2] / CPL_MATH_FWHM_SIG;
if (sig_y) *sig_y = pstats[3] / CPL_MATH_FWHM_SIG;
if (norm) *norm = pstats[5] * CPL_MATH_2PI *
(pstats[2]*pstats[3]) / (CPL_MATH_FWHM_SIG*CPL_MATH_FWHM_SIG);
cpl_bivector_delete(stats);
return CPL_ERROR_NONE;
}
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Compute FWHM values in x and y for an object
@param in the input image
@param xpos the x position of the object (1 for the first pixel)
@param ypos the y position of the object (1 for the first pixel)
@param fwhm_x the computed FWHM in x or -1 on error
@param fwhm_y the computed FWHM in y or -1 on error
@return CPL_ERROR_NONE or the relevant #_cpl_error_code_
This function uses a basic method: start from the center of the object
and go away until the half maximum value is reached in x and y.
For the FWHM in x (resp. y) to be computed, the image size in the x (resp. y)
direction should be at least of 5 pixels.
If for any reason, one of the FHWMs cannot be computed, its returned value
is -1.0, but an error is not necessarily raised. For example, if a 4 column
image is passed, the fwhm_x would be -1.0, the fwhm_y would be correctly
computed, and no error would be raised.
Possible #_cpl_error_code_ set in this function:
- CPL_ERROR_NULL_INPUT if an input pointer is NULL
- CPL_ERROR_DATA_NOT_FOUND if (xpos, ypos) specifies a rejected pixel or a
pixel with a non-positive value
- CPL_ERROR_ACCESS_OUT_OF_RANGE if xpos or ypos is outside the image
size range
*/
/*----------------------------------------------------------------------------*/
cpl_error_code cpl_image_get_fwhm(
const cpl_image * in,
cpl_size xpos,
cpl_size ypos,
double * fwhm_x,
double * fwhm_y)
{
double half_max;
double thres;
const cpl_size minimum_size = 5;
int is_rejected;
/* Check entries - and initialize *fwhm_{x,y} */
if (fwhm_y != NULL) *fwhm_y = -1;
cpl_ensure_code(fwhm_x, CPL_ERROR_NULL_INPUT);
*fwhm_x = -1;
cpl_ensure_code(fwhm_y, CPL_ERROR_NULL_INPUT);
/* This call will check the validity of image, xpos and ypos */
half_max = 0.5 * cpl_image_get(in, xpos, ypos, &is_rejected);
cpl_ensure_code(is_rejected >= 0, cpl_error_get_code());
cpl_ensure_code(!is_rejected, CPL_ERROR_DATA_NOT_FOUND);
cpl_ensure_code(half_max > 0, CPL_ERROR_DATA_NOT_FOUND);
/* FWHM in x */
if (in->nx >= minimum_size) {
cpl_errorstate pstate;
/* Extract the vector centered on the maximum */
cpl_vector * row = cpl_vector_new_from_image_row(in, ypos);
/* If an error happened, update its location */
cpl_ensure_code(row, cpl_error_get_code());
pstate = cpl_errorstate_get();
/* Find out threshold */
thres = cpl_vector_get_noise(row, xpos);
/* Compute the FWHM */
if (cpl_errorstate_is_equal(pstate))
*fwhm_x = cpl_vector_get_fwhm(row, xpos, half_max + thres * 0.5);
cpl_vector_delete(row);
/* Propagate the error, if any */
cpl_ensure_code(cpl_errorstate_is_equal(pstate), cpl_error_get_code());
}
/* FWHM in y */
if (in->ny >= minimum_size) {
cpl_errorstate pstate;
/* Extract the vector centered on the maximum */
cpl_vector * col = cpl_vector_new_from_image_column(in, xpos);
/* If an error happened, update its location */
cpl_ensure_code(col, cpl_error_get_code());
pstate = cpl_errorstate_get();
/* Find out threshold */
thres = cpl_vector_get_noise(col, ypos);
/* Compute the FWHM */
if (cpl_errorstate_is_equal(pstate))
*fwhm_y = cpl_vector_get_fwhm(col, ypos, half_max + thres * 0.5);
cpl_vector_delete(col);
/* Propagate the error, if any */
cpl_ensure_code(cpl_errorstate_is_equal(pstate), cpl_error_get_code());
}
return CPL_ERROR_NONE;
}
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Fast Fourier Transform a square, power-of-two sized image
@param img_real The image real part to be transformed in place
@param img_imag The image imaginary part to be transformed in place
@param mode The desired FFT options (combined with bitwise or)
@return CPL_ERROR_NONE or the relevant #_cpl_error_code_ on error
The input images must be of double type.
If the second passed image is NULL, the resulting imaginary part
cannot be returned. This can be useful if the input is real and the
output is known to also be real. But if the output has a significant
imaginary part, you might want to pass a 0-valued image as the second
parameter.
Any rejected pixel is used as if it were a good pixel.
The image must be square with a size that is a power of two.
These are the supported FFT modes:
CPL_FFT_DEFAULT: Default, forward FFT transform
CPL_FFT_INVERSE: Inverse FFT transform
CPL_FFT_UNNORMALIZED: Do not normalize (with N*N for N-by-N image) on inverse.
Has no effect on forward transform.
CPL_FFT_SWAP_HALVES: Swap the four quadrants of the result image.
Possible #_cpl_error_code_ set in this function:
- CPL_ERROR_NULL_INPUT if (one of) the input pointer(s) is NULL
- CPL_ERROR_ILLEGAL_INPUT if the image is not square or if the image size is
not a power of 2.
- CPL_ERROR_INVALID_TYPE if mode is 1, e.g. due to a logical or (||) of the
allowed FFT options.
- CPL_ERROR_UNSUPPORTED_MODE if mode is otherwise different from the allowed
FFT options.
- CPL_ERROR_INVALID_TYPE if the passed image type is not supported
*/
/*----------------------------------------------------------------------------*/
cpl_error_code cpl_image_fft(
cpl_image * img_real,
cpl_image * img_imag,
unsigned mode)
{
unsigned dim[2];
double * imag_part;
/* Check entries */
cpl_ensure_code(img_real, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(mode != 1, CPL_ERROR_INVALID_TYPE);
cpl_ensure_code(
mode <= (CPL_FFT_INVERSE|CPL_FFT_UNNORMALIZED|CPL_FFT_SWAP_HALVES),
CPL_ERROR_UNSUPPORTED_MODE);
cpl_ensure_code(img_real->nx==img_real->ny, CPL_ERROR_ILLEGAL_INPUT);
cpl_ensure_code(img_real->type==CPL_TYPE_DOUBLE,CPL_ERROR_INVALID_TYPE);
cpl_ensure_code(cpl_tools_is_power_of_2(img_real->nx)>=0,
CPL_ERROR_ILLEGAL_INPUT);
if (img_imag != NULL) {
cpl_ensure_code(img_real->nx==img_imag->nx,CPL_ERROR_ILLEGAL_INPUT);
cpl_ensure_code(img_real->ny==img_imag->ny,CPL_ERROR_ILLEGAL_INPUT);
cpl_ensure_code(img_imag->type==CPL_TYPE_DOUBLE,
CPL_ERROR_INVALID_TYPE);
}
/* Initialize */
dim[0] = dim[1] = (unsigned)img_real->nx;
cpl_ensure_code((cpl_size)dim[0] == img_real->nx, CPL_ERROR_ILLEGAL_INPUT);
if (img_imag == NULL) {
/* Create the imaginary part and set it to 0 */
imag_part = cpl_calloc(dim[0]*dim[1], sizeof(double));
} else {
/* Put the input imaginary part in a local object */
imag_part = img_imag->pixels;
}
/* APPLY THE FFT HERE */
cpl_ensure_code(!cpl_fft(img_real->pixels, imag_part, dim, 2,
(mode & CPL_FFT_INVERSE) ? -1 : 1),cpl_error_get_code());
/* Free the imaginary part result in the input image */
if (img_imag == NULL) {
cpl_free(imag_part);
}
/* Normalize on the inverse transform */
if (!(mode & CPL_FFT_UNNORMALIZED) && (mode & CPL_FFT_INVERSE)) {
cpl_ensure_code(!cpl_image_divide_scalar(img_real, dim[0]*dim[1]),
cpl_error_get_code());
if (img_imag != NULL) {
cpl_ensure_code(!cpl_image_divide_scalar(img_imag, dim[0]*dim[1]),
cpl_error_get_code());
}
}
/* Swap halves in both dimensions */
if (mode & CPL_FFT_SWAP_HALVES) {
const cpl_size new_pos[] = {4, 3, 2, 1};
cpl_ensure_code(!cpl_image_move(img_real, 2, new_pos),
cpl_error_get_code());
if (img_imag != NULL) {
cpl_ensure_code(!cpl_image_move(img_imag, 2, new_pos),
cpl_error_get_code());
}
}
return CPL_ERROR_NONE;
}
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief The bit-wise or of the input masks onto the output mask
@param self Pre-allocated image to hold the result
@param first First operand, or NULL for an in-place operation
@param second Second operand
@return void
@note Error checking assumed to have been performed by the caller
*/
/*----------------------------------------------------------------------------*/
void cpl_image_or_mask(cpl_image * self,
const cpl_image * first,
const cpl_image * second)
{
const size_t nxy = (size_t)self->nx * (size_t)self->ny;
if (self->bpm == NULL) {
/* Create the bad pixel map, if an input one is non-NULL */
if (first != NULL && first->bpm != NULL && second->bpm != NULL) {
self->bpm = cpl_mask_wrap(self->nx, self->ny, cpl_malloc(nxy));
cpl_mask_or_(cpl_mask_get_data(self->bpm),
cpl_mask_get_data_const(first->bpm),
cpl_mask_get_data_const(second->bpm), nxy);
} else if (second->bpm != NULL) {
self->bpm = cpl_mask_duplicate(second->bpm);
} else if (first != NULL && first->bpm != NULL) {
self->bpm = cpl_mask_duplicate(first->bpm);
}
} else {
/* The self bpm is non-NULL. If first is NULL, then the operation is
in-place, so self is an input parameter */
if (first != NULL && first->bpm != NULL && second->bpm != NULL) {
cpl_mask_or_(cpl_mask_get_data(self->bpm),
cpl_mask_get_data_const(first->bpm),
cpl_mask_get_data_const(second->bpm), nxy);
} else if (first != NULL && first->bpm != NULL) {
assert(second->bpm == NULL);
(void)memcpy(cpl_mask_get_data(self->bpm),
cpl_mask_get_data_const(first->bpm), nxy);
} else if (second->bpm != NULL) {
if (first == NULL) {
/* Self is an input parameter */
cpl_mask_or(self->bpm, second->bpm);
} else {
/* First is non-NULL, but happens to have a NULL bpm */
assert(first->bpm == NULL);
(void)memcpy(cpl_mask_get_data(self->bpm),
cpl_mask_get_data_const(second->bpm), nxy);
}
} else if (first != NULL) {
/* First is non-NULL, but happens to have a NULL bpm,
so the result is an empty bpm */
(void)memset(cpl_mask_get_data(self->bpm), 0, nxy);
}
}
}
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief The bit-wise or of the input mask(s) onto the output mask
@param self Pre-allocated image to hold the result
@param first First operand, or NULL for an in-place operation
@return void
@note Error checking assumed to have been performed by the caller
@see cpl_image_or_mask
*/
/*----------------------------------------------------------------------------*/
void cpl_image_or_mask_unary(cpl_image * self,
const cpl_image * first)
{
const size_t nxy = (size_t)self->nx * (size_t)self->ny;
if (self->bpm == NULL) {
/* Create the bad pixel map, if an input one is non-NULL */
if (first != NULL && first->bpm != NULL) {
self->bpm = cpl_mask_duplicate(first->bpm);
}
} else {
/* The self bpm is non-NULL. If first is NULL, then the operation is
in-place, so self is an input parameter */
if (first != NULL && first->bpm != NULL) {
(void)memcpy(cpl_mask_get_data(self->bpm),
cpl_mask_get_data_const(first->bpm), nxy);
} else if (first != NULL) {
/* First is non-NULL, but happens to have a NULL bpm,
so the result is an empty bpm */
(void)memset(cpl_mask_get_data(self->bpm), 0, nxy);
}
}
}
/*----------------------------------------------------------------------------*/
/**
@internal
@ingroup cpl_image
@brief N-dimensional FFT.
@param real N-dimensional data set stored in 1d (real part).
@param imag N-dimensional data set stored in 1d (imaginary part).
@param nn Dimensions of the set.
@param ndim How many dimensions this set has.
@param isign Transform direction.
@return the #_cpl_error_code_ or CPL_ERROR_NONE
This routine is a public domain FFT. See extract of Usenet article below.
Found on http://www.tu-chemnitz.de/~arndt/joerg.html
@verbatim
From: alee@tybalt.caltech.edu (Andrew Lee)
Newsgroups: comp.sources.misc
Subject: N-dimensional, Radix 2 FFT Routine
Date: 17 Jul 87 22:26:29 GMT
Approved: allbery@ncoast.UUCP
X-Archive: comp.sources.misc/8707/48
[..]
Now for the usage (finally):
real[] and imag[] are the array of complex numbers to be transformed,
nn[] is the array giving the dimensions (I mean size) of the array,
ndim is the number of dimensions of the array, and
isign is +1 for a forward transform, and -1 for an inverse transform.
real[], imag[] and nn[] are stored in the "natural" order for C:
nn[0] gives the number of elements along the leftmost index,
nn[ndim - 1] gives the number of elements along the rightmost index.
Additional notes: The routine does NO NORMALIZATION, so if you do a forward,
and then an inverse transform on an array, the result will be identical to
the original array MULTIPLIED BY THE NUMBER OF ELEMENTS IN THE ARRAY. Also,
of course, the dimensions of real[] and imag[] must all be powers of 2.
@endverbatim
Possible #_cpl_error_code_ set in this function:
- CPL_ERROR_NULL_INPUT if an input pointer is NULL
*/
/*----------------------------------------------------------------------------*/
static cpl_error_code cpl_fft(
double * real,
double * imag,
const unsigned * nn,
int ndim,
int isign)
{
int idim;
unsigned i1, i2rev, i3rev, ibit;
unsigned ip2, ifp1, ifp2, k2, n;
unsigned nprev = 1, ntot = 1;
register unsigned i2, i3;
double theta;
double w_r, w_i, wp_r, wp_i;
double wtemp;
double temp_r, temp_i, wt_r, wt_i;
double t1, t2;
/* Check entries */
cpl_ensure_code(real, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(imag, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(nn, CPL_ERROR_NULL_INPUT);
for (idim = 0; idim < ndim; ++idim)
cpl_ensure_code(cpl_tools_is_power_of_2(nn[idim])>=0,
CPL_ERROR_ILLEGAL_INPUT);
/* Compute total number of complex values */
for (idim = 0; idim < ndim; ++idim) ntot *= nn[idim];
for (idim = ndim - 1; idim >= 0; --idim) {
n = nn[idim];
ip2 = nprev * n; /* Unit step for next dimension */
i2rev = 0; /* Bit reversed i2 */
/* This is the bit reversal section of the routine */
/* Loop over current dimension */
for (i2 = 0; i2 < ip2; i2 += nprev) {
if (i2 < i2rev)
/* Loop over lower dimensions */
for (i1 = i2; i1 < i2 + nprev; ++i1)
/* Loop over higher dimensions */
for (i3 = i1; i3 < ntot; i3 += ip2) {
i3rev = i3 + i2rev - i2;
temp_r = real[i3];
temp_i = imag[i3];
real[i3] = real[i3rev];
imag[i3] = imag[i3rev];
real[i3rev] = temp_r;
imag[i3rev] = temp_i;
}
ibit = ip2;
/* Increment from high end of i2rev to low */
do {
ibit >>= 1;
i2rev ^= ibit;
} while (ibit >= nprev && !(ibit & i2rev));
}
/* Here begins the Danielson-Lanczos section of the routine */
/* Loop over step sizes */
for (ifp1 = nprev; ifp1 < ip2; ifp1 <<= 1) {
ifp2 = ifp1 << 1;
/* Initialize for the trig. recurrence */
theta = isign * CPL_MATH_2PI / (ifp2 / nprev);
wp_r = sin(0.5 * theta);
wp_r *= -2.0 * wp_r;
wp_i = sin(theta);
w_r = 1.0;
w_i = 0.0;
/* Loop by unit step in current dimension */
for (i3 = 0; i3 < ifp1; i3 += nprev) {
/* Loop over lower dimensions */
for (i1 = i3; i1 < i3 + nprev; ++i1)
/* Loop over higher dimensions */
for (i2 = i1; i2 < ntot; i2 += ifp2) {
/* Danielson-Lanczos formula */
k2 = i2 + ifp1;
wt_r = real[k2];
wt_i = imag[k2];
/* Complex multiply using 3 real multiplies. */
real[k2] = real[i2] - (temp_r =
(t1 = w_r * wt_r) - (t2 = w_i * wt_i));
imag[k2] = imag[i2] - (temp_i =
(w_r + w_i) * (wt_r + wt_i) - t1 - t2);
real[i2] += temp_r;
imag[i2] += temp_i;
}
/* Trigonometric recurrence */
wtemp = w_r;
/* Complex multiply using 3 real multiplies. */
w_r += (t1 = w_r * wp_r) - (t2 = w_i * wp_i);
w_i += (wtemp + w_i) * (wp_r + wp_i) - t1 - t2;
}
}
nprev *= n;
}
return CPL_ERROR_NONE;
}
/*----------------------------------------------------------------------------*/
/**
@internal
@ingroup cpl_image
@brief Compute the noise around a peak in a vector
@param vec the input cpl_vector
@param pos the peak position (from 1 to the vector size)
@return the noise value.
The passed cpl_vector object must have at least two elements.
In case of error, the #_cpl_error_code_ code is set, and the returned double
is undefined.
Possible #_cpl_error_code_ set in this function:
- CPL_ERROR_NULL_INPUT if an input pointer is NULL
- CPL_ERROR_ILLEGAL_INPUT
*/
/*----------------------------------------------------------------------------*/
static double cpl_vector_get_noise(
const cpl_vector * vec,
cpl_size pos)
{
cpl_size nelem;
cpl_vector * smooth_vec;
const double * vector_data;
double noise_left, noise_right;
cpl_errorstate prestate = cpl_errorstate_get();
cpl_size i;
/* Check entries */
cpl_ensure(vec, CPL_ERROR_NULL_INPUT, -1.0);
nelem = cpl_vector_get_size(vec);
cpl_ensure(pos >= 1, CPL_ERROR_ILLEGAL_INPUT, -1.0);
cpl_ensure(pos <= nelem, CPL_ERROR_ILLEGAL_INPUT, -1.0);
cpl_ensure(nelem > 1, CPL_ERROR_ILLEGAL_INPUT, -1.0);
/* Smooth out the array to be less sensitive to noise */
smooth_vec = cpl_vector_filter_lowpass_create(vec, CPL_LOWPASS_LINEAR, 1);
if (!cpl_errorstate_is_equal(prestate)) {
/* Recover and use the unsmoothed vector */
cpl_errorstate_set(prestate);
cpl_vector_delete(smooth_vec);
smooth_vec = NULL;
}
vector_data = cpl_vector_get_data_const(smooth_vec ? smooth_vec : vec);
if (smooth_vec != NULL) {
/* The smoothing (half-size is 1) may have moved the maximum 1 pixel */
if (pos < nelem && vector_data[pos-1] < vector_data[pos]) {
pos++;
} else if (pos > 1 && vector_data[pos-1] < vector_data[pos-2]) {
pos--;
}
}
/* Find noise level on the left side of the peak. */
i = pos - 1;
while (i > 0) {
if (vector_data[i] > vector_data[i-1]) i--;
else break;
}
noise_left = vector_data[i];
/* Find noise level on the right side of the peak */
i = pos - 1;
while (i < nelem-1) {
if (vector_data[i] > vector_data[i+1]) i++;
else break;
}
noise_right = vector_data[i];
cpl_vector_delete(smooth_vec);
return 0.5 * (noise_left + noise_right);
}
/*----------------------------------------------------------------------------*/
/**
@internal
@ingroup cpl_image
@brief Compute the FWHM of an object in a cpl_vector
@param vec the input cpl_vector
@param pos the object position (from 1 to the vector size)
@param half_max the half maximum value
@return the FWHM or a negative value on error
@note The return value may be -1 with no error condition
Possible #_cpl_error_code_ set in this function:
- CPL_ERROR_NULL_INPUT if an input pointer is NULL
- CPL_ERROR_ILLEGAL_INPUT if pos is less than 1 or greater than the vec-length
*/
/*----------------------------------------------------------------------------*/
static double cpl_vector_get_fwhm(
const cpl_vector * vec,
cpl_size pos,
double half_max)
{
const double * vec_data;
cpl_size nelem;
double x_left, x_right;
double y_1, y_2;
cpl_size i;
/* Check entries */
cpl_ensure(vec, CPL_ERROR_NULL_INPUT, -1.0);
nelem = cpl_vector_get_size(vec);
cpl_ensure(pos >= 1, CPL_ERROR_ILLEGAL_INPUT, -1.0);
cpl_ensure(pos <= nelem, CPL_ERROR_ILLEGAL_INPUT, -1.0);
vec_data = cpl_vector_get_data_const(vec);
/* Object may be too noisy - or strange in some other way */
if (vec_data[pos - 1] <= half_max) return -1.0;
/* Find first pair of values, y(i) <= half_max < y(i+1)
on the left of the maximum */
i = pos - 1;
while ((vec_data[i] > half_max) && (i > 0)) i--;
if (vec_data[i] > half_max) return -1.0; /* y_1 could not be found */
y_1 = vec_data[i];
y_2 = vec_data[i+1];
/* assert ( y_1 <= half_max && half_max < y_2 ); */
/* Assume linearity between y_1 and y_2 */
x_left = (double)i + (half_max-y_1) / (y_2-y_1);
/* assert( x_left >= i ); */
/* Find first pair of values, y(i-1) > half_max >= y(i)
on the right of the maximum */
i = pos - 1;
while ((vec_data[i] > half_max) && (i < nelem-1)) i++;
if (vec_data[i] > half_max) return -1.0; /* y_2 could not be found */
y_1 = vec_data[i-1];
y_2 = vec_data[i];
/* assert( y_1 > half_max && half_max >= y_2 ); */
/* Assume linearity between y_1 and y_2 */
x_right = (double)i + (half_max-y_2) / (y_2-y_1);
/* assert( x_right < i ); */
if (x_right < x_left || x_right - x_left > FLT_MAX) return -1;
return x_right - x_left;
}
#if (defined __SSE3__ || defined __SSE2__)
/*----------------------------------------------------------------------------*/
/**
@internal
@ingroup cpl_image
@brief multiply two vectors of two complex floats
@param a first operand
@param b second operand
@param i offset
@return result of the multiplication
@see fcompl_mult_sse_aligned()
Function not inlineable to work around code generation issue of gcc.
The pointer additions must be hidden in this function or gcc will pointlessly
add them to the inner loop of the complex multiplication even though they
are only required in the unlikely branch.
Tested with gcc <= 4.7. Up to 30% speed improvement on some machines.
*/
/*----------------------------------------------------------------------------*/
static void CPL_ATTR_NOINLINE
fcompl_mult_scalar2(float complex * a, const float complex * b, const size_t i)
{
a[i] = a[i] * b[i];
a[i + 1] = a[i + 1] * b[i + 1];
}
/*----------------------------------------------------------------------------*/
/**
@internal
@ingroup cpl_image
@brief check result for NaN and recompute correctly if so
@return true if nan was fixed and stored, false if no fixup required
if false no store was done
@see fcompl_mult_sse_aligned()
*/
/*----------------------------------------------------------------------------*/
static inline cpl_boolean
fcompl_fix_nan(__m128 res, float complex * a,
const float complex * b,
const size_t i)
{
#ifndef __FAST_MATH__
/* check for NaN, redo all in libc if found */
__m128 n = _mm_cmpneq_ps(res, res);
if (CPL_UNLIKELY(_mm_movemask_ps(n) != 0)) {
fcompl_mult_scalar2(a, b, i);
return CPL_TRUE;
}
else
#endif
return CPL_FALSE;
}
/*----------------------------------------------------------------------------*/
/**
@internal
@ingroup cpl_image
@brief vertical multiply two vectors of each two complex floats
@param va first operand (real, imag, real, imag)
@param vb second operand (real, imag, real, imag)
@return result of the multiplication
does not handle NaN's correctly
*/
/*----------------------------------------------------------------------------*/
static inline __m128
fcompl_mult_sse_fast(__m128 va, __m128 vb)
{
/* optimized to SSE3 _mm_move[hl]dup_ps by gcc */
__m128 reala = _mm_shuffle_ps(va, va, _MM_SHUFFLE(2, 2, 0, 0)); /* x x */
__m128 imaga = _mm_shuffle_ps(va, va, _MM_SHUFFLE(3, 3, 1, 1)); /* w w */
__m128 t1 = _mm_mul_ps(reala, vb); /* x*y x*z */
__m128 sb = _mm_shuffle_ps(vb, vb, _MM_SHUFFLE(2, 3, 0, 1)); /* z y */
__m128 t2 = _mm_mul_ps(imaga, sb); /* w*z w*y */
return CPL_MM_ADDSUB_PS(t1, t2); /* x*y-w*z x*z+w*y*/
}
/*----------------------------------------------------------------------------*/
/**
@internal
@ingroup cpl_image
@brief complex multiplication loop, aligned
@param im1 first operand, must be 16 byte aligned
@param im2 second operand, must be 16 byte aligned
@param Nt number of pixels
@return the #_cpl_error_code_ or CPL_ERROR_NONE
*/
/*----------------------------------------------------------------------------*/
static cpl_error_code
fcompl_mult_sse_aligned(float complex * a,
const float complex * b,
const size_t Nt)
{
const size_t n = (Nt % 2) == 1 ? Nt - 1 : Nt;
/* no overflow hint for the compiler */
cpl_ensure_code(n < SIZE_MAX-1, CPL_ERROR_ACCESS_OUT_OF_RANGE);
for (size_t i = 0; i < n; i+=2) {
__m128 va = _mm_load_ps((const float *)&a[i]); /* x w */
__m128 vb = _mm_load_ps((const float *)&b[i]); /* y z */
__m128 res = fcompl_mult_sse_fast(va, vb);
if (CPL_LIKELY(fcompl_fix_nan(res, a, b, i) == CPL_FALSE))
_mm_store_ps((float *)&a[i], res);
}
if ((Nt) % 2 == 1)
a[Nt - 1] = a[Nt - 1] * b[Nt - 1];
return CPL_ERROR_NONE;
}
/*----------------------------------------------------------------------------*/
/**
@internal
@ingroup cpl_image
@brief complex multiplication loop, unaligned
@param im1 first operand
@param im2 second operand.
@param Nt number of pixels
@return the #_cpl_error_code_ or CPL_ERROR_NONE
*/
/*----------------------------------------------------------------------------*/
static cpl_error_code
fcompl_mult_sse_unaligned(float complex * a,
const float complex * b,
const size_t Nt)
{
const size_t n = (Nt % 2) == 1 ? Nt - 1 : Nt;
/* no overflow hint for the compiler */
cpl_ensure_code(n < SIZE_MAX-1, CPL_ERROR_ACCESS_OUT_OF_RANGE);
for (size_t i = 0; i < n; i+=2) {
__m128 va = _mm_loadu_ps((const float *)&a[i]); /* x w */
__m128 vb = _mm_loadu_ps((const float *)&b[i]); /* y z */
__m128 res = fcompl_mult_sse_fast(va, vb);
if (CPL_LIKELY(fcompl_fix_nan(res, a, b, i) == CPL_FALSE))
_mm_storeu_ps((float *)&a[i], res);
}
if ((Nt) % 2 == 1)
a[Nt - 1] = a[Nt - 1] * b[Nt - 1];
return CPL_ERROR_NONE;
}
/*----------------------------------------------------------------------------*/
/**
@internal
@ingroup cpl_image
@brief Multiply two float complex images using sse2 or 3
@param im1 first operand.
@param im2 second operand.
@return the #_cpl_error_code_ or CPL_ERROR_NONE
@see cpl_image_multiply()
@note No error checking performed in this static function
*/
/*----------------------------------------------------------------------------*/
static cpl_error_code cpl_image_multiply_fcomplex_sse_(cpl_image * im1,
const cpl_image * im2)
{
const size_t Nt = im1->nx * im1->ny;
float complex * a = im1->pixels;
const float complex * b = im2->pixels;
cpl_error_code err = CPL_ERROR_NONE;
/* FIXME: cases for a xor b unaligned? */
if (((intptr_t)a % 16) == 0 && ((intptr_t)b % 16) == 0)
err = fcompl_mult_sse_aligned(a, b, Nt);
else if (((intptr_t)a % 16) == 8 && ((intptr_t)b % 16) == 8) {
a[0] = a[0] * b[0];
err = fcompl_mult_sse_aligned(a + 1, b + 1, Nt - 1);
}
else
err = fcompl_mult_sse_unaligned(a, b, Nt);
/* Handle bad pixels map */
if (im2->bpm != NULL) {
if (im1->bpm == NULL) {
im1->bpm = cpl_mask_duplicate(im2->bpm);
} else {
cpl_mask_or(im1->bpm, im2->bpm);
}
}
return err;
}
/*----------------------------------------------------------------------------*/
/**
@internal
@ingroup cpl_image
@brief multiply two vectors of two complex double
@param a first operand
@param b second operand
@param i offset
@return result of the multiplication
@see fcompl_mult_scalar2()
*/
/*----------------------------------------------------------------------------*/
static void CPL_ATTR_NOINLINE
dcompl_mult_scalar(double complex * a, const double complex * b, const size_t i)
{
a[i] = a[i] * b[i];
}
/*----------------------------------------------------------------------------*/
/**
@internal
@ingroup cpl_image
@brief check result for NaN and recompute correctly if so
@return true if nan was fixed and stored, false if no fixup required
if false no store was done
@see dcompl_mult_sse_aligned()
*/
/*----------------------------------------------------------------------------*/
static inline cpl_boolean
dcompl_fix_nan(__m128d res, double complex * a,
const double complex * b,
const size_t i)
{
#ifndef __FAST_MATH__
/* check for NaN, redo all in libc if found */
__m128d n = _mm_cmpneq_pd(res, res);
if (CPL_UNLIKELY(_mm_movemask_pd(n) != 0)) {
dcompl_mult_scalar(a, b, i);
return CPL_TRUE;
}
else
#endif
return CPL_FALSE;
}
/*----------------------------------------------------------------------------*/
/**
@internal
@ingroup cpl_image
@brief vertical multiply two vectors of each two complex double
@param va first operand (real, imag, real, imag)
@param vb second operand (real, imag, real, imag)
@return result of the multiplication
does not handle NaN's correctly
*/
/*----------------------------------------------------------------------------*/
static inline __m128d
dcompl_mult_sse_fast(__m128d va, __m128d vb)
{
/* optimized to SSE3 _mm_movedup_pd by gcc */
__m128d rb = _mm_unpacklo_pd(vb, vb); /* y, y */
__m128d ib = _mm_unpackhi_pd(vb, vb); /* w, w*/
__m128d t1 = _mm_mul_pd(rb, va); /* y * x, y * z */
__m128d t2 = _mm_mul_pd(ib, va); /* w * x, w * z*/
__m128d sb = _mm_shuffle_pd(t2, t2, _MM_SHUFFLE2(0, 1)); /* w * z, w * x */
return CPL_MM_ADDSUB_PD(t1, sb); /* x * y - y * w, z*y + x * w */
}
/*----------------------------------------------------------------------------*/
/**
@internal
@ingroup cpl_image
@brief complex multiplication loop, aligned
@param im1 first operand, must be 16 byte aligned
@param im2 second operand, must be 16 byte aligned
@param Nt number of pixels
@return the #_cpl_error_code_ or CPL_ERROR_NONE
*/
/*----------------------------------------------------------------------------*/
static cpl_error_code CPL_ATTR_NOINLINE
dcompl_mult_sse_aligned(double complex * a,
const double complex * b,
const size_t n)
{
/* no overflow hint for the compiler */
cpl_ensure_code(n < SIZE_MAX, CPL_ERROR_ACCESS_OUT_OF_RANGE);
for (size_t i = 0; i < n; i++) {
__m128d va = _mm_load_pd((const double *)&a[i]); /* x w */
__m128d vb = _mm_load_pd((const double *)&b[i]); /* y z */
__m128d res = dcompl_mult_sse_fast(va, vb);
if (CPL_LIKELY(dcompl_fix_nan(res, a, b, i) == CPL_FALSE))
_mm_store_pd((double *)&a[i], res);
}
return CPL_ERROR_NONE;
}
/*----------------------------------------------------------------------------*/
/**
@internal
@ingroup cpl_image
@brief complex multiplication loop, unaligned
@param im1 first operand
@param im2 second operand.
@param Nt number of pixels
@return the #_cpl_error_code_ or CPL_ERROR_NONE
*/
/*----------------------------------------------------------------------------*/
static cpl_error_code
dcompl_mult_sse_unaligned(double complex * a,
const double complex * b,
const size_t n)
{
/* no overflow hint for the compiler */
cpl_ensure_code(n < SIZE_MAX, CPL_ERROR_ACCESS_OUT_OF_RANGE);
for (size_t i = 0; i < n; i++) {
__m128d va = _mm_loadu_pd((const double *)&a[i]); /* x w */
__m128d vb = _mm_loadu_pd((const double *)&b[i]); /* y z */
__m128d res = dcompl_mult_sse_fast(va, vb);
if (CPL_LIKELY(dcompl_fix_nan(res, a, b, i) == CPL_FALSE))
_mm_storeu_pd((double *)&a[i], res);
}
return CPL_ERROR_NONE;
}
/*----------------------------------------------------------------------------*/
/**
@internal
@ingroup cpl_image
@brief Multiply two double complex images using sse2 or 3
@param im1 first operand.
@param im2 second operand.
@return the #_cpl_error_code_ or CPL_ERROR_NONE
@see cpl_image_multiply()
@note No error checking performed in this static function
*/
/*----------------------------------------------------------------------------*/
static cpl_error_code cpl_image_multiply_dcomplex_sse_(cpl_image * im1,
const cpl_image * im2)
{
const size_t Nt = im1->nx * im1->ny;
double complex * a = im1->pixels;
const double complex * b = im2->pixels;
cpl_error_code err = CPL_ERROR_NONE;
/* FIXME: cases for a xor b unaligned? */
if (((intptr_t)a % 16) == 0 && ((intptr_t)b % 16) == 0)
err = dcompl_mult_sse_aligned(a, b, Nt);
else
err = dcompl_mult_sse_unaligned(a, b, Nt);
/* Handle bad pixels map */
if (im2->bpm != NULL) {
if (im1->bpm == NULL) {
im1->bpm = cpl_mask_duplicate(im2->bpm);
} else {
cpl_mask_or(im1->bpm, im2->bpm);
}
}
return err;
}
#endif
|