% Script for 11.2 computer problem 5
% relevant picture files:
%   p5base.png
%   p5quant1.png
%   p5quant2.png
%   p5quant4.png

image = imread('codyPic.jpg');
imgray = double(image);

x=1900;
y=482;

im8x8 = imgray(x:(x+7),y:(y+7),:);
C=dctMat(8);
Q=8./hilb(8);
figure;imagesc(image(x:(x+7),y:(y+7),:));colormap default; % p5base.png

im8x8dct(:,:,1)=twoDimDCT(im8x8(:,:,1),C);
im8x8dct(:,:,2)=twoDimDCT(im8x8(:,:,2),C);
im8x8dct(:,:,3)=twoDimDCT(im8x8(:,:,3),C);

im8x8q1(:,:,1)=round(im8x8dct(:,:,1)./Q);
im8x8q1(:,:,2)=round(im8x8dct(:,:,2)./Q);
im8x8q1(:,:,3)=round(im8x8dct(:,:,3)./Q);
im8x8q1

im8x8r1(:,:,1) = invertTwoDimDCT(im8x8q1(:,:,1).*Q,C);
im8x8r1(:,:,2) = invertTwoDimDCT(im8x8q1(:,:,2).*Q,C);
im8x8r1(:,:,3) = invertTwoDimDCT(im8x8q1(:,:,3).*Q,C);

im8x8ru1=uint8(im8x8r1);
figure;colormap default;imagesc(im8x8ru1); % p5quant1.png

Q=Q.*2;

im8x8q2(:,:,1)=round(im8x8dct(:,:,1)./Q);
im8x8q2(:,:,2)=round(im8x8dct(:,:,2)./Q);
im8x8q2(:,:,3)=round(im8x8dct(:,:,3)./Q);
im8x8q2

im8x8r2(:,:,1) = invertTwoDimDCT(im8x8q2(:,:,1).*Q,C);
im8x8r2(:,:,2) = invertTwoDimDCT(im8x8q2(:,:,2).*Q,C);
im8x8r2(:,:,3) = invertTwoDimDCT(im8x8q2(:,:,3).*Q,C);

im8x8ru2=uint8(im8x8r2);
figure;colormap default;imagesc(im8x8ru2); % p5quant2.png

Q=Q.*2;

im8x8q4(:,:,1)=round(im8x8dct(:,:,1)./Q);
im8x8q4(:,:,2)=round(im8x8dct(:,:,2)./Q);
im8x8q4(:,:,3)=round(im8x8dct(:,:,3)./Q);
im8x8q4

im8x8r4(:,:,1) = invertTwoDimDCT(im8x8q4(:,:,1).*Q,C);
im8x8r4(:,:,2) = invertTwoDimDCT(im8x8q4(:,:,2).*Q,C);
im8x8r4(:,:,3) = invertTwoDimDCT(im8x8q4(:,:,3).*Q,C);

im8x8ru4=uint8(im8x8r4);
figure;colormap default;imagesc(im8x8ru4); % p5quant4.png