-
Notifications
You must be signed in to change notification settings - Fork 0
/
ntools_elec_save_nifti.m
180 lines (156 loc) · 5.13 KB
/
ntools_elec_save_nifti.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
function err = ntools_elec_save_nifti(hdr,niftifile)
% err = save_nifti(hdr,niftifile)
%
% Pixel data should be in hdr.vol
%
% Handles data structures with more than 32k cols by
% setting hdr.dim(2) = -1 and hdr.glmin = ncols. This
% is FreeSurfer specific, for handling surfaces.
%
% $Id: save_nifti.m,v 1.8 2007/05/22 05:24:11 greve Exp $
%
% save_nifti.m
%
% Original Author: Doug Greve
% CVS Revision Info:
% $Author: greve $
% $Date: 2007/05/22 05:24:11 $
% $Revision: 1.8 $
%
% Copyright (C) 2002-2007,
% The General Hospital Corporation (Boston, MA).
% All rights reserved.
%
% Distribution, usage and copying of this software is covered under the
% terms found in the License Agreement file named 'COPYING' found in the
% FreeSurfer source code root directory, and duplicated here:
% https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferOpenSourceLicense
%
% General inquiries: freesurfer@nmr.mgh.harvard.edu
% Bug reports: analysis-bugs@nmr.mgh.harvard.edu
%
err = 1;
if(nargin ~= 2)
fprintf('err = save_nifti(hdr,niftifile)\n');
return;
end
ext = niftifile((strlen(niftifile)-2):strlen(niftifile));
if(strcmpi(ext,'.gz'))
gzip_needed = 1;
niftifile = niftifile(1:strlen(niftifile)-3);
%fprintf('First, saving to %s before compressing\n',niftifile);
else
gzip_needed = 0;
end
fp = fopen(niftifile,'w');
if(fp == -1)
fprintf('ERROR: could not open %s\n',niftifile);
return;
end
hdr.data_type = [hdr.data_type(:)' repmat(' ',[1 10])];
hdr.data_type = hdr.data_type(1:10);
hdr.db_name = [hdr.data_type(:)' repmat(' ',[1 18])];
hdr.db_name = hdr.db_name(1:18);
hdr.dim = ones(1,8);
hdr.dim(1) = 4;
hdr.dim(2) = size(hdr.vol,1);
hdr.dim(3) = size(hdr.vol,2);
hdr.dim(4) = size(hdr.vol,3);
hdr.dim(5) = size(hdr.vol,4);
% This is to accomodate structures with more than 32k cols
% FreeSurfer specific. See also mriio.c.
if(hdr.dim(2) > 2^15)
hdr.glmin = hdr.dim(2);
hdr.dim(2) = -1;
end
hdr.pixdim = [hdr.pixdim(:)' repmat(0,[1 8])];
hdr.pixdim = hdr.pixdim(1:8);
hdr.descrip = [hdr.descrip(:)' repmat(' ',[1 80])];
hdr.descrip = hdr.descrip(1:80);
hdr.aux_file = [hdr.aux_file(:)' repmat(' ',[1 24])];
hdr.aux_file = hdr.aux_file(1:24);
hdr.intent_name = [hdr.intent_name(:)' repmat(' ',[1 16])];
hdr.intent_name = hdr.intent_name(1:16);
hdr.magic = [hdr.magic(:)' repmat(0,[1 4])];
hdr.magic = hdr.magic(1:4);
if(isempty(hdr.regular)) hdr.regular = ' '; end
if(isempty(hdr.dim_info)) hdr.dim_info = ' '; end
if(isempty(hdr.slice_code)) hdr.slice_code = ' '; end
if(isempty(hdr.xyzt_units)) hdr.xyzt_units = ' '; end % should be err
hdr.vox_offset = 352; % not 348
fwrite(fp,348,'int');
fwrite(fp,hdr.data_type, 'char');
fwrite(fp,hdr.db_name, 'char');
fwrite(fp,hdr.extents, 'int');
fwrite(fp,hdr.session_error,'short');
fwrite(fp,hdr.regular, 'char');
fwrite(fp,hdr.dim_info, 'char');
fwrite(fp,hdr.dim, 'short');
fwrite(fp,hdr.intent_p1, 'float');
fwrite(fp,hdr.intent_p2, 'float');
fwrite(fp,hdr.intent_p3, 'float');
fwrite(fp,hdr.intent_code, 'short');
fwrite(fp,hdr.datatype, 'short');
fwrite(fp,hdr.bitpix, 'short');
fwrite(fp,hdr.slice_start, 'short');
fwrite(fp,hdr.pixdim, 'float');
fwrite(fp,hdr.vox_offset, 'float');
fwrite(fp,hdr.scl_slope, 'float');
fwrite(fp,hdr.scl_inter, 'float');
fwrite(fp,hdr.slice_end, 'short');
fwrite(fp,hdr.slice_code, 'char');
fwrite(fp,hdr.xyzt_units, 'char');
fwrite(fp,hdr.cal_max, 'float');
fwrite(fp,hdr.cal_min, 'float');
fwrite(fp,hdr.slice_duration,'float');
fwrite(fp,hdr.toffset, 'float');
fwrite(fp,hdr.glmax, 'int');
fwrite(fp,hdr.glmin, 'int');
fwrite(fp,hdr.descrip, 'char');
fwrite(fp,hdr.aux_file, 'char');
fwrite(fp,hdr.qform_code, 'short');
fwrite(fp,hdr.sform_code, 'short');
fwrite(fp,hdr.quatern_b, 'float');
fwrite(fp,hdr.quatern_c, 'float');
fwrite(fp,hdr.quatern_d, 'float');
fwrite(fp,hdr.quatern_x, 'float');
fwrite(fp,hdr.quatern_y, 'float');
fwrite(fp,hdr.quatern_z, 'float');
fwrite(fp,hdr.srow_x, 'float');
fwrite(fp,hdr.srow_y, 'float');
fwrite(fp,hdr.srow_z, 'float');
fwrite(fp,hdr.intent_name, 'char');
fwrite(fp,hdr.magic, 'char');
% Pad to get to 352 bytes (header size is 348)
fwrite(fp,0,'char');
fwrite(fp,0,'char');
fwrite(fp,0,'char');
fwrite(fp,0,'char');
npix = prod(size(hdr.vol));
switch(hdr.datatype)
case 2, nitemswritten = fwrite(fp,hdr.vol,'uchar');
case 4, nitemswritten = fwrite(fp,hdr.vol,'short');
case 8, nitemswritten = fwrite(fp,hdr.vol,'int');
case 16, nitemswritten = fwrite(fp,hdr.vol,'float');
case 64, nitemswritten = fwrite(fp,hdr.vol,'double');
case 512, nitemswritten = fwrite(fp,hdr.vol,'ushort');
case 768, nitemswritten = fwrite(fp,hdr.vol,'uint');
otherwise,
fprintf('WARNING: data type %d not supported, but writing as float',...
hdr.datatype);
nitemswritten = fwrite(fp,hdr.vol,'float');
return;
end
fclose(fp);
if(npix ~= nitemswritten)
fprintf('ERROR: tried to write %d, but only wrote %d',npix,nitemswritten);
return;
end
if(gzip_needed)
cmd = sprintf('gzip -f %s', niftifile);
%fprintf('Compressing with\n');
%fprintf(' %s\n',cmd);
unix(cmd);
end
err = 0;
return;