
X = imread('Pensive Parakeet.jpg');


figure(4);
imagesc(X);

[S,T,K] = size(X);
Z = double(zeros(S,T,3));
Zy = double(zeros(S,T));
Zu = double(zeros(S,T));
Zv = double(zeros(S,T));

Samp1 = zeros(8,8,3);
Samp2 = zeros(8,8,3);


m=8;n=8;n=m;
C = zeros(n,n);

         for i=2:n
             for j=1:n
                 C(i,j)=sqrt(2/n)*cos((i-1)*(j-1/2)*pi/n);
             end
         end
         C(1,1:n)=ones(1,n)/sqrt(n);

         
p = 1;
%Use this Q for linear quantization
%Q = p*8./hilb(8);
        
%This one is for luminance color differences components
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 = p*[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];

X = double(X);

 Y = double(.299*X(:,:,1) + .587*X(:,:,2) + .114*X(:,:,3));
 U = X(:,:,3) - Y;
 V = X(:,:,1) - Y;

% Test = zeros(S,T,3);
% Test = double(Test);
% 
% Test(:,:,3) = U + Y;
% Test(:,:,1) = V + Y;
% Test(:,:,2) = (Y - .299*Test(:,:,1) - .114*Test(:,:,3))/(.587);

for k=1:3

%Xk = X(:,:,k);



if k == 1
    Xk = Y;
end
if k == 2
    Xk = U;
end
if k == 3
    Xk = V;
end



for a=1:8:S
    for b=1:8:T

        
        if k == 1
            Q = Qy;
        else
            Q = Qc;
        end
        
        Xd = Xk(a:a+7,b:b+7);
        Xd = double(Xd);
        Xc = Xd - 128;

        


        Y = C*Xc*C';


        %the loss

        Yq = round(Y./Q);

        %recovering

        Ydq = Yq.*Q;
        Xdq = C'*Ydq*C;
        Xe = Xdq+128;
        %Xf = uint8(Xe);
        
        for c=0:7
            for d=0:7
                
                if k == 1
                    Zy(a+c,b+d) = double(Xe(1+c,1+d));
                end
                if k == 2
                    Zu(a+c,b+d) = double(Xe(1+c,1+d));
                end
                if k == 3
                    Zv(a+c,b+d) = double(Xe(1+c,1+d));
                end
                
            end
        end
        
        %81,81
        %377 121
        
        if a==81 && b==81
            
            Samp1k(:,:,k) = Xe;
            %Here, 1=y , 2=u , 3=v
        
        end
        
        
        if a==105 && b==601
        
            Samp2k(:,:,k) = Xe;
        
        end
        
        %try using 105 , 601 for color...
        
    end
    
end
end


Z(:,:,3) = Zu + Zy;
Z(:,:,1) = Zv + Zy;
Z(:,:,2) = (Zy - .299*Z(:,:,1) - .114*Z(:,:,3))/(.587);
%Z(:,:,3)

figure(1);
imagesc(uint8(Z));



Samp1(:,:,3) = Samp1k(:,:,2) + Samp1k(:,:,1);
Samp1(:,:,1) = Samp1k(:,:,3) + Samp1k(:,:,1);
Samp1(:,:,2) = (Samp1k(:,:,1) - .299*Samp1(:,:,1) - .114*Samp1(:,:,3))/(.587);

figure(2);
imagesc(uint8(Samp1));

Samp2(:,:,3) = Samp2k(:,:,2) + Samp2k(:,:,1);
Samp2(:,:,1) = Samp2k(:,:,3) + Samp2k(:,:,1);
Samp2(:,:,2) = (Samp2k(:,:,1) - .299*Samp2(:,:,1) - .114*Samp2(:,:,3))/(.587);

figure(3);
imagesc(uint8(Samp2));






% figure(7);
% imagesc(uint8(Test));
% 
% figure(5)
% imagesc(uint8(OrigSamp1));
% 
% figure(6)
% imagesc(uint8(OrigSamp2));
