Skip to content

[rtextures] ColorReplace() does not replace alpha values for RGB images #5009

Open
@jestarray

Description

@jestarray

EDIT

In a way this isn't a bug cause RGB images have no alpha values, but nonetheless

Issue description

steps to reproduce:

  1. Load in an imageformat that is RGB(use the one here):

Image

  1. Call ColorReplace() and you will notice it doesnt replace the alpha values

If we have:

// Imagine the image we're passing in a 1x1 (0, 0, 0, 255) pixel
ColorReplace([0, 0, 0, 255], [0, 0, 0, 0]);
// the 255 does not get replaced with a 0 for RGB image formats

https://github.com/raysan5/raylib/blob/master/src/rtextures.c#L2930
The bug occurs because of these lines:

    int format = image->format;
    RL_FREE(image->data);

    image->data = pixels;
    image->format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8;

    ImageFormat(image, format);

We set the new format but ImageFormat will write it back to the OLD format because that's what we pass in. This leads to the PIXELFORMAT_UNCOMPRESSED_R8G8B8A8 branch not being taken for Images that are RGB

purposed solutions:

  • Don't call ImageFormat because we've already replaced the colors above. HOWEVER, ImageFormat does also handle mipmap generation, so if ColorReplace are to replace the colors in those mipmaps, then this solution is invalid.
  • Let ImageFormat do the overwriting of image->format

A lot of the other image manipulation functions also have this same pattern, so there might be potential bugs there, and they should be reviewed for RGB images

Environment

Linux, GL 3.3, rx480

This guy had same issue
https://discord.com/channels/426912293134270465/540620507708522517/1387477654768320552

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions