diff --git a/src/NIfTI.jl b/src/NIfTI.jl index 9e5a0e1..4294287 100644 --- a/src/NIfTI.jl +++ b/src/NIfTI.jl @@ -3,7 +3,7 @@ module NIfTI using CodecZlib, Mmap, MappedArrays, TranscodingStreams import Base.getindex, Base.size, Base.ndims, Base.length, Base.write, Base64 -export NIVolume, niread, niwrite, voxel_size, time_step, vox, getaffine, setaffine +export NIVolume, niread, niwrite, voxel_size, time_step, vox, getaffine, setaffine, new_vol_like include("parsers.jl") include("extensions.jl") @@ -176,6 +176,15 @@ function NIVolume( (orientation[2, :]...,), (orientation[3, :]...,), string_tuple(intent_name, 16), NP1_MAGIC), extensions, raw) end +""" + new_vol_like(vol::NIVolume{T}, raw::R) where {R,T} + +Convenience function to create a new NIfTI volume with data `raw`, using +the header and extensions of `vol`. +""" +new_vol_like(vol::NIVolume{T}, raw::R) where {R,T} = NIVolume(vol.header,vol.extensions,raw) + + # Validates the header of a volume and updates it to match the volume's contents function niupdate(vol::NIVolume{T}) where {T} vol.header.sizeof_hdr = SIZEOF_HDR1 diff --git a/test/runtests.jl b/test/runtests.jl index 29c85c9..a072738 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -93,6 +93,14 @@ const TEMP_GZIPPED_FILE = joinpath(TEMP_DIR_NAME, "$(tempname()).nii.gz") niwrite(TEMP_GZIPPED_FILE, vol) niread(TEMP_GZIPPED_FILE) +# Test new_img_like +img = niread(GZIPPED_NII) +test_raw = ones(size(img)) +new_img = new_vol_like(img, test_raw) +@test new_img.header == img.header +@test new_img.raw == test_raw +@test new_img.extensions == img.extensions + # Write and read DT_BINARY const BOOL_WRITE = joinpath(TEMP_DIR_NAME, "$(tempname()).nii") const BIT_WRITE = joinpath(TEMP_DIR_NAME, "$(tempname()).nii")