% *******************************************************************
% preskladavani zpet podle chaoticke mapy

function [vysledek]=chaos_obraz_zpet(obraz_IIBPC,chaos,B,kolikrat_rotovat) 

vysledek = zeros(size(obraz_IIBPC));
mat_help = struct('obr',{zeros(B)},'label',{});
chaos_help = struct('obr',{zeros(B)},'label',{});

[m,n]=size(obraz_IIBPC);
x_krat = m/B;
y_krat = n/B;

cnt = 0;
    for j=0:(x_krat-1)
        for k=0:(y_krat-1)
             cnt = cnt+1;
             chaos_help{cnt}.obr = obraz_IIBPC(j*B+1:j*B+B,k*B+1:k*B+B);
        end 
    end

for i=1:kolikrat_rotovat
    cnt = 1;  
        for j=0:(x_krat-1)
            for k=0:(y_krat-1)
                index = chaos{cnt}.label;
                mat_help{index}.obr = chaos_help{cnt}.obr;
                cnt = cnt+1;                               % vyhledavani cisel 0.1.2.3...
            end
        end
           chaos_help = mat_help;
end   

cnt = 0;
    for j=0:(x_krat-1)
        for k=0:(y_krat-1)
             cnt = cnt+1;
             vysledek(j*B+1:j*B+B,k*B+1:k*B+B) = chaos_help{cnt}.obr;
        end 
    end
       
% figure(5)
% colormap('Gray')
% imagesc(vysledek)                                % opetovne slozeni obrazu
% title('Zpetne slozen puvodni obraz + vodoznak');