close all;
clear all;

color = imread('baby_animal.jpg');
figure;
imagesc(color);
x_color=double(color);
r=x_color(:,:,1);g=x_color(:,:,2);b=x_color(:,:,3);
y = 0.299*r + 0.587*g + 0.114*b;
u = b - y;
v = r - y;

n = 8;

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=4;

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

Qy = 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];
     
Qc = [17 18 24 47 99 99 99 99; 
      18 21 26 66 99 99 99 99;
      24 26 56 99 99 99 99 99;
      47 66 99 99 99 99 99 99;
      99 99 99 99 99 99 99 99;
      99 99 99 99 99 99 99 99;
      99 99 99 99 99 99 99 99;
      99 99 99 99 99 99 99 99];
  
for k = 0:49
    for l = 0:49
        Y = y(8*l+1:8*l+8,8*k+1:8*k+8);
        Y1 = Y-128;
        Yy = C*Y1*C';
        Yqy = round(Yy./Qy);
        Ydqy=Yqy.*Qy;
        Xdqy=C'*Ydqy*C;
        Xey=Xdqy+128;
        
        
        U = u(8*l+1:8*l+8,8*k+1:8*k+8);
        U1 = U-128;
        Yu = C*U1*C';
        Yqu = round(Yu./Qc);
        Ydqu=Yqu.*Qc;
        Xdqu=C'*Ydqu*C;
        Xeu=Xdqu+128;
        
        V = v(8*l+1:8*l+8,8*k+1:8*k+8);
        V1 = V-128;
        Yv = C*V1*C';
        Yqv = round(Yv./Qc);
        Ydqv=Yqv.*Qc;
        Xdqv=C'*Ydqv*C;
        Xev=Xdqv+128;


        Xeb = Xeu+Xey;
        Xer = Xev+Xey;
        Xeg = (Xey-.299*Xer-0.114*Xeb)/(.587);

        Xf(8*l+1:8*l+8,8*k+1:8*k+8,1)=Xer;
        Xf(8*l+1:8*l+8,8*k+1:8*k+8,2)=Xeg;
        Xf(8*l+1:8*l+8,8*k+1:8*k+8,3)=Xeb;
        
         if k==1&&l==1
            Yqy
            Yqu
            Yqv
         end
        
    end
end

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

