function A = FTCM_generate(texture, M, N, alpha, delta)
% FTCM_generate    Generate predefined texture image from the FTCM texture model
%
%  use: A = FTCM_generate(texture, M, N, alpha, delta);
% ----------------------------------------------------------------------------
%   A       - the returned image (matrix)
%   texture - which example texture to make, the following examples are ready
%             1 : a 6x6 tile        2 : a 4x4 tile
%             3 : a 12x12 tile      4 : a 8x8 tile
%             this argument may be a matrix (of size IxJ) to make one image 
%             containing IxJ subblocks (each of size MxN) of each texture
%   M,N     - give the size of (each block of) the returned image
%   alpha   - angle (in degrees) for the rotation of the texture
%   delta   - optional argument for the sampling distance (gives scale of texture)
%             alpha and delta may be scalar or matrix with same size as texture
% ----------------------------------------------------------------------------
% ex:
% A = uint8(floor(256*FTCM_generate(2, 128, 128, 0, 0.07)));
% A = FTCM_generate([1,2;2,1], 128, 150, 20, 0.073);
% A = FTCM_generate([1,2,2,1;1,1,2,2], 128, 64, 30, 0.053);
% figure(1); clf; imagesc(A); colormap(gray);
% A = FTCM_generate([1,2;3,4],128,128,15,0.013); imagesc(A); colormap(gray);
% % smoothing of the image (blurring), 
% b = [1,4,15,4,1]/25; A = filter2(b'*b, A); imagesc(A); colormap(gray);

%----------------------------------------------------------------------
% Copyright (c) 2002-2013.  Karl Skretting.  All rights reserved.
% University of Stavanger, Signal Processing Group
% Mail:  karl.skretting@uis.no   Homepage:  http://www.ux.uis.no/~karlsk/
% 
% HISTORY:  dd.mm.yyyy
% Ver. 1.0  04.02.2013  Made function based on older functions 
%                       (makeextexture.m, GenIntPol.m, InterpolateImage.m)
%----------------------------------------------------------------------

mfile = 'FTCM_generate';

if (nargin < 3)
    error([mfile,': wrong number of arguments, see help.']);
end
if (nargin < 4)
    alpha = 20;            % degrees
end
if (nargin < 5)
    delta = 0.077;
end

% define the basic patterns of texture 1 and 2
X = cell(4,1);
X{1} = [ 1,1,0,1,1,0; ...
       1,1,0,1,1,0; ...
       2,2,0,2,2,2; ...
       1,1,0,1,1,0; ...
       1,1,0,1,1,0; ...
       2,2,2,2,2,0 ] / 2;
X{3}=kron(X{1},[1,0.8;0.8,1]);  % a 12x12 matrix
X{2} = [ 1,0,1,0; ...
       2,0,2,2; ...
       1,0,1,0; ...
       2,2,2,0 ] / 2;
X{4}=kron(X{2},[1,0.8;0.8,1]);  % a 8x8 matrix

% some variables used as arguments in InterpolateImage
x0 = 0.01;
y0 = 0.01;
type = 1;

% check size of input arguments
[I,J] = size(texture);
if (size(alpha,1)~=I);  alpha=ones(I,1)*alpha(1,:);  end;
if (size(alpha,2)~=J);  alpha=alpha(:,1)*ones(1,J);  end;
if (size(delta,1)~=I);  delta=ones(I,1)*delta(1,:);  end;
if (size(delta,2)~=J);  delta=delta(:,1)*ones(1,J);  end;

A = zeros(M*I,N*J);
for i=1:I
    for j=1:J
        a = alpha(i,j)*pi/180;
        d = delta(i,j);
        x1 = mod(x0+(i-1)*(M*cos(a)*d)-(j-1)*(N*sin(a)*d),1);
        y1 = mod(y0+(i-1)*(M*sin(a)*d)+(j-1)*(N*cos(a)*d),1);
        Aij = zeros(M,N);
        if (texture(i,j) >= 1) && (texture(i,j) <= numel(X))
            Aij = FTCM_interpolate(X{floor(texture(i,j))}, x1, y1, d, d, a, type, M, N); 
        end
        A((1:M)+(i-1)*M,(1:N)+(j-1)*N) = Aij;
    end
end

return

