0001 function varargout=mars_vol_utils(varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045 nout = max(nargout,1);
0046 varargout = cell(1, nout);
0047 if nargin < 1
0048 error('Need action');
0049 end
0050 Action = lower(varargin{1});
0051 if nargin > 1
0052 V = varargin{2};
0053 end
0054
0055 switch(Action)
0056
0057
0058 case 'is_vol'
0059
0060 if nargin < 2
0061 error('Need vol struct to check');
0062 end
0063 varargout = {sf_is_vol(V)};
0064
0065
0066 case 'type'
0067
0068 if nargin < 2
0069 error('Need vol struct to give type');
0070 end
0071 sf_die_no_vol(V);
0072 varargout{:} = sf_type(V);
0073
0074
0075 case 'set_type'
0076
0077 if nargin < 2
0078 error('Need vol struct to set type');
0079 end
0080 sf_die_no_vol(V);
0081 if nargin < 3
0082 error('Need type to set');
0083 end
0084 type_info = varargin{3};
0085 varargout = {sf_set_type(V, type_info)};
0086
0087
0088 case 'is_swapped'
0089
0090 if nargin < 2
0091 error('Need vol struct to check swapping');
0092 end
0093 sf_die_no_vol(V);
0094 [t be sw] = sf_type(V);
0095 varargout = {sw};
0096
0097
0098 case 'ver'
0099
0100 if nargin < 2
0101 error('Need vol struct to give version');
0102 end
0103 sf_die_no_vol(V);
0104 varargout = {sf_ver(V)};
0105
0106
0107 case 'current_ver'
0108
0109 varargout = {sf_current_ver};
0110
0111
0112 case 'is_swapped_wrong'
0113
0114 if nargin < 2
0115 error('Need vol struct to test');
0116 end
0117 sf_die_no_vol(V);
0118 V2 = spm_vol(V.fname);
0119 [t be] = sf_type(V);
0120 [t be2] = sf_type(V2);
0121 varargout = {be ~= be2};
0122
0123
0124 case 'byte_swap'
0125
0126 if nargin < 2
0127 error('Need vol struct to swap');
0128 end
0129 sf_die_no_vol(V);
0130 switch sf_ver(V)
0131 case '99'
0132 for i = 1:numel(V)
0133 if V(i).dim(4) < 256, scf = 256; else scf = 1/256; end
0134 V(i).dim(4) = V(i).dim(4) * scf;
0135 end
0136 case '5'
0137 for i = 1:numel(V)
0138 V(i).dt(2) = 1-V(i).dt(2);
0139 end
0140 otherwise
0141 error(['Don''t often see those ' sf_ver(V)]);
0142 end
0143 varargout = {V};
0144
0145
0146 case 'convert'
0147
0148 if nargin < 2
0149 error('Need vol struct to convert');
0150 end
0151 if ~sf_is_vol(V), varargout={V}; return, end
0152 if nargin < 3
0153 ver = sf_current_ver;
0154 else
0155 ver = varargin{3};
0156 if sf_is_vol(ver), ver = sf_ver(ver); end
0157 end
0158 if sf_same_ver(V, ver)
0159 varargout = {V};
0160 return
0161 end
0162 switch sf_ver(V)
0163 case '99'
0164 varargout = {sf_99_to_5(V)};
0165 case '5'
0166 varargout = {sf_5_to_99(V)};
0167 otherwise
0168 error([sf_ver(V) ' is just nuts']);
0169 end
0170
0171
0172 case 'def_vol'
0173
0174 if nargin < 2
0175 ver = sf_current_ver;
0176 else
0177 ver = varargin{2};
0178 if sf_is_vol(ver), ver = sf_ver(ver); end
0179 end
0180 switch ver
0181 case '99'
0182 varargout = {sf_def_vol_99};
0183 case '5'
0184 varargout = {sf_def_vol_5};
0185 otherwise
0186 error([ver ' is unacceptable']);
0187 end
0188 return
0189
0190 otherwise
0191 error([Action ' is beyond my range']);
0192 end
0193 return
0194
0195
0196
0197 function tf = sf_is_vol(V)
0198
0199 tf = 0;
0200 if ~isstruct(V), return, end
0201 if ~isfield(V, 'dim'), return, end
0202 tf = 1;
0203 return
0204
0205 function sf_die_no_vol(V)
0206 if ~sf_is_vol(V)
0207 error('I really wanted a vol struct here');
0208 end
0209 return
0210
0211 function [t, be, sw] = sf_type(V)
0212
0213 plat_be = spm_platform('bigend');
0214 V = V(1);
0215 if sf_same_ver(V, '5')
0216 t = V.dt(1);
0217 be = V.dt(2);
0218 sw = xor(be, plat_be);
0219 elseif length(V.dim) > 3
0220 t = V.dim(4);
0221 sw = (t > 256);
0222 if sw, t = t/256; end
0223 be = xor(plat_be, sw);
0224 else
0225 error('Could not get vol type');
0226 end
0227 return
0228
0229 function V = sf_set_type(V, type_info)
0230
0231 switch sf_current_ver
0232 case '99'
0233 if ischar(type_info)
0234 if strcmp(type_info, 'float32'), type_info='float'; end
0235 if strcmp(type_info, 'float64'), type_info='double'; end
0236 type_info = spm_type(type_info);
0237 end
0238 case '5'
0239 if ischar(type_info)
0240 if strcmp(type_info, 'float'), type_info='float32'; end
0241 if strcmp(type_info, 'double'), type_info='float64'; end
0242 type_info = spm_type(type_info);
0243 end
0244 otherwise
0245 error(['Don''t like your type: ' sf_current_ver]);
0246 end
0247 switch sf_ver(V)
0248 case '99'
0249 V.dim(4) = type_info;
0250 case '5'
0251 V.dt(1) = type_info;
0252 otherwise
0253 error(['That''s a funny type: ' sf_ver(V)]);
0254 end
0255 return
0256
0257 function ver = sf_ver(V)
0258
0259 if isfield(V, 'dt')
0260 ver = '5';
0261 else
0262 ver = '99';
0263 end
0264 return
0265
0266 function ver = sf_current_ver;
0267
0268
0269 try
0270 V = spm_vol;
0271 ver = sf_ver(V);
0272 catch
0273 ver = '99';
0274 end
0275 return
0276
0277 function V = sf_def_vol_99;
0278 V = struct(...
0279 'fname', '',...
0280 'dim', zeros(1,4), ...
0281 'pinfo', [1 0 0]', ...
0282 'mat', eye(4), ...
0283 'n', [], ...
0284 'descrip', '');
0285 return
0286
0287 function V = sf_def_vol_5;
0288 V = struct('fname', '',...
0289 'dim', zeros(1,3),...
0290 'dt', [0 0],...
0291 'pinfo', [1 0 0]',...
0292 'mat', eye(4),...
0293 'n', [],...
0294 'descrip', '',...
0295 'private',[]);
0296 return
0297
0298 function Vo = sf_99_to_5(Vi)
0299
0300 d = sf_def_vol_5;
0301 Vo = d;
0302 is_n = isfield(Vi, 'n');
0303 for i = 1:numel(Vi)
0304 OV = Vi(i);
0305 NV = d;
0306 [t be] = sf_type(OV);
0307 NV.fname = OV.fname;
0308 NV.dim = OV.dim(1:3);
0309 NV.dt = [t be];
0310 NV.mat = OV.mat;
0311 NV.pinfo = OV.pinfo;
0312 NV.descrip = OV.descrip;
0313 if is_n, NV.n = [OV.n 1]; end
0314 Vo(i) = NV;
0315 end
0316 Vo = reshape(Vo, size(Vi));
0317 return
0318
0319 function Vo = sf_5_to_99(Vi)
0320
0321 d = sf_def_vol_99;
0322 Vo = d;
0323 for i = 1:numel(Vi)
0324 OV = Vi(i);
0325 NV = d;
0326 [t be sw] = sf_type(OV);
0327 if sw, t = t * 256; end
0328 NV.fname = OV.fname;
0329 NV.dim = [OV.dim(1:3) t];
0330 NV.mat = OV.mat;
0331 NV.pinfo = OV.pinfo;
0332 NV.descrip = OV.descrip;
0333 NV.n = OV.n(1);
0334 Vo(i) = NV;
0335 end
0336 Vo = reshape(Vo, size(Vi));
0337 return
0338
0339 function tf = sf_same_ver(V, ver)
0340 tf = strcmp(sf_ver(V), ver);
0341 return