0001 function avw_hdr_write(avw, fileprefix, machine, verbose)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039 if ~exist('verbose','var'), verbose = 1; end
0040 if ~exist('machine','var'), machine = 'ieee-le'; end
0041
0042 if verbose,
0043 version = '[$Revision: 1.1 $]';
0044 fprintf('AVW_HDR_WRITE [v%s]\n',version(12:16)); tic;
0045 end
0046
0047
0048
0049
0050 if ~exist('avw','var'),
0051 warning('...no input avw - calling avw_hdr_make\n');
0052 avw = avw_hdr_make;
0053 elseif isempty(avw),
0054 warning('...empty input avw - calling avw_hdr_make\n');
0055 avw = avw_hdr_make;
0056 elseif ~isfield(avw,'hdr'),
0057 warning('...empty input avw.hdr - calling avw_hdr_make\n');
0058 avw = avw_hdr_make;
0059 end
0060 if ~isequal(avw.hdr.hk.sizeof_hdr,348),
0061 msg = sprintf('...avw.hdr.hk.sizeof_hdr must be 348!\n');
0062 error(msg);
0063 end
0064
0065 quit = 0;
0066 if ~exist('fileprefix','var'),
0067 if isfield(avw,'fileprefix'),
0068 if ~isempty(avw.fileprefix),
0069 fileprefix = avw.fileprefix;
0070 else,
0071 quit = 1;
0072 end
0073 else
0074 quit = 1;
0075 end
0076 if quit,
0077 helpwin avw_hdr_write;
0078 error('...no input fileprefix - see help avw_hdr_write\n\n');
0079 return;
0080 end
0081 end
0082
0083 if findstr('.hdr',fileprefix),
0084
0085 fileprefix = strrep(fileprefix,'.hdr','');
0086 end
0087
0088
0089
0090
0091
0092
0093 if verbose, tic; end
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111 fid = fopen(sprintf('%s.hdr',fileprefix),'w',machine);
0112 if fid < 0,
0113 msg = sprintf('Cannot write to file %s.hdr\n',fileprefix);
0114 error(msg);
0115 else
0116 if verbose, fprintf('...writing %s Analyze header.\n',machine); end
0117 write_header(fid,avw,verbose);
0118 end
0119
0120 if verbose, t=toc; fprintf('...done (%5.2f sec).\n\n',t); end
0121
0122 return
0123
0124
0125
0126
0127
0128
0129
0130 function write_header(fid,avw,verbose)
0131
0132 header_key(fid,avw.hdr.hk);
0133 image_dimension(fid,avw.hdr.dime);
0134 data_history(fid,avw.hdr.hist);
0135
0136
0137 fbytes = ftell(fid);
0138 fclose(fid);
0139 if ~isequal(fbytes,348),
0140 msg = sprintf('...file size is not 348 bytes!\n');
0141 warning(msg);
0142 end
0143
0144 return
0145
0146
0147
0148 function header_key(fid,hk)
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162 fseek(fid,0,'bof');
0163
0164 fwrite(fid, hk.sizeof_hdr(1), 'int32');
0165
0166 data_type = sprintf('%-10s',hk.data_type);
0167 fwrite(fid, hk.data_type(1:10), 'uchar');
0168
0169 db_name = sprintf('%-18s',hk.db_name);
0170 fwrite(fid, db_name(1:18), 'uchar');
0171
0172 fwrite(fid, hk.extents(1), 'int32');
0173 fwrite(fid, hk.session_error(1),'int16');
0174
0175 regular = sprintf('%1s',hk.regular);
0176 fwrite(fid, regular(1), 'uchar');
0177
0178
0179
0180 fwrite(fid, hk.hkey_un0(1), 'uint8');
0181
0182
0183
0184
0185
0186
0187
0188
0189 return
0190
0191
0192
0193 function image_dimension(fid,dime)
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224 fwrite(fid, dime.dim(1:8), 'int16');
0225 fwrite(fid, dime.vox_units(1:4),'uchar');
0226 fwrite(fid, dime.cal_units(1:8),'uchar');
0227 fwrite(fid, dime.unused1(1), 'int16');
0228 fwrite(fid, dime.datatype(1), 'int16');
0229 fwrite(fid, dime.bitpix(1), 'int16');
0230 fwrite(fid, dime.dim_un0(1), 'int16');
0231 fwrite(fid, dime.pixdim(1:8), 'float32');
0232 fwrite(fid, dime.vox_offset(1), 'float32');
0233
0234
0235 if dime.roi_scale == 0, dime.roi_scale = 0.00392157; end
0236 fwrite(fid, dime.roi_scale(1), 'float32');
0237
0238 fwrite(fid, dime.funused1(1), 'float32');
0239 fwrite(fid, dime.funused2(1), 'float32');
0240 fwrite(fid, dime.cal_max(1), 'float32');
0241 fwrite(fid, dime.cal_min(1), 'float32');
0242 fwrite(fid, dime.compressed(1), 'int32');
0243 fwrite(fid, dime.verified(1), 'int32');
0244 fwrite(fid, dime.glmax(1), 'int32');
0245 fwrite(fid, dime.glmin(1), 'int32');
0246
0247 return
0248
0249
0250
0251 function data_history(fid,hist)
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262
0263
0264
0265
0266
0267
0268
0269
0270
0271
0272
0273
0274
0275
0276 descrip = sprintf('%-80s', hist.descrip);
0277 aux_file = sprintf('%-24s', hist.aux_file);
0278 originator = sprintf('%-10s', hist.originator);
0279 generated = sprintf('%-10s', hist.generated);
0280 scannum = sprintf('%-10s', hist.scannum);
0281 patient_id = sprintf('%-10s', hist.patient_id);
0282 exp_date = sprintf('%-10s', hist.exp_date);
0283 exp_time = sprintf('%-10s', hist.exp_time);
0284 hist_un0 = sprintf( '%-3s', hist.hist_un0);
0285
0286
0287
0288
0289
0290
0291
0292
0293 if length(descrip) < 80,
0294 paddingN = 80-length(descrip);
0295 padding = char(repmat(double(' '),1,paddingN));
0296 descrip = [descrip,padding];
0297 end
0298 if length(aux_file) < 24,
0299 paddingN = 24-length(aux_file);
0300 padding = char(repmat(double(' '),1,paddingN));
0301 aux_file = [aux_file,padding];
0302 end
0303 if length(originator) < 10,
0304 paddingN = 10-length(originator);
0305 padding = char(repmat(double(' '),1,paddingN));
0306 originator = [originator, padding];
0307 end
0308 if length(generated) < 10,
0309 paddingN = 10-length(generated);
0310 padding = char(repmat(double(' '),1,paddingN));
0311 generated = [generated, padding];
0312 end
0313 if length(scannum) < 10,
0314 paddingN = 10-length(scannum);
0315 padding = char(repmat(double(' '),1,paddingN));
0316 scannum = [scannum, padding];
0317 end
0318 if length(patient_id) < 10,
0319 paddingN = 10-length(patient_id);
0320 padding = char(repmat(double(' '),1,paddingN));
0321 patient_id = [patient_id, padding];
0322 end
0323 if length(exp_date) < 10,
0324 paddingN = 10-length(exp_date);
0325 padding = char(repmat(double(' '),1,paddingN));
0326 exp_date = [exp_date, padding];
0327 end
0328 if length(exp_time) < 10,
0329 paddingN = 10-length(exp_time);
0330 padding = char(repmat(double(' '),1,paddingN));
0331 exp_time = [exp_time, padding];
0332 end
0333 if length(hist_un0) < 10,
0334 paddingN = 10-length(hist_un0);
0335 padding = char(repmat(double(' '),1,paddingN));
0336 hist_un0 = [hist_un0, padding];
0337 end
0338
0339
0340
0341
0342 if find(double(descrip)>128),
0343 indexStrangeChar = find(double(descrip)>128);
0344 descrip(indexStrangeChar) = ' ';
0345 end
0346 if find(double(aux_file)>128),
0347 indexStrangeChar = find(double(aux_file)>128);
0348 aux_file(indexStrangeChar) = ' ';
0349 end
0350 if find(double(originator)>128),
0351 indexStrangeChar = find(double(originator)>128);
0352 originator(indexStrangeChar) = ' ';
0353 end
0354 if find(double(generated)>128),
0355 indexStrangeChar = find(double(generated)>128);
0356 generated(indexStrangeChar) = ' ';
0357 end
0358 if find(double(scannum)>128),
0359 indexStrangeChar = find(double(scannum)>128);
0360 scannum(indexStrangeChar) = ' ';
0361 end
0362 if find(double(patient_id)>128),
0363 indexStrangeChar = find(double(patient_id)>128);
0364 patient_id(indexStrangeChar) = ' ';
0365 end
0366 if find(double(exp_date)>128),
0367 indexStrangeChar = find(double(exp_date)>128);
0368 exp_date(indexStrangeChar) = ' ';
0369 end
0370 if find(double(exp_time)>128),
0371 indexStrangeChar = find(double(exp_time)>128);
0372 exp_time(indexStrangeChar) = ' ';
0373 end
0374 if find(double(hist_un0)>128),
0375 indexStrangeChar = find(double(hist_un0)>128);
0376 hist_un0(indexStrangeChar) = ' ';
0377 end
0378
0379
0380
0381
0382 fwrite(fid, descrip(1:80), 'uchar');
0383 fwrite(fid, aux_file(1:24), 'uchar');
0384
0385
0386
0387
0388 fwrite(fid, hist.orient(1), 'uint8');
0389
0390 fwrite(fid, originator(1:10), 'uchar');
0391 fwrite(fid, generated(1:10), 'uchar');
0392 fwrite(fid, scannum(1:10), 'uchar');
0393 fwrite(fid, patient_id(1:10), 'uchar');
0394 fwrite(fid, exp_date(1:10), 'uchar');
0395 fwrite(fid, exp_time(1:10), 'uchar');
0396 fwrite(fid, hist_un0(1:3), 'uchar');
0397
0398 fwrite(fid, hist.views(1), 'int32');
0399 fwrite(fid, hist.vols_added(1), 'int32');
0400 fwrite(fid, hist.start_field(1),'int32');
0401 fwrite(fid, hist.field_skip(1), 'int32');
0402 fwrite(fid, hist.omax(1), 'int32');
0403 fwrite(fid, hist.omin(1), 'int32');
0404 fwrite(fid, hist.smax(1), 'int32');
0405 fwrite(fid, hist.smin(1), 'int32');
0406
0407 return
0408
0409
0410
0411
0412
0413
0414
0415
0416
0417
0418
0419
0420
0421
0422
0423
0424
0425
0426
0427