x = imread('flower.jpg');
%image is 700 by 940
x=x(1:640, 201:840, :); %crop the image so that its dimentsion is a multiple of 8
x=double(x);
r=x(:,:,1); 
g=x(:,:,2);
b=x(:,:,3);
xgray=0.2126*r+0.7152*g+0.0722*b;
xgray=uint8(xgray);
%figure(1);imagesc(xgray);colormap(gray);%grid

X=xgray(331:338, 76:83) %extract an 8 by 8 pixel block
%figure(2);imagesc(X, [0 255]); colormap(gray);
Xd=double(X)
Xc=Xd-128
C=dct(Xc);
Y=C*Xc*C';

p=4; % Loss parameter
Q=p*8./hilb(8) %linear quantization matrix
Yq=round(Y./Q)

%to recover the image back
Ydq=Yq.*Q; 
Xdq=C'*Ydq*C;
Xe=Xdq+128;
Xf=uint8(Xe)

figure(3);imagesc(Xf, [0 255]); colormap(gray); %[0 255] so that it gives the right intensity of gray-ness, rather than relative intensity 
%figure(4);imagesc(Xf);colormap(gray) %here you can compare one wihtout [0 255]