function F_lin=txsim_lin(f,dir,n,d)
% TXSIM_LIN  Linearity texture measure
%	F_lin=txsim_lin(f,dir,n,d) computes the linearity measure suggested by
%	H. Tamura, S. Mori, T. Yamawaki, ``Textural Features Corresponding
%	to Visual Perception'', IEEE Trans. Sys., Man, Cyb., June 1978

if exist('n')~=1
  n=16;
end
if exist('d')~=1
  d=4;
end

if length(dir)>1
  F_lin=0;
  for or=dir
    F_lin=F_lin+txsim_lin(f,or,n,d);
  end
  F_lin=F_lin/length(dir);
  return;
end

% Compute cooccurrence matrix
f=round(scale(f,1,n));
P_D=zeros(n,n);
d_v=round(d*cos(dir));
d_h=round(d*sin(dir));
f_d=hshift(vshift(f,d_v),d_h);
for i=1:n
  for j=1:n
    P_D(i,j)=sum(sum((f==i ) & (f_d==j)));
  end
end

% Compute linearity measure
[j,i]=meshgrid(1:n);
F_lin=sum(sum(P_D .* cos((i-j)*2*pi/n))) / sum(P_D(:)); 