function xyinput = dic_cpcorr(CORRSIZE, PRECISION, OPTIMIZE, hwid, base, xyinput_in, input)
%   Copyright 1993-2004 The MathWorks, Inc.
%   $Revision: 1.16.4.4 $  $Date: 2004/10/20 17:54:13 $

ncp = size(xyinput_in,1);
xyinput = xyinput_in; % initialize adjusted control points matrix

normxcorr_hw(hwid, 3, xyinput_in(:,1), xyinput_in(:,2));
normxcorr_hw(hwid, 12, input)
new_xy = normxcorr_hw(hwid, 4);

for icp = 1:ncp
    corr_offset = new_xy(icp,:);
    input_fractional_offset = xyinput(icp,:) - round(xyinput(icp,:)*PRECISION)/PRECISION;
    xyinput(icp,:) = xyinput(icp,:) - input_fractional_offset - corr_offset + base(icp).base_fractional_offset;
end


%{
% Just for validation purposes
for icp=1:ncp
    norm_cross_corr = double(normxcorr_hw(hwid, 11, icp, input));

    subpixel = true;
    [xpeak, ypeak, amplitude] = findpeak(norm_cross_corr,subpixel);

    %THRESHOLD = 0.5;
    %if (amplitude < THRESHOLD) 
        % low correlation, unable to adjust
    %    continue
    %end
    
    % offset found by cross correlation
    corr_offset = [ (xpeak-3*CORRSIZE-1) (ypeak-3*CORRSIZE-1) ];

    res(icp,1) = corr_offset(1);
    res(icp,2) = corr_offset(2);

    % eliminate any big changes in control points
    ind = find(abs(corr_offset) > (CORRSIZE-1));
    if ~isempty(ind)
        % peak of norxcorr2 not well constrained, unable to adjust
        continue
    end

    input_fractional_offset = xyinput(icp,:) - round(xyinput(icp,:)*PRECISION)/PRECISION;
    
    % adjust control point
    %xyinput(icp,:) = xyinput(icp,:) - input_fractional_offset - corr_offset + base(icp).base_fractional_offset;
end

%o1=res(500:510,:)'
%n1=new_xy(500:510,:)'
%o2=res(800:810,:)'
%n2=new_xy(800:810,:)'

mydiff = abs(new_xy - res);
sum(mydiff)
max(mydiff)
%}
