function [ I_colorvis ] = hdrvis( I_hdr )
%UNTITLED Summary of this function goes here
%   Detailed explanation goes here

bias = 0.0;
I_lum = I_hdr(:,:,1) * 0.2126 + I_hdr(:,:,2) * 0.7152 + I_hdr(:,:,3) * 0.0722 + bias;

I_lum1D = reshape(I_lum, 1, size(I_lum, 1) * size(I_lum, 2));

Lmin = min(I_lum1D)
Lmax = max(I_lum1D)

bins = logspace(log10(Lmin), log10(Lmax), 100)

I_hist = hist(I_lum1D, bins);
%I_hist = hist(log10(I_lum1D), 100);
figure;
plot(I_hist);


DR = log10(Lmax) - log10(Lmin)

Lmin5 = prctile(I_lum1D, 5);
Lmax5 = prctile(I_lum1D, 95);
Lmin1 = prctile(I_lum1D, 1);
Lmax1 = prctile(I_lum1D, 99);

DR1 = log10(Lmax1) - log10(Lmin1)

Llogavg = exp(mean(log(I_lum1D)))

key = 0.18;
I_lumscaled = (key / Llogavg) * I_lum(:,:);
size(I_lumscaled)

I_lumclamped5 = I_lum;
I_lumclamped5(I_lum > Lmax5) = Lmax5;
I_lumclamped5(I_lum < Lmin5) = Lmin5;

I_lumclamped1 = I_lum;
I_lumclamped1(I_lum > Lmax1) = Lmax1;
I_lumclamped1(I_lum < Lmin1) = Lmin1;

%Lminlog = log(min(I_lum1D))
Lminlog = log(Lmin1)
Lmaxlog = log(max(I_lum1D))

I_hsvperc5 = zeros(size(I_hdr));
I_hsvperc1 = zeros(size(I_hdr));
I_hsvlog = zeros(size(I_hdr));
I_hsvtm = zeros(size(I_hdr));

I_hsvperc5(:,:,1) = (240.0 - 240.0*((I_lumclamped5(:,:) - Lmin5) / (Lmax5 - Lmin5))) / 360.0;
I_hsvperc1(:,:,1) = (240.0 - 240.0*((I_lumclamped1(:,:) - Lmin1) / (Lmax1 - Lmin1))) / 360.0;
I_hsvlog(:,:,1) = (240.0 - 240.0*((log(I_lum(:,:)) - Lminlog) / (Lmaxlog - Lminlog))) / 360.0;
I_hsvtm(:,:,1) = (240.0 - 240.0*(I_lumscaled(:,:) ./ (1.0 + I_lumscaled(:,:)))) / 360.0;

%Lw = 0.85;
%Lo = I_lumscaled(:,:) .* (1.0 + (I_lumscaled(:,:) ./ (Lw*Lw))) ./ (1.0 + I_lumscaled(:,:));
%Lo(Lo > 1.0) = 1.0;
%I_hsvtm(:,:,1) = (240.0 - 240.0*Lo) / 360.0;

I_hsvperc5(:,:,2) = 1;
I_hsvperc1(:,:,2) = 1;
I_hsvlog(:,:,2) = 1;
I_hsvtm(:,:,2) = 1;

I_hsvperc5(:,:,3) = 1;
I_hsvperc1(:,:,3) = 1;
I_hsvlog(:,:,3) = 1;
I_hsvtm(:,:,3) = 1;

I_rgbperc5 = hsv2rgb(I_hsvperc5);
I_rgbperc1 = hsv2rgb(I_hsvperc1);
I_rgblog = hsv2rgb(I_hsvlog);
I_rgbtm = hsv2rgb(I_hsvtm);

%figure;
%imshow(I_rgbperc5, 'InitialMagnification', 100);
LetterStore = char(97:122);
fp = fopen('names.txt', 'a');
name = LetterStore(ceil(length(LetterStore).*rand(1,3)));
fprintf(fp, 'perc5 = %s\n', name);
%imwrite(I_rgbperc5, strcat(name, '.png'));
imwrite(I_rgbperc5, strcat('perc5', '.png'));


%figure;
%imshow(I_rgbperc1, 'InitialMagnification', 100);
name = LetterStore(ceil(length(LetterStore).*rand(1,3)));
fprintf(fp, 'perc1 = %s\n', name);
%imwrite(I_rgbperc1, strcat(name, '.png'));
imwrite(I_rgbperc1, strcat('perc1', '.png'));

%figure;
%imshow(I_rgblog, 'InitialMagnification', 100);
name = LetterStore(ceil(length(LetterStore).*rand(1,3)));
fprintf(fp, 'log = %s\n', name);
%imwrite(I_rgblog, strcat(name, '.png'));
imwrite(I_rgblog, strcat('log', '.png'));

%figure;
%imshow(I_rgbtm, 'InitialMagnification', 100);
name = LetterStore(ceil(length(LetterStore).*rand(1,3)));
fprintf(fp, 'tm = %s\n\n', name);
%imwrite(I_rgbtm, strcat(name, '.png'));
imwrite(I_rgbtm, strcat('tm', '.png'));
fclose(fp);

I_colorvis = I_rgbtm;

end

