Skip to content

Commit

Permalink
rework structure - add sixel ref files
Browse files Browse the repository at this point in the history
  • Loading branch information
t-bltg committed Aug 30, 2022
1 parent 09c8f21 commit 4b37961
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 46 deletions.
2 changes: 1 addition & 1 deletion src/UnicodePlots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ include("lut.jl")
include("graphics.jl")
include("graphics/bargraphics.jl")
include("graphics/boxgraphics.jl")
include("graphics/imagegraphics.jl")

include("canvas.jl")
include("canvas/lookupcanvas.jl")
Expand All @@ -92,7 +93,6 @@ include("canvas/blockcanvas.jl")
include("canvas/asciicanvas.jl")
include("canvas/dotcanvas.jl")
include("canvas/heatmapcanvas.jl")
include("canvas/imgcanvas.jl")

include("description.jl")
include("volume.jl")
Expand Down
2 changes: 1 addition & 1 deletion src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ function __init__()
end
@require ImageInTerminal = "d8c32880-2388-543b-8c61-d9f865259254" begin
imageplot(img::AbstractArray{<:Colorant}; kwargs...) =
Plot(ImgCanvas(img); kwargs...)
Plot(ImageGraphics(img); kwargs...)
end
nothing
end
Expand Down
21 changes: 10 additions & 11 deletions src/canvas/imgcanvas.jl → src/graphics/imagegraphics.jl
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
struct ImgCanvas{C<:Colorant} <: Canvas
struct ImageGraphics{C<:Colorant} <: GraphicsArea
img::Matrix{C}
sixel::RefValue{Bool}
visible::Bool
encoded_size::Vector{Int}
ren::Vector{String}
end

ImgCanvas(img::AbstractArray{<:Colorant}) =
ImgCanvas(img, Ref(false), true, [0, 0], String[])
ImageGraphics(img::AbstractArray{<:Colorant}) =
ImageGraphics(img, Ref(false), true, [0, 0], String[])

@inline nrows(c::ImgCanvas) = c.encoded_size[1]
@inline ncols(c::ImgCanvas) = c.encoded_size[2]
@inline nrows(c::ImageGraphics) = c.encoded_size[1]
@inline ncols(c::ImageGraphics) = c.encoded_size[2]

function preprocess!(io::IO, c::ImgCanvas)
function preprocess!(io::IO, c::ImageGraphics)
ctx = IOContext(PipeBuffer(), :displaysize => displaysize(io))
caret = fallback_caret = 15, 7 # determine the terminal caret size, in pixels
c.sixel[] = false
char_h = char_w = nothing # determine the terminal caret size, in pixels
if ImageInTerminal.choose_sixel(c.img)
ans = ImageInTerminal.Sixel.TerminalTools.query_terminal("\e[16t", stdout)
if ans isa String && (m = match(r"\e\[6;(\d+);(\d+)t", ans)) nothing
caret = tryparse.(Int, m.captures)
c.sixel[] = true
char_h, char_w = tryparse.(Int, m.captures)
c.sixel[] = char_h nothing && char_w nothing
end
end
if c.sixel[]
char_h, char_w = something.(caret, fallback_caret)
h, w = size(c.img)
lines = String[]
for r 1:char_h:h
Expand All @@ -43,7 +42,7 @@ function preprocess!(io::IO, c::ImgCanvas)
c -> nothing
end

function print_row(io::IO, print_nocol, _, c::ImgCanvas, row::Integer)
function print_row(io::IO, print_nocol, _, c::ImageGraphics, row::Integer)
0 < row nrows(c) || throw(ArgumentError("`row` out of bounds: $row"))
print_nocol(io, c.ren[row])
nothing
Expand Down
38 changes: 19 additions & 19 deletions src/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -137,20 +137,20 @@ function _show(end_io::IO, print_nocol, print_color, p::Plot)
io_color = get(end_io, :color, false)
io = IOContext(buf, :color => io_color, :displaysize => displaysize(end_io))

c = p.graphics
🗷 = Char(BLANK) # blank outside canvas
🗹 = blank(c) # blank inside canvas
g = p.graphics
🗷 = Char(BLANK) # blank outside graphics
🗹 = blank(g) # blank inside graphics
############################################################
# 🗷 = 'x' # debug
# 🗹 = Char(typeof(c) <: BrailleCanvas ? '⠿' : 'o') # debug
# 🗹 = Char(typeof(g) <: BrailleCanvas ? '⠿' : 'o') # debug
############################################################
postprocess! = preprocess!(io, c)
postprocess! = preprocess!(io, g)

nr = nrows(c)
nc = ncols(c)
p_width = nc + 2 # left corner + border length (number of canvas cols) + right corner
nr = nrows(g)
nc = ncols(g)
p_width = nc + 2 # left corner + border length (number of graphics cols) + right corner

bmap = BORDERMAP[p.border :none && c isa BrailleCanvas ? :bnone : p.border]
bmap = BORDERMAP[p.border :none && g isa BrailleCanvas ? :bnone : p.border]
bc = BORDER_COLOR[]

# get length of largest strings to the left and right
Expand Down Expand Up @@ -212,7 +212,7 @@ function _show(end_io::IO, print_nocol, print_color, p::Plot)
🗹 * border_right_pad * '\n',
🗹,
)
c.visible && print_border(
g.visible && print_border(
io,
print_nocol,
print_color,
Expand Down Expand Up @@ -255,16 +255,16 @@ function _show(end_io::IO, print_nocol, print_color, p::Plot)
# print the left annotation
print_color(io, left_col, left_str)
end
if c.visible
if g.visible
# print left border
print_nocol(io, plot_padding)
print_color(io, bc, bmap[:l])
# print canvas row
print_row(io, print_nocol, print_color, c, row)
if c isa ImgCanvas && c.sixel[]
print_row(io, print_nocol, print_color, g, row)
if g isa ImageGraphics && g.sixel[]
offset = plot_offset + nc + 1
# 1F: move cursor to beginning of previous line, 1 line up
# $(offset)C: move cursor right $offset columns
# 1F: move cursor to the beginning of the previous line, 1 line up
# $(offset)C: move cursor to the right by an amount of $offset columns
write(io, "\e[1F\e[$(offset)C")
end
# print right label and padding
Expand All @@ -282,7 +282,7 @@ function _show(end_io::IO, print_nocol, print_color, p::Plot)
io,
print_nocol,
print_color,
c,
g,
row,
p.cmap,
min_max_z_str,
Expand All @@ -292,13 +292,13 @@ function _show(end_io::IO, print_nocol, print_color, p::Plot)
🗷,
)
end
row < nrows(c) && print_nocol(io, '\n')
row < nrows(g) && print_nocol(io, '\n')
end

postprocess!(c)
postprocess!(g)

# draw bottom border
c.visible && print_border(
g.visible && print_border(
io,
print_nocol,
print_color,
Expand Down
25 changes: 25 additions & 0 deletions test/references_24/imageplot/img_sixel.txt

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions test/references_8/imageplot/img_sixel.txt

Large diffs are not rendered by default.

28 changes: 14 additions & 14 deletions test/tst_imageplot.jl
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
if isinteractive() # FIXME: Sixel.query_terminal(...) fails when !(stdin isa TTY)
img = testimage("monarch_color_256")
img = testimage("monarch_color_256")

@testset "blocks" begin
_old_enc = ImageInTerminal.ENCODER_BACKEND[]
ImageInTerminal.ENCODER_BACKEND[] = :XTermColors
p = imageplot(img, title = "blocks")
test_ref("imageplot/img_blocks.txt", @show_col(p, :displaysize => T_SZ))
@test !p.graphics.sixel[]
ImageInTerminal.ENCODER_BACKEND[] = _old_enc
end

if ImageInTerminal.Sixel.is_sixel_supported()
@testset "sixel" begin
old_enc = ImageInTerminal.ENCODER_BACKEND[]
_old_enc = ImageInTerminal.ENCODER_BACKEND[]
ImageInTerminal.ENCODER_BACKEND[] = :Sixel
p = imageplot(img, title = "sixel")
@test p.graphics.sixel[]
test_ref("imageplot/img_sixel.txt", @show_col(p, :displaysize => T_SZ))
ImageInTerminal.ENCODER_BACKEND[] = old_enc
end

@testset "blocks" begin
old_enc = ImageInTerminal.ENCODER_BACKEND[]
ImageInTerminal.ENCODER_BACKEND[] = :XTermColors
p = imageplot(img, title = "blocks")
@test !p.graphics.sixel[]
test_ref("imageplot/img_blocks.txt", @show_col(p, :displaysize => T_SZ))
ImageInTerminal.ENCODER_BACKEND[] = old_enc
@test p.graphics.sixel[]
ImageInTerminal.ENCODER_BACKEND[] = _old_enc
end
end

0 comments on commit 4b37961

Please sign in to comment.