function Project5c(p)

x = imread('cheetah.jpg');
xd = double(x);
r = xd(:,:,1);g = xd(:,:,2);b = xd(:,:,3);
y = 0.299*r + 0.587*g + 0.114*b;
u = b - y;
v = r - y;

% Original 496 x 496 image
figure(1); imagesc(x); axis square;

% JPEG quantization matrix
Q = [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;];
Q = p*Q;

% Color Difference Matrix
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];
  
Xf = zeros(496,496,3);
C = getC();

for i= 1:8:496
    for j = 1:8:496
                % Quantizize
        % Y
        Y = y(i:i+7,j:j+7);
        Y1 = Y - 128;
        Yy = dct(dct(Y1')');
        Yqy = round(Yy./Q);
        % U
        U = u(i:i+7,j:j+7);
        U1 = U - 128;
        Yu = dct(dct(U1')');
        Yqu = round(Yu./Qc);
        % V
        V = v(i:i+7,j:j+7);
        V1 = V - 128;
        Yv = dct(dct(V1')');
        Yqv = round(Yv./Qc);
               
                % Dequantizize
        % Y
        Ydqy = Yqy.*Q;
        Xdqy = C'*Ydqy*C;
        Xey = Xdqy + 128;
   
        % U 
        Ydqu = Yqu.*Qc;
        Xdqu = C'*Ydqu*C;
        Xeu = Xdqu + 128;
  
        % V
        Ydqv = Yqv.*Qc;   
        Xdqv = C'*Ydqv*C;
        Xev = Xdqv + 128;      
        
        % To RGB
        Xeb = Xeu + Xey;    
        Xer = Xev + Xey;
        Xeg = (Xey - 0.299*Xer - 0.114*Xeb)/(.587);
         
        Xf(i:i+7,j:j+7,1) = Xer; 
        Xf(i:i+7,j:j+7,2) = Xeg;
        Xf(i:i+7,j:j+7,3) = Xeb;
        
    end
end
figure(2);imagesc(uint8(Xf)); 
axis square;


