%  This function looks through the images and eliminates those
%  spaces which contain only "snow".
function [W, Wmask] = WhiteSpace(S)
  W = [];
  %  The "whiteness" limit, white being .5
  limit = .48;
  %  The minimum area we find interesting enough to keep.
  limit2 = 6;
  %  We know that our base image will always be 32 by 32
    [sr, sc] = size(S)
    %  We search for rows and columns with only white.
    c = 1;
    %  First we search for columns that are all white.
    while (c<sc)
      while (mean(S(:,c))>limit)&(c<sc)
	%  We have a column that is all white
	c = c+1;
      end
      %  We are now at the start of a footprint
      cstart = c;
      cend = cstart;
      if c<sc
	while (mean(S(:,c))<limit)&(c<sc)
	  %  We have a column with a foot print
	  c = c+1;
	end
	cend = c-1;
      end
      %  Now we are at the end of a footprint
      r = 1;
      while (r<sr)&(cstart<sc)
	%  Now we have a range of columns with something interesting in
	%  them.  Search for the non-white rows between these columns.
	while (mean(S(r,cstart:cend))>limit)&(r<sr)
	  r = r+1;
	end
	rstart = r;
	rend = rstart;
	if r<sr
	  while (mean(S(r,cstart:cend))<limit)&(r<sr)
	    r = r+1;
	  end
	  rend = r-1;
	end
	%  Now we have a range of columns and a range of rows that
        %  contains something of interest.
	%  Now narrow down the range of columns even more.
	cn = cstart;
	while cn<cend
	  cstartnew = cstart;
	  cendnew = cend;
	  while (mean(S(rstart:rend, cn))>limit)&(cn<cend)
	    cn = cn+1;
	  end
	  cstartnew = cn;
	  cendnew = cstartnew;
	  if cn<cend
	    while (mean(S(rstart:rend, cn))<limit)&(cn<cend);
	      cn = cn+1;
	    end
	    cendnew = cn-1;
	  end
	  %  Now in the range in the matrix defined by these bounds we have
	  %  something of interest.  Store this information.
	  if (rstart ~= sr)
	    %  Check to see if our box is big enough to be of
	    %  interest
	    if(rend - rstart > limit2)&(cendnew - cstartnew> ...
					limit2)
	      %  Store the bounds of our box into the output matrix.
	      if sum(size(W))>0
		W = [W(1,:), rstart; W(2,:), rend; W(3,:), cstartnew; ...
		     W(4,:), cendnew];
	      else
		W = [rstart;rend;cstartnew;cendnew];
	      end
	    end
	  end
	  %  End the inner column loop.
	end
	%  End the row loop.
      end
      %  End the outer column loop.
    end
    % Create a graphical mask that shows the areas whitespace picked.
    Wmask = zeros(sr, sc);
    for index = 1:1:size(W,2)
      for r = W(1, index):1:W(2,index)
	for c = W(3, index):1:W(4, index)
	  Wmask(r, c) = 1;
	end
      end
    end
    
	




