Skip to content

Commit

Permalink
Merge pull request #21 from JuliaImages/teh/0.6
Browse files Browse the repository at this point in the history
Updates for new type system and tuple-printing
  • Loading branch information
timholy authored Feb 13, 2017
2 parents 8b01c7e + fe10938 commit b31e927
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 27 deletions.
1 change: 1 addition & 0 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
julia 0.5
FixedPointNumbers 0.3.0
ColorTypes
Colors
MappedArrays 0.0.3
Graphics
Expand Down
1 change: 1 addition & 0 deletions src/ImageCore.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module ImageCore

using Colors, FixedPointNumbers, MappedArrays, Graphics, ShowItLikeYouBuildIt
using OffsetArrays # for show.jl
using ColorTypes: colorant_string
using Colors: Fractional

using Base: tail, @pure, Indices
Expand Down
15 changes: 9 additions & 6 deletions src/colorchannels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ typealias NonparametricColors Union{RGB24,ARGB32,Gray24,AGray32}
immutable ChannelView{T,N,A<:AbstractArray} <: AbstractArray{T,N}
parent::A

function ChannelView{C<:Colorant}(parent::AbstractArray{C})
function (::Type{ChannelView{T,N,A}}){T,N,A,C<:Colorant}(parent::AbstractArray{C})
n = length(channelview_indices(parent))
n == N || throw(DimensionMismatch("for an $N-dimensional ChannelView with color type $C, input dimensionality should be $n instead of $(ndims(parent))"))
new(parent)
new{T,N,A}(parent)
end
end

Expand Down Expand Up @@ -117,11 +117,11 @@ The opposite transformation is implemented by `ChannelView`.
immutable ColorView{C<:Colorant,N,A<:AbstractArray} <: AbstractArray{C,N}
parent::A

function ColorView{T<:Number}(parent::AbstractArray{T})
function (::Type{ColorView{C,N,A}}){C,N,A,T<:Number}(parent::AbstractArray{T})
n = length(colorview_size(C, parent))
n == N || throw(DimensionMismatch("for an $N-dimensional ColorView with color type $C, input dimensionality should be $n instead of $(ndims(parent))"))
checkdim1(C, indices(parent))
new(parent)
new{C,N,A}(parent)
end
end

Expand Down Expand Up @@ -276,12 +276,15 @@ end
_colorview_type{T}(::Type{Any}, ::Type{T}) = T
_colorview_type{T1,T2}(::Type{T1}, ::Type{T2}) = T1

Base.@pure promote_eleltype_all(gray, grays...) = _promote_eleltype_all(eltype(eltype(gray)), grays...)
Base.@pure promote_eleltype_all(gray, grays...) = _promote_eleltype_all(beltype(eltype(gray)), grays...)
@inline function _promote_eleltype_all{T}(::Type{T}, gray, grays...)
_promote_eleltype_all(promote_type(T, eltype(eltype(gray))), grays...)
_promote_eleltype_all(promote_type(T, beltype(eltype(gray))), grays...)
end
_promote_eleltype_all{T}(::Type{T}) = T

beltype{T}(::Type{T}) = eltype(T)
beltype(::Type{Union{}}) = Union{}

## Tuple & indexing utilities

_size(A::AbstractArray) = map(length, indices(A))
Expand Down
4 changes: 2 additions & 2 deletions src/convert_reinterpret.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ end

function throw_color_typeerror{CV,T<:Unsigned}(::Type{CV}, ::Type{T}, funcs)
funcstr = join(funcs, " or ")
throw(ArgumentError("$(CV.name.name){$T} is not an allowed type; for an array with element type $T,\n before calling $funcstr consider calling normedview, or specify the Normed{$T,f} element type"))
throw(ArgumentError("$(colorant_string(CV)){$T} is not an allowed type; for an array with element type $T,\n before calling $funcstr consider calling normedview, or specify the Normed{$T,f} element type"))
end

function throw_color_typeerror{CV,T<:Integer}(::Type{CV}, ::Type{T}, funcs)
funcstr = join(funcs, " or ")
throw(ArgumentError("$(CV.name.name){$T} is not an allowed type; before calling $funcstr, please specify the concrete\n element type as a Fixed{$T,f} type"))
throw(ArgumentError("$(colorant_string(CV)){$T} is not an allowed type; before calling $funcstr, please specify the concrete\n element type as a Fixed{$T,f} type"))
end

# This version is used by the deserializer to convert UInt8 buffers
Expand Down
10 changes: 8 additions & 2 deletions src/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,17 @@ function colorim{T<:Union{Int8,Int16,Int32,Int64,Int128}}(A::AbstractArray{T})
throw(ArgumentError("colorim does not support arrays of element type $T.\n If all values are positive, consider using normedview([U], mappedarray($(unsigned(T)), A)).\n Or convert to floating point."))
end

if isdefined(Core, :UnionAll)
isparametric1{CV<:Color}(::Type{CV}) = !Base.isabstract(CV) && length(Base.unwrap_unionall(CV).parameters) == 1
else
isparametric1{CV<:Color}(::Type{CV}) = !(CV.abstract) && length(CV.parameters) == 1
end

colorspacedict = Dict{String,Any}()
for ACV in (Color, AbstractRGB)
for CV in subtypes(ACV)
(length(CV.parameters) == 1 && !(CV.abstract)) || continue
str = string(CV.name.name)
isparametric1(CV) || continue
str = colorant_string(CV)
colorspacedict[str] = CV
end
end
Expand Down
41 changes: 31 additions & 10 deletions src/stackedviews.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
immutable StackedView{T<:Number,N,A<:Tuple{Vararg{AbstractArray}}} <: AbstractArray{T,N}
parents::A

function StackedView(parents::Tuple{Vararg{AbstractArray{T}}})
inds = indices(parents[1])
length(inds) == N-1 || throw(DimensionMismatch("component arrays must be of dimension $(N-1), got $(length(inds))"))
for i = 2:length(parents)
indices(parents[i]) == inds || throw(DimensionMismatch("all arrays must have the same indices, got $inds and $(indices(parents[i]))"))
if VERSION < v"0.6.0-dev.2741"
# using the version below breaks inferrence on 0.5, so lets use the old syntax here
include_string("""
immutable StackedView{T<:Number,N,A<:Tuple{Vararg{AbstractArray}}} <: AbstractArray{T,N}
parents::A
function StackedView(parents::Tuple{Vararg{AbstractArray{T}}})
inds = indices(parents[1])
length(inds) == N-1 || throw(DimensionMismatch("component arrays must be of dimension \$(N-1), got \$(length(inds))"))
for i = 2:length(parents)
indices(parents[i]) == inds || throw(DimensionMismatch("all arrays must have the same indices, got \$inds and \$(indices(parents[i]))"))
end
new(parents)
end
new(parents)
end
""")
else
include_string("""
struct StackedView{T<:Number,N,A<:Tuple{Vararg{AbstractArray{T}}}} <: AbstractArray{T,N}
parents::A
function (::Type{StackedView{T,N,A}}){T,N,A}(parents::A)
inds = indices(parents[1])
length(inds) == N-1 || throw(DimensionMismatch("component arrays must be of dimension \$(N-1), got \$(length(inds))"))
for i = 2:length(parents)
indices(parents[i]) == inds || throw(DimensionMismatch("all arrays must have the same indices, got \$inds and \$(indices(parents[i]))"))
end
new{T,N,A}(parents)
end
end
""")
end


"""
StackedView(B, C, ...) -> A
Expand Down Expand Up @@ -128,7 +149,7 @@ end
# to use tuple tricks (i.e., make a tuple of length(inds)+1)
_stackedview(T, (length(arrays), inds...), arrays_T)
end
_stackedview{T,N}(::Type{T}, ::NTuple{N}, arrays) = StackedView{T,N,typeof(arrays)}(arrays)
_stackedview{T,N}(::Type{T}, ::Tuple{Vararg{Any,N}}, arrays) = StackedView{T,N,typeof(arrays)}(arrays)


@inline firstinds(A::AbstractArray, Bs...) = indices(A)
Expand Down
4 changes: 2 additions & 2 deletions test/convert_reinterpret.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ using Base.Test
end

# indeterminate type tests
a = Array(RGB{AbstractFloat},3)
a = Array{RGB{AbstractFloat}}(3)
@test_throws ErrorException reinterpret(Float64, a)
Tu = TypeVar(:T)
a = Array(RGB{Tu},3)
a = Array{RGB{Tu}}(3)
@test_throws ErrorException reinterpret(Float64, a)

# Invalid conversions
Expand Down
6 changes: 5 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ module ImageCoreTests

using ImageCore, Base.Test

@test isempty(detect_ambiguities(ImageCore,Base,Core))
if VERSION <= v"0.6.0-dev"
@test isempty(detect_ambiguities(ImageCore,Base,Core))
end

include("colorchannels.jl")
include("views.jl")
Expand All @@ -11,11 +13,13 @@ include("traits.jl")
include("map.jl")
include("functions.jl")
include("show.jl")
info("Beginning of tests with deprecation warnings")
include("deprecated.jl")

# run these last
isCI = haskey(ENV, "CI") || get(ENV, "JULIA_PKGEVAL", false)
if Base.JLOptions().can_inline == 1 && !isCI
info("running benchmarks")
include("benchmarks.jl") # these fail if inlining is off
end

Expand Down
12 changes: 9 additions & 3 deletions test/show.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
using ImageCore, Colors, FixedPointNumbers, OffsetArrays, Base.Test

if VERSION >= v"0.6.0-dev.2505"
tformat(x...) = join(string.(x), ", ")
else
tformat(x...) = join(map(string, x), ",")
end

@testset "show" begin
rgb32 = rand(RGB{Float32}, 3, 5)
a = ChannelView(rgb32)
Expand All @@ -13,11 +19,11 @@ using ImageCore, Colors, FixedPointNumbers, OffsetArrays, Base.Test
@test summary(rgb8) == "3×5 Array{RGB{N0f8},2}"
rand8 = rand(UInt8, 3, 5)
d = normedview(permuteddimsview(rand8, (2,1)))
@test summary(d) == "5×3 normedview(N0f8, permuteddimsview(::Array{UInt8,2}, (2,1))) with element type FixedPointNumbers.Normed{UInt8,8}"
@test summary(d) == "5×3 normedview(N0f8, permuteddimsview(::Array{UInt8,2}, $(tformat((2,1))))) with element type FixedPointNumbers.Normed{UInt8,8}"
e = permuteddimsview(normedview(rand8), (2,1))
@test summary(e) == "5×3 permuteddimsview(::Array{N0f8,2}, (2,1)) with element type FixedPointNumbers.Normed{UInt8,8}"
@test summary(e) == "5×3 permuteddimsview(::Array{N0f8,2}, $(tformat((2,1)))) with element type FixedPointNumbers.Normed{UInt8,8}"
f = permuteddimsview(normedview(N0f16, rand(UInt16, 3, 5)), (2,1))
@test summary(f) == "5×3 permuteddimsview(::Array{N0f16,2}, (2,1)) with element type FixedPointNumbers.Normed{UInt16,16}"
@test summary(f) == "5×3 permuteddimsview(::Array{N0f16,2}, $(tformat((2,1)))) with element type FixedPointNumbers.Normed{UInt16,16}"
g = channelview(rgb8)
@test summary(g) == "3×3×5 Array{N0f8,3}"
h = OffsetArray(rgb8, -1:1, -2:2)
Expand Down
2 changes: 1 addition & 1 deletion test/views.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# some views are in colorchannels.jl
using Colors, ImageCore, Base.Test
using Colors, FixedPointNumbers, ImageCore, Base.Test

@testset "rawview" begin
a = map(N0f8, rand(3,5))
Expand Down

0 comments on commit b31e927

Please sign in to comment.