Skip to content

Commit f83d17d

Browse files
authored
Merge pull request #9 from JuliaImages/teh/non1
Fix display of images with non-1 indices
2 parents 9d134d5 + 7900671 commit f83d17d

File tree

10 files changed

+90
-20
lines changed

10 files changed

+90
-20
lines changed

REQUIRE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ julia 0.5
22
Crayons 0.1.0
33
ColorTypes 0.3.2
44
ImageCore 0.1.1
5-
ImageTransformations 0.1.0
5+
ImageTransformations 0.2.1
66
Compat 0.17

appveyor.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ matrix:
2323
- JULIAVERSION: "julianightlies/bin/winnt/x86/julia-latest-win32.exe"
2424
- JULIAVERSION: "julianightlies/bin/winnt/x64/julia-latest-win64.exe"
2525

26+
build: off
27+
2628
install:
2729
# Download most recent Julia Windows binary
2830
- ps: (new-object net.webclient).DownloadFile(

src/encodeimg.jl

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,18 @@ function encodeimg{C<:Colorant}(
4444
maxwidth::Int = 80)
4545
maxheight = max(maxheight, 5)
4646
maxwidth = max(maxwidth, 5)
47-
h, w = size(img)
47+
h, w = map(length, indices(img))
4848
while ceil(h/2) > maxheight || w > maxwidth
4949
img = restrict(img)
50-
h, w = size(img)
50+
h, w = map(length, indices(img))
5151
end
52+
yinds, xinds = indices(img)
5253
io = IOBuffer()
53-
for y in 1:2:h
54+
for y in first(yinds):2:last(yinds)
5455
print(io, Crayon(reset = true))
55-
for x in 1:w
56+
for x in xinds
5657
fgcol = _colorant2ansi(img[y,x], colordepth)
57-
bgcol = if y+1 <= h
58+
bgcol = if y+1 <= last(yinds)
5859
_colorant2ansi(img[y+1,x], colordepth)
5960
else
6061
# if reached it means that the last character row
@@ -76,15 +77,16 @@ function encodeimg{C<:Colorant}(
7677
maxwidth::Int = 80)
7778
maxheight = max(maxheight, 5)
7879
maxwidth = max(maxwidth, 5)
79-
h, w = size(img)
80+
h, w = map(length, indices(img))
8081
while h > maxheight || 2w > maxwidth
8182
img = restrict(img)
82-
h, w = size(img)
83+
h, w = map(length, indices(img))
8384
end
85+
yinds, xinds = indices(img)
8486
io = IOBuffer()
85-
for y in 1:h
87+
for y in yinds
8688
print(io, Crayon(reset = true))
87-
for x in 1:w
89+
for x in xinds
8890
color = img[y,x]
8991
fgcol = _colorant2ansi(color, colordepth)
9092
chr = _charof(alpha(color))
@@ -102,14 +104,14 @@ function encodeimg{C<:Colorant}(
102104
img::AbstractVector{C},
103105
maxwidth::Int = 80)
104106
maxwidth = max(maxwidth, 5)
105-
w = length(img)
107+
w = length(indices(img, 1))
106108
if w > maxwidth
107109
img = imresize(img, maxwidth)
108-
w = length(img)
110+
w = length(indices(img, 1))
109111
end
110112
io = IOBuffer()
111113
print(io, Crayon(reset = true))
112-
for i in 1:w
114+
for i in indices(img, 1)
113115
color = img[i]
114116
fgcol = _colorant2ansi(color, colordepth)
115117
chr = _charof(alpha(color))
@@ -125,19 +127,20 @@ function encodeimg{C<:Colorant}(
125127
img::AbstractVector{C},
126128
maxwidth::Int = 80)
127129
maxwidth = max(maxwidth, 5)
128-
w = length(img)
130+
inds = indices(img, 1)
131+
w = length(inds)
129132
n = 3w > maxwidth ? floor(Int,maxwidth/6) : w
130133
io = IOBuffer()
131134
print(io, Crayon(reset = true))
132-
for i in 1:n
135+
for i in (0:n-1)+first(inds)
133136
color = img[i]
134137
fgcol = _colorant2ansi(color, colordepth)
135138
chr = _charof(alpha(color))
136139
print(io, Crayon(foreground = fgcol), chr, chr, " ")
137140
end
138141
if n < w
139142
print(io, Crayon(reset = true), "")
140-
for i in w-n+1:w
143+
for i in last(inds)-n+1:last(inds)
141144
color = img[i]
142145
fgcol = _colorant2ansi(color, colordepth)
143146
chr = _charof(alpha(color))
@@ -147,4 +150,3 @@ function encodeimg{C<:Colorant}(
147150
println(io, Crayon(reset = true))
148151
replace.(readlines(seek(io,0)), ["\n"], [""])::Vector{String}, 1, n < w ? 3*(length(1:n) + 1 + length(w-n+1:w)) : 3w
149152
end
150-

src/imshow.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function imshow{C<:Colorant}(
1515
colordepth::TermColorDepth,
1616
maxsize::Tuple = displaysize(io))
1717
io_h, io_w = maxsize
18-
img_h, img_w = size(img)
18+
img_h, img_w = map(length, indices(img))
1919
str = if img_h <= io_h-4 && 2img_w <= io_w
2020
first(encodeimg(BigBlocks(), colordepth, img, io_h-4, io_w))
2121
else
@@ -76,4 +76,3 @@ downsampled to fit into the display (using `restrict`).
7676
"""
7777
imshow24bit(io::IO, img, args...) = imshow(io, img, TermColor24bit(), args...)
7878
imshow24bit(img, args...) = imshow24bit(STDOUT, img, args...)
79-

test/REQUIRE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
FixedPointNumbers 0.3
22
TestImages
3+
OffsetArrays
4+
CoordinateTransformations
5+
Rotations
36
@windows ImageMagick
47
@linux ImageMagick
58
@osx QuartzImageIO
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+
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using ImageInTerminal, ImageCore, ColorTypes, FixedPointNumbers, TestImages, ImageTransformations
1+
using ImageInTerminal, ImageCore, ColorTypes, FixedPointNumbers, TestImages, ImageTransformations, CoordinateTransformations, Rotations, OffsetArrays
22
using Base.Test
33

44
reference_path(filename) = joinpath(dirname(@__FILE__), "reference", "$(filename).txt")

test/tst_baseshow.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ end
3434
@test_reference "colorant_show_24bit" res
3535
end
3636

37+
ImageInTerminal.use_256()

test/tst_encodeimg.jl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,3 +365,34 @@ end
365365
end
366366
end
367367

368+
@testset "non-1 indexing" begin
369+
@testset "rgb line" begin
370+
res, h, w = @inferred ImageInTerminal.encodeimg(ImageInTerminal.SmallBlocks(), ImageInTerminal.TermColor256(), OffsetArray(rgb_line, (-1,)), 8)
371+
@test typeof(res) <: Vector{String}
372+
@test h === 1
373+
@test w === 8
374+
@test length(res) === 1
375+
@test res[1] == "\e[0m\e[38;5;21m█\e[38;5;56m█\e[38;5;91m█\e[38;5;91m█\e[38;5;126m█\e[38;5;126m█\e[38;5;161m█\e[38;5;196m█\e[0m"
376+
end
377+
@testset "rgb line2" begin
378+
res, h, w = @inferred ImageInTerminal.encodeimg(ImageInTerminal.BigBlocks(), ImageInTerminal.TermColor256(), OffsetArray(rgb_line, (2,)), 9)
379+
@test typeof(res) <: Vector{String}
380+
@test h === 1
381+
@test w === 9
382+
@test length(res) === 1
383+
@test res[1] == "\e[0m\e[38;5;21m██ \e[0m … \e[38;5;196m██ \e[0m"
384+
res, h, w = @inferred ImageInTerminal.encodeimg(ImageInTerminal.BigBlocks(), ImageInTerminal.TermColor256(), OffsetArray(rgb_line, (-2,)), 22)
385+
@test typeof(res) <: Vector{String}
386+
@test h === 1
387+
@test w === 21
388+
@test length(res) === 1
389+
@test res[1] == "\e[0m\e[38;5;21m██ \e[38;5;21m██ \e[38;5;56m██ \e[0m … \e[38;5;161m██ \e[38;5;196m██ \e[38;5;196m██ \e[0m"
390+
end
391+
@testset "lighthouse" begin
392+
res, h, w = @inferred ImageInTerminal.encodeimg(ImageInTerminal.SmallBlocks(), ImageInTerminal.TermColor256(), OffsetArray(lighthouse, (2,-10)), 60, 60)
393+
@test typeof(res) <: Vector{String}
394+
@test h === 17
395+
@test w === 49
396+
@test_reference "lighthouse_small_60x60_256" res
397+
end
398+
end

test/tst_imshow.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,20 @@ end
7575
end
7676
end
7777

78+
@testset "imshow non1" begin
79+
@testset "lena" begin
80+
img = OffsetArray(imresize(lena, 10, 10), (-10,5))
81+
io = IOBuffer()
82+
imshow(io, img)
83+
res = replace.(readlines(seek(io,0)), ["\n"], [""])
84+
@test_reference "lena_big_imshow" res
85+
end
86+
@testset "rotation" begin
87+
tfm = recenter(RotMatrix(pi/4), center(lighthouse))
88+
lhr = warp(lighthouse, tfm)
89+
io = IOBuffer()
90+
imshow(io, lhr)
91+
res = replace.(readlines(seek(io,0)), ["\n"], [""])
92+
@test_reference "lighthouse_rotated" res
93+
end
94+
end

0 commit comments

Comments
 (0)