Skip to content

Commit 0690146

Browse files
committed
Add a manager for import filters + 2 examples
This adds a new callback to darktable: darktable.register_import_filter(name, callback)
1 parent 6bfabc0 commit 0690146

File tree

2 files changed

+159
-0
lines changed

2 files changed

+159
-0
lines changed

official/import_filter_manager.lua

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
--[[
2+
This file is part of darktable,
3+
copyright (c) 2015 Tobias Ellinghaus
4+
5+
darktable is free software: you can redistribute it and/or modify
6+
it under the terms of the GNU General Public License as published by
7+
the Free Software Foundation, either version 3 of the License, or
8+
(at your option) any later version.
9+
10+
darktable is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU General Public License for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with darktable. If not, see <http://www.gnu.org/licenses/>.
17+
]]
18+
--[[
19+
IMPORT FILTER MANAGER
20+
This script adds a dropdown list with import filters to the import dialog.
21+
Scripts can add new filters by registering them with
22+
darktable.register_import_filter(name, callback)
23+
The callback has type function(event, images), i.e., it is the same as when
24+
directly registering the pre-import event.
25+
26+
27+
USAGE
28+
* require this script from your main lua file
29+
* also require some files with import filters, for example import_filters.lua.
30+
it is important to add them AFTER this one!
31+
]]
32+
33+
local dt = require "darktable"
34+
35+
local import_filter_list = {}
36+
local n_import_filters = 1
37+
38+
-- allow changing the filter from the preferences
39+
dt.preferences.register("import_filter_manager", "active_filter", "string",
40+
"import filter", "the name of the filter used for importing images", "")
41+
42+
43+
-- the dropdown to select the active filter from the import dialog
44+
local filter_dropdown = dt.new_widget("combobox")
45+
{
46+
label = "import filter",
47+
editable = false,
48+
49+
changed_callback = function(widget)
50+
dt.preferences.write("import_filter_manager", "active_filter", "string", widget.value)
51+
end,
52+
53+
"" -- the first entry in the list is hard coded to "" so it's possible to have no filter
54+
}
55+
dt.gui.libs.import.register_widget(filter_dropdown)
56+
57+
58+
-- this is just a wrapper which calls the active import filter
59+
dt.register_event("pre-import", function(event, images)
60+
local active_filter = dt.preferences.read("import_filter_manager", "active_filter", "string")
61+
if active_filter == "" then return end
62+
local callback = import_filter_list[active_filter]
63+
if callback then callback(event, images) end
64+
end)
65+
66+
67+
-- add a new global function to register import filters
68+
dt.register_import_filter = function(name, callback)
69+
local active_filter = dt.preferences.read("import_filter_manager", "active_filter", "string")
70+
dt.print_error("registering import filter `" .. name .. "'")
71+
import_filter_list[name] = callback
72+
n_import_filters = n_import_filters + 1
73+
filter_dropdown[n_import_filters] = name
74+
if name == active_filter then filter_dropdown.value = n_import_filters end
75+
end
76+
77+
78+
-- vim: shiftwidth=2 expandtab tabstop=2 cindent
79+
-- kate: tab-indents: off; indent-width 2; replace-tabs on; remove-trailing-space on;

official/import_filters.lua

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
--[[
2+
This file is part of darktable,
3+
copyright (c) 2015 Tobias Ellinghaus
4+
5+
darktable is free software: you can redistribute it and/or modify
6+
it under the terms of the GNU General Public License as published by
7+
the Free Software Foundation, either version 3 of the License, or
8+
(at your option) any later version.
9+
10+
darktable is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU General Public License for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with darktable. If not, see <http://www.gnu.org/licenses/>.
17+
]]
18+
--[[
19+
EXAMPLE IMPORT FILTERS
20+
This script goes along with the import filter manager. It adds two filters:
21+
* ignore jpegs: this one does the same as the existing option in the import dialog
22+
and just skips all JPEGs during import.
23+
* prefer raw over jpeg: this one is a bit more elaborate, it ignores JPEGs when there
24+
is also another file with the same basename, otherwise it
25+
allows JPEGs, too.
26+
27+
USAGE
28+
* require this script from your main lua file AFTER import_filter_manager.lua
29+
]]
30+
31+
local dt = require "darktable"
32+
33+
-- we get fed a sorted list of filenames. just setting images to ignore to nil is enough
34+
35+
-- ignore jpeg
36+
dt.register_import_filter("ignore jpegs", function(event, images)
37+
dt.print_error("ignoring all jpegs")
38+
for i, img in ipairs(images) do
39+
local extension = img:match("[^.]*$"):upper()
40+
if (extension == "JPG") or (extension == "JPEG") then
41+
images[i] = nil
42+
end
43+
end
44+
end)
45+
46+
47+
-- ignore jpeg iff another format for the image is found
48+
dt.register_import_filter("prefer raw over jpeg", function(event, images)
49+
dt.print_error("prefering raw over jpeg")
50+
local current_base = ""
51+
local jpg_indices = {}
52+
local other_format_found = false
53+
for i, img in ipairs(images) do
54+
local extension = img:match("[^.]*$"):upper()
55+
local base = img:match("^.*[.]")
56+
57+
if base ~= current_base then
58+
-- we are done with the base name, act according to what we found out
59+
if other_format_found then
60+
for _, jpg in ipairs(jpg_indices) do
61+
images[jpg] = nil
62+
end
63+
end
64+
current_base = base
65+
other_format_found = false
66+
for k,_ in pairs(jpg_indices) do jpg_indices[k] = nil end
67+
end
68+
69+
-- remember what we have here to act accordingly after all instances of this base name were checked
70+
if (extension == "JPG") or (extension == "JPEG") then
71+
table.insert(jpg_indices, i)
72+
else
73+
other_format_found = true
74+
end
75+
76+
end
77+
end)
78+
79+
-- vim: shiftwidth=2 expandtab tabstop=2 cindent
80+
-- kate: tab-indents: off; indent-width 2; replace-tabs on; remove-trailing-space on;

0 commit comments

Comments
 (0)