function I = im2gray(X) %#codegen

% Copyright 2020 The MathWorks, Inc.

narginchk(1,1);

% If 2D perform check for valid numeric types and fall through. Otherwise pass on to
% rgb2gray to do the remaining checks and convert to grayscale.
if ismatrix(X)
   validateattributes(X, {'uint8', 'uint16', 'double', 'single'}, {'real'}, mfilename, 'RGB'); 
   I = X;
else
   I = rgb2gray(X);
end