import sys import os def xyz_to_lab(X, Y, Z): """CIE XYZ to CIELAB (D50 illuminant)""" Xn, Yn, Zn = 96.42, 100.0, 82.49 # D50 def f(t): if t > 0.008856: return t ** (1.0/3.0) else: return 7.787 * t + 16.0/116.0 fx = f(X / Xn) fy = f(Y / Yn) fz = f(Z / Zn) L = 116.0 * fy - 16.0 a = 500.0 * (fx - fy) b = 200.0 * (fy - fz) return L, a, b def convert(ti1_path, ti3_path): with open(ti1_path, "r") as fin: lines = fin.readlines() color_rep = None data_rows = [] first_format = None in_format = False in_data = False for line in lines: stripped = line.strip() if stripped.startswith("COLOR_REP"): parts = stripped.split() if len(parts) >= 2 and color_rep is None: color_rep = parts[1].replace('"', '') elif stripped == "BEGIN_DATA_FORMAT": in_format = True continue elif stripped == "END_DATA_FORMAT": in_format = False continue elif in_format and stripped: if first_format is None: first_format = stripped.split() elif stripped == "BEGIN_DATA": in_data = True continue elif stripped == "END_DATA": in_data = False continue elif in_data and stripped: data_rows.append(stripped.split()) # From first_format, figure out dev_fields # first_format is like: ['SAMPLE_ID', 'CMYKO_C', 'CMYKO_M', 'CMYKO_Y', 'CMYKO_K', 'CMYKO_O', 'XYZ_X', 'XYZ_Y', 'XYZ_Z'] dev_fields = [] for token in first_format[1:]: if token in ("XYZ_X", "XYZ_Y", "XYZ_Z", "LAB_L", "LAB_A", "LAB_B"): break dev_fields.append(token) num_dev = len(dev_fields) ti3_color_rep = f"{color_rep}_LAB" field_names = ["SAMPLE_ID"] + dev_fields + ["LAB_L", "LAB_A", "LAB_B"] with open(ti3_path, "w") as fout: fout.write("CTI3 \n\n") fout.write('DESCRIPTOR "Argyll Calibration Target chart information 3"\n') fout.write('ORIGINATOR "Synthetic test data for multichannel verification"\n') fout.write('DEVICE_CLASS "OUTPUT"\n') fout.write('APPROX_WHITE_POINT "96.420000 100.000000 82.490000"\n') fout.write(f'COLOR_REP "{ti3_color_rep}"\n\n') fout.write(f"NUMBER_OF_FIELDS {len(field_names)}\n") fout.write("BEGIN_DATA_FORMAT\n") fout.write(" ".join(field_names) + "\n") fout.write("END_DATA_FORMAT\n\n") fout.write(f"NUMBER_OF_SETS {len(data_rows)}\n") fout.write("BEGIN_DATA\n") for idx, row in enumerate(data_rows, 1): # row: [id, dev_0, ..., dev_{num_dev-1}, X, Y, Z] dev_vals = row[1:1+num_dev] X = float(row[-3]) Y = float(row[-2]) Z = float(row[-1]) L, a, b = xyz_to_lab(X, Y, Z) line_parts = [str(idx)] + [f"{float(v):.5f}" for v in dev_vals] + [f"{L:.4f}", f"{a:.4f}", f"{b:.4f}"] fout.write(" ".join(line_parts) + "\n") fout.write("END_DATA\n") print(f"Successfully converted {ti1_path} -> {ti3_path}: {len(data_rows)} patches, {num_dev} channels ({ti3_color_rep})") if __name__ == "__main__": if len(sys.argv) < 3: print("Usage: python3 ti1_to_ti3.py input.ti1 output.ti3") sys.exit(1) convert(sys.argv[1], sys.argv[2])