function RetAvg2(infiles,outfile,posneg,setto,thresh,add);
%function RetAvg2(infiles,outfile,posneg,setto,thresh,add);
% non-linear averaging of infiles
%
% if posneg ~= 0 ->  average values will be set to either -1 or 1
% if posneg == 0 and setto == 0 -> takes averages 
%  exept if one is - and the other + (irrelevant witch value) 
%  and will set that to 0
% if posneg == 0 and setto ~= 0 -> takes averages 
%  exept if one is - and the other + (irrelevant witch value) 
%  and will set that to the smallest absolute value
% if thresh is defined 0 wil be replaced by +/- thresh
%  only for posneg ~= 0
% if add ~= 0 will not take averages but will add them in similar way

% ---------------------------------
% 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,
   posneg=0;
end;
if nargin < 4,
   setto=0;
end;
if nargin < 5,
   thresh=0;
else,
   thresh=abs(thresh);
end;
if nargin < 6,
   add=0;
end;

tic 

nfiles = size(infiles,1);
h = zeros(1,nfiles);

for ix=1:nfiles
  h(ix) = openimage(deblank(infiles(ix,:)));
end;

sizes=getimageinfo(h(1),'ImageSize');
NumSlices=getimageinfo(h(1),'NumSlices');
ImageHeight=sizes(1);
ImageWidth=sizes(2);
PixelsPerSlice = sizes(1).*sizes(2); % total pixels per slice
 
fname_out = [ outfile ];   
new_handle_out = newimage(fname_out,[0 NumSlices],deblank(infiles(1,:)));

for s=1:NumSlices,
   if s==1, fprintf('[%s]: Processing:',mfilename); end;
   fprintf('.');drawnow;
   for ix=1:nfiles,
      sliceframes(ix,:)=getimages(h(ix),s)';
   end; 
   
   if posneg==0,
      neg=sliceframes;
      neg(find(sign(sliceframes)~=-1))=NaN;
      neg2=neg;
      neg2(isnan(neg))=0;
      pos=sliceframes;
      pos(find(sign(sliceframes)~= 1))=NaN;
      pos2=pos;
      pos2(isnan(pos))=0;
      if add==0,
         Output=mean(sliceframes,1);
	 % dont depend on matlab toolbox
         %posOut=nanmean(pos);
         %negOut=nanmean(neg);
         div=sum(~isnan(pos));
         zindex=find(div==0);
         div(zindex)=1;
	 posOut=sum(pos2)./div;
         posOut(zindex)=NaN;
         
         div=sum(~isnan(neg));
         zindex=find(div==0);
         div(zindex)=1;
	 negOut=sum(neg2)./div;
         negOut(zindex)=NaN;
      else,
         Output=sum(sliceframes,1);
         %posOut=nansum(pos);
         %negOut=nansum(neg);
         div=sign(abs(sum(~isnan(pos))));
         zindex=find(div==0);
         div(zindex)=1;
	 posOut=sum(pos2)./div;
         posOut(zindex)=NaN;
         
         div=sign(abs(sum(~isnan(neg))));
         zindex=find(div==0);
         div(zindex)=1;
	 negOut=sum(neg2)./div;
         negOut(zindex)=NaN;
      end;
      
      if setto==0,
         Output(find(abs(sum(sign(sliceframes))) < size(sliceframes,1)))=0;
      else,
         % if majorituy is pos
         more_pos_signs=find(sum(sign(sliceframes)) > 0 & ...
                     sum(sign(sliceframes)) < size(sliceframes,1));
         Output(more_pos_signs) = posOut(more_pos_signs);
      
         % if majority is neg
         more_neg_signs=find(sum(sign(sliceframes)) < 0 & ...
                     sum(sign(sliceframes)) > -1*size(sliceframes,1));
         Output(more_neg_signs) = negOut(more_neg_signs);
      
         % Equal amount pos & neg
         equal_signs_2neg= ...
             find(sum(sign(sliceframes)) == 0 & ...
                  negOut < posOut);
         Output(equal_signs_2neg)=negOut(equal_signs_2neg);
         equal_signs_2pos= ...
             find(sum(sign(sliceframes)) == 0 & ...
                  negOut > posOut);
         Output(equal_signs_2pos)=posOut(equal_signs_2pos);
      end;
   elseif posneg~=0;
     sliceframes(find(sliceframes >  thresh))= 1;
     sliceframes(find(sliceframes < -thresh))=-1;
     sliceframes(find((sliceframes > -thresh) & ...
                      (sliceframes <  thresh)))= 0;
     if add==0,
        Output=round(mean(sliceframes,1));
     else,
        Output=round(sum(sliceframes,1));
     end;
     %putimages(new_handle_out,Output',s); 
   end;
   putimages(new_handle_out,Output',s); 
end; %for s

closeimage(new_handle_out);
for ix=1:nfiles
  closeimage(h(ix));
end;

fprintf('Done[%d minutes].\n',round(toc/60));

