function [values]=find_values_in_mask(file,mask_file,resample_first);


% ---------------------------------
% Copyright (c) 2003 Serge Dumoulin
% This program is free software; you can redistribute it and/or
% modify it under the terms of the GNU General Public License version 2
% as published by the Free Software Foundation.
% ---------------------------------


if nargin < 3,
   resample_first=[];
end;
if nargin < 2,
   help(mfilename);
   return;
end;

mask_thresh=0.5;

%--- get file data
h=openimage(deblank(file));
ns1=getimageinfo(h,'NumSlices');
Data = getimages(h,1:ns1);
closeimage(h);

%--- resample mask to file if necessary
if ~isempty(resample_first),
   mymaskfile='tmpmask.mnc';
   unix(['mincresample -clobber -like ' file ...
            ' ' mask_file ' ' mymaskfile]);
else,
   mymaskfile=mask_file;
end;

%--- get mask data
h=openimage(mymaskfile);
ns=getimageinfo(h,'NumSlices');
if ns1~=ns, %check
   closeimage(h);
   error('Not the same number of slices');
end;
MaskData = getimages(h,1:ns);
closeimage(h);

values = Data(find(MaskData >= mask_thresh));


