%
% StandardDisparity.m
%
% The standard scheme is where a single integer disparity value is
% specified, and the target image is predicted by averaging the 
% disparity compensated blocks from all the reference images.
%

function [R, D, pdfout, predBlock] = StandardDisparity(...
	Itarget, Irefs, lambda, ...
	indices, pdfin, ...
	rowBlock, colBlock, ...
	blocksize)

R = 0;
D = 0;
J = inf;
i = 0;
r = (rowBlock-1)*blocksize + 1;
c = (colBlock-1)*blocksize + 1;
targetBlock = GetBlock(Itarget, r, c, blocksize);

for disparity = indices

  i = i+1;
  p_i = pdfin(i)/sum(pdfin);
  Rcurr = -log2(p_i);

  % left
  refBlock{1} = GetBlock(Irefs{1}, r, c+disparity, blocksize);
  % right
  refBlock{2} = GetBlock(Irefs{2}, r, c-disparity, blocksize);
  % top
  refBlock{3} = GetBlock(Irefs{3}, r+disparity, c, blocksize);
  % bottom
  refBlock{4} = GetBlock(Irefs{4}, r-disparity, c, blocksize);
  
  avgRefBlock = zeros(blocksize);
  for j=1:4
	avgRefBlock = avgRefBlock + refBlock{j};
  end
  avgRefBlock = avgRefBlock/4;
  
  Dcurr = GetDistortion(avgRefBlock, targetBlock);
  
  Jcurr = Dcurr + lambda*Rcurr;
  
  if (Jcurr < J)
	J = Jcurr;
	R = Rcurr;
	D = Dcurr;
	predBlock = avgRefBlock;
	pdfout = zeros(size(pdfin));
	pdfout(i) = 1;
  end
  
end

%if (max(max(targetBlock)) > 0) 
%  figure(1), myshow(targetBlock);
%  figure(2), myshow(predBlock);
%  fprintf('(r,c) = (%i,%i) ||', rowBlock, colBlock);
%  fprintf('press any key to continue'); pause;
%  fprintf('\n');
%end
