将X8B8G8R8转换为R8G8B8 C ++代码

时间:2017-08-25 15:21:18

标签: c++ ogre3d

我想将格式为X8B8G8R8的硬件像素缓冲区转换为无符号整数24位内存缓冲区。

这是我的尝试:

 // pixels is uin32_t;
 src.pixels = new pixel_t[src.width*src.height];





    readbuffer->lock( Ogre::HardwareBuffer::HBL_DISCARD );
            const Ogre::PixelBox &pb = readbuffer->getCurrentLock();

            /// Update the contents of pb here
            /// Image data starts at pb.data and has format pb.format
            uint32 *data = static_cast<uint32*>(pb.data);
            size_t height = pb.getHeight();
            size_t width = pb.getWidth();
            size_t pitch = pb.rowPitch; // Skip between rows of image
            for ( size_t y = 0; y<height; ++y )
            {
                for ( size_t x = 0; x<width; ++x )
                {
                    src.pixels[pitch*y + x] = data[pitch*y + x];
                }
            }

1 个答案:

答案 0 :(得分:0)

这应该

uint32_t BGRtoRGB(uint32_t col) {
    return (col & 0x0000ff00) | ((col & 0x000000ff) << 16) | ((col & 0x00ff0000) >> 16)
}

使用

src.pixels[pitch*y + x] = BGRtoRGB(data[pitch*y + x]);

注意:BGRtoRGB这里可以转换两种方式,但是记住它会丢弃X8位(alpha?)中的所有内容,但它应该保留值本身。< / p>

使用0xff

的alpha进行反向转换
uint32_t RGBtoXBGR(uint32_t col) {
    return 0xff000000 | (col & 0x0000ff00) | ((col & 0x000000ff) << 16) | ((col & 0x00ff0000) >> 16)
}