@@ -36,17 +36,6 @@ The function returns a tuple with three elements:
36363. Number of visible characters per line (the remaining are colorcodes).
3737"""
3838
39- # colorant matrix
40- function ascii_encode (
41- colordepth:: TermColorDepth ,
42- img:: AbstractMatrix{<:Colorant} ,
43- maxheight:: Int = 50 ,
44- maxwidth:: Int = 80 )
45- img_h, img_w = map (length, axes (img))
46- enc = img_h <= maxheight && 2 img_w <= maxwidth ? BigBlocks : SmallBlocks
47- ascii_encode (enc (), colordepth, img, maxheight)
48- end
49-
5039function ascii_encode (
5140 :: SmallBlocks ,
5241 colordepth:: TermColorDepth ,
@@ -108,16 +97,6 @@ function ascii_encode(
10897 replace .(readlines (io), Ref (" \n " => " " )):: Vector{String} , h, 2 w
10998end
11099
111- # colorant vector
112- function ascii_encode (
113- colordepth:: TermColorDepth ,
114- img:: AbstractVector{<:Colorant} ,
115- maxwidth:: Int = 80 )
116- img_w = length (img)
117- enc = 3 img_w <= maxwidth ? BigBlocks : SmallBlocks
118- ascii_encode (enc (), colordepth, img, maxwidth)
119- end
120-
121100function ascii_encode (
122101 :: SmallBlocks ,
123102 colordepth:: TermColorDepth ,
@@ -168,3 +147,101 @@ function ascii_encode(
168147 println (io, Crayon (reset = true ))
169148 replace .(readlines (io), Ref (" \n " => " " )):: Vector{String} , 1 , n < w ? 3 (length (1 : n) + 1 + length (w- n+ 1 : w)) : 3 w
170149end
150+
151+
152+ """
153+ ascii_encode([stream], img, [depth::TermColorDepth], [maxsize])
154+
155+ Displays the given image `img` using unicode characters and
156+ terminal colors (defaults to 256 colors).
157+ `img` has to be an array of `Colorant`.
158+
159+ If working in the REPL, the function tries to choose the encoding
160+ based on the current display size. The image will also be
161+ downsampled to fit into the display (using `restrict`).
162+ """
163+
164+ # colorant matrix
165+ function ascii_encode (
166+ io:: IO ,
167+ img:: AbstractMatrix{<:Colorant} ,
168+ colordepth:: TermColorDepth ,
169+ maxsize:: Tuple = displaysize (io))
170+ io_h, io_w = maxsize
171+ img_h, img_w = map (length, axes (img))
172+ enc = img_h <= io_h - 4 && 2 img_w <= io_w ? BigBlocks : SmallBlocks
173+ str = first (ascii_encode (enc (), colordepth, img, io_h - 4 , io_w))
174+ for (idx, line) in enumerate (str)
175+ print (io, line)
176+ idx < length (str) && println (io)
177+ end
178+ end
179+
180+ # colorant vector
181+ function ascii_encode (
182+ io:: IO ,
183+ img:: AbstractVector{<:Colorant} ,
184+ colordepth:: TermColorDepth ,
185+ maxsize:: Tuple = displaysize (io))
186+ io_h, io_w = maxsize
187+ img_w = length (img)
188+ enc = 3 img_w <= io_w ? BigBlocks : SmallBlocks
189+ str = first (ascii_encode (enc (), colordepth, img, io_w))
190+ for (idx, line) in enumerate (str)
191+ print (io, line)
192+ idx < length (str) && println (io)
193+ end
194+ end
195+
196+ """
197+ ascii_encode([stream], img, [depth::TermColorDepth], [maxsize])
198+
199+ Displays the given image `img` using unicode characters and
200+ terminal colors (defaults to 256 colors).
201+ `img` has to be an array of `Colorant`.
202+
203+ If working in the REPL, the function tries to choose the encoding
204+ based on the current display size. The image will also be
205+ downsampled to fit into the display (using `restrict`).
206+ """
207+ function ascii_encode (
208+ io:: IO ,
209+ img:: AbstractArray{<:Colorant} ,
210+ colordepth:: TermColorDepth ,
211+ maxsize:: Tuple = displaysize (io))
212+ # otherwise, use our own implementation
213+ print_matrix (io, x) = ascii_encode (io, x, colordepth, maxsize)
214+ Base. show_nd (io, img, print_matrix, true )
215+ end
216+
217+ ascii_encode (io:: IO , img, args... ) = ascii_encode (io, img, colormode[], args... )
218+ ascii_encode (img, args... ) = ascii_encode (stdout , img, colormode[], args... )
219+ ascii_encode (io:: IO , img, colordepth:: TermColorDepth , args... ) = throw (ArgumentError (" imshow only supports colorant arrays with 1 or 2 dimensions" ))
220+
221+ """
222+ ascii_encode256([stream], img, [maxsize])
223+
224+ Displays the given image `img` using unicode characters and
225+ the widely supported 256 terminal colors.
226+ `img` has to be an array of `Colorant`.
227+
228+ If working in the REPL, the function tries to choose the encoding
229+ based on the current display size. The image will also be
230+ downsampled to fit into the display (using `restrict`).
231+ """
232+ ascii_encode256 (io:: IO , img, args... ) = ascii_encode (io, img, TermColor256 (), args... )
233+ ascii_encode256 (img, args... ) = ascii_encode256 (stdout , img, args... )
234+
235+ """
236+ ascii_encode24bit([stream], img, [maxsize])
237+
238+ Displays the given image `img` using unicode characters and
239+ the 24 terminal colors that some modern terminals support.
240+ `img` has to be an array of `Colorant`.
241+
242+ If working in the REPL, the function tries to choose the encoding
243+ based on the current display size. The image will also be
244+ downsampled to fit into the display (using `restrict`).
245+ """
246+ ascii_encode24bit (io:: IO , img, args... ) = ascii_encode (io, img, TermColor24bit (), args... )
247+ ascii_encode24bit (img, args... ) = ascii_encode24bit (stdout , img, args... )
0 commit comments