% 11.2 Computer Problem 6
clear all; clearvars; close all; clc;
x=imread('Sedona_AZ.JPG');
figure(1);
axis on;
imagesc(x);
title('Sedona, AZ');

%x_temp = x
x_size = size(x)
x_temp = uint8(x);

% Convert to Grayscale (recover Matrix sizE)
x=double(x);
r=x(:,:,1);     % Level 1
g=x(:,:,2);     % Level 2
b=x(:,:,3);     % Level 3
xgray=0.2126*r+0.7152*g+0.0722*b;
xgray=uint8(xgray);
%{
figure(2);
axis on;
imagesc(xgray);
colormap(gray)
title('Grayscale');
%}

% Discrete Cosine Transform (DCT) Matrix C
n=8;
C = zeros(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);

% Adjust image to an 8*8 pixel block based on Grayscale
xgray_size = size(xgray)
sizexgray8 = zeros(size(xgray)/8);
[row,col] = size(sizexgray8);

for i=1:row;
    for j=1:col;
        r1=r(i*8-7:i*8, j*8-7:j*8); %red
        g1=g(i*8-7:i*8, j*8-7:j*8); %green
        b1=b(i*8-7:i*8, j*8-7:j*8); %blue
        % Conversion & Centering
        r2=double(r1);
        r3=r2-128;
        g2=double(g1);
        g3=g2-128;
        b2=double(b1);
        b3=b2-128;
        % Convert RBG Color Data to YUV System
        Y = 0.299*r3 + 0.587*g3 + 0.148*b3;
        U = b3 - Y;
        V = r3 - Y;
        % Apply the 2D-DCT
        ryY=C*Y*C';
        gyU=C*U*C';
        byV=C*V*C';
        % Quantization: Convert YUV System to "Baseline JPEG"
        %               for Luminance & Color Differences
        %               utilizing Qy and Qc Matrices
        %p=1;
        %p=2;
        %p=4;
        p=8;
        %p=16
        %p=32;
        %p=64;
        %p=128;
        %Q=p*8./hilb(8);
        % JPEG Quantization (Compression)
        QyJPEG = [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;];
        Qy=p.*QyJPEG;
        ryYq=round(ryY./Qy);
        QcJPEG = [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;];
        Qc=p.*QcJPEG;
        gyUq=round(gyU./Qc);
        byVq=round(byV./Qc);
        % Recover the image:
        %   1.  JPEG De-Quantization (Decompression)
        ryYdq=ryYq.*Qy;
        gyUdq=gyUq.*Qc;
        byVdq=byVq.*Qc;
        %   2.  Inverse 2D-DCT
        rxYdq=C'*ryYdq*C;
        gxUdq=C'*gyUdq*C;
        bxVdq=C'*byVdq*C;
        %   3.  Transform YUV System back to RGB Color Data
        %           R=V+Y
        %           B=U+Y
        %           G=(Y-0.299R-0.114B)/0.587
        rxdq = bxVdq + rxYdq;
        bxdq = gxUdq + rxYdq;
        gxdq = (rxYdq - 0.299*rxdq - 0.114*bxdq)/0.587;
        %   4.  Offset 128
        rxe=rxdq+128;
        gxe=gxdq+128;
        bxe=bxdq+128;
        %   5.  Converted back to uint8
        rxe=uint8(rxe);
        gxe=uint8(gxe);
        bxe=uint8(bxe);

        % Merge 8*8 pixel blocks
        Xe(i*8-7:i*8, j*8-7:j*8, 1)=rxe;
        Xe(i*8-7:i*8, j*8-7:j*8, 2)=gxe;
        Xe(i*8-7:i*8, j*8-7:j*8, 3)=bxe;
    end
end
% Image output
figure(3);
axis on;
imagesc(uint8(Xe));
%imagesc(Xe);
title(['p = ',num2str(p)]);
x_size =

        2848        4288           3


xgray_size =

        2848        4288