1
1
abstract type ImageEncoder end
2
2
struct BigBlocks <: ImageEncoder
3
- size:: NTuple{2, Int}
3
+ size:: NTuple{2,Int}
4
4
end
5
5
struct SmallBlocks <: ImageEncoder
6
- size:: NTuple{2, Int}
6
+ size:: NTuple{2,Int}
7
7
end
8
8
9
- const RESET = Crayon (reset = true )
9
+ const RESET = Crayon (; reset = true )
10
10
const alpha_chars = (' ⋅' , ' ░' , ' ▒' , ' ▓' , ' █' )
11
11
12
12
function _charof (alpha)
@@ -91,22 +91,22 @@ function ascii_encode(
91
91
:: SmallBlocks ,
92
92
colordepth:: TermColorDepth ,
93
93
img:: AbstractMatrix{<:Colorant} ;
94
- trail_nl:: Bool = false ,
95
- ret:: Bool = false
94
+ trail_nl:: Bool = false ,
95
+ ret:: Bool = false ,
96
96
)
97
97
yinds, xinds = axes (img)
98
98
for y in first (yinds): 2 : last (yinds)
99
99
_printc (io, RESET)
100
100
for x in xinds
101
101
fgcol = _colorant2ansi (img[y, x], colordepth)
102
- bgcol = if y+ 1 <= last (yinds)
103
- _colorant2ansi (img[y+ 1 , x], colordepth)
102
+ bgcol = if y + 1 <= last (yinds)
103
+ _colorant2ansi (img[y + 1 , x], colordepth)
104
104
else
105
105
# if reached it means that the last character row
106
106
# has only the upper pixel defined.
107
107
nothing
108
108
end
109
- _printc (io, Crayon (foreground= fgcol, background= bgcol), " ▀" )
109
+ _printc (io, Crayon (; foreground= fgcol, background= bgcol), " ▀" )
110
110
end
111
111
_printc (io, RESET)
112
112
(trail_nl || y < last (yinds)) && println (io)
@@ -119,8 +119,8 @@ function ascii_encode(
119
119
:: BigBlocks ,
120
120
colordepth:: TermColorDepth ,
121
121
img:: AbstractMatrix{<:Colorant} ;
122
- trail_nl:: Bool = false ,
123
- ret:: Bool = false ,
122
+ trail_nl:: Bool = false ,
123
+ ret:: Bool = false ,
124
124
)
125
125
yinds, xinds = axes (img)
126
126
for y in yinds
@@ -129,7 +129,7 @@ function ascii_encode(
129
129
color = img[y, x]
130
130
fgcol = _colorant2ansi (color, colordepth)
131
131
chr = _charof (alpha (color))
132
- _printc (io, Crayon (foreground = fgcol), chr, chr)
132
+ _printc (io, Crayon (; foreground = fgcol), chr, chr)
133
133
end
134
134
_printc (io, RESET)
135
135
(trail_nl || y < last (yinds)) && println (io)
@@ -142,15 +142,15 @@ function ascii_encode(
142
142
:: SmallBlocks ,
143
143
colordepth:: TermColorDepth ,
144
144
img:: AbstractVector{<:Colorant} ;
145
- trail_nl:: Bool = false ,
146
- ret:: Bool = false
145
+ trail_nl:: Bool = false ,
146
+ ret:: Bool = false ,
147
147
)
148
148
_printc (io, RESET)
149
149
for i in axes (img, 1 )
150
150
color = img[i]
151
151
fgcol = _colorant2ansi (color, colordepth)
152
152
chr = _charof (alpha (color))
153
- _printc (io, Crayon (foreground = fgcol), chr)
153
+ _printc (io, Crayon (; foreground = fgcol), chr)
154
154
end
155
155
_printc (io, RESET)
156
156
trail_nl && println (io)
@@ -162,26 +162,26 @@ function ascii_encode(
162
162
enc:: BigBlocks ,
163
163
colordepth:: TermColorDepth ,
164
164
img:: AbstractVector{<:Colorant} ;
165
- trail_nl:: Bool = false ,
166
- ret:: Bool = false
165
+ trail_nl:: Bool = false ,
166
+ ret:: Bool = false ,
167
167
)
168
168
w = length (img)
169
169
n = enc. size[2 ] ÷ 3 == w ? w : enc. size[2 ] ÷ 6
170
170
# left or full
171
171
_printc (io, RESET)
172
- for i in (0 : n - 1 ) .+ firstindex (img)
172
+ for i in (0 : (n - 1 ) ) .+ firstindex (img)
173
173
color = img[i]
174
174
fgcol = _colorant2ansi (color, colordepth)
175
175
chr = _charof (alpha (color))
176
- _printc (io, Crayon (foreground = fgcol), chr, chr, " " )
176
+ _printc (io, Crayon (; foreground = fgcol), chr, chr, " " )
177
177
end
178
178
if n < w # right part
179
179
_printc (io, RESET, " … " )
180
- for i in (- n + 1 : 0 ) .+ lastindex (img)
180
+ for i in (( - n + 1 ) : 0 ) .+ lastindex (img)
181
181
color = img[i]
182
182
fgcol = _colorant2ansi (color, colordepth)
183
183
chr = _charof (alpha (color))
184
- _printc (io, Crayon (foreground = fgcol), chr, chr, " " )
184
+ _printc (io, Crayon (; foreground = fgcol), chr, chr, " " )
185
185
end
186
186
end
187
187
_printc (io, RESET)
@@ -190,11 +190,9 @@ function ascii_encode(
190
190
end
191
191
192
192
# use a `PipeBuffer` as io and returns encoded data reading lines of this buffer (using `readlines(io)`)
193
- ascii_encode (enc:: SmallBlocks , args... ) =
194
- ascii_encode (PipeBuffer (), enc, args... ; ret= true )
193
+ ascii_encode (enc:: SmallBlocks , args... ) = ascii_encode (PipeBuffer (), enc, args... ; ret= true )
195
194
196
- ascii_encode (enc:: BigBlocks , args... ) =
197
- ascii_encode (PipeBuffer (), enc, args... ; ret= true )
195
+ ascii_encode (enc:: BigBlocks , args... ) = ascii_encode (PipeBuffer (), enc, args... ; ret= true )
198
196
199
197
"""
200
198
ascii_display([stream], img, [depth::TermColorDepth], [maxsize])
@@ -214,8 +212,8 @@ function ascii_display(
214
212
io:: IO ,
215
213
img:: AbstractMatrix{<:Colorant} ,
216
214
colordepth:: TermColorDepth ,
217
- maxsize:: Tuple = displaysize (io);
218
- kwargs...
215
+ maxsize:: Tuple = displaysize (io);
216
+ kwargs... ,
219
217
)
220
218
io_h, io_w = maxsize
221
219
img_h, img_w = map (length, axes (img))
@@ -229,8 +227,8 @@ function ascii_display(
229
227
io:: IO ,
230
228
img:: AbstractVector{<:Colorant} ,
231
229
colordepth:: TermColorDepth ,
232
- maxsize:: Tuple = displaysize (io);
233
- kwargs...
230
+ maxsize:: Tuple = displaysize (io);
231
+ kwargs... ,
234
232
)
235
233
io_h, io_w = maxsize
236
234
img_w = length (img)
0 commit comments