%%% This program hides an image into the original by the algorithm
clear;
clc;
img=double(imread('lena.bmp'));%% Read the original
[row col]=size(img);
sec=double(imread('images.jpg'));%% Read the secret image
%%Standard quantization table
qTable=[16 11 10 16 24 40 51 61;
12 12 14 19 26 58 60 55;
14 13 16 24 40 57 69 56;
14 17 22 29 51 87 80 62;
18 22 37 56 68 109 103 77;
24 35 55 64 81 104 113 92;
49 64 78 87 103 121 120 101;
72 92 95 98 112 100 103 99];
nimg=zeros(row,col);
mrec=zeros(row/8,col/8);
rec=zeros(row/8,col/8);
th=175;
correct=0;
%ptr=1;
for m=1:row/8
for n=1:col/8
s1=img((m-1)*8+1:m*8,(n-1)*8+1:n*8);
d1=dct2(s1);
%u=A(:,:,j,k)-128;
%v=a*u*a';
q1=floor(d1./qTable+0.5);
if sec(m,n)>th
mark=1;
else
mark=0;
end;
w1=embed(q1,mark);
r1=w1.*qTable;
k1=idct2(r1);
nimg((m-1)*8+1:m*8,(n-1)*8+1:n*8)=k1;
end;
end;
%mse=mean(mean((nimg-img).^2));
%psnr=10*log10(255*255/mse)
%calculation
[PSNR_OUT,Z] = psnr(img,nimg);
str = sprintf('PSNR = %f',PSNR_OUT);
disp(str);
%I0 = double(img);
%I1 = double(nimg);
%Id = (I0-I1);
%signal = sum(sum(I0.^2));
%noise = sum(sum(Id.^2));
%SNR = 10*log10(signal/noise);
%disp('SNR Signal to Noise Ratio');
%disp(SNR);
%MSE = noise/numel(I0);
%peak = max(I0(:));
%PSNR = 10*log10(peak^2/MSE);
%disp('PSNR Peak Signal to Noise Ratio');
%disp(PSNR);
%show
subplot(1,3,1)
imshow(uint8(img))
title('Cover image')
subplot(1,3,2)
imshow(uint8(sec))
title('Secret image')
subplot(1,3,3)
imshow(nimg,[])
%AutoImage(nimg)
title('Stego image')
imwrite(uint8(round(nimg)),'Stego image.jpg');