Skip to content

Commit

Permalink
Merge pull request #29 from JuliaIO/teh/warn
Browse files Browse the repository at this point in the history
warn -> atsign-warn, and fix loading of too-small grayscale images
  • Loading branch information
timholy authored Feb 3, 2019
2 parents 56bbd5b + 60e0284 commit 569413d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/NRRD.jl
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ function load(io::Stream{format"NRRD"}, Tuser::Type=Any; mode="r", mmap=:auto)
do_mmap |= can_mmap && (mmap == true)

if !compressed
szraw = checked_size(Traw, szraw, sz, iodata)
szraw, sz = checked_size(Traw, szraw, sz, iodata)
end

if do_mmap
Expand Down Expand Up @@ -775,7 +775,7 @@ function get_axes(header, nd)
for idx in findall(istime)
labelt = axnames[idx]
if !istimeaxis(Axis{Symbol(labelt)})
warn("label $labelt is not defined as a time axis, define it with `@traitimpl TimeAxis{Axis{:$labelt}}` (see ImageAxes for more information)")
@warn("label $labelt is not defined as a time axis, define it with `@traitimpl TimeAxis{Axis{:$labelt}}` (see ImageAxes for more information)")
end
end
end
Expand Down Expand Up @@ -1188,10 +1188,10 @@ function checked_size(Traw, szraw, sz, iodata)
sznew[k] = div(datalen, strds[k])
end
tsznew = (sznew...,)
warn("header indicates an array size $szraw, but the file size is consistent with at most $tsznew")
szraw = tsznew
@warn("header indicates an array size $szraw, but the file size is consistent with at most $tsznew")
szraw = sz = tsznew
end
szraw
szraw, sz
end

function stream2name(s::IO)
Expand Down
23 changes: 22 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using FileIO, FixedPointNumbers, ColorTypes, Unitful, AxisArrays, ImageAxes, ImageMetadata
using Test
using Test, Base.CoreLogging

include("unu-make.jl")

Expand Down Expand Up @@ -159,6 +159,27 @@ include("unu-make.jl")
@test pixelspacing(imgc) == (1.0u"mm", 1.2u"mm")
end

@testset "Size warning" begin
for (T, Traw) in ((N0f8, UInt8), (N0f16, UInt16))
img = rand(T, 5, 7, 3)
outname = joinpath(writedir, "imagetrunc.nhdr")
props = Dict("datafile"=>joinpath(writedir, "imagetrunc.raw"))
save(outname, img, props=props)
# Create a too-small data array
open(props["datafile"], "w") do io
write(io, rand(Traw, 5, 7, 2))
end
# Test that it can be read but produces a warning
logger = Test.TestLogger()
with_logger(logger) do
imgr = load(outname)
@test size(imgr) == (5, 7, 2)
end
record = logger.logs[1]
@test occursin("header indicates an array size (5, 7, 3), but the file size is consistent with at most (5, 7, 2)", record.message)
end
end

GC.gc() # to close any mmapped files
try
rm(workdir, recursive=true)
Expand Down

0 comments on commit 569413d

Please sign in to comment.