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
|
/* save to deep zoom format
*
* 21/3/12
* - from the tiff pyramid writer
* 5/7/12 (thanks Alexander Koshman)
* - make tiles down to 1x1 pixels
* - oop make right-hand edge tiles
* - improve overlap handling
* 7/7/12
* - threaded write
* 6/8/12 (thanks to Benjamin Gilbert for pointing out the errors)
* - shrink down to a 1x1 pixel tile, even for very long and thin images
* - round image size up on shrink
* - write a .dzi file with the pyramid params
* - default tile size and overlap now matches the openslide writer
* 7/8/12 (thanks to Benjamin Gilbert again for more testing)
* - reorganise the directory structure
* - rename to basename and tile_size
* - deprecate tile_width/_height and dirname
* 1/10/12
* - did not write low pyramid layers for images with an odd number of
* scan lines (thanks Martin)
* 2/10/12
* - remove filename options from format string in .dzi (thanks Martin)
* 3/10/12
* - add zoomify and google maps output
* 10/10/12
* - add @background option
* 1/11/12
* - add @depth option
* 21/1/13
* - add @centre option
* 26/2/13
* - fix another corner case, thanks Martin
* 29/5/13
* - add --angle option
* 19/6/13
* - faster --centre logic, thanks Kacey
* 18/4/14
* - use libgsf for output so we can write to .zip etc. as well as the
* filesystem
* 8/5/14
* - set Type on strips so we can convert for save correctly, thanks
* philipgiuliani
* 25/6/14
* - stop on zip write >4gb, thanks bgilbert
* - save metadata, see https://github.com/libvips/libvips/issues/137
* 18/8/14
* - use g_ date funcs, helps Windows
* 14/2/15
* - use vips_region_shrink()
* 22/2/15
* - use a better temp dir name for fs dz output
* 8/8/15
* - allow zip > 4gb if we have a recent libgsf
* 9/9/15
* - better overlap handling, thanks robclouth
* 24/11/15
* - don't write almost blank tiles in google mode
* 25/11/15
* - always strip tile metadata
* 16/12/15
* - fix overlap handling again, thanks erdmann
* 8/6/16 Felix Bünemann
* - add @compression option
* 5/9/16
* - more overlap changes to help gmaps mode
* 8/9/16 Felix Bünemann
* - move vips-properties out of subdir for gm and zoomify layouts
* 15/10/16
* - add dzsave_buffer
* 11/11/16 Felix Bünemann
* - better >4gb detection for zip output on older libgsfs
* 18/8/17
* - shut down the output earlier to flush zip output
* 24/11/17
* - output overlap-only tiles on edges for better deepzoom spec
* compliance
* 6/1/18
* - add scan-properties.xml for szi output
* - write all associated images
* 19/12/18
* - add @skip_blanks
* 21/10/19
* - add @no_strip
* 9/11/19
* - add IIIF layout
* 24/4/20 [IllyaMoskvin]
* - better IIIF tile naming
* 15/10/21 martimpassos
* - add IIIF3 layout
* 21/12/21 whalehub
* - remove trailing comma from IIIFv3 folder names
* 29/3/22
* - always write a properties file
* - add .szi as a registered suffix
* 9/5/22
* - add dzsave_target
*/
/*
This file is part of VIPS.
VIPS is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
/*
These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
*/
/*
This is difficult to test, there are so many options.
It's failed in the past in these cases. These have layers with strips which
exactly align with image edges, or which have orphan scanlines which need
adding for the shrink.
1. $ header test.v
test.v: 14016x16448 uchar, 3 bands, srgb, openin VipsImage (0x11e7060)
$ time vips dzsave test.v x --overlap 0
Not all layers written.
2. $ header ~/Desktop/leicaimage.scn
/home/john/Desktop/leicaimage.scn: 4225x7905 uchar, 4 bands, rgb
Not all layers written.
3. $ header ~/leicatest1.scn
/home/john/leicatest1.scn: 11585x8449 uchar, 4 bands, rgb
Not all layers written.
various combinations of odd and even tile-size and overlap need testing too.
Overlap handling
For deepzoom, tile-size == 254 and overlap == 1 means that edge tiles are
255 x 255 (though less at the bottom right) and non-edge tiles are 256 x
256. Tiles are positioned across the image in tile-size steps. This means
(confusingly) that two adjoining tiles will have two pixels in common.
This has caused bugs in the past.
*/
/*
#define DEBUG_VERBOSE
#define VIPS_DEBUG
#define DEBUG
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <glib/gi18n-lib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <vips/vips.h>
#include <vips/internal.h>
#ifdef HAVE_GSF
/* Disable deprecation warnings from gsf. There are loads, and still not
* patched as of 12/2020.
*/
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#include <gsf/gsf.h>
#pragma GCC diagnostic pop
/* A GSF output object that can write to a VipsTarget.
*/
typedef struct _GsfOutputTarget {
GsfOutput output;
VipsTarget *target;
} GsfOutputTarget;
typedef struct {
GsfOutputClass output_class;
} GsfOutputTargetClass;
G_DEFINE_TYPE( GsfOutputTarget, gsf_output_target, GSF_OUTPUT_TYPE );
static gboolean
gsf_output_target_close( GsfOutput *output )
{
GsfOutputTarget *output_target = (GsfOutputTarget *) output;
if( output_target->target ) {
/* No easy way to report errors here, sadly.
*/
(void) vips_target_end( output_target->target );
VIPS_UNREF( output_target->target );
return( TRUE );
}
return( FALSE );
}
static void
gsf_output_target_finalize( GObject *obj )
{
GsfOutputTarget *output_target = (GsfOutputTarget *) obj;
(void) gsf_output_target_close( GSF_OUTPUT( output_target ) );
G_OBJECT_CLASS( gsf_output_target_parent_class )->finalize( obj );
}
static gboolean
gsf_output_target_write( GsfOutput *output,
size_t num_bytes, guint8 const *buffer )
{
GsfOutputTarget *output_target = (GsfOutputTarget *) output;
if( vips_target_write( output_target->target, buffer, num_bytes ) )
return( FALSE );
return( TRUE );
}
static gboolean
gsf_output_target_seek( GsfOutput *output, gsf_off_t offset, GSeekType whence )
{
#ifdef HAVE_GSF_ZIP64
/* No seek needed.
*/
return( FALSE );
#else
GsfOutputTarget *output_target = (GsfOutputTarget *) output;
int stdio_whence;
switch( whence ) {
case G_SEEK_CUR: stdio_whence = SEEK_CUR; break;
case G_SEEK_END: stdio_whence = SEEK_END; break;
case G_SEEK_SET: stdio_whence = SEEK_SET; break;
default:
g_assert_not_reached();
}
if( vips_target_seek( output_target->target,
offset, stdio_whence ) == -1 )
return( FALSE );
/* This will make our parent class handle the seek.
*/
return( TRUE );
#endif
}
static void
gsf_output_target_init( GsfOutputTarget *output )
{
}
static void
gsf_output_target_class_init( GsfOutputTargetClass *class )
{
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
GsfOutputClass *output_class = GSF_OUTPUT_CLASS( class );
gobject_class->finalize = gsf_output_target_finalize;
output_class->Close = gsf_output_target_close;
output_class->Write = gsf_output_target_write;
output_class->Seek = gsf_output_target_seek;
}
static GsfOutput *
gsf_output_target_new( VipsTarget *target )
{
GsfOutputTarget *output;
output = g_object_new( gsf_output_target_get_type(), NULL );
output->target = target;
g_object_ref( target );
return( GSF_OUTPUT( output ) );
}
/* A GSF output object that can write to a directory.
*/
typedef struct _GsfOutputDir {
GsfOutfile output;
char *root;
} GsfOutputDir;
typedef struct {
GsfOutfileClass output_class;
} GsfOutputDirClass;
G_DEFINE_TYPE( GsfOutputDir, gsf_output_dir, GSF_OUTFILE_TYPE );
static gboolean
gsf_output_dir_close( GsfOutput *output )
{
return( TRUE );
}
static void
gsf_output_dir_finalize( GObject *obj )
{
GsfOutputDir *output_dir = (GsfOutputDir *) obj;
g_free( output_dir->root );
G_OBJECT_CLASS( gsf_output_dir_parent_class )->finalize( obj );
}
static GsfOutfile *
gsf_output_dir_new_valist( char const *root,
char const *first_property_name, va_list var_args )
{
GsfOutputDir *output_dir;
if( g_mkdir( root, 0777 ) != 0 && errno != EEXIST ) {
int save_errno = errno;
char *utf8name = g_filename_display_name( root );
vips_error( "dzsave",
_( "unable to create directory \"%s\", %s" ),
utf8name, g_strerror( save_errno ) );
g_free( utf8name );
return( NULL );
}
output_dir = (GsfOutputDir *) g_object_new_valist( GSF_OUTFILE_STDIO_TYPE,
first_property_name, var_args );
output_dir->root = g_strdup( root );
gsf_output_set_name_from_filename( GSF_OUTPUT( output_dir ), root );
return( GSF_OUTFILE( output_dir ) );
}
static GsfOutput *
gsf_output_dir_new_child( GsfOutfile *parent,
char const *name, gboolean is_dir,
char const *first_property_name,
va_list args )
{
GsfOutputDir *output_dir = (GsfOutputDir *) parent;
GsfOutput *child;
char *path = g_build_filename( output_dir->root, name, NULL );
if( is_dir )
child = (GsfOutput *) gsf_output_dir_new_valist( path,
first_property_name, args );
else
child = gsf_output_stdio_new_valist( path, NULL,
first_property_name, args );
g_free( path );
return( child );
}
static void
gsf_output_dir_init( GsfOutputDir *output )
{
output->root = NULL;
}
static void
gsf_output_dir_class_init( GsfOutputDirClass *class )
{
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
GsfOutputClass *output_class = GSF_OUTPUT_CLASS( class );
GsfOutfileClass *outfile_class = GSF_OUTFILE_CLASS( class );
gobject_class->finalize = gsf_output_dir_finalize;
output_class->Close = gsf_output_dir_close;
output_class->Seek = NULL;
output_class->Write = NULL;
output_class->Vprintf = NULL;
outfile_class->new_child = gsf_output_dir_new_child;
}
static GsfOutput *
gsf_output_dir_new( char const *root, ... )
{
GsfOutfile *output;
va_list var_args;
va_start( var_args, root );
output = gsf_output_dir_new_valist( root, NULL, var_args );
va_end( var_args );
return( GSF_OUTPUT( output ) );
}
/* Simple wrapper around libgsf.
*
* We need to be able to do scattered writes to structured files. So while
* building a zip (for example) we need to be able to write to file/a/b.jpg,
* then to file/c/d.jpg, then back to file/a/e.jpg. This is tricky with the
* libgsf API which is happier doing writes in order.
*
* Put an API over libgsf to track refs to all directories and finish/close
* them.
*/
/* Need to track the directory tree we are writing, with a ref for each
* GsfOutput.
*/
typedef struct _VipsGsfDirectory {
struct _VipsGsfDirectory *parent;
char *name;
/* List of child directories, if any.
*/
GSList *children;
/* The GsfOutput we use for this object.
*/
GsfOutput *out;
/* The root node holds the enclosing zip file or FS root ... finish
* this on cleanup.
*/
GsfOutput *container;
/* Track number of files in tree and total length of filenames. We use
* this to estimate zip size to spot a >4gb write.
*/
size_t file_count;
size_t filename_lengths;
/* Set deflate compression level for zip container.
*/
gint deflate_level;
} VipsGsfDirectory;
static void *vips_gsf_tree_close( VipsGsfDirectory *tree );
static void *
vips_gsf_tree_close_cb( void *item, void *a, void *b )
{
VipsGsfDirectory *tree = (VipsGsfDirectory *) item;
return( vips_gsf_tree_close( tree ) );
}
/* Close all dirs, non-NULL on error.
*/
static void *
vips_gsf_tree_close( VipsGsfDirectory *tree )
{
vips_slist_map2( tree->children, vips_gsf_tree_close_cb, NULL, NULL );
if( tree->out ) {
if( !gsf_output_is_closed( tree->out ) &&
!gsf_output_close( tree->out ) ) {
vips_error( "vips_gsf",
"%s", _( "unable to close stream" ) );
return( tree );
}
VIPS_UNREF( tree->out );
}
if( tree->container ) {
if( !gsf_output_is_closed( tree->container ) &&
!gsf_output_close( tree->container ) ) {
vips_error( "vips_gsf",
"%s", _( "unable to close stream" ) );
return( tree );
}
VIPS_UNREF( tree->container );
}
VIPS_FREEF( g_slist_free, tree->children );
VIPS_FREE( tree->name );
VIPS_FREE( tree );
return( NULL );
}
/* Make a new tree root.
*/
static VipsGsfDirectory *
vips_gsf_tree_new( GsfOutput *out, gint deflate_level )
{
VipsGsfDirectory *tree = g_new( VipsGsfDirectory, 1 );
tree->parent = NULL;
tree->name = NULL;
tree->children = NULL;
tree->out = out;
tree->container = NULL;
tree->file_count = 0;
tree->filename_lengths = 0;
tree->deflate_level = deflate_level;
return( tree );
}
static void *
vips_gsf_child_by_name_sub( VipsGsfDirectory *dir, const char *name, void *b )
{
if( strcmp( dir->name, name ) == 0 )
return( dir );
return( NULL );
}
/* Look up a child by name.
*/
static VipsGsfDirectory *
vips_gsf_child_by_name( VipsGsfDirectory *dir, const char *name )
{
return( vips_slist_map2( dir->children,
(VipsSListMap2Fn) vips_gsf_child_by_name_sub,
(char *) name, NULL ) );
}
/* Make a new directory.
*/
static VipsGsfDirectory *
vips_gsf_dir_new( VipsGsfDirectory *parent, const char *name )
{
VipsGsfDirectory *dir = g_new( VipsGsfDirectory, 1 );
g_assert( !vips_gsf_child_by_name( parent, name ) );
dir->parent = parent;
dir->name = g_strdup( name );
dir->children = NULL;
dir->container = NULL;
dir->file_count = 0;
dir->filename_lengths = 0;
dir->deflate_level = parent->deflate_level;
if( GSF_IS_OUTFILE_ZIP( parent->out ) )
dir->out = gsf_outfile_new_child_full(
(GsfOutfile *) parent->out,
name, TRUE,
"compression-level", GSF_ZIP_STORED,
NULL );
else
dir->out = gsf_outfile_new_child(
(GsfOutfile *) parent->out,
name, TRUE );
g_assert( dir->out );
parent->children = g_slist_prepend( parent->children, dir );
return( dir );
}
/* Return a GsfOutput or %NULL for writing to a path. Paths are object name first,
* then path components with least-specific first, NULL-terminated. For example:
*
* GsfOutput *obj = vips_gsf_path( tree, "fred.jpg", "a", "b", NULL );
*
* Returns an obj you can use to write to a/b/fred.jpg, or %NULL when it exceeds
* the path limits.
*
* You must write, close and unref obj.
*/
static GsfOutput *
vips_gsf_path( VipsGsfDirectory *tree, const char *name, ... )
{
va_list ap;
VipsGsfDirectory *dir;
VipsGsfDirectory *child;
char *dir_name;
GsfOutput *obj;
/* vips_gsf_path() always makes a new file, though it may add to an
* existing directory. Note the file, and note the length of the full
* path we are creating.
*/
tree->file_count += 1;
tree->filename_lengths +=
strlen( tree->out->name ) + strlen( name ) + 1;
dir = tree;
va_start( ap, name );
while( (dir_name = va_arg( ap, char * )) ) {
if( (child = vips_gsf_child_by_name( dir, dir_name )) )
dir = child;
else
dir = vips_gsf_dir_new( dir, dir_name );
tree->filename_lengths += strlen( dir_name ) + 1;
}
va_end( ap );
if( GSF_IS_OUTFILE_ZIP( dir->out ) ) {
/* Confusingly, libgsf compression-level really means
* compression-method. They have a separate deflate-level
* property for the deflate compression level.
*/
if( dir->deflate_level == 0 )
obj = gsf_outfile_new_child_full(
(GsfOutfile *) dir->out,
name, FALSE,
"compression-level", GSF_ZIP_STORED,
NULL );
else if( dir->deflate_level == -1 )
obj = gsf_outfile_new_child_full(
(GsfOutfile *) dir->out,
name, FALSE,
"compression-level", GSF_ZIP_DEFLATED,
NULL );
else
obj = gsf_outfile_new_child_full(
(GsfOutfile *) dir->out,
name, FALSE,
"compression-level", GSF_ZIP_DEFLATED,
"deflate-level", dir->deflate_level,
NULL );
}
else
obj = gsf_outfile_new_child( (GsfOutfile *) dir->out,
name, FALSE );
return( obj );
}
typedef struct _VipsForeignSaveDz VipsForeignSaveDz;
typedef struct _Layer Layer;
/* A layer in the pyramid.
*/
struct _Layer {
VipsForeignSaveDz *dz;
/* The real size of the image. image->Xsize and image->Ysize are
* always even to make x2 shrink easy. The real image may be a
* smaller, odd size,
*/
int width;
int height;
/* Number of tiles across and down in this layer. Zoomify needs this
* to calculate the directory to put each tile in.
*/
int tiles_across;
int tiles_down;
/* The rect within width/height that contains real image, as opposed
* to background. In centre mode we can have large image borders.
*/
VipsRect real_pixels;
/* The image we build.
*/
VipsImage *image;
/* The top of this strip of tiles.
*/
int y;
/* The next line we write to in this strip.
*/
int write_y;
VipsRegion *strip; /* The current strip of pixels */
VipsRegion *copy; /* Pixels we copy to the next strip */
int sub; /* Subsample factor for this layer */
int n; /* Layer number ... 0 for smallest */
Layer *below; /* Tiles go to here */
Layer *above; /* Tiles come from here */
};
struct _VipsForeignSaveDz {
VipsForeignSave parent_object;
/* The target we are writing to. This is set by our subclasses.
*/
VipsTarget *target;
/* Alternatively, the filename, for filesystem output.
*/
char *filename;
char *suffix;
int overlap;
int tile_size;
VipsForeignDzLayout layout;
VipsForeignDzDepth depth;
gboolean centre;
gboolean properties;
VipsAngle angle;
VipsForeignDzContainer container;
int compression;
VipsRegionShrink region_shrink;
int skip_blanks;
gboolean no_strip;
char *id;
/* Tile and overlap geometry. The members above are the parameters we
* accept, this next set are the derived values which are actually
* used in pyramid generation.
*
* Tiles have a base tile_size. Imagine a square placed at the top left.
* This is the size of that square.
*
* Tiles have a margin. The square from tile_size is expanded outward
* up/down/left/right by this amount. Parts going off the image are
* clipped.
*
* Each time we write a new tile, we step the position by tile_step
* pixels.
*
* We need all three of tile_size, tile_margin and tile_step since
* deepzoom and google maps have different meanings for overlap and we
* want to be able to support both models.
*
* For deepzoom:
*
* tile_margin = overlap
* tile_step = tile_size
*
* For google maps:
*
* tile_margin = 0
* tile_step = tile_size - overlap
*/
int tile_margin;
int tile_step;
Layer *layer; /* x2 shrink pyr layer */
/* Count zoomify tiles we write.
*/
int tile_count;
/* The tree structure we are writing tiles to. Can be filesystem, a
* zipfile, etc.
*/
VipsGsfDirectory *tree;
/* The actual output object our zip (or whatever) is writing to.
*/
GsfOutput *out;
/* The name to save as, eg. deepzoom tiles go into ${basename}_files.
* No suffix, no path at the start.
*/
char *basename;
/* The directory we write the output to, or NULL for memory output.
*/
char *dirname;
/* The root directory name ... $basename with perhaps some extra
* stuff, eg. $(basename)_files, etc.
*/
char *root_name;
/* @suffix, but without any options. So @suffix == ".jpg[Q=90]"
* becomes ".jpg".
*/
char *file_suffix;
/* libgsf before 1.14.31 can't write zip files larger than 4gb.
* Track bytes written here and try to guess when we'll go over.
*/
size_t bytes_written;
/* save->background turned into a pixel that matches the image we are
* saving .. used to test for blank tiles.
*/
VipsPel *ink;
};
typedef VipsForeignSaveClass VipsForeignSaveDzClass;
G_DEFINE_ABSTRACT_TYPE( VipsForeignSaveDz, vips_foreign_save_dz,
VIPS_TYPE_FOREIGN_SAVE );
#define VIPS_ZIP_FIXED_LH_SIZE (30 + 29)
#define VIPS_ZIP_FIXED_CD_SIZE (46 + 9)
#define VIPS_ZIP_EOCD_SIZE 22
#ifndef HAVE_GSF_ZIP64
/* ZIP and SZI are both written as zip files.
*/
static gboolean
iszip( VipsForeignDzContainer container )
{
switch( container ) {
case VIPS_FOREIGN_DZ_CONTAINER_ZIP:
case VIPS_FOREIGN_DZ_CONTAINER_SZI:
return( TRUE );
default:
return( FALSE );
}
}
static size_t
estimate_zip_size( VipsForeignSaveDz *dz )
{
size_t estimated_zip_size = dz->bytes_written +
dz->tree->file_count * VIPS_ZIP_FIXED_LH_SIZE +
dz->tree->filename_lengths +
dz->tree->file_count * VIPS_ZIP_FIXED_CD_SIZE +
dz->tree->filename_lengths +
VIPS_ZIP_EOCD_SIZE;
#ifdef DEBUG_VERBOSE
printf( "estimate_zip_size: %zd\n", estimated_zip_size );
#endif /*DEBUG_VERBOSE*/
return( estimated_zip_size );
}
#endif /*HAVE_GSF_ZIP64*/
static int
write_image( VipsForeignSaveDz *dz,
GsfOutput *out, VipsImage *image, const char *format )
{
VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( dz );
VipsImage *t;
void *buf;
size_t len;
/* We need to block progress signalling on individual image write, so
* we need a copy of the tile in case it's shared (eg. associated
* images).
*/
if( vips_copy( image, &t, NULL ) )
return( -1 );
/* We default to stripping all metadata. "no_strip" turns this
* off. Most people don't want metadata on every tile.
*/
vips_image_set_int( t, "hide-progress", 1 );
if( vips_image_write_to_buffer( t, format, &buf, &len,
"strip", !dz->no_strip,
NULL ) ) {
VIPS_UNREF( t );
return( -1 );
}
VIPS_UNREF( t );
/* gsf doesn't like more than one write active at once.
*/
g_mutex_lock( vips__global_lock );
if( !gsf_output_write( out, len, buf ) ) {
gsf_output_close( out );
g_mutex_unlock( vips__global_lock );
g_free( buf );
if( gsf_output_error( out ) )
vips_error( class->nickname,
"%s", gsf_output_error( out )->message );
return( -1 );
}
dz->bytes_written += len;
gsf_output_close( out );
#ifndef HAVE_GSF_ZIP64
if( iszip( dz->container ) ) {
/* Leave 3 entry headroom for blank.png and metadata files.
*/
if( dz->tree->file_count + 3 >= (unsigned int) USHRT_MAX ) {
g_mutex_unlock( vips__global_lock );
vips_error( class->nickname,
"%s", _( "too many files in zip" ) );
return( -1 );
}
/* Leave 16k headroom for blank.png and metadata files.
*/
if( estimate_zip_size( dz ) > (size_t) UINT_MAX - 16384) {
g_mutex_unlock( vips__global_lock );
vips_error( class->nickname,
"%s", _( "output file too large" ) );
return( -1 );
}
}
#endif /*HAVE_GSF_ZIP64*/
g_mutex_unlock( vips__global_lock );
g_free( buf );
return( 0 );
}
/* Free a pyramid.
*/
static void
layer_free( Layer *layer )
{
VIPS_FREEF( g_object_unref, layer->strip );
VIPS_FREEF( g_object_unref, layer->copy );
VIPS_FREEF( g_object_unref, layer->image );
VIPS_FREEF( layer_free, layer->below );
}
static void
vips_foreign_save_dz_dispose( GObject *gobject )
{
VipsForeignSaveDz *dz = (VipsForeignSaveDz *) gobject;
VIPS_UNREF( dz->target );
VIPS_FREEF( layer_free, dz->layer );
VIPS_FREEF( vips_gsf_tree_close, dz->tree );
VIPS_FREEF( g_object_unref, dz->out );
VIPS_FREE( dz->basename );
VIPS_FREE( dz->dirname );
VIPS_FREE( dz->root_name );
VIPS_FREE( dz->file_suffix );
G_OBJECT_CLASS( vips_foreign_save_dz_parent_class )->
dispose( gobject );
}
/* Build a pyramid.
*
* width/height is the size of this layer, real_* the subsection of the layer
* which is real pixels (as opposed to background).
*/
static Layer *
pyramid_build( VipsForeignSaveDz *dz, Layer *above,
int width, int height, VipsRect *real_pixels )
{
VipsForeignSave *save = VIPS_FOREIGN_SAVE( dz );
Layer *layer = VIPS_NEW( dz, Layer );
VipsRect strip;
int limit;
layer->dz = dz;
layer->width = width;
layer->height = height;
/* We need to output all possible tiles, even if they give no new pixels.
*/
layer->tiles_across = VIPS_ROUND_UP( width, dz->tile_step ) /
dz->tile_step;
layer->tiles_down = VIPS_ROUND_UP( height, dz->tile_step ) /
dz->tile_step;
layer->real_pixels = *real_pixels;
layer->image = NULL;
layer->strip = NULL;
layer->copy = NULL;
if( !above )
/* Top of pyramid.
*/
layer->sub = 1;
else
layer->sub = above->sub * 2;
layer->below = NULL;
layer->above = above;
/* We round the image size up to an even number to make x2 shrink
* easy.
*/
layer->image = vips_image_new();
if( vips_image_pipelinev( layer->image,
VIPS_DEMAND_STYLE_ANY, save->ready, NULL ) ) {
layer_free( layer );
return( NULL );
}
layer->image->Xsize = width + (width & 1);
layer->image->Ysize = height + (height & 1);
layer->strip = vips_region_new( layer->image );
layer->copy = vips_region_new( layer->image );
/* The regions will get used in the bg thread callback, so make sure
* we don't own them.
*/
vips__region_no_ownership( layer->strip );
vips__region_no_ownership( layer->copy );
/* Build a line of tiles here.
*
* Expand the strip if necessary to make sure we have an even
* number of lines.
*
* This is just the height of the first row of tiles, so only add 1*
* tile_margin.
*/
layer->y = 0;
layer->write_y = 0;
strip.left = 0;
strip.top = 0;
strip.width = layer->image->Xsize;
strip.height = dz->tile_size + dz->tile_margin;
if( (strip.height & 1) == 1 )
strip.height += 1;
if( vips_region_buffer( layer->strip, &strip ) ) {
layer_free( layer );
return( NULL );
}
switch( dz->depth ) {
case VIPS_FOREIGN_DZ_DEPTH_ONEPIXEL:
limit = 1;
break;
case VIPS_FOREIGN_DZ_DEPTH_ONETILE:
limit = dz->tile_size;
break;
case VIPS_FOREIGN_DZ_DEPTH_ONE:
limit = VIPS_MAX( width, height );
break;
default:
g_assert_not_reached();
/* Stop compiler warnings.
*/
limit = 1;
}
if( width > limit ||
height > limit ) {
/* Round up, so eg. a 5 pixel wide image becomes 3 a layer
* down.
*
* For the rect, round left/top down, round bottom/right up,
* so we get all possible pixels.
*/
VipsRect halfrect;
halfrect.left = real_pixels->left / 2;
halfrect.top = real_pixels->top / 2;
halfrect.width = (VIPS_RECT_RIGHT( real_pixels ) + 1) / 2 -
halfrect.left;
halfrect.height = (VIPS_RECT_BOTTOM( real_pixels ) + 1) / 2 -
halfrect.top;
if( !(layer->below = pyramid_build( dz, layer,
(width + 1) / 2, (height + 1) / 2,
&halfrect )) ) {
layer_free( layer );
return( NULL );
}
layer->n = layer->below->n + 1;
}
else
layer->n = 0;
#ifdef DEBUG
printf( "pyramid_build:\n" );
printf( "\tn = %d\n", layer->n );
printf( "\twidth = %d, height = %d\n", width, height );
printf( "\tXsize = %d, Ysize = %d\n",
layer->image->Xsize, layer->image->Ysize );
printf( "\ttiles_across = %d, tiles_down = %d\n",
layer->tiles_across, layer->tiles_down );
printf( "\treal_pixels.left = %d, real_pixels.top = %d\n",
real_pixels->left, real_pixels->top );
printf( "\treal_pixels.width = %d, real_pixels.height = %d\n",
real_pixels->width, real_pixels->height );
#endif /*DEBUG*/
return( layer );
}
static int
write_dzi( VipsForeignSaveDz *dz )
{
GsfOutput *out;
char buf[VIPS_PATH_MAX];
char *p;
vips_snprintf( buf, VIPS_PATH_MAX, "%s.dzi", dz->basename );
if( !(out = vips_gsf_path( dz->tree, buf, NULL ) ))
return( -1 );
vips_snprintf( buf, VIPS_PATH_MAX, "%s", dz->suffix + 1 );
if( (p = (char *) vips__find_rightmost_brackets( buf )) )
*p = '\0';
gsf_output_printf( out, "<?xml "
"version=\"1.0\" encoding=\"UTF-8\"?>\n" );
gsf_output_printf( out, "<Image "
"xmlns=\"http://schemas.microsoft.com/deepzoom/2008\"\n" );
gsf_output_printf( out, " Format=\"%s\"\n", buf );
gsf_output_printf( out, " Overlap=\"%d\"\n", dz->overlap );
gsf_output_printf( out, " TileSize=\"%d\"\n", dz->tile_size );
gsf_output_printf( out, " >\n" );
gsf_output_printf( out, " <Size \n" );
gsf_output_printf( out, " Height=\"%d\"\n", dz->layer->height );
gsf_output_printf( out, " Width=\"%d\"\n", dz->layer->width );
gsf_output_printf( out, " />\n" );
gsf_output_printf( out, "</Image>\n" );
(void) gsf_output_close( out );
g_object_unref( out );
return( 0 );
}
static int
write_properties( VipsForeignSaveDz *dz )
{
GsfOutput *out;
if( !(out = vips_gsf_path( dz->tree,
"ImageProperties.xml", NULL ) )) {
return( -1 );
}
gsf_output_printf( out, "<IMAGE_PROPERTIES "
"WIDTH=\"%d\" HEIGHT=\"%d\" NUMTILES=\"%d\" "
"NUMIMAGES=\"1\" VERSION=\"1.8\" TILESIZE=\"%d\" />\n",
dz->layer->width,
dz->layer->height,
dz->tile_count,
dz->tile_size );
(void) gsf_output_close( out );
g_object_unref( out );
return( 0 );
}
static int
write_blank( VipsForeignSaveDz *dz )
{
VipsForeignSave *save = (VipsForeignSave *) dz;
VipsImage *x, *t;
int n;
VipsArea *ones;
double *d;
double *bg;
int i;
GsfOutput *out;
/* Number of bands we will end up making. We need to set this in
* vips_black() to make sure we set Type correctly, otherwise we can
* try saving a B_W image as PNG, with disasterous results.
*/
bg = (double *) vips_area_get_data( VIPS_AREA( save->background ),
NULL, &n, NULL, NULL );
if( vips_black( &x, dz->tile_size, dz->tile_size, "bands", n, NULL ) )
return( -1 );
ones = vips_area_new_array( G_TYPE_DOUBLE, sizeof( double ), n );
d = (double *) vips_area_get_data( ones, NULL, NULL, NULL, NULL );
for( i = 0; i < n; i++ )
d[i] = 1.0;
if( vips_linear( x, &t, d, bg, n, NULL ) ) {
vips_area_unref( ones );
g_object_unref( x );
return( -1 );
}
vips_area_unref( ones );
g_object_unref( x );
x = t;
if( !(out = vips_gsf_path( dz->tree, "blank.png", NULL ) )) {
g_object_unref( x );
return( -1 );
}
if( write_image( dz, out, x, ".png" ) ) {
g_object_unref( out );
g_object_unref( x );
return( -1 );
}
g_object_unref( out );
g_object_unref( x );
return( 0 );
}
/* Write IIIF/IIF3 JSON metadata.
*/
static int
write_json( VipsForeignSaveDz *dz )
{
/* Can be NULL for memory output.
*/
const char *name = dz->basename ? dz->basename : "untitled";
/* dz->file_suffix has a leading "." character.
*/
const char *suffix = dz->file_suffix[0] == '.' ?
dz->file_suffix + 1 : dz->file_suffix;
GsfOutput *out;
int i;
if( !(out = vips_gsf_path( dz->tree, "info.json", NULL ) ))
return( -1 );
if( dz->layout == VIPS_FOREIGN_DZ_LAYOUT_IIIF3 )
gsf_output_printf( out,
"{\n"
" \"@context\": "
"\"http://iiif.io/api/image/3/context.json\",\n"
" \"id\": \"%s/%s\",\n"
" \"type\": \"ImageService3\",\n"
" \"profile\": \"level0\",\n"
" \"protocol\": \"http://iiif.io/api/image\",\n",
dz->id ? dz->id : "https://example.com/iiif",
name );
else
gsf_output_printf( out,
"{\n"
" \"@context\": "
"\"http://iiif.io/api/image/2/context.json\",\n"
" \"@id\": \"%s/%s\",\n"
" \"profile\": [\n"
" \"http://iiif.io/api/image/2/level0.json\",\n"
" {\n"
" \"formats\": [\n"
" \"%s\"\n"
" ],\n"
" \"qualities\": [\n"
" \"default\"\n"
" ]\n"
" }\n"
" ],\n"
" \"protocol\": \"http://iiif.io/api/image\",\n",
dz->id ? dz->id : "https://example.com/iiif",
name,
suffix );
/* "sizes" is needed for the full/ set of untiled images, which we
* don't yet support. Leave this commented out for now.
gsf_output_printf( out,
" \"sizes\": [\n" );
for( i = 0; i < dz->layer->n + 5; i++ ) {
gsf_output_printf( out,
" {\n"
" \"width\": %d,\n"
" \"height\": \"full\"\n"
" }",
1 << (i + 4) );
if( i != dz->layer->n - 4 )
gsf_output_printf( out, "," );
gsf_output_printf( out, "\n" );
}
gsf_output_printf( out,
" ],\n" );
*/
/* The set of pyramid layers we have written.
*/
gsf_output_printf( out,
" \"tiles\": [\n"
" {\n"
" \"scaleFactors\": [\n" );
for( i = 0; i < dz->layer->n; i++ ) {
gsf_output_printf( out,
" %d",
1 << i );
if( i != dz->layer->n - 1 )
gsf_output_printf( out, "," );
gsf_output_printf( out, "\n" );
}
gsf_output_printf( out,
" ],\n"
" \"width\": %d\n"
" }\n"
" ],\n", dz->tile_size );
gsf_output_printf( out,
" \"width\": %d,\n"
" \"height\": %d\n",
dz->layer->width,
dz->layer->height );
gsf_output_printf( out,
"}\n" );
(void) gsf_output_close( out );
g_object_unref( out );
return( 0 );
}
static int
write_vips_meta( VipsForeignSaveDz *dz )
{
VipsForeignSave *save = (VipsForeignSave *) dz;
char *dump;
GsfOutput *out;
if( !(dump = vips__xml_properties( save->ready )) )
return( -1 );
/* For deepzom the props must go inside the ${name}_files subdir, for
* gm and zoomify it can sit in the main folder.
*/
if( dz->layout == VIPS_FOREIGN_DZ_LAYOUT_DZ )
out = vips_gsf_path( dz->tree,
"vips-properties.xml", dz->root_name, NULL );
else
out = vips_gsf_path( dz->tree, "vips-properties.xml", NULL );
if( out == NULL ) {
g_free( dump );
return( -1 );
}
(void) gsf_output_write( out, strlen( dump ), (guchar *) dump );
(void) gsf_output_close( out );
g_object_unref( out );
g_free( dump );
return( 0 );
}
static void
build_scan_property( VipsDbuf *dbuf, VipsImage *image,
const char *vips_name, const char *szi_name )
{
const char *str;
GValue value = { 0 };
GValue save_value = { 0 };
GType type;
if( !vips_image_get_typeof( image, vips_name ) )
return;
if( vips_image_get( image, vips_name, &value ) )
return;
type = G_VALUE_TYPE( &value );
if( !g_value_type_transformable( type, VIPS_TYPE_SAVE_STRING ) ) {
g_value_unset( &value );
return;
}
g_value_init( &save_value, VIPS_TYPE_SAVE_STRING );
if( !g_value_transform( &value, &save_value ) ) {
g_value_unset( &value );
return;
}
g_value_unset( &value );
if( !(str = vips_value_get_save_string( &save_value )) ) {
g_value_unset( &save_value );
return;
}
if( !g_utf8_validate( str, -1, NULL ) ) {
g_value_unset( &save_value );
return;
}
vips_dbuf_writef( dbuf, " <property>\n" );
vips_dbuf_writef( dbuf, " <name>" );
vips_dbuf_write_amp( dbuf, szi_name );
vips_dbuf_writef( dbuf, "</name>\n" );
vips_dbuf_writef( dbuf, " <value type=\"%s\">",
g_type_name( type ) );
vips_dbuf_write_amp( dbuf, str );
vips_dbuf_writef( dbuf, "</value>\n" );
vips_dbuf_writef( dbuf, " </property>\n" );
g_value_unset( &save_value );
}
static char *scan_property_names[][2] = {
{ "openslide.vendor", "Vendor" },
{ "openslide.objective-power", "ObjectiveMagnification" },
{ "openslide.mpp-x", "MicronsPerPixelX" },
{ "openslide.mpp-y", "MicronsPerPixelY" },
{ "width", "ImageWidth" },
{ "height", "ImageHeight" }
};
/* Make the xml we write to scan-properties.xml in szi write.
* Free with g_free().
*/
static char *
build_scan_properties( VipsImage *image )
{
VipsDbuf dbuf;
char *date;
int i;
date = vips__get_iso8601();
vips_dbuf_init( &dbuf );
vips_dbuf_writef( &dbuf, "<?xml version=\"1.0\"?>\n" );
vips_dbuf_writef( &dbuf, "<image xmlns=\"http://www.pathozoom.com/szi\""
" date=\"%s\" version=\"1.0\">\n", date );
vips_dbuf_writef( &dbuf, " <properties>\n" );
g_free( date );
for( i = 0; i < VIPS_NUMBER( scan_property_names ); i++ )
build_scan_property( &dbuf, image,
scan_property_names[i][0],
scan_property_names[i][1] );
vips_dbuf_writef( &dbuf, " </properties>\n" );
vips_dbuf_writef( &dbuf, "</image>\n" );
return( (char *) vips_dbuf_steal( &dbuf, NULL ) );
}
static int
write_scan_properties( VipsForeignSaveDz *dz )
{
VipsForeignSave *save = (VipsForeignSave *) dz;
char *dump;
GsfOutput *out;
if( !(dump = build_scan_properties( save->ready )) )
return( -1 );
if( !(out = vips_gsf_path( dz->tree,
"scan-properties.xml", NULL ) )) {
g_free( dump );
return( -1 );
}
(void) gsf_output_write( out, strlen( dump ), (guchar *) dump );
(void) gsf_output_close( out );
g_object_unref( out );
g_free( dump );
return( 0 );
}
static void *
write_associated_images( VipsImage *image,
const char *field, GValue *value, void *a )
{
VipsForeignSaveDz *dz = (VipsForeignSaveDz *) a;
if( vips_isprefix( "openslide.associated.", field ) ) {
VipsImage *associated;
const char *p;
const char *q;
GsfOutput *out;
char buf[VIPS_PATH_MAX];
p = field + strlen( "openslide.associated." );
/* Make sure there are no '/' in the filename.
*/
if( (q = strrchr( p, '/' )) )
p = q + 1;
if( vips_image_get_image( image, field, &associated ) )
return( image );
vips_snprintf( buf, VIPS_PATH_MAX, "%s.jpg", p );
if( !(out = vips_gsf_path( dz->tree, buf,
"associated_images", NULL ) )) {
g_object_unref( associated );
return( image );
}
if( write_image( dz, out, associated, ".jpg" ) ) {
g_object_unref( out );
g_object_unref( associated );
return( image );
}
g_object_unref( out );
g_object_unref( associated );
}
return( NULL );
}
static int
write_associated( VipsForeignSaveDz *dz )
{
VipsForeignSave *save = (VipsForeignSave *) dz;
if( vips_image_map( save->ready, write_associated_images, dz ) )
return( -1 );
return( 0 );
}
/* Our state during a threaded write of a strip.
*/
typedef struct _Strip {
Layer *layer;
VipsImage *image;
/* Allocate the next tile on this boundary.
*/
int x;
} Strip;
static void
strip_free( Strip *strip )
{
g_object_unref( strip->image );
}
static void
strip_init( Strip *strip, Layer *layer )
{
VipsForeignSaveDz *dz = layer->dz;
VipsRect line, image;
strip->layer = layer;
strip->image = NULL;
strip->x = 0;
/* The image we wrap around our pixel buffer must be the full width,
* including any rounding up, since we must have contiguous pixels.
* We can trim the height down though.
*
* When we loop across the strip writing tiles we have to look out for
* the smaller width.
*/
image.left = 0;
image.top = 0;
image.width = layer->image->Xsize;
image.height = layer->height;
line.left = 0;
line.top = layer->y;
line.width = image.width;
line.height = dz->tile_size;
vips_rect_marginadjust( &line, dz->tile_margin );
vips_rect_intersectrect( &image, &line, &line );
if( !(strip->image = vips_image_new_from_memory(
VIPS_REGION_ADDR( layer->strip, 0, line.top ),
VIPS_IMAGE_SIZEOF_LINE( layer->image ) * line.height,
line.width, line.height,
layer->image->Bands, layer->image->BandFmt )) ) {
strip_free( strip );
return;
}
/* The strip needs to inherit the layer's metadata.
*/
if( vips__image_meta_copy( strip->image, layer->image ) ) {
strip_free( strip );
return;
}
/* Type needs to be set so we know how to convert for save correctly.
*/
strip->image->Type = layer->image->Type;
}
static int
strip_allocate( VipsThreadState *state, void *a, gboolean *stop )
{
Strip *strip = (Strip *) a;
Layer *layer = strip->layer;
VipsForeignSaveDz *dz = layer->dz;
VipsRect image;
#ifdef DEBUG_VERBOSE
printf( "strip_allocate\n" );
#endif /*DEBUG_VERBOSE*/
/* We can't test for allocated area empty, since it might just have
* bits of the left-hand overlap in and no new pixels. Safest to count
* tiles across.
*/
if( strip->x / dz->tile_step >= layer->tiles_across ) {
*stop = TRUE;
#ifdef DEBUG_VERBOSE
printf( "strip_allocate: done\n" );
#endif /*DEBUG_VERBOSE*/
return( 0 );
}
image.left = 0;
image.top = 0;
image.width = layer->width;
image.height = layer->height;
/* Position this tile.
*/
state->pos.left = strip->x;
state->pos.top = layer->y;
state->pos.width = dz->tile_size;
state->pos.height = dz->tile_size;
vips_rect_marginadjust( &state->pos, dz->tile_margin );
vips_rect_intersectrect( &image, &state->pos, &state->pos );
state->x = strip->x;
state->y = layer->y;
strip->x += dz->tile_step;
return( 0 );
}
/* Make an output object for a tile in the current layout.
*/
static GsfOutput *
tile_name( Layer *layer, int x, int y )
{
VipsForeignSaveDz *dz = layer->dz;
VipsForeignSave *save = (VipsForeignSave *) dz;
GsfOutput *out;
char name[VIPS_PATH_MAX];
char dirname[VIPS_PATH_MAX];
char dirname2[VIPS_PATH_MAX];
Layer *p;
int n;
switch( dz->layout ) {
case VIPS_FOREIGN_DZ_LAYOUT_DZ:
vips_snprintf( dirname, VIPS_PATH_MAX, "%d", layer->n );
vips_snprintf( name, VIPS_PATH_MAX,
"%d_%d%s", x, y, dz->file_suffix );
out = vips_gsf_path( dz->tree, name,
dz->root_name, dirname, NULL );
break;
case VIPS_FOREIGN_DZ_LAYOUT_ZOOMIFY:
/* We need to work out the tile number so we can calculate the
* directory to put this tile in.
*
* Tiles are numbered from 0 for the most-zoomed-out tile.
*/
n = 0;
/* Count all tiles in layers below this one.
*/
for( p = layer->below; p; p = p->below )
n += p->tiles_across * p->tiles_down;
/* And count tiles so far in this layer.
*/
n += y * layer->tiles_across + x;
vips_snprintf( dirname, VIPS_PATH_MAX, "TileGroup%d", n / 256 );
vips_snprintf( name, VIPS_PATH_MAX,
"%d-%d-%d%s", layer->n, x, y, dz->file_suffix );
/* Used at the end in ImageProperties.xml
*/
dz->tile_count += 1;
out = vips_gsf_path( dz->tree, name, dirname, NULL );
break;
case VIPS_FOREIGN_DZ_LAYOUT_GOOGLE:
vips_snprintf( dirname, VIPS_PATH_MAX, "%d", layer->n );
vips_snprintf( dirname2, VIPS_PATH_MAX, "%d", y );
vips_snprintf( name, VIPS_PATH_MAX,
"%d%s", x, dz->file_suffix );
out = vips_gsf_path( dz->tree, name, dirname, dirname2, NULL );
break;
case VIPS_FOREIGN_DZ_LAYOUT_IIIF:
case VIPS_FOREIGN_DZ_LAYOUT_IIIF3:
{
/* Tiles are addressed in full resolution coordinates, so
* scale up by layer->sub and dz->tile_size
*
* We always clip against the full-sized image, not the scaled
* up layer.
*
* This will break for overlap != 0, but hopefully no one will
* ever use that.
*/
int left = x * dz->tile_size * layer->sub;
int top = y * dz->tile_size * layer->sub;
int width = VIPS_MIN( dz->tile_size * layer->sub,
save->ready->Xsize - left );
int height = VIPS_MIN( dz->tile_size * layer->sub,
save->ready->Ysize - top );
vips_snprintf( dirname, VIPS_PATH_MAX, "%d,%d,%d,%d",
left, top, width, height );
if( dz->layout == VIPS_FOREIGN_DZ_LAYOUT_IIIF3 ) {
int xsize = VIPS_MIN( dz->tile_size,
layer->width - x * dz->tile_size );
int ysize = VIPS_MIN( dz->tile_size,
layer->height - y * dz->tile_size );
vips_snprintf( dirname2, VIPS_PATH_MAX, "%d,%d",
xsize, ysize );
}
else {
/* IIIF2 "size" is just real tile width, I think.
*/
int size = VIPS_MIN( dz->tile_size,
layer->width - x * dz->tile_size );
vips_snprintf( dirname2, VIPS_PATH_MAX, "%d,", size );
}
vips_snprintf( name, VIPS_PATH_MAX, "default%s",
dz->file_suffix );
/* "0" is rotation and is always 0.
*/
out = vips_gsf_path( dz->tree,
name, dirname, dirname2, "0", NULL );
}
break;
default:
g_assert_not_reached();
/* Stop compiler warnings.
*/
out = NULL;
}
#ifdef DEBUG_VERBOSE
printf( "tile_name: writing to %s\n", name );
#endif /*DEBUG_VERBOSE*/
return( out );
}
/* Test for tile nearly equal to background colour. In google maps mode, we
* skip blank background tiles.
*
* Don't use exactly equality since compression artefacts or noise can upset
* this.
*/
static gboolean
tile_equal( VipsImage *image, int threshold, VipsPel * restrict ink )
{
const int bytes = VIPS_IMAGE_SIZEOF_PEL( image );
VipsRect rect;
VipsRegion *region;
int x, y, b;
region = vips_region_new( image );
/* We know @image is part of a memory buffer, so this will be quick.
*/
rect.left = 0;
rect.top = 0;
rect.width = image->Xsize;
rect.height = image->Ysize;
if( vips_region_prepare( region, &rect ) ) {
g_object_unref( region );
return( FALSE );
}
for( y = 0; y < image->Ysize; y++ ) {
VipsPel * restrict p = VIPS_REGION_ADDR( region, 0, y );
for( x = 0; x < image->Xsize; x++ ) {
for( b = 0; b < bytes; b++ )
if( VIPS_ABS( p[b] - ink[b] ) > threshold ) {
g_object_unref( region );
return( FALSE );
}
p += bytes;
}
}
g_object_unref( region );
return( TRUE );
}
static int
strip_work( VipsThreadState *state, void *a )
{
Strip *strip = (Strip *) a;
Layer *layer = strip->layer;
VipsForeignSaveDz *dz = layer->dz;
VipsForeignSave *save = (VipsForeignSave *) dz;
VipsImage *x;
VipsImage *t;
GsfOutput *out;
#ifdef DEBUG_VERBOSE
printf( "strip_work\n" );
#endif /*DEBUG_VERBOSE*/
/* killed is checked by sink_disc, but that's only once per strip, and
* they can be huge. Check per output tile as well.
*/
if( vips_image_iskilled( save->in ) )
return( -1 );
/* If we are centering we may be outside the real pixels. Skip in
* this case, and the viewer will display blank.png for us.
*/
if( dz->centre ) {
VipsRect tile;
tile.left = state->x;
tile.top = state->y;
tile.width = dz->tile_size;
tile.height = dz->tile_size;
if( !vips_rect_overlapsrect( &tile, &layer->real_pixels ) ) {
#ifdef DEBUG_VERBOSE
printf( "strip_work: skipping tile %d x %d\n",
state->x / dz->tile_size,
state->y / dz->tile_size );
#endif /*DEBUG_VERBOSE*/
return( 0 );
}
}
g_assert( vips_object_sanity( VIPS_OBJECT( strip->image ) ) );
/* Extract relative to the strip top-left corner.
*/
if( vips_extract_area( strip->image, &x,
state->pos.left, 0,
state->pos.width, state->pos.height, NULL ) )
return( -1 );
if( dz->skip_blanks >= 0 &&
tile_equal( x, dz->skip_blanks, dz->ink ) ) {
g_object_unref( x );
#ifdef DEBUG_VERBOSE
printf( "strip_work: skipping blank tile %d x %d\n",
state->x / dz->tile_size,
state->y / dz->tile_size );
#endif /*DEBUG_VERBOSE*/
return( 0 );
}
/* Google tiles need to be padded up to tilesize.
*/
if( dz->layout == VIPS_FOREIGN_DZ_LAYOUT_GOOGLE ) {
if( vips_embed( x, &t, 0, 0, dz->tile_size, dz->tile_size,
"background", save->background,
NULL ) ) {
g_object_unref( x );
return( -1 );
}
g_object_unref( x );
x = t;
}
/* we need to single-thread around calls to gsf.
*/
g_mutex_lock( vips__global_lock );
out = tile_name( layer,
state->x / dz->tile_step, state->y / dz->tile_step );
g_mutex_unlock( vips__global_lock );
/* vips_gsf_path() can return NULL when it exceeds the path limits.
*/
if( out == NULL ) {
g_object_unref( x );
return( -1 );
}
vips_image_set_int( x, VIPS_META_CONCURRENCY, 1 );
if( write_image( dz, out, x, dz->suffix ) ) {
g_object_unref( out );
g_object_unref( x );
return( -1 );
}
g_object_unref( out );
g_object_unref( x );
#ifdef DEBUG_VERBOSE
printf( "strip_work: success\n" );
#endif /*DEBUG_VERBOSE*/
return( 0 );
}
/* Write a line of tiles with a threadpool.
*/
static int
strip_save( Layer *layer )
{
Strip strip;
#ifdef DEBUG
printf( "strip_save: n = %d, y = %d\n", layer->n, layer->y );
#endif /*DEBUG*/
strip_init( &strip, layer );
if( vips_threadpool_run( strip.image,
vips_thread_state_new, strip_allocate, strip_work, NULL,
&strip ) ) {
strip_free( &strip );
return( -1 );
}
strip_free( &strip );
#ifdef DEBUG
printf( "strip_save: success\n" );
#endif /*DEBUG*/
return( 0 );
}
/* A strip has filled, but the rightmost column and the bottom-most row may
* not have been if we've rounded the size up.
*
* Fill them, if necessary, by copying the previous row/column.
*/
static void
layer_generate_extras( Layer *layer )
{
VipsRegion *strip = layer->strip;
/* We only work for full-width strips.
*/
g_assert( strip->valid.width == layer->image->Xsize );
if( layer->width < layer->image->Xsize ) {
int ps = VIPS_IMAGE_SIZEOF_PEL( strip->im );
int b, y;
/* Need to add a right-most column.
*/
for( y = 0; y < strip->valid.height; y++ ) {
VipsPel *p = VIPS_REGION_ADDR( strip,
layer->width - 1, strip->valid.top + y );
VipsPel *q = p + ps;
for( b = 0; b < ps; b++ )
q[b] = p[b];
}
}
if( layer->height < layer->image->Ysize ) {
VipsRect last;
/* The last two lines of the image.
*/
last.left = 0;
last.top = layer->image->Ysize - 2;
last.width = layer->image->Xsize;
last.height = 2;
/* Do we have them both? Fill the last with the next-to-last.
*/
vips_rect_intersectrect( &last, &strip->valid, &last );
if( last.height == 2 ) {
last.height = 1;
vips_region_copy( strip, strip, &last,
0, last.top + 1 );
}
}
}
static int strip_arrived( Layer *layer );
/* Shrink what pixels we can from this strip into the layer below. If the
* strip below fills, recurse.
*/
static int
strip_shrink( Layer *layer )
{
Layer *below = layer->below;
VipsRegion *from = layer->strip;
VipsRegion *to = below->strip;
VipsForeignSaveDz *dz = layer->dz;
VipsRegionShrink region_shrink = dz->region_shrink;
VipsRect target;
VipsRect source;
#ifdef DEBUG
printf( "strip_shrink: %d lines in layer %d to layer %d\n",
from->valid.height, layer->n, below->n );
#endif /*DEBUG*/
/* We may have an extra column of pixels on the right or
* bottom that need filling: generate them.
*/
layer_generate_extras( layer );
/* Our pixels might cross a strip boundary in the layer below, so we
* have to write repeatedly until we run out of pixels.
*/
for(;;) {
/* The pixels the layer below needs.
*/
target.left = 0;
target.top = below->write_y;
target.width = below->image->Xsize;
target.height = to->valid.height;
vips_rect_intersectrect( &target, &to->valid, &target );
/* Those pixels need this area of this layer.
*/
source.left = target.left * 2;
source.top = target.top * 2;
source.width = target.width * 2;
source.height = target.height * 2;
/* Of which we have these available.
*/
vips_rect_intersectrect( &source, &from->valid, &source );
/* So these are the pixels in the layer below we can provide.
*/
target.left = source.left / 2;
target.top = source.top / 2;
target.width = source.width / 2;
target.height = source.height / 2;
/* None? All done.
*/
if( vips_rect_isempty( &target ) )
break;
(void) vips_region_shrink_method( from, to,
&target, region_shrink );
below->write_y += target.height;
/* If we've filled the strip below, let it know.
* We can either fill the region, if it's somewhere half-way
* down the image, or, if it's at the bottom, get to the last
* real line of pixels.
*/
if( below->write_y == VIPS_RECT_BOTTOM( &to->valid ) ||
below->write_y == below->height ) {
if( strip_arrived( below ) )
return( -1 );
}
}
return( 0 );
}
/* A new strip has arrived! The strip has enough pixels in to write a line of
* tiles.
*
* - write a line of tiles
* - shrink what we can to the layer below
* - move our strip down by the tile step
* - copy the overlap with the previous strip
*/
static int
strip_arrived( Layer *layer )
{
VipsForeignSaveDz *dz = layer->dz;
VipsRect new_strip;
VipsRect overlap;
VipsRect image_area;
#ifdef DEBUG
printf( "strip_arrived: layer %d, strip at %d, height %d\n",
layer->n, layer->y, layer->strip->valid.height );
#endif /*DEBUG*/
if( strip_save( layer ) )
return( -1 );
if( layer->below &&
strip_shrink( layer ) )
return( -1 );
/* Position our strip down the image.
*
* Expand the strip if necessary to make sure we have an even
* number of lines.
*/
layer->y += dz->tile_step;
new_strip.left = 0;
new_strip.top = layer->y - dz->tile_margin;
new_strip.width = layer->image->Xsize;
new_strip.height = dz->tile_size + 2 * dz->tile_margin;
image_area.left = 0;
image_area.top = 0;
image_area.width = layer->image->Xsize;
image_area.height = layer->image->Ysize;
vips_rect_intersectrect( &new_strip, &image_area, &new_strip );
if( (new_strip.height & 1) == 1 )
new_strip.height += 1;
/* We may exactly hit the bottom of the real image (ie. before borders
* have been possibly expanded by 1 pixel). In this case, we'll not
* be able to do the expansion in layer_generate_extras(), since the
* region won't be large enough, and we'll not get another chance
* since this is the bottom.
*
* Add another scanline if this has happened.
*/
if( VIPS_RECT_BOTTOM( &new_strip ) == layer->height )
new_strip.height = layer->image->Ysize - new_strip.top;
/* What pixels that we will need do we already have? Save them in
* overlap.
*/
vips_rect_intersectrect( &new_strip, &layer->strip->valid, &overlap );
if( !vips_rect_isempty( &overlap ) ) {
if( vips_region_buffer( layer->copy, &overlap ) )
return( -1 );
vips_region_copy( layer->strip, layer->copy,
&overlap, overlap.left, overlap.top );
}
if( !vips_rect_isempty( &new_strip ) ) {
if( vips_region_buffer( layer->strip, &new_strip ) )
return( -1 );
/* And copy back again.
*/
if( !vips_rect_isempty( &overlap ) )
vips_region_copy( layer->copy, layer->strip,
&overlap, overlap.left, overlap.top );
}
return( 0 );
}
/* The image has been completely written. Flush any strips which might have
* overlaps in.
*/
static int
strip_flush( Layer *layer )
{
if( layer->y < layer->height )
if( strip_save( layer ) )
return( -1 );
if( layer->below )
if( strip_flush( layer->below ) )
return( -1 );
return( 0 );
}
/* Another strip of image pixels from vips_sink_disc(). Write into the top
* pyramid layer.
*/
static int
pyramid_strip( VipsRegion *region, VipsRect *area, void *a )
{
VipsForeignSaveDz *dz = (VipsForeignSaveDz *) a;
Layer *layer = dz->layer;
#ifdef DEBUG
printf( "pyramid_strip: strip at %d, height %d\n",
area->top, area->height );
#endif /*DEBUG*/
for(;;) {
VipsRect *to = &layer->strip->valid;
VipsRect target;
/* The bit of strip that needs filling.
*/
target.left = 0;
target.top = layer->write_y;
target.width = layer->image->Xsize;
target.height = to->height;
vips_rect_intersectrect( &target, to, &target );
/* Clip against what we have available.
*/
vips_rect_intersectrect( &target, area, &target );
/* Have we written all the pixels we were given? We are done.
*/
if( vips_rect_isempty( &target ) )
break;
/* And copy those pixels in.
*
* FIXME: If the strip fits inside the region we've just
* received, we could skip the copy. Will this happen very
* often? Unclear.
*/
vips_region_copy( region, layer->strip,
&target, target.left, target.top );
layer->write_y += target.height;
/* We can either fill the strip, if it's somewhere half-way
* down the image, or, if it's at the bottom, get to the last
* real line of pixels.
*/
if( layer->write_y == VIPS_RECT_BOTTOM( to ) ||
layer->write_y == layer->height ) {
if( strip_arrived( layer ) )
return( -1 );
}
}
/* If we've reached the bottom of the image, we won't get called again.
*
* However, there may be some unwritten pixels in the pyramid still!
* Suppose a layer is exactly a multiple of tile_step in height.
* When we finished that last strip, we will have copied the last few
* lines of overlap over into the top of the next row. Deepzoom says we
* must flush these half-written strips to the output.
*/
if( layer->write_y == layer->height ) {
#ifdef DEBUG
printf( "pyramid_strip: flushing ..\n" );
#endif /*DEBUG*/
if( strip_flush( layer ) )
return( -1 );
}
return( 0 );
}
static int
vips_foreign_save_dz_build( VipsObject *object )
{
VipsForeignSave *save = (VipsForeignSave *) object;
VipsForeignSaveDz *dz = (VipsForeignSaveDz *) object;
VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( dz );
VipsRect real_pixels;
char *p;
/* Google, zoomify and iiif default to zero overlap, ".jpg".
*/
if( dz->layout == VIPS_FOREIGN_DZ_LAYOUT_ZOOMIFY ||
dz->layout == VIPS_FOREIGN_DZ_LAYOUT_GOOGLE ||
dz->layout == VIPS_FOREIGN_DZ_LAYOUT_IIIF ||
dz->layout == VIPS_FOREIGN_DZ_LAYOUT_IIIF3 ) {
if( !vips_object_argument_isset( object, "overlap" ) )
dz->overlap = 0;
if( !vips_object_argument_isset( object, "suffix" ) )
VIPS_SETSTR( dz->suffix, ".jpg" );
}
/* Google and zoomify default to 256 pixel tiles.
*/
if( dz->layout == VIPS_FOREIGN_DZ_LAYOUT_ZOOMIFY ||
dz->layout == VIPS_FOREIGN_DZ_LAYOUT_GOOGLE ) {
if( !vips_object_argument_isset( object, "tile_size" ) )
dz->tile_size = 256;
}
/* Some iiif writers default to 256, some to 512. We pick 512.
*/
if( dz->layout == VIPS_FOREIGN_DZ_LAYOUT_IIIF ||
dz->layout == VIPS_FOREIGN_DZ_LAYOUT_IIIF3 ) {
if( !vips_object_argument_isset( object, "tile_size" ) )
dz->tile_size = 512;
}
/* skip_blanks defaults to 5 in google mode.
*/
if( dz->layout == VIPS_FOREIGN_DZ_LAYOUT_GOOGLE &&
!vips_object_argument_isset( object, "skip_blanks" ) )
dz->skip_blanks = 5;
/* Our tile layout.
*/
if( dz->layout == VIPS_FOREIGN_DZ_LAYOUT_DZ ) {
dz->tile_margin = dz->overlap;
dz->tile_step = dz->tile_size;
}
else {
dz->tile_margin = 0;
dz->tile_step = dz->tile_size - dz->overlap;
}
if( dz->tile_step <= 0 ) {
vips_error( "dzsave", "%s", _( "overlap too large" ) );
return( -1 );
}
/* Default to white background. vips_foreign_save_init() defaults to
* black.
*/
if( !vips_object_argument_isset( object, "background" ) ) {
VipsArrayDouble *background;
/* Using g_object_set() to set an input param in build will
* change the hash and confuse caching, but we don't cache
* savers, so it's fine.
*/
background = vips_array_double_newv( 1, 255.0 );
g_object_set( object, "background", background, NULL );
vips_area_unref( VIPS_AREA( background ) );
}
/* DeepZoom stops at 1x1 pixels, others when the image fits within a
* tile.
*/
if( dz->layout == VIPS_FOREIGN_DZ_LAYOUT_DZ ) {
if( !vips_object_argument_isset( object, "depth" ) )
dz->depth = VIPS_FOREIGN_DZ_DEPTH_ONEPIXEL;
}
else
if( !vips_object_argument_isset( object, "depth" ) )
dz->depth = VIPS_FOREIGN_DZ_DEPTH_ONETILE;
if( VIPS_OBJECT_CLASS( vips_foreign_save_dz_parent_class )->
build( object ) )
return( -1 );
/* Optional rotate.
*/
{
VipsImage *z;
if( vips_rot( save->ready, &z, dz->angle, NULL ) )
return( -1 );
VIPS_UNREF( save->ready );
save->ready = z;
}
/* We use ink to check for blank tiles.
*/
if( dz->skip_blanks >= 0 ) {
if( !(dz->ink = vips__vector_to_ink(
class->nickname, save->ready,
VIPS_AREA( save->background )->data, NULL,
VIPS_AREA( save->background )->n )) )
return( -1 );
}
/* The real pixels we have from our input. This is about to get
* expanded with background.
*/
real_pixels.left = 0;
real_pixels.top = 0;
real_pixels.width = save->ready->Xsize;
real_pixels.height = save->ready->Ysize;
/* For centred images, imagine shrinking so that the image fits in a
* single tile, centering in that tile, then expanding back again.
*/
if( dz->layout == VIPS_FOREIGN_DZ_LAYOUT_GOOGLE &&
dz->centre ) {
VipsImage *z;
Layer *layer;
int n_layers;
int size;
if( !(layer = pyramid_build( dz, NULL,
save->ready->Xsize, save->ready->Ysize,
&real_pixels )) )
return( -1 );
n_layers = layer->n;
/* This would cause interesting problems.
*/
g_assert( n_layers < 30 );
layer_free( layer );
size = dz->tile_size * (1 << n_layers);
real_pixels.left = (size - save->ready->Xsize) / 2;
real_pixels.top = (size - save->ready->Ysize) / 2;
if( vips_embed( save->ready, &z,
real_pixels.left, real_pixels.top,
size, size,
"background", save->background,
NULL ) )
return( -1 );
VIPS_UNREF( save->ready );
save->ready = z;
#ifdef DEBUG
printf( "centre: centring within a %d x %d image\n",
size, size );
#endif /*DEBUG*/
}
#ifdef DEBUG
printf( "vips_foreign_save_dz_build: tile_size == %d\n",
dz->tile_size );
printf( "vips_foreign_save_dz_build: overlap == %d\n",
dz->overlap );
printf( "vips_foreign_save_dz_build: tile_margin == %d\n",
dz->tile_margin );
printf( "vips_foreign_save_dz_build: tile_step == %d\n",
dz->tile_step );
#endif /*DEBUG*/
/* Init basename and dirname from the associated filesystem names, if
* we can.
*/
{
const char *filename = dz->filename ?
dz->filename :
vips_connection_filename( VIPS_CONNECTION( dz->target ) );
if( !vips_object_argument_isset( object, "basename" ) ) {
if( filename )
dz->basename = g_path_get_basename( filename );
else
dz->basename = g_strdup( "untitled" );
}
if( !vips_object_argument_isset( object, "dirname" ) ) {
if( filename )
dz->dirname = g_path_get_dirname( filename );
}
}
/* Remove any [options] from basename.
*/
if( (p = (char *) vips__find_rightmost_brackets( dz->basename )) )
*p = '\0';
/* If we're writing thing.zip or thing.szi, default to zip
* container.
*/
if( (p = strrchr( dz->basename, '.' )) ) {
if( !vips_object_argument_isset( object, "container" ) ) {
if( g_ascii_strcasecmp( p + 1, "zip" ) == 0 )
dz->container = VIPS_FOREIGN_DZ_CONTAINER_ZIP;
if( g_ascii_strcasecmp( p + 1, "szi" ) == 0 )
dz->container = VIPS_FOREIGN_DZ_CONTAINER_SZI;
}
/* Remove any legal suffix. We don't remove all suffixes
* since we might be writing to a dirname with a dot in.
*/
if( g_ascii_strcasecmp( p + 1, "zip" ) == 0 ||
g_ascii_strcasecmp( p + 1, "szi" ) == 0 ||
g_ascii_strcasecmp( p + 1, "dz" ) == 0 )
*p = '\0';
}
/* Build the skeleton of the image pyramid.
*/
if( !(dz->layer = pyramid_build( dz, NULL,
save->ready->Xsize, save->ready->Ysize, &real_pixels )) )
return( -1 );
if( dz->layout == VIPS_FOREIGN_DZ_LAYOUT_DZ )
dz->root_name = g_strdup_printf( "%s_files", dz->basename );
else
dz->root_name = g_strdup( dz->basename );
/* Drop any [options] from @suffix.
*/
dz->file_suffix = g_strdup( dz->suffix );
if( (p = (char *) vips__find_rightmost_brackets( dz->file_suffix )) )
*p = '\0';
/* Make the thing we write the tiles into.
*/
switch( dz->container ) {
case VIPS_FOREIGN_DZ_CONTAINER_FS:
{
GsfOutput *out;
char name[VIPS_PATH_MAX];
/* For filesystem output of deepzoom, we write
* dirname/basename_files/ and dirname/basename.dzi, ie. the
* output does not go into a subdirectory.
*/
if( dz->layout == VIPS_FOREIGN_DZ_LAYOUT_DZ )
vips_snprintf( name, VIPS_PATH_MAX,
"%s", dz->dirname );
else
vips_snprintf( name, VIPS_PATH_MAX,
"%s/%s", dz->dirname, dz->basename );
if( !(out = (GsfOutput *)
gsf_output_dir_new( name, NULL )) ) {
return( -1 );
}
dz->tree = vips_gsf_tree_new( out, 0 );
}
break;
case VIPS_FOREIGN_DZ_CONTAINER_ZIP:
case VIPS_FOREIGN_DZ_CONTAINER_SZI:
{
GsfOutput *zip;
GsfOutput *out2;
GError *error = NULL;
/* We can have dzsave("x.zip", container="fs"), ie. zip output
* from write to file. Make a target if we need one.
*/
if( !dz->target ) {
if( !(dz->target =
vips_target_new_to_file( dz->filename )) )
return( -1 );
}
/* Can be memory, a file (not a directory tree), pipe, etc.
*/
dz->out = gsf_output_target_new( dz->target );
if( !(zip = (GsfOutput *)
gsf_outfile_zip_new( dz->out, &error )) ) {
vips_g_error( &error );
return( -1 );
}
/* Make the base directory inside the zip. All stuff goes into
* this.
*/
out2 = gsf_outfile_new_child_full( (GsfOutfile *) zip,
dz->basename, TRUE,
"compression-level", GSF_ZIP_STORED,
NULL );
#ifndef HAVE_GSF_DEFLATE_LEVEL
if( dz->compression > 0 ) {
g_warning( "%s",
_( "deflate-level not supported by libgsf, "
"using default compression" ) );
dz->compression = -1;
}
#endif /*HAVE_GSF_DEFLATE_LEVEL*/
dz->tree = vips_gsf_tree_new( out2, dz->compression );
/* Note the thing that will need closing up on exit.
*/
dz->tree->container = zip;
}
break;
default:
g_assert_not_reached();
}
if( vips_sink_disc( save->ready, pyramid_strip, dz ) )
return( -1 );
switch( dz->layout ) {
case VIPS_FOREIGN_DZ_LAYOUT_DZ:
if( write_dzi( dz ) )
return( -1 );
break;
case VIPS_FOREIGN_DZ_LAYOUT_ZOOMIFY:
if( write_properties( dz ) )
return( -1 );
break;
case VIPS_FOREIGN_DZ_LAYOUT_GOOGLE:
if( write_blank( dz ) )
return( -1 );
break;
case VIPS_FOREIGN_DZ_LAYOUT_IIIF:
case VIPS_FOREIGN_DZ_LAYOUT_IIIF3:
if( write_json( dz ) )
return( -1 );
break;
default:
g_assert_not_reached();
}
if( write_vips_meta( dz ) )
return( -1 );
if( dz->container == VIPS_FOREIGN_DZ_CONTAINER_SZI &&
write_scan_properties( dz ) )
return( -1 );
if( dz->container == VIPS_FOREIGN_DZ_CONTAINER_SZI &&
write_associated( dz ) )
return( -1 );
/* Shut down the output to flush everything.
*/
if( vips_gsf_tree_close( dz->tree ) )
return( -1 );
dz->tree = NULL;
/* unref out to force flush in gsf_output_target_close().
*/
VIPS_UNREF( dz->out );
return( 0 );
}
/* Save a bit of typing.
*/
#define UC VIPS_FORMAT_UCHAR
#define C VIPS_FORMAT_CHAR
#define US VIPS_FORMAT_USHORT
#define S VIPS_FORMAT_SHORT
#define UI VIPS_FORMAT_UINT
#define I VIPS_FORMAT_INT
#define F VIPS_FORMAT_FLOAT
#define X VIPS_FORMAT_COMPLEX
#define D VIPS_FORMAT_DOUBLE
#define DX VIPS_FORMAT_DPCOMPLEX
static VipsBandFormat bandfmt_dz[10] = {
/* Band format: UC C US S UI I F X D DX */
/* Promotion: */ UC, C, US, S, UI, I, F, F, D, D
};
static const char *dz_suffs[] = { ".dz", ".szi", NULL };
static void
vips_foreign_save_dz_class_init( VipsForeignSaveDzClass *class )
{
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
VipsObjectClass *object_class = (VipsObjectClass *) class;
VipsForeignClass *foreign_class = (VipsForeignClass *) class;
VipsForeignSaveClass *save_class = (VipsForeignSaveClass *) class;
gobject_class->dispose = vips_foreign_save_dz_dispose;
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
object_class->nickname = "dzsave_base";
object_class->description = _( "save image to deep zoom format" );
object_class->build = vips_foreign_save_dz_build;
foreign_class->suffs = dz_suffs;
save_class->saveable = VIPS_SAVEABLE_ANY;
save_class->format_table = bandfmt_dz;
save_class->coding[VIPS_CODING_LABQ] = TRUE;
VIPS_ARG_STRING( class, "basename", 2,
_( "Base name" ),
_( "Base name to save to" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsForeignSaveDz, basename ),
NULL );
VIPS_ARG_ENUM( class, "layout", 8,
_( "Layout" ),
_( "Directory layout" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsForeignSaveDz, layout ),
VIPS_TYPE_FOREIGN_DZ_LAYOUT, VIPS_FOREIGN_DZ_LAYOUT_DZ );
VIPS_ARG_STRING( class, "suffix", 9,
_( "Suffix" ),
_( "Filename suffix for tiles" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsForeignSaveDz, suffix ),
".jpeg" );
VIPS_ARG_INT( class, "overlap", 10,
_( "Overlap" ),
_( "Tile overlap in pixels" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsForeignSaveDz, overlap ),
0, 8192, 1 );
VIPS_ARG_INT( class, "tile_size", 11,
_( "Tile size" ),
_( "Tile size in pixels" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsForeignSaveDz, tile_size ),
1, 8192, 254 );
VIPS_ARG_ENUM( class, "depth", 13,
_( "Depth" ),
_( "Pyramid depth" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsForeignSaveDz, depth ),
VIPS_TYPE_FOREIGN_DZ_DEPTH, VIPS_FOREIGN_DZ_DEPTH_ONEPIXEL );
VIPS_ARG_BOOL( class, "centre", 13,
_( "Center" ),
_( "Center image in tile" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsForeignSaveDz, centre ),
FALSE );
VIPS_ARG_ENUM( class, "angle", 14,
_( "Angle" ),
_( "Rotate image during save" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsForeignSaveDz, angle ),
VIPS_TYPE_ANGLE, VIPS_ANGLE_D0 );
VIPS_ARG_ENUM( class, "container", 15,
_( "Container" ),
_( "Pyramid container type" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsForeignSaveDz, container ),
VIPS_TYPE_FOREIGN_DZ_CONTAINER, VIPS_FOREIGN_DZ_CONTAINER_FS );
VIPS_ARG_INT( class, "compression", 17,
_( "Compression" ),
_( "ZIP deflate compression level" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsForeignSaveDz, compression ),
-1, 9, 0 );
VIPS_ARG_ENUM( class, "region_shrink", 18,
_( "Region shrink" ),
_( "Method to shrink regions" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsForeignSaveDz, region_shrink ),
VIPS_TYPE_REGION_SHRINK, VIPS_REGION_SHRINK_MEAN );
VIPS_ARG_INT( class, "skip_blanks", 19,
_( "Skip blanks" ),
_( "Skip tiles which are nearly equal to the background" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsForeignSaveDz, skip_blanks ),
-1, 65535, -1 );
VIPS_ARG_BOOL( class, "no_strip", 20,
_( "No strip" ),
_( "Don't strip tile metadata" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsForeignSaveDz, no_strip ),
FALSE );
VIPS_ARG_STRING( class, "id", 21,
_( "id" ),
_( "Resource ID" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsForeignSaveDz, id ),
"https://example.com/iiif" );
/* How annoying. We stupidly had these in earlier versions.
*/
VIPS_ARG_STRING( class, "dirname", 1,
_( "Directory name" ),
_( "Directory name to save to" ),
VIPS_ARGUMENT_OPTIONAL_INPUT | VIPS_ARGUMENT_DEPRECATED,
G_STRUCT_OFFSET( VipsForeignSaveDz, dirname ),
NULL );
VIPS_ARG_INT( class, "tile_width", 12,
_( "Tile width" ),
_( "Tile width in pixels" ),
VIPS_ARGUMENT_OPTIONAL_INPUT | VIPS_ARGUMENT_DEPRECATED,
G_STRUCT_OFFSET( VipsForeignSaveDz, tile_size ),
1, 8192, 254 );
VIPS_ARG_INT( class, "tile_height", 12,
_( "Tile height" ),
_( "Tile height in pixels" ),
VIPS_ARGUMENT_OPTIONAL_INPUT | VIPS_ARGUMENT_DEPRECATED,
G_STRUCT_OFFSET( VipsForeignSaveDz, tile_size ),
1, 8192, 254 );
VIPS_ARG_BOOL( class, "properties", 16,
_( "Properties" ),
_( "Write a properties file to the output directory" ),
VIPS_ARGUMENT_OPTIONAL_INPUT | VIPS_ARGUMENT_DEPRECATED,
G_STRUCT_OFFSET( VipsForeignSaveDz, properties ),
FALSE );
}
static void
vips_foreign_save_dz_init( VipsForeignSaveDz *dz )
{
VIPS_SETSTR( dz->suffix, ".jpeg" );
dz->layout = VIPS_FOREIGN_DZ_LAYOUT_DZ;
dz->overlap = 1;
dz->tile_size = 254;
dz->tile_count = 0;
dz->depth = VIPS_FOREIGN_DZ_DEPTH_ONEPIXEL;
dz->angle = VIPS_ANGLE_D0;
dz->container = VIPS_FOREIGN_DZ_CONTAINER_FS;
dz->compression = 0;
dz->region_shrink = VIPS_REGION_SHRINK_MEAN;
dz->skip_blanks = -1;
}
typedef struct _VipsForeignSaveDzTarget {
VipsForeignSaveDz parent_object;
VipsTarget *target;
} VipsForeignSaveDzTarget;
typedef VipsForeignSaveDzClass VipsForeignSaveDzTargetClass;
G_DEFINE_TYPE( VipsForeignSaveDzTarget, vips_foreign_save_dz_target,
vips_foreign_save_dz_get_type() );
static int
vips_foreign_save_dz_target_build( VipsObject *object )
{
VipsForeignSaveDz *dz = (VipsForeignSaveDz *) object;
VipsForeignSaveDzTarget *target = (VipsForeignSaveDzTarget *) object;
dz->target = target->target;
g_object_ref( dz->target );
if( VIPS_OBJECT_CLASS( vips_foreign_save_dz_target_parent_class )->
build( object ) )
return( -1 );
return( 0 );
}
static void
vips_foreign_save_dz_target_class_init( VipsForeignSaveDzTargetClass *class )
{
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
VipsObjectClass *object_class = (VipsObjectClass *) class;
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
object_class->nickname = "dzsave_target";
object_class->description = _( "save image to deepzoom target" );
object_class->build = vips_foreign_save_dz_target_build;
VIPS_ARG_OBJECT( class, "target", 1,
_( "Target" ),
_( "Target to save to" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsForeignSaveDzTarget, target ),
VIPS_TYPE_TARGET );
}
static void
vips_foreign_save_dz_target_init( VipsForeignSaveDzTarget *target )
{
VipsForeignSaveDz *dz = (VipsForeignSaveDz *) target;
/* zip default for target output.
*/
dz->container = VIPS_FOREIGN_DZ_CONTAINER_ZIP;
}
typedef struct _VipsForeignSaveDzFile {
VipsForeignSaveDz parent_object;
/* Filename for save.
*/
char *filename;
} VipsForeignSaveDzFile;
typedef VipsForeignSaveDzClass VipsForeignSaveDzFileClass;
G_DEFINE_TYPE( VipsForeignSaveDzFile, vips_foreign_save_dz_file,
vips_foreign_save_dz_get_type() );
static int
vips_foreign_save_dz_file_build( VipsObject *object )
{
VipsForeignSaveDz *dz = (VipsForeignSaveDz *) object;
VipsForeignSaveDzFile *file = (VipsForeignSaveDzFile *) object;
dz->filename = file->filename;
if( VIPS_OBJECT_CLASS( vips_foreign_save_dz_file_parent_class )->
build( object ) )
return( -1 );
return( 0 );
}
static void
vips_foreign_save_dz_file_class_init( VipsForeignSaveDzFileClass *class )
{
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
VipsObjectClass *object_class = (VipsObjectClass *) class;
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
object_class->nickname = "dzsave";
object_class->description = _( "save image to deepzoom file" );
object_class->build = vips_foreign_save_dz_file_build;
VIPS_ARG_STRING( class, "filename", 1,
_( "Filename" ),
_( "Filename to save to" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsForeignSaveDzFile, filename ),
NULL );
}
static void
vips_foreign_save_dz_file_init( VipsForeignSaveDzFile *file )
{
}
typedef struct _VipsForeignSaveDzBuffer {
VipsForeignSaveDz parent_object;
VipsArea *buf;
} VipsForeignSaveDzBuffer;
typedef VipsForeignSaveDzClass VipsForeignSaveDzBufferClass;
G_DEFINE_TYPE( VipsForeignSaveDzBuffer, vips_foreign_save_dz_buffer,
vips_foreign_save_dz_get_type() );
static int
vips_foreign_save_dz_buffer_build( VipsObject *object )
{
VipsForeignSaveDz *dz = (VipsForeignSaveDz *) object;
VipsForeignSaveDzBuffer *buffer = (VipsForeignSaveDzBuffer *) object;
VipsBlob *blob;
if( !(dz->target = vips_target_new_to_memory()) )
return( -1 );
if( VIPS_OBJECT_CLASS( vips_foreign_save_dz_buffer_parent_class )->
build( object ) )
return( -1 );
g_object_get( dz->target, "blob", &blob, NULL );
g_object_set( buffer, "buffer", blob, NULL );
vips_area_unref( VIPS_AREA( blob ) );
return( 0 );
}
static void
vips_foreign_save_dz_buffer_class_init( VipsForeignSaveDzBufferClass *class )
{
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
VipsObjectClass *object_class = (VipsObjectClass *) class;
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
object_class->nickname = "dzsave_buffer";
object_class->description = _( "save image to dz buffer" );
object_class->build = vips_foreign_save_dz_buffer_build;
VIPS_ARG_BOXED( class, "buffer", 1,
_( "Buffer" ),
_( "Buffer to save to" ),
VIPS_ARGUMENT_REQUIRED_OUTPUT,
G_STRUCT_OFFSET( VipsForeignSaveDzBuffer, buf ),
VIPS_TYPE_BLOB );
}
static void
vips_foreign_save_dz_buffer_init( VipsForeignSaveDzBuffer *buffer )
{
VipsForeignSaveDz *dz = (VipsForeignSaveDz *) buffer;
/* zip default for memory output.
*/
dz->container = VIPS_FOREIGN_DZ_CONTAINER_ZIP;
}
#endif /*HAVE_GSF*/
/**
* vips_dzsave: (method)
* @in: image to save
* @name: name to save to
* @...: %NULL-terminated list of optional named arguments
*
* Optional arguments:
*
* * @basename: %gchar base part of name
* * @layout: #VipsForeignDzLayout directory layout convention
* * @suffix: %gchar suffix for tiles
* * @overlap: %gint set tile overlap
* * @tile_size: %gint set tile size
* * @background: #VipsArrayDouble background colour
* * @depth: #VipsForeignDzDepth how deep to make the pyramid
* * @centre: %gboolean centre the tiles
* * @angle: #VipsAngle rotate the image by this much
* * @container: #VipsForeignDzContainer set container type
* * @compression: %gint zip deflate compression level
* * @region_shrink: #VipsRegionShrink how to shrink each 2x2 region
* * @skip_blanks: %gint skip tiles which are nearly equal to the background
* * @no_strip: %gboolean don't strip tiles
* * @id: %gchar id for IIIF properties
*
* Save an image as a set of tiles at various resolutions. By default dzsave
* uses DeepZoom layout -- use @layout to pick other conventions.
*
* vips_dzsave() creates a directory called @name to hold the tiles. If @name
* ends `.zip`, vips_dzsave() will create a zip file called @name to hold the
* tiles. You can use @container to force zip file output.
*
* Use @basename to set the name of the directory tree we are creating. The
* default value is set from @name.
*
* You can set @suffix to something like `".jpg[Q=85]"` to control the tile
* write options.
*
* In Google layout mode, edge tiles are expanded to @tile_size by @tile_size
* pixels. Normally they are filled with white, but you can set another colour
* with @background. Images are usually placed at the top-left of the tile,
* but you can have them centred by turning on @centre.
*
* You can set the size and overlap of tiles with @tile_size and @overlap.
* They default to the correct settings for the selected @layout. The deepzoom
* defaults produce 256x256 jpeg files for centre tiles, the most efficient
* size.
*
* Use @depth to control how low the pyramid goes. This defaults to the
* correct setting for the @layout you select.
*
* You can rotate the image during write with the @angle argument. However,
* this will only work for images which support random access, like openslide,
* and not for things like JPEG. You'll need to rotate those images
* yourself with vips_rot(). Note that the `autorotate` option to the loader
* may do what you need.
*
* By default, all tiles are stripped since usually you do not want a copy of
* all metadata in every tile. Set @no_strip if you want to keep metadata.
*
* If @container is set to `zip`, you can set a compression level from -1
* (use zlib default), 0 (store, compression disabled) to 9 (max compression).
* If no value is given, the default is to store files without compression.
*
* You can use @region_shrink to control the method for shrinking each 2x2
* region. This defaults to using the average of the 4 input pixels but you can
* also use the median in cases where you want to preserve the range of values.
*
* If you set @skip_blanks to a value greater than or equal to zero, tiles
* which are all within that many pixel values to the background are skipped.
* This can save a lot of space for some image types. This option defaults to
* 5 in Google layout mode, -1 otherwise.
*
* In IIIF layout, you can set the base of the `id` property in `info.json`
* with @id. The default is `https://example.com/iiif`.
*
* Use @layout #VIPS_FOREIGN_DZ_LAYOUT_IIIF3 for IIIF v3 layout.
*
* See also: vips_tiffsave().
*
* Returns: 0 on success, -1 on error.
*/
int
vips_dzsave( VipsImage *in, const char *name, ... )
{
va_list ap;
int result;
va_start( ap, name );
result = vips_call_split( "dzsave", ap, in, name );
va_end( ap );
return( result );
}
/**
* vips_dzsave_buffer: (method)
* @in: image to save
* @buf: (array length=len) (element-type guint8): return output buffer here
* @len: (type gsize): return output length here
* @...: %NULL-terminated list of optional named arguments
*
* Optional arguments:
*
* * @basename: %gchar base part of name
* * @layout: #VipsForeignDzLayout directory layout convention
* * @suffix: %gchar suffix for tiles
* * @overlap: %gint set tile overlap
* * @tile_size: %gint set tile size
* * @background: #VipsArrayDouble background colour
* * @depth: #VipsForeignDzDepth how deep to make the pyramid
* * @centre: %gboolean centre the tiles
* * @angle: #VipsAngle rotate the image by this much
* * @container: #VipsForeignDzContainer set container type
* * @compression: %gint zip deflate compression level
* * @region_shrink: #VipsRegionShrink how to shrink each 2x2 region.
* * @skip_blanks: %gint skip tiles which are nearly equal to the background
* * @no_strip: %gboolean don't strip tiles
* * @id: %gchar id for IIIF properties
*
* As vips_dzsave(), but save to a memory buffer.
*
* Output is always in a zip container. Use @basename to set the name of the
* directory that the zip will create when unzipped.
*
* The address of the buffer is returned in @buf, the length of the buffer in
* @len. You are responsible for freeing the buffer with g_free() when you
* are done with it.
*
* See also: vips_dzsave(), vips_image_write_to_file().
*
* Returns: 0 on success, -1 on error.
*/
int
vips_dzsave_buffer( VipsImage *in, void **buf, size_t *len, ... )
{
va_list ap;
VipsArea *area;
int result;
area = NULL;
va_start( ap, len );
result = vips_call_split( "dzsave_buffer", ap, in, &area );
va_end( ap );
if( !result &&
area ) {
if( buf ) {
*buf = area->data;
area->free_fn = NULL;
}
if( len )
*len = area->length;
vips_area_unref( area );
}
return( result );
}
/**
* vips_dzsave_target: (method)
* @in: image to save
* @target: save image to this target
* @...: %NULL-terminated list of optional named arguments
*
* Optional arguments:
*
* * @basename: %gchar base part of name
* * @layout: #VipsForeignDzLayout directory layout convention
* * @suffix: %gchar suffix for tiles
* * @overlap: %gint set tile overlap
* * @tile_size: %gint set tile size
* * @background: #VipsArrayDouble background colour
* * @depth: #VipsForeignDzDepth how deep to make the pyramid
* * @centre: %gboolean centre the tiles
* * @angle: #VipsAngle rotate the image by this much
* * @container: #VipsForeignDzContainer set container type
* * @compression: %gint zip deflate compression level
* * @region_shrink: #VipsRegionShrink how to shrink each 2x2 region.
* * @skip_blanks: %gint skip tiles which are nearly equal to the background
* * @no_strip: %gboolean don't strip tiles
* * @id: %gchar id for IIIF properties
*
* As vips_dzsave(), but save to a target.
*
* See also: vips_dzsave(), vips_image_write_to_target().
*
* Returns: 0 on success, -1 on error.
*/
int
vips_dzsave_target( VipsImage *in, VipsTarget *target, ... )
{
va_list ap;
int result;
va_start( ap, target );
result = vips_call_split( "dzsave_target", ap, in, target );
va_end( ap );
return( result );
}
|