%-------------------------------
% Function  local_sum
%
function local_sum_A = dic_local_sum(A,m,n)

% We thank Eli Horn for providing this code, used with his permission,
% to speed up the calculation of local sums. The algorithm depends on
% precomputing running sums as described in "Fast Normalized
% Cross-Correlation", by J. P. Lewis, Industrial Light & Magic.
% http://www.idiom.com/~zilla/Papers/nvisionInterface/nip.html

[mA,nA]=size(A);

B = [zeros(m,nA+2*n-1);
     zeros(mA,n) A zeros(mA,n-1);
     zeros(m-1,nA+2*n-1)];
s = cumsum(B,1);
c = s(1+m:end,:)-s(1:end-m,:);
s = cumsum(c,2);
local_sum_A = s(:,1+n:end)-s(:,1:end-n);
