Skip to content

Commit 22c568e

Browse files
committed
use view/OffsetArrays instead of ranges
1 parent 7d7d8cb commit 22c568e

File tree

3 files changed

+26
-20
lines changed

3 files changed

+26
-20
lines changed

src/UnicodeGraphics.jl

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@ module UnicodeGraphics
55

66
export blockize, brailize
77

8-
blockize(a, cutoff=0) = blockize(a, cutoff, 1:size(a, 1), 1:size(a, 2))
9-
blockize(a, cutoff, yrange, xrange) = begin
8+
blockize(a, cutoff=0) = blockize(a, cutoff)
9+
blockize(a, cutoff) = begin
10+
yrange, xrange = axes(a)
1011
out = Array{Char,2}(undef, length(xrange) + 1, (length(yrange) - 1) ÷ 2 + 1)
11-
blockize!(out, a, cutoff, yrange, xrange)
12+
blockize!(out, a, cutoff)
1213
end
1314

14-
blockize!(out, a, cutoff, yrange, xrange) = join(block_array!(out, a, cutoff, yrange, xrange))
15+
blockize!(out, a, cutoff) = join(block_array!(out, a, cutoff))
1516

16-
function block_array!(out, a, cutoff, yrange, xrange)
17-
for y in yrange.start:2:yrange.stop
17+
function block_array!(out, a, cutoff)
18+
yrange, xrange = axes(a)
19+
for y in first(yrange):2:last(yrange)
1820
for x in xrange
1921
top = checkval(a, y, x, yrange, xrange, cutoff)
2022
bottom = checkval(a, y + 1, x, yrange, xrange, cutoff)
@@ -23,10 +25,10 @@ function block_array!(out, a, cutoff, yrange, xrange)
2325
else
2426
ch = bottom ? '' : ' '
2527
end
26-
out[x-xrange.start + 1, (y-yrange.start) ÷ 2 + 1] = Char(ch)
28+
out[x-first(xrange) + 1, (y-first(yrange)) ÷ 2 + 1] = Char(ch)
2729
end
2830
# Return after every column
29-
out[end, (y-yrange.start) ÷ 2 + 1] = Char('\n')
31+
out[end, (y-first(yrange)) ÷ 2 + 1] = Char('\n')
3032
end
3133
# The last character is null
3234
out[end, end] = 0x00
@@ -35,35 +37,37 @@ end
3537

3638
const braile_hex = ((0x01, 0x08), (0x02, 0x10), (0x04, 0x20), (0x40, 0x80))
3739

38-
brailize(a, cutoff=0) = brailize(a, cutoff, 1:size(a, 1), 1:size(a, 2))
39-
brailize(a, cutoff, yrange, xrange) = begin
40+
brailize(a) = brailize(a, 0)
41+
brailize(a, cutoff) = begin
42+
yrange, xrange = axes(a)
4043
out = Array{Char,2}(undef, (length(xrange) - 1) ÷ 2 + 2, (length(yrange) - 1) ÷ 4 + 1)
41-
brailize!(out, a, cutoff, yrange, xrange)
44+
brailize!(out, a, cutoff)
4245
end
4346

44-
brailize!(out, a, cutoff, yrange, xrange) = join(braile_array!(out, a, cutoff, yrange, xrange))
47+
brailize!(out, a, cutoff) = join(braile_array!(out, a, cutoff))
4548

46-
function braile_array!(out, a, cutoff, yrange, xrange)
47-
for y = yrange.start:4:yrange.stop
48-
for x = xrange.start:2:xrange.stop
49+
function braile_array!(out, a, cutoff)
50+
yrange, xrange = axes(a)
51+
for y in first(yrange):4:last(yrange)
52+
for x in first(xrange):2:last(xrange)
4953
ch = 0x2800
5054
for j = 0:3, i = 0:1
5155
if checkval(a, y+j, x+i, yrange, xrange, cutoff)
5256
ch += braile_hex[j % 4 + 1][i % 2 + 1]
5357
end
5458
end
55-
out[(x - xrange.start) ÷ 2 + 1, (y-yrange.start) ÷ 4 + 1] = ch
59+
out[(x - first(xrange)) ÷ 2 + 1, (y-first(yrange)) ÷ 4 + 1] = ch
5660
end
5761
# Return after every column
58-
out[end, (y-yrange.start) ÷ 4 + 1] = Char('\n')
62+
out[end, (y-first(yrange)) ÷ 4 + 1] = Char('\n')
5963
end
6064
# The last character is null
6165
out[end, end] = 0x00
6266
out
6367
end
6468

6569
checkval(a, y, x, yrange, xrange, cutoff) = begin
66-
if x <= xrange.stop && y <= yrange.stop
70+
if x <= last(xrange) && y <= last(yrange)
6771
a[y, x] > cutoff
6872
else
6973
false

test/REQUIRE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
OffsetArrays

test/runtests.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using UnicodeGraphics,
2+
OffsetArrays,
23
Test
34

45
pac = [0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 0;
@@ -82,7 +83,7 @@ block_ghost =
8283
██▀███▀▀███▀██
8384
▀ ▀▀ ▀▀ ▀\0"""
8485

85-
test_ghost = blockize(ghost, 0.5, 3:15, 4:17)
86+
test_ghost = blockize(OffsetArray(ghost[3:15, 4:17], 3:15, 4:17), 0.5, )
8687
@test test_ghost == block_ghost
8788
println(test_ghost, "\n\n", block_ghost, "\n\n")
8889

@@ -94,6 +95,6 @@ braile_ghost =
9495
⣿⣶⣿⣿⣶⣿⣿
9596
⠋⠈⠛⠀⠛⠁⠙\0"""
9697

97-
test_ghost = brailize(ghost, 0.5, 2:15, 4:17)
98+
test_ghost = brailize(OffsetArray(ghost[2:15, 4:17], 2:15, 4:17), 0.5)
9899
@test test_ghost == braile_ghost
99100
println(test_ghost, "\n\n", braile_ghost)

0 commit comments

Comments
 (0)