[Feat] (MG_Util/PixelStoreProcessor): properly handle BGRA to RGBA

swizzle
This commit is contained in:
2025-12-28 23:18:26 +08:00
parent b220c8baab
commit 45276bbb7c
@@ -42,6 +42,40 @@ namespace MobileGL::MG_Util::PixelStoreProcessor {
bytes += rowBytes;
}
}
static Uint8 GetSwizzledChannelValue(Uint8* pixel, TextureSwizzleParam param) {
switch (param) {
case TextureSwizzleParam::Red:
return pixel[0];
case TextureSwizzleParam::Green:
return pixel[1];
case TextureSwizzleParam::Blue:
return pixel[2];
case TextureSwizzleParam::Alpha:
return pixel[3];
case TextureSwizzleParam::Zero:
return 0;
case TextureSwizzleParam::One:
return 0xFF;
default:
return 0xBD;
}
}
// assume 8 bit per channel
// swizzle.size() == channel count
static void ProcessColorSwizzle(void* data, SizeT pixelCount, const Vector<TextureSwizzleParam>& swizzle) {
const auto bpp = swizzle.size();
Uint8* bytes = static_cast<Uint8*>(data);
static Uint8 pixelScratch[4];
for (SizeT i = 0; i < pixelCount; ++i) {
Uint8* pixel = bytes + i * bpp;
for (SizeT ch = 0; ch < bpp; ++ch) {
pixelScratch[ch] = GetSwizzledChannelValue(pixel, swizzle[ch]);
}
Memcpy(pixel, pixelScratch, bpp);
}
}
void* ProcessTexturePixelsDataUnpack(const void* inputPixels, const PixelStoreParameters& params,
TextureInternalFormat targetInternalFormat, TextureInputFormat textureInputFormat, TexturePixelDataType inputDataType,
@@ -90,14 +124,29 @@ namespace MobileGL::MG_Util::PixelStoreProcessor {
for (Int y = 0; y < copyHeight; ++y) {
Memcpy(layerDst, layerSrc, static_cast<SizeT>(copyWidth) * pixelSize);
if (params.SwapBytes && pixelSize > 1) {
MGLOG_D("%s: SwapBytes", __func__);
SwapBytes(layerDst, pixelSize, static_cast<SizeT>(copyWidth));
}
if (params.LSBFirst && isBitmap) {
MGLOG_D("%s: LSBFirst", __func__);
ProcessLSBFirst(layerDst, static_cast<SizeT>(copyWidth), 1);
}
if (textureInputFormat == TextureInputFormat::BGRA && targetInternalFormat == TextureInternalFormat::RGBA8) {
MGLOG_D("%s: Swizzle (BGRA)", __func__);
MGLOG_D("%s: pixel0 before = %x", __func__, *((Uint32*)layerDst));
ProcessColorSwizzle(layerDst, static_cast<SizeT>(copyWidth), {
TextureSwizzleParam::Green,
TextureSwizzleParam::Blue,
TextureSwizzleParam::Alpha,
TextureSwizzleParam::Red
});
MGLOG_D("%s: pixel0 after = %x", __func__, *((Uint32*)layerDst));
} else
MGLOG_D("%s: pixel0 = %x", __func__, *((Uint32*)layerDst));
layerSrc += inputStride;
layerDst += static_cast<SizeT>(copyWidth) * pixelSize;