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
17 changes: 10 additions & 7 deletions lua/image/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ local render = function(image)
if not term_size then return end
local scale_factor = 1.0
if type(state.options.scale_factor) == "number" then scale_factor = state.options.scale_factor end
local image_rows = math.floor(image.image_height / term_size.cell_height * scale_factor)
local image_columns = math.floor(image.image_width / term_size.cell_width * scale_factor)
local natural_image_rows = math.floor(image.image_height / term_size.cell_height)
local natural_image_columns = math.floor(image.image_width / term_size.cell_width)
-- scaled natural size: the image must never be rendered larger than this
local scaled_natural_width = math.max(1, math.floor(natural_image_columns * scale_factor))
local scaled_natural_height = math.max(1, math.floor(natural_image_rows * scale_factor))

log.debug(("render() %s"):format(image.id), {
id = image.id,
Expand Down Expand Up @@ -72,15 +75,15 @@ local render = function(image)
if width == 0 and height ~= 0 then width = math.ceil(geometry_height_px * aspect_ratio / term_size.cell_width) end
if height == 0 and width ~= 0 then height = math.ceil(geometry_width_px / aspect_ratio / term_size.cell_height) end

-- if both w/h are missing, use the image dimensions
-- if both w/h are missing, use the scaled natural image dimensions
if width == 0 and height == 0 then
width = image_columns
height = image_rows
width = scaled_natural_width
height = scaled_natural_height
end

-- rendered size cannot be larger than the image itself
-- width = math.min(width, image_columns)
-- height = math.min(height, image_rows)
width = math.min(width, scaled_natural_width)
height = math.min(height, scaled_natural_height)

-- screen max width/height
width = math.min(width, term_size.screen_cols)
Expand Down
28 changes: 25 additions & 3 deletions lua/image/utils/document.lua
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,34 @@ local create_document_integration = function(config)

local term_size = utils.term.get_size()
if not term_size then return end
local width, height = utils.math.adjust_to_aspect_ratio(
local state = image.global_state
local scale_factor = state.options.scale_factor or 1.0

-- compute the image's scaled natural width and apply same constraints as the renderer
local natural_width = math.max(1, math.floor(image.image_width / term_size.cell_width * scale_factor))
local width = math.min(natural_width, math.floor(term_size.screen_cols / 2))
if state.options.max_width then width = math.min(width, state.options.max_width) end
if state.options.max_width_window_percentage then
width = math.min(width, math.floor(term_size.screen_cols * state.options.max_width_window_percentage / 100))
end
local height = 0
width, height = utils.math.adjust_to_aspect_ratio(
term_size,
image.image_width,
image.image_height,
width,
height
)
if state.options.max_height then height = math.min(height, state.options.max_height) end
if state.options.max_height_window_percentage then
height = math.min(height, math.floor(term_size.screen_rows * state.options.max_height_window_percentage / 100))
end
width, height = utils.math.adjust_to_aspect_ratio(
term_size,
image.image_width,
image.image_height,
math.floor(term_size.screen_cols / 2),
0
width,
height
)
local win_config = {
relative = "cursor",
Expand Down
Loading