2017-08-25 83 views
-1

我想将X8B8G8R8格式的硬件像素缓冲区转换为无符号整型24位内存缓冲区。将X8B8G8R8转换为R8G8B8 C++代码

这里是我的尝试:

// 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]; 
       } 
      } 
+0

所以,您有什么问题的阿尔法转换? – auburg

+0

而结果/问题是? – Matt

回答

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?)中拥有的任何东西,但它会抛出他们自己保留这些价值。

要倒过来用的0xff

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

谢谢。我还有一件事。我将如何将rgb转换为xbgr – andre

+0

对,这个函数转换两种方式,但会丢掉较高的位('X8'?)。你必须从某处获得这8位数据。 – N00byEdge

+0

如果你想保留转换后数字中的8位高位(我不确定你是如何解析它的话),把0x0000ff00的第一个位掩码改为0xff00ff00如果你这样做,它将完全对称。 – N00byEdge