%Full picture, and jpeg
x = imread('hipster_cat2.jpeg');
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);
[m,n]=size(x);
s=m/8;
t=n/8;
k=8;
for i=2:k
    for j=1:k
        C(i,j)=sqrt(2/k)*cos((i-1)*(j-1/2)*pi/(k));
    end
end
C(1,1:k)=ones(1,k)/sqrt(k);

for k = 1:s
    for l = 1:t
        xb = xgray(l*8+1:l*8+8,k*8+1:k*8+8);
        xd = double(xb);
        xc=xd-128;
        p = 4;
        Y=C*xc*C';
        %Q = p*8./hilb(8);
        Q = p*[ 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];
        Yq=round(Y./Q)
        Ydq=Yq.*Q;
        Xdq=C'*Ydq*C;
        Xe=Xdq+128;
        Xf(l*8+1:l*8+8,k*8+1:k*8+8)=Xe;
    end
end
imagesc(uint8(Xf));