clear all
close all

load macbethChart; 
greenChip = 11; redChip = 15; whiteChip = 19; grayChip = 20;
macbethImage = flipud(rot90(reshape(1:24,6,4)));

% This loads the illuminant (sky-light)
load D65; 
lgt = D65;

% This contains the monitor phosphors
load /local/class/psych221/tutorials/cMatch/monitor;

% This contains the CIELAB XYZ functions
load XYZ;

% Estimated gamma value for the monitor
myGamma=2.7;


macbethXYZ = XYZ'* diag(lgt)*macbethChart;
xyz2rgb = inv(XYZ'*phosphors);
macbethLinearRGB1 = xyz2rgb*macbethXYZ;

wht = macbethLinearRGB1(:,whiteChip);

macbethLinearRGB1 = diag(1./wht)*macbethLinearRGB1;

macbethLinearRGB1 = scale(macbethLinearRGB1,0,1);
macbethRGB1 = macbethLinearRGB1.^(1/myGamma);

figure; colormap(macbethRGB1'); 
image(macbethImage);
title('Original Macbeth');

% Add filter (cast) to a few images
factor = 1.5;   % Cast intensity

macbethLinearRGB2 = macbethLinearRGB1; 

% GREEN 
% macbethLinearRGB2(1,:) = factor*macbethLinearRGB1(1,:);
macbethLinearRGB2(2,:) = factor*macbethLinearRGB1(2,:);
% macbethLinearRGB2(3,:) = factor*macbethLinearRGB1(3,:);


macbethRGBCast = scale(macbethLinearRGB2,0,1);

macbethRGB2 = (macbethRGBCast).^(1/myGamma);

figure; colormap(macbethRGB2'); 
image(macbethImage);
title('Green cast with intensity = 1.5');

macbethLinearRGB3 = scale(macbethLinearRGB2,0,1);
r=macbethLinearRGB3(1,:);
g=macbethLinearRGB3(2,:);
b=macbethLinearRGB3(3,:);

[newR,newG,newB] = mbal(r,g,b);
SRbalanced = [newR;newG;newB];
SRbalanced = scale(SRbalanced,0,1);
macbethRGB3 = (SRbalanced).^(1/myGamma);

figure; colormap(macbethRGB3'); 
image(macbethImage);
title('SRA color-balanced image');







