Skip to content

Commit

Permalink
Merge pull request #175 from linkarzu/master
Browse files Browse the repository at this point in the history
add support for AVIF files
  • Loading branch information
3rd committed Jun 5, 2024
2 parents 645f997 + 5161b04 commit a2a0849
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ require("image").setup({
window_overlap_clear_ft_ignore = { "cmp_menu", "cmp_docs", "" },
editor_only_render_when_focused = false, -- auto show/hide images when the editor gains/looses focus
tmux_show_only_in_active_window = false, -- auto show/hide images in the correct Tmux window (needs visual-activity off)
hijack_file_patterns = { "*.png", "*.jpg", "*.jpeg", "*.gif", "*.webp" }, -- render image files as images when opened
hijack_file_patterns = { "*.png", "*.jpg", "*.jpeg", "*.gif", "*.webp", "*.avif" }, -- render image files as images when opened
})
```

Expand Down
2 changes: 1 addition & 1 deletion lua/image/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ local default_options = {
window_overlap_clear_ft_ignore = { "cmp_menu", "cmp_docs", "scrollview", "scrollview_sign" },
editor_only_render_when_focused = false,
tmux_show_only_in_active_window = false,
hijack_file_patterns = { "*.png", "*.jpg", "*.jpeg", "*.gif", "*.webp" },
hijack_file_patterns = { "*.png", "*.jpg", "*.jpeg", "*.gif", "*.webp", "*.avif" },
}

---@type State
Expand Down
24 changes: 17 additions & 7 deletions lua/image/utils/magic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ local image_signatures = {
HEIC = "\x66\x74\x79\x70",
XPM = "\x2F\x2A\x20\x58\x50\x4D\x20\x2A\x2F",
ICO = "\x00\x00\x01\x00",
AVIF = "\x66\x74\x79\x70\x61\x76\x69\x66",
SVG = "<svg",
XML = "<?xml",
}
Expand All @@ -33,25 +34,34 @@ local function is_image(path)
local file, _ = io.open(path, "rb")
if not file then return false end

local max_bytes = 9
local max_bytes = 12
local header, _ = read_file_header(file, max_bytes)
if not header then
file:close()
return false
end

local is_image_flag = false
for _, signature in pairs(image_signatures) do
for key, signature in pairs(image_signatures) do
local bytes = { signature:byte(1, #signature) }
local match = true
for i = 1, #bytes do
if header[i] ~= bytes[i] then
match = false
break
if key == "AVIF" then
for i = 1, #bytes do
if header[i + 4] ~= bytes[i] then
match = false
break
end
end
else
for i = 1, #bytes do
if header[i] ~= bytes[i] then
match = false
break
end
end
end
if match then
if signature == image_signatures.JPEG then
if key == "JPEG" then
is_image_flag = has_jpeg_end_signature(file)
else
is_image_flag = true
Expand Down

0 comments on commit a2a0849

Please sign in to comment.