This is version 1,assume that every pixsel is expandable or shiftable

%%%%This program implement thodi's paper about Prediction-Error%%%
f = double(rgb2gray(f));
%q = rgb2gray(q);
%f = double(q(333:337,333:337));

h = f;
T = 3;
L = 256;
expandible = 0;
N = 0;
U = 0;
R = 0;
Data = round(rand(1,size(f,1)*size(f,2)));
k = 1;

%%%%%%%%%%%%Encode%%%%%%%%%%%%%%
for i = 1:size(f,1)-1
for j = 1:size(f,2)-1
a = f(i+1,j);
b = f(i,j+1);
c = f(i+1,j+1);
if c<= min(a,b)
x = max(a,b);
elseif c>=max(a,b)
x = min(a,b);
else x = a + b -c;
end
pe = f(i,j) - x;
%%%%%%%%%%%calculate the numbers of expandible,N,U
if abs(pe) < T && ((0<= f(i,j)+pe) && (f(i,j)+pe<=L-2))
expandible = expandible + 1;
pe1 = 2*pe + Data(k);
f(i,j) = x + pe1;
k = k + 1;

elseif pe>=T && f(i,j)+T >=-1 && f(i,j)+T <=L-1
N = N + 1;
pe1 = pe + T  ;
f(i,j) = x + pe1;

elseif  pe <=-T && f(i,j)-T >=-1 && f(i,j)-T <=L-1
N = N + 1;
pe1 = pe -T+1;
f(i,j) = x + pe1;

else U = U + 1;
end

end
end
m = f;

%%%%%%%%%%%%%%%%%%decode%%%%%%%%%%%%%%%%%%%%%%%
Data_re = round(rand(1,size(f,1)*size(f,2)));
k = 1;
for i = 1:size(f,1)-1
i = size(f,1)-i;
for j = 1:size(f,2)-1

j = size(f,2)-j;
a = f(i+1,j);
b = f(i,j+1);
c = f(i+1,j+1);
if c<= min(a,b)
x = max(a,b);
elseif c>=max(a,b)
x = min(a,b);
else x = a + b -c;
end
pe1 = f(i,j) - x;

if pe1<2*T && pe1>-2*T+1;
pe = floor(pe1/2);
Data_re(k) = mod(pe1,2);
k = k + 1;
f(i,j) =  x + pe;
elseif pe1 < 0
pe = pe1 + T-1;
f(i,j) = x + pe;
elseif pe1>=0
pe = pe1 - T ;
f(i,j)  = x + pe;
end

end
end