Skip to content

Commit b195f34

Browse files
authored
skip prefilter for Linear and Constant (#125)
1 parent eb94fc2 commit b195f34

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

src/compat.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,12 @@ end
2323
@inline _nan(::Type{HSV{Float32}}) = HSV{Float32}(NaN32,NaN32,NaN32)
2424
@inline _nan(::Type{HSV{Float64}}) = HSV{Float64}(NaN,NaN,NaN)
2525
@inline _nan(::Type{T}) where {T} = nan(T)
26+
27+
if !hasmethod(Constant{Nearest}, ())
28+
# `Constant{Nearest}()` is not defined for Interpolations <= v0.13.2
29+
# https://github.com/JuliaMath/Interpolations.jl/pull/426
30+
construct_interpolation_type(::Type{T}) where T<:Union{Linear, Constant} = T()
31+
construct_interpolation_type(::Type{Constant{Nearest}}) = Constant()
32+
else
33+
construct_interpolation_type(::Type{T}) where T<:Union{Linear, Constant} = T()
34+
end

src/interpolations.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,22 @@ function box_extrapolation(
2323
method=Linear(),
2424
kwargs...)
2525
T = typeof(zero(Interpolations.tweight(parent)) * zero(eltype(parent)))
26-
itp = maybe_lazy_interpolate(T, parent, method)
26+
itp = maybe_skip_prefilter(T, parent, method)
2727
extrapolate(itp, _make_compatible(T, fillvalue))
2828
end
2929
box_extrapolation(etp::AbstractExtrapolation) = etp
3030
box_extrapolation(itp::AbstractInterpolation{T}; fillvalue=_default_fillvalue(T)) where T =
3131
extrapolate(itp, _make_compatible(T, fillvalue))
3232

33-
@inline function maybe_lazy_interpolate(::Type{T}, A::AbstractArray, degree::D) where {T, D<:Union{Linear, Constant}}
33+
@inline function maybe_skip_prefilter(::Type{T}, A::AbstractArray, degree::D) where {T, D<:Union{Linear, Constant}}
3434
axs = axes(A)
3535
return Interpolations.BSplineInterpolation{T,ndims(A),typeof(A),BSpline{D},typeof(axs)}(A, axs, BSpline(degree))
3636
end
37+
@inline function maybe_skip_prefilter(::Type{T}, A::AbstractArray, method::BSpline{D}) where {T, D<:Union{Linear, Constant}}
38+
maybe_skip_prefilter(T, A, construct_interpolation_type(D))
39+
end
3740

38-
@inline function maybe_lazy_interpolate(::Type{T}, A::AbstractArray, method::MethodType) where T
41+
@inline function maybe_skip_prefilter(::Type{T}, A::AbstractArray, method::MethodType) where T
3942
return interpolate(A, wrap_BSpline(method))
4043
end
4144

test/interpolations.jl

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,27 @@ end
4646
n0f8_str = typestring(N0f8)
4747
matrixf64_str = typestring(Matrix{Float64})
4848

49-
etp = @inferred ImageTransformations.box_extrapolation(img)
50-
@test @inferred(ImageTransformations.box_extrapolation(etp)) === etp
51-
@test summary(etp) == "2×2 extrapolate(interpolate(::Array{Gray{N0f8},2}, BSpline(Linear())), Gray{N0f8}(0.0)) with element type $(ctqual)Gray{$(fpqual)$n0f8_str}"
52-
@test typeof(etp) <: Interpolations.FilledExtrapolation
53-
@test etp.fillvalue === Gray{N0f8}(0.0)
54-
@test etp.itp.coefs === img
49+
for method in (Linear(), BSpline(Linear()), Constant(), BSpline(Constant()))
50+
etp = @inferred ImageTransformations.box_extrapolation(img, method=method)
51+
@test @inferred(ImageTransformations.box_extrapolation(etp)) === etp
52+
# @test summary(etp) == "2×2 extrapolate(interpolate(::Array{Gray{N0f8},2}, BSpline(Linear())), Gray{N0f8}(0.0)) with element type $(ctqual)Gray{$(fpqual)$n0f8_str}"
53+
@test typeof(etp) <: Interpolations.FilledExtrapolation
54+
@test etp.fillvalue === Gray{N0f8}(0.0)
55+
@test etp.itp.coefs === img
56+
end
5557

5658
# to catch regressions like #60
5759
@test @inferred(ImageTransformations._getindex(img, @SVector([1,2]))) isa Gray{N0f8}
5860

61+
etp = @inferred ImageTransformations.box_extrapolation(img)
5962
etp2 = @inferred ImageTransformations.box_extrapolation(etp.itp)
6063
@test summary(etp2) == "2×2 extrapolate(interpolate(::Array{Gray{N0f8},2}, BSpline(Linear())), Gray{N0f8}(0.0)) with element type $(ctqual)Gray{$(fpqual)$n0f8_str}"
6164
@test typeof(etp2) <: Interpolations.FilledExtrapolation
6265
@test etp2.fillvalue === Gray{N0f8}(0.0)
6366
@test etp2 !== etp
6467
@test etp2.itp === etp.itp
6568

69+
etp = @inferred ImageTransformations.box_extrapolation(img)
6670
etp2 = @inferred ImageTransformations.box_extrapolation(etp.itp, fillvalue=Flat())
6771
@test summary(etp2) == "2×2 extrapolate(interpolate(::Array{Gray{N0f8},2}, BSpline(Linear())), Flat()) with element type $(ctqual)Gray{$(fpqual)$n0f8_str}"
6872
@test typeof(etp2) <: Interpolations.Extrapolation

0 commit comments

Comments
 (0)