function [Normaliz] = generateFileDat_1(ny,nz, discMax)
%% 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
%
%----------------------------------------------------------------------
%Write 'divergence.dat' file for AMPL solver.
%--- INPUT ARGS ---
%-> ny: to-be-mapped histogram
%-> nt: target (i.e., desired) histogram
%-> discMax: maximum allowed displacement

X = 256 ;
%N = 10000000000;
% Py = attacked
% Pz = target
% in generale 0 < D < N*(X - 1), un bound pi� ristretto si
% pu� trovare tenendo conto volta per volta delle ddp Py e Pz

fs = fopen('divergence.dat', 'w+'); % w+ discard existing contents if any 
assert(isinteger(discMax)|| discMax>=0,'Optimization model error: maximum displacement must be a non-negative integer');
fprintf(fs, 'param DiscMax = %d;', discMax);

Pz = nz/ sum(nz);
N = sum(ny); Normaliz = N;
normalization = ['param N := ' num2str(N) ';'];
fprintf(fs, '%s\n', normalization);
% usiamo un D = aN_ lo setto alto, cos� non interviene nella minimizzazione
maximumDistance = ['param D := ' num2str(300*N) ';'];
fprintf(fs, '%s\n', maximumDistance);
fprintf(fs, 'param X = 256;');
fprintf(fs, '\n');
fprintf(fs, '\n');
fclose(fs);

w = [1:X];

fs = fopen('divergence.dat', 'a'); 

str = 'set I := ' ;
fprintf(fs, '%s\n', str);
fclose(fs);

dlmwrite('divergence.dat', w , 'delimiter', ' ', '-append');

fs = fopen('divergence.dat', 'a'); 
fprintf(fs, ';\n');
fclose(fs);



% % determino il vettore distanza vettoriale tra i due istogrammi
% 
% vecD = zeros(1,X);
% vecD = Py - Pz;
% I = [];
% J = [];
% 
% for k = 1:X
%     if vecD(k) > 0
%       I(end + 1) = k;        
%     end
%     if vecD(k) < 0
%         J(end + 1) = k;
%     end
%     
%     
% end


% % %ora carico nel file .dat i due set trovati che mi servono nella
% % %minimizzazione nella giusta forma
% % % comincio con I
% % 
% fs = fopen('HistogramMapping.dat', 'a'); 
% 
% str = 'set I := ' ;
% fprintf(fs, '%s\n', str);
% fclose(fs);
% 
% dlmwrite('HistogramMapping.dat', I , 'delimiter', ' ', '-append');
% 
% fs = fopen('HistogramMapping.dat', 'a'); 
% fprintf(fs, ';\n');
% fclose(fs);
% 
% % passo a j
% fs = fopen('HistogramMapping.dat', 'a'); 
% 
% str = 'set J := ' ;
% fprintf(fs, '%s\n', str);
% fclose(fs);
% 
% dlmwrite('HistogramMapping.dat', J , 'delimiter', ' ', '-append');
% 
% fs = fopen('HistogramMapping.dat', 'a'); 
% fprintf(fs, ';\n');
% fclose(fs);



% % ora occorre inserire il vettore della distanza vettoriale in HistogramMapping.dat che 
% %viene poi dato in pasto ad HistogramMapping.mod  per vedere se �
% %ammissibile
% 
% a = (1:X)'; % indici colonna
% b = vecD';
% 
% fs = fopen('HistogramMapping.dat', 'a'); 
% fprintf(fs, '\n');
% str = 'param vecD :=';
% fprintf(fs, '%s\n', str);
% fclose(fs);
% 
% dlmwrite('HistogramMapping.dat', [a b], 'delimiter', ' ', '-append');
% 
% fs = fopen('HistogramMapping.dat', 'a'); 
% fprintf(fs, ';\n');
% fclose(fs);


% inserisco in 'HistogramMapping.dat' anche il vettore Py chiamato 
% in causa in alcuni vincoli del .mod

a = (1:X)'; % indici colonna
c = ny';

fs = fopen('divergence.dat', 'a'); 
fprintf(fs, '\n');
str = 'param ny :=';
fprintf(fs, '%s\n', str);
fclose(fs);

dlmwrite('divergence.dat', [a c], 'delimiter', ' ', '-append');

fs = fopen('divergence.dat', 'a'); 
fprintf(fs, ';\n');
fclose(fs);


% inserisco in 'HistogramMapping.dat' anche il vettore Pz chiamato 
% in causa in alcuni vincoli del .mod

d = Pz';

fs = fopen('divergence.dat', 'a'); 
fprintf(fs, '\n');
str = 'param Pz :=';
fprintf(fs, '%s\n', str);
fclose(fs);

dlmwrite('divergence.dat', [a d], 'delimiter', ' ', '-append');

fs = fopen('divergence.dat', 'a'); 
fprintf(fs, ';\n');
fclose(fs);




end


