function [imagex2_out, coordpick, anchorlines] = calculate_alignedimage_from_correspondpoints(...
	  xsel,xsel_ladder,diagonalline,mainband,imagex2,coordpick); 
      
[sizex, sizey] = size(imagex2);  
numcorrespondpoints = size(coordpick,2);

subplot(1,2,2); % Show in right hand image.
image(imagex2); hold on

countline=1;
%First anchorpoints -- actually totally fixed points -- on the "mainband".
anchorlines(1:sizex,countline,1) = mainband;
anchorlines(1:sizex,countline,2) = mainband;
plot(anchorlines(:,countline,1),1:sizex,'r');


%OK, now user defined correspondences...
NEIGHBORDIST = 10;
k=1;
usedinanchorline(1:numcorrespondpoints) = 0;
while (k <= numcorrespondpoints)
  if (usedinanchorline(k) == 0)
    usedinanchorline(k) = 1;
    countline=countline+1;

    % This is a little byzantine. Basically, if the user has selected
    % two alignment points whose x coordinates are really close, it
    % makes sense to put them in the same anchorline, rather than two
    % different ones. So look throughlist to look for such "neighbors".
    pointlist = k;
    for j=k+1: numcorrespondpoints
      if abs(coordpick(1,k) - coordpick(1,j)) < NEIGHBORDIST
	usedinanchorline(j) = 1;
	pointlist = [pointlist j];
      end
    end
    %Sort this set of points by distance vertically (away from diagonal)
    [dummy, sortindex] = sort( coordpick(2, pointlist));
    pointlist = pointlist(sortindex);
    
    xcoord = coordpick(1, pointlist(1));
    ycoord = coordpick(2, pointlist(1));
    xcoord_toberefined = coordpick(5, pointlist(1));
    ycoord_toberefined = coordpick(6, pointlist(1));

    [dummy, wherediagonalbegins] = min((diagonalline-xcoord).^2);

    %Top of gel
    pointonline = 1; anchorpoints = [ ];
    anchorpoints(1,pointonline) = 1;
    anchorpoints(2,pointonline) = xcoord;
    anchorpoints(3,pointonline) = xcoord;

    % Intersects diagonaline
    pointonline = pointonline+1;
    anchorpoints(1,pointonline) = wherediagonalbegins;
    anchorpoints(2,pointonline) = xcoord;
    anchorpoints(3,pointonline) = xcoord;

    % Actual alignment points
    for i = 1: length(pointlist)
      pointonline = pointonline+1;
      xcoord = coordpick(1, pointlist(i));
      ycoord = coordpick(2, pointlist(i));
      xcoord_toberefined = coordpick(5, pointlist(i));
      ycoord_toberefined = coordpick(6, pointlist(i));

      anchorpoints(1,pointonline) = ycoord_toberefined;
      anchorpoints(2,pointonline) = xcoord_toberefined;
      anchorpoints(3,pointonline) = xcoord;
    end

    % Bottom of gel.
    pointonline = pointonline+1;
    anchorpoints(1,pointonline) = sizex;
    anchorpoints(2,pointonline) = xcoord_toberefined;
    anchorpoints(3,pointonline) = xcoord;

    %Where we are starting
    anchorlines(:,countline,1) = interp1( anchorpoints(1,:), anchorpoints(2,:), 1:sizex);
    %Where we are going
    anchorlines(:,countline,2) = interp1( anchorpoints(1,:), anchorpoints(3,:), 1:sizex);
    plot(anchorlines(:,countline,1),1:sizex,'r');
  end

  k=k+1;
end

%Last anchorpoints -- actually totally fixed points -- on the diagonal.
countline=countline+1;
anchorlines(1:sizex,countline,1) = diagonalline;
anchorlines(1:sizex,countline,2) = diagonalline;
plot(anchorlines(:,countline,1),1:sizex,'r');

[imagex2_out] = calculate_alignedprofiles_2Dgel(imagex2,anchorlines,diagonalline,mainband);

for k=1: numcorrespondpoints
  xcoord_toberefined = coordpick(5,k);
  ycoord_toberefined = coordpick(6,k);
  [xcoord_new, ycoord_new] = maptonewimage(xcoord_toberefined, ...
				       ycoord_toberefined, anchorlines);
  coordpick(3,k) = xcoord_new;
  coordpick(4,k) = ycoord_new;
end

%[imagex2_out] = horizalign(imagex2,anchorlines');
