function [] = bifilter() 

% This function is the main function, which imports the different images and creates both an HDR image 
% and a bilateral filtered image. Currently, only Black and White images are outputted.

%string = {'window1.jpg', 'window2.jpg', 'window3.jpg', 'window5.jpg', 'window6.jpg'};
%timer= [1/8, 1/4, 1/2, 1, 2];

string = { 'Capture_00040_lowres.JPG', 'Capture_00038_lowres.JPG', 'Capture_00036_lowres.JPG', 'Capture_00034_lowres.JPG', 'Capture_00031_lowres.JPG',   };
timer = [1/64, 1/16, 1/4, 1, 6];

%Sequence that imports the image and creates an intensity image

Image1 = imread(string{1});
intensity1 = rgb2gray(Image1);
Image2 = imread(string{2});
intensity2 = rgb2gray(Image2);
Image3 = imread(string{3});
intensity3 = rgb2gray(Image3);
Image4 = imread(string{4});
intensity4 = rgb2gray(Image4);
Image5 = imread(string{5});
intensity5 = rgb2gray(Image5);

%Makes images into intensity images of type double

Image1 = im2double(Image1);
intensity1 = im2double(intensity1);
Image2 = im2double(Image2);
intensity2 = im2double(intensity2);
Image3 = im2double(Image3);
intensity3 = im2double(intensity3);
Image4 = im2double(Image4);
intensity4 = im2double(intensity4);
Image5 = im2double(Image5);
intensity5 = im2double(intensity5);

largearray = double(zeros(size(intensity1,1),size(intensity1,2), size(timer,2)));

% Imports all intensity images into a large array
largearray(:,:,1) = intensity1;
largearray(:,:,2) = intensity2;
largearray(:,:,3) = intensity3;
largearray(:,:,4) = intensity4;
largearray(:,:,5) = intensity5;

%Attempt at creating color images
largearrayRcolor = double(zeros(size(intensity1,1),size(intensity1,2), size(timer,2)));
largearrayGcolor = double(zeros(size(intensity1,1),size(intensity1,2), size(timer,2)));
largearrayBcolor = double(zeros(size(intensity1,1),size(intensity1,2), size(timer,2)));

largearrayRcolor(:,:,1) = Image1(:,:,1);
largearrayGcolor(:,:,1) = Image1(:,:,2);
largearrayBcolor(:,:,1) = Image1(:,:,3);
largearrayRcolor(:,:,2) = Image2(:,:,1);
largearrayGcolor(:,:,2) = Image2(:,:,2);
largearrayBcolor(:,:,2) = Image2(:,:,3);
largearrayRcolor(:,:,3) = Image3(:,:,1);
largearrayGcolor(:,:,3) = Image3(:,:,2);
largearrayBcolor(:,:,3) = Image3(:,:,3);
largearrayRcolor(:,:,4) = Image4(:,:,1);
largearrayGcolor(:,:,4) = Image4(:,:,2);
largearrayBcolor(:,:,4) = Image4(:,:,3);
largearrayRcolor(:,:,5) = Image5(:,:,1);
largearrayGcolor(:,:,5) = Image5(:,:,2);
largearrayBcolor(:,:,5) = Image5(:,:,3);

%Creates a sample array to use in calculating g Values;
[result, deltaT] = sample(largearray, timer);
[resultR, deltaTR] = sample(largearrayRcolor, timer);
[resultG, deltaTG] = sample(largearrayGcolor, timer);
[resultB, deltaTB] = sample(largearrayBcolor, timer);

%Transform sample matrix into RGB
result = im2uint8(result);
resultR = im2uint8(resultR);
resultG = im2uint8(resultG);
resultB = im2uint8(resultB);

%Solves g values for all images.

[gSolve, lE] = gsolve(result, deltaT, 255);
[gSolveR, lE] = gsolve(resultR, deltaTR, 255);
[gSolveG, lE] = gsolve(resultG, deltaTG, 255);
[gSolveB, lE] = gsolve(resultB, deltaTB, 255);

largearraytemp = im2uint8(largearray);

largearrayRcolortemp = im2uint8(largearrayRcolor);
largearrayGcolortemp = im2uint8(largearrayGcolor);
largearrayBcolortemp = im2uint8(largearrayBcolor);


output = double(zeros(size(largearray,1),size(largearray,2)));
outputR = double(zeros(size(largearray,1),size(largearray,2)));
outputG = double(zeros(size(largearray,1),size(largearray,2)));
outputB = double(zeros(size(largearray,1),size(largearray,2)));

temp = double(zeros(size(largearray,3),1));
temp2 = double(zeros(size(largearray,3),1));
tempR = double(zeros(size(largearray,3),1));
temp2R = double(zeros(size(largearray,3),1));
tempG = double(zeros(size(largearray,3),1));
temp2G = double(zeros(size(largearray,3),1));
tempB = double(zeros(size(largearray,3),1));
temp2B = double(zeros(size(largearray,3),1));

%Implements Radiance Map function.

for x=1:size(largearray,1)
    for y=1:size(largearray,2)    
        for z=1:size(largearray,3)
            temp =(double(w(largearraytemp(x,y,z))) .* (gSolve((largearraytemp(x,y,z))+1) - log(deltaT)));
            temp2(z,1) = (double(w(largearraytemp(x,y,z))));
            
            tempR =(double(w(largearrayRcolortemp(x,y,z))) .* (gSolve((largearrayRcolortemp(x,y,z))+1) - log(deltaTR)));
            temp2R(z,1) = (double(w(largearrayRcolortemp(x,y,z))));
            
            tempG =(double(w(largearrayGcolortemp(x,y,z))) .* (gSolve((largearrayGcolortemp(x,y,z))+1) - log(deltaTG)));
            temp2G(z,1) = (double(w(largearrayGcolortemp(x,y,z))));
            
            tempB =(double(w(largearrayBcolortemp(x,y,z))) .* (gSolve((largearrayBcolortemp(x,y,z))+1) - log(deltaTB)));
            temp2B(z,1) = (double(w(largearrayBcolortemp(x,y,z))));
            
        end
        
        Eitemp = sum(sum(temp)) ./ sum(sum(temp2));
        
        EitempR = sum(sum(tempR)) ./ sum(sum(temp2R));
        EitempG = sum(sum(tempG)) ./ sum(sum(temp2G));
        EitempB = sum(sum(tempB)) ./ sum(sum(temp2B));
        
        output(x,y) = Eitemp;
        outputR(x,y) = EitempR;
        outputG(x,y) = EitempG;
        outputB(x,y) = EitempB;
    end
end

%output = uint8(output);

%finalimage(:,:,1) = uint8(output);
%finalimage(:,:,2) = uint8(output);
%finalimage(:,:,3) = uint8(output);

outputG = zeros(size(outputR,1), size(outputR,2));

finalimage2(:,:,1) = uint8(outputR);
finalimage2(:,:,2) = uint8(outputG);
finalimage2(:,:,3) = uint8(outputB);
        
base = output;

for i=2:(size(output,1) - 1)
    for j=2:(size(output,2) - 1)
        
        base = bilateral_filter(output, i,j);
        
    end
end

detail = output / base;

base = contrastReduce(base);

newoutput = base* detail;

%final image

finalimage(:,:,1) = uint8(output);
finalimage(:,:,2) = uint8(output);
finalimage(:,:,3) = uint8(output);

imwrite(finalimage, 'bilateralfilteredfinalimage.jpeg');

imshow(finalimage);         