Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deprecate invwarpedview in favor of InvWarpedView #138

Merged
merged 3 commits into from
Aug 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion docs/src/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ ImageTransformations.imresize
ImageTransformations.warp
ImageTransformations.WarpedView
ImageTransformations.InvWarpedView
ImageTransformations.invwarpedview
```

# Utilities
Expand Down
6 changes: 0 additions & 6 deletions src/compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@
# - Codes for backward compatibility
# - Glue codes that might no longer be necessary in the future

@static if !isdefined(Base, :IdentityUnitRange)
const IdentityUnitRange = Base.Slice
else
using Base: IdentityUnitRange
end

@static if VERSION < v"1.1"
@inline isnothing(x) = x === nothing
end
Expand Down
17 changes: 9 additions & 8 deletions src/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@
@deprecate WarpedView(img::AbstractArray, tform::Transformation, inds, fillvalue::FillType, method::MethodType) WarpedView(img, tform, inds; method=method, fillvalue=fillvalue)

@deprecate warpedview(args...; kwargs...) WarpedView(args...; kwargs...)
@deprecate invwarpedview(args...; kwargs...) InvWarpedView(args...; kwargs...)

@deprecate invwarpedview(img::AbstractArray, tinv::Transformation, method::MethodType, ) invwarpedview(img, tinv; method=method)
@deprecate invwarpedview(img::AbstractArray, tinv::Transformation, fillvalue::FillType) invwarpedview(img, tinv; fillvalue=fillvalue)
@deprecate invwarpedview(img::AbstractArray, tinv::Transformation, method::MethodType, fillvalue::FillType) invwarpedview(img, tinv; method=method, fillvalue=fillvalue)
@deprecate invwarpedview(img::AbstractArray, tinv::Transformation, fillvalue::FillType, method::MethodType) invwarpedview(img, tinv; method=method, fillvalue=fillvalue)
@deprecate invwarpedview(img::AbstractArray, tinv::Transformation, inds, method::MethodType, ) invwarpedview(img, tinv, inds; method=method)
@deprecate invwarpedview(img::AbstractArray, tinv::Transformation, inds, fillvalue::FillType) invwarpedview(img, tinv, inds; fillvalue=fillvalue)
@deprecate invwarpedview(img::AbstractArray, tinv::Transformation, inds, method::MethodType, fillvalue::FillType) invwarpedview(img, tinv, inds; method=method, fillvalue=fillvalue)
@deprecate invwarpedview(img::AbstractArray, tinv::Transformation, inds, fillvalue::FillType, method::MethodType) invwarpedview(img, tinv, inds; method=method, fillvalue=fillvalue)
@deprecate InvWarpedView(img::AbstractArray, tinv::Transformation, method::MethodType, ) InvWarpedView(img, tinv; method=method)
@deprecate InvWarpedView(img::AbstractArray, tinv::Transformation, fillvalue::FillType) InvWarpedView(img, tinv; fillvalue=fillvalue)
@deprecate InvWarpedView(img::AbstractArray, tinv::Transformation, method::MethodType, fillvalue::FillType) InvWarpedView(img, tinv; method=method, fillvalue=fillvalue)
@deprecate InvWarpedView(img::AbstractArray, tinv::Transformation, fillvalue::FillType, method::MethodType) InvWarpedView(img, tinv; method=method, fillvalue=fillvalue)
@deprecate InvWarpedView(img::AbstractArray, tinv::Transformation, inds, method::MethodType, ) InvWarpedView(img, tinv, inds; method=method)
@deprecate InvWarpedView(img::AbstractArray, tinv::Transformation, inds, fillvalue::FillType) InvWarpedView(img, tinv, inds; fillvalue=fillvalue)
@deprecate InvWarpedView(img::AbstractArray, tinv::Transformation, inds, method::MethodType, fillvalue::FillType) InvWarpedView(img, tinv, inds; method=method, fillvalue=fillvalue)
@deprecate InvWarpedView(img::AbstractArray, tinv::Transformation, inds, fillvalue::FillType, method::MethodType) InvWarpedView(img, tinv, inds; method=method, fillvalue=fillvalue)

# END 0.9 deprecations
60 changes: 15 additions & 45 deletions src/invwarpedview.jl
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
"""
InvWarpedView(img, tinv, [indices]) -> wv
InvWarpedView(img, tinv, [indices]; kwargs...) -> wv
InvWarpedView(inner_view, tinv) -> wv

Create a view of `img` that lazily transforms any given index `I`
passed to `wv[I]` so that `wv[I] == img[inv(tinv)(I)]`.

Except for the lazy evaluation, the following two lines are expected to be equivalent:

```julia
warp(img, inv(tform), [indices]; kwargs...)
invwarpedview(img, tform, [indices]; kwargs...)
```

The conceptual difference to [`WarpedView`](@ref) is that
`InvWarpedView` is intended to be used when reasoning about the
image is more convenient that reasoning about the indices.
Furthermore, `InvWarpedView` allows simple nesting of
transformations, in which case the transformations will be
composed into a single one.

See [`invwarpedview`](@ref) for a convenient constructor of `InvWarpedView`.

For detailed explaination of warp, associated arguments and parameters,
please refer to [`warp`](@ref).
"""
Expand All @@ -26,8 +32,9 @@ function InvWarpedView(inner::WarpedView{T,N,TA,F,I,E}) where {T,N,TA,F,I,E}
InvWarpedView{T,N,TA,F,I,typeof(tinv),E}(inner, tinv)
end

function InvWarpedView(A::AbstractArray, tinv::Transformation, inds::Tuple = autorange(A, tinv))
InvWarpedView(WarpedView(A, inv(tinv), inds), tinv)
function InvWarpedView(A::AbstractArray, tinv::Transformation, inds::Tuple = autorange(A, tinv); kwargs...)
inner = WarpedView(A, inv(tinv), inds; kwargs...)
InvWarpedView(inner, tinv)
end

function InvWarpedView(inner::InvWarpedView, outer_tinv::Transformation)
Expand All @@ -40,14 +47,11 @@ function InvWarpedView(inner::InvWarpedView, outer_tinv::Transformation, inds::T
InvWarpedView(parent(inner), tinv, inds)
end

Base.parent(A::InvWarpedView) = parent(A.inner)
@inline Base.parent(A::InvWarpedView) = parent(A.inner)
@inline Base.axes(A::InvWarpedView) = axes(A.inner)
@inline Base.size(A::InvWarpedView) = size(A.inner)

IndexStyle(::Type{T}) where {T<:InvWarpedView} = IndexCartesian()
@inline Base.getindex(A::InvWarpedView{T,N}, I::Vararg{Int,N}) where {T,N} = A.inner[I...]

Base.size(A::InvWarpedView) = size(A.inner)
Base.size(A::InvWarpedView, d) = size(A.inner, d)
Base.@propagate_inbounds Base.getindex(A::InvWarpedView{T,N}, I::Vararg{Int,N}) where {T,N} = A.inner[I...]

function Base.showarg(io::IO, A::InvWarpedView, toplevel)
print(io, "InvWarpedView(")
Expand All @@ -60,37 +64,3 @@ function Base.showarg(io::IO, A::InvWarpedView, toplevel)
print(io, ')')
end
end

"""
invwarpedview(img, tinv, [indices]; kwargs...) -> wv

Create a view of `img` that lazily transforms any given index `I`
passed to `wv[I]` so that `wv[I] == img[inv(tinv)(I)]`.

Except for the lazy evaluation, the following two lines are equivalent:

```julia
warp(img, inv(tform), [indices]; kwargs...)
invwarpedview(img, tform, [indices]; kwargs...)
```

For detailed explaination of warp, associated arguments and parameters,
please refer to [`warp`](@ref).
"""
function invwarpedview(A::AbstractArray, tinv::Transformation, indices::Tuple=autorange(A, tinv); kwargs...)
InvWarpedView(box_extrapolation(A; kwargs...), tinv, indices)
end

# For SubArray:
# 1. We can exceed the boundary of SubArray by using its parent and thus trick Interpolations in
# order to get better extrapolation result around the border. Otherwise it will just fill it.
# 2. For default indices, we use `IdentityUnitRange`, which guarantees `r[i] == i`, to preserve the view indices.
function invwarpedview(A::SubArray, tinv::Transformation; kwargs...)
default_indices = map(IdentityUnitRange, autorange(CartesianIndices(A.indices), tinv))
invwarpedview(A, tinv, default_indices; kwargs...)
end
function invwarpedview(A::SubArray, tinv::Transformation, indices::Tuple; kwargs...)
inner = parent(A)
new_inner = InvWarpedView(inner, tinv, autorange(inner, tinv))
view(new_inner, indices...)
end
4 changes: 2 additions & 2 deletions src/warp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ There're some high-level interfaces of `warp`:
- image rotation: [`imrotate`](@ref)
- image resize: [`imresize`](@ref)

There are also lazy version of `warp`:
There are also lazy versions of `warp`:

- [`WarpedView`](@ref) is almost equivalent to `warp` except that it does not allocate memory.
- [`invwarpedview(img, tform, [indices]; kwargs...)`](@ref ImageTransformations.invwarpedview)
- [`InvWarpedView(img, tform, [indices]; kwargs...)`](@ref ImageTransformations.InvWarpedView)
is almost equivalent to `warp(img, inv(tform), [indices]; kwargs...)` except that it does not
allocate memory.

Expand Down
14 changes: 5 additions & 9 deletions src/warpedview.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,13 @@ function WarpedView(
WarpedView{T,N,typeof(A),typeof(tform),typeof(inds),typeof(etp)}(A, tform, inds, etp)
end

Base.parent(A::WarpedView) = A.parent
@inline Base.parent(A::WarpedView) = A.parent
@inline Base.axes(A::WarpedView) = A.indices
@inline Base.size(A::WarpedView) = map(length,axes(A))

IndexStyle(::Type{T}) where {T<:WarpedView} = IndexCartesian()
@inline Base.getindex(A::WarpedView{T,N}, I::Vararg{Int,N}) where {T,N} =
T(_getindex(A.extrapolation, A.transform(SVector(I))))
Base.size(A::WarpedView{T,N,TA,F}) where {T,N,TA,F} = map(length,axes(A))
Base.size(A::WarpedView{T,N,TA,F}, d) where {T,N,TA,F} = length(axes(A,d))

Base.size(A::WarpedView{T,N,TA,F,NTuple{N,Base.OneTo{Int}}}) where {T,N,TA,F} = map(length, A.indices)
Base.size(A::WarpedView{T,N,TA,F,NTuple{N,Base.OneTo{Int}}}, d) where {T,N,TA,F} = d <= N ? length(A.indices[d]) : 1
Base.@propagate_inbounds function Base.getindex(A::WarpedView{T,N}, I::Vararg{Int,N}) where {T,N}
convert(T, _getindex(A.extrapolation, A.transform(SVector(I))))
end

function Base.showarg(io::IO, A::WarpedView, toplevel)
print(io, "WarpedView(")
Expand Down
12 changes: 12 additions & 0 deletions test/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,22 @@ using ImageTransformations: box_extrapolation
@test nearlysame(invwarpedview(img, tfm, 1), InvWarpedView(box_extrapolation(img; fillvalue=1), tfm))
@test nearlysame(invwarpedview(img, tfm, Periodic()), InvWarpedView(box_extrapolation(img; fillvalue=Periodic()), tfm))

@test nearlysame(InvWarpedView(img, tfm, Constant()), InvWarpedView(img, tfm; method=Constant()))
@test nearlysame(InvWarpedView(img, tfm, Constant(), 1), InvWarpedView(img, tfm; method=Constant(), fillvalue=1))
@test nearlysame(InvWarpedView(img, tfm, 1, Constant()), InvWarpedView(img, tfm; method=Constant(), fillvalue=1))
@test nearlysame(InvWarpedView(img, tfm, 1), InvWarpedView(img, tfm; fillvalue=1))
@test nearlysame(InvWarpedView(img, tfm, Periodic()), InvWarpedView(img, tfm; fillvalue=Periodic()))

@test nearlysame(invwarpedview(img, tfm, axes(img), Constant()), InvWarpedView(box_extrapolation(img; method=Constant()), tfm, axes(img)))
@test nearlysame(invwarpedview(img, tfm, axes(img), Constant(), 1), InvWarpedView(box_extrapolation(img; method=Constant(), fillvalue=1), tfm, axes(img)))
@test nearlysame(invwarpedview(img, tfm, axes(img), 1, Constant()), InvWarpedView(box_extrapolation(img; method=Constant(), fillvalue=1), tfm, axes(img)))
@test nearlysame(invwarpedview(img, tfm, axes(img), 1), InvWarpedView(box_extrapolation(img; fillvalue=1), tfm, axes(img)))
@test nearlysame(invwarpedview(img, tfm, axes(img), Periodic()), InvWarpedView(box_extrapolation(img; fillvalue=Periodic()), tfm, axes(img)))

@test nearlysame(InvWarpedView(img, tfm, axes(img), Constant()), InvWarpedView(img, tfm, axes(img); method=Constant()))
@test nearlysame(InvWarpedView(img, tfm, axes(img), Constant(), 1), InvWarpedView(img, tfm, axes(img); method=Constant(), fillvalue=1))
@test nearlysame(InvWarpedView(img, tfm, axes(img), 1, Constant()), InvWarpedView(img, tfm, axes(img); method=Constant(), fillvalue=1))
@test nearlysame(InvWarpedView(img, tfm, axes(img), 1), InvWarpedView(img, tfm, axes(img); fillvalue=1))
@test nearlysame(InvWarpedView(img, tfm, axes(img), Periodic()), InvWarpedView(img, tfm, axes(img); fillvalue=Periodic()))
end
end
Loading