From 12747d9054f75674fe12052b92c9ed6b85ce5198 Mon Sep 17 00:00:00 2001 From: Till Theato Date: Wed, 18 Feb 2015 20:57:04 +0100 Subject: [PATCH 1/6] Add cr2hdr.lua script. This script automates the steps involved to process an image created with the Magic Lantern Dual ISO module. Upon invoking the script with a shortcut "cr2hdr" provided by Magic Lantern is run on the selected images. The processed files are imported. They are also made group leaders to hide the original files. --- contrib/cr2hdr.lua | 79 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 contrib/cr2hdr.lua diff --git a/contrib/cr2hdr.lua b/contrib/cr2hdr.lua new file mode 100644 index 0000000..9a5d1ba --- /dev/null +++ b/contrib/cr2hdr.lua @@ -0,0 +1,79 @@ +--[[ + Copyright (C) 2015 Till Theato . + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +]] +--[[ +cr2hdr … + +This script … + +USAGE +* require this script from your main lua file +* … +]] + +local darktable = require "darktable" + +-- Tested with darktable 1.6.2 +darktable.configuration.check_version(...,{2,0,2}) + +local processed_files = {} + +function file_imported(event, image) + local filename = image.path .. "/" .. image.filename + if processed_files[filename] then + image.make_group_leader(image) + processed_files[filename] = nil + end +end + +-- Source: http://lua-users.org/wiki/SplitJoin +function lines(str) + local t = {} + local function helper(line) table.insert(t, line) return "" end + helper((str:gsub("(.-)\r?\n", helper))) + return t +end + +function convert_image(image) + if string.sub(image.filename, -3) == "CR2" then + local filename = image.path .. "/" .. image.filename + local handle = io.popen("cr2hdr " .. filename) + local result = handle:read("*a") + handle:close() + local out_filename = string.gsub(filename, ".CR2", ".DNG") + local file = io.open(out_filename) + if file then + file:close() + processed_files[out_filename] = true + darktable.database.import(out_filename) + else + result = lines(result) + darktable.print_error(image.filename .. ": cr2hdr failed: " .. result[#result-1]) + end + else + darktable.print_error(image.filename .. " is not a Canon RAW.") + end +end + +function convert_action_images(shortcut) + local images = darktable.gui.action_images + for _,image in pairs(images) do + convert_image(image) + end +end + +darktable.register_event("shortcut", convert_action_images, "Run cr2hdr (Magic Lantern DualISO converter) on ... images") +darktable.register_event("post-import-image", file_imported) From a458d5c991616e156788007261183fc431f0ae91 Mon Sep 17 00:00:00 2001 From: Till Theato Date: Wed, 18 Feb 2015 21:19:51 +0100 Subject: [PATCH 2/6] cr2hdr: Show progress bar (job). Also add script description. --- contrib/cr2hdr.lua | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/contrib/cr2hdr.lua b/contrib/cr2hdr.lua index 9a5d1ba..8f133dc 100644 --- a/contrib/cr2hdr.lua +++ b/contrib/cr2hdr.lua @@ -15,9 +15,16 @@ along with this program. If not, see . ]] --[[ -cr2hdr … +cr2hdr Magic Lantern Dual ISO processing. -This script … +This script automates the steps involved to process an image created +with the Magic Lantern Dual ISO module. Upon invoking the script with a +shortcut "cr2hdr" provided by Magic Lantern is run on the selected +images. The processed files are imported. They are also made group +leaders to hide the original files. + +ADDITIANAL SOFTWARE NEEDED FOR THIS SCRIPT +* cr2hdr (sources can be obtained through the Magic Lantern repository) USAGE * require this script from your main lua file @@ -30,6 +37,7 @@ local darktable = require "darktable" darktable.configuration.check_version(...,{2,0,2}) local processed_files = {} +local job function file_imported(event, image) local filename = image.path .. "/" .. image.filename @@ -39,6 +47,10 @@ function file_imported(event, image) end end +function stop_conversion(job) + job.valid = false +end + -- Source: http://lua-users.org/wiki/SplitJoin function lines(str) local t = {} @@ -69,10 +81,17 @@ function convert_image(image) end function convert_action_images(shortcut) + job = darktable.gui.create_job("Dual ISO conversion", true, stop_conversion) local images = darktable.gui.action_images - for _,image in pairs(images) do - convert_image(image) + for key,image in pairs(images) do + if job.valid then + job.percent = (key-1)/#images + convert_image(image) + else + return + end end + job.valid = false end darktable.register_event("shortcut", convert_action_images, "Run cr2hdr (Magic Lantern DualISO converter) on ... images") From 5504f2b232cbc4184a58aa1c119c9fbdecde34b3 Mon Sep 17 00:00:00 2001 From: Till Theato Date: Wed, 18 Feb 2015 21:38:42 +0100 Subject: [PATCH 3/6] cr2hdr: Make call to "cr2hdr" non-blocking. --- contrib/cr2hdr.lua | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/contrib/cr2hdr.lua b/contrib/cr2hdr.lua index 8f133dc..b2d2328 100644 --- a/contrib/cr2hdr.lua +++ b/contrib/cr2hdr.lua @@ -51,20 +51,10 @@ function stop_conversion(job) job.valid = false end --- Source: http://lua-users.org/wiki/SplitJoin -function lines(str) - local t = {} - local function helper(line) table.insert(t, line) return "" end - helper((str:gsub("(.-)\r?\n", helper))) - return t -end - function convert_image(image) if string.sub(image.filename, -3) == "CR2" then local filename = image.path .. "/" .. image.filename - local handle = io.popen("cr2hdr " .. filename) - local result = handle:read("*a") - handle:close() + local result = coroutine.yield("RUN_COMMAND", "cr2hdr " .. filename) local out_filename = string.gsub(filename, ".CR2", ".DNG") local file = io.open(out_filename) if file then @@ -72,8 +62,7 @@ function convert_image(image) processed_files[out_filename] = true darktable.database.import(out_filename) else - result = lines(result) - darktable.print_error(image.filename .. ": cr2hdr failed: " .. result[#result-1]) + darktable.print_error(filename .. ": cr2hdr failed.") end else darktable.print_error(image.filename .. " is not a Canon RAW.") From fdbef5dc848aa52175d7ba243d5a0750689cd139 Mon Sep 17 00:00:00 2001 From: Till Theato Date: Wed, 18 Feb 2015 22:13:13 +0100 Subject: [PATCH 4/6] cr2hdr: Show message when done. --- contrib/cr2hdr.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/contrib/cr2hdr.lua b/contrib/cr2hdr.lua index b2d2328..f3eff7d 100644 --- a/contrib/cr2hdr.lua +++ b/contrib/cr2hdr.lua @@ -43,7 +43,7 @@ function file_imported(event, image) local filename = image.path .. "/" .. image.filename if processed_files[filename] then image.make_group_leader(image) - processed_files[filename] = nil + processed_files[filename] = false end end @@ -80,6 +80,9 @@ function convert_action_images(shortcut) return end end + local success_count = 0 + for _ in pairs(processed_files) do success_count = success_count + 1 end + darktable.print("Dual ISO conversion successful on " .. success_count .. "/" .. #images .. " images.") job.valid = false end From a7fafa4ee038c3fa75b5b2d0dd948f815dd33341 Mon Sep 17 00:00:00 2001 From: Till Theato Date: Wed, 18 Feb 2015 23:21:44 +0100 Subject: [PATCH 5/6] cr2hdr: Run when importing collection (optin). --- contrib/cr2hdr.lua | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/contrib/cr2hdr.lua b/contrib/cr2hdr.lua index f3eff7d..90e8c06 100644 --- a/contrib/cr2hdr.lua +++ b/contrib/cr2hdr.lua @@ -23,12 +23,13 @@ shortcut "cr2hdr" provided by Magic Lantern is run on the selected images. The processed files are imported. They are also made group leaders to hide the original files. -ADDITIANAL SOFTWARE NEEDED FOR THIS SCRIPT +ADDITIONAL SOFTWARE NEEDED FOR THIS SCRIPT * cr2hdr (sources can be obtained through the Magic Lantern repository) USAGE * require this script from your main lua file -* … +* trigger conversion on selected/hovered images by shortcut (set shortcut in settings dialog) +* it is also possible to have the script run after importing a collection (optin, since it is not that fast) ]] local darktable = require "darktable" @@ -36,6 +37,7 @@ local darktable = require "darktable" -- Tested with darktable 1.6.2 darktable.configuration.check_version(...,{2,0,2}) +local queue = {} local processed_files = {} local job @@ -44,6 +46,10 @@ function file_imported(event, image) if processed_files[filename] then image.make_group_leader(image) processed_files[filename] = false + else + if darktable.preferences.read("cr2hdr", "onimport", "bool") then + table.insert(queue, image) + end end end @@ -69,22 +75,39 @@ function convert_image(image) end end -function convert_action_images(shortcut) +function convert_images() + if next(queue) == nil then return end + job = darktable.gui.create_job("Dual ISO conversion", true, stop_conversion) - local images = darktable.gui.action_images - for key,image in pairs(images) do + for key,image in pairs(queue) do if job.valid then - job.percent = (key-1)/#images + job.percent = (key-1)/#queue convert_image(image) else - return + break end end local success_count = 0 for _ in pairs(processed_files) do success_count = success_count + 1 end - darktable.print("Dual ISO conversion successful on " .. success_count .. "/" .. #images .. " images.") + darktable.print("Dual ISO conversion successful on " .. success_count .. "/" .. #queue .. " images.") job.valid = false + processed_files = {} + queue = {} end -darktable.register_event("shortcut", convert_action_images, "Run cr2hdr (Magic Lantern DualISO converter) on ... images") +function film_imported(event, film) + if darktable.preferences.read("cr2hdr", "onimport", "bool") then + convert_images() + end +end + +function convert_action_images(shortcut) + queue = darktable.gui.action_images + convert_images() +end + +darktable.register_event("shortcut", convert_action_images, "Run cr2hdr (Magic Lantern DualISO converter) on selected images") darktable.register_event("post-import-image", file_imported) +darktable.register_event("post-import-film", film_imported) + +darktable.preferences.register("cr2hdr", "onimport", "bool", "Invoke on import", "If true then cr2hdr will try to proccess every file during importing. Warning: cr2hdr is quite slow even in figuring out on whether the file is Dual ISO or not.", false) From 4baf385ba7c165daec5170671e44a7f5933cc731 Mon Sep 17 00:00:00 2001 From: Till Theato Date: Thu, 19 Feb 2015 23:00:52 +0100 Subject: [PATCH 6/6] cr2hdr: Make functions local. --- contrib/cr2hdr.lua | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/contrib/cr2hdr.lua b/contrib/cr2hdr.lua index 90e8c06..e6bc214 100644 --- a/contrib/cr2hdr.lua +++ b/contrib/cr2hdr.lua @@ -41,7 +41,7 @@ local queue = {} local processed_files = {} local job -function file_imported(event, image) +local function file_imported(event, image) local filename = image.path .. "/" .. image.filename if processed_files[filename] then image.make_group_leader(image) @@ -53,11 +53,11 @@ function file_imported(event, image) end end -function stop_conversion(job) +local function stop_conversion(job) job.valid = false end -function convert_image(image) +local function convert_image(image) if string.sub(image.filename, -3) == "CR2" then local filename = image.path .. "/" .. image.filename local result = coroutine.yield("RUN_COMMAND", "cr2hdr " .. filename) @@ -75,7 +75,7 @@ function convert_image(image) end end -function convert_images() +local function convert_images() if next(queue) == nil then return end job = darktable.gui.create_job("Dual ISO conversion", true, stop_conversion) @@ -95,13 +95,13 @@ function convert_images() queue = {} end -function film_imported(event, film) +local function film_imported(event, film) if darktable.preferences.read("cr2hdr", "onimport", "bool") then convert_images() end end -function convert_action_images(shortcut) +local function convert_action_images(shortcut) queue = darktable.gui.action_images convert_images() end