0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 function save_nii_hdr(hdr, fid)
0018
0019 if ~exist('hdr','var') | ~exist('fid','var')
0020 error('Usage: save_nii_hdr(hdr, fid)');
0021 end
0022
0023 if ~isequal(hdr.hk.sizeof_hdr,348),
0024 error('hdr.hk.sizeof_hdr must be 348.');
0025 end
0026
0027 if hdr.hist.qform_code == 0 & hdr.hist.sform_code == 0
0028 hdr.hist.sform_code = 1;
0029 hdr.hist.srow_x(1) = hdr.dime.pixdim(2);
0030 hdr.hist.srow_x(2) = 0;
0031 hdr.hist.srow_x(3) = 0;
0032 hdr.hist.srow_y(1) = 0;
0033 hdr.hist.srow_y(2) = hdr.dime.pixdim(3);
0034 hdr.hist.srow_y(3) = 0;
0035 hdr.hist.srow_z(1) = 0;
0036 hdr.hist.srow_z(2) = 0;
0037 hdr.hist.srow_z(3) = hdr.dime.pixdim(4);
0038 hdr.hist.srow_x(4) = (1-hdr.hist.originator(1))*hdr.dime.pixdim(2);
0039 hdr.hist.srow_y(4) = (1-hdr.hist.originator(2))*hdr.dime.pixdim(3);
0040 hdr.hist.srow_z(4) = (1-hdr.hist.originator(3))*hdr.dime.pixdim(4);
0041 end
0042
0043 write_header(hdr, fid);
0044
0045 return;
0046
0047
0048
0049 function write_header(hdr, fid)
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059 header_key(fid, hdr.hk);
0060 image_dimension(fid, hdr.dime);
0061 data_history(fid, hdr.hist);
0062
0063
0064
0065 fbytes = ftell(fid);
0066
0067 if ~isequal(fbytes,348),
0068 msg = sprintf('Header size is not 348 bytes.');
0069 warning(msg);
0070 end
0071
0072 return;
0073
0074
0075
0076 function header_key(fid, hk)
0077
0078 fseek(fid,0,'bof');
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092 fwrite(fid, hk.sizeof_hdr(1), 'int32');
0093
0094
0095
0096 pad = zeros(1, 10-length(hk.data_type));
0097 hk.data_type = [hk.data_type char(pad)];
0098 fwrite(fid, hk.data_type(1:10), 'uchar');
0099
0100
0101
0102 pad = zeros(1, 18-length(hk.db_name));
0103 hk.db_name = [hk.db_name char(pad)];
0104 fwrite(fid, hk.db_name(1:18), 'uchar');
0105
0106 fwrite(fid, hk.extents(1), 'int32');
0107 fwrite(fid, hk.session_error(1), 'int16');
0108 fwrite(fid, hk.regular(1), 'uchar');
0109
0110
0111
0112 fwrite(fid, hk.dim_info(1), 'uchar');
0113
0114 return;
0115
0116
0117
0118 function image_dimension(fid, dime)
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154 fwrite(fid, dime.dim(1:8), 'int16');
0155 fwrite(fid, dime.intent_p1(1), 'float32');
0156 fwrite(fid, dime.intent_p2(1), 'float32');
0157 fwrite(fid, dime.intent_p3(1), 'float32');
0158 fwrite(fid, dime.intent_code(1), 'int16');
0159 fwrite(fid, dime.datatype(1), 'int16');
0160 fwrite(fid, dime.bitpix(1), 'int16');
0161 fwrite(fid, dime.slice_start(1), 'int16');
0162 fwrite(fid, dime.pixdim(1:8), 'float32');
0163 fwrite(fid, dime.vox_offset(1), 'float32');
0164 fwrite(fid, dime.scl_slope(1), 'float32');
0165 fwrite(fid, dime.scl_inter(1), 'float32');
0166 fwrite(fid, dime.slice_end(1), 'int16');
0167 fwrite(fid, dime.slice_code(1), 'uchar');
0168 fwrite(fid, dime.xyzt_units(1), 'uchar');
0169 fwrite(fid, dime.cal_max(1), 'float32');
0170 fwrite(fid, dime.cal_min(1), 'float32');
0171 fwrite(fid, dime.slice_duration(1), 'float32');
0172 fwrite(fid, dime.toffset(1), 'float32');
0173 fwrite(fid, dime.glmax(1), 'int32');
0174 fwrite(fid, dime.glmin(1), 'int32');
0175
0176 return;
0177
0178
0179
0180 function data_history(fid, hist)
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204 pad = zeros(1, 80-length(hist.descrip));
0205 hist.descrip = [hist.descrip char(pad)];
0206 fwrite(fid, hist.descrip(1:80), 'uchar');
0207
0208
0209
0210 pad = zeros(1, 24-length(hist.aux_file));
0211 hist.aux_file = [hist.aux_file char(pad)];
0212 fwrite(fid, hist.aux_file(1:24), 'uchar');
0213
0214 fwrite(fid, hist.qform_code, 'int16');
0215 fwrite(fid, hist.sform_code, 'int16');
0216 fwrite(fid, hist.quatern_b, 'float32');
0217 fwrite(fid, hist.quatern_c, 'float32');
0218 fwrite(fid, hist.quatern_d, 'float32');
0219 fwrite(fid, hist.qoffset_x, 'float32');
0220 fwrite(fid, hist.qoffset_y, 'float32');
0221 fwrite(fid, hist.qoffset_z, 'float32');
0222 fwrite(fid, hist.srow_x(1:4), 'float32');
0223 fwrite(fid, hist.srow_y(1:4), 'float32');
0224 fwrite(fid, hist.srow_z(1:4), 'float32');
0225
0226
0227
0228 pad = zeros(1, 16-length(hist.intent_name));
0229 hist.intent_name = [hist.intent_name char(pad)];
0230 fwrite(fid, hist.intent_name(1:16), 'uchar');
0231
0232
0233
0234 pad = zeros(1, 4-length(hist.magic));
0235 hist.magic = [hist.magic char(pad)];
0236 fwrite(fid, hist.magic(1:4), 'uchar');
0237
0238 return;
0239