-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRawForge.lua
More file actions
359 lines (317 loc) · 9.71 KB
/
RawForge.lua
File metadata and controls
359 lines (317 loc) · 9.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
-- RawForge.lua
local dt = require "darktable"
local du = require "lib/dtutils"
du.check_min_api_version("7.0.0", "RawForge")
local gettext = dt.gettext.gettext
local function _(msgid) return gettext(msgid) end
-----------------------------------------------------------------------
-- Script metadata
-----------------------------------------------------------------------
local script_data = {}
script_data.metadata = {
name = "RawForge",
purpose = _("RAW denoise with RawForge"),
author = "Ayeda Okambawa",
}
-----------------------------------------------------------------------
-- Module state
-----------------------------------------------------------------------
local mE = {}
mE.widgets = {}
mE.event_registered = false
mE.module_installed = false
local mod = "module_RawForge"
local GUI = {}
-----------------------------------------------------------------------
-- Helpers
-----------------------------------------------------------------------
local function file_exists(path)
local f = io.open(path, "rb")
if f then
f:close()
return true
end
return false
end
local function build_unique_output_path(dir_path, base_name, sep)
local candidate = string.format("%s%s%s-rawforge.dng", dir_path, sep, base_name)
if not file_exists(candidate) then
return candidate
end
local index = 1
while true do
candidate = string.format("%s%s%s-rawforge_%d.dng", dir_path, sep, base_name, index)
if not file_exists(candidate) then
return candidate
end
index = index + 1
end
end
-----------------------------------------------------------------------
-- UI Widgets (main page)
-----------------------------------------------------------------------
local cbb_menu = dt.new_widget("combobox"){
label = _("Menu"),
tooltip = _("Switch between RawForge and Settings views"),
selected = 1,
"RawForge",
"Settings",
changed_callback = function(self)
dt.print_log("RawForge menu changed; GUI.stack=" .. tostring(GUI.stack))
if GUI.stack then
if self.selected == 1 then
GUI.stack.active = 1
elseif self.selected == 2 then
GUI.stack.active = 2
end
else
dt.print_log("RawForge: GUI.stack not ready; menu change ignored")
end
end
}
local cbb_models = dt.new_widget("combobox"){
label = _("Model"),
tooltip = _("Select RawForge denoise model"),
selected = 1,
"TreeNetDenoise",
"TreeNetDenoiseLight",
"TreeNetDenoiseHeavy",
"TreeNetDenoiseSuperLight",
"Deblur",
"DeepSharpen",
}
local sld_lumi = dt.new_widget("slider") {
label = _("Lumi"),
tooltip = _("Luminance noise added back to output (0 to 1)"),
soft_min = 0,
soft_max = 1,
hard_min = 0,
hard_max = 1,
step = 0.1,
digits = 1,
value = 0,
}
local sld_chroma = dt.new_widget("slider") {
label = _("Chroma"),
tooltip = _("Chroma noise added back to output (0 to 1)"),
soft_min = 0,
soft_max = 1,
hard_min = 0,
hard_max = 1,
step = 0.1,
digits = 1,
value = 0,
}
-----------------------------------------------------------------------
-- Main processing function
-----------------------------------------------------------------------
local function do_denoise()
local images = dt.gui.selection()
if #images < 1 then
dt.print(_("Select at least one image"))
return
end
local rawforge_bin = dt.preferences.read(mod, "rawforge_bin", "string") or "rawforge"
local exif_bin = dt.preferences.read(mod, "exif_bin", "string") or "exiftool"
if rawforge_bin == "" or exif_bin == "" then
dt.print(_("Please set executable paths in Settings"))
return
end
local is_windows = package.config:sub(1, 1) == "\\"
local sep = is_windows and "\\" or "/"
local model = cbb_models.value
local lumi = tostring(sld_lumi.value):gsub(",", ".")
local chroma = tostring(sld_chroma.value):gsub(",", ".")
local is_refine_model = (model == "Deblur" or model == "DeepSharpen")
for image_index, img in ipairs(images) do
local input_file = img.path .. sep .. img.filename
local base = img.filename:match("(.+)%.[^%.]+$") or img.filename
local out_file = build_unique_output_path(img.path, base, sep)
local command_rawforge
if is_windows then
if is_refine_model then
command_rawforge = string.format(
'cmd /c ""%s" %s "%s" "%s""',
rawforge_bin, model, input_file, out_file
)
else
command_rawforge = string.format(
'cmd /c ""%s" %s "%s" "%s" --cfa --lumi %s --chroma %s"',
rawforge_bin, model, input_file, out_file, lumi, chroma
)
end
else
if is_refine_model then
command_rawforge = string.format(
'"%s" %s "%s" "%s"',
rawforge_bin, model, input_file, out_file
)
else
command_rawforge = string.format(
'"%s" %s "%s" "%s" --cfa --lumi %s --chroma %s',
rawforge_bin, model, input_file, out_file, lumi, chroma
)
end
end
dt.print_log("Running: " .. command_rawforge)
local h = io.popen(command_rawforge)
local rawforge_out = ""
if h then
rawforge_out = h:read("*a") or ""
h:close()
end
dt.print_log(rawforge_out)
if not file_exists(out_file) then
dt.print(_("RawForge failed for ") .. img.filename)
goto continue
end
local command_exif
if is_windows then
command_exif = string.format(
'cmd /c ""%s" -overwrite_original -TagsFromFile "%s" -all:all "%s""',
exif_bin, input_file, out_file
)
else
command_exif = string.format(
'"%s" -overwrite_original -TagsFromFile "%s" -all:all "%s"',
exif_bin, input_file, out_file
)
end
dt.print_log("Running: " .. command_exif)
local h2 = io.popen(command_exif)
local exif_out = ""
if h2 then
exif_out = h2:read("*a") or ""
h2:close()
end
dt.print_log(exif_out)
dt.database.import(out_file)
dt.print(_("RawForge done for ") .. img.filename)
::continue::
end
end
-----------------------------------------------------------------------
-- Executable path selection UI
-----------------------------------------------------------------------
dt.preferences.register(
mod,
"rawforge_bin",
"string",
_("RawForge binary path"),
_("Path to the RawForge executable"),
""
)
dt.preferences.register(
mod,
"exif_bin",
"string",
_("ExifTool binary path"),
_("Path to the exiftool executable"),
""
)
local exe_rawforge = dt.new_widget("file_chooser_button"){
title = _("Select rawforge executable"),
value = dt.preferences.read(mod, "rawforge_bin", "string") or "rawforge",
is_directory = false,
tooltip = _("Path to the rawforge executable"),
}
local exe_exif = dt.new_widget("file_chooser_button"){
title = _("Select exiftool executable"),
value = dt.preferences.read(mod, "exif_bin", "string") or "exiftool",
is_directory = false,
tooltip = _("Path to the exiftool executable"),
}
local exe_update = dt.new_widget("button"){
label = _("Save paths"),
clicked_callback = function()
dt.preferences.write(mod, "rawforge_bin", "string", exe_rawforge.value)
dt.preferences.write(mod, "exif_bin", "string", exe_exif.value)
GUI.stack.active = 1
dt.print(_("Executable paths updated"))
end
}
-----------------------------------------------------------------------
-- Labels and buttons
-----------------------------------------------------------------------
local lbl_rawforge = dt.new_widget("section_label"){ label = _("RawForge") }
local denoise_button = dt.new_widget("button") {
label = _("Denoise"),
clicked_callback = function(_) do_denoise() end,
}
-----------------------------------------------------------------------
-- GUI layout assembly
-----------------------------------------------------------------------
GUI.options = dt.new_widget("box"){
orientation = "vertical",
lbl_rawforge,
cbb_menu,
cbb_models,
sld_lumi,
sld_chroma,
denoise_button,
}
local exe_box = dt.new_widget("box"){
orientation = "vertical",
exe_rawforge,
exe_exif,
exe_update,
}
GUI.stack = dt.new_widget("stack"){ GUI.options, exe_box }
if (dt.preferences.read(mod, "rawforge_bin", "string") or "") == "" or
(dt.preferences.read(mod, "exif_bin", "string") or "") == "" then
GUI.stack.active = 2
else
GUI.stack.active = 1
end
-----------------------------------------------------------------------
-- Module registration
-----------------------------------------------------------------------
local function install_module()
if not mE.module_installed then
dt.register_lib(
"RawForge",
_("RawForge"),
true,
false,
{[dt.gui.views.lighttable] = {"DT_UI_CONTAINER_PANEL_RIGHT_CENTER", 100}},
dt.new_widget("box"){ orientation = "vertical", GUI.stack },
nil,
nil
)
mE.module_installed = true
end
end
local function destroy()
if dt.gui.libs["RawForge"] then
dt.gui.libs["RawForge"].visible = false
end
end
local function restart()
if dt.gui.libs["RawForge"] then
dt.gui.libs["RawForge"].visible = true
end
end
-----------------------------------------------------------------------
-- View handling
-----------------------------------------------------------------------
if dt.gui.current_view().id == "lighttable" then
install_module()
else
if not mE.event_registered then
dt.register_event("RawForge", "view-changed",
function(event, old_view, new_view)
if new_view.id == "lighttable" then
install_module()
end
end)
mE.event_registered = true
end
end
-----------------------------------------------------------------------
-- API for Darktable script manager
-----------------------------------------------------------------------
script_data.destroy = destroy
script_data.restart = restart
script_data.destroy_method = "hide"
script_data.show = restart
return script_data