Skip to content

Commit

Permalink
start with multiple images refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Evizero committed Apr 27, 2018
1 parent cbb8a23 commit 28877d5
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/operation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ prepareaffine(img) = invwarpedview(img, toaffinemap(NoOp(), img), Flat())
prepareaffine(img::AbstractExtrapolation) = invwarpedview(img, toaffinemap(NoOp(), img))
@inline prepareaffine(img::SubArray{T,N,<:InvWarpedView}) where {T,N} = img
@inline prepareaffine(img::InvWarpedView) = img
prepareaffine(imgs::Tuple) = map(prepareaffine, imgs)

# currently unused
@inline preparelazy(img) = img
Expand Down
37 changes: 30 additions & 7 deletions src/operations/cache.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ applyeager(op::CacheImage, img::OffsetArray) = img
applyeager(op::CacheImage, img::SubArray) = copy(img)
applyeager(op::CacheImage, img::InvWarpedView) = copy(img)
applyeager(op::CacheImage, img) = collect(img)
applyeager(op::CacheImage, imgs::Tuple) = map(img->applyeager(op,img), imgs)

function showconstruction(io::IO, op::CacheImage)
print(io, typeof(op).name.name, "()")
Expand All @@ -79,26 +80,48 @@ end
see [`CacheImage`](@ref)
"""
struct CacheImageInto{T<:AbstractArray} <: ImageOperation
struct CacheImageInto{T<:Union{AbstractArray,Tuple}} <: ImageOperation
buffer::T
end
CacheImage(buffer::AbstractArray) = CacheImageInto(buffer)
CacheImage(buffers::AbstractArray...) = CacheImageInto(buffers)
CacheImage(buffers::NTuple{N,AbstractArray}) where {N} = CacheImageInto(buffers)

@inline supports_lazy(::Type{<:CacheImageInto}) = true

applyeager(op::CacheImageInto, img) = applylazy(op, img)

function applylazy(op::CacheImageInto, img)
function applylazy(op::CacheImageInto{<:AbstractArray}, img::AbstractArray)
copy!(match_idx(op.buffer, indices(img)), img)
end

function showconstruction(io::IO, op::CacheImageInto)
print(io, "CacheImage(") # shows exported API
function applylazy(op::CacheImageInto{<:Tuple}, imgs::Tuple)
map(op.buffer, imgs) do buffer, img
copy!(match_idx(buffer, indices(img)), img)
end
end

function _showarrayconstruction(io::IO, array::AbstractArray)
print(io, "Array{")
_showcolor(io, eltype(op.buffer))
_showcolor(io, eltype(array))
print(io, "}(")
print(io, join(map(i->string(length(i)), indices(op.buffer)), ", "))
print(io, "))")
print(io, join(map(i->string(length(i)), indices(array)), ", "))
print(io, ")")
end

function showconstruction(io::IO, op::CacheImageInto{<:AbstractArray})
print(io, "CacheImage(") # shows exported API
_showarrayconstruction(io, op.buffer)
print(io, ")")
end

function showconstruction(io::IO, op::CacheImageInto{<:Tuple})
print(io, "CacheImage(")
for (i, buffer) in enumerate(op.buffer)
_showarrayconstruction(io, buffer)
i < length(op.buffer) && print(io, ", ")
end
print(io, ")")
end

function Base.show(io::IO, op::CacheImageInto)
Expand Down
1 change: 1 addition & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ end
# avoid recursion
@inline plain_array(A::SubArray) = _plain_array(copy(A))
@inline plain_array(A::AbstractArray) = _plain_array(collect(A))
plain_array(A::Tuple) = map(plain_array, A)

# --------------------------------------------------------------------

Expand Down
10 changes: 10 additions & 0 deletions test/operations/tst_cache.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,25 @@
@test str_showcompact(CacheImage()) == "Cache into temporary buffer"

@test @inferred(Augmentor.applyeager(CacheImage(),square)) === square
@test @inferred(Augmentor.applyeager(CacheImage(),(square,))) === (square,)
@test @inferred(Augmentor.applyeager(CacheImage(),(square,square2))) === (square,square2)

v = view(square, :, :)
img = @inferred Augmentor.applyeager(CacheImage(), v)
@test typeof(img) <: Array
@test eltype(img) == eltype(v)
@test img !== square
@test img == square
tmp,img = @inferred Augmentor.applyeager(CacheImage(), (square,v))
@test typeof(img) <: Array
@test eltype(img) == eltype(v)
@test tmp === square
@test img !== square
@test img == square

o = OffsetArray(square, (-1,2))
@test @inferred(Augmentor.applyeager(CacheImage(),o)) === o
@test @inferred(Augmentor.applyeager(CacheImage(),(o,square))) === (o,square)

@test Augmentor.supports_eager(CacheImage) === true
@test Augmentor.supports_lazy(CacheImage) === false
Expand All @@ -27,6 +36,7 @@
@test Augmentor.supports_affineview(CacheImage) === false

@test_throws MethodError Augmentor.applylazy(CacheImage(), v)
@test_throws MethodError Augmentor.applylazy(CacheImage(), (v,v))
@test_throws MethodError Augmentor.applyview(CacheImage(), v)
@test_throws MethodError Augmentor.applystepview(CacheImage(), v)
@test_throws MethodError Augmentor.applypermute(CacheImage(), v)
Expand Down

0 comments on commit 28877d5

Please sign in to comment.