%luminance
clear
% Image Compression code

% getting the original image
C8 = dct(eye(8));
Qy = [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];
Xbig = imread('flower_pic.jpg');
Xbig = double(Xbig);

%cropping image
X_crop = Xbig(2:257,100:355,:);
flower_image = X_crop;

%define rgb components separately
R = flower_image(:,:,1);
G = flower_image(:,:,2);
B = flower_image(:,:,3);

%change to luminance
Y = 0.299*R+0.587*G+0.114*B;
U = B-Y;
V = R-Y;
new_quantized_image = zeros(256,256,3);


p1 = 1;
Qy = p1*Qy;
Q = p1*8./hilb(8);
Qc = p1*Qc;
%quantization steps
for i = 1:8:249
    for j = 1:8:249

        new_Y_image = Y(i:i+7,j:j+7);
        
        % quantized 8x8 image
        
        quantized_imageY = Quantization(new_Y_image,C8,Qy);
        
        %quantized 8 by 8 for U
        new_U_image = U(i:i+7,j:j+7);
      
        quantized_imageU = Quantization(new_U_image,C8,Qc);
        
        %quantized 8 by 8 for V
        new_V_image = V(i:i+7,j:j+7);
        quantized_imageV = Quantization(new_V_image,C8,Qc);
        
        Y(i:i+7,j:j+7) = quantized_imageY;
        U(i:i+7,j:j+7) = quantized_imageU;
        V(i:i+7,j:j+7) = quantized_imageV;
       
    end
end
R = V + Y;
B = U + Y;
G = (Y-0.299*R - 0.114*B)/(0.587);

new_quantized_image(:,:,1) = R;
new_quantized_image(:,:,2) = G;
new_quantized_image(:,:,3) = B;

%original color image
figure(1)
flower_image = uint8(flower_image);
imagesc(flower_image); 

%quantized color image
figure(2)
new_quantized_image = uint8(new_quantized_image);
imagesc(new_quantized_image); 



    