Home > rest_20090422 > rest_spm5_files > nic_spm_create_vol.m

nic_spm_create_vol

PURPOSE ^

Create a volume

SYNOPSIS ^

function V = nic_spm_create_vol(V,varargin)

DESCRIPTION ^

 Create a volume
 FORMAT V = spm_create_vol(V)
 V - image volume information (see spm_vol.m)
____________________________________________________________________________
 Copyright (C) 2005 Wellcome Department of Imaging Neuroscienic_e

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

DOWNLOAD ^

nic_spm_create_vol.m

SOURCE CODE ^

0001 function V = nic_spm_create_vol(V,varargin)
0002 % Create a volume
0003 % FORMAT V = spm_create_vol(V)
0004 % V - image volume information (see spm_vol.m)
0005 %____________________________________________________________________________
0006 % Copyright (C) 2005 Wellcome Department of Imaging Neuroscienic_e
0007 
0008 % John Ashburner
0009 % $Id: spm_create_vol.m 202 2005-07-20 14:01:00Z john $
0010 
0011 
0012 for i=1:numel(V),
0013     if nargin>1,
0014         v = create_vol(V(i),varargin{:});
0015     else
0016         v = create_vol(V(i));
0017     end;
0018     f = fieldnames(v);
0019     for j=1:size(f,1),
0020         V(i).(f{j}) = v.(f{j});
0021     end;
0022 end;
0023 
0024 
0025 
0026 function V = create_vol(V,varargin)
0027 if ~isstruct(V),        error('Not a structure.'); end;
0028 if ~isfield(V,'fname'), error('No "fname" field'); end;
0029 
0030 if ~isfield(V,'dim'), error('No "dim" field'); end;
0031 if ~all(size(V.dim)==[1 3]),
0032     error(['"dim" field is the wrong size (' num2str(size(V.dim)) ').']);
0033 end;
0034 
0035 if ~isfield(V,'n'),
0036     V.n       = [1 1];
0037 else
0038     V.n       = [V.n(:)' 1 1];
0039     V.n       =  V.n(1:2);
0040 end;
0041 
0042 if V.n(1)>1 && V.n(2)>1,
0043     error('Can only do up to 4D data (%s).',V.fname);
0044 end;
0045 
0046 if ~isfield(V,'dt'),
0047     V.dt = [nic_spm_type('float64') nic_spm_platform('bigend')];
0048 end;
0049 
0050 dt{1} = nic_spm_type(V.dt(1));
0051 if strcmp(dt{1},'unknown'),
0052     error(['"' dt(1) '" is an unrecognised datatype (' num2str(V.dt(1)) ').']);
0053 end;
0054 if V.dt(2), dt{2} = 'BE'; else dt{2} = 'LE'; end;
0055 
0056 if ~isfield(V,'pinfo'), V.pinfo      = [1 0 0]'; end;
0057 if size(V.pinfo,1)==2,  V.pinfo(3,:) = 0;        end;
0058 V.fname       = deblank(V.fname);
0059 [pth,nam,ext] = fileparts(V.fname);
0060 switch ext,
0061 case {'.img'}
0062     minoff = 0;
0063 case {'.nii'}
0064     minoff = 352;
0065 otherwise
0066     error(['"' ext '" is not a recognised extension.']);
0067 end;
0068 bits   = nic_spm_type(V.dt(1),'bits');
0069 minoff = minoff + ceil(prod(V.dim(1:2))*bits/8)*V.dim(3)*(V.n(1)-1+V.n(2)-1);
0070 V.pinfo(3,1) = max(V.pinfo(3,:),minoff);
0071 
0072 if ~isfield(V,'descrip'), V.descrip = '';    end;
0073 if ~isfield(V,'private'), V.private = struct;    end;
0074 
0075 dim    = [V.dim(1:3) V.n];
0076 dat    = create_file_array(V.fname,dim,[dt{1} '-' dt{2}],0,V.pinfo(1),V.pinfo(2));
0077 N      = read_nifti;
0078 N.dat  = dat;
0079 N.mat  = V.mat;
0080 N.mat0 = V.mat;
0081 N.mat_intent  = 'Aligned';
0082 N.mat0_intent = 'Aligned';
0083 N.descrip = V.descrip;
0084 
0085 N0  = [];
0086 % try
0087 %     N0  = read_nifti(V.fname);
0088 % catch
0089 %     N0  = [];
0090 % end;
0091 
0092 if ~isempty(N0),
0093     tmp = [N0.dat.dim ones(1,5)];
0094     if any(tmp(1:3) ~= dim(1:3))
0095         %warning(['Inic_ompatible x,y,z dimensions in file "' V.fname '" [' num2str(tmp(1:3)) ']~=[' num2str(dim(1:3)) '].']);
0096     end;
0097     if dim(5) > tmp(5) && tmp(4) > 1,
0098         %warning(['Inic_ompatible 4th and 5th dimensions in file "' V.fname '" (' num2str([tmp(4:5) dim(4:5)]) ').']);
0099     end;
0100     N.dat.dim = max(dim(1:5),tmp(1:5));
0101     if ~strcmp(dat.dtype,N0.dat.dtype),
0102         %warning(['Inic_ompatible datatype in file "' V.fname '" ' N0.dat.dtype ' ~= ' dat.dtype '.']);
0103     end;
0104     if single(N.dat.scl_slope) ~= single(N0.dat.scl_slope) && (size(N0.dat,4)>1 || V.n(1)>1),
0105         %warning(['Inic_ompatible scalefactor in "' V.fname '" ' num2str(N0.dat.scl_slope) '~=' num2str(N.dat.scl_slope) '.']);
0106     end;
0107     if single(N.dat.scl_inter) ~= single(N0.dat.scl_inter),
0108         %warning(['Inic_ompatible intercept in "' V.fname '" ' num2str(N0.dat.scl_inter) '~=' num2str(N.dat.scl_inter) '.']);
0109     end;
0110     if single(N.dat.offset) ~= single(N0.dat.offset),
0111         %warning(['Inic_ompatible intercept in "' V.fname '" ' num2str(N0.dat.offset) '~=' num2str(N.dat.offset) '.']);
0112     end;
0113 
0114     if V.n(1)==1,
0115 
0116         % Ensure volumes 2..N have the original matrix
0117         nt = size(N.dat,4);
0118         if nt>1 && sum(sum((N0.mat-V.mat).^2))>1e-8,
0119             M0 = N0.mat;
0120             if ~isfield(N0.extras,'mat'),
0121                 N0.extras.mat = zeros([4 4 nt]);
0122             else
0123                 if size(N0.extras.mat,4)<nt,
0124                     N0.extras.mat(:,:,nt) = zeros(4);
0125                 end;
0126             end;
0127             for i=2:nt,
0128                 if sum(sum(N0.extras.mat(:,:,i).^2))==0,
0129                     N0.extras.mat(:,:,i) = M0;
0130                 end;
0131             end;
0132             N.extras.mat = N0.extras.mat;
0133         end;
0134 
0135         N0.mat = V.mat;
0136         if strcmp(N0.mat0_intent,'Aligned'), N.mat0 = V.mat; end;
0137         if ~isempty(N.extras) && isstruct(N.extras) && isfield(N.extras,'mat') &&...
0138             size(N.extras.mat,3)>=1,
0139             N.extras.mat(:,:,V.n(1)) = V.mat;
0140         end;
0141     else
0142         N.extras.mat(:,:,V.n(1)) = V.mat;
0143     end;
0144 
0145     if ~isempty(N0.extras) && isstruct(N0.extras) && isfield(N0.extras,'mat'),
0146         N0.extras.mat(:,:,V.n(1)) = N.mat;
0147         N.extras                  = N0.extras;
0148     end;
0149     if sum((V.mat(:)-N0.mat(:)).^2) > 1e-4,
0150         N.extras.mat(:,:,V.n(1)) = V.mat;
0151     end;
0152 end;
0153 create(N);
0154 V.private = N;
0155

Generated on Wed 29-Apr-2009 01:06:38 by m2html © 2005