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_ALPHA, GL_RGB, and GL_RGBA.

type

Specifies the data type of the pixel data. Must be one of GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT_5_6_5, GL_UNSIGNED_SHORT_4_4_4_4, or GL_UNSIGNED_SHORT_5_5_5_1.

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. The GL_PACK_ALIGNMENT parameter, set with the glPixelStorei command, affects the processing of the pixel data before it is placed into 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_ALPHA
GL_RGB
GL_RGBA

RGBA color components are read from the color buffer. Each color component is converted to floating point such that zero intensity maps to 0.0 and full intensity maps to 1.0.

Unneeded data is then discarded. For example, GL_ALPHA discards the red, green, and blue components, while GL_RGB discards only the alpha component. The final values are clamped to the range 0 1 .

Finally, the components are converted to the proper format, as specified by type. When type is GL_UNSIGNED_BYTE, each component is multiplied by 2 8 - 1 . When type is GL_UNSIGNED_SHORT_5_6_5, GL_UNSIGNED_SHORT_4_4_4_4, or GL_UNSIGNED_SHORT_5_5_5_1, each component is multiplied by 2 N - 1 , where N is the number of bits in the bitfield.

Return values are placed in memory as follows. If format is GL_ALPHA, a single value is returned and the data for the ith pixel in the jth row is placed in location j width + i . GL_RGB returns three values and GL_RGBA returns four values for each pixel, with all values corresponding to a single pixel occupying contiguous space in data. Storage parameter GL_PACK_ALIGNMENT, set by glPixelStorei, affects the way that data is written into memory. See glPixelStorei for a description.

Notes

If the currently bound framebuffer is not the default framebuffer object, color components are read from the color image attached to the GL_COLOR_ATTACHMENT0 attachment point.

Only two format/type parameter pairs are accepted. GL_RGBA/GL_UNSIGNED_BYTE is always accepted, and the other acceptable pair can be discovered by querying GL_IMPLEMENTATION_COLOR_READ_FORMAT and GL_IMPLEMENTATION_COLOR_READ_TYPE.

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.

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 type is GL_UNSIGNED_SHORT_5_6_5 and format is not GL_RGB.

GL_INVALID_OPERATION is generated if type is GL_UNSIGNED_SHORT_4_4_4_4 or GL_UNSIGNED_SHORT_5_5_5_1 and format is not GL_RGBA.

GL_INVALID_OPERATION is generated if 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_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_IMPLEMENTATION_COLOR_READ_FORMAT or GL_IMPLEMENTATION_COLOR_READ_TYPE

glGet with argument GL_PACK_ALIGNMENT

Tutorials

Think you can improve this page? Edit this page on GitHub.