0001 function vol = read_hdr(fname)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 persistent d
0014 if isempty(d), d = getdict; end;
0015
0016 [pth,nam,ext] = fileparts(fname);
0017 switch ext
0018 case '.hdr'
0019 hname = fullfile(pth,[nam '.hdr']);
0020 case '.HDR'
0021 hname = fullfile(pth,[nam '.HDR']);
0022 case '.img'
0023 hname = fullfile(pth,[nam '.hdr']);
0024 case '.IMG'
0025 hname = fullfile(pth,[nam '.HDR']);
0026 case '.nii'
0027 hname = fullfile(pth,[nam '.nii']);
0028 case '.NII'
0029 hname = fullfile(pth,[nam '.NII']);
0030 otherwise
0031 hname = fullfile(pth,[nam ext]);
0032 end
0033 [hdr,be] = read_hdr_raw(hname);
0034 if isempty(hdr)
0035 error(['Error reading header file "' hname '"']);
0036 end;
0037
0038 if ~isfield(hdr,'magic'),
0039
0040 switch hdr.datatype,
0041 case 130, hdr.datatype = 256;
0042 case 132, hdr.datatype = 512;
0043 case 136, hdr.datatype = 768;
0044 end;
0045 end;
0046
0047 dt = [];
0048 for i=1:length(d.dtype)
0049 if hdr.datatype == d.dtype(i).code
0050 dt = d.dtype(i);
0051 break;
0052 end;
0053 end;
0054 if isempty(dt)
0055 error(['Unrecognised datatype (' num2str(double(hdr.datatype)) ') for "' fname '.'] );
0056 end
0057 if isfield(hdr,'magic')
0058 switch deblank(hdr.magic)
0059 case {'n+1'}
0060 iname = hname;
0061 if hdr.vox_offset < hdr.sizeof_hdr
0062 error(['Bad vox_offset (' num2str(double(hdr.vox_offset)) ') for "' fname '.'] );
0063 end
0064 case {'ni1'}
0065 if strcmp(ext,lower(ext)),
0066 iname = fullfile(pth,[nam '.img']);
0067 else
0068 iname = fullfile(pth,[nam '.IMG']);
0069 end;
0070 otherwise
0071 error(['Bad magic (' hdr.magic ') for "' fname '.'] );
0072 end
0073 else
0074 if strcmp(ext,lower(ext)),
0075 iname = fullfile(pth,[nam '.img']);
0076 else
0077 iname = fullfile(pth,[nam '.IMG']);
0078 end;
0079 end
0080 if rem(double(hdr.vox_offset),dt.size)
0081 error(['Bad alignment of voxels (' num2str(double(hdr.vox_offset)) '/' num2str(double(dt.size)) ') for "' fname '.'] );
0082 end;
0083
0084 vol = struct('hdr',hdr,'be',be,'hname',hname,'iname',iname);
0085 return