clear; %close all;

load responsivity_meas/dcm_crw24.mat;
load responsivity_meas/dcmT_24.mat;
dcm; % 24x4
dcmT; % 24x3


color_flag = 3;
if color_flag == 1;
	% Jong and Ulrich try 1
	color_corr = [
		1   1   -1   0
		1   0    0    0
		1   1    0   -1];
elseif color_flag == 2;
	% Jong and Ulrich try 2
	rgb2gmcy = [
	 0 1 0
	 1 0 1
	 0 1 1
	 1 1 0 ];
elseif color_flag == 3;
	% From the c code
	rgb2gmcy =     [
			 0.11, 0.86, 0.08
			 0.50, 0.29, 0.51
			 0.11, 0.92, 0.75
			 0.81, 0.98, 0.08 ];
end;
color_corr = pinv(rgb2gmcy)
	RGB_arr = color_corr*dcm';
	max_rgb = max(RGB_arr(:));
old_color_corr = color_corr/max_rgb;

gamma = 2.2;

	dcmT = dcmT.^gamma;

for ind = 1:1;
	RGB_arr = old_color_corr*dcm';
	y_diff = (RGB_arr - dcmT')';
 	C_e = dcm \ y_diff; % Least Squares
	new_color_corr = old_color_corr - C_e';
	error_arr(ind) = norm(C_e);
	%keyboard
	old_color_corr = new_color_corr;
end;

color_corr = old_color_corr*max_rgb
%save new_color_corr color_corr;
%------------ The END of Color CORRECTION -----------------%
