%#codegen
function varargout = ind2rgb(a,cm) 
%IND2RGB Convert indexed image to RGB image.
%   RGB = IND2RGB(X,MAP) converts the matrix X and corresponding
%   colormap MAP to RGB (truecolor) format.
%
%   Class Support
%   -------------
%   X can be of class uint8, uint16, or double. RGB is an 
%   M-by-N-by-3 array of class double.
%
%   See also RGB2IND.

%   Clay M. Thompson 9-29-92
%   Copyright 1984-2018 The MathWorks, Inc.

narginchk(2,2);

if coder.isColumnMajor
    if nargout == 1
        varargout{1} = matlab.images.internal.ind2rgb(a, cm);
    else
        [varargout{1}, varargout{2}, varargout{3}] = matlab.images.internal.ind2rgb(a, cm);
    end
else
    indexedImage = matlab.images.internal.ind2rgbPreProcess(a, cm);
    
    height = size(indexedImage, 1);
    width = size(indexedImage, 2);

    if nargout > 1 % R, G and B channels are requested as separate outputs
        r = coder.nullcopy(zeros(height, width));
        g = coder.nullcopy(zeros(height, width));
        b = coder.nullcopy(zeros(height, width));
        for y = 1:height
            for x = 1:width
                r(y, x) = cm(indexedImage(y, x), 1);
                g(y, x) = cm(indexedImage(y, x), 2);
                b(y, x) = cm(indexedImage(y, x), 3);
            end
        end
        varargout{1} = r;
        varargout{2} = g;
        varargout{3} = b;
    else % Output requested as one true colour image
        r = coder.nullcopy(zeros(height, width, 3));
        for y = 1:height
            for x = 1:width
                r(y, x, 1) = cm(indexedImage(y, x), 1);
                r(y, x, 2) = cm(indexedImage(y, x), 2);
                r(y, x, 3) = cm(indexedImage(y, x), 3);
            end
        end
        varargout{1} = r;
    end
end
