% Script for 11.2 computer problem 4
% relevant picture files:
%   p4base.png
%   p4quant1.png
%   p4quant2.png
%   p4quant4.png

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

im8x8 = imgray(96:(96+7),1:8);
C=dctMat(8);
Q=[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];
figure;imagesc(im8x8);colormap(gray); % p4base.png

im8x8dct=twoDimDCT(im8x8,C);
im8x8q1=round(im8x8dct./Q);
im8x8q1

im8x8r1 = invertTwoDimDCT(im8x8q1.*Q,C);
im8x8ru1=uint8(im8x8r1);
figure;colormap(gray);imagesc(im8x8ru1); % p4quant1.png
Q=Q.*2;
im8x8q2=round(im8x8dct./Q);
im8x8q2

im8x8r2 = invertTwoDimDCT(im8x8q2.*Q,C);
im8x8ru2=uint8(im8x8r2);
figure;colormap(gray);imagesc(im8x8ru2); % p4quant2.png

Q=Q.*2;
im8x8q4=round(im8x8dct./Q);
im8x8q4

im8x8r4 = invertTwoDimDCT(im8x8q4.*Q,C);
im8x8ru4=uint8(im8x8r4);
figure;colormap(gray);imagesc(im8x8ru4); % p4quant4.png