function img = XYZ2sRGB(img, Illum)
% The following function performs the transformation from XYZ to sRGB.

[height,width,Ncolors] = size(img);
img = permute(img,[3,1,2]);
img = reshape(img,Ncolors,height*width);

if Illum == 65
    % T to transform from XYZ2sRGB based on D65 Illuminant
    T = [  3.2404542, -1.5371385, -0.4985314;
        -0.9692660,  1.8760108,  0.0415560;
        0.0556434, -0.2040259,  1.0572252];
elseif Illum == 1
    T = [0.4969 0.3391 0.1640;
        0.2562 0.6782 0.0656;
        0.0233 0.1130 0.8637];
elseif Illum == 50
    % T to transform from XYZ2sRGB based on D50 Illuminant
    T = [  3.1338561,   -1.6168667, -0.4906146;
        -0.9787684,    1.9161415,  0.0334540;
        0.0719453,   -0.2289914,  1.4052427];
end
% Transform to XYZ coordinates. We use the inverse transformation.
img=double(img);
img = T*img;

% img = T.\img;


% Put image back in standard format
img = reshape(img,Ncolors,height,width);
img = ipermute(img,[3,1,2]);


