

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);

% Darken Corners
macbethLinearRGB1(:,1) = 0;
macbethLinearRGB1(:,6) = 0;
macbethLinearRGB1(:,19)= 0;
macbethLinearRGB1(:,24)= 0;


% Add test patch (Yellow and Blue)
macbethLinearRGB1(:,9) = [1;1;0];
macbethLinearRGB1(:,16)= [1;1;0];
macbethLinearRGB1(:,10)= [0;0;1];
macbethLinearRGB1(:,15)= [0;0;1];


% Performing the gray world assumption on the image
GWmacbeth1 = GWbalance(macbethLinearRGB1');
GWmacbeth1 = scale(GWmacbeth1,0,1);
macbethRGB1 = (GWmacbeth1).^(1/myGamma);

figure; colormap(macbethRGB1); 
image(macbethImage);
title('Macbeth with Yellow-Blue patch');

% Add cast to image
factor = 1.5;

GWmacbeth2 = GWmacbeth1;

% GREEN
% GWmacbeth2(:,1) = factor*GWmacbeth1(:,1);
GWmacbeth2(:,2) = factor*GWmacbeth1(:,2);
% GWmacbeth2(:,3) = factor*GWmacbeth1(:,3);


macbethRGBCast = scale(GWmacbeth2,0,1);
macbethRGB2 = (macbethRGBCast).^(1/myGamma);

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

GWmacbeth2 = scale(GWmacbeth2,0,1);
r=GWmacbeth2(:,1);
g=GWmacbeth2(:,2);
b=GWmacbeth2(:,3);
[newR,newG,newB] = mbal(r,g,b);  % Call SRA balance function
SRbalanced = [newR,newG,newB];
SRbalanced = scale(SRbalanced,0,1);
macbethRGB3 = (SRbalanced).^(1/myGamma);
figure; colormap(macbethRGB3); 
image(macbethImage);
title('SRA color-balanced image');







