
clc;
clear; 
tic; 

cover=imread('lena.bmp'); 
[m,n]=size(cover); 

message=im2bw(imread('copyright.bmp')); 
[mm,mn]=size(message); 

rand('state',256);

% fun = @dct2;
% J = blkproc(message,[8 8],fun);
f = @(x) x.*25;
J = blkproc(cover,[8 8],f);

J(1)

imshow(J)

delta=2;                %*256;

ax=randperm(m);
ay=randperm(n); 
bx=randperm(m); 
by=randperm(n); 

f = cover;

for r=1:mm 
for s=1:mn 
    if message(r,s)==1 
        f(ax(r),ay(s))=f(ax(r),ay(s))+delta; 
        f(bx(r),by(s))=f(bx(r),by(s))-delta; 
    else 
        f(ax(r),ay(s))=f(ax(r),ay(s))-delta; 
        f(bx(r),by(s))=f(bx(r),by(s))+delta;     
    end 
end 
end 

figure 
subplot(121),imshow(cover); 
subplot(122),imshow(f); 
imwrite((f.*255),'f.jpg','jpg','Quality',50); 
 
rand('state',256); 

 %jednoduchy utok 
 imwrite(uint8(f),'jpeg_n.jpg','jpg','Quality',100); 
 f=imread('jpeg_n.jpg','jpg'); 

ax=randperm(m);
ay=randperm(n); 
bx=randperm(m); 
by=randperm(n);

for r=1:mm 
for s=1:mn 
    if f(ax(r),ay(s))-f(bx(r),by(s))>0 
        watermark(r,s)=1; 
    else 
        watermark(r,s)=0; 
    end 
end 
end 

figure 
subplot(121),imshow(message); 
imwrite((watermark*.255),'vodoznak.jpg','jpg','Quality',100); 
subplot(122),imshow(watermark.*255); 
toc