close all;
clear all;
clc;

x = imread('baby_animal.jpg');
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);
%imagesc(xgray);
colormap(gray)


for k = 0:49
    for l = 0:49
        xb = xgray(l*8+1:l*8+8,k*8+1:k*8+8);
        xd = double(xb);
        xc=xd-128;
        [m,n] = size(xc); m=n;
        for i=1:n
            for j=1:n
                C(i,j)=cos((i-1)*(2*j-1)*pi/(2*n));
            end
        end
        C=sqrt(2/n)*C;
        C(1,:)=C(1,:)/sqrt(2);
        
        p = 1;
        
        Y=C*xc*C';
        Q = p*8./hilb(8);
        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

str1 = 'p = ';
str2 = int2str(p);
s = [str1 str2];

imagesc(uint8(Xf));
title(s)