% dct_compress.m
% Setting some DCT coefs to zero to achieve lossy compression
% David Pan, for EE 604, UAH

clear all; close all;

I_org = imread('coins.png');  % 246 * 300
imshow(I_org)
I = double(I_org);
J = dct2(I);
figure; imagesc(log(1+abs(J))); colorbar

K = zeros(size(I));   
M = 120; N = 140;         % Number of DCT coefs to keep
K(1:M,1:N) = J(1:M, 1:N);
R = idct2(K);
figure; imshow(uint8(R))

% Measure distortion of the reconstructed image
psnr(I_org, uint8(R))

% Fraction of energy preserved
norm(K,'fro')^2/norm(J,'fro')^2

% Compression Ratio
numel(K)/(M*N)



