"""Core functionality for display calibration and profiling workflows. It includes classes and methods for interacting with hardware instruments, handling pattern generators, managing profiles, and executing Argyll CMS utilities. The module also supports various platform-specific features and integrates with external tools like madVR and colord. """ # stdlib from __future__ import annotations import atexit import codecs import contextlib import ctypes import datetime import functools import getpass import http.client import math import mimetypes import operator import os import platform import random import re import shutil import socket import string import struct import subprocess as sp import sys import tempfile import textwrap import threading import traceback import urllib.error import urllib.parse import urllib.request import warnings import zipfile import zlib from binascii import hexlify from collections import UserString from functools import partial from hashlib import md5, sha256 from io import BytesIO from pathlib import Path from threading import current_thread, main_thread from time import sleep, strftime, time from typing import Any, Callable, ClassVar import distro from send2trash import send2trash if sys.platform == "darwin": from _thread import start_new_thread elif sys.platform == "win32": from ctypes import windll else: import grp # 3rd party if sys.platform == "win32": import pythoncom import pywintypes import win32api import win32con import win32event import winerror from win32com.shell import shell as win32com_shell # custom from DisplayCAL import audio, colormath, config, defaultpaths, imfile, madvr, wexpect from DisplayCAL import localization as lang 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, ) from DisplayCAL.argyll_instruments import ( instruments as all_instruments, ) from DisplayCAL.argyll_names import ( INTENTS, OBSERVERS, VIEWCONDS, ) from DisplayCAL.cgats import ( CGATS, CGATSError, CGATSInvalidError, CGATSInvalidOperationError, CGATSKeyError, CGATSTypeError, CGATSValueError, rpad, sort_by_rec709_luma, sort_by_rgb, sort_by_rgb_sum, stable_sort_by_l, ) from DisplayCAL.colormath import VidRGB_to_eeColor, eeColor_to_VidRGB from DisplayCAL.config import ( APPBASENAME, AUTOSTART, AUTOSTART_HOME, DEFAULTS, ENC, EXE, EXE_EXT, EXEDIR, FS_ENC, ISAPP, PROFILE_EXT, PYDIR, SCRIPT_EXT, get_data_path, get_icon, get_total_patches, getcfg, is_ccxx_testchart, setcfg, setcfg_cond, split_display_name, writecfg, ) from DisplayCAL.debughelpers import ( DownloadError, Error, Info, UnloggedError, UnloggedInfo, UnloggedWarning, UntracedError, Warn, handle_error, ) from DisplayCAL.edid import WMIError, get_display_name_from_system_profiler, get_edid from DisplayCAL.icc_profile import ( GAMUT_VOLUME_SRGB, ChromaticAdaptionTag, ChromaticityType, CRInterpolation, CurveType, DictType, ICCProfile, ICCProfileInvalidError, ICCProfileTag, LUT16Type, MultiLocalizedUnicodeType, ProfileSequenceDescType, Text, TextDescriptionType, TextType, VideoCardGammaTableType, VideoCardGammaType, XYZType, _mp_apply, create_RGB_A2B_XYZ, create_synthetic_hdr_clut_profile, get_display_profile, s15Fixed16Number, s15Fixed16Number_tohex, set_display_profile, ) from DisplayCAL.log import LOG, DummyLogger, LogFile, get_file_logger from DisplayCAL.meta import DOMAIN, VERSION_STRING from DisplayCAL.meta import NAME as APPNAME from DisplayCAL.multiprocess import cpu_count, pool_slice from DisplayCAL.network import LoggingHTTPRedirectHandler, NoHTTPRedirectHandler from DisplayCAL.options import ( ALWAYS_FAIL_DOWNLOAD, DEBUG, EECOLOR65, EXPERIMENTAL, TEST, TEST_BADSSL, TEST_REQUIRE_SENSOR_CAL, VERBOSE, ) from DisplayCAL.patterngenerators import ( PrismaPatternGeneratorClient, ResolveCMPatternGeneratorServer, ResolveLSPatternGeneratorServer, 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, TarFileProper, ) from DisplayCAL.util_io import StringIOu as StringIO from DisplayCAL.util_list import intlist, natsort if sys.platform == "darwin": from DisplayCAL.util_mac import ( get_model_id, mac_app_activate, mac_terminal_do_script, mac_terminal_set_colors, osascript, ) 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 ( BUSTYPE_SESSION, DBusException, DBusObject, 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, 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_asciize, safe_basestring, safe_str, strtr, universal_newlines, ) from DisplayCAL.worker_base import ( WorkerBase, Xicclu, XiccluMP, _mp_generate_B2A_clut, _xicclu_mp, printcmdline, ) from DisplayCAL.wx_addons import BetterCallLater, BetterWindowDisabler, wx from DisplayCAL.wx_display_adjustment_frame import DisplayAdjustmentFrame from DisplayCAL.wx_display_uniformity_frame import DisplayUniformityFrame from DisplayCAL.wx_untethered_frame import UntetheredFrame from DisplayCAL.wx_windows import ( ConfirmDialog, HtmlInfoDialog, InfoDialog, ProgressDialog, SimpleTerminal, show_result_dialog, ) real_display_size_mm = None if sys.platform not in ("darwin", "win32"): try: from DisplayCAL import real_display_size_mm except ImportError as exception: warnings.warn(str(exception), ImportWarning, stacklevel=2) from wx.lib import 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) """ INSTRUMENT_MEASUREMENT_MODES_TEST_OUTPUT = """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""" # noqa: E501 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, ): """Check if delta E between a patch and its sRGB equivalent is suspiciously high.""" 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 ): """Check if the delta E between two patches is suspiciously low.""" 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 for suspiciously low delta E between patches with different RGB values. 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): # noqa: SIM300 # 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]) # cLUT profile or Shaper + matrix profile final = 2049 if bwd_mtx * [1, 1, 1] == [1, 1, 1] else 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 elif 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 = [[], [], []] 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 ) if bwd_mtx * [1, 1, 1] != [1, 1, 1]: # Matrix display profile: the matrix maps device (1, 1, 1) to the PCS # media white, so the shaper (TRC) curves have to reach 1.0 at device # maximum for device white to actually map to media white. The curves # are built (and optimized) from a fit normalized to the matrix white, # which can leave their endpoint slightly below 1.0 when the matrix # white and the gray ramp white are not perfectly consistent. A sub-1.0 # endpoint makes device max map to a fraction of media white, which in # turn makes a CMM clip near-white source values to device white when it # inverts the profile at limited (e.g. 8 bit) precision. Normalize each # curve so its endpoint is exactly 1.0 to avoid this near-white crush # (issue #710). This matches what Argyll's colprof does for matrix # profiles. Scaling is uniform, so the curve shape (gamma) and gray # neutrality are preserved. for curve in curves: endpoint = curve[-1] if endpoint and endpoint != 1.0: curve[:] = [min(v / endpoint, 1.0) for v in curve] 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: with contextlib.suppress(ValueError): # Not a gamma value, if it raises a ValueError, ignore calgamma = float(calgarg[1][1:]) if calgamma: gamma_type = calgarg[1][0] outoffset = DEFAULTS["calibration.black_output_offset"] calfarg = get_arg("f", options_dispcal) if calfarg: with contextlib.suppress(ValueError): outoffset = float(calfarg[1][1:]) 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) if logfn: logfn(f"Black relative luminance = {round(black_Y, 6):.6f}") if outoffset is not None and logfn: logfn(f"Black output offset = {round(outoffset, 2):.2f}") if calgamma > 0 and logfn: logfn( "Calibration gamma = {:.2f} {}".format( round(calgamma, 2), {"g": "relative", "G": "absolute"}.get(gamma_type) ) ) if calgamma and logfn: logfn( "Calibration overall transfer function ≈ " f"{caltf[0][0]} (Δ {1 - caltf[1]:.2%})" ) 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)]) if logfn: 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(f"≈ {tf[0][0]} (Δ {1 - tf[1]:.2%})") if logfn: 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) if logfn: logfn(f"Effective gamma = {round(gamma, 2):.2f}") # Only use standard transfer function if we got a good match if tf[1] >= 0.98: # Good match if logfn: 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 if logfn: 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 # Use multi-entry curve (not single-entry gamma) so the # v / 65535 conversion below stays in [0, 1] range. trc.set_trc(gamma) # Complex or gamma with offset elif 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) if logfn: logfn(f"Using transfer function for {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) if logfn: logfn(f"Effective gamma = {round(gamma, 2):.2f}") 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 gamma = trc_tag[0] if num_entries else 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: bool = True, save_profile_if_no_path: bool = False, ) -> None | str: """Get the current profile path from the configuration. Args: include_display_profile (bool): Whether to include the display profile. save_profile_if_no_path (bool): Whether to save the profile if it has no path. Returns: None | str: The profile path or None if not found. """ 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(f"ICCProfile({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.calculate_id() profile_cache_path = os.path.join(defaultpaths.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 return profile def parse_argument_string(args): """Parse an argument string and return 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) -> tuple[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 ") colprof_args = colprof_args[1] if len(colprof_args) > 1 else None return dispcal_args, colprof_args def get_options_from_cal(cal) -> tuple[list, list]: """Try and get options from calibration file. First, try the 'ARGYLL_DISPCAL_ARGS' and 'ARGYLL_COLPROF_ARGS' tags. If either does not exist, fall back to the copyright tag.""" 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 of '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): """Get dispcal and colprof options from TI3 file if present. This looks for the special DisplayCAL sections of '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)) display_size = [int(item) for item in match.groups()] if match else [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") # Linux / Mac OS X elif 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) -> None | tuple[int, str]: """Return first found entry beginning with the argmatch string or None. Returns: None: if no match found. tuple[int, str]: index and value of the match. """ for i, arg in enumerate(args): if (whole and arg == argmatch) or (not whole and arg.startswith(argmatch)): return i, arg return None 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 = f"Mac OS X {platform.mac_ver()[0]}; {platform.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 = ( f"{distro.id()} {distro.version()} {distro.codename()}; " f"{platform.machine()}" ) return { "User-Agent": f"{APPNAME}/{VERSION_STRING} ({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 elif 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 (OSError, 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=get_icon(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"", re.I | re.S), "", html, ) html = re.sub( re.compile(r"", re.I | re.S), "", html, ) print(html) if not silent: wx.CallAfter( HtmlInfoDialog, parent, msg=msg, html=html, ok=lang.getstr("ok"), bitmap=get_icon(32, "dialog-error"), log=False, ) return False return resp def insert_ti_patches_omitting_RGB_duplicates(cgats1, cgats2_path, logfn=print): """Insert unique patches from the first TI file after the first patch of the second TI. Returns: CGATS: Second TI as CGATS instance. """ # noqa: E501 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( f"{APPNAME}: Adding {len(cgats1_datasets):d} " f"fixed points to {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: str) -> Any: """Get attribute from the wrapped object. Args: name (str): The name of the attribute to get. Returns: Any: The attribute of the wrapped object. """ return getattr(object.__getattribute__(self, "_wrapped_object"), name) def __bool__(self) -> bool: """Return False for boolean comparisons.""" return False class DummyDialog: """Dummy dialog class for testing.""" def __init__(self, *args, **kwargs): self.is_shown_on_screen = True def Close(self, force=False): """Simulate closing the dialog. Args: force (bool): Whether to force close the dialog. Defaults to False. """ def Destroy(self): """Simulate destroying the dialog.""" def EndModal(self, id=-1): # noqa: A002 """Simulate ending the modal dialog. Args: id (int): The ID of the dialog. Defaults to -1. """ return id def Hide(self): """Simulate hiding the dialog.""" def IsShownOnScreen(self): """Check if the dialog is shown on screen.""" return self.is_shown_on_screen def Show(self, show=True): """Simulate showing the dialog. Args: show (bool): Whether to show the dialog. Defaults to True. """ self.is_shown_on_screen = show def ShowModal(self): """Simulate showing the dialog modally.""" 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: ClassVar[list] = [ "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: ClassVar[dict] = { 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: str) -> Any: """Get attribute from the wrapped stream. Args: name (str): The name of the attribute to get. Returns: Any: The attribute of the wrapped stream. """ 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 if 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. Args: worker (Worker): The worker instance that will run the producer. producer (callable): The function to be called by the worker. continue_next (bool): If True, the worker will continue to the next task even if the producer raises an exception. Defaults to False. """ def __init__(self, worker, producer, continue_next=False): self.worker = worker self.producer = producer self.continue_next = continue_next def __call__(self, *args, **kwargs): """Call the producer function with the given arguments. Args: *args: Positional arguments to pass to the producer. **kwargs: Keyword arguments to pass to the producer. Returns: The result of the producer function. """ 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 and 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) -> int: """Return the length of the string. Returns: int: The length of the string. """ 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) -> int: """Return 1 if sudo is available, otherwise 0. Returns: int: 1 if sudo is available, otherwise 0. """ return int(bool(self.sudo)) def __str__(self) -> str: """Return the sudo command with options. Returns: str: The sudo command with options. """ 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 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=get_icon(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 = f"{errstr}\n\n{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): """A wrapper around subprocess.Popen to handle output and errors.""" 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): """Check if the process is still alive. Returns: bool: True if the process is still running, False otherwise. """ self.exitstatus = self.poll() return self.exitstatus is None def expect(self, patterns, timeout=-1): """Wait for one of the specified patterns in the process's stdout. Args: patterns (list or str): A list of patterns to match against the process's stdout, or a single pattern. timeout (int): The maximum time to wait for a match, in seconds. If -1, the default timeout is used. Returns: str: The matched pattern if found, or None if no match is found before the timeout. """ 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 return None def send(self, s): """Send a string to the process's stdin. Args: s (str): The string to send. """ 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): """Terminate the process, optionally forcing it. Args: force (bool): If True, force termination of the process. """ sp.Popen.terminate(self) _coregraphics = None _corefoundation = None def _get_coregraphics(): """Return the CoreGraphics CDLL (macOS only), or None.""" global _coregraphics if _coregraphics is None and sys.platform == "darwin": try: _coregraphics = ctypes.CDLL( "/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics" ) except OSError: _coregraphics = False # Mark as tried-and-failed return _coregraphics or None def _get_corefoundation(): """Return the CoreFoundation CDLL (macOS only), or None.""" global _corefoundation if _corefoundation is None and sys.platform == "darwin": try: _corefoundation = ctypes.CDLL( "/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation" ) except OSError: _corefoundation = False # Mark as tried-and-failed return _corefoundation or None def _macos_active_display_ids(max_displays=16): """Return active CGDirectDisplayIDs in CoreGraphics enumeration order. This order matches ArgyllCMS' ``dispwin -d`` numbering (both come from CGGetActiveDisplayList), so index i here is display i+1 for dispwin. Returns: None | ctypes.CDLL, list[int]: The CoreGraphics CDLL (for reuse by the caller) and the list of display IDs, or (None, []) if CoreGraphics is unavailable. """ cg = _get_coregraphics() if cg is None: return None, [] cg.CGGetActiveDisplayList.argtypes = [ ctypes.c_uint32, ctypes.POINTER(ctypes.c_uint32), ctypes.POINTER(ctypes.c_uint32), ] ids = (ctypes.c_uint32 * max_displays)() count = ctypes.c_uint32(0) if cg.CGGetActiveDisplayList(max_displays, ids, ctypes.byref(count)) != 0: return cg, [] return cg, list(ids[: count.value]) def get_macos_videolut_maxima(max_displays=16): """Return the maximum VideoLUT output value for each active display (macOS). Used to detect a display whose VideoLUT has been clobbered to all-black (issue #694): a black VideoLUT has a maximum output near 0.0, a normal one near 1.0. The list is indexed in CoreGraphics active-display order, which matches ArgyllCMS' ``dispwin -d`` numbering (both come from CGGetActiveDisplayList). Returns: list[None | float]: Per-display maxima (0.0 - 1.0), None for a display that couldn't be read, or [] if CoreGraphics is unavailable. """ cg, ids = _macos_active_display_ids(max_displays) if cg is None or not ids: return [] cg.CGDisplayGammaTableCapacity.restype = ctypes.c_uint32 cg.CGDisplayGammaTableCapacity.argtypes = [ctypes.c_uint32] cg.CGGetDisplayTransferByTable.argtypes = [ ctypes.c_uint32, ctypes.c_uint32, ctypes.POINTER(ctypes.c_float), ctypes.POINTER(ctypes.c_float), ctypes.POINTER(ctypes.c_float), ctypes.POINTER(ctypes.c_uint32), ] maxima = [] for did in ids: cap = cg.CGDisplayGammaTableCapacity(did) or 256 red = (ctypes.c_float * cap)() green = (ctypes.c_float * cap)() blue = (ctypes.c_float * cap)() nent = ctypes.c_uint32(0) if ( cg.CGGetDisplayTransferByTable( did, cap, red, green, blue, ctypes.byref(nent) ) != 0 ): maxima.append(None) continue channel_max = 0.0 for channel in (red, green, blue): for j in range(nent.value): if channel[j] > channel_max: channel_max = channel[j] maxima.append(channel_max) return maxima def get_macos_videolut_capacity(display_index, max_displays=16): """Return the VideoLUT entry count for one active macOS display. Args: display_index (int): Zero-based display index (CoreGraphics/dispwin order, see :func:`_macos_active_display_ids`). Returns: None | int: The VideoLUT capacity, or None if it couldn't be read. """ cg, ids = _macos_active_display_ids(max_displays) if cg is None or display_index >= len(ids): return None cg.CGDisplayGammaTableCapacity.restype = ctypes.c_uint32 cg.CGDisplayGammaTableCapacity.argtypes = [ctypes.c_uint32] return cg.CGDisplayGammaTableCapacity(ids[display_index]) or None def set_macos_videolut(display_index, red, green, blue, max_displays=16): """Set one active macOS display's VideoLUT directly via CoreGraphics. Bypasses spawning a ``dispwin`` subprocess entirely. Argyll's dispwin has to exit after every invocation, and macOS's own cleanup of a just-exited process's transient VideoLUT claim has a bug where it can zero a *different*, untouched display's gamma table (issue #824). Applying corrected tables to every clobbered display from a single persistent process, with no process exit in between, avoids that OS bug entirely - confirmed experimentally on real multi-display hardware. Args: display_index (int): Zero-based display index (CoreGraphics/dispwin order, see :func:`_macos_active_display_ids`). red, green, blue (Sequence[float]): Per-channel output values (0.0 - 1.0), all the same length. Returns: bool: True on success. """ cg, ids = _macos_active_display_ids(max_displays) if cg is None or display_index >= len(ids): return False nent = len(red) cg.CGSetDisplayTransferByTable.restype = ctypes.c_int32 cg.CGSetDisplayTransferByTable.argtypes = [ ctypes.c_uint32, ctypes.c_uint32, ctypes.POINTER(ctypes.c_float), ctypes.POINTER(ctypes.c_float), ctypes.POINTER(ctypes.c_float), ] red_arr = (ctypes.c_float * nent)(*red) green_arr = (ctypes.c_float * nent)(*green) blue_arr = (ctypes.c_float * nent)(*blue) return ( cg.CGSetDisplayTransferByTable( ids[display_index], nent, red_arr, green_arr, blue_arr ) == 0 ) def vcgt_values_to_videolut(vcgt_values, nent): """Interpolate vcgt channel points onto a VideoLUT of ``nent`` entries. Args: vcgt_values (list[list[float, float]]): A single channel's vcgt points as returned by :meth:`DisplayCAL.icc_profile.VideoCardGammaType.get_values`, i.e. ``[input_0_255, output_0_65535]`` pairs. nent (int): Target VideoLUT entry count. Returns: list[float]: ``nent`` output values (0.0 - 1.0), linearly interpolated the same way ArgyllCMS' dispwin interpolates a .cal file's entries onto a display's actual VideoLUT size. """ ncal = len(vcgt_values) values = [point[1] / 65535.0 for point in vcgt_values] if ncal < 2 or nent < 2: return [values[-1] if values else 1.0] * nent out = [] for i in range(nent): pos = (ncal - 1) * i / (nent - 1) ix = min(int(pos), ncal - 2) weight = pos - ix out.append(values[ix] + weight * (values[ix + 1] - values[ix])) return out def get_macos_display_profile_data(display_index, max_displays=16): """Return the raw ICC profile bytes CoreGraphics currently has for a display. Uses ``CGDisplayCopyColorSpace()``/``CGColorSpaceCopyICCData()`` directly, rather than DisplayCAL's usual :func:`DisplayCAL.icc_profile.get_display_profile` (which queries the "Image Events" AppleScript scripting interface - this has been confirmed to fail outright on current macOS, e.g. Tahoe, raising "Can't get display profile of display N (-1728)"), and rather than running a dispwin subprocess (which risks re-triggering issue #824). Args: display_index (int): Zero-based display index (CoreGraphics/dispwin order, see :func:`_macos_active_display_ids`). Returns: None | bytes: The profile data, or None if it couldn't be read. """ cg, ids = _macos_active_display_ids(max_displays) if cg is None or display_index >= len(ids): return None cf = _get_corefoundation() if cf is None: return None cg.CGDisplayCopyColorSpace.restype = ctypes.c_void_p cg.CGDisplayCopyColorSpace.argtypes = [ctypes.c_uint32] cg.CGColorSpaceCopyICCData.restype = ctypes.c_void_p cg.CGColorSpaceCopyICCData.argtypes = [ctypes.c_void_p] cg.CGColorSpaceRelease.argtypes = [ctypes.c_void_p] cf.CFDataGetLength.restype = ctypes.c_long cf.CFDataGetLength.argtypes = [ctypes.c_void_p] cf.CFDataGetBytePtr.restype = ctypes.c_void_p cf.CFDataGetBytePtr.argtypes = [ctypes.c_void_p] cf.CFRelease.argtypes = [ctypes.c_void_p] colorspace = cg.CGDisplayCopyColorSpace(ids[display_index]) if not colorspace: return None try: icc_data = cg.CGColorSpaceCopyICCData(colorspace) if not icc_data: return None try: length = cf.CFDataGetLength(icc_data) ptr = cf.CFDataGetBytePtr(icc_data) if not ptr or length <= 0: return None return ctypes.string_at(ptr, length) finally: cf.CFRelease(icc_data) finally: cg.CGColorSpaceRelease(colorspace) def _get_macos_display_profile(display_index, log=None): """Return an ICCProfile for a display, preferring CoreGraphics over AppleScript. See :func:`get_macos_display_profile_data` for why. Falls back to :func:`DisplayCAL.icc_profile.get_display_profile` if CoreGraphics doesn't yield usable profile data. Returns: None | ICCProfile: The display's current profile, or None. """ data = get_macos_display_profile_data(display_index) if data: try: return ICCProfile(data) except Exception as exception: if log: log(exception) try: return get_display_profile(display_index) except Exception as exception: if log: log(exception) return None def reload_macos_videoluts(indices, log=None): """Re-apply calibration to macOS displays directly via CoreGraphics. Reads each display's currently active ICC profile (no subprocess) and applies its vcgt calibration (or a linear ramp if it has none) to all given displays back-to-back, from this single process. See :func:`set_macos_videolut` for why avoiding a subprocess-per-display matters (issue #824). Args: indices (Iterable[int]): Zero-based display indices to reload. log (None | Callable): Optional logging callable. Returns: list[int]: Indices that were successfully reloaded. """ reloaded = [] for i in indices: nent = get_macos_videolut_capacity(i) if not nent: continue profile = _get_macos_display_profile(i, log=log) vcgt = profile.tags.get("vcgt") if profile else None if vcgt: r_points, g_points, b_points = vcgt.get_values()[:3] red = vcgt_values_to_videolut(r_points, nent) green = vcgt_values_to_videolut(g_points, nent) blue = vcgt_values_to_videolut(b_points, nent) else: red = green = blue = [j / (nent - 1.0) for j in range(nent)] if set_macos_videolut(i, red, green, blue): reloaded.append(i) elif log: log(f"Failed to set VideoLUT for display {i + 1:d}") return reloaded def reload_black_videoluts( dispwin, threshold=0.05, retries=4, settle=0.3, log=None, _maxima_fn=None, _run=None, _sleep=None, ): """Re-load the VideoLUT of any macOS display that has gone black (issue #694). On macOS the OS (WindowServer) intermittently resets a display's VideoLUT to all-black when an ArgyllCMS display tool exits, turning the screen black. This detects that via CoreGraphics and re-loads the display's installed calibration with ``dispwin -d{N} -L`` (exactly what un-blacks the screen), retrying until it sticks (the re-load itself can occasionally be clobbered again) or the retries run out. Args: dispwin (str): Path to the dispwin executable. threshold (float): VideoLUT max output below which a display is considered black. retries (int): Maximum number of detect/re-load passes. settle (float): Seconds to wait between passes. log (None | Callable): Optional logging callable. _maxima_fn, _run, _sleep: Injection points for testing. Returns: list[int]: Zero-based indices of displays that were re-loaded. """ if sys.platform != "darwin" or not dispwin: return [] maxima_fn = _maxima_fn or get_macos_videolut_maxima run = _run or (lambda cmd: sp.call(cmd)) sleep_fn = _sleep or sleep reloaded = [] for _ in range(max(1, int(retries))): black = [ i for i, m in enumerate(maxima_fn()) if m is not None and m < threshold ] if not black: break for i in black: if log: log( f"{APPNAME}: VideoLUT for display {i + 1:d} is black - " "re-loading calibration (issue #694)" ) run([dispwin, "-v", f"-d{i + 1:d}", "-L"]) if i not in reloaded: reloaded.append(i) sleep_fn(settle) return reloaded class Worker(WorkerBase): """Worker class for ArgyllCMS. Args: owner (wx.Frame, optional): The owner frame for dialogs. Defaults to None. This is typically used to display dialogs or messages related to the worker's operations. """ def __init__(self, owner=None): 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] : void CGSUpdateManager::log() const: conn 0x1ec57 token 0x3ffffffffffd0a # noqa: E501 prestrip = re.compile( r"\D+\s+\d+\s+\d+:\d+:\d+\s+\w+\[\d+]\s+:[\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) -> None: """Initialize sounds for measurement and commit actions. Args: dummy (bool): If True, use dummy sounds instead of actual sound files. """ 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. Args: args (list): The list of arguments to modify. display (bool): Whether to add display-related arguments. ignore_display_name (bool): Whether to ignore the display name when adding display-related arguments. allow_nondefault_observer (bool): Whether to allow non-default observer settings. ambient (bool): Whether to add ambient light measurement options. allow_video_levels (bool): Whether to allow video levels (e.g. for dispread). quantize (bool): Whether to add quantization options. cmd (str, optional): The command being run (e.g. "dispcal", "dispread", "spotread"). If provided, it can be used to tailor the arguments added based on the command type. Returns: None: The function modifies the args list in place. """ 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") elif 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) ccmx = ccmx[1] if len(ccmx) > 1 and ccmx[1] else 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 (OSError, 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): """Add -E option to args if video levels are used. Args: args (list): The list of arguments to which the -E option will be added. """ 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. Args: cmd (str): The command to authenticate for. title (str): The title of the authentication dialog. parent (wx.Window, optional): The parent window for the dialog. Returns: bool or None or Error: True if authentication was successful and the command is allowed, False if the user cancelled the dialog, None if running on Windows or as root, or an Error if the command is not allowed or an error occurred during authentication. """ if sys.platform == "win32" or os.geteuid() == 0: return None 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 ), 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, ) -> None: """Apply BT.1886-like tone response to profile1 using profile2 blackpoint. `profile1` has to be a matrix profile. The function modifies `profile1` in place. Args: profile1 (Profile): The profile to apply the blackpoint to. profile2 (Profile): The profile to get the blackpoint from. XYZbp (tuple, optional): The blackpoint in XYZ coordinates. If not provided, it will be read from profile2. outoffset (float, optional): The output offset to apply. Defaults to 0.0. gamma (float or str, optional): The gamma value or type to apply. Defaults to 2.4. gamma_type (str, optional): The gamma type to apply. Defaults to "B". size (tuple, optional): The size of the output profile. Defaults to None. apply_trc (bool, optional): Whether to apply the TRC. Defaults to True. white_cdm2 (float, optional): The white luminance in cd/m². Defaults to 100. minmll (float, optional): The minimum mastering display luminance level in cd/m². Defaults to 0. maxmll (float, optional): The maximum mastering display luminance level in cd/m². Defaults to 10000. use_alternate_master_white_clip (bool, optional): Whether to use the alternate master white clip. Defaults to True. ambient_cdm2 (float, optional): The ambient luminance in cd/m². Defaults to 5. content_rgb_space (str, optional): The RGB space of the content. Defaults to "DCI P3". hdr_chroma_compression (bool, optional): Whether to apply HDR chroma compression. Defaults to False. hdr_sat (float, optional): The saturation for HDR chroma compression. Defaults to 0.5. hdr_hue (float, optional): The hue for HDR chroma compression. Defaults to 0.5. hdr_target_profile (Profile, optional): The target profile for HDR chroma compression. Defaults to None. """ 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(f"Output offset = {outoffset:.2%}") 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.decode("utf-8"), ), ) ) 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 = XiccluMP( 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, logfile=logfiles, thread_abort=self.thread_abort, abortmessage=lang.getstr("aborted"), ) def calibrate_instrument_producer(self): """Calibrate the instrument using spotread. Returns: str or Error: The result of the calibration command, or an error if the command could not be executed or the instrument is not found. """ 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 return Error(lang.getstr("argyll.util.not_found", "spotread")) def instrument_can_use_ccxx( self, check_measurement_mode=True, instrument_name=None ): """Check if the instrument can use CCMX/CCSS in the current measurement mode. Args: check_measurement_mode (bool): If True, check the current measurement mode. instrument_name (str, optional): The name of the instrument to check. Returns: bool: True if the instrument can use a CCMX or CCSS correction, False otherwise. """ # 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 not in ("SpyderX", "SpyderX2", "Spyder 2024") 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): """Return boolean whether the instrument can use a non-default observer. Args: instrument_name (str, optional): The name of the instrument to check. Returns: bool: True if the instrument can use a non-default observer, False otherwise. """ 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): """Return the main progress window. Returns: wx.Dialog: The main progress dialog window, or None if it is not set. """ if not self._progress_wnd and ( getattr(self, "progress_start_timer", None) and self.progress_start_timer.IsRunning() ): if current_thread() != main_thread(): 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): """Set the main progress window. Args: progress_wnd (wx.Dialog): The progress dialog window to set. """ self._progress_wnd = progress_wnd @property def progress_wnds(self): """Return a list of all progress windows. This includes the main progress dialog and any additional dialogs created for subprocesses or other tasks. If a terminal window is present, it is also included. Returns: list: A list of progress dialog windows. """ progress_wnds = list(self._progress_dlgs.values()) if hasattr(self, "terminal"): progress_wnds.append(self.terminal) return progress_wnds @property def pwd(self): """Return the password for sudo authentication. Returns: str: The decoded password string. """ 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): """Set the password for sudo authentication. Args: pwd (str): The password to set. """ encoded_user_name = codecs.encode( md5(getpass.getuser().encode()).hexdigest().encode(), # noqa: S324 "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}" # noqa: S108 def check_add_display_type_base_id(self, cgats, cfgname="measurement_mode"): """Add DISPLAY_TYPE_BASE_ID to CCMX. Args: cgats (CGATS): The CGATS object to modify. cfgname (str): The configuration name to check for the display type. Returns: bool: True if DISPLAY_TYPE_BASE_ID was added, False if it was already present. If the DISPLAY_TYPE_BASE_ID is not present, it will be added based on the display type from the configuration. """ if cgats.queryv1("DISPLAY_TYPE_BASE_ID"): return False # 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(f"Added DISPLAY_TYPE_BASE_ID {cgats[0].DISPLAY_TYPE_BASE_ID!r}") 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 """ if wx.Display.GetCount() <= 1 and display_no != 1: return False 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 and display_rect_1.Intersects( display_rect_2 ): oyranos = False break if not oyranos: break return oyranos def check_is_single_measurement(self, txt): """Check if we are doing a single measurement.""" 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): """Perform a single measurement, e.g. ambient light measurement.""" if getattr(self, "subprocess_abort", False) or getattr( self, "thread_abort", False ): # If we are aborting, ignore request return None 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=get_icon(32, "dialog-information"), ) self.progress_wnd.dlg = dlg dlg_result = dlg.ShowModal() dlg.Destroy() if self.finished: return None if dlg_result != wx.ID_OK: self.abort_subprocess() return False if self.safe_send(" "): self.progress_wnd.Pulse(lang.getstr("please_wait")) return None def check_instrument_calibration(self, txt): """Check if instrument needs calibration from Argyll CMS output. Args: txt (str): The output text from Argyll CMS to check for calibration messages. """ if self.instrument_calibration_complete: return if "calibration complete" in txt.lower(): self.log(f"{APPNAME}: Detected instrument calibration complete message") self.instrument_calibration_complete = True self._last_calibration_msg = None else: for calmsg in INST_CAL_MSGS: if calmsg not in txt and "calibration failed" not in txt.lower(): continue failed = "calibration failed" in txt.lower() if failed: self._last_calibration_msg = None if ( calmsg == self._last_calibration_msg and not failed ): # Same calibration prompt seen again after already sending a # keypress (e.g. ArgyllCMS 3.4.1 race condition with ColorMunki # where the prompt echoes before the device completes its single # calibration step). Auto-retry silently to avoid an infinite # dialog loop. self.log( f"{APPNAME}: Duplicate calibration prompt detected, " "auto-retrying without dialog" ) self.safe_send(" ") else: self.log(f"{APPNAME}: Detected instrument calibration message") self._last_calibration_msg = calmsg self.do_instrument_calibration(failed) break def check_instrument_calibration_file(self): """Check if the instrument calibration file is valid. This is done by checking the SpyderX calibration file for black calibration offsets, which should not be higher than 15. If the offsets are higher, we assume that the user has left the instrument on screen, which is not allowed, and we remove the calibration file and return False. If the file is not present, we return True, as this means that the instrument calibration is not done yet, or that the user has removed the instrument from the screen. If the instrument is not a SpyderX, we return True, as we don't have to check the calibration file. Returns: bool: True if the calibration file is valid or not present, False if the calibration file is present but invalid. """ # XXX: Check instrument calibration for SpyderX. For some reason, # users tend to leave the instrument on screen despite being told # otherwise... if not self._detected_instrument or "SpyderX" not in self._detected_instrument: return True if sys.platform == "win32": cachepath = os.path.join(defaultpaths.APPDATA, "Cache") else: cachepath = defaultpaths.CACHE spydx_cal_fn = os.path.join( cachepath, "ArgyllCMS", f".spydX_{self._detected_instrument_serial}.cal", ) if not os.path.isfile(spydx_cal_fn): return True # 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 OSError as exception: self.log( f"{APPNAME}: Warning - could not read SpyderX sensor cal:", exception, ) self.exec_cmd_returnvalue = Error( f"Could not read SpyderX sensor cal: {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 fmt = {2: " 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. This is done by looking at Argyll CMS command output. Args: txt (str): The output text from the Argyll CMS command. """ 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 ) and 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() elif 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): """Continue after instrument on screen message.""" 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. Args: txt (str): The output text from the Argyll CMS command. """ 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): """Check if measurement should be retried based on Argyll CMS output. Args: txt (str): The output text from the Argyll CMS command. """ 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. Args: txt (str): The output text from the Argyll CMS command. """ 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): """Return True if we should skip video levels detection. Returns: bool: True if video levels detection should be skipped, False if it should not be skipped. """ display_name = config.get_display_name(None, True) return ( self._detected_output_levels or not getcfg("patterngenerator.detect_video_levels") # Web @ localhost uses Argyll's built-in web server (-dweb:). # Video level detection would require a separate dispread session, # which tears down/restarts that server before interactive adjust. # Skip auto-detection here to keep a single continuous web session. or display_name == "Web @ localhost" or display_name == "Untethered" or is_ccxx_testchart() ) def detect_video_levels(self): """Detect wether we need video (16..235) or data (0..255) levels. Returns: bool: True if video levels were detected, False if data levels were detected, """ if self.get_skip_video_levels_detection(): if config.get_display_name(None, True) == "Web @ localhost" and not getattr( self, "_web_video_levels_skip_logged", False ): self.log( "Skipping output levels detection for Web @ localhost " "to keep a continuous web session." ) self._web_video_levels_skip_logged = True 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. Returns: bool: True if video levels were detected, False if data levels were detected, Exception: If an error occurred during detection. """ 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( b"""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 """ ) 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 (OSError, 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 if self._use_detected_video_levels is None: # Retry return None 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): """Show a confirmation dialog to the user if a levels issue was detected.""" 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. Args: failed (bool): If True, the calibration failed and the user should be informed about it. If False, the calibration was successful and the user should be prompted to calibrate the instrument. Returns: None | bool: None if the calibration was initiated, False if the user canceled """ if getattr(self, "subprocess_abort", False) or getattr( self, "thread_abort", False ): # If we are aborting, ignore request return None 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=get_icon(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 None if dlg_result != wx.ID_OK: self.log(f"{APPNAME}: Canceled instrument calibration prompt") self._last_calibration_msg = None 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) return None def abort_all(self, confirm=False): """Abort all worker threads and subprocesses. Args: confirm (bool): If True, show a confirmation dialog before aborting. Returns: bool: True if at least one worker was aborted, False otherwise. """ 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. Args: confirm (bool): If True, show a confirmation dialog before aborting. """ 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=get_icon(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): """Consumer for delayed result of subprocess termination. Args: delayedResult: Delayed result object containing the result of the subprocess termination. """ 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) -> None | bool: """Prompt user to place the instrument on the screen. Returns: None: if the user canceled the dialog. bool: True if the user confirmed the dialog. """ if getattr(self, "subprocess_abort", False) or getattr( self, "thread_abort", False ): # If we are aborting, ignore request return None 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=get_icon(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 None 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) return None def instrument_reposition_sensor(self): """Show a dialog asking user to reposition the instrument sensor. Returns: None: if the user canceled the dialog. bool: True if the user confirmed the dialog. """ if getattr(self, "subprocess_abort", False) or getattr( self, "thread_abort", False ): # If we are aborting, ignore request return None 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=get_icon(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 None 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) return None 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 ): """Generate a filename for the 3D LUT based on current settings. Args: path (str): Optional base path for the LUT filename. include_input_profile (bool): Whether to include the input profile name in the filename. include_ext (bool): Whether to include the file extension in the filename. Returns: str: The generated filename for the 3D LUT. """ # 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 cal_exclude = "" if getcfg("3dlut.output.profile.apply_cal") else "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", file_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.decode("utf-8"), profile.colorSpace.decode("utf-8"), ), ) ) 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], is_name=True) ) 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), is_name=True ) # 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), is_name=True ) if profile_in_basename == profile_out_basename: (profile_out_filename, profile_out_ext) = os.path.splitext( profile_out_basename ) profile_out_basename = f"{profile_out_filename} (2){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 " f"data (normalized 0..100): {XYZbp[1] * 100:.6f}" ) 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 if 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 file_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 channel in ["r", "g", "b"]: if f"{channel}TRC" in profile_in.tags: profile_in.tags[f"{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) # noqa: S306 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 if 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, file_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) # noqa: S306 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 if 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 if 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 file_format == "madVR": args.append("-3m") elif file_format == "eeColor" and not TEST: args.append("-3e") elif file_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 = file_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 occurred, 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): cond = v <= 16.0 / 255.0 if black_hack else 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 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(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 = [ [v * (1 - (n * 2001 + i) / 20000.0) for v in XYZw] for i in range(2001) ] 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( f"Solution found at index {i:d} " f"(step size {1 / 2000.0:f})\n" ) 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( f"No solution found in {n:d} iterations with {i:d} steps" ) for XYZ in 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 direction = "backward" if use_b2a else "inverse forward" logfiles.write( f"Creating device link from {direction} " f"lookup ({num_workers:d} workers)...\n" ) RGB_dst_out = [] for slices in pool_slice( _xicclu_mp, 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 not clipmask: RGB[j] = v continue if ( input_encoding != "T" and scale > 1 and v > 16.0 / 255.0 ): # We got +ve clipping # Re-scale all non-black values 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 ( ( ( file_format == "eeColor" or (file_format == "cube" and collink_version >= [1, 7]) ) and not TEST ) or file_format == "madVR" ) ) or file_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_STRING), ( "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.calculate_id() 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 file_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(f"--hdr={smpte2084 + hdr_display:0.0f}") 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 file_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( # noqa: UP032 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( # noqa: UP032 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( # noqa: UP032 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( # noqa: UP032 os.path.splitext(profile_in.filename)[0] ) ) if is_argyll_lut_format or (use_xicclu and file_format == "madVR"): # Collink has already written the 3DLUT for us, # or we have written the madVR 3D LUT if file_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 if not result: raise UnloggedError(lang.getstr("aborted")) # We have to create the 3DLUT ourselves logfiles.write(f"Generating {file_format} 3D LUT...\n") # Create input RGB values RGB_oin = [] RGB_in = [] RGB_indexes = [] seen = {} if file_format == "eeColor": # Fixed size size = 65 elif file_format == "ReShade": file_format = "png" if file_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 def quantizer(v): return math.ceil(v * (2**input_bits - 1)) scale = quantizer(1.0) else: def quantizer(v): return 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 file_format in ("3dl", "mga", "spi3d") or ( file_format == "png" and getcfg("3dlut.image.order") == "bgr" ): columns = (0, 1, 2) elif file_format == "eeColor": columns = (2, 0, 1) else: columns = (2, 1, 0) for i in range(size): # Red if file_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(size): # Green if file_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(size): # Blue if self.thread_abort: raise Info(lang.getstr("aborted")) if file_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 file_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 file_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 file_format not in ("dcl", "png"): lut = [[f"# Created with {APPNAME} {VERSION_STRING}"]] linesep = "\n" if file_format in ("3dl", "dcl"): maxval = math.pow(2, output_bits) - 1 if file_format == "3dl": lut.append([f"# INPUT RANGE: {input_bits:d}"]) lut.append([f"# OUTPUT RANGE: {output_bits:d}"]) lut.append([]) for i in range(size): lut[-1].append(f"{quantizer(i * step):d}") 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( f"{round(RGB_triplet[component] / scale * maxval):d}" ) elif file_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( *((f"{{:.{len(str(maxval)[fp_offset + 1 :]):d}f}}",) * 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(f"{RGB_triplet[component] * maxval:.6f}") elif file_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(f"{RGB_triplet[component] * maxval:.6f}") elif file_format == "eeColor": if maxval is None: maxval = 1.0 lut = [] for i, RGB_triplet in enumerate(RGB_out): lut.append([f"{component * maxval:.6f}" 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 file_format == "mga": lut = [ ["#HEADER"], [f"#filename: {os.path.basename(path)}"], ["#type: 3D cube file"], ["#format: 1.00"], ["#created: {}".format(strftime("%d %B %Y"))], [f"#owner: {getpass.getuser()}"], [f"#title: {os.path.splitext(os.path.basename(path))[0]}"], ["#END"], ] lut.append([]) lut.append(["channel 3d"]) lut.append([f"in {size**3:d}"]) maxval = 2**output_bits - 1 lut.append([f"out {maxval + 1:d}"]) 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(f"{round(RGB_triplet[component] * maxval)}") valsep = "\t" elif file_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([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 file_format != "png": lut.append([]) for i, line in enumerate(lut): lut[i] = valsep.join(line) result = linesep.join(lut) # Write 3DLUT with open(path, "wb") as lut_file: if file_format != "png": lut_file.write(result.encode()) else: im = imfile.Image(lut, output_bits) im.write(lut_file) if file_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 not isinstance(line, str): continue 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_STRING} 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 # TODO: OBSERVERS should include 2012_2 and 2012_10 already. if self.argyll_version >= [1, 9, 4]: if self.argyll_version < [3, 4, 0]: # Add CIE 2012 observers valid_observers = natsort([*OBSERVERS, "2012_2", "2012_10"]) else: # Add CIE 2015 observers (renamed from "2012_*" in # ArgyllCMS 3.4.0; migrate any stored "2012_*" values # so they don't silently fall back to the default # "1931_2" observer and produce wrong results). valid_observers = natsort([*OBSERVERS, "2015_2", "2015_10"]) observer_rename = {"2012_2": "2015_2", "2012_10": "2015_10"} else: valid_observers = OBSERVERS for key in [ "{}", "colorimeter_correction.{}", "colorimeter_correction.{}.reference", ]: key = key.format("observer") config.VALID_VALUES[key] = valid_observers if self.argyll_version >= [3, 4, 0]: stored = getcfg(key, fallback=False, raw=True) if stored in observer_rename: setcfg(key, observer_rename[stored]) 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( f"Chromecast {line[0]}: {safe_str(value, ENC)}" ) elif arg == "-dprisma[:host]": # Prisma (via Argyll CMS) # : @ # 141550000000: prisma-0000 @ 172.31.31.162 match = re.findall(r".+?: (.+) @ (.+)", value) if len(match): displays.append( f"Prisma {line[0]}: {safe_str(match[0][0], ENC)} " f"@ {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 real_display_size_mm: # Sync with Argyll - needed under Linux to map EDID real_display_size_mm.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 ", "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, f"{m_left:d}, {m_top:d}, {m_width:d}x{m_height:d}", ] ) 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 sys.platform == "darwin" and i < len(self.display_rects): rect = self.display_rects[i] sp_name = get_display_name_from_system_profiler( rect.width, rect.height ) if sp_name: if sp_name in ("Color LCD", "iMac"): model_id = get_model_id() if model_id: sp_name = model_id desc = [sp_name] 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 ", "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 OSError 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). shell (bool): If True, runs the command in a shell. 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. dry_run (bool): If True, does not run the command, but returns. sessionlogfile (str): If not None, the session log file to write. use_pty (bool): If True, uses a pseudo terminal to run the command. """ 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 = { 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(256)), list(range(256)), list(range(256)), ], } ) srgb.setDescription(f"{APPNAME} Linear Calibration sRGB Profile") srgb.calculate_id() 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")) if 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"{i / 255.0:f} {R:f} {G:f} {B:f}\n" 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 (OSError, CGATSError) as exception: self.madtpg_disconnect(False) return exception else: # ICC profile try: profile = ICCProfile(calfilename) except (OSError, 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] = round( cal.DATA[i][f"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(f"madVR_{endis.capitalize()}3dlut failed") 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(f"Pattern area: {patternconfig[0]:d}%") self.log(f"Background level: {patternconfig[1]:d}%") self.log( "Background mode: {}".format( {0: "Constant", 1: "APL gamma", 2: "APL linear"}.get( patternconfig[2], patternconfig[2] ) ) ) self.log(f"Border width: {patternconfig[3]:d} pixels") 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, f"-d{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, f"-P0.5,0.5,{math.sqrt(patternconfig[0]):f}") 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( # noqa: UP032 safe_str(os.pathsep.join(pythonpath), ENC) ) ) waitfile.write( f'"{safe_str(python, ENC)}" ' f'-S "{safe_str(scriptfilename, ENC)}" %*\n' ) 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) # noqa: S103 args[index] += "{} ./{}".format( strtr(safe_str(python), {'"': r"\"", "$": r"\$"}), os.path.basename(waitfilename), ) if (VERBOSE >= 1 or not silent) and (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 if 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, f"{working_basename}.{cmdname}{SCRIPT_EXT}" ) allfilename = os.path.join( working_dir, f"{working_basename}.all{SCRIPT_EXT}" ) first = not os.path.exists(allfilename) last = cmdname == get_argyll_utilname("dispwin") cmdfile = open(cmdfilename, "w") # noqa: SIM115 allfile = open(allfilename, "a") # noqa: SIM115 cmdfiles = Files((cmdfile, allfile)) context = cmdfiles if first else cmdfile if sys.platform == "win32": context.write("@echo off\n") context.write( f"PATH {os.path.dirname(cmd)};%PATH%\n".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( f"PATH={os.path.dirname(cmd)}:$PATH\n".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) # noqa: S103 os.chmod(allfilename, 0o755) # noqa: S103 cmdfiles.write( " ".join(quote_args(cmdline)) .replace(cmd, cmdname) .encode(ENC, "safe_asciize") + b"\n" ) 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, f"{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, f"{appfilename}/Contents/Resources/main.command", ) os.chmod( f"{appfilename}/Contents/Resources/main.command", 0o755, # noqa: S103 ) # 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, f"{appfilename}/Contents/Resources/main.command", ) os.chmod( f"{appfilename}/Contents/Resources/main.command", 0o755, # noqa: S103 ) 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 = working_dir if working_dir else None 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 stderr = sp.STDOUT if silent else tempfile.SpooledTemporaryFile() # noqa: SIM115 if capture_output: stdout = tempfile.SpooledTemporaryFile() # noqa: SIM115 elif ( sys.stdout and hasattr(sys.stdout, "isatty") and sys.stdout.isatty() ): stdout = sys.stdout else: stdout = sp.PIPE if sudo: stdin = tempfile.SpooledTemporaryFile() # noqa: SIM115 stdin.write(self.pwd.encode(ENC, "replace") + os.linesep) stdin.seek(0) else: stdin = sp.PIPE else: data_encoding = self.pty_encoding kwargs = {"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(), # noqa: SIM115 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)) from 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 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(proc): proc.wait() self.log( f"{APPNAME}: caffeinate exited " f"with code {proc.returncode}" ) wait_thread = threading.Thread( target=partial( caffeinate_wait, proc=caffeinated ) ) 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 if 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 if 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 if 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 " f"(unknown reason)" ) break sleep(0.05) self.log( f"{APPNAME}: Send buffer received: " f"{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 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)) from 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() # noqa: SIM115 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() # noqa: SIM115 if not silent and len(self.errors): errstr = "".join(self.errors).strip() self.log(errstr) except (OSError, Error, 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 and hasattr(self.patterngenerator, "conn"): try: if config.get_display_name() == "Resolve": # Send fullscreen black to prevent burn-in if finished: with contextlib.suppress(OSError): self.patterngenerator.send((0,) * 3, x=0, y=0, w=1, h=1) elif isinstance( self.patterngenerator, WebWinHTTPPatternGeneratorServer ): # Keep WebWin server alive across setup/test and # interactive adjustment so connected browsers do not # need to reconnect between consecutive operations. 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()) # macOS clobbers a display's VideoLUT to all-black when an ArgyllCMS # display tool exits (issue #694). Recover right here, in the worker # thread, after the tool has finished - this is race-free (sequential) # and independent of whether the GUI stays open, so the screen doesn't # stay black through the subsequent profile-creation steps. if ( sys.platform == "darwin" and not dry_run and args and "-?" not in args and getcfg("profile_loader.macos_reapply_watch") and cmdname in ( get_argyll_utilname("dispcal"), get_argyll_utilname("dispread"), get_argyll_utilname("spotread"), get_argyll_utilname("dispwin"), get_argyll_utilname("colprof"), ) ): reload_black_videoluts(get_argyll_util("dispwin"), log=self.log) if self.exec_cmd_returnvalue is not None: return self.exec_cmd_returnvalue return self.retcode == 0 def flush(self): """Flush the output and error streams of the worker.""" def _generic_consumer( self, delayedResult, consumer, continue_next, *args, **kwargs ): """Generic consumer for delayed results. Args: delayedResult (DelayedResult): The delayed result to process. consumer (callable): The consumer function to call with the result. continue_next (bool): Whether to continue to the next step after processing the result. *args: Additional arguments to pass to the consumer. **kwargs: Additional keyword arguments to pass to the consumer. Raises: UnloggedError: If an error occurs during processing. """ # 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): """Generate a profile's A2B0 table (perceptual). Args: profile (Profile): The profile to generate the A2B0 table for. clutres (int, optional): The cLUT resolution to use. If not specified, the resolution of the A2B0 table in the profile will be used. logfile (file-like, optional): A file-like object to write log messages to. If not specified, no logging will be done. Raises: Error: If the profile's connection color space is not XYZ. Returns: bool: True if the A2B0 table was successfully generated, """ # Lab cLUT is currently not implemented and should NOT be used! if profile.connectionColorSpace != b"XYZ": connection_color_space = profile.connectionColorSpace.decode("utf-8") raise Error( lang.getstr( "profile.unsupported", (connection_color_space, connection_color_space), ) ) 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) channel = [j * 257 for j in range(256)] A2B0.input = [channel for _ in range(3)] A2B0.output = [channel for _ in range(3)] # 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(f"cLUT grid res: {clutres:d}\n") vrange = range(clutres) step = 1.0 / (clutres - 1.0) idata = [ [v * step for v in (R, G, B)] for R in vrange for G in vrange for B in vrange ] 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( f"Number of cLUT entries ({numrows}) exceeds cLUT res " f"maximum ({clutres}^3 = {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(f"\r{i / (numrows - 1.0):0.0%}") # 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. Args: profile (Profile): The profile to generate the B2A table for. clutres (int, optional): The cLUT resolution to use. If not specified, the resolution of the A2B table will be used. source (str, optional): The source table to use for generating the B2A table. Can be "A2B" (default) or "B2A". If "B2A", the B2A table will be re-generated by interpolation. tableno (int, optional): The table number to use for the B2A table. If not specified, it will default to 1 if "A2B1" is in the profile's tags, otherwise 0. bpc (bool, optional): Whether to apply black point compensation to the B2A table. Defaults to False. smooth (bool, optional): Whether to apply smoothing to the generated B2A table. Defaults to True. rgb_space (str, optional): The RGB color space to use for the B2A table. If not specified, the RGB color space of the profile will be used. logfile (file-like, optional): A file-like object to write log messages to. If not specified, no logging will be done. filename (str, optional): The filename to save the generated B2A table to. If not specified, the table will not be saved to a file. only_input_curves (bool, optional): If True, only the input curves of the B2A table will be generated, without the cLUT. Defaults to False. Returns: bool: True if the B2A table was successfully generated, False otherwise. """ if tableno is None: tableno = 1 if "A2B1" in profile.tags else 0 if not clutres: if f"B2A{tableno:d}" 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 " f"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 - after normalisation it should be close to # D50 (the ICC PCS white). Use a tolerance rather than an exact rounded # match: a real profile's adapted white is routinely off D50 by ~0.001 # (chromatic adaptation / measurement / 16-bit encoding) and must not be # rejected. The tolerance still catches a grossly wrong white (e.g. from # a failed inverse lookup, which lands far from D50). D50_wp = colormath.get_whitepoint("D50") if ( abs(XYZwp[0] - D50_wp[0]) > 0.01 or abs(XYZwp[1] - D50_wp[1]) > 0.01 or abs(XYZwp[2] - D50_wp[2]) > 0.01 ): 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 _, v in enumerate(XYZ): if v > XYZ[i]: raise Error( "xicclu: Invalid primary {} XYZ: {:.4f} {:.4f} {:.4f}".format( *("RGB"[i], *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], *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, *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, *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 " f"{sat:0.0%}...\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(f"{channel.upper()} xy {x:.6f} {y:.6f} -> ") x, y, Y = colormath.xyYsaturation(x, y, Y, wx, wy, sat) if logfile: logfile.write(f"{x:.6f} {y:.6f}\n") 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 _channel, components in 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( f"Skipping {rgb_space[-1]} because it does " f"not encompass Rec. 709 {color}\n" ) 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( f"{rgb_space[-1]} " f"fit: {round(1.0 / max(extremes), 2):.2f} " f"(area: {area1 / area2:.2%})\n" ) 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 False: # if not pcs_candidate: # 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])) rgb_space = None for _coverage, fit, rgb_space_inner in pcs_candidates: if round(fit, 2) >= 1: rgb_space = rgb_space_inner break if rgb_space is None: if not pcs_candidates: raise Error(lang.getstr("profile.no_suitable_pcs_candidate")) # None of the reference gamuts fully encompass the # display's actual primaries. Fall back to the # candidate that clips the least instead of crashing. rgb_space = max(pcs_candidates, key=lambda row_: row_[1])[2] if logfile: logfile.write(f"Using primaries: {rgb_space[-1]}\n") for _, 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], *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: " f"{Xr + Xg + Xb:.4f} {Yr + Yg + Yb:.4f} {Zr + Zg + Zb:.4f}\n" ) 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(f"Correction factors: {Sr:.4f} {Sg:.4f} {Sb:.4f}\n") 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(f"Matrix {m + 1:d}:\n") for row in matrix: logfile.write(f"{row!r}\n") itable.matrix = m2 * m3 if logfile: logfile.write("Final matrix:\n") for row in itable.matrix: logfile.write(f"{row!r}\n") 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(f"\r{j / maxval:0.0%}") if logfile: logfile.write("\n") if False: # if 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 = 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(f"#{j:d} {itable.input[i][j]:d} -> {v:d}\n") itable.input[i][j] = v if only_input_curves: # We are done return True if clutres == -1: # Auto # 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 if pcs == "l" and smooth else 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( f"Generating {source}{tableno:d} table lookup input values...\n" ) 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, _ in enumerate(v): v[i] *= (threshold - d) / r v2[i] *= 1 - (threshold - d) / r v[i] += v2[i] else: v = [100.0, 0.0, 100.0] if cam_diag else 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(f"\r{(R * G * B) / ((clutres - 1.0) ** 3):0.0%}") 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(f"\r{i / (numrows - 1.0):0.0%}") # 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} {!r}".format(i, RGB)): RGB = 0, 0, 0 elif i == ( clutres**2 * (clutres - 1) + clutres * (clutres // 2) + clutres // 2 ): # if raw_input("{:d} {!r}".format(i, 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 RGB to 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, *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, *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 RGB in oRGB: for j, v in enumerate(RGB): ocurves[j].append(v) ointerp = [] for ocurve in 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. Args: profile (ICCProfile): The ICC profile to smooth. tableno (int): The table number to smooth. diagpng (int): If 1, generate diagnostic PNGs. filename (str): The filename to use for diagnostic PNGs. logfile (TextIO): The logfile to write to. Returns: bool: True if smoothing was successful, False otherwise. """ 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. Args: quirk (bool): If True, use quirked EDID. use_serial_32 (bool): If True, use 32-bit serial number. truncate_edid_strings (bool): If True, truncate EDID strings. omit_manufacturer (bool): If True, omit manufacturer from EDID. query (bool): If True, query the device ID instead of returning the EDID. Returns: str or None: The device ID for the display, or None if no display is configured or the EDID is not available. """ 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 None if real_display_size_mm: display = real_display_size_mm.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. Returns: str: The display identifier for Argyll CMS. """ 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 return "1" if display_name == "Prisma" and not DEFAULTS["patterngenerator.prisma.argyll"]: if self.argyll_virtual_display: return self.argyll_virtual_display return "1" if display_name == "Prisma": host = getcfg("patterngenerator.prisma.host") with contextlib.suppress(OSError): host = socket.gethostbyname(host) 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 i, disp in enumerate(self.lut_access): if disp: display_lut_no = i break display = f"{display},{display_lut_no + 1}" return display def get_display_edid(self): """Return EDID of currently configured display. Returns: dict: The EDID of the display, or an empty dict if no EDID is available. """ 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. Args: prepend_manufacturer (bool): If True, prepend the manufacturer to the display name. prefer_edid (bool): If True, prefer the EDID name over the configured display name. remove_manufacturer (bool): If True, remove the manufacturer from the display name if it is already present. Returns: str: The display name. """ 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. Args: prepend_manufacturer (bool): If True, prepend the manufacturer to the display name. prefer_edid (bool): If True, prefer the EDID name over the configured display name. Returns: str: The shortened display name. """ 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. return re.sub(r"[\s]+", "_", 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. Args: display_no (int): The display number to get the profile for. Returns: str: The argument for dispwin, either '-L' or a profile 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 get_calibration_load_discrepancy(self, dispwin, display_no, profile_arg): """Return how much a display's loaded calibration differs from a profile. Runs ``dispwin -d{N} -V `` which compares the VideoLUT contents currently loaded in the hardware against the given profile's calibration and reports the discrepancy as a percentage. A verify-only run does not open a window nor alter the VideoLUT, so this is safe to call freely. Used to detect the macOS "black screen" condition where the OS clobbers the VideoLUT after it was loaded (issue #694): a clobbered/black VideoLUT yields a very large discrepancy (close to 100%), whereas a correctly loaded calibration is at most a few percent off due to quantization. Args: dispwin (str): Path to the dispwin executable. display_no (int): Zero-based display index. profile_arg (str): Profile path to verify against. ``-L`` is not a valid argument here (pass a real profile path). Returns: None | float: The discrepancy in percent (0.0 - 100.0), or None if it could not be determined (e.g. no VideoLUT access). """ self.exec_cmd( dispwin, ["-v", f"-d{display_no + 1:d}", "-V", profile_arg], capture_output=True, skip_scripts=True, silent=True, ) # The 'Verify: ... (discrepancy X%)' message goes to stdout, # other errors go to stderr. output = "\n".join(self.errors + self.output) if "We don't have access to the VideoLUT" in output: return None match = re.search(r"discrepancy\s+([0-9]+(?:\.[0-9]+)?)%", output) if match: return float(match.group(1)) return None def calibration_is_clobbered(self, dispwin, display_no, profile_arg): """Return True if a display's loaded calibration looks clobbered/black. See :meth:`get_calibration_load_discrepancy`. Only a discrepancy above ``profile_loader.clobbered_discrepancy_threshold`` is treated as clobbered, so benign quantization differences don't trigger needless re-loads (which could themselves re-trigger the macOS bug). Args: dispwin (str): Path to the dispwin executable. display_no (int): Zero-based display index. profile_arg (str): Profile path to verify against. Returns: bool: True if the loaded calibration is considered clobbered. """ discrepancy = self.get_calibration_load_discrepancy( dispwin, display_no, profile_arg ) if discrepancy is None: return False threshold = float(getcfg("profile_loader.clobbered_discrepancy_threshold")) return discrepancy > threshold def update_display_name_manufacturer( self, ti3, display_name=None, display_manufacturer=None, write=True ): """Update display name and manufacturer in 'ARGYLL_COLPROF_ARGS' section of a TI3 file. Args: ti3 (CGATS): The CGATS object representing the TI3 file. display_name (str): The display name to set. display_manufacturer (str): The display manufacturer to set. write (bool): If True, write the updated TI3 file back to disk. Returns: list: List of options for colprof command. """ # noqa: E501 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 ( OSError, 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 options_dispcal = [] if is_ccxx_testchart() else 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. Args: instrument_name (str): Name of the instrument to get features for. Returns: dict: Mapping of feature names to their values. """ 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. Args: instrument_id (str): ID of the instrument to get measurement modes for. skip_ccxx_modes (bool): If True, skip modes supplied via CCMX/CCSS. Returns: dict: Mapping of measurement mode selectors to their descriptions. """ if not instrument_id: features = self.get_instrument_features() instrument_id = features.get("id", self.get_instrument_name()) if not instrument_id: return {} measurement_modes = self.measurement_modes.get(instrument_id, {}) if measurement_modes: return 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(INSTRUMENT_MEASUREMENT_MODES_TEST_OUTPUT.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 not measurement_modes_follow or len(parts) != 2: continue 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: continue # Found a mode for our instrument if ( re.sub(r"\s*\(.*?\)?$", "", desc) in [*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 def get_instrument_name(self): """Return name of currently configured instrument. Returns: str: Name of the instrument, or an empty string if not found. """ try: return self.instruments[getcfg("comport.number") - 1] except IndexError: return "" def get_real_displays(self) -> list[int]: """Return real (nonvirtual) displays. Return: list[int]: List of display numbers (0-indexed) of real displays. """ return [ display_no for display_no in range(len(self.displays)) if not config.is_virtual_display(display_no) ] def get_technology_strings(self): """Return technology strings mapping (from ccxxmake -??). Returns: dict: Mapping of technology strings to their descriptions. """ 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 {} technology_strings = {} 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): """Return True if the current display has LUT access. Returns: bool: True if the current display has LUT access, False otherwise. """ 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. Returns: bool: True if separate LUT access is available and needed, False otherwise. """ # 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. Args: cmd (str): The command to execute for importing corrections. Can be 'i1d3ccss', 'spyd4en' or 'oeminst'. args (list, optional): Additional arguments to pass to the command. asroot (bool, optional): Whether to run the command as root. Returns: Exception: If an error occurs during the import. """ 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. Args: args (list, optional): Additional arguments to pass to the command. asroot (bool, optional): Whether to run the command as root. Returns: Exception: If an error occurs during the import. """ 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. Args: args (list, optional): Additional arguments to pass to the command. asroot (bool, optional): Whether to run the command as root. Returns: Exception: If an error occurs during the import. """ return self.import_colorimeter_corrections( get_argyll_util("spyd4en"), args, asroot ) def install_3dlut(self, path, filename=None): """Install 3D LUT from file. Args: path (str): Path to the 3D LUT file. filename (str, optional): Filename to use for the 3D LUT. If not provided, the filename will be derived from the path. Returns: Exception: If an error occurs during the installation. """ 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"), *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 " f"{lut3d_section} slot {slot:d} ({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")) 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 to the system and register it. Args: profile_path (str): Path to the profile file to install. capture_output (bool): Whether to capture output from the installation process. skip_scripts (bool): Whether to skip running scripts during installation. silent (bool): Whether to suppress output messages during installation. Returns: bool | str: True if the installation was successful, a string with the installed profile path if the profile was installed with a different name, or an exception if the installation failed. """ 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 (OSError, 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 defaultpaths.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. Args: uninstall (bool): If True, uninstall the configuration. filenames (list[str]): List of filenames to install or uninstall. If None, use the default Argyll instrument configuration files (e.g. 'colord.conf', 'colord.rules', etc.) or uninstall all installed files. Returns: Error, Info, or None: Returns an Error if something went wrong, Info if the operation was successful, or None if no files were installed or uninstalled. """ 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.DATA_HOME, "backup", strftime("%Y%m%dT%H%M%S") ) for filename in filenames: dst = udevrules if filename.endswith(".rules") else 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, f"{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 if 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 and ("colord" not in [g.gr_name for g in grp.getgrall()]) and (groupadd := which("groupadd", paths)) ): # Add colord group if it does not exist result = self.exec_cmd( groupadd, ["colord"], capture_output=True, skip_scripts=True, asroot=True, ) if ( not isinstance(result, Exception) and result and "colord" not in getgroups(getpass.getuser(), True) and (usermod := which("usermod", paths)) ): # Add user to colord group if not yet a member 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 and (udevadm := which("udevadm", paths)) ): # Reload udev rules 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. Args: uninstall (bool): If True, uninstall the drivers. launch_devman (bool): If True, launch Device Manager after installing or uninstalling the drivers. Returns: Error, Info, or None: Returns an Error if something went wrong, Info if the operation was successful, or None if no drivers were installed or uninstalled. """ 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") ) if 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.DATA_HOME, "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 if not installer_zip: # Cancelled return None 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 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 = current_thread() != main_thread() 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 if 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. Args: profile_path (str): The path to the profile to install. capture_output (bool): Whether to capture the output of the command. skip_scripts (bool): Whether to skip running scripts. silent (bool): Whether to suppress output messages. Return: str | bool | Exception: The profile path, an error or False. """ if False: # if sys.platform == "darwin": # 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( f"util_win.enable_per_user_profiles({per_user}, " f"devicekey={device.DeviceKey!r}): " f"{exception}" ) if "-Sl" in args and ( sys.platform != "darwin" or intlist(platform.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(platform.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 if False: # if sys.platform == "darwin": # 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 ""', # '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. Args: profile (ICCProfile): The ICCProfile object to install. device_id (str): The device ID to use for installation. Returns: bool | colord.CDError: True if the profile was successfully installed, or a colord.CDError if an error occurred. """ 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): """Attempt to install a profile using colord with various quirks. Args: profile (ICCProfile): The ICCProfile object to install. device_id (str, optional): The device ID to use for installation. Returns: None | bool | colord.CDError: True if the profile was successfully installed, None if the device ID was not found, or a colord.CDError or colord.CDObjectQueryError if an error occurred. """ if not device_id: device_id = self.get_device_id(quirk=False, query=True) if not device_id: return None 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): if not device_id: continue # 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 # Either returned ok or there was another error break return result def _install_profile_gcm(self, profile): """Install profile using gcm-import. Args: profile (ICCProfile): The ICCProfile object to install. Returns: None: If the profile was successfully installed or already exists. """ 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 # 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 defaultpaths.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. Args: profile_path (str): Path to the profile file to install. profile_name (str, optional): Name to use for the installed profile. Defaults to None, in which case the basename of the profile file is used. capture_output (bool, optional): If True, captures the output of the command. Defaults to False. skip_scripts (bool, optional): If True, skips running scripts. Defaults to False. silent (bool, optional): If True, suppresses error messages. Returns: bool or Error: True on success, Error on failure. """ 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 defaultpaths.ICCPROFILES_DISPLAY_HOME: if os.path.isdir(dirname): # Use the first one that exists break dirname = None if not dirname: # Create the first one in the list dirname = defaultpaths.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. Args: silent (bool): If True, suppresses error messages. Returns: bool or Warning: True on success, Warning on error. """ 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 " f"v0.1b calibration loader '{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 " f"v0.2b calibration loader '{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 " f"v0.2b calibration loader '{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 " f"v0.2b calibration loader '{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 = current_thread() != main_thread() 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.CONFIG_HOME, 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 elif 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) ) elif 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. Returns: bool: True if the uninstallation was successful, False otherwise. """ 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. Args: silent (bool): If True, suppresses error messages. Returns: bool or Warning: True on success, Warning on error. """ # 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: 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 and ( # Move system-wide loader 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. Args: instrument_name (str): Optional name of the instrument to check. Returns: bool: True if the instrument supports CCSS, False otherwise. """ 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. Args: args (list): Additional arguments to pass to `ccxxmake`. working_dir (str): Directory to run the command in. Returns: subprocess.CompletedProcess: The result of the command execution. """ 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: str ) -> tuple[None, None] | tuple[float, dict]: """Generate gamut views (VRML files). Also show progress in current progress dialog. Args: profile_path (str): Path to the ICC profile for which to create gamut views. Returns: tuple[None, None] | tuple[float, dict]: A tuple containing the gamut_volume and gamut_coverage or (None, None) if gamut view creation is disabled. """ 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) 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. Args: dst_path (str): Path to save the profile to. If None, uses the default save path and profile name. skip_scripts (bool): If True, skips running scripts after profile creation. display_name (str): Optional display name for the profile. display_manufacturer (str): Optional manufacturer name for the display. tags (list): Optional list of tags to apply to the profile. Returns: str or Exception: The path to the created profile on success, or an Exception object on error. """ 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:0.0%} 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)) 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 (OSError, 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) black_XYZ = XYZbp if XYZbp else (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 (OSError, 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 (OSError, ICCProfileInvalidError) as exception: self.log(exception) else: if ( "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 ): # 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, dirname=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 = [(i / 10.0, 0, 0) for i in range(501)] 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 (OSError, 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, dirname=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 if 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) # noqa: S306 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 (OSError, 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] = [ interp(j / 4095.0 * 65535) for j in range(4096) ] # 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) ) num_workers = 1 if len(table.clut[0]) < 33 else None table.clut = functools.reduce( operator.iadd, 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 and (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 and ( "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(f"{args[-1]}.ti3.backup") and os.path.isfile( f"{args[-1]}.ti3" ): # Restore backed up TI3 os.rename(f"{args[-1]}.ti3", f"{args[-1]}.bpc.ti3") os.rename(f"{args[-1]}.ti3.backup", f"{args[-1]}.ti3") with open(f"{args[-1]}.ti3", "rb") as ti3_file: ti3 = ti3_file.read() 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 # noqa: E501 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. Args: ti3 (dict): The TI3 data. description (str): Profile description. copyright_ (str): Profile copyright. manufacturer (str): Profile manufacturer. model (str): Profile model. logfn (callable, optional): Function to log messages. clutres (int, optional): Resolution of the cLUT. cat (str, optional): Color appearance model. Returns: ICCProfile: The created ICC 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(f"L* {L:5.2f} a* {a:5.2f} b* {b:5.2f} dE*00 {dE:4.2f}") 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 = { 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 # NOTE: step must be (re)computed for the *target* clutres BEFORE # building RGB_in. Otherwise it still holds the previous low-res # iclutres step, so the RGB_in grid coordinates run far past device # 100% (e.g. 32 * 6.25 = 200), which clamps most lookups to the # white corner and produces a grossly corrupt A2B cLUT. step = 100 / (clutres - 1.0) RGB_in = [ [a * step, b * step, c * step] for a in range(clutres) for b in range(clutres) for c in range(clutres) ] 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 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( f"cLUT resolution {clutres:d}x{clutres:d}x{clutres:d}: Got " f"{actual - clut_actual:d} additional values from " "actual measurements" ) 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", ): """Create a simple matrix profile from primaries and gray. Args: ti3_RGB_XYZ (dict): A dictionary with RGB tuples as keys and XYZ tuples as values, representing the RGB to XYZ measurements. ti3_remaining (dict): A dictionary with RGB tuples as keys and XYZ tuples as values, representing the remaining RGB to XYZ measurements. desc (str): The description of the profile. copyright_ (str): The copyright of the profile. display_manufacturer (str, optional): The manufacturer of the display. display_name (str, optional): The name of the display. bpc (bool): If True, black point compensation will be applied. cat (str): The chromatic adaptation transform to use, default is 'Bradford'. Returns: ICCProfile: An ICCProfile with fileName set to None, containing the matrix tags created from the primaries and gray. """ 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)]: RGB_XYZ = ti3_RGB_XYZ if R == G == B else 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) return 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, ) def _create_matrix_profile( self, outname, profile=None, ptype="s", omit=None, bpc=False, cat="Bradford" ): """Create matrix profile from lookup through ti3. .ti3 has to exist. If is given, it has to be an ICCProfile instance, and the matrix tags will be added to this profile. should be the type of profile, i.e. one of g, G, s or S. 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. Args: outname (str): The base name of the output profile, without extension. profile (ICCProfile, optional): An ICCProfile instance to which the tags will be added. If not given, a new profile will be created. ptype (str): The type of profile to create, one of 'g', 'G', 's', or 'S'. omit (str, optional): If 'XYZ', the XYZ tags will not be created. bpc (bool): If True, black point compensation will be applied. cat (str): The chromatic adaptation transform to use, default is 'Bradford'. Returns: ICCProfile: An ICCProfile with fileName set to .ic[cm]. """ if profile: cat = profile.guess_cat() or cat tags = "shaper" if omit == "XYZ" else "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 (OSError, 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 if 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 = [(i / maxval,) * 3 for i in range(numentries)] # Inverse backward if "B2A0" in tags, otherwise forward direction = "ib" if "B2A0" in profile.tags else "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( f"Adding {tagname} from interpolation " f"to {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 OSError 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 OSError as exception: self.log(exception) if not result: return UnloggedError("\n".join(self.errors)) if isinstance(result, Exception): return result try: os.remove(f"{outname}.0.ti3") except OSError 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 OSError as exception: self.log(exception) if isinstance(result, Exception) or not result: return result try: matrix_profile = ICCProfile(fakeout + PROFILE_EXT) except (OSError, 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( f"Adding {tagname} from matrix profile " f"to {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 OSError as exception: self.log(exception) return profile def update_profile( self, profile: ICCProfile, ti3: None | CGATS = None, chrm: None | ChromaticityType = None, tags: None | bool | dict = None, avg: None | str = None, peak: None | str = None, rms: None | str = None, gamut_volume: None | float = None, gamut_coverage: None | float = None, quality: None | str = None, ): """Update profile tags and metadata. Args: profile (ICCProfile): The profile to update. ti3 (str or bytes, optional): Original TI3 data to embed. chrm (ChromaticityType, optional): Chromaticity data to add. tags (dict or bool, optional): Custom tags to add or True for default tags. avg (str, optional): Average error from profile check. peak (str, optional): Peak error from profile check. rms (str, optional): RMS error from profile check. gamut_volume (float, optional): Gamut volume of the profile. gamut_coverage (float, optional): Gamut coverage of the profile. quality (str, optional): Quality of the profile, e.g., 'v', 'l', 'm', 'h'. Returns: ICCProfile: The updated profile. """ if isinstance(profile, str): profile_path = profile try: profile = ICCProfile(profile_path) except (OSError, 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_STRING, } ) # 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.calculate_id() try: profile.write() except Exception as exception: return exception return True def get_logfiles(self, include_progress_buffers: bool = True) -> LineBufferedStream: """Get logfiles for logging. Args: include_progress_buffers (bool): Whether to include progress buffers in the returned logfiles. Defaults to True. Returns: LineBufferedStream: A stream that writes to the logfiles. """ 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: ICCProfile, generate_perceptual_table: bool = True, clutres: None | int = None, smooth: None | bool = None, rgb_space: None | str = None, ) -> list | Exception: """Update B2A tables in profile. Args: profile (ICCProfile): The ICC profile to update. generate_perceptual_table (bool): Whether to generate a perceptual B2A table if not present. clutres (int, optional): The resolution of the cLUT to generate. If not specified, it will be determined based on the profile settings. smooth (bool, optional): Whether to apply smoothing to the generated B2A table. Defaults to the value from the profile configuration. rgb_space (str, optional): The RGB color space to use for the B2A table. If not specified, the RGB space from the profile will be used. Returns: list | Exception: A list of generated B2A tables, or an exception if an error occurred. """ # 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}" not in profile.tags: continue 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) -> bool: """Check if any Worker instance is busy. Returns: bool: Return True if at least one of the Worker instances is busy, False if all are finished. """ return any(not getattr(worker, "finished", True) for worker in workers) def start_measurement( self, consumer: Callable, apply_calibration: None | bool | str = True, progress_msg: str = "", resume: bool = False, continue_next: bool = False, ) -> None: """Start a measurement and use a progress dialog for progress information. Args: consumer (callable): The consumer function to call with progress updates. apply_calibration (None | bool | str): If True, apply calibration to the measurements. If it is None or a str value, it will be passed as the `apply_calibration` argument to the `measure` method. progress_msg (str): Message to display in the progress dialog. resume (bool): If True, resume from the last measurement point. continue_next (bool): If True, continue to the next step after measurement. """ 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. Args: consumer (callable): The consumer function to call with progress updates. remove (bool): If True, remove existing calibration before starting. progress_msg (str): Message to display in the progress dialog. resume (bool): If True, resume from the last calibration point. continue_next (bool): If True, continue to the next step after calibration. """ 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): """Initialize madTPG connection.""" if hasattr(self, "madtpg"): return 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.MadTPGNet() self.madtpg.debug = VERBOSE def madtpg_connect(self): """Connect to madTPG and check version. Returns: bool: True if connected successfully, False otherwise. """ 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. Args: restore_settings (bool): If True, restore madTPG settings before disconnecting. If False, do not restore settings. """ 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 ): """Restore madTPG settings (fullscreen and OSD). Args: reconnect (bool): If True, re-connect to madTPG before restoring settings. If False, assume madTPG is already connected. restore_fullscreen (bool): If True, restore the fullscreen state of madTPG. If None, use the previous state stored in `madtpg_previous_fullscreen`. restore_osd (bool): If True, restore the OSD state of madTPG. If None, use the previous state stored in `madtpg_osd`. If both `restore_fullscreen` and `restore_osd` are None, no settings are restored. """ 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 not restore_fullscreen and not restore_osd: return 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. Args: msg (str): Message to show in OSD. If None, no message is shown. leave_fullscreen (bool): If True, leave fullscreen mode if currently in fullscreen mode. If False, stay in fullscreen mode. """ 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: bool = True) -> bool | Exception: """Measure the configured testchart. Args: apply_calibration (None | bool | str): If True, apply calibration to the measurement. If False, use the raw measurement data. If it is None or a string, it will be passed to the `prepare_dispread` method to prepare the command for measurement. Returns: bool | Exception : True if successful, or an Exception if an error occurred. """ 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 Args: ti1 (str or CGATS): Path to TI1 file or CGATS object. write (bool): If True, write the modified TI1 back to the original file. If False, return the modified CGATS object without writing. Returns: str | CGATS: The modified CGATS object if the patch sequence was changed, or the original path/TI1 if no changes were made. """ 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): """Parse the command output and check for specific conditions. Args: txt (str): Command output. """ 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): """Provide audio/visual feedback based on the command output. Args: txt (str): Command output. """ if isinstance(txt, bytes): txt = txt.decode() if self.cmdname not in ( get_argyll_utilname("dispcal"), get_argyll_utilname("dispread"), get_argyll_utilname("spotread"), ): return 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): """Set up the pattern generator instance based on the current configuration. Args: logfile (File, optional): Log file to use for the pattern generator. """ 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 (OSError, 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_pattern_generator 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): """Return the current pattern generator instance. Returns: PatternGenerator: Instance of a pattern generator class, such as PrismaPatternGeneratorClient or WebWinHTTPPatternGeneratorServer. """ pgname = config.get_display_name() return self._patterngenerators.get(pgname) @patterngenerator.setter def patterngenerator(self, patterngenerator): """Set the current pattern generator instance. Args: patterngenerator (PatternGenerator): Instance of a pattern generator class, such as PrismaPatternGeneratorClient or WebWinHTTPPatternGeneratorServer. """ pgname = config.get_display_name() self._patterngenerators[pgname] = patterngenerator @property def patterngenerators(self): """Return all pattern generators. Returns: dict: Dictionary of pattern generator instances, keyed by display """ return self._patterngenerators def patterngenerator_send( self, rgb, bgrgb=None, raise_exceptions=False, increase_sent_count=True ): """Send RGB color to pattern generator. Args: rgb (tuple): RGB color to send, as a tuple of three floats in the range [0, 1]. bgrgb (tuple, optional): Background RGB color to send, as a tuple of three floats in the range [0, 1]. Defaults to None. raise_exceptions (bool, optional): Whether to raise exceptions on failure. Defaults to False. increase_sent_count (bool, optional): Whether to increase the sent count. Defaults to True. Returns: None or Exception: Returns None if successful, or an Exception if an error occurred. """ 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 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 whether the current measurement or calibration is pauseable. Returns: bool: True if the current measurement or calibration is pauseable, False otherwise. """ return self._pauseable @pauseable.setter def pauseable(self, pauseable): """Set whether the current measurement or calibration is pauseable.""" self._pauseable = pauseable self.pauseable_now = False def pause_continue(self): """Pause or continue the current measurement or calibration.""" 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 OSError 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": value = "g" if value else "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 = ( f"3DLUT_CONTENT_COLORSPACE_{color.upper()}_{coord.upper()}" ) if getcfg("3dlut.create"): value = getcfg(f"3dlut.content.colorspace.{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 if 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(f"-a{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. args.append("-b") 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). Args: apply_calibration (None | bool | str): If True, apply the current calibration (i.e. the one created by dispcal). If a string, it should be a path to a .cal file to apply. If None, use the 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 (OSError, 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(f"{filename}{ext}", f"{inoutfile}.ti1") else: # ti3 try: if ext.lower() != ".ti1": with open(f"{filename}{ext}", "rb") as ti3: ti3_lines = [line.strip() for line in ti3] 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: return ( Error( lang.getstr( "error.testchart.read", getcfg("testchart.file") ) ), None, ) 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 ( OSError, 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 (OSError, 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: with open(calcopy, "w") as tmpcal: tmpcal.write(extract_cal_from_ti3(ti3_lines)) 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) elif 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 (OSError, 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.decode("utf-8"), profile.colorSpace.decode("utf-8"), ), ) + "\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(defaultpaths.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"-f{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 with contextlib.suppress(ValueError): percentage = int(self.lastmsg.read().split("%")[0]) 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 and not getattr(getattr(self, "terminal", None), "is_measuring", False) ): # 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(f"{msg}\n{lastmsg}") elif 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(current_thread(), "name") or current_thread().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: " f"{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: " f"{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. Args: report_calibrated (bool): If True, report on calibrated display device response, otherwise report on uncalibrated display device response. Returns: str | Exception: a string containing the report if successful, an Exception if an error occurs. """ 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): """Reset the current display calibration. Returns: None | Exception: None if successful, or an Exception if an error occurs. """ cmd, args = self.prepare_dispwin(False) return self.exec_cmd( cmd, args, capture_output=True, skip_scripts=True, silent=False ) def safe_send(self, data): """Send data to the current subprocess, retrying if necessary. Returns: bool: Always send True. """ self.send_buffer = data return True def _safe_send(self, data, retry=3, obfuscate=False): """Safely send a keystroke to the current subprocess. Args: data (str): Data to send to the subprocess. retry (int): Number of times to retry sending the data. obfuscate (bool): If True, obfuscate the data in the log output. Returns: bool: True if the data was sent successfully, False otherwise. """ for i in range(retry): logbytes = "***" if obfuscate else data self.logger.info(f"Sending key(s) {logbytes} ({i + 1:d})") try: if sys.platform == "win32": # directly send the bytes_ without encoding to anything self.subprocess.send(data) else: # for Linux and MacOS encode to bytes self.subprocess.send(data.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. Args: display_no (int): Display number to save the videoLUT for. outfilename (str): Output filename to save the videoLUT to. interpolate_to_256 (bool): If True, interpolate the videoLUT to 256 entries. silent (bool): If True, do not print any messages. Returns: None | Exception: None if successful, or an Exception if an error occurs. """ 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(f"VideoLUT has {len(data):d} entries, interpolating to 256") 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): """Set the Argyll CMS version from a name. Args: name (str): Name of the Argyll CMS version to set. silent (bool): If True, do not print the version string. cfg (bool): If True, write the version to the configuration. """ 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): """Set the Argyll CMS version from a version string. Args: argyll_version_string (str): The version string to set. cfg (bool): If True, write the version to the configuration. """ 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) -> None: """Set the session logfile for the current session. Args: sessionlogfile (None | LogFile): LogFile object for the session log, or None to create a new one. basename (str): Basename for the session log file. dirname (str): Directory where the session log file will be stored. """ if sessionlogfile: self.sessionlogfile = sessionlogfile else: self.sessionlogfile = LogFile(basename, dirname) self.sessionlogfiles[basename] = self.sessionlogfile def set_terminal_cgats(self, cgats): """Set the CGATS object for the terminal. Args: cgats (CGATS): CGATS object. Returns: None | Exception: None if successful, or an Exception if an error occurs. """ if not isinstance(cgats, CGATS): try: cgats = CGATS(cgats) except ( OSError, CGATSInvalidError, CGATSInvalidOperationError, CGATSKeyError, CGATSTypeError, CGATSValueError, ) as exception: return exception self.terminal.cgats = cgats return None def setup_inout(self, basename=None): """Setup in/outfile basename and session logfile. Args: basename (None | str): Basename for the in/output file(s). If None, it will use the expanded profile name from the configuration. Returns: str | Exception: Full path to the in/output file(s) or an Exception if an error occurs. """ 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, is_name=True) self.set_sessionlogfile(None, basename, dirname) return os.path.join(dirname, basename) def single_real_display(self): """Check if there is only one real display connected. Returns: bool: True if there is only one real display, False otherwise. """ return len(self.get_real_displays()) == 1 def argyll_support_file_exists(self, name, scope=None): """Check if file exists in Argyll support locations for current version. Args: name (str): Name of the file to check for. scope (None | str): Scope can be 'u' (user), 'l' (local system) or None (both) Returns: bool: True if the file exists, False otherwise. """ 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) return any(os.path.isfile(searchpath) for searchpath in searchpaths) def spyder2_firmware_exists(self, scope=None): """Check if the Spyder 2 firmware file exists for the current Argyll version. Args: scope (None | str): Scope can be 'u' (user), 'l' (local system) or None (both). Returns: bool: True if the Spyder 2 firmware file exists, False otherwise. """ 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") ) return self.argyll_support_file_exists("spyd2PLD.bin", scope=scope) def spyder4_cal_exists(self): """Check if Spyder4/5 calibration file exists for current Argyll version. Returns: bool: True if the Spyder4/5 calibration file exists, False otherwise. """ 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, ): """Start a worker process with a progress dialog. Args: consumer (callable): Consumer function to handle results. producer (callable): Producer function to run in a separate thread. cargs (tuple): Arguments for the consumer function. ckwargs (dict): Keyword arguments for the consumer function. wargs (tuple): Arguments for the producer function. wkwargs (dict): Keyword arguments for the producer function. progress_title (str): Title for the progress dialog. Defaults to '{APPNAME}'. progress_msg (str): Message for the progress dialog. Defaults to ''. parent (wx.Window): Parent window for the progress dialog. progress_start (int): Delay before showing the progress dialog (ms). resume (bool): Resume previous progress dialog (elapsed time etc). continue_next (bool): Do not hide progress dialog after producer finishes. stop_timers (bool): Stop the timers on the owner window if True. interactive_frame (str or wx.TopLevelWindow): Type of interactive window or a wx.TopLevelWindow instance. pauseable (bool): Is the operation pauseable? Show pause button on the progress dialog. cancelable (bool): Is the operation cancelable? Show cancel button on the progress dialog. show_remaining_time (bool): Show remaining time in the progress dialog. fancy (bool): Use fancy progress dialog with animated throbber & sound effects. Returns: None: If the worker thread is already running. bool: True if the worker thread was started successfully, """ # 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 progress_start = max(progress_start, 1) # Can't be zero! self.activated = False self.cmdname = None self.cmdrun = False self.finished = False self.instrument_calibration_complete = False self._last_calibration_msg = None 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 in {"ambient", "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, DisplayUniformityFrame, 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): """Stop the progress dialog and clean up.""" 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 # occurred 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. Args: event (wx.Event): The key event to handle. """ 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 with contextlib.suppress(Exception): self.safe_send(chr(keycode)) def calculate_gamut( self, profile_paths, 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. Args: profile_paths (str or list): Path(s) to ICC profile(s) to calculate gamut from. intent (str): Rendering intent, one of "r" (relative colorimetric), "s" (saturation), "m" (perceptual), "a" (absolute colorimetric). direction (str): Gamut direction, one of "f" (forward), "b" (backward). order (str): Gamut order, one of "n" (normal), "i" (interpolated), "c" (continuous). compare_standard_gamuts (bool): If True, compare gamut against standard gamuts (sRGB, Adobe RGB, DCI-P3) and create intersection VRML files. If False, only create gamut VRML files for the provided profile(s). Returns: tuple[float, dict]: A tuple containing the gamut_volume and gamut_coverage. """ if not isinstance(profile_paths, list): profile_paths = [profile_paths] 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([f"[{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 ) return vrml.replace(b"\0$", b"100") 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, open(tmpfilename, "rb") as infile, ): # Always use original filename with '.gz' extension, # that way the filename in the header will be correct 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 ) -> None: """Gamut view creation producer. Args: worker (Worker): Worker instance to execute the command. viewgam (str): Path to the viewgam executable. args (list): Arguments for the viewgam command. key (str): Key for the gamut coverage dictionary. src (str): Source file name for the gamut view. gamut_coverage (dict): Dictionary to store the coverage percentage. """ 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( rf"[/\\]{re.escape(src)}.gam'\s+volume\s*=\s*" r"\d+(?:\.\d+)?\s+cubic\s+units,?" r"\s+intersect\s*=\s*" r"(\d+(?:\.\d+)?)", 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). Args: remove (bool): If True, remove the generated calibration files after processing. Defaults to False. Returns: result (Exception or bool): Returns an Exception if an error occurs, or True if the calibration was successful. If no calibration was performed, it returns False. """ 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): max_diffuser_retries = 2 diffuser_retries = 0 while True: result = self.exec_cmd(cmd, args, capture_output=capture_output) # Argyll's specbos driver spawns a background "Diffuser thread" # that polls the instrument over the same serial port as # measurements, causing a race condition. When this happens # dispcal exits with "Instrument Access Failed". Restarting # dispcal opens a fresh serial connection and usually avoids # the race on subsequent attempts. if ( isinstance(result, Exception) and not self.subprocess_abort and diffuser_retries < max_diffuser_retries and "Instrument Access Failed" in str(result) and any( "Diffuser thread failed" in line for line in self.output ) ): diffuser_retries += 1 self.log( f"{APPNAME}: Calibration failed due to instrument " "communication issue (Diffuser thread conflict). " f"Retrying (attempt {diffuser_retries} of " f"{max_diffuser_retries})..." ) continue break 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"), ) cal = args[-1] + ".cal" result = check_cal_isfile( cal, lang.getstr("error.calibration.file_not_created") ) 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 (OSError, 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.calculate_id() 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): """Check if loading/clearing calibration is generally supported. Returns: bool: True if loading/clearing calibration is generally supported, False otherwise. """ # 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(platform.mac_ver()[0].split(".")) < [10, 6] ) and os.getenv("XDG_SESSION_TYPE") != "wayland" @property def calibration_loading_supported(self): """Check if loading/clearing calibration is supported. Returns: bool: True if loading/clearing calibration is supported, False otherwise. """ # 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 = [[i / 255.0] * 3 for i in range(256)] 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 = [[v * (1 - i / 1999.0) for v in XYZw] for i in range(2000)] 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( f"Solution found at index {i:d} (step size {1 / 1999.0:f})\n" ) 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(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 RGB in 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"{i / 255.0:f} {R:f} {G:f} {B:f}\n" 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.calculate_id() profile.write(outfilename) self.wrapup(False) return True # Re-create profile cti3 = None if isinstance(profile.tags.get("targ"), Text): # Get measurement data try: cti3 = CGATS(profile.tags.targ) except (OSError, 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 sample in cti3[0].DATA.values(): RGB = [] for component in "RGB": RGB.append(sample[f"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) and lookup device->pcs values. Lookup device->pcs values colorimetrically through profile using Argyll's xicclu utility and return TI3 (CGATS instance). Args: ti1 (str or CGATS): Filename of TI1 file or CGATS instance. profile (ICCProfile): ICC profile to use for lookup. function (str): Function to use for lookup, 'f' for forward, 'b' for backward, 'a' for adapt. pcs (str): PCS color space, 'l' for Lab, 'x' for XYZ. intent (str): Rendering intent, 'r' for relative colorimetric, 'a' for absolute colorimetric. white_patches (int): Number of white patches to add to the TI1. white_patches_total (bool): If True, ensure total of n white patches, if False, add exactly n white patches. Raises: TypeError: If `ti1` is not a CGATS instance or a valid filename. ValueError: If `ti1` is invalid or missing required fields. Returns: tuple: A tuple containing: - CGATS instance of TI3 with device->pcs values. - CGATS instance of TI3 with reference values (if available). - Gray patch data (if available). """ # 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 {ti1.__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) elif 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 as e: raise ValueError( lang.getstr("error.testchart.invalid", ti1_filename) ) from e except CGATSKeyError as e: raise ValueError( lang.getstr( "error.testchart.missing_fields", (ti1_filename, ", ".join(required)), ) ) from e # 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(f"Added {white_added_count:d} white patch(es)") idata = [list(primaries.values()) for primaries in list(device_data.values())] if DEBUG: print(f"ti1_lookup_to_ti3 {profile.colorSpace} -> {color_rep} idata") for v in idata: print(" ".join(("{:3.4f}",) * len(v)).format(*v)) # lookup device->cie values through profile using (x)icclu # # Under older ArgyllCMS versions, we have to use DeviceLink profile with # icclu, because older xicclu cannot handle devicelink use_icclu = not (pcs or self.argyll_version >= [1, 6]) 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 False: # if igray: # 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) and lookup cie->device values. Lookup cie->device values colorimetrically through profile using Argyll's xicclu utility and return TI1 and compatible TI3 (CGATS instances). Args: ti3 (str or CGATS): TI3 filename or CGATS instance. profile (str or ICCProfile): Profile filename or ICCProfile instance. fields (str): CIE color representation to use, either "XYZ" or "LAB". intent (str): Rendering intent to use, either "r" for relative colorimetric or "a" for absolute colorimetric. add_white_patches (int): Number of white patches to add to the TI1/ti3 file. If set to 0, no white patches are added. If set to a positive integer, the first n patches will be white, so the whitepoint can be averaged. Defaults to 4. Raises: TypeError: If `ti3` is not a CGATS instance or a valid filename. ValueError: If `ti3` is invalid or missing required fields. Returns: tuple: A tuple containing: - CGATS instance of TI1 with device->cie values. - CGATS instance of TI3 with device->cie values. - Gray patch data (if available). """ # 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( f"{lang.getstr('error.testchart.invalid', ti3_filename)}\n" f"{lang.getstr(str(exception))}" ) from exception except CGATSKeyError: try: if fields: raise labels = ("XYZ_X", "XYZ_Y", "XYZ_Z") ti3v = verify_cgats(ti3, labels, True) except CGATSKeyError as e: missing = ", ".join(labels) if not fields: missing = f"{missing} {lang.getstr('or')} LAB_L, LAB_A, LAB_B" raise ValueError( lang.getstr( "error.testchart.missing_fields", (ti3_filename, missing) ) ) from e 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 = {"L": wp[0], "a": wp[1], "b": wp[2]} else: wp = {"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 olabels[2] not in list(ti3v.DATA_FORMAT.values()) and ( ocolor == b"RGB" or ( ocolor == b"CMYK" and olabels[3] not in list(ti3v.DATA_FORMAT.values()) ) ) ): ti3v.DATA_FORMAT.add_data(olabels) # add required fields to DATA_FORMAT if not yet present if ( bytes(required[0], "utf-8") not in list(ti3v.DATA_FORMAT.values()) and bytes(required[1], "utf-8") not in list(ti3v.DATA_FORMAT.values()) and bytes(required[2], "utf-8") not 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): device = [100.00, 100.00, 100.00] if ocolor == b"RGB" else [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): """Download a file from the specified URI. Args: uri (str): The URI of the file to download. force (bool, optional): If True, forces a re-download even if the file exists locally. Defaults to False. download_dir (str, optional): Directory to save the downloaded file. If None, uses a default download directory. """ # Set timeout to a sane value default_timeout = socket.getdefaulttimeout() socket.setdefaulttimeout(20) # 20 seconds try: max_attempts = 3 retry_wait = 1.0 for attempt in range(1, max_attempts + 1): result = self._download(uri, force=force, download_dir=download_dir) if not isinstance(result, Exception): return result if ( self.thread_abort or attempt >= max_attempts or not self._is_retryable_download_error(result) ): return result wait_seconds = retry_wait + random.uniform(0, 0.25) print( f"{result}\n" f"{lang.getstr('download.retrying', (attempt + 1, max_attempts, wait_seconds))}" ) sleep(wait_seconds) retry_wait *= 2 # Ensure next attempt refreshes server state and local file. force = True finally: socket.setdefaulttimeout(default_timeout) @staticmethod def _is_retryable_download_error(error: Exception) -> bool: """Return True for network/transient download errors.""" return bool(getattr(error, "retryable", False)) def _download(self, uri, force=False, download_dir=None): """Download a file from the specified URI. Optional hash verification and progress reporting are available. This method handles downloading files (such as ArgyllCMS archives) from a given URI. It supports forced downloads, custom download directories, and can verify file hashes if available. The method also manages HTTP redirects, SSL certificate verification, and provides progress feedback during the download. Notes: - For certain domains, the method always forces a download to ensure hash checking. - Handles SSL certificate errors and HTTP redirects. - If the file already exists and matches the expected size, it is not re-downloaded unless forced. - If hash verification is enabled, the method checks the downloaded file's hash. - Provides progress updates during the download process. Args: uri (str): The URI of the file to download. force (bool, optional): If True, forces a re-download even if the file exists locally. Defaults to False. download_dir (str, optional): Directory to save the downloaded file. If None, uses a default download directory. Returns: str: The path to the downloaded file if successful. Exception: An Exception object if the download fails. """ 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.DATA_HOME, "dl") download_path = os.path.join(download_dir, filename) response = None hashes = None expectedhash_hex = None actualhash = None def make_download_error(message: str, retryable: bool = False) -> DownloadError: error = DownloadError(message, orig_uri) error.retryable = retryable return error 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()) argyllcms_binaries_base = ( "https://github.com/eoyilmaz/argyllcms-binaries/releases/download/" ) try: response = opener.open(uri) if ALWAYS_FAIL_DOWNLOAD or TEST_BADSSL: raise urllib.error.URLError("") newurl = getattr(LoggingHTTPRedirectHandler, "newurl", uri) if not orig_uri.startswith(argyllcms_binaries_base) and ( 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). argyllcms-binaries uses the GitHub # API digest field instead (handled below). noredir = urllib.request.build_opener(NoHTTPRedirectHandler) noredir.addheaders = list(get_default_headers().items()) hashes = noredir.open(f"https://{DOMAIN}/sha256sums.txt") except ( OSError, 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): exception.retryable = True return exception else: print(exception) if getattr(LoggingHTTPRedirectHandler, "newurl", uri) != uri: uri = LoggingHTTPRedirectHandler.newurl return make_download_error( lang.getstr("download.fail") + " " + lang.getstr("connection.fail", uri), retryable=True, ) uri = response.geturl() filename = os.path.basename(Path(uri).name) actualhash = sha256() if orig_uri.startswith(argyllcms_binaries_base): # Use the GitHub API digest field, available for every release # asset without any separate upload step. # URL: .../releases/download/{version}/{filename} path_tail = orig_uri[len("https://github.com/"):] parts = path_tail.split("/") # parts: ["eoyilmaz", "argyllcms-binaries", "releases", "download", version, filename] if len(parts) >= 6: version, asset_name = parts[4], parts[5] api_url = ( "https://api.github.com/repos/eoyilmaz/argyllcms-binaries" f"/releases/tags/{version}" ) try: import json api_resp = urllib.request.urlopen(api_url, timeout=20) # noqa: S310 release_data = json.loads(api_resp.read()) for asset in release_data.get("assets", []): if asset["name"] == asset_name: digest = asset.get("digest", "") if digest.startswith("sha256:"): expectedhash_hex = digest[7:].lower() break except (urllib.error.URLError, OSError, KeyError, ValueError): pass if hashes: # 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 b"" in name_hash: response.close() return make_download_error( lang.getstr("file.hash.malformed", filename), retryable=False, ) hash_filename = Path( name_hash[1].decode("utf-8", "replace").lstrip("*") ) hashesdict[hash_filename.name] = ( name_hash[0].decode("ascii", "replace").lower() ) expectedhash_hex = hashesdict.get(filename) if not expectedhash_hex: response.close() return make_download_error( lang.getstr("file.hash.missing", filename), retryable=False ) total_size = response.info().get("Content-Length") if total_size is not None: try: total_size = int(total_size) except (TypeError, ValueError): return make_download_error( lang.getstr( "download.fail.wrong_size", ("<{}>".format(lang.getstr("unknown")),) * 2, ), retryable=True, ) else: if not total_size: total_size = None contentdispo = response.info().get("Content-Disposition") if contentdispo: m = re.search(r'filename[*]?=(?:"([^"]+)"|([^\s;]+))', contentdispo, re.I) if m: filename = (m.group(1) or m.group(2)).strip() 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 download_path = mksfile(download_path) tmp_download_path = mksfile(f"{download_path}.download") 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 and total_size > 1048576: unit = "MiB" unit_size = 1048576.0 elif total_size and 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_exception = None download_succeeded = False try: with open(tmp_download_path, "wb+") 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) if expectedhash_hex: actualhash.update(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: bps = bytes_so_far / tdiff if tdiff else 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 = math.floor(float(bytes_so_far) / total_size * 100) if ( percent > prev_percent or time() >= update_ts + frametime ): self.lastmsg.write( f"\r{percent:d}% ({bytes_so_far / unit_size:.1f} " f"/ {total_size / unit_size:.1f} {unit}, " f"{round(bps / bps_unit_size, 2):.2f} {bps_unit}/s)" ) 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( f"\r{bytes_so_far / unit_size:.1f} {unit} " f"({round(bps / bps_unit_size, 2):.2f} " f"{bps_unit}/s)" ) 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 " f"{chunk_size / 1024.0:0.0f} KB to " f"{bps / fps / 1024:0.0f} KB" ) chunk_size = int(bps / fps) if not bytes_so_far: return make_download_error( lang.getstr("download.fail.empty_response", uri), retryable=True, ) if total_size is not None and bytes_so_far != total_size: return make_download_error( lang.getstr( "download.fail.wrong_size", (total_size, bytes_so_far) ), retryable=True, ) if total_size is None or bytes_so_far == total_size: # Successful download, persist and atomically place file. download_succeeded = True tmp_download_file.flush() os.fsync(tmp_download_file.fileno()) if download_succeeded: try: os.replace(tmp_download_path, download_path) except OSError as e: return e print(lang.getstr("success")) finally: if response: response.close() if ( self.thread_abort or download_file_exception or not download_succeeded ): # Remove destination file if download aborted, incomplete # or there was an error writing destination. try: os.remove(download_path) except OSError as exception: print(exception) if not download_succeeded: try: os.remove(tmp_download_path) except OSError as exception: print(exception) else: # File already exists if response: response.close() if expectedhash_hex: # 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 expectedhash_hex: # Verify hash. Compare to expected hash actualhash_hex = actualhash.hexdigest().lower() if actualhash_hex != expectedhash_hex: if os.path.isfile(download_path): os.remove(download_path) return Error( lang.getstr( "file.hash.verification.fail", ( download_path, f"SHA-256= {expectedhash_hex}", f"SHA-256= {actualhash_hex}", ), ) ) print(f"Verified hash SHA-256= {actualhash_hex}") return download_path def process_argyll_download(self, result, exit_=False): """Handle the result of an ArgyllCMS download operation. This method processes the outcome of downloading an ArgyllCMS archive (ZIP or TGZ). If the result is an Exception, it displays an error dialog. If the result is a valid archive file, it starts extraction of the archive and sets the Argyll bin directory upon success. If the result is not a supported archive file, it displays an error dialog. Side Effects: - May display dialogs to the user. - May start extraction of the downloaded archive. - May update the Argyll bin directory configuration. Args: result (str or Exception): The result of the download operation. This can be a file path to the downloaded archive or an Exception if the download failed. exit_ (bool, optional): If True, closes the owner window or exits the application after processing. Defaults to False. """ if isinstance(result, Exception): if isinstance(result, DownloadError): # DownloadError would otherwise force a "go to URL" prompt. retry = show_result_dialog( Error(str(result)), self.owner, confirm=lang.getstr("retry"), ) if retry: self._reopen_argyll_download_prompt() else: 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): """Extract the contents of a ZIP or TGZ archive file. Notes: - For ZIP files, files are extracted using the `zipfile.ZipFile` class. - For TGZ files, files are extracted using the `TarFileProper.open` method. - The method ensures that all extracted files remain within the intended output directory to prevent directory traversal vulnerabilities. - If the archive is invalid or extraction fails, the method may delete the archive file. Args: filename (str): The path to the archive file to extract. Supported formats are .zip and .tgz. Raises: EOFError: If the archive file is incomplete or corrupted (e.g., unexpected end of file). Exception: If a file in the archive would be extracted outside the intended directory. Returns: list: A list of extracted file paths. If the archive is invalid or extraction fails, returns an empty list or an Exception. """ 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 try: with cls(filename, mode) as z: outdir = os.path.realpath(os.path.dirname(filename)) method = z.getnames if cls is not zipfile.ZipFile else z.namelist names = method() 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)) except Exception: # Corrupt/incomplete archive. Delete it so a retry gets a clean file. if os.path.isfile(filename): print(lang.getstr("archive.extract.fail.delete", filename)) os.remove(filename) raise return extracted def _reopen_argyll_download_prompt(self): """Re-open the Argyll download prompt after a failed archive download.""" if not self.owner: return from DisplayCAL.display_cal import app_update_check app_update_check(self.owner, argyll=True) def set_argyll_bin(self, result, filename): """Set Argyll bin directory. Args: result (str | Exception): Either a str showing the result message or an Exception. filename (str): The archive filename. """ if isinstance(result, Exception): if not os.path.isfile(filename): retry = show_result_dialog( result, self.owner, confirm=lang.getstr("retry"), ) if retry: self._reopen_argyll_download_prompt() else: 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 = False 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): """Process download. Args: result (str, Exception): Either a str or an Exception to process. exit_ (bool): If True close the owner window if any otherwise close the main application. If False do not do anything. """ 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. Returns: str or Exception: The result of the verification, which can be a str indicating success or an Exception if an error occurred. """ 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. Args: ti1_path (str): Path to the TI1 file to measure. cal_path (str): Path to the calibration file, if any. colormanaged (bool): Whether the measurement is color managed. allow_video_levels (bool): Whether to allow video levels in the measurement. Returns: str or Exception: The path to the measured TI1 file, or an Exception if an error occurred. """ 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")) if sys.platform == "win32": args = quote_args(args) result = self.add_measurement_features( 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). Args: copy (bool or Exception): If True, copy temporary files to `dst_path` (if given) or to the default save path. If False, do not copy temporary files. remove (bool): If True, remove temporary files. dst_path (str): Destination path to copy temporary files to. ext_filter (list of str): List of file extensions to filter out when copying temporary files. If None, defaults to a predefined list of common file extensions. Returns: bool or Exception: True if successful, False if nothing to do, or an Exception if an error occurred. """ if DEBUG: print(f"[D] wrapup(copy={copy}, remove={remove})") if not self.tempdir or not os.path.isdir(self.tempdir): return None # 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.DATA_HOME, "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}' " f"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}' " f"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 elif 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): """Write text to the terminal. Args: txt (str): Text to write. """ if True: # Don't buffer self._write(txt) return # NEVER - Use line buffer self.buffer.append(txt) self.buffer = list(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) and ( # There are some intial measurements which we can't check for # unless -D (debug) is used for Argyll tools "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 |" r"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 and ( # Check if patch count is higher than patterngenerator sent count 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 )