Light | Dark

glReadPixels

Name

glReadPixels — read a block of pixels from the frame buffer

C Specification

void glReadPixels( GLint x,
GLint y,
GLsizei width,
GLsizei height,
GLenum format,
GLenum type,
GLvoid * data);

Parameters

x, y

Specify the window coordinates of the first pixel that is read from the frame buffer. This location is the lower left corner of a rectangular block of pixels.

width, height

Specify the dimensions of the pixel rectangle. width and height of one correspond to a single pixel.

format

Specifies the format of the pixel data. The following symbolic values are accepted: GL_RGBA, and GL_RGBA_INTEGER. An implementation-chosen format will also be accepted. This can be queried with glGet and GL_IMPLEMENTATION_COLOR_READ_FORMAT.

type

Specifies the data type of the pixel data. Must be one of GL_UNSIGNED_BYTE, GL_UNSIGNED_INT, GL_UNSIGNED_INT_2_10_10_10_REV, GL_INT, or GL_FLOAT. An implementation-chosen type will also be accepted. This can be queried with glGet and GL_IMPLEMENTATION_COLOR_READ_TYPE.

data

Returns the pixel data.

Description

glReadPixels returns pixel data from the frame buffer, starting with the pixel whose lower left corner is at location (x, y), into client memory starting at location data. Several parameters control the processing of the pixel data before it is placed into client memory. These parameters are set with glPixelStorei. This reference page describes the effects on glReadPixels of most, but not all of the parameters specified by these three commands.

If a non-zero named buffer object is bound to the GL_PIXEL_PACK_BUFFER target (see glBindBuffer) while a block of pixels is requested, data is treated as a byte offset into the buffer object's data store rather than a pointer to client memory.

glReadPixels returns values from each pixel with lower left corner at x + i y + j for 0 <= i < width and 0 <= j < height . This pixel is said to be the ith pixel in the jth row. Pixels are returned in row order from the lowest to the highest row, left to right in each row.

format specifies the format for the returned pixel values; accepted values are GL_RED, GL_RED_INTEGER, GL_RG, GL_RG_INTEGER, GL_RGB, GL_RGB_INTEGER, GL_RGBA, GL_RGBA_INTEGER, GL_LUMINANCE_ALPHA, GL_LUMINANCE, and GL_ALPHA

Finally, the indices or components are converted to the proper format, as specified by type. If type is GL_FLOAT, then each integer index is converted to single-precision floating-point format.

If format is GL_RED, GL_RG, GL_RGB, or GL_RGBA, and type is not GL_FLOAT, each component is multiplied by the multiplier shown in the following table. If type is GL_FLOAT, then each component is passed as is (or converted to the client's single-precision floating-point format if it is different from the one used by the GL).

type Index Mask Component Conversion
GL_UNSIGNED_BYTE 2 8 - 1 2 8 - 1 c
GL_BYTE 2 7 - 1 2 8 - 1 c - 1 2
GL_HALF_FLOAT none c
GL_FLOAT none c
GL_UNSIGNED_SHORT_5_6_5 2 N - 1 2 N - 1 c
GL_UNSIGNED_SHORT_4_4_4_4 2 N - 1 2 N - 1 c
GL_UNSIGNED_SHORT_5_5_5_1 2 N - 1 2 N - 1 c
GL_UNSIGNED_INT_2_10_10_10_REV 2 N - 1 2 N - 1 c
GL_UNSIGNED_INT_10F_11F_11F_REV -- Special
GL_UNSIGNED_INT_5_9_9_9_REV -- Special

Return values are placed in memory as follows. If format is GL_RED, or GL_RED_INTEGER, a single value is returned and the data for the ith pixel in the jth row is placed in location j width + i . GL_RG and GL_RG_INTEGER return two values, GL_RGB and GL_RGB_INTEGER return three values, GL_RGBA and GL_RGBA_INTEGER return four values for each pixel, with all values corresponding to a single pixel occupying contiguous space in data. See glPixelStorei for a description of parameters which affect the packing of data into memory.

Notes

Values for pixels that lie outside the window connected to the current GL context are undefined.

If an error is generated, no change is made to the contents of data.

Only three format/type parameter pairs are accepted. For normalized fixed point rendering surfaces, GL_RGBA/GL_UNSIGNED_BYTE is accepted. For signed integer rendering surfaces, GL_RGBA_INTEGER/GL_INT is accepted. For unsigned integer rendering surfaces, GL_RGBA_INTEGER/GL_UNSIGNED_INT is accepted. For GL_RGBA10_A2 rendering surfaces, GL_RGBA/GL_UNSIGNED_INT_2_10_10_10_REV is accepted. The other acceptable pair can be discovered by querying GL_IMPLEMENTATION_COLOR_READ_FORMAT and GL_IMPLEMENTATION_COLOR_READ_TYPE. The implementation chosen format may also vary depending on the format of the currently bound rendering surface.

Errors

GL_INVALID_ENUM is generated if format or type is not an accepted value.

GL_INVALID_VALUE is generated if either width or height is negative.

GL_INVALID_OPERATION is generated if a non-zero buffer object name is bound to the GL_PIXEL_PACK_BUFFER target and the buffer object's data store is currently mapped.

GL_INVALID_OPERATION is generated if a non-zero buffer object name is bound to the GL_PIXEL_PACK_BUFFER target and the data would be packed to the buffer object such that the memory writes required would exceed the data store size.

GL_INVALID_OPERATION is generated if GL_READ_BUFFER is GL_NONE or if GL_READ_FRAMEBUFFER_BINDING is non-zero and the read buffer selects an attachment that has no image attached.

GL_INVALID_OPERATION is generated if GL_READ_FRAMEBUFFER_BINDING is non-zero, the read framebuffer is complete, and the value of GL_SAMPLE_BUFFERS for the read framebuffer is greater than zero.

GL_INVALID_OPERATION is generated if the readbuffer of the currently bound framebuffer is a fixed point normalized surface and format and type are neither GL_RGBA and GL_UNSIGNED_BYTE, respectively, nor the format/type pair returned by querying GL_IMPLEMENTATION_COLOR_READ_FORMAT and GL_IMPLEMENTATION_COLOR_READ_TYPE.

GL_INVALID_OPERATION is generated if the readbuffer of the currently bound framebuffer is a floating point surface and format and type are neither GL_RGBA and GL_FLOAT, respectively, nor the format/type pair returned by querying GL_IMPLEMENTATION_COLOR_READ_FORMAT and GL_IMPLEMENTATION_COLOR_READ_TYPE.

GL_INVALID_OPERATION is generated if the readbuffer of the currently bound framebuffer is a signed integer surface and format and type are neither GL_RGBA_INTEGER and GL_INT, respectively, nor the format/type pair returned by querying GL_IMPLEMENTATION_COLOR_READ_FORMAT and GL_IMPLEMENTATION_COLOR_READ_TYPE.

GL_INVALID_OPERATION is generated if the readbuffer of the currently bound framebuffer is an unsigned integer surface and format and type are neither GL_RGBA_INTEGER and GL_UNSIGNED_INT, respectively, nor the format/type pair returned by querying GL_IMPLEMENTATION_COLOR_READ_FORMAT and GL_IMPLEMENTATION_COLOR_READ_TYPE.

GL_INVALID_FRAMEBUFFER_OPERATION is generated if the currently bound framebuffer is not framebuffer complete (i.e. the return value from glCheckFramebufferStatus is not GL_FRAMEBUFFER_COMPLETE).

Associated Gets

glGet with argument GL_PIXEL_PACK_BUFFER_BINDING

Tutorials

API Version Support

OpenGL ES API Version
Function Name 2.0 3.0 3.1
glReadPixels
Think you can improve this page? Edit this page on GitHub.