function co = cooccur(a,dr,dc)
%
% cooccur - Computes gray level cooccurrence matrix (see Haralick 
%           Computer and Robot Vision, pp. 458-460
% On input:
%     a: gray level matrix
%     dr: offset in row
%     dc: offset in col
% On output:
%     co: cooccurrence matrix:  Note: gray levels shifted by adding 1 
%         so as to not have index 0 in Matlab
% Call:
%     co = cooccur(a,dr,dc);
%

[m,n] = size(a);
codim = max(max(a))+1;
co = zeros(codim,codim);

for r1 = 1:m
  for c1 = 1:n
    index1 = a(r1,c1) + 1;
    r2 = r1 + dr;
    c2 = c1 + dc;
    if (r2>0) & (r2<=m) & (c2>0) & (c2<=n)
      index2 = a(r2,c2) + 1;
      co(index1,index2) = co(index1,index2) + 1;
    end
    r2 = r1 - dr;
    c2 = c1 - dc;
    if (r2>0) & (r2<=m) & (c2>0) & (c2<=n)
      index2 = a(r2,c2) + 1;
      co(index1,index2) = co(index1,index2) + 1;
    end
  end
end

co = co/(m*n);