% makes 2 matrices the same size                          Steve Mann
%
% pads one or both, as needed; useful for image cross-correlation, etc.
%
% Examples:
% [out1,out2] = padsamesize(in1,in2); % pads with NaN (default) to same size
%
% [ap,bp] = padsamesize(a,b,'mean');        % pads with mean value
% [ap,bp] = padsamesize(a,b,'anystring');   % pads with mean value
%
% [ap,bp] = padsamesize(a,b,255); % pads with 255
% [ap,bp] = padsamesize(a,b,NaN); % pads with NaN
%
% [ap,bp] = padsamesize(a,b,NaN,Moffset1,Noffset1,Moffset2,Noffset2);
%
% [ap,bp,Moffset1,Noffset1,Moffset2,Noffset2] = padsamesize(a,b);% return offset
%
% rounds a little toward upper left on odd size differences

function [empty1,empty2] = filling_up_empties_with_in1_and_in2(in1,in2,mynan)

[M1,N1]=size(in1);
[M2,N2]=size(in2);

M=max(M1,M2);
N=max(N1,N2);

if nargin==1
  error('padsamesize.m: requires 2 input images')
end%if
if nargout==1
  error('padsamesize.m: requires 2 output images')
end%if

if nargin==2;  % use default pad value of zero
  disp('padsamesize.m: mynan defaulting to NaN')
  empty1 = NaN*ones(M,N);
  empty2 = empty1;
else % nargin > 2, so myNaN is specified
  if isstr(mynan)
    disp('padsamesize.m: padding each with mean value (mean2nan) of each image')
    empty1 = ones(M,N)*mean2nan(in1);
    empty2 = ones(M,N)*mean2nan(in2);
  end%if
  empty1 = myNaN*ones(M,N);
  empty2 = empty1;
end%if

if nargin<4; Moffset1=floor((M-M1)/2); end%if
if nargin<5; Noffset1=floor((N-N1)/2); end%if
if nargin<6; Moffset2=floor((M-M2)/2); end%if
if nargin<7; Noffset2=floor((N-N2)/2); end%if

empty1(Moffset1:Moffset1+M1-1, Noffset1:Noffset1+N1-1) = in1;
empty2(Moffset2:Moffset2+M2-1, Noffset2:Noffset2+N2-1) = in2;

