GL_OES_read_format#

Supported Hardware#

Series5, Series5XT, Series6, Series6XE, Series6XT

Valid APIs#

OpenGL ES 1.x

Description#

Reading pixels from a framebuffer in OpenGL ES is performed via the function glReadPixels(). This function is able to read data out in only one format for OpenGL ES 1.x; RGBA8888. This extension adds another, implementation defined format, which can be queried with glGetIntegerv(). These additional formats are limited to being RGBA, RGB or Alpha-Only formats.

Note#

This functionality is core to OpenGL ES 2.0 and 3.0.

Example#

// Find out the preferred format and type to read back pixels on a platform
GLint preferredFormat;
GLint preferredType;
glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES, &preferredFormat);
glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES, &preferredType);
// Figure out how big the texture is going to be.
GLuint pixelReadDataSize = 0;
switch(preferredFormat)
{
case GL_RGBA:
    {
        switch(preferredType)
        {
        case GL_UNSIGNED_BYTE:
            pixelReadDataSize = readWidth * readHeight * 4;
            break;
        case GL_UNSIGNED_SHORT_4_4_4_4:
        case GL_UNSIGNED_SHORT_5_5_5_1:
            pixelReadDataSize = readWidth * readHeight * 2;
            break;
        }
        break;
    }
case GL_RGB:
    {
        switch(preferredType)
        {
        case GL_UNSIGNED_BYTE:
            pixelReadDataSize = readWidth * readHeight * 3;
            break;
        case GL_UNSIGNED_SHORT_5_6_5:
            pixelReadDataSize = readWidth * readHeight * 2;
            break;
        }
        break;
    }
case GL_LUMINANCE_ALPHA:
    {
        pixelReadDataSize = readWidth * readHeight * 2;
        break;
    }
case GL_LUMINANCE:
    {
        pixelReadDataSize = readWidth * readHeight;
        break;
    }
case GL_ALPHA:
    {
        pixelReadDataSize = readWidth * readHeight;
        break;
    }
}
// Allocate enough space to read the data
GLvoid* pixelReadData = malloc(pixelReadDataSize);
// Specify the pixel unpack operation to be tightly packed (no padding) when reading back
// data, otherwise this needs to be handled when working out the size
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// Read pixels using these formats
if (pixelReadData)
{
    glReadPixels(readOriginX, readOriginY, readWidth, readHeight, preferredFormat, preferredType, pixelReadData);
}