% Andrew Puryear
% 362 Project - projectfigures
%   Matlab file used to generate many of the figures presented in the text.

wind=ones(8);
toplot=matrixexpand([zeros(4,16);[zeros(8,4),wind,zeros(8,4)];zeros(4,16)],8);
n=[-8+1/16:1/8:8-1/16]*2;
mesh(n,n,toplot); axis tight;
title('Figure 2 - 8-by-8 Image Window');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear;
load F:\MATLAB6p1\work\PSYCH221\Project\TestPictures\einstein.mat;
im = round(X/max(X(:))*256);
Q_factor = 3;
q1 = makeQTable(Q_factor);
imjpeg2 = jpegCompress(im, 1./q1);
imjpeg2 = truncate(imjpeg2, 0, 255);
imshow(imjpeg2, gray(256));
title('Figure 3 - JPEG Image with Compression of 9.14');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% create COLA window
wind=[ones(1,10)*.25;
.25, .25, ones(1,6)*3/4, .25, .25;
.25, .75, ones(1,6), .75, .25;
.25, .75, ones(1,6), .75, .25;
.25, .75, ones(1,6), .75, .25;
.25, .75, ones(1,6), .75, .25;
.25, .75, ones(1,6), .75, .25;
.25, .75, ones(1,6), .75, .25;
.25, .25, ones(1,6)*3/4, .25, .25;
ones(1,10)*.25];

% plot COLA window
toplot=matrixexpand([zeros(1,12);[zeros(10,1),wind,zeros(10,1)];zeros(1,12)],8);
n=[-5+5/length(toplot(1,:)):10/length(toplot(1,:)):5-5/length(toplot(1,:))];
mesh(n,n,toplot);
title('Figure 4 - COLA-JPEG 10-by-10 Image Window');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear;
close all;

load F:\MATLAB6p1\work\PSYCH221\Project\TestPictures\einstein.mat;
load F:\MATLAB6p1\work\PSYCH221\tutorials\jpegFiles\dctMatrix;
im = round(X/max(X(:))*256);
% show origninal image
imshow(im/256,256);
title('JPEG Image with no Lossy Compression');
figure;imshow(im/256,256);
title('COLA-JPEG Image with no Lossy Compression');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear;
close all;
load F:\MATLAB6p1\work\PSYCH221\Project\TestPictures\einstein.mat;
load F:\MATLAB6p1\work\PSYCH221\tutorials\jpegFiles\dctMatrix;
[imw, imsize]=compress_jpega(X, 1.8);%1.8
ima=decompress_jpega(imw, imsize);
figure;imshow(ima/256,256)
title('COLA-JPEG Image with Lossy Compression Ratio of ~9');

clear;
load F:\MATLAB6p1\work\PSYCH221\Project\TestPictures\einstein.mat;
im = round(X/max(X(:))*256);
Q_factor = 3;
q1 = makeQTable(Q_factor);
imjpeg2 = jpegCompress(im, 1./q1);
imjpeg2 = truncate(imjpeg2, 0, 255);
imshow(imjpeg2, gray(256));
title('JPEG Image with Lossy Compression Ratio of ~9');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear;
close all;
load F:\MATLAB6p1\work\PSYCH221\Project\TestPictures\einstein.mat;
load F:\MATLAB6p1\work\PSYCH221\tutorials\jpegFiles\dctMatrix;
[imw, imsize]=compress_jpega(X, 25);
ima=decompress_jpega(imw, imsize);
figure;imshow(ima/256,256)
title('COLA-JPEG Image with Lossy Compression Ratio of ~3');

clear;
figure;
load F:\MATLAB6p1\work\PSYCH221\Project\TestPictures\einstein.mat;
im = round(X/max(X(:))*256);
Q_factor = 50;
q1 = makeQTable(Q_factor);
imjpeg2 = jpegCompress(im, 1./q1);
imjpeg2 = truncate(imjpeg2, 0, 255);
imshow(imjpeg2, gray(256));
title('JPEG Image with Lossy Compression Ratio of ~3');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

bs=zeros(256,4);
ws=ones(256,4);
im=[];
for i=1:128/size(bs,2)
    im=[im,bs,ws];
end

im = round(im/max(im(:))*256);
% show origninal image
imshow(im/256,256);
title('JPEG Image with no Lossy Compression');
figure;imshow(im/256,256);
title('COLA-JPEG Image with no Lossy Compression');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear;
close all;
bs=zeros(256,4);
ws=ones(256,4);
im=[];
for i=1:128/size(bs,2)
    im=[im,bs,ws];
end

im = round(im/max(im(:))*256);
[imw, imsize]=compress_jpega(im, 1.8);%1.8
ima=decompress_jpega(imw, imsize);
figure;imshow(ima/256,256)
title('COLA-JPEG Image with Lossy Compression Ratio of ~9');

clear;
bs=zeros(256,4);
ws=ones(256,4);
im=[];
for i=1:128/size(bs,2)
    im=[im,bs,ws];
end
im = round(im/max(im(:))*256);
Q_factor = 3;
q1 = makeQTable(Q_factor);
imjpeg2 = jpegCompress(im, 1./q1);
imjpeg2 = truncate(imjpeg2, 0, 255);
figure;
imshow(imjpeg2, gray(256));
title('JPEG Image with Lossy Compression Ratio of ~9');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear;
close all;
bs=zeros(256,4);
ws=ones(256,4);
im=[];
for i=1:128/size(bs,2)
    im=[im,bs,ws];
end

im = round(im/max(im(:))*256);
[imw, imsize]=compress_jpega(im, 25);%1.8
ima=decompress_jpega(imw, imsize);
figure;imshow(ima/256,256)
title('COLA-JPEG Image with Lossy Compression Ratio of ~3');

clear;
bs=zeros(256,4);
ws=ones(256,4);
im=[];
for i=1:128/size(bs,2)
    im=[im,bs,ws];
end
im = round(im/max(im(:))*256);
Q_factor = 50;
q1 = makeQTable(Q_factor);
imjpeg2 = jpegCompress(im, 1./q1);
imjpeg2 = truncate(imjpeg2, 0, 255);
figure;
imshow(imjpeg2, gray(256));
title('JPEG Image with Lossy Compression Ratio of ~3');