Skip to content

Commit f6a1d1c

Browse files
committed
Made the UltraHDR generation more robust.
- Appropriate color profiles are used when generating UltraHDR files. DT_COLORSPACE_DISPLAY_P3 is used for SDR, and DT_COLORSPACE_PQ_P3 is used for HDR, regardless of what was selected in the export module UI. - Added correction for odd image dimensions in SDR + HDR mode. - Added quality setting for JPEG compression - SDR + HDR option can source from any DT image (JPEG-XL is generated on the fly). - generation can abort on errors and display error messages.
1 parent 118bbad commit f6a1d1c

File tree

1 file changed

+142
-48
lines changed

1 file changed

+142
-48
lines changed

contrib/ultrahdr.lua

Lines changed: 142 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ local GUI = {
7575
metadata_label = {},
7676
metadata_box = {},
7777
edit_executables_button = {},
78-
executable_path_widget = {}
78+
executable_path_widget = {},
79+
quality_widget = {}
7980
},
8081
options = {},
8182
run = {}
@@ -101,6 +102,10 @@ local ENCODING_VARIANT_SDR_AUTO_GAINMAP = 3
101102
local SELECTION_TYPE_ONE_STACK = 1
102103
local SELECTION_TYPE_GROUP_BY_FNAME = 2
103104

105+
-- Values are defined in darktable/src/common/colorspaces.h
106+
local DT_COLORSPACE_PQ_P3 = 24
107+
local DT_COLORSPACE_DISPLAY_P3 = 26
108+
104109
local function generate_metadata_file(settings)
105110
local metadata_file_fmt = [[--maxContentBoost %f
106111
--minContentBoost %f
@@ -138,6 +143,7 @@ local function save_preferences()
138143
dt.preferences.write(namespace, "hdr_capacity_min", "float", GUI.optionwidgets.hdr_capacity_min.value)
139144
dt.preferences.write(namespace, "hdr_capacity_max", "float", GUI.optionwidgets.hdr_capacity_max.value)
140145
end
146+
dt.preferences.write(namespace, "quality", "integer", GUI.optionwidgets.quality_widget.value)
141147
end
142148

143149
local function default_to(value, default)
@@ -169,6 +175,30 @@ local function load_preferences()
169175
1.0)
170176
GUI.optionwidgets.hdr_capacity_max.value = default_to(dt.preferences.read(namespace, "hdr_capacity_max", "float"),
171177
6.0)
178+
GUI.optionwidgets.quality_widget.value = default_to(dt.preferences.read(namespace, "quality", "integer"), 95)
179+
end
180+
181+
-- Changes the combobox selection blindly until a paired config value is set.
182+
-- Workaround for https://github.com/darktable-org/lua-scripts/issues/522
183+
local function set_combobox(path, instance, config_name, new_config_value)
184+
185+
local pref = dt.preferences.read("darktable", config_name, "integer")
186+
if pref == new_config_value then
187+
return new_config_value
188+
end
189+
190+
dt.gui.action(path, 0, "selection", "first", 1.0)
191+
local limit, i = 30, 0 -- in case there is no matching config value in the first n entries of a combobox.
192+
while i < limit do
193+
i = i + 1
194+
dt.gui.action(path, 0, "selection", "next", 1.0)
195+
dt.control.sleep(10)
196+
if dt.preferences.read("darktable", config_name, "integer") == new_config_value then
197+
log.msg(log.debug, string.format(_("Changed %s from %d to %d"), config_name, pref, new_config_value))
198+
return pref
199+
end
200+
end
201+
log.msg(log.error, string.format(_("Could not change %s from %d to %d"), config_name, pref, new_config_value))
172202
end
173203

174204
local function assert_settings_correct(encoding_variant)
@@ -189,6 +219,7 @@ local function assert_settings_correct(encoding_variant)
189219
hdr_capacity_min = GUI.optionwidgets.hdr_capacity_min.value,
190220
hdr_capacity_max = GUI.optionwidgets.hdr_capacity_max.value
191221
},
222+
quality = GUI.optionwidgets.quality_widget.value,
192223
tmpdir = dt.configuration.tmp_dir
193224
}
194225

@@ -226,11 +257,10 @@ end
226257

227258
local function get_stacks(images, encoding_variant, selection_type)
228259
local stacks = {}
229-
local extra_image_content_type, extra_image_extension
260+
local extra_image_content_type
230261
if encoding_variant == ENCODING_VARIANT_SDR_AND_GAINMAP then
231262
extra_image_content_type = "gainmap"
232263
elseif encoding_variant == ENCODING_VARIANT_SDR_AND_HDR then
233-
extra_image_extension = "jxl"
234264
extra_image_content_type = "hdr"
235265
elseif encoding_variant == ENCODING_VARIANT_SDR_AUTO_GAINMAP then
236266
extra_image_content_type = nil
@@ -278,9 +308,6 @@ local function get_stacks(images, encoding_variant, selection_type)
278308
if extra_image_content_type then
279309
if not v["sdr"] or not v[extra_image_content_type] then
280310
stacks[k] = nil
281-
elseif extra_image_extension and df.get_filetype(v[extra_image_content_type].filename) ~=
282-
extra_image_extension then
283-
stacks[k] = nil
284311
else
285312
local sdr_w, sdr_h = get_dimensions(v["sdr"])
286313
local extra_w, extra_h = get_dimensions(v[extra_image_content_type])
@@ -309,6 +336,7 @@ local function generate_ultrahdr(encoding_variant, images, settings, step, total
309336
local total_substeps
310337
local substep = 0
311338
local uhdr
339+
local errors = {}
312340

313341
function update_job_progress()
314342
substep = substep + 1
@@ -319,28 +347,56 @@ local function generate_ultrahdr(encoding_variant, images, settings, step, total
319347
job.percent = (total_substeps * step + substep) / (total_steps * total_substeps)
320348
end
321349

322-
function copy_or_export_jpg(src, dest)
323-
if df.get_filetype(src.filename) == "jpg" and not src.is_altered then
324-
df.file_copy(src.path .. PS .. src.filename, dest)
350+
function copy_or_export(src_image, dest, format, colorspace, props)
351+
if df.get_filetype(src_image.filename) == df.get_filetype(dest) and not src_image.is_altered then
352+
return df.file_copy(src_image.path .. PS .. src_image.filename, dest)
325353
else
326-
local exporter = dt.new_format("jpeg")
327-
exporter.quality = 95
328-
exporter:write_image(src, dest)
354+
local prev = set_combobox("lib/export/profile", 0, "plugins/lighttable/export/icctype", colorspace)
355+
if not prev then
356+
return false
357+
end
358+
local exporter = dt.new_format(format)
359+
for k, v in pairs(props) do
360+
exporter[k] = v
361+
end
362+
local ok = not exporter:write_image(src_image, dest)
363+
if prev then
364+
set_combobox("lib/export/profile", 0, "plugins/lighttable/export/icctype", prev)
365+
end
366+
return ok
329367
end
368+
return true
330369
end
331370

332371
if encoding_variant == ENCODING_VARIANT_SDR_AND_GAINMAP or encoding_variant == ENCODING_VARIANT_SDR_AUTO_GAINMAP then
333372
total_substeps = 6
373+
local ok
334374
-- Export/copy both SDR and gainmap to JPEGs
335375
local sdr = df.create_unique_filename(settings.tmpdir .. PS .. df.chop_filetype(images["sdr"].filename) ..
336376
".jpg")
337-
copy_or_export_jpg(images["sdr"], sdr)
377+
ok = copy_or_export(images["sdr"], sdr, "jpeg", DT_COLORSPACE_DISPLAY_P3, {
378+
quality = settings.quality
379+
})
380+
if not ok then
381+
os.remove(sdr)
382+
table.insert(errors, string.format(_("Error exporting %s to %s"), images["sdr"].filename, "jpeg"))
383+
return false, errors
384+
end
385+
338386
local gainmap
339387
if encoding_variant == ENCODING_VARIANT_SDR_AUTO_GAINMAP then -- SDR is also a gainmap
340388
gainmap = sdr
341389
else
342390
gainmap = df.create_unique_filename(settings.tmpdir .. PS .. images["gainmap"].filename .. "_gainmap.jpg")
343-
copy_or_export_jpg(images["gainmap"], gainmap)
391+
ok = copy_or_export(images["gainmap"], gainmap, "jpeg", DT_COLORSPACE_DISPLAY_P3, {
392+
quality = settings.quality
393+
})
394+
if not ok then
395+
os.remove(sdr)
396+
os.remove(sdr)
397+
table.insert(errors, string.format(_("Error exporting %s to %s"), images["gainmap"].filename, "jpeg"))
398+
return false, errors
399+
end
344400
end
345401
log.msg(log.debug, string.format(_("Exported files: %s, %s"), sdr, gainmap))
346402
update_job_progress()
@@ -377,30 +433,54 @@ local function generate_ultrahdr(encoding_variant, images, settings, step, total
377433
end
378434
update_job_progress()
379435
elseif encoding_variant == ENCODING_VARIANT_SDR_AND_HDR then
380-
total_substeps = 5
436+
local ok
437+
total_substeps = 6
381438
-- https://discuss.pixls.us/t/manual-creation-of-ultrahdr-images/45004/20
382-
-- Step 1: Export SDR to PNG (HDR is already a JPEG-XL)
383-
local exporter = dt.new_format("png")
384-
exporter.bpp = 8
439+
-- Step 1: Export HDR to JPEG-XL with DT_COLORSPACE_PQ_P3
440+
local hdr = df.create_unique_filename(settings.tmpdir .. PS .. df.chop_filetype(images["hdr"].filename) ..
441+
".jxl")
442+
ok = copy_or_export(images["hdr"], hdr, "jpegxl", DT_COLORSPACE_PQ_P3, {
443+
bpp = 10,
444+
quality = 100 -- lossless
445+
})
446+
if not ok then
447+
os.remove(hdr)
448+
table.insert(errors, string.format(_("Error exporting %s to %s"), images["hdr"].filename, "jxl"))
449+
return false, errors
450+
end
451+
update_job_progress()
452+
-- Step 2: Export SDR to PNG
385453
local sdr = df.create_unique_filename(settings.tmpdir .. PS .. df.chop_filetype(images["sdr"].filename) ..
386454
".png")
387-
exporter:write_image(images["sdr"], sdr)
455+
ok = copy_or_export(images["sdr"], sdr, "png", DT_COLORSPACE_DISPLAY_P3, {
456+
bpp = 8
457+
})
458+
if not ok then
459+
os.remove(hdr)
460+
os.remove(sdr)
461+
table.insert(errors, string.format(_("Error exporting %s to %s"), images["sdr"].filename, "png"))
462+
return false, errors
463+
end
388464
uhdr = df.chop_filetype(sdr) .. "_ultrahdr.jpg"
389465

390466
update_job_progress()
391-
local extra = df.create_unique_filename(settings.tmpdir .. PS .. images["hdr"].filename .. ".raw")
392-
393-
-- Step 3: Generate libultrahdr RAW images
394-
execute_cmd(settings.bin.ffmpeg .. " -i " .. df.sanitize_filename(sdr) .. " -pix_fmt rgba -f rawvideo " ..
467+
-- Step 3: Generate libultrahdr RAW images
468+
local sdr_w, sdr_h = get_dimensions(images["sdr"])
469+
local resize_cmd = ""
470+
if sdr_h % 2 + sdr_w % 2 > 0 then -- needs resizing to even dimensions.
471+
resize_cmd = string.format(" -vf 'crop=%d:%d:0:0' ", sdr_w - sdr_w % 2, sdr_h - sdr_h % 2)
472+
end
473+
474+
execute_cmd(settings.bin.ffmpeg .. " -i " .. df.sanitize_filename(sdr) .. resize_cmd .. " -pix_fmt rgba -f rawvideo " ..
395475
df.sanitize_filename(sdr .. ".raw"))
396-
execute_cmd(settings.bin.ffmpeg .. " -i " ..
397-
df.sanitize_filename(images["hdr"].path .. PS .. images["hdr"].filename) ..
398-
" -pix_fmt p010le -f rawvideo " .. df.sanitize_filename(extra))
476+
execute_cmd(settings.bin.ffmpeg .. " -i " .. df.sanitize_filename(hdr) .. resize_cmd .. " -pix_fmt p010le -f rawvideo " ..
477+
df.sanitize_filename(hdr .. ".raw"))
399478
update_job_progress()
400-
local sdr_w, sdr_h = get_dimensions(images["sdr"])
401479
execute_cmd(settings.bin.ultrahdr_app .. " -m 0 -y " .. df.sanitize_filename(sdr .. ".raw") .. " -p " ..
402-
df.sanitize_filename(extra) .. " -a 0 -b 3 -c 1 -C 1 -t 2 -M 1 -s 1 -q 95 -Q 95 -D 1 " .. " -w " ..
403-
tostring(sdr_w) .. " -h " .. tostring(sdr_h) .. " -z " .. df.sanitize_filename(uhdr))
480+
df.sanitize_filename(hdr .. ".raw") ..
481+
string.format(" -a 0 -b 3 -c 1 -C 1 -t 2 -M 1 -s 1 -q %d -Q %d -D 1 ", settings.quality,
482+
settings.quality) .. " -w " .. tostring(sdr_w - sdr_w % 2) .. " -h " .. tostring(sdr_h - sdr_h % 2) .. " -z " ..
483+
df.sanitize_filename(uhdr))
404484
update_job_progress()
405485
if settings.copy_exif then
406486
-- Restricting tags to EXIF only, to make sure we won't mess up XMP tags (-all>all).
@@ -409,9 +489,10 @@ local function generate_ultrahdr(encoding_variant, images, settings, step, total
409489
df.sanitize_filename(uhdr) .. " -overwrite_original -preserve")
410490
end
411491
-- Cleanup
492+
os.remove(hdr)
412493
os.remove(sdr)
494+
os.remove(hdr .. ".raw")
413495
os.remove(sdr .. ".raw")
414-
os.remove(extra)
415496
update_job_progress()
416497
end
417498

@@ -433,12 +514,10 @@ local function generate_ultrahdr(encoding_variant, images, settings, step, total
433514
log.msg(log.info, msg)
434515
dt.print(msg)
435516
update_job_progress()
517+
return true, nil
436518
end
437519

438520
local function main()
439-
local saved_log_level = log.log_level()
440-
log.log_level(log.info)
441-
442521
save_preferences()
443522

444523
local selection_type = GUI.optionwidgets.selection_type_combo.selected
@@ -447,21 +526,25 @@ local function main()
447526

448527
local settings, errors = assert_settings_correct(encoding_variant)
449528
if not settings then
450-
dt.print(string.format(_("Export settings are incorrect, exiting:\n\n%s"), table.concat(errors, "\n")))
451-
log.log_level(saved_log_level)
529+
dt.print(string.format(_("Export settings are incorrect, exiting:\n\n- %s"), table.concat(errors, "\n- ")))
452530
return
453531
end
454532

455533
local stacks, stack_count = get_stacks(dt.gui.selection(), encoding_variant, selection_type)
456534
dt.print(string.format(_("Detected %d image stack(s)"), stack_count))
457535
if stack_count == 0 then
458-
log.log_level(saved_log_level)
459536
return
460537
end
461538
job = dt.gui.create_job(_("Generating UltraHDR images"), true, stop_job)
462539
local count = 0
540+
local msg
463541
for i, v in pairs(stacks) do
464-
generate_ultrahdr(encoding_variant, v, settings, count, stack_count)
542+
local ok, errors = generate_ultrahdr(encoding_variant, v, settings, count, stack_count)
543+
if not ok then
544+
dt.print(string.format(_("Errors generating images:\n\n- %s"), table.concat(errors, "\n- ")))
545+
job.valid = false
546+
return
547+
end
465548
count = count + 1
466549
-- sleep for a short moment to give stop_job callback function a chance to run
467550
dt.control.sleep(10)
@@ -471,10 +554,9 @@ local function main()
471554
job.valid = false
472555
end
473556

474-
local msg = string.format(_("Generated %d UltraHDR image(s)."), count)
557+
msg = string.format(_("Generated %d UltraHDR image(s)."), count)
475558
log.msg(log.info, msg)
476559
dt.print(msg)
477-
log.log_level(saved_log_level)
478560
end
479561

480562
GUI.optionwidgets.settings_label = dt.new_widget("section_label") {
@@ -591,12 +673,12 @@ GUI.optionwidgets.encoding_variant_combo = dt.new_widget("combobox") {
591673
This will determine the method used to generate UltraHDR.
592674
593675
- %s: SDR image paired with a gain map image.
594-
- %s: SDR image paired with a JPEG-XL HDR image (10-bit, 'PQ P3 RGB' profile recommended).
595-
- %s: SDR images only. Gainmaps will be copies of SDR images (the simplest option).
676+
- %s: SDR image paired with an HDR image.
677+
- %s: Each stack consists of a single SDR image. Gainmaps will be copies of SDR images.
596678
597679
By default, the first image in a stack is treated as SDR, and the second one is a gainmap/HDR.
598-
You can force the image into a specific stack slot by attaching "hdr" / "gainmap" tags to them.
599-
]]), _("SDR + gainmap"), _("SDR + JPEG-XL HDR"), _("SDR only")),
680+
You can force the image into a specific stack slot by attaching "hdr" / "gainmap" tags to it.
681+
]]), _("SDR + gainmap"), _("SDR + HDR"), _("SDR only")),
600682
selected = 0,
601683
changed_callback = function(self)
602684
GUI.run.sensitive = self.selected and self.selected > 0
@@ -607,7 +689,7 @@ You can force the image into a specific stack slot by attaching "hdr" / "gainmap
607689
end
608690
end,
609691
_("SDR + gainmap"), -- ENCODING_VARIANT_SDR_AND_GAINMAP
610-
_("SDR + JPEG-XL HDR"), -- ENCODING_VARIANT_SDR_AND_HDR
692+
_("SDR + HDR"), -- ENCODING_VARIANT_SDR_AND_HDR
611693
_("SDR only") -- ENCODING_VARIANT_SDR_AUTO_GAINMAP
612694
}
613695

@@ -627,10 +709,25 @@ As an added precaution, each image in a stack needs to have the same dimensions.
627709
_("multiple stacks (use filename)") -- SELECTION_TYPE_GROUP_BY_FNAME
628710
}
629711

712+
GUI.optionwidgets.quality_widget = dt.new_widget("slider") {
713+
label = _('Quality'),
714+
tooltip = _('Quality of the output UltraHDR JPEG file'),
715+
hard_min = 0,
716+
hard_max = 100,
717+
soft_min = 0,
718+
soft_max = 100,
719+
step = 1,
720+
digits = 0,
721+
reset_callback = function(self)
722+
self.value = 95
723+
end
724+
}
725+
630726
GUI.optionwidgets.encoding_settings_box = dt.new_widget("box") {
631727
orientation = "vertical",
632728
GUI.optionwidgets.selection_type_combo,
633729
GUI.optionwidgets.encoding_variant_combo,
730+
GUI.optionwidgets.quality_widget,
634731
GUI.optionwidgets.metadata_box
635732
}
636733

@@ -656,10 +753,7 @@ GUI.options = dt.new_widget("box") {
656753

657754
GUI.run = dt.new_widget("button") {
658755
label = _("Generate UltraHDR"),
659-
tooltip = _([[Generate UltraHDR image(s) from selection
660-
661-
Global options in the export module apply to the SDR image. Make sure that a proper color 'profile' setting is used (e.g. Display P3)
662-
]]),
756+
tooltip = _("Generate UltraHDR image(s) from selection"),
663757
clicked_callback = main
664758
}
665759

0 commit comments

Comments
 (0)