Skip to content

Commit 1284eaa

Browse files
committed
allow direct encoding to io
1 parent caf4e34 commit 1284eaa

File tree

2 files changed

+55
-40
lines changed

2 files changed

+55
-40
lines changed

AsciiPixel/src/ascii_encode.jl

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ struct SmallBlocks <: ImageEncoder
66
size::NTuple{2, Int}
77
end
88

9+
const RESET = Crayon(reset = true)
910
const alpha_chars = ('', '', '', '', '')
11+
1012
function _charof(alpha)
1113
idx = round(Int, alpha * (length(alpha_chars)-1))
1214
alpha_chars[clamp(idx + 1, 1, length(alpha_chars))]
@@ -78,13 +80,15 @@ The function returns a tuple with three elements:
7880
"""
7981

8082
function ascii_encode(
81-
enc::SmallBlocks,
82-
colordepth::TermColorDepth,
83-
img::AbstractMatrix{<:Colorant})
83+
io::IO,
84+
::SmallBlocks,
85+
colordepth::TermColorDepth,
86+
img::AbstractMatrix{<:Colorant};
87+
ret::Bool = false
88+
)
8489
yinds, xinds = axes(img)
85-
io = PipeBuffer()
8690
for y in first(yinds):2:last(yinds)
87-
print(io, Crayon(reset = true))
91+
print(io, RESET)
8892
for x in xinds
8993
fgcol = _colorant2ansi(img[y, x], colordepth)
9094
bgcol = if y+1 <= last(yinds)
@@ -96,74 +100,87 @@ function ascii_encode(
96100
end
97101
print(io, Crayon(foreground=fgcol, background=bgcol), "")
98102
end
99-
println(io, Crayon(reset = true))
103+
println(io, RESET)
100104
end
101-
readlines(io)
105+
ret ? readlines(io) : nothing
102106
end
103107

104108
function ascii_encode(
105-
enc::BigBlocks,
106-
colordepth::TermColorDepth,
107-
img::AbstractMatrix{<:Colorant})
109+
io::IO,
110+
::BigBlocks,
111+
colordepth::TermColorDepth,
112+
img::AbstractMatrix{<:Colorant};
113+
ret::Bool = false
114+
)
108115
yinds, xinds = axes(img)
109-
io = PipeBuffer()
110116
for y in yinds
111-
print(io, Crayon(reset = true))
117+
print(io, RESET)
112118
for x in xinds
113119
color = img[y, x]
114120
fgcol = _colorant2ansi(color, colordepth)
115121
chr = _charof(alpha(color))
116122
print(io, Crayon(foreground = fgcol), chr, chr)
117123
end
118-
println(io, Crayon(reset = true))
124+
println(io, RESET)
119125
end
120-
readlines(io)
126+
ret ? readlines(io) : nothing
121127
end
122128

123129
function ascii_encode(
124-
enc::SmallBlocks,
125-
colordepth::TermColorDepth,
126-
img::AbstractVector{<:Colorant})
127-
io = PipeBuffer()
128-
print(io, Crayon(reset = true))
130+
io::IO,
131+
::SmallBlocks,
132+
colordepth::TermColorDepth,
133+
img::AbstractVector{<:Colorant};
134+
ret::Bool = false
135+
)
136+
print(io, RESET)
129137
for i in axes(img, 1)
130138
color = img[i]
131139
fgcol = _colorant2ansi(color, colordepth)
132140
chr = _charof(alpha(color))
133141
print(io, Crayon(foreground = fgcol), chr)
134142
end
135-
println(io, Crayon(reset = true))
136-
readlines(io)
143+
print(io, RESET)
144+
ret ? readlines(io) : nothing
137145
end
138146

139147
function ascii_encode(
140-
enc::BigBlocks,
141-
colordepth::TermColorDepth,
142-
img::AbstractVector{<:Colorant})
148+
io::IO,
149+
enc::BigBlocks,
150+
colordepth::TermColorDepth,
151+
img::AbstractVector{<:Colorant};
152+
ret::Bool = false
153+
)
143154
w = length(img)
144155
n = enc.size[2] ÷ 3 == w ? w : enc.size[2] ÷ 6
145-
io = PipeBuffer()
146156
# left or full
147-
print(io, Crayon(reset = true))
157+
print(io, RESET)
148158
for i in (0:n-1) .+ firstindex(img)
149159
color = img[i]
150160
fgcol = _colorant2ansi(color, colordepth)
151161
chr = _charof(alpha(color))
152162
print(io, Crayon(foreground = fgcol), chr, chr, " ")
153163
end
154164
if n < w # right part
155-
print(io, Crayon(reset = true), "")
165+
print(io, RESET, "")
156166
for i in (-n+1:0) .+ lastindex(img)
157167
color = img[i]
158168
fgcol = _colorant2ansi(color, colordepth)
159169
chr = _charof(alpha(color))
160170
print(io, Crayon(foreground = fgcol), chr, chr, " ")
161171
end
162172
end
163-
println(io, Crayon(reset = true))
164-
readlines(io)
173+
print(io, RESET)
174+
ret ? readlines(io) : nothing
165175
end
166176

177+
ascii_encode(enc::SmallBlocks, args...) =
178+
ascii_encode(PipeBuffer(), enc, args...; ret=true)
179+
180+
ascii_encode(enc::BigBlocks, args...) =
181+
ascii_encode(PipeBuffer(), enc, args...; ret=true)
182+
183+
167184
"""
168185
ascii_display([stream], img, [depth::TermColorDepth], [maxsize])
169186
@@ -189,11 +206,8 @@ function ascii_display(
189206
img_h, img_w = map(length, axes(img))
190207
scale = img_h <= io_h - 4 && 2img_w <= io_w ? downscale_big : downscale_small
191208
img, enc = scale(img, io_h - 4, io_w)
192-
str = ascii_encode(enc, colordepth, img)
193-
for (idx, line) in enumerate(str)
194-
print(io, line)
195-
idx < length(str) && println(io)
196-
end
209+
ascii_encode(io, enc, colordepth, img)
210+
io
197211
end
198212

199213
# colorant vector
@@ -206,11 +220,8 @@ function ascii_display(
206220
img_w = length(img)
207221
scale = 3img_w <= io_w ? downscale_big : downscale_small
208222
img, enc = scale(img, io_w)
209-
str = ascii_encode(enc, colordepth, img)
210-
for (idx, line) in enumerate(str)
211-
print(io, line)
212-
idx < length(str) && println(io)
213-
end
223+
ascii_encode(io, enc, colordepth, img)
224+
io
214225
end
215226

216227
ascii_display(io::IO, img::AbstractArray{<:Colorant}) = ascii_display(io, img, colormode[])

src/ImageInTerminal.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ function imshow(
9595
sixel_encode(io, img)
9696
else
9797
if ndims(img) > 2
98-
Base.show_nd(io, img, (io, x) -> ascii_display(io, x), true)
98+
print_matrix(io, x) = begin
99+
bytes = read(ascii_display(PipeBuffer(), x))
100+
write(io, bytes[1:(end-1)]) # remove traling Char(0x0a): '\n'
101+
end
102+
Base.show_nd(io, img, print_matrix, true)
99103
else
100104
ascii_display(io, img)
101105
end

0 commit comments

Comments
 (0)