Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lua/image/utils/magic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ local image_signatures = {
pdf = "%PDF",
}

local jxl_signatures = {
"\xFF\x0A",
"\x00\x00\x00\x0CJXL \x0D\x0A\x87\x0A",
}

local function read_file_header(file, num_bytes)
if not file then return nil end
local current_pos = file:seek()
Expand Down Expand Up @@ -68,6 +73,13 @@ M.detect_format = function(path)
return nil
end

for _, signature in ipairs(jxl_signatures) do
if check_signature_match(header, "jxl", signature) then
file:close()
return "jxl"
end
end

for format, signature in pairs(image_signatures) do
if check_signature_match(header, format, signature) then
if format == "jpeg" then
Expand Down
17 changes: 17 additions & 0 deletions tests/utils/magic_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,23 @@ describe("magic", function()
end)
end

it("detects JPEG XL codestream and container signatures", function()
local signatures = {
"\xFF\x0A",
"\x00\x00\x00\x0CJXL \x0D\x0A\x87\x0A",
}

for _, signature in ipairs(signatures) do
local path = vim.fn.tempname()
local file = assert(io.open(path, "wb"))
file:write(signature)
file:close()
assert.are.same("jxl", detect_format(path))
assert.is_true(require("image/utils/magic").is_image(path))
vim.fn.delete(path)
end
end)

it("returns nil for non-existent files", function()
assert.is_nil(detect_format("tests/test_data/nonexistent.png"))
end)
Expand Down