OpenGL byte order for read pixels

Posted on Updated on

OpenGL represent the type and pixel format as big endian byte order,
for example GL_UNSINGED_INT_8_8_8_8, with GL_BGRA, is BGRA in BIG-endian, where
GL_UNSIGNED_INT_8_8_8_8_REV, with GL_BGRA is ARGB in BIG-endian, so, if you want to read the data and send it to other device with little-endian byte order you should:

  1. create a bitmap context with little-endian byte order
  2. read opengl buffer with type and packet format as GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV( or xxx_REV)
  3. set opengl swap byte to 1
  4. read pixel with 2’s parameter setting

sample code:

read from frame buffer:


.... allocate memory for your buffer to contain the data read from opengl's frame buffer
void *data = calloc(bytePerRow,height);
.... create context object
GLenum packedPixelFormat = GL_UNSIGNED_SHORT_1_5_5_5_REV ;
GLenum type = GL_BGRA ;
CGLSetCurrentContext( m_pGLContextObj) ;
CGLSetFullScreen( m_pGLContextObj) ;
glFlush();
glReadBuffer(GL_FRONT);
glFinish();
glReadBuffer();
glPixelStorei(GL_PACK_SWAP_BYTES,1);
glReadPixels( 0, 0, width, height, type, packedPixelFormat , data);
...destory context object

bind with a Quartz bitmap context:

CGContextRef m_hdcWindow = CGBitmapContextCreate( data,m_nWindowX ,m_nWindowY, bitsPerComponent,bytesPerRow,
colorspace, kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder16Little) ;

for create and destory context object, please refer to previous post.

廣告

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

您的留言將使用 WordPress.com 帳號。 登出 / 變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 / 變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 / 變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 / 變更 )

連結到 %s