@@ -328,16 +328,23 @@ local function stop_job(job)
328
328
job .valid = false
329
329
end
330
330
331
- local function execute_cmd (cmd )
332
- log .msg (log .debug , cmd )
333
- return dtsys .external_command (cmd )
334
- end
335
-
336
331
local function generate_ultrahdr (encoding_variant , images , settings , step , total_steps )
337
332
local total_substeps
338
333
local substep = 0
339
334
local uhdr
340
335
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
341
348
342
349
function update_job_progress ()
343
350
substep = substep + 1
@@ -374,72 +381,82 @@ local function generate_ultrahdr(encoding_variant, images, settings, step, total
374
381
return true
375
382
end
376
383
384
+ function cleanup ()
385
+ for _ , v in pairs (remove_files ) do
386
+ os.remove (v )
387
+ end
388
+ return false
389
+ end
390
+
377
391
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
380
393
-- Export/copy both SDR and gainmap to JPEGs
381
394
local sdr = df .create_unique_filename (settings .tmpdir .. PS .. df .chop_filetype (images [" sdr" ].filename ) ..
382
395
" .jpg" )
396
+ table.insert (remove_files , sdr )
383
397
ok = copy_or_export (images [" sdr" ], sdr , " jpeg" , DT_COLORSPACE_DISPLAY_P3 , {
384
398
quality = settings .quality
385
399
})
386
400
if not ok then
387
- os.remove (sdr )
388
401
table.insert (errors , string.format (_ (" Error exporting %s to %s" ), images [" sdr" ].filename , " jpeg" ))
389
- return false , errors
402
+ return cleanup () , errors
390
403
end
391
404
392
405
local gainmap
393
406
if encoding_variant == ENCODING_VARIANT_SDR_AUTO_GAINMAP then -- SDR is also a gainmap
394
407
gainmap = sdr
395
408
else
396
409
gainmap = df .create_unique_filename (settings .tmpdir .. PS .. images [" gainmap" ].filename .. " _gainmap.jpg" )
410
+ table.insert (remove_files , gainmap )
397
411
ok = copy_or_export (images [" gainmap" ], gainmap , " jpeg" , DT_COLORSPACE_DISPLAY_P3 , {
398
412
quality = settings .quality
399
413
})
400
414
if not ok then
401
- os.remove (sdr )
402
- os.remove (sdr )
403
415
table.insert (errors , string.format (_ (" Error exporting %s to %s" ), images [" gainmap" ].filename , " jpeg" ))
404
- return false , errors
416
+ return cleanup () , errors
405
417
end
406
418
end
407
419
log .msg (log .debug , string.format (_ (" Exported files: %s, %s" ), sdr , gainmap ))
408
420
update_job_progress ()
409
421
-- 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
412
428
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
414
433
end
415
434
update_job_progress ()
416
435
-- Generate metadata.cfg file
417
436
local metadata_file = generate_metadata_file (settings )
437
+ table.insert (remove_files , metadata_file )
418
438
-- Merge files
419
439
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
424
447
update_job_progress ()
425
448
-- Copy SDR's EXIF to UltraHDR file
426
449
if settings .copy_exif then
427
450
-- Restricting tags to EXIF only, to make sure we won't mess up XMP tags (-all>all).
428
451
-- 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
439
457
end
440
458
update_job_progress ()
441
459
elseif encoding_variant == ENCODING_VARIANT_SDR_AND_HDR then
442
- local ok
443
460
total_substeps = 6
444
461
-- https://discuss.pixls.us/t/manual-creation-of-ultrahdr-images/45004/20
445
462
-- 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
448
465
ok = copy_or_export (images [" hdr" ], hdr , " jpegxl" , DT_COLORSPACE_PQ_P3 , {
449
466
bpp = 10 ,
450
467
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.
452
469
})
453
470
if not ok then
454
- os.remove (hdr )
455
471
table.insert (errors , string.format (_ (" Error exporting %s to %s" ), images [" hdr" ].filename , " jxl" ))
456
- return false , errors
472
+ return cleanup () , errors
457
473
end
458
474
update_job_progress ()
459
475
-- Step 2: Export SDR to PNG
460
476
local sdr = df .create_unique_filename (settings .tmpdir .. PS .. df .chop_filetype (images [" sdr" ].filename ) ..
461
477
" .png" )
478
+ table.insert (remove_files , sdr )
462
479
ok = copy_or_export (images [" sdr" ], sdr , " png" , DT_COLORSPACE_DISPLAY_P3 , {
463
480
bpp = 8
464
481
})
465
482
if not ok then
466
- os.remove (hdr )
467
- os.remove (sdr )
468
483
table.insert (errors , string.format (_ (" Error exporting %s to %s" ), images [" sdr" ].filename , " png" ))
469
- return false , errors
484
+ return cleanup () , errors
470
485
end
471
486
uhdr = df .chop_filetype (sdr ) .. " _ultrahdr.jpg"
472
-
487
+ table.insert ( remove_files , uhdr )
473
488
update_job_progress ()
474
489
-- Step 3: Generate libultrahdr RAW images
475
490
local sdr_w , sdr_h = get_dimensions (images [" sdr" ])
476
491
local resize_cmd = " "
477
492
if sdr_h % 2 + sdr_w % 2 > 0 then -- needs resizing to even dimensions.
478
493
resize_cmd = string.format (" -vf 'crop=%d:%d:0:0' " , sdr_w - sdr_w % 2 , sdr_h - sdr_h % 2 )
479
494
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
485
508
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
491
517
update_job_progress ()
492
518
if settings .copy_exif then
493
519
-- Restricting tags to EXIF only, to make sure we won't mess up XMP tags (-all>all).
494
520
-- 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
497
526
end
498
- -- Cleanup
499
- os.remove (hdr )
500
- os.remove (sdr )
501
- os.remove (hdr .. " .raw" )
502
- os.remove (sdr .. " .raw" )
503
527
update_job_progress ()
504
528
end
505
529
506
530
local output_dir = settings .use_original_dir and images [" sdr" ].path or settings .output
507
531
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
509
537
if settings .import_to_darktable then
510
538
local img = dt .database .import (output_file )
511
539
-- Add "ultrahdr" tag to the imported image
@@ -516,11 +544,11 @@ local function generate_ultrahdr(encoding_variant, images, settings, step, total
516
544
end
517
545
dt .tags .attach (tagnr , img )
518
546
end
519
-
547
+ cleanup ()
548
+ update_job_progress ()
520
549
local msg = string.format (_ (" Generated %s." ), df .get_filename (output_file ))
521
550
log .msg (log .info , msg )
522
551
dt .print (msg )
523
- update_job_progress ()
524
552
return true , nil
525
553
end
526
554
0 commit comments