0001 function h = read_nifti(varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010 switch nargin
0011 case 0,
0012 org = niftistruc;
0013 hdr = [];
0014 for i=1:length(org),
0015 hdr.(org(i).label) = feval(org(i).dtype.conv,org(i).def);
0016 end;
0017 h = struct('hdr',hdr,'dat',[],'extras',struct);
0018 h = class(h,'read_nifti');
0019
0020 case 1
0021 if ischar(varargin{1})
0022 if size(varargin{1},1)>1,
0023 h = read_nifti(cellstr(varargin{1}));
0024 return;
0025 end;
0026 fname = deblank(varargin{1});
0027 vol = read_hdr(fname);
0028 extras = read_extras(fname);
0029
0030 if ~isfield(vol.hdr,'magic'),
0031 vol.hdr = mayo2nifti1(vol.hdr);
0032
0033
0034 if isfield(extras,'M') && ~isfield(extras,'mat'),
0035 extras.mat = extras.M;
0036 if nc_spm_flip_analyze_images,
0037 extras.mat = diag([-1 1 1 1])*extras.mat;
0038 end;
0039 end;
0040
0041
0042 if isfield(extras,'mat') && size(extras.mat,3)>=1,
0043 mat = extras.mat(:,:,1);
0044 mat1 = mat*[eye(4,3) [1 1 1 1]'];
0045 vol.hdr.srow_x = mat1(1,:);
0046 vol.hdr.srow_y = mat1(2,:);
0047 vol.hdr.srow_z = mat1(3,:);
0048 vol.hdr.sform_code = 2;
0049 vol.hdr.qform_code = 2;
0050 vol.hdr = encode_qform0(mat,vol.hdr);
0051 end;
0052 end;
0053
0054 if isfield(extras,'M'), extras = rmfield(extras,'M'); end;
0055 if isfield(extras,'mat') && size(extras.mat,3)<=1,
0056 extras = rmfield(extras,'mat');
0057 end;
0058
0059 dim = double(vol.hdr.dim);
0060 dim = dim(2:(dim(1)+1));
0061 dt = double(vol.hdr.datatype);
0062 offs = double(vol.hdr.vox_offset);
0063
0064 if ~vol.hdr.scl_slope && ~vol.hdr.scl_inter,
0065 vol.hdr.scl_slope = 1;
0066 end;
0067 slope = double(vol.hdr.scl_slope);
0068 inter = double(vol.hdr.scl_inter);
0069
0070 dat = create_file_array(vol.iname,dim,[dt,vol.be],offs,slope,inter);
0071 h = struct('hdr',vol.hdr,'dat',dat,'extras',extras);
0072 h = class(h,'read_nifti');
0073
0074 elseif isstruct(varargin{1})
0075 h = class(varargin{1},'read_nifti');
0076
0077 elseif iscell(varargin{1})
0078 fnames = varargin{1};
0079 h(numel(fnames)) = struct('hdr',[],'dat',[],'extras',struct);
0080 h = class(h,'read_nifti');
0081 for i=1:numel(fnames),
0082 h(i) = read_nifti(fnames{i});
0083 end;
0084
0085 else
0086 error('Dont know what to do yet.');
0087 end;
0088 otherwise
0089 error('Dont know what to do yet');
0090 end;
0091 return;