% reconIm = decodeErr(errIm,buffer,xdispl,ydispl,tdel,blocksize); 
% 
% Reconstrucs an image using the quantized error image and displacements.
%
% Inputs:
% -------
%  errIm:  quantized error image
%  buffer: time buffer of previously-reconstructed frames
%  xdispl: image of x-displacements at each block
%  ydispl: image of y-displacements at each block
%  tdel: image of time buffer indices at each block
%  blocksize: width/height of match blocks in pels
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function reconIm = decodeErr(errIm,buffer,xdispl,ydispl,tdel,blksz); 

imsize = size(errIm,1);
nblocks = imsize/blksz;

reconIm = 0.*errIm;

for yblock = 1:nblocks,
  for xblock = 1:nblocks,

  % get vectors representing pixel positions of current block:
  ypels = (yblock-1)*blksz+1:yblock*blksz;
  xpels = (xblock-1)*blksz+1:xblock*blksz;

  % get error image block:
  errblock = errIm(ypels,xpels); 

  sh_ypels = ypels+ydispl(yblock,xblock);
  sh_xpels = xpels+xdispl(yblock,xblock); 

  block = buffer(sh_ypels,sh_xpels,tdel(yblock,xblock)); 

  reconIm(ypels,xpels) = errblock + block;

% done shifting through blocks 
  end
end


% end function 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
