function[mapping objval] = readFileDat_1(fname)
%% Copyright (C) 2012 Telematic and Telecommunication Laboratory (LTT),       
% Dipartimento di Ingegneria dell'Informazione- Università di Siena
% via Roma 56 53100 - Siena, Italy                   
% 
%----------------------------------------------------------------------
%Permission to use, copy, or modify this software and its documentation
%for educational and research purposes only and without fee is hereby
%granted, provided that this copyright notice and the original authors'
%names appear on all copies and supporting documentation. This program
%shall not be used, rewritten, or adapted as the basis of a commercial
%software or hardware product without first obtaining permission of the
%authors. The authors make no representations about the suitability of
%this software for any purpose. It is provided "as is" without express
%or implied warranty.
%----------------------------------------------------------------------
%
%This is an implementation of the algorithm for removing statistical
%traces left into the histogram of an image by any processing operator. 
%Please refer to the following paper:
%
%M. Barni, M. Fontani, B. Tondi, "A Universal Technique to Hide Traces of 
%Histogram-Based Image Manipulations", ACM Int. Workshop on Multimedia and 
%Security (MMSEC) 2012, Coventry, UK, 2012.
%
%Kindly report any suggestions or corrections to marco.fontani@unisi.it
%
%----------------------------------------------------------------------
%Read output generated by AMPL in file 'fname', and parse it into the
%'mapping' matrix. Parsing is NOT ELEGANT at all, but it works. Some files
%will be generated during execution.

fid = fopen(fname,'r');   

txt = textscan(fid,'%s','delimiter','\n');
a = txt{1};

% assumo che N,D e X siano sempre nelle prime tre righe
N = regexp(a(1),'\d+','match'); 
N = str2num(cell2mat(N{1,1}));
D = regexp(a(2),'\d+','match'); 
D = str2num(cell2mat(D{1,1}));
X = regexp(a(3),'\d+','match'); 
X = str2num(cell2mat(X{1,1}));


% introduco una matrice quadrata X x X
n = zeros(X);

% assumo che il primo elemento di n parta sempre dalla riga 7
  
 j = 0;
 
 z = [];
 w = 1;
   while w < 256
      k = 8 + j;
      b = a{k};
      temp = str2num(b);
      v = w + size(temp,2) - 1;
        for i= k : k + X - 1 
            b = a{i};
            z = [str2num(b)];
           for h = w : v - 1
               n(z(1),h) = z(h - w + 2); 
           end
        end
    w = v;
    j = j + X + 2; 
   end

  
divergence=[];

tok = strtok(a{j});
z=0;
while ~(strcmpi(tok,'divergence'))
    z=z+1;    
    tok = strtok(a{j+z});
end
try
divergence = eval(strrep(a{j+z},'divergence = ',''));
catch ME
    display('Error in file .dat parsing');
end
objval = divergence;


mapping = n;
save 'opt_config.mat' n ;

fs = fopen('distance_opt_config.txt', 'w+');
str = 'N = ' ;
fprintf(fs, '%s\n', str);
fclose(fs);

dlmwrite('distance_opt_config.txt', N , 'delimiter', ' ', '-append');

fs = fopen('distance_opt_config.txt', 'a');
str = 'distance = ' ;
fprintf(fs,'\n');
fprintf(fs, '%s\n', str);
fclose(fs);

dlmwrite('distance_opt_config.txt', divergence , 'delimiter', ' ', '-append');



fclose(fid);


%clear all


