Skip to content

Commit

Permalink
fix: optimize kitty rendering (non-cropped)
Browse files Browse the repository at this point in the history
  • Loading branch information
3rd committed Jul 3, 2023
1 parent 8f7a054 commit 52b25c3
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
24 changes: 14 additions & 10 deletions lua/image/backends/kitty/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,27 @@ backend.setup = function(state)
})
end

-- extend from empty line strategy to use extmarks
local transmitted_images = {}
backend.render = function(image, x, y, width, height)
local with_virtual_placeholders = backend.state.options.kitty_method == "unicode-placeholders"

-- save cursor
helpers.move_cursor(x + 1, y + 1, true)

-- transmit image
helpers.write_graphics({
if transmitted_images[image.id] ~= image.crop_hash then
action = codes.control.action.transmit,
image_id = image.internal_id,
transmit_format = codes.control.transmit_format.png,
transmit_medium = codes.control.transmit_medium.file,
display_cursor_policy = codes.control.display_cursor_policy.do_not_move,
display_virtual_placeholder = with_virtual_placeholders and 1 or 0,
quiet = 2,
}, image.path)
helpers.write_graphics({
action = codes.control.action.transmit,
image_id = image.internal_id,
transmit_format = codes.control.transmit_format.png,
transmit_medium = codes.control.transmit_medium.file,
display_cursor_policy = codes.control.display_cursor_policy.do_not_move,
display_virtual_placeholder = with_virtual_placeholders and 1 or 0,
quiet = 2,
}, image.path)
transmitted_images[image.id] = true
end

-- unicode placeholders
if with_virtual_placeholders then
Expand Down Expand Up @@ -94,7 +98,7 @@ backend.clear = function(image_id, shallow)
if not image then return end
helpers.write_graphics({
action = codes.control.action.delete,
display_delete = "i",
display_delete = shallow and "i" or "I",
image_id = image.internal_id,
quiet = 2,
})
Expand Down
2 changes: 1 addition & 1 deletion lua/image/image.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ local from_file = function(path, options, state)
},
with_virtual_padding = opts.with_virtual_padding or false,
is_rendered = false,
is_cropped = false,
crop_hash = nil,
}

---@param geometry? ImageGeometry
Expand Down
6 changes: 4 additions & 2 deletions lua/image/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ local render = function(image, state)

if image.window and image.buffer then
local win_info = vim.fn.getwininfo(image.window)[1]
if not win_info then return false end
local topline = win_info.topline
local botline = win_info.botline

Expand Down Expand Up @@ -281,7 +282,8 @@ local render = function(image, state)
end

-- perform crop
if needs_crop or (image.is_cropped and not needs_crop) then
local crop_hash = needs_crop and ("%d-%d-%d-%d"):format(pixel_width, pixel_height, 0, crop_offset_top) or nil
if crop_hash or (image.crop_hash ~= crop_hash) then
local cropped_image = magick.load_image(image.original_path)
cropped_image:set_format("png")
utils.debug(("cropping image: %d, %d, %d, %d"):format(pixel_width, pixel_height, 0, crop_offset_top))
Expand All @@ -290,7 +292,7 @@ local render = function(image, state)
cropped_image:write(tmp_path)
cropped_image:destroy()
image.path = tmp_path
image.is_cropped = true
image.crop_hash = crop_hash
end

-- utils.debug(("(5) x: %d, y: %d, width: %d, height: %d y_offset: %d"):format(x, y, width, height, y_offset))
Expand Down
4 changes: 2 additions & 2 deletions lua/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
---@field rendered_geometry ImageGeometry
---@field bounds ImageBounds
---@field is_rendered boolean
---@field is_cropped boolean
---@field crop_hash? string
---@field render fun(geometry?: ImageGeometry)
---@field clear fun()

Expand Down Expand Up @@ -134,4 +134,4 @@
---@field display_cursor_policy 0|1
---@field display_virtual_placeholder 0|1
---@field display_zindex number
---@field display_delete "a"|"i"|"p"
---@field display_delete "a"|"i"|"I"|"p"

0 comments on commit 52b25c3

Please sign in to comment.