@@ -221,7 +221,8 @@ local function assert_settings_correct(encoding_variant)
221
221
hdr_capacity_max = GUI .optionwidgets .hdr_capacity_max .value
222
222
},
223
223
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.
225
226
}
226
227
227
228
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)
328
329
job .valid = false
329
330
end
330
331
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
+
331
342
local function generate_ultrahdr (encoding_variant , images , settings , step , total_steps )
332
343
local total_substeps
333
344
local substep = 0
@@ -382,6 +393,9 @@ local function generate_ultrahdr(encoding_variant, images, settings, step, total
382
393
end
383
394
384
395
function cleanup ()
396
+ if settings .skip_cleanup then
397
+ return false
398
+ end
385
399
for _ , v in pairs (remove_files ) do
386
400
os.remove (v )
387
401
end
@@ -462,6 +476,7 @@ local function generate_ultrahdr(encoding_variant, images, settings, step, total
462
476
-- Step 1: Export HDR to JPEG-XL with DT_COLORSPACE_PQ_P3
463
477
local hdr = df .create_unique_filename (settings .tmpdir .. PS .. df .chop_filetype (images [" hdr" ].filename ) ..
464
478
" .jxl" )
479
+ table.insert (remove_files , hdr )
465
480
ok = copy_or_export (images [" hdr" ], hdr , " jpegxl" , DT_COLORSPACE_PQ_P3 , {
466
481
bpp = 10 ,
467
482
quality = 100 , -- lossless
@@ -487,27 +502,34 @@ local function generate_ultrahdr(encoding_variant, images, settings, step, total
487
502
table.insert (remove_files , uhdr )
488
503
update_job_progress ()
489
504
-- 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 )
490
508
local sdr_w , sdr_h = get_dimensions (images [" sdr" ])
491
509
local resize_cmd = " "
492
510
if sdr_h % 2 + sdr_w % 2 > 0 then -- needs resizing to even dimensions.
493
511
resize_cmd = string.format (" -vf 'crop=%d:%d:0:0' " , sdr_w - sdr_w % 2 , sdr_h - sdr_h % 2 )
494
512
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 )
497
514
cmd =
498
515
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
501
518
return cleanup (), errors
502
519
end
503
520
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 ))
506
528
return cleanup (), errors
507
529
end
508
530
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 ) ..
511
533
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
534
settings .quality ) .. " -w " .. tostring (sdr_w - sdr_w % 2 ) .. " -h " .. tostring (sdr_h - sdr_h % 2 ) ..
513
535
" -z " .. df .sanitize_filename (uhdr )
@@ -566,17 +588,18 @@ local function main()
566
588
end
567
589
568
590
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 ))
570
591
if stack_count == 0 then
592
+ dt .print (string.format (_ (" No image stacks detected.\n\n Make sure that the image pairs have the same widths and heights." ), stack_count ))
571
593
return
572
594
end
595
+ dt .print (string.format (_ (" Detected %d image stack(s)" ), stack_count ))
573
596
job = dt .gui .create_job (_ (" Generating UltraHDR images" ), true , stop_job )
574
597
local count = 0
575
598
local msg
576
599
for i , v in pairs (stacks ) do
577
600
local ok , errors = generate_ultrahdr (encoding_variant , v , settings , count , stack_count )
578
601
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 - " )))
580
603
job .valid = false
581
604
return
582
605
end
0 commit comments