% m-file to create reconstructed buffer to test motion estimator.

more off;

frames = readFluoro('/user/julie/study02', 40);

bufflen = 18;
blksize = 16;
dispbits = 4;

for n = bufflen+1:2*bufflen,
  im = frames(:,:,n);
  clear buffer;
  for j = 1:bufflen,
    buffer(:,:,j) = frames(:,:,n-j);
  end
  [xdispl(:,:,n-bufflen), ydispl(:,:,n-bufflen), tdel(:,:,n-bufflen), ...
   errIm(:,:,n-bufflen)] ...
	= blockMatch(im, buffer, blksize, dispbits);
end

quant_errIm = 0.*errIm;

nbits = 3;
for n = 1:bufflen,
  [image_dct, dct_in_columns] = getBlockDCT(errIm(:,:,n),blksize);
  dct_col_quant = quantizeDCTrows(dct_in_columns,nbits); 
  quant_errIm(:,:,n) = ...
     reconFromDCTinRows(dct_col_quant,blksize,size(im,2)/blksize,size(im,1)/blksize);
end

recon_im = 0.*errIm;

for n = bufflen+1:2*bufflen,
  errIm = quant_errIm(:,:,n-bufflen);
  clear buffer;
  for j = 1:bufflen,
    buffer(:,:,j) = frames(:,:,n-j);
  end
  xd = xdispl(:,:,n-bufflen);
  yd = ydispl(:,:,n-bufflen);
  td = tdel(:,:,n-bufflen);
  recon_im(:,:,n-bufflen) = ...
	decodeErr(errIm,buffer,xd,yd,td,blksize);   
end

% write a file so if something happens in encode we don't have
% to do this again.
fid = fopen('study02_reconedbuffer', 'w');
fwrite(fid, recon_im, 'uchar');
fclose(fid);
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

