Tag
Hash :
976e612a
Author :
Date :
2020-09-04T05:49:50
A minor fix to sRGB gamma approximation functions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212
//$ nobt
//$ nocpp
/**
* @file avir.h
*
* @brief The "main" inclusion file with all required classes and functions.
*
* This is the "main" inclusion file for the "AVIR" image resizer. This
* inclusion file contains implementation of the AVIR image resizing algorithm
* in its entirety. Also includes several classes and functions that can be
* useful elsewhere.
*
* AVIR Copyright (c) 2015-2020 Aleksey Vaneev
*
* @mainpage
*
* @section intro_sec Introduction
*
* Description is available at https://github.com/avaneev/avir
*
* AVIR is devoted to women. Your digital photos can look good at any size!
*
* @section license License
*
* AVIR License Agreement
*
* The MIT License (MIT)
*
* Copyright (c) 2015-2020 Aleksey Vaneev
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
* Please credit the author of this library in your documentation in the
* following way: "AVIR image resizing algorithm designed by Aleksey Vaneev"
*
* @version 2.6
*/
#ifndef AVIR_CIMAGERESIZER_INCLUDED
#define AVIR_CIMAGERESIZER_INCLUDED
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
namespace avir {
/**
* The macro defines AVIR version string.
*/
#define AVIR_VERSION "2.6"
/**
* The macro equals to "pi" constant, fills 53-bit floating point mantissa.
* Undefined at the end of file.
*/
#define AVIR_PI 3.1415926535897932
/**
* The macro equals to "pi divided by 2" constant, fills 53-bit floating
* point mantissa. Undefined at the end of file.
*/
#define AVIR_PId2 1.5707963267948966
/**
* Rounding function, based on the (int) typecast. Biased result. Not suitable
* for numbers >= 2^31.
*
* @param d Value to round.
* @return Rounded value. Some bias may be introduced.
*/
template< class T >
inline T round( const T d )
{
return( d < 0.0 ? -(T) (int) ( (T) 0.5 - d ) : (T) (int) ( d + (T) 0.5 ));
}
/**
* Template function "clamps" (clips) the specified value so that it is not
* lesser than "minv", and not greater than "maxv".
*
* @param Value Value to clamp.
* @param minv Minimal allowed value.
* @param maxv Maximal allowed value.
* @return The clamped value.
*/
template< class T >
inline T clamp( const T& Value, const T minv, const T maxv )
{
if( Value < minv )
{
return( minv );
}
else
if( Value > maxv )
{
return( maxv );
}
else
{
return( Value );
}
}
/**
* Power 2.4 approximation function, designed for sRGB gamma correction.
*
* @param x Argument, in the range 0.09 to 1.
* @return Value raised into power 2.4, approximate.
*/
template< class T >
inline T pow24_sRGB( const T x )
{
const double x2 = (double) x * x;
const double x3 = x2 * x;
const double x4 = x2 * x2;
return( (T) ( 0.0985766365536824 + 0.839474952656502 * x2 +
0.363287814061725 * x3 - 0.0125559718896615 /
( 0.12758338921578 + 0.290283465468235 * x ) -
0.231757513261358 * x - 0.0395365717969074 * x4 ));
}
/**
* Power 1/2.4 approximation function, designed for sRGB gamma correction.
*
* @param x Argument, in the range 0.003 to 1.
* @return Value raised into power 1/2.4, approximate.
*/
template< class T >
inline T pow24i_sRGB( const T x )
{
const double sx = sqrt( (double) x );
const double ssx = sqrt( sx );
const double sssx = sqrt( ssx );
return( (T) ( 0.000213364515060263 + 0.0149409239419218 * x +
0.433973412731747 * sx + ssx * ( 0.659628181609715 * sssx -
0.0380957908841466 - 0.0706476137208521 * sx )));
}
/**
* Function approximately linearizes the sRGB gamma value.
*
* @param s sRGB gamma value, in the range 0 to 1.
* @return Linearized sRGB gamma value, approximated.
*/
template< class T >
inline T convertSRGB2Lin( const T s )
{
const T a = (T) 0.055;
if( s <= (T) 0.04045 )
{
return( s / (T) 12.92 );
}
return( pow24_sRGB(( s + a ) / ( (T) 1 + a )));
}
/**
* Function approximately de-linearizes the linear gamma value.
*
* @param s Linear gamma value, in the range 0 to 1.
* @return sRGB gamma value, approximated.
*/
template< class T >
inline T convertLin2SRGB( const T s )
{
const T a = (T) 0.055;
if( s <= (T) 0.0031308 )
{
return( (T) 12.92 * s );
}
return(( (T) 1 + a ) * pow24i_sRGB( s ) - a );
}
/**
* Function converts (via typecast) specified array of type T1 values of
* length l into array of type T2 values. If T1 is the same as T2, copy
* operation is performed. When copying data at overlapping address spaces,
* "op" should be lower than "ip".
*
* @param ip Input buffer.
* @param[out] op Output buffer.
* @param l The number of elements to copy.
* @param ip Input buffer pointer increment.
* @param op Output buffer pointer increment.
*/
template< class T1, class T2 >
inline void copyArray( const T1* ip, T2* op, int l,
const int ipinc = 1, const int opinc = 1 )
{
while( l > 0 )
{
*op = (T2) *ip;
op += opinc;
ip += ipinc;
l--;
}
}
/**
* Function adds values located in array "ip" to array "op".
*
* @param ip Input buffer.
* @param[out] op Output buffer.
* @param l The number of elements to add.
* @param ip Input buffer pointer increment.
* @param op Output buffer pointer increment.
*/
template< class T1, class T2 >
inline void addArray( const T1* ip, T2* op, int l,
const int ipinc = 1, const int opinc = 1 )
{
while( l > 0 )
{
*op += *ip;
op += opinc;
ip += ipinc;
l--;
}
}
/**
* Function that replicates a set of adjacent elements several times in a row.
* This operation is usually used to replicate pixels at the start or end of
* image's scanline.
*
* @param ip Source array.
* @param ipl Source array length (usually 1..4, but can be any number).
* @param[out] op Destination buffer.
* @param l Number of times the source array should be replicated (the
* destination buffer should be able to hold ipl * l number of elements).
* @param opinc Destination buffer position increment after replicating the
* source array. This value should be equal to at least ipl.
*/
template< class T1, class T2 >
inline void replicateArray( const T1* const ip, const int ipl, T2* op, int l,
const int opinc )
{
if( ipl == 1 )
{
while( l > 0 )
{
op[ 0 ] = ip[ 0 ];
op += opinc;
l--;
}
}
else
if( ipl == 4 )
{
while( l > 0 )
{
op[ 0 ] = ip[ 0 ];
op[ 1 ] = ip[ 1 ];
op[ 2 ] = ip[ 2 ];
op[ 3 ] = ip[ 3 ];
op += opinc;
l--;
}
}
else
if( ipl == 3 )
{
while( l > 0 )
{
op[ 0 ] = ip[ 0 ];
op[ 1 ] = ip[ 1 ];
op[ 2 ] = ip[ 2 ];
op += opinc;
l--;
}
}
else
if( ipl == 2 )
{
while( l > 0 )
{
op[ 0 ] = ip[ 0 ];
op[ 1 ] = ip[ 1 ];
op += opinc;
l--;
}
}
else
{
while( l > 0 )
{
int i;
for( i = 0; i < ipl; i++ )
{
op[ i ] = ip[ i ];
}
op += opinc;
l--;
}
}
}
/**
* Function calculates frequency response of the specified FIR filter at the
* specified circular frequency. Phase can be calculated as atan2( im, re ).
* Function uses computationally-efficient oscillators instead of "cos" and
* "sin" functions.
*
* @param flt FIR filter's coefficients.
* @param fltlen Number of coefficients (taps) in the filter.
* @param th Circular frequency [0; pi].
* @param[out] re0 Resulting real part of the complex frequency response.
* @param[out] im0 Resulting imaginary part of the complex frequency response.
* @param fltlat Filter's latency in samples (taps).
*/
template< class T >
inline void calcFIRFilterResponse( const T* flt, int fltlen,
const double th, double& re0, double& im0, const int fltlat = 0 )
{
const double sincr = 2.0 * cos( th );
double cvalue1;
double svalue1;
if( fltlat == 0 )
{
cvalue1 = 1.0;
svalue1 = 0.0;
}
else
{
cvalue1 = cos( -fltlat * th );
svalue1 = sin( -fltlat * th );
}
double cvalue2 = cos( -( fltlat + 1 ) * th );
double svalue2 = sin( -( fltlat + 1 ) * th );
double re = 0.0;
double im = 0.0;
while( fltlen > 0 )
{
re += cvalue1 * flt[ 0 ];
im += svalue1 * flt[ 0 ];
flt++;
fltlen--;
double tmp = cvalue1;
cvalue1 = sincr * cvalue1 - cvalue2;
cvalue2 = tmp;
tmp = svalue1;
svalue1 = sincr * svalue1 - svalue2;
svalue2 = tmp;
}
re0 = re;
im0 = im;
}
/**
* Function normalizes FIR filter so that its frequency response at DC is
* equal to DCGain.
*
* @param[in,out] p Filter coefficients.
* @param l Filter length.
* @param DCGain Filter's gain at DC.
* @param pstep "p" array step.
*/
template< class T >
inline void normalizeFIRFilter( T* const p, const int l, const double DCGain,
const int pstep = 1 )
{
double s = 0.0;
T* pp = p;
int i = l;
while( i > 0 )
{
s += *pp;
pp += pstep;
i--;
}
s = DCGain / s;
pp = p;
i = l;
while( i > 0 )
{
*pp = (T) ( *pp * s );
pp += pstep;
i--;
}
}
/**
* @brief Memory buffer class for element array storage, with capacity
* tracking.
*
* Allows easier handling of memory blocks allocation and automatic
* deallocation for arrays (buffers) consisting of elements of specified
* class. Tracks buffer's capacity in "int" variable; unsuitable for
* allocation of very large memory blocks (with more than 2 billion elements).
*
* This class manages memory space only - it does not perform element class
* construction (initialization) operations. Buffer's required memory address
* alignment specification is supported.
*
* Uses standard library to allocate and deallocate memory.
*
* @tparam T Buffer element's type.
* @tparam capint Buffer capacity's type to use. Use size_t for large buffers.
*/
template< class T, typename capint = int >
class CBuffer
{
public:
CBuffer()
: Data( NULL )
, DataAligned( NULL )
, Capacity( 0 )
, Alignment( 0 )
{
}
/**
* Constructor creates the buffer with the specified capacity.
*
* @param aCapacity Buffer's capacity.
* @param aAlignment Buffer's required memory address alignment. 0 - use
* stdlib's default alignment.
*/
CBuffer( const capint aCapacity, const int aAlignment = 0 )
{
allocinit( aCapacity, aAlignment );
}
CBuffer( const CBuffer& Source )
{
allocinit( Source.Capacity, Source.Alignment );
if( Capacity > 0 )
{
memcpy( DataAligned, Source.DataAligned, Capacity * sizeof( T ));
}
}
~CBuffer()
{
freeData();
}
CBuffer& operator = ( const CBuffer& Source )
{
alloc( Source.Capacity, Source.Alignment );
if( Capacity > 0 )
{
memcpy( DataAligned, Source.DataAligned, Capacity * sizeof( T ));
}
return( *this );
}
/**
* Function allocates memory so that the specified number of elements
* can be stored in *this buffer object.
*
* @param aCapacity Storage for this number of elements to allocate.
* @param aAlignment Buffer's required memory address alignment,
* power-of-2 values only. 0 - use stdlib's default alignment.
*/
void alloc( const capint aCapacity, const int aAlignment = 0 )
{
freeData();
allocinit( aCapacity, aAlignment );
}
/**
* Function deallocates any previously allocated buffer.
*/
void free()
{
freeData();
Data = NULL;
DataAligned = NULL;
Capacity = 0;
Alignment = 0;
}
/**
* @return The capacity of the element buffer.
*/
capint getCapacity() const
{
return( Capacity );
}
/**
* Function "forces" *this buffer to have an arbitary capacity. Calling
* this function invalidates all further operations except deleting *this
* object. This function should not be usually used at all. Function can
* be used to "model" certain buffer capacity without calling a costly
* memory allocation function.
*
* @param NewCapacity A new "forced" capacity.
*/
void forceCapacity( const capint NewCapacity )
{
Capacity = NewCapacity;
}
/**
* Function reallocates *this buffer to a larger size so that it will be
* able to hold the specified number of elements. Downsizing is not
* performed. Alignment is not changed.
*
* @param NewCapacity New (increased) capacity.
* @param DoDataCopy "True" if data in the buffer should be retained.
*/
void increaseCapacity( const capint NewCapacity,
const bool DoDataCopy = true )
{
if( NewCapacity < Capacity )
{
return;
}
if( DoDataCopy )
{
const capint PrevCapacity = Capacity;
T* const PrevData = Data;
T* const PrevDataAligned = DataAligned;
allocinit( NewCapacity, Alignment );
if( PrevCapacity > 0 )
{
memcpy( DataAligned, PrevDataAligned,
PrevCapacity * sizeof( T ));
}
:: free( PrevData );
}
else
{
:: free( Data );
allocinit( NewCapacity, Alignment );
}
}
/**
* Function "truncates" (reduces) capacity of the buffer without
* reallocating it. Alignment is not changed.
*
* @param NewCapacity New required capacity.
*/
void truncateCapacity( const capint NewCapacity )
{
if( NewCapacity >= Capacity )
{
return;
}
Capacity = NewCapacity;
}
/**
* Function increases capacity so that the specified number of
* elements can be stored. This function increases the previous capacity
* value by third the current capacity value until space for the required
* number of elements is available. Alignment is not changed.
*
* @param ReqCapacity Required capacity.
*/
void updateCapacity( const capint ReqCapacity )
{
if( ReqCapacity <= Capacity )
{
return;
}
capint NewCapacity = Capacity;
while( NewCapacity < ReqCapacity )
{
NewCapacity += NewCapacity / 3 + 1;
}
increaseCapacity( NewCapacity );
}
operator T* () const
{
return( DataAligned );
}
private:
T* Data; ///< Element buffer pointer.
///<
T* DataAligned; ///< Memory address-aligned element buffer pointer.
///<
capint Capacity; ///< Element buffer capacity.
///<
int Alignment; ///< Memory address alignment in use. 0 - use stdlib's
///< default alignment.
///<
/**
* Internal element buffer allocation function used during object
* construction.
*
* @param aCapacity Storage for this number of elements to allocate.
* @param aAlignment Buffer's required memory address alignment. 0 - use
* stdlib's default alignment.
*/
void allocinit( const capint aCapacity, const int aAlignment )
{
if( aAlignment == 0 )
{
Data = (T*) :: malloc( aCapacity * sizeof( T ));
DataAligned = Data;
Alignment = 0;
}
else
{
Data = (T*) :: malloc( aCapacity * sizeof( T ) + aAlignment );
DataAligned = alignptr( Data, aAlignment );
Alignment = aAlignment;
}
Capacity = aCapacity;
}
/**
* Function frees a previously allocated Data buffer.
*/
void freeData()
{
:: free( Data );
}
/**
* Function modifies the specified pointer so that it becomes memory
* address-aligned.
*
* @param ptr Pointer to align.
* @param align Alignment in bytes to apply.
* @return Pointer aligned to align bytes. Works with power-of-2
* alignments only. If no alignment is necessary, "align" bytes will be
* added to the pointer value.
*/
template< class Tp >
inline Tp alignptr( const Tp ptr, const uintptr_t align )
{
return( (Tp) ( (uintptr_t) ptr + align -
( (uintptr_t) ptr & ( align - 1 ))) );
}
};
/**
* Function optimizes the length of the symmetric-odd FIR filter by removing
* left- and rightmost elements that are below specific threshold.
*
* Synthetic test shows that filter gets optimized in 2..3% of cases and in
* each such case optimization reduces filter length by 6..8%. Optimization,
* however, may skew the results of algorithm modeling and complexity
* calculation leading to a choice of a less optimal algorithm.
*
* @param[in,out] Flt Buffer that contains filter being optimized.
* @param[in,out] FltLatency Variable that holds the current latency of the
* filter. May be adjusted on function return.
* @param Threshold Threshold level.
*/
template< class T >
inline void optimizeFIRFilter( CBuffer< T >& Flt, int& FltLatency,
T const Threshold = (T) 0.00001 )
{
int i;
// Optimize length.
for( i = 0; i <= FltLatency; i++ )
{
if( fabs( Flt[ i ]) >= Threshold || i == FltLatency )
{
if( i > 0 )
{
const int NewCapacity = Flt.getCapacity() - i * 2;
copyArray( &Flt[ i ], &Flt[ 0 ], NewCapacity );
Flt.truncateCapacity( NewCapacity );
FltLatency -= i;
}
break;
}
}
}
/**
* @brief Array of structured objects.
*
* Implements allocation of a linear array of objects of class T (which are
* initialized), addressable via operator[]. Each object is created via the
* "operator new". New object insertions are quick since implementation uses
* prior space allocation (capacity), thus not requiring frequent memory block
* reallocations.
*
* @tparam T Array element's type.
*/
template< class T >
class CStructArray
{
public:
CStructArray()
: ItemCount( 0 )
{
}
CStructArray( const CStructArray& Source )
: ItemCount( 0 )
, Items( Source.getItemCount() )
{
while( ItemCount < Source.getItemCount() )
{
Items[ ItemCount ] = new T( Source[ ItemCount ]);
ItemCount++;
}
}
~CStructArray()
{
clear();
}
CStructArray& operator = ( const CStructArray& Source )
{
clear();
const int NewCount = Source.ItemCount;
Items.updateCapacity( NewCount );
while( ItemCount < NewCount )
{
Items[ ItemCount ] = new T( Source[ ItemCount ]);
ItemCount++;
}
return( *this );
}
T& operator []( const int Index )
{
return( *Items[ Index ]);
}
const T& operator []( const int Index ) const
{
return( *Items[ Index ]);
}
/**
* Function creates a new object of type T with the default constructor
* and adds this object to the array.
*
* @return Reference to a newly added object.
*/
T& add()
{
if( ItemCount == Items.getCapacity() )
{
Items.increaseCapacity( ItemCount * 3 / 2 + 1 );
}
Items[ ItemCount ] = new T();
ItemCount++;
return( (*this)[ ItemCount - 1 ]);
}
/**
* Function changes number of allocated items. New items are created with
* the default constructor. If NewCount is below the current item count,
* items that are above NewCount range will be destructed.
*
* @param NewCount New requested item count.
*/
void setItemCount( const int NewCount )
{
if( NewCount > ItemCount )
{
Items.increaseCapacity( NewCount );
while( ItemCount < NewCount )
{
Items[ ItemCount ] = new T();
ItemCount++;
}
}
else
{
while( ItemCount > NewCount )
{
ItemCount--;
delete Items[ ItemCount ];
}
}
}
/**
* Function erases all items of *this array.
*/
void clear()
{
while( ItemCount > 0 )
{
ItemCount--;
delete Items[ ItemCount ];
}
}
/**
* @return The number of allocated items.
*/
int getItemCount() const
{
return( ItemCount );
}
private:
int ItemCount; ///< The number of items available in the array.
///<
CBuffer< T* > Items; ///< Element buffer.
///<
};
/**
* @brief Sine signal generator class.
*
* Class implements sine signal generator without biasing, with
* constructor-based initalization only. This generator uses oscillator
* instead of "sin" function.
*/
class CSineGen
{
public:
/**
* Constructor initializes *this sine signal generator.
*
* @param si Sine function increment, in radians.
* @param ph Starting phase, in radians. Add 0.5 * AVIR_PI for cosine
* function.
*/
CSineGen( const double si, const double ph )
: svalue1( sin( ph ))
, svalue2( sin( ph - si ))
, sincr( 2.0 * cos( si ))
{
}
/**
* @return The next value of the sine function, without biasing.
*/
double generate()
{
const double res = svalue1;
svalue1 = sincr * res - svalue2;
svalue2 = res;
return( res );
}
private:
double svalue1; ///< Current sine value.
///<
double svalue2; ///< Previous sine value.
///<
double sincr; ///< Sine value increment.
///<
};
/**
* @brief Peaked Cosine window function generator class.
*
* Class implements Peaked Cosine window function generator. Generates the
* right-handed half of the window function. The Alpha parameter of this
* window function offers the control of the balance between the early and
* later taps of the filter. E.g. at Alpha=1 both early and later taps are
* attenuated, but at Alpha=4 mostly later taps are attenuated. This offers a
* great control over ringing artifacts produced by a low-pass filter in image
* processing, without compromising achieved image sharpness.
*/
class CDSPWindowGenPeakedCosine
{
public:
/**
* Constructor initializes *this window function generator.
*
* @param aAlpha Alpha parameter, affects the peak shape (peak
* augmentation) of the window function. Should be >= 1.0.
* @param aLen2 Half filter's length (non-truncated).
*/
CDSPWindowGenPeakedCosine( const double aAlpha, const double aLen2 )
: Alpha( aAlpha )
, Len2( aLen2 )
, wn( 0 )
, w1( AVIR_PId2 / Len2, AVIR_PI * 0.5 )
{
}
/**
* @return The next Peaked Cosine window function coefficient.
*/
double generate()
{
const double h = pow( wn / Len2, Alpha );
wn++;
return( w1.generate() * ( 1.0 - h ));
}
private:
double Alpha; ///< Alpha parameter, affects the peak shape of window.
///<
double Len2; ///< Half length of the window function.
///<
int wn; ///< Window function integer position. 0 - center of the
///< window function.
///<
CSineGen w1; ///< Sine-wave generator.
///<
};
/**
* @brief FIR filter-based equalizer generator.
*
* Class implements an object used to generate symmetric-odd FIR filters with
* the specified frequency response (aka paragraphic equalizer). The
* calculated filter is windowed by the Peaked Cosine window function.
*
* In image processing, due to short length of filters being used (6-8 taps)
* the resulting frequency response of the filter is approximate and may be
* mathematically imperfect, but still adequate to the visual requirements.
*
* On a side note, this equalizer generator can be successfully used for audio
* signal equalization as well: for example, it is used in almost the same
* form in Voxengo Marvel GEQ equalizer plug-in.
*
* Filter generation is based on decomposition of frequency range into
* spectral bands, with each band represented by linear and ramp "kernels".
* When the filter is built, these kernels are combined together with
* different weights that approximate the required frequency response.
*/
class CDSPFIREQ
{
public:
/**
* Function initializes *this object with the required parameters. The
* gain of frequencies beyond the MinFreq..MaxFreq range are controlled by
* the first and the last band's gain.
*
* @param SampleRate Processing sample rate (use 2 for image processing).
* @param aFilterLength Required filter length in samples (taps). The
* actual filter length is truncated to an integer value.
* @param aBandCount Number of band crossover points required to control,
* including bands at MinFreq and MaxFreq.
* @param MinFreq Minimal frequency that should be controlled.
* @param MaxFreq Maximal frequency that should be controlled.
* @param IsLogBands "True" if the bands should be spaced logarithmically.
* @param WFAlpha Peaked Cosine window function's Alpha parameter.
*/
void init( const double SampleRate, const double aFilterLength,
const int aBandCount, const double MinFreq, const double MaxFreq,
const bool IsLogBands, const double WFAlpha )
{
FilterLength = aFilterLength;
BandCount = aBandCount;
CenterFreqs.alloc( BandCount );
z = (int) ceil( FilterLength * 0.5 );
zi = z + ( z & 1 );
z2 = z * 2;
CBuffer< double > oscbuf( z2 );
initOscBuf( oscbuf );
CBuffer< double > winbuf( z );
initWinBuf( winbuf, WFAlpha );
UseFirstVirtBand = ( MinFreq > 0.0 );
const int k = zi * ( BandCount + ( UseFirstVirtBand ? 1 : 0 ));
Kernels1.alloc( k );
Kernels2.alloc( k );
double m; // Frequency step multiplier.
double mo; // Frequency step offset (addition).
if( IsLogBands )
{
m = exp( log( MaxFreq / MinFreq ) / ( BandCount - 1 ));
mo = 0.0;
}
else
{
m = 1.0;
mo = ( MaxFreq - MinFreq ) / ( BandCount - 1 );
}
double f = MinFreq;
double x1 = 0.0;
double x2;
int si;
if( UseFirstVirtBand )
{
si = 0;
}
else
{
si = 1;
CenterFreqs[ 0 ] = 0.0;
f = f * m + mo;
}
double* kernbuf1 = &Kernels1[ 0 ];
double* kernbuf2 = &Kernels2[ 0 ];
int i;
for( i = si; i < BandCount; i++ )
{
x2 = f * 2.0 / SampleRate;
CenterFreqs[ i ] = x2;
fillBandKernel( x1, x2, kernbuf1, kernbuf2, oscbuf, winbuf );
kernbuf1 += zi;
kernbuf2 += zi;
x1 = x2;
f = f * m + mo;
}
if( x1 < 1.0 )
{
UseLastVirtBand = true;
fillBandKernel( x1, 1.0, kernbuf1, kernbuf2, oscbuf, winbuf );
}
else
{
UseLastVirtBand = false;
}
}
/**
* @return Filter's length, in samples (taps).
*/
int getFilterLength() const
{
return( z2 - 1 );
}
/**
* @return Filter's latency (group delay), in samples (taps).
*/
int getFilterLatency() const
{
return( z - 1 );
}
/**
* Function creates symmetric-odd FIR filter with the specified gain
* levels at band crossover points.
*
* @param BandGains Array of linear gain levels, count=BandCount specified
* in the init() function.
* @param[out] Filter Output filter buffer, length = getFilterLength().
*/
void buildFilter( const double* const BandGains, double* const Filter )
{
const double* kernbuf1 = &Kernels1[ 0 ];
const double* kernbuf2 = &Kernels2[ 0 ];
double x1 = 0.0;
double y1 = BandGains[ 0 ];
double x2;
double y2;
int i;
int si;
if( UseFirstVirtBand )
{
si = 1;
x2 = CenterFreqs[ 0 ];
y2 = y1;
}
else
{
si = 2;
x2 = CenterFreqs[ 1 ];
y2 = BandGains[ 1 ];
}
copyBandKernel( Filter, kernbuf1, kernbuf2, y1 - y2,
x1 * y2 - x2 * y1 );
kernbuf1 += zi;
kernbuf2 += zi;
x1 = x2;
y1 = y2;
for( i = si; i < BandCount; i++ )
{
x2 = CenterFreqs[ i ];
y2 = BandGains[ i ];
addBandKernel( Filter, kernbuf1, kernbuf2, y1 - y2,
x1 * y2 - x2 * y1 );
kernbuf1 += zi;
kernbuf2 += zi;
x1 = x2;
y1 = y2;
}
if( UseLastVirtBand )
{
addBandKernel( Filter, kernbuf1, kernbuf2, y1 - y2,
x1 * y2 - y1 );
}
for( i = 0; i < z - 1; i++ )
{
Filter[ z + i ] = Filter[ z - 2 - i ];
}
}
/**
* Function calculates filter's length (in samples) and latency depending
* on the required non-truncated filter length.
*
* @param aFilterLength Required filter length in samples (non-truncated).
* @param[out] Latency Resulting latency (group delay) of the filter,
* in samples (taps).
* @return Filter length in samples (taps).
*/
static int calcFilterLength( const double aFilterLength, int& Latency )
{
const int l = (int) ceil( aFilterLength * 0.5 );
Latency = l - 1;
return( l * 2 - 1 );
}
private:
double FilterLength; ///< Length of filter.
///<
int z; ///< Equals (int) ceil( FilterLength * 0.5 ).
///<
int zi; ///< Equals "z" if z is even, or z + 1 if z is odd. Used as a
///< Kernels1 and Kernels2 size multiplier and kernel buffer increment
///< to make sure each kernel buffer is 16-byte aligned.
///<
int z2; ///< Equals z * 2.
///<
int BandCount; ///< Number of controllable bands.
///<
CBuffer< double > CenterFreqs; ///< Center frequencies for all bands,
///< normalized to 0.0-1.0 range.
///<
CBuffer< double > Kernels1; ///< Half-length kernel buffers for each
///< spectral band (linear part).
///<
CBuffer< double > Kernels2; ///< Half-length kernel buffers for each
///< spectral band (ramp part).
///<
bool UseFirstVirtBand; ///< "True" if the first virtual band
///< (between 0.0 and MinFreq) should be used. The first virtual band
///< won't be used if MinFreq equals 0.0.
///<
bool UseLastVirtBand; ///< "True" if the last virtual band (between
///< MaxFreq and SampleRate * 0.5) should be used. The last virtual
///< band won't be used if MaxFreq * 2.0 equals SampleRate.
///<
/**
* Function initializes the "oscbuf" used in the fillBandKernel()
* function.
*
* @param oscbuf Oscillator buffer, length = z * 2.
*/
void initOscBuf( double* oscbuf ) const
{
int i = z;
while( i > 0 )
{
oscbuf[ 0 ] = 0.0;
oscbuf[ 1 ] = 1.0;
oscbuf += 2;
i--;
}
}
/**
* Function initializes window function buffer. This function generates
* Peaked Cosine window function.
*
* @param winbuf Windowing buffer.
* @param Alpha Peaked Cosine alpha parameter.
*/
void initWinBuf( double* winbuf, const double Alpha ) const
{
CDSPWindowGenPeakedCosine wf( Alpha, FilterLength * 0.5 );
int i;
for( i = 1; i <= z; i++ )
{
winbuf[ z - i ] = wf.generate();
}
}
/**
* Function fills first half of symmetric-odd FIR kernel for the band.
* This function should be called successively for adjacent bands.
* Previous band's x2 should be equal to current band's x1. A band kernel
* consists of 2 elements: linear kernel and ramp kernel.
*
* @param x1 Band's left corner frequency (0..1).
* @param x2 Band's right corner frequency (0..1).
* @param kernbuf1 Band kernel buffer 1 (linear part), length = z.
* @param kernbuf2 Band kernel buffer 2 (ramp part), length = z.
* @param oscbuf Oscillation buffer. Before the first call of the
* fillBandKernel() should be initialized with the call of the
* initOscBuf() function.
* @param winbuf Buffer that contains windowing function.
*/
void fillBandKernel( const double x1, const double x2, double* kernbuf1,
double* kernbuf2, double* oscbuf, const double* const winbuf )
{
const double s2_incr = AVIR_PI * x2;
const double s2_coeff = 2.0 * cos( s2_incr );
double s2_value1 = sin( s2_incr * ( -z + 1 ));
double c2_value1 = sin( s2_incr * ( -z + 1 ) + AVIR_PI * 0.5 );
oscbuf[ 0 ] = sin( s2_incr * -z );
oscbuf[ 1 ] = sin( s2_incr * -z + AVIR_PI * 0.5 );
int ks;
for( ks = 1; ks < z; ks++ )
{
const int ks2 = ks * 2;
const double s1_value1 = oscbuf[ ks2 ];
const double c1_value1 = oscbuf[ ks2 + 1 ];
oscbuf[ ks2 ] = s2_value1;
oscbuf[ ks2 + 1 ] = c2_value1;
const double x = AVIR_PI * ( ks - z );
const double v0 = winbuf[ ks - 1 ] / (( x1 - x2 ) * x );
kernbuf1[ ks - 1 ] = ( x2 * s2_value1 - x1 * s1_value1 +
( c2_value1 - c1_value1 ) / x ) * v0;
kernbuf2[ ks - 1 ] = ( s2_value1 - s1_value1 ) * v0;
s2_value1 = s2_coeff * s2_value1 - oscbuf[ ks2 - 2 ];
c2_value1 = s2_coeff * c2_value1 - oscbuf[ ks2 - 1 ];
}
kernbuf1[ z - 1 ] = ( x2 * x2 - x1 * x1 ) / ( x1 - x2 ) * 0.5;
kernbuf2[ z - 1 ] = -1.0;
}
/**
* Function copies band kernel's elements to the output buffer.
*
* @param outbuf Output buffer.
* @param kernbuf1 Kernel buffer 1 (linear part).
* @param kernbuf2 Kernel buffer 2 (ramp part).
* @param c Multiplier for linear kernel element.
* @param d Multiplier for ramp kernel element.
*/
void copyBandKernel( double* outbuf, const double* const kernbuf1,
const double* const kernbuf2, const double c, const double d ) const
{
int ks;
for( ks = 0; ks < z; ks++ )
{
outbuf[ ks ] = c * kernbuf1[ ks ] + d * kernbuf2[ ks ];
}
}
/**
* Function adds band kernel's elements to the output buffer.
*
* @param outbuf Output buffer.
* @param kernbuf1 Kernel buffer 1 (linear part).
* @param kernbuf2 Kernel buffer 2 (ramp part).
* @param c Multiplier for linear kernel element.
* @param d Multiplier for ramp kernel element.
*/
void addBandKernel( double* outbuf, const double* const kernbuf1,
const double* const kernbuf2, const double c, const double d ) const
{
int ks;
for( ks = 0; ks < z; ks++ )
{
outbuf[ ks ] += c * kernbuf1[ ks ] + d * kernbuf2[ ks ];
}
}
};
/**
* @brief Low-pass filter windowed by Peaked Cosine window function.
*
* This class implements calculation of linear-phase symmetric-odd FIR
* low-pass filter windowed by the Peaked Cosine window function, for image
* processing applications.
*/
class CDSPPeakedCosineLPF
{
public:
int fl2; ///< Half filter's length, excluding the peak value. This value
///< can be also used as filter's latency in samples (taps).
///<
int FilterLen; ///< Filter's length in samples (taps).
///<
/**
* Constructor initalizes *this object.
*
* @param aLen2 Half-length (non-truncated) of low-pass filter, in samples
* (taps).
* @param aFreq2 Low-pass filter's corner frequency [0; pi].
* @param aAlpha Peaked Cosine window function Alpha parameter.
*/
CDSPPeakedCosineLPF( const double aLen2, const double aFreq2,
const double aAlpha )
: fl2( (int) ceil( aLen2 ) - 1 )
, FilterLen( fl2 + fl2 + 1 )
, Len2( aLen2 )
, Freq2( aFreq2 )
, Alpha( aAlpha )
{
}
/**
* Function generates a linear-phase low-pass filter windowed by Peaked
* Cosine window function.
*
* @param[out] op Output buffer, length = FilterLen (fl2 * 2 + 1).
* @param DCGain Required gain at DC. The resulting filter will be
* normalized to achieve this DC gain.
*/
template< class T >
void generateLPF( T* op, const double DCGain )
{
CDSPWindowGenPeakedCosine wf( Alpha, Len2 );
CSineGen f2( Freq2, 0.0 );
op += fl2;
T* op2 = op;
f2.generate();
int t = 1;
*op = (T) ( Freq2 * wf.generate() / AVIR_PI );
double s = *op;
while( t <= fl2 )
{
const double v = f2.generate() * wf.generate() / t / AVIR_PI;
op++;
op2--;
*op = (T) v;
*op2 = (T) v;
s += *op + *op2;
t++;
}
t = FilterLen;
s = DCGain / s;
while( t > 0 )
{
*op2 = (T) ( *op2 * s );
op2++;
t--;
}
}
private:
double Len2; ///< Half-length (non-truncated) of low-pass filter, in
///< samples (taps).
///<
double Freq2; ///< Low-pass filter's corner frequency.
///<
double Alpha; ///< Peaked Cosine window function Alpha parameter.
///<
};
/**
* @brief Buffer class for parametrized low-pass filter.
*
* This class extends the CBuffer< double > class by adding several variables
* that define a symmetric-odd FIR low-pass filter windowed by Peaked Cosine
* window function. This class can be used to compare filters without
* comparing their buffer contents.
*/
class CFltBuffer : public CBuffer< double >
{
public:
double Len2; ///< Half-length (non-truncated) of low-pass filters, in
///< samples (taps).
///<
double Freq; ///< Low-pass filter's corner frequency.
///<
double Alpha; ///< Peaked Cosine window function Alpha parameter.
///<
double DCGain; ///< DC gain applied to the filter.
///<
CFltBuffer()
: CBuffer< double >()
, Len2( 0.0 )
, Freq( 0.0 )
, Alpha( 0.0 )
, DCGain( 0.0 )
{
}
/**
* @param b2 Filter buffer to compare *this object to.
* @return Operator returns "true" if both filters have same parameters.
*/
bool operator == ( const CFltBuffer& b2 ) const
{
return( Len2 == b2.Len2 && Freq == b2.Freq && Alpha == b2.Alpha &&
DCGain == b2.DCGain );
}
};
/**
* @brief Sinc function-based fractional delay filter bank.
*
* Class implements storage and initialization of a bank of sinc
* function-based fractional delay filters, expressed as 1st order polynomial
* interpolation coefficients. The filters are produced from a single "long"
* windowed low-pass filter. Also supports 0th-order ("nearest neighbor")
* interpolation.
*
* This class also supports multiplication of each fractional delay filter by
* an external filter (usually a low-pass filter).
*
* @tparam fptype Specifies storage type of the filter coefficients bank. The
* filters are initially calculated using the "double" precision.
*/
template< class fptype >
class CDSPFracFilterBankLin
{
public:
CDSPFracFilterBankLin()
: Order( -1 )
{
}
/**
* Copy constructor copies a limited set of parameters of the source
* filter bank. The actual filters are not copied. Such copying is used
* during filtering steps "modeling" stage. A further init() function
* call is required.
*
* @param s Source filter bank.
*/
void copyInitParams( const CDSPFracFilterBankLin& s )
{
WFLen2 = s.WFLen2;
WFFreq = s.WFFreq;
WFAlpha = s.WFAlpha;
FracCount = s.FracCount;
Order = s.Order;
Alignment = s.Alignment;
SrcFilterLen = s.SrcFilterLen;
FilterLen = s.FilterLen;
FilterSize = s.FilterSize;
IsSrcTableBuilt = false;
ExtFilter = s.ExtFilter;
TableFillFlags.alloc( s.TableFillFlags.getCapacity() );
int i;
// Copy table fill flags, but shifted so that further initialization
// is still possible (such feature should not be used, though).
for( i = 0; i < TableFillFlags.getCapacity(); i++ )
{
TableFillFlags[ i ] = (uint8_t) ( s.TableFillFlags[ i ] << 2 );
}
}
/**
* Operator compares *this filter bank and another filter bank and returns
* "true" if their parameters are equal. Alignment is not taken into
* account.
*
* @param s Filter bank to compare to.
* @return "True" if compared banks have equal parameters.
*/
bool operator == ( const CDSPFracFilterBankLin& s ) const
{
return( Order == s.Order && WFLen2 == s.WFLen2 &&
WFFreq == s.WFFreq && WFAlpha == s.WFAlpha &&
FracCount == s.FracCount && ExtFilter == s.ExtFilter );
}
/**
* Function initializes (builds) the filter bank based on the supplied
* parameters. If the supplied parameters are equal to previously defined
* parameters, function does nothing (alignment is assumed to be never
* changing between the init() function calls).
*
* @param ReqFracCount Required number of fractional delays in the filter
* bank. The minimal value is 2.
* @param ReqOrder Required order of the interpolation polynomial
* (0 or 1).
* @param BaseLen Low-pass filter's base length, in samples (taps).
* Affects the actual length of the filter and its overall steepness.
* @param Cutoff Low-pass filter's normalized cutoff frequency [0; 1].
* @param aWFAlpha Peaked Cosine window function's Alpha parameter.
* @param aExtFilter External filter to apply to each fractional delay
* filter.
* @param aAlignment Memory alignment of the filter bank, power-of-2
* value. 0 - use default stdlib alignment.
* @param FltLenAlign Filter's length alignment, power-of-2 value.
*/
void init( const int ReqFracCount, const int ReqOrder,
const double BaseLen, const double Cutoff, const double aWFAlpha,
const CFltBuffer& aExtFilter, const int aAlignment = 0,
const int FltLenAlign = 1 )
{
double NewWFLen2 = 0.5 * BaseLen * ReqFracCount;
double NewWFFreq = AVIR_PI * Cutoff / ReqFracCount;
double NewWFAlpha = aWFAlpha;
if( ReqOrder == Order && NewWFLen2 == WFLen2 && NewWFFreq == WFFreq &&
NewWFAlpha == WFAlpha && ReqFracCount == FracCount &&
aExtFilter == ExtFilter )
{
IsInitRequired = false;
return;
}
WFLen2 = NewWFLen2;
WFFreq = NewWFFreq;
WFAlpha = NewWFAlpha;
FracCount = ReqFracCount;
Order = ReqOrder;
Alignment = aAlignment;
ExtFilter = aExtFilter;
CDSPPeakedCosineLPF p( WFLen2, WFFreq, WFAlpha );
SrcFilterLen = ( p.fl2 / ReqFracCount + 1 ) * 2;
const int ElementSize = ReqOrder + 1;
FilterLen = SrcFilterLen;
if( ExtFilter.getCapacity() > 0 )
{
FilterLen += ExtFilter.getCapacity() - 1;
}
FilterLen = ( FilterLen + FltLenAlign - 1 ) & ~( FltLenAlign - 1 );
FilterSize = FilterLen * ElementSize;
IsSrcTableBuilt = false;
IsInitRequired = true;
}
/**
* @return The length of each fractional delay filter, in samples (taps).
* Always an even value.
*/
int getFilterLen() const
{
return( FilterLen );
}
/**
* @return The number of fractional filters in use by *this bank.
*/
int getFracCount() const
{
return( FracCount );
}
/**
* @return The order of the interpolation polynomial.
*/
int getOrder() const
{
return( Order );
}
/**
* Function returns the pointer to the specified interpolation table
* filter.
*
* @param i Filter (fractional delay) index, in the range 0 to
* ReqFracCount - 1, inclusive.
* @return Pointer to filter. Higher order polynomial coefficients are
* stored after after previous order coefficients, separated by FilterLen
* elements.
*/
const fptype* getFilter( const int i )
{
if( !IsSrcTableBuilt )
{
buildSrcTable();
}
fptype* const Res = &Table[ i * FilterSize ];
if(( TableFillFlags[ i ] & 2 ) == 0 )
{
createFilter( i );
TableFillFlags[ i ] |= 2;
if( Order > 0 )
{
createFilter( i + 1 );
const fptype* const Res2 = Res + FilterSize;
fptype* const op = Res + FilterLen;
int j;
// Create higher-order interpolation coefficients (linear
// interpolation).
for( j = 0; j < FilterLen; j++ )
{
op[ j ] = Res2[ j ] - Res[ j ];
}
}
}
return( Res );
}
/**
* Function makes sure all fractional delay filters were created.
*/
void createAllFilters()
{
int i;
for( i = 0; i < FracCount; i++ )
{
getFilter( i );
}
}
/**
* Function returns an approximate initialization complexity, expressed in
* the number of multiply-add operations. This includes fractional delay
* filters calculation and multiplication by an external filter. This
* function can only be called after the init() function.
*
* @param FracUseMap Fractional delays use map, each element corresponds
* to a single fractional delay, will be compared to the internal table
* fill flags. This map should include 0 and 1 values only.
* @return The complexity of the initialization, expressed in the number
* of multiply-add operations.
*/
int calcInitComplexity( const CBuffer< uint8_t >& FracUseMap ) const
{
const int FltInitCost = 65; // Cost to initialize a single sample
// of the fractional delay filter.
const int FltUseCost = FilterLen * Order +
SrcFilterLen * ExtFilter.getCapacity(); // Cost to use a single
// fractional delay filter.
const int ucb[ 2 ] = { 0, FltUseCost };
int ic;
int i;
if( IsInitRequired )
{
ic = FracCount * SrcFilterLen * FltInitCost;
for( i = 0; i < FracCount; i++ )
{
ic += ucb[ FracUseMap[ i ]];
}
}
else
{
ic = 0;
for( i = 0; i < FracCount; i++ )
{
if( FracUseMap[ i ] != 0 )
{
ic += ucb[ TableFillFlags[ i ] == 0 ? 1 : 0 ];
}
}
}
return( ic );
}
private:
static const int InterpPoints = 2; ///< The maximal number of points the
///< interpolation is based on.
///<
double WFLen2; ///< Window function's Len2 parameter.
///<
double WFFreq; ///< Window function's Freq parameter.
///<
double WFAlpha; ///< Window function's Alpha parameter.
///<
int FracCount; ///< The required number of fractional delay filters.
///<
int Order; ///< The order of the interpolation polynomial.
///<
int Alignment; ///< The required filter table alignment.
///<
int SrcFilterLen; ///< Length of the "source" filters. This is always an
///< even value.
///<
int FilterLen; ///< Specifies the number of samples (taps) each fractional
///< delay filter has. This is always an even value, adjusted by the
///< FltLenAlign.
///<
int FilterSize; ///< The size of a single filter element, equals
///< FilterLen * ElementSize.
///<
bool IsInitRequired; ///< "True" if SrcTable filter table initialization
///< is required. This value is available only after the call to the
///< init() function.
///<
CBuffer< fptype > Table; ///< Interpolation table, size equals to
///< ReqFracCount * FilterLen * ElementSize.
///<
CBuffer< uint8_t > TableFillFlags; ///< Contains ReqFracCount + 1
///< elements. Bit 0 of every element is 1 if Table already contains
///< the filter from SrcTable filtered by ExtFilter. Bit 1 of every
///< element means higher order coefficients were filled for the
///< filter.
///<
CFltBuffer ExtFilter; ///< External filter that should be applied to every
///< fractional delay filter. Can be empty. Half of this filter's
///< capacity is used as latency (group delay) value of the filter.
///<
CBuffer< double > SrcTable; ///< Source table of delay filters, contains
///< ReqFracCount + 1 elements. This table is used to fill the Table
///< with the actual filters, filtered by an external filter.
///<
bool IsSrcTableBuilt; ///< "True" if the SrcTable was built already. This
///< variable is set to "false" in the init() function.
///<
/**
* Function builds source table used in the createFilter() function.
*/
void buildSrcTable()
{
IsSrcTableBuilt = true;
IsInitRequired = false;
CDSPPeakedCosineLPF p( WFLen2, WFFreq, WFAlpha );
const int BufLen = SrcFilterLen * FracCount + InterpPoints - 1;
const int BufOffs = InterpPoints / 2 - 1;
const int BufCenter = SrcFilterLen * FracCount / 2 + BufOffs;
CBuffer< double > Buf( BufLen );
memset( Buf, 0, ( BufCenter - p.fl2 ) * sizeof( double ));
int i = BufLen - BufCenter - p.fl2 - 1;
memset( &Buf[ BufLen - i ], 0, i * sizeof( double ));
p.generateLPF( &Buf[ BufCenter - p.fl2 ], FracCount );
SrcTable.alloc(( FracCount + 1 ) * SrcFilterLen );
TableFillFlags.alloc( FracCount + 1 );
int j;
double* op0 = SrcTable;
for( i = FracCount; i >= 0; i-- )
{
TableFillFlags[ i ] = 0;
double* p = Buf + BufOffs + i;
for( j = 0; j < SrcFilterLen; j++ )
{
op0[ 0 ] = p[ 0 ];
op0++;
p += FracCount;
}
}
Table.alloc(( FracCount + 1 ) * FilterSize, Alignment );
}
/**
* Function creates the specified filter in the Table by copying it from
* the SrcTable and filtering by ExtFilter. Function does nothing if
* filter was already created.
*
* @param k Filter index to create, in the range 0 to FracCount,
* inclusive.
*/
void createFilter( const int k )
{
if( TableFillFlags[ k ] != 0 )
{
return;
}
TableFillFlags[ k ] |= 1;
const int ExtFilterLatency = ExtFilter.getCapacity() / 2;
const int ResLatency = ExtFilterLatency + SrcFilterLen / 2;
int ResLen = SrcFilterLen;
if( ExtFilter.getCapacity() > 0 )
{
ResLen += ExtFilter.getCapacity() - 1;
}
const int ResOffs = FilterLen / 2 - ResLatency;
fptype* op = &Table[ k * FilterSize ];
int i;
for( i = 0; i < ResOffs; i++ )
{
op[ i ] = 0.0;
}
for( i = ResOffs + ResLen; i < FilterLen; i++ )
{
op[ i ] = 0.0;
}
op += ResOffs;
const double* const srcflt = &SrcTable[ k * SrcFilterLen ];
if( ExtFilter.getCapacity() == 0 )
{
for( i = 0; i < ResLen; i++ )
{
op[ i ] = (fptype) srcflt[ i ];
}
return;
}
// Perform convolution of extflt and srcflt.
const double* const extflt = &ExtFilter[ 0 ];
int j;
for( j = 0; j < ResLen; j++ )
{
int k = 0;
int l = j - ExtFilter.getCapacity() + 1;
int r = l + ExtFilter.getCapacity();
if( l < 0 )
{
k -= l;
l = 0;
}
if( r > SrcFilterLen )
{
r = SrcFilterLen;
}
const double* const extfltb = extflt + k;
const double* const srcfltb = srcflt + l;
double s = 0.0;
l = r - l;
for( i = 0; i < l; i++ )
{
s += extfltb[ i ] * srcfltb[ i ];
}
op[ j ] = (fptype) s;
}
}
};
/**
* @brief Thread pool for multi-threaded image resizing operation.
*
* This base class is used to organize a multi-threaded image resizing
* operation. The thread pool should consist of threads that initially wait
* for a signal. Upon receiving a signal (via the startAllWorkloads()
* function) each previously added thread should execute its workload's
* process() function once, and return to the wait signal state again. The
* thread pool should be also able to efficiently wait for all workloads to
* finish via the waitAllWorkloadsToFinish() function.
*
* The image resizing algorithm makes calls to functions of this class.
*/
class CImageResizerThreadPool
{
public:
CImageResizerThreadPool()
{
}
virtual ~CImageResizerThreadPool()
{
}
/**
* @brief Thread pool's workload object class.
*
* This class should be used as a base class for objects that perform the
* actual work spread over several threads.
*/
class CWorkload
{
public:
virtual ~CWorkload()
{
}
/**
* Function that gets called from the thread when thread pool's
* startAllWorkloads() function is called.
*/
virtual void process() = 0;
};
/**
* @return The suggested number of workloads (and their associated
* threads) to add. The minimal value this function can return is 1. The
* usual value may depend on the number of physical and virtual cores
* present in the system, and on other considerations.
*/
virtual int getSuggestedWorkloadCount() const
{
return( 1 );
}
/**
* Function adds a new workload (and possibly thread) to the thread pool.
* The caller decides how many parallel workloads (and threads) it
* requires, but this number will not exceed the value returned by the
* getSuggestedWorkloadCount() function. It is implementation-specific how
* many workloads to associate with a single thread. But for efficiency
* reasons each workload should be associated with its own thread.
*
* Note that the same set of workload objects will be processed each time
* the startAllWorkloads() function is called. This means that workload
* objects are added only once. The caller changes the state of the
* workload objects and then calls the startAllWorkloads() function to
* process them.
*
* @param Workload Workload object whose process() function will be called
* from within the thread when the startAllWorkloads() function is called.
*/
virtual void addWorkload( CWorkload* const Workload )
{
}
/**
* Function starts all workloads associated with threads previously added
* via the addWorkload() function. It is assumed that this function
* performs the necessary "memory barrier" (or "cache sync") kind of
* operation so that all threads catch up the prior changes made to the
* workload objects during their wait state.
*/
virtual void startAllWorkloads()
{
}
/**
* Function waits for all workloads to finish.
*/
virtual void waitAllWorkloadsToFinish()
{
}
/**
* Function removes all workloads previously added via the addWorkload()
* function. This function gets called only after the
* waitAllWorkloadsToFinish() function call.
*/
virtual void removeAllWorkloads()
{
}
};
/**
* @brief Resizing algorithm parameters structure.
*
* This structure holds all selectable parameters used by the resizing
* algorithm at various stages, for both downsizing and upsizing. There are no
* other parameters exist that can optimize the performance of the resizing
* algorithm. Filter length parameters can take fractional values.
*
* Beside quality, these parameters (except Alpha parameters) directly affect
* the computative cost of the resizing algorithm. It is possible to trade
* the visual quality for computative cost.
*
* Anti-alias filtering during downsizing can be defined as a considerable
* reduction of contrast of smallest features of an image. Unfortunately, such
* de-contrasting partially affects features of all sizes thus producing a
* non-linearity of frequency response. All pre-defined parameter sets are
* described by 3 values separated by slashes. The first value is the
* de-contrasting factor of small features (which are being removed) while
* the second value is the de-contrasting factor of large features (which
* should remain intact), with value of 1 equating to "no contrast change".
* The third value is the optimization score (see below), with value of 0
* equating to the "perfect" linearity of frequency response.
*
* The pre-defined parameter sets offered by this library were auto-optimized
* for the given LPFltBaseLen, IntFltLen and CorrFltAlpha values. The
* optimization goal was to minimize the score: the sum of squares of the
* difference between original and processed images (which was not actually
* resized, k=1). The original image was a 0.5 megapixel uniformly-distributed
* white-noise image with pixel intensities in the 0-1 range. Such goal
* converges very well and produces filtering system with the flattest
* frequency response possible for the given constraints. With this goal,
* increasing the LPFltBaseLen value reduces the general amount of aliasing
* artifacts.
*/
struct CImageResizerParams
{
double CorrFltAlpha; ///< Alpha parameter of the Peaked Cosine window
///< function used on the correction filter. The "usable" values are
///< in the narrow range 1.0 to 1.5.
///<
double CorrFltLen; ///< Correction filter's length in samples (taps). The
///< "usable" range is narrow, 5.5 to 8, as to minimize the
///< "overcorrection" which is mathematically precise, but visually
///< unacceptable.
///<
double IntFltAlpha; ///< Alpha parameter of the Peaked Cosine window
///< function used on the interpolation low-pass filter. The "usable"
///< values are in the range 1.5 to 2.5.
///<
double IntFltCutoff; ///< Interpolation low-pass filter's cutoff frequency
///< (normalized, [0; 1]). The "usable" range is 0.6 to 0.8.
///<
double IntFltLen; ///< Interpolation low-pass filter's length in samples
///< (taps). The length value should be at least 18 or otherwise a
///< "dark grid" artifact will be introduced if a further sharpening
///< is applied. IntFltLen together with other IntFlt parameters
///< should be tuned in a way that produces the flattest frequency
///< response in 0-0.5 normalized frequency range (this range is due
///< to 2X upsampling).
///<
double LPFltAlpha; ///< Alpha parameter of the Peaked Cosine window
///< function used on the low-pass filter. The "usable" values are
///< in the range 1.5 to 6.5.
///<
double LPFltBaseLen; ///< Base length of the low-pass (aka anti-aliasing
///< or reconstruction) filter, in samples (taps), further adjusted by
///< the actual cutoff frequency, upsampling and downsampling factors.
///< The "usable" range is between 6 and 9.
///<
double LPFltCutoffMult; ///< Low-pass filter's cutoff frequency
///< multiplier. This value can be both below and above 1.0 as
///< low-pass filters are inserted on downsampling and upsampling
///< steps and always have corner frequency equal to or below 0.5pi.
///< This multiplier shifts low-pass filter's corner frequency towards
///< lower (if below 1.0) or higher (if above 1.0) frequencies. This
///< multiplier can be way below 1.0 since any additional
///< high-frequency damping will be partially corrected by the
///< correction filter. The "usable" range is 0.3 to 1.0.
///<
CImageResizerParams()
: HBFltAlpha( 1.75395 )
, HBFltCutoff( 0.40356 )
, HBFltLen( 22.00000 )
{
}
double HBFltAlpha; ///< Half-band filter's Alpha. Assigned internally.
///<
double HBFltCutoff; ///< Half-band filter's cutoff point [0; 1]. Assigned
///< internally.
///<
double HBFltLen; ///< Length of the half-band low-pass filter. Assigned
///< internally. Internally used to perform 2X or higher downsampling.
///< These filter parameters should be treated as "technical" and do
///< not require adjustment as they were tuned to suit all
///< combinations of other parameters. This half-band filter provides
///< a wide transition band (for minimal ringing artifacts) and a high
///< stop-band attenuation (for minimal aliasing).
///<
};
/**
* @brief The default set of resizing algorithm parameters
* (10.01/1.029/0.019169).
*
* This is the default set of resizing parameters that was designed to deliver
* a sharp image while still providing a low amount of ringing artifacts, and
* having a reasonable computational cost.
*/
struct CImageResizerParamsDef : public CImageResizerParams
{
CImageResizerParamsDef()
{
CorrFltAlpha = 1.0;//10.01/1.88/1.029(522.43)/0.019169:258648,446808
CorrFltLen = 6.30770;
IntFltAlpha = 2.27825;
IntFltCutoff = 0.75493;
IntFltLen = 18.0;
LPFltAlpha = 3.40127;
LPFltBaseLen = 7.78;
LPFltCutoffMult = 0.78797;
}
};
/**
* @brief Set of resizing algorithm parameters for ultra-low-ringing
* performance (7.69/1.069/0.000245).
*
* This set of resizing algorithm parameters offers the lowest amount of
* ringing this library is capable of providing while still offering a decent
* quality. Low ringing is attained at the expense of higher aliasing
* artifacts and a slightly reduced contrast.
*/
struct CImageResizerParamsULR : public CImageResizerParams
{
CImageResizerParamsULR()
{
CorrFltAlpha = 1.0;//7.69/1.97/1.069(31445.45)/0.000245:258627,436845
CorrFltLen = 5.83280;
IntFltAlpha = 2.11453;
IntFltCutoff = 0.73986;
IntFltLen = 18.0;
LPFltAlpha = 1.73455;
LPFltBaseLen = 6.40;
LPFltCutoffMult = 0.61314;
}
};
/**
* @brief Set of resizing algorithm parameters for low-ringing performance
* (7.86/1.065/0.000106).
*
* This set of resizing algorithm parameters offers a very low-ringing
* performance at the expense of higher aliasing artifacts and a slightly
* reduced contrast.
*/
struct CImageResizerParamsLR : public CImageResizerParams
{
CImageResizerParamsLR()
{
CorrFltAlpha = 1.0;//7.86/1.96/1.065(73865.02)/0.000106:258636,437381
CorrFltLen = 5.87671;
IntFltAlpha = 2.25322;
IntFltCutoff = 0.74090;
IntFltLen = 18.0;
LPFltAlpha = 1.79306;
LPFltBaseLen = 7.00;
LPFltCutoffMult = 0.68881;
}
};
/**
* @brief Set of resizing algorithm parameters for lower-ringing performance
* (8.86/1.046/0.010168).
*
* This set of resizing algorithm parameters offers a lower-ringing
* performance in comparison to the default setting, at the expense of higher
* aliasing artifacts and a slightly reduced contrast.
*/
struct CImageResizerParamsLow : public CImageResizerParams
{
CImageResizerParamsLow()
{
CorrFltAlpha = 1.0;//8.86/1.92/1.046(871.54)/0.010168:258647,442252
CorrFltLen = 6.09757;
IntFltAlpha = 2.36704;
IntFltCutoff = 0.74674;
IntFltLen = 18.0;
LPFltAlpha = 2.19427;
LPFltBaseLen = 7.66;
LPFltCutoffMult = 0.75380;
}
};
/**
* @brief Set of resizing algorithm parameters for low-aliasing
* resizing (11.81/1.012/0.038379).
*
* This set of resizing algorithm parameters offers a considerable
* anti-aliasing performance with a good frequency response linearity (and
* contrast). This is an intermediate setting between the default and Ultra
* parameters.
*/
struct CImageResizerParamsHigh : public CImageResizerParams
{
CImageResizerParamsHigh()
{
CorrFltAlpha = 1.0;//11.81/1.83/1.012(307.84)/0.038379:258660,452719
CorrFltLen = 6.80909;
IntFltAlpha = 2.44917;
IntFltCutoff = 0.75856;
IntFltLen = 18.0;
LPFltAlpha = 4.39527;
LPFltBaseLen = 8.18;
LPFltCutoffMult = 0.79172;
}
};
/**
* @brief Set of resizing algorithm parameters for ultra low-aliasing
* resizing (13.65/1.001/0.000483).
*
* This set of resizing algorithm parameters offers a very considerable
* anti-aliasing performance with a good frequency response linearity (and
* contrast). This set of parameters is computationally expensive and may
* produce ringing artifacts on sharp features.
*/
struct CImageResizerParamsUltra : public CImageResizerParams
{
CImageResizerParamsUltra()
{
CorrFltAlpha = 1.0;//13.65/1.79/1.001(28288.41)/0.000483:258658,457974
CorrFltLen = 7.48060;
IntFltAlpha = 1.93750;
IntFltCutoff = 0.75462;
IntFltLen = 18.0;
LPFltAlpha = 5.55209;
LPFltBaseLen = 8.34;
LPFltCutoffMult = 0.78002;
}
};
/**
* @brief Image resizing variables class.
*
* This is an utility "catch all" class that defines various variables used
* during image resizing. Several variables that are explicitly initialized in
* this class' constructor are also used as additional "input" variables to
* the image resizing function. These variables will not be changed by the
* avir::CImageResizer<>::resizeImage() function.
*/
class CImageResizerVars
{
public:
int ElCount; ///< The number of "fptype" elements used to store 1 pixel.
///<
int ElCountIO; ///< The number of source and destination image's elements
///< used to store 1 pixel.
///<
int fppack; ///< The number of atomic types stored in a single "fptype"
///< element.
///<
int fpalign; ///< Suggested alignment size in bytes. This is not a
///< required alignment, because image resizing algorithm cannot be
///< made to have a strictly aligned data access in all cases (e.g.
///< de-interleaved interpolation cannot perform aligned accesses).
///<
int elalign; ///< Length alignment of arrays of elements. This applies to
///< filters and intermediate buffers: this constant forces filters
///< and scanlines to have a length which is a multiple of this value,
///< for more efficient SIMD implementation.
///<
int packmode; ///< 0 if interleaved packing, 1 if de-interleaved.
///<
int BufLen[ 2 ]; ///< Intermediate buffers' lengths in "fptype" elements.
int BufOffs[ 2 ]; ///< Offsets into the intermediate buffers, used to
///< provide prefix elements required during processing so that no
///< "out of range" access happens. This offset is a multiple of
///< ElCount if pixels are stored in interleaved form.
///<
double k; ///< Resizing step coefficient, updated to reflect the actually
///< used coefficient during resizing.
///<
double o; ///< Starting pixel offset inside the source image, updated to
///< reflect the actually used offset during resizing.
///<
int ResizeStep; ///< Index of the resizing step in the latest filtering
///< steps array.
///<
double InGammaMult; ///< Input gamma multiplier, used to convert input
///< data to 0 to 1 range. 0.0 if no gamma is in use.
///<
double OutGammaMult; ///< Output gamma multiplier, used to convert data to
///< 0 to 255/65535 range. 0.0 if no gamma is in use.
///<
double ox; ///< Start X pixel offset within source image (can be
///< negative). Positive offset moves image to the left.
///<
double oy; ///< Start Y pixel offset within source image (can be
///< negative). Positive offset moves image to the top.
///<
CImageResizerThreadPool* ThreadPool; ///< Thread pool to be used by the
///< image resizing function. Set to NULL to use single-threaded
///< processing.
///<
bool UseSRGBGamma; ///< Perform sRGB gamma linearization (correction).
///<
int BuildMode; ///< The build mode to use, for debugging purposes. Set to
///< -1 to select a minimal-complexity mode automatically. All build
///< modes deliver similar results with minor deviations.
///<
int RndSeed; ///< Random seed parameter. This parameter may be incremented
///< after each random generator initialization. The use of this
///< variable depends on the ditherer implementation.
///<
CImageResizerVars()
: ox( 0.0 )
, oy( 0.0 )
, ThreadPool( NULL )
, UseSRGBGamma( false )
, BuildMode( -1 )
, RndSeed( 0 )
{
}
};
/**
* @brief Image resizer's filtering step class.
*
* Class defines data to perform a single filtering step over a whole
* horizontal or vertical scanline. Resizing consists of 1 or more steps that
* may be performed before the actual resizing takes place. Filtering may also
* follow a resizing step. Each step must ensure that scanline data contains
* enough pixels to perform the next step (which may be resizing) without
* exceeding scanline's bounds.
*
* A derived class must implement several "const" and "static" functions that
* are used to perform the actual filtering in interleaved or de-interleaved
* mode.
*
* @tparam fptype Floating point type to use for storing pixel elements. SIMD
* types can be used: in this case each element may hold a whole pixel.
* @tparam fptypeatom The atomic type the "fptype" consists of.
*/
template< class fptype, class fptypeatom >
class CImageResizerFilterStep
{
public:
bool IsUpsample; ///< "True" if this step is an upsampling step, "false"
///< if downsampling step. Should be set to "false" if ResampleFactor
///< equals 0.
///<
int ResampleFactor; ///< Resample factor (>=1). If 0, this is a resizing
///< step. This value should be >1 if IsUpsample equals "true".
///<
CBuffer< fptype > Flt; ///< Filter to use at this step.
///<
CFltBuffer FltOrig; ///< Originally-designed filter. This buffer may not
///< be assigned. Assigned by filters that precede the resizing step
///< if such filter is planned to be embedded into the interpolation
///< filter as "external" filter. If IsUpsample=true and this filter
///< buffer is not empty, the upsampling step will not itself apply
///< any filtering over upsampled input scanline.
///<
double DCGain; ///< DC gain which was applied to the filter. Not defined
///< if ResampleFactor = 0.
///<
int FltLatency; ///< Filter's latency (group delay, shift) in pixels.
///<
const CImageResizerVars* Vars; ///< Image resizing-related variables.
///<
int InLen; ///< Input scanline's length in pixels.
///<
int InBuf; ///< Input buffer index, 0 or 1.
///<
int InPrefix; ///< Required input prefix pixels. These prefix pixels will
///< be filled with source scanline's first pixel value. If IsUpsample
///< is "true", this is the additional number of times the first pixel
///< will be filtered before processing scanline, this number is also
///< reflected in the OutPrefix.
///<
int InSuffix; ///< Required input suffix pixels. These suffix pixels will
///< be filled with source scanline's last pixel value. If IsUpsample
///< is "true", this is the additional number of times the last pixel
///< will be filtered before processing scanline, this number is also
///< reflected in the OutSuffix.
///<
int InElIncr; ///< Pixel element increment within the input buffer, used
///< during de-interleaved processing: in this case each image's
///< channel is stored independently, InElIncr elements apart.
///<
int OutLen; ///< Length of the resulting scanline.
///<
int OutBuf; ///< Output buffer index. 0 or 1; 2 for the last step.
///<
int OutPrefix; ///< Required output prefix pixels. These prefix pixels
///< will not be pre-filled with any values. Value is valid only if
///< IsUpsample equals "true".
///<
int OutSuffix; ///< Required input suffix pixels. These suffix pixels will
///< not be pre-filled with any values. Value is valid only if
///< IsUpsample equals "true".
///<
int OutElIncr; ///< Pixel element increment within the output buffer, used
///< during de-interleaved processing. Equals to the InBufElIncr of
///< the next step.
///<
CBuffer< fptype > PrefixDC; ///< DC component fluctuations added at the
///< start of the resulting scanline, used when IsUpsample equals
///< "true".
///<
CBuffer< fptype > SuffixDC; ///< DC component fluctuations added at the
///< end of the resulting scanline, used when IsUpsample equals
///< "true".
///<
int EdgePixelCount; ///< The number of edge pixels added. Affects the
///< initial position within the input scanline, used to produce edge
///< pixels. This variable is used and should be defined when
///< IsUpsample=false and ResampleFactor>0. When assigning this
///< variable it is also necessary to update InPrefix, OutLen and
///< Vars.o variables.
///<
static const int EdgePixelCountDef = 3; ///< The default number of pixels
///< additionally produced at scanline edges during filtering. This is
///< required to reduce edge artifacts.
///<
/**
* @brief Resizing position structure.
*
* Structure holds resizing position and pointer to fractional delay
* filter.
*/
struct CResizePos
{
int SrcPosInt; ///< Source scanline position.
///<
int fti; ///< Fractional delay filter index.
///<
const fptype* ftp; ///< Fractional delay filter pointer.
///<
fptypeatom x; ///< Interpolation coefficient between delay filters.
///<
int SrcOffs; ///< Source scanline offset.
///<
};
/**
* @brief Resizing positions buffer class.
*
* This class combines buffer together with variables that define resizing
* stepping.
*/
class CRPosBuf : public CBuffer< CResizePos >
{
public:
double k; ///< Resizing step.
///<
double o; ///< Resizing offset.
///<
int FracCount; ///< The number of fractional delay filters in a filter
///< bank used together with this buffer.
///<
};
/**
* @brief Resizing positions buffer array class.
*
* This class combines structure array of the CRPosBuf class objects with
* the function that locates or creates buffer with the required resizing
* stepping.
*/
class CRPosBufArray : public CStructArray< CRPosBuf >
{
public:
using CStructArray< CRPosBuf > :: add;
using CStructArray< CRPosBuf > :: getItemCount;
/**
* Function returns the resizing positions buffer with the required
* stepping. If no such buffer exists, it is created.
*
* @param k Resizing step.
* @param o Resizing offset.
* @param FracCount The number of fractional delay filters in a filter
* bank used together with this buffer.
* @return Reference to the CRPosBuf object.
*/
CRPosBuf& getRPosBuf( const double k, const double o,
const int FracCount )
{
int i;
for( i = 0; i < getItemCount(); i++ )
{
CRPosBuf& Buf = (*this)[ i ];
if( Buf.k == k && Buf.o == o && Buf.FracCount == FracCount )
{
return( Buf );
}
}
CRPosBuf& NewBuf = add();
NewBuf.k = k;
NewBuf.o = o;
NewBuf.FracCount = FracCount;
return( NewBuf );
}
};
CRPosBuf* RPosBuf; ///< Resizing positions buffer. Used when
///< ResampleFactor equals 0 (resizing step).
///<
CDSPFracFilterBankLin< fptype >* FltBank; ///< Filter bank in use by *this
///< resizing step.
///<
};
/**
* @brief Interleaved filtering steps implementation class.
*
* This class implements scanline filtering functions in interleaved mode.
* This means that each pixel is processed independently, not in groups.
*
* @tparam fptype Floating point type to use for storing pixel elements. SIMD
* types can be used: in this case each element may hold a whole pixel.
* @tparam fptypeatom The atomic type the "fptype" consists of.
*/
template< class fptype, class fptypeatom >
class CImageResizerFilterStepINL :
public CImageResizerFilterStep< fptype, fptypeatom >
{
public:
using CImageResizerFilterStep< fptype, fptypeatom > :: IsUpsample;
using CImageResizerFilterStep< fptype, fptypeatom > :: ResampleFactor;
using CImageResizerFilterStep< fptype, fptypeatom > :: Flt;
using CImageResizerFilterStep< fptype, fptypeatom > :: FltOrig;
using CImageResizerFilterStep< fptype, fptypeatom > :: FltLatency;
using CImageResizerFilterStep< fptype, fptypeatom > :: Vars;
using CImageResizerFilterStep< fptype, fptypeatom > :: InLen;
using CImageResizerFilterStep< fptype, fptypeatom > :: InPrefix;
using CImageResizerFilterStep< fptype, fptypeatom > :: InSuffix;
using CImageResizerFilterStep< fptype, fptypeatom > :: OutLen;
using CImageResizerFilterStep< fptype, fptypeatom > :: OutPrefix;
using CImageResizerFilterStep< fptype, fptypeatom > :: OutSuffix;
using CImageResizerFilterStep< fptype, fptypeatom > :: PrefixDC;
using CImageResizerFilterStep< fptype, fptypeatom > :: SuffixDC;
using CImageResizerFilterStep< fptype, fptypeatom > :: RPosBuf;
using CImageResizerFilterStep< fptype, fptypeatom > :: FltBank;
using CImageResizerFilterStep< fptype, fptypeatom > :: EdgePixelCount;
/**
* Function performs "packing" of a scanline and type conversion.
* Scanline, depending on the "fptype" can be potentially stored as a
* packed SIMD values having a certain atomic type. If required, the sRGB
* gamma correction is applied.
*
* @param ip Input scanline.
* @param op0 Output scanline.
* @param l0 The number of pixels to "pack".
*/
template< class Tin >
void packScanline( const Tin* ip, fptype* const op0, const int l0 ) const
{
const int ElCount = Vars -> ElCount;
const int ElCountIO = Vars -> ElCountIO;
fptype* op = op0;
int l = l0;
if( !Vars -> UseSRGBGamma )
{
if( ElCountIO == 1 )
{
while( l > 0 )
{
fptypeatom* v = (fptypeatom*) op;
v[ 0 ] = (fptypeatom) ip[ 0 ];
op += ElCount;
ip++;
l--;
}
}
else
if( ElCountIO == 4 )
{
while( l > 0 )
{
fptypeatom* v = (fptypeatom*) op;
v[ 0 ] = (fptypeatom) ip[ 0 ];
v[ 1 ] = (fptypeatom) ip[ 1 ];
v[ 2 ] = (fptypeatom) ip[ 2 ];
v[ 3 ] = (fptypeatom) ip[ 3 ];
op += ElCount;
ip += 4;
l--;
}
}
else
if( ElCountIO == 3 )
{
while( l > 0 )
{
fptypeatom* v = (fptypeatom*) op;
v[ 0 ] = (fptypeatom) ip[ 0 ];
v[ 1 ] = (fptypeatom) ip[ 1 ];
v[ 2 ] = (fptypeatom) ip[ 2 ];
op += ElCount;
ip += 3;
l--;
}
}
else
if( ElCountIO == 2 )
{
while( l > 0 )
{
fptypeatom* v = (fptypeatom*) op;
v[ 0 ] = (fptypeatom) ip[ 0 ];
v[ 1 ] = (fptypeatom) ip[ 1 ];
op += ElCount;
ip += 2;
l--;
}
}
}
else
{
const fptypeatom gm = (fptypeatom) Vars -> InGammaMult;
if( ElCountIO == 1 )
{
while( l > 0 )
{
fptypeatom* v = (fptypeatom*) op;
v[ 0 ] = convertSRGB2Lin( (fptypeatom) ip[ 0 ] * gm );
op += ElCount;
ip++;
l--;
}
}
else
if( ElCountIO == 4 )
{
while( l > 0 )
{
fptypeatom* v = (fptypeatom*) op;
v[ 0 ] = convertSRGB2Lin( (fptypeatom) ip[ 0 ] * gm );
v[ 1 ] = convertSRGB2Lin( (fptypeatom) ip[ 1 ] * gm );
v[ 2 ] = convertSRGB2Lin( (fptypeatom) ip[ 2 ] * gm );
v[ 3 ] = convertSRGB2Lin( (fptypeatom) ip[ 3 ] * gm );
op += ElCount;
ip += 4;
l--;
}
}
else
if( ElCountIO == 3 )
{
while( l > 0 )
{
fptypeatom* v = (fptypeatom*) op;
v[ 0 ] = convertSRGB2Lin( (fptypeatom) ip[ 0 ] * gm );
v[ 1 ] = convertSRGB2Lin( (fptypeatom) ip[ 1 ] * gm );
v[ 2 ] = convertSRGB2Lin( (fptypeatom) ip[ 2 ] * gm );
op += ElCount;
ip += 3;
l--;
}
}
else
if( ElCountIO == 2 )
{
while( l > 0 )
{
fptypeatom* v = (fptypeatom*) op;
v[ 0 ] = convertSRGB2Lin( (fptypeatom) ip[ 0 ] * gm );
v[ 1 ] = convertSRGB2Lin( (fptypeatom) ip[ 1 ] * gm );
op += ElCount;
ip += 2;
l--;
}
}
}
const int ZeroCount = ElCount * Vars -> fppack - ElCountIO;
op = op0;
l = l0;
if( ZeroCount == 1 )
{
while( l > 0 )
{
fptypeatom* v = (fptypeatom*) op + ElCountIO;
v[ 0 ] = (fptypeatom) 0;
op += ElCount;
l--;
}
}
else
if( ZeroCount == 2 )
{
while( l > 0 )
{
fptypeatom* v = (fptypeatom*) op + ElCountIO;
v[ 0 ] = (fptypeatom) 0;
v[ 1 ] = (fptypeatom) 0;
op += ElCount;
l--;
}
}
else
if( ZeroCount == 3 )
{
while( l > 0 )
{
fptypeatom* v = (fptypeatom*) op + ElCountIO;
v[ 0 ] = (fptypeatom) 0;
v[ 1 ] = (fptypeatom) 0;
v[ 2 ] = (fptypeatom) 0;
op += ElCount;
l--;
}
}
}
/**
* Function applies Linear to sRGB gamma correction to the specified
* scanline.
*
* @param p Scanline.
* @param l The number of pixels to de-linearize.
* @param Vars0 Image resizing-related variables.
*/
static void applySRGBGamma( fptype* p, int l,
const CImageResizerVars& Vars0 )
{
const int ElCount = Vars0.ElCount;
const int ElCountIO = Vars0.ElCountIO;
const fptypeatom gm = (fptypeatom) Vars0.OutGammaMult;
if( ElCountIO == 1 )
{
while( l > 0 )
{
fptypeatom* v = (fptypeatom*) p;
v[ 0 ] = convertLin2SRGB( v[ 0 ]) * gm;
p += ElCount;
l--;
}
}
else
if( ElCountIO == 4 )
{
while( l > 0 )
{
fptypeatom* v = (fptypeatom*) p;
v[ 0 ] = convertLin2SRGB( v[ 0 ]) * gm;
v[ 1 ] = convertLin2SRGB( v[ 1 ]) * gm;
v[ 2 ] = convertLin2SRGB( v[ 2 ]) * gm;
v[ 3 ] = convertLin2SRGB( v[ 3 ]) * gm;
p += ElCount;
l--;
}
}
else
if( ElCountIO == 3 )
{
while( l > 0 )
{
fptypeatom* v = (fptypeatom*) p;
v[ 0 ] = convertLin2SRGB( v[ 0 ]) * gm;
v[ 1 ] = convertLin2SRGB( v[ 1 ]) * gm;
v[ 2 ] = convertLin2SRGB( v[ 2 ]) * gm;
p += ElCount;
l--;
}
}
else
if( ElCountIO == 2 )
{
while( l > 0 )
{
fptypeatom* v = (fptypeatom*) p;
v[ 0 ] = convertLin2SRGB( v[ 0 ]) * gm;
v[ 1 ] = convertLin2SRGB( v[ 1 ]) * gm;
p += ElCount;
l--;
}
}
}
/**
* Function converts vertical scanline to horizontal scanline. This
* function is called by the image resizer when image is resized
* vertically. This means that the vertical scanline is stored in the
* same format produced by the packScanline() and maintained by other
* filtering functions.
*
* @param ip Input vertical scanline.
* @param op Output buffer (temporary buffer used during resizing).
* @param SrcLen The number of pixels in the input scanline, also used to
* calculate input buffer increment.
* @param SrcIncr Input buffer increment to the next vertical pixel.
*/
void convertVtoH( const fptype* ip, fptype* op, const int SrcLen,
const int SrcIncr ) const
{
const int ElCount = Vars -> ElCount;
int j;
if( ElCount == 1 )
{
for( j = 0; j < SrcLen; j++ )
{
op[ 0 ] = ip[ 0 ];
ip += SrcIncr;
op++;
}
}
else
if( ElCount == 4 )
{
for( j = 0; j < SrcLen; j++ )
{
op[ 0 ] = ip[ 0 ];
op[ 1 ] = ip[ 1 ];
op[ 2 ] = ip[ 2 ];
op[ 3 ] = ip[ 3 ];
ip += SrcIncr;
op += 4;
}
}
else
if( ElCount == 3 )
{
for( j = 0; j < SrcLen; j++ )
{
op[ 0 ] = ip[ 0 ];
op[ 1 ] = ip[ 1 ];
op[ 2 ] = ip[ 2 ];
ip += SrcIncr;
op += 3;
}
}
else
if( ElCount == 2 )
{
for( j = 0; j < SrcLen; j++ )
{
op[ 0 ] = ip[ 0 ];
op[ 1 ] = ip[ 1 ];
ip += SrcIncr;
op += 2;
}
}
}
/**
* Function performs "unpacking" of a scanline and type conversion
* (truncation is used when floating point is converted to integer).
* Scanline, depending on the "fptype" can be potentially stored as a
* packed SIMD values having a certain atomic type. The unpacking function
* assumes that scanline is stored in the style produced by the
* packScanline() function.
*
* @param ip Input scanline.
* @param op Output scanline.
* @param l The number of pixels to "unpack".
* @param Vars0 Image resizing-related variables.
*/
template< class Tout >
static void unpackScanline( const fptype* ip, Tout* op, int l,
const CImageResizerVars& Vars0 )
{
const int ElCount = Vars0.ElCount;
const int ElCountIO = Vars0.ElCountIO;
if( ElCountIO == 1 )
{
while( l > 0 )
{
const fptypeatom* v = (const fptypeatom*) ip;
op[ 0 ] = (Tout) v[ 0 ];
ip += ElCount;
op++;
l--;
}
}
else
if( ElCountIO == 4 )
{
while( l > 0 )
{
const fptypeatom* v = (const fptypeatom*) ip;
op[ 0 ] = (Tout) v[ 0 ];
op[ 1 ] = (Tout) v[ 1 ];
op[ 2 ] = (Tout) v[ 2 ];
op[ 3 ] = (Tout) v[ 3 ];
ip += ElCount;
op += 4;
l--;
}
}
else
if( ElCountIO == 3 )
{
while( l > 0 )
{
const fptypeatom* v = (const fptypeatom*) ip;
op[ 0 ] = (Tout) v[ 0 ];
op[ 1 ] = (Tout) v[ 1 ];
op[ 2 ] = (Tout) v[ 2 ];
ip += ElCount;
op += 3;
l--;
}
}
else
if( ElCountIO == 2 )
{
while( l > 0 )
{
const fptypeatom* v = (const fptypeatom*) ip;
op[ 0 ] = (Tout) v[ 0 ];
op[ 1 ] = (Tout) v[ 1 ];
ip += ElCount;
op += 2;
l--;
}
}
}
/**
* Function prepares input scanline buffer for *this filtering step.
* Left- and right-most pixels are replicated to make sure no buffer
* overrun happens. Such approach also allows to bypass any pointer
* range checks.
*
* @param Src Source buffer.
*/
void prepareInBuf( fptype* Src ) const
{
if( IsUpsample || InPrefix + InSuffix == 0 )
{
return;
}
const int ElCount = Vars -> ElCount;
replicateArray( Src, ElCount, Src - ElCount, InPrefix, -ElCount );
Src += ( InLen - 1 ) * ElCount;
replicateArray( Src, ElCount, Src + ElCount, InSuffix, ElCount );
}
/**
* Function peforms scanline upsampling with filtering.
*
* @param Src Source scanline buffer (length = this -> InLen). Source
* scanline increment will be equal to ElCount.
* @param Dst Destination scanline buffer.
*/
void doUpsample( const fptype* const Src, fptype* const Dst ) const
{
const int ElCount = Vars -> ElCount;
fptype* op0 = &Dst[ -OutPrefix * ElCount ];
memset( op0, 0, ( OutPrefix + OutLen + OutSuffix ) * ElCount *
sizeof( fptype ));
const fptype* ip = Src;
const int opstep = ElCount * ResampleFactor;
int l;
if( FltOrig.getCapacity() > 0 )
{
// Do not perform filtering, only upsample.
op0 += ( OutPrefix % ResampleFactor ) * ElCount;
l = OutPrefix / ResampleFactor;
if( ElCount == 1 )
{
while( l > 0 )
{
op0[ 0 ] = ip[ 0 ];
op0 += opstep;
l--;
}
l = InLen - 1;
while( l > 0 )
{
op0[ 0 ] = ip[ 0 ];
op0 += opstep;
ip += ElCount;
l--;
}
l = OutSuffix / ResampleFactor;
while( l >= 0 )
{
op0[ 0 ] = ip[ 0 ];
op0 += opstep;
l--;
}
}
else
if( ElCount == 4 )
{
while( l > 0 )
{
op0[ 0 ] = ip[ 0 ];
op0[ 1 ] = ip[ 1 ];
op0[ 2 ] = ip[ 2 ];
op0[ 3 ] = ip[ 3 ];
op0 += opstep;
l--;
}
l = InLen - 1;
while( l > 0 )
{
op0[ 0 ] = ip[ 0 ];
op0[ 1 ] = ip[ 1 ];
op0[ 2 ] = ip[ 2 ];
op0[ 3 ] = ip[ 3 ];
op0 += opstep;
ip += ElCount;
l--;
}
l = OutSuffix / ResampleFactor;
while( l >= 0 )
{
op0[ 0 ] = ip[ 0 ];
op0[ 1 ] = ip[ 1 ];
op0[ 2 ] = ip[ 2 ];
op0[ 3 ] = ip[ 3 ];
op0 += opstep;
l--;
}
}
else
if( ElCount == 3 )
{
while( l > 0 )
{
op0[ 0 ] = ip[ 0 ];
op0[ 1 ] = ip[ 1 ];
op0[ 2 ] = ip[ 2 ];
op0 += opstep;
l--;
}
l = InLen - 1;
while( l > 0 )
{
op0[ 0 ] = ip[ 0 ];
op0[ 1 ] = ip[ 1 ];
op0[ 2 ] = ip[ 2 ];
op0 += opstep;
ip += ElCount;
l--;
}
l = OutSuffix / ResampleFactor;
while( l >= 0 )
{
op0[ 0 ] = ip[ 0 ];
op0[ 1 ] = ip[ 1 ];
op0[ 2 ] = ip[ 2 ];
op0 += opstep;
l--;
}
}
else
if( ElCount == 2 )
{
while( l > 0 )
{
op0[ 0 ] = ip[ 0 ];
op0[ 1 ] = ip[ 1 ];
op0 += opstep;
l--;
}
l = InLen - 1;
while( l > 0 )
{
op0[ 0 ] = ip[ 0 ];
op0[ 1 ] = ip[ 1 ];
op0 += opstep;
ip += ElCount;
l--;
}
l = OutSuffix / ResampleFactor;
while( l >= 0 )
{
op0[ 0 ] = ip[ 0 ];
op0[ 1 ] = ip[ 1 ];
op0 += opstep;
l--;
}
}
return;
}
const fptype* const f = Flt;
const int flen = Flt.getCapacity();
fptype* op;
int i;
if( ElCount == 1 )
{
l = InPrefix;
while( l > 0 )
{
op = op0;
for( i = 0; i < flen; i++ )
{
op[ i ] += f[ i ] * ip[ 0 ];
}
op0 += opstep;
l--;
}
l = InLen - 1;
while( l > 0 )
{
op = op0;
for( i = 0; i < flen; i++ )
{
op[ i ] += f[ i ] * ip[ 0 ];
}
ip += ElCount;
op0 += opstep;
l--;
}
l = InSuffix;
while( l >= 0 )
{
op = op0;
for( i = 0; i < flen; i++ )
{
op[ i ] += f[ i ] * ip[ 0 ];
}
op0 += opstep;
l--;
}
}
else
if( ElCount == 4 )
{
l = InPrefix;
while( l > 0 )
{
op = op0;
for( i = 0; i < flen; i++ )
{
op[ 0 ] += f[ i ] * ip[ 0 ];
op[ 1 ] += f[ i ] * ip[ 1 ];
op[ 2 ] += f[ i ] * ip[ 2 ];
op[ 3 ] += f[ i ] * ip[ 3 ];
op += 4;
}
op0 += opstep;
l--;
}
l = InLen - 1;
while( l > 0 )
{
op = op0;
for( i = 0; i < flen; i++ )
{
op[ 0 ] += f[ i ] * ip[ 0 ];
op[ 1 ] += f[ i ] * ip[ 1 ];
op[ 2 ] += f[ i ] * ip[ 2 ];
op[ 3 ] += f[ i ] * ip[ 3 ];
op += 4;
}
ip += ElCount;
op0 += opstep;
l--;
}
l = InSuffix;
while( l >= 0 )
{
op = op0;
for( i = 0; i < flen; i++ )
{
op[ 0 ] += f[ i ] * ip[ 0 ];
op[ 1 ] += f[ i ] * ip[ 1 ];
op[ 2 ] += f[ i ] * ip[ 2 ];
op[ 3 ] += f[ i ] * ip[ 3 ];
op += 4;
}
op0 += opstep;
l--;
}
}
else
if( ElCount == 3 )
{
l = InPrefix;
while( l > 0 )
{
op = op0;
for( i = 0; i < flen; i++ )
{
op[ 0 ] += f[ i ] * ip[ 0 ];
op[ 1 ] += f[ i ] * ip[ 1 ];
op[ 2 ] += f[ i ] * ip[ 2 ];
op += 3;
}
op0 += opstep;
l--;
}
l = InLen - 1;
while( l > 0 )
{
op = op0;
for( i = 0; i < flen; i++ )
{
op[ 0 ] += f[ i ] * ip[ 0 ];
op[ 1 ] += f[ i ] * ip[ 1 ];
op[ 2 ] += f[ i ] * ip[ 2 ];
op += 3;
}
ip += ElCount;
op0 += opstep;
l--;
}
l = InSuffix;
while( l >= 0 )
{
op = op0;
for( i = 0; i < flen; i++ )
{
op[ 0 ] += f[ i ] * ip[ 0 ];
op[ 1 ] += f[ i ] * ip[ 1 ];
op[ 2 ] += f[ i ] * ip[ 2 ];
op += 3;
}
op0 += opstep;
l--;
}
}
else
if( ElCount == 2 )
{
l = InPrefix;
while( l > 0 )
{
op = op0;
for( i = 0; i < flen; i++ )
{
op[ 0 ] += f[ i ] * ip[ 0 ];
op[ 1 ] += f[ i ] * ip[ 1 ];
op += 2;
}
op0 += opstep;
l--;
}
l = InLen - 1;
while( l > 0 )
{
op = op0;
for( i = 0; i < flen; i++ )
{
op[ 0 ] += f[ i ] * ip[ 0 ];
op[ 1 ] += f[ i ] * ip[ 1 ];
op += 2;
}
ip += ElCount;
op0 += opstep;
l--;
}
l = InSuffix;
while( l >= 0 )
{
op = op0;
for( i = 0; i < flen; i++ )
{
op[ 0 ] += f[ i ] * ip[ 0 ];
op[ 1 ] += f[ i ] * ip[ 1 ];
op += 2;
}
op0 += opstep;
l--;
}
}
op = op0;
const fptype* dc = SuffixDC;
l = SuffixDC.getCapacity();
if( ElCount == 1 )
{
for( i = 0; i < l; i++ )
{
op[ i ] += ip[ 0 ] * dc[ i ];
}
}
else
if( ElCount == 4 )
{
while( l > 0 )
{
op[ 0 ] += ip[ 0 ] * dc[ 0 ];
op[ 1 ] += ip[ 1 ] * dc[ 0 ];
op[ 2 ] += ip[ 2 ] * dc[ 0 ];
op[ 3 ] += ip[ 3 ] * dc[ 0 ];
dc++;
op += 4;
l--;
}
}
else
if( ElCount == 3 )
{
while( l > 0 )
{
op[ 0 ] += ip[ 0 ] * dc[ 0 ];
op[ 1 ] += ip[ 1 ] * dc[ 0 ];
op[ 2 ] += ip[ 2 ] * dc[ 0 ];
dc++;
op += 3;
l--;
}
}
else
if( ElCount == 2 )
{
while( l > 0 )
{
op[ 0 ] += ip[ 0 ] * dc[ 0 ];
op[ 1 ] += ip[ 1 ] * dc[ 0 ];
dc++;
op += 2;
l--;
}
}
ip = Src;
op = Dst - InPrefix * opstep;
dc = PrefixDC;
l = PrefixDC.getCapacity();
if( ElCount == 1 )
{
for( i = 0; i < l; i++ )
{
op[ i ] += ip[ 0 ] * dc[ i ];
}
}
else
if( ElCount == 4 )
{
while( l > 0 )
{
op[ 0 ] += ip[ 0 ] * dc[ 0 ];
op[ 1 ] += ip[ 1 ] * dc[ 0 ];
op[ 2 ] += ip[ 2 ] * dc[ 0 ];
op[ 3 ] += ip[ 3 ] * dc[ 0 ];
dc++;
op += 4;
l--;
}
}
else
if( ElCount == 3 )
{
while( l > 0 )
{
op[ 0 ] += ip[ 0 ] * dc[ 0 ];
op[ 1 ] += ip[ 1 ] * dc[ 0 ];
op[ 2 ] += ip[ 2 ] * dc[ 0 ];
dc++;
op += 3;
l--;
}
}
else
if( ElCount == 2 )
{
while( l > 0 )
{
op[ 0 ] += ip[ 0 ] * dc[ 0 ];
op[ 1 ] += ip[ 1 ] * dc[ 0 ];
dc++;
op += 2;
l--;
}
}
}
/**
* Function peforms scanline filtering with optional downsampling.
* Function makes use of the symmetry of the filter.
*
* @param Src Source scanline buffer (length = this -> InLen). Source
* scanline increment will be equal to ElCount.
* @param Dst Destination scanline buffer.
* @param DstIncr Destination scanline buffer increment, used for
* horizontal or vertical scanline stepping.
*/
void doFilter( const fptype* const Src, fptype* Dst,
const int DstIncr ) const
{
const int ElCount = Vars -> ElCount;
const fptype* const f = &Flt[ FltLatency ];
const int flen = FltLatency + 1;
const int ipstep = ElCount * ResampleFactor;
const fptype* ip = Src - EdgePixelCount * ipstep;
const fptype* ip1;
const fptype* ip2;
int l = OutLen;
int i;
if( ElCount == 1 )
{
while( l > 0 )
{
fptype s = f[ 0 ] * ip[ 0 ];
ip1 = ip;
ip2 = ip;
for( i = 1; i < flen; i++ )
{
ip1++;
ip2--;
s += f[ i ] * ( ip1[ 0 ] + ip2[ 0 ]);
}
Dst[ 0 ] = s;
Dst += DstIncr;
ip += ipstep;
l--;
}
}
else
if( ElCount == 4 )
{
while( l > 0 )
{
fptype s1 = f[ 0 ] * ip[ 0 ];
fptype s2 = f[ 0 ] * ip[ 1 ];
fptype s3 = f[ 0 ] * ip[ 2 ];
fptype s4 = f[ 0 ] * ip[ 3 ];
ip1 = ip;
ip2 = ip;
for( i = 1; i < flen; i++ )
{
ip1 += 4;
ip2 -= 4;
s1 += f[ i ] * ( ip1[ 0 ] + ip2[ 0 ]);
s2 += f[ i ] * ( ip1[ 1 ] + ip2[ 1 ]);
s3 += f[ i ] * ( ip1[ 2 ] + ip2[ 2 ]);
s4 += f[ i ] * ( ip1[ 3 ] + ip2[ 3 ]);
}
Dst[ 0 ] = s1;
Dst[ 1 ] = s2;
Dst[ 2 ] = s3;
Dst[ 3 ] = s4;
Dst += DstIncr;
ip += ipstep;
l--;
}
}
else
if( ElCount == 3 )
{
while( l > 0 )
{
fptype s1 = f[ 0 ] * ip[ 0 ];
fptype s2 = f[ 0 ] * ip[ 1 ];
fptype s3 = f[ 0 ] * ip[ 2 ];
ip1 = ip;
ip2 = ip;
for( i = 1; i < flen; i++ )
{
ip1 += 3;
ip2 -= 3;
s1 += f[ i ] * ( ip1[ 0 ] + ip2[ 0 ]);
s2 += f[ i ] * ( ip1[ 1 ] + ip2[ 1 ]);
s3 += f[ i ] * ( ip1[ 2 ] + ip2[ 2 ]);
}
Dst[ 0 ] = s1;
Dst[ 1 ] = s2;
Dst[ 2 ] = s3;
Dst += DstIncr;
ip += ipstep;
l--;
}
}
else
if( ElCount == 2 )
{
while( l > 0 )
{
fptype s1 = f[ 0 ] * ip[ 0 ];
fptype s2 = f[ 0 ] * ip[ 1 ];
ip1 = ip;
ip2 = ip;
for( i = 1; i < flen; i++ )
{
ip1 += 2;
ip2 -= 2;
s1 += f[ i ] * ( ip1[ 0 ] + ip2[ 0 ]);
s2 += f[ i ] * ( ip1[ 1 ] + ip2[ 1 ]);
}
Dst[ 0 ] = s1;
Dst[ 1 ] = s2;
Dst += DstIncr;
ip += ipstep;
l--;
}
}
}
/**
* Function performs resizing of a single scanline. This function does
* not "know" about the length of the source scanline buffer. This buffer
* should be padded with enough pixels so that ( SrcPos - FilterLenD2 ) is
* always >= 0 and ( SrcPos + ( DstLineLen - 1 ) * k + FilterLenD2 + 1 )
* does not exceed source scanline's buffer length. SrcLine's increment is
* assumed to be equal to ElCount.
*
* @param SrcLine Source scanline buffer.
* @param DstLine Destination (resized) scanline buffer.
* @param DstLineIncr Destination scanline position increment, used for
* horizontal or vertical scanline stepping.
* @param xx Temporary buffer, of size FltBank -> getFilterLen(), must be
* aligned by fpclass :: fpalign.
*/
void doResize( const fptype* SrcLine, fptype* DstLine,
const int DstLineIncr, fptype* const ) const
{
const int IntFltLen = FltBank -> getFilterLen();
const int ElCount = Vars -> ElCount;
const typename CImageResizerFilterStep< fptype, fptypeatom > ::
CResizePos* rpos = &(*RPosBuf)[ 0 ];
const typename CImageResizerFilterStep< fptype, fptypeatom > ::
CResizePos* const rpose = rpos + OutLen;
#define AVIR_RESIZE_PART1 \
while( rpos < rpose ) \
{ \
const fptype x = (fptype) rpos -> x; \
const fptype* const ftp = rpos -> ftp; \
const fptype* const ftp2 = ftp + IntFltLen; \
const fptype* Src = SrcLine + rpos -> SrcOffs; \
int i;
#define AVIR_RESIZE_PART1nx \
while( rpos < rpose ) \
{ \
const fptype* const ftp = rpos -> ftp; \
const fptype* Src = SrcLine + rpos -> SrcOffs; \
int i;
#define AVIR_RESIZE_PART2 \
DstLine += DstLineIncr; \
rpos++; \
}
if( FltBank -> getOrder() == 1 )
{
if( ElCount == 1 )
{
AVIR_RESIZE_PART1
fptype sum = 0.0;
for( i = 0; i < IntFltLen; i++ )
{
sum += ( ftp[ i ] + ftp2[ i ] * x ) * Src[ i ];
}
DstLine[ 0 ] = sum;
AVIR_RESIZE_PART2
}
else
if( ElCount == 4 )
{
AVIR_RESIZE_PART1
fptype sum[ 4 ];
sum[ 0 ] = 0.0;
sum[ 1 ] = 0.0;
sum[ 2 ] = 0.0;
sum[ 3 ] = 0.0;
for( i = 0; i < IntFltLen; i++ )
{
const fptype xx = ftp[ i ] + ftp2[ i ] * x;
sum[ 0 ] += xx * Src[ 0 ];
sum[ 1 ] += xx * Src[ 1 ];
sum[ 2 ] += xx * Src[ 2 ];
sum[ 3 ] += xx * Src[ 3 ];
Src += 4;
}
DstLine[ 0 ] = sum[ 0 ];
DstLine[ 1 ] = sum[ 1 ];
DstLine[ 2 ] = sum[ 2 ];
DstLine[ 3 ] = sum[ 3 ];
AVIR_RESIZE_PART2
}
else
if( ElCount == 3 )
{
AVIR_RESIZE_PART1
fptype sum[ 3 ];
sum[ 0 ] = 0.0;
sum[ 1 ] = 0.0;
sum[ 2 ] = 0.0;
for( i = 0; i < IntFltLen; i++ )
{
const fptype xx = ftp[ i ] + ftp2[ i ] * x;
sum[ 0 ] += xx * Src[ 0 ];
sum[ 1 ] += xx * Src[ 1 ];
sum[ 2 ] += xx * Src[ 2 ];
Src += 3;
}
DstLine[ 0 ] = sum[ 0 ];
DstLine[ 1 ] = sum[ 1 ];
DstLine[ 2 ] = sum[ 2 ];
AVIR_RESIZE_PART2
}
else
if( ElCount == 2 )
{
AVIR_RESIZE_PART1
fptype sum[ 2 ];
sum[ 0 ] = 0.0;
sum[ 1 ] = 0.0;
for( i = 0; i < IntFltLen; i++ )
{
const fptype xx = ftp[ i ] + ftp2[ i ] * x;
sum[ 0 ] += xx * Src[ 0 ];
sum[ 1 ] += xx * Src[ 1 ];
Src += 2;
}
DstLine[ 0 ] = sum[ 0 ];
DstLine[ 1 ] = sum[ 1 ];
AVIR_RESIZE_PART2
}
}
else
{
if( ElCount == 1 )
{
AVIR_RESIZE_PART1nx
fptype sum = 0.0;
for( i = 0; i < IntFltLen; i++ )
{
sum += ftp[ i ] * Src[ i ];
}
DstLine[ 0 ] = sum;
AVIR_RESIZE_PART2
}
else
if( ElCount == 4 )
{
AVIR_RESIZE_PART1nx
fptype sum[ 4 ];
sum[ 0 ] = 0.0;
sum[ 1 ] = 0.0;
sum[ 2 ] = 0.0;
sum[ 3 ] = 0.0;
for( i = 0; i < IntFltLen; i++ )
{
const fptype xx = ftp[ i ];
sum[ 0 ] += xx * Src[ 0 ];
sum[ 1 ] += xx * Src[ 1 ];
sum[ 2 ] += xx * Src[ 2 ];
sum[ 3 ] += xx * Src[ 3 ];
Src += 4;
}
DstLine[ 0 ] = sum[ 0 ];
DstLine[ 1 ] = sum[ 1 ];
DstLine[ 2 ] = sum[ 2 ];
DstLine[ 3 ] = sum[ 3 ];
AVIR_RESIZE_PART2
}
else
if( ElCount == 3 )
{
AVIR_RESIZE_PART1nx
fptype sum[ 3 ];
sum[ 0 ] = 0.0;
sum[ 1 ] = 0.0;
sum[ 2 ] = 0.0;
for( i = 0; i < IntFltLen; i++ )
{
const fptype xx = ftp[ i ];
sum[ 0 ] += xx * Src[ 0 ];
sum[ 1 ] += xx * Src[ 1 ];
sum[ 2 ] += xx * Src[ 2 ];
Src += 3;
}
DstLine[ 0 ] = sum[ 0 ];
DstLine[ 1 ] = sum[ 1 ];
DstLine[ 2 ] = sum[ 2 ];
AVIR_RESIZE_PART2
}
else
if( ElCount == 2 )
{
AVIR_RESIZE_PART1nx
fptype sum[ 2 ];
sum[ 0 ] = 0.0;
sum[ 1 ] = 0.0;
for( i = 0; i < IntFltLen; i++ )
{
const fptype xx = ftp[ i ];
sum[ 0 ] += xx * Src[ 0 ];
sum[ 1 ] += xx * Src[ 1 ];
Src += 2;
}
DstLine[ 0 ] = sum[ 0 ];
DstLine[ 1 ] = sum[ 1 ];
AVIR_RESIZE_PART2
}
}
}
#undef AVIR_RESIZE_PART2
#undef AVIR_RESIZE_PART1nx
#undef AVIR_RESIZE_PART1
};
/**
* @brief Image resizer's default dithering class.
*
* This class defines an object that performs rounding, clipping and dithering
* operations over horizontal scanline pixels before scanline is stored in the
* output buffer.
*
* The ditherer should expect the same storage order of the pixels in a
* scanline as used in the "filtering step" class. So, a separate ditherer
* class should be defined for each scanline pixel storage style. The default
* ditherer implements a simple rounding without dithering: it can be used for
* an efficient dithering method which can be multi-threaded.
*
* @tparam fptype Floating point type to use for storing pixel data. SIMD
* types can be used.
*/
template< class fptype >
class CImageResizerDithererDefINL
{
public:
/**
* Function initializes the ditherer object.
*
* @param aLen Scanline length in pixels to process.
* @param aVars Image resizing-related variables.
* @param aTrMul Bit-depth truncation multiplier. 1 - no additional
* truncation.
* @param aPkOut Peak output value allowed.
*/
void init( const int aLen, const CImageResizerVars& aVars,
const double aTrMul, const double aPkOut )
{
Len = aLen;
Vars = &aVars;
LenE = aLen * Vars -> ElCount;
TrMul0 = aTrMul;
PkOut0 = aPkOut;
}
/**
* @return "True" if dithering is recursive relative to scanlines meaning
* multi-threaded execution is not supported by this dithering method.
*/
static bool isRecursive()
{
return( false );
}
/**
* Function performs rounding and clipping operations.
*
* @param ResScanline The buffer containing the final scanline.
*/
void dither( fptype* const ResScanline ) const
{
const fptype c0 = 0.0;
const fptype PkOut = (fptype) PkOut0;
int j;
if( TrMul0 == 1.0 )
{
// Optimization - do not perform bit depth truncation.
for( j = 0; j < LenE; j++ )
{
ResScanline[ j ] = clamp( round( ResScanline[ j ]), c0,
PkOut );
}
}
else
{
const fptype TrMul = (fptype) TrMul0;
for( j = 0; j < LenE; j++ )
{
const fptype z0 = round( ResScanline[ j ] / TrMul ) * TrMul;
ResScanline[ j ] = clamp( z0, c0, PkOut );
}
}
}
protected:
int Len; ///< Scanline's length in pixels.
///<
const CImageResizerVars* Vars; ///< Image resizing-related variables.
///<
int LenE; ///< = LenE * ElCount.
///<
double TrMul0; ///< Bit-depth truncation multiplier.
///<
double PkOut0; ///< Peak output value allowed.
///<
};
/**
* @brief Image resizer's error-diffusion dithering class, interleaved mode.
*
* This ditherer implements error-diffusion dithering which looks good, and
* whose results are compressed by PNG well. This implementation uses
* weighting coefficients obtained via machine optimization and visual
* evaluation.
*
* @tparam fptype Floating point type to use for storing pixel data. SIMD
* types can be used.
*/
template< class fptype >
class CImageResizerDithererErrdINL :
public CImageResizerDithererDefINL< fptype >
{
public:
/**
* Function initializes the ditherer object.
*
* @param aLen Scanline length in pixels to process.
* @param aVars Image resizing-related variables.
* @param aTrMul Bit-depth truncation multiplier. 1 - no additional
* truncation.
* @param aPkOut Peak output value allowed.
*/
void init( const int aLen, const CImageResizerVars& aVars,
const double aTrMul, const double aPkOut )
{
CImageResizerDithererDefINL< fptype > :: init( aLen, aVars, aTrMul,
aPkOut );
ResScanlineDith0.alloc( LenE + Vars -> ElCount, sizeof( fptype ));
ResScanlineDith = ResScanlineDith0 + Vars -> ElCount;
int i;
for( i = 0; i < LenE + Vars -> ElCount; i++ )
{
ResScanlineDith0[ i ] = 0.0;
}
}
static bool isRecursive()
{
return( true );
}
void dither( fptype* const ResScanline )
{
const int ElCount = Vars -> ElCount;
const fptype c0 = 0.0;
const fptype TrMul = (fptype) TrMul0;
const fptype PkOut = (fptype) PkOut0;
int j;
for( j = 0; j < LenE; j++ )
{
ResScanline[ j ] += ResScanlineDith[ j ];
ResScanlineDith[ j ] = 0.0;
}
for( j = 0; j < LenE - ElCount; j++ )
{
// Perform rounding, noise estimation and saturation.
const fptype z0 = round( ResScanline[ j ] / TrMul ) * TrMul;
const fptype Noise = ResScanline[ j ] - z0;
ResScanline[ j ] = clamp( z0, c0, PkOut );
ResScanline[ j + ElCount ] += Noise * (fptype) 0.364842;
ResScanlineDith[ j - ElCount ] += Noise * (fptype) 0.207305;
ResScanlineDith[ j ] += Noise * (fptype) 0.364842;
ResScanlineDith[ j + ElCount ] += Noise * (fptype) 0.063011;
}
while( j < LenE )
{
const fptype z0 = round( ResScanline[ j ] / TrMul ) * TrMul;
const fptype Noise = ResScanline[ j ] - z0;
ResScanline[ j ] = clamp( z0, c0, PkOut );
ResScanlineDith[ j - ElCount ] += Noise * (fptype) 0.207305;
ResScanlineDith[ j ] += Noise * (fptype) 0.364842;
j++;
}
}
protected:
using CImageResizerDithererDefINL< fptype > :: Len;
using CImageResizerDithererDefINL< fptype > :: Vars;
using CImageResizerDithererDefINL< fptype > :: LenE;
using CImageResizerDithererDefINL< fptype > :: TrMul0;
using CImageResizerDithererDefINL< fptype > :: PkOut0;
CBuffer< fptype > ResScanlineDith0; ///< Error diffusion buffer.
///<
fptype* ResScanlineDith; ///< Error diffusion buffer pointer which skips
///< the first ElCount elements.
///<
};
/**
* @brief Floating-point processing definition and abstraction class.
*
* This class defines several constants and typedefs that point to classes
* that should be used by the image resizing algorithm. Such "definition
* class" can be used to define alternative scanline processing algorithms
* (e.g. SIMD) and image scanline packing styles used during processing. This
* class also offers an abstraction layer for dithering, rounding and
* clamping (saturation) operation.
*
* The fpclass_def class can be used to define processing using both SIMD and
* non-SIMD types, but using algorithms that are operate on interleaved pixels
* and non-SIMD optimized themselves.
*
* @tparam afptype Floating point type to use for storing intermediate data
* and variables. For variables that are not used in intensive calculations
* the "double" type is always used. On the latest Intel processors (like
* i7-4770K) there is almost no performance difference between "double" and
* "float". Image quality differences between "double" and "float" are not
* apparent on 8-bit images. At the same time the "float" uses half amount of
* working memory the "double" type uses. SIMD types can be used. The
* functions round() and clamp() in the "avir" or other visible namespace
* should be available for the specified type. SIMD types allow to perform
* resizing of images with more than 4 channels, to be exact 4 * SIMD element
* number (e.g. 16 for float4), without modification of the image resizing
* algorithm required.
* @tparam afptypeatom The atomic type the "afptype" consists of.
* @tparam adith Ditherer class to use during processing.
*/
template< class afptype, class afptypeatom = afptype,
class adith = CImageResizerDithererDefINL< afptype > >
class fpclass_def
{
public:
typedef afptype fptype; ///< Floating-point type to use during processing.
///<
typedef afptypeatom fptypeatom; ///< Atomic type "fptype" consists of.
///<
static const int fppack = sizeof( fptype ) / sizeof( fptypeatom ); ///<
///< The number of atomic types stored in a single "fptype" element.
///<
static const int fpalign = sizeof( fptype ); ///< Suggested alignment size
///< in bytes. This is not a required alignment, because image
///< resizing algorithm cannot be made to have a strictly aligned data
///< access at all steps (e.g. interpolation cannot perform aligned
///< accesses).
///<
static const int elalign = 1; ///< Length alignment of arrays of elements.
///< This applies to filters and intermediate buffers: this constant
///< forces filters and scanlines to have a length which is a multiple
///< of this value, for more efficient SIMD implementation.
///<
static const int packmode = 0; ///< 0 if interleaved packing, 1 if
///< de-interleaved.
///<
typedef CImageResizerFilterStepINL< fptype, fptypeatom > CFilterStep; ///<
///< Filtering step class to use during processing.
///<
typedef adith CDitherer; ///< Ditherer class to use during processing.
///<
};
/**
* @brief Image resizer class.
*
* The object of this class can be used to resize 1-4 channel images to any
* required size. Resizing is performed by utilizing interpolated sinc
* fractional delay filters plus (if necessary) a cascade of built-in
* sinc function-based 2X upsampling or 2X downsampling stages, followed by a
* correction filtering.
*
* Object of this class can be allocated on stack.
*
* @tparam fpclass Floating-point processing definition class to use. See
* avir::fpclass_def for more details.
*/
template< class fpclass = fpclass_def< float > >
class CImageResizer
{
public:
/**
* Constructor initializes the resizer.
*
* @param aResBitDepth Required bit depth of resulting image (1-16). If
* integer value output is used (e.g. uint8_t), the bit depth also affects
* rounding: for example, if aResBitDepth=6 and "Tout" is uint8_t, the
* result will be rounded to 6 most significant bits (2 least significant
* bits truncated, with dithering applied).
* @param aSrcBitDepth Source image's real bit-depth. Set to 0 to use
* aResBitDepth.
* @param aParams Resizing algorithm's parameters to use. Leave out for
* default values. Can be useful when performing automatic optimization of
* parameters.
*/
CImageResizer( const int aResBitDepth = 8, const int aSrcBitDepth = 0,
const CImageResizerParams& aParams = CImageResizerParamsDef() )
: Params( aParams )
, ResBitDepth( aResBitDepth )
{
SrcBitDepth = ( aSrcBitDepth == 0 ? ResBitDepth : aSrcBitDepth );
initFilterBank( FixedFilterBank, 1.0, false, CFltBuffer() );
FixedFilterBank.createAllFilters();
}
/**
* Function resizes image.
*
* @param SrcBuf Source image buffer.
* @param SrcWidth Source image width.
* @param SrcHeight Source image height.
* @param SrcScanlineSize Physical size of source scanline in elements
* (not bytes). If this value is below 1, SrcWidth * ElCountIO will be
* used as the physical source scanline size.
* @param[out] NewBuf Buffer to accept the resized image. Can be equal to
* SrcBuf if the size of the resized image is smaller or equal to source
* image in size.
* @param NewWidth New image width.
* @param NewHeight New image height.
* @param ElCountIO The number of elements (channels) used to store each
* source and destination pixel (1-4).
* @param k Resizing step (one output pixel corresponds to "k" input
* pixels). A downsizing factor if > 1.0; upsizing factor if <= 1.0.
* Multiply by -1 if you would like to bypass "ox" and "oy" adjustment
* which is done by default to produce a centered image. If step value
* equals 0, the step value will be chosen automatically and independently
* for horizontal and vertical resizing.
* @param[in,out] aVars Pointer to variables structure to be passed to the
* image resizing function. Can be NULL. Only variables that are
* initialized in default constructor of this structure are accepted by
* this function. These variables will not be changed by this function.
* All other variables can be modified by this function. The access to
* this object is not thread-safe, each concurrent instance of this
* function should use a separate aVars object.
* @tparam Tin Input buffer element's type. Can be uint8_t (0-255 value
* range), uint16_t (0-65535 value range), float (0.0-1.0 value range),
* double (0.0-1.0 value range). Larger integer types are treated as
* uint16_t. Signed integer types are unsupported.
* @tparam Tout Output buffer element's type. Can be uint8_t (0-255 value
* range), uint16_t (0-65535 value range), float (0.0-1.0 value range),
* double (0.0-1.0 value range). Larger integer types are treated as
* uint16_t. Signed integer types are unsupported.
*/
template< class Tin, class Tout >
void resizeImage( const Tin* const SrcBuf, const int SrcWidth,
const int SrcHeight, int SrcScanlineSize, Tout* const NewBuf,
const int NewWidth, const int NewHeight, const int ElCountIO,
const double k, CImageResizerVars* const aVars = NULL ) const
{
if( SrcWidth == 0 || SrcHeight == 0 )
{
memset( NewBuf, 0, (size_t) NewWidth * NewHeight *
sizeof( Tout ));
return;
}
else
if( NewWidth == 0 || NewHeight == 0 )
{
return;
}
CImageResizerVars DefVars;
CImageResizerVars& Vars = ( aVars == NULL ? DefVars : *aVars );
CImageResizerThreadPool DefThreadPool;
CImageResizerThreadPool& ThreadPool = ( Vars.ThreadPool == NULL ?
DefThreadPool : *Vars.ThreadPool );
// Define resizing steps, also optionally modify offsets so that
// resizing produces a "centered" image.
double kx;
double ky;
double ox = Vars.ox;
double oy = Vars.oy;
if( k == 0.0 )
{
if( NewWidth > SrcWidth )
{
kx = (double) ( SrcWidth - 1 ) / ( NewWidth - 1 );
}
else
{
kx = (double) SrcWidth / NewWidth;
ox += ( kx - 1.0 ) * 0.5;
}
if( NewHeight > SrcHeight )
{
ky = (double) ( SrcHeight - 1 ) / ( NewHeight - 1 );
}
else
{
ky = (double) SrcHeight / NewHeight;
oy += ( ky - 1.0 ) * 0.5;
}
}
else
if( k > 0.0 )
{
kx = k;
ky = k;
if( k > 1.0 )
{
const double ko = ( k - 1.0 ) * 0.5;
ox += ko;
oy += ko;
}
}
else
{
kx = -k;
ky = -k;
}
// Evaluate pre-multipliers used on the output stage.
const bool IsInFloat = ( (Tin) 0.4 != 0 );
const bool IsOutFloat = ( (Tout) 0.4 != 0 );
double OutMul; // Output multiplier.
if( Vars.UseSRGBGamma )
{
if( IsInFloat )
{
Vars.InGammaMult = 1.0;
}
else
{
Vars.InGammaMult =
1.0 / ( sizeof( Tin ) == 1 ? 255.0 : 65535.0 );
}
if( IsOutFloat )
{
Vars.OutGammaMult = 1.0;
}
else
{
Vars.OutGammaMult = ( sizeof( Tout ) == 1 ? 255.0 : 65535.0 );
}
OutMul = 1.0;
}
else
{
if( IsOutFloat )
{
OutMul = 1.0;
}
else
{
OutMul = ( sizeof( Tout ) == 1 ? 255.0 : 65535.0 );
}
if( !IsInFloat )
{
OutMul /= ( sizeof( Tin ) == 1 ? 255.0 : 65535.0 );
}
}
// Fill widely-used variables.
const int ElCount = ( ElCountIO + fpclass :: fppack - 1 ) /
fpclass :: fppack;
const int NewWidthE = NewWidth * ElCount;
if( SrcScanlineSize < 1 )
{
SrcScanlineSize = SrcWidth * ElCountIO;
}
Vars.ElCount = ElCount;
Vars.ElCountIO = ElCountIO;
Vars.fppack = fpclass :: fppack;
Vars.fpalign = fpclass :: fpalign;
Vars.elalign = fpclass :: elalign;
Vars.packmode = fpclass :: packmode;
// Horizontal scanline filtering and resizing.
CDSPFracFilterBankLin< fptype > FltBank;
CFilterSteps FltSteps;
typename CFilterStep :: CRPosBufArray RPosBufArray;
CBuffer< uint8_t > UsedFracMap;
// Perform the filtering steps modeling at various modes, find the
// most efficient mode for both horizontal and vertical resizing.
int UseBuildMode = 1;
const int BuildModeCount =
( FixedFilterBank.getOrder() == 0 ? 4 : 2 );
int m;
if( Vars.BuildMode >= 0 )
{
UseBuildMode = Vars.BuildMode;
}
else
{
int BestScore = 0x7FFFFFFF;
for( m = 0; m < BuildModeCount; m++ )
{
CDSPFracFilterBankLin< fptype > TmpBank;
CFilterSteps TmpSteps;
Vars.k = kx;
Vars.o = ox;
buildFilterSteps( TmpSteps, Vars, TmpBank, OutMul, m, true );
updateFilterStepBuffers( TmpSteps, Vars, RPosBufArray,
SrcWidth, NewWidth );
fillUsedFracMap( TmpSteps[ Vars.ResizeStep ], UsedFracMap );
const int c = calcComplexity( TmpSteps, Vars, UsedFracMap,
SrcHeight );
if( c < BestScore )
{
UseBuildMode = m;
BestScore = c;
}
}
}
// Perform the actual filtering steps building.
Vars.k = kx;
Vars.o = ox;
buildFilterSteps( FltSteps, Vars, FltBank, OutMul, UseBuildMode,
false );
updateFilterStepBuffers( FltSteps, Vars, RPosBufArray, SrcWidth,
NewWidth );
updateBufLenAndRPosPtrs( FltSteps, Vars, NewWidth );
const int ThreadCount = ThreadPool.getSuggestedWorkloadCount();
// Includes the current thread.
CStructArray< CThreadData< Tin, Tout > > td;
td.setItemCount( ThreadCount );
int i;
for( i = 0; i < ThreadCount; i++ )
{
if( i > 0 )
{
ThreadPool.addWorkload( &td[ i ]);
}
td[ i ].init( i, ThreadCount, FltSteps, Vars );
td[ i ].initScanlineQueue( td[ i ].sopResizeH, SrcHeight,
SrcWidth );
}
CBuffer< fptype, size_t > FltBuf( (size_t) NewWidthE * SrcHeight,
fpclass :: fpalign ); // Temporary buffer that receives
// horizontally-filtered and resized image.
for( i = 0; i < SrcHeight; i++ )
{
td[ i % ThreadCount ].addScanlineToQueue(
(void*) &SrcBuf[ (size_t) i * SrcScanlineSize ],
&FltBuf[ (size_t) i * NewWidthE ]);
}
ThreadPool.startAllWorkloads();
td[ 0 ].processScanlineQueue();
ThreadPool.waitAllWorkloadsToFinish();
// Vertical scanline filtering and resizing, reuse previously defined
// filtering steps if possible.
const int PrevUseBuildMode = UseBuildMode;
if( Vars.BuildMode >= 0 )
{
UseBuildMode = Vars.BuildMode;
}
else
{
CImageResizerVars TmpVars( Vars );
int BestScore = 0x7FFFFFFF;
for( m = 0; m < BuildModeCount; m++ )
{
CDSPFracFilterBankLin< fptype > TmpBank;
TmpBank.copyInitParams( FltBank );
CFilterSteps TmpSteps;
TmpVars.k = ky;
TmpVars.o = oy;
buildFilterSteps( TmpSteps, TmpVars, TmpBank, 1.0, m, true );
updateFilterStepBuffers( TmpSteps, TmpVars, RPosBufArray,
SrcHeight, NewHeight );
fillUsedFracMap( TmpSteps[ TmpVars.ResizeStep ],
UsedFracMap );
const int c = calcComplexity( TmpSteps, TmpVars, UsedFracMap,
NewWidth );
if( c < BestScore )
{
UseBuildMode = m;
BestScore = c;
}
}
}
Vars.k = ky;
Vars.o = oy;
if( UseBuildMode == PrevUseBuildMode && ky == kx )
{
if( OutMul != 1.0 )
{
modifyCorrFilterDCGain( FltSteps, 1.0 / OutMul );
}
}
else
{
buildFilterSteps( FltSteps, Vars, FltBank, 1.0, UseBuildMode,
false );
}
updateFilterStepBuffers( FltSteps, Vars, RPosBufArray, SrcHeight,
NewHeight );
updateBufLenAndRPosPtrs( FltSteps, Vars, NewWidth );
if( IsOutFloat && sizeof( FltBuf[ 0 ]) == sizeof( Tout ) &&
fpclass :: packmode == 0 )
{
// In-place output.
for( i = 0; i < ThreadCount; i++ )
{
td[ i ].initScanlineQueue( td[ i ].sopResizeV, NewWidth,
SrcHeight, NewWidthE, NewWidthE );
}
for( i = 0; i < NewWidth; i++ )
{
td[ i % ThreadCount ].addScanlineToQueue(
&FltBuf[ (size_t) i * ElCount ],
(fptype*) &NewBuf[ (size_t) i * ElCount ]);
}
ThreadPool.startAllWorkloads();
td[ 0 ].processScanlineQueue();
ThreadPool.waitAllWorkloadsToFinish();
ThreadPool.removeAllWorkloads();
return;
}
CBuffer< fptype, size_t > ResBuf( (size_t) NewWidthE * NewHeight,
fpclass :: fpalign );
for( i = 0; i < ThreadCount; i++ )
{
td[ i ].initScanlineQueue( td[ i ].sopResizeV, NewWidth,
SrcHeight, NewWidthE, NewWidthE );
}
const int im = ( fpclass :: packmode == 0 ? ElCount : 1 );
for( i = 0; i < NewWidth; i++ )
{
td[ i % ThreadCount ].addScanlineToQueue(
&FltBuf[ (size_t) i * im ], &ResBuf[ (size_t) i * im ]);
}
ThreadPool.startAllWorkloads();
td[ 0 ].processScanlineQueue();
ThreadPool.waitAllWorkloadsToFinish();
if( IsOutFloat )
{
// Perform output, but skip dithering.
for( i = 0; i < ThreadCount; i++ )
{
td[ i ].initScanlineQueue( td[ i ].sopUnpackH,
NewHeight, NewWidth );
}
for( i = 0; i < NewHeight; i++ )
{
td[ i % ThreadCount ].addScanlineToQueue(
&ResBuf[ (size_t) i * NewWidthE ],
&NewBuf[ (size_t) i * NewWidth * ElCountIO ]);
}
ThreadPool.startAllWorkloads();
td[ 0 ].processScanlineQueue();
ThreadPool.waitAllWorkloadsToFinish();
ThreadPool.removeAllWorkloads();
return;
}
// Perform output with dithering (for integer output only).
int TruncBits; // The number of lower bits to truncate and dither.
int OutRange; // Output range.
if( sizeof( Tout ) == 1 )
{
TruncBits = 8 - ResBitDepth;
OutRange = 255;
}
else
{
TruncBits = 16 - ResBitDepth;
OutRange = 65535;
}
const double PkOut = OutRange;
const double TrMul = ( TruncBits > 0 ?
PkOut / ( OutRange >> TruncBits ) : 1.0 );
if( CDitherer :: isRecursive() )
{
td[ 0 ].getDitherer().init( NewWidth, Vars, TrMul, PkOut );
if( Vars.UseSRGBGamma )
{
for( i = 0; i < NewHeight; i++ )
{
fptype* const ResScanline =
&ResBuf[ (size_t) i * NewWidthE ];
CFilterStep :: applySRGBGamma( ResScanline, NewWidth,
Vars );
td[ 0 ].getDitherer().dither( ResScanline );
CFilterStep :: unpackScanline( ResScanline,
&NewBuf[ (size_t) i * NewWidth * ElCountIO ],
NewWidth, Vars );
}
}
else
{
for( i = 0; i < NewHeight; i++ )
{
fptype* const ResScanline =
&ResBuf[ (size_t) i * NewWidthE ];
td[ 0 ].getDitherer().dither( ResScanline );
CFilterStep :: unpackScanline( ResScanline,
&NewBuf[ (size_t) i * NewWidth * ElCountIO ],
NewWidth, Vars );
}
}
}
else
{
for( i = 0; i < ThreadCount; i++ )
{
td[ i ].initScanlineQueue( td[ i ].sopDitherAndUnpackH,
NewHeight, NewWidth );
td[ i ].getDitherer().init( NewWidth, Vars, TrMul, PkOut );
}
for( i = 0; i < NewHeight; i++ )
{
td[ i % ThreadCount ].addScanlineToQueue(
&ResBuf[ (size_t) i * NewWidthE ],
&NewBuf[ (size_t) i * NewWidth * ElCountIO ]);
}
ThreadPool.startAllWorkloads();
td[ 0 ].processScanlineQueue();
ThreadPool.waitAllWorkloadsToFinish();
}
ThreadPool.removeAllWorkloads();
}
private:
typedef typename fpclass :: fptype fptype; ///< Floating-point type to use
///< during processing.
///<
typedef typename fpclass :: CFilterStep CFilterStep; ///< Filtering step
///< class to use during processing.
///<
typedef typename fpclass :: CDitherer CDitherer; ///< Ditherer class to
///< use during processing.
///<
CImageResizerParams Params; ///< Algorithm's parameters currently in use.
///<
int SrcBitDepth; ///< Bit resolution of the source image.
///<
int ResBitDepth; ///< Bit resolution of the resulting image.
///<
CDSPFracFilterBankLin< fptype > FixedFilterBank; ///< Fractional delay
///< filter bank with fixed characteristics, mainly for upsizing
///< cases.
///<
/**
* @brief Filtering steps array.
*
* The object of this class stores filtering steps together.
*/
typedef CStructArray< CFilterStep > CFilterSteps;
/**
* Function initializes the filter bank in the specified resizing step
* according to the source and resulting image bit depths.
*
* @param FltBank Filter bank to initialize.
* @param CutoffMult Cutoff multiplier, 0 to 1. 1 corresponds to 0.5pi
* cutoff point.
* @param ForceHiOrder "True" if a high-order interpolation should be
* forced which requires considerably less resources for initialization.
* @param ExtFilter External filter to apply to interpolation filter.
*/
void initFilterBank( CDSPFracFilterBankLin< fptype >& FltBank,
const double CutoffMult, const bool ForceHiOrder,
const CFltBuffer& ExtFilter ) const
{
const int IntBitDepth = ( ResBitDepth > SrcBitDepth ? ResBitDepth :
SrcBitDepth );
const double SNR = -6.02 * ( IntBitDepth + 3 );
int UseOrder;
int FracCount; // The number of fractional delay filters sampled by
// the filter bank. This variable affects the signal-to-noise
// ratio at interpolation stage. Theoretically, at UseOrder==1,
// 8-bit image resizing requires 66.2 dB SNR or 11. 16-bit
// resizing requires 114.4 dB SNR or 150. At UseOrder=0 the
// required number of filters is exponentially higher.
if( ForceHiOrder || IntBitDepth > 8 )
{
UseOrder = 1; // -146 dB max
FracCount = (int) ceil( 0.23134052 * exp( -0.058062929 * SNR ));
}
else
{
UseOrder = 0; // -72 dB max
FracCount = (int) ceil( 0.33287686 * exp( -0.11334583 * SNR ));
}
if( FracCount < 2 )
{
FracCount = 2;
}
FltBank.init( FracCount, UseOrder, Params.IntFltLen / CutoffMult,
Params.IntFltCutoff * CutoffMult, Params.IntFltAlpha, ExtFilter,
fpclass :: fpalign, fpclass :: elalign );
}
/**
* Function allocates filter buffer taking "fpclass" alignments into
* account. The allocated buffer may be larger than the requested size: in
* this case the additional elements will be zeroed by this function.
*
* @param Flt Filter buffer.
* @param ReqCapacity The required filter buffer's capacity.
* @param IsModel "True" if filtering steps modeling is performed without
* actual filter allocation.
* @param FltExt If non-NULL this variable will receive the number of
* elements the filter was extended by.
*/
static void allocFilter( CBuffer< fptype >& Flt, const int ReqCapacity,
const bool IsModel = false, int* const FltExt = NULL )
{
int UseCapacity = ( ReqCapacity + fpclass :: elalign - 1 ) &
~( fpclass :: elalign - 1 );
int Ext = UseCapacity - ReqCapacity;
if( FltExt != NULL )
{
*FltExt = Ext;
}
if( IsModel )
{
Flt.forceCapacity( UseCapacity );
return;
}
Flt.alloc( UseCapacity, fpclass :: fpalign );
while( Ext > 0 )
{
Ext--;
Flt[ ReqCapacity + Ext ] = 0.0;
}
}
/**
* Function assigns filter parameters to the specified filtering step
* object.
*
* @param fs Filtering step to assign parameter to. This step cannot be
* the last step if ResampleFactor greater than 1 was specified.
* @param IsUpsample "True" if upsampling step. Should be set to "false"
* if FltCutoff is negative.
* @param ResampleFactor Resampling factor of this filter (>=1).
* @param FltCutoff Filter cutoff point. This value will be divided by the
* ResampleFactor if IsUpsample equals "true". If zero value was
* specified, the "half-band" predefined filter will be created. In this
* case the ResampleFactor will modify the filter cutoff point.
* @param DCGain DC gain to apply to the filter. Assigned to filtering
* step's DCGain variable.
* @param UseFltOrig "True" if the originally-designed filter should be
* left in filtering step's FltOrig buffer. Otherwise it will be freed.
* @param IsModel "True" if filtering steps modeling is performed without
* actual filter building.
*/
void assignFilterParams( CFilterStep& fs, const bool IsUpsample,
const int ResampleFactor, const double FltCutoff, const double DCGain,
const bool UseFltOrig, const bool IsModel ) const
{
double FltAlpha;
double Len2;
double Freq;
if( FltCutoff == 0.0 )
{
const double m = 2.0 / ResampleFactor;
FltAlpha = Params.HBFltAlpha;
Len2 = 0.5 * Params.HBFltLen / m;
Freq = AVIR_PI * Params.HBFltCutoff * m;
}
else
{
FltAlpha = Params.LPFltAlpha;
Len2 = 0.25 * Params.LPFltBaseLen / FltCutoff;
Freq = AVIR_PI * Params.LPFltCutoffMult * FltCutoff;
}
if( IsUpsample )
{
Len2 *= ResampleFactor;
Freq /= ResampleFactor;
fs.DCGain = DCGain * ResampleFactor;
}
else
{
fs.DCGain = DCGain;
}
fs.FltOrig.Len2 = Len2;
fs.FltOrig.Freq = Freq;
fs.FltOrig.Alpha = FltAlpha;
fs.FltOrig.DCGain = fs.DCGain;
CDSPPeakedCosineLPF w( Len2, Freq, FltAlpha );
fs.IsUpsample = IsUpsample;
fs.ResampleFactor = ResampleFactor;
fs.FltLatency = w.fl2;
int FltExt; // Filter's extension due to fpclass :: elalign.
if( IsModel )
{
allocFilter( fs.Flt, w.FilterLen, true, &FltExt );
if( UseFltOrig )
{
// Allocate a real buffer even in modeling mode since this
// filter may be copied by the filter bank.
fs.FltOrig.alloc( w.FilterLen );
memset( &fs.FltOrig[ 0 ], 0,
w.FilterLen * sizeof( fs.FltOrig[ 0 ]));
}
}
else
{
fs.FltOrig.alloc( w.FilterLen );
w.generateLPF( &fs.FltOrig[ 0 ], 1.0 );
optimizeFIRFilter( fs.FltOrig, fs.FltLatency );
normalizeFIRFilter( &fs.FltOrig[ 0 ], fs.FltOrig.getCapacity(),
fs.DCGain );
allocFilter( fs.Flt, fs.FltOrig.getCapacity(), false, &FltExt );
copyArray( &fs.FltOrig[ 0 ], &fs.Flt[ 0 ],
fs.FltOrig.getCapacity() );
if( !UseFltOrig )
{
fs.FltOrig.free();
}
}
if( IsUpsample )
{
int l = fs.Flt.getCapacity() - fs.FltLatency - ResampleFactor -
FltExt;
allocFilter( fs.PrefixDC, l, IsModel );
allocFilter( fs.SuffixDC, fs.FltLatency, IsModel );
if( IsModel )
{
return;
}
// Create prefix and suffix "tails" used during upsampling.
const fptype* ip = &fs.Flt[ fs.FltLatency + ResampleFactor ];
copyArray( ip, &fs.PrefixDC[ 0 ], l );
while( true )
{
ip += ResampleFactor;
l -= ResampleFactor;
if( l <= 0 )
{
break;
}
addArray( ip, &fs.PrefixDC[ 0 ], l );
}
l = fs.FltLatency;
fptype* op = &fs.SuffixDC[ 0 ];
copyArray( &fs.Flt[ 0 ], op, l );
while( true )
{
op += ResampleFactor;
l -= ResampleFactor;
if( l <= 0 )
{
break;
}
addArray( &fs.Flt[ 0 ], op, l );
}
}
else
if( !UseFltOrig )
{
fs.EdgePixelCount = fs.EdgePixelCountDef;
}
}
/**
* Function adds a correction filter that tries to achieve a linear
* frequency response at all frequencies. The actual resulting response
* may feature a slight damping of the highest frequencies since a
* suitably short correction filter cannot fix steep high-frequency
* damping.
*
* This function assumes that the resizing step is currently the last
* step, even if it was not inserted yet: this allows placement of the
* correction filter both before and after the resizing step.
*
* @param Steps Filtering steps.
* @param bw Resulting bandwidth relative to the original bandwidth (which
* is 1.0), usually 1/k. Should be <= 1.0.
* @param IsPreCorrection "True" if the filtering step was already created
* and it is first in the Steps array. "True" also adds edge pixels to
* reduce edge artifacts.
* @param IsModel "True" if filtering steps modeling is performed without
* actual filter building.
*/
void addCorrectionFilter( CFilterSteps& Steps, const double bw,
const bool IsPreCorrection, const bool IsModel ) const
{
CFilterStep& fs = ( IsPreCorrection ? Steps[ 0 ] : Steps.add() );
fs.IsUpsample = false;
fs.ResampleFactor = 1;
fs.DCGain = 1.0;
fs.EdgePixelCount = ( IsPreCorrection ? fs.EdgePixelCountDef : 0 );
if( IsModel )
{
allocFilter( fs.Flt, CDSPFIREQ :: calcFilterLength(
Params.CorrFltLen, fs.FltLatency ), true );
return;
}
const int BinCount = 65; // Frequency response bins to control.
const int BinCount1 = BinCount - 1;
double curbw = 1.0; // Bandwidth of the filter at the current step.
int i;
int j;
double re;
double im;
CBuffer< double > Bins( BinCount ); // Adjustment introduced by all
// steps at all frequencies of interest.
for( j = 0; j < BinCount; j++ )
{
Bins[ j ] = 1.0;
}
const int si = ( IsPreCorrection ? 1 : 0 );
for( i = si; i < Steps.getItemCount() - ( si ^ 1 ); i++ )
{
const CFilterStep& fs = Steps[ i ];
if( fs.IsUpsample )
{
curbw *= fs.ResampleFactor;
if( fs.FltOrig.getCapacity() > 0 )
{
continue;
}
}
const double dcg = 1.0 / fs.DCGain; // DC gain correction.
const fptype* Flt;
int FltLen;
if( fs.ResampleFactor == 0 )
{
Flt = fs.FltBank -> getFilter( 0 );
FltLen = fs.FltBank -> getFilterLen();
}
else
{
Flt = &fs.Flt[ 0 ];
FltLen = fs.Flt.getCapacity();
}
// Calculate frequency response adjustment introduced by the
// filter at this step, within the bounds of bandwidth of
// interest.
for( j = 0; j < BinCount; j++ )
{
const double th = AVIR_PI * bw / curbw * j / BinCount1;
calcFIRFilterResponse( Flt, FltLen, th, re, im );
Bins[ j ] /= sqrt( re * re + im * im ) * dcg;
}
if( !fs.IsUpsample && fs.ResampleFactor > 1 )
{
curbw /= fs.ResampleFactor;
}
}
// Calculate filter.
CDSPFIREQ EQ;
EQ.init( bw * 2.0, Params.CorrFltLen, BinCount, 0.0, bw, false,
Params.CorrFltAlpha );
fs.FltLatency = EQ.getFilterLatency();
CBuffer< double > Filter( EQ.getFilterLength() );
EQ.buildFilter( Bins, &Filter[ 0 ]);
normalizeFIRFilter( &Filter[ 0 ], Filter.getCapacity(), 1.0 );
optimizeFIRFilter( Filter, fs.FltLatency );
normalizeFIRFilter( &Filter[ 0 ], Filter.getCapacity(), 1.0 );
allocFilter( fs.Flt, Filter.getCapacity() );
copyArray( &Filter[ 0 ], &fs.Flt[ 0 ], Filter.getCapacity() );
// Print a theoretically achieved final frequency response at various
// feature sizes (from DC to 1 pixel). Values above 255 means features
// become brighter, values below 255 means features become dimmer.
/* const double sbw = ( bw > 1.0 ? 1.0 / bw : 1.0 );
for( j = 0; j < BinCount; j++ )
{
const double th = AVIR_PI * sbw * j / BinCount1;
calcFIRFilterResponse( &fs.Flt[ 0 ], fs.Flt.getCapacity(),
th, re, im );
printf( "%f\n", sqrt( re * re + im * im ) / Bins[ j ] * 255 );
}
printf( "***\n" );*/
}
/**
* Function adds a sharpening filter if image is being upsized. Such
* sharpening allows to spot interpolation filter's stop-band attenuation:
* if attenuation is too weak, a "dark grid" and other artifacts may
* become visible.
*
* It is assumed that 40 decibel stop-band attenuation should be
* considered a required minimum: this allows application of (deliberately
* strong) 64X sharpening without spotting any artifacts.
*
* @param Steps Filtering steps.
* @param bw Resulting bandwidth relative to the original bandwidth (which
* is 1.0), usually 1/k.
* @param IsModel "True" if filtering steps modeling is performed without
* actual filter building.
*/
static void addSharpenTest( CFilterSteps& Steps, const double bw,
const bool IsModel )
{
if( bw <= 1.0 )
{
return;
}
const double FltLen = 10.0 * bw;
CFilterStep& fs = Steps.add();
fs.IsUpsample = false;
fs.ResampleFactor = 1;
fs.DCGain = 1.0;
fs.EdgePixelCount = 0;
if( IsModel )
{
allocFilter( fs.Flt, CDSPFIREQ :: calcFilterLength( FltLen,
fs.FltLatency ), true );
return;
}
const int BinCount = 200;
CBuffer< double > Bins( BinCount );
int Thresh = (int) round( BinCount / bw * 1.75 );
if( Thresh > BinCount )
{
Thresh = BinCount;
}
int j;
for( j = 0; j < Thresh; j++ )
{
Bins[ j ] = 1.0;
}
for( j = Thresh; j < BinCount; j++ )
{
Bins[ j ] = 256.0;
}
CDSPFIREQ EQ;
EQ.init( bw * 2.0, FltLen, BinCount, 0.0, bw, false, 1.7 );
fs.FltLatency = EQ.getFilterLatency();
CBuffer< double > Filter( EQ.getFilterLength() );
EQ.buildFilter( Bins, &Filter[ 0 ]);
normalizeFIRFilter( &Filter[ 0 ], Filter.getCapacity(), 1.0 );
optimizeFIRFilter( Filter, fs.FltLatency );
normalizeFIRFilter( &Filter[ 0 ], Filter.getCapacity(), 1.0 );
allocFilter( fs.Flt, Filter.getCapacity() );
copyArray( &Filter[ 0 ], &fs.Flt[ 0 ], Filter.getCapacity() );
/* for( j = 0; j < BinCount; j++ )
{
const double th = AVIR_PI * j / ( BinCount - 1 );
double re;
double im;
calcFIRFilterResponse( &fs.Flt[ 0 ], fs.Flt.getCapacity(),
th, re, im );
printf( "%f\n", sqrt( re * re + im * im ));
}
printf( "***\n" );*/
}
/**
* Function builds sequence of filtering steps depending on the specified
* resizing coefficient. The last steps included are always the resizing
* step then (possibly) the correction step.
*
* @param Steps Array that receives filtering steps.
* @param[out] Vars Variables object.
* @param FltBank Filter bank to initialize and use.
* @param DCGain The overall DC gain to apply. This DC gain is applied to
* the first filtering step only (upsampling or filtering step).
* @param ModeFlags Build mode flags to use. This is a bitmap of switches
* that enable or disable certain algorithm features.
* @param IsModel "True" if filtering steps modeling is performed without
* the actual filter allocation and building.
*/
void buildFilterSteps( CFilterSteps& Steps, CImageResizerVars& Vars,
CDSPFracFilterBankLin< fptype >& FltBank, const double DCGain,
const int ModeFlags, const bool IsModel ) const
{
Steps.clear();
const bool DoFltAndIntCombo = (( ModeFlags & 1 ) != 0 ); // Do filter
// and interpolator combining.
const bool ForceHiOrderInt = (( ModeFlags & 2 ) != 0 ); // Force use
// of a higher-order interpolation.
const bool UseHalfband = (( ModeFlags & 4 ) != 0 ); // Use half-band
// filter.
const double bw = 1.0 / Vars.k; // Resulting bandwidth.
const int UpsampleFactor = ( (int) floor( Vars.k ) < 2 ? 2 : 1 );
double IntCutoffMult; // Interpolation filter cutoff multiplier.
CFilterStep* ReuseStep; // If not NULL, resizing step should use
// this step object instead of creating a new one.
CFilterStep* ExtFltStep; // Use FltOrig of this step as the external
// filter to applied to the interpolator.
bool IsPreCorrection; // "True" if the correction filter is applied
// first.
double FltCutoff; // Cutoff frequency of the first filtering step.
double corrbw; ///< Bandwidth at the correction step.
if( Vars.k <= 1.0 )
{
IsPreCorrection = true;
FltCutoff = 1.0;
corrbw = 1.0;
Steps.add();
}
else
{
IsPreCorrection = false;
FltCutoff = bw;
corrbw = bw;
}
// Add 1 upsampling or several downsampling filters.
if( UpsampleFactor > 1 )
{
CFilterStep& fs = Steps.add();
assignFilterParams( fs, true, UpsampleFactor, FltCutoff, DCGain,
DoFltAndIntCombo, IsModel );
IntCutoffMult = FltCutoff * 2.0 / UpsampleFactor;
ReuseStep = NULL;
ExtFltStep = ( DoFltAndIntCombo ? &fs : NULL );
}
else
{
int DownsampleFactor;
while( true )
{
DownsampleFactor = (int) floor( 0.5 / FltCutoff );
bool DoHBFltAdd;
if( DownsampleFactor > 16 )
{
// Add half-band filter unconditionally in order to keep
// filter lengths lower for more precise frequency
// response and less edge artifacts.
DoHBFltAdd = true;
DownsampleFactor = 16;
}
else
{
DoHBFltAdd = ( UseHalfband && DownsampleFactor > 1 );
}
if( DoHBFltAdd )
{
assignFilterParams( Steps.add(), false, DownsampleFactor,
0.0, 1.0, false, IsModel );
FltCutoff *= DownsampleFactor;
}
else
{
if( DownsampleFactor < 1 )
{
DownsampleFactor = 1;
}
break;
}
}
CFilterStep& fs = Steps.add();
assignFilterParams( fs, false, DownsampleFactor, FltCutoff,
DCGain, DoFltAndIntCombo, IsModel );
IntCutoffMult = FltCutoff / 0.5;
if( DoFltAndIntCombo )
{
ReuseStep = &fs;
ExtFltStep = &fs;
}
else
{
IntCutoffMult *= DownsampleFactor;
ReuseStep = NULL;
ExtFltStep = NULL;
}
}
// Insert resizing and correction steps.
CFilterStep& fs = ( ReuseStep == NULL ? Steps.add() : *ReuseStep );
Vars.ResizeStep = Steps.getItemCount() - 1;
fs.IsUpsample = false;
fs.ResampleFactor = 0;
fs.DCGain = ( ExtFltStep == NULL ? 1.0 : ExtFltStep -> DCGain );
initFilterBank( FltBank, IntCutoffMult, ForceHiOrderInt,
( ExtFltStep == NULL ? fs.FltOrig : ExtFltStep -> FltOrig ));
if( FltBank == FixedFilterBank )
{
fs.FltBank = (CDSPFracFilterBankLin< fptype >*) &FixedFilterBank;
}
else
{
fs.FltBank = &FltBank;
}
addCorrectionFilter( Steps, corrbw, IsPreCorrection, IsModel );
//addSharpenTest( Steps, bw, IsModel );
}
/**
* Function extends *this upsampling step so that it produces more
* upsampled pixels that cover the prefix and suffix needs of the next
* step. After the call to this function the InPrefix and InSuffix
* variables of the next step will be set to zero.
*
* @param fs Upsampling filtering step.
* @param NextStep The next step structure.
*/
static void extendUpsample( CFilterStep& fs, CFilterStep& NextStep )
{
fs.InPrefix = ( NextStep.InPrefix + fs.ResampleFactor - 1 ) /
fs.ResampleFactor;
fs.OutPrefix += fs.InPrefix * fs.ResampleFactor;
NextStep.InPrefix = 0;
fs.InSuffix = ( NextStep.InSuffix + fs.ResampleFactor - 1 ) /
fs.ResampleFactor;
fs.OutSuffix += fs.InSuffix * fs.ResampleFactor;
NextStep.InSuffix = 0;
}
/**
* Function fills resizing step's RPosBuf array, excluding the actual
* "ftp" pointers and "SrcOffs" offsets.
*
* This array should be cleared if the resizing step or offset were
* changed. Otherwise this function only fills the elements required to
* cover resizing step's OutLen.
*
* This function is called by the updateFilterStepBuffers() function.
*
* @param fs Resizing step.
* @param Vars Variables object.
*/
static void fillRPosBuf( CFilterStep& fs, const CImageResizerVars& Vars )
{
const int PrevLen = fs.RPosBuf -> getCapacity();
if( fs.OutLen > PrevLen )
{
fs.RPosBuf -> increaseCapacity( fs.OutLen );
}
typename CFilterStep :: CResizePos* rpos = &(*fs.RPosBuf)[ PrevLen ];
const int FracCount = fs.FltBank -> getFracCount();
const double o = Vars.o;
const double k = Vars.k;
int i;
for( i = PrevLen; i < fs.OutLen; i++ )
{
const double SrcPos = o + k * i;
const int SrcPosInt = (int) floor( SrcPos );
const double x = ( SrcPos - SrcPosInt ) * FracCount;
const int fti = (int) x;
rpos -> x = (typename fpclass :: fptypeatom) ( x - fti );
rpos -> fti = fti;
rpos -> SrcPosInt = SrcPosInt;
rpos++;
}
}
/**
* Function updates filtering step buffer lengths depending on the
* specified source and new scanline lengths. This function should be
* called after the buildFilterSteps() function.
*
* @param Steps Array that receives filtering steps.
* @param[out] Vars Variables object, will receive buffer size and length.
* This function expects "k" and "o" variable values that will be
* adjusted by this function.
* @param RPosBufArray Resizing position buffers array, used to obtain
* buffer to initialize and use (will be reused if it is already fully or
* partially filled).
* @param SrcLen Source scanline's length in pixels.
* @param NewLen New scanline's length in pixels.
*/
static void updateFilterStepBuffers( CFilterSteps& Steps,
CImageResizerVars& Vars,
typename CFilterStep :: CRPosBufArray& RPosBufArray, int SrcLen,
const int NewLen )
{
int upstep = -1;
int InBuf = 0;
int i;
for( i = 0; i < Steps.getItemCount(); i++ )
{
CFilterStep& fs = Steps[ i ];
fs.Vars = &Vars;
fs.InLen = SrcLen;
fs.InBuf = InBuf;
fs.OutBuf = ( InBuf + 1 ) & 1;
if( fs.IsUpsample )
{
upstep = i;
Vars.k *= fs.ResampleFactor;
Vars.o *= fs.ResampleFactor;
fs.InPrefix = 0;
fs.InSuffix = 0;
fs.OutLen = fs.InLen * fs.ResampleFactor;
fs.OutPrefix = fs.FltLatency;
fs.OutSuffix = fs.Flt.getCapacity() - fs.FltLatency -
fs.ResampleFactor;
int l0 = fs.OutPrefix + fs.OutLen + fs.OutSuffix;
int l = fs.InLen * fs.ResampleFactor +
fs.SuffixDC.getCapacity();
if( l > l0 )
{
fs.OutSuffix += l - l0;
}
l0 = fs.OutLen + fs.OutSuffix;
if( fs.PrefixDC.getCapacity() > l0 )
{
fs.OutSuffix += fs.PrefixDC.getCapacity() - l0;
}
}
else
if( fs.ResampleFactor == 0 )
{
const int FilterLenD2 = fs.FltBank -> getFilterLen() / 2;
const int FilterLenD21 = FilterLenD2 - 1;
const int ResizeLPix = (int) floor( Vars.o ) - FilterLenD21;
fs.InPrefix = ( ResizeLPix < 0 ? -ResizeLPix : 0 );
const int ResizeRPix = (int) floor( Vars.o +
( NewLen - 1 ) * Vars.k ) + FilterLenD2 + 1;
fs.InSuffix = ( ResizeRPix > fs.InLen ?
ResizeRPix - fs.InLen : 0 );
fs.OutLen = NewLen;
fs.RPosBuf = &RPosBufArray.getRPosBuf( Vars.k, Vars.o,
fs.FltBank -> getFracCount() );
fillRPosBuf( fs, Vars );
}
else
{
Vars.k /= fs.ResampleFactor;
Vars.o /= fs.ResampleFactor;
Vars.o += fs.EdgePixelCount;
fs.InPrefix = fs.FltLatency;
fs.InSuffix = fs.Flt.getCapacity() - fs.FltLatency - 1;
// Additionally extend OutLen to produce more precise edge
// pixels.
fs.OutLen = ( fs.InLen + fs.ResampleFactor - 1 ) /
fs.ResampleFactor + fs.EdgePixelCount;
fs.InSuffix += ( fs.OutLen - 1 ) * fs.ResampleFactor + 1 -
fs.InLen;
fs.InPrefix += fs.EdgePixelCount * fs.ResampleFactor;
fs.OutLen += fs.EdgePixelCount;
}
InBuf = fs.OutBuf;
SrcLen = fs.OutLen;
}
Steps[ Steps.getItemCount() - 1 ].OutBuf = 2;
if( upstep != -1 )
{
extendUpsample( Steps[ upstep ], Steps[ upstep + 1 ]);
}
}
/**
* Function calculates an optimal intermediate buffer length that will
* cover all needs of the specified filtering steps. This function should
* be called after the updateFilterStepBuffers() function.
*
* Function also updates resizing step's RPosBuf pointers to the filter
* bank and SrcOffs values.
*
* @param Steps Filtering steps.
* @param[out] Vars Variables object, will receive buffer size and length.
* @param ResElIncr Resulting (final) element increment, used to produce
* de-interleaved result. For horizontal processing this value is equal
* to last step's OutLen, for vertical processing this value is equal to
* resulting image's width.
*/
static void updateBufLenAndRPosPtrs( CFilterSteps& Steps,
CImageResizerVars& Vars, const int ResElIncr )
{
int MaxPrefix[ 2 ] = { 0, 0 };
int MaxLen[ 2 ] = { 0, 0 };
int i;
for( i = 0; i < Steps.getItemCount(); i++ )
{
CFilterStep& fs = Steps[ i ];
const int ib = fs.InBuf;
if( fs.InPrefix > MaxPrefix[ ib ])
{
MaxPrefix[ ib ] = fs.InPrefix;
}
int l = fs.InLen + fs.InSuffix;
if( l > MaxLen[ ib ])
{
MaxLen[ ib ] = l;
}
fs.InElIncr = fs.InPrefix + l;
if( fs.OutBuf == 2 )
{
break;
}
const int ob = fs.OutBuf;
if( fs.IsUpsample )
{
if( fs.OutPrefix > MaxPrefix[ ob ])
{
MaxPrefix[ ob ] = fs.OutPrefix;
}
l = fs.OutLen + fs.OutSuffix;
if( l > MaxLen[ ob ])
{
MaxLen[ ob ] = l;
}
}
else
{
if( fs.OutLen > MaxLen[ ob ])
{
MaxLen[ ob ] = fs.OutLen;
}
}
}
// Update OutElIncr values of all steps.
for( i = 0; i < Steps.getItemCount(); i++ )
{
CFilterStep& fs = Steps[ i ];
if( fs.OutBuf == 2 )
{
fs.OutElIncr = ResElIncr;
break;
}
CFilterStep& fs2 = Steps[ i + 1 ];
if( fs.IsUpsample )
{
fs.OutElIncr = fs.OutPrefix + fs.OutLen + fs.OutSuffix;
if( fs.OutElIncr > fs2.InElIncr )
{
fs2.InElIncr = fs.OutElIncr;
}
else
{
fs.OutElIncr = fs2.InElIncr;
}
}
else
{
fs.OutElIncr = fs2.InElIncr;
}
}
// Update temporary buffer's length.
for( i = 0; i < 2; i++ )
{
Vars.BufLen[ i ] = MaxPrefix[ i ] + MaxLen[ i ];
Vars.BufOffs[ i ] = MaxPrefix[ i ];
if( Vars.packmode == 0 )
{
Vars.BufOffs[ i ] *= Vars.ElCount;
}
Vars.BufLen[ i ] *= Vars.ElCount;
}
// Update RPosBuf pointers and SrcOffs.
CFilterStep& fs = Steps[ Vars.ResizeStep ];
typename CFilterStep :: CResizePos* rpos = &(*fs.RPosBuf)[ 0 ];
const int em = ( fpclass :: packmode == 0 ? Vars.ElCount : 1 );
const int FilterLenD21 = fs.FltBank -> getFilterLen() / 2 - 1;
for( i = 0; i < fs.OutLen; i++ )
{
rpos -> ftp = fs.FltBank -> getFilter( rpos -> fti );
rpos -> SrcOffs = ( rpos -> SrcPosInt - FilterLenD21 ) * em;
rpos++;
}
}
/**
* Function modifies the overall (DC) gain of the correction filter in the
* pre-built filtering steps array.
*
* @param Steps Filtering steps.
* @param m Multiplier to apply to the correction filter.
*/
void modifyCorrFilterDCGain( CFilterSteps& Steps, const double m ) const
{
CBuffer< fptype >* Flt;
const int z = Steps.getItemCount() - 1;
if( !Steps[ z ].IsUpsample && Steps[ z ].ResampleFactor == 1 )
{
Flt = &Steps[ z ].Flt;
}
else
{
Flt = &Steps[ 0 ].Flt;
}
int i;
for( i = 0; i < Flt -> getCapacity(); i++ )
{
(*Flt)[ i ] = (fptype) ( (double) (*Flt)[ i ] * m );
}
}
/**
* Function builds a map of used fractional delay filters based on the
* resizing positions buffer.
*
* @param fs Resizing step.
* @param[out] UsedFracMap Map of used fractional delay filters.
*/
static void fillUsedFracMap( const CFilterStep& fs,
CBuffer< uint8_t >& UsedFracMap )
{
const int FracCount = fs.FltBank -> getFracCount();
UsedFracMap.increaseCapacity( FracCount, false );
memset( &UsedFracMap[ 0 ], 0, FracCount * sizeof( UsedFracMap[ 0 ]));
typename CFilterStep :: CResizePos* rpos = &(*fs.RPosBuf)[ 0 ];
int i;
for( i = 0; i < fs.OutLen; i++ )
{
UsedFracMap[ rpos -> fti ] |= 1;
rpos++;
}
}
/**
* Function calculates the overall filtering steps complexity per
* scanline. Each complexity unit corresponds to a single multiply-add
* operation. Data copy and pointer math operations are not included in
* this calculation, it is assumed that they correlate to the multiply-add
* operations. Calculation also does not include final rounding, dithering
* and clamping operations since they cannot be optimized out anyway.
*
* Calculation of the CRPosBuf buffer is not included since it cannot be
* avoided.
*
* This function should be called after the updateFilterStepBuffers()
* function.
*
* @param Steps Filtering steps array.
* @param Vars Variables object.
* @param UsedFracMap The map of used fractional delay filters.
* @param ScanlineCount Scanline count.
*/
static int calcComplexity( const CFilterSteps& Steps,
const CImageResizerVars& Vars, const CBuffer< uint8_t >& UsedFracMap,
const int ScanlineCount )
{
int fcnum; // Filter complexity multiplier numerator.
int fcdenom; // Filter complexity multiplier denominator.
if( Vars.packmode != 0 )
{
fcnum = 1;
fcdenom = 1;
}
else
{
// In interleaved processing mode, filters require 1 less
// multiplication per 2 multiply-add instructions.
fcnum = 3;
fcdenom = 4;
}
int s = 0; // Complexity per one scanline.
int s2 = 0; // Complexity per all scanlines.
int i;
for( i = 0; i < Steps.getItemCount(); i++ )
{
const CFilterStep& fs = Steps[ i ];
s2 += 65 * fs.Flt.getCapacity(); // Filter creation complexity.
if( fs.IsUpsample )
{
if( fs.FltOrig.getCapacity() > 0 )
{
continue;
}
s += ( fs.Flt.getCapacity() *
( fs.InPrefix + fs.InLen + fs.InSuffix ) +
fs.SuffixDC.getCapacity() + fs.PrefixDC.getCapacity() ) *
Vars.ElCount;
}
else
if( fs.ResampleFactor == 0 )
{
s += fs.FltBank -> getFilterLen() *
( fs.FltBank -> getOrder() + Vars.ElCount ) * fs.OutLen;
s2 += fs.FltBank -> calcInitComplexity( UsedFracMap );
}
else
{
s += fs.Flt.getCapacity() * Vars.ElCount * fs.OutLen *
fcnum / fcdenom;
}
}
return( s + s2 / ScanlineCount );
}
/**
* @brief Thread-isolated data used for scanline processing.
*
* This structure holds data necessary for image's horizontal or vertical
* scanline processing, including scanline processing queue.
*
* @tparam Tin Source element data type. Intermediate buffers store data
* in floating point format.
* @tparam Tout Destination element data type. Intermediate buffers store
* data in floating point format.
*/
template< class Tin, class Tout >
class CThreadData : public CImageResizerThreadPool :: CWorkload
{
public:
virtual void process()
{
processScanlineQueue();
}
/**
* This enumeration lists possible scanline operations.
*/
enum EScanlineOperation
{
sopResizeH, ///< Resize horizontal scanline.
///<
sopResizeV, ///< Resize vertical scanline.
///<
sopDitherAndUnpackH, ///< Dither and unpack horizontal scanline.
///<
sopUnpackH ///< Unpack horizontal scanline.
///<
};
/**
* Function initializes *this thread data object and assigns certain
* variables provided by the higher level code.
*
* @param aThreadIndex Index of this thread data (0-based).
* @param aThreadCount Total number of threads used during processing.
* @param aSteps Filtering steps.
* @param aVars Image resizer variables.
*/
void init( const int aThreadIndex, const int aThreadCount,
const CFilterSteps& aSteps, const CImageResizerVars& aVars )
{
ThreadIndex = aThreadIndex;
ThreadCount = aThreadCount;
Steps = &aSteps;
Vars = &aVars;
}
/**
* Function initializes scanline processing queue, and updates
* capacities of intermediate buffers.
*
* @param aOp Operation to perform over scanline.
* @param TotalLines The total number of scanlines that will be
* processed by all threads.
* @param aSrcLen Source scanline length in pixels.
* @param aSrcIncr Source scanline buffer increment. Ignored in
* horizontal scanline processing.
* @param aResIncr Resulting scanline buffer increment. Ignored in
* horizontal scanline processing.
*/
void initScanlineQueue( const EScanlineOperation aOp,
const int TotalLines, const int aSrcLen, const int aSrcIncr = 0,
const int aResIncr = 0 )
{
const int l = Vars -> BufLen[ 0 ] + Vars -> BufLen[ 1 ];
if( Bufs.getCapacity() < l )
{
Bufs.alloc( l, fpclass :: fpalign );
}
BufPtrs[ 0 ] = Bufs + Vars -> BufOffs[ 0 ];
BufPtrs[ 1 ] = Bufs + Vars -> BufLen[ 0 ] + Vars -> BufOffs[ 1 ];
int j;
int ml = 0;
for( j = 0; j < Steps -> getItemCount(); j++ )
{
const CFilterStep& fs = (*Steps)[ j ];
if( fs.ResampleFactor == 0 &&
ml < fs.FltBank -> getFilterLen() )
{
ml = fs.FltBank -> getFilterLen();
}
}
TmpFltBuf.alloc( ml, fpclass :: fpalign );
ScanlineOp = aOp;
SrcLen = aSrcLen;
SrcIncr = aSrcIncr;
ResIncr = aResIncr;
QueueLen = 0;
Queue.increaseCapacity(( TotalLines + ThreadCount - 1 ) /
ThreadCount, false );
}
/**
* Function adds a scanline to the queue buffer. The
* initScanlineQueue() function should be called before calling this
* function. The number of calls to this add function should not
* exceed the TotalLines spread over all threads.
*
* @param SrcBuf Source scanline buffer.
* @param ResBuf Resulting scanline buffer.
*/
void addScanlineToQueue( void* const SrcBuf, void* const ResBuf )
{
Queue[ QueueLen ].SrcBuf = SrcBuf;
Queue[ QueueLen ].ResBuf = ResBuf;
QueueLen++;
}
/**
* Function processes all queued scanlines.
*/
void processScanlineQueue()
{
int i;
switch( ScanlineOp )
{
case sopResizeH:
{
for( i = 0; i < QueueLen; i++ )
{
resizeScanlineH( (Tin*) Queue[ i ].SrcBuf,
(fptype*) Queue[ i ].ResBuf );
}
break;
}
case sopResizeV:
{
for( i = 0; i < QueueLen; i++ )
{
resizeScanlineV( (fptype*) Queue[ i ].SrcBuf,
(fptype*) Queue[ i ].ResBuf );
}
break;
}
case sopDitherAndUnpackH:
{
if( Vars -> UseSRGBGamma )
{
for( i = 0; i < QueueLen; i++ )
{
CFilterStep :: applySRGBGamma(
(fptype*) Queue[ i ].SrcBuf, SrcLen, *Vars );
Ditherer.dither( (fptype*) Queue[ i ].SrcBuf );
CFilterStep :: unpackScanline(
(fptype*) Queue[ i ].SrcBuf,
(Tout*) Queue[ i ].ResBuf, SrcLen, *Vars );
}
}
else
{
for( i = 0; i < QueueLen; i++ )
{
Ditherer.dither( (fptype*) Queue[ i ].SrcBuf );
CFilterStep :: unpackScanline(
(fptype*) Queue[ i ].SrcBuf,
(Tout*) Queue[ i ].ResBuf, SrcLen, *Vars );
}
}
break;
}
case sopUnpackH:
{
if( Vars -> UseSRGBGamma )
{
for( i = 0; i < QueueLen; i++ )
{
CFilterStep :: applySRGBGamma(
(fptype*) Queue[ i ].SrcBuf, SrcLen, *Vars );
CFilterStep :: unpackScanline(
(fptype*) Queue[ i ].SrcBuf,
(Tout*) Queue[ i ].ResBuf, SrcLen, *Vars );
}
}
else
{
for( i = 0; i < QueueLen; i++ )
{
CFilterStep :: unpackScanline(
(fptype*) Queue[ i ].SrcBuf,
(Tout*) Queue[ i ].ResBuf, SrcLen, *Vars );
}
}
break;
}
}
}
/**
* Function returns ditherer object associated with *this thread data
* object.
*/
CDitherer& getDitherer()
{
return( Ditherer );
}
private:
int ThreadIndex; ///< Thread index.
///<
int ThreadCount; ///< Thread count.
///<
const CFilterSteps* Steps; ///< Filtering steps.
///<
const CImageResizerVars* Vars; ///< Image resizer variables.
///<
CBuffer< fptype > Bufs; ///< Flip-flop intermediate buffers.
///<
fptype* BufPtrs[ 3 ]; ///< Flip-flop buffer pointers (referenced by
///< filtering step's InBuf and OutBuf indices).
///<
CBuffer< fptype > TmpFltBuf; ///< Temporary buffer used in the
///< doResize() function, aligned by fpclass :: fpalign.
///<
EScanlineOperation ScanlineOp; ///< Operation to perform over
///< scanline.
///<
int SrcLen; ///< Source scanline length in the last queue.
///<
int SrcIncr; ///< Source scanline buffer increment in the last queue.
///<
int ResIncr; ///< Resulting scanline buffer increment in the last
///< queue.
///<
CDitherer Ditherer; ///< Ditherer object to use.
///<
/**
* @brief Scanline processing queue item.
*
* Scanline processing queue item.
*/
struct CQueueItem
{
void* SrcBuf; ///< Source scanline buffer, will by typecasted to
///< Tin or fptype*.
///<
void* ResBuf; ///< Resulting scanline buffer, will by typecasted
///< to Tout or fptype*.
///<
};
CBuffer< CQueueItem > Queue; ///< Scanline processing queue.
///<
int QueueLen; ///< Queue length.
///<
/**
* Function resizes a single horizontal scanline.
*
* @param SrcBuf Source scanline buffer. Can be either horizontal or
* vertical.
* @param ResBuf Resulting scanline buffer.
*/
void resizeScanlineH( const Tin* const SrcBuf, fptype* const ResBuf )
{
(*Steps)[ 0 ].packScanline( SrcBuf, BufPtrs[ 0 ], SrcLen );
BufPtrs[ 2 ] = ResBuf;
int j;
for( j = 0; j < Steps -> getItemCount(); j++ )
{
const CFilterStep& fs = (*Steps)[ j ];
fs.prepareInBuf( BufPtrs[ fs.InBuf ]);
const int DstIncr =
( Vars -> packmode == 0 ? Vars -> ElCount : 1 );
if( fs.ResampleFactor != 0 )
{
if( fs.IsUpsample )
{
fs.doUpsample( BufPtrs[ fs.InBuf ],
BufPtrs[ fs.OutBuf ]);
}
else
{
fs.doFilter( BufPtrs[ fs.InBuf ],
BufPtrs[ fs.OutBuf ], DstIncr );
}
}
else
{
fs.doResize( BufPtrs[ fs.InBuf ], BufPtrs[ fs.OutBuf ],
DstIncr, TmpFltBuf );
}
}
}
/**
* Function resizes a single vertical scanline.
*
* @param SrcBuf Source scanline buffer. Can be either horizontal or
* vertical.
* @param ResBuf Resulting scanline buffer.
*/
void resizeScanlineV( const fptype* const SrcBuf,
fptype* const ResBuf )
{
(*Steps)[ 0 ].convertVtoH( SrcBuf, BufPtrs[ 0 ], SrcLen,
SrcIncr );
BufPtrs[ 2 ] = ResBuf;
int j;
for( j = 0; j < Steps -> getItemCount(); j++ )
{
const CFilterStep& fs = (*Steps)[ j ];
fs.prepareInBuf( BufPtrs[ fs.InBuf ]);
const int DstIncr = ( fs.OutBuf == 2 ? ResIncr :
( Vars -> packmode == 0 ? Vars -> ElCount : 1 ));
if( fs.ResampleFactor != 0 )
{
if( fs.IsUpsample )
{
fs.doUpsample( BufPtrs[ fs.InBuf ],
BufPtrs[ fs.OutBuf ]);
}
else
{
fs.doFilter( BufPtrs[ fs.InBuf ],
BufPtrs[ fs.OutBuf ], DstIncr );
}
}
else
{
fs.doResize( BufPtrs[ fs.InBuf ], BufPtrs[ fs.OutBuf ],
DstIncr, TmpFltBuf );
}
}
}
};
};
#undef AVIR_PI
#undef AVIR_PId2
} // namespace avir
#endif // AVIR_CIMAGERESIZER_INCLUDED