% Multi-scale Structural Similarity Index (MS-SSIM)
% Z. Wang, E. P. Simoncelli and A. C. Bovik, "Multi-scale structural similarity
% for image quality assessment," Invited Paper, IEEE Asilomar Conference on
% Signals, Systems and Computers, Nov. 2003

% MS-SSIM qmap for 5 level steerable pyramid.
% Only contrast and structure terms for subbands 4,7,10,13,16,19,22,25,28,31.

function [cs_map_array] = msssim_qmap_StPyr(img1, img2)
    % SSIM constants
    K = [0.01 0.03];
    C2 = (K(2)*255)^2;
    win = fspecial('gaussian', 11, 1.5);
    win = win/sum(win(:));
    
    % Other constants
    index = [4,7,10,13,16,19,22,25,28,31];
    
    %convert img1 to StPyr.
    [pyr, pind] = buildSpyr(img1, length(index)/2, 'sp5Filters', 'reflect1');
    img1Tree = ind2wtree(pyr,pind);
    %convert img2 to StPyr
    [pyr, pind] = buildSpyr(img2, length(index)/2, 'sp5Filters', 'reflect1');
    img2Tree = ind2wtree(pyr,pind);
    
    cs_map_array = cell(length(index),1);
    %for each Hor and Ver subband compute the contrast and saturation
    for idx = 1:length(index)
        img1_sb = img1Tree{index(idx)};
        img2_sb = img2Tree{index(idx)};
        
        sigma1_sq = filter2(win, img1_sb.*img1_sb, 'valid');
        sigma2_sq = filter2(win, img2_sb.*img2_sb, 'valid');
        sigma12 = filter2(win, img1_sb.*img2_sb, 'valid');
        cs_map_array{idx} = (2*sigma12 + C2)./(sigma1_sq + sigma2_sq + C2);
    end 
end
