%
% GrayWorldCompare.m
%
% 1. Compute difference of input and Gray World Algorithm output;
% 2. Compute difference of camera output and Gray World Algorithm output;
%
% Hui Li
% 3/14/2000
%

load colorRGB

% laod and format camera output
load centerYesDifferences
CameraDiff = zeros(21,12,4);
CameraDiff(:,:,1:3) = differences;
CameraDiff(:,:,4) = (CameraDiff(:,:,1) + CameraDiff(:,:,2) + CameraDiff(:,:,3))./3;


% compute GW difference
%backgroundCol = 430:15:730;
%primeCol = 430+5:25:730;

GWDiff = zeros(21,12,4);

i=1;
for primeCol = 430+5:25:730
	j = 1;
	for backgroundCol = 430:15:730
		indexTarget = primeCol-430+1;

		GWoutputfn = strcat('../web/GWoutput/GWoutput',num2str(backgroundCol),'-',num2str(primeCol),'.bmp');

		inputRGB(1) = 255*colorR(1,indexTarget);
		inputRGB(2) = 255*colorG(1,indexTarget);
		inputRGB(3) = 255*colorB(1,indexTarget);

%		if backgroundCol == primeCol
%			im = imread(GWoutputfn,'bmp');
%			im = double(im)./255;
%		else
			[X,map] = imread(GWoutputfn,'bmp');
			
			% Read into RGB and compute mean
			Y = double(X)+1;
			[r,g,b] = ind2rgb(Y,map);
			im(:,:,1) = r;
			im(:,:,2) = g;
			im(:,:,3) = b;
%		end

		
		% Upper Left
		upper = 114;
		lower = 260;
		left = 114;
		right = 260;
		avg1 = mean(mean(double(im(upper:1:lower,left:1:right,:))));

		outputRGB(1) = mean(avg1(1,1,1));
		outputRGB(2) = mean(avg1(1,1,2));
		outputRGB(3) = mean(avg1(1,1,3));
 
		GWDiff(j,i,1:3) = (outputRGB*255+1) - inputRGB;
		
		j=j+1;
	end
	i=i+1;
end


GWDiff(:,:,4) = (GWDiff(:,:,1) + GWDiff(:,:,2) + GWDiff(:,:,3))./3;

CA_GWDiff = GWDiff - CameraDiff;

save('GWDiff.mat','CameraDiff','GWDiff','CA_GWDiff');


% Plot Input/GrayWorld Differences

backgroundCol = 430:15:730;
primeCol = 430+5:25:730;

figure
mesh(backgroundCol,primeCol,GWDiff(:,:,1)');
title('Input/GrayWorld Red Value Differences');
xlabel('Background Wavelength');
ylabel('Target Wavelength');
zlabel('R Value Differences');

figure
mesh(backgroundCol,primeCol,GWDiff(:,:,2)');
title('Input/GrayWorld Green Value Differences');
xlabel('Background Wavelength');
ylabel('Target Wavelength');
zlabel('G Value Differences');

figure
mesh(backgroundCol,primeCol,GWDiff(:,:,3)');
title('Input/GrayWorld Blue Value Differences');
xlabel('Background Wavelength');
ylabel('Target Wavelength');
zlabel('B Value Differences');

figure
mesh(backgroundCol,primeCol,GWDiff(:,:,4)');
title('Input/GrayWorld Gray Value Differences');
xlabel('Background Wavelength');
ylabel('Target Wavelength');
zlabel('Gray Value Differences');


% Plot Camera/GrayWorld Differences
figure
mesh(backgroundCol,primeCol,CA_GWDiff(:,:,1)');
title('Camera/GrayWorld Red Value Differences');
xlabel('Background Wavelength');
ylabel('Target Wavelength');
zlabel('R Value Differences');

figure
mesh(backgroundCol,primeCol,CA_GWDiff(:,:,2)');
title('Camera/GrayWorld Green Value Differences');
xlabel('Background Wavelength');
ylabel('Target Wavelength');
zlabel('G Value Differences');

figure
mesh(backgroundCol,primeCol,CA_GWDiff(:,:,3)');
title('Camera/GrayWorld Blue Value Differences');
xlabel('Background Wavelength');
ylabel('Target Wavelength');
zlabel('B Value Differences');

figure
mesh(backgroundCol,primeCol,CA_GWDiff(:,:,4)');
title('Camera/GrayWorld Gray Value Differences');
xlabel('Background Wavelength');
ylabel('Target Wavelength');
zlabel('Gray Value Differences');


