Debian Codegraph @

DisplayCAL/ccmx.py

raw
1 """Convert iColorDisplay DeviceCorrections.txt to Argyll CCMX files.
2
3 It parses the input file, extracts relevant data, and generates CCMX files in
4 the specified format for use with color calibration tools.
5 """
6
7 import json
8 import os
9 import sys
10 import time
11
12 CCMX_TEMPLATE = """CCMX
13
14 DESCRIPTOR "%(Name)s"
15 KEYWORD "INSTRUMENT"
16 INSTRUMENT "%(Device)s"
17 KEYWORD "DISPLAY"
18 DISPLAY "%(Display)s"
19 KEYWORD "DISPLAY_TYPE_BASE_ID"
20 DISPLAY_TYPE_BASE_ID "1"
21 KEYWORD "REFERENCE"
22 REFERENCE "%(ReferenceDevice)s"
23 ORIGINATOR "%(Originator)s"
24 CREATED "%(DateTime)s"
25 KEYWORD "COLOR_REP"
26 COLOR_REP "XYZ"
27
28 NUMBER_OF_FIELDS 3
29 BEGIN_DATA_FORMAT
30 XYZ_X XYZ_Y XYZ_Z
31 END_DATA_FORMAT
32
33 NUMBER_OF_SETS 3
34 BEGIN_DATA
35 %(MatrixXYZ)s
36 END_DATA
37 """
38
39
40 def convert_devicecorrections_to_ccmx(path: str, target_dir: str) -> tuple[int, int]:
41 """Convert iColorDisplay DeviceCorrections.txt to individual Argyll CCMX files.
42
43 Args:
44 path (str): Path to the DeviceCorrections.txt file.
45 target_dir (str): Directory where the generated CCMX files will be saved.
46
47 Returns:
48 tuple[int, int]: A tuple containing the number of imported and skipped
49 entries.
50 """
51 with open(path, "utf8") as devcorrections_file:
52 lines = devcorrections_file.read().strip().splitlines()
53 # Convert to JSON
54 # The DeviceCorrections.txt format is as follows, so a conversion is pretty
55 # straightforward:
56 # "Description here, e.g. Instrument X for Monitor Y" =
57 # {
58 # Name = "Description here, e.g. Instrument X for Monitor Y"
59 # Device = "Instrument X"
60 # Display = "Monitor Y"
61 # ReferenceDevice = "eye-one Pro Rev.D"
62 # MatrixXYZ = "3 3 1482250784 1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 1.0 "
63 # }
64 # "Description here, e.g. Instrument X for Monitor Y" =
65 # {
66 # Name = "Description here, e.g. Instrument X for Monitor Y"
67 # Device = "Instrument X"
68 # Display = "Monitor Y"
69 # ReferenceDevice = "eye-one Pro Rev.D"
70 # MatrixXYZ = "3 3 1482250784 1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 1.0 "
71 # }
72 # ...etc.
73 # NOTE: The first three numbers in MatrixXYZ are irrelevant for our purposes.
74 for i, line in enumerate(lines):
75 parts = line.strip().split("=")
76 if len(parts) == 2:
77 for j, part in enumerate(parts):
78 part = part.strip()
79 if part and not part.startswith('"') and not part.endswith('"'):
80 parts[j] = f'"{part}"'
81 if parts[-1].strip() not in ("", "{") and i < len(lines) - 1:
82 parts[-1] += ","
83 lines[i] = ":".join(parts)
84 devcorrections_data = "{{{}}}".format("".join(lines).replace(",}", "}"))
85 # Parse JSON
86 devcorrections = json.loads(devcorrections_data)
87 # Convert to ccmx
88 imported = 0
89 skipped = 0
90 for name in devcorrections:
91 devcorrection = devcorrections[name]
92 values = {
93 "DateTime": time.strftime("%a %b %d %H:%M:%S %Y"),
94 "Originator": "Quato iColorDisplay",
95 "Name": "{} & {}".format(
96 devcorrection.get("Device"), devcorrection.get("Display")
97 ),
98 }
99 for key in ("Device", "Display", "ReferenceDevice", "MatrixXYZ"):
100 value = devcorrection.get(key)
101 if value is None:
102 break
103 if key == "MatrixXYZ":
104 # The first three numbers in the matrix are irrelevant for our
105 # purposes (see format example above).
106 matrix = value.split()[3:]
107 value = "\n".join(
108 [" ".join(part) for part in (matrix[0:3], matrix[3:6], matrix[6:9])]
109 )
110 values[key] = value
111 if value is None:
112 skipped += 1
113 continue
114 imported += 1
115 with open(
116 os.path.join(target_dir, f"{name}.ccmx"), "w", encoding="utf8"
117 ) as ccmx:
118 ccmx.write(CCMX_TEMPLATE % values)
119 return imported, skipped
120
121
122 if __name__ == "__main__":
123 convert_devicecorrections_to_ccmx(sys.argv[1], os.path.dirname(sys.argv[1]))