load colorRGB

i = 1:256;
iMonitorGamaR = (i/256).^(1/1.4870);
iMonitorGamaG = (i/256).^(1/1.6327);
iMonitorGamaB = (i/256).^(1/1.6565);

fidR = fopen('centerYesDifferencesR.txt','w');
fidG = fopen('centerYesDifferencesG.txt','w');
fidB = fopen('centerYesDifferencesB.txt','w');

countBack = 0;
%for backgroundWavelength = 610
for backgroundWavelength = 430:15:730
indexBack = backgroundWavelength-430+1;
countBack = countBack + 1;

countTarget = 0;
%for targetWavelength = 435
%for targetWavelength = 485
for targetWavelength = 430+5:25:730
indexTarget = targetWavelength-430+1;
countTarget = countTarget + 1;

['Processing: ' num2str(backgroundWavelength) '-' num2str(targetWavelength)]

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

imageG = imread(['centerYesOutput/' num2str(backgroundWavelength) '-' num2str(targetWavelength) '.jpg']);

image(:,:,1) = iMonitorGamaR(double(imageG(:,:,1))+1);
image(:,:,2) = iMonitorGamaG(double(imageG(:,:,2))+1);
image(:,:,3) = iMonitorGamaB(double(imageG(:,:,3))+1);

[X map] = rgb2ind(image, 256);
imwrite(X, map, ['centerYesOutputG/a-' num2str(backgroundWavelength) '-' num2str(targetWavelength) '.bmp'], 'bmp')

% Upper Left
upper = 100;
lower = 270;
left = 90;
right = 250;
avg1 = mean(mean(double(image(upper:1:lower,left:1:right,:))));
image(upper:1:lower,left:1:right,:) = 1;

% Upper Right
upper = 120;
lower = 285;
left = 1055;
right = 1215;
avg2 = mean(mean(double(image(upper:1:lower,left:1:right,:))));
image(upper:1:lower,left:1:right,:) = 1;

% Lower Left
upper = 700;
lower = 870;
left = 85;
right = 250;
avg3 = mean(mean(double(image(upper:1:lower,left:1:right,:))));
image(upper:1:lower,left:1:right,:) = 1;

% Lower Right
upper = 700;
lower = 875;
left = 1050;
right = 1210;
avg4 = mean(mean(double(image(upper:1:lower,left:1:right,:))));
image(upper:1:lower,left:1:right,:) = 1;

%imshow(image);

outputRGB(1) = mean([avg1(1,1,1), mean([avg2(1,1,1), avg3(1,1,1)])]);
outputRGB(2) = mean([avg1(1,1,2), mean([avg2(1,1,2), avg3(1,1,2)])]);
outputRGB(3) = mean([avg1(1,1,3), mean([avg2(1,1,3), avg3(1,1,3)])]);

%inputRGB
%outputRGB*255+1
%(outputRGB*255+1) - inputRGB

differences(countBack,countTarget,:) = (outputRGB*255+1) - inputRGB;

fprintf(fidR,'%f\t',differences(countBack,countTarget,1));
fprintf(fidG,'%f\t',differences(countBack,countTarget,2));
fprintf(fidB,'%f\t',differences(countBack,countTarget,3));

[X map] = rgb2ind(image, 256);
imwrite(X, map, ['centerYesOutputG/b-' num2str(backgroundWavelength) '-' num2str(targetWavelength) '.bmp'], 'bmp')

end

fprintf(fidR,'\n');
fprintf(fidG,'\n');
fprintf(fidB,'\n');

end

save 'centerYesDifferences.mat' differences

fclose('all');
