Skip to content

Commit a61329c

Browse files
committed
Verify raw file size in pixels.
Added option to keep temporary files around for analysis.
1 parent cf3e882 commit a61329c

File tree

1 file changed

+34
-11
lines changed

1 file changed

+34
-11
lines changed

contrib/ultrahdr.lua

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,8 @@ local function assert_settings_correct(encoding_variant)
221221
hdr_capacity_max = GUI.optionwidgets.hdr_capacity_max.value
222222
},
223223
quality = GUI.optionwidgets.quality_widget.value,
224-
tmpdir = dt.configuration.tmp_dir
224+
tmpdir = dt.configuration.tmp_dir,
225+
skip_cleanup = false -- keep temporary files around, for debugging.
225226
}
226227

227228
if not settings.use_original_dir and (not settings.output or not df.check_if_file_exists(settings.output)) then
@@ -328,6 +329,16 @@ local function stop_job(job)
328329
job.valid = false
329330
end
330331

332+
local function file_size(path)
333+
local f, err = io.open(path, "r")
334+
if not f then
335+
return 0
336+
end
337+
local size = f:seek("end")
338+
f:close()
339+
return size
340+
end
341+
331342
local function generate_ultrahdr(encoding_variant, images, settings, step, total_steps)
332343
local total_substeps
333344
local substep = 0
@@ -382,6 +393,9 @@ local function generate_ultrahdr(encoding_variant, images, settings, step, total
382393
end
383394

384395
function cleanup()
396+
if settings.skip_cleanup then
397+
return false
398+
end
385399
for _, v in pairs(remove_files) do
386400
os.remove(v)
387401
end
@@ -462,6 +476,7 @@ local function generate_ultrahdr(encoding_variant, images, settings, step, total
462476
-- Step 1: Export HDR to JPEG-XL with DT_COLORSPACE_PQ_P3
463477
local hdr = df.create_unique_filename(settings.tmpdir .. PS .. df.chop_filetype(images["hdr"].filename) ..
464478
".jxl")
479+
table.insert(remove_files, hdr)
465480
ok = copy_or_export(images["hdr"], hdr, "jpegxl", DT_COLORSPACE_PQ_P3, {
466481
bpp = 10,
467482
quality = 100, -- lossless
@@ -487,27 +502,34 @@ local function generate_ultrahdr(encoding_variant, images, settings, step, total
487502
table.insert(remove_files, uhdr)
488503
update_job_progress()
489504
-- Step 3: Generate libultrahdr RAW images
505+
local sdr_raw, hdr_raw = sdr .. ".raw", hdr .. ".raw"
506+
table.insert(remove_files, sdr_raw)
507+
table.insert(remove_files, hdr_raw)
490508
local sdr_w, sdr_h = get_dimensions(images["sdr"])
491509
local resize_cmd = ""
492510
if sdr_h % 2 + sdr_w % 2 > 0 then -- needs resizing to even dimensions.
493511
resize_cmd = string.format(" -vf 'crop=%d:%d:0:0' ", sdr_w - sdr_w % 2, sdr_h - sdr_h % 2)
494512
end
495-
table.insert(remove_files, sdr .. ".raw")
496-
table.insert(remove_files, hdr .. ".raw")
513+
local size_in_px = (sdr_w - sdr_w % 2) * (sdr_h - sdr_h % 2)
497514
cmd =
498515
settings.bin.ffmpeg .. " -i " .. df.sanitize_filename(sdr) .. resize_cmd .. " -pix_fmt rgba -f rawvideo " ..
499-
df.sanitize_filename(sdr .. ".raw")
500-
if not execute_cmd(cmd, string.format(_("Error generating %s"), sdr .. ".raw")) then
516+
df.sanitize_filename(sdr_raw)
517+
if not execute_cmd(cmd, string.format(_("Error generating %s"), sdr_raw)) then
501518
return cleanup(), errors
502519
end
503520
cmd = settings.bin.ffmpeg .. " -i " .. df.sanitize_filename(hdr) .. resize_cmd ..
504-
" -pix_fmt p010le -f rawvideo " .. df.sanitize_filename(hdr .. ".raw")
505-
if not execute_cmd(cmd, string.format(_("Error generating %s"), hdr .. ".raw")) then
521+
" -pix_fmt p010le -f rawvideo " .. df.sanitize_filename(hdr_raw)
522+
if not execute_cmd(cmd, string.format(_("Error generating %s"), hdr_raw)) then
523+
return cleanup(), errors
524+
end
525+
-- sanity check for file sizes (sometimes dt exports different size images if the files were never opened in darktable view)
526+
if file_size(sdr_raw) ~= size_in_px * 4 or file_size(hdr_raw) ~= size_in_px & 3 then
527+
table.insert(errors, string.format(_("Wrong raw image dimensions: %s, expected %dx%d. Try opening the image in darktable mode first."), images["sdr"].filename, sdr_w, sdr_h))
506528
return cleanup(), errors
507529
end
508530
update_job_progress()
509-
cmd = settings.bin.ultrahdr_app .. " -m 0 -y " .. df.sanitize_filename(sdr .. ".raw") .. " -p " ..
510-
df.sanitize_filename(hdr .. ".raw") ..
531+
cmd = settings.bin.ultrahdr_app .. " -m 0 -y " .. df.sanitize_filename(sdr_raw) .. " -p " ..
532+
df.sanitize_filename(hdr_raw) ..
511533
string.format(" -a 0 -b 3 -c 1 -C 1 -t 2 -M 0 -s 1 -q %d -Q %d -D 1 ", settings.quality,
512534
settings.quality) .. " -w " .. tostring(sdr_w - sdr_w % 2) .. " -h " .. tostring(sdr_h - sdr_h % 2) ..
513535
" -z " .. df.sanitize_filename(uhdr)
@@ -566,17 +588,18 @@ local function main()
566588
end
567589

568590
local stacks, stack_count = get_stacks(dt.gui.selection(), encoding_variant, selection_type)
569-
dt.print(string.format(_("Detected %d image stack(s)"), stack_count))
570591
if stack_count == 0 then
592+
dt.print(string.format(_("No image stacks detected.\n\nMake sure that the image pairs have the same widths and heights."), stack_count))
571593
return
572594
end
595+
dt.print(string.format(_("Detected %d image stack(s)"), stack_count))
573596
job = dt.gui.create_job(_("Generating UltraHDR images"), true, stop_job)
574597
local count = 0
575598
local msg
576599
for i, v in pairs(stacks) do
577600
local ok, errors = generate_ultrahdr(encoding_variant, v, settings, count, stack_count)
578601
if not ok then
579-
dt.print(string.format(_("Errors generating images:\n\n- %s"), table.concat(errors, "\n- ")))
602+
dt.print(string.format(_("Generating UltraHDR images failed:\n\n- %s"), table.concat(errors, "\n- ")))
580603
job.valid = false
581604
return
582605
end

0 commit comments

Comments
 (0)