function t_outfiles = roi_truncate(varargin)
% Program to truncate the intensities that are less than an
% inputted threshold in image mask files.
% To be used after spatially smoothing the ROI image files, in order to
% constrain better the ROI extent. The 0.2 values is arbitrary (though
% selected after some testing.
%
% NB: This method is not perfect - in fact, it's an ad hoc solution to
% the problem of conservative gray definition of the Talairach brain,
% compared to the MNI template. Other methods should be considered if
% possible.
%
% Kalina Christoff	/	2000-08-18
if nargin==0
    vols = spm_vol(spm_get(Inf,'.img','Select images to truncate.'));
else
    vols = spm_vol(varargin{1});
end

t = spm_input('Select truncation threshold ',1,'e','0.2');

for i = 1:length(vols)
  vol = vols(i);
  Y = spm_read_vols(vol,0);
  
  % zero out all voxels with intensity < 0.2 and make the file binary
  Y(Y<t) = 0;
  Y(Y>0)	  = 1;

  % form the toutput volo with a recomputed output file name
  tmp  = max([findstr('/',vol.fname), findstr('\',vol.fname)]);
  volo = vol;
  volo.fname = [vol.fname(1:tmp),'t',vol.fname(tmp+1:end)];
  t_out{i,1} = volo.fname; 
  % write out new image
  spm_write_vol(volo,Y);
end

t_outfiles = char(t_out);