0001 function V = spm_write_vol(V,Y)
0002
0003
0004
0005
0006
0007
0008
0009
0010 if ndims(Y)>3, error('Can only handle a maximum of 3 dimensions.'), end
0011
0012 if ~isfield(V,'pinfo'), V.pinfo = [1,0,0]'; end
0013
0014 dim = [size(Y) 1 1 1];
0015 if ~all(dim(1:3) == V.dim(1:3)) | (size(V.pinfo,2)~=1 & size(V.pinfo,2)~=dim(3)),
0016 error('Incompatible dimensions.');
0017 end
0018
0019
0020
0021
0022 dt = V.dim(4); if dt>256, dt = dt/256; end;
0023 if any(dt == [128+2 128+4 128+8]),
0024
0025 dt = dt - 128;
0026 end;
0027 s = find(dt == [2 4 8 128+2 128+4 128+8]);
0028 dmnmx = [0 -2^15 -2^31 -2^7 0 0 ; 2^8-1 2^15-1 2^31-1 2^7-1 2^16 2^32];
0029 dmnmx = dmnmx(:,s);
0030 V.pinfo(1,:) = 1;
0031 V.pinfo(2,:) = 0;
0032 mxs = zeros(dim(3),1)+NaN;
0033 mns = zeros(dim(3),1)+NaN;
0034 if ~isempty(s),
0035 for p=1:dim(3),
0036 tmp = double(Y(:,:,p));
0037 tmp = tmp(isfinite(tmp));
0038 if ~isempty(tmp),
0039 mxs(p) = max(tmp);
0040 mns(p) = min(tmp);
0041 end;
0042 end;
0043
0044 if size(V.pinfo,2) ~= 1,
0045 for p=1:dim(3),
0046 mx = mxs(p);
0047 mn = mns(p);
0048 if ~isfinite(mx), mx = 0; end;
0049 if ~isfinite(mn), mn = 0; end;
0050 if mx~=mn,
0051 V.pinfo(1,p) = (mx-mn)/(dmnmx(2)-dmnmx(1));
0052 V.pinfo(2,p) = ...
0053 (dmnmx(2)*mn-dmnmx(1)*mx)/(dmnmx(2)-dmnmx(1));
0054 else,
0055 V.pinfo(1,p) = 0;
0056 V.pinfo(2,p) = mx;
0057 end;
0058 end;
0059 else,
0060 mx = max(mxs(isfinite(mxs)));
0061 mn = min(mns(isfinite(mns)));
0062 if isempty(mx), mx = 0; end;
0063 if isempty(mn), mn = 0; end;
0064 if mx~=mn,
0065 V.pinfo(1,1) = (mx-mn)/(dmnmx(2)-dmnmx(1));
0066 V.pinfo(2,1) = (dmnmx(2)*mn-dmnmx(1)*mx)/(dmnmx(2)-dmnmx(1));
0067 else,
0068 V.pinfo(1,1) = 0;
0069 V.pinfo(2,1) = mx;
0070 end;
0071 end;
0072 end;
0073
0074
0075
0076 V = spm_create_vol(V);
0077 for p=1:V.dim(3),
0078 V = spm_write_plane(V,Y(:,:,p),p);
0079 end;
0080 V = spm_close_vol(V);