Skip to content

Commit 33ef58a

Browse files
authored
Merge pull request darktable-org#315 from schwerdf/master
New utility function for random image access by ID.
2 parents 8091ba9 + 8e0bf9e commit 33ef58a

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

lib/dtutils.lua

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,4 +283,47 @@ function dtutils.check_os(operating_systems)
283283
return false
284284
end
285285

286+
dtutils.libdoc.functions["find_image_by_id"] = {
287+
Name = [[find_image_by_id]],
288+
Synopsis = [[look up an image by ID in the database]],
289+
Usage = [[local du = require "lib/dtutils"
290+
local img = du.find_image_by_id(imgid)
291+
id - int - the ID to look up
292+
]],
293+
Description = [[find_image_by_id looks up an image by ID in the database.]],
294+
Return_Value = [[result - dt_lua_image_t - image with the given ID if found, nil if not]],
295+
Limitations = [[]],
296+
Example = [[]],
297+
See_Also = [[]],
298+
Reference = [[]],
299+
License = [[]],
300+
Copyright = [[]],
301+
}
302+
303+
function dtutils.find_image_by_id(imgid)
304+
if #dt.database == 0 or imgid > dt.database[#dt.database].id then
305+
return nil
306+
end
307+
local min = 1
308+
local max = #dt.database
309+
while (max-min)//2 > 0 do
310+
local mid = min + (max-min)//2
311+
local midID = dt.database[mid].id
312+
if imgid == midID then
313+
return dt.database[mid]
314+
elseif imgid < midID then
315+
max = mid-1
316+
else
317+
min = mid+1
318+
end
319+
end
320+
if dt.database[min].id == imgid then
321+
return dt.database[min]
322+
elseif dt.database[max].id == imgid then
323+
return dt.database[max]
324+
else
325+
return nil
326+
end
327+
end
328+
286329
return dtutils

0 commit comments

Comments
 (0)