Repository: Genesis-Embodied-AI/Genesis
File: genesis/ext/pyrender/utils.py (Python)

Code before:
            texture = texture.astype(np.uint8)
        else:
            raise TypeError("Invalid type {} for texture".format(type(texture)))

        # Format array by picking out correct texture channels or padding
        if texture.ndim == 2:
            texture = texture[:, :, np.newaxis]
        if target_channels == "R":
            texture = texture[:, :, 0]
            texture = texture.squeeze()
        elif target_channels == "RG":
            if texture.shape[2] == 1:
                texture = np.repeat(texture, 2, axis=2)
            else:
                texture = texture[:, :, (0, 1)]
        elif target_channels == "GB":
            if texture.shape[2] == 1:
                texture = np.repeat(texture, 2, axis=2)
            elif texture.shape[2] > 2:
                texture = texture[:, :, (1, 2)]
        elif target_channels == "RGB":
            if texture.shape[2] == 1:
                texture = np.repeat(texture, 3, axis=2)
            elif texture.shape[2] == 2:
                l = texture[:, :, 0]
                texture = np.stack([l, l, l], axis=2)
            else:
                texture = texture[:, :, (0, 1, 2)]
        elif target_channels == "RGBA":
            if texture.shape[2] == 1:
                texture = np.repeat(texture, 4, axis=2)
                texture[:, :, 3] = 255
            elif texture.shape[2] == 2:
                l = texture[:, :, 0]
                a = texture[:, :, 1]
                tx = np.empty((texture.shape[0], texture.shape[1], 4), dtype=texture.dtype)
                tx[:, :, 0] = l
                tx[:, :, 1] = l
                tx[:, :, 2] = l
                tx[:, :, 3] = a
                texture = tx
            elif texture.shape[2] == 3:
                tx = np.empty((texture.shape[0], texture.shape[1], 4), dtype=np.uint8)
                tx[:, :, :3] = texture
                tx[:, :, 3] = 255
                texture = tx
        else:
            raise ValueError("Invalid texture channel specification: {}".format(target_channels))
    else:
        raise TypeError("Invalid type {} for texture".format(type(texture)))


Review comment (suggestion):
Just do `texture = texture[:, :, (0, 0, 0)]`.

Plus add a comment to explain what it does (as you did in the description of the PR).

Code after:
            texture = texture.astype(np.uint8)
        else:
            raise TypeError("Invalid type {} for texture".format(type(texture)))

        # Format array by picking out correct texture channels or padding
        if texture.ndim == 2:
            texture = texture[:, :, np.newaxis]
        if target_channels == "R":
            texture = texture[:, :, 0]
            texture = texture.squeeze()
        elif target_channels == "RG":
            if texture.shape[2] == 1:
                texture = np.repeat(texture, 2, axis=2)
            else:
                texture = texture[:, :, (0, 1)]
        elif target_channels == "GB":
            if texture.shape[2] == 1:
                texture = np.repeat(texture, 2, axis=2)
            elif texture.shape[2] > 2:
                texture = texture[:, :, (1, 2)]
        elif target_channels == "RGB":
            if texture.shape[2] == 1:
                texture = np.repeat(texture, 3, axis=2)
            elif texture.shape[2] == 2:
                # Convert 2-channel texture to grayscale RGB by duplicating the first channel
                texture = texture[:, :, (0, 0, 0)]
            else:
                texture = texture[:, :, (0, 1, 2)]
        elif target_channels == "RGBA":
            if texture.shape[2] == 1:
                texture = np.repeat(texture, 4, axis=2)
                texture[:, :, 3] = 255
            elif texture.shape[2] == 2:
                # Convert 2-channel texture (luminance + alpha) to RGBA by duplicating luminance to RGB
                luminance = texture[:, :, :1]
                alpha = texture[:, :, 1]
                texture = np.empty((*texture.shape[:2], 4), dtype=texture.dtype)
                texture[:, :, :3] = luminance
                texture[:, :, 3] = alpha
            elif texture.shape[2] == 3:
                tx = np.empty((*texture.shape[:2], 4), dtype=np.uint8)
                tx[:, :, :3] = texture
                tx[:, :, 3] = 255
                texture = tx
        else:
            raise ValueError("Invalid texture channel specification: {}".format(target_channels))
    else:
        raise TypeError("Invalid type {} for texture".format(type(texture)))

    return texture