function t = luminance_texton( u )
%LUMINANCE_TEXTON Luminance texton
%   t = luminance_texton( u ) computes a luminance texton.
%
%   The luminance texton has been introduced in the paper
%   "A Compact Representation of Random Phase and Gaussian Textures" 
%   (Agnès Desolneux, Lionel Moisan, Samuel Ronsin), 
%   proceedings of the IEEE ICASSP, pp. 1381-1384, 2012.
%
%   Notice that the luminance texton is here normalized to be a square
%   root of the covariance for the periodic convolution.
%
%   Author : Arthur Leclaire
%   v. 1.0 (03/2014) : first public version
%   v. 1.1 (03/2014) : updated luminance coefficients

[M,N,C] = size(u);
if C~=3
    error('u must have three color channels');
end

mu = repmat(mean(mean(u),2),[M N 1]);
un = 1/sqrt(M*N)*(u-mu);
lum = 0.299*u(:,:,1)+0.587*u(:,:,2)+0.114*u(:,:,3);
phlum = angle(fft2(lum));
t = zeros(M,N,C);
for c=1:3
    t(:,:,c) = real(ifft2(fft2(un(:,:,c)).*exp(-1i*phlum)));
end

end

