c++graphicscudargbaoptix

BGRA -> RGBA and vertical flip, OptiX


I have a buffer containing a "raw" BGRA texture with one byte per color. The lines are in reversed order (the texture is upside down).

The BGRA buffer is all green (0, 255, 0, 255).

I need to convert that to RGBA and flip the textures lines. I tried this:

// bgra is an  unsigned char*

int width = 1366;
int height = 768;    

unsigned char* rgba = new unsigned char[width * height * 4];

for(int y = height - 1; y >= 0; y--)
{
    for(int x = 0; x < width; x++)
    {
        rgba[(x * y * 4)]     = bgra[(x * y * 4) + 2];
        rgba[(x * y * 4) + 1] = bgra[(x * y * 4) + 1];
        rgba[(x * y * 4) + 2] = bgra[(x * y * 4)];
        rgba[(x * y * 4) + 3] = bgra[(x * y * 4) + 3];
    }
}

But the result when rendered is not a full green screen, but this:

What might i be doing wrong here?


Solution

  • You're indexing wrong.

    This is how it should be done:

    rgba[(x + y * width) * 4]     = bgra[(x + y * width) * 4 + 2]