% deinterlaces by selecting lower field, then
% upsamples to twice height,
% then swarps,
% then downsamples to size it was when deinterlaced,
% then crop


% Toshiba IKUM41, with 7.5mm lens
x = 312.06; % for panasonic was        336.00
y = 228.25; % for panasonic was        274.69
a = 9.760263567e-01; % for panasonic was       9.826702635e-01
b = 2.779294222e-07; % for panasonic was       2.333436093e-07
c = 2.969901299e-13; % for panasonic was       -1.157903449e-13



hak('decide if running this deletes files after downsized: ^c if not sure')




for k = 0:_________insert max frame number________
  dis('********* doing frame %d',k)
  eval(sprintf('[R G B]=loadpnm(''v%03d.ppm'');',k))

  dis('selecting lower field (undersampling in UD direction) of frame %d',k)
  R=undsambot(R); % black line at top indicates bottom better
  G=undsambot(G);
  B=undsambot(B);

  dis('upsampling again in UD direction, frame %d',k)
  R = upsam_field2frame(R);
  G = upsam_field2frame(G);
  B = upsam_field2frame(B);
  [M N]=size(R);
  [Rs Gs Bs] = swarp_ppm(R,G,B,x,y,a,b,c);

  % deinterlace by deletion doesn't make sense once swarped...
  %disp('deinterlacing by deletion')
  %disp('averageing ajacent pixels each row; assuming image width even')
  %R=round((Rs(1:2:M,1:2:N)+Rs(1:2:M,2:2:N))/2);
  %G=round((Gs(1:2:M,1:2:N)+Gs(1:2:M,2:2:N))/2);
  %B=round((Bs(1:2:M,1:2:N)+Bs(1:2:M,2:2:N))/2);

  R=undsamavg(Rs);
  G=undsamavg(Gs);
  B=undsamavg(Bs);

  disp('cropping 1 pixel off top and left, and 2 pixels of bottom and right')
  R=R(2:2+239,2:2+319);
  G=G(2:2+239,2:2+319);
  B=B(2:2+239,2:2+319);

  k_out=k; % possibly renumber images if downsampling sequence in time

  eval(sprintf('savepnm(''pic%03d.ppm'',R,G,B)',k_out))
  eval(sprintf('!rm v%03d.ppm',k))

end%for

