% [X Y Z x y z] = rgb2xyz(M) Colour transformation from RGB to CIE XYZ
%
% Reference:
%
%   William K. Pratt: "Digital Image Processing", 3rd ed. pp. 64ff
%
% In:
%   M: Nx3 array containing rows of RGB values in [0,1].
% Out:
%   X, Y, Z: Nx1 arrays containing the CIE XYZ values.
%   x, y, z: real numbers containing the CIE XYZ values for the
%      reference white (RGB = [1 1 1]).

% Copyright (c) 2005 by Miguel A. Carreira-Perpinan

function [X,Y,Z,x,y,z] = rgb2xyz(M)

% (RGB)_N = RGB NTSC receiver primary color coordinate system
chr = [0.670 0.330 0.000;0.210 0.710 0.080;0.140 0.080 0.780]';
ill = [0.980708;1;1.182163];		% Illuminant C
% $$$ % (RGB)_S = RGB SMPTE receiver primary color coordinate system
% $$$ chr = [0.630 0.340 0.030;0.310 0.595 0.095;0.155 0.070 0.775]';
% $$$ ill = [0.950456;1;1.089058];		% Illuminant D65

% Transformation matrix RGB -> XYZ
RGB2XYZ = chr * diag(chr \ ill/ill(2));

% CIE XYZ
m = M*RGB2XYZ'; X = m(:,1); Y = m(:,2); Z = m(:,3);

% Reference white
x = ill(1); y = ill(2); z = ill(3);

