

%%

% Generate images

clear;

%-------
delete('fcc*.j*');
%delete('fcc*.mat');
%-------

%inFile = 'cameraman.tif';
inFile='7.png';
folder = fileparts(which(inFile));
A = imread(fullfile(folder,inFile));
[Height,Width,Depth] = size(A);
if Depth > 1
    A = rgb2gray(A);
end

dwt_levels=8;
show_files_at_size_kB=20;

N1=8;
N2=8;

%V1=linspace(1,80,N1);
V1=[1 5 10 17 25 35 52 60];
V2=[25 40 60 100 300 1000 2000 4000];



ijpg=1;
for Q_JPEG=V1,
    fprintf('JPEG coding...\n');
    
    fname=sprintf('fcc_jpeg_%d.jpg',ijpg);
    
    tic;
    imwrite(A,fname,'Quality',Q_JPEG);
    t_jpeg_coding(ijpg)=toc;
    fprintf('JPEG decoding...\n');
    tic;
    A_jpg=imread(fname);
    t_jpeg_decoding(ijpg)=toc;
    
    fprintf('Quality assesment\n');
    [mssim, ssim_map] = ssim(A, A_jpg);
    ssim_jpg(ijpg)=mssim;
    
    [psnr,mse]=PSNR(A,A_jpg,255);
    psnr_jpg(ijpg)=psnr;
    
    f=dir(fname);
    size_jpg(ijpg)=f.bytes;
    
    ijpg=ijpg+1;
end



ijpg2000=1;
for CR_JPEG2000=V2,
    fprintf('JPEG2000 coding...\n');
    
    fname=sprintf('fcc_jpeg_2000_%d.jp2',ijpg2000);
    
    tic;
    imwrite(A,fname,...
        'ReductionLevels',dwt_levels, 'CompressionRatio',CR_JPEG2000);
    t_jpeg2000_coding(ijpg2000)=toc;
    tic
    A_jpg2000=imread(fname);
    t_jpeg2000_decoding(ijpg2000)=toc;
    
    fprintf('Quality assesment\n');
    [mssim, ssim_map] = ssim(A, A_jpg2000);
    ssim_jpg2000(ijpg2000)=mssim;
    
    [psnr,mse]=PSNR(A,A_jpg2000,255);
    psnr_jpg2000(ijpg2000)=psnr;
    
    f=dir(fname);
    size_jpg2000(ijpg2000)=f.bytes;
    
    
    ijpg2000=ijpg2000+1;
end

%%
% generating file list

f1=dir('fcc_jpeg*.jp*');
nf=length(f1);
pairs=[];
for i=0:nf,
    for j=0:nf,
        if j>i
            pairs=[pairs; i j];
        end
    end
end

np=size(pairs,1);
ri=randperm(np);
scores=zeros(nf+1,1);
%%

%starting assesment
fh=figure('units','normalized','outerposition',[0 0 1 1]);
for ic=1:np,
    cur_ri=ri(ic);
    
    figure(fh);
    ip1=pairs(cur_ri,1);
    ip2=pairs(cur_ri,2);
    subplot(1,2,1),
    if not(ip1)
        imshow(A);
    else
        imshow(imread(f1(ip1).name));
    end
    %truesize(size(A));
    

    subplot(1,2,2),
    if not(ip2)
        imshow(A);
    else
        imshow(imread(f1(ip2).name));
    end
    %truesize(size(A));
    
    choice = questdlg('Melyik kep magasabb minosegu ?', ...
        'Valassz', ...
        'Bal','Jobb','Bal');
    switch choice
        case 'Bal'
            scores(ip1+1)=scores(ip1+1)+1;
        case 'Jobb'
            scores(ip2+1)=scores(ip2+1)+1;
    end
    
end


fprintf('ready\n');


%%
% make results
for iif=1:nf,
    [path,name,ext]=fileparts(f1(iif).name);
    if strcmp(ext,'.jpg')
        ijpg=sscanf(name,'fcc_jpeg_%d');
        jpeg_data(ijpg,:)=[f1(iif).bytes ssim_jpg(ijpg) psnr_jpg(ijpg) scores(iif+1)];
    elseif  strcmp(ext,'.jp2')
        ijpg=sscanf(name,'fcc_jpeg_2000_%d');
        jpeg2000_data(ijpg,:)=[f1(iif).bytes ssim_jpg2000(ijpg) psnr_jpg2000(ijpg) scores(iif+1)];
    end
    
end
rng('shuffle');
save(sprintf('fcc_data_%6d.mat',round(rand*1E5)));

%%
figure(1),
subplot(2,2,1),
plot(jpeg_data(:,2),jpeg_data(:,3),'*:'),xlabel('SSIM'),ylabel('PSNR');title('JPEG');
subplot(2,2,2),
plot(jpeg_data(:,2),jpeg_data(:,4),'*:'),xlabel('SSIM'),ylabel('Scores');title('JPEG');
subplot(2,2,3),
plot(jpeg_data(:,3),jpeg_data(:,4),'*:'),xlabel('PSNR'),ylabel('Scores');title('JPEG');


figure(2),
subplot(2,2,1),
plot(jpeg2000_data(:,2),jpeg2000_data(:,3),'*:'),xlabel('SSIM'),ylabel('PSNR');title('JPEG2000');
subplot(2,2,2),
plot(jpeg2000_data(:,2),jpeg2000_data(:,4),'*:'),xlabel('SSIM'),ylabel('Scores');title('JPEG2000');
subplot(2,2,3),
plot(jpeg2000_data(:,3),jpeg2000_data(:,4),'*:'),xlabel('PSNR'),ylabel('Scores');title('JPEG2000');

figure(3),
plot(jpeg_data(:,1),jpeg_data(:,4),'r'); hold on;
plot(jpeg2000_data(:,1),jpeg2000_data(:,4),'b'); xlabel('File size'),ylabel('Score'),legend('JPEG','JPEG2000');





