function psnr = psnrfun(image1, image2, off, maxval);

% image1 = imread('lenalena', 'tif');
% image2 = imread('lena_test.tif', 'tif');
if nargin<4
    maxval = 255;
end
if nargin<3
    off = 5;
end
  
image1 = double(image1);
s = size(image1);
image1 = image1(off+1:(s(1)-off), off+1:(s(2)-off));
% image1 = image1(371:486, 321:436);
% image1 = image1(156:244, 21:125);
%  image1 = image1(207:220, 91:127);
% image1 = image1(124:260, 18:191);
image2 = double(image2);
image2 = image2(off+1:(s(1)-off), off+1:(s(2)-off));
% image2 = image2(371:486, 321:436);
% image2 = image2(155:243, 20:124);
%  image2 = image2(206:219, 90:126);
% image2 = image2(123:259, 17:190);
s = size(image1);
x = image1 - image2;
x = x.^2;
y = sum(sum(x))/(s(1)*s(2));
y = y.^0.5;
psnr = 20*log10(maxval/y);
% clear all;