%CoxDetect.m
function sim = CoxDetect(I, J, W, alpha, N)
%COXDETECT Detect wheter a watermark is present in J.
%   sim = COXDETECT(I, J, W, alpha, N) simply extracts the
%   watermark from J and check whether it is similar to the
%   original one (W). I is the original image.
%   alpha and N are optional parameters used during the
%   embedding of the watermark (see COXWMK)
%
%   See: COXWMK, COXTEST, COXEXTRACT, 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 == 3)
   N = 1000;
%  alpha = 0.1;
   alpha = 0.01;
end
if (nargin == 4)
   N = 1000;
end

if (~isrgb(I))
   % First extract the watermark from suspect image
   X = CoxExtract(I, J, alpha, N);
   
   % Compare with original watermark
   sim = Similar(W, X);
else
   
%   % Simple implementation: apply the same algorithm
%   % to each color
%   
%   for l = 1:3
%     % First extract the watermark from suspect image
%     X = CoxExtract(I(:, :, l), J(:, :, l), alpha, N);
%     % Compare with original watermark  
%	sim(l) = similar(W, X);
%   end

   % The process extraction and sim of the watermark was completely 
   % changed.
   % The watermark if present will be found in the Y plane of the 
   % image.
   originalYIQ=RGBtoYIQ(I);
   originalY=originalYIQ(:,:,1);

   marcadaYIQ=RGBtoYIQ(J);
   marcadaY=marcadaYIQ(:,:,1);
   
   % We first extract the watermark from J
   X = CoxExtract(originalY, marcadaY, alpha, N);
   
   % We compare the original with the extracted watermark
   sim = similar(W, X);

end
