11abstract type ImageEncoder end
2- struct BigBlocks <: ImageEncoder end
3- struct SmallBlocks <: ImageEncoder end
2+ struct BigBlocks <: ImageEncoder
3+ size:: NTuple{2, Int}
4+ end
5+ struct SmallBlocks <: ImageEncoder
6+ size:: NTuple{2, Int}
7+ end
48
59const alpha_chars = (' ⋅' , ' ░' , ' ▒' , ' ▓' , ' █' )
610function _charof (alpha)
711 idx = round (Int, alpha * (length (alpha_chars)- 1 ))
812 alpha_chars[clamp (idx + 1 , 1 , length (alpha_chars))]
913end
1014
11- function downscale ( :: SmallBlocks , img:: AbstractMatrix{<:Colorant} , maxheight:: Int , maxwidth:: Int )
15+ function downscale_small ( img:: AbstractMatrix{<:Colorant} , maxheight:: Int , maxwidth:: Int )
1216 maxheight = max (maxheight, 5 )
1317 maxwidth = max (maxwidth, 5 )
1418 h, w = map (length, axes (img))
1519 while ceil (h/ 2 ) > maxheight || w > maxwidth
1620 img = restrict (img)
1721 h, w = map (length, axes (img))
1822 end
19- img, length (1 : 2 : h), w
23+ img, SmallBlocks (( length (1 : 2 : h), w))
2024end
2125
22- function downscale ( :: BigBlocks , img:: AbstractMatrix{<:Colorant} , maxheight:: Int , maxwidth:: Int )
26+ function downscale_big ( img:: AbstractMatrix{<:Colorant} , maxheight:: Int , maxwidth:: Int )
2327 maxheight = max (maxheight, 5 )
2428 maxwidth = max (maxwidth, 5 )
2529 h, w = map (length, axes (img))
2630 while h > maxheight || 2 w > maxwidth
2731 img = restrict (img)
2832 h, w = map (length, axes (img))
2933 end
30- img, h, 2 w
34+ img, BigBlocks (( h, 2 w))
3135end
3236
33- function downscale ( :: SmallBlocks , img:: AbstractVector{<:Colorant} , maxwidth:: Int )
37+ function downscale_small ( img:: AbstractVector{<:Colorant} , maxwidth:: Int )
3438 maxwidth = max (maxwidth, 5 )
3539 while length (img) > maxwidth
3640 img = restrict (img)
3741 end
38- img, 1 , length (img)
42+ img, SmallBlocks (( 1 , length (img)) )
3943end
4044
41- function downscale ( :: BigBlocks , img:: AbstractVector{<:Colorant} , maxwidth:: Int )
45+ function downscale_big ( img:: AbstractVector{<:Colorant} , maxwidth:: Int )
4246 maxwidth = max (maxwidth, 5 )
4347 w = length (img)
4448 n = 3 w > maxwidth ? maxwidth ÷ 6 : w
45- return img, 1 , n < w ? 3 (2 n + 1 ) : 3 w
49+ return img, BigBlocks (( 1 , n < w ? 3 (2 n + 1 ) : 3 w))
4650end
4751
4852"""
@@ -76,11 +80,7 @@ The function returns a tuple with three elements:
7680function ascii_encode (
7781 enc:: SmallBlocks ,
7882 colordepth:: TermColorDepth ,
79- img:: AbstractMatrix{<:Colorant} ,
80- maxheight:: Int = 50 ,
81- maxwidth:: Int = 80 )
82- img, hh, ww = downscale (enc, img, maxheight, maxwidth)
83- h, w = map (length, axes (img))
83+ img:: AbstractMatrix{<:Colorant} )
8484 yinds, xinds = axes (img)
8585 io = PipeBuffer ()
8686 for y in first (yinds): 2 : last (yinds)
@@ -98,17 +98,13 @@ function ascii_encode(
9898 end
9999 println (io, Crayon (reset = true ))
100100 end
101- readlines (io), hh, ww
101+ readlines (io)
102102end
103103
104104function ascii_encode (
105105 enc:: BigBlocks ,
106106 colordepth:: TermColorDepth ,
107- img:: AbstractMatrix{<:Colorant} ,
108- maxheight:: Int = 50 ,
109- maxwidth:: Int = 80 )
110- img, hh, ww = downscale (enc, img, maxheight, maxwidth)
111- h, w = map (length, axes (img))
107+ img:: AbstractMatrix{<:Colorant} )
112108 yinds, xinds = axes (img)
113109 io = PipeBuffer ()
114110 for y in yinds
@@ -121,15 +117,13 @@ function ascii_encode(
121117 end
122118 println (io, Crayon (reset = true ))
123119 end
124- readlines (io), hh, ww
120+ readlines (io)
125121end
126122
127123function ascii_encode (
128124 enc:: SmallBlocks ,
129125 colordepth:: TermColorDepth ,
130- img:: AbstractVector{<:Colorant} ,
131- maxwidth:: Int = 80 )
132- img, hh, ww = downscale (enc, img, maxwidth)
126+ img:: AbstractVector{<:Colorant} )
133127 io = PipeBuffer ()
134128 print (io, Crayon (reset = true ))
135129 for i in axes (img, 1 )
@@ -139,17 +133,15 @@ function ascii_encode(
139133 print (io, Crayon (foreground = fgcol), chr)
140134 end
141135 println (io, Crayon (reset = true ))
142- readlines (io), hh, ww
136+ readlines (io)
143137end
144138
145139function ascii_encode (
146140 enc:: BigBlocks ,
147141 colordepth:: TermColorDepth ,
148- img:: AbstractVector{<:Colorant} ,
149- maxwidth:: Int = 80 )
150- img, hh, ww = downscale (enc, img, maxwidth)
142+ img:: AbstractVector{<:Colorant} )
151143 w = length (img)
152- n = ww ÷ 3 == w ? w : ww ÷ 6
144+ n = enc . size[ 2 ] ÷ 3 == w ? w : enc . size[ 2 ] ÷ 6
153145 io = PipeBuffer ()
154146 # left or full
155147 print (io, Crayon (reset = true ))
@@ -169,7 +161,7 @@ function ascii_encode(
169161 end
170162 end
171163 println (io, Crayon (reset = true ))
172- readlines (io), hh, ww
164+ readlines (io)
173165end
174166
175167"""
@@ -195,12 +187,9 @@ function ascii_display(
195187 maxsize:: Tuple = displaysize (io))
196188 io_h, io_w = maxsize
197189 img_h, img_w = map (length, axes (img))
198- enc = img_h <= io_h - 4 && 2 img_w <= io_w ? BigBlocks () : SmallBlocks ()
199- # @sync for row in img_rows
200- # buffer = ascii_encode(enc, colordepth, img[row, :], io_w)
201- # @async print(io, buffer)
202- # end
203- str, = ascii_encode (enc, colordepth, img, io_h - 4 , io_w)
190+ scale = img_h <= io_h - 4 && 2 img_w <= io_w ? downscale_big : downscale_small
191+ img, enc = scale (img, io_h - 4 , io_w)
192+ str = ascii_encode (enc, colordepth, img)
204193 for (idx, line) in enumerate (str)
205194 print (io, line)
206195 idx < length (str) && println (io)
@@ -215,8 +204,9 @@ function ascii_display(
215204 maxsize:: Tuple = displaysize (io))
216205 io_h, io_w = maxsize
217206 img_w = length (img)
218- enc = 3 img_w <= io_w ? BigBlocks () : SmallBlocks ()
219- str, = ascii_encode (enc, colordepth, img, io_w)
207+ scale = 3 img_w <= io_w ? downscale_big : downscale_small
208+ img, enc = scale (img, io_w)
209+ str = ascii_encode (enc, colordepth, img)
220210 for (idx, line) in enumerate (str)
221211 print (io, line)
222212 idx < length (str) && println (io)
0 commit comments