function [landmarkpairs] = ChooseLandmarkPairs(image, outfilename)
%Joshua Stough
%March 27, 2003
%COMP 254, Pizer, Assn 4

%Given an image (as instantiated with imread) and an 
%output file name as a string, this program allows 
%the user to select point pairs, drawing those pairs
%as selected, and writes out a landmark pair file as
%per the program that Brad Davis put together for this
%assignment.

if (nargin < 2), outfilename = 'lmpairdata'; end

disp('For every two points you select on the image, a');
disp('line is drawn between them, the first point green,');
disp('the second violet, with a red line between them.');
disp('When you are finished, press return twice, and');
disp('the landmark pair file will be written.');

if (size(image,3) > 1)
    I = double(rgb2gray(image));
else
    I = double(image);
end

I = I/max(max(I));

s = size(I);
Overlay = zeros(s(1),s(2),3);

Overlay(:,:,1) = I;
Overlay(:,:,2) = I;
Overlay(:,:,3) = I;

%The picture is multichanneled so that the overlay of
%selected point pairs is visible.

%Display the image.
h = figure('Name','Image'); 
imshow(Overlay, 'truesize');
%imagesc(Overlay);

c = 0;

while 1
    c = c + 1;
    
    [x,y] = ginput(1);
    [xp,yp] = ginput(1);
    
    if (isempty(x) | isempty(xp)) 
        disp('Thanks for trying me out.');
        break
    end
    
    %MATLAB is confusing to me in x and y every time.
    %I switch the components returned to me here.
    temp = y;
    y = x;
    x = temp;
    
    temp = yp;
    yp = xp;
    xp = temp;
    
    disp(sprintf('(%f , %f)-->(%f , %f)', x, y, xp, yp));
    
    %Draw the green dot starting point.
    if (x > 1 & y > 1 & x <= s(1)-1 & y <= s(2)-1)
        Overlay(x-1,y-1,2) = 1;
        Overlay(x-1,y,2) = 1;
        Overlay(x-1,y+1,2) = 1;
        Overlay(x,y-1,2) = 1;
        %Overlay(x,y,2) = 1;
        Overlay(x,y+1,2) = 1;
        Overlay(x+1,y-1,2) = 1;
        Overlay(x+1,y,2) = 1;
        Overlay(x+1,y+1,2) = 1;
    end
    
    if (xp > 1 & yp > 1 & xp <= s(1)-1 & yp <= s(2)-1)
        Overlay(xp-1,yp-1,1) = 1;
        Overlay(xp-1,yp,1) = 1;
        Overlay(xp-1,yp+1,1) = 1;
        Overlay(xp,yp-1,1) = 1;
        %Overlay(xp,yp,2) = 1;
        Overlay(xp,yp+1,1) = 1;
        Overlay(xp+1,yp-1,1) = 1;
        Overlay(xp+1,yp,1) = 1;
        Overlay(xp+1,yp+1,1) = 1;
        
        Overlay(xp-1,yp-1,3) = 1;
        Overlay(xp-1,yp,3) = 1;
        Overlay(xp-1,yp+1,3) = 1;
        Overlay(xp,yp-1,3) = 1;
        %Overlay(xp,yp,3) = 1;
        Overlay(xp,yp+1,3) = 1;
        Overlay(xp+1,yp-1,3) = 1;
        Overlay(xp+1,yp,3) = 1;
        Overlay(xp+1,yp+1,3) = 1;
    end
    
    landmarkpairs(c,:) = [x y xp yp];
     
    figure(h)
    imshow(Overlay, 'truesize');
    %imagesc(Overlay);
    line([landmarkpairs(:,2) landmarkpairs(:,4)]', [landmarkpairs(:,1) landmarkpairs(:,3)]','Color', [1 0 0]);
    %line([y yp], [x xp],'Color', [1 0 0]);
    
end

s = size(landmarkpairs);
fid = fopen(outfilename, 'w');
cn = fprintf(fid,'%i\n', s(1));

for i = 1:s(1)
    cn = fprintf(fid,'%f %f %f %f\n', landmarkpairs(i,:));
end
fclose(fid);