%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% .m script of web demo "Insert title here" 
% Written for GNU Octave
% 
% INSTITUTE OF TELECOMMUNICATIONS  
% University of Stuttgart 
% www.inue-uni-stuttgart.de 
% author: Marc Gauger / Alexander Knaub   
% date: 11.04.2014   
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


function [h_x,r] = entropy2_fun(filename_input, uploadfile, svg_en, webdemo_en, filname_output)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% BEGIN, set up graphical output

% Create an invisible figure.
if (webdemo_en)
  fig = figure(1); set(fig, "visible", "off");
else
  fig = figure(1); set(fig, "visible", "on");
endif

landscape = "-S930,350";
portrait  = "-S640,480";

output_format = landscape; %portrait;     % default


set(0, "defaultlinelinewidth", 2);

if (webdemo_en)
  set(0, "defaultaxesfontsize", 8);
  set(0, "defaultaxesfontname", "Helvetica");
  set(0, "defaulttextfontsize", 8);
  set(0, "defaulttextfontname", "Helvetica");
  const_lw = 2;
elseif (svg_en)
  set(0, "defaultaxesfontsize", 6);
  set(0, "defaultaxesfontname", "Helvetica");
  set(0, "defaulttextfontsize", 6);
  set(0, "defaulttextfontname", "Helvetica");
  const_lw = 1;
else
  set(0, "defaultaxesfontsize", 10);
  set(0, "defaultaxesfontname", "Helvetica");
  set(0, "defaulttextfontsize", 8);
  set(0, "defaulttextfontname", "Helvetica");
  const_lw = 2;
endif

% END, set up graphical output
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if(strcmp(filename_input,"tmp-File"))
	fileID = fopen(uploadfile, 'r');
else
	fileID = fopen(filename_input, 'r');
endif	
A = fread(fileID, inf, 'uint8=>double');
[n, x] = hist(A, 256);
p = n/sum(n);
information=log(1./(p))/log(2);
hold on
bar(x,information,'b','EdgeColor','b', "linewidth",  const_lw);
infinite(isfinite(information))=NaN;
infinite(~isfinite(information))=0;
plot(x,infinite,'.','color','magenta');
hold off
axis([0 255 0 max(information)]);
line([0 255],[8 8],'color','r');
xlabel('symbol x_i');
ylabel('information content I(x_i) [bit/symbol]','rotation',90);
lgnd = legend({filename_input,'infinite value','I(x_i)= \ld(N)=8'},'location','northoutside' )
%legend location northeastoutside
%set(lgnd,'color','black');

fclose(fileID);

print(filname_output, "-dpng","-S640,480");
%print(filname_output, "-dpng");
if (svg_en)
  print(["fig_",filname_output], "-dsvg",output_format);
endif

end
