
%  Load in a file, crop and pad the image to the correct size
%  (32x32).  This function is used to provide standardized
%  footprints for use throughout the project.
function [clean] = prep(footprint, format)
  clean = Loadin(footprint, format);
  %  Make a negative of our image, to aid in cropping and resizing
  clean = .5*ones(size(clean)) - clean;
  [r, c] = size(clean);
  %  Remove the excess whitespace from the image (crop it)
  while sum(clean(1,:)) == 0
    clean = clean(2:r,:);
    r = r-1;
  end
  while sum(clean(:,1)) == 0
    clean = clean(:,2:c);
    c = c-1;
  end
  while sum(clean(r,:)) == 0
    r = r-1;
    clean = clean(1:r,:);
  end
  while sum(clean(:,c)) == 0
    c = c-1;
    clean = clean(:,1:c);
  end
  %  Now our image is as cropped as possible, so we add lines until
  %  we have a square image
  while c>r
    clean = [zeros(1,c); clean; zeros(1,c)];
    r = r+2;
  end
  while r>c
    clean = [zeros(1,r);  clean';  zeros(1,r)]';
    c = c+2;;
  end
  %  Now our image is square to within one row our column, so we
  %  make it truly square
  if c>r
    clean = [zeros(1,c); clean];
    r = r+1;
  end
  if r>c
    clean = [zeros(1,r); clean']';
    c = c+1;
  end
  %  Now we have a perfectly square matrix.  We need to check to
  %  see if we can rotate without losing information.
  for angle = 1:1:45
    %  Compare the original image to the image that has been
    %  rotated and rotated back
    while sum(sum(clean - imrotate(imrotate(clean,angle,'crop'),-angle,'crop'))) > 15
      clean = [zeros(1,c); clean ; zeros(1,c)];
      r = r+2;
      clean = [zeros(1,r); clean'; zeros(1,r)]';
      c = c+2;
    end
  end
  %  We now have the footprint in its final form, but unscaled.
  clean = imresize(clean, [32,32]);
  %  We need to use the negative of clean, to invert our earlier
  %  operation.
  clean = -clean+1*ones(32);
  %  Now we should have our footprint matrix in its optimal format.




