color = imread('hipster_cat2.jpeg');
figure(1);
imagesc(color);
x_color=double(color);
[m,n,R]=size(color);
color_img=zeros(m,n,R);
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;
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);
p=4;%loss parameter

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];
  
xeY=zeros(s,t);
for i=1:8;
    for j=1:8;
        x=Y(i:i+7,j:j+7);
        xc=x-128;
        y=C*xc*C';
        
yq = round(y./Q);

ydq = yq.*Q;
xdq=C'*yq*C;
xe=xdq+128;
xeY(i:i+7,j:j+7)=xe;
    end
end

xeU=zeros(s,t);
i=0;
j=0;
for i=1:8;
    for j=1:8;
        x=U(i:i+7,j:j+7);
        xc=x-128;
        y=C*xc*C';
        
yq = round(y./Q);

ydq = yq.*Q;
xdq=C'*yq*C;
xe=xdq+128;
xeU(i:i+7,j:j+7)=xe;
    end
end
xeV=zeros(s,t);
i=0;j=0;
for i=1:8;
    for j=1:8;
        x=V(i:i+7,j:j+7);
        xc=x-128;
        y=C*xc*C';
        
yq = round(y./Q);

ydq = yq.*Q;
xdq=C'*yq*C;
xe=xdq+128;
xeV(i:i+7,j:j+7)=xe;
    end
end
B=xeU+xeY;
R=xeV+xeY;
G=(xeY-0.299*R-0.114*B)/0.587;
figure(1); imagesc(uint8(color));
figure(2); imagesc(uint8(color_img));