% Copyright (c) 2017 Indian Institute of Science, Bangalore, India.
% All rights reserved.
% 
% Permission to use, copy, or modify this software and its documentation
% for educational and research purposes only and without fee is
% hereby 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.
% 
% The following papers are to be cited in the bibliography whenever
% the software is used as:
% Hisham Rahman, Rajiv Soundararajan, and R. Venkatesh Babu,
% "Evaluating Multiexposure Fusion Using Image Information",
% Signal Processing Letters, in print, DOI: 10.1109/LSP.2017.2752233.
% 
% The attached code computes a single quality score given a test image
% and set of over/under-exposed reference images.
% 
% For questions and/or comments please email
% Hisham Rahman (hishamrahman@gmail.com).

% For given refImgs in HxWxN in [0,255] double-precision
% Return the spatial weights for each location in 5-level
% Steerable pyramid representation of quality map.

function [wght_array, imgRefPyr] = compute_weights_StPyr(imgRef)
    % Convert the distorted images into Steerable pyramid of 5-levels.
    [H, W, N] = size(imgRef);
    % Choose only horizontal and vertical subbands at each scale.
    index = [4,7,10,13,16,19,22,25,28,31];
    imgRefPyr = cell(length(index),1);
    for j=1:N
        currImg = imgRef(:,:,j);
        [pyr, pind] = buildSpyr(currImg, length(index)/2, 'sp5Filters', 'reflect1');
        currTree = ind2wtree(pyr, pind);
        for idx=1:length(index)
            if(j==1) %Create arrays for 1st image
                [H1, W1] = size(currTree{index(idx)});
                imgRefPyr{idx} = zeros(H1, W1, N);
            end
            imgRefPyr{idx}(:,:,j) = currTree{index(idx)};
        end
    end
    
    %Create the wght_array
    wght_array = cell(length(index),1);    
    for lev = 1:length(index)
        [H1, W1] = size(imgRefPyr{lev}(:,:,1));
        wght_array{lev} = zeros(H1, W1);
        for j=1:N
            wght_array{lev} = max(wght_array{lev},abs(imgRefPyr{lev}(:,:,j)));
        end
        % Strip edges to match SSIM index dimension.
        wght_array{lev} = wght_array{lev}(6:end-5,6:end-5);
    end
end
