% converts complex affine parameters to necessary pchirp parameters.
%
% this group y=ax+b has 4 real degrees of freedom (transr,transi,zoom,rot)
%
% Use: p=caffine2p(exp(i*30*o)) % gives rotation 30deg for a square image
%                               % (non-square image would shear some)
%      p=caffine2p(1+i)         % gives rot 45deg and zoom by sqrt(2)
%
%      p=caffine2p(1+i,0,1+i)    or   p=caffine2p(1+i,0,[1,1]) 
%      will rotate a non-square image properly
%
%      p=caffine2p(1,.1+.2*i)   % translates the image 10% down and 20% right
%      p=caffine2p(1,12+13*i,sizec(image)) % translates image 12 pixels down
%                               % and 13 pixels to the right

% 
% 

function p = parametermapping(a,b,image_size)

if nargin == 1
  disp('caffine2p: No translation specified, therefore assuming you want b=0.');
  b=0;
end%if
 
if nargin <= 2
  disp('caffine2p: No image size specified; assuming image is 1 by 1;')
  disp('           this will have the effect of normalizing the image')
  disp('           so the 4 corners will lie at [0,i,1,1+i]')
  disp('           and so non-square images will shear as they rotate.')
  image_size = 1+i;
end%if

if min(size(image_size)) > 1
  disp('caffine2p: image size must be a complex number or a real 2-vector')
end%if
if length(image_size) > 2
  disp('caffine2p: image size must have length=1 (complex) or length=2 (real)')
end%if

if length(image_size) == 2  % allow user to enter as 480+i*512 as [480,512]
  image_size=image_size(1) + i*image_size(2);
end%if

% real axis points down, imaginary axis points right
%
% corner layout:
%
% 1--2
% |  |
% 3--4

M = real(image_size);
N = imag(image_size);

corner1 = 0;
corner2 = i*N;
corner3 = 1*M;
corner4 = 1*M+i*N;

X = [corner1 corner2 corner3 corner4];  % domain of mapping defines 4 corners

%Y = a*X + b;  % caffine transformation for rechirp parameters (corners2r.mex)
%Y = (X-b)/a;  % caffine transformation for dechirp parameters (corners2d.m)
Y = a*X+b;  % caffine transf for dechirp par.: this agrees with h=g(aZ+b)

%cornersr(1:2:7) = real(Y)/M;  % corner parameters for rechirp
%cornersr(2:2:8) = imag(Y)/N;
cornersd(1:2:7) = real(Y)/M;  % corner parameters for dechirp
cornersd(2:2:8) = imag(Y)/N;

%p = corners2r(cornersr);  % calls mex file to get p-chirp parameters
p = corners2d(cornersd);  % calls m file; less numerical err than big mex file 

