 function [ VSE, LabComputed, LabValidation, XYZn, Results] = LxyComputedAnalysis(LxyComputed,RGBComputed, RGBValidation, ValidationXYZ, WhiteReference)
%function [ VSE, LabComputed, LabValidation, XYZn, Results] = LxyComputedAnalysis(LxyMeasuredDTRXYZ,RGBDTR, ValidationRGB, ValidationXYZ, WhiteReference)

% RGBs computed and the ValidationRGB need to be in unit8.
% ValidationXYZ needs to be normalized.

%function [VSE, XYZn, LabComputed, Results, RGB ] = LxyComputedAnalysis(LxyComputed, LabValidation, WhiteReference, RGBValidation)

     VSE = [];


% First we need to transfer the data in Lxy to XYZ

     XYZ = Lxy2XYZ(LxyComputed);

% Seconed we need to normalize the XYZ values using the white display measurements

     XYZn = 100 .*(XYZ ./ WhiteReference(1,2));


% Normalize the WhiteReference to use for the converting the XYZ to Lab
% The white reference is specifed for each display and can be found in
% Rampas last row.

     WhiteReferenceN = 100 .*(WhiteReference./WhiteReference(1,2));

% Third we need to convert the XYZn to L*a*b to preform Visual error(VSE) 

% For the normalized Validation XYZ and the normalized of computed XYZ by
% the models.

     LabValidation = xyz2lab(ValidationXYZ,"WhitePoint",WhiteReferenceN);

     LabComputed = xyz2lab(XYZn,"WhitePoint",WhiteReferenceN); 


% Forth we need to compute the VSE

     %Loop for Visual Error

     N = length(LabComputed);


     for i = 1:N
        VSE(end+1) = deltaE00(LabComputed(i,:), LabValidation(i,:)); 
     end


     VSE = VSE';


     MSE_RGB = immse(RGBComputed,RGBValidation); % Computing the mean square error
     MDeltaE00 = mean(VSE); % Computing the average of visual error (VSE).
     SD = std(VSE);  % Computing the standard deviation.


     count_X_less_than_one = sum( VSE <= 1);
     count_X_between_one_and_two = sum( VSE < 2 & VSE >= 1);
     count_X_bigger_than_two = sum( VSE > 2 );



% Finally orgnize the data for the latex table

     Results = [' & $' sprintf('%1.2f',MSE_RGB) '$ & $' sprintf('%2.2f',MDeltaE00) ...
         '$ & $' sprintf('%1.2f',SD) '$ & $' sprintf('%1.2f',count_X_less_than_one) ...
         '$ & $' sprintf('%1.2f',count_X_between_one_and_two) '$ & $' ...
         sprintf('%1.2f',count_X_bigger_than_two) '$' ];

% Plotting the results

ComputedxyY = XYZ2xyY(XYZn);

x = ComputedxyY(:,1);
y = ComputedxyY(:,2);
Y = ComputedxyY(:,3);

count_X_bigger_than_five = sum( VSE > 5 )

% Plot DeltaE00 vs Y.
figure; hold on

C = RGBValidation(:,1:3)/255;
S = 4*ones(size(C(:,1)));
axis([0 100 0 5]);
set(gca,'linewidth',0.75)
scatter(Y,VSE,S,C,"filled")
xlabel('Y','FontSize',12,'FontWeight','bold')
ylabel('Visual error','FontSize',12,'FontWeight','bold')
exportgraphics(gca, 'DeltaE00_vs_Y.png', 'Resolution',1200)

%Plot DeltaE00 vs xy.
figure;


scatter3(x,y,VSE,S,C,"filled")
view(40, 35);
set(gca,'linewidth',0.75)
xlabel('x','FontSize',12,'FontWeight','bold')
ylabel('y','FontSize',12,'FontWeight','bold')
zlabel('Visual error','FontSize',12,'FontWeight','bold')
xlim([0, 0.75]);
ylim([0, 0.75]);
zlim([0, 5]);
exportgraphics(gca, 'DeltaE00_vs_xy.png', 'Resolution',1200)


% Plot histogram for the visual error.
 %figure;

 %NumBins = 1000;
 %histogram(VSE,NumBins,"FaceColor","b")
 %xlim([0, 3]);
 %ylim([0, 150]);
 %exportgraphics(gca, 'HistogramDeltaE00.png', 'Resolution',1200)


end