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
|
# -*- coding: utf-8 -*-
# stdlib
import codecs
from binascii import hexlify
import atexit
import ctypes
import datetime
import getpass
import http.client
import math
import mimetypes
import os
from io import BytesIO
from pathlib import Path
import distro
import platform
import re
import socket
import shutil
import string
import struct
import subprocess as sp
import sys
import tempfile
import textwrap
import threading
import traceback
import urllib.request
import urllib.parse
import urllib.error
import warnings
import zipfile
import zlib
from collections import UserString
from hashlib import md5, sha256
from threading import currentThread
from time import sleep, strftime, time
from send2trash import send2trash
if sys.platform == "darwin":
from platform import mac_ver
from _thread import start_new_thread
elif sys.platform == "win32":
from ctypes import windll
else:
import grp
# 3rd party
if sys.platform == "win32":
from win32com.shell import shell as win32com_shell
import pythoncom
import win32api
import win32con
import win32event
import pywintypes
import winerror
# custom
from DisplayCAL.cgats import (
CGATS,
CGATSError,
CGATSInvalidError,
CGATSInvalidOperationError,
CGATSKeyError,
CGATSTypeError,
CGATSValueError,
rpad,
stable_sort_by_L,
sort_by_rec709_luma,
sort_by_RGB,
sort_by_RGB_sum,
)
from DisplayCAL import audio
from DisplayCAL import colormath
from DisplayCAL import config
from DisplayCAL import defaultpaths
from DisplayCAL import imfile
from DisplayCAL import localization as lang
from DisplayCAL import wexpect
from DisplayCAL.argyll import (
check_argyll_bin,
check_set_argyll_bin,
get_argyll_instrument_config,
get_argyll_util,
get_argyll_utilname,
get_argyll_version_string,
make_argyll_compatible_path,
parse_argyll_version_string,
)
from DisplayCAL.argyll_cgats import (
add_dispcal_options_to_cal,
add_options_to_ti3,
cal_to_fake_profile,
cal_to_vcgt,
extract_cal_from_profile,
extract_cal_from_ti3,
extract_device_gray_primaries,
extract_fix_copy_cal,
ti3_to_ti1,
verify_cgats,
verify_ti1_rgb_xyz,
)
from DisplayCAL.argyll_instruments import (
get_canonical_instrument_name,
instruments as all_instruments,
)
from DisplayCAL.argyll_names import (
viewconds,
intents,
observers,
)
from DisplayCAL.colormath import VidRGB_to_eeColor, eeColor_to_VidRGB
from DisplayCAL.config import (
autostart,
autostart_home,
script_ext,
defaults,
enc,
exe,
exedir,
exe_ext,
fs_enc,
getcfg,
geticon,
get_data_path,
get_total_patches,
isapp,
is_ccxx_testchart,
profile_ext,
pydir,
setcfg,
setcfg_cond,
split_display_name,
writecfg,
appbasename,
)
from DisplayCAL.debughelpers import (
Error,
DownloadError,
Info,
UnloggedError,
UnloggedInfo,
UnloggedWarning,
UntracedError,
Warn,
handle_error,
)
from DisplayCAL.defaultpaths import (
cache,
iccprofiles_home,
iccprofiles_display_home,
appdata,
)
from DisplayCAL.edid import WMIError, get_edid
from DisplayCAL.icc_profile import (
_mp_apply,
chromaticAdaptionTag,
ChromaticityType,
create_RGB_A2B_XYZ,
create_synthetic_hdr_clut_profile,
CRInterpolation,
CurveType,
DictType,
ICCProfile,
ICCProfileInvalidError,
ICCProfileTag,
GAMUT_VOLUME_SRGB,
get_display_profile,
LUT16Type,
MultiLocalizedUnicodeType,
ProfileSequenceDescType,
s15Fixed16Number,
s15Fixed16Number_tohex,
set_display_profile,
Text,
TextDescriptionType,
TextType,
VideoCardGammaTableType,
VideoCardGammaType,
XYZType,
)
from DisplayCAL.log import DummyLogger, LogFile, get_file_logger, log
from DisplayCAL import madvr
from DisplayCAL.meta import VERSION, VERSION_BASE, DOMAIN, name as appname, version
from DisplayCAL.multiprocess import cpu_count, pool_slice
from DisplayCAL.options import (
always_fail_download,
debug,
eecolor65,
experimental,
test,
test_badssl,
test_require_sensor_cal,
verbose,
)
from DisplayCAL.network import LoggingHTTPRedirectHandler, NoHTTPRedirectHandler
from DisplayCAL.patterngenerators import (
PrismaPatternGeneratorClient,
ResolveLSPatternGeneratorServer,
ResolveCMPatternGeneratorServer,
WebWinHTTPPatternGeneratorServer,
)
from DisplayCAL.util_decimal import stripzeros
from DisplayCAL.util_dict import dict_sort
from DisplayCAL.util_http import encode_multipart_formdata
from DisplayCAL.util_io import (
EncodedWriter,
Files,
GzipFileProper,
LineBufferedStream,
LineCache,
StringIOu as StringIO,
TarFileProper,
)
from DisplayCAL.util_list import intlist, natsort
if sys.platform == "darwin":
from DisplayCAL.util_mac import (
mac_app_activate,
mac_terminal_do_script,
mac_terminal_set_colors,
osascript,
get_model_id,
)
elif sys.platform == "win32":
from DisplayCAL import util_win
from DisplayCAL.util_win import run_as_admin, shell_exec, win_ver
try:
import wmi
except Exception as exception:
print("Error - could not import WMI:", exception)
wmi = None
else:
# Linux
from DisplayCAL.defaultpaths import xdg_data_home
try:
from DisplayCAL.util_dbus import (
DBusObject,
DBusException,
DBusObjectError,
BUSTYPE_SESSION,
dbus_session,
dbus_system,
)
except ImportError:
dbus_session = None
dbus_system = None
from DisplayCAL import colord
from DisplayCAL.util_os import (
dlopen,
getenvu,
is_superuser,
launch_file,
make_win32_compatible_long_path,
mksfile,
mkstemp_bypath,
quote_args,
safe_glob,
which,
)
if sys.platform not in ("darwin", "win32"):
from DisplayCAL.util_os import getgroups
if sys.platform == "win32" and sys.getwindowsversion() >= (6,):
from DisplayCAL.util_os import win64_disable_file_system_redirection
from DisplayCAL.util_str import (
make_filename_safe,
safe_basestring,
safe_asciize,
safe_str,
strtr,
universal_newlines,
)
from DisplayCAL.worker_base import (
MP_Xicclu,
WorkerBase,
Xicclu,
_mp_generate_B2A_clut,
_mp_xicclu,
printcmdline,
)
from DisplayCAL.wxaddons import BetterCallLater, BetterWindowDisabler, wx
from DisplayCAL.wxwindows import (
ConfirmDialog,
HtmlInfoDialog,
InfoDialog,
ProgressDialog,
SimpleTerminal,
show_result_dialog,
)
from DisplayCAL.wxDisplayAdjustmentFrame import DisplayAdjustmentFrame
from DisplayCAL.wxDisplayUniformityFrame import DisplayUniformityFrame
from DisplayCAL.wxUntetheredFrame import UntetheredFrame
RDSMM = None
if sys.platform not in ("darwin", "win32"):
try:
from DisplayCAL import RealDisplaySizeMM as RDSMM
except ImportError as exception:
warnings.warn(str(exception), ImportWarning)
import wx.lib.delayedresult as delayedresult
INST_CAL_MSGS = [
"Do a reflective white calibration",
"Do a transmissive white calibration",
"Do a transmissive dark calibration",
"Place the instrument on its reflective white reference",
"Click the instrument on its reflective white reference",
"Place the instrument in the dark",
"Place cap on the instrument", # i1 Pro/SpyderX
"Place ambient adapter and cap on the instrument",
"Set instrument sensor to calibration position", # ColorMunki
"Place the instrument on its transmissive white source",
"Use the appropriate tramissive blocking",
"Change filter on instrument to",
]
USE_WPOPEN = 0
keycodes = {
wx.WXK_NUMPAD0: ord("0"),
wx.WXK_NUMPAD1: ord("1"),
wx.WXK_NUMPAD2: ord("2"),
wx.WXK_NUMPAD3: ord("3"),
wx.WXK_NUMPAD4: ord("4"),
wx.WXK_NUMPAD5: ord("5"),
wx.WXK_NUMPAD6: ord("6"),
wx.WXK_NUMPAD7: ord("7"),
wx.WXK_NUMPAD8: ord("8"),
wx.WXK_NUMPAD9: ord("9"),
wx.WXK_NUMPAD_ADD: ord("+"),
wx.WXK_NUMPAD_ENTER: ord("\n"),
wx.WXK_NUMPAD_EQUAL: ord("="),
wx.WXK_NUMPAD_DIVIDE: ord("/"),
wx.WXK_NUMPAD_MULTIPLY: ord("*"),
wx.WXK_NUMPAD_SUBTRACT: ord("-"),
}
workers = []
WAIT_FILE_TEMPLATE = """# coding=utf-8
import os, sys, time
if sys.platform != "win32":
print(*(["\\nCurrent RGB"] + sys.argv[1:]))
abortfilename = os.path.join(r"{script_dir}", ".abort")
okfilename = os.path.join(r"{script_dir}", ".ok")
while True:
if os.path.isfile(abortfilename):
break
if os.path.isfile(okfilename):
try:
os.remove(okfilename)
except OSError as e:
pass
else:
break
time.sleep(0.001)
"""
def add_keywords_to_cgats(cgats, keywords):
"""Add keywords to CGATS"""
if not isinstance(cgats, CGATS):
cgats = CGATS(cgats)
for keyword in keywords:
value = keywords[keyword]
cgats[0].add_keyword(keyword, value)
return cgats
def check_create_dir(path):
"""Try to create a directory and show an error message on failure."""
if not os.path.exists(path):
try:
os.makedirs(path)
except Exception as exception:
return Error(f"{lang.getstr('error.dir_creation', path)}\n\n{exception}")
if not os.path.isdir(path):
return Error(lang.getstr("error.dir_notdir", path))
return True
def check_cal_isfile(cal=None, missing_msg=None, notfile_msg=None, silent=False):
"""Check if a calibration file exists and show an error message if not."""
if not silent:
if not missing_msg:
missing_msg = lang.getstr("error.calibration.file_missing", cal)
if not notfile_msg:
notfile_msg = lang.getstr("file_notfile", cal)
return check_file_isfile(cal, missing_msg, notfile_msg, silent)
def check_profile_isfile(
profile_path=None, missing_msg=None, notfile_msg=None, silent=False
):
"""Check if a profile exists and show an error message if not."""
if not silent:
if not missing_msg:
missing_msg = lang.getstr("error.profile.file_missing", profile_path)
if not notfile_msg:
notfile_msg = lang.getstr("file_notfile", profile_path)
return check_file_isfile(profile_path, missing_msg, notfile_msg, silent)
def check_file_isfile(filename, missing_msg=None, notfile_msg=None, silent=False):
"""Check if a file exists and show an error message if not."""
if not os.path.exists(filename):
if not silent:
if not missing_msg:
missing_msg = lang.getstr("file.missing", filename)
return Error(missing_msg)
return False
if not os.path.isfile(filename):
if not silent:
if not notfile_msg:
notfile_msg = lang.getstr("file.notfile", filename)
return Error(notfile_msg)
return False
return True
def check_ti3_criteria1(
RGB,
XYZ,
black_XYZ,
white_XYZ,
delta_to_sRGB_threshold_E=10,
delta_to_sRGB_threshold_L=10,
delta_to_sRGB_threshold_C=75,
delta_to_sRGB_threshold_H=75,
print_debuginfo=True,
):
sRGBLab = colormath.RGB2Lab(
RGB[0] / 100.0, RGB[1] / 100.0, RGB[2] / 100.0, noadapt=not white_XYZ
)
if white_XYZ:
if black_XYZ:
black_Lab = colormath.XYZ2Lab(
*colormath.adapt(black_XYZ[0], black_XYZ[1], black_XYZ[2], white_XYZ)
)
black_C = math.sqrt(math.pow(black_Lab[1], 2) + math.pow(black_Lab[2], 2))
if black_Lab[0] < 3 and black_C < 3:
# Sanity check: Is this color reasonably dark and achromatic?
# Then do BPC so we can compare better to perfect black sRGB
XYZ = colormath.blend_blackpoint(
XYZ[0], XYZ[1], XYZ[2], black_XYZ, None, white_XYZ
)
XYZ = colormath.adapt(XYZ[0], XYZ[1], XYZ[2], white_XYZ)
Lab = colormath.XYZ2Lab(*XYZ)
delta_to_sRGB = colormath.delta(*sRGBLab + Lab + (2000,))
# Depending on how (a)chromatic the sRGB color is, scale the thresholds
# Use math derived from DE2000 formula to get chroma and hue angle
L, a, b = sRGBLab
b_pow = math.pow(b, 2)
C = math.sqrt(math.pow(a, 2) + b_pow)
C_pow = math.pow(C, 7)
G = 0.5 * (1 - math.sqrt(C_pow / (C_pow + math.pow(25, 7))))
a = (1 + G) * a
C = math.sqrt(math.pow(a, 2) + b_pow)
h = (
0
if a == 0 and b == 0
else math.degrees(math.atan2(b, a)) + (0 if b >= 0 else 360.0)
)
# C and h scaling factors
C_scale = C / 100.0
h_scale = h / 360.0
# RGB hue, saturation and value scaling factors
H, S, V = colormath.RGB2HSV(*[v / 100.0 for v in RGB])
SV_scale = S * V
# Scale the thresholds
delta_to_sRGB_threshold_E += delta_to_sRGB_threshold_E * max(C_scale, SV_scale)
delta_to_sRGB_threshold_L += delta_to_sRGB_threshold_L * max(C_scale, SV_scale)
# Allow higher chroma errors as luminance of reference decreases
L_scale = max(1 - (1 * C_scale) + (100.0 - L) / 100.0, 1)
delta_to_sRGB_threshold_C = (
delta_to_sRGB_threshold_C * max(C_scale, SV_scale) + 2
) * L_scale
delta_to_sRGB_threshold_H = (
delta_to_sRGB_threshold_H * max(C_scale, h_scale, H, SV_scale) + 2
) * L_scale
criteria1 = delta_to_sRGB["E"] > delta_to_sRGB_threshold_E and (
abs(delta_to_sRGB["L"]) > delta_to_sRGB_threshold_L
or abs(delta_to_sRGB["C"]) > delta_to_sRGB_threshold_C
or abs(delta_to_sRGB["H"]) > delta_to_sRGB_threshold_H
)
# This patch has an unusually high delta 00 to its sRGB equivalent
delta_to_sRGB["E_ok"] = delta_to_sRGB["E"] <= delta_to_sRGB_threshold_E
delta_to_sRGB["L_ok"] = abs(delta_to_sRGB["L"]) <= delta_to_sRGB_threshold_L
delta_to_sRGB["C_ok"] = abs(delta_to_sRGB["C"]) <= delta_to_sRGB_threshold_C
delta_to_sRGB["H_ok"] = abs(delta_to_sRGB["H"]) <= delta_to_sRGB_threshold_H
delta_to_sRGB["ok"] = (
delta_to_sRGB["E_ok"]
and delta_to_sRGB["L_ok"]
and delta_to_sRGB["C_ok"]
and delta_to_sRGB["H_ok"]
)
debuginfo = (
f"RGB: {RGB[0]:6.2f} {RGB[1]:6.2f} {RGB[2]:6.2f} "
f"RGB(sRGB)->Lab(D50): {sRGBLab[0]:6.2f} {sRGBLab[1]:6.2f} {sRGBLab[2]:6.2f} "
f"L_scale: {L_scale:5.3f} C: {C:5.2f} C_scale: {C_scale:5.3f} h: {h:5.2f} "
f"h_scale: {h_scale:5.3f} H: {H * 360:5.2f} H_scale: {H:5.3f} S: {S:5.2f} "
f"V: {V:5.2f} SV_scale: {SV_scale:5.3f} "
f"Thresholds: E {delta_to_sRGB_threshold_E:5.2f} "
f"L {delta_to_sRGB_threshold_L:5.2f} C {delta_to_sRGB_threshold_C:5.2f} "
f"H {delta_to_sRGB_threshold_H:5.2f} "
f"XYZ->Lab(D50): {Lab[0]:6.2f} {Lab[1]:6.2f} {Lab[2]:6.2f} "
f"delta RGB(sRGB)->Lab(D50) to XYZ->Lab(D50): dE {delta_to_sRGB['E']:5.2f} "
f"dL {delta_to_sRGB['L']:5.2f} dC {delta_to_sRGB['C']:5.2f} "
f"dH {delta_to_sRGB['H']:5.2f}"
)
if print_debuginfo:
print(debuginfo)
return sRGBLab, Lab, delta_to_sRGB, criteria1, debuginfo
def check_ti3_criteria2(
prev_Lab, Lab, prev_sRGBLab, sRGBLab, prev_RGB, RGB, sRGB_delta_E_scale_factor=0.5
):
delta = colormath.delta(*prev_Lab + Lab + (2000,))
sRGB_delta = colormath.delta(*prev_sRGBLab + sRGBLab + (2000,))
sRGB_delta["E"] *= sRGB_delta_E_scale_factor
criteria2 = delta["E"] < sRGB_delta["E"]
# These two patches have different RGB values
# but suspiciously low delta E 76.
if criteria2 and (
prev_RGB[0] == prev_RGB[1] == prev_RGB[2] and RGB[0] == RGB[1] == RGB[2]
):
# If RGB gray, check if the Y difference makes sense
criteria2 = (RGB[0] > prev_RGB[0] and Lab[0] <= prev_Lab[0]) or (
RGB[0] < prev_RGB[0] and Lab[0] >= prev_Lab[0]
)
delta["L_ok"] = not criteria2
delta["E_ok"] = True
else:
delta["E_ok"] = not criteria2
delta["L_ok"] = True
return delta, sRGB_delta, criteria2
def check_ti3(ti3, print_debuginfo=True):
"""Check subsequent patches' expected vs real deltaE and collect patches
with different RGB values, but suspiciously low delta E
Used as a means to find misreads.
The expected dE is calculated by converting from a patches RGB values
(assuming sRGB) to Lab and comparing the values.
"""
if not isinstance(ti3, CGATS):
ti3 = CGATS(ti3)
data = ti3.queryv1("DATA")
datalen = len(data)
black = data.queryi1({"RGB_R": 0, "RGB_G": 0, "RGB_B": 0})
if black:
black = black["XYZ_X"], black["XYZ_Y"], black["XYZ_Z"]
elif print_debuginfo:
print("Warning - no black patch found in CGATS")
white = data.queryi1({"RGB_R": 100, "RGB_G": 100, "RGB_B": 100})
if white:
white = white["XYZ_X"], white["XYZ_Y"], white["XYZ_Z"]
elif print_debuginfo:
print("Warning - no white patch found in CGATS")
suspicious = []
prev = {}
delta = {}
for index in data:
item = data[index]
(sRGBLab, Lab, delta_to_sRGB, criteria1, debuginfo) = check_ti3_criteria1(
(item["RGB_R"], item["RGB_G"], item["RGB_B"]),
(item["XYZ_X"], item["XYZ_Y"], item["XYZ_Z"]),
black,
white,
print_debuginfo=False,
)
if criteria1 or (
prev
and (
max(prev["item"]["RGB_R"], item["RGB_R"])
- min(prev["item"]["RGB_R"], item["RGB_R"])
> 1.0 / 2.55
or max(prev["item"]["RGB_G"], item["RGB_G"])
- min(prev["item"]["RGB_G"], item["RGB_G"])
> 1.0 / 2.55
or max(prev["item"]["RGB_B"], item["RGB_B"])
- min(prev["item"]["RGB_B"], item["RGB_B"])
> 1.0 / 2.55
)
):
if prev:
(delta, sRGB_delta, criteria2) = check_ti3_criteria2(
prev["Lab"],
Lab,
prev["sRGBLab"],
sRGBLab,
(
prev["item"]["RGB_R"],
prev["item"]["RGB_G"],
prev["item"]["RGB_B"],
),
(item["RGB_R"], item["RGB_G"], item["RGB_B"]),
)
else:
criteria2 = False
if criteria1 or criteria2:
if print_debuginfo:
if criteria2:
debuginfo = (
f"{debuginfo} dE to previous XYZ->Lab(D50): "
f"{delta['E']:5.3f} dE_OK: {delta['E_ok']} "
f"L_OK: {delta['L_ok']} "
"0.5 dE RGB(sRGB)->Lab(D50) to previous "
f"RGB(sRGB)->Lab(D50): {sRGB_delta['E']:5.3f}"
)
sample_id = f"Patch #{{:.0{len(str(datalen)):d}f}}"
print(sample_id.format(item.SAMPLE_ID), debuginfo)
suspicious.append(
(
prev["item"] if criteria2 else None,
item,
delta if criteria2 else None,
sRGB_delta if criteria2 else None,
prev["delta_to_sRGB"] if criteria2 else None,
delta_to_sRGB,
)
)
prev["item"] = item
prev["sRGBLab"] = sRGBLab
prev["Lab"] = Lab
prev["delta_to_sRGB"] = delta_to_sRGB
return suspicious
def create_shaper_curves(
RGB_XYZ,
bwd_mtx,
single_curve=False,
bpc=True,
logfn=None,
slope_limit=False,
profile=None,
options_dispcal=None,
optimize=False,
cat="Bradford",
):
"""Create input (device to PCS) shaper curves"""
RGB_XYZ = dict_sort(RGB_XYZ)
R_R = []
G_G = []
B_B = []
R_X = []
G_Y = []
B_Z = []
XYZbp = None
XYZwp = None
for R, G, B in RGB_XYZ:
X, Y, Z = RGB_XYZ[(R, G, B)]
X, Y, Z = colormath.adapt(X, Y, Z, RGB_XYZ[(100, 100, 100)], cat=cat)
if 100 > R > 0 and min(X, Y, Z) < 100.0 / 65535:
# Skip non-black/non-white gray values not encodable in 16-bit
continue
if 100 > R > 0 and G_Y and (Y < G_Y[-1] * 100 or Y > 100):
# Skip values with negative Y increments,
# or Y above 100 with RGB < 100
if logfn:
logfn(
f"Warning: Skipping RGB {R:.2f} {G:.2f} {B:.2f} "
f"XYZ {X:.6f} {Y:.6f} {Z:.6f}",
"because Y is not monotonically increasing",
)
continue
R_R.append(R / 100.0)
G_G.append(G / 100.0)
B_B.append(B / 100.0)
R_X.append(X / 100.0)
G_Y.append(Y / 100.0)
B_Z.append(Z / 100.0)
if R == G == B == 0:
XYZbp = [v / 100.0 for v in (X, Y, Z)]
elif R == G == B == 100:
XYZwp = [v / 100.0 for v in (X, Y, Z)]
numvalues = len(R_R)
if numvalues <= 2 or XYZbp[1] >= XYZwp[1] * 0.9:
# Botched measurements
raise UntracedError(lang.getstr("error.luminance.not_monotonically_increasing"))
if XYZwp[1] <= 0 or math.isnan(XYZwp[1]):
raise UntracedError(lang.getstr("error.luminance.invalid"))
# Scale black to zero
for i in range(numvalues):
if bwd_mtx * [1, 1, 1] == [1, 1, 1]:
(R_X[i], G_Y[i], B_Z[i]) = colormath.apply_bpc(
R_X[i], G_Y[i], B_Z[i], XYZbp, (0, 0, 0), XYZwp
)
else:
(R_X[i], G_Y[i], B_Z[i]) = colormath.blend_blackpoint(
R_X[i], G_Y[i], B_Z[i], XYZbp
)
# Interpolate TRC
use_individual_channel_gammas = True
gammas = [[], [], []]
for j, gamma in enumerate(gammas):
values = []
for i, v in enumerate((R_R, G_G, B_B)[j]):
channel = (R_X, G_Y, B_Z)[j]
vv = colormath.convert_range(
channel[i], channel[0], XYZwp[j], channel[0], 1
)
if use_individual_channel_gammas:
channel[i] = vv
values.append((v, vv))
gamma[:] = colormath.get_gamma(values, average=False)
gamma.insert(0, gamma[0])
gamma.append(gamma[-1])
if bwd_mtx * [1, 1, 1] == [1, 1, 1]:
# cLUT profile
final = 2049
else:
# Shaper + matrix profile
final = 256
if all(len(gamma) == numvalues for gamma in gammas):
# Follow curvature by using gamma as hint
# This can be used to fill in 'missing' values with the right slope
# E.g. suppose we have measured at signal levels 0%, 25%, 50%, 75%, 100%
# Linearly interpolating between those (or even using polynomials)
# will yield unacceptable results especially near black due to not
# following the typical curvature of additive displays. Using this
# method, a sensible curvature is maintained.
numentries = numvalues
while numentries < 2**12 + 1:
numentries = (numentries - 1) * 2 + 1
RGB = (R_R, G_G, B_B)
gammas_resized = [
gamma
and colormath.interp_fill(
RGB[j],
gamma,
(numvalues - 1) * 2 + 1,
# numentries,
True,
)
for j, gamma in enumerate(gammas)
]
for gamma in gammas_resized:
gamma[1:-1] = colormath.smooth_avg(gamma[1:-1], 1)
gammas_resized = [
gamma and colormath.interp_resize(gamma, numentries, True)
for gamma in gammas_resized
]
for i in range(1, numvalues - 1):
X, Y, Z = R_X[i], G_Y[i], B_Z[i]
if use_individual_channel_gammas:
R_X[i], G_Y[i], B_Z[i] = (
v ** (1.0 / gammas[j][i]) for j, v in enumerate((X, Y, Z))
)
else:
if Y < 0:
# This should NOT happen - is this check needed?
if logfn:
logfn("Warning: Y is negative - clamping to zero")
Y = 0
if Y:
Y2 = Y ** (1.0 / gammas[1][i])
R_X[i], G_Y[i], B_Z[i] = (v / Y * Y2 for v in (X, Y, Z))
for j, values in enumerate((R_X, G_Y, B_Z)):
values[:] = colormath.interp_fill(RGB[j], values, numentries, True)
for i in range(1, numentries - 1):
X, Y, Z = R_X[i], G_Y[i], B_Z[i]
if use_individual_channel_gammas:
X, Y, Z = (
colormath.convert_range(
v ** gammas_resized[j][i],
(R_X, G_Y, B_Z)[j][0],
1,
(R_X, G_Y, B_Z)[j][0],
XYZwp[j],
)
for j, v in enumerate((X, Y, Z))
)
R_X[i], G_Y[i], B_Z[i] = X, Y, Z
else:
if Y:
Y2 = Y ** gammas_resized[1][i]
R_X[i], G_Y[i], B_Z[i] = (v / Y * Y2 for v in (X, Y, Z))
for values in RGB:
values[:] = colormath.interp_fill(values, values, numentries, True)
# for i in range(numentries):
# print(i, R_R[i], G_G[i], B_B[i], gammas_resized[0][i],
# gammas_resized[1][i], gammas_resized[2][i], R_X[i],
# G_Y[i], B_Z[i])
numentries = final
else:
# Hmm. Can this happen? Use old (pre 3.8.9.2) interpolation
numentries = 33
rinterp = colormath.Interp(R_R, R_X, use_numpy=True)
ginterp = colormath.Interp(G_G, G_Y, use_numpy=True)
binterp = colormath.Interp(B_B, B_Z, use_numpy=True)
curves = []
for i in range(3):
curves.append([])
maxval = numentries - 1.0
powinterp = {
"r": colormath.Interp([], []),
"g": colormath.Interp([], []),
"b": colormath.Interp([], []),
}
RGBwp = bwd_mtx * XYZwp
for n in range(numentries):
n /= maxval
if numentries < final:
# Apply slight power to input value so we sample near
# black more accurately
n **= 1.2
Y = ginterp(n)
X = rinterp(n)
Z = binterp(n)
if Y >= 1:
# Fix Y >= 1 to whitepoint. Mainly for HDR with PQ clipping,
# where input gray < 1 can result in >= white Y
X, Y, Z = XYZwp
elif single_curve:
X, Y, Z = [v * Y for v in XYZwp]
RGB = bwd_mtx * (X, Y, Z)
for i, channel in enumerate("rgb"):
v = RGB[i]
v = min(max(v, 0), 1)
if slope_limit:
v = max(v, n / 64.25)
if numentries < final:
powinterp[channel].xp.append(n)
powinterp[channel].fp.append(v)
else:
curves[i].append(v)
if numentries < final:
for n in range(numentries):
for i, channel in enumerate("rgb"):
v = powinterp[channel](n / maxval)
curves[i].append(v)
for curve in curves:
# Ensure monotonically increasing
curve[:] = colormath.make_monotonically_increasing(curve)
if numentries < final:
# Interpolate to final resolution
# Spline interpolation to larger size
x = (i / (final - 1.0) * (len(curve) - 1) for i in range(final))
spline = CRInterpolation(curve)
curve[:] = (min(max(spline(v), 0), 1) for v in x)
# Ensure still monotonically increasing
curve[:] = colormath.make_monotonically_increasing(curve)
if optimize:
curves = _create_optimized_shaper_curves(
bwd_mtx, bpc, single_curve, curves, profile, options_dispcal, XYZbp, logfn
)
return curves
def _create_optimized_shaper_curves(
bwd_mtx, bpc, single_curve, curves, profile, options_dispcal, XYZbp=None, logfn=None
):
# Get black and white luminance
if isinstance(profile.tags.get("lumi"), XYZType):
white_cdm2 = profile.tags.lumi.Y
else:
white_cdm2 = 100.0
black_Y = XYZbp and XYZbp[1] or 0
black_cdm2 = black_Y * white_cdm2
# Calibration gamma defaults
gamma_type = None
calgamma = 0
outoffset = None
# Get actual calibration gamma (if any)
calgarg = get_arg("g", options_dispcal)
if not calgarg:
calgarg = get_arg("G", options_dispcal)
if (
calgarg
and isinstance(profile.tags.get("vcgt"), VideoCardGammaType)
and not profile.tags.vcgt.is_linear()
):
calgamma = {"l": -3.0, "s": -2.4, "709": -709, "240": -240}.get(
calgarg[1][1:], calgamma
)
if not calgamma:
try:
calgamma = float(calgarg[1][1:])
except ValueError:
# Not a gamma value
pass
if calgamma:
gamma_type = calgarg[1][0]
outoffset = defaults["calibration.black_output_offset"]
calfarg = get_arg("f", options_dispcal)
if calfarg:
try:
outoffset = float(calfarg[1][1:])
except ValueError:
pass
caltrc = CurveType(profile=profile)
if calgamma > 0:
caltrc.set_bt1886_trc(black_Y, outoffset, calgamma, gamma_type)
else:
caltrc.set_trc(calgamma)
caltf = caltrc.get_transfer_function(True, (0, 1), black_Y, outoffset)
logfn and logfn(f"Black relative luminance = {round(black_Y, 6):.6f}")
if outoffset is not None:
logfn and logfn(f"Black output offset = {round(outoffset, 2):.2f}")
if calgamma > 0:
logfn and logfn(
"Calibration gamma = {:.2f} {}".format(
round(calgamma, 2), {"g": "relative", "G": "absolute"}.get(gamma_type)
)
)
if calgamma:
logfn and logfn(
"Calibration overall transfer function ≈ {} (Δ {:.2f}%)".format(
caltf[0][0], 100 - caltf[1] * 100
)
)
if calgamma > 0 and black_Y:
# Calculate effective gamma
midpoint = colormath.interp(
(len(caltrc) - 1) / 2.0, list(range(len(caltrc))), caltrc
)
gamma = colormath.get_gamma([(0.5, midpoint / 65535.0)])
logfn and logfn(f"Calibration effective gamma = {gamma:.2f}")
tfs = []
for i, channel in enumerate("rgb"):
trc = CurveType(profile=profile)
trc[:] = [v / float(curves[i][-1]) * 65535 for v in curves[i]]
# Get transfer function and see if we have a good match
# to a standard. If we do, use the standard transfer
# function instead of our measurement based one.
# This avoids artifacts when processing is done in
# limited (e.g. 8 bit) precision by a color managed
# application and the source profile uses the same
# standard transfer function.
tf = trc.get_transfer_function(True, (0, 1), black_Y, outoffset)
label = ["Transfer function", channel.upper()]
label.append("≈ {} (Δ {:.2f}%)".format(tf[0][0], 100 - tf[1] * 100))
logfn and logfn(" ".join(label))
gamma = tf[0][1]
if gamma > 0 and black_Y:
# Calculate effective gamma
gamma = colormath.get_gamma([(0.5, 0.5**gamma)], vmin=-black_Y)
logfn and logfn("Effective gamma = {:.2f}".format(round(gamma, 2)))
# Only use standard transfer function if we got a good match
if tf[1] >= 0.98:
# Good match
logfn and logfn("Got good match (+-2%)")
if (
single_curve
and calgamma
and round(tf[0][1], 1) == round(caltf[0][1], 1)
):
# Use calibration gamma
tf = caltf
logfn and logfn("Using calibration transfer function")
tfs.append((tf, trc))
if len(tfs) == 3:
# Only use standard transfer function if we got a good
# identical match for all three channels.
optcurves = []
for i, channel in enumerate("rgb"):
tf, trc = tfs[i]
gamma = tf[0][1]
if gamma > 0 and black_Y:
# Calculate effective gamma
egamma = colormath.get_gamma([(0.5, 0.5**gamma)], vmin=-black_Y)
else:
egamma = gamma
if outoffset is None:
outoffset = tf[0][2]
if (
gamma > 0
and bpc
and (outoffset == 1.0 or not black_Y)
and bwd_mtx * [1, 1, 1] != [1, 1, 1]
):
# Single gamma value, BPC, all output offset or
# zero black luminance
if gamma_type == "g":
gamma = egamma
trc.set_trc(gamma, 1)
else:
# Complex or gamma with offset
if gamma == -1023:
# DICOM is a special case
trc.set_dicom_trc(black_cdm2, white_cdm2)
elif gamma == -1886:
# BT.1886 is a special case
trc.set_bt1886_trc(black_Y)
elif gamma == -2084:
# SMPTE 2084 is a special case
trc.set_smpte2084_trc(black_cdm2, white_cdm2)
elif gamma > 0 and black_Y:
# BT.1886-like or power law with offset
if bpc and gamma_type == "g":
# Use effective gamma needed to
# achieve target effective gamma
# after accounting for BPC
eegamma = colormath.get_gamma(
[(0.5, 0.5**egamma)], vmin=-black_Y
)
else:
eegamma = egamma
trc.set_bt1886_trc(black_Y, outoffset, eegamma, "g")
else:
# L*, sRGB, Rec. 709, SMPTE 240M, or
# power law without offset
if bpc and gamma_type == "g":
# Use effective gamma
gamma = egamma
trc.set_trc(gamma)
trc.apply_bpc()
tf = trc.get_transfer_function(True, (0, 1), black_Y, outoffset)
logfn and logfn(
"Using transfer function for {}: {}".format(channel.upper(), tf[0][0])
)
gamma = tf[0][1]
if gamma > 0 and black_Y:
# Calculate effective gamma
gamma = colormath.get_gamma([(0.5, 0.5**gamma)], vmin=-black_Y)
logfn and logfn("Effective gamma = {:.2f}".format(round(gamma, 2)))
optcurves.append([v / 65535.0 * curves[i][-1] for v in trc])
curves = optcurves
return curves
def _applycal_bug_workaround(profile):
# Argyll applycal can't deal with single gamma TRC tags
# or TRC tags with less than 256 entries
for channel in "rgb":
trc_tag = profile.tags.get(channel + "TRC")
if isinstance(trc_tag, CurveType) and len(trc_tag) < 256:
num_entries = len(trc_tag)
if num_entries <= 1:
# Single gamma
if num_entries:
gamma = trc_tag[0]
else:
gamma = 1.0
trc_tag.set_trc(gamma, 256)
else:
# Interpolate to 256 entries
entry_max = num_entries - 1.0
interp = colormath.Interp(
[i / entry_max for i in range(num_entries)], trc_tag[:]
)
trc_tag[:] = [interp(i / 255.0) for i in range(256)]
def get_current_profile_path(
include_display_profile=True, save_profile_if_no_path=False
):
profile = None
profile_path = getcfg("calibration.file", False)
if profile_path:
filename, ext = os.path.splitext(profile_path)
if ext.lower() in (".icc", ".icm"):
try:
profile = ICCProfile(profile_path)
except Exception as exception:
print("ICCProfile({}):".format(profile_path), exception)
elif include_display_profile:
profile = config.get_display_profile()
if profile and not profile.fileName and save_profile_if_no_path:
if profile.ID == "\0" * 16:
profile.calculateID()
profile_cache_path = os.path.join(cache, "icc")
if check_create_dir(profile_cache_path) is True:
profile.fileName = os.path.join(
profile_cache_path,
"id=" + hexlify(profile.ID).decode() + profile_ext,
)
if not os.path.isfile(profile.fileName):
profile.write()
if profile:
return profile.fileName
def parse_argument_string(args):
"""Parses an argument string and returns a list of arguments."""
return [
re.sub(r'^["\']|["\']$', "", arg)
for arg in re.findall(
r'(?:^|\s+)(-[^\s"\']+|"[^"]+?"|\'[^\']+?\'|[^\s"\']+)', args
)
]
def get_cfg_option_from_args(option_name, argmatch, args, whole=False):
"""Parse args and return option (if found), otherwise return default."""
option = defaults[option_name]
iarg = get_arg(argmatch, args, whole)
if iarg:
if len(iarg[1]) == len(argmatch):
# Option value is the next argument
if len(args) > iarg[0] + 1:
option = args[iarg[0] + 1]
else:
# Option value follows argument directly
option = iarg[1][len(argmatch) :]
return option
def get_options_from_args(dispcal_args=None, colprof_args=None) -> ([str], [str]):
"""Extract options used for dispcal and colprof from argument strings."""
re_options_dispcal = [
r"[moupHVFE]",
r"d(?:\d+(?:,\d+)?|madvr|web)",
r"[cv]\d+",
r"q(?:{})".format("|".join(config.valid_values["calibration.quality"])),
r"y(?:{})".format(
"|".join([_f for _f in config.valid_values["measurement_mode"] if _f])
),
r"[tT](?:\d+(?:\.\d+)?)?",
r"w\d+(?:\.\d+)?,\d+(?:\.\d+)?",
r"[bfakAB]\d+(?:\.\d+)?",
r"(?:g(?:240|709|l|s)|[gG]\d+(?:\.\d+)?)",
r"[pP]\d+(?:\.\d+)?,\d+(?:\.\d+)?,\d+(?:\.\d+)?",
r'X(?:\s*\d+|\s+["\'][^"\']+?["\'])', # Argyll >= 1.3.0 colorimeter correction matrix / Argyll >= 1.3.4 calibration spectral sample
r"I[bw]{,2}", # Argyll >= 1.3.0 drift compensation
r"YA", # Argyll >= 1.5.0 disable adaptive mode
r"Q\w+",
]
re_options_colprof = [
r"q[lmh]",
r"b[lmh]", # B2A quality
r"a(?:{})".format("|".join(config.valid_values["profile.type"])),
r'[sSMA]\s+["\'][^"\']+?["\']',
r"[cd](?:{})(?=\W|$)".format("|".join(viewconds)),
r"[tT](?:{})(?=\W|$)".format("|".join(intents)),
]
options_dispcal = []
options_colprof = []
if dispcal_args:
if isinstance(dispcal_args, bytes):
dispcal_args = dispcal_args.decode("UTF-8")
options_dispcal = re.findall(
r" -({})".format("|".join(re_options_dispcal)), f" {dispcal_args}"
)
if colprof_args:
if isinstance(colprof_args, bytes):
colprof_args = colprof_args.decode("UTF-8")
options_colprof = re.findall(
r" -(" + "|".join(re_options_colprof) + ")", " " + colprof_args
)
return options_dispcal, options_colprof
def get_options_from_cprt(cprt):
"""Extract options used for dispcal and colprof from profile copyright."""
if not isinstance(cprt, str):
if isinstance(cprt, (TextDescriptionType, MultiLocalizedUnicodeType)):
cprt = str(cprt)
else:
cprt = str(cprt, fs_enc, "replace")
dispcal_args = cprt.split(" dispcal ")
colprof_args = None
if len(dispcal_args) > 1:
dispcal_args[1] = dispcal_args[1].split(" colprof ")
if len(dispcal_args[1]) > 1:
colprof_args = dispcal_args[1][1]
dispcal_args = dispcal_args[1][0]
else:
dispcal_args = None
colprof_args = cprt.split(" colprof ")
if len(colprof_args) > 1:
colprof_args = colprof_args[1]
else:
colprof_args = None
return dispcal_args, colprof_args
def get_options_from_cal(cal) -> ([str], [str]):
if not isinstance(cal, CGATS):
cal = CGATS(cal)
if 0 in cal:
cal = cal[0]
if not cal or "ARGYLL_DISPCAL_ARGS" not in cal or not cal.ARGYLL_DISPCAL_ARGS:
return [], []
dispcal_args = cal.ARGYLL_DISPCAL_ARGS[0]
return get_options_from_args(dispcal_args)
def get_options_from_profile(profile):
"""Try and get options from profile. First, try the 'targ' tag and
look for the special DisplayCAL sections 'ARGYLL_DISPCAL_ARGS' and
'ARGYLL_COLPROF_ARGS'. If either does not exist, fall back to the
copyright tag (DisplayCAL < 0.4.0.2)"""
if not isinstance(profile, ICCProfile):
profile = ICCProfile(profile)
dispcal_args = None
colprof_args = None
if "targ" in profile.tags:
ti3 = CGATS(profile.tags.targ)
if 1 in ti3 and "ARGYLL_DISPCAL_ARGS" in ti3[1] and ti3[1].ARGYLL_DISPCAL_ARGS:
dispcal_args = ti3[1].ARGYLL_DISPCAL_ARGS[0]
if 0 in ti3 and "ARGYLL_COLPROF_ARGS" in ti3[0] and ti3[0].ARGYLL_COLPROF_ARGS:
colprof_args = ti3[0].ARGYLL_COLPROF_ARGS[0]
if not dispcal_args and "cprt" in profile.tags:
dispcal_args = get_options_from_cprt(profile.getCopyright())[0]
if not colprof_args and "cprt" in profile.tags:
colprof_args = get_options_from_cprt(profile.getCopyright())[1]
return get_options_from_args(dispcal_args, colprof_args)
def get_options_from_ti3(ti3):
"""Try and get options from TI3 file by looking for the special
DisplayCAL sections 'ARGYLL_DISPCAL_ARGS' and 'ARGYLL_COLPROF_ARGS'.
"""
if not isinstance(ti3, CGATS):
ti3 = CGATS(ti3)
dispcal_args = None
colprof_args = None
if 1 in ti3 and "ARGYLL_DISPCAL_ARGS" in ti3[1] and ti3[1].ARGYLL_DISPCAL_ARGS:
dispcal_args = ti3[1].ARGYLL_DISPCAL_ARGS[0]
if 0 in ti3 and "ARGYLL_COLPROF_ARGS" in ti3[0] and ti3[0].ARGYLL_COLPROF_ARGS:
colprof_args = ti3[0].ARGYLL_COLPROF_ARGS[0]
return get_options_from_args(dispcal_args, colprof_args)
def get_pattern_geometry():
"""Return pattern geometry for pattern generator"""
x, y, size = [float(v) for v in getcfg("dimensions.measureframe").split(",")]
if os.getenv("XDG_SESSION_TYPE") == "wayland":
# No way to get coordinates under Wayland, default to center
x = y = 0.5
size = size * defaults["size.measureframe"]
match = re.search(r"@ -?\d+, -?\d+, (\d+)x(\d+)", getcfg("displays", raw=True))
if match:
display_size = [int(item) for item in match.groups()]
else:
display_size = 1920, 1080
w, h = [min(size / v, 1.0) for v in display_size]
if config.get_display_name(None, True) == "Prisma":
w = h
x = (display_size[0] - w * display_size[0]) * x / display_size[0]
y = (display_size[1] - h * display_size[1]) * y / display_size[1]
x, y, w, h = [max(v, 0) for v in (x, y, w, h)]
size = w * h
return x, y, w, h, size
def get_python_and_pythonpath():
"""Return (system) python and pythonpath"""
# Determine the path of python, and python module search paths
# If we are running 'frozen', expect python.exe in the same directory
# as the packed executable.
# py2exe: The python executable can be included via setup script by
# adding it to 'data_files'
pythonpath = list(sys.path)
dirname = os.path.dirname(sys.executable)
if sys.platform == "win32":
if getattr(sys, "frozen", False):
pythonpath = [dirname]
# If we are running 'frozen', add library.zip and lib\library.zip
# to sys.path
# py2exe: Needs appropriate 'zipfile' option in setup script and
# 'bundle_files' 3
pythonpath.append(os.path.join(dirname, "library.zip"))
pythonpath.append(os.path.join(dirname, "library.zip", appname))
if os.path.isdir(os.path.join(dirname, "lib")):
dirname = os.path.join(dirname, "lib")
pythonpath.append(os.path.join(dirname, "library.zip"))
pythonpath.append(os.path.join(dirname, "library.zip", appname))
python = os.path.join(dirname, "python.exe")
else:
# Linux / Mac OS X
if isapp:
python = os.path.join(dirname, "python")
else:
paths = os.defpath.split(os.pathsep)
python = (
which("python3.7", paths)
or which("python3.6", paths)
or "/usr/bin/env python"
)
return (python, pythonpath)
def get_arg(argmatch, args, whole=False):
"""Return first found entry beginning with the argmatch string or None"""
for i, arg in enumerate(args):
if (whole and arg == argmatch) or (not whole and arg.startswith(argmatch)):
return i, arg
def get_default_headers():
"""Get default headers for HTTP request"""
if sys.platform == "darwin":
# Python's platform.platform output is useless under Mac OS X
# (e.g. 'Darwin-15.0.0-x86_64-i386-64bit' for Mac OS X 10.11 El Capitan)
oscpu = "Mac OS X {}; {}".format(mac_ver()[0], mac_ver()[-1])
elif sys.platform == "win32":
machine = platform.machine()
oscpu = "{}; {}".format(
safe_str(" ".join([v for v in win_ver() if v]), "ASCII", "asciize"),
{"AMD64": "x86_64"}.get(machine, machine),
)
else:
# Linux
oscpu = "{}; {}".format(
" ".join([distro.id(), distro.version(), distro.codename()]),
platform.machine(),
)
return {
"User-Agent": f"{appname}/{version} ({oscpu})",
"Accept-Language": f"{lang.getcode()},*;q=0.5",
}
def http_request(
parent=None,
domain=None,
request_type="GET",
path="",
params=None,
files=None,
headers=None,
charset="UTF-8",
failure_msg="",
silent=False,
):
"""HTTP request wrapper"""
if params is None:
params = {}
if files:
content_type, params = encode_multipart_formdata(iter(params.items()), files)
else:
for key in params:
params[key] = safe_str(params[key], charset)
params = urllib.parse.urlencode(params)
if headers is None:
headers = get_default_headers()
if request_type == "GET":
path += "?" + params
params = None
else:
if files:
headers.update(
{"Content-Type": content_type, "Content-Length": str(len(params))}
)
else:
headers.update(
{
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "text/plain",
}
)
conn = http.client.HTTPConnection(domain)
try:
conn.request(request_type, path, params, headers)
resp = conn.getresponse()
except (socket.error, http.client.HTTPException) as exception:
msg = " ".join(
[
failure_msg,
lang.getstr(
"connection.fail", " ".join([str(arg) for arg in exception.args])
),
]
).strip()
print(msg)
if not silent:
wx.CallAfter(
InfoDialog,
parent,
msg=msg,
ok=lang.getstr("ok"),
bitmap=geticon(32, "dialog-error"),
log=False,
)
return False
if resp.status >= 400:
uri = "http://" + domain + path
msg = (
" ".join(
[
failure_msg,
lang.getstr(
"connection.fail.http",
" ".join([str(resp.status), resp.reason]),
),
]
).strip()
+ "\n"
+ uri
)
print(msg)
html = universal_newlines(resp.read().strip())
html = re.sub(
re.compile(r"<script.*?</script>", re.I | re.S),
"<!-- SCRIPT removed -->",
html,
)
html = re.sub(
re.compile(r"<style.*?</style>", re.I | re.S),
"<!-- STYLE removed -->",
html,
)
print(html)
if not silent:
wx.CallAfter(
HtmlInfoDialog,
parent,
msg=msg,
html=html,
ok=lang.getstr("ok"),
bitmap=geticon(32, "dialog-error"),
log=False,
)
return False
return resp
def insert_ti_patches_omitting_RGB_duplicates(cgats1, cgats2_path, logfn=print):
"""Insert patches from first TI file after first patch of second TI,
ignoring RGB duplicates. Return second TI as CGATS instance.
"""
cgats2 = CGATS(cgats2_path)
cgats1_data = cgats1.queryv1("DATA")
data = cgats2.queryv1("DATA")
data_format = cgats2.queryv1("DATA_FORMAT")
# Get only RGB data
data.parent.DATA_FORMAT = CGATS()
data.parent.DATA_FORMAT.key = "DATA_FORMAT"
data.parent.DATA_FORMAT.parent = data
data.parent.DATA_FORMAT.root = data.root
data.parent.DATA_FORMAT.type = b"DATA_FORMAT"
for i, label in enumerate(("RGB_R", "RGB_G", "RGB_B")):
data.parent.DATA_FORMAT[i] = label
cgats1_data.parent.DATA_FORMAT = data.parent.DATA_FORMAT
rgbdata = str(data)
# Restore DATA_FORMAT
data.parent.DATA_FORMAT = data_format
# Collect all preconditioning point datasets not in data
cgats1_data.vmaxlen = data.vmaxlen
cgats1_datasets = []
for i in cgats1_data:
dataset = cgats1_data[i]
if str(dataset) not in rgbdata:
# Not a duplicate
cgats1_datasets.append(dataset)
if cgats1_datasets:
# Insert preconditioned point datasets after first patch
if logfn:
logfn(
"{}: Adding {:d} fixed points to {}".format(
appname, len(cgats1_datasets), cgats2_path
)
)
data.moveby1(1, len(cgats1_datasets))
for i, dataset in enumerate(cgats1_datasets):
dataset.key = i + 1
dataset.parent = data
dataset.root = data.root
data[dataset.key] = dataset
return cgats2
class EvalFalse:
"""Evaluate to False in boolean comparisons."""
def __init__(self, wrapped_object):
self._wrapped_object = wrapped_object
def __getattribute__(self, name):
return getattr(object.__getattribute__(self, "_wrapped_object"), name)
def __bool__(self):
return False
class DummyDialog:
def __init__(self, *args, **kwargs):
self.is_shown_on_screen = True
def Close(self, force=False):
pass
def Destroy(self):
pass
def EndModal(self, id=-1):
return id
def Hide(self):
pass
def IsShownOnScreen(self):
return self.is_shown_on_screen
def Show(self, show=True):
self.is_shown_on_screen = show
def ShowModal(self):
pass
class FilteredStream:
"""Wrap a stream and filter all lines written to it."""
# Discard the whole line if it is empty after replacing patterns
discard = ""
# If one of the triggers is contained in a line, skip the whole line
triggers = [
"Place instrument on test window",
"key to continue",
"key to retry",
"key to take a reading",
"] to read",
"' to set",
"' to report",
"' to toggle",
" or Q to ",
"place on the white calibration reference",
"or place on the calibration reference",
"read failed due to the sensor being in the wrong position",
"Ambient filter should be removed",
"The instrument can be removed from the screen",
] + INST_CAL_MSGS
substitutions = {
r"\^\[": "", # ESC key on Linux/OSX
"patch ": "Patch ",
re.compile(r"Point \d+", re.I): "",
}
# Strip these patterns from input before writing. Note that this only works
# on full lines (ending with linesep_in)
prestrip = ""
def __init__(
self,
stream,
data_encoding=None,
file_encoding=None,
errors="replace",
discard=None,
linesep_in="\r\n",
linesep_out="\n",
substitutions=None,
triggers=None,
prestrip=None,
):
self.stream = stream
self.data_encoding = data_encoding
self.file_encoding = file_encoding
self.errors = errors
if discard is not None:
self.discard = discard
self.linesep_in = linesep_in
self.linesep_out = linesep_out
if substitutions is not None:
self.substitutions = substitutions
if triggers is not None:
self.triggers = triggers
if prestrip is not None:
self.prestrip = prestrip
self._buffer = ""
def __getattr__(self, name):
return getattr(self.stream, name)
def write(self, data):
"""Write data to stream, stripping all unwanted output.
Incoming lines are expected to be delimited by linesep_in.
"""
if not data:
return
if isinstance(data, bytes):
data = data.decode()
if self.prestrip and (re.search(self.prestrip, data) or self._buffer):
if not data.endswith(self.linesep_in):
# Buffer all data until we see a line ending
self._buffer += data
return
elif self._buffer:
# Assemble the full line from the buffer
data = self._buffer + data
self._buffer = ""
data = re.sub(self.prestrip, "", data)
lines = []
for line in data.split(self.linesep_in):
if line and not re.sub(self.discard, "", line):
line = ""
write = True
for trigger in self.triggers:
if trigger.lower() in line.lower():
write = False
break
if write:
if self.data_encoding and not isinstance(line, str):
line = line.decode(self.data_encoding, self.errors)
for search in self.substitutions:
sub = self.substitutions[search]
line = re.sub(search, sub, line)
if self.file_encoding:
line = line.encode(self.file_encoding, self.errors)
lines.append(line)
if lines:
self.stream.write(self.linesep_out.join(lines))
class Producer:
"""Generic producer."""
def __init__(self, worker, producer, continue_next=False):
self.worker = worker
self.producer = producer
self.continue_next = continue_next
def __call__(self, *args, **kwargs):
try:
result = self.producer(*args, **kwargs)
except Exception as exception:
if debug:
messages = traceback.format_exception(exception)
print(
"[D] Worker raised an unhandled exception: \n" + "\n".join(messages)
)
raise
if not self.continue_next and self.worker._progress_wnd:
if hasattr(
self.worker.progress_wnd, "animbmp"
) and self.worker.progress_wnd.progress_type in (0, 2):
# Allow time for animation fadeout
wx.CallAfter(self.worker.progress_wnd.stop_timer, False)
if self.worker.progress_wnd.progress_type == 0:
sleep(4)
else:
sleep(1)
return result
class StringWithLengthOverride(UserString):
"""Allow defined behavior in comparisons and when evaluating length."""
def __init__(self, seq, length=None):
UserString.__init__(self, seq)
if length is None:
length = len(seq)
self.length = length
def __len__(self):
return self.length
class Sudo:
"""Determine if a command can be run via sudo"""
def __init__(self):
self.availoptions = {}
self.sudo = which("sudo")
if self.sudo:
# Determine available sudo options
man = which("man")
if man:
manproc = sp.Popen([man, "sudo"], stdout=sp.PIPE, stderr=sp.PIPE)
# Strip formatting
stdout = re.sub(b".\x08", b"", manproc.communicate()[0])
self.availoptions = {
"E": bool(re.search(rb"-E\W", stdout)),
"l [command]": bool(re.search(rb"-l\W(?:.*?\W)?command\W", stdout)),
"K": bool(re.search(rb"-K\W", stdout)),
"k": bool(re.search(rb"-k\W", stdout)),
}
if debug:
print(
"[D] Available sudo options:",
", ".join(
[
option
for option in list(self.availoptions.keys())
if self.availoptions[option]
]
),
)
def __len__(self):
return int(bool(self.sudo))
def __str__(self):
return str(self.sudo or "")
def _expect_timeout(self, patterns, timeout=-1, child_timeout=1):
"""wexpect.spawn.expect with better timeout handling.
The default expect can block up to timeout seconds if the child is already dead.
To prevent this, we run expect in a loop until a pattern is matched, timeout is
reached or an exception occurs. The max time an expect call will block if the
child is already dead can be set with the child_timeout parameter.
"""
if timeout == -1:
timeout = self.subprocess.timeout
patterns = list(patterns)
if wexpect.TIMEOUT not in patterns:
patterns.append(wexpect.TIMEOUT)
start = time()
while True:
result = self.subprocess.expect(patterns, timeout=child_timeout)
if (
self.subprocess.after is not wexpect.TIMEOUT
or time() - start >= timeout
):
break
return result
def _terminate(self):
"""Terminate running sudo subprocess."""
self.subprocess.sendcontrol("C")
self._expect_timeout([wexpect.EOF], 10)
if self.subprocess.after is wexpect.TIMEOUT:
print("Warning: sudo timed out")
if not self.subprocess.terminate(force=True):
print("Warning: Couldn't terminate timed-out sudo subprocess")
else:
subprocess_before = self.subprocess.before.strip()
if isinstance(subprocess_before, bytes):
subprocess_before = subprocess_before.decode(enc, "replace")
print(subprocess_before)
def authenticate(self, args, title, parent=None):
"""Authenticate for a given command
The return value will be a tuple (auth_successful, password).
auth_successful will be a custom class that will always have length 0 if
authentication was not successful or the command is not allowed (even
if the actual string length is non-zero), thus allowing for easy
boolean comparisons.
"""
# Authentication using sudo is pretty convoluted if dealing with
# platform and configuration differences. Ask for a password by first
# clearing any cached credentials (sudo -K) so that sudo is guaranteed
# to ask for a password if a command is run through it, then we spawn
# sudo true (with true being the standard GNU utility that always
# has an exit status of 0) and expect the password prompt. The user
# is then given the opportunity to enter a password, which is then fed
# to sudo. If sudo exits with a status of 0, the password must have
# been accepted, but we still don't know for sure if our command is
# allowed, so we run sudo -l <command> to determine if it is
# indeed allowed.
pwd = ""
dlg = ConfirmDialog(
parent,
title=title,
msg=lang.getstr("dialog.enter_password"),
ok=lang.getstr("ok"),
cancel=lang.getstr("cancel"),
bitmap=geticon(32, "lock"),
)
dlg.pwd_txt_ctrl = wx.TextCtrl(
dlg, -1, pwd, size=(320, -1), style=wx.TE_PASSWORD | wx.TE_PROCESS_ENTER
)
dlg.pwd_txt_ctrl.Bind(wx.EVT_TEXT_ENTER, lambda event: dlg.EndModal(wx.ID_OK))
dlg.sizer3.Add(
dlg.pwd_txt_ctrl,
1,
# flag=wx.TOP | wx.ALIGN_LEFT, border=12)
flag=wx.TOP,
border=12,
)
dlg.ok.SetDefault()
dlg.sizer0.SetSizeHints(dlg)
dlg.sizer0.Layout()
# Remove cached credentials
self.kill()
sudo_args = ["-p", "Password:", "true"]
try:
p = self.subprocess = wexpect.spawn(safe_str(self.sudo), sudo_args)
except Exception as exception:
return (
StringWithLengthOverride(
"Could not run {} {}: {}".format(
self.sudo, " ".join(sudo_args), exception
),
0,
),
pwd,
)
self._expect_timeout(["Password:", wexpect.EOF], 10)
# We need to call isalive() to set the exitstatus
while p.isalive() and p.after == "Password:":
# Ask for password
dlg.pwd_txt_ctrl.SetFocus()
result = dlg.ShowModal()
pwd = dlg.pwd_txt_ctrl.GetValue()
if result != wx.ID_OK:
self._terminate()
return False, pwd
p.send(pwd + os.linesep)
self._expect_timeout(["Password:", wexpect.EOF], 10)
if p.after == "Password:":
msg = lang.getstr("dialog.enter_password")
errstr = p.before.strip()
if errstr:
print(errstr)
msg = "\n\n".join([errstr, msg])
dlg.message.SetLabel(msg)
dlg.message.Wrap(dlg.GetSize()[0] - 32 - 12 * 2)
dlg.pwd_txt_ctrl.SetValue("")
dlg.sizer0.SetSizeHints(dlg)
dlg.sizer0.Layout()
dlg.Destroy()
if p.after is wexpect.TIMEOUT:
print("Error: sudo timed out")
if not p.terminate(force=True):
print("Warning: Couldn't terminate timed-out sudo subprocess")
return StringWithLengthOverride("sudo timed out", 0), pwd
if p.exitstatus != 0:
return (
StringWithLengthOverride(
p.before.strip().decode(enc, "replace")
or (f"sudo exited prematurely with status {p.exitstatus}"),
0,
),
pwd,
)
# Password was accepted, check if command is allowed
return self.is_allowed(args, pwd), pwd
def is_allowed(self, args=None, pwd=""):
"""Check if a command is allowed via sudo.
Return either a string listing allowed and forbidden commands, or the
fully-qualified path of the command along with any arguments, or an error
message in case the command is not allowed, or False if the password was not
accepted.
The returned error is a custom class that will always have length 0
if the command is not allowed (even if the actual string length is
non-zero), thus allowing for easy boolean comparisons.
"""
sudo_args = ["-p", "Password:", "-l"]
# Set sudo args based on available options
if self.availoptions.get("l [command]") and args:
sudo_args += args
try:
p = self.subprocess = wexpect.spawn(safe_str(self.sudo), sudo_args)
except Exception as exception:
return StringWithLengthOverride(
"Could not run {} {}: {}".format(
self.sudo, " ".join(sudo_args), exception
),
0,
)
self._expect_timeout(["Password:", wexpect.EOF], 10)
# We need to call isalive() to set the exitstatus
while p.isalive() and p.after == "Password:":
p.send(pwd + os.linesep)
self._expect_timeout(["Password:", wexpect.EOF], 10)
if p.after == "Password:":
# Password was not accepted
self._terminate()
return StringWithLengthOverride(p.before.strip(), 0)
if p.after is wexpect.TIMEOUT:
print("Error: sudo timed out")
if not p.terminate(force=True):
print("Warning: Couldn't terminate timed-out sudo subprocess")
return StringWithLengthOverride("sudo timed out", 0)
if p.exitstatus != 0:
return StringWithLengthOverride(
p.before.strip()
or (f"sudo exited prematurely with status {p.exitstatus}"),
0,
)
return p.before.strip()
def kill(self):
"""Remove cached credentials"""
kill_arg = None
if self.availoptions.get("K"):
kill_arg = "-K"
elif self.availoptions.get("k"):
kill_arg = "-k"
if kill_arg:
sp.call([safe_str(self.sudo), kill_arg])
class WPopen(sp.Popen):
def __init__(self, *args, **kwargs):
sp.Popen.__init__(self, *args, **kwargs)
self._seekpos = 0
self._stdout = kwargs["stdout"]
self.after = None
self.before = None
self.exitstatus = None
self.logfile_read = None
self.match = None
self.maxlen = 80
self.timeout = 30
def isalive(self):
self.exitstatus = self.poll()
return self.exitstatus is None
def expect(self, patterns, timeout=-1):
if not isinstance(patterns, list):
patterns = [patterns]
if timeout == -1:
timeout = self.timeout
if timeout is not None:
end = time() + timeout
while timeout is None or time() < end:
self._stdout.seek(self._seekpos)
buf = self._stdout.read()
self._seekpos += len(buf)
if not buf and not self.isalive():
self.match = wexpect.EOF(
"End Of File (EOF) in expect() - dead child process"
)
if wexpect.EOF in patterns:
return self.match
raise self.match
if buf and self.logfile_read:
self.logfile_read.write(buf)
for pattern in patterns:
if isinstance(pattern, str) and pattern in buf:
offset = buf.find(pattern)
self.after = buf[offset:]
self.before = buf[:offset]
self.match = buf[offset : offset + len(pattern)]
return self.match
sleep(0.01)
if timeout is not None:
self.match = wexpect.TIMEOUT("Timeout exceeded in expect()")
if wexpect.TIMEOUT in patterns:
return self.match
raise self.match
def send(self, s):
self.stdin.write(s)
self._stdout.seek(self._seekpos)
buf = self._stdout.read()
self._seekpos += len(buf)
if buf and self.logfile_read:
self.logfile_read.write(buf)
def terminate(self, force=False):
sp.Popen.terminate(self)
class Worker(WorkerBase):
def __init__(self, owner=None):
"""Create and return a new worker instance."""
WorkerBase.__init__(self)
self.argyll_bin_dir = None
self.argyll_version = [0, 0, 0]
self.argyll_version_string = "0.0.0"
self._displays = []
self.display_edid = []
self.display_manufacturers = []
self.display_names = []
self.display_rects = []
self.displays = []
self.instruments = []
self.lut_access = []
self.paused = False
self._patterngenerator_wait = False
self.send_buffer = None
self.partial_data = ""
self.owner = owner # owner should be a wxFrame or similar
if sys.platform == "win32":
self.pty_encoding = f"cp{windll.kernel32.GetACP():d}"
else:
self.pty_encoding = enc
self.cmdrun = False
self.dispcal_create_fast_matrix_shaper = False
self.dispread_after_dispcal = False
self.finished = True
self.instrument_on_screen = False
self.interactive = False
self.spotread_just_do_instrument_calibration = False
self.lastcmdname = None
# Filter out warnings from OS components (e.g. shared libraries)
# E.g.:
# Nov 26 16:28:16 dispcal[1006] <Warning>: void CGSUpdateManager::log() const: conn 0x1ec57 token 0x3ffffffffffd0a
prestrip = re.compile(
r"\D+\s+\d+\s+\d+:\d+:\d+\s+\w+\[\d+]\s+<Warning>:[\S\s]*"
)
discard = [r"[\*\.]+|Current (?:RGB|XYZ)(?: +.*)?"]
self.lastmsg_discard = re.compile(r"|".join(discard))
self._init_sounds(dummy=True)
self.options_colprof = []
self.options_dispcal = []
self.options_dispread = []
self.options_targen = []
self.pauseable = False
discard = [
r"^Display type is .+",
r"^Doing (?:some initial|check) measurements",
r"^Adjust .+? Press space when done\.\s*",
r"^\s*(?:[/\\]\s+)?(?:Adjusted )?(Current",
r"Initial",
r"[Tt]arget) (?:Br(?:ightness)?",
r"50% Level",
r"white",
r"(?:Near )?[Bb]lack",
r"(?:advertised )?gamma",
r"RGB",
r"\d(?:\.\d+)?).*",
r"^Gamma curve .+",
r"^Display adjustment menu:",
r"^Press",
r"^\d\).+",
r"^(?:1%|Black|Red|Green|Blue|White|Grey)\s+=.+",
r"^\s*patch \d+ of \d+.*",
r"^\s*point \d+.*",
r"^\s*Added \d+/\d+",
# These need to be last because they're very generic!
r"[\*\.]+",
r"\s*\d*%?",
]
self.recent_discard = re.compile(r"|".join(discard), re.I)
self.resume = False
self.sudo = None
self.auth_timestamp = 0
self.sessionlogfiles = {}
self.triggers = ["Password:"]
self.recent = FilteredStream(
LineCache(maxlines=3),
self.pty_encoding,
discard=self.recent_discard,
triggers=self.triggers + ["stopped at user request"],
prestrip=prestrip,
)
self.lastmsg = FilteredStream(
LineCache(),
self.pty_encoding,
discard=self.lastmsg_discard,
triggers=self.triggers,
prestrip=prestrip,
)
self.clear_argyll_info()
self.set_argyll_version_from_string(getcfg("argyll.version"), False)
self.clear_cmd_output()
self._detecting_video_levels = False
self._detected_output_levels = False
self._patterngenerators = {}
self._progress_dlgs = {}
self._progress_wnd = None
self._pwdstr = ""
workers.append(self)
def _init_sounds(self, dummy=False):
if dummy:
self.measurement_sound = audio.DummySound()
self.commit_sound = audio.DummySound()
else:
# Sounds when measuring
# Needs to be stereo!
self.measurement_sound = audio.Sound(get_data_path("beep.wav"))
self.commit_sound = audio.Sound(get_data_path("camera_shutter.wav"))
def add_measurement_features(
self,
args,
display=True,
ignore_display_name=False,
allow_nondefault_observer=False,
ambient=False,
allow_video_levels=True,
quantize=False,
cmd=None,
):
"""Add common options and to dispcal, dispread and spotread arguments"""
if display and not get_arg("-d", args):
args.append("-d" + self.get_display())
if display and allow_video_levels:
self.add_video_levels_arg(args)
if (
display
and quantize
and not get_arg("-Z", args)
and getcfg("patterngenerator.quantize_bits")
):
# dispread only
args.append("-Z{:d}".format(getcfg("patterngenerator.quantize_bits")))
if not get_arg("-c", args):
args.append("-c{}".format(getcfg("comport.number")))
instrument_name = self.get_instrument_name()
measurement_mode = getcfg("measurement_mode")
if measurement_mode == "auto":
# Make changes in DisplayCAL.MainFrame.set_ccxx_measurement_mode too!
if instrument_name == "ColorHug":
measurement_mode = "R"
elif instrument_name == "ColorHug2":
measurement_mode = "F"
else:
measurement_mode = "l"
instrument_features = self.get_instrument_features()
if (
not ambient
and measurement_mode
and not get_arg("-y", args)
and instrument_name != "specbos 1201"
):
# Always specify -y for colorimeters (won't be read from cal
# when updating)
# The specbos 1201 (unlike 1211) doesn't support measurement
# mode selection
if self.argyll_version >= [1, 5, 0]:
measurement_mode_map = instrument_features.get(
"measurement_mode_map", {}
)
measurement_mode = measurement_mode_map.get(
measurement_mode[0], measurement_mode
)
args.append("-y" + measurement_mode[0])
if (
getcfg("measurement_mode.projector")
and instrument_features.get("projector_mode")
and self.argyll_version >= [1, 1, 0]
and not get_arg("-p", args)
):
# Projector mode, Argyll >= 1.1.0 Beta
args.append("-p")
if instrument_features.get("adaptive_mode"):
if getcfg("measurement_mode.adaptive"):
if (
(
self.argyll_version[0:3] > [1, 1, 0]
or (
self.argyll_version[0:3] == [1, 1, 0]
and "Beta" not in self.argyll_version_string
and "RC1" not in self.argyll_version_string
and "RC2" not in self.argyll_version_string
)
)
and self.argyll_version[0:3] < [1, 5, 0]
and not get_arg("-V", args, True)
):
# Adaptive measurement mode, Argyll >= 1.1.0 RC3
args.append("-V")
else:
if self.argyll_version[0:3] >= [1, 5, 0]:
# Disable adaptive measurement mode
args.append("-YA")
if (
instrument_name in ("Spyder4", "Spyder5")
and self.argyll_version == [1, 7, 0]
and measurement_mode in ("f", "e")
and not get_arg("-YR:", args)
):
# Prevent 'Warning - Spyder: measuring refresh rate failed'
args.append("-YR:60")
non_argyll_prisma = (
config.get_display_name() == "Prisma"
and not defaults["patterngenerator.prisma.argyll"]
)
if display and not (get_arg("-dweb", args) or get_arg("-dmadvr", args)):
if (
(self.argyll_version <= [1, 0, 4] and not get_arg("-p", args))
or (self.argyll_version > [1, 0, 4] and not get_arg("-P", args))
and f"-d{self.argyll_virtual_display}" not in args
):
if (
config.get_display_name() == "Resolve" or non_argyll_prisma
) and not ignore_display_name:
# Move Argyll test window to lower right corner and make it
# very small
dimensions_measureframe = "1,1,0.01"
else:
if os.getenv("XDG_SESSION_TYPE") == "wayland":
# The following is a workaround for Wayland not exposing the
# absolute screen positions of a window.
dimensions_measureframe = "0.5,0.5,1.5"
else:
dimensions_measureframe = getcfg("dimensions.measureframe")
if get_arg("-dcc", args):
# Rescale for Chromecast default patch size of 10%
dimensions_measureframe = config.get_measureframe_dimensions(
dimensions_measureframe, 10
)
args.append(f"-P{dimensions_measureframe}")
farg = get_arg("-F", args, True)
if (
config.get_display_name() == "Resolve" or non_argyll_prisma
) and not ignore_display_name:
if farg:
# Remove -F (darken background) as we relay colors to
# pattern generator
args = args[: farg[0]] + args[farg[0] + 1 :]
elif getcfg("measure.darken_background") and not farg:
args.append("-F")
if (
getcfg("measurement_mode.highres")
and instrument_features.get("highres_mode")
and not get_arg("-H", args, True)
):
args.append("-H")
if (
allow_nondefault_observer
and self.instrument_can_use_nondefault_observer()
and getcfg("observer") != defaults["observer"]
and not get_arg("-Q", args)
):
args.append("-Q" + getcfg("observer"))
if (
not ambient
and self.instrument_can_use_ccxx()
and not is_ccxx_testchart()
and not get_arg("-X", args)
):
# Use colorimeter correction?
ccmx = getcfg("colorimeter_correction_matrix_file").split(":", 1)
if len(ccmx) > 1 and ccmx[1]:
ccmx = ccmx[1]
else:
ccmx = None
if ccmx and (
not ccmx.lower().endswith(".ccss") or self.instrument_supports_ccss()
):
result = check_file_isfile(ccmx)
if isinstance(result, Exception):
return result
try:
cgats = CGATS(ccmx)
except (IOError, CGATSError) as exception:
return exception
else:
ccxx_instrument_from_cgats = cgats.queryv1("INSTRUMENT") or b""
ccxx_instrument = get_canonical_instrument_name(
ccxx_instrument_from_cgats,
{
"DTP94-LCD mode": "DTP94",
"eye-one display": "i1 Display",
"Spyder 2 LCD": "Spyder2",
"Spyder 3": "Spyder3",
},
)
if isinstance(ccxx_instrument, bytes):
ccxx_instrument = ccxx_instrument.decode("utf-8")
if (
ccxx_instrument
and instrument_name.lower().replace(" ", "")
in ccxx_instrument.lower().replace(" ", "")
) or ccmx.lower().endswith(".ccss"):
tempdir = self.create_tempdir()
if isinstance(tempdir, Exception):
return tempdir
ccmxcopy = os.path.join(tempdir, os.path.basename(ccmx))
if not os.path.isfile(ccmxcopy):
if 0 in cgats and cgats[0].type.strip() == b"CCMX":
# Add display base ID if missing
cbid_added = self.check_add_display_type_base_id(cgats)
else:
cbid_added = False
try:
# Copy ccmx to profile dir
if cbid_added:
# Write updated CCMX
cgats.write(ccmxcopy)
else:
# Copy original
shutil.copyfile(ccmx, ccmxcopy)
except Exception as exception:
return Error(
lang.getstr("error.copy_failed", (ccmx, ccmxcopy))
+ "\n\n"
+ str(exception)
)
result = check_file_isfile(ccmxcopy)
if isinstance(result, Exception):
return result
args.append("-X")
args.append(os.path.basename(ccmxcopy))
if (
display
and (
getcfg("drift_compensation.blacklevel")
or getcfg("drift_compensation.whitelevel")
)
and self.argyll_version >= [1, 3, 0]
and not get_arg("-I", args)
):
args.append("-I")
if getcfg("drift_compensation.blacklevel"):
args[-1] += "b"
if getcfg("drift_compensation.whitelevel"):
args[-1] += "w"
# TTBD/FIXME: Skipping of sensor calibration can't be done in
# emissive mode (see Argyll source spectro/ss.c, around line 40)
# (is this still the case though?)
# Note we implicitly skip sensor calibration if already done during
# output levels detection or if using spotread, but only for colorimeters
if (
(
getcfg("allow_skip_sensor_cal")
or (
not instrument_features.get("spectral")
and (
self._detected_output_levels
or (self.dispread_after_dispcal and self.resume)
or cmd == get_argyll_util("spotread")
)
and not self._detecting_video_levels
)
)
and instrument_features.get("skip_sensor_cal")
and self.argyll_version >= [1, 1, 0]
and not get_arg("-N", args, True)
and not self.spotread_just_do_instrument_calibration
):
args.append("-N")
return True
def add_video_levels_arg(self, args):
if (
config.get_display_name() not in ("madVR", "Resolve", "Prisma")
and getcfg("patterngenerator.use_video_levels")
and self.argyll_version >= [1, 6]
and self.get_display() != self.argyll_virtual_display
):
# For madVR and dummy display, -E is invalid
args.append("-E")
def authenticate(self, cmd, title=appname, parent=None):
"""Athenticate (using sudo) for a given command
The return value will either be True (authentication successful and
command allowed), False (in case of the user cancelling the password
dialog), None (Windows or running as root) or an error.
"""
if sys.platform == "win32" or os.geteuid() == 0:
return
self.auth_timestamp = 0
ocmd = cmd
if cmd and not os.path.isabs(cmd):
cmd = get_argyll_util(ocmd)
if not cmd:
cmd = which(ocmd)
if not cmd or not os.path.isfile(cmd):
return Error(lang.getstr("file.missing", ocmd))
_disabler = BetterWindowDisabler()
result = True
if not self.sudo:
self.sudo = Sudo()
if not self.sudo:
result = Error(lang.getstr("file.missing", "sudo"))
if result is True:
pwd = self.pwd
args = [cmd, "-?"]
if not pwd or not self.sudo.is_allowed(args, pwd):
# If no password was previously available, or if the requested
# command cannot be run via sudo regardless of password (we check
# this with sudo -l <command>), we ask for a password.
print(lang.getstr("auth"))
progress_dlg = self._progress_wnd or getattr(
wx.GetApp(), "progress_dlg", None
)
if parent is None:
if progress_dlg and progress_dlg.IsShownOnScreen():
parent = progress_dlg
else:
parent = self.owner
result, pwd = self.sudo.authenticate(args, title, parent)
if result:
self.pwd = pwd
result = True
elif result is False:
print(lang.getstr("aborted"))
else:
result = Error(result)
if result is True:
self.auth_timestamp = time()
del _disabler
return result
def blend_profile_blackpoint(
self,
profile1,
profile2,
XYZbp=None,
outoffset=0.0,
gamma=2.4,
gamma_type="B",
size=None,
apply_trc=True,
white_cdm2=100,
minmll=0,
maxmll=10000,
use_alternate_master_white_clip=True,
ambient_cdm2=5,
content_rgb_space="DCI P3",
hdr_chroma_compression=False,
hdr_sat=0.5,
hdr_hue=0.5,
hdr_target_profile=None,
):
"""Apply BT.1886-like tone response to profile1 using profile2 blackpoint.
profile1 has to be a matrix profile
"""
odata = self.xicclu(profile2, (0, 0, 0), pcs="x")
if len(odata) != 1 or len(odata[0]) != 3:
raise ValueError(f"Blackpoint is invalid: {odata}")
oXYZbp = odata[0]
if not XYZbp:
XYZbp = oXYZbp
smpte2084 = gamma in ("smpte2084.hardclip", "smpte2084.rolloffclip")
hlg = gamma == "hlg"
hdr = smpte2084 or hlg
lumi = profile2.tags.get("lumi", XYZType())
if not lumi.Y:
lumi.Y = 100.0
profile_black_cdm2 = XYZbp[1] * lumi.Y
if smpte2084:
# SMPTE ST.2084 (PQ)
if gamma != "smpte2084.rolloffclip":
maxmll = white_cdm2
self.log(
os.path.basename(profile1.fileName)
+ " → "
+ lang.getstr("trc." + gamma)
+ (
" {:0.0f} cd/m² (mastering {}-{:0.0f} cd/m²)".format(
white_cdm2, stripzeros(f"{minmll:.4f}"), maxmll
)
)
)
elif hlg:
# Hybrid Log-Gamma (HLG)
outoffset = 1.0
self.log(
os.path.basename(profile1.fileName)
+ " → "
+ lang.getstr("trc." + gamma)
+ (
" {:0.0f} cd/m² (ambient {} cd/m²)".format(
lumi.Y, stripzeros(f"{ambient_cdm2:.2f}")
)
)
)
elif apply_trc:
self.log(
"Applying BT.1886-like TRC to " + os.path.basename(profile1.fileName)
)
else:
self.log(
"Applying BT.1886-like black offset to "
+ os.path.basename(profile1.fileName)
)
self.log(
"Black XYZ (normalized 0..100) = {:.6f} {:.6f} {:.6f}".format(
*[v * 100 for v in XYZbp]
)
)
self.log(
"Black Lab = {:.6f} {:.6f} {:.6f}".format(
*colormath.XYZ2Lab(*[v * 100 for v in XYZbp])
)
)
self.log("Output offset = {:.2f}%".format(outoffset * 100))
if hdr:
odesc = profile1.getDescription()
desc = re.sub(
r"\s*(?:color profile|primaries with \S+ transfer function)$", "", odesc
)
if smpte2084:
# SMPTE ST.2084 (PQ)
black_cdm2 = profile_black_cdm2 * (1 - outoffset)
if XYZbp[1]:
XYZbp_cdm2 = [v / XYZbp[1] * black_cdm2 for v in XYZbp]
else:
XYZbp_cdm2 = [0, 0, 0]
profile1.set_smpte2084_trc(
XYZbp_cdm2,
white_cdm2,
minmll,
maxmll,
use_alternate_master_white_clip,
rolloff=True,
blend_blackpoint=False,
)
desc += (
" "
+ lang.getstr("trc." + gamma)
+ (
" {}-{:0.0f} cd/m² (mastering {}-{:0.0f} cd/m²)".format(
stripzeros(f"{profile_black_cdm2:.4f}"),
white_cdm2,
stripzeros(f"{minmll:.4f}"),
maxmll,
)
)
)
elif hlg:
# Hybrid Log-Gamma (HLG)
black_cdm2 = 0 # Black offset will be applied separate for HLG
white_cdm2 = lumi.Y
profile1.set_hlg_trc((0, 0, 0), white_cdm2, 1.2, ambient_cdm2)
desc += (
" "
+ lang.getstr("trc." + gamma)
+ (
" {}-{:0.0f} cd/m² (ambient {} cd/m²)".format(
stripzeros(f"{profile_black_cdm2:.4f}"),
white_cdm2,
stripzeros(f"{ambient_cdm2:.2f}"),
)
)
)
profile1.setDescription(desc)
if gamma == "smpte2084.rolloffclip" or hlg:
rgb_space = profile1.get_rgb_space()
if not rgb_space:
raise Error(
odesc
+ ": "
+ lang.getstr(
"profile.unsupported",
(lang.getstr("unknown"), profile1.colorSpace),
)
)
rgb_space[0] = 1.0 # Set gamma to 1.0 (not actually used)
rgb_space = colormath.get_rgb_space(rgb_space)
self.recent.write(desc + "\n")
linebuffered_logfiles = []
if sys.stdout and hasattr(sys.stdout, "isatty") and sys.stdout.isatty():
linebuffered_logfiles.append(print)
else:
linebuffered_logfiles.append(log)
if self.sessionlogfile:
linebuffered_logfiles.append(self.sessionlogfile)
logfiles = Files(
[
LineBufferedStream(
FilteredStream(
Files(linebuffered_logfiles),
enc,
discard="",
linesep_in="\n",
triggers=[],
)
),
self.recent,
self.lastmsg,
]
)
if hdr_chroma_compression:
xf = Xicclu(
hdr_target_profile, "r", direction="f", pcs="x", worker=self
)
xb = MP_Xicclu(
hdr_target_profile,
"r",
direction="if",
pcs="x",
use_cam_clipping=True,
worker=self,
logfile=logfiles,
)
if content_rgb_space:
content_rgb_space = colormath.get_rgb_space(content_rgb_space)
for i, color in enumerate(("white", "red", "green", "blue")):
if i == 0:
xyY = colormath.XYZ2xyY(*content_rgb_space[1])
else:
xyY = content_rgb_space[2:][i - 1]
for j, coord in enumerate("xy"):
v = xyY[j]
self.log(
"{} {} {} {:6.4f}".format(
lang.getstr("3dlut.content.colorspace"),
lang.getstr(color),
coord,
v,
)
)
else:
xf = None
xb = None
if smpte2084:
hdr_format = "PQ"
elif hlg:
hdr_format = "HLG"
cat = profile1.guess_cat() or "Bradford"
self.log("Using chromatic adaptation transform matrix:", cat)
profile = create_synthetic_hdr_clut_profile(
hdr_format,
rgb_space,
desc,
black_cdm2,
white_cdm2,
minmll, # Not used for HLG
maxmll, # Not used for HLG
use_alternate_master_white_clip, # Not used for HLG
system_gamma=1.2, # Not used for PQ
ambient_cdm2=ambient_cdm2, # Not used for PQ
maxsignal=1.0, # Not used for PQ
content_rgb_space=content_rgb_space,
sat=hdr_sat,
hue=hdr_hue,
forward_xicclu=xf,
backward_xicclu=xb,
worker=self,
logfile=logfiles,
cat=cat,
)
profile1.tags.A2B0 = profile.tags.A2B0
profile1.tags.DBG0 = profile.tags.DBG0
profile1.tags.DBG1 = profile.tags.DBG1
profile1.tags.DBG2 = profile.tags.DBG2
profile1.tags.kTRC = profile.tags.kTRC
elif apply_trc:
# Apply BT.1886-like TRC
if gamma_type in ("b", "g"):
# Get technical gamma needed to achieve effective gamma
self.log(f"Effective gamma = {gamma:.2f}")
tgamma = colormath.xicc_tech_gamma(gamma, XYZbp[1], outoffset)
else:
tgamma = gamma
self.log(f"Technical gamma = {tgamma:.2f}")
profile1.set_bt1886_trc(XYZbp, outoffset, gamma, gamma_type, size)
if not apply_trc or hdr or XYZbp is not oXYZbp:
# Apply black offset
logfiles = self.get_logfiles()
logfiles.write(
"Applying black offset (normalized 0..100) "
"{:.6f} {:.6f} {:.6f}...\n".format(*[v * 100 for v in oXYZbp])
)
profile1.apply_black_offset(
oXYZbp,
logfiles=logfiles,
thread_abort=self.thread_abort,
abortmessage=lang.getstr("aborted"),
)
def calibrate_instrument_producer(self):
cmd, args = get_argyll_util("spotread"), ["-v", "-e"]
if cmd:
self.spotread_just_do_instrument_calibration = True
result = self.add_measurement_features(args, display=False)
if isinstance(result, Exception):
self.spotread_just_do_instrument_calibration = False
return result
result = self.exec_cmd(cmd, args, skip_scripts=True)
self.spotread_just_do_instrument_calibration = False
return result
else:
return Error(lang.getstr("argyll.util.not_found", "spotread"))
def instrument_can_use_ccxx(
self, check_measurement_mode=True, instrument_name=None
):
"""Return boolean whether the instrument in its current measurement mode
can use a CCMX or CCSS colorimeter correction
"""
# Special cases:
# Spectrometer (not needed),
# ColorHug (only sensible in factory or raw measurement mode),
# ColorMunki Smile (only generic LCD CCFL measurement mode),
# Colorimétre HCFR (only raw measurement mode),
# DTP94 (only LCD, refresh and generic measurement modes)
# Spyder4/5 (only generic LCD and refresh measurement modes)
# K-10 (only factory measurement mode)
# IMPORTANT: Make changes aswell in the following locations:
# - DisplayCAL.MainFrame.create_colorimeter_correction_handler
# - DisplayCAL.MainFrame.get_ccxx_measurement_modes
# - DisplayCAL.MainFrame.set_ccxx_measurement_mode
# - DisplayCAL.MainFrame.update_colorimeter_correction_matrix_ctrl_items
# - worker.Worker.check_add_display_type_base_id
if not instrument_name:
instrument_name = self.get_instrument_name()
return (
self.argyll_version >= [1, 3, 0]
and bool(instrument_name)
and not self.get_instrument_features(instrument_name).get("spectral")
and (
not check_measurement_mode
or getcfg("measurement_mode") == "auto"
or (
(
instrument_name not in ("ColorHug", "ColorHug2")
or getcfg("measurement_mode") in ("F", "R")
)
and (
instrument_name != "ColorMunki Smile"
or getcfg("measurement_mode") == "f"
)
and (
instrument_name != "Colorimtre HCFR"
or getcfg("measurement_mode") == "R" # Missing é is NOT a typo
)
and (
instrument_name != "DTP94"
or getcfg("measurement_mode") in ("l", "c", "g")
)
and (
instrument_name not in ("Spyder4", "Spyder5")
or getcfg("measurement_mode") in ("l", "c")
)
and (
instrument_name != "SpyderX"
or getcfg("measurement_mode") == "l"
)
and (instrument_name != "K-10" or getcfg("measurement_mode") == "F")
)
)
)
def instrument_can_use_nondefault_observer(self, instrument_name=None):
if not instrument_name:
instrument_name = self.get_instrument_name()
return bool(
self.get_instrument_features(instrument_name).get("spectral")
or self.instrument_supports_ccss(instrument_name)
)
@property
def progress_wnd(self):
if not self._progress_wnd:
if (
getattr(self, "progress_start_timer", None)
and self.progress_start_timer.IsRunning()
):
if currentThread().__class__.__name__ != "_MainThread":
raise RuntimeError("GUI access in non-main thread!")
# Instantiate the progress dialog instantly on access
self.progress_start_timer.Notify()
self.progress_start_timer.Stop()
return self._progress_wnd
@progress_wnd.setter
def progress_wnd(self, progress_wnd):
self._progress_wnd = progress_wnd
@property
def progress_wnds(self):
progress_wnds = list(self._progress_dlgs.values())
if hasattr(self, "terminal"):
progress_wnds.append(self.terminal)
return progress_wnds
@property
def pwd(self):
return codecs.decode(
self._pwdstr[10:]
.ljust(int(math.ceil(len(self._pwdstr[10:]) / 4.0) * 4), "=")
.encode(),
"base64",
).decode("utf-8")
@pwd.setter
def pwd(self, pwd):
encoded_user_name = codecs.encode(
md5(getpass.getuser().encode()).hexdigest().encode(), "base64"
)[:5].decode("utf-8")
encoded_pwd = (
codecs.encode(pwd.encode(), "base64").decode("utf-8").rstrip("=\n")
)
self._pwdstr = f"/tmp/{encoded_user_name}{encoded_pwd}"
def check_add_display_type_base_id(self, cgats, cfgname="measurement_mode"):
"""Add DISPLAY_TYPE_BASE_ID to CCMX"""
if not cgats.queryv1("DISPLAY_TYPE_BASE_ID"):
# c, l (most colorimeters)
# R (ColorHug and Colorimétre HCFR)
# F (ColorHug)
# f (ColorMunki Smile)
# g (DTP94)
# IMPORTANT: Make changes aswell in the following locations:
# - DisplayCAL.MainFrame.create_colorimeter_correction_handler
# - DisplayCAL.MainFrame.get_ccxx_measurement_modes
# - DisplayCAL.MainFrame.set_ccxx_measurement_modes
# - DisplayCAL.MainFrame.update_colorimeter_correction_matrix_ctrl_items
# - worker.Worker.instrument_can_use_ccxx
cgats[0].add_keyword(
"DISPLAY_TYPE_BASE_ID",
{"c": 2, "l": 1, "R": 2, "F": 1, "f": 1, "g": 3}.get(
getcfg(cfgname), 1
),
)
print(
"Added DISPLAY_TYPE_BASE_ID {}".format(
repr(cgats[0].DISPLAY_TYPE_BASE_ID)
)
)
return True
def check_display_conf_oy_compat(self, display_no):
"""Check the screen configuration for oyranos-monitor compatibility
oyranos-monitor works off screen coordinates, so it will not handle
overlapping screens (like separate X screens, which will usually
have the same x, y coordinates)!
So, oyranos-monitor can only be used if:
- The wx.Display count is > 1 which means NOT separate X screens
OR if we use the 1st screen
- The screens don't overlap
"""
oyranos = False
if wx.Display.GetCount() > 1 or display_no == 1:
oyranos = True
for display_rect_1 in self.display_rects:
for display_rect_2 in self.display_rects:
if display_rect_1 is not display_rect_2:
if display_rect_1.Intersects(display_rect_2):
oyranos = False
break
if not oyranos:
break
return oyranos
def check_is_single_measurement(self, txt):
if (
"ambient light measuring" in txt.lower()
or "Will use emissive mode instead" in txt
) and not getattr(self, "is_ambient_measurement", False):
self.is_ambient_measurement = True
self.is_single_measurement = True
if (
getattr(self, "is_single_measurement", False)
and self.instrument_place_on_spot_msg
):
self.is_single_measurement = False
self.do_single_measurement()
self.is_ambient_measurement = False
def do_single_measurement(self):
if getattr(self, "subprocess_abort", False) or getattr(
self, "thread_abort", False
):
# If we are aborting, ignore request
return
self.progress_wnd.Pulse(" " * 4)
if self.is_ambient_measurement:
self.is_ambient_measurement = False
dlg = ConfirmDialog(
self.progress_wnd,
msg=lang.getstr("instrument.measure_ambient"),
ok=lang.getstr("ok"),
cancel=lang.getstr("cancel"),
bitmap=geticon(32, "dialog-information"),
)
self.progress_wnd.dlg = dlg
dlg_result = dlg.ShowModal()
dlg.Destroy()
if self.finished:
return
if dlg_result != wx.ID_OK:
self.abort_subprocess()
return False
if self.safe_send(" "):
self.progress_wnd.Pulse(lang.getstr("please_wait"))
def check_instrument_calibration(self, txt):
"""Check if current instrument needs sensor calibration by looking
at Argyll CMS command output"""
if not self.instrument_calibration_complete:
if "calibration complete" in txt.lower():
self.log(f"{appname}: Detected instrument calibration complete message")
self.instrument_calibration_complete = True
else:
for calmsg in INST_CAL_MSGS:
if calmsg in txt or "calibration failed" in txt.lower():
self.log(f"{appname}: Detected instrument calibration message")
self.do_instrument_calibration(
"calibration failed" in txt.lower()
)
break
def check_instrument_calibration_file(self):
# XXX: Check instrument calibration for SpyderX. For some reason,
# users tend to leave the instrument on screen despite being told
# otherwise...
if self._detected_instrument and "SpyderX" in self._detected_instrument:
if sys.platform == "win32":
cachepath = os.path.join(appdata, "Cache")
else:
cachepath = cache
spydx_cal_fn = os.path.join(
cachepath,
"ArgyllCMS",
f".spydX_{self._detected_instrument_serial}.cal",
)
if os.path.isfile(spydx_cal_fn):
# Argyll sensor cal file format offsets and lengths depend on
# the size of various C data types as seen by Argyll.
# We can determine the needed offset and length of the cal info
# (C 'int') by subtracting the length of the NULL-terminated
# serial string and size of time_t from the file size, then
# dividing by the number of C 'int's in the file.
# File structure for SpyderX cal:
# Argyll version (C 'int')
# Size of spydX (2x C 'int')
# NULL-terminated serial string (variable length)
# Black cal done (C 'int')
# Date & time (time_t)
# Calibration info (3x C 'int')
# Checksum (C 'int')
serial0 = self._detected_instrument_serial + "\0"
numints = 8
spydx_cal_size = os.stat(spydx_cal_fn).st_size
spydx_cal_int_bytes = 4 # C 'int'
# time_t might be 8 or 4 bytes, 0 is sentinel
for time_t_size in (8, 4, 0):
if (
spydx_cal_size - len(serial0) - time_t_size
) == spydx_cal_int_bytes * numints:
break
if not time_t_size:
self.log(
f"{appname}: Warning - could not determine SpyderX "
"sensor cal file format"
)
self.exec_cmd_returnvalue = Error(
"Could not determine SpyderX sensor cal file format"
)
self.abort_subprocess()
return False
self.log(f"{appname}: SpyderX cal time_t size =", time_t_size)
try:
with open(spydx_cal_fn, "rb") as spydx_cal:
# Seek to cal entries offset
spydx_cal.seek(
spydx_cal_int_bytes * 4 + len(serial0) + time_t_size
)
# Read three entries black cal
spydx_bcal = spydx_cal.read(spydx_cal_int_bytes * 3)
except EnvironmentError as exception:
self.log(
f"{appname}: Warning - could not read SpyderX sensor cal:",
exception,
)
self.exec_cmd_returnvalue = Error(
"Could not read SpyderX sensor cal: {}".format(
safe_str(exception)
)
)
self.abort_subprocess()
return False
else:
if len(spydx_bcal) < spydx_cal_int_bytes * 3:
self.log(
f"{appname}: Warning - SpyderX "
"sensor cal has unexpected length: "
f"{len(spydx_bcal)} != {spydx_cal_int_bytes * 3:d}"
)
self.exec_cmd_returnvalue = Error(
"SpyderX sensor cal "
"has unexpected "
f"length: {len(spydx_bcal)} != {spydx_cal_int_bytes * 3:d}"
)
self.abort_subprocess()
return False
else:
fmt = {2: "<HHH", 4: "<III", 8: "<QQQ"}[spydx_cal_int_bytes]
spydx_bcal = struct.unpack(fmt, spydx_bcal)
self.log(
f"{appname}: SpyderX sensor cal "
f"{spydx_bcal[0]:d} {spydx_bcal[1]:d} {spydx_bcal[2]:d}"
)
if max(spydx_bcal) > 15:
# Black cal offsets too high - user error?
self.exec_cmd_returnvalue = Error(
lang.getstr("error.spyderx.black_cal_offsets_too_high")
)
self.abort_subprocess()
# Nuke the cal file
try:
os.remove(spydx_cal_fn)
except OSError:
self.log(
f"{appname}: Warning - Could not remove "
"SpyderX sensor cal file",
spydx_cal_fn,
)
return False
return True
def check_instrument_place_on_screen(self, txt):
"""Check if instrument should be placed on screen by looking at Argyll CMS command output"""
self.instrument_place_on_spot_msg = False
if "place instrument on test window" in txt.lower():
self.instrument_place_on_screen_msg = True
elif "place instrument on spot" in txt.lower():
self.instrument_place_on_spot_msg = True
if self.instrument_place_on_screen_msg or self.instrument_place_on_spot_msg:
if not self.check_instrument_calibration_file():
return
if (
self.instrument_place_on_screen_msg and "key to continue" in txt.lower()
) or (
self.instrument_calibration_complete
and self.instrument_place_on_spot_msg
and self.progress_wnd is getattr(self, "terminal", None)
):
self.log(f"{appname}: Detected instrument placement (screen/spot) message")
self.instrument_place_on_screen_msg = False
if (
self.cmdname == get_argyll_utilname("dispcal")
and sys.platform == "darwin"
):
# On the Mac dispcal's test window
# hides the cursor and steals focus
start_new_thread(mac_app_activate, (1, wx.GetApp().AppName))
# IMPORTANT: When making changes to the instrument on screen
# detection, also apply them to appropriate part in Worker.exec_cmd
if self.instrument_calibration_complete or (
(
config.is_untethered_display()
or getcfg("measure.darken_background")
or self.madtpg_fullscreen is False
)
and (
not self.dispread_after_dispcal
or self.cmdname == get_argyll_utilname("dispcal")
or self._detecting_video_levels
)
and not self.instrument_on_screen
):
# Show a dialog asking user to place the instrument on the
# screen if the instrument calibration was completed,
# or if we measure a remote ("Web") display,
# or if we use a black background during measurements,
# but in case of the latter two only if dispread is not
# run directly after dispcal
self.instrument_calibration_complete = False
self.instrument_place_on_screen()
else:
if self.isalive():
# Delay to work-around a problem with i1D2 and Argyll 1.7
# to 1.8.3 under Mac OS X 10.11 El Capitan where skipping
# interactive display adjustment would botch the first
# reading (black)
wx.CallLater(1500, self.instrument_on_screen_continue)
elif self.instrument_place_on_spot_msg:
self.log(f"{appname}: Assuming instrument on screen")
self.instrument_on_screen = True
def instrument_on_screen_continue(self):
self.log(f"{appname}: Skipping place instrument on screen message...")
self.safe_send(" ")
self.pauseable_now = True
self.instrument_on_screen = True
def check_instrument_sensor_position(self, txt):
"""Check instrument sensor position by looking
at Argyll CMS command output"""
if "read failed due to the sensor being in the wrong position" in txt.lower():
self.instrument_sensor_position_msg = True
if self.instrument_sensor_position_msg and " or q to " in txt.lower():
self.log(f"{appname}: Detected read failed due to wrong sensor position")
self.instrument_sensor_position_msg = False
self.instrument_reposition_sensor()
def check_retry_measurement(self, txt):
if (
"key to retry:" in txt
and "read stopped at user request!" not in self.recent.read()
and (
"Sample read failed due to misread" in self.recent.read()
or "Sample read failed due to communication problem"
in self.recent.read()
)
and not self.subprocess_abort
):
self.retrycount += 1
self.log(f"{appname}: Retrying ({self.retrycount})...")
self.recent.write(f"\r\n{appname}: Retrying ({self.retrycount})...")
self.safe_send(" ")
def check_spotread_result(self, txt):
"""Check if spotread returned a result"""
if (
self.cmdname == get_argyll_utilname("spotread")
and (
self.progress_wnd is not getattr(self, "terminal", None)
or getattr(self.terminal, "Name", None) == "VisualWhitepointEditor"
)
and (
"Result is XYZ:" in txt
or "Result is Y:" in txt
or (
self.instrument_calibration_complete
and self.spotread_just_do_instrument_calibration
)
)
):
# Single spotread reading, we are done
wx.CallLater(1000, self.quit_terminate_cmd)
def get_skip_video_levels_detection(self):
"""Should we skip video levels detection?"""
return (
self._detected_output_levels
or not getcfg("patterngenerator.detect_video_levels")
or config.get_display_name() == "Untethered"
or is_ccxx_testchart()
)
def detect_video_levels(self):
"""Detect wether we need video (16..235) or data (0..255) levels"""
if self.get_skip_video_levels_detection():
return True
self._detecting_video_levels = True
try:
while True:
result = self._detect_video_levels()
if result is not None:
return result
finally:
self._detecting_video_levels = False
def _detect_video_levels(self):
"""Detect black clipping due to incorrect levels"""
self.log("Detecting output levels range...")
tempdir = self.create_tempdir()
if isinstance(tempdir, Exception):
return tempdir
ti1_path = os.path.join(tempdir, "0_16.ti1")
try:
with open(ti1_path, "wb") as ti1:
ti1.write(
"""CTI1
DESCRIPTOR "Argyll Calibration Target chart information 1"
ORIGINATOR "Argyll targen"
CREATED "Thu Apr 20 12:22:05 2017"
APPROX_WHITE_POINT "95.045781 100.000003 108.905751"
COLOR_REP "RGB"
NUMBER_OF_FIELDS 7
BEGIN_DATA_FORMAT
SAMPLE_ID RGB_R RGB_G RGB_B XYZ_X XYZ_Y XYZ_Z
END_DATA_FORMAT
NUMBER_OF_SETS 3
BEGIN_DATA
1 100.00 100.00 100.00 95.046 100.00 108.91
2 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
3 6.2500 6.2500 6.2500 0.2132 0.2241 0.2443
END_DATA
""".encode()
)
except Exception as exception:
return exception
setcfg("patterngenerator.use_video_levels", 0)
result = self.measure_ti1(
ti1_path, get_data_path("linear.cal"), allow_video_levels=False
)
if result is None:
return False
if isinstance(result, Exception) or not result:
return result
ti3_path = os.path.join(tempdir, "0_16.ti3")
try:
ti3 = CGATS(ti3_path)
except (IOError, CGATSError) as exception:
return exception
try:
verify_ti1_rgb_xyz(ti3)
except CGATSError as exception:
return exception
luminance_XYZ_cdm2 = ti3.queryv1("LUMINANCE_XYZ_CDM2")
if not luminance_XYZ_cdm2:
return Error(
lang.getstr(
"error.testchart.missing_fields", (ti3_path, "LUMINANCE_XYZ_CDM2")
)
)
try:
Y_cdm2 = float(luminance_XYZ_cdm2.split()[1])
except (IndexError, ValueError):
return Error(lang.getstr("error.testchart.invalid", ti3_path))
if Y_cdm2 <= 0 or math.isnan(Y_cdm2):
return Error(lang.getstr("error.luminance.invalid"))
black_0 = ti3[0].DATA[1]
black_16 = ti3[0].DATA[2]
if black_0 and black_16:
self.log(
"RGB level 0 is {:.6f} cd/m2".format(black_0["XYZ_Y"] / 100.0 * Y_cdm2)
)
self.log(
"RGB level 16 is {:.6f} cd/m2".format(
black_16["XYZ_Y"] / 100.0 * Y_cdm2
)
)
# Check delta cd/m2 to determine if data or video levels
# We need to take the display white luminance into account
threshold = 0.02 / Y_cdm2 * 100 # Threshold 0.02 cd/m2
assume_video_levels = black_16["XYZ_Y"] - black_0["XYZ_Y"] < threshold
if assume_video_levels:
if config.get_display_name() == "madVR":
# This is an error
return Error(lang.getstr("madvr.wrong_levels_detected"))
if not config.is_virtual_display():
# Could be a misconfiguration of display or graphics driver.
# Make the user aware.
self._detected_levels_issue_confirm_wait = True
wx.CallAfter(self.detected_levels_issue_confirm)
# Wait for call to return
while (
self._detected_levels_issue_confirm_wait
and not self.subprocess_abort
):
sleep(0.05)
if self._use_detected_video_levels is False:
return False
elif self._use_detected_video_levels is None:
# Retry
return
self.log("Using limited range output levels")
else:
self.log("Assuming full range output levels")
setcfg("patterngenerator.use_video_levels", int(assume_video_levels))
if not config.is_patterngenerator() and sys.platform == "darwin":
# macOS video levels encoding seems to only work right on
# some machines if not using videoLUT to do the scaling
setcfg_cond(assume_video_levels, "calibration.use_video_lut", 0)
self._detected_output_levels = True
else:
return Error(
lang.getstr(
"error.testchart.missing_fields",
(ti3_path, ", ".join(list(black_0.keys()))),
)
)
return True
def detected_levels_issue_confirm(self):
dlg = ConfirmDialog(
None,
msg=lang.getstr("display.levels_issue_detected"),
ok=lang.getstr("retry"),
alt=lang.getstr("fix_output_levels_using_vcgt"),
wrap=100,
)
result = dlg.ShowModal()
dlg.Destroy()
if result == wx.ID_OK:
# Retry
self._use_detected_video_levels = None
elif result == wx.ID_CANCEL:
self._use_detected_video_levels = False
else:
# Fix output levels using calibration
self._use_detected_video_levels = True
self._detected_levels_issue_confirm_wait = False
def do_instrument_calibration(self, failed=False):
"""Ask user to initiate sensor calibration and execute.
Give an option to cancel."""
if getattr(self, "subprocess_abort", False) or getattr(
self, "thread_abort", False
):
# If we are aborting, ignore request
return
self.instrument_on_screen = False
self.log(f"{appname}: Prompting to calibrate instrument")
self.progress_wnd.Pulse(" " * 4)
if failed:
msg = lang.getstr("failure")
elif self.get_instrument_name() == "ColorMunki":
msg = lang.getstr("instrument.calibrate.colormunki")
else:
# Detect type of calibration (emissive dark or reflective)
# by looking for serial number of calibration tile
# Argyll up to 1.8.3: 'Serial no.'
# Argyll 1.9.x: 'S/N'
serial = re.search(r"(?:Serial no.|S/N) (\S+)", self.recent.read(), re.I)
if serial:
# Reflective calibration, white reference tile required
# (e.g. i1 Pro hires mode)
msg = lang.getstr("instrument.calibrate.reflective", serial.group(1))
elif self._detected_instrument and "SpyderX" in self._detected_instrument:
msg = lang.getstr("instrument.calibrate.spyderx")
else:
# Emissive dark calibration
msg = lang.getstr("instrument.calibrate")
if self.use_madvr:
fullscreen = self.madtpg.is_fullscreen()
self.madtpg_show_osd(
msg, sys.platform == "win32" and self.single_real_display()
)
dlg = ConfirmDialog(
self.progress_wnd,
msg="{}\n\n{}".format(
msg,
(
self._detected_instrument
and "{}{}".format(
self._detected_instrument,
self._detected_instrument_serial
and " ({} {})".format(
lang.getstr("serial_number"),
self._detected_instrument_serial,
)
or "",
)
or self.get_instrument_name()
),
),
ok=lang.getstr("ok"),
cancel=lang.getstr("cancel"),
bitmap=geticon(32, "dialog-information"),
)
self.progress_wnd.dlg = dlg
dlg_result = dlg.ShowModal()
dlg.Destroy()
if self.finished:
self.log(
f"{appname}: Ignoring instrument calibration prompt (worker "
"thread finished)"
)
return
if dlg_result != wx.ID_OK:
self.log(f"{appname}: Canceled instrument calibration prompt")
self.abort_subprocess()
return False
self.log(f"{appname}: About to calibrate instrument")
self.progress_wnd.Pulse(lang.getstr("please_wait"))
if self.safe_send(" "):
self.progress_wnd.Pulse(lang.getstr("instrument.calibrating"))
if self.use_madvr:
if sys.platform == "win32":
self.madtpg.set_osd_text("\u25b6") # "Play" symbol
self.madtpg_restore_settings(False, fullscreen)
def abort_all(self, confirm=False):
aborted = False
for worker in workers:
if not getattr(worker, "finished", True):
worker.abort_subprocess(confirm)
aborted = True
return aborted
def abort_subprocess(self, confirm=False):
"""Abort the current subprocess or thread"""
if getattr(self, "abort_requested", False):
return
self.abort_requested = True
if confirm and getattr(self, "progress_wnd", None):
prev_dlg = getattr(self.progress_wnd, "dlg", None)
if (
prev_dlg
and prev_dlg.IsShownOnScreen()
and not isinstance(prev_dlg, DummyDialog)
):
self.abort_requested = False
return
pause = not getattr(self.progress_wnd, "paused", False) and hasattr(
self.progress_wnd, "pause_continue_handler"
)
if pause:
self.progress_wnd.pause_continue_handler(True)
self.pause_continue()
dlg = ConfirmDialog(
self.progress_wnd,
msg=lang.getstr("dialog.confirm_cancel"),
ok=lang.getstr("yes"),
cancel=lang.getstr("no"),
bitmap=geticon(32, "dialog-warning"),
)
self.progress_wnd.dlg = dlg
dlg_result = dlg.ShowModal()
if isinstance(prev_dlg, DummyDialog):
self.progress_wnd.dlg = prev_dlg
dlg.Destroy()
if dlg_result != wx.ID_OK:
if pause:
self.progress_wnd.Resume()
else:
self.progress_wnd.keepGoing = True
if hasattr(self.progress_wnd, "cancel"):
self.progress_wnd.cancel.Enable()
self.abort_requested = False
return
self.patch_count = 0
self.subprocess_abort = True
self.thread_abort = True
if self.use_patterngenerator or self.use_madnet_tpg:
abortfilename = os.path.join(self.tempdir, ".abort")
open(abortfilename, "w").close()
delayedresult.startWorker(self.quit_terminate_consumer, self.quit_terminate_cmd)
def quit_terminate_consumer(self, delayedResult):
try:
result = delayedResult.get()
except Exception as exception:
if hasattr(exception, "originalTraceback"):
self.log(exception.originalTraceback, fn=log)
else:
self.log(traceback.format_exc(), fn=log)
result = UnloggedError(safe_str(exception))
if isinstance(result, Exception):
show_result_dialog(result, getattr(self, "progress_wnd", None))
result = False
self.subprocess_abort = False
if not result:
self.thread_abort = False
self.abort_requested = False
if getattr(self, "progress_wnd", None):
self.progress_wnd.Resume()
def instrument_place_on_screen(self):
"""Show a dialog asking user to place the instrument on the screen
and give an option to cancel"""
if getattr(self, "subprocess_abort", False) or getattr(
self, "thread_abort", False
):
# If we are aborting, ignore request
return
self.log(f"{appname}: Prompting to place instrument on screen")
self.progress_wnd.Pulse(" " * 4)
if self.use_madvr:
fullscreen = self.madtpg.is_fullscreen()
self.madtpg_show_osd(
lang.getstr("instrument.place_on_screen"),
sys.platform == "win32" and self.single_real_display(),
)
dlg = ConfirmDialog(
self.progress_wnd,
msg="{}\n\n{}".format(
lang.getstr("instrument.place_on_screen"),
(
self._detected_instrument
and "{}{}".format(
self._detected_instrument,
self._detected_instrument_serial
and " ({} {})".format(
lang.getstr("serial_number"),
self._detected_instrument_serial,
)
or "",
)
or self.get_instrument_name()
),
),
ok=lang.getstr("ok"),
cancel=lang.getstr("cancel"),
bitmap=geticon(32, "dialog-information"),
)
self.progress_wnd.dlg = dlg
dlg_result = dlg.ShowModal()
dlg.Destroy()
if self.finished:
self.log(
f"{appname}: Ignoring instrument placement prompt (worker thread "
"finished)"
)
return
if dlg_result != wx.ID_OK:
self.log(f"{appname}: Canceled instrument placement prompt")
self.abort_subprocess()
return False
self.instrument_on_screen = True
self.log(f"{appname}: Instrument on screen")
if not isinstance(self.progress_wnd, (UntetheredFrame, DisplayUniformityFrame)):
self.safe_send(" ")
self.pauseable_now = True
if self.use_madvr:
if sys.platform == "win32":
self.madtpg.set_osd_text("\u25b6") # "Play" symbol
self.madtpg_restore_settings(False, fullscreen)
def instrument_reposition_sensor(self):
if getattr(self, "subprocess_abort", False) or getattr(
self, "thread_abort", False
):
# If we are aborting, ignore request
return
self.log(f"{appname}: Prompting to reposition instrument sensor")
self.progress_wnd.Pulse(" " * 4)
fullscreen = False
if self.use_madvr:
fullscreen = self.madtpg.is_fullscreen()
self.madtpg_show_osd(
lang.getstr("instrument.reposition_sensor"),
sys.platform == "win32" and self.single_real_display(),
)
dlg = ConfirmDialog(
self.progress_wnd,
msg=lang.getstr("instrument.reposition_sensor"),
ok=lang.getstr("ok"),
cancel=lang.getstr("cancel"),
bitmap=geticon(32, "dialog-warning"),
)
self.progress_wnd.dlg = dlg
dlg_result = dlg.ShowModal()
dlg.Destroy()
if self.finished:
self.log(
f"{appname}: Ignoring instrument sensor repositioning prompt (worker "
"thread finished)"
)
return
if dlg_result != wx.ID_OK:
self.log(f"{appname}: Canceled instrument sensor repositioning prompt")
self.abort_subprocess()
return False
self.safe_send(" ")
if self.use_madvr:
if sys.platform == "win32":
self.madtpg.set_osd_text("\u25b6") # "Play" symbol
self.madtpg_restore_settings(False, fullscreen)
def clear_argyll_info(self):
"""Clear Argyll CMS version, detected displays and instruments."""
self.argyll_bin_dir = None
self.argyll_version = [0, 0, 0]
self.argyll_version_string = "0.0.0"
self._displays = []
self.display_edid = []
self.display_manufacturers = []
self.display_names = []
self.display_rects = []
self.displays = []
self.instruments = []
self.lut_access = []
self.reset_argyll_enum()
def reset_argyll_enum(self):
"""Reset auto-detected (during display/instrument enumeration) properties"""
self.measurement_modes = {}
self.argyll_virtual_display = None
def clear_cmd_output(self):
"""Clear any output from the last run command."""
self.cmd = None
self.cmdname = None
self.retcode = -1
self.output = []
self.errors = []
self.recent.clear()
self.retrycount = 0
self.lastmsg.clear()
self.repeat = False
self.send_buffer = None
# Log interaction with Argyll tools
if not hasattr(self, "logger") or (
isinstance(self.logger, DummyLogger)
and self.owner
and self.owner.Name == "mainframe"
):
if not self.owner or self.owner.Name != "mainframe":
self.logger = DummyLogger()
else:
self.logger = get_file_logger("interact")
if hasattr(self, "thread") and self.thread.is_alive() and self.interactive:
self.logger.info("-" * 80)
self.sessionlogfile = None
self.madtpg_bw_lvl = None
self.madtpg_fullscreen = None
self.use_madvr = False
self.use_madnet_tpg = False
self.use_patterngenerator = False
self.patch_sequence = False
self.patch_count = 0
self.patterngenerator_sent_count = 0
self.exec_cmd_returnvalue = None
self.tmpfiles = {}
self.buffer = []
self._detected_instrument = None
self._detected_instrument_serial = None
def lut3d_get_filename(
self, path=None, include_input_profile=True, include_ext=True
):
# 3D LUT filename with crcr32 hash before extension - up to DCG 2.9.0.7
profile_save_path = os.path.splitext(
path or getcfg("calibration.file") or defaults["calibration.file"]
)[0]
lut3d = [
getcfg("3dlut.gamap.use_b2a") and "gg" or "G",
"i{}".format(getcfg("3dlut.rendering_intent")),
"r{:d}".format(getcfg("3dlut.size")),
"e{}".format(getcfg("3dlut.encoding.input")),
"E{}".format(getcfg("3dlut.encoding.output")),
"I{}:{}:{}".format(
getcfg("3dlut.trc_gamma_type"),
getcfg("3dlut.trc_output_offset"),
getcfg("3dlut.trc_gamma"),
),
]
if getcfg("3dlut.format") == "3dl":
lut3d.append(str(getcfg("3dlut.bitdepth.input")))
if getcfg("3dlut.format") in ("3dl", "png", "ReShade"):
lut3d.append(str(getcfg("3dlut.bitdepth.output")))
if include_ext:
lut3d_ext = getcfg("3dlut.format")
if lut3d_ext == "eeColor":
lut3d_ext = "txt"
elif lut3d_ext == "madVR":
lut3d_ext = "3dlut"
elif lut3d_ext == "ReShade":
lut3d_ext = "png"
elif lut3d_ext == "icc":
lut3d_ext = profile_ext[1:]
else:
lut3d_ext = ""
if include_input_profile:
input_profname = os.path.splitext(
os.path.basename(getcfg("3dlut.input.profile"))
)[0]
else:
input_profname = ""
lut3d_path = ".".join(
[
_f
for _f in [
profile_save_path,
input_profname,
"{:X}".format(zlib.crc32("-".join(lut3d).encode()) & 0xFFFFFFFF),
lut3d_ext,
]
if _f
]
)
if not include_input_profile or not os.path.isfile(lut3d_path):
# 3D LUT filename with plain options before extension - DCG 2.9.0.8+
enc_in = lut3d[3][1:]
enc_out = lut3d[4][1:]
encoding = enc_in
if enc_in != enc_out:
encoding += enc_out
if getcfg("3dlut.output.profile.apply_cal"):
cal_exclude = ""
else:
cal_exclude = "e"
if getcfg("3dlut.trc").startswith("smpte2084"):
lut3dp = [str(getcfg("3dlut.trc_output_offset")) + ",2084"]
if (
getcfg("3dlut.hdr_peak_luminance") < 10000
or getcfg("3dlut.trc") == "smpte2084.rolloffclip"
or getcfg("3dlut.hdr_minmll")
or getcfg("3dlut.hdr_maxmll") < 10000
):
lut3dp.append("@{:0.0f}".format(getcfg("3dlut.hdr_peak_luminance")))
if getcfg("3dlut.trc") == "smpte2084.hardclip":
lut3dp.append("h")
else:
lut3dp.append("s")
# Alternate clip - softer
if getcfg("3dlut.hdr_maxmll_alt_clip"):
lut3dp.append("s")
if getcfg("3dlut.hdr_minmll"):
lut3dp.append("{:.4f}".format(getcfg("3dlut.hdr_minmll")))
if (
getcfg("3dlut.hdr_minmll")
and getcfg("3dlut.hdr_maxmll") < 10000
):
lut3dp.append("-")
if getcfg("3dlut.hdr_maxmll") < 10000:
lut3dp.append("{:0.0f}".format(getcfg("3dlut.hdr_maxmll")))
if getcfg("3dlut.trc") == "smpte2084.rolloffclip":
# Hue and chroma preservation
if getcfg("3dlut.hdr_sat") != 0.5:
lut3dp.append("s{:.1f}".format(getcfg("3dlut.hdr_sat")))
if getcfg("3dlut.hdr_hue") != 1.0:
lut3dp.append("h{:.1f}".format(getcfg("3dlut.hdr_hue")))
elif getcfg("3dlut.trc") == "hlg":
lut3dp = ["HLG"]
if getcfg("3dlut.hdr_ambient_luminance") != 5:
ambient = stripzeros(getcfg("3dlut.hdr_ambient_luminance"))
lut3dp.append(f"@{ambient}")
elif getcfg("3dlut.apply_trc"):
lut3dp = [
lut3d[5][1].replace("b", "bb") + lut3d[5][3:].replace(":", ",")
] # TRC
else:
# Use src profile TRC unmodified
lut3dp = []
lut3dp.extend(
[
cal_exclude,
lut3d[0], # Gamut mapping mode
lut3d[1][1:], # Rendering intent
encoding,
lut3d[2][1:],
]
) # Resolution
bitdepth_in = None
bitdepth_out = None
if len(lut3d) > 6:
bitdepth_in = lut3d[6] # Input bitdepth
if len(lut3d) > 7:
bitdepth_out = lut3d[7] # Output bitdepth
if bitdepth_in or bitdepth_out:
bitdepth = bitdepth_in
if bitdepth_out and bitdepth_in != bitdepth_out:
bitdepth += bitdepth_out
lut3dp.append(bitdepth)
lut3d_path = ".".join(
[
_f
for _f in [
profile_save_path,
input_profname,
"".join(lut3dp),
lut3d_ext,
]
if _f
]
)
return lut3d_path
def create_3dlut(
self,
profile_in,
path,
profile_abst=None,
profile_out=None,
apply_cal=True,
intent="r",
format="cube",
size=17,
input_bits=10,
output_bits=12,
maxval=None,
input_encoding="n",
output_encoding="n",
trc_gamma=None,
trc_gamma_type="B",
trc_output_offset=0.0,
save_link_icc=True,
apply_black_offset=True,
use_b2a=False,
white_cdm2=100,
minmll=0,
maxmll=10000,
use_alternate_master_white_clip=True,
hdr_sat=0.5,
hdr_hue=0.5,
ambient_cdm2=5,
content_rgb_space="DCI P3",
hdr_display=False,
XYZwp=None,
):
"""Create a 3D LUT from one (device link) or two (device) profiles,
optionally incorporating an abstract profile."""
# .cube: http://doc.iridas.com/index.php?title=LUT_Formats
# .3dl: http://www.kodak.com/US/plugins/acrobat/en/motion/products/look/UserGuide.pdf
# http://download.autodesk.com/us/systemdocs/pdf/lustre_color_management_user_guide.pdf
# .spi3d: https://github.com/imageworks/OpenColorIO/blob/master/src/core/FileFormatSpi3D.cpp
# .mga: http://pogle.pandora-int.com/download/manual/lut3d_format.html
print("-" * 80)
print(lang.getstr("3dlut.create"))
for profile in (profile_in, profile_out):
if (
profile.profileClass not in (b"mntr", b"link", b"scnr", b"spac")
or profile.colorSpace != b"RGB"
):
raise NotImplementedError(
lang.getstr(
"profile.unsupported",
(profile.profileClass, profile.colorSpace),
)
)
if profile_in.profileClass == b"link":
break
# Setup temp dir
cwd = self.create_tempdir()
if isinstance(cwd, Exception):
raise cwd
result = None
path = os.path.split(path)
path = os.path.join(path[0], make_argyll_compatible_path(path[1]))
filename, ext = os.path.splitext(path)
name = os.path.basename(filename)
if profile_in.profileClass == b"link":
link_basename = os.path.basename(profile_in.fileName)
link_filename = os.path.join(cwd, link_basename)
profile_in.write(link_filename)
else:
collink = get_argyll_util("collink")
if not collink:
raise Error(lang.getstr("argyll.util.not_found", "collink"))
extra_args = parse_argument_string(getcfg("extra_args.collink"))
smpte2084 = trc_gamma in ("smpte2084.hardclip", "smpte2084.rolloffclip")
hlg = trc_gamma == "hlg"
hdr = smpte2084 or hlg
profile_in_basename = make_argyll_compatible_path(
os.path.basename(profile_in.fileName)
)
# XXX: collink creates dark blotch in yellow with perceptual intent
# when using HDR PQ tonemapping - force to colorimetric and rely on
# our own perceptual mapping
if hdr and not hlg:
if intent == "p":
intent = "r"
elif intent == "pa":
intent = "aw"
hdr_use_src_gamut = (
hdr
and profile_in_basename == "Rec2020.icm"
and intent in ("la", "lp", "p", "pa", "ms", "s", "aa")
and not get_arg("-G", extra_args)
and not get_arg("-g", extra_args)
)
if hdr and not hdr_use_src_gamut and not use_b2a:
# Always use B2A instead of inverse forward table (faster and
# better result if B2A table has enough effective resolution)
# for HDR with colorimetric intents
# Check B2A resolution and regenerate on-the-fly if too low
# and created by ArgyllCMS
b2a = profile_out.tags.get("B2A1", profile_out.tags.get("B2A0"))
a2b = profile_out.tags.get("A2B1", profile_out.tags.get("A2B0"))
if (
not b2a
or (
isinstance(b2a, LUT16Type)
and b2a.clut_grid_steps < 17
and profile_out.creator == "argl"
)
) and a2b:
clutres = getcfg("profile.b2a.hires.size")
b2aresult = self.update_profile_B2A(profile_out, clutres=clutres)
if isinstance(b2aresult, Exception):
raise b2aresult
profile_out.write()
if "B2A1" in profile_out.tags or "B2A0" in profile_out.tags:
use_b2a = True
# Argyll applycal can't deal with single gamma TRC tags
# or TRC tags with less than 256 entries
_applycal_bug_workaround(profile_out)
# Prepare building a device link
link_basename = name + profile_ext
link_filename = os.path.join(cwd, link_basename)
profile_out_basename = make_argyll_compatible_path(
os.path.basename(profile_out.fileName)
)
if profile_in_basename == profile_out_basename:
(profile_out_filename, profile_out_ext) = os.path.splitext(
profile_out_basename
)
profile_out_basename = "{} (2){}".format(
profile_out_filename,
profile_out_ext,
)
profile_out.fileName = os.path.join(cwd, profile_out_basename)
profile_out.write()
profile_out_cal_path = os.path.splitext(profile_out.fileName)[0] + ".cal"
manufacturer = profile_out.getDeviceManufacturerDescription()
model = profile_out.getDeviceModelDescription()
device_manufacturer = profile_out.device["manufacturer"]
device_model = profile_out.device["model"]
mmod = profile_out.tags.get("mmod")
self.set_sessionlogfile(None, name, cwd)
collink_version_string = get_argyll_version_string("collink")
collink_version = parse_argyll_version_string(collink_version_string)
use_collink_bt1886 = trc_gamma and not hdr
# The display profile may not reflect the measured black point.
# Get it from the embedded TI3 instead if zero from lookup.
odata = self.xicclu(profile_out, (0, 0, 0), pcs="x")
if len(odata) != 1 or len(odata[0]) != 3:
raise ValueError(f"Blackpoint is invalid: {odata}")
XYZbp = odata[0]
if not XYZbp[1] and isinstance(profile_out.tags.get("targ"), Text):
XYZbp = profile_out.get_chardata_bkpt()
if XYZbp:
XYZbp = [
v * XYZbp[1] for v in list(profile_out.tags.wtpt.pcs.values())
]
self.log(
"Using black Y from destination profile "
"characterization data (normalized 0..100): "
"{:.6f}".format(XYZbp[1] * 100)
)
use_collink_bt1886 = False
elif XYZbp[1]:
# Use display profile blackpoint
XYZbp = None
# Using xicclu instead of collink is more accurate on and near
# the gray axis (in some cases, i.e. HDR and cLUT res < 65,
# significantly so)
use_xicclu = (
(experimental or hdr)
and not profile_abst
and intent in ("a", "aw", "r")
and input_encoding in ("n", "t", "T")
and output_encoding in ("n", "t")
)
# Apply calibration?
if apply_cal:
extract_cal_from_profile(profile_out, profile_out_cal_path)
if self.argyll_version < [1, 6] or use_xicclu:
# Can't apply the calibration with old collink versions -
# apply the calibration to the 'out' profile prior to
# device linking instead
applycal = get_argyll_util("applycal")
if not applycal:
raise Error(lang.getstr("argyll.util.not_found", "applycal"))
self.log(lang.getstr("apply_cal"))
result = self.exec_cmd(
applycal,
[
"-v",
profile_out_cal_path,
profile_out_basename,
profile_out.fileName,
],
capture_output=True,
skip_scripts=True,
sessionlogfile=self.sessionlogfile,
)
if isinstance(result, Exception) and not getcfg("dry_run"):
raise result
elif not result:
raise Error(
"\n\n".join(
[lang.getstr("apply_cal.error"), "\n".join(self.errors)]
)
)
profile_out = ICCProfile(profile_out.fileName)
in_rgb_space = profile_in.get_rgb_space()
if in_rgb_space:
in_colors = colormath.get_rgb_space_primaries_wp_xy(in_rgb_space)
if format == "madVR":
# Use a D65 white for the 3D LUT Input_Primaries as
# madVR can only deal correctly with D65
# Use the same D65 xy values as written by madVR
# 3D LUT install API (ASTM E308-01)
in_colors[6:] = [0.31273, 0.32902]
else:
in_colors = []
if hdr_use_src_gamut:
content_rgb_space = colormath.get_rgb_space(content_rgb_space)
# Always force content RGB space whitepoint to D65 so
# default RGB to ICtCp matrix gives Ct=Cp=0 for R=G=B
# when tone mapping
content_rgb_space = list(content_rgb_space)
content_rgb_space[1] = colormath.get_whitepoint("D65")
content_colors = colormath.get_rgb_space_primaries_wp_xy(
content_rgb_space
)
if hdr_use_src_gamut and content_colors[:6] != in_colors[:6]:
# Use source gamut to preserve more saturation.
# Assume content colorspace (i.e. DCI P3) encoded within
# container colorspace (i.e. Rec. 2020)
# Get source profile
crx, cry = content_rgb_space[2:][0][:2]
cgx, cgy = content_rgb_space[2:][1][:2]
cbx, cby = content_rgb_space[2:][2][:2]
cwx, cwy = colormath.XYZ2xyY(*content_rgb_space[1])[:2]
rgb_space_name = (
colormath.find_primaries_wp_xy_rgb_space_name(content_colors)
or "Custom"
)
cat = profile_in.guess_cat() or "Bradford"
self.log(
"Using chromatic adaptation transform matrix from "
"input profile for content colorspace:",
cat,
)
profile_src = ICCProfile.from_chromaticities(
crx,
cry,
cgx,
cgy,
cbx,
cby,
cwx,
cwy,
2.2,
rgb_space_name,
"",
cat=cat,
)
fd, profile_src.fileName = tempfile.mkstemp(
profile_ext, f"{rgb_space_name}-", dir=cwd
)
stream = os.fdopen(fd, "wb")
profile_src.write(stream)
stream.close()
else:
profile_src = profile_out
# Deal with applying TRC
if trc_gamma:
if (
use_collink_bt1886
and not use_xicclu
and (collink_version >= [1, 7] or not trc_output_offset)
):
# Make sure the profile has the expected Rec. 709 TRC
# for BT.1886
self.log(
f"{appname}: Applying Rec. 709 TRC to "
f"{os.path.basename(profile_in.fileName)}"
)
for i, channel in enumerate(("r", "g", "b")):
if channel + "TRC" in profile_in.tags:
profile_in.tags[channel + "TRC"].set_trc(-709)
else:
# For HDR or Argyll < 1.7 beta, alter profile TRC
# Argyll CMS prior to 1.7 beta development code 2014-07-10
# does not support output offset, alter the source profile
# instead (note that accuracy is limited due to 16-bit
# encoding used in ICC profile, collink 1.7 can use full
# floating point processing and will be more precise)
self.blend_profile_blackpoint(
profile_in,
profile_out,
XYZbp,
trc_output_offset,
trc_gamma,
trc_gamma_type,
white_cdm2=white_cdm2,
minmll=minmll,
maxmll=maxmll,
use_alternate_master_white_clip=use_alternate_master_white_clip,
ambient_cdm2=ambient_cdm2,
content_rgb_space=content_rgb_space,
hdr_chroma_compression=not hdr_use_src_gamut
or content_colors[:6] != in_colors[:6],
hdr_sat=hdr_sat,
hdr_hue=hdr_hue,
hdr_target_profile=profile_src,
)
elif apply_black_offset:
# Apply only the black point blending portion of BT.1886 mapping
self.blend_profile_blackpoint(
profile_in, profile_out, XYZbp, 1.0, apply_trc=False
)
# Deal with whitepoint
profile_in_wtpt_XYZ = list(profile_in.tags.wtpt.ir.values())
if XYZwp:
# Quantize to ICC s15Fixed16Number encoding
XYZwp = [
s15Fixed16Number(s15Fixed16Number_tohex(v)) for v in XYZwp
]
else:
XYZwp = profile_in_wtpt_XYZ
if XYZwp != profile_in_wtpt_XYZ:
self.log(
"Using whitepoint chromaticity {:.4f} {:.4f} for input".format(
*colormath.XYZ2xyY(*XYZwp)[:2]
)
)
(
profile_in.tags.wtpt.X,
profile_in.tags.wtpt.Y,
profile_in.tags.wtpt.Z,
) = XYZwp
profile_in.fileName = os.path.join(cwd, profile_in_basename)
profile_in.write()
if hdr_use_src_gamut and content_colors[:6] != in_colors[:6] and False:
# XXX: Old code - could be removed?
# Use source gamut to preserve more saturation.
# Assume content colorspace (i.e. DCI P3) encoded within
# container colorspace (i.e. Rec. 2020)
tools = {}
for toolname in ( # "iccgamut",
# "timage",
"cctiff",
"tiffgamut",
):
tools[toolname] = get_argyll_util(toolname)
if not tools[toolname]:
raise Error(lang.getstr("argyll.util.not_found", toolname))
# Apply HDR TRC to source
self.blend_profile_blackpoint(
profile_src,
profile_out,
XYZbp,
trc_output_offset,
trc_gamma,
trc_gamma_type,
white_cdm2=white_cdm2,
minmll=minmll,
maxmll=maxmll,
use_alternate_master_white_clip=use_alternate_master_white_clip,
ambient_cdm2=ambient_cdm2,
hdr_chroma_compression=False,
hdr_sat=hdr_sat,
hdr_hue=hdr_hue,
)
profile_src.write()
# Create link from source to destination profile
gam_link_filename = tempfile.mktemp(profile_ext, "gam-link-", dir=cwd)
result = self.exec_cmd(
collink,
[
"-v",
"-G",
"-ir",
profile_src.fileName,
profile_in_basename,
gam_link_filename,
],
capture_output=True,
skip_scripts=True,
sessionlogfile=self.sessionlogfile,
)
if isinstance(result, Exception) and not getcfg("dry_run"):
raise result
elif not result:
raise Error(
"\n".join(self.errors)
or "{} {}".format(collink, lang.getstr("error"))
)
# Create RGB image
# gam_in_tiff = tempfile.mktemp(".tif", "gam-in-", dir=cwd)
# result = self.exec_cmd(tools["timage"], ["-x", gam_in_tiff],
# capture_output=True,
# skip_scripts=True,
# sessionlogfile=self.sessionlogfile)
# if isinstance(result, Exception) and not getcfg("dry_run"):
# raise result
# elif not result:
# raise Error("\n".join(self.errors) or lang.getstr("error"))
fd, gam_in_tiff = tempfile.mkstemp(".tif", "gam-in-", dir=cwd)
stream = os.fdopen(fd, "wb")
imfile.write_rgb_clut(stream, 65, format="TIFF")
stream.close()
# Convert RGB image from source to destination to get source
# encoded within destination.
gam_out_tiff = tempfile.mktemp(".tif", "gam-out-", dir=cwd)
result = self.exec_cmd(
tools["cctiff"],
["-p", gam_link_filename, gam_in_tiff, gam_out_tiff],
capture_output=True,
skip_scripts=True,
sessionlogfile=self.sessionlogfile,
)
if isinstance(result, Exception) and not getcfg("dry_run"):
raise result
elif not result:
raise Error(
"\n".join(self.errors)
or "{} {}".format(tools["cctiff"], lang.getstr("error"))
)
# Create gamut surface from image
# gam_filename = os.path.splitext(profile_src.fileName)[0] + ".gam"
# result = self.exec_cmd(tools["iccgamut"], ["-ir", "-pj",
# profile_src.fileName],
# capture_output=True,
# skip_scripts=True,
# sessionlogfile=self.sessionlogfile)
gam_filename = os.path.splitext(gam_out_tiff)[0] + ".gam"
result = self.exec_cmd(
tools["tiffgamut"],
["-ir", "-pj", profile_in.fileName, gam_out_tiff],
capture_output=True,
skip_scripts=True,
sessionlogfile=self.sessionlogfile,
)
if isinstance(result, Exception) and not getcfg("dry_run"):
raise result
elif not result:
raise Error(
"\n".join(self.errors)
or "{} {}".format(tools["tiffgamut"], lang.getstr("error"))
)
else:
gam_filename = None
# Now build the device link
args = [
"-v",
"-qh",
"-g" if use_b2a else "-G",
f"-i{intent}",
f"-r{size:d}",
"-n",
]
if gam_filename:
# Use source gamut
args.insert(3, gam_filename)
if profile_abst:
profile_abst.write(os.path.join(cwd, "abstract.icc"))
args.extend(["-p", "abstract.icc"])
if self.argyll_version >= [1, 6]:
if format == "madVR":
args.append("-3m")
elif format == "eeColor" and not test:
args.append("-3e")
elif format == "cube" and collink_version >= [1, 7] and not test:
args.append("-3c")
args.append(f"-e{input_encoding}")
args.append(f"-E{output_encoding}")
if collink_version >= [1, 7]:
args.append("-b") # Use RGB->RGB forced black point hack
if use_collink_bt1886 and trc_gamma_type in ("b", "B"):
if collink_version >= [1, 7]:
args.append(
f"-I{trc_gamma_type}:{trc_output_offset}:{trc_gamma}"
)
elif not trc_output_offset:
args.append(f"-I{trc_gamma_type}:{trc_gamma}")
if apply_cal:
# Apply the calibration when building our device link
# i.e. use collink -a parameter (apply calibration curves
# to link output and append linear)
args.extend(["-a", os.path.basename(profile_out_cal_path)])
if extra_args:
args += extra_args
# cLUT Input value tweaks to make Video encoded black land on
# 65 res grid nodes, which should help 33 and 17 res cLUTs too
def cLUT65_to_VidRGB(v):
if size not in (17, 33, 65):
return v
return colormath.cLUT65_to_VidRGB(v)
def VidRGB_to_cLUT65(v):
if size not in (17, 33, 65):
return v
return colormath.VidRGB_to_cLUT65(v)
logfiles = self.get_logfiles()
xts = time()
if use_xicclu:
# Create device link using xicclu
is_argyll_lut_format = format == "icc"
def clipVidRGB(RGB, black_hack=True):
"""Clip a value to the RGB Video range 16..235 RGB.
Clip the incoming value RGB[] in place.
Return a bit mask of the channels that have/would clip,
scale all non-black values to avoid positive clipping and
return the restoring scale factor (> 1.0) if this has occured,
return the full value in the clip direction in full[],
and return the uncliped value in unclipped[].
"""
clipmask = 0
scale = 1.0
full = [None] * 3
unclipped = list(RGB)
# Locate the channel with the largest value
mx = max(RGB)
# One channel positively clipping
if mx > 235.0 / 255.0:
scale = ((235.0 - 16.0) / 255.0) / (mx - (16.0 / 255.0))
# Scale all non-black value down towards black, to avoid clipping
for i, v in enumerate(RGB):
# Note if channel would clip in itself
if v > 235.0 / 255.0:
full[i] = 1.0
clipmask |= 1 << i
if v > 16.0 / 255.0:
RGB[i] = (v - 16.0 / 255.0) * scale + 16.0 / 255.0
# See if any values negatively clip
for i, v in enumerate(RGB):
if black_hack:
cond = v <= 16.0 / 255.0
else:
cond = v < 16.0 / 255.0
if cond:
RGB[i] = 16.0 / 255.0
full[i] = 0.0
clipmask |= 1 << i
scale = 1.0 / scale
return clipmask, scale, full, unclipped, RGB
logfiles.write("Generating input values...\n")
clip = {}
seen = {} # Seen RGB input values
seenkeys = {}
RGB_src_in = []
maxind = size - 1.0
level_16 = VidRGB_to_cLUT65(16.0 / 255) * maxind
level_235 = VidRGB_to_cLUT65(235.0 / 255) * maxind
prevperc = 0
cliphack = False # Clip TV levels BtB/WtW in for full range out?
for a in range(size):
for b in range(size):
for c in range(size):
if self.thread_abort:
raise Info(lang.getstr("aborted"))
abc = (a, b, c)
RGB = [v / maxind for v in abc]
if input_encoding in ("t", "T"):
# TV levels in
if (
cliphack
and output_encoding == "n"
and (
min(abc) < level_16
or max(abc) > math.ceil(level_235)
)
):
# Don't lookup out of range values
continue
else:
RGB = [cLUT65_to_VidRGB(v) for v in RGB]
clip[abc] = clipVidRGB(RGB)
if a == b == c and a in (
math.ceil(level_16),
math.ceil(level_235),
):
self.log(
"Input RGB (0..255) {:f} {:f} {:f}".format(
*[v * 255 for v in RGB]
)
)
# Convert 16..235 to 0..255 for xicclu
RGB = [
colormath.convert_range(
v, 16.0 / 255, 235.0 / 255, 0, 1
)
for v in RGB
]
RGB = [min(max(v, 0), 1) * 255 for v in RGB]
seenkey = tuple(int(round(v * 257)) for v in RGB)
seenkeys[abc] = seenkey
if seenkey in seen:
continue
seen[seenkey] = True
RGB_src_in.append(RGB)
perc = round(
((a + 1) * (b + 1) * (c + 1)) / float(size**3) * 100
)
if perc > prevperc:
logfiles.write(f"\r{perc:0.0f}%")
prevperc = perc
logfiles.write("\n")
self.log("Skipped", size**3 - len(seen), "duplicate input values")
# Forward lookup input RGB through source profile
logfiles.write("Looking up input values through source profile...\n")
XYZ_src_out = self.xicclu(
profile_in,
RGB_src_in,
intent[0],
pcs="x",
scale=255,
logfile=logfiles,
)
del RGB_src_in
if intent == "aw":
XYZw = self.xicclu(profile_in, [[1, 1, 1]], intent[0], pcs="x")[0]
# Lookup scaled down white XYZ
logfiles.write("Looking for solution...\n")
for n in range(9):
XYZscaled = []
for i in range(2001):
XYZscaled.append(
[v * (1 - (n * 2001 + i) / 20000.0) for v in XYZw]
)
RGBscaled = self.xicclu(
profile_out,
XYZscaled,
"a",
"b" if use_b2a else "if",
pcs="x",
get_clip=True,
)
# Find point at which it no longer clips
XYZwscaled = None
for i, RGBclip in enumerate(RGBscaled):
if self.thread_abort:
raise Info(lang.getstr("aborted"))
if RGBclip[3] is True or max(RGBclip[:3]) > 1:
# Clipped, skip
continue
# Found
XYZwscaled = XYZscaled[i]
logfiles.write(
"Solution found at index {:d} "
"(step size {:f})\n".format(i, 1 / 2000.0)
)
logfiles.write(
"RGB white {:6.4f} {:6.4f} {:6.4f}\n".format(
*RGBclip[:3]
)
)
logfiles.write(
"XYZ white {:6.4f} {:6.4f} {:6.4f}, CCT {:.1f} K\n".format(
*(XYZscaled[i] + [colormath.XYZ2CCT(*XYZwscaled)])
)
)
break
else:
if n == 8:
break
if XYZwscaled:
# Found solution
break
if not XYZwscaled:
raise Error(
"No solution found in {:d} iterations with {:d} steps".format(
n, i
)
)
for i, XYZ in enumerate(XYZ_src_out):
XYZ[:] = [v / XYZw[1] * XYZwscaled[1] for v in XYZ]
del RGBscaled
# Inverse forward lookup source profile output XYZ through
# destination profile
num_cpus = cpu_count()
num_workers = min(max(num_cpus, 1), size)
if "A2B0" in profile_out.tags and not use_b2a:
if num_cpus > 2:
num_workers = int(num_workers * 0.75)
num_batches = size // 9
else:
if num_cpus > 2:
num_workers = 2
num_batches = 1
if use_b2a:
direction = "backward"
else:
direction = "inverse forward"
logfiles.write(
"Creating device link from {} lookup ({:d} workers)...\n".format(
direction, num_workers
)
)
RGB_dst_out = []
for slices in pool_slice(
_mp_xicclu,
XYZ_src_out,
(profile_out.fileName, intent[0], "b" if use_b2a else "if"),
{
"pcs": "x",
"use_cam_clipping": True,
"abortmessage": lang.getstr("aborted"),
},
num_workers,
self.thread_abort,
logfiles,
num_batches=num_batches,
):
RGB_dst_out.extend(slices)
del XYZ_src_out
logfiles.write("\n")
logfiles.write("Filling cLUT...\n")
profile_link = ICCProfile()
profile_link.profileClass = b"link"
profile_link.connectionColorSpace = b"RGB"
profile_link.setDescription(name)
profile_link.setCopyright(getcfg("copyright"))
profile_link.tags.pseq = ProfileSequenceDescType(
profile=profile_link
)
profile_link.tags.pseq.add(profile_in)
profile_link.tags.pseq.add(profile_out)
profile_link.tags.A2B0 = A2B0 = LUT16Type(
None, "A2B0", profile_link
)
A2B0.matrix = colormath.Matrix3x3([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
if input_encoding in ("t", "T"):
A2B0.input = [[]] * 3
for i in range(2048):
A2B0.input[0].append(VidRGB_to_cLUT65(i / 2047.0) * 65535)
else:
A2B0.input = [[0, 65535]] * 3
A2B0.output = [[0, 65535]] * 3
A2B0.clut = []
clut = {}
n = 0
for a in range(size):
for b in range(size):
for c in range(size):
abc = (a, b, c)
if (
cliphack
and input_encoding in ("t", "T")
and output_encoding == "n"
and (
min(abc) < level_16
or max(abc) > math.ceil(level_235)
)
):
# TV levels in, no value from lookup, full
# range out
continue
seenkey = seenkeys[abc]
if seenkey in seen and seen[seenkey] is not True:
RGB = seen[seenkey]
else:
if input_encoding in ("t", "T"):
cond = min(abc) <= level_16 and min(abc) == max(abc)
else:
cond = max(abc) == 0
if cond:
# Black hack
self.log(
"Black hack - forcing output RGB {:0.0f} {:0.0f} {:0.0f}".format(
*[
cLUT65_to_VidRGB(v / maxind) * 255
for v in abc
]
)
)
RGB = [0] * 3
else:
RGB = RGB_dst_out[n]
seen[seenkey] = RGB
n += 1
clut[abc] = RGB
del seen
del seenkeys
del RGB_dst_out
preserve_sync = getcfg("3dlut.preserve_sync")
prevperc = 0
for a in range(size):
for b in range(size):
A2B0.clut.append([])
for c in range(size):
abc = (a, b, c)
if (
cliphack
and input_encoding in ("t", "T")
and output_encoding == "n"
and (
min(abc) < level_16
or max(abc) > math.ceil(level_235)
)
):
# TV levels in, no value from lookup, full
# range out
key = tuple(
min(
max(v, math.ceil(level_16)),
math.ceil(level_235),
)
for v in abc
)
RGB = clut[key]
else:
# Got value from lookup
RGB = clut[abc]
del clut[abc]
if output_encoding == "t":
# TV levels out
# Convert 0..255 to 16..235
RGB = [
colormath.convert_range(
v, 0, 1, 16.0 / 255, 235.0 / 255
)
for v in RGB
]
if input_encoding in ("t", "T"):
# TV levels in, scale/extrapolate clip
clipmask, scale, full, uci, cin = clip[abc]
del clip[abc]
for j, v in enumerate(RGB):
if clipmask:
if input_encoding != "T" and scale > 1:
# We got +ve clipping
# Re-scale all non-black values
if v > 16.0 / 255.0:
v = (
v - 16.0 / 255.0
) * scale + 16.0 / 255.0
# Deal with -ve clipping and sync
if clipmask & (1 << j):
if full[j] == 0.0:
# Only extrapolate in black direction
ifull = (
1.0 - full[j]
) # Opposite limit to full
# Do simple extrapolation (Not perfect though)
v = ifull + (v - ifull) * (
uci[j] - ifull
) / (cin[j] - ifull)
# Clip or pass sync through
if (
v < 0.0
or v > 1.0
or (
preserve_sync
and abs(uci[j] - full[j])
< 1e-6
)
):
v = full[j]
RGB[j] = v
RGB = [min(max(v, 0), 1) * 65535 for v in RGB]
A2B0.clut[-1].append(RGB)
perc = round(len(A2B0.clut) / float(size**2) * 100)
if perc > prevperc:
logfiles.write(f"\r{perc:0.0f}%")
prevperc = perc
logfiles.write("\n")
profile_link.write(link_filename)
self.log("Finished creating device link in", time() - xts, "seconds")
result = True
del clip
del clut
del A2B0
else:
# Create device link (and 3D LUT, if applicable) using collink
is_argyll_lut_format = (
self.argyll_version >= [1, 6]
and (
(
(
format == "eeColor"
or (format == "cube" and collink_version >= [1, 7])
)
and not test
)
or format == "madVR"
)
or format == "icc"
)
result = self.exec_cmd(
collink,
args + [profile_in_basename, profile_out_basename, link_filename],
capture_output=True,
skip_scripts=True,
)
if result and not isinstance(result, Exception):
self.log("Finished creating 3D LUT in", time() - xts, "seconds")
if (
result
and not isinstance(result, Exception)
and save_link_icc
and os.path.isfile(link_filename)
):
profile_link = ICCProfile(link_filename)
profile_link.setDescription(name)
profile_link.setCopyright(getcfg("copyright"))
if manufacturer:
profile_link.setDeviceManufacturerDescription(manufacturer)
if model:
profile_link.setDeviceModelDescription(model)
profile_link.device["manufacturer"] = device_manufacturer
profile_link.device["model"] = device_model
if mmod:
profile_link.tags.mmod = mmod
profile_link.tags.meta = DictType()
profile_link.tags.meta.update(
[
("CMF_product", appname),
("CMF_binary", appname),
("CMF_version", version),
(
"collink.args",
sp.list2cmdline(
args
+ [
profile_in_basename,
profile_out_basename,
link_basename,
]
),
),
("collink.version", collink_version_string),
("encoding.input", input_encoding),
("encoding.output", output_encoding),
]
)
profile_link.calculateID()
profile_link.write(f"{filename}{profile_ext}")
profile_link.tags.A2B0.clut_writepng(f"{filename}.A2B0.CLUT.png")
del profile_link
if use_xicclu and format == "madVR":
# We need to up-interpolate the device link ourself
# Call this in a separate process so I/O congestion
# doesn't interfere with UI updates and audio
# madvr.icc_device_link_to_madvr(link_filename,
# rgb_space=profile_in.get_rgb_space(),
# hdr=smpte2084 + hdr_display,
# logfile=logfiles,
# convert_video_rgb_to_clut65=True)
interp_args = []
if os.path.basename(exe).lower().startswith("python"):
if os.path.basename(exe).lower().startswith("pythonw"):
cmd = os.path.join(
exedir, "python" + os.path.basename(exe)[7:]
)
else:
cmd = exe
py = os.path.normpath(
os.path.join(
pydir, "..", f"{appname}-eeColor-to-madVR-converter.py"
)
)
if os.path.exists(py):
# Running from source or 0install
# If parent is running from 0install, the child
# will inherit the correct environment (PYTHONPATH)
# so no need to run child through 0install
# Need to run unbuffered so we can grab
# progress
interp_args.append("-u")
interp_args.append(py)
else:
# Regular install
# Need to run unbuffered so we can grab
# progress
interp_args.append("-u")
if sys.platform in ("win32", "darwin"):
interp_args.append(
get_data_path(
os.path.join(
"scripts",
f"{appname.lower()}-eecolor-to-madvr-converter",
)
)
)
else:
# Linux
interp_args.append(
which(
f"{appname.lower()}-eecolor-to-madvr-converter"
)
)
elif isapp:
cmd = os.path.join(exedir, "python")
interp_args.extend(
[
"-c",
"import sys;sys.path.insert(0, {});execfile({})".format(
os.path.join(
pydir.encode(fs_enc),
"lib",
"python{}{}".format(*sys.version_info[:2]),
"site-packages.zip",
),
os.path.join(
pydir.encode(fs_enc),
f"{appname.lower()}-eecolor-to-madvr-converter",
),
),
]
)
else:
cmd = os.path.join(
pydir, f"{appname}-eeColor-to-madVR-converter{exe_ext}"
)
if in_colors:
interp_args.append(
"--colorspace={}".format(
",".join([str(v) for v in in_colors])
)
)
interp_args.append("--hdr={:0.0f}".format(smpte2084 + hdr_display))
interp_args.append("--convert-video-rgb-to-clut65")
interp_args.append("--append-linear-cal")
interp_args.append("--batch")
interp_args.append(link_filename)
result = self.exec_cmd(
cmd,
interp_args,
capture_output=True,
skip_scripts=True,
use_pty=True,
)
if debug:
h3d = madvr.H3DLUT(os.path.join(cwd, name + ".3dlut"))
h3d.write_devicelink(filename + ".3dlut" + profile_ext)
if result and not isinstance(result, Exception):
if format == "madVR" and is_argyll_lut_format:
# We need to update Input_Primaries, otherwise the
# madVR 3D LUT won't work correctly! (collink fills
# Input_Primaries from a lookup through the input
# profile, which won't work correctly if the input
# profile is cLUT-based. Also, we want to use a D65
# white as madVR can only deal correctly with D65
h3d = madvr.H3DLUT(os.path.join(cwd, name + ".3dlut"))
input_primaries = h3d.parametersData.get("Input_Primaries")
if input_primaries:
if in_colors:
h3d.parametersData["Input_Primaries"] = in_colors
if smpte2084:
h3d.parametersData["Input_Transfer_Function"] = "PQ"
if hdr_display:
h3d.parametersData["Output_Transfer_Function"] = "PQ"
h3d.write()
else:
raise Error("madVR 3D LUT doesn't contain Input_Primaries")
if hdr or not use_collink_bt1886 or XYZwp:
if not hdr and XYZwp != profile_in_wtpt_XYZ:
# Update profile description
profile_in.setDescription(
"{} {}x {}y".format(
profile_in.getDescription(),
*[
stripzeros(f"{v:.4f}")
for v in colormath.XYZ2xyY(*XYZwp)[:2]
],
)
)
# Save source profile
in_name, in_ext = os.path.splitext(profile_in_basename)
profile_in.fileName = os.path.join(
os.path.dirname(path),
self.lut3d_get_filename(profile_in_basename, False, False)
+ in_ext,
)
profile_in.write()
if isinstance(profile_in.tags.get("A2B0"), LUT16Type):
# Write diagnostic PNG
profile_in.tags.A2B0.clut_writepng(
"{}.A2B0.CLUT.png".format(
os.path.splitext(profile_in.fileName)[0]
)
)
if isinstance(profile_in.tags.get("DBG0"), LUT16Type):
# HDR RGB
profile_in.tags.DBG0.clut_writepng(
"{}.DBG0.CLUT.png".format(
os.path.splitext(profile_in.fileName)[0]
)
)
if isinstance(profile_in.tags.get("DBG1"), LUT16Type):
# Display RGB
profile_in.tags.DBG1.clut_writepng(
"{}.DBG1.CLUT.png".format(
os.path.splitext(profile_in.fileName)[0]
)
)
if isinstance(profile_in.tags.get("DBG2"), LUT16Type):
# Display XYZ
profile_in.tags.DBG2.clut_writepng(
"{}.DBG2.CLUT.png".format(
os.path.splitext(profile_in.fileName)[0]
)
)
if is_argyll_lut_format or (use_xicclu and format == "madVR"):
# Collink has already written the 3DLUT for us,
# or we have written the madVR 3D LUT
if format == "cube":
if maxval is None:
maxval = 1.0
# Strip any leading whitespace from each line (although
# leading/trailing spaces are allowed according to the spec).
# Also, Argyll does not write (optional) DOMAIN_MIN/MAX
# keywords. Add them after the fact.
cube_filename = os.path.join(cwd, f"{name}.cube")
if os.path.isfile(cube_filename):
add_domain = True
cube_data = []
with open(cube_filename, "rb") as cube_file:
for line in cube_file:
# Strip any leading whitespace
line = line.lstrip()
if line.startswith(b"DOMAIN_"):
# Account for the possibility that a
# future Argyll version might write
# DOMAIN_MIN/MAX keywords.
add_domain = False
elif line.startswith(b"0.") and add_domain:
# 1st cube data entry marks end of keywords.
# Add DOMAIN_MIN/MAX keywords
cube_data.append(b"DOMAIN_MIN 0.0 0.0 0.0\n")
fp_offset = str(maxval).find(".")
domain_max = b"DOMAIN_MAX %s %s %s\n" % (
(b"%%.%if" % len(str(maxval)[fp_offset + 1 :]),)
* 3
)
cube_data.append(domain_max % ((maxval,) * 3))
cube_data.append(b"\n")
add_domain = False
cube_data.append(line)
# Write updated cube
with open(cube_filename, "wb") as cube_file:
cube_file.write(b"".join(cube_data))
result2 = self.wrapup(
not isinstance(result, UnloggedInfo) and result,
dst_path=path,
ext_filter=[".3dlut", ".cube", ".log", ".png", ".txt", ".wrl"],
)
if not result:
result = UnloggedError(lang.getstr("aborted"))
if isinstance(result2, Exception):
if isinstance(result, Exception):
result = Error(f"{result}\n\n{result2}")
else:
result = result2
if not isinstance(result, Exception):
return
if isinstance(result, Exception):
raise result
elif not result:
raise UnloggedError(lang.getstr("aborted"))
# We have to create the 3DLUT ourselves
logfiles.write(f"Generating {format} 3D LUT...\n")
# Create input RGB values
RGB_oin = []
RGB_in = []
RGB_indexes = []
seen = {}
if format == "eeColor":
# Fixed size
size = 65
elif format == "ReShade":
format = "png"
if format == "3dl":
if maxval is None:
maxval = 1023
if output_bits is None:
output_bits = math.log(maxval + 1) / math.log(2)
if input_bits is None:
input_bits = output_bits
# Note: We only round up for the input values, output values
# are rounded to nearest integer
quantizer = lambda v: int(math.ceil(v * (2**input_bits - 1)))
scale = quantizer(1.0)
else:
quantizer = lambda v: v
scale = 1.0
step = 1.0 / (size - 1)
RGB_triplet = [0.0, 0.0, 0.0]
RGB_index = [0, 0, 0]
# Set the fastest and slowest changing columns, from right to left
if format in ("3dl", "mga", "spi3d") or (
format == "png" and getcfg("3dlut.image.order") == "bgr"
):
columns = (0, 1, 2)
elif format == "eeColor":
columns = (2, 0, 1)
else:
columns = (2, 1, 0)
for i in range(0, size):
# Red
if format == "eeColor" and not eecolor65 and i == size - 1:
# Last cLUT entry is fixed to 1.0 for eeColor and unchangeable
continue
RGB_triplet[columns[0]] = quantizer(step * i)
RGB_index[columns[0]] = i
for j in range(0, size):
# Green
if format == "eeColor" and not eecolor65 and j == size - 1:
# Last cLUT entry is fixed to 1.0 for eeColor and unchangeable
continue
RGB_triplet[columns[1]] = quantizer(step * j)
RGB_index[columns[1]] = j
for k in range(0, size):
# Blue
if self.thread_abort:
raise Info(lang.getstr("aborted"))
if format == "eeColor" and not eecolor65 and k == size - 1:
# Last cLUT entry is fixed to 1.0 for eeColor and unchangeable
continue
RGB_triplet[columns[2]] = quantizer(step * k)
RGB_oin.append(list(RGB_triplet))
RGB_copy = list(RGB_triplet)
if format == "eeColor":
for l in range(3):
RGB_copy[l] = eeColor_to_VidRGB(RGB_copy[l])
if input_encoding in ("t", "T"):
RGB_copy[l] = VidRGB_to_cLUT65(RGB_copy[l])
RGB_index[columns[2]] = k
RGB_in.append(RGB_copy)
RGB_indexes.append(list(RGB_index))
# Lookup RGB -> RGB values through devicelink profile using icclu
# (Using icclu instead of xicclu because xicclu in versions
# prior to Argyll CMS 1.6.0 could not deal with devicelink profiles)
RGB_out = self.xicclu(
link_filename, RGB_in, scale=scale, use_icclu=True, logfile=logfiles
)
if format == "eeColor" and output_encoding == "n":
RGBw = self.xicclu(link_filename, [[1, 1, 1]], use_icclu=True)[0]
# Remove temporary files, move log file
result2 = self.wrapup(dst_path=path, ext_filter=[".log"])
if isinstance(result, Exception):
raise result
valsep = " "
if format not in ("dcl", "png"):
lut = [[f"# Created with {appname} {version}"]]
linesep = "\n"
if format in ("3dl", "dcl"):
maxval = math.pow(2, output_bits) - 1
if format == "3dl":
lut.append([f"# INPUT RANGE: {input_bits:d}"])
lut.append([f"# OUTPUT RANGE: {output_bits:d}"])
lut.append([])
for i in range(0, size):
lut[-1].append("{:d}".format(quantizer(i * step)))
else:
# dcl
lut = [["# DeviceControl-LG 3D"]]
linesep = "\r\n"
for RGB_triplet in RGB_out:
lut.append([])
for component in (0, 1, 2):
lut[-1].append(
"{:d}".format(
int(round(RGB_triplet[component] / scale * maxval))
)
)
elif format == "cube":
if maxval is None:
maxval = 1.0
lut.append([f"LUT_3D_SIZE {size:d}"])
lut.append(["DOMAIN_MIN 0.0 0.0 0.0"])
fp_offset = str(maxval).find(".")
domain_max = "DOMAIN_MAX {} {} {}".format(
("{{:.{:d}f}}".format(len(str(maxval)[fp_offset + 1 :])),) * 3
)
lut.append([domain_max.format((maxval,) * 3)])
lut.append([])
for RGB_triplet in RGB_out:
lut.append([])
for component in (0, 1, 2):
lut[-1].append("{:.6f}".format(RGB_triplet[component] * maxval))
elif format == "spi3d":
if maxval is None:
maxval = 1.0
lut = [["SPILUT 1.0"]]
lut.append(["3 3"])
lut.append(["{:d} {:d} {:d}".format(*([size] * 3))])
for i, RGB_triplet in enumerate(RGB_out):
lut.append([str(index) for index in RGB_indexes[i]])
for component in (0, 1, 2):
lut[-1].append("{:.6f}".format(RGB_triplet[component] * maxval))
elif format == "eeColor":
if maxval is None:
maxval = 1.0
lut = []
for i, RGB_triplet in enumerate(RGB_out):
lut.append(
["{:.6f}".format(component * maxval) for component in RGB_oin[i]]
)
for component in (0, 1, 2):
v = RGB_triplet[component] * maxval
if output_encoding == "n":
# For eeColor and full range RGB, make sure that the cLUT
# output maps to 1.0
# The output curve will correct this
v /= RGBw[component]
v = min(v, 1)
v = VidRGB_to_eeColor(v)
lut[-1].append(f"{v:.6f}")
linesep = "\r\n"
elif format == "mga":
lut = [
["#HEADER"],
["#filename: {}".format(os.path.basename(path))],
["#type: 3D cube file"],
["#format: 1.00"],
["#created: {}".format(strftime("%d %B %Y"))],
[f"#owner: {getpass.getuser()}"],
["#title: {}".format(os.path.splitext(os.path.basename(path))[0])],
["#END"],
]
lut.append([])
lut.append(["channel 3d"])
lut.append(["in {:d}".format(size**3)])
maxval = 2**output_bits - 1
lut.append(["out {:d}".format(maxval + 1)])
lut.append([""])
lut.append(["format lut"])
lut.append([""])
lut.append(["values\tred\tgreen\tblue"])
for i, RGB_triplet in enumerate(RGB_out):
lut.append([f"{i:d}"])
for component in (0, 1, 2):
lut[-1].append(
("{}".format(int(round(RGB_triplet[component] * maxval))))
)
valsep = "\t"
elif format == "png":
lut = [[]]
if output_bits > 8:
# PNG only supports 8 and 16 bit
output_bits = 16
maxval = 2**output_bits - 1
for RGB_triplet in RGB_out:
if len(lut[-1]) == size:
# Append new scanline
lut.append([])
lut[-1].append([int(round(v * maxval)) for v in RGB_triplet])
# Current layout is vertical
if getcfg("3dlut.image.layout") == "h":
# Change layout to horizontal
lutv = lut
lut = [[]]
for i in range(size):
if len(lut[-1]) == size**2:
# Append new scanline
lut.append([])
for j in range(size):
lut[-1].extend(lutv[i + size * j])
if format != "png":
lut.append([])
for i, line in enumerate(lut):
lut[i] = valsep.join(line)
result = linesep.join(lut)
# Write 3DLUT
lut_file = open(path, "wb")
if format != "png":
lut_file.write(result.encode())
else:
im = imfile.Image(lut, output_bits)
im.write(lut_file)
lut_file.close()
if format == "eeColor":
# Write eeColor 1D LUTs
for i, color in enumerate(["red", "green", "blue"]):
for count, inout in [(1024, "first"), (8192, "second")]:
with open(f"{filename}-{inout}1d{color}.txt", "wb") as lut1d:
for j in range(count):
v = j / (count - 1.0)
if inout == "second" and output_encoding == "n":
# For eeColor and Full range RGB, unmap the
# cLUT output maps from 1.0
v *= RGBw[i]
n_v = f"{v:.6f}\n"
lut1d.write(n_v.encode())
if isinstance(result2, Exception):
raise result2
def enumerate_displays_and_ports(
self,
silent=False,
check_lut_access=True,
enumerate_ports=True,
include_network_devices=True,
):
"""Enumerate the available displays and ports.
Also sets Argyll version number, availability of certain options
like black point rate, and checks LUT access for each display.
"""
if (silent and check_argyll_bin()) or (not silent and check_set_argyll_bin()):
displays = []
xrandr_names = {}
lut_access = []
if verbose >= 1:
print(lang.getstr("enumerating_displays_and_comports"))
instruments = []
current_display_name = config.get_display_name()
cfg_instruments = getcfg("instruments")
current_instrument = config.get_instrument_name()
if enumerate_ports:
cmd = get_argyll_util("dispcal")
else:
cmd = get_argyll_util("dispwin")
for instrument in cfg_instruments:
# Names are canonical from 1.1.4.7 onwards, but we may have
# verbose names from an old configuration
instrument = get_canonical_instrument_name(instrument)
if instrument.strip():
instruments.append(instrument)
args = []
if include_network_devices:
args.append("-dcc:?")
args.append("-?")
argyll_bin_dir = os.path.dirname(cmd)
if argyll_bin_dir != self.argyll_bin_dir:
self.argyll_bin_dir = argyll_bin_dir
print(self.argyll_bin_dir)
result = self.exec_cmd(
cmd,
args,
capture_output=True,
skip_scripts=True,
silent=True,
log_output=False,
)
if isinstance(result, Exception):
print(result)
arg = None
defaults["calibration.black_point_hack"] = 0
defaults["calibration.black_point_rate.enabled"] = 0
defaults["patterngenerator.prisma.argyll"] = 0
n = -1
self.display_rects = []
non_standard_display_args = ("-dweb[:port]", "-dmadvr")
if test:
# Add dummy Chromecasts
self.output.append(" -dcc[:n]")
self.output.append(" 100 = '\xd4\xc7\xf3 Test A'")
self.output.append(" 101 = '\xd4\xc7\xf3 Test B'")
argyll_version_string = None
self.reset_argyll_enum()
for line in self.output:
if isinstance(line, str):
n += 1
line = line.strip()
if argyll_version_string is None and "version" in line.lower():
argyll_version_string = line[line.lower().find("version") + 8 :]
if argyll_version_string != self.argyll_version_string:
self.set_argyll_version_from_string(argyll_version_string)
print(f"ArgyllCMS {self.argyll_version_string}")
defaults["copyright"] = (
f"No copyright. Created with {appname} {version} and "
f"Argyll CMS {argyll_version_string}"
)
if self.argyll_version > [1, 0, 4]:
# Rate of blending from neutral to black point.
defaults["calibration.black_point_rate.enabled"] = 1
if (
self.argyll_version >= [1, 7]
and "Beta" not in self.argyll_version_string
):
# Forced black point hack available
# (Argyll CMS 1.7)
defaults["calibration.black_point_hack"] = 1
if self.argyll_version >= [1, 9, 4]:
# Add CIE 2012 observers
valid_observers = natsort(observers + ["2012_2", "2012_10"])
else:
valid_observers = observers
for key in [
"{}",
"colorimeter_correction.{}",
"colorimeter_correction.{}.reference",
]:
key = key.format("observer")
config.valid_values[key] = valid_observers
continue
line = line.split(None, 1)
if len(line) and line[0][0] == "-":
arg = line[0]
value = line[-1].split(None, 1)[0]
if arg == "-d" and not value.startswith("n"):
# Argyll 2.0.2 started listing -d madvr and -d dummy
# instead of -dmadvr and -ddummy
# Use the non-space-delimited as the canonical form
arg += value
if arg == "-A":
# Rate of blending from neutral to black point.
defaults["calibration.black_point_rate.enabled"] = 1
elif arg in non_standard_display_args:
displays.append(arg)
elif arg == "-b" and not line[-1].startswith("bright"):
# Forced black point hack available
# (Argyll CMS 1.7b 2014-12-22)
defaults["calibration.black_point_hack"] = 1
elif arg == "-dprisma[:host]":
defaults["patterngenerator.prisma.argyll"] = 1
elif arg in ("-dvirtual", "-ddummy"):
# Custom modified Argyll V2.0.2 (-dvirtual)
# or Argyll >= 2.0.2 (-d dummy)
self.argyll_virtual_display = arg[2:]
print("Argyll has virtual display support")
elif len(line) > 1 and line[1][0] == "=":
value = line[1].strip(" ='")
if arg == "-d":
# Standard displays
match = re.findall(
r"(.+?),? at (-?\d+), (-?\d+), width (\d+), height (\d+)",
value,
)
if len(match):
xrandr_name = re.search(r", Output (.+)", match[0][0])
if xrandr_name:
xrandr_names[len(displays)] = xrandr_name.group(1)
display = "{} @ {}, {}, {}x{}".format(*match[0])
if " ".join(value.split()[-2:]) == "(Primary Display)":
display += " [PRIMARY]"
displays.append(display)
self.display_rects.append(
wx.Rect(*[int(item) for item in match[0][1:]])
)
elif arg == "-dcc[:n]":
# Chromecast
if value:
# Note the Chromecast name may be mangled due
# to being UTF-8 encoded, but commandline output
# will always be decoded to Unicode using the
# stdout encoding, which may not be UTF-8
# (e.g. under Windows). We can recover characters
# valid in both UTF-8 and stdout encoding
# by a re-encode/decode step
displays.append(
"Chromecast {}: {}".format(
line[0], safe_str(value, enc)
)
)
elif arg == "-dprisma[:host]":
# Prisma (via Argyll CMS)
# <serial no>: <name> @ <ip>
# 141550000000: prisma-0000 @ 172.31.31.162
match = re.findall(r".+?: (.+) @ (.+)", value)
if len(match):
displays.append(
"Prisma {}: {} @ {}".format(
line[0],
safe_str(match[0][0], enc),
match[0][1],
)
)
elif arg == "-c" and enumerate_ports:
if (
re.match(r"/dev(?:/[\w.\-]+)*$", value)
or re.match(r"COM\d+$", value)
) and getcfg("skip_legacy_serial_ports"):
# Skip all legacy serial ports (this means we
# deliberately don't support DTP92 and
# Spectrolino, although they may work when
# used with a serial to USB adaptor)
continue
value = value.split(None, 1)
if len(value) > 1:
value = value[1].split("'", 1)[0].strip("()")
else:
value = value[0]
value = get_canonical_instrument_name(value)
instruments.append(value)
if test:
inames = list(all_instruments.keys())
inames.sort()
for iname in inames:
iname = get_canonical_instrument_name(iname)
if iname not in instruments:
instruments.append(iname)
if verbose >= 1:
print(lang.getstr("success"))
if instruments != self.instruments:
self.instruments = instruments
setcfg("instruments", instruments)
if (
current_instrument != self.get_instrument_name()
and current_instrument in instruments
):
setcfg("comport.number", instruments.index(current_instrument) + 1)
if displays != self._displays:
self._displays = list(displays)
setcfg("displays", displays)
if RDSMM:
# Sync with Argyll - needed under Linux to map EDID
RDSMM.enumerate_displays()
displays = [
display
for display in displays
if display not in non_standard_display_args
]
self.display_edid = []
self.display_manufacturers = []
self.display_names = []
if sys.platform == "win32":
# The ordering will work as long
# as Argyll continues using
# EnumDisplayMonitors
try:
monitors = util_win.get_real_display_devices_info()
except Exception as exception:
if not isinstance(exception, pywintypes.error):
print(traceback.format_exc())
monitors = []
for i, display in enumerate(displays):
if display.startswith("Chromecast ") or display.startswith(
"Prisma "
):
self.display_edid.append({})
if display.startswith("Prisma "):
display_manufacturer = "Q, Inc"
else:
display_manufacturer = "Google"
self.display_manufacturers.append(display_manufacturer)
self.display_names.append(display.split(":", 1)[1].strip())
continue
display_name = split_display_name(display)
# Make sure we have nice descriptions
desc = []
if sys.platform == "win32" and i < len(monitors):
# Get monitor description using win32api
device = util_win.get_active_display_device(
monitors[i]["Device"]
)
if device:
desc.append(device.DeviceString)
# Deal with HiDPI - update monitor rect
m_left, m_top, m_right, m_bottom = monitors[i]["Monitor"]
m_width = m_right - m_left
m_height = m_bottom - m_top
self.display_rects[i] = wx.Rect(
m_left, m_top, m_width, m_height
)
is_primary = " [PRIMARY]" in display
display = " @ ".join(
[
display_name,
"{:d}, {:d}, {:d}x{:d}".format(
m_left, m_top, m_width, m_height
),
]
)
if is_primary:
display += " [PRIMARY]"
# Get monitor descriptions from EDID
try:
# Important: display_name must be given for get_edid
# under Mac OS X, but it doesn't hurt to always
# include it
edid = get_edid(i, display_name)
except Exception as exception:
suppress_errors = (SystemError, TypeError, ValueError, WMIError)
if sys.platform == "win32":
suppress_errors += (pywintypes.error,)
if isinstance(exception, EnvironmentError):
print(exception)
elif not isinstance(exception, suppress_errors):
print(traceback.format_exc())
edid = {}
self.display_edid.append(edid)
if edid:
manufacturer = edid.get("manufacturer", "").split()
monitor = edid.get(
"monitor_name",
edid.get("ascii", str(edid["product_id"] or "")),
)
if (
monitor in ("Color LCD", "iMac")
and edid["manufacturer_id"] == "APP"
and sys.platform == "darwin"
):
# Get mac model if internal display
model_id = get_model_id()
if model_id:
# Override monitor name
monitor = model_id
# Override EDID
edid["monitor_name"] = monitor
if monitor and monitor not in "".join(desc):
desc = [monitor]
else:
manufacturer = []
if desc and desc[-1] not in display:
# Only replace the description if it not already
# contains the monitor model
display = " @".join([" ".join(desc), display.split("@")[-1]])
displays[i] = display
self.display_manufacturers.append(" ".join(manufacturer))
self.display_names.append(split_display_name(display))
if self.argyll_version >= [1, 4, 0]:
displays.append("Web @ localhost")
self.display_edid.append({})
self.display_manufacturers.append("")
self.display_names.append("Web")
if self.argyll_version >= [1, 6, 0]:
displays.append("madVR")
self.display_edid.append({})
self.display_manufacturers.append("")
self.display_names.append("madVR")
# Prisma (via DisplayCAL)
displays.append("Prisma")
self.display_edid.append({})
self.display_manufacturers.append("Q, Inc")
self.display_names.append("Prisma")
# Resolve
displays.append("Resolve")
self.display_edid.append({})
self.display_manufacturers.append("DaVinci")
self.display_names.append("Resolve")
# Untethered
displays.append("Untethered")
self.display_edid.append({})
self.display_manufacturers.append("")
self.display_names.append("Untethered")
#
self.displays = displays
setcfg("displays", displays)
if (
current_display_name != config.get_display_name()
and current_display_name in self.display_names
):
setcfg(
"display.number",
self.display_names.index(current_display_name) + 1,
)
# Filter out Prisma (via DisplayCAL), Resolve and Untethered
# IMPORTANT: Also make changes to display filtering in
# worker.Worker.has_separate_lut_access
displays = displays[:-3]
if self.argyll_version >= [1, 6, 0]:
# Filter out madVR
displays = displays[:-1]
if self.argyll_version >= [1, 4, 0]:
# Filter out Web @ localhost
displays = displays[:-1]
if check_lut_access:
dispwin = get_argyll_util("dispwin")
test_cal = get_data_path("test.cal")
if not test_cal:
print(lang.getstr("file.missing", "test.cal"))
tmp = self.create_tempdir()
if isinstance(tmp, Exception):
print(tmp)
tmp = None
for i, disp in enumerate(displays):
if (
disp.startswith("Chromecast ")
or disp.startswith("Prisma ")
or not test_cal
):
lut_access.append(None)
continue
if sys.platform == "darwin":
# There's no easy way to check LUT access under
# Mac OS X because loading a LUT isn't persistent
# unless you run as root. Just assume we have
# access to all displays.
lut_access.append(True)
continue
if verbose >= 1:
print(lang.getstr("checking_lut_access", (i + 1)))
# Save current calibration?
if tmp:
current_cal = os.path.join(tmp, "current.cal")
tmp_cal = current_cal
result = self.save_current_video_lut(
i + 1, current_cal, silent=True
)
# Load test.cal
result = self.exec_cmd(
dispwin,
[f"-d{i + 1}", "-c", test_cal],
capture_output=True,
skip_scripts=True,
silent=True,
)
if isinstance(result, Exception):
print(result)
elif result is None:
lut_access.append(None)
continue
# Check if LUT == test.cal
result = self.exec_cmd(
dispwin,
[f"-d{(i + 1)}", "-V", test_cal],
capture_output=True,
skip_scripts=True,
silent=True,
)
if isinstance(result, Exception):
print(result)
elif result is None:
lut_access.append(None)
continue
retcode = -1
for line in self.output:
if line.find("IS loaded") >= 0:
retcode = 0
break
# Reset LUT & (re-)load previous cal (if any)
if not tmp or not os.path.isfile(current_cal):
current_cal = self.get_dispwin_display_profile_argument(i)
result = self.exec_cmd(
dispwin,
[f"-d{i + 1}", "-c", current_cal],
capture_output=True,
skip_scripts=True,
silent=True,
)
if isinstance(result, Exception):
print(result)
if tmp and os.path.isfile(tmp_cal):
try:
os.remove(tmp_cal)
except EnvironmentError as exception:
print(exception)
lut_access.append(retcode == 0)
if verbose >= 1:
if retcode == 0:
print(lang.getstr("success"))
else:
print(lang.getstr("failure"))
else:
lut_access.extend([None] * len(displays))
if self.argyll_version >= [1, 4, 0]:
# Web @ localhost
lut_access.append(False)
if self.argyll_version >= [1, 6, 0]:
# madVR
lut_access.append(True)
# Prisma (via DisplayCAL)
lut_access.append(False)
# Resolve
lut_access.append(False)
# Untethered
lut_access.append(False)
self.lut_access = lut_access
elif silent or not check_argyll_bin():
self.clear_argyll_info()
def exec_cmd(
self,
cmd,
args=None,
capture_output=False,
display_output=False,
low_contrast=True,
skip_scripts=False,
silent=False,
parent=None,
asroot=False,
log_output=True,
title=appname,
shell=False,
working_dir=None,
dry_run=None,
sessionlogfile=None,
use_pty=False,
):
"""Execute a command.
Return value is either True (succeed), False (failed), None (canceled)
or an exception.
Args:
cmd (str): the full path of the command.
args (List[str]): The arguments, if any.
capture_output (bool): If True, swallows any output from the
command and sets the 'output' and 'errors' properties of the
Worker instance.
display_output (bool): Shows the log after the command.
low_contrast (bool): If True sets low contrast shell colors while
the command is run.
skip_scripts (bool): If True, skips the creation of shell scripts
that allow re-running the command. Note that this is also
controlled by a global config option and scripts will only be
created if it evaluates to False.
silent (bool): If True, skips most output and also most error
dialogs (except unexpected failures).
parent (): Sets the parent window for auth dialog (if asroot is
True).
asroot (bool): If True, on Linux runs the command using sudo.
log_output (bool): If True, logs any output if capture_output is
also set.
title (str): Title for auth dialog (if asroot is True).
working_dir (str): Working directory. If None, will be determined
from absulte path of last argument and last argument will be
set to only the basename. If False, no working dir will be used
and file arguments not changed.
"""
if args is None:
args = []
# convert all args to str if any are bytes
for i, arg in enumerate(args):
if isinstance(arg, bytes):
args[i] = arg.decode("utf-8")
# If dry_run is explicitly set to False, ignore dry_run config value
dry_run = dry_run is not False and (dry_run or getcfg("dry_run"))
if not capture_output:
capture_output = False
if sys.stdout and hasattr(sys.stdout, "isatty") and not sys.stdout.isatty():
capture_output = True
self.clear_cmd_output()
if None in [cmd, args]:
if verbose >= 1 and not silent:
print(lang.getstr("aborted"))
return False
self.cmd = cmd
cmdname = os.path.splitext(os.path.basename(cmd))[0]
self.cmdname = cmdname
if "-?" not in args and cmdname == get_argyll_utilname("dispwin"):
if "-I" in args or "-U" in args:
if "-Sl" in args or "-Sn" in args:
# Root is required if installing a profile to a system
# location
asroot = True
elif "-s" not in args and not self.calibration_loading_generally_supported:
# Loading/clearing calibration not supported
# Don't actually do it, pretend we were successful
if "-V" in args:
self.output.append("IS loaded")
self.retcode = 0
return True
if asroot:
silent = False
measure_cmds = (
get_argyll_utilname("dispcal"),
get_argyll_utilname("dispread"),
get_argyll_utilname("spotread"),
)
process_cmds = (
get_argyll_utilname("collink"),
get_argyll_utilname("colprof"),
get_argyll_utilname("targen"),
)
# Run commands through wexpect.spawn instead of subprocess.Popen if
# any of these conditions apply
use_pty = (
args
and "-?" not in args
and (use_pty or cmdname in measure_cmds + process_cmds)
)
self.measure_cmd = "-?" not in args and cmdname in measure_cmds
report_current_cal = cmdname == get_argyll_utilname("dispcal") and (
"-r" in args or "-z" in args
)
if self.measure_cmd and not dry_run and sys.platform not in ("darwin", "win32"):
# Inhibit session to prevent screensaver/powersaving etc.
# Useful commands to figure out list of DBus interfaces:
# dbus-send [--session|--system] \
# --dest=org.freedesktop.DBus \
# --type=method_call \
# --print-reply \
# /org/freedesktop/DBus \
# org.freedesktop.DBus.ListNames
if dbus_session:
desktop = os.getenv("XDG_CURRENT_DESKTOP", "").split(":")
inhibit_reason = "Display device measurements"
gnome_sm = "org.gnome.SessionManager"
gnome_sm_xid = 0
# GNOME SessionManager.Inhibit flags:
# 1 Inhibit logging out
# 2 Inhibit user switching
# 4 Inhibit suspending
# 8 Inhibit idle
gnome_sm_flags = 1 | 2 | 4 | 8
ifaces = getattr(self, "dbus_ifaces", None)
if not ifaces:
ifaces = dict(
[
(
gnome_sm,
{
"args": (
gnome_sm_xid,
inhibit_reason,
gnome_sm_flags,
),
"uninhibit": "uninhibit",
},
),
("org.freedesktop.ScreenSaver", {"precedence": [gnome_sm]}),
(
"org.freedesktop.PowerManagement.Inhibit",
{"precedence": [gnome_sm]},
),
]
)
self.dbus_ifaces = ifaces
for bus_name in ifaces:
iface_dict = ifaces[bus_name]
if bus_name.startswith("org.freedesktop."):
skip = False
for precedence in iface_dict.get("precedence", []):
if ifaces[precedence].get("cookie"):
# Already took care of inhibit
skip = True
break
if skip:
continue
else:
iface_domain = bus_name.split(".")[1]
if iface_domain.upper() not in desktop:
# Not current desktop environment
continue
object_path = "/" + bus_name.replace(".", "/")
other_path = iface_dict.get("path")
if other_path:
if other_path.startswith("/"):
object_path = other_path
else:
object_path += "/" + other_path
if iface_dict.get("cookie"):
continue
try:
iface = iface_dict.get("iface")
if not iface:
iface = DBusObject(BUSTYPE_SESSION, bus_name, object_path)
cookie = iface.inhibit(
appname, *iface_dict.get("args", (inhibit_reason,))
)
except DBusException as exception:
# The user might be running a minimal Wayland environment
# without a screensaver or power management daemon
self.log(
f"{appname}: Warning - could not inhibit "
f"{bus_name}: {exception}"
)
continue # Skip to the next interface
iface_dict["iface"] = iface
iface_dict["cookie"] = cookie
self.log(f"{appname}: Inhibited {bus_name}")
else:
self.log(
f"{appname}: Warning - no D-Bus session bus - "
"cannot inhibit session, screensaver/powersaving may "
"still be active!"
)
profiling_inhibit = False
display_profile = None
if (
sys.platform not in ("darwin", "win32")
and self.measure_cmd
and self._use_patternwindow
and not dry_run
and not report_current_cal
):
# Preliminary Wayland support. This still needs a lot
# of work as Argyll doesn't support Wayland natively yet,
# so we use virtual display to drive our own patch window.
# We need to make sure videoLUT is linear. Only way to
# achieve this currently under Wayland is by using colord.
# Inhibit display device to reset videoLUT to linear and profile
# to none
device_id = self.get_device_id(query=True)
object_path = None
if device_id:
try:
object_path = colord.get_object_path(device_id, "device")
except colord.CDError as exception:
self.log(exception)
else:
self.log(
f"{appname}: Warning - couldn't get display device ID - "
"cannot inhibit display device!"
)
if dbus_system and object_path:
# Use DBus interface to call ProfilingInhibit()
try:
cd_device = colord.Device(object_path)
cd_device.profiling_inhibit()
except (colord.CDError, DBusException) as exception:
self.log(f"{appname}: Warning - Handled exception: {exception}")
else:
profiling_inhibit = True
self.log(f"{appname}: Inhibited display device", object_path)
elif object_path:
self.log(
f"{appname}: Warning - no D-Bus system bus - "
"cannot inhibit display device!"
)
if not profiling_inhibit:
# Fallback - install linear cal sRGB profile
self.log(f"{appname}: Temporarily installing sRGB profile...")
display_profile = config.get_display_profile()
self.srgb = srgb = ICCProfile.from_named_rgb_space("sRGB")
# Date should not change so the ID stays the same.
srgb.dateTime = datetime.datetime(2003, 0o1, 23, 0, 0, 0)
srgb.tags.vcgt = VideoCardGammaTableType("", "vcgt")
srgb.tags.vcgt.update(
{
"channels": 3,
"entryCount": 256,
"entrySize": 1,
"data": [
list(range(0, 256)),
list(range(0, 256)),
list(range(0, 256)),
],
}
)
srgb.setDescription(f"{appname} Linear Calibration sRGB Profile")
srgb.calculateID()
srgb.write(
os.path.join(
self.tempdir, f"{appname} Linear Calibration sRGB Profile.icc"
)
)
cdinstall = self._attempt_install_profile_colord(srgb)
if not cdinstall:
return Error(lang.getstr("calibration.reset_error"))
elif isinstance(cdinstall, Exception):
return UnloggedError(safe_str(cdinstall))
self.log(f"{appname}: Successfully assigned sRGB profile")
self.use_patterngenerator = (
self.measure_cmd
and cmdname != get_argyll_utilname("spotread")
and (
config.get_display_name() == "Resolve"
or (
config.get_display_name() == "Prisma"
and not defaults["patterngenerator.prisma.argyll"]
)
or self._use_patternwindow
)
)
use_3dlut_override = (
(
(
self.use_patterngenerator
and config.get_display_name(None, True) == "Prisma"
)
or (
config.get_display_name(None, True) == "madVR"
and (sys.platform != "win32" or not getcfg("madtpg.native"))
)
)
and cmdname == get_argyll_utilname("dispread")
and "-V" in args
)
if use_3dlut_override:
args.remove("-V")
working_basename = None
if args and args[-1].find(os.path.sep) > -1:
working_basename = os.path.basename(args[-1])
if cmdname not in (
get_argyll_utilname("dispcal"),
get_argyll_utilname("dispread"),
get_argyll_utilname("colprof"),
get_argyll_utilname("targen"),
get_argyll_utilname("txt2ti3"),
):
# Last arg is with extension
working_basename = os.path.splitext(working_basename)[0]
if working_dir is None:
working_dir = os.path.dirname(args[-1])
if working_dir is None:
if self.use_patterngenerator:
working_dir = self.create_tempdir()
else:
working_dir = self.tempdir
if working_dir and not os.path.isdir(working_dir):
working_dir = None
if working_dir and working_dir == self.tempdir:
# Get a list of files in the temp directory and their modification
# times so we can determine later if anything has changed and we
# should keep the files in case of errors
for filename in os.listdir(working_dir):
self.tmpfiles[filename] = os.stat(
os.path.join(working_dir, filename)
).st_mtime
if (
working_basename
and working_dir == self.tempdir
and not silent
and log_output
and not dry_run
):
self.set_sessionlogfile(sessionlogfile, working_basename, working_dir)
if not silent or verbose >= 3:
self.log("-" * 80)
if self.sessionlogfile:
print("Session log:", self.sessionlogfile.filename)
print("")
use_madvr = (
cmdname
in (
get_argyll_utilname("dispcal"),
get_argyll_utilname("dispread"),
get_argyll_utilname("dispwin"),
)
and get_arg("-dmadvr", args)
and madvr
)
self.use_madvr = use_madvr
madvr_use_virtual_display = (
use_madvr
and self.argyll_virtual_display
and (sys.platform != "win32" or getcfg("patterngenerator.ffp_insertion"))
)
if use_madvr:
# Try to connect to running madTPG or launch a new instance
try:
if self.madtpg_connect():
# Connected
if (
isinstance(self.madtpg, madvr.MadTPG_Net)
or madvr_use_virtual_display
):
# Need to handle calibration clearing/loading/saving
# for madVR net-protocol pure python implementation
cal = None
calfilename = None
profile = None
ramp = False
if cmdname == get_argyll_utilname("dispwin"):
if "-c" in args:
# Clear calibration
self.log("MadTPG_Net clear calibration")
ramp = None
if "-L" in args:
# NOTE: Hmm, profile will be None. It's not
# functionality we currently use though.
profile = config.get_display_profile()
if "-s" in args:
# Save calibration. Get from madTPG
self.log("MadTPG_Net save calibration:", args[-1])
ramp = self.madtpg.get_device_gamma_ramp()
if not ramp:
self.madtpg_disconnect(False)
return Error("madVR_GetDeviceGammaRamp failed")
cal = """CAL
KEYWORD "DEVICE_CLASS"
DEVICE_CLASS "DISPLAY"
KEYWORD "COLOR_REP"
COLOR_REP "RGB"
BEGIN_DATA_FORMAT
RGB_I RGB_R RGB_G RGB_B
END_DATA_FORMAT
NUMBER_OF_SETS 256
BEGIN_DATA
"""
# Convert ushort_Array_256_Array_3 to dictionary
RGB = {}
for j in range(3):
for i in range(256):
if i not in RGB:
RGB[i] = []
RGB[i].append(ramp[j][i] / 65535.0)
# Get RGB from dictionary
for i in RGB:
R, G, B = RGB[i]
cal += "{:f} {:f} {:f} {:f}\n".format(
i / 255.0, R, G, B
)
cal += "END_DATA"
# Write out .cal file
try:
with open(args[-1], "w") as calfile:
calfile.write(cal)
except OSError as exception:
self.madtpg_disconnect(False)
return exception
cal = None
ramp = False
elif working_basename:
# .cal/.icc/.icm file to load
calfilename = args[-1]
elif cmdname == get_argyll_utilname("dispread"):
# Check for .cal file to load
# NOTE this isn't normally used as we use -K
# for madTPG, but is overridable by the user
k_arg = get_arg("-k", args, True)
if k_arg:
calfilename = args[k_arg[0] + 1]
else:
ramp = None
else:
# dispcal
ramp = None
if calfilename:
# Load calibration from cal file or ICC profile
self.log("MadTPG_Net load calibration:", calfilename)
result = check_file_isfile(calfilename)
if isinstance(result, Exception):
self.madtpg_disconnect(False)
return result
if calfilename.lower().endswith(".cal"):
# .cal file
try:
cal = CGATS(calfilename)
except (IOError, CGATSError) as exception:
self.madtpg_disconnect(False)
return exception
else:
# ICC profile
try:
profile = ICCProfile(calfilename)
except (
IOError,
ICCProfileInvalidError,
) as exception:
self.madtpg_disconnect(False)
return exception
if profile:
# Load calibration from ICC profile (if present)
cal = extract_cal_from_profile(profile, None, False)
if not cal:
# Linear
ramp = None
if cal:
# Check calibration we're going to load
try:
cal = verify_cgats(cal, ("RGB_R", "RGB_G", "RGB_B"))
if len(cal.DATA) != 256:
# Needs to have 256 entries
raise CGATSError(
"{}: {} != 256".format(
lang.getstr("calibration"),
lang.getstr("number_of_entries"),
)
)
except CGATSError as exception:
self.madtpg_disconnect(False)
return exception
# Convert calibration to ushort_Array_256_Array_3
ramp = ((ctypes.c_ushort * 256) * 3)()
for i in range(256):
for j, channel in enumerate("RGB"):
ramp[j][i] = int(
round(cal.DATA[i]["RGB_" + channel] * 65535)
)
else:
ramp = None
if ramp is not False and not self.madtpg.set_device_gamma_ramp(
ramp
):
self.madtpg_disconnect(False)
return Error("madVR_SetDeviceGammaRamp failed")
if (
isinstance(self.madtpg, madvr.MadTPG_Net)
or madvr_use_virtual_display
) and cmdname == get_argyll_utilname("dispwin"):
# For madVR net-protocol pure python implementation
# we are now done
if "-s" not in args:
# Only disconnect if we didn't save calibration (as
# part of a follow-up measurement)
self.madtpg_disconnect(False)
return True
if "-V" not in args and not use_3dlut_override:
endis = "disable"
else:
endis = "enable"
if not getattr(self.madtpg, endis + "_3dlut")():
self.madtpg_disconnect(False)
return Error("madVR_{}3dlut failed".format(endis.capitalize()))
fullscreen = self.madtpg.is_use_fullscreen_button_pressed()
if sys.platform == "win32":
# Make sure interactive display adjustment window isn't
# concealed by temporarily disabling fullscreen if
# needed. This is only a problem in single display
# configurations.
if self.single_real_display():
# We only have one "real" display connected
if cmdname == get_argyll_utilname("dispcal") or (
self.dispread_after_dispcal
and cmdname == get_argyll_utilname("dispread")
and self._detecting_video_levels
):
if (
getcfg("calibration.interactive_display_adjustment")
and not getcfg("calibration.update")
and fullscreen
):
# Disable fullscreen
if self.madtpg.set_use_fullscreen_button(False):
self.log(
"Temporarily leaving madTPG fullscreen"
)
self.madtpg_previous_fullscreen = fullscreen
else:
self.log(
"Warning - couldn't "
"temporarily leave madTPG "
"fullscreen"
)
elif (
getattr(self, "madtpg_previous_fullscreen", None)
and cmdname == get_argyll_utilname("dispread")
and self.dispread_after_dispcal
):
# Restore fullscreen
if self.madtpg.set_use_fullscreen_button(True):
self.log("Restored madTPG fullscreen")
self.madtpg_previous_fullscreen = None
else:
self.log(
"Warning - couldn't restore madTPG fullscreen"
)
self.madtpg_fullscreen = (
self.madtpg.is_use_fullscreen_button_pressed()
)
# IMPORTANT: When making changes to the instrument on screen
# detection, also apply them to appropriate part in
# Worker.check_instrument_place_on_screen
if (
(
not (
cmdname == get_argyll_utilname("dispwin")
or self.dispread_after_dispcal
)
or (
cmdname == get_argyll_utilname("dispcal")
and ("-m" in args or "-u" in args)
)
or self._detecting_video_levels
)
and self.madtpg_fullscreen
and not self.instrument_on_screen
):
# Show place instrument on screen message with countdown
countdown = 15
# We need to show the progress bar to make OSD visible
if not self.madtpg.show_progress_bar(countdown):
self.madtpg_disconnect()
return Error("madVR_ShowProgressBar failed")
self.madtpg_osd = (
not self.madtpg.is_disable_osd_button_pressed()
)
if not self.madtpg_osd:
# Enable OSD if disabled
if self.madtpg.set_disable_osd_button(False):
self.log(
"Temporarily enabled madTPG OSD for "
"instrument placement countdown"
)
else:
self.log(
"Warning - couldn't temporarily enable madTPG OSD"
)
for i in range(countdown):
if self.subprocess_abort:
break
if not self.madtpg.set_osd_text(
lang.getstr(
"instrument.place_on_screen.madvr",
(countdown - i, self.get_instrument_name()),
)
):
self.madtpg_disconnect()
return Error("madVR_SetOsdText failed")
ts = time()
if i % 2 == 0:
# Flash test area red
self.madtpg.show_rgb(0.25, 0.10, 0.10)
else:
self.madtpg.show_rgb(0.15, 0.15, 0.15)
delay = time() - ts
if delay < 1:
sleep(1 - delay)
self.instrument_on_screen = True
if not self.madtpg_osd:
# Disable OSD
if self.madtpg.set_disable_osd_button(True):
self.log("Restored madTPG 'Disable OSD' button state")
self.madtpg_osd = None
else:
self.log(
"Warning - could not restore madTPG "
"'Disable OSD' button state"
)
self.madtpg.set_osd_text("\u25b6") # "Play" symbol
self.madtpg.show_rgb(0.5, 0.5, 0.5)
# Get black and white level
self.madtpg_bw_lvl = self.madtpg.get_black_and_white_level()
if not self.madtpg_bw_lvl:
self.log("madVR_GetBlackAndWhiteLevel failed")
else:
self.log("Output levels: {}-{}".format(self.madtpg_bw_lvl))
# Get pattern config
patternconfig = self.madtpg.get_pattern_config()
if (
not patternconfig
or not isinstance(patternconfig, tuple)
or len(patternconfig) != 4
):
self.madtpg_disconnect()
return Error("madVR_GetPatternConfig failed")
self.log("Pattern area: {:d}%".format(patternconfig[0]))
self.log("Background level: {:d}%".format(patternconfig[1]))
self.log(
"Background mode: {}".format(
{0: "Constant", 1: "APL gamma", 2: "APL linear"}.get(
patternconfig[2], patternconfig[2]
)
)
)
self.log("Border width: {:d} pixels".format(patternconfig[3]))
if (
isinstance(self.madtpg, madvr.MadTPG_Net)
or madvr_use_virtual_display
):
dindex = args.index("-dmadvr")
args.remove("-dmadvr")
if madvr_use_virtual_display:
args.insert(
dindex, "-d{}".format(self.argyll_virtual_display)
)
else:
args.insert(0, "-P1,1,0.01")
else:
# Only if using native madTPG implementation!
if not get_arg("-P", args):
# Setup patch size to match pattern config
args.insert(
0, "-P0.5,0.5,{:f}".format(math.sqrt(patternconfig[0]))
)
if not patternconfig[1] and self.argyll_version >= [1, 7]:
# Setup black background if background level is zero.
# Only do this for Argyll >= 1.7 to prevent messing
# with pattern area when Argyll 1.6.x is used
args.insert(0, "-F")
self.log("")
else:
return Error(lang.getstr("madtpg.launch.failure"))
except Exception as exception:
if isinstance(getattr(self, "madtpg", None), madvr.MadTPG_Net) or (
getattr(self, "madtpg", None) and madvr_use_virtual_display
):
self.madtpg_disconnect()
return exception
# Use mad* net protocol pure python implementation
use_madnet = use_madvr and (
isinstance(self.madtpg, madvr.MadTPG_Net) or madvr_use_virtual_display
)
# Use mad* net protocol pure python implementation as pattern generator
self.use_madnet_tpg = use_madnet and cmdname != get_argyll_utilname("dispwin")
if self.use_patterngenerator or self.use_madnet_tpg:
# Run a dummy command so we can grab the RGB numbers for
# the pattern generator from the output
carg = get_arg("-C", args, True)
if carg:
index = min(carg[0] + 1, len(args) - 1)
args[index] += " && "
else:
args.insert(0, "-C")
args.insert(1, "")
index = 1
python, pythonpath = get_python_and_pythonpath()
script_dir = working_dir
pythonscript = WAIT_FILE_TEMPLATE.format(script_dir=script_dir)
waitfilename = os.path.join(script_dir, ".wait")
if sys.platform == "win32":
# Avoid problems with encoding
python = win32api.GetShortPathName(python)
for i, path in enumerate(pythonpath):
if os.path.exists(path):
pythonpath[i] = win32api.GetShortPathName(path)
# Write out .wait.py file
scriptfilename = f"{waitfilename}.py"
with open(scriptfilename, "w", encoding="utf-8") as scriptfile:
scriptfile.write(pythonscript)
scriptfilename = win32api.GetShortPathName(scriptfilename)
# Write out .wait.cmd file
with open(f"{waitfilename}.cmd", "w", encoding="utf-8") as waitfile:
waitfile.write("@echo off\n")
waitfile.write("echo.\n")
waitfile.write("echo Current RGB %*\n")
waitfile.write(
'set "PYTHONPATH={}"\n'.format(
safe_str(os.pathsep.join(pythonpath)), enc
)
)
waitfile.write(
'"{}" -S "{}" %*\n'.format(
safe_str(python, enc), safe_str(scriptfilename, enc)
)
)
args[index] += waitfilename
else:
# Write out .wait file
with open(waitfilename, "w") as waitfile:
waitfile.write(f"#!/usr/bin/env python3\n{pythonscript}")
os.chmod(waitfilename, 0o755)
args[index] += "{} ./{}".format(
strtr(safe_str(python), {'"': r"\"", "$": r"\$"}),
os.path.basename(waitfilename),
)
if verbose >= 1 or not silent:
if not silent or verbose >= 3:
if not silent and dry_run and not self.cmdrun:
print(lang.getstr("dry_run"))
print("")
self.cmdrun = True
if working_dir:
self.log(lang.getstr("working_dir"))
indent = " "
for name in working_dir.split(os.path.sep):
self.log(
textwrap.fill(
name + os.path.sep,
80,
expand_tabs=False,
replace_whitespace=False,
initial_indent=indent,
subsequent_indent=indent,
)
)
indent += " "
self.log("")
self.log(lang.getstr("commandline"))
printcmdline(cmd, args, fn=self.log, cwd=working_dir)
self.log("")
if not silent and dry_run:
if not self.lastcmdname or self.lastcmdname == cmdname:
print(lang.getstr("dry_run.end"))
if self.owner and hasattr(self.owner, "infoframe_toggle_handler"):
wx.CallAfter(self.owner.infoframe_toggle_handler, show=True)
if use_madnet:
self.madtpg_disconnect()
return UnloggedInfo(lang.getstr("dry_run.info"))
cmdline = [cmd] + args
for i, item in enumerate(cmdline):
if i > 0 and item.find(os.path.sep) > -1:
if sys.platform == "win32":
item = make_win32_compatible_long_path(item)
if (
re.search(r"[^\x20-\x7e]", os.path.basename(item))
and os.path.exists(item)
and i < len(cmdline) - 1
):
# Avoid problems with encoding under Windows by using
# GetShortPathName, but be careful with the last
# parameter which may be used as the basename for the
# output file
item = win32api.GetShortPathName(item)
if working_dir and os.path.dirname(cmdline[i]) == working_dir:
# Strip the path from all items in the working dir
item = os.path.basename(item)
if item != cmdline[i]:
cmdline[i] = item
if (
working_dir
and sys.platform == "win32"
and re.search(r"[^\x20-\x7e]", working_dir)
and os.path.exists(working_dir)
):
# Avoid problems with encoding
working_dir = win32api.GetShortPathName(working_dir)
sudo = None
if asroot and (
(sys.platform != "win32" and os.geteuid() != 0)
or (sys.platform == "win32" and sys.getwindowsversion() >= (6,))
):
if sys.platform == "win32":
# Vista and later
pass
else:
if not self.auth_timestamp:
if hasattr(self, "thread") and self.thread.is_alive():
# Careful: We can only show the auth dialog if running
# in the main GUI thread!
if use_madnet:
self.madtpg_disconnect()
return Error("Authentication requested in non-GUI thread")
result = self.authenticate(cmd, title, parent)
if result is False:
if use_madnet:
self.madtpg_disconnect()
return None
elif isinstance(result, Exception):
if use_madnet:
self.madtpg_disconnect()
return result
sudo = str(self.sudo)
if sudo:
if not use_pty:
# Sudo may need a tty depending on configuration
use_pty = True
cmdline.insert(0, sudo)
if (
cmdname == get_argyll_utilname("dispwin")
and sys.platform != "darwin"
and self.sudo.availoptions.get("E")
and getcfg("sudo.preserve_environment")
):
# Preserve environment so $DISPLAY is set
cmdline.insert(1, "-E")
if not use_pty:
cmdline.insert(1, "-S")
# Set empty string as password prompt to hide it from stderr
cmdline.insert(1, "")
cmdline.insert(1, "-p")
else:
# Use a designated prompt
cmdline.insert(1, "Password:")
cmdline.insert(1, "-p")
if (
working_dir
and working_basename
and not skip_scripts
and not getcfg("skip_scripts")
):
try:
cmdfilename = os.path.join(
working_dir, working_basename + "." + cmdname + script_ext
)
allfilename = os.path.join(
working_dir, working_basename + ".all" + script_ext
)
first = not os.path.exists(allfilename)
last = cmdname == get_argyll_utilname("dispwin")
cmdfile = open(cmdfilename, "w")
allfile = open(allfilename, "a")
cmdfiles = Files((cmdfile, allfile))
if first:
context = cmdfiles
else:
context = cmdfile
if sys.platform == "win32":
context.write("@echo off\n")
context.write(
("PATH {};%PATH%\n".format(os.path.dirname(cmd))).encode(
enc, "safe_asciize"
)
)
cmdfiles.write('pushd "%~dp0"\n'.encode(enc, "safe_asciize"))
if cmdname in (
get_argyll_utilname("dispcal"),
get_argyll_utilname("dispread"),
):
cmdfiles.write("color 07\n")
else:
context.write(
(
"PATH={}:$PATH\n".format(os.path.dirname(cmd)).encode(
enc, "safe_asciize"
)
)
)
if sys.platform == "darwin" and config.mac_create_app:
cmdfiles.write('pushd "`dirname \\"$0\\"`/../../.."\n')
else:
cmdfiles.write('pushd "`dirname \\"$0\\"`"\n')
if (
cmdname
in (
get_argyll_utilname("dispcal"),
get_argyll_utilname("dispread"),
)
and sys.platform != "darwin"
):
cmdfiles.write('echo -e "\\033[40;2;37m" && clear\n')
os.chmod(cmdfilename, 0o755)
os.chmod(allfilename, 0o755)
cmdfiles.write(
" ".join(quote_args(cmdline))
.replace(cmd, cmdname)
.encode(enc, "safe_asciize")
+ "\n".encode()
)
if sys.platform == "win32":
cmdfiles.write("set exitcode=%errorlevel%\n")
if cmdname in (
get_argyll_utilname("dispcal"),
get_argyll_utilname("dispread"),
):
# Reset to default commandline shell colors
cmdfiles.write("color\n")
cmdfiles.write("popd\n")
cmdfiles.write("if not %exitcode%==0 exit /B %exitcode%\n")
else:
cmdfiles.write("exitcode=$?\n")
if (
cmdname
in (
get_argyll_utilname("dispcal"),
get_argyll_utilname("dispread"),
)
and sys.platform != "darwin"
):
# reset to default commandline shell colors
cmdfiles.write('echo -e "\\033[0m" && clear\n')
cmdfiles.write("popd\n")
cmdfiles.write("if [ $exitcode -ne 0 ]; then exit $exitcode; fi\n")
cmdfiles.close()
if sys.platform == "darwin":
if config.mac_create_app:
# Could also use .command file directly, but using
# applescript allows giving focus to the terminal
# window automatically after a delay
script = (
mac_terminal_do_script()
+ mac_terminal_set_colors(do=False)
+ [
"-e",
"set shellscript to quoted form of "
"(POSIX path of (path to resource "
'"main.command"))',
"-e",
'tell app "Terminal"',
"-e",
"do script shellscript in first window",
"-e",
"delay 3",
"-e",
"activate",
"-e",
"end tell",
"-o",
]
)
# Part 1: "cmdfile"
appfilename = os.path.join(
working_dir, working_basename + "." + cmdname + ".app"
)
cmdargs = ["osacompile"] + script + [appfilename.decode()]
p = sp.Popen(
cmdargs, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE
)
p.communicate()
shutil.move(
cmdfilename,
appfilename + "/Contents/Resources/main.command",
)
os.chmod(
appfilename + "/Contents/Resources/main.command", 0o755
)
# Part 2: "allfile"
appfilename = os.path.join(
working_dir, working_basename + ".all.app"
)
cmdargs = ["osacompile"] + script + [appfilename]
p = sp.Popen(
cmdargs, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE
)
p.communicate()
shutil.copyfile(
allfilename,
appfilename + "/Contents/Resources/main.command",
)
os.chmod(
appfilename + "/Contents/Resources/main.command", 0o755
)
if last:
os.remove(allfilename)
except Exception as exception:
self.log(
"Warning - error during shell script creation:", str(exception)
)
cmdline = [safe_str(arg, fs_enc) for arg in cmdline]
working_dir = None if not working_dir else working_dir
try:
if not self.measure_cmd and self.argyll_version >= [1, 2]:
# Argyll tools will no longer respond to keys
if debug:
self.log("[D] Setting ARGYLL_NOT_INTERACTIVE 1")
os.environ["ARGYLL_NOT_INTERACTIVE"] = "1"
elif "ARGYLL_NOT_INTERACTIVE" in os.environ:
del os.environ["ARGYLL_NOT_INTERACTIVE"]
if debug:
self.log("[D] argyll_version", self.argyll_version)
self.log(
"[D] ARGYLL_NOT_INTERACTIVE",
os.environ.get("ARGYLL_NOT_INTERACTIVE"),
)
if self.measure_cmd:
if isinstance(self.measurement_sound, audio.DummySound):
self._init_sounds(dummy=False)
for name, version in (
("MIN_DISPLAY_UPDATE_DELAY_MS", [1, 5]),
("DISPLAY_SETTLE_TIME_MULT", [1, 7]),
):
backup = os.getenv(f"ARGYLL_{name}_BACKUP")
value = None
if (
getcfg(f"measure.override_{name.lower()}")
and self.argyll_version >= version
):
if backup is None:
# Backup current value if any
current = os.getenv(f"ARGYLL_{name}", "")
os.environ[f"ARGYLL_{name}_BACKUP"] = current
else:
current = backup
if current:
self.log(f"{appname}: Overriding ARGYLL_{name} {current}")
# Override
value = str(getcfg(f"measure.{name.lower()}"))
self.log(f"{appname}: Setting ARGYLL_{name} {value}")
elif backup is not None:
value = backup
del os.environ[f"ARGYLL_{name}_BACKUP"]
if value:
self.log(f"{appname}: Restoring ARGYLL_{name} {value}")
elif f"ARGYLL_{name}" in os.environ:
del os.environ[f"ARGYLL_{name}"]
elif f"ARGYLL_{name}" in os.environ:
self.log(
f"{appname}: ARGYLL_{name}",
os.getenv(f"ARGYLL_{name}"),
)
if value:
os.environ[f"ARGYLL_{name}"] = value
elif cmdname in (
get_argyll_utilname("iccgamut"),
get_argyll_utilname("tiffgamut"),
get_argyll_utilname("viewgam"),
get_argyll_utilname("colprof"),
get_argyll_utilname("collink"),
):
os.environ["ARGYLL_3D_DISP_FORMAT"] = "VRML"
if sys.platform not in ("darwin", "win32"):
os.environ["ENABLE_COLORHUG"] = "1"
if sys.platform == "win32":
startupinfo = sp.STARTUPINFO()
startupinfo.dwFlags |= sp.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = sp.SW_HIDE
else:
startupinfo = None
if not use_pty:
data_encoding = enc
if silent:
stderr = sp.STDOUT
else:
stderr = tempfile.SpooledTemporaryFile()
if capture_output:
stdout = tempfile.SpooledTemporaryFile()
elif (
sys.stdout and hasattr(sys.stdout, "isatty") and sys.stdout.isatty()
):
stdout = sys.stdout
else:
stdout = sp.PIPE
if sudo:
stdin = tempfile.SpooledTemporaryFile()
stdin.write(self.pwd.encode(enc, "replace") + os.linesep)
stdin.seek(0)
else:
stdin = sp.PIPE
else:
data_encoding = self.pty_encoding
kwargs = dict(timeout=20, cwd=working_dir, env=os.environ)
if sys.platform == "win32":
# FIX: stdio cp1252 vs utf-8 (cp65001) issue under Windows 10/11+
# os.environ["PYTHONLEGACYWINDOWSSTDIO"] = "1"
kwargs["codepage"] = windll.kernel32.GetACP()
# As Windows' console always hard wraps at the
# rightmost column, increase the buffer width
kwargs["columns"] = 160
stderr = None
stdout = EncodedWriter(StringIO(), None, data_encoding)
logfiles = []
if (
hasattr(self, "thread")
and self.thread.is_alive()
and self.interactive
and getattr(self, "terminal", None)
):
logfiles.append(
FilteredStream(
self.terminal, discard="", triggers=self.triggers
)
)
if log_output:
linebuffered_logfiles = []
if (
sys.stdout
and hasattr(sys.stdout, "isatty")
and sys.stdout.isatty()
):
linebuffered_logfiles.append(print)
else:
linebuffered_logfiles.append(log)
if self.sessionlogfile:
linebuffered_logfiles.append(self.sessionlogfile)
logfiles.append(
LineBufferedStream(
FilteredStream(
Files(linebuffered_logfiles),
data_encoding,
discard="",
linesep_in="\n",
triggers=[],
)
)
)
logfiles.append(stdout)
if hasattr(self, "thread") and self.thread.is_alive():
logfiles.extend([self.recent, self.lastmsg, self])
logfiles = Files(logfiles)
if self.use_patterngenerator:
pgname = config.get_display_name()
self.setup_patterngenerator(logfiles)
if not hasattr(self.patterngenerator, "conn") and hasattr(
self.patterngenerator, "wait"
):
# Wait for connection - blocking
self.patterngenerator.wait()
if hasattr(self.patterngenerator, "conn"):
self.patterngenerator_send(
(0.5,) * 3, increase_sent_count=False
)
else:
# User aborted before connection was established
return False
if pgname == "Prisma":
x, y, w, h, size = get_pattern_geometry()
if use_3dlut_override:
self.patterngenerator.enable_processing(size=size * 100)
else:
self.patterngenerator.disable_processing(size=size * 100)
tries = 1
while tries > 0:
if self.subprocess_abort:
break
if use_pty:
if (
self.argyll_version >= [1, 2]
and USE_WPOPEN
and os.environ.get("ARGYLL_NOT_INTERACTIVE")
):
self.subprocess = WPopen(
cmdline,
stdin=sp.PIPE,
stdout=tempfile.SpooledTemporaryFile(),
stderr=sp.STDOUT,
shell=shell,
cwd=working_dir,
startupinfo=startupinfo,
)
else:
# Minimum Windows version: XP or Server 2003
if sys.platform == "win32" and sys.getwindowsversion() < (5, 1):
raise Error(lang.getstr("windows.version.unsupported"))
try:
self.subprocess = wexpect.spawn(
cmdline[0], cmdline[1:], **kwargs
)
except wexpect.ExceptionPexpect as exception:
self.retcode = -1
raise Error(str(exception))
if sys.platform == "darwin" and self.measure_cmd:
# Caffeinate (prevent sleep/screensaver)
caffeinate = which("caffeinate")
if caffeinate:
wait_pid = str(self.subprocess.pid)
try:
# -d prevent idle display sleep
# -i prevent idle system sleep
# -w wait for process <pid> to exit
caffeinated = sp.Popen(
[caffeinate, "-d", "-i", "-w", wait_pid],
stdin=sp.PIPE,
stdout=sp.PIPE,
stderr=sp.PIPE,
)
except Exception as exception:
self.log(
f"{appname}: caffeinate could not "
"be started - screensaver and "
"display/system sleep may still "
"occur!"
)
self.log(exception)
else:
self.log(
f"{appname}: caffeinate started, "
"preventing screensaver and "
"display/system sleep - waiting "
"for {} (PID {}) to exit".format(
cmdname, wait_pid
)
)
def caffeinate_wait():
caffeinated.wait()
self.log(
f"{appname}: caffeinate exited "
"with code {}".format(
caffeinated.returncode
)
)
wait_thread = threading.Thread(
target=caffeinate_wait
)
wait_thread.daemon = True
wait_thread.start()
else:
self.log(
f"{appname}: caffeinate not found - "
"screensaver and display/system sleep "
"may still occur!"
)
if debug >= 9 or (test and "-?" not in args):
self.subprocess.interact()
self.subprocess.logfile_read = logfiles
if self.measure_cmd:
keyhit_strs = [" or Q to ", r"8\) Exit"]
patterns = keyhit_strs + ["Current", r" \d+ of \d+"]
self.log(f"{appname}: Starting interaction with subprocess")
else:
patterns = []
self.log(f"{appname}: Waiting for EOF")
loop = 0
pwdsent = False
authfailed = False
eof = False
while 1:
if loop < 1 and sudo:
curpatterns = ["Password:"] + patterns
else:
curpatterns = patterns
# NOTE: Using a timeout of None can block indefinitely
# and prevent expect() from ever returning!
self.subprocess.expect(
curpatterns + [wexpect.EOF, wexpect.TIMEOUT], timeout=1
)
if self.subprocess.after is wexpect.EOF:
self.log(f"{appname}: Reached EOF (OK)")
break
elif self.subprocess.after is wexpect.TIMEOUT:
if not self.subprocess.isalive():
self.log(
f"{appname}: Subprocess no longer alive (timeout)"
)
if eof:
break
eof = True
continue
elif self.subprocess.after == "Password:" and loop < 1 and sudo:
if pwdsent:
self.subprocess.sendcontrol("C")
authfailed = True
self.auth_timestamp = 0
else:
self._safe_send(
self.pwd + os.linesep,
obfuscate=True,
)
pwdsent = True
if not self.subprocess.isalive():
break
continue
elif self.measure_cmd:
if [
keyhit_str
for keyhit_str in keyhit_strs
if re.search(keyhit_str, self.subprocess.after)
]:
# Wait for the keypress
self.log(f"{appname}: Waiting for send buffer")
while not self.send_buffer:
if not self.subprocess.isalive():
self.log(
f"{appname}: Subprocess no longer alive (unknown reason)"
)
break
sleep(0.05)
self.log(
f"{appname}: Send buffer received: {self.send_buffer}"
)
if (
self.send_buffer is not None
and self.subprocess.isalive()
):
if (
self.send_buffer == "7"
and use_madvr
and cmdname == get_argyll_utilname("dispcal")
):
# Restore madTPG OSD and fullscreen
self.madtpg_restore_settings(False)
self.log(
f"{appname}: Sending buffer: {self.send_buffer}"
)
self._safe_send(self.send_buffer)
self.send_buffer = None
if not self.subprocess.isalive():
break
loop += 1
# We need to call isalive() to set the exitstatus.
# We can't use wait() because it might block in the
# case of a timeout
if self.subprocess.isalive():
self.log(f"{appname}: Checking subprocess status")
while self.subprocess.isalive():
sleep(0.1)
self.log(f"{appname}: Subprocess no longer alive (OK)")
self.retcode = self.subprocess.exitstatus
if authfailed:
raise Error(lang.getstr("auth.failed"))
else:
try:
if (
asroot
and sys.platform == "win32"
and sys.getwindowsversion() >= (6,)
):
p = run_as_admin(cmd, args, close_process=False, show=False)
while not self.subprocess_abort:
# Wait for subprocess to exit
self.retcode = win32event.WaitForSingleObject(
p["hProcess"], 50
)
if not self.retcode:
break
p["hProcess"].Close()
return self.retcode == 0
else:
self.subprocess = sp.Popen(
cmdline,
stdin=stdin,
stdout=stdout,
stderr=stderr,
shell=shell,
cwd=working_dir,
startupinfo=startupinfo,
)
except Exception as exception:
self.retcode = -1
raise Error(str(exception))
self.retcode = self.subprocess.wait()
if stdin != sp.PIPE and not getattr(stdin, "closed", True):
stdin.close()
if self.is_working() and self.subprocess_abort and self.retcode == 0:
self.retcode = -1
self.subprocess = None
tries -= 1
if not silent and stderr:
stderr.seek(0)
errors = stderr.readlines()
if not capture_output or stderr is not stdout:
stderr.close()
if len(errors):
for line in errors:
if isinstance(line, bytes):
line = line.decode()
line = line.strip()
if (
"Instrument Access Failed" in line
and "-N" in cmdline[:-1]
):
cmdline.remove("-N")
tries = 1
break
if (
line.find("User Aborted") < 0
and line.find(
"XRandR 1.2 is faulty - falling back "
"to older extensions"
)
< 0
):
self.errors.append(line)
if tries > 0 and not use_pty:
stderr = tempfile.SpooledTemporaryFile()
if capture_output or use_pty:
stdout.seek(0)
stdout_readlines = stdout.readlines()
self.output = []
for line in stdout_readlines:
if isinstance(line, bytes):
line = line.decode()
line = line.strip()
line = re.sub(r"^\.{4,}\s*$", "", line).strip()
self.output.append(line)
stdout.close()
if len(self.output) and log_output:
if not use_pty:
self.log("".join(self.output).strip())
if (
display_output
and self.owner
and hasattr(self.owner, "infoframe_toggle_handler")
):
wx.CallAfter(self.owner.infoframe_toggle_handler, show=True)
if tries > 0 and not use_pty:
stdout = tempfile.SpooledTemporaryFile()
if not silent and len(self.errors):
errstr = "".join(self.errors).strip()
self.log(errstr)
except (Error, socket.error, EnvironmentError, RuntimeError) as exception:
return exception
except Exception:
if debug:
self.log("[D] working_dir:", working_dir)
# TODO: Fix this in the source of the problem, not here!!!
new_cmdline = []
for item in cmdline:
if isinstance(item, bytes):
item = item.decode()
new_cmdline.append(item)
cmdline = new_cmdline
errmsg = " ".join(cmdline) + "\n" + str(traceback.format_exc())
self.retcode = -1
return Error(errmsg)
finally:
if (
sudo
and cmdname not in ("chown", get_argyll_utilname("dispwin"))
and working_dir
and working_dir == self.tempdir
and os.listdir(working_dir)
):
# We need to take ownership of any files created by commands
# run via sudo otherwise we cannot move or remove them from
# the temporary directory!
errors = self.errors
output = self.output
retcode = self.retcode
self.exec_cmd(
"chown",
["-R", getpass.getuser().decode(fs_enc), working_dir],
capture_output=capture_output,
skip_scripts=True,
asroot=True,
)
self.errors = errors
self.output = output
self.retcode = retcode
finished = (
(self.cmdname == "dispcal" and not self.dispread_after_dispcal)
or (self.cmdname == "dispread" and not self._detecting_video_levels)
or self.retcode
)
if self.patterngenerator:
if hasattr(self.patterngenerator, "conn"):
try:
if config.get_display_name() == "Resolve":
# Send fullscreen black to prevent burn-in
if finished:
try:
self.patterngenerator.send(
(0,) * 3, x=0, y=0, w=1, h=1
)
except socket.error:
pass
else:
self.patterngenerator.disconnect_client()
except Exception as exception:
self.log(exception)
if hasattr(self, "madtpg") and finished:
self.madtpg_disconnect()
if (
sys.platform not in ("darwin", "win32")
and self.measure_cmd
and self._use_patternwindow
and (profiling_inhibit or display_profile)
and not report_current_cal
):
# Preliminary Wayland support. This still needs a lot
# of work as Argyll doesn't support Wayland natively yet,
# so we use virtual display to drive our own patch window.
# We need to restore the display profile.
if profiling_inhibit:
# Use DBus interface to call ProfilingInhibit()
try:
cd_device.profiling_uninhibit()
except Exception as cd_exception:
self.log(cd_exception)
profiling_inhibit = False
else:
self.log(f"{appname}: Uninhibited display device")
if not profiling_inhibit:
# Fallback - restore display profile
self.log(f"{appname}: Re-assigning display profile...")
cdinstall = self._attempt_install_profile_colord(display_profile)
if cdinstall is True:
self.log(f"{appname}: Successfully re-assigned display profile")
elif not cdinstall:
self.log(lang.getstr("calibration.load_error"))
# Remove temp sRGB profile
try:
os.remove(
os.path.join(
xdg_data_home,
"icc",
os.path.basename(self.srgb.fileName),
)
)
except Exception as exception:
self.log(exception)
if not silent:
self.log(cmdname, "exitcode:", self.retcode)
if self.retcode != 0:
if use_pty and verbose >= 1 and not silent:
self.log(lang.getstr("aborted"))
if use_pty and len(self.output):
errmsg = None
for i, line in enumerate(self.output):
if (
"Calibrate failed with 'User hit Abort Key' (No device error)"
in line
):
break
if (
(
": Error" in line
and "failed with 'User Aborted'" not in line
and "returned error code 1" not in line
)
or (
line.startswith("Failed to")
and "Failed to meet target" not in line
)
or (
"Requested ambient light capability" in line
and len(self.output) == i + 2
)
or (
"Diagnostic:" in line
and (
len(self.output) == i + 1
or self.output[i + 1].startswith("usage:")
)
)
or "communications failure" in line.lower()
):
# "returned error code 1" == user aborted
if (
sys.platform == "win32"
and (
"config 1 failed (Operation not supported or "
"unimplemented on this platform) (Permissions ?)"
)
in line
):
self.output.insert(
i,
lang.getstr("argyll.instrument.driver.missing")
+ "\n\n"
+ lang.getstr("argyll.error.detail")
+ " ",
)
if "Diagnostic:" in line:
errmsg = line
else:
errmsg = "".join(self.output[i:])
startpos = errmsg.find(": Error")
if startpos > -1:
errmsg = errmsg[startpos + 2 :]
if errmsg:
return UnloggedError(errmsg.strip())
if self.exec_cmd_returnvalue is not None:
return self.exec_cmd_returnvalue
return self.retcode == 0
def flush(self):
pass
def _generic_consumer(
self, delayedResult, consumer, continue_next, *args, **kwargs
):
# consumer must accept result as first arg
result = None
try:
result = delayedResult.get()
except Exception as exception:
if (
exception.__class__ is Exception
and exception.args
and exception.args[0] == "aborted"
):
# Special case - aborted
result = False
elif isinstance(exception, (UntracedError, Info)):
result = exception
else:
if hasattr(exception, "originalTraceback"):
self.log(exception.originalTraceback)
else:
self.log(traceback.format_exc())
result = UnloggedError(safe_str(exception))
if self.progress_start_timer.IsRunning():
self.progress_start_timer.Stop()
self.finished = True
if not continue_next or isinstance(result, Exception) or not result:
self.stop_progress()
self.subprocess_abort = False
self.thread_abort = False
self.recent.clear()
self.lastmsg.clear()
if self.thread.is_alive():
# Consumer may check if thread is still alive. Technically
# it shouldn't be at that point when using CallAfter, but e.g. on
# OS X there seems to be overlap with the thread counting as
# 'alive' even though it already exited
self.thread.join()
wx.CallAfter(consumer, result, *args, **kwargs)
def generate_A2B0(self, profile, clutres=None, logfile=None):
# Lab cLUT is currently not implemented and should NOT be used!
if profile.connectionColorSpace != b"XYZ":
raise Error(
lang.getstr(
"profile.unsupported",
(profile.connectionColorSpace, profile.connectionColorSpace),
)
)
if logfile:
print("-" * 80)
logfile.write("Creating perceptual A2B0 table\n")
logfile.write("\n")
# Make new A2B0
A2B0 = LUT16Type(None, "A2B0", profile)
# Matrix (identity)
A2B0.matrix = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
# Input / output curves (linear)
A2B0.input = []
A2B0.output = []
channel = []
for j in range(256):
channel.append(j * 257)
for table in (A2B0.input, A2B0.output):
for i in range(3):
table.append(channel)
# cLUT
if logfile:
logfile.write("Generating A2B0 table lookup input values...\n")
A2B0.clut = []
if not clutres:
clutres = len(profile.tags.A2B0.clut[0])
if logfile:
logfile.write("cLUT grid res: {:d}\n".format(clutres))
vrange = range(clutres)
step = 1.0 / (clutres - 1.0)
idata = []
for R in vrange:
for G in vrange:
for B in vrange:
idata.append([v * step for v in (R, G, B)])
if logfile:
logfile.write("Looking up input values through A2B0 table...\n")
odata = self.xicclu(profile, idata, pcs="x", logfile=logfile)
numrows = len(odata)
if numrows != clutres**3:
raise ValueError(
"Number of cLUT entries ({}) exceeds cLUT res "
"maximum ({}^3 = {})".format(numrows, clutres, clutres**3)
)
XYZbp = list(odata[0])
XYZwp = list(odata[-1])
if logfile:
logfile.write("Filling cLUT...\n")
for i, (X, Y, Z) in enumerate(odata):
if i % clutres == 0:
if self.thread_abort:
raise Info(lang.getstr("aborted"))
A2B0.clut.append([])
if logfile:
logfile.write("\r{:0.0f}%".format(round(i / (numrows - 1.0) * 100)))
# Apply black point compensation
XYZ = colormath.blend_blackpoint(X, Y, Z, XYZbp)
XYZ = [v / XYZwp[1] for v in XYZ]
A2B0.clut[-1].append([max(v * 32768, 0) for v in XYZ])
if logfile:
logfile.write("\n")
profile.tags.A2B0 = A2B0
return True
def generate_B2A_from_inverse_table(
self,
profile,
clutres=None,
source="A2B",
tableno=None,
bpc=False,
smooth=True,
rgb_space=None,
logfile=None,
filename=None,
only_input_curves=False,
):
"""Generate a profile's B2A table by inverting the A2B table
(default A2B1 or A2B0)
It is also possible to re-generate a B2A table by interpolating
the B2A table itself.
"""
if tableno is None:
if "A2B1" in profile.tags:
tableno = 1
else:
tableno = 0
if not clutres:
if "B2A{:d}".format(tableno) in profile.tags:
tablename = f"B2A{tableno:d}"
else:
tablename = f"A2B{tableno:d}"
clutres = len(profile.tags[tablename].clut[0])
if source == "B2A" and clutres > 23:
# B2A interpolation is smoothest when used with a lower cLUT res
clutres = 23
if logfile:
if source == "A2B":
msg = f"Generating B2A{tableno:d} table by inverting A2B{tableno:d} table\n"
else:
msg = f"Re-generating B2A{tableno:d} table by interpolation\n"
logfile.write(msg)
logfile.write("\n")
cat = profile.guess_cat() or "Bradford"
self.log("Using chromatic adaptation transform matrix:", cat)
# Note that intent 0 will be colorimetric if no other tables are
# present
intent = {0: "p", 1: "r", 2: "s"}[tableno]
# Lookup RGB -> XYZ for primaries, black- and white point
idata = [[0, 0, 0], [1, 1, 1], [1, 0, 0], [0, 1, 0], [0, 0, 1]]
direction = {"A2B": "f", "B2A": "ib"}[source]
odata = self.xicclu(profile, idata, intent, direction, pcs="x")
# Scale to Y = 1
XYZwp_abs = odata[1]
XYZwpY = odata[1][1]
odata = [[n / XYZwpY for n in v] for v in odata]
XYZbp = odata[0]
XYZwp = odata[1]
XYZr = odata[2]
XYZg = odata[3]
XYZb = odata[4]
# Sanity check whitepoint
if (
round(XYZwp[0], 3) != 0.964
or round(XYZwp[1], 3) != 1
or round(XYZwp[2], 3) != 0.825
):
raise Error(
"Argyll CMS xicclu: Invalid white XYZ: {:.4f} {:.4f} {:.4f}".format(
*XYZwp_abs
)
)
# Get the primaries
XYZrgb = [XYZr, XYZg, XYZb]
# Sanity check primaries:
# Red Y, Z shall not be higher than X
# Green X, Z shall not be higher than Y
# Blue X, Y shall not be higher than Z
for i, XYZ in enumerate(XYZrgb):
for j, v in enumerate(XYZ):
if v > XYZ[i]:
raise Error(
"xicclu: Invalid primary {} XYZ: {:.4f} {:.4f} {:.4f}".format(
*(("RGB"[i],) + tuple(XYZ))
)
)
if logfile:
logfile.write("Black XYZ: {:.4f} {:.4f} {:.4f}\n".format(*XYZbp))
logfile.write("White XYZ: {:.4f} {:.4f} {:.4f}\n".format(*XYZwp))
for i in range(3):
logfile.write(
"{} XYZ: {:.4f} {:.4f} {:.4f}\n".format(
*(("RGB"[i],) + tuple(XYZrgb[i]))
)
)
# Prepare input PCS values
if logfile:
logfile.write("Generating input curve PCS values...\n")
idata = []
numentries = 4096
maxval = numentries - 1.0
vrange = range(numentries)
Lbp, abp, bbp = colormath.XYZ2Lab(*[v * 100 for v in XYZbp])
# Method to determine device <-> PCS neutral axis relationship
# 0: L* (a*=b*=0) -> RGB
# 1: As method 0, but blend a* b* to blackpoint hue
# 2: R=G=B -> PCS
# 3: As method 0, but blend a* b* to blackpoint hue in XYZ (BPC)
method = 2
if method != 2:
for i in vrange:
L, a, b = i / maxval * 100, 0, 0
if method in (1, 3) and not bpc and XYZbp != [0, 0, 0]:
# Blend to blackpoint hue
if method == 1:
vv = (L - Lbp) / (100.0 - Lbp) # 0 at bp, 1 at wp
vv = 1.0 - vv
if vv < 0.0:
vv = 0.0
elif vv > 1.0:
vv = 1.0
vv = math.pow(vv, 40.0)
a += vv * abp
b += vv * bbp
else:
X, Y, Z = colormath.Lab2XYZ(L, a, b)
XYZ = colormath.blend_blackpoint(X, Y, Z, None, XYZbp)
a, b = colormath.XYZ2Lab(*[v * 100 for v in XYZ])[1:]
idata.append((L, a, b))
pcs = profile.connectionColorSpace[0:1].lower().decode("utf-8")
if source == "B2A":
# NOTE:
# Argyll's B2A tables are slightly inaccurate:
# 0 0 0 PCS -> RGB may give RGB levels > 0 (it should clip
# instead). Inversely, 0 0 0 RGB -> PCS (through inverted B2A)
# will return PCS values that are too low or zero (ie. not the
# black point as expected)
# TODO: How to deal with this?
# TODO: Erkan Ozgur Yilmaz, please do a research for this issue.
pass
if method == 2:
# lookup RGB -> XYZ values through profile using xicclu to get TRC
odata = []
if not bpc:
numentries -= 1
maxval -= 1
for i in range(numentries):
odata.append([i / maxval] * 3)
idata = self.xicclu(
profile, odata, intent, {"A2B": "f", "B2A": "ib"}[source], pcs="x"
)
wY = idata[-1][1]
idata = [[v[1] / wY * n for n in XYZwp] for v in idata]
# Input curve maps neutral (except near black) XYZ -> RGB
idata = [
list(
colormath.blend_blackpoint(
*v, bp_in=idata[0], bp_out=XYZbp, wp=XYZwp
)
)
for v in idata
]
if not bpc:
numentries += 1
maxval += 1
idata.insert(0, [0, 0, 0])
odata.insert(0, [0, 0, 0])
oXYZ = idata
D50 = colormath.get_whitepoint("D50")
fpL = [colormath.XYZ2Lab(*v + [D50])[0] for v in oXYZ]
else:
oXYZ = [colormath.Lab2XYZ(*v) for v in idata]
fpL = [v[0] for v in idata]
fpX = [v[0] for v in oXYZ]
fpY = [v[1] for v in oXYZ]
fpZ = [v[2] for v in oXYZ]
if method != 2 and bpc and XYZbp != [0, 0, 0]:
if logfile:
logfile.write("Applying BPC to input curve PCS values...\n")
for i, (L, a, b) in enumerate(idata):
X, Y, Z = colormath.Lab2XYZ(L, a, b)
X, Y, Z = colormath.blend_blackpoint(X, Y, Z, None, XYZbp)
idata[i] = colormath.XYZ2Lab(X * 100, Y * 100, Z * 100)
if logfile:
logfile.write("Looking up input curve RGB values...\n")
direction = {"A2B": "if", "B2A": "b"}[source]
if method != 2:
# Lookup Lab -> RGB values through profile using xicclu to get TRC
odata = self.xicclu(
profile, idata, intent, direction, pcs="l", get_clip=True
)
# Deal with values that got clipped (below black as well as white)
do_low_clip = True
for i, values in enumerate(odata):
if values[3] is True or i == 0:
if do_low_clip and (i / maxval * 100 < Lbp or i == 0):
# Set to black
self.log(
"Setting curve entry #{:d} ({:.6f} {:.6f} {:.6f}) to "
"black because it got clipped".format(
*((i,) + tuple(values[:3]))
)
)
values[:] = [0.0, 0.0, 0.0]
elif i == maxval and [round(v, 4) for v in values[:3]] == [1, 1, 1]:
# Set to white
self.log(
"Setting curve entry #{:d} ({:.6f} {:.6f} {:.6f}) to "
"white because it got clipped".format(
*((i,) + tuple(values[:3]))
)
)
values[:] = [1.0, 1.0, 1.0]
else:
# First non-clipping value disables low clipping
do_low_clip = False
if len(values) > 3:
values.pop()
# Sanity check white
if (
round(odata[-1][0], 3) != 1
or round(odata[-1][1], 3) != 1
or round(odata[-1][2], 3) != 1
):
wrgb_warning = (
"Warning: xicclu: Suspicious white RGB: "
"{:.4f} {:.4f} {:.4f}\n".format(*odata[-1])
)
if logfile:
logfile.write(wrgb_warning)
else:
print(wrgb_warning)
xpR = [v[0] for v in odata]
xpG = [v[1] for v in odata]
xpB = [v[2] for v in odata]
# Initialize B2A
if only_input_curves:
# Use existing B2A table
itable = profile.tags[f"B2A{tableno:d}"]
else:
# Create new B2A table
itable = LUT16Type(None, f"B2A{tableno:d}", profile)
use_cam_clipping = True
# Setup matrix
scale = 1 + (32767 / 32768.0)
m3 = colormath.Matrix3x3(((scale, 0, 0), (0, scale, 0), (0, 0, scale)))
matrices = []
if profile.connectionColorSpace == b"Lab":
# L*a*b* LUT
# Use identity matrix for Lab as mandated by ICC spec
itable.matrix = colormath.Matrix3x3([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
m2 = None
elif only_input_curves:
# Use existing matrix
m2 = itable.matrix * m3.inverted()
# Scale for BPC
XYZbp_relcol = self.xicclu(profile, [[0, 0, 0]], pcs="x")[0]
XYZbp_relcol_D50 = [v * XYZbp_relcol[1] for v in XYZwp]
m2i = m2.inverted()
XYZrgb = [m2i * (1, 0, 0), m2i * (0, 1, 0), m2i * (0, 0, 1)]
for i, XYZ in enumerate(XYZrgb):
XYZrgb[i] = colormath.apply_bpc(
*XYZ, bp_in=(0, 0, 0), bp_out=XYZbp_relcol_D50, wp_out=XYZwp
)
else:
# Use a matrix that scales the profile colorspace into the XYZ
# encoding range, to make optimal use of the cLUT grid points
xyYrgb = [colormath.XYZ2xyY(*XYZ) for XYZ in XYZrgb]
area1 = 0.5 * abs(
sum(
x0 * y1 - x1 * y0
for ((x0, y0, Y0), (x1, y1, Y1)) in zip(
xyYrgb, xyYrgb[1:] + [xyYrgb[0]]
)
)
)
if logfile:
logfile.write("Setting up matrix\n")
# RGB spaces used as PCS candidates.
# Six synthetic RGB spaces that are large enough to encompass
# display gamuts (except the LaserVue one) found on
# http://www.tftcentral.co.uk/articles/pointers_gamut.htm
# aswell as a selection of around two dozen profiles from openSUSE
# ICC Profile Taxi database aswell as other user contributions
rgb_spaces = []
rgb = [
("R", (1.0, 0.0, 0.0)),
("G", (0.0, 1.0, 0.0)),
("B", (0.0, 0.0, 1.0)),
]
# Add matrix from just white + 50% gray + black + primaries
# as first entry
# outname = os.path.splitext(profile.fileName)[0]
# if not os.path.isfile(outname + ".ti3"):
# if isinstance(profile.tags.get("targ"), Text):
# with open(outname + ".ti3", "wb") as ti3:
# ti3.write(profile.tags.targ)
# else:
# ti1name = "ti1/d3-e4-s2-g28-m0-b0-f0.ti1"
# ti1 = get_data_path(ti1name)
# if not ti1:
# raise Error(lang.getstr("file.missing", ti1name))
# ti1, ti3, gray = self.ti1_lookup_to_ti3(ti1, profile,
# intent="a",
# white_patches=1)
# outname += "." + ti1name
# ti3.write(outname + ".ti3")
# result = self._create_matrix_profile(outname, omit="TRC")
result = True
if isinstance(result, Exception) or not result:
if logfile:
logfile.write("Warning - could not compute RGBW matrix")
if result:
logfile.write(": " + str(result))
else:
logfile.write("\n")
else:
comp_rgb_space = [2.2, "D50"]
# Oversaturate primaries to add headroom
wx, wy = colormath.XYZ2xyY(*XYZwp)[:2]
# Determine saturation factor by looking at distance between
# Rec. 2020 blue and actual blue primary
bx, by, bY = colormath.XYZ2xyY(
*colormath.adapt(
*colormath.RGB2XYZ(0, 0, 1, "Rec. 2020"),
whitepoint_source="D65",
cat=cat,
)
)
bx2, by2, bY2 = xyYrgb[2]
bu, bv = colormath.xyY2Lu_v_(bx, by, bY)[1:]
bu2, bv2 = colormath.xyY2Lu_v_(bx2, by2, bY2)[1:]
dist = math.sqrt((bx - bx2) ** 2 + (by - by2) ** 2)
sat = 1 + math.ceil(dist * 100) / 100.0
if logfile:
logfile.write(
"Increasing saturation of actual "
"primaries for PCS candidate to "
"{:0.0f}%...\n".format(round(sat * 100))
)
for i, channel in enumerate("rgb"):
# x, y, Y = result.tags[channel + "XYZ"].pcs.xyY
x, y, Y = xyYrgb[i]
if logfile:
logfile.write(
"{} xy {:.6f} {:.6f} -> ".format(channel.upper(), x, y)
)
x, y, Y = colormath.xyYsaturation(x, y, Y, wx, wy, sat)
if logfile:
logfile.write("{:.6f} {:.6f}\n".format(x, y))
xyYrgb[i] = x, y, Y
(rx, ry, rY), (gx, gy, gY), (bx, by, bY) = xyYrgb
mtx = colormath.rgb_to_xyz_matrix(rx, ry, gx, gy, bx, by, XYZwp)
for i, (channel, components) in enumerate(rgb):
X, Y, Z = mtx * components
comp_rgb_space.append(colormath.XYZ2xyY(X, Y, Z))
comp_rgb_space.append("PCS candidate based on actual primaries")
rgb_spaces.append(comp_rgb_space)
# A colorspace that encompasses Rec709. Uses Rec. 2020 blue.
rgb_spaces.append(
[
2.2,
"D50",
[0.68280181011, 0.315096403371, 0.224182128906],
[0.310096375087, 0.631250246526, 0.73258972168],
[0.129244796433, 0.0471357502953, 0.0432281494141],
"Rec709-encompassing, variant 1",
]
)
# A colorspace that encompasses Rec709. Uses slightly different
# primaries (based on WLED and B+RG LED) than variant 1.
rgb_spaces.append(
[
2.2,
"D50",
[0.664580313612, 0.329336320112, 0.228820800781],
[0.318985161632, 0.644740328564, 0.742568969727],
[0.143284983488, 0.0303535582465, 0.0286102294922],
"Rec709-encompassing, variant 2",
]
)
# A colorspace that encompasses Rec709 with imaginary red and blue
rgb_spaces.append(
[
2.2,
"D50",
[0.672053135694, 0.331936091367, 0.250122070312],
[0.319028093312, 0.644705491217, 0.710144042969],
[0.113899645442, 0.0424325604954, 0.0396270751953],
"Rec709-encompassing, variant 3",
]
)
# A colorspace that encompasses Rec709. Uses red and blue from
# variant 2, but a slightly different green primary.
rgb_spaces.append(
[
2.2,
"D50",
[0.664627993657, 0.329338357942, 0.244033813477],
[0.300155771607, 0.640946446796, 0.728302001953],
[0.143304213944, 0.0303410650333, 0.0276641845703],
"Rec709-encompassing, variant 4",
]
)
# A colorspace with Plasma-like primaries. Uses Rec. 2020 blue.
rgb_spaces.append(
[
2.2,
"D50",
[0.692947816539, 0.30857396028, 0.244430541992],
[0.284461719244, 0.70017174365, 0.709167480469],
[0.129234824405, 0.0471509419335, 0.0464019775391],
"SMPTE-431-2/DCI-P3-encompassing, variant 1",
]
)
# A colorspace that encompasses both AdobeRGB and NTSC1953. Uses
# Rec. 2020 blue.
rgb_spaces.append(
[
2.2,
"D50",
[0.680082575358, 0.319746686121, 0.314331054688],
[0.200003470174, 0.730003123156, 0.641983032227],
[0.129238369699, 0.0471305063812, 0.0436706542969],
"AdobeRGB-NTSC1953-hybrid, variant 1",
]
)
# A colorspace with Plasma-like primaries. Uses Rec. 2020 blue.
# More saturated green primary than variant 1.
rgb_spaces.append(
[
2.2,
"D50",
[0.692943297796, 0.308579731457, 0.268966674805],
[0.249088838269, 0.730263586072, 0.684844970703],
[0.129230721306, 0.047147329564, 0.0461883544922],
"SMPTE-431-2/DCI-P3-encompassing, variant 2",
]
)
# A colorspace that encompasses both AdobeRGB and NTSC1953.
# Different, more saturated primaries than variant 1.
rgb_spaces.append(
[
2.2,
"D50",
[0.700603882817, 0.299296301842, 0.274520874023],
[0.200006562972, 0.75, 0.697494506836],
[0.143956453416, 0.0296952711131, 0.0279693603516],
"AdobeRGB-NTSC1953-hybrid, variant 2",
]
)
# A colorspace that encompasses DCI P3 with imaginary red and blue
rgb_spaces.append(
[
2.2,
"D50",
[0.699964323939, 0.312334528794, 0.253814697266],
[0.284337791321, 0.68212854805, 0.73779296875],
[0.098165262763, 0.00937830372063, 0.00839233398438],
"SMPTE-431-2/DCI-P3-encompassing, variant 3",
]
)
# A colorspace that encompasses DCI P3 with imaginary red and blue
# (red and blue are practically the same as Rec2020-encompassing)
rgb_spaces.append(
[
2.2,
"D50",
[0.717267960557, 0.29619267491, 0.231002807617],
[0.284277504105, 0.682088122605, 0.760604858398],
[0.0981978292034, 0.00940337224384, 0.00840759277344],
"SMPTE-431-2/DCI-P3-encompassing, variant 4",
]
)
# Rec. 2020
rgb_spaces.append(
[
2.2,
"D50",
[0.7084978651, 0.293540723619, 0.279037475586],
[0.190200063067, 0.775375775201, 0.675354003906],
[0.129244405192, 0.0471399056886, 0.0456085205078],
"Rec2020",
]
)
# A colorspace that encompasses Rec2020 with imaginary primaries
rgb_spaces.append(
[
2.2,
"D50",
[0.71715243505, 0.296225595183, 0.291244506836],
[0.191129060214, 0.795212673762, 0.700057983398],
[0.0981403936826, 0.00939694681658, 0.00869750976562],
"Rec2020-encompassing",
]
)
if rgb_space:
rgb_space = list(rgb_space[:5])
rgb_spaces = [rgb_space + ["Custom"]]
# Find smallest candidate that encompasses space defined by actual
# primaries
if logfile:
logfile.write("Checking for suitable PCS candidate...\n")
pcs_candidate = None
pcs_candidates = []
XYZsrgb = []
for channel, components in rgb:
XYZsrgb.append(
colormath.adapt(
*colormath.RGB2XYZ(*components),
whitepoint_source="D65",
cat=cat,
)
)
XYZrgb_sequence = [XYZrgb, XYZsrgb]
for rgb_space in rgb_spaces:
if rgb_space[1] not in ("D50", colormath.get_whitepoint("D50")):
# Adapt to D50
for i in range(3):
X, Y, Z = colormath.xyY2XYZ(*rgb_space[2 + i])
X, Y, Z = colormath.adapt(X, Y, Z, rgb_space[1], cat=cat)
rgb_space[2 + i] = colormath.XYZ2xyY(X, Y, Z)
rgb_space[1] = "D50"
extremes = []
skip = False
for XYZrgb in XYZrgb_sequence:
for i, color in enumerate(["red", "green", "blue"]):
RGB = colormath.XYZ2RGB(
XYZrgb[i][0],
XYZrgb[i][1],
XYZrgb[i][2],
rgb_space=rgb_space,
clamp=False,
)
maxima = max(RGB)
minima = min(RGB)
if minima < 0:
maxima += abs(minima)
if XYZrgb is XYZsrgb:
if maxima > 1 and color == "blue":
# We want our PCS candiate to contain Rec. 709
# blue, which may not be the case for our
# candidate based off the actual display
# primaries. Blue region is typically the one
# where the most artifacts would be visible in
# color conversions if the source blue is
# clipped
if logfile:
logfile.write(
"Skipping {} because it does "
"not encompass Rec. 709 {}\n".format(
rgb_space[-1], color
)
)
skip = True
break
else:
extremes.append(maxima)
if skip:
break
if skip:
continue
# Check area % (in xy for simplicity's sake)
xyYrgb = rgb_space[2:5]
area2 = 0.5 * abs(
sum(
x0 * y1 - x1 * y0
for ((x0, y0, Y0), (x1, y1, Y1)) in zip(
xyYrgb, xyYrgb[1:] + [xyYrgb[0]]
)
)
)
if logfile:
logfile.write(
"{} fit: {:.2f} (area: {:.2f}%)\n".format(
rgb_space[-1],
round(1.0 / max(extremes), 2),
area1 / area2 * 100,
)
)
pcs_candidates.append((area1 / area2, 1.0 / max(extremes), rgb_space))
# Check if tested RGB space contains actual primaries
if round(max(extremes), 2) <= 1.0:
break
XYZrgb = []
if not pcs_candidate and False: # NEVER?
# Create quick medium quality shaper+matrix profile and use the
# matrix from that
if logfile:
logfile.write(
"No suitable PCS candidate. Computing best fit matrix...\n"
)
# Lookup small testchart so profile computation finishes quickly
ti1name = "ti1/d3-e4-s5-g52-m5-b0-f0.ti1"
ti1 = get_data_path(ti1name)
if not ti1:
raise Error(lang.getstr("file.missing", ti1name))
ti1, ti3, gray = self.ti1_lookup_to_ti3(
ti1, profile, intent="a", white_patches=1
)
dirname, basename = os.path.split(profile.fileName)
basepath = os.path.join(
dirname, f".{os.path.splitext(basename)[0]}-MTX.tmp"
)
ti3.write(f"{basepath}.ti3")
# Calculate profile
cmd, args = get_argyll_util("colprof"), ["-v", "-qm", "-as", basepath]
result = self.exec_cmd(
cmd,
args,
capture_output=True,
sessionlogfile=getattr(self, "sessionlogfile", None),
)
if isinstance(result, Exception):
raise result
if result:
mtx = ICCProfile(f"{basepath}{profile_ext}")
for column in "rgb":
tag = mtx.tags.get(column + "XYZ")
if isinstance(tag, XYZType):
XYZrgb.append(list(tag.values()))
# os.remove(basepath + ".ti3")
# os.remove(basepath + profile_ext)
if not XYZrgb:
raise Error(
lang.getstr(
"profile.required_tags_missing", "rXYZ/gXYZ/bXYZ"
)
)
pcs_candidate = "BestFit"
else:
# If clutres is -1 (auto), set it depending on area coverage
if clutres == -1:
if area1 / area2 <= 0.51:
clutres = 45
elif area1 / area2 <= 0.73:
clutres = 33
# Use PCS candidate with best fit
# (best fit is the smallest fit greater or equal 1 and
# largest possible coverage)
pcs_candidates.sort(key=lambda row_: (-row_[0], row_[1]))
for coverage, fit, rgb_space in pcs_candidates:
if round(fit, 2) >= 1:
break
if logfile:
logfile.write("Using primaries: {}\n".format(rgb_space[-1]))
for channel, components in rgb:
XYZrgb.append(colormath.RGB2XYZ(*components, rgb_space=rgb_space))
pcs_candidate = rgb_space[-1]
for i in range(3):
logfile.write(
"Using {} XYZ: {:.4f} {:.4f} {:.4f}\n".format(
*(("RGB"[i],) + tuple(XYZrgb[i]))
)
)
if profile.connectionColorSpace == b"XYZ":
# Construct the final matrix
Xr, Yr, Zr = XYZrgb[0]
Xg, Yg, Zg = XYZrgb[1]
Xb, Yb, Zb = XYZrgb[2]
if logfile:
logfile.write(
"R+G+B XYZ: {:.4f} {:.4f} {:.4f}\n".format(
Xr + Xg + Xb,
Yr + Yg + Yb,
Zr + Zg + Zb,
)
)
m1 = colormath.Matrix3x3(
((Xr, Xg, Xb), (Yr, Yg, Yb), (Zr, Zg, Zb))
).inverted()
matrices.append(m1)
Sr, Sg, Sb = m1 * XYZwp
if logfile:
logfile.write(
"Correction factors: {:.4f} {:.4f} {:.4f}\n".format(Sr, Sg, Sb)
)
m2 = colormath.Matrix3x3(
(
(Sr * Xr, Sg * Xg, Sb * Xb),
(Sr * Yr, Sg * Yg, Sb * Yb),
(Sr * Zr, Sg * Zg, Sb * Zb),
)
).inverted()
matrices.append(m2)
matrices.append(m3)
for m, matrix in enumerate(matrices):
if logfile:
logfile.write("Matrix {:d}:\n".format(m + 1))
for row in matrix:
logfile.write("{}\n".format(repr(row)))
itable.matrix = m2 * m3
if logfile:
logfile.write("Final matrix:\n")
for row in itable.matrix:
logfile.write("{}\n".format(repr(row)))
if logfile:
logfile.write("Applying matrix to input curve XYZ values...\n")
# Apply matrix
rX = []
rY = []
rZ = []
for i in vrange:
X, Y, Z = fpX[i], fpY[i], fpZ[i]
X, Y, Z = m2 * (X, Y, Z)
rX.append(X)
rY.append(Y)
rZ.append(Z)
interp = (
colormath.Interp(xpR, rX),
colormath.Interp(xpG, rY),
colormath.Interp(xpB, rZ),
)
rinterp = (
colormath.Interp(rX, xpR),
colormath.Interp(rY, xpG),
colormath.Interp(rZ, xpB),
)
Linterp = None
else:
Lscale = 65280.0 / 65535.0
oldmin = (xpR[0] + xpG[0] + xpB[0]) / 3.0
oldmax = (xpR[-1] + xpG[-1] + xpB[-1]) / 3.0
oldrange = oldmax - oldmin
newmin = 0.0
newmax = 100
newrange = newmax - newmin
xpL = []
for i in vrange:
v = (xpR[i] + xpG[i] + xpB[i]) / 3.0
v = max((((v - oldmin) * newrange) / oldrange) + newmin, 0)
xpL.append(v)
Linterp = colormath.Interp(xpL, fpL)
rLinterp = colormath.Interp(fpL, xpL)
interp = None
# Set input curves
# Apply inverse TRC to input values to distribute them
# optimally across cLUT grid points
if logfile:
logfile.write("Generating input curves...\n")
itable.input = [[], [], []]
for j in vrange:
if self.thread_abort:
raise Info(lang.getstr("aborted"))
if profile.connectionColorSpace == b"XYZ":
v = [rinterp[i](j / maxval) for i in range(3)]
else:
# CIELab PCS encoding
v = [rLinterp(j / (maxval * Lscale) * 100) / 100.0]
v.extend([j / maxval] * 2)
for i in range(len(itable.input)):
itable.input[i].append(min(v[i] * 65535, 65535))
if logfile and j % math.floor(maxval / 100.0) == 0:
logfile.write("\r{:0.0f}%".format(round(j / maxval * 100)))
if logfile:
logfile.write("\n")
if False and method and not bpc:
# Force the blackpoint - NEVER
if logfile:
logfile.write("Forcing B2A input curve blackpoint...\n")
XYZbp_m = m2 * XYZbp
for i in range(3):
black_index = int(math.ceil(maxval * XYZbp_m[i]))
if logfile:
logfile.write(f"Channel #{i:d}\n")
for j in range(black_index + 1):
v = 0
if logfile:
logfile.write(
"#{:d} {:d} -> {:d}\n".format(j, itable.input[i][j], v)
)
itable.input[i][j] = v
if only_input_curves:
# We are done
return True
if clutres == -1:
# Auto
if pcs == "l" and smooth:
# Counteract L*a*b* cLUT accuracy loss when smoothing.
# With a res of 45, about the same accuracy as when using
# colprof B2A with a res of 33 and no smoothing.
# This is not necessary with XYZ cLUT as the accuracy is always
# higher than colprof baseline due to restricting the XYZ space
# with a matrix.
clutres = 45
else:
clutres = 33
step = 1.0 / (clutres - 1.0)
do_lookup = True
if do_lookup:
# Generate inverse table lookup input values
# Use slightly less than equal the amount of CPUs for workers
# for best utilization (each worker has 2 xicclu sub-processes)
num_cpus = cpu_count()
num_workers = min(max(num_cpus, 1), clutres)
if num_cpus > 2:
num_workers = int(num_workers * 0.75)
if logfile:
logfile.write(
"Generating {}{:d} table lookup input values...\n".format(
source, tableno
)
)
logfile.write(f"cLUT grid res: {clutres:d}\n")
logfile.write(
"Looking up input values through "
f"{source}{tableno:d} table ({num_workers:d} workers)...\n"
)
logfile.write(
"{} CAM Jab for clipping\n".format(
use_cam_clipping and "Using" or "Not using"
)
)
idata = []
odata1 = []
odata2 = []
threshold = int((clutres - 1) * 0.75)
threshold2 = int((clutres - 1) / 3)
for slices in pool_slice(
_mp_generate_B2A_clut,
list(range(clutres)),
(
profile.fileName,
intent,
direction,
pcs,
use_cam_clipping,
clutres,
step,
threshold,
threshold2,
interp,
Linterp,
m2,
XYZbp,
XYZwp,
bpc,
lang.getstr("aborted"),
),
{},
num_workers,
self.thread_abort,
logfile,
num_batches=clutres // 9,
):
for i, data in enumerate((idata, odata1, odata2)):
data.extend(slices[i])
if logfile:
logfile.write("\n")
if logfile and (pcs == "x" or clutres // 2 != clutres / 2.0):
if pcs == "x":
iXYZbp = idata[0]
iXYZwp = idata[-1]
logfile.write(f"Input black XYZ: {iXYZbp}\n")
logfile.write(f"Input white XYZ: {iXYZwp}\n")
else:
iLabbp = idata[clutres * (clutres // 2) + clutres // 2]
iLabwp = idata[
(
clutres**2 * (clutres - 1)
+ clutres * (clutres // 2)
+ clutres // 2
)
]
logfile.write(f"Input black L*a*b*: {iLabbp}\n")
logfile.write(f"Input white L*a*b*: {iLabwp}\n")
if not use_cam_clipping:
odata = odata1
elif pcs == "l":
odata = odata2
else:
# Linearly interpolate the crossover to CAM Jab clipping region
cam_diag = False
odata = []
j, k = 0, 0
r = float(threshold - threshold2)
for a in range(clutres):
for b in range(clutres):
for c in range(clutres):
if a <= threshold and b <= threshold and c <= threshold:
v = odata1[j]
j += 1
if a > threshold2 or b > threshold2 or c > threshold2:
d = max(a, b, c)
if cam_diag:
v = [100.0, 100.0, 0.0]
v2 = [0.0, 100.0, 100.0]
else:
v2 = odata2[k]
k += 1
for i, n in enumerate(v):
v[i] *= (threshold - d) / r
v2[i] *= 1 - (threshold - d) / r
v[i] += v2[i]
else:
if cam_diag:
v = [100.0, 0.0, 100.0]
else:
v = odata2[k]
k += 1
odata.append(v)
numrows = len(odata)
if numrows != clutres**3:
raise ValueError(
f"Number of cLUT entries ({numrows}) exceeds cLUT res "
f"maximum ({clutres}^3 = {clutres**3})"
)
if logfile and (pcs == "x" or clutres // 2 != clutres / 2.0):
if pcs == "x":
oRGBbp = odata[0]
oRGBwp = odata[-1]
else:
oRGBbp = odata[clutres * (clutres // 2) + clutres // 2]
oRGBwp = odata[
(
clutres**2 * (clutres - 1)
+ clutres * (clutres // 2)
+ clutres // 2
)
]
logfile.write(
"Output black RGB: {:.4f} {:.4f} {:.4f}\n".format(*oRGBbp)
)
logfile.write(
"Output white RGB: {:.4f} {:.4f} {:.4f}\n".format(*oRGBwp)
)
odata = [[n / 100.0 for n in v] for v in odata]
# Fill cCLUT
itable.clut = []
if logfile:
logfile.write("Filling cLUT...\n")
if not do_lookup:
# Linearly scale RGB
for R in range(clutres):
if self.thread_abort:
raise Info(lang.getstr("aborted"))
for G in range(clutres):
itable.clut.append([])
for B in range(clutres):
itable.clut[-1].append([v * step * 65535 for v in (R, G, B)])
if logfile:
logfile.write(
"\r{:0.0f}%".format(
round((R * G * B) / ((clutres - 1.0) ** 3) * 100)
)
)
else:
for i, RGB in enumerate(odata):
if i % clutres == 0:
if self.thread_abort:
raise Info(lang.getstr("aborted"))
itable.clut.append([])
if logfile:
logfile.write(
"\r{:0.0f}%".format(round(i / (numrows - 1.0) * 100))
)
# Set RGB black and white explicitly
if pcs == "x":
if i == 0:
RGB = 0, 0, 0
elif i == numrows - 1.0:
RGB = 1, 1, 1
elif clutres // 2 != clutres / 2.0:
# For CIELab cLUT, white- and black point will only
# fall on a cLUT point if uneven cLUT res
if i == clutres * (clutres // 2) + clutres // 2:
# if raw_input("{:d} {}".format(i, repr(RGB))):
RGB = 0, 0, 0
elif i == (
clutres**2 * (clutres - 1)
+ clutres * (clutres // 2)
+ clutres // 2
):
# if raw_input("{:d} {}".format(i, repr(RGB))):
RGB = 1, 1, 1
itable.clut[-1].append([v * 65535 for v in RGB])
if logfile:
logfile.write("\n")
if getcfg("profile.b2a.hires.diagpng") and filename:
# Generate diagnostic images
fname, ext = os.path.splitext(filename)
for suffix, table in [
("pre", profile.tags.get(f"B2A{tableno}")),
("post", itable),
]:
if table:
table.clut_writepng(f"{fname}.B2A{tableno:d}.{suffix}.CLUT.png")
# Update profile
profile.tags[f"B2A{tableno:d}"] = itable
if method == 2:
if logfile:
logfile.write("Generating output curves...\n")
# Output curve maps <clutres> RGB to <numentries> RGB
# Lookup neutral (except near black) XYZ -> RGB
oRGB = self.xicclu(
profile,
oXYZ,
intent,
{"A2B": "if", "B2A": "b"}[source],
pcs="x",
get_clip=True,
)
# Deal with values that got clipped (below black as well as white)
do_low_clip = True
for i, values in enumerate(oRGB):
if values[3] is True or i == 0:
if do_low_clip:
# Set to black
self.log(
"Setting curve entry #{:d} ({:.6f} {:.6f} {:.6f}) to "
"black because it got clipped".format(
*((i,) + tuple(values[:3]))
)
)
values[:] = [0.0, 0.0, 0.0]
elif i == maxval and [round(v, 4) for v in values[:3]] == [1, 1, 1]:
# Set to white
self.log(
"Setting curve entry #{:d} ({:.6f} {:.6f} {:.6f}) to "
"white because it got clipped".format(
*((i,) + tuple(values[:3]))
)
)
values[:] = [1.0, 1.0, 1.0]
else:
# First non-clipping value disables low clipping
do_low_clip = False
if len(values) > 3:
values.pop()
ocurves = [[], [], []]
for i, RGB in enumerate(oRGB):
for j, v in enumerate(RGB):
ocurves[j].append(v)
ointerp = []
for i, ocurve in enumerate(ocurves):
olen = len(ocurve)
ointerp_i = colormath.Interp(
[j / (olen - 1.0) for j in range(olen)], ocurve, use_numpy=True
)
ocurve_i = [ointerp_i(j / (clutres - 1.0)) for j in range(clutres)]
ocurve_i = colormath.interp_resize(
ocurve_i, len(ocurve), use_numpy=True
)
ointerp_o = colormath.Interp(ocurve_i, ocurve, use_numpy=True)
ointerp.append(ointerp_o)
# Set output curves
itable.output = [[], [], []]
numentries = 256
maxval = numentries - 1.0
for i in range(len(itable.output)):
for j in range(numentries):
v = j / maxval
if method == 2:
v = ointerp[i](v)
itable.output[i].append(v * 65535)
if smooth:
self.smooth_B2A(
profile, tableno, getcfg("profile.b2a.hires.diagpng"), filename, logfile
)
return True
def smooth_B2A(self, profile, tableno, diagpng=2, filename=None, logfile=None):
"""Apply extra smoothing to the cLUT"""
itable = profile.tags.get(f"B2A{tableno:d}")
if not itable:
return False
itable.smooth2(diagpng, profile.connectionColorSpace, filename, logfile)
return True
def get_device_id(
self,
quirk=False,
use_serial_32=True,
truncate_edid_strings=False,
omit_manufacturer=False,
query=False,
):
"""Get org.freedesktop.ColorManager device key"""
if not self.display_edid or config.is_virtual_display():
return None
display_no = max(0, min(len(self.displays) - 1, getcfg("display.number") - 1))
edid = self.display_edid[display_no]
if not edid:
# Fall back to XrandR name
if not (
not quirk
and use_serial_32
and not truncate_edid_strings
and not omit_manufacturer
):
return
if RDSMM:
display = RDSMM.get_display(display_no)
if display:
xrandr_name = display.get("xrandr_name")
if xrandr_name:
edid = {"monitor_name": xrandr_name}
elif os.getenv("XDG_SESSION_TYPE") == "wayland":
# Preliminary Wayland support under non-GNOME desktops.
# This still needs a lot of work.
device_ids = colord.get_display_device_ids()
if device_ids and display_no < len(device_ids):
return device_ids[display_no]
return colord.device_id_from_edid(
edid,
quirk=quirk,
use_serial_32=use_serial_32,
truncate_edid_strings=truncate_edid_strings,
omit_manufacturer=omit_manufacturer,
query=query,
)
def get_display(self):
"""Get the currently configured display.
Returned is the Argyll CMS dispcal/dispread -d argument
"""
display_name = config.get_display_name(None, True)
if display_name == "Web @ localhost":
return "web:{:d}".format(getcfg("webserver.portnumber"))
if display_name == "madVR":
return "madvr"
if display_name == "Untethered":
return "0"
if display_name == "Resolve":
if (
self.argyll_virtual_display
# and os.getenv("XDG_SESSION_TYPE") == "wayland"
):
return self.argyll_virtual_display
else:
return "1"
if display_name == "Prisma" and not defaults["patterngenerator.prisma.argyll"]:
if self.argyll_virtual_display:
return self.argyll_virtual_display
else:
return "1"
if display_name == "Prisma":
host = getcfg("patterngenerator.prisma.host")
try:
host = socket.gethostbyname(host)
except socket.error:
pass
return f"prisma:{host}"
if display_name.startswith("Chromecast "):
return "cc:{}".format(display_name.split(":")[0].split(None, 1)[1].strip())
if display_name.startswith("Prisma "):
return "prisma:{}".format(display_name.split("@")[-1].strip())
if self._use_patternwindow:
# Preliminary Wayland support. This still needs a lot
# of work as Argyll doesn't support Wayland natively,
# so we use virtual display to drive our own patch window.
return self.argyll_virtual_display
display_no = min(len(self.displays), getcfg("display.number")) - 1
display = str(display_no + 1)
if (
(sys.platform not in ("darwin", "win32") or test)
and (self.has_separate_lut_access() or getcfg("use_separate_lut_access"))
and (
not getcfg("display_lut.link")
or (display_no > -1 and not self.lut_access[display_no])
)
):
display_lut_no = min(len(self.displays), getcfg("display_lut.number")) - 1
if display_lut_no > -1 and not self.lut_access[display_lut_no]:
for display_lut_no, disp in enumerate(self.lut_access):
if disp:
break
display += "," + str(display_lut_no + 1)
return display
def get_display_edid(self):
"""Return EDID of currently configured display"""
n = getcfg("display.number") - 1
if 0 <= n < len(self.display_edid):
return self.display_edid[n]
return {}
def get_display_name(
self, prepend_manufacturer=False, prefer_edid=False, remove_manufacturer=True
):
"""Return name of currently configured display"""
n = getcfg("display.number") - 1
if 0 <= n < len(self.display_names):
display = []
manufacturer = None
display_name = None
if prefer_edid:
edid = self.get_display_edid()
manufacturer = edid.get("manufacturer")
display_name = edid.get(
"monitor_name", edid.get("ascii", str(edid.get("product_id") or ""))
)
if not manufacturer:
manufacturer = self.display_manufacturers[n]
if not display_name:
display_name = self.display_names[n]
if manufacturer:
manufacturer = colord.quirk_manufacturer(manufacturer)
if prepend_manufacturer:
if manufacturer.lower() not in display_name.lower():
display.append(manufacturer)
elif remove_manufacturer:
start = display_name.lower().find(manufacturer.lower())
if start > -1:
display_name = (
display_name[:start]
+ display_name[start + len(manufacturer) :]
).lstrip()
display_name = re.sub(r"^[^([{\w]+", "", display_name)
display.append(display_name)
return " ".join(display)
return ""
def get_display_name_short(self, prepend_manufacturer=False, prefer_edid=False):
"""Return shortened name of configured display (if possible)
If name can't be shortened (e.g. because it's already 10 characters
or less), return full string
"""
display_name = self.get_display_name(prepend_manufacturer, prefer_edid)
if len(display_name) > 10:
maxweight = 0
for part in re.findall(
r"[^\s_]+(?:\s*\d+)?", re.sub(r"\([^)]+\)", "", display_name)
):
digits = re.search(r"\d+", part)
if digits:
# Weigh parts with digits higher than those without
chars = re.sub(r"\d+", "", part)
weight = len(chars) + len(digits.group()) * 5
else:
# Weigh parts with uppercase letters higher than those without
chars = ""
for char in part:
if char.lower() != char:
chars += char
weight = len(chars)
if chars and weight >= maxweight:
# Weigh parts further to the right higher
display_name = re.sub(r"^[^([{\w]+", "", part)
maxweight = weight
# Shortname should not contain any spaces.
display_name = re.sub(r"[\s]+", "_", display_name)
return display_name
def get_dispwin_display_profile_argument(self, display_no=0):
"""Return argument corresponding to the display profile for use with dispwin.
Will either return '-L' (use current profile) or a filename.
"""
arg = "-L"
try:
profile = get_display_profile(display_no)
except Exception as exception:
print(exception)
return arg
else:
if not profile:
return arg
if profile.fileName:
prefix = os.path.basename(profile.fileName)
else:
prefix = (
make_filename_safe(profile.getDescription(), concat=False)
+ profile_ext
)
prefix += "-"
if profile.version >= 4 and not profile.convert_iccv4_tags_to_iccv2():
print(
"\n".join(
[lang.getstr("profile.iccv4.unsupported"), profile.getDescription()]
)
)
elif not profile.fileName:
fd, profile.fileName = tempfile.mkstemp("", prefix)
stream = os.fdopen(fd, "wb")
profile.write(stream)
stream.close()
atexit.register(os.remove, profile.fileName)
return profile.fileName or arg
def update_display_name_manufacturer(
self, ti3, display_name=None, display_manufacturer=None, write=True
):
"""Update display name and manufacturer in colprof arguments
embedded in 'ARGYLL_COLPROF_ARGS' section in a TI3 file."""
options_colprof = []
if not display_name and not display_manufacturer:
# Note: Do not mix'n'match display name and manufacturer from
# different sources
try:
ti3_options_colprof = get_options_from_ti3(ti3)[1]
except (
IOError,
CGATSInvalidError,
CGATSInvalidOperationError,
CGATSKeyError,
CGATSTypeError,
CGATSValueError,
) as exception:
print(exception)
ti3_options_colprof = []
for option in ti3_options_colprof:
if option[0] == "M":
display_name = option.split(None, 1)[-1][1:-1]
elif option[0] == "A":
display_manufacturer = option.split(None, 1)[-1][1:-1]
if not display_name and not display_manufacturer:
# Note: Do not mix'n'match display name and manufacturer from
# different sources
edid = self.display_edid[
max(0, min(len(self.displays), getcfg("display.number")) - 1)
]
display_name = edid.get(
"monitor_name", edid.get("ascii", str(edid.get("product_id") or ""))
)
display_manufacturer = edid.get("manufacturer")
if not display_name and not display_manufacturer:
# Note: Do not mix'n'match display name and manufacturer from
# different sources
display_name = self.get_display_name()
if display_name:
options_colprof.append("-M")
options_colprof.append(display_name)
if display_manufacturer:
options_colprof.append("-A")
options_colprof.append(display_manufacturer)
if write:
# Add dispcal and colprof arguments to ti3
if is_ccxx_testchart():
options_dispcal = []
else:
options_dispcal = self.options_dispcal
ti3 = add_options_to_ti3(ti3, options_dispcal, options_colprof)
if ti3:
ti3.write()
return options_colprof
def get_instrument_features(self, instrument_name=None):
"""Return features of currently configured instrument"""
features = all_instruments.get(
instrument_name or self.get_instrument_name(), {}
)
if test_require_sensor_cal:
features["sensor_cal"] = True
features["skip_sensor_cal"] = False
return features
def get_instrument_measurement_modes(
self, instrument_id=None, skip_ccxx_modes=True
):
"""Enumerate measurement modes supported by the instrument"""
if not instrument_id:
features = self.get_instrument_features()
instrument_id = features.get("id", self.get_instrument_name())
if instrument_id:
measurement_modes = self.measurement_modes.get(instrument_id, dict())
if not measurement_modes:
result = self.exec_cmd(
get_argyll_util("spotread"),
["-?"],
capture_output=True,
skip_scripts=True,
silent=True,
log_output=False,
)
if isinstance(result, Exception):
print(result)
# Need to get and remember output of spotread because calling
# self.get_technology_strings() will overwrite self.output
output = self.output
if test:
output.extend(
"""Measure spot values, Version 1.7.0_beta
Author: Graeme W. Gill, licensed under the GPL Version 2 or later
Diagnostic: Usage requested
usage: spotread [-options] [logfile]
-v Verbose mode
-s Print spectrum for each reading
-S Plot spectrum for each reading
-c listno Set communication port from the following list (default 1)
1 = 'COM13 (Klein K-10)'
2 = 'COM1'
3 = 'COM3'
4 = 'COM4'
-t Use transmission measurement mode
-e Use emissive measurement mode (absolute results)
-eb Use display white brightness relative measurement mode
-ew Use display white point relative chromatically adjusted mode
-p Use telephoto measurement mode (absolute results)
-pb Use projector white brightness relative measurement mode
-pw Use projector white point relative chromatically adjusted mode
-a Use ambient measurement mode (absolute results)
-f Use ambient flash measurement mode (absolute results)
-y F K-10: Factory Default [Default,CB1]
c K-10: Default CRT File
P K-10: Klein DLP Lux
E K-10: Klein SMPTE C
b K-10: TVL XVM245
d K-10: Klein LED Bk LCD
m K-10: Klein Plasma
p K-10: DLP Screen
o K-10: TVL LEM150
O K-10: Sony EL OLED
z K-10: Eizo CG LCD
L K-10: FSI 2461W
h K-10: HP DreamColor 2
1 K-10: LCD CCFL Wide Gamut IPS (LCD2690WUXi)
l|c Other: l = LCD, c = CRT
-I illum Set simulated instrument illumination using FWA (def -i illum):
M0, M1, M2, A, C, D50, D50M2, D65, F5, F8, F10 or file.sp]
-i illum Choose illuminant for computation of CIE XYZ from spectral data & FWA:
A, C, D50 (def.), D50M2, D65, F5, F8, F10 or file.sp
-Q observ Choose CIE Observer for spectral data or CCSS instrument:
1931_2 (def), 1964_10, S&B 1955_2, shaw, J&V 1978_2
(Choose FWA during operation)
-F filter Set filter configuration (if aplicable):
n None
p Polarising filter
6 D65
u U.V. Cut
-E extrafilterfile Apply extra filter compensation file
-x Display Yxy instead of Lab
-h Display LCh instead of Lab
-V Show running average and std. devation from ref.
-T Display correlated color temperatures and CRI
-N Disable auto calibration of instrument
-O Do one cal. or measure and exit
-H Start in high resolution spectrum mode (if available)
-X file.ccmx Apply Colorimeter Correction Matrix
-Y r|n Override refresh, non-refresh display mode
-Y R:rate Override measured refresh rate with rate Hz
-Y A Use non-adaptive integration time mode (if available).
-W n|h|x Override serial port flow control: n = none, h = HW, x = Xon/Xoff
-D [level] Print debug diagnostics to stderr
logfile Optional file to save reading results as text""".splitlines()
)
measurement_modes_follow = False
technology_strings = self.get_technology_strings()
for line in output:
line = line.strip()
if line.startswith("-y "):
line = line.lstrip("-y ")
measurement_modes_follow = True
elif line.startswith("-"):
measurement_modes_follow = False
parts = [v.strip() for v in line.split(None, 1)]
if measurement_modes_follow and len(parts) == 2:
measurement_mode, desc = parts
if measurement_mode not in (
string.digits[1:] + string.ascii_letters
):
# Ran out of selectors
continue
measurement_mode_instrument_id, desc = desc.split(":", 1)
desc = desc.strip()
if measurement_mode_instrument_id == instrument_id:
# Found a mode for our instrument
if (
re.sub(r"\s*\(.*?\)?$", "", desc)
in list(technology_strings.values()) + [""]
and skip_ccxx_modes
):
# This mode is supplied via CCMX/CCSS, skip
continue
desc = re.sub(r"\s*(?:File|\[[^\]]*\])", "", desc)
measurement_modes[measurement_mode] = desc
self.measurement_modes[instrument_id] = measurement_modes
return measurement_modes
return {}
def get_instrument_name(self):
"""Return name of currently configured instrument"""
try:
return self.instruments[getcfg("comport.number") - 1]
except IndexError:
return ""
def get_real_displays(self):
"""Get real (nonvirtual) displays"""
real_displays = []
for display_no in range(len(self.displays)):
if not config.is_virtual_display(display_no):
real_displays.append(display_no)
return real_displays
def get_technology_strings(self):
"""Return technology strings mapping (from ccxxmake -??)"""
if self.argyll_version < [1, 7]:
# Argyll ccxxmake before V1.7 didn't have the ability to list
# dtechs with -??
# Argyll V1.7 had different keys (we still want to use those for
# V1.7) with some duplicates
# Use Argyll V1.7.1 mapping (which has no duplicate keys) for
# Argyll before V1.7
return {
"c": "CRT",
"m": "Plasma",
"l": "LCD",
"1": "LCD CCFL",
"2": "LCD CCFL IPS",
"3": "LCD CCFL VPA",
"4": "LCD CCFL TFT",
"L": "LCD CCFL Wide Gamut",
"5": "LCD CCFL Wide Gamut IPS",
"6": "LCD CCFL Wide Gamut VPA",
"7": "LCD CCFL Wide Gamut TFT",
"e": "LCD White LED",
"8": "LCD White LED IPS",
"9": "LCD White LED VPA",
"d": "LCD White LED TFT",
"b": "LCD RGB LED",
"f": "LCD RGB LED IPS",
"g": "LCD RGB LED VPA",
"i": "LCD RGB LED TFT",
"h": "LCD RG Phosphor",
"j": "LCD RG Phosphor IPS",
"k": "LCD RG Phosphor VPA",
"n": "LCD RG Phosphor TFT",
"o": "LED OLED",
"a": "LED AMOLED",
"p": "DLP Projector",
"q": "DLP Projector RGB Filter Wheel",
"r": "DPL Projector RGBW Filter Wheel",
"s": "DLP Projector RGBCMY Filter Wheel",
"u": "Unknown",
}
result = self.exec_cmd(
get_argyll_util("ccxxmake"),
["-??"],
capture_output=True,
skip_scripts=True,
silent=True,
log_output=False,
)
if isinstance(result, Exception):
traceback.print_exc()
print(result)
return dict()
technology_strings = dict()
in_tech = False
for line in self.output:
if not (parts := line.strip().split(None, 1)):
continue
if (arg := parts.pop(0)) == "-t":
parts = parts[0].split(None, 1)
if len(parts) == 2:
arg = parts.pop(0)
in_tech = True
elif arg.startswith("-"):
in_tech = False
if in_tech and parts:
technology_strings[arg] = parts[0]
return technology_strings
def has_lut_access(self):
display_no = min(len(self.lut_access), getcfg("display.number")) - 1
return display_no > -1 and bool(self.lut_access[display_no])
def has_separate_lut_access(self):
"""Return True if separate LUT access is possible and needed."""
# Filter out Prisma, Resolve and Untethered
# IMPORTANT: Also make changes to display filtering in
# worker.Worker.enumerate_displays_and_ports
lut_access = self.lut_access[:-3]
if self.argyll_version >= [1, 6, 0]:
# Filter out madVR
lut_access = lut_access[:-1]
if self.argyll_version >= [1, 4, 0]:
# Filter out Web @ localhost
lut_access = lut_access[:-1]
return len(self.displays) > 1 and False in lut_access and True in lut_access
def import_colorimeter_corrections(self, cmd, args=None, asroot=False):
"""Import colorimeter corrections. cmd can be 'i1d3ccss', 'spyd4en'
or 'oeminst'"""
if not args:
args = []
if (is_superuser() or asroot) and "-Sl" not in args:
# If we are root or need root privs anyway, install to local
# system scope
args.insert(0, "-Sl")
return self.exec_cmd(
cmd,
["-v"] + args,
capture_output=True,
skip_scripts=True,
silent=False,
asroot=asroot,
)
def import_edr(self, args=None, asroot=False):
"""Import X-Rite .edr files"""
return self.import_colorimeter_corrections(
get_argyll_util("i1d3ccss"), args, asroot
)
def import_spyd4cal(self, args=None, asroot=False):
"""Import Spyder4/5 calibrations to spy4cal.bin"""
return self.import_colorimeter_corrections(
get_argyll_util("spyd4en"), args, asroot
)
def install_3dlut(self, path, filename=None):
if getcfg("3dlut.format") == "madVR" and madvr:
# Install (load) 3D LUT using madTPG
print(path)
hdr_to_sdr = not getcfg("3dlut.hdr_display")
# Get parameters from actual 3D LUT file
h3dlut = madvr.H3DLUT(path)
xy = h3dlut.parametersData.get("Input_Primaries", [])
smpte2084 = h3dlut.parametersData.get("Input_Transfer_Function") == "PQ"
if len(xy) < 6:
# Should never happen
return Error("madVR 3D LUT doesn't contain Input_Primaries")
slot, rgb_space_name = h3dlut.source_colorspace
if slot is not None:
print("Input primaries match", rgb_space_name)
else:
return Error(
lang.getstr(
"3dlut.madvr.colorspace.unsupported",
[rgb_space_name or lang.getstr("unknown")] + list(xy[:6]),
)
)
args = [path, True, slot]
if smpte2084:
methodname = "load_hdr_3dlut_file"
args.append(hdr_to_sdr)
lut3d_section = "HDR"
if hdr_to_sdr:
lut3d_section += " to SDR"
else:
methodname = "load_3dlut_file"
lut3d_section = "calibration"
print(
"Installing madVR 3D LUT for {} slot {:d} ({})...".format(
lut3d_section, slot, rgb_space_name
)
)
try:
# Connect & load 3D LUT
if self.madtpg_connect() and getattr(self.madtpg, methodname)(*args):
raise Info(lang.getstr("3dlut.install.success"))
else:
raise Error(lang.getstr("3dlut.install.failure"))
except Exception as exception:
return exception
elif config.get_display_name(None, True) == "Prisma":
try:
# Use Prisma HTTP REST interface to upload 3D LUT
if not self.patterngenerator:
self.setup_patterngenerator()
self.patterngenerator.connect()
# Check preset. If it has a custom LUT assigned, we delete the
# currently assigned LUT before uploading, unless its filename
# is the same as the LUT to be uploaded, in which case it will
# be simply overwritten, and unless the custom LUT is still
# assigned to another preset as well.
presetname = getcfg("patterngenerator.prisma.preset")
if False:
# NEVER?
# Check all presets for currently assigned LUT
# Check the preset we're going to use for the upload last
presetnames = [
name
for name in config.valid_values[
"patterngenerator.prisma.preset"
]
if name != presetname
]
else:
# Check only the preset we're going to use for the upload
presetnames = []
presetnames.append(presetname)
assigned_luts = {}
for presetname in presetnames:
self.log("Loading Prisma preset", presetname)
preset = self.patterngenerator.load_preset(presetname)
assigned_lut = preset["v"].get("cube", "")
if assigned_lut not in assigned_luts:
assigned_luts[assigned_lut] = 0
assigned_luts[assigned_lut] += 1
# Only remove the currently assigned custom LUT if it's not
# assigned to another preset
if (
assigned_lut.lower().endswith(".3dl")
and assigned_luts[assigned_lut] == 1
):
remove = preset["v"]["cube"]
else:
remove = False
# Check total size of installed 3D LUTs. The Prisma has 1 MB of
# custom LUT storage, which is enough for 15 67 KB LUTs.
maxsize = 1024 * 67 * 15
installed = self.patterngenerator.get_installed_3dluts()
rawlen = len(installed["raw"])
size = 0
numinstalled = 0
for table in installed["v"]["tables"]:
if table.get("n") != remove:
size += table.get("s", 0)
numinstalled += 1
else:
rawlen -= len(
'{"n":"{}", "s":{:d}},'.format(
table["n"], table.get("s", 0)
)
)
filesize = os.stat(path).st_size
size_exceeded = size + filesize > maxsize
# NOTE that the total number of 3D LUT slots seems to be limited
# to 32, which includes built-in LUTs.
maxluts = 32
luts_exceeded = numinstalled >= maxluts
if size_exceeded or luts_exceeded:
if size_exceeded:
missing = size + filesize - maxsize
elif luts_exceeded:
missing = filesize * (numinstalled + 1 - maxluts)
else:
missing = size + filesize - 1024 * 67 * 2
raise Error(
lang.getstr(
"3dlut.holder.out_of_memory",
(
getcfg("patterngenerator.prisma.host"),
round(missing / 1024.0),
getcfg("patterngenerator.prisma.host"),
"Prisma",
),
)
)
if remove and remove != filename:
self.log("Removing currently assigned LUT", remove)
self.patterngenerator.remove_3dlut(remove)
self.log("Uploading LUT", path, "as", filename)
self.patterngenerator.load_3dlut_file(path, filename)
self.log("Setting LUT", filename)
self.patterngenerator.set_3dlut(filename)
self.log("Setting PrismaVue to zero")
self.patterngenerator.set_prismavue(0)
except Exception as exception:
return exception
return Info(lang.getstr("3dlut.install.success"))
else:
return Error(lang.getstr("3dlut.install.unsupported"))
def install_profile(
self, profile_path, capture_output=True, skip_scripts=False, silent=False
):
"""Install a profile by copying it to an appropriate location and
registering it with the system"""
colord_install = None
oy_install = None
argyll_install = self._install_profile_argyll(
profile_path, capture_output, skip_scripts, silent
)
if isinstance(argyll_install, str):
# The installed name may be different due to escaping non-ASCII
# chars (see prepare_dispwin), so get the profile path
profile_path = argyll_install
argyll_install = True
if sys.platform == "win32":
# Assign profile to active display
display_no = min(len(self.displays), getcfg("display.number")) - 1
monitors = util_win.get_real_display_devices_info()
moninfo = monitors[display_no]
displays = util_win.get_display_devices(moninfo["Device"])
active_display = util_win.get_active_display_device(None, displays)
if not active_display:
self.log(f"{appname}: Warning - no active display device!")
if not displays:
self.log(
f"{appname}: Warning - could not enumerate "
"display devices for {}!".format(moninfo["Device"])
)
elif active_display.DeviceKey != displays[0].DeviceKey:
self.log(f"{appname}: Setting profile for active display device...")
try:
set_display_profile(
os.path.basename(profile_path),
devicekey=active_display.DeviceKey,
)
except Exception as exception:
# Not a critical error. Only log the exception.
if (
isinstance(exception, EnvironmentError)
and not exception.filename
and not os.path.isfile(profile_path)
):
exception.filename = profile_path
self.log(
f"{appname}: Warning - could not set profile for "
"active display device:",
exception,
)
else:
self.log(f"{appname}: ...ok")
loader_install = None
profile = None
try:
profile = ICCProfile(profile_path)
except (IOError, ICCProfileInvalidError) as exception:
return exception
device_id = self.get_device_id(quirk=False, query=True)
if (
sys.platform not in ("darwin", "win32")
and not getcfg("dry_run")
and (
argyll_install is not True
or self.argyll_version < [1, 6]
or not os.getenv("ARGYLL_USE_COLORD")
or not dlopen("libcolordcompat.so")
)
and which("colormgr")
):
if device_id:
result = self._attempt_install_profile_colord(profile, device_id)
colord_install = result
if not device_id or result is not True:
gcm_import = bool(which("gcm-import"))
if gcm_import:
self._install_profile_gcm(profile)
# gcm-import doesn't seem to return a useful exit code or
# stderr output, so check for our profile
profilename = os.path.basename(profile.fileName)
for dirname in iccprofiles_home:
profile_install_path = os.path.join(dirname, profilename)
if os.path.isfile(profile_install_path):
colord_install = Warn(lang.getstr("profile.import.success"))
break
if which("oyranos-monitor") and self.check_display_conf_oy_compat(
getcfg("display.number")
):
if device_id:
profile_name = re.sub(r"[- ]", "_", device_id.lower()) + ".icc"
else:
profile_name = None
result = self._install_profile_oy(
profile_path, profile_name, capture_output, skip_scripts, silent
)
oy_install = result
if argyll_install is not True and (
(colord_install and not isinstance(colord_install, colord.CDError))
or oy_install is True
):
# Ignore Argyll install errors if colord or Oyranos install was
# succesful
argyll_install = None
# Check if atleast one of our profile install methods did return a
# result that is not an error
for result in (argyll_install, colord_install, oy_install):
check = result is True or isinstance(result, Warning)
if check:
break
# Only go on to create profile loader if profile loading on login
# isn't disabled in the config file, and we are not under Mac OS X
# (no loader required there), and if atleast one of our profile
# install methods did return a result that is not an error
if getcfg("profile.load_on_login") and sys.platform != "darwin" and check:
# Create profile loader. Failing to create it is a critical error
# under Windows if calibration loading isn't handled by the OS
# (this is checked), and also under Linux if colord profile install
# failed (colord handles loading otherwise)
check = sys.platform == "win32" or (
not colord_install or isinstance(colord_install, colord.CDError)
)
if getcfg("profile.install_scope") == "l" and sys.platform != "win32":
# We need a system-wide config file to store the path to
# the Argyll binaries for the profile loader
if not config.makecfgdir("system", self) or (
not config.writecfg("system", self) and check
):
# If the system-wide config dir could not be created,
# or the system-wide config file could not be written,
# error out if under Linux and colord profile install failed
return Error(lang.getstr("error.autostart_system"))
if sys.platform == "win32":
loader_install = self._install_profile_loader_win32(silent)
else:
loader_install = self._install_profile_loader_xdg(silent)
if loader_install is not True and check:
return loader_install
# Check if atleast one of our profile install methods returned a result
for result in (argyll_install, colord_install, oy_install):
if result is not None:
return argyll_install, colord_install, oy_install, loader_install
if not result:
# This should never happen
result = Error(lang.getstr("profile.install.error"))
return result
def install_argyll_instrument_conf(self, uninstall=False, filenames=None):
"""(Un-)install Argyll CMS instrument configuration under Linux"""
udevrules = "/etc/udev/rules.d"
hotplug = "/etc/hotplug"
if not os.path.isdir(udevrules) and not os.path.isdir(hotplug):
return Error(lang.getstr("udev_hotplug.unavailable"))
if not filenames:
filenames = get_argyll_instrument_config("installed" if uninstall else None)
if not filenames:
return Error(
"\n".join(
lang.getstr("file.missing", filename)
for filename in get_argyll_instrument_config("expected")
)
)
if uninstall:
backupbase = os.path.join(
config.datahome, "backup", strftime("%Y%m%dT%H%M%S")
)
for filename in filenames:
if filename.endswith(".rules"):
dst = udevrules
else:
dst = hotplug
if uninstall:
# Move file to backup location
backupdir = "".join([backupbase, os.path.dirname(filename)])
if not os.path.isdir(backupdir):
os.makedirs(backupdir)
cmd, args = "mv", [filename, "".join([backupbase, filename])]
else:
cmd, args = "cp", ["--remove-destination", filename]
args.append(os.path.join(dst, os.path.basename(filename)))
result = self.exec_cmd(
cmd, args, capture_output=True, skip_scripts=True, asroot=True
)
if result is not True:
break
elif not uninstall:
self.exec_cmd(
"chmod",
["0644", args[-1]],
capture_output=True,
skip_scripts=True,
asroot=True,
)
install_result = result
paths = ["/sbin", "/usr/sbin"]
paths.extend(getenvu("PATH", os.defpath).split(os.pathsep))
if not uninstall:
if not isinstance(result, Exception) and result:
# Add colord group if it does not exist
if "colord" not in [g.gr_name for g in grp.getgrall()]:
groupadd = which("groupadd", paths)
if groupadd:
result = self.exec_cmd(
groupadd,
["colord"],
capture_output=True,
skip_scripts=True,
asroot=True,
)
if not isinstance(result, Exception) and result:
# Add user to colord group if not yet a member
if "colord" not in getgroups(getpass.getuser(), True):
usermod = which("usermod", paths)
if usermod:
result = self.exec_cmd(
usermod,
["-a", "-G", "colord", getpass.getuser()],
capture_output=True,
skip_scripts=True,
asroot=True,
)
if install_result is True and dst == udevrules:
# Reload udev rules
udevadm = which("udevadm", paths)
if udevadm:
result = self.exec_cmd(
udevadm,
["control", "--reload-rules"],
capture_output=True,
skip_scripts=True,
asroot=True,
)
return result
def install_argyll_instrument_drivers(self, uninstall=False, launch_devman=False):
"""(Un-)install the Argyll CMS instrument drivers under Windows"""
winxp = sys.getwindowsversion() < (6,)
if launch_devman:
if winxp:
cmd = "start"
args = ["mmc", "devmgmt.msc"]
else:
cmd = "mmc"
args = ["devmgmt.msc"]
self.exec_cmd(
cmd,
args,
capture_output=True,
skip_scripts=True,
asroot=not winxp,
shell=winxp,
working_dir=False,
)
if uninstall:
if not winxp:
# Windows Vista and newer
with win64_disable_file_system_redirection():
pnputil = which("PnPutil.exe")
if not pnputil:
return Error(lang.getstr("file.missing", "PnPutil.exe"))
result = self.exec_cmd(
pnputil,
["-e"],
capture_output=True,
log_output=False,
silent=True,
skip_scripts=True,
)
if not result:
return Error(
lang.getstr("argyll.instrument.drivers.uninstall.failure")
)
elif isinstance(result, Exception):
return result
output = universal_newlines("".join(self.output))
for entry in output.split("\n\n"):
entry = [
line.split(":", 1)[-1].strip() for line in entry.split("\n")
]
for value in entry:
if value == "ArgyllCMS":
result = self.exec_cmd(
pnputil,
["-f", "-d", entry[0]],
capture_output=True,
skip_scripts=True,
asroot=True,
)
else:
# Windows XP
# Uninstallation not supported
pass
else:
# Install driver using modified 'zadic' command line tool from
# https://github.com/fhoech/libwdi
result = None
# Get Argyll version
argyll_version_string = self.argyll_version_string
if argyll_version_string == "0.0.0":
# Download version info
resp = http_request(
None,
DOMAIN,
"GET",
"/Argyll/VERSION",
failure_msg=lang.getstr("update_check.fail"),
silent=True,
)
if resp:
argyll_version_string = resp.read().strip()
installer_basename = "ArgyllCMS_install_USB.exe"
download_dir = os.path.join(config.datahome, "dl")
installer = os.path.join(
download_dir,
f"Argyll_V{argyll_version_string}",
"usb",
installer_basename,
)
if not os.path.isfile(installer):
# Update installer_basename to the one that is used in DisplayCAL.net
installer_basename = (
f"Argyll_V{argyll_version_string}_USB_driver_installer.exe"
)
installer_zip = self.download(
f"https://{DOMAIN}/Argyll/{installer_basename}.zip"
)
if isinstance(installer_zip, Exception):
return installer_zip
elif not installer_zip:
# Cancelled
return
installer = os.path.splitext(installer_zip)[0]
# Open installer ZIP archive
try:
with zipfile.ZipFile(installer_zip) as z:
# Extract installer if it does not exist or if the existing
# file is different from the one in the archive
if os.path.isfile(installer):
with open(installer, "rb") as f:
crc = zlib.crc32(f.read())
else:
crc = None
if (
not os.path.isfile(installer)
or crc != z.getinfo(installer_basename).CRC
):
z.extract(
installer_basename, os.path.dirname(installer_zip)
)
except Exception as exception:
return exception
# Get supported instruments USB device IDs
usb_ids = {}
for instrument_name in all_instruments:
instrument = all_instruments[instrument_name]
if instrument.get("usb_ids"):
for entry in instrument.get("usb_ids"):
usb_id = (entry["vid"], entry["pid"])
if entry.get("hid"):
# Skip HID devices
continue
else:
usb_ids[usb_id] = usb_ids.get(usb_id, [instrument_name])
usb_ids[usb_id].append(instrument_name)
# Check connected USB devices for supported instruments
not_main_thread = currentThread().__class__.__name__ != "_MainThread"
if not_main_thread:
# If running in a thread, need to call pythoncom.CoInitialize
pythoncom.CoInitialize()
try:
if not wmi:
raise NotImplementedError("WMI not available")
wmi_connection = wmi.WMI()
query = "Select * From Win32_USBControllerDevice"
for item in wmi_connection.query(query):
try:
device_id = item.Dependent.DeviceID
except wmi.x_wmi as exception:
self.log(exception)
continue
for usb_id in usb_ids:
instrument_names = usb_ids[usb_id]
hardware_id = r"USB\VID_{:04X}&PID_{:04X}".format(*usb_id)
if device_id.startswith(hardware_id):
# Found supported instrument
try:
self.log(item.Dependent.Caption)
except wmi.x_wmi:
self.log(", ".join(instrument_names))
# Install driver for specific device
result = self.exec_cmd(
installer,
[
"--noprompt",
"--usealldevices",
"--vid",
hex(usb_id[0]),
"--pid",
hex(usb_id[1]),
],
capture_output=True,
skip_scripts=True,
asroot=True,
)
output = universal_newlines("".join(self.output))
if isinstance(result, Exception):
return result
elif not result or "Failed to install driver" in output:
return Error(
lang.getstr(
"argyll.instrument.drivers.install.failure"
)
)
except Exception as exception:
self.log(exception)
finally:
if not_main_thread:
# If running in a thread, need to call pythoncom.CoUninitialize
pythoncom.CoUninitialize()
if not result:
# No matching device found. Install driver anyway, doesn't
# matter for which specific device as the .inf contains entries
# for all supported ones, thus instruments that are connected
# later should be recognized
usb_id, instrument_names = usb_ids.popitem()
result = self.exec_cmd(
installer,
[
"--noprompt",
"--vid",
hex(usb_id[0]),
"--pid",
hex(usb_id[1]),
"--create",
f"{instrument_names[-1]} (Argyll)",
],
capture_output=True,
skip_scripts=True,
asroot=True,
)
output = universal_newlines("".join(self.output))
if not result or "Failed to install driver" in output:
return Error(
lang.getstr("argyll.instrument.drivers.install.failure")
)
return result
def _install_profile_argyll(
self, profile_path, capture_output=False, skip_scripts=False, silent=False
):
"""Install profile using dispwin.
Return the profile path, an error or False
"""
if sys.platform == "darwin" and False: # NEVER
# Alternate way of 'installing' the profile under OS X by just
# copying it
profiles = os.path.join("Library", "ColorSync", "Profiles")
profile_install_path = os.path.join(
profiles, os.path.basename(profile_path)
)
network = os.path.join(os.path.sep, "Network", profiles)
if getcfg("profile.install_scope") == "l":
profile_install_path = os.path.join(os.path.sep, profile_install_path)
elif getcfg("profile.install_scope") == "n" and os.path.isdir(network):
profile_install_path = os.path.join(network, profile_install_path)
else:
profile_install_path = os.path.join(
os.path.expanduser("~"), profile_install_path
)
cmd, args = "cp", ["-f", profile_path, profile_install_path]
result = self.exec_cmd(
cmd,
args,
capture_output,
low_contrast=False,
skip_scripts=skip_scripts,
silent=silent,
asroot=getcfg("profile.install_scope") in ("l", "n"),
title=lang.getstr("profile.install"),
)
if not isinstance(result, Exception) and result:
self.output = ["Installed"]
else:
cmd, args = self.prepare_dispwin(None, profile_path, True)
if not isinstance(cmd, Exception):
profile_path = args[-1]
if sys.platform == "win32" and sys.getwindowsversion() >= (6,):
# Set/unset per-user profiles under Vista / Windows 7
idx = getcfg("display.number") - 1
try:
device0 = util_win.get_display_device(idx, exception_cls=None)
devicea = util_win.get_display_device(idx, True)
except Exception as exception:
self.log(f"util_win.get_display_device({idx}):", exception)
else:
per_user = "-Sl" not in args
for i, device in enumerate((device0, devicea)):
if not device:
self.log(
"Warning: There is no {} display "
"device to {} per-user profiles".format(
"1st" if i == 0 else "active",
"enable" if per_user else "disable",
)
)
continue
try:
util_win.enable_per_user_profiles(
per_user, devicekey=device.DeviceKey
)
except Exception as exception:
self.log(
"util_win.enable_per_user_profiles({}, devicekey={}): {}".format(
per_user, repr(device.DeviceKey), exception
)
)
if "-Sl" in args and (
sys.platform != "darwin"
or intlist(mac_ver()[0].split(".")) >= [10, 6]
):
# If a 'system' install is requested under Linux,
# Mac OS X >= 10.6 or Windows,
# install in 'user' scope first because a system-wide install
# doesn't also set it as current user profile on those systems
# (on Mac OS X < 10.6, we can use ColorSyncScripting to set it).
# It has the small drawback under Linux and OS X 10.6 that
# it will copy the profile to both the user and system-wide
# locations, though, which is not a problem under Windows as
# they are the same.
args.remove("-Sl")
result = self.exec_cmd(
cmd,
args,
capture_output,
low_contrast=False,
skip_scripts=skip_scripts,
silent=silent,
title=lang.getstr("profile.install"),
)
output = list(self.output)
args.insert(0, "-Sl")
else:
output = None
result = True
if not isinstance(result, Exception) and result:
result = self.exec_cmd(
cmd,
args,
capture_output,
low_contrast=False,
skip_scripts=skip_scripts,
silent=silent,
title=lang.getstr("profile.install"),
)
else:
result = cmd
if not isinstance(result, Exception) and result is not None:
result = False
for line in output or self.output:
if "Installed" in line:
if (
sys.platform == "darwin"
and "-Sl" in args
and intlist(mac_ver()[0].split(".")) < [10, 6]
):
# The profile has been installed, but we need a little
# help from AppleScript to actually make it the default
# for the current user. Only works under Mac OS < 10.6
n = getcfg("display.number")
path = os.path.join(
os.path.sep,
"Library",
"ColorSync",
"Profiles",
os.path.basename(args[-1]),
)
applescript = [
'tell app "ColorSyncScripting"',
f'set displayProfile to POSIX file "{path}" as alias',
f"set display profile of display {n:d} to displayProfile",
"end tell",
]
try:
retcode, output, errors = osascript(applescript)
except Exception as exception:
self.log(exception)
else:
if errors.strip():
self.log(f"osascript error: {errors}")
else:
result = True
break
elif sys.platform == "darwin" and False: # NEVER
# After 'installing' a profile under Mac OS X by just
# copying it, show system preferences
applescript = [
'tell application "System Preferences"',
"activate",
'set current pane to pane id "com.apple.preference.displays"',
'reveal (first anchor of current pane whose name is "displaysColorTab")',
# This needs access for assistive devices enabled
# 'tell application "System Events"',
# 'tell process "System Preferences"',
# 'select row 2 of table 1 of scroll area 1 of group 1 of tab group 1 of window "<Display name from EDID here>"',
# 'end tell',
# 'end tell',
"end tell",
]
try:
retcode, output, errors = osascript(applescript)
except Exception as exception:
self.log(exception)
else:
if errors.strip():
self.log(f"osascript error: {errors}")
else:
result = True
else:
result = True
break
if not result and self.errors:
result = Error("".join(self.errors).strip())
else:
result = profile_path
# DO NOT call wrapup() here, it'll remove the profile in the temp
# directory before dispwin can get a hold of it when running with UAC
# under Windows, because that makes the call to dispwin run in a
# separate thread. DisplayCAL.MainFrame.profile_finish_consumer calls
# DisplayCAL.MainFrame.start_timers(True) after displaying the result
# message which takes care of cleanup.
return result
def _install_profile_colord(self, profile, device_id):
"""Install profile using colord"""
self.log(f"{appname}: Trying device ID {device_id}")
try:
colord.install_profile(device_id, profile, logfn=self.log)
except Exception as exception:
self.log(exception)
return exception
return True
def _attempt_install_profile_colord(self, profile, device_id=None):
if not device_id:
device_id = self.get_device_id(quirk=False, query=True)
if device_id:
result = False
# Try a range of possible device IDs
device_ids = [
device_id,
self.get_device_id(quirk=True, truncate_edid_strings=True),
self.get_device_id(quirk=True, use_serial_32=False),
self.get_device_id(
quirk=True, use_serial_32=False, truncate_edid_strings=True
),
self.get_device_id(quirk=True),
self.get_device_id(quirk=False, truncate_edid_strings=True),
self.get_device_id(quirk=False, use_serial_32=False),
self.get_device_id(
quirk=False, use_serial_32=False, truncate_edid_strings=True
),
# Try with manufacturer omitted
self.get_device_id(omit_manufacturer=True),
self.get_device_id(truncate_edid_strings=True, omit_manufacturer=True),
self.get_device_id(use_serial_32=False, omit_manufacturer=True),
self.get_device_id(
use_serial_32=False,
truncate_edid_strings=True,
omit_manufacturer=True,
),
]
for device_id in dict.fromkeys(device_ids).keys():
if device_id:
# NOTE: This can block
result = self._install_profile_colord(profile, device_id)
if isinstance(result, colord.CDObjectQueryError):
# Device ID was not found, try next one
continue
else:
# Either returned ok or there was another error
break
return result
def _install_profile_gcm(self, profile):
"""Install profile using gcm-import"""
if which("colormgr"):
# Check if profile already exists in database
try:
colord.get_object_path("icc-" + hexlify(profile.ID).decode(), "profile")
except colord.CDObjectQueryError:
# Profile not in database
pass
except colord.CDError as exception:
self.log(exception)
else:
# Profile already in database, nothing to do
return None
# gcm-import will check if the profile is already in the database
# (based on profile ID), but will fail to overwrite a profile with the
# same name. We need to remove those profiles so gcm-import can work.
profilename = os.path.basename(profile.fileName)
for dirname in iccprofiles_home:
profile_install_path = os.path.join(dirname, profilename)
if (
os.path.isfile(profile_install_path)
and profile_install_path != profile.fileName
):
try:
send2trash([profile_install_path])
except OSError as exc:
self.log(exc)
else:
# Give colord time to recognize that the profile was
# removed, otherwise gcm-import may complain if it's
# a profile that was already in the database
sleep(3)
if self._progress_wnd and not getattr(self._progress_wnd, "dlg", None):
self._progress_wnd.dlg = DummyDialog()
# Run gcm-import
cmd, args = which("gcm-import"), [profile.fileName]
# gcm-import does not seem to return a useful exit code (it's always 1)
# or stderr output
self.exec_cmd(cmd, args, capture_output=True, skip_scripts=True)
def _install_profile_oy(
self,
profile_path,
profile_name=None,
capture_output=False,
skip_scripts=False,
silent=False,
):
"""Install profile using oyranos-monitor"""
display = self.displays[
max(0, min(len(self.displays) - 1, getcfg("display.number") - 1))
]
x, y = [pos.strip() for pos in display.split("@")[-1].split(",")[0:2]]
if getcfg("profile.install_scope") == "l":
# If system-wide install, copy profile to
# /var/lib/color/icc/devices/display
var_icc = "/var/lib/color/icc/devices/display"
if not profile_name:
profile_name = os.path.basename(profile_path)
profile_install_path = os.path.join(var_icc, profile_name)
result = self.exec_cmd(
"mkdir",
["-p", os.path.dirname(profile_install_path)],
capture_output=True,
low_contrast=False,
skip_scripts=True,
silent=True,
asroot=True,
)
if not isinstance(result, Exception) and result:
result = self.exec_cmd(
"cp",
["-f", profile_path, profile_install_path],
capture_output=True,
low_contrast=False,
skip_scripts=True,
silent=True,
asroot=True,
)
else:
result = True
dirname = None
for dirname in iccprofiles_display_home:
if os.path.isdir(dirname):
# Use the first one that exists
break
else:
dirname = None
if not dirname:
# Create the first one in the list
dirname = iccprofiles_display_home[0]
try:
os.makedirs(dirname)
except Exception as exception:
self.log(exception)
result = False
if result is not False:
profile_install_path = os.path.join(
dirname, os.path.basename(profile_path)
)
try:
shutil.copyfile(profile_path, profile_install_path)
except Exception as exception:
self.log(exception)
result = False
if not isinstance(result, Exception) and result is not False:
cmd = which("oyranos-monitor")
args = ["-x", x, "-y", y, profile_install_path]
result = self.exec_cmd(
cmd,
args,
capture_output,
low_contrast=False,
skip_scripts=skip_scripts,
silent=silent,
working_dir=False,
)
# if getcfg("profile.install_scope") == "l":
# result = self.exec_cmd(cmd, args,
# capture_output,
# low_contrast=False,
# skip_scripts=skip_scripts,
# silent=silent,
# asroot=True,
# working_dir=False)
if not result and self.errors:
result = Error("".join(self.errors).strip())
return result
def _install_profile_loader_win32(self, silent=False):
"""Install profile loader"""
if (
sys.platform == "win32"
and sys.getwindowsversion() >= (6, 1)
and util_win.calibration_management_isenabled()
):
self._uninstall_profile_loader_win32()
return True
# Must return either True on success or an Exception object on error
result = True
# Remove outdated (pre-0.5.5.9) profile loaders
display_no = self.get_display()
name = f"{appname} Calibration Loader (Display {display_no})"
if autostart_home:
loader_v01b = os.path.join(autostart_home, f"dispwin-d{display_no}-c-L.lnk")
if os.path.exists(loader_v01b):
try:
# delete v0.1b loader
os.remove(loader_v01b)
except Exception as exception:
self.log(
"Warning - could not remove old "
"v0.1b calibration loader '{}': {}".format(
loader_v01b, exception
)
)
loader_v02b = os.path.join(autostart_home, f"{name}.lnk")
if os.path.exists(loader_v02b):
try:
# delete v02.b/v0.2.1b loader
os.remove(loader_v02b)
except Exception as exception:
self.log(
"Warning - could not remove old "
"v0.2b calibration loader '{}': {}".format(
loader_v02b, exception
)
)
loader_v0558 = os.path.join(autostart_home, name + ".lnk")
if os.path.exists(loader_v0558):
try:
# delete v0.5.5.8 user loader
os.remove(loader_v0558)
except Exception as exception:
self.log(
"Warning - could not remove old "
"v0.2b calibration loader '{}': {}".format(
loader_v02b, exception
)
)
if autostart:
loader_v0558 = os.path.join(autostart, f"{name}.lnk")
if os.path.exists(loader_v0558):
try:
# delete v0.5.5.8 system loader
os.remove(loader_v0558)
except Exception as exception:
self.log(
"Warning - could not remove old "
"v0.2b calibration loader '{}': {}".format(
loader_v02b, exception
)
)
# Create unified loader
name = f"{appname} Profile Loader"
if autostart:
autostart_lnkname = os.path.join(autostart, name + ".lnk")
if autostart_home:
autostart_home_lnkname = os.path.join(autostart_home, name + ".lnk")
loader_args = []
if os.path.basename(exe).lower() in ("python.exe", "pythonw.exe"):
cmd = os.path.join(exedir, "pythonw.exe")
pyw = os.path.normpath(
os.path.join(pydir, "..", f"{appname}-apply-profiles.pyw")
)
if os.path.exists(pyw):
# Running from source or 0install
# Check if this is a 0install implementation, in which
# case we want to call 0launch with the appropriate
# command
if re.match(
r"sha\d+(?:new)?", os.path.basename(os.path.dirname(pydir))
):
cmd = which("0install-win.exe") or "0install-win.exe"
loader_args.extend(
[
"run",
"--batch",
"--no-wait",
"--offline",
"--command=run-apply-profiles",
f"http://{DOMAIN}/0install/{appname}.xml",
]
)
else:
# Running from source
loader_args.append(pyw)
else:
# Regular install
loader_args.append(
get_data_path(os.path.join("scripts", f"{appname}-apply-profiles"))
)
else:
cmd = os.path.join(pydir, f"{appname}-apply-profiles.exe")
not_main_thread = currentThread().__class__.__name__ != "_MainThread"
if not_main_thread:
# If running in a thread, need to call pythoncom.CoInitialize
pythoncom.CoInitialize()
from DisplayCAL import taskscheduler
try:
ts = taskscheduler.TaskScheduler()
except Exception as exception:
print("Warning - could not access task scheduler:", exception)
ts = None
task = None
else:
task = ts.query_task(f"{appname} Profile Loader Launcher")
elevate = getcfg(
"profile.install_scope"
) == "l" and sys.getwindowsversion() >= (6,)
loader_lockfile = os.path.join(
config.confighome, appbasename + "-apply-profiles.lock"
)
loader_running = os.path.isfile(loader_lockfile)
if not loader_running or (not task and elevate):
# Launch profile loader if not yet running, or restart if no task
# and going to elevate (launching profile loader with elevated
# privileges will try to create scheduled task so we can run
# elevated at login without UAC prompt)
errmsg = "Warning - could not launch profile loader"
if elevate:
errmsg += " with elevated privileges"
try:
shell_exec(
cmd,
loader_args + ["--skip"],
operation="runas" if elevate else "open",
wait_for_idle=True,
)
except pywintypes.error as exception:
if exception.args[0] == winerror.ERROR_CANCELLED:
errmsg += " (user cancelled)"
else:
errmsg += " " + safe_str(exception)
print(errmsg)
if task or (ts and ts.query_task(f"{appname} Profile Loader Launcher")):
if not_main_thread:
pythoncom.CoUninitialize()
return True
try:
scut = pythoncom.CoCreateInstance(
win32com_shell.CLSID_ShellLink,
None,
pythoncom.CLSCTX_INPROC_SERVER,
win32com_shell.IID_IShellLink,
)
scut.SetPath(cmd)
if len(loader_args) == 1:
scut.SetWorkingDirectory(pydir)
if os.path.basename(cmd) == f"{appname}-apply-profiles.exe":
scut.SetIconLocation(cmd, 0)
else:
scut.SetIconLocation(
get_data_path(
os.path.join("theme", "icons", f"{appname}-apply-profiles.ico")
),
0,
)
scut.SetArguments(sp.list2cmdline(loader_args))
scut.SetShowCmd(win32con.SW_SHOWDEFAULT)
if is_superuser():
if autostart:
try:
scut.QueryInterface(pythoncom.IID_IPersistFile).Save(
autostart_lnkname, 0
)
except Exception as exception:
if not silent:
result = Warning(
lang.getstr("error.autostart_creation", autostart)
+ "\n"
+ str(exception)
)
# Now try user scope
else:
if not silent:
result = Warning(lang.getstr("error.autostart_system"))
if autostart_home:
if autostart and os.path.isfile(autostart_lnkname):
# Remove existing user loader
if os.path.isfile(autostart_home_lnkname):
os.remove(autostart_home_lnkname)
else:
# Only create user loader if no system loader
try:
scut.QueryInterface(pythoncom.IID_IPersistFile).Save(
autostart_home_lnkname, 0
)
except Exception as exception:
if not silent:
result = Warning(
lang.getstr("error.autostart_creation", autostart_home)
+ "\n"
+ str(exception)
)
else:
if not silent:
result = Warning(lang.getstr("error.autostart_user"))
except Exception as exception:
if not silent:
result = Warning(
lang.getstr("error.autostart_creation", autostart_home)
+ "\n"
+ str(exception)
)
finally:
if not_main_thread:
# If running in a thread, need to call pythoncom.CoUninitialize
pythoncom.CoUninitialize()
return result
def _uninstall_profile_loader_win32(self):
"""Uninstall profile loader"""
name = f"{appname} Profile Loader"
if autostart and is_superuser():
autostart_lnkname = os.path.join(autostart, name + ".lnk")
if os.path.exists(autostart_lnkname):
try:
os.remove(autostart_lnkname)
except Exception as exception:
self.log(autostart_lnkname, exception)
if autostart_home:
autostart_home_lnkname = os.path.join(autostart_home, name + ".lnk")
if os.path.exists(autostart_home_lnkname):
try:
os.remove(autostart_home_lnkname)
except Exception as exception:
self.log(autostart_home_lnkname, exception)
return True
def _install_profile_loader_xdg(self, silent=False):
"""Install profile loader"""
# See http://standards.freedesktop.org/autostart-spec
# Must return either True on success or an Exception object on error
result = True
# Remove wrong-cased entry potentially created by DisplayCAL < 3.1.6
name = f"z-{appname}-apply-profiles"
desktop_file_path = os.path.join(autostart_home, f"{name}.desktop")
if os.path.exists(desktop_file_path):
try:
os.remove(desktop_file_path)
except Exception as exception:
result = Warning(
lang.getstr("error.autostart_remove_old", desktop_file_path)
)
# Create unified loader
# Prepend 'z' so our loader hopefully loads after
# possible nvidia-settings entry (which resets gamma table)
name = f"z-{appname.lower()}-apply-profiles"
desktop_file_path = os.path.join(autostart_home, f"{name}.desktop")
system_desktop_file_path = os.path.join(autostart, f"{name}.desktop")
try:
# Create user loader, even if we later try to
# move it to the system-wide location so that at least
# the user loader is present if the move to the system
# dir fails
if not os.path.exists(autostart_home):
os.makedirs(autostart_home)
desktop_file_buffer = []
desktop_file_buffer.append("[Desktop Entry]")
desktop_file_buffer.append("Version=1.0")
desktop_file_buffer.append("Encoding=UTF-8")
desktop_file_buffer.append("Type=Application")
desktop_file_buffer.append(f"Name={appname} ICC Profile Loader")
desktop_file_buffer.append(
"Comment={}".format(
lang.getstr("calibrationloader.description", lcode="en")
)
)
if lang.getcode() != "en":
desktop_file_buffer.append(
(
"Comment[{}]={}".format(
lang.getcode(), lang.getstr("calibrationloader.description")
)
)
)
pyw = os.path.normpath(
os.path.join(pydir, "..", f"{appname}-apply-profiles.pyw")
)
icon = f"{appname.lower()}-apply-profiles"
if os.path.exists(pyw):
# Running from source, or 0install/installer install
# Check if this is a 0install implementation, in which
# case we want to call 0launch with the appropriate
# command
if re.match(
r"sha\d+(?:new)?", os.path.basename(os.path.dirname(pydir))
):
executable = (
"0launch --console --offline "
"--command=run-apply-profiles "
f"http://{DOMAIN}/0install/{appname}.xml"
)
else:
icon = os.path.join(
pydir,
"theme",
"icons",
"256x256",
f"{appname.lower()}-apply-profiles.png",
)
executable = pyw
else:
# Regular install
executable = f"{appname.lower()}-apply-profiles"
desktop_file_buffer.append(f"Icon={icon}")
desktop_file_buffer.append(f"Exec={executable}")
desktop_file_buffer.append("Terminal=false")
with open(desktop_file_path, "w") as desktop_file:
desktop_file.write("\n".join(desktop_file_buffer))
except Exception as exception:
if not silent:
result = Warning(
lang.getstr("error.autostart_creation", desktop_file_path)
+ "\n"
+ str(exception)
)
else:
if getcfg("profile.install_scope") == "l" and autostart:
# Move system-wide loader
if (
self.exec_cmd(
"mkdir",
["-p", autostart],
capture_output=True,
low_contrast=False,
skip_scripts=True,
silent=True,
asroot=True,
)
is not True
or self.exec_cmd(
"mv",
["-f", desktop_file_path, system_desktop_file_path],
capture_output=True,
low_contrast=False,
skip_scripts=True,
silent=True,
asroot=True,
)
is not True
) and not silent:
result = Warning(
lang.getstr(
"error.autostart_creation", system_desktop_file_path
)
)
return result
def instrument_supports_ccss(self, instrument_name=None):
"""Return whether instrument supports CCSS files or not"""
if not instrument_name:
instrument_name = self.get_instrument_name()
return self.get_instrument_features(instrument_name).get("spectral_cal")
def create_ccxx(self, args=None, working_dir=None):
"""Create CCMX or CCSS"""
if not args:
args = []
cmd = get_argyll_util("ccxxmake")
if "-I" not in args:
# Display manufacturer & name
name = self.get_display_name(True)
if name:
args.insert(0, "-I")
args.insert(1, name)
elif "-T" not in args:
# Display technology
args.insert(0, "-T")
displaytech = ["LCD" if getcfg("measurement_mode") == "l" else "CRT"]
if self.get_instrument_features().get("projector_mode") and getcfg(
"measurement_mode.projector"
):
displaytech.append("Projector")
args.insert(1, " ".join(displaytech))
if not getcfg("ccmx.use_four_color_matrix_method"):
args.insert(0, "-v")
return self.exec_cmd(
cmd,
args,
capture_output=True,
skip_scripts=True,
silent=False,
working_dir=working_dir,
)
def create_gamut_views(self, profile_path):
"""Generate gamut views (VRML files) and show progress in current
progress dialog"""
if getcfg("profile.create_gamut_views"):
self.log("-" * 80)
self.log(lang.getstr("gamut.view.create"))
self.lastmsg.clear()
self.recent.clear()
self.recent.write(lang.getstr("gamut.view.create"))
sleep(0.75) # Allow time for progress window to update
return self.calculate_gamut(profile_path)
else:
return None, None
def create_profile(
self,
dst_path=None,
skip_scripts=False,
display_name=None,
display_manufacturer=None,
tags=None,
):
"""Create an ICC profile and process the generated file"""
print(lang.getstr("create_profile"))
if dst_path is None:
dst_path = os.path.join(
getcfg("profile.save_path"),
getcfg("profile.name.expanded"),
getcfg("profile.name.expanded") + profile_ext,
)
cmd, args = self.prepare_colprof(
os.path.basename(os.path.splitext(dst_path)[0]),
display_name,
display_manufacturer,
tags,
)
if isinstance(cmd, Exception):
result = cmd
else:
result = True
profile = None
profile_path = args[-1] + profile_ext
if "-aX" in args:
# If profile type is X (XYZ cLUT + matrix), only create the
# cLUT, then add the matrix tags later from a forward lookup of
# a smaller testchart (faster computation!)
args.insert(args.index("-aX"), "-ax")
args.remove("-aX")
check_for_ti1_match = False
is_regular_grid = False
is_primaries_only = False
ti3 = CGATS(args[-1] + ".ti3")
XYZbp = None
try:
(
ti3_extracted,
ti3_RGB_XYZ,
ti3_remaining,
) = extract_device_gray_primaries(ti3, logfn=self.log)
except Error as exception:
self.log(exception)
else:
if getcfg("profile.type") in ("X", "x", "S", "s"):
# Check if TI3 RGB matches one of our regular grid or
# primaries + gray charts
check_for_ti1_match = True
if ti3_RGB_XYZ[(0, 0, 0)] != (0, 0, 0):
# Note: Setting black chroma to zero fixes smoothness
# issues on devices with not very neutral black.
bpcorr = getcfg("profile.black_point_correction")
if False: # getcfg("profile.black_point_compensation"):
XYZbp = (0, 0, 0)
Labbp = (0, 0, 0)
else:
# Correct black point a* b* and make neutral hues near
# black blend over to the new blackpoint. The correction
# factor determines the amount of the measured black hue
# that should be retained.
# It makes the profile slightly less accurate near
# black, but the effect is negligible and the visual
# benefit is of greater importance (allows for
# calibration blackpoint hue correction to have desired
# effect, and makes relcol with BPC visually match
# perceptual in Photoshop).
XYZwp = ti3_RGB_XYZ[(100, 100, 100)]
Labbp = colormath.XYZ2Lab(
*ti3_RGB_XYZ[(0, 0, 0)], whitepoint=XYZwp
)
Labbp = (Labbp[0], Labbp[1] * bpcorr, Labbp[2] * bpcorr)
XYZbp = colormath.Lab2XYZ(
*Labbp, whitepoint=[v / XYZwp[1] for v in XYZwp]
)
if XYZbp and bpcorr < 1:
ti3.write(args[-1] + ".ti3.backup")
if False: # getcfg("profile.black_point_compensation"):
logmsg = "Applying black point compensation"
else:
logmsg = (
f"Applying {bpcorr * 100:0.0f}% black point correction"
)
self.log(f"{logmsg} to TI3")
ti3[0].apply_bpc(XYZbp)
ti3.write()
if check_for_ti1_match:
for ti1_name in (
"ti1/d3-e4-s2-g28-m0-b0-f0", # Primaries + gray
"ti1/d3-e4-s3-g52-m3-b0-f0", # 3^3 grid
"ti1/d3-e4-s4-g52-m4-b0-f0", # 4^3 grid
"ti1/d3-e4-s5-g52-m5-b0-f0", # 5^3 grid
"ti1/d3-e4-s9-g52-m9-b0-f0", # 9^3 grid
"ti1/d3-e4-s17-g52-m17-b0-f0",
): # 17^3 grid
ti1_filename = f"{ti1_name}.ti1"
ti1_path = get_data_path(ti1_filename)
if not ti1_path:
if ti1_name in (
"ti1/d3-e4-s2-g28-m0-b0-f0",
"ti1/d3-e4-s3-g52-m3-b0-f0",
"ti1/d3-e4-s4-g52-m4-b0-f0",
"ti1/d3-e4-s5-g52-m5-b0-f0",
):
return Error(lang.getstr("file.missing", ti1_filename))
else:
continue
ti1 = CGATS(ti1_path)
(
ti1_extracted,
ti1_RGB_XYZ,
ti1_remaining,
) = extract_device_gray_primaries(ti1)
# Quantize to 8 bit for comparison
# XXX Note that round(50 * 2.55) = 127, but
# round(50 / 100 * 255) = 128 (the latter is what we want)!
if sorted(
tuple(round(v / 100.0 * 255) for v in RGB)
for RGB in list(ti3_remaining.keys())
) == sorted(
tuple(round(v / 100.0 * 255) for v in RGB)
for RGB in list(ti1_remaining.keys())
):
if ti1_name == "ti1/d3-e4-s2-g28-m0-b0-f0":
is_primaries_only = True
if getcfg("profile.type") not in ("S", "s"):
options_dispcal = get_options_from_ti3(ti3)[0]
setcfg(
"profile.type",
"S" if get_arg("q", options_dispcal) else "s",
)
elif getcfg("profile.type") in ("X", "x"):
is_regular_grid = True
break
if not display_name:
arg = get_arg("-M", args, True)
if arg and len(args) - 1 > arg[0]:
display_name = args[arg[0] + 1]
if not display_manufacturer:
arg = get_arg("-A", args, True)
if arg and len(args) - 1 > arg[0]:
display_manufacturer = args[arg[0] + 1]
if is_regular_grid:
# Use our own forward profile code
profile = self.create_RGB_XYZ_cLUT_fwd_profile(
ti3,
os.path.basename(args[-1]),
getcfg("copyright"),
display_manufacturer,
display_name,
self.log,
)
if getcfg("profile.type") == "x" and (
self.argyll_version < [2, 1, 0] or "-aY" in args
):
# Swapped matrix - need to create it ourselves
# Start with sRGB
sRGB = profile.from_named_rgb_space("sRGB")
for channel in "rgb":
tagname = channel + "TRC"
profile.tags[tagname] = sRGB.tags[tagname]
# Swap RGB -> BRG
profile.tags.rXYZ = sRGB.tags.bXYZ
profile.tags.gXYZ = sRGB.tags.rXYZ
profile.tags.bXYZ = sRGB.tags.gXYZ
elif not is_primaries_only:
# Use colprof to create profile
result = self.exec_cmd(
cmd, args, low_contrast=False, skip_scripts=skip_scripts
)
if not isinstance(result, Exception) and result:
try:
profile = ICCProfile(profile_path)
except (IOError, ICCProfileInvalidError):
result = Error(
lang.getstr("profile.invalid") + "\n" + profile_path
)
if not isinstance(result, Exception) and result:
if getcfg("profile.type") == "X" or is_primaries_only:
# Use our own accurate matrix
cat = "Bradford"
if not is_primaries_only:
cat = profile.guess_cat() or cat
mtx = self._create_simple_matrix_profile(
ti3_RGB_XYZ,
ti3_remaining,
os.path.basename(args[-1]),
getcfg("copyright"),
display_manufacturer,
display_name,
(
getcfg("profile.type") in ("X", "x")
and getcfg("profile.b2a.hires")
)
or getcfg("profile.black_point_compensation"),
cat,
)
if is_primaries_only:
# Use matrix profile
profile = mtx
# Add luminance tag
luminance_XYZ_cdm2 = ti3.queryv1("LUMINANCE_XYZ_CDM2")
if luminance_XYZ_cdm2:
profile.tags.lumi = XYZType(profile=profile)
profile.tags.lumi.Y = float(luminance_XYZ_cdm2.split()[1])
# Add blackpoint tag
profile.tags.bkpt = XYZType(profile=profile)
if XYZbp:
black_XYZ = XYZbp
else:
black_XYZ = (0, 0, 0)
(
profile.tags.bkpt.X,
profile.tags.bkpt.Y,
profile.tags.bkpt.Z,
) = black_XYZ
# Check if we have calibration, if so, add vcgt
for cgats in ti3.values():
if cgats.type == b"CAL":
profile.tags.vcgt = cal_to_vcgt(cgats)
else:
# Add matrix tags to cLUT profile
for tagcls in ("XYZ", "TRC"):
for channel in "rgb":
tagname = channel + tagcls
profile.tags[tagname] = mtx.tags[tagname]
if is_regular_grid or is_primaries_only:
# Write profile
profile.write(profile_path)
if os.path.isfile(args[-1] + ".chrm"):
# Get ChromaticityType tag
with open(args[-1] + ".chrm", "rb") as blob:
chrm = ChromaticityType(blob.read())
else:
chrm = None
if XYZbp:
Ybp = XYZbp[1]
XYZbp = colormath.adapt(
*XYZbp,
whitepoint_source=[v / XYZwp[1] for v in XYZwp],
cat=profile.guess_cat() or "Bradford",
)
XYZbp = tuple(XYZbp)
bpc_applied = False
profchanged = False
if not isinstance(result, Exception) and result:
errors = self.errors
output = self.output
retcode = self.retcode
try:
if not profile:
profile = ICCProfile(profile_path)
except (IOError, ICCProfileInvalidError):
result = Error(lang.getstr("profile.invalid") + "\n" + profile_path)
else:
# Do we have a B2A0 table?
has_B2A = "B2A0" in profile.tags
# Hires CIECAM02 gamut mapping - perceptual and saturation
# tables, also colorimetric table for non-RGB profiles
# Use collink for smoother result.
# Only for XYZ LUT and if source profile is a simple matrix
# profile, otherwise CIECAM02 tables will already have been
# created by colprof
gamap = (
getcfg("gamap_perceptual") or getcfg("gamap_saturation")
) and getcfg("gamap_profile")
collink = None
if (
"A2B0" in profile.tags
and profile.connectionColorSpace == b"XYZ"
and (gamap or profile.colorSpace != b"RGB")
and getcfg("profile.b2a.hires")
):
collink = get_argyll_util("collink")
if not collink:
self.log(lang.getstr("argyll.util.not_found", "collink"))
gamap_profiles = []
gamap_profile = None
if gamap and collink:
gamap_profile_filename = getcfg("gamap_profile")
try:
gamap_profile = ICCProfile(gamap_profile_filename)
except (IOError, ICCProfileInvalidError) as exception:
self.log(exception)
else:
if (
not "A2B0" in gamap_profile.tags
and "rXYZ" in gamap_profile.tags
and "gXYZ" in gamap_profile.tags
and "bXYZ" in gamap_profile.tags
and "rTRC" in gamap_profile.tags
and "gTRC" in gamap_profile.tags
and "bTRC" in gamap_profile.tags
):
# Simple matrix source profile
if gamap_profile.convert_iccv4_tags_to_iccv2():
# Write to temp file
fd, gamap_profile.fileName = mkstemp_bypath(
gamap_profile_filename, dir=self.tempdir
)
stream = os.fdopen(fd, "wb")
gamap_profile.write(stream)
stream.close()
gamap_profiles.append(gamap_profile)
if profile.colorSpace == b"RGB" and has_B2A:
# Only table 0 (colorimetric) in display profile.
# Assign it to table 1 as per ICC spec to prepare
# for adding table 0 (perceptual) and 2 (saturation)
profile.tags.B2A1 = profile.tags.B2A0
else:
gamap_profile = None
tables = []
if gamap_profile or (collink and profile.colorSpace != b"RGB"):
self.log("-" * 80)
self.log("Creating CIECAM02 gamut mapping using collink")
size = getcfg("profile.b2a.hires.size")
# Make sure to map 'auto' value (-1) to an actual size
size = {-1: 33}.get(size, size)
collink_args = [
"-v",
"-q" + getcfg("profile.quality"),
"-G",
f"-r{size:d}",
]
if is_regular_grid:
# Do not preserve output shaper curves in devicelink
collink_args.append("-no")
if profile.colorSpace == b"RGB" and self.argyll_version >= [1, 7]:
# Use RGB->RGB forced black point hack
collink_args.append("-b")
if gamap_profile:
for tableno, cfgname in [
(0, "gamap_perceptual"),
(2, "gamap_saturation"),
]:
if getcfg(cfgname):
tables.append((tableno, getcfg(cfgname + "_intent")))
if profile.colorSpace != b"RGB":
# Create colorimetric table for non-RGB profile
tables.insert(0, (1, "r"))
if not gamap_profile:
# Create perceptual table with
# luminance matched appearance rendering
# (similar to rel. col. + BPC)
tables.append((0, "la"))
# Get inking rules from colprof extra args
extra_args = getcfg("extra_args.colprof")
inking = re.findall(
r"-[Kk](?:[zhxr]|p[0-9.\s]+)|-[Ll][0-9.\s]+",
extra_args,
)
if inking:
collink_args.extend(parse_argument_string(" ".join(inking)))
# Get rid of lores B2A tables - they affect
# collink even in inverse forward lookup mode
# (probably because collink tries to determine inking
# rules by looking at B2A?)
for tableno in range(3):
tablename = f"B2A{tableno:d}"
if tablename in profile.tags:
del profile.tags[tablename]
profchanged = True
if profchanged:
# Need to write updated profile for xicclu/collink
profile.write()
# Input curve clipping.
# Setting to True makes rel. col. + BPC produce a
# smoother result, but reduces accuracy of the
# colorimetric table
input_curve_clipping = False
# Determine profile blackpoint
if input_curve_clipping:
# Figure out profile blackpoint by looking up
# neutral values from L* = 0 to L* = 50,
# in 0.1 increments
idata = []
for i in range(501):
idata.append((i / 10.0, 0, 0))
odata = self.xicclu(
profile,
idata,
intent="r",
direction="if",
pcs="l",
use_cam_clipping=True,
get_clip=True,
)
Labbp = (0, 0, 0)
for i, values in enumerate(odata):
if not values[-1]:
# Not clipped
Labbp = idata[i]
break
ogamap_profile = gamap_profile
for tableno, intent in tables:
# Create device link(s)
gamap_args = []
if tableno == 1:
# Use PhotoPrintRGB as PCS if creating colorimetric
# table for non-RGB profile.
# PhotoPrintRGB is a synthetic L* TRC space
# with the Rec2020 red and blue adapted to D50
# and a green of x 0.1292 (same as blue x) y 0.8185
# It almost completely encompasses PhotoGamutRGB
pcs = get_data_path("ref/PhotoPrintRGB_Lstar.icc")
if pcs:
try:
gamap_profile = ICCProfile(pcs)
except (IOError, ICCProfileInvalidError) as exception:
self.log(exception)
else:
missing = lang.getstr(
"file.missing", "ref/PhotoPrintRGB_Lstar.icc"
)
if gamap_profile:
self.log(missing)
else:
result = Error(missing)
break
if pcs and input_curve_clipping and Labbp:
self.log(
"Applying black offset L*a*b* {:.2f} {:.2f} {:.2f} to {}...".format(
*(Labbp + (gamap_profile.getDescription(),))
)
)
XYZbp = colormath.Lab2XYZ(*Labbp)
gamap_profile.apply_black_offset(XYZbp)
# Write to temp file because file changed
fd, gamap_profile.fileName = mkstemp_bypath(
gamap_profile.fileName, dir=self.tempdir
)
stream = os.fdopen(fd, "wb")
gamap_profile.write(stream)
stream.close()
gamap_profiles.append(gamap_profile)
elif gamap:
gamap_profile = ogamap_profile
if getcfg("gamap_src_viewcond"):
gamap_args.append("-c" + getcfg("gamap_src_viewcond"))
if getcfg("gamap_out_viewcond"):
gamap_args.append("-d" + getcfg("gamap_out_viewcond"))
# Preserve input curves in resulting devicelink?
preserve_input_curves = True
if gamap_profile and not preserve_input_curves:
for channel in "rgb":
trc = gamap_profile.tags[channel + "TRC"]
trc_len = len(trc)
# Do not preserve input shaper curves if nonlinear
if trc_len > 1 or trc[0] != 1.0:
if trc_len > 1:
# Check if nonlinear (10 bit precision)
values = [
round(v / 65535.0 * 1023) for v in trc
]
ivalues = [
round(v / (trc_len - 1.0) * 1023)
for v in range(trc_len)
]
ni = values != ivalues
else:
# Single gamma != 1.0 (nonlinear)
ni = True
if ni and "-ni" not in collink_args:
# Do not preserve input shaper curves in
# devicelink
collink_args.append("-ni")
break
elif not ni and "-ni" in collink_args:
# Preserve input shaper curves in
# devicelink
collink_args.remove("-ni")
break
link_profile = tempfile.mktemp(profile_ext, dir=self.tempdir)
result = self.exec_cmd(
collink,
collink_args
+ gamap_args
+ [
"-i" + intent,
gamap_profile.fileName,
profile_path,
link_profile,
],
sessionlogfile=self.sessionlogfile,
)
if not isinstance(result, Exception) and result:
try:
link_profile = ICCProfile(link_profile)
except (IOError, ICCProfileInvalidError) as exception:
self.log(exception)
continue
table = f"B2A{tableno:d}"
profile.tags[table] = link_profile.tags.A2B0
if gamap_profile:
# Map B2A input curves to inverse source
# profile TRC
for i, channel in enumerate("rgb"):
curve = profile.tags[table].input[i]
trc = gamap_profile.tags[channel + "TRC"]
if len(trc) == 1:
trc.set_trc(trc[0], len(curve))
elif len(trc) != len(curve):
trc = colormath.interp_resize(
trc, len(curve), use_numpy=True
)
interp = colormath.Interp(
trc, curve, use_numpy=True
)
profile.tags[table].input[i] = icurve = []
for j in range(4096):
icurve.append(interp(j / 4095.0 * 65535))
# Remove temporary link profile
os.remove(link_profile.fileName)
# Update B2A matrix with source profile matrix
matrix = colormath.Matrix3x3(
[
[
gamap_profile.tags.rXYZ.X,
gamap_profile.tags.gXYZ.X,
gamap_profile.tags.bXYZ.X,
],
[
gamap_profile.tags.rXYZ.Y,
gamap_profile.tags.gXYZ.Y,
gamap_profile.tags.bXYZ.Y,
],
[
gamap_profile.tags.rXYZ.Z,
gamap_profile.tags.gXYZ.Z,
gamap_profile.tags.bXYZ.Z,
],
]
)
matrix.invert()
scale = 1 + (32767 / 32768.0)
matrix *= colormath.Matrix3x3(
((scale, 0, 0), (0, scale, 0), (0, 0, scale))
)
profile.tags[table].matrix = matrix
profchanged = True
else:
break
elif (
getcfg("profile.b2a.hires")
and getcfg("profile.b2a.hires.smooth")
and gamap
):
# Smooth existing B2A tables
linebuffered_logfiles = []
if (
sys.stdout
and hasattr(sys.stdout, "isatty")
and sys.stdout.isatty()
):
linebuffered_logfiles.append(print)
else:
linebuffered_logfiles.append(log)
if self.sessionlogfile:
linebuffered_logfiles.append(self.sessionlogfile)
logfiles = Files(
[
LineBufferedStream(
FilteredStream(
Files(linebuffered_logfiles),
enc,
discard="",
linesep_in="\n",
triggers=[],
)
),
self.recent,
self.lastmsg,
]
)
smooth_tables = []
for tableno in (0, 2):
table = profile.tags.get(f"B2A{tableno:d}")
if table in smooth_tables:
continue
if self.smooth_B2A(
profile,
tableno,
getcfg("profile.b2a.hires.diagpng") and 2,
logfile=logfiles,
):
smooth_tables.append(table)
profchanged = True
self.errors = errors
self.output = output
self.retcode = retcode
if not isinstance(result, Exception) and result:
for gamap_profile in gamap_profiles:
if (
gamap_profile
and os.path.dirname(gamap_profile.fileName) == self.tempdir
):
# Remove temporary source profile
os.remove(gamap_profile.fileName)
if profchanged and tables:
# Make sure we match Argyll colprof i.e. have a complete
# set of tables
if "A2B1" not in profile.tags:
profile.tags.A2B1 = profile.tags.A2B0
if "A2B2" not in profile.tags:
profile.tags.A2B2 = profile.tags.A2B0
if (
profile.colorSpace == b"RGB"
and profile.connectionColorSpace in (b"XYZ", b"Lab")
and "A2B0" in profile.tags
and (getcfg("profile.b2a.hires") or "B2A1" in profile.tags)
):
# Apply BPC to A2B0/A2B2 to match B2A0/B2A2 black
if profchanged:
# We need to write the changed profile
try:
profile.write()
except Exception as exception:
return exception
if "A2B1" not in profile.tags:
self.log("Generating A2B1 by copying A2B0")
profile.tags.A2B1 = profile.tags.A2B0
A2B1 = profile.tags.get("A2B1")
a2b_tables = [0]
if profile.tags.get("B2A2"):
a2b_tables.append(2)
D50 = colormath.get_whitepoint("D50")
XYZrgb = self.xicclu(
profile, [[1, 0, 0], [0, 1, 0], [0, 0, 1]], pcs="x"
)
Xr, Yr, Zr = XYZrgb[0]
Xg, Yg, Zg = XYZrgb[1]
Xb, Yb, Zb = XYZrgb[2]
m1 = colormath.Matrix3x3(
((Xr, Xg, Xb), (Yr, Yg, Yb), (Zr, Zg, Zb))
).inverted()
Sr, Sg, Sb = m1 * D50
m2 = colormath.Matrix3x3(
(
(Sr * Xr, Sg * Xg, Sb * Xb),
(Sr * Yr, Sg * Yg, Sb * Yb),
(Sr * Zr, Sg * Zg, Sb * Zb),
)
).inverted()
for tableno in a2b_tables:
tablename = f"A2B{tableno:d}"
table = profile.tags.get(tablename)
if not table:
continue
if tables:
# Get inverse B2A0/B2A2 black
XYZbp_B2A = self.xicclu(
profile,
[[0, 0, 0]],
intent="prs"[tableno],
direction="ib",
pcs="x",
)[0]
XYZbp_B2A = tuple(XYZbp_B2A)
else:
XYZbp_B2A = (0, 0, 0)
Labbp_B2A = colormath.XYZ2Lab(*XYZbp_B2A, scale=1)
if XYZbp and (0, 0, 0) not in (XYZbp, XYZbp_B2A):
# Make it same hue as relcol black
self.log(
f"Inverse B2A{tableno:d}",
"blackpoint L*a*b* {:.6f} {:.6f} {:.6f}".format(
*Labbp_B2A
),
)
if XYZbp_B2A != (0, 0, 0):
# Always...
continue
Labbp_B2A = (Labbp_B2A[0], Labbp[1], Labbp[2])
self.log(
"Adjusted",
tablename,
"blackpoint L*a*b* {:.6f} {:.6f} {:.6f}".format(
*Labbp_B2A
),
)
XYZbp_B2A = colormath.Lab2XYZ(*Labbp_B2A)
if XYZbp_B2A == XYZbp:
continue
if table is A2B1 and not getcfg(
"profile.black_point_compensation"
):
# Make A2B0/A2B2 a distinct table
self.log("Making distinct", tablename)
table = LUT16Type(None, tablename, profile)
table.matrix = []
for row in A2B1.matrix:
table.matrix.append(list(row))
table.input = []
table.output = []
for curves in ("input", "output"):
for channel in getattr(A2B1, curves):
getattr(table, curves).append(list(channel))
table.clut = []
for block in A2B1.clut:
table.clut.append([])
for row in block:
table.clut[-1].append(list(row))
profile.tags[tablename] = table
else:
table = profile.tags[tablename]
# Apply BPC to A2B0/A2B2
self.log(
f"Applying black point compensation to {tablename}:",
"L*a*b* {:.6f} {:.6f} {:.6f}".format(*Labbp_B2A),
)
table.apply_black_offset(
XYZbp_B2A, None, self.thread_abort, lang.getstr("aborted")
)
profchanged = True
if XYZbp:
# Adjust for BPC
if XYZbp_B2A != (0, 0, 0):
XYZbp_A2B = XYZbp_B2A
else:
XYZbp_A2B = [v * Ybp for v in D50]
XYZtmp = list(XYZrgb)
for i, XYZ in enumerate(XYZtmp):
XYZtmp[i] = colormath.blend_blackpoint(
*XYZ, bp_in=(0, 0, 0), bp_out=XYZbp_A2B
)
Xr, Yr, Zr = XYZtmp[0]
Xg, Yg, Zg = XYZtmp[1]
Xb, Yb, Zb = XYZtmp[2]
m3 = colormath.Matrix3x3(
((Xr, Xg, Xb), (Yr, Yg, Yb), (Zr, Zg, Zb))
).inverted()
Sr, Sg, Sb = m3 * D50
m4 = colormath.Matrix3x3(
(
(Sr * Xr, Sg * Xg, Sb * Xb),
(Sr * Yr, Sg * Yg, Sb * Yb),
(Sr * Zr, Sg * Zg, Sb * Zb),
)
)
if debug:
for i in range(3):
self.log(
colormath.XYZ2xyY(*XYZrgb[i]),
colormath.XYZ2xyY(*XYZtmp[i]),
colormath.XYZ2xyY(*m4 * (m2 * XYZrgb[i])),
)
interp = []
rinterp = []
osize = len(table.output[0])
omaxv = osize - 1.0
orange = [i / omaxv * 65535 for i in range(osize)]
for i in range(3):
interp.append(colormath.Interp(orange, table.output[i]))
rinterp.append(
colormath.Interp(table.output[i], orange)
)
if len(table.clut[0]) < 33:
num_workers = 1
else:
num_workers = None
table.clut = sum(
pool_slice(
_mp_apply,
table.clut,
(
profile.connectionColorSpace,
colormath.matmul,
(m4, m2),
D50,
interp,
rinterp,
lang.getstr("aborted"),
),
{},
num_workers,
self.thread_abort,
),
[],
)
# A2B processing
process_A2B = (
"A2B0" in profile.tags
and profile.colorSpace == b"RGB"
and profile.connectionColorSpace in (b"XYZ", b"Lab")
and (
getcfg("profile.b2a.hires")
or getcfg("profile.quality.b2a") in ("l", "n")
or not has_B2A
)
)
if process_A2B:
if getcfg("profile.b2a.hires") or not has_B2A:
if profchanged:
# We need to write the changed profile before
# enhancing B2A resolution!
try:
profile.write()
except Exception as exception:
return exception
result = self.update_profile_B2A(profile)
if not isinstance(result, Exception) and result:
profchanged = True
# All table processing done
if not isinstance(result, Exception) and result:
if profchanged and tables:
# Make sure we match Argyll colprof i.e. have a complete
# set of tables
if profile.colorSpace != b"RGB":
if len(tables) == 1:
# We only created a colorimetric table, the
# others are still low quality. Assign
# colorimetric table
profile.tags.B2A0 = profile.tags.B2A1
if len(tables) < 3:
# We only created colorimetric and/or perceptual
# tables, saturation table is still low quality.
# Assign perceptual table
profile.tags.B2A2 = profile.tags.B2A0
if "B2A2" not in profile.tags:
profile.tags.B2A2 = profile.tags.B2A0
apply_bpc = (
getcfg("profile.black_point_compensation")
and "A2B0" not in profile.tags
) or (process_A2B and (getcfg("profile.b2a.hires") or not has_B2A))
# If profile type is X (XYZ cLUT + matrix) add the matrix tags
# from a lookup of a smaller testchart or A2B/B2A (faster
# computation!). If profile is a shaper+matrix profile,
# re-generate shaper curves from testchart with only
# gray+primaries (better curve smoothness and neutrality)
# Make sure we do this *after* all changes on the A2B/B2A tables
# are done, because we may end up using them for lookup!
if (
getcfg("profile.type") in ("X", "s", "S")
and profile.colorSpace == b"RGB"
):
if getcfg("profile.type") == "X":
if (
not isinstance(
profile.tags.get("vcgt"), VideoCardGammaType
)
or profile.tags.vcgt.is_linear()
):
# Use matrix from 3x shaper curves profile if vcgt
# is linear
ptype = "s"
if profchanged:
# We need to write the changed profile before
# creating TRC tags because we will be using
# lookup through A2B/B2A table!
try:
profile.write()
except Exception as exception:
return exception
else:
# Use matrix from single shaper curve profile if
# vcgt is nonlinear
ptype = "S"
else:
ptype = getcfg("profile.type")
result = self._create_matrix_profile(
args[-1], profile, ptype, "XYZ", apply_bpc
)
if isinstance(result, ICCProfile):
result = True
profchanged = True
if not isinstance(result, Exception) and result:
if (
"rTRC" in profile.tags
and "gTRC" in profile.tags
and "bTRC" in profile.tags
and isinstance(profile.tags.rTRC, CurveType)
and isinstance(profile.tags.gTRC, CurveType)
and isinstance(profile.tags.bTRC, CurveType)
and apply_bpc
and len(profile.tags.rTRC) > 1
and len(profile.tags.gTRC) > 1
and len(profile.tags.bTRC) > 1
and (
profile.tags.rTRC[0] != 0
or profile.tags.gTRC[0] != 0
or profile.tags.bTRC[0] != 0
)
):
self.log("-" * 80)
for component in ("r", "g", "b"):
self.log(f"Applying black point compensation to {component}TRC")
profile.apply_black_offset(
(0, 0, 0), include_A2B=False, set_blackpoint=False
)
if getcfg("profile.black_point_compensation"):
bpc_applied = True
profchanged = True
if profchanged and not isinstance(result, Exception) and result:
if "bkpt" in profile.tags and bpc_applied:
# We need to update the blackpoint tag
try:
odata = self.xicclu(profile, (0, 0, 0), intent="a", pcs="x")
if len(odata) != 1 or len(odata[0]) != 3:
raise ValueError(f"Blackpoint is invalid: {odata}")
except Exception as exception:
self.log(exception)
else:
(
profile.tags.bkpt.X,
profile.tags.bkpt.Y,
profile.tags.bkpt.Z,
) = odata[0]
# We need to write the changed profile
try:
profile.write()
except Exception as exception:
return exception
if os.path.isfile(args[-1] + ".ti3.backup") and os.path.isfile(
args[-1] + ".ti3"
):
# Restore backed up TI3
os.rename(args[-1] + ".ti3", args[-1] + ".bpc.ti3")
os.rename(args[-1] + ".ti3.backup", args[-1] + ".ti3")
ti3_file = open(args[-1] + ".ti3", "rb")
ti3 = ti3_file.read()
ti3_file.close()
elif not is_regular_grid and not is_primaries_only:
ti3 = None
if not isinstance(result, Exception) and result:
# Always explicitly do profile self check
self.exec_cmd(
get_argyll_util("profcheck"),
[args[-1] + ".ti3", args[-1] + profile_ext],
capture_output=True,
skip_scripts=True,
)
# Get profile max and avg err to be later added to metadata
# Argyll outputs the following:
# Profile check complete, peak err = x.xxxxxx, avg err = x.xxxxxx, RMS = x.xxxxxx
peak = None
avg = None
rms = None
for line in self.output:
if line.startswith("Profile check complete"):
peak = re.search(r"(?:peak err|max\.) = (\d+(?:\.\d+))", line)
avg = re.search(r"avg(?: err|\.) = (\d+(?:\.\d+))", line)
rms = re.search(r"RMS = (\d+(?:\.\d+))", line)
if peak:
peak = peak.groups()[0]
if avg:
avg = avg.groups()[0]
if rms:
rms = rms.groups()[0]
break
if not isinstance(result, Exception) and result:
(gamut_volume, gamut_coverage) = self.create_gamut_views(profile_path)
self.log("-" * 80)
if not isinstance(result, Exception) and result:
result = self.update_profile(
profile,
ti3,
chrm,
tags,
avg,
peak,
rms,
gamut_volume,
gamut_coverage,
quality=getcfg("profile.quality"),
)
result2 = self.wrapup(
not isinstance(result, UnloggedInfo) and result, dst_path=dst_path
)
if isinstance(result2, Exception):
if isinstance(result, Exception):
result = Error(str(result) + "\n\n" + str(result2))
else:
result = result2
elif not isinstance(result, Exception) and result:
setcfg("last_cal_or_icc_path", dst_path)
setcfg("last_icc_path", dst_path)
return result
def create_RGB_XYZ_cLUT_fwd_profile(
self,
ti3,
description,
copyright,
manufacturer,
model,
logfn=None,
clutres=33,
cat="Bradford",
):
"""Create a RGB to XYZ forward profile"""
# Extract grays and remaining colors
(ti3_extracted, RGB_XYZ, remaining) = extract_device_gray_primaries(
ti3, True, logfn
)
bwd_matrix = colormath.Matrix3x3([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
# Check if we have calibration, if so, add vcgt
vcgt = False
options_dispcal = []
is_hq_cal = False
for cgats in ti3.values():
if cgats.type == b"CAL":
vcgt = cal_to_vcgt(cgats)
options_dispcal = get_options_from_cal(cgats)[0]
is_hq_cal = "qh" in options_dispcal
dEs = []
RGB_XYZ = dict_sort(RGB_XYZ)
for X, Y, Z in RGB_XYZ.values():
if Y >= 1:
X, Y, Z = colormath.adapt(X, Y, Z, RGB_XYZ[(100, 100, 100)], cat=cat)
L, a, b = colormath.XYZ2Lab(X, Y, Z)
dE = colormath.delta(L, 0, 0, L, a, b, "00")["E"]
dEs.append(dE)
if debug or verbose > 1:
self.log(
"L* {:5.2f} a* {:5.2f} b* {:5.2f} dE*00 {:4.2f}".format(
L, a, b, dE
)
)
dE_avg = sum(dEs) / len(dEs)
dE_max = max(dEs)
self.log("R=G=B (>= 1% luminance) dE*00 avg", dE_avg, "peak", dE_max)
# Create profile
profile = ICCProfile()
profile.version = 2.2 # Match ArgyllCMS
profile.setDescription(description)
profile.setCopyright(copyright)
if manufacturer:
profile.setDeviceManufacturerDescription(manufacturer)
if model:
profile.setDeviceModelDescription(model)
luminance_XYZ_cdm2 = ti3.queryv1("LUMINANCE_XYZ_CDM2")
if luminance_XYZ_cdm2:
profile.tags.lumi = XYZType(profile=profile)
profile.tags.lumi.Y = float(luminance_XYZ_cdm2.split()[1])
profile.tags.wtpt = XYZType(profile=profile)
white_XYZ = [v / 100.0 for v in RGB_XYZ[(100, 100, 100)]]
(profile.tags.wtpt.X, profile.tags.wtpt.Y, profile.tags.wtpt.Z) = white_XYZ
profile.tags.bkpt = XYZType(profile=profile)
black_XYZ = [v / 100.0 for v in RGB_XYZ[(0, 0, 0)]]
(profile.tags.bkpt.X, profile.tags.bkpt.Y, profile.tags.bkpt.Z) = black_XYZ
profile.tags.arts = chromaticAdaptionTag()
profile.tags.arts.update(colormath.get_cat_matrix(cat))
# Check if we have calibration, if so, add vcgt
if vcgt:
profile.tags.vcgt = vcgt
# Interpolate shaper curves from grays - returned curves are adapted
# to D50
self.single_curve = round(dE_max, 2) <= 1.25 and round(dE_avg, 2) <= 0.5
if self.single_curve:
self.log(
"Got high quality calibration, using single device to PCS "
"shaper curve for cLUT"
)
gray = create_shaper_curves(
RGB_XYZ,
bwd_matrix,
self.single_curve,
getcfg("profile.black_point_compensation"),
logfn,
profile=profile,
options_dispcal=options_dispcal,
optimize=self.single_curve,
cat=cat,
)
curves = [curve[:] for curve in gray]
# Add black back in
XYZbp = [
v / 100.0
for v in colormath.adapt(
*RGB_XYZ[(0, 0, 0)], whitepoint_source=RGB_XYZ[(100, 100, 100)], cat=cat
)
]
for i in range(len(gray[0])):
(gray[0][i], gray[1][i], gray[2][i]) = colormath.apply_bpc(
*(curve[i] for curve in gray), bp_in=(0, 0, 0), bp_out=XYZbp
)
grayinterp = [
colormath.Interp(list(range(len(curve))), curve, use_numpy=True)
for curve in gray
]
def get_XYZ_from_curves(n, m):
# Curves are adapted to D50
return [interp((len(interp.xp) - 1.0) / m * n) for interp in grayinterp]
# Quantize RGB to make lookup easier
# XXX Note that round(50 * 2.55) = 127, but
# round(50 / 100 * 255) = 128 (the latter is what we want)!
remaining = dict(
[
(tuple(round(k / 100.0 * 255) for k in RGB), XYZ)
for RGB, XYZ in remaining.items()
]
)
# Need to sort so columns increase (fastest to slowest) B G R
remaining = dict_sort(remaining)
# Build initial cLUT
# Try to fill a 5x5x5 or 3x3x3 cLUT
clut_actual = 0
for iclutres in (17, 9, 5, 4, 3, 2):
clut = []
step = 100 / (iclutres - 1.0)
for a in range(iclutres):
for b in range(iclutres):
clut.append([])
for c in range(iclutres):
# XXX Note that round(50 * 2.55) = 127, but
# round(50 / 100 * 255) = 128 (the latter is what we want)!
RGB = tuple(round((k * step) / 100.0 * 255) for k in (a, b, c))
# Prefer actual measurements over interpolated values
XYZ = remaining.get(RGB)
if not XYZ:
if a == b == c:
# Fall back to interpolated values for gray
# (already black scaled)
XYZ = get_XYZ_from_curves(a, iclutres - 1)
# Range 0..1
clut[-1].append(XYZ)
elif iclutres > 2:
# Try next smaller cLUT res
break
else:
raise ValueError(
"Measurement data is missing RGB "
"{:.4f} {:.4f} {:.4f}".format(*RGB)
)
else:
clut_actual += 1
X, Y, Z = (v / 100.0 for v in XYZ)
# # Need to black scale actual measurements
# X, Y, Z = colormath.blend_blackpoint(X, Y, Z,
# black_XYZ, None, white_XYZ)
# Range 0..1
clut[-1].append(
colormath.adapt(X, Y, Z, white_XYZ, cat=cat)
)
if c < iclutres - 1:
break
if b < iclutres - 1:
break
if a == iclutres - 1:
break
self.log("Initial cLUT resolution {:d}x{:d}x{:d}".format(*(iclutres,) * 3))
profile.tags.A2B0 = create_RGB_A2B_XYZ(curves, clut, self.log)
# Interpolate to higher cLUT resolution
quality = getcfg("profile.quality")
clutres = {"m": 17, "l": 9}.get(quality, clutres)
# XXX: Need to implement proper 3D interpolation
if clutres > iclutres:
# Lookup input RGB to interpolated XYZ
RGB_in = []
step = 100 / (clutres - 1.0)
for a in range(clutres):
for b in range(clutres):
for c in range(clutres):
RGB_in.append([a * step, b * step, c * step])
XYZ_out = self.xicclu(profile, RGB_in, "a", pcs="X", scale=100)
profile.fileName = None
# Create new cLUT
clut = []
i = -1
actual = 0
interpolated = 0
for a in range(clutres):
for b in range(clutres):
clut.append([])
for c in range(clutres):
# XXX Note that round(50 * 2.55) = 127, but
# round(50 / 100 * 255) = 128 (the latter is what we want)!
RGB = tuple(round((k * step) / 100.0 * 255) for k in (a, b, c))
# Prefer actual measurements over interpolated values
prev_actual = actual
XYZ = remaining.get(RGB)
i += 1
if not XYZ:
# Fall back to interpolated values
# (already black scaled)
if a == b == c:
XYZ = get_XYZ_from_curves(a, clutres - 1)
# Range 0..1
clut[-1].append(XYZ)
continue
else:
XYZ = XYZ_out[i]
interpolated += 1
else:
actual += 1
X, Y, Z = (v / 100.0 for v in XYZ)
# if actual > prev_actual:
# # Need to black scale actual measurements
# X, Y, Z = colormath.blend_blackpoint(X, Y, Z,
# black_XYZ, None, white_XYZ)
# Range 0..1
clut[-1].append(colormath.adapt(X, Y, Z, white_XYZ, cat=cat))
if actual > clut_actual:
# Did we get any additional actual measured cLUT points?
self.log(
"cLUT resolution {:d}x{:d}x{:d}: Got {:d} additional values from "
"actual measurements".format(
clutres, clutres, clutres, actual - clut_actual
)
)
profile.tags.A2B0 = create_RGB_A2B_XYZ(curves, clut, self.log)
clut_actual = actual
self.log(
"Final interpolated cLUT resolution {:d}x{:d}x{:d}".format(
*((len(profile.tags.A2B0.clut[0]),) * 3)
)
)
profile.tags.A2B0.profile = profile
# # Add black back in
# black_XYZ_D50 = colormath.adapt(*black_XYZ, whitepoint_source=white_XYZ, cat=cat)
# profile.tags.A2B0.apply_black_offset(black_XYZ_D50)
return profile
def _create_simple_matrix_profile(
self,
ti3_RGB_XYZ,
ti3_remaining,
desc,
copyright="No copyright",
display_manufacturer=None,
display_name=None,
bpc=False,
cat="Bradford",
):
self.log("-" * 80)
self.log("Creating matrix from primaries")
xy = []
XYZbp = ti3_RGB_XYZ[(0, 0, 0)]
XYZwp = ti3_RGB_XYZ[(100, 100, 100)]
for R, G, B in [(100, 0, 0), (0, 100, 0), (0, 0, 100), (100, 100, 100)]:
if R == G == B:
RGB_XYZ = ti3_RGB_XYZ
else:
RGB_XYZ = ti3_remaining
X, Y, Z = RGB_XYZ[(R, G, B)]
if XYZbp != (0, 0, 0) and not bpc:
# Adjust for black offset
X, Y, Z = colormath.blend_blackpoint(X, Y, Z, XYZbp, (0, 0, 0), XYZwp)
xy.append(colormath.XYZ2xyY(*(v / 100 for v in (X, Y, Z)))[:2])
self.log("Using chromatic adaptation transform matrix:", cat)
mtx = ICCProfile.from_chromaticities(
xy[0][0],
xy[0][1],
xy[1][0],
xy[1][1],
xy[2][0],
xy[2][1],
xy[3][0],
xy[3][1],
2.2, # Will be replaced
desc,
copyright,
display_manufacturer,
display_name,
cat=cat,
)
return mtx
def _create_matrix_profile(
self, outname, profile=None, ptype="s", omit=None, bpc=False, cat="Bradford"
):
"""Create matrix profile from lookup through ti3
<outname>.ti3 has to exist.
If <profile> is given, it has to be an ICCProfile instance, and the
matrix tags will be added to this profile.
<ptype> should be the type of profile, i.e. one of g, G, s or S.
<omit> if given should be the tag to omit, either 'TRC' or 'XYZ'.
We use a testchart with only gray + primaries for TRC,
and larger testchart for the matrix. This should give the smoothest
overall result.
Returns an ICCProfile with fileName set to <outname>.ic[cm].
"""
if profile:
cat = profile.guess_cat() or cat
if omit == "XYZ":
tags = "shaper"
else:
tags = "shaper+matrix"
self.log("-" * 80)
self.log(f"Creating {tags} tags in separate step")
self.log("Using chromatic adaptation transform matrix:", cat)
fakeread = get_argyll_util("fakeread")
if not fakeread:
return Error(lang.getstr("argyll.util.not_found", "fakeread"))
colprof = get_argyll_util("colprof")
if not colprof:
return Error(lang.getstr("argyll.util.not_found", "colprof"))
# Strip potential CAL from Ti3
try:
oti3 = CGATS(outname + ".ti3")
except (IOError, CGATSError) as exception:
return exception
else:
if 0 in oti3:
ti3 = oti3[0]
ti3.filename = oti3.filename # So we can log the name
ti3.fix_zero_measurements(logfile=self.get_logfiles(False))
else:
return Error(
lang.getstr("error.measurement.file_invalid", outname + ".ti3")
)
for ti1name, tagcls in [("d3-e4-s3-g52-m3-b0-f0", "XYZ"), (None, "TRC")]:
if tagcls == omit:
continue
elif (
tagcls == "TRC" and profile and "A2B0" in profile.tags and ptype == "s"
):
# Create TRC from forward lookup through A2B
numentries = 256
maxval = numentries - 1.0
RGBin = []
for i in range(numentries):
RGBin.append((i / maxval,) * 3)
if "B2A0" in profile.tags:
# Inverse backward
direction = "ib"
else:
# Forward
direction = "f"
try:
XYZout = self.xicclu(profile, RGBin, "p", direction, pcs="x")
except Info as exception:
return exception
# Get RGB space from already added matrix column tags
rgb_space = colormath.get_rgb_space(profile.get_rgb_space("pcs", 1))
mtx = rgb_space[-1].inverted()
self.log("-" * 80)
for channel in "rgb":
tagname = channel + tagcls
self.log(
"Adding {} from {} lookup to {}".format(
tagname,
{
"f": "forward A2B0",
"ib": "inverse backward B2A0",
}.get(direction),
profile.getDescription(),
)
)
profile.tags[tagname] = CurveType()
for XYZ in XYZout:
RGB = mtx * XYZ
for i, channel in enumerate("rgb"):
tagname = channel + tagcls
profile.tags[tagname].append(min(max(RGB[i], 0), 1) * 65535)
break
if not ti1name:
# Extract gray+primaries into new TI3
(ti3, RGB_XYZ, remaining) = extract_device_gray_primaries(
ti3, tagcls == "TRC", self.log
)
ti3.sort_by_RGB()
self.log(ti3.DATA)
if tagcls == "TRC" and profile:
rgb_space = colormath.get_rgb_space(profile.get_rgb_space("pcs", 1))
fwd_mtx = rgb_space[-1]
bwd_mtx = fwd_mtx.inverted()
self.log("-" * 80)
options_dispcal = get_options_from_ti3(oti3)[0]
curves = create_shaper_curves(
RGB_XYZ,
bwd_mtx,
ptype == "S",
bpc,
self.log,
profile=profile,
options_dispcal=options_dispcal,
optimize=ptype == "S",
cat=cat,
)
for i, channel in enumerate("rgb"):
tagname = channel + tagcls
self.log(
"Adding {} from interpolation to {}".format(
tagname, profile.getDescription()
)
)
profile.tags[tagname] = trc = CurveType(profile=profile)
# Slope limit for 16-bit encoding
trc[:] = [
max(v, j / 65535.0) * 65535 for j, v in enumerate(curves[i])
]
if not bpc:
XYZbp = None
for R, G, B in RGB_XYZ:
X, Y, Z = RGB_XYZ[(R, G, B)]
if R == G == B == 0:
XYZbp = [v / 100 for v in (X, Y, Z)]
XYZbp = colormath.adapt(
*XYZbp,
whitepoint_source=RGB_XYZ[(100, 100, 100)],
cat=cat,
)
if XYZbp:
# Add black back in
profile.apply_black_offset(XYZbp, include_A2B=False)
if ptype == "S":
# Single curve
profile.tags["rTRC"][:] = profile.tags["gTRC"][:]
profile.tags["bTRC"][:] = profile.tags["gTRC"][:]
break
ti3.write(f"{outname}.0.ti3")
if ti1name:
ti1 = get_data_path(f"ti1/{ti1name}.ti1")
if not ti1:
return Error(lang.getstr("file.missing", f"ti1/{ti1name}.ti1"))
fakeout = f"{outname}.{ti1name}"
try:
shutil.copyfile(ti1, f"{fakeout}.ti1")
except EnvironmentError as exception:
return exception
# Lookup ti1 through ti3
result = self.exec_cmd(
fakeread,
[f"{outname}.0.ti3", fakeout],
capture_output=True,
skip_scripts=True,
sessionlogfile=self.sessionlogfile,
)
try:
os.remove(f"{fakeout}.ti1")
except EnvironmentError as exception:
self.log(exception)
if not result:
return UnloggedError("\n".join(self.errors))
elif isinstance(result, Exception):
return result
try:
os.remove(f"{outname}.0.ti3")
except EnvironmentError as exception:
self.log(exception)
else:
# Use gray+primaries from existing ti3
fakeout = f"{outname}.0"
result = self.exec_cmd(
colprof,
["-v", "-q" + getcfg("profile.quality"), "-a" + ptype, fakeout],
sessionlogfile=self.sessionlogfile,
)
try:
os.remove(f"{fakeout}.ti3")
except EnvironmentError as exception:
self.log(exception)
if isinstance(result, Exception) or not result:
return result
try:
matrix_profile = ICCProfile(fakeout + profile_ext)
except (IOError, ICCProfileInvalidError):
return Error(
lang.getstr("profile.invalid") + "\n" + fakeout + profile_ext
)
if profile:
self.log("-" * 80)
for channel in "rgb":
tagname = channel + tagcls
tag = matrix_profile.tags.get(tagname)
if tag:
self.log(
"Adding {} from matrix profile to {}".format(
tagname, profile.getDescription()
)
)
profile.tags[tagname] = tag
else:
self.log(lang.getstr("profile.required_tags_missing", tagname))
else:
profile = matrix_profile
profile.fileName = outname + profile_ext
try:
os.remove(fakeout + profile_ext)
except EnvironmentError as exception:
self.log(exception)
return profile
def update_profile(
self,
profile,
ti3=None,
chrm=None,
tags=None,
avg=None,
peak=None,
rms=None,
gamut_volume=None,
gamut_coverage=None,
quality=None,
):
"""Update profile tags and metadata"""
if isinstance(profile, str):
profile_path = profile
try:
profile = ICCProfile(profile_path)
except (IOError, ICCProfileInvalidError):
return Error(lang.getstr("profile.invalid") + "\n" + profile_path)
else:
profile_path = profile.fileName
if (
profile.profileClass == b"mntr"
and profile.colorSpace == b"RGB"
and not (self.tempdir and profile_path.startswith(self.tempdir))
):
setcfg("last_cal_or_icc_path", profile_path)
setcfg("last_icc_path", profile_path)
if ti3:
# Embed original TI3
profile.tags.targ = profile.tags.DevD = profile.tags.CIED = TextType(
b"text\0\0\0\0" + bytes(ti3) + b"\0", b"targ"
)
if chrm:
# Add ChromaticityType tag
profile.tags.chrm = chrm
# Fixup desc tags - ASCII needs to be 7-bit
# also add Unicode strings if different from ASCII
if "desc" in profile.tags and isinstance(
profile.tags.desc, TextDescriptionType
):
profile.setDescription(profile.getDescription())
if "dmdd" in profile.tags and isinstance(
profile.tags.dmdd, TextDescriptionType
):
profile.setDeviceModelDescription(profile.getDeviceModelDescription())
if "dmnd" in profile.tags and isinstance(
profile.tags.dmnd, TextDescriptionType
):
profile.setDeviceManufacturerDescription(
profile.getDeviceManufacturerDescription()
)
if tags and tags is not True:
# Add custom tags
for tagname in tags:
tag = tags[tagname]
if tagname == "mmod":
profile.device["manufacturer"] = (
b"\0\0" + tag["manufacturer"][1] + tag["manufacturer"][0]
)
profile.device["model"] = (
b"\0\0" + tag["model"][0] + tag["model"][1]
)
profile.tags[tagname] = tag
elif tags is True:
edid = self.get_display_edid()
if edid:
profile.device["manufacturer"] = (
b"\0\0" + edid["edid"][9:10] + edid["edid"][8:9]
)
profile.device["model"] = (
b"\0\0" + edid["edid"][11:12] + edid["edid"][10:11]
)
# Add Apple-specific 'mmod' tag (TODO: need full spec)
mmod = (
b"mmod"
+ (b"\x00" * 6)
+ edid["edid"][8:10]
+ (b"\x00" * 2)
+ edid["edid"][11:12]
+ edid["edid"][10:11]
+ (b"\x00" * 4)
+ (b"\x00" * 20)
)
profile.tags.mmod = ICCProfileTag(mmod, "mmod")
# Add new meta information based on EDID
profile.set_edid_metadata(edid)
elif "meta" not in profile.tags:
# Make sure meta tag exists
profile.tags.meta = DictType()
if tags is True or (tags and "meta" in tags):
profile.tags.meta.update(
{"CMF_product": appname, "CMF_binary": appname, "CMF_version": version}
)
# Set license
profile.tags.meta["License"] = getcfg("profile.license")
# Set profile quality
quality = {"v": "very low", "l": "low", "m": "medium", "h": "high"}.get(
quality
)
if quality:
profile.tags.meta["Quality"] = quality
spec_prefixes = "CMF_"
if tags is True:
# Set OPENICC_automatic_generated to "0"
profile.tags.meta["OPENICC_automatic_generated"] = "0"
# Set GCM DATA_source to "calib"
profile.tags.meta["DATA_source"] = "calib"
# Add instrument
profile.tags.meta["MEASUREMENT_device"] = self.get_instrument_name().lower()
spec_prefixes = "CMF_,DATA_,MEASUREMENT_,OPENICC_"
# Add screen brightness if applicable
if sys.platform not in ("darwin", "win32") and dbus_session:
try:
iface = DBusObject(
BUSTYPE_SESSION,
"org.gnome.SettingsDaemon.Power",
"/org/gnome/SettingsDaemon/Power",
"Screen",
)
brightness = iface.get_percentage()
except DBusException:
pass
else:
profile.tags.meta["SCREEN_brightness"] = str(brightness)
spec_prefixes += ",SCREEN_"
# Set device ID
device_id = self.get_device_id(quirk=False, query=True)
if device_id:
profile.tags.meta["MAPPING_device_id"] = device_id
spec_prefixes += ",MAPPING_"
if tags is True or (tags and "meta" in tags):
prefix = profile.tags.meta.getvalue("prefix", b"", None)
if isinstance(prefix, bytes):
prefix = prefix.decode("utf-8")
prefixes = (prefix or spec_prefixes).split(",")
for prefix in spec_prefixes.split(","):
if prefix not in prefixes:
prefixes.append(prefix)
profile.tags.meta["prefix"] = ",".join(prefixes)
if (avg, peak, rms) != (None,) * 3:
# Make sure meta tag exists
if "meta" not in profile.tags:
profile.tags.meta = DictType()
# Update meta prefix
prefix = profile.tags.meta.getvalue("prefix", b"", None)
if isinstance(prefix, bytes):
prefix = prefix.decode("utf-8")
prefixes = (prefix or "ACCURACY_").split(",")
if "ACCURACY_" not in prefixes:
prefixes.append("ACCURACY_")
profile.tags.meta["prefix"] = ",".join(prefixes)
# Add error info
if avg is not None:
profile.tags.meta["ACCURACY_dE76_avg"] = avg
if peak is not None:
profile.tags.meta["ACCURACY_dE76_max"] = peak
if rms is not None:
profile.tags.meta["ACCURACY_dE76_rms"] = rms
profile.set_gamut_metadata(gamut_volume, gamut_coverage)
# Set default rendering intent
if "B2A0" in profile.tags and (
"B2A1" in profile.tags or "B2A2" in profile.tags
):
profile.intent = {"p": 0, "r": 1, "s": 2, "a": 3}[
getcfg("gamap_default_intent")
]
# Check if we need to video scale vcgt
if isinstance(profile.tags.get("vcgt"), VideoCardGammaType):
try:
cal = extract_cal_from_profile(profile, None, False, prefer_cal=True)
except Exception as exception:
self.log(exception)
else:
white = False
if cal:
if cal.queryv1("TV_OUTPUT_ENCODING") == "YES":
black, white = (16, 235)
else:
output_enc = cal.queryv1("OUTPUT_ENCODING")
if output_enc:
try:
black, white = (float(v) for v in output_enc.split())
except (TypeError, ValueError):
white = False
if white and (black, white) != (0, 255):
self.log(f"Need to scale vcgt to video levels ({black}..{white})")
# Need to create videoscaled vcgt from calibration
data = cal.queryv1("DATA")
if data:
self.log(f"Scaling vcgt to video levels ({black}..{white})")
for entry in data.values():
for column in "RGB":
v_old = entry["RGB_" + column]
# For video encoding the extra bits of
# precision are created by bit shifting rather
# than scaling, so we need to scale the fp
# value to account for this
newmin = (black / 256.0) * (65536 / 65535.0)
newmax = (white / 256.0) * (65536 / 65535.0)
v_new = colormath.convert_range(
v_old, 0, 1, newmin, newmax
)
entry["RGB_" + column] = v_new
profile.tags.vcgt = cal_to_vcgt(cal)
else:
self.log("Warning - no scaling applied - no calibration data!")
# Calculate profile ID
profile.calculateID()
try:
profile.write()
except Exception as exception:
return exception
return True
def get_logfiles(self, include_progress_buffers=True):
linebuffered_logfiles = []
if sys.stdout and hasattr(sys.stdout, "isatty") and sys.stdout.isatty():
linebuffered_logfiles.append(print)
else:
linebuffered_logfiles.append(log)
if self.sessionlogfile:
linebuffered_logfiles.append(self.sessionlogfile)
logfiles = LineBufferedStream(
FilteredStream(
Files(linebuffered_logfiles),
enc,
discard="",
linesep_in="\n",
triggers=[],
)
)
if include_progress_buffers:
logfiles = Files([logfiles, self.recent, self.lastmsg])
return logfiles
def update_profile_B2A(
self,
profile,
generate_perceptual_table=True,
clutres=None,
smooth=None,
rgb_space=None,
):
# Use reverse A2B interpolation to generate B2A table
if not clutres:
if getcfg("profile.b2a.hires"):
# Chosen resolution
clutres = getcfg("profile.b2a.hires.size")
elif getcfg("profile.quality.b2a") in "ln":
# Low quality/resolution
clutres = 9
else:
# Medium quality/resolution
clutres = 17
if smooth is None:
smooth = getcfg("profile.b2a.hires.smooth")
logfiles = self.get_logfiles()
tables = [1]
self.log("-" * 80)
# Add perceptual tables if not present
if "A2B0" in profile.tags and "A2B1" not in profile.tags:
if not isinstance(profile.tags.A2B0, LUT16Type):
self.log(f"{appname}: Can't process non-LUT16Type A2B0 table")
return []
try:
# Copy A2B0
logfiles.write("Generating A2B1 by copying A2B0\n")
profile.tags.A2B1 = profile.tags.A2B0
if "B2A0" in profile.tags:
# Copy B2A0
B2A0 = profile.tags.B2A0
profile.tags.B2A1 = B2A1 = LUT16Type(None, "B2A1", profile)
B2A1.matrix = []
for row in B2A0.matrix:
B2A1.matrix.append(list(row))
B2A1.input = []
B2A1.output = []
for table in ("input", "output"):
for channel in getattr(B2A0, table):
getattr(B2A1, table).append(list(channel))
B2A1.clut = []
for block in B2A0.clut:
B2A1.clut.append([])
for row in block:
B2A1.clut[-1].append(list(row))
except Exception as exception:
return exception
if "A2B2" not in profile.tags and generate_perceptual_table:
# Argyll always creates a complete set of A2B / B2A tables if
# colprof -s (perceptual) or -S (perceptual and saturation) is used,
# so we can assume that if A2B2 is not present then it is safe to
# re-generate B2A0 because it was not created by Argyll CMS.
tables.append(0)
# Invert A2B tables if present. Always invert colorimetric A2B table.
rtables = []
filename = profile.fileName
for tableno in tables:
if f"A2B{tableno:d}" in profile.tags:
if (
f"B2A{tableno:d}" in profile.tags
and profile.tags[f"B2A{tableno:d}"] in rtables
):
continue
if not isinstance(profile.tags[f"A2B{tableno:d}"], LUT16Type):
self.log(
f"{appname}: Can't process non-LUT16Type A2B{tableno:d} table"
)
continue
# Check if we want to apply BPC
bpc = tableno != 1
if bpc and (
profile.tags["A2B1"].clut[0][0] != [0, 0, 0]
or profile.tags["A2B1"].input[0][0] != 0
or profile.tags["A2B1"].input[1][0] != 0
or profile.tags["A2B1"].input[2][0] != 0
or profile.tags["A2B1"].output[0][0] != 0
or profile.tags["A2B1"].output[1][0] != 0
or profile.tags["A2B1"].output[2][0] != 0
):
# Need to apply BPC
table = LUT16Type(profile=profile)
# Copy existing B2A1 table matrix, cLUT and output curves
table.matrix = rtables[0].matrix
table.clut = rtables[0].clut
table.output = rtables[0].output
profile.tags[f"B2A{tableno:d}"] = table
elif bpc:
# BPC not needed, copy existing B2A
profile.tags[f"B2A{tableno:d}"] = rtables[0]
return rtables
if not filename or not os.path.isfile(filename) or bpc:
# Write profile to temp dir
tempdir = self.create_tempdir()
if isinstance(tempdir, Exception):
return tempdir
fd, profile.fileName = tempfile.mkstemp(profile_ext, dir=tempdir)
stream = os.fdopen(fd, "wb")
profile.write(stream)
stream.close()
temp = True
else:
temp = False
# Invert A2B
try:
result = self.generate_B2A_from_inverse_table(
profile,
clutres,
"A2B",
tableno,
bpc,
smooth,
rgb_space,
logfiles,
filename,
bpc,
)
except (Error, Info) as exception:
return exception
except Exception as exception:
self.log(traceback.format_exc())
return exception
else:
if result:
rtables.append(profile.tags[f"B2A{tableno:d}"])
else:
return False
finally:
if temp:
os.remove(profile.fileName)
profile.fileName = filename
return rtables
def is_working(self):
"""Check if any Worker instance is busy. Return True or False."""
for worker in workers:
if not getattr(worker, "finished", True):
return True
return False
def start_measurement(
self,
consumer,
apply_calibration=True,
progress_msg="",
resume=False,
continue_next=False,
):
"""Start a measurement and use a progress dialog for progress
information"""
self.start(
consumer,
self.measure,
wkwargs={"apply_calibration": apply_calibration},
progress_msg=progress_msg,
resume=resume,
continue_next=continue_next,
pauseable=True,
)
def start_calibration(
self, consumer, remove=False, progress_msg="", resume=False, continue_next=False
):
"""Start a calibration and use a progress dialog for progress
information"""
self.start(
consumer,
self.calibrate,
wkwargs={"remove": remove},
progress_msg=progress_msg,
resume=resume,
continue_next=continue_next,
interactive_frame="adjust",
pauseable=True,
)
def madtpg_init(self):
if not hasattr(self, "madtpg"):
if sys.platform == "win32" and getcfg("madtpg.native"):
# Using native implementation (madHcNet32.dll)
self.madtpg = madvr.MadTPG()
else:
# Using madVR net-protocol pure python implementation
self.madtpg = madvr.MadTPG_Net()
self.madtpg.debug = verbose
def madtpg_connect(self):
self.madtpg_init()
self.patterngenerator = self.madtpg
if self.madtpg.connect(method3=madvr.CM_StartLocalInstance, timeout3=3000):
# Check madVR version
madvr_version = self.madtpg.get_version()
if not madvr_version or madvr_version < madvr.min_version:
self.madtpg_disconnect(False)
raise Error(lang.getstr("madvr.outdated", madvr.min_version))
self.log(
"Connected to madVR version {:d}.{:d}.{:d}.{:d} ({})".format(
*(madvr_version + (self.madtpg.uri,))
)
)
self.madtpg.set_osd_text("\u25b6") # "Play" symbol
return True
return False
def madtpg_disconnect(self, restore_settings=True):
"""Restore madVR settings and disconnect"""
if restore_settings:
self.madtpg_restore_settings()
if self.madtpg.disconnect():
self.log("Successfully disconnected from madTPG")
def madtpg_restore_settings(
self, reconnect=True, restore_fullscreen=None, restore_osd=None
):
if restore_fullscreen is None:
restore_fullscreen = getattr(self, "madtpg_previous_fullscreen", None)
if restore_osd is None:
restore_osd = getattr(self, "madtpg_osd", None) is False
if restore_fullscreen or restore_osd:
check = not reconnect or self.madtpg.get_version()
if not check and reconnect:
check = self.madtpg_connect()
if check:
if restore_fullscreen:
# Restore fullscreen
if self.madtpg.set_use_fullscreen_button(True):
self.log(
f"{appname}: Restored madTPG 'use fullscreen' button state"
)
self.madtpg_previous_fullscreen = None
else:
self.log(
f"{appname}: Warning - couldn't restore madTPG "
"'use fullscreen' button state"
)
if not reconnect:
if self.madtpg.is_fse_mode_enabled():
# Allow three seconds for automatic switch to FSE
sleep(3)
# Allow three seconds for fullscreen to settle
sleep(3)
if restore_osd:
# Restore disable OSD
if self.madtpg.set_disable_osd_button(True):
self.log(
f"{appname}: Restored madTPG 'disable OSD' button state"
)
self.madtpg_osd = None
else:
self.log(
f"{appname}: Warning - couldn't restore madTPG "
"'disable OSD' button state"
)
else:
buttons = []
if restore_fullscreen:
buttons.append("'use fullscreen'")
if restore_osd:
buttons.append("'disable OSD'")
self.log(
f"{appname}: Warning - couldn't re-connect to madTPG "
f"to restore {'/'.join(buttons)} button states"
)
def madtpg_show_osd(self, msg=None, leave_fullscreen=False):
"""Show madTPG OSD, optionally with message and leaving fullscreen"""
if self.madtpg.is_fullscreen() and leave_fullscreen:
self.madtpg_previous_fullscreen = True
self.madtpg.set_use_fullscreen_button(False)
self.madtpg_osd = not self.madtpg.is_disable_osd_button_pressed()
if not self.madtpg_osd:
self.madtpg.set_disable_osd_button(False)
if msg:
self.madtpg.set_osd_text(msg)
if self.cmdname == "dispcal":
self.madtpg.show_progress_bar(6)
def measure(self, apply_calibration=True):
"""Measure the configured testchart"""
result = self.detect_video_levels()
if isinstance(result, Exception) or not result:
return result
precond_ti1 = None
precond_ti3 = None
testchart_file = getcfg("testchart.file")
auto = getcfg("testchart.auto_optimize") or 7
if testchart_file == "auto" and auto > 4:
# Testchart auto-optimization
# Create optimized testchart on-the-fly. To do this, create a
# simple profile for preconditioning
if config.get_display_name() == "Untethered":
return Error(
lang.getstr("testchart.auto_optimize.untethered.unsupported")
)
# Use small testchart for grayscale+primaries (34 patches)
precond_ti1_path = get_data_path("ti1/d3-e4-s2-g28-m0-b0-f0.ti1")
precond_ti1 = CGATS(precond_ti1_path)
setcfg("testchart.file", precond_ti1_path)
cmd, args = self.prepare_dispread(apply_calibration)
setcfg("testchart.file", "auto")
if not isinstance(cmd, Exception):
# Measure testchart
result = self.exec_cmd(cmd, args)
if not isinstance(result, Exception) and result:
# Create preconditioning profile
self.pauseable = False
basename = args[-1]
precond_ti3 = CGATS(f"{basename}.ti3")
precond_ti3.fix_zero_measurements(logfile=self.get_logfiles(False))
precond_ti3.write()
# Extract grays and remaining colors
(
ti3_extracted,
ti3_RGB_XYZ,
ti3_remaining,
) = extract_device_gray_primaries(precond_ti3, True, self.log)
profile = self._create_simple_matrix_profile(
ti3_RGB_XYZ, ti3_remaining, os.path.basename(basename)
)
result = self._create_matrix_profile(basename, profile, omit="XYZ")
if not isinstance(result, Exception) and result:
# Write matrix profile
result.write(basename + profile_ext)
# Create optimized testchart
if getcfg("use_fancy_progress"):
# Fade out animation/sound
self.cmdname = get_argyll_utilname("targen")
# Allow time for fade out
sleep(4)
s = min(auto, 11) * 4 - 3
g = s * 3 - 2
f = get_total_patches(4, 4, s, g, auto, auto, 0)
f += 120
cmd, args = (
get_argyll_util("targen"),
[
"-v",
"-d3",
"-e4",
f"-s{s:d}",
f"-g{g:d}",
"-m0",
f"-f{f:d}",
"-A1.0",
],
)
if self.argyll_version >= [1, 1]:
args.append("-G")
if self.argyll_version >= [1, 3, 3]:
args.append("-N0.5")
if self.argyll_version >= [1, 6]:
args.extend(["-B4", "-b0"])
if self.argyll_version >= [1, 6, 2]:
args.append("-V1.6")
args.extend(["-c", basename + profile_ext, basename])
result = self.exec_cmd(cmd, args)
self.pauseable = True
else:
result = cmd
else:
result = True
if testchart_file == "auto" and auto < 5:
# Use pre-baked testchart
if auto == 1:
testchart = "ti1/d3-e4-s2-g28-m0-b0-f0.ti1"
elif auto == 2:
testchart = "ti1/d3-e4-s3-g52-m3-b0-f0.ti1"
elif auto == 3:
testchart = "ti1/d3-e4-s4-g52-m4-b0-f0.ti1"
else:
testchart = "ti1/d3-e4-s5-g52-m5-b0-f0.ti1"
testchart_path = get_data_path(testchart)
if testchart_path:
setcfg("testchart.file", testchart_path)
else:
result = Error(lang.getstr("not_found", testchart))
if not isinstance(result, Exception) and result:
cmd, args = self.prepare_dispread(apply_calibration)
if testchart_file == "auto":
# Restore "auto" setting
setcfg("testchart.file", "auto")
else:
cmd = result
if not isinstance(cmd, Exception) and cmd:
if config.get_display_name() == "Untethered":
cmd, args2 = get_argyll_util("spotread"), ["-v", "-e"]
if getcfg("extra_args.spotread").strip():
args2 += parse_argument_string(getcfg("extra_args.spotread"))
result = self.add_measurement_features(
args2, False, allow_nondefault_observer=is_ccxx_testchart()
)
if isinstance(result, Exception):
return result
else:
args2 = args
result = self.exec_cmd(cmd, args2)
if not isinstance(result, Exception) and result:
self.update_display_name_manufacturer(args[-1] + ".ti3")
ti3 = args[-1] + ".ti3"
if precond_ti3:
# Add patches from preconditioning measurements
ti3 = insert_ti_patches_omitting_RGB_duplicates(
precond_ti3, ti3, self.log
)
options = {
"OBSERVER": get_cfg_option_from_args("observer", "-Q", args[:-1])
}
ti3 = add_keywords_to_cgats(ti3, options)
if 1 in ti3:
# Add video level encoding flag if needed
black_white = False
if "-E" in args2:
black_white = (16, 235)
elif config.get_display_name() == "madVR":
# Get output encoding from madVR
# Note: 'tags' will only be True if creating profile
# directly after measurements, and only in that case will
# we want to query madVR
black_white = self.madtpg_bw_lvl
if black_white == (16, 235):
ti3[1].add_keyword("TV_OUTPUT_ENCODING", "YES")
elif black_white == (0, 255):
ti3[1].add_keyword("TV_OUTPUT_ENCODING", "NO")
elif black_white and black_white != (0, 255):
ti3[1].add_keyword(
"OUTPUT_ENCODING", " ".join(str(v) for v in black_white)
)
ti3.write()
# Restore original TI1
ti1_orig = args[-1] + ".original.ti1"
ti1 = args[-1] + ".ti1"
if os.path.isfile(ti1_orig):
try:
if os.path.isfile(ti1):
# Should always exist
os.remove(ti1)
os.rename(ti1_orig, ti1)
except Exception as exception:
self.log(
"Warning - could not restore "
f"backup of original TI1 file {ti1_orig}:",
exception,
)
else:
self.log("Restored backup of original TI1 file")
if precond_ti1 and os.path.isfile(ti1):
# Need to add precond TI1 patches to TI1.
# Do this AFTER the measurements because we don't want to
# measure precond patches twice.
ti1 = insert_ti_patches_omitting_RGB_duplicates(
precond_ti1, ti1, self.log
)
ti1.write()
else:
result = cmd
result2 = self.wrapup(
not isinstance(result, UnloggedInfo) and result,
isinstance(result, Exception) or not result,
)
if isinstance(result2, Exception):
if isinstance(result, Exception):
result = Error(str(result) + "\n\n" + str(result2))
else:
result = result2
return result
def ensure_patch_sequence(self, ti1, write=True):
"""Ensure correct patch sequence of TI1 file
Return either the changed CGATS object or the original path/TI1
"""
patch_sequence = getcfg("testchart.patch_sequence")
if patch_sequence != "optimize_display_response_delay":
# Need to re-order patches
if not isinstance(ti1, CGATS):
try:
ti1 = CGATS(ti1)
except Exception as exception:
self.log(f"Warning - could not process TI1 file {ti1}:", exception)
return ti1
self.log(
"Changing patch sequence:", lang.getstr(f"testchart.{patch_sequence}")
)
if patch_sequence == "maximize_lightness_difference":
result = ti1.checkerboard(sort1=stable_sort_by_L)
elif patch_sequence == "maximize_rec709_luma_difference":
result = ti1.checkerboard(sort_by_rec709_luma)
elif patch_sequence == "maximize_RGB_difference":
result = ti1.checkerboard(sort_by_RGB_sum)
elif patch_sequence == "vary_RGB_difference":
result = ti1.checkerboard(
sort_by_RGB, None, split_grays=True, shift=True
)
if not result:
self.log("Warning - patch sequence was not changed")
elif write:
# Make a copy
try:
shutil.copyfile(
ti1.filename,
os.path.splitext(ti1.filename)[0] + ".original.ti1",
)
except Exception as exception:
self.log(
f"Warning - could not make backup of TI1 file {ti1.filename}:",
exception,
)
# Write new TI1 to original filename
try:
ti1.write()
except Exception as exception:
self.log(
f"Warning - could not write TI1 file {ti1.filename}:",
exception,
)
return ti1
def parse(self, txt):
if not txt:
return
self.logger.info(f"{txt}")
self.check_instrument_calibration(txt)
self.check_instrument_place_on_screen(txt)
self.check_instrument_sensor_position(txt)
self.check_retry_measurement(txt)
self.check_is_single_measurement(txt)
self.check_spotread_result(txt)
def audio_visual_feedback(self, txt):
if isinstance(txt, bytes):
txt = txt.decode()
if self.cmdname in (
get_argyll_utilname("dispcal"),
get_argyll_utilname("dispread"),
get_argyll_utilname("spotread"),
):
if (
self.cmdname == get_argyll_utilname("dispcal")
and ", repeat" in txt.lower()
):
self.repeat = True
elif ", ok" in txt.lower():
self.repeat = False
if (
re.search(r"Patch [2-9]\d* of ", txt, re.I)
or (
re.search(
r"Patch \d+ of |The instrument can be removed from the screen",
txt,
re.I,
)
and self.patch_count > 1
)
or (
"Result is XYZ:" in txt
and not isinstance(self.progress_wnd, UntetheredFrame)
)
):
if self.cmdname == get_argyll_utilname("dispcal") and self.repeat:
if getcfg("measurement.play_sound") and hasattr(
self.progress_wnd, "sound_on_off_btn"
):
self.measurement_sound.safe_play()
else:
if getcfg("measurement.play_sound") and (
hasattr(self.progress_wnd, "sound_on_off_btn")
or isinstance(self.progress_wnd, DisplayUniformityFrame)
or getattr(self.progress_wnd, "Name", None)
== "VisualWhitepointEditor"
):
self.commit_sound.safe_play()
if hasattr(self.progress_wnd, "animbmp"):
self.progress_wnd.animbmp.frame = 0
def setup_patterngenerator(self, logfile=None):
pgname = config.get_display_name(None, True)
if self.patterngenerator:
# Use existing pattern generator instance
self.patterngenerator.logfile = logfile
self.patterngenerator.use_video_levels = getcfg(
"patterngenerator.use_video_levels"
)
if hasattr(self.patterngenerator, "conn"):
# Try to use existing connection
try:
self.patterngenerator_send(
(0.5,) * 3, raise_exceptions=True, increase_sent_count=False
)
except (socket.error, http.client.HTTPException) as exception:
self.log(exception)
self.patterngenerator.disconnect_client()
elif pgname == "Prisma":
patterngenerator = PrismaPatternGeneratorClient
self.patterngenerator = patterngenerator(
host=getcfg("patterngenerator.prisma.host"),
port=getcfg("patterngenerator.prisma.port"),
use_video_levels=getcfg("patterngenerator.use_video_levels"),
logfile=logfile,
)
elif pgname == "Web @ localhost":
patterngenerator = WebWinHTTPPatternGeneratorServer
self.patterngenerator = patterngenerator(
port=getcfg("webserver.portnumber"), logfile=logfile
)
elif pgname.startswith("Chromecast "):
from DisplayCAL.chromecast_patterngenerator import (
ChromeCastPatternGenerator,
)
self.patterngenerator = ChromeCastPatternGenerator(
name=self.get_display_name(), logfile=logfile
)
elif pgname == "Resolve":
# Resolve
if getcfg("patterngenerator.resolve") == "LS":
patterngenerator = ResolveLSPatternGeneratorServer
else:
patterngenerator = ResolveCMPatternGeneratorServer
self.patterngenerator = patterngenerator(
port=getcfg("patterngenerator.resolve.port"),
use_video_levels=getcfg("patterngenerator.use_video_levels"),
logfile=logfile,
)
@property
def patterngenerator(self):
pgname = config.get_display_name()
return self._patterngenerators.get(pgname)
@patterngenerator.setter
def patterngenerator(self, patterngenerator):
pgname = config.get_display_name()
self._patterngenerators[pgname] = patterngenerator
@property
def patterngenerators(self):
return self._patterngenerators
def patterngenerator_send(
self, rgb, bgrgb=None, raise_exceptions=False, increase_sent_count=True
):
"""Send RGB color to pattern generator"""
if getattr(self, "abort_requested", False):
return
x, y, w, h, size = get_pattern_geometry()
if bgrgb is not None:
pass
elif getcfg("measure.darken_background"):
bgrgb = (0, 0, 0)
elif size == 1.0:
bgrgb = list(rgb)
else:
# Constant APL (matches madTPG 'gamma light')
desired_apl = getcfg("patterngenerator.apl")
bgrgb = [min(max(desired_apl - v * size, 0) / (1.0 - size), 1) for v in rgb]
bgrgb_apl = sum(bgrgb) / 3.0 * (1 - size)
rgb_apl = sum(rgb) / 3.0 * size
needed_bgrgb_apl = max(desired_apl - rgb_apl, 0)
if bgrgb_apl > needed_bgrgb_apl:
f = needed_bgrgb_apl / bgrgb_apl
bgrgb = [v * f for v in bgrgb]
self.log(
f"{appname}: Sending RGB {rgb[0]:.3f} {rgb[1]:.3f} {rgb[2]:.3f}, "
f"background RGB {bgrgb[0]:.3f} {bgrgb[1]:.3f} {bgrgb[2]:.3f}, "
f"x {x:.4f}, y {y:.4f}, w {w:.4f}, h {h:.4f}"
)
if self._use_patternwindow:
# Preliminary Wayland support. This still needs a lot
# of work as Argyll doesn't support Wayland natively yet,
# so we use virtual display to drive our own patch window.
self._patterngenerator_wait = True
wx.CallAfter(self.owner.measureframe.show_rgb, rgb)
# Wait for call to return
while self._patterngenerator_wait and not self.subprocess_abort:
sleep(0.001)
else:
try:
self.patterngenerator.send(rgb, bgrgb, x=x, y=y, w=w, h=h)
except Exception as exception:
if raise_exceptions:
raise
else:
self.exec_cmd_returnvalue = exception
self.abort_subprocess()
return
if increase_sent_count:
self.patterngenerator_sent_count += 1
self.log(
f"{appname}: Patterngenerator sent count: "
f"{self.patterngenerator_sent_count:d}"
)
@property
def pauseable(self):
return self._pauseable
@pauseable.setter
def pauseable(self, pauseable):
self._pauseable = pauseable
self.pauseable_now = False
def pause_continue(self):
if not self.pauseable or not self.pauseable_now:
return
if getattr(self.progress_wnd, "paused", False) and not getattr(
self, "paused", False
):
self.paused = True
self.log(f"{appname}: Pausing...")
self.safe_send("\x1b")
if self.use_madnet_tpg:
self.madtpg.set_osd_text("\u23f8") # "Pause" symbol
elif not getattr(self.progress_wnd, "paused", False) and getattr(
self, "paused", False
):
self.paused = False
self.log(f"{appname}: Continuing...")
self.safe_send(" ")
if self.use_madnet_tpg:
self.madtpg.set_osd_text("\u25b6") # "Play" symbol
def prepare_colprof(
self, profile_name=None, display_name=None, display_manufacturer=None, tags=None
):
"""Prepare a colprof commandline.
All options are read from the user configuration.
Profile name and display name can be overridden by passing thecorresponding arguments.
"""
if profile_name is None:
profile_name = getcfg("profile.name.expanded")
in_out_file = self.setup_inout(profile_name)
if not in_out_file or isinstance(in_out_file, Exception):
return in_out_file, None
if not os.path.exists(f"{in_out_file}.ti3"):
return (
Error(
lang.getstr("error.measurement.file_missing", f"{in_out_file}.ti3")
),
None,
)
if not os.path.isfile(f"{in_out_file}.ti3"):
return Error(lang.getstr("file_notfile", f"{in_out_file}.ti3")), None
cmd = get_argyll_util("colprof")
args = ["-v", f"-q{getcfg('profile.quality')}", f"-a{getcfg('profile.type')}"]
gamap_args = args
if getcfg("profile.type") in ["l", "x", "X"]:
if getcfg("gamap_saturation"):
gamap = "S"
elif getcfg("gamap_perceptual"):
gamap = "s"
else:
gamap = None
gamap_profile = None
if gamap and getcfg("gamap_profile"):
# CIECAM02 gamut mapping - perceptual and saturation tables
# Only for L*a*b* LUT or if source profile is not a simple matrix
# profile, otherwise create hires CIECAM02 tables with collink
try:
gamap_profile = ICCProfile(getcfg("gamap_profile"))
except ICCProfileInvalidError as exception:
self.log(exception)
return Error(
lang.getstr("profile.invalid") + "\n" + getcfg("gamap_profile")
)
except IOError as exception:
return exception
if (
getcfg("profile.type") != "l"
and getcfg("profile.b2a.hires")
and "A2B0" not in gamap_profile.tags
and "rXYZ" in gamap_profile.tags
and "gXYZ" in gamap_profile.tags
and "bXYZ" in gamap_profile.tags
and "rTRC" in gamap_profile.tags
and "gTRC" in gamap_profile.tags
and "bTRC" in gamap_profile.tags
):
self.log("Delegating CIECAM02 gamut mapping to collink")
# Make a copy, so we can store options without adding them
# to actual colprof arguments
gamap_args = []
gamap_profile = None
gamap_args.append(f"-{gamap}")
gamap_args.append(getcfg("gamap_profile"))
gamap_args.append(f"-t{getcfg('gamap_perceptual_intent')}")
if gamap == "S":
gamap_args.append(f"-T{getcfg('gamap_saturation_intent')}")
if getcfg("gamap_src_viewcond"):
gamap_args.append(f"-c{getcfg('gamap_src_viewcond')}")
if getcfg("gamap_out_viewcond"):
gamap_args.append(f"-d{getcfg('gamap_out_viewcond')}")
b2a_q = getcfg("profile.quality.b2a")
if (
getcfg("profile.b2a.hires")
and getcfg("profile.type") in ("l", "x", "X")
and not (gamap and gamap_profile)
):
rgb = False
is_lab_clut_ptype = getcfg("profile.type") == "l"
if is_lab_clut_ptype:
with open(f"{in_out_file}.ti3", "rb") as ti3_file:
for line in ti3_file:
if line.startswith(b"COLOR_REP"):
if b"RGB_XYZ" in line:
rgb = True
break
if rgb or not is_lab_clut_ptype:
# Disable B2A creation in colprof, B2A is handled
# by A2B inversion code (only for cLUT profiles)
b2a_q = "n"
if b2a_q and b2a_q != getcfg("profile.quality"):
args.append(f"-b{b2a_q}")
args.append("-C")
args.append(getcfg("copyright").encode("ASCII", "asciize").decode("utf-8"))
if getcfg("extra_args.colprof").strip():
args += parse_argument_string(getcfg("extra_args.colprof"))
options_dispcal = []
if "-d3" in self.options_targen:
# only add display desc and dispcal options if creating RGB profile
options_dispcal = self.options_dispcal
if len(self.displays):
args.extend(
self.update_display_name_manufacturer(
f"{in_out_file}.ti3",
display_name,
display_manufacturer,
write=False,
)
)
self.options_colprof = list(args)
if gamap_args is not args:
self.options_colprof.extend(gamap_args)
args.append("-D")
args.append(profile_name)
args.append(in_out_file)
# Add dispcal and colprof arguments to ti3
ti3 = add_options_to_ti3(
f"{in_out_file}.ti3", options_dispcal, self.options_colprof
)
if ti3:
color_rep = (ti3.queryv1("COLOR_REP").decode("utf-8") or "").split("_")
# Prepare ChromaticityType tag
self.log("Preparing ChromaticityType tag from TI3 colorants")
colorants = ti3.get_colorants()
if colorants and None not in colorants:
chrm = ChromaticityType()
chrm.type = 0
for colorant in colorants:
if color_rep[1] == "LAB":
XYZ = colormath.Lab2XYZ(
colorant["LAB_L"], colorant["LAB_A"], colorant["LAB_B"]
)
else:
XYZ = (colorant["XYZ_X"], colorant["XYZ_Y"], colorant["XYZ_Z"])
chrm.channels.append(colormath.XYZ2xyY(*XYZ)[:-1])
with open(in_out_file + ".chrm", "wb") as blob:
blob.write(chrm.tagData)
self.log("Storing settings in TI3")
# Black point compensation
ti3[0].add_keyword(
"USE_BLACK_POINT_COMPENSATION",
"YES" if getcfg("profile.black_point_compensation") else "NO",
)
# Black point correction
# NOTE that profile black point correction is not the same as
# calibration black point correction!
# See Worker.create_profile
ti3[0].add_keyword(
"BLACK_POINT_CORRECTION", getcfg("profile.black_point_correction")
)
# Hires B2A with optional smoothing
ti3[0].add_keyword(
"HIRES_B2A", "YES" if getcfg("profile.b2a.hires") else "NO"
)
ti3[0].add_keyword("HIRES_B2A_SIZE", getcfg("profile.b2a.hires.size"))
ti3[0].add_keyword(
"SMOOTH_B2A", "YES" if getcfg("profile.b2a.hires.smooth") else "NO"
)
# Display update delay
if getcfg("measure.override_min_display_update_delay_ms"):
ti3[0].add_keyword(
"MIN_DISPLAY_UPDATE_DELAY_MS",
getcfg("measure.min_display_update_delay_ms"),
)
# Display settle time multiplier
if getcfg("measure.override_display_settle_time_mult"):
ti3[0].add_keyword(
"DISPLAY_SETTLE_TIME_MULT",
getcfg("measure.display_settle_time_mult"),
)
# FFP
if getcfg("patterngenerator.ffp_insertion"):
for keyword in ("INTERVAL", "DURATION", "LEVEL"):
ti3[0].add_keyword(
f"FFP_INSERTION_{keyword}",
getcfg(f"patterngenerator.ffp_insertion.{keyword.lower()}"),
)
# Remove AUTO_OPTIMIZE
if ti3[0].queryv1("AUTO_OPTIMIZE"):
ti3[0].remove_keyword("AUTO_OPTIMIZE")
# Patch sequence
ti3[0].add_keyword(
"PATCH_SEQUENCE", getcfg("testchart.patch_sequence").upper()
)
# Add 3D LUT options if set, else remove them
for keyword, cfgname in {
"3DLUT_SOURCE_PROFILE": "3dlut.input.profile",
"3DLUT_TRC": "3dlut.trc",
"3DLUT_HDR_PEAK_LUMINANCE": "3dlut.hdr_peak_luminance",
"3DLUT_HDR_SAT": "3dlut.hdr_sat",
"3DLUT_HDR_HUE": "3dlut.hdr_hue",
"3DLUT_HDR_MAXMLL": "3dlut.hdr_maxmll",
"3DLUT_HDR_MAXMLL_ALT_CLIP": "3dlut.hdr_maxmll_alt_clip",
"3DLUT_HDR_MINMLL": "3dlut.hdr_minmll",
"3DLUT_HDR_AMBIENT_LUMINANCE": "3dlut.hdr_ambient_luminance",
"3DLUT_HDR_DISPLAY": "3dlut.hdr_display",
"3DLUT_GAMMA": "3dlut.trc_gamma",
"3DLUT_DEGREE_OF_BLACK_OUTPUT_OFFSET": "3dlut.trc_output_offset",
"3DLUT_INPUT_ENCODING": "3dlut.encoding.input",
"3DLUT_OUTPUT_ENCODING": "3dlut.encoding.output",
"3DLUT_GAMUT_MAPPING_MODE": "3dlut.gamap.use_b2a",
"3DLUT_RENDERING_INTENT": "3dlut.rendering_intent",
"3DLUT_FORMAT": "3dlut.format",
"3DLUT_SIZE": "3dlut.size",
"3DLUT_INPUT_BITDEPTH": "3dlut.bitdepth.input",
"3DLUT_OUTPUT_BITDEPTH": "3dlut.bitdepth.output",
"3DLUT_APPLY_CAL": "3dlut.output.profile.apply_cal",
"SIMULATION_PROFILE": "measurement_report.simulation_profile",
}.items():
if getcfg("3dlut.create"):
value = getcfg(cfgname)
if cfgname == "3dlut.gamap.use_b2a":
if value:
value = "g"
else:
value = "G"
elif cfgname == "3dlut.trc_gamma":
if getcfg("3dlut.trc_gamma_type") == "B":
value = -value
elif (
cfgname == "3dlut.input.profile"
and os.path.basename(os.path.dirname(value)) == "ref"
and get_data_path("ref/" + os.path.basename(value)) == value
):
# Store relative path instead of absolute path if
# ref file
value = "ref/" + os.path.basename(value)
elif cfgname == "measurement_report.simulation_profile":
if (
getcfg("3dlut.trc").startswith("smpte2084")
or getcfg("3dlut.trc") == "hlg"
):
# Use 3D LUT profile
value = getcfg("3dlut.input.profile")
# Add 3D LUT HDR parameters and store only filename
# (file will be copied to profile dir)
fn, ext = os.path.splitext(os.path.basename(value))
lut3d_fn = self.lut3d_get_filename(fn, False, False)
value = lut3d_fn + ext
else:
value = None
else:
value = None
if value is not None:
ti3[0].add_keyword(keyword, safe_str(value, "utf-8"))
elif keyword in ti3[0]:
ti3[0].remove_keyword(keyword)
# 3D LUT content color space (currently only used for HDR)
for color in ("white", "red", "green", "blue"):
for coord in "xy":
keyword = "3DLUT_CONTENT_COLORSPACE_{}_{}".format(
color.upper(),
coord.upper(),
)
if getcfg("3dlut.create"):
value = getcfg(
"3dlut.content.colorspace.{}.{}".format(color, coord)
)
ti3[0].add_keyword(keyword, safe_str(value, "utf-8"))
elif keyword in ti3[0]:
ti3[0].remove_keyword(keyword)
ti3[0].fix_zero_measurements(logfile=self.get_logfiles(False))
ti3.write()
return cmd, args
def prepare_dispcal(self, calibrate=True, verify=False, dry_run=False):
"""Prepare a dispcal commandline.
All options are read from the user configuration.
You can choose if you want to calibrate and/or verify by passing
the corresponding arguments.
"""
cmd = get_argyll_util("dispcal")
args = ["-v2"]
if getcfg("argyll.debug"):
args.append("-D8")
result = self.add_measurement_features(args, allow_nondefault_observer=True)
if isinstance(result, Exception):
return result, None
if calibrate:
if getcfg("trc"):
args.append("-q" + getcfg("calibration.quality"))
inoutfile = self.setup_inout()
if not inoutfile or isinstance(inoutfile, Exception):
return inoutfile, None
if getcfg("profile.update") or self.dispcal_create_fast_matrix_shaper:
args.append("-o")
if getcfg("calibration.update") and not dry_run:
cal = getcfg("calibration.file", False)
calcopy = os.path.join(f"{inoutfile}.cal")
filename, ext = os.path.splitext(cal)
ext = ".cal"
cal = f"{filename}{ext}"
if ext.lower() == ".cal":
result = check_cal_isfile(cal)
if isinstance(result, Exception):
return result, None
if not result:
return None, None
if not os.path.exists(calcopy):
try:
# Copy cal to profile dir
shutil.copyfile(cal, calcopy)
except Exception as exception:
return (
Error(
lang.getstr("error.copy_failed", (cal, calcopy))
+ "\n\n"
+ str(exception)
),
None,
)
result = check_cal_isfile(calcopy)
if isinstance(result, Exception):
return result, None
if not result:
return None, None
cal = calcopy
else:
rslt = extract_fix_copy_cal(cal, calcopy)
if isinstance(rslt, ICCProfileInvalidError):
return Error(lang.getstr("profile.invalid") + "\n" + cal), None
elif isinstance(rslt, Exception):
traceback.print_exc()
return (
Error(
lang.getstr("cal_extraction_failed")
+ "\n"
+ cal
+ "\n\n"
+ rslt
),
None,
)
if not isinstance(rslt, list):
return None, None
if getcfg("profile.update"):
profile_path = (
os.path.splitext(getcfg("calibration.file", False))[0]
+ profile_ext
)
result = check_profile_isfile(profile_path)
if isinstance(result, Exception):
return result, None
if not result:
return None, None
profilecopy = os.path.join(inoutfile + profile_ext)
if not os.path.exists(profilecopy):
try:
# Copy profile to profile dir
shutil.copyfile(profile_path, profilecopy)
except Exception as exception:
return (
Error(
lang.getstr(
"error.copy_failed", (profile_path, profilecopy)
)
+ "\n\n"
+ str(exception)
),
None,
)
result = check_profile_isfile(profilecopy)
if isinstance(result, Exception):
return result, None
if not result:
return None, None
args.append("-u")
if calibrate or verify:
if calibrate and not getcfg("calibration.interactive_display_adjustment"):
# Skip interactive display adjustment
args.append("-m")
whitepoint_colortemp = getcfg("whitepoint.colortemp", False)
whitepoint_x = getcfg("whitepoint.x", False)
whitepoint_y = getcfg("whitepoint.y", False)
if whitepoint_colortemp or None in (whitepoint_x, whitepoint_y):
whitepoint = getcfg("whitepoint.colortemp.locus")
if whitepoint_colortemp:
whitepoint += str(whitepoint_colortemp)
args.append(f"-{whitepoint}")
else:
args.append(f"-w{whitepoint_x},{whitepoint_y}")
luminance = getcfg("calibration.luminance", False)
self.log(f"{appname}: luminance: {luminance}")
if luminance is not None:
args.append(f"-b{luminance}")
if getcfg("trc"):
args.append("-{}{}".format(getcfg("trc.type"), getcfg("trc")))
args.append("-f{}".format(getcfg("calibration.black_output_offset")))
if bool(int(getcfg("calibration.ambient_viewcond_adjust"))):
# Argyll dispcal uses 20% of ambient (in lux,
# fixed steradiant of 3.1415) as adapting
# luminance, but we assume it already *is*
# the adapting luminance. To correct for this,
# scale so that dispcal gets the correct value.
ambient = getcfg("calibration.ambient_viewcond_adjust.lux")
args.append("-a{}".format(ambient * 5))
if not getcfg("calibration.black_point_correction.auto"):
args.append(
"-k{}".format(getcfg("calibration.black_point_correction"))
)
if (
defaults["calibration.black_point_rate.enabled"]
and float(getcfg("calibration.black_point_correction")) < 1
):
black_point_rate = getcfg("calibration.black_point_rate")
if black_point_rate:
args.append(f"-A{black_point_rate}")
black_luminance = getcfg("calibration.black_luminance", False)
if black_luminance:
args.append(f"-B{black_luminance:f}")
elif (
not (
getcfg("calibration.black_point_correction.auto")
or getcfg("calibration.black_point_correction")
)
and defaults["calibration.black_point_hack"]
):
# Forced black point hack
# (Argyll CMS 1.7b 2014-12-22)
# Always use this if no black luminance or black point hue
# correction specified. The rationale is that a reasonably good
# quality digitally driven display should have no "dead zone"
# above zero device input if set up correctly. Using this option
# with a display that is not well behaved may result in a loss
# of shadow detail.
self.log("skipping -b parameter")
# args.append("-b")
pass
if verify:
if calibrate and isinstance(verify, int):
args.append(f"-e{verify}") # Verify final computed curves
elif self.argyll_version >= [1, 6]:
args.append("-z") # Verify current curves
else:
args.append("-E") # Verify current curves
if getcfg("extra_args.dispcal").strip():
args += parse_argument_string(getcfg("extra_args.dispcal"))
if config.get_display_name() == "Resolve" or (
config.get_display_name() == "Prisma"
and not defaults["patterngenerator.prisma.argyll"]
):
# Substitute actual measurement frame dimensions
self.options_dispcal = [
re.sub(
r"^-[Pp]1,1,0.01$", "-P" + getcfg("dimensions.measureframe"), arg
)
for arg in args
]
# Re-add -F (darken background) so it can be set when loading settings
if getcfg("measure.darken_background"):
self.options_dispcal.append("-F")
else:
self.options_dispcal = list(args)
if calibrate:
args.append(inoutfile)
return cmd, args
def prepare_dispread(self, apply_calibration=True):
"""Prepare a dispread commandline.
All options are read from the user configuration.
You can choose if you want to apply the current calibration,
either the previously by dispcal created one by passing in True, by
passing in a valid path to a .cal file, or by passing in None
(current video card gamma table).
"""
self.lastcmdname = get_argyll_utilname("dispread")
inoutfile = self.setup_inout()
if not inoutfile or isinstance(inoutfile, Exception):
return inoutfile, None
if not os.path.exists(f"{inoutfile}.ti1"):
filename, ext = os.path.splitext(getcfg("testchart.file"))
result = check_file_isfile(filename + ext)
if isinstance(result, Exception):
return result, None
try:
if ext.lower() in (".icc", ".icm"):
try:
profile = ICCProfile(filename + ext)
except (IOError, ICCProfileInvalidError):
return (
Error(
lang.getstr(
"error.testchart.read", getcfg("testchart.file")
)
),
None,
)
ti3 = BytesIO(
profile.tags.get("CIED", b"") or profile.tags.get("targ", b"")
)
elif ext.lower() == ".ti1":
shutil.copyfile(filename + ext, f"{inoutfile}.ti1")
else: # ti3
try:
ti3 = open(filename + ext, "rb")
except Exception:
return (
Error(
lang.getstr(
"error.testchart.read", getcfg("testchart.file")
)
),
None,
)
if ext.lower() != ".ti1":
ti3_lines = [line.strip() for line in ti3]
ti3.close()
if b"CTI3" not in ti3_lines:
return (
Error(
lang.getstr(
"error.testchart.invalid", getcfg("testchart.file")
)
),
None,
)
with open(f"{inoutfile}.ti1", "wb") as ti1:
ti1.write(ti3_to_ti1(ti3_lines))
except Exception as exception:
localized_error_message = lang.getstr(
"error.testchart.creation_failed", f"{inoutfile}.ti1"
)
return (
Error(f"{localized_error_message}\n\n{exception}"),
None,
)
if apply_calibration is not False:
if apply_calibration is True:
# Always a .cal file in that case
cal = (
os.path.join(
getcfg("profile.save_path"),
getcfg("profile.name.expanded"),
getcfg("profile.name.expanded"),
)
+ ".cal"
)
elif apply_calibration is None:
# Use current videoLUT
cal = f"{inoutfile}.cal"
result = self.save_current_video_lut(self.get_display(), cal)
if isinstance(result, Exception) and not isinstance(
result, UnloggedInfo
):
return result, None
else:
cal = apply_calibration # can be .cal or .icc / .icm
calcopy = f"{inoutfile}.cal"
filename, ext = os.path.splitext(cal)
if getcfg("dry_run"):
options_dispcal = []
elif ext.lower() == ".cal":
result = check_cal_isfile(cal)
if isinstance(result, Exception):
return result, None
if not result:
return None, None
# Get dispcal options if present
try:
options_dispcal = get_options_from_cal(cal)[0]
except (
IOError,
CGATSInvalidError,
CGATSInvalidOperationError,
CGATSKeyError,
CGATSTypeError,
CGATSValueError,
) as exception:
return exception, None
if not os.path.exists(calcopy):
try:
# Copy cal to temp dir
shutil.copyfile(cal, calcopy)
except Exception as exception:
return (
Error(
lang.getstr("error.copy_failed", (cal, calcopy))
+ "\n\n"
+ str(exception)
),
None,
)
result = check_cal_isfile(calcopy)
if isinstance(result, Exception):
return result, None
if not result:
return None, None
else:
# .icc / .icm
result = check_profile_isfile(cal)
if isinstance(result, Exception):
return result, None
if not result:
return None, None
try:
profile = ICCProfile(filename + ext)
except (IOError, ICCProfileInvalidError):
profile = None
if profile:
ti3 = StringIO(
profile.tags.get("CIED", "") or profile.tags.get("targ", "")
)
# Get dispcal options if present
options_dispcal = get_options_from_profile(profile)[0]
else:
ti3 = StringIO("")
ti3_lines = [line.strip() for line in ti3]
ti3.close()
if "CTI3" not in ti3_lines:
return Error(lang.getstr("error.cal_extraction", (cal))), None
try:
tmpcal = open(calcopy, "w")
tmpcal.write(extract_cal_from_ti3(ti3_lines))
tmpcal.close()
except Exception as exception:
return (
Error(
lang.getstr("error.cal_extraction", (cal))
+ "\n\n"
+ str(exception)
),
None,
)
cal = calcopy
if options_dispcal:
self.options_dispcal = [f"-{arg}" for arg in options_dispcal]
#
# Make sure any measurement options are present
if not self.options_dispcal:
self.prepare_dispcal(dry_run=True)
# Special case -X because it can have a separate filename argument
if "-X" in self.options_dispcal:
index = self.options_dispcal.index("-X")
if (
len(self.options_dispcal) > index + 1
and self.options_dispcal[index + 1][0] != "-"
):
self.options_dispcal = (
self.options_dispcal[:index] + self.options_dispcal[index + 2 :]
)
# Strip options we may override (basically all the stuff which can be
# added by add_measurement_features. -X is repeated because it can
# have a number instead of explicit filename argument, e.g. -X1)
dispcal_override_args = (
"-F",
"-H",
"-I",
"-P",
"-V",
"-X",
"-d",
"-c",
"-p",
"-y",
)
self.options_dispcal = [
arg for arg in self.options_dispcal if arg[:2] not in dispcal_override_args
]
# Only add the dispcal extra args which may override measurement features
dispcal_extra_args = parse_argument_string(getcfg("extra_args.dispcal"))
for i, arg in enumerate(dispcal_extra_args):
if not arg.startswith("-") and i > 0:
# Assume option to previous arg
arg = dispcal_extra_args[i - 1]
if arg[:2] in dispcal_override_args:
self.options_dispcal.append(dispcal_extra_args[i])
result = self.add_measurement_features(
self.options_dispcal, ignore_display_name=True
)
if isinstance(result, Exception):
return result, None
cmd = get_argyll_util("dispread")
args = []
args.append("-v") # verbose
if getcfg("argyll.debug"):
args.append("-D8")
result = self.add_measurement_features(
args, allow_nondefault_observer=is_ccxx_testchart(), quantize=True
)
if isinstance(result, Exception):
return result, None
if apply_calibration is not False:
if (
self.argyll_version >= [1, 3, 3]
and (
not self.has_lut_access() or not getcfg("calibration.use_video_lut")
)
# Only use -K if we don't use -d n,m (Linux X11)
and len(get_arg("-d", args)[1].split(",")) == 1
):
# Apply calibration to test values directly instead of via
# videoLUT
args.append("-K")
else:
# Apply calibration via videoLUT (in special case of madVR,
# always applied to test values directly instead)
args.append("-k")
args.append(cal)
if self.get_instrument_features().get("spectral"):
args.append("-s")
if getcfg("extra_args.dispread").strip():
args += parse_argument_string(getcfg("extra_args.dispread"))
self.options_dispread = list(args)
cgats = self.ensure_patch_sequence(inoutfile + ".ti1")
if getattr(self, "terminal", None) and isinstance(
self.terminal, UntetheredFrame
):
result = self.set_terminal_cgats(cgats)
if isinstance(result, Exception):
return result, None
return cmd, self.options_dispread + [inoutfile]
def prepare_dispwin(self, cal=None, profile_path=None, install=True):
"""Prepare a dispwin commandline.
All options are read from the user configuration.
If you pass in cal as True, it will try to load the current
display profile's calibration. If cal is a path, it'll use
that instead. If cal is False, it'll clear the current calibration.
If cal is None, it'll try to load the calibration from a profile
specified by profile_path.
"""
cmd = get_argyll_util("dispwin")
args = []
args.append("-v")
if getcfg("argyll.debug"):
if self.argyll_version >= [1, 3, 1]:
args.append("-D8")
else:
args.append("-E8")
args.append("-d" + self.get_display())
if sys.platform != "darwin" or cal is False:
# Mac OS X 10.7 Lion needs root privileges when clearing
# calibration
args.append("-c")
if cal is True:
args.append(
self.get_dispwin_display_profile_argument(
max(0, min(len(self.displays), getcfg("display.number")) - 1)
)
)
elif cal:
result = check_cal_isfile(cal)
if isinstance(result, Exception):
return result, None
if not result:
return None, None
args.append(cal)
else:
if cal is None:
if not profile_path:
profile_save_path = os.path.join(
getcfg("profile.save_path"), getcfg("profile.name.expanded")
)
profile_path = os.path.join(
profile_save_path, getcfg("profile.name.expanded") + profile_ext
)
result = check_profile_isfile(profile_path)
if isinstance(result, Exception):
return result, None
if not result:
return None, None
try:
profile = ICCProfile(profile_path)
except (IOError, ICCProfileInvalidError):
return (
Error(lang.getstr("profile.invalid") + "\n" + profile_path),
None,
)
if profile.profileClass != b"mntr" or profile.colorSpace != b"RGB":
return (
Error(
lang.getstr(
"profile.unsupported",
(profile.profileClass, profile.colorSpace),
)
+ "\n"
+ profile_path
),
None,
)
if install:
if getcfg("profile.install_scope") != "u" and (
(
(
sys.platform == "darwin"
or (
sys.platform != "win32"
and self.argyll_version >= [1, 1, 0]
)
)
and (os.geteuid() == 0 or which("sudo"))
)
or (
sys.platform == "win32"
and sys.getwindowsversion() >= (6,)
and self.argyll_version > [1, 1, 1]
)
):
# -S option is broken on Linux with current Argyll
# releases
args.append("-S" + getcfg("profile.install_scope"))
else:
# Make sure user profile dir exists
# (e.g. on Mac OS X 10.9 Mavericks, it does not by
# default)
for profile_dir in reversed(iccprofiles_home):
if os.path.isdir(profile_dir):
break
if not os.path.isdir(profile_dir):
try:
os.makedirs(profile_dir)
except OSError as exception:
return exception, None
args.append("-I")
# Always copy to temp dir so if a user accidentally tries
# to install a profile from the location where it's already
# installed (e.g. system32/spool/drivers/color) it doesn't
# get nuked by dispwin
tmp_dir = self.create_tempdir()
if not tmp_dir or isinstance(tmp_dir, Exception):
return tmp_dir, None
# Check directory and in/output file(s)
result = check_create_dir(tmp_dir)
if isinstance(result, Exception):
return result, None
profile_name = os.path.basename(profile_path)
if (
sys.platform in ("win32", "darwin")
or fs_enc.upper() not in ("UTF8", "UTF-8")
) and re.search(r"[^\x20-\x7e]", profile_name):
# Copy to temp dir and give ASCII-only name to
# avoid profile install issues
profile_tmp_path = os.path.join(
tmp_dir,
safe_asciize(profile_name).decode("utf-8", "ignore"),
)
else:
profile_tmp_path = os.path.join(tmp_dir, profile_name)
shutil.copyfile(profile_path, profile_tmp_path)
profile_path = profile_tmp_path
args.append(profile_path)
return cmd, args
def prepare_targen(self):
"""Prepare a targen commandline.
All options are read from the user configuration.
"""
path = self.create_tempdir()
if not path or isinstance(path, Exception):
return path, None
# Check directory and in/output file(s)
result = check_create_dir(path)
if isinstance(result, Exception):
return result, None
inoutfile = os.path.join(path, "temp")
cmd = get_argyll_util("targen")
args = []
args.append("-v")
args.append("-d3")
args.append("-e{}".format(getcfg("tc_white_patches")))
if self.argyll_version >= [1, 6]:
args.append("-B{}".format(getcfg("tc_black_patches")))
args.append("-s{}".format(getcfg("tc_single_channel_patches")))
args.append("-g{}".format(getcfg("tc_gray_patches")))
args.append("-m{}".format(getcfg("tc_multi_steps")))
if self.argyll_version >= [1, 6, 0]:
args.append("-b{}".format(getcfg("tc_multi_bcc_steps")))
tc_algo = getcfg("tc_algo")
if getcfg("tc_fullspread_patches") > 0:
args.append("-f{}".format(config.get_total_patches()))
if tc_algo:
args.append("-" + tc_algo)
if tc_algo in ("i", "I"):
args.append("-a{}".format(getcfg("tc_angle")))
if tc_algo == "":
args.append("-A{}".format(getcfg("tc_adaption")))
if self.argyll_version >= [1, 3, 3]:
args.append("-N{}".format(getcfg("tc_neutral_axis_emphasis")))
if self.argyll_version == [1, 1, "RC1"] or self.argyll_version >= [1, 1]:
args.append("-G")
else:
args.append("-f0")
if getcfg("tc_precond") and getcfg("tc_precond_profile"):
args.append("-c")
args.append(getcfg("tc_precond_profile"))
if getcfg("tc_filter"):
args.append(
"-F{},{},{},{}".format(
getcfg("tc_filter_L"),
getcfg("tc_filter_a"),
getcfg("tc_filter_b"),
getcfg("tc_filter_rad"),
)
)
if self.argyll_version >= [1, 6, 2] and (
"-c" in args or self.argyll_version >= [1, 6, 3]
):
args.append("-V{}".format(1 + getcfg("tc_dark_emphasis") * 3))
if self.argyll_version == [1, 1, "RC2"] or self.argyll_version >= [1, 1]:
args.append("-p{}".format(getcfg("tc_gamma")))
if getcfg("extra_args.targen").strip():
# Disallow -d and -D as the testchart editor only supports
# video RGB (-d3)
args += [
arg
for arg in parse_argument_string(getcfg("extra_args.targen"))
if not arg.lower().startswith("-d")
]
self.options_targen = list(args)
args.append(inoutfile)
return cmd, args
def progress_handler(self, event):
"""Handle progress dialog updates and react to Argyll CMS command output"""
if getattr(self, "subprocess_abort", False) or getattr(
self, "thread_abort", False
):
self.progress_wnd.Pulse(lang.getstr("aborting"))
return
percentage = None
msg = self.recent.read(FilteredStream.triggers)
lastmsg = self.lastmsg.read(FilteredStream.triggers).strip()
warning = r"\D+: Warning -.*"
msg = re.sub(warning, "", msg)
lastmsg = re.sub(warning, "", lastmsg)
# Filter for '=' so that 1% reading during calibration check
# measurements doesn't trigger swapping from the interactive adjustment
# to the progress window
if re.match(r"\s*\d+%\s*(?:[^=]+)?$", lastmsg):
# colprof, download progress
try:
percentage = int(self.lastmsg.read().split("%")[0])
except ValueError:
pass
elif re.match(r"Patch \d+ of \d+", lastmsg, re.I):
# dispcal/dispread
components = lastmsg.split()
try:
start = float(components[1])
end = float(components[3])
except ValueError:
pass
else:
percentage = max(start - 1, 0) / end * 100
elif re.match(r"Added \d+/\d+", lastmsg, re.I):
# targen
components = lastmsg.lower().replace("added ", "").split("/")
try:
start = float(components[0])
end = float(components[1])
except ValueError:
pass
else:
percentage = start / end * 100
else:
iteration = re.search(r"It (\d+):", msg)
if iteration:
# targen
try:
start = float(iteration.groups()[0])
except ValueError:
pass
else:
end = 20
percentage = min(start, 20.0) / end * 100
lastmsg = ""
if (
percentage is not None
and time() > self.starttime + 3
and self.progress_wnd is getattr(self, "terminal", None)
and not self._detecting_video_levels
):
# We no longer need keyboard interaction, switch over to
# progress dialog
wx.CallAfter(self.swap_progress_wnds)
self.set_progress_type()
if (
getattr(self.progress_wnd, "original_msg", None)
and msg != self.progress_wnd.original_msg
):
# UGLY HACK: This 'print' call fixes a GTK assertion and
# segfault under Arch Linux when setting the window title.
# This has a chance of throwing a IOError: [Errno 9] Bad file
# descriptor under Windows, so check for wxGTK
if "__WXGTK__" in wx.PlatformInfo:
print("")
self.progress_wnd.SetTitle(self.progress_wnd.original_msg)
self.progress_wnd.original_msg = None
if percentage is not None:
if (
"Setting up the instrument" in msg
or "Commencing device calibration" in msg
or "Commencing display calibration" in msg
or "Calibration complete" in msg
):
self.recent.clear()
msg = ""
keepGoing, skip = self.progress_wnd.UpdateProgress(
max(min(percentage, 100), 0), msg + "\n" + lastmsg
)
elif re.match(r"\d+(?:\.\d+)? (?:[KM]iB)", lastmsg, re.I):
keepGoing, skip = self.progress_wnd.Pulse("\n".join([msg, lastmsg]))
else:
if getattr(self.progress_wnd, "lastmsg", "") == msg or not msg:
keepGoing, skip = self.progress_wnd.Pulse()
else:
if "Setting up the instrument" in lastmsg:
msg = lang.getstr("instrument.initializing")
elif "Created web server at" in msg:
webserver = re.search(r"(http\:\/\/[^']+)", msg)
if webserver:
msg = (
lang.getstr("webserver.waiting")
+ " "
+ webserver.groups()[0]
)
keepGoing, skip = self.progress_wnd.Pulse(msg)
self.pause_continue()
if hasattr(self.progress_wnd, "pause_continue"):
if "read stopped at user request!" in lastmsg:
self.progress_wnd.pause_continue.Enable()
if self.progress_wnd.pause_continue.IsShown() != self.pauseable:
self.progress_wnd.pause_continue.Show(self.pauseable)
self.progress_wnd.Layout()
if not keepGoing and not getattr(self, "abort_requested", False):
# Only confirm abort if we are not currently doing interactive
# display adjustment
self.abort_subprocess(
not isinstance(self._progress_wnd, DisplayAdjustmentFrame)
)
if self.finished is True:
return
if (
self.progress_wnd.IsShownOnScreen()
and not self.progress_wnd.IsActive()
and (
not getattr(self.progress_wnd, "dlg", None)
or not self.progress_wnd.dlg.IsShownOnScreen()
)
and wx.GetApp().GetTopWindow()
and wx.GetApp().GetTopWindow().IsShownOnScreen()
and (
wx.GetApp().IsActive()
or (sys.platform == "darwin" and not self.activated)
)
):
for window in list(wx.GetTopLevelWindows()):
if (
window
and window is not self.progress_wnd
and isinstance(window, wx.Dialog)
and window.IsShownOnScreen()
):
return
self.activated = True
self.progress_wnd.Raise()
def progress_dlg_start(
self, progress_title="", progress_msg="", parent=None, resume=False, fancy=True
):
"""Start a progress dialog, replacing existing one if present"""
if self._progress_wnd and self.progress_wnd is getattr(self, "terminal", None):
self.terminal.stop_timer()
self.terminal.Hide()
if self.finished is True:
return
fancy = fancy and getcfg("use_fancy_progress")
if (
resume
and self._progress_wnd
and self._progress_wnd in list(self._progress_dlgs.values())
):
progress_wnd = self._progress_wnd
else:
key = (
self.show_remaining_time,
self.cancelable,
fancy,
parent,
parent and parent.IsShownOnScreen(),
)
progress_wnd = self._progress_dlgs.get(key)
if progress_wnd:
if self._progress_wnd is not progress_wnd:
self.progress_wnd = progress_wnd
# UGLY HACK: This 'print' call fixes a GTK assertion and
# segfault under Arch Linux when setting the window title
# This has a chance of throwing a IOError: [Errno 9] Bad file
# descriptor under Windows, so check for wxGTK
if "__WXGTK__" in wx.PlatformInfo:
print("")
self.progress_wnd.SetTitle(progress_title)
if not resume or not self.progress_wnd.IsShown():
self.progress_wnd.reset()
self.progress_wnd.Pulse(progress_msg)
if hasattr(self.progress_wnd, "pause_continue"):
self.progress_wnd.pause_continue.Show(self.pauseable)
self.progress_wnd.Layout()
self.progress_wnd.Resume()
if not self.progress_wnd.IsShownOnScreen():
self.progress_wnd.place()
self.progress_wnd.Show()
else:
style = wx.PD_SMOOTH | wx.PD_ELAPSED_TIME
if self.show_remaining_time:
style |= wx.PD_REMAINING_TIME
if self.cancelable:
style |= wx.PD_CAN_ABORT
# Set maximum to 101 to prevent the 'cancel' changing to 'close'
# when 100 is reached
self._progress_dlgs[key] = ProgressDialog(
progress_title,
progress_msg,
maximum=101,
parent=parent,
handler=self.progress_handler,
keyhandler=self.terminal_key_handler,
pauseable=self.pauseable,
style=style,
start_timer=False,
fancy=fancy,
)
self.progress_wnd = self._progress_dlgs[key]
self.set_progress_type()
if not self.progress_wnd.timer.IsRunning():
self.progress_wnd.start_timer()
self.progress_wnd.original_msg = progress_msg
def set_progress_type(self):
"""Set progress type for fancy progress dialog"""
if hasattr(self.progress_wnd, "progress_type"):
if self.pauseable or getattr(self, "interactive_frame", "") in (
"ambient",
"luminance",
):
# If pauseable, we assume it's a measurement
progress_type = 1 # Measuring
elif self.cmdname == get_argyll_utilname("targen"):
progress_type = 2 # Generating test patches
else:
progress_type = 0 # Processing
if self.progress_wnd.progress_type != progress_type:
if self.progress_wnd.timer.IsRunning():
# Fade-out current progress type, fade-in new one
self.progress_wnd.set_progress_type(progress_type)
else:
# Not yet running, just set initial progress type
self.progress_wnd.progress_type = progress_type
def quit_terminate_cmd(self):
"""Forcefully abort the current subprocess.
Try to gracefully exit first by sending common Argyll CMS abort
keystrokes (ESC), forcefully terminate the subprocess if not
reacting
"""
# If running wexpect.spawn in a thread under Windows, writing to
# sys.stdout from another thread can fail sporadically with IOError 9
# 'Bad file descriptor', so don't use sys.stdout
# Careful: Python 2.5 Producer objects don't have a name attribute
if (
hasattr(self, "thread")
and self.thread.is_alive()
and (
not hasattr(currentThread(), "name")
or currentThread().name != self.thread.name
)
):
logfn = log
else:
logfn = print
subprocess = getattr(self, "subprocess", None)
if self.isalive(subprocess):
try:
if self.measure_cmd and hasattr(subprocess, "send"):
self.log(
f"{appname}: Trying to end subprocess gracefully...", fn=logfn
)
try:
if subprocess.after == "Current":
# Stop measurement
self.safe_send(" ")
sleep(1)
ts = time()
while (
self.isalive(subprocess) and self.subprocess == subprocess
):
self.safe_send("\x1b")
if time() > ts + 20:
break
sleep(0.5)
except Exception as exception:
self.log(traceback.format_exc(), fn=logfn)
self.log(
f"{appname}: Exception in quit_terminate_command: {exception}",
fn=logfn,
)
elif hasattr(subprocess, "sendintr"):
self.log(f"{appname}: Sending CTRL+C to subprocess...", fn=logfn)
try:
subprocess.sendintr()
except Exception as exception:
self.log(traceback.format_exc(), fn=logfn)
self.log(
f"{appname}: Exception in quit_terminate_command: {exception}",
fn=logfn,
)
ts = time()
while self.isalive(subprocess):
if time() > ts + 20:
break
sleep(0.25)
if self.isalive(subprocess):
self.log(f"{appname}: Trying to terminate subprocess...", fn=logfn)
subprocess.terminate()
ts = time()
while self.isalive(subprocess):
if time() > ts + 3:
break
sleep(0.25)
if sys.platform != "win32" and self.isalive(subprocess):
self.log(
f"{appname}: Trying to terminate subprocess forcefully...",
fn=logfn,
)
if isinstance(subprocess, sp.Popen):
subprocess.kill()
else:
subprocess.terminate(force=True)
ts = time()
while self.isalive(subprocess):
if time() > ts + 3:
break
sleep(0.25)
if self.isalive(subprocess):
self.log("...warning: couldn't terminate subprocess.", fn=logfn)
else:
self.log("...subprocess terminated.", fn=logfn)
except Exception as exception:
self.log(traceback.format_exc(), fn=logfn)
self.log(
f"{appname}: Exception in quit_terminate_command: {exception}",
fn=logfn,
)
subprocess_isalive = self.isalive(subprocess)
if subprocess_isalive or (
hasattr(self, "thread") and not self.thread.is_alive()
):
# We don't normally need this as closing of the progress window is
# handled by _generic_consumer(), but there are two cases where it
# is desirable to have this 'safety net':
# 1. The user aborted a running task, but we couldn't terminate the
# associated subprocess. In that case, we have a lingering
# subprocess which is problematic but we can't do anything about
# it. Atleast we need to give control back to the user by closing
# the progress window so he can interact with the application
# and doesn't have to resort to forecfully terminate it.
# 2. We started a thread with continue_next=True, which then exited
# without returning an error, yet not the result we were looking
# for, so we never started the next thread with resume=True, but
# we forgot to call stop_progress() exlicitly. This should never
# happen if we design our result consumer correctly to handle
# this particular case, but we need to make sure the user can
# close the progress window in case we mess up.
if hasattr(self, "thread") and not self.thread.is_alive():
wx.CallAfter(self.stop_progress)
if subprocess_isalive:
wx.CallAfter(
show_result_dialog,
Warning(
f"Couldn't terminate {self.cmd}. Please try to end it manually "
f"before continuing to use {appname}. If you can not terminate "
f"{self.cmd}, restarting {appname} may also help. Apologies "
"for the inconvenience."
),
self.owner,
)
if self.patterngenerator:
self.patterngenerator.listening = False
return not subprocess_isalive
def report(self, report_calibrated=True):
"""Report on calibrated or uncalibrated display device response"""
result = self.detect_video_levels()
if isinstance(result, Exception) or not result:
return result
cmd, args = self.prepare_dispcal(calibrate=False)
if isinstance(cmd, Exception):
return cmd
if args:
if report_calibrated:
args.append("-r")
else:
args.append("-R")
return self.exec_cmd(cmd, args, capture_output=True, skip_scripts=True)
def reset_cal(self):
cmd, args = self.prepare_dispwin(False)
result = self.exec_cmd(
cmd, args, capture_output=True, skip_scripts=True, silent=False
)
return result
def safe_send(self, bytes_):
self.send_buffer = bytes_
return True
def _safe_send(self, bytes_, retry=3, obfuscate=False):
"""Safely send a keystroke to the current subprocess."""
for i in range(0, retry):
if obfuscate:
logbytes = "***"
else:
logbytes = bytes_
self.logger.info("Sending key(s) {} ({:d})".format(logbytes, i + 1))
try:
if sys.platform == "win32":
# directly send the bytes_ without encoding to anything
self.subprocess.send(bytes_)
else:
# for Linux and MacOS encode to bytes
self.subprocess.send(bytes_.encode())
except Exception as exception:
self.logger.exception(f"Exception: {exception}")
else:
return True
sleep(0.25)
return False
def save_current_video_lut(
self, display_no, outfilename, interpolate_to_256=True, silent=False
):
"""Save current videoLUT, optionally interpolating to n entries"""
result = None
if self.argyll_version[0:3] > [1, 1, 0] or (
self.argyll_version[0:3] == [1, 1, 0]
and "Beta" not in self.argyll_version_string
):
cmd, args = (
get_argyll_util("dispwin"),
[f"-d{display_no}", "-s", outfilename],
)
result = self.exec_cmd(
cmd, args, capture_output=True, skip_scripts=True, silent=silent
)
if isinstance(result, Exception) and not isinstance(result, UnloggedInfo):
return result
if not result:
return Error(lang.getstr("calibration.load_error"))
# Make sure there are 256 entries in the .cal file, otherwise dispwin
# will complain and not be able to load it back in.
# Under Windows, the GetDeviceGammaRamp API seems to enforce 256 entries
# regardless of graphics card, but under Linux and Mac OS X there may be
# more than 256 entries if the graphics card has greater than 8 bit
# videoLUTs (e.g. Quadro and newer consumer cards)
cgats = CGATS(outfilename)
data = cgats.queryv1("DATA")
if data and len(data) != 256:
print("VideoLUT has {:d} entries, interpolating to 256".format(len(data)))
rgb = {"I": [], "R": [], "G": [], "B": []}
for entry in data.values():
for column in ("I", "R", "G", "B"):
rgb[column].append(entry["RGB_" + column])
interp = {}
for column in ("R", "G", "B"):
interp[column] = colormath.Interp(rgb["I"], rgb[column])
resized = CGATS()
data.parent.DATA = resized
resized.key = "DATA"
resized.parent = data.parent
resized.root = cgats
resized.type = b"DATA"
for i in range(256):
entry = {"RGB_I": i / 255.0}
for column in ("R", "G", "B"):
entry["RGB_" + column] = interp[column](entry["RGB_I"])
resized.add_data(entry)
cgats.write()
return result
def set_argyll_version(self, name, silent=False, cfg=False):
self.set_argyll_version_from_string(
get_argyll_version_string(name, silent), cfg
)
def set_argyll_version_from_string(self, argyll_version_string, cfg=True):
self.argyll_version_string = argyll_version_string
if cfg:
setcfg("argyll.version", argyll_version_string)
# Always write cfg directly after setting Argyll version so
# subprocesses that read the configuration will use the right
# version
writecfg()
self.argyll_version = parse_argyll_version_string(argyll_version_string)
def set_sessionlogfile(self, sessionlogfile, basename, dirname):
if sessionlogfile:
self.sessionlogfile = sessionlogfile
else:
self.sessionlogfile = LogFile(basename, dirname)
self.sessionlogfiles[basename] = self.sessionlogfile
def set_terminal_cgats(self, cgats):
if not isinstance(cgats, CGATS):
try:
cgats = CGATS(cgats)
except (
IOError,
CGATSInvalidError,
CGATSInvalidOperationError,
CGATSKeyError,
CGATSTypeError,
CGATSValueError,
) as exception:
return exception
self.terminal.cgats = cgats
def setup_inout(self, basename=None):
"""Setup in/outfile basename and session logfile."""
dirname = self.create_tempdir()
if not dirname or isinstance(dirname, Exception):
return dirname
# Check directory and in/output file(s)
result = check_create_dir(dirname)
if isinstance(result, Exception):
return result
if basename is None:
basename = getcfg("profile.name.expanded")
basename = make_argyll_compatible_path(basename)
self.set_sessionlogfile(None, basename, dirname)
return os.path.join(dirname, basename)
def single_real_display(self):
return len(self.get_real_displays()) == 1
def argyll_support_file_exists(self, name, scope=None):
"""Check if named file exists in any of the known Argyll support
locations valid for the chosen Argyll CMS version.
Scope can be 'u' (user), 'l' (local system) or None (both)
"""
paths = []
if sys.platform != "darwin":
if not scope or scope == "u":
paths.append(defaultpaths.appdata)
if not scope or scope == "l":
paths += defaultpaths.commonappdata
else:
if not scope or scope == "u":
paths.append(defaultpaths.library_home)
if not scope or scope == "l":
paths.append(defaultpaths.library)
searchpaths = []
if self.argyll_version >= [1, 5, 0]:
if sys.platform != "darwin":
searchpaths.extend(
os.path.join(dir_, "ArgyllCMS", name) for dir_ in paths
)
else:
paths2 = []
if not scope or scope == "u":
paths2.append(defaultpaths.appdata)
if not scope or scope == "l":
paths2.append(defaultpaths.library)
if [1, 9] <= self.argyll_version <= [1, 9, 1]:
# Argyll CMS 1.9 and 1.9.1 use *nix locations due to a
# configuration problem
paths2.extend(
[
os.path.join(defaultpaths.home, ".local", "share"),
"/usr/local/share",
]
)
searchpaths.extend(
os.path.join(dir_, "ArgyllCMS", name) for dir_ in paths2
)
searchpaths.extend(os.path.join(dir_, "color", name) for dir_ in paths)
for searchpath in searchpaths:
if os.path.isfile(searchpath):
return True
return False
def spyder2_firmware_exists(self, scope=None):
"""Check if the Spyder 2 firmware file exists in any of the known
locations valid for the chosen Argyll CMS version.
Scope can be 'u' (user), 'l' (local system) or None (both)
"""
if self.argyll_version < [1, 2, 0]:
spyd2en = get_argyll_util("spyd2en")
if not spyd2en:
return False
return os.path.isfile(
os.path.join(os.path.dirname(spyd2en), "spyd2PLD.bin")
)
else:
return self.argyll_support_file_exists("spyd2PLD.bin", scope=scope)
def spyder4_cal_exists(self):
"""Check if the Spyder4/5 calibration file exists in any of the known
locations valid for the chosen Argyll CMS version."""
if self.argyll_version < [1, 3, 6]:
# We couldn't use it even if it exists
return False
return self.argyll_support_file_exists("spyd4cal.bin")
def start(
self,
consumer,
producer,
cargs=(),
ckwargs=None,
wargs=(),
wkwargs=None,
progress_title=appname,
progress_msg="",
parent=None,
progress_start=100,
resume=False,
continue_next=False,
stop_timers=True,
interactive_frame="",
pauseable=False,
cancelable=True,
show_remaining_time=True,
fancy=True,
):
# The following statement is super hacky, I didn't want to use it but the
# original developer wanted to add the ``appname`` into the docstring
# programmatically. So, I think this is the best but hacky way to do it.
self.start.__func__.__doc__ = f"""Start a worker process.
Also show a progress dialog while the process is running.
consumer consumer function.
producer producer function.
cargs consumer arguments.
ckwargs consumer keyword arguments.
wargs producer arguments.
wkwargs producer keyword arguments.
progress_title progress dialog title. Defaults to '{appname}'.
progress_msg progress dialog message. Defaults to ''.
progress_start show progress dialog after delay (ms).
resume resume previous progress dialog (elapsed time etc).
continue_next do not hide progress dialog after producer finishes.
stop_timers stop the timers on the owner window if True
interactive_frame "" or "uniformity" (selects the type of
interactive window)
pauseable Is the operation pauseable? (show pause button on
progress dialog)
cancelable Is the operation cancelable? (show cancel button on
progress dialog)
fancy Use fancy progress dialog with animated throbber &
sound fx
"""
if ckwargs is None:
ckwargs = {}
if wkwargs is None:
wkwargs = {}
while self.is_working():
sleep(0.25) # wait until previous worker thread finishes
if hasattr(self.owner, "stop_timers") and stop_timers:
self.owner.stop_timers()
if not parent:
parent = self.owner
if progress_start < 1:
# Can't be zero!
progress_start = 1
self.activated = False
self.cmdname = None
self.cmdrun = False
self.finished = False
self.instrument_calibration_complete = False
if not resume:
self.instrument_on_screen = False
self.instrument_place_on_screen_msg = False
self.instrument_sensor_position_msg = False
self.interactive_frame = interactive_frame
self.is_single_measurement = (
interactive_frame == "ambient"
or interactive_frame == "luminance"
or (
isinstance(interactive_frame, wx.TopLevelWindow)
and interactive_frame.Name == "VisualWhitepointEditor"
)
)
self.is_ambient_measurement = interactive_frame == "ambient"
self.lastcmdname = None
self.pauseable = pauseable
self.paused = False
self.cancelable = cancelable
self.show_remaining_time = show_remaining_time
self.fancy = fancy
self.resume = resume
self.subprocess_abort = False
self.abort_requested = False
self.starttime = time()
self.thread_abort = False
if (
fancy
and (
not self.interactive
or interactive_frame not in ("uniformity", "untethered")
)
and not isinstance(interactive_frame, wx.TopLevelWindow)
):
# Pre-init progress dialog bitmaps
ProgressDialog.get_bitmaps(0)
ProgressDialog.get_bitmaps(1)
if self.interactive:
self.progress_start_timer = wx.Timer()
if progress_msg and progress_title == appname:
progress_title = progress_msg
if (
config.get_display_name() == "Untethered"
and interactive_frame != "uniformity"
):
interactive_frame = "untethered"
if interactive_frame == "adjust":
windowclass = DisplayAdjustmentFrame
elif interactive_frame == "uniformity":
windowclass = DisplayUniformityFrame
elif interactive_frame == "untethered":
windowclass = UntetheredFrame
else:
windowclass = SimpleTerminal
if getattr(self, "terminal", None) and isinstance(
self.terminal, windowclass
):
self.progress_wnd = self.terminal
if not resume:
if isinstance(self.progress_wnd, SimpleTerminal):
self.progress_wnd.console.SetValue("")
elif (
isinstance(self.progress_wnd, DisplayAdjustmentFrame)
or isinstance(self.progress_wnd, DisplayUniformityFrame)
or isinstance(self.progress_wnd, UntetheredFrame)
):
self.progress_wnd.reset()
self.progress_wnd.stop_timer()
self.progress_wnd.Resume()
self.progress_wnd.start_timer()
# UGLY HACK: This 'print' call fixes a GTK assertion and
# segfault under Arch Linux when setting the window title
print("")
if isinstance(self.progress_wnd, SimpleTerminal):
self.progress_wnd.SetTitle(progress_title)
self.progress_wnd.Show()
if resume and isinstance(self.progress_wnd, SimpleTerminal):
self.progress_wnd.console.ScrollLines(
self.progress_wnd.console.GetNumberOfLines()
)
else:
if getattr(self, "terminal", None):
# Destroy the wx object so the decref'd python object can
# be garbage collected
self.terminal.Destroy()
if interactive_frame == "adjust":
self.terminal = DisplayAdjustmentFrame(
parent,
handler=self.progress_handler,
keyhandler=self.terminal_key_handler,
)
elif interactive_frame == "uniformity":
self.terminal = DisplayUniformityFrame(
parent,
handler=self.progress_handler,
keyhandler=self.terminal_key_handler,
)
elif interactive_frame == "untethered":
self.terminal = UntetheredFrame(
parent,
handler=self.progress_handler,
keyhandler=self.terminal_key_handler,
)
elif isinstance(interactive_frame, wx.TopLevelWindow):
self.terminal = interactive_frame
else:
self.terminal = SimpleTerminal(
parent,
title=progress_title,
handler=self.progress_handler,
keyhandler=self.terminal_key_handler,
)
if getattr(self.terminal, "worker", self) is not self:
print(
"DEBUG: In worker.Worker.start: worker.Worker()."
"terminal.worker is not self! This is probably a"
"bug."
)
self.terminal.worker = self
self.progress_wnd = self.terminal
else:
if not progress_msg:
progress_msg = lang.getstr("please_wait")
# Show the progress dialog after a delay
self.progress_start_timer = BetterCallLater(
progress_start,
self.progress_dlg_start,
progress_title,
progress_msg,
parent,
resume,
fancy,
)
if not hasattr(self, "_disabler"):
skip = self.progress_wnds
if self.owner and hasattr(self.owner, "measureframe"):
skip.append(self.owner.measureframe)
self._disabler = BetterWindowDisabler(skip)
# Can't use startWorker because we may need access to self.thread from
# within thread, and startWorker does not return before the actual
# thread starts running
jobID = None
sender = delayedresult.SenderCallAfter(
self._generic_consumer,
jobID,
args=[consumer, continue_next] + list(cargs),
kwargs=ckwargs,
)
self.thread = delayedresult.Producer(
sender,
Producer(self, producer, continue_next),
args=wargs,
kwargs=wkwargs,
name=jobID,
group=None,
daemon=False,
senderArg=None,
sendReturn=True,
)
self.thread.start()
return True
def stop_progress(self):
if hasattr(self, "_disabler"):
del self._disabler
if getattr(self, "progress_wnd", False):
if getattr(self.progress_wnd, "dlg", None):
if self.progress_wnd.dlg.IsShownOnScreen():
self.progress_wnd.dlg.EndModal(wx.ID_CANCEL)
self.progress_wnd.dlg = None
self.progress_wnd.stop_timer()
if getattr(self.progress_wnd, "Name", None) == "VisualWhitepointEditor":
self.progress_wnd.Close()
else:
self.progress_wnd.Hide()
self.subprocess_abort = False
self.thread_abort = False
self.interactive = False
self.resume = False
self._detected_output_levels = False
# Uninhibit session if needed
if hasattr(self, "dbus_ifaces"):
for bus_name in self.dbus_ifaces:
iface_dict = self.dbus_ifaces[bus_name]
cookie = iface_dict.get("cookie")
if cookie:
# Uninhibit. Note that if (e.g.) screensaver timeout has
# occured during the time the session was inhibited, that
# may now kick in immediately after uninhibiting
uninhibit = iface_dict.get("uninhibit", "un_inhibit")
try:
getattr(iface_dict["iface"], uninhibit)(cookie)
except DBusException as exception:
self.log(exception)
else:
iface_dict["cookie"] = None
self.log(f"{appname}: Uninhibited {bus_name}")
def swap_progress_wnds(self):
"""Swap the current interactive window with a progress dialog"""
parent = self.terminal.GetParent()
if isinstance(self.terminal, DisplayAdjustmentFrame):
title = lang.getstr("calibration")
else:
title = self.terminal.GetTitle()
self.progress_dlg_start(title, "", parent, self.resume, self.fancy)
def terminal_key_handler(self, event):
"""Key handler for the interactive window or progress dialog."""
keycode = None
if event.GetEventType() in (wx.EVT_CHAR_HOOK.typeId, wx.EVT_KEY_DOWN.typeId):
keycode = event.GetKeyCode()
elif event.GetEventType() == wx.EVT_MENU.typeId:
keycode = self.progress_wnd.id_to_keycode.get(event.GetId())
if (
keycode is not None
and getattr(self, "subprocess", None)
and hasattr(self.subprocess, "send")
):
keycode = keycodes.get(keycode, keycode)
if keycode in (ord("\x1b"), ord("8"), ord("Q"), ord("q")):
# exit
self.abort_subprocess(True)
return
try:
self.safe_send(chr(keycode))
except Exception:
pass
def calculate_gamut(
self,
profile_path,
intent="r",
direction="f",
order="n",
compare_standard_gamuts=True,
):
"""Calculate gamut, volume, and coverage % against sRGB and Adobe RGB.
Return gamut volume (int, scaled to sRGB = 1.0) and
coverage (dict) as tuple.
"""
if isinstance(profile_path, list):
profile_paths = profile_path
else:
profile_paths = [profile_path]
outname = os.path.splitext(profile_paths[0])[0]
mods = []
if intent != "r":
mods.append(intent)
if direction != "f":
mods.append(direction)
if order != "n":
mods.append(order)
if mods:
outname += " {}".format(
"".join(["[{}]".format(mod.upper()) for mod in mods])
)
gamut_volume = None
gamut_coverage = {}
# Create profile gamut and vrml
det = getcfg("iccgamut.surface_detail")
for i, profile_path in enumerate(profile_paths):
if not profile_path:
self.log(f"Warning: calculate_gamut(): No profile path {i:d}")
continue
result = self.exec_cmd(
get_argyll_util("iccgamut"),
[
"-v",
"-w",
"-i" + intent,
"-f" + direction,
"-o" + order,
f"-d{det:.2f}",
profile_path,
],
capture_output=True,
skip_scripts=True,
)
if not isinstance(result, Exception) and result:
# iccgamut output looks like this:
# Header:
# <...>
#
# Total volume of gamut is xxxxxx.xxxxxx cubic colorspace units
for line in self.output:
match = re.search(
r"(\d+(?:\.\d+)?)\s+cubic\s+colorspace\s+units", line
)
if match:
gamut_volume = float(match.groups()[0]) / GAMUT_VOLUME_SRGB
break
else:
break
name = os.path.splitext(profile_paths[0])[0]
gamfilename = name + ".gam"
wrlfilename = name + ".wrl"
tmpfilenames = [gamfilename, wrlfilename]
if compare_standard_gamuts:
comparison_gamuts = [
("srgb", "sRGB"),
("adobe-rgb", "ClayRGB1998"),
("dci-p3", "SMPTE431_P3"),
]
else:
comparison_gamuts = []
for profile_path in profile_paths[1:]:
filename, ext = os.path.splitext(profile_path)
comparison_gamuts.append(
(filename.lower().replace(" ", "-"), filename + ".gam")
)
threads = []
viewgam = get_argyll_util("viewgam")
for key, src in comparison_gamuts:
if not isinstance(result, Exception) and result:
# Create gamut view and intersection
if os.path.isabs(src):
src_path = src
src = os.path.splitext(os.path.basename(src))[0]
else:
if mods:
src += " " + "".join([f"[{mod.upper()}]" for mod in mods])
src_path = get_data_path(f"ref/{src}.gam")
if not src_path:
continue
outfilename = outname + " vs " + src
if mods:
outfilename += " " + "".join([f"[{mod.upper()}]" for mod in mods])
outfilename += ".wrl"
tmpfilenames.append(outfilename)
# Multi-threaded gamut view calculation
worker = Worker()
args = [
"-cw",
"-t0",
"-w",
src_path,
"-cn",
"-t.3",
"-s",
gamfilename,
"-i",
outfilename,
]
thread = threading.Thread(
target=self.create_gamut_view_worker,
name="CreateGamutViewWorker",
args=(worker, viewgam, args, key, src, gamut_coverage),
)
threads.append((thread, worker, args))
thread.start()
# Wait for threads to finish
for thread, worker, args in threads:
thread.join()
self.log("-" * 80)
self.log(lang.getstr("commandline"))
printcmdline(viewgam, args, fn=self.log, cwd=os.path.dirname(args[-1]))
self.log("")
self.log("".join(worker.output))
if not isinstance(result, Exception) and result:
for tmpfilename in tmpfilenames:
if tmpfilename == gamfilename and tmpfilename != f"{outname}.gam":
# Use the original file name
filename = f"{outname}.gam"
elif tmpfilename == wrlfilename and tmpfilename != f"{outname}.wrl":
# Use the original file name
filename = f"{outname}.wrl"
else:
filename = tmpfilename
try:
def tweak_vrml(vrml):
# Set viewpoint further away
vrml = re.sub(
rb"(Viewpoint\s*\{)[^}]+\}", rb"\1 position 0 0 340 }", vrml
)
# Fix label color for -a* axis
label = re.search(
rb'Transform\s*\{\s*translation\s+[+\-0-9.]+\s*[+\-0-9.]+\s*[+\-0-9.]+\s+children\s*\[\s*Shape\s*\{\s*geometry\s+Text\s*\{\s*string\s*\["-a\*"\]\s*fontStyle\s+FontStyle\s*\{[^}]*\}\s*\}\s*appearance\s+Appearance\s*\{\s*material\s+Material\s*{[^}]*\}\s*\}\s*\}\s*\]\s*\}',
vrml,
)
if label:
label = label.group()
vrml = vrml.replace(
label,
re.sub(
rb"(diffuseColor)\s+[+\-0-9.]+\s+[+\-0-9.]+\s+[+\-0-9.]+",
rb"\1 0.0 1.0 0.0",
label,
),
)
# Add range to axes
vrml = re.sub(
rb'(string\s*\[")(\+?)(L\*)("\])',
rb'\1\3", b"\2\0$\4',
vrml,
)
vrml = re.sub(
rb'(string\s*\[")([+\-]?)(a\*)("\])',
rb'\1\3", b"\2\0$\4',
vrml,
)
vrml = re.sub(
rb'(string\s*\[")([+\-]?)(b\*)("\])', rb"\1\3 \2\0$\4", vrml
)
vrml = vrml.replace(b"\0$", b"100")
return vrml
gzfilename = f"{filename}.gz"
if sys.platform == "win32":
filename = make_win32_compatible_long_path(filename)
gzfilename = make_win32_compatible_long_path(gzfilename)
tmpfilename = make_win32_compatible_long_path(tmpfilename)
if getcfg("vrml.compress"):
# Compress gam and wrl files using gzip
with GzipFileProper(gzfilename, "wb") as gz:
# Always use original filename with '.gz' extension,
# that way the filename in the header will be correct
with open(tmpfilename, "rb") as infile:
gz.write(tweak_vrml(infile.read()))
# Remove uncompressed file
os.remove(tmpfilename)
tmpfilename = gzfilename
else:
with open(tmpfilename, "rb") as infile:
vrml = infile.read().decode()
with open(tmpfilename, "wb") as outfile:
outfile.write(tweak_vrml(vrml).encode())
if filename.endswith(".wrl"):
filename = f"{filename[:-4]}.wrz"
else:
filename = gzfilename
if tmpfilename != filename:
# Rename the file if filename is different
if os.path.exists(filename):
os.remove(filename)
os.rename(tmpfilename, filename)
except Exception as exception:
self.log(exception)
elif result:
# Exception
self.log(result)
return gamut_volume, gamut_coverage
@staticmethod
def create_gamut_view_worker(worker, viewgam, args, key, src, gamut_coverage):
"""Gamut view creation producer"""
try:
result = worker.exec_cmd(
viewgam,
args,
capture_output=True,
skip_scripts=True,
silent=True,
log_output=False,
)
if not isinstance(result, Exception) and result:
# viewgam output looks like this:
# Intersecting volume = xxx.x cubic units
# 'path/to/1.gam' volume = xxx.x cubic units, intersect = xx.xx%
# 'path/to/2.gam' volume = xxx.x cubic units, intersect = xx.xx%
for line in worker.output:
match = re.search(
r"[\/]{}.gam'\s+volume\s*=\s*"
r"\d+(?:\.\d+)?\s+cubic\s+units,?"
r"\s+intersect\s*=\s*"
r"(\d+(?:\.\d+)?)".format(re.escape(src)),
line,
)
if match:
gamut_coverage[key] = float(match.groups()[0]) / 100.0
break
except Exception:
traceback.print_exc()
worker.log(traceback.format_exc())
def calibrate(self, remove=False):
"""Calibrate the screen and process the generated file(s)."""
result = self.detect_video_levels()
if isinstance(result, Exception) or not result:
return result
capture_output = False
if sys.stdout and hasattr(sys.stdout, "isatty") and not sys.stdout.isatty():
capture_output = True
cmd, args = self.prepare_dispcal()
if not isinstance(cmd, Exception):
print(f"cmd: {cmd}")
print(f"args: {args}")
result = self.exec_cmd(cmd, args, capture_output=capture_output)
print(f"result: {result}")
else:
result = cmd
if not isinstance(result, Exception) and result and getcfg("trc"):
dst_pathname = os.path.join(
getcfg("profile.save_path"),
getcfg("profile.name.expanded"),
getcfg("profile.name.expanded"),
)
print(f"dst_pathname: {dst_pathname}")
cal = args[-1] + ".cal"
print(f"cal : {cal}")
result = check_cal_isfile(
cal, lang.getstr("error.calibration.file_not_created")
)
print(f"result : {result}")
if not isinstance(result, Exception) and result:
cal_cgats = add_dispcal_options_to_cal(cal, self.options_dispcal)
if cal_cgats:
cal_cgats.write()
if getcfg("profile.update") or self.dispcal_create_fast_matrix_shaper:
profile_path = args[-1] + profile_ext
result = check_profile_isfile(
profile_path, lang.getstr("error.profile.file_not_created")
)
if not isinstance(result, Exception) and result:
try:
profile = ICCProfile(profile_path)
except (IOError, ICCProfileInvalidError):
result = Error(
lang.getstr("profile.invalid") + "\n" + profile_path
)
if not isinstance(result, Exception) and result:
if not getcfg("profile.update"):
# Created fast matrix shaper profile
# we need to set cprt, targ and a few other things
profile.setCopyright(getcfg("copyright"))
# Fast matrix shaper profiles currently don't
# contain TI3 data, but look for it anyways
# to be future-proof
ti3 = add_options_to_ti3(
profile.tags.get("targ", profile.tags.get("CIED", "")),
self.options_dispcal,
)
if not ti3:
ti3 = CGATS("TI3\n")
ti3[1] = cal_cgats
edid = self.get_display_edid()
display_name = edid.get(
"monitor_name",
edid.get("ascii", str(edid.get("product_id") or "")),
)
display_manufacturer = edid.get("manufacturer")
profile.setDeviceModelDescription(display_name)
if display_manufacturer:
profile.setDeviceManufacturerDescription(
display_manufacturer
)
(gamut_volume, gamut_coverage) = self.create_gamut_views(
profile_path
)
self.update_profile(
profile,
ti3=str(ti3),
chrm=None,
tags=True,
avg=None,
peak=None,
rms=None,
gamut_volume=gamut_volume,
gamut_coverage=gamut_coverage,
quality=getcfg("calibration.quality"),
)
else:
# Update desc tag - ASCII needs to be 7-bit
# also add Unicode string if different from ASCII
if "desc" in profile.tags and isinstance(
profile.tags.desc, TextDescriptionType
):
profile.setDescription(getcfg("profile.name.expanded"))
# Calculate profile ID
profile.calculateID()
try:
profile.write()
except Exception as exception:
self.log(exception)
result2 = self.wrapup(
not isinstance(result, UnloggedInfo) and result,
remove or isinstance(result, Exception) or not result,
)
if isinstance(result2, Exception):
if isinstance(result, Exception):
result = Error(str(result) + "\n\n" + str(result2))
else:
result = result2
elif not isinstance(result, Exception) and result and getcfg("trc"):
setcfg("last_cal_path", dst_pathname + ".cal")
setcfg("calibration.file.previous", getcfg("calibration.file", False))
if getcfg("profile.update") or self.dispcal_create_fast_matrix_shaper:
setcfg("last_cal_or_icc_path", dst_pathname + profile_ext)
setcfg("last_icc_path", dst_pathname + profile_ext)
setcfg("calibration.file", dst_pathname + profile_ext)
else:
setcfg("calibration.file", dst_pathname + ".cal")
setcfg("last_cal_or_icc_path", dst_pathname + ".cal")
return result
@property
def calibration_loading_generally_supported(self):
# Loading/clearing calibration seems to have undesirable side-effects
# on Mac OS X 10.6 and newer
# Wayland does not support videoLUT access (only by installing a
# profile via colord)
return (
sys.platform != "darwin" or intlist(mac_ver()[0].split(".")) < [10, 6]
) and os.getenv("XDG_SESSION_TYPE") != "wayland"
@property
def calibration_loading_supported(self):
# Loading/clearing calibration seems to have undesirable side-effects
# on Mac OS X 10.6 and newer
return (
self.calibration_loading_generally_supported
and not config.is_virtual_display()
)
def change_display_profile_cal_whitepoint(
self, profile, x, y, outfilename, calibration_only=False, use_collink=False
):
"""Change display profile (and calibration) whitepoint.
Do it in an colorimetrically accurate manner (as far as possible).
"""
XYZw = colormath.xyY2XYZ(x, y)
xicclu = get_argyll_util("xicclu")
if not xicclu:
return Error(lang.getstr("argyll.util.not_found", "xicclu"))
tempdir = self.create_tempdir()
if isinstance(tempdir, Exception):
return tempdir
outpathname = os.path.splitext(outfilename)[0]
outname = os.path.basename(outpathname)
logfiles = Files([self.recent, self.lastmsg, LineBufferedStream(print)])
ofilename = profile.fileName
temppathname = os.path.join(tempdir, outname)
if ofilename and os.path.isfile(ofilename):
# Profile comes from a file
temp = False
temporig = ofilename
else:
# Profile not associated to a file, write to temp dir
temp = True
profile.fileName = temporig = temppathname + ".orig.icc"
profile.write(temporig)
# Remember original white XYZ
origXYZ = profile.tags.wtpt.X, profile.tags.wtpt.Y, profile.tags.wtpt.Z
# Make a copy of the profile with changed whitepoint
profile.tags.wtpt.X, profile.tags.wtpt.Y, profile.tags.wtpt.Z = XYZw
tempcopy = temppathname + ".copy.icc"
profile.write(tempcopy)
# Generate updated calibration with changed whitepoint
if use_collink:
collink = get_argyll_util("collink")
if not collink:
profile.fileName = ofilename
self.wrapup(False)
return Error(lang.getstr("argyll.util.not_found", "collink"))
linkpath = temppathname + ".link.icc"
result = self.exec_cmd(
collink,
["-v", "-n", "-G", "-iaw", "-b", tempcopy, temporig, linkpath],
capture_output=True,
)
if not result or isinstance(result, Exception):
profile.fileName = ofilename
self.wrapup(False)
return result
link = ICCProfile(linkpath)
RGBscaled = []
for i in range(256):
RGBscaled.append([i / 255.0] * 3)
RGBscaled = self.xicclu(link, RGBscaled)
logfiles.write("RGB white {:6.4f} {:6.4f} {:6.4f}\n".format(*RGBscaled[-1]))
# Restore original white XYZ
profile.tags.wtpt.X, profile.tags.wtpt.Y, profile.tags.wtpt.Z = origXYZ
# Get white RGB
XYZwscaled = self.xicclu(profile, RGBscaled[-1], "a", pcs="x")[0]
logfiles.write(
"XYZ white {:6.4f} {:6.4f} {:6.4f}, CCT {:0.0f}\n".format(
*(XYZwscaled + [colormath.XYZ2CCT(*XYZwscaled)])
)
)
else:
# Lookup scaled down white XYZ
logfiles.write("Looking for solution...\n")
XYZscaled = []
for i in range(2000):
XYZscaled.append([v * (1 - i / 1999.0) for v in XYZw])
RGBscaled = self.xicclu(
profile, XYZscaled, "a", "if", pcs="x", get_clip=True
)
# Set filename to copy (used by worker.xicclu to get profile path)
profile.fileName = tempcopy
# Find point at which it no longer clips
for i, RGBclip in enumerate(RGBscaled):
if RGBclip[3] is True:
# Clipped, skip
continue
# Found
XYZwscaled = XYZscaled[i]
logfiles.write(
"Solution found at index {:d} (step size {:f})\n".format(
i, 1 / 1999.0
)
)
logfiles.write(
"RGB white {:6.4f} {:6.4f} {:6.4f}\n".format(*RGBclip[:3])
)
logfiles.write(
"XYZ white {:6.4f} {:6.4f} {:6.4f}, CCT {:0.0f}\n".format(
*(XYZscaled[i] + [colormath.XYZ2CCT(*XYZscaled[i])])
)
)
break
else:
profile.fileName = ofilename
self.wrapup(False)
return Error(f"No solution found in {i:d} steps")
# Generate RGB input values
# Reduce interpolation res as target whitepoint moves farther away
# from profile whitepoint in RGB
res = max(int(round((min(RGBclip[:3]) / 1.0) * 33)), 9)
logfiles.write(f"Interpolation res {res:d}\n")
RGBscaled = []
for i in range(res):
RGBscaled.append([v * (i / (res - 1.0)) for v in (1, 1, 1)])
# Lookup RGB -> XYZ through whitepoint adjusted profile
RGBscaled2XYZ = self.xicclu(profile, RGBscaled, "a", pcs="x")
# Restore original XYZ
profile.tags.wtpt.X, profile.tags.wtpt.Y, profile.tags.wtpt.Z = origXYZ
# Restore original filename (used by worker.xicclu to get profile path)
profile.fileName = temporig
# Get original black point
XYZk = self.xicclu(profile, [0, 0, 0], "a", pcs="x")[0]
logfiles.write("XYZ black {:6.4f} {:6.4f} {:6.4f}\n".format(*XYZk))
logfiles.write(
"XYZ white after forward lookup {:6.4f} {:6.4f} {:6.4f}\n".format(
*RGBscaled2XYZ[-1]
)
)
# Scale down XYZ
XYZscaled = []
for i in range(res):
XYZ = [v * XYZwscaled[1] for v in RGBscaled2XYZ[i]]
if i == 0:
bp_in = XYZ
XYZ = colormath.apply_bpc(
*XYZ, bp_in=bp_in, bp_out=XYZk, wp_out=XYZwscaled, weight=True
)
XYZscaled.append(XYZ)
logfiles.write(
"XYZ white after scale down {:6.4f} {:6.4f} {:6.4f}\n".format(
*XYZscaled[-1]
)
)
# Lookup XYZ -> RGB through original profile
RGBscaled = self.xicclu(
profile, XYZscaled, "a", "if", pcs="x", use_cam_clipping=True
)
logfiles.write(
"RGB black after inverse forward lookup {:6.4f} {:6.4f} {:6.4f}\n".format(
RGBscaled[0]
)
)
logfiles.write(
"RGB white after inverse forward lookup {:6.4f} {:6.4f} {:6.4f}\n".format(
*RGBscaled[-1]
)
)
if res != 256:
# Interpolate
R = []
G = []
B = []
for i, RGB in enumerate(RGBscaled):
R.append(RGB[0])
G.append(RGB[1])
B.append(RGB[2])
if res > 2:
# Catmull-Rom spline interpolation
Ri = CRInterpolation(R)
Gi = CRInterpolation(G)
Bi = CRInterpolation(B)
else:
# Linear interpolation
Ri = colormath.Interp(list(range(res)), R)
Gi = colormath.Interp(list(range(res)), G)
Bi = colormath.Interp(list(range(res)), B)
RGBscaled = []
step = (res - 1) / 255.0
for i in range(256):
RGB = [Ri(i * step), Gi(i * step), Bi(i * step)]
RGBscaled.append(RGB)
logfiles.write(
"RGB black after interpolation {:6.4f} {:6.4f} {:6.4f}\n".format(
*RGBscaled[0]
)
)
logfiles.write(
"RGB white after interpolation {:6.4f} {:6.4f} {:6.4f}\n".format(
*RGBscaled[-1]
)
)
has_nonlinear_vcgt = (
isinstance(profile.tags.get("vcgt"), VideoCardGammaType)
and not profile.tags.vcgt.is_linear()
)
if has_nonlinear_vcgt:
# Apply cal
ocal = extract_cal_from_profile(profile)
bp_out = list(ocal.queryv1({"RGB_R": 0, "RGB_G": 0, "RGB_B": 0}).values())
ocalpath = temppathname + ".cal"
ocal.filename = ocalpath
RGBscaled = self.xicclu(ocal, RGBscaled)
logfiles.write(
"RGB black after cal {:6.4f} {:6.4f} {:6.4f}\n".format(*RGBscaled[0])
)
logfiles.write(
"RGB white after cal {:6.4f} {:6.4f} {:6.4f}\n".format(*RGBscaled[-1])
)
else:
bp_out = (0, 0, 0)
cal = """CAL
KEYWORD "DEVICE_CLASS"
DEVICE_CLASS "DISPLAY"
KEYWORD "COLOR_REP"
COLOR_REP "RGB"
BEGIN_DATA_FORMAT
RGB_I RGB_R RGB_G RGB_B
END_DATA_FORMAT
NUMBER_OF_SETS 256
BEGIN_DATA
"""
for i, RGB in enumerate(RGBscaled):
R, G, B = colormath.apply_bpc(
*RGB,
bp_in=RGBscaled[0],
bp_out=bp_out,
wp_out=RGBscaled[-1],
weight=True,
)
cal += "{:f} {:f} {:f} {:f}\n".format(i / 255.0, R, G, B)
cal += "END_DATA"
cal = CGATS(cal)
cal.filename = outpathname + ".cal"
cal.write()
if calibration_only:
# Only update calibration
profile.setDescription(outname)
profile.tags.vcgt = cal_to_fake_profile(cal).tags.vcgt
profile.tags.wtpt.X, profile.tags.wtpt.Y, profile.tags.wtpt.Z = XYZw
profile.calculateID()
profile.write(outfilename)
self.wrapup(False)
return True
else:
# Re-create profile
cti3 = None
if isinstance(profile.tags.get("targ"), Text):
# Get measurement data
try:
cti3 = CGATS(profile.tags.targ)
except (IOError, CGATSError):
pass
else:
if 0 not in cti3 or cti3[0].type.strip() != b"CTI3":
# Not Argyll measurement data
cti3 = None
if not cti3:
# Use fakeread
fakeread = get_argyll_util("fakeread")
if not fakeread:
profile.fileName = ofilename
self.wrapup(False)
return Error(lang.getstr("argyll.util.not_found", "fakeread"))
shutil.copyfile(defaults["testchart.file"], temppathname + ".ti1")
result = self.exec_cmd(fakeread, [temporig, temppathname])
if not result or isinstance(result, Exception):
profile.fileName = ofilename
self.wrapup(False)
return result
cti3 = CGATS(temppathname + ".ti3")
# Get RGB from measurement data
RGBorig = []
for i, sample in cti3[0].DATA.items():
RGB = []
for j, component in enumerate("RGB"):
RGB.append(sample["RGB_" + component])
RGBorig.append(RGB)
# Lookup RGB -> scaled RGB through calibration
RGBscaled = self.xicclu(cal, RGBorig, scale=100)
if has_nonlinear_vcgt:
# Undo original calibration
RGBscaled = self.xicclu(ocal, RGBscaled, direction="b", scale=100)
# Update CAL in ti3 file
if 1 in cti3 and cti3[1].type.strip() == b"CAL":
cti3[1].DATA = cal[0].DATA
else:
cti3[1] = cal[0]
# Lookup scaled RGB -> XYZ through profile
RGBscaled2XYZ = self.xicclu(profile, RGBscaled, "a", pcs="x", scale=100)
# Update measurement data
if "LUMINANCE_XYZ_CDM2" in cti3[0]:
XYZa = [
float(v) * XYZwscaled[i]
for i, v in enumerate(cti3[0].LUMINANCE_XYZ_CDM2.split())
]
cti3[0].add_keyword(
"LUMINANCE_XYZ_CDM2", " ".join([str(v) for v in XYZa])
)
for i, sample in cti3[0].DATA.items():
for j, component in enumerate("XYZ"):
sample["XYZ_" + component] = (
RGBscaled2XYZ[i][j] / XYZwscaled[1] * 100
)
cti3.write(temppathname + ".ti3")
# Preserve custom tags
display_name = profile.getDeviceModelDescription()
display_manufacturer = profile.getDeviceManufacturerDescription()
tags = {}
for tagname in ("mmod", "meta"):
if tagname in profile.tags:
tags[tagname] = profile.tags[tagname]
if temp:
os.remove(temporig)
os.remove(tempcopy)
# Compute profile
self.options_targen = ["-d3"]
return self.create_profile(
outfilename, True, display_name, display_manufacturer, tags
)
def chart_lookup(
self,
cgats,
profile,
as_ti3=False,
fields=None,
check_missing_fields=False,
function="f",
pcs="l",
intent="r",
bt1886=None,
white_patches=4,
white_patches_total=True,
raise_exceptions=False,
):
"""Lookup CIE or device values through profile"""
if profile.colorSpace == b"RGB":
labels = ("RGB_R", "RGB_G", "RGB_B")
else:
labels = ("CMYK_C", "CMYK_M", "CMYK_Y", "CMYK_K")
ti1 = None
ti3_ref = None
gray = None
try:
if not isinstance(cgats, CGATS):
cgats = CGATS(cgats, True)
else:
# Always make a copy and do not alter a passed in CGATS instance!
cgats_filename = cgats.filename
cgats = CGATS(bytes(cgats))
cgats.filename = cgats_filename
if 0 in cgats:
# only look at the first section
cgats[0].filename = cgats.filename
cgats = cgats[0]
primaries = cgats.queryi(labels)
if primaries and not as_ti3:
primaries.fix_device_values_scaling(profile.colorSpace)
cgats.type = b"CTI1"
cgats.COLOR_REP = profile.colorSpace
ti1, ti3_ref, gray = self.ti1_lookup_to_ti3(
cgats,
profile,
function,
pcs,
"r",
white_patches,
white_patches_total,
)
if bt1886 or intent == "a":
cat = profile.guess_cat() or "Bradford"
for item in ti3_ref.DATA.values():
if pcs == "l":
X, Y, Z = colormath.Lab2XYZ(
item["LAB_L"], item["LAB_A"], item["LAB_B"]
)
else:
X, Y, Z = (
v / 100.0
for v in (item["XYZ_X"], item["XYZ_Y"], item["XYZ_Z"])
)
if bt1886:
X, Y, Z = bt1886.apply(X, Y, Z)
if intent == "a":
X, Y, Z = colormath.adapt(
X,
Y,
Z,
"D50",
list(profile.tags.wtpt.values()),
cat=cat,
)
X, Y, Z = [v * 100 for v in (X, Y, Z)]
if pcs == "l":
(
item["LAB_L"],
item["LAB_A"],
item["LAB_B"],
) = colormath.XYZ2Lab(X, Y, Z)
else:
item["XYZ_X"], item["XYZ_Y"], item["XYZ_Z"] = X, Y, Z
else:
if not primaries and check_missing_fields:
raise ValueError(
lang.getstr(
"error.testchart.missing_fields",
(cgats.filename, ", ".join(labels)),
)
)
ti1, ti3_ref = self.ti3_lookup_to_ti1(
cgats, profile, fields, intent, white_patches
)
except Exception as exception:
traceback.print_exc()
if raise_exceptions:
raise exception
if not isinstance(exception, (TypeError, ValueError)) or isinstance(
exception, UnicodeError
):
handle_error(exception, self.owner)
else:
show_result_dialog(exception, self.owner)
return ti1, ti3_ref, gray
def ti1_lookup_to_ti3(
self,
ti1,
profile,
function="f",
pcs=None,
intent="r",
white_patches=4,
white_patches_total=True,
):
"""Read TI1 (filename or CGATS instance), lookup device->pcs values
colorimetrically through profile using Argyll's xicclu
utility and return TI3 (CGATS instance)
"""
# ti1
if isinstance(ti1, str):
ti1 = CGATS(ti1)
if not isinstance(ti1, CGATS):
raise TypeError(
"Wrong type for ti1, needs to be a CGATS instance, "
f"not {t11.__class__.__name__}"
)
# profile
if isinstance(profile, str):
profile = ICCProfile(profile)
if not isinstance(profile, ICCProfile):
raise TypeError(
"Wrong type for profile, needs to be a ICCProfile instance, "
f"not {profile.__class__.__name__}"
)
# determine pcs for lookup
color_rep = profile.connectionColorSpace.upper()
if color_rep == b"RGB":
pcs = None
elif not pcs:
if color_rep == b"LAB":
pcs = "l"
elif color_rep == b"XYZ":
pcs = "x"
else:
raise ValueError("Unknown CIE color representation " + color_rep)
else:
if pcs == "l":
color_rep = b"LAB"
elif pcs == "x":
color_rep = b"XYZ"
# get profile color space
colorspace = profile.colorSpace
# required fields for ti1
if colorspace == b"CMYK":
required = ("CMYK_C", "CMYK_M", "CMYK_Y", "CMYK_K")
else:
required = ("RGB_R", "RGB_G", "RGB_B")
ti1_filename = ti1.filename
try:
ti1 = verify_cgats(ti1, required, True)
except CGATSInvalidError:
raise ValueError(lang.getstr("error.testchart.invalid", ti1_filename))
except CGATSKeyError:
raise ValueError(
lang.getstr(
"error.testchart.missing_fields",
(ti1_filename, ", ".join(required)),
)
)
# read device values from ti1
data = ti1.queryv1("DATA")
if not data:
raise ValueError(lang.getstr("error.testchart.invalid", ti1_filename))
device_data = data.queryv(required)
if not device_data:
raise ValueError(
lang.getstr(
"error.testchart.missing_fields",
(ti1_filename, ", ".join(required)),
)
)
if colorspace == b"RGB" and white_patches:
# make sure the first four patches are white so the whitepoint can be
# averaged
white_rgb = {"RGB_R": 100, "RGB_G": 100, "RGB_B": 100}
white = dict(white_rgb)
wp = ti1.queryv1("APPROX_WHITE_POINT")
if wp:
wp = [float(v) for v in wp.split()]
wp = [rpad((v / wp[1]) * 100.0, data.vmaxlen) for v in wp]
else:
wp = colormath.get_standard_illuminant("D65", scale=100)
for label in list(data.parent.DATA_FORMAT.values()):
label = label.decode("utf-8")
if label not in white:
if label.upper() == "LAB_L":
value = 100
elif label.upper() in ("LAB_A", "LAB_B"):
value = 0
elif label.upper() == "XYZ_X":
value = wp[0]
elif label.upper() == "XYZ_Y":
value = 100
elif label.upper() == "XYZ_Z":
value = wp[2]
else:
value = b"0"
white.update({label: value})
white_added_count = 0
if profile.profileClass != b"link":
if white_patches_total:
# Ensure total of n white patches
while len(data.queryi(white_rgb)) < white_patches:
data.insert(0, white)
white_added_count += 1
else:
# Add exactly n white patches
while white_added_count < white_patches:
data.insert(0, white)
white_added_count += 1
print("Added {:d} white patch(es)".format(white_added_count))
idata = []
for primaries in list(device_data.values()):
idata.append(list(primaries.values()))
if debug:
print(
"ti1_lookup_to_ti3 {} -> {} idata".format(profile.colorSpace, color_rep)
)
for v in idata:
print(" ".join(("{:3.4f}",) * len(v)).format(*v))
# lookup device->cie values through profile using (x)icclu
if pcs or self.argyll_version >= [1, 6]:
use_icclu = False
else:
# DeviceLink profile, we have to use icclu under older Argyll CMS
# versions because older xicclu cannot handle devicelink
use_icclu = True
input_encoding = None
output_encoding = None
if not pcs:
# Try to determine input/output encoding for devicelink
if isinstance(profile.tags.get("meta"), DictType):
input_encoding = profile.tags.meta.getvalue("encoding.input")
output_encoding = profile.tags.meta.getvalue("encoding.output")
if input_encoding == "T":
# 'T' (clip wtw on input) not supported for xicclu
input_encoding = "t"
# Fall back to configured 3D LUT encoding
if (
not input_encoding
or input_encoding not in config.valid_values["3dlut.encoding.input"]
):
input_encoding = getcfg("3dlut.encoding.input")
if input_encoding == "T":
# 'T' (clip wtw on input) not supported for xicclu
input_encoding = "t"
if (
not output_encoding
or output_encoding not in config.valid_values["3dlut.encoding.output"]
):
output_encoding = getcfg("3dlut.encoding.output")
if self.argyll_version < [1, 6] and not (
input_encoding == output_encoding == "n"
):
# Fail if encoding is not n (data levels)
raise ValueError(
"The used version of ArgyllCMS only"
"supports full range RGB encoding."
)
odata = self.xicclu(
profile,
idata,
intent,
function,
pcs=pcs,
scale=100,
use_icclu=use_icclu,
input_encoding=input_encoding,
output_encoding=output_encoding,
)
gray = []
igray = []
igray_idx = []
if colorspace == b"RGB":
# treat r=g=b specially: set expected a=b=0
for i, cie in enumerate(odata):
r, g, b = idata[i]
if r == g == b < 100:
# if grayscale and not white
if pcs == "x":
# Need to scale XYZ coming from xicclu
# Lab is already scaled
cie = colormath.XYZ2Lab(*[n * 100.0 for n in cie])
cie = (cie[0], 0, 0) # set a=b=0
igray.append("{} {} {}".format(*cie))
igray_idx.append(i)
if pcs == "x":
cie = colormath.Lab2XYZ(*cie)
luminance = cie[1]
else:
luminance = colormath.Lab2XYZ(*cie)[1]
if luminance * 100.0 >= 1:
# only add if luminance is greater or equal 1% because
# dark tones fluctuate too much
gray.append((r, g, b))
if False: # NEVER?
# set cie in odata to a=b=0
odata[i] = cie
if igray and False: # NEVER?
# lookup cie->device values for grays through profile using xicclu
gray = []
ogray = self.xicclu(profile, igray, "r", "b", pcs="l", scale=100)
for i, rgb in enumerate(ogray):
cie = idata[i]
if colormath.Lab2XYZ(cie[0], 0, 0)[1] * 100.0 >= 1:
# only add if luminance is greater or equal 1% because
# dark tones fluctuate too much
gray.append(rgb)
# update values in ti1 and data for ti3
for n, channel in enumerate(("R", "G", "B")):
data[igray_idx[i] + white_added_count]["RGB_" + channel] = rgb[n]
odata[igray_idx[i]] = cie
# write output ti3
ofile = BytesIO()
if pcs:
ofile.write(b"CTI3 \n")
ofile.write(
b'\nDESCRIPTOR "Argyll Calibration Target chart information 3"\n'
)
else:
ofile.write(b"CTI1 \n")
ofile.write(
b'\nDESCRIPTOR "Argyll Calibration Target chart information 1"\n'
)
ofile.write(b'KEYWORD "DEVICE_CLASS"\n')
ofile.write(
b'DEVICE_CLASS "'
+ (b"DISPLAY" if colorspace == b"RGB" else b"OUTPUT")
+ b'"\n'
)
include_sample_name = False
for i, cie in enumerate(odata):
if i == 0:
icolor = profile.colorSpace
if icolor == b"RGB":
olabel = b"RGB_R RGB_G RGB_B"
elif icolor == b"CMYK":
olabel = b"CMYK_C CMYK_M CMYK_Y CMYK_K"
else:
raise ValueError("Unknown color representation " + icolor)
if color_rep == b"LAB":
ilabel = b"LAB_L LAB_A LAB_B"
elif color_rep in (b"XYZ", b"RGB"):
ilabel = b"XYZ_X XYZ_Y XYZ_Z"
else:
raise ValueError("Unknown CIE color representation " + color_rep)
ofile.write(b'KEYWORD "COLOR_REP"\n')
if icolor == color_rep:
ofile.write(b'COLOR_REP "' + icolor + b'"\n')
else:
ofile.write(b'COLOR_REP "' + icolor + b"_" + color_rep + b'"\n')
ofile.write(b"\n")
ofile.write(b"NUMBER_OF_FIELDS ")
if include_sample_name:
ofile.write(
bytes(str(2 + len(icolor) + len(color_rep)), "utf-8") + b"\n"
)
else:
ofile.write(
bytes(str(1 + len(icolor) + len(color_rep)), "utf-8") + b"\n"
)
ofile.write(b"BEGIN_DATA_FORMAT\n")
ofile.write(b"SAMPLE_ID ")
if include_sample_name:
ofile.write(b"SAMPLE_NAME " + olabel + b" " + ilabel + b"\n")
else:
ofile.write(olabel + b" " + ilabel + b"\n")
ofile.write(b"END_DATA_FORMAT\n")
ofile.write(b"\n")
ofile.write(
b"NUMBER_OF_SETS " + bytes(str(len(odata)), "utf-8") + b"\n"
)
ofile.write(b"BEGIN_DATA\n")
if pcs == "x":
# Need to scale XYZ coming from xicclu, Lab is already scaled
cie = [round(n * 100.0, 5 - len(str(int(abs(n * 100.0))))) for n in cie]
elif not pcs:
# Actually CIE = RGB because Devicelink
idata[i] = cie
cie = [
round(n * 100.0, 5 - len(str(int(abs(n * 100.0)))))
for n in colormath.RGB2XYZ(*[n / 100.0 for n in cie])
]
device = [bytes(str(n), "utf-8") for n in idata[i]]
cie = [bytes(str(n), "utf-8") for n in cie]
if include_sample_name:
ofile.write(
bytes(str(i), "utf-8")
+ b" "
+ data[i - 1][1].strip(b'"')
+ b" "
+ b" ".join(device)
+ b" "
+ b" ".join(cie)
+ b"\n"
)
else:
ofile.write(
bytes(str(i), "utf-8")
+ b" "
+ b" ".join(device)
+ b" "
+ b" ".join(cie)
+ b"\n"
)
ofile.write(b"END_DATA\n")
ofile.seek(0)
ti3 = CGATS(ofile)[0]
if colorspace == b"RGB" and white_patches and profile.profileClass == b"link":
if white_patches_total:
# Ensure total of n white patches
while len(ti3.DATA.queryi(white_rgb)) < white_patches:
ti3.DATA.insert(0, white)
white_added_count += 1
else:
# Add exactly n white patches
while white_added_count < white_patches:
ti3.DATA.insert(0, white)
white_added_count += 1
print(f"Added {white_added_count:d} white patch(es)")
if debug:
print(f"ti3: {ti3}")
return ti1, ti3, list(map(list, gray))
def ti3_lookup_to_ti1(
self, ti3, profile, fields=None, intent="r", add_white_patches=4
):
"""Read TI3 (filename or CGATS instance), lookup cie->device values
colorimetrically through profile using Argyll's xicclu
utility and return TI1 and compatible TI3 (CGATS instances)
"""
# ti3
copy = True
if isinstance(ti3, (str, bytes)):
copy = False
ti3 = CGATS(ti3)
if not isinstance(ti3, CGATS):
raise TypeError(
"Wrong type for ti3, needs to be CGATS instance, "
f"not {ti3.__class__.__name__}"
)
ti3_filename = ti3.filename
if copy:
# Make a copy and do not alter a passed in CGATS instance!
ti3 = CGATS(bytes(ti3))
if fields == "XYZ":
labels = ("XYZ_X", "XYZ_Y", "XYZ_Z")
else:
labels = ("LAB_L", "LAB_A", "LAB_B")
try:
ti3v = verify_cgats(ti3, labels, True)
except CGATSInvalidError as exception:
raise ValueError(
lang.getstr("error.testchart.invalid", ti3_filename)
+ "\n"
+ lang.getstr(str(exception))
)
except CGATSKeyError:
try:
if fields:
raise
else:
labels = ("XYZ_X", "XYZ_Y", "XYZ_Z")
ti3v = verify_cgats(ti3, labels, True)
except CGATSKeyError:
missing = ", ".join(labels)
if not fields:
missing += " " + lang.getstr("or") + " LAB_L, LAB_A, LAB_B"
raise ValueError(
lang.getstr(
"error.testchart.missing_fields", (ti3_filename, missing)
)
)
else:
color_rep = "XYZ"
else:
color_rep = fields or "LAB"
# profile
if isinstance(profile, str):
profile = ICCProfile(profile)
if not isinstance(profile, ICCProfile):
raise TypeError(
"Wrong type for profile, needs to be ICCProfile instance, "
f"not {profile.__class__.__name__}"
)
# determine pcs for lookup
if color_rep == "LAB":
pcs = "l"
required = ("LAB_L", "LAB_A", "LAB_B")
elif color_rep == "XYZ":
pcs = "x"
required = ("XYZ_X", "XYZ_Y", "XYZ_Z")
else:
raise ValueError("Unknown CIE color representation " + color_rep)
# get profile color space
colorspace = profile.colorSpace
# read cie values from ti3
data = ti3v.queryv1("DATA")
if not data:
raise ValueError(lang.getstr("error.testchart.invalid", ti3_filename))
cie_data = data.queryv(required)
if not cie_data:
raise ValueError(
lang.getstr(
"error.testchart.missing_fields",
(ti3_filename, ", ".join(required)),
)
)
idata = []
if colorspace == "RGB" and add_white_patches:
# make sure the first four patches are white so the whitepoint can be
# averaged
wp = [n * 100.0 for n in list(profile.tags.wtpt.values())]
if color_rep == "LAB":
wp = colormath.XYZ2Lab(*wp)
wp = dict((("L", wp[0]), ("a", wp[1]), ("b", wp[2])))
else:
wp = dict((("X", wp[0]), ("Y", wp[1]), ("Z", wp[2])))
wp = [wp] * int(add_white_patches)
print(f"Added {add_white_patches:d} white patches")
else:
wp = []
for cie in wp + list(cie_data.values()):
cie = list(cie.values())
if color_rep == "XYZ":
# assume scale 0...100 in ti3, we need to convert to 0...1
cie = [n / 100.0 for n in cie]
idata.append(cie)
if debug:
print(f"ti3_lookup_to_ti1 {color_rep} -> {profile.colorSpace} idata")
for v in idata:
print(" ".join(("{:3.4f}",) * len(v)).format(*v))
# lookup cie->device values through profile.icc using xicclu
odata = self.xicclu(profile, idata, intent, "b", pcs=pcs, scale=100)
# write output ti1/ti3
ti1out = BytesIO()
ti1out.write(b"CTI1\n")
ti1out.write(b"\n")
ti1out.write(b'DESCRIPTOR "Argyll Calibration Target chart information 1"\n')
include_sample_name = False
for i, device in enumerate(odata):
if i == 0:
if color_rep == "LAB":
ilabel = b"LAB_L LAB_A LAB_B"
elif color_rep == "XYZ":
ilabel = b"XYZ_X XYZ_Y XYZ_Z"
else:
raise ValueError(f"Unknown CIE color representation {color_rep}")
ocolor = profile.colorSpace.upper()
if ocolor == b"RGB":
olabel = b"RGB_R RGB_G RGB_B"
elif ocolor == b"CMYK":
olabel = b"CMYK_C CMYK_M CMYK_Y CMYK_K"
else:
raise ValueError(f"Unknown color representation {ocolor}")
olabels = olabel.split()
# add device fields to DATA_FORMAT if not yet present
if (
olabels[0] not in list(ti3v.DATA_FORMAT.values())
and olabels[1] not in list(ti3v.DATA_FORMAT.values())
and not olabels[2] in list(ti3v.DATA_FORMAT.values())
and (
ocolor == b"RGB"
or (
ocolor == b"CMYK"
and not olabels[3] in list(ti3v.DATA_FORMAT.values())
)
)
):
ti3v.DATA_FORMAT.add_data(olabels)
# add required fields to DATA_FORMAT if not yet present
if (
not bytes(required[0], "utf-8") in list(ti3v.DATA_FORMAT.values())
and not bytes(required[1], "utf-8")
in list(ti3v.DATA_FORMAT.values())
and not bytes(required[2], "utf-8")
in list(ti3v.DATA_FORMAT.values())
):
ti3v.DATA_FORMAT.add_data(required)
ti1out.write(b'KEYWORD "COLOR_REP"\n')
ti1out.write(b'COLOR_REP "%s"\n' % ocolor)
ti1out.write(b"\n")
ti1out.write(b"NUMBER_OF_FIELDS ")
if include_sample_name:
ti1out.write(
bytes(str(2 + len(color_rep) + len(ocolor)), "utf-8") + b"\n"
)
else:
ti1out.write(
bytes(str(1 + len(color_rep) + len(ocolor)), "utf-8") + b"\n"
)
ti1out.write(b"BEGIN_DATA_FORMAT\n")
ti1out.write(b"SAMPLE_ID ")
if include_sample_name:
ti1out.write(b"SAMPLE_NAME %s %s\n" % (olabel, ilabel))
else:
ti1out.write(b"%s %s\n" % (olabel, ilabel))
ti1out.write(b"END_DATA_FORMAT\n")
ti1out.write(b"\n")
ti1out.write(b"NUMBER_OF_SETS %s\n" % bytes(str(len(odata)), "utf-8"))
ti1out.write(b"BEGIN_DATA\n")
if i < len(wp):
if ocolor == b"RGB":
device = [100.00, 100.00, 100.00]
else:
device = [0, 0, 0, 0]
# Make sure device values do not exceed valid range of 0..100
device = [bytes(str(max(0, min(v, 100))), "utf-8") for v in device]
cie = list((wp + list(cie_data.values()))[i].values())
cie = [bytes(str(n), "utf-8") for n in cie]
if include_sample_name:
cie_ = b"%s %s %s %s\n" % (
bytes(str(i + 1)),
data[i][1].strip(b'"'),
b" ".join(device),
b" ".join(cie),
)
ti1out.write(cie_)
else:
cie_ = b"%s %s %s\n" % (
bytes(str(i + 1), "utf-8"),
b" ".join(device),
b" ".join(cie),
)
ti1out.write(cie_)
if i > len(wp) - 1: # don't include whitepoint patches in ti3
# set device values in ti3
for n, v in enumerate(olabels):
# Assuming 0..100, 4 decimal digits is
# enough for roughly 19 bits integer
# device values
ti3v.DATA[i - len(wp)][v.decode("utf-8")] = round(
float(device[n]), 4
)
# set PCS values in ti3
for n, v in enumerate(cie):
ti3v.DATA[i - len(wp)][required[n]] = float(v)
ti1out.write(b"END_DATA\n")
ti1out.seek(0)
ti1 = CGATS(ti1out)
if debug:
print(ti1)
return ti1, ti3v
def download(self, uri, force=False, download_dir=None):
# Set timeout to a sane value
default_timeout = socket.getdefaulttimeout()
socket.setdefaulttimeout(20) # 20 seconds
try:
return self._download(uri, force=force, download_dir=download_dir)
finally:
socket.setdefaulttimeout(default_timeout)
def _download(self, uri, force=False, download_dir=None):
if test_badssl:
uri = f"https://{test_badssl}.badssl.com/"
orig_uri = uri
total_size = None
filename = os.path.basename(uri)
if not download_dir:
download_dir = os.path.join(config.datahome, "dl")
download_path = os.path.join(download_dir, filename)
response = None
hashes = None
is_main_dl = uri.startswith(f"https://{DOMAIN}/download/")
if is_main_dl:
# Always force connection to server even if local file exists for
# displaycal.net/downloads/* and displaycal.net/Argyll/*
# to force a hash check
force = True
if force or not os.path.isfile(download_path):
cafile = None
try:
import ssl
from ssl import CertificateError, SSLError
except ImportError:
CertificateError = ValueError
SSLError = socket.error
else:
if hasattr(ssl, "get_default_verify_paths"):
cafile = ssl.get_default_verify_paths().cafile
if cafile:
print("Using CA file", cafile)
components = urllib.parse.urlparse(uri)
# Don't use socket.getservbyname, as it apparently can fail with
# socket.error under Windows 10 (although why remains a mystery,
# we're only dealing with HTTP and HTTPS protocols...)
scheme2port = {"http": 80, "https": 443}
print(
lang.getstr(
"connecting.to",
(
f"{components.scheme}://{components.hostname}",
components.port or scheme2port.get(components.scheme, "?"),
),
)
)
LoggingHTTPRedirectHandler.newurl = uri
opener = urllib.request.build_opener(LoggingHTTPRedirectHandler)
opener.addheaders = list(get_default_headers().items())
try:
response = opener.open(uri)
if always_fail_download or test_badssl:
raise urllib.error.URLError("")
newurl = getattr(LoggingHTTPRedirectHandler, "newurl", uri)
if is_main_dl or not newurl.startswith(f"https://{DOMAIN}/"):
# Get SHA-256 hashes so we can verify the downloaded file.
# Only do this for 3rd party hosts/mirrors (no sense
# doing it for files downloaded securely directly from
# displaycal.net when that is also the source of our hashes
# file, unless we are verifying an existing local app setup
# or portable archive)
noredir = urllib.request.build_opener(NoHTTPRedirectHandler)
noredir.addheaders = list(get_default_headers().items())
hashes = noredir.open(f"https://{DOMAIN}/sha256sums.txt")
except (
socket.error,
urllib.error.URLError,
http.client.HTTPException,
CertificateError,
SSLError,
) as exception:
if response:
response.close()
if "CERTIFICATE_VERIFY_FAILED" in safe_str(exception):
ekey = "ssl.certificate_verify_failed"
if not cafile and isapp:
ekey += ".root_ca_missing"
print(lang.getstr(ekey))
elif "SSLV3_ALERT_HANDSHAKE_FAILURE" in safe_str(exception):
print(lang.getstr("ssl.handshake_failure"))
elif "SSL:" not in safe_str(exception):
return exception
else:
print(exception)
if getattr(LoggingHTTPRedirectHandler, "newurl", uri) != uri:
uri = LoggingHTTPRedirectHandler.newurl
return DownloadError(
lang.getstr("download.fail")
+ " "
+ lang.getstr("connection.fail", uri),
orig_uri,
)
uri = response.geturl()
filename = Path(Path(uri).name)
actualhash = sha256()
if hashes and False: # skip this for now
# Read max. 64 KB hashes
hashesdata = hashes.read(1024 * 64)
hashes.close()
hashesdict = {}
for line in hashesdata.splitlines():
if not line.strip():
continue
name_hash = [value.strip() for value in line.split(None, 1)]
if len(name_hash) != 2 or "" in name_hash:
response.close()
return DownloadError(
lang.getstr("file.hash.malformed", filename), orig_uri
)
else:
hashesdict[Path(name_hash[1].decode().lstrip("*"))] = name_hash[
0
]
expectedhash_hex = hashesdict[filename]
if not expectedhash_hex:
response.close()
return DownloadError(
lang.getstr("file.hash.missing", filename), orig_uri
)
total_size = response.info().get("Content-Length")
if total_size is not None:
try:
total_size = int(total_size)
except (TypeError, ValueError):
return DownloadError(
lang.getstr(
"download.fail.wrong_size",
("<{}>".format(lang.getstr("unknown")),) * 2,
),
orig_uri,
)
else:
if not total_size:
total_size = None
contentdispo = response.info().get("Content-Disposition")
if contentdispo:
filename = re.search(r'filename="([^"]+)"', contentdispo)
if filename:
filename = filename.groups()[0]
if not filename:
filename = make_filename_safe(uri.rstrip("/"), concat=False)
content_type = response.info().get("Content-Type")
if content_type:
content_type = content_type.split(";", 1)[0]
ext = mimetypes.guess_extension(content_type or "", False)
filename += ext or ".html"
download_path = os.path.join(download_dir, filename)
if not os.path.isfile(download_path) or (
total_size is not None and os.stat(download_path).st_size != total_size
):
if not os.path.isdir(download_dir):
os.makedirs(download_dir)
# Acquire files safely so no-one but us can mess with them
fd = tmp_fd = tmp_download_path = None
try:
fd, download_path = mksfile(download_path)
tmp_fd, tmp_download_path = mksfile(download_path + ".download")
except EnvironmentError as mksfile_exception:
response.close()
for fd, pth in [(fd, download_path), (tmp_fd, tmp_download_path)]:
if fd:
os.close(fd)
try:
os.remove(download_path)
except EnvironmentError as exception:
print(exception)
return mksfile_exception
print(lang.getstr("downloading"), uri, "\u2192", download_path)
self.recent.write(lang.getstr(f"downloading {filename}\n"))
min_chunk_size = 1024 * 8
chunk_size = min_chunk_size
bytes_so_far = 0
prev_bytes_so_far = 0
unit = "Bytes"
unit_size = 1.0
if total_size > 1048576:
unit = "MiB"
unit_size = 1048576.0
elif total_size > 1024:
unit = "KiB"
unit_size = 1024.0
ts = time()
bps = 0
prev_percent = -1
update_ts = time()
fps = 20
frametime = 1.0 / fps
download_file = None
download_file_exception = None
try:
with os.fdopen(tmp_fd, "rb+") as tmp_download_file:
while True:
if self.thread_abort:
print(lang.getstr("aborted"))
return False
chunk = response.read(chunk_size)
if not chunk:
break
bytes_read = len(chunk)
bytes_so_far += bytes_read
tmp_download_file.write(chunk)
# Determine data rate
tdiff = time() - ts
if tdiff >= 1:
bps = (bytes_so_far - prev_bytes_so_far) / tdiff
prev_bytes_so_far = bytes_so_far
ts = time()
elif not bps:
if tdiff:
bps = bytes_so_far / tdiff
else:
bps = bytes_read
bps_unit = "Bytes"
bps_unit_size = 1.0
if bps > 1048576:
bps_unit = "MiB"
bps_unit_size = 1048576.0
elif bps > 1024:
bps_unit = "KiB"
bps_unit_size = 1024.0
if total_size:
percent = int(
math.floor(float(bytes_so_far) / total_size * 100)
)
if (
percent > prev_percent
or time() >= update_ts + frametime
):
self.lastmsg.write(
"\r{:d}% ({:.1f} / {:.1f} {}, {:.2f} {}/s)".format(
percent,
bytes_so_far / unit_size,
total_size / unit_size,
unit,
round(bps / bps_unit_size, 2),
bps_unit,
)
)
prev_percent = percent
update_ts = time()
elif time() >= update_ts + frametime:
if bytes_so_far > 1048576 > unit_size:
unit = "MiB"
unit_size = 1048576.0
elif bytes_so_far > 1024 > unit_size:
unit = "KiB"
unit_size = 1024.0
self.lastmsg.write(
"\r{:.1f} {} ({:.2f} {}/s)".format(
bytes_so_far / unit_size,
unit,
round(bps / bps_unit_size, 2),
bps_unit,
)
)
update_ts = time()
# Adjust chunk size based on DL rate
if int(bps / fps) > chunk_size or min_chunk_size <= int(
bps / fps
) < int(chunk_size * 0.75):
if debug or test or verbose > 1:
print(
"Download buffer size changed from {:0.0f} KB to {:0.0f} KB".format(
chunk_size / 1024.0, bps / fps / 1024
)
)
chunk_size = int(bps / fps)
if not bytes_so_far:
return DownloadError(
lang.getstr("download.fail.empty_response", uri), orig_uri
)
if total_size is not None and bytes_so_far != total_size:
return DownloadError(
lang.getstr(
"download.fail.wrong_size", (total_size, bytes_so_far)
),
orig_uri,
)
if total_size is None or bytes_so_far == total_size:
# Succesful download, write to destination
tmp_download_file.seek(0)
try:
with os.fdopen(fd, "wb") as download_file:
while True:
chunk = tmp_download_file.read(1024 * 1024)
if not chunk:
break
download_file.write(chunk)
if hashes:
actualhash.update(chunk)
except EnvironmentError as download_file_exception:
return download_file_exception
print(lang.getstr("success"))
finally:
response.close()
if not download_file_exception:
# Remove temporary download file unless there was an error
# writing destination
try:
os.remove(tmp_download_path)
except EnvironmentError as exception:
print(exception)
if self.thread_abort or download_file_exception:
# Remove destination file if download aborted or error
# writing destination \
if not download_file:
# Need to close file descriptor first
os.close(fd)
try:
os.remove(download_path)
except EnvironmentError as exception:
print(exception)
else:
# File already exists
if response:
response.close()
if hashes:
# Verify hash. Get hash of existing file
with open(download_path, "rb") as download_file:
while True:
chunk = download_file.read(1024 * 1024)
if not chunk:
break
actualhash.update(chunk)
if hashes and False: # skip this for now
# Verify hash. Compare to expected hash
actualhash_hex = actualhash.hexdigest()
if actualhash_hex != expectedhash_hex.decode():
return Error(
lang.getstr(
"file.hash.verification.fail",
(
download_path,
f"SHA-256= {expectedhash_hex.decode()}",
f"SHA-256= {actualhash_hex}",
),
)
)
else:
print(f"Verified hash SHA-256= {actualhash_hex}")
return download_path
def process_argyll_download(self, result, exit=False):
if isinstance(result, Exception):
show_result_dialog(result, self.owner)
elif result:
if result.lower().endswith(".zip") or result.lower().endswith(".tgz"):
self.start(
self.set_argyll_bin,
self.extract_archive,
cargs=(result,),
wargs=(result,),
progress_msg=lang.getstr("please_wait"),
fancy=False,
)
else:
show_result_dialog(
f"{lang.getstr('error.file_type_unsupported')}\n{result}",
self.owner,
)
def extract_archive(self, filename):
extracted = []
if filename.lower().endswith(".zip"):
cls = zipfile.ZipFile
mode = "r"
elif filename.lower().endswith(".tgz"):
cls = TarFileProper.open # classmethod
mode = "r:gz"
else:
return extracted
with cls(filename, mode) as z:
outdir = os.path.realpath(os.path.dirname(filename))
if cls is not zipfile.ZipFile:
method = z.getnames
else:
method = z.namelist
try:
names = method()
except EOFError as e:
# probably didn't download properly,
# delete the file and let the user download again
print(f"Got EOFError, deleting the current downloaded file: {filename}")
os.remove(filename)
raise e
names.sort()
extracted = [os.path.join(outdir, os.path.normpath(name)) for name in names]
if names:
name0 = os.path.normpath(names[0])
for outpath in extracted:
if not os.path.realpath(outpath).startswith(
os.path.join(outdir, name0)
):
return Error(lang.getstr("file.invalid") + "\n" + filename)
if cls is not zipfile.ZipFile:
z.extractall(outdir)
else:
for name in names:
# If the ZIP file was created with Unicode names stored
# in the file, 'name' will already be Unicode.
# Otherwise, it'll either be 7-bit ASCII or (legacy)
# cp437 encoding
outname = name
outpath = os.path.join(outdir, os.path.normpath(outname))
if outname.endswith("/"):
if not os.path.isdir(outpath):
os.makedirs(outpath)
elif not os.path.isfile(outpath):
with open(outpath, "wb") as outfile:
outfile.write(z.read(name))
return extracted
def set_argyll_bin(self, result, filename):
"""Set Argyll bin directory."""
if isinstance(result, Exception):
show_result_dialog(result, self.owner)
elif result and os.path.isdir(result[0]):
setcfg("argyll.dir", os.path.join(result[0], "bin"))
# Always write cfg directly after setting Argyll directory so
# subprocesses that read the configuration will use the right
# executables
writecfg()
from DisplayCAL.display_cal import check_donation
snapshot = VERSION > VERSION_BASE
self.owner.set_argyll_bin_handler(
None,
True,
self.owner.check_instrument_setup,
(check_donation, (self.owner, snapshot)),
)
else:
show_result_dialog(
lang.getstr("error.no_files_extracted_from_archive", filename),
self.owner,
)
def process_download(self, result, exit=False):
if isinstance(result, Exception):
show_result_dialog(result, self.owner)
elif result:
if exit:
if self.owner:
self.owner.Close()
else:
wx.GetApp().ExitMainLoop()
launch_file(result)
def verify_calibration(self):
"""Verify the current calibration"""
result = self.detect_video_levels()
if isinstance(result, Exception) or not result:
return result
cmd, args = self.prepare_dispcal(calibrate=False, verify=True)
if not isinstance(cmd, Exception):
result = self.exec_cmd(cmd, args, capture_output=True, skip_scripts=True)
else:
result = cmd
return result
def measure_ti1(
self, ti1_path, cal_path=None, colormanaged=False, allow_video_levels=True
):
"""Measure a TI1 testchart file"""
if allow_video_levels:
result = self.detect_video_levels()
if isinstance(result, Exception) or not result:
return result
if config.get_display_name() == "Untethered":
cmd, args = get_argyll_util("spotread"), ["-v", "-e"]
if getcfg("extra_args.spotread").strip():
args += parse_argument_string(getcfg("extra_args.spotread"))
result = self.set_terminal_cgats(ti1_path)
if isinstance(result, Exception):
return result
else:
cmd = get_argyll_util("dispread")
args = ["-v"]
if getcfg("argyll.debug"):
args.append("-D8")
if config.get_display_name() in ("madVR", "Prisma") and colormanaged:
args.append("-V")
if cal_path:
if self.argyll_version >= [1, 3, 3] and (
not self.has_lut_access() or not getcfg("calibration.use_video_lut")
):
args.append("-K")
else:
args.append("-k")
args.append(cal_path)
if getcfg("extra_args.dispread").strip():
args += parse_argument_string(getcfg("extra_args.dispread"))
result = self.add_measurement_features(
quote_args(args) if sys.platform == "win32" else args,
cmd == get_argyll_util("dispread"),
allow_nondefault_observer=is_ccxx_testchart(),
allow_video_levels=allow_video_levels,
quantize=True,
)
if isinstance(result, Exception):
return result
self.options_dispread = list(args)
if config.get_display_name() != "Untethered":
args.append(os.path.splitext(ti1_path)[0])
return self.exec_cmd(cmd, args, skip_scripts=True)
def wrapup(self, copy=True, remove=True, dst_path=None, ext_filter=None):
"""Wrap up - copy and/or clean temporary file(s)."""
if debug:
print(f"[D] wrapup(copy={copy}, remove={remove})")
if not self.tempdir or not os.path.isdir(self.tempdir):
return # nothing to do
if (
isinstance(copy, Exception)
and not isinstance(copy, (UnloggedError, UnloggedInfo, UnloggedWarning))
and self.sessionlogfile
):
# This is an incomplete run, log exception to session logfile
self.sessionlogfile.write(safe_basestring(copy))
while self.sessionlogfiles:
self.sessionlogfiles.popitem()[1].close()
self.sessionlogfile = None
if isinstance(copy, Exception):
# This is an incomplete run, check if any files have been added or
# modified (except log files and 'hidden' temporary files)
changes = False
for filename in os.listdir(self.tempdir):
if filename.endswith(".log") or filename in (
".abort",
".ok",
".wait",
".wait.cmd",
".wait.py",
):
# Skip log files and 'hidden' temporary files
continue
if (
filename not in self.tmpfiles
or os.stat(os.path.join(self.tempdir, filename)).st_mtime
!= self.tmpfiles[filename]
):
changes = True
break
if not changes:
copy = False
result = True
if copy:
try:
src_listdir = os.listdir(self.tempdir)
except Exception as exception:
result = exception
remove = False
if not isinstance(result, Exception) and src_listdir:
if not ext_filter:
ext_filter = [
".app",
".cal",
".ccmx",
".ccss",
".cmd",
".command",
".gam",
".gz",
".icc",
".icm",
".log",
".png",
".sh",
".ti1",
".ti3",
".wrl",
".wrz",
]
save_path = getcfg("profile.save_path")
if dst_path is None:
dst_path = os.path.join(
save_path,
getcfg("profile.name.expanded"),
getcfg("profile.name.expanded") + ".ext",
)
if isinstance(copy, Exception):
# This is an incomplete run
parts = os.path.normpath(dst_path).split(os.sep)
# DON'T use os.path.join due to how it works under Windows:
# os.path.join("c:", "foo") represents a path relative to
# the current directory on drive C: (c:foo), not c:\foo.
# Save incomplete runs to different directory
parts = [config.datahome, "incomplete"] + parts[-2:]
dst_path = os.sep.join(parts)
result = check_create_dir(os.path.dirname(dst_path))
if isinstance(result, Exception):
remove = False
else:
if isinstance(copy, Exception):
print(
"Moving files of incomplete run to",
os.path.dirname(dst_path),
)
for basename in src_listdir:
name, ext = os.path.splitext(basename)
if ext_filter is None or ext.lower() in ext_filter:
src = os.path.join(self.tempdir, basename)
dst = os.path.join(os.path.dirname(dst_path), basename)
if sys.platform == "win32":
dst = make_win32_compatible_long_path(dst)
if os.path.exists(dst):
if os.path.isdir(dst):
if verbose >= 2:
print(
f"{appname}: Removing existing destination "
"directory tree",
dst,
)
try:
shutil.rmtree(dst, True)
except Exception as exception:
print(
f"Warning - directory '{dst}' could not be removed: {exception}"
)
else:
if verbose >= 2:
print(
f"{appname}: Removing existing destination "
"file",
dst,
)
try:
os.remove(dst)
except Exception as exception:
print(
f"Warning - file '{dst}' could not be removed: {exception}"
)
if remove:
if verbose >= 2:
print(
f"{appname}: Moving temporary object "
f"{src} to {dst}"
)
try:
shutil.move(src, dst)
except Exception as exception:
print(
f"Warning - temporary object '{src}' could not "
f"be moved to '{dst}': {exception}"
)
try:
shutil.copyfile(src, dst)
except Exception as exception:
result2 = Error(
lang.getstr("error.copy_failed", (src, dst))
+ "\n"
+ str(exception)
)
if isinstance(result, Exception):
result = "\n".join(
str(error)
for error in (result, result2)
)
else:
result = result2
remove = False
else:
if os.path.isdir(src):
if verbose >= 2:
print(
f"{appname}: Copying temporary "
f"directory tree {src} to {dst}"
)
try:
shutil.copytree(src, dst)
except Exception as exception:
print(
f"Warning - temporary directory '{src}' "
"could not be copied to "
f"'{dst}': {exception}"
)
else:
if verbose >= 2:
print(
f"{appname}: Copying temporary "
f"file {src} to {dst}"
)
try:
shutil.copyfile(src, dst)
except Exception as exception:
print(
f"Warning - temporary file '{src}' could "
f"not be copied to '{dst}': {exception}"
)
if remove:
try:
src_listdir = os.listdir(self.tempdir)
except Exception as exception:
print(f"Error - directory '{self.tempdir}' listing failed: {exception}")
else:
for basename in src_listdir:
name, ext = os.path.splitext(basename)
if ext_filter is None or ext.lower() not in ext_filter:
src = os.path.join(self.tempdir, basename)
isdir = os.path.isdir(src)
if isdir:
if verbose >= 2:
print(
f"{appname}: Removing temporary directory tree",
src,
)
try:
shutil.rmtree(src, True)
except Exception as exception:
print(
f"Warning - temporary directory '{src}' could not "
f"be removed: {exception}"
)
else:
if verbose >= 2:
print(f"{appname}: Removing temporary file", src)
try:
os.remove(src)
except Exception as exception:
print(
f"Warning - temporary file '{src}' could not be "
f"removed: {exception}"
)
try:
src_listdir = os.listdir(self.tempdir)
except Exception as exception:
print(f"Error - directory '{self.tempdir}' listing failed: {exception}")
else:
if not src_listdir:
if verbose >= 2:
print(
f"{appname}: Removing empty temporary directory",
self.tempdir,
)
try:
shutil.rmtree(self.tempdir, True)
except Exception as exception:
print(
f"Warning - temporary directory '{self.tempdir}' could "
f"not be removed: {exception}"
)
if isinstance(result, Exception):
result = Error(
"{}\n\n{}".format(
result,
lang.getstr("tempdir_should_still_contain_files", self.tempdir),
)
)
return result
def write(self, txt):
if True:
# Don't buffer
self._write(txt)
return
# NEVER - Use line buffer
self.buffer.append(txt)
self.buffer = [line for line in StringIO("".join(self.buffer))]
for line in self.buffer:
if not (
line.endswith("\n")
or line.rstrip().endswith(":")
or line.rstrip().endswith(",")
or line.rstrip().endswith(".")
):
break
self._write(line)
self.buffer = self.buffer[1:]
def _write(self, txt):
if isinstance(txt, bytes):
txt = txt.decode()
wx.CallAfter(self.audio_visual_feedback, txt)
if getattr(self, "measure_cmd", None):
# i1 Pro, Spyders: Instrument Type
# i1D3: Product Name
# K10: Model
# specbos: Identification
instrument = re.search(
r"(?:Instrument Type|Product Name|Model|Identificaton):\s+([^\r\n]+)",
txt,
re.I,
)
if instrument:
self._detected_instrument = instrument.group(1)
serial = re.search(r"(?:Serial Number):\s+([^\r\n]+)", txt, re.I)
if serial:
self._detected_instrument_serial = serial.group(1)
if re.search(r"press 1|space when done|patch 1 of ", txt, re.I):
# There are some intial measurements which we can't check for
# unless -D (debug) is used for Argyll tools
if "patch 1 of " not in txt.lower() or not self.patch_sequence:
if "patch 1 of " in txt.lower():
self.patch_sequence = True
self.patch_count = 0
self.patterngenerator_sent_count = 0
update = re.search(
r"[/\\] current|patch \d+ of |the instrument can be removed from the screen",
txt,
re.I,
)
# Send colors to pattern generator
use_patterngenerator = (
self.use_patterngenerator
and self.patterngenerator
and hasattr(self.patterngenerator, "conn")
)
if use_patterngenerator or self.use_madnet_tpg or self._use_patternwindow:
# sometimes the data is not fully received
if self.partial_data != "":
# combine the partial data with the currently received one
self.log(f"Combining previous data of: {self.partial_data}")
self.log(f"with : {txt}")
txt = self.partial_data + txt
self.log(f"to : {txt}")
rgb = re.search(r"Current RGB(?:\s+\d+){3}((?:\s+\d+(?:\.\d+)){3})", txt)
# Check if the data is partial
if "Current RGB" in txt and rgb is None:
self.log(f"Data is not fully received, storing partial data: {txt}")
self.partial_data = txt
if rgb:
# reset the partial data storage, as we fully received the data
self.partial_data = ""
update_ffp_insertion_ts = False
if (
getcfg("patterngenerator.ffp_insertion")
and self.patterngenerator_sent_count > 1
):
# Frame insertion
frq = getcfg("patterngenerator.ffp_insertion.interval")
if time() - getattr(self, "_ffp_insertion_ts", 0) > frq:
dur = getcfg("patterngenerator.ffp_insertion.duration")
lvl = getcfg("patterngenerator.ffp_insertion.level")
self.log(
f"{appname}: Frame insertion duration {dur:d}s, "
f"level = {lvl * 100:d}%"
)
ts = time()
patternconfig = []
if self.use_madnet_tpg:
patternconfig = self.madtpg.get_pattern_config()
self.madtpg.set_pattern_config(
patternconfig[0], int(lvl * 100), 0, 0
)
self.madtpg.show_rgb(lvl, lvl, lvl)
self.madtpg.set_pattern_config(100, 0, 0, 0)
else:
self.patterngenerator_send((lvl, lvl, lvl), (lvl, lvl, lvl))
while time() - ts < dur and not (
self.subprocess_abort or self.thread_abort
):
sleep(0.05)
if self.use_madnet_tpg:
self.madtpg.set_pattern_config(*patternconfig)
update_ffp_insertion_ts = True
if (
not hasattr(self, "_ffp_insertion_ts")
or update_ffp_insertion_ts
):
self._ffp_insertion_ts = time()
rgb = [float(v) for v in rgb.groups()[0].strip().split()]
if self.use_madnet_tpg:
if self.madtpg.show_rgb(*rgb):
self.patterngenerator_sent_count += 1
self.log(
f"{appname}: MadTPG_Net sent count: "
f"{self.patterngenerator_sent_count}"
)
else:
self.exec_cmd_returnvalue = Error(
lang.getstr("patterngenerator.sync_lost")
)
self.abort_subprocess()
else:
self.patterngenerator_send(rgb)
if getcfg("patterngenerator.ffp_insertion") and update_ffp_insertion_ts:
# Delay to allow patch update and settle time after
# frame insertion. If display update delay is bigger,
# do not use extra delay. Otherwise, subtract display
# update delay from fixed delay.
if getcfg("measure.override_min_display_update_delay_ms"):
dur = getcfg("measure.min_display_update_delay_ms") / 1000.0
else:
dur = 0
ts = time()
while time() - ts < max(0.8 - dur, 0) and not (
self.subprocess_abort or self.thread_abort
):
sleep(0.05)
# Create .ok file which will be picked up by .wait script
okfilename = os.path.join(self.tempdir, ".ok")
open(okfilename, "w").close()
if update:
# Check if patch count is higher than patterngenerator sent count
if (
self.patch_count > self.patterngenerator_sent_count
and self.exec_cmd_returnvalue is None
):
# XXX: This can happen when pausing/unpausing?
# Need to investigate
self.log("Warning - did we loose sync with the pattern generator?")
# self.exec_cmd_returnvalue = Error(lang.getstr("patterngenerator.sync_lost"))
# self.abort_subprocess()
if update and not (
self.subprocess_abort
or self.thread_abort
or "the instrument can be removed from the screen" in txt.lower()
):
self.patch_count += 1
if use_patterngenerator or self.use_madnet_tpg:
self.log(f"{appname}: Patch update count: {self.patch_count:d}")
if self.use_madnet_tpg:
progress = re.search(
r"(?:Patch (\d+) of|Number of patches =) (\d+)", txt, re.I
)
if progress:
# Set madTPG progress bar
try:
start = int(progress.group(1) or 0)
end = int(progress.group(2))
except ValueError:
pass
else:
self.madtpg.set_progress_bar_pos(start, end)
# Parse
wx.CallAfter(self.parse, txt)
@property
def _use_patternwindow(self):
# Preliminary Wayland support. This still needs a lot
# of work as Argyll doesn't support Wayland natively yet,
# so we use virtual display to drive our own patch window.
return (
not config.is_virtual_display()
and (
# os.getenv("XDG_SESSION_TYPE") == "wayland"
getcfg("patterngenerator.use_pattern_window")
)
and self.argyll_virtual_display
)
|