function [landmarkpairs] = ChooseLandmarkCurves(image, n, outfilename)
%Joshua Stough
%March 28, 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 curves, drawing those curves
%as selected, and writes out landmark pair files as
%per the program that Brad Davis put together for this
%assignment.

if (nargin < 3), outfilename = 'lmcurvedata'; end
if (nargin < 2), n = 10; end

disp('  For every two points you select on the image, a');
disp('line is drawn between them, first point in green,'); 
disp('other points in violet, with a red line between them.');
disp('  Select n points and they are connected in this');
disp('manner.  Then a further n are selected. When you');
disp('are finished, press return (after k*n selections');
disp('would be nice) and n-1 landmark pair files will ');
disp('be written.');

landmarkpairs = zeros(1,4,n-1);

I = double(rgb2gray(image));
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');

quitter = 0;
c = 0;

while quitter ~= 1
    c = c + 1;
    
    [x,y] = ginput(1); 
    
    if (isempty(x)) 
        disp('Thanks for trying me out.');
        break
    end
    
    %Now select more points on the curve.
    for i = 2:n
        
        [xp,yp] = ginput(1);
        
        if (isempty(xp)) 
            disp('Thanks for trying me out.');
            quitter = 1;
            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)
            if (i == 2) %Starting point in green
                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;
            else
                Overlay(x-1,y-1,1) = 1;
                Overlay(x-1,y,1) = 1;
                Overlay(x-1,y+1,1) = 1;
                Overlay(x,y-1,1) = 1;
                %Overlay(x,y,1) = 1;
                Overlay(x,y+1,1) = 1;
                Overlay(x+1,y-1,1) = 1;
                Overlay(x+1,y,1) = 1;
                Overlay(x+1,y+1,1) = 1;
                
                Overlay(x-1,y-1,3) = 1;
                Overlay(x-1,y,3) = 1;
                Overlay(x-1,y+1,3) = 1;
                Overlay(x,y-1,3) = 1;
                %Overlay(x,y,3) = 1;
                Overlay(x,y+1,3) = 1;
                Overlay(x+1,y-1,3) = 1;
                Overlay(x+1,y,3) = 1;
                Overlay(x+1,y+1,3) = 1;
            end
        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,:,i-1) = [x y xp yp];
         
        figure(h)
        imshow(Overlay, 'truesize');
        for j = 1:n-1
            line([landmarkpairs(:,2,j) landmarkpairs(:,4,j)]', [landmarkpairs(:,1,j) landmarkpairs(:,3,j)]','Color', [1 0 0]);
        end
        %line([y yp], [x xp],'Color', [1 0 0]);
        
        %Set x to be xp.
        
        x = yp;
        y = xp;
        
    end
    
end

s = size(landmarkpairs);

for j = 1:n-1
	
    [curfile,errmsg] = sprintf('%s_%i',outfilename,j);
    
    if (isempty(errmsg))
		fid = fopen(curfile, 'w');
		cn = fprintf(fid,'%i\n', s(1));
		
		for i = 1:s(1)
            cn = fprintf(fid,'%f %f %f %f\n', landmarkpairs(i,:,j));
		end
		fclose(fid);
    end
end