| 1 |
"""3D LUT creation window implementation for DisplayCAL. |
| 2 |
|
| 3 |
It includes functionality for managing ICC profiles, configuring 3D LUT |
| 4 |
settings, handling user interactions, and integrating with wxPython for the |
| 5 |
graphical interface. The module also supports various 3D LUT formats and |
| 6 |
advanced options for color space and tone mapping adjustments. |
| 7 |
""" |
| 8 |
|
| 9 |
import os |
| 10 |
import re |
| 11 |
import shutil |
| 12 |
import sys |
| 13 |
import traceback |
| 14 |
|
| 15 |
from DisplayCAL.argyll import check_set_argyll_bin |
| 16 |
|
| 17 |
if sys.platform == "win32": |
| 18 |
import win32api |
| 19 |
|
| 20 |
from wx import xrc |
| 21 |
|
| 22 |
from DisplayCAL import ( |
| 23 |
colormath, |
| 24 |
config, |
| 25 |
floatspin, |
| 26 |
worker, |
| 27 |
xh_bitmapctrls, |
| 28 |
xh_filebrowsebutton, |
| 29 |
xh_floatspin, |
| 30 |
) |
| 31 |
from DisplayCAL import ( |
| 32 |
localization as lang, |
| 33 |
) |
| 34 |
from DisplayCAL.argyll_cgats import cal_to_fake_profile |
| 35 |
from DisplayCAL.argyll_names import VIDEO_ENCODINGS |
| 36 |
from DisplayCAL.config import ( |
| 37 |
DEFAULTS, |
| 38 |
PROFILE_EXT, |
| 39 |
get_data_path, |
| 40 |
get_icon, |
| 41 |
get_verified_path, |
| 42 |
) |
| 43 |
from DisplayCAL.icc_profile import ( |
| 44 |
CurveType, |
| 45 |
DictType, |
| 46 |
ICCProfile, |
| 47 |
ICCProfileInvalidError, |
| 48 |
LUT16Type, |
| 49 |
VideoCardGammaType, |
| 50 |
) |
| 51 |
from DisplayCAL.meta import NAME as APPNAME |
| 52 |
from DisplayCAL.meta import VERSION_STRING |
| 53 |
from DisplayCAL.options import DEBUG |
| 54 |
from DisplayCAL.util_decimal import stripzeros |
| 55 |
from DisplayCAL.util_os import islink, readlink, safe_glob, waccess |
| 56 |
from DisplayCAL.util_str import strtr |
| 57 |
from DisplayCAL.worker import ( |
| 58 |
Error, |
| 59 |
UnloggedInfo, |
| 60 |
UnloggedWarning, |
| 61 |
get_current_profile_path, |
| 62 |
show_result_dialog, |
| 63 |
) |
| 64 |
from DisplayCAL.wx_addons import CustomEvent |
| 65 |
from DisplayCAL.wx_fixes import TempXmlResource |
| 66 |
from DisplayCAL.wx_windows import ( |
| 67 |
BaseApp, |
| 68 |
BaseFrame, |
| 69 |
ConfirmDialog, |
| 70 |
FileDrop, |
| 71 |
wx, |
| 72 |
) |
| 73 |
|
| 74 |
|
| 75 |
class LUT3DMixin: |
| 76 |
"""Mixin class that adds some ``lut3d_`` functions to the mixed-in class.""" |
| 77 |
|
| 78 |
def lut3d_bind_event_handlers(self): |
| 79 |
"""Bind event handlers for the 3D LUT controls.""" |
| 80 |
# Shared with main window |
| 81 |
self.lut3d_apply_cal_cb.Bind(wx.EVT_CHECKBOX, self.lut3d_apply_cal_ctrl_handler) |
| 82 |
self.lut3d_create_btn.Bind(wx.EVT_BUTTON, self.lut3d_create_handler) |
| 83 |
self.lut3d_hdr_peak_luminance_ctrl.Bind( |
| 84 |
floatspin.EVT_FLOATSPIN, self.lut3d_hdr_peak_luminance_handler |
| 85 |
) |
| 86 |
self.lut3d_hdr_display_ctrl.Bind(wx.EVT_CHOICE, self.lut3d_hdr_display_handler) |
| 87 |
self.lut3d_bind_trc_handlers() |
| 88 |
self.lut3d_bind_hdr_trc_handlers() |
| 89 |
self.lut3d_bind_content_colorspace_handlers() |
| 90 |
self.lut3d_bind_common_handlers() |
| 91 |
|
| 92 |
def lut3d_bind_trc_handlers(self): |
| 93 |
"""Bind event handlers for TRC-related controls in the 3D LUT settings.""" |
| 94 |
self.lut3d_trc_ctrl.Bind(wx.EVT_CHOICE, self.lut3d_trc_ctrl_handler) |
| 95 |
self.lut3d_trc_gamma_ctrl.Bind( |
| 96 |
wx.EVT_COMBOBOX, self.lut3d_trc_gamma_ctrl_handler |
| 97 |
) |
| 98 |
self.lut3d_trc_gamma_ctrl.Bind( |
| 99 |
wx.EVT_KILL_FOCUS, self.lut3d_trc_gamma_ctrl_handler |
| 100 |
) |
| 101 |
self.lut3d_trc_gamma_type_ctrl.Bind( |
| 102 |
wx.EVT_CHOICE, self.lut3d_trc_gamma_type_ctrl_handler |
| 103 |
) |
| 104 |
self.lut3d_trc_black_output_offset_ctrl.Bind( |
| 105 |
wx.EVT_SLIDER, self.lut3d_trc_black_output_offset_ctrl_handler |
| 106 |
) |
| 107 |
self.lut3d_trc_black_output_offset_intctrl.Bind( |
| 108 |
wx.EVT_TEXT, self.lut3d_trc_black_output_offset_ctrl_handler |
| 109 |
) |
| 110 |
|
| 111 |
def lut3d_bind_hdr_trc_handlers(self): |
| 112 |
"""Bind event handlers for HDR-related controls in the 3D LUT settings.""" |
| 113 |
self.lut3d_hdr_ambient_luminance_ctrl.Bind( |
| 114 |
floatspin.EVT_FLOATSPIN, self.lut3d_hdr_ambient_luminance_handler |
| 115 |
) |
| 116 |
self.lut3d_hdr_minmll_ctrl.Bind( |
| 117 |
floatspin.EVT_FLOATSPIN, self.lut3d_hdr_minmll_handler |
| 118 |
) |
| 119 |
self.lut3d_hdr_maxmll_ctrl.Bind( |
| 120 |
floatspin.EVT_FLOATSPIN, self.lut3d_hdr_maxmll_handler |
| 121 |
) |
| 122 |
self.lut3d_hdr_maxmll_alt_clip_cb.Bind( |
| 123 |
wx.EVT_CHECKBOX, self.lut3d_hdr_maxmll_alt_clip_handler |
| 124 |
) |
| 125 |
self.lut3d_hdr_sat_ctrl.Bind(wx.EVT_SLIDER, self.lut3d_hdr_sat_ctrl_handler) |
| 126 |
self.lut3d_hdr_hue_ctrl.Bind(wx.EVT_SLIDER, self.lut3d_hdr_hue_ctrl_handler) |
| 127 |
self.lut3d_hdr_hue_intctrl.Bind(wx.EVT_TEXT, self.lut3d_hdr_hue_ctrl_handler) |
| 128 |
|
| 129 |
def lut3d_bind_content_colorspace_handlers(self): |
| 130 |
"""Bind event handlers for the content colorspace controls.""" |
| 131 |
self.lut3d_content_colorspace_ctrl.Bind( |
| 132 |
wx.EVT_CHOICE, self.lut3d_content_colorspace_handler |
| 133 |
) |
| 134 |
for color in ("white", "red", "green", "blue"): |
| 135 |
for coord in "xy": |
| 136 |
_ = self.getcfg(f"3dlut.content.colorspace.{color}.{coord}") |
| 137 |
getattr(self, f"lut3d_content_colorspace_{color}_{coord}").Bind( |
| 138 |
floatspin.EVT_FLOATSPIN, self.lut3d_content_colorspace_xy_handler |
| 139 |
) |
| 140 |
|
| 141 |
def lut3d_bind_common_handlers(self): |
| 142 |
"""Bind common event handlers for 3D LUT controls.""" |
| 143 |
self.encoding_input_ctrl.Bind( |
| 144 |
wx.EVT_CHOICE, self.lut3d_encoding_input_ctrl_handler |
| 145 |
) |
| 146 |
self.encoding_output_ctrl.Bind( |
| 147 |
wx.EVT_CHOICE, self.lut3d_encoding_output_ctrl_handler |
| 148 |
) |
| 149 |
self.gamut_mapping_inverse_a2b.Bind( |
| 150 |
wx.EVT_RADIOBUTTON, self.lut3d_gamut_mapping_mode_handler |
| 151 |
) |
| 152 |
self.gamut_mapping_b2a.Bind( |
| 153 |
wx.EVT_RADIOBUTTON, self.lut3d_gamut_mapping_mode_handler |
| 154 |
) |
| 155 |
self.lut3d_rendering_intent_ctrl.Bind( |
| 156 |
wx.EVT_CHOICE, self.lut3d_rendering_intent_ctrl_handler |
| 157 |
) |
| 158 |
self.lut3d_format_ctrl.Bind(wx.EVT_CHOICE, self.lut3d_format_ctrl_handler) |
| 159 |
self.lut3d_size_ctrl.Bind(wx.EVT_CHOICE, self.lut3d_size_ctrl_handler) |
| 160 |
self.lut3d_bitdepth_input_ctrl.Bind( |
| 161 |
wx.EVT_CHOICE, self.lut3d_bitdepth_input_ctrl_handler |
| 162 |
) |
| 163 |
self.lut3d_bitdepth_output_ctrl.Bind( |
| 164 |
wx.EVT_CHOICE, self.lut3d_bitdepth_output_ctrl_handler |
| 165 |
) |
| 166 |
|
| 167 |
def lut3d_trc_apply_ctrl_handler(self, event=None): |
| 168 |
"""Handle changes to the "Apply TRC" checkbox for 3D LUTs. |
| 169 |
|
| 170 |
Args: |
| 171 |
event: The event that triggered this handler, which contains the |
| 172 |
state of the checkbox. |
| 173 |
""" |
| 174 |
v = self.lut3d_trc_apply_ctrl.GetValue() |
| 175 |
self.lut3d_trc_ctrl.Enable(v) |
| 176 |
self.lut3d_trc_gamma_label.Enable(v) |
| 177 |
self.lut3d_trc_gamma_ctrl.Enable(v) |
| 178 |
self.lut3d_trc_gamma_type_ctrl.Enable(v) |
| 179 |
if event: |
| 180 |
self.setcfg("3dlut.apply_trc", int(v)) |
| 181 |
self.setcfg( |
| 182 |
"3dlut.apply_black_offset", |
| 183 |
int(self.lut3d_trc_apply_black_offset_ctrl.GetValue()), |
| 184 |
) |
| 185 |
self.lut3d_hdr_peak_luminance_label.Enable(v) |
| 186 |
self.lut3d_hdr_peak_luminance_ctrl.Enable(v) |
| 187 |
self.lut3d_hdr_peak_luminance_ctrl_label.Enable(v) |
| 188 |
self.lut3d_hdr_minmll_label.Enable(v) |
| 189 |
self.lut3d_hdr_minmll_ctrl.Enable(v) |
| 190 |
self.lut3d_hdr_minmll_ctrl_label.Enable(v) |
| 191 |
self.lut3d_hdr_maxmll_label.Enable(v) |
| 192 |
self.lut3d_hdr_maxmll_ctrl.Enable(v) |
| 193 |
self.lut3d_hdr_maxmll_ctrl_label.Enable(v) |
| 194 |
self.lut3d_hdr_diffuse_white_label.Enable(v) |
| 195 |
self.lut3d_hdr_diffuse_white_txt.Enable(v) |
| 196 |
self.lut3d_hdr_diffuse_white_txt_label.Enable(v) |
| 197 |
self.lut3d_hdr_ambient_luminance_label.Enable(v) |
| 198 |
self.lut3d_hdr_ambient_luminance_ctrl.Enable(v) |
| 199 |
self.lut3d_hdr_ambient_luminance_ctrl_label.Enable(v) |
| 200 |
self.lut3d_hdr_system_gamma_label.Enable(v) |
| 201 |
self.lut3d_hdr_system_gamma_txt.Enable(v) |
| 202 |
self.lut3d_content_colorspace_label.Enable(v) |
| 203 |
self.lut3d_content_colorspace_ctrl.Enable(v) |
| 204 |
for color in ("white", "red", "green", "blue"): |
| 205 |
for coord in "xy": |
| 206 |
getattr(self, f"lut3d_content_colorspace_{color[0]}_label").Enable(v) |
| 207 |
getattr(self, f"lut3d_content_colorspace_{color}_{coord}").Enable(v) |
| 208 |
getattr(self, f"lut3d_content_colorspace_{color}_{coord}_label").Enable( |
| 209 |
v |
| 210 |
) |
| 211 |
self.lut3d_trc_black_output_offset_label.Enable(v) |
| 212 |
self.lut3d_trc_black_output_offset_ctrl.Enable(v) |
| 213 |
self.lut3d_trc_black_output_offset_intctrl.Enable(v) |
| 214 |
self.lut3d_trc_black_output_offset_intctrl_label.Enable(v) |
| 215 |
self.lut3d_show_input_value_clipping_warning(event) |
| 216 |
self.lut3d_show_hdr_display_control() |
| 217 |
|
| 218 |
def lut3d_show_input_value_clipping_warning(self, layout): |
| 219 |
"""Show or hide the input value clipping warning based on the current settings. |
| 220 |
|
| 221 |
Args: |
| 222 |
layout (bool): If True, the layout will be updated after showing or |
| 223 |
hiding the warning. |
| 224 |
""" |
| 225 |
self.panel.Freeze() |
| 226 |
show = ( |
| 227 |
self.lut3d_trc_apply_none_ctrl.GetValue() |
| 228 |
and self.XYZbpout > self.XYZbpin |
| 229 |
and self.getcfg("3dlut.rendering_intent") |
| 230 |
not in ("la", "p", "pa", "ms", "s", "lp") |
| 231 |
) |
| 232 |
self.lut3d_input_value_clipping_bmp.Show(show) |
| 233 |
self.lut3d_input_value_clipping_label.Show(show) |
| 234 |
if layout: |
| 235 |
self.panel.Sizer.Layout() |
| 236 |
self.update_layout() |
| 237 |
self.panel.Thaw() |
| 238 |
|
| 239 |
def lut3d_hdr_maxmll_alt_clip_handler(self, event): |
| 240 |
"""Handle changes to the HDR maximum MLL alternative clipping checkbox. |
| 241 |
|
| 242 |
MLL stands for Minimum Light Level, which is used to adjust the maximum |
| 243 |
light level in HDR content. |
| 244 |
|
| 245 |
Args: |
| 246 |
event: The event that triggered this handler, which contains the |
| 247 |
state of the checkbox. |
| 248 |
""" |
| 249 |
self.lut3d_set_option( |
| 250 |
"3dlut.hdr_maxmll_alt_clip", |
| 251 |
int(not self.lut3d_hdr_maxmll_alt_clip_cb.GetValue()), |
| 252 |
) |
| 253 |
self.lut3d_hdr_update_diffuse_white() |
| 254 |
|
| 255 |
def lut3d_apply_cal_ctrl_handler(self, event): |
| 256 |
"""Handle changes to the "Apply calibration" checkbox for 3D LUTs. |
| 257 |
|
| 258 |
Args: |
| 259 |
event: The event that triggered this handler, which contains the |
| 260 |
state of the checkbox. |
| 261 |
""" |
| 262 |
self.setcfg( |
| 263 |
"3dlut.output.profile.apply_cal", int(self.lut3d_apply_cal_cb.GetValue()) |
| 264 |
) |
| 265 |
|
| 266 |
def lut3d_hdr_display_handler(self, event): |
| 267 |
"""Handle changes to the HDR display selection for 3D LUTs. |
| 268 |
|
| 269 |
Args: |
| 270 |
event: The event that triggered this handler, which contains the |
| 271 |
selection index of the HDR display control. |
| 272 |
""" |
| 273 |
if ( |
| 274 |
self.lut3d_hdr_display_ctrl.GetSelection() |
| 275 |
and not self.getcfg("3dlut.hdr_display") |
| 276 |
and not show_result_dialog( |
| 277 |
UnloggedInfo(lang.getstr("3dlut.format.madVR.hdr.confirm")), |
| 278 |
self, |
| 279 |
confirm=lang.getstr("ok"), |
| 280 |
) |
| 281 |
): |
| 282 |
self.lut3d_hdr_display_ctrl.SetSelection(0) |
| 283 |
return |
| 284 |
self.lut3d_set_option( |
| 285 |
"3dlut.hdr_display", self.lut3d_hdr_display_ctrl.GetSelection() |
| 286 |
) |
| 287 |
|
| 288 |
def lut3d_hdr_peak_luminance_handler(self, event): |
| 289 |
"""Handle changes to the peak luminance value for HDR 3D LUTs. |
| 290 |
|
| 291 |
Args: |
| 292 |
event: The event that triggered this handler, which contains the |
| 293 |
new value of the peak luminance control. |
| 294 |
""" |
| 295 |
target_peak = self.lut3d_hdr_peak_luminance_ctrl.GetValue() |
| 296 |
maxmll = self.lut3d_hdr_maxmll_ctrl.GetValue() |
| 297 |
if maxmll < target_peak: |
| 298 |
self.setcfg("3dlut.hdr_maxmll", target_peak) |
| 299 |
self.lut3d_hdr_maxmll_ctrl.SetRange(target_peak, 10000) |
| 300 |
self.lut3d_set_option( |
| 301 |
"3dlut.hdr_peak_luminance", self.lut3d_hdr_peak_luminance_ctrl.GetValue() |
| 302 |
) |
| 303 |
|
| 304 |
def lut3d_hdr_ambient_luminance_handler(self, event): |
| 305 |
"""Handle changes to the ambient luminance value for HDR 3D LUTs. |
| 306 |
|
| 307 |
Args: |
| 308 |
event: The event that triggered this handler, which contains the |
| 309 |
new value of the ambient luminance control. |
| 310 |
""" |
| 311 |
self.lut3d_set_option( |
| 312 |
"3dlut.hdr_ambient_luminance", |
| 313 |
self.lut3d_hdr_ambient_luminance_ctrl.GetValue(), |
| 314 |
) |
| 315 |
|
| 316 |
def lut3d_hdr_minmll_handler(self, event): |
| 317 |
"""Handle changes to the minimum MLL value for HDR 3D LUTs. |
| 318 |
|
| 319 |
MLL stands for Minimum Light Level, which is used to adjust the minimum |
| 320 |
light level in HDR content. |
| 321 |
|
| 322 |
Args: |
| 323 |
event: The event that triggered this handler, which contains the |
| 324 |
new value of the minimum MLL control. |
| 325 |
""" |
| 326 |
self.lut3d_set_option("3dlut.hdr_minmll", self.lut3d_hdr_minmll_ctrl.GetValue()) |
| 327 |
|
| 328 |
def lut3d_hdr_maxmll_handler(self, event): |
| 329 |
"""Handle changes to the maximum MLL value for HDR 3D LUTs. |
| 330 |
|
| 331 |
MLL stands for Minimum Light Level, which is used to adjust the maximum |
| 332 |
light level in HDR content. |
| 333 |
|
| 334 |
Args: |
| 335 |
event: The event that triggered this handler, which contains the |
| 336 |
new value of the maximum MLL control. |
| 337 |
""" # noqa: E501 |
| 338 |
self.lut3d_set_option("3dlut.hdr_maxmll", self.lut3d_hdr_maxmll_ctrl.GetValue()) |
| 339 |
|
| 340 |
def lut3d_hdr_sat_ctrl_handler(self, event): |
| 341 |
"""Handle changes to the saturation value for HDR 3D LUTs. |
| 342 |
|
| 343 |
Args: |
| 344 |
event: The event that triggered this handler, which contains the |
| 345 |
new value of the saturation control. |
| 346 |
""" |
| 347 |
self.lut3d_set_option( |
| 348 |
"3dlut.hdr_sat", self.lut3d_hdr_sat_ctrl.GetValue() / 100.0 |
| 349 |
) |
| 350 |
self.lut3d_hdr_update_sat_val() |
| 351 |
|
| 352 |
def lut3d_hdr_hue_ctrl_handler(self, event): |
| 353 |
"""Handle changes to the hue value for HDR 3D LUTs. |
| 354 |
|
| 355 |
Args: |
| 356 |
event: The event that triggered this handler, which contains the |
| 357 |
new value of the hue control. |
| 358 |
""" |
| 359 |
if event.GetId() == self.lut3d_hdr_hue_intctrl.GetId(): |
| 360 |
self.lut3d_hdr_hue_ctrl.SetValue(self.lut3d_hdr_hue_intctrl.GetValue()) |
| 361 |
else: |
| 362 |
self.lut3d_hdr_hue_intctrl.SetValue(self.lut3d_hdr_hue_ctrl.GetValue()) |
| 363 |
v = self.lut3d_hdr_hue_ctrl.GetValue() / 100.0 |
| 364 |
if v != self.getcfg("3dlut.hdr_hue"): |
| 365 |
self.lut3d_set_option("3dlut.hdr_hue", v) |
| 366 |
|
| 367 |
def lut3d_trc_black_output_offset_ctrl_handler(self, event): |
| 368 |
"""Handle changes to the TRC black output offset value for 3D LUTs. |
| 369 |
|
| 370 |
Args: |
| 371 |
event: The event that triggered this handler, which contains the |
| 372 |
new value of the TRC black output offset control. |
| 373 |
""" |
| 374 |
if event.GetId() == self.lut3d_trc_black_output_offset_intctrl.GetId(): |
| 375 |
self.lut3d_trc_black_output_offset_ctrl.SetValue( |
| 376 |
self.lut3d_trc_black_output_offset_intctrl.GetValue() |
| 377 |
) |
| 378 |
else: |
| 379 |
self.lut3d_trc_black_output_offset_intctrl.SetValue( |
| 380 |
self.lut3d_trc_black_output_offset_ctrl.GetValue() |
| 381 |
) |
| 382 |
v = self.lut3d_trc_black_output_offset_ctrl.GetValue() / 100.0 |
| 383 |
if v != self.getcfg("3dlut.trc_output_offset"): |
| 384 |
self.lut3d_set_option("3dlut.trc_output_offset", v) |
| 385 |
self.lut3d_update_trc_control() |
| 386 |
# self.lut3d_show_trc_controls() |
| 387 |
|
| 388 |
def lut3d_trc_gamma_ctrl_handler(self, event): |
| 389 |
"""Handle changes to the TRC gamma value for 3D LUTs. |
| 390 |
|
| 391 |
Args: |
| 392 |
event: The event that triggered this handler, which contains the |
| 393 |
new value of the TRC gamma control. |
| 394 |
""" |
| 395 |
try: |
| 396 |
v = float(self.lut3d_trc_gamma_ctrl.GetValue().replace(",", ".")) |
| 397 |
if ( |
| 398 |
v < config.VALID_RANGES["3dlut.trc_gamma"][0] |
| 399 |
or v > config.VALID_RANGES["3dlut.trc_gamma"][1] |
| 400 |
): |
| 401 |
raise ValueError |
| 402 |
except ValueError: |
| 403 |
wx.Bell() |
| 404 |
self.lut3d_trc_gamma_ctrl.SetValue(str(self.getcfg("3dlut.trc_gamma"))) |
| 405 |
else: |
| 406 |
if str(v) != self.lut3d_trc_gamma_ctrl.GetValue(): |
| 407 |
self.lut3d_trc_gamma_ctrl.SetValue(str(v)) |
| 408 |
if v != self.getcfg("3dlut.trc_gamma"): |
| 409 |
self.lut3d_set_option("3dlut.trc_gamma", v) |
| 410 |
self.lut3d_update_trc_control() |
| 411 |
# self.lut3d_show_trc_controls() |
| 412 |
event.Skip() |
| 413 |
|
| 414 |
def lut3d_trc_ctrl_handler(self, event): |
| 415 |
"""Handle changes to the TRC selection for 3D LUTs. |
| 416 |
|
| 417 |
Args: |
| 418 |
event: The event that triggered this handler, which contains the |
| 419 |
selection index of the TRC. |
| 420 |
""" |
| 421 |
self.Freeze() |
| 422 |
if self.lut3d_trc_ctrl.GetSelection() == 1: |
| 423 |
# BT.1886 |
| 424 |
self.lut3d_set_option("3dlut.trc_gamma", 2.4) |
| 425 |
self.lut3d_set_option("3dlut.trc_gamma_type", "B") |
| 426 |
self.lut3d_set_option("3dlut.trc_output_offset", 0.0) |
| 427 |
trc = "bt1886" |
| 428 |
elif self.lut3d_trc_ctrl.GetSelection() == 0: |
| 429 |
# Pure power gamma 2.2 |
| 430 |
self.lut3d_set_option("3dlut.trc_gamma", 2.2) |
| 431 |
self.lut3d_set_option("3dlut.trc_gamma_type", "b") |
| 432 |
self.lut3d_set_option("3dlut.trc_output_offset", 1.0) |
| 433 |
trc = "gamma2.2" |
| 434 |
elif self.lut3d_trc_ctrl.GetSelection() == 2: # SMPTE 2084, hard clip |
| 435 |
self.lut3d_set_option("3dlut.trc_output_offset", 0.0) |
| 436 |
trc = "smpte2084.hardclip" |
| 437 |
self.lut3d_set_option("3dlut.hdr_maxmll", 10000) |
| 438 |
elif self.lut3d_trc_ctrl.GetSelection() == 3: # SMPTE 2084, roll-off clip |
| 439 |
self.lut3d_set_option("3dlut.trc_output_offset", 0.0) |
| 440 |
trc = "smpte2084.rolloffclip" |
| 441 |
self.lut3d_set_option("3dlut.hdr_maxmll", 10000) |
| 442 |
elif self.lut3d_trc_ctrl.GetSelection() == 4: # HLG |
| 443 |
self.lut3d_set_option("3dlut.trc_output_offset", 0.0) |
| 444 |
trc = "hlg" |
| 445 |
else: |
| 446 |
trc = "customgamma" |
| 447 |
# Have to use CallAfter, otherwise only part of the text will |
| 448 |
# be selected (wxPython bug?) |
| 449 |
wx.CallAfter(self.lut3d_trc_gamma_ctrl.SetFocus) |
| 450 |
wx.CallLater(1, self.lut3d_trc_gamma_ctrl.SelectAll) |
| 451 |
self.lut3d_set_option("3dlut.trc", trc) |
| 452 |
if trc != "customgamma": |
| 453 |
self.lut3d_update_trc_controls() |
| 454 |
self.lut3d_show_trc_controls() |
| 455 |
self.Thaw() |
| 456 |
|
| 457 |
def lut3d_trc_gamma_type_ctrl_handler(self, event): |
| 458 |
"""Handle changes to the TRC gamma type selection for 3D LUTs. |
| 459 |
|
| 460 |
Args: |
| 461 |
event: The event that triggered this handler, which contains the |
| 462 |
selection index of the TRC gamma type. |
| 463 |
""" |
| 464 |
v = self.trc_gamma_types_ab[self.lut3d_trc_gamma_type_ctrl.GetSelection()] |
| 465 |
if v != self.getcfg("3dlut.trc_gamma_type"): |
| 466 |
self.lut3d_set_option("3dlut.trc_gamma_type", v) |
| 467 |
self.lut3d_update_trc_control() |
| 468 |
self.lut3d_show_trc_controls() |
| 469 |
|
| 470 |
def lut3d_encoding_input_ctrl_handler(self, event): |
| 471 |
"""Handle changes to the input encoding selection for 3D LUTs. |
| 472 |
|
| 473 |
Args: |
| 474 |
event: The event that triggered this handler, which contains the |
| 475 |
selection index of the input encoding. |
| 476 |
""" |
| 477 |
encoding = self.encoding_input_ab[self.encoding_input_ctrl.GetSelection()] |
| 478 |
self.lut3d_set_option("3dlut.encoding.input", encoding) |
| 479 |
if getattr(self, "lut3dframe", None): |
| 480 |
self.lut3dframe.lut3d_update_encoding_controls() |
| 481 |
elif self.Parent: |
| 482 |
self.Parent.lut3d_update_encoding_controls() |
| 483 |
|
| 484 |
def lut3d_encoding_output_ctrl_handler(self, event): |
| 485 |
"""Handle changes to the output encoding selection for 3D LUTs. |
| 486 |
|
| 487 |
Args: |
| 488 |
event: The event that triggered this handler, which contains the |
| 489 |
selection index of the output encoding. |
| 490 |
""" |
| 491 |
encoding = self.encoding_output_ab[self.encoding_output_ctrl.GetSelection()] |
| 492 |
if self.getcfg("3dlut.format") == "madVR" and encoding != "t": |
| 493 |
profile = getattr(self, "output_profile", None) |
| 494 |
if ( |
| 495 |
profile |
| 496 |
and "meta" in profile.tags |
| 497 |
and isinstance(profile.tags.meta, DictType) |
| 498 |
and "EDID_model" in profile.tags.meta |
| 499 |
): |
| 500 |
devicename = profile.tags.meta["EDID_model"] |
| 501 |
else: |
| 502 |
devicename = None |
| 503 |
dlg = ConfirmDialog( |
| 504 |
self, |
| 505 |
msg=lang.getstr( |
| 506 |
"3dlut.encoding.output.warning.madvr", |
| 507 |
devicename or lang.getstr("device.name.placeholder"), |
| 508 |
), |
| 509 |
ok=lang.getstr("ok"), |
| 510 |
cancel=lang.getstr("cancel"), |
| 511 |
bitmap=get_icon(32, "dialog-warning"), |
| 512 |
) |
| 513 |
result = dlg.ShowModal() |
| 514 |
dlg.Destroy() |
| 515 |
if result != wx.ID_OK: |
| 516 |
self.encoding_output_ctrl.SetSelection( |
| 517 |
self.encoding_output_ba[self.getcfg("3dlut.encoding.output")] |
| 518 |
) |
| 519 |
return False |
| 520 |
self.lut3d_set_option("3dlut.encoding.output", encoding) |
| 521 |
if getattr(self, "lut3dframe", None): |
| 522 |
self.lut3dframe.lut3d_update_encoding_controls() |
| 523 |
elif self.Parent: |
| 524 |
self.Parent.lut3d_update_encoding_controls() |
| 525 |
return None |
| 526 |
|
| 527 |
def lut3d_bitdepth_input_ctrl_handler(self, event): |
| 528 |
"""Handle changes to the input bit depth selection for 3D LUTs. |
| 529 |
|
| 530 |
Args: |
| 531 |
event: The event that triggered this handler, which contains the |
| 532 |
selection index of the input bit depth. |
| 533 |
""" |
| 534 |
self.lut3d_set_option( |
| 535 |
"3dlut.bitdepth.input", |
| 536 |
self.lut3d_bitdepth_ab[self.lut3d_bitdepth_input_ctrl.GetSelection()], |
| 537 |
) |
| 538 |
if getattr(self, "lut3dframe", None): |
| 539 |
self.lut3dframe.lut3d_update_shared_controls() |
| 540 |
elif self.Parent: |
| 541 |
self.Parent.lut3d_update_shared_controls() |
| 542 |
|
| 543 |
def lut3d_bitdepth_output_ctrl_handler(self, event): |
| 544 |
"""Handle changes to the output bit depth selection for 3D LUTs. |
| 545 |
|
| 546 |
Args: |
| 547 |
event: The event that triggered this handler, which contains the |
| 548 |
selection index of the output bit depth. |
| 549 |
""" |
| 550 |
if self.getcfg("3dlut.format") in ("png", "ReShade") and self.lut3d_bitdepth_ab[ |
| 551 |
self.lut3d_bitdepth_output_ctrl.GetSelection() |
| 552 |
] not in (8, 16): |
| 553 |
wx.Bell() |
| 554 |
self.lut3d_bitdepth_output_ctrl.SetSelection(self.lut3d_bitdepth_ba[8]) |
| 555 |
self.lut3d_set_option( |
| 556 |
"3dlut.bitdepth.output", |
| 557 |
self.lut3d_bitdepth_ab[self.lut3d_bitdepth_output_ctrl.GetSelection()], |
| 558 |
) |
| 559 |
if getattr(self, "lut3dframe", None): |
| 560 |
self.lut3dframe.lut3d_update_shared_controls() |
| 561 |
elif self.Parent: |
| 562 |
self.Parent.lut3d_update_shared_controls() |
| 563 |
|
| 564 |
def lut3d_content_colorspace_handler(self, event): |
| 565 |
"""Handle changes to the content colorspace selection. |
| 566 |
|
| 567 |
Args: |
| 568 |
event: The event that triggered this handler, which contains the |
| 569 |
selection index of the content colorspace. |
| 570 |
""" |
| 571 |
sel = self.lut3d_content_colorspace_ctrl.Selection |
| 572 |
try: |
| 573 |
rgb_space = self.lut3d_content_colorspace_names[sel] |
| 574 |
except IndexError: |
| 575 |
# Custom |
| 576 |
rgb_space = None |
| 577 |
else: |
| 578 |
rgb_space = colormath.get_rgb_space(rgb_space) |
| 579 |
for i, color in enumerate(("white", "red", "green", "blue")): |
| 580 |
if i == 0: |
| 581 |
xyY = colormath.XYZ2xyY(*rgb_space[1]) |
| 582 |
else: |
| 583 |
xyY = rgb_space[2:][i - 1] |
| 584 |
for j, coord in enumerate("xy"): |
| 585 |
v = round(xyY[j], 4) |
| 586 |
self.lut3d_set_option( |
| 587 |
f"3dlut.content.colorspace.{color}.{coord}", v |
| 588 |
) |
| 589 |
self.lut3d_update_trc_controls() |
| 590 |
self.panel.Freeze() |
| 591 |
sizer = self.lut3d_content_colorspace_red_x.ContainingSizer |
| 592 |
sizer.ShowItems(not rgb_space) |
| 593 |
self.panel.Layout() |
| 594 |
self.panel.Thaw() |
| 595 |
if isinstance(self, LUT3DFrame): |
| 596 |
self.update_layout() |
| 597 |
|
| 598 |
def lut3d_content_colorspace_xy_handler(self, event): |
| 599 |
"""Handle changes to the content colorspace XY coordinates. |
| 600 |
|
| 601 |
Args: |
| 602 |
event: The event that triggered this handler, which contains the |
| 603 |
name of the control that was changed. |
| 604 |
""" |
| 605 |
option = event.GetEventObject().Name.replace("_", ".")[5:] |
| 606 |
self.lut3d_set_option("3dlut" + option, event.GetEventObject().GetValue()) |
| 607 |
self.lut3d_update_trc_controls() |
| 608 |
|
| 609 |
def lut3d_create_consumer(self, result=None): |
| 610 |
"""Handle the result of the 3D LUT creation process. |
| 611 |
|
| 612 |
Args: |
| 613 |
result: The result of the 3D LUT creation process, which can be |
| 614 |
either a success message or an exception. |
| 615 |
""" |
| 616 |
if isinstance(result, Exception): |
| 617 |
show_result_dialog(result, self) |
| 618 |
# Remove temporary files |
| 619 |
self.worker.wrapup(False) |
| 620 |
if ( |
| 621 |
not isinstance(result, Exception) |
| 622 |
and result |
| 623 |
and not isinstance(self, LUT3DFrame) |
| 624 |
and getattr(self, "lut3d_path", None) |
| 625 |
): |
| 626 |
# 3D LUT tab is part of main window |
| 627 |
if self.getcfg("3dlut.create"): |
| 628 |
# 3D LUT was created automatically after profiling, show |
| 629 |
# usual profile summary window |
| 630 |
self.profile_finish( |
| 631 |
True, |
| 632 |
self.getcfg("calibration.file", False), |
| 633 |
lang.getstr("calibration_profiling.complete"), |
| 634 |
lang.getstr("profiling.incomplete"), |
| 635 |
install_3dlut=True, |
| 636 |
) |
| 637 |
else: |
| 638 |
# 3D LUT was created manually |
| 639 |
self.profile_finish( |
| 640 |
True, |
| 641 |
self.getcfg("calibration.file", False), |
| 642 |
"", |
| 643 |
lang.getstr("profiling.incomplete"), |
| 644 |
install_3dlut=True, |
| 645 |
) |
| 646 |
|
| 647 |
def lut3d_create_handler(self, event, path=None, copy_from_path=None): |
| 648 |
"""Create a 3D LUT. |
| 649 |
|
| 650 |
Args: |
| 651 |
event: The event that triggered this handler. |
| 652 |
path (str, optional): The path to save the 3D LUT file. If not |
| 653 |
provided, a file dialog will be shown to select the path. |
| 654 |
copy_from_path (str, optional): If provided, the function will copy |
| 655 |
an existing 3D LUT from this path instead of creating a new |
| 656 |
one. |
| 657 |
""" |
| 658 |
if sys.platform == "darwin" or DEBUG: |
| 659 |
self.focus_handler(event) |
| 660 |
if not check_set_argyll_bin(): |
| 661 |
return |
| 662 |
if isinstance(self, LUT3DFrame): |
| 663 |
profile_in = self.set_profile("input") |
| 664 |
if self.getcfg("3dlut.use_abstract_profile"): |
| 665 |
profile_abst = self.set_profile("abstract") |
| 666 |
else: |
| 667 |
profile_abst = None |
| 668 |
profile_out = self.set_profile("output") |
| 669 |
else: |
| 670 |
profile_abst = None |
| 671 |
profile_in_path = self.getcfg("3dlut.input.profile") |
| 672 |
if not profile_in_path or not os.path.isfile(profile_in_path): |
| 673 |
show_result_dialog( |
| 674 |
Error( |
| 675 |
lang.getstr( |
| 676 |
"error.profile.file_missing", |
| 677 |
self.getcfg("3dlut.input.profile", raw=True), |
| 678 |
) |
| 679 |
), |
| 680 |
parent=self, |
| 681 |
) |
| 682 |
return |
| 683 |
|
| 684 |
try: |
| 685 |
profile_in = ICCProfile(profile_in_path) |
| 686 |
except (OSError, ICCProfileInvalidError): |
| 687 |
show_result_dialog( |
| 688 |
Error(lang.getstr("profile.invalid") + "\n" + profile_in_path), |
| 689 |
parent=self, |
| 690 |
) |
| 691 |
return |
| 692 |
profile_out = config.get_current_profile() |
| 693 |
if not profile_out: |
| 694 |
show_result_dialog( |
| 695 |
Error( |
| 696 |
lang.getstr("profile.invalid") |
| 697 |
+ "\n{}".format(self.getcfg("calibration.file", False)) |
| 698 |
), |
| 699 |
parent=self, |
| 700 |
) |
| 701 |
return |
| 702 |
if path: |
| 703 |
# Called from script client |
| 704 |
self.lut3d_path = None |
| 705 |
elif not copy_from_path: |
| 706 |
self.lut3d_set_path() |
| 707 |
path = self.lut3d_path |
| 708 |
if None not in (profile_in, profile_out) or ( |
| 709 |
profile_in and profile_in.profileClass == "link" |
| 710 |
): |
| 711 |
if ( |
| 712 |
profile_out |
| 713 |
and profile_in.is_same(profile_out, force_calculation=True) |
| 714 |
and not show_result_dialog( |
| 715 |
Warning(lang.getstr("error.source_dest_same")), |
| 716 |
self, |
| 717 |
confirm=lang.getstr("continue"), |
| 718 |
) |
| 719 |
): |
| 720 |
return |
| 721 |
checkoverwrite = True |
| 722 |
remember_last_3dlut_path = False |
| 723 |
if not path: |
| 724 |
defaultDir, defaultFile = get_verified_path("last_3dlut_path") |
| 725 |
if copy_from_path: |
| 726 |
defaultFile = os.path.basename(copy_from_path) |
| 727 |
# Only remember last used path if it was a deliberate user |
| 728 |
# choice via the filedialog |
| 729 |
remember_last_3dlut_path = True |
| 730 |
if copy_from_path and config.get_display_name() == "Resolve": |
| 731 |
# Find path to Resolve LUT folder |
| 732 |
if sys.platform == "win32": |
| 733 |
drives = win32api.GetLogicalDriveStrings() |
| 734 |
for drive in drives.split("\0")[:-1]: |
| 735 |
lut_dir = os.path.join( |
| 736 |
drive, |
| 737 |
"ProgramData", |
| 738 |
"Blackmagic Design", |
| 739 |
"DaVinci Resolve", |
| 740 |
"Support", |
| 741 |
"LUT", |
| 742 |
) |
| 743 |
if os.path.isdir(lut_dir): |
| 744 |
defaultDir = lut_dir |
| 745 |
remember_last_3dlut_path = False |
| 746 |
break |
| 747 |
else: |
| 748 |
# Assume OS X |
| 749 |
volumes = ["/", *safe_glob("/Volumes/*")] |
| 750 |
for volume in volumes: |
| 751 |
lut_dir = os.path.join( |
| 752 |
volume, |
| 753 |
"Library", |
| 754 |
"Application Support", |
| 755 |
"Blackmagic Design", |
| 756 |
"DaVinci Resolve", |
| 757 |
"LUT", |
| 758 |
) |
| 759 |
if os.path.isdir(lut_dir): |
| 760 |
defaultDir = lut_dir |
| 761 |
remember_last_3dlut_path = False |
| 762 |
break |
| 763 |
ext = self.getcfg("3dlut.format") |
| 764 |
if ( |
| 765 |
ext != "madVR" |
| 766 |
and not isinstance(self, LUT3DFrame) |
| 767 |
and self.getcfg("3dlut.output.profile.apply_cal") |
| 768 |
): |
| 769 |
# Check if there's a clash between current videoLUT |
| 770 |
# and 3D LUT (do both contain non-linear calibration?) |
| 771 |
profile_display_name = profile_out.getDeviceModelDescription() |
| 772 |
tempdir = self.worker.create_tempdir() |
| 773 |
if isinstance(tempdir, Exception): |
| 774 |
show_result_dialog(tempdir, self) |
| 775 |
return |
| 776 |
tempcal = os.path.join(tempdir, "temp.cal") |
| 777 |
cancel = False |
| 778 |
real_displays_count = 0 |
| 779 |
show_nonlinear_videolut_warning = False |
| 780 |
for i, display_name in enumerate(self.worker.display_names): |
| 781 |
if ( |
| 782 |
display_name == profile_display_name |
| 783 |
and display_name != "madVR" |
| 784 |
and self.worker.lut_access[i] |
| 785 |
and self.worker.save_current_video_lut( |
| 786 |
i + 1, tempcal, silent=True |
| 787 |
) |
| 788 |
is True |
| 789 |
and not cal_to_fake_profile(tempcal).tags.vcgt.is_linear() |
| 790 |
): |
| 791 |
show_nonlinear_videolut_warning = True |
| 792 |
break |
| 793 |
if not config.is_virtual_display(i): |
| 794 |
real_displays_count += 1 |
| 795 |
if show_nonlinear_videolut_warning or real_displays_count == 1: |
| 796 |
if copy_from_path: |
| 797 |
confirm = lang.getstr("3dlut.save_as") |
| 798 |
else: |
| 799 |
confirm = lang.getstr("3dlut.install") |
| 800 |
if not show_result_dialog( |
| 801 |
UnloggedWarning( |
| 802 |
lang.getstr("3dlut.1dlut.videolut.nonlinear") |
| 803 |
), |
| 804 |
self, |
| 805 |
confirm=confirm, |
| 806 |
): |
| 807 |
cancel = True |
| 808 |
if os.path.isfile(tempcal): |
| 809 |
os.remove(tempcal) |
| 810 |
if cancel: |
| 811 |
return |
| 812 |
if ext == "ReShade": |
| 813 |
dlg = wx.DirDialog( |
| 814 |
self, lang.getstr("3dlut.install"), defaultPath=defaultDir |
| 815 |
) |
| 816 |
else: |
| 817 |
if ext == "eeColor": |
| 818 |
ext = "txt" |
| 819 |
elif ext == "madVR": |
| 820 |
ext = "3dlut" |
| 821 |
elif ext == "icc": |
| 822 |
ext = PROFILE_EXT[1:] |
| 823 |
defaultFile = ( |
| 824 |
os.path.splitext( |
| 825 |
defaultFile |
| 826 |
or os.path.basename(config.DEFAULTS.get("last_3dlut_path")) |
| 827 |
)[0] |
| 828 |
+ "." |
| 829 |
+ ext |
| 830 |
) |
| 831 |
dlg = wx.FileDialog( |
| 832 |
self, |
| 833 |
lang.getstr("3dlut.save_as"), |
| 834 |
defaultDir=defaultDir, |
| 835 |
defaultFile=defaultFile, |
| 836 |
wildcard="*." + ext, |
| 837 |
style=wx.SAVE | wx.FD_OVERWRITE_PROMPT, |
| 838 |
) |
| 839 |
dlg.Center(wx.BOTH) |
| 840 |
if dlg.ShowModal() == wx.ID_OK: |
| 841 |
path = dlg.GetPath() |
| 842 |
if ext == "ReShade": |
| 843 |
path = os.path.join( |
| 844 |
path.rstrip(os.path.sep), "ColorLookupTable.png" |
| 845 |
) |
| 846 |
dlg.Destroy() |
| 847 |
checkoverwrite = False |
| 848 |
if path: |
| 849 |
if not waccess(path, os.W_OK): |
| 850 |
show_result_dialog( |
| 851 |
Error(lang.getstr("error.access_denied.write", path)), self |
| 852 |
) |
| 853 |
return |
| 854 |
if remember_last_3dlut_path: |
| 855 |
self.setcfg("last_3dlut_path", path) |
| 856 |
if checkoverwrite and os.path.isfile(path): |
| 857 |
dlg = ConfirmDialog( |
| 858 |
self, |
| 859 |
msg=lang.getstr("dialog.confirm_overwrite", (path)), |
| 860 |
ok=lang.getstr("overwrite"), |
| 861 |
cancel=lang.getstr("cancel"), |
| 862 |
bitmap=get_icon(32, "dialog-warning"), |
| 863 |
) |
| 864 |
result = dlg.ShowModal() |
| 865 |
dlg.Destroy() |
| 866 |
if result != wx.ID_OK: |
| 867 |
return |
| 868 |
if self.Parent: |
| 869 |
config.writecfg() |
| 870 |
elif isinstance(self, LUT3DFrame): |
| 871 |
config.writecfg( |
| 872 |
module="3DLUT-maker", |
| 873 |
options=( |
| 874 |
"3dlut.", |
| 875 |
"last_3dlut_path", |
| 876 |
"position.lut3dframe", |
| 877 |
"size.lut3dframe", |
| 878 |
), |
| 879 |
) |
| 880 |
if copy_from_path: |
| 881 |
# Instead of creating a 3D LUT, copy existing one |
| 882 |
src_name = os.path.splitext(copy_from_path)[0] |
| 883 |
dst_name = os.path.splitext(path)[0] |
| 884 |
src_paths = [copy_from_path] |
| 885 |
dst_paths = [path] |
| 886 |
if self.getcfg("3dlut.format") == "eeColor": |
| 887 |
# eeColor: 3D LUT + 6x 1D LUT |
| 888 |
for part in ("first", "second"): |
| 889 |
for channel in ("blue", "green", "red"): |
| 890 |
src_path = f"{src_name}-{part}1d{channel}.txt" |
| 891 |
if os.path.isfile(src_path): |
| 892 |
src_paths.append(src_path) |
| 893 |
dst_paths.append( |
| 894 |
f"{dst_name}-{part}1d{channel}.txt" |
| 895 |
) |
| 896 |
elif self.getcfg("3dlut.format") == "ReShade": |
| 897 |
dst_dir = os.path.dirname(path) |
| 898 |
# Check if MasterEffect is installed |
| 899 |
me_header_path = os.path.join(dst_dir, "MasterEffect.h") |
| 900 |
use_mclut3d = False |
| 901 |
if ( |
| 902 |
use_mclut3d |
| 903 |
and os.path.isfile(me_header_path) |
| 904 |
and os.path.isdir(os.path.join(dst_dir, "MasterEffect")) |
| 905 |
): |
| 906 |
dst_paths = [ |
| 907 |
os.path.join(dst_dir, "MasterEffect", "mclut3d.png") |
| 908 |
] |
| 909 |
# Alter existing MasterEffect.h |
| 910 |
with open(me_header_path, "rb") as me_header_file: |
| 911 |
me_header = me_header_file.read() |
| 912 |
# Enable LUT |
| 913 |
# (re.sub needs a bytes pattern here since |
| 914 |
# me_header was read as bytes -- a str |
| 915 |
# pattern raises TypeError) |
| 916 |
me_header = re.sub( |
| 917 |
rb"#define\s+USE_LUT\s+0", b"#define USE_LUT 1", me_header |
| 918 |
) |
| 919 |
# iLookupTableMode 2 = use mclut3d.png |
| 920 |
me_header = re.sub( |
| 921 |
rb"#define\s+iLookupTableMode\s+\d+", |
| 922 |
b"#define iLookupTableMode 2", |
| 923 |
me_header, |
| 924 |
) |
| 925 |
# Amount of color change by lookup table. |
| 926 |
# 1.0 means full effect. |
| 927 |
me_header = re.sub( |
| 928 |
rb"#define\s+fLookupTableMix\s+\d+(?:\.\d+)", |
| 929 |
b"#define fLookupTableMix 1.0", |
| 930 |
me_header, |
| 931 |
) |
| 932 |
with open(me_header_path, "wb") as me_header_file: |
| 933 |
me_header_file.write(me_header) |
| 934 |
else: |
| 935 |
# Write out our own shader |
| 936 |
clut_fx_path = get_data_path("ColorLookupTable.fx") |
| 937 |
if not clut_fx_path: |
| 938 |
show_result_dialog( |
| 939 |
Error( |
| 940 |
lang.getstr( |
| 941 |
"file.missing", "ColorLookupTable.fx" |
| 942 |
) |
| 943 |
), |
| 944 |
self, |
| 945 |
) |
| 946 |
return |
| 947 |
with open(clut_fx_path, "rb") as clut_fx_file: |
| 948 |
clut_fx = clut_fx_file.read() |
| 949 |
clut_size = self.getcfg("3dlut.size") |
| 950 |
clut_fx = strtr( |
| 951 |
clut_fx, |
| 952 |
{ |
| 953 |
"${VERSION}": VERSION_STRING, |
| 954 |
"${WIDTH}": str(clut_size**2), |
| 955 |
"${HEIGHT}": str(clut_size), |
| 956 |
"${FORMAT}": "RGBA{}".format( |
| 957 |
self.getcfg("3dlut.bitdepth.output") |
| 958 |
), |
| 959 |
}, |
| 960 |
) |
| 961 |
reshade_shaders = os.path.join(dst_dir, "reshade-shaders") |
| 962 |
if os.path.isdir(reshade_shaders): |
| 963 |
# ReShade >= 3.x with default shaders |
| 964 |
path = os.path.join( |
| 965 |
reshade_shaders, "Textures", os.path.basename(path) |
| 966 |
) |
| 967 |
dst_paths = [path] |
| 968 |
dst_dir = os.path.join(reshade_shaders, "Shaders") |
| 969 |
else: |
| 970 |
reshade_fx_path = os.path.join(dst_dir, "ReShade.fx") |
| 971 |
# Adjust path for correct installation if ReShade.fx |
| 972 |
# is a symlink. |
| 973 |
if islink(reshade_fx_path): |
| 974 |
# ReShade < 3.0 |
| 975 |
reshade_fx_path = readlink(reshade_fx_path) |
| 976 |
dst_dir = os.path.dirname(reshade_fx_path) |
| 977 |
path = os.path.join(dst_dir, os.path.basename(path)) |
| 978 |
dst_paths = [path] |
| 979 |
if os.path.isfile(reshade_fx_path): |
| 980 |
# ReShade < 3.0 |
| 981 |
# Alter existing ReShade.fx |
| 982 |
with open(reshade_fx_path, "rb") as reshade_fx_file: |
| 983 |
reshade_fx = reshade_fx_file.read() |
| 984 |
# Remove existing shader include |
| 985 |
# (re.sub needs a bytes pattern here since |
| 986 |
# reshade_fx was read as bytes -- a str |
| 987 |
# pattern raises TypeError) |
| 988 |
reshade_fx = re.sub( |
| 989 |
rb"[ \t]*//\s*Automatically\s+\S+\s+by\s+" |
| 990 |
+ re.escape(APPNAME.encode()) |
| 991 |
+ rb"\s+.+[ \t]*\r?\n?", |
| 992 |
b"", |
| 993 |
reshade_fx, |
| 994 |
) |
| 995 |
reshade_fx = re.sub( |
| 996 |
rb'[ \t]*#include\s+"ColorLookupTable.fx"' |
| 997 |
rb"[ \t]*\r?\n?", |
| 998 |
b"", |
| 999 |
reshade_fx, |
| 1000 |
).rstrip(b"\r\n") |
| 1001 |
reshade_fx += ( |
| 1002 |
f"{os.linesep * 2}// Automatically added by " |
| 1003 |
f"{APPNAME} {VERSION_STRING}{os.linesep}" |
| 1004 |
).encode() |
| 1005 |
reshade_fx += ( |
| 1006 |
'#include "ColorLookupTable.fx"' + os.linesep |
| 1007 |
).encode() |
| 1008 |
with open(reshade_fx_path, "wb") as reshade_fx_file: |
| 1009 |
reshade_fx_file.write(reshade_fx) |
| 1010 |
clut_fx_path = os.path.join(dst_dir, "ColorLookupTable.fx") |
| 1011 |
with open(clut_fx_path, "wb") as clut_fx_file: |
| 1012 |
clut_fx_file.write(clut_fx) |
| 1013 |
for src_path, dst_path in zip(src_paths, dst_paths): |
| 1014 |
shutil.copyfile(src_path, dst_path) |
| 1015 |
return |
| 1016 |
self.worker.interactive = False |
| 1017 |
self.worker.start( |
| 1018 |
self.lut3d_create_consumer, |
| 1019 |
self.lut3d_create_producer, |
| 1020 |
wargs=(profile_in, profile_abst, profile_out, path), |
| 1021 |
progress_msg=lang.getstr("3dlut.create"), |
| 1022 |
resume=not isinstance(self, LUT3DFrame) |
| 1023 |
and self.getcfg("3dlut.create"), |
| 1024 |
) |
| 1025 |
|
| 1026 |
def lut3d_create_producer(self, profile_in, profile_abst, profile_out, path): |
| 1027 |
"""Create a 3D LUT. |
| 1028 |
|
| 1029 |
Args: |
| 1030 |
profile_in (ICCProfile): Input profile. |
| 1031 |
profile_abst (ICCProfile or None): Abstract profile, if used. |
| 1032 |
profile_out (ICCProfile): Output profile. |
| 1033 |
path (str): Path to save the 3D LUT file. |
| 1034 |
|
| 1035 |
Returns: |
| 1036 |
bool or Exception: True if successful, or an Exception if an error |
| 1037 |
occurred. |
| 1038 |
""" |
| 1039 |
apply_cal = ( |
| 1040 |
profile_out |
| 1041 |
and isinstance(profile_out.tags.get("vcgt"), VideoCardGammaType) |
| 1042 |
and ( |
| 1043 |
self.getcfg("3dlut.output.profile.apply_cal") |
| 1044 |
or not hasattr(self, "lut3d_apply_cal_cb") |
| 1045 |
) |
| 1046 |
) |
| 1047 |
input_encoding = self.getcfg("3dlut.encoding.input") |
| 1048 |
output_encoding = self.getcfg("3dlut.encoding.output") |
| 1049 |
if self.getcfg("3dlut.apply_trc") or not hasattr( |
| 1050 |
self, "lut3d_trc_apply_none_ctrl" |
| 1051 |
): |
| 1052 |
if ( |
| 1053 |
self.getcfg("3dlut.trc").startswith("smpte2084") |
| 1054 |
or self.getcfg("3dlut.trc") == "hlg" # SMPTE ST.2084 (PQ) |
| 1055 |
): # Hybrid Log-Gamma (HLG) |
| 1056 |
trc_gamma = self.getcfg("3dlut.trc") |
| 1057 |
else: |
| 1058 |
trc_gamma = self.getcfg("3dlut.trc_gamma") |
| 1059 |
else: |
| 1060 |
trc_gamma = None |
| 1061 |
XYZwp = None |
| 1062 |
if not isinstance(self, LUT3DFrame): |
| 1063 |
x = self.getcfg("3dlut.whitepoint.x", False) |
| 1064 |
y = self.getcfg("3dlut.whitepoint.y", False) |
| 1065 |
if x and y: |
| 1066 |
XYZwp = colormath.xyY2XYZ(x, y) |
| 1067 |
trc_gamma_type = self.getcfg("3dlut.trc_gamma_type") |
| 1068 |
outoffset = self.getcfg("3dlut.trc_output_offset") |
| 1069 |
intent = self.getcfg("3dlut.rendering_intent") |
| 1070 |
file_format = self.getcfg("3dlut.format") |
| 1071 |
size = self.getcfg("3dlut.size") |
| 1072 |
input_bits = self.getcfg("3dlut.bitdepth.input") |
| 1073 |
output_bits = self.getcfg("3dlut.bitdepth.output") |
| 1074 |
apply_black_offset = self.getcfg("3dlut.apply_black_offset") |
| 1075 |
use_b2a = self.getcfg("3dlut.gamap.use_b2a") |
| 1076 |
white_cdm2 = self.getcfg("3dlut.hdr_peak_luminance") |
| 1077 |
minmll = self.getcfg("3dlut.hdr_minmll") |
| 1078 |
maxmll = self.getcfg("3dlut.hdr_maxmll") |
| 1079 |
ambient_cdm2 = self.getcfg("3dlut.hdr_ambient_luminance") |
| 1080 |
content_rgb_space = [1.0, [], [], [], []] |
| 1081 |
for i, color in enumerate(("white", "red", "green", "blue")): |
| 1082 |
for coord in "xy": |
| 1083 |
v = self.getcfg(f"3dlut.content.colorspace.{color}.{coord}") |
| 1084 |
content_rgb_space[i + 1].append(v) |
| 1085 |
# Dummy Y value, not used for primaries but needs to be present |
| 1086 |
content_rgb_space[i + 1].append(1.0) |
| 1087 |
content_rgb_space[1] = colormath.xyY2XYZ(*content_rgb_space[1]) |
| 1088 |
content_rgb_space = colormath.get_rgb_space(content_rgb_space) |
| 1089 |
try: |
| 1090 |
# We need to make sure the link between the original profile object |
| 1091 |
# and the one we're eventually going to change is broken |
| 1092 |
profile_in = ICCProfile(profile_in.filename) |
| 1093 |
self.worker.create_3dlut( |
| 1094 |
profile_in, |
| 1095 |
path, |
| 1096 |
profile_abst, |
| 1097 |
profile_out, |
| 1098 |
apply_cal=apply_cal, |
| 1099 |
intent=intent, |
| 1100 |
file_format=file_format, |
| 1101 |
size=size, |
| 1102 |
input_bits=input_bits, |
| 1103 |
output_bits=output_bits, |
| 1104 |
input_encoding=input_encoding, |
| 1105 |
output_encoding=output_encoding, |
| 1106 |
trc_gamma=trc_gamma, |
| 1107 |
trc_gamma_type=trc_gamma_type, |
| 1108 |
trc_output_offset=outoffset, |
| 1109 |
apply_black_offset=apply_black_offset, |
| 1110 |
use_b2a=use_b2a, |
| 1111 |
white_cdm2=white_cdm2, |
| 1112 |
minmll=minmll, |
| 1113 |
maxmll=maxmll, |
| 1114 |
use_alternate_master_white_clip=self.getcfg( |
| 1115 |
"3dlut.hdr_maxmll_alt_clip" |
| 1116 |
), |
| 1117 |
hdr_sat=self.getcfg("3dlut.hdr_sat"), |
| 1118 |
hdr_hue=self.getcfg("3dlut.hdr_hue"), |
| 1119 |
ambient_cdm2=ambient_cdm2, |
| 1120 |
content_rgb_space=content_rgb_space, |
| 1121 |
hdr_display=self.getcfg("3dlut.hdr_display"), |
| 1122 |
XYZwp=XYZwp, |
| 1123 |
) |
| 1124 |
except Exception as exception: |
| 1125 |
# if exception.__class__.__name__ in dir(exceptions): |
| 1126 |
# raise |
| 1127 |
if DEBUG: |
| 1128 |
messages = traceback.format_exception( |
| 1129 |
type(exception), exception, exception.__traceback__ |
| 1130 |
) |
| 1131 |
print("[D] Worker raised exception: \n" + "\n".join(messages)) |
| 1132 |
return exception |
| 1133 |
return True |
| 1134 |
|
| 1135 |
def lut3d_format_ctrl_handler(self, event): |
| 1136 |
"""Handle changes to the 3D LUT format control. |
| 1137 |
|
| 1138 |
Args: |
| 1139 |
event (wx.Event): The event triggered by the control. |
| 1140 |
""" |
| 1141 |
# Get selected format |
| 1142 |
file_format = self.lut3d_formats_ab[self.lut3d_format_ctrl.GetSelection()] |
| 1143 |
encoding_overrides = ("dcl", "eeColor", "madVR", "ReShade") |
| 1144 |
size_overrides = ("dcl", "eeColor", "madVR", "mga", "ReShade") |
| 1145 |
if ( |
| 1146 |
self.getcfg("3dlut.format") in encoding_overrides |
| 1147 |
and file_format not in encoding_overrides |
| 1148 |
): |
| 1149 |
# If previous format forced specific encoding, restore encoding |
| 1150 |
self.setcfg( |
| 1151 |
"3dlut.encoding.input", self.getcfg("3dlut.encoding.input.backup") |
| 1152 |
) |
| 1153 |
self.setcfg( |
| 1154 |
"3dlut.encoding.output", self.getcfg("3dlut.encoding.output.backup") |
| 1155 |
) |
| 1156 |
if self.getcfg("3dlut.format") in size_overrides: |
| 1157 |
# If previous format forced specific size, restore size |
| 1158 |
self.setcfg("3dlut.size", self.getcfg("3dlut.size.backup")) |
| 1159 |
if ( |
| 1160 |
self.getcfg("3dlut.format") not in encoding_overrides |
| 1161 |
and file_format in encoding_overrides |
| 1162 |
): |
| 1163 |
# If selected format forces specific encoding, backup current encoding |
| 1164 |
self.setcfg( |
| 1165 |
"3dlut.encoding.input.backup", self.getcfg("3dlut.encoding.input") |
| 1166 |
) |
| 1167 |
self.setcfg( |
| 1168 |
"3dlut.encoding.output.backup", self.getcfg("3dlut.encoding.output") |
| 1169 |
) |
| 1170 |
# Set selected format |
| 1171 |
self.lut3d_set_option("3dlut.format", file_format) |
| 1172 |
if file_format in size_overrides: |
| 1173 |
# If selected format forces specific size, backup current size |
| 1174 |
self.setcfg("3dlut.size.backup", self.getcfg("3dlut.size")) |
| 1175 |
if file_format == "eeColor": |
| 1176 |
# -et -Et for eeColor |
| 1177 |
if self.getcfg("3dlut.encoding.input") not in ("t", "T"): |
| 1178 |
self.lut3d_set_option("3dlut.encoding.input", "t") |
| 1179 |
self.lut3d_set_option("3dlut.encoding.output", "t") |
| 1180 |
# eeColor uses a fixed size of 65x65x65 |
| 1181 |
self.lut3d_set_option("3dlut.size", 65) |
| 1182 |
elif file_format == "mga": |
| 1183 |
# Pandora uses a fixed bitdepth of 16 |
| 1184 |
self.lut3d_set_option("3dlut.bitdepth.output", 16) |
| 1185 |
self.lut3d_bitdepth_output_ctrl.SetSelection(self.lut3d_bitdepth_ba[16]) |
| 1186 |
elif file_format == "madVR": |
| 1187 |
# -et -Et for madVR |
| 1188 |
if self.getcfg("3dlut.encoding.input") not in ("t", "T"): |
| 1189 |
self.lut3d_set_option("3dlut.encoding.input", "t") |
| 1190 |
self.lut3d_set_option("3dlut.encoding.output", "t") |
| 1191 |
# collink says madVR works best with 65 |
| 1192 |
self.lut3d_set_option("3dlut.size", 65) |
| 1193 |
elif file_format in ("png", "ReShade"): |
| 1194 |
if file_format == "ReShade": |
| 1195 |
self.lut3d_set_option("3dlut.encoding.input", "n") |
| 1196 |
self.lut3d_set_option("3dlut.encoding.output", "n") |
| 1197 |
self.lut3d_set_option("3dlut.bitdepth.output", 8) |
| 1198 |
elif self.getcfg("3dlut.bitdepth.output") not in (8, 16): |
| 1199 |
self.lut3d_set_option("3dlut.bitdepth.output", 8) |
| 1200 |
self.lut3d_bitdepth_output_ctrl.SetSelection( |
| 1201 |
self.lut3d_bitdepth_ba[self.getcfg("3dlut.bitdepth.output")] |
| 1202 |
) |
| 1203 |
elif file_format == "dcl": |
| 1204 |
self.lut3d_set_option("3dlut.encoding.input", "n") |
| 1205 |
self.lut3d_set_option("3dlut.encoding.output", "n") |
| 1206 |
self.lut3d_set_option("3dlut.size", 33) |
| 1207 |
self.lut3d_set_option("3dlut.bitdepth.output", 12) |
| 1208 |
self.lut3d_bitdepth_output_ctrl.SetSelection(self.lut3d_bitdepth_ba[12]) |
| 1209 |
size = self.getcfg("3dlut.size") |
| 1210 |
snap_size = self.lut3d_snap_size(size) |
| 1211 |
if snap_size != size: |
| 1212 |
self.lut3d_set_option("3dlut.size", snap_size) |
| 1213 |
self.lut3d_size_ctrl.SetSelection(self.lut3d_size_ba[self.getcfg("3dlut.size")]) |
| 1214 |
self.lut3d_update_encoding_controls() |
| 1215 |
self.lut3d_enable_size_controls() |
| 1216 |
self.lut3d_show_bitdepth_controls() |
| 1217 |
if not isinstance(self, LUT3DFrame): |
| 1218 |
self.panel.Freeze() |
| 1219 |
self.lut3d_show_controls() |
| 1220 |
self.calpanel.Layout() |
| 1221 |
self.update_main_controls() |
| 1222 |
self.panel.Thaw() |
| 1223 |
if getattr(self, "lut3dframe", None): |
| 1224 |
self.lut3dframe.lut3d_update_shared_controls() |
| 1225 |
return |
| 1226 |
self.panel.Freeze() |
| 1227 |
self.lut3d_show_hdr_display_control() |
| 1228 |
self.panel.Layout() |
| 1229 |
self.panel.Thaw() |
| 1230 |
self.update_layout() |
| 1231 |
if self.Parent: |
| 1232 |
self.Parent.lut3d_update_shared_controls() |
| 1233 |
self.lut3d_create_btn.Enable( |
| 1234 |
file_format != "madVR" or self.output_profile_ctrl.IsShown() |
| 1235 |
) |
| 1236 |
|
| 1237 |
def lut3d_size_ctrl_handler(self, event): |
| 1238 |
"""Handle changes to the 3D LUT size control. |
| 1239 |
|
| 1240 |
Args: |
| 1241 |
event (wx.Event): The event triggered by the control. |
| 1242 |
""" |
| 1243 |
size = self.lut3d_size_ab[self.lut3d_size_ctrl.GetSelection()] |
| 1244 |
snap_size = self.lut3d_snap_size(size) |
| 1245 |
if snap_size != size: |
| 1246 |
wx.Bell() |
| 1247 |
self.lut3d_size_ctrl.SetSelection(self.lut3d_size_ba[snap_size]) |
| 1248 |
self.lut3d_set_option("3dlut.size", snap_size) |
| 1249 |
if getattr(self, "lut3dframe", None): |
| 1250 |
self.lut3dframe.lut3d_update_shared_controls() |
| 1251 |
elif self.Parent: |
| 1252 |
self.Parent.lut3d_update_shared_controls() |
| 1253 |
|
| 1254 |
def lut3d_snap_size(self, size): |
| 1255 |
"""Snap size to valid 3D LUT size. |
| 1256 |
|
| 1257 |
Returns: |
| 1258 |
int: Snapped size. |
| 1259 |
""" |
| 1260 |
if self.getcfg("3dlut.format") == "mga" and size not in (17, 33): |
| 1261 |
size = 17 if size < 33 else 33 |
| 1262 |
elif self.getcfg("3dlut.format") == "ReShade" and size not in (16, 32, 64): |
| 1263 |
if size < 32: |
| 1264 |
size = 16 |
| 1265 |
elif size < 64: |
| 1266 |
size = 32 |
| 1267 |
else: |
| 1268 |
size = 64 |
| 1269 |
return size |
| 1270 |
|
| 1271 |
def lut3d_gamut_mapping_mode_handler(self, event): |
| 1272 |
"""Handle changes to the gamut mapping mode control.""" |
| 1273 |
self.lut3d_set_option( |
| 1274 |
"3dlut.gamap.use_b2a", int(self.gamut_mapping_b2a.GetValue()) |
| 1275 |
) |
| 1276 |
if getattr(self, "lut3dframe", None): |
| 1277 |
self.lut3dframe.update_controls() |
| 1278 |
elif self.Parent: |
| 1279 |
self.Parent.lut3d_update_b2a_controls() |
| 1280 |
|
| 1281 |
def lut3d_rendering_intent_ctrl_handler(self, event): |
| 1282 |
"""Handle changes to the rendering intent control.""" |
| 1283 |
self.lut3d_set_option( |
| 1284 |
"3dlut.rendering_intent", |
| 1285 |
self.rendering_intents_ab[self.lut3d_rendering_intent_ctrl.GetSelection()], |
| 1286 |
) |
| 1287 |
if getattr(self, "lut3dframe", None): |
| 1288 |
self.lut3dframe.lut3d_update_shared_controls() |
| 1289 |
else: |
| 1290 |
if isinstance(self, LUT3DFrame): |
| 1291 |
self.lut3d_show_input_value_clipping_warning(True) |
| 1292 |
if self.Parent: |
| 1293 |
self.Parent.lut3d_update_shared_controls() |
| 1294 |
|
| 1295 |
def lut3d_setup_language(self): |
| 1296 |
"""Set up language for 3D LUT controls.""" |
| 1297 |
# Shared with main window |
| 1298 |
tone_curve_names = ( |
| 1299 |
"Gamma 2.2", |
| 1300 |
"trc.rec1886", |
| 1301 |
"trc.smpte2084.hardclip", |
| 1302 |
"trc.smpte2084.rolloffclip", |
| 1303 |
"trc.hlg", |
| 1304 |
"custom", |
| 1305 |
) |
| 1306 |
self.lut3d_trc_ctrl.SetItems([lang.getstr(item) for item in tone_curve_names]) |
| 1307 |
self.trc_gamma_types_ab = {0: "b", 1: "B"} |
| 1308 |
self.trc_gamma_types_ba = {"b": 0, "B": 1} |
| 1309 |
self.lut3d_trc_gamma_type_ctrl.SetItems( |
| 1310 |
[lang.getstr("trc.type.relative"), lang.getstr("trc.type.absolute")] |
| 1311 |
) |
| 1312 |
|
| 1313 |
self.lut3d_content_colorspace_names = ["Rec. 2020", "DCI P3 D65", "Rec. 709"] |
| 1314 |
self.lut3d_content_colorspace_ctrl.SetItems( |
| 1315 |
[*self.lut3d_content_colorspace_names, lang.getstr("custom")] |
| 1316 |
) |
| 1317 |
|
| 1318 |
self.rendering_intents_ab = {} |
| 1319 |
self.rendering_intents_ba = {} |
| 1320 |
self.lut3d_rendering_intent_ctrl.Clear() |
| 1321 |
intents = list(config.VALID_VALUES["3dlut.rendering_intent"]) |
| 1322 |
if self.worker.argyll_version < [1, 8, 3]: |
| 1323 |
intents.remove("lp") |
| 1324 |
for i, ri in enumerate(intents): |
| 1325 |
self.lut3d_rendering_intent_ctrl.Append(lang.getstr("gamap.intents." + ri)) |
| 1326 |
self.rendering_intents_ab[i] = ri |
| 1327 |
self.rendering_intents_ba[ri] = i |
| 1328 |
|
| 1329 |
self.lut3d_formats_ab = {} |
| 1330 |
self.lut3d_formats_ba = {} |
| 1331 |
self.lut3d_format_ctrl.Clear() |
| 1332 |
i = 0 |
| 1333 |
for file_format in config.VALID_VALUES["3dlut.format"]: |
| 1334 |
if file_format != "madVR" or self.worker.argyll_version >= [1, 6]: |
| 1335 |
self.lut3d_format_ctrl.Append( |
| 1336 |
lang.getstr(f"3dlut.format.{file_format}") |
| 1337 |
) |
| 1338 |
self.lut3d_formats_ab[i] = file_format |
| 1339 |
self.lut3d_formats_ba[file_format] = i |
| 1340 |
i += 1 |
| 1341 |
|
| 1342 |
self.lut3d_hdr_display_ctrl.SetItems( |
| 1343 |
[ |
| 1344 |
lang.getstr(item) |
| 1345 |
for item in ("3dlut.format.madVR.hdr_to_sdr", "3dlut.format.madVR.hdr") |
| 1346 |
] |
| 1347 |
) |
| 1348 |
|
| 1349 |
self.lut3d_size_ab = {} |
| 1350 |
self.lut3d_size_ba = {} |
| 1351 |
self.lut3d_size_ctrl.Clear() |
| 1352 |
for i, size in enumerate(config.VALID_VALUES["3dlut.size"]): |
| 1353 |
self.lut3d_size_ctrl.Append("%sx%sx%s" % ((size,) * 3)) |
| 1354 |
self.lut3d_size_ab[i] = size |
| 1355 |
self.lut3d_size_ba[size] = i |
| 1356 |
|
| 1357 |
self.lut3d_bitdepth_ab = {} |
| 1358 |
self.lut3d_bitdepth_ba = {} |
| 1359 |
self.lut3d_bitdepth_input_ctrl.Clear() |
| 1360 |
self.lut3d_bitdepth_output_ctrl.Clear() |
| 1361 |
for i, bitdepth in enumerate(config.VALID_VALUES["3dlut.bitdepth.input"]): |
| 1362 |
self.lut3d_bitdepth_input_ctrl.Append(str(bitdepth)) |
| 1363 |
self.lut3d_bitdepth_output_ctrl.Append(str(bitdepth)) |
| 1364 |
self.lut3d_bitdepth_ab[i] = bitdepth |
| 1365 |
self.lut3d_bitdepth_ba[bitdepth] = i |
| 1366 |
|
| 1367 |
def lut3d_setup_encoding_ctrl(self): |
| 1368 |
"""Set up encoding controls for 3D LUTs.""" |
| 1369 |
file_format = self.getcfg("3dlut.format") |
| 1370 |
# Shared with amin window |
| 1371 |
if file_format == "madVR": |
| 1372 |
encodings = ["t"] |
| 1373 |
config.DEFAULTS["3dlut.encoding.input"] = "t" |
| 1374 |
config.DEFAULTS["3dlut.encoding.output"] = "t" |
| 1375 |
else: |
| 1376 |
encodings = ["n"] if file_format == "dcl" else list(VIDEO_ENCODINGS) |
| 1377 |
config.DEFAULTS["3dlut.encoding.input"] = "n" |
| 1378 |
config.DEFAULTS["3dlut.encoding.output"] = "n" |
| 1379 |
if ( |
| 1380 |
self.worker.argyll_version >= [1, 7] |
| 1381 |
and self.worker.argyll_version != [1, 7, 0, "_beta"] |
| 1382 |
and file_format != "dcl" |
| 1383 |
): |
| 1384 |
# Argyll 1.7 beta 3 (2015-04-02) added clip WTW on input TV encoding |
| 1385 |
encodings.insert(2, "T") |
| 1386 |
config.VALID_VALUES["3dlut.encoding.input"] = encodings |
| 1387 |
# collink: xvYCC output encoding is not supported |
| 1388 |
config.VALID_VALUES["3dlut.encoding.output"] = [ |
| 1389 |
v for v in encodings if v not in ("T", "x", "X") |
| 1390 |
] |
| 1391 |
self.encoding_input_ab = {} |
| 1392 |
self.encoding_input_ba = {} |
| 1393 |
self.encoding_output_ab = {} |
| 1394 |
self.encoding_output_ba = {} |
| 1395 |
self.encoding_input_ctrl.Freeze() |
| 1396 |
self.encoding_input_ctrl.Clear() |
| 1397 |
self.encoding_output_ctrl.Freeze() |
| 1398 |
self.encoding_output_ctrl.Clear() |
| 1399 |
for i, encoding in enumerate(config.VALID_VALUES["3dlut.encoding.input"]): |
| 1400 |
lstr = lang.getstr(f"3dlut.encoding.type_{encoding}") |
| 1401 |
self.encoding_input_ctrl.Append(lstr) |
| 1402 |
self.encoding_input_ab[i] = encoding |
| 1403 |
self.encoding_input_ba[encoding] = i |
| 1404 |
for o, encoding in enumerate(config.VALID_VALUES["3dlut.encoding.output"]): |
| 1405 |
lstr = lang.getstr(f"3dlut.encoding.type_{encoding}") |
| 1406 |
self.encoding_output_ctrl.Append(lstr) |
| 1407 |
self.encoding_output_ab[o] = encoding |
| 1408 |
self.encoding_output_ba[encoding] = o |
| 1409 |
self.encoding_input_ctrl.Thaw() |
| 1410 |
self.encoding_output_ctrl.Thaw() |
| 1411 |
|
| 1412 |
def lut3d_set_option(self, option, v, set_changed=True): |
| 1413 |
"""Set option to value and update settings state.""" |
| 1414 |
if ( |
| 1415 |
hasattr(self, "profile_settings_changed") |
| 1416 |
and set_changed |
| 1417 |
and self.getcfg("3dlut.create") |
| 1418 |
and v != self.getcfg(option) |
| 1419 |
): |
| 1420 |
self.profile_settings_changed() |
| 1421 |
self.setcfg(option, v) |
| 1422 |
if option in ( |
| 1423 |
"3dlut.hdr_peak_luminance", |
| 1424 |
"3dlut.hdr_minmll", |
| 1425 |
"3dlut.hdr_maxmll", |
| 1426 |
): |
| 1427 |
self.lut3d_show_hdr_maxmll_alt_clip_ctrl() |
| 1428 |
self.lut3d_hdr_update_diffuse_white() |
| 1429 |
elif option == "3dlut.hdr_ambient_luminance": |
| 1430 |
self.lut3d_hdr_update_system_gamma() |
| 1431 |
|
| 1432 |
def lut3d_hdr_update_diffuse_white(self): |
| 1433 |
"""Update diffuse white for HDR roll-off.""" |
| 1434 |
# Update knee start info for BT.2390-3 roll-off |
| 1435 |
bt2390 = colormath.BT2390( |
| 1436 |
0, |
| 1437 |
self.getcfg("3dlut.hdr_peak_luminance"), |
| 1438 |
self.getcfg("3dlut.hdr_minmll"), |
| 1439 |
self.getcfg("3dlut.hdr_maxmll"), |
| 1440 |
self.getcfg("3dlut.hdr_maxmll_alt_clip"), |
| 1441 |
) |
| 1442 |
diffuse_ref_cdm2 = 94.37844 |
| 1443 |
diffuse_PQ = colormath.special_pow(diffuse_ref_cdm2 / 10000, 1.0 / -2084) |
| 1444 |
# Determine white cd/m2 after roll-off |
| 1445 |
diffuse_tgt_cdm2 = ( |
| 1446 |
colormath.special_pow(bt2390.apply(diffuse_PQ), -2084) * 10000 |
| 1447 |
) |
| 1448 |
signalcolor = "#CC0000" if diffuse_tgt_cdm2 < diffuse_ref_cdm2 else "#008000" |
| 1449 |
self.lut3d_hdr_diffuse_white_txt.ForegroundColour = signalcolor |
| 1450 |
self.lut3d_hdr_diffuse_white_txt.Label = f"{diffuse_tgt_cdm2:.2f}" |
| 1451 |
self.lut3d_hdr_diffuse_white_txt_label.ForegroundColour = signalcolor |
| 1452 |
self.lut3d_hdr_diffuse_white_txt.ContainingSizer.Layout() |
| 1453 |
|
| 1454 |
def lut3d_hdr_update_sat_val(self): |
| 1455 |
"""Update saturation and value for HDR saturation control.""" |
| 1456 |
v = self.getcfg("3dlut.hdr_sat") * 100 |
| 1457 |
self.lut3d_hdr_sat_ctrl_lum_val.Label = f"{100 - v}%" |
| 1458 |
self.lut3d_hdr_sat_ctrl_sat_val.Label = f"{v}%" |
| 1459 |
|
| 1460 |
def lut3d_hdr_update_system_gamma(self): |
| 1461 |
"""Update system gamma for HLG based on ambient luminance.""" |
| 1462 |
# Update system gamma for HLG based on ambient luminance (BT.2390-3) |
| 1463 |
hlg = colormath.HLG(ambient_cdm2=self.getcfg("3dlut.hdr_ambient_luminance")) |
| 1464 |
self.lut3d_hdr_system_gamma_txt.Label = str(stripzeros(f"{hlg.gamma:.4f}")) |
| 1465 |
|
| 1466 |
def lut3d_update_shared_controls(self): |
| 1467 |
"""Update shared controls in the main window.""" |
| 1468 |
# Shared with main window |
| 1469 |
self.lut3d_update_trc_controls() |
| 1470 |
self.lut3d_rendering_intent_ctrl.SetSelection( |
| 1471 |
self.rendering_intents_ba[self.getcfg("3dlut.rendering_intent")] |
| 1472 |
) |
| 1473 |
# MadVR only available with Argyll 1.6+, fall back to default |
| 1474 |
self.lut3d_format_ctrl.SetSelection( |
| 1475 |
self.lut3d_formats_ba.get( |
| 1476 |
self.getcfg("3dlut.format"), |
| 1477 |
self.lut3d_formats_ba[DEFAULTS["3dlut.format"]], |
| 1478 |
) |
| 1479 |
) |
| 1480 |
self.lut3d_hdr_display_ctrl.SetSelection(self.getcfg("3dlut.hdr_display")) |
| 1481 |
self.lut3d_size_ctrl.SetSelection(self.lut3d_size_ba[self.getcfg("3dlut.size")]) |
| 1482 |
self.lut3d_enable_size_controls() |
| 1483 |
self.lut3d_bitdepth_input_ctrl.SetSelection( |
| 1484 |
self.lut3d_bitdepth_ba[self.getcfg("3dlut.bitdepth.input")] |
| 1485 |
) |
| 1486 |
self.lut3d_bitdepth_output_ctrl.SetSelection( |
| 1487 |
self.lut3d_bitdepth_ba[self.getcfg("3dlut.bitdepth.output")] |
| 1488 |
) |
| 1489 |
self.lut3d_show_bitdepth_controls() |
| 1490 |
if self.Parent: |
| 1491 |
self.Parent.lut3d_update_shared_controls() |
| 1492 |
|
| 1493 |
def lut3d_update_trc_control(self): |
| 1494 |
"""Update the TRC control based on the current settings.""" |
| 1495 |
if self.getcfg("3dlut.trc").startswith("smpte2084"): # SMPTE 2084 |
| 1496 |
sel = 2 if self.getcfg("3dlut.trc") == "smpte2084.hardclip" else 3 |
| 1497 |
self.lut3d_trc_ctrl.SetSelection(sel) |
| 1498 |
elif self.getcfg("3dlut.trc") == "hlg": # Hybrid Log-Gamma (HLG) |
| 1499 |
self.lut3d_trc_ctrl.SetSelection(4) |
| 1500 |
elif ( |
| 1501 |
self.getcfg("3dlut.trc_gamma_type") == "B" |
| 1502 |
and self.getcfg("3dlut.trc_output_offset") == 0 |
| 1503 |
and self.getcfg("3dlut.trc_gamma") == 2.4 |
| 1504 |
): |
| 1505 |
self.lut3d_trc_ctrl.SetSelection(1) # BT.1886 |
| 1506 |
self.setcfg("3dlut.trc", "bt1886") |
| 1507 |
elif ( |
| 1508 |
self.getcfg("3dlut.trc_gamma_type") == "b" |
| 1509 |
and self.getcfg("3dlut.trc_output_offset") == 1 |
| 1510 |
and self.getcfg("3dlut.trc_gamma") == 2.2 |
| 1511 |
): |
| 1512 |
self.lut3d_trc_ctrl.SetSelection(0) # Pure power gamma 2.2 |
| 1513 |
self.setcfg("3dlut.trc", "gamma2.2") |
| 1514 |
else: |
| 1515 |
self.lut3d_trc_ctrl.SetSelection(5) # Custom |
| 1516 |
self.setcfg("3dlut.trc", "customgamma") |
| 1517 |
|
| 1518 |
def lut3d_update_trc_controls(self): |
| 1519 |
"""Update the TRC controls based on the current settings.""" |
| 1520 |
self.lut3d_update_trc_control() |
| 1521 |
self.lut3d_trc_gamma_ctrl.SetValue(str(self.getcfg("3dlut.trc_gamma"))) |
| 1522 |
self.lut3d_trc_gamma_type_ctrl.SetSelection( |
| 1523 |
self.trc_gamma_types_ba[self.getcfg("3dlut.trc_gamma_type")] |
| 1524 |
) |
| 1525 |
outoffset = int(self.getcfg("3dlut.trc_output_offset") * 100) |
| 1526 |
self.lut3d_trc_black_output_offset_ctrl.SetValue(outoffset) |
| 1527 |
self.lut3d_trc_black_output_offset_intctrl.SetValue(outoffset) |
| 1528 |
target_peak = self.getcfg("3dlut.hdr_peak_luminance") |
| 1529 |
maxmll = self.getcfg("3dlut.hdr_maxmll") |
| 1530 |
# Don't allow maxmll < target peak. Technically this restriction does |
| 1531 |
# not exist, but practically maxmll < target peak doesn't make sense. |
| 1532 |
if maxmll < target_peak: |
| 1533 |
maxmll = target_peak |
| 1534 |
self.setcfg("3dlut.hdr_maxmll", maxmll) |
| 1535 |
self.lut3d_hdr_maxmll_ctrl.SetRange(target_peak, 10000) |
| 1536 |
self.lut3d_hdr_peak_luminance_ctrl.SetValue(target_peak) |
| 1537 |
self.lut3d_hdr_minmll_ctrl.SetValue(self.getcfg("3dlut.hdr_minmll")) |
| 1538 |
self.lut3d_hdr_maxmll_ctrl.SetValue(maxmll) |
| 1539 |
self.lut3d_hdr_maxmll_alt_clip_cb.SetValue( |
| 1540 |
not bool(self.getcfg("3dlut.hdr_maxmll_alt_clip")) |
| 1541 |
) |
| 1542 |
self.lut3d_hdr_update_diffuse_white() |
| 1543 |
self.lut3d_hdr_ambient_luminance_ctrl.SetValue( |
| 1544 |
self.getcfg("3dlut.hdr_ambient_luminance") |
| 1545 |
) |
| 1546 |
self.lut3d_hdr_update_system_gamma() |
| 1547 |
# Content colorspace (currently only used for SMPTE 2084) |
| 1548 |
content_colors = [] |
| 1549 |
for color in ("red", "green", "blue", "white"): |
| 1550 |
for coord in "xy": |
| 1551 |
v = self.getcfg(f"3dlut.content.colorspace.{color}.{coord}") |
| 1552 |
getattr(self, f"lut3d_content_colorspace_{color}_{coord}").SetValue(v) |
| 1553 |
content_colors.append(round(v, 4)) |
| 1554 |
rgb_space_name = colormath.find_primaries_wp_xy_rgb_space_name( |
| 1555 |
content_colors, self.lut3d_content_colorspace_names |
| 1556 |
) |
| 1557 |
if rgb_space_name: |
| 1558 |
i = self.lut3d_content_colorspace_names.index(rgb_space_name) |
| 1559 |
else: |
| 1560 |
i = self.lut3d_content_colorspace_ctrl.Count - 1 |
| 1561 |
self.lut3d_content_colorspace_ctrl.SetSelection(i) |
| 1562 |
self.lut3d_hdr_sat_ctrl.SetValue(round(self.getcfg("3dlut.hdr_sat") * 100)) |
| 1563 |
self.lut3d_hdr_update_sat_val() |
| 1564 |
hue = round(self.getcfg("3dlut.hdr_hue") * 100) |
| 1565 |
self.lut3d_hdr_hue_ctrl.SetValue(hue) |
| 1566 |
self.lut3d_hdr_hue_intctrl.SetValue(hue) |
| 1567 |
|
| 1568 |
def lut3d_show_bitdepth_controls(self): |
| 1569 |
"""Show or hide the bitdepth controls.""" |
| 1570 |
frozen = self.IsFrozen() |
| 1571 |
if not frozen: |
| 1572 |
self.Freeze() |
| 1573 |
show = True |
| 1574 |
input_show = show and self.getcfg("3dlut.format") == "3dl" |
| 1575 |
self.lut3d_bitdepth_input_label.Show(input_show) |
| 1576 |
self.lut3d_bitdepth_input_ctrl.Show(input_show) |
| 1577 |
output_show = show and self.getcfg("3dlut.format") in ("3dl", "png") |
| 1578 |
self.lut3d_bitdepth_output_label.Show(output_show) |
| 1579 |
self.lut3d_bitdepth_output_ctrl.Show(output_show) |
| 1580 |
if isinstance(self, LUT3DFrame): |
| 1581 |
self.panel.GetSizer().Layout() |
| 1582 |
self.update_layout() |
| 1583 |
else: |
| 1584 |
self.update_scrollbars() |
| 1585 |
if not frozen: |
| 1586 |
self.Thaw() |
| 1587 |
|
| 1588 |
def lut3d_show_hdr_display_control(self): |
| 1589 |
"""Show or hide the HDR display control.""" |
| 1590 |
self.lut3d_hdr_display_ctrl.Show( |
| 1591 |
( |
| 1592 |
self.getcfg("3dlut.apply_trc") |
| 1593 |
or not hasattr(self, "lut3d_trc_apply_none_ctrl") |
| 1594 |
) |
| 1595 |
and self.getcfg("3dlut.trc").startswith("smpte2084") |
| 1596 |
and self.getcfg("3dlut.format") == "madVR" |
| 1597 |
) |
| 1598 |
|
| 1599 |
def lut3d_show_hdr_maxmll_alt_clip_ctrl(self): |
| 1600 |
"""Show or hide the alternate master white clipping control.""" |
| 1601 |
self.panel.Freeze() |
| 1602 |
show = self.lut3d_hdr_maxmll_ctrl.IsShown() # BT.2390 (roll-off) |
| 1603 |
self.lut3d_hdr_maxmll_alt_clip_cb.Show( |
| 1604 |
show and self.getcfg("3dlut.hdr_maxmll") < 10000 |
| 1605 |
) |
| 1606 |
self.panel.Layout() |
| 1607 |
self.panel.Thaw() |
| 1608 |
|
| 1609 |
def lut3d_show_trc_controls(self, show=True): |
| 1610 |
"""Show or hide the TRC controls based on the Argyll version. |
| 1611 |
|
| 1612 |
Args: |
| 1613 |
show (bool): Whether to show the controls. Defaults to True. |
| 1614 |
""" |
| 1615 |
self.panel.Freeze() |
| 1616 |
show = show and self.worker.argyll_version >= [1, 6] |
| 1617 |
if hasattr(self, "lut3d_trc_apply_ctrl"): |
| 1618 |
self.lut3d_trc_apply_ctrl.Show(show) |
| 1619 |
self.lut3d_trc_ctrl.Show(show) |
| 1620 |
smpte2084 = self.getcfg("3dlut.trc").startswith("smpte2084") |
| 1621 |
hlg = self.getcfg("3dlut.trc") == "hlg" |
| 1622 |
hdr = smpte2084 or hlg |
| 1623 |
show = show and ( |
| 1624 |
self.getcfg("3dlut.trc") == "customgamma" |
| 1625 |
or (isinstance(self, LUT3DFrame) or self.getcfg("show_advanced_options")) |
| 1626 |
) |
| 1627 |
self.lut3d_trc_gamma_label.Show(show and not hdr) |
| 1628 |
self.lut3d_trc_gamma_ctrl.Show(show and not hdr) |
| 1629 |
smpte2084r = self.getcfg("3dlut.trc") == "smpte2084.rolloffclip" |
| 1630 |
# Show items in this order so we end up with the correct controls shown |
| 1631 |
showcc = (smpte2084r or hlg) and ( |
| 1632 |
isinstance(self, LUT3DFrame) or self.getcfg("show_advanced_options") |
| 1633 |
) |
| 1634 |
self.lut3d_content_colorspace_label.ContainingSizer.ShowItems(showcc) |
| 1635 |
sel = self.lut3d_content_colorspace_ctrl.Selection |
| 1636 |
lastsel = self.lut3d_content_colorspace_ctrl.Count - 1 |
| 1637 |
sizer = self.lut3d_content_colorspace_red_x.ContainingSizer |
| 1638 |
sizer.ShowItems(showcc and sel == lastsel) |
| 1639 |
self.lut3d_hdr_minmll_label.Show(show and smpte2084) |
| 1640 |
self.lut3d_hdr_minmll_ctrl.Show(show and smpte2084) |
| 1641 |
self.lut3d_hdr_minmll_ctrl_label.Show(show and smpte2084) |
| 1642 |
self.lut3d_hdr_maxmll_label.Show(show and smpte2084r) |
| 1643 |
self.lut3d_hdr_maxmll_ctrl.Show(show and smpte2084r) |
| 1644 |
self.lut3d_hdr_maxmll_ctrl_label.Show(show and smpte2084r) |
| 1645 |
self.lut3d_show_hdr_maxmll_alt_clip_ctrl() |
| 1646 |
self.lut3d_hdr_diffuse_white_label.Show(show and smpte2084r) |
| 1647 |
self.lut3d_hdr_diffuse_white_txt.Show(show and smpte2084r) |
| 1648 |
self.lut3d_hdr_diffuse_white_txt_label.Show(show and smpte2084r) |
| 1649 |
self.lut3d_hdr_ambient_luminance_label.Show(show and hlg) |
| 1650 |
self.lut3d_hdr_ambient_luminance_ctrl.Show(show and hlg) |
| 1651 |
self.lut3d_hdr_ambient_luminance_ctrl_label.Show(show and hlg) |
| 1652 |
self.lut3d_hdr_system_gamma_label.Show(show and hlg) |
| 1653 |
self.lut3d_hdr_system_gamma_txt.Show(show and hlg) |
| 1654 |
sizer = self.lut3d_hdr_sat_ctrl.ContainingSizer |
| 1655 |
sizer.ShowItems(show and smpte2084r) |
| 1656 |
sizer = self.lut3d_hdr_hue_ctrl.ContainingSizer |
| 1657 |
sizer.ShowItems(show and smpte2084r) |
| 1658 |
show = (show or smpte2084) and not hlg |
| 1659 |
show = show and ( |
| 1660 |
(hasattr(self, "lut3d_create_cb") and self.getcfg("3dlut.create")) |
| 1661 |
or self.XYZbpout > [0, 0, 0] |
| 1662 |
) |
| 1663 |
self.lut3d_trc_gamma_type_ctrl.Show(show and not hdr) |
| 1664 |
self.lut3d_trc_black_output_offset_label.Show(show) |
| 1665 |
self.lut3d_trc_black_output_offset_ctrl.Show(show) |
| 1666 |
self.lut3d_trc_black_output_offset_intctrl.Show(show) |
| 1667 |
self.lut3d_trc_black_output_offset_intctrl_label.Show(show) |
| 1668 |
self.lut3d_hdr_peak_luminance_label.Show(smpte2084) |
| 1669 |
self.lut3d_hdr_peak_luminance_ctrl.Show(smpte2084) |
| 1670 |
self.lut3d_hdr_peak_luminance_ctrl_label.Show(smpte2084) |
| 1671 |
self.lut3d_show_hdr_display_control() |
| 1672 |
self.panel.Layout() |
| 1673 |
self.panel.Thaw() |
| 1674 |
if isinstance(self, LUT3DFrame): |
| 1675 |
self.update_layout() |
| 1676 |
|
| 1677 |
def lut3d_show_encoding_controls(self, show=True): |
| 1678 |
"""Show or hide the encoding controls based on the Argyll version. |
| 1679 |
|
| 1680 |
Args: |
| 1681 |
show (bool): Whether to show the controls. Defaults to True. |
| 1682 |
""" |
| 1683 |
show = show and ( |
| 1684 |
( |
| 1685 |
self.worker.argyll_version >= [1, 7] |
| 1686 |
and self.worker.argyll_version != [1, 7, 0, "_beta"] |
| 1687 |
) |
| 1688 |
or self.worker.argyll_version >= [1, 6] |
| 1689 |
) |
| 1690 |
# Argyll 1.7 beta 3 (2015-04-02) added clip WTW on input TV encoding |
| 1691 |
self.encoding_input_label.Show(show) |
| 1692 |
self.encoding_input_ctrl.Show(show) |
| 1693 |
show = show and self.worker.argyll_version >= [1, 6] |
| 1694 |
self.encoding_output_label.Show(show) |
| 1695 |
self.encoding_output_ctrl.Show(show) |
| 1696 |
|
| 1697 |
def lut3d_update_encoding_controls(self): |
| 1698 |
"""Update the encoding controls based on the selected format.""" |
| 1699 |
self.lut3d_setup_encoding_ctrl() |
| 1700 |
self.encoding_input_ctrl.SetSelection( |
| 1701 |
self.encoding_input_ba[self.getcfg("3dlut.encoding.input")] |
| 1702 |
) |
| 1703 |
self.encoding_input_ctrl.Enable(self.encoding_input_ctrl.Count > 1) |
| 1704 |
self.encoding_output_ctrl.SetSelection( |
| 1705 |
self.encoding_output_ba[self.getcfg("3dlut.encoding.output")] |
| 1706 |
) |
| 1707 |
self.encoding_output_ctrl.Enable( |
| 1708 |
self.getcfg("3dlut.format") not in ("dcl", "madVR") |
| 1709 |
) |
| 1710 |
|
| 1711 |
def lut3d_enable_size_controls(self): |
| 1712 |
"""Enable or disable the 3D LUT size controls based on the selected format.""" |
| 1713 |
self.lut3d_size_ctrl.Enable( |
| 1714 |
self.getcfg("3dlut.format") not in ("eeColor", "madVR") |
| 1715 |
) |
| 1716 |
|
| 1717 |
|
| 1718 |
class LUT3DFrame(BaseFrame, LUT3DMixin): |
| 1719 |
"""3D LUT creation window. |
| 1720 |
|
| 1721 |
Args: |
| 1722 |
parent (wx.Window, optional): The parent window. Defaults to None. |
| 1723 |
setup (bool, optional): Whether to set up the frame immediately. |
| 1724 |
Defaults to True. |
| 1725 |
""" |
| 1726 |
|
| 1727 |
def __init__(self, parent=None, setup=True): |
| 1728 |
self.input_profile = None |
| 1729 |
self.res = TempXmlResource(get_data_path(os.path.join("xrc", "3dlut.xrc"))) |
| 1730 |
self.res.InsertHandler( |
| 1731 |
xh_filebrowsebutton.FileBrowseButtonWithHistoryXmlHandler() |
| 1732 |
) |
| 1733 |
self.res.InsertHandler(xh_floatspin.FloatSpinCtrlXmlHandler()) |
| 1734 |
self.res.InsertHandler(xh_bitmapctrls.BitmapButton()) |
| 1735 |
self.res.InsertHandler(xh_bitmapctrls.StaticBitmap()) |
| 1736 |
if hasattr(wx, "PreFrame"): |
| 1737 |
# Classic |
| 1738 |
pre = wx.PreFrame() |
| 1739 |
self.res.LoadOnFrame(pre, parent, "lut3dframe") |
| 1740 |
self.PostCreate(pre) |
| 1741 |
else: |
| 1742 |
# Phoenix |
| 1743 |
wx.Frame.__init__(self) |
| 1744 |
self.res.LoadFrame(self, parent, "lut3dframe") |
| 1745 |
self.init() |
| 1746 |
self.Bind(wx.EVT_CLOSE, self.OnClose) |
| 1747 |
if sys.platform == "win32": |
| 1748 |
self.Bind(wx.EVT_SIZE, self.OnSize) |
| 1749 |
|
| 1750 |
self.SetIcons( |
| 1751 |
config.get_icon_bundle([256, 48, 32, 16], APPNAME + "-3DLUT-maker") |
| 1752 |
) |
| 1753 |
|
| 1754 |
self.set_child_ctrls_as_attrs(self) |
| 1755 |
|
| 1756 |
self.panel = xrc.XRCCTRL(self, "panel") |
| 1757 |
|
| 1758 |
if setup: |
| 1759 |
self.setup() |
| 1760 |
|
| 1761 |
def OnSize(self, event): |
| 1762 |
"""Handle the size event for the 3D LUT frame. |
| 1763 |
|
| 1764 |
Args: |
| 1765 |
event (wx.Event): The event that triggered this handler. |
| 1766 |
""" |
| 1767 |
event.Skip() |
| 1768 |
self.Refresh() # Prevents distorted drawing under Windows |
| 1769 |
|
| 1770 |
def setup(self): |
| 1771 |
"""Set up the 3D LUT frame.""" |
| 1772 |
self.worker = worker.Worker(self) |
| 1773 |
self.worker.set_argyll_version("collink") |
| 1774 |
|
| 1775 |
for which in ("input", "abstract", "output"): |
| 1776 |
ctrl = xrc.XRCCTRL(self, f"{which}_profile_ctrl") |
| 1777 |
setattr(self, f"{which}_profile_ctrl", ctrl) |
| 1778 |
ctrl.changeCallback = getattr(self, f"{which}_profile_ctrl_handler") |
| 1779 |
if which not in ("abstract", "output"): |
| 1780 |
ctrl.SetHistory(get_data_path("ref", r"\.(icc|icm)$")) |
| 1781 |
ctrl.SetMaxFontSize(11) |
| 1782 |
# Drop targets |
| 1783 |
droptarget = FileDrop( |
| 1784 |
self, |
| 1785 |
{ |
| 1786 |
".icc": getattr(self, f"{which}_drop_handler"), |
| 1787 |
".icm": getattr(self, f"{which}_drop_handler"), |
| 1788 |
}, |
| 1789 |
) |
| 1790 |
ctrl.SetDropTarget(droptarget) |
| 1791 |
|
| 1792 |
# Bind event handlers |
| 1793 |
self.abstract_profile_cb.Bind( |
| 1794 |
wx.EVT_CHECKBOX, self.use_abstract_profile_ctrl_handler |
| 1795 |
) |
| 1796 |
self.output_profile_current_btn.Bind( |
| 1797 |
wx.EVT_BUTTON, self.output_profile_current_ctrl_handler |
| 1798 |
) |
| 1799 |
self.lut3d_trc_apply_none_ctrl.Bind( |
| 1800 |
wx.EVT_RADIOBUTTON, self.lut3d_trc_apply_ctrl_handler |
| 1801 |
) |
| 1802 |
self.lut3d_trc_apply_black_offset_ctrl.Bind( |
| 1803 |
wx.EVT_RADIOBUTTON, self.lut3d_trc_apply_ctrl_handler |
| 1804 |
) |
| 1805 |
self.lut3d_trc_apply_ctrl.Bind( |
| 1806 |
wx.EVT_RADIOBUTTON, self.lut3d_trc_apply_ctrl_handler |
| 1807 |
) |
| 1808 |
self.lut3d_bind_event_handlers() |
| 1809 |
|
| 1810 |
self.lut3d_create_btn.SetDefault() |
| 1811 |
|
| 1812 |
self.setup_language() |
| 1813 |
self.XYZbpin = [0, 0, 0] |
| 1814 |
# XYZbpout will be set to the blackpoint of the selected profile. This |
| 1815 |
# is used to determine if lack output offset controls should be shown. |
| 1816 |
# Set a initial value slightly above zero so output offset controls are |
| 1817 |
# shown if the selected profile doesn't exist. |
| 1818 |
self.XYZbpout = [0.001, 0.001, 0.001] |
| 1819 |
self.update_controls() |
| 1820 |
self.update_layout() |
| 1821 |
scrollrate_x = 2 if self.panel.VirtualSize[0] > self.panel.Size[0] else 0 |
| 1822 |
self.panel.SetScrollRate(scrollrate_x, 2) |
| 1823 |
|
| 1824 |
config.DEFAULTS.update( |
| 1825 |
{ |
| 1826 |
"position.lut3dframe.x": self.GetDisplay().ClientArea[0] + 40, |
| 1827 |
"position.lut3dframe.y": self.GetDisplay().ClientArea[1] + 60, |
| 1828 |
"size.lut3dframe.w": self.ClientSize[0], |
| 1829 |
"size.lut3dframe.h": self.ClientSize[1], |
| 1830 |
} |
| 1831 |
) |
| 1832 |
|
| 1833 |
if ( |
| 1834 |
self.hascfg("position.lut3dframe.x") |
| 1835 |
and self.hascfg("position.lut3dframe.y") |
| 1836 |
and self.hascfg("size.lut3dframe.w") |
| 1837 |
and self.hascfg("size.lut3dframe.h") |
| 1838 |
): |
| 1839 |
self.SetSaneGeometry( |
| 1840 |
int(self.getcfg("position.lut3dframe.x")), |
| 1841 |
int(self.getcfg("position.lut3dframe.y")), |
| 1842 |
int(self.getcfg("size.lut3dframe.w")), |
| 1843 |
int(self.getcfg("size.lut3dframe.h")), |
| 1844 |
) |
| 1845 |
else: |
| 1846 |
self.Center() |
| 1847 |
|
| 1848 |
def OnClose(self, event=None): |
| 1849 |
"""Handle the close event for the 3D LUT frame. |
| 1850 |
|
| 1851 |
Args: |
| 1852 |
event (wx.Event, optional): The event that triggered this handler. |
| 1853 |
""" |
| 1854 |
if getattr(self.worker, "thread", None) and self.worker.thread.is_alive(): |
| 1855 |
self.worker.abort_subprocess(True) |
| 1856 |
return |
| 1857 |
if sys.platform == "darwin" or DEBUG: |
| 1858 |
self.focus_handler(event) |
| 1859 |
if self.IsShownOnScreen() and not self.IsMaximized() and not self.IsIconized(): |
| 1860 |
x, y = self.GetScreenPosition() |
| 1861 |
self.setcfg("position.lut3dframe.x", x) |
| 1862 |
self.setcfg("position.lut3dframe.y", y) |
| 1863 |
self.setcfg("size.lut3dframe.w", self.ClientSize[0]) |
| 1864 |
self.setcfg("size.lut3dframe.h", self.ClientSize[1]) |
| 1865 |
if self.Parent: |
| 1866 |
config.writecfg() |
| 1867 |
else: |
| 1868 |
config.writecfg( |
| 1869 |
module="3DLUT-maker", |
| 1870 |
options=( |
| 1871 |
"3dlut.", |
| 1872 |
"last_3dlut_path", |
| 1873 |
"position.lut3dframe", |
| 1874 |
"size.lut3dframe", |
| 1875 |
), |
| 1876 |
) |
| 1877 |
if event: |
| 1878 |
# Hide first (looks nicer) |
| 1879 |
self.Hide() |
| 1880 |
# Need to use CallAfter to prevent hang under Windows if minimized |
| 1881 |
wx.CallAfter(self.Destroy) |
| 1882 |
|
| 1883 |
def use_abstract_profile_ctrl_handler(self, event): |
| 1884 |
"""Handle the abstract profile checkbox. |
| 1885 |
|
| 1886 |
Args: |
| 1887 |
event (wx.Event): The event that triggered this handler. |
| 1888 |
""" |
| 1889 |
self.setcfg( |
| 1890 |
"3dlut.use_abstract_profile", int(self.abstract_profile_cb.GetValue()) |
| 1891 |
) |
| 1892 |
enable = bool(self.getcfg("3dlut.use_abstract_profile")) |
| 1893 |
self.abstract_profile_ctrl.Enable(enable) |
| 1894 |
|
| 1895 |
def abstract_drop_handler(self, path): |
| 1896 |
"""Set abstract profile to selected profile path. |
| 1897 |
|
| 1898 |
Args: |
| 1899 |
path (str): Path to the abstract profile. |
| 1900 |
""" |
| 1901 |
if not self.worker.is_working(): |
| 1902 |
self.abstract_profile_ctrl.SetPath(path) |
| 1903 |
self.set_profile("abstract") |
| 1904 |
|
| 1905 |
def input_drop_handler(self, path): |
| 1906 |
"""Set input profile to selected profile path. |
| 1907 |
|
| 1908 |
Args: |
| 1909 |
path (str): Path to the input profile. |
| 1910 |
""" |
| 1911 |
if not self.worker.is_working(): |
| 1912 |
self.input_profile_ctrl.SetPath(path) |
| 1913 |
self.set_profile("input") |
| 1914 |
|
| 1915 |
def output_drop_handler(self, path): |
| 1916 |
"""Set output profile to selected profile path. |
| 1917 |
|
| 1918 |
Args: |
| 1919 |
path (str): Path to the output profile. |
| 1920 |
""" |
| 1921 |
if not self.worker.is_working(): |
| 1922 |
self.output_profile_ctrl.SetPath(path) |
| 1923 |
self.set_profile("output") |
| 1924 |
|
| 1925 |
def abstract_profile_ctrl_handler(self, event): |
| 1926 |
"""Set abstract profile to selected profile path. |
| 1927 |
|
| 1928 |
Args: |
| 1929 |
event (wx.Event): The event that triggered this handler, if any. |
| 1930 |
""" |
| 1931 |
self.set_profile("abstract", silent=not event) |
| 1932 |
|
| 1933 |
def input_profile_ctrl_handler(self, event): |
| 1934 |
"""Set input profile to selected profile path. |
| 1935 |
|
| 1936 |
Args: |
| 1937 |
event (wx.Event): The event that triggered this handler, if any. |
| 1938 |
""" |
| 1939 |
self.set_profile("input", silent=not event) |
| 1940 |
if self.Parent: |
| 1941 |
self.Parent.lut3d_init_input_profiles() |
| 1942 |
self.Parent.lut3d_update_controls() |
| 1943 |
|
| 1944 |
def output_profile_ctrl_handler(self, event): |
| 1945 |
"""Set output profile to selected profile path. |
| 1946 |
|
| 1947 |
Args: |
| 1948 |
event (wx.Event): The event that triggered this handler, if any. |
| 1949 |
""" |
| 1950 |
self.set_profile("output", silent=not event) |
| 1951 |
|
| 1952 |
def output_profile_current_ctrl_handler(self, event): |
| 1953 |
"""Set output profile to current profile path. |
| 1954 |
|
| 1955 |
Args: |
| 1956 |
event (wx.Event): The event that triggered this handler, if any. |
| 1957 |
""" |
| 1958 |
profile_path = get_current_profile_path(True, True) |
| 1959 |
if profile_path and os.path.isfile(profile_path): |
| 1960 |
self.output_profile_ctrl.SetPath(profile_path) |
| 1961 |
self.set_profile("output", profile_path or False, silent=not event) |
| 1962 |
|
| 1963 |
def get_commands(self): |
| 1964 |
"""Get commands for the command line or IPC. |
| 1965 |
|
| 1966 |
Returns: |
| 1967 |
list: List of commands that can be used to control the 3D LUT |
| 1968 |
maker. |
| 1969 |
""" |
| 1970 |
return [*self.get_common_commands(), "3DLUT-maker [create <filename>]"] |
| 1971 |
|
| 1972 |
def process_data(self, data): |
| 1973 |
"""Process data from command line or IPC. |
| 1974 |
|
| 1975 |
Args: |
| 1976 |
data (list): List of strings, where the first element is the command |
| 1977 |
and the second element is the action or additional data. |
| 1978 |
|
| 1979 |
Returns: |
| 1980 |
str: "ok" if the command was processed successfully, "invalid" if |
| 1981 |
the command was not recognized |
| 1982 |
""" |
| 1983 |
if data[0] == "3DLUT-maker" and ( |
| 1984 |
len(data) == 1 or (len(data) == 3 and data[1] == "create") |
| 1985 |
): |
| 1986 |
if self.IsIconized(): |
| 1987 |
self.Restore() |
| 1988 |
self.Raise() |
| 1989 |
if len(data) == 3: |
| 1990 |
wx.CallAfter( |
| 1991 |
self.lut3d_create_handler, |
| 1992 |
CustomEvent(wx.EVT_BUTTON.evtType[0], self.lut3d_create_btn), |
| 1993 |
path=data[2], |
| 1994 |
) |
| 1995 |
return "ok" |
| 1996 |
return "invalid" |
| 1997 |
|
| 1998 |
def set_profile(self, which, profile_path=None, silent=False): |
| 1999 |
"""Set profile for input, abstract or output profile controls. |
| 2000 |
|
| 2001 |
Args: |
| 2002 |
which (str): Which profile to set, one of "input", "abstract" or "output". |
| 2003 |
profile_path (str, optional): Path to the profile to set. If None, the |
| 2004 |
current profile path is used for the output profile. |
| 2005 |
silent (bool): If True, do not show any dialogs or messages. |
| 2006 |
|
| 2007 |
Returns: |
| 2008 |
None | ICCProfile: None if the profile was set successfully, or if |
| 2009 |
the profile is a device link profile that was set as input |
| 2010 |
profile. Otherwise, returns None if the profile is invalid or |
| 2011 |
missing, or if an error occurred. |
| 2012 |
""" |
| 2013 |
path = getattr(self, f"{which}_profile_ctrl").GetPath() |
| 2014 |
if which == "output": |
| 2015 |
if profile_path is None: |
| 2016 |
profile_path = get_current_profile_path(True, True) |
| 2017 |
self.output_profile_current_btn.Enable( |
| 2018 |
self.output_profile_ctrl.IsShown() |
| 2019 |
and bool(profile_path) |
| 2020 |
and os.path.isfile(profile_path) |
| 2021 |
and profile_path != path |
| 2022 |
) |
| 2023 |
if path: |
| 2024 |
if not os.path.isfile(path): |
| 2025 |
if not silent: |
| 2026 |
show_result_dialog( |
| 2027 |
Error(lang.getstr("file.missing", path)), parent=self |
| 2028 |
) |
| 2029 |
return None |
| 2030 |
try: |
| 2031 |
profile = ICCProfile(path) |
| 2032 |
except ICCProfileInvalidError: |
| 2033 |
if not silent: |
| 2034 |
show_result_dialog( |
| 2035 |
Error(f"{lang.getstr('profile.invalid')}\n{path}"), parent=self |
| 2036 |
) |
| 2037 |
except OSError as exception: |
| 2038 |
if not silent: |
| 2039 |
show_result_dialog(exception, parent=self) |
| 2040 |
else: |
| 2041 |
if ( |
| 2042 |
which in ("input", "output") |
| 2043 |
and ( |
| 2044 |
profile.profileClass not in (b"mntr", b"link", b"scnr", b"spac") |
| 2045 |
or profile.colorSpace != b"RGB" |
| 2046 |
) |
| 2047 |
) or ( |
| 2048 |
which == "abstract" |
| 2049 |
and ( |
| 2050 |
profile.profileClass != b"abst" |
| 2051 |
or profile.colorSpace not in (b"Lab", b"XYZ") |
| 2052 |
) |
| 2053 |
): |
| 2054 |
show_result_dialog( |
| 2055 |
Error( |
| 2056 |
lang.getstr( |
| 2057 |
"profile.unsupported", |
| 2058 |
( |
| 2059 |
profile.profileClass.decode("utf-8"), |
| 2060 |
profile.colorSpace.decode("utf-8"), |
| 2061 |
), |
| 2062 |
) |
| 2063 |
), |
| 2064 |
parent=self, |
| 2065 |
) |
| 2066 |
else: |
| 2067 |
if profile.profileClass == b"link": |
| 2068 |
if which == "output": |
| 2069 |
self.input_profile_ctrl.SetPath(path) |
| 2070 |
if self.getcfg("3dlut.output.profile") == path: |
| 2071 |
# The original file was probably overwritten |
| 2072 |
# by the device link. Reset. |
| 2073 |
self.setcfg("3dlut.output.profile", None) |
| 2074 |
self.output_profile_ctrl.SetPath( |
| 2075 |
self.getcfg("3dlut.output.profile") |
| 2076 |
) |
| 2077 |
self.set_profile("input", silent=silent) |
| 2078 |
return None |
| 2079 |
self.Freeze() |
| 2080 |
self.abstract_profile_cb.SetValue(False) |
| 2081 |
self.abstract_profile_cb.Hide() |
| 2082 |
self.abstract_profile_ctrl.Hide() |
| 2083 |
self.output_profile_label.Hide() |
| 2084 |
self.output_profile_ctrl.Hide() |
| 2085 |
self.output_profile_current_btn.Hide() |
| 2086 |
self.lut3d_apply_cal_cb.Hide() |
| 2087 |
self.lut3d_trc_label.Hide() |
| 2088 |
self.lut3d_trc_apply_none_ctrl.Hide() |
| 2089 |
self.lut3d_input_value_clipping_bmp.Hide() |
| 2090 |
self.lut3d_input_value_clipping_label.Hide() |
| 2091 |
self.lut3d_trc_apply_black_offset_ctrl.Hide() |
| 2092 |
self.gamut_mapping_mode.Hide() |
| 2093 |
self.gamut_mapping_inverse_a2b.Hide() |
| 2094 |
self.gamut_mapping_b2a.Hide() |
| 2095 |
self.lut3d_show_encoding_controls(False) |
| 2096 |
self.lut3d_show_trc_controls(False) |
| 2097 |
self.lut3d_rendering_intent_label.Hide() |
| 2098 |
self.lut3d_rendering_intent_ctrl.Hide() |
| 2099 |
self.panel.GetSizer().Layout() |
| 2100 |
self.update_layout() |
| 2101 |
self.Thaw() |
| 2102 |
else: |
| 2103 |
if which == "input": |
| 2104 |
if ( |
| 2105 |
not hasattr(self, f"{which}_profile") |
| 2106 |
or self.getcfg(f"3dlut.{which}.profile") |
| 2107 |
!= profile.filename |
| 2108 |
): |
| 2109 |
# Get profile blackpoint so we can check if it makes |
| 2110 |
# sense to show TRC type and output offset controls |
| 2111 |
try: |
| 2112 |
odata = self.worker.xicclu( |
| 2113 |
profile, (0, 0, 0), pcs="x" |
| 2114 |
) |
| 2115 |
except Exception as exception: |
| 2116 |
show_result_dialog(exception, self) |
| 2117 |
self.set_profile_ctrl_path(which) |
| 2118 |
return None |
| 2119 |
else: |
| 2120 |
if len(odata) != 1 or len(odata[0]) != 3: |
| 2121 |
show_result_dialog( |
| 2122 |
f"Blackpoint is invalid: {odata}", self |
| 2123 |
) |
| 2124 |
self.set_profile_ctrl_path(which) |
| 2125 |
return None |
| 2126 |
self.XYZbpin = odata[0] |
| 2127 |
self.Freeze() |
| 2128 |
self.lut3d_trc_label.Show() |
| 2129 |
self.lut3d_trc_apply_none_ctrl.Show() |
| 2130 |
self.lut3d_trc_apply_black_offset_ctrl.Show() |
| 2131 |
self.gamut_mapping_mode.Show() |
| 2132 |
self.gamut_mapping_inverse_a2b.Show() |
| 2133 |
self.gamut_mapping_b2a.Show() |
| 2134 |
enable = bool(self.getcfg("3dlut.use_abstract_profile")) |
| 2135 |
self.abstract_profile_cb.SetValue(enable) |
| 2136 |
self.abstract_profile_cb.Show() |
| 2137 |
self.abstract_profile_ctrl.Enable(enable) |
| 2138 |
self.abstract_profile_ctrl.Show() |
| 2139 |
self.output_profile_label.Show() |
| 2140 |
self.output_profile_ctrl.Show() |
| 2141 |
self.output_profile_current_btn.Show() |
| 2142 |
self.lut3d_apply_cal_cb.Show() |
| 2143 |
self.lut3d_show_encoding_controls() |
| 2144 |
self.lut3d_update_encoding_controls() |
| 2145 |
# Update controls related to output profile |
| 2146 |
self.input_profile = profile |
| 2147 |
if not self.set_profile("output", silent=silent): |
| 2148 |
self.update_linking_controls() |
| 2149 |
self.lut3d_trc_apply_ctrl_handler() |
| 2150 |
elif which == "output": |
| 2151 |
if ( |
| 2152 |
not hasattr(self, f"{which}_profile") |
| 2153 |
or self.getcfg(f"3dlut.{which}.profile") |
| 2154 |
!= profile.filename |
| 2155 |
): |
| 2156 |
# Get profile blackpoint so we can check if input |
| 2157 |
# values would be clipped |
| 2158 |
try: |
| 2159 |
odata = self.worker.xicclu( |
| 2160 |
profile, (0, 0, 0), pcs="x" |
| 2161 |
) |
| 2162 |
except Exception as exception: |
| 2163 |
show_result_dialog(exception, self) |
| 2164 |
self.set_profile_ctrl_path(which) |
| 2165 |
return None |
| 2166 |
else: |
| 2167 |
if len(odata) != 1 or len(odata[0]) != 3: |
| 2168 |
show_result_dialog( |
| 2169 |
f"Blackpoint is invalid: {odata}", self |
| 2170 |
) |
| 2171 |
self.set_profile_ctrl_path(which) |
| 2172 |
return None |
| 2173 |
if odata[0][1]: |
| 2174 |
# Got above zero blackpoint from lookup |
| 2175 |
self.XYZbpout = odata[0] |
| 2176 |
else: |
| 2177 |
# Got zero blackpoint from lookup. |
| 2178 |
# Try chardata instead. |
| 2179 |
XYZbp = profile.get_chardata_bkpt() |
| 2180 |
if XYZbp: |
| 2181 |
self.XYZbpout = XYZbp |
| 2182 |
else: |
| 2183 |
self.XYZbpout = [0, 0, 0] |
| 2184 |
self.Freeze() |
| 2185 |
enable_apply_cal = isinstance( |
| 2186 |
profile.tags.get("vcgt"), VideoCardGammaType |
| 2187 |
) |
| 2188 |
self.lut3d_apply_cal_cb.SetValue( |
| 2189 |
enable_apply_cal |
| 2190 |
and bool(self.getcfg("3dlut.output.profile.apply_cal")) |
| 2191 |
) |
| 2192 |
self.lut3d_apply_cal_cb.Enable(enable_apply_cal) |
| 2193 |
self.gamut_mapping_inverse_a2b.Enable() |
| 2194 |
allow_b2a_gamap = ( |
| 2195 |
"B2A0" in profile.tags |
| 2196 |
and isinstance(profile.tags.B2A0, LUT16Type) |
| 2197 |
and profile.tags.B2A0.clut_grid_steps >= 17 |
| 2198 |
) |
| 2199 |
# Allow using B2A instead of inverse A2B? |
| 2200 |
self.gamut_mapping_b2a.Enable(allow_b2a_gamap) |
| 2201 |
if not allow_b2a_gamap: |
| 2202 |
self.setcfg("3dlut.gamap.use_b2a", 0) |
| 2203 |
self.update_linking_controls() |
| 2204 |
self.lut3d_trc_apply_ctrl_handler() |
| 2205 |
self.lut3d_rendering_intent_label.Show() |
| 2206 |
self.lut3d_rendering_intent_ctrl.Show() |
| 2207 |
self.panel.GetSizer().Layout() |
| 2208 |
self.update_layout() |
| 2209 |
if which != "abstract": |
| 2210 |
self.Thaw() |
| 2211 |
setattr(self, f"{which}_profile", profile) |
| 2212 |
if which == "output" and not self.output_profile_ctrl.IsShown(): |
| 2213 |
return None |
| 2214 |
self.setcfg(f"3dlut.{which}.profile", profile.filename) |
| 2215 |
self.lut3d_create_btn.Enable( |
| 2216 |
bool(self.getcfg("3dlut.input.profile")) |
| 2217 |
and os.path.isfile(self.getcfg("3dlut.input.profile")) |
| 2218 |
and ( |
| 2219 |
( |
| 2220 |
bool(self.getcfg("3dlut.output.profile")) |
| 2221 |
and os.path.isfile(self.getcfg("3dlut.output.profile")) |
| 2222 |
) |
| 2223 |
or profile.profileClass == b"link" |
| 2224 |
) |
| 2225 |
and ( |
| 2226 |
self.getcfg("3dlut.format") != "madVR" |
| 2227 |
or self.output_profile_ctrl.IsShown() |
| 2228 |
) |
| 2229 |
) |
| 2230 |
return profile |
| 2231 |
self.set_profile_ctrl_path(which) |
| 2232 |
elif which == "input": |
| 2233 |
self.set_profile_ctrl_path(which) |
| 2234 |
self.lut3d_update_encoding_controls() |
| 2235 |
elif not silent: |
| 2236 |
setattr(self, f"{which}_profile", None) |
| 2237 |
self.setcfg(f"3dlut.{which}.profile", None) |
| 2238 |
if which == "output": |
| 2239 |
self.lut3d_apply_cal_cb.Disable() |
| 2240 |
self.lut3d_create_btn.Disable() |
| 2241 |
return None |
| 2242 |
|
| 2243 |
def set_profile_ctrl_path(self, which): |
| 2244 |
"""Set the profile path for the specified profile control.""" |
| 2245 |
getattr(self, f"{which}_profile_ctrl").SetPath( |
| 2246 |
self.getcfg(f"3dlut.{which}.profile") |
| 2247 |
) |
| 2248 |
|
| 2249 |
def setup_language(self): |
| 2250 |
"""Setup language for the 3D LUT frame.""" |
| 2251 |
BaseFrame.setup_language(self) |
| 2252 |
|
| 2253 |
for which in ("input", "abstract", "output"): |
| 2254 |
msg = { |
| 2255 |
"input": lang.getstr("3dlut.input.profile"), |
| 2256 |
"abstract": lang.getstr("3dlut.use_abstract_profile"), |
| 2257 |
"output": lang.getstr("output.profile"), |
| 2258 |
}[which] |
| 2259 |
kwargs = { |
| 2260 |
"toolTip": msg.rstrip(":"), |
| 2261 |
"dialogTitle": msg, |
| 2262 |
"fileMask": lang.getstr("filetype.icc") + "|*.icc;*.icm", |
| 2263 |
} |
| 2264 |
ctrl = getattr(self, f"{which}_profile_ctrl") |
| 2265 |
for name in kwargs: |
| 2266 |
value = kwargs[name] |
| 2267 |
setattr(ctrl, name, value) |
| 2268 |
|
| 2269 |
self.lut3d_setup_language() |
| 2270 |
|
| 2271 |
def update_controls(self): |
| 2272 |
"""Update controls with values from the configuration.""" |
| 2273 |
self.panel.Freeze() |
| 2274 |
self.lut3d_create_btn.Disable() |
| 2275 |
self.input_profile_ctrl.SetPath(self.getcfg("3dlut.input.profile")) |
| 2276 |
self.output_profile_ctrl.SetPath(self.getcfg("3dlut.output.profile")) |
| 2277 |
self.input_profile_ctrl_handler(None) |
| 2278 |
enable = bool(self.getcfg("3dlut.use_abstract_profile")) |
| 2279 |
self.abstract_profile_cb.SetValue(enable) |
| 2280 |
self.abstract_profile_ctrl.SetPath(self.getcfg("3dlut.abstract.profile")) |
| 2281 |
self.abstract_profile_ctrl_handler(None) |
| 2282 |
self.output_profile_ctrl_handler(None) |
| 2283 |
self.lut3d_update_shared_controls() |
| 2284 |
self.panel.Thaw() |
| 2285 |
|
| 2286 |
def update_linking_controls(self): |
| 2287 |
"""Update controls related to linking input and output profiles.""" |
| 2288 |
self.gamut_mapping_inverse_a2b.SetValue(not self.getcfg("3dlut.gamap.use_b2a")) |
| 2289 |
self.gamut_mapping_b2a.SetValue(bool(self.getcfg("3dlut.gamap.use_b2a"))) |
| 2290 |
if ( |
| 2291 |
hasattr(self, "input_profile") |
| 2292 |
and "rTRC" in self.input_profile.tags |
| 2293 |
and "gTRC" in self.input_profile.tags |
| 2294 |
and "bTRC" in self.input_profile.tags |
| 2295 |
and self.input_profile.tags.rTRC |
| 2296 |
== self.input_profile.tags.gTRC |
| 2297 |
== self.input_profile.tags.bTRC |
| 2298 |
and isinstance(self.input_profile.tags.rTRC, CurveType) |
| 2299 |
): |
| 2300 |
tf = self.input_profile.tags.rTRC.get_transfer_function(outoffset=1.0) |
| 2301 |
if self.getcfg("3dlut.input.profile") != self.input_profile.filename: |
| 2302 |
# Use BT.1886 gamma mapping for SMPTE 240M / |
| 2303 |
# Rec. 709 TRC |
| 2304 |
self.setcfg( |
| 2305 |
"3dlut.apply_trc", |
| 2306 |
int( |
| 2307 |
tf[0][1] in (-240, -709) |
| 2308 |
or (tf[0][0].startswith("Gamma") and tf[1] >= 0.95) |
| 2309 |
), |
| 2310 |
) |
| 2311 |
# Use only BT.1886 black output offset |
| 2312 |
self.setcfg( |
| 2313 |
"3dlut.apply_black_offset", |
| 2314 |
int( |
| 2315 |
tf[0][1] not in (-240, -709) |
| 2316 |
and (not tf[0][0].startswith("Gamma") or tf[1] < 0.95) |
| 2317 |
and self.XYZbpin != self.XYZbpout |
| 2318 |
), |
| 2319 |
) |
| 2320 |
# Set gamma to profile gamma if single gamma |
| 2321 |
# profile |
| 2322 |
if tf[0][0].startswith("Gamma") and tf[1] >= 0.95: |
| 2323 |
if not self.getcfg("3dlut.trc_gamma.backup", False): |
| 2324 |
# Backup current gamma |
| 2325 |
self.setcfg( |
| 2326 |
"3dlut.trc_gamma.backup", self.getcfg("3dlut.trc_gamma") |
| 2327 |
) |
| 2328 |
self.setcfg("3dlut.trc_gamma", round(tf[0][1], 2)) |
| 2329 |
# Restore previous gamma if not single gamma |
| 2330 |
# profile |
| 2331 |
elif self.getcfg("3dlut.trc_gamma.backup", False): |
| 2332 |
self.setcfg( |
| 2333 |
"3dlut.trc_gamma", self.getcfg("3dlut.trc_gamma.backup") |
| 2334 |
) |
| 2335 |
self.setcfg("3dlut.trc_gamma.backup", None) |
| 2336 |
self.lut3d_trc_apply_black_offset_ctrl.Enable( |
| 2337 |
tf[0][1] not in (-240, -709) and self.XYZbpin != self.XYZbpout |
| 2338 |
) |
| 2339 |
self.lut3d_update_trc_controls() |
| 2340 |
elif ( |
| 2341 |
hasattr(self, "input_profile") |
| 2342 |
and isinstance(self.input_profile.tags.get("A2B0"), LUT16Type) |
| 2343 |
and isinstance(self.input_profile.tags.get("A2B1", LUT16Type()), LUT16Type) |
| 2344 |
and isinstance(self.input_profile.tags.get("A2B2", LUT16Type()), LUT16Type) |
| 2345 |
): |
| 2346 |
self.lut3d_trc_apply_black_offset_ctrl.Enable(self.XYZbpin != self.XYZbpout) |
| 2347 |
if self.XYZbpin == self.XYZbpout: |
| 2348 |
self.setcfg("3dlut.apply_black_offset", 0) |
| 2349 |
else: |
| 2350 |
self.lut3d_trc_apply_black_offset_ctrl.Disable() |
| 2351 |
self.setcfg("3dlut.apply_black_offset", 0) |
| 2352 |
if self.getcfg("3dlut.apply_black_offset"): |
| 2353 |
self.lut3d_trc_apply_black_offset_ctrl.SetValue(True) |
| 2354 |
elif self.getcfg("3dlut.apply_trc"): |
| 2355 |
self.lut3d_trc_apply_ctrl.SetValue(True) |
| 2356 |
else: |
| 2357 |
self.lut3d_trc_apply_none_ctrl.SetValue(True) |
| 2358 |
self.lut3d_show_trc_controls() |
| 2359 |
|
| 2360 |
|
| 2361 |
def main(): |
| 2362 |
"""Main function to run the 3D LUT maker application.""" |
| 2363 |
config.initcfg("3DLUT-maker") |
| 2364 |
lang.init() |
| 2365 |
lang.update_defaults() |
| 2366 |
app = BaseApp(0) |
| 2367 |
app.TopWindow = LUT3DFrame(setup=False) |
| 2368 |
if sys.platform == "darwin": |
| 2369 |
app.TopWindow.init_menubar() |
| 2370 |
wx.CallLater(1, _main, app) |
| 2371 |
app.MainLoop() |
| 2372 |
|
| 2373 |
|
| 2374 |
def _main(app): |
| 2375 |
app.TopWindow.listen() |
| 2376 |
app.TopWindow.setup() |
| 2377 |
app.TopWindow.Show() |
| 2378 |
|
| 2379 |
|
| 2380 |
if __name__ == "__main__": |
| 2381 |
main() |