diff --git a/src/NRRD.jl b/src/NRRD.jl index b5eabaa..978a455 100644 --- a/src/NRRD.jl +++ b/src/NRRD.jl @@ -58,7 +58,12 @@ const string2type = Dict( type2string(::Type{Float16}) = "float16" type2string(::Type{Float32}) = "float" type2string(::Type{Float64}) = "double" -type2string(::Type{T}) where {T<:Integer} = lowercase(string(T.name.name)) +type2string(::Type{Bool}) = "uint8" # bool is not supported +function type2string(::Type{T}) where {T<:Integer} + str = lowercase(string(T.name.name)) + haskey(string2type, str) && return str + error("integer type $T not supported") +end type2string(::Type{Normed{T,f}}) where {T<:Unsigned,f} = type2string(T) type2string(::Type{T}) where {T} = type2string(eltype(T), T) type2string(::Type{T}, ::Type{T}) where {T} = error("type $T unrecognized") diff --git a/test/runtests.jl b/test/runtests.jl index 74bb97f..01e5f3a 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,6 +1,15 @@ using FileIO, ImageCore, Unitful, AxisArrays, ImageAxes, ImageMetadata using Test, Base.CoreLogging +struct UnsupportedInt16 <: Signed + val::Int16 +end +Base.flipsign(x::UnsupportedInt16, y::UnsupportedInt16) = UnsupportedInt16(flipsign(x.val, y.val)) +Base.Unsigned(x::UnsupportedInt16) = Unsigned(x.val) +Base.UInt16(x::UnsupportedInt16) = UInt16(x.val) +Base.Int(x::UnsupportedInt16) = Int(x.val) +Base.promote_rule(::Type{UnsupportedInt16}, ::Type{Int}) = Int + include("unu-make.jl") @testset "NRRD" begin @@ -102,6 +111,23 @@ include("unu-make.jl") end end + @testset "bool" begin + # Bool is not one of the supported eltypes in the `type` field of + # http://teem.sourceforge.net/nrrd/format.html. + # Make sure we don't do something wrong + fn = joinpath(writedir, "eltype_bool.nrrd") + img = rand(Bool, 5, 6) + save(fn, img) + imgr = load(fn) + @test imgr == img + # We don't insist on eltype(imgr) == Bool + img = UnsupportedInt16[1 2; 3 4] + @info "The following error is expected" + fn = joinpath(writedir, "eltype_unsupported.nrrd") + ex = try save(fn, img) catch e e end + @test ex.ex == ErrorException("integer type UnsupportedInt16 not supported") + end + @testset "Mmapped" begin fn = joinpath(writedir, "volume.nrrd") save(fn, zeros(UInt8,500,500,500))