Skip to content

Commit 046f4e4

Browse files
authored
restrict: better offset support (#9)
1 parent 1eb0a79 commit 046f4e4

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

src/restrict.jl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ Reduce the size of `img` by approximately two-fold along the dimensions listed i
88
99
The type of output array `imgr` depends on the input type:
1010
11-
- If `img` is not an `OffsetArray`, then output array `imgr` will be a typical `Array` type.
1211
- If `img` is an `OffsetArray`, then output array `imgr` will also be an `OffsetArray`.
12+
- If `img` is not an `OffsetArray`, then output array `imgr` will be an `Array` type
13+
even if it has offset indices.
1314
1415
The size of `imgr` is approximately `1/2` of the original size. More specifically:
1516
@@ -122,7 +123,10 @@ function restrict(A::AbstractArray, dims::Dims)
122123
end
123124

124125
function restrict(A::AbstractArray{T,N}, dim::Integer) where {T,N}
125-
require_one_based_indexing(A)
126+
if Base.has_offset_axes(A)
127+
# For type stability, we cannot return `OffsetArray` in this method
128+
A = OffsetArrays.no_offset_view(A)
129+
end
126130

127131
indsA = axes(A)
128132
newinds = ntuple(i->i==dim ? restrict_indices(indsA[dim]) : indsA[i], Val(N))
@@ -133,11 +137,9 @@ end
133137
function restrict(A::OffsetArray{T,N}, dim::Integer) where {T,N}
134138
indsA = axes(A)
135139
newinds = map(UnitRange, ntuple(i->i==dim ? restrict_indices(indsA[dim]) : indsA[i], Val(N)))
136-
# This calls OffsetArrays implementation: a type piracy
137-
# https://github.com/JuliaArrays/OffsetArrays.jl/issues/87
138-
out = similar(A, restrict_eltype(first(A)), newinds)
139-
restrict!(out, A, dim)
140-
out
140+
# By shifting it back to normal array, the internal for loop becomes faster because
141+
# it requires less indexing computation
142+
OffsetArray(restrict(A.parent, dim), newinds)
141143
end
142144

143145
function restrict_eltype(A::AbstractArray)

test/restrict.jl

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

78+
@testset "SubArray" begin
79+
x0 = [0, 0, 1, 1, 0, 0]
80+
xv = @view x0[1:2:6]
81+
x = x0[1:2:6]
82+
xr = restrict(xv)
83+
@test xr == restrict(x)
84+
@test xr isa Array
85+
86+
x = view(x0, IdentityUnitRange(3:5))
87+
xr = restrict(x)
88+
@test xr == restrict(collect(x))
89+
@test xr isa Array
90+
end
91+
7892
@testset "FixedPoint overflow" begin
7993
# issue https://github.com/JuliaImages/Images.jl/issues/395
8094
img1 = colorview(RGB, fill(0.9, 3, 5, 5))

test/runtests.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using ImageBase, ImageCore, OffsetArrays
22
using Test, TestImages
33

4+
using OffsetArrays: IdentityUnitRange
5+
46
@testset "ImageBase.jl" begin
57
include("restrict.jl")
68

0 commit comments

Comments
 (0)