function [MSE_R, MSE_G, MSE_B, MSE, PSNR,bpp] = compress()

disp('Inserisci il nome dell''immagine:');
fname=input(' ','s');
a=imread(fname);
[M,N,c]=size(a);
a=double(a);
dim=size(fname(:));
fnamenew=fname(1:dim(1)-4);

for i=1:100
    str=num2str(i);
    imwrite(uint8(a),[fnamenew str 'comp.jpg' ], 'JPEG','Quality',i);
    b=imread([fnamenew str 'comp.jpg' ]);
    
    info_file=dir([fnamenew str 'comp.jpg' ]);
    n_bit=info_file.bytes*8;
    bpp(i)=n_bit/(M*N*c);
    
    b=double(b);
    SE_R=(a(:,:,1)-b(:,:,1)).^2;
    MSE_R(i)=mean(SE_R(:));
    SE_G=(a(:,:,2)-b(:,:,2)).^2;
    MSE_G(i)=mean(SE_G(:));
    SE_B=(a(:,:,3)-b(:,:,3)).^2;
    MSE_B(i)=mean(SE_B(:));
    MSE(i)=mean([MSE_R(i),MSE_G(i),MSE_B(i)]);
    PSNR(i)= 10*log10((255^2)/MSE(i));
end
figure; plot(PSNR);
figure; plot([1:100], MSE_R,'r', [1:100], MSE_G,'g',  [1:100], MSE_B, 'b', [1:100], MSE, '--k');
figure; plot(bpp, MSE);
end

%[MSE_R, MSE_G, MSE_B, MSE, PSNR,bpp] = compress()
