% this file processes the chormaticity of the color background
% pictures that we received.

load result	% result from the file colorprocess

% Calculate the Chromaticity of the original images
% using the rgb definition for the encoding.
red = [1 0 0];
green = [0 1 0];
blue = [ 0 0 1];

% some other surrounding color
yellow = [0.985 0.864 0.371];
purple = [0.528 0.432 0.562];
orange = [0.932 0.600 0.403];
sinline = 0.9+0.1*sin(2*pi*[0:800]/120);
colorset = [red;green;blue;yellow;orange;purple];
rgb2xyz = [0.5879 0.1792 0.1831; 0.2896 0.6058 0.1046; 0.0 0.06826 1.02];

for i=1:1:6
   colorChrom(i,1:801,1:2) = chrom(rgb2xyz*(colorset(i,:)'*sinline*100))';
end



setcolor = ['r' 'g' 'b' 'y' 'c' 'm'];
colorstring = {'Red','Green','Blue','Yellow','Orange','Purple','Original'};
company = {'PhotoNet','Ofoto','PhotoAccess','Shutterfly'};
for i=1:4
   figure
   for j=1:6
      hold on
      temp = squeeze(chroresult(i,j,:,:));
      plot(temp(:,1),temp(:,2),[setcolor(j) '.']);
   end
   for j=1:6
      plot(squeeze(colorChrom(j,:,1)),squeeze(colorChrom(j,:,2)),'k.');
   end
   legend(colorstring);
   title(['Chromaticity of Mean Color of the Photographs Received from ' company{i}]);
   xlabel('X-Chromaticity');
   ylabel('Y-Chromaticity');
   axis([0.1 0.7 0.05 0.8]);
end

% collecting the chromaticity from the gray patches
companyname = {'net','ofoto','acess','fly'};
for i=1:4
   for j=1:6
      filename = [companyname{i} int2str(j+10)];
		im = imread(filename,'jpg');
      [x,y,z] = size(im);
      vloc = [10 109 10 109 x-109 x-10 x-109 x-10];
      hloc = [30 129 y-129 y-30 30 129 y-129 y-30];
      temp1 = im(vloc(1):vloc(2),hloc(1):hloc(2),:);
		temp2 = im(vloc(3):vloc(4),hloc(3):hloc(4),:);
		temp3 = im(vloc(5):vloc(6),hloc(5):hloc(6),:);
		temp4 = im(vloc(7):vloc(8),hloc(7):hloc(8),:);
      temp1 = double(reshape(temp1,10000,3));
		temp2 = double(reshape(temp2,10000,3));
		temp3 = double(reshape(temp3,10000,3));
		temp4 = double(reshape(temp4,10000,3));
      grayChrom(i,j,1,:,:) = chrom(rgb2xyz*temp1'*100/255);
      grayChrom(i,j,2,:,:) = chrom(rgb2xyz*temp2'*100/255);
      grayChrom(i,j,3,:,:) = chrom(rgb2xyz*temp3'*100/155);
      grayChrom(i,j,4,:,:) = chrom(rgb2xyz*temp4'*100/255);
   end
end

graypatches = [0.5 0.5 0.5];
graypatches = [graypatches;graypatches/norm(graypatches)];
graypatcheschro = chrom(rgb2xyz*graypatches'*100);
% plotting these patches
grayChrom = grayChrom(:,:,:,:,1:10:1000);

for i=1:4
   figure
   for j=1:6
   hold on
   plot(reshape(squeeze(grayChrom(i,j,:,1,:)),400,1),...
      reshape(squeeze(grayChrom(i,j,:,2,:)),400,1),...
            [setcolor(j) '.']);
   end
   plot(graypatcheschro(1,:),graypatcheschro(2,:),'k*');
   legend(colorstring);
   title(['Chromaticity of Gray Patches of the Photographs Received from ' company{i}]);
   xlabel('X-Chromaticity');
   ylabel('Y-Chromaticity');
   axis([0.25 0.35 0.25 0.35]);
end
