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
|
/* Panorama_Tools - Generate, Edit and Convert Panoramic Images
Copyright (C) 1998,1999 - Helmut Dersch der@fh-furtwangen.de
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this software; see the file COPYING. If not, a copy
can be downloaded from http://www.gnu.org/licenses/gpl.html, or
obtained by writing to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
/*------------------------------------------------------------*/
// Functions to read and write Photoshop, and write tiffs
// Updated by Jim Watters 2003 Nov 21
// Every layer of a multi layer PSD file should have a shape mask.
// The shape mask defines the shape of the image from the background.
// 3 problems with PTSTitcher
// - The Seam options for middle(s0 'blend')and edge(s1 'paste') are ignored by PTStitcher
// - Both the PSD options of "With_Mask" and "Without_Mask" both create mask except:
// - the "With_Mask" puts the seam in the center and uses the feathering to blend
// - the "Without_Mask" puts the seam at the edge and ignores the feathering, does no blending
// - 16bit input images are reduced to 8bit and not able to create 16bit psd files.
// For backward compatability reasons continue to create mask for "WithOut_Mask"
// When creating multi image PSD files try to create shape mask (Alpha) and clip mask properly
// Updated writePSDwithLayer and addLayer to create both proper shape mask and clip mask
// As a hack to check to see if a shape mask can be created from the alpha channel do a check for feathering in the Alpha channel
// If Alpha channel has feathering create a new shape mask
// Add two new functions hasFeather and writeTransparentAlpha
// Enabled 16bit multilayer PSD files; except they still are 8bit; They are already converted in PTStitcher
// Updated by Jim Watters 2003 Nov 24
// fixed bug with odd data size for psd files
// There is only one pad char at the end of all layer and channel data, if everything is a odd length
// Update by Jim Watters 2003 Dec 29
// Also fix ReadPSD to handle multilayers
// Update by Rik Littlefield 2004 June 29
// Fix getImageRectangle to not pagefault so badly.
// Dynamically allocate scanline buffer in writeWhiteBackground,
// to avoid buffer overflow and crash on large images.
// Fix 16bit Radial Shift and Adjust - Kevin & Jim 2004 July
// 2006 Max Lyons - Various modifications
#include <assert.h>
#include <time.h>
#include "filter.h"
#include "file.h"
#include "pttiff.h"
#include "metadata.h"
#include "sys_compat.h"
// local functions
static int writeImageDataPlanar ( Image *im, file_spec fnum );
static int readImageDataPlanar (Image *im, file_spec fnum ) ;
static int ParsePSDHeader ( char *header, Image *im, Boolean *pbBig );
static int writeChannelData ( Image *im, file_spec fnum, int channel, PTRect *r );
static int writeLayerAndMask ( Image *im, file_spec fnum, Boolean bBig );
static void getImageRectangle ( Image *im, PTRect *r );
static int fileCopy ( file_spec src, file_spec dest, size_t numBytes, unsigned char *buf);
static void orAlpha ( unsigned char* alpha, unsigned char *buf, Image *im, PTRect *r );
static void writeWhiteBackground ( uint32_t width, uint32_t height, file_spec fnum, Boolean bBig );
static int addLayer ( Image *im, file_spec src, file_spec fnum , stBuf *sB, Boolean bBig );
static int hasFeather ( Image *im );
static int writeTransparentAlpha ( Image *im, file_spec fnum, PTRect *theRect );
char *psdBlendingModesNames[] = {
"Normal",
"Color",
"Darken",
"Difference",
"Dissolve",
"Hard Light",
"Hue",
"Lighten",
"Luminosity",
"Multiply",
"Overlay",
"Sof Light",
"Saturation",
"Screen"
};
char *psdBlendingModesInternalName[] = {
"norm",
"colr",
"dark",
"diff",
"diss",
"hLit",
"hue",
"lite",
"lum",
"mul",
"over",
"sLit",
"sat",
"scrn"
};
#define PSDHLENGTH 26
size_t panoPSDResourceWrite(file_spec fnum, uint16_t resource, int32_t len, size_t dataLen, char *resourceData)
{
//struct _ColorModeDataBlock
//{
// BYTE Type[4]; /* Always "8BIM" */
// WORD ID; /* (See table below) */
// BYTE Name[]; /* Even-length Pascal-format string, 2 bytes or longer */
// LONG Size; /* Length of resource data following, in bytes */
// BYTE Data[]; /* Resource data, padded to even length */
//};
//
size_t startLocation = ftell(fnum);
panoWriteUCHAR( fnum, '8' );
panoWriteUCHAR( fnum, 'B' );
panoWriteUCHAR( fnum, 'I' );
panoWriteUCHAR( fnum, 'M' );
panoWriteSHORT( fnum, resource);
panoWriteSHORT( fnum, 0); // Null string 2 bytes (length)
panoWriteINT32( fnum, len); //size of the record (4 bytes)
if (dataLen > 0 && resourceData != NULL) {
mywrite( fnum, dataLen, resourceData );
if( (ftell(fnum) - startLocation)%2 )
{
panoWriteUCHAR( fnum, 0 );
}
}
return ftell(fnum) - startLocation;
}
size_t panoPSDPICTResourceWrite(file_spec fnum, unsigned char resource, unsigned char record, size_t len, char *recordData)
{
// See IPTC Information Intercharge Model Exchange Version 4.
size_t startLocation = ftell(fnum);
panoWriteUCHAR( fnum, 0x1c );
panoWriteUCHAR( fnum, resource );
panoWriteUCHAR( fnum, record );
panoWriteSHORT( fnum, (short)len ); //length
if (len !=0 && recordData != NULL ) {
mywrite( fnum, len, recordData);
}
return ftell(fnum) - startLocation;
}
#define IPTC_VERSION_ID 0
#define IPTC_DATE_CREATED_ID 0x37
#define IPTC_TIME_CREATED_ID 0x3C
#define IPTC_ORIGINATING_PROGRAM_ID 0x41
#define IPTC_BY_LINE_ID 0x50
#define IPTC_COPYRIGHTNOTICE_ID 0x74
#define IPTC_CAPTION_ABSTRACT_ID 0x78
#define IPTC_DESCRIPTION_WRITER_ID 0x7a
size_t panoPSDResourcesBlockWrite(Image *im, file_spec fnum)
{
size_t saveLocation = 0;
size_t saveLocationForSize=0;
// This function is a bit cryptic mainly because PSD forces the lenght to be known before a record is written.
// What I have decided is to make the writing process non-sequential.
// Write an empty length first, then "rewind" and write its real
// one. It will make things way easier and more maintainable.
// Write 4 bytes
saveLocationForSize = ftell(fnum);
panoWriteINT32( fnum, 1234 ); // Image Resources size
if (im->metadata.iccProfile.size > 0) {
// Currently we only include ICC profile if it exists
// We need to write an image Resources block
// Write Image resources header
// Write ICC Profile block
panoPSDResourceWrite(fnum, 0x040f, im->metadata.iccProfile.size,
im->metadata.iccProfile.size, im->metadata.iccProfile.data);
}
{
// we will refactor this chunck
size_t startLocationPICT = 0;
size_t saveLocationPICT = 0;
size_t descLength = 0;
char IPTCVersion[3];
// To write PICT we need to create a resource of type IPCT
// Inside it write the sequence of PICT records
// It is easy to add new records
// The computation of the length of the subrecords is automatic
// The collection of IPTC records is padded with a single character if the length is odd
// save the location so we can rewind later and write the correct value
saveLocationPICT = ftell(fnum);
// Write an empty length first, then "rewind" and write its real
// one. It will make things way easier and more maintainable.
panoPSDResourceWrite(fnum, 0x0404, 0000, 0, NULL);
startLocationPICT = ftell(fnum);
IPTCVersion[0] = 0;
IPTCVersion[1] = 2;
IPTCVersion[2] = 0;
panoPSDPICTResourceWrite(fnum, 0x02, IPTC_VERSION_ID, 2, IPTCVersion);
if(im->metadata.imageDescription)
{
// caption abstract: 0x78 "script... (2000 bytes max)
// Must not exceed 2000 char by IPTC standard.
descLength = (short)min( 2000, strlen(im->metadata.imageDescription) );
panoPSDPICTResourceWrite(fnum, 0x02, IPTC_CAPTION_ABSTRACT_ID, descLength, im->metadata.imageDescription );
}
if(im->metadata.artist)
{
// By-line: 0x50 "John Smith"
// Must not exceed 32 char by IPTC standard.
descLength = (short)min( 32, strlen(im->metadata.artist) );
panoPSDPICTResourceWrite(fnum, 0x02, IPTC_BY_LINE_ID, descLength, im->metadata.artist );
}
if(im->metadata.copyright)
{
//Copyright: 0x74 "(c) John Smith, 2011"
// Must not exceed 128 char by IPTC standard.
descLength = (short)min( 128, strlen(im->metadata.copyright) );
panoPSDPICTResourceWrite(fnum, 0x02, IPTC_COPYRIGHTNOTICE_ID, descLength, im->metadata.copyright );
}
if(TRUE)
{
char *name;
name = panoBasenameOfExecutable();
//Originating Program: 0x41 "PTtiff2PSD"
// Must not exceed 32 char by IPTC standard.
descLength = (short)min( 32, strlen(name) );
panoPSDPICTResourceWrite(fnum, 0x02, IPTC_ORIGINATING_PROGRAM_ID, descLength, name );
}
// date time will be the current date time the image was created
if(FALSE)//im->metadata.datetime)
{
char sDate[20];
char sTime[20];
time_t t;
struct tm *currentTime;
// get the local time,
t = time(NULL);
currentTime = localtime(&t);
//date: 0x37 "YYYYMMDD"
if (strftime(sDate, sizeof(sDate), "%Y%m%d", currentTime) != 0) {
// Must not exceed 8 char by IPTC standard.
assert(strlen(sDate)== 8); // Just making sure
descLength = (short)min( 8, strlen(sDate) );
panoPSDPICTResourceWrite(fnum, 0x02, IPTC_DATE_CREATED_ID, descLength, sDate );
}
else
PrintError("Error converting local time in PSD creation");
//Time: 0x3c "HHMMSSHHMM"
// First get the time
if (panoTimeToStrWithTimeZone(sTime, sizeof(sTime), currentTime)) {
assert(strlen(sTime)==11); // it should always be 11
descLength = (short)min( 11, strlen(sTime) );
panoPSDPICTResourceWrite(fnum, 0x02, IPTC_TIME_CREATED_ID, descLength, sTime );
} else
PrintError("Error converting local time in PSD creation");
}
// Check for odd size resource rec
if( (ftell(fnum) - saveLocationPICT) %2 )
{
// The section is of an odd size pad with a single character
panoWriteUCHAR( fnum, 0 );
}
// Write length
saveLocation = ftell(fnum);
fseek(fnum, saveLocationPICT, SEEK_SET);
assert(saveLocation > saveLocationForSize);
panoPSDResourceWrite(fnum, 0x0404, saveLocation-startLocationPICT, 0, NULL);
fseek(fnum, saveLocation, SEEK_SET);
}
// Write length
saveLocation = ftell(fnum);
fseek(fnum, saveLocationForSize, SEEK_SET);
assert(saveLocation > saveLocationForSize);
panoWriteINT32( fnum, saveLocation - saveLocationForSize-4 ); // Image Resources size
fseek(fnum, saveLocation, SEEK_SET);
return ftell(fnum) - saveLocationForSize;
}
int writePSD ( Image *im, fullPath* fname )
{
return writePS(im, fname, FALSE);
}
// Save image as the background, as a PSD or PSB file
// Image is background, there are no layers
int writePS(Image *im, fullPath *sfile, Boolean bBig )
{
file_spec fnum;
int channels;
int BitsPerChannel;
// Check to see if we need to create PSB instead of PSD file
if(panoImageFullHeight(im) > 30000 || panoImageFullWidth(im) > 30000)
bBig = TRUE;
GetChannels( im, channels );
GetBitsPerChannel( im, BitsPerChannel );
if( myopen( sfile, write_bin, fnum ) )
{
PrintError("Error Writing Image File");
return -1;
}
// Write PSD and PSB Header
// WRITEINT32( '8BPS' );
panoWriteUCHAR( fnum, '8' );
panoWriteUCHAR( fnum, 'B' );
panoWriteUCHAR( fnum, 'P' );
panoWriteUCHAR( fnum, 'S' );
panoWriteSHORT( fnum, bBig? 2 : 1 ); // PSD file must be 1, PSB files must be 2
panoWriteINT32( fnum, 0 ); panoWriteSHORT( fnum, 0 ); // 6 bytes zeroed
panoWriteSHORT( fnum, channels ); // Num of channels
// The Photoshop file has the dimensions of the full size image, regardless
// of whether the TIFF file is "cropped" or not.
// Layers of a PSD file can have the data constrained to only the cropped area, but that is not what we are doing here.
// PSD file is limited to 30000 in width and height, PSB are limited to 300000 in width and height
panoWriteINT32( fnum, panoImageHeight(im) ); // Rows
panoWriteINT32( fnum, panoImageWidth(im) ); // Columns
panoWriteSHORT( fnum, BitsPerChannel ); // BitsPerChannel
switch( im->dataformat ) // Color mode
{
case _Lab: panoWriteSHORT( fnum, 9 );
break;
case _RGB: panoWriteSHORT( fnum, 3 );
break;
default: panoWriteSHORT( fnum, 3 );
}
panoWriteINT32( fnum, 0 ); // Color Mode block
panoPSDResourcesBlockWrite( im, fnum ); // PSD Resource
panoWriteINT32or64( fnum, 0, bBig ); // Layer & Mask
writeImageDataPlanar( im, fnum ); // Image data
myclose (fnum );
return 0;
}
int writePSDwithLayer ( Image *im, fullPath *fname)
{
return writePSwithLayer( im, fname, FALSE);
}
// Save image as single layer PSD or PSB file
// Image is layer in front of white background
int writePSwithLayer(Image *im, fullPath *sfile, Boolean bBig )
{
file_spec fnum;
int BitsPerChannel;
// Check to see if we need to create PSB instead of PSD file
if(panoImageFullHeight(im) > 30000 || panoImageFullWidth(im) > 30000)
bBig = TRUE;
// Jim Watters 2003/11/18: Photoshop CS does 16bit channels if 16 bit in, allow 16 bit out
// TwoToOneByte( im ); // Multilayer image format doesn't support 16 bit channels
GetBitsPerChannel( im, BitsPerChannel );
if( myopen( sfile, write_bin, fnum ) )
{
PrintError("Error Writing Image File");
return -1;
}
// Write PSD and PSB Header
// panoWriteINT32( fnum, '8BPS' );
panoWriteUCHAR( fnum, '8' );
panoWriteUCHAR( fnum, 'B' );
panoWriteUCHAR( fnum, 'P' );
panoWriteUCHAR( fnum, 'S' );
panoWriteSHORT( fnum, bBig? 2 : 1 ); // PSD file must be 1, PSB files must be 2
panoWriteINT32( fnum, 0 ); panoWriteSHORT( fnum, 0 ); // 6 bytes zeroed
panoWriteSHORT( fnum, 3 ); // No of channels; Background always white, 3 channels
//The Photoshop file has the dimensions of the full size image, regardless
//of whether the TIFF file is "cropped" or not.
// PSD file is limited to 30000 in width and height, PSB are limited to 300000 in width and height
panoWriteINT32( fnum, panoImageFullHeight(im) ); // Rows
panoWriteINT32( fnum, panoImageFullWidth(im) ); // Columns
panoWriteSHORT( fnum, BitsPerChannel ); // BitsPerChannel
switch( im->dataformat )
{
case _Lab: panoWriteSHORT( fnum, 9 );
break;
case _RGB: panoWriteSHORT( fnum, 3 );
break;
default: panoWriteSHORT( fnum, 3 );
}
panoWriteINT32( fnum, 0 ); // Color Mode
panoPSDResourcesBlockWrite(im, fnum);
writeLayerAndMask( im, fnum, bBig );
writeWhiteBackground( panoImageFullWidth(im) * (BitsPerChannel/8), panoImageFullHeight(im), fnum, bBig );
myclose (fnum );
return 0;
}
// Add image as additional layer into PSD-file (exported function)
int addLayerToFile( Image *im, fullPath* sfile, fullPath* dfile, stBuf *sB)
{
file_spec src;
file_spec fnum;
Image sim; // background image
char header[128], *h;
size_t count, i, srcCount = 0;
uint32_t len;
unsigned char **buf;
int BitsPerChannel, result = 0;
Boolean bBig = FALSE;
// Jim Watters 2003/11/18: Photoshop CS does 16bit channels if 16 bit in, allow 16 bit out
// TwoToOneByte( im ); // Multilayer image format doesn't support 16 bit channels
GetBitsPerChannel( im, BitsPerChannel );
if( myopen( sfile,read_bin, src ) )
{
PrintError("Error Opening Image File");
return -1;
}
// Read psd header
h = header;
count = PSDHLENGTH;
myread( src,count,h); srcCount += count;
if( count != PSDHLENGTH )
{
PrintError("Error Reading Image File");
myclose( src );
return -1;
}
if( ParsePSDHeader( header, &sim, &bBig ) != 0 )
{
PrintError("addLayerToFile: Wrong File Format");
myclose( src );
return -1;
}
// Check if image can be inserted
// printf("Image size %d %d im %d %d\n", sim.width, sim.height, panoImageWidth(im), panoImageHeight(im));
if( sim.width != panoImageFullWidth(im) || sim.height != panoImageFullHeight(im) )
{
PrintError("Can't add layer: Images have different size");
return -1;
}
// Read (and ignore) Color mode data
panoReadINT32( src, &len );
srcCount += (4 + len);
count = 1;
for( i=0; i<len; i++ )
{
myread(src,count,h);
}
// Read (and ingnore) Image resources
panoReadINT32( src, &len );
srcCount += (4 + len);
count = 1;
for( i=0; i<len; i++ )
{
myread(src,count,h);
}
myclose( src );
if( myopen( sfile, read_bin, src ) )
{
PrintError("Error Opening Image File");
return -1;
}
if( myopen( dfile, write_bin, fnum ) )
{
PrintError("Error Opening Image File");
return -1;
}
// Read and write Fileheader
buf = (unsigned char**)mymalloc( srcCount );
if( buf == NULL )
{
PrintError("Not enough memory");
result = -1;
goto _addLayerToFile_exit;
}
fileCopy( src, fnum, srcCount, *buf );
myfree( (void**)buf );
// Add one layer
if( addLayer( im, src, fnum, sB, bBig ) != 0 )
{
result = -1;
goto _addLayerToFile_exit;
}
writeWhiteBackground( sim.width * (BitsPerChannel/8), sim.height, fnum, bBig );
_addLayerToFile_exit:
myclose( src ); myclose( fnum );
return result;
}
static int writeImageDataPlanar( Image *im, file_spec fnum )
{
register unsigned int x,y,idy, bpp;
unsigned char **channel;
register unsigned char *ch, *idata;
int color;
size_t count;
int BitsPerChannel, channels;
GetBitsPerChannel( im, BitsPerChannel );
GetChannels( im,channels);
printf("Bitx per channel %d channels %d\n", BitsPerChannel, channels);
bpp = im->bitsPerPixel / 8;
// Write Compression info
panoWriteSHORT( fnum, 0 ); // Raw data
// Buffer to hold data in one channel
count = (size_t)im->width * im->height * (BitsPerChannel / 8);
channel = (unsigned char**)mymalloc( count );
if( channel == NULL )
{
PrintError("Not Enough Memory");
return -1;
}
if( BitsPerChannel == 8 )
{
for( color = 0; color<3; color++)
{
ch = *channel; idata = &(*im->data)[color + channels - 3];
for(y=0; y<im->height;y++)
{
idy = y * im->bytesPerLine;
for(x=0; x<im->width;x++)
{
*ch++ = idata [ idy + x * bpp ];
}
}
mywrite( fnum, count, *channel );
}
}
else // 16
{
unsigned short storage;//Kekus 2003 16 bit support
for( color = 0; color<3; color++)
{
ch = *channel; idata = &(*im->data)[2*(color + channels - 3)];
for(y=0; y<im->height;y++)
{
idy = y * im->bytesPerLine;
for(x=0; x<im->width;x++)
{
//Kekus:2003/Nov/18 // JMW 2004 July
storage = *(unsigned short*)&idata [ idy + x * bpp ];
SHORTNUMBER( storage, ch );
//Kekus.
}
}
mywrite( fnum, count, *channel );
}
}
if( im->bitsPerPixel == 32 )
{
// Write 1byte alpha channel
ch = *channel; idata = &(*im->data)[0];
for(y=0; y<im->height;y++)
{
idy = y * im->bytesPerLine;
for(x=0; x<im->width;x++)
{
*ch++ = idata [ idy + x * bpp ];
}
}
mywrite( fnum, count, *channel );
}
else if( im->bitsPerPixel == 64 )
{
// Write 2byte alpha channel
unsigned short storage;
ch = *channel; idata = &(*im->data)[0];
for(y=0; y<im->height;y++)
{
idy = y * im->bytesPerLine;
for(x=0; x<im->width;x++)
{
storage = *(unsigned short*)&idata [ idy + x * bpp ];
SHORTNUMBER( storage, ch );
}
}
mywrite( fnum, count, *channel );
}
myfree( (void**)channel );
return 0;
}
// Write white background, RLE-compressed
static void writeWhiteBackground(uint32_t width, uint32_t height, file_spec fnum, Boolean bBig )
{
uint32_t w8, w;
size_t count;
char *d;
char **scanline;
int numChannels = 3;
int i;
int bytecount;
int dim = height*numChannels;
size_t maxscanline = (width/128)*2 + 2;
scanline = (char**)mymalloc( maxscanline );
if( scanline == NULL )
{
PrintError("Not enough memory");
return;
}
panoWriteSHORT( fnum, 1 ); // RLE compressed
w8 = width;
d = *scanline;
// Set up scanline
for(w=w8; w>128; w-=128)
{
*d++ = -127;
*d++ = 255U;
}
switch(w)
{
case 0: break;
case 1: *d++ = 0;
*d++ = 255U;
break;
default: *d++ = 1-(char)w;
*d++ = 255U;
break;
}
bytecount = (int)(d - *scanline);
// Scanline counts (rows*channels)
for(i=0; i < dim; i++)
{
if(bBig)
{
panoWriteINT32( fnum, bytecount );
}
else
{
panoWriteSHORT( fnum, bytecount );
}
}
// RLE compressed data
count = bytecount;
for(i=0; i < dim; i++)
{
mywrite( fnum, count, *scanline );
}
myfree((void**)scanline);
}
// image is allocated, but not image data
// mode = 0: only load image struct
// mode = 1: also allocate and load data
int readPSD(Image *im, fullPath *sfile, int mode)
{
file_spec src;
char header[128];
char *h;
uint32 len;
uint32 i;
size_t count;
Boolean bBig = FALSE;
if( myopen( sfile, read_bin, src ) )
{
PrintError("Error Opening Image File");
return -1;
}
// Read psd header
h = header;
count = PSDHLENGTH;
myread( src,count,h );
if( count != PSDHLENGTH )
{
PrintError("Error Reading Image File");
myclose( src );
return -1;
}
if( ParsePSDHeader( header, im, &bBig ) != 0 )
{
PrintError("readPSD: Wrong File Format");
myclose( src );
return -1;
}
if( mode == 0 )
{
myclose( src );
return 0;
}
im->data = (unsigned char**) mymalloc( im->dataSize );
if( im->data == NULL )
{
PrintError("Not enough memory to read image");
myclose( src );
return -1;
}
// Read (and ingnore) Color mode data
panoReadINT32( src, &len );
count = 1;
for( i=0; i<len; i++ )
myread(src,count,h);
// Read (and ingnore) Image resources
panoReadINT32( src, &len );
count = 1;
for( i=0; i<len; i++ )
myread(src,count,h);
// Read (and ingnore) Layer mask info
panoReadINT32( src, &len );
count = 1;
for( i=0; i<len; i++ )
myread(src,count,h);
if( readImageDataPlanar( im, src ) != 0 )
{
PrintError("Error reading image");
myclose( src );
return -1;
}
myclose (src );
return 0;
}
static int ParsePSDHeader( char *header, Image *im, Boolean *pbBig )
{
register char *h = header;
short s;
int channels;
if(pbBig == NULL)
{
PrintError( "ParsePSDHeader: Error pbBig is NULL");
return -1;
}
*pbBig = FALSE;
if( *h++ != '8' || *h++ != 'B' || *h++ != 'P' || *h++ != 'S' ||
*h++ != 0 || (*h++ != 1 && *(h-1) != 2 ) ||
*h++ != 0 || *h++ != 0 || *h++ != 0 || *h++ != 0 || *h++ != 0 || *h++ != 0 )
{
PrintError( "ParsePSDHeader: Error reading PSD Header: %c%c%c%c", header[0], header[1], header[2], header[3] );
return -1;
}
if(header[5] == 2)
*pbBig = TRUE;
NUMBERSHORT( s, h );
channels = s;
if( channels < 3 ) //!= 3 && channels != 4 )
{
PrintError( "Number of channels must be 3 or larger" );
return -1;
}
if( channels > 4 ) channels = 4;
NUMBERLONG( im->height, h );
NUMBERLONG( im->width, h );
NUMBERSHORT( s, h );
if( s!= 8 && s!= 16)
{
PrintError( "Depth must be 8 or 16 Bits per Channel" );
return -1;
}
im->bitsPerPixel = s * channels;
NUMBERSHORT( s, h );
switch( s )
{
case 3: im->dataformat = _RGB; break;
case 9: im->dataformat = _Lab; break;
default: PrintError( "Color mode must be RGB or Lab" );return -1;
}
im->bytesPerLine = im->width * (im->bitsPerPixel/8);
im->dataSize = (size_t)im->height * im->bytesPerLine;
return 0;
}
static int readImageDataPlanar(Image *im, file_spec src )
{
register unsigned int x,y,idy, bpp;
unsigned char **channel = NULL;
register unsigned char *h, *idata;
int result = 0, i, chnum,BitsPerChannel, channels;
size_t count;
unsigned short usvar;
GetBitsPerChannel( im, BitsPerChannel );
GetChannels( im, channels );
bpp = im->bitsPerPixel / 8;
// Read Compression info
panoReadSHORT( src, &usvar );
if( usvar!= 0 )
{
PrintError("Image data must not be compressed");
return -1;
}
// Allocate memory for one channel
count = (size_t)im->width * im->height * (BitsPerChannel/8);
channel = (unsigned char**)mymalloc( count );
if( channel == NULL )
{
PrintError("Not Enough Memory");
return -1;
}
for(i = 0; i < channels; i++) // Read each channel
{
chnum = i + channels - 3;
if(chnum == 4) chnum = 0; // Order: r g b (alpha)
myread(src,count,*channel);
if( count != (size_t)im->width * im->height * (BitsPerChannel/8))
{
PrintError("Error Reading Image Data");
result = -1;
goto readImageDataPlanar_exit;
}
h = *channel;
if( BitsPerChannel == 8 )
{
idata = &(*im->data)[chnum];
for(y=0; y<im->height;y++)
{
idy = y * im->bytesPerLine;
for(x=0; x<im->width;x++)
{
idata [ idy + x * bpp ] = *h++;
}
}
}
else // 16
{
idata = &(*im->data)[chnum*2];
for(y=0; y<im->height;y++)
{
idy = y * im->bytesPerLine;
for(x=0; x<im->width;x++)
{
NUMBERSHORT( usvar, h );
*((unsigned short*)&idata [ idy + x * bpp ]) = usvar;
}
}
}
}
readImageDataPlanar_exit:
if( channel != NULL )
myfree( (void**)channel );
return result;
}
// Write image as separate first layer
static int writeLayerAndMask( Image *im, file_spec fnum, Boolean bBig )
{
PTRect theRect;
int64_t lenLayerInfo;
int64_t channelLength;
int i, BitsPerChannel, channels, psdchannels;
int oddSized = 0;
int hasClipMask = 0; // Create a mask
int hasShapeMask = 0; // Create alpha channel
GetBitsPerChannel( im, BitsPerChannel );
GetChannels( im, channels );
psdchannels = channels;
if( channels > 3 ) // Alpha channel present
{
hasClipMask = 1;
psdchannels++;
if ( !hasFeather( im ) )// If there is feathering do Not use Alpha channel for Shape Mask
{ // Only use alpha if there is no feathering
hasShapeMask = 1; // The Alpha channel can be used as a Shape Mask
}
}
//we only write to the PSD file the smallest region of the input file that
//contains "non-empty" image data. The bounds of this region are recorded in
//in "theRect". If this input image is a "cropped" image, then theRect will
//just use the data region specified "crop_info".
getImageRectangle( im, &theRect );
channelLength = ((int64_t)theRect.right-theRect.left) * (theRect.bottom-theRect.top) * (BitsPerChannel/8) + 2;
lenLayerInfo = 2 + 4*4 + 2 + psdchannels * 6 + 4 + 4 + 4 * 1 + 4 + 12 + psdchannels * channelLength;
if(bBig)
{
lenLayerInfo += psdchannels * 4;
}
if(hasClipMask)
lenLayerInfo += 20;
// Length of the layers info section, rounded up to a multiple of 2.
if( lenLayerInfo%2 ) // odd
{
lenLayerInfo += 1; oddSized = 1;
}
panoWriteINT32or64( fnum, lenLayerInfo + 4 + (bBig?8:4), bBig );
// Layer info. See table 213.
panoWriteINT32or64( fnum, lenLayerInfo, bBig );
// 2 bytes Count Number of layers. If <0, then number of layers is absolute value,
// and the first alpha channel contains the transparency data for
// the merged result.
panoWriteSHORT( fnum, 1 );
// ********** Layer Structure ********************************* //
panoWriteINT32( fnum, theRect.top ); // Layer top
panoWriteINT32( fnum, theRect.left ); // Layer left
panoWriteINT32( fnum, theRect.bottom ); // Layer bottom
panoWriteINT32( fnum, theRect.right ); // Layer right
panoWriteSHORT( fnum, psdchannels ); // The number of channels in the layer.
// ********** Channel information ***************************** //
panoWriteSHORT( fnum, 0 ); // red
panoWriteINT32or64( fnum, channelLength, bBig ); // Length of following channel data.
panoWriteSHORT( fnum, 1 ); // green
panoWriteINT32or64( fnum, channelLength, bBig ); // Length of following channel data.
panoWriteSHORT( fnum, 2 ); // blue
panoWriteINT32or64( fnum, channelLength, bBig ); // Length of following channel data.
if( hasClipMask ) // Mask channel
{
panoWriteSHORT( fnum, -1 ); // Shape Mask
panoWriteINT32or64( fnum, channelLength, bBig ); // Length of following channel data.
panoWriteSHORT( fnum, -2 ); // Clip Mask
panoWriteINT32or64( fnum, channelLength, bBig ); // Length of following channel data.
}
// ********** End Channel information ***************************** //
// panoWriteINT32( fnum, '8BIM'); // Blend mode signature Always 8BIM.
panoWriteUCHAR( fnum, '8' );
panoWriteUCHAR( fnum, 'B' );
panoWriteUCHAR( fnum, 'I' );
panoWriteUCHAR( fnum, 'M' );
// panoWriteINT32( fnum, 'norm'); // Blend mode key
panoWriteUCHAR( fnum, 'n' );
panoWriteUCHAR( fnum, 'o' );
panoWriteUCHAR( fnum, 'r' );
panoWriteUCHAR( fnum, 'm' );
panoWriteUCHAR( fnum, 255 ); // 1 byte Opacity 0 = transparent ... 255 = opaque
panoWriteUCHAR( fnum, 0 ); // 1 byte Clipping 0 = base, 1 = nonbase
panoWriteUCHAR( fnum, hasShapeMask ); // 1 byte Flags bit 0 = transparency protected bit 1 = visible
panoWriteUCHAR( fnum, 0 ); // 1 byte (filler) (zero)
if(hasClipMask)
{
panoWriteINT32( fnum, 32 ); // Extra data size Length of the extra data field. This is the total length of the next five fields.
panoWriteINT32( fnum, 20 ); // Yes Layer Mask data
panoWriteINT32( fnum, theRect.top ); // Layer top
panoWriteINT32( fnum, theRect.left ); // Layer left
panoWriteINT32( fnum, theRect.bottom ); // Layer bottom
panoWriteINT32( fnum, theRect.right ); // Layer right
panoWriteUCHAR( fnum, 0 ); // Default color 0 or 255
panoWriteUCHAR( fnum, 0 ); // Flag: bit 0 = position relative to layer bit 1 = layer mask disabled bit 2 = invert layer mask when blending
panoWriteUCHAR( fnum, 0 ); // padding
panoWriteUCHAR( fnum, 0 ); // padding
}
else
{
panoWriteINT32( fnum, 12 ); // Extra data size Length of the extra data field. This is the total length of the next five fields.
panoWriteINT32( fnum, 0 ); // No Layer Mask data
}
panoWriteINT32( fnum, 0 ); // Layer blending ranges
// Layer name 001
panoWriteUCHAR( fnum, 3 ); // length of name
panoWriteUCHAR( fnum, '0' );
panoWriteUCHAR( fnum, '0' );
panoWriteUCHAR( fnum, '1' );
// ************* End Layer Structure ******************************* //
// ************* Write Channel Image data ************************** //
// Write color channels
for( i=0; i<3; i++)
{
if( writeChannelData( im, fnum, i + channels - 3, &theRect ) )
return -1;
}
if( hasShapeMask )
{
if( writeChannelData( im, fnum, 0, &theRect ) )
return -1;
}
else
{
if( writeTransparentAlpha(im, fnum, &theRect ) )
return -1;
}
if( hasClipMask )
{
if( writeChannelData( im, fnum, 0, &theRect ) )
return -1;
}
if( oddSized ) // pad byte
{
panoWriteUCHAR( fnum, 0 );
}
// ************* End Write Channel Image data ************************** //
// ************* Global layer mask info ******************************** //
panoWriteINT32( fnum, 0 ); // Length of global layer mask info section.
// panoWriteSHORT( fnum, 0 ); // 2 bytes Overlay color space
// panoWriteSHORT( fnum, 0 ); // 4 * 2 byte color components
// panoWriteSHORT( fnum, 0 );
// panoWriteSHORT( fnum, 0 );
// panoWriteSHORT( fnum, 0 );
// panoWriteSHORT( fnum, 0 ); // 2 bytes Opacity 0 = transparent, 100 = opaque.
// panoWriteUCHAR( fnum, 128 ); // 1 byte Kind 0=Color selectedi.e. inverted;
// 1=Color protected;128=use value stored per layer.
// This value is preferred. The others are for back-ward
// compatibility with beta versions.
// panoWriteUCHAR( fnum, 0 );
return 0;
}
static int writeChannelData( Image *im, file_spec fnum, int channel, PTRect *theRect )
{
register unsigned int x, y, idx, idy, bpp, BitsPerChannel;
unsigned char **ch;
register unsigned char *c, *idata;
size_t count;
uint32_t outputRegionWidth, outputRegionHeight;
GetBitsPerChannel( im, BitsPerChannel );
// Write Compression info
panoWriteSHORT( fnum, 0 ); // Raw data
bpp = im->bitsPerPixel/8;
count = ((size_t)theRect->right - theRect->left) * (theRect->bottom - theRect->top) * (BitsPerChannel/8);
ch = (unsigned char**)mymalloc( count );
if( ch == NULL )
{
PrintError("Not Enough Memory");
return -1;
}
//note: theRect specifies the position in the output layer where the data should
//be placed. crop_info contains information about where the source data
//is positioned relative to the full output size
outputRegionWidth = (theRect->right - theRect->left);
outputRegionHeight = (theRect->bottom - theRect->top);
if (outputRegionWidth > panoImageWidth(im) || outputRegionHeight > panoImageHeight(im))
{
printf("output region (%d x %d) is larger than input image data region (%d x %d)\n",
(int)outputRegionWidth, (int)outputRegionHeight, (int)panoImageWidth(im), (int)panoImageHeight(im));
return 1;
}
c = *ch; idata = &((*(im->data))[channel*(BitsPerChannel/8)]);
if(BitsPerChannel == 8)
{
for(y=theRect->top; y<theRect->bottom;y++)
{
idy = (y - panoImageOffsetY(im)) * im->bytesPerLine;
if (idy < 0) { //should never happen
PrintError("writeChannelData: index error");
return 1;
}
for(x=theRect->left; x<theRect->right;x++)
{
idx = ((x - panoImageOffsetX(im)) * bpp);
*c++ = idata [ idy + idx ];
}
}
}
else // 16
{
unsigned short storage;
for(y=theRect->top; y<theRect->bottom;y++)
{
idy = (y - panoImageOffsetY(im)) * im->bytesPerLine;
if (idy < 0) { //should never happen
PrintError("writeChannelData: index error");
return 1;
}
for(x=theRect->left; x<theRect->right;x++)
{
idx = ((x - panoImageOffsetX(im)) * bpp);
storage = *(unsigned short*)&idata [ idy + idx ];
SHORTNUMBER( storage, c );
}
}
}
mywrite( fnum, count, *ch );
myfree( (void**)ch );
return 0;
}
static int writeTransparentAlpha( Image *im, file_spec fnum, PTRect *theRect )
{
register unsigned int y, BitsPerChannel;
size_t line, count;
unsigned char **ch;
register unsigned char *c;
GetBitsPerChannel( im, BitsPerChannel );
line = ((size_t)theRect->right - theRect->left) * (BitsPerChannel/8);
ch = (unsigned char**)mymalloc( line );
if( ch == NULL )
{
PrintError("Not Enough Memory");
return -1;
}
c = *ch;
// Write Compression info
panoWriteSHORT( fnum, 0 ); // Raw data
// fill in line
memset( c, 255, line );
// write out the results
for(y=theRect->top; y<theRect->bottom;y++)
{
count = line;
mywrite( fnum, count, *ch );
}
myfree( (void**)ch );
return 0;
}
// Return the smallest rectangle enclosing the image;
// Use alpha channel and rgb data
static void getImageRectangle( Image *im, PTRect *theRect )
{
register unsigned char *alpha, *data;
register unsigned int x,y,cy,bpp,channels;
int BitsPerChannel;
//If this is a cropped TIFF then we can get the image rectangle ROI from
//the metadata in crop_info, rather than having to parse the entire
//file to look for ROI
if ( panoImageIsCropped(im)) {
theRect->left = panoImageOffsetX(im);
theRect->top = panoImageOffsetY(im);
//Slightly counter-intuitive...right and bottom are one pixel larger than expected
//so that the width and height can be calculated by subtracting
//left from right or top from bottom
theRect->right = theRect->left + panoImageWidth(im);
theRect->bottom = theRect->top + panoImageHeight(im);
return;
}
//the crop_info seems indicates that this isn't a cropped TIFF...we have
//to parse the entire file to determine the ROI that contains non-empty
//image data
GetChannels( im, channels );
GetBitsPerChannel( im, BitsPerChannel );
bpp = im->bitsPerPixel/8;
theRect->top = im->height;
theRect->left = im->width;
theRect->bottom = 0;
theRect->right = 0;
if( channels == 4 ){ // alpha channel present: use image and alpha
if( BitsPerChannel == 8 ){
for(y=0; y<im->height; y++){
for(x=0, alpha = *(im->data) + y * im->bytesPerLine;
x<im->width;
x++, alpha += 4){
data = alpha + 1;
if( *alpha ||
*(data) || *(data+1) || *(data+2) ){
if (y < theRect->top) theRect->top = y;
if (y+1 > theRect->bottom) theRect->bottom = y+1;
if (x < theRect->left) theRect->left = x;
if (x+1 > theRect->right) theRect->right = x+1;
}
}
}
}else{ // 16
for(y=0; y<im->height; y++){
for(x=0, alpha = *(im->data) + y * im->bytesPerLine;
x<im->width;
x++, alpha += 8){
data = alpha + 2;
if( *((uint16_t*)alpha) ||
*((uint16_t*)data) || *(((uint16_t*)data)+1) || *(((uint16_t*)data)+2) ){
if (y < theRect->top) theRect->top = y;
if (y+1 > theRect->bottom) theRect->bottom = y+1;
if (x < theRect->left) theRect->left = x;
if (x+1 > theRect->right) theRect->right = x+1;
}
}
}
}
}
else // no alpha channel: Use non black rectangle only
{
alpha = *(im->data);
if( BitsPerChannel == 8 )
{
for(y=0; y<im->height; y++)
{
cy = y * im->bytesPerLine;
for(x=0; x<im->width; x++)
{
data = alpha + cy + bpp*x;
if( *data || *(data+1) || *(data+2) )
{
if (y < theRect->top) theRect->top = y;
if (y+1 > theRect->bottom) theRect->bottom = y+1;
if (x < theRect->left) theRect->left = x;
if (x+1 > theRect->right) theRect->right = x+1;
}
}
}
}
else // 16
{
for(y=0; y<im->height; y++)
{
cy = y * im->bytesPerLine;
for(x=0; x<im->width; x++)
{
data = alpha + cy + bpp*x;
if( *((uint16_t*)data) || *(((uint16_t*)data)+1) || *(((uint16_t*)data)+2) )
{
if (y < theRect->top) theRect->top = y;
if (y+1 > theRect->bottom) theRect->bottom = y+1;
if (x < theRect->left) theRect->left = x;
if (x+1 > theRect->right) theRect->right = x+1;
}
}
}
}
}
if (theRect->bottom <= theRect->top) { // no valid pixels found
theRect->top = 0;
theRect->left = 0;
theRect->bottom = im->height;
theRect->right = im->width;
}
//printf("Image Rectangle = %d,%d - %d,%d\n", theRect->left, theRect->top, theRect->right, theRect->bottom);
#if 0
{ char msg[1000];
sprintf(msg,"width=%d, height=%d, top = %d, bottom = %d, left = %d, right = %d\n",
im->width, im->height, theRect->top, theRect->bottom, theRect->left, theRect->right);
PrintError(msg);
}
#endif
}
// Image is added to layer structure of file.
// There must be one valid layer structure, and the
// filepointer is at the beginning of it in both src and dest
static int addLayer( Image *im, file_spec src, file_spec fnum, stBuf *sB, Boolean bBig )
{
uint32_t var;
int64_t var64;
PTRect theRect, *nRect = NULL;
int oddSized = 0, oddSizedOld = 0;
int64_t channelLength, lenLayerInfo;
int result = 0;
size_t count;
char sLayerName[5];
unsigned char ch;
int i, channels, psdchannels;
uint16_t numLayers;
uint32_t k;
unsigned int BitsPerChannel;
uint16_t *uChannel = NULL;
uint16_t *chid = NULL;
uint32_t *uFlag = NULL;
uint32_t *uExtra = NULL;
uint32_t *uMask = NULL;
uint32_t *uMaskData = NULL;
uint32_t *cNames = NULL; //used to store a 4 character name
int64_t *chlength = NULL;
unsigned char **alpha = NULL, **buf = NULL;
int hasClipMask = 0; // Create a mask
int hasShapeMask = 0; // Create alpha channel
char * blendingModeKey;
int j;
GetChannels( im, channels );
psdchannels = channels;
GetBitsPerChannel( im, BitsPerChannel );
if( channels > 3 ) // Alpha channel present
{
hasClipMask = 1;
psdchannels++;
if ( !hasFeather( im ) )// If there is feathering do Not use Alpha channel for Shape Mask
{ // Only use alpha if there is no feathering
hasShapeMask = 1; // The Alpha channel can be used as a Shape Mask
}
if( sB->seam == _middle ) // we have to stitch
{
size_t AlaphaSize = (size_t)panoImageFullWidth(im) * panoImageFullHeight(im) * (BitsPerChannel/8);
alpha = (unsigned char**) mymalloc( AlaphaSize);
if( alpha != NULL )
{
memset( *alpha, 0 , AlaphaSize);
}
}
}
getImageRectangle( im, &theRect );
channelLength = ((size_t)theRect.right-theRect.left) * (theRect.bottom-theRect.top) * (BitsPerChannel/8) + 2;
// Read layerinfo up to channeldata
panoReadINT32or64( src, &var64, bBig ); // Length of the miscellaneous information section (ignored)
panoReadINT32or64( src, &var64, bBig ); // Length of the layers info section, rounded up to a multiple of 2(ignored)
panoReadSHORT( src, (uint16_t *)&numLayers ); // Number of layers If it is a negative number, its absolute value is the number of layers and the first alpha channel contains the transparency data for the merged result.
lenLayerInfo = 2;
chlength = (int64_t*) malloc( numLayers * 8 );
uChannel = (uint16_t*) malloc( numLayers * sizeof( uint16_t ) );
cNames = (uint32_t*) malloc( numLayers * sizeof( uint32_t ) );
nRect = (PTRect*) malloc( numLayers * sizeof( PTRect ) );
uExtra = (uint32_t*) malloc( numLayers * sizeof( uint32_t ) );
uMask = (uint32_t*) malloc( numLayers * sizeof( uint32_t ) );
uMaskData= (uint32_t*) malloc( numLayers * 5 * sizeof( uint32_t ) );
chid = (uint16_t*) malloc( numLayers * 5 * sizeof( uint16_t ) );// five posible channels
uFlag = (uint32_t*) malloc( numLayers * 5 * sizeof( uint32_t ) );
if( chlength == NULL || uChannel== NULL || nRect == NULL ||
cNames == NULL || uExtra == NULL || uMask == NULL ||
uMaskData == NULL || chid == NULL || uFlag == NULL )
{
PrintError("Not enough memory 1");
result = -1;
goto _addLayer_exit;
}
for(i=0; i<numLayers; i++)
{
uint32_t number;
panoReadINT32( src, &number ); nRect[i].top = number; // Layer top
panoReadINT32( src, &number ); nRect[i].left = number; // Layer left
panoReadINT32( src, &number ); nRect[i].bottom = number; // Layer bottom
panoReadINT32( src, &number ); nRect[i].right = number; // Layer right
panoReadSHORT( src, &uChannel[i] ); // The number of channels in the layer.
// ********** Channel information ***************************** //
panoReadSHORT( src, &chid[i*5 +0] ); // red
panoReadINT32or64( src, &chlength[i], bBig ); // Length of following channel data.
panoReadSHORT( src, &chid[i*5 +1] ); // green
panoReadINT32or64( src, &chlength[i], bBig ); // Length of following channel data.
panoReadSHORT( src, &chid[i*5 +2] ); // blue
panoReadINT32or64( src, &chlength[i], bBig ); // Length of following channel data.
if( uChannel[i] > 3 ) // 1st alpha channel
{
panoReadSHORT( src, &chid[i*5 +3] ); // transparency mask
panoReadINT32or64( src, &chlength[i], bBig ); // Length of following channel data.
}
if( uChannel[i] > 4 ) // 2nd alpha channel
{
panoReadSHORT( src, &chid[i*5 +4] ); // transparency mask
panoReadINT32or64( src, &chlength[i], bBig ); // Length of following channel data.
}
// ********** End Channel information ***************************** //
panoReadINT32( src, &var ); // Blend mode signature Always 8BIM.
panoReadINT32( src, &var ); // Blend mode key
panoReadINT32( src, &uFlag[i] ); // Four flag bytes: Opacity, clipping, flag, filler
panoReadINT32( src, &uExtra[i] ); // Extra data size Length of the extra data field. This is the total length of the next five fields.
panoReadINT32( src, &uMask[i] ); // Layer Mask data length
if(uMask[i])
for(k=0; k<uMask[i]/4; k++) // either 0 or 20
{
panoReadINT32( src, &uMaskData[i*5 +k] ); // Layer Mask Data
}
panoReadINT32( src, &var ); // Layer blending ranges
panoReadINT32( src, &cNames[i] ); // Layer name. Panotools only uses 4 bytes to make 3 char name.
var64 = 4*4 + 2 + uChannel[i] * 6 + 4 + 4 + 4 * 1 + 4 + 4 + uMask[i] + 8 + uChannel[i] * chlength[i]; // length
if(bBig)
{
var64 += uChannel[i] * 4;
}
lenLayerInfo += var64;
}
if( 1 == lenLayerInfo%2 ) //odd
{
oddSizedOld = 1;
}
// length of new channel
var64 = 4*4 + 2 + psdchannels * 6 + 4 + 4 + 4 * 1 + 4 + 12 + psdchannels * channelLength;
if(bBig)
{
var64 += psdchannels * 4;
}
if(hasClipMask)
var64 += 20;
lenLayerInfo += var64;
// Length of the layers info section, rounded up to a multiple of 2.
if( 1 == lenLayerInfo%2 ) // odd
{
oddSized = 1; lenLayerInfo++;
}
// Length of the layers info section, rounded up to a multiple of 2.
panoWriteINT32or64( fnum, lenLayerInfo + 4 + (bBig?8:4), bBig );
panoWriteINT32or64( fnum, lenLayerInfo, bBig );
// 2 bytes Count Number of layers. If <0, then number of layers is absolute value,
// and the first alpha channel contains the transparency data for
// the merged result.
panoWriteSHORT( fnum, numLayers + 1);
// Write previous layers, read channel data
for(i=0; i<numLayers; i++)
{
panoWriteINT32( fnum, nRect[i].top ); // Layer top
panoWriteINT32( fnum, nRect[i].left ); // Layer left
panoWriteINT32( fnum, nRect[i].bottom ); // Layer bottom
panoWriteINT32( fnum, nRect[i].right ); // Layer right
panoWriteSHORT( fnum, uChannel[i] ); // The number of channels in the layer.
// ********** Channel information ***************************** //
panoWriteSHORT( fnum, chid[i*5 +0] ); // red
panoWriteINT32or64( fnum, chlength[i], bBig ); // Length of following channel data.
panoWriteSHORT( fnum, chid[i*5 +1] ); // green
panoWriteINT32or64( fnum, chlength[i], bBig ); // Length of following channel data.
panoWriteSHORT( fnum, chid[i*5 +2] ); // blue
panoWriteINT32or64( fnum, chlength[i], bBig ); // Length of following channel data.
if( uChannel[i] > 3 ) // 1st alpha channel
{
panoWriteSHORT( fnum, chid[i*5 +3] ); // channel ID
panoWriteINT32or64( fnum, chlength[i], bBig );// Length of following channel data.
}
if( uChannel[i] > 4 ) // 2nd alpha channel
{
panoWriteSHORT( fnum, chid[i*5 +4] ); // channel ID
panoWriteINT32or64( fnum, chlength[i], bBig );// Length of following channel data.
}
// ********** End Channel information ***************************** //
// panoWriteINT32( fnum, '8BIM'); // Blend mode signature Always 8BIM.
panoWriteUCHAR( fnum, '8' );
panoWriteUCHAR( fnum, 'B' );
panoWriteUCHAR( fnum, 'I' );
panoWriteUCHAR( fnum, 'M' );
// panoWriteINT32( fnum, 'norm'); // Blend mode key
if (i == 0) {
//use norm
panoWriteUCHAR( fnum, 'n' );
panoWriteUCHAR( fnum, 'o' );
panoWriteUCHAR( fnum, 'r' );
panoWriteUCHAR( fnum, 'm' );
} else {
// use the desired one.
// XXX TODO: read the blending more from the layer so we can write it the same way back
assert(sizeof(psdBlendingModesNames) == sizeof(psdBlendingModesInternalName));
blendingModeKey = psdBlendingModesInternalName[sB->psdBlendingMode];
for (j = 0; j< 4; j++ ) {
panoWriteUCHAR( fnum, blendingModeKey[j]);
}
}
panoWriteINT32( fnum, uFlag[i] ); // Opacity, clipping, flag, filler
panoWriteINT32( fnum, uExtra[i] ); // Extra data size Length of the extra data field. This is the total length of the next five fields.
panoWriteINT32( fnum, uMask[i] ); // Layer Mask data
if(uMask[i])
for(k=0; k<uMask[i]/4; k++) // either 0 or 20
{
panoWriteINT32( fnum, uMaskData[i*5 +k] ); // Layer Mask Data
}
panoWriteINT32( fnum, 0 ); // Layer blending ranges
panoWriteINT32( fnum, cNames[i] ); // Layer name
}
// ************** The New Layer ************************************ //
// ********** Layer Structure ********************************* //
// PrintError("Adding layer : top %d, left %d, bottom %d, right %d, size %d\n",
// theRect.top, theRect.left, theRect.bottom, theRect.right, channelLength);
panoWriteINT32( fnum, theRect.top ); // Layer top
panoWriteINT32( fnum, theRect.left ); // Layer left
panoWriteINT32( fnum, theRect.bottom ); // Layer bottom
panoWriteINT32( fnum, theRect.right ); // Layer right
panoWriteSHORT( fnum, psdchannels ); // The number of channels in the layer.
// ********** Channel information ***************************** //
panoWriteSHORT( fnum, 0 ); // red
panoWriteINT32or64( fnum, channelLength, bBig ); // Length of following channel data.
panoWriteSHORT( fnum, 1 ); // green
panoWriteINT32or64( fnum, channelLength, bBig ); // Length of following channel data.
panoWriteSHORT( fnum, 2 ); // blue
panoWriteINT32or64( fnum, channelLength, bBig ); // Length of following channel data.
if( hasClipMask ) // Mask channel
{
panoWriteSHORT( fnum, -1 ); // Shape Mask
panoWriteINT32or64( fnum, channelLength, bBig ); // Length of following channel data.
panoWriteSHORT( fnum, -2 ); // Clip Mask
panoWriteINT32or64( fnum, channelLength, bBig ); // Length of following channel data.
}
// ********** End Channel information ***************************** //
// panoWriteINT32( fnum, '8BIM'); // Blend mode signature Always 8BIM.
panoWriteUCHAR( fnum, '8' );
panoWriteUCHAR( fnum, 'B' );
panoWriteUCHAR( fnum, 'I' );
panoWriteUCHAR( fnum, 'M' );
// the next 4 bytes are the blending mode key
/* panoWriteINT32( fnum, 'norm'); // Blend mode key */
assert(PSD_NUMBER_BLENDING_MODES == sizeof(psdBlendingModesNames)/sizeof(char*));
assert(sizeof(psdBlendingModesNames) == sizeof(psdBlendingModesInternalName));
blendingModeKey = psdBlendingModesInternalName[sB->psdBlendingMode];
for (j = 0; j< 4; j++ )
{
panoWriteUCHAR( fnum, blendingModeKey[j] );
}
panoWriteUCHAR( fnum, sB->psdOpacity ); // 1 byte Opacity 0 = transparent ... 255 = opaque
panoWriteUCHAR( fnum, 0 ); // 1 byte Clipping 0 = base, 1 = nonbase
panoWriteUCHAR( fnum, hasShapeMask ); // 1 byte Flags bit 0 = transparency protected bit 1 = visible
panoWriteUCHAR( fnum, 0 ); // 1 byte (filler) (zero)
if(hasClipMask)
{
panoWriteINT32( fnum, 32 ); // Extra data size Length of the extra data field. This is the total length of the next five fields.
panoWriteINT32( fnum, 20 ); // Yes Layer Mask data
panoWriteINT32( fnum, theRect.top ); // Layer top
panoWriteINT32( fnum, theRect.left ); // Layer left
panoWriteINT32( fnum, theRect.bottom ); // Layer bottom
panoWriteINT32( fnum, theRect.right ); // Layer right
panoWriteUCHAR( fnum, 0 ); // Default color 0 or 255
panoWriteUCHAR( fnum, 0 ); // Flag: bit 0 = position relative to layer bit 1 = layer mask disabled bit 2 = invert layer mask when blending
panoWriteUCHAR( fnum, 0 ); // padding
panoWriteUCHAR( fnum, 0 ); // padding
}
else
{
panoWriteINT32( fnum, 12 ); // Extra data size Length of the extra data field. This is the total length of the next five fields.
panoWriteINT32( fnum, 0 ); // No Layer Mask data
}
panoWriteINT32( fnum, 0 ); // Layer blending ranges
snprintf( &(sLayerName[1]), 3, "%03d", numLayers+1 );
sLayerName[0] = 3;
count = 4;
mywrite( fnum, count, sLayerName ); // Layer Name
// ************* End Layer Structure ******************************* //
// ************* Write Channel Image data ************************** //
for( i=0; i< numLayers; i++)
{
buf = (unsigned char**) mymalloc( chlength[i] );
if( buf == NULL )
{
PrintError("Not enough memory, %d bytes needed", chlength[i]);
result = -1;
goto _addLayer_exit;
}
for( k=0; k< uChannel[i]; k++ )
{
fileCopy( src, fnum, chlength[i], *buf );
//printf("Compression Layer %d Channel %d: %d\n", i,k,(int)*((short*)*buf));
if( chid[i*5 +k] == -1 ) // found an alpha channel
{
if( alpha!= NULL )
{
orAlpha( *alpha, &((*buf)[2]), im, &(nRect[i]) );
}
}
}
myfree( (void**)buf );
}
if( 1 == oddSizedOld%2 ) // If an odd number of odd layers then there is a single pad byte
{
panoReadUCHAR( src, &ch );
}
// Write color channels
for( i=0; i<3; i++)
{
if( writeChannelData( im, fnum, i + channels - 3, &theRect ) )
{
result = -1;
goto _addLayer_exit;
}
}
if( hasShapeMask ) // Alpha channel present
{
if( writeChannelData( im, fnum, 0, &theRect ) )
{
result = -1;
goto _addLayer_exit;
}
}
else
{
if( writeTransparentAlpha(im, fnum, &theRect ) )
{
result = -1;
goto _addLayer_exit;
}
}
if( hasClipMask )
{
if( sB->seam == _middle && alpha != NULL ) // Create stitching mask
{
mergeAlpha( im, *alpha, sB->feather, &theRect );
#if 0
{
static nim = 0;
fullPath fp;
makePathToHost( &fp );
sprintf( (char*)fp.name, "Test.%d", nim++);
PrintError("creating file: %s", fp.name);
c2pstr((char*)fp.name);
mycreate( &fp, '8BIM', '8BPS' );
writePSD(im, &fp );
}
#endif
}
if( writeChannelData( im, fnum, 0, &theRect ) )
{
result = -1;
goto _addLayer_exit;
}
}
if( oddSized ) // pad byte
{
panoWriteUCHAR( fnum, 0 );
}
// ************* End Write Channel Image data ************************** //
// ************* Global layer mask info ******************************** //
panoReadINT32( src, &var ); // Length of global layer mask info section.
panoWriteINT32( fnum, 0 );
//panoReadSHORT( src, &svar ); panoWriteSHORT( fnum, 0 ); // 2 bytes Overlay color space
//panoReadSHORT( src, &svar ); panoWriteSHORT( fnum, 0 ); // 4 * 2 byte color components
//panoReadSHORT( src, &svar ); panoWriteSHORT( fnum, 0 );
//panoReadSHORT( src, &svar ); panoWriteSHORT( fnum, 0 );
//panoReadSHORT( src, &svar ); panoWriteSHORT( fnum, 0 );
//panoReadSHORT( src, &svar ); panoWriteSHORT( fnum, 0 ); // 2 bytes Opacity 0 = transparent, 100 = opaque.
//panoReadSHORT( src, &svar ); panoWriteUCHAR( fnum, 128 ); // 1 byte Kind 0=Color selectedi.e. inverted;
// 1=Color protected;128=use value stored per layer.
// This value is preferred. The others are for back-ward
// compatibility with beta versions.
//panoWriteUCHAR( fnum, 0 );
_addLayer_exit:
if( alpha != NULL ) myfree( (void**)alpha );
if( chlength != NULL ) free(chlength);
if( uChannel != NULL ) free(uChannel);
if( nRect != NULL ) free(nRect);
if( cNames != NULL ) free(cNames);
if( uExtra != NULL ) free(uExtra);
if( uMask != NULL ) free(uMask);
if( uMaskData != NULL ) free(uMaskData);
if( chid != NULL ) free(chid);
if( uFlag != NULL ) free(uFlag);
return result;
}
int panoCreateLayeredPSD(fullPath * fullPathImages, int numberImages,
fullPath * outputFileName, pano_flattening_parms *flatteningParms)
{
int result = 0;
Image image;
Boolean bBig = FALSE;
file_spec fnum;
int BitsPerChannel;
int channels;
int psdchannels;
int hasClipMask = 0; // Create a mask
int hasShapeMask = 0; // Create alpha channel
unsigned char **alpha = NULL;
stBuf stitchInfo;
char *blendingModeKey;
char sLayerName[5];
PTRect theRect;
char tempString[128];
size_t saveLocation = 0;
size_t saveLocationForSize=0;
size_t saveLocationForLayerRecords=0;
int64_t channelLength;
int count;
int i;
int j;
assert(numberImages > 0);
assert(fullPathImages != NULL);
assert(outputFileName != NULL);
if (ptQuietFlag == 0) {
Progress(_initProgress, "Converting TIFF to PSD");
snprintf(tempString, sizeof(tempString)-1, "%d", 100 / numberImages);
Progress(_setProgress, tempString);
}
// Process background of PSD
SetImageDefaults(&image);
if (panoTiffRead(&image, fullPathImages[0].name) == 0) {
PrintError("Could not read TIFF image No 0 %s", fullPathImages[0].name);
if (ptQuietFlag == 0)
Progress(_disposeProgress, tempString);
return -1;
}
// Check to see if we need to create PSB instead of PSD file
if(panoImageFullHeight(&image) > 30000 || panoImageFullWidth(&image) > 30000 || flatteningParms->forceBig == 1)
bBig = TRUE;
if (!(image.bitsPerPixel == 64 || image.bitsPerPixel == 32)) {
PrintError("Image type not supported (%d bits per pixel)\n",
image.bitsPerPixel);
return -1;
}
// New versions of Photoshop can handle multilayer 16bit files.
// Down sample to 8bit only if user request.
if (numberImages > 1 && image.bitsPerPixel != 32) {
if (image.bitsPerPixel == 64 && flatteningParms->force8bit == 1) {
TwoToOneByte(&image); //we need to downsample to 8 bit if we are provided 16 bit images
}
}
// Determine the number of channels that are going to be writen in all of the photoshop layers.
// All layers are going to have the same number of channels.
// The first image is going to determin if the layers will have shape or clip masks.
GetBitsPerChannel( &image, BitsPerChannel );
GetChannels( &image, channels );
psdchannels = channels;
stitchInfo.seam = _dest; // @ToDo Pass this in with flatteningParms
stitchInfo.feather = 0;
stitchInfo.psdOpacity = 255;
assert(PSD_NUMBER_BLENDING_MODES == sizeof(psdBlendingModesNames)/sizeof(char*));
assert(sizeof(psdBlendingModesNames) == sizeof(psdBlendingModesInternalName));
blendingModeKey = psdBlendingModesInternalName[flatteningParms->psdBlendingMode];// The blending mode key
if( channels > 3 ) // Alpha channel present
{
hasClipMask = 1;
psdchannels++;
if ( !hasFeather( &image ) ) // If there is feathering do Not use Alpha channel for Shape Mask
{ // Only use alpha if there is no feathering
hasShapeMask = 1; // The Alpha channel can be used as a Shape Mask
}
if( stitchInfo.seam == _middle ) // we have to stitch
{
size_t AlaphaSize = (size_t) panoImageFullWidth(&image) * panoImageFullHeight(&image) * (BitsPerChannel/8);
alpha = (unsigned char**) mymalloc(AlaphaSize );
if( alpha != NULL )
{
memset( *alpha, 0 , AlaphaSize);
}
else
{
PrintError("Not enough memory to create Alpha channel for blending");
// Try to continue without Alpha
}
}
}
if( myopen( outputFileName, write_bin, fnum ) )
{
PrintError("Error Writing Image File");
return -1;
}
// Write PSD and PSB Header
// panoWriteINT32( fnum, '8BPS' );
panoWriteUCHAR( fnum, '8' );
panoWriteUCHAR( fnum, 'B' );
panoWriteUCHAR( fnum, 'P' );
panoWriteUCHAR( fnum, 'S' );
panoWriteSHORT( fnum, bBig? 2 : 1 ); // PSD file must be 1, PSB files must be 2
panoWriteINT32( fnum, 0 ); panoWriteSHORT( fnum, 0 ); // 6 bytes zeroed
panoWriteSHORT( fnum, 3 ); // No of channels; Background always white, 3 channels
//The Photoshop file has the dimensions of the full size image, regardless
//of whether the TIFF file is "cropped" or not.
// PSD file is limited to 30000 in width and height, PSB are limited to 300000 in width and height
panoWriteINT32( fnum, panoImageFullHeight(&image) ); // Rows
panoWriteINT32( fnum, panoImageFullWidth(&image) ); // Columns
panoWriteSHORT( fnum, BitsPerChannel ); // BitsPerChannel
switch( image.dataformat )
{
case _Lab: panoWriteSHORT( fnum, 9 );
break;
case _RGB: panoWriteSHORT( fnum, 3 );
break;
default: panoWriteSHORT( fnum, 3 );
}
panoWriteINT32( fnum, 0 ); // Color Mode
panoPSDResourcesBlockWrite( &image, fnum );
// Write an empty length first, then "rewind" and write its real one.
// It will make things way easier and more maintainable.
// Save our Current location so we can come back later.
saveLocationForSize = ftell( fnum );
// Place holder for Length of the Layer and mask information section
panoWriteINT32or64( fnum, 0, bBig );
// Place holder for Length of the Layer info
panoWriteINT32or64( fnum, 0, bBig );
// 2 bytes Count Number of layers.
panoWriteSHORT( fnum, numberImages );
saveLocationForLayerRecords = ftell( fnum );
//Now iterate over the images and add placeholders them as layers to the PSD file
for (i = 0; i < numberImages; i++) {
//Add placeholders for all the layer records to be filled in later.
panoWriteINT32( fnum, 0 ); // Layer top
panoWriteINT32( fnum, 1 ); // Layer left
panoWriteINT32( fnum, 2 ); // Layer bottom
panoWriteINT32( fnum, 3 ); // Layer right
panoWriteSHORT( fnum, psdchannels ); // The number of channels in the layer.
// ********** Channel information ***************************** //
panoWriteSHORT( fnum, 0 ); // red
panoWriteINT32or64( fnum, 00, bBig ); // Length of following channel data.
panoWriteSHORT( fnum, 1 ); // green
panoWriteINT32or64( fnum, 11, bBig ); // Length of following channel data.
panoWriteSHORT( fnum, 2 ); // blue
panoWriteINT32or64( fnum, 22, bBig ); // Length of following channel data.
if( hasClipMask ) // Mask channel
{
panoWriteSHORT( fnum, -1 ); // Shape Mask
panoWriteINT32or64( fnum, 11, bBig ); // Length of following channel data.
panoWriteSHORT( fnum, -2 ); // Clip Mask
panoWriteINT32or64( fnum, 22, bBig ); // Length of following channel data.
}
// ********** End Channel information ***************************** //
// panoWriteINT32( fnum, '8BIM'); // Blend mode signature Always 8BIM.
panoWriteUCHAR( fnum, '8' );
panoWriteUCHAR( fnum, 'B' );
panoWriteUCHAR( fnum, 'I' );
panoWriteUCHAR( fnum, 'M' );
// the next 4 bytes are the blending mode key
panoWriteUCHAR( fnum, 'n' );
panoWriteUCHAR( fnum, 'o' );
panoWriteUCHAR( fnum, 'r' );
panoWriteUCHAR( fnum, 'm' );
panoWriteUCHAR( fnum, 255 ); // 1 byte Opacity 0 = transparent ... 255 = opaque
panoWriteUCHAR( fnum, 0 ); // 1 byte Clipping 0 = base, 1 = nonbase
panoWriteUCHAR( fnum, hasShapeMask ); // 1 byte Flags bit 0 = transparency protected bit 1 = visible
panoWriteUCHAR( fnum, 0 ); // 1 byte (filler) (zero)
if(hasClipMask)
{
panoWriteINT32( fnum, 32 ); // Extra data size Length of the extra data field. This is the total length of the next five fields.
panoWriteINT32( fnum, 20 ); // Yes Layer Mask data
panoWriteINT32( fnum, 0 ); // Layer top
panoWriteINT32( fnum, 1 ); // Layer left
panoWriteINT32( fnum, 2 ); // Layer bottom
panoWriteINT32( fnum, 3 ); // Layer right
panoWriteUCHAR( fnum, 0 ); // Default color 0 or 255
panoWriteUCHAR( fnum, 0 ); // Flag: bit 0 = position relative to layer bit 1 = layer mask disabled bit 2 = invert layer mask when blending
panoWriteUCHAR( fnum, 0 ); // padding
panoWriteUCHAR( fnum, 0 ); // padding
}
else
{
panoWriteINT32( fnum, 12 ); // Extra data size Length of the extra data field. This is the total length of the next five fields.
panoWriteINT32( fnum, 0 ); // No Layer Mask data
}
panoWriteINT32( fnum, 0 ); // Layer blending ranges
snprintf(&(sLayerName[1]), 3, "%03d", (i+1)%1000 );
sLayerName[0] = 3;
count = 4;
mywrite( fnum, count, sLayerName ); // Layer Name
}
//Now iterate over the images and add them as layers to the PSD file
for (i = 0; i < numberImages; i++) {
if (ptQuietFlag == 0) {
snprintf(tempString, sizeof(tempString)-1, "%d", i * 100 / numberImages);
if (Progress(_setProgress, tempString) == 0) {
remove(outputFileName->name);
return -1;
}
}
// We already read in image 0, so we skip it here
if (i != 0 && panoTiffRead( &image, fullPathImages[i].name) == 0) {
PrintError("Could not read TIFF image No &d", i);
if (ptQuietFlag == 0)
Progress(_disposeProgress, tempString);
return -1;
}
// downsample 16bit to 8 bit if necessary
if (image.bitsPerPixel == 64 && flatteningParms->force8bit == 1)
TwoToOneByte( &image );
if (flatteningParms->stacked)
stitchInfo.psdOpacity = (unsigned char) (255.0/ (i + 1));
// else we leave it at 255
getImageRectangle( &image, &theRect );
// Uncompressed channel length data
// @ToDo Save the data compressed and record the actual lengths
channelLength = ((size_t)theRect.right-theRect.left) * (theRect.bottom-theRect.top) * (BitsPerChannel/8) + 2;
// Write color channels
for( j=0; j<3; j++)
{
if( writeChannelData( &image, fnum, j + channels - 3, &theRect ) )
{
result = -1;
goto _addLayer_exit;
}
}
if( hasShapeMask ) // Alpha channel present
{
if( writeChannelData( &image, fnum, 0, &theRect ) )
{
result = -1;
goto _addLayer_exit;
}
}
else
{
if( writeTransparentAlpha( &image, fnum, &theRect ) )
{
result = -1;
goto _addLayer_exit;
}
}
if( hasClipMask )
{
if( stitchInfo.seam == _middle && alpha != NULL ) // Create stitching mask
{
// orAlpha( *alpha, &((*buf)[2]), im, &(nRect[i]) );
mergeAlpha( &image, *alpha, stitchInfo.feather, &theRect );
}
if( writeChannelData( &image, fnum, 0, &theRect ) )
{
result = -1;
goto _addLayer_exit;
}
}
// Remember our place writeing channel data
saveLocation = ftell( fnum );
// Go back to fill in actual data for the Layer recode
fseek(fnum, saveLocationForLayerRecords, SEEK_SET);
//Replace the layer records data.
panoWriteINT32( fnum, theRect.top ); // Layer top
panoWriteINT32( fnum, theRect.left ); // Layer left
panoWriteINT32( fnum, theRect.bottom ); // Layer bottom
panoWriteINT32( fnum, theRect.right ); // Layer right
panoWriteSHORT( fnum, psdchannels ); // The number of channels in the layer.
// ********** Channel information ***************************** //
panoWriteSHORT( fnum, 0 ); // red
panoWriteINT32or64( fnum, channelLength, bBig ); // Length of following channel data.
panoWriteSHORT( fnum, 1 ); // green
panoWriteINT32or64( fnum, channelLength, bBig ); // Length of following channel data.
panoWriteSHORT( fnum, 2 ); // blue
panoWriteINT32or64( fnum, channelLength, bBig ); // Length of following channel data.
if( hasClipMask ) // Mask channel
{
panoWriteSHORT( fnum, -1 ); // Shape Mask
panoWriteINT32or64( fnum, channelLength, bBig ); // Length of following channel data.
panoWriteSHORT( fnum, -2 ); // Clip Mask
panoWriteINT32or64( fnum, channelLength, bBig ); // Length of following channel data.
}
// ********** End Channel information ***************************** //
// panoWriteINT32( fnum, '8BIM'); // Blend mode signature Always 8BIM.
panoWriteUCHAR( fnum, '8' );
panoWriteUCHAR( fnum, 'B' );
panoWriteUCHAR( fnum, 'I' );
panoWriteUCHAR( fnum, 'M' );
// the next 4 bytes are the blending mode key
/* panoWriteINT32( fnum, 'norm'); // Blend mode key */
if(i==0)
{
panoWriteUCHAR( fnum, 'n' );
panoWriteUCHAR( fnum, 'o' );
panoWriteUCHAR( fnum, 'r' );
panoWriteUCHAR( fnum, 'm' );
}
else
{
for (j = 0; j< 4; j++ )
{
panoWriteUCHAR( fnum, blendingModeKey[j] );
}
}
panoWriteUCHAR( fnum, stitchInfo.psdOpacity ); // 1 byte Opacity 0 = transparent ... 255 = opaque
panoWriteUCHAR( fnum, 0 ); // 1 byte Clipping 0 = base, 1 = nonbase
panoWriteUCHAR( fnum, hasShapeMask ); // 1 byte Flags bit 0 = transparency protected bit 1 = visible
panoWriteUCHAR( fnum, 0 ); // 1 byte (filler) (zero)
if(hasClipMask)
{
panoWriteINT32( fnum, 32 ); // Extra data size Length of the extra data field. This is the total length of the next five fields.
panoWriteINT32( fnum, 20 ); // Yes Layer Mask data
panoWriteINT32( fnum, theRect.top ); // Layer top
panoWriteINT32( fnum, theRect.left ); // Layer left
panoWriteINT32( fnum, theRect.bottom ); // Layer bottom
panoWriteINT32( fnum, theRect.right ); // Layer right
panoWriteUCHAR( fnum, 0 ); // Default color 0 or 255
panoWriteUCHAR( fnum, 0 ); // Flag: bit 0 = position relative to layer bit 1 = layer mask disabled bit 2 = invert layer mask when blending
panoWriteUCHAR( fnum, 0 ); // padding
panoWriteUCHAR( fnum, 0 ); // padding
}
else
{
panoWriteINT32( fnum, 12 ); // Extra data size Length of the extra data field. This is the total length of the next five fields.
panoWriteINT32( fnum, 0 ); // No Layer Mask data
}
panoWriteINT32( fnum, 0 ); // Layer blending ranges
snprintf( &(sLayerName[1]), 3, "%03d", i+1 );
sLayerName[0] = 3;
count = 4;
mywrite( fnum, count, sLayerName ); // Layer Name
// The location of the next Layer record
saveLocationForLayerRecords = ftell( fnum );
// Get ready to write the next. This should be the same as going to the end.
fseek( fnum, saveLocation, SEEK_SET );
}
if( (ftell( fnum ) - saveLocationForSize)%2 )
{
panoWriteUCHAR( fnum, 0 );
}
// Rewind back to write the actual size of all the layer data
saveLocation = ftell( fnum );
fseek( fnum, saveLocationForSize, SEEK_SET );
panoWriteINT32or64( fnum, saveLocation - saveLocationForSize - (bBig?8:4) + 4, bBig );
panoWriteINT32or64( fnum, saveLocation - saveLocationForSize - (bBig?8:4)*2 , bBig );
// write the background data
fseek( fnum, saveLocation, SEEK_SET );
// ************* Global layer mask info ******************************** //
panoWriteINT32( fnum, 0 ); // Length of global layer mask info section.
writeWhiteBackground( panoImageFullWidth(&image) * (BitsPerChannel/8), panoImageFullHeight(&image), fnum, bBig );
myclose (fnum );
if (!ptQuietFlag) {
Progress(_setProgress, "100");
Progress(_disposeProgress, tempString);
}
_addLayer_exit:
if( alpha != NULL ) myfree( (void**)alpha );
return result;
}
static int fileCopy( file_spec src, file_spec dest, size_t numBytes, unsigned char *buf )
{
size_t count = numBytes;
myread( src, count, buf );
mywrite( dest, count, buf );
return 0;
}
// Or two alpha channels: one in alpha (same size as image im)
// one in buf comprising the rectangle top,bottom,left,right
// store result in alpha
static void orAlpha( unsigned char* alpha, unsigned char *buf, Image *im, PTRect *theRect )
{
register unsigned int x,y,ay,by,w;
int BitsPerChannel;
GetBitsPerChannel( im, BitsPerChannel );
if( im->bitsPerPixel != 32 && im->bitsPerPixel != 64 && im->bitsPerPixel != 128) return;
w = (theRect->right - theRect->left);
if( BitsPerChannel == 8 )
{
for(y=theRect->top; y<theRect->bottom; y++)
{
ay = y * im->width;
by = (y - theRect->top) * w;
{
for(x=theRect->left; x<theRect->right; x++)
{
if( buf[ by + x - theRect->left ] )
alpha[ ay + x ] = 255U;
}
}
}
}
if( BitsPerChannel == 16 )
{
for(y=theRect->top; y<theRect->bottom; y++)
{
ay = y * im->width * 2;
by = (y - theRect->top) * w * 2;
{
for(x=theRect->left; x<theRect->right; x++)
{
if( *((uint16_t*)(buf+ by + 2*(x - theRect->left))) )
*((uint16_t*)(alpha + ay + 2*x) ) = 65535U;
}
}
}
}
if( BitsPerChannel == 32 )
{
for(y=theRect->top; y<theRect->bottom; y++)
{
ay = y * im->width * 2;
by = (y - theRect->top) * w * 2;
{
for(x=theRect->left; x<theRect->right; x++)
{
if( *((float*)(buf+ by + 2*(x - theRect->left))) )
*((float*)(alpha + ay + 2*x) ) = 1.0;
}
}
}
}
}
void FindScript( aPrefs *thePrefs )
{
FindFile( &(thePrefs->scriptFile) );
}
int LoadOptions( cPrefs * thePrefs )
{
fullPath path;
file_spec fnum;
struct correct_Prefs loadPrefs;
size_t count;
int result;
if( FindFile( &path ) )
{
return -1;
}
if( myopen( &path, read_bin, fnum ) )
{
PrintError("Could not open file");
return -1;
}
count = sizeof( struct correct_Prefs );
myread( fnum, count, &loadPrefs );
if( count != sizeof( struct correct_Prefs) || loadPrefs.magic != 20 )
{
PrintError( "Wrong format!" );
result = -1;
}
else
{
memcpy( (char*) thePrefs, (char*)&loadPrefs, sizeof(struct correct_Prefs) );
result = 0;
}
myclose(fnum);
return result;
}
char* LoadScript( fullPath* scriptFile )
{
fullPath sfile;
int i;
file_spec fnum;
size_t count;
char *script = NULL, ch;
memset( &sfile, 0, sizeof( fullPath ) );
if( memcmp( scriptFile, &sfile, sizeof( fullPath ) ) == 0 )
{
PrintError("No Scriptfile selected");
goto _loadError;
}
if( myopen( scriptFile, read_text, fnum ) )
{
PrintError("Error Opening Scriptfile: %s", scriptFile->name);
goto _loadError;
}
count = 1; i=0; // Get file length
while( count == 1 )
{
myread( fnum, count, &ch );
if(count==1) i++;
}
myclose(fnum);
count = i;
script = (char*)malloc( count+1 );
if( script == NULL )
{
PrintError("Not enough memory to load scriptfile");
goto _loadError;
}
if( myopen( scriptFile, read_text, fnum ))
{
PrintError("Error Opening Scriptfile: %s", scriptFile->name);
free(script);
goto _loadError;
}
myread(fnum,count,script);
script[count] = 0;
myclose(fnum);
return script;
_loadError:
return (char*)NULL;
}
int WriteScript( char* res, fullPath* scriptFile, int launch )
{
fullPath sfile;
file_spec fnum;
size_t count;
memset( &sfile, 0, sizeof( fullPath ) );
if( memcmp( scriptFile, &sfile, sizeof( fullPath ) ) == 0 )
{
PrintError("No Scriptfile selected");
goto _writeError;
}
memcpy( &sfile, scriptFile, sizeof (fullPath) );
mydelete( &sfile );
mycreate(&sfile,'ttxt','TEXT');
if( myopen( &sfile, write_text, fnum ) )
{
PrintError("Error Opening Scriptfile");
goto _writeError;
}
count = strlen( res );
mywrite( fnum, count, res );
myclose (fnum );
if( launch == 1 )
{
showScript( &sfile);
}
return 0;
_writeError:
return -1;
}
void SaveOptions( cPrefs * thePrefs )
{
fullPath path;
file_spec fspec;
size_t count;
memset( &path, 0, sizeof( fullPath ) );
if( SaveFileAs( &path, "Save Settings as..", "Params" ) )
return;
mycreate (&path,'GKON','TEXT');
if( myopen( &path, write_bin, fspec ) )
return;
count = sizeof( cPrefs );
mywrite( fspec, count, thePrefs );
myclose( fspec );
}
// Temporarily save a 32bit image (including Alpha-channel) using name fname
int SaveBufImage( Image *image, char *fname )
{
fullPath fspec;
MakeTempName( &fspec, fname );
mydelete( &fspec );
mycreate( &fspec, '8BIM', '8BPS' );
return writePSD( image, &fspec );
}
// Load a saved 32bit image (including Alpha-channel) using name fname
// image must be allocated (not data)
// mode = 0: only load image struct
// mode = 1: also allocate and load data
int LoadBufImage( Image *image, char *fname, int mode)
{
fullPath fspec;
MakeTempName( &fspec, fname );
return readPSD( image, &fspec, mode );
}
// Read Photoshop Multilayer-image
int readPSDMultiLayerImage( MultiLayerImage *mim, fullPath* sfile){
file_spec src;
char header[128], *h;
int64_t chlength;
size_t count, iul, kul;
uint32_t var;
int64_t var64;
uint16_t svar;
int i, k, result = 0, odd = 0;
uint16_t uChannel;
unsigned char **buf = NULL, ch;
Image im;
int BitsPerSample = 8;
Boolean bBig = FALSE;
SetImageDefaults( &im );
if( myopen( sfile,read_bin, src ) ){
PrintError("Error Opening Image File");
return -1;
}
// Read psd header
h = header;
count = PSDHLENGTH;
myread( src, count, h );
if( count != PSDHLENGTH ){
PrintError("Error Reading Image Header");
myclose( src );
return -1;
}
if( ParsePSDHeader( header, &im, &bBig ) != 0 ){
PrintError("readPSDMultiLayerImage: Wrong File Format");
myclose( src );
return -1;
}
// Read (and ignore) Color mode data
panoReadINT32( src, &var );
count = 1;
for( iul=0; iul<var; iul++ )
{
myread( src, count, h );
}
// Read (and ingnore) Image resources
panoReadINT32( src, &var );
count = 1;
for( iul=0; iul<var; iul++ )
{
myread( src, count, h );
}
// Read the layers
// Read layerinfo up to channeldata
panoReadINT32or64( src, &var64, bBig ); // Length of info section(ignored)
panoReadINT32or64( src, &var64, bBig ); // Length of layer info (ignored)
panoReadSHORT( src, &svar ); // Number of layers
mim->numLayers = svar;
mim->Layer = (Image*) malloc( mim->numLayers * sizeof( Image ) );
if( mim->Layer == NULL )
{
PrintError("Not enough memory");
result = -1;
goto readPSDMultiLayerImage_exit;
}
for(i=0; i<mim->numLayers; i++)
{
SetImageDefaults( &mim->Layer[i] );
mim->Layer[i].width = im.width;
mim->Layer[i].height = im.height;
panoReadINT32( src, (uint32_t*)&mim->Layer[i].selection.top ); // Layer top
panoReadINT32( src, (uint32_t*)&mim->Layer[i].selection.left ); // Layer left
panoReadINT32( src, (uint32_t*)&mim->Layer[i].selection.bottom ); // Layer bottom
panoReadINT32( src, (uint32_t*)&mim->Layer[i].selection.right ); // Layer right
panoReadSHORT( src, &uChannel ); // The number of channels in the layer.
mim->Layer[i].bitsPerPixel = uChannel * BitsPerSample;
mim->Layer[i].bytesPerLine = (mim->Layer[i].selection.right - mim->Layer[i].selection.left) *
mim->Layer[i].bitsPerPixel/8;
mim->Layer[i].dataSize = mim->Layer[i].bytesPerLine *
(mim->Layer[i].selection.bottom - mim->Layer[i].selection.top);
mim->Layer[i].data = (unsigned char**) mymalloc((size_t)(mim->Layer[i].dataSize));
if( mim->Layer[i].data == NULL )
{
PrintError("Not enough memory");
result = -1;
goto readPSDMultiLayerImage_exit;
}
// ********** Channel information ***************************** //
panoReadSHORT( src, &svar ); // red
panoReadINT32or64( src, &chlength, bBig ); // Length of following channel data.
panoReadSHORT( src, &svar ); // green
panoReadINT32or64( src, &var64, bBig ); // Length of following channel data.
panoReadSHORT( src, &svar ); // blue
panoReadINT32or64( src, &var64, bBig ); // Length of following channel data.
if( 3 < uChannel ) // alpha channel
{
panoReadSHORT( src, &svar ); // transparency mask
panoReadINT32or64( src, &var64, bBig ); // Length of following channel data.
}
if( 4 < uChannel ) // alpha channel
{
panoReadSHORT( src, &svar ); // transparency mask
panoReadINT32or64( src, &var64, bBig ); // Length of following channel data.
}
// ********** End Channel information ***************************** //
panoReadINT32( src, &var ); // Blend mode signature Always 8BIM.
panoReadINT32( src, &var ); // Blend mode key
panoReadINT32( src, &var ); // Four flag bytes
panoReadINT32( src, &var ); // Extra data size Length of the extra data field. This is the total length of the next five fields.
panoReadINT32( src, &var ); // Layer Mask data
if(var) // either 0 or 20
{
for(kul=0; kul<var; kul++)
{
panoReadUCHAR( src, &ch ); // Layer Mask Data
}
}
panoReadINT32( src, &var ); // Layer blending ranges
panoReadINT32( src, &var ); // Layer name
var64 = 4*4 + 2 + uChannel * 6 + 4 + 4 + 4 * 1 + 4 + 12 + uChannel * chlength; // length
if(bBig)
{
var64 += uChannel * 4;
}
if( var64/2 != (var64+1)/2 ) // odd
{
odd++;
}
}
// ************* End Layer Structure ******************************* //
// ************* Read Channel Image data ************************** //
for( i=0; i< mim->numLayers; i++)
{
uChannel = (uint16_t)(mim->Layer[i].bitsPerPixel/BitsPerSample);
chlength = mim->Layer[i].dataSize/ uChannel;
buf = (unsigned char**) mymalloc( chlength );
if( buf == NULL )
{
PrintError("Not enough memory");
result = -1;
goto readPSDMultiLayerImage_exit;
}
for( k=0; k< uChannel; k++ )
{
panoReadSHORT( src, &svar );
if( svar != 0 )
{
PrintError("File format error");
result = -1;
goto readPSDMultiLayerImage_exit;
}
count = chlength;
myread( src, count, *buf );
{
register int x,y,cy,by;
register unsigned char* theData = *(mim->Layer[i].data);
int offset,bpp;
int DataWidth = mim->Layer[i].selection.right - mim->Layer[i].selection.left;
int DataHeight = mim->Layer[i].selection.bottom - mim->Layer[i].selection.top;
// JMW ToDo Upgrade allow 16 bit.
offset = (mim->Layer[i].bitsPerPixel == 32?1:0) + k;
if( k==3 ) offset = 0;
bpp = mim->Layer[i].bitsPerPixel/8;
for(y=0; y<DataHeight; y++)
{
cy = y*mim->Layer[i].bytesPerLine + offset;
by = y*DataWidth;
for(x=0; x<DataWidth; x++)
{
theData[cy + bpp*x] = (*buf)[by + x];
}
}
}
}
myfree( (void**)buf );
}
if( 1 == odd%2 ) // pad byte
{
panoReadUCHAR( src, &ch );
}
readPSDMultiLayerImage_exit:
myclose( src );
return result;
}
// JMW: July 2 2004, Check to see if alpha has any semi transparent pixels.
// Also check to see if alpha is completely black
// If completely black or using feathering then alpha
// channel can not be used as shape mask.
int hasFeather ( Image *im )
{
register unsigned int y, x, a;
register unsigned char *alpha;
int BitsPerChannel;
GetBitsPerChannel( im, BitsPerChannel );
if( im->bitsPerPixel != 32 && im->bitsPerPixel != 64) return 1;
a = 1;
if( BitsPerChannel == 8 )
{
for(y=0; y<im->height; y++)
{
for(x=0, alpha = *(im->data) + y * im->bytesPerLine; x<im->width;
x++, alpha += 4)
{
if(a && *alpha != 0)// have data
a = 0;
if( *alpha != 0 && *alpha != 255 )
return 1;
}
}
}
else // 16
{
for(y=0; y<im->height; y++)
{
for(x=0, alpha = *(im->data) + y * im->bytesPerLine; x<im->width;
x++, alpha += 8)
{
if(a && *((uint16_t*)alpha) != 0 )// have data
a = 0;
if( *((uint16_t*)alpha) != 0xFFFF && *((uint16_t*)alpha) != 0 )
return 1;
}
}
}
return a;
}
// Create a temporary file
int panoFileMakeTemp(fullPath * path)
{
file_spec fnum;
char *dir;
static int nTry = 0;
int i;
char fname[40];
dir = strrchr(path->name, PATH_SEP);
if (dir == NULL) {
dir = path->name;
}
else {
dir++;
}
nTry++;
for (i = 0; i < MAX_TEMP_TRY; nTry++, i++) {
snprintf(fname, sizeof(fname)-1, "_PTStitcher_tmp_%06d", nTry);
if (strlen(fname) + 2 <
sizeof(path->name) - (strlen(path->name) - strlen(dir))) {
snprintf(dir, strlen(fname), "%s", fname);
// TODO (dmg -- 060730)
// THis routine is not perfect. IT does not check if the
// file is really a file, nor that there is write access to it
if (myopen(path, read_bin, fnum)) {
return TRUE;
}
myclose(fnum);
}
else {
PrintError("Path too long");
return FALSE;
}
}
return FALSE;
}
// Read an image
int panoImageRead(Image * im, fullPath * sfile)
{
char *ext, extension[5];
int i;
assert(sfile != NULL);
assert(im != NULL);
printf("Filename %s\n", sfile->name);
ext = strrchr(sfile->name, '.');
if (ext == NULL || strlen(ext) < 4 || strlen(ext) > 5) {
PrintError("Unsupported file format [%s]: must have extension JPG, JPEG, PNG, TIF, TIFF, BMP, PPM, PSD, PSB, or HDR", sfile);
return 0;
}
ext++;
strcpy(extension, ext);
// convert to lowercase
for (i = 0; i < 4; i++)
extension[i] = tolower(extension[i]);
if (strcmp(extension, "ppm") == 0) {
return panoPPMRead(im, sfile);
}
else if (strcmp(extension, "jpg") == 0 || strcmp(extension, "jpeg") == 0) {
return panoJPEGRead(im, sfile);
}
else if (strcmp(extension, "tif") == 0 || strcmp(extension, "tiff") == 0) {
return panoTiffRead(im, sfile->name);
}
else if (strcmp( extension, "bmp" ) == 0 ) {
#ifdef WIN32
return panoBMPRead( im, sfile );
#else
PrintError("BMP is not a supported format in this operating system");
return FALSE;
#endif
}
else if( strcmp( extension, "png" ) == 0 ) {
return panoPNGRead(im, sfile );
}
else if( strcmp( extension, "psd" ) == 0 || strcmp( extension, "psb" ) == 0 ) {
return readPSD(im, sfile, 1 );
}
else if (strcmp( extension, "hdr" ) == 0 ) {
return panoHDRRead(im, sfile );
}
else {
PrintError("Unsupported file format [%s]: must have extension JPG, JPEG, PNG, TIF, TIFF, BMP, PPM, PSD, PSB, or HDR", sfile);
return FALSE;
}
// it should never get here
assert(0);
}
// Takes a prefix, and creates a list of unique filenames
int panoFileOutputNamesCreate(fullPath *ptrOutputFiles, int filesCount, char *outputPrefix)
{
int i;
char outputFilename[MAX_PATH_LENGTH];
// I am sure we will never have more than 10000 images in a project!
#define DEFAULT_PREFIX_NUMBER_FORMAT "%04d"
printf("Output prefix %d %s\n",filesCount, outputPrefix);
if (strchr(outputPrefix, '%') == NULL) {
if ((strlen(outputPrefix) + strlen(DEFAULT_PREFIX_NUMBER_FORMAT)) >= MAX_PATH_LENGTH) {
PrintError("Output prefix too long [%s]", outputPrefix);
return 0;
}
strcat(outputPrefix, DEFAULT_PREFIX_NUMBER_FORMAT);
}
else {
// TODO: sanitize outputPrefix, only a single format specifier %??d or %??i
// is allowed, all other should be escaped
// until this is implemented refuse to process further if prefix string
// contains a percentage sign to prevent string vulnerability in
// sprintf(outputFilename, outputPrefix ...) below
PrintError("Output prefix must not contain a percentage sign");
return 0;
}
for (i =0; i< filesCount ; i++) {
snprintf( outputFilename, sizeof(outputFilename)-1, outputPrefix, i );
// Verify the filename is different from the prefix
if (strcmp(outputFilename, outputPrefix) == 0) {
PrintError("Invalid output prefix. It does not generate unique filenames.");
return -1;
}
if (StringtoFullPath(&ptrOutputFiles[i], outputFilename) != 0) {
PrintError("Syntax error: Not a valid pathname");
return(-1);
}
panoReplaceExt(ptrOutputFiles[i].name, ".tif");
// fprintf(stderr, "Output filename [%s]\n", ptrOutputFiles[i].name);
}
return 1;
}
// Find the first file that exists in the input array of filenames
char *panoFileExists(fullPath *ptrFiles, int filesCount)
{
int i;
FILE *testFile;
for (i =0; i< filesCount ; i++) {
if ((testFile = fopen(ptrFiles[i].name, "r"))!= NULL) {
fclose(testFile);
return ptrFiles[i].name;
}
}
return NULL;
}
// Check if a file exists. Returns 1 if it does exist, 0 otherwise
int panoSingleFileExists(char * filename)
{
FILE *testFile;
if ((testFile = fopen(filename, "r"))!= NULL) {
fclose(testFile);
return 1;
}
return 0;
}
#ifdef DONOTCOMPILE_TOBE_DELETED
I am not sure this function is used at all
int writeImage( Image *im, fullPath *sfile )
{
char *ext,extension[4];
int i;
ext = strrchr( sfile->name, '.' );
ext++;
strcpy( extension, ext );
for(i=0; i<3; i++)
extension[i] = tolower(extension[i]);
if( strcmp( extension, "jpg" ) == 0 )
return writeJPEG(im, sfile, 90, 0 );
else if( strcmp( extension, "tif" ) == 0 )
return writeTIFF(im, sfile );
else if( strcmp( extension, "png" ) == 0 )
return writePNG(im, sfile );
else if( strcmp( extension, "hdr" ) == 0 )
return writeHDR(im, sfile );
else {
return writeBMP( im, sfile );
}
}
#endif
int panoFileDeleteMultiple(fullPath* files, int filesCount)
{
extern int ptQuietFlag;
int i;
assert(files != NULL);
for (i = 0; i < filesCount; i++) {
if (!ptQuietFlag) {
PrintError("Deleting %-th source file %s", i, files[i].name);
}
if(remove(files[i].name) != 0) {
PrintError("Unable to remove file %s. Continuing", files[i].name);
}
}
return 1;
}
|