Skip to content

Commit e28ed63

Browse files
committed
move imshow to AsciiPixel
1 parent 5ecde36 commit e28ed63

26 files changed

+387
-308
lines changed

AsciiPixel/Project.toml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,24 @@ version = "0.1.0"
66
Crayons = "a8cc5b0e-0ffa-5ad4-8c14-923d3ee1735f"
77
ImageBase = "c817782e-172a-44cc-b673-b171935fbb9e"
88
ImageCore = "a09fc81d-aa75-5fe9-8630-4744c3626534"
9-
ImageIO = "82e4d734-157c-48bb-816b-45c225c6df19"
109

1110
[compat]
1211
Crayons = "0.5, 1, 2, 3, 4"
1312
ImageBase = "0.1"
1413
ImageCore = "0.9"
15-
ImageIO = "0.5"
1614
julia = "1.6"
1715

1816
[extras]
17+
CoordinateTransformations = "150eb455-5306-5404-9cee-2592286d6298"
18+
ImageIO = "82e4d734-157c-48bb-816b-45c225c6df19"
1919
ImageMagick = "6218d12a-5da1-5696-b52f-db25d2ecc6d1"
20+
ImageTransformations = "02fcd773-0e25-5acc-982a-7f6622650795"
2021
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
2122
ReferenceTests = "324d217c-45ce-50fc-942e-d289b448e8cf"
23+
Rotations = "6038ab10-8711-5258-84ad-4b1120ba62dc"
24+
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
2225
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2326
TestImages = "5e47fb64-e119-507b-a336-dd2b206d9990"
2427

2528
[targets]
26-
test = ["ImageMagick", "OffsetArrays", "ReferenceTests", "Test", "TestImages"]
29+
test = ["CoordinateTransformations", "ImageIO", "ImageMagick", "ImageTransformations", "OffsetArrays", "Rotations", "ReferenceTests", "SparseArrays", "Test", "TestImages"]

AsciiPixel/src/AsciiPixel.jl

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,28 @@ using ImageBase: restrict
44
using ImageCore
55
using Crayons
66

7-
export TermColorDepth, TermColor256, TermColor24bit, ascii_encode
7+
export ascii_encode
88

99
include("colorant2ansi.jl")
1010
include("ascii_encode.jl")
1111

12+
const colormode = Ref{TermColorDepth}(TermColor256())
13+
14+
"""
15+
use_256()
16+
17+
Triggers `ascii_encode256` (256 colors, 8bit) automatically if an array of colorants is to
18+
be displayed in the julia REPL. (This is the default)
19+
"""
20+
use_256() = (colormode[] = TermColor256())
21+
22+
"""
23+
use_24bit()
24+
25+
Triggers `ascii_encode24bit` automatically if an array of colorants is to
26+
be displayed in the julia REPL.
27+
Call `AsciiPixel.use_256()` to restore default behaviour.
28+
"""
29+
use_24bit() = (colormode[] = TermColor24bit())
30+
1231
end # module

AsciiPixel/src/ascii_encode.jl

Lines changed: 98 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,6 @@ The function returns a tuple with three elements:
3636
3. 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 && 2img_w <= maxwidth ? BigBlocks : SmallBlocks
47-
ascii_encode(enc(), colordepth, img, maxheight)
48-
end
49-
5039
function ascii_encode(
5140
::SmallBlocks,
5241
colordepth::TermColorDepth,
@@ -108,16 +97,6 @@ function ascii_encode(
10897
replace.(readlines(io), Ref("\n" => ""))::Vector{String}, h, 2w
10998
end
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 = 3img_w <= maxwidth ? BigBlocks : SmallBlocks
118-
ascii_encode(enc(), colordepth, img, maxwidth)
119-
end
120-
121100
function 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)) : 3w
170149
end
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 && 2img_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 = 3img_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...)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
2+
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
3+
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
4+
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
5+
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
6+
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
7+
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
8+
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
9+
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
10+
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
11+
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
12+
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
13+
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
14+
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
15+
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
████████████████████
2+
████████████████████
3+
████████████████████
4+
████████████████████
5+
████████████████████
6+
████████████████████
7+
████████████████████
8+
████████████████████
9+
████████████████████
10+
████████████████████

0 commit comments

Comments
 (0)