1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 15:27:35 +00:00

LibGL: Fix interpretation of BGRA byte order

This fixes byte order interpretation in several places.
This commit is contained in:
Stephan Unverwerth 2021-08-18 13:20:00 +02:00 committed by Andreas Kling
parent e9514cf6c0
commit 22905daacb
4 changed files with 39 additions and 72 deletions

View file

@ -225,7 +225,7 @@ bool GLContextWidget::load(const String& filename)
if (texture_image) {
// Upload texture data to the GL
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texture_image->width(), texture_image->height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, texture_image->scanline(0));
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texture_image->width(), texture_image->height(), 0, GL_BGRA, GL_UNSIGNED_BYTE, texture_image->scanline(0));
} else {
dbgln("3DFileViewer: Couldn't load texture for {}", filename);
}

View file

@ -35,15 +35,15 @@ static Gfx::RGBA32 to_rgba32(const FloatVector4& v)
u8 g = clamped.y() * 255;
u8 b = clamped.z() * 255;
u8 a = clamped.w() * 255;
return a << 24 | b << 16 | g << 8 | r;
return a << 24 | r << 16 | g << 8 | b;
}
static FloatVector4 to_vec4(Gfx::RGBA32 rgba)
{
return {
(rgba & 0xff) / 255.0f,
((rgba >> 8) & 0xff) / 255.0f,
((rgba >> 16) & 0xff) / 255.0f,
((rgba >> 8) & 0xff) / 255.0f,
(rgba & 0xff) / 255.0f,
((rgba >> 24) & 0xff) / 255.0f
};
}

View file

@ -34,9 +34,9 @@ public:
u32 texel = m_pixel_data.at(y * m_width + x);
return {
(texel & 0xff) / 255.f,
((texel >> 8) & 0xff) / 255.f,
((texel >> 16) & 0xff) / 255.f,
((texel >> 8) & 0xff) / 255.f,
(texel & 0xff) / 255.f,
((texel >> 24) & 0xff) / 255.f
};
}

View file

@ -31,81 +31,48 @@ void Texture2D::upload_texture_data(GLenum, GLint lod, GLint internal_format, GL
auto& mip = m_mipmaps[lod];
const u8* pixel_byte_array = reinterpret_cast<const u8*>(pixels);
// Copy pixel data to storage
mip.pixel_data().clear();
if (format == GL_RGBA) {
for (auto i = 0; i < width * height * 4; i += 4) {
u32 r = pixel_byte_array[i + 0];
u32 g = pixel_byte_array[i + 1];
u32 b = pixel_byte_array[i + 2];
u32 a = pixel_byte_array[i + 3];
// Pixels are already 32-bits wide
if (format == GL_RGBA || format == GL_BGRA) {
mip.pixel_data().resize(width * height * sizeof(u32));
memcpy(mip.pixel_data().data(), pixels, width * height * sizeof(u32));
} else {
mip.pixel_data().resize(width * height * 3);
// Copy RGB or BGR pixel data
for (auto i = 0; i < width * height * 3; i += 3) {
u32 b0 = pixel_byte_array[i]; // B or R
u32 b1 = pixel_byte_array[i + 1]; // G
u32 b2 = pixel_byte_array[i + 2]; // R or B
u32 pixel = ((0xffu << 24) | (b0 << 16) | (b1 << 8) | b2);
u32 pixel = ((a << 24) | (r << 16) | (g << 8) | b);
mip.pixel_data().append(pixel);
}
} else if (format == GL_BGRA) {
for (auto i = 0; i < width * height * 4; i += 4) {
u32 b = pixel_byte_array[i + 0];
u32 g = pixel_byte_array[i + 1];
u32 r = pixel_byte_array[i + 2];
u32 a = pixel_byte_array[i + 3];
u32 pixel = ((a << 24) | (r << 16) | (g << 8) | b);
mip.pixel_data().append(pixel);
}
} else if (format == GL_BGR) {
for (auto i = 0; i < width * height * 3; i += 3) {
u32 b = pixel_byte_array[i + 0];
u32 g = pixel_byte_array[i + 1];
u32 r = pixel_byte_array[i + 2];
u32 a = 255;
// Now we need to swizzle the texture data from `format` to `internal_format`
switch (format) {
case GL_BGR: {
if (internal_format == GL_RGB) {
swizzle(mip.pixel_data(), [](u32 pixel) -> u32 {
u8 r = pixel & 0xff;
u8 g = (pixel >> 8) & 0xff;
u8 b = (pixel >> 16) & 0xff;
return (0xff << 24) | (r << 16) | (g << 8) | b;
});
} else if (internal_format == GL_RGBA) {
swizzle(mip.pixel_data(), [](u32 pixel) -> u32 {
u8 r = pixel & 0xff;
u8 g = (pixel >> 8) & 0xff;
u8 b = (pixel >> 16) & 0xff;
return (r << 24) | (g << 16) | (b << 8) | 0xff;
});
u32 pixel = ((a << 24) | (r << 16) | (g << 8) | b);
mip.pixel_data().append(pixel);
}
} break;
case GL_BGRA: {
if (internal_format == GL_RGB) {
swizzle(mip.pixel_data(), [](u32 pixel) -> u32 {
u8 r = (pixel >> 8) & 0xff;
u8 g = (pixel >> 16) & 0xff;
u8 b = (pixel >> 24) & 0xff;
} else if (format == GL_RGB) {
for (auto i = 0; i < width * height * 3; i += 3) {
u32 r = pixel_byte_array[i + 0];
u32 g = pixel_byte_array[i + 1];
u32 b = pixel_byte_array[i + 2];
u32 a = 255;
return (0xff << 24) | (r << 16) | (g << 8) | b;
});
} else if (internal_format == GL_RGBA) {
swizzle(mip.pixel_data(), [](u32 pixel) -> u32 {
u8 a = pixel & 0xff;
u8 r = (pixel >> 8) & 0xff;
u8 g = (pixel >> 16) & 0xff;
u8 b = (pixel >> 24) & 0xff;
return (r << 24) | (g << 16) | (b << 8) | a;
});
u32 pixel = ((a << 24) | (r << 16) | (g << 8) | b);
mip.pixel_data().append(pixel);
}
} break;
case GL_RGB: {
if (internal_format == GL_RGBA) {
swizzle(mip.pixel_data(), [](u32 pixel) -> u32 {
u8 r = pixel & 0xff;
u8 g = (pixel >> 8) & 0xff;
u8 b = (pixel >> 16) & 0xff;
return (r << 24) | (g << 16) | (b << 8) | 0xff;
});
}
} break;
case GL_RGBA:
break;
default:
// Let's crash for now so we can implement format by format
} else {
VERIFY_NOT_REACHED();
}