function [midpos,sp2sp,imtilt] = ppcalfind2p(immat,tleft,tright,plt)
% Function PPCALFIND2P
%
%    [midpos,sp2sp,imtilt] = ppcalfind2p(immat,tleft,tright,plt)
%
%    PPCALFIND2P is used to find the rotation, centre and scaling of
%    Pepperpot data images for a particular data set, using the position of
%    2 calibration markings on the copper mounting block, each spaced 9mm
%    apart, on a calibration image.  It is a modified version of PPCALFIND,
%    used to estimate the position of the bottom-left and bottom-right
%    narkings if they're not easily visible.
%
%    PPCALFIND2P(IMMAT) loads the image matrix IMMAT and displays it on
%    screen: the user can then find the pixel positions of the top-left and
%    top-right calibration markings and input this information at the
%    prompt in the form of a matrix:
%
%       > Please enter top-left marker coordinates:   [950 950]
%
%      The centre of the image, the rotation of the image and the
%      spot-to-spot spacing are then calculated and displayed on screen.
%
%    The image data should be preloaded using the PEPPERPLOTFUN function.
%
%    [MIDPOS] = PPCALFIND2P(IMMAT) outputs the centre position of the
%    calibration image to the variable MIDPOS.
%
%    [MIDPOS,SP2SP] = PPCALFIND2P(IMMAT) also outputs the spot-to-spot
%    spacing of the mesh, in pixels, to the variable SP2SP.
%
%    [MIDPOS,SP2SP,IMTILT] = PPCALFIND2P(IMMAT) also outputs the tilt of the
%    image in radians to the variable IMTILT.
%
%    PPCALFIND2P(IMMAT,[TLEFT],[TRIGHT]) makes it possible to specify the
%    pixel locations of the top-left and top-right calibration markings,
%    where:
%
%        TLEFT = [TLX TLY] ;    TRIGHT = [TRX TRY] ;
%
%      PPCALFIND2P then generates the positions of the bottom-left and
%    bottom-right markings and passes the results to PPCALFIND.
%
%    PPCALFIND2P(IMMAT,[TLEFT],[TRIGHT],PLT) controls whether the
%    background image is plotted: it PLT = 1, the data and fit are plotted.
%    The default value is 1 (data is plotted).
%
%    The function requires the following input variables:
%
%    IMMAT - The data/image file with the image information, including
%              the file extension and the path (absolute or relative) -
%              must be a string.
%

    if nargin < 1
        error('Need at least 1 input variable: [midpos,sp2sp,imtilt] = ppcalfind2p(immat,tleft,tright,plt)') ;
    elseif nargin > 4
        error('Can only specify 4 input variables: [midpos,sp2sp,imtilt] = ppcalfind2p(immat,tleft,tright,plt)') ;
    elseif ( nargin == 2 )
        error('Must specify coordinates for both markings: [midpos,sp2sp,imtilt] = ppcalfind2p(immat,tleft,tright,plt)') ;
    elseif nargout > 3
        error('Can only specify 3 output variables: [midpos,sp2sp,imtilt] = ppcalfind2p(immat,tleft,tright,plt)') ;
    elseif nargin > 2
        if ( length(tleft) ~= 2 ) | ( length(tright) ~= 2 )
            error('Ruler coordinates must contain X and Y pixels: tleft = [tlx tly]') ;
        end
    end

    if nargin < 4
        plt = 1 ;
    end

    if ischar(immat)

        if plt == 1
            funplt = 2 ;
        else
            funplt = 0 ;
        end

        [ppim] = pepperplotfun(immat,0,[funplt 0]) ;
        immat = ppim ;

    end

    immin = min(min(immat)) ; immax = max(max(immat)) ;

%    Ask for positions of ruler ends if not provided and plot data

    if nargin < 2

        gsim = figure ; imagesc(immat, [immin immax]), axis image ;
        colormap(gray) ;

%        ppbw = edge(immat,'canny') ;
%        bwim = figure ; imagesc(ppbw), axis square ;
%        colormap(gray) ;

        disp(' ') ;
        tleft = input('Please enter top-left marking coordinates:  ') ;
        disp(' ') ;
        tright = input('Please enter top-right marking coordinates:  ') ;
        disp(' ') ;
%        bleft = input('Please enter bottom-left marking coordinates:  ') ;
%        disp(' ') ;
%        bright = input('Please enter bottom-right marking coordinates:  ') ;
%        disp(' ') ;

        figure(gsim), hold on ;
        plot([tleft(1),tright(1)], [tleft(2),tright(2)],'-xr') ;
        hold off ;

    end

%    Find bottom-left and bottom-right points

    hdiff = tright - tleft ;
    vdiff = [-hdiff(2) hdiff(1)] ;
    bleft = tleft + vdiff ;
    bright = tright + vdiff ;

%    Pass results to PPCALFIND

    [midpos,sp2sp,imtilt] = ppcalfind(immat,tleft,tright,bleft,bright,plt) ;

    return








