Skip to content

Commit

Permalink
skip prefilter for Linear and Constant (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnychen94 authored May 20, 2021
1 parent eb94fc2 commit b195f34
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
9 changes: 9 additions & 0 deletions src/compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,12 @@ end
@inline _nan(::Type{HSV{Float32}}) = HSV{Float32}(NaN32,NaN32,NaN32)
@inline _nan(::Type{HSV{Float64}}) = HSV{Float64}(NaN,NaN,NaN)
@inline _nan(::Type{T}) where {T} = nan(T)

if !hasmethod(Constant{Nearest}, ())
# `Constant{Nearest}()` is not defined for Interpolations <= v0.13.2
# https://github.com/JuliaMath/Interpolations.jl/pull/426
construct_interpolation_type(::Type{T}) where T<:Union{Linear, Constant} = T()
construct_interpolation_type(::Type{Constant{Nearest}}) = Constant()
else
construct_interpolation_type(::Type{T}) where T<:Union{Linear, Constant} = T()
end
9 changes: 6 additions & 3 deletions src/interpolations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,22 @@ function box_extrapolation(
method=Linear(),
kwargs...)
T = typeof(zero(Interpolations.tweight(parent)) * zero(eltype(parent)))
itp = maybe_lazy_interpolate(T, parent, method)
itp = maybe_skip_prefilter(T, parent, method)
extrapolate(itp, _make_compatible(T, fillvalue))
end
box_extrapolation(etp::AbstractExtrapolation) = etp
box_extrapolation(itp::AbstractInterpolation{T}; fillvalue=_default_fillvalue(T)) where T =
extrapolate(itp, _make_compatible(T, fillvalue))

@inline function maybe_lazy_interpolate(::Type{T}, A::AbstractArray, degree::D) where {T, D<:Union{Linear, Constant}}
@inline function maybe_skip_prefilter(::Type{T}, A::AbstractArray, degree::D) where {T, D<:Union{Linear, Constant}}
axs = axes(A)
return Interpolations.BSplineInterpolation{T,ndims(A),typeof(A),BSpline{D},typeof(axs)}(A, axs, BSpline(degree))
end
@inline function maybe_skip_prefilter(::Type{T}, A::AbstractArray, method::BSpline{D}) where {T, D<:Union{Linear, Constant}}
maybe_skip_prefilter(T, A, construct_interpolation_type(D))
end

@inline function maybe_lazy_interpolate(::Type{T}, A::AbstractArray, method::MethodType) where T
@inline function maybe_skip_prefilter(::Type{T}, A::AbstractArray, method::MethodType) where T
return interpolate(A, wrap_BSpline(method))
end

Expand Down
16 changes: 10 additions & 6 deletions test/interpolations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,27 @@ end
n0f8_str = typestring(N0f8)
matrixf64_str = typestring(Matrix{Float64})

etp = @inferred ImageTransformations.box_extrapolation(img)
@test @inferred(ImageTransformations.box_extrapolation(etp)) === etp
@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}"
@test typeof(etp) <: Interpolations.FilledExtrapolation
@test etp.fillvalue === Gray{N0f8}(0.0)
@test etp.itp.coefs === img
for method in (Linear(), BSpline(Linear()), Constant(), BSpline(Constant()))
etp = @inferred ImageTransformations.box_extrapolation(img, method=method)
@test @inferred(ImageTransformations.box_extrapolation(etp)) === etp
# @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}"
@test typeof(etp) <: Interpolations.FilledExtrapolation
@test etp.fillvalue === Gray{N0f8}(0.0)
@test etp.itp.coefs === img
end

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

etp = @inferred ImageTransformations.box_extrapolation(img)
etp2 = @inferred ImageTransformations.box_extrapolation(etp.itp)
@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}"
@test typeof(etp2) <: Interpolations.FilledExtrapolation
@test etp2.fillvalue === Gray{N0f8}(0.0)
@test etp2 !== etp
@test etp2.itp === etp.itp

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

0 comments on commit b195f34

Please sign in to comment.