function [T1,N] = CA_A3(T1,white, black) 
%CA_A3 (boji artikulacione cvorove. Oni koji mogu biti dio backbone-a boji u bijelo, a u drugom koraku 
%taguje odgovarajuce susjede koristeci jednu od 12 boja)
%CA_A3 marks in the first pass the articulation cells with white and in the second pass taggs the remaining 
%potentzial articulation cells with one of 12 tagColors.


T2=T1;
N=0;
global cmap;
global Plot;

% stopUnutrasnjeRupe = true;
% stopCluster = false;

tagColor1 = length(cmap)-4;
tagColor2 = length(cmap)-5;
tagColor3 = length(cmap)-6;
tagColor4 = length(cmap)-7;
tagColor5 = length(cmap)-8;
tagColor6 = length(cmap)-9;
tagColor7 = length(cmap)-10;
tagColor8 = length(cmap)-11;
tagColor9 = length(cmap)-12;
tagColor10 = length(cmap)-13;
tagColor11 = length(cmap)-14;
tagColor12 = length(cmap)-15;


for lines=2:size(T1,1)-1
    for columns=2:size(T1,2)-1
        %             stopUnutrasnjeRupe = false;
        if (T1(lines, columns) == black) % if black 
%   %%ADES
%         if (lines == 3) && (T1(lines+1, columns) == T1(size(T1,1)-1, 2) || T1(lines+1, columns) == T1(size(T1,1)-1, size(T1,2)-1)) 
%             T2(lines, columns) = T1(lines+1, columns);  end  %color cells in the second row with S cells color if it is equal to the color corners on lower boundary 
%         if (lines == size(T1,1)-2) && (T1(lines-1, columns) == T1(size(T1,1)-1, 2) || T1(lines-1, columns) == T1(size(T1,1)-1, size(T1,2)-1)) 
%             T2(lines, columns) = T1(lines-1, columns);  end  %color cells in the (last-1) row with N cells' color if it is equal to color of corners on lower boundary 
%   %%end ADES
            if (lines ~= 1 && lines ~= size(T1,1)-1  && columns ~= 1 && columns ~= size(T1,2)-1  ) %not on boundaries
     %           if (T1(lines, columns-1)~= white && T1(lines, columns+1)~= white && T1(lines-1, columns)~= white && T1(lines+1, columns)~= white) %W,E,S,N
                    %tu ni sigurno nobene bele, saj smo vse spremenili v CA1???
                   if (T1(lines, columns+1)== T1(lines, columns-1) && T1(lines, columns+1)~= T1(lines, columns)) ...
                           %&& T1(lines, columns+1)~= white)
                       T2(lines, columns) = T1(lines, columns+1);  end
                   if (T1(lines+1, columns)== T1(lines-1, columns) && T1(lines+1, columns)~= T1(lines, columns)) ...
                           %&& T1(lines+1, columns)~= white)
                       T2(lines, columns) = T1(lines+1, columns);  end

                   %dva crna susjeda
                   if (T1(lines-1, columns)== black && T1(lines, columns+1)== black && T1(lines, columns-1)== T1(lines-1, columns+1) && ...
                           T1(lines, columns-1)== T1(lines+1, columns) && T1(lines, columns-1)~= black)   
                           T2(lines, columns) = T1(lines, columns-1);  end
                   if (T1(lines-1, columns)== black && T1(lines, columns-1)== black && T1(lines, columns+1)== T1(lines-1, columns-1) && ...
                           T1(lines, columns+1)== T1(lines+1, columns) && T1(lines, columns+1)~= black)
                           T2(lines, columns) = T1(lines, columns+1);  end
                   if (T1(lines+1, columns)== black && T1(lines, columns-1)== black && T1(lines, columns+1)== T1(lines+1, columns-1) && ...
                           T1(lines, columns+1)== T1(lines-1, columns) && T1(lines, columns+1)~= black)
                           T2(lines, columns) = T1(lines, columns+1);  end
                   if (T1(lines+1, columns)== black && T1(lines, columns+1)== black && T1(lines, columns-1)== T1(lines+1, columns+1) && ...
                           T1(lines, columns-1)== T1(lines-1, columns) && T1(lines, columns-1)~= black)
                           T2(lines, columns) = T1(lines, columns-1);  end

                   %tri crna susjeda 
                   if (T1(lines-1, columns)== black && T1(lines, columns+1)== black && T1(lines+1, columns)== black &&...
                           T1(lines, columns-1)== T1(lines-1, columns+1) && T1(lines, columns-1)== T1(lines+1, columns+1) && T1(lines, columns-1)~= black)
                           T2(lines, columns) = T1(lines, columns-1);  end
                   if (T1(lines-1, columns)== black && T1(lines, columns+1)== black && T1(lines+1, columns)== black &&...
                           T1(lines, columns-1)== T1(lines-1, columns+1) && T1(lines+1, columns+1)~= T1(lines, columns-1) && T1(lines, columns-1)~= black)
                           T2(lines, columns) = white;  end
                   if (T1(lines-1, columns)== black && T1(lines, columns+1)== black && T1(lines+1, columns)== black &&...
                           T1(lines, columns-1)== T1(lines-1, columns+1) && T1(lines-1, columns+1)~= T1(lines, columns-1) && T1(lines, columns-1)~= black)
                           T2(lines, columns) = white;  end
%
                   if (T1(lines, columns-1)== black && T1(lines, columns+1)== black && T1(lines-1, columns)== black &&...
                           T1(lines+1, columns)== T1(lines-1, columns-1) && T1(lines+1, columns)== T1(lines-1, columns+1) && T1(lines+1, columns)~= black)
                           T2(lines, columns) = T1(lines+1, columns);  end
                   if (T1(lines, columns-1)== black && T1(lines, columns+1)== black && T1(lines-1, columns)== black &&...
                           T1(lines+1, columns)== T1(lines-1, columns+1) && T1(lines-1, columns-1)~= T1(lines+1, columns) && T1(lines+1, columns)~= black)
                           T2(lines, columns) = white;  end
                   if (T1(lines, columns-1)== black && T1(lines, columns+1)== black && T1(lines-1, columns)== black &&...
                           T1(lines+1, columns)== T1(lines-1, columns-1) && T1(lines-1, columns+1)~= T1(lines+1, columns) && T1(lines+1, columns)~= black)
                           T2(lines, columns) = white;  end
%
                   if (T1(lines-1, columns)== black && T1(lines+1, columns)== black && T1(lines, columns-1)== black &&...
                           T1(lines, columns+1)== T1(lines-1, columns-1) && T1(lines, columns+1)== T1(lines+1, columns-1) && T1(lines, columns+1)~= black)
                           T2(lines, columns) = T1(lines, columns+1);  end
                   if (T1(lines-1, columns)== black && T1(lines+1, columns)== black && T1(lines, columns-1)== black &&...
                           T1(lines, columns+1)== T1(lines-1, columns-1) && T1(lines+1, columns-1)~= T1(lines, columns+1) && T1(lines, columns+1)~= black)
                           T2(lines, columns) = white; end
                   if (T1(lines-1, columns)== black && T1(lines+1, columns)== black && T1(lines, columns-1)== black &&...
                           T1(lines, columns+1)== T1(lines+1, columns-1) && T1(lines-1, columns-1)~= T1(lines, columns+1) && T1(lines, columns+1)~= black)
                           T2(lines, columns) = white;  end
%
                   if (T1(lines, columns+1)== black && T1(lines, columns-1)== black && T1(lines+1, columns)== black &&...
                           T1(lines-1, columns)== T1(lines+1, columns-1) && T1(lines-1, columns)== T1(lines+1, columns+1) && T1(lines-1, columns)~= black)
                           T2(lines, columns) = T1(lines-1, columns);  end
                   if (T1(lines, columns+1)== black && T1(lines, columns-1)== black && T1(lines+1, columns)== black &&...
                           T1(lines-1, columns)== T1(lines+1, columns+1) && T1(lines+1, columns-1)~= T1(lines-1, columns) && T1(lines-1, columns)~= black)
                           T2(lines, columns) = white;  end
                   if (T1(lines, columns+1)== black && T1(lines, columns-1)== black && T1(lines+1, columns)== black &&...
                           T1(lines-1, columns)== T1(lines+1, columns-1) && T1(lines+1, columns+1)~= T1(lines-1, columns) && T1(lines-1, columns)~= black)
                           T2(lines, columns) = white;  end

                  %4 crna susjeda
                   if (T1(lines, columns-1)== black && T1(lines, columns+1)== black && T1(lines-1, columns)== black && T1(lines+1, columns)== black &&... %W,E,S,N
                           T1(lines-1, columns-1)== T1(lines+1, columns+1) && T1(lines+1, columns-1)~= T1(lines-1, columns-1) && ...
                           T1(lines-1, columns+1)~= T1(lines-1, columns-1) && T1(lines-1, columns-1)~= black)
                           T2(lines, columns) = white; end
                   if (T1(lines, columns-1)== black && T1(lines, columns+1)== black && T1(lines-1, columns)== black && T1(lines+1, columns)== black &&... %W,E,S,N
                           T1(lines-1, columns+1)== T1(lines+1, columns-1) && T1(lines-1, columns-1)~= T1(lines-1, columns+1) && ...
                           T1(lines+1, columns+1)~= T1(lines-1, columns+1) && T1(lines-1, columns+1)~= black)
                           T2(lines, columns) = white; 
                   end
             %   end
            end
        end
    N=N+1;
    end
end
   if (Plot)      figure; colormap(cmap); image(T2((2:size(T2,1)-1),(2:size(T2,2)-1)));  set(gcf,'renderer','OpenGL'); end
    %title('CA\_A3-white'); 
T1=T2;



%U drugom koraku tagujemo susjede bijelih celija (ovo je drugi korak jer tek nakon prvog imamo bijelih celija na gridu
for lines=2:size(T1,1)-1
    for columns=2:size(T1,2)-1
        if (lines ~= 1 && lines ~= size(T1,1)-1  && columns ~= 1 && columns ~= size(T1,2)-1  ) %not on boundaries 
            if(T1(lines,columns) == black && (T1(lines,columns+1) == white || T1(lines-1,columns) == white || T1(lines+1,columns) == white || T1(lines,columns-1) == white))
                if(T1(lines-1,columns) == white && T1(lines,columns+1) ~= T1(lines-1,columns-1) && ...
                     (T1(lines-1,columns+1) == black || T1(lines-1,columns+1) == tagColor1) && ...
                     T1(lines-1,columns-1) ~= black && T1(lines-2,columns+1) == T1(lines-1,columns-1) && T1(lines,columns+1) ~= black)
                     T2(lines, columns) = tagColor1;  end

                if(T1(lines,columns-1) == white && T1(lines-1,columns) ~= T1(lines+1,columns) && ...
                     (T1(lines+1,columns-1) == black || T1(lines+1,columns-1) == tagColor1) && ...
                     T1(lines-1,columns) ~= black && T1(lines,columns-2) == T1(lines-1,columns)  && T1(lines+1,columns) ~= black)
                     T2(lines, columns) = tagColor1;  end

                if(T1(lines+1,columns) == white && T1(lines+1,columns-1) ~= T1(lines,columns+1) && ...
                     (T1(lines+1,columns+1) == black || T1(lines+1,columns+1) == tagColor1) && ...
                     T1(lines+1,columns-1) ~= black && T1(lines+2,columns+1) == T1(lines+1,columns-1) && T1(lines,columns+1) ~= black)
                     T2(lines, columns) = tagColor6;  end

                if(T1(lines,columns-1) == white && T1(lines-1,columns) ~= T1(lines+1,columns) && ...
                     (T1(lines-1,columns-1) == black || T1(lines-1,columns-1) == tagColor1) && ...
                    T1(lines+1,columns) ~= black && T1(lines,columns-2) == T1(lines+1,columns) && T1(lines-1,columns) ~= black) 
                    T2(lines, columns) = tagColor6;  end

                if(T1(lines+1,columns) == white && T1(lines,columns-1) ~= T1(lines,columns+1) && ...
                     (T1(lines+1,columns-1) == black ||  T1(lines+1,columns-1) == tagColor1) && ...
                     T1(lines,columns+1) ~= black && T1(lines,columns+1) == T1(lines+2,columns) && T1(lines,columns-1) ~= black)
                    T2(lines, columns) = tagColor7;  end

                if(T1(lines,columns+1) == white && T1(lines-1,columns) ~= T1(lines+1,columns+1) && ...
                    (T1(lines-1,columns+1) == black || T1(lines-1,columns+1) == tagColor1) && ...
                    T1(lines+1,columns+1) ~= black && T1(lines-1,columns+2) == T1(lines+1,columns+1) && T1(lines-1,columns) ~= black)
                    T2(lines, columns) = tagColor7;  end

                if(T1(lines,columns-1) == white && T1(lines-1,columns) ~= T1(lines+1,columns-1) && ...
                    (T1(lines-1,columns-1) == black || T1(lines-1,columns-1) == tagColor1) && ...
                    T1(lines+1,columns-1) ~= black && T1(lines-1,columns-2) == T1(lines+1,columns-1) && T1(lines-1,columns) ~= black)
                    T2(lines, columns) = tagColor8;  end

                if(T1(lines+1,columns) == white && T1(lines,columns-1) ~= T1(lines,columns+1) && ...
                    (T1(lines+1,columns+1) == black || T1(lines+1,columns+1) == tagColor1) && ...
                    T1(lines,columns-1) ~= black && T1(lines,columns-1) == T1(lines+2,columns) && T1(lines,columns+1) ~= black)
                    T2(lines, columns) = tagColor8;  end      

                if(T1(lines-1,columns) == white && T1(lines,columns-1) ~= T1(lines-1,columns+1) && ...
                    (T1(lines-1,columns-1) == black || T1(lines-1,columns-1) == tagColor1) && ...
                    T1(lines-1,columns+1) ~= black && T1(lines-1,columns+1) == T1(lines-2,columns-1) && T1(lines,columns-1) ~= black)
                    T2(lines, columns) = tagColor9;  end

                if(T1(lines,columns+1) == white && T1(lines-1,columns) ~= T1(lines+1,columns) && ...
                    (T1(lines+1,columns+1) == black || T1(lines+1,columns+1) == tagColor1) && ...
                    T1(lines-1,columns) ~= black && T1(lines-1,columns) == T1(lines,columns+2) && T1(lines+1,columns) ~= black)
                    T2(lines, columns) = tagColor9;  end

                if(T1(lines+1,columns) == white && T1(lines,columns-1) ~= T1(lines+1,columns+1) && ...
                    (T1(lines+1,columns-1) == black || T1(lines+1,columns-1) == tagColor1) && ...
                    T1(lines+1,columns+1) ~= black && T1(lines+1,columns+1) == T1(lines+2,columns-1) && T1(lines,columns-1) ~= black)
                    T2(lines, columns) = tagColor10;  end

                if(T1(lines,columns+1) == white && T1(lines-1,columns) ~= T1(lines+1,columns) && ...
                    (T1(lines-1,columns+1) == black || T1(lines-1,columns+1) == tagColor1) && ...
                    T1(lines+1,columns) ~= black && T1(lines+1,columns) == T1(lines,columns+2) && T1(lines-1,columns) ~= black)
                    T2(lines, columns) = tagColor10;  end

                if(T1(lines-1,columns) == white && T1(lines,columns-1) ~= T1(lines,columns+1) && ...
                    (T1(lines-1,columns+1) == black || T1(lines-1,columns+1) == tagColor1) && ...
                    T1(lines,columns-1) ~= black && T1(lines,columns-1) == T1(lines-2,columns) && T1(lines,columns+1) ~= black)
                    T2(lines, columns) = tagColor11;  end

                if(T1(lines,columns-1) == white && T1(lines-1,columns-1) ~= T1(lines+1,columns) && ...
                    (T1(lines+1,columns-1) == black || T1(lines+1,columns-1) == tagColor1) && ...
                    T1(lines-1,columns-1) ~= black && T1(lines-1,columns-1) == T1(lines+1,columns-2) && T1(lines+1,columns) ~= black)
                    T2(lines, columns) = tagColor11;  end

                if (T1(lines,columns+1) == white && T1(lines+1,columns) ~= T1(lines-1,columns+1) && ...
                    (T1(lines+1,columns+1) == black || T1(lines+1,columns+1) == tagColor1) && ...
                    T1(lines-1,columns+1) ~= black && T1(lines-1,columns+1) == T1(lines+1,columns+2) && T1(lines+1,columns) ~= black)
                    T2(lines, columns) = tagColor1;  end

                if(T1(lines-1,columns) == white && T1(lines,columns-1) ~= T1(lines,columns+1) &&  ...
                    (T1(lines-1,columns-1) == black ||  T1(lines-1,columns-1) == tagColor1) && ...
                    T1(lines,columns+1) ~= black && T1(lines,columns+1) == T1(lines-2,columns) && T1(lines,columns-1) ~= black)
                    T2(lines, columns) = tagColor1;  end

                %za 4 susjeda
                if ((T1(lines-1,columns) == white && T1(lines,columns-1) ~= T1(lines,columns+1) && ...
                    T1(lines-1,columns+1) == black && T1(lines-1,columns-1) == black && T1(lines-2,columns) == black && T1(lines,columns+1) ~= black && ...
                    T1(lines,columns+1) == T1(lines-2,columns-1) ) ||...
                    (T1(lines,columns+1) == white && T1(lines-1,columns) ~= T1(lines+1,columns) && T1(lines-1,columns+1) == black && T1(lines+1,columns+1) == black && ...
                    T1(lines,columns+2) == black && T1(lines-1,columns) ~= black &&  T1(lines-1,columns) == T1(lines+2,columns+2)))
                    T2(lines, columns) = tagColor2;  end

                if((T1(lines,columns-1) == white && T1(lines+1,columns) ~= T1(lines-1,columns) && ...
                    T1(lines-1,columns-1) == black && T1(lines+1,columns-1) == black && T1(lines,columns-2) == black && T1(lines+1,columns) ~= black && ...
                    T1(lines+1,columns) == T1(lines-1,columns-2)) ||...
                    (T1(lines+1,columns) == white && T1(lines,columns+1) ~= T1(lines,columns-1) && T1(lines+1,columns+1) == black && T1(lines+1,columns-1) == black && ...
                    T1(lines+2,columns) == black && T1(lines,columns-1) ~= black && T1(lines,columns-1) == T1(lines+2,columns+1)))
                    T2(lines, columns) = tagColor3;  end

                if ((T1(lines-1,columns) == white && T1(lines,columns-1) ~= T1(lines,columns+1) && ...
                    T1(lines-1,columns+1) == black && T1(lines-1,columns-1) == black && T1(lines-2,columns) == black && T1(lines,columns-1) ~= black && ...
                    T1(lines,columns-1) == T1(lines-2,columns+1)) ||...
                    (T1(lines,columns-1) == white && T1(lines-1,columns) ~= T1(lines+1,columns) && T1(lines-1,columns-1) == black && T1(lines+1,columns-1) == black && ...
                    T1(lines,columns-2) == black && T1(lines-1,columns) ~= black && T1(lines-1,columns) == T1(lines+1,columns-2)))
    %                 (T1(lines,columns+1) == white && T1(lines-1,columns) ~=
    %                 T1(lines+1,columns) && T1(lines-1,columns+1) == black && T1(lines+1,columns+1) == black && 
    %                 T1(lines,columns+2) == black && T1(lines+1,columns) ~= black && T1(lines+1,columns) == T1(lines-1,columns+2)))
                    T2(lines, columns) = tagColor4;  end


    %             if ((T1(lines,columns-1) == white && T1(lines+1,columns) ~= T1(lines-1,columns) && 
    %                 T1(lines-1,columns-1) == black && T1(lines+1,columns-1) == black && 
    %                 T1(lines,columns-2) == black && T1(lines-1,columns) ~= black && 
    %                 T1(lines-1,columns) == T1(lines+1,columns-2)) ||
                if ((T1(lines,columns+1) == white && T1(lines+1,columns) ~= T1(lines-1,columns) && ...
                    T1(lines-1,columns+1) == black && T1(lines+1,columns+1) == black && T1(lines,columns+2) == black && T1(lines+1,columns) ~= black && ...
                    T1(lines+1,columns) == T1(lines-1,columns+2)) ||...
                    (T1(lines+1,columns) == white && T1(lines,columns+1) ~= T1(lines,columns-1) && T1(lines+1,columns+1) == black && T1(lines+1,columns-1) == black && ...
                    T1(lines+2,columns) == black && T1(lines,columns+1) ~= black && T1(lines,columns+1) == T1(lines+2,columns-1)))
                    T2(lines, columns) = tagColor5;  end
            end
        end
    N=N+1;
    end
end
% if (Plot)       figure; colormap(cmap); image(T2((2:size(T2,1)-1),(2:size(T2,2)-1))); title('CA\_A3-tagged'); colorbar;  set(gcf,'renderer','OpenGL'); end
T1=T2;

