Skip to content

Commit ea7ac8e

Browse files
committed
Abort and cleanup early when any of the binaries fail.
1 parent 2a4452c commit ea7ac8e

File tree

1 file changed

+85
-57
lines changed

1 file changed

+85
-57
lines changed

contrib/ultrahdr.lua

Lines changed: 85 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -328,16 +328,23 @@ local function stop_job(job)
328328
job.valid = false
329329
end
330330

331-
local function execute_cmd(cmd)
332-
log.msg(log.debug, cmd)
333-
return dtsys.external_command(cmd)
334-
end
335-
336331
local function generate_ultrahdr(encoding_variant, images, settings, step, total_steps)
337332
local total_substeps
338333
local substep = 0
339334
local uhdr
340335
local errors = {}
336+
local remove_files = {}
337+
local ok
338+
local cmd
339+
340+
local function execute_cmd(cmd, errormsg)
341+
log.msg(log.debug, cmd)
342+
local code = dtsys.external_command(cmd)
343+
if errormsg and code > 0 then
344+
table.insert(errors, errormsg)
345+
end
346+
return code == 0
347+
end
341348

342349
function update_job_progress()
343350
substep = substep + 1
@@ -374,72 +381,82 @@ local function generate_ultrahdr(encoding_variant, images, settings, step, total
374381
return true
375382
end
376383

384+
function cleanup()
385+
for _, v in pairs(remove_files) do
386+
os.remove(v)
387+
end
388+
return false
389+
end
390+
377391
if encoding_variant == ENCODING_VARIANT_SDR_AND_GAINMAP or encoding_variant == ENCODING_VARIANT_SDR_AUTO_GAINMAP then
378-
total_substeps = 6
379-
local ok
392+
total_substeps = 5
380393
-- Export/copy both SDR and gainmap to JPEGs
381394
local sdr = df.create_unique_filename(settings.tmpdir .. PS .. df.chop_filetype(images["sdr"].filename) ..
382395
".jpg")
396+
table.insert(remove_files, sdr)
383397
ok = copy_or_export(images["sdr"], sdr, "jpeg", DT_COLORSPACE_DISPLAY_P3, {
384398
quality = settings.quality
385399
})
386400
if not ok then
387-
os.remove(sdr)
388401
table.insert(errors, string.format(_("Error exporting %s to %s"), images["sdr"].filename, "jpeg"))
389-
return false, errors
402+
return cleanup(), errors
390403
end
391404

392405
local gainmap
393406
if encoding_variant == ENCODING_VARIANT_SDR_AUTO_GAINMAP then -- SDR is also a gainmap
394407
gainmap = sdr
395408
else
396409
gainmap = df.create_unique_filename(settings.tmpdir .. PS .. images["gainmap"].filename .. "_gainmap.jpg")
410+
table.insert(remove_files, gainmap)
397411
ok = copy_or_export(images["gainmap"], gainmap, "jpeg", DT_COLORSPACE_DISPLAY_P3, {
398412
quality = settings.quality
399413
})
400414
if not ok then
401-
os.remove(sdr)
402-
os.remove(sdr)
403415
table.insert(errors, string.format(_("Error exporting %s to %s"), images["gainmap"].filename, "jpeg"))
404-
return false, errors
416+
return cleanup(), errors
405417
end
406418
end
407419
log.msg(log.debug, string.format(_("Exported files: %s, %s"), sdr, gainmap))
408420
update_job_progress()
409421
-- Strip EXIFs
410-
execute_cmd(settings.bin.exiftool .. " -all= " .. df.sanitize_filename(sdr) .. " -o " ..
411-
df.sanitize_filename(sdr .. ".noexif"))
422+
table.insert(remove_files, sdr .. ".noexif")
423+
cmd = settings.bin.exiftool .. " -all= " .. df.sanitize_filename(sdr) .. " -o " ..
424+
df.sanitize_filename(sdr .. ".noexif")
425+
if not execute_cmd(cmd, string.format(_("Error stripping EXIF from %s"), sdr)) then
426+
return cleanup(), errors
427+
end
412428
if sdr ~= gainmap then
413-
execute_cmd(settings.bin.exiftool .. " -all= " .. df.sanitize_filename(gainmap) .. " -overwrite_original")
429+
if not execute_cmd(settings.bin.exiftool .. " -all= " .. df.sanitize_filename(gainmap) ..
430+
" -overwrite_original", string.format(_("Error stripping EXIF from %s"), gainmap)) then
431+
return cleanup(), errors
432+
end
414433
end
415434
update_job_progress()
416435
-- Generate metadata.cfg file
417436
local metadata_file = generate_metadata_file(settings)
437+
table.insert(remove_files, metadata_file)
418438
-- Merge files
419439
uhdr = df.chop_filetype(sdr) .. "_ultrahdr.jpg"
420-
421-
execute_cmd(settings.bin.ultrahdr_app .. " -m 0 -i " .. df.sanitize_filename(sdr .. ".noexif") .. " -g " ..
422-
df.sanitize_filename(gainmap) .. " -f " .. df.sanitize_filename(metadata_file) .. " -z " ..
423-
df.sanitize_filename(uhdr))
440+
table.insert(remove_files, uhdr)
441+
cmd = settings.bin.ultrahdr_app .. " -m 0 -i " .. df.sanitize_filename(sdr .. ".noexif") .. " -g " ..
442+
df.sanitize_filename(gainmap) .. " -f " .. df.sanitize_filename(metadata_file) .. " -z " ..
443+
df.sanitize_filename(uhdr)
444+
if not execute_cmd(cmd, string.format(_("Error merging UltraHDR to %s"), uhdr)) then
445+
return cleanup(), errors
446+
end
424447
update_job_progress()
425448
-- Copy SDR's EXIF to UltraHDR file
426449
if settings.copy_exif then
427450
-- Restricting tags to EXIF only, to make sure we won't mess up XMP tags (-all>all).
428451
-- This might hapen e.g. when the source files are Adobe gainmap HDRs.
429-
execute_cmd(settings.bin.exiftool .. " -tagsfromfile " .. df.sanitize_filename(sdr) .. " -exif " ..
430-
df.sanitize_filename(uhdr) .. " -overwrite_original -preserve")
431-
end
432-
update_job_progress()
433-
-- Cleanup
434-
os.remove(sdr)
435-
os.remove(sdr .. ".noexif")
436-
os.remove(metadata_file)
437-
if sdr ~= gainmap then
438-
os.remove(gainmap)
452+
cmd = settings.bin.exiftool .. " -tagsfromfile " .. df.sanitize_filename(sdr) .. " -exif " ..
453+
df.sanitize_filename(uhdr) .. " -overwrite_original -preserve"
454+
if not execute_cmd(cmd, string.format(_("Error adding EXIF to %s"), uhdr)) then
455+
return cleanup(), errors
456+
end
439457
end
440458
update_job_progress()
441459
elseif encoding_variant == ENCODING_VARIANT_SDR_AND_HDR then
442-
local ok
443460
total_substeps = 6
444461
-- https://discuss.pixls.us/t/manual-creation-of-ultrahdr-images/45004/20
445462
-- Step 1: Export HDR to JPEG-XL with DT_COLORSPACE_PQ_P3
@@ -448,64 +465,75 @@ local function generate_ultrahdr(encoding_variant, images, settings, step, total
448465
ok = copy_or_export(images["hdr"], hdr, "jpegxl", DT_COLORSPACE_PQ_P3, {
449466
bpp = 10,
450467
quality = 100, -- lossless
451-
effort = 1, -- we don't care about the size, the faile is temporary.
468+
effort = 1 -- we don't care about the size, the faile is temporary.
452469
})
453470
if not ok then
454-
os.remove(hdr)
455471
table.insert(errors, string.format(_("Error exporting %s to %s"), images["hdr"].filename, "jxl"))
456-
return false, errors
472+
return cleanup(), errors
457473
end
458474
update_job_progress()
459475
-- Step 2: Export SDR to PNG
460476
local sdr = df.create_unique_filename(settings.tmpdir .. PS .. df.chop_filetype(images["sdr"].filename) ..
461477
".png")
478+
table.insert(remove_files, sdr)
462479
ok = copy_or_export(images["sdr"], sdr, "png", DT_COLORSPACE_DISPLAY_P3, {
463480
bpp = 8
464481
})
465482
if not ok then
466-
os.remove(hdr)
467-
os.remove(sdr)
468483
table.insert(errors, string.format(_("Error exporting %s to %s"), images["sdr"].filename, "png"))
469-
return false, errors
484+
return cleanup(), errors
470485
end
471486
uhdr = df.chop_filetype(sdr) .. "_ultrahdr.jpg"
472-
487+
table.insert(remove_files, uhdr)
473488
update_job_progress()
474489
-- Step 3: Generate libultrahdr RAW images
475490
local sdr_w, sdr_h = get_dimensions(images["sdr"])
476491
local resize_cmd = ""
477492
if sdr_h % 2 + sdr_w % 2 > 0 then -- needs resizing to even dimensions.
478493
resize_cmd = string.format(" -vf 'crop=%d:%d:0:0' ", sdr_w - sdr_w % 2, sdr_h - sdr_h % 2)
479494
end
480-
481-
execute_cmd(settings.bin.ffmpeg .. " -i " .. df.sanitize_filename(sdr) .. resize_cmd .. " -pix_fmt rgba -f rawvideo " ..
482-
df.sanitize_filename(sdr .. ".raw"))
483-
execute_cmd(settings.bin.ffmpeg .. " -i " .. df.sanitize_filename(hdr) .. resize_cmd .. " -pix_fmt p010le -f rawvideo " ..
484-
df.sanitize_filename(hdr .. ".raw"))
495+
table.insert(remove_files, sdr .. ".raw")
496+
table.insert(remove_files, hdr .. ".raw")
497+
cmd =
498+
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
501+
return cleanup(), errors
502+
end
503+
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
506+
return cleanup(), errors
507+
end
485508
update_job_progress()
486-
execute_cmd(settings.bin.ultrahdr_app .. " -m 0 -y " .. df.sanitize_filename(sdr .. ".raw") .. " -p " ..
487-
df.sanitize_filename(hdr .. ".raw") ..
488-
string.format(" -a 0 -b 3 -c 1 -C 1 -t 2 -M 0 -s 1 -q %d -Q %d -D 1 ", settings.quality,
489-
settings.quality) .. " -w " .. tostring(sdr_w - sdr_w % 2) .. " -h " .. tostring(sdr_h - sdr_h % 2) .. " -z " ..
490-
df.sanitize_filename(uhdr))
509+
cmd = settings.bin.ultrahdr_app .. " -m 0 -y " .. df.sanitize_filename(sdr .. ".raw") .. " -p " ..
510+
df.sanitize_filename(hdr .. ".raw") ..
511+
string.format(" -a 0 -b 3 -c 1 -C 1 -t 2 -M 0 -s 1 -q %d -Q %d -D 1 ", settings.quality,
512+
settings.quality) .. " -w " .. tostring(sdr_w - sdr_w % 2) .. " -h " .. tostring(sdr_h - sdr_h % 2) ..
513+
" -z " .. df.sanitize_filename(uhdr)
514+
if not execute_cmd(cmd, string.format(_("Error merging %s"), uhdr)) then
515+
return cleanup(), errors
516+
end
491517
update_job_progress()
492518
if settings.copy_exif then
493519
-- Restricting tags to EXIF only, to make sure we won't mess up XMP tags (-all>all).
494520
-- This might hapen e.g. when the source files are Adobe gainmap HDRs.
495-
execute_cmd(settings.bin.exiftool .. " -tagsfromfile " .. df.sanitize_filename(sdr) .. " -exif " ..
496-
df.sanitize_filename(uhdr) .. " -overwrite_original -preserve")
521+
cmd = settings.bin.exiftool .. " -tagsfromfile " .. df.sanitize_filename(sdr) .. " -exif " ..
522+
df.sanitize_filename(uhdr) .. " -overwrite_original -preserve"
523+
if not execute_cmd(cmd, string.format(_("Error adding EXIF to %s"), uhdr)) then
524+
return cleanup(), errors
525+
end
497526
end
498-
-- Cleanup
499-
os.remove(hdr)
500-
os.remove(sdr)
501-
os.remove(hdr .. ".raw")
502-
os.remove(sdr .. ".raw")
503527
update_job_progress()
504528
end
505529

506530
local output_dir = settings.use_original_dir and images["sdr"].path or settings.output
507531
local output_file = df.create_unique_filename(output_dir .. PS .. df.get_filename(uhdr))
508-
df.file_move(uhdr, output_file)
532+
ok = df.file_move(uhdr, output_file)
533+
if not ok then
534+
table.insert(errors, string.format(_("Error generating UltraHDR for %s"), images["sdr"].filename))
535+
return cleanup(), errors
536+
end
509537
if settings.import_to_darktable then
510538
local img = dt.database.import(output_file)
511539
-- Add "ultrahdr" tag to the imported image
@@ -516,11 +544,11 @@ local function generate_ultrahdr(encoding_variant, images, settings, step, total
516544
end
517545
dt.tags.attach(tagnr, img)
518546
end
519-
547+
cleanup()
548+
update_job_progress()
520549
local msg = string.format(_("Generated %s."), df.get_filename(output_file))
521550
log.msg(log.info, msg)
522551
dt.print(msg)
523-
update_job_progress()
524552
return true, nil
525553
end
526554

0 commit comments

Comments
 (0)