x=imread('lion2.jpg')
x=double(x);
r=x(:,:,1);g=x(:,:,2); b=x(:,:,3);
xgray=.2126*r + .7152*g + .0722*b;
xgray=uint8(xgray);
%imagesc(xgray);colormap(gray)
n=8;
for i=2:n
   for j=1:n
       D(i,j)=sqrt(2/n)*cos((i-1)*(j-1/2)*pi/n);
   end
end
D(1,1:n)=ones(1,n)/sqrt(n);
p=4;

for k=1:8
    for l=1:8
         Q(k,l)=8*p*((k-1)+(l-1)+1);
    end
end



Xf=zeros(240,240);
for t=1:8:240
    for z=1:8:240
        X=xgray(t:t+7,z:z+7);
        xd=double(X);
        xc=xd-120;
        
        
        Y=D*xc*D';
        YQ=round(Y./Q);

        Ydq=YQ.*Q;
        Xdq=D'*Ydq*D;
        Xe=Xdq+120;
        Xf(t:t+7,z:z+7)=uint8(Xe);
    
    end
end
figure;
imagesc(Xf,[0 255]);
colormap(gray)
axis square;