function I = cmap2gray(X) %#codegen

% Copyright 2020 The MathWorks, Inc.

narginchk(1,1);

% Check whether inputs are 2D
is2D = coder.const(numel(size(X)) == 2);
coder.internal.assert(is2D, 'MATLAB:images:cmap2gray:invalidSizeForColormap');

% Check if size of colormap is valid
coder.internal.errorIf((size(X,2) ~= 3 || size(X,1) < 1),'MATLAB:images:cmap2gray:invalidSizeForColormap');

% Check if MAP is a double in [0,1]
validateattributes(X,{'double'},{'nonnan','finite','real'},mfilename, 'MAP');
% Calculate transformation matrix
T    = inv([1.0 0.956 0.621; 1.0 -0.272 -0.647; 1.0 -1.106 1.703]);
coef = T(1,:);

%coef = [0.2989 0.5870 0.1140];
coder.const(coef);

 % input is 2D
I1 = X * coef';
I2 = min(max(I1,0),1);
I = [I2,I2,I2];


end
