0001 function [hdr,otherendian] = spm_read_hdr(fname)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 fid = fopen(fname,'r','native');
0014 otherendian = 0;
0015 if (fid > 0)
0016 dime = read_dime(fid);
0017 if dime.dim(1)<0 | dime.dim(1)>15,
0018
0019 fclose(fid);
0020 if spm_platform('bigend'), fid = fopen(fname,'r','ieee-le');
0021 else, fid = fopen(fname,'r','ieee-be'); end;
0022 otherendian = 1;
0023 dime = read_dime(fid);
0024 end;
0025 hk = read_hk(fid);
0026 hist = read_hist(fid);
0027 hdr.hk = hk;
0028 hdr.dime = dime;
0029 hdr.hist = hist;
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039 fclose(fid);
0040 else,
0041 hdr = [];
0042 otherendian = NaN;
0043
0044 end;
0045 return;
0046
0047
0048 function hk = read_hk(fid)
0049
0050
0051 fseek(fid,0,'bof');
0052 hk.sizeof_hdr = fread(fid,1,'int32');
0053 hk.data_type = mysetstr(fread(fid,10,'uchar'))';
0054 hk.db_name = mysetstr(fread(fid,18,'uchar'))';
0055 hk.extents = fread(fid,1,'int32');
0056 hk.session_error = fread(fid,1,'int16');
0057 hk.regular = mysetstr(fread(fid,1,'uchar'))';
0058 hk.hkey_un0 = mysetstr(fread(fid,1,'uchar'))';
0059 if isempty(hk.hkey_un0), error(['Problem reading "hk" of header file (' fopen(fid) ').']); end;
0060 return;
0061
0062
0063 function dime = read_dime(fid)
0064
0065
0066 fseek(fid,40,'bof');
0067 dime.dim = fread(fid,8,'int16')';
0068 dime.vox_units = mysetstr(fread(fid,4,'uchar'))';
0069 dime.cal_units = mysetstr(fread(fid,8,'uchar'))';
0070 dime.unused1 = fread(fid,1,'int16');
0071 dime.datatype = fread(fid,1,'int16');
0072 dime.bitpix = fread(fid,1,'int16');
0073 dime.dim_un0 = fread(fid,1,'int16');
0074 dime.pixdim = fread(fid,8,'float')';
0075 dime.vox_offset = fread(fid,1,'float');
0076 dime.funused1 = fread(fid,1,'float');
0077 dime.funused2 = fread(fid,1,'float');
0078 dime.funused3 = fread(fid,1,'float');
0079 dime.cal_max = fread(fid,1,'float');
0080 dime.cal_min = fread(fid,1,'float');
0081 dime.compressed = fread(fid,1,'int32');
0082 dime.verified = fread(fid,1,'int32');
0083 dime.glmax = fread(fid,1,'int32');
0084 dime.glmin = fread(fid,1,'int32');
0085 if isempty(dime.glmin), error(['Problem reading "dime" of header file (' fopen(fid) ').']); end;
0086 return;
0087
0088
0089 function hist = read_hist(fid)
0090
0091
0092 fseek(fid,148,'bof');
0093 hist.descrip = mysetstr(fread(fid,80,'uchar'))';
0094 hist.aux_file = mysetstr(fread(fid,24,'uchar'))';
0095 hist.orient = fread(fid,1,'uchar');
0096 hist.origin = fread(fid,5,'int16')';
0097 hist.generated = mysetstr(fread(fid,10,'uchar'))';
0098 hist.scannum = mysetstr(fread(fid,10,'uchar'))';
0099 hist.patient_id = mysetstr(fread(fid,10,'uchar'))';
0100 hist.exp_date = mysetstr(fread(fid,10,'uchar'))';
0101 hist.exp_time = mysetstr(fread(fid,10,'uchar'))';
0102 hist.hist_un0 = mysetstr(fread(fid,3,'uchar'))';
0103 hist.views = fread(fid,1,'int32');
0104 hist.vols_added = fread(fid,1,'int32');
0105 hist.start_field= fread(fid,1,'int32');
0106 hist.field_skip = fread(fid,1,'int32');
0107 hist.omax = fread(fid,1,'int32');
0108 hist.omin = fread(fid,1,'int32');
0109 hist.smax = fread(fid,1,'int32');
0110 hist.smin = fread(fid,1,'int32');
0111 if isempty(hist.smin), error(['Problem reading "hist" of header file (' fopen(fid) ').']); end;
0112 return;
0113
0114
0115 function spmf = read_spmf(fid,n)
0116
0117
0118
0119 fseek(fid,348,'bof');
0120 mgc = fread(fid,1,'int32');
0121 if mgc ~= 20020417, spmf = []; return; end;
0122
0123 for j=1:n,
0124 spmf(j).mat = fread(fid,16,'double');
0125 spmf(j).unused = fread(fid,384,'uchar');
0126 if length(spmf(j).unused)<384,
0127 error(['Problem reading "spmf" of header file (' fopen(fid) ').']);
0128 end;
0129 spmf(j).mat = reshape(spmf(j).mat,[4 4]);
0130 end;
0131 return;
0132
0133
0134 function out = mysetstr(in)
0135 tmp = find(in == 0);
0136 tmp = min([min(tmp) length(in)]);
0137 out = setstr([in(1:tmp)' zeros(1,length(in)-(tmp))])';
0138 return;
0139
0140