
function plotting_decoder(filename)


[decoded_visualized, dic_fancy] = LZ78decoderB('LZ78_encoded.txt','t');

read_file = fopen('LZ78_encoded_fancy.txt','r');
encoded_stream_fancy=fscanf(read_file,'%c');




%% Figure allignement
f=figure(); 
set(f,'visible','off');
set(gca,'visible','off');

x0=400;
y0=100;
width=600;
height=450;
set(gcf,'position',[x0,y0,width,height])

%% Drawing an empty dicitonary
   empty_latex_table = ['\begin{tabular}{|c|c|}  \multicolumn{2}{c}{\textbf{Decoder Dictionary}}\\ \hline Index&String\\ \hline & \\\hline & \\',...
 '\hline & \\\hline & \\\hline & \\\hline & \\\hline & \\\hline & \\\hline & \\\hline & \\\hline & \\\hline & \\\hline & \\\hline \end{tabular}']; 
a = annotation('textbox',[0.63 0.8 0.2 0.2], 'EdgeColor','none', 'FitBoxToText','on', 'Interpreter', 'latex'); %[x y w h]    
a.FontSize=18;
    set(a, 'String', empty_latex_table);


%%  Write the word "input"

b = annotation('textbox',[0 0.8 0.2 0.2], 'EdgeColor','none', 'FitBoxToText','on', 'Interpreter', 'latex'); %[x y w h]
b.FontSize=18;


     latex_input='\underline{\underline{\textbf{Compressed Input:}}} \\ ';
     set(b, 'String', latex_input);

%% Box for input

b = annotation('textbox',[0.01 0.6 0.6 0.3], 'EdgeColor','black','LineWidth',1,  'Interpreter', 'latex'); %[x y w h]
b.FontSize=18;
     set(b, 'String', '');

%% Draw O"Decoded Output: "

b = annotation('textbox',[0 0.35 0.2 0.2], 'EdgeColor','none', 'FitBoxToText','on', 'Interpreter', 'latex'); %[x y w h]
b.FontSize=18;
     latex_input='\underline{\underline{\textbf{Decoded Output:}}} \\ ';
     set(b, 'String', latex_input);
     
%% Box for Output

b = annotation('textbox',[0.01 0.15 0.6 0.3], 'EdgeColor','black','LineWidth',1, 'Interpreter', 'latex'); %[x y w h]
b.FontSize=18;
     set(b, 'String','');

%% Save default figure
 new_filename = [filename '_0' ];
print(new_filename, '-dpng' );

 
 oldStr='';
 input_index=1;
output='';
input_string='';
fancy_index=1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:length(dic_fancy)
f=figure(); 
set(f,'visible','off');
set(gca,'visible','off');

hold all
x0=400;
y0=100;
width=600;
height=450;
set(gcf,'position',[x0,y0,width,height])
%%  Write the word "input"

b = annotation('textbox',[0 0.8 0.2 0.2], 'EdgeColor','none', 'FitBoxToText','on', 'Interpreter', 'latex'); %[x y w h]
b.FontSize=18;
latex_input='\underline{\underline{\textbf{Compressed Input::}}} \\ ';
set(b, 'String', latex_input);
%% %% Box for input

b = annotation('textbox',[0.01 0.6 0.6 0.3], 'EdgeColor','black','LineWidth',1,  'Interpreter', 'latex'); %[x y w h]
b.FontSize=18;

input_fancy='';
while encoded_stream_fancy(fancy_index) ~= ')'
input_fancy=[input_fancy encoded_stream_fancy(fancy_index)];
fancy_index= fancy_index + 1;
end

input_fancy =[input_fancy, ')'];
if fancy_index < length(encoded_stream_fancy)
fancy_index= fancy_index + 1;
end

input_string_2=[input_string, '\textbf{\underline{',input_fancy,'}}'];
input_string=[input_string, input_fancy,' '];

set(b, 'String', input_string_2);


%% Draw Decoder's Dictionary
if i < 19  %To handle the error of drawing the dicitonary
   latex_table_2=['\begin{tabular}{|c|c|}  \multicolumn{2}{c}{\textbf{Decoder Dictionary}}\\ \hline Index&String\\ \hline',  ...
       [oldStr, '\textbf{\underline{',num2str(i),'}}','&','\textbf{\underline{', dic_fancy{i},'}}','\\ \hline'],...
            ]; 
   latex_table = ['\begin{tabular}{|c|c|}  \multicolumn{2}{c}{\textbf{Decoder Dictionary}}\\ \hline Index&String\\ \hline',  ...
       [oldStr, num2str(i),'&', dic_fancy{i},'\\ \hline'],...
            ]; 
   
   for j=1:(13-i)  %%adds empty cells
       latex_table = [latex_table, ' & \\\hline'];
       latex_table_2 = [latex_table_2, ' & \\\hline'];
   end
    latex_table=[latex_table,' \end{tabular}'];
    latex_table_2=[latex_table_2,' \end{tabular}'];

   oldStr=[oldStr, num2str(i),'&', dic_fancy{i},'\\ \hline'];
   
    a = annotation('textbox',[0.63 0.8 0.2 0.2], 'EdgeColor','none', 'FitBoxToText','on', 'Interpreter', 'latex'); %[x y w h]    
    a.FontSize=18;
    set(a, 'String', latex_table_2);
else
    a = annotation('textbox',[0.62 0.8 0.2 0.2], 'EdgeColor','none', 'FitBoxToText','on', 'Interpreter', 'latex'); %[x y w h]    
    a.FontSize=18;
    set(a, 'String', latex_table);
    
end

%% Draw  "Decoded Output: "

b = annotation('textbox',[0 0.35 0.2 0.2], 'EdgeColor','none', 'FitBoxToText','on', 'Interpreter', 'latex'); %[x y w h]
b.FontSize=18;
     latex_input='\underline{\underline{\textbf{Decoded Output:}}} \\ ';
     set(b, 'String', latex_input);
     
%% Box for output
b = annotation('textbox',[0.01 0.15 0.6 0.3], 'EdgeColor','black','LineWidth',1, 'Interpreter', 'latex'); %[x y w h]
b.FontSize=18;
output_2=[output, '\textbf{\underline{',decoded_visualized(input_index:input_index+length(dic_fancy{i})-1),'}}'];
output =[output, decoded_visualized(input_index:input_index+length(dic_fancy{i})-1),' '];
set(b, 'String', output_2);
input_index=input_index + length(dic_fancy{i});


hold off;
set(gca,'XTick',[]);
set(gca,'YTick',[]);
set(gca,'Box','off');

hold off;
 new_filename = [filename '_' num2str(i)];
print(new_filename, '-dpng' ); 

counter=i;
end

while counter < 40

new_filename = [filename '_' num2str(counter)];
saveas(f,new_filename,'png');
counter = counter + 1;

end

end


%%
function [decoded_visualized, dic_fancy] = LZ78decoderB(encoded_file_name, mode)
%display the received encoded stream to the user


 
%Reads the text file (article) to be decoded.

read_file = fopen(encoded_file_name,'r');
received=fscanf(read_file,'%c');


if(mode == 't')
%Prepares the Output (decoded) file.
%'LZ78_decoded.txt'
write_file = fopen('LZ78_decoded.txt','w');
end

%Prepares input to be decoded in an array of characters
%processing_array = fread(read_file, '*char');
processing_array = received;

fclose(read_file);
clearvars read_file;

%Prepare an empty dictionary and an empty decoded stream string
dic= [];  %empty array

dic_fancy=[];
decoded_stream ='';

prefix='';  
dic_index = 1;
index=1;
codeword_number=1;

last_element_starting_index = 0;  %handles the last element with only dic refrence and no char

%% The core of the decoding algorithm 
while index <= size(processing_array,2)  %while there is still elements to be decoded
    index;
    if codeword_number == 1
        number_of_bits = 1;
    else
        number_of_bits = ceil(log2(codeword_number));
    end
    codeword_number = codeword_number + 1;
    
    codeword_dic_index='';
    
    for counter = 0:number_of_bits-1
       codeword_dic_index=[codeword_dic_index processing_array(index+counter)];
    end
    
    
    if index + number_of_bits - 1 == length(processing_array)  %handles the last element
        
    codeword_dic_index = bin2dec(codeword_dic_index);  %transforms the dic index to decimal
    string = dic{codeword_dic_index};  %fetches the element from the dictionary
    decoded_stream = [decoded_stream string];  %adds the last decoded element to the decoded stream
    
    index = size(processing_array,2) + 1;  %This sets the index so that the while loop is ended
    
    else    
    index = index + number_of_bits;
    
    fetched_char = processing_array(index:index+7);
    fetched_char_fancy=char(bin2dec(fetched_char));

    index = index + 8;
    
    %convert codeword_dic_index from binary to decimal
    codeword_dic_index = bin2dec(codeword_dic_index);
     
    if codeword_dic_index == 0
        string = '';
        string_fancy='';
    else
        
        string = dic{codeword_dic_index};
        string_fancy='';
        for j=1:8:length(string)
            string_fancy=[string_fancy char(bin2dec(string(j:j+7)))];
        end
    end

         output = [string fetched_char];  %pepares output element in ascii
         dic{dic_index} =output;  %add it to the dictionary (in binary)
         dic_fancy{dic_index}=[string_fancy fetched_char_fancy];
         dic_index= dic_index + 1;  %increment dictionary index
         decoded_stream = [decoded_stream output];
    end
    
      
end

if mode == 't'
%% Converts the decoded binary stream to ASCII for visualization
decoded_visualized ='';
for counter=1:8:length(decoded_stream)
    decoded_visualized=[decoded_visualized  char(bin2dec(decoded_stream(counter:counter+7)))];
end

decoded_visualized;

fwrite(write_file, decoded_visualized, '*char');
fclose(write_file);
end



end