-
Notifications
You must be signed in to change notification settings - Fork 2
/
nifticreate.m
50 lines (46 loc) · 1.65 KB
/
nifticreate.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
function header = nifticreate(img, format)
%
% header=nifticreate(img)
% or
% header=nifticreate(img,format)
%
% Create a default NIfTI header
%
% author: Qianqian Fang (q.fang <at> neu.edu)
%
% input:
% img: the image data matching the header
% format: can only be 'nifti1'. can be ignored
%
% output:
% header: a struct that is byte-wise compatible with NIfTI-1
%
% this file is part of JNIfTI specification: https://github.com/NeuroJSON/jnifti
%
% License: Apache 2.0, see https://github.com/NeuroJSON/jnifti for details
%
if (nargin < 2)
format = 'nifti1';
end
datatype = struct('int8', 256, 'int16', 4, 'int32', 8, 'int64', 1024, 'uint8', 2, 'uint16', 512, 'uint32', 768, 'uint64', 1280, 'single', 16, 'double', 64);
if (strcmp(format, 'nifti1'))
headerlen = 348;
else
headerlen = 540;
end
header = memmapstream(uint8(zeros(1, headerlen + 4)), niiformat(format));
header.sizeof_hdr = cast(headerlen, class(header.sizeof_hdr));
header.datatype = cast(datatype.(class(img)), class(header.datatype));
header.dim(1:end) = cast(1, class(header.dim));
header.dim(1:ndims(img) + 1) = cast([ndims(img), size(img)], class(header.dim));
header.pixdim(1:ndims(img) + 1) = cast(1, class(header.pixdim));
header.vox_offset = cast(headerlen + 4, class(header.vox_offset));
if (header.sizeof_hdr == 540)
header.magic(1:3) = cast('ni2', class(header.magic));
else
header.magic(1:3) = cast('ni1', class(header.magic));
end
header.srow_x(1) = cast(1, class(header.srow_x));
header.srow_y(2) = cast(1, class(header.srow_y));
header.srow_z(3) = cast(1, class(header.srow_z));
header.sform_code = cast(1, class(header.sform_code));