x = imread('project5original.jpg');
xd = double(x);
[m,n,a]=size(x);

RED=xd(:,:,1);
GREEN=xd(:,:,2);
BLUE=xd(:,:,3);
picture1 = xd(:,:,1:3);
figure(1); imagesc(x, [0,255])

%p = 1;
%p = 2;
%p = 4;
p = 100;
Q = p*8./hilb(8);
xbred1 = RED(1:m,1:n);
xbgreen1 = GREEN(1:m,1:n);
xbblue1 = BLUE(1:m,1:n);
picture = zeros(m,n,3);
for i = 1:8:m
    for j = 1:8:n
       
       xbred = xbred1(i:i+7,j:j+7);
       xbgreen = xbgreen1(i:i+7,j:j+7);
       xbblue = xbblue1(i:i+7,j:j+7);
       
       %For red
       xcred = xbred - 128;
       yred = dct(dct(xcred')');
       yqred=round(yred./Q);
       ydqred = yqred.*Q;
       xdqred = idct(idct(ydqred')');
       xered = xdqred + 128;
       %For green
       xcgreen = xbgreen - 128;
       ygreen = dct(dct(xcgreen')');
       yqgreen=round(ygreen./Q);
       ydqgreen = yqgreen.*Q;
       xdqgreen = idct(idct(ydqgreen')');
       xegreen = xdqgreen + 128;
       %For blue
       xcblue = xbblue - 128;
       yblue = dct(dct(xcblue')');
       yqblue=round(yblue./Q);
       ydqblue = yqblue.*Q;
       xdqblue = idct(idct(ydqblue')');
       xeblue = xdqblue + 128;
       %adds to compressed picture
       picture(i:i+7,j:j+7,1) = xered;
       picture(i:i+7,j:j+7,2) = xegreen;
       picture(i:i+7,j:j+7,3) = xeblue;
    end
end
figure(2);imagesc(uint8(picture), [0,255])