/usr/include/vigra/multi_array.hxx is in libvigraimpex-dev 1.7.1+dfsg1-2ubuntu4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /************************************************************************/
/* */
/* Copyright 2003-2008 by Gunnar Kedenburg and Ullrich Koethe */
/* */
/* This file is part of the VIGRA computer vision library. */
/* The VIGRA Website is */
/* http://hci.iwr.uni-heidelberg.de/vigra/ */
/* Please direct questions, bug reports, and contributions to */
/* ullrich.koethe@iwr.uni-heidelberg.de or */
/* vigra@informatik.uni-hamburg.de */
/* */
/* 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. */
/* */
/************************************************************************/
#ifndef VIGRA_MULTI_ARRAY_HXX
#define VIGRA_MULTI_ARRAY_HXX
#include <memory>
#include <algorithm>
#include "accessor.hxx"
#include "tinyvector.hxx"
#include "rgbvalue.hxx"
#include "basicimageview.hxx"
#include "imageiterator.hxx"
#include "numerictraits.hxx"
#include "multi_iterator.hxx"
#include "metaprogramming.hxx"
#include "mathutil.hxx"
// Bounds checking Macro used if VIGRA_CHECK_BOUNDS is defined.
#ifdef VIGRA_CHECK_BOUNDS
#define VIGRA_ASSERT_INSIDE(diff) \
vigra_precondition(this->isInside(diff), "Index out of bounds")
#else
#define VIGRA_ASSERT_INSIDE(diff)
#endif
namespace vigra
{
namespace detail
{
/********************************************************/
/* */
/* defaultStride */
/* */
/********************************************************/
/* generates the stride for a gapless shape.
Namespace: vigra::detail
*/
template <unsigned int N>
inline TinyVector <MultiArrayIndex, N>
defaultStride(const TinyVector <MultiArrayIndex, N> &shape)
{
TinyVector <MultiArrayIndex, N> ret;
ret [0] = 1;
for (int i = 1; i < (int)N; ++i)
ret [i] = ret [i-1] * shape [i-1];
return ret;
}
/********************************************************/
/* */
/* ScanOrderToOffset */
/* */
/********************************************************/
/* transforms an index in scan order sense to a pointer offset in a possibly
strided, multi-dimensional array.
Namespace: vigra::detail
*/
template <int K>
struct ScanOrderToOffset
{
template <int N>
static MultiArrayIndex
exec(MultiArrayIndex d, const TinyVector <MultiArrayIndex, N> &shape,
const TinyVector <MultiArrayIndex, N> & stride)
{
return stride[N-K] * (d % shape[N-K]) +
ScanOrderToOffset<K-1>::exec(d / shape[N-K], shape, stride);
}
};
template <>
struct ScanOrderToOffset<1>
{
template <int N>
static MultiArrayIndex
exec(MultiArrayIndex d, const TinyVector <MultiArrayIndex, N> & /*shape*/,
const TinyVector <MultiArrayIndex, N> & stride)
{
return stride[N-1] * d;
}
};
template <int K>
struct ScanOrderToCoordinate
{
template <int N>
static void
exec(MultiArrayIndex d, const TinyVector <MultiArrayIndex, N> &shape,
TinyVector <MultiArrayIndex, N> & result)
{
result[N-K] = (d % shape[N-K]);
ScanOrderToCoordinate<K-1>::exec(d / shape[N-K], shape, result);
}
};
template <>
struct ScanOrderToCoordinate<1>
{
template <int N>
static void
exec(MultiArrayIndex d, const TinyVector <MultiArrayIndex, N> & /*shape*/,
TinyVector <MultiArrayIndex, N> & result)
{
result[N-1] = d;
}
};
template <int K>
struct CoordinateToScanOrder
{
template <int N>
static MultiArrayIndex
exec(const TinyVector <MultiArrayIndex, N> &shape,
const TinyVector <MultiArrayIndex, N> & coordinate)
{
return coordinate[N-K] + shape[N-K] * CoordinateToScanOrder<K-1>::exec(shape, coordinate);
}
};
template <>
struct CoordinateToScanOrder<1>
{
template <int N>
static MultiArrayIndex
exec(const TinyVector <MultiArrayIndex, N> & /*shape*/,
const TinyVector <MultiArrayIndex, N> & coordinate)
{
return coordinate[N-1];
}
};
template <class C>
struct CoordinatesToOffest
{
template <int N>
static MultiArrayIndex
exec(const TinyVector <MultiArrayIndex, N> & stride, MultiArrayIndex x)
{
return stride[0] * x;
}
template <int N>
static MultiArrayIndex
exec(const TinyVector <MultiArrayIndex, N> & stride, MultiArrayIndex x, MultiArrayIndex y)
{
return stride[0] * x + stride[1] * y;
}
};
template <>
struct CoordinatesToOffest<UnstridedArrayTag>
{
template <int N>
static MultiArrayIndex
exec(const TinyVector <MultiArrayIndex, N> & /*stride*/, MultiArrayIndex x)
{
return x;
}
template <int N>
static MultiArrayIndex
exec(const TinyVector <MultiArrayIndex, N> & stride, MultiArrayIndex x, MultiArrayIndex y)
{
return x + stride[1] * y;
}
};
/********************************************************/
/* */
/* MaybeStrided */
/* */
/********************************************************/
/* metatag implementing a test for marking MultiArrays that were
indexed at the zero'th dimension as strided, and all others as
unstrided.
<b>\#include</b>
\<<a href="multi__array_8hxx-source.html">vigra/multi_array.hxx</a>\>
Namespace: vigra::detail
*/
template <unsigned int N>
struct MaybeStrided
{
typedef UnstridedArrayTag type;
};
template <>
struct MaybeStrided <0>
{
typedef StridedArrayTag type;
};
/********************************************************/
/* */
/* MultiIteratorChooser */
/* */
/********************************************************/
/* metatag implementing a test (by pattern matching) for marking
MultiArrays that were indexed at the zero'th dimension as strided.
<b>\#include</b>
\<<a href="multi__array_8hxx-source.html">vigra/multi_array.hxx</a>\>
Namespace: vigra::detail
*/
template <class O>
struct MultiIteratorChooser
{
struct Nil {};
template <unsigned int N, class T, class REFERENCE, class POINTER>
struct Traverser
{
typedef Nil type;
};
};
/********************************************************/
/* */
/* MultiIteratorChooser <StridedArrayTag> */
/* */
/********************************************************/
/* specialization of the MultiIteratorChooser for strided arrays.
<b>\#include</b>
\<<a href="multi__array_8hxx-source.html">vigra/multi_array.hxx</a>\>
Namespace: vigra::detail
*/
template <>
struct MultiIteratorChooser <StridedArrayTag>
{
template <unsigned int N, class T, class REFERENCE, class POINTER>
struct Traverser
{
typedef StridedMultiIterator <N, T, REFERENCE, POINTER> type;
};
};
/********************************************************/
/* */
/* MultiIteratorChooser <UnstridedArrayTag> */
/* */
/********************************************************/
/* specialization of the MultiIteratorChooser for unstrided arrays.
<b>\#include</b>
\<<a href="multi__array_8hxx-source.html">vigra/multi_array.hxx</a>\>
Namespace: vigra::detail
*/
template <>
struct MultiIteratorChooser <UnstridedArrayTag>
{
template <unsigned int N, class T, class REFERENCE, class POINTER>
struct Traverser
{
typedef MultiIterator <N, T, REFERENCE, POINTER> type;
};
};
/********************************************************/
/* */
/* helper functions */
/* */
/********************************************************/
template <class DestIterator, class Shape, class T>
inline void
initMultiArrayData(DestIterator d, Shape const & shape, T const & init, MetaInt<0>)
{
DestIterator dend = d + shape[0];
for(; d < dend; ++d)
{
*d = init;
}
}
template <class DestIterator, class Shape, class T, int N>
void
initMultiArrayData(DestIterator d, Shape const & shape, T const & init, MetaInt<N>)
{
DestIterator dend = d + shape[N];
for(; d < dend; ++d)
{
initMultiArrayData(d.begin(), shape, init, MetaInt<N-1>());
}
}
#define VIGRA_COPY_MULTI_ARRAY_DATA(name, op) \
template <class SrcIterator, class Shape, class DestIterator> \
inline void \
name##MultiArrayData(SrcIterator s, Shape const & shape, DestIterator d, MetaInt<0>) \
{ \
SrcIterator send = s + shape[0]; \
for(; s < send; ++s, ++d) \
{ \
*d op detail::RequiresExplicitCast<typename DestIterator::value_type>::cast(*s); \
} \
} \
\
template <class SrcIterator, class Shape, class DestIterator, int N> \
void \
name##MultiArrayData(SrcIterator s, Shape const & shape, DestIterator d, MetaInt<N>) \
{ \
SrcIterator send = s + shape[N]; \
for(; s < send; ++s, ++d) \
{ \
name##MultiArrayData(s.begin(), shape, d.begin(), MetaInt<N-1>()); \
} \
} \
\
template <class DestIterator, class Shape, class T> \
inline void \
name##ScalarMultiArrayData(DestIterator d, Shape const & shape, T const & init, MetaInt<0>) \
{ \
DestIterator dend = d + shape[0]; \
for(; d < dend; ++d) \
{ \
*d op detail::RequiresExplicitCast<typename DestIterator::value_type>::cast(init); \
} \
} \
\
template <class DestIterator, class Shape, class T, int N> \
void \
name##ScalarMultiArrayData(DestIterator d, Shape const & shape, T const & init, MetaInt<N>) \
{ \
DestIterator dend = d + shape[N]; \
for(; d < dend; ++d) \
{ \
name##ScalarMultiArrayData(d.begin(), shape, init, MetaInt<N-1>()); \
} \
}
VIGRA_COPY_MULTI_ARRAY_DATA(copy, =)
VIGRA_COPY_MULTI_ARRAY_DATA(copyAdd, +=)
VIGRA_COPY_MULTI_ARRAY_DATA(copySub, -=)
VIGRA_COPY_MULTI_ARRAY_DATA(copyMul, *=)
VIGRA_COPY_MULTI_ARRAY_DATA(copyDiv, /=)
#undef VIGRA_COPY_MULTI_ARRAY_DATA
template <class SrcIterator, class Shape, class T, class ALLOC>
inline void
uninitializedCopyMultiArrayData(SrcIterator s, Shape const & shape, T * & d, ALLOC & a, MetaInt<0>)
{
SrcIterator send = s + shape[0];
for(; s < send; ++s, ++d)
{
a.construct(d, static_cast<T const &>(*s));
}
}
template <class SrcIterator, class Shape, class T, class ALLOC, int N>
void
uninitializedCopyMultiArrayData(SrcIterator s, Shape const & shape, T * & d, ALLOC & a, MetaInt<N>)
{
SrcIterator send = s + shape[N];
for(; s < send; ++s)
{
uninitializedCopyMultiArrayData(s.begin(), shape, d, a, MetaInt<N-1>());
}
}
template <class SrcIterator, class Shape, class T>
inline void
normMaxOfMultiArray(SrcIterator s, Shape const & shape, T & result, MetaInt<0>)
{
SrcIterator send = s + shape[0];
for(; s < send; ++s)
{
T v = norm(*s);
if(result < v)
result = v;
}
}
template <class SrcIterator, class Shape, class T, int N>
void
normMaxOfMultiArray(SrcIterator s, Shape const & shape, T & result, MetaInt<N>)
{
SrcIterator send = s + shape[N];
for(; s < send; ++s)
{
normMaxOfMultiArray(s.begin(), shape, result, MetaInt<N-1>());
}
}
template <class T>
struct MultiArrayL1Functor
{
template <class U>
T operator()(U t) const
{ return norm(t); }
};
template <class T>
struct MultiArrayL2Functor
{
template <class U>
T operator()(U t) const
{ return squaredNorm(t); }
};
template <class T>
struct MultiArrayScaledL2Functor
{
T scale;
MultiArrayScaledL2Functor(T s)
: scale(s)
{}
template <class U>
T operator()(U t) const
{ return squaredNorm(T(t) / scale); }
};
template <class SrcIterator, class Shape, class Functor, class T>
inline void
sumOverMultiArray(SrcIterator s, Shape const & shape, Functor f, T & result, MetaInt<0>)
{
SrcIterator send = s + shape[0];
for(; s < send; ++s)
{
result += f(*s);
}
}
template <class SrcIterator, class Shape, class Functor, class T, int N>
void
sumOverMultiArray(SrcIterator s, Shape const & shape, Functor f, T & result, MetaInt<N>)
{
SrcIterator send = s + shape[N];
for(; s < send; ++s)
{
sumOverMultiArray(s.begin(), shape, f, result, MetaInt<N-1>());
}
}
template <class SrcIterator, class Shape, class DestIterator>
inline bool
equalityOfMultiArrays(SrcIterator s, Shape const & shape, DestIterator d, MetaInt<0>)
{
SrcIterator send = s + shape[0];
for(; s < send; ++s, ++d)
{
if(!(*s == *d))
return false;
}
return true;
}
template <class SrcIterator, class Shape, class DestIterator, int N>
bool
equalityOfMultiArrays(SrcIterator s, Shape const & shape, DestIterator d, MetaInt<N>)
{
SrcIterator send = s + shape[N];
for(; s < send; ++s, ++d)
{
if(!equalityOfMultiArrays(s.begin(), shape, d.begin(), MetaInt<N-1>()))
return false;
}
return true;
}
template <class SrcIterator, class Shape, class DestIterator>
inline void
swapDataImpl(SrcIterator s, Shape const & shape, DestIterator d, MetaInt<0>)
{
SrcIterator send = s + shape[0];
for(; s < send; ++s, ++d)
std::swap(*s, *d);
}
template <class SrcIterator, class Shape, class DestIterator, int N>
void
swapDataImpl(SrcIterator s, Shape const & shape, DestIterator d, MetaInt<N>)
{
SrcIterator send = s + shape[N];
for(; s < send; ++s, ++d)
swapDataImpl(s.begin(), shape, d.begin(), MetaInt<N-1>());
}
} // namespace detail
/********************************************************/
/* */
/* MultiArrayView */
/* */
/********************************************************/
// forward declaration
template <unsigned int N, class T, class C = UnstridedArrayTag>
class MultiArrayView;
template <unsigned int N, class T, class A = std::allocator<T> >
class MultiArray;
/********************************************************/
/* */
/* NormTraits */
/* */
/********************************************************/
template <unsigned int N, class T, class C>
struct NormTraits<MultiArrayView<N, T, C> >
{
typedef MultiArrayView<N, T, C> Type;
typedef typename NormTraits<T>::SquaredNormType SquaredNormType;
typedef typename SquareRootTraits<SquaredNormType>::SquareRootResult NormType;
};
template <unsigned int N, class T, class A>
struct NormTraits<MultiArray<N, T, A> >
: public NormTraits<MultiArrayView<N, T, UnstridedArrayTag> >
{
typedef NormTraits<MultiArrayView<N, T, UnstridedArrayTag> > BaseType;
typedef MultiArray<N, T, A> Type;
typedef typename BaseType::SquaredNormType SquaredNormType;
typedef typename BaseType::NormType NormType;
};
/** \brief Base class for, and view to, \ref vigra::MultiArray.
This class implements the interface of both MultiArray and
MultiArrayView. By default, MultiArrayViews are tagged as
unstrided. If necessary, strided arrays are constructed automatically
by calls to a variant of the bind...() function.
If you want to apply an algorithm requiring an image to a
<tt>MultiArrayView</tt> of appropriate (2-dimensional) shape, you can
create a \ref vigra::BasicImageView that acts as a wrapper with the
necessary interface -- see \ref MultiArrayToImage.
The template parameter are as follows
\code
N: the array dimension
T: the type of the array elements
C: a tag determining whether the array's inner dimension is strided
or not. An array is unstrided if the array elements occupy consecutive
memory location, strided if there is an offset in between (e.g.
when a view is created that skips every other array element).
The compiler can generate faster code for unstrided arrays.
Possible values: UnstridedArrayTag (default), StridedArrayTag
\endcode
<b>\#include</b>
\<<a href="multi__array_8hxx-source.html">vigra/multi_array.hxx</a>\>
Namespace: vigra
*/
template <unsigned int N, class T, class C>
class MultiArrayView
{
public:
/** the array's actual dimensionality.
This ensures that MultiArrayView can also be used for
scalars (that is, when <tt>N == 0</tt>). Calculated as:<br>
\code
actual_dimension = (N==0) ? 1 : N
\endcode
*/
enum ActualDimension { actual_dimension = (N==0) ? 1 : N };
/** the array's value type
*/
typedef T value_type;
/** reference type (result of operator[])
*/
typedef value_type &reference;
/** const reference type (result of operator[] const)
*/
typedef const value_type &const_reference;
/** pointer type
*/
typedef value_type *pointer;
/** const pointer type
*/
typedef const value_type *const_pointer;
/** difference type (used for multi-dimensional offsets and indices)
*/
typedef typename MultiArrayShape<actual_dimension>::type difference_type;
/** size type
*/
typedef difference_type size_type;
/** difference and index type for a single dimension
*/
typedef MultiArrayIndex difference_type_1;
/** traverser (MultiIterator) type
*/
typedef typename vigra::detail::MultiIteratorChooser <
C>::template Traverser <actual_dimension, T, T &, T *>::type traverser;
/** const traverser (MultiIterator) type
*/
typedef typename vigra::detail::MultiIteratorChooser <
C>::template Traverser <actual_dimension, T, T const &, T const *>::type const_traverser;
/** the view type associated with this array.
*/
typedef MultiArrayView <N, T, C> view_type;
/** the matrix type associated with this array.
*/
typedef MultiArray <N, T> matrix_type;
protected:
typedef typename difference_type::value_type diff_zero_t;
/** the shape of the image pointed to is stored here.
*/
difference_type m_shape;
/** the strides (offset of a sample to the next) for every dimension
are stored here.
*/
difference_type m_stride;
/** pointer to the image.
*/
pointer m_ptr;
template <class U, class CN>
void copyImpl(const MultiArrayView <N, U, CN>& rhs);
template <class U, class CN>
void swapDataImpl(MultiArrayView <N, U, CN> rhs);
template <class CN>
bool arraysOverlap(const MultiArrayView <N, T, CN>& rhs) const;
template <class U, class CN>
bool arraysOverlap(const MultiArrayView <N, U, CN>&) const
{
return false;
}
public:
/** default constructor: create an invalid view,
* i.e. hasData() returns false and size() is zero.
*/
MultiArrayView ()
: m_shape (diff_zero_t(0)), m_stride (diff_zero_t(0)), m_ptr (0)
{}
/** construct from shape and pointer
*/
MultiArrayView (const difference_type &shape, pointer ptr)
: m_shape (shape),
m_stride (detail::defaultStride <MultiArrayView<N,T>::actual_dimension> (shape)),
m_ptr (ptr)
{}
/** Construct from shape, strides (offset of a sample to the
next) for every dimension, and pointer. (Note that
strides are not given in bytes, but in offsets of the
respective pointer type.)
*/
MultiArrayView (const difference_type &shape,
const difference_type &stride,
pointer ptr)
: m_shape (shape),
m_stride (stride),
m_ptr (ptr)
{}
/** Assignment. There are 3 cases:
<ul>
<li> When this <tt>MultiArrayView</tt> does not point to valid data
(e.g. after default construction), it becomes a copy of \a rhs.
<li> When the shapes of the two arrays match, the array contents are copied.
<li> Otherwise, a <tt>PreconditionViolation</tt> exception is thrown.
</ul>
*/
MultiArrayView & operator=(MultiArrayView const & rhs);
/** Assignment of a differently typed MultiArrayView. Fails with
<tt>PreconditionViolation</tt> exception when the shapes do not match.
*/
template<class U, class C1>
MultiArrayView & operator=(MultiArrayView<N, U, C1> const & rhs)
{
vigra_precondition(this->shape() == rhs.shape(),
"MultiArrayView::operator=() size mismatch.");
this->copyImpl(rhs);
return *this;
}
/** Add-assignment of a compatible MultiArrayView. Fails with
<tt>PreconditionViolation</tt> exception when the shapes do not match.
*/
template<class U, class C1>
MultiArrayView & operator+=(MultiArrayView<N, U, C1> const & rhs);
/** Subtract-assignment of a compatible MultiArrayView. Fails with
<tt>PreconditionViolation</tt> exception when the shapes do not match.
*/
template<class U, class C1>
MultiArrayView & operator-=(MultiArrayView<N, U, C1> const & rhs);
/** Multiply-assignment of a compatible MultiArrayView. Fails with
<tt>PreconditionViolation</tt> exception when the shapes do not match.
*/
template<class U, class C1>
MultiArrayView & operator*=(MultiArrayView<N, U, C1> const & rhs);
/** Divide-assignment of a compatible MultiArrayView. Fails with
<tt>PreconditionViolation</tt> exception when the shapes do not match.
*/
template<class U, class C1>
MultiArrayView & operator/=(MultiArrayView<N, U, C1> const & rhs);
/** Add-assignment of a scalar.
*/
MultiArrayView & operator+=(T const & rhs)
{
detail::copyAddScalarMultiArrayData(traverser_begin(), shape(), rhs, MetaInt<actual_dimension-1>());
return *this;
}
/** Subtract-assignment of a scalar.
*/
MultiArrayView & operator-=(T const & rhs)
{
detail::copySubScalarMultiArrayData(traverser_begin(), shape(), rhs, MetaInt<actual_dimension-1>());
return *this;
}
/** Multiply-assignment of a scalar.
*/
MultiArrayView & operator*=(T const & rhs)
{
detail::copyMulScalarMultiArrayData(traverser_begin(), shape(), rhs, MetaInt<actual_dimension-1>());
return *this;
}
/** Divide-assignment of a scalar.
*/
MultiArrayView & operator/=(T const & rhs)
{
detail::copyDivScalarMultiArrayData(traverser_begin(), shape(), rhs, MetaInt<actual_dimension-1>());
return *this;
}
/** array access.
*/
reference operator[] (const difference_type &d)
{
VIGRA_ASSERT_INSIDE(d);
return m_ptr [dot (d, m_stride)];
}
/** array access.
*/
const_reference operator[] (const difference_type &d) const
{
VIGRA_ASSERT_INSIDE(d);
return m_ptr [dot (d, m_stride)];
}
/** equvalent to bindInner(), when M < N.
*/
template <unsigned int M>
MultiArrayView <N-M, T, StridedArrayTag> operator[] (const TinyVector<MultiArrayIndex, M> &d) const
{
return bindInner(d);
}
/** Array access in scan-order sense.
Mostly useful to support standard indexing for 1-dimensional multi-arrays,
but works for any N. Use scanOrderIndexToCoordinate() and
coordinateToScanOrderIndex() for conversion between indices and coordinates.
<b>Note:</b> This function should not be used in the inner loop, because the
conversion of the scan order index into a memory address is expensive
(it must take into account that memory may not be consecutive for subarrays
and/or strided arrays). Always prefer operator() if possible.
*/
reference operator[](difference_type_1 d)
{
VIGRA_ASSERT_INSIDE(scanOrderIndexToCoordinate(d));
return m_ptr [detail::ScanOrderToOffset<actual_dimension>::exec(d, m_shape, m_stride)];
}
/** Array access in scan-order sense.
Mostly useful to support standard indexing for 1-dimensional multi-arrays,
but works for any N. Use scanOrderIndexToCoordinate() and
coordinateToScanOrderIndex() for conversion between indices and coordinates.
<b>Note:</b> This function should not be used in the inner loop, because the
conversion of the scan order index into a memory address is expensive
(it must take into account that memory may not be consecutive for subarrays
and/or strided arrays). Always prefer operator() if possible.
*/
const_reference operator[](difference_type_1 d) const
{
VIGRA_ASSERT_INSIDE(scanOrderIndexToCoordinate(d));
return m_ptr [detail::ScanOrderToOffset<actual_dimension>::exec(d, m_shape, m_stride)];
}
/** convert scan-order index to coordinate.
*/
difference_type scanOrderIndexToCoordinate(difference_type_1 d) const
{
difference_type result;
detail::ScanOrderToCoordinate<actual_dimension>::exec(d, m_shape, result);
return result;
}
/** convert coordinate to scan-order index.
*/
difference_type_1 coordinateToScanOrderIndex(const difference_type &d) const
{
return detail::CoordinateToScanOrder<actual_dimension>::exec(m_shape, d);
}
/** 1D array access. Use only if N == 1.
*/
reference operator() (difference_type_1 x)
{
VIGRA_ASSERT_INSIDE(difference_type(x));
return m_ptr [detail::CoordinatesToOffest<C>::exec(m_stride, x)];
}
/** 2D array access. Use only if N == 2.
*/
reference operator() (difference_type_1 x, difference_type_1 y)
{
VIGRA_ASSERT_INSIDE(difference_type(x, y));
return m_ptr [detail::CoordinatesToOffest<C>::exec(m_stride, x, y)];
}
/** 3D array access. Use only if N == 3.
*/
reference operator() (difference_type_1 x, difference_type_1 y, difference_type_1 z)
{
VIGRA_ASSERT_INSIDE(difference_type(x, y, z));
return m_ptr [m_stride[0]*x + m_stride[1]*y + m_stride[2]*z];
}
/** 4D array access. Use only if N == 4.
*/
reference operator() (difference_type_1 x, difference_type_1 y,
difference_type_1 z, difference_type_1 u)
{
VIGRA_ASSERT_INSIDE(difference_type(x, y, z, u));
return m_ptr [m_stride[0]*x + m_stride[1]*y + m_stride[2]*z + m_stride[3]*u];
}
/** 5D array access. Use only if N == 5.
*/
reference operator() (difference_type_1 x, difference_type_1 y, difference_type_1 z,
difference_type_1 u, difference_type_1 v)
{
VIGRA_ASSERT_INSIDE(difference_type(x, y,z, u,v));
return m_ptr [m_stride[0]*x + m_stride[1]*y + m_stride[2]*z + m_stride[3]*u + m_stride[4]*v];
}
/** 1D const array access. Use only if N == 1.
*/
const_reference operator() (difference_type_1 x) const
{
VIGRA_ASSERT_INSIDE(difference_type(x));
return m_ptr [detail::CoordinatesToOffest<C>::exec(m_stride, x)];
}
/** 2D const array access. Use only if N == 2.
*/
const_reference operator() (difference_type_1 x, difference_type_1 y) const
{
VIGRA_ASSERT_INSIDE(difference_type(x, y));
return m_ptr [detail::CoordinatesToOffest<C>::exec(m_stride, x, y)];
}
/** 3D const array access. Use only if N == 3.
*/
const_reference operator() (difference_type_1 x, difference_type_1 y, difference_type_1 z) const
{
VIGRA_ASSERT_INSIDE(difference_type(x,y,z));
return m_ptr [m_stride[0]*x + m_stride[1]*y + m_stride[2]*z];
}
/** 4D const array access. Use only if N == 4.
*/
const_reference operator() (difference_type_1 x, difference_type_1 y,
difference_type_1 z, difference_type_1 u) const
{
VIGRA_ASSERT_INSIDE(difference_type(x,y,z,u));
return m_ptr [m_stride[0]*x + m_stride[1]*y + m_stride[2]*z + m_stride[3]*u];
}
/** 5D const array access. Use only if N == 5.
*/
const_reference operator() (difference_type_1 x, difference_type_1 y, difference_type_1 z,
difference_type_1 u, difference_type_1 v) const
{
VIGRA_ASSERT_INSIDE(difference_type(x,y,z,u,v));
return m_ptr [m_stride[0]*x + m_stride[1]*y + m_stride[2]*z + m_stride[3]*u + m_stride[4]*v];
}
/** Init with a constant.
*/
template <class U>
MultiArrayView & init(const U & init)
{
detail::copyScalarMultiArrayData(traverser_begin(), shape(), init, MetaInt<actual_dimension-1>());
return *this;
}
/** Copy the data of the right-hand array (array shapes must match).
*/
void copy(const MultiArrayView & rhs)
{
if(this == &rhs)
return;
this->copyImpl(rhs);
}
/** Copy the data of the right-hand array (array shapes must match).
*/
template <class U, class CN>
void copy(const MultiArrayView <N, U, CN>& rhs)
{
this->copyImpl(rhs);
}
/** swap the data between two MultiArrayView objects.
The shapes of the two array must match.
*/
void swapData(MultiArrayView rhs)
{
if(this != &rhs)
swapDataImpl(rhs);
}
/** swap the data between two MultiArrayView objects.
The shapes of the two array must match.
*/
template <class T2, class C2>
void swapData(MultiArrayView <N, T2, C2> rhs)
{
swapDataImpl(rhs);
}
/** bind the M outmost dimensions to certain indices.
this reduces the dimensionality of the image to
max { 1, N-M }.
<b>Usage:</b>
\code
// create a 3D array of size 40x30x20
typedef MultiArray<3, double>::difference_type Shape;
MultiArray<3, double> array3(Shape(40, 30, 20));
// get a 1D array by fixing index 1 to 12, and index 2 to 10
MultiArrayView <1, double> array1 = array3.bindOuter(TinyVector<MultiArrayIndex, 2>(12, 10));
\endcode
*/
template <unsigned int M>
MultiArrayView <N-M, T, C> bindOuter (const TinyVector <MultiArrayIndex, M> &d) const;
/** bind the M innermost dimensions to certain indices.
this reduces the dimensionality of the image to
max { 1, N-M }.
<b>Usage:</b>
\code
// create a 3D array of size 40x30x20
typedef MultiArray<3, double>::difference_type Shape;
MultiArray<3, double> array3(Shape(40, 30, 20));
// get a 1D array by fixing index 0 to 12, and index 1 to 10
MultiArrayView <1, double, StridedArrayTag> array1 = array3.bindInner(TinyVector<MultiArrayIndex, 2>(12, 10));
\endcode
*/
template <unsigned int M>
MultiArrayView <N-M, T, StridedArrayTag>
bindInner (const TinyVector <MultiArrayIndex, M> &d) const;
/** bind dimension M to index d.
this reduces the dimensionality of the image to
max { 1, N-1 }.
<b>Usage:</b>
\code
// create a 3D array of size 40x30x20
typedef MultiArray<3, double>::difference_type Shape;
MultiArray<3, double> array3(Shape(40, 30, 20));
// get a 2D array by fixing index 1 to 12
MultiArrayView <2, double> array2 = array3.bind<1>(12);
// get a 2D array by fixing index 0 to 23
MultiArrayView <2, double, StridedArrayTag> array2a = array3.bind<0>(23);
\endcode
*/
template <unsigned int M>
MultiArrayView <N-1, T, typename vigra::detail::MaybeStrided <M>::type >
bind (difference_type_1 d) const;
/** bind the outmost dimension to a certain index.
this reduces the dimensionality of the image to
max { 1, N-1 }.
<b>Usage:</b>
\code
// create a 3D array of size 40x30x20
typedef MultiArray<3, double>::difference_type Shape;
MultiArray<3, double> array3(Shape(40, 30, 20));
// get a 2D array by fixing the outermost index (i.e. index 2) to 12
MultiArrayView <2, double> array2 = array3.bindOuter(12);
\endcode
*/
MultiArrayView <N-1, T, C> bindOuter (difference_type_1 d) const;
/** bind the innermost dimension to a certain index.
this reduces the dimensionality of the image to
max { 1, N-1 }.
<b>Usage:</b>
\code
// create a 3D array of size 40x30x20
typedef MultiArray<3, double>::difference_type Shape;
MultiArray<3, double> array3(Shape(40, 30, 20));
// get a 2D array by fixing the innermost index (i.e. index 0) to 23
MultiArrayView <2, double, StridedArrayTag> array2 = array3.bindInner(23);
\endcode
*/
MultiArrayView <N-1, T, StridedArrayTag> bindInner (difference_type_1 d) const;
/** bind dimension m to index d.
this reduces the dimensionality of the image to
max { 1, N-1 }.
<b>Usage:</b>
\code
// create a 3D array of size 40x30x20
typedef MultiArray<3, double>::difference_type Shape;
MultiArray<3, double> array3(Shape(40, 30, 20));
// get a 2D array by fixing index 2 to 15
MultiArrayView <2, double, StridedArrayTag> array2 = array3.bindAt(2, 15);
\endcode
*/
MultiArrayView <N-1, T, StridedArrayTag>
bindAt (difference_type_1 m, difference_type_1 d) const;
/** Add a singleton dimension (dimension of legth 1).
Singleton dimensions don't change the size of the data, but introduce
a new index that can only take the value 0. This is mainly useful for
the 'reduce mode' of transformMultiArray() and combineTwoMultiArrays(),
because these functions require the source and destination arrays to
have the same number of dimensions.
The range of \a i must be <tt>0 <= i <= N</tt>. The new dimension will become
the i'th index, and the old indices from i upwards will shift one
place to the right.
<b>Usage:</b>
Suppose we want have a 2D array and want to create a 1D array that contains
the row average of the first array.
\code
typedef MultiArrayShape<2>::type Shape2;
MultiArray<2, double> original(Shape2(40, 30));
typedef MultiArrayShape<1>::type Shape1;
MultiArray<1, double> rowAverages(Shape1(30));
// temporarily add a singleton dimension to the destination array
transformMultiArray(srcMultiArrayRange(original),
destMultiArrayRange(rowAverages.insertSingletonDimension(0)),
FindAverage<double>());
\endcode
*/
MultiArrayView <N+1, T, C>
insertSingletonDimension (difference_type_1 i) const;
/** create a rectangular subarray that spans between the
points p and q, where p is in the subarray, q not.
<b>Usage:</b>
\code
// create a 3D array of size 40x30x20
typedef MultiArray<3, double>::difference_type Shape;
MultiArray<3, double> array3(Shape(40, 30, 20));
// get a subarray set is smaller by one element at all sides
MultiArrayView <3, double> subarray = array3.subarray(Shape(1,1,1), Shape(39, 29, 19));
\endcode
*/
MultiArrayView subarray (const difference_type &p,
const difference_type &q) const
{
const difference_type_1 offset = dot (m_stride, p);
return MultiArrayView (q - p, m_stride, m_ptr + offset);
}
/** apply an additional striding to the image, thereby reducing
the shape of the array.
for example, multiplying the stride of dimension one by three
turns an appropriately layed out (interleaved) rgb image into
a single band image.
*/
MultiArrayView <N, T, StridedArrayTag>
stridearray (const difference_type &s) const
{
difference_type shape = m_shape;
for (unsigned int i = 0; i < actual_dimension; ++i)
shape [i] /= s [i];
return MultiArrayView <N, T, StridedArrayTag>(shape, m_stride * s, m_ptr);
}
/** Transpose an array. If N==2, this implements the usual matrix transposition.
For N > 2, it reverses the order of the indices.
<b>Usage:</b><br>
\code
typedef MultiArray<2, double>::difference_type Shape;
MultiArray<2, double> array(10, 20);
MultiArray<2, double, StridedArrayTag> transposed = array.transpose();
for(int i=0; i<array.shape(0), ++i)
for(int j=0; j<array.shape(1); ++j)
assert(array(i, j) == transposed(j, i));
\endcode
*/
MultiArrayView <N, T, StridedArrayTag>
transpose () const
{
difference_type shape(m_shape.begin(), difference_type::ReverseCopy),
stride(m_stride.begin(), difference_type::ReverseCopy);
return MultiArrayView <N, T, StridedArrayTag>(shape, stride, m_ptr);
}
/** permute the dimensions of the array.
The function exchanges the meaning of the dimensions without copying the data.
In case of a 2-dimensional array, this is simply array transposition. In higher dimensions,
there are more possibilities.
<b>Usage:</b><br>
\code
typedef MultiArray<2, double>::difference_type Shape;
MultiArray<2, double> array(10, 20);
MultiArray<2, double, StridedArrayTag> transposed = array.permuteDimensions(Shape(1,0));
for(int i=0; i<array.shape(0), ++i)
for(int j=0; j<array.shape(1); ++j)
assert(array(i, j) == transposed(j, i));
\endcode
*/
MultiArrayView <N, T, StridedArrayTag>
permuteDimensions (const difference_type &s) const;
/** Permute the dimensions of the array so that the strides are in ascending order.
Determines the appropriate permutation and then calls permuteDimensions().
*/
MultiArrayView <N, T, StridedArrayTag>
permuteStridesAscending() const;
/** Permute the dimensions of the array so that the strides are in descending order.
Determines the appropriate permutation and then calls permuteDimensions().
*/
MultiArrayView <N, T, StridedArrayTag>
permuteStridesDescending() const;
/** Compute the ordering of the strides in this array.
The result is describes the current permutation of the axes relative
to the standard ascending stride order.
*/
difference_type strideOrdering() const
{
return strideOrdering(m_stride);
}
/** Compute the ordering of the given strides.
The result is describes the current permutation of the axes relative
to the standard ascending stride order.
*/
static difference_type strideOrdering(difference_type strides);
/** number of the elements in the array.
*/
difference_type_1 elementCount () const
{
difference_type_1 ret = m_shape[0];
for(int i = 1; i < actual_dimension; ++i)
ret *= m_shape[i];
return ret;
}
/** number of the elements in the array.
Same as <tt>elementCount()</tt>. Mostly useful to support the std::vector interface.
*/
difference_type_1 size () const
{
return elementCount();
}
/** return the array's shape.
*/
const difference_type & shape () const
{
return m_shape;
}
/** return the array's size at a certain dimension.
*/
difference_type_1 size (difference_type_1 n) const
{
return m_shape [n];
}
/** return the array's shape at a certain dimension
(same as <tt>size(n)</tt>).
*/
difference_type_1 shape (difference_type_1 n) const
{
return m_shape [n];
}
/** return the array's stride for every dimension.
*/
const difference_type & stride () const
{
return m_stride;
}
/** return the array's stride at a certain dimension.
*/
difference_type_1 stride (int n) const
{
return m_stride [n];
}
/** check whether two arrays are elementwise equal.
*/
template <class U, class C1>
bool operator==(MultiArrayView<N, U, C1> const & rhs) const
{
if(this->shape() != rhs.shape())
return false;
return detail::equalityOfMultiArrays(traverser_begin(), shape(), rhs.traverser_begin(), MetaInt<actual_dimension-1>());
}
/** check whether two arrays are not elementwise equal.
Also true when the two arrays have different shapes.
*/
template <class U, class C1>
bool operator!=(MultiArrayView<N, U, C1> const & rhs) const
{
return !operator==(rhs);
}
/** check whether the given point is in the array range.
*/
bool isInside (difference_type const & p) const
{
for(int d=0; d<actual_dimension; ++d)
if(p[d] < 0 || p[d] >= shape(d))
return false;
return true;
}
/** Compute the squared Euclidean norm of the array (sum of squares of the array elements).
*/
typename NormTraits<MultiArrayView>::SquaredNormType squaredNorm() const
{
typedef typename NormTraits<MultiArrayView>::SquaredNormType SquaredNormType;
SquaredNormType res = NumericTraits<SquaredNormType>::zero();
detail::sumOverMultiArray(traverser_begin(), shape(), detail::MultiArrayL2Functor<SquaredNormType>(),
res, MetaInt<actual_dimension-1>());
return res;
}
/** Compute various norms of the array.
The norm is determined by parameter \a type:
<ul>
<li> type == 0: maximum norm (L-infinity): maximum of absolute values of the array elements
<li> type == 1: Manhattan norm (L1): sum of absolute values of the array elements
<li> type == 2: Euclidean norm (L2): square root of <tt>squaredNorm()</tt> when \a useSquaredNorm is <tt>true</tt>,<br>
or direct algorithm that avoids underflow/overflow otherwise.
</ul>
Parameter \a useSquaredNorm has no effect when \a type != 2. Defaults: compute L2 norm as square root of
<tt>squaredNorm()</tt>.
*/
typename NormTraits<MultiArrayView>::NormType norm(int type = 2, bool useSquaredNorm = true) const;
/** return the pointer to the image data
*/
pointer data () const
{
return m_ptr;
}
/**
* returns true iff this view refers to valid data,
* i.e. data() is not a NULL pointer. (this is false after
* default construction.)
*/
bool hasData () const
{
return m_ptr != 0;
}
/** returns the N-dimensional MultiIterator pointing
to the first element in every dimension.
*/
traverser traverser_begin ()
{
traverser ret (m_ptr, m_stride.begin (), m_shape.begin ());
return ret;
}
/** returns the N-dimensional MultiIterator pointing
to the const first element in every dimension.
*/
const_traverser traverser_begin () const
{
const_traverser ret (m_ptr, m_stride.begin (), m_shape.begin ());
return ret;
}
/** returns the N-dimensional MultiIterator pointing
beyond the last element in dimension N, and to the
first element in every other dimension.
*/
traverser traverser_end ()
{
traverser ret (m_ptr, m_stride.begin (), m_shape.begin ());
ret += m_shape [actual_dimension-1];
return ret;
}
/** returns the N-dimensional const MultiIterator pointing
beyond the last element in dimension N, and to the
first element in every other dimension.
*/
const_traverser traverser_end () const
{
const_traverser ret (m_ptr, m_stride.begin (), m_shape.begin ());
ret += m_shape [actual_dimension-1];
return ret;
}
view_type view ()
{
return *this;
}
};
template <unsigned int N, class T, class C>
MultiArrayView<N, T, C> &
MultiArrayView <N, T, C>::operator=(MultiArrayView<N, T, C> const & rhs)
{
if(this == &rhs)
return *this;
vigra_precondition(this->shape() == rhs.shape() || m_ptr == 0,
"MultiArrayView::operator=(MultiArrayView const &) size mismatch - use MultiArrayView::reset().");
if(m_ptr == 0)
{
m_shape = rhs.m_shape;
m_stride = rhs.m_stride;
m_ptr = rhs.m_ptr;
}
else
this->copyImpl(rhs);
return *this;
}
template <unsigned int N, class T, class C>
template <class CN>
bool
MultiArrayView <N, T, C>::arraysOverlap(const MultiArrayView <N, T, CN>& rhs) const
{
vigra_precondition (shape () == rhs.shape (),
"MultiArrayView::arraysOverlap(): shape mismatch.");
const_pointer first_element = this->m_ptr,
last_element = first_element + dot(this->m_shape - difference_type(1), this->m_stride);
typename MultiArrayView <N, T, CN>::const_pointer
rhs_first_element = rhs.data(),
rhs_last_element = rhs_first_element + dot(rhs.shape() - difference_type(1), rhs.stride());
return !(last_element < rhs_first_element || rhs_last_element < first_element);
}
template <unsigned int N, class T, class C>
template <class U, class CN>
void
MultiArrayView <N, T, C>::copyImpl(const MultiArrayView <N, U, CN>& rhs)
{
if(!arraysOverlap(rhs))
{
// no overlap -- can copy directly
detail::copyMultiArrayData(rhs.traverser_begin(), shape(), traverser_begin(), MetaInt<actual_dimension-1>());
}
else
{
// overlap: we got different views to the same data -- copy to intermediate memory in order to avoid
// overwriting elements that are still needed on the rhs.
MultiArray<N, T> tmp(rhs);
detail::copyMultiArrayData(tmp.traverser_begin(), shape(), traverser_begin(), MetaInt<actual_dimension-1>());
}
}
#define VIGRA_MULTI_ARRY_COMPUTED_ASSIGNMENT(name, op) \
template <unsigned int N, class T, class C> \
template<class U, class C1> \
MultiArrayView<N, T, C> & \
MultiArrayView <N, T, C>::operator op(MultiArrayView<N, U, C1> const & rhs) \
{ \
vigra_precondition(this->shape() == rhs.shape(), "MultiArrayView::operator" #op "() size mismatch."); \
if(!arraysOverlap(rhs)) \
{ \
detail::name##MultiArrayData(rhs.traverser_begin(), shape(), traverser_begin(), MetaInt<actual_dimension-1>()); \
} \
else \
{ \
MultiArray<N, T> tmp(rhs); \
detail::name##MultiArrayData(tmp.traverser_begin(), shape(), traverser_begin(), MetaInt<actual_dimension-1>()); \
} \
return *this; \
}
VIGRA_MULTI_ARRY_COMPUTED_ASSIGNMENT(copyAdd, +=)
VIGRA_MULTI_ARRY_COMPUTED_ASSIGNMENT(copySub, -=)
VIGRA_MULTI_ARRY_COMPUTED_ASSIGNMENT(copyMul, *=)
VIGRA_MULTI_ARRY_COMPUTED_ASSIGNMENT(copyDiv, /=)
#undef VIGRA_MULTI_ARRY_COMPUTED_ASSIGNMENT
template <unsigned int N, class T, class C>
template <class U, class CN>
void
MultiArrayView <N, T, C>::swapDataImpl(MultiArrayView <N, U, CN> rhs)
{
vigra_precondition (shape () == rhs.shape (),
"MultiArrayView::swapData(): shape mismatch.");
// check for overlap of this and rhs
const_pointer first_element = this->m_ptr,
last_element = first_element + dot(this->m_shape - difference_type(1), this->m_stride);
typename MultiArrayView <N, U, CN>::const_pointer
rhs_first_element = rhs.data(),
rhs_last_element = rhs_first_element + dot(rhs.shape() - difference_type(1), rhs.stride());
if(last_element < rhs_first_element || rhs_last_element < first_element)
{
// no overlap -- can swap directly
detail::swapDataImpl(traverser_begin(), shape(), rhs.traverser_begin(), MetaInt<actual_dimension-1>());
}
else
{
// overlap: we got different views to the same data -- copy to intermediate memory in order to avoid
// overwriting elements that are still needed.
MultiArray<N, T> tmp(*this);
copy(rhs);
rhs.copy(tmp);
}
}
template <unsigned int N, class T, class C>
MultiArrayView <N, T, StridedArrayTag>
MultiArrayView <N, T, C>::permuteDimensions (const difference_type &s) const
{
difference_type shape, stride, check((typename difference_type::value_type)0);
for (unsigned int i = 0; i < actual_dimension; ++i)
{
shape[i] = m_shape[s[i]];
stride[i] = m_stride[s[i]];
++check[s[i]];
}
vigra_precondition(check == difference_type(1),
"MultiArrayView::permuteDimensions(): every dimension must occur exactly once.");
return MultiArrayView <N, T, StridedArrayTag>(shape, stride, m_ptr);
}
template <unsigned int N, class T, class C>
typename MultiArrayView <N, T, C>::difference_type
MultiArrayView <N, T, C>::strideOrdering(difference_type stride)
{
difference_type permutation;
for(unsigned int k=0; k<N; ++k)
permutation[k] = k;
for(unsigned int k=0; k<N-1; ++k)
{
unsigned int smallest = k;
for(unsigned int j=k+1; j<N; ++j)
{
if(stride[j] < stride[smallest])
smallest = j;
}
if(smallest != k)
{
std::swap(stride[k], stride[smallest]);
std::swap(permutation[k], permutation[smallest]);
}
}
difference_type ordering;
for(unsigned int k=0; k<N; ++k)
ordering[permutation[k]] = k;
return ordering;
}
template <unsigned int N, class T, class C>
MultiArrayView <N, T, StridedArrayTag>
MultiArrayView <N, T, C>::permuteStridesAscending() const
{
difference_type ordering(strideOrdering(m_stride)), permutation;
for(MultiArrayIndex k=0; k<N; ++k)
permutation[ordering[k]] = k;
return permuteDimensions(permutation);
}
template <unsigned int N, class T, class C>
MultiArrayView <N, T, StridedArrayTag>
MultiArrayView <N, T, C>::permuteStridesDescending() const
{
difference_type ordering(strideOrdering(m_stride)), permutation;
for(MultiArrayIndex k=0; k<N; ++k)
permutation[ordering[N-1-k]] = k;
return permuteDimensions(permutation);
}
template <unsigned int N, class T, class C>
template <unsigned int M>
MultiArrayView <N-M, T, C>
MultiArrayView <N, T, C>::bindOuter (const TinyVector <MultiArrayIndex, M> &d) const
{
TinyVector <MultiArrayIndex, M> stride;
stride.init (m_stride.begin () + N-M, m_stride.end ());
pointer ptr = m_ptr + dot (d, stride);
static const int NNew = (N-M == 0) ? 1 : N-M;
TinyVector <MultiArrayIndex, NNew> inner_shape, inner_stride;
if (N-M == 0)
{
inner_shape [0] = 1;
inner_stride [0] = 0;
}
else
{
inner_shape.init (m_shape.begin (), m_shape.end () - M);
inner_stride.init (m_stride.begin (), m_stride.end () - M);
}
return MultiArrayView <N-M, T, C> (inner_shape, inner_stride, ptr);
}
template <unsigned int N, class T, class C>
template <unsigned int M>
MultiArrayView <N - M, T, StridedArrayTag>
MultiArrayView <N, T, C>::bindInner (const TinyVector <MultiArrayIndex, M> &d) const
{
TinyVector <MultiArrayIndex, M> stride;
stride.init (m_stride.begin (), m_stride.end () - N + M);
pointer ptr = m_ptr + dot (d, stride);
static const int NNew = (N-M == 0) ? 1 : N-M;
TinyVector <MultiArrayIndex, NNew> outer_shape, outer_stride;
if (N-M == 0)
{
outer_shape [0] = 1;
outer_stride [0] = 0;
}
else
{
outer_shape.init (m_shape.begin () + M, m_shape.end ());
outer_stride.init (m_stride.begin () + M, m_stride.end ());
}
return MultiArrayView <N-M, T, StridedArrayTag>
(outer_shape, outer_stride, ptr);
}
template <unsigned int N, class T, class C>
template <unsigned int M>
MultiArrayView <N-1, T, typename detail::MaybeStrided <M>::type >
MultiArrayView <N, T, C>::bind (difference_type_1 d) const
{
static const int NNew = (N-1 == 0) ? 1 : N-1;
TinyVector <MultiArrayIndex, NNew> shape, stride;
// the remaining dimensions are 0..n-1,n+1..N-1
if (N-1 == 0)
{
shape[0] = 1;
stride[0] = 0;
}
else
{
std::copy (m_shape.begin (), m_shape.begin () + M, shape.begin ());
std::copy (m_shape.begin () + M+1, m_shape.end (),
shape.begin () + M);
std::copy (m_stride.begin (), m_stride.begin () + M, stride.begin ());
std::copy (m_stride.begin () + M+1, m_stride.end (),
stride.begin () + M);
}
return MultiArrayView <N-1, T, typename detail::MaybeStrided <M>::type>
(shape, stride, m_ptr + d * m_stride[M]);
}
template <unsigned int N, class T, class C>
MultiArrayView <N - 1, T, C>
MultiArrayView <N, T, C>::bindOuter (difference_type_1 d) const
{
static const int NNew = (N-1 == 0) ? 1 : N-1;
TinyVector <MultiArrayIndex, NNew> inner_shape, inner_stride;
if (N-1 == 0)
{
inner_shape [0] = 1;
inner_stride [0] = 0;
}
else
{
inner_shape.init (m_shape.begin (), m_shape.end () - 1);
inner_stride.init (m_stride.begin (), m_stride.end () - 1);
}
return MultiArrayView <N-1, T, C> (inner_shape, inner_stride,
m_ptr + d * m_stride [N-1]);
}
template <unsigned int N, class T, class C>
MultiArrayView <N - 1, T, StridedArrayTag>
MultiArrayView <N, T, C>::bindInner (difference_type_1 d) const
{
static const int NNew = (N-1 == 0) ? 1 : N-1;
TinyVector <MultiArrayIndex, NNew> outer_shape, outer_stride;
if (N-1 == 0)
{
outer_shape [0] = 1;
outer_stride [0] = 0;
}
else
{
outer_shape.init (m_shape.begin () + 1, m_shape.end ());
outer_stride.init (m_stride.begin () + 1, m_stride.end ());
}
return MultiArrayView <N-1, T, StridedArrayTag>
(outer_shape, outer_stride, m_ptr + d * m_stride [0]);
}
template <unsigned int N, class T, class C>
MultiArrayView <N - 1, T, StridedArrayTag>
MultiArrayView <N, T, C>::bindAt (difference_type_1 n, difference_type_1 d) const
{
vigra_precondition (
n < static_cast <int> (N),
"MultiArrayView <N, T, C>::bindAt(): dimension out of range.");
static const int NNew = (N-1 == 0) ? 1 : N-1;
TinyVector <MultiArrayIndex, NNew> shape, stride;
// the remaining dimensions are 0..n-1,n+1..N-1
if (N-1 == 0)
{
shape [0] = 1;
stride [0] = 0;
}
else
{
std::copy (m_shape.begin (), m_shape.begin () + n, shape.begin ());
std::copy (m_shape.begin () + n+1, m_shape.end (),
shape.begin () + n);
std::copy (m_stride.begin (), m_stride.begin () + n, stride.begin ());
std::copy (m_stride.begin () + n+1, m_stride.end (),
stride.begin () + n);
}
return MultiArrayView <N-1, T, StridedArrayTag>
(shape, stride, m_ptr + d * m_stride[n]);
}
template <unsigned int N, class T, class C>
MultiArrayView <N + 1, T, C>
MultiArrayView <N, T, C>::insertSingletonDimension (difference_type_1 i) const
{
vigra_precondition (
0 <= i && i <= static_cast <difference_type_1> (N),
"MultiArrayView <N, T, C>::insertSingletonDimension(): index out of range.");
TinyVector <MultiArrayIndex, N+1> shape, stride;
std::copy (m_shape.begin (), m_shape.begin () + i, shape.begin ());
std::copy (m_shape.begin () + i, m_shape.end (), shape.begin () + i + 1);
std::copy (m_stride.begin (), m_stride.begin () + i, stride.begin ());
std::copy (m_stride.begin () + i, m_stride.end (), stride.begin () + i + 1);
shape[i] = 1;
stride[i] = 1;
return MultiArrayView <N+1, T, C>(shape, stride, m_ptr);
}
template <unsigned int N, class T, class C>
typename NormTraits<MultiArrayView <N, T, C> >::NormType
MultiArrayView <N, T, C>::norm(int type, bool useSquaredNorm) const
{
typedef typename NormTraits<MultiArrayView>::NormType NormType;
switch(type)
{
case 0:
{
NormType res = NumericTraits<NormType>::zero();
detail::normMaxOfMultiArray(traverser_begin(), shape(), res, MetaInt<actual_dimension-1>());
return res;
}
case 1:
{
NormType res = NumericTraits<NormType>::zero();
detail::sumOverMultiArray(traverser_begin(), shape(), detail::MultiArrayL1Functor<NormType>(),
res, MetaInt<actual_dimension-1>());
return res;
}
case 2:
{
if(useSquaredNorm)
{
return sqrt((NormType)squaredNorm());
}
else
{
NormType normMax = NumericTraits<NormType>::zero();
detail::normMaxOfMultiArray(traverser_begin(), shape(), normMax, MetaInt<actual_dimension-1>());
if(normMax == NumericTraits<NormType>::zero())
return normMax;
NormType res = NumericTraits<NormType>::zero();
detail::sumOverMultiArray(traverser_begin(), shape(), detail::MultiArrayScaledL2Functor<NormType>(normMax),
res, MetaInt<actual_dimension-1>());
return sqrt(res)*normMax;
}
}
default:
vigra_precondition(false, "MultiArrayView::norm(): Unknown norm type.");
return NumericTraits<NormType>::zero(); // unreachable
}
}
/********************************************************/
/* */
/* norm */
/* */
/********************************************************/
template <unsigned int N, class T, class C>
inline typename NormTraits<MultiArrayView <N, T, C> >::SquaredNormType
squaredNorm(MultiArrayView <N, T, C> const & a)
{
return a.squaredNorm();
}
template <unsigned int N, class T, class C>
inline typename NormTraits<MultiArrayView <N, T, C> >::NormType
norm(MultiArrayView <N, T, C> const & a)
{
return a.norm();
}
/********************************************************/
/* */
/* MultiArray */
/* */
/********************************************************/
/** \brief Main <TT>MultiArray</TT> class containing the memory
management.
This class inherits the interface of MultiArrayView, and implements
the memory ownership.
MultiArray's are always unstrided, striding them creates a MultiArrayView.
The template parameters are as follows
\code
N: the array dimension
T: the type of the array elements
A: the allocator used for internal storage management
(default: std::allocator<T>)
\endcode
<b>\#include</b>
\<<a href="multi__array_8hxx-source.html">vigra/multi_array.hxx</a>\>
Namespace: vigra
*/
template <unsigned int N, class T, class A /* default already declared above */>
class MultiArray : public MultiArrayView <N, T>
{
public:
using MultiArrayView <N, T>::actual_dimension;
/** the allocator type used to allocate the memory
*/
typedef A allocator_type;
/** the view type associated with this array.
*/
typedef MultiArrayView <N, T> view_type;
/** the matrix type associated with this array.
*/
typedef MultiArray <N, T, A> matrix_type;
/** the array's value type
*/
typedef typename view_type::value_type value_type;
/** pointer type
*/
typedef typename view_type::pointer pointer;
/** const pointer type
*/
typedef typename view_type::const_pointer const_pointer;
/** reference type (result of operator[])
*/
typedef typename view_type::reference reference;
/** const reference type (result of operator[] const)
*/
typedef typename view_type::const_reference const_reference;
/** size type
*/
typedef typename view_type::size_type size_type;
/** difference type (used for multi-dimensional offsets and indices)
*/
typedef typename view_type::difference_type difference_type;
/** difference and index type for a single dimension
*/
typedef typename view_type::difference_type_1 difference_type_1;
/** traverser type
*/
typedef typename vigra::detail::MultiIteratorChooser <
UnstridedArrayTag>::template Traverser <N, T, T &, T *>::type
traverser;
/** traverser type to const data
*/
typedef typename vigra::detail::MultiIteratorChooser <
UnstridedArrayTag>::template Traverser <N, T, T const &, T const *>::type
const_traverser;
/** sequential (random access) iterator type
*/
typedef T * iterator;
/** sequential (random access) const iterator type
*/
typedef T * const_iterator;
protected:
typedef typename difference_type::value_type diff_zero_t;
/** the allocator used to allocate the memory
*/
allocator_type m_alloc;
/** allocate memory for s pixels, write its address into the given
pointer and initialize the pixels with init.
*/
void allocate (pointer &ptr, difference_type_1 s, const_reference init);
/** allocate memory for s pixels, write its address into the given
pointer and initialize the linearized pixels to the values of init.
*/
template <class U>
void allocate (pointer &ptr, difference_type_1 s, U const * init);
/** allocate memory, write its address into the given
pointer and initialize it by copying the data from the given MultiArrayView.
*/
template <class U, class C>
void allocate (pointer &ptr, MultiArrayView<N, U, C> const & init);
/** deallocate the memory (of length s) starting at the given address.
*/
void deallocate (pointer &ptr, difference_type_1 s);
template <class U, class C>
void copyOrReshape (const MultiArrayView<N, U, C> &rhs);
public:
/** default constructor
*/
MultiArray ()
: MultiArrayView <N, T> (difference_type (diff_zero_t(0)),
difference_type (diff_zero_t(0)), 0)
{}
/** construct with given allocator
*/
MultiArray (allocator_type const & alloc)
: MultiArrayView <N, T> (difference_type (diff_zero_t(0)),
difference_type (diff_zero_t(0)), 0),
m_alloc(alloc)
{}
/** construct with given shape
*/
explicit MultiArray (const difference_type &shape,
allocator_type const & alloc = allocator_type());
/** construct from shape with an initial value
*/
MultiArray (const difference_type &shape, const_reference init,
allocator_type const & alloc = allocator_type());
/** construct from shape and copy values from the given array
*/
MultiArray (const difference_type &shape, const_pointer init,
allocator_type const & alloc = allocator_type());
/** copy constructor
*/
MultiArray (const MultiArray &rhs)
: MultiArrayView <N, T> (rhs.m_shape, rhs.m_stride, 0),
m_alloc (rhs.m_alloc)
{
allocate (this->m_ptr, this->elementCount (), rhs.data ());
}
/** construct by copying from a MultiArrayView
*/
template <class U, class C>
MultiArray (const MultiArrayView<N, U, C> &rhs,
allocator_type const & alloc = allocator_type());
/** assignment.<br>
If the size of \a rhs is the same as the left-hand side arrays's old size, only
the data are copied. Otherwise, new storage is allocated, which invalidates all
objects (array views, iterators) depending on the lhs array.
*/
MultiArray & operator= (const MultiArray &rhs)
{
if (this != &rhs)
this->copyOrReshape(rhs);
return *this;
}
/** assignment from arbitrary MultiArrayView.<br>
If the size of \a rhs is the same as the left-hand side arrays's old size, only
the data are copied. Otherwise, new storage is allocated, which invalidates all
objects (array views, iterators) depending on the lhs array.
*/
template <class U, class C>
MultiArray &operator= (const MultiArrayView<N, U, C> &rhs)
{
this->copyOrReshape(rhs);
return *this;
}
/** Add-assignment from arbitrary MultiArrayView. Fails with
<tt>PreconditionViolation</tt> exception when the shapes do not match.
*/
template <class U, class C>
MultiArray &operator+= (const MultiArrayView<N, U, C> &rhs)
{
view_type::operator+=(rhs);
return *this;
}
/** Subtract-assignment from arbitrary MultiArrayView. Fails with
<tt>PreconditionViolation</tt> exception when the shapes do not match.
*/
template <class U, class C>
MultiArray &operator-= (const MultiArrayView<N, U, C> &rhs)
{
view_type::operator-=(rhs);
return *this;
}
/** Multiply-assignment from arbitrary MultiArrayView. Fails with
<tt>PreconditionViolation</tt> exception when the shapes do not match.
*/
template <class U, class C>
MultiArray &operator*= (const MultiArrayView<N, U, C> &rhs)
{
view_type::operator*=(rhs);
return *this;
}
/** Divide-assignment from arbitrary MultiArrayView. Fails with
<tt>PreconditionViolation</tt> exception when the shapes do not match.
*/
template <class U, class C>
MultiArray &operator/= (const MultiArrayView<N, U, C> &rhs)
{
view_type::operator/=(rhs);
return *this;
}
/** Add-assignment of a scalar.
*/
MultiArray &operator+= (const T &rhs)
{
view_type::operator+=(rhs);
return *this;
}
/** Subtract-assignment of a scalar.
*/
MultiArray &operator-= (const T &rhs)
{
view_type::operator-=(rhs);
return *this;
}
/** Multiply-assignment of a scalar.
*/
MultiArray &operator*= (const T &rhs)
{
view_type::operator*=(rhs);
return *this;
}
/** Divide-assignment of a scalar.
*/
MultiArray &operator/= (const T &rhs)
{
view_type::operator/=(rhs);
return *this;
}
/** destructor
*/
~MultiArray ()
{
deallocate (this->m_ptr, this->elementCount ());
}
/** init elements with a constant
*/
template <class U>
MultiArray & init(const U & init)
{
view_type::init(init);
return *this;
}
/** Allocate new memory with the given shape and initialize with zeros.<br>
<em>Note:</em> this operation invalidates all dependent objects
(array views and iterators)
*/
void reshape (const difference_type &shape)
{
reshape (shape, NumericTraits <T>::zero ());
}
/** Allocate new memory with the given shape and initialize it
with the given value.<br>
<em>Note:</em> this operation invalidates all dependent objects
(array views and iterators)
*/
void reshape (const difference_type &shape, const_reference init);
/** Swap the contents with another MultiArray. This is fast,
because no data are copied, but only pointers and shapes swapped.
<em>Note:</em> this operation invalidates all dependent objects
(array views and iterators)
*/
void swap (MultiArray & other);
/** sequential iterator pointing to the first array element.
*/
iterator begin ()
{
return this->data();
}
/** sequential iterator pointing beyond the last array element.
*/
iterator end ()
{
return this->data() + this->elementCount();
}
/** sequential const iterator pointing to the first array element.
*/
const_iterator begin () const
{
return this->data();
}
/** sequential const iterator pointing beyond the last array element.
*/
const_iterator end () const
{
return this->data() + this->elementCount();
}
/** get the allocator.
*/
allocator_type const & allocator () const
{
return m_alloc;
}
};
template <unsigned int N, class T, class A>
MultiArray <N, T, A>::MultiArray (const difference_type &shape,
allocator_type const & alloc)
: MultiArrayView <N, T> (shape,
detail::defaultStride <MultiArrayView<N,T>::actual_dimension> (shape),
0),
m_alloc(alloc)
{
if (N == 0)
{
this->m_shape [0] = 1;
this->m_stride [0] = 0;
}
allocate (this->m_ptr, this->elementCount (), NumericTraits<T>::zero ());
}
template <unsigned int N, class T, class A>
MultiArray <N, T, A>::MultiArray (const difference_type &shape, const_reference init,
allocator_type const & alloc)
: MultiArrayView <N, T> (shape,
detail::defaultStride <MultiArrayView<N,T>::actual_dimension> (shape),
0),
m_alloc(alloc)
{
if (N == 0)
{
this->m_shape [0] = 1;
this->m_stride [0] = 0;
}
allocate (this->m_ptr, this->elementCount (), init);
}
template <unsigned int N, class T, class A>
MultiArray <N, T, A>::MultiArray (const difference_type &shape, const_pointer init,
allocator_type const & alloc)
: MultiArrayView <N, T> (shape,
detail::defaultStride <MultiArrayView<N,T>::actual_dimension> (shape),
0),
m_alloc(alloc)
{
if (N == 0)
{
this->m_shape [0] = 1;
this->m_stride [0] = 0;
}
allocate (this->m_ptr, this->elementCount (), init);
}
template <unsigned int N, class T, class A>
template <class U, class C>
MultiArray <N, T, A>::MultiArray(const MultiArrayView<N, U, C> &rhs,
allocator_type const & alloc)
: MultiArrayView <N, T> (rhs.shape(),
detail::defaultStride <MultiArrayView<N,T>::actual_dimension>(rhs.shape()),
0),
m_alloc (alloc)
{
allocate (this->m_ptr, rhs);
}
template <unsigned int N, class T, class A>
template <class U, class C>
void
MultiArray <N, T, A>::copyOrReshape(const MultiArrayView<N, U, C> &rhs)
{
if (this->shape() == rhs.shape())
this->copy(rhs);
else
{
MultiArray t(rhs);
this->swap(t);
}
}
template <unsigned int N, class T, class A>
void MultiArray <N, T, A>::reshape (const difference_type & new_shape,
const_reference initial)
{
if (N== 0)
{
return;
}
else if(new_shape == this->shape())
{
this->init(initial);
}
else
{
difference_type new_stride = detail::defaultStride <MultiArrayView<N,T>::actual_dimension> (new_shape);
difference_type_1 new_size = new_shape [MultiArrayView<N,T>::actual_dimension-1] * new_stride [MultiArrayView<N,T>::actual_dimension-1];
T *new_ptr;
allocate (new_ptr, new_size, initial);
deallocate (this->m_ptr, this->elementCount ());
this->m_ptr = new_ptr;
this->m_shape = new_shape;
this->m_stride = new_stride;
}
}
template <unsigned int N, class T, class A>
inline void
MultiArray <N, T, A>::swap (MultiArray <N, T, A> & other)
{
if (this == &other)
return;
std::swap(this->m_shape, other.m_shape);
std::swap(this->m_stride, other.m_stride);
std::swap(this->m_ptr, other.m_ptr);
std::swap(this->m_alloc, other.m_alloc);
}
template <unsigned int N, class T, class A>
void MultiArray <N, T, A>::allocate (pointer & ptr, difference_type_1 s,
const_reference init)
{
ptr = m_alloc.allocate ((typename A::size_type)s);
difference_type_1 i;
try {
for (i = 0; i < s; ++i)
m_alloc.construct (ptr + i, init);
}
catch (...) {
for (difference_type_1 j = 0; j < i; ++j)
m_alloc.destroy (ptr + j);
m_alloc.deallocate (ptr, (typename A::size_type)s);
throw;
}
}
template <unsigned int N, class T, class A>
template <class U>
void MultiArray <N, T, A>::allocate (pointer & ptr, difference_type_1 s,
U const * init)
{
ptr = m_alloc.allocate ((typename A::size_type)s);
difference_type_1 i;
try {
for (i = 0; i < s; ++i, ++init)
m_alloc.construct (ptr + i, *init);
}
catch (...) {
for (difference_type_1 j = 0; j < i; ++j)
m_alloc.destroy (ptr + j);
m_alloc.deallocate (ptr, (typename A::size_type)s);
throw;
}
}
template <unsigned int N, class T, class A>
template <class U, class C>
void MultiArray <N, T, A>::allocate (pointer & ptr, MultiArrayView<N, U, C> const & init)
{
difference_type_1 s = init.elementCount();
ptr = m_alloc.allocate ((typename A::size_type)s);
pointer p = ptr;
try {
detail::uninitializedCopyMultiArrayData(init.traverser_begin(), init.shape(),
p, m_alloc, MetaInt<actual_dimension-1>());
}
catch (...) {
for (pointer pp = ptr; pp < p; ++pp)
m_alloc.destroy (pp);
m_alloc.deallocate (ptr, (typename A::size_type)s);
throw;
}
}
template <unsigned int N, class T, class A>
inline void MultiArray <N, T, A>::deallocate (pointer & ptr, difference_type_1 s)
{
if (ptr == 0)
return;
for (difference_type_1 i = 0; i < s; ++i)
m_alloc.destroy (ptr + i);
m_alloc.deallocate (ptr, (typename A::size_type)s);
ptr = 0;
}
/********************************************************/
/* */
/* argument object factories */
/* */
/********************************************************/
template <unsigned int N, class T, class C>
inline triple<typename MultiArrayView<N,T,C>::const_traverser,
typename MultiArrayView<N,T,C>::difference_type,
typename AccessorTraits<T>::default_const_accessor >
srcMultiArrayRange( MultiArrayView<N,T,C> const & array )
{
return triple<typename MultiArrayView<N,T,C>::const_traverser,
typename MultiArrayView<N,T,C>::difference_type,
typename AccessorTraits<T>::default_const_accessor >
( array.traverser_begin(),
array.shape(),
typename AccessorTraits<T>::default_const_accessor() );
}
template <unsigned int N, class T, class C, class Accessor>
inline triple<typename MultiArrayView<N,T,C>::const_traverser,
typename MultiArrayView<N,T,C>::difference_type,
Accessor >
srcMultiArrayRange( MultiArrayView<N,T,C> const & array, Accessor a )
{
return triple<typename MultiArrayView<N,T,C>::const_traverser,
typename MultiArrayView<N,T,C>::difference_type,
Accessor >
( array.traverser_begin(),
array.shape(),
a);
}
template <unsigned int N, class T, class C>
inline pair<typename MultiArrayView<N,T,C>::const_traverser,
typename AccessorTraits<T>::default_const_accessor >
srcMultiArray( MultiArrayView<N,T,C> const & array )
{
return pair<typename MultiArrayView<N,T,C>::const_traverser,
typename AccessorTraits<T>::default_const_accessor >
( array.traverser_begin(),
typename AccessorTraits<T>::default_const_accessor() );
}
template <unsigned int N, class T, class C, class Accessor>
inline pair<typename MultiArrayView<N,T,C>::const_traverser,
Accessor >
srcMultiArray( MultiArrayView<N,T,C> const & array, Accessor a )
{
return pair<typename MultiArrayView<N,T,C>::const_traverser,
Accessor >
( array.traverser_begin(), a );
}
template <unsigned int N, class T, class C>
inline triple<typename MultiArrayView<N,T,C>::traverser,
typename MultiArrayView<N,T,C>::difference_type,
typename AccessorTraits<T>::default_accessor >
destMultiArrayRange( MultiArrayView<N,T,C> & array )
{
return triple<typename MultiArrayView<N,T,C>::traverser,
typename MultiArrayView<N,T,C>::difference_type,
typename AccessorTraits<T>::default_accessor >
( array.traverser_begin(),
array.shape(),
typename AccessorTraits<T>::default_accessor() );
}
template <unsigned int N, class T, class C, class Accessor>
inline triple<typename MultiArrayView<N,T,C>::traverser,
typename MultiArrayView<N,T,C>::difference_type,
Accessor >
destMultiArrayRange( MultiArrayView<N,T,C> & array, Accessor a )
{
return triple<typename MultiArrayView<N,T,C>::traverser,
typename MultiArrayView<N,T,C>::difference_type,
Accessor >
( array.traverser_begin(),
array.shape(),
a );
}
template <unsigned int N, class T, class C>
inline pair<typename MultiArrayView<N,T,C>::traverser,
typename AccessorTraits<T>::default_accessor >
destMultiArray( MultiArrayView<N,T,C> & array )
{
return pair<typename MultiArrayView<N,T,C>::traverser,
typename AccessorTraits<T>::default_accessor >
( array.traverser_begin(),
typename AccessorTraits<T>::default_accessor() );
}
template <unsigned int N, class T, class C, class Accessor>
inline pair<typename MultiArrayView<N,T,C>::traverser,
Accessor >
destMultiArray( MultiArrayView<N,T,C> & array, Accessor a )
{
return pair<typename MultiArrayView<N,T,C>::traverser,
Accessor >
( array.traverser_begin(), a );
}
/********************************************************************/
template <class PixelType, class Accessor>
inline triple<ConstStridedImageIterator<PixelType>,
ConstStridedImageIterator<PixelType>, Accessor>
srcImageRange(const MultiArrayView<2, PixelType, StridedArrayTag> & img, Accessor a)
{
ConstStridedImageIterator<PixelType>
ul(img.data(), 1, img.stride(0), img.stride(1));
return triple<ConstStridedImageIterator<PixelType>,
ConstStridedImageIterator<PixelType>,
Accessor>(
ul, ul + Size2D(img.shape(0), img.shape(1)), a);
}
template <class PixelType, class Accessor>
inline pair<ConstStridedImageIterator<PixelType>, Accessor>
srcImage(const MultiArrayView<2, PixelType, StridedArrayTag> & img, Accessor a)
{
ConstStridedImageIterator<PixelType>
ul(img.data(), 1, img.stride(0), img.stride(1));
return pair<ConstStridedImageIterator<PixelType>, Accessor>
(ul, a);
}
template <class PixelType, class Accessor>
inline triple<StridedImageIterator<PixelType>,
StridedImageIterator<PixelType>, Accessor>
destImageRange(MultiArrayView<2, PixelType, StridedArrayTag> & img, Accessor a)
{
StridedImageIterator<PixelType>
ul(img.data(), 1, img.stride(0), img.stride(1));
return triple<StridedImageIterator<PixelType>,
StridedImageIterator<PixelType>,
Accessor>(
ul, ul + Size2D(img.shape(0), img.shape(1)), a);
}
template <class PixelType, class Accessor>
inline pair<StridedImageIterator<PixelType>, Accessor>
destImage(MultiArrayView<2, PixelType, StridedArrayTag> & img, Accessor a)
{
StridedImageIterator<PixelType>
ul(img.data(), 1, img.stride(0), img.stride(1));
return pair<StridedImageIterator<PixelType>, Accessor>
(ul, a);
}
template <class PixelType, class Accessor>
inline pair<StridedImageIterator<PixelType>, Accessor>
maskImage(MultiArrayView<2, PixelType, StridedArrayTag> & img, Accessor a)
{
StridedImageIterator<PixelType>
ul(img.data(), 1, img.stride(0), img.stride(1));
return pair<StridedImageIterator<PixelType>, Accessor>
(ul, a);
}
// -------------------------------------------------------------------
template <class PixelType>
inline triple<ConstStridedImageIterator<PixelType>,
ConstStridedImageIterator<PixelType>,
typename AccessorTraits<PixelType>::default_const_accessor>
srcImageRange(MultiArrayView<2, PixelType, StridedArrayTag> const & img)
{
ConstStridedImageIterator<PixelType>
ul(img.data(), 1, img.stride(0), img.stride(1));
typedef typename AccessorTraits<PixelType>::default_const_accessor Accessor;
return triple<ConstStridedImageIterator<PixelType>,
ConstStridedImageIterator<PixelType>,
Accessor>
(ul, ul + Size2D(img.shape(0), img.shape(1)), Accessor());
}
template <class PixelType>
inline triple<ConstImageIterator<PixelType>,
ConstImageIterator<PixelType>,
typename AccessorTraits<PixelType>::default_const_accessor>
srcImageRange(MultiArrayView<2, PixelType, UnstridedArrayTag> const & img)
{
ConstImageIterator<PixelType>
ul(img.data(), img.stride(1));
typedef typename AccessorTraits<PixelType>::default_const_accessor Accessor;
return triple<ConstImageIterator<PixelType>,
ConstImageIterator<PixelType>,
Accessor>
(ul, ul + Size2D(img.shape(0), img.shape(1)), Accessor());
}
template <class PixelType>
inline pair< ConstStridedImageIterator<PixelType>,
typename AccessorTraits<PixelType>::default_const_accessor>
srcImage(MultiArrayView<2, PixelType, StridedArrayTag> const & img)
{
ConstStridedImageIterator<PixelType>
ul(img.data(), 1, img.stride(0), img.stride(1));
typedef typename AccessorTraits<PixelType>::default_const_accessor Accessor;
return pair<ConstStridedImageIterator<PixelType>,
Accessor>
(ul, Accessor());
}
template <class PixelType>
inline pair< ConstImageIterator<PixelType>,
typename AccessorTraits<PixelType>::default_const_accessor>
srcImage(MultiArrayView<2, PixelType, UnstridedArrayTag> const & img)
{
ConstImageIterator<PixelType>
ul(img.data(), img.stride(1));
typedef typename AccessorTraits<PixelType>::default_const_accessor Accessor;
return pair<ConstImageIterator<PixelType>,
Accessor>
(ul, Accessor());
}
template <class PixelType>
inline triple< StridedImageIterator<PixelType>,
StridedImageIterator<PixelType>,
typename AccessorTraits<PixelType>::default_accessor>
destImageRange(MultiArrayView<2, PixelType, StridedArrayTag> & img)
{
StridedImageIterator<PixelType>
ul(img.data(), 1, img.stride(0), img.stride(1));
typedef typename AccessorTraits<PixelType>::default_accessor Accessor;
return triple<StridedImageIterator<PixelType>,
StridedImageIterator<PixelType>,
Accessor>
(ul, ul + Size2D(img.shape(0), img.shape(1)), Accessor());
}
template <class PixelType>
inline triple< ImageIterator<PixelType>,
ImageIterator<PixelType>,
typename AccessorTraits<PixelType>::default_accessor>
destImageRange(MultiArrayView<2, PixelType, UnstridedArrayTag> & img)
{
ImageIterator<PixelType>
ul(img.data(), img.stride(1));
typedef typename AccessorTraits<PixelType>::default_accessor Accessor;
return triple<ImageIterator<PixelType>,
ImageIterator<PixelType>,
Accessor>
(ul, ul + Size2D(img.shape(0), img.shape(1)), Accessor());
}
template <class PixelType>
inline pair< StridedImageIterator<PixelType>,
typename AccessorTraits<PixelType>::default_accessor>
destImage(MultiArrayView<2, PixelType, StridedArrayTag> & img)
{
StridedImageIterator<PixelType>
ul(img.data(), 1, img.stride(0), img.stride(1));
typedef typename AccessorTraits<PixelType>::default_accessor Accessor;
return pair<StridedImageIterator<PixelType>, Accessor>
(ul, Accessor());
}
template <class PixelType>
inline pair< ImageIterator<PixelType>,
typename AccessorTraits<PixelType>::default_accessor>
destImage(MultiArrayView<2, PixelType, UnstridedArrayTag> & img)
{
ImageIterator<PixelType> ul(img.data(), img.stride(1));
typedef typename AccessorTraits<PixelType>::default_accessor Accessor;
return pair<ImageIterator<PixelType>, Accessor>(ul, Accessor());
}
template <class PixelType>
inline pair< ConstStridedImageIterator<PixelType>,
typename AccessorTraits<PixelType>::default_accessor>
maskImage(MultiArrayView<2, PixelType, StridedArrayTag> const & img)
{
ConstStridedImageIterator<PixelType>
ul(img.data(), 1, img.stride(0), img.stride(1));
typedef typename AccessorTraits<PixelType>::default_accessor Accessor;
return pair<ConstStridedImageIterator<PixelType>, Accessor>
(ul, Accessor());
}
template <class PixelType>
inline pair< ConstImageIterator<PixelType>,
typename AccessorTraits<PixelType>::default_accessor>
maskImage(MultiArrayView<2, PixelType, UnstridedArrayTag> const & img)
{
ConstImageIterator<PixelType>
ul(img.data(), img.stride(1));
typedef typename AccessorTraits<PixelType>::default_accessor Accessor;
return pair<ConstImageIterator<PixelType>, Accessor>
(ul, Accessor());
}
/********************************************************/
/* */
/* makeBasicImageView */
/* */
/********************************************************/
/** \addtogroup MultiArrayToImage Wrap a \ref vigra::MultiArrayView in
a \ref vigra::BasicImageView
*/
//@{
/** Create a \ref vigra::BasicImageView from an unstrided 2-dimensional
\ref vigra::MultiArrayView.
The \ref vigra::BasicImageView will have the same <tt>value_type </tt>
as the original \ref vigra::MultiArrayView.
*/
template <class T>
BasicImageView <T>
makeBasicImageView (MultiArrayView <2, T, UnstridedArrayTag> const &array)
{
return BasicImageView <T> (array.data (), array.shape (0),
array.shape (1));
}
/** Create a \ref vigra::BasicImageView from a 3-dimensional
\ref vigra::MultiArray.
This wrapper flattens the two innermost dimensions of the array
into single rows of the resulting image.
The \ref vigra::BasicImageView will have the same <tt>value_type </tt>
as the original \ref vigra::MultiArray.
*/
template <class T>
BasicImageView <T>
makeBasicImageView (MultiArray <3, T> const &array)
{
return BasicImageView <T> (array.data (),
array.shape (0)*array.shape (1), array.shape (2));
}
/** Create a \ref vigra::BasicImageView from a 3-dimensional
\ref vigra::MultiArray.
This wrapper only works if <tt>T</tt> is a scalar type and the
array's innermost dimension has size 3. It then re-interprets
the data array as a 2-dimensional array with value_type
<tt>RGBValue<T></tt>.
*/
template <class T>
BasicImageView <RGBValue<T> >
makeRGBImageView (MultiArray<3, T> const &array)
{
vigra_precondition (
array.shape (0) == 3, "makeRGBImageView(): array.shape(0) must be 3.");
return BasicImageView <RGBValue<T> > (
reinterpret_cast <RGBValue <T> *> (array.data ()),
array.shape (1), array.shape (2));
}
//@}
} // namespace vigra
#undef VIGRA_ASSERT_INSIDE
#endif // VIGRA_MULTI_ARRAY_HXX
|