1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328 8329 8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8432 8433 8434 8435 8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446 8447 8448 8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542 8543 8544 8545 8546 8547 8548 8549 8550 8551 8552 8553 8554 8555 8556 8557 8558 8559 8560 8561 8562 8563 8564 8565 8566 8567 8568 8569 8570 8571 8572 8573 8574 8575 8576 8577 8578 8579 8580 8581 8582 8583 8584 8585 8586 8587 8588 8589 8590 8591 8592 8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610 8611 8612 8613 8614 8615 8616 8617 8618 8619 8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634 8635 8636 8637 8638 8639 8640 8641 8642 8643 8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661 8662 8663 8664 8665 8666 8667 8668 8669 8670 8671 8672 8673 8674 8675 8676 8677 8678 8679 8680 8681 8682 8683 8684 8685 8686 8687 8688 8689 8690 8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705 8706 8707 8708 8709 8710 8711 8712 8713 8714 8715 8716 8717 8718 8719 8720 8721 8722 8723 8724 8725 8726 8727 8728 8729 8730 8731 8732 8733 8734 8735 8736 8737 8738 8739 8740 8741 8742 8743 8744 8745 8746 8747 8748 8749 8750 8751 8752 8753 8754 8755 8756 8757 8758 8759 8760 8761 8762 8763 8764 8765 8766 8767 8768 8769 8770 8771 8772 8773 8774 8775 8776 8777 8778 8779 8780 8781 8782 8783 8784 8785 8786 8787 8788 8789 8790 8791 8792 8793 8794 8795 8796 8797 8798 8799 8800 8801 8802 8803 8804 8805 8806 8807 8808 8809 8810 8811 8812 8813 8814 8815 8816 8817 8818 8819 8820 8821 8822 8823 8824 8825 8826 8827 8828 8829 8830 8831 8832 8833 8834 8835 8836 8837 8838 8839 8840 8841 8842 8843 8844 8845 8846 8847 8848 8849 8850 8851 8852 8853 8854 8855 8856 8857 8858 8859 8860 8861 8862 8863 8864 8865 8866 8867 8868 8869 8870 8871 8872 8873 8874 8875 8876 8877 8878 8879 8880 8881 8882 8883 8884 8885 8886 8887 8888 8889 8890 8891 8892 8893 8894 8895 8896 8897 8898 8899 8900 8901 8902 8903 8904 8905 8906 8907 8908 8909 8910 8911 8912 8913 8914 8915 8916 8917 8918 8919 8920 8921 8922 8923 8924 8925 8926 8927 8928 8929 8930 8931 8932 8933 8934 8935 8936 8937 8938 8939 8940 8941 8942 8943 8944 8945 8946 8947 8948 8949 8950 8951 8952 8953 8954 8955 8956 8957 8958 8959 8960 8961 8962 8963 8964 8965 8966 8967 8968 8969 8970 8971 8972 8973 8974 8975 8976 8977 8978 8979 8980 8981 8982 8983 8984 8985 8986 8987 8988 8989 8990 8991 8992 8993 8994 8995 8996 8997 8998 8999 9000 9001 9002 9003 9004 9005 9006 9007 9008 9009 9010 9011 9012 9013 9014 9015 9016 9017 9018 9019 9020 9021 9022 9023 9024 9025 9026 9027 9028 9029 9030 9031 9032 9033 9034 9035 9036 9037 9038 9039 9040 9041 9042 9043 9044 9045 9046 9047 9048 9049 9050 9051 9052 9053 9054 9055 9056 9057 9058 9059 9060 9061 9062 9063 9064 9065 9066 9067 9068 9069 9070 9071 9072 9073 9074 9075 9076 9077 9078 9079 9080 9081 9082 9083 9084 9085 9086 9087 9088 9089 9090 9091 9092 9093 9094 9095 9096 9097 9098 9099 9100 9101 9102 9103 9104 9105 9106 9107 9108 9109 9110 9111 9112 9113 9114 9115 9116 9117 9118 9119 9120 9121 9122 9123 9124 9125 9126 9127 9128 9129 9130 9131 9132 9133 9134 9135 9136 9137 9138 9139 9140 9141 9142 9143 9144 9145 9146 9147 9148 9149 9150 9151 9152 9153 9154 9155 9156 9157 9158 9159 9160 9161 9162 9163 9164 9165 9166 9167 9168 9169 9170 9171 9172 9173 9174 9175 9176 9177 9178 9179 9180 9181 9182 9183 9184 9185 9186 9187 9188 9189 9190 9191 9192 9193 9194 9195 9196 9197 9198 9199 9200 9201 9202 9203 9204 9205 9206 9207 9208 9209 9210 9211 9212 9213 9214 9215 9216 9217 9218 9219 9220 9221 9222 9223 9224 9225 9226 9227 9228 9229 9230 9231 9232 9233 9234 9235 9236 9237 9238 9239 9240 9241 9242 9243 9244 9245 9246 9247 9248 9249 9250 9251 9252 9253 9254 9255 9256 9257 9258 9259 9260 9261 9262 9263 9264 9265 9266 9267 9268 9269 9270 9271 9272 9273 9274 9275 9276 9277 9278 9279 9280 9281 9282 9283 9284 9285 9286 9287 9288 9289 9290 9291 9292 9293 9294 9295 9296 9297 9298 9299 9300 9301 9302 9303 9304 9305 9306 9307 9308 9309 9310 9311 9312 9313 9314 9315 9316 9317 9318 9319 9320 9321 9322 9323 9324 9325 9326 9327 9328 9329 9330 9331 9332 9333 9334 9335 9336 9337 9338 9339 9340 9341 9342 9343 9344 9345 9346 9347 9348 9349 9350 9351 9352 9353 9354 9355 9356 9357 9358 9359 9360 9361 9362 9363 9364 9365 9366 9367 9368 9369 9370 9371 9372 9373 9374 9375 9376 9377 9378 9379 9380 9381 9382 9383 9384 9385 9386 9387 9388 9389 9390 9391 9392 9393 9394 9395 9396 9397 9398 9399 9400 9401 9402 9403 9404 9405 9406 9407 9408 9409 9410 9411 9412 9413 9414 9415 9416 9417 9418 9419 9420 9421 9422 9423 9424 9425 9426 9427 9428 9429 9430 9431 9432 9433 9434 9435 9436 9437 9438 9439 9440 9441 9442 9443 9444 9445 9446 9447 9448 9449 9450 9451 9452 9453 9454 9455 9456 9457 9458 9459 9460 9461 9462 9463 9464 9465 9466 9467 9468 9469 9470 9471 9472 9473 9474 9475 9476 9477 9478 9479 9480 9481 9482 9483 9484 9485 9486 9487 9488 9489 9490 9491 9492 9493 9494 9495 9496 9497 9498 9499 9500 9501 9502 9503 9504 9505 9506 9507 9508 9509 9510 9511 9512 9513 9514 9515 9516 9517 9518 9519 9520 9521 9522 9523 9524 9525 9526 9527 9528 9529 9530 9531 9532 9533 9534 9535 9536 9537 9538 9539 9540 9541 9542 9543 9544 9545 9546 9547 9548 9549 9550 9551 9552 9553 9554 9555 9556 9557 9558 9559 9560 9561 9562 9563 9564 9565 9566 9567 9568 9569 9570 9571 9572 9573 9574 9575 9576 9577 9578 9579 9580 9581 9582 9583 9584 9585 9586 9587 9588 9589 9590 9591 9592 9593 9594 9595 9596 9597 9598 9599 9600 9601 9602 9603 9604 9605 9606 9607 9608 9609 9610 9611 9612 9613 9614 9615 9616 9617 9618 9619 9620 9621 9622 9623 9624 9625 9626 9627 9628 9629 9630 9631 9632 9633 9634 9635 9636 9637 9638 9639 9640 9641 9642 9643 9644 9645 9646 9647 9648 9649 9650 9651 9652 9653 9654 9655 9656 9657 9658 9659 9660 9661 9662 9663 9664 9665 9666 9667 9668 9669 9670 9671 9672 9673 9674 9675 9676 9677 9678 9679 9680 9681 9682 9683 9684 9685 9686 9687 9688 9689 9690 9691 9692 9693 9694 9695 9696 9697 9698 9699 9700 9701 9702 9703 9704 9705 9706 9707 9708 9709 9710 9711 9712 9713 9714 9715 9716 9717 9718 9719 9720 9721 9722 9723 9724 9725 9726 9727 9728 9729 9730 9731 9732 9733 9734 9735 9736 9737 9738 9739 9740 9741 9742 9743 9744 9745 9746 9747 9748 9749 9750 9751 9752 9753 9754 9755 9756 9757 9758 9759 9760 9761 9762 9763 9764 9765 9766 9767 9768 9769 9770 9771 9772 9773 9774 9775 9776 9777 9778 9779 9780 9781 9782 9783 9784 9785 9786 9787 9788 9789 9790 9791 9792 9793 9794 9795 9796 9797 9798 9799 9800 9801 9802 9803 9804 9805 9806 9807 9808 9809 9810 9811 9812 9813 9814 9815 9816 9817 9818 9819 9820 9821 9822 9823 9824 9825 9826 9827 9828 9829 9830 9831 9832 9833 9834 9835 9836 9837 9838 9839 9840 9841 9842 9843 9844 9845 9846 9847 9848 9849 9850 9851 9852 9853 9854 9855 9856 9857 9858 9859 9860 9861 9862 9863 9864 9865 9866 9867 9868 9869 9870 9871 9872 9873 9874 9875 9876 9877 9878 9879 9880 9881 9882 9883 9884 9885 9886 9887 9888 9889 9890 9891 9892 9893 9894 9895 9896 9897 9898 9899 9900 9901 9902 9903 9904 9905 9906 9907 9908 9909 9910 9911 9912 9913 9914 9915 9916 9917 9918 9919 9920 9921 9922 9923 9924 9925 9926 9927 9928 9929 9930 9931 9932 9933 9934 9935 9936 9937 9938 9939 9940 9941 9942 9943 9944 9945 9946 9947 9948 9949 9950 9951 9952 9953 9954 9955 9956 9957 9958 9959 9960 9961 9962 9963 9964 9965 9966 9967 9968 9969 9970 9971 9972 9973 9974 9975 9976 9977 9978 9979 9980 9981 9982 9983 9984 9985 9986 9987 9988 9989 9990 9991 9992 9993 9994 9995 9996 9997 9998 9999 10000 10001 10002 10003 10004 10005 10006 10007 10008 10009 10010 10011 10012 10013 10014 10015 10016 10017 10018 10019 10020 10021 10022 10023 10024 10025 10026 10027 10028 10029 10030 10031 10032 10033 10034 10035 10036 10037 10038 10039 10040 10041 10042 10043 10044 10045 10046 10047 10048 10049 10050 10051 10052 10053 10054 10055 10056 10057 10058 10059 10060 10061 10062 10063 10064 10065 10066 10067 10068 10069 10070 10071 10072 10073 10074 10075 10076 10077 10078 10079 10080 10081 10082 10083 10084 10085 10086 10087 10088 10089 10090 10091 10092 10093 10094 10095 10096 10097 10098 10099 10100 10101 10102 10103 10104 10105 10106 10107 10108 10109 10110 10111 10112 10113 10114 10115 10116 10117 10118 10119 10120 10121 10122 10123 10124 10125 10126 10127 10128 10129 10130 10131 10132 10133 10134 10135 10136 10137 10138 10139 10140 10141 10142 10143 10144 10145 10146 10147 10148 10149 10150 10151 10152 10153 10154 10155 10156 10157 10158 10159 10160 10161 10162 10163 10164 10165 10166 10167 10168 10169 10170 10171 10172 10173 10174 10175 10176 10177 10178 10179 10180 10181 10182 10183 10184 10185 10186 10187 10188 10189 10190 10191 10192 10193 10194 10195 10196 10197 10198 10199 10200 10201 10202 10203 10204 10205 10206 10207 10208 10209 10210 10211 10212 10213 10214 10215 10216 10217 10218 10219 10220 10221 10222 10223 10224 10225 10226 10227 10228 10229 10230 10231 10232 10233 10234 10235 10236 10237 10238 10239 10240 10241 10242 10243 10244 10245 10246 10247 10248 10249 10250 10251 10252 10253 10254 10255 10256 10257 10258 10259 10260 10261 10262 10263 10264 10265 10266 10267 10268 10269 10270 10271 10272 10273 10274 10275 10276 10277 10278 10279 10280 10281 10282 10283 10284 10285 10286 10287 10288 10289 10290 10291 10292 10293 10294 10295 10296 10297 10298 10299 10300 10301 10302 10303 10304 10305 10306 10307 10308 10309 10310 10311 10312 10313 10314 10315 10316 10317 10318 10319 10320 10321 10322 10323 10324 10325 10326 10327 10328 10329 10330 10331 10332 10333 10334 10335 10336 10337 10338 10339 10340 10341 10342 10343 10344 10345 10346 10347 10348 10349 10350 10351 10352 10353 10354 10355 10356 10357 10358 10359 10360 10361 10362 10363 10364 10365 10366 10367 10368 10369 10370 10371 10372 10373 10374 10375 10376 10377 10378 10379 10380 10381 10382 10383 10384 10385 10386 10387 10388 10389 10390 10391 10392 10393 10394 10395 10396 10397 10398 10399 10400 10401 10402 10403 10404 10405 10406 10407 10408 10409 10410 10411 10412 10413 10414 10415 10416 10417 10418 10419 10420 10421 10422 10423 10424 10425 10426 10427 10428 10429 10430 10431 10432 10433 10434 10435 10436 10437 10438 10439 10440 10441 10442 10443 10444 10445 10446 10447 10448 10449 10450 10451 10452 10453 10454 10455 10456 10457 10458 10459 10460 10461 10462 10463 10464 10465 10466 10467 10468 10469 10470 10471 10472 10473 10474 10475 10476 10477 10478 10479 10480 10481 10482 10483 10484 10485 10486 10487 10488 10489 10490 10491 10492 10493 10494 10495 10496 10497 10498 10499 10500 10501 10502 10503 10504 10505 10506 10507 10508 10509 10510 10511 10512 10513 10514 10515 10516 10517 10518 10519 10520 10521 10522 10523 10524 10525 10526 10527 10528 10529 10530 10531 10532 10533 10534 10535 10536 10537 10538 10539 10540 10541 10542 10543 10544 10545 10546 10547 10548 10549 10550 10551 10552 10553 10554 10555 10556 10557 10558 10559 10560 10561 10562 10563 10564 10565 10566 10567 10568 10569 10570 10571 10572 10573 10574 10575 10576 10577 10578 10579 10580 10581 10582 10583 10584 10585 10586 10587 10588 10589 10590 10591 10592 10593 10594 10595 10596 10597 10598 10599 10600 10601 10602 10603 10604 10605 10606 10607 10608 10609 10610 10611 10612 10613 10614 10615 10616 10617 10618 10619 10620 10621 10622 10623 10624 10625 10626 10627 10628 10629 10630 10631 10632 10633 10634 10635 10636 10637 10638 10639 10640 10641 10642 10643 10644 10645 10646 10647 10648 10649 10650 10651 10652 10653 10654 10655 10656 10657 10658 10659 10660 10661 10662 10663 10664 10665 10666 10667 10668 10669 10670 10671 10672 10673 10674 10675 10676 10677 10678 10679 10680 10681 10682 10683 10684 10685 10686 10687 10688 10689 10690 10691 10692 10693 10694 10695 10696 10697 10698 10699 10700 10701 10702 10703 10704 10705 10706 10707 10708 10709 10710 10711 10712 10713 10714 10715 10716 10717 10718 10719 10720 10721 10722 10723 10724 10725 10726 10727 10728 10729 10730 10731 10732 10733 10734 10735 10736 10737 10738 10739 10740 10741 10742 10743 10744 10745 10746 10747 10748 10749 10750 10751 10752 10753 10754 10755 10756 10757 10758 10759 10760 10761 10762 10763 10764 10765 10766 10767 10768 10769 10770 10771 10772 10773 10774 10775 10776 10777 10778 10779 10780 10781 10782 10783 10784 10785 10786 10787 10788 10789 10790 10791 10792 10793 10794 10795 10796 10797 10798 10799 10800 10801 10802 10803 10804 10805 10806 10807 10808 10809 10810 10811 10812 10813 10814 10815 10816 10817 10818 10819 10820 10821 10822 10823 10824 10825 10826 10827 10828 10829 10830 10831 10832 10833 10834 10835 10836 10837 10838 10839 10840 10841 10842 10843 10844 10845 10846 10847 10848 10849 10850 10851 10852 10853 10854 10855 10856 10857 10858 10859 10860 10861 10862 10863 10864 10865 10866 10867 10868 10869 10870 10871 10872 10873 10874 10875 10876 10877 10878 10879 10880 10881 10882 10883 10884 10885 10886 10887 10888 10889 10890 10891 10892 10893 10894 10895 10896 10897 10898 10899 10900 10901 10902 10903 10904 10905 10906 10907 10908 10909 10910 10911 10912 10913 10914 10915 10916 10917 10918 10919 10920 10921 10922 10923 10924 10925 10926 10927 10928 10929 10930 10931 10932 10933 10934 10935 10936 10937 10938 10939 10940 10941 10942 10943 10944 10945 10946 10947 10948 10949 10950 10951 10952 10953 10954 10955 10956 10957 10958 10959 10960 10961 10962 10963 10964 10965 10966 10967 10968 10969 10970 10971 10972 10973 10974 10975 10976 10977 10978 10979 10980 10981 10982 10983 10984 10985 10986 10987 10988 10989 10990 10991 10992 10993 10994 10995 10996 10997 10998 10999 11000 11001 11002 11003 11004 11005 11006 11007 11008 11009 11010 11011 11012 11013 11014 11015 11016 11017 11018 11019 11020 11021 11022 11023 11024 11025 11026 11027 11028 11029 11030 11031 11032 11033 11034 11035 11036 11037 11038 11039 11040 11041 11042 11043 11044 11045 11046 11047 11048 11049 11050 11051 11052 11053 11054 11055 11056 11057 11058 11059 11060 11061 11062 11063 11064 11065 11066 11067 11068 11069 11070 11071 11072 11073 11074 11075 11076 11077 11078 11079 11080 11081 11082 11083 11084 11085 11086 11087 11088 11089 11090 11091 11092 11093 11094 11095 11096 11097 11098 11099 11100 11101 11102 11103 11104 11105 11106 11107 11108 11109 11110 11111 11112 11113 11114 11115 11116 11117 11118 11119 11120 11121 11122 11123 11124 11125 11126 11127 11128 11129 11130 11131 11132 11133 11134 11135 11136 11137 11138 11139 11140 11141 11142 11143 11144 11145 11146 11147 11148 11149 11150 11151 11152 11153 11154 11155 11156 11157 11158 11159 11160 11161 11162 11163 11164 11165 11166 11167 11168 11169 11170 11171 11172 11173 11174 11175 11176 11177 11178 11179 11180 11181 11182 11183 11184 11185 11186 11187 11188 11189 11190 11191 11192 11193 11194 11195 11196 11197 11198 11199 11200 11201 11202 11203 11204 11205 11206 11207 11208 11209 11210 11211 11212 11213 11214 11215 11216 11217 11218 11219 11220 11221 11222 11223 11224 11225 11226 11227 11228 11229 11230 11231 11232 11233 11234 11235 11236 11237 11238 11239 11240 11241 11242 11243 11244 11245 11246 11247 11248 11249 11250 11251 11252 11253 11254 11255 11256 11257 11258 11259 11260 11261 11262 11263 11264 11265 11266 11267 11268 11269 11270 11271 11272 11273 11274 11275 11276 11277 11278 11279 11280 11281 11282 11283 11284 11285 11286 11287 11288 11289 11290 11291 11292 11293 11294 11295 11296 11297 11298 11299 11300 11301 11302 11303 11304 11305 11306 11307 11308 11309 11310 11311 11312 11313 11314 11315 11316 11317 11318 11319 11320 11321 11322 11323 11324 11325 11326 11327 11328 11329 11330 11331 11332 11333 11334 11335 11336 11337 11338 11339 11340 11341 11342 11343 11344 11345 11346 11347 11348 11349 11350 11351 11352 11353 11354 11355 11356 11357 11358 11359 11360 11361 11362 11363 11364 11365 11366 11367 11368 11369 11370 11371 11372 11373 11374 11375 11376 11377 11378 11379 11380 11381 11382 11383 11384 11385 11386 11387 11388 11389 11390 11391 11392 11393 11394 11395 11396 11397 11398 11399 11400 11401 11402 11403 11404 11405 11406 11407 11408 11409 11410 11411 11412 11413 11414 11415 11416 11417 11418 11419 11420 11421 11422 11423 11424 11425 11426 11427 11428 11429 11430 11431 11432 11433 11434 11435 11436 11437 11438 11439 11440 11441 11442 11443 11444 11445 11446 11447 11448 11449 11450 11451 11452 11453 11454 11455 11456 11457 11458 11459 11460 11461 11462 11463 11464 11465 11466 11467 11468 11469 11470 11471 11472 11473 11474 11475 11476 11477 11478 11479 11480 11481 11482 11483 11484 11485 11486 11487 11488 11489 11490 11491 11492 11493 11494 11495 11496 11497 11498 11499 11500 11501 11502 11503 11504 11505 11506 11507 11508 11509 11510 11511 11512 11513 11514 11515 11516 11517 11518 11519 11520 11521 11522 11523 11524 11525 11526 11527 11528 11529 11530 11531 11532 11533 11534 11535 11536 11537 11538 11539 11540 11541 11542 11543 11544 11545 11546 11547 11548 11549 11550 11551 11552 11553 11554 11555 11556 11557 11558 11559 11560 11561 11562 11563 11564 11565 11566 11567 11568 11569 11570 11571 11572 11573 11574 11575 11576 11577 11578 11579 11580 11581 11582 11583 11584 11585 11586 11587 11588 11589 11590 11591 11592 11593 11594 11595 11596 11597 11598 11599 11600 11601 11602 11603 11604 11605 11606 11607 11608 11609 11610 11611 11612 11613 11614 11615 11616 11617 11618 11619 11620 11621 11622 11623 11624 11625 11626 11627 11628 11629 11630 11631 11632 11633 11634 11635 11636 11637 11638 11639 11640 11641 11642 11643 11644 11645 11646 11647 11648 11649 11650 11651 11652 11653 11654 11655 11656 11657 11658 11659 11660 11661 11662 11663 11664 11665 11666 11667 11668 11669 11670 11671 11672 11673 11674 11675 11676 11677 11678 11679 11680 11681 11682 11683 11684 11685 11686 11687 11688 11689 11690 11691 11692 11693 11694 11695 11696 11697 11698 11699 11700 11701 11702 11703 11704 11705 11706 11707 11708 11709 11710 11711 11712 11713 11714 11715 11716 11717 11718 11719 11720 11721 11722 11723 11724 11725 11726 11727 11728 11729 11730 11731 11732 11733 11734 11735 11736 11737 11738 11739 11740 11741 11742 11743 11744 11745 11746 11747 11748 11749 11750 11751 11752 11753 11754 11755 11756 11757 11758 11759 11760 11761 11762 11763 11764 11765 11766 11767 11768 11769 11770 11771 11772 11773 11774 11775 11776 11777 11778 11779 11780 11781 11782 11783 11784 11785 11786 11787 11788 11789 11790 11791 11792 11793 11794 11795 11796 11797 11798 11799 11800 11801 11802 11803 11804 11805 11806 11807 11808 11809 11810 11811 11812 11813 11814 11815 11816 11817 11818 11819 11820 11821 11822 11823 11824 11825 11826 11827 11828 11829 11830 11831 11832 11833 11834 11835 11836 11837 11838 11839 11840 11841 11842 11843 11844 11845 11846 11847 11848 11849 11850 11851 11852 11853 11854 11855 11856 11857 11858 11859 11860 11861 11862 11863 11864 11865 11866 11867 11868 11869 11870 11871 11872 11873 11874 11875 11876 11877 11878 11879 11880 11881 11882 11883 11884 11885 11886 11887 11888 11889 11890 11891 11892 11893 11894 11895 11896 11897 11898 11899 11900 11901 11902 11903 11904 11905 11906 11907 11908 11909 11910 11911 11912 11913 11914 11915 11916 11917 11918 11919 11920 11921 11922 11923 11924 11925 11926 11927 11928 11929 11930 11931 11932 11933 11934 11935 11936 11937 11938 11939 11940 11941 11942 11943 11944 11945 11946 11947 11948 11949 11950 11951 11952 11953 11954 11955 11956 11957 11958 11959 11960 11961 11962 11963 11964 11965 11966 11967 11968 11969 11970 11971 11972 11973 11974 11975 11976 11977 11978 11979 11980 11981 11982 11983 11984 11985 11986 11987 11988 11989 11990 11991 11992 11993 11994 11995 11996 11997 11998 11999 12000 12001 12002 12003 12004 12005 12006 12007 12008 12009 12010 12011 12012 12013 12014 12015 12016 12017 12018 12019 12020 12021 12022 12023 12024 12025 12026 12027 12028 12029 12030 12031 12032 12033 12034 12035 12036 12037 12038 12039 12040 12041 12042 12043 12044 12045 12046 12047 12048 12049 12050 12051 12052 12053 12054 12055 12056 12057 12058 12059 12060 12061 12062 12063 12064 12065 12066 12067 12068 12069 12070 12071 12072 12073 12074 12075 12076 12077 12078 12079 12080 12081 12082 12083 12084 12085 12086 12087 12088 12089 12090 12091 12092 12093 12094 12095 12096 12097 12098 12099 12100 12101 12102 12103 12104 12105 12106 12107 12108 12109 12110 12111 12112 12113 12114 12115 12116 12117 12118 12119 12120 12121 12122 12123 12124 12125 12126 12127 12128 12129 12130 12131 12132 12133 12134 12135 12136 12137 12138 12139 12140 12141 12142 12143 12144 12145 12146 12147 12148 12149 12150 12151 12152 12153 12154 12155 12156 12157 12158 12159 12160 12161 12162 12163 12164 12165 12166 12167 12168 12169 12170 12171 12172 12173 12174 12175 12176 12177 12178 12179 12180 12181 12182 12183 12184 12185 12186 12187 12188 12189 12190 12191 12192 12193 12194 12195 12196 12197 12198 12199 12200 12201 12202 12203 12204 12205 12206 12207 12208 12209 12210 12211 12212 12213 12214 12215 12216 12217 12218 12219 12220 12221 12222 12223 12224 12225 12226 12227 12228 12229 12230 12231 12232 12233 12234 12235 12236 12237 12238 12239 12240 12241 12242 12243 12244 12245 12246 12247 12248 12249 12250 12251 12252 12253 12254 12255 12256 12257 12258 12259 12260 12261 12262 12263 12264 12265 12266 12267 12268 12269 12270 12271 12272 12273 12274 12275 12276 12277 12278 12279 12280 12281 12282 12283 12284 12285 12286 12287 12288 12289 12290 12291 12292 12293 12294 12295 12296 12297 12298 12299 12300 12301 12302 12303 12304 12305 12306 12307 12308 12309 12310 12311 12312 12313 12314 12315 12316 12317 12318 12319 12320 12321 12322 12323 12324 12325 12326 12327 12328 12329 12330 12331 12332 12333 12334 12335 12336 12337 12338 12339 12340 12341 12342 12343 12344 12345 12346 12347 12348 12349 12350 12351 12352 12353 12354 12355 12356 12357 12358 12359 12360 12361 12362 12363 12364 12365 12366 12367 12368 12369 12370 12371 12372 12373 12374 12375 12376 12377 12378 12379 12380 12381 12382 12383 12384 12385 12386 12387 12388 12389 12390 12391 12392 12393 12394 12395 12396 12397 12398 12399 12400 12401 12402 12403 12404 12405 12406 12407 12408 12409 12410 12411 12412 12413 12414 12415 12416 12417 12418 12419 12420 12421 12422 12423 12424 12425 12426 12427 12428 12429 12430 12431 12432 12433 12434 12435 12436 12437 12438 12439 12440 12441 12442 12443 12444 12445 12446 12447 12448 12449 12450 12451 12452 12453 12454 12455 12456 12457 12458 12459 12460 12461 12462 12463 12464 12465 12466 12467 12468 12469 12470 12471 12472 12473 12474 12475 12476 12477 12478 12479 12480 12481 12482 12483 12484 12485 12486 12487 12488 12489 12490 12491 12492 12493 12494 12495 12496 12497 12498 12499 12500 12501 12502 12503 12504 12505 12506 12507 12508 12509 12510 12511 12512 12513 12514 12515 12516 12517 12518 12519 12520 12521 12522 12523 12524 12525 12526 12527 12528 12529 12530 12531 12532 12533 12534 12535 12536 12537 12538 12539 12540 12541 12542 12543 12544 12545 12546 12547 12548 12549 12550 12551 12552 12553 12554 12555 12556 12557 12558 12559 12560 12561 12562 12563 12564 12565 12566 12567 12568 12569 12570 12571 12572 12573 12574 12575 12576 12577 12578 12579 12580 12581 12582 12583 12584 12585 12586 12587 12588 12589 12590 12591 12592 12593 12594 12595 12596 12597 12598 12599 12600 12601 12602 12603 12604 12605 12606 12607 12608 12609 12610 12611 12612 12613 12614 12615 12616 12617 12618 12619 12620 12621 12622 12623 12624 12625 12626 12627 12628 12629 12630 12631 12632 12633 12634 12635 12636 12637 12638 12639 12640 12641 12642 12643 12644 12645 12646 12647 12648 12649 12650 12651 12652 12653 12654 12655 12656 12657 12658 12659 12660 12661 12662 12663 12664 12665 12666 12667 12668 12669 12670 12671 12672 12673 12674 12675 12676 12677 12678 12679 12680 12681 12682 12683 12684 12685 12686 12687 12688 12689 12690 12691 12692 12693 12694 12695 12696 12697 12698 12699 12700 12701 12702 12703 12704 12705 12706 12707 12708 12709 12710 12711 12712 12713 12714 12715 12716 12717 12718 12719 12720 12721 12722 12723 12724 12725 12726 12727 12728 12729 12730 12731 12732 12733 12734 12735 12736 12737 12738 12739 12740 12741 12742 12743 12744 12745 12746 12747 12748 12749 12750 12751 12752 12753 12754 12755 12756 12757 12758 12759 12760 12761 12762 12763 12764 12765 12766 12767 12768 12769 12770 12771 12772 12773 12774 12775 12776 12777 12778 12779 12780 12781 12782 12783 12784 12785 12786 12787 12788 12789 12790 12791 12792 12793 12794 12795 12796 12797 12798 12799 12800 12801 12802 12803 12804 12805 12806 12807 12808 12809 12810 12811 12812 12813 12814 12815 12816 12817 12818 12819 12820 12821 12822 12823 12824 12825 12826 12827 12828 12829 12830 12831 12832 12833 12834 12835 12836 12837 12838 12839 12840 12841 12842 12843 12844 12845 12846 12847 12848 12849 12850 12851 12852 12853 12854 12855 12856 12857 12858 12859 12860 12861 12862 12863 12864 12865 12866 12867 12868 12869 12870 12871 12872 12873 12874 12875 12876 12877 12878 12879 12880 12881 12882 12883 12884 12885 12886 12887 12888 12889 12890 12891 12892 12893 12894 12895 12896 12897 12898 12899 12900 12901 12902 12903 12904 12905 12906 12907 12908 12909 12910 12911 12912 12913 12914 12915 12916 12917 12918 12919 12920 12921 12922 12923 12924 12925 12926 12927 12928 12929 12930 12931 12932 12933 12934 12935 12936 12937 12938 12939 12940 12941 12942 12943 12944 12945 12946 12947 12948 12949 12950 12951 12952 12953 12954 12955 12956 12957 12958 12959 12960 12961 12962 12963 12964 12965 12966 12967 12968 12969 12970 12971 12972 12973 12974 12975 12976 12977 12978 12979 12980 12981 12982 12983 12984 12985 12986 12987 12988 12989 12990 12991 12992 12993 12994 12995 12996 12997 12998 12999 13000 13001 13002 13003 13004 13005 13006 13007 13008 13009 13010 13011 13012 13013 13014 13015 13016 13017 13018 13019 13020 13021 13022 13023 13024 13025 13026 13027 13028 13029 13030 13031 13032 13033 13034 13035 13036 13037 13038 13039 13040 13041 13042 13043 13044 13045 13046 13047 13048 13049 13050 13051 13052 13053 13054 13055 13056 13057 13058 13059 13060 13061 13062 13063 13064 13065 13066 13067 13068 13069 13070 13071 13072 13073 13074 13075 13076 13077 13078 13079 13080 13081 13082 13083 13084 13085 13086 13087 13088 13089 13090 13091 13092 13093 13094 13095 13096 13097 13098 13099 13100 13101 13102 13103 13104 13105 13106 13107 13108 13109 13110 13111 13112 13113 13114 13115 13116 13117 13118 13119 13120 13121 13122 13123 13124 13125 13126 13127 13128 13129 13130 13131 13132 13133 13134 13135 13136 13137 13138 13139 13140 13141 13142 13143 13144 13145 13146 13147 13148 13149 13150 13151 13152 13153 13154 13155 13156 13157 13158 13159 13160 13161 13162 13163 13164 13165 13166 13167 13168 13169 13170 13171 13172 13173 13174 13175 13176 13177 13178 13179 13180 13181 13182 13183 13184 13185 13186 13187 13188 13189 13190 13191 13192 13193 13194 13195 13196 13197 13198 13199 13200 13201 13202 13203 13204 13205 13206 13207 13208 13209 13210 13211 13212 13213 13214 13215 13216 13217 13218 13219 13220 13221 13222 13223 13224 13225 13226 13227 13228 13229 13230 13231 13232 13233 13234 13235 13236 13237 13238 13239 13240 13241 13242 13243 13244 13245 13246 13247 13248 13249 13250 13251 13252 13253 13254 13255 13256 13257 13258 13259 13260 13261 13262 13263 13264 13265 13266 13267 13268 13269 13270 13271 13272 13273 13274 13275 13276 13277 13278 13279 13280 13281 13282 13283 13284 13285 13286 13287 13288 13289 13290 13291 13292 13293 13294 13295 13296 13297 13298 13299 13300 13301 13302 13303 13304 13305 13306 13307 13308 13309 13310 13311 13312 13313 13314 13315 13316 13317 13318 13319 13320 13321 13322 13323 13324 13325 13326 13327 13328 13329 13330 13331 13332 13333 13334 13335 13336 13337 13338 13339 13340 13341 13342 13343 13344 13345 13346 13347 13348 13349 13350 13351 13352 13353 13354 13355 13356 13357 13358 13359 13360 13361 13362 13363 13364 13365 13366 13367 13368 13369 13370 13371 13372 13373 13374 13375 13376 13377 13378 13379 13380 13381 13382 13383 13384 13385 13386 13387 13388 13389 13390 13391 13392 13393 13394 13395 13396 13397 13398 13399 13400 13401 13402 13403 13404 13405 13406 13407 13408 13409 13410 13411 13412 13413 13414 13415 13416 13417 13418 13419 13420 13421 13422 13423 13424 13425 13426 13427 13428 13429 13430 13431 13432 13433 13434 13435 13436 13437 13438 13439 13440 13441 13442 13443 13444 13445 13446 13447 13448 13449 13450 13451 13452 13453 13454 13455 13456 13457 13458 13459 13460 13461 13462 13463 13464 13465 13466 13467 13468 13469 13470 13471 13472 13473 13474 13475 13476 13477 13478 13479 13480 13481 13482 13483 13484 13485 13486 13487 13488 13489 13490 13491 13492 13493 13494 13495 13496 13497 13498 13499 13500 13501 13502 13503 13504 13505 13506 13507 13508 13509 13510 13511 13512 13513 13514 13515 13516 13517 13518 13519 13520 13521 13522 13523 13524 13525 13526 13527 13528 13529 13530 13531 13532 13533 13534 13535 13536 13537 13538 13539 13540 13541 13542 13543 13544 13545 13546 13547 13548 13549 13550 13551 13552 13553 13554 13555 13556 13557 13558 13559 13560 13561 13562 13563 13564 13565 13566 13567 13568 13569 13570 13571 13572 13573 13574 13575 13576 13577 13578 13579 13580 13581 13582 13583 13584 13585 13586 13587 13588 13589 13590 13591 13592 13593 13594 13595 13596 13597 13598 13599 13600 13601 13602 13603 13604 13605 13606 13607 13608 13609 13610 13611 13612 13613 13614 13615 13616 13617 13618 13619 13620 13621 13622 13623 13624 13625 13626 13627 13628 13629 13630 13631 13632 13633 13634 13635 13636 13637 13638 13639 13640 13641 13642 13643 13644 13645 13646 13647 13648 13649 13650 13651 13652 13653 13654 13655 13656 13657 13658 13659 13660 13661 13662 13663 13664 13665 13666 13667 13668 13669 13670 13671 13672 13673 13674 13675 13676 13677 13678 13679 13680 13681 13682 13683 13684 13685 13686 13687 13688 13689 13690 13691 13692 13693 13694 13695 13696 13697 13698 13699 13700 13701 13702 13703 13704 13705 13706 13707 13708 13709 13710 13711 13712 13713 13714 13715 13716 13717 13718 13719 13720 13721 13722 13723 13724 13725 13726 13727 13728 13729 13730 13731 13732 13733 13734 13735 13736 13737 13738 13739 13740 13741 13742 13743 13744 13745 13746 13747 13748 13749 13750 13751 13752 13753 13754 13755 13756 13757 13758 13759 13760 13761 13762 13763 13764 13765 13766 13767 13768 13769 13770 13771 13772 13773 13774 13775 13776 13777 13778 13779 13780 13781 13782 13783 13784 13785 13786 13787 13788 13789 13790 13791 13792 13793 13794 13795 13796 13797 13798 13799 13800 13801 13802 13803 13804 13805 13806 13807 13808 13809 13810 13811 13812 13813 13814 13815 13816 13817 13818 13819 13820 13821 13822 13823 13824 13825 13826 13827 13828 13829 13830 13831 13832 13833 13834 13835 13836 13837 13838 13839 13840 13841 13842 13843 13844 13845 13846 13847 13848 13849 13850 13851 13852 13853 13854 13855 13856 13857 13858 13859 13860 13861 13862 13863 13864 13865 13866 13867 13868 13869 13870 13871 13872 13873 13874 13875 13876 13877 13878 13879 13880 13881 13882 13883 13884 13885 13886 13887 13888 13889 13890 13891 13892 13893 13894 13895 13896 13897 13898 13899 13900 13901 13902 13903 13904 13905 13906 13907 13908 13909 13910 13911 13912 13913 13914 13915 13916 13917 13918 13919 13920 13921 13922 13923 13924 13925 13926 13927 13928 13929 13930 13931 13932 13933 13934 13935 13936 13937 13938 13939 13940 13941 13942 13943 13944 13945 13946 13947 13948 13949 13950 13951 13952 13953 13954 13955 13956 13957 13958 13959 13960 13961 13962 13963 13964 13965 13966 13967 13968 13969 13970 13971 13972 13973 13974 13975 13976 13977 13978 13979 13980 13981 13982 13983 13984 13985 13986 13987 13988 13989 13990 13991 13992 13993 13994 13995 13996 13997 13998 13999 14000 14001 14002 14003 14004 14005 14006 14007 14008 14009 14010 14011 14012 14013 14014 14015 14016 14017 14018 14019 14020 14021 14022 14023 14024 14025 14026 14027 14028 14029 14030 14031 14032 14033 14034 14035 14036 14037 14038 14039 14040 14041 14042 14043 14044 14045 14046 14047 14048 14049 14050 14051 14052 14053 14054 14055 14056 14057 14058 14059 14060 14061 14062 14063 14064 14065 14066 14067 14068 14069 14070 14071 14072 14073 14074 14075 14076 14077 14078 14079 14080 14081 14082 14083 14084 14085 14086 14087 14088 14089 14090 14091 14092 14093 14094 14095 14096 14097 14098 14099 14100 14101 14102 14103 14104 14105 14106 14107 14108 14109 14110 14111 14112 14113 14114 14115 14116 14117 14118 14119 14120 14121 14122 14123 14124 14125 14126 14127 14128 14129 14130 14131 14132 14133 14134 14135 14136 14137 14138 14139 14140 14141 14142 14143 14144 14145 14146 14147 14148 14149 14150 14151 14152 14153 14154 14155 14156 14157 14158 14159 14160 14161 14162 14163 14164 14165 14166 14167 14168 14169 14170 14171 14172 14173 14174 14175 14176 14177 14178 14179 14180 14181 14182 14183 14184 14185 14186 14187 14188 14189 14190 14191 14192 14193 14194 14195 14196 14197 14198 14199 14200 14201 14202 14203 14204 14205 14206 14207 14208 14209 14210 14211 14212 14213 14214 14215 14216 14217 14218 14219 14220 14221 14222 14223 14224 14225 14226 14227 14228 14229 14230 14231 14232 14233 14234 14235 14236 14237 14238 14239 14240 14241 14242 14243 14244 14245 14246 14247 14248 14249 14250 14251 14252 14253 14254 14255 14256 14257 14258 14259 14260 14261 14262 14263 14264 14265 14266 14267 14268 14269 14270 14271 14272 14273 14274 14275 14276 14277 14278 14279 14280 14281 14282 14283 14284 14285 14286 14287 14288 14289 14290 14291 14292 14293 14294 14295 14296 14297 14298 14299 14300 14301 14302 14303 14304 14305 14306 14307 14308 14309 14310 14311 14312 14313 14314 14315 14316 14317 14318 14319 14320 14321 14322 14323 14324 14325 14326 14327 14328 14329 14330 14331 14332 14333 14334 14335 14336 14337 14338 14339 14340 14341 14342 14343 14344 14345 14346 14347 14348 14349 14350 14351 14352 14353 14354 14355 14356 14357 14358 14359 14360 14361 14362 14363 14364 14365 14366 14367 14368 14369 14370 14371 14372 14373 14374 14375 14376 14377 14378 14379 14380 14381 14382 14383 14384 14385 14386 14387 14388 14389 14390 14391 14392 14393 14394 14395 14396 14397 14398 14399 14400 14401 14402 14403 14404 14405 14406 14407 14408 14409 14410 14411 14412 14413 14414 14415 14416 14417 14418 14419 14420 14421 14422 14423 14424 14425 14426 14427 14428 14429 14430 14431 14432 14433 14434 14435 14436 14437 14438 14439 14440 14441 14442 14443 14444 14445 14446 14447 14448 14449 14450 14451 14452 14453 14454 14455 14456 14457 14458 14459 14460 14461 14462 14463 14464 14465 14466 14467 14468 14469 14470 14471 14472 14473 14474 14475 14476 14477 14478 14479 14480 14481 14482 14483 14484 14485 14486 14487 14488 14489 14490 14491 14492 14493 14494 14495 14496 14497 14498 14499 14500 14501 14502 14503 14504 14505 14506 14507 14508 14509 14510 14511 14512 14513 14514 14515 14516 14517 14518 14519 14520 14521 14522 14523 14524 14525 14526 14527 14528 14529 14530 14531 14532 14533 14534 14535 14536 14537 14538 14539 14540 14541 14542 14543 14544 14545 14546 14547 14548 14549 14550 14551 14552 14553 14554 14555 14556 14557 14558 14559 14560 14561 14562 14563 14564 14565 14566 14567 14568 14569 14570 14571 14572 14573 14574 14575 14576 14577 14578 14579 14580 14581 14582 14583 14584 14585 14586 14587 14588 14589 14590 14591 14592 14593 14594 14595 14596 14597 14598 14599 14600 14601 14602 14603 14604 14605 14606 14607 14608 14609 14610 14611 14612 14613 14614 14615 14616 14617 14618 14619 14620 14621 14622 14623 14624 14625 14626 14627 14628 14629 14630 14631 14632 14633 14634 14635 14636 14637 14638 14639 14640 14641 14642 14643 14644 14645 14646 14647 14648 14649 14650 14651 14652 14653 14654 14655 14656 14657 14658 14659 14660 14661 14662 14663 14664 14665 14666 14667 14668 14669 14670 14671 14672 14673 14674 14675 14676 14677 14678 14679 14680 14681 14682 14683 14684 14685 14686 14687 14688 14689 14690 14691 14692 14693 14694 14695 14696 14697 14698 14699 14700 14701 14702 14703 14704 14705 14706 14707 14708 14709 14710 14711 14712 14713 14714 14715 14716 14717 14718 14719 14720 14721 14722 14723 14724 14725 14726 14727 14728 14729 14730 14731 14732 14733 14734 14735 14736 14737 14738 14739 14740 14741 14742 14743 14744 14745 14746 14747 14748 14749 14750 14751 14752 14753 14754 14755 14756 14757 14758 14759 14760 14761 14762 14763 14764 14765 14766 14767 14768 14769 14770 14771 14772 14773 14774 14775 14776 14777 14778 14779 14780 14781 14782 14783 14784 14785 14786 14787 14788 14789 14790 14791 14792 14793 14794 14795 14796 14797 14798 14799 14800 14801 14802 14803 14804 14805 14806 14807 14808 14809 14810 14811 14812 14813 14814 14815 14816 14817 14818 14819 14820 14821 14822 14823 14824 14825 14826 14827 14828 14829 14830 14831 14832 14833 14834 14835 14836 14837 14838 14839 14840 14841 14842 14843 14844 14845 14846 14847 14848 14849 14850 14851 14852 14853 14854 14855 14856 14857 14858 14859 14860 14861 14862 14863 14864 14865 14866 14867 14868 14869 14870 14871 14872 14873 14874 14875 14876 14877 14878 14879 14880 14881 14882 14883 14884 14885 14886 14887 14888 14889 14890 14891 14892 14893 14894 14895 14896 14897 14898 14899 14900 14901 14902 14903 14904 14905 14906 14907 14908 14909 14910 14911 14912 14913 14914 14915 14916 14917 14918 14919 14920 14921 14922 14923 14924 14925 14926 14927 14928 14929 14930 14931 14932 14933 14934 14935 14936 14937 14938 14939 14940 14941 14942 14943 14944 14945 14946 14947 14948 14949 14950 14951 14952 14953 14954 14955 14956 14957 14958 14959 14960 14961 14962 14963 14964 14965 14966 14967 14968 14969 14970 14971 14972 14973 14974 14975 14976 14977 14978 14979 14980 14981 14982 14983 14984 14985 14986 14987 14988 14989 14990 14991 14992 14993 14994 14995 14996 14997 14998 14999 15000 15001 15002 15003 15004 15005 15006 15007 15008 15009 15010 15011 15012 15013 15014 15015 15016 15017 15018 15019 15020 15021 15022 15023 15024 15025 15026 15027 15028 15029 15030 15031 15032 15033 15034 15035 15036 15037 15038 15039 15040 15041 15042 15043 15044 15045 15046 15047 15048 15049 15050 15051 15052 15053 15054 15055 15056 15057 15058 15059 15060 15061 15062 15063 15064 15065 15066 15067 15068 15069 15070 15071 15072 15073 15074 15075 15076 15077 15078 15079 15080 15081 15082 15083 15084 15085 15086 15087 15088 15089 15090 15091 15092 15093 15094 15095 15096 15097 15098 15099 15100 15101 15102 15103 15104 15105 15106 15107 15108 15109 15110 15111 15112 15113 15114 15115 15116 15117 15118 15119 15120 15121 15122 15123 15124 15125 15126 15127 15128 15129 15130 15131 15132 15133 15134 15135 15136 15137 15138 15139 15140 15141 15142 15143 15144 15145 15146 15147 15148 15149 15150 15151 15152 15153 15154 15155 15156 15157 15158 15159 15160 15161 15162 15163 15164 15165 15166 15167 15168 15169 15170 15171 15172 15173 15174 15175 15176 15177 15178 15179 15180 15181 15182 15183 15184 15185 15186 15187 15188 15189 15190 15191 15192 15193 15194 15195 15196 15197 15198 15199 15200 15201 15202 15203 15204 15205 15206 15207 15208 15209 15210 15211 15212 15213 15214 15215 15216 15217 15218 15219 15220 15221 15222 15223 15224 15225 15226 15227 15228 15229 15230 15231 15232 15233 15234 15235 15236 15237 15238 15239 15240 15241 15242 15243 15244 15245 15246 15247 15248 15249 15250 15251 15252 15253 15254 15255 15256 15257 15258 15259 15260 15261 15262 15263 15264 15265 15266 15267 15268 15269 15270 15271 15272 15273 15274 15275 15276 15277 15278 15279 15280 15281 15282 15283 15284 15285 15286 15287 15288 15289 15290 15291 15292 15293 15294 15295 15296 15297 15298 15299 15300 15301 15302 15303 15304 15305 15306 15307 15308 15309 15310 15311 15312 15313 15314 15315 15316 15317 15318 15319 15320 15321 15322 15323 15324 15325 15326 15327 15328 15329 15330 15331 15332 15333 15334 15335 15336 15337 15338 15339 15340 15341 15342 15343 15344 15345 15346 15347 15348 15349 15350 15351 15352 15353 15354 15355 15356 15357 15358 15359 15360 15361 15362 15363 15364 15365 15366 15367 15368 15369 15370 15371 15372 15373 15374 15375 15376 15377 15378 15379 15380 15381 15382 15383 15384 15385 15386 15387 15388 15389 15390 15391 15392 15393 15394 15395 15396 15397 15398 15399 15400 15401 15402 15403 15404 15405 15406 15407 15408 15409 15410 15411 15412 15413 15414 15415 15416 15417 15418 15419 15420 15421 15422 15423 15424 15425 15426 15427 15428 15429 15430 15431 15432 15433 15434 15435 15436 15437 15438 15439 15440 15441 15442 15443 15444 15445 15446 15447 15448 15449 15450 15451 15452 15453 15454 15455 15456 15457 15458 15459 15460 15461 15462 15463 15464 15465 15466 15467 15468 15469 15470 15471 15472 15473 15474 15475 15476 15477 15478 15479 15480 15481 15482 15483 15484 15485 15486 15487 15488 15489 15490 15491 15492 15493 15494 15495 15496 15497 15498 15499 15500 15501 15502 15503 15504 15505 15506 15507 15508 15509 15510 15511 15512 15513 15514 15515 15516 15517 15518 15519 15520 15521 15522 15523 15524 15525 15526 15527 15528 15529 15530 15531 15532 15533 15534 15535 15536 15537 15538 15539 15540 15541 15542 15543 15544 15545 15546 15547 15548 15549 15550 15551 15552 15553 15554 15555 15556 15557 15558 15559 15560 15561 15562 15563 15564 15565 15566 15567 15568 15569 15570 15571 15572 15573 15574 15575 15576 15577 15578 15579 15580 15581 15582 15583 15584 15585 15586 15587 15588 15589 15590 15591 15592 15593 15594 15595 15596 15597 15598 15599 15600 15601 15602 15603 15604 15605 15606 15607 15608 15609 15610 15611 15612 15613 15614 15615 15616 15617 15618 15619 15620 15621 15622 15623 15624 15625 15626 15627 15628 15629 15630 15631 15632 15633 15634 15635 15636 15637 15638 15639 15640 15641 15642 15643 15644 15645 15646 15647 15648 15649 15650 15651 15652 15653 15654 15655 15656 15657 15658 15659 15660 15661 15662 15663 15664 15665 15666 15667 15668 15669 15670 15671 15672 15673 15674 15675 15676 15677 15678 15679 15680 15681 15682 15683 15684 15685 15686 15687 15688 15689 15690 15691 15692 15693 15694 15695 15696 15697 15698 15699 15700 15701 15702 15703 15704 15705 15706 15707 15708 15709 15710 15711 15712 15713 15714 15715 15716 15717 15718 15719 15720 15721 15722 15723 15724 15725 15726 15727 15728 15729 15730 15731 15732 15733 15734 15735 15736 15737 15738 15739 15740 15741 15742 15743 15744 15745 15746 15747 15748 15749 15750 15751 15752 15753 15754 15755 15756 15757 15758 15759 15760 15761 15762 15763 15764 15765 15766 15767 15768 15769 15770 15771 15772 15773 15774 15775 15776 15777 15778 15779 15780 15781 15782 15783 15784 15785 15786 15787 15788 15789 15790 15791 15792 15793 15794 15795 15796 15797 15798 15799 15800 15801 15802 15803 15804 15805 15806 15807 15808 15809 15810 15811 15812 15813 15814 15815 15816 15817 15818 15819 15820 15821 15822 15823 15824 15825 15826 15827 15828 15829 15830 15831 15832 15833 15834 15835 15836 15837 15838 15839 15840 15841 15842 15843 15844 15845 15846 15847 15848 15849 15850 15851 15852 15853 15854 15855 15856 15857 15858 15859 15860 15861 15862 15863 15864 15865 15866 15867 15868 15869 15870 15871 15872 15873 15874 15875 15876 15877 15878 15879 15880 15881 15882 15883 15884 15885 15886 15887 15888 15889 15890 15891 15892 15893 15894 15895 15896 15897 15898 15899 15900 15901 15902 15903 15904 15905 15906 15907 15908 15909 15910 15911 15912 15913 15914 15915 15916 15917 15918 15919 15920 15921 15922 15923 15924 15925 15926 15927 15928 15929 15930 15931 15932 15933 15934 15935 15936 15937 15938 15939 15940 15941 15942 15943 15944 15945 15946 15947 15948 15949 15950 15951 15952 15953 15954 15955 15956 15957 15958 15959 15960 15961 15962 15963 15964 15965 15966 15967 15968 15969 15970 15971 15972 15973 15974 15975 15976 15977 15978 15979 15980 15981 15982 15983 15984 15985 15986 15987 15988 15989 15990 15991 15992 15993 15994 15995 15996 15997 15998 15999 16000 16001 16002 16003 16004 16005 16006 16007 16008 16009 16010 16011 16012 16013 16014 16015 16016 16017 16018 16019 16020 16021 16022 16023 16024 16025 16026 16027 16028 16029 16030 16031 16032 16033 16034 16035 16036 16037 16038 16039 16040 16041 16042 16043 16044 16045 16046 16047 16048 16049 16050 16051 16052 16053 16054 16055 16056 16057 16058 16059 16060 16061 16062 16063 16064 16065 16066 16067 16068 16069 16070 16071 16072 16073 16074 16075 16076 16077 16078 16079 16080 16081 16082 16083 16084 16085 16086 16087 16088 16089 16090 16091 16092 16093 16094 16095 16096 16097 16098 16099 16100 16101 16102 16103 16104 16105 16106 16107 16108 16109 16110 16111 16112 16113 16114 16115 16116 16117 16118 16119 16120 16121 16122 16123 16124 16125 16126 16127 16128 16129 16130 16131 16132 16133 16134 16135 16136 16137 16138 16139 16140 16141 16142 16143 16144 16145 16146 16147 16148 16149 16150 16151 16152 16153 16154 16155 16156 16157 16158 16159 16160 16161 16162 16163 16164 16165 16166 16167 16168 16169 16170 16171 16172 16173 16174 16175 16176 16177 16178 16179 16180 16181 16182 16183 16184 16185 16186 16187 16188 16189 16190 16191 16192 16193 16194 16195 16196 16197 16198 16199 16200 16201 16202 16203 16204 16205 16206 16207 16208 16209 16210 16211 16212 16213 16214 16215 16216 16217 16218 16219 16220 16221 16222 16223 16224 16225 16226 16227 16228 16229 16230 16231 16232 16233 16234 16235 16236 16237 16238 16239 16240 16241 16242 16243 16244 16245 16246 16247 16248 16249 16250 16251 16252 16253 16254 16255 16256 16257 16258 16259 16260 16261 16262 16263 16264 16265 16266 16267 16268 16269 16270 16271 16272 16273 16274 16275 16276 16277 16278 16279 16280 16281 16282 16283 16284 16285 16286 16287 16288 16289 16290 16291 16292 16293 16294 16295 16296 16297 16298 16299 16300 16301 16302 16303 16304 16305 16306 16307 16308 16309 16310 16311 16312 16313 16314 16315 16316 16317 16318 16319 16320 16321 16322 16323 16324 16325 16326 16327 16328 16329 16330 16331 16332 16333 16334 16335 16336 16337 16338 16339 16340 16341 16342 16343 16344 16345 16346 16347 16348 16349 16350 16351 16352 16353 16354 16355 16356 16357 16358 16359 16360 16361 16362 16363 16364 16365 16366 16367 16368 16369 16370 16371 16372 16373 16374 16375 16376 16377 16378 16379 16380 16381 16382 16383 16384 16385 16386 16387 16388 16389 16390 16391 16392 16393 16394 16395 16396 16397 16398 16399 16400 16401 16402 16403 16404 16405 16406 16407 16408 16409 16410 16411 16412 16413 16414 16415 16416 16417 16418 16419 16420 16421 16422 16423 16424 16425 16426 16427 16428 16429 16430 16431 16432 16433 16434 16435 16436 16437 16438 16439 16440 16441 16442 16443 16444 16445 16446 16447 16448 16449 16450 16451 16452 16453 16454 16455 16456 16457 16458 16459 16460 16461 16462 16463 16464 16465 16466 16467 16468 16469 16470 16471 16472 16473 16474 16475 16476 16477 16478 16479 16480 16481 16482 16483 16484 16485 16486 16487 16488 16489 16490 16491 16492 16493 16494 16495 16496 16497 16498 16499 16500 16501 16502 16503 16504 16505 16506 16507 16508 16509 16510 16511 16512 16513 16514 16515 16516 16517 16518 16519 16520 16521 16522 16523 16524 16525 16526 16527 16528 16529 16530 16531 16532 16533 16534 16535 16536 16537 16538 16539 16540 16541 16542 16543 16544 16545 16546 16547 16548 16549 16550 16551 16552 16553 16554 16555 16556 16557 16558 16559 16560 16561 16562 16563 16564 16565 16566 16567 16568 16569 16570 16571 16572 16573 16574 16575 16576 16577 16578 16579 16580 16581 16582 16583 16584 16585 16586 16587 16588 16589 16590 16591 16592 16593 16594 16595 16596 16597 16598 16599 16600 16601 16602 16603 16604 16605 16606 16607 16608 16609 16610 16611 16612 16613 16614 16615 16616 16617 16618 16619 16620 16621 16622 16623 16624 16625 16626 16627 16628 16629 16630 16631 16632 16633 16634 16635 16636 16637 16638 16639 16640 16641 16642 16643 16644 16645 16646 16647 16648 16649 16650 16651 16652 16653 16654 16655 16656 16657 16658 16659 16660 16661 16662 16663 16664 16665 16666 16667 16668 16669 16670 16671 16672 16673 16674 16675 16676 16677 16678 16679 16680 16681 16682 16683 16684 16685 16686 16687 16688 16689 16690 16691 16692 16693 16694 16695 16696 16697 16698 16699 16700 16701 16702 16703 16704 16705 16706 16707 16708 16709 16710 16711 16712 16713 16714 16715 16716 16717 16718 16719 16720 16721 16722 16723 16724 16725 16726 16727 16728 16729 16730 16731 16732 16733 16734 16735 16736 16737 16738 16739 16740 16741 16742 16743 16744 16745 16746 16747 16748 16749 16750 16751 16752 16753 16754 16755 16756 16757 16758 16759 16760 16761 16762 16763 16764 16765 16766 16767 16768 16769 16770 16771 16772 16773 16774 16775 16776 16777 16778 16779 16780 16781 16782 16783 16784 16785 16786 16787 16788 16789 16790 16791 16792 16793 16794 16795 16796 16797 16798 16799 16800 16801 16802 16803 16804 16805 16806 16807 16808 16809 16810 16811 16812 16813 16814 16815 16816 16817 16818 16819 16820 16821 16822 16823 16824 16825 16826 16827 16828 16829 16830 16831 16832 16833 16834 16835 16836 16837 16838 16839 16840 16841 16842 16843 16844 16845 16846 16847 16848 16849 16850 16851 16852 16853 16854 16855 16856 16857 16858 16859 16860 16861 16862 16863 16864 16865 16866 16867 16868 16869 16870 16871 16872 16873 16874 16875 16876 16877 16878 16879 16880 16881 16882 16883 16884 16885 16886 16887 16888 16889 16890 16891 16892 16893 16894 16895 16896 16897 16898 16899 16900 16901 16902 16903 16904 16905 16906 16907 16908 16909 16910 16911 16912 16913 16914 16915 16916 16917 16918 16919 16920 16921 16922 16923 16924 16925 16926 16927 16928 16929 16930 16931 16932 16933 16934 16935 16936 16937 16938 16939 16940 16941 16942 16943 16944 16945 16946 16947 16948 16949 16950 16951 16952 16953 16954 16955 16956 16957 16958 16959 16960 16961 16962 16963 16964 16965 16966 16967 16968 16969 16970 16971 16972 16973 16974 16975 16976 16977 16978 16979 16980 16981 16982 16983 16984 16985 16986 16987 16988 16989 16990 16991 16992 16993 16994 16995 16996 16997 16998 16999 17000 17001 17002 17003 17004 17005 17006 17007 17008 17009 17010 17011 17012 17013 17014 17015 17016 17017 17018 17019 17020 17021 17022 17023 17024 17025 17026 17027 17028 17029 17030 17031 17032 17033 17034 17035 17036 17037 17038 17039 17040 17041 17042 17043 17044 17045 17046 17047 17048 17049 17050 17051 17052 17053 17054 17055 17056 17057 17058 17059 17060 17061 17062 17063 17064 17065 17066 17067 17068 17069 17070 17071 17072 17073 17074 17075 17076 17077 17078 17079 17080 17081 17082 17083 17084 17085 17086 17087 17088 17089 17090 17091 17092 17093 17094 17095 17096 17097 17098 17099 17100 17101 17102 17103 17104 17105 17106 17107 17108 17109 17110 17111 17112 17113 17114 17115 17116 17117 17118 17119 17120 17121 17122 17123 17124 17125 17126 17127 17128 17129 17130 17131 17132 17133 17134 17135 17136 17137 17138 17139 17140 17141 17142 17143 17144 17145 17146 17147 17148 17149 17150 17151 17152 17153 17154 17155 17156 17157 17158 17159 17160 17161 17162 17163 17164 17165 17166 17167 17168 17169 17170 17171 17172 17173 17174 17175 17176 17177 17178 17179 17180 17181 17182 17183 17184 17185 17186 17187 17188 17189 17190 17191 17192 17193 17194 17195 17196 17197 17198 17199 17200 17201 17202 17203 17204 17205 17206 17207 17208 17209 17210 17211 17212 17213 17214 17215 17216 17217 17218 17219 17220 17221 17222 17223 17224 17225 17226 17227 17228 17229 17230 17231 17232 17233 17234 17235 17236 17237 17238 17239 17240 17241 17242 17243 17244 17245 17246 17247 17248 17249 17250 17251 17252 17253 17254 17255 17256 17257 17258 17259 17260 17261 17262 17263 17264 17265 17266 17267 17268 17269 17270 17271 17272 17273 17274 17275 17276 17277 17278 17279 17280 17281 17282 17283 17284 17285 17286 17287 17288 17289 17290 17291 17292 17293 17294 17295 17296 17297 17298 17299 17300 17301 17302 17303 17304 17305 17306 17307 17308 17309 17310 17311 17312 17313 17314 17315 17316 17317 17318 17319 17320 17321 17322 17323 17324 17325 17326 17327 17328 17329 17330 17331 17332 17333 17334 17335 17336 17337 17338 17339 17340 17341 17342 17343 17344 17345 17346 17347 17348 17349 17350 17351 17352 17353 17354 17355 17356 17357 17358 17359 17360 17361 17362 17363 17364 17365 17366 17367 17368 17369 17370 17371 17372 17373 17374 17375 17376 17377 17378 17379 17380 17381 17382 17383 17384 17385 17386 17387 17388 17389 17390 17391 17392 17393 17394 17395 17396 17397 17398 17399 17400 17401 17402 17403 17404 17405 17406 17407 17408 17409 17410 17411 17412 17413 17414 17415 17416 17417 17418 17419 17420 17421 17422 17423 17424 17425 17426 17427 17428 17429 17430 17431 17432 17433 17434 17435 17436 17437 17438 17439 17440 17441 17442 17443 17444 17445 17446 17447 17448 17449 17450 17451 17452 17453 17454 17455 17456 17457 17458 17459 17460 17461 17462 17463 17464 17465 17466 17467 17468 17469 17470 17471 17472 17473 17474 17475 17476 17477 17478 17479 17480 17481 17482 17483 17484 17485 17486 17487 17488 17489 17490 17491 17492 17493 17494 17495 17496 17497 17498 17499 17500 17501 17502 17503 17504 17505 17506 17507 17508 17509 17510 17511 17512 17513 17514 17515 17516 17517 17518 17519 17520 17521 17522 17523 17524 17525 17526 17527 17528 17529 17530 17531 17532 17533 17534 17535 17536 17537 17538 17539 17540 17541 17542 17543 17544 17545 17546 17547 17548 17549 17550 17551 17552 17553 17554 17555 17556 17557 17558 17559 17560 17561 17562 17563 17564 17565 17566 17567 17568 17569 17570 17571 17572 17573 17574 17575 17576 17577 17578 17579 17580 17581 17582 17583 17584 17585 17586 17587 17588 17589 17590 17591 17592 17593 17594 17595 17596 17597 17598 17599 17600 17601 17602 17603 17604 17605 17606 17607 17608 17609 17610 17611 17612 17613 17614 17615 17616 17617 17618 17619 17620 17621 17622 17623 17624 17625 17626 17627 17628 17629 17630 17631 17632 17633 17634 17635 17636 17637 17638 17639 17640 17641 17642 17643 17644 17645 17646 17647 17648 17649 17650 17651 17652 17653 17654 17655 17656 17657 17658 17659 17660 17661 17662 17663 17664 17665 17666 17667 17668 17669 17670 17671 17672 17673 17674 17675 17676 17677 17678 17679 17680 17681 17682 17683 17684 17685 17686 17687 17688 17689 17690 17691 17692 17693 17694 17695 17696 17697 17698 17699 17700 17701 17702 17703 17704 17705 17706 17707 17708 17709 17710 17711 17712 17713 17714 17715 17716 17717 17718 17719 17720 17721 17722 17723 17724 17725 17726 17727 17728 17729 17730 17731 17732 17733 17734 17735 17736 17737 17738 17739 17740 17741 17742 17743 17744 17745 17746 17747 17748 17749 17750 17751 17752 17753 17754 17755 17756 17757 17758 17759 17760 17761 17762 17763 17764 17765 17766 17767 17768 17769 17770 17771 17772 17773 17774 17775 17776 17777 17778 17779 17780 17781 17782 17783 17784 17785 17786 17787 17788 17789 17790 17791 17792 17793 17794 17795 17796 17797 17798 17799 17800 17801 17802 17803 17804 17805 17806 17807 17808 17809 17810 17811 17812 17813 17814 17815 17816 17817 17818 17819 17820 17821 17822 17823 17824 17825 17826 17827 17828 17829 17830 17831 17832 17833 17834 17835 17836 17837 17838 17839 17840 17841 17842 17843 17844 17845 17846 17847 17848 17849 17850 17851 17852 17853 17854 17855 17856 17857 17858 17859 17860 17861 17862 17863 17864 17865 17866 17867 17868 17869 17870 17871 17872 17873 17874 17875 17876 17877 17878 17879 17880 17881 17882 17883 17884 17885 17886 17887 17888 17889 17890 17891 17892 17893 17894 17895 17896 17897 17898 17899 17900 17901 17902 17903 17904 17905 17906 17907 17908 17909 17910 17911 17912 17913 17914 17915 17916 17917 17918 17919 17920 17921 17922 17923 17924 17925 17926 17927 17928 17929 17930 17931 17932 17933 17934 17935 17936 17937 17938 17939 17940 17941 17942 17943 17944 17945 17946 17947 17948 17949 17950 17951 17952 17953 17954 17955 17956 17957 17958 17959 17960 17961 17962 17963 17964 17965 17966 17967 17968 17969 17970 17971 17972 17973 17974 17975 17976 17977 17978 17979 17980 17981 17982 17983 17984 17985 17986 17987 17988 17989 17990 17991 17992 17993 17994 17995 17996 17997 17998 17999 18000 18001 18002 18003 18004 18005 18006 18007 18008 18009 18010 18011 18012 18013 18014 18015 18016 18017 18018 18019 18020 18021 18022 18023 18024 18025 18026 18027 18028 18029 18030 18031 18032 18033 18034 18035 18036 18037 18038 18039 18040 18041 18042 18043 18044 18045 18046 18047 18048 18049 18050 18051 18052 18053 18054 18055 18056 18057 18058 18059 18060 18061 18062 18063 18064 18065 18066 18067 18068 18069 18070 18071 18072 18073 18074 18075 18076 18077 18078 18079 18080 18081 18082 18083 18084 18085 18086 18087 18088 18089 18090 18091 18092 18093 18094 18095 18096 18097 18098 18099 18100 18101 18102 18103 18104 18105 18106 18107 18108 18109 18110 18111 18112 18113 18114 18115 18116 18117 18118 18119 18120 18121 18122 18123 18124 18125 18126 18127 18128 18129 18130 18131 18132 18133 18134 18135 18136 18137 18138 18139 18140 18141 18142 18143 18144 18145 18146 18147 18148 18149 18150 18151 18152 18153 18154 18155 18156 18157 18158 18159 18160 18161 18162 18163 18164 18165 18166 18167 18168 18169 18170 18171 18172 18173 18174 18175 18176 18177 18178 18179 18180 18181 18182 18183 18184 18185 18186 18187 18188 18189 18190 18191 18192 18193 18194 18195 18196 18197 18198 18199 18200 18201 18202 18203 18204 18205 18206 18207 18208 18209 18210 18211 18212 18213 18214 18215 18216 18217 18218 18219 18220 18221 18222 18223 18224 18225 18226 18227 18228 18229 18230 18231 18232 18233 18234 18235 18236 18237 18238 18239 18240 18241 18242 18243 18244 18245 18246 18247 18248 18249 18250 18251 18252 18253 18254 18255 18256 18257 18258 18259 18260 18261 18262 18263 18264 18265 18266 18267 18268 18269 18270 18271 18272 18273 18274 18275 18276 18277 18278 18279 18280 18281 18282 18283 18284 18285 18286 18287 18288 18289 18290 18291 18292 18293 18294 18295 18296 18297 18298 18299 18300 18301 18302 18303 18304 18305 18306 18307 18308 18309 18310 18311 18312 18313 18314 18315 18316 18317 18318 18319 18320 18321 18322 18323 18324 18325 18326 18327 18328 18329 18330 18331 18332 18333 18334 18335 18336 18337 18338 18339 18340 18341 18342 18343 18344 18345 18346 18347 18348 18349 18350 18351 18352 18353 18354 18355 18356 18357 18358 18359 18360 18361 18362 18363 18364 18365 18366 18367 18368 18369 18370 18371 18372 18373 18374 18375 18376 18377 18378 18379 18380 18381 18382 18383 18384 18385 18386 18387 18388 18389 18390 18391 18392 18393 18394 18395 18396 18397 18398 18399 18400 18401 18402 18403 18404 18405 18406 18407 18408 18409 18410 18411 18412 18413 18414 18415 18416 18417 18418 18419 18420 18421 18422 18423 18424 18425 18426 18427 18428 18429 18430 18431 18432 18433 18434 18435 18436 18437 18438 18439 18440 18441 18442 18443 18444 18445 18446 18447 18448 18449 18450 18451 18452 18453 18454 18455 18456 18457 18458 18459 18460 18461 18462 18463 18464 18465 18466 18467 18468 18469 18470 18471 18472 18473 18474 18475 18476 18477 18478 18479 18480 18481 18482 18483 18484 18485 18486 18487 18488 18489 18490 18491 18492 18493 18494 18495 18496 18497 18498 18499 18500 18501 18502 18503 18504 18505 18506 18507 18508 18509 18510 18511 18512 18513 18514 18515 18516 18517 18518 18519 18520 18521 18522 18523 18524 18525 18526 18527 18528 18529 18530 18531 18532 18533 18534 18535 18536 18537 18538 18539 18540 18541 18542 18543 18544 18545 18546 18547 18548 18549 18550 18551 18552 18553 18554 18555 18556 18557 18558 18559 18560 18561 18562 18563 18564 18565 18566 18567 18568 18569 18570 18571 18572 18573 18574 18575 18576 18577 18578 18579 18580 18581 18582 18583 18584 18585 18586 18587 18588 18589 18590 18591 18592 18593 18594 18595 18596 18597 18598 18599 18600 18601 18602 18603 18604 18605 18606 18607 18608 18609 18610 18611 18612 18613 18614 18615 18616 18617 18618 18619 18620 18621 18622 18623 18624 18625 18626 18627 18628 18629 18630 18631 18632 18633 18634 18635 18636 18637 18638 18639 18640 18641 18642 18643 18644 18645 18646 18647 18648 18649 18650 18651 18652 18653 18654 18655 18656 18657 18658 18659 18660 18661 18662 18663 18664 18665 18666 18667 18668 18669 18670 18671 18672 18673 18674 18675 18676 18677 18678 18679 18680 18681 18682 18683 18684 18685 18686 18687 18688 18689 18690 18691 18692 18693 18694 18695 18696 18697 18698 18699 18700 18701 18702 18703 18704 18705 18706 18707 18708 18709 18710 18711 18712 18713 18714 18715 18716 18717 18718 18719 18720 18721 18722 18723 18724 18725 18726 18727 18728 18729 18730 18731 18732 18733 18734 18735 18736 18737 18738 18739 18740 18741 18742 18743 18744 18745 18746 18747 18748 18749 18750 18751 18752 18753 18754 18755 18756 18757 18758 18759 18760 18761 18762 18763 18764 18765 18766 18767 18768 18769 18770 18771 18772 18773 18774 18775 18776 18777 18778 18779 18780 18781 18782 18783 18784 18785 18786 18787 18788 18789 18790 18791 18792 18793 18794 18795 18796 18797 18798 18799 18800 18801 18802 18803 18804 18805 18806 18807 18808 18809 18810 18811 18812 18813 18814 18815 18816 18817 18818 18819 18820 18821 18822 18823 18824 18825 18826 18827 18828 18829 18830 18831 18832 18833 18834 18835 18836 18837 18838 18839 18840 18841 18842 18843 18844 18845 18846 18847 18848 18849 18850 18851 18852 18853 18854 18855 18856 18857 18858 18859 18860 18861 18862 18863 18864 18865 18866 18867 18868 18869 18870 18871 18872 18873 18874 18875 18876 18877 18878 18879 18880 18881 18882 18883 18884 18885 18886 18887 18888 18889 18890 18891 18892 18893 18894 18895 18896 18897 18898 18899 18900 18901 18902 18903 18904 18905 18906 18907 18908 18909 18910 18911 18912 18913 18914 18915 18916 18917 18918 18919 18920 18921 18922 18923 18924 18925 18926 18927 18928 18929 18930 18931 18932 18933 18934 18935 18936 18937 18938 18939 18940 18941 18942 18943 18944 18945 18946 18947 18948 18949 18950 18951 18952 18953 18954 18955 18956 18957 18958 18959 18960 18961 18962 18963 18964 18965 18966 18967 18968 18969 18970 18971 18972 18973 18974 18975 18976 18977 18978 18979 18980 18981 18982 18983 18984 18985 18986 18987 18988 18989 18990 18991 18992 18993 18994 18995 18996 18997 18998 18999 19000 19001 19002 19003 19004 19005 19006 19007 19008 19009 19010 19011 19012 19013 19014 19015 19016 19017 19018 19019 19020 19021 19022 19023 19024 19025 19026 19027 19028 19029 19030 19031 19032 19033 19034 19035 19036 19037 19038 19039 19040 19041 19042 19043 19044 19045 19046 19047 19048 19049 19050 19051 19052 19053 19054 19055 19056 19057 19058 19059 19060 19061 19062 19063 19064 19065 19066 19067 19068 19069 19070 19071 19072 19073 19074 19075 19076 19077 19078 19079 19080 19081 19082 19083 19084 19085 19086 19087 19088 19089 19090 19091 19092 19093 19094 19095 19096 19097 19098 19099 19100 19101 19102 19103 19104 19105 19106 19107 19108 19109 19110 19111 19112 19113 19114 19115 19116 19117 19118 19119 19120 19121 19122 19123 19124 19125 19126 19127 19128 19129 19130 19131 19132 19133 19134 19135 19136 19137 19138 19139 19140 19141 19142 19143 19144 19145 19146 19147 19148 19149 19150 19151 19152 19153 19154 19155 19156 19157 19158 19159 19160 19161 19162 19163 19164 19165 19166 19167 19168 19169 19170 19171 19172 19173 19174 19175 19176 19177 19178 19179 19180 19181 19182 19183 19184 19185 19186 19187 19188 19189 19190 19191 19192 19193 19194 19195 19196 19197 19198 19199 19200 19201 19202 19203 19204 19205 19206 19207 19208 19209 19210 19211 19212 19213 19214 19215 19216 19217 19218 19219 19220 19221 19222 19223 19224 19225 19226 19227 19228 19229 19230 19231 19232 19233 19234 19235 19236 19237 19238 19239 19240 19241 19242 19243 19244 19245 19246 19247 19248 19249 19250 19251 19252 19253 19254 19255 19256 19257 19258 19259 19260 19261 19262 19263 19264 19265 19266 19267 19268 19269 19270 19271 19272 19273 19274 19275 19276 19277 19278 19279 19280 19281 19282 19283 19284 19285 19286 19287 19288 19289 19290 19291 19292 19293 19294 19295 19296 19297 19298 19299 19300 19301 19302 19303 19304 19305 19306 19307 19308 19309 19310 19311 19312 19313 19314 19315 19316 19317 19318 19319 19320 19321 19322 19323 19324 19325 19326 19327 19328 19329 19330 19331 19332 19333 19334 19335 19336 19337 19338 19339 19340 19341 19342 19343 19344 19345 19346 19347 19348 19349 19350 19351 19352 19353 19354 19355 19356 19357 19358 19359 19360 19361 19362 19363 19364 19365 19366 19367 19368 19369 19370 19371 19372 19373 19374 19375 19376 19377 19378 19379 19380 19381 19382 19383 19384 19385 19386 19387 19388 19389 19390 19391 19392 19393 19394 19395 19396 19397 19398 19399 19400 19401 19402 19403 19404 19405 19406 19407 19408 19409 19410 19411 19412 19413 19414 19415 19416 19417 19418 19419 19420 19421 19422 19423 19424 19425 19426 19427 19428 19429 19430 19431 19432 19433 19434 19435 19436 19437 19438 19439 19440 19441 19442 19443 19444 19445 19446 19447 19448 19449 19450 19451 19452 19453 19454 19455 19456 19457 19458 19459 19460 19461 19462 19463 19464 19465 19466 19467 19468 19469 19470 19471 19472 19473 19474 19475 19476 19477 19478 19479 19480 19481 19482 19483 19484 19485 19486 19487 19488 19489 19490 19491 19492 19493 19494 19495 19496 19497 19498 19499 19500 19501 19502 19503 19504 19505 19506 19507 19508 19509 19510 19511 19512 19513 19514 19515 19516 19517 19518 19519 19520 19521 19522 19523 19524 19525 19526 19527 19528 19529 19530 19531 19532 19533 19534 19535 19536 19537 19538 19539 19540 19541 19542 19543 19544 19545 19546 19547 19548 19549 19550 19551 19552 19553 19554 19555 19556 19557 19558 19559 19560 19561 19562 19563 19564 19565 19566 19567 19568 19569 19570 19571 19572 19573 19574 19575 19576 19577 19578 19579 19580 19581 19582 19583 19584 19585 19586 19587 19588 19589 19590 19591 19592 19593 19594 19595 19596 19597 19598 19599 19600 19601 19602 19603 19604 19605 19606 19607 19608 19609 19610 19611 19612 19613 19614 19615 19616 19617 19618 19619 19620 19621 19622 19623 19624 19625 19626 19627 19628 19629 19630 19631 19632 19633 19634 19635 19636 19637 19638 19639 19640 19641 19642 19643 19644 19645 19646 19647 19648 19649 19650 19651 19652 19653 19654 19655 19656 19657 19658 19659 19660 19661 19662 19663 19664 19665 19666 19667 19668 19669 19670 19671 19672 19673 19674 19675 19676 19677 19678 19679 19680 19681 19682 19683 19684 19685 19686 19687 19688 19689 19690 19691 19692 19693 19694 19695 19696 19697 19698 19699 19700 19701 19702 19703 19704 19705 19706 19707 19708 19709 19710 19711 19712 19713 19714 19715 19716 19717 19718 19719 19720 19721 19722 19723 19724 19725 19726 19727 19728 19729 19730 19731 19732 19733 19734 19735 19736 19737 19738 19739 19740 19741 19742 19743 19744 19745 19746 19747 19748 19749 19750 19751 19752 19753 19754 19755 19756 19757 19758 19759 19760 19761 19762 19763 19764 19765 19766 19767 19768 19769 19770 19771 19772 19773 19774 19775 19776 19777 19778 19779 19780 19781 19782 19783 19784 19785 19786 19787 19788 19789 19790 19791 19792 19793 19794 19795 19796 19797 19798 19799 19800 19801 19802 19803 19804 19805 19806 19807 19808 19809 19810 19811 19812 19813 19814 19815 19816 19817 19818 19819 19820 19821 19822 19823 19824 19825 19826 19827 19828 19829 19830 19831 19832 19833 19834 19835 19836 19837 19838 19839 19840 19841 19842 19843 19844 19845 19846 19847 19848 19849 19850 19851 19852 19853 19854 19855 19856 19857 19858 19859 19860 19861 19862 19863 19864 19865 19866 19867 19868 19869 19870 19871 19872 19873 19874 19875 19876 19877 19878 19879 19880 19881 19882 19883 19884 19885 19886 19887 19888 19889 19890 19891 19892 19893 19894 19895 19896 19897 19898 19899 19900 19901 19902 19903 19904
|
/*
* International Color Consortium Format Library (icclib)
* For ICC profile version 3.4
*
* Author: Graeme W. Gill
* Date: 2002/04/22
* Version: 2.15
*
* Copyright 1997 - 2013 Graeme W. Gill
*
* This material is licensed with an "MIT" free use license:-
* see the License4.txt file in this directory for licensing details.
*/
/*
* TTBD:
*
* Add a "warning mode" to file reading, in which file format
* errors are ignored where possible, rather than generating
* a fatal error (see ICM_STRICT #define).
*
* NameColor Dump doesn't handle device space correctly -
* should use appropriate interpretation in case device is Lab etc.
*
* Should recognise & honour unicode 0xFFFE endian marker.
* Should generate it on writing too ?
*
* Add support for copying tags from one icc to another.
*
* Should fix all write_number failure errors to indicate failed value.
* (Partially implemented - need to check all write_number functions)
*
* Make write fail error messages be specific on which element failed.
*
* Should add named color space lookup function support.
*
* Would be nice to add generic ability to add new tag type handling,
* so that the base library doesn't need to be modified (ie. VideoCardGamma) ?
*
* Need to add DeviceSettings and OutputResponse tags to bring up to
* ICC.1:1998-09 [started but not complete]
*
*/
#undef ICM_STRICT /* Not fully implimented - switch off strict checking of file format */
/* Make the default grid points of the Lab clut be symetrical about */
/* a/b 0.0, and also make L = 100.0 fall on a grid point. */
#define SYMETRICAL_DEFAULT_LAB_RANGE
#define _ICC_C_ /* Turn on implimentation code */
#undef DEBUG_SETLUT /* [Und] Show each value being set in setting lut contents */
#undef DEBUG_SETLUT_CLIP /* [Und] Show clipped values when setting LUT */
#undef DEBUG_LULUT /* [Und] Show each value being looked up from lut contents */
#undef DEBUG_LLULUT /* [Und] Debug individual lookup steps (not fully implemented) */
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
#ifdef __sun
#include <unistd.h>
#endif
#if defined(__IBMC__) && defined(_M_IX86)
#include <float.h>
#endif
#include "icc.h"
#if defined(_MSC_VER) && !defined(vsnprintf)
#define vsnprintf _vsnprintf
#define snprintf _snprintf
#endif
/* ========================================================== */
/* Default system interface object implementations */
#ifndef SEPARATE_STD
#define COMBINED_STD
#include "iccstd.c"
#undef COMBINED_STD
#endif /* SEPARATE_STD */
/* Forced byte alignment for tag table and tags */
#define ALIGN_SIZE 4
/* =========================================================== */
#ifdef DEBUG_SETLUT
#undef DBGSL
#define DBGSL(xxx) printf xxx ;
#else
#undef DBGSL
#define DBGSL(xxx)
#endif
#if defined(DEBUG_SETLUT) || defined(DEBUG_SETLUT_CLIP)
#undef DBGSLC
#define DBGSLC(xxx) printf xxx ;
#else
#undef DBGSLC
#define DBGSLC(xxx)
#endif
#ifdef DEBUG_LULUT
#undef DBGLL
#define DBGLL(xxx) printf xxx ;
#else
#undef DBGLL
#define DBGLL(xxx)
#endif
#ifdef DEBUG_LLULUT
#undef DBLLL
#define DBLLL(xxx) printf xxx ;
#else
#undef DBLLL
#define DBLLL(xxx)
#endif
#ifndef M_PI
# define M_PI 3.14159265358979323846
#endif
/* =========================================================== */
/* Overflow protected unsigned int arithmatic functions. */
/* These functions saturate rather than wrapping around. */
/* (Divide doesn't need protection) */
/* They return UINT_MAX if there was an overflow */
/* a + b */
static unsigned int sat_add(unsigned int a, unsigned int b) {
if (b > (UINT_MAX - a))
return UINT_MAX;
return a + b;
}
/* a - b */
static unsigned int sat_sub(unsigned int a, unsigned int b) {
if (a < b)
return UINT_MAX;
return a - b;
}
/* a * b */
static unsigned int sat_mul(unsigned int a, unsigned int b) {
unsigned int c;
if (a == 0 || b == 0)
return 0;
if (a > (UINT_MAX/b))
return UINT_MAX;
else
return a * b;
}
/* A + B + C */
#define sat_addadd(A, B, C) sat_add(A, sat_add(B, C))
/* A + B * C */
#define sat_addmul(A, B, C) sat_add(A, sat_mul(B, C))
/* A + B + C * D */
#define sat_addaddmul(A, B, C, D) sat_add(A, sat_add(B, sat_mul(C, D)))
/* A * B * C */
#define sat_mul3(A, B, C) sat_mul(A, sat_mul(B, C))
/* a ^ b */
static unsigned int sat_pow(unsigned int a, unsigned int b) {
unsigned int c = 1;
for (; b > 0; b--) {
c = sat_mul(c, a);
if (c == UINT_MAX)
break;
}
return c;
}
/* Alignment */
static unsigned int sat_align(unsigned int align_size, unsigned int a) {
align_size--;
if (align_size > (UINT_MAX - a))
return UINT_MAX;
return (a + align_size) & ~align_size;
}
/* These test functions detect whether an overflow would occur */
/* Return nz if add would overflow */
static int ovr_add(unsigned int a, unsigned int b) {
if (b > (UINT_MAX - a))
return 1;
return 0;
}
/* Return nz if sub would overflow */
static int ovr_sub(unsigned int a, unsigned int b) {
if (a < b)
return 1;
return 0;
}
/* Return nz if mult would overflow */
static int ovr_mul(unsigned int a, unsigned int b) {
if (a > (UINT_MAX/b))
return 1;
return 0;
}
/* size_t versions of saturating arithmatic */
#ifndef SIZE_MAX
# define SIZE_MAX ((size_t)(-1))
#endif
/* a + b */
static size_t ssat_add(size_t a, size_t b) {
if (b > (SIZE_MAX - a))
return SIZE_MAX;
return a + b;
}
/* a - b */
static size_t ssat_sub(size_t a, size_t b) {
if (a < b)
return SIZE_MAX;
return a - b;
}
/* a * b */
static size_t ssat_mul(size_t a, size_t b) {
size_t c;
if (a == 0 || b == 0)
return 0;
if (a > (SIZE_MAX/b))
return SIZE_MAX;
else
return a * b;
}
/* ------------------------------------------------- */
/* Memory image icmFile compatible class */
/* Buffer is assumed to have been allocated by the given allocator, */
/* and will be expanded on write. */
/* Get the size of the file */
static size_t icmFileMem_get_size(icmFile *pp) {
icmFileMem *p = (icmFileMem *)pp;
return p->end - p->start;
}
/* Set current position to offset. Return 0 on success, nz on failure. */
static int icmFileMem_seek(
icmFile *pp,
unsigned int offset
) {
icmFileMem *p = (icmFileMem *)pp;
unsigned char *np;
np = p->start + offset;
if (np < p->start || np >= p->end)
return 1;
p->cur = np;
return 0;
}
/* Read count items of size length. Return number of items successfully read. */
static size_t icmFileMem_read(
icmFile *pp,
void *buffer,
size_t size,
size_t count
) {
icmFileMem *p = (icmFileMem *)pp;
size_t len;
len = ssat_mul(size, count);
if (len > (p->end - p->cur)) { /* Too much */
if (size > 0)
count = (p->end - p->cur)/size;
else
count = 0;
}
len = size * count;
if (len > 0)
memmove(buffer, p->cur, len);
p->cur += len;
return count;
}
/* Expand the memory buffer file to hold up to pointer ep */
/* Don't expand if realloc fails */
static void icmFileMem_filemem_resize(icmFileMem *p, unsigned char *ep) {
size_t na, co, ce;
unsigned char *nstart;
/* No need to realloc */
if (ep <= p->aend) {
return;
}
co = p->cur - p->start; /* Current offset */
ce = p->end - p->start; /* Current end */
na = ep - p->start; /* new allocated size */
/* Round new allocation up */
if (na <= 1024)
na += 1024;
else
na += 4096;
if ((nstart = p->al->realloc(p->al, p->start, na)) != NULL) {
p->start = nstart;
p->cur = nstart + co;
p->end = nstart + ce;
p->aend = nstart + na;
}
}
/* write count items of size length. Return number of items successfully written. */
static size_t icmFileMem_write(
icmFile *pp,
void *buffer,
size_t size,
size_t count
) {
icmFileMem *p = (icmFileMem *)pp;
size_t len;
len = ssat_mul(size, count);
if (len > (size_t)(p->aend - p->cur)) /* Try and expand buffer */
icmFileMem_filemem_resize(p, p->start + len);
if (len > (size_t)(p->aend - p->cur)) {
if (size > 0)
count = (p->aend - p->cur)/size;
else
count = 0;
}
len = size * count;
if (len > 0)
memmove(p->cur, buffer, len);
p->cur += len;
if (p->end < p->cur)
p->end = p->cur;
return count;
}
/* do a printf */
static int icmFileMem_printf(
icmFile *pp,
const char *format,
...
) {
int rv;
va_list args;
icmFileMem *p = (icmFileMem *)pp;
int alen, len;
va_start(args, format);
rv = 1;
alen = 100; /* Initial allocation for printf */
icmFileMem_filemem_resize(p, p->cur + alen);
/* We have to use the available printf functions to resize the buffer if needed. */
for (;rv != 0;) {
/* vsnprintf() either returns -1 if it doesn't fit, or */
/* returns the size-1 needed in order to fit. */
len = vsnprintf((char *)p->cur, (p->aend - p->cur), format, args);
if (len > -1 && ((p->cur + len +1) <= p->aend)) /* Fitted in current allocation */
break;
if (len > -1) /* vsnprintf returned needed size-1 */
alen = len+2; /* (In case vsnprintf returned 1 less than it needs) */
else
alen *= 2; /* We just have to guess */
/* Attempt to resize */
icmFileMem_filemem_resize(p, p->cur + alen);
/* If resize failed */
if ((p->aend - p->cur) < alen) {
rv = 0;
break;
}
}
if (rv != 0) {
/* Figure out where end of printf is */
len = strlen((char *)p->cur); /* Length excluding nul */
p->cur += len;
if (p->cur > p->end)
p->end = p->cur;
rv = len;
}
va_end(args);
return rv;
}
/* flush all write data out to secondary storage. Return nz on failure. */
static int icmFileMem_flush(
icmFile *pp
) {
return 0;
}
/* Return the memory buffer. Error if not icmFileMem */
static int icmFileMem_get_buf(
icmFile *pp,
unsigned char **buf,
size_t *len
) {
icmFileMem *p = (icmFileMem *)pp;
if (buf != NULL)
*buf = p->start;
if (len != NULL)
*len = p->end - p->start;
return 0;
}
/* we're done with the file object, return nz on failure */
static int icmFileMem_delete(
icmFile *pp
) {
icmFileMem *p = (icmFileMem *)pp;
icmAlloc *al = p->al;
int del_al = p->del_al;
if (p->del_buf) /* Free the memory buffer */
al->free(al, p->start);
al->free(al, p); /* Free object */
if (del_al) /* We are responsible for deleting allocator */
al->del(al);
return 0;
}
/* Create a memory image file access class with allocator */
/* Buffer is used as is. */
icmFile *new_icmFileMem_a(
void *base, /* Pointer to base of memory buffer */
size_t length, /* Number of bytes in buffer */
icmAlloc *al /* heap allocator */
) {
icmFileMem *p;
if ((p = (icmFileMem *) al->calloc(al, 1, sizeof(icmFileMem))) == NULL) {
return NULL;
}
p->al = al; /* Heap allocator */
p->get_size = icmFileMem_get_size;
p->seek = icmFileMem_seek;
p->read = icmFileMem_read;
p->write = icmFileMem_write;
p->gprintf = icmFileMem_printf;
p->flush = icmFileMem_flush;
p->get_buf = icmFileMem_get_buf;
p->del = icmFileMem_delete;
p->start = (unsigned char *)base;
p->cur = p->start;
p->aend = p->end = p->start + length;
return (icmFile *)p;
}
/* Create a memory image file access class with given allocator */
/* and delete base when icmFile is deleted. */
icmFile *new_icmFileMem_ad(void *base, size_t length, icmAlloc *al) {
icmFile *fp;
if ((fp = new_icmFileMem_a(base, length, al)) != NULL) {
((icmFileMem *)fp)->del_buf = 1;
}
return fp;
}
/* ========================================================== */
/* Conversion support functions */
/* Convert between ICC storage types and native C types */
/* Write routine return non-zero if numbers can't be represented */
/* Unsigned */
static unsigned int read_UInt8Number(char *p) {
unsigned int rv;
rv = (unsigned int)((ORD8 *)p)[0];
return rv;
}
static int write_UInt8Number(unsigned int d, char *p) {
if (d > 255)
return 1;
((ORD8 *)p)[0] = (ORD8)d;
return 0;
}
static unsigned int read_UInt16Number(char *p) {
unsigned int rv;
rv = 256 * (unsigned int)((ORD8 *)p)[0]
+ (unsigned int)((ORD8 *)p)[1];
return rv;
}
static int write_UInt16Number(unsigned int d, char *p) {
if (d > 65535)
return 1;
((ORD8 *)p)[0] = (ORD8)(d >> 8);
((ORD8 *)p)[1] = (ORD8)(d);
return 0;
}
static unsigned int read_UInt32Number(char *p) {
unsigned int rv;
rv = 16777216 * (unsigned int)((ORD8 *)p)[0]
+ 65536 * (unsigned int)((ORD8 *)p)[1]
+ 256 * (unsigned int)((ORD8 *)p)[2]
+ (unsigned int)((ORD8 *)p)[3];
return rv;
}
static int write_UInt32Number(unsigned int d, char *p) {
((ORD8 *)p)[0] = (ORD8)(d >> 24);
((ORD8 *)p)[1] = (ORD8)(d >> 16);
((ORD8 *)p)[2] = (ORD8)(d >> 8);
((ORD8 *)p)[3] = (ORD8)(d);
return 0;
}
static void read_UInt64Number(icmUint64 *d, char *p) {
d->h = 16777216 * (unsigned int)((ORD8 *)p)[0]
+ 65536 * (unsigned int)((ORD8 *)p)[1]
+ 256 * (unsigned int)((ORD8 *)p)[2]
+ (unsigned int)((ORD8 *)p)[3];
d->l = 16777216 * (unsigned int)((ORD8 *)p)[4]
+ 65536 * (unsigned int)((ORD8 *)p)[5]
+ 256 * (unsigned int)((ORD8 *)p)[6]
+ (unsigned int)((ORD8 *)p)[7];
}
static int write_UInt64Number(icmUint64 *d, char *p) {
((ORD8 *)p)[0] = (ORD8)(d->h >> 24);
((ORD8 *)p)[1] = (ORD8)(d->h >> 16);
((ORD8 *)p)[2] = (ORD8)(d->h >> 8);
((ORD8 *)p)[3] = (ORD8)(d->h);
((ORD8 *)p)[4] = (ORD8)(d->l >> 24);
((ORD8 *)p)[5] = (ORD8)(d->l >> 16);
((ORD8 *)p)[6] = (ORD8)(d->l >> 8);
((ORD8 *)p)[7] = (ORD8)(d->l);
return 0;
}
static double read_U8Fixed8Number(char *p) {
ORD32 o32;
o32 = 256 * (ORD32)((ORD8 *)p)[0] /* Read big endian 16 bit unsigned */
+ (ORD32)((ORD8 *)p)[1];
return (double)o32/256.0;
}
static int write_U8Fixed8Number(double d, char *p) {
ORD32 o32;
d = d * 256.0 + 0.5;
if (d >= 65536.0)
return 1;
if (d < 0.0)
return 1;
o32 = (ORD32)d;
((ORD8 *)p)[0] = (ORD8)((o32) >> 8);
((ORD8 *)p)[1] = (ORD8)((o32));
return 0;
}
static double read_U16Fixed16Number(char *p) {
ORD32 o32;
o32 = 16777216 * (ORD32)((ORD8 *)p)[0] /* Read big endian 32 bit unsigned */
+ 65536 * (ORD32)((ORD8 *)p)[1]
+ 256 * (ORD32)((ORD8 *)p)[2]
+ (ORD32)((ORD8 *)p)[3];
return (double)o32/65536.0;
}
static int write_U16Fixed16Number(double d, char *p) {
ORD32 o32;
d = d * 65536.0 + 0.5;
if (d >= 4294967296.0)
return 1;
if (d < 0.0)
return 1;
o32 = (ORD32)d;
((ORD8 *)p)[0] = (ORD8)((o32) >> 24);
((ORD8 *)p)[1] = (ORD8)((o32) >> 16);
((ORD8 *)p)[2] = (ORD8)((o32) >> 8);
((ORD8 *)p)[3] = (ORD8)((o32));
return 0;
}
/* Signed numbers */
static int read_SInt8Number(char *p) {
int rv;
rv = (int)((INR8 *)p)[0];
return rv;
}
static int write_SInt8Number(int d, char *p) {
if (d > 127)
return 1;
else if (d < -128)
return 1;
((INR8 *)p)[0] = (INR8)d;
return 0;
}
static int read_SInt16Number(char *p) {
int rv;
rv = 256 * (int)((INR8 *)p)[0]
+ (int)((ORD8 *)p)[1];
return rv;
}
static int write_SInt16Number(int d, char *p) {
if (d > 32767)
return 1;
else if (d < -32768)
return 1;
((INR8 *)p)[0] = (INR8)(d >> 8);
((ORD8 *)p)[1] = (ORD8)(d);
return 0;
}
static int read_SInt32Number(char *p) {
int rv;
rv = 16777216 * (int)((INR8 *)p)[0]
+ 65536 * (int)((ORD8 *)p)[1]
+ 256 * (int)((ORD8 *)p)[2]
+ (int)((ORD8 *)p)[3];
return rv;
}
static int write_SInt32Number(int d, char *p) {
((INR8 *)p)[0] = (INR8)(d >> 24);
((ORD8 *)p)[1] = (ORD8)(d >> 16);
((ORD8 *)p)[2] = (ORD8)(d >> 8);
((ORD8 *)p)[3] = (ORD8)(d);
return 0;
}
static void read_SInt64Number(icmInt64 *d, char *p) {
d->h = 16777216 * (int)((INR8 *)p)[0]
+ 65536 * (int)((ORD8 *)p)[1]
+ 256 * (int)((ORD8 *)p)[2]
+ (int)((ORD8 *)p)[3];
d->l = 16777216 * (unsigned int)((ORD8 *)p)[4]
+ 65536 * (unsigned int)((ORD8 *)p)[5]
+ 256 * (unsigned int)((ORD8 *)p)[6]
+ (unsigned int)((ORD8 *)p)[7];
}
static int write_SInt64Number(icmInt64 *d, char *p) {
((INR8 *)p)[0] = (INR8)(d->h >> 24);
((ORD8 *)p)[1] = (ORD8)(d->h >> 16);
((ORD8 *)p)[2] = (ORD8)(d->h >> 8);
((ORD8 *)p)[3] = (ORD8)(d->h);
((ORD8 *)p)[4] = (ORD8)(d->l >> 24);
((ORD8 *)p)[5] = (ORD8)(d->l >> 16);
((ORD8 *)p)[6] = (ORD8)(d->l >> 8);
((ORD8 *)p)[7] = (ORD8)(d->l);
return 0;
}
static double read_S15Fixed16Number(char *p) {
INR32 i32;
i32 = 16777216 * (INR32)((INR8 *)p)[0] /* Read big endian 32 bit signed */
+ 65536 * (INR32)((ORD8 *)p)[1]
+ 256 * (INR32)((ORD8 *)p)[2]
+ (INR32)((ORD8 *)p)[3];
return (double)i32/65536.0;
}
static int write_S15Fixed16Number(double d, char *p) {
INR32 i32;
d = floor(d * 65536.0 + 0.5); /* Beware! (int)(d + 0.5) doesn't work! */
if (d >= 2147483648.0)
return 1;
if (d < -2147483648.0)
return 1;
i32 = (INR32)d;
((INR8 *)p)[0] = (INR8)((i32) >> 24); /* Write big endian 32 bit signed */
((ORD8 *)p)[1] = (ORD8)((i32) >> 16);
((ORD8 *)p)[2] = (ORD8)((i32) >> 8);
((ORD8 *)p)[3] = (ORD8)((i32));
return 0;
}
/* Round a number to the same quantization as a S15Fixed16 */
static double round_S15Fixed16Number(double d) {
d = floor(d * 65536.0 + 0.5); /* Beware! (int)(d + 0.5) doesn't work for -ve nummbets ! */
d = d/65536.0;
return d;
}
/* Macro version */
#define RND_S15FIXED16(xxx) ((xxx) > 0.0 ? (int)((xxx) * 65536.0 + 0.5)/65536.0 \
: (int)((xxx) * 65536.0 - 0.5)/65536.0)
/* Device coordinate as 8 bit value range 0.0 - 1.0 */
static double read_DCS8Number(char *p) {
unsigned int rv;
rv = (unsigned int)((ORD8 *)p)[0];
return (double)rv/255.0;
}
static int write_DCS8Number(double d, char *p) {
ORD32 o32;
d = d * 255.0 + 0.5;
if (d >= 256.0)
return 1;
if (d < 0.0)
return 1;
o32 = (ORD32)d;
((ORD8 *)p)[0] = (ORD8)(o32);
return 0;
}
/* Device coordinate as 16 bit value range 0.0 - 1.0 */
static double read_DCS16Number(char *p) {
unsigned int rv;
rv = 256 * (unsigned int)((ORD8 *)p)[0]
+ (unsigned int)((ORD8 *)p)[1];
return (double)rv/65535.0;
}
static int write_DCS16Number(double d, char *p) {
ORD32 o32;
d = d * 65535.0 + 0.5;
if (d >= 65536.0)
return 1;
if (d < 0.0)
return 1;
o32 = (ORD32)d;
((ORD8 *)p)[0] = (ORD8)(o32 >> 8);
((ORD8 *)p)[1] = (ORD8)(o32);
return 0;
}
static void Lut_Lut2XYZ(double *out, double *in);
static void Lut_XYZ2Lut(double *out, double *in);
static void Lut_Lut2Lab_8(double *out, double *in);
static void Lut_Lab2Lut_8(double *out, double *in);
static void Lut_Lut2LabV2_16(double *out, double *in);
static void Lut_Lab2LutV2_16(double *out, double *in);
static void Lut_Lut2LabV4_16(double *out, double *in);
static void Lut_Lab2LutV4_16(double *out, double *in);
static void Lut_Lut2Y(double *out, double *in);
static void Lut_Y2Lut(double *out, double *in);
static void Lut_Lut2L_8(double *out, double *in);
static void Lut_L2Lut_8(double *out, double *in);
static void Lut_Lut2LV2_16(double *out, double *in);
static void Lut_L2LutV2_16(double *out, double *in);
static void Lut_Lut2LV4_16(double *out, double *in);
static void Lut_L2LutV4_16(double *out, double *in);
/* read a PCS number. PCS can be profile PCS, profile version Lab, */
/* or a specific type of Lab, depending on the value of csig: */
/* icmSigPCSData, icSigXYZData, icmSigLab8Data, icSigLabData, */
/* icmSigLabV2Data or icmSigLabV4Data */
/* Do nothing if not one of the above. */
static void read_PCSNumber(icc *icp, icColorSpaceSignature csig, double pcs[3], char *p) {
if (csig == icmSigPCSData)
csig = icp->header->pcs;
if (csig == icSigLabData) {
if (icp->ver >= icmVersion4_1)
csig = icmSigLabV4Data;
else
csig = icmSigLabV2Data;
}
if (csig == icmSigLab8Data) {
pcs[0] = read_DCS8Number(p);
pcs[1] = read_DCS8Number(p+1);
pcs[2] = read_DCS8Number(p+2);
} else {
pcs[0] = read_DCS16Number(p);
pcs[1] = read_DCS16Number(p+2);
pcs[2] = read_DCS16Number(p+4);
}
switch ((int)csig) {
case icSigXYZData:
Lut_Lut2XYZ(pcs, pcs);
break;
case icmSigLab8Data:
Lut_Lut2Lab_8(pcs, pcs);
break;
case icmSigLabV2Data:
Lut_Lut2LabV2_16(pcs, pcs);
break;
case icmSigLabV4Data:
Lut_Lut2LabV4_16(pcs, pcs);
break;
default:
break;
}
}
/* write a PCS number. PCS can be profile PCS, profile version Lab, */
/* or a specific type of Lab, depending on the value of csig: */
/* icmSigPCSData, icSigXYZData, icmSigLab8Data, icSigLabData, */
/* icmSigLabV2Data or icmSigLabV4Data */
/* Return 1 if error */
static int write_PCSNumber(icc *icp, icColorSpaceSignature csig, double pcs[3], char *p) {
double v[3];
int j;
if (csig == icmSigPCSData)
csig = icp->header->pcs;
if (csig == icSigLabData) {
if (icp->ver >= icmVersion4_1)
csig = icmSigLabV4Data;
else
csig = icmSigLabV2Data;
}
switch ((int)csig) {
case icSigXYZData:
Lut_XYZ2Lut(v, pcs);
break;
case icmSigLab8Data:
Lut_Lab2Lut_8(v, pcs);
break;
case icmSigLabV2Data:
Lut_Lab2LutV2_16(v, pcs);
break;
case icmSigLabV4Data:
Lut_Lab2LutV4_16(v, pcs);
break;
default:
return 1;
}
if (csig == icmSigLab8Data) {
for (j = 0; j < 3; j++) {
if (write_DCS8Number(v[j], p+j))
return 1;
}
} else {
for (j = 0; j < 3; j++) {
if (write_DCS16Number(v[j], p+(2 * j)))
return 1;
}
}
return 0;
}
/* Read a given primitive type. Return non-zero on error */
/* (Not currently used internaly ?) */
/* Public: */
int read_Primitive(icc *icp, icmPrimType ptype, void *prim, char *p) {
switch (ptype) {
case icmUInt8Number:
*((unsigned int *)prim) = read_UInt8Number(p);
return 0;
case icmUInt16Number:
*((unsigned int *)prim) = read_UInt16Number(p);
return 0;
case icmUInt32Number:
*((unsigned int *)prim) = read_UInt32Number(p);
return 0;
case icmUInt64Number:
read_UInt64Number((icmUint64 *)prim, p);
return 0;
case icmU8Fixed8Number:
*((double *)prim) = read_U8Fixed8Number(p);
return 0;
case icmU16Fixed16Number:
*((double *)prim) = read_U16Fixed16Number(p);
return 0;
case icmSInt8Number:
*((int *)prim) = read_SInt8Number(p);
return 0;
case icmSInt16Number:
*((int *)prim) = read_SInt16Number(p);
return 0;
case icmSInt32Number:
*((int *)prim) = read_SInt32Number(p);
return 0;
case icmSInt64Number:
read_SInt64Number((icmInt64 *)prim, p);
return 0;
case icmS15Fixed16Number:
*((double *)prim) = read_S15Fixed16Number(p);
return 0;
case icmDCS8Number:
*((double *)prim) = read_DCS8Number(p);
return 0;
case icmDCS16Number:
*((double *)prim) = read_DCS16Number(p);
return 0;
case icmPCSNumber:
read_PCSNumber(icp, icmSigPCSData, ((double *)prim), p);
return 0;
case icmPCSXYZNumber:
read_PCSNumber(icp, icSigXYZData, ((double *)prim), p);
return 0;
case icmPCSLab8Number:
read_PCSNumber(icp, icmSigLab8Data, ((double *)prim), p);
return 0;
case icmPCSLabNumber:
read_PCSNumber(icp, icSigLabData, ((double *)prim), p);
return 0;
case icmPCSLabV2Number:
read_PCSNumber(icp, icmSigLabV2Data, ((double *)prim), p);
return 0;
case icmPCSLabV4Number:
read_PCSNumber(icp, icmSigLabV4Data, ((double *)prim), p);
return 0;
}
return 2;
}
/* Write a given primitive type. Return non-zero on error */
/* (Not currently used internaly ?) */
/* Public: */
int write_Primitive(icc *icp, icmPrimType ptype, char *p, void *prim) {
switch (ptype) {
case icmUInt8Number:
return write_UInt8Number(*((unsigned int *)prim), p);
case icmUInt16Number:
return write_UInt16Number(*((unsigned int *)prim), p);
case icmUInt32Number:
return write_UInt32Number(*((unsigned int *)prim), p);
case icmUInt64Number:
return write_UInt64Number((icmUint64 *)prim, p);
case icmU8Fixed8Number:
return write_U8Fixed8Number(*((double *)prim), p);
case icmU16Fixed16Number:
return write_U16Fixed16Number(*((double *)prim), p);
case icmSInt8Number:
return write_SInt8Number(*((int *)prim), p);
case icmSInt16Number:
return write_SInt16Number(*((int *)prim), p);
case icmSInt32Number:
return write_SInt32Number(*((int *)prim), p);
case icmSInt64Number:
return write_SInt64Number((icmInt64 *)prim, p);
case icmS15Fixed16Number:
return write_S15Fixed16Number(*((double *)prim), p);
case icmDCS8Number:
return write_DCS8Number(*((double *)prim), p);
case icmDCS16Number:
return write_DCS16Number(*((double *)prim), p);
case icmPCSNumber:
return write_PCSNumber(icp, icmSigPCSData, ((double *)prim), p);
case icmPCSXYZNumber:
return write_PCSNumber(icp, icSigXYZData, ((double *)prim), p);
case icmPCSLab8Number:
return write_PCSNumber(icp, icmSigLab8Data, ((double *)prim), p);
case icmPCSLabNumber:
return write_PCSNumber(icp, icSigLabData, ((double *)prim), p);
case icmPCSLabV2Number:
return write_PCSNumber(icp, icmSigLabV2Data, ((double *)prim), p);
case icmPCSLabV4Number:
return write_PCSNumber(icp, icmSigLabV4Data, ((double *)prim), p);
}
return 2;
}
/* ---------------------------------------------------------- */
/* Auiliary function - return a string that represents a tag */
/* Note - returned buffers are static, can only be used 5 */
/* times before buffers get reused. */
char *tag2str(
int tag
) {
int i;
static int si = 0; /* String buffer index */
static char buf[5][20]; /* String buffers */
char *bp;
unsigned char c[4];
bp = buf[si++];
si %= 5; /* Rotate through buffers */
c[0] = 0xff & (tag >> 24);
c[1] = 0xff & (tag >> 16);
c[2] = 0xff & (tag >> 8);
c[3] = 0xff & (tag >> 0);
for (i = 0; i < 4; i++) { /* Can we represent it as a string ? */
if (!isprint(c[i]))
break;
}
if (i < 4) { /* Not printable - use hex */
sprintf(bp,"0x%x",tag);
} else { /* Printable */
sprintf(bp,"'%c%c%c%c'",c[0],c[1],c[2],c[3]);
}
return bp;
}
/* Auiliary function - return a tag created from a string */
/* Note there is also the icmMakeTag() macro */
unsigned int str2tag(
const char *str
) {
unsigned int tag;
tag = (((unsigned int)str[0]) << 24)
+ (((unsigned int)str[1]) << 16)
+ (((unsigned int)str[2]) << 8)
+ (((unsigned int)str[3]));
return tag;
}
/* helper - return 1 if the string doesn't have a */
/* null terminator within len, return 0 if it has null at exactly len, */
/* and 2 if it has null before len. */
/* Note: will return 1 if len == 0 */
static int check_null_string(char *cp, int len) {
for (; len > 0; len--) {
if (cp[0] == '\000')
break;
cp++;
}
if (len == 0)
return 1;
if (len > 1)
return 2;
return 0;
}
/* helper - return 1 if the string doesn't have a */
/* null terminator within len, return 0 has null at exactly len, */
/* and 2 if it has null before len. */
/* Note: will return 1 if len == 0 */
/* Unicode version */
static int check_null_string16(char *cp, int len) {
for (; len > 0; len--) { /* Length is in characters */
if (cp[0] == 0 && cp[1] == 0)
break;
cp += 2;
}
if (len == 0)
return 1;
if (len > 1)
return 2;
return 0;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* Color Space to number of component conversion */
/* Return 0 on error */
static unsigned int number_ColorSpaceSignature(icColorSpaceSignature sig) {
switch ((int)sig) {
case icSigXYZData:
return 3;
case icSigLabData:
return 3;
case icSigLuvData:
return 3;
case icSigYCbCrData:
return 3;
case icSigYxyData:
return 3;
case icSigRgbData:
return 3;
case icSigGrayData:
return 1;
case icSigHsvData:
return 3;
case icSigHlsData:
return 3;
case icSigCmykData:
return 4;
case icSigCmyData:
return 3;
case icSig2colorData:
return 2;
case icSig3colorData:
return 3;
case icSig4colorData:
return 4;
case icSig5colorData:
case icSigMch5Data:
return 5;
case icSig6colorData:
case icSigMch6Data:
return 6;
case icSig7colorData:
case icSigMch7Data:
return 7;
case icSig8colorData:
case icSigMch8Data:
return 8;
case icSig9colorData:
return 9;
case icSig10colorData:
return 10;
case icSig11colorData:
return 11;
case icSig12colorData:
return 12;
case icSig13colorData:
return 13;
case icSig14colorData:
return 14;
case icSig15colorData:
return 15;
/* Non-standard and Pseudo spaces */
case icmSigYData:
return 1;
case icmSigLData:
return 1;
case icmSigLptData:
return 3;
case icmSigL8Data:
return 1;
case icmSigLV2Data:
return 1;
case icmSigLV4Data:
return 1;
case icmSigPCSData:
return 3;
case icmSigLab8Data:
return 3;
case icmSigLabV2Data:
return 3;
case icmSigLabV4Data:
return 3;
default:
break;
}
return 0;
}
/* Public version of above */
/* Return the number of channels for the given color space. Return 0 if unknown. */
ICCLIB_API unsigned int icmCSSig2nchan(icColorSpaceSignature sig) {
return number_ColorSpaceSignature(sig);
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* Return the individual channel names and number of channels give a colorspace signature. */
/* Return 0 if it is not a colorspace that itself defines particular channels, */
/* 1 if it is a colorant based colorspace, and 2 if it is not a colorant based space */
static int chnames_ColorSpaceSignature(
icColorSpaceSignature sig,
char *cvals[] /* Pointers to return for each channel */
) {
switch ((int)sig) {
case icSigXYZData:
cvals[0] = "CIE X";
cvals[1] = "CIE Y";
cvals[2] = "CIE Z";
return 2;
case icSigLabData:
cvals[0] = "CIE L*";
cvals[1] = "CIE a*";
cvals[2] = "CIE b*";
return 2;
case icSigLuvData:
cvals[0] = "CIE L*";
cvals[1] = "CIE u*";
cvals[2] = "CIE v*";
return 2;
/* Usually ITU-R BT.601 (was CCIR 601) */
case icSigYCbCrData:
cvals[0] = "ITU Y";
cvals[1] = "ITU Cb";
cvals[2] = "ITU Cr";
return 2;
case icSigYxyData:
cvals[0] = "CIE Y";
cvals[1] = "CIE x";
cvals[2] = "CIE y";
return 2;
/* Alvy Ray Smith ? */
case icSigHsvData:
cvals[0] = "RGB Hue";
cvals[1] = "RGB Saturation";
cvals[2] = "RGB Value";
return 2;
/* GSPC ? */
case icSigHlsData:
cvals[0] = "RGB Hue";
cvals[1] = "RGB Lightness";
cvals[2] = "RGB Saturation";
return 2;
case icSigCmyData:
cvals[0] = "Cyan";
cvals[1] = "Magenta";
cvals[2] = "Yellow";
return 1;
case icSigRgbData:
cvals[0] = "Red";
cvals[1] = "Green";
cvals[2] = "Blue";
return 1;
case icSigCmykData:
cvals[0] = "Cyan";
cvals[1] = "Magenta";
cvals[2] = "Yellow";
cvals[3] = "Black";
return 1;
/* Non-standard and Pseudo spaces */
case icmSigYData:
cvals[0] = "CIE Y";
return 2;
case icmSigLData:
cvals[0] = "CIE L*";
return 2;
case icmSigLptData:
cvals[0] = "L";
cvals[1] = "p";
cvals[2] = "t";
return 2;
default:
break;
}
return 0;
}
/* Public version of above */
/* Return the individual channel names and number of channels give a colorspace signature. */
/* Return 0 if it is not a colorspace that itself defines particular channels, */
/* 1 if it is a colorant based colorspace, and 2 if it is not a colorant based space */
ICCLIB_API unsigned int icmCSSig2chanNames(icColorSpaceSignature sig, char *cvals[]) {
return chnames_ColorSpaceSignature(sig, cvals);
}
/* ------------------------------------------------------- */
/* Flag dump functions */
/* Note - returned buffers are static, can only be used 5 */
/* times before buffers get reused. */
/* Screening Encodings */
static char *string_ScreenEncodings(unsigned int flags) {
static int si = 0; /* String buffer index */
static char buf[5][80]; /* String buffers */
char *bp, *cp;
cp = bp = buf[si++];
si %= 5; /* Rotate through buffers */
if (flags & icPrtrDefaultScreensTrue) {
sprintf(cp,"Default Screen");
} else {
sprintf(cp,"No Default Screen");
}
cp = cp + strlen(cp);
if (flags & icLinesPerInch) {
sprintf(cp,", Lines Per Inch");
} else {
sprintf(cp,", Lines Per cm");
}
cp = cp + strlen(cp);
return bp;
}
/* Device attributes */
static char *string_DeviceAttributes(unsigned int flags) {
static int si = 0; /* String buffer index */
static char buf[5][80]; /* String buffers */
char *bp, *cp;
cp = bp = buf[si++];
si %= 5; /* Rotate through buffers */
if (flags & icTransparency) {
sprintf(cp,"Transparency");
} else {
sprintf(cp,"Reflective");
}
cp = cp + strlen(cp);
if (flags & icMatte) {
sprintf(cp,", Matte");
} else {
sprintf(cp,", Glossy");
}
cp = cp + strlen(cp);
if (flags & icNegative) {
sprintf(cp,", Negative");
} else {
sprintf(cp,", Positive");
}
cp = cp + strlen(cp);
if (flags & icBlackAndWhite) {
sprintf(cp,", BlackAndWhite");
} else {
sprintf(cp,", Color");
}
cp = cp + strlen(cp);
return bp;
}
/* Profile header flags */
static char *string_ProfileHeaderFlags(unsigned int flags) {
static int si = 0; /* String buffer index */
static char buf[5][80]; /* String buffers */
char *bp, *cp;
cp = bp = buf[si++];
si %= 5; /* Rotate through buffers */
if (flags & icEmbeddedProfileTrue) {
sprintf(cp,"Embedded Profile");
} else {
sprintf(cp,"Not Embedded Profile");
}
cp = cp + strlen(cp);
if (flags & icUseWithEmbeddedDataOnly) {
sprintf(cp,", Use with embedded data only");
} else {
sprintf(cp,", Use anywhere");
}
cp = cp + strlen(cp);
return bp;
}
static char *string_AsciiOrBinaryData(unsigned int flags) {
static int si = 0; /* String buffer index */
static char buf[5][80]; /* String buffers */
char *bp, *cp;
cp = bp = buf[si++];
si %= 5; /* Rotate through buffers */
if (flags & icBinaryData) {
sprintf(cp,"Binary");
} else {
sprintf(cp,"Ascii");
}
cp = cp + strlen(cp);
return bp;
}
/* ------------------------------------------------------------ */
/* Enumeration dump functions */
/* Note - returned buffers are static, can only be used once */
/* before buffers get reused if type is unknown. */
/* public tags and sizes */
static const char *string_TagSignature(icTagSignature sig) {
static char buf[80];
switch ((int)sig) {
case icSigAToB0Tag:
return "AToB0 Multidimentional Transform";
case icSigAToB1Tag:
return "AToB1 Multidimentional Transform";
case icSigAToB2Tag:
return "AToB2 Multidimentional Transform";
case icSigBlueColorantTag:
return "Blue Colorant";
case icSigBlueTRCTag:
return "Blue Tone Reproduction Curve";
case icSigBToA0Tag:
return "BToA0 Multidimentional Transform";
case icSigBToA1Tag:
return "BToA1 Multidimentional Transform";
case icSigBToA2Tag:
return "BToA2 Multidimentional Transform";
case icSigCalibrationDateTimeTag:
return "Calibration Date & Time";
case icSigChromaticAdaptationTag:
return "Chromatic Adaptation";
case icSigCharTargetTag:
return "Characterization Target";
case icSigCopyrightTag:
return "Copyright";
case icSigCrdInfoTag:
return "CRD Info";
case icSigDeviceMfgDescTag:
return "Device Manufacturer Description";
case icSigDeviceModelDescTag:
return "Device Model Description";
case icSigGamutTag:
return "Gamut";
case icSigGrayTRCTag:
return "Gray Tone Reproduction Curve";
case icSigGreenColorantTag:
return "Green Colorant";
case icSigGreenTRCTag:
return "Green Tone Reproduction Curve";
case icSigLuminanceTag:
return "Luminance";
case icSigMeasurementTag:
return "Measurement";
case icSigMediaBlackPointTag:
return "Media Black Point";
case icSigMediaWhitePointTag:
return "Media White Point";
case icSigNamedColorTag:
return "Named Color";
case icSigNamedColor2Tag:
return "Named Color 2";
case icSigPreview0Tag:
return "Preview0";
case icSigPreview1Tag:
return "Preview1";
case icSigPreview2Tag:
return "Preview2";
case icSigProfileDescriptionTag:
return "Profile Description";
case icSigProfileSequenceDescTag:
return "Profile Sequence";
case icSigPs2CRD0Tag:
return "PS Level 2 CRD perceptual";
case icSigPs2CRD1Tag:
return "PS Level 2 CRD colorimetric";
case icSigPs2CRD2Tag:
return "PS Level 2 CRD saturation";
case icSigPs2CRD3Tag:
return "PS Level 2 CRD absolute";
case icSigPs2CSATag:
return "PS Level 2 color space array";
case icSigPs2RenderingIntentTag:
return "PS Level 2 Rendering Intent";
case icSigRedColorantTag:
return "Red Colorant";
case icSigRedTRCTag:
return "Red Tone Reproduction Curve";
case icSigScreeningDescTag:
return "Screening Description";
case icSigScreeningTag:
return "Screening Attributes";
case icSigTechnologyTag:
return "Device Technology";
case icSigUcrBgTag:
return "Under Color Removal & Black Generation";
case icSigVideoCardGammaTag:
return "Video Card Gamma Curve";
case icSigViewingCondDescTag:
return "Viewing Condition Description";
case icSigViewingConditionsTag:
return "Viewing Condition Parameters";
/* ArgyllCMS private tag: */
case icmSigAbsToRelTransSpace:
return "Absolute to Media Relative Transformation Space matrix";
default:
sprintf(buf,"Unrecognized - %s",tag2str(sig));
return buf;
}
}
/* technology signature descriptions */
static const char *string_TechnologySignature(icTechnologySignature sig) {
static char buf[80];
switch (sig) {
case icSigDigitalCamera:
return "Digital Camera";
case icSigFilmScanner:
return "Film Scanner";
case icSigReflectiveScanner:
return "Reflective Scanner";
case icSigInkJetPrinter:
return "InkJet Printer";
case icSigThermalWaxPrinter:
return "Thermal WaxPrinter";
case icSigElectrophotographicPrinter:
return "Electrophotographic Printer";
case icSigElectrostaticPrinter:
return "Electrostatic Printer";
case icSigDyeSublimationPrinter:
return "DyeSublimation Printer";
case icSigPhotographicPaperPrinter:
return "Photographic Paper Printer";
case icSigFilmWriter:
return "Film Writer";
case icSigVideoMonitor:
return "Video Monitor";
case icSigVideoCamera:
return "Video Camera";
case icSigProjectionTelevision:
return "Projection Television";
case icSigCRTDisplay:
return "Cathode Ray Tube Display";
case icSigPMDisplay:
return "Passive Matrix Display";
case icSigAMDisplay:
return "Active Matrix Display";
case icSigPhotoCD:
return "Photo CD";
case icSigPhotoImageSetter:
return "Photo ImageSetter";
case icSigGravure:
return "Gravure";
case icSigOffsetLithography:
return "Offset Lithography";
case icSigSilkscreen:
return "Silkscreen";
case icSigFlexography:
return "Flexography";
default:
sprintf(buf,"Unrecognized - %s",tag2str(sig));
return buf;
}
}
/* type signatures */
static const char *string_TypeSignature(icTagTypeSignature sig) {
static char buf[80];
switch (sig) {
case icSigCurveType:
return "Curve";
case icSigDataType:
return "Data";
case icSigDateTimeType:
return "DateTime";
case icSigLut16Type:
return "Lut16";
case icSigLut8Type:
return "Lut8";
case icSigMeasurementType:
return "Measurement";
case icSigNamedColorType:
return "Named Color";
case icSigProfileSequenceDescType:
return "Profile Sequence Desc";
case icSigS15Fixed16ArrayType:
return "S15Fixed16 Array";
case icSigScreeningType:
return "Screening";
case icSigSignatureType:
return "Signature";
case icSigTextType:
return "Text";
case icSigTextDescriptionType:
return "Text Description";
case icSigU16Fixed16ArrayType:
return "U16Fixed16 Array";
case icSigUcrBgType:
return "Under Color Removal & Black Generation";
case icSigUInt16ArrayType:
return "UInt16 Array";
case icSigUInt32ArrayType:
return "UInt32 Array";
case icSigUInt64ArrayType:
return "UInt64 Array";
case icSigUInt8ArrayType:
return "UInt8 Array";
case icSigVideoCardGammaType:
return "Video Card Gamma";
case icSigViewingConditionsType:
return "Viewing Conditions";
case icSigXYZType:
return "XYZ (Array?)";
case icSigNamedColor2Type:
return "Named Color 2";
case icSigCrdInfoType:
return "CRD Info";
default:
sprintf(buf,"Unrecognized - %s",tag2str(sig));
return buf;
}
}
/* Color Space Signatures */
static const char *string_ColorSpaceSignature(icColorSpaceSignature sig) {
static char buf[80];
switch ((int)sig) {
case icSigXYZData:
return "XYZ";
case icSigLabData:
return "Lab";
case icSigLuvData:
return "Luv";
case icSigYCbCrData:
return "YCbCr";
case icSigYxyData:
return "Yxy";
case icSigRgbData:
return "RGB";
case icSigGrayData:
return "Gray";
case icSigHsvData:
return "HSV";
case icSigHlsData:
return "HLS";
case icSigCmykData:
return "CMYK";
case icSigCmyData:
return "CMY";
case icSig2colorData:
return "2 Color";
case icSig3colorData:
return "3 Color";
case icSig4colorData:
return "4 Color";
case icSig5colorData:
case icSigMch5Data:
return "5 Color";
case icSig6colorData:
case icSigMch6Data:
return "6 Color";
case icSig7colorData:
case icSigMch7Data:
return "7 Color";
case icSig8colorData:
case icSigMch8Data:
return "8 Color";
case icSig9colorData:
return "9 Color";
case icSig10colorData:
return "10 Color";
case icSig11colorData:
return "11 Color";
case icSig12colorData:
return "12 Color";
case icSig13colorData:
return "13 Color";
case icSig14colorData:
return "14 Color";
case icSig15colorData:
return "15 Color";
/* Non-standard and Pseudo spaces */
case icmSigYData:
return "Y";
case icmSigLData:
return "L";
case icmSigL8Data:
return "L";
case icmSigLptData:
return "Lpt";
case icmSigLV2Data:
return "L";
case icmSigLV4Data:
return "L";
case icmSigPCSData:
return "PCS";
case icmSigLab8Data:
return "Lab";
case icmSigLabV2Data:
return "Lab";
case icmSigLabV4Data:
return "Lab";
default:
sprintf(buf,"Unrecognized - %s",tag2str(sig));
return buf;
}
}
#ifdef NEVER
/* Public version of above */
char *ColorSpaceSignature2str(icColorSpaceSignature sig) {
return string_ColorSpaceSignature(sig);
}
#endif
/* profileClass enumerations */
static const char *string_ProfileClassSignature(icProfileClassSignature sig) {
static char buf[80];
switch (sig) {
case icSigInputClass:
return "Input";
case icSigDisplayClass:
return "Display";
case icSigOutputClass:
return "Output";
case icSigLinkClass:
return "Link";
case icSigAbstractClass:
return "Abstract";
case icSigColorSpaceClass:
return "Color Space";
case icSigNamedColorClass:
return "Named Color";
default:
sprintf(buf,"Unrecognized - %s",tag2str(sig));
return buf;
}
}
/* Platform Signatures */
static const char *string_PlatformSignature(icPlatformSignature sig) {
static char buf[80];
switch ((int)sig) {
case icSigMacintosh:
return "Macintosh";
case icSigMicrosoft:
return "Microsoft";
case icSigSolaris:
return "Solaris";
case icSigSGI:
return "SGI";
case icSigTaligent:
return "Taligent";
case icmSig_nix:
return "*nix";
default:
sprintf(buf,"Unrecognized - %s",tag2str(sig));
return buf;
}
}
/* Measurement Geometry, used in the measurmentType tag */
static const char *string_MeasurementGeometry(icMeasurementGeometry sig) {
static char buf[30];
switch (sig) {
case icGeometryUnknown:
return "Unknown";
case icGeometry045or450:
return "0/45 or 45/0";
case icGeometry0dord0:
return "0/d or d/0";
default:
sprintf(buf,"Unrecognized - 0x%x",sig);
return buf;
}
}
/* Rendering Intents, used in the profile header */
static const char *string_RenderingIntent(icRenderingIntent sig) {
static char buf[30];
switch((int)sig) {
case icPerceptual:
return "Perceptual";
case icRelativeColorimetric:
return "Relative Colorimetric";
case icSaturation:
return "Saturation";
case icAbsoluteColorimetric:
return "Absolute Colorimetric";
case icmAbsolutePerceptual: /* icclib specials */
return "Absolute Perceptual";
case icmAbsoluteSaturation: /* icclib specials */
return "Absolute Saturation";
case icmDefaultIntent: /* icclib specials */
return "Default Intent";
default:
sprintf(buf,"Unrecognized - 0x%x",sig);
return buf;
}
}
/* Transform Lookup function */
static const char *string_LookupFunc(icmLookupFunc sig) {
static char buf[30];
switch(sig) {
case icmFwd:
return "Forward";
case icmBwd:
return "Backward";
case icmGamut:
return "Gamut";
case icmPreview:
return "Preview";
default:
sprintf(buf,"Unrecognized - 0x%x",sig);
return buf;
}
}
/* Different Spot Shapes currently defined, used for screeningType */
static const char *string_SpotShape(icSpotShape sig) {
static char buf[30];
switch(sig) {
case icSpotShapeUnknown:
return "Unknown";
case icSpotShapePrinterDefault:
return "Printer Default";
case icSpotShapeRound:
return "Round";
case icSpotShapeDiamond:
return "Diamond";
case icSpotShapeEllipse:
return "Ellipse";
case icSpotShapeLine:
return "Line";
case icSpotShapeSquare:
return "Square";
case icSpotShapeCross:
return "Cross";
default:
sprintf(buf,"Unrecognized - 0x%x",sig);
return buf;
}
}
/* Standard Observer, used in the measurmentType tag */
static const char *string_StandardObserver(icStandardObserver sig) {
static char buf[30];
switch(sig) {
case icStdObsUnknown:
return "Unknown";
case icStdObs1931TwoDegrees:
return "1931 Two Degrees";
case icStdObs1964TenDegrees:
return "1964 Ten Degrees";
default:
sprintf(buf,"Unrecognized - 0x%x",sig);
return buf;
}
}
/* Pre-defined illuminants, used in measurement and viewing conditions type */
static const char *string_Illuminant(icIlluminant sig) {
static char buf[30];
switch(sig) {
case icIlluminantUnknown:
return "Unknown";
case icIlluminantD50:
return "D50";
case icIlluminantD65:
return "D65";
case icIlluminantD93:
return "D93";
case icIlluminantF2:
return "F2";
case icIlluminantD55:
return "D55";
case icIlluminantA:
return "A";
case icIlluminantEquiPowerE:
return "Equi-Power(E)";
case icIlluminantF8:
return "F8";
default:
sprintf(buf,"Unrecognized - 0x%x",sig);
return buf;
}
}
/* Return a text abreviation of a color lookup algorithm */
static const char *string_LuAlg(icmLuAlgType alg) {
static char buf[80];
switch(alg) {
case icmMonoFwdType:
return "MonoFwd";
case icmMonoBwdType:
return "MonoBwd";
case icmMatrixFwdType:
return "MatrixFwd";
case icmMatrixBwdType:
return "MatrixBwd";
case icmLutType:
return "Lut";
default:
sprintf(buf,"Unrecognized - %d",alg);
return buf;
}
}
/* Return a string description of the given enumeration value */
/* Public: */
const char *icm2str(icmEnumType etype, int enumval) {
switch(etype) {
case icmScreenEncodings:
return string_ScreenEncodings((unsigned int) enumval);
case icmDeviceAttributes:
return string_DeviceAttributes((unsigned int) enumval);
case icmProfileHeaderFlags:
return string_ProfileHeaderFlags((unsigned int) enumval);
case icmAsciiOrBinaryData:
return string_AsciiOrBinaryData((unsigned int) enumval);
case icmTagSignature:
return string_TagSignature((icTagSignature) enumval);
case icmTechnologySignature:
return string_TechnologySignature((icTechnologySignature) enumval);
case icmTypeSignature:
return string_TypeSignature((icTagTypeSignature) enumval);
case icmColorSpaceSignature:
return string_ColorSpaceSignature((icColorSpaceSignature) enumval);
case icmProfileClassSignature:
return string_ProfileClassSignature((icProfileClassSignature) enumval);
case icmPlatformSignature:
return string_PlatformSignature((icPlatformSignature) enumval);
case icmMeasurementGeometry:
return string_MeasurementGeometry((icMeasurementGeometry) enumval);
case icmRenderingIntent:
return string_RenderingIntent((icRenderingIntent) enumval);
case icmTransformLookupFunc:
return string_LookupFunc((icmLookupFunc) enumval);
case icmSpotShape:
return string_SpotShape((icSpotShape) enumval);
case icmStandardObserver:
return string_StandardObserver((icStandardObserver) enumval);
case icmIlluminant:
return string_Illuminant((icIlluminant) enumval);
case icmLuAlg:
return string_LuAlg((icmLuAlgType) enumval);
default:
return "enum2str got unknown type";
}
}
/* ========================================================== */
/* Object I/O routines */
/* ========================================================== */
/* icmUnknown object */
/* Return the number of bytes needed to write this tag */
static unsigned int icmUnknown_get_size(
icmBase *pp
) {
icmUnknown *p = (icmUnknown *)pp;
unsigned int len = 0;
len = sat_add(len, 8); /* 8 bytes for tag and padding */
len = sat_addmul(len, p->size, 1); /* 1 byte for each unknown data */
return len;
}
/* read the object, return 0 on success, error code on fail */
static int icmUnknown_read(
icmBase *pp,
unsigned int len, /* tag length */
unsigned int of /* start offset within file */
) {
icmUnknown *p = (icmUnknown *)pp;
icc *icp = p->icp;
int rv = 0;
unsigned int i, size;
char *bp, *buf;
if (len < 8) {
sprintf(icp->err,"icmUnknown_read: Tag too small to be legal");
return icp->errc = 1;
}
/* Allocate a file read buffer */
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmUnknown_read: malloc() failed");
return icp->errc = 2;
}
bp = buf;
/* Read portion of file into buffer */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->read(icp->fp, bp, 1, len) != len) {
sprintf(icp->err,"icmUnknown_read: fseek() or fread() failed");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
p->size = size = (len - 8)/1; /* Number of elements in the array */
if ((rv = p->allocate((icmBase *)p)) != 0) {
icp->al->free(icp->al, buf);
return rv;
}
/* Read type descriptor from the buffer */
p->uttype = (icTagTypeSignature)read_SInt32Number(bp);
bp += 8; /* Skip padding */
/* Read all the data from the buffer */
for (i = 0; i < size; i++, bp += 1) {
p->data[i] = read_UInt8Number(bp);
}
icp->al->free(p->icp->al, buf);
return 0;
}
/* Write the contents of the object. Return 0 on sucess, error code on failure */
static int icmUnknown_write(
icmBase *pp,
unsigned int of /* File offset to write from */
) {
icmUnknown *p = (icmUnknown *)pp;
icc *icp = p->icp;
unsigned int i;
unsigned int len;
char *bp, *buf; /* Buffer to write from */
int rv = 0;
/* Allocate a file write buffer */
if ((len = p->get_size((icmBase *)p)) == UINT_MAX) {
sprintf(icp->err,"icmUnknown_write get_size overflow");
return icp->errc = 1;
}
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmUnknown_write malloc() failed");
return icp->errc = 2;
}
bp = buf;
/* Write type descriptor to the buffer */
if ((rv = write_SInt32Number((int)p->uttype,bp)) != 0) {
sprintf(icp->err,"icmUnknown_write: write_SInt32Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
write_SInt32Number(0,bp+4); /* Set padding to 0 */
bp += 8; /* Skip padding */
/* Write all the data to the buffer */
for (i = 0; i < p->size; i++, bp += 1) {
if ((rv = write_UInt8Number(p->data[i],bp)) != 0) {
sprintf(icp->err,"icmUnknown_write: write_UInt8umber() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
}
/* Write to the file */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->write(icp->fp, buf, 1, len) != len) {
sprintf(icp->err,"icmUnknown_write fseek() or fwrite() failed");
icp->al->free(icp->al, buf);
return icp->errc = 2;
}
icp->al->free(icp->al, buf);
return 0;
}
/* Dump a text description of the object */
static void icmUnknown_dump(
icmBase *pp,
icmFile *op, /* Output to dump to */
int verb /* Verbosity level */
) {
icmUnknown *p = (icmUnknown *)pp;
unsigned int i, ii, r, ph;
if (verb <= 1)
return;
op->gprintf(op,"Unknown:\n");
op->gprintf(op," Payload size in bytes = %u\n",p->size);
/* Print one row of binary and ASCII interpretation if verb == 2, All if == 3 */
/* else print all of it. */
ii = i = ph = 0;
for (r = 1;; r++) { /* count rows */
int c = 1; /* Character location */
c = 1;
if (ph != 0) { /* Print ASCII under binary */
op->gprintf(op," ");
i = ii; /* Swap */
c += 12;
} else {
op->gprintf(op," 0x%04lx: ",i);
ii = i; /* Swap */
c += 12;
}
while (i < p->size && c < 60) {
if (ph == 0)
op->gprintf(op,"%02x ",p->data[i]);
else {
if (isprint(p->data[i]))
op->gprintf(op,"%c ",p->data[i]);
else
op->gprintf(op," ",p->data[i]);
}
c += 3;
i++;
}
if (ph == 0 || i < p->size)
op->gprintf(op,"\n");
if (ph == 1 && i >= p->size) {
op->gprintf(op,"\n");
break;
}
if (ph == 1 && r > 1 && verb < 3) {
op->gprintf(op," ...\n");
break; /* Print 1 row if not verbose */
}
if (ph == 0)
ph = 1;
else
ph = 0;
}
}
/* Allocate variable sized data elements */
static int icmUnknown_allocate(
icmBase *pp
) {
icmUnknown *p = (icmUnknown *)pp;
icc *icp = p->icp;
if (p->size != p->_size) {
if (ovr_mul(p->size, sizeof(unsigned char))) {
sprintf(icp->err,"icmUnknown_alloc: size overflow");
return icp->errc = 1;
}
if (p->data != NULL)
icp->al->free(icp->al, p->data);
if ((p->data = (unsigned char *) icp->al->calloc(icp->al, p->size, sizeof(unsigned char)))
== NULL) {
sprintf(icp->err,"icmUnknown_alloc: malloc() of icmUnknown data failed");
return icp->errc = 2;
}
p->_size = p->size;
}
return 0;
}
/* Free all storage in the object */
static void icmUnknown_delete(
icmBase *pp
) {
icmUnknown *p = (icmUnknown *)pp;
icc *icp = p->icp;
if (p->data != NULL)
icp->al->free(icp->al, p->data);
icp->al->free(icp->al, p);
}
/* Create an empty object. Return null on error */
static icmBase *new_icmUnknown(
icc *icp
) {
icmUnknown *p;
if ((p = (icmUnknown *) icp->al->calloc(icp->al,1,sizeof(icmUnknown))) == NULL)
return NULL;
p->ttype = icmSigUnknownType;
p->uttype = icmSigUnknownType;
p->refcount = 1;
p->get_size = icmUnknown_get_size;
p->read = icmUnknown_read;
p->write = icmUnknown_write;
p->dump = icmUnknown_dump;
p->allocate = icmUnknown_allocate;
p->del = icmUnknown_delete;
p->icp = icp;
return (icmBase *)p;
}
/* ---------------------------------------------------------- */
/* icmUInt8Array object */
/* Return the number of bytes needed to write this tag */
static unsigned int icmUInt8Array_get_size(
icmBase *pp
) {
icmUInt8Array *p = (icmUInt8Array *)pp;
unsigned int len = 0;
len = sat_add(len, 8); /* 8 bytes for tag and padding */
len = sat_addmul(len, p->size, 1); /* 1 byte for each UInt8 */
return len;
}
/* read the object, return 0 on success, error code on fail */
static int icmUInt8Array_read(
icmBase *pp,
unsigned int len, /* tag length */
unsigned int of /* start offset within file */
) {
icmUInt8Array *p = (icmUInt8Array *)pp;
icc *icp = p->icp;
int rv = 0;
unsigned int i, size;
char *bp, *buf;
if (len < 8) {
sprintf(icp->err,"icmUInt8Array_read: Tag too small to be legal");
return icp->errc = 1;
}
/* Allocate a file read buffer */
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmUInt8Array_read: malloc() failed");
return icp->errc = 2;
}
bp = buf;
/* Read portion of file into buffer */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->read(icp->fp, bp, 1, len) != len) {
sprintf(icp->err,"icmUInt8Array_read: fseek() or fread() failed");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
p->size = size = (len - 8)/1; /* Number of elements in the array */
if ((rv = p->allocate((icmBase *)p)) != 0) {
icp->al->free(icp->al, buf);
return rv;
}
/* Read type descriptor from the buffer */
if (((icTagTypeSignature)read_SInt32Number(bp)) != p->ttype) {
icp->al->free(icp->al, buf);
sprintf(icp->err,"icmUInt8Array_read: Wrong tag type for icmUInt8Array");
return icp->errc = 1;
}
bp += 8; /* Skip padding */
/* Read all the data from the buffer */
for (i = 0; i < size; i++, bp += 1) {
p->data[i] = read_UInt8Number(bp);
}
icp->al->free(p->icp->al, buf);
return 0;
}
/* Write the contents of the object. Return 0 on sucess, error code on failure */
static int icmUInt8Array_write(
icmBase *pp,
unsigned int of /* File offset to write from */
) {
icmUInt8Array *p = (icmUInt8Array *)pp;
icc *icp = p->icp;
unsigned int i;
unsigned int len;
char *bp, *buf; /* Buffer to write from */
int rv = 0;
/* Allocate a file write buffer */
if ((len = p->get_size((icmBase *)p)) == UINT_MAX) {
sprintf(icp->err,"icmUInt8Array_write get_size overflow");
return icp->errc = 1;
}
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmUInt8Array_write malloc() failed");
return icp->errc = 2;
}
bp = buf;
/* Write type descriptor to the buffer */
if ((rv = write_SInt32Number((int)p->ttype,bp)) != 0) {
sprintf(icp->err,"icmUInt8Array_write: write_SInt32Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
write_SInt32Number(0,bp+4); /* Set padding to 0 */
bp += 8; /* Skip padding */
/* Write all the data to the buffer */
for (i = 0; i < p->size; i++, bp += 1) {
if ((rv = write_UInt8Number(p->data[i],bp)) != 0) {
sprintf(icp->err,"icmUInt8Array_write: write_UInt8umber() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
}
/* Write to the file */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->write(icp->fp, buf, 1, len) != len) {
sprintf(icp->err,"icmUInt8Array_write fseek() or fwrite() failed");
icp->al->free(icp->al, buf);
return icp->errc = 2;
}
icp->al->free(icp->al, buf);
return 0;
}
/* Dump a text description of the object */
static void icmUInt8Array_dump(
icmBase *pp,
icmFile *op, /* Output to dump to */
int verb /* Verbosity level */
) {
icmUInt8Array *p = (icmUInt8Array *)pp;
if (verb <= 0)
return;
op->gprintf(op,"UInt8Array:\n");
op->gprintf(op," No. elements = %lu\n",p->size);
if (verb >= 2) {
unsigned int i;
for (i = 0; i < p->size; i++)
op->gprintf(op," %lu: %u\n",i,p->data[i]);
}
}
/* Allocate variable sized data elements */
static int icmUInt8Array_allocate(
icmBase *pp
) {
icmUInt8Array *p = (icmUInt8Array *)pp;
icc *icp = p->icp;
if (p->size != p->_size) {
if (ovr_mul(p->size, sizeof(unsigned int))) {
sprintf(icp->err,"icmUInt8Array_alloc: size overflow");
return icp->errc = 1;
}
if (p->data != NULL)
icp->al->free(icp->al, p->data);
if ((p->data = (unsigned int *) icp->al->calloc(icp->al, p->size, sizeof(unsigned int)))
== NULL) {
sprintf(icp->err,"icmUInt8Array_alloc: malloc() of icmUInt8Array data failed");
return icp->errc = 2;
}
p->_size = p->size;
}
return 0;
}
/* Free all storage in the object */
static void icmUInt8Array_delete(
icmBase *pp
) {
icmUInt8Array *p = (icmUInt8Array *)pp;
icc *icp = p->icp;
if (p->data != NULL)
icp->al->free(icp->al, p->data);
icp->al->free(icp->al, p);
}
/* Create an empty object. Return null on error */
static icmBase *new_icmUInt8Array(
icc *icp
) {
icmUInt8Array *p;
if ((p = (icmUInt8Array *) icp->al->calloc(icp->al,1,sizeof(icmUInt8Array))) == NULL)
return NULL;
p->ttype = icSigUInt8ArrayType;
p->refcount = 1;
p->get_size = icmUInt8Array_get_size;
p->read = icmUInt8Array_read;
p->write = icmUInt8Array_write;
p->dump = icmUInt8Array_dump;
p->allocate = icmUInt8Array_allocate;
p->del = icmUInt8Array_delete;
p->icp = icp;
return (icmBase *)p;
}
/* ---------------------------------------------------------- */
/* icmUInt16Array object */
/* Return the number of bytes needed to write this tag */
static unsigned int icmUInt16Array_get_size(
icmBase *pp
) {
icmUInt16Array *p = (icmUInt16Array *)pp;
unsigned int len = 0;
len = sat_add(len, 8); /* 8 bytes for tag and padding */
len = sat_addmul(len, p->size, 2); /* 2 bytes for each UInt16 */
return len;
}
/* read the object, return 0 on success, error code on fail */
static int icmUInt16Array_read(
icmBase *pp,
unsigned int len, /* tag length */
unsigned int of /* start offset within file */
) {
icmUInt16Array *p = (icmUInt16Array *)pp;
icc *icp = p->icp;
int rv = 0;
unsigned int i, size;
char *bp, *buf;
if (len < 8) {
sprintf(icp->err,"icmUInt16Array_read: Tag too small to be legal");
return icp->errc = 1;
}
/* Allocate a file read buffer */
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmUInt16Array_read: malloc() failed");
return icp->errc = 2;
}
bp = buf;
/* Read portion of file into buffer */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->read(icp->fp, bp, 1, len) != len) {
sprintf(icp->err,"icmUInt16Array_read: fseek() or fread() failed");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
p->size = size = (len - 8)/2; /* Number of elements in the array */
if ((rv = p->allocate((icmBase *)p)) != 0) {
icp->al->free(icp->al, buf);
return rv;
}
/* Read type descriptor from the buffer */
if (((icTagTypeSignature)read_SInt32Number(bp)) != p->ttype) {
sprintf(icp->err,"icmUInt16Array_read: Wrong tag type for icmUInt16Array");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
bp += 8; /* Skip padding */
/* Read all the data from the buffer */
for (i = 0; i < size; i++, bp += 2) {
p->data[i] = read_UInt16Number(bp);
}
icp->al->free(icp->al, buf);
return 0;
}
/* Write the contents of the object. Return 0 on sucess, error code on failure */
static int icmUInt16Array_write(
icmBase *pp,
unsigned int of /* File offset to write from */
) {
icmUInt16Array *p = (icmUInt16Array *)pp;
icc *icp = p->icp;
unsigned int i;
unsigned int len;
char *bp, *buf; /* Buffer to write from */
int rv = 0;
/* Allocate a file write buffer */
if ((len = p->get_size((icmBase *)p)) == UINT_MAX) {
sprintf(icp->err,"icmUInt16Array_write get_size overflow");
return icp->errc = 1;
}
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmUInt16Array_write malloc() failed");
return icp->errc = 2;
}
bp = buf;
/* Write type descriptor to the buffer */
if ((rv = write_SInt32Number((int)p->ttype,bp)) != 0) {
sprintf(icp->err,"icmUInt16Array_write: write_SInt32Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
write_SInt32Number(0,bp+4); /* Set padding to 0 */
/* Write all the data to the buffer */
bp += 8; /* Skip padding */
for (i = 0; i < p->size; i++, bp += 2) {
if ((rv = write_UInt16Number(p->data[i],bp)) != 0) {
sprintf(icp->err,"icmUInt16Array_write: write_UInt16umber() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
}
/* Write to the file */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->write(icp->fp, buf, 1, len) != len) {
sprintf(icp->err,"icmUInt16Array_write fseek() or fwrite() failed");
icp->al->free(icp->al, buf);
return icp->errc = 2;
}
icp->al->free(icp->al, buf);
return 0;
}
/* Dump a text description of the object */
static void icmUInt16Array_dump(
icmBase *pp,
icmFile *op, /* Output to dump to */
int verb /* Verbosity level */
) {
icmUInt16Array *p = (icmUInt16Array *)pp;
if (verb <= 0)
return;
op->gprintf(op,"UInt16Array:\n");
op->gprintf(op," No. elements = %lu\n",p->size);
if (verb >= 2) {
unsigned int i;
for (i = 0; i < p->size; i++)
op->gprintf(op," %lu: %u\n",i,p->data[i]);
}
}
/* Allocate variable sized data elements */
static int icmUInt16Array_allocate(
icmBase *pp
) {
icmUInt16Array *p = (icmUInt16Array *)pp;
icc *icp = p->icp;
if (p->size != p->_size) {
if (ovr_mul(p->size, sizeof(unsigned int))) {
sprintf(icp->err,"icmUInt16Array_alloc:: size overflow");
return icp->errc = 1;
}
if (p->data != NULL)
icp->al->free(icp->al, p->data);
if ((p->data = (unsigned int *) icp->al->calloc(icp->al, p->size, sizeof(unsigned int)))
== NULL) {
sprintf(icp->err,"icmUInt16Array_alloc: malloc() of icmUInt16Array data failed");
return icp->errc = 2;
}
p->_size = p->size;
}
return 0;
}
/* Free all storage in the object */
static void icmUInt16Array_delete(
icmBase *pp
) {
icmUInt16Array *p = (icmUInt16Array *)pp;
icc *icp = p->icp;
if (p->data != NULL)
icp->al->free(icp->al, p->data);
icp->al->free(icp->al, p);
}
/* Create an empty object. Return null on error */
static icmBase *new_icmUInt16Array(
icc *icp
) {
icmUInt16Array *p;
if ((p = (icmUInt16Array *) icp->al->calloc(icp->al,1,sizeof(icmUInt16Array))) == NULL)
return NULL;
p->ttype = icSigUInt16ArrayType;
p->refcount = 1;
p->get_size = icmUInt16Array_get_size;
p->read = icmUInt16Array_read;
p->write = icmUInt16Array_write;
p->dump = icmUInt16Array_dump;
p->allocate = icmUInt16Array_allocate;
p->del = icmUInt16Array_delete;
p->icp = icp;
return (icmBase *)p;
}
/* ---------------------------------------------------------- */
/* icmUInt32Array object */
/* Return the number of bytes needed to write this tag */
static unsigned int icmUInt32Array_get_size(
icmBase *pp
) {
icmUInt32Array *p = (icmUInt32Array *)pp;
unsigned int len = 0;
len = sat_add(len, 8); /* 8 bytes for tag and padding */
len = sat_addmul(len, p->size, 4); /* 4 bytes for each UInt32 */
return len;
}
/* read the object, return 0 on success, error code on fail */
static int icmUInt32Array_read(
icmBase *pp,
unsigned int len, /* tag length */
unsigned int of /* start offset within file */
) {
icmUInt32Array *p = (icmUInt32Array *)pp;
icc *icp = p->icp;
int rv = 0;
unsigned int i, size;
char *bp, *buf;
if (len < 8) {
sprintf(icp->err,"icmUInt32Array_read: Tag too small to be legal");
return icp->errc = 1;
}
/* Allocate a file read buffer */
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmUInt32Array_read: malloc() failed");
return icp->errc = 2;
}
bp = buf;
/* Read portion of file into buffer */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->read(icp->fp, bp, 1, len) != len) {
sprintf(icp->err,"icmUInt32Array_read: fseek() or fread() failed");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
p->size = size = (len - 8)/4; /* Number of elements in the array */
if ((rv = p->allocate((icmBase *)p)) != 0) {
icp->al->free(icp->al, buf);
return rv;
}
/* Read type descriptor from the buffer */
if (((icTagTypeSignature)read_SInt32Number(bp)) != p->ttype) {
sprintf(icp->err,"icmUInt32Array_read: Wrong tag type for icmUInt32Array");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
bp += 8; /* Skip padding */
/* Read all the data from the buffer */
for (i = 0; i < size; i++, bp += 4) {
p->data[i] = read_UInt32Number(bp);
}
icp->al->free(icp->al, buf);
return 0;
}
/* Write the contents of the object. Return 0 on sucess, error code on failure */
static int icmUInt32Array_write(
icmBase *pp,
unsigned int of /* File offset to write from */
) {
icmUInt32Array *p = (icmUInt32Array *)pp;
icc *icp = p->icp;
unsigned int i;
unsigned int len;
char *bp, *buf; /* Buffer to write from */
int rv = 0;
/* Allocate a file write buffer */
if ((len = p->get_size((icmBase *)p)) == UINT_MAX) {
sprintf(icp->err,"icmUInt32Array_write get_size overflow");
return icp->errc = 1;
}
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmUInt32Array_write malloc() failed");
return icp->errc = 2;
}
bp = buf;
/* Write type descriptor to the buffer */
if ((rv = write_SInt32Number((int)p->ttype,bp)) != 0) {
sprintf(icp->err,"icmUInt32Array_write: write_SInt32Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
write_SInt32Number(0,bp+4); /* Set padding to 0 */
/* Write all the data to the buffer */
bp += 8; /* Skip padding */
for (i = 0; i < p->size; i++, bp += 4) {
if ((rv = write_UInt32Number(p->data[i],bp)) != 0) {
sprintf(icp->err,"icmUInt32Array_write: write_UInt32umber() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
}
/* Write to the file */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->write(icp->fp, buf, 1, len) != len) {
sprintf(icp->err,"icmUInt32Array_write fseek() or fwrite() failed");
icp->al->free(icp->al, buf);
return icp->errc = 2;
}
icp->al->free(icp->al, buf);
return 0;
}
/* Dump a text description of the object */
static void icmUInt32Array_dump(
icmBase *pp,
icmFile *op, /* Output to dump to */
int verb /* Verbosity level */
) {
icmUInt32Array *p = (icmUInt32Array *)pp;
if (verb <= 0)
return;
op->gprintf(op,"UInt32Array:\n");
op->gprintf(op," No. elements = %lu\n",p->size);
if (verb >= 2) {
unsigned int i;
for (i = 0; i < p->size; i++)
op->gprintf(op," %lu: %u\n",i,p->data[i]);
}
}
/* Allocate variable sized data elements */
static int icmUInt32Array_allocate(
icmBase *pp
) {
icmUInt32Array *p = (icmUInt32Array *)pp;
icc *icp = p->icp;
if (p->size != p->_size) {
if (ovr_mul(p->size, sizeof(unsigned int))) {
sprintf(icp->err,"icmUInt32Array_alloc: size overflow");
return icp->errc = 1;
}
if (p->data != NULL)
icp->al->free(icp->al, p->data);
if ((p->data = (unsigned int *) icp->al->calloc(icp->al, p->size, sizeof(unsigned int)))
== NULL) {
sprintf(icp->err,"icmUInt32Array_alloc: malloc() of icmUInt32Array data failed");
return icp->errc = 2;
}
p->_size = p->size;
}
return 0;
}
/* Free all storage in the object */
static void icmUInt32Array_delete(
icmBase *pp
) {
icmUInt32Array *p = (icmUInt32Array *)pp;
icc *icp = p->icp;
if (p->data != NULL)
icp->al->free(icp->al, p->data);
icp->al->free(icp->al, p);
}
/* Create an empty object. Return null on error */
static icmBase *new_icmUInt32Array(
icc *icp
) {
icmUInt32Array *p;
if ((p = (icmUInt32Array *) icp->al->calloc(icp->al,1,sizeof(icmUInt32Array))) == NULL)
return NULL;
p->ttype = icSigUInt32ArrayType;
p->refcount = 1;
p->get_size = icmUInt32Array_get_size;
p->read = icmUInt32Array_read;
p->write = icmUInt32Array_write;
p->dump = icmUInt32Array_dump;
p->allocate = icmUInt32Array_allocate;
p->del = icmUInt32Array_delete;
p->icp = icp;
return (icmBase *)p;
}
/* ---------------------------------------------------------- */
/* icmUInt64Array object */
/* Return the number of bytes needed to write this tag */
static unsigned int icmUInt64Array_get_size(
icmBase *pp
) {
icmUInt64Array *p = (icmUInt64Array *)pp;
unsigned int len = 0;
len = sat_add(len, 8); /* 8 bytes for tag and padding */
len = sat_addmul(len, p->size, 8); /* 8 bytes for each UInt64 */
return len;
}
/* read the object, return 0 on success, error code on fail */
static int icmUInt64Array_read(
icmBase *pp,
unsigned int len, /* tag length */
unsigned int of /* start offset within file */
) {
icmUInt64Array *p = (icmUInt64Array *)pp;
icc *icp = p->icp;
int rv = 0;
unsigned int i, size;
char *bp, *buf;
if (len < 8) {
sprintf(icp->err,"icmUInt64Array_read: Tag too small to be legal");
return icp->errc = 1;
}
/* Allocate a file read buffer */
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmUInt64Array_read: malloc() failed");
return icp->errc = 2;
}
bp = buf;
/* Read portion of file into buffer */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->read(icp->fp, bp, 1, len) != len) {
sprintf(icp->err,"icmUInt64Array_read: fseek() or fread() failed");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
p->size = size = (len - 8)/8; /* Number of elements in the array */
if ((rv = p->allocate((icmBase *)p)) != 0) {
icp->al->free(icp->al, buf);
return rv;
}
/* Read type descriptor from the buffer */
if (((icTagTypeSignature)read_SInt32Number(bp)) != p->ttype) {
sprintf(icp->err,"icmUInt64Array_read: Wrong tag type for icmUInt64Array");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
bp += 8; /* Skip padding */
/* Read all the data from the buffer */
for (i = 0; i < size; i++, bp += 8) {
read_UInt64Number(&p->data[i], bp);
}
icp->al->free(icp->al, buf);
return 0;
}
/* Write the contents of the object. Return 0 on sucess, error code on failure */
static int icmUInt64Array_write(
icmBase *pp,
unsigned int of /* File offset to write from */
) {
icmUInt64Array *p = (icmUInt64Array *)pp;
icc *icp = p->icp;
unsigned int i;
unsigned int len;
char *bp, *buf; /* Buffer to write from */
int rv = 0;
/* Allocate a file write buffer */
if ((len = p->get_size((icmBase *)p)) == UINT_MAX) {
sprintf(icp->err,"icmUInt64Array_write get_size overflow");
return icp->errc = 1;
}
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmUInt64Array_write malloc() failed");
return icp->errc = 2;
}
bp = buf;
/* Write type descriptor to the buffer */
if ((rv = write_SInt32Number((int)p->ttype,bp)) != 0) {
sprintf(icp->err,"icmUInt64Array_write: write_SInt32Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
write_SInt32Number(0,bp+4); /* Set padding to 0 */
/* Write all the data to the buffer */
bp += 8; /* Skip padding */
for (i = 0; i < p->size; i++, bp += 8) {
if ((rv = write_UInt64Number(&p->data[i],bp)) != 0) {
sprintf(icp->err,"icmUInt64Array_write: write_UInt64umber() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
}
/* Write to the file */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->write(icp->fp, buf, 1, len) != len) {
sprintf(icp->err,"icmUInt64Array_write fseek() or fwrite() failed");
icp->al->free(icp->al, buf);
return icp->errc = 2;
}
icp->al->free(icp->al, buf);
return 0;
}
/* Dump a text description of the object */
static void icmUInt64Array_dump(
icmBase *pp,
icmFile *op, /* Output to dump to */
int verb /* Verbosity level */
) {
icmUInt64Array *p = (icmUInt64Array *)pp;
if (verb <= 0)
return;
op->gprintf(op,"UInt64Array:\n");
op->gprintf(op," No. elements = %lu\n",p->size);
if (verb >= 2) {
unsigned int i;
for (i = 0; i < p->size; i++)
op->gprintf(op," %lu: h=%lu, l=%lu\n",i,p->data[i].h,p->data[i].l);
}
}
/* Allocate variable sized data elements */
static int icmUInt64Array_allocate(
icmBase *pp
) {
icmUInt64Array *p = (icmUInt64Array *)pp;
icc *icp = p->icp;
if (p->size != p->_size) {
if (ovr_mul(p->size, sizeof(icmUint64))) {
sprintf(icp->err,"icmUInt64Array_alloc: size overflow");
return icp->errc = 1;
}
if (p->data != NULL)
icp->al->free(icp->al, p->data);
if ((p->data = (icmUint64 *) icp->al->calloc(icp->al, p->size, sizeof(icmUint64)))
== NULL) {
sprintf(icp->err,"icmUInt64Array_alloc: malloc() of icmUInt64Array data failed");
return icp->errc = 2;
}
p->_size = p->size;
}
return 0;
}
/* Free all storage in the object */
static void icmUInt64Array_delete(
icmBase *pp
) {
icmUInt64Array *p = (icmUInt64Array *)pp;
icc *icp = p->icp;
if (p->data != NULL)
icp->al->free(icp->al, p->data);
icp->al->free(icp->al, p);
}
/* Create an empty object. Return null on error */
static icmBase *new_icmUInt64Array(
icc *icp
) {
icmUInt64Array *p;
if ((p = (icmUInt64Array *) icp->al->calloc(icp->al,1,sizeof(icmUInt64Array))) == NULL)
return NULL;
p->ttype = icSigUInt64ArrayType;
p->refcount = 1;
p->get_size = icmUInt64Array_get_size;
p->read = icmUInt64Array_read;
p->write = icmUInt64Array_write;
p->dump = icmUInt64Array_dump;
p->allocate = icmUInt64Array_allocate;
p->del = icmUInt64Array_delete;
p->icp = icp;
return (icmBase *)p;
}
/* ---------------------------------------------------------- */
/* icmU16Fixed16Array object */
/* Return the number of bytes needed to write this tag */
static unsigned int icmU16Fixed16Array_get_size(
icmBase *pp
) {
icmU16Fixed16Array *p = (icmU16Fixed16Array *)pp;
unsigned int len = 0;
len = sat_add(len, 8); /* 8 bytes for tag and padding */
len = sat_addmul(len, p->size, 4); /* 4 byte for each U16Fixed16 */
return len;
}
/* read the object, return 0 on success, error code on fail */
static int icmU16Fixed16Array_read(
icmBase *pp,
unsigned int len, /* tag length */
unsigned int of /* start offset within file */
) {
icmU16Fixed16Array *p = (icmU16Fixed16Array *)pp;
icc *icp = p->icp;
int rv = 0;
unsigned int i, size;
char *bp, *buf;
if (len < 8) {
sprintf(icp->err,"icmU16Fixed16Array_read: Tag too small to be legal");
return icp->errc = 1;
}
/* Allocate a file read buffer */
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmU16Fixed16Array_read: malloc() failed");
return icp->errc = 2;
}
bp = buf;
/* Read portion of file into buffer */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->read(icp->fp, bp, 1, len) != len) {
sprintf(icp->err,"icmU16Fixed16Array_read: fseek() or fread() failed");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
p->size = size = (len - 8)/4; /* Number of elements in the array */
if ((rv = p->allocate((icmBase *)p)) != 0) {
icp->al->free(icp->al, buf);
return rv;
}
/* Read type descriptor from the buffer */
if (((icTagTypeSignature)read_SInt32Number(bp)) != p->ttype) {
sprintf(icp->err,"icmU16Fixed16Array_read: Wrong tag type for icmU16Fixed16Array");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
bp += 8; /* Skip padding */
/* Read all the data from the buffer */
for (i = 0; i < size; i++, bp += 4) {
p->data[i] = read_U16Fixed16Number(bp);
}
icp->al->free(icp->al, buf);
return 0;
}
/* Write the contents of the object. Return 0 on sucess, error code on failure */
static int icmU16Fixed16Array_write(
icmBase *pp,
unsigned int of /* File offset to write from */
) {
icmU16Fixed16Array *p = (icmU16Fixed16Array *)pp;
icc *icp = p->icp;
unsigned int i;
unsigned int len;
char *bp, *buf; /* Buffer to write from */
int rv = 0;
/* Allocate a file write buffer */
if ((len = p->get_size((icmBase *)p)) == UINT_MAX) {
sprintf(icp->err,"icmU16Fixed16Array_write get_size overflow");
return icp->errc = 1;
}
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmU16Fixed16Array_write malloc() failed");
return icp->errc = 2;
}
bp = buf;
/* Write type descriptor to the buffer */
if ((rv = write_SInt32Number((int)p->ttype,bp)) != 0) {
sprintf(icp->err,"icmU16Fixed16Array_write: write_SInt32Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
write_SInt32Number(0,bp+4); /* Set padding to 0 */
/* Write all the data to the buffer */
bp += 8; /* Skip padding */
for (i = 0; i < p->size; i++, bp += 4) {
if ((rv = write_U16Fixed16Number(p->data[i],bp)) != 0) {
sprintf(icp->err,"icmU16Fixed16Array_write: write_U16Fixed16umber() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
}
/* Write to the file */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->write(icp->fp, buf, 1, len) != len) {
sprintf(icp->err,"icmU16Fixed16Array_write fseek() or fwrite() failed");
icp->al->free(icp->al, buf);
return icp->errc = 2;
}
icp->al->free(icp->al, buf);
return 0;
}
/* Dump a text description of the object */
static void icmU16Fixed16Array_dump(
icmBase *pp,
icmFile *op, /* Output to dump to */
int verb /* Verbosity level */
) {
icmU16Fixed16Array *p = (icmU16Fixed16Array *)pp;
if (verb <= 0)
return;
op->gprintf(op,"U16Fixed16Array:\n");
op->gprintf(op," No. elements = %lu\n",p->size);
if (verb >= 2) {
unsigned int i;
for (i = 0; i < p->size; i++)
op->gprintf(op," %lu: %.8f\n",i,p->data[i]);
}
}
/* Allocate variable sized data elements */
static int icmU16Fixed16Array_allocate(
icmBase *pp
) {
icmU16Fixed16Array *p = (icmU16Fixed16Array *)pp;
icc *icp = p->icp;
if (p->size != p->_size) {
if (ovr_mul(p->size, sizeof(double))) {
sprintf(icp->err,"icmU16Fixed16Array_alloc: size overflow");
return icp->errc = 1;
}
if (p->data != NULL)
icp->al->free(icp->al, p->data);
if ((p->data = (double *) icp->al->calloc(icp->al, p->size, sizeof(double))) == NULL) {
sprintf(icp->err,"icmU16Fixed16Array_alloc: malloc() of icmU16Fixed16Array data failed");
return icp->errc = 2;
}
p->_size = p->size;
}
return 0;
}
/* Free all storage in the object */
static void icmU16Fixed16Array_delete(
icmBase *pp
) {
icmU16Fixed16Array *p = (icmU16Fixed16Array *)pp;
icc *icp = p->icp;
if (p->data != NULL)
icp->al->free(icp->al, p->data);
icp->al->free(icp->al, p);
}
/* Create an empty object. Return null on error */
static icmBase *new_icmU16Fixed16Array(
icc *icp
) {
icmU16Fixed16Array *p;
if ((p = (icmU16Fixed16Array *) icp->al->calloc(icp->al,1,sizeof(icmU16Fixed16Array))) == NULL)
return NULL;
p->ttype = icSigU16Fixed16ArrayType;
p->refcount = 1;
p->get_size = icmU16Fixed16Array_get_size;
p->read = icmU16Fixed16Array_read;
p->write = icmU16Fixed16Array_write;
p->dump = icmU16Fixed16Array_dump;
p->allocate = icmU16Fixed16Array_allocate;
p->del = icmU16Fixed16Array_delete;
p->icp = icp;
return (icmBase *)p;
}
/* ---------------------------------------------------------- */
/* icmS15Fixed16Array object */
/* Return the number of bytes needed to write this tag */
static unsigned int icmS15Fixed16Array_get_size(
icmBase *pp
) {
icmS15Fixed16Array *p = (icmS15Fixed16Array *)pp;
unsigned int len = 0;
len = sat_add(len, 8); /* 8 bytes for tag and padding */
len = sat_addmul(len, p->size, 4); /* 4 byte for each S15Fixed16 */
return len;
}
/* read the object, return 0 on success, error code on fail */
static int icmS15Fixed16Array_read(
icmBase *pp,
unsigned int len, /* tag length */
unsigned int of /* start offset within file */
) {
icmS15Fixed16Array *p = (icmS15Fixed16Array *)pp;
icc *icp = p->icp;
int rv = 0;
unsigned int i, size;
char *bp, *buf;
if (len < 8) {
sprintf(icp->err,"icmS15Fixed16Array_read: Tag too small to be legal");
return icp->errc = 1;
}
/* Allocate a file read buffer */
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmS15Fixed16Array_read: malloc() failed");
return icp->errc = 2;
}
bp = buf;
/* Read portion of file into buffer */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->read(icp->fp, bp, 1, len) != len) {
sprintf(icp->err,"icmS15Fixed16Array_read: fseek() or fread() failed");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
p->size = size = (len - 8)/4; /* Number of elements in the array */
if ((rv = p->allocate((icmBase *)p)) != 0) {
icp->al->free(icp->al, buf);
return rv;
}
/* Read type descriptor from the buffer */
if (((icTagTypeSignature)read_SInt32Number(bp)) != p->ttype) {
sprintf(icp->err,"icmS15Fixed16Array_read: Wrong tag type for icmS15Fixed16Array");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
bp += 8; /* Skip padding */
/* Read all the data from the buffer */
for (i = 0; i < size; i++, bp += 4) {
p->data[i] = read_S15Fixed16Number(bp);
}
icp->al->free(icp->al, buf);
return 0;
}
/* Write the contents of the object. Return 0 on sucess, error code on failure */
static int icmS15Fixed16Array_write(
icmBase *pp,
unsigned int of /* File offset to write from */
) {
icmS15Fixed16Array *p = (icmS15Fixed16Array *)pp;
icc *icp = p->icp;
unsigned int i;
unsigned int len;
char *bp, *buf; /* Buffer to write from */
int rv = 0;
/* Allocate a file write buffer */
if ((len = p->get_size((icmBase *)p)) == UINT_MAX) {
sprintf(icp->err,"icmS15Fixed16Array_write get_size overflow");
return icp->errc = 1;
}
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmS15Fixed16Array_write malloc() failed");
return icp->errc = 2;
}
bp = buf;
/* Write type descriptor to the buffer */
if ((rv = write_SInt32Number((int)p->ttype,bp)) != 0) {
sprintf(icp->err,"icmS15Fixed16Array_write: write_SInt32Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
write_SInt32Number(0,bp+4); /* Set padding to 0 */
/* Write all the data to the buffer */
bp += 8; /* Skip padding */
for (i = 0; i < p->size; i++, bp += 4) {
if ((rv = write_S15Fixed16Number(p->data[i],bp)) != 0) {
sprintf(icp->err,"icmS15Fixed16Array_write: write_S15Fixed16umber() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
}
/* Write to the file */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->write(icp->fp, buf, 1, len) != len) {
sprintf(icp->err,"icmS15Fixed16Array_write fseek() or fwrite() failed");
icp->al->free(icp->al, buf);
return icp->errc = 2;
}
icp->al->free(icp->al, buf);
return 0;
}
/* Dump a text description of the object */
static void icmS15Fixed16Array_dump(
icmBase *pp,
icmFile *op, /* Output to dump to */
int verb /* Verbosity level */
) {
icmS15Fixed16Array *p = (icmS15Fixed16Array *)pp;
if (verb <= 0)
return;
op->gprintf(op,"S15Fixed16Array:\n");
op->gprintf(op," No. elements = %lu\n",p->size);
if (verb >= 2) {
unsigned int i;
for (i = 0; i < p->size; i++)
op->gprintf(op," %lu: %.8f\n",i,p->data[i]);
}
}
/* Allocate variable sized data elements */
static int icmS15Fixed16Array_allocate(
icmBase *pp
) {
icmS15Fixed16Array *p = (icmS15Fixed16Array *)pp;
icc *icp = p->icp;
if (p->size != p->_size) {
if (ovr_mul(p->size, sizeof(double))) {
sprintf(icp->err,"icmS15Fixed16Array_alloc: size overflow");
return icp->errc = 1;
}
if (p->data != NULL)
icp->al->free(icp->al, p->data);
if ((p->data = (double *) icp->al->calloc(icp->al, p->size, sizeof(double))) == NULL) {
sprintf(icp->err,"icmS15Fixed16Array_alloc: malloc() of icmS15Fixed16Array data failed");
return icp->errc = 2;
}
p->_size = p->size;
}
return 0;
}
/* Free all storage in the object */
static void icmS15Fixed16Array_delete(
icmBase *pp
) {
icmS15Fixed16Array *p = (icmS15Fixed16Array *)pp;
icc *icp = p->icp;
if (p->data != NULL)
icp->al->free(icp->al, p->data);
icp->al->free(icp->al, p);
}
/* Create an empty object. Return null on error */
static icmBase *new_icmS15Fixed16Array(
icc *icp
) {
icmS15Fixed16Array *p;
if ((p = (icmS15Fixed16Array *) icp->al->calloc(icp->al,1,sizeof(icmS15Fixed16Array))) == NULL)
return NULL;
p->ttype = icSigS15Fixed16ArrayType;
p->refcount = 1;
p->get_size = icmS15Fixed16Array_get_size;
p->read = icmS15Fixed16Array_read;
p->write = icmS15Fixed16Array_write;
p->dump = icmS15Fixed16Array_dump;
p->allocate = icmS15Fixed16Array_allocate;
p->del = icmS15Fixed16Array_delete;
p->icp = icp;
return (icmBase *)p;
}
/* ---------------------------------------------------------- */
/* Data conversion support functions */
static int write_XYZNumber(icmXYZNumber *p, char *d) {
int rv;
if ((rv = write_S15Fixed16Number(p->X, d + 0)) != 0)
return rv;
if ((rv = write_S15Fixed16Number(p->Y, d + 4)) != 0)
return rv;
if ((rv = write_S15Fixed16Number(p->Z, d + 8)) != 0)
return rv;
return 0;
}
static int read_XYZNumber(icmXYZNumber *p, char *d) {
p->X = read_S15Fixed16Number(d + 0);
p->Y = read_S15Fixed16Number(d + 4);
p->Z = read_S15Fixed16Number(d + 8);
return 0;
}
/* Helper: Return a string that shows the XYZ number value */
static char *string_XYZNumber(icmXYZNumber *p) {
static char buf[40];
sprintf(buf,"%.8f, %.8f, %.8f", p->X, p->Y, p->Z);
return buf;
}
/* Helper: Return a string that shows the XYZ number value, */
/* and the Lab D50 number in paren. Note the buffer will be re-used on every call. */
static char *string_XYZNumber_and_Lab(icmXYZNumber *p) {
static char buf[100];
double lab[3];
lab[0] = p->X;
lab[1] = p->Y;
lab[2] = p->Z;
icmXYZ2Lab(&icmD50, lab, lab);
snprintf(buf,sizeof(buf),"%.8f, %.8f, %.8f [Lab %f, %f, %f]", p->X, p->Y, p->Z, lab[0], lab[1], lab[2]);
return buf;
}
/* icmXYZArray object */
/* Return the number of bytes needed to write this tag */
static unsigned int icmXYZArray_get_size(
icmBase *pp
) {
icmXYZArray *p = (icmXYZArray *)pp;
unsigned int len = 0;
len = sat_add(len, 8); /* 8 bytes for tag and padding */
len = sat_addmul(len, p->size, 12); /* 12 bytes for each XYZ */
return len;
}
/* read the object, return 0 on success, error code on fail */
static int icmXYZArray_read(
icmBase *pp,
unsigned int len, /* tag length */
unsigned int of /* start offset within file */
) {
icmXYZArray *p = (icmXYZArray *)pp;
icc *icp = p->icp;
int rv = 0;
unsigned int i, size;
char *bp, *buf;
if (len < 8) {
sprintf(icp->err,"icmXYZArray_read: Tag too small to be legal");
return icp->errc = 1;
}
/* Allocate a file read buffer */
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmXYZArray_read: malloc() failed");
return icp->errc = 2;
}
bp = buf;
/* Read portion of file into buffer */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->read(icp->fp, bp, 1, len) != len) {
sprintf(icp->err,"icmXYZArray_read: fseek() or fread() failed");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
p->size = size = (len - 8)/12; /* Number of elements in the array */
if ((rv = p->allocate((icmBase *)p)) != 0) {
icp->al->free(icp->al, buf);
return rv;
}
/* Read type descriptor from the buffer */
if (((icTagTypeSignature)read_SInt32Number(bp)) != p->ttype) {
sprintf(icp->err,"icmXYZArray_read: Wrong tag type for icmXYZArray");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
bp += 8; /* Skip padding */
/* Read all the data from the buffer */
for (i = 0; i < size; i++, bp += 12) {
read_XYZNumber(&p->data[i], bp);
}
icp->al->free(icp->al, buf);
return 0;
}
/* Write the contents of the object. Return 0 on sucess, error code on failure */
static int icmXYZArray_write(
icmBase *pp,
unsigned int of /* File offset to write from */
) {
icmXYZArray *p = (icmXYZArray *)pp;
icc *icp = p->icp;
unsigned int i;
unsigned int len;
char *bp, *buf; /* Buffer to write from */
int rv = 0;
/* Allocate a file write buffer */
if ((len = p->get_size((icmBase *)p)) == UINT_MAX) {
sprintf(icp->err,"icmXYZArray_write get_size overflow");
return icp->errc = 1;
}
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmXYZArray_write malloc() failed");
return icp->errc = 2;
}
bp = buf;
/* Write type descriptor to the buffer */
if ((rv = write_SInt32Number((int)p->ttype,bp)) != 0) {
sprintf(icp->err,"icmXYZArray_write: write_SInt32Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
write_SInt32Number(0,bp+4); /* Set padding to 0 */
/* Write all the data to the buffer */
bp += 8; /* Skip padding */
for (i = 0; i < p->size; i++, bp += 12) {
if ((rv = write_XYZNumber(&p->data[i],bp)) != 0) {
sprintf(icp->err,"icmXYZArray_write: write_XYZumber() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
}
/* Write to the file */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->write(icp->fp, buf, 1, len) != len) {
sprintf(icp->err,"icmXYZArray_write fseek() or fwrite() failed");
icp->al->free(icp->al, buf);
return icp->errc = 2;
}
icp->al->free(icp->al, buf);
return 0;
}
/* Dump a text description of the object */
static void icmXYZArray_dump(
icmBase *pp,
icmFile *op, /* Output to dump to */
int verb /* Verbosity level */
) {
icmXYZArray *p = (icmXYZArray *)pp;
if (verb <= 0)
return;
op->gprintf(op,"XYZArray:\n");
op->gprintf(op," No. elements = %lu\n",p->size);
if (verb >= 2) {
unsigned int i;
for (i = 0; i < p->size; i++) {
op->gprintf(op," %lu: %s\n",i,string_XYZNumber_and_Lab(&p->data[i]));
}
}
}
/* Allocate variable sized data elements */
static int icmXYZArray_allocate(
icmBase *pp
) {
icmXYZArray *p = (icmXYZArray *)pp;
icc *icp = p->icp;
if (p->size != p->_size) {
if (ovr_mul(p->size, sizeof(icmXYZNumber))) {
sprintf(icp->err,"icmXYZArray_alloc: size overflow");
return icp->errc = 1;
}
if (p->data != NULL)
icp->al->free(icp->al, p->data);
if ((p->data = (icmXYZNumber *) icp->al->malloc(icp->al, sat_mul(p->size, sizeof(icmXYZNumber)))) == NULL) {
sprintf(icp->err,"icmXYZArray_alloc: malloc() of icmXYZArray data failed");
return icp->errc = 2;
}
p->_size = p->size;
}
return 0;
}
/* Free all storage in the object */
static void icmXYZArray_delete(
icmBase *pp
) {
icmXYZArray *p = (icmXYZArray *)pp;
icc *icp = p->icp;
if (p->data != NULL)
icp->al->free(icp->al, p->data);
icp->al->free(icp->al, p);
}
/* Create an empty object. Return null on error */
static icmBase *new_icmXYZArray(
icc *icp
) {
icmXYZArray *p;
if ((p = (icmXYZArray *) icp->al->calloc(icp->al,1,sizeof(icmXYZArray))) == NULL)
return NULL;
p->ttype = icSigXYZArrayType;
p->refcount = 1;
p->get_size = icmXYZArray_get_size;
p->read = icmXYZArray_read;
p->write = icmXYZArray_write;
p->dump = icmXYZArray_dump;
p->allocate = icmXYZArray_allocate;
p->del = icmXYZArray_delete;
p->icp = icp;
return (icmBase *)p;
}
/* ---------------------------------------------------------- */
/* icmCurve object */
/* Do a forward lookup through the curve */
/* Return 0 on success, 1 if clipping occured, 2 on other error */
static int icmCurve_lookup_fwd(
icmCurve *p,
double *out,
double *in
) {
int rv = 0;
if (p->flag == icmCurveLin) {
*out = *in;
} else if (p->flag == icmCurveGamma) {
double val = *in;
if (val <= 0.0)
*out = 0.0;
else
*out = pow(val, p->data[0]);
} else if (p->size == 0) { /* Table of 0 size */
*out = *in;
} else { /* Use linear interpolation */
unsigned int ix;
double val, w;
double inputEnt_1 = (double)(p->size-1);
val = *in * inputEnt_1;
if (val < 0.0) {
val = 0.0;
rv |= 1;
} else if (val > inputEnt_1) {
val = inputEnt_1;
rv |= 1;
}
ix = (unsigned int)floor(val); /* Coordinate */
if (ix > (p->size-2))
ix = (p->size-2);
w = val - (double)ix; /* weight */
val = p->data[ix];
*out = val + w * (p->data[ix+1] - val);
}
return rv;
}
/* - - - - - - - - - - - - */
/* Support for reverse interpolation of 1D lookup tables */
/* Create a reverse curve lookup acceleration table */
/* return non-zero on error, 2 = malloc error. */
static int icmTable_setup_bwd(
icc *icp, /* Base icc object */
icmRevTable *rt, /* Reverse table data to setup */
unsigned int size, /* Size of fwd table */
double *data /* Table */
) {
unsigned int i;
rt->size = size; /* Stash pointers to these away */
rt->data = data;
/* Find range of output values */
rt->rmin = 1e300;
rt->rmax = -1e300;
for (i = 0; i < rt->size; i++) {
if (rt->data[i] > rt->rmax)
rt->rmax = rt->data[i];
if (rt->data[i] < rt->rmin)
rt->rmin = rt->data[i];
}
/* Decide on reverse granularity */
rt->rsize = sat_add(rt->size,2)/2;
rt->qscale = (double)rt->rsize/(rt->rmax - rt->rmin); /* Scale factor to quantize to */
if (ovr_mul(rt->size, sizeof(unsigned int *))) {
return 2;
}
/* Initialize the reverse lookup structures, and get overall min/max */
if ((rt->rlists = (unsigned int **) icp->al->calloc(icp->al, rt->rsize, sizeof(unsigned int *))) == NULL) {
return 2;
}
/* Assign each output value range bucket lists it intersects */
for (i = 0; i < (rt->size-1); i++) {
unsigned int s, e, j; /* Start and end indexes (inclusive) */
s = (unsigned int)((rt->data[i] - rt->rmin) * rt->qscale);
e = (unsigned int)((rt->data[i+1] - rt->rmin) * rt->qscale);
if (s >= rt->rsize)
s = rt->rsize-1;
if (e >= rt->rsize)
e = rt->rsize-1;
if (s > e) { /* swap */
unsigned int t;
t = s; s = e; e = t;
}
/* For all buckets that may contain this output range, add index of this output */
for (j = s; j <= e; j++) {
unsigned int as; /* Allocation size */
unsigned int nf; /* Next free slot */
if (rt->rlists[j] == NULL) { /* No allocation */
as = 5; /* Start with space for 5 */
if ((rt->rlists[j] = (unsigned int *) icp->al->calloc(icp->al, as, sizeof(unsigned int))) == NULL) {
return 2;
}
rt->rlists[j][0] = as;
nf = rt->rlists[j][1] = 2;
} else {
as = rt->rlists[j][0]; /* Allocate space for this list */
nf = rt->rlists[j][1]; /* Next free location in list */
if (nf >= as) { /* need to expand space */
if ((as = sat_mul(as, 2)) == UINT_MAX
|| ovr_mul(as, sizeof(unsigned int))) {
return 2;
}
rt->rlists[j] = (unsigned int *) icp->al->realloc(icp->al,rt->rlists[j], as * sizeof(unsigned int));
if (rt->rlists[j] == NULL) {
return 2;
}
rt->rlists[j][0] = as;
}
}
rt->rlists[j][nf++] = i;
rt->rlists[j][1] = nf;
}
}
rt->inited = 1;
return 0;
}
/* Free up any data */
static void icmTable_delete_bwd(
icc *icp, /* Base icc */
icmRevTable *rt /* Reverse table data to setup */
) {
if (rt->inited != 0) {
while (rt->rsize > 0)
icp->al->free(icp->al, rt->rlists[--rt->rsize]);
icp->al->free(icp->al, rt->rlists);
rt->size = 0; /* Don't keep these */
rt->data = NULL;
}
}
/* Do a reverse lookup through the curve */
/* Return 0 on success, 1 if clipping occured, 2 on other error */
static int icmTable_lookup_bwd(
icmRevTable *rt,
double *out,
double *in
) {
int rv = 0;
unsigned int ix, k, i;
double oval, ival = *in, val;
double rsize_1;
/* Find appropriate reverse list */
rsize_1 = (double)(rt->rsize-1);
val = ((ival - rt->rmin) * rt->qscale);
if (val < 0.0)
val = 0.0;
else if (val > rsize_1)
val = rsize_1;
ix = (unsigned int)floor(val); /* Coordinate */
if (ix > (rt->size-2))
ix = (rt->size-2);
if (rt->rlists[ix] != NULL) { /* There is a list of fwd candidates */
/* For each candidate forward range */
for (i = 2; i < rt->rlists[ix][1]; i++) { /* For all fwd indexes */
double lv,hv;
k = rt->rlists[ix][i]; /* Base index */
lv = rt->data[k];
hv = rt->data[k+1];
if ((ival >= lv && ival <= hv) /* If this slot contains output value */
|| (ival >= hv && ival <= lv)) {
/* Reverse linear interpolation */
if (hv == lv) { /* Technically non-monotonic - due to quantization ? */
oval = (k + 0.5)/(rt->size-1.0);
} else
oval = (k + ((ival - lv)/(hv - lv)))/(rt->size-1.0);
/* If we kept looking, we would find multiple */
/* solution for non-monotonic curve */
*out = oval;
return rv;
}
}
}
/* We have failed to find an exact value, so return the nearest value */
/* (This is slow !) */
val = fabs(ival - rt->data[0]);
for (k = 0, i = 1; i < rt->size; i++) {
double er;
er = fabs(ival - rt->data[i]);
if (er < val) { /* new best */
val = er;
k = i;
}
}
*out = k/(rt->size-1.0);
rv |= 1;
return rv;
}
/* - - - - - - - - - - - - */
/* Do a reverse lookup through the curve */
/* Return 0 on success, 1 if clipping occured, 2 on other error */
/* (Note that clipping means mathematical clipping, and is not */
/* set just because a device value is out of gamut. */
static int icmCurve_lookup_bwd(
icmCurve *p,
double *out,
double *in
) {
icc *icp = p->icp;
int rv = 0;
if (p->flag == icmCurveLin) {
*out = *in;
} else if (p->flag == icmCurveGamma) {
double val = *in;
if (val <= 0.0)
*out = 0.0;
else
*out = pow(val, 1.0/p->data[0]);
} else if (p->size == 0) { /* Table of 0 size */
*out = *in;
} else { /* Use linear interpolation */
if (p->rt.inited == 0) {
rv = icmTable_setup_bwd(icp, &p->rt, p->size, p->data);
if (rv != 0) {
sprintf(icp->err,"icmCurve_lookup: Malloc failure in reverse lookup init.");
return icp->errc = rv;
}
}
rv = icmTable_lookup_bwd(&p->rt, out, in);
}
return rv;
}
/* Return the number of bytes needed to write this tag */
static unsigned int icmCurve_get_size(
icmBase *pp
) {
icmCurve *p = (icmCurve *)pp;
unsigned int len = 0;
len = sat_add(len, 12); /* 12 bytes for tag, padding and count */
len = sat_addmul(len, p->size, 2); /* 2 bytes for each UInt16 */
return len;
}
/* read the object, return 0 on success, error code on fail */
static int icmCurve_read(
icmBase *pp,
unsigned int len, /* tag length */
unsigned int of /* start offset within file */
) {
icmCurve *p = (icmCurve *)pp;
icc *icp = p->icp;
int rv = 0;
unsigned int i;
char *bp, *buf, *end;
if (len < 12) {
sprintf(icp->err,"icmCurve_read: Tag too small to be legal");
return icp->errc = 1;
}
/* Allocate a file read buffer */
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmCurve_read: malloc() failed");
return icp->errc = 2;
}
bp = buf;
end = buf + len;
/* Read portion of file into buffer */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->read(icp->fp, bp, 1, len) != len) {
sprintf(icp->err,"icmCurve_read: fseek() or fread() failed");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
/* Read type descriptor from the buffer */
if (((icTagTypeSignature)read_SInt32Number(bp)) != p->ttype) {
sprintf(icp->err,"icmCurve_read: Wrong tag type for icmCurve");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
p->size = read_UInt32Number(bp+8);
bp = bp + 12;
/* Set flag up before allocating */
if (p->size == 0) { /* Linear curve */
p->flag = icmCurveLin;
} else if (p->size == 1) { /* Gamma curve */
p->flag = icmCurveGamma;
} else {
p->flag = icmCurveSpec;
if (p->size > (len - 12)/2) {
sprintf(icp->err,"icmCurve_read: size overflow");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
}
if ((rv = p->allocate((icmBase *)p)) != 0) {
icp->al->free(icp->al, buf);
return rv;
}
if (p->flag == icmCurveGamma) { /* Gamma curve */
if (bp > end || 1 > (end - bp)) {
sprintf(icp->err,"icmCurve_read: Data too short for curve gamma");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
p->data[0] = read_U8Fixed8Number(bp);
} else if (p->flag == icmCurveSpec) {
/* Read all the data from the buffer */
for (i = 0; i < p->size; i++, bp += 2) {
if (bp > end || 2 > (end - bp)) {
sprintf(icp->err,"icmCurve_read: Data too short for curve value");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
p->data[i] = read_DCS16Number(bp);
}
}
icp->al->free(icp->al, buf);
return 0;
}
/* Write the contents of the object. Return 0 on sucess, error code on failure */
static int icmCurve_write(
icmBase *pp,
unsigned int of /* File offset to write from */
) {
icmCurve *p = (icmCurve *)pp;
icc *icp = p->icp;
unsigned int i;
unsigned int len;
char *bp, *buf; /* Buffer to write from */
int rv = 0;
/* Allocate a file write buffer */
if ((len = p->get_size((icmBase *)p)) == UINT_MAX) {
sprintf(icp->err,"icmCurve_write get_size overflow");
return icp->errc = 1;
}
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmCurve_write malloc() failed");
return icp->errc = 2;
}
bp = buf;
/* Write type descriptor to the buffer */
if ((rv = write_SInt32Number((int)p->ttype,bp)) != 0) {
sprintf(icp->err,"icmCurve_write: write_SInt32Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
write_SInt32Number(0,bp+4); /* Set padding to 0 */
/* Write count */
if ((rv = write_UInt32Number(p->size,bp+8)) != 0) {
sprintf(icp->err,"icmCurve_write: write_UInt32Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
/* Write all the data to the buffer */
bp += 12; /* Skip padding */
if (p->flag == icmCurveLin) {
if (p->size != 0) {
sprintf(icp->err,"icmCurve_write: Must be exactly 0 entry for Linear");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
} else if (p->flag == icmCurveGamma) {
if (p->size != 1) {
sprintf(icp->err,"icmCurve_write: Must be exactly 1 entry for Gamma");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
if ((rv = write_U8Fixed8Number(p->data[0],bp)) != 0) {
sprintf(icp->err,"icmCurve_write: write_U8Fixed8umber(%.8f) failed",p->data[0]);
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
} else if (p->flag == icmCurveSpec) {
if (p->size < 2) {
sprintf(icp->err,"icmCurve_write: Must be 2 or more entries for Specified curve");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
for (i = 0; i < p->size; i++, bp += 2) {
if ((rv = write_DCS16Number(p->data[i],bp)) != 0) {
sprintf(icp->err,"icmCurve_write: write_UInt16umber(%.8f) failed",p->data[i]);
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
}
}
/* Write to the file */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->write(icp->fp, buf, 1, len) != len) {
sprintf(icp->err,"icmCurve_write fseek() or fwrite() failed");
icp->al->free(icp->al, buf);
return icp->errc = 2;
}
icp->al->free(icp->al, buf);
return 0;
}
/* Dump a text description of the object */
static void icmCurve_dump(
icmBase *pp,
icmFile *op, /* Output to dump to */
int verb /* Verbosity level */
) {
icmCurve *p = (icmCurve *)pp;
if (verb <= 0)
return;
op->gprintf(op,"Curve:\n");
if (p->flag == icmCurveLin) {
op->gprintf(op," Curve is linear\n");
} else if (p->flag == icmCurveGamma) {
op->gprintf(op," Curve is gamma of %.8f\n",p->data[0]);
} else {
op->gprintf(op," No. elements = %lu\n",p->size);
if (verb >= 2) {
unsigned int i;
for (i = 0; i < p->size; i++)
op->gprintf(op," %3lu: %.8f\n",i,p->data[i]);
}
}
}
/* Allocate variable sized data elements */
static int icmCurve_allocate(
icmBase *pp
) {
icmCurve *p = (icmCurve *)pp;
icc *icp = p->icp;
if (p->flag == icmCurveUndef) {
sprintf(icp->err,"icmCurve_alloc: flag not set");
return icp->errc = 1;
} else if (p->flag == icmCurveLin) {
p->size = 0;
} else if (p->flag == icmCurveGamma) {
p->size = 1;
}
if (p->size != p->_size) {
if (ovr_mul(p->size, sizeof(double))) {
sprintf(icp->err,"icmCurve_alloc: size overflow");
return icp->errc = 1;
}
if (p->data != NULL)
icp->al->free(icp->al, p->data);
if ((p->data = (double *) icp->al->calloc(icp->al, p->size, sizeof(double))) == NULL) {
sprintf(icp->err,"icmCurve_alloc: malloc() of icmCurve data failed");
return icp->errc = 2;
}
p->_size = p->size;
}
return 0;
}
/* Free all storage in the object */
static void icmCurve_delete(
icmBase *pp
) {
icmCurve *p = (icmCurve *)pp;
icc *icp = p->icp;
if (p->data != NULL)
icp->al->free(icp->al, p->data);
icmTable_delete_bwd(icp, &p->rt); /* Free reverse table info */
icp->al->free(icp->al, p);
}
/* Create an empty object. Return null on error */
static icmBase *new_icmCurve(
icc *icp
) {
icmCurve *p;
if ((p = (icmCurve *) icp->al->calloc(icp->al,1,sizeof(icmCurve))) == NULL)
return NULL;
p->ttype = icSigCurveType;
p->refcount = 1;
p->get_size = icmCurve_get_size;
p->read = icmCurve_read;
p->write = icmCurve_write;
p->dump = icmCurve_dump;
p->allocate = icmCurve_allocate;
p->del = icmCurve_delete;
p->icp = icp;
p->lookup_fwd = icmCurve_lookup_fwd;
p->lookup_bwd = icmCurve_lookup_bwd;
p->rt.inited = 0;
p->flag = icmCurveUndef;
return (icmBase *)p;
}
/* ---------------------------------------------------------- */
/* icmData object */
/* Return the number of bytes needed to write this tag */
static unsigned int icmData_get_size(
icmBase *pp
) {
icmData *p = (icmData *)pp;
unsigned int len = 0;
len = sat_add(len, 12); /* 12 bytes for tag and padding */
len = sat_addmul(len, p->size, 1); /* 1 byte for each data element */
return len;
}
/* read the object, return 0 on success, error code on fail */
static int icmData_read(
icmBase *pp,
unsigned int len, /* tag length */
unsigned int of /* start offset within file */
) {
icmData *p = (icmData *)pp;
icc *icp = p->icp;
int rv;
unsigned size, f;
char *bp, *buf;
if (len < 12) {
sprintf(icp->err,"icmData_read: Tag too small to be legal");
return icp->errc = 1;
}
/* Allocate a file read buffer */
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmData_read: malloc() failed");
return icp->errc = 2;
}
bp = buf;
/* Read portion of file into buffer */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->read(icp->fp, bp, 1, len) != len) {
sprintf(icp->err,"icmData_read: fseek() or fread() failed");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
p->size = size = (len - 12)/1; /* Number of elements in the array */
/* Read type descriptor from the buffer */
if (((icTagTypeSignature)read_SInt32Number(bp)) != p->ttype) {
sprintf(icp->err,"icmData_read: Wrong tag type for icmData");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
/* Read the data type flag */
f = read_UInt32Number(bp+8);
if (f == 0) {
p->flag = icmDataASCII;
} else if (f == 1) {
p->flag = icmDataBin;
#ifndef ICM_STRICT /* Profile maker sometimes has a problem */
} else if (f == 0x01000000) {
p->flag = icmDataBin;
#endif
} else {
sprintf(icp->err,"icmData_read: Unknown flag value 0x%x",f);
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
bp += 12; /* Skip padding and flag */
if (p->size > 0) {
if (p->flag == icmDataASCII) {
if ((rv = check_null_string(bp,p->size)) == 1) {
sprintf(icp->err,"icmData_read: ACSII is not null terminated");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
/* Haven't checked if rv == 2 is legal or not */
}
if ((rv = p->allocate((icmBase *)p)) != 0) {
icp->al->free(icp->al, buf);
return rv;
}
memmove((void *)p->data, (void *)bp, p->size);
}
icp->al->free(icp->al, buf);
return 0;
}
/* Write the contents of the object. Return 0 on sucess, error code on failure */
static int icmData_write(
icmBase *pp,
unsigned int of /* File offset to write from */
) {
icmData *p = (icmData *)pp;
icc *icp = p->icp;
unsigned int len, f;
char *bp, *buf; /* Buffer to write from */
int rv;
/* Allocate a file write buffer */
if ((len = p->get_size((icmBase *)p)) == UINT_MAX) {
sprintf(icp->err,"icmData_write get_size overflow");
return icp->errc = 1;
}
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmData_write malloc() failed");
return icp->errc = 2;
}
bp = buf;
/* Write type descriptor to the buffer */
if ((rv = write_SInt32Number((int)p->ttype,bp)) != 0) {
sprintf(icp->err,"icmData_write: write_SInt32Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
write_SInt32Number(0,bp+4); /* Set padding to 0 */
switch(p->flag) {
case icmDataASCII:
f = 0;
break;
case icmDataBin:
f = 1;
break;
default:
sprintf(icp->err,"icmData_write: Unknown Data Flag value");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
/* Write data flag descriptor to the buffer */
if ((rv = write_UInt32Number(f,bp+8)) != 0) {
sprintf(icp->err,"icmData_write: write_SInt32Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
bp += 12; /* Skip padding */
if (p->data != NULL) {
if (p->flag == icmDataASCII) {
if ((rv = check_null_string((char *)p->data, p->size)) == 1) {
sprintf(icp->err,"icmData_write: ASCII is not null terminated");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
/* Haven't checked if rv == 2 is legal or not */
}
memmove((void *)bp, (void *)p->data, p->size);
}
/* Write to the file */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->write(icp->fp, buf, 1, len) != len) {
sprintf(icp->err,"icmData_write fseek() or fwrite() failed");
icp->al->free(icp->al, buf);
return icp->errc = 2;
}
icp->al->free(icp->al, buf);
return 0;
}
/* Dump a text description of the object */
static void icmData_dump(
icmBase *pp,
icmFile *op, /* Output to dump to */
int verb /* Verbosity level */
) {
icmData *p = (icmData *)pp;
unsigned int i, r, c, ii, size = 0;
int ph = 0; /* Phase */
if (verb <= 0)
return;
op->gprintf(op,"Data:\n");
switch(p->flag) {
case icmDataASCII:
op->gprintf(op," ASCII data\n");
size = p->size > 0 ? p->size-1 : 0;
break;
case icmDataBin:
op->gprintf(op," Binary data\n");
size = p->size;
break;
case icmDataUndef:
op->gprintf(op," Undefined data\n");
size = p->size;
break;
}
op->gprintf(op," No. elements = %lu\n",p->size);
ii = i = 0;
for (r = 1;; r++) { /* count rows */
if (i >= size) {
op->gprintf(op,"\n");
break;
}
if (r > 1 && verb < 2) {
op->gprintf(op,"...\n");
break; /* Print 1 row if not verbose */
}
c = 1;
if (ph != 0) { /* Print ASCII under binary */
op->gprintf(op," ");
i = ii;
c += 11;
} else {
op->gprintf(op," 0x%04lx: ",i);
ii = i;
c += 10;
}
while (i < size && c < 75) {
if (p->flag == icmDataASCII) {
if (isprint(p->data[i])) {
op->gprintf(op,"%c",p->data[i]);
c++;
} else {
op->gprintf(op,"\\%03o",p->data[i]);
c += 4;
}
} else {
if (ph == 0)
op->gprintf(op,"%02x ",p->data[i]);
else {
if (isprint(p->data[i]))
op->gprintf(op," %c ",p->data[i]);
else
op->gprintf(op," ",p->data[i]);
}
c += 3;
}
i++;
}
if (i < size)
op->gprintf(op,"\n");
if (verb > 2 && p->flag != icmDataASCII && ph == 0)
ph = 1;
else
ph = 0;
}
}
/* Allocate variable sized data elements */
static int icmData_allocate(
icmBase *pp
) {
icmData *p = (icmData *)pp;
icc *icp = p->icp;
if (p->size != p->_size) {
if (ovr_mul(p->size, sizeof(unsigned char))) {
sprintf(icp->err,"icmData_alloc: size overflow");
return icp->errc = 1;
}
if (p->data != NULL)
icp->al->free(icp->al, p->data);
if ((p->data = (unsigned char *) icp->al->calloc(icp->al, p->size, sizeof(unsigned char))) == NULL) {
sprintf(icp->err,"icmData_alloc: malloc() of icmData data failed");
return icp->errc = 2;
}
p->_size = p->size;
}
return 0;
}
/* Free all storage in the object */
static void icmData_delete(
icmBase *pp
) {
icmData *p = (icmData *)pp;
icc *icp = p->icp;
if (p->data != NULL)
icp->al->free(icp->al, p->data);
icp->al->free(icp->al, p);
}
/* Create an empty object. Return null on error */
static icmBase *new_icmData(
icc *icp
) {
icmData *p;
if ((p = (icmData *) icp->al->calloc(icp->al,1,sizeof(icmData))) == NULL)
return NULL;
p->ttype = icSigDataType;
p->refcount = 1;
p->get_size = icmData_get_size;
p->read = icmData_read;
p->write = icmData_write;
p->dump = icmData_dump;
p->allocate = icmData_allocate;
p->del = icmData_delete;
p->icp = icp;
p->flag = icmDataUndef;
return (icmBase *)p;
}
/* ---------------------------------------------------------- */
/* icmText object */
/* Return the number of bytes needed to write this tag */
static unsigned int icmText_get_size(
icmBase *pp
) {
icmText *p = (icmText *)pp;
unsigned int len = 0;
len = sat_add(len, 8); /* 8 bytes for tag and padding */
len = sat_addmul(len, p->size, 1); /* 1 byte for each character element (inc. null) */
return len;
}
/* read the object, return 0 on success, error code on fail */
static int icmText_read(
icmBase *pp,
unsigned int len, /* tag length */
unsigned int of /* start offset within file */
) {
icmText *p = (icmText *)pp;
icc *icp = p->icp;
int rv;
char *bp, *buf;
if (len < 8) {
sprintf(icp->err,"icmText_read: Tag too short to be legal");
return icp->errc = 1;
}
/* Allocate a file read buffer */
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmText_read: malloc() failed");
return icp->errc = 2;
}
bp = buf;
/* Read portion of file into buffer */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->read(icp->fp, bp, 1, len) != len) {
sprintf(icp->err,"icmText_read: fseek() or fread() failed");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
p->size = (len - 8)/1; /* Number of elements in the array */
/* Read type descriptor from the buffer */
if (((icTagTypeSignature)read_SInt32Number(bp)) != p->ttype) {
sprintf(icp->err,"icmText_read: Wrong tag type for icmText");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
bp = bp + 8;
if (p->size > 0) {
if ((rv = check_null_string(bp,p->size)) == 1) {
sprintf(icp->err,"icmText_read: text is not null terminated");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
/* Haven't checked if rv == 2 is legal or not */
if ((rv = p->allocate((icmBase *)p)) != 0) {
icp->al->free(icp->al, buf);
return rv;
}
memmove((void *)p->data, (void *)bp, p->size);
}
icp->al->free(icp->al, buf);
return 0;
}
/* Write the contents of the object. Return 0 on sucess, error code on failure */
static int icmText_write(
icmBase *pp,
unsigned int of /* File offset to write from */
) {
icmText *p = (icmText *)pp;
icc *icp = p->icp;
unsigned int len;
char *bp, *buf; /* Buffer to write from */
int rv;
/* Allocate a file write buffer */
if ((len = p->get_size((icmBase *)p)) == UINT_MAX) {
sprintf(icp->err,"icmText_write get_size overflow");
return icp->errc = 1;
}
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmText_write malloc() failed");
return icp->errc = 2;
}
bp = buf;
/* Write type descriptor to the buffer */
if ((rv = write_SInt32Number((int)p->ttype,bp)) != 0) {
sprintf(icp->err,"icmText_write: write_SInt32Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
write_SInt32Number(0,bp+4); /* Set padding to 0 */
bp = bp + 8;
if (p->data != NULL) {
if ((rv = check_null_string(p->data, p->size)) == 1) {
sprintf(icp->err,"icmText_write: text is not null terminated");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
/* Haven't checked if rv == 2 is legal or not */
memmove((void *)bp, (void *)p->data, p->size);
}
/* Write to the file */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->write(icp->fp, buf, 1, len) != len) {
sprintf(icp->err,"icmText_write fseek() or fwrite() failed");
icp->al->free(icp->al, buf);
return icp->errc = 2;
}
icp->al->free(icp->al, buf);
return 0;
}
/* Dump a text description of the object */
static void icmText_dump(
icmBase *pp,
icmFile *op, /* Output to dump to */
int verb /* Verbosity level */
) {
icmText *p = (icmText *)pp;
unsigned int i, r, c, size;
if (verb <= 0)
return;
op->gprintf(op,"Text:\n");
op->gprintf(op," No. chars = %lu\n",p->size);
size = p->size > 0 ? p->size-1 : 0;
i = 0;
for (r = 1;; r++) { /* count rows */
if (i >= size) {
op->gprintf(op,"\n");
break;
}
if (r > 1 && verb < 2) {
op->gprintf(op,"...\n");
break; /* Print 1 row if not verbose */
}
c = 1;
op->gprintf(op," 0x%04lx: ",i);
c += 10;
while (i < size && c < 75) {
if (isprint(p->data[i])) {
op->gprintf(op,"%c",p->data[i]);
c++;
} else {
op->gprintf(op,"\\%03o",p->data[i]);
c += 4;
}
i++;
}
if (i < size)
op->gprintf(op,"\n");
}
}
/* Allocate variable sized data elements */
static int icmText_allocate(
icmBase *pp
) {
icmText *p = (icmText *)pp;
icc *icp = p->icp;
if (p->size != p->_size) {
if (ovr_mul(p->size, sizeof(char))) {
sprintf(icp->err,"icmText_alloc: size overflow");
return icp->errc = 1;
}
if (p->data != NULL)
icp->al->free(icp->al, p->data);
if ((p->data = (char *) icp->al->calloc(icp->al, p->size, sizeof(char))) == NULL) {
sprintf(icp->err,"icmText_alloc: malloc() of icmText data failed");
return icp->errc = 2;
}
p->_size = p->size;
}
return 0;
}
/* Free all storage in the object */
static void icmText_delete(
icmBase *pp
) {
icmText *p = (icmText *)pp;
icc *icp = p->icp;
if (p->data != NULL)
icp->al->free(icp->al, p->data);
icp->al->free(icp->al, p);
}
/* Create an empty object. Return null on error */
static icmBase *new_icmText(
icc *icp
) {
icmText *p;
if ((p = (icmText *) icp->al->calloc(icp->al,1,sizeof(icmText))) == NULL)
return NULL;
p->ttype = icSigTextType;
p->refcount = 1;
p->get_size = icmText_get_size;
p->read = icmText_read;
p->write = icmText_write;
p->dump = icmText_dump;
p->allocate = icmText_allocate;
p->del = icmText_delete;
p->icp = icp;
return (icmBase *)p;
}
/* ---------------------------------------------------------- */
/* Data conversion support functions */
static int write_DateTimeNumber(icmDateTimeNumber *p, char *d) {
int rv;
if (p->year < 1900 || p->year > 3000
|| p->month == 0 || p->month > 12
|| p->day == 0 || p->day > 31
|| p->hours > 23
|| p->minutes > 59
|| p->seconds > 59)
return 1;
if ((rv = write_UInt16Number(p->year, d + 0)) != 0)
return rv;
if ((rv = write_UInt16Number(p->month, d + 2)) != 0)
return rv;
if ((rv = write_UInt16Number(p->day, d + 4)) != 0)
return rv;
if ((rv = write_UInt16Number(p->hours, d + 6)) != 0)
return rv;
if ((rv = write_UInt16Number(p->minutes, d + 8)) != 0)
return rv;
if ((rv = write_UInt16Number(p->seconds, d + 10)) != 0)
return rv;
return 0;
}
static int read_DateTimeNumber(icmDateTimeNumber *p, char *d) {
p->year = read_UInt16Number(d + 0);
p->month = read_UInt16Number(d + 2);
p->day = read_UInt16Number(d + 4);
p->hours = read_UInt16Number(d + 6);
p->minutes = read_UInt16Number(d + 8);
p->seconds = read_UInt16Number(d + 10);
/* Sanity check the date and time */
if (p->year >= 1900 && p->year <= 3000
&& p->month != 0 && p->month <= 12
&& p->day != 0 && p->day <= 31
&& p->hours <= 23
&& p->minutes <= 59
&& p->seconds <= 59)
return 0;
#ifdef NEVER
printf("Raw year = %d, month = %d, day = %d\n",p->year, p->month, p->day);
printf("Raw hour = %d, minutes = %d, seconds = %d\n", p->hours, p->minutes, p->seconds);
#endif /* NEVER */
#ifdef ICM_STRICT
return 1; /* Not legal */
#else
/* Be more forgiving */
/* Check for Adobe problem */
if (p->month >= 1900 && p->month <= 3000
&& p->year != 0 && p->year <= 12
&& p->hours != 0 && p->hours <= 31
&& p->day <= 23
&& p->seconds <= 59
&& p->minutes <= 59) {
unsigned int tt;
/* Correct Adobe's faulty profile */
tt = p->month; p->month = p->year; p->year = tt;
tt = p->hours; p->hours = p->day; p->day = tt;
tt = p->seconds; p->seconds = p->minutes; p->minutes = tt;
return 0;
}
/* Hmm. some other sort of corruption. Limit values to sane */
if (p->year < 1900) {
if (p->year < 100) /* Hmm. didn't use 4 digit year, guess it's 19xx ? */
p->year += 1900;
else
p->year = 1900;
} else if (p->year > 3000)
p->year = 3000;
if (p->month == 0)
p->month = 1;
else if (p->month > 12)
p->month = 12;
if (p->day == 0)
p->day = 1;
else if (p->day > 31)
p->day = 31;
if (p->hours > 23)
p->hours = 23;
if (p->minutes > 59)
p->minutes = 59;
if (p->seconds > 59)
p->seconds = 59;
return 0;
#endif
}
/* Return a string that shows the given date and time */
static char *string_DateTimeNumber(icmDateTimeNumber *p) {
static const char *mstring[13] = {"Bad", "Jan","Feb","Mar","Apr","May","Jun",
"Jul","Aug","Sep","Oct","Nov","Dec"};
static char buf[80];
sprintf(buf,"%d %s %4d, %d:%02d:%02d",
p->day, mstring[p->month > 12 ? 0 : p->month], p->year,
p->hours, p->minutes, p->seconds);
return buf;
}
/* Set the DateTime structure to the current date and time */
static void setcur_DateTimeNumber(icmDateTimeNumber *p) {
time_t cclk;
struct tm *ctm;
cclk = time(NULL);
ctm = localtime(&cclk);
p->year = ctm->tm_year + 1900;
p->month = ctm->tm_mon + 1;
p->day = ctm->tm_mday;
p->hours = ctm->tm_hour;
p->minutes = ctm->tm_min;
p->seconds = ctm->tm_sec;
}
/* Return the number of bytes needed to write this tag */
static unsigned int icmDateTimeNumber_get_size(
icmBase *pp
) {
unsigned int len = 0;
len = sat_add(len, 8); /* 8 bytes for tag and padding */
len = sat_add(len, 12); /* 12 bytes for Date & Time */
return len;
}
/* read the object, return 0 on success, error code on fail */
static int icmDateTimeNumber_read(
icmBase *pp,
unsigned int len, /* tag length */
unsigned int of /* start offset within file */
) {
icmDateTimeNumber *p = (icmDateTimeNumber *)pp;
icc *icp = p->icp;
int rv;
char *bp, *buf;
if (len < 20) {
sprintf(icp->err,"icmDateTimeNumber_read: Tag too small to be legal");
return icp->errc = 1;
}
/* Allocate a file read buffer */
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmDateTimeNumber_read: malloc() failed");
return icp->errc = 2;
}
bp = buf;
/* Read portion of file into buffer */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->read(icp->fp, bp, 1, len) != len) {
sprintf(icp->err,"icmDateTimeNumber_read: fseek() or fread() failed");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
/* Read type descriptor from the buffer */
if (((icTagTypeSignature)read_SInt32Number(bp)) != p->ttype) {
sprintf(icp->err,"icmDateTimeNumber_read: Wrong tag type for icmDateTimeNumber");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
bp += 8; /* Skip padding */
/* Read the time and date from buffer */
if((rv = read_DateTimeNumber(p, bp)) != 0) {
sprintf(icp->err,"icmDateTimeNumber_read: Corrupted DateTime");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
icp->al->free(icp->al, buf);
return 0;
}
/* Write the contents of the object. Return 0 on sucess, error code on failure */
static int icmDateTimeNumber_write(
icmBase *pp,
unsigned int of /* File offset to write from */
) {
icmDateTimeNumber *p = (icmDateTimeNumber *)pp;
icc *icp = p->icp;
unsigned int len;
char *bp, *buf; /* Buffer to write from */
int rv = 0;
/* Allocate a file write buffer */
if ((len = p->get_size((icmBase *)p)) == UINT_MAX) {
sprintf(icp->err,"icmDateTimeNumber_write get_size overflow");
return icp->errc = 1;
}
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmDateTimeNumber_write malloc() failed");
return icp->errc = 2;
}
bp = buf;
/* Write type descriptor to the buffer */
if ((rv = write_SInt32Number((int)p->ttype,bp)) != 0) {
sprintf(icp->err,"icmDateTimeNumber_write: write_SInt32Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
write_SInt32Number(0,bp+4); /* Set padding to 0 */
/* Write all the data to the buffer */
bp += 8; /* Skip padding */
if ((rv = write_DateTimeNumber(p, bp)) != 0) {
sprintf(icp->err,"icmDateTimeNumber_write: write_DateTimeNumber() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
/* Write to the file */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->write(icp->fp, buf, 1, len) != len) {
sprintf(icp->err,"icmDateTimeNumber_write fseek() or fwrite() failed");
icp->al->free(icp->al, buf);
return icp->errc = 2;
}
icp->al->free(icp->al, buf);
return 0;
}
/* Dump a text description of the object */
static void icmDateTimeNumber_dump(
icmBase *pp,
icmFile *op, /* Output to dump to */
int verb /* Verbosity level */
) {
icmDateTimeNumber *p = (icmDateTimeNumber *)pp;
if (verb <= 0)
return;
op->gprintf(op,"DateTimeNumber:\n");
op->gprintf(op," Date = %s\n", string_DateTimeNumber(p));
}
/* Allocate variable sized data elements */
static int icmDateTimeNumber_allocate(
icmBase *pp
) {
/* Nothing to do */
return 0;
}
/* Free all storage in the object */
static void icmDateTimeNumber_delete(
icmBase *pp
) {
icmDateTimeNumber *p = (icmDateTimeNumber *)pp;
icc *icp = p->icp;
icp->al->free(icp->al, p);
}
/* Create an empty object. Return null on error */
static icmBase *new_icmDateTimeNumber(
icc *icp
) {
icmDateTimeNumber *p;
if ((p = (icmDateTimeNumber *) icp->al->calloc(icp->al,1,sizeof(icmDateTimeNumber))) == NULL)
return NULL;
p->ttype = icSigDateTimeType;
p->refcount = 1;
p->get_size = icmDateTimeNumber_get_size;
p->read = icmDateTimeNumber_read;
p->write = icmDateTimeNumber_write;
p->dump = icmDateTimeNumber_dump;
p->allocate = icmDateTimeNumber_allocate;
p->del = icmDateTimeNumber_delete;
p->icp = icp;
setcur_DateTimeNumber(p); /* Default to current date and time */
return (icmBase *)p;
}
/* ---------------------------------------------------------- */
/* icmLut object */
/* Check if the matrix is non-zero */
static int icmLut_nu_matrix(
icmLut *p /* Pointer to Lut object */
) {
int i, j;
for (j = 0; j < 3; j++) { /* Rows */
for (i = 0; i < 3; i++) { /* Columns */
if ( (i == j && p->e[j][i] != 1.0)
|| (i != j && p->e[j][i] != 0.0))
return 1;
}
}
return 0;
}
/* return the locations of the minimum and */
/* maximum values of the given channel, in the clut */
static void icmLut_min_max(
icmLut *p, /* Pointer to Lut object */
double *minp, /* Return position of min/max */
double *maxp,
int chan /* Channel, -1 for average of all */
) {
double *tp;
double minv, maxv; /* Values */
unsigned int e, ee, f;
int gc[MAX_CHAN]; /* Grid coordinate */
minv = 1e6;
maxv = -1e6;
for (e = 0; e < p->inputChan; e++)
gc[e] = 0; /* init coords */
/* Search the whole table */
for (tp = p->clutTable, e = 0; e < p->inputChan; tp += p->outputChan) {
double v;
if (chan == -1) {
for (v = 0.0, f = 0; f < p->outputChan; f++)
v += tp[f];
} else {
v = tp[chan];
}
if (v < minv) {
minv = v;
for (ee = 0; ee < p->inputChan; ee++)
minp[ee] = gc[ee]/(p->clutPoints-1.0);
}
if (v > maxv) {
maxv = v;
for (ee = 0; ee < p->inputChan; ee++)
maxp[ee] = gc[ee]/(p->clutPoints-1.0);
}
/* Increment coord */
for (e = 0; e < p->inputChan; e++) {
if (++gc[e] < p->clutPoints)
break; /* No carry */
gc[e] = 0;
}
}
}
/* Convert XYZ throught Luts matrix */
/* Return 0 on success, 1 if clipping occured, 2 on other error */
static int icmLut_lookup_matrix(
icmLut *p, /* Pointer to Lut object */
double *out, /* Output array[outputChan] in ICC order - see Table 39 in 6.5.5 */
double *in /* Input array[inputChan] */
) {
double t0,t1; /* Take care if out == in */
t0 = p->e[0][0] * in[0] + p->e[0][1] * in[1] + p->e[0][2] * in[2];
t1 = p->e[1][0] * in[0] + p->e[1][1] * in[1] + p->e[1][2] * in[2];
out[2] = p->e[2][0] * in[0] + p->e[2][1] * in[1] + p->e[2][2] * in[2];
out[0] = t0;
out[1] = t1;
return 0;
}
/* Convert normalized numbers though this Luts per channel input tables. */
/* Return 0 on success, 1 if clipping occured, 2 on other error */
static int icmLut_lookup_input(
icmLut *p, /* Pointer to Lut object */
double *out, /* Output array[inputChan] */
double *in /* Input array[inputChan] */
) {
int rv = 0;
unsigned int ix, n;
double inputEnt_1 = (double)(p->inputEnt-1);
double *table = p->inputTable;
if (p->inputEnt == 0) { /* Hmm. */
for (n = 0; n < p->inputChan; n++)
out[n] = in[n];
} else {
/* Use linear interpolation */
for (n = 0; n < p->inputChan; n++, table += p->inputEnt) {
double val, w;
val = in[n] * inputEnt_1;
if (val < 0.0) {
val = 0.0;
rv |= 1;
} else if (val > inputEnt_1) {
val = inputEnt_1;
rv |= 1;
}
ix = (unsigned int)floor(val); /* Grid coordinate */
if (ix > (p->inputEnt-2))
ix = (p->inputEnt-2);
w = val - (double)ix; /* weight */
val = table[ix];
out[n] = val + w * (table[ix+1] - val);
}
}
return rv;
}
/* Convert normalized numbers though this Luts multi-dimensional table. */
/* using multi-linear interpolation. */
static int icmLut_lookup_clut_nl(
/* Return 0 on success, 1 if clipping occured, 2 on other error */
icmLut *p, /* Pointer to Lut object */
double *out, /* Output array[inputChan] */
double *in /* Input array[outputChan] */
) {
icc *icp = p->icp;
int rv = 0;
double *gp; /* Pointer to grid cube base */
double co[MAX_CHAN]; /* Coordinate offset with the grid cell */
double *gw, GW[1 << 8]; /* weight for each grid cube corner */
if (p->inputChan <= 8) {
gw = GW; /* Use stack allocation */
} else {
if ((gw = (double *) icp->al->malloc(icp->al, sat_mul((1 << p->inputChan), sizeof(double)))) == NULL) {
sprintf(icp->err,"icmLut_lookup_clut: malloc() failed");
return icp->errc = 2;
}
}
/* We are using an multi-linear (ie. Trilinear for 3D input) interpolation. */
/* The implementation here uses more multiplies that some other schemes, */
/* (for instance, see "Tri-Linear Interpolation" by Steve Hill, */
/* Graphics Gems IV, page 521), but has less involved bookeeping, */
/* needs less local storage for intermediate output values, does fewer */
/* output and intermediate value reads, and fp multiplies are fast on */
/* todays processors! */
/* Compute base index into grid and coordinate offsets */
{
unsigned int e;
double clutPoints_1 = (double)(p->clutPoints-1);
int clutPoints_2 = p->clutPoints-2;
gp = p->clutTable; /* Base of grid array */
for (e = 0; e < p->inputChan; e++) {
unsigned int x;
double val;
val = in[e] * clutPoints_1;
if (val < 0.0) {
val = 0.0;
rv |= 1;
} else if (val > clutPoints_1) {
val = clutPoints_1;
rv |= 1;
}
x = (unsigned int)floor(val); /* Grid coordinate */
if (x > clutPoints_2)
x = clutPoints_2;
co[e] = val - (double)x; /* 1.0 - weight */
gp += x * p->dinc[e]; /* Add index offset for base of cube */
}
}
/* Compute corner weights needed for interpolation */
{
unsigned int e;
int i, g = 1;
gw[0] = 1.0;
for (e = 0; e < p->inputChan; e++) {
for (i = 0; i < g; i++) {
gw[g+i] = gw[i] * co[e];
gw[i] *= (1.0 - co[e]);
}
g *= 2;
}
}
/* Now compute the output values */
{
int i;
unsigned int f;
double w = gw[0];
double *d = gp + p->dcube[0];
for (f = 0; f < p->outputChan; f++) /* Base of cube */
out[f] = w * d[f];
for (i = 1; i < (1 << p->inputChan); i++) { /* For all other corners of cube */
w = gw[i]; /* Strength reduce */
d = gp + p->dcube[i];
for (f = 0; f < p->outputChan; f++)
out[f] += w * d[f];
}
}
if (gw != GW)
icp->al->free(icp->al, (void *)gw);
return rv;
}
/* Convert normalized numbers though this Luts multi-dimensional table */
/* using simplex interpolation. */
static int icmLut_lookup_clut_sx(
/* Return 0 on success, 1 if clipping occured, 2 on other error */
icmLut *p, /* Pointer to Lut object */
double *out, /* Output array[inputChan] */
double *in /* Input array[outputChan] */
) {
int rv = 0;
double *gp; /* Pointer to grid cube base */
double co[MAX_CHAN]; /* Coordinate offset with the grid cell */
int si[MAX_CHAN]; /* co[] Sort index, [0] = smallest */
/* We are using a simplex (ie. tetrahedral for 3D input) interpolation. */
/* This method is more appropriate for XYZ/RGB/CMYK input spaces, */
/* Compute base index into grid and coordinate offsets */
{
unsigned int e;
double clutPoints_1 = (double)(p->clutPoints-1);
int clutPoints_2 = p->clutPoints-2;
gp = p->clutTable; /* Base of grid array */
for (e = 0; e < p->inputChan; e++) {
unsigned int x;
double val;
val = in[e] * clutPoints_1;
if (val < 0.0) {
val = 0.0;
rv |= 1;
} else if (val > clutPoints_1) {
val = clutPoints_1;
rv |= 1;
}
x = (unsigned int)floor(val); /* Grid coordinate */
if (x > clutPoints_2)
x = clutPoints_2;
co[e] = val - (double)x; /* 1.0 - weight */
gp += x * p->dinc[e]; /* Add index offset for base of cube */
}
}
#ifdef NEVER
/* Do selection sort on coordinates, smallest to largest. */
{
int e, f;
for (e = 0; e < p->inputChan; e++)
si[e] = e; /* Initial unsorted indexes */
for (e = 0; e < (p->inputChan-1); e++) {
double cosn;
cosn = co[si[e]]; /* Current smallest value */
for (f = e+1; f < p->inputChan; f++) { /* Check against rest */
int tt;
tt = si[f];
if (cosn > co[tt]) {
si[f] = si[e]; /* Exchange */
si[e] = tt;
cosn = co[tt];
}
}
}
}
#else
/* Do insertion sort on coordinates, smallest to largest. */
{
int f, vf;
unsigned int e;
double v;
for (e = 0; e < p->inputChan; e++)
si[e] = e; /* Initial unsorted indexes */
for (e = 1; e < p->inputChan; e++) {
f = e;
v = co[si[f]];
vf = f;
while (f > 0 && co[si[f-1]] > v) {
si[f] = si[f-1];
f--;
}
si[f] = vf;
}
}
#endif
/* Now compute the weightings, simplex vertices and output values */
{
unsigned int e, f;
double w; /* Current vertex weight */
w = 1.0 - co[si[p->inputChan-1]]; /* Vertex at base of cell */
for (f = 0; f < p->outputChan; f++)
out[f] = w * gp[f];
for (e = p->inputChan-1; e > 0; e--) { /* Middle verticies */
w = co[si[e]] - co[si[e-1]];
gp += p->dinc[si[e]]; /* Move to top of cell in next largest dimension */
for (f = 0; f < p->outputChan; f++)
out[f] += w * gp[f];
}
w = co[si[0]];
gp += p->dinc[si[0]]; /* Far corner from base of cell */
for (f = 0; f < p->outputChan; f++)
out[f] += w * gp[f];
}
return rv;
}
/* Convert normalized numbers though this Luts per channel output tables. */
/* Return 0 on success, 1 if clipping occured, 2 on other error */
static int icmLut_lookup_output(
icmLut *p, /* Pointer to Lut object */
double *out, /* Output array[outputChan] */
double *in /* Input array[outputChan] */
) {
int rv = 0;
unsigned int ix, n;
double outputEnt_1 = (double)(p->outputEnt-1);
double *table = p->outputTable;
if (p->outputEnt == 0) { /* Hmm. */
for (n = 0; n < p->outputChan; n++)
out[n] = in[n];
} else {
/* Use linear interpolation */
for (n = 0; n < p->outputChan; n++, table += p->outputEnt) {
double val, w;
val = in[n] * outputEnt_1;
if (val < 0.0) {
val = 0.0;
rv |= 1;
} else if (val > outputEnt_1) {
val = outputEnt_1;
rv |= 1;
}
ix = (unsigned int)floor(val); /* Grid coordinate */
if (ix > (p->outputEnt-2))
ix = (p->outputEnt-2);
w = val - (double)ix; /* weight */
val = table[ix];
out[n] = val + w * (table[ix+1] - val);
}
}
return rv;
}
/* ----------------------------------------------- */
/* Tune a single interpolated value. Based on lookup_clut functions (above) */
/* Helper function to fine tune a single value interpolation */
/* Return 0 on success, 1 if input clipping occured, 2 if output clipping occured */
int icmLut_tune_value_nl(
icmLut *p, /* Pointer to Lut object */
double *out, /* Output array[inputChan] */
double *in /* Input array[outputChan] */
) {
icc *icp = p->icp;
int rv = 0;
double *gp; /* Pointer to grid cube base */
double co[MAX_CHAN]; /* Coordinate offset with the grid cell */
double *gw, GW[1 << 8]; /* weight for each grid cube corner */
double cout[MAX_CHAN]; /* Current output value */
if (p->inputChan <= 8) {
gw = GW; /* Use stack allocation */
} else {
if ((gw = (double *) icp->al->malloc(icp->al, sat_mul((1 << p->inputChan), sizeof(double)))) == NULL) {
sprintf(icp->err,"icmLut_lookup_clut: malloc() failed");
return icp->errc = 2;
}
}
/* We are using an multi-linear (ie. Trilinear for 3D input) interpolation. */
/* The implementation here uses more multiplies that some other schemes, */
/* (for instance, see "Tri-Linear Interpolation" by Steve Hill, */
/* Graphics Gems IV, page 521), but has less involved bookeeping, */
/* needs less local storage for intermediate output values, does fewer */
/* output and intermediate value reads, and fp multiplies are fast on */
/* todays processors! */
/* Compute base index into grid and coordinate offsets */
{
unsigned int e;
double clutPoints_1 = (double)(p->clutPoints-1);
int clutPoints_2 = p->clutPoints-2;
gp = p->clutTable; /* Base of grid array */
for (e = 0; e < p->inputChan; e++) {
unsigned int x;
double val;
val = in[e] * clutPoints_1;
if (val < 0.0) {
val = 0.0;
rv |= 1;
} else if (val > clutPoints_1) {
val = clutPoints_1;
rv |= 1;
}
x = (unsigned int)floor(val); /* Grid coordinate */
if (x > clutPoints_2)
x = clutPoints_2;
co[e] = val - (double)x; /* 1.0 - weight */
gp += x * p->dinc[e]; /* Add index offset for base of cube */
}
}
/* Compute corner weights needed for interpolation */
{
unsigned int e;
int i, g = 1;
gw[0] = 1.0;
for (e = 0; e < p->inputChan; e++) {
for (i = 0; i < g; i++) {
gw[g+i] = gw[i] * co[e];
gw[i] *= (1.0 - co[e]);
}
g *= 2;
}
}
/* Now compute the current output value, and distribute the correction */
{
int i;
unsigned int f;
double w, *d, ww = 0.0;
for (f = 0; f < p->outputChan; f++)
cout[f] = 0.0;
for (i = 0; i < (1 << p->inputChan); i++) { /* For all other corners of cube */
w = gw[i]; /* Strength reduce */
ww += w * w; /* Sum of weights squared */
d = gp + p->dcube[i];
for (f = 0; f < p->outputChan; f++)
cout[f] += w * d[f];
}
/* We distribute the correction needed in proportion to the */
/* interpolation weighting, so the biggest correction is to the */
/* closest vertex. */
for (f = 0; f < p->outputChan; f++)
cout[f] = (out[f] - cout[f])/ww; /* Amount to distribute */
for (i = 0; i < (1 << p->inputChan); i++) { /* For all other corners of cube */
w = gw[i]; /* Strength reduce */
d = gp + p->dcube[i];
for (f = 0; f < p->outputChan; f++) {
d[f] += w * cout[f]; /* Apply correction */
if (d[f] < 0.0) {
d[f] = 0.0;
rv |= 2;
} else if (d[f] > 1.0) {
d[f] = 1.0;
rv |= 2;
}
}
}
}
if (gw != GW)
icp->al->free(icp->al, (void *)gw);
return rv;
}
/* Helper function to fine tune a single value interpolation */
/* Return 0 on success, 1 if input clipping occured, 2 if output clipping occured */
int icmLut_tune_value_sx(
icmLut *p, /* Pointer to Lut object */
double *out, /* Output array[inputChan] */
double *in /* Input array[outputChan] */
) {
int rv = 0;
double *gp; /* Pointer to grid cube base */
double co[MAX_CHAN]; /* Coordinate offset with the grid cell */
int si[MAX_CHAN]; /* co[] Sort index, [0] = smallest */
/* We are using a simplex (ie. tetrahedral for 3D input) interpolation. */
/* This method is more appropriate for XYZ/RGB/CMYK input spaces, */
/* Compute base index into grid and coordinate offsets */
{
unsigned int e;
double clutPoints_1 = (double)(p->clutPoints-1);
int clutPoints_2 = p->clutPoints-2;
gp = p->clutTable; /* Base of grid array */
for (e = 0; e < p->inputChan; e++) {
unsigned int x;
double val;
val = in[e] * clutPoints_1;
if (val < 0.0) {
val = 0.0;
rv |= 1;
} else if (val > clutPoints_1) {
val = clutPoints_1;
rv |= 1;
}
x = (unsigned int)floor(val); /* Grid coordinate */
if (x > clutPoints_2)
x = clutPoints_2;
co[e] = val - (double)x; /* 1.0 - weight */
gp += x * p->dinc[e]; /* Add index offset for base of cube */
}
}
/* Do insertion sort on coordinates, smallest to largest. */
{
int f, vf;
unsigned int e;
double v;
for (e = 0; e < p->inputChan; e++)
si[e] = e; /* Initial unsorted indexes */
for (e = 1; e < p->inputChan; e++) {
f = e;
v = co[si[f]];
vf = f;
while (f > 0 && co[si[f-1]] > v) {
si[f] = si[f-1];
f--;
}
si[f] = vf;
}
}
/* Now compute the current output value, and distribute the correction */
{
unsigned int e, f;
double w, ww = 0.0; /* Current vertex weight, sum of weights squared */
double cout[MAX_CHAN]; /* Current output value */
double *ogp = gp; /* Pointer to grid cube base */
w = 1.0 - co[si[p->inputChan-1]]; /* Vertex at base of cell */
ww += w * w; /* Sum of weights squared */
for (f = 0; f < p->outputChan; f++)
cout[f] = w * gp[f];
for (e = p->inputChan-1; e > 0; e--) { /* Middle verticies */
w = co[si[e]] - co[si[e-1]];
ww += w * w; /* Sum of weights squared */
gp += p->dinc[si[e]]; /* Move to top of cell in next largest dimension */
for (f = 0; f < p->outputChan; f++)
cout[f] += w * gp[f];
}
w = co[si[0]];
ww += w * w; /* Sum of weights squared */
gp += p->dinc[si[0]]; /* Far corner from base of cell */
for (f = 0; f < p->outputChan; f++)
cout[f] += w * gp[f];
/* We distribute the correction needed in proportion to the */
/* interpolation weighting, so the biggest correction is to the */
/* closest vertex. */
for (f = 0; f < p->outputChan; f++)
cout[f] = (out[f] - cout[f])/ww; /* Amount to distribute */
gp = ogp;
w = 1.0 - co[si[p->inputChan-1]]; /* Vertex at base of cell */
for (f = 0; f < p->outputChan; f++) {
gp[f] += w * cout[f]; /* Apply correction */
if (gp[f] < 0.0) {
gp[f] = 0.0;
rv |= 2;
} else if (gp[f] > 1.0) {
gp[f] = 1.0;
rv |= 2;
}
}
for (e = p->inputChan-1; e > 0; e--) { /* Middle verticies */
w = co[si[e]] - co[si[e-1]];
gp += p->dinc[si[e]]; /* Move to top of cell in next largest dimension */
for (f = 0; f < p->outputChan; f++) {
gp[f] += w * cout[f]; /* Apply correction */
if (gp[f] < 0.0) {
gp[f] = 0.0;
rv |= 2;
} else if (gp[f] > 1.0) {
gp[f] = 1.0;
rv |= 2;
}
}
}
w = co[si[0]];
gp += p->dinc[si[0]]; /* Far corner from base of cell */
for (f = 0; f < p->outputChan; f++) {
gp[f] += w * cout[f]; /* Apply correction */
if (gp[f] < 0.0) {
gp[f] = 0.0;
rv |= 2;
} else if (gp[f] > 1.0) {
gp[f] = 1.0;
rv |= 2;
}
}
}
return rv;
}
/* ----------------------------------------------- */
/* Pseudo - Hilbert count sequencer */
/* This multi-dimensional count sequence is a distributed */
/* Gray code sequence, with direction reversal on every */
/* alternate power of 2 scale. */
/* It is intended to aid cache coherence in multi-dimensional */
/* regular sampling. It approximates the Hilbert curve sequence. */
/* Initialise, returns total usable count */
unsigned
psh_init(
psh *p, /* Pointer to structure to initialise */
int di, /* Dimensionality */
unsigned int res, /* Size per coordinate */
int co[] /* Coordinates to initialise (May be NULL) */
) {
int e;
p->di = di;
p->res = res;
/* Compute bits */
for (p->bits = 0; (1u << p->bits) < res; p->bits++)
;
/* Compute the total count mask */
p->tmask = ((((unsigned)1) << (p->bits * di))-1);
/* Compute usable count */
p->count = 1;
for (e = 0; e < di; e++)
p->count *= res;
p->ix = 0;
if (co != NULL) {
for (e = 0; e < di; e++)
co[e] = 0;
}
return p->count;
}
/* Reset the counter */
void
psh_reset(
psh *p /* Pointer to structure */
) {
p->ix = 0;
}
/* Increment pseudo-hilbert coordinates */
/* Return non-zero if count rolls over to 0 */
int
psh_inc(
psh *p, /* Pointer to structure */
int co[] /* Coordinates to return */
) {
int di = p->di;
unsigned int res = p->res;
unsigned int bits = p->bits;
int e;
do {
unsigned int b;
int gix; /* Gray code index */
p->ix = (p->ix + 1) & p->tmask;
gix = p->ix ^ (p->ix >> 1); /* Convert to gray code index */
for (e = 0; e < di; e++)
co[e] = 0;
for (b = 0; b < bits; b++) { /* Distribute bits */
if (b & 1) {
for (e = di-1; e >= 0; e--) { /* In reverse order */
co[e] |= (gix & 1) << b;
gix >>= 1;
}
} else {
for (e = 0; e < di; e++) { /* In normal order */
co[e] |= (gix & 1) << b;
gix >>= 1;
}
}
}
/* Convert from Gray to binary coordinates */
for (e = 0; e < di; e++) {
unsigned int sh, tv;
for(sh = 1, tv = co[e];; sh <<= 1) {
unsigned ptv = tv;
tv ^= (tv >> sh);
if (ptv <= 1 || sh == 16)
break;
}
if (tv >= res) /* Dumbo filter - increment again if outside cube range */
break;
co[e] = tv;
}
} while (e < di);
return (p->ix == 0);
}
/* ------------------------------------------------------- */
#ifndef COUNTERS_H
/* Macros for a multi-dimensional counter. */
/* Declare the counter name nn, maximum di mxdi, dimensions di, & count */
/* This counter can have each dimension range clipped */
#define FCOUNT(nn, mxdi, di) \
int nn[mxdi]; /* counter value */ \
int nn##_di = (di); /* Number of dimensions */ \
int nn##_stt[mxdi]; /* start count value */ \
int nn##_res[mxdi]; /* last count +1 */ \
int nn##_e /* dimension index */
#define FRECONF(nn, start, endp1) \
for (nn##_e = 0; nn##_e < nn##_di; nn##_e++) { \
nn##_stt[nn##_e] = (start); /* start count value */ \
nn##_res[nn##_e] = (endp1); /* last count +1 */ \
}
/* Set the counter value to 0 */
#define FC_INIT(nn) \
{ \
for (nn##_e = 0; nn##_e < nn##_di; nn##_e++) \
nn[nn##_e] = nn##_stt[nn##_e]; \
nn##_e = 0; \
}
/* Increment the counter value */
#define FC_INC(nn) \
{ \
for (nn##_e = 0; nn##_e < nn##_di; nn##_e++) { \
nn[nn##_e]++; \
if (nn[nn##_e] < nn##_res[nn##_e]) \
break; /* No carry */ \
nn[nn##_e] = nn##_stt[nn##_e]; \
} \
}
/* After increment, expression is TRUE if counter is done */
#define FC_DONE(nn) \
(nn##_e >= nn##_di)
#endif /* COUNTERS_H */
/* Parameter to getNormFunc function */
typedef enum {
icmFromLuti = 0, /* return "fromo Lut normalized index" conversion function */
icmToLuti = 1, /* return "to Lut normalized index" conversion function */
icmFromLutv = 2, /* return "from Lut normalized value" conversion function */
icmToLutv = 3 /* return "to Lut normalized value" conversion function */
} icmNormFlag;
/* Return an appropriate color space normalization function, */
/* given the color space and Lut type */
/* Return 0 on success, 1 on match failure */
static int getNormFunc(
icc *icp,
icColorSpaceSignature csig,
icTagTypeSignature tagSig,
icmNormFlag flag,
void (**nfunc)(double *out, double *in)
);
#define CLIP_MARGIN 0.005 /* Margine to allow before reporting clipping = 0.5% */
/* NOTE that ICM_CLUT_SET_FILTER turns out to be not very useful, */
/* as it can result in reversals. Could #ifdef out the code ?? */
/* Helper function to set multiple Lut tables simultaneously. */
/* Note that these tables all have to be compatible in */
/* having the same configuration and resolution. */
/* Set errc and return error number in underlying icc */
/* Set warnc if there is clipping in the output values: */
/* 1 = input table, 2 = main clut, 3 = clut midpoint, 4 = midpoint interp, 5 = output table */
/* Note that clutfunc in[] value has "index under", ie: */
/* at ((int *)in)[-chan-1], and for primary grid is simply the */
/* grid index (ie. 5,3,8), and for the center of cells grid, is */
/* the -index-1, ie. -6,-3,-8 */
int icmSetMultiLutTables(
int ntables, /* Number of tables to be set, 1..n */
icmLut **pp, /* Pointer to array of Lut objects */
int flags, /* Setting flags */
void *cbctx, /* Opaque callback context pointer value */
icColorSpaceSignature insig, /* Input color space */
icColorSpaceSignature outsig, /* Output color space */
void (*infunc)(void *cbctx, double *out, double *in),
/* Input transfer function, inspace->inspace' (NULL = default) */
/* Will be called ntables times for each input grid value */
double *inmin, double *inmax, /* Maximum range of inspace' values */
/* (NULL = default) */
void (*clutfunc)(void *cbntx, double *out, double *in),
/* inspace' -> outspace[ntables]' transfer function */
/* will be called once for each input' grid value, and */
/* ntables output values should be written consecutively */
/* to out[]. */
double *clutmin, double *clutmax, /* Maximum range of outspace' values */
/* (NULL = default) */
void (*outfunc)(void *cbntx, double *out, double *in),
/* Output transfer function, outspace'->outspace (NULL = deflt) */
/* Will be called ntables times on each output value */
int *apxls_gmin, int *apxls_gmax /* If not NULL, the grid indexes not to be affected */
/* by ICM_CLUT_SET_APXLS, defaulting to 0..>clutPoints-1 */
) {
icmLut *p, *pn; /* Pointer to 0'th nd tn'th Lut object */
icc *icp; /* Pointer to common icc */
int tn;
unsigned int e, f, i, n;
double **clutTable2 = NULL; /* Cell center values for ICM_CLUT_SET_APXLS */
double *clutTable3 = NULL; /* Vertex smoothing radius values [ntables] per entry */
int dinc3[MAX_CHAN]; /* Dimensional increment through clut3 (in doubles) */
int dcube3[1 << MAX_CHAN]; /* Hyper cube offsets throught clut3 (in doubles) */
int ii[MAX_CHAN]; /* Index value */
psh counter; /* Pseudo-Hilbert counter */
// double _iv[4 * MAX_CHAN], *iv = &_iv[MAX_CHAN], *ivn; /* Real index value/table value */
int maxchan; /* Actual max of input and output */
double *_iv, *iv, *ivn; /* Real index value/table value */
double imin[MAX_CHAN], imax[MAX_CHAN];
double omin[MAX_CHAN], omax[MAX_CHAN];
int def_apxls_gmin[MAX_CHAN], def_apxls_gmax[MAX_CHAN];
void (*ifromindex)(double *out, double *in); /* Index to input color space function */
void (*itoentry)(double *out, double *in); /* Input color space to entry function */
void (*ifromentry)(double *out, double *in); /* Entry to input color space function */
void (*otoentry)(double *out, double *in); /* Output colorspace to table value function */
void (*ofromentry)(double *out, double *in); /* Table value to output color space function */
int clip = 0;
/* Check that everything is OK to proceed */
if (ntables < 1 || ntables > MAX_CHAN) {
if (ntables >= 1) {
icp = pp[0]->icp;
sprintf(icp->err,"icmSetMultiLutTables has illegal number of tables %d",ntables);
return icp->errc = 1;
} else {
/* Can't write error message anywhere */
return 1;
}
}
p = pp[0];
icp = p->icp;
for (tn = 1; tn < ntables; tn++) {
if (pp[tn]->icp != icp) {
sprintf(icp->err,"icmSetMultiLutTables Tables base icc is different");
return icp->errc = 1;
}
if (pp[tn]->ttype != p->ttype) {
sprintf(icp->err,"icmSetMultiLutTables Tables have different Tag Type");
return icp->errc = 1;
}
if (pp[tn]->inputChan != p->inputChan) {
sprintf(icp->err,"icmSetMultiLutTables Tables have different inputChan");
return icp->errc = 1;
}
if (pp[tn]->outputChan != p->outputChan) {
sprintf(icp->err,"icmSetMultiLutTables Tables have different outputChan");
return icp->errc = 1;
}
if (pp[tn]->clutPoints != p->clutPoints) {
sprintf(icp->err,"icmSetMultiLutTables Tables have different clutPoints");
return icp->errc = 1;
}
}
if (getNormFunc(icp, insig, p->ttype, icmFromLuti, &ifromindex) != 0) {
sprintf(icp->err,"icmLut_set_tables index to input colorspace function lookup failed");
return icp->errc = 1;
}
if (getNormFunc(icp, insig, p->ttype, icmToLutv, &itoentry) != 0) {
sprintf(icp->err,"icmLut_set_tables input colorspace to table entry function lookup failed");
return icp->errc = 1;
}
if (getNormFunc(icp, insig, p->ttype, icmFromLutv, &ifromentry) != 0) {
sprintf(icp->err,"icmLut_set_tables table entry to input colorspace function lookup failed");
return icp->errc = 1;
}
if (getNormFunc(icp, outsig, p->ttype, icmToLutv, &otoentry) != 0) {
sprintf(icp->err,"icmLut_set_tables output colorspace to table entry function lookup failed");
return icp->errc = 1;
}
if (getNormFunc(icp, outsig, p->ttype, icmFromLutv, &ofromentry) != 0) {
sprintf(icp->err,"icmLut_set_tables table entry to output colorspace function lookup failed");
return icp->errc = 1;
}
/* Allocate an array to hold the input and output values. */
/* It needs to be able to hold di "index under valus as in[], */
/* and ntables ICM_CLUT_SET_FILTER values as out[], so we assume maxchan >= di */
maxchan = p->inputChan > p->outputChan ? p->inputChan : p->outputChan;
if ((_iv = (double *) icp->al->malloc(icp->al, sizeof(double) * maxchan * (ntables+1)))
== NULL) {
sprintf(icp->err,"icmLut_read: malloc() failed");
return icp->errc = 2;
}
iv = _iv + maxchan; /* Allow for "index under" and smoothing radius values */
/* Setup input table value min-max */
if (inmin == NULL || inmax == NULL) {
#ifdef SYMETRICAL_DEFAULT_LAB_RANGE /* Symetrical default range. */
/* We are assuming V2 Lab16 encoding, since this is a lut16type that always uses */
/* this encoding */
if (insig == icSigLabData) { /* Special case Lab */
double mn[3], mx[3];
/* This is to ensure that Lab 100,0,0 maps exactly to a clut grid point. */
/* This should work well if there is an odd grid resolution, */
/* and icclib is being used, as input lookup will */
/* be computed using floating point, so that the CLUT input value */
/* 0.5 can be represented exactly. */
/* Because the symetric range will cause slight clipping, */
/* only do it if the input table has sufficient resolution */
/* to represent the clipping faithfuly. */
if (p->inputEnt >= 64) {
if (p->ttype == icSigLut8Type) {
mn[0] = 0.0, mn[1] = mn[2] = -127.0;
mx[0] = 100.0, mx[1] = mx[2] = 127.0;
} else {
mn[0] = 0.0, mn[1] = mn[2] = -127.0 - 255.0/256.0;
mx[0] = 100.0, mx[1] = mx[2] = 127.0 + 255.0/256.0;
}
itoentry(imin, mn); /* Convert from input color space to table representation */
itoentry(imax, mx);
} else {
for (e = 0; e < p->inputChan; e++) {
imin[e] = 0.0;
imax[e] = 1.0;
}
}
} else
#endif
{
for (e = 0; e < p->inputChan; e++) {
imin[e] = 0.0; /* We are assuming this is true for all other color spaces. */
imax[e] = 1.0;
}
}
} else {
itoentry(imin, inmin); /* Convert from input color space to table representation */
itoentry(imax, inmax);
}
/* Setup output table value min-max */
if (clutmin == NULL || clutmax == NULL) {
#ifdef SYMETRICAL_DEFAULT_LAB_RANGE
/* This really isn't doing much, since the full range encoding doesn't need */
/* any adjustment to map a*b* 0 to an integer value. */
/* We are tweaking the 16 bit L* = 100 to the last index into */
/* the output table, which may help its accuracy slightly. */
/* We are assuming V2 Lab16 encoding, since this is a lut16type that always uses */
/* this encoding */
if (outsig == icSigLabData) { /* Special case Lab */
double mn[3], mx[3];
/* The output of the CLUT will be an 8 or 16 bit value, and we want to */
/* adjust the range so that the input grid point holding the white */
/* point can encode 0.0 exactly. */
/* Note that in the case of the a & b values, the range equates to */
/* normalised 0.0 .. 1.0, since 0 can be represented exactly in it. */
if (p->outputEnt >= 64) {
if (p->ttype == icSigLut8Type) {
mn[0] = 0.0, mn[1] = mn[2] = -128.0;
mx[0] = 100.0, mx[1] = mx[2] = 127.0;
} else {
mn[0] = 0.0, mn[1] = mn[2] = -128.0;
mx[0] = 100.0, mx[1] = mx[2] = (65535.0 * 255.0)/65280.0 - 128.0;
}
otoentry(omin, mn);/* Convert from output color space to table representation */
otoentry(omax, mx);
} else {
for (e = 0; e < p->inputChan; e++) {
omin[e] = 0.0;
omax[e] = 1.0;
}
}
} else
#endif
{
for (f = 0; f < p->outputChan; f++) {
omin[f] = 0.0; /* We are assuming this is true for all other color spaces. */
omax[f] = 1.0;
}
}
} else {
otoentry(omin, clutmin);/* Convert from output color space to table representation */
otoentry(omax, clutmax);
}
/* Create the input table entry values */
for (tn = 0; tn < ntables; tn++) {
pn = pp[tn];
for (n = 0; n < pn->inputEnt; n++) {
double fv;
fv = n/(pn->inputEnt-1.0);
for (e = 0; e < pn->inputChan; e++)
iv[e] = fv;
ifromindex(iv,iv); /* Convert from index value to input color space value */
if (infunc != NULL)
infunc(cbctx, iv, iv); /* In colorspace -> input table -> In colorspace. */
itoentry(iv,iv); /* Convert from input color space value to table value */
/* Expand used range to 0.0 - 1.0, and clip to legal values */
/* Note that if the range is reduced, and clipping occurs, */
/* then there should be enough resolution within the input */
/* table, to represent the sharp edges of the clipping. */
for (e = 0; e < pn->inputChan; e++) {
double tt;
tt = (iv[e] - imin[e])/(imax[e] - imin[e]);
if (tt < 0.0) {
DBGSLC(("iclip: tt = %f, iv = %f, omin = %f, omax = %f\n",tt,iv[e],omin[e],omax[e]));
if (tt < -CLIP_MARGIN)
clip = 1;
tt = 0.0;
} else if (tt > 1.0) {
DBGSLC(("iclip: tt = %f, iv = %f, omin = %f, omax = %f\n",tt,iv[e],omin[e],omax[e]));
if (tt > (1.0 + CLIP_MARGIN))
clip = 1;
tt = 1.0;
}
iv[e] = tt;
}
for (e = 0; e < pn->inputChan; e++) /* Input tables */
pn->inputTable[e * pn->inputEnt + n] = iv[e];
}
}
/* Allocate space for cell center value lookup */
if (flags & ICM_CLUT_SET_APXLS) {
if (apxls_gmin == NULL) {
apxls_gmin = def_apxls_gmin;
for (e = 0; e < p->inputChan; e++)
apxls_gmin[e] = 0;
}
if (apxls_gmax == NULL) {
apxls_gmax = def_apxls_gmax;
for (e = 0; e < p->inputChan; e++)
apxls_gmax[e] = p->clutPoints-1;
}
if ((clutTable2 = (double **) icp->al->calloc(icp->al,sizeof(double *), ntables)) == NULL) {
sprintf(icp->err,"icmLut_set_tables malloc of cube center array failed");
icp->al->free(icp->al, _iv);
return icp->errc = 1;
}
for (tn = 0; tn < ntables; tn++) {
if ((clutTable2[tn] = (double *) icp->al->calloc(icp->al,sizeof(double),
p->clutTable_size)) == NULL) {
for (--tn; tn >= 0; tn--)
icp->al->free(icp->al, clutTable2[tn]);
icp->al->free(icp->al, _iv);
icp->al->free(icp->al, clutTable2);
sprintf(icp->err,"icmLut_set_tables malloc of cube center array failed");
return icp->errc = 1;
}
}
}
/* Allocate space for smoothing radius values */
if (flags & ICM_CLUT_SET_FILTER) {
unsigned int j, g, size;
/* Private: compute dimensional increment though clut3 */
i = p->inputChan-1;
dinc3[i--] = ntables;
for (; i < p->inputChan; i--)
dinc3[i] = dinc3[i+1] * p->clutPoints;
/* Private: compute offsets from base of cube to other corners */
for (dcube3[0] = 0, g = 1, j = 0; j < p->inputChan; j++) {
for (i = 0; i < g; i++)
dcube3[g+i] = dcube3[i] + dinc3[j];
g *= 2;
}
if ((size = sat_mul(ntables, sat_pow(p->clutPoints,p->inputChan))) == UINT_MAX) {
sprintf(icp->err,"icmLut_alloc size overflow");
if (flags & ICM_CLUT_SET_APXLS) {
for (tn = 0; tn < ntables; tn++)
icp->al->free(icp->al, clutTable2[tn]);
}
icp->al->free(icp->al, clutTable2);
icp->al->free(icp->al, _iv);
return icp->errc = 1;
}
if ((clutTable3 = (double *) icp->al->calloc(icp->al,sizeof(double),
size)) == NULL) {
if (flags & ICM_CLUT_SET_APXLS) {
for (tn = 0; tn < ntables; tn++)
icp->al->free(icp->al, clutTable2[tn]);
}
icp->al->free(icp->al, clutTable2);
icp->al->free(icp->al, _iv);
sprintf(icp->err,"icmLut_set_tables malloc of vertex smoothing value array failed");
return icp->errc = 1;
}
}
/* Create the multi-dimensional lookup table values */
/* To make this clut function cache friendly, we use the pseudo-hilbert */
/* count sequence. This keeps each point close to the last in the */
/* multi-dimensional space. This is the point of setting multiple Luts at */
/* once too - the assumption is that these tables are all related (different */
/* gamut compressions for instance), and hence calling the clutfunc() with */
/* close values will maximise reverse lookup cache hit rate. */
psh_init(&counter, p->inputChan, p->clutPoints, ii); /* Initialise counter */
/* Itterate through all verticies in the grid */
for (;;) {
int ti, ti3; /* Table indexes */
for (ti = e = 0; e < p->inputChan; e++) { /* Input tables */
ti += ii[e] * p->dinc[e]; /* Clut index */
iv[e] = ii[e]/(p->clutPoints-1.0); /* Vertex coordinates */
iv[e] = iv[e] * (imax[e] - imin[e]) + imin[e]; /* Undo expansion to 0.0 - 1.0 */
*((int *)&iv[-((int)e)-1]) = ii[e]; /* Trick to supply grid index in iv[] */
}
if (flags & ICM_CLUT_SET_FILTER) {
for (ti3 = e = 0; e < p->inputChan; e++) /* Input tables */
ti3 += ii[e] * dinc3[e]; /* Clut3 index */
}
DBGSL(("\nix %s\n",icmPiv(p->inputChan, ii)));
DBGSL(("raw itv %s to iv'",icmPdv(p->inputChan, iv)));
ifromentry(iv,iv); /* Convert from table value to input color space */
DBGSL((" %s\n",icmPdv(p->inputChan, iv)));
/* Apply incolor -> outcolor function we want to represent for all tables */
DBGSL(("iv: %s to ov'",icmPdv(p->inputChan, iv)));
clutfunc(cbctx, iv, iv);
DBGSL((" %s\n",icmPdv(p->outputChan, iv)));
/* Save the results to the output tables */
for (tn = 0, ivn = iv; tn < ntables; ivn += p->outputChan, tn++) {
pn = pp[tn];
DBGSL(("tn %d, ov' %s -> otv",tn,icmPdv(p->outputChan, ivn)));
otoentry(ivn,ivn); /* Convert from output color space value to table value */
DBGSL((" %s\n -> oval",icmPdv(p->outputChan, ivn)));
/* Expand used range to 0.0 - 1.0, and clip to legal values */
for (f = 0; f < pn->outputChan; f++) {
double tt;
tt = (ivn[f] - omin[f])/(omax[f] - omin[f]);
if (tt < 0.0) {
DBGSLC(("lclip: tt = %f, ivn= %f, omin = %f, omax = %f\n",tt,ivn[f],omin[f],omax[f]));
if (tt < -CLIP_MARGIN)
clip = 2;
tt = 0.0;
} else if (tt > 1.0) {
DBGSLC(("lclip: tt = %f, ivn= %f, omin = %f, omax = %f\n",tt,ivn[f],omin[f],omax[f]));
if (tt > (1.0 + CLIP_MARGIN))
clip = 2;
tt = 1.0;
}
ivn[f] = tt;
}
for (f = 0; f < pn->outputChan; f++) /* Output chans */
pn->clutTable[ti + f] = ivn[f];
DBGSL((" %s\n",icmPdv(pn->outputChan, ivn)));
if (flags & ICM_CLUT_SET_FILTER) {
clutTable3[ti3 + tn] = iv[-1 -tn]; /* Filter radiuses */
}
}
/* Lookup cell center value if ICM_CLUT_SET_APXLS */
if (clutTable2 != NULL) {
for (e = 0; e < p->inputChan; e++) {
if (ii[e] < apxls_gmin[e]
|| ii[e] >= apxls_gmax[e])
break; /* Don't lookup outside least squares area */
iv[e] = (ii[e] + 0.5)/(p->clutPoints-1.0); /* Vertex coordinates + 0.5 */
iv[e] = iv[e] * (imax[e] - imin[e]) + imin[e]; /* Undo expansion to 0.0 - 1.0 */
*((int *)&iv[-((int)e)-1]) = -ii[e]-1; /* Trick to supply -ve grid index in iv[] */
/* (Not this is only the base for +0.5 center) */
}
if (e >= p->inputChan) { /* We're not on the last row */
ifromentry(iv,iv); /* Convert from table value to input color space */
/* Apply incolor -> outcolor function we want to represent */
clutfunc(cbctx, iv, iv);
/* Save the results to the output tables */
for (tn = 0, ivn = iv; tn < ntables; ivn += p->outputChan, tn++) {
pn = pp[tn];
otoentry(ivn,ivn); /* Convert from output color space value to table value */
/* Expand used range to 0.0 - 1.0, and clip to legal values */
for (f = 0; f < pn->outputChan; f++) {
double tt;
tt = (ivn[f] - omin[f])/(omax[f] - omin[f]);
if (tt < 0.0) {
DBGSLC(("lclip: tt = %f, ivn= %f, omin = %f, omax = %f\n",tt,ivn[f],omin[f],omax[f]));
if (tt < -CLIP_MARGIN)
clip = 3;
tt = 0.0;
} else if (tt > 1.0) {
DBGSLC(("lclip: tt = %f, ivn= %f, omin = %f, omax = %f\n",tt,ivn[f],omin[f],omax[f]));
if (tt > (1.0 + CLIP_MARGIN))
clip = 3;
tt = 1.0;
}
ivn[f] = tt;
}
for (f = 0; f < pn->outputChan; f++) /* Output chans */
clutTable2[tn][ti + f] = ivn[f];
}
}
}
/* Increment index within block (Reverse index significancd) */
if (psh_inc(&counter, ii))
break;
}
#define APXLS_WHT 0.5
#define APXLS_DIFF_THRHESH 0.2
/* Deal with cell center value, aproximate least squares adjustment. */
/* Subtract some of the mean of the surrounding center values from each grid value. */
/* Skip the range edges so that things like the white point or Video sync are not changed. */
/* Avoid modifying the value if the difference between the */
/* interpolated value and the current value is too great, */
/* and there is the possibility of different color aliases. */
if (clutTable2 != NULL) {
int ti; /* cube vertex table index */
int ti2; /* cube center table2 index */
int ee;
double cw = 1.0/(double)(1 << p->inputChan); /* Weight for each cube corner */
/* For each cell center point except last row because we access ii[e]+1 */
for (e = 0; e < p->inputChan; e++)
ii[e] = apxls_gmin[e]; /* init coords */
/* Compute linear interpolated value from center values */
for (ee = 0; ee < p->inputChan;) {
/* Compute base index for table2 */
for (ti2 = e = 0; e < p->inputChan; e++) /* Input tables */
ti2 += ii[e] * p->dinc[e]; /* Clut index */
ti = ti2 + p->dcube[(1 << p->inputChan)-1]; /* +1 to each coord for vertex index */
for (tn = 0; tn < ntables; tn++) {
double mval[MAX_CHAN], vv;
double maxd = 0.0;
pn = pp[tn];
/* Compute mean of center values */
for (f = 0; f < pn->outputChan; f++) { /* Output chans */
mval[f] = 0.0;
for (i = 0; i < (1 << p->inputChan); i++) { /* For surrounding center values */
mval[f] += clutTable2[tn][ti2 + p->dcube[i] + f];
}
mval[f] = pn->clutTable[ti + f] - mval[f] * cw; /* Diff to mean */
vv = fabs(mval[f]);
if (vv > maxd)
maxd = vv;
}
if (pn->outputChan <= 3 || maxd < APXLS_DIFF_THRHESH) {
for (f = 0; f < pn->outputChan; f++) { /* Output chans */
vv = pn->clutTable[ti + f] + APXLS_WHT * mval[f];
/* Hmm. This is a bit crude. How do we know valid range is 0-1 ? */
/* What about an ink limit ? */
if (vv < 0.0) {
vv = 0.0;
} else if (vv > 1.0) {
vv = 1.0;
}
pn->clutTable[ti + f] = vv;
}
DBGSL(("nix %s apxls ov %s\n",icmPiv(p->inputChan, ii), icmPdv(pn->outputChan, ivn)));
}
}
/* Increment coord */
for (ee = 0; ee < p->inputChan; ee++) {
if (++ii[ee] < (apxls_gmax[ee]-1)) /* Stop short of upper row of clutTable2 */
break; /* No carry */
ii[ee] = apxls_gmin[ee];
}
}
/* Done with center values */
for (tn = 0; tn < ntables; tn++)
icp->al->free(icp->al, clutTable2[tn]);
icp->al->free(icp->al, clutTable2);
}
/* Apply any smoothing in the clipped region to the resulting clutTable */
/* !!! should avoid smoothing outside apxls_gmin[e] & apxls_gmax[e] region !!! */
if (clutTable3 != NULL) {
double *clutTable1; /* Copy of current unfilted values */
FCOUNT(cc, MAX_CHAN, p->inputChan); /* Surrounding counter */
if ((clutTable1 = (double *) icp->al->calloc(icp->al,sizeof(double),
p->clutTable_size)) == NULL) {
icp->al->free(icp->al, clutTable3);
icp->al->free(icp->al, _iv);
sprintf(icp->err,"icmLut_set_tables malloc of grid copy failed");
return icp->errc = 1;
}
for (tn = 0; tn < ntables; tn++) {
int aa;
int ee;
int ti, ti3; /* Table indexes */
pn = pp[tn];
/* For each pass */
for (aa = 0; aa < 2; aa++) {
/* Copy current values */
memcpy(clutTable1, pn->clutTable, sizeof(double) * pn->clutTable_size);
/* Filter each point */
for (e = 0; e < pn->inputChan; e++)
ii[e] = 0; /* init coords */
/* Compute linear interpolated error to actual cell center value */
for (ee = 0; ee < pn->inputChan;) {
double rr; /* Filter radius */
int ir; /* Integer radius */
double tw; /* Total weight */
/* Compute base index for this cell */
for (ti3 = ti = e = 0; e < pn->inputChan; e++) { /* Input tables */
ti += ii[e] * pn->dinc[e]; /* Clut index */
ti3 += ii[e] * dinc3[e]; /* Clut3 index */
}
rr = clutTable3[ti3 + tn] * (pn->clutPoints-1.0);
ir = (int)floor(rr + 0.5); /* Don't bother unless 1/2 over vertex */
if (ir < 1)
goto next_vert;
//FRECONF(cc, -ir, ir + 1); /* Set size of surroundign grid */
/* Clip scanning cube to be within grid */
for (e = 0; e < pn->inputChan; e++) {
int cr = ir;
if ((ii[e] - ir) < 0)
cr = ii[e];
if ((ii[e] + ir) >= pn->clutPoints)
cr = pn->clutPoints -1 -ii[e];
cc_stt[e] = -cr;
cc_res[e] = cr + 1;
}
for (f = 0; f < pn->outputChan; f++)
pn->clutTable[ti + f] = 0.0;
tw = 0.0;
FC_INIT(cc)
for (tw = 0.0; !FC_DONE(cc);) {
double r;
int tti;
/* Radius of this cell */
for (r = 0.0, tti = e = 0; e < pn->inputChan; e++) {
int ix;
r += cc[e] * cc[e];
tti += (ii[e] + cc[e]) * p->dinc[e];
}
r = sqrt(r);
if (r <= rr && e >= pn->inputChan) {
double w = (rr - r)/rr; /* Triangle weighting */
w = sqrt(w);
for (f = 0; f < pn->outputChan; f++)
pn->clutTable[ti+f] += w * clutTable1[tti + f];
tw += w;
}
FC_INC(cc);
}
for (f = 0; f < pn->outputChan; f++) {
double vv = pn->clutTable[ti+f] / tw;
if (vv < 0.0) {
vv = 0.0;
} else if (vv > 1.0) {
vv = 1.0;
}
pn->clutTable[ti+f] = vv;
}
/* Increment coord */
next_vert:;
for (ee = 0; ee < pn->inputChan; ee++) {
if (++ii[ee] < (pn->clutPoints-1)) /* Don't go through upper edge */
break; /* No carry */
ii[ee] = 0;
}
} /* Next grid point to filter */
} /* Next pass */
} /* Next table */
icp->al->free(icp->al, clutTable1);
icp->al->free(icp->al, clutTable3);
}
/* Create the 1D output table entry values */
for (tn = 0; tn < ntables; tn++) {
pn = pp[tn];
for (n = 0; n < pn->outputEnt; n++) {
double fv;
fv = n/(pn->outputEnt-1.0);
for (f = 0; f < pn->outputChan; f++)
iv[f] = fv;
/* Undo expansion to 0.0 - 1.0 */
for (f = 0; f < pn->outputChan; f++) /* Output tables */
iv[f] = iv[f] * (omax[f] - omin[f]) + omin[f];
ofromentry(iv,iv); /* Convert from table value to output color space value */
if (outfunc != NULL)
outfunc(cbctx, iv, iv); /* Out colorspace -> output table -> out colorspace. */
otoentry(iv,iv); /* Convert from output color space value to table value */
/* Clip to legal values */
for (f = 0; f < pn->outputChan; f++) {
double tt;
tt = iv[f];
if (tt < 0.0) {
DBGSLC(("oclip: tt = %f\n",tt));
if (tt < -CLIP_MARGIN)
clip = 5;
tt = 0.0;
} else if (tt > 1.0) {
DBGSLC(("oclip: tt = %f\n",tt));
if (tt > (1.0 + CLIP_MARGIN))
clip = 5;
tt = 1.0;
}
iv[f] = tt;
}
for (f = 0; f < pn->outputChan; f++) /* Input tables */
pn->outputTable[f * pn->outputEnt + n] = iv[f];
}
}
icp->al->free(icp->al, _iv);
icp->warnc = 0;
if (clip) {
DBGSLC(("Returning clip status = %d\n",clip));
icp->warnc = clip;
}
return 0;
}
/* Helper function to initialize a Lut tables contents */
/* from supplied transfer functions. */
/* Set errc and return error number */
/* Set warnc if there is clipping in the output values */
static int icmLut_set_tables (
icmLut *p, /* Pointer to Lut object */
int flags, /* Setting flags */
void *cbctx, /* Opaque callback context pointer value */
icColorSpaceSignature insig, /* Input color space */
icColorSpaceSignature outsig, /* Output color space */
void (*infunc)(void *cbcntx, double *out, double *in),
/* Input transfer function, inspace->inspace' (NULL = default) */
double *inmin, double *inmax, /* Maximum range of inspace' values (NULL = default) */
void (*clutfunc)(void *cbctx, double *out, double *in),
/* inspace' -> outspace' transfer function */
double *clutmin, double *clutmax, /* Maximum range of outspace' values (NULL = default) */
void (*outfunc)(void *cbctx, double *out, double *in),
/* Output transfer function, outspace'->outspace (NULL = deflt) */
int *apxls_gmin, int *apxls_gmax /* If not NULL, the grid indexes not to be affected */
/* by ICM_CLUT_SET_APXLS, defaulting to 0..>clutPoints-1 */
) {
struct _icmLut *pp[3];
/* Simply call the multiple table function with one table */
pp[0] = p;
return icmSetMultiLutTables(1, pp, flags,
cbctx, insig, outsig,
infunc,
inmin, inmax,
clutfunc,
clutmin, clutmax,
outfunc,
apxls_gmin, apxls_gmax);
}
/* - - - - - - - - - - - - - - - - */
/* Return the number of bytes needed to write this tag */
static unsigned int icmLut_get_size(
icmBase *pp
) {
icmLut *p = (icmLut *)pp;
unsigned int len = 0;
if (p->ttype == icSigLut8Type) {
len = sat_add(len, 48); /* tag and header */
len = sat_add(len, sat_mul3(1, p->inputChan, p->inputEnt));
len = sat_add(len, sat_mul3(1, p->outputChan, sat_pow(p->clutPoints,p->inputChan)));
len = sat_add(len, sat_mul3(1, p->outputChan, p->outputEnt));
} else {
len = sat_add(len, 52); /* tag and header */
len = sat_add(len, sat_mul3(2, p->inputChan, p->inputEnt));
len = sat_add(len, sat_mul3(2, p->outputChan, sat_pow(p->clutPoints,p->inputChan)));
len = sat_add(len, sat_mul3(2, p->outputChan, p->outputEnt));
}
return len;
}
/* read the object, return 0 on success, error code on fail */
static int icmLut_read(
icmBase *pp,
unsigned int len, /* tag length */
unsigned int of /* start offset within file */
) {
icmLut *p = (icmLut *)pp;
icc *icp = p->icp;
int rv = 0;
unsigned int i, j, g, size;
char *bp, *buf;
if (len < 4) {
sprintf(icp->err,"icmLut_read: Tag too small to be legal");
return icp->errc = 1;
}
/* Allocate a file read buffer */
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmLut_read: malloc() failed");
return icp->errc = 2;
}
bp = buf;
/* Read portion of file into buffer */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->read(icp->fp, bp, 1, len) != len) {
sprintf(icp->err,"icmLut_read: fseek() or fread() failed");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
/* Read type descriptor from the buffer */
p->ttype = (icTagTypeSignature)read_SInt32Number(bp);
if (p->ttype != icSigLut8Type && p->ttype != icSigLut16Type) {
sprintf(icp->err,"icmLut_read: Wrong tag type for icmLut");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
if (p->ttype == icSigLut8Type) {
if (len < 48) {
sprintf(icp->err,"icmLut_read: Tag too small to be legal");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
} else {
if (len < 52) {
sprintf(icp->err,"icmLut_read: Tag too small to be legal");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
}
/* Read in the info common to 8 and 16 bit Lut */
p->inputChan = read_UInt8Number(bp+8);
p->outputChan = read_UInt8Number(bp+9);
p->clutPoints = read_UInt8Number(bp+10);
if (icp->allowclutPoints256 && p->clutPoints == 0) /* Special case */
p->clutPoints = 256;
/* Read 3x3 transform matrix */
for (j = 0; j < 3; j++) { /* Rows */
for (i = 0; i < 3; i++) { /* Columns */
p->e[j][i] = read_S15Fixed16Number(bp + 12 + ((j * 3 + i) * 4));
}
}
/* Read 16 bit specific stuff */
if (p->ttype == icSigLut8Type) {
p->inputEnt = 256; /* By definition */
p->outputEnt = 256; /* By definition */
bp = buf+48;
} else {
p->inputEnt = read_UInt16Number(bp+48);
p->outputEnt = read_UInt16Number(bp+50);
bp = buf+52;
}
/* Sanity check tag size. This protects against */
/* subsequent integer overflows involving the dimensions. */
if ((size = icmLut_get_size((icmBase *)p)) == UINT_MAX
|| size > len) {
sprintf(icp->err,"icmLut_read: Tag wrong size for contents");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
/* Sanity check the dimensions and resolution values agains limits, */
/* allocate space for them and generate internal offset tables. */
if ((rv = p->allocate((icmBase *)p)) != 0) {
icp->al->free(icp->al, buf);
return rv;
}
/* Read the input tables */
size = (p->inputChan * p->inputEnt);
if (p->ttype == icSigLut8Type) {
for (i = 0; i < size; i++, bp += 1)
p->inputTable[i] = read_DCS8Number(bp);
} else {
for (i = 0; i < size; i++, bp += 2)
p->inputTable[i] = read_DCS16Number(bp);
}
/* Read the clut table */
size = (p->outputChan * sat_pow(p->clutPoints,p->inputChan));
if (p->ttype == icSigLut8Type) {
for (i = 0; i < size; i++, bp += 1)
p->clutTable[i] = read_DCS8Number(bp);
} else {
for (i = 0; i < size; i++, bp += 2)
p->clutTable[i] = read_DCS16Number(bp);
}
/* Read the output tables */
size = (p->outputChan * p->outputEnt);
if (p->ttype == icSigLut8Type) {
for (i = 0; i < size; i++, bp += 1)
p->outputTable[i] = read_DCS8Number(bp);
} else {
for (i = 0; i < size; i++, bp += 2)
p->outputTable[i] = read_DCS16Number(bp);
}
icp->al->free(icp->al, buf);
return 0;
}
/* Write the contents of the object. Return 0 on sucess, error code on failure */
static int icmLut_write(
icmBase *pp,
unsigned int of /* File offset to write from */
) {
icmLut *p = (icmLut *)pp;
icc *icp = p->icp;
unsigned int i,j;
unsigned int len, size;
char *bp, *buf; /* Buffer to write from */
int rv = 0;
/* Allocate a file write buffer */
if ((len = p->get_size((icmBase *)p)) == UINT_MAX) {
sprintf(icp->err,"icmLut_write get_size overflow");
return icp->errc = 1;
}
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmLut_write malloc() failed");
return icp->errc = 2;
}
bp = buf;
/* Write type descriptor to the buffer */
if ((rv = write_SInt32Number((int)p->ttype,bp)) != 0) {
sprintf(icp->err,"icmLut_write: write_SInt32Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
write_SInt32Number(0,bp+4); /* Set padding to 0 */
/* Write the info common to 8 and 16 bit Lut */
if ((rv = write_UInt8Number(p->inputChan, bp+8)) != 0) {
sprintf(icp->err,"icmLut_write: write_UInt8Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
if ((rv = write_UInt8Number(p->outputChan, bp+9)) != 0) {
sprintf(icp->err,"icmLut_write: write_UInt8Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
if (icp->allowclutPoints256 && p->clutPoints == 256) {
if ((rv = write_UInt8Number(0, bp+10)) != 0) {
sprintf(icp->err,"icmLut_write: write_UInt8Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
} else {
if ((rv = write_UInt8Number(p->clutPoints, bp+10)) != 0) {
sprintf(icp->err,"icmLut_write: write_UInt8Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
}
write_UInt8Number(0, bp+11); /* Set padding to 0 */
/* Write 3x3 transform matrix */
for (j = 0; j < 3; j++) { /* Rows */
for (i = 0; i < 3; i++) { /* Columns */
if ((rv = write_S15Fixed16Number(p->e[j][i],bp + 12 + ((j * 3 + i) * 4))) != 0) {
sprintf(icp->err,"icmLut_write: write_S15Fixed16Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
}
}
/* Write 16 bit specific stuff */
if (p->ttype == icSigLut8Type) {
if (p->inputEnt != 256 || p->outputEnt != 256) {
sprintf(icp->err,"icmLut_write: 8 bit Input and Output tables must be 256 entries");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
bp = buf+48;
} else {
if (p->inputEnt > 4096 || p->outputEnt > 4096) {
sprintf(icp->err,"icmLut_write: 16 bit Input and Output tables must each be less than 4096 entries");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
if ((rv = write_UInt16Number(p->inputEnt, bp+48)) != 0) {
sprintf(icp->err,"icmLut_write: write_UInt16Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
if ((rv = write_UInt16Number(p->outputEnt, bp+50)) != 0) {
sprintf(icp->err,"icmLut_write: write_UInt16Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
bp = buf+52;
}
/* Write the input tables */
size = (p->inputChan * p->inputEnt);
if (p->ttype == icSigLut8Type) {
for (i = 0; i < size; i++, bp += 1) {
if ((rv = write_DCS8Number(p->inputTable[i], bp)) != 0) {
sprintf(icp->err,"icmLut_write: inputTable write_DCS8Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
}
} else {
for (i = 0; i < size; i++, bp += 2) {
if ((rv = write_DCS16Number(p->inputTable[i], bp)) != 0) {
sprintf(icp->err,"icmLut_write: inputTable write_DCS16Number(%.8f) failed",p->inputTable[i]);
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
}
}
/* Write the clut table */
size = (p->outputChan * sat_pow(p->clutPoints,p->inputChan));
if (p->ttype == icSigLut8Type) {
for (i = 0; i < size; i++, bp += 1) {
if ((rv = write_DCS8Number(p->clutTable[i], bp)) != 0) {
sprintf(icp->err,"icmLut_write: clutTable write_DCS8Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
}
} else {
for (i = 0; i < size; i++, bp += 2) {
if ((rv = write_DCS16Number(p->clutTable[i], bp)) != 0) {
sprintf(icp->err,"icmLut_write: clutTable write_DCS16Number(%.8f) failed",p->clutTable[i]);
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
}
}
/* Write the output tables */
size = (p->outputChan * p->outputEnt);
if (p->ttype == icSigLut8Type) {
for (i = 0; i < size; i++, bp += 1) {
if ((rv = write_DCS8Number(p->outputTable[i], bp)) != 0) {
sprintf(icp->err,"icmLut_write: outputTable write_DCS8Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
}
} else {
for (i = 0; i < size; i++, bp += 2) {
if ((rv = write_DCS16Number(p->outputTable[i], bp)) != 0) {
sprintf(icp->err,"icmLut_write: outputTable write_DCS16Number(%.8f) failed",p->outputTable[i]);
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
}
}
/* Write buffer to the file */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->write(icp->fp, buf, 1, len) != len) {
sprintf(icp->err,"icmLut_write fseek() or fwrite() failed");
icp->al->free(icp->al, buf);
return icp->errc = 2;
}
icp->al->free(icp->al, buf);
return 0;
}
/* Dump a text description of the object */
static void icmLut_dump(
icmBase *pp,
icmFile *op, /* Output to dump to */
int verb /* Verbosity level */
) {
icmLut *p = (icmLut *)pp;
if (verb <= 0)
return;
if (p->ttype == icSigLut8Type) {
op->gprintf(op,"Lut8:\n");
} else {
op->gprintf(op,"Lut16:\n");
}
op->gprintf(op," Input Channels = %u\n",p->inputChan);
op->gprintf(op," Output Channels = %u\n",p->outputChan);
op->gprintf(op," CLUT resolution = %u\n",p->clutPoints);
op->gprintf(op," Input Table entries = %u\n",p->inputEnt);
op->gprintf(op," Output Table entries = %u\n",p->outputEnt);
op->gprintf(op," XYZ matrix = %.8f, %.8f, %.8f\n",p->e[0][0],p->e[0][1],p->e[0][2]);
op->gprintf(op," %.8f, %.8f, %.8f\n",p->e[1][0],p->e[1][1],p->e[1][2]);
op->gprintf(op," %.8f, %.8f, %.8f\n",p->e[2][0],p->e[2][1],p->e[2][2]);
if (verb >= 2) {
unsigned int i, j, size;
unsigned int ii[MAX_CHAN]; /* maximum no of input channels */
op->gprintf(op," Input table:\n");
for (i = 0; i < p->inputEnt; i++) {
op->gprintf(op," %3u: ",i);
for (j = 0; j < p->inputChan; j++)
op->gprintf(op," %1.10f",p->inputTable[j * p->inputEnt + i]);
op->gprintf(op,"\n");
}
op->gprintf(op,"\n CLUT table:\n");
if (p->inputChan > MAX_CHAN) {
op->gprintf(op," !!Can't dump > %d input channel CLUT table!!\n",MAX_CHAN);
} else {
size = (p->outputChan * sat_pow(p->clutPoints,p->inputChan));
for (j = 0; j < p->inputChan; j++)
ii[j] = 0;
for (i = 0; i < size;) {
unsigned int k;
/* Print table entry index */
op->gprintf(op," ");
for (j = p->inputChan-1; j < p->inputChan; j--)
op->gprintf(op," %2u",ii[j]);
op->gprintf(op,":");
/* Print table entry contents */
for (k = 0; k < p->outputChan; k++, i++)
op->gprintf(op," %1.10f",p->clutTable[i]);
op->gprintf(op,"\n");
for (j = 0; j < p->inputChan; j++) { /* Increment index */
ii[j]++;
if (ii[j] < p->clutPoints)
break; /* No carry */
ii[j] = 0;
}
}
}
op->gprintf(op,"\n Output table:\n");
for (i = 0; i < p->outputEnt; i++) {
op->gprintf(op," %3u: ",i);
for (j = 0; j < p->outputChan; j++)
op->gprintf(op," %1.10f",p->outputTable[j * p->outputEnt + i]);
op->gprintf(op,"\n");
}
}
}
/* Sanity check the input & output dimensions, and */
/* allocate variable sized data elements, and */
/* generate internal dimension offset tables */
static int icmLut_allocate(
icmBase *pp
) {
unsigned int i, j, g, size;
icmLut *p = (icmLut *)pp;
icc *icp = p->icp;
/* Sanity check, so that dinc[] comp. won't fail */
if (p->inputChan < 1) {
sprintf(icp->err,"icmLut_alloc: Can't handle %d input channels\n",p->inputChan);
return icp->errc = 1;
}
if (p->inputChan > MAX_CHAN) {
sprintf(icp->err,"icmLut_alloc: Can't handle > %d input channels\n",MAX_CHAN);
return icp->errc = 1;
}
if (p->outputChan > MAX_CHAN) {
sprintf(icp->err,"icmLut_alloc: Can't handle > %d output channels\n",MAX_CHAN);
return icp->errc = 1;
}
if ((size = sat_mul(p->inputChan, p->inputEnt)) == UINT_MAX) {
sprintf(icp->err,"icmLut_alloc size overflow");
return icp->errc = 1;
}
if (size != p->inputTable_size) {
if (ovr_mul(size, sizeof(double))) {
sprintf(icp->err,"icmLut_alloc: size overflow");
return icp->errc = 1;
}
if (p->inputTable != NULL)
icp->al->free(icp->al, p->inputTable);
if ((p->inputTable = (double *) icp->al->calloc(icp->al,size, sizeof(double))) == NULL) {
sprintf(icp->err,"icmLut_alloc: calloc() of Lut inputTable data failed");
return icp->errc = 2;
}
p->inputTable_size = size;
}
if ((size = sat_mul(p->outputChan, sat_pow(p->clutPoints,p->inputChan))) == UINT_MAX) {
sprintf(icp->err,"icmLut_alloc size overflow");
return icp->errc = 1;
}
if (size != p->clutTable_size) {
if (ovr_mul(size, sizeof(double))) {
sprintf(icp->err,"icmLut_alloc: size overflow");
return icp->errc = 1;
}
if (p->clutTable != NULL)
icp->al->free(icp->al, p->clutTable);
if ((p->clutTable = (double *) icp->al->calloc(icp->al,size, sizeof(double))) == NULL) {
sprintf(icp->err,"icmLut_alloc: calloc() of Lut clutTable data failed");
return icp->errc = 2;
}
p->clutTable_size = size;
}
if ((size = sat_mul(p->outputChan, p->outputEnt)) == UINT_MAX) {
sprintf(icp->err,"icmLut_alloc size overflow");
return icp->errc = 1;
}
if (size != p->outputTable_size) {
if (ovr_mul(size, sizeof(double))) {
sprintf(icp->err,"icmLut_alloc: size overflow");
return icp->errc = 1;
}
if (p->outputTable != NULL)
icp->al->free(icp->al, p->outputTable);
if ((p->outputTable = (double *) icp->al->calloc(icp->al,size, sizeof(double))) == NULL) {
sprintf(icp->err,"icmLut_alloc: calloc() of Lut outputTable data failed");
return icp->errc = 2;
}
p->outputTable_size = size;
}
/* Private: compute dimensional increment though clut */
/* Note that first channel varies least rapidly. */
i = p->inputChan-1;
p->dinc[i--] = p->outputChan;
for (; i < p->inputChan; i--)
p->dinc[i] = p->dinc[i+1] * p->clutPoints;
/* Private: compute offsets from base of cube to other corners */
for (p->dcube[0] = 0, g = 1, j = 0; j < p->inputChan; j++) {
for (i = 0; i < g; i++)
p->dcube[g+i] = p->dcube[i] + p->dinc[j];
g *= 2;
}
return 0;
}
/* Free all storage in the object */
static void icmLut_delete(
icmBase *pp
) {
icmLut *p = (icmLut *)pp;
icc *icp = p->icp;
int i;
if (p->inputTable != NULL)
icp->al->free(icp->al, p->inputTable);
if (p->clutTable != NULL)
icp->al->free(icp->al, p->clutTable);
if (p->outputTable != NULL)
icp->al->free(icp->al, p->outputTable);
for (i = 0; i < p->inputChan; i++)
icmTable_delete_bwd(icp, &p->rit[i]);
for (i = 0; i < p->outputChan; i++)
icmTable_delete_bwd(icp, &p->rot[i]);
icp->al->free(icp->al, p);
}
/* Create an empty object. Return null on error */
static icmBase *new_icmLut(
icc *icp
) {
int i,j;
icmLut *p;
if ((p = (icmLut *) icp->al->calloc(icp->al,1,sizeof(icmLut))) == NULL)
return NULL;
p->icp = icp;
p->ttype = icSigLut16Type;
p->refcount = 1;
p->get_size = icmLut_get_size;
p->read = icmLut_read;
p->write = icmLut_write;
p->dump = icmLut_dump;
p->allocate = icmLut_allocate;
p->del = icmLut_delete;
/* Lookup methods */
p->nu_matrix = icmLut_nu_matrix;
p->min_max = icmLut_min_max;
p->lookup_matrix = icmLut_lookup_matrix;
p->lookup_input = icmLut_lookup_input;
p->lookup_clut_nl = icmLut_lookup_clut_nl;
p->lookup_clut_sx = icmLut_lookup_clut_sx;
p->lookup_output = icmLut_lookup_output;
/* Set method */
p->set_tables = icmLut_set_tables;
p->tune_value = icmLut_tune_value_sx; /* Default to most likely simplex */
/* Set matrix to reasonable default */
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++) {
if (i == j)
p->e[i][j] = 1.0;
else
p->e[i][j] = 0.0;
}
/* Init lookups to non-dangerous values */
for (i = 0; i < MAX_CHAN; i++)
p->dinc[i] = 0;
for (i = 0; i < (1 << MAX_CHAN); i++)
p->dcube[i] = 0;
for (i = 0; i < MAX_CHAN; i++) {
p->rit[i].inited = 0;
p->rot[i].inited = 0;
}
return (icmBase *)p;
}
/* ---------------------------------------------------------- */
/* Measurement */
/* Return the number of bytes needed to write this tag */
static unsigned int icmMeasurement_get_size(
icmBase *pp
) {
unsigned int len = 0;
len = sat_add(len, 8); /* 8 bytes for tag and padding */
len = sat_add(len, 4); /* 4 for standard observer */
len = sat_add(len, 12); /* 12 for XYZ of measurement backing */
len = sat_add(len, 4); /* 4 for measurement geometry */
len = sat_add(len, 4); /* 4 for measurement flare */
len = sat_add(len, 4); /* 4 for standard illuminant */
return len;
}
/* read the object, return 0 on success, error code on fail */
static int icmMeasurement_read(
icmBase *pp,
unsigned int len, /* tag length */
unsigned int of /* start offset within file */
) {
icmMeasurement *p = (icmMeasurement *)pp;
icc *icp = p->icp;
int rv;
char *bp, *buf;
if (len < 36) {
sprintf(icp->err,"icmMeasurement_read: Tag too small to be legal");
return icp->errc = 1;
}
/* Allocate a file read buffer */
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmMeasurement_read: malloc() failed");
return icp->errc = 2;
}
bp = buf;
/* Read portion of file into buffer */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->read(icp->fp, bp, 1, len) != len) {
sprintf(icp->err,"icmMeasurement_read: fseek() or fread() failed");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
/* Read type descriptor from the buffer */
if (((icTagTypeSignature)read_SInt32Number(bp)) != p->ttype) {
sprintf(icp->err,"icmMeasurement_read: Wrong tag type for icmMeasurement");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
/* Read the encoded standard observer */
p->observer = (icStandardObserver)read_SInt32Number(bp + 8);
/* Read the XYZ values for measurement backing */
if ((rv = read_XYZNumber(&p->backing, bp+12)) != 0) {
sprintf(icp->err,"icmMeasurement: read_XYZNumber error");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
/* Read the encoded measurement geometry */
p->geometry = (icMeasurementGeometry)read_SInt32Number(bp + 24);
/* Read the proportion of flare */
p->flare = read_U16Fixed16Number(bp + 28);
/* Read the encoded standard illuminant */
p->illuminant = (icIlluminant)read_SInt32Number(bp + 32);
icp->al->free(icp->al, buf);
return 0;
}
/* Write the contents of the object. Return 0 on sucess, error code on failure */
static int icmMeasurement_write(
icmBase *pp,
unsigned int of /* File offset to write from */
) {
icmMeasurement *p = (icmMeasurement *)pp;
icc *icp = p->icp;
unsigned int len;
char *bp, *buf; /* Buffer to write from */
int rv = 0;
/* Allocate a file write buffer */
if ((len = p->get_size((icmBase *)p)) == UINT_MAX) {
sprintf(icp->err,"icmMeasurement_write get_size overflow");
return icp->errc = 1;
}
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmMeasurement_write malloc() failed");
return icp->errc = 2;
}
bp = buf;
/* Write type descriptor to the buffer */
if ((rv = write_SInt32Number((int)p->ttype,bp)) != 0) {
sprintf(icp->err,"icmMeasurement_write, type: write_SInt32Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
write_SInt32Number(0,bp+4); /* Set padding to 0 */
/* Write the encoded standard observer */
if ((rv = write_SInt32Number((int)p->observer, bp + 8)) != 0) {
sprintf(icp->err,"icmMeasurementa_write, observer: write_SInt32Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
/* Write the XYZ values for measurement backing */
if ((rv = write_XYZNumber(&p->backing, bp+12)) != 0) {
sprintf(icp->err,"icmMeasurement, backing: write_XYZNumber error");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
/* Write the encoded measurement geometry */
if ((rv = write_SInt32Number((int)p->geometry, bp + 24)) != 0) {
sprintf(icp->err,"icmMeasurementa_write, geometry: write_SInt32Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
/* Write the proportion of flare */
if ((rv = write_U16Fixed16Number(p->flare, bp + 28)) != 0) {
sprintf(icp->err,"icmMeasurementa_write, flare: write_U16Fixed16Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
/* Write the encoded standard illuminant */
if ((rv = write_SInt32Number((int)p->illuminant, bp + 32)) != 0) {
sprintf(icp->err,"icmMeasurementa_write, illuminant: write_SInt32Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
/* Write to the file */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->write(icp->fp, buf, 1, len) != len) {
sprintf(icp->err,"icmMeasurement_write fseek() or fwrite() failed");
icp->al->free(icp->al, buf);
return icp->errc = 2;
}
icp->al->free(icp->al, buf);
return 0;
}
/* Dump a text description of the object */
static void icmMeasurement_dump(
icmBase *pp,
icmFile *op, /* Output to dump to */
int verb /* Verbosity level */
) {
icmMeasurement *p = (icmMeasurement *)pp;
if (verb <= 0)
return;
op->gprintf(op,"Measurement:\n");
op->gprintf(op," Standard Observer = %s\n", string_StandardObserver(p->observer));
op->gprintf(op," XYZ for Measurement Backing = %s\n", string_XYZNumber_and_Lab(&p->backing));
op->gprintf(op," Measurement Geometry = %s\n", string_MeasurementGeometry(p->geometry));
op->gprintf(op," Measurement Flare = %5.1f%%\n", p->flare * 100.0);
op->gprintf(op," Standard Illuminant = %s\n", string_Illuminant(p->illuminant));
}
/* Allocate variable sized data elements */
static int icmMeasurement_allocate(
icmBase *pp
) {
/* Nothing to do */
return 0;
}
/* Free all storage in the object */
static void icmMeasurement_delete(
icmBase *pp
) {
icmMeasurement *p = (icmMeasurement *)pp;
icc *icp = p->icp;
icp->al->free(icp->al, p);
}
/* Create an empty object. Return null on error */
static icmBase *new_icmMeasurement(
icc *icp
) {
icmMeasurement *p;
if ((p = (icmMeasurement *) icp->al->calloc(icp->al,1,sizeof(icmMeasurement))) == NULL)
return NULL;
p->ttype = icSigMeasurementType;
p->refcount = 1;
p->get_size = icmMeasurement_get_size;
p->read = icmMeasurement_read;
p->write = icmMeasurement_write;
p->dump = icmMeasurement_dump;
p->allocate = icmMeasurement_allocate;
p->del = icmMeasurement_delete;
p->icp = icp;
return (icmBase *)p;
}
/* ---------------------------------------------------------- */
/* Named color structure read/write support */
static int read_NamedColorVal(
icmNamedColorVal *p,
char *bp,
char *end,
icColorSpaceSignature pcs, /* Header Profile Connection Space */
unsigned int ndc /* Number of device corrds */
) {
icc *icp = p->icp;
unsigned int i;
unsigned int mxl; /* Max possible string length */
int rv;
if (bp > end) {
sprintf(icp->err,"icmNamedColorVal_read: Data too short to read");
return icp->errc = 1;
}
mxl = (end - bp) < 32 ? (end - bp) : 32;
if ((rv = check_null_string(bp,mxl)) == 1) {
sprintf(icp->err,"icmNamedColorVal_read: Root name string not terminated");
return icp->errc = 1;
}
/* Haven't checked if rv == 2 is legal or not */
strcpy(p->root, bp);
bp += strlen(p->root) + 1;
if (bp > end || ndc > (end - bp)) {
sprintf(icp->err,"icmNamedColorVal_read: Data too short to read device coords");
return icp->errc = 1;
}
for (i = 0; i < ndc; i++) {
p->deviceCoords[i] = read_DCS8Number(bp);
bp += 1;
}
return 0;
}
static int read_NamedColorVal2(
icmNamedColorVal *p,
char *bp,
char *end,
icColorSpaceSignature pcs, /* Header Profile Connection Space */
unsigned int ndc /* Number of device coords */
) {
int rv;
icc *icp = p->icp;
unsigned int i;
if (bp > end
|| (32 + 6) > (end - bp)
|| ndc > (end - bp - 32 - 6)/2) {
sprintf(icp->err,"icmNamedColorVal2_read: Data too short to read");
return icp->errc = 1;
}
if ((rv = check_null_string(bp,32)) == 1) {
sprintf(icp->err,"icmNamedColorVal2_read: Root name string not terminated");
return icp->errc = 1;
}
memmove((void *)p->root,(void *)(bp + 0),32);
switch(pcs) {
case icSigXYZData:
read_PCSNumber(icp, icSigXYZData, p->pcsCoords, bp+32);
break;
case icSigLabData:
/* namedColor2Type retains legacy Lab encoding */
read_PCSNumber(icp, icmSigLabV2Data, p->pcsCoords, bp+32);
break;
default:
return 1; /* Unknown PCS */
}
for (i = 0; i < ndc; i++)
p->deviceCoords[i] = read_DCS16Number(bp + 32 + 6 + 2 * i);
return 0;
}
static int write_NamedColorVal(
icmNamedColorVal *p,
char *d,
icColorSpaceSignature pcs, /* Header Profile Connection Space */
unsigned int ndc /* Number of device corrds */
) {
icc *icp = p->icp;
unsigned int i;
int rv;
if ((rv = check_null_string(p->root,32)) == 1) {
sprintf(icp->err,"icmNamedColorVal_write: Root string names is unterminated");
return icp->errc = 1;
}
strcpy(d, p->root);
d += strlen(p->root) + 1;
for (i = 0; i < ndc; i++) {
if ((rv = write_DCS8Number(p->deviceCoords[i], d)) != 0) {
sprintf(icp->err,"icmNamedColorVal_write: write of device coord failed");
return icp->errc = 1;
}
d += 1;
}
return 0;
}
static int write_NamedColorVal2(
icmNamedColorVal *p,
char *bp,
icColorSpaceSignature pcs, /* Header Profile Connection Space */
unsigned int ndc /* Number of device coords */
) {
icc *icp = p->icp;
unsigned int i;
int rv;
if ((rv = check_null_string(p->root,32)) == 1) {
sprintf(icp->err,"icmNamedColorVal2_write: Root string names is unterminated");
return icp->errc = 1;
}
rv = 0;
memmove((void *)(bp + 0),(void *)p->root,32);
switch(pcs) {
case icSigXYZData:
rv |= write_PCSNumber(icp, icSigXYZData, p->pcsCoords, bp+32);
break;
case icSigLabData:
/* namedColor2Type retains legacy Lab encoding */
rv |= write_PCSNumber(icp, icmSigLabV2Data, p->pcsCoords, bp+32);
break;
default:
sprintf(icp->err,"icmNamedColorVal2_write: Unknown PCS");
return icp->errc = 1;
}
if (rv) {
sprintf(icp->err,"icmNamedColorVal2_write: write of PCS coord failed");
return icp->errc = 1;
}
for (i = 0; i < ndc; i++) {
if ((rv = write_DCS16Number(p->deviceCoords[i], bp + 32 + 6 + 2 * i)) != 0) {
sprintf(icp->err,"icmNamedColorVal2_write: write of device coord failed");
return icp->errc = 1;
}
}
return 0;
}
/* - - - - - - - - - - - */
/* icmNamedColor object */
/* Return the number of bytes needed to write this tag */
static unsigned int icmNamedColor_get_size(
icmBase *pp
) {
icmNamedColor *p = (icmNamedColor *)pp;
unsigned int len = 0;
if (p->ttype == icSigNamedColorType) {
unsigned int i;
len = sat_add(len, 8); /* 8 bytes for tag and padding */
len = sat_add(len, 4); /* 4 for vendor specific flags */
len = sat_add(len, 4); /* 4 for count of named colors */
len = sat_add(len, strlen(p->prefix) + 1); /* prefix of color names */
len = sat_add(len, strlen(p->suffix) + 1); /* suffix of color names */
for (i = 0; i < p->count; i++) {
len = sat_add(len, strlen(p->data[i].root) + 1); /* color names */
len = sat_add(len, p->nDeviceCoords * 1); /* bytes for each named color */
}
} else { /* Named Color 2 */
len = sat_add(len, 8); /* 8 bytes for tag and padding */
len = sat_add(len, 4); /* 4 for vendor specific flags */
len = sat_add(len, 4); /* 4 for count of named colors */
len = sat_add(len, 4); /* 4 for number of device coords */
len = sat_add(len, 32); /* 32 for prefix of color names */
len = sat_add(len, 32); /* 32 for suffix of color names */
len = sat_add(len, sat_mul(p->count, (32 + 6 + p->nDeviceCoords * 2)));
/* bytes for each named color */
}
return len;
}
/* read the object, return 0 on success, error code on fail */
static int icmNamedColor_read(
icmBase *pp,
unsigned int len, /* tag length */
unsigned int of /* start offset within file */
) {
icmNamedColor *p = (icmNamedColor *)pp;
icc *icp = p->icp;
unsigned int i;
char *bp, *buf, *end;
int rv;
if (len < 4) {
sprintf(icp->err,"icmNamedColor_read: Tag too small to be legal");
return icp->errc = 1;
}
/* Allocate a file read buffer */
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmNamedColor_read: malloc() failed");
return icp->errc = 2;
}
bp = buf;
end = buf + len;
/* Read portion of file into buffer */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->read(icp->fp, bp, 1, len) != len) {
sprintf(icp->err,"icmNamedColor_read: fseek() or fread() failed");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
/* Read type descriptor from the buffer */
p->ttype = (icTagTypeSignature)read_SInt32Number(bp);
if (p->ttype != icSigNamedColorType && p->ttype != icSigNamedColor2Type) {
sprintf(icp->err,"icmNamedColor_read: Wrong tag type for icmNamedColor");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
if (p->ttype == icSigNamedColorType) {
if (len < 16) {
sprintf(icp->err,"icmNamedColor_read: Tag too small to be legal");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
/* Make sure that the number of device coords in known */
p->nDeviceCoords = number_ColorSpaceSignature(icp->header->colorSpace);
if (p->nDeviceCoords > MAX_CHAN) {
sprintf(icp->err,"icmNamedColor_read: Can't handle more than %d device channels",MAX_CHAN);
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
} else { /* icmNC2 */
if (len < 84) {
sprintf(icp->err,"icmNamedColor_read: Tag too small to be legal");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
}
/* Read vendor specific flag */
p->vendorFlag = read_UInt32Number(bp+8);
/* Read count of named colors */
p->count = read_UInt32Number(bp+12);
if (p->ttype == icSigNamedColorType) {
unsigned int mxl; /* Max possible string length */
bp = bp + 16;
/* Prefix for each color name */
if (bp > end) {
sprintf(icp->err,"icmNamedColor_read: Data too short to read");
return icp->errc = 1;
}
mxl = (end - bp) < 32 ? (end - bp) : 32;
if ((rv = check_null_string(bp,mxl)) == 1) {
sprintf(icp->err,"icmNamedColor_read: Color prefix is not null terminated");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
/* Haven't checked if rv == 2 is legal or not */
strcpy(p->prefix, bp);
bp += strlen(p->prefix) + 1;
/* Suffix for each color name */
if (bp > end) {
sprintf(icp->err,"icmNamedColor_read: Data too short to read");
return icp->errc = 1;
}
mxl = (end - bp) < 32 ? (end - bp) : 32;
if ((rv = check_null_string(bp,mxl)) == 1) {
sprintf(icp->err,"icmNamedColor_read: Color suffix is not null terminated");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
/* Haven't checked if rv == 2 is legal or not */
strcpy(p->suffix, bp);
bp += strlen(p->suffix) + 1;
if ((rv = p->allocate((icmBase *)p)) != 0) {
icp->al->free(icp->al, buf);
return rv;
}
/* Read all the data from the buffer */
for (i = 0; i < p->count; i++) {
if ((rv = read_NamedColorVal(p->data+i, bp, end, icp->header->pcs, p->nDeviceCoords)) != 0) {
icp->al->free(icp->al, buf);
return rv;
}
bp += strlen(p->data[i].root) + 1;
bp += p->nDeviceCoords * 1;
}
} else { /* icmNC2 */
/* Number of device coords per color */
p->nDeviceCoords = read_UInt32Number(bp+16);
if (p->nDeviceCoords > MAX_CHAN) {
sprintf(icp->err,"icmNamedColor_read: Can't handle more than %d device channels",MAX_CHAN);
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
/* Prefix for each color name */
memmove((void *)p->prefix, (void *)(bp + 20), 32);
if ((rv = check_null_string(p->prefix,32)) == 1) {
sprintf(icp->err,"icmNamedColor_read: Color prefix is not null terminated");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
/* Suffix for each color name */
memmove((void *)p->suffix, (void *)(bp + 52), 32);
if ((rv = check_null_string(p->suffix,32)) == 1) {
sprintf(icp->err,"icmNamedColor_read: Color suffix is not null terminated");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
if ((rv = p->allocate((icmBase *)p)) != 0) {
icp->al->free(icp->al, buf);
return rv;
}
/* Read all the data from the buffer */
bp = bp + 84;
for (i = 0; i < p->count; i++) {
if ((rv = read_NamedColorVal2(p->data+i, bp, end, icp->header->pcs, p->nDeviceCoords)) != 0) {
icp->al->free(icp->al, buf);
return rv;
}
bp += 32 + 6 + p->nDeviceCoords * 2;
}
}
icp->al->free(icp->al, buf);
return 0;
}
/* Write the contents of the object. Return 0 on sucess, error code on failure */
static int icmNamedColor_write(
icmBase *pp,
unsigned int of /* File offset to write from */
) {
icmNamedColor *p = (icmNamedColor *)pp;
icc *icp = p->icp;
unsigned int i;
unsigned int len;
char *bp, *buf; /* Buffer to write from */
int rv;
/* Allocate a file write buffer */
if ((len = p->get_size((icmBase *)p)) == UINT_MAX) {
sprintf(icp->err,"icmNamedColor_write get_size overflow");
return icp->errc = 1;
}
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmNamedColor_write malloc() failed");
return icp->errc = 2;
}
bp = buf;
/* Write type descriptor to the buffer */
if ((rv = write_SInt32Number((int)p->ttype,bp)) != 0) {
sprintf(icp->err,"icmNamedColor_write: write_SInt32Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
write_SInt32Number(0,bp+4); /* Set padding to 0 */
/* Write vendor specific flag */
if ((rv = write_UInt32Number(p->vendorFlag, bp+8)) != 0) {
sprintf(icp->err,"icmNamedColor_write: write_UInt32Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
/* Write count of named colors */
if ((rv = write_UInt32Number(p->count, bp+12)) != 0) {
sprintf(icp->err,"icmNamedColor_write: write_UInt32Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
if (p->ttype == icSigNamedColorType) {
bp = bp + 16;
/* Prefix for each color name */
if ((rv = check_null_string(p->prefix,32)) == 1) {
sprintf(icp->err,"icmNamedColor_write: Color prefix is not null terminated");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
strcpy(bp, p->prefix);
bp += strlen(p->prefix) + 1;
/* Suffix for each color name */
if ((rv = check_null_string(p->suffix,32)) == 1) {
sprintf(icp->err,"icmNamedColor_write: Color sufix is not null terminated");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
strcpy(bp, p->suffix);
bp += strlen(p->suffix) + 1;
/* Write all the data to the buffer */
for (i = 0; i < p->count; i++) {
if ((rv = write_NamedColorVal(p->data+i, bp, icp->header->pcs, p->nDeviceCoords)) != 0) {
icp->al->free(icp->al, buf);
return rv;
}
bp += strlen(p->data[i].root) + 1;
bp += p->nDeviceCoords * 1;
}
} else { /* icmNC2 */
/* Number of device coords per color */
if ((rv = write_UInt32Number(p->nDeviceCoords, bp+16)) != 0) {
sprintf(icp->err,"icmNamedColor_write: write_UInt32Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
/* Prefix for each color name */
if ((rv = check_null_string(p->prefix,32)) == 1) {
sprintf(icp->err,"icmNamedColor_write: Color prefix is not null terminated");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
memmove((void *)(bp + 20), (void *)p->prefix, 32);
/* Suffix for each color name */
if ((rv = check_null_string(p->suffix,32)) == 1) {
sprintf(icp->err,"icmNamedColor_write: Color sufix is not null terminated");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
memmove((void *)(bp + 52), (void *)p->suffix, 32);
/* Write all the data to the buffer */
bp = bp + 84;
for (i = 0; i < p->count; i++, bp += (32 + 6 + p->nDeviceCoords * 2)) {
if ((rv = write_NamedColorVal2(p->data+i, bp, icp->header->pcs, p->nDeviceCoords)) != 0) {
icp->al->free(icp->al, buf);
return rv;
}
}
}
/* Write to the file */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->write(icp->fp, buf, 1, len) != len) {
sprintf(icp->err,"icmNamedColor_write fseek() or fwrite() failed");
icp->al->free(icp->al, buf);
return icp->errc = 2;
}
icp->al->free(icp->al, buf);
return 0;
}
/* Dump a text description of the object */
static void icmNamedColor_dump(
icmBase *pp,
icmFile *op, /* Output to dump to */
int verb /* Verbosity level */
) {
icmNamedColor *p = (icmNamedColor *)pp;
icc *icp = p->icp;
if (verb <= 0)
return;
if (p->ttype == icSigNamedColorType)
op->gprintf(op,"NamedColor:\n");
else
op->gprintf(op,"NamedColor2:\n");
op->gprintf(op," Vendor Flag = 0x%x\n",p->vendorFlag);
op->gprintf(op," No. colors = %u\n",p->count);
op->gprintf(op," No. dev. coords = %u\n",p->nDeviceCoords);
op->gprintf(op," Name prefix = '%s'\n",p->prefix);
op->gprintf(op," Name suffix = '%s'\n",p->suffix);
if (verb >= 2) {
unsigned int i, n;
icmNamedColorVal *vp;
for (i = 0; i < p->count; i++) {
vp = p->data + i;
op->gprintf(op," Color %lu:\n",i);
op->gprintf(op," Name root = '%s'\n",vp->root);
if (p->ttype == icSigNamedColor2Type) {
switch(icp->header->pcs) {
case icSigXYZData:
op->gprintf(op," XYZ = %.8f, %.8f, %.8f\n",
vp->pcsCoords[0],vp->pcsCoords[1],vp->pcsCoords[2]);
break;
case icSigLabData:
op->gprintf(op," Lab = %f, %f, %f\n",
vp->pcsCoords[0],vp->pcsCoords[1],vp->pcsCoords[2]);
break;
default:
op->gprintf(op," Unexpected PCS\n");
break;
}
}
if (p->nDeviceCoords > 0) {
op->gprintf(op," Device Coords = ");
for (n = 0; n < p->nDeviceCoords; n++) {
if (n > 0)
op->gprintf(op,", ");
op->gprintf(op,"%.8f",vp->deviceCoords[n]);
}
op->gprintf(op,"\n");
}
}
}
}
/* Allocate variable sized data elements */
static int icmNamedColor_allocate(
icmBase *pp
) {
icmNamedColor *p = (icmNamedColor *)pp;
icc *icp = p->icp;
if (p->count != p->_count) {
unsigned int i;
if (ovr_mul(p->count, sizeof(icmNamedColorVal))) {
sprintf(icp->err,"icmNamedColor_alloc: size overflow");
return icp->errc = 1;
}
if (p->data != NULL)
icp->al->free(icp->al, p->data);
if ((p->data = (icmNamedColorVal *) icp->al->calloc(icp->al,p->count, sizeof(icmNamedColorVal))) == NULL) {
sprintf(icp->err,"icmNamedColor_alloc: malloc() of icmNamedColor data failed");
return icp->errc = 2;
}
for (i = 0; i < p->count; i++) {
p->data[i].icp = icp; /* Do init */
}
p->_count = p->count;
}
return 0;
}
/* Free all storage in the object */
static void icmNamedColor_delete(
icmBase *pp
) {
icmNamedColor *p = (icmNamedColor *)pp;
icc *icp = p->icp;
if (p->data != NULL)
icp->al->free(icp->al, p->data);
icp->al->free(icp->al, p);
}
/* Create an empty object. Return null on error */
static icmBase *new_icmNamedColor(
icc *icp
) {
icmNamedColor *p;
if ((p = (icmNamedColor *) icp->al->calloc(icp->al,1,sizeof(icmNamedColor))) == NULL)
return NULL;
p->ttype = icSigNamedColor2Type;
p->refcount = 1;
p->get_size = icmNamedColor_get_size;
p->read = icmNamedColor_read;
p->write = icmNamedColor_write;
p->dump = icmNamedColor_dump;
p->allocate = icmNamedColor_allocate;
p->del = icmNamedColor_delete;
p->icp = icp;
/* Default the the number of device coords appropriately for NamedColorType */
p->nDeviceCoords = number_ColorSpaceSignature(icp->header->colorSpace);
return (icmBase *)p;
}
/* ---------------------------------------------------------- */
/* Colorant table structure read/write support */
/* (Contribution from Piet Vandenborre) */
static int read_ColorantTableVal(
icmColorantTableVal *p,
char *bp,
char *end,
icColorSpaceSignature pcs /* Header Profile Connection Space */
) {
int rv;
icc *icp = p->icp;
if (bp > end || (32 + 6) > (end - bp)) {
sprintf(icp->err,"icmColorantTableVal_read: Data too short to read");
return icp->errc = 1;
}
if ((rv = check_null_string(bp,32)) == 1) {
sprintf(icp->err,"icmColorantTableVal_read: Name string not terminated");
return icp->errc = 1;
}
memmove((void *)p->name,(void *)(bp + 0),32);
switch(pcs) {
case icSigXYZData:
case icSigLabData:
read_PCSNumber(icp, pcs, p->pcsCoords, bp+32);
break;
default:
return 1; /* Unknown PCS */
}
return 0;
}
static int write_ColorantTableVal(
icmColorantTableVal *p,
char *bp,
icColorSpaceSignature pcs /* Header Profile Connection Space */
) {
int rv;
icc *icp = p->icp;
if ((rv = check_null_string(p->name,32)) == 1) {
sprintf(icp->err,"icmColorantTableVal_write: Name string is unterminated");
return icp->errc = 1;
}
memmove((void *)(bp + 0),(void *)p->name,32);
rv = 0;
switch(pcs) {
case icSigXYZData:
case icSigLabData:
rv |= write_PCSNumber(icp, pcs, p->pcsCoords, bp+32);
break;
default:
sprintf(icp->err,"icmColorantTableVal_write: Unknown PCS");
return icp->errc = 1;
}
if (rv) {
sprintf(icp->err,"icmColorantTableVal_write: write of PCS coord failed");
return icp->errc = 1;
}
return 0;
}
/* - - - - - - - - - - - */
/* icmColorantTable object */
/* Return the number of bytes needed to write this tag */
static unsigned int icmColorantTable_get_size(
icmBase *pp
) {
icmColorantTable *p = (icmColorantTable *)pp;
unsigned int len = 0;
if (p->ttype == icSigColorantTableType
|| p->ttype == icmSigAltColorantTableType) {
unsigned int i;
len = sat_add(len, 8); /* 8 bytes for tag and padding */
len = sat_add(len, 4); /* 4 for count of colorants */
for (i = 0; i < p->count; i++) {
len = sat_add(len, 32); /* colorant names - 32 bytes*/
len = sat_add(len, 6); /* colorant pcs value - 3 x 16bit number*/
}
}
return len;
}
/* read the object, return 0 on success, error code on fail */
static int icmColorantTable_read(
icmBase *pp,
unsigned int len, /* tag length */
unsigned int of /* start offset within file */
) {
icmColorantTable *p = (icmColorantTable *)pp;
icc *icp = p->icp;
icColorSpaceSignature pcs;
unsigned int i;
char *bp, *buf, *end;
int rv = 0;
if (icp->header->deviceClass != icSigLinkClass)
pcs = icp->header->pcs;
else
pcs = icSigLabData;
if (len < 4) {
sprintf(icp->err,"icmColorantTable_read: Tag too small to be legal");
return icp->errc = 1;
}
/* Allocate a file read buffer */
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmColorantTable_read: malloc() failed");
return icp->errc = 2;
}
bp = buf;
end = buf + len;
/* Read portion of file into buffer */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->read(icp->fp, bp, 1, len) != len) {
sprintf(icp->err,"icmColorantTable_read: fseek() or fread() failed");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
/* Read type descriptor from the buffer */
p->ttype = (icTagTypeSignature)read_SInt32Number(bp);
if (p->ttype != icSigColorantTableType
&& p->ttype != icmSigAltColorantTableType) {
sprintf(icp->err,"icmColorantTable_read: Wrong tag type for icmColorantTable");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
if (len < 12) {
sprintf(icp->err,"icmColorantTable_read: Tag too small to be legal");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
/* Read count of colorants */
if (p->ttype == icmSigAltColorantTableType)
p->count = read_UInt8Number(bp+8); /* Hmm. This is Little Endian */
else
p->count = read_UInt32Number(bp+8);
if (p->count > ((len - 12) / (32 + 6))) {
sprintf(icp->err,"icmColorantTable_read count overflow, count %x, len %d",p->count,len);
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
bp = bp + 12;
if ((rv = p->allocate((icmBase *)p)) != 0) {
icp->al->free(icp->al, buf);
return rv;
}
/* Read all the data from the buffer */
for (i = 0; i < p->count; i++, bp += (32 + 6)) {
if (p->ttype == icmSigAltColorantTableType /* Hack to reverse little endian */
&& (end - bp) >= 38) {
int tt;
tt = *(bp + 32);
*(bp+32) = *(bp+33);
*(bp+33) = tt;
tt = *(bp + 34);
*(bp+34) = *(bp+35);
*(bp+35) = tt;
tt = *(bp + 36);
*(bp+36) = *(bp+37);
*(bp+37) = tt;
}
if ((rv = read_ColorantTableVal(p->data+i, bp, end, pcs)) != 0) {
icp->al->free(icp->al, buf);
return rv;
}
}
icp->al->free(icp->al, buf);
return rv;
}
/* Write the contents of the object. Return 0 on sucess, error code on failure */
static int icmColorantTable_write(
icmBase *pp,
unsigned int of /* File offset to write from */
) {
icmColorantTable *p = (icmColorantTable *)pp;
icc *icp = p->icp;
icColorSpaceSignature pcs;
unsigned int i;
unsigned int len;
char *bp, *buf; /* Buffer to write from */
int rv = 0;
if (icp->header->deviceClass != icSigLinkClass)
pcs = icp->header->pcs;
else
pcs = icSigLabData;
/* Allocate a file write buffer */
if ((len = p->get_size((icmBase *)p)) == UINT_MAX) {
sprintf(icp->err,"icmColorantTable_write get_size overflow");
return icp->errc = 1;
}
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmColorantTable_write malloc() failed");
return icp->errc = 2;
}
bp = buf;
/* Write type descriptor to the buffer */
if ((rv = write_SInt32Number((int)p->ttype,bp)) != 0) {
sprintf(icp->err,"icmColorantTable_write: write_SInt32Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
write_SInt32Number(0,bp+4); /* Set padding to 0 */
/* Write count of colorants */
if ((rv = write_UInt32Number(p->count, bp+8)) != 0) {
sprintf(icp->err,"icmColorantTable_write: write_UInt32Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
bp = bp + 12;
/* Write all the data to the buffer */
for (i = 0; i < p->count; i++, bp += (32 + 6)) {
if ((rv = write_ColorantTableVal(p->data+i, bp, pcs)) != 0) {
icp->al->free(icp->al, buf);
return rv;
}
}
/* Write to the file */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->write(icp->fp, buf, 1, len) != len) {
sprintf(icp->err,"icmColorantTable_write fseek() or fwrite() failed");
icp->al->free(icp->al, buf);
return icp->errc = 2;
}
icp->al->free(icp->al, buf);
return 0;
}
/* Dump a text description of the object */
static void icmColorantTable_dump(
icmBase *pp,
icmFile *op, /* Output to dump to */
int verb /* Verbosity level */
) {
icmColorantTable *p = (icmColorantTable *)pp;
icc *icp = p->icp;
icColorSpaceSignature pcs;
if (icp->header->deviceClass != icSigLinkClass)
pcs = icp->header->pcs;
else
pcs = icSigLabData;
if (verb <= 0)
return;
if (p->ttype == icSigColorantTableType
|| p->ttype == icmSigAltColorantTableType)
op->gprintf(op,"ColorantTable:\n");
op->gprintf(op," No. colorants = %u\n",p->count);
if (verb >= 2) {
unsigned int i;
icmColorantTableVal *vp;
for (i = 0; i < p->count; i++) {
vp = p->data + i;
op->gprintf(op," Colorant %lu:\n",i);
op->gprintf(op," Name = '%s'\n",vp->name);
if (p->ttype == icSigColorantTableType
|| p->ttype == icmSigAltColorantTableType) {
switch(pcs) {
case icSigXYZData:
op->gprintf(op," XYZ = %.8f, %.8f, %.8f\n",
vp->pcsCoords[0],vp->pcsCoords[1],vp->pcsCoords[2]);
break;
case icSigLabData:
op->gprintf(op," Lab = %f, %f, %f\n",
vp->pcsCoords[0],vp->pcsCoords[1],vp->pcsCoords[2]);
break;
default:
op->gprintf(op," Unexpected PCS\n");
break;
}
}
}
}
}
/* Allocate variable sized data elements */
static int icmColorantTable_allocate(
icmBase *pp
) {
icmColorantTable *p = (icmColorantTable *)pp;
icc *icp = p->icp;
if (p->count != p->_count) {
unsigned int i;
if (ovr_mul(p->count, sizeof(icmColorantTableVal))) {
sprintf(icp->err,"icmColorantTable_alloc: count overflow (%d of %lu bytes)",
p->count,(unsigned long)sizeof(icmColorantTableVal));
return icp->errc = 1;
}
if (p->data != NULL)
icp->al->free(icp->al, p->data);
if ((p->data = (icmColorantTableVal *) icp->al->calloc(icp->al,p->count, sizeof(icmColorantTableVal))) == NULL) {
sprintf(icp->err,"icmColorantTable_alloc: malloc() of icmColorantTable data failed");
return icp->errc = 2;
}
for (i = 0; i < p->count; i++) {
p->data[i].icp = icp; /* Do init */
}
p->_count = p->count;
}
return 0;
}
/* Free all storage in the object */
static void icmColorantTable_delete(
icmBase *pp
) {
icmColorantTable *p = (icmColorantTable *)pp;
icc *icp = p->icp;
if (p->data != NULL)
icp->al->free(icp->al, p->data);
icp->al->free(icp->al, p);
}
/* Create an empty object. Return null on error */
static icmBase *new_icmColorantTable(
icc *icp
) {
icmColorantTable *p;
if ((p = (icmColorantTable *) icp->al->calloc(icp->al,1,sizeof(icmColorantTable))) == NULL)
return NULL;
p->ttype = icSigColorantTableType;
p->refcount = 1;
p->get_size = icmColorantTable_get_size;
p->read = icmColorantTable_read;
p->write = icmColorantTable_write;
p->dump = icmColorantTable_dump;
p->allocate = icmColorantTable_allocate;
p->del = icmColorantTable_delete;
p->icp = icp;
return (icmBase *)p;
}
/* ---------------------------------------------------------- */
/* textDescription */
/* Return the number of bytes needed to write this tag */
static unsigned int icmTextDescription_get_size(
icmBase *pp
) {
icmTextDescription *p = (icmTextDescription *)pp;
unsigned int len = 0;
len = sat_add(len, 8); /* 8 bytes for tag and padding */
len = sat_addadd(len, 4, p->size); /* Ascii string length + ascii string */
len = sat_addaddmul(len, 8, 2, p->ucSize); /* Unicode language code + length + string */
len = sat_addadd(len, 3, 67); /* ScriptCode code, length string */
return len;
}
/* read the object, return 0 on success, error code on fail */
static int icmTextDescription_read(
icmBase *pp,
unsigned int len, /* tag length */
unsigned int of /* start offset within file */
) {
icmTextDescription *p = (icmTextDescription *)pp;
icc *icp = p->icp;
int rv;
char *bp, *buf, *end;
#ifdef ICM_STRICT
if (len < (8 + 4 + 8 + 3 /* + 67 */)) {
#else
if (len < (8 + 4 + 8 + 3)) {
#endif
sprintf(icp->err,"icmTextDescription_read: Tag too small to be legal");
return icp->errc = 1;
}
/* Allocate a file read buffer */
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmTextDescription_read: malloc() failed");
return icp->errc = 2;
}
bp = buf;
end = buf + len;
/* Read portion of file into buffer */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->read(icp->fp, bp, 1, len) != len) {
sprintf(icp->err,"icmTextDescription_read: fseek() or fread() failed");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
/* Read from the buffer into the structure */
if ((rv = p->core_read(p, &bp, end)) != 0) {
icp->al->free(icp->al, buf);
return rv;
}
icp->al->free(icp->al, buf);
return 0;
}
/* core read the object, return 0 on success, error code on fail */
static int icmTextDescription_core_read(
icmTextDescription *p,
char **bpp, /* Pointer to buffer pointer, returns next after read */
char *end /* Pointer to past end of read buffer */
) {
icc *icp = p->icp;
int rv;
char *bp = *bpp;
if (bp > end || 8 > (end - bp)) {
sprintf(icp->err,"icmTextDescription_read: Data too short to type descriptor");
*bpp = bp;
return icp->errc = 1;
}
p->size = read_UInt32Number(bp);
/* Read type descriptor from the buffer */
if (((icTagTypeSignature)read_SInt32Number(bp)) != p->ttype) {
*bpp = bp;
sprintf(icp->err,"icmTextDescription_read: Wrong tag type ('%s') for icmTextDescription",
tag2str((icTagTypeSignature)read_SInt32Number(bp)));
return icp->errc = 1;
}
bp = bp + 8;
/* Read the Ascii string */
if (bp > end || 4 > (end - bp)) {
*bpp = bp;
sprintf(icp->err,"icmTextDescription_read: Data too short to read Ascii header");
return icp->errc = 1;
}
p->size = read_UInt32Number(bp);
bp += 4;
if (p->size > 0) {
int chrv;
if (bp > end || p->size > (end - bp)) {
*bpp = bp;
sprintf(icp->err,"icmTextDescription_read: Data too short to read Ascii string");
return icp->errc = 1;
}
if ((chrv = check_null_string(bp,p->size)) == 1) {
*bpp = bp;
sprintf(icp->err,"icmTextDescription_read: ascii string is not terminated");
return icp->errc = 1;
}
#ifdef ICM_STRICT
if (chrv == 2) {
*bpp = bp;
sprintf(icp->err,"icmTextDescription_read: ascii string is shorter than count");
return icp->errc = 1;
}
#endif
if ((rv = p->allocate((icmBase *)p)) != 0) {
return rv;
}
strcpy(p->desc, bp);
bp += p->size;
if (chrv == 2)
p->size = strlen(bp); /* Repair string */
}
/* Read the Unicode string */
if (bp > end || 8 > (end - bp)) {
*bpp = bp;
sprintf(icp->err,"icmTextDescription_read: Data too short to read Unicode string");
return icp->errc = 1;
}
p->ucLangCode = read_UInt32Number(bp);
bp += 4;
p->ucSize = read_UInt32Number(bp);
bp += 4;
if (p->ucSize > 0) {
int chrv;
ORD16 *up, len;
char *tbp;
if (bp > end || p->ucSize > (end - bp)/2) {
*bpp = bp;
sprintf(icp->err,"icmTextDescription_read: Data too short to read Unicode string");
return icp->errc = 1;
}
if ((chrv = check_null_string16(bp,p->ucSize)) == 1) {
*bpp = bp;
sprintf(icp->err,"icmTextDescription_read: Unicode string is not terminated");
return icp->errc = 1;
}
#ifdef ICM_STRICT
if (chrv == 2) {
*bpp = bp;
sprintf(icp->err,"icmTextDescription_read: Unicode string is shorter than count");
return icp->errc = 1;
}
#endif
if ((rv = p->allocate((icmBase *)p)) != 0) {
return rv;
}
for (len = 0, up = p->ucDesc, tbp = bp; tbp[0] != 0 || tbp[1] != 0; up++, tbp += 2, len++)
*up = read_UInt16Number(tbp);
*up = 0; /* Unicode null */
bp += p->ucSize * 2;
if (chrv == 2)
p->ucSize = len+1; /* Repair string */
}
/* Read the ScriptCode string */
if (bp > end || 3 > (end - bp)) {
*bpp = bp;
sprintf(icp->err,"icmTextDescription_read: Data too short to read ScriptCode header");
return icp->errc = 1;
}
p->scCode = read_UInt16Number(bp);
bp += 2;
p->scSize = read_UInt8Number(bp);
bp += 1;
if (p->scSize > 0) {
if (p->scSize > 67) {
*bpp = bp;
sprintf(icp->err,"icmTextDescription_read: ScriptCode string too long");
return icp->errc = 1;
}
if (bp > end || p->scSize > (end - bp)) {
*bpp = bp;
sprintf(icp->err,"icmTextDescription_read: Data too short to read ScriptCode string");
return icp->errc = 1;
}
if ((rv = check_null_string(bp,p->scSize)) == 1) {
#ifdef ICM_STRICT
*bpp = bp;
sprintf(icp->err,"icmTextDescription_read: ScriptCode string is not terminated");
return icp->errc = 1;
#else
/* Patch it up */
bp[p->scSize-1] = '\000';
#endif
}
memmove((void *)p->scDesc, (void *)bp, p->scSize);
} else {
memset((void *)p->scDesc, 0, 67);
}
bp += 67;
*bpp = bp;
return 0;
}
/* Write the contents of the object. Return 0 on sucess, error code on failure */
static int icmTextDescription_write(
icmBase *pp,
unsigned int of /* File offset to write from */
) {
icmTextDescription *p = (icmTextDescription *)pp;
icc *icp = p->icp;
unsigned int len;
char *bp, *buf; /* Buffer to write from */
int rv = 0;
/* Allocate a file write buffer */
if ((len = p->get_size((icmBase *)p)) == UINT_MAX) {
sprintf(icp->err,"icmTextDescription_write get_size overflow");
return icp->errc = 1;
}
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmTextDescription_write malloc() failed");
return icp->errc = 2;
}
bp = buf;
/* Write to the buffer from the structure */
if ((rv = p->core_write(p, &bp)) != 0) {
icp->al->free(icp->al, buf);
return rv;
}
/* Write to the file */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->write(icp->fp, buf, 1, len) != len) {
sprintf(icp->err,"icmTextDescription_write fseek() or fwrite() failed");
icp->al->free(icp->al, buf);
return icp->errc = 2;
}
icp->al->free(icp->al, buf);
return 0;
}
/* Core write the contents of the object. Return 0 on sucess, error code on failure */
static int icmTextDescription_core_write(
icmTextDescription *p,
char **bpp /* Pointer to buffer pointer, returns next after write */
) {
icc *icp = p->icp;
char *bp = *bpp;
int rv;
/* Write type descriptor to the buffer */
if ((rv = write_SInt32Number((int)p->ttype,bp)) != 0) {
sprintf(icp->err,"icmTextDescription_write: write_SInt32Number() failed");
*bpp = bp;
return icp->errc = rv;
}
write_SInt32Number(0,bp+4); /* Set padding to 0 */
bp = bp + 8;
/* Write the Ascii string */
if ((rv = write_UInt32Number(p->size,bp)) != 0) {
sprintf(icp->err,"icmTextDescription_write: write_UInt32Number() failed");
*bpp = bp;
return icp->errc = rv;
}
bp += 4;
if (p->size > 0) {
if ((rv = check_null_string(p->desc,p->size)) == 1) {
*bpp = bp;
sprintf(icp->err,"icmTextDescription_write: ascii string is not terminated");
return icp->errc = 1;
}
if (rv == 2) {
*bpp = bp;
sprintf(icp->err,"icmTextDescription_write: ascii string is shorter than length");
return icp->errc = 1;
}
strcpy(bp, p->desc);
bp += strlen(p->desc) + 1;
}
/* Write the Unicode string */
if ((rv = write_UInt32Number(p->ucLangCode, bp)) != 0) {
sprintf(icp->err,"icmTextDescription_write: write_UInt32Number() failed");
*bpp = bp;
return icp->errc = rv;
}
bp += 4;
if ((rv = write_UInt32Number(p->ucSize, bp)) != 0) {
sprintf(icp->err,"icmTextDescription_write: write_UInt32Number() failed");
*bpp = bp;
return icp->errc = rv;
}
bp += 4;
if (p->ucSize > 0) {
ORD16 *up;
if ((rv = check_null_string16((char *)p->ucDesc,p->ucSize)) == 1) {
*bpp = bp;
sprintf(icp->err,"icmTextDescription_write: Unicode string is not terminated");
return icp->errc = 1;
}
if (rv == 2) {
*bpp = bp;
sprintf(icp->err,"icmTextDescription_write: Unicode string is shorter than length");
return icp->errc = 1;
}
for(up = p->ucDesc; *up != 0; up++, bp += 2) {
if ((rv = write_UInt16Number(((unsigned int)*up), bp)) != 0) {
sprintf(icp->err,"icmTextDescription_write: write_UInt16Number() failed");
*bpp = bp;
return icp->errc = rv;
}
}
bp[0] = 0; /* null */
bp[1] = 0;
bp += 2;
}
/* Write the ScriptCode string */
if ((rv = write_UInt16Number(p->scCode, bp)) != 0) {
sprintf(icp->err,"icmTextDescription_write: write_UInt16Number() failed");
*bpp = bp;
return icp->errc = rv;
}
bp += 2;
if ((rv = write_UInt8Number(p->scSize, bp)) != 0) {
sprintf(icp->err,"icmTextDescription_write: write_UInt8Number() failed");
*bpp = bp;
return icp->errc = rv;
}
bp += 1;
if (p->scSize > 0) {
if (p->scSize > 67) {
*bpp = bp;
sprintf(icp->err,"icmTextDescription_write: ScriptCode string too long");
return icp->errc = 1;
}
if ((rv = check_null_string((char *)p->scDesc,p->scSize)) == 1) {
*bpp = bp;
sprintf(icp->err,"icmTextDescription_write: ScriptCode string is not terminated");
return icp->errc = 1;
}
memmove((void *)bp, (void *)p->scDesc, 67);
} else {
memset((void *)bp, 0, 67);
}
bp += 67;
*bpp = bp;
return 0;
}
/* Dump a text description of the object */
static void icmTextDescription_dump(
icmBase *pp,
icmFile *op, /* Output to dump to */
int verb /* Verbosity level */
) {
icmTextDescription *p = (icmTextDescription *)pp;
unsigned int i, r, c;
if (verb <= 0)
return;
op->gprintf(op,"TextDescription:\n");
if (p->size > 0) {
unsigned int size = p->size > 0 ? p->size-1 : 0;
op->gprintf(op," ASCII data, length %lu chars:\n",p->size);
i = 0;
for (r = 1;; r++) { /* count rows */
if (i >= size) {
op->gprintf(op,"\n");
break;
}
if (r > 1 && verb < 2) {
op->gprintf(op,"...\n");
break; /* Print 1 row if not verbose */
}
c = 1;
op->gprintf(op," 0x%04lx: ",i);
c += 10;
while (i < size && c < 75) {
if (isprint(p->desc[i])) {
op->gprintf(op,"%c",p->desc[i]);
c++;
} else {
op->gprintf(op,"\\%03o",p->desc[i]);
c += 4;
}
i++;
}
if (i < size)
op->gprintf(op,"\n");
}
} else {
op->gprintf(op," No ASCII data\n");
}
/* Can't dump Unicode or ScriptCode as text with portable code */
if (p->ucSize > 0) {
unsigned int size = p->ucSize;
op->gprintf(op," Unicode Data, Language code 0x%x, length %lu chars\n",
p->ucLangCode, p->ucSize);
i = 0;
for (r = 1;; r++) { /* count rows */
if (i >= size) {
op->gprintf(op,"\n");
break;
}
if (r > 1 && verb < 2) {
op->gprintf(op,"...\n");
break; /* Print 1 row if not verbose */
}
c = 1;
op->gprintf(op," 0x%04lx: ",i);
c += 10;
while (i < size && c < 75) {
op->gprintf(op,"%04x ",p->ucDesc[i]);
c += 5;
i++;
}
if (i < size)
op->gprintf(op,"\n");
}
} else {
op->gprintf(op," No Unicode data\n");
}
if (p->scSize > 0) {
unsigned int size = p->scSize;
op->gprintf(op," ScriptCode Data, Code 0x%x, length %lu chars\n",
p->scCode, p->scSize);
i = 0;
for (r = 1;; r++) { /* count rows */
if (i >= size) {
op->gprintf(op,"\n");
break;
}
if (r > 1 && verb < 2) {
op->gprintf(op,"...\n");
break; /* Print 1 row if not verbose */
}
c = 1;
op->gprintf(op," 0x%04lx: ",i);
c += 10;
while (i < size && c < 75) {
op->gprintf(op,"%02x ",p->scDesc[i]);
c += 3;
i++;
}
if (i < size)
op->gprintf(op,"\n");
}
} else {
op->gprintf(op," No ScriptCode data\n");
}
}
/* Allocate variable sized data elements */
static int icmTextDescription_allocate(
icmBase *pp
) {
icmTextDescription *p = (icmTextDescription *)pp;
icc *icp = p->icp;
if (p->size != p->_size) {
if (ovr_mul(p->size, sizeof(char))) {
sprintf(icp->err,"icmTextDescription_alloc: size overflow");
return icp->errc = 1;
}
if (p->desc != NULL)
icp->al->free(icp->al, p->desc);
if ((p->desc = (char *) icp->al->calloc(icp->al, p->size, sizeof(char))) == NULL) {
sprintf(icp->err,"icmTextDescription_alloc: malloc() of Ascii description failed");
return icp->errc = 2;
}
p->_size = p->size;
}
if (p->ucSize != p->uc_size) {
if (ovr_mul(p->ucSize, sizeof(ORD16))) {
sprintf(icp->err,"icmTextDescription_alloc: size overflow");
return icp->errc = 1;
}
if (p->ucDesc != NULL)
icp->al->free(icp->al, p->ucDesc);
if ((p->ucDesc = (ORD16 *) icp->al->calloc(icp->al, p->ucSize, sizeof(ORD16))) == NULL) {
sprintf(icp->err,"icmTextDescription_alloc: malloc() of Unicode description failed");
return icp->errc = 2;
}
p->uc_size = p->ucSize;
}
return 0;
}
/* Free all variable sized elements */
static void icmTextDescription_unallocate(
icmTextDescription *p
) {
icc *icp = p->icp;
if (p->desc != NULL)
icp->al->free(icp->al, p->desc);
if (p->ucDesc != NULL)
icp->al->free(icp->al, p->ucDesc);
}
/* Free all storage in the object */
static void icmTextDescription_delete(
icmBase *pp
) {
icmTextDescription *p = (icmTextDescription *)pp;
icc *icp = p->icp;
icmTextDescription_unallocate(p);
icp->al->free(icp->al, p);
}
/* Initialze a named object */
static void icmTextDescription_init(
icmTextDescription *p,
icc *icp
) {
memset((void *)p, 0, sizeof(icmTextDescription)); /* Imitate calloc */
p->ttype = icSigTextDescriptionType;
p->refcount = 1;
p->get_size = icmTextDescription_get_size;
p->read = icmTextDescription_read;
p->write = icmTextDescription_write;
p->dump = icmTextDescription_dump;
p->allocate = icmTextDescription_allocate;
p->del = icmTextDescription_delete;
p->icp = icp;
p->core_read = icmTextDescription_core_read;
p->core_write = icmTextDescription_core_write;
}
/* Create an empty object. Return null on error */
static icmBase *new_icmTextDescription(
icc *icp
) {
icmTextDescription *p;
if ((p = (icmTextDescription *) icp->al->calloc(icp->al,1,sizeof(icmTextDescription))) == NULL)
return NULL;
icmTextDescription_init(p,icp);
return (icmBase *)p;
}
/* ---------------------------------------------------------- */
/* Support for icmDescStruct */
/* Return the number of bytes needed to write this tag */
static unsigned int icmDescStruct_get_size(
icmDescStruct *p
) {
unsigned int len = 0;
len = sat_add(len, 20); /* 20 bytes for header info */
len = sat_add(len, p->device.get_size((icmBase *)&p->device));
if (p->device.size == 0)
len = sat_add(len, 1); /* Extra 1 because of zero length desciption */
len = sat_add(len, p->model.get_size((icmBase *)&p->model));
if (p->model.size == 0)
len = sat_add(len, 1); /* Extra 1 because of zero length desciption */
return len;
}
/* read the object, return 0 on success, error code on fail */
static int icmDescStruct_read(
icmDescStruct *p,
char **bpp, /* Pointer to buffer pointer, returns next after read */
char *end /* Pointer to past end of read buffer */
) {
icc *icp = p->icp;
char *bp = *bpp;
int rv = 0;
if (bp > end || 20 > (end - bp)) {
sprintf(icp->err,"icmDescStruct_read: Data too short read header");
*bpp = bp;
return icp->errc = 1;
}
p->deviceMfg = read_SInt32Number(bp + 0);
p->deviceModel = read_UInt32Number(bp + 4);
read_UInt64Number(&p->attributes, bp + 8);
p->technology = (icTechnologySignature) read_UInt32Number(bp + 16);
*bpp = bp += 20;
/* Read the device text description */
if ((rv = p->device.core_read(&p->device, bpp, end)) != 0) {
return rv;
}
/* Read the model text description */
if ((rv = p->model.core_read(&p->model, bpp, end)) != 0) {
return rv;
}
return 0;
}
/* Write the contents of the object. Return 0 on sucess, error code on failure */
static int icmDescStruct_write(
icmDescStruct *p,
char **bpp /* Pointer to buffer pointer, returns next after read */
) {
icc *icp = p->icp;
char *bp = *bpp;
int rv = 0;
char *ttd = NULL;
unsigned int tts = 0;
if ((rv = write_SInt32Number(p->deviceMfg, bp + 0)) != 0) {
sprintf(icp->err,"icmDescStruct_write: write_SInt32Number() failed");
*bpp = bp;
return icp->errc = rv;
}
if ((rv = write_UInt32Number(p->deviceModel, bp + 4)) != 0) {
sprintf(icp->err,"icmDescStruct_write: write_UInt32Number() failed");
*bpp = bp;
return icp->errc = rv;
}
if ((rv = write_UInt64Number(&p->attributes, bp + 8)) != 0) {
sprintf(icp->err,"icmDescStruct_write: write_UInt64Number() failed");
*bpp = bp;
return icp->errc = rv;
}
if ((rv = write_UInt32Number(p->technology, bp + 16)) != 0) {
sprintf(icp->err,"icmDescStruct_write: write_UInt32Number() failed");
*bpp = bp;
return icp->errc = rv;
}
*bpp = bp += 20;
/* Make sure the ASCII device text is a minimum size of 1, as per the spec. */
ttd = p->device.desc;
tts = p->device.size;
if (p->device.size == 0) {
p->device.desc = "";
p->device.size = 1;
}
/* Write the device text description */
if ((rv = p->device.core_write(&p->device, bpp)) != 0) {
return rv;
}
p->device.desc = ttd;
p->device.size = tts;
/* Make sure the ASCII model text is a minimum size of 1, as per the spec. */
ttd = p->model.desc;
tts = p->model.size;
if (p->model.size == 0) {
p->model.desc = "";
p->model.size = 1;
}
/* Write the model text description */
if ((rv = p->model.core_write(&p->model, bpp)) != 0) {
return rv;
}
p->model.desc = ttd;
p->model.size = tts;
/* Make sure the ASCII model text is a minimum size of 1, as per the spec. */
ttd = p->device.desc;
tts = p->device.size;
return 0;
}
/* Dump a text description of the object */
static void icmDescStruct_dump(
icmDescStruct *p,
icmFile *op, /* Output to dump to */
int verb, /* Verbosity level */
int index /* Description index */
) {
if (verb <= 0)
return;
op->gprintf(op,"DescStruct %u:\n",index);
if (verb >= 1) {
op->gprintf(op," Dev. Mnfctr. = %s\n",tag2str(p->deviceMfg)); /* ~~~ */
op->gprintf(op," Dev. Model = %s\n",tag2str(p->deviceModel)); /* ~~~ */
op->gprintf(op," Dev. Attrbts = %s\n", string_DeviceAttributes(p->attributes.l));
op->gprintf(op," Dev. Technology = %s\n", string_TechnologySignature(p->technology));
p->device.dump((icmBase *)&p->device, op,verb);
p->model.dump((icmBase *)&p->model, op,verb);
op->gprintf(op,"\n");
}
}
/* Allocate variable sized data elements (ie. descriptions) */
static int icmDescStruct_allocate(
icmDescStruct *p
) {
int rv;
if ((rv = p->device.allocate((icmBase *)&p->device)) != 0) {
return rv;
}
if ((rv = p->model.allocate((icmBase *)&p->model)) != 0) {
return rv;
}
return 0;
}
/* Free all storage in the object */
static void icmDescStruct_delete(
icmDescStruct *p
) {
icmTextDescription_unallocate(&p->device);
icmTextDescription_unallocate(&p->model);
}
/* Init a DescStruct object */
static void icmDescStruct_init(
icmDescStruct *p,
icc *icp
) {
p->allocate = icmDescStruct_allocate;
p->icp = icp;
icmTextDescription_init(&p->device, icp);
icmTextDescription_init(&p->model, icp);
}
/* - - - - - - - - - - - - - - - */
/* icmProfileSequenceDesc object */
/* Return the number of bytes needed to write this tag */
static unsigned int icmProfileSequenceDesc_get_size(
icmBase *pp
) {
icmProfileSequenceDesc *p = (icmProfileSequenceDesc *)pp;
unsigned int len = 0;
unsigned int i;
len = sat_add(len, 12); /* 12 bytes for tag, padding and count */
for (i = 0; i < p->count; i++) { /* All the description structures */
len = sat_add(len, icmDescStruct_get_size(&p->data[i]));
}
return len;
}
/* read the object, return 0 on success, error code on fail */
static int icmProfileSequenceDesc_read(
icmBase *pp,
unsigned int len, /* tag length */
unsigned int of /* start offset within file */
) {
icmProfileSequenceDesc *p = (icmProfileSequenceDesc *)pp;
icc *icp = p->icp;
unsigned int i;
char *bp, *buf, *end;
int rv = 0;
if (len < 12) {
sprintf(icp->err,"icmProfileSequenceDesc_read: Tag too small to be legal");
return icp->errc = 1;
}
/* Allocate a file read buffer */
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmProfileSequenceDesc_read: malloc() failed");
return icp->errc = 2;
}
bp = buf;
end = buf + len;
/* Read portion of file into buffer */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->read(icp->fp, bp, 1, len) != len) {
sprintf(icp->err,"icmProfileSequenceDesc_read: fseek() or fread() failed");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
/* Read type descriptor from the buffer */
if (((icTagTypeSignature)read_SInt32Number(bp)) != p->ttype) {
sprintf(icp->err,"icmProfileSequenceDesc_read: Wrong tag type for icmProfileSequenceDesc");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
bp += 8; /* Skip padding */
p->count = read_UInt32Number(bp); /* Number of sequence descriptions */
bp += 4;
/* Read all the sequence descriptions */
if ((rv = p->allocate((icmBase *)p)) != 0) {
icp->al->free(icp->al, buf);
return rv;
}
for (i = 0; i < p->count; i++) {
if ((rv = icmDescStruct_read(&p->data[i], &bp, end)) != 0) {
icp->al->free(icp->al, buf);
return rv;
}
}
icp->al->free(icp->al, buf);
return 0;
}
/* Write the contents of the object. Return 0 on sucess, error code on failure */
static int icmProfileSequenceDesc_write(
icmBase *pp,
unsigned int of /* File offset to write from */
) {
icmProfileSequenceDesc *p = (icmProfileSequenceDesc *)pp;
icc *icp = p->icp;
unsigned int i;
unsigned int len;
char *bp, *buf; /* Buffer to write from */
int rv = 0;
/* Allocate a file write buffer */
if ((len = p->get_size((icmBase *)p)) == UINT_MAX) {
sprintf(icp->err,"icmProfileSequenceDesc_write get_size overflow");
return icp->errc = 1;
}
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmProfileSequenceDesc_write malloc() failed");
return icp->errc = 2;
}
bp = buf;
/* Write type descriptor to the buffer */
if ((rv = write_SInt32Number((int)p->ttype,bp)) != 0) {
sprintf(icp->err,"icmProfileSequenceDesc_write: write_SInt32Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
write_SInt32Number(0,bp+4); /* Set padding to 0 */
if ((rv = write_UInt32Number(p->count,bp+8)) != 0) {
sprintf(icp->err,"icmProfileSequenceDesc_write: write_UInt32Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
bp = bp + 12;
/* Write all the description structures */
for (i = 0; i < p->count; i++) {
if ((rv = icmDescStruct_write(&p->data[i], &bp)) != 0) {
icp->al->free(icp->al, buf);
return rv;
}
}
/* Write to the file */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->write(icp->fp, buf, 1, len) != len) {
sprintf(icp->err,"icmProfileSequenceDesc_write fseek() or fwrite() failed");
icp->al->free(icp->al, buf);
return icp->errc = 2;
}
icp->al->free(icp->al, buf);
return 0;
}
/* Dump a text description of the object */
static void icmProfileSequenceDesc_dump(
icmBase *pp,
icmFile *op, /* Output to dump to */
int verb /* Verbosity level */
) {
icmProfileSequenceDesc *p = (icmProfileSequenceDesc *)pp;
if (verb <= 0)
return;
op->gprintf(op,"ProfileSequenceDesc:\n");
op->gprintf(op," No. elements = %u\n",p->count);
if (verb >= 2) {
unsigned int i;
for (i = 0; i < p->count; i++)
icmDescStruct_dump(&p->data[i], op, verb-1, i);
}
}
/* Allocate variable sized data elements (ie. count of profile descriptions) */
static int icmProfileSequenceDesc_allocate(
icmBase *pp
) {
icmProfileSequenceDesc *p = (icmProfileSequenceDesc *)pp;
icc *icp = p->icp;
unsigned int i;
if (p->count != p->_count) {
if (ovr_mul(p->count, sizeof(icmDescStruct))) {
sprintf(icp->err,"icmProfileSequenceDesc_allocate: size overflow");
return icp->errc = 1;
}
if (p->data != NULL)
icp->al->free(icp->al, p->data);
if ((p->data = (icmDescStruct *) icp->al->calloc(icp->al, p->count, sizeof(icmDescStruct))) == NULL) {
sprintf(icp->err,"icmProfileSequenceDesc_allocate Allocation of DescStruct array failed");
return icp->errc = 2;
}
/* Now init the DescStructs */
for (i = 0; i < p->count; i++) {
icmDescStruct_init(&p->data[i], icp);
}
p->_count = p->count;
}
return 0;
}
/* Free all storage in the object */
static void icmProfileSequenceDesc_delete(
icmBase *pp
) {
icmProfileSequenceDesc *p = (icmProfileSequenceDesc *)pp;
icc *icp = p->icp;
unsigned int i;
for (i = 0; i < p->count; i++) {
icmDescStruct_delete(&p->data[i]); /* Free allocated contents */
}
if (p->data != NULL)
icp->al->free(icp->al, p->data);
icp->al->free(icp->al, p);
}
/* Create an empty object. Return null on error */
static icmBase *new_icmProfileSequenceDesc(
icc *icp
) {
icmProfileSequenceDesc *p;
if ((p = (icmProfileSequenceDesc *) icp->al->calloc(icp->al,1,sizeof(icmProfileSequenceDesc))) == NULL)
return NULL;
p->ttype = icSigProfileSequenceDescType;
p->refcount = 1;
p->get_size = icmProfileSequenceDesc_get_size;
p->read = icmProfileSequenceDesc_read;
p->write = icmProfileSequenceDesc_write;
p->dump = icmProfileSequenceDesc_dump;
p->allocate = icmProfileSequenceDesc_allocate;
p->del = icmProfileSequenceDesc_delete;
p->icp = icp;
return (icmBase *)p;
}
/* ---------------------------------------------------------- */
/* Signature */
/* Return the number of bytes needed to write this tag */
static unsigned int icmSignature_get_size(
icmBase *pp
) {
unsigned int len = 0;
len = sat_add(len, 8); /* 8 bytes for tag and padding */
len = sat_add(len, 4); /* 4 for signature */
return len;
}
/* read the object, return 0 on success, error code on fail */
static int icmSignature_read(
icmBase *pp,
unsigned int len, /* tag length */
unsigned int of /* start offset within file */
) {
icmSignature *p = (icmSignature *)pp;
icc *icp = p->icp;
char *bp, *buf;
if (len < 12) {
sprintf(icp->err,"icmSignature_read: Tag too small to be legal");
return icp->errc = 1;
}
/* Allocate a file read buffer */
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmSignature_read: malloc() failed");
return icp->errc = 2;
}
bp = buf;
/* Read portion of file into buffer */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->read(icp->fp, bp, 1, len) != len) {
sprintf(icp->err,"icmSignature_read: fseek() or fread() failed");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
/* Read type descriptor from the buffer */
if (((icTagTypeSignature)read_SInt32Number(bp)) != p->ttype) {
sprintf(icp->err,"icmSignaturSignatureng tag type for icmSignature");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
/* Read the encoded measurement geometry */
p->sig = (icTechnologySignature)read_SInt32Number(bp + 8);
icp->al->free(icp->al, buf);
return 0;
}
/* Write the contents of the object. Return 0 on sucess, error code on failure */
static int icmSignature_write(
icmBase *pp,
unsigned int of /* File offset to write from */
) {
icmSignature *p = (icmSignature *)pp;
icc *icp = p->icp;
unsigned int len;
char *bp, *buf; /* Buffer to write from */
int rv = 0;
/* Allocate a file write buffer */
if ((len = p->get_size((icmBase *)p)) == UINT_MAX) {
sprintf(icp->err,"icmSignature_write get_size overflow");
return icp->errc = 1;
}
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmSignature_write malloc() failed");
return icp->errc = 2;
}
bp = buf;
/* Write type descriptor to the buffer */
if ((rv = write_SInt32Number((int)p->ttype,bp)) != 0) {
sprintf(icp->err,"icmSignature_write: write_SInt32Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
write_SInt32Number(0,bp+4); /* Set padding to 0 */
/* Write the signature */
if ((rv = write_SInt32Number((int)p->sig, bp + 8)) != 0) {
sprintf(icp->err,"icmSignaturea_write: write_SInt32Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
/* Write to the file */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->write(icp->fp, buf, 1, len) != len) {
sprintf(icp->err,"icmSignature_write fseek() or fwrite() failed");
icp->al->free(icp->al, buf);
return icp->errc = 2;
}
icp->al->free(icp->al, buf);
return 0;
}
/* Dump a text description of the object */
static void icmSignature_dump(
icmBase *pp,
icmFile *op, /* Output to dump to */
int verb /* Verbosity level */
) {
icmSignature *p = (icmSignature *)pp;
if (verb <= 0)
return;
op->gprintf(op,"Signature\n");
op->gprintf(op," Technology = %s\n", string_TechnologySignature(p->sig));
}
/* Allocate variable sized data elements */
static int icmSignature_allocate(
icmBase *pp
) {
/* Nothing to do */
return 0;
}
/* Free all storage in the object */
static void icmSignature_delete(
icmBase *pp
) {
icmSignature *p = (icmSignature *)pp;
icc *icp = p->icp;
icp->al->free(icp->al, p);
}
/* Create an empty object. Return null on error */
static icmBase *new_icmSignature(
icc *icp
) {
icmSignature *p;
if ((p = (icmSignature *) icp->al->calloc(icp->al,1,sizeof(icmSignature))) == NULL)
return NULL;
p->ttype = icSigSignatureType;
p->refcount = 1;
p->get_size = icmSignature_get_size;
p->read = icmSignature_read;
p->write = icmSignature_write;
p->dump = icmSignature_dump;
p->allocate = icmSignature_allocate;
p->del = icmSignature_delete;
p->icp = icp;
return (icmBase *)p;
}
/* ---------------------------------------------------------- */
/* Data conversion support functions */
static int read_ScreeningData(icmScreeningData *p, char *d) {
p->frequency = read_S15Fixed16Number(d + 0);
p->angle = read_S15Fixed16Number(d + 4);
p->spotShape = (icSpotShape)read_SInt32Number(d + 8);
return 0;
}
static int write_ScreeningData(icmScreeningData *p, char *d) {
int rv;
if ((rv = write_S15Fixed16Number(p->frequency, d + 0)) != 0)
return rv;
if ((rv = write_S15Fixed16Number(p->angle, d + 4)) != 0)
return rv;
if ((rv = write_SInt32Number((int)p->spotShape, d + 8)) != 0)
return rv;
return 0;
}
/* icmScreening object */
/* Return the number of bytes needed to write this tag */
static unsigned int icmScreening_get_size(
icmBase *pp
) {
icmScreening *p = (icmScreening *)pp;
unsigned int len = 0;
len = sat_add(len, 16); /* 16 bytes for tag, padding, flag & channeles */
len = sat_addmul(len, p->channels, 12); /* 12 bytes for each channel */
return len;
}
/* read the object, return 0 on success, error code on fail */
static int icmScreening_read(
icmBase *pp,
unsigned int len, /* tag length */
unsigned int of /* start offset within file */
) {
icmScreening *p = (icmScreening *)pp;
icc *icp = p->icp;
int rv = 0;
unsigned int i;
char *bp, *buf, *end;
if (len < 12) {
sprintf(icp->err,"icmScreening_read: Tag too small to be legal");
return icp->errc = 1;
}
/* Allocate a file read buffer */
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmScreening_read: malloc() failed");
return icp->errc = 2;
}
bp = buf;
end = buf + len;
/* Read portion of file into buffer */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->read(icp->fp, bp, 1, len) != len) {
sprintf(icp->err,"icmScreening_read: fseek() or fread() failed");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
/* Read type descriptor from the buffer */
if (((icTagTypeSignature)read_SInt32Number(bp)) != p->ttype) {
sprintf(icp->err,"icmScreening_read: Wrong tag type for icmScreening");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
p->screeningFlag = read_UInt32Number(bp+8); /* Flags */
p->channels = read_UInt32Number(bp+12); /* Number of channels */
bp = bp + 16;
if ((rv = p->allocate((icmBase *)p)) != 0) {
icp->al->free(icp->al, buf);
return rv;
}
/* Read all the data from the buffer */
for (i = 0; i < p->channels; i++, bp += 12) {
if (bp > end || 12 > (end - bp)) {
sprintf(icp->err,"icmScreening_read: Data too short to read Screening Data");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
read_ScreeningData(&p->data[i], bp);
}
icp->al->free(icp->al, buf);
return 0;
}
/* Write the contents of the object. Return 0 on sucess, error code on failure */
static int icmScreening_write(
icmBase *pp,
unsigned int of /* File offset to write from */
) {
icmScreening *p = (icmScreening *)pp;
icc *icp = p->icp;
unsigned int i;
unsigned int len;
char *bp, *buf; /* Buffer to write from */
int rv = 0;
/* Allocate a file write buffer */
if ((len = p->get_size((icmBase *)p)) == UINT_MAX) {
sprintf(icp->err,"icmScreening_write get_size overflow");
return icp->errc = 1;
}
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmScreening_write malloc() failed");
return icp->errc = 2;
}
bp = buf;
/* Write type descriptor to the buffer */
if ((rv = write_SInt32Number((int)p->ttype,bp)) != 0) {
sprintf(icp->err,"icmScreening_write: write_SInt32Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
write_SInt32Number(0,bp+4); /* Set padding to 0 */
if ((rv = write_UInt32Number(p->screeningFlag,bp+8)) != 0) {
sprintf(icp->err,"icmScreening_write: write_UInt32Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
if ((rv = write_UInt32Number(p->channels,bp+12)) != 0) {
sprintf(icp->err,"icmScreening_write: write_UInt32NumberXYZumber() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
bp = bp + 16;
/* Write all the data to the buffer */
for (i = 0; i < p->channels; i++, bp += 12) {
if ((rv = write_ScreeningData(&p->data[i],bp)) != 0) {
sprintf(icp->err,"icmScreening_write: write_ScreeningData() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
}
/* Write to the file */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->write(icp->fp, buf, 1, len) != len) {
sprintf(icp->err,"icmScreening_write fseek() or fwrite() failed");
icp->al->free(icp->al, buf);
return icp->errc = 2;
}
icp->al->free(icp->al, buf);
return 0;
}
/* Dump a text description of the object */
static void icmScreening_dump(
icmBase *pp,
icmFile *op, /* Output to dump to */
int verb /* Verbosity level */
) {
icmScreening *p = (icmScreening *)pp;
if (verb <= 0)
return;
op->gprintf(op,"Screening:\n");
op->gprintf(op," Flags = %s\n", string_ScreenEncodings(p->screeningFlag));
op->gprintf(op," No. channels = %u\n",p->channels);
if (verb >= 2) {
unsigned int i;
for (i = 0; i < p->channels; i++) {
op->gprintf(op," %lu:\n",i);
op->gprintf(op," Frequency: %f\n",p->data[i].frequency);
op->gprintf(op," Angle: %f\n",p->data[i].angle);
op->gprintf(op," Spot shape: %s\n", string_SpotShape(p->data[i].spotShape));
}
}
}
/* Allocate variable sized data elements */
static int icmScreening_allocate(
icmBase *pp
) {
icmScreening *p = (icmScreening *)pp;
icc *icp = p->icp;
if (p->channels != p->_channels) {
if (ovr_mul(p->channels, sizeof(icmScreeningData))) {
sprintf(icp->err,"icmScreening_alloc: size overflow");
return icp->errc = 1;
}
if (p->data != NULL)
icp->al->free(icp->al, p->data);
if ((p->data = (icmScreeningData *) icp->al->malloc(icp->al, p->channels * sizeof(icmScreeningData))) == NULL) {
sprintf(icp->err,"icmScreening_alloc: malloc() of icmScreening data failed");
return icp->errc = 2;
}
p->_channels = p->channels;
}
return 0;
}
/* Free all storage in the object */
static void icmScreening_delete(
icmBase *pp
) {
icmScreening *p = (icmScreening *)pp;
icc *icp = p->icp;
if (p->data != NULL)
icp->al->free(icp->al, p->data);
icp->al->free(icp->al, p);
}
/* Create an empty object. Return null on error */
static icmBase *new_icmScreening(
icc *icp
) {
icmScreening *p;
if ((p = (icmScreening *) icp->al->calloc(icp->al,1,sizeof(icmScreening))) == NULL)
return NULL;
p->ttype = icSigScreeningType;
p->refcount = 1;
p->get_size = icmScreening_get_size;
p->read = icmScreening_read;
p->write = icmScreening_write;
p->dump = icmScreening_dump;
p->allocate = icmScreening_allocate;
p->del = icmScreening_delete;
p->icp = icp;
return (icmBase *)p;
}
/* ---------------------------------------------------------- */
/* icmUcrBg object */
/* Return the number of bytes needed to write this tag */
static unsigned int icmUcrBg_get_size(
icmBase *pp
) {
icmUcrBg *p = (icmUcrBg *)pp;
unsigned int len = 0;
len = sat_add(len, 8); /* 8 bytes for tag and padding */
len = sat_addaddmul(len, 4, p->UCRcount, 2); /* Undercolor Removal */
len = sat_addaddmul(len, 4, p->BGcount, 2); /* Black Generation */
len = sat_add(len, p->size); /* Description string */
return len;
}
/* read the object, return 0 on success, error code on fail */
static int icmUcrBg_read(
icmBase *pp,
unsigned int len, /* tag length */
unsigned int of /* start offset within file */
) {
icmUcrBg *p = (icmUcrBg *)pp;
icc *icp = p->icp;
unsigned int i;
int rv;
char *bp, *buf, *end;
if (len < 16) {
sprintf(icp->err,"icmUcrBg_read: Tag too small to be legal");
return icp->errc = 1;
}
/* Allocate a file read buffer */
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmUcrBg_read: malloc() failed");
return icp->errc = 2;
}
bp = buf;
end = buf + len;
/* Read portion of file into buffer */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->read(icp->fp, bp, 1, len) != len) {
sprintf(icp->err,"icmUcrBg_read: fseek() or fread() failed");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
/* Read type descriptor from the buffer */
if (((icTagTypeSignature)read_SInt32Number(bp)) != p->ttype) {
sprintf(icp->err,"icmUcrBg_read: Wrong tag type for icmUcrBg");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
p->UCRcount = read_UInt32Number(bp+8); /* First curve count */
bp = bp + 12;
if (p->UCRcount > 0) {
if ((rv = p->allocate((icmBase *)p)) != 0) {
icp->al->free(icp->al, buf);
return rv;
}
for (i = 0; i < p->UCRcount; i++, bp += 2) {
if (bp > end || 2 > (end - bp)) {
sprintf(icp->err,"icmUcrBg_read: Data too short to read UCR Data");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
if (p->UCRcount == 1) /* % */
p->UCRcurve[i] = (double)read_UInt16Number(bp);
else /* 0.0 - 1.0 */
p->UCRcurve[i] = read_DCS16Number(bp);
}
} else {
p->UCRcurve = NULL;
}
if (bp > end || 4 > (end - bp)) {
sprintf(icp->err,"icmData_read: Data too short to read Black Gen count");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
p->BGcount = read_UInt32Number(bp); /* First curve count */
bp += 4;
if (p->BGcount > 0) {
if ((rv = p->allocate((icmBase *)p)) != 0) {
icp->al->free(icp->al, buf);
return rv;
}
for (i = 0; i < p->BGcount; i++, bp += 2) {
if (bp > end || 2 > (end - bp)) {
sprintf(icp->err,"icmUcrBg_read: Data too short to read BG Data");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
if (p->BGcount == 1) /* % */
p->BGcurve[i] = (double)read_UInt16Number(bp);
else /* 0.0 - 1.0 */
p->BGcurve[i] = read_DCS16Number(bp);
}
} else {
p->BGcurve = NULL;
}
p->size = end - bp; /* Nominal string length */
if (p->size > 0) {
if ((rv = check_null_string(bp, p->size)) == 1) {
sprintf(icp->err,"icmUcrBg_read: string is not null terminated");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
p->size = strlen(bp) + 1;
if ((rv = p->allocate((icmBase *)p)) != 0) {
icp->al->free(icp->al, buf);
return rv;
}
memmove((void *)p->string, (void *)bp, p->size);
bp += p->size;
} else {
p->string = NULL;
}
icp->al->free(icp->al, buf);
return 0;
}
/* Write the contents of the object. Return 0 on sucess, error code on failure */
static int icmUcrBg_write(
icmBase *pp,
unsigned int of /* File offset to write from */
) {
icmUcrBg *p = (icmUcrBg *)pp;
icc *icp = p->icp;
unsigned int i;
unsigned int len;
char *bp, *buf; /* Buffer to write from */
int rv;
/* Allocate a file write buffer */
if ((len = p->get_size((icmBase *)p)) == UINT_MAX) {
sprintf(icp->err,"icmUcrBg_write get_size overflow");
return icp->errc = 1;
}
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmUcrBg_write malloc() failed");
return icp->errc = 2;
}
bp = buf;
/* Write type descriptor to the buffer */
if ((rv = write_SInt32Number((int)p->ttype,bp)) != 0) {
sprintf(icp->err,"icmUcrBg_write: write_SInt32Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
write_SInt32Number(0,bp+4); /* Set padding to 0 */
bp = bp + 8;
/* Write UCR curve */
if ((rv = write_UInt32Number(p->UCRcount,bp)) != 0) {
sprintf(icp->err,"icmUcrBg_write: write_UInt32Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
bp += 4;
for (i = 0; i < p->UCRcount; i++, bp += 2) {
if (p->UCRcount == 1) { /* % */
if ((rv = write_UInt16Number((unsigned int)(p->UCRcurve[i]+0.5),bp)) != 0) {
sprintf(icp->err,"icmUcrBg_write: write_UInt16umber() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
} else {
if ((rv = write_DCS16Number(p->UCRcurve[i],bp)) != 0) {
sprintf(icp->err,"icmUcrBg_write: write_DCS16umber(%.8f) failed",p->UCRcurve[i]);
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
}
}
/* Write BG curve */
if ((rv = write_UInt32Number(p->BGcount,bp)) != 0) {
sprintf(icp->err,"icmUcrBg_write: write_UInt32Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
bp += 4;
for (i = 0; i < p->BGcount; i++, bp += 2) {
if (p->BGcount == 1) { /* % */
if ((rv = write_UInt16Number((unsigned int)(p->BGcurve[i]+0.5),bp)) != 0) {
sprintf(icp->err,"icmUcrBg_write: write_UInt16umber() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
} else {
if ((rv = write_DCS16Number(p->BGcurve[i],bp)) != 0) {
sprintf(icp->err,"icmUcrBg_write: write_DCS16umber(%.8f) failed",p->BGcurve[i]);
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
}
}
if (p->string != NULL) {
if ((rv = check_null_string(p->string,p->size)) == 1) {
sprintf(icp->err,"icmUcrBg_write: text is not null terminated");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
if (rv == 2) {
sprintf(icp->err,"icmUcrBg_write: text is shorter than length");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
memmove((void *)bp, (void *)p->string, p->size);
}
/* Write to the file */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->write(icp->fp, buf, 1, len) != len) {
sprintf(icp->err,"icmUcrBg_write fseek() or fwrite() failed");
icp->al->free(icp->al, buf);
return icp->errc = 2;
}
icp->al->free(icp->al, buf);
return 0;
}
/* Dump a text description of the object */
static void icmUcrBg_dump(
icmBase *pp,
icmFile *op, /* Output to dump to */
int verb /* Verbosity level */
) {
icmUcrBg *p = (icmUcrBg *)pp;
if (verb <= 0)
return;
op->gprintf(op,"Undercolor Removal Curve & Black Generation:\n");
if (p->UCRcount == 0) {
op->gprintf(op," UCR: Not specified\n");
} else if (p->UCRcount == 1) {
op->gprintf(op," UCR: %f%%\n",p->UCRcurve[0]);
} else {
op->gprintf(op," UCR curve no. elements = %u\n",p->UCRcount);
if (verb >= 2) {
unsigned int i;
for (i = 0; i < p->UCRcount; i++)
op->gprintf(op," %3lu: %f\n",i,p->UCRcurve[i]);
}
}
if (p->BGcount == 0) {
op->gprintf(op," BG: Not specified\n");
} else if (p->BGcount == 1) {
op->gprintf(op," BG: %f%%\n",p->BGcurve[0]);
} else {
op->gprintf(op," BG curve no. elements = %u\n",p->BGcount);
if (verb >= 2) {
unsigned int i;
for (i = 0; i < p->BGcount; i++)
op->gprintf(op," %3lu: %f\n",i,p->BGcurve[i]);
}
}
{
unsigned int i, r, c, size;
op->gprintf(op," Description:\n");
op->gprintf(op," No. chars = %lu\n",p->size);
size = p->size > 0 ? p->size-1 : 0;
i = 0;
for (r = 1;; r++) { /* count rows */
if (i >= size) {
op->gprintf(op,"\n");
break;
}
if (r > 1 && verb < 2) {
op->gprintf(op,"...\n");
break; /* Print 1 row if not verbose */
}
c = 1;
op->gprintf(op," 0x%04lx: ",i);
c += 10;
while (i < size && c < 73) {
if (isprint(p->string[i])) {
op->gprintf(op,"%c",p->string[i]);
c++;
} else {
op->gprintf(op,"\\%03o",p->string[i]);
c += 4;
}
i++;
}
if (i < size)
op->gprintf(op,"\n");
}
}
}
/* Allocate variable sized data elements */
static int icmUcrBg_allocate(
icmBase *pp
) {
icmUcrBg *p = (icmUcrBg *)pp;
icc *icp = p->icp;
if (p->UCRcount != p->UCR_count) {
if (ovr_mul(p->UCRcount, sizeof(double))) {
sprintf(icp->err,"icmUcrBg_allocate: size overflow");
return icp->errc = 1;
}
if (p->UCRcurve != NULL)
icp->al->free(icp->al, p->UCRcurve);
if ((p->UCRcurve = (double *) icp->al->calloc(icp->al, p->UCRcount, sizeof(double))) == NULL) {
sprintf(icp->err,"icmUcrBg_allocate: malloc() of UCR curve data failed");
return icp->errc = 2;
}
p->UCR_count = p->UCRcount;
}
if (p->BGcount != p->BG_count) {
if (ovr_mul(p->BGcount, sizeof(double))) {
sprintf(icp->err,"icmUcrBg_allocate: size overflow");
return icp->errc = 1;
}
if (p->BGcurve != NULL)
icp->al->free(icp->al, p->BGcurve);
if ((p->BGcurve = (double *) icp->al->calloc(icp->al, p->BGcount, sizeof(double))) == NULL) {
sprintf(icp->err,"icmUcrBg_allocate: malloc() of BG curve data failed");
return icp->errc = 2;
}
p->BG_count = p->BGcount;
}
if (p->size != p->_size) {
if (ovr_mul(p->size, sizeof(char))) {
sprintf(icp->err,"icmUcrBg_allocate: size overflow");
return icp->errc = 1;
}
if (p->string != NULL)
icp->al->free(icp->al, p->string);
if ((p->string = (char *) icp->al->calloc(icp->al, p->size, sizeof(char))) == NULL) {
sprintf(icp->err,"icmUcrBg_allocate: malloc() of string data failed");
return icp->errc = 2;
}
p->_size = p->size;
}
return 0;
}
/* Free all storage in the object */
static void icmUcrBg_delete(
icmBase *pp
) {
icmUcrBg *p = (icmUcrBg *)pp;
icc *icp = p->icp;
if (p->UCRcurve != NULL)
icp->al->free(icp->al, p->UCRcurve);
if (p->BGcurve != NULL)
icp->al->free(icp->al, p->BGcurve);
if (p->string != NULL)
icp->al->free(icp->al, p->string);
icp->al->free(icp->al, p);
}
/* Create an empty object. Return null on error */
static icmBase *new_icmUcrBg(
icc *icp
) {
icmUcrBg *p;
if ((p = (icmUcrBg *) icp->al->calloc(icp->al,1,sizeof(icmUcrBg))) == NULL)
return NULL;
p->ttype = icSigUcrBgType;
p->refcount = 1;
p->get_size = icmUcrBg_get_size;
p->read = icmUcrBg_read;
p->write = icmUcrBg_write;
p->dump = icmUcrBg_dump;
p->allocate = icmUcrBg_allocate;
p->del = icmUcrBg_delete;
p->icp = icp;
return (icmBase *)p;
}
/* ---------------------------------------------------------- */
/* VideoCardGamma (ColorSync 2.5 specific - c/o Neil Okamoto) */
/* 'vcgt' */
static unsigned int icmVideoCardGamma_get_size(
icmBase *pp
) {
icmVideoCardGamma *p = (icmVideoCardGamma *)pp;
unsigned int len = 0;
len = sat_add(len, 8); /* 8 bytes for tag and padding */
len = sat_add(len, 4); /* 4 for gamma type */
/* compute size of remainder */
if (p->tagType == icmVideoCardGammaTableType) {
len = sat_add(len, 2); /* 2 bytes for channels */
len = sat_add(len, 2); /* 2 for entry count */
len = sat_add(len, 2); /* 2 for entry size */
len = sat_add(len, sat_mul3(p->u.table.channels, /* compute table size */
p->u.table.entryCount, p->u.table.entrySize));
}
else if (p->tagType == icmVideoCardGammaFormulaType) {
len = sat_add(len, 12); /* 4 bytes each for red gamma, min, & max */
len = sat_add(len, 12); /* 4 bytes each for green gamma, min & max */
len = sat_add(len, 12); /* 4 bytes each for blue gamma, min & max */
}
return len;
}
/* read the object, return 0 on success, error code on fail */
static int icmVideoCardGamma_read(
icmBase *pp,
unsigned int len, /* tag length */
unsigned int of /* start offset within file */
) {
icmVideoCardGamma *p = (icmVideoCardGamma *)pp;
icc *icp = p->icp;
int rv, c;
char *bp, *buf;
ORD8 *pchar;
ORD16 *pshort;
if (len < 18) {
sprintf(icp->err,"icmVideoCardGamma_read: Tag too small to be legal");
return icp->errc = 1;
}
/* Allocate a file read buffer */
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmVideoCardGamma_read: malloc() failed");
return icp->errc = 2;
}
bp = buf;
/* Read portion of file into buffer */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->read(icp->fp, bp, 1, len) != len) {
sprintf(icp->err,"icmVideoCardGamma_read: fseek() or fread() failed");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
/* Read type descriptor from the buffer */
if (((icTagTypeSignature)read_SInt32Number(bp)) != p->ttype) {
sprintf(icp->err,"icmVideoCardGamma_read: Wrong tag type for icmVideoCardGamma");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
/* Read gamma format (eg. table or formula) from the buffer */
p->tagType = (icmVideoCardGammaTagType)read_UInt32Number(bp+8);
/* Read remaining gamma data based on format */
if (p->tagType == icmVideoCardGammaTableType) {
p->u.table.channels = read_UInt16Number(bp+12);
p->u.table.entryCount = read_UInt16Number(bp+14);
p->u.table.entrySize = read_UInt16Number(bp+16);
if ((len-18) < sat_mul3(p->u.table.channels, p->u.table.entryCount,
p->u.table.entrySize)) {
sprintf(icp->err,"icmVideoCardGamma_read: Tag too small to be legal");
return icp->errc = 1;
}
if ((rv = pp->allocate(pp)) != 0) { /* make space for table */
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
/* ~~~~ This should be a table of doubles like the rest of icclib ! ~~~~ */
pchar = (ORD8 *)p->u.table.data;
pshort = (ORD16 *)p->u.table.data;
for (c=0, bp=bp+18; c<p->u.table.channels*p->u.table.entryCount; c++) {
switch (p->u.table.entrySize) {
case 1:
*pchar++ = read_UInt8Number(bp);
bp++;
break;
case 2:
*pshort++ = read_UInt16Number(bp);
bp+=2;
break;
default:
sprintf(icp->err,"icmVideoCardGamma_read: unsupported table entry size");
pp->del(pp);
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
}
} else if (p->tagType == icmVideoCardGammaFormulaType) {
if (len < 48) {
sprintf(icp->err,"icmVideoCardGamma_read: Tag too small to be legal");
return icp->errc = 1;
}
p->u.table.channels = 3; /* Always 3 for formula */
p->u.formula.redGamma = read_S15Fixed16Number(bp+12);
p->u.formula.redMin = read_S15Fixed16Number(bp+16);
p->u.formula.redMax = read_S15Fixed16Number(bp+20);
p->u.formula.greenGamma = read_S15Fixed16Number(bp+24);
p->u.formula.greenMin = read_S15Fixed16Number(bp+28);
p->u.formula.greenMax = read_S15Fixed16Number(bp+32);
p->u.formula.blueGamma = read_S15Fixed16Number(bp+36);
p->u.formula.blueMin = read_S15Fixed16Number(bp+40);
p->u.formula.blueMax = read_S15Fixed16Number(bp+44);
} else {
sprintf(icp->err,"icmVideoCardGammaTable_read: Unknown gamma format for icmVideoCardGamma");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
icp->al->free(icp->al, buf);
return 0;
}
/* Write the contents of the object. Return 0 on sucess, error code on failure */
static int icmVideoCardGamma_write(
icmBase *pp,
unsigned int of /* File offset to write from */
) {
icmVideoCardGamma *p = (icmVideoCardGamma *)pp;
icc *icp = p->icp;
unsigned int len;
char *bp, *buf; /* Buffer to write from */
int rv = 0, c;
ORD8 *pchar;
ORD16 *pshort;
/* Allocate a file write buffer */
if ((len = p->get_size((icmBase *)p)) == UINT_MAX) {
sprintf(icp->err,"icmViewingConditions_write get_size overflow");
return icp->errc = 1;
}
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmViewingConditions_write malloc() failed");
return icp->errc = 2;
}
bp = buf;
/* Write type descriptor to the buffer */
if ((rv = write_SInt32Number((int)p->ttype,bp)) != 0) {
sprintf(icp->err,"icmVideoCardGamma_write: write_SInt32Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
write_SInt32Number(0,bp+4); /* Set padding to 0 */
/* Write gamma format (eg. table of formula) */
if ((rv = write_UInt32Number(p->tagType,bp+8)) != 0) {
sprintf(icp->err,"icmVideoCardGamma_write: write_UInt32Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
/* Write remaining gamma data based on format */
if (p->tagType == icmVideoCardGammaTableType) {
if ((rv = write_UInt16Number(p->u.table.channels,bp+12)) != 0) {
sprintf(icp->err,"icmVideoCardGamma_write: write_UInt16Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
if ((rv = write_UInt16Number(p->u.table.entryCount,bp+14)) != 0) {
sprintf(icp->err,"icmVideoCardGamma_write: write_UInt16Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
if ((rv = write_UInt16Number(p->u.table.entrySize,bp+16)) != 0) {
sprintf(icp->err,"icmVideoCardGamma_write: write_UInt16Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
pchar = (ORD8 *)p->u.table.data;
pshort = (ORD16 *)p->u.table.data;
for (c=0, bp=bp+18; c<p->u.table.channels*p->u.table.entryCount; c++) {
switch (p->u.table.entrySize) {
case 1:
write_UInt8Number(*pchar++,bp);
bp++;
break;
case 2:
write_UInt16Number(*pshort++,bp);
bp+=2;
break;
default:
sprintf(icp->err,"icmVideoCardGamma_write: unsupported table entry size");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
}
} else if (p->tagType == icmVideoCardGammaFormulaType) {
if ((rv = write_S15Fixed16Number(p->u.formula.redGamma,bp+12)) != 0) {
sprintf(icp->err,"icmVideoCardGamma_write: write_S15Fixed16Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
if ((rv = write_S15Fixed16Number(p->u.formula.redMin,bp+16)) != 0) {
sprintf(icp->err,"icmVideoCardGamma_write: write_S15Fixed16Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
if ((rv = write_S15Fixed16Number(p->u.formula.redMax,bp+20)) != 0) {
sprintf(icp->err,"icmVideoCardGamma_write: write_S15Fixed16Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
if ((rv = write_S15Fixed16Number(p->u.formula.greenGamma,bp+24)) != 0) {
sprintf(icp->err,"icmVideoCardGamma_write: write_S15Fixed16Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
if ((rv = write_S15Fixed16Number(p->u.formula.greenMin,bp+28)) != 0) {
sprintf(icp->err,"icmVideoCardGamma_write: write_S15Fixed16Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
if ((rv = write_S15Fixed16Number(p->u.formula.greenMax,bp+32)) != 0) {
sprintf(icp->err,"icmVideoCardGamma_write: write_S15Fixed16Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
if ((rv = write_S15Fixed16Number(p->u.formula.blueGamma,bp+36)) != 0) {
sprintf(icp->err,"icmVideoCardGamma_write: write_S15Fixed16Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
if ((rv = write_S15Fixed16Number(p->u.formula.blueMin,bp+40)) != 0) {
sprintf(icp->err,"icmVideoCardGamma_write: write_S15Fixed16Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
if ((rv = write_S15Fixed16Number(p->u.formula.blueMax,bp+44)) != 0) {
sprintf(icp->err,"icmVideoCardGamma_write: write_S15Fixed16Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
} else {
sprintf(icp->err,"icmVideoCardGammaTable_write: Unknown gamma format for icmVideoCardGamma");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
/* Write to the file */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->write(icp->fp, buf, 1, len) != len) {
sprintf(icp->err,"icmViewingConditions_write fseek() or fwrite() failed");
icp->al->free(icp->al, buf);
return icp->errc = 2;
}
icp->al->free(icp->al, buf);
return 0;
}
/* Dump a text description of the object */
static void icmVideoCardGamma_dump(
icmBase *pp,
icmFile *op, /* Output to dump to */
int verb /* Verbosity level */
) {
icmVideoCardGamma *p = (icmVideoCardGamma *)pp;
int c,i;
if (verb <= 0)
return;
if (p->tagType == icmVideoCardGammaTableType) {
op->gprintf(op,"VideoCardGammaTable:\n");
op->gprintf(op," channels = %d\n", p->u.table.channels);
op->gprintf(op," entries = %d\n", p->u.table.entryCount);
op->gprintf(op," entrysize = %d\n", p->u.table.entrySize);
if (verb >= 2) {
/* dump array contents also */
for (c=0; c<p->u.table.channels; c++) {
op->gprintf(op," channel #%d\n",c);
for (i=0; i<p->u.table.entryCount; i++) {
if (p->u.table.entrySize == 1) {
op->gprintf(op," %d: %d\n",i,((ORD8 *)p->u.table.data)[c*p->u.table.entryCount+i]);
}
else if (p->u.table.entrySize == 2) {
op->gprintf(op," %d: %d\n",i,((ORD16 *)p->u.table.data)[c*p->u.table.entryCount+i]);
}
}
}
}
} else if (p->tagType == icmVideoCardGammaFormulaType) {
op->gprintf(op,"VideoCardGammaFormula:\n");
op->gprintf(op," red gamma = %.8f\n", p->u.formula.redGamma);
op->gprintf(op," red min = %.8f\n", p->u.formula.redMin);
op->gprintf(op," red max = %.8f\n", p->u.formula.redMax);
op->gprintf(op," green gamma = %.8f\n", p->u.formula.greenGamma);
op->gprintf(op," green min = %.8f\n", p->u.formula.greenMin);
op->gprintf(op," green max = %.8f\n", p->u.formula.greenMax);
op->gprintf(op," blue gamma = %.8f\n", p->u.formula.blueGamma);
op->gprintf(op," blue min = %.8f\n", p->u.formula.blueMin);
op->gprintf(op," blue max = %.8f\n", p->u.formula.blueMax);
} else {
op->gprintf(op," Unknown tag format\n");
}
}
/* Allocate variable sized data elements */
static int icmVideoCardGamma_allocate(
icmBase *pp
) {
icmVideoCardGamma *p = (icmVideoCardGamma *)pp;
icc *icp = p->icp;
unsigned int size;
/* note: allocation is only relevant for table type
* and in that case the channels, entryCount, and entrySize
* fields must all be set prior to getting here
*/
if (p->tagType == icmVideoCardGammaTableType) {
size = sat_mul(p->u.table.channels, p->u.table.entryCount);
switch (p->u.table.entrySize) {
case 1:
size = sat_mul(size, sizeof(ORD8));
break;
case 2:
size = sat_mul(size, sizeof(unsigned short));
break;
default:
sprintf(icp->err,"icmVideoCardGamma_alloc: unsupported table entry size");
return icp->errc = 1;
}
if (size == UINT_MAX) {
sprintf(icp->err,"icmVideoCardGamma_alloc: size overflow");
return icp->errc = 1;
}
if (p->u.table.data != NULL)
icp->al->free(icp->al, p->u.table.data);
if ((p->u.table.data = (void*) icp->al->malloc(icp->al, size)) == NULL) {
sprintf(icp->err,"icmVideoCardGamma_alloc: malloc() of table data failed");
return icp->errc = 2;
}
}
return 0;
}
/* Read a value */
static double icmVideoCardGamma_lookup(
icmVideoCardGamma *p,
int chan, /* Channel, 0, 1 or 2 */
double iv /* Input value 0.0 - 1.0 */
) {
double ov = 0.0;
if (chan < 0 || chan > (p->u.table.channels-1)
|| iv < 0.0 || iv > 1.0)
return iv;
if (p->tagType == icmVideoCardGammaTableType && p->u.table.entryCount == 0) {
/* Deal with siliness */
ov = iv;
} else if (p->tagType == icmVideoCardGammaTableType) {
/* Use linear interpolation */
unsigned int ix;
double val0, val1, w;
double inputEnt_1 = (double)(p->u.table.entryCount-1);
val0 = iv * inputEnt_1;
if (val0 < 0.0)
val0 = 0.0;
else if (val0 > inputEnt_1)
val0 = inputEnt_1;
ix = (unsigned int)floor(val0); /* Coordinate */
if (ix > (p->u.table.entryCount-2))
ix = (p->u.table.entryCount-2);
w = val0 - (double)ix; /* weight */
if (p->u.table.entrySize == 1) {
val0 = ((ORD8 *)p->u.table.data)[chan * p->u.table.entryCount + ix]/255.0;
val1 = ((ORD8 *)p->u.table.data)[chan * p->u.table.entryCount + ix + 1]/255.0;
} else if (p->u.table.entrySize == 2) {
val0 = ((ORD16 *)p->u.table.data)[chan * p->u.table.entryCount + ix]/65535.0;
val1 = ((ORD16 *)p->u.table.data)[chan * p->u.table.entryCount + ix + 1]/65535.0;
} else {
val0 = val1 = iv;
}
ov = val0 + w * (val1 - val0);
} else if (p->tagType == icmVideoCardGammaFormulaType) {
double min, max, gam;
if (iv == 0) {
min = p->u.formula.redMin;
max = p->u.formula.redMax;
gam = p->u.formula.redGamma;
} else if (iv == 1) {
min = p->u.formula.greenMin;
max = p->u.formula.greenMax;
gam = p->u.formula.greenGamma;
} else {
min = p->u.formula.blueMin;
max = p->u.formula.blueMax;
gam = p->u.formula.blueGamma;
}
/* The Apple OSX doco confirms this is the formula */
ov = min + (max - min) * pow(iv, gam);
}
return ov;
}
/* Free all storage in the object */
static void icmVideoCardGamma_delete(
icmBase *pp
) {
icmVideoCardGamma *p = (icmVideoCardGamma *)pp;
icc *icp = p->icp;
if (p->tagType == icmVideoCardGammaTableType && p->u.table.data != NULL)
icp->al->free(icp->al, p->u.table.data);
icp->al->free(icp->al, p);
}
/* Create an empty object. Return null on error */
static icmBase *new_icmVideoCardGamma(
icc *icp
) {
icmVideoCardGamma *p;
if ((p = (icmVideoCardGamma *) icp->al->calloc(icp->al,1,sizeof(icmVideoCardGamma))) == NULL)
return NULL;
p->ttype = icSigVideoCardGammaType;
p->refcount = 1;
p->get_size = icmVideoCardGamma_get_size;
p->read = icmVideoCardGamma_read;
p->write = icmVideoCardGamma_write;
p->lookup = icmVideoCardGamma_lookup;
p->dump = icmVideoCardGamma_dump;
p->allocate = icmVideoCardGamma_allocate;
p->del = icmVideoCardGamma_delete;
p->icp = icp;
return (icmBase *)p;
}
/* ---------------------------------------------------------- */
/* ViewingConditions */
/* Return the number of bytes needed to write this tag */
static unsigned int icmViewingConditions_get_size(
icmBase *pp
) {
unsigned int len = 0;
len = sat_add(len, 8); /* 8 bytes for tag and padding */
len = sat_add(len, 12); /* 12 for XYZ of illuminant */
len = sat_add(len, 12); /* 12 for XYZ of surround */
len = sat_add(len, 4); /* 4 for illuminant type */
return len;
}
/* read the object, return 0 on success, error code on fail */
static int icmViewingConditions_read(
icmBase *pp,
unsigned int len, /* tag length */
unsigned int of /* start offset within file */
) {
icmViewingConditions *p = (icmViewingConditions *)pp;
icc *icp = p->icp;
int rv;
char *bp, *buf;
if (len < 36) {
sprintf(icp->err,"icmViewingConditions_read: Tag too small to be legal");
return icp->errc = 1;
}
/* Allocate a file read buffer */
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmViewingConditions_read: malloc() failed");
return icp->errc = 2;
}
bp = buf;
/* Read portion of file into buffer */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->read(icp->fp, bp, 1, len) != len) {
sprintf(icp->err,"icmViewingConditions_read: fseek() or fread() failed");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
/* Read type descriptor from the buffer */
if (((icTagTypeSignature)read_SInt32Number(bp)) != p->ttype) {
sprintf(icp->err,"icmViewingConditions_read: Wrong tag type for icmViewingConditions");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
/* Read the XYZ values for the illuminant */
if ((rv = read_XYZNumber(&p->illuminant, bp+8)) != 0) {
sprintf(icp->err,"icmViewingConditions: read_XYZNumber error");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
/* Read the XYZ values for the surround */
if ((rv = read_XYZNumber(&p->surround, bp+20)) != 0) {
sprintf(icp->err,"icmViewingConditions: read_XYZNumber error");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
/* Read the encoded standard illuminant */
p->stdIlluminant = (icIlluminant)read_SInt32Number(bp + 32);
icp->al->free(icp->al, buf);
return 0;
}
/* Write the contents of the object. Return 0 on sucess, error code on failure */
static int icmViewingConditions_write(
icmBase *pp,
unsigned int of /* File offset to write from */
) {
icmViewingConditions *p = (icmViewingConditions *)pp;
icc *icp = p->icp;
unsigned int len;
char *bp, *buf; /* Buffer to write from */
int rv = 0;
/* Allocate a file write buffer */
if ((len = p->get_size((icmBase *)p)) == UINT_MAX) {
sprintf(icp->err,"icmViewingConditions_write get_size overflow");
return icp->errc = 1;
}
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmViewingConditions_write malloc() failed");
return icp->errc = 2;
}
bp = buf;
/* Write type descriptor to the buffer */
if ((rv = write_SInt32Number((int)p->ttype,bp)) != 0) {
sprintf(icp->err,"icmViewingConditions_write: write_SInt32Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
write_SInt32Number(0,bp+4); /* Set padding to 0 */
/* Write the XYZ values for the illuminant */
if ((rv = write_XYZNumber(&p->illuminant, bp+8)) != 0) {
sprintf(icp->err,"icmViewingConditions: write_XYZNumber error");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
/* Write the XYZ values for the surround */
if ((rv = write_XYZNumber(&p->surround, bp+20)) != 0) {
sprintf(icp->err,"icmViewingConditions: write_XYZNumber error");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
/* Write the encoded standard illuminant */
if ((rv = write_SInt32Number((int)p->stdIlluminant, bp + 32)) != 0) {
sprintf(icp->err,"icmViewingConditionsa_write: write_SInt32Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
/* Write to the file */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->write(icp->fp, buf, 1, len) != len) {
sprintf(icp->err,"icmViewingConditions_write fseek() or fwrite() failed");
icp->al->free(icp->al, buf);
return icp->errc = 2;
}
icp->al->free(icp->al, buf);
return 0;
}
/* Dump a text description of the object */
static void icmViewingConditions_dump(
icmBase *pp,
icmFile *op, /* Output to dump to */
int verb /* Verbosity level */
) {
icmViewingConditions *p = (icmViewingConditions *)pp;
if (verb <= 0)
return;
op->gprintf(op,"Viewing Conditions:\n");
op->gprintf(op," XYZ value of illuminant in cd/m^2 = %s\n", string_XYZNumber(&p->illuminant));
op->gprintf(op," XYZ value of surround in cd/m^2 = %s\n", string_XYZNumber(&p->surround));
op->gprintf(op," Illuminant type = %s\n", string_Illuminant(p->stdIlluminant));
}
/* Allocate variable sized data elements */
static int icmViewingConditions_allocate(
icmBase *pp
) {
/* Nothing to do */
return 0;
}
/* Free all storage in the object */
static void icmViewingConditions_delete(
icmBase *pp
) {
icmViewingConditions *p = (icmViewingConditions *)pp;
icc *icp = p->icp;
icp->al->free(icp->al, p);
}
/* Create an empty object. Return null on error */
static icmBase *new_icmViewingConditions(
icc *icp
) {
icmViewingConditions *p;
if ((p = (icmViewingConditions *) icp->al->calloc(icp->al,1,sizeof(icmViewingConditions))) == NULL)
return NULL;
p->ttype = icSigViewingConditionsType;
p->refcount = 1;
p->get_size = icmViewingConditions_get_size;
p->read = icmViewingConditions_read;
p->write = icmViewingConditions_write;
p->dump = icmViewingConditions_dump;
p->allocate = icmViewingConditions_allocate;
p->del = icmViewingConditions_delete;
p->icp = icp;
return (icmBase *)p;
}
/* ---------------------------------------------------------- */
/* icmCrdInfo object */
/* Return the number of bytes needed to write this tag */
static unsigned int icmCrdInfo_get_size(
icmBase *pp
) {
icmCrdInfo *p = (icmCrdInfo *)pp;
unsigned int len = 0, t;
len = sat_add(len, 8); /* 8 bytes for tag and padding */
len = sat_addadd(len, 4, p->ppsize); /* Postscript product name */
for (t = 0; t < 4; t++) { /* For all 4 intents */
len = sat_addadd(len, 4, p->crdsize[t]); /* crd names */
}
return len;
}
/* read the object, return 0 on success, error code on fail */
static int icmCrdInfo_read(
icmBase *pp,
unsigned int len, /* tag length */
unsigned int of /* start offset within file */
) {
icmCrdInfo *p = (icmCrdInfo *)pp;
icc *icp = p->icp;
unsigned int t;
int rv;
char *bp, *buf, *end;
if (len < 28) {
sprintf(icp->err,"icmCrdInfo_read: Tag too small to be legal");
return icp->errc = 1;
}
/* Allocate a file read buffer */
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmCrdInfo_read: malloc() failed");
return icp->errc = 2;
}
bp = buf;
end = buf + len;
/* Read portion of file into buffer */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->read(icp->fp, bp, 1, len) != len) {
sprintf(icp->err,"icmCrdInfo_read: fseek() or fread() failed");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
/* Read type descriptor from the buffer */
if (((icTagTypeSignature)read_SInt32Number(bp)) != p->ttype) {
sprintf(icp->err,"icmCrdInfo_read: Wrong tag type for icmCrdInfo");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
bp = bp + 8;
/* Postscript product name */
if (bp > end || 4 > (end - bp)) {
sprintf(icp->err,"icmCrdInfo_read: Data too short to read Postscript product name");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
p->ppsize = read_UInt32Number(bp);
bp += 4;
if (p->ppsize > 0) {
if (p->ppsize > (end - bp)) {
sprintf(icp->err,"icmCrdInfo_read: Data to short to read Postscript product string");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
if ((rv = check_null_string(bp,p->ppsize)) == 1) {
sprintf(icp->err,"icmCrdInfo_read: Postscript product name is not terminated");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
/* Haven't checked if rv == 2 is legal or not */
if ((rv = p->allocate((icmBase *)p)) != 0) {
icp->al->free(icp->al, buf);
return rv;
}
memmove((void *)p->ppname, (void *)bp, p->ppsize);
bp += p->ppsize;
}
/* CRD names for the four rendering intents */
for (t = 0; t < 4; t++) { /* For all 4 intents */
if (bp > end || 4 > (end - bp)) {
sprintf(icp->err,"icmCrdInfo_read: Data too short to read CRD%d name",t);
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
p->crdsize[t] = read_UInt32Number(bp);
bp += 4;
if (p->crdsize[t] > 0) {
if (p->crdsize[t] > (end - bp)) {
sprintf(icp->err,"icmCrdInfo_read: Data to short to read CRD%d string",t);
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
if ((rv = check_null_string(bp,p->crdsize[t])) == 1) {
sprintf(icp->err,"icmCrdInfo_read: CRD%d name is not terminated",t);
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
/* Haven't checked if rv == 2 is legal or not */
if ((rv = p->allocate((icmBase *)p)) != 0) {
icp->al->free(icp->al, buf);
return rv;
}
memmove((void *)p->crdname[t], (void *)bp, p->crdsize[t]);
bp += p->crdsize[t];
}
}
icp->al->free(icp->al, buf);
return 0;
}
/* Write the contents of the object. Return 0 on sucess, error code on failure */
static int icmCrdInfo_write(
icmBase *pp,
unsigned int of /* File offset to write from */
) {
icmCrdInfo *p = (icmCrdInfo *)pp;
icc *icp = p->icp;
unsigned int t;
unsigned int len;
char *bp, *buf; /* Buffer to write from */
int rv;
/* Allocate a file write buffer */
if ((len = p->get_size((icmBase *)p)) == UINT_MAX) {
sprintf(icp->err,"icmCrdInfo_write get_size overflow");
return icp->errc = 1;
}
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmCrdInfo_write malloc() failed");
return icp->errc = 2;
}
bp = buf;
/* Write type descriptor to the buffer */
if ((rv = write_SInt32Number((int)p->ttype,bp)) != 0) {
sprintf(icp->err,"icmCrdInfo_write: write_SInt32Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
write_SInt32Number(0,bp+4); /* Set padding to 0 */
bp = bp + 8;
/* Postscript product name */
if ((rv = write_UInt32Number(p->ppsize,bp)) != 0) {
sprintf(icp->err,"icmCrdInfo_write: write_UInt32Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
bp += 4;
if (p->ppsize > 0) {
if ((rv = check_null_string(p->ppname,p->ppsize)) == 1) {
sprintf(icp->err,"icmCrdInfo_write: Postscript product name is not terminated");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
/* Haven't checked if rv == 2 is legal or not */
memmove((void *)bp, (void *)p->ppname, p->ppsize);
bp += p->ppsize;
}
/* CRD names for the four rendering intents */
for (t = 0; t < 4; t++) { /* For all 4 intents */
if ((rv = write_UInt32Number(p->crdsize[t],bp)) != 0) {
sprintf(icp->err,"icmCrdInfo_write: write_UInt32Number() failed");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
bp += 4;
if (p->ppsize > 0) {
if ((rv = check_null_string(p->crdname[t],p->crdsize[t])) == 1) {
sprintf(icp->err,"icmCrdInfo_write: CRD%d name is not terminated",t);
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
/* Haven't checked if rv == 2 is legal or not */
memmove((void *)bp, (void *)p->crdname[t], p->crdsize[t]);
bp += p->crdsize[t];
}
}
/* Write to the file */
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->write(icp->fp, buf, 1, len) != len) {
sprintf(icp->err,"icmCrdInfo_write fseek() or fwrite() failed");
icp->al->free(icp->al, buf);
return icp->errc = 2;
}
icp->al->free(icp->al, buf);
return 0;
}
/* Dump a text description of the object */
static void icmCrdInfo_dump(
icmBase *pp,
icmFile *op, /* Output to dump to */
int verb /* Verbosity level */
) {
icmCrdInfo *p = (icmCrdInfo *)pp;
unsigned int i, r, c, size, t;
if (verb <= 0)
return;
op->gprintf(op,"PostScript Product name and CRD names:\n");
op->gprintf(op," Product name:\n");
op->gprintf(op," No. chars = %lu\n",p->ppsize);
size = p->ppsize > 0 ? p->ppsize-1 : 0;
i = 0;
for (r = 1;; r++) { /* count rows */
if (i >= size) {
op->gprintf(op,"\n");
break;
}
if (r > 1 && verb < 2) {
op->gprintf(op,"...\n");
break; /* Print 1 row if not verbose */
}
c = 1;
op->gprintf(op," 0x%04lx: ",i);
c += 10;
while (i < size && c < 73) {
if (isprint(p->ppname[i])) {
op->gprintf(op,"%c",p->ppname[i]);
c++;
} else {
op->gprintf(op,"\\%03o",p->ppname[i]);
c += 4;
}
i++;
}
if (i < size)
op->gprintf(op,"\n");
}
for (t = 0; t < 4; t++) { /* For all 4 intents */
op->gprintf(op," CRD%ld name:\n",t);
op->gprintf(op," No. chars = %lu\n",p->crdsize[t]);
size = p->crdsize[t] > 0 ? p->crdsize[t]-1 : 0;
i = 0;
for (r = 1;; r++) { /* count rows */
if (i >= size) {
op->gprintf(op,"\n");
break;
}
if (r > 1 && verb < 2) {
op->gprintf(op,"...\n");
break; /* Print 1 row if not verbose */
}
c = 1;
op->gprintf(op," 0x%04lx: ",i);
c += 10;
while (i < size && c < 73) {
if (isprint(p->crdname[t][i])) {
op->gprintf(op,"%c",p->crdname[t][i]);
c++;
} else {
op->gprintf(op,"\\%03o",p->crdname[t][i]);
c += 4;
}
i++;
}
if (i < size)
op->gprintf(op,"\n");
}
}
}
/* Allocate variable sized data elements */
static int icmCrdInfo_allocate(
icmBase *pp
) {
icmCrdInfo *p = (icmCrdInfo *)pp;
icc *icp = p->icp;
unsigned int t;
if (p->ppsize != p->_ppsize) {
if (ovr_mul(p->ppsize, sizeof(char))) {
sprintf(icp->err,"icmCrdInfo_alloc: size overflow");
return icp->errc = 1;
}
if (p->ppname != NULL)
icp->al->free(icp->al, p->ppname);
if ((p->ppname = (char *) icp->al->calloc(icp->al, p->ppsize, sizeof(char))) == NULL) {
sprintf(icp->err,"icmCrdInfo_alloc: malloc() of string data failed");
return icp->errc = 2;
}
p->_ppsize = p->ppsize;
}
for (t = 0; t < 4; t++) { /* For all 4 intents */
if (p->crdsize[t] != p->_crdsize[t]) {
if (ovr_mul(p->crdsize[t], sizeof(char))) {
sprintf(icp->err,"icmCrdInfo_alloc: size overflow");
return icp->errc = 1;
}
if (p->crdname[t] != NULL)
icp->al->free(icp->al, p->crdname[t]);
if ((p->crdname[t] = (char *) icp->al->calloc(icp->al, p->crdsize[t], sizeof(char))) == NULL) {
sprintf(icp->err,"icmCrdInfo_alloc: malloc() of CRD%d name string failed",t);
return icp->errc = 2;
}
p->_crdsize[t] = p->crdsize[t];
}
}
return 0;
}
/* Free all storage in the object */
static void icmCrdInfo_delete(
icmBase *pp
) {
icmCrdInfo *p = (icmCrdInfo *)pp;
icc *icp = p->icp;
unsigned int t;
if (p->ppname != NULL)
icp->al->free(icp->al, p->ppname);
for (t = 0; t < 4; t++) { /* For all 4 intents */
if (p->crdname[t] != NULL)
icp->al->free(icp->al, p->crdname[t]);
}
icp->al->free(icp->al, p);
}
/* Create an empty object. Return null on error */
static icmBase *new_icmCrdInfo(
icc *icp
) {
icmCrdInfo *p;
if ((p = (icmCrdInfo *) icp->al->calloc(icp->al,1,sizeof(icmCrdInfo))) == NULL)
return NULL;
p->ttype = icSigCrdInfoType;
p->refcount = 1;
p->get_size = icmCrdInfo_get_size;
p->read = icmCrdInfo_read;
p->write = icmCrdInfo_write;
p->dump = icmCrdInfo_dump;
p->allocate = icmCrdInfo_allocate;
p->del = icmCrdInfo_delete;
p->icp = icp;
return (icmBase *)p;
}
/* ========================================================== */
/* icmHeader object */
/* ========================================================== */
/* Return the number of bytes needed to write this tag */
static unsigned int icmHeader_get_size(
icmHeader *p
) {
return 128; /* By definition */
}
/* read the object, return 0 on success, error code on fail */
static int icmHeader_read(
icmHeader *p,
unsigned int len, /* tag length */
unsigned int of /* start offset within file */
) {
icc *icp = p->icp;
char *buf;
unsigned int tt;
int rv = 0;
if (len != 128) {
sprintf(icp->err,"icmHeader_read: Length expected to be 128");
return icp->errc = 1;
}
if ((buf = (char *) icp->al->malloc(icp->al, len)) == NULL) {
sprintf(icp->err,"icmHeader_read: malloc() failed");
return icp->errc = 2;
}
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->read(icp->fp, buf, 1, len) != len) {
sprintf(icp->err,"icmHeader_read: fseek() or fread() failed");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
/* Check that the magic number is right */
tt = read_SInt32Number(buf+36);
if (tt != icMagicNumber) { /* Check magic number */
sprintf(icp->err,"icmHeader_read: wrong magic number 0x%x",tt);
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
/* Fill in the in-memory structure */
p->size = read_UInt32Number(buf + 0); /* Profile size in bytes */
if (p->size < (128 + 4)) {
sprintf(icp->err,"icmHeader_read: file size %d too small to be legal",p->size);
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
p->cmmId = read_SInt32Number(buf + 4); /* CMM for profile */
tt = read_UInt8Number(buf + 8); /* Raw major version number */
p->majv = (tt >> 4) * 10 + (tt & 0xf); /* Integer major version number */
tt = read_UInt8Number(buf + 9); /* Raw minor & bug fix version numbers */
p->minv = (tt >> 4); /* Integer minor version number */
p->bfv = (tt & 0xf); /* Integer bug fix version number */
if (p->majv < 3) { /* Set version class */
if (p->minv >= 4)
icp->ver = icmVersion2_4;
else if (p->minv >= 3)
icp->ver = icmVersion2_3;
else
icp->ver = icmVersionDefault;
} else
icp->ver = icmVersion4_1;
p->deviceClass = (icProfileClassSignature)
read_SInt32Number(buf + 12); /* Type of profile */
p->colorSpace = (icColorSpaceSignature)
read_SInt32Number(buf + 16); /* Color space of data */
p->pcs = (icColorSpaceSignature)
read_SInt32Number(buf + 20); /* PCS: XYZ or Lab */
if ((rv = read_DateTimeNumber(&p->date, buf + 24)) != 0) { /* Creation Date */
sprintf(icp->err,"icmHeader_read: read_DateTimeNumber corrupted");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
p->platform = (icPlatformSignature)
read_SInt32Number(buf + 40); /* Primary platform */
p->flags = read_UInt32Number(buf + 44); /* Various bits */
p->manufacturer = read_SInt32Number(buf + 48); /* Dev manufacturer */
p->model = read_SInt32Number(buf + 52); /* Dev model */
read_UInt64Number(&p->attributes, buf + 56); /* Device attributes */
p->renderingIntent = (icRenderingIntent)
read_SInt32Number(buf + 64); /* Rendering intent */
if ((rv = read_XYZNumber(&p->illuminant, buf + 68)) != 0) { /* Profile illuminant */
sprintf(icp->err,"icmHeader_read: read_XYZNumber error");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
p->creator = read_SInt32Number(buf + 80); /* Profile creator */
for (tt = 0; tt < 16; tt++) /* Profile ID */
p->id[tt] = icp->ver >= icmVersion4_1 ? read_UInt8Number(buf + 84 + tt) : 0;
icp->al->free(icp->al, buf);
#ifndef ENABLE_V4
if (icp->ver >= icmVersion4_1) {
sprintf(icp->err,"icmHeader_read: ICC V4 not supported!");
return icp->errc = 1;
}
#endif
return 0;
}
/* Write the contents of the object. Return 0 on sucess, error code on failure */
static int icmHeader_write(
icmHeader *p,
unsigned int of, /* File offset to write from */
int doid /* Flag, nz = writing to compute ID */
) {
icc *icp = p->icp;
char *buf; /* Buffer to write from */
unsigned int len;
unsigned int tt;
int rv = 0;
/* Allocate a file write buffer */
if ((len = p->get_size(p)) == UINT_MAX) {
sprintf(icp->err,"icmHeader_write get_size overflow");
return icp->errc = 1;
}
if ((buf = (char *) icp->al->calloc(icp->al,1,len)) == NULL) { /* Zero it - some CMS are fussy */
sprintf(icp->err,"icmHeader_write calloc() failed");
return icp->errc = 2;
}
/* Fill in the write buffer */
if ((rv = write_UInt32Number(p->size, buf + 0)) != 0) { /* Profile size in bytes */
sprintf(icp->err,"icmHeader_write: profile size");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
if ((rv = write_SInt32Number(p->cmmId, buf + 4)) != 0) { /* CMM for profile */
sprintf(icp->err,"icmHeader_write: cmmId");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
if (p->majv < 0 || p->majv > 99 /* Sanity check version numbers */
|| p->minv < 0 || p->minv > 9
|| p->bfv < 0 || p->bfv > 9) {
sprintf(icp->err,"icmHeader_write: version number");
icp->al->free(icp->al, buf);
return icp->errc = 1;
}
// ~~~ Hmm. We're not checking ->ver is >= corresponding header version number ~~
tt = ((p->majv/10) << 4) + (p->majv % 10);
if ((rv = write_UInt8Number(tt, buf + 8)) != 0) { /* Raw major version number */
sprintf(icp->err,"icmHeader_write: Uint8Number major version");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
tt = (p->minv << 4) + p->bfv;
if ((rv = write_UInt8Number(tt, buf + 9)) != 0) { /* Raw minor/bug fix version numbers */
sprintf(icp->err,"icmHeader_write: Uint8Number minor/bug fix");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
if ((rv = write_SInt32Number((int)p->deviceClass, buf + 12)) != 0) { /* Type of profile */
sprintf(icp->err,"icmHeader_write: SInt32Number deviceClass");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
if ((rv = write_SInt32Number((int)p->colorSpace, buf + 16)) != 0) { /* Color space of data */
sprintf(icp->err,"icmHeader_write: SInt32Number data color space");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
if ((rv = write_SInt32Number((int)p->pcs, buf + 20)) != 0) { /* PCS: XYZ or Lab */
sprintf(icp->err,"icmHeader_write: SInt32Number PCS");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
if ((rv = write_DateTimeNumber(&p->date, buf + 24)) != 0) { /* Creation Date */
sprintf(icp->err,"icmHeader_write: DateTimeNumber creation");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
if ((rv = write_SInt32Number(icMagicNumber, buf+36)) != 0) { /* Magic number */
sprintf(icp->err,"icmHeader_write: SInt32Number magic");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
if ((rv = write_SInt32Number((int)p->platform, buf + 40)) != 0) { /* Primary platform */
sprintf(icp->err,"icmHeader_write: SInt32Number platform");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
if ((rv = write_UInt32Number(doid ? 0 : p->flags, buf + 44)) != 0) { /* Various flag bits */
sprintf(icp->err,"icmHeader_write: UInt32Number flags");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
if ((rv = write_SInt32Number(p->manufacturer, buf + 48)) != 0) { /* Dev manufacturer */
sprintf(icp->err,"icmHeader_write: SInt32Number manufaturer");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
if ((write_SInt32Number(p->model, buf + 52)) != 0) { /* Dev model */
sprintf(icp->err,"icmHeader_write: SInt32Number model");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
if ((rv = write_UInt64Number(&p->attributes, buf + 56)) != 0) { /* Device attributes */
sprintf(icp->err,"icmHeader_write: UInt64Number attributes");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
if ((rv = write_SInt32Number(doid ? 0 : (int)p->renderingIntent, buf + 64)) != 0) { /* Rendering intent */
sprintf(icp->err,"icmHeader_write: SInt32Number rendering intent");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
if ((rv = write_XYZNumber(&p->illuminant, buf + 68)) != 0) { /* Profile illuminant */
sprintf(icp->err,"icmHeader_write: XYZNumber illuminant");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
if ((rv = write_SInt32Number(p->creator, buf + 80)) != 0) { /* Profile creator */
sprintf(icp->err,"icmHeader_write: SInt32Number creator");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
if (doid == 0 && icp->ver >= icmVersion4_1) { /* ID is V4.0+ feature */
for (tt = 0; tt < 16; tt++) {
if ((rv = write_UInt8Number(p->id[tt], buf + 84 + tt)) != 0) { /* Profile ID */
sprintf(icp->err,"icmHeader_write: UInt8Number creator");
icp->al->free(icp->al, buf);
return icp->errc = rv;
}
}
}
if ( icp->fp->seek(icp->fp, of) != 0
|| icp->fp->write(icp->fp, buf, 1, len) != len) {
sprintf(icp->err,"icmHeader_write fseek() or fwrite() failed");
icp->al->free(icp->al, buf);
return icp->errc = 2;
}
icp->al->free(icp->al, buf);
return rv;
}
static void icmHeader_dump(
icmHeader *p,
icmFile *op, /* Output to dump to */
int verb /* Verbosity level */
) {
int i;
if (verb <= 0)
return;
op->gprintf(op,"Header:\n");
op->gprintf(op," size = %d bytes\n",p->size);
op->gprintf(op," CMM = %s\n",tag2str(p->cmmId));
op->gprintf(op," Version = %d.%d.%d\n",p->majv, p->minv, p->bfv);
op->gprintf(op," Device Class = %s\n", string_ProfileClassSignature(p->deviceClass));
op->gprintf(op," Color Space = %s\n", string_ColorSpaceSignature(p->colorSpace));
op->gprintf(op," Conn. Space = %s\n", string_ColorSpaceSignature(p->pcs));
op->gprintf(op," Date, Time = %s\n", string_DateTimeNumber(&p->date));
op->gprintf(op," Platform = %s\n", string_PlatformSignature(p->platform));
op->gprintf(op," Flags = %s\n", string_ProfileHeaderFlags(p->flags));
op->gprintf(op," Dev. Mnfctr. = %s\n", tag2str(p->manufacturer)); /* ~~~ */
op->gprintf(op," Dev. Model = %s\n", tag2str(p->model)); /* ~~~ */
op->gprintf(op," Dev. Attrbts = %s\n", string_DeviceAttributes(p->attributes.l));
op->gprintf(op," Rndrng Intnt = %s\n", string_RenderingIntent(p->renderingIntent));
op->gprintf(op," Illuminant = %s\n", string_XYZNumber_and_Lab(&p->illuminant));
op->gprintf(op," Creator = %s\n", tag2str(p->creator)); /* ~~~ */
if (p->icp->ver >= icmVersion4_1) { /* V4.0+ feature */
for (i = 0; i < 16; i++) { /* Check if ID has been set */
if (p->id[i] != 0)
break;
}
if (i < 16)
op->gprintf(op," ID = %02X%02X%02X%02X%02X%02X%02X%02X"
"%02X%02X%02X%02X%02X%02X%02X%02X\n",
p->id[0], p->id[1], p->id[2], p->id[3], p->id[4], p->id[5], p->id[6], p->id[7],
p->id[8], p->id[9], p->id[10], p->id[11], p->id[12], p->id[13], p->id[14], p->id[15]);
else
op->gprintf(op," ID = <Not set>\n");
}
op->gprintf(op,"\n");
}
static void icmHeader_delete(
icmHeader *p
) {
icc *icp = p->icp;
icp->al->free(icp->al, p);
}
/* Create an empty object. Return null on error */
static icmHeader *new_icmHeader(
icc *icp
) {
icmHeader *p;
if ((p = (icmHeader *) icp->al->calloc(icp->al,1,sizeof(icmHeader))) == NULL)
return NULL;
p->icp = icp;
p->get_size = icmHeader_get_size;
p->read = icmHeader_read;
p->write = icmHeader_write;
p->dump = icmHeader_dump;
p->del = icmHeader_delete;
return p;
}
/* ---------------------------------------------------------- */
/* Type vector table. Match the Tag type against the object creator */
static struct {
icTagTypeSignature ttype; /* The tag type signature */
icmBase * (*new_obj)(icc *icp);
} typetable[] = {
{icSigColorantTableType, new_icmColorantTable},
{icmSigAltColorantTableType, new_icmColorantTable},
{icSigCrdInfoType, new_icmCrdInfo},
{icSigCurveType, new_icmCurve},
{icSigDataType, new_icmData},
{icSigDateTimeType, new_icmDateTimeNumber},
{icSigLut16Type, new_icmLut},
{icSigLut8Type, new_icmLut},
{icSigMeasurementType, new_icmMeasurement},
{icSigNamedColorType, new_icmNamedColor},
{icSigNamedColor2Type, new_icmNamedColor},
{icSigProfileSequenceDescType, new_icmProfileSequenceDesc},
{icSigS15Fixed16ArrayType, new_icmS15Fixed16Array},
{icSigScreeningType, new_icmScreening},
{icSigSignatureType, new_icmSignature},
{icSigTextDescriptionType, new_icmTextDescription},
{icSigTextType, new_icmText},
{icSigU16Fixed16ArrayType, new_icmU16Fixed16Array},
{icSigUcrBgType, new_icmUcrBg},
{icSigVideoCardGammaType, new_icmVideoCardGamma},
{icSigUInt16ArrayType, new_icmUInt16Array},
{icSigUInt32ArrayType, new_icmUInt32Array},
{icSigUInt64ArrayType, new_icmUInt64Array},
{icSigUInt8ArrayType, new_icmUInt8Array},
{icSigViewingConditionsType, new_icmViewingConditions},
{icSigXYZArrayType, new_icmXYZArray},
{icMaxEnumType, NULL}
};
/* Table that lists the legal Types for each Tag Signature */
static struct {
icTagSignature sig;
icTagTypeSignature ttypes[4]; /* Arbitrary max of 4 */
} sigtypetable[] = {
{icSigAToB0Tag, {icSigLut8Type,icSigLut16Type,icMaxEnumType}},
{icSigAToB1Tag, {icSigLut8Type,icSigLut16Type,icMaxEnumType}},
{icSigAToB2Tag, {icSigLut8Type,icSigLut16Type,icMaxEnumType}},
{icSigBlueColorantTag, {icSigXYZType,icMaxEnumType}},
{icSigBlueTRCTag, {icSigCurveType,icMaxEnumType}},
{icSigBToA0Tag, {icSigLut8Type,icSigLut16Type,icMaxEnumType}},
{icSigBToA1Tag, {icSigLut8Type,icSigLut16Type,icMaxEnumType}},
{icSigBToA2Tag, {icSigLut8Type,icSigLut16Type,icMaxEnumType}},
{icSigCalibrationDateTimeTag, {icSigDateTimeType,icMaxEnumType}},
{icSigChromaticAdaptationTag, {icSigS15Fixed16ArrayType,icMaxEnumType}},
{icSigCharTargetTag, {icSigTextType,icMaxEnumType}},
{icSigColorantTableTag, {icSigColorantTableType,icmSigAltColorantTableType,
icMaxEnumType}},
{icSigColorantTableOutTag, {icSigColorantTableType,icmSigAltColorantTableType,
icMaxEnumType}},
{icSigCopyrightTag, {icSigTextType,icMaxEnumType}},
{icSigCrdInfoTag, {icSigCrdInfoType,icMaxEnumType}},
{icSigDeviceMfgDescTag, {icSigTextDescriptionType,icMaxEnumType}},
{icSigDeviceModelDescTag, {icSigTextDescriptionType,icMaxEnumType}},
{icSigGamutTag, {icSigLut8Type,icSigLut16Type,icMaxEnumType}},
{icSigGrayTRCTag, {icSigCurveType,icMaxEnumType}},
{icSigGreenColorantTag, {icSigXYZType,icMaxEnumType}},
{icSigGreenTRCTag, {icSigCurveType,icMaxEnumType}},
{icSigLuminanceTag, {icSigXYZType,icMaxEnumType}},
{icSigMeasurementTag, {icSigMeasurementType,icMaxEnumType}},
{icSigMediaBlackPointTag, {icSigXYZType,icMaxEnumType}},
{icSigMediaWhitePointTag, {icSigXYZType,icMaxEnumType}},
{icSigNamedColorTag, {icSigNamedColorType,icMaxEnumType}},
{icSigNamedColor2Tag, {icSigNamedColor2Type,icMaxEnumType}},
{icSigPreview0Tag, {icSigLut8Type,icSigLut16Type,icMaxEnumType}},
{icSigPreview1Tag, {icSigLut8Type,icSigLut16Type,icMaxEnumType}},
{icSigPreview2Tag, {icSigLut8Type,icSigLut16Type,icMaxEnumType}},
{icSigProfileDescriptionTag, {icSigTextDescriptionType,icMaxEnumType}},
{icSigProfileSequenceDescTag, {icSigProfileSequenceDescType,icMaxEnumType}},
{icSigPs2CRD0Tag, {icSigDataType,icMaxEnumType}},
{icSigPs2CRD1Tag, {icSigDataType,icMaxEnumType}},
{icSigPs2CRD2Tag, {icSigDataType,icMaxEnumType}},
{icSigPs2CRD3Tag, {icSigDataType,icMaxEnumType}},
{icSigPs2CSATag, {icSigDataType,icMaxEnumType}},
{icSigPs2RenderingIntentTag, {icSigDataType,icMaxEnumType}},
{icSigRedColorantTag, {icSigXYZType,icMaxEnumType}},
{icSigRedTRCTag, {icSigCurveType,icMaxEnumType}},
{icSigScreeningDescTag, {icSigTextDescriptionType,icMaxEnumType}},
{icSigScreeningTag, {icSigScreeningType,icMaxEnumType}},
{icSigTechnologyTag, {icSigSignatureType,icMaxEnumType}},
{icSigUcrBgTag, {icSigUcrBgType,icMaxEnumType}},
{icSigVideoCardGammaTag, {icSigVideoCardGammaType,icMaxEnumType}},
{icSigViewingCondDescTag, {icSigTextDescriptionType,icMaxEnumType}},
{icSigViewingConditionsTag, {icSigViewingConditionsType,icMaxEnumType}},
{icmSigAbsToRelTransSpace, {icSigS15Fixed16ArrayType,icMaxEnumType}},
{icMaxEnumTag, {icMaxEnumType}}
};
/* Fake color tag for specifying PCS */
#define icSigPCSData ((icColorSpaceSignature) 0x50435320L)
/* Table that lists the required tags for various profiles */
static struct {
icProfileClassSignature sig; /* Profile signature */
int chans; /* Data Color channels, -ve for match but try next, */
/* -100 for ignore, -200 for ignore and try next */
icColorSpaceSignature colsig; /* Data Color space signature, icMaxEnumData for ignore, */
/* icSigPCSData for XYZ of Lab */
icColorSpaceSignature pcssig; /* PCS Color space signature, icMaxEnumData for ignore, */
/* icSigPCSData for XYZ or Lab */
icTagSignature tags[12]; /* Arbitrary max of 12 */
} tagchecktable[] = {
{icSigInputClass, -1, icMaxEnumData, icSigPCSData,
{icSigProfileDescriptionTag,
icSigGrayTRCTag,
icSigMediaWhitePointTag,
icSigCopyrightTag, icMaxEnumTag}},
{icSigInputClass, -3, icMaxEnumData, icSigXYZData,
{icSigProfileDescriptionTag,
icSigRedColorantTag,
icSigGreenColorantTag,
icSigBlueColorantTag,
icSigRedTRCTag,
icSigGreenTRCTag,
icSigBlueTRCTag,
icSigMediaWhitePointTag,
icSigCopyrightTag, icMaxEnumTag}},
{icSigInputClass, -100, icMaxEnumData, icSigPCSData,
{icSigProfileDescriptionTag,
icSigAToB0Tag,
icSigMediaWhitePointTag,
icSigCopyrightTag, icMaxEnumTag}},
{icSigDisplayClass, -1, icMaxEnumData, icSigPCSData,
{icSigProfileDescriptionTag,
icSigGrayTRCTag,
icSigMediaWhitePointTag,
icSigCopyrightTag, icMaxEnumTag}},
{icSigDisplayClass, -3, icSigRgbData, icSigXYZData, /* Rgb or any 3 component space ?? */
{icSigProfileDescriptionTag,
icSigRedColorantTag,
icSigGreenColorantTag,
icSigBlueColorantTag,
icSigRedTRCTag,
icSigGreenTRCTag,
icSigBlueTRCTag,
icSigMediaWhitePointTag,
icSigCopyrightTag, icMaxEnumTag}},
/* Non-3 component Display device */
{icSigDisplayClass, -100, icMaxEnumData, icSigPCSData,
{icSigProfileDescriptionTag,
icSigAToB0Tag, /* BToA doesn't seem to be required, which is strange... */
icSigMediaWhitePointTag,
icSigCopyrightTag, icMaxEnumTag}},
{icSigOutputClass, -1, icMaxEnumData, icSigPCSData,
{icSigProfileDescriptionTag,
icSigGrayTRCTag,
icSigMediaWhitePointTag,
icSigCopyrightTag, icMaxEnumTag}},
{icSigOutputClass, -1, icMaxEnumData, icSigPCSData,
{icSigProfileDescriptionTag,
icSigAToB0Tag,
icSigBToA0Tag,
icSigGamutTag,
icSigAToB1Tag,
icSigBToA1Tag,
icSigAToB2Tag,
icSigBToA2Tag,
icSigMediaWhitePointTag,
icSigCopyrightTag, icMaxEnumTag}},
{icSigOutputClass, -2, icMaxEnumData, icSigPCSData,
{icSigProfileDescriptionTag,
icSigAToB0Tag,
icSigBToA0Tag,
icSigGamutTag,
icSigAToB1Tag,
icSigBToA1Tag,
icSigAToB2Tag,
icSigBToA2Tag,
icSigMediaWhitePointTag,
icSigCopyrightTag, icMaxEnumTag}},
{icSigOutputClass, -3, icMaxEnumData, icSigPCSData,
{icSigProfileDescriptionTag,
icSigAToB0Tag,
icSigBToA0Tag,
icSigGamutTag,
icSigAToB1Tag,
icSigBToA1Tag,
icSigAToB2Tag,
icSigBToA2Tag,
icSigMediaWhitePointTag,
icSigCopyrightTag, icMaxEnumTag}},
{icSigOutputClass, -4, icMaxEnumData, icSigPCSData,
{icSigProfileDescriptionTag,
icSigAToB0Tag,
icSigBToA0Tag,
icSigGamutTag,
icSigAToB1Tag,
icSigBToA1Tag,
icSigAToB2Tag,
icSigBToA2Tag,
icSigMediaWhitePointTag,
icSigCopyrightTag, icMaxEnumTag}},
{icSigOutputClass, -100, icMaxEnumData, icSigPCSData, /* Assumed from Hexachrome examples */
{icSigProfileDescriptionTag,
icSigBToA0Tag,
icSigBToA1Tag,
icSigBToA2Tag,
icSigMediaWhitePointTag,
icSigCopyrightTag, icMaxEnumTag}},
{icSigLinkClass, -100, icMaxEnumData, icMaxEnumData,
{icSigProfileDescriptionTag,
icSigAToB0Tag,
icSigProfileSequenceDescTag,
icSigCopyrightTag, icMaxEnumTag}},
{icSigColorSpaceClass, -100, icMaxEnumData, icSigPCSData,
{icSigProfileDescriptionTag,
icSigBToA0Tag,
icSigAToB0Tag,
icSigMediaWhitePointTag,
icSigCopyrightTag, icMaxEnumTag}},
{icSigAbstractClass, -100, icSigPCSData, icSigPCSData,
{icSigProfileDescriptionTag,
icSigAToB0Tag,
icSigMediaWhitePointTag,
icSigCopyrightTag, icMaxEnumTag}},
{icSigNamedColorClass, -200, icMaxEnumData, icMaxEnumData,
{icSigProfileDescriptionTag,
icSigNamedColorTag, /* Not strictly V3.4 */
icSigMediaWhitePointTag,
icSigCopyrightTag, icMaxEnumTag}},
{icSigNamedColorClass, -100, icMaxEnumData, icMaxEnumData,
{icSigProfileDescriptionTag,
icSigNamedColor2Tag,
icSigMediaWhitePointTag,
icSigCopyrightTag, icMaxEnumTag}},
{icMaxEnumClass,-1,icMaxEnumData, icMaxEnumData, {icMaxEnumTag}}
};
/* ------------------------------------------------------------- */
/* Return the current read fp (if any) */
static icmFile *icc_get_rfp(icc *p) {
return p->fp;
}
/* Change the version to be non-default (ie. not 2.2.0), */
/* e.g. ICC V4 (used for creation) */
/* Return 0 if OK */
/* Return 1 on error */
static int icc_set_version(icc *p, icmICCVersion ver) {
if (p->header == NULL) {
sprintf(p->err,"icc_set_version: Header is missing");
return p->errc = 1;
}
switch (ver) {
case icmVersionDefault:
p->header->majv = 2;
p->header->minv = 2;
p->header->bfv = 0;
break;
case icmVersion2_3:
p->header->majv = 2;
p->header->minv = 3;
p->header->bfv = 0;
break;
case icmVersion2_4:
p->header->majv = 2;
p->header->minv = 4;
p->header->bfv = 0;
break;
#ifdef ENABLE_V4
case icmVersion4_1:
p->header->majv = 4;
p->header->minv = 1;
p->header->bfv = 0;
break;
#endif
default:
sprintf(p->err,"icc_set_version: Unsupported version 0x%x",ver);
return p->errc = 1;
}
return 0;
}
/* Check that the ICC profile looks like it will be legal. */
/* Return non-zero and set error string if not */
static int check_icc_legal(
icc *p
) {
int i, j;
icProfileClassSignature sig;
icColorSpaceSignature colsig;
icColorSpaceSignature pcssig;
int dchans;
if (p->header == NULL) {
sprintf(p->err,"icc_check_legal: Header is missing");
return p->errc = 1;
}
sig = p->header->deviceClass;
colsig = p->header->colorSpace;
dchans = number_ColorSpaceSignature(colsig);
pcssig = p->header->pcs;
/* Find a matching table entry */
for (i = 0; tagchecktable[i].sig != icMaxEnumType; i++) {
if ( tagchecktable[i].sig == sig
&& ( tagchecktable[i].chans == dchans /* Exactly matches */
|| tagchecktable[i].chans == -dchans /* Exactly matches, but can try next table */
|| tagchecktable[i].chans < -99) /* Doesn't have to match or try next table */
&& ( tagchecktable[i].colsig == colsig
|| (tagchecktable[i].colsig == icSigPCSData
&& (colsig == icSigXYZData || colsig == icSigLabData))
|| tagchecktable[i].colsig == icMaxEnumData)
&& ( tagchecktable[i].pcssig == pcssig
|| (tagchecktable[i].pcssig == icSigPCSData
&& (pcssig == icSigXYZData || pcssig == icSigLabData))
|| tagchecktable[i].pcssig == icMaxEnumData)) {
/* Found entry, so now check that all the required tags are present. */
for (j = 0; tagchecktable[i].tags[j] != icMaxEnumType; j++) {
if (p->find_tag(p, tagchecktable[i].tags[j]) != 0) { /* Not present! */
#ifdef NEVER
printf("icc_check_legal: deviceClass %s is missing required tag %s\n", tag2str(sig), tag2str(tagchecktable[i].tags[j]));
#endif
if (tagchecktable[i].chans == -200
|| tagchecktable[i].chans == -dchans) { /* But can try next table */
break;
}
/* ~~99 Hmm. Should report all possible missing tags from */
/* previous failed tables ~~~999 */
sprintf(p->err,"icc_check_legal: deviceClass %s is missing required tag %s",
tag2str(sig), tag2str(tagchecktable[i].tags[j]));
return p->errc = 1;
}
}
if (tagchecktable[i].tags[j] == icMaxEnumType) {
break; /* Fount all required tags */
}
}
}
/* According to the spec. if the deviceClass is:
an Abstract Class: both in and out color spaces should be PCS
an Link Class: both in and out color spaces can be any, and should
be the input space of the first profile in the link, and the
input space of the last profile in the link respectively.
a Named Class: in and out color spaces are not defined in the spec.
Input, Display, Output and ColorSpace Classes, input color
space can be any, and the output space must be PCS.
~~ should check this here ???
*/
return 0; /* Assume anything is ok */
}
/* read the object, return 0 on success, error code on fail */
/* NOTE: this doesn't read the tag types, they should be read on demand. */
/* NOTE: fp ownership is taken even if the function fails. */
static int icc_read_x(
icc *p,
icmFile *fp, /* File to read from */
unsigned int of, /* File offset to read from */
int take_fp /* NZ if icc is to take ownership of fp */
) {
char tcbuf[4]; /* Tag count read buffer */
unsigned int i, len;
unsigned int minoff, maxoff; /* Minimum and maximum offsets of tag data */
int er = 0; /* Error code */
p->fp = fp;
if (take_fp)
p->del_fp = 1;
p->of = of;
if (p->header == NULL) {
sprintf(p->err,"icc_read: No header defined");
return p->errc = 1;
}
/* Read the header */
if (p->header->read(p->header, 128, of)) {
return 1;
}
/* Read the tag count */
if ( p->fp->seek(p->fp, of + 128) != 0
|| p->fp->read(p->fp, tcbuf, 1, 4) != 4) {
sprintf(p->err,"icc_read: fseek() or fread() failed on tag count");
return p->errc = 1;
}
p->count = read_UInt32Number(tcbuf);
/* Sanity check it */
if (p->count > 357913940 /* (2^32-5)/12 */
|| (p->count > ((p->header->size - 128 - 4) / 12))) {
sprintf(p->err,"icc_read: tag count %d is too large to be legal",p->count);
return p->errc = 1;
}
minoff = 128 + 4 + p->count * 12;
maxoff = p->header->size;
if (p->count > 0) {
char *bp, *buf;
if (ovr_mul(p->count, sizeof(icmTag))) {
sprintf(p->err,"icc_read: size overflow");
return p->errc = 1;
}
/* Read the table into memory */
if ((p->data = (icmTag *) p->al->calloc(p->al, p->count, sizeof(icmTag))) == NULL) {
sprintf(p->err,"icc_read: Tag table malloc() failed");
return p->errc = 2;
}
len = sat_mul(p->count, 12);
if ((buf = (char *) p->al->malloc(p->al, len)) == NULL) {
sprintf(p->err,"icc_read: Tag table read buffer malloc() failed");
p->al->free(p->al, p->data);
p->data = NULL;
return p->errc = 2;
}
if ( p->fp->seek(p->fp, of + 128 + 4) != 0
|| p->fp->read(p->fp, buf, 1, len) != len) {
sprintf(p->err,"icc_read: fseek() or fread() failed on tag table");
p->al->free(p->al, p->data);
p->data = NULL;
p->al->free(p->al, buf);
return p->errc = 1;
}
/* Fill in the tag table structure for each tag */
for (bp = buf, i = 0; i < p->count; i++, bp += 12) {
p->data[i].sig = (icTagSignature)read_SInt32Number(bp + 0);
p->data[i].offset = read_UInt32Number(bp + 4);
p->data[i].size = read_UInt32Number(bp + 8);
}
p->al->free(p->al, buf);
/* Check that each tag lies within the nominated space available, */
/* and has a reasonable size. */
for (i = 0; i < p->count; i++) {
if (p->data[i].offset < minoff
|| p->data[i].offset > maxoff
|| p->data[i].size < 4
|| p->data[i].size > (maxoff - minoff)
|| (p->data[i].offset + p->data[i].size) < p->data[i].offset /* Overflow */
|| (p->data[i].offset + p->data[i].size) > p->header->size) {
sprintf(p->err,"icc_read: tag %d sig %s offset %d size %d is out of range of the nominated file size %d",i,tag2str(p->data[i].sig),p->data[i].offset,p->data[i].size,maxoff);
p->al->free(p->al, p->data);
p->data = NULL;
return p->errc = 1;
}
}
/* Read each tag type */
for (i = 0; i < p->count; i++) {
if ( p->fp->seek(p->fp, of + p->data[i].offset) != 0
|| p->fp->read(p->fp, tcbuf, 1, 4) != 4) {
sprintf(p->err,"icc_read: fseek() or fread() failed on tag headers");
p->al->free(p->al, p->data);
p->data = NULL;
return p->errc = 1;
}
p->data[i].ttype = (icTagTypeSignature) read_SInt32Number(tcbuf); /* Tag type */
p->data[i].objp = NULL; /* Read on demand */
}
} /* p->count > 0 */
/* Check if there is an ArgyllCMS 'arts' tag, and setup the wpchtmx[][] matrix from it. */
{
icmS15Fixed16Array *artsTag;
if ((artsTag = (icmS15Fixed16Array *)p->read_tag(p, icmSigAbsToRelTransSpace)) != NULL
&& artsTag->ttype == icSigS15Fixed16ArrayType
&& artsTag->size >= 9) {
p->wpchtmx[0][0] = artsTag->data[0];
p->wpchtmx[0][1] = artsTag->data[1];
p->wpchtmx[0][2] = artsTag->data[2];
p->wpchtmx[1][0] = artsTag->data[3];
p->wpchtmx[1][1] = artsTag->data[4];
p->wpchtmx[1][2] = artsTag->data[5];
p->wpchtmx[2][0] = artsTag->data[6];
p->wpchtmx[2][1] = artsTag->data[7];
p->wpchtmx[2][2] = artsTag->data[8];
icmInverse3x3(p->iwpchtmx, p->wpchtmx);
p->useArts = 1; /* Save it if it was in profile */
} else {
/* If an ArgyllCMS created profile, or if it's a Display profile, */
/* use Bradford. This makes sRGB and AdobeRGB etc. work correctly */
/* for absolute colorimetic. Note that for display profiles that set */
/* the WP to D50 and store their chromatic transform in the 'chad' tag, */
/* (i.e. some V2 profiles and all V4 profiles) this will have no effect */
/* on the Media Relative WP Transformation since D50 -> D50, and */
/* the 'chad' tag will be used to set the internal MediaWhite value */
/* and transform matrix. */
if (p->header->creator == str2tag("argl")
|| p->header->deviceClass == icSigDisplayClass) {
icmCpy3x3(p->wpchtmx, icmBradford);
icmInverse3x3(p->iwpchtmx, p->wpchtmx);
/* Default to ICC standard Wrong Von Kries */
/* for non-ArgyllCMS, non-Display profiles. */
} else {
icmCpy3x3(p->wpchtmx, icmWrongVonKries);
icmCpy3x3(p->iwpchtmx, icmWrongVonKries);
}
p->useArts = 0; /* Don't save it, as it wasn't in profile */
}
p->wpchtmx_class = p->header->deviceClass; /* It's set for this class now */
}
/* If this is a Display or Output profile, check if there is a 'chad' tag, and read it */
/* in if it exists. We will use this latter when we interpret absolute colorimetric, */
/* and this also prevents auto creation of a chad tag on write if wrD/OChad is set. */
{
icmS15Fixed16Array *chadTag;
if ((p->header->deviceClass == icSigDisplayClass
|| p->header->deviceClass == icSigOutputClass)
&& (chadTag = (icmS15Fixed16Array *)p->read_tag(p, icSigChromaticAdaptationTag)) != NULL
&& chadTag->ttype == icSigS15Fixed16ArrayType
&& chadTag->size == 9) {
p->chadmx[0][0] = chadTag->data[0];
p->chadmx[0][1] = chadTag->data[1];
p->chadmx[0][2] = chadTag->data[2];
p->chadmx[1][0] = chadTag->data[3];
p->chadmx[1][1] = chadTag->data[4];
p->chadmx[1][2] = chadTag->data[5];
p->chadmx[2][0] = chadTag->data[6];
p->chadmx[2][1] = chadTag->data[7];
p->chadmx[2][2] = chadTag->data[8];
p->naturalChad = 1;
p->chadmxValid = 1;
}
}
/* It would be nice to have an option to convert 'chad' based profile */
/* into non-chad profiles, but this is non trivial, since the wpchtmx would */
/* need to be determined from the chad matrix. While this is technically */
/* possible (see chex.c for an attempt at this), it is not easy, and */
/* it's possible for the chad matrix to be a non Von Kries type transformation, */
/* which cannot be exactly decomposed into a cone space matrix + Von Kries scaling. */
return er;
}
/* read the object, return 0 on success, error code on fail */
/* NOTE: this doesn't read the tag types, they should be read on demand. */
/* (backward compatible version) */
static int icc_read(
icc *p,
icmFile *fp, /* File to read from */
unsigned int of /* File offset to read from */
) {
return icc_read_x(p, fp, of, 0);
}
/* Check the profiles ID. We assume the file has already been read. */
/* Return 0 if OK, 1 if no ID to check, 2 if doesn't match, 3 if some other error. */
/* NOTE: this reads the whole file again, to compute the checksum. */
static int icc_check_id(
icc *p,
ORD8 *rid /* Optionaly return computed ID */
) {
unsigned char buf[128];
ORD8 id[16];
icmMD5 *md5 = NULL;
unsigned int len;
if (p->header == NULL) {
sprintf(p->err,"icc_check_id: No header defined");
return p->errc = 3;
}
len = p->header->size; /* Claimed size of profile */
/* See if there is an ID to compare against */
for (len = 0; len < 16; len++) {
if (p->header->id[len] != 0)
break;
}
if (len >= 16) {
return 1;
}
if ((md5 = new_icmMD5_a(p->al)) == NULL) {
sprintf(p->err,"icc_check_id: new_icmMD5 failed");
return p->errc = 3;
}
/* Check the header */
if ( p->fp->seek(p->fp, p->of) != 0
|| p->fp->read(p->fp, buf, 1, 128) != 128) {
sprintf(p->err,"icc_check_id: fseek() or fread() failed");
return p->errc = 3;
}
/* Zero the appropriate bytes in the header */
buf[44] = buf[45] = buf[46] = buf[47] = 0;
buf[64] = buf[65] = buf[66] = buf[67] = 0;
buf[84] = buf[85] = buf[86] = buf[87] =
buf[88] = buf[89] = buf[90] = buf[91] =
buf[92] = buf[93] = buf[94] = buf[95] =
buf[96] = buf[97] = buf[98] = buf[99] = 0;
md5->add(md5, buf, 128);
/* Suck in the rest of the profile */
for (;len > 0;) {
unsigned int rsize = 128;
if (rsize > len)
rsize = len;
if (p->fp->read(p->fp, buf, 1, rsize) != rsize) {
sprintf(p->err,"icc_check_id: fread() failed");
return p->errc = 3;
}
md5->add(md5, buf, rsize);
len -= rsize;
}
md5->get(md5, id);
md5->del(md5);
if (rid != NULL) {
for (len = 0; len < 16; len++)
rid[len] = id[len];
}
/* Check the ID */
for (len = 0; len < 16; len++) {
if (p->header->id[len] != id[len])
break;
}
if (len >= 16) {
return 0; /* Matched */
}
return 2; /* Didn't match */
}
static void icc_setup_wpchtmx(icc *p);
void icmQuantize3x3S15Fixed16(double targ[3], double mat[3][3], double in[3]);
/* Add any automatically created tags. */
/* Modify white point value if wr is nz. (i.e. in middle of ->write()) */
/* The 'chad' tag is only added if there is no natural 'chad' tag, */
/* and will be remove once the write is complete. */
static int icc_add_auto_tags(icc *p, int wr) {
/* If we're using the ArgyllCMS 'arts' tag to record the chromatic */
/* adapation cone matrix used for the Media Relative WP Transformation, */
/* create it and set it from the wpchtmx[][] matrix. */
/* Don't write it if there is no 'wtpt' tag (i.e. it's a device link) */
if (p->useArts
&& p->find_tag(p, icSigMediaWhitePointTag) == 0) {
int rv;
icmS15Fixed16Array *artsTag;
/* Make sure wpchtmx[][] has been set correctly for device class */
if (p->wpchtmx_class != p->header->deviceClass) {
icc_setup_wpchtmx(p);
}
/* Make sure no 'arts' tag currently exists */
if (p->delete_tag(p, icmSigAbsToRelTransSpace) != 0
&& p->errc != 2) {
sprintf(p->err,"icc_write: Deleting existing 'arts' tag failed");
return p->errc = 1;
}
/* Add one */
if ((artsTag = (icmS15Fixed16Array *)p->add_tag(p, icmSigAbsToRelTransSpace,
icSigS15Fixed16ArrayType)) == NULL) {
sprintf(p->err,"icc_write: Adding 'arts' tag failed");
return p->errc = 1;
}
artsTag->size = 9;
if ((rv = artsTag->allocate((icmBase *)artsTag) ) != 0) {
sprintf(p->err,"icc_write: Allocating 'arts' tag failed");
return p->errc = 1;
}
if (wr) {
/* The cone matrix is assumed to be arranged conventionaly for matrix */
/* times vector multiplication. */
/* Consistent with ICC usage, the dimension corresponding to the matrix */
/* rows varies least rapidly while the one corresponding to the matrix */
/* columns varies most rapidly. */
artsTag->data[0] = p->wpchtmx[0][0];
artsTag->data[1] = p->wpchtmx[0][1];
artsTag->data[2] = p->wpchtmx[0][2];
artsTag->data[3] = p->wpchtmx[1][0];
artsTag->data[4] = p->wpchtmx[1][1];
artsTag->data[5] = p->wpchtmx[1][2];
artsTag->data[6] = p->wpchtmx[2][0];
artsTag->data[7] = p->wpchtmx[2][1];
artsTag->data[8] = p->wpchtmx[2][2];
}
}
/* If this is a Display profile, and we have been told to save it in */
/* ICCV4 style, then set the media white point tag to D50 and save */
/* the chromatic adapation matrix to the 'chad' tag. */
{
int rv;
icmXYZArray *whitePointTag;
icmS15Fixed16Array *chadTag;
if (p->header->deviceClass == icSigDisplayClass
&& p->wrDChad && !p->naturalChad
&& (whitePointTag = (icmXYZArray *)p->read_tag(p, icSigMediaWhitePointTag)) != NULL
&& whitePointTag->ttype == icSigXYZType
&& whitePointTag->size >= 1) {
/* If we've set this profile, not just read it, */
/* compute the fromAbs/chad matrix from media white point and cone matrix */
if (!p->chadmxValid) {
double wp[3];
p->chromAdaptMatrix(p, ICM_CAM_NONE, NULL, p->chadmx,
icmD50, whitePointTag->data[0]);
/* Optimally quantize chad matrix to preserver white point */
icmXYZ2Ary(wp, whitePointTag->data[0]);
icmQuantize3x3S15Fixed16(icmD50_ary3, p->chadmx, wp);
p->chadmxValid = 1;
}
/* Make sure no 'chad' tag currently exists */
if (p->delete_tag(p, icSigChromaticAdaptationTag) != 0
&& p->errc != 2) {
sprintf(p->err,"icc_write: Deleting existing 'chad' tag failed");
return p->errc = 1;
}
/* Add one */
if ((chadTag = (icmS15Fixed16Array *)p->add_tag(p, icSigChromaticAdaptationTag,
icSigS15Fixed16ArrayType)) == NULL) {
sprintf(p->err,"icc_write: Adding 'chad' tag failed");
return p->errc = 1;
}
chadTag->size = 9;
if ((rv = chadTag->allocate((icmBase *)chadTag) ) != 0) {
sprintf(p->err,"icc_write: Allocating 'chad' tag failed");
return p->errc = 1;
}
p->tempChad = 1;
if (wr) {
/* Save in ICC matrix order */
chadTag->data[0] = p->chadmx[0][0];
chadTag->data[1] = p->chadmx[0][1];
chadTag->data[2] = p->chadmx[0][2];
chadTag->data[3] = p->chadmx[1][0];
chadTag->data[4] = p->chadmx[1][1];
chadTag->data[5] = p->chadmx[1][2];
chadTag->data[6] = p->chadmx[2][0];
chadTag->data[7] = p->chadmx[2][1];
chadTag->data[8] = p->chadmx[2][2];
/* Set 'chad' adhusted white point */
p->tempWP = whitePointTag->data[0];
whitePointTag->data[0] = icmD50;
}
}
}
/* If this is an Output profile with a non-standard illuminant set, */
/* and we have been told to save it using a 'chad' tag to represent */
/* the illuminant difference, then adjust the media white point tag */
/* for the illuminant, and change the 'chad' tag. */
{
int rv;
icmXYZArray *whitePointTag;
icmS15Fixed16Array *chadTag;
if (p->header->deviceClass == icSigOutputClass
&& p->chadmxValid
&& p->wrOChad && !p->naturalChad
&& (whitePointTag = (icmXYZArray *)p->read_tag(p, icSigMediaWhitePointTag)) != NULL
&& whitePointTag->ttype == icSigXYZType
&& whitePointTag->size >= 1) {
double wp[3];
/* Make sure no 'chad' tag currently exists */
if (p->delete_tag(p, icSigChromaticAdaptationTag) != 0
&& p->errc != 2) {
sprintf(p->err,"icc_write: Deleting existing 'chad' tag failed");
return p->errc = 1;
}
/* Add one */
if ((chadTag = (icmS15Fixed16Array *)p->add_tag(p, icSigChromaticAdaptationTag,
icSigS15Fixed16ArrayType)) == NULL) {
sprintf(p->err,"icc_write: Adding 'chad' tag failed");
return p->errc = 1;
}
chadTag->size = 9;
if ((rv = chadTag->allocate((icmBase *)chadTag) ) != 0) {
sprintf(p->err,"icc_write: Allocating 'chad' tag failed");
return p->errc = 1;
}
p->tempChad = 1;
if (wr) {
/* Save in ICC matrix order */
chadTag->data[0] = p->chadmx[0][0];
chadTag->data[1] = p->chadmx[0][1];
chadTag->data[2] = p->chadmx[0][2];
chadTag->data[3] = p->chadmx[1][0];
chadTag->data[4] = p->chadmx[1][1];
chadTag->data[5] = p->chadmx[1][2];
chadTag->data[6] = p->chadmx[2][0];
chadTag->data[7] = p->chadmx[2][1];
chadTag->data[8] = p->chadmx[2][2];
/* Transform white point to take 'chad' into account */
p->tempWP = whitePointTag->data[0];
icmXYZ2Ary(wp, whitePointTag->data[0]);
icmMulBy3x3(wp, p->chadmx, wp);
icmAry2XYZ(whitePointTag->data[0], wp);
}
}
}
return 0;
}
/* Restore profile after creating temporary 'chad' tag, and */
/* modifying the white point. */
static int icc_rem_temp_tags(icc *p) {
/* Restore profile if Display 'chad' has been temporarily added. */
{
int rv;
icmXYZArray *whitePointTag;
icmS15Fixed16Array *chadTag;
if (p->header->deviceClass == icSigDisplayClass
&& p->tempChad && p->wrDChad && !p->naturalChad
&& (whitePointTag = (icmXYZArray *)p->read_tag(p, icSigMediaWhitePointTag)) != NULL
&& whitePointTag->ttype == icSigXYZType
&& whitePointTag->size >= 1) {
/* Remove temporary 'chad' tag */
if (p->delete_tag(p, icSigChromaticAdaptationTag) != 0
&& p->errc != 2) {
sprintf(p->err,"icc_write: Deleting temporary 'chad' tag failed");
return p->errc = 1;
}
/* Restore original white point */
whitePointTag->data[0] = p->tempWP;
}
}
/* Restore profile if Output 'chad' has been temporarily added. */
{
int rv;
icmXYZArray *whitePointTag;
icmS15Fixed16Array *chadTag;
if (p->header->deviceClass == icSigOutputClass
&& p->tempChad && p->wrOChad && !p->naturalChad
&& (whitePointTag = (icmXYZArray *)p->read_tag(p, icSigMediaWhitePointTag)) != NULL
&& whitePointTag->ttype == icSigXYZType
&& whitePointTag->size >= 1) {
double wp[3];
/* Remove temporary 'chad' tag */
if (p->delete_tag(p, icSigChromaticAdaptationTag) != 0
&& p->errc != 2) {
sprintf(p->err,"icc_write: Deleting temporary 'chad' tag failed");
return p->errc = 1;
}
/* Restore original white point */
whitePointTag->data[0] = p->tempWP;
}
}
return 0;
}
/* Return the total size needed for the profile. */
/* This will add any automatic tags such as 'arts' tag, */
/* so the current information needs to be final enough */
/* for the automatic tag creation to be correct. */
/* Return 0 on error. */
static unsigned int icc_get_size(
icc *p
) {
unsigned int i, size = 0;
/* Add 'arts' tag and temporary 'chad' tag if so configured */
icc_add_auto_tags(p, 0);
#ifdef ICM_STRICT
/* Check that the right tags etc. are present for a legal ICC profile */
if (check_icc_legal(p) != 0) {
return 0;
}
#endif /* ICM_STRICT */
/* Compute the total size and tag element data offsets */
if (p->header == NULL) {
sprintf(p->err,"icc_get_size: No header defined");
p->errc = 1;
return 0;
}
size = sat_add(size, p->header->get_size(p->header));
/* Assume header is aligned */
size = sat_addaddmul(size, 4, p->count, 12); /* Tag table length */
size = sat_align(ALIGN_SIZE, size);
if (size == UINT_MAX) {
sprintf(p->err,"icc_get_size: size overflow");
return p->errc = 1;
}
/* Reset touched flag for each tag type */
for (i = 0; i < p->count; i++) {
if (p->data[i].objp == NULL) {
sprintf(p->err,"icc_get_size: Internal error - NULL tag element");
p->errc = 1;
return 0;
}
p->data[i].objp->touched = 0;
}
/* Get size for each tag type, skipping links */
for (i = 0; i < p->count; i++) {
if (p->data[i].objp->touched == 0) { /* Not alllowed for previously */
size = sat_add(size, p->data[i].objp->get_size(p->data[i].objp));
size = sat_align(ALIGN_SIZE, size);
p->data[i].objp->touched = 1; /* Don't account for this again */
}
}
return size; /* Total size needed, or UINT_MAX if overflow */
}
/* Write the contents of the object. Return 0 on sucess, error code on failure */
/* NOTE: fp ownership is taken even if the function fails. */
static int icc_write_x(
icc *p,
icmFile *fp, /* File to write to */
unsigned int of, /* File offset to write to */
int take_fp /* NZ if icc is to take ownership of fp */
) {
char *bp, *buf; /* tag table buffer */
unsigned int len;
int rv = 0;
unsigned int i, size = 0;
unsigned char pbuf[ALIGN_SIZE];
/* Add 'arts' tag and temporary 'chad' tag and modify white point, if so configured */
if ((rv = icc_add_auto_tags(p, 1)) != 0)
return rv;
p->fp = fp; /* Open file pointer */
if (take_fp)
p->del_fp = 1;
p->of = of; /* Offset of ICC profile */
/* Compute the total size and tag element data offsets */
if (p->header == NULL) {
sprintf(p->err,"icc_write: No header defined");
return p->errc = 1;
}
/* Check that the right tags etc. are present for a legal ICC profile */
if ((rv = check_icc_legal(p)) != 0) {
return rv;
}
for (i = 0; i < ALIGN_SIZE; i++)
pbuf[i] = 0;
size = sat_add(size, p->header->get_size(p->header));
/* Assume header is aligned */
len = sat_addmul(4, p->count, 12); /* Tag table length */
len = sat_sub(sat_align(ALIGN_SIZE, sat_add(size, len)), size); /* Aligned size */
size = sat_align(ALIGN_SIZE, sat_add(size, len));
if (len == UINT_MAX) {
sprintf(p->err,"icc_write get_size overflow");
return p->errc = 1;
}
/* Allocate memory buffer for tag table */
if ((buf = (char *) p->al->calloc(p->al, 1, len)) == NULL) {
sprintf(p->err,"icc_write calloc() failed");
return p->errc = 2;
}
bp = buf;
if ((rv = write_UInt32Number(p->count, bp)) != 0) { /* Tag count */
sprintf(p->err,"icc_write: write_UInt32Number() failed on tag count");
p->al->free(p->al, buf);
return p->errc = rv;
}
bp += 4;
/* Reset touched flag for each tag type */
for (i = 0; i < p->count; i++) {
if (p->data[i].objp == NULL) {
sprintf(p->err,"icc_write: Internal error - NULL tag element");
p->al->free(p->al, buf);
return p->errc = 1;
}
p->data[i].objp->touched = 0;
}
/* Set the offset and size for each tag type, create the tag table write data */
/* and compute the total profile size. */
for (i = 0; i < p->count; i++) {
if (p->data[i].objp->touched == 0) { /* Allocate space for tag type */
p->data[i].offset = size; /* Profile relative target */
p->data[i].size = p->data[i].objp->get_size(p->data[i].objp);
size = sat_add(size, p->data[i].size);
p->data[i].pad = sat_sub(sat_align(ALIGN_SIZE, size), size);
size = sat_align(ALIGN_SIZE, size);
p->data[i].objp->touched = 1; /* Allocated space for it */
if (size == UINT_MAX) {
sprintf(p->err,"icc_write: size overflow");
return p->errc = 1;
}
} else { /* must be linked - copy allocation */
unsigned int k;
for (k = 0; k < p->count; k++) { /* Find linked tag */
if (p->data[k].objp == p->data[i].objp)
break;
}
if (k == p->count) {
sprintf(p->err,"icc_write: corrupted link");
return p->errc = 2;
}
p->data[i].offset = p->data[k].offset;
p->data[i].size = p->data[k].size;
p->data[i].pad = p->data[k].pad;
}
/* Write tag table entry for this tag */
if ((rv = write_SInt32Number((int)p->data[i].sig,bp + 0)) != 0) {
sprintf(p->err,"icc_write: write_SInt32Number() failed on tag signature");
p->al->free(p->al, buf);
return p->errc = rv;
}
if ((rv = write_UInt32Number(p->data[i].offset, bp + 4)) != 0) {
sprintf(p->err,"icc_write: write_UInt32Number() failed on tag offset");
p->al->free(p->al, buf);
return p->errc = rv;
}
if ((rv = write_UInt32Number(p->data[i].size, bp + 8)) != 0) {
sprintf(p->err,"icc_write: write_UInt32Number() failed on tag size");
p->al->free(p->al, buf);
return p->errc = rv;
}
bp += 12;
}
p->header->size = size; /* Record total icc padded size */
/* If V4.0+, Compute the MD5 id for the profile. */
/* We do this by writing to a fake icmFile */
if (p->ver >= icmVersion4_1) {
icmMD5 *md5 = NULL;
icmFile *ofp, *dfp = NULL;
if ((md5 = new_icmMD5_a(p->al)) == NULL) {
sprintf(p->err,"icc_write: new_icmMD5 failed");
p->al->free(p->al, buf);
return p->errc = 2;
}
if ((dfp = new_icmFileMD5_a(md5, p->al)) == NULL) {
sprintf(p->err,"icc_write: new_icmFileMD5 failed");
md5->del(md5);
p->al->free(p->al, buf);
return p->errc = 2;
}
ofp = p->fp;
p->fp = dfp;
/* Dummy write the header */
if ((rv = p->header->write(p->header, 0, 1)) != 0) {
p->al->free(p->al, buf);
return rv;
}
/* Dummy write the tag table */
if ( p->fp->seek(p->fp, 128) != 0
|| p->fp->write(p->fp, buf, 1, len) != len) {
sprintf(p->err,"icc_write: seek() or write() failed");
p->al->free(p->al, buf);
return p->errc = 1;
}
/* Dummy write all the tag element data */
/* (We invert meaning of touched here) */
for (i = 0; i < p->count; i++) { /* For all the tag element data */
if (p->data[i].objp->touched == 0)
continue; /* Must be linked, and we've already written it */
if ((rv = p->data[i].objp->write(p->data[i].objp, p->data[i].offset)) != 0) {
p->al->free(p->al, buf);
return rv;
}
/* Pad with 0 to next boundary */
if (p->data[i].pad > 0) {
if (p->fp->write(p->fp, pbuf, 1, p->data[i].pad) != p->data[i].pad) {
sprintf(p->err,"icc_write: write() failed");
p->al->free(p->al, buf);
return p->errc = 1;
}
}
p->data[i].objp->touched = 0; /* Written it, so don't write it again. */
}
if (p->fp->flush(p->fp) != 0) {
sprintf(p->err,"icc_write flush() failed");
p->al->free(p->al, buf);
return p->errc = 1;
}
if ((p->errc = ((icmFileMD5 *)dfp)->get_errc(dfp)) != 0) {
sprintf(p->err,"icc_write compute ID failed with code %d", p->errc);
p->al->free(p->al, buf);
return p->errc;
}
/* Get the MD5 checksum ID */
md5->get(md5, p->header->id);
dfp->del(dfp);
md5->del(md5);
p->fp = ofp;
/* Reset the touched flags */
for (i = 0; i < p->count; i++)
p->data[i].objp->touched = 1;
}
/* Now write out the profile for real. */
/* Although it may appear like we're seeking for each element, */
/* in fact elements will be written in file order. */
/* Write the header */
if ((rv = p->header->write(p->header, of, 0)) != 0) {
p->al->free(p->al, buf);
return rv;
}
/* Write the tag table */
if ( p->fp->seek(p->fp, of + 128) != 0
|| p->fp->write(p->fp, buf, 1, len) != len) {
sprintf(p->err,"icc_write: seek() or write() failed");
p->al->free(p->al, buf);
return p->errc = 1;
}
p->al->free(p->al, buf);
/* Write all the tag element data */
/* (We invert the meaning of touched here) */
for (i = 0; i < p->count; i++) { /* For all the tag element data */
if (p->data[i].objp->touched == 0)
continue; /* Must be linked, and we've already written it */
if ((rv = p->data[i].objp->write(p->data[i].objp, of + p->data[i].offset)) != 0) {
return rv;
}
/* Pad with 0 to next boundary */
if (p->data[i].pad > 0) {
if (p->fp->write(p->fp, pbuf, 1, p->data[i].pad) != p->data[i].pad) {
sprintf(p->err,"icc_write: write() failed");
return p->errc = 1;
}
}
p->data[i].objp->touched = 0; /* Written it, so don't write it again. */
}
/* Remove any temporary 'chad' tag and restore white point */
if ((rv = icc_rem_temp_tags(p)) != 0)
return rv;
if (p->fp->flush(p->fp) != 0) {
sprintf(p->err,"icc_write flush() failed");
return p->errc = 1;
}
return rv;
}
/* Write the contents of the object. Return 0 on sucess, error code on failure */
/* (backwards compatible version) */
static int icc_write(
icc *p,
icmFile *fp, /* File to write to */
unsigned int of /* File offset to write to */
) {
return icc_write_x(p, fp, of, 0);
}
/* Create and add a tag with the given signature. */
/* Returns a pointer to the element object */
/* Returns NULL if error - icc->errc will contain */
/* 2 on system error, */
/* 3 if unknown tag */
/* 4 if duplicate tag */
/* NOTE: that we prevent tag duplication */
/* NOTE: to create a tag type icmSigUnknownType, set ttype to icmSigUnknownType, */
/* and set the actual tag type in icmSigUnknownType->uttype */
static icmBase *icc_add_tag(
icc *p,
icTagSignature sig, /* Tag signature - may be unknown */
icTagTypeSignature ttype /* Tag type */
) {
icmBase *tp;
icmBase *nob;
int i = 0, ok = 1;
unsigned int j;
if (ttype != icmSigUnknownType) { /* Check only for possibly known types */
/* Check that a known signature has an acceptable type */
for (i = 0; sigtypetable[i].sig != icMaxEnumType; i++) {
if (sigtypetable[i].sig == sig) { /* recognized signature */
ok = 0;
for (j = 0; sigtypetable[i].ttypes[j] != icMaxEnumType; j++) {
if (sigtypetable[i].ttypes[j] == ttype) /* recognized type */
ok = 1;
}
break;
}
}
if (!ok) {
sprintf(p->err,"icc_add_tag: wrong tag type for signature");
p->errc = 1;
return NULL;
}
/* Check that we know how to handle this type */
for (i = 0; typetable[i].ttype != icMaxEnumType; i++) {
if (typetable[i].ttype == ttype)
break;
}
if (typetable[i].ttype == icMaxEnumType) {
sprintf(p->err,"icc_add_tag: unsupported tag type");
p->errc = 1;
return NULL;
}
}
/* Check that this tag doesn't already exist */
/* (Perhaps we should simply replace it, rather than erroring ?) */
for (j = 0; j < p->count; j++) {
if (p->data[j].sig == sig) {
sprintf(p->err,"icc_add_tag: Already have tag '%s' in profile",tag2str(p->data[j].sig));
p->errc = 4;
return NULL;
}
}
/* Make space in tag table for new tag item */
if (ovr_mul(sat_add(p->count,1), sizeof(icmTag))) {
sprintf(p->err,"icc_add_tag: size overflow");
p->errc = 1;
return NULL;
}
if (p->data == NULL)
tp = (icmBase *)p->al->malloc(p->al, (p->count+1) * sizeof(icmTag));
else
tp = (icmBase *)p->al->realloc(p->al, (void *)p->data, (p->count+1) * sizeof(icmTag));
if (tp == NULL) {
sprintf(p->err,"icc_add_tag: Tag table realloc() failed");
p->errc = 2;
return NULL;
}
p->data = (icmTag *)tp;
if (ttype == icmSigUnknownType) {
if ((nob = new_icmUnknown(p)) == NULL)
return NULL;
} else {
/* Allocate the empty object */
if ((nob = typetable[i].new_obj(p)) == NULL)
return NULL;
}
/* Fill out our tag table entry */
p->data[p->count].sig = sig; /* The tag signature */
p->data[p->count].ttype = nob->ttype = ttype; /* The tag type signature */
p->data[p->count].offset = 0; /* Unknown offset yet */
p->data[p->count].size = 0; /* Unknown size yet */
p->data[p->count].objp = nob; /* Empty object */
p->count++;
/* Track whether we have a natural 'chad' tag */
if (sig == icSigChromaticAdaptationTag)
p->naturalChad = 1;
return nob;
}
/* Create and add a tag which is a link to an existing tag. */
/* Returns a pointer to the element object */
/* Returns NULL if error - icc->errc will contain */
/* 3 if incompatible tag */
/* NOTE: that we prevent tag duplication */
static icmBase *icc_link_tag(
icc *p,
icTagSignature sig, /* Tag signature - may be unknown */
icTagSignature ex_sig /* Tag signature of tag to link to */
) {
icmBase *tp;
unsigned int j, exi;
int i, ok = 1;
/* Search for existing signature */
for (exi = 0; exi < p->count; exi++) {
if (p->data[exi].sig == ex_sig) /* Found it */
break;
}
if (exi == p->count) {
sprintf(p->err,"icc_link_tag: Can't find existing tag '%s'",tag2str(ex_sig));
p->errc = 1;
return NULL;
}
if (p->data[exi].objp == NULL) {
sprintf(p->err,"icc_link_tag: Existing tag '%s' isn't loaded",tag2str(ex_sig));
p->errc = 1;
return NULL;
}
/* Check that a known signature has an acceptable type */
for (i = 0; sigtypetable[i].sig != icMaxEnumType; i++) {
if (sigtypetable[i].sig == sig) { /* recognized signature */
ok = 0;
for (j = 0; sigtypetable[i].ttypes[j] != icMaxEnumType; j++) {
if (sigtypetable[i].ttypes[j] == p->data[exi].ttype) /* recognized type */
ok = 1;
}
break;
}
}
if (!ok) {
sprintf(p->err,"icc_link_tag: wrong tag type for signature");
p->errc = 1;
return NULL;
}
/* Check that this tag doesn't already exits */
for (j = 0; j < p->count; j++) {
if (p->data[j].sig == sig) {
sprintf(p->err,"icc_link_tag: Already have tag '%s' in profile",tag2str(p->data[j].sig));
p->errc = 1;
return NULL;
}
}
/* Make space in tag table for new tag item */
if (p->data == NULL)
tp = (icmBase *)p->al->malloc(p->al, (p->count+1) * sizeof(icmTag));
else
tp = (icmBase *)p->al->realloc(p->al, (void *)p->data, (p->count+1) * sizeof(icmTag));
if (tp == NULL) {
sprintf(p->err,"icc_link_tag: Tag table realloc() failed");
p->errc = 2;
return NULL;
}
p->data = (icmTag *)tp;
/* Fill out our tag table entry */
p->data[p->count].sig = sig; /* The tag signature */
p->data[p->count].ttype = p->data[exi].ttype; /* The tag type signature */
p->data[p->count].offset = p->data[exi].offset; /* Same offset (may not be allocated yet) */
p->data[p->count].size = p->data[exi].size; /* Same size (may not be allocated yet) */
p->data[p->count].objp = p->data[exi].objp; /* Shared object */
p->data[exi].objp->refcount++; /* Bump reference count on tag type */
p->count++;
/* Track whether we have a natural 'chad' tag */
if (sig == icSigChromaticAdaptationTag)
p->naturalChad = 1;
return p->data[exi].objp;
}
/* Search for tag signature */
/* return: */
/* 0 if found */
/* 1 if found but not handled type */
/* 2 if not found */
/* NOTE: doesn't set icc->errc or icc->err[] */
/* NOTE: we don't handle tag duplication - you'll always get the first in the file. */
static int icc_find_tag(
icc *p,
icTagSignature sig /* Tag signature - may be unknown */
) {
unsigned int i;
int j;
/* Search for signature */
for (i = 0; i < p->count; i++) {
if (p->data[i].sig == sig) /* Found it */
break;
}
if (i == p->count)
return 2;
/* See if we can handle this type */
for (j = 0; typetable[j].ttype != icMaxEnumType; j++) {
if (typetable[j].ttype == p->data[i].ttype)
break;
}
if (typetable[j].ttype == icMaxEnumType)
return 1;
return 0;
}
/* Read the specific tag element data, and return a pointer to the object */
/* (This is an internal function) */
/* Returns NULL if error - icc->errc will contain: */
/* 2 if not found */
/* Returns an icmSigUnknownType object if the tag type isn't handled by a */
/* specific object and alow_unk is NZ */
/* NOTE: we don't handle tag duplication - you'll always get the first in the file */
static icmBase *icc_read_tag_ix(
icc *p,
unsigned int i, /* Index from 0.. p->count-1 */
int alow_unk /* NZ to allow unknown tag to load */
) {
icTagTypeSignature ttype; /* Tag type we will create */
icmBase *nob;
unsigned int k;
int j;
if (i >= p->count) {
sprintf(p->err,"icc_read_tag_ix: index %d is out of range",i);
p->errc = 2;
return NULL;
}
/* See if it's already been read */
if (p->data[i].objp != NULL) {
return p->data[i].objp; /* Just return it */
}
/* See if this should be a link */
for (k = 0; k < p->count; k++) {
if (i == k)
continue;
if (p->data[i].ttype == p->data[k].ttype /* Exact match and already read */
&& p->data[i].offset == p->data[k].offset
&& p->data[i].size == p->data[k].size
&& p->data[k].objp != NULL)
break;
}
if (k < p->count) { /* Make this a link */
p->data[i].objp = p->data[k].objp;
p->data[k].objp->refcount++; /* Bump reference count */
return p->data[k].objp; /* Done */
}
/* See if we can handle this type */
for (j = 0; typetable[j].ttype != icMaxEnumType; j++) {
if (typetable[j].ttype == p->data[i].ttype)
break;
}
if (typetable[j].ttype == icMaxEnumType) {
if (!alow_unk) {
sprintf(p->err,"icc_read_tag_ix: found unknown tag");
p->errc = 2;
return NULL;
}
ttype = icmSigUnknownType; /* Use the Unknown type to handle an unknown tag type */
} else {
ttype = p->data[i].ttype; /* We known this type */
}
/* Create and read in the object */
if (ttype == icmSigUnknownType)
nob = new_icmUnknown(p);
else
nob = typetable[j].new_obj(p);
if (nob == NULL)
return NULL;
if ((nob->read(nob, p->data[i].size, p->of + p->data[i].offset)) != 0) {
nob->del(nob); /* Failed, so destroy it */
return NULL;
}
p->data[i].objp = nob;
return nob;
}
/* Read the tag element data of the first matching, and return a pointer to the object */
/* Returns NULL if error - icc->errc will contain: */
/* 2 if not found */
/* Doesn't read uknown type tags */
static icmBase *icc_read_tag(
icc *p,
icTagSignature sig /* Tag signature - may be unknown */
) {
unsigned int i;
/* Search for signature */
for (i = 0; i < p->count; i++) {
if (p->data[i].sig == sig) /* Found it */
break;
}
if (i >= p->count) {
sprintf(p->err,"icc_read_tag: Tag '%s' not found",string_TagSignature(sig));
p->errc = 2;
return NULL;
}
/* Let read_tag_ix do all the work */
return icc_read_tag_ix(p, i, 0);
}
/* Read the tag element data of the first matching, and return a pointer to the object */
/* Returns NULL if error. */
/* Returns an icmSigUnknownType object if the tag type isn't handled by a specific object. */
/* NOTE: we don't handle tag duplication - you'll always get the first in the file. */
static icmBase *icc_read_tag_any(
icc *p,
icTagSignature sig /* Tag signature - may be unknown */
) {
unsigned int i;
/* Search for signature */
for (i = 0; i < p->count; i++) {
if (p->data[i].sig == sig) /* Found it */
break;
}
if (i >= p->count) {
sprintf(p->err,"icc_read_tag: Tag '%s' not found",string_TagSignature(sig));
p->errc = 2;
return NULL;
}
/* Let read_tag_ix do all the work */
return icc_read_tag_ix(p, i, 1);
}
/* Rename a tag signature */
static int icc_rename_tag(
icc *p,
icTagSignature sig, /* Existing Tag signature - may be unknown */
icTagSignature sigNew /* New Tag signature - may be unknown */
) {
unsigned int k;
int i, j, ok = 1;
/* Search for signature */
for (k = 0; k < p->count; k++) {
if (p->data[k].sig == sig) /* Found it */
break;
}
if (k >= p->count) {
sprintf(p->err,"icc_rename_tag: Tag '%s' not found",string_TagSignature(sig));
return p->errc = 2;
}
/* Check that a known new signature has an acceptable type */
for (i = 0; sigtypetable[i].sig != icMaxEnumType; i++) {
if (sigtypetable[i].sig == sigNew) { /* recognized signature */
ok = 0;
for (j = 0; sigtypetable[i].ttypes[j] != icMaxEnumType; j++) {
if (sigtypetable[i].ttypes[j] == p->data[k].ttype) /* recognized type */
ok = 1;
}
break;
}
}
if (!ok) {
sprintf(p->err,"icc_rename_tag: wrong signature for tag type");
p->errc = 1;
return p->errc;
}
/* change its signature */
p->data[k].sig = sigNew;
/* Track whether we have a natural 'chad' tag */
if (sig == icSigChromaticAdaptationTag)
p->naturalChad = 0;
if (sigNew == icSigChromaticAdaptationTag)
p->naturalChad = 1;
return 0;
}
/* Unread a specific tag, and free the underlying tag type data */
/* if this was the last reference to it. */
/* (This is an internal function) */
/* Returns non-zero on error: */
/* tag not found - icc->errc will contain 2 */
/* tag not read - icc->errc will contain 2 */
static int icc_unread_tag_ix(
icc *p,
unsigned int i /* Index from 0.. p->count-1 */
) {
if (i >= p->count) {
sprintf(p->err,"icc_unread_tag_ix: index %d is out of range",i);
return p->errc = 2;
}
/* See if it's been read */
if (p->data[i].objp == NULL) {
sprintf(p->err,"icc_unread_tag: Tag '%s' not currently loaded",string_TagSignature(p->data[i].sig));
return p->errc = 2;
}
if (--(p->data[i].objp->refcount) == 0) /* decrement reference count */
(p->data[i].objp->del)(p->data[i].objp); /* Last reference */
p->data[i].objp = NULL;
return 0;
}
/* Unread the tag, and free the underlying tag type */
/* if this was the last reference to it. */
/* Returns non-zero on error: */
/* tag not found - icc->errc will contain 2 */
/* tag not read - icc->errc will contain 2 */
/* NOTE: we don't handle tag duplication - you'll always get the first in the file */
static int icc_unread_tag(
icc *p,
icTagSignature sig /* Tag signature - may be unknown */
) {
unsigned int i;
/* Search for signature */
for (i = 0; i < p->count; i++) {
if (p->data[i].sig == sig) /* Found it */
break;
}
if (i >= p->count) {
sprintf(p->err,"icc_unread_tag: Tag '%s' not found",string_TagSignature(sig));
return p->errc = 2;
}
return icc_unread_tag(p, i);
}
/* Delete the tag, and free the underlying tag type, */
/* if this was the last reference to it. */
/* Returns non-zero on error: */
/* tag not found - icc->errc will contain 2 */
static int icc_delete_tag_ix(
icc *p,
unsigned int i /* Index from 0.. p->count-1 */
) {
if (i >= p->count) {
sprintf(p->err,"icc_delete_tag_ix: index %d of range",i);
return p->errc = 2;
}
/* If it's been read into memory, decrement the reference count */
if (p->data[i].objp != NULL) {
if (--(p->data[i].objp->refcount) == 0) /* decrement reference count */
(p->data[i].objp->del)(p->data[i].objp); /* Last reference */
p->data[i].objp = NULL;
}
/* Now remove it from the tag list */
for (; i < (p->count-1); i++)
p->data[i] = p->data[i+1]; /* Copy the structure down */
p->count--; /* One less tag in list */
return 0;
}
/* Delete the tag, and free the underlying tag type, */
/* if this was the last reference to it. */
/* Note this finds the first tag with a matching signature. */
/* Returns non-zero on error: */
/* tag not found - icc->errc will contain 2 */
static int icc_delete_tag(
icc *p,
icTagSignature sig /* Tag signature - may be unknown */
) {
unsigned int i;
int rv;
/* Search for signature */
for (i = 0; i < p->count; i++) {
if (p->data[i].sig == sig) /* Found it */
break;
}
if (i >= p->count) {
sprintf(p->err,"icc_delete_tag: Tag '%s' not found",string_TagSignature(sig));
return p->errc = 2;
}
rv = icc_delete_tag_ix(p, i);
/* Track whether we still have a natural 'chad' tag */
if (rv == 0) {
if (sig == icSigChromaticAdaptationTag)
p->naturalChad = 0;
}
return rv;
}
/* Read all the tags into memory, including unknown types. */
/* Returns non-zero on error. */
static int icc_read_all_tags(
icc *p
) {
unsigned int i;
for (i = 0; i < p->count; i++) { /* For all the tag element data */
if (icc_read_tag_ix(p, i, 1) == NULL)
return p->errc;
}
return 0;
}
static void icc_dump(
icc *p,
icmFile *op, /* Output to dump to */
int verb /* Verbosity level */
) {
unsigned int i;
if (verb <= 0)
return;
op->gprintf(op,"icc:\n");
/* Dump the header */
if (p->header != NULL)
p->header->dump(p->header,op,verb);
/* Dump all the tag elements */
for (i = 0; i < p->count; i++) { /* For all the tag element data */
icmBase *ob;
int tr;
op->gprintf(op,"tag %d:\n",i);
op->gprintf(op," sig %s\n",tag2str(p->data[i].sig));
op->gprintf(op," type %s\n",tag2str(p->data[i].ttype));
op->gprintf(op," offset %d\n", p->data[i].offset);
op->gprintf(op," size %d\n", p->data[i].size);
tr = 0;
if (p->data[i].objp == NULL) {
/* The object is not loaded, so load it then free it */
if (icc_read_tag_ix(p, i, 1) == NULL)
op->gprintf(op,"Unable to read: %d, %s\n",p->errc,p->err);
tr = 1;
}
if ((ob = p->data[i].objp) != NULL) {
/* op->gprintf(op," refcount %d\n", ob->refcount); */
ob->dump(ob,op,verb-1);
if (tr != 0) { /* Cleanup if temporary */
icc_unread_tag_ix(p, i);
}
}
op->gprintf(op,"\n");
}
}
static void icc_delete(
icc *p
) {
unsigned int i;
icmAlloc *al = p->al;
int del_al = p->del_al;
/* Free up the header */
if (p->header != NULL)
(p->header->del)(p->header);
/* Free up the tag data objects */
if (p->data != NULL) {
for (i = 0; i < p->count; i++) {
if (p->data[i].objp != NULL) {
if (--(p->data[i].objp->refcount) == 0) /* decrement reference count */
(p->data[i].objp->del)(p->data[i].objp); /* Last reference */
p->data[i].objp = NULL;
}
}
/* Free tag table */
al->free(al, p->data);
}
/* We are responsible for deleting the file object */
if (p->del_fp && p->fp != NULL)
p->fp->del(p->fp);
/* This object */
al->free(al, p);
if (del_al) /* We are responsible for deleting allocator */
al->del(al);
}
/* ================================================== */
/* Lut Color normalizing and de-normalizing functions */
/* As a rule, I am representing Lut in memory as values in machine form as real */
/* numbers in the range 0.0 - 1.0. For many color spaces (ie. RGB, Gray, */
/* hsv, hls, cmyk and other device coords), this is entirely appropriate. */
/* For CIE based spaces though, this is not correct, since (I assume!) the binary */
/* representation will be consistent with the encoding in Annex A, page 74 */
/* of the standard. Note that the standard doesn't specify the encoding of */
/* many color spaces (ie. Yuv, Yxy etc.), and is unclear about PCS. */
/* The following functions convert to and from the CIE base spaces */
/* and the real Lut input/output values. These are used to convert real color */
/* space values into/out of the raw lut 0.0-1.0 representation (which subsequently */
/* get converted to ICC integer values in the obvious way as a mapping to 0 .. 2^n-1). */
/* This is used internally to support the Lut->lookup() function, */
/* and can also be used by someone writing a Lut based profile to determine */
/* the colorspace range that the input lut indexes cover, as well */
/* as processing the output luts values into normalized form ready */
/* for writing. */
/* These functions should be accessed by calling icc.getNormFuncs() */
/* - - - - - - - - - - - - - - - - */
/* According to 6.5.5 and 6.5.6 of the spec., */
/* XYZ index values are represented the same as their table */
/* values, ie. as a u1.15 representation, with a value */
/* range from 0.0 -> 1.999969482422 */
/* Convert Lut index/value to XYZ coord. */
static void Lut_Lut2XYZ(double *out, double *in) {
out[0] = in[0] * (1.0 + 32767.0/32768); /* X */
out[1] = in[1] * (1.0 + 32767.0/32768); /* Y */
out[2] = in[2] * (1.0 + 32767.0/32768); /* Z */
}
/* Convert XYZ coord to Lut index/value. */
static void Lut_XYZ2Lut(double *out, double *in) {
out[0] = in[0] * (1.0/(1.0 + 32767.0/32768));
out[1] = in[1] * (1.0/(1.0 + 32767.0/32768));
out[2] = in[2] * (1.0/(1.0 + 32767.0/32768));
}
/* Convert Lut index/value to Y coord. */
static void Lut_Lut2Y(double *out, double *in) {
out[0] = in[0] * (1.0 + 32767.0/32768); /* Y */
}
/* Convert Y coord to Lut index/value. */
static void Lut_Y2Lut(double *out, double *in) {
out[0] = in[0] * (1.0/(1.0 + 32767.0/32768));
}
/* - - - - - - - - - - - - - - - - */
/* Convert 8 bit Lab to Lut numbers */
/* Annex A specifies 8 and 16 bit encoding, but is */
/* silent on the Lut index normalization. */
/* Following Michael Bourgoin's 1998 SIGGRAPH course comment on this, */
/* we assume here that the index encoding is the same as the */
/* value encoding. */
/* Convert Lut8 table index/value to Lab */
static void Lut_Lut2Lab_8(double *out, double *in) {
out[0] = in[0] * 100.0; /* L */
out[1] = (in[1] * 255.0) - 128.0; /* a */
out[2] = (in[2] * 255.0) - 128.0; /* b */
}
/* Convert Lab to Lut8 table index/value */
static void Lut_Lab2Lut_8(double *out, double *in) {
out[0] = in[0] * 1.0/100.0; /* L */
out[1] = (in[1] + 128.0) * 1.0/255.0; /* a */
out[2] = (in[2] + 128.0) * 1.0/255.0; /* b */
}
/* Convert Lut8 table index/value to L */
static void Lut_Lut2L_8(double *out, double *in) {
out[0] = in[0] * 100.0; /* L */
}
/* Convert L to Lut8 table index/value */
static void Lut_L2Lut_8(double *out, double *in) {
out[0] = in[0] * 1.0/100.0; /* L */
}
/* - - - - - - - - - - - - - - - - */
/* Convert 16 bit Lab to Lut numbers, V2 */
/* Convert Lut16 table index/value to Lab */
static void Lut_Lut2LabV2_16(double *out, double *in) {
out[0] = in[0] * (100.0 * 65535.0)/65280.0; /* L */
out[1] = (in[1] * (255.0 * 65535.0)/65280) - 128.0; /* a */
out[2] = (in[2] * (255.0 * 65535.0)/65280) - 128.0; /* b */
}
/* Convert Lab to Lut16 table index/value */
static void Lut_Lab2LutV2_16(double *out, double *in) {
out[0] = in[0] * 65280.0/(100.0 * 65535.0); /* L */
out[1] = (in[1] + 128.0) * 65280.0/(255.0 * 65535.0); /* a */
out[2] = (in[2] + 128.0) * 65280.0/(255.0 * 65535.0); /* b */
}
/* Convert Lut16 table index/value to L */
static void Lut_Lut2LV2_16(double *out, double *in) {
out[0] = in[0] * (100.0 * 65535.0)/65280.0; /* L */
}
/* Convert Lab to Lut16 table index/value */
static void Lut_L2LutV2_16(double *out, double *in) {
out[0] = in[0] * 65280.0/(100.0 * 65535.0); /* L */
}
/* - - - - - - - - - - - - - - - - */
/* Convert 16 bit Lab to Lut numbers, V4 */
/* Convert Lut16 table index/value to Lab */
static void Lut_Lut2LabV4_16(double *out, double *in) {
out[0] = in[0] * 100.0; /* L */
out[1] = (in[1] * 255.0) - 128.0; /* a */
out[2] = (in[2] * 255.0) - 128.0; /* b */
}
/* Convert Lab to Lut16 table index/value */
static void Lut_Lab2LutV4_16(double *out, double *in) {
out[0] = in[0] * 1.0/100.0; /* L */
out[1] = (in[1] + 128.0) * 1.0/255.0; /* a */
out[2] = (in[2] + 128.0) * 1.0/255.0; /* b */
}
/* Convert Lut16 table index/value to L */
static void Lut_Lut2LV4_16(double *out, double *in) {
out[0] = in[0] * 100.0; /* L */
}
/* Convert L to Lut16 table index/value */
static void Lut_L2LutV4_16(double *out, double *in) {
out[0] = in[0] * 1.0/100.0; /* L */
}
/* - - - - - - - - - - - - - - - - */
/* Convert Luv to Lut number */
/* This data normalization is taken from Apples */
/* Colorsync specification. */
/* As per other color spaces, we assume that the index */
/* normalization is the same as the data normalization. */
/* Convert Lut table index/value to Luv */
static void Lut_Lut2Luv(double *out, double *in) {
out[0] = in[0] * 100.0; /* L */
out[1] = (in[1] * 65535.0/256.0) - 128.0; /* u */
out[2] = (in[2] * 65535.0/256.0) - 128.0; /* v */
}
/* Convert Luv to Lut table index/value */
static void Lut_Luv2Lut(double *out, double *in) {
out[0] = in[0] * 1.0/100.0; /* L */
out[1] = (in[1] + 128.0) * 256.0/65535.0; /* u */
out[2] = (in[2] + 128.0) * 256.0/65535.0; /* v */
}
/* - - - - - - - - - - - - - - - - */
/* Convert YCbCr to Lut number */
/* We are assuming full range here. foot/head scaling */
/* should be done outside the icc profile. */
/* Convert Lut table index/value to YCbCr */
static void Lut_Lut2YCbCr(double *out, double *in) {
out[0] = in[0]; /* Y */
out[1] = in[1] - 0.5; /* Cb */
out[2] = in[2] - 0.5; /* Cr */
}
/* Convert YCbCr to Lut table index/value */
static void Lut_YCbCr2Lut(double *out, double *in) {
out[0] = in[0]; /* Y */
out[1] = in[1] + 0.5; /* Cb */
out[2] = in[2] + 0.5; /* Cr */
}
/* - - - - - - - - - - - - - - - - */
/* Default N component conversions */
static void Lut_N(double *out, double *in, int nc) {
for (--nc; nc >= 0; nc--)
out[nc] = in[nc];
}
/* 1 */
static void Lut_1(double *out, double *in) {
out[0] = in[0];
}
/* 2 */
static void Lut_2(double *out, double *in) {
out[0] = in[0];
out[1] = in[1];
}
/* 3 */
static void Lut_3(double *out, double *in) {
out[0] = in[0];
out[1] = in[1];
out[2] = in[2];
}
/* 4 */
static void Lut_4(double *out, double *in) {
out[0] = in[0];
out[1] = in[1];
out[2] = in[2];
out[3] = in[3];
}
/* 5 */
static void Lut_5(double *out, double *in) {
out[0] = in[0];
out[1] = in[1];
out[2] = in[2];
out[3] = in[3];
out[4] = in[4];
}
/* 6 */
static void Lut_6(double *out, double *in) {
out[0] = in[0];
out[1] = in[1];
out[2] = in[2];
out[3] = in[3];
out[4] = in[4];
out[5] = in[5];
}
/* 7 */
static void Lut_7(double *out, double *in) {
Lut_N(out, in, 7);
}
/* 8 */
static void Lut_8(double *out, double *in) {
Lut_N(out, in, 8);
}
/* 9 */
static void Lut_9(double *out, double *in) {
Lut_N(out, in, 9);
}
/* 10 */
static void Lut_10(double *out, double *in) {
Lut_N(out, in, 10);
}
/* 11 */
static void Lut_11(double *out, double *in) {
Lut_N(out, in, 11);
}
/* 12 */
static void Lut_12(double *out, double *in) {
Lut_N(out, in, 12);
}
/* 13 */
static void Lut_13(double *out, double *in) {
Lut_N(out, in, 13);
}
/* 14 */
static void Lut_14(double *out, double *in) {
Lut_N(out, in, 14);
}
/* 15 */
static void Lut_15(double *out, double *in) {
Lut_N(out, in, 15);
}
/* Function table - match conversions to color spaces. */
/* Anything not here, we don't know how to convert. */
static struct {
icColorSpaceSignature csig;
void (*fromLut)(double *out, double *in); /* from Lut index/entry */
void (*toLut)(double *out, double *in); /* to Lut index/entry */
} colnormtable[] = {
{icSigXYZData, Lut_Lut2XYZ, Lut_XYZ2Lut },
{icmSigYData, Lut_Lut2Y, Lut_Y2Lut },
{icmSigLab8Data, Lut_Lut2Lab_8, Lut_Lab2Lut_8 },
{icmSigLabV2Data, Lut_Lut2LabV2_16, Lut_Lab2LutV2_16 },
{icmSigLabV4Data, Lut_Lut2LabV4_16, Lut_Lab2LutV4_16 },
{icmSigL8Data, Lut_Lut2L_8, Lut_L2Lut_8 },
{icmSigLV2Data, Lut_Lut2LV2_16, Lut_L2LutV2_16 },
{icmSigLV4Data, Lut_Lut2LV4_16, Lut_L2LutV4_16 },
{icSigLuvData, Lut_Lut2Luv, Lut_Luv2Lut },
{icSigYCbCrData, Lut_Lut2YCbCr, Lut_YCbCr2Lut },
{icSigYxyData, Lut_3, Lut_3 },
{icSigRgbData, Lut_3, Lut_3 },
{icSigGrayData, Lut_1, Lut_1 },
{icSigHsvData, Lut_3, Lut_3 },
{icSigHlsData, Lut_3, Lut_3 },
{icSigCmykData, Lut_4, Lut_4 },
{icSigCmyData, Lut_3, Lut_3 },
{icSigMch6Data, Lut_6, Lut_6 },
{icSig2colorData, Lut_2, Lut_2 },
{icSig3colorData, Lut_3, Lut_3 },
{icSig4colorData, Lut_4, Lut_4 },
{icSig5colorData, Lut_5, Lut_5 },
{icSig6colorData, Lut_6, Lut_6 },
{icSig7colorData, Lut_7, Lut_7 },
{icSigMch5Data, Lut_5, Lut_5 },
{icSigMch6Data, Lut_6, Lut_6 },
{icSigMch7Data, Lut_7, Lut_7 },
{icSigMch8Data, Lut_8, Lut_8 },
{icSig8colorData, Lut_8, Lut_8 },
{icSig9colorData, Lut_9, Lut_9 },
{icSig10colorData, Lut_10, Lut_10 },
{icSig11colorData, Lut_11, Lut_11 },
{icSig12colorData, Lut_12, Lut_12 },
{icSig13colorData, Lut_13, Lut_13 },
{icSig14colorData, Lut_14, Lut_14 },
{icSig15colorData, Lut_15, Lut_15 },
{icMaxEnumData, NULL, NULL }
};
/*
Legacy Lab encoding:
The V4 specificatins are misleading on this, since they assume in this
instance that all devices spaces, however labeled, have no defined
ICC encoding. The end result is simple enough though:
ICC V2 Lab encoding should be used in all PCS encodings in
a icSigLut16Type or icSigNamedColor2Type tag, and can be used
for device space Lab encoding for these tags.
ICC V4 Lab encoding should be used in all PCS encodings in
all other situations, and can be used for device space Lab encoding
for all other situtaions.
[ Since the ICC spec. doesn't cover device spaces labeled as Lab,
these are ripe for mis-matches between different implementations.]
This logic has yet to be fully implemented here.
*/
/* Find appropriate conversion functions from the normalised */
/* Lut data range 0.0 - 1.0 to/from a given colorspace value, */
/* given the color space and Lut type. */
/* Return 0 on success, 1 on match failure. */
/* NOTE: doesn't set error value, message etc.! */
static int getNormFunc(
icc *icp,
// icProfileClassSignature psig, /* Profile signature to use */
icColorSpaceSignature csig, /* Colorspace to use. */
// int lutin, /* 0 if this is for a icSigLut16Type input, nz for output */
// icTagSignature tagSig, /* Tag signature involved (AtoB or B2A etc.) */
icTagTypeSignature tagType, /* icSigLut8Type or icSigLut16Type or V4 lut */
icmNormFlag flag, /* icmFromLuti, icmFromLutv or icmToLuti, icmToLutv */
void (**nfunc)(double *out, double *in)
) {
int i;
if (tagType == icSigLut8Type && csig == icSigLabData) {
csig = icmSigLab8Data;
}
if (csig == icSigLabData) {
if (tagType == icSigLut16Type) /* Lut16 retains legacy encoding */
csig = icmSigLabV2Data;
else { /* Other tag types use version specific encoding */
if (icp->ver >= icmVersion4_1)
csig = icmSigLabV4Data;
else
csig = icmSigLabV2Data;
}
}
for (i = 0; colnormtable[i].csig != icMaxEnumData; i++) {
if (colnormtable[i].csig == csig)
break; /* Found it */
}
if (colnormtable[i].csig == icMaxEnumData) { /* Oops */
*nfunc = NULL;
return 1;
}
if (flag == icmFromLuti || flag == icmFromLutv) { /* Table index/value decoding functions */
*nfunc = colnormtable[i].fromLut;
return 0;
} else if (flag == icmToLuti || flag == icmToLutv) { /* Table index/value encoding functions */
*nfunc = colnormtable[i].toLut;
return 0;
}
*nfunc = NULL;
return 1;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - */
/* Colorspace ranges - used instead of norm/denorm by Mono, Matrix and */
/* override PCS */
/* Function table - match ranges to color spaces. */
/* Anything not here, we don't know how to convert. */
/* Hmm. we're not handling Lab8 properly ?? ~~~8888 */
static struct {
icColorSpaceSignature csig;
int same; /* Non zero if first entry applies to all channels */
double min[3]; /* Minimum value for this colorspace */
double max[3]; /* Maximum value for this colorspace */
} colorrangetable[] = {
{icSigXYZData, 1, { 0.0 } , { 1.0 + 32767.0/32768.0 } },
{icmSigLab8Data, 0, { 0.0, -128.0, -128.0 }, { 100.0, 127.0, 127.0 } },
{icmSigLabV2Data, 0, { 0.0, -128.0, -128.0 },
{ 100.0 + 25500.0/65280.0, 127.0 + 255.0/256.0, 127.0 + 255.0/256.0 } },
{icmSigLabV4Data, 0, { 0.0, -128.0, -128.0 }, { 100.0, 127.0, 127.0 } },
{icmSigYData, 1, { 0.0 }, { 1.0 + 32767.0/32768.0 } },
{icmSigL8Data, 1, { 0.0 }, { 100.0 } },
{icmSigLV2Data, 1, { 0.0 }, { 100.0 + 25500.0/65280.0 } },
{icmSigLV4Data, 1, { 0.0 }, { 100.0 } },
{icSigLuvData, 0, { 0.0, -128.0, -128.0 },
{ 100.0, 127.0 + 255.0/256.0, 127.0 + 255.0/256.0 } },
{icSigYCbCrData, 0, { 0.0, -0.5, -0.5 }, { 1.0, 0.5, 0.5 } }, /* Full range */
{icSigYxyData, 1, { 0.0 }, { 1.0 } }, /* ??? */
{icSigRgbData, 1, { 0.0 }, { 1.0 } },
{icSigGrayData, 1, { 0.0 }, { 1.0 } },
{icSigHsvData, 1, { 0.0 }, { 1.0 } },
{icSigHlsData, 1, { 0.0 }, { 1.0 } },
{icSigCmykData, 1, { 0.0 }, { 1.0 } },
{icSigCmyData, 1, { 0.0 }, { 1.0 } },
{icSigMch6Data, 1, { 0.0 }, { 1.0 } },
{icSig2colorData, 1, { 0.0 }, { 1.0 } },
{icSig3colorData, 1, { 0.0 }, { 1.0 } },
{icSig4colorData, 1, { 0.0 }, { 1.0 } },
{icSig5colorData, 1, { 0.0 }, { 1.0 } },
{icSig6colorData, 1, { 0.0 }, { 1.0 } },
{icSig7colorData, 1, { 0.0 }, { 1.0 } },
{icSig8colorData, 1, { 0.0 }, { 1.0 } },
{icSigMch5Data, 1, { 0.0 }, { 1.0 } },
{icSigMch6Data, 1, { 0.0 }, { 1.0 } },
{icSigMch7Data, 1, { 0.0 }, { 1.0 } },
{icSigMch8Data, 1, { 0.0 }, { 1.0 } },
{icSig9colorData, 1, { 0.0 }, { 1.0 } },
{icSig10colorData, 1, { 0.0 }, { 1.0 } },
{icSig11colorData, 1, { 0.0 }, { 1.0 } },
{icSig12colorData, 1, { 0.0 }, { 1.0 } },
{icSig13colorData, 1, { 0.0 }, { 1.0 } },
{icSig14colorData, 1, { 0.0 }, { 1.0 } },
{icSig15colorData, 1, { 0.0 }, { 1.0 } },
{icMaxEnumData, 1, { 0.0 }, { 1.0 } }
};
/* Find appropriate typical encoding ranges for a */
/* colorspace given the color space. */
/* Return 0 on success, 1 on match failure */
static int getRange(
icc *icp,
// icProfileClassSignature psig, /* Profile signature to use */
icColorSpaceSignature csig, /* Colorspace to use. */
// int lutin, /* 0 if this is for a icSigLut16Type input, nz for output */
// icTagSignature tagSig, /* Tag signature involved (AtoB or B2A etc.) */
icTagTypeSignature tagType, /* icSigLut8Type or icSigLut16Type or V4 lut */
double *min, double *max /* Return range values */
) {
int i, e, ee;
if (tagType == icSigLut8Type && csig == icSigLabData) {
csig = icmSigLab8Data;
}
if (csig == icSigLabData) {
if (tagType == icSigLut16Type) /* Lut16 retains legacy encoding */
csig = icmSigLabV2Data;
else { /* Other tag types use version specific encoding */
if (icp->ver >= icmVersion4_1)
csig = icmSigLabV4Data;
else
csig = icmSigLabV2Data;
}
}
for (i = 0; colorrangetable[i].csig != icMaxEnumData; i++) {
if (colorrangetable[i].csig == csig)
break; /* Found it */
}
if (colorrangetable[i].csig == icMaxEnumData) { /* Oops */
return 1;
}
ee = number_ColorSpaceSignature(csig); /* Get number of components */
if (colorrangetable[i].same) { /* All channels are the same */
for (e = 0; e < ee; e++) {
if (min != NULL)
min[e] = colorrangetable[i].min[0];
if (max != NULL)
max[e] = colorrangetable[i].max[0];
}
} else {
for (e = 0; e < ee; e++) {
if (min != NULL)
min[e] = colorrangetable[i].min[e];
if (max != NULL)
max[e] = colorrangetable[i].max[e];
}
}
return 0;
}
/* =============================================================== */
/* Misc. support functions. */
/* Clamp a 3 vector to be +ve */
void icmClamp3(double out[3], double in[3]) {
int i;
for (i = 0; i < 3; i++)
out[i] = in[i] < 0.0 ? 0.0 : in[i];
}
/* Invert (negate) a 3 vector */
void icmInv3(double out[3], double in[3]) {
int i;
for (i = 0; i < 3; i++)
out[i] = -in[i];
}
/* Add two 3 vectors */
void icmAdd3(double out[3], double in1[3], double in2[3]) {
out[0] = in1[0] + in2[0];
out[1] = in1[1] + in2[1];
out[2] = in1[2] + in2[2];
}
/* Subtract two 3 vectors, out = in1 - in2 */
void icmSub3(double out[3], double in1[3], double in2[3]) {
out[0] = in1[0] - in2[0];
out[1] = in1[1] - in2[1];
out[2] = in1[2] - in2[2];
}
/* Divide two 3 vectors, out = in1/in2 */
void icmDiv3(double out[3], double in1[3], double in2[3]) {
out[0] = in1[0]/in2[0];
out[1] = in1[1]/in2[1];
out[2] = in1[2]/in2[2];
}
/* Multiply two 3 vectors, out = in1 * in2 */
void icmMul3(double out[3], double in1[3], double in2[3]) {
out[0] = in1[0] * in2[0];
out[1] = in1[1] * in2[1];
out[2] = in1[2] * in2[2];
}
/* Take values to power */
void icmPow3(double out[3], double in[3], double p) {
int i;
for (i = 0; i < 3; i++) {
if (in[i] < 0.0)
out[i] = -pow(-in[i], p);
else
out[i] = pow(in[i], p);
}
}
/* Take absolute of a 3 vector */
void icmAbs3(double out[3], double in[3]) {
out[0] = fabs(in[0]);
out[1] = fabs(in[1]);
out[2] = fabs(in[2]);
}
/* - - - - - - - - - - - - - - - - - - - - - - - - */
/* Set a 3x3 matrix to a value */
void icmSetVal3x3(double mat[3][3], double val) {
int i, j;
for (j = 0; j < 3; j++) {
for (i = 0; i < 3; i++) {
mat[j][i] = val;
}
}
}
/* Set a 3x3 matrix to unity */
void icmSetUnity3x3(double mat[3][3]) {
int i, j;
for (j = 0; j < 3; j++) {
for (i = 0; i < 3; i++) {
if (i == j)
mat[j][i] = 1.0;
else
mat[j][i] = 0.0;
}
}
}
/* Copy a 3x3 transform matrix */
void icmCpy3x3(double dst[3][3], double src[3][3]) {
int i, j;
for (j = 0; j < 3; j++) {
for (i = 0; i < 3; i++)
dst[j][i] = src[j][i];
}
}
/* Add one 3x3 to another */
/* dst = src1 + src2 */
void icmAdd3x3(double dst[3][3], double src1[3][3], double src2[3][3]) {
int i, j;
for (j = 0; j < 3; j++) {
for (i = 0; i < 3; i++)
dst[j][i] = src1[j][i] + src2[j][i];
}
}
/* Scale each element of a 3x3 transform matrix */
void icmScale3x3(double dst[3][3], double src[3][3], double scale) {
int i, j;
for (j = 0; j < 3; j++) {
for (i = 0; i < 3; i++)
dst[j][i] = src[j][i] * scale;
}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - */
/*
mat in out
[ ] [] []
[ ] * [] => []
[ ] [] []
*/
/* Multiply 3 array by 3x3 transform matrix */
void icmMulBy3x3(double out[3], double mat[3][3], double in[3]) {
double tt[3];
tt[0] = mat[0][0] * in[0] + mat[0][1] * in[1] + mat[0][2] * in[2];
tt[1] = mat[1][0] * in[0] + mat[1][1] * in[1] + mat[1][2] * in[2];
tt[2] = mat[2][0] * in[0] + mat[2][1] * in[1] + mat[2][2] * in[2];
out[0] = tt[0];
out[1] = tt[1];
out[2] = tt[2];
}
/* - - - - - - - - - - - - - - - - - - - - - - - - */
/* - - - - - - - - - - - - - - - - - - - - - - - - */
/* Tensor product. Multiply two 3 vectors to form a 3x3 matrix */
/* src1[] forms the colums, and src2[] forms the rows in the result */
void icmTensMul3(double dst[3][3], double src1[3], double src2[3]) {
int i, j;
for (j = 0; j < 3; j++) {
for (i = 0; i < 3; i++)
dst[j][i] = src1[j] * src2[i];
}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - */
/* Multiply one 3x3 with another */
/* dst = src * dst */
void icmMul3x3(double dst[3][3], double src[3][3]) {
int i, j, k;
double td[3][3]; /* Temporary dest */
for (j = 0; j < 3; j++) {
for (i = 0; i < 3; i++) {
double tt = 0.0;
for (k = 0; k < 3; k++)
tt += src[j][k] * dst[k][i];
td[j][i] = tt;
}
}
/* Copy result out */
for (j = 0; j < 3; j++)
for (i = 0; i < 3; i++)
dst[j][i] = td[j][i];
}
/* Multiply one 3x3 with another #2 */
/* dst = src1 * src2 */
void icmMul3x3_2(double dst[3][3], double src1[3][3], double src2[3][3]) {
int i, j, k;
double td[3][3]; /* Temporary dest */
for (j = 0; j < 3; j++) {
for (i = 0; i < 3; i++) {
double tt = 0.0;
for (k = 0; k < 3; k++)
tt += src1[j][k] * src2[k][i];
td[j][i] = tt;
}
}
/* Copy result out */
for (j = 0; j < 3; j++)
for (i = 0; i < 3; i++)
dst[j][i] = td[j][i];
}
/* - - - - - - - - - - - - - - - - - - - - - - - - */
/*
Matrix Inversion
by Richard Carling
from "Graphics Gems", Academic Press, 1990
*/
/*
* adjoint( original_matrix, inverse_matrix )
*
* calculate the adjoint of a 3x3 matrix
*
* Let a denote the minor determinant of matrix A obtained by
* ij
*
* deleting the ith row and jth column from A.
*
* i+j
* Let b = (-1) a
* ij ji
*
* The matrix B = (b ) is the adjoint of A
* ij
*/
#define det2x2(a, b, c, d) (a * d - b * c)
static void adjoint(
double out[3][3],
double in[3][3]
) {
double a1, a2, a3, b1, b2, b3, c1, c2, c3;
/* assign to individual variable names to aid */
/* selecting correct values */
a1 = in[0][0]; b1 = in[0][1]; c1 = in[0][2];
a2 = in[1][0]; b2 = in[1][1]; c2 = in[1][2];
a3 = in[2][0]; b3 = in[2][1]; c3 = in[2][2];
/* row column labeling reversed since we transpose rows & columns */
out[0][0] = det2x2(b2, b3, c2, c3);
out[1][0] = - det2x2(a2, a3, c2, c3);
out[2][0] = det2x2(a2, a3, b2, b3);
out[0][1] = - det2x2(b1, b3, c1, c3);
out[1][1] = det2x2(a1, a3, c1, c3);
out[2][1] = - det2x2(a1, a3, b1, b3);
out[0][2] = det2x2(b1, b2, c1, c2);
out[1][2] = - det2x2(a1, a2, c1, c2);
out[2][2] = det2x2(a1, a2, b1, b2);
}
/*
* double = icmDet3x3( a1, a2, a3, b1, b2, b3, c1, c2, c3 )
*
* calculate the determinant of a 3x3 matrix
* in the form
*
* | a1, b1, c1 |
* | a2, b2, c2 |
* | a3, b3, c3 |
*/
double icmDet3x3(double in[3][3]) {
double a1, a2, a3, b1, b2, b3, c1, c2, c3;
double ans;
a1 = in[0][0]; b1 = in[0][1]; c1 = in[0][2];
a2 = in[1][0]; b2 = in[1][1]; c2 = in[1][2];
a3 = in[2][0]; b3 = in[2][1]; c3 = in[2][2];
ans = a1 * det2x2(b2, b3, c2, c3)
- b1 * det2x2(a2, a3, c2, c3)
+ c1 * det2x2(a2, a3, b2, b3);
return ans;
}
#define ICM_SMALL_NUMBER 1.e-8
/*
* inverse( original_matrix, inverse_matrix )
*
* calculate the inverse of a 4x4 matrix
*
* -1
* A = ___1__ adjoint A
* det A
*/
/* Return non-zero if not invertable */
int icmInverse3x3(
double out[3][3],
double in[3][3]
) {
int i, j;
double det;
/* calculate the 3x3 determinant
* if the determinant is zero,
* then the inverse matrix is not unique.
*/
det = icmDet3x3(in);
if (fabs(det) < ICM_SMALL_NUMBER)
return 1;
/* calculate the adjoint matrix */
adjoint(out, in);
/* scale the adjoint matrix to get the inverse */
for (i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
out[i][j] /= det;
return 0;
}
/* Invert a 2x2 transform matrix. Return 1 if error. */
int icmInverse2x2(double out[2][2], double in[2][2]) {
double det = det2x2(in[0][0], in[0][1], in[1][0], in[1][1]);
if (fabs(det) < ICM_SMALL_NUMBER)
return 1;
det = 1.0/det;
out[0][0] = det * in[1][1];
out[0][1] = det * -in[0][1];
out[1][0] = det * -in[1][0];
out[1][1] = det * in[0][0];
return 0;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - */
/* Transpose a 3x3 matrix */
void icmTranspose3x3(double out[3][3], double in[3][3]) {
int i, j;
if (out != in) {
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
out[i][j] = in[j][i];
} else {
double tt[3][3];
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
tt[i][j] = in[j][i];
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
out[i][j] = tt[i][j];
}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - */
/* Compute the dot product of two 3 vectors */
double icmDot3(double in1[3], double in2[3]) {
return in1[0] * in2[0] + in1[1] * in2[1] + in1[2] * in2[2];
}
/* - - - - - - - - - - - - - - - - - - - - - - - - */
/* Compute the cross product of two 3D vectors, out = in1 x in2 */
void icmCross3(double out[3], double in1[3], double in2[3]) {
double tt[3];
tt[0] = (in1[1] * in2[2]) - (in1[2] * in2[1]);
tt[1] = (in1[2] * in2[0]) - (in1[0] * in2[2]);
tt[2] = (in1[0] * in2[1]) - (in1[1] * in2[0]);
out[0] = tt[0];
out[1] = tt[1];
out[2] = tt[2];
}
/* - - - - - - - - - - - - - - - - - - - - - - - - */
/* Compute the norm (length) squared of a 3 vector */
double icmNorm3sq(double in[3]) {
return in[0] * in[0] + in[1] * in[1] + in[2] * in[2];
}
/* Compute the norm (length) of a 3 vector */
double icmNorm3(double in[3]) {
return sqrt(in[0] * in[0] + in[1] * in[1] + in[2] * in[2]);
}
/* Scale a 3 vector by the given ratio */
void icmScale3(double out[3], double in[3], double rat) {
out[0] = in[0] * rat;
out[1] = in[1] * rat;
out[2] = in[2] * rat;
}
/* Scale a 3 vector by the given ratio and add it */
void icmScaleAdd3(double out[3], double in2[3], double in1[3], double rat) {
out[0] = in2[0] + in1[0] * rat;
out[1] = in2[1] + in1[1] * rat;
out[2] = in2[2] + in1[2] * rat;
}
/* Compute a blend between in0 and in1 */
void icmBlend3(double out[3], double in0[3], double in1[3], double bf) {
out[0] = (1.0 - bf) * in0[0] + bf * in1[0];
out[1] = (1.0 - bf) * in0[1] + bf * in1[1];
out[2] = (1.0 - bf) * in0[2] + bf * in1[2];
}
/* Clip a vector to the range 0.0 .. 1.0 */
void icmClip3(double out[3], double in[3]) {
int j;
for (j = 0; j < 3; j++) {
out[j] = in[j];
if (out[j] < 0.0)
out[j] = 0.0;
else if (out[j] > 1.0)
out[j] = 1.0;
}
}
/* Clip a vector to the range 0.0 .. 1.0 */
/* and retun nz if clipping occured */
int icmClip3sig(double out[3], double in[3]) {
int j;
int clip = 0;
for (j = 0; j < 3; j++) {
out[j] = in[j];
if (out[j] < 0.0) {
out[j] = 0.0;
clip = 1;
} else if (out[j] > 1.0) {
out[j] = 1.0;
clip = 1;
}
}
return clip;
}
/* Clip a vector to the range 0.0 .. 1.0 */
/* and return any clipping margine */
double icmClip3marg(double out[3], double in[3]) {
int j;
double tt, marg = 0.0;
for (j = 0; j < 3; j++) {
out[j] = in[j];
if (out[j] < 0.0) {
tt = 0.0 - out[j];
out[j] = 0.0;
if (tt > marg)
marg = tt;
} else if (out[j] > 1.0) {
tt = out[j] - 1.0;
out[j] = 1.0;
if (tt > marg)
marg = tt;
}
}
return marg;
}
/* Normalise a 3 vector to the given length. Return nz if not normalisable */
int icmNormalize3(double out[3], double in[3], double len) {
double tt = sqrt(in[0] * in[0] + in[1] * in[1] + in[2] * in[2]);
if (tt < ICM_SMALL_NUMBER)
return 1;
tt = len/tt;
out[0] = in[0] * tt;
out[1] = in[1] * tt;
out[2] = in[2] * tt;
return 0;
}
/* Compute the norm (length) squared of a vector define by two points */
double icmNorm33sq(double in1[3], double in0[3]) {
int j;
double rv;
for (rv = 0.0, j = 0; j < 3; j++) {
double tt = in1[j] - in0[j];
rv += tt * tt;
}
return rv;
}
/* Compute the norm (length) of a vector define by two points */
double icmNorm33(double in1[3], double in0[3]) {
int j;
double rv;
for (rv = 0.0, j = 0; j < 3; j++) {
double tt = in1[j] - in0[j];
rv += tt * tt;
}
return sqrt(rv);
}
/* Scale a two point vector by the given ratio. in0[] is the origin */
void icmScale33(double out[3], double in1[3], double in0[3], double rat) {
out[0] = in0[0] + (in1[0] - in0[0]) * rat;
out[1] = in0[1] + (in1[1] - in0[1]) * rat;
out[2] = in0[2] + (in1[2] - in0[2]) * rat;
}
/* Normalise a vector from 0->1 to the given length. */
/* The new location of in1[] is returned in out[]. */
/* Return nz if not normalisable */
int icmNormalize33(double out[3], double in1[3], double in0[3], double len) {
int j;
double vl;
for (vl = 0.0, j = 0; j < 3; j++) {
double tt = in1[j] - in0[j];
vl += tt * tt;
}
vl = sqrt(vl);
if (vl < ICM_SMALL_NUMBER)
return 1;
vl = len/vl;
for (j = 0; j < 3; j++) {
out[j] = in0[j] + (in1[j] - in0[j]) * vl;
}
return 0;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - */
/* Given two 3D points, create a matrix that rotates */
/* and scales one onto the other about the origin 0,0,0. */
/* The maths is from page 52 of Tomas Moller and Eric Haines "Real-Time Rendering". */
/* s is source vector, t is target vector. */
/* Usage of icmRotMat: */
/* t[0] == mat[0][0] * s[0] + mat[0][1] * s[1] + mat[0][2] * s[2]; */
/* t[1] == mat[1][0] * s[0] + mat[1][1] * s[1] + mat[1][2] * s[2]; */
/* t[2] == mat[2][0] * s[0] + mat[2][1] * s[1] + mat[2][2] * s[2]; */
/* i.e. use icmMulBy3x3 */
void icmRotMat(double m[3][3], double s[3], double t[3]) {
double sl, tl, sn[3], tn[3];
double v[3]; /* Cross product */
double e; /* Dot product */
double h; /* 1-e/Cross product dot squared */
/* Normalise input vectors */
/* The rotation will be about 0,0,0 */
sl = sqrt(s[0] * s[0] + s[1] * s[1] + s[2] * s[2]);
tl = sqrt(t[0] * t[0] + t[1] * t[1] + t[2] * t[2]);
if (sl < 1e-12 || tl < 1e-12) { /* Hmm. Do nothing */
m[0][0] = 1.0;
m[0][1] = 0.0;
m[0][2] = 0.0;
m[1][0] = 0.0;
m[1][1] = 1.0;
m[1][2] = 0.0;
m[2][0] = 0.0;
m[2][1] = 0.0;
m[2][2] = 1.0;
return;
}
sn[0] = s[0]/sl; sn[1] = s[1]/sl; sn[2] = s[2]/sl;
tn[0] = t[0]/tl; tn[1] = t[1]/tl; tn[2] = t[2]/tl;
/* Compute the cross product */
v[0] = (sn[1] * tn[2]) - (sn[2] * tn[1]);
v[1] = (sn[2] * tn[0]) - (sn[0] * tn[2]);
v[2] = (sn[0] * tn[1]) - (sn[1] * tn[0]);
/* Compute the dot product */
e = sn[0] * tn[0] + sn[1] * tn[1] + sn[2] * tn[2];
/* Cross product dot squared */
h = v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
/* If the two input vectors are close to being parallel, */
/* then h will be close to zero. */
if (fabs(h) < 1e-12) {
/* Make sure scale is the correct sign */
if (s[0] * t[0] + s[1] * t[1] + s[2] * t[2] < 0.0)
tl = -tl;
m[0][0] = tl/sl;
m[0][1] = 0.0;
m[0][2] = 0.0;
m[1][0] = 0.0;
m[1][1] = tl/sl;
m[1][2] = 0.0;
m[2][0] = 0.0;
m[2][1] = 0.0;
m[2][2] = tl/sl;
} else {
/* 1-e/Cross product dot squared */
h = (1.0 - e) / h;
m[0][0] = tl/sl * (e + h * v[0] * v[0]);
m[0][1] = tl/sl * (h * v[0] * v[1] - v[2]);
m[0][2] = tl/sl * (h * v[0] * v[2] + v[1]);
m[1][0] = tl/sl * (h * v[0] * v[1] + v[2]);
m[1][1] = tl/sl * (e + h * v[1] * v[1]);
m[1][2] = tl/sl * (h * v[1] * v[2] - v[0]);
m[2][0] = tl/sl * (h * v[0] * v[2] - v[1]);
m[2][1] = tl/sl * (h * v[1] * v[2] + v[0]);
m[2][2] = tl/sl * (e + h * v[2] * v[2]);
}
#ifdef NEVER /* Check result */
{
double tt[3];
icmMulBy3x3(tt, m, s);
if (icmLabDEsq(t, tt) > 1e-4) {
printf("icmRotMat error t, is %f %f %f\n",tt[0],tt[1],tt[2]);
printf(" should be %f %f %f\n",t[0],t[1],t[2]);
}
}
#endif /* NEVER */
}
/* - - - - - - - - - - - - - - - - - - - - - - - - */
/*
mat in out
[ ] [] []
[ ] * [] => []
[ ] [] []
[ ] [] []
*/
/* Multiply 4 array by 4x4 transform matrix */
void icmMulBy4x4(double out[4], double mat[4][4], double in[4]) {
double tt[4];
tt[0] = mat[0][0] * in[0] + mat[0][1] * in[1] + mat[0][2] * in[2] + mat[0][3] * in[3];
tt[1] = mat[1][0] * in[0] + mat[1][1] * in[1] + mat[1][2] * in[2] + mat[1][3] * in[3];
tt[2] = mat[2][0] * in[0] + mat[2][1] * in[1] + mat[2][2] * in[2] + mat[2][3] * in[3];
tt[3] = mat[3][0] * in[0] + mat[3][1] * in[1] + mat[3][2] * in[2] + mat[3][3] * in[3];
out[0] = tt[0];
out[1] = tt[1];
out[2] = tt[2];
out[3] = tt[3];
}
/* Transpose a 4x4 matrix */
void icmTranspose4x4(double out[4][4], double in[4][4]) {
int i, j;
if (out != in) {
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
out[i][j] = in[j][i];
} else {
double tt[4][4];
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
tt[i][j] = in[j][i];
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
out[i][j] = tt[i][j];
}
}
/* Clip a vector to the range 0.0 .. 1.0 */
/* and return any clipping margine */
double icmClip4marg(double out[4], double in[4]) {
int j;
double tt, marg = 0.0;
for (j = 0; j < 4; j++) {
out[j] = in[j];
if (out[j] < 0.0) {
tt = 0.0 - out[j];
out[j] = 0.0;
if (tt > marg)
marg = tt;
} else if (out[j] > 1.0) {
tt = out[j] - 1.0;
out[j] = 1.0;
if (tt > marg)
marg = tt;
}
}
return marg;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - */
/* Copy a 3x4 transform matrix */
void icmCpy3x4(double dst[3][4], double src[3][4]) {
int i, j;
for (j = 0; j < 3; j++) {
for (i = 0; i < 4; i++)
dst[j][i] = src[j][i];
}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - */
/* Multiply 3 array by 3x4 transform matrix */
void icmMul3By3x4(double out[3], double mat[3][4], double in[3]) {
double tt[3];
tt[0] = mat[0][0] * in[0] + mat[0][1] * in[1] + mat[0][2] * in[2] + mat[0][3];
tt[1] = mat[1][0] * in[0] + mat[1][1] * in[1] + mat[1][2] * in[2] + mat[1][3];
tt[2] = mat[2][0] * in[0] + mat[2][1] * in[1] + mat[2][2] * in[2] + mat[2][3];
out[0] = tt[0];
out[1] = tt[1];
out[2] = tt[2];
}
/* - - - - - - - - - - - - - - - - - - - - - - - - */
/* Given two 3D vectors, create a matrix that translates, */
/* rotates and scales one onto the other. */
/* The maths is from page 52 of Tomas Moller and Eric Haines */
/* "Real-Time Rendering". */
/* s0 -> s1 is source vector, t0 -> t1 is target vector. */
/* Usage of icmRotMat: */
/* t[0] = mat[0][0] * s[0] + mat[0][1] * s[1] + mat[0][2] * s[2] + mat[0][3]; */
/* t[1] = mat[1][0] * s[0] + mat[1][1] * s[1] + mat[1][2] * s[2] + mat[1][3]; */
/* t[2] = mat[2][0] * s[0] + mat[2][1] * s[1] + mat[2][2] * s[2] + mat[2][3]; */
/* i.e. use icmMul3By3x4 */
void icmVecRotMat(double m[3][4], double s1[3], double s0[3], double t1[3], double t0[3]) {
int i, j;
double ss[3], tt[3], rr[3][3];
/* Create the rotation matrix: */
for (i = 0; i < 3; i++) {
ss[i] = s1[i] - s0[i];
tt[i] = t1[i] - t0[i];
}
icmRotMat(rr, ss, tt);
/* Create rotated version of s0: */
icmMulBy3x3(ss, rr, s0);
/* Create 4x4 matrix */
for (j = 0; j < 3; j++) {
for (i = 0; i < 4; i++) {
if (i < 3 && j < 3)
m[j][i] = rr[j][i];
else if (i == 3 && j < 3)
m[j][i] = t0[j] - ss[j];
else if (i == j)
m[j][i] = 1.0;
else
m[j][i] = 0.0;
}
}
#ifdef NEVER /* Check result */
{
double tt0[3], tt1[3];
icmMul3By3x4(tt0, m, s0);
if (icmLabDEsq(t0, tt0) > 1e-4) {
printf("icmVecRotMat error t0, is %f %f %f\n",tt0[0],tt0[1],tt0[2]);
printf(" should be %f %f %f\n",t0[0],t0[1],t0[2]);
}
icmMul3By3x4(tt1, m, s1);
if (icmLabDEsq(t1, tt1) > 1e-4) {
printf("icmVecRotMat error t1, is %f %f %f\n",tt1[0],tt1[1],tt1[2]);
printf(" should be %f %f %f\n",t1[0],t1[1],t1[2]);
}
}
#endif /* NEVER */
}
/* - - - - - - - - - - - - - - - - - - - - - - - - */
/* Compute the 3D intersection of a vector and a plane */
/* return nz if there is no intersection */
int icmVecPlaneIsect(
double isect[3], /* return intersection point */
double pl_const, /* Plane equation constant */
double pl_norm[3], /* Plane normal vector */
double ve_1[3], /* Point on line */
double ve_0[3] /* Second point on line */
) {
double den; /* denominator */
double rv; /* Vector parameter value */
double vvec[3]; /* Vector vector */
double ival[3]; /* Intersection value */
/* Compute vector between the two points */
vvec[0] = ve_1[0] - ve_0[0];
vvec[1] = ve_1[1] - ve_0[1];
vvec[2] = ve_1[2] - ve_0[2];
/* Compute the denominator */
den = pl_norm[0] * vvec[0] + pl_norm[1] * vvec[1] + pl_norm[2] * vvec[2];
/* Too small to intersect ? */
if (fabs(den) < 1e-12) {
return 1;
}
/* Compute the parameterized intersction point */
rv = -(pl_norm[0] * ve_0[0] + pl_norm[1] * ve_0[1] + pl_norm[2] * ve_0[2] + pl_const)/den;
/* Compute the actual intersection point */
isect[0] = ve_0[0] + rv * vvec[0];
isect[1] = ve_0[1] + rv * vvec[1];
isect[2] = ve_0[2] + rv * vvec[2];
return 0;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - */
/* Compute the closest point on a line to a point. */
/* Return closest point and parameter value if not NULL. */
/* Return nz if the line length is zero */
int icmLinePointClosest(double cp[3], double *pa,
double la0[3], double la1[3], double pp[3]) {
double va[3], vp[3];
double val; /* Vector length squared */
double a; /* Parameter value */
icmSub3(va, la1, la0); /* Line vector */
val = icmNorm3sq(va); /* Vector length squared */
if (val < 1e-12)
return 1;
icmSub3(vp, pp, la0); /* Point vector to line base */
a = icmDot3(vp, va) / val; /* Normalised dist of point projected onto line */
if (cp != NULL)
icmBlend3(cp, la0, la1, a);
if (pa != NULL)
*pa = a;
return 0;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - */
/* Compute the closest points between two lines a and b. */
/* Return closest points and parameter values if not NULL. */
/* Return nz if they are paralel. */
/* The maths is from page 338 of Tomas Moller and Eric Haines "Real-Time Rendering". */
int icmLineLineClosest(double ca[3], double cb[3], double *pa, double * pb,
double la0[3], double la1[3], double lb0[3], double lb1[3]) {
double va[3], vb[3];
double vvab[3], vvabns; /* Cross product of va and vb and its norm squared */
double vba0[3]; /* lb0 - la0 */
double tt[3];
double a, b; /* Parameter values */
icmSub3(va, la1, la0);
icmSub3(vb, lb1, lb0);
icmCross3(vvab, va, vb);
vvabns = icmNorm3sq(vvab);
if (vvabns < 1e-12)
return 1;
icmSub3(vba0, lb0, la0);
icmCross3(tt, vba0, vb);
a = icmDot3(tt, vvab) / vvabns;
icmCross3(tt, vba0, va);
b = icmDot3(tt, vvab) / vvabns;
if (pa != NULL)
*pa = a;
if (pb != NULL)
*pb = b;
if (ca != NULL) {
ca[0] = la0[0] + a * va[0];
ca[1] = la0[1] + a * va[1];
ca[2] = la0[2] + a * va[2];
}
if (cb != NULL) {
cb[0] = lb0[0] + b * vb[0];
cb[1] = lb0[1] + b * vb[1];
cb[2] = lb0[2] + b * vb[2];
}
#ifdef NEVER /* Verify */
{
double vab[3]; /* Vector from ca to cb */
vab[0] = lb0[0] + b * vb[0] - la0[0] - a * va[0];
vab[1] = lb0[1] + b * vb[1] - la0[1] - a * va[1];
vab[2] = lb0[2] + b * vb[2] - la0[2] - a * va[2];
if (icmDot3(va, vab) > 1e-6
|| icmDot3(vb, vab) > 1e-6)
warning("icmLineLineClosest verify failed\n");
}
#endif
return 0;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - */
/* Given 3 3D points, compute a plane equation. */
/* The normal will be right handed given the order of the points */
/* The plane equation will be the 3 normal components and the constant. */
/* Return nz if any points are cooincident or co-linear */
int icmPlaneEqn3(double eq[4], double p0[3], double p1[3], double p2[3]) {
double ll, v1[3], v2[3];
/* Compute vectors along edges */
v2[0] = p1[0] - p0[0];
v2[1] = p1[1] - p0[1];
v2[2] = p1[2] - p0[2];
v1[0] = p2[0] - p0[0];
v1[1] = p2[1] - p0[1];
v1[2] = p2[2] - p0[2];
/* Compute cross products v1 x v2, which will be the normal */
eq[0] = v1[1] * v2[2] - v1[2] * v2[1];
eq[1] = v1[2] * v2[0] - v1[0] * v2[2];
eq[2] = v1[0] * v2[1] - v1[1] * v2[0];
/* Normalise the equation */
ll = sqrt(eq[0] * eq[0] + eq[1] * eq[1] + eq[2] * eq[2]);
if (ll < 1e-10) {
return 1;
}
eq[0] /= ll;
eq[1] /= ll;
eq[2] /= ll;
/* Compute the plane equation constant */
eq[3] = - (eq[0] * p0[0])
- (eq[1] * p0[1])
- (eq[2] * p0[2]);
return 0;
}
/* Given a 3D point and a plane equation, return the signed */
/* distance from the plane */
double icmPlaneDist3(double eq[4], double p[3]) {
double rv;
rv = eq[0] * p[0]
+ eq[1] * p[1]
+ eq[2] * p[2]
+ eq[3];
return rv;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - */
/* Compute the norm (length) of a vector define by two points */
double icmNorm22(double in1[2], double in0[2]) {
int j;
double rv;
for (rv = 0.0, j = 0; j < 2; j++) {
double tt = in1[j] - in0[j];
rv += tt * tt;
}
return sqrt(rv);
}
/* Compute the norm (length) squared of of a vector defined by two points */
double icmNorm22sq(double in1[2], double in0[2]) {
int j;
double rv;
for (rv = 0.0, j = 0; j < 2; j++) {
double tt = in1[j] - in0[j];
rv += tt * tt;
}
return rv;
}
/* Multiply 2 array by 2x2 transform matrix */
void icmMulBy2x2(double out[2], double mat[2][2], double in[2]) {
double tt[2];
tt[0] = mat[0][0] * in[0] + mat[0][1] * in[1];
tt[1] = mat[1][0] * in[0] + mat[1][1] * in[1];
out[0] = tt[0];
out[1] = tt[1];
}
/* Compute the dot product of two 2 vectors */
double icmDot2(double in1[2], double in2[2]) {
return in1[0] * in2[0] + in1[1] * in2[1];
}
/* Compute the dot product of two 2 vectors defined by 4 points */
/* 1->2 and 3->4 */
double icmDot22(double in1[2], double in2[2], double in3[2], double in4[2]) {
return (in2[0] - in1[0]) * (in4[0] - in3[0])
+ (in2[1] - in1[1]) * (in4[1] - in3[1]);
}
/* Given 2 2D points, compute a plane equation (implicit line equation). */
/* The normal will be right handed given the order of the points */
/* The plane equation will be the 2 normal components and the constant. */
/* Return nz if any points are cooincident or co-linear */
int icmPlaneEqn2(double eq[3], double p0[2], double p1[2]) {
double ll, v1[3];
/* Compute vectors along edge */
v1[0] = p1[0] - p0[0];
v1[1] = p1[1] - p0[1];
/* Normal to vector */
eq[0] = v1[1];
eq[1] = -v1[0];
/* Normalise the equation */
ll = sqrt(eq[0] * eq[0] + eq[1] * eq[1]);
if (ll < 1e-10) {
return 1;
}
eq[0] /= ll;
eq[1] /= ll;
/* Compute the plane equation constant */
eq[2] = - (eq[0] * p0[0])
- (eq[1] * p0[1]);
return 0;
}
/* Given a 2D point and a plane equation (implicit line), return the signed */
/* distance from the plane. The distance will be +ve if the point */
/* is to the right of the plane formed by two points in order */
double icmPlaneDist2(double eq[3], double p[2]) {
double rv;
rv = eq[0] * p[0]
+ eq[1] * p[1]
+ eq[2];
return rv;
}
/* Return the closest point on an implicit line to a point. */
/* Also return the absolute distance */
double icmImpLinePointClosest2(double cp[2], double eq[3], double pp[2]) {
double q; /* Closest distance to line */
q = eq[0] * pp[0]
+ eq[1] * pp[1]
+ eq[2];
cp[0] = pp[0] - q * eq[0];
cp[1] = pp[1] - q * eq[1];
return fabs(q);
}
/* Return the point of intersection of two implicit lines . */
/* Return nz if there is no intersection (lines are parallel) */
int icmImpLineIntersect2(double res[2], double eq1[3], double eq2[3]) {
double num;
num = eq1[0] * eq2[1] - eq2[0] * eq1[1];
if (fabs(num) < 1e-10)
return 1;
res[0] = (eq2[2] * eq1[1] - eq1[2] * eq2[1])/num;
res[1] = (eq1[2] * eq2[0] - eq2[2] * eq1[0])/num;
return 0;
}
/* Compute the closest point on a line to a point. */
/* Return closest point and parameter value if not NULL. */
/* Return nz if the line length is zero */
int icmLinePointClosest2(double cp[2], double *pa,
double la0[2], double la1[2], double pp[2]) {
double va[2], vp[2];
double val; /* Vector length squared */
double a; /* Parameter value */
va[0] = la1[0] - la0[0]; /* Line vector */
va[1] = la1[1] - la0[1];
val = va[0] * va[0] + va[1] * va[1];
if (val < 1e-12)
return 1;
vp[0] = pp[0] - la0[0]; /* Point vector to line base */
vp[1] = pp[1] - la0[1];
a = (vp[0] * va[0] + vp[1] * va[1]) / val; /* Normalised dist of point projected onto line */
if (cp != NULL) {
cp[0] = (1.0 - a) * la0[0] + a * la1[0];
cp[1] = (1.0 - a) * la0[1] + a * la1[1];
}
if (pa != NULL)
*pa = a;
return 0;
}
/* Given two infinite 2D lines define by 4 points, compute the intersection. */
/* Return nz if there is no intersection (lines are parallel) */
int icmLineIntersect2(double res[2], double p1[2], double p2[2], double p3[2], double p4[2]) {
/* Compute by determinants */
double x1y2_y1x2 = p1[0] * p2[1] - p1[1] * p2[0];
double x3y4_y3x4 = p3[0] * p4[1] - p3[1] * p4[0];
double x1_x2 = p1[0] - p2[0];
double y1_y2 = p1[1] - p2[1];
double x3_x4 = p3[0] - p4[0];
double y3_y4 = p3[1] - p4[1];
double num; /* Numerator */
num = x1_x2 * y3_y4 - y1_y2 * x3_x4;
if (fabs(num) < 1e-10)
return 1;
res[0] = (x1y2_y1x2 * x3_x4 - x1_x2 * x3y4_y3x4)/num;
res[1] = (x1y2_y1x2 * y3_y4 - y1_y2 * x3y4_y3x4)/num;
return 0;
}
/* Given two finite 2D lines define by 4 points, compute their paramaterized intersection. */
/* aprm may be NULL. Param is prop. from p1 -> p2, p3 -> p4 */
/* Return 2 if there is no intersection (lines are parallel) */
/* Return 1 lines do not cross within their length */
int icmParmLineIntersect2(double res[2], double aprm[2], double p1[2], double p2[2], double p3[2], double p4[2]) {
double _prm[2];
double *prm = aprm != NULL ? aprm : _prm;
double x21 = p2[0] - p1[0];
double y21 = p2[1] - p1[1];
double x31 = p3[0] - p1[0];
double y31 = p3[1] - p1[1];
double x43 = p4[0] - p3[0];
double y43 = p4[1] - p3[1];
double num; /* Numerator */
num = x43 * y21 - x21 * y43;
if (fabs(num) < 1e-10)
return 2;
prm[0] = (x43 * y31 - x31 * y43)/num; /* Parameter of 1->2 */
prm[1] = (x21 * y31 - x31 * y21)/num; /* Parameter of 3->4 */
if (res != NULL) {
res[0] = x21 * prm[0] + p1[0];
res[1] = y21 * prm[0] + p1[1];
}
if (prm[0] < -1e-10 || prm[0] > (1.0 + 1e-10)
|| prm[1] < -1e-10 || prm[1] > (1.0 + 1e-10))
return 1;
return 0;
}
/* Compute a blend between in0 and in1 */
void icmBlend2(double out[2], double in0[2], double in1[2], double bf) {
out[0] = (1.0 - bf) * in0[0] + bf * in1[0];
out[1] = (1.0 - bf) * in0[1] + bf * in1[1];
}
/* Scale a 2 vector by the given ratio */
void icmScale2(double out[2], double in[2], double rat) {
out[0] = in[0] * rat;
out[1] = in[1] * rat;
}
/* Scale a 2 vector by the given ratio and add it */
void icmScaleAdd2(double out[3], double in2[2], double in1[2], double rat) {
out[0] = in2[0] + in1[0] * rat;
out[1] = in2[1] + in1[1] * rat;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - */
/* CIE Y (range 0 .. 1) to perceptual CIE 1976 L* (range 0 .. 100) */
double
icmY2L(double val) {
if (val > 0.008856451586)
val = pow(val,1.0/3.0);
else
val = 7.787036979 * val + 16.0/116.0;
val = (116.0 * val - 16.0);
return val;
}
/* Perceptual CIE 1976 L* (range 0 .. 100) to CIE Y (range 0 .. 1) */
double
icmL2Y(double val) {
val = (val + 16.0)/116.0;
if (val > 24.0/116.0)
val = pow(val,3.0);
else
val = (val - 16.0/116.0)/7.787036979;
return val;
}
/* CIE XYZ to perceptual CIE 1976 L*a*b* */
void
icmXYZ2Lab(icmXYZNumber *w, double *out, double *in) {
double X = in[0], Y = in[1], Z = in[2];
double x,y,z,fx,fy,fz;
x = X/w->X;
y = Y/w->Y;
z = Z/w->Z;
if (x > 0.008856451586)
fx = pow(x,1.0/3.0);
else
fx = 7.787036979 * x + 16.0/116.0;
if (y > 0.008856451586)
fy = pow(y,1.0/3.0);
else
fy = 7.787036979 * y + 16.0/116.0;
if (z > 0.008856451586)
fz = pow(z,1.0/3.0);
else
fz = 7.787036979 * z + 16.0/116.0;
out[0] = 116.0 * fy - 16.0;
out[1] = 500.0 * (fx - fy);
out[2] = 200.0 * (fy - fz);
}
/* Perceptual CIE 1976 L*a*b* to CIE XYZ */
void
icmLab2XYZ(icmXYZNumber *w, double *out, double *in) {
double L = in[0], a = in[1], b = in[2];
double x,y,z,fx,fy,fz;
fy = (L + 16.0)/116.0;
fx = a/500.0 + fy;
fz = fy - b/200.0;
if (fy > 24.0/116.0)
y = pow(fy,3.0);
else
y = (fy - 16.0/116.0)/7.787036979;
if (fx > 24.0/116.0)
x = pow(fx,3.0);
else
x = (fx - 16.0/116.0)/7.787036979;
if (fz > 24.0/116.0)
z = pow(fz,3.0);
else
z = (fz - 16.0/116.0)/7.787036979;
out[0] = x * w->X;
out[1] = y * w->Y;
out[2] = z * w->Z;
}
/*
* This is a modern update to L*a*b*, based on IPT space.
*
* Differences to L*a*b* and IPT:
* Using inverse CIE 2012 2degree LMS to XYZ matrix instead of Hunt-Pointer-Estevez.
* Von Kries chromatic adapation in LMS space.
* Using L* compression rather than IPT pure 0.43 power.
* Tweaked LMS' to IPT matrix to account for change in XYZ to LMS matrix.
* Output scaled to L*a*b* type ranges, to maintain 1 JND scale.
* (Watch out - L* value is not a non-linear Y value though!
* - Interesting that Dolby force L to be just dependent on Y
* by making L = 0.5 L | 0.5 M in ICtCp space).
*/
/* CIE XYZ to perceptual Lpt */
void
icmXYZ2Lpt(icmXYZNumber *w, double *out, double *in) {
double wxyz[3];
double wlms[3];
double lms[3];
double xyz2lms[3][3] = {
{ 0.2052445519046028, 0.8334486497310412, -0.0386932016356441 },
{ -0.4972221301804286, 1.4034846060306130, 0.0937375241498157 },
{ 0.0000000000000000, 0.0000000000000000, 1.0000000000000000 }
};
double lms2ipt[3][3] = {
{ 0.6585034777870502, 0.1424555300344579, 0.1990409921784920 },
{ 5.6413505933276049, -6.1697985811414187, 0.5284479878138138 },
{ 1.6370552576322106, 0.0192823194340315, -1.6563375770662419 }
};
int j;
/* White point in Cone space */
wxyz[0] = w->X;
wxyz[1] = w->Y;
wxyz[2] = w->Z;
icmMulBy3x3(wlms, xyz2lms, wxyz);
/* Incoming XYZ to Cone space */
icmMulBy3x3(lms, xyz2lms, in);
for (j = 0; j < 3; j++) {
/* Von Kries chromatic adapation */
lms[j] /= wlms[j];
/* Non-linearity */
if (lms[j] > 0.008856451586)
lms[j] = pow(lms[j],1.0/3.0);
else
lms[j] = 7.787036979 * lms[j] + 16.0/116.0;
lms[j] = 116.0 * lms[j] - 16.0;
}
/* IPT */
icmMulBy3x3(out, lms2ipt, lms);
}
void
icmLpt2XYZ(icmXYZNumber *w, double *out, double *in) {
double wxyz[3];
double wlms[3];
double lms[3];
double xyz2lms[3][3] = {
{ 0.2052445519046028, 0.8334486497310412, -0.0386932016356441 },
{ -0.4972221301804286, 1.4034846060306130, 0.0937375241498157 },
{ 0.0000000000000000, 0.0000000000000000, 1.0000000000000000 }
};
double ipt2lms[3][3] = {
{ 1.0000000000000000, 0.0234881527511557, 0.1276631419615779 },
{ 1.0000000000000000, -0.1387534648407132, 0.0759005921388901 },
{ 1.0000000000000000, 0.0215994105411036, -0.4766811148374502 }
};
double lms2xyz[3][3] = {
{ 1.9979376130193824, -1.1864600428553205, 0.1885224298359384 },
{ 0.7078230795296872, 0.2921769204703129, -0.0000000000000000 },
{ 0.0000000000000000, 0.0000000000000000, 1.0000000000000000 }
};
int j;
wxyz[0] = w->X;
wxyz[1] = w->Y;
wxyz[2] = w->Z;
icmMulBy3x3(wlms, xyz2lms, wxyz);
icmMulBy3x3(lms, ipt2lms, in);
for (j = 0; j < 3; j++) {
lms[j] = (lms[j] + 16.0)/116.0;
if (lms[j] > 24.0/116.0)
lms[j] = pow(lms[j], 3.0);
else
lms[j] = (lms[j] - 16.0/116.0)/7.787036979;
lms[j] *= wlms[j];
}
icmMulBy3x3(out, lms2xyz, lms);
}
/* LCh to Lab (general to polar, works with Lpt, Luv too) */
void icmLCh2Lab(double *out, double *in) {
double C, h;
C = in[1];
h = M_PI/180.0 * in[2];
out[0] = in[0];
out[1] = C * cos(h);
out[2] = C * sin(h);
}
/* Lab to LCh (general to polar, works with Lpt, Luv too) */
void icmLab2LCh(double *out, double *in) {
double C, h;
C = sqrt(in[1] * in[1] + in[2] * in[2]);
h = (180.0/M_PI) * atan2(in[2], in[1]);
h = (h < 0.0) ? h + 360.0 : h;
out[0] = in[0];
out[1] = C;
out[2] = h;
}
/* XYZ to Yxy */
extern ICCLIB_API void icmXYZ2Yxy(double *out, double *in) {
double sum = in[0] + in[1] + in[2];
double Y, x, y;
if (sum < 1e-9) {
Y = 0.0;
y = 1.0/3.0;
x = 1.0/3.0;
} else {
Y = in[1];
x = in[0]/sum;
y = in[1]/sum;
}
out[0] = Y;
out[1] = x;
out[2] = y;
}
/* XYZ to xy */
extern ICCLIB_API void icmXYZ2xy(double *out, double *in) {
double sum = in[0] + in[1] + in[2];
double x, y;
if (sum < 1e-9) {
y = 1.0/3.0;
x = 1.0/3.0;
} else {
x = in[0]/sum;
y = in[1]/sum;
}
out[0] = x;
out[1] = y;
}
/* Yxy to XYZ */
extern ICCLIB_API void icmYxy2XYZ(double *out, double *in) {
double Y = in[0];
double x = in[1];
double y = in[2];
double z = 1.0 - x - y;
double sum;
if (y < 1e-9) {
out[0] = out[1] = out[2] = 0.0;
} else {
sum = Y/y;
out[0] = x * sum;
out[1] = Y;
out[2] = z * sum;
}
}
/* Y & xy to XYZ */
extern ICCLIB_API void icmY_xy2XYZ(double *out, double *xy, double Y) {
double x = xy[0];
double y = xy[1];
double z = 1.0 - x - y;
double sum;
if (y < 1e-9) {
out[0] = out[1] = out[2] = 0.0;
} else {
sum = Y/y;
out[0] = x * sum;
out[1] = Y;
out[2] = z * sum;
}
}
/* CIE XYZ to perceptual CIE 1976 L*u*v* */
extern ICCLIB_API void icmXYZ2Luv(icmXYZNumber *w, double *out, double *in) {
double X = in[0], Y = in[1], Z = in[2];
double un, vn, u, v, fl, fu, fv;
un = (4.0 * w->X) / (w->X + 15.0 * w->Y + 3.0 * w->Z);
vn = (9.0 * w->Y) / (w->X + 15.0 * w->Y + 3.0 * w->Z);
u = (4.0 * X) / (X + 15.0 * Y + 3.0 * Z);
v = (9.0 * Y) / (X + 15.0 * Y + 3.0 * Z);
Y /= w->Y;
if (Y > 0.008856451586)
fl = pow(Y,1.0/3.0);
else
fl = 7.787036979 * Y + 16.0/116.0;
fu = u - un;
fv = v - vn;
out[0] = 116.0 * fl - 16.0;
out[1] = 13.0 * out[0] * fu;
out[2] = 13.0 * out[0] * fv;
}
/* Perceptual CIE 1976 L*u*v* to CIE XYZ */
extern ICCLIB_API void icmLuv2XYZ(icmXYZNumber *w, double *out, double *in) {
double un, vn, u, v, fl, fu, fv, sum, X, Y, Z;
fl = (in[0] + 16.0)/116.0;
fu = in[1] / (13.0 * in[0]);
fv = in[2] / (13.0 * in[0]);
un = (4.0 * w->X) / (w->X + 15.0 * w->Y + 3.0 * w->Z);
vn = (9.0 * w->Y) / (w->X + 15.0 * w->Y + 3.0 * w->Z);
u = fu + un;
v = fv + vn;
if (fl > 24.0/116.0)
Y = pow(fl,3.0);
else
Y = (fl - 16.0/116.0)/7.787036979;
Y *= w->Y;
sum = (9.0 * Y)/v;
X = (u * sum)/4.0;
Z = (sum - X - 15.0 * Y)/3.0;
out[0] = X;
out[1] = Y;
out[2] = Z;
}
/* CIE XYZ to perceptual CIE 1976 UCS diagram Yu'v'*/
/* (Yu'v' is a better linear chromaticity space than Yxy) */
extern ICCLIB_API void icmXYZ21976UCS(double *out, double *in) {
double X = in[0], Y = in[1], Z = in[2];
double den, u, v;
den = (X + 15.0 * Y + 3.0 * Z);
if (den < 1e-9) {
Y = 0.0;
u = 4.0/19.0;
v = 9.0/19.0;
} else {
u = (4.0 * X) / den;
v = (9.0 * Y) / den;
}
out[0] = Y;
out[1] = u;
out[2] = v;
}
/* Perceptual CIE 1976 UCS diagram Yu'v' to CIE XYZ */
extern ICCLIB_API void icm1976UCS2XYZ(double *out, double *in) {
double u, v, fl, fu, fv, sum, X, Y, Z;
Y = in[0];
u = in[1];
v = in[2];
if (v < 1e-9) {
X = Y = Z = 0.0;
} else {
X = ((9.0 * u * Y)/(4.0 * v));
Z = -(((20.0 * v + 3.0 * u - 12.0) * Y)/(4.0 * v));
}
out[0] = X;
out[1] = Y;
out[2] = Z;
}
/* CIE XYZ to perceptual CIE 1976 UCS diagram u'v'*/
/* (u'v' is a better linear chromaticity space than xy) */
extern ICCLIB_API void icmXYZ21976UCSuv(double *out, double *in) {
double X = in[0], Y = in[1], Z = in[2];
double den, u, v;
den = (X + 15.0 * Y + 3.0 * Z);
if (den < 1e-9) {
u = 4.0/19.0;
v = 9.0/19.0;
} else {
u = (4.0 * X) / den;
v = (9.0 * Y) / den;
}
out[0] = u;
out[1] = v;
}
/* CIE XYZ to perceptual CIE 1960 UCS */
/* (This was obsoleted by the 1976UCS, but is still used */
/* in computing color temperatures.) */
extern ICCLIB_API void icmXYZ21960UCS(double *out, double *in) {
double X = in[0], Y = in[1], Z = in[2];
double den, u, v;
den = (X + 15.0 * Y + 3.0 * Z);
if (den < 1e-9) {
Y = 0.0;
u = 4.0/19.0;
v = 6.0/19.0;
} else {
u = (4.0 * X) / den;
v = (6.0 * Y) / den;
}
out[0] = Y;
out[1] = u;
out[2] = v;
}
/* Perceptual CIE 1960 UCS to CIE XYZ */
extern ICCLIB_API void icm1960UCS2XYZ(double *out, double *in) {
double u, v, fl, fu, fv, sum, X, Y, Z;
Y = in[0];
u = in[1];
v = in[2];
if (v < 1e-9) {
X = Y = Z = 0.0;
} else {
X = ((3.0 * u * Y)/(2.0 * v));
Z = -(((10.0 * v + u - 4.0) * Y)/(2.0 * v));
}
out[0] = X;
out[1] = Y;
out[2] = Z;
}
/* CIE XYZ to perceptual CIE 1964 WUV (U*V*W*) */
/* (This is obsolete but still used in computing CRI) */
extern ICCLIB_API void icmXYZ21964WUV(icmXYZNumber *w, double *out, double *in) {
double W, U, V;
double wucs[3];
double iucs[3];
icmXYZ2Ary(wucs, *w);
icmXYZ21960UCS(wucs, wucs);
icmXYZ21960UCS(iucs, in);
W = 25.0 * pow(iucs[0] * 100.0/wucs[0], 1.0/3.0) - 17.0;
U = 13.0 * W * (iucs[1] - wucs[1]);
V = 13.0 * W * (iucs[2] - wucs[2]);
out[0] = W;
out[1] = U;
out[2] = V;
}
/* Perceptual CIE 1964 WUV (U*V*W*) to CIE XYZ */
extern ICCLIB_API void icm1964WUV2XYZ(icmXYZNumber *w, double *out, double *in) {
double W, U, V;
double wucs[3];
double iucs[3];
icmXYZ2Ary(wucs, *w);
icmXYZ21960UCS(wucs, wucs);
W = in[0];
U = in[1];
V = in[2];
iucs[0] = pow((W + 17.0)/25.0, 3.0) * wucs[0]/100.0;
iucs[1] = U / (13.0 * W) + wucs[1];
iucs[2] = V / (13.0 * W) + wucs[2];
icm1960UCS2XYZ(out, iucs);
}
/* CIE CIE1960 UCS to perceptual CIE 1964 WUV (U*V*W*) */
/* (This is used in computing CRI) */
extern ICCLIB_API void icm1960UCS21964WUV(icmXYZNumber *w, double *out, double *in) {
double W, U, V;
double wucs[3];
icmXYZ2Ary(wucs, *w);
icmXYZ21960UCS(wucs, wucs);
W = 25.0 * pow(in[0] * 100.0/wucs[0], 1.0/3.0) - 17.0;
U = 13.0 * W * (in[1] - wucs[1]);
V = 13.0 * W * (in[2] - wucs[2]);
out[0] = W;
out[1] = U;
out[2] = V;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - */
/* NOTE :- that these values are for the 1931 standard observer. */
/* Since they are an arbitrary 4 decimal place accuracy, we round */
/* them to be exactly the same as ICC header encoded values, */
/* to avoid any slight discrepancy with PCS white from profiles. */
/* available D50 Illuminant */
icmXYZNumber icmD50 = { /* Profile illuminant - D50 */
RND_S15FIXED16(0.9642),
RND_S15FIXED16(1.0000),
RND_S15FIXED16(0.8249)
};
icmXYZNumber icmD50_100 = { /* Profile illuminant - D50, scaled to 100 */
RND_S15FIXED16(0.9642) * 100.0,
RND_S15FIXED16(1.0000) * 100.0,
RND_S15FIXED16(0.8249) * 100.0
};
double icmD50_ary3[3] = { /* Profile illuminant - D50 */
RND_S15FIXED16(0.9642),
RND_S15FIXED16(1.0000),
RND_S15FIXED16(0.8249)
};
double icmD50_100_ary3[3] = { /* Profile illuminant - D50, scaled to 100 */
RND_S15FIXED16(0.9642) * 100.0,
RND_S15FIXED16(1.0000) * 100.0,
RND_S15FIXED16(0.8249) * 100.0
};
/* available D65 Illuminant */
icmXYZNumber icmD65 = { /* Profile illuminant - D65 */
RND_S15FIXED16(0.9505),
RND_S15FIXED16(1.0000),
RND_S15FIXED16(1.0890)
};
icmXYZNumber icmD65_100 = { /* Profile illuminant - D65, scaled to 100 */
RND_S15FIXED16(0.9505) * 100.0,
RND_S15FIXED16(1.0000) * 100.0,
RND_S15FIXED16(1.0890) * 100.0
};
double icmD65_ary3[3] = { /* Profile illuminant - D65 */
RND_S15FIXED16(0.9505),
RND_S15FIXED16(1.0000),
RND_S15FIXED16(1.0890)
};
double icmD65_100_ary3[3] = { /* Profile illuminant - D65, scaled to 100 */
RND_S15FIXED16(0.9505) * 100.0,
RND_S15FIXED16(1.0000) * 100.0,
RND_S15FIXED16(1.0890) * 100.0
};
/* Default black point */
icmXYZNumber icmBlack = {
0.0000, 0.0000, 0.0000
};
/* The Standard ("wrong Von-Kries") chromatic transform matrix */
double icmWrongVonKries[3][3] = {
{ 1.0000, 0.0000, 0.0000 },
{ 0.0000, 1.0000, 0.0000 },
{ 0.0000, 0.0000, 1.0000 }
};
/* The Bradford chromatic transform matrix */
double icmBradford[3][3] = {
{ RND_S15FIXED16( 0.8951), RND_S15FIXED16( 0.2664), RND_S15FIXED16(-0.1614) },
{ RND_S15FIXED16(-0.7502), RND_S15FIXED16( 1.7135), RND_S15FIXED16( 0.0367) },
{ RND_S15FIXED16( 0.0389), RND_S15FIXED16(-0.0685), RND_S15FIXED16( 1.0296) }
};
/* Return the normal Delta E given two Lab values */
double icmLabDE(double *Lab0, double *Lab1) {
double rv = 0.0, tt;
tt = Lab0[0] - Lab1[0];
rv += tt * tt;
tt = Lab0[1] - Lab1[1];
rv += tt * tt;
tt = Lab0[2] - Lab1[2];
rv += tt * tt;
return sqrt(rv);
}
/* Return the normal Delta E squared, given two Lab values */
double icmLabDEsq(double *Lab0, double *Lab1) {
double rv = 0.0, tt;
tt = Lab0[0] - Lab1[0];
rv += tt * tt;
tt = Lab0[1] - Lab1[1];
rv += tt * tt;
tt = Lab0[2] - Lab1[2];
rv += tt * tt;
return rv;
}
/* Return the normal Delta E squared given two XYZ values */
extern ICCLIB_API double icmXYZLabDEsq(icmXYZNumber *w, double *in0, double *in1) {
double lab0[3], lab1[3], rv;
icmXYZ2Lab(w, lab0, in0);
icmXYZ2Lab(w, lab1, in1);
rv = icmLabDEsq(lab0, lab1);
return rv;
}
/* Return the normal Delta E given two XYZ values */
extern ICCLIB_API double icmXYZLabDE(icmXYZNumber *w, double *in0, double *in1) {
double lab0[3], lab1[3], rv;
icmXYZ2Lab(w, lab0, in0);
icmXYZ2Lab(w, lab1, in1);
rv = icmLabDE(lab0, lab1);
return rv;
}
/* Return the normal Delta E squared given two XYZ values */
extern ICCLIB_API double icmXYZLptDEsq(icmXYZNumber *w, double *in0, double *in1) {
double lab0[3], lab1[3], rv;
icmXYZ2Lpt(w, lab0, in0);
icmXYZ2Lpt(w, lab1, in1);
rv = icmLabDEsq(lab0, lab1);
return rv;
}
/* Return the normal Delta E given two XYZ values */
extern ICCLIB_API double icmXYZLptDE(icmXYZNumber *w, double *in0, double *in1) {
double lab0[3], lab1[3], rv;
icmXYZ2Lpt(w, lab0, in0);
icmXYZ2Lpt(w, lab1, in1);
rv = icmLabDE(lab0, lab1);
return rv;
}
/* (Note that CIE94 can give odd results for very large delta E's, */
/* when one of the two points is near the neutral axis: */
/* ie DE(A,B + del) != DE(A,B) + DE(del) */
#ifdef NEVER
{
double w1[3] = { 99.996101, -0.058417, -0.010557 };
double c1[3] = { 60.267956, 98.845863, -61.163277 };
double w2[3] = { 100.014977, -0.138339, 0.089744 };
double c2[3] = { 60.294464, 98.117104, -60.843159 };
printf("DE 1 = %f, 2 = %f\n", icmLabDE(c1, w1), icmLabDE(c2, w2));
printf("DE94 1 = %f, 2 = %f\n", icmCIE94(c1, w1), icmCIE94(c2, w2));
}
#endif
/* Return the CIE94 Delta E color difference measure, squared */
double icmCIE94sq(double Lab0[3], double Lab1[3]) {
double desq, dhsq;
double dlsq, dcsq;
double c12;
{
double dl, da, db;
dl = Lab0[0] - Lab1[0];
dlsq = dl * dl; /* dl squared */
da = Lab0[1] - Lab1[1];
db = Lab0[2] - Lab1[2];
/* Compute normal Lab delta E squared */
desq = dlsq + da * da + db * db;
}
{
double c1, c2, dc;
/* Compute chromanance for the two colors */
c1 = sqrt(Lab0[1] * Lab0[1] + Lab0[2] * Lab0[2]);
c2 = sqrt(Lab1[1] * Lab1[1] + Lab1[2] * Lab1[2]);
c12 = sqrt(c1 * c2); /* Symetric chromanance */
/* delta chromanance squared */
dc = c1 - c2;
dcsq = dc * dc;
}
/* Compute delta hue squared */
if ((dhsq = desq - dlsq - dcsq) < 0.0)
dhsq = 0.0;
{
double sc, sh;
/* Weighting factors for delta chromanance & delta hue */
sc = 1.0 + 0.045 * c12;
sh = 1.0 + 0.015 * c12;
return dlsq + dcsq/(sc * sc) + dhsq/(sh * sh);
}
}
/* Return the CIE94 Delta E color difference measure */
double icmCIE94(double Lab0[3], double Lab1[3]) {
return sqrt(icmCIE94sq(Lab0, Lab1));
}
/* Return the CIE94 Delta E color difference measure for two XYZ values */
extern ICCLIB_API double icmXYZCIE94(icmXYZNumber *w, double *in0, double *in1) {
double lab0[3], lab1[3];
icmXYZ2Lab(w, lab0, in0);
icmXYZ2Lab(w, lab1, in1);
return sqrt(icmCIE94sq(lab0, lab1));
}
/* From the paper "The CIEDE2000 Color-Difference Formula: Implementation Notes, */
/* Supplementary Test Data, and Mathematical Observations", by */
/* Gaurav Sharma, Wencheng Wu and Edul N. Dalal, */
/* Color Res. Appl., vol. 30, no. 1, pp. 21-30, Feb. 2005. */
/* Return the CIEDE2000 Delta E color difference measure squared, for two Lab values */
double icmCIE2Ksq(double *Lab0, double *Lab1) {
double C1, C2;
double h1, h2;
double dL, dC, dH;
double dsq;
/* The trucated value of PI is needed to ensure that the */
/* test cases pass, as one of them lies on the edge of */
/* a mathematical discontinuity. The precision is still */
/* enough for any practical use. */
#define RAD2DEG(xx) (180.0/M_PI * (xx))
#define DEG2RAD(xx) (M_PI/180.0 * (xx))
/* Compute Cromanance and Hue angles */
{
double C1ab, C2ab;
double Cab, Cab7, G;
double a1, a2;
C1ab = sqrt(Lab0[1] * Lab0[1] + Lab0[2] * Lab0[2]);
C2ab = sqrt(Lab1[1] * Lab1[1] + Lab1[2] * Lab1[2]);
Cab = 0.5 * (C1ab + C2ab);
Cab7 = pow(Cab,7.0);
G = 0.5 * (1.0 - sqrt(Cab7/(Cab7 + 6103515625.0)));
a1 = (1.0 + G) * Lab0[1];
a2 = (1.0 + G) * Lab1[1];
C1 = sqrt(a1 * a1 + Lab0[2] * Lab0[2]);
C2 = sqrt(a2 * a2 + Lab1[2] * Lab1[2]);
if (C1 < 1e-9)
h1 = 0.0;
else {
h1 = RAD2DEG(atan2(Lab0[2], a1));
if (h1 < 0.0)
h1 += 360.0;
}
if (C2 < 1e-9)
h2 = 0.0;
else {
h2 = RAD2DEG(atan2(Lab1[2], a2));
if (h2 < 0.0)
h2 += 360.0;
}
}
/* Compute delta L, C and H */
{
double dh;
dL = Lab1[0] - Lab0[0];
dC = C2 - C1;
if (C1 < 1e-9 || C2 < 1e-9) {
dh = 0.0;
} else {
dh = h2 - h1;
if (dh > 180.0)
dh -= 360.0;
else if (dh < -180.0)
dh += 360.0;
}
dH = 2.0 * sqrt(C1 * C2) * sin(DEG2RAD(0.5 * dh));
}
{
double L, C, h, T;
double hh, ddeg;
double C7, RC, L50sq, SL, SC, SH, RT;
double dLsq, dCsq, dHsq, RCH;
L = 0.5 * (Lab0[0] + Lab1[0]);
C = 0.5 * (C1 + C2);
if (C1 < 1e-9 || C2 < 1e-9) {
h = h1 + h2;
} else {
h = h1 + h2;
if (fabs(h1 - h2) > 180.0) {
if (h < 360.0)
h += 360.0;
else if (h >= 360.0)
h -= 360.0;
}
h *= 0.5;
}
T = 1.0 - 0.17 * cos(DEG2RAD(h-30.0)) + 0.24 * cos(DEG2RAD(2.0 * h))
+ 0.32 * cos(DEG2RAD(3.0 * h + 6.0)) - 0.2 * cos(DEG2RAD(4.0 * h - 63.0));
L50sq = (L - 50.0) * (L - 50.0);
SL = 1.0 + (0.015 * L50sq)/sqrt(20.0 + L50sq);
SC = 1.0 + 0.045 * C;
SH = 1.0 + 0.015 * C * T;
dLsq = dL/SL;
dCsq = dC/SC;
dHsq = dH/SH;
hh = (h - 275.0)/25.0;
ddeg = 30.0 * exp(-hh * hh);
C7 = pow(C, 7.0);
RC = 2.0 * sqrt(C7/(C7 + 6103515625.0));
RT = -sin(DEG2RAD(2 * ddeg)) * RC;
RCH = RT * dCsq * dHsq;
dLsq *= dLsq;
dCsq *= dCsq;
dHsq *= dHsq;
dsq = dLsq + dCsq + dHsq + RCH;
}
return dsq;
#undef RAD2DEG
#undef DEG2RAD
}
/* Return the CIE2DE000 Delta E color difference measure for two Lab values */
double icmCIE2K(double *Lab0, double *Lab1) {
return sqrt(icmCIE2Ksq(Lab0, Lab1));
}
/* Return the CIEDE2000 Delta E color difference measure for two XYZ values */
ICCLIB_API double icmXYZCIE2K(icmXYZNumber *w, double *in0, double *in1) {
double lab0[3], lab1[3];
icmXYZ2Lab(w, lab0, in0);
icmXYZ2Lab(w, lab1, in1);
return sqrt(icmCIE2Ksq(lab0, lab1));
}
/* - - - - - - - - - - - - - - - - - - - - - - - - */
/* Independent chromatic adaptation transform utility. */
/* Return a 3x3 chromatic adaptation matrix */
/* Use icmMulBy3x3(dst, mat, src) */
/* NOTE that to transform primaries they */
/* must be mat[XYZ][RGB] format! */
void icmChromAdaptMatrix(
int flags, /* Use Bradford flag, Transform given matrix flag */
icmXYZNumber d_wp, /* Destination white point */
icmXYZNumber s_wp, /* Source white point */
double mat[3][3] /* Destination matrix */
) {
double dst[3], src[3]; /* Source & destination white points */
double vkmat[3][3]; /* Von Kries matrix */
static int inited = 0; /* Compute inverse bradford once */
static double ibradford[3][3]; /* Inverse Bradford */
/* Set initial matrix to unity if creating from scratch */
if (!(flags & ICM_CAM_MULMATRIX)) {
icmSetUnity3x3(mat);
}
icmXYZ2Ary(src, s_wp);
icmXYZ2Ary(dst, d_wp);
if (flags & ICM_CAM_BRADFORD) {
icmMulBy3x3(src, icmBradford, src);
icmMulBy3x3(dst, icmBradford, dst);
}
/* Setup the Von Kries white point adaptation matrix */
vkmat[0][0] = dst[0]/src[0];
vkmat[1][1] = dst[1]/src[1];
vkmat[2][2] = dst[2]/src[2];
vkmat[0][1] = vkmat[0][2] = 0.0;
vkmat[1][0] = vkmat[1][2] = 0.0;
vkmat[2][0] = vkmat[2][1] = 0.0;
/* Transform to Bradford space if requested */
if (flags & ICM_CAM_BRADFORD) {
icmMul3x3(mat, icmBradford);
}
/* Apply chromatic adaptation */
icmMul3x3(mat, vkmat);
/* Transform from Bradford space */
if (flags & ICM_CAM_BRADFORD) {
if (inited == 0) {
icmInverse3x3(ibradford, icmBradford);
inited = 1;
}
icmMul3x3(mat, ibradford);
}
/* We're done */
}
/* Setup the wpchtmx appropriately for creating profile. */
/* This is called if the deviceClass has changed on a call */
/* to ->chromAdaptMatrix(), ->get_size() or ->write(). */
static void icc_setup_wpchtmx(icc *p) {
int useBradford = 1; /* Default use Bradford */
/* If set by reading profile or already set appropriately */
if (p->wpchtmx_class == p->header->deviceClass)
return;
/* If we should use ICC standard Wrong Von Kries for white point chromatic adapation */
if (p->header->deviceClass == icSigOutputClass
&& p->useLinWpchtmx) {
useBradford = 0;
}
if (useBradford) {
icmCpy3x3(p->wpchtmx, icmBradford);
icmInverse3x3(p->iwpchtmx, p->wpchtmx);
} else {
icmCpy3x3(p->wpchtmx, icmWrongVonKries);
icmCpy3x3(p->iwpchtmx, icmWrongVonKries);
}
/* This is set for this profile class now */
p->wpchtmx_class = p->header->deviceClass;
}
/* Clear any existing 'chad' matrix, and if Output type profile */
/* and ARGYLL_CREATE_OUTPUT_PROFILE_WITH_CHAD set and */
/* ill_wp != NULL, create a 'chad' matrix. */
static void icc_set_illum(struct _icc *p, double ill_wp[3]) {
p->chadmxValid = 0; /* Calling set_illum signals profile creation, */
/* so discard any previous (i.e. read) chad matrix */
if (ill_wp != NULL) {
icmCpy3(p->illwp, ill_wp);
p->illwpValid = 1;
}
/* Is illuminant chromatic adapation chad matrix needed ? */
if (p->header->deviceClass == icSigOutputClass
&& p->illwpValid
&& p->wrOChad) {
double wp[3];
icmXYZNumber iwp;
/* Create Output illuminant 'chad' matrix */
icmAry2XYZ(iwp, p->illwp);
icmChromAdaptMatrix(ICM_CAM_BRADFORD, icmD50, iwp, p->chadmx);
/* Optimally quantize chad matrix to preserver white point */
icmQuantize3x3S15Fixed16(icmD50_ary3, p->chadmx, p->illwp);
p->chadmxValid = 1;
}
}
/* Return an overall Chromatic Adaptation Matrix for the given source and */
/* destination white points. This will depend on the icc profiles current setup */
/* for Abs->Rel conversion (wpchtmx[][] set to wrong Von Kries or not, whether */
/* 'arts' tag has been read), and whether an Output profile 'chad' tag has bean read */
/* or will be created. (i.e. on creation, assumes icc->set_illum() called). */
/* Use icmMulBy3x3(dst, mat, src) */
/* NOTE that to transform primaries they must be mat[XYZ][RGB] format! */
static void icc_chromAdaptMatrix(
icc *p,
int flags, /* ICM_CAM_NONE or ICM_CAM_MULMATRIX to mult by mat */
double imat[3][3], /* Optional inverse CAT matrix result */
double mat[3][3], /* CAT optional input if ICM_CAM_MULMATRIX & result matrix */
icmXYZNumber d_wp, /* Destination white point (Usually PCS D50) */
icmXYZNumber s_wp /* Source media absolute white point */
) {
double dst[3], src[3]; /* Source & destination white points */
double vkmat[3][3]; /* Von Kries matrix */
double omat[3][3]; /* Output matrix */
if (p->header->deviceClass == icMaxEnumClass) {
fprintf(stderr,"icc_chromAdaptMatrix called with no deviceClass!\n");
}
/* Take a copy of src/dst */
icmXYZ2Ary(src, s_wp);
icmXYZ2Ary(dst, d_wp);
/* See if the profile type has changed, re-evaluate wpchtmx */
if (p->wpchtmx_class != p->header->deviceClass) {
icc_setup_wpchtmx(p);
}
/* Set initial matrix to unity if creating from scratch */
if (flags & ICM_CAM_MULMATRIX)
icmCpy3x3(omat, mat);
else
icmSetUnity3x3(omat);
/* Incorporate Output chad matrix if we will be creating one */
if (p->header->deviceClass == icSigOutputClass
&& p->chadmxValid) {
icmMulBy3x3(src, p->chadmx, src);
icmMul3x3(omat, p->chadmx);
}
/* Transform src/dst to cone space */
icmMulBy3x3(src, p->wpchtmx, src);
icmMulBy3x3(dst, p->wpchtmx, dst);
/* Transform incoming matrix to cone space */
icmMul3x3(omat, p->wpchtmx);
/* Setup the Von Kries white point adaptation matrix */
vkmat[0][0] = dst[0]/src[0];
vkmat[1][1] = dst[1]/src[1];
vkmat[2][2] = dst[2]/src[2];
vkmat[0][1] = vkmat[0][2] = 0.0;
vkmat[1][0] = vkmat[1][2] = 0.0;
vkmat[2][0] = vkmat[2][1] = 0.0;
/* Apply chromatic adaptation */
icmMul3x3(omat, vkmat);
/* Transform from con space */
icmMul3x3(omat, p->iwpchtmx);
if (mat != NULL)
icmCpy3x3(mat, omat);
if (imat != NULL)
icmInverse3x3(imat, omat);
}
/* - - - - - - - - - - - - - - - - - - - - - - - - */
/* RGB XYZ primaries device to RGB->XYZ transform matrix. */
/* We assume that the device is perfectly additive, but that */
/* there may be a scale factor applied to the channels to */
/* match the white point at RGB = 1. */
/* Use icmMulBy3x3(dst, mat, src) */
/* Return non-zero if matrix would be singular */
int icmRGBXYZprim2matrix(
double red[3], /* Red colorant */
double green[3], /* Green colorant */
double blue[3], /* Blue colorant */
double white[3], /* White point */
double mat[3][3] /* Destination matrix[RGB][XYZ] */
) {
double tmat[3][3];
double t[3];
/* Assemble the colorants into a matrix */
tmat[0][0] = red[0];
tmat[0][1] = green[0];
tmat[0][2] = blue[0];
tmat[1][0] = red[1];
tmat[1][1] = green[1];
tmat[1][2] = blue[1];
tmat[2][0] = red[2];
tmat[2][1] = green[2];
tmat[2][2] = blue[2];
/* Compute the inverse */
if (icmInverse3x3(mat, tmat))
return 1;
/* Compute scale vector that maps colorants to white point */
t[0] = mat[0][0] * white[0]
+ mat[0][1] * white[1]
+ mat[0][2] * white[2];
t[1] = mat[1][0] * white[0]
+ mat[1][1] * white[1]
+ mat[1][2] * white[2];
t[2] = mat[2][0] * white[0]
+ mat[2][1] * white[1]
+ mat[2][2] * white[2];
/* Now formulate the transform matrix */
mat[0][0] = red[0] * t[0];
mat[0][1] = green[0] * t[1];
mat[0][2] = blue[0] * t[2];
mat[1][0] = red[1] * t[0];
mat[1][1] = green[1] * t[1];
mat[1][2] = blue[1] * t[2];
mat[2][0] = red[2] * t[0];
mat[2][1] = green[2] * t[1];
mat[2][2] = blue[2] * t[2];
return 0;
}
/* RGB Yxy primaries to device to RGB->XYZ transform matrix */
/* Return non-zero if matrix would be singular */
/* Use icmMulBy3x3(dst, mat, src) */
int icmRGBYxyprim2matrix(
double red[3], /* Red colorant */
double green[3], /* Green colorant */
double blue[3], /* Blue colorant */
double white[3], /* White point */
double mat[3][3], /* Return matrix[RGB][XYZ] */
double wXYZ[3] /* Return white XYZ */
) {
double r[3], g[3], b[3];
icmYxy2XYZ(r, red);
icmYxy2XYZ(g, green);
icmYxy2XYZ(b, blue);
icmYxy2XYZ(wXYZ, white);
return icmRGBXYZprim2matrix(r, g, b, wXYZ, mat);
}
/* - - - - - - - - - - - - - - - - - - - - - - - - */
/* Pre-round a 3x3 matrix to ensure that the product of */
/* the matrix and the input value is the same as */
/* the quantized matrix product. This is used to improve accuracy */
/* of 'chad' tag in computing absolute white point. */
void icmQuantize3x3S15Fixed16(
double targ[3], /* Target of product */
double mat[3][3], /* matrix[][] to be quantized */
double in[3] /* Input of product - must not be 0.0! */
) {
int i, j;
double sum[3]; /* == target */
double tmp[3]; /* Uncorrected sum */
printf("In = %.8f %.8f %.8f\n",in[0], in[1], in[2]);
printf("Target = %.8f %.8f %.8f\n",targ[0], targ[1], targ[2]);
for (j = 0; j < 3; j++)
sum[j] = targ[j];
/* Pre-quantize the matrix, and then ensure that the */
/* sum of the product of the quantized values is the quantized */
/* sum by assigning the rounding error to the largest component. */
for (i = 0; i < 3; i++) {
int bix = 0;
double bval = -1e9;
/* locate the largest and quantize each matrix component */
for (j = 0; j < 3; j++) {
if (fabs(mat[i][j]) > bval) { /* Locate largest */
bix = j;
bval = fabs(mat[i][j]);
}
mat[i][j] = round_S15Fixed16Number(mat[i][j]);
}
/* Check the product of the uncorrected quantized values */
tmp[i] = 0.0;
for (j = 0; j < 3; j++)
tmp[i] += mat[i][j] * in[j];
/* Compute the value the largest has to be */
/* to ensure that sum of the quantized mat[][] times in[] is */
/* equal to the quantized sum. */
for (j = 0; j < 3; j++) {
if (j == bix)
continue;
sum[i] -= mat[i][j] * in[j];
}
mat[i][bix] = round_S15Fixed16Number(sum[i]/in[i]);
/* Check the product of the corrected quantized values */
sum[i] = 0.0;
for (j = 0; j < 3; j++)
sum[i] += mat[i][j] * in[j];
}
printf("Q Sum = %.8f %.8f %.8f\n",tmp[0], tmp[1], tmp[2]);
printf("Q cor Sum = %.8f %.8f %.8f\n",sum[0], sum[1], sum[2]);
}
/* Pre-round RGB device primary values to ensure that */
/* the sum of the quantized primaries is the same as */
/* the quantized sum. */
/* [Note matrix is transposed compared to quantize3x3S15Fixed16() ] */
void quantizeRGBprimsS15Fixed16(
double mat[3][3] /* matrix[RGB][XYZ] */
) {
int i, j;
double sum[3];
// printf("D50 = %f %f %f\n",icmD50.X, icmD50.Y, icmD50.Z);
/* Compute target sum of primary XYZ */
for (i = 0; i < 3; i++) {
sum[i] = 0.0;
for (j = 0; j < 3; j++)
sum[i] += mat[j][i];
}
// printf("Sum = %f %f %f\n",sum[0], sum[1], sum[2]);
/* Pre-quantize the primary XYZ's, and then ensure that the */
/* sum of the quantized values is the quantized sum by assigning */
/* the rounding error to the largest component. */
for (i = 0; i < 3; i++) {
int bix = 0;
double bval = -1e9;
/* locate the largest and quantize each component */
for (j = 0; j < 3; j++) {
if (fabs(mat[j][i]) > bval) { /* Locate largest */
bix = j;
bval = fabs(mat[j][i]);
}
mat[j][i] = round_S15Fixed16Number(mat[j][i]);
}
/* Compute the value the largest has to be */
/* to ensure that sum of the quantized values is */
/* equal to the quantized sum */
for (j = 0; j < 3; j++) {
if (j == bix)
continue;
sum[i] -= mat[j][i];
}
mat[bix][i] = round_S15Fixed16Number(sum[i]);
/* Check the sum of the quantized values */
// sum[i] = 0.0;
// for (j = 0; j < 3; j++)
// sum[i] += mat[j][i];
}
// printf("Q cor Sum = %f %f %f\n",sum[0], sum[1], sum[2]);
}
/* - - - - - - - - - - - - - - - - - - - - - - - - */
/* Some PCS utility functions */
/* Clip Lab, while maintaining hue angle. */
/* Return nz if clipping occured */
int icmClipLab(double out[3], double in[3]) {
double C;
out[0] = in[0];
out[1] = in[1];
out[2] = in[2];
if (out[0] >= 0.0 && out[0] <= 100.0
&& out[1] >= -128.0 && out[1] <= 127.0
&& out[2] >= -128.0 && out[2] <= 127.0)
return 0;
/* Clip L */
if (out[0] < 0.0)
out[0] = 0.0;
else if (out[0] > 100.0)
out[0] = 100.0;
C = out[1];
if (fabs(out[2]) > fabs(C))
C = out[2];
if (C < -128.0 || C > 127.0) {
if (C < 0.0)
C = -128.0/C;
else
C = 127.0/C;
out[1] *= C;
out[2] *= C;
}
return 1;
}
/* Clip XYZ, while maintaining hue angle */
/* Return nz if clipping occured */
int icmClipXYZ(double out[3], double in[3]) {
out[0] = in[0];
out[1] = in[1];
out[2] = in[2];
if (out[0] >= 0.0 && out[0] <= 1.9999
&& out[1] >= 0.0 && out[1] <= 1.9999
&& out[2] >= 0.0 && out[2] <= 1.9999)
return 0;
/* Clip Y and scale X and Z similarly */
if (out[1] > 1.9999) {
out[0] *= 1.9999/out[1];
out[2] *= 1.9999/out[1];
out[1] = 1.9999;
} else if (out[1] < 0.0) {
out[0] = 0.0;
out[1] = 0.0;
out[2] = 0.0;
}
if (out[0] < 0.0 || out[0] > 1.9999
|| out[2] < 0.0 || out[2] > 1.9999) {
double D50[3] = { 0.9642, 1.0000, 0.8249 };
double bb = 0.0;
/* Scale the D50 so that it has the same Y value as our color */
D50[0] *= out[1];
D50[1] *= out[1];
D50[2] *= out[1];
/* Figure out what blend factor with Y scaled D50, brings our */
/* color X and Z values into range */
if (out[0] < 0.0) {
double b;
b = (0.0 - out[0])/(D50[0] - out[0]);
if (b > bb)
bb = b;
} else if (out[0] > 1.9999) {
double b;
b = (1.9999 - out[0])/(D50[0] - out[0]);
if (b > bb)
bb = b;
}
if (out[2] < 0.0) {
double b;
b = (0.0 - out[2])/(D50[2] - out[2]);
if (b > bb)
bb = b;
} else if (out[2] > 1.9999) {
double b;
b = (1.9999 - out[2])/(D50[2] - out[2]);
if (b > bb)
bb = b;
}
/* Do the desaturation */
out[0] = D50[0] * bb + (1.0 - bb) * out[0];
out[2] = D50[2] * bb + (1.0 - bb) * out[2];
}
return 1;
}
/* --------------------------------------------------------------- */
/* Some video specific functions */
/* Should add ST.2048 log functions */
/* Convert Lut table index/value to YPbPr */
/* (Same as Lut_Lut2YPbPr() ) */
void icmLut2YPbPr(double *out, double *in) {
out[0] = in[0]; /* Y */
out[1] = in[1] - 0.5; /* Cb */
out[2] = in[2] - 0.5; /* Cr */
}
/* Convert YPbPr to Lut table index/value */
/* (Same as Lut_YPbPr2Lut() ) */
void icmYPbPr2Lut(double *out, double *in) {
out[0] = in[0]; /* Y */
out[1] = in[1] + 0.5; /* Cb */
out[2] = in[2] + 0.5; /* Cr */
}
/* Convert Rec601 RGB' into YPbPr, or "full range YCbCr" */
/* where input 0..1, output 0..1, -0.5 .. 0.5, -0.5 .. 0.5 */
/* [From the Rec601 spec. ] */
void icmRec601_RGBd_2_YPbPr(double out[3], double in[3]) {
double tt[3];
tt[0] = 0.299 * in[0] + 0.587 * in[1] + 0.114 * in[2];
tt[1] = -0.299 /1.772 * in[0]
+ -0.587 /1.772 * in[1]
+ (1.0-0.114)/1.772 * in[2];
tt[2] = (1.0-0.299)/1.402 * in[0]
+ -0.587 /1.402 * in[1]
+ -0.114 /1.402 * in[2];
out[0] = tt[0];
out[1] = tt[1];
out[2] = tt[2];
}
/* Convert Rec601 YPbPr to RGB' (== "full range YCbCr") */
/* where input 0..1, -0.5 .. 0.5, -0.5 .. 0.5, output 0.0 .. 1 */
/* [Inverse of above] */
void icmRec601_YPbPr_2_RGBd(double out[3], double in[3]) {
double tt[3];
tt[0] = 1.000000000 * in[0] + 0.000000000 * in[1] + 1.402000000 * in[2];
tt[1] = 1.000000000 * in[0] + -0.344136286 * in[1] + -0.714136286 * in[2];
tt[2] = 1.000000000 * in[0] + 1.772000000 * in[1] + 0.000000000 * in[2];
out[0] = tt[0];
out[1] = tt[1];
out[2] = tt[2];
}
/* Convert Rec709 1150/60/2:1 RGB' into YPbPr, or "full range YCbCr" */
/* where input 0..1, output 0..1, -0.5 .. 0.5, -0.5 .. 0.5 */
/* (This is for digital Rec709 and is very close to analog Rec709 60Hz.) */
/* [From the Rec709 specification] */
void icmRec709_RGBd_2_YPbPr(double out[3], double in[3]) {
double tt[3];
tt[0] = 0.2126 * in[0] + 0.7152 * in[1] + 0.0722 * in[2];
tt[1] = 1.0/1.8556 * -0.2126 * in[0]
+ 1.0/1.8556 * -0.7152 * in[1]
+ 1.0/1.8556 * (1.0-0.0722) * in[2];
tt[2] = 1.0/1.5748 * (1.0-0.2126) * in[0]
+ 1.0/1.5748 * -0.7152 * in[1]
+ 1.0/1.5748 * -0.0722 * in[2];
out[0] = tt[0];
out[1] = tt[1];
out[2] = tt[2];
}
/* Convert Rec709 1150/60/2:1 YPbPr to RGB' (== "full range YCbCr") */
/* where input 0..1, -0.5 .. 0.5, -0.5 .. 0.5, output 0.0 .. 1 */
/* (This is for digital Rec709 and is very close to analog Rec709 60Hz.) */
/* [Inverse of above] */
void icmRec709_YPbPr_2_RGBd(double out[3], double in[3]) {
double tt[3];
tt[0] = 1.000000000 * in[0] + 0.000000000 * in[1] + 1.574800000 * in[2];
tt[1] = 1.000000000 * in[0] + -0.187324273 * in[1] + -0.468124273 * in[2];
tt[2] = 1.000000000 * in[0] + 1.855600000 * in[1] + 0.000000000 * in[2];
out[0] = tt[0];
out[1] = tt[1];
out[2] = tt[2];
}
/* Convert Rec709 1250/50/2:1 RGB' into YPbPr, or "full range YCbCr" */
/* where input 0..1, output 0..1, -0.5 .. 0.5, -0.5 .. 0.5 */
/* (This is for analog Rec709 50Hz) */
/* [From the Rec709 specification] */
void icmRec709_50_RGBd_2_YPbPr(double out[3], double in[3]) {
double tt[3];
tt[0] = 0.299 * in[0] + 0.587 * in[1] + 0.114 * in[2];
tt[1] = 0.564 * -0.299 * in[0]
+ 0.564 * -0.587 * in[1]
+ 0.564 * (1.0-0.114) * in[2];
tt[2] = 0.713 * (1.0-0.299) * in[0]
+ 0.713 * -0.587 * in[1]
+ 0.713 * -0.114 * in[2];
out[0] = tt[0];
out[1] = tt[1];
out[2] = tt[2];
}
/* Convert Rec709 1250/50/2:1 YPbPr to RGB' (== "full range YCbCr") */
/* where input 0..1, -0.5 .. 0.5, -0.5 .. 0.5, output 0.0 .. 1 */
/* (This is for analog Rec709 50Hz) */
/* [Inverse of above] */
void icmRec709_50_YPbPr_2_RGBd(double out[3], double in[3]) {
double tt[3];
tt[0] = 1.000000000 * in[0] + 0.000000000 * in[1] + 1.402524544 * in[2];
tt[1] = 1.000000000 * in[0] + -0.344340136 * in[1] + -0.714403473 * in[2];
tt[2] = 1.000000000 * in[0] + 1.773049645 * in[1] + 0.000000000 * in[2];
out[0] = tt[0];
out[1] = tt[1];
out[2] = tt[2];
}
/* Convert Rec2020 RGB' into Non-constant luminance YPbPr, or "full range YCbCr" */
/* where input 0..1, output 0..1, -0.5 .. 0.5, -0.5 .. 0.5 */
/* [From the Rec2020 specification] */
void icmRec2020_NCL_RGBd_2_YPbPr(double out[3], double in[3]) {
double tt[3];
tt[0] = 0.2627 * in[0] + 0.6780 * in[1] + 0.0593 * in[2];
tt[1] = 1/1.8814 * -0.2627 * in[0]
+ 1/1.8814 * -0.6780 * in[1]
+ 1/1.8814 * (1.0-0.0593) * in[2];
tt[2] = 1/1.4746 * (1.0-0.2627) * in[0]
+ 1/1.4746 * -0.6780 * in[1]
+ 1/1.4746 * -0.0593 * in[2];
out[0] = tt[0];
out[1] = tt[1];
out[2] = tt[2];
}
/* Convert Rec2020 Non-constant luminance YPbPr into RGB' (== "full range YCbCr") */
/* where input 0..1, -0.5 .. 0.5, -0.5 .. 0.5, output 0.0 .. 1 */
/* [Inverse of above] */
void icmRec2020_NCL_YPbPr_2_RGBd(double out[3], double in[3]) {
double tt[3];
tt[0] = 1.000000000 * in[0] + 0.000000000 * in[1] + 1.474600000 * in[2];
tt[1] = 1.000000000 * in[0] + -0.164553127 * in[1] + -0.571353127 * in[2];
tt[2] = 1.000000000 * in[0] + 1.881400000 * in[1] + 0.000000000 * in[2];
out[0] = tt[0];
out[1] = tt[1];
out[2] = tt[2];
}
/* Convert Rec2020 RGB' into Constant luminance YPbPr, or "full range YCbCr" */
/* where input 0..1, output 0..1, -0.5 .. 0.5, -0.5 .. 0.5 */
/* [From the Rec2020 specification] */
void icmRec2020_CL_RGBd_2_YPbPr(double out[3], double in[3]) {
int i;
double tt[3];
/* Convert RGB' to RGB */
for (i = 0; i < 3; i++) {
if (in[i] < (4.5 * 0.0181))
tt[i] = in[i]/4.5;
else
tt[i] = pow((in[i] + 0.0993)/1.0993, 1.0/0.45);
}
/* Y value */
tt[0] = 0.2627 * tt[0] + 0.6780 * tt[1] + 0.0593 * tt[2];
/* Y' value */
if (tt[0] < 0.0181)
tt[0] = tt[0] * 4.5;
else
tt[0] = 1.0993 * pow(tt[0], 0.45) - 0.0993;
tt[1] = in[2] - tt[0];
if (tt[1] <= 0.0)
tt[1] /= 1.9404;
else
tt[1] /= 1.5816;
tt[2] = in[0] - tt[0];
if (tt[2] <= 0.0)
tt[2] /= 1.7184;
else
tt[2] /= 0.9936;
out[0] = tt[0];
out[1] = tt[1];
out[2] = tt[2];
}
/* Convert Rec2020 Constant luminance YPbPr into RGB' (== "full range YCbCr") */
/* where input 0..1, -0.5 .. 0.5, -0.5 .. 0.5, output 0.0 .. 1 */
/* [Inverse of above] */
void icmRec2020_CL_YPbPr_2_RGBd(double out[3], double in[3]) {
int i;
double tin[3], tt[3];
/* Y' */
tin[0] = in[0];
/* B' - Y' */
if (in[1] <= 0.0)
tin[1] = 1.9404 * in[1];
else
tin[1] = 1.5816 * in[1];
/* R' - Y' */
if (in[2] <= 0.0)
tin[2] = 1.7184 * in[2];
else
tin[2] = 0.9936 * in[2];
/* R' */
tt[0] = tin[2] + tin[0];
/* Y' */
tt[1] = tin[0];
/* B' */
tt[2] = tin[1] + tin[0];
/* Convert RYB' to RYB */
for (i = 0; i < 3; i++) {
if (tt[i] < (4.5 * 0.0181))
tin[i] = tt[i]/4.5;
else
tin[i] = pow((tt[i] + 0.0993)/1.0993, 1.0/0.45);
}
/* G */
tt[1] = (tin[1] - 0.2627 * tin[0] - 0.0593 * tin[2])/0.6780;
/* G' */
if (tt[1] < 0.0181)
tt[1] = tt[1] * 4.5;
else
tt[1] = 1.0993 * pow(tt[1], 0.45) - 0.0993;
out[0] = tt[0];
out[1] = tt[1];
out[2] = tt[2];
}
/* Convert Rec601/Rec709/Rec2020 YPbPr to YCbCr Video range. */
/* input 0..1, -0.5 .. 0.5, -0.5 .. 0.5, */
/* output 16/255 .. 235/255, 16/255 .. 240/255, 16/255 .. 240/255 */
void icmRecXXX_YPbPr_2_YCbCr(double out[3], double in[3]) {
out[0] = ((235.0 - 16.0) * in[0] + 16.0)/255.0;
out[1] = ((128.0 - 16.0) * 2.0 * in[1] + 128.0)/255.0;
out[2] = ((128.0 - 16.0) * 2.0 * in[2] + 128.0)/255.0;
}
/* Convert Rec601/Rec709/Rec2020 Video YCbCr to YPbPr range. */
/* input 16/255 .. 235/255, 16/255 .. 240/255, 16/255 .. 240/255 */
/* output 0..1, -0.5 .. 0.5, -0.5 .. 0.5, */
void icmRecXXX_YCbCr_2_YPbPr(double out[3], double in[3]) {
out[0] = (255.0 * in[0] - 16.0)/(235.0 - 16.0);
out[1] = (255.0 * in[1] - 128.0)/(2.0 * (128.0 - 16.0));
out[2] = (255.0 * in[2] - 128.0)/(2.0 * (128.0 - 16.0));
}
/* Convert full range RGB to Video range 16..235 RGB */
void icmRGB_2_VidRGB(double out[3], double in[3]) {
out[0] = ((235.0 - 16.0) * in[0] + 16.0)/255.0;
out[1] = ((235.0 - 16.0) * in[1] + 16.0)/255.0;
out[2] = ((235.0 - 16.0) * in[2] + 16.0)/255.0;
}
/* Convert Video range 16..235 RGB to full range RGB */
/* Return nz if outside RGB range */
void icmVidRGB_2_RGB(double out[3], double in[3]) {
out[0] = (255.0 * in[0] - 16.0)/(235.0 - 16.0);
out[1] = (255.0 * in[1] - 16.0)/(235.0 - 16.0);
out[2] = (255.0 * in[2] - 16.0)/(235.0 - 16.0);
}
/* =============================================================== */
/* PS 3.14-2009, Digital Imaging and Communications in Medicine */
/* (DICOM) Part 14: Grayscale Standard Display Function */
/* JND index value 1..1023 to L 0.05 .. 3993.404 cd/m^2 */
static double icmDICOM_fwd_nl(double jnd) {
double a = -1.3011877;
double b = -2.5840191e-2;
double c = 8.0242636e-2;
double d = -1.0320229e-1;
double e = 1.3646699e-1;
double f = 2.8745620e-2;
double g = -2.5468404e-2;
double h = -3.1978977e-3;
double k = 1.2992634e-4;
double m = 1.3635334e-3;
double jj, num, den, rv;
jj = jnd = log(jnd);
num = a;
den = 1.0;
num += c * jj;
den += b * jj;
jj *= jnd;
num += e * jj;
den += d * jj;
jj *= jnd;
num += g * jj;
den += f * jj;
jj *= jnd;
num += m * jj;
den += h * jj;
jj *= jnd;
den += k * jj;
rv = pow(10.0, num/den);
return rv;
}
/* JND index value 1..1023 to L 0.05 .. 3993.404 cd/m^2 */
double icmDICOM_fwd(double jnd) {
if (jnd < 0.5)
jnd = 0.5;
if (jnd > 1024.0)
jnd = 1024.0;
return icmDICOM_fwd_nl(jnd);
}
/* L 0.05 .. 3993.404 cd/m^2 to JND index value 1..1023 */
/* This is not super accurate - typically to 0.03 .. 0.1 jne. */
static double icmDICOM_bwd_apx(double L) {
double A = 71.498068;
double B = 94.593053;
double C = 41.912053;
double D = 9.8247004;
double E = 0.28175407;
double F = -1.1878455;
double G = -0.18014349;
double H = 0.14710899;
double I = -0.017046845;
double rv, LL;
if (L < 0.049982) { /* == jnd 0.5 */
return 0.5;
}
if (L > 4019.354716) /* == jnd 1024 */
L = 4019.354716;
LL = L = log10(L);
rv = A;
rv += B * LL;
LL *= L;
rv += C * LL;
LL *= L;
rv += D * LL;
LL *= L;
rv += E * LL;
LL *= L;
rv += F * LL;
LL *= L;
rv += G * LL;
LL *= L;
rv += H * LL;
LL *= L;
rv += I * LL;
return rv;
}
/* L 0.05 .. 3993.404 cd/m^2 to JND index value 1..1023 */
/* Polish the aproximate solution twice using Newton's itteration */
double icmDICOM_bwd(double L) {
double rv, Lc, prv, pLc, de;
int i;
if (L < 0.045848) /* == jnd 0.5 */
L = 0.045848;
if (L > 4019.354716) /* == jnd 1024 */
L = 4019.354716;
/* Approx solution */
rv = icmDICOM_bwd_apx(L);
/* Compute aprox derivative */
Lc = icmDICOM_fwd_nl(rv);
prv = rv + 0.01;
pLc = icmDICOM_fwd_nl(prv);
do {
de = (rv - prv)/(Lc - pLc);
prv = rv;
rv -= (Lc - L) * de;
pLc = Lc;
Lc = icmDICOM_fwd_nl(rv);
} while (fabs(Lc - L) > 1e-8);
return rv;
}
/* =============================================================== */
/* Object for computing RFC 1321 MD5 checksums. */
/* Derived from Colin Plumb's 1993 public domain code. */
/* Reset the checksum */
static void icmMD5_reset(icmMD5 *p) {
p->tlen = 0;
p->sum[0] = 0x67452301;
p->sum[1] = 0xefcdab89;
p->sum[2] = 0x98badcfe;
p->sum[3] = 0x10325476;
p->fin = 0;
}
#define F1(x, y, z) (z ^ (x & (y ^ z)))
#define F2(x, y, z) F1(z, x, y)
#define F3(x, y, z) (x ^ y ^ z)
#define F4(x, y, z) (y ^ (x | ~z))
#define MD5STEP(f, w, x, y, z, pp, xtra, s) \
data = (pp)[0] + ((pp)[3] << 24) + ((pp)[2] << 16) + ((pp)[1] << 8); \
w += f(x, y, z) + data + xtra; \
w = (w << s) | (w >> (32-s)); \
w += x;
/* Add another 64 bytes to the checksum */
static void icmMD5_accume(icmMD5 *p, ORD8 *in) {
ORD32 data, a, b, c, d;
a = p->sum[0];
b = p->sum[1];
c = p->sum[2];
d = p->sum[3];
MD5STEP(F1, a, b, c, d, in + (4 * 0), 0xd76aa478, 7);
MD5STEP(F1, d, a, b, c, in + (4 * 1), 0xe8c7b756, 12);
MD5STEP(F1, c, d, a, b, in + (4 * 2), 0x242070db, 17);
MD5STEP(F1, b, c, d, a, in + (4 * 3), 0xc1bdceee, 22);
MD5STEP(F1, a, b, c, d, in + (4 * 4), 0xf57c0faf, 7);
MD5STEP(F1, d, a, b, c, in + (4 * 5), 0x4787c62a, 12);
MD5STEP(F1, c, d, a, b, in + (4 * 6), 0xa8304613, 17);
MD5STEP(F1, b, c, d, a, in + (4 * 7), 0xfd469501, 22);
MD5STEP(F1, a, b, c, d, in + (4 * 8), 0x698098d8, 7);
MD5STEP(F1, d, a, b, c, in + (4 * 9), 0x8b44f7af, 12);
MD5STEP(F1, c, d, a, b, in + (4 * 10), 0xffff5bb1, 17);
MD5STEP(F1, b, c, d, a, in + (4 * 11), 0x895cd7be, 22);
MD5STEP(F1, a, b, c, d, in + (4 * 12), 0x6b901122, 7);
MD5STEP(F1, d, a, b, c, in + (4 * 13), 0xfd987193, 12);
MD5STEP(F1, c, d, a, b, in + (4 * 14), 0xa679438e, 17);
MD5STEP(F1, b, c, d, a, in + (4 * 15), 0x49b40821, 22);
MD5STEP(F2, a, b, c, d, in + (4 * 1), 0xf61e2562, 5);
MD5STEP(F2, d, a, b, c, in + (4 * 6), 0xc040b340, 9);
MD5STEP(F2, c, d, a, b, in + (4 * 11), 0x265e5a51, 14);
MD5STEP(F2, b, c, d, a, in + (4 * 0), 0xe9b6c7aa, 20);
MD5STEP(F2, a, b, c, d, in + (4 * 5), 0xd62f105d, 5);
MD5STEP(F2, d, a, b, c, in + (4 * 10), 0x02441453, 9);
MD5STEP(F2, c, d, a, b, in + (4 * 15), 0xd8a1e681, 14);
MD5STEP(F2, b, c, d, a, in + (4 * 4), 0xe7d3fbc8, 20);
MD5STEP(F2, a, b, c, d, in + (4 * 9), 0x21e1cde6, 5);
MD5STEP(F2, d, a, b, c, in + (4 * 14), 0xc33707d6, 9);
MD5STEP(F2, c, d, a, b, in + (4 * 3), 0xf4d50d87, 14);
MD5STEP(F2, b, c, d, a, in + (4 * 8), 0x455a14ed, 20);
MD5STEP(F2, a, b, c, d, in + (4 * 13), 0xa9e3e905, 5);
MD5STEP(F2, d, a, b, c, in + (4 * 2), 0xfcefa3f8, 9);
MD5STEP(F2, c, d, a, b, in + (4 * 7), 0x676f02d9, 14);
MD5STEP(F2, b, c, d, a, in + (4 * 12), 0x8d2a4c8a, 20);
MD5STEP(F3, a, b, c, d, in + (4 * 5), 0xfffa3942, 4);
MD5STEP(F3, d, a, b, c, in + (4 * 8), 0x8771f681, 11);
MD5STEP(F3, c, d, a, b, in + (4 * 11), 0x6d9d6122, 16);
MD5STEP(F3, b, c, d, a, in + (4 * 14), 0xfde5380c, 23);
MD5STEP(F3, a, b, c, d, in + (4 * 1), 0xa4beea44, 4);
MD5STEP(F3, d, a, b, c, in + (4 * 4), 0x4bdecfa9, 11);
MD5STEP(F3, c, d, a, b, in + (4 * 7), 0xf6bb4b60, 16);
MD5STEP(F3, b, c, d, a, in + (4 * 10), 0xbebfbc70, 23);
MD5STEP(F3, a, b, c, d, in + (4 * 13), 0x289b7ec6, 4);
MD5STEP(F3, d, a, b, c, in + (4 * 0), 0xeaa127fa, 11);
MD5STEP(F3, c, d, a, b, in + (4 * 3), 0xd4ef3085, 16);
MD5STEP(F3, b, c, d, a, in + (4 * 6), 0x04881d05, 23);
MD5STEP(F3, a, b, c, d, in + (4 * 9), 0xd9d4d039, 4);
MD5STEP(F3, d, a, b, c, in + (4 * 12), 0xe6db99e5, 11);
MD5STEP(F3, c, d, a, b, in + (4 * 15), 0x1fa27cf8, 16);
MD5STEP(F3, b, c, d, a, in + (4 * 2), 0xc4ac5665, 23);
MD5STEP(F4, a, b, c, d, in + (4 * 0), 0xf4292244, 6);
MD5STEP(F4, d, a, b, c, in + (4 * 7), 0x432aff97, 10);
MD5STEP(F4, c, d, a, b, in + (4 * 14), 0xab9423a7, 15);
MD5STEP(F4, b, c, d, a, in + (4 * 5), 0xfc93a039, 21);
MD5STEP(F4, a, b, c, d, in + (4 * 12), 0x655b59c3, 6);
MD5STEP(F4, d, a, b, c, in + (4 * 3), 0x8f0ccc92, 10);
MD5STEP(F4, c, d, a, b, in + (4 * 10), 0xffeff47d, 15);
MD5STEP(F4, b, c, d, a, in + (4 * 1), 0x85845dd1, 21);
MD5STEP(F4, a, b, c, d, in + (4 * 8), 0x6fa87e4f, 6);
MD5STEP(F4, d, a, b, c, in + (4 * 15), 0xfe2ce6e0, 10);
MD5STEP(F4, c, d, a, b, in + (4 * 6), 0xa3014314, 15);
MD5STEP(F4, b, c, d, a, in + (4 * 13), 0x4e0811a1, 21);
MD5STEP(F4, a, b, c, d, in + (4 * 4), 0xf7537e82, 6);
MD5STEP(F4, d, a, b, c, in + (4 * 11), 0xbd3af235, 10);
MD5STEP(F4, c, d, a, b, in + (4 * 2), 0x2ad7d2bb, 15);
MD5STEP(F4, b, c, d, a, in + (4 * 9), 0xeb86d391, 21);
p->sum[0] += a;
p->sum[1] += b;
p->sum[2] += c;
p->sum[3] += d;
}
#undef F1
#undef F2
#undef F3
#undef F4
#undef MD5STEP
/* Add some bytes */
static void icmMD5_add(icmMD5 *p, ORD8 *ibuf, unsigned int len) {
unsigned int bs;
if (p->fin)
return; /* This is actually an error */
bs = p->tlen; /* Current bytes added */
p->tlen = bs + len; /* Update length after adding this buffer */
bs &= 0x3f; /* Bytes already in buffer */
/* Deal with any existing partial bytes in p->buf */
if (bs) {
ORD8 *np = (ORD8 *)p->buf + bs; /* Next free location in partial buffer */
bs = 64 - bs; /* Free space in partial buffer */
if (len < bs) { /* Not enought new to make a full buffer */
memmove(np, ibuf, len);
return;
}
memmove(np, ibuf, bs); /* Now got one full buffer */
icmMD5_accume(p, np);
ibuf += bs;
len -= bs;
}
/* Deal with input data 64 bytes at a time */
while (len >= 64) {
icmMD5_accume(p, ibuf);
ibuf += 64;
len -= 64;
}
/* Deal with any remaining bytes */
memmove(p->buf, ibuf, len);
}
/* Finalise the checksum and return the result. */
static void icmMD5_get(icmMD5 *p, ORD8 chsum[16]) {
int i;
unsigned count;
ORD32 bits1, bits0;
ORD8 *pp;
if (p->fin == 0) {
/* Compute number of bytes processed mod 64 */
count = p->tlen & 0x3f;
/* Set the first char of padding to 0x80. This is safe since there is
always at least one byte free */
pp = p->buf + count;
*pp++ = 0x80;
/* Bytes of padding needed to make 64 bytes */
count = 64 - 1 - count;
/* Pad out to 56 mod 64, allowing 8 bytes for length in bits. */
if (count < 8) { /* Not enough space for padding and length */
memset(pp, 0, count);
icmMD5_accume(p, p->buf);
/* Now fill the next block with 56 bytes */
memset(p->buf, 0, 56);
} else {
/* Pad block to 56 bytes */
memset(pp, 0, count - 8);
}
/* Compute number of bits */
bits1 = 0x7 & (p->tlen >> (32 - 3));
bits0 = p->tlen << 3;
/* Append number of bits */
p->buf[64 - 8] = bits0 & 0xff;
p->buf[64 - 7] = (bits0 >> 8) & 0xff;
p->buf[64 - 6] = (bits0 >> 16) & 0xff;
p->buf[64 - 5] = (bits0 >> 24) & 0xff;
p->buf[64 - 4] = bits1 & 0xff;
p->buf[64 - 3] = (bits1 >> 8) & 0xff;
p->buf[64 - 2] = (bits1 >> 16) & 0xff;
p->buf[64 - 1] = (bits1 >> 24) & 0xff;
icmMD5_accume(p, p->buf);
p->fin = 1;
}
/* Return the result, lsb to msb */
pp = chsum;
for (i = 0; i < 4; i++) {
*pp++ = p->sum[i] & 0xff;
*pp++ = (p->sum[i] >> 8) & 0xff;
*pp++ = (p->sum[i] >> 16) & 0xff;
*pp++ = (p->sum[i] >> 24) & 0xff;
}
}
/* Delete the instance */
static void icmMD5_del(icmMD5 *p) {
icmAlloc *al = p->al;
int del_al = p->del_al;
/* This object */
al->free(al, p);
if (del_al) /* We are responsible for deleting allocator */
al->del(al);
}
/* Create a new MD5 checksumming object, with a reset checksum value */
/* Return it or NULL if there is an error */
icmMD5 *new_icmMD5_a(icmAlloc *al) {
icmMD5 *p;
if ((p = (icmMD5 *)al->calloc(al,1,sizeof(icmMD5))) == NULL)
return NULL;
p->al = al;
p->reset = icmMD5_reset;
p->add = icmMD5_add;
p->get = icmMD5_get;
p->del = icmMD5_del;
p->reset(p);
return p;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - */
/* Dumy icmFile used to compute MD5 checksum on write */
/* Get the size of the file (Only valid for reading file. */
static size_t icmFileMD5_get_size(icmFile *pp) {
icmFileMD5 *p = (icmFileMD5 *)pp;
return p->size;
}
/* Set current position to offset. Return 0 on success, nz on failure. */
/* Seek can't be supported for MD5, so and seek must be to current location. */
static int icmFileMD5_seek(
icmFile *pp,
unsigned int offset
) {
icmFileMD5 *p = (icmFileMD5 *)pp;
if (p->of != offset) {
p->errc = 1;
}
if (p->of > p->size)
p->size = p->of;
return 0;
}
/* Read count items of size length. Return number of items successfully read. */
/* Read is not implemented */
static size_t icmFileMD5_read(
icmFile *pp,
void *buffer,
size_t size,
size_t count
) {
return 0;
}
/* write count items of size length. Return number of items successfully written. */
/* Simply pass to MD5 to compute checksum */
static size_t icmFileMD5_write(
icmFile *pp,
void *buffer,
size_t size,
size_t count
) {
icmFileMD5 *p = (icmFileMD5 *)pp;
size_t len = size * count;
p->md5->add(p->md5, (ORD8 *)buffer, len);
p->of += len;
if (p->of > p->size)
p->size = p->of;
return count;
}
/* do a printf */
/* Not implemented */
static int icmFileMD5_printf(
icmFile *pp,
const char *format,
...
) {
icmFileMD5 *p = (icmFileMD5 *)pp;
p->errc = 2;
return 0;
}
/* flush all write data out to secondary storage. Return nz on failure. */
static int icmFileMD5_flush(
icmFile *pp
) {
return 0;
}
/* we're done with the file object, return nz on failure */
static int icmFileMD5_delete(
icmFile *pp
) {
icmFileMD5 *p = (icmFileMD5 *)pp;
p->al->free(p->al, p); /* Free object */
return 0;
}
/* Return the error code. Error code will usually be set */
/* if we did a seek to other than the current location, */
/* or did a printf. */
static int icmFileMD5_geterrc(
icmFile *pp
) {
icmFileMD5 *p = (icmFileMD5 *)pp;
return p->errc;
}
/* Create a checksum dump file access class with allocator */
icmFile *new_icmFileMD5_a(
icmMD5 *md5, /* MD5 object to use */
icmAlloc *al /* heap allocator */
) {
icmFileMD5 *p;
if ((p = (icmFileMD5 *) al->calloc(al, 1, sizeof(icmFileMD5))) == NULL) {
return NULL;
}
p->md5 = md5; /* MD5 compute object */
p->al = al; /* Heap allocator */
p->get_size = icmFileMD5_get_size;
p->seek = icmFileMD5_seek;
p->read = icmFileMD5_read;
p->write = icmFileMD5_write;
p->gprintf = icmFileMD5_printf;
p->flush = icmFileMD5_flush;
p->del = icmFileMD5_delete;
p->get_errc = icmFileMD5_geterrc;
p->of = 0;
p->errc = 0;
return (icmFile *)p;
}
/* ============================================= */
/* Implementation of color transform lookups. */
/* - - - - - - - - - - - - - - - - - - - - - - - */
/* Methods common to all transforms (icmLuBase) : */
/* Return information about the native lut in/out/pcs colorspaces. */
/* Any pointer may be NULL if value is not to be returned */
static void
icmLutSpaces(
struct _icmLuBase *p, /* This */
icColorSpaceSignature *ins, /* Return Native input color space */
int *inn, /* Return number of input components */
icColorSpaceSignature *outs, /* Return Native output color space */
int *outn, /* Return number of output components */
icColorSpaceSignature *pcs /* Return Native PCS color space */
/* (this will be the same as ins or outs */
/* depending on the lookup direction) */
) {
if (ins != NULL)
*ins = p->inSpace;
if (inn != NULL)
*inn = (int)number_ColorSpaceSignature(p->inSpace);
if (outs != NULL)
*outs = p->outSpace;
if (outn != NULL)
*outn = (int)number_ColorSpaceSignature(p->outSpace);
if (pcs != NULL)
*pcs = p->pcs;
}
/* Return information about the effective lookup in/out colorspaces, */
/* including allowance for PCS override. */
/* Any pointer may be NULL if value is not to be returned */
static void
icmLuSpaces(
struct _icmLuBase *p, /* This */
icColorSpaceSignature *ins, /* Return effective input color space */
int *inn, /* Return number of input components */
icColorSpaceSignature *outs, /* Return effective output color space */
int *outn, /* Return number of output components */
icmLuAlgType *alg, /* Return type of lookup algorithm used */
icRenderingIntent *intt, /* Return the intent being implented */
icmLookupFunc *fnc, /* Return the profile function being implemented */
icColorSpaceSignature *pcs, /* Return the profile effective PCS */
icmLookupOrder *ord /* return the search Order */
) {
if (ins != NULL)
*ins = p->e_inSpace;
if (inn != NULL)
*inn = (int)number_ColorSpaceSignature(p->e_inSpace);
if (outs != NULL)
*outs = p->e_outSpace;
if (outn != NULL)
*outn = (int)number_ColorSpaceSignature(p->e_outSpace);
if (alg != NULL)
*alg = p->ttype;
if (intt != NULL)
*intt = p->intent;
if (fnc != NULL)
*fnc = p->function;
if (pcs != NULL)
*pcs = p->e_pcs;
if (ord != NULL)
*ord = p->order;
}
/* Relative to Absolute for this WP in XYZ */
static void icmLuXYZ_Rel2Abs(icmLuBase *p, double *out, double *in) {
icmMulBy3x3(out, p->toAbs, in);
}
/* Absolute to Relative for this WP in XYZ */
static void icmLuXYZ_Abs2Rel(icmLuBase *p, double *out, double *in) {
icmMulBy3x3(out, p->fromAbs, in);
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* Methods common to all non-named transforms (icmLuBase) : */
/* Initialise the LU white and black points from the ICC tags, */
/* and the corresponding absolute<->relative conversion matrices */
/* return nz on error */
static int icmLuInit_Wh_bk(
struct _icmLuBase *lup
) {
icmXYZArray *whitePointTag, *blackPointTag;
icc *p = lup->icp;
if ((whitePointTag = (icmXYZArray *)p->read_tag(p, icSigMediaWhitePointTag)) == NULL
|| whitePointTag->ttype != icSigXYZType || whitePointTag->size < 1) {
if (p->header->deviceClass != icSigLinkClass
&& (lup->intent == icAbsoluteColorimetric
|| lup->intent == icmAbsolutePerceptual
|| lup->intent == icmAbsoluteSaturation)) {
sprintf(p->err,"icc_lookup: Profile is missing Media White Point Tag");
p->errc = 1;
return 1;
}
p->err[0] = '\000';
p->errc = 0;
lup->whitePoint = icmD50; /* safe value */
} else
lup->whitePoint = whitePointTag->data[0]; /* Copy structure */
if ((blackPointTag = (icmXYZArray *)p->read_tag(p, icSigMediaBlackPointTag)) == NULL
|| blackPointTag->ttype != icSigXYZType || blackPointTag->size < 1) {
p->err[0] = '\000';
p->errc = 0;
lup->blackPoint = icmBlack; /* default */
lup->blackisassumed = 1; /* We assumed the default */
} else {
lup->blackPoint = blackPointTag->data[0]; /* Copy structure */
lup->blackisassumed = 0; /* The black is from the tag */
}
/* If this is a Display profile, check if there is a 'chad' tag, then */
/* setup the white point and toAbs/fromAbs matricies from that, so as to implement an */
/* effective Absolute Colorimetric intent for such profiles. */
if (p->header->deviceClass == icSigDisplayClass
&& p->naturalChad && p->chadmxValid) {
double wp[3];
/* Conversion matrix is chad matrix. */
icmCpy3x3(lup->fromAbs, p->chadmx);
icmInverse3x3(lup->toAbs, lup->fromAbs);
/* Compute absolute white point. We deliberately ignore what's in the white point tag */
/* and assume D50, since dealing with a non-D50 white point tag is contrary to ICCV4 */
/* and full of ambiguity (i.e. is it a separate "media" white different to the */
/* display white and not D50, or has the profile creator mistakenly put the display */
/* white in the white point tag ?) */
icmMulBy3x3(wp, lup->toAbs, icmD50_ary3);
icmAry2XYZ(lup->whitePoint, wp);
DBLLL(("toAbs and fromAbs created from 'chad' tag\n"));
DBLLL(("computed wp %.8f %.8f %.8f\n", lup->whitePoint.X,
lup->whitePoint.Y, lup->whitePoint.Z));
/* If this is an Output profile, check if there is a 'chad' tag, and */
/* setup the toAbs/fromAbs matricies so that they include it, so as to implement an */
/* effective Absolute Colorimetric intent for such profiles. */
} else if (p->header->deviceClass == icSigOutputClass
&& p->naturalChad && p->chadmxValid) {
double wp[3];
double ichad[3][3];
/* Convert the white point tag value backwards through the 'chad' */
icmXYZ2Ary(wp, lup->whitePoint);
icmInverse3x3(ichad, p->chadmx);
icmMulBy3x3(wp, ichad, wp);
icmAry2XYZ(lup->whitePoint, wp);
/* Create absolute <-> relative conversion matricies */
p->chromAdaptMatrix(p, ICM_CAM_NONE, lup->toAbs, lup->fromAbs, icmD50, lup->whitePoint);
DBLLL(("toAbs and fromAbs created from 'chad' tag & WP tag\n"));
DBLLL(("toAbs and fromAbs created from wp %f %f %f and D50 %f %f %f\n", lup->whitePoint.X,
lup->whitePoint.Y, lup->whitePoint.Z, icmD50.X, icmD50.Y, icmD50.Z));
} else {
/* Create absolute <-> relative conversion matricies */
p->chromAdaptMatrix(p, ICM_CAM_NONE, lup->toAbs, lup->fromAbs, icmD50, lup->whitePoint);
DBLLL(("toAbs and fromAbs created from wp %f %f %f and D50 %f %f %f\n", lup->whitePoint.X,
lup->whitePoint.Y, lup->whitePoint.Z, icmD50.X, icmD50.Y, icmD50.Z));
}
DBLLL(("toAbs = %f %f %f\n %f %f %f\n %f %f %f\n",
lup->toAbs[0][0], lup->toAbs[0][1], lup->toAbs[0][2],
lup->toAbs[1][0], lup->toAbs[1][1], lup->toAbs[1][2],
lup->toAbs[2][0], lup->toAbs[2][1], lup->toAbs[2][2]));
DBLLL(("fromAbs = %f %f %f\n %f %f %f\n %f %f %f\n",
lup->fromAbs[0][0], lup->fromAbs[0][1], lup->fromAbs[0][2],
lup->fromAbs[1][0], lup->fromAbs[1][1], lup->fromAbs[1][2],
lup->fromAbs[2][0], lup->fromAbs[2][1], lup->fromAbs[2][2]));
return 0;
}
/* Return the media white and black points in absolute XYZ space. */
/* Note that if not in the icc, the black point will be returned as 0, 0, 0, */
/* and the function will return nz. */
/* Any pointer may be NULL if value is not to be returned */
static int icmLuWh_bk_points(
struct _icmLuBase *p,
double *wht,
double *blk
) {
if (wht != NULL) {
icmXYZ2Ary(wht,p->whitePoint);
}
if (blk != NULL) {
icmXYZ2Ary(blk,p->blackPoint);
}
if (p->blackisassumed)
return 1;
return 0;
}
/* Get the LU white and black points in LU PCS space, converted to XYZ. */
/* (ie. white and black will be relative if LU is relative intent etc.) */
/* Return nz if the black point is being assumed to be 0,0,0 rather */
/* than being from the tag. */ \
static int icmLuLu_wh_bk_points(
struct _icmLuBase *p,
double *wht,
double *blk
) {
if (wht != NULL) {
icmXYZ2Ary(wht,p->whitePoint);
}
if (blk != NULL) {
icmXYZ2Ary(blk,p->blackPoint);
}
if (p->intent != icAbsoluteColorimetric
&& p->intent != icmAbsolutePerceptual
&& p->intent != icmAbsoluteSaturation) {
if (wht != NULL)
icmMulBy3x3(wht, p->fromAbs, wht);
if (blk != NULL)
icmMulBy3x3(blk, p->fromAbs, blk);
}
if (p->blackisassumed)
return 1;
return 0;
}
/* Get the native (internal) ranges for the Monochrome or Matrix profile */
/* Arguments may be NULL */
static void
icmLu_get_lutranges (
struct _icmLuBase *p,
double *inmin, double *inmax, /* Return maximum range of inspace values */
double *outmin, double *outmax /* Return maximum range of outspace values */
) {
icTagTypeSignature tagType;
if (p->ttype == icmLutType) {
icmLuLut *pp = (icmLuLut *)p;
tagType = pp->lut->ttype;
} else {
tagType = icMaxEnumType;
}
/* Hmm. we have no way of handling an error from getRange. */
/* It shouldn't ever return one unless there is a mismatch between */
/* getRange and Lu creation... */
getRange(p->icp, p->inSpace, tagType, inmin, inmax);
getRange(p->icp, p->outSpace, tagType, outmin, outmax);
}
/* Get the effective (externally visible) ranges for the all profile types */
/* Arguments may be NULL */
static void
icmLu_get_ranges (
struct _icmLuBase *p,
double *inmin, double *inmax, /* Return maximum range of inspace values */
double *outmin, double *outmax /* Return maximum range of outspace values */
) {
icTagTypeSignature tagType;
if (p->ttype == icmLutType) {
icmLuLut *pp = (icmLuLut *)p;
tagType = pp->lut->ttype;
} else {
tagType = icMaxEnumType;
}
/* Hmm. we have no way of handling an error from getRange. */
/* It shouldn't ever return one unless there is a mismatch between */
/* getRange and Lu creation... */
getRange(p->icp, p->e_inSpace, tagType, inmin, inmax);
getRange(p->icp, p->e_outSpace, tagType, outmin, outmax);
}
/* - - - - - - - - - - - - - - - - - - - - - - - - */
/* Forward and Backward Monochrome type methods: */
/* Return 0 on success, 1 if clipping occured, 2 on other error */
/* Individual components of Fwd conversion: */
/* Actual device to linearised device */
static int
icmLuMonoFwd_curve (
icmLuMono *p, /* This */
double *out, /* Vector of output values */
double *in /* Vector of input values */
) {
icc *icp = p->icp;
int rv = 0;
/* Translate from device to PCS scale */
if ((rv |= p->grayCurve->lookup_fwd(p->grayCurve,&out[0],&in[0])) > 1) {
sprintf(icp->err,"icc_lookup: Curve->lookup_fwd() failed");
icp->errc = rv;
return 2;
}
return rv;
}
/* Linearised device to relative PCS */
static int
icmLuMonoFwd_map (
icmLuMono *p, /* This */
double *out, /* Vector of output values (native space) */
double *in /* Vector of input values (native space) */
) {
int rv = 0;
double Y = in[0]; /* In case out == in */
out[0] = p->pcswht.X;
out[1] = p->pcswht.Y;
out[2] = p->pcswht.Z;
if (p->pcs == icSigLabData)
icmXYZ2Lab(&p->pcswht, out, out); /* in Lab */
/* Scale linearized device level to PCS white */
out[0] *= Y;
out[1] *= Y;
out[2] *= Y;
return rv;
}
/* relative PCS to absolute PCS (if required) */
static int
icmLuMonoFwd_abs ( /* Abs comes last in Fwd conversion */
icmLuMono *p, /* This */
double *out, /* Vector of output values in Effective PCS */
double *in /* Vector of input values in Native PCS */
) {
int rv = 0;
if (out != in) { /* Don't alter input values */
out[0] = in[0];
out[1] = in[1];
out[2] = in[2];
}
/* Do absolute conversion */
if (p->intent == icAbsoluteColorimetric
|| p->intent == icmAbsolutePerceptual
|| p->intent == icmAbsoluteSaturation) {
if (p->pcs == icSigLabData) /* Convert L to Y */
icmLab2XYZ(&p->pcswht, out, out);
/* Convert from Relative to Absolute colorimetric */
icmMulBy3x3(out, p->toAbs, out);
if (p->e_pcs == icSigLabData)
icmXYZ2Lab(&p->pcswht, out, out);
} else {
/* Convert from Native to Effective output space */
if (p->pcs == icSigLabData && p->e_pcs == icSigXYZData)
icmLab2XYZ(&p->pcswht, out, out);
else if (p->pcs == icSigXYZData && p->e_pcs == icSigLabData)
icmXYZ2Lab(&p->pcswht, out, out);
}
return rv;
}
/* Overall Fwd conversion routine (Dev->PCS) */
static int
icmLuMonoFwd_lookup (
icmLuBase *pp, /* This */
double *out, /* Vector of output values */
double *in /* Input value */
) {
int rv = 0;
icmLuMono *p = (icmLuMono *)pp;
rv |= icmLuMonoFwd_curve(p, out, in);
rv |= icmLuMonoFwd_map(p, out, out);
rv |= icmLuMonoFwd_abs(p, out, out);
return rv;
}
/* Three stage conversion routines */
static int
icmLuMonoFwd_lookup_in(
icmLuBase *pp, /* This */
double *out, /* Output value */
double *in /* Vector of input values */
) {
int rv = 0;
icmLuMono *p = (icmLuMono *)pp;
rv |= icmLuMonoFwd_curve(p, out, in);
return rv;
}
static int
icmLuMonoFwd_lookup_core(
icmLuBase *pp, /* This */
double *out, /* Output value */
double *in /* Vector of input values */
) {
int rv = 0;
icmLuMono *p = (icmLuMono *)pp;
rv |= icmLuMonoFwd_map(p, out, in);
rv |= icmLuMonoFwd_abs(p, out, out);
return rv;
}
static int
icmLuMonoFwd_lookup_out(
icmLuBase *pp, /* This */
double *out, /* Output value */
double *in /* Vector of input values */
) {
int rv = 0;
out[0] = in[0];
out[1] = in[1];
out[2] = in[2];
return rv;
}
/* - - - - - - - - - - - - - - */
/* Individual components of Bwd conversion: */
/* Convert from relative PCS to absolute PCS (if required) */
static int
icmLuMonoBwd_abs ( /* Abs comes first in Bwd conversion */
icmLuMono *p, /* This */
double *out, /* Vector of output values in Native PCS */
double *in /* Vector of input values in Effective PCS */
) {
int rv = 0;
if (out != in) { /* Don't alter input values */
out[0] = in[0];
out[1] = in[1];
out[2] = in[2];
}
/* Force to monochrome locus in correct space */
if (p->e_pcs == icSigLabData) {
double wp[3];
if (p->intent == icAbsoluteColorimetric
|| p->intent == icmAbsolutePerceptual
|| p->intent == icmAbsoluteSaturation) {
wp[0] = p->whitePoint.X;
wp[1] = p->whitePoint.Y;
wp[2] = p->whitePoint.Z;
} else {
wp[0] = p->pcswht.X;
wp[1] = p->pcswht.Y;
wp[2] = p->pcswht.Z;
}
icmXYZ2Lab(&p->pcswht, wp, wp); /* Convert to Lab white point */
out[1] = out[0]/wp[0] * wp[1];
out[2] = out[0]/wp[0] * wp[2];
} else {
if (p->intent == icAbsoluteColorimetric
|| p->intent == icmAbsolutePerceptual
|| p->intent == icmAbsoluteSaturation) {
out[0] = out[1]/p->whitePoint.Y * p->whitePoint.X;
out[2] = out[1]/p->whitePoint.Y * p->whitePoint.Z;
} else {
out[0] = out[1]/p->pcswht.Y * p->pcswht.X;
out[2] = out[1]/p->pcswht.Y * p->pcswht.Z;
}
}
/* Do absolute conversion, and conversion to effective PCS */
if (p->intent == icAbsoluteColorimetric
|| p->intent == icmAbsolutePerceptual
|| p->intent == icmAbsoluteSaturation) {
if (p->e_pcs == icSigLabData)
icmLab2XYZ(&p->pcswht, out, out);
icmMulBy3x3(out, p->fromAbs, out);
/* Convert from Effective to Native input space */
if (p->pcs == icSigLabData)
icmXYZ2Lab(&p->pcswht, out, out);
} else {
/* Convert from Effective to Native input space */
if (p->e_pcs == icSigLabData && p->pcs == icSigXYZData)
icmLab2XYZ(&p->pcswht, out, out);
else if (p->e_pcs == icSigXYZData && p->pcs == icSigLabData)
icmXYZ2Lab(&p->pcswht, out, out);
}
return rv;
}
/* Map from relative PCS to linearised device */
static int
icmLuMonoBwd_map (
icmLuMono *p, /* This */
double *out, /* Output value */
double *in /* Vector of input values (native space) */
) {
int rv = 0;
double pcsw[3];
pcsw[0] = p->pcswht.X;
pcsw[1] = p->pcswht.Y;
pcsw[2] = p->pcswht.Z;
if (p->pcs == icSigLabData)
icmXYZ2Lab(&p->pcswht, pcsw, pcsw); /* in Lab (should be 100.0!) */
/* Divide linearized device level into PCS white luminence */
if (p->pcs == icSigLabData)
out[0] = in[0]/pcsw[0];
else
out[0] = in[1]/pcsw[1];
return rv;
}
/* Map from linearised device to actual device */
static int
icmLuMonoBwd_curve (
icmLuMono *p, /* This */
double *out, /* Output value */
double *in /* Input value */
) {
icc *icp = p->icp;
int rv = 0;
/* Convert to device value through curve */
if ((rv = p->grayCurve->lookup_bwd(p->grayCurve,&out[0],&in[0])) > 1) {
sprintf(icp->err,"icc_lookup: Curve->lookup_bwd() failed");
icp->errc = rv;
return 2;
}
return rv;
}
/* Overall Bwd conversion routine (PCS->Dev) */
static int
icmLuMonoBwd_lookup (
icmLuBase *pp, /* This */
double *out, /* Output value */
double *in /* Vector of input values */
) {
double temp[3];
int rv = 0;
icmLuMono *p = (icmLuMono *)pp;
rv |= icmLuMonoBwd_abs(p, temp, in);
rv |= icmLuMonoBwd_map(p, out, temp);
rv |= icmLuMonoBwd_curve(p, out, out);
return rv;
}
/* Three stage conversion routines */
static int
icmLuMonoBwd_lookup_in(
icmLuBase *pp, /* This */
double *out, /* Output value */
double *in /* Vector of input values */
) {
int rv = 0;
out[0] = in[0];
out[1] = in[1];
out[2] = in[2];
return rv;
}
static int
icmLuMonoBwd_lookup_core(
icmLuBase *pp, /* This */
double *out, /* Output value */
double *in /* Vector of input values */
) {
double temp[3];
int rv = 0;
icmLuMono *p = (icmLuMono *)pp;
rv |= icmLuMonoBwd_abs(p, temp, in);
rv |= icmLuMonoBwd_map(p, out, temp);
return rv;
}
static int
icmLuMonoBwd_lookup_out(
icmLuBase *pp, /* This */
double *out, /* Output value */
double *in /* Vector of input values */
) {
int rv = 0;
icmLuMono *p = (icmLuMono *)pp;
rv |= icmLuMonoBwd_curve(p, out, in);
return rv;
}
/* - - - - - - - - - - - - - - */
static void
icmLuMono_delete(
icmLuBase *p
) {
icc *icp = p->icp;
icp->al->free(icp->al, p);
}
static icmLuBase *
new_icmLuMono(
struct _icc *icp,
icColorSpaceSignature inSpace, /* Native Input color space */
icColorSpaceSignature outSpace, /* Native Output color space */
icColorSpaceSignature pcs, /* Native PCS */
icColorSpaceSignature e_inSpace, /* Effective Input color space */
icColorSpaceSignature e_outSpace, /* Effective Output color space */
icColorSpaceSignature e_pcs, /* Effective PCS */
icRenderingIntent intent, /* Rendering intent */
icmLookupFunc func, /* Functionality requested */
int dir /* 0 = fwd, 1 = bwd */
) {
icmLuMono *p;
if ((p = (icmLuMono *) icp->al->calloc(icp->al,1,sizeof(icmLuMono))) == NULL)
return NULL;
p->icp = icp;
p->del = icmLuMono_delete;
p->lutspaces= icmLutSpaces;
p->spaces = icmLuSpaces;
p->XYZ_Rel2Abs = icmLuXYZ_Rel2Abs;
p->XYZ_Abs2Rel = icmLuXYZ_Abs2Rel;
p->get_lutranges = icmLu_get_lutranges;
p->get_ranges = icmLu_get_ranges;
p->init_wh_bk = icmLuInit_Wh_bk;
p->wh_bk_points = icmLuWh_bk_points;
p->lu_wh_bk_points = icmLuLu_wh_bk_points;
p->fwd_lookup = icmLuMonoFwd_lookup;
p->fwd_curve = icmLuMonoFwd_curve;
p->fwd_map = icmLuMonoFwd_map;
p->fwd_abs = icmLuMonoFwd_abs;
p->bwd_lookup = icmLuMonoBwd_lookup;
p->bwd_abs = icmLuMonoFwd_abs;
p->bwd_map = icmLuMonoFwd_map;
p->bwd_curve = icmLuMonoFwd_curve;
if (dir) {
p->ttype = icmMonoBwdType;
p->lookup = icmLuMonoBwd_lookup;
p->lookup_in = icmLuMonoBwd_lookup_in;
p->lookup_core = icmLuMonoBwd_lookup_core;
p->lookup_out = icmLuMonoBwd_lookup_out;
p->lookup_inv_in = icmLuMonoFwd_lookup_out; /* Opposite of Bwd_lookup_in */
} else {
p->ttype = icmMonoFwdType;
p->lookup = icmLuMonoFwd_lookup;
p->lookup_in = icmLuMonoFwd_lookup_in;
p->lookup_core = icmLuMonoFwd_lookup_core;
p->lookup_out = icmLuMonoFwd_lookup_out;
p->lookup_inv_in = icmLuMonoBwd_lookup_out; /* Opposite of Fwd_lookup_in */
}
/* Lookup the white and black points */
if (p->init_wh_bk((icmLuBase *)p)) {
p->del((icmLuBase *)p);
return NULL;
}
/* See if the color spaces are appropriate for the mono type */
if (number_ColorSpaceSignature(icp->header->colorSpace) != 1
|| ( icp->header->pcs != icSigXYZData && icp->header->pcs != icSigLabData)) {
p->del((icmLuBase *)p);
return NULL;
}
/* Find the appropriate tags */
if ((p->grayCurve = (icmCurve *)icp->read_tag(icp, icSigGrayTRCTag)) == NULL
|| p->grayCurve->ttype != icSigCurveType) {
p->del((icmLuBase *)p);
return NULL;
}
p->pcswht = icp->header->illuminant;
p->intent = intent;
p->function = func;
p->inSpace = inSpace;
p->outSpace = outSpace;
p->pcs = pcs;
p->e_inSpace = e_inSpace;
p->e_outSpace = e_outSpace;
p->e_pcs = e_pcs;
return (icmLuBase *)p;
}
static icmLuBase *
new_icmLuMonoFwd(
struct _icc *icp,
icColorSpaceSignature inSpace, /* Native Input color space */
icColorSpaceSignature outSpace, /* Native Output color space */
icColorSpaceSignature pcs, /* Native PCS */
icColorSpaceSignature e_inSpace, /* Effective Input color space */
icColorSpaceSignature e_outSpace, /* Effective Output color space */
icColorSpaceSignature e_pcs, /* Effective PCS */
icRenderingIntent intent, /* Rendering intent */
icmLookupFunc func /* Functionality requested */
) {
return new_icmLuMono(icp, inSpace, outSpace, pcs, e_inSpace, e_outSpace, e_pcs,
intent, func, 0);
}
static icmLuBase *
new_icmLuMonoBwd(
struct _icc *icp,
icColorSpaceSignature inSpace, /* Native Input color space */
icColorSpaceSignature outSpace, /* Native Output color space */
icColorSpaceSignature pcs, /* Native PCS */
icColorSpaceSignature e_inSpace, /* Effective Input color space */
icColorSpaceSignature e_outSpace, /* Effective Output color space */
icColorSpaceSignature e_pcs, /* Effective PCS */
icRenderingIntent intent, /* Rendering intent */
icmLookupFunc func /* Functionality requested */
) {
return new_icmLuMono(icp, inSpace, outSpace, pcs, e_inSpace, e_outSpace, e_pcs,
intent, func, 1);
}
/* - - - - - - - - - - - - - - - - - - - - - - - */
/* Forward and Backward Matrix type conversion */
/* Return 0 on success, 1 if clipping occured, 2 on other error */
/* Individual components of Fwd conversion: */
static int
icmLuMatrixFwd_curve (
icmLuMatrix *p, /* This */
double *out, /* Vector of output values */
double *in /* Vector of input values */
) {
icc *icp = p->icp;
int rv = 0;
/* Curve lookups */
if ((rv |= p->redCurve->lookup_fwd( p->redCurve, &out[0],&in[0])) > 1
|| (rv |= p->greenCurve->lookup_fwd(p->greenCurve,&out[1],&in[1])) > 1
|| (rv |= p->blueCurve->lookup_fwd( p->blueCurve, &out[2],&in[2])) > 1) {
sprintf(icp->err,"icc_lookup: Curve->lookup_fwd() failed");
icp->errc = rv;
return 2;
}
return rv;
}
static int
icmLuMatrixFwd_matrix (
icmLuMatrix *p, /* This */
double *out, /* Vector of output values */
double *in /* Vector of input values */
) {
int rv = 0;
double tt[3];
/* Matrix */
tt[0] = p->mx[0][0] * in[0] + p->mx[0][1] * in[1] + p->mx[0][2] * in[2];
tt[1] = p->mx[1][0] * in[0] + p->mx[1][1] * in[1] + p->mx[1][2] * in[2];
tt[2] = p->mx[2][0] * in[0] + p->mx[2][1] * in[1] + p->mx[2][2] * in[2];
out[0] = tt[0];
out[1] = tt[1];
out[2] = tt[2];
return rv;
}
static int
icmLuMatrixFwd_abs (/* Abs comes last in Fwd conversion */
icmLuMatrix *p, /* This */
double *out, /* Vector of output values */
double *in /* Vector of input values */
) {
int rv = 0;
if (out != in) { /* Don't alter input values */
out[0] = in[0];
out[1] = in[1];
out[2] = in[2];
}
/* If required, convert from Relative to Absolute colorimetric */
if (p->intent == icAbsoluteColorimetric
|| p->intent == icmAbsolutePerceptual
|| p->intent == icmAbsoluteSaturation) {
icmMulBy3x3(out, p->toAbs, out);
}
/* If e_pcs is Lab, then convert XYZ to Lab */
if (p->e_pcs == icSigLabData)
icmXYZ2Lab(&p->pcswht, out, out);
return rv;
}
/* Overall Fwd conversion (Dev->PCS)*/
static int
icmLuMatrixFwd_lookup (
icmLuBase *pp, /* This */
double *out, /* Vector of output values */
double *in /* Vector of input values */
) {
int rv = 0;
icmLuMatrix *p = (icmLuMatrix *)pp;
rv |= icmLuMatrixFwd_curve(p, out, in);
rv |= icmLuMatrixFwd_matrix(p, out, out);
rv |= icmLuMatrixFwd_abs(p, out, out);
return rv;
}
/* Three stage conversion routines */
static int
icmLuMatrixFwd_lookup_in (
icmLuBase *pp, /* This */
double *out, /* Vector of output values */
double *in /* Vector of input values */
) {
int rv = 0;
icmLuMatrix *p = (icmLuMatrix *)pp;
rv |= icmLuMatrixFwd_curve(p, out, in);
return rv;
}
static int
icmLuMatrixFwd_lookup_core (
icmLuBase *pp, /* This */
double *out, /* Vector of output values */
double *in /* Vector of input values */
) {
int rv = 0;
icmLuMatrix *p = (icmLuMatrix *)pp;
rv |= icmLuMatrixFwd_matrix(p, out, in);
rv |= icmLuMatrixFwd_abs(p, out, out);
return rv;
}
static int
icmLuMatrixFwd_lookup_out (
icmLuBase *pp, /* This */
double *out, /* Vector of output values */
double *in /* Vector of input values */
) {
int rv = 0;
out[0] = in[0];
out[1] = in[1];
out[2] = in[2];
return rv;
}
/* - - - - - - - - - - - - - - */
/* Individual components of Bwd conversion: */
static int
icmLuMatrixBwd_abs (/* Abs comes first in Bwd conversion */
icmLuMatrix *p, /* This */
double *out, /* Vector of output values */
double *in /* Vector of input values */
) {
int rv = 0;
if (out != in) { /* Don't alter input values */
out[0] = in[0];
out[1] = in[1];
out[2] = in[2];
}
/* If e_pcs is Lab, then convert Lab to XYZ */
if (p->e_pcs == icSigLabData)
icmLab2XYZ(&p->pcswht, out, out);
/* If required, convert from Absolute to Relative colorimetric */
if (p->intent == icAbsoluteColorimetric
|| p->intent == icmAbsolutePerceptual
|| p->intent == icmAbsoluteSaturation) {
icmMulBy3x3(out, p->fromAbs, out);
}
return rv;
}
static int
icmLuMatrixBwd_matrix (
icmLuMatrix *p, /* This */
double *out, /* Vector of output values */
double *in /* Vector of input values */
) {
int rv = 0;
double tt[3];
tt[0] = in[0];
tt[1] = in[1];
tt[2] = in[2];
/* Matrix */
out[0] = p->bmx[0][0] * tt[0] + p->bmx[0][1] * tt[1] + p->bmx[0][2] * tt[2];
out[1] = p->bmx[1][0] * tt[0] + p->bmx[1][1] * tt[1] + p->bmx[1][2] * tt[2];
out[2] = p->bmx[2][0] * tt[0] + p->bmx[2][1] * tt[1] + p->bmx[2][2] * tt[2];
return rv;
}
static int
icmLuMatrixBwd_curve (
icmLuMatrix *p, /* This */
double *out, /* Vector of output values */
double *in /* Vector of input values */
) {
icc *icp = p->icp;
int rv = 0;
/* Curves */
if ((rv |= p->redCurve->lookup_bwd(p->redCurve,&out[0],&in[0])) > 1
|| (rv |= p->greenCurve->lookup_bwd(p->greenCurve,&out[1],&in[1])) > 1
|| (rv |= p->blueCurve->lookup_bwd(p->blueCurve,&out[2],&in[2])) > 1) {
sprintf(icp->err,"icc_lookup: Curve->lookup_bwd() failed");
icp->errc = rv;
return 2;
}
return rv;
}
/* Overall Bwd conversion (PCS->Dev) */
static int
icmLuMatrixBwd_lookup (
icmLuBase *pp, /* This */
double *out, /* Vector of output values */
double *in /* Vector of input values */
) {
int rv = 0;
icmLuMatrix *p = (icmLuMatrix *)pp;
rv |= icmLuMatrixBwd_abs(p, out, in);
rv |= icmLuMatrixBwd_matrix(p, out, out);
rv |= icmLuMatrixBwd_curve(p, out, out);
return rv;
}
/* Three stage conversion routines */
static int
icmLuMatrixBwd_lookup_in (
icmLuBase *pp, /* This */
double *out, /* Vector of output values */
double *in /* Vector of input values */
) {
int rv = 0;
out[0] = in[0];
out[1] = in[1];
out[2] = in[2];
return rv;
}
static int
icmLuMatrixBwd_lookup_core (
icmLuBase *pp, /* This */
double *out, /* Vector of output values */
double *in /* Vector of input values */
) {
int rv = 0;
icmLuMatrix *p = (icmLuMatrix *)pp;
rv |= icmLuMatrixBwd_abs(p, out, in);
rv |= icmLuMatrixBwd_matrix(p, out, out);
return rv;
}
static int
icmLuMatrixBwd_lookup_out (
icmLuBase *pp, /* This */
double *out, /* Vector of output values */
double *in /* Vector of input values */
) {
int rv = 0;
icmLuMatrix *p = (icmLuMatrix *)pp;
rv |= icmLuMatrixBwd_curve(p, out, in);
return rv;
}
/* - - - - - - - - - - - - - - */
static void
icmLuMatrix_delete(
icmLuBase *p
) {
icc *icp = p->icp;
icp->al->free(icp->al, p);
}
/* We setup valid fwd and bwd component conversions, */
/* but setup only the asked for overal conversion. */
static icmLuBase *
new_icmLuMatrix(
struct _icc *icp,
icColorSpaceSignature inSpace, /* Native Input color space */
icColorSpaceSignature outSpace, /* Native Output color space */
icColorSpaceSignature pcs, /* Native PCS */
icColorSpaceSignature e_inSpace, /* Effective Input color space */
icColorSpaceSignature e_outSpace, /* Effective Output color space */
icColorSpaceSignature e_pcs, /* Effective PCS */
icRenderingIntent intent, /* Rendering intent */
icmLookupFunc func, /* Functionality requested */
int dir /* 0 = fwd, 1 = bwd */
) {
icmLuMatrix *p;
if ((p = (icmLuMatrix *) icp->al->calloc(icp->al,1,sizeof(icmLuMatrix))) == NULL)
return NULL;
p->icp = icp;
p->del = icmLuMatrix_delete;
p->lutspaces= icmLutSpaces;
p->spaces = icmLuSpaces;
p->XYZ_Rel2Abs = icmLuXYZ_Rel2Abs;
p->XYZ_Abs2Rel = icmLuXYZ_Abs2Rel;
p->get_lutranges = icmLu_get_lutranges;
p->get_ranges = icmLu_get_ranges;
p->init_wh_bk = icmLuInit_Wh_bk;
p->wh_bk_points = icmLuWh_bk_points;
p->lu_wh_bk_points = icmLuLu_wh_bk_points;
p->fwd_lookup = icmLuMatrixFwd_lookup;
p->fwd_curve = icmLuMatrixFwd_curve;
p->fwd_matrix = icmLuMatrixFwd_matrix;
p->fwd_abs = icmLuMatrixFwd_abs;
p->bwd_lookup = icmLuMatrixBwd_lookup;
p->bwd_abs = icmLuMatrixBwd_abs;
p->bwd_matrix = icmLuMatrixBwd_matrix;
p->bwd_curve = icmLuMatrixBwd_curve;
if (dir) {
p->ttype = icmMatrixBwdType;
p->lookup = icmLuMatrixBwd_lookup;
p->lookup_in = icmLuMatrixBwd_lookup_in;
p->lookup_core = icmLuMatrixBwd_lookup_core;
p->lookup_out = icmLuMatrixBwd_lookup_out;
p->lookup_inv_in = icmLuMatrixFwd_lookup_out; /* Opposite of Bwd_lookup_in */
} else {
p->ttype = icmMatrixFwdType;
p->lookup = icmLuMatrixFwd_lookup;
p->lookup_in = icmLuMatrixFwd_lookup_in;
p->lookup_core = icmLuMatrixFwd_lookup_core;
p->lookup_out = icmLuMatrixFwd_lookup_out;
p->lookup_inv_in = icmLuMatrixBwd_lookup_out; /* Opposite of Fwd_lookup_in */
}
/* Lookup the white and black points */
if (p->init_wh_bk((icmLuBase *)p)) {
p->del((icmLuBase *)p);
return NULL;
}
/* Note that we can use matrix type even if PCS is Lab, */
/* by simply converting it. */
/* Find the appropriate tags */
if ((p->redCurve = (icmCurve *)icp->read_tag(icp, icSigRedTRCTag)) == NULL
|| p->redCurve->ttype != icSigCurveType
|| (p->greenCurve = (icmCurve *)icp->read_tag(icp, icSigGreenTRCTag)) == NULL
|| p->greenCurve->ttype != icSigCurveType
|| (p->blueCurve = (icmCurve *)icp->read_tag(icp, icSigBlueTRCTag)) == NULL
|| p->blueCurve->ttype != icSigCurveType
|| (p->redColrnt = (icmXYZArray *)icp->read_tag(icp, icSigRedColorantTag)) == NULL
|| p->redColrnt->ttype != icSigXYZType || p->redColrnt->size < 1
|| (p->greenColrnt = (icmXYZArray *)icp->read_tag(icp, icSigGreenColorantTag)) == NULL
|| p->greenColrnt->ttype != icSigXYZType || p->greenColrnt->size < 1
|| (p->blueColrnt = (icmXYZArray *)icp->read_tag(icp, icSigBlueColorantTag)) == NULL
|| p->blueColrnt->ttype != icSigXYZType || p->blueColrnt->size < 1) {
p->del((icmLuBase *)p);
return NULL;
}
/* Setup the matrix */
p->mx[0][0] = p->redColrnt->data[0].X;
p->mx[0][1] = p->greenColrnt->data[0].X;
p->mx[0][2] = p->blueColrnt->data[0].X;
p->mx[1][1] = p->greenColrnt->data[0].Y;
p->mx[1][0] = p->redColrnt->data[0].Y;
p->mx[1][2] = p->blueColrnt->data[0].Y;
p->mx[2][1] = p->greenColrnt->data[0].Z;
p->mx[2][0] = p->redColrnt->data[0].Z;
p->mx[2][2] = p->blueColrnt->data[0].Z;
#ifndef ICM_STRICT
/* Workaround for buggy Kodak RGB profiles. Their matrix values */
/* may be scaled to 100 rather than 1.0, and the colorant curves */
/* may be scaled by 0.5 */
if (icp->header->cmmId == str2tag("KCMS")) {
int i, j, oc = 0;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
if (p->mx[i][j] > 5.0)
oc++;
if (oc > 4) { /* Looks like it */
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
p->mx[i][j] /= 100.0;
}
}
#endif /* ICM_STRICT */
if (icmInverse3x3(p->bmx, p->mx) != 0) { /* Compute inverse */
sprintf(icp->err,"icc_new_iccLuMatrix: Matrix wasn't invertable");
icp->errc = 2;
p->del((icmLuBase *)p);
return NULL;
}
p->pcswht = icp->header->illuminant;
p->intent = intent;
p->function = func;
p->inSpace = inSpace;
p->outSpace = outSpace;
p->pcs = pcs;
p->e_inSpace = e_inSpace;
p->e_outSpace = e_outSpace;
p->e_pcs = e_pcs;
/* Lookup the white and black points */
if (p->init_wh_bk((icmLuBase *)p)) {
p->del((icmLuBase *)p);
return NULL;
}
return (icmLuBase *)p;
}
static icmLuBase *
new_icmLuMatrixFwd(
struct _icc *icp,
icColorSpaceSignature inSpace, /* Native Input color space */
icColorSpaceSignature outSpace, /* Native Output color space */
icColorSpaceSignature pcs, /* Native PCS */
icColorSpaceSignature e_inSpace, /* Effective Input color space */
icColorSpaceSignature e_outSpace, /* Effective Output color space */
icColorSpaceSignature e_pcs, /* Effective PCS */
icRenderingIntent intent, /* Rendering intent */
icmLookupFunc func /* Functionality requested */
) {
return new_icmLuMatrix(icp, inSpace, outSpace, pcs, e_inSpace, e_outSpace, e_pcs,
intent, func, 0);
}
static icmLuBase *
new_icmLuMatrixBwd(
struct _icc *icp,
icColorSpaceSignature inSpace, /* Native Input color space */
icColorSpaceSignature outSpace, /* Native Output color space */
icColorSpaceSignature pcs, /* Native PCS */
icColorSpaceSignature e_inSpace, /* Effective Input color space */
icColorSpaceSignature e_outSpace, /* Effective Output color space */
icColorSpaceSignature e_pcs, /* Effective PCS */
icRenderingIntent intent, /* Rendering intent */
icmLookupFunc func /* Functionality requested */
) {
return new_icmLuMatrix(icp, inSpace, outSpace, pcs, e_inSpace, e_outSpace, e_pcs,
intent, func, 1);
}
/* - - - - - - - - - - - - - - - - - - - - - - - */
/* Forward and Backward Multi-Dimensional Interpolation type conversion */
/* Return 0 on success, 1 if clipping occured, 2 on other error */
/* Components of overall lookup, in order */
static int icmLuLut_in_abs(icmLuLut *p, double *out, double *in) {
icmLut *lut = p->lut;
int rv = 0;
DBLLL(("icm in_abs: input %s\n",icmPdv(lut->inputChan, in)));
if (out != in) {
unsigned int i;
for (i = 0; i < lut->inputChan; i++) /* Don't alter input values */
out[i] = in[i];
}
/* If Bwd Lut, take care of Absolute color space and effective input space */
if ((p->function == icmBwd || p->function == icmGamut || p->function == icmPreview)
&& (p->e_inSpace == icSigLabData
|| p->e_inSpace == icSigXYZData)
&& (p->intent == icAbsoluteColorimetric
|| p->intent == icmAbsolutePerceptual
|| p->intent == icmAbsoluteSaturation)) {
if (p->e_inSpace == icSigLabData) {
icmLab2XYZ(&p->pcswht, out, out);
DBLLL(("icm in_abs: after Lab2XYZ %s\n",icmPdv(lut->inputChan, out)));
}
/* Convert from Absolute to Relative colorimetric */
icmMulBy3x3(out, p->fromAbs, out);
DBLLL(("icm in_abs: after fromAbs %s\n",icmPdv(lut->inputChan, out)));
if (p->inSpace == icSigLabData) {
icmXYZ2Lab(&p->pcswht, out, out);
DBLLL(("icm in_abs: after XYZ2Lab %s\n",icmPdv(lut->inputChan, out)));
}
} else {
/* Convert from Effective to Native input space */
if (p->e_inSpace == icSigLabData && p->inSpace == icSigXYZData) {
icmLab2XYZ(&p->pcswht, out, out);
DBLLL(("icm in_abs: after Lab2XYZ %s\n",icmPdv(lut->inputChan, out)));
} else if (p->e_inSpace == icSigXYZData && p->inSpace == icSigLabData) {
icmXYZ2Lab(&p->pcswht, out, out);
DBLLL(("icm in_abs: after XYZ2Lab %s\n",icmPdv(lut->inputChan, out)));
}
}
DBLLL(("icm in_abs: returning %s\n",icmPdv(lut->inputChan, out)));
return rv;
}
/* Possible matrix lookup */
static int icmLuLut_matrix(icmLuLut *p, double *out, double *in) {
icmLut *lut = p->lut;
int rv = 0;
if (p->usematrix)
rv |= lut->lookup_matrix(lut,out,in);
else if (out != in) {
unsigned int i;
for (i = 0; i < lut->inputChan; i++)
out[i] = in[i];
}
return rv;
}
/* Do input -> input' lookup */
static int icmLuLut_input(icmLuLut *p, double *out, double *in) {
icmLut *lut = p->lut;
int rv = 0;
p->in_normf(out, in); /* Normalize from input color space */
rv |= lut->lookup_input(lut,out,out); /* Lookup though input tables */
p->in_denormf(out,out); /* De-normalize to input color space */
return rv;
}
/* Do input'->output' lookup */
static int icmLuLut_clut(icmLuLut *p, double *out, double *in) {
icmLut *lut = p->lut;
double temp[MAX_CHAN];
int rv = 0;
p->in_normf(temp, in); /* Normalize from input color space */
rv |= p->lookup_clut(lut,out,temp); /* Lookup though clut tables */
p->out_denormf(out,out); /* De-normalize to output color space */
return rv;
}
/* Do output'->output lookup */
static int icmLuLut_output(icmLuLut *p, double *out, double *in) {
icmLut *lut = p->lut;
int rv = 0;
p->out_normf(out,in); /* Normalize from output color space */
rv |= lut->lookup_output(lut,out,out); /* Lookup though output tables */
p->out_denormf(out, out); /* De-normalize to output color space */
return rv;
}
static int icmLuLut_out_abs(icmLuLut *p, double *out, double *in) {
icmLut *lut = p->lut;
int rv = 0;
DBLLL(("icm out_abs: input %s\n",icmPdv(lut->outputChan, in)));
if (out != in) {
unsigned int i;
for (i = 0; i < lut->outputChan; i++) /* Don't alter input values */
out[i] = in[i];
}
/* If Fwd Lut, take care of Absolute color space */
/* and convert from native to effective out PCS */
if ((p->function == icmFwd || p->function == icmPreview)
&& (p->outSpace == icSigLabData
|| p->outSpace == icSigXYZData)
&& (p->intent == icAbsoluteColorimetric
|| p->intent == icmAbsolutePerceptual
|| p->intent == icmAbsoluteSaturation)) {
if (p->outSpace == icSigLabData) {
icmLab2XYZ(&p->pcswht, out, out);
DBLLL(("icm out_abs: after Lab2XYZ %s\n",icmPdv(lut->outputChan, out)));
}
/* Convert from Relative to Absolute colorimetric XYZ */
icmMulBy3x3(out, p->toAbs, out);
DBLLL(("icm out_abs: after toAbs %s\n",icmPdv(lut->outputChan, out)));
if (p->e_outSpace == icSigLabData) {
icmXYZ2Lab(&p->pcswht, out, out);
DBLLL(("icm out_abs: after XYZ2Lab %s\n",icmPdv(lut->outputChan, out)));
}
} else {
/* Convert from Native to Effective output space */
if (p->outSpace == icSigLabData && p->e_outSpace == icSigXYZData) {
icmLab2XYZ(&p->pcswht, out, out);
DBLLL(("icm out_abs: after Lab2 %s\n",icmPdv(lut->outputChan, out)));
} else if (p->outSpace == icSigXYZData && p->e_outSpace == icSigLabData) {
icmXYZ2Lab(&p->pcswht, out, out);
DBLLL(("icm out_abs: after XYZ2Lab %s\n",icmPdv(lut->outputChan, out)));
}
}
DBLLL(("icm out_abs: returning %s\n",icmPdv(lut->outputChan, out)));
return rv;
}
/* Overall lookup */
static int
icmLuLut_lookup (
icmLuBase *pp, /* This */
double *out, /* Vector of output values */
double *in /* Vector of input values */
) {
int rv = 0;
icmLuLut *p = (icmLuLut *)pp;
icmLut *lut = p->lut;
double temp[MAX_CHAN];
DBGLL(("icmLuLut_lookup: in = %s\n", icmPdv(p->lut->inputChan, in)));
rv |= p->in_abs(p,temp,in); /* Possible absolute conversion */
DBGLL(("icmLuLut_lookup: in_abs = %s\n", icmPdv(p->lut->inputChan, temp)));
if (p->usematrix) {
rv |= lut->lookup_matrix(lut,temp,temp);/* If XYZ, multiply by non-unity matrix */
DBGLL(("icmLuLut_lookup: matrix = %s\n", icmPdv(p->lut->inputChan, temp)));
}
p->in_normf(temp, temp); /* Normalize for input color space */
DBGLL(("icmLuLut_lookup: norm = %s\n", icmPdv(p->lut->inputChan, temp)));
rv |= lut->lookup_input(lut,temp,temp); /* Lookup though input tables */
DBGLL(("icmLuLut_lookup: input = %s\n", icmPdv(p->lut->inputChan, temp)));
rv |= p->lookup_clut(lut,out,temp); /* Lookup though clut tables */
DBGLL(("icmLuLut_lookup: clut = %s\n", icmPdv(p->lut->outputChan, out)));
rv |= lut->lookup_output(lut,out,out); /* Lookup though output tables */
DBGLL(("icmLuLut_lookup: output = %s\n", icmPdv(p->lut->outputChan, out)));
p->out_denormf(out,out); /* Normalize for output color space */
DBGLL(("icmLuLut_lookup: denorm = %s\n", icmPdv(p->lut->outputChan, out)));
rv |= p->out_abs(p,out,out); /* Possible absolute conversion */
DBGLL(("icmLuLut_lookup: out_abse = %s\n", icmPdv(p->lut->outputChan, out)));
return rv;
}
#ifdef NEVER /* The following should be identical in effect to the above. */
/* Overall lookup */
static int
icmLuLut_lookup (
icmLuBase *pp, /* This */
double *out, /* Vector of output values */
double *in /* Vector of input values */
) {
int i, rv = 0;
icmLuLut *p = (icmLuLut *)pp;
icmLut *lut = p->lut;
double temp[MAX_CHAN];
rv |= p->in_abs(p,temp,in);
rv |= p->matrix(p,temp,temp);
rv |= p->input(p,temp,temp);
rv |= p->clut(p,out,temp);
rv |= p->output(p,out,out);
rv |= p->out_abs(p,out,out);
return rv;
}
#endif /* NEVER */
/* Three stage conversion */
static int
icmLuLut_lookup_in (
icmLuBase *pp, /* This */
double *out, /* Vector of output values */
double *in /* Vector of input values */
) {
int rv = 0;
icmLuLut *p = (icmLuLut *)pp;
icmLut *lut = p->lut;
/* If in_abs() or matrix() are active, then we can't have a per component input curve */
if (((p->function == icmBwd || p->function == icmGamut || p->function == icmPreview)
&& (p->intent == icAbsoluteColorimetric
|| p->intent == icmAbsolutePerceptual
|| p->intent == icmAbsoluteSaturation))
|| (p->e_inSpace != p->inSpace)
|| (p->usematrix)) {
unsigned int i;
for (i = 0; i < lut->inputChan; i++)
out[i] = in[i];
} else {
rv |= p->input(p,out,in);
}
return rv;
}
static int
icmLuLut_lookup_core (
icmLuBase *pp, /* This */
double *out, /* Vector of output values */
double *in /* Vector of input values */
) {
int rv = 0;
icmLuLut *p = (icmLuLut *)pp;
/* If in_abs() or matrix() are active, then we have to do the per component input curve here */
if (((p->function == icmBwd || p->function == icmGamut || p->function == icmPreview)
&& (p->intent == icAbsoluteColorimetric
|| p->intent == icmAbsolutePerceptual
|| p->intent == icmAbsoluteSaturation))
|| (p->e_inSpace != p->inSpace)
|| (p->usematrix)) {
double temp[MAX_CHAN];
rv |= p->in_abs(p,temp,in);
rv |= p->matrix(p,temp,temp);
rv |= p->input(p,temp,temp);
rv |= p->clut(p,out,temp);
} else {
rv |= p->clut(p,out,in);
}
/* If out_abs() is active, then we can't have do per component out curve here */
if (((p->function == icmFwd || p->function == icmPreview)
&& (p->intent == icAbsoluteColorimetric
|| p->intent == icmAbsolutePerceptual
|| p->intent == icmAbsoluteSaturation))
|| (p->outSpace != p->e_outSpace)) {
rv |= p->output(p,out,out);
rv |= p->out_abs(p,out,out);
}
return rv;
}
static int
icmLuLut_lookup_out (
icmLuBase *pp, /* This */
double *out, /* Vector of output values */
double *in /* Vector of input values */
) {
int rv = 0;
icmLuLut *p = (icmLuLut *)pp;
icmLut *lut = p->lut;
/* If out_abs() is active, then we can't have a per component out curve */
if (((p->function == icmFwd || p->function == icmPreview)
&& (p->intent == icAbsoluteColorimetric
|| p->intent == icmAbsolutePerceptual
|| p->intent == icmAbsoluteSaturation))
|| (p->outSpace != p->e_outSpace)) {
unsigned int i;
for (i = 0; i < lut->outputChan; i++)
out[i] = in[i];
} else {
rv |= p->output(p,out,in);
}
return rv;
}
/* Inverse three stage conversion (partly implemented) */
static int
icmLuLut_lookup_inv_in (
icmLuBase *pp, /* This */
double *out, /* Vector of output values */
double *in /* Vector of input values */
) {
int rv = 0;
icmLuLut *p = (icmLuLut *)pp;
icmLut *lut = p->lut;
/* If in_abs() or matrix() are active, then we can't have a per component input curve */
if (((p->function == icmBwd || p->function == icmGamut || p->function == icmPreview)
&& (p->intent == icAbsoluteColorimetric
|| p->intent == icmAbsolutePerceptual
|| p->intent == icmAbsoluteSaturation))
|| (p->e_inSpace != p->inSpace)
|| (p->usematrix)) {
unsigned int i;
for (i = 0; i < lut->inputChan; i++)
out[i] = in[i];
} else {
rv |= p->inv_input(p,out,in);
}
return rv;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* Some components of inverse lookup, in order */
/* ~~ should these be in icmLut (like all the fwd transforms)? */
static int icmLuLut_inv_out_abs(icmLuLut *p, double *out, double *in) {
icmLut *lut = p->lut;
int rv = 0;
DBLLL(("icm inv_out_abs: input %s\n",icmPdv(lut->outputChan, in)));
if (out != in) {
unsigned int i;
for (i = 0; i < lut->outputChan; i++) /* Don't alter input values */
out[i] = in[i];
}
/* If Fwd Lut, take care of Absolute color space */
/* and convert from effective to native inverse output PCS */
/* OutSpace must be PCS: XYZ or Lab */
if ((p->function == icmFwd || p->function == icmPreview)
&& (p->e_outSpace == icSigLabData
|| p->e_outSpace == icSigXYZData)
&& (p->intent == icAbsoluteColorimetric
|| p->intent == icmAbsolutePerceptual
|| p->intent == icmAbsoluteSaturation)) {
if (p->e_outSpace == icSigLabData) {
icmLab2XYZ(&p->pcswht, out, out);
DBLLL(("icm inv_out_abs: after Lab2XYZ %s\n",icmPdv(lut->outputChan, out)));
}
/* Convert from Absolute to Relative colorimetric */
icmMulBy3x3(out, p->fromAbs, out);
DBLLL(("icm inv_out_abs: after fromAbs %s\n",icmPdv(lut->outputChan, out)));
if (p->outSpace == icSigLabData) {
icmXYZ2Lab(&p->pcswht, out, out);
DBLLL(("icm inv_out_abs: after XYZ2Lab %s\n",icmPdv(lut->outputChan, out)));
}
} else {
/* Convert from Effective to Native output space */
if (p->e_outSpace == icSigLabData && p->outSpace == icSigXYZData) {
icmLab2XYZ(&p->pcswht, out, out);
DBLLL(("icm inv_out_abs: after Lab2XYZ %s\n",icmPdv(lut->outputChan, out)));
} else if (p->e_outSpace == icSigXYZData && p->outSpace == icSigLabData) {
icmXYZ2Lab(&p->pcswht, out, out);
DBLLL(("icm inv_out_abs: after XYZ2Lab %s\n",icmPdv(lut->outputChan, out)));
}
}
return rv;
}
/* Do output->output' inverse lookup */
static int icmLuLut_inv_output(icmLuLut *p, double *out, double *in) {
icc *icp = p->icp;
icmLut *lut = p->lut;
int i;
int rv = 0;
if (lut->rot[0].inited == 0) {
for (i = 0; i < lut->outputChan; i++) {
rv = icmTable_setup_bwd(icp, &lut->rot[i], lut->outputEnt,
lut->outputTable + i * lut->outputEnt);
if (rv != 0) {
sprintf(icp->err,"icc_Lut_inv_input: Malloc failure in inverse lookup init.");
return icp->errc = rv;
}
}
}
p->out_normf(out,in); /* Normalize from output color space */
for (i = 0; i < lut->outputChan; i++) {
/* Reverse lookup though output tables */
rv |= icmTable_lookup_bwd(&lut->rot[i], &out[i], &out[i]);
}
p->out_denormf(out, out); /* De-normalize to output color space */
return rv;
}
/* No output' -> input inverse lookup. */
/* This is non-trivial ! */
/* Do input' -> input inverse lookup */
static int icmLuLut_inv_input(icmLuLut *p, double *out, double *in) {
icc *icp = p->icp;
icmLut *lut = p->lut;
int i;
int rv = 0;
if (lut->rit[0].inited == 0) {
for (i = 0; i < lut->inputChan; i++) {
rv = icmTable_setup_bwd(icp, &lut->rit[i], lut->inputEnt,
lut->inputTable + i * lut->inputEnt);
if (rv != 0) {
sprintf(icp->err,"icc_Lut_inv_input: Malloc failure in inverse lookup init.");
return icp->errc = rv;
}
}
}
p->in_normf(out, in); /* Normalize from input color space */
for (i = 0; i < lut->inputChan; i++) {
/* Reverse lookup though input tables */
rv |= icmTable_lookup_bwd(&lut->rit[i], &out[i], &out[i]);
}
p->in_denormf(out,out); /* De-normalize to input color space */
return rv;
}
/* Possible inverse matrix lookup */
static int icmLuLut_inv_matrix(icmLuLut *p, double *out, double *in) {
icc *icp = p->icp;
icmLut *lut = p->lut;
int rv = 0;
if (p->usematrix) {
double tt[3];
if (p->imx_valid == 0) {
if (icmInverse3x3(p->imx, lut->e) != 0) { /* Compute inverse */
sprintf(icp->err,"icc_new_iccLuMatrix: Matrix wasn't invertable");
icp->errc = 2;
return 2;
}
p->imx_valid = 1;
}
/* Matrix multiply */
tt[0] = p->imx[0][0] * in[0] + p->imx[0][1] * in[1] + p->imx[0][2] * in[2];
tt[1] = p->imx[1][0] * in[0] + p->imx[1][1] * in[1] + p->imx[1][2] * in[2];
tt[2] = p->imx[2][0] * in[0] + p->imx[2][1] * in[1] + p->imx[2][2] * in[2];
out[0] = tt[0], out[1] = tt[1], out[2] = tt[2];
} else if (out != in) {
unsigned int i;
for (i = 0; i < lut->inputChan; i++)
out[i] = in[i];
}
return rv;
}
static int icmLuLut_inv_in_abs(icmLuLut *p, double *out, double *in) {
icmLut *lut = p->lut;
int rv = 0;
DBLLL(("icm inv_in_abs: input %s\n",icmPdv(lut->inputChan, in)));
if (out != in) {
unsigned int i;
for (i = 0; i < lut->inputChan; i++) /* Don't alter input values */
out[i] = in[i];
}
/* If Bwd Lut, take care of Absolute color space, and */
/* convert from native to effective input space */
if ((p->function == icmBwd || p->function == icmGamut || p->function == icmPreview)
&& (p->inSpace == icSigLabData
|| p->inSpace == icSigXYZData)
&& (p->intent == icAbsoluteColorimetric
|| p->intent == icmAbsolutePerceptual
|| p->intent == icmAbsoluteSaturation)) {
if (p->inSpace == icSigLabData) {
icmLab2XYZ(&p->pcswht, out, out);
DBLLL(("icm inv_in_abs: after Lab2XYZ %s\n",icmPdv(lut->inputChan, out)));
}
/* Convert from Relative to Absolute colorimetric XYZ */
icmMulBy3x3(out, p->toAbs, out);
DBLLL(("icm inv_in_abs: after toAbs %s\n",icmPdv(lut->inputChan, out)));
if (p->e_inSpace == icSigLabData) {
icmXYZ2Lab(&p->pcswht, out, out);
DBLLL(("icm inv_in_abs: after XYZ2Lab %s\n",icmPdv(lut->inputChan, out)));
}
} else {
/* Convert from Native to Effective input space */
if (p->inSpace == icSigLabData && p->e_inSpace == icSigXYZData) {
icmLab2XYZ(&p->pcswht, out, out);
DBLLL(("icm inv_in_abs: after Lab2XYZ %s\n",icmPdv(lut->inputChan, out)));
} else if (p->inSpace == icSigXYZData && p->e_inSpace == icSigLabData) {
icmXYZ2Lab(&p->pcswht, out, out);
DBLLL(("icm inv_in_abs: after XYZ2Lab %s\n",icmPdv(lut->inputChan, out)));
}
}
DBLLL(("icm inv_in_abs: returning %s\n",icmPdv(lut->inputChan, out)));
return rv;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* Return LuLut information */
static void icmLuLut_get_info(
icmLuLut *p, /* this */
icmLut **lutp, /* Pointer to icc lut type */
icmXYZNumber *pcswhtp, /* Pointer to profile PCS white point */
icmXYZNumber *whitep, /* Pointer to profile absolute white point */
icmXYZNumber *blackp /* Pointer to profile absolute black point */
) {
if (lutp != NULL)
*lutp = p->lut;
if (pcswhtp != NULL)
*pcswhtp = p->pcswht;
if (whitep != NULL)
*whitep = p->whitePoint;
if (blackp != NULL)
*blackp = p->blackPoint;
}
/* Get the native ranges for the LuLut */
/* This is computed differently to the mono & matrix types, to */
/* accurately take into account the different range for 8 bit Lab */
/* lut type. The range returned for the effective PCS is not so accurate. */
static void
icmLuLut_get_lutranges (
struct _icmLuBase *pp,
double *inmin, double *inmax, /* Return maximum range of inspace values */
double *outmin, double *outmax /* Return maximum range of outspace values */
) {
icmLuLut *p = (icmLuLut *)pp;
unsigned int i;
for (i = 0; i < p->lut->inputChan; i++) {
inmin[i] = 0.0; /* Normalized range of input space values */
inmax[i] = 1.0;
}
p->in_denormf(inmin,inmin); /* Convert to real colorspace range */
p->in_denormf(inmax,inmax);
/* Make sure min and max are so. */
for (i = 0; i < p->lut->inputChan; i++) {
if (inmin[i] > inmax[i]) {
double tt;
tt = inmin[i];
inmin[i] = inmax[i];
inmax[i] = tt;
}
}
for (i = 0; i < p->lut->outputChan; i++) {
outmin[i] = 0.0; /* Normalized range of output space values */
outmax[i] = 1.0;
}
p->out_denormf(outmin,outmin); /* Convert to real colorspace range */
p->out_denormf(outmax,outmax);
/* Make sure min and max are so. */
for (i = 0; i < p->lut->outputChan; i++) {
if (outmin[i] > outmax[i]) {
double tt;
tt = outmin[i];
outmin[i] = outmax[i];
outmax[i] = tt;
}
}
}
/* Get the effective (externaly visible) ranges for the LuLut */
/* This will be accurate if there is no override, but only */
/* aproximate if a PCS override is in place. */
static void
icmLuLut_get_ranges (
struct _icmLuBase *pp,
double *inmin, double *inmax, /* Return maximum range of inspace values */
double *outmin, double *outmax /* Return maximum range of outspace values */
) {
icmLuLut *p = (icmLuLut *)pp;
/* Get the native ranges first */
icmLuLut_get_lutranges(pp, inmin, inmax, outmin, outmax);
/* And replace them if the effective space is different */
if (p->e_inSpace != p->inSpace)
getRange(p->icp, p->e_inSpace, p->lut->ttype, inmin, inmax);
if (p->e_outSpace != p->outSpace)
getRange(p->icp, p->e_outSpace, p->lut->ttype, outmin, outmax);
}
/* Return the underlying Lut matrix */
static void
icmLuLut_get_matrix (
struct _icmLuLut *p,
double m[3][3]
) {
int i, j;
icmLut *lut = p->lut;
if (p->usematrix) {
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
m[i][j] = lut->e[i][j]; /* Copy from Lut */
} else { /* return unity matrix */
icmSetUnity3x3(m);
}
}
static void
icmLuLut_delete(
icmLuBase *p
) {
icc *icp = p->icp;
icp->al->free(icp->al, p);
}
icmLuBase *
icc_new_icmLuLut(
icc *icp,
icTagSignature ttag, /* Target Lut tag */
icColorSpaceSignature inSpace, /* Native Input color space */
icColorSpaceSignature outSpace, /* Native Output color space */
icColorSpaceSignature pcs, /* Native PCS (from header) */
icColorSpaceSignature e_inSpace, /* Effective Input color space */
icColorSpaceSignature e_outSpace, /* Effective Output color space */
icColorSpaceSignature e_pcs, /* Effective PCS */
icRenderingIntent intent, /* Rendering intent (For absolute) */
icmLookupFunc func /* Functionality requested (for icmLuSpaces()) */
) {
icmLuLut *p;
if ((p = (icmLuLut *) icp->al->calloc(icp->al,1,sizeof(icmLuLut))) == NULL)
return NULL;
p->ttype = icmLutType;
p->icp = icp;
p->del = icmLuLut_delete;
p->lutspaces= icmLutSpaces;
p->spaces = icmLuSpaces;
p->XYZ_Rel2Abs = icmLuXYZ_Rel2Abs;
p->XYZ_Abs2Rel = icmLuXYZ_Abs2Rel;
p->init_wh_bk = icmLuInit_Wh_bk;
p->wh_bk_points = icmLuWh_bk_points;
p->lu_wh_bk_points = icmLuLu_wh_bk_points;
p->lookup = icmLuLut_lookup;
p->lookup_in = icmLuLut_lookup_in;
p->lookup_core = icmLuLut_lookup_core;
p->lookup_out = icmLuLut_lookup_out;
p->lookup_inv_in = icmLuLut_lookup_inv_in;
p->in_abs = icmLuLut_in_abs;
p->matrix = icmLuLut_matrix;
p->input = icmLuLut_input;
p->clut = icmLuLut_clut;
p->output = icmLuLut_output;
p->out_abs = icmLuLut_out_abs;
p->inv_in_abs = icmLuLut_inv_in_abs;
p->inv_matrix = icmLuLut_inv_matrix;
p->inv_input = icmLuLut_inv_input;
p->inv_output = icmLuLut_inv_output;
p->inv_out_abs = icmLuLut_inv_out_abs;
p->pcswht = icp->header->illuminant;
p->intent = intent; /* used to trigger absolute processing */
p->function = func;
p->inSpace = inSpace;
p->outSpace = outSpace;
p->pcs = pcs;
p->e_inSpace = e_inSpace;
p->e_outSpace = e_outSpace;
p->e_pcs = e_pcs;
p->get_info = icmLuLut_get_info;
p->get_lutranges = icmLuLut_get_lutranges;
p->get_ranges = icmLuLut_get_ranges;
p->get_matrix = icmLuLut_get_matrix;
/* Lookup the white and black points */
if (p->init_wh_bk((icmLuBase *)p)) {
p->del((icmLuBase *)p);
return NULL;
}
/* Get the Lut tag, & check that it is expected type */
if ((p->lut = (icmLut *)icp->read_tag(icp, ttag)) == NULL
|| (p->lut->ttype != icSigLut8Type && p->lut->ttype != icSigLut16Type)) {
p->del((icmLuBase *)p);
return NULL;
}
/* Check if matrix should be used */
if (inSpace == icSigXYZData && p->lut->nu_matrix(p->lut))
p->usematrix = 1;
else
p->usematrix = 0;
/* Lookup input color space to normalized index function */
if (getNormFunc(icp, inSpace, p->lut->ttype, icmToLuti, &p->in_normf)) {
sprintf(icp->err,"icc_get_luobj: Unknown colorspace");
icp->errc = 1;
p->del((icmLuBase *)p);
return NULL;
}
/* Lookup normalized index to input color space function */
if (getNormFunc(icp, inSpace, p->lut->ttype, icmFromLuti, &p->in_denormf)) {
sprintf(icp->err,"icc_get_luobj: Unknown colorspace");
icp->errc = 1;
p->del((icmLuBase *)p);
return NULL;
}
/* Lookup output color space to normalized Lut entry value function */
if (getNormFunc(icp, outSpace, p->lut->ttype, icmToLutv, &p->out_normf)) {
sprintf(icp->err,"icc_get_luobj: Unknown colorspace");
icp->errc = 1;
p->del((icmLuBase *)p);
return NULL;
}
/* Lookup normalized Lut entry value to output color space function */
if (getNormFunc(icp, outSpace, p->lut->ttype, icmFromLutv, &p->out_denormf)) {
sprintf(icp->err,"icc_get_luobj: Unknown colorspace");
icp->errc = 1;
p->del((icmLuBase *)p);
return NULL;
}
/* Note that the following two are only used in computing the expected */
/* value ranges of the effective PCS. This might not be the best way of */
/* doing this. */
/* Lookup normalized index to effective input color space function */
if (getNormFunc(icp, e_inSpace, p->lut->ttype, icmFromLuti, &p->e_in_denormf)) {
sprintf(icp->err,"icc_get_luobj: Unknown effective colorspace");
icp->errc = 1;
p->del((icmLuBase *)p);
return NULL;
}
/* Lookup normalized Lut entry value to effective output color space function */
if (getNormFunc(icp, e_outSpace, p->lut->ttype, icmFromLutv, &p->e_out_denormf)) {
sprintf(icp->err,"icc_get_luobj: Unknown effective colorspace");
icp->errc = 1;
p->del((icmLuBase *)p);
return NULL;
}
/* Determine appropriate clut lookup algorithm */
{
int use_sx; /* -1 = undecided, 0 = N-linear, 1 = Simplex lookup */
icColorSpaceSignature ins, outs; /* In and out Lut color spaces */
int inn, outn; /* in and out number of Lut components */
p->lutspaces((icmLuBase *)p, &ins, &inn, &outs, &outn, NULL);
/* Determine if the input space is "Device" like, */
/* ie. luminance will be expected to vary most strongly */
/* with the diagonal change in input coordinates. */
switch(ins) {
/* Luminence is carried by the sum of all the output channels, */
/* so output luminence will dominantly be in diagonal direction. */
case icSigXYZData: /* This seems to be appropriate ? */
case icSigRgbData:
case icSigGrayData:
case icSigCmykData:
case icSigCmyData:
case icSigMch6Data:
use_sx = 1; /* Simplex interpolation is appropriate */
break;
/* A single channel carries the luminence information */
case icSigLabData:
case icSigLuvData:
case icSigYCbCrData:
case icSigYxyData:
case icSigHlsData:
case icSigHsvData:
use_sx = 0; /* N-linear interpolation is appropriate */
break;
default:
use_sx = -1; /* undecided */
break;
}
/* If we couldn't figure it out from the input space, */
/* check output luminance variation with a diagonal input */
/* change. */
if (use_sx == -1) {
int lc; /* Luminance channel */
/* Determine where the luminence is carried in the output */
switch(outs) {
/* Luminence is carried by the sum of all the output channels */
case icSigRgbData:
case icSigGrayData:
case icSigCmykData:
case icSigCmyData:
case icSigMch6Data:
lc = -1; /* Average all channels */
break;
/* A single channel carries the luminence information */
case icSigLabData:
case icSigLuvData:
case icSigYCbCrData:
case icSigYxyData:
lc = 0;
break;
case icSigXYZData:
case icSigHlsData:
lc = 1;
break;
case icSigHsvData:
lc = 2;
break;
/* default means give up and use N-linear type lookup */
default:
lc = -2;
break;
}
/* If we know how luminance is represented in output space */
if (lc != -2) {
double tout1[MAX_CHAN]; /* Test output values */
double tout2[MAX_CHAN];
double tt, diag;
int n;
/* Determine input space location of min and max of */
/* given output channel (chan = -1 means average of all) */
p->lut->min_max(p->lut, tout1, tout2, lc);
/* Convert to vector and then calculate normalized */
/* dot product with diagonal vector (1,1,1...) */
for (tt = 0.0, n = 0; n < inn; n++) {
tout1[n] = tout2[n] - tout1[n];
tt += tout1[n] * tout1[n];
}
if (tt > 0.0)
tt = sqrt(tt); /* normalizing factor for maximum delta */
else
tt = 1.0; /* Hmm. */
tt *= sqrt((double)inn); /* Normalizing factor for diagonal vector */
for (diag = 0.0, n = 0; n < outn; n++)
diag += tout1[n] / tt;
diag = fabs(diag);
/* I'm not really convinced that this is a reliable */
/* indicator of whether simplex interpolation should be used ... */
/* It does seem to do the right thing with YCC space though. */
if (diag > 0.8) /* Diagonal is dominant ? */
use_sx = 1;
/* If we couldn't figure it out, use N-linear interpolation */
if (use_sx == -1)
use_sx = 0;
}
}
if (use_sx) {
p->lookup_clut = p->lut->lookup_clut_sx;
p->lut->tune_value = icmLut_tune_value_sx;
} else {
p->lookup_clut = p->lut->lookup_clut_nl;
p->lut->tune_value = icmLut_tune_value_nl;
}
}
return (icmLuBase *)p;
}
/* - - - - - - - - - - - - - - - - - - - - - - - */
/* Return an appropriate lookup object */
/* Return NULL on error, and detailed error in icc */
static icmLuBase* icc_get_luobj (
icc *p, /* ICC */
icmLookupFunc func, /* Conversion functionality */
icRenderingIntent intent, /* Rendering intent, including icmAbsoluteColorimetricXYZ */
icColorSpaceSignature pcsor,/* PCS override (0 = def) */
icmLookupOrder order /* Conversion representation search Order */
) {
icmLuBase *luobj = NULL; /* Lookup object to return */
icColorSpaceSignature pcs, e_pcs; /* PCS and effective PCS */
#ifdef ICM_STRICT
int rv;
/* Check that the profile is legal, since we depend on it ? */
if ((rv = check_icc_legal(p)) != 0)
return NULL;
#endif /* ICM_STRICT */
/* Figure out the native and effective PCS */
e_pcs = pcs = p->header->pcs;
if (pcsor != icmSigDefaultData)
e_pcs = pcsor; /* Override */
/* How we expect to execute the request depends firstly on the type of profile */
switch (p->header->deviceClass) {
case icSigInputClass:
case icSigDisplayClass:
case icSigColorSpaceClass:
/* Look for Intent based AToBX profile + optional BToAX reverse */
/* or for AToB0 based profile + optional BToA0 reverse */
/* or three component matrix profile (reversable) */
/* or momochrome table profile (reversable) */
/* No Lut intent for ICC < V2.4, but possible for >= V2.4, */
/* so fall back if we can't find the chosen Lut intent.. */
/* Device <-> PCS */
/* Determine the algorithm and set its parameters */
switch (func) {
icRenderingIntent fbintent; /* Fallback intent */
icTagSignature ttag, fbtag;
case icmFwd: /* Device to PCS */
if (intent == icmDefaultIntent)
intent = icPerceptual; /* Make this the default */
switch ((int)intent) {
case icAbsoluteColorimetric:
ttag = icSigAToB1Tag;
fbtag = icSigAToB0Tag;
fbintent = intent;
break;
case icRelativeColorimetric:
ttag = icSigAToB1Tag;
fbtag = icSigAToB0Tag;
fbintent = icmDefaultIntent;
break;
case icPerceptual:
ttag = icSigAToB0Tag;
fbtag = icSigAToB0Tag;
fbintent = icmDefaultIntent;
break;
case icSaturation:
ttag = icSigAToB2Tag;
fbtag = icSigAToB0Tag;
fbintent = icmDefaultIntent;
break;
case icmAbsolutePerceptual: /* Special icclib intent */
ttag = icSigAToB0Tag;
fbtag = icSigAToB0Tag;
fbintent = intent;
break;
case icmAbsoluteSaturation: /* Special icclib intent */
ttag = icSigAToB2Tag;
fbtag = icSigAToB0Tag;
fbintent = intent;
break;
default:
sprintf(p->err,"icc_get_luobj: Unknown intent");
p->errc = 1;
return NULL;
}
if (order != icmLuOrdRev) {
/* Try Lut type lookup with the chosen intent first */
if ((luobj = icc_new_icmLuLut(p, ttag,
p->header->colorSpace, pcs, pcs,
p->header->colorSpace, e_pcs, e_pcs,
intent, func)) != NULL)
break;
/* Try the fallback tag */
if ((luobj = icc_new_icmLuLut(p, fbtag,
p->header->colorSpace, pcs, pcs,
p->header->colorSpace, e_pcs, e_pcs,
fbintent, func)) != NULL)
break;
/* See if it could be a matrix lookup */
if ((luobj = new_icmLuMatrixFwd(p,
p->header->colorSpace, pcs, pcs,
p->header->colorSpace, e_pcs, e_pcs,
intent, func)) != NULL)
break;
/* See if it could be a monochrome lookup */
if ((luobj = new_icmLuMonoFwd(p,
p->header->colorSpace, pcs, pcs,
p->header->colorSpace, e_pcs, e_pcs,
intent, func)) != NULL)
break;
} else {
/* See if it could be a monochrome lookup */
if ((luobj = new_icmLuMonoFwd(p,
p->header->colorSpace, pcs, pcs,
p->header->colorSpace, e_pcs, e_pcs,
intent, func)) != NULL)
break;
/* See if it could be a matrix lookup */
if ((luobj = new_icmLuMatrixFwd(p,
p->header->colorSpace, pcs, pcs,
p->header->colorSpace, e_pcs, e_pcs,
intent, func)) != NULL)
break;
/* Try Lut type lookup last */
if ((luobj = icc_new_icmLuLut(p, ttag,
p->header->colorSpace, pcs, pcs,
p->header->colorSpace, e_pcs, e_pcs,
intent, func)) != NULL)
break;
/* Try the fallback tag */
if ((luobj = icc_new_icmLuLut(p, fbtag,
p->header->colorSpace, pcs, pcs,
p->header->colorSpace, e_pcs, e_pcs,
fbintent, func)) != NULL)
break;
}
break;
case icmBwd: /* PCS to Device */
if (intent == icmDefaultIntent)
intent = icPerceptual; /* Make this the default */
switch ((int)intent) {
case icAbsoluteColorimetric:
ttag = icSigBToA1Tag;
fbtag = icSigBToA0Tag;
fbintent = intent;
break;
case icRelativeColorimetric:
ttag = icSigBToA1Tag;
fbtag = icSigBToA0Tag;
fbintent = icmDefaultIntent;
break;
case icPerceptual:
ttag = icSigBToA0Tag;
fbtag = icSigBToA0Tag;
fbintent = icmDefaultIntent;
break;
case icSaturation:
ttag = icSigBToA2Tag;
fbtag = icSigBToA0Tag;
fbintent = icmDefaultIntent;
break;
case icmAbsolutePerceptual: /* Special icclib intent */
ttag = icSigBToA0Tag;
fbtag = icSigBToA0Tag;
fbintent = intent;
break;
case icmAbsoluteSaturation: /* Special icclib intent */
ttag = icSigBToA2Tag;
fbtag = icSigBToA0Tag;
fbintent = intent;
break;
default:
sprintf(p->err,"icc_get_luobj: Unknown intent");
p->errc = 1;
return NULL;
}
if (order != icmLuOrdRev) {
/* Try Lut type lookup first */
if ((luobj = icc_new_icmLuLut(p, ttag,
pcs, p->header->colorSpace, pcs,
e_pcs, p->header->colorSpace, e_pcs,
intent, func)) != NULL)
break;
/* Try the fallback Lut */
if ((luobj = icc_new_icmLuLut(p, fbtag,
pcs, p->header->colorSpace, pcs,
e_pcs, p->header->colorSpace, e_pcs,
fbintent, func)) != NULL)
break;
/* See if it could be a matrix lookup */
if ((luobj = new_icmLuMatrixBwd(p,
pcs, p->header->colorSpace, pcs,
e_pcs, p->header->colorSpace, e_pcs,
intent, func)) != NULL)
break;
/* See if it could be a monochrome lookup */
if ((luobj = new_icmLuMonoBwd(p,
pcs, p->header->colorSpace, pcs,
e_pcs, p->header->colorSpace, e_pcs,
intent, func)) != NULL)
break;
} else {
/* See if it could be a monochrome lookup */
if ((luobj = new_icmLuMonoBwd(p,
pcs, p->header->colorSpace, pcs,
e_pcs, p->header->colorSpace, e_pcs,
intent, func)) != NULL)
break;
/* See if it could be a matrix lookup */
if ((luobj = new_icmLuMatrixBwd(p,
pcs, p->header->colorSpace, pcs,
e_pcs, p->header->colorSpace, e_pcs,
intent, func)) != NULL)
break;
/* Try Lut type lookup last */
if ((luobj = icc_new_icmLuLut(p, ttag,
pcs, p->header->colorSpace, pcs,
e_pcs, p->header->colorSpace, e_pcs,
intent, func)) != NULL)
break;
/* Try the fallback Lut */
if ((luobj = icc_new_icmLuLut(p, fbtag,
pcs, p->header->colorSpace, pcs,
e_pcs, p->header->colorSpace, e_pcs,
fbintent, func)) != NULL)
break;
}
break;
default:
sprintf(p->err,"icc_get_luobj: Inaproptiate function requested");
p->errc = 1;
return NULL;
}
break;
case icSigOutputClass:
/* Expect BToA Lut and optional AToB Lut, All intents, expect gamut */
/* or momochrome table profile (reversable) */
/* Device <-> PCS */
/* Gamut Lut - no intent */
/* Optional preview links PCS <-> PCS */
/* Determine the algorithm and set its parameters */
switch (func) {
icTagSignature ttag;
case icmFwd: /* Device to PCS */
if (intent == icmDefaultIntent)
intent = icPerceptual; /* Make this the default */
switch ((int)intent) {
case icRelativeColorimetric:
case icAbsoluteColorimetric:
ttag = icSigAToB1Tag;
break;
case icPerceptual:
case icmAbsolutePerceptual: /* Special icclib intent */
ttag = icSigAToB0Tag;
break;
case icSaturation:
case icmAbsoluteSaturation: /* Special icclib intent */
ttag = icSigAToB2Tag;
break;
default:
sprintf(p->err,"icc_get_luobj: Unknown intent");
p->errc = 1;
return NULL;
}
if (order != icmLuOrdRev) {
/* Try Lut type lookup first */
if ((luobj = icc_new_icmLuLut(p, ttag,
p->header->colorSpace, pcs, pcs,
p->header->colorSpace, e_pcs, e_pcs,
intent, func)) != NULL) {
break;
}
/* See if it could be a matrix lookup */
if ((luobj = new_icmLuMatrixFwd(p,
p->header->colorSpace, pcs, pcs,
p->header->colorSpace, e_pcs, e_pcs,
intent, func)) != NULL)
break;
/* See if it could be a monochrome lookup */
if ((luobj = new_icmLuMonoFwd(p,
p->header->colorSpace, pcs, pcs,
p->header->colorSpace, e_pcs, e_pcs,
intent, func)) != NULL)
break;
} else {
/* See if it could be a monochrome lookup */
if ((luobj = new_icmLuMonoFwd(p,
p->header->colorSpace, pcs, pcs,
p->header->colorSpace, e_pcs, e_pcs,
intent, func)) != NULL)
break;
/* See if it could be a matrix lookup */
if ((luobj = new_icmLuMatrixFwd(p,
p->header->colorSpace, pcs, pcs,
p->header->colorSpace, e_pcs, e_pcs,
intent, func)) != NULL)
break;
/* Try Lut type lookup last */
if ((luobj = icc_new_icmLuLut(p, ttag,
p->header->colorSpace, pcs, pcs,
p->header->colorSpace, e_pcs, e_pcs,
intent, func)) != NULL)
break;
}
break;
case icmBwd: /* PCS to Device */
if (intent == icmDefaultIntent)
intent = icPerceptual; /* Make this the default */
switch ((int)intent) {
case icRelativeColorimetric:
case icAbsoluteColorimetric:
ttag = icSigBToA1Tag;
break;
case icPerceptual:
case icmAbsolutePerceptual: /* Special icclib intent */
ttag = icSigBToA0Tag;
break;
case icSaturation:
case icmAbsoluteSaturation: /* Special icclib intent */
ttag = icSigBToA2Tag;
break;
default:
sprintf(p->err,"icc_get_luobj: Unknown intent");
p->errc = 1;
return NULL;
}
if (order != icmLuOrdRev) {
/* Try Lut type lookup first */
if ((luobj = icc_new_icmLuLut(p, ttag,
pcs, p->header->colorSpace, pcs,
e_pcs, p->header->colorSpace, e_pcs,
intent, func)) != NULL)
break;
/* See if it could be a matrix lookup */
if ((luobj = new_icmLuMatrixBwd(p,
pcs, p->header->colorSpace, pcs,
e_pcs, p->header->colorSpace, e_pcs,
intent, func)) != NULL)
break;
/* See if it could be a monochrome lookup */
if ((luobj = new_icmLuMonoBwd(p,
pcs, p->header->colorSpace, pcs,
e_pcs, p->header->colorSpace, e_pcs,
intent, func)) != NULL)
break;
} else {
/* See if it could be a monochrome lookup */
if ((luobj = new_icmLuMonoBwd(p,
pcs, p->header->colorSpace, pcs,
e_pcs, p->header->colorSpace, e_pcs,
intent, func)) != NULL)
break;
/* See if it could be a matrix lookup */
if ((luobj = new_icmLuMatrixBwd(p,
pcs, p->header->colorSpace, pcs,
e_pcs, p->header->colorSpace, e_pcs,
intent, func)) != NULL)
break;
/* Try Lut type lookup last */
if ((luobj = icc_new_icmLuLut(p, ttag,
pcs, p->header->colorSpace, pcs,
e_pcs, p->header->colorSpace, e_pcs,
intent, func)) != NULL)
break;
}
break;
case icmGamut: /* PCS to 1D */
#ifdef ICM_STRICT /* Allow only default and absolute */
if (intent != icmDefaultIntent
&& intent != icAbsoluteColorimetric) {
sprintf(p->err,"icc_get_luobj: Intent is inappropriate for Gamut table");
p->errc = 1;
return NULL;
}
#else /* Be more forgiving */
switch ((int)intent) {
case icAbsoluteColorimetric:
case icmAbsolutePerceptual: /* Special icclib intent */
case icmAbsoluteSaturation: /* Special icclib intent */
break;
case icmDefaultIntent:
case icRelativeColorimetric:
case icPerceptual:
case icSaturation:
intent = icmDefaultIntent; /* Make all other look like default */
break;
default:
sprintf(p->err,"icc_get_luobj: Unknown intent (0x%x)",intent);
p->errc = 1;
return NULL;
}
#endif
/* If the target tag exists, and it is a Lut */
luobj = icc_new_icmLuLut(p, icSigGamutTag,
pcs, icSigGrayData, pcs,
e_pcs, icSigGrayData, e_pcs,
intent, func);
break;
case icmPreview: /* PCS to PCS */
switch ((int)intent) {
case icRelativeColorimetric:
ttag = icSigPreview1Tag;
break;
case icPerceptual:
ttag = icSigPreview0Tag;
break;
case icSaturation:
ttag = icSigPreview2Tag;
break;
case icAbsoluteColorimetric:
case icmAbsolutePerceptual: /* Special icclib intent */
case icmAbsoluteSaturation: /* Special icclib intent */
sprintf(p->err,"icc_get_luobj: Intent is inappropriate for preview table");
p->errc = 1;
return NULL;
default:
sprintf(p->err,"icc_get_luobj: Unknown intent");
p->errc = 1;
return NULL;
}
/* If the target tag exists, and it is a Lut */
luobj = icc_new_icmLuLut(p, ttag,
pcs, pcs, pcs,
e_pcs, e_pcs, e_pcs,
intent, func);
break;
default:
sprintf(p->err,"icc_get_luobj: Inaproptiate function requested");
p->errc = 1;
return NULL;
}
break;
case icSigLinkClass:
/* Expect AToB0 Lut and optional BToA0 Lut, One intent in header */
/* Device <-> Device */
if (intent != p->header->renderingIntent
&& intent != icmDefaultIntent) {
sprintf(p->err,"icc_get_luobj: Intent is inappropriate for Link profile");
p->errc = 1;
return NULL;
}
intent = p->header->renderingIntent;
/* Determine the algorithm and set its parameters */
switch (func) {
case icmFwd: /* Device to PCS (== Device) */
luobj = icc_new_icmLuLut(p, icSigAToB0Tag,
p->header->colorSpace, pcs, pcs,
p->header->colorSpace, pcs, pcs,
intent, func);
break;
case icmBwd: /* PCS (== Device) to Device */
luobj = icc_new_icmLuLut(p, icSigBToA0Tag,
pcs, p->header->colorSpace, pcs,
pcs, p->header->colorSpace, pcs,
intent, func);
break;
default:
sprintf(p->err,"icc_get_luobj: Inaproptiate function requested");
p->errc = 1;
return NULL;
}
break;
case icSigAbstractClass:
/* Expect AToB0 Lut and option BToA0 Lut, with either relative or absolute intent. */
/* PCS <-> PCS */
/* Determine the algorithm and set its parameters */
if (intent != icmDefaultIntent
&& intent != icRelativeColorimetric
&& intent != icAbsoluteColorimetric) {
sprintf(p->err,"icc_get_luobj: Intent is inappropriate for Abstract profile");
p->errc = 1;
return NULL;
}
switch (func) {
case icmFwd: /* PCS (== Device) to PCS */
luobj = icc_new_icmLuLut(p, icSigAToB0Tag,
p->header->colorSpace, pcs, pcs,
e_pcs, e_pcs, e_pcs,
intent, func);
break;
case icmBwd: /* PCS to PCS (== Device) */
luobj = icc_new_icmLuLut(p, icSigBToA0Tag,
pcs, p->header->colorSpace, pcs,
e_pcs, e_pcs, e_pcs,
intent, func);
break;
default:
sprintf(p->err,"icc_get_luobj: Inaproptiate function requested");
p->errc = 1;
return NULL;
}
break;
case icSigNamedColorClass:
/* Expect Name -> Device, Optional PCS */
/* and a reverse lookup would be useful */
/* (ie. PCS or Device coords to closest named color) */
/* ~~ to be implemented ~~ */
/* ~~ Absolute intent is valid for processing of */
/* PCS from named Colors. Also allow for e_pcs */
if (intent != icmDefaultIntent
&& intent != icRelativeColorimetric
&& intent != icAbsoluteColorimetric) {
sprintf(p->err,"icc_get_luobj: Intent is inappropriate for Named Color profile");
p->errc = 1;
return NULL;
}
sprintf(p->err,"icc_get_luobj: Named Colors not handled yet");
p->errc = 1;
return NULL;
default:
sprintf(p->err,"icc_get_luobj: Unknown profile class");
p->errc = 1;
return NULL;
}
if (luobj == NULL) {
sprintf(p->err,"icc_get_luobj: Unable to locate usable conversion");
p->errc = 1;
} else {
luobj->order = order;
}
return luobj;
}
/* - - - - - - - - - - - - - - - - - - - - - - - */
/* Returns total ink limit and channel maximums. */
/* Returns -1.0 if not applicable for this type of profile. */
/* Returns -1.0 for grey, additive, or any profiles < 4 channels. */
/* This is a place holder that uses a heuristic, */
/* until there is a private or standard tag for this information */
static double icm_get_tac( /* return TAC */
icc *p,
double *chmax, /* device return channel sums. May be NULL */
void (*calfunc)(void *cntx, double *out, double *in), /* Optional calibration func. */
void *cntx
) {
icmHeader *rh = p->header;
icmLuBase *luo;
icmLuLut *ll;
icmLut *lut;
icColorSpaceSignature outs; /* Type of output space */
int inn, outn; /* Number of components */
icmLuAlgType alg; /* Type of lookup algorithm */
double tac = 0.0;
double max[MAX_CHAN]; /* Channel maximums */
int i, f;
unsigned int uf;
int size; /* Lut table size */
double *gp; /* Pointer to grid cube base */
/* If not something that can really have a TAC */
if (rh->deviceClass != icSigDisplayClass
&& rh->deviceClass != icSigOutputClass
&& rh->deviceClass != icSigLinkClass) {
return -1.0;
}
/* If not a suitable color space */
switch (rh->colorSpace) {
/* Not applicable */
case icSigXYZData:
case icSigLabData:
case icSigLuvData:
case icSigYCbCrData:
case icSigYxyData:
case icSigHsvData:
case icSigHlsData:
return -1.0;
/* Assume no limit */
case icSigGrayData:
case icSig2colorData:
case icSig3colorData:
case icSigRgbData:
return -1.0;
default:
break;
}
/* Get a PCS->device colorimetric lookup */
if ((luo = p->get_luobj(p, icmBwd, icRelativeColorimetric, icmSigDefaultData, icmLuOrdNorm)) == NULL) {
if ((luo = p->get_luobj(p, icmBwd, icmDefaultIntent, icmSigDefaultData, icmLuOrdNorm)) == NULL) {
return -1.0;
}
}
/* Get details of conversion (Arguments may be NULL if info not needed) */
luo->spaces(luo, NULL, &inn, &outs, &outn, &alg, NULL, NULL, NULL, NULL);
/* Assume any non-Lut type doesn't have a TAC */
if (alg != icmLutType) {
return -1.0;
}
ll = (icmLuLut *)luo;
/* We have a Lut type. Search the lut for the largest values */
for (f = 0; f < outn; f++)
max[f] = 0.0;
lut = ll->lut;
gp = lut->clutTable; /* Base of grid array */
size = sat_pow(lut->clutPoints,lut->inputChan);
for (i = 0; i < size; i++) {
double tot, vv[MAX_CHAN];
lut->lookup_output(lut,vv,gp); /* Lookup though output tables */
ll->out_denormf(vv,vv); /* Normalize for output color space */
if (calfunc != NULL)
calfunc(cntx, vv, vv); /* Apply device calibration */
for (tot = 0.0, uf = 0; uf < lut->outputChan; uf++) {
tot += vv[uf];
if (vv[uf] > max[uf])
max[uf] = vv[uf];
}
if (tot > tac)
tac = tot;
gp += lut->outputChan;
}
if (chmax != NULL) {
for (f = 0; f < outn; f++)
chmax[f] = max[f];
}
luo->del(luo);
return tac;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - */
/* Create an empty object. Return NULL on error */
icc *new_icc_a(
icmAlloc *al /* Memory allocator */
) {
unsigned int i;
icc *p;
if ((p = (icc *) al->calloc(al, 1,sizeof(icc))) == NULL) {
return NULL;
}
p->ver = icmVersionDefault; /* default is V2.2.0 profile */
p->al = al; /* Heap allocator */
p->get_rfp = icc_get_rfp;
p->set_version = icc_set_version;
p->get_size = icc_get_size;
p->read = icc_read;
p->read_x = icc_read_x;
p->write = icc_write;
p->write_x = icc_write_x;
p->dump = icc_dump;
p->del = icc_delete;
p->add_tag = icc_add_tag;
p->link_tag = icc_link_tag;
p->find_tag = icc_find_tag;
p->read_tag = icc_read_tag;
p->read_tag_any = icc_read_tag_any;
p->rename_tag = icc_rename_tag;
p->unread_tag = icc_unread_tag;
p->read_all_tags = icc_read_all_tags;
p->delete_tag = icc_delete_tag;
p->check_id = icc_check_id;
p->get_tac = icm_get_tac;
p->get_luobj = icc_get_luobj;
p->new_clutluobj = icc_new_icmLuLut;
p->set_illum = icc_set_illum;
p->chromAdaptMatrix = icc_chromAdaptMatrix;
#if defined(__IBMC__) && defined(_M_IX86)
_control87(EM_UNDERFLOW, EM_UNDERFLOW);
#endif
/* Allocate a header object */
if ((p->header = new_icmHeader(p)) == NULL) {
al->free(al, p);
return NULL;
}
/* Values that must be set before writing */
p->header->deviceClass = icMaxEnumClass;/* Type of profile - must be set! */
p->header->colorSpace = icMaxEnumData; /* Clr space of data - must be set! */
p->header->pcs = icMaxEnumData; /* PCS: XYZ or Lab - must be set! */
p->header->renderingIntent = icMaxEnumIntent; /* Rendering intent - must be set ! */
/* Values that should be set before writing */
p->header->manufacturer = icmSigUnknownType;/* Dev manufacturer - should be set ! */
p->header->model = icmSigUnknownType; /* Dev model number - should be set ! */
p->header->attributes.l = 0; /* ICC Device attributes - should set ! */
p->header->flags = 0; /* Embedding flags - should be set ! */
/* Values that may be set before writing */
p->header->attributes.h = 0; /* Dev Device attributes - may be set ! */
p->header->creator = str2tag("argl"); /* Profile creator - Argyll - may be set ! */
/* Init default values in header */
p->header->cmmId = str2tag("argl"); /* CMM for profile - Argyll CMM */
p->header->majv = 2; /* Current version 2.2.0 */
p->header->minv = 2;
p->header->bfv = 0;
setcur_DateTimeNumber(&p->header->date);/* Creation Date */
#ifdef NT
p->header->platform = icSigMicrosoft; /* Primary Platform */
#endif
#ifdef __APPLE__
p->header->platform = icSigMacintosh;
#endif
#if defined(UNIX) && !defined(__APPLE__)
p->header->platform = icmSig_nix;
#endif
p->header->illuminant = icmD50; /* Profile illuminant - D50 */
/* Values that will be created automatically */
for (i = 0; i < 16; i++)
p->header->id[i] = 0;
/* Should we use ICC standard Wrong Von Kries for */
/* white point chromatic adapation for output class ? */
if (getenv("ARGYLL_CREATE_WRONG_VON_KRIES_OUTPUT_CLASS_REL_WP") != NULL)
p->useLinWpchtmx = 1; /* Use Wrong Von Kries */
else
p->useLinWpchtmx = 0; /* Use Bradford by default */
p->wpchtmx_class = icMaxEnumClass; /* Not set yet - auto set on create. */
/* Default to saving ArgyllCMS private 'arts' tag (if appropriate type of */
/* profile) to make white point chromatic adapation explicit. */
p->useArts = 1;
/* Should we create a V4 style Display profile with D50 media white point */
/* tag and 'chad' tag ? - or - */
/* Should we create an Output profile using a 'chad' tag if it uses */
/* a non-standard illuminant ? */
if (getenv("ARGYLL_CREATE_DISPLAY_PROFILE_WITH_CHAD") != NULL)
p->wrDChad = 1; /* For Display profile mark media WP as D50 and put */
/* absolute to relative transform matrix in 'chad' tag. */
else
p->wrDChad = 0; /* No by default - use Bradford and store real Media WP */
/* Should we create an Output profile using a 'chad' tag if it uses */
/* a non-standard illuminant ? */
if (getenv("ARGYLL_CREATE_OUTPUT_PROFILE_WITH_CHAD") != NULL)
p->wrOChad = 1; /* For Output profile, put illuminant to D50 Bradford */
/* matrix in 'chad' tag, and transform real WP by it. */
else
p->wrOChad = 0; /* No by default - Media WP inclues effect of illuminant. */
/* Set a default wpchtmx in case the profile being read or written */
/* doesn't use a white point (i.e., it's a device link) */
/* This will be reset if the wpchtmx_class gets changed. */
if (!p->useLinWpchtmx) {
icmCpy3x3(p->wpchtmx, icmBradford);
icmInverse3x3(p->iwpchtmx, p->wpchtmx);
} else {
icmCpy3x3(p->wpchtmx, icmWrongVonKries);
icmCpy3x3(p->iwpchtmx, icmWrongVonKries);
}
return p;
}
/* ---------------------------------------------------------- */
/* Convert an angle in radians into chromatic RGB values */
/* in a simple geometric fashion, with 0 = Red. */
void icmRad2RGB(double rgb[3], double ang) {
double th1 = 1.0/3.0 * 2.0 * M_PI;
double th2 = 2.0/3.0 * 2.0 * M_PI;
double bl;
while (ang < 0.0)
ang += 2.0 * M_PI;
while (ang >= (2.0 * M_PI))
ang -= 2.0 * M_PI;
if (ang < th1) {
bl = ang/th1;
rgb[0] = (1.0 - bl);
rgb[1] = bl;
rgb[2] = 0.0;
} else if (ang < th2) {
bl = (ang - th1)/th1;
rgb[0] = 0.0;
rgb[1] = (1.0 - bl);
rgb[2] = bl;
} else {
bl = (ang - th2)/th1;
rgb[0] = bl;
rgb[1] = 0.0;
rgb[2] = (1.0 - bl);
}
}
/* ---------------------------------------------------------- */
/* Print an int vector to a string. */
/* Returned static buffer is re-used every 5 calls. */
char *icmPiv(int di, int *p) {
static char buf[5][MAX_CHAN * 16];
static int ix = 0;
int e;
char *bp;
if (++ix >= 5)
ix = 0;
bp = buf[ix];
if (di > MAX_CHAN)
di = MAX_CHAN; /* Make sure that buf isn't overrun */
for (e = 0; e < di; e++) {
if (e > 0)
*bp++ = ' ';
sprintf(bp, "%d", p[e]); bp += strlen(bp);
}
return buf[ix];
}
/* Print a double color vector to a string. */
/* Returned static buffer is re-used every 5 calls. */
char *icmPdv(int di, double *p) {
static char buf[5][MAX_CHAN * 16];
static int ix = 0;
int e;
char *bp;
if (++ix >= 5)
ix = 0;
bp = buf[ix];
if (di > MAX_CHAN)
di = MAX_CHAN; /* Make sure that buf isn't overrun */
for (e = 0; e < di; e++) {
if (e > 0)
*bp++ = ' ';
sprintf(bp, "%.8f", p[e]); bp += strlen(bp);
}
return buf[ix];
}
/* Print a float color vector to a string. */
/* Returned static buffer is re-used every 5 calls. */
char *icmPfv(int di, float *p) {
static char buf[5][MAX_CHAN * 16];
static int ix = 0;
int e;
char *bp;
if (++ix >= 5)
ix = 0;
bp = buf[ix];
if (di > MAX_CHAN)
di = MAX_CHAN; /* Make sure that buf isn't overrun */
for (e = 0; e < di; e++) {
if (e > 0)
*bp++ = ' ';
sprintf(bp, "%.8f", p[e]); bp += strlen(bp);
}
return buf[ix];
}
/* Print an 0..1 range XYZ as a D50 Lab string */
/* Returned static buffer is re-used every 5 calls. */
char *icmPLab(double *p) {
static char buf[5][MAX_CHAN * 16];
static int ix = 0;
int e;
char *bp;
double lab[3];
if (++ix >= 5)
ix = 0;
bp = buf[ix];
icmXYZ2Lab(&icmD50, lab, p);
for (e = 0; e < 3; e++) {
if (e > 0)
*bp++ = ' ';
sprintf(bp, "%f", lab[e]); bp += strlen(bp);
}
return buf[ix];
}
/* ---------------------------------------------------------- */
|