function changetoYUVOrRGB(p)

%Display image as a matrix
x = imread('C:\Users\cnelso17\Desktop\project5.jpg');
[height, width, dim] = size(x); %Establish Dimensions of Matrix

%Breaks into 8x8 blocks
h8=floor(height/8);
w8=floor(width/8);
h88=h8*8;
w88=w8*8;

%Restricts the image to pixel dimensions divisible by 8.
x = x(1:h88,1:w88,:);

%Display image.
imagesc(x);figure(2);

%Quantization matricies.

%JPEG
Q_Y = 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];
   
%YUV   
Q_C = [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];

%Depending on which way you are going:
%Have only #1s uncommented or #2s uncommented

%Converts one way.
XRGBYUV = changetoYUV(x);  %#1
%XRGBYUV = changetoRGB(x);   %#2

%Quantize, deQuantize.
q = QuantizeLUM(XRGBYUV,Q_Y,Q_C,h8,w8);
d = deQuantizeLUM(q,Q_Y,Q_C,h8,w8);

%Converts back.
d = changetoRGB(d);    %#1
%d=changetoYUV(d);       %#2

%Prints image.
imagesc(uint8(d),[0 255])

%Changes the image to YUV.
function x = changetoYUV(X_RGB)
x = double(X_RGB);

r = x(:,:,1); %Defining Red.
g = x(:,:,2); %Defining Green.
b = x(:,:,3); %Defining Blue.

%Defines YUV
y = .299*r + .587*g + .114*b;
u = b - y;
v = r - y;

%Appends into the original matrix.
x(:,:,1) = y;
x(:,:,2) = u;
x(:,:,3) = v;

%Changes the image to RGB.
function d = changetoRGB(Y_YUV)
d = double(Y_YUV);

y = d(:,:,1); %Defining Y.
u = d(:,:,2); %Defining U.
v = d(:,:,3); %Defining V.

%Defines RGB.
r = v + y;
b = u + y;
g = (y - .299*r - .114*b)/(.587);

%Appends into the original matrix
d(:,:,1) = r;
d(:,:,2) = g;
d(:,:,3) = b;


%Quantiezs the image w.r.t. each color.
function img = QuantizeLUM(XLUM,Q_Y,Q_C,h8,w8)
img=zeros(h8*8,w8*8,3);

%for Y/R
for i = 1:h8
     for j = 1:w8
        xb = XLUM((i-1)*8+1:i*8,(j-1)*8+1:j*8,1);
        xb = double(xb);
        Xc = xb - 128;
        Y = dct(dct(Xc')');
        Yq = round(Y./Q_Y);
        img((i-1)*8+1:i*8,(j-1)*8+1:j*8,1) = Yq;
     end
end
 %for U/G
 for i = 1:h8
     for j = 1:w8
        xb = XLUM((i-1)*8+1:i*8,(j-1)*8+1:j*8,2);
        xb = double(xb);
        Xc = xb - 128;
        Y = dct(dct(Xc')');
        Yq = round(Y./Q_C);
        img((i-1)*8+1:i*8,(j-1)*8+1:j*8,2) = Yq;
    end
end
 %for V/B
for i = 1:h8
     for j = 1:w8
        xb = XLUM((i-1)*8+1:i*8,(j-1)*8+1:j*8,3);
        xb = double(xb);
        Xc = xb - 128;
        Y = dct(dct(Xc')');
        Yq = round(Y./Q_C);
        img((i-1)*8+1:i*8,(j-1)*8+1:j*8,3) = Yq;
     end
end

%Dequantizes the image w.r.t. each color.
function img = deQuantizeLUM(yq,Q_Y,Q_C,h8,w8)
    img=zeros(h8*8,w8*8,3);
 %for Y/R
 for i = 1:h8
     for j = 1:w8
        Yq = yq((i-1)*8+1:i*8,(j-1)*8+1:j*8,1); 
        Ydq = Yq.*Q_Y;
        Xdq = idct(idct(Ydq')');
        Xf = Xdq + 128;
        img((i-1)*8+1:i*8,(j-1)*8+1:j*8,1) = Xf;
     end
 end
 %for U/B
for i = 1:h8
     for j = 1:w8
        Uq = yq((i-1)*8+1:i*8,(j-1)*8+1:j*8,2); 
        Udq = Uq.*Q_C;
        Xdq = idct(idct(Udq')');
        Xf = Xdq + 128;
        img((i-1)*8+1:i*8,(j-1)*8+1:j*8,2) = Xf;
     end
end
 %for V/G
for i = 1:h8
     for j = 1:w8
        Vq = yq((i-1)*8+1:i*8,(j-1)*8+1:j*8,3); 
        Vdq = Vq.*Q_C;
        Xdq = idct(idct(Vdq')');
        Xf = Xdq + 128;
        img((i-1)*8+1:i*8,(j-1)*8+1:j*8,3) = Xf;
     end
end

