% cement a range of images from entrance77 sequence
%
% Examples: c3133=cement77(0,7);       % defaults to frame 0 as reference frame
% Examples: c3133=cement77(6,8,7);     % frame 7 as reference frame
% Examples: c3133=cement77(31,33,31);  % makes frame 31 the reference frame
%
% saves memory by loading images, cementing to make mosaic, then clearing images

function mosaic=cementrange(l1,l2,mref);

if nargin==1
  error('cement77: need at least 2 input args (start and end frame numbers)')
end%if
if nargin==2
  disp(sprintf('cement77: reference frame defaulting to frame %d (lower end of frame range)',l1))
  mref=l1;
end%if

more off

disp('loading from entrance77_parameters_pairwise.mat')
load entrance77_parameters_pairwise_fixed0708
% now in vector ``p''

% extract part used,
% e.g. [1 0 0 0 1 0 0 0],v04, P(01,:),v05, P(02,:),v06)
%p_part=[[1 0 0 0 1 0 0 0]; p(l1+1:l2,:)];
%p_part=[p(l1+1:l2,:)]; % now pintegrate inserts identity
%clear p
P=pintegrate(p,mref); % contains [P0 for frame 0, P1 for frame 1, etc...]
                    % with the reference frame at frame 0, that is.
                    % P(1,:) maps frame 0 to frame 0 (is the identity)
                    % P(2,:) maps frame 1 to frame 0
                    % P(3,:) maps frame 2 to frame 0
                    % P(4,:) maps frame 3 to frame 0
%clear p

increment=1;
if l1>l2
  error('cement77: l1 is greater than l2; no point dealing with this')
%  disp('cement77: l1 is greater than l2; calculating reverse parameters')
%  disp('cement77: this feature still not working')
%  p_inverse=NaN*p;  % initialize to empty
%  for l=l2:l1
%    p_inverse(l,:)=pinverse(p(l,:));
%  end%for
%  p=p_inverse;
%  %l=l1;
%  %l1=l2;
%  %l2=l;
%  increment=-1;
end%if

for l=l1:increment:l2
  eval(sprintf('v%02d=load77(%02d);',l,l))
  % hamming vignetting is a bad idea... perhaps raised cosine better???
  %%disp(sprintf('cement77.m: about to vignette image %d',l))
  %%eval(sprintf('v%02d=bordervignette(v%02d);',l,l))
  %  eval(sprintf('v%02d=border(v%02d);',l,l))
end%for

%s=sprintf('mosaic=cement_cumulative(P(01,:),v%02d, ',l1);
s=sprintf('mosaic=cement_cumulative(P(%02d,:),v%02d, ',l1+1,l1);
for l=l1+increment:increment:l2-increment
  s=[s sprintf('P(%02d,:),v%02d, ',l+1,l)];
end%for
s=[s sprintf('P(%02d,:),v%02d);',l2+1,l2)];

disp('cement77.m: about to call cement')
%keyboard
%string of the form:
%mosaic=cement_cumulative(P(01,:)],v04, P(02,:),v05, P(03,:),v06);
eval(s);

