function [imagex2_out,coordpick] = ...
    correspondpoints_alignimages(imagex1,imagex2,xsel,xsel_ladder, ...
				 mainband, POS_ArrayBOMB, ...
				 diagonalline, maxres, coordpick)

%Preparatory stuff.
figure(2)
clf;
ContrastScale = sqrt(2);
ZOOMSCALE=5;
maxprof1 = max(max(imagex1))/80;
maxprof2 = max(max(imagex2))/80;
maxprof = max(maxprof1,maxprof2);
imagex2 = imagex2* maxprof1/maxprof2;

[sizex1,sizey1] = size(imagex1);
[sizex2,sizey2] = size(imagex2);
xsel1 = xsel;
xsel2 = xsel;
xsel1_ladder = xsel_ladder;
xsel2_ladder = xsel_ladder;

imagex2_out = imagex2;

%Align images if some correspondence picking has already occurred.
if exist('coordpick') 
  count=size(coordpick,2);
else
  count = 0;
  coordpick = [];
end
[imagex2_out, coordpick, anchorlines] = ...
    calculate_alignedimage_from_correspondpoints(...
    xsel,xsel_ladder,diagonalline,mainband,imagex2,coordpick); 

%Plot images.
currentaxis1=[1 sizey1 1 sizex1];
plotimage(1, imagex1, diagonalline, mainband, currentaxis1);
handles1 = get(gcf,'CurrentAxes');

currentaxis2=[1 sizey2 1 sizex2];
plotimage(2, imagex2_out, diagonalline, mainband, currentaxis2);
handles2 = get(gcf,'CurrentAxes');
[h1,h2,xselpick1,xselpick2,yselpick1,yselpick2] = drawx(coordpick);

grayscaleon = 1;
setcolormap(grayscaleon, maxprof);   

totalres = max(POS_ArrayBOMB);
ygrid1 = zeros(1,totalres); xgrid1 = zeros(1,totalres); ygrid2 = zeros(1,totalres);
ygrid1(1:length(xsel1))        = xsel1;
xgrid1(1:length(xsel1_ladder)) = xsel1_ladder;
ygrid2(1:length(xsel2))        = xsel2;

stopinput = 0; zoomedin1 = 0; zoomedin2=0; 
waitingforpointin1 = 0;
waitingforpointin2 = 0;
savethepoint = 0;

while (stopinput == 0)
  subplot(1,2,1);
  [xsel,ysel,button] = ginput(1);
  
  switch button
   case 1 % Pick corresponding points
    currenthandle =  get(gcf,'CurrentAxes');
    
    if (currenthandle == handles1 & ~waitingforpointin2)
      %Point was picked in left hand panel
      if ~waitingforpointin1
	count = count+1;
	h1(count) = plot(xsel,ysel,'rx');
	subplot(1,2,2)
	title('Now pick a point over here');
	waitingforpointin2 = 1;
	xselpick2(count) = 0;
	yselpick2(count) = 0;
      else
	h1(count) = plot(xsel,ysel,'rx');	  
	waitingforpointin1 = 0;
	savethepoint = 1;
      end
      xselpick1(count) = xsel;
      yselpick1(count) = ysel;
    end
    
    if (currenthandle == handles2 & ~waitingforpointin1)
      %Point was picked in the right hand panel.
      if ~waitingforpointin2
	count = count+1;
	h2(count) = plot(xsel,ysel,'rx');
	subplot(1,2,1)
	title('Now pick a point over here');
	waitingforpointin1 = 1;
	xselpick1(count) = 0;
	yselpick1(count) = 0;
      else
	h2(count) = plot(xsel,ysel,'rx');
	waitingforpointin2 = 0;
	savethepoint = 1;
      end
      xselpick2(count) = xsel;
      yselpick2(count) = ysel;
    end
    
    if (savethepoint)
      [xsel2_orig, ysel2_orig] = mapbacktooriginalcoords( xselpick2(count), yselpick2(count),anchorlines);
      coordpick(:,count) = [xselpick1(count) yselpick1(count) ...
		    xselpick2(count) yselpick2(count), xsel2_orig, ysel2_orig]';
      title(' ');
      savethepoint = 0;
    end    
    
   case 3 %Zoom in/out
    if get(gcf,'CurrentAxes') == handles1
      sizex=sizey1;sizey=sizex1;zoomedin=zoomedin1;
    else
      sizex=sizey2;sizey=sizex2;zoomedin=zoomedin2;
    end
    if (zoomedin == 1)
      currentaxis = [0 sizex 1 sizey]; 
      axis(currentaxis);
      zoomedin = 0;                    
    else
      xselzoom = xsel;
      yselzoom = ysel;
      ymin = max(round(yselzoom - sizey/ZOOMSCALE),0);
      ymax = min(round(yselzoom + sizey/ZOOMSCALE),sizey);
      xmin = max(round(xselzoom - sizex/ZOOMSCALE),0);
      xmax = min(round(xselzoom + sizex/ZOOMSCALE),sizex);
      currentaxis=[xmin xmax ymin ymax ];
      axis(currentaxis);	
      zoomedin = 1;
    end
    if get(gcf,'CurrentAxes') == handles1
      zoomedin1=zoomedin; currentaxis1=currentaxis;
    else
      zoomedin2=zoomedin; currentaxis2=currentaxis;
    end
   case 2 %Erase a point
    if ~waitingforpointin1 & ~waitingforpointin2
      if get(gcf,'CurrentAxes') == handles1
	[dummy,closestpoint] = min((xselpick1-xsel).^2 + (yselpick1-ysel).^2);
      else
	[dummy,closestpoint] = min((xselpick2-xsel).^2 + (yselpick2-ysel).^2);      
      end
      subplot(1,2,1);set(h1(closestpoint),'visible','off');
      subplot(1,2,2);set(h2(closestpoint),'visible','off');
      newpoints = [1:closestpoint-1 closestpoint+1:count];    
      coordpick = coordpick(:,newpoints);
      xselpick1 = xselpick1(newpoints);    
      yselpick1 = yselpick1(newpoints);    
      xselpick2 = xselpick2(newpoints);    
      yselpick2 = yselpick2(newpoints);  
      h1=h1(newpoints); h2=h2(newpoints);count=count-1;          
    end
  end
  
  switch char(button)
   case '1'
    if (xsel<=currentaxis1(2)) %Clicked on left hand graph.
      subplot(1,2,1);
      imagex1 = imagex1* ContrastScale;
      hold off; plot(1,1);
      image(imagex1);axis off
      axis(currentaxis1);
      hold on
      for i=1:count;        h1(i) = plot(xselpick1(i),yselpick1(i),'rx');    end
    else
      subplot(1,2,2);
      imagex2     = imagex2* ContrastScale; 
      imagex2_out = imagex2_out* ContrastScale; 
      hold off; plot(1,1);
      image(imagex2_out);axis off
      axis(currentaxis2);
      hold on
      for i=1:count;        h2(i) = plot(xselpick2(i),yselpick2(i),'rx');    end	
    end    
   case '2'
    if (xsel<=currentaxis1(2)) %Clicked on left hand graph.
      subplot(1,2,1);
      imagex1 = imagex1/ ContrastScale;
      hold off; plot(1,1);
      image(imagex1);axis off
      axis(currentaxis1);
      hold on
      for i=1:count;        h1(i) = plot(xselpick1(i),yselpick1(i),'rx');    end
    else
      subplot(1,2,2);
      imagex2     = imagex2 / ContrastScale;
      imagex2_out = imagex2_out/ ContrastScale;
      hold off; plot(1,1);
      image(imagex2_out);axis off
      axis(currentaxis2);
      hold on
      for i=1:count;        h2(i) = plot(xselpick2(i),yselpick2(i),'rx');    end
    end    
   case '3'
    maxprof = maxprof/ContrastScale;
    setcolormap(grayscaleon, maxprof);    
   case '4'
    maxprof = maxprof*ContrastScale;
    setcolormap(grayscaleon, maxprof);    
   case {'c','C'}
    grayscaleon = 1 - grayscaleon;
    setcolormap(grayscaleon, maxprof);   
   case {'z','Z'}
    stopinput = 1;      
   case {'q','Q'}
    [imagex2_out, coordpick, anchorlines] = calculate_alignedimage_from_correspondpoints(...
	xsel,xsel_ladder,diagonalline,mainband,imagex2,coordpick); 
    plotimage(1, imagex1    , diagonalline, mainband, currentaxis1);
    plotimage(2, imagex2_out, diagonalline, mainband, currentaxis2);
    [h1,h2,xselpick1,xselpick2,yselpick1,yselpick2] = drawx(coordpick);
  end
end
set(gcf,'pointer','arrow');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [h1,h2,xselpick1,xselpick2,yselpick1,yselpick2] =  drawx(coordpick)

h1=[];h2=[];xselpick1=[];xselpick2=[]; yselpick1=[];yselpick2=[];

if ~isempty(coordpick)    
  count = size(coordpick,2);
  xselpick1 = coordpick(1,:);
  yselpick1 = coordpick(2,:);
  xselpick2 = coordpick(3,:);
  yselpick2 = coordpick(4,:);
  for i=1:count
    subplot(1,2,1)
    h1(i) = plot(xselpick1(i),yselpick1(i),'rx');
  end
  for i=1:count
    subplot(1,2,2)
    h2(i) = plot(xselpick2(i),yselpick2(i),'rx');
  end    
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function plotimage(numplot, imagex, diagonalline, mainband, currentaxis);
[sizex,sizey] = size(imagex);

subplot(1,2,numplot);
hold off;  plot(1,1);
image(imagex);axis off
hold on
plot(diagonalline,1:sizex,'m');
plot([mainband mainband],[1 sizey],'m');
axis(currentaxis)
