From b195f3426bc56d4770c5d4d91a0338aee5bcbaa8 Mon Sep 17 00:00:00 2001 From: Johnny Chen Date: Thu, 20 May 2021 18:24:42 +0800 Subject: [PATCH] skip prefilter for Linear and Constant (#125) --- src/compat.jl | 9 +++++++++ src/interpolations.jl | 9 ++++++--- test/interpolations.jl | 16 ++++++++++------ 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/compat.jl b/src/compat.jl index 7043b23..71107f8 100644 --- a/src/compat.jl +++ b/src/compat.jl @@ -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 diff --git a/src/interpolations.jl b/src/interpolations.jl index 021fea3..191bc64 100644 --- a/src/interpolations.jl +++ b/src/interpolations.jl @@ -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 diff --git a/test/interpolations.jl b/test/interpolations.jl index fb8c375..e20a7af 100644 --- a/test/interpolations.jl +++ b/test/interpolations.jl @@ -46,16 +46,19 @@ 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 @@ -63,6 +66,7 @@ end @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