From 60e028449d6af0a2f1465c2333411822154abd13 Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Sun, 3 Feb 2019 10:43:54 -0600 Subject: [PATCH] warn -> atsign-warn, and fix loading of too-small grayscale images --- src/NRRD.jl | 10 +++++----- test/runtests.jl | 23 ++++++++++++++++++++++- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/NRRD.jl b/src/NRRD.jl index ccd350a..9760705 100644 --- a/src/NRRD.jl +++ b/src/NRRD.jl @@ -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 @@ -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 @@ -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) diff --git a/test/runtests.jl b/test/runtests.jl index 65dec67..cfed718 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,5 +1,5 @@ using FileIO, FixedPointNumbers, ColorTypes, Unitful, AxisArrays, ImageAxes, ImageMetadata -using Test +using Test, Base.CoreLogging include("unu-make.jl") @@ -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)