2003-2004 Silicon Graphics, Inc.
glReadPixels 3G
glReadPixels read a block of pixels from the color buffer

## C Specification

void `glReadPixels` GLint x GLint y GLsizei width GLsizei height GLenum format GLenum type GLvoid * pixels

## Parameters

x, y

  Specify the window coordinates of the first pixel that is read from the color 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. Must be either `GL_RGBA` or the value of `GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES`.

type

  Specifies the data type of the pixel data. Must be either `GL_UNSIGNED_BYTE` or the value of `GL_IMPLEMENTATION_COLOR_READ_TYPE_OES`.

pixels

  Returns the pixel data.

## Description

`glReadPixels` returns pixel data from the color buffer, starting with the pixel whose lower left corner is at location (x, y), into client memory starting at location pixels. The processing of the pixel data before it is placed into client memory can be controlled with glPixelStorei.

`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 of 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. `GL_LUMINANCE` computes a single-component value as the sum of the red, green, and blue components, and `GL_LUMINANCE_ALPHA` does the same, while keeping alpha as a second value. The final values are clamped to the range [0, 1].

Finally, the components are converted to the proper, as specified by type where each component is multiplied by 2 n-1 , where n is the number of bits per component.

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 pixels. Storage parameter `GL_PACK_ALIGNMENT` set by glPixelStorei, affects the way that data is written into memory. See glPixelStorei for a description.

## Notes

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_OES` and `GL_IMPLEMENTATION_COLOR_READ_TYPE_OES`.

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 pixels.

## Errors

`GL_INVALID_ENUM` is generated if format is not `GL_RGBA` or the value of `GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES`.

`GL_INVALID_ENUM` is generated if type is not `GL_UNSIGNED_BYTE` or the value of `GL_IMPLEMENTATION_COLOR_READ_TYPE_OES`.

`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_OES` and `GL_IMPLEMENTATION_COLOR_READ_TYPE_OES`.

## Associated Gets

glGet with argument `GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES`

glGet with argument `GL_IMPLEMENTATION_COLOR_READ_TYPE_OES`

glGet with argument `GL_PACK_ALIGNMENT`

## See Also

glPixelStorei

## Copyright

Copyright 2003-2004 Silicon Graphics, Inc. This document is licensed under the SGI Free Software B License. For details, see http://oss.sgi.com/projects/FreeB/.