% Description:
%
%       map.m generates the radiance map
%       
% Input:
%
%       Z - pixel values
%       B - array of the log delta t's from the different exposures
%       w - array of the weights for pixels 0 to 255
%       g - response function
%
% Output:
%
%       lE - log of the irradiance values
%

function [lE] = map(Z,B,w,g)

lE = zeros(size(Z,1),1);

for i = 1:1:size(Z,1),
    
    num = 0;
    for j = 1:1:size(Z,2),
        num = num + w(Z(i,j)+1)*(g(Z(i,j)+1)-B(j));
    end
    
    den = 0;
    for j = 1:1:size(Z,2),
        den = den + w(Z(i,j)+1);
    end
    
    if den == 0
        if g(Z(i,1)+1) > g(256/2)
            lE(i) = -inf;
            for j = 1:1:size(Z,2),
                if  lE(i) < (g(Z(i,j)+1)-B(j))
                    lE(i) = g(Z(i,j)+1)-B(j);
                end
            end
        else
            lE(i) = inf;
            for j = 1:1:size(Z,2),
                if  lE(i) > (g(Z(i,j)+1)-B(j))
                    lE(i) = g(Z(i,j)+1)-B(j);
                end
            end
        end
    else
        lE(i) = num/den;
    end
    
end