Skip to content

Commit

Permalink
Merge pull request #11 from JuliaImages/teh/getindex
Browse files Browse the repository at this point in the history
Fix indexing, similar behavior for ImageMetaAxis
  • Loading branch information
timholy authored Feb 4, 2017
2 parents 15a28f7 + 2530adc commit aaa3508
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 30 deletions.
78 changes: 48 additions & 30 deletions src/ImageMetadata.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ using ColorVectorSpace # for overriding math operations with Gray/RGB

import Base: +, .+, -, .-, *, .*, /, ./, .^, .<, .>, .==
import Base: permutedims
using Base: ViewIndex

export
# types
Expand Down Expand Up @@ -53,44 +54,60 @@ Base.size(A::ImageMeta) = size(A.data)
Base.linearindexing(A::ImageMeta) = Base.linearindexing(A.data)

# getindex and setindex!
@inline function Base.getindex{T}(img::ImageMeta{T,1}, i::Int)
@boundscheck checkbounds(img.data, i)
@inbounds ret = img.data[i]
ret
end
@inline function Base.getindex(img::ImageMeta, i::Int)
@boundscheck checkbounds(img.data, i)
@inbounds ret = img.data[i]
ret
end
@inline function Base.getindex{T,N}(img::ImageMeta{T,N}, I::Vararg{Int,N})
@boundscheck checkbounds(img.data, I...)
@inbounds ret = img.data[I...]
ret
for AType in (ImageMeta, ImageMetaAxis)
@eval begin
@inline function Base.getindex{T}(img::$AType{T,1}, i::Int)
@boundscheck checkbounds(img.data, i)
@inbounds ret = img.data[i]
ret
end
@inline function Base.getindex(img::$AType, i::Int)
@boundscheck checkbounds(img.data, i)
@inbounds ret = img.data[i]
ret
end
@inline function Base.getindex{T,N}(img::$AType{T,N}, I::Vararg{Int,N})
@boundscheck checkbounds(img.data, I...)
@inbounds ret = img.data[I...]
ret
end

@inline function Base.setindex!{T}(img::$AType{T,1}, val, i::Int)
@boundscheck checkbounds(img.data, i)
@inbounds img.data[i] = val
val
end
@inline function Base.setindex!(img::$AType, val, i::Int)
@boundscheck checkbounds(img.data, i)
@inbounds img.data[i] = val
val
end
@inline function Base.setindex!{T,N}(img::$AType{T,N}, val, I::Vararg{Int,N})
@boundscheck checkbounds(img.data, I...)
@inbounds img.data[I...] = val
val
end
end
end
@inline function Base.getindex{T,N}(img::ImageMetaAxis{T,N}, ax::Axis, I...)

@inline function Base.getindex(img::ImageMetaAxis, ax::Axis, I...)
img.data[ax, I...]
end
@inline function Base.setindex!{T}(img::ImageMeta{T,1}, val, i::Int)
@boundscheck checkbounds(img.data, i)
@inbounds img.data[i] = val
val
@inline function Base.getindex(img::ImageMetaAxis, i::Union{Integer,AbstractVector,Colon}, I...)
img.data[i, I...]
end
@inline function Base.setindex!(img::ImageMeta, val, i::Int)
@boundscheck checkbounds(img.data, i)
@inbounds img.data[i] = val
val
end
@inline function Base.setindex!{T,N}(img::ImageMeta{T,N}, val, I::Vararg{Int,N})
@boundscheck checkbounds(img.data, I...)
@inbounds img.data[I...] = val
val
end
@inline function Base.setindex!{T,N}(img::ImageMetaAxis{T,N}, val, ax::Axis, I...)

@inline function Base.setindex!(img::ImageMetaAxis, val, ax::Axis, I...)
setindex!(img.data, val, ax, I...)
end
@inline function Base.setindex!(img::ImageMetaAxis, val, i::Union{Integer,AbstractVector,Colon}, I...)
setindex!(img.data, val, i, I...)
end

Base.view(img::ImageMetaAxis, ax::Axis, I...) = view(img.data, ax, I...)
Base.view{T,N}(img::ImageMetaAxis{T,N}, I::Vararg{ViewIndex,N}) = view(img.data, I...)
Base.view(img::ImageMetaAxis, i::ViewIndex) = view(img.data, i)
Base.view{N}(img::ImageMetaAxis, I::Vararg{ViewIndex,N}) = view(img.data, I...)

Base.getindex(img::ImageMeta, propname::AbstractString) = img.properties[propname]

Expand All @@ -115,6 +132,7 @@ end

# similar
Base.similar{T}(img::ImageMeta, ::Type{T}, shape::Dims) = ImageMeta(similar(img.data, T, shape), deepcopy(img.properties))
Base.similar{T}(img::ImageMetaAxis, ::Type{T}) = ImageMeta(similar(img.data, T), deepcopy(img.properties))

"""
copyproperties(img::ImageMeta, data) -> imgnew
Expand Down
23 changes: 23 additions & 0 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ using Base.Test
@test val == 1.234
a = zeros(3,5)
end

# Issue #10
A = AxisArray(rand(3,5), :y, :x)
B = ImageMeta(A, info="blah")
Broi = B[2:3, 2:3]
@test isa(Broi, AxisArray)
@test axisnames(Broi) == (:y, :x)
A1, B1 = A[2:7], B[2:7]
@test typeof(A1) == typeof(B1) && A1 == B1
Broi = view(B, 2:3, 2:3)
@test isa(Broi, AxisArray)
@test axisnames(Broi) == (:y, :x)
end

@testset "convert" begin
Expand Down Expand Up @@ -136,6 +148,17 @@ end
@test img2.data != img.data
img2["prop3"] = 7
@test !haskey(img, "prop3")

A = AxisArray(rand(3,5), :y, :x)
B = ImageMeta(A, info="blah")
C = similar(B)
@test isa(C, ImageMeta)
@test isa(C.data, AxisArray)
@test eltype(C) == Float64
C = similar(B, RGB{Float16})
@test isa(C, ImageMeta)
@test isa(C.data, AxisArray)
@test eltype(C) == RGB{Float16}
end

@testset "copy/shareproperties/viewim" begin
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ using Base.Test

include("core.jl")
include("operations.jl")
info("Beginning of tests with deprecation warnings\n\n")
include("deprecated.jl")

nothing

0 comments on commit aaa3508

Please sign in to comment.