%CoxExtract.m
function X = CoxExtract(I, J, alpha, N)
%COXEXTRACT Extract a watermark from an image
%   Given an original image I and a watermarked version J
%   (probably modified), COXEXTRACT extracts a watermark
%   from J using some knowledge about I.
%
%   See: COXWMK, COXDETECT, COXTEST, SIM

%   This is a modified version of Fabien Petitcolas' implementation
%   of Cox's technique by Gabriela Delfino and Fabian Martinez
%
%   Fabien Petitcolas' website: http://petitcolas.net/fabien/

if (nargin == 2)
   N = 1000;
%  alpha = 0.1;
   alpha = 0.1;
end
if (nargin == 3)
   N = 1000;
end

if (isrgb(I))
   error('Use grayscale image only');
end

sI = size(I);
if ((sI(1) * sI(2)) < N)
   error('Image too small or too many coefficients');
end
sJ = size(J);
if ((sI(1) ~= sJ(1)) | (sI(2) ~= sJ(2)))
   error('The images have different size.');
end

% Compute the DCT of the images
DCTI = dct2T(I);
DCTJ = dct2T(J);

% Find the N largest coefficients in the DCT matrix
Index = FindNLargest(abs(DCTI), N);

% Extract the watermark
for i = 1:N
   X(i) = (DCTJ(Index(1, i), Index(2, i)) / DCTI(Index(1, i), Index(2, i)) - 1) / alpha;
end
