Debian Codegraph @

util/update_presets.py

raw
1 #!/usr/bin/env python3
2 import os
3 import sys
4
5 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
6
7 from DisplayCAL import colormath, config, worker
8 from DisplayCAL.icc_profile import ICCProfile, TextType
9
10
11 config.initcfg()
12 srgb = config.get_data_path("ref/sRGB.icm")
13 if not srgb:
14 raise OSError("File not found: ref/sRGB.icm")
15 ref = ICCProfile(srgb)
16 print("sRGB:", ref.fileName)
17
18
19 def update_preset(name):
20 print("Preset name:", name)
21 pth = config.get_data_path("presets/%s.icc" % name)
22 if not pth:
23 print("ERROR: Preset not found")
24 return False
25 print("Path:", pth)
26 with open(
27 os.path.join(os.path.dirname(__file__), "..", "misc", "ti3", "%s.ti3" % name),
28 "rb",
29 ) as f:
30 ti3 = f.read()
31 prof = ICCProfile(pth)
32 if prof.tags.targ != ti3:
33 print("Updating 'targ'...")
34 prof.tags.targ = TextType(b"text\0\0\0\0%s\0" % ti3, b"targ")
35 options_dispcal, options_colprof = worker.get_options_from_profile(prof)
36 trc_a2b = {"240": -240, "709": -709, "l": -3, "s": -2.4}
37 t_a2b = {"t": colormath.CIEDCCT2XYZ, "T": colormath.planckianCT2XYZ}
38 trc = None
39 for option in options_dispcal:
40 if option[0] in ("g", "G"):
41 trc = trc_a2b.get(option[1:])
42 if not trc:
43 try:
44 trc = float(option[1:])
45 except ValueError:
46 trc = False
47 print("Invalid dispcal -g parameter:", option[1:])
48 if trc:
49 print("dispcal -%s parameter:" % option[0], option[1:])
50 print("Updating tone response curves...")
51 for chan in ("r", "g", "b"):
52 prof.tags[f"{chan}TRC"].set_trc(trc, 256)
53 print(
54 "Transfer function:",
55 prof.tags[f"{chan}TRC"].get_transfer_function()[0][0],
56 )
57 elif option[0] in ("t", "T") and option[1:]:
58 print("dispcal -t parameter:", option[1:])
59 print("Updating white point...")
60 (prof.tags.wtpt.X, prof.tags.wtpt.Y, prof.tags.wtpt.Z) = t_a2b[option[0]](
61 float(option[1:])
62 )
63 elif option[0] == "w":
64 print("dispcal -w parameter:", option[1:])
65 x, y = [float(v) for v in option[1:].split(",")]
66 print("Updating white point...")
67 (prof.tags.wtpt.X, prof.tags.wtpt.Y, prof.tags.wtpt.Z) = colormath.xyY2XYZ(
68 x, y
69 )
70 elif option[0] in ("t", "T"):
71 print("Updating white point...")
72 (
73 prof.tags.wtpt.X,
74 prof.tags.wtpt.Y,
75 prof.tags.wtpt.Z,
76 ) = colormath.get_whitepoint("D65")
77 for option in options_colprof:
78 if option[0] == "M":
79 print("Updating device model description...")
80 prof.setDeviceModelDescription(option[2:].strip('"'))
81 if "CIED" in prof.tags:
82 print("Removing 'CIED'...")
83 del prof.tags["CIED"]
84 if "DevD" in prof.tags:
85 print("Removing 'DevD'...")
86 del prof.tags["DevD"]
87 if "clrt" in prof.tags:
88 print("Removing 'clrt'...")
89 del prof.tags["clrt"]
90 print("Setting RGB matrix column tags to reference values...")
91 prof.tags.rXYZ = ref.tags.rXYZ
92 prof.tags.gXYZ = ref.tags.gXYZ
93 prof.tags.bXYZ = ref.tags.bXYZ
94 print("Updating profile ID...")
95 prof.calculate_id()
96 prof.write()
97 print("")
98 return True
99
100
101 def update_presets():
102 presets = config.get_data_path("presets", r"\.icc$")
103 for fn in presets:
104 update_preset(os.path.splitext(os.path.basename(fn))[0])
105
106
107 if __name__ == "__main__":
108 if "--help" in sys.argv[1:]:
109 print("Usage: %s [ preset_name [ preset_name ] ... ]" % __file__)
110 elif sys.argv[1:]:
111 for name in sys.argv[1:]:
112 update_preset(name)
113 else:
114 update_presets()