%
% PerfectReflectorCompare.m
%
% 1. Compute difference of input and Perfect Reflector Algorithm output;
% 2. Compute difference of camera output and Perfect Reflector 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 PR difference
%backgroundCol = 430:15:730;
%primeCol = 430+5:25:730;

PRDiff = zeros(21,12,4);

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

		PRoutputfn = strcat('../web/PRoutput/PRoutput',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(PRoutputfn,'bmp');
			im = double(im)./255;
		else
			[X,map] = imread(PRoutputfn,'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));
 
		PRDiff(j,i,1:3) = (outputRGB*255+1) - inputRGB;
		
		j=j+1;
	end
	i=i+1;
end

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

CA_PRDiff = PRDiff - CameraDiff;

save('PRDiff.mat','CameraDiff','PRDiff','CA_PRDiff');


% Plot Input/PerfectReflector Differences

backgroundCol = 430:15:730;
primeCol = 430+5:25:730;
PRDiffR=PRDiff(:,:,1)';

figure
mesh(backgroundCol,primeCol,PRDiffR);
title('Input/PerfectReflector Red Value Differences');
xlabel('Background Wavelength');
ylabel('Target Wavelength');
zlabel('R Value Differences');

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

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

PRDiffGray=PRDiff(:,:,4)';
figure
mesh(backgroundCol,primeCol,PRDiffGray);
title('Input/PerfectReflector Gray Value Differences');
xlabel('Background Wavelength');
ylabel('Target Wavelength');
zlabel('Gray Value Differences');


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

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

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

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


