% =========================================================================
% LRTR for hyperspectral image deblurring, Version 1.0
% Copyright(c) 2017 Yi Chang. All Rights Reserved.
%
% ----------------------------------------------------------------------
% Permission to use, copy, or modify this software and its documentation
% for educational and research purposes only and without fee is here
% granted, provided that this copyright notice and the original authors'
% names appear on all copies and supporting documentation. This program
% shall not be used, rewritten, or adapted as the basis of a commercial
% software or hardware product without first obtaining permission of the
% authors. The authors make no representations about the suitability of
% this software for any purpose. It is provided "as is" without express
% or implied warranty.
%----------------------------------------------------------------------
%
% This is an implementation of the algorithm for hyperspectral image deconvolution
% 
% Please cite the following paper if you use this code:
%
% Yi Chang, Luxin Yan, and Sheng Zhong,"Hyperspectral Image Restoration: Where Does the
% Low-Rank Property Exist", submitted to TIP
% 
%--------------------------------------------------------------------------
% clc;
clear all;
close all;

addpath(genpath('Utilize\'));
kernel_type     =    'Uniform_blur';
Dir             =    'Data\Data_SR';
pre             =    'OLRT';
Result_dir      =    'Results\CAVE_UniformDeblur25\';
Out_dir         =    fullfile(Result_dir, sprintf('KernelSize_%d',25));
fn_txt          =    strcat( pre, 'PSNR_SSIM.txt' ); 
fd_txt          =    fopen( fullfile(Out_dir, fn_txt), 'wt');

Test_file       =    {'balloons_ms','beads_ms','cd_ms','chart_and_stuffed_toy_ms','clay_ms','cloth_ms','egyptian_statue_ms','face_ms','fake_and_real_beers_ms'...
    ,'fake_and_real_food_ms','fake_and_real_lemon_slices_ms','fake_and_real_lemons_ms','fake_and_real_peppers_ms','fake_and_real_strawberries_ms','fake_and_real_sushi_ms','fake_and_real_tomatoes_ms'...
    ,'feathers_ms','flowers_ms','glass_tiles_ms','hairs_ms','jelly_beans_ms','oil_painting_ms','paints_ms','photo_and_face_ms','pompoms_ms','real_and_fake_apples_ms','real_and_fake_peppers_ms'...
    ,'sponges_ms','stuffed_toys_ms','superballs_ms','thread_spools_ms','watercolors_ms'};
for i = 1:32

Z_ori           =    load_HSI( Dir, Test_file{i} );
rand('seed',0);
nlevel          =    0/256;
wrapSize        =    6;
for band = 1:size(Z_ori,3)
O_Img(:,:,band) =    wrap_boundary_liu( Z_ori(:,:,band), opt_fft_size( [size(Z_ori,1), size(Z_ori,2)] + wrapSize ) );   % bounary extension
end
par             =    ParamDeblur_setting( nlevel,O_Img );
Y_Img           =    par.B(O_Img);
par.lambda = 0.00000001;
par.alpha  = 0.0000001;
E_Img           =    LRTR_Deblur_Main(O_Img, Y_Img, par);
HSI_res         =    E_Img(1:size(Z_ori,1),1:size(Z_ori,2),:);
    
    Dir_out         =    fullfile( Out_dir, Test_file{i} );
    if exist(Dir_out,'dir')==0
        mkdir( Dir_out );
    end

    Temp1           =    Z_ori;
    Temp2           =    HSI_res;
    PSNR            =    csnr(256*Temp1,256*Temp2,0,0);
    SSIM            =    cal_ssim( 256*Temp1, 256*Temp2, 0, 0 );
    ERGAS           =    ErrRelGlobAdimSyn(256*Temp1, 256*Temp2);
    SAM             =    SpectAngMapper(256*Temp1, 256*Temp2);

    disp( sprintf('Scaling factor = %d,  %s,  PSNR = %2.3f, SSIM = %2.4f, ERGAS = %2.4f, SAM = %2.4f \n', 17, Test_file{i}, PSNR, SSIM, ERGAS, SAM));
    Save_HSI( reshape(permute(HSI_res,[3,1,2]),31,512*512), [512,512], Dir_out, strcat(pre,Test_file{i}) );
    fprintf(fd_txt, '%s : PSNR_LRTR = %2.2f  SSIM_LRTR = %2.4f SAM_LRTR = %f  ERGAS_LRTR = %f \n', Test_file{i}, PSNR, SSIM, SAM, ERGAS); 
end
fclose(fd_txt);

