% Image Compression code

% getting the original image
C8 = dct(eye(8));
Qy = [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];
Xbig = imread('flower_pic.jpg');
Xbig = double(Xbig);
[length width rgb] = size(Xbig);

%define rgb components separately
r = Xbig(:,:,1);
g = Xbig(:,:,2);
b = Xbig(:,:,3);

%change to grayscale
xgray=0.2126*r+0.7152*g+0.0722*b;

%cropping image
X_crop = xgray(2:257,100:355);
image = X_crop;
new_quantized_image = zeros(256,256);

p = 2;
Q = p*8./hilb(8);
Qy = p*Qy;
%quantization steps
for i = 1:8:249
    for j = 1:8:249
        new_image = image(i:i+7,j:j+7);
        
        % quantized 8x8 image
        quantized_image = Quantization(new_image,C8,Qy);
        new_quantized_image(i:i+7,j:j+7) = quantized_image;

    end
end
%original gray image
figure(1)
image = uint8(image);
imagesc(image); colormap(gray)

%quantized gray image
figure(2)
new_quantized_image = uint8(new_quantized_image);
imagesc(new_quantized_image); colormap(gray)

% original 8x8 chunk
figure(3)
crop_image = double(image);
crop_image_chunk = crop_image(200:207,150:157);
crop_image_chunk = uint8(crop_image_chunk);
imagesc(crop_image_chunk); colormap(gray)

%quantized 8x8 chunk
figure(4)
quant_image = double(new_quantized_image);
quant_image_chunk = quant_image(200:207,150:157);
quant_image_chunk = uint8(quant_image_chunk);
imagesc(quant_image_chunk); colormap(gray)

% original gray with 8x8 chunk taken out
figure(5)
block_image = double(image);
block_image(200:207,150:157) = 0;
white_block = uint8(block_image);
imagesc(white_block); colormap(gray)

    