% cements 2 or more frames together, given the CUMULATIVE cross chirp between
%
% parameters are vectors, cumulatively expressed already
%
% Examples of use: first use P=pintegrate(p,3); % make cumulative, select lref
%                : ementcolor(P,l1,l2,skip_increment,filename);
%                : cementcolor(P,73,98,3,'c73_98r03'); % every third frame
%                : cementcolor(P,98,73,-1,'c98_73r03'); % returns nothing
%                : cementcolor(P,98,73,-1,'c98_73r03',70,100); % bigger canvas
%                                                              % for figures,
%                                                              % illustrative
%                : cementcolor(P,98,73,-1,'c98_73r03',70,100,80);% border on v80
% See also: cement_cumulative which takes a list of variables
%                             rather than calling loadframe
%
% calls ``loadframer.m'', loadframeg.m, loadframeb.m to get input data

function myname(P,l1,l2,skip_increment,myfilename,l1_pork,l2_pork,borderedframe) % returns nothing

if nargin>=7
  if l1_pork > l2_pork
    error('cement: parameters providing extra "pork" support in wrong order')
  end%if
  if l1_pork > min(l1,l2)
    error('cement: first parameter''s ``pork'''' cannot be negative')
  end%if
  if l2_pork < max(l1,l2)
    error('cement: second parameter''s ``pork'''' cannot be negative')
  end%if
end%if

if nargin==6
  error('cement: 6 arguments not handled now; must be 5 or 7 or 8')
end%if

if nargin<5
  error('cement: right now you need all 5 input arguments (no defaluts)')
end%if

if max(max(size(l1))) > 1
  error('cement: starting frame number (l1) must be a scalar')
end%if
if max(max(size(l2))) > 1
  error('cement: ending frame number (l2) must be a scalar')
end%if
if max(max(size(skip_increment))) > 1
  error('cement: skip_increment must be a scalar')
end%if
if isstr(l1)
  error('cement: starting frame (l1) must be a NUMBER, not a string')
end%if
if isstr(l2)
  error('cement: ending frame (l2) must be a NUMBER, not a string')
end%if
if isstr(skip_increment)
  error('cement: skip_increment must be a NUMBER, not a string')
end%if


[M_P, N_P]=size(P);
if N_P ~= 8
  error('cement2: input parameters must be in matrix with rows of width 8')
end%if
if isstr(P)
  error('cement2: input parameters must be numbers not a string')
end%if
if ~isreal(P)
  error('cement2: input parameters must be real')
end%if

if nargin <7 % no "pork" (e.g. tight boundingbox)
  [Ml,Nl,Mh,Nh]=boundingboxextrema([P(l1+1:skip_increment:l2+1,:)]);
  % +1 needed, e.g. v03 to v15 uses P(4:16,:)
else%if  % pork (e.g. loose boundingbox for illustrative mpeg sequences, etc)
  [Ml,Nl,Mh,Nh]=boundingboxextrema([P(l1_pork+1:abs(skip_increment):l2_pork+1,:)]);
  % +1 needed, e.g. v03 to v15 uses P(4:16,:)
% note that P includes reference parameter which may or may not be the identity
end%if

% the following not appropriate for batch jobs
%answer=input(sprintf('would you like to delete ``%s'''' to continue?  ',myfilename),'s');
%  if ~(strcmp(answer,'y') | strcmp(answer,'Y') | strcmp(answer,'yes'))
%  dis('cementcolor: you decided not to delete %s',myfilename)
%  error('cementcolor: thus I am exiting')
%end%if
eval(sprintf('!rm -r %s',myfilename))
eval(sprintf('!mkdir %s',myfilename))

for channel=0:2
  disp(sprintf('cementcolor: about to do channel %d',channel))
  for l=l1:skip_increment:l2
    if channel == 0
      eval(sprintf('v%02d=loadframer(%02d);',l,l))
    end%if
    if channel == 1 
      eval(sprintf('v%02d=loadframeg(%02d);',l,l))
    end%if
    if channel == 2
      eval(sprintf('v%02d=loadframeb(%02d);',l,l))
    end%if
    if (nargin==8)  % if a frame was selected for bordered highlight
      if l==borderedframe
        dis('cement: highlighting frame %02d with black border',l)
        eval(sprintf('v%02d=borderblack(v%02d,5,2);',l,l))
      end%if
    end%if

    if l==l1 % find size from first frame, hope that all frames same size
      [M, N]=size(eval(sprintf('v%02d',l1)));
      % base on size of one of frames which should be equal so base on frame l1
      canvassize=[M*(Mh-Ml),  N*(Nh-Nl)];
      canvassize=2*ceil(canvassize/2);  % getting even with mpeg
      disp(sprintf('cementcolor: about to nalloc %d by %d canvas (approx %7.3g Meg)',canvassize, 8*prod(canvassize)/(1024.^2)))
      empty=NaN*ones(ceil(canvassize));
    else%if % each time, check that other frames are same size as first
      [Mnow, Nnow]=size(eval(sprintf('v%02d',l)));
      if Mnow~=M
        error(sprintf('cement: frame %d different height than first frame',l))
      end%if
      if Nnow~=N
        error(sprintf('cement: frame %d different width than first frame',l))
      end%if
    end%if l==0
  % now insert frame l:
    eval(sprintf('[v%02dd,Ml%02d,Nl%02d]=pchirp2nocrop(P(%02d+1,:),v%02d);',...
               l,l,l,l,l))  % note P(4,:) pertains to v03, etc.
    eval(sprintf('clear v%02d',l))
    eval(sprintf('[M%02d,N%02d]=size(v%02dd);',l,l,l)) % size after dechirp
    eval(sprintf('Mh%02d=Ml%02d+(M%02d-1)/M%02d; Nh%02d=Nl%02d+(N%02d-1)/N%02d;',l,l,l,l,l,l,l,l))
    eval(sprintf('Mpixell%02d=round(M*Ml%02d+1   - M*Ml);',l,l))
    eval(sprintf('Mpixelh%02d=round(M*Ml%02d+M%02d - M*Ml);',l,l,l))
    eval(sprintf('Npixell%02d=round(N*Nl%02d+1   - N*Nl);',l,l))
    eval(sprintf('Npixelh%02d=round(N*Nl%02d+N%02d - N*Nl);',l,l,l))
    % uncomment 1 of these (max, mean, new, newbroken):
    % eval(sprintf('empty(Mpixell%02d:Mpixelh%02d, Npixell%02d:Npixelh%02d)=maxnan(empty(Mpixell%02d:Mpixelh%02d, Npixell%02d:Npixelh%02d),v%02dd);',l,l,l,l,l,l,l,l,l))
    % eval(sprintf('empty(Mpixell%02d:Mpixelh%02d, Npixell%02d:Npixelh%02d)=meannan(empty(Mpixell%02d:Mpixelh%02d, Npixell%02d:Npixelh%02d),v%02dd);',l,l,l,l,l,l,l,l,l))
    % eval(sprintf('empty(Mpixell%02d:Mpixelh%02d, Npixell%02d:Npixelh%02d)=newnan(empty(Mpixell%02d:Mpixelh%02d, Npixell%02d:Npixelh%02d),v%02dd);',l,l,l,l,l,l,l,l,l))
    eval(sprintf('empty(Mpixell%02d:Mpixelh%02d, Npixell%02d:Npixelh%02d)=feathernewnan(empty(Mpixell%02d:Mpixelh%02d, Npixell%02d:Npixelh%02d),v%02dd,21);',l,l,l,l,l,l,l,l,l))
    % eval(sprintf('empty(Mpixell%02d:Mpixelh%02d, Npixell%02d:Npixelh%02d)=v%02dd;',l,l,l,l,l))
    eval(sprintf('clear v%02dd',l))
  end%for

  eval(sprintf('saveraw(''%s/data%d'',round(chnan(empty,255)))',myfilename,channel))  % presumed on interval from 0..255
end%for channel
clear empty 
disp('cementcolor: about to cat 3 channels data0, data1, and data2 into data')
eval(sprintf('!cat %s/data0 %s/data1 %s/data2 > %s/data',...
     myfilename, myfilename, myfilename, myfilename))
dis('cementcolor: about to remove %s/data0, %s/data1, and %s/data2',...
    myfilename, myfilename, myfilename)
eval(sprintf('!rm %s/data0',myfilename))
eval(sprintf('!rm %s/data1',myfilename))
eval(sprintf('!rm %s/data2',myfilename))

eval(sprintf('myintfid=fopen(''%s/descriptor'',''w'');',myfilename))
fprintf(myintfid,'(_data_files 1)\n');
fprintf(myintfid,'(_data_sets 1)\n');
fprintf(myintfid,'(_channels 3)\n');
fprintf(myintfid,'(_dimensions %d %d)\n',canvassize);
fprintf(myintfid,'(_data_type unsigned_1)\n');
clockdat=fix(clock);
fprintf(myintfid,'(_comment generated from Matlab by Steve Mann, %s; %dh%d; %d)\n',date,clockdat(4:6));
fclose(myintfid);

