import subprocess import math import os import sys import re def get_channel_count(profile): res = subprocess.run( ["../icc/iccdump", profile], capture_output=True, text=True, check=True ) m = re.search(r"Color Space\s+=\s+(\d+)\s+Color", res.stdout) if m: return int(m.group(1)) if "Color Space = CMYK" in res.stdout: return 4 if "Color Space = RGB" in res.stdout: return 3 raise RuntimeError(f"Could not determine channel count from iccdump output") def run_forward(profile, device_coords): inp = " ".join(f"{x:.6f}" for x in device_coords) + "\nq\n" res = subprocess.run( ["../xicc/xicclu", "-ff", "-ir", profile], input=inp, text=True, capture_output=True, check=True ) for line in res.stdout.strip().split("\n"): if "-> Lut ->" in line: parts = line.split("-> Lut ->")[1].strip().split() return [float(parts[0]), float(parts[1]), float(parts[2])] raise RuntimeError(f"Could not parse: {res.stdout}") def run_backward(profile, lab_coords): inp = " ".join(f"{x:.6f}" for x in lab_coords) + "\nq\n" res = subprocess.run( ["../xicc/xicclu", "-fb", "-ir", profile], input=inp, text=True, capture_output=True, check=True ) for line in res.stdout.strip().split("\n"): if "-> Lut ->" in line: parts = line.split("-> Lut ->")[1].strip().split() vals = [] for token in parts: try: vals.append(float(token)) except ValueError: break return vals raise RuntimeError(f"Could not parse: {res.stdout}") def delta_e(lab1, lab2): return math.sqrt(sum((a - b)**2 for a, b in zip(lab1, lab2))) def generate_test_cases(nchan): names = ["Cyan", "Magenta", "Yellow", "Black", "Orange", "Green", "Violet"] cases = [ ("White", [0.0] * nchan) ] # Single inks at 50% for i in range(nchan): cname = names[i] if i < len(names) else f"Ink {i+1}" vec = [0.0] * nchan vec[i] = 0.5 cases.append((f"{cname} 50%", vec)) # Mixed CMY vec = [0.0] * nchan vec[0] = 0.2 vec[1] = 0.3 vec[2] = 0.1 cases.append(("Mixed CMY", vec)) # Extra inks mix if nchan >= 6: vec = [0.0] * nchan vec[4] = 0.2 vec[5] = 0.3 if nchan >= 7: vec[6] = 0.1 cases.append(("Mixed Extra", vec)) # All-channel mix val = round(0.7 / nchan, 2) cases.append((f"Mixed {nchan}ch", [val] * nchan)) return cases def main(): profile = sys.argv[1] if len(sys.argv) > 1 else "test_7clr.icc" script_dir = os.path.dirname(os.path.abspath(__file__)) os.chdir(script_dir) nchan = get_channel_count(profile) test_cases = generate_test_cases(nchan) header = f"{'Patch':15} | {'Forward Lab':20} | {'Backward Dev':35} | {'Roundtrip Lab':20} | {'dE':5}" print(f"=== Roundtrip Verification for {profile} ({nchan} Channels) ===") print(header) print("-" * len(header)) de_list = [] for name, dev in test_cases: fwd_lab = run_forward(profile, dev) bwd_dev = run_backward(profile, fwd_lab) rt_lab = run_forward(profile, bwd_dev) de = delta_e(fwd_lab, rt_lab) de_list.append(de) fwd_str = f"[{fwd_lab[0]:5.1f}, {fwd_lab[1]:5.1f}, {fwd_lab[2]:5.1f}]" rt_str = f"[{rt_lab[0]:5.1f}, {rt_lab[1]:5.1f}, {rt_lab[2]:5.1f}]" bwd_str = " ".join(f"{x:4.2f}" for x in bwd_dev) print(f"{name:15} | {fwd_str:20} | {bwd_str:35} | {rt_str:20} | {de:5.2f}") print("-" * len(header)) print(f"Summary: avg dE = {sum(de_list)/len(de_list):.2f}, max dE = {max(de_list):.2f}\n") if __name__ == "__main__": main()