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

Don't write Bool as bool #40

Merged
merged 2 commits into from
Sep 10, 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
7 changes: 6 additions & 1 deletion src/NRRD.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
26 changes: 26 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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))
Expand Down