function features = features_lbp(image, sbin,conf)
% Calculates lbp features, acording to the configuration structure

% figure(1); imshow(image);
% figure
% img_a=image;
% img_b=image;

% Non-overlapping cells
if ~conf.lbp.overlapping
    
    cells(1) = round(size(image,1)/sbin);
    cells(2) = round(size(image,2)/sbin);

%     disp(cells)
    
    % 'u2'   for uniform LBP
    % 'ri'   for rotation-invariant LBP
    % 'riu2' for uniform rotation-invariant LBP.
    %  no mapping, standard lbp
    
    mapping = getmapping(8,conf.lbp.mapping);
    
    if strcmpi(conf.lbp.clbp,'S+M')
        features = zeros(cells(1)-2,cells(2)-2,mapping.num*2);
    elseif strcmpi(conf.lbp.clbp,'S') || strcmpi(conf.lbp.clbp,'M')
        features = zeros(cells(1)-2,cells(2)-2,mapping.num);
    end

    for row=1:cells(1)-2
        for col=1:cells(2)-2

%             img_b(row*sbin+1:(row+1)*sbin+1,col*sbin+1:(col+1)*sbin+1,1)=255;
%             img_a(row*sbin+1:(row+1)*sbin+1,col*sbin+1:(col+1)*sbin+1,1)=255;
%             
%             figure(1);
%             imshow(img_a); 
%             
%             figure(2);
%             imshow(img_b); 
%             
%             img_a=image;
%             
%             pause
            
            row_il = max(row*sbin+1,1);
            row_ul = min((row+1)*sbin+1,size(image,1));
            
            col_il = max(col*sbin+1,1);
            col_ul = min((col+1)*sbin+1,size(image,2));
            
            
            window = image(row_il:row_ul,col_il:col_ul);

            [clbp_S, clbp_M, ~] = clbp_oo(window,1,8,mapping,'nh');

            if strcmpi(conf.lbp.clbp,'S+M')
                features(row,col,:) = [clbp_S, clbp_M];
            elseif strcmpi(conf.lbp.clbp,'S')
                features(row,col,:) = clbp_S;
            elseif strcmpi(conf.lbp.clbp,'M')
                features(row,col,:) = clbp_M;
            end
        end
    end
    
end

% Overlapping cells

% shapeInserter = vision.ShapeInserter('BorderColor','Custom','CustomBorderColor',[1 0 0]);

% f1=figure; imshow(image);
% f2=figure;

% img_b=image;
% img_a=image;
% hold on

if conf.lbp.overlapping
    %Do it a different way
    
    cells(1) = floor(size(image,1)/sbin);
    cells(2) = floor(size(image,2)/sbin);

    % 'u2'   for uniform LBP
    % 'ri'   for rotation-invariant LBP
    % 'riu2' for uniform rotation-invariant LBP.
    %  no mapping, standard lbp
    
    mapping = getmapping(8,conf.lbp.mapping);
    
    if strcmpi(conf.lbp.clbp,'S+M')
        features = zeros(cells(1),cells(2),mapping.num*2);
    elseif strcmpi(conf.lbp.clbp,'S') || strcmpi(conf.lbp.clbp,'M')
        features = zeros(cells(1),cells(2),mapping.num);
    end

    for row=1:cells(1)-2
        for col=1:cells(2)-2
            
            cp = [row*sbin+sbin/2, col*sbin+sbin/2];
            
            row_il = max(cp(1)-sbin +1,1);
            row_ul = min(cp(1)+sbin +1,size(image,1));
            
            col_il = max(cp(2)-sbin +1,1);
            col_ul = min(cp(2)+sbin +1,size(image,2));
            
%             rectangle = int32([row_il col_il row_ul-row_il col_ul-col_il]);
            
%             img_a(row_il:row_ul,col_il:col_ul,1) = 255; 
%             img_b(row_il:row_ul,col_il:col_ul,1) = 255;
%             image(row_il:row_ul,col_il:col_ul,1) = 1;
            
%             figure(f1);
%             imshow(img_a); 
            
%             figure(f2);
%             imshow(img_b); 
            
%             img_a=image;
%             square = [row_il col_il
%                       row_il col_ul
%                       row_ul col_ul
%                       row_ul col_il];
                  
%             plot(square(:,1),square(:,2))
%             pause
            
            window = image(row_il:row_ul,col_il:col_ul);

            [clbp_S, clbp_M, ~] = clbp(window,1,8,mapping,'nh');

            if strcmpi(conf.lbp.clbp,'S+M')
                features(row+1,col+1,:) = [clbp_S, clbp_M];
            elseif strcmpi(conf.lbp.clbp,'S')
                features(row+1,col+1,:) = clbp_S;
            elseif strcmpi(conf.lbp.clbp,'M')
                features(row+1,col+1,:) = clbp_M;
            end
        end
    end
end