Skip to content

Commit d2197f6

Browse files
authored
restrict: only return OffsetArray when input is an OffsetArray (#4)
1 parent eff14e9 commit d2197f6

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

src/ImageUtils.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ export restrict
44

55
using Base.Cartesian: @nloops
66
using ImageCore
7+
using ImageCore.OffsetArrays
78

89
include("restrict.jl")
10+
include("compat.jl")
911

1012
end

src/compat.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
if VERSION < v"1.2"
2+
require_one_based_indexing(A...) = !Base.has_offset_axes(A...) || throw(ArgumentError("offset arrays are not supported but got an array with index other than 1"))
3+
else
4+
const require_one_based_indexing = Base.require_one_based_indexing
5+
end

src/restrict.jl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,20 @@ function restrict(A::AbstractArray, region::Dims)
7676
end
7777

7878
function restrict(A::AbstractArray{T,N}, dim::Integer) where {T,N}
79+
require_one_based_indexing(A)
80+
81+
indsA = axes(A)
82+
newinds = ntuple(i->i==dim ? restrict_indices(indsA[dim]) : indsA[i], Val(N))
83+
out = Array{restrict_eltype(first(A)), N}(undef, last.(newinds))
84+
restrict!(out, A, dim)
85+
out
86+
end
87+
function restrict(A::OffsetArray{T,N}, dim::Integer) where {T,N}
7988
indsA = axes(A)
8089
newinds = map(UnitRange, ntuple(i->i==dim ? restrict_indices(indsA[dim]) : indsA[i], Val(N)))
81-
out = similar(Array{restrict_eltype(first(A)), N}, newinds)
90+
# This calls OffsetArrays implementation: a type piracy
91+
# https://github.com/JuliaArrays/OffsetArrays.jl/issues/87
92+
out = similar(A, restrict_eltype(first(A)), newinds)
8293
restrict!(out, A, dim)
8394
out
8495
end

test/restrict.jl

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
1-
@testset "restrict" begin
1+
@testset "restrict" begin
2+
@testset "interfaces" begin
3+
A = rand(N0f8, 4, 5, 3)
4+
5+
Ar = @inferred restrict(A, 1)
6+
@test typeof(Ar) <: Array
7+
@test size(Ar) == (3, 5, 3)
8+
9+
Ar = @inferred restrict(A, (1, ))
10+
@test typeof(Ar) <: Array
11+
@test size(Ar) == (3, 5, 3)
12+
13+
Ar = @inferred restrict(A, (1, 2, 3))
14+
@test typeof(Ar) <: Array
15+
@test size(Ar) == (3, 3, 2)
16+
@test Ar == restrict(A)
17+
18+
@test_throws MethodError restrict(A, 1, 2, 3)
19+
end
20+
221
@testset "numerical test" begin
322
A = reshape([UInt16(i) for i = 1:60], 4, 5, 3)
423
B = restrict(A, (1,2))
@@ -36,9 +55,17 @@
3655
@testset "OffsetArray" begin
3756
A = rand(5, 4, 3)
3857
Ao = OffsetArray(A, (-2,1,0))
39-
@test parent(@inferred(restrict(Ao, 1))) == restrict(A, 1)
40-
@test parent(@inferred(restrict(Ao, 2))) == restrict(A, 2)
41-
@test parent(@inferred(restrict(Ao, (1,2)))) == restrict(A, (1,2))
58+
59+
for (dims, offsets) in [
60+
(1, (-1, 1, 0)),
61+
(2, (-2, 0, 0)),
62+
((1, 2), (-1, 0, 0))
63+
]
64+
Ar = @inferred restrict(Ao, dims)
65+
@test typeof(Ar) <: OffsetArray
66+
@test Ar.offsets == offsets
67+
@test parent(Ar) == restrict(A, dims)
68+
end
4269
end
4370

4471
@testset "FixedPoint overflow" begin

0 commit comments

Comments
 (0)