%Name:		 TheMask
%Course:	 Data Hiding
%Components: Francesco Filippi, Marco Scarpa, Lorenzo Marchiori, Riccardo Longo
%Project: 	 Block DCT Based method, using comparision between mid-band coefficients
%            Watermark Recovery

function recover = recover(imm_watermarked,imm_original)


blocksize=8;    % set the size of the block in cover to be used for each bit in watermark
marchio=1;

% read in the watermarked object
file_name=imm_watermarked;
watermarked_image=double(imread(file_name));

% determine size of watermarked image
Mw=size(watermarked_image,1);	        %Height
Nw=size(watermarked_image,2);	        %Width

% determine maximum message size based on cover object, and blocksize
max_message=Mw*Nw/(blocksize^2);

% read in original watermark
orig_watermark = importdata('mark19.mat');

% determine size of original watermark
Mo=size(orig_watermark,1)	%Height
No=size(orig_watermark,2)	%Width

% process the image in blocks
x=1;
y=1;
for (kk = 1:max_message)

    % transform block using DCT
    dct_block=dct2(watermarked_image(y:y+blocksize-1,x:x+blocksize-1));
    
    % if dct_block(7,5) > dct_block(4,3) then message(kk)=0
    % otherwise message(kk)=1
    if dct_block(7,5) > dct_block(8,6)
        message_vector(kk)=0;
    else
        message_vector(kk)=1;
    end
 
    % move on to next block. At and of row move to next row
    if (x+blocksize) >= Nw
        x=1;
        y=y+blocksize;
    else
        x=x+blocksize;
    end
end

% reshape the embeded message
message=reshape(message_vector(1:Mo*No),Mo,No);

% read in the original object
cover_object=double(imread(imm_original));

psnr=PSNR_gara(cover_object,watermarked_image)
wpsnr=WPSNR(cover_object,watermarked_image)

% reshape the watermark to a vector
watermark=reshape(orig_watermark,1,Mo*No);

% reshape the message to a vector
message_vec=reshape(message,1,Mo*No);

%eseguo detection
SIM = watermark * message_vec' / sqrt( message_vec * message_vec' );

if SIM > 14.2141
    
    disp('marchio ancora presente');
    marchio
else
    disp('marchio rimosso');
    marchio=0
end

    
