function TextUnitComb=GeneralCodeCominationForTextUnit(NeighborhoodSize,TextCode)

% This function generate the whole possible combinations 
% (without a repetition)for a given codification input (TextCode). 
% The combinations are generated for : (NeighborhoodSize) postions. 
% For separated bands codification of % multi-band images we set 
% NBbande to one. % This function do not check the validity of the input TextCode.
% This validation is performed in another function

% Combinations are generated with Repetition. Order does not matter, and you can repeat!
% factorial(n+r-1)/(factorial(r)*factorial(n-1)):
% n = base, r = NeighborhoodSize
% Number of combinations = factorial(base+NeighborhoodSize-1)/(factorial(NeighborhoodSize)*factorial(base-1));


% INPUTS:
% NBBands : Number of Spectral Bands 
% NBposition: Number of pixels in a neighborhood in each band
% TextCode: Texture Unit codification to be Checked.

% OUTPUTS: TextUnitComb : the combinations (without a repetition)



% Copyright 2011 Abdelmounaime Safia
% Lats Revision December, 12, 2011


TextUnitComb=[];
TempBloc=zeros(1,NeighborhoodSize);
TempBloc(:)=TextCode(1);
TextUnitComb= cat(1,TextUnitComb,TempBloc);

k=NeighborhoodSize;
curcodepos=1;

while k>=1
    while TempBloc(k)~=TextCode(end)
        curcodepos=curcodepos+1;
        TempBloc(k)=TextCode(curcodepos);
        TextUnitComb= cat(1,TextUnitComb,TempBloc);
    end
    s=NeighborhoodSize;
    while TempBloc(s)==TextCode(end)
        s=s-1;
        if s==0;
            k=-1;
            break;
        end
    end
    if k==-1
        break;
    end
    k=s;
    curcodepos=find(TextCode==TempBloc(k));
    TempBloc(k:end)=TextCode(curcodepos+1);
    TextUnitComb= cat(1,TextUnitComb,TempBloc);
    k=NeighborhoodSize;
    curcodepos=find(TextCode==TempBloc(k));
end