perf!: faster image preview with optimized magick arguments (#2533)

Co-authored-by: sxyazi <sxyazi@gmail.com>
This commit is contained in:
ze0987 2025-03-26 14:10:14 +01:00 committed by GitHub
parent aedfe766c5
commit ad09fb89d9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 72 additions and 19 deletions

View file

@ -98,7 +98,8 @@ spotters = [
{ mime = "text/*", run = "code" },
{ mime = "application/{mbox,javascript,wine-extension-ini}", run = "code" },
# Image
{ mime = "image/{avif,hei?,jxl,svg+xml}", run = "magick" },
{ mime = "image/{avif,hei?,jxl}", run = "magick" },
{ mime = "image/svg+xml", run = "svg" },
{ mime = "image/*", run = "image" },
# Video
{ mime = "video/*", run = "video" },
@ -107,7 +108,8 @@ spotters = [
]
preloaders = [
# Image
{ mime = "image/{avif,hei?,jxl,svg+xml}", run = "magick" },
{ mime = "image/{avif,hei?,jxl}", run = "magick" },
{ mime = "image/svg+xml", run = "svg" },
{ mime = "image/*", run = "image" },
# Video
{ mime = "video/*", run = "video" },
@ -125,7 +127,8 @@ previewers = [
# JSON
{ mime = "application/{json,ndjson}", run = "json" },
# Image
{ mime = "image/{avif,hei?,jxl,svg+xml}", run = "magick" },
{ mime = "image/{avif,hei?,jxl}", run = "magick" },
{ mime = "image/svg+xml", run = "svg" },
{ mime = "image/*", run = "image" },
# Video
{ mime = "video/*", run = "video" },

View file

@ -24,24 +24,19 @@ function M:preload(job)
return true
end
local cmd = Command("magick"):args {
"-density",
200,
tostring(job.file.url),
"-flatten",
"-resize",
string.format("%dx%d^", rt.preview.max_width, rt.preview.max_height),
"-quality",
rt.preview.image_quality,
"-auto-orient",
"JPG:" .. tostring(cache),
}
if rt.tasks.image_alloc > 0 then
cmd = cmd:env("MAGICK_MEMORY_LIMIT", rt.tasks.image_alloc)
local cmd = M.with_env()
if job.args.flatten then
cmd = cmd:arg("-flatten")
end
local status, err = cmd:env("MAGICK_THREAD_LIMIT", 1):status()
-- stylua: ignore
local status, err = cmd:args {
tostring(job.file.url), "-auto-orient", "-strip",
"-sample", string.format("%dx%d>", rt.preview.max_width, rt.preview.max_height),
"-quality", rt.preview.image_quality,
string.format("JPG:%s", cache),
}:status()
if status then
return status.success
else
@ -51,4 +46,12 @@ end
function M:spot(job) require("file"):spot(job) end
function M.with_env()
local cmd = Command("magick"):env("MAGICK_THREAD_LIMIT", 1)
if rt.tasks.image_alloc > 0 then
cmd = cmd:env("MAGICK_MEMORY_LIMIT", rt.tasks.image_alloc)
end
return cmd
end
return M

View file

@ -0,0 +1,46 @@
local M = {}
function M:peek(job)
local start, cache = os.clock(), ya.file_cache(job)
if not cache then
return
end
local ok, err = self:preload(job)
if not ok or err then
return
end
ya.sleep(math.max(0, rt.preview.image_delay / 1000 + start - os.clock()))
ya.image_show(cache, job.area)
ya.preview_widgets(job, {})
end
function M:seek() end
function M:preload(job)
local cache = ya.file_cache(job)
if not cache or fs.cha(cache) then
return true
end
-- stylua: ignore
local cmd = require("magick").with_env():args {
"-density", 200,
tostring(job.file.url), "-strip",
"-resize", string.format("%dx%d^", rt.preview.max_width, rt.preview.max_height),
"-quality", rt.preview.image_quality,
string.format("JPG:%s", cache),
}
local status, err = cmd:status()
if status then
return status.success
else
return true, Err("Failed to start `magick`, error: %s", err)
end
end
function M:spot(job) require("file"):spot(job) end
return M

View file

@ -42,6 +42,7 @@ impl Default for Loader {
("noop".to_owned(), preset!("plugins/noop").into()),
("pdf".to_owned(), preset!("plugins/pdf").into()),
("session".to_owned(), preset!("plugins/session").into()),
("svg".to_owned(), preset!("plugins/svg").into()),
("video".to_owned(), preset!("plugins/video").into()),
("zoxide".to_owned(), preset!("plugins/zoxide").into()),
]);