[ Previous | Next | Contents | Glossary | Home | Search ]
OpenGL 1.1 for AIX: Reference Manual

glReadPixels Subroutine

Purpose

Reads a block of pixels from the frame buffer.

Library

OpenGL C bindings library: libGL.a

C Syntax

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 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 1 (one) correspond to a single pixel.
Format Specifies the format of the pixel data. The following symbolic values are accepted:
  • GL_COLOR_INDEX
  • GL_STENCIL_INDEX
  • GL_DEPTH_COMPONENT
  • GL_RED
  • GL_GREEN
  • GL_BLUE
  • GL_ALPHA
  • GL_RGB
  • GL_RGBA
  • GL_ABGR_EXT
  • GL_LUMINANCE
  • GL_LUMINANCE_ALPHA
Type Specifies the data type of the pixel data. Must be one the following:
  • GL_UNSIGNED_BYTE
  • GL_BYTE
  • GL_BITMAP
  • GL_UNSIGNED_SHORT
  • GL_SHORT
  • GL_UNSIGNED_INT
  • GL_INT
  • GL_FLOAT
Pixels Returns the pixel data.

Description

The glReadPixels subroutine returns pixel data from the frame buffer, starting with the pixel whose lower left corner is at location (X, Y), and puts it into client memory starting at the location specified by the Pixels parameter. Several parameters control the processing of the pixel data before it is placed into client memory. These parameters are set with three subroutines: glPixelStore, glPixelTransfer, and glPixelMap. The effects on glReadPixels of most, but not all, of the parameters specified by these three subroutines are described here.

The glReadPixels subroutine returns values from each pixel with the lower left-hand 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.

The Format parameter specifies the format for the returned pixel values. Accepted values for Format are as follows:

GL_COLOR_INDEX Color indexes are read from the color buffer selected by the glReadBuffer subroutine. Each index is converted to fixed-point format, shifted left or right depending on the value and sign of GL_INDEX_SHIFT, and added to GL_INDEX_OFFSET. If GL_MAP_COLOR is GL_TRUE, indexes are replaced by their mappings in the table GL_PIXEL_MAP_I_TO_I.
GL_STENCIL_INDEX Stencil values are read from the stencil buffer. Each index is converted to fixed-point format, shifted left or right depending on the value and sign of GL_INDEX_SHIFT, and added to GL_INDEX_OFFSET. If GL_MAP_STENCIL is GL_TRUE, indexes are replaced by their mappings in the table GL_PIXEL_MAP_S_TO_S.
GL_DEPTH_COMPONENT Depth values are read from the depth buffer. Each component is converted to floating-point format such that the minimum depth value maps to 0.0 and the maximum value maps to 1.0. Each component is then multiplied by GL_DEPTH_SCALE, added to GL_DEPTH_BIAS, and finally clamped to the range [0,1].
GL_ABGR_EXT Each pixel is a four-component group: for GL_RGBA, the red component is first, followed by green, followed by blue, followed by alpha; for GL_ABGR_EXT the order is alpha, blue, green, and then red. Floating-point values are converted directly to an internal floatingpoint format with unspecified precision. Signed integer values are mapped linearly to the internal floating-point format such that the most positive representable integer value maps to 1.0, and the most negative representable value maps to -1.0. Unsigned integer data is mapped similarly: the largest integer value maps to 1.0, and zero maps to 0.0. The resulting floating-point color values are then multiplied by GL_c_SCALE and added to GL_c_BIAS, where c is RED, GREEN, BLUE, and ALPHA for the respective color components. The results are clamped to the range [0,1].
GL_RED
GL_GREEN
GL_BLUE
GL_ALPHA
GL_RGB
GL_RGBA
GL_LUMINANCE
GL_LUMINANCE_ALPHA Processing differs depending on whether color buffers store color indexes or red, green, blue, alpha (RGBA) color components. If color indexes are stored, they are read from the color buffer selected by glReadBuffer. Each index is converted to fixed-point format, shifted left or right depending on the value and sign of GL_INDEX_SHIFT, and added to GL_INDEX_OFFSET. Indexes are then replaced by the RGBA values obtained by indexing the GL_PIXEL_MAP_I_TO_R, GL_PIXEL_MAP_I_TO_G, GL_PIXEL_MAP_I_TO_B, and GL_PIXEL_MAP_I_TO_A tables.

If RGBA color components are stored in the color buffers, they are read from the color buffer selected by glReadBuffer. Each color component is converted to floating-point format such that zero intensity maps to 0.0 and full intensity maps to 1.0. Each component is then multiplied by GL_c_SCALE and added to GL_c_BIAS, where c is GL_RED, GL_GREEN, GL_BLUE, and GL_ALPHA. Each component is clamped to the range [0,1]. Finally, if GL_MAP_COLOR is GL_TRUE, each color component c is replaced by its mapping in the table GL_PIXEL_MAP_c_TO_c, where c again is GL_RED, GL_GREEN, GL_BLUE, and GL_ALPHA. Each component is scaled to the size its corresponding table before the lookup is performed.

Finally, unneeded data is discarded. For example, GL_RED discards the green, blue, and alpha 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 shift, scale, bias, and lookup factors described in the preceding section are all specified by glPixelTransfer. The lookup table contents themselves are specified by the glPixelMap subroutine.

The final step involves converting the indexes or components to the proper format, as specified by the Type parameter. If the Format parameter is GL_COLOR_INDEX or GL_STENCIL_INDEX and Type is not GL_FLOAT, each index is masked with the mask value given in the following table. If the Type parameter is GL_FLOAT, each integer index is converted to single-precision floating-point format.

If the Format parameter is GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA, GL_RGB, GL_RGBA, GL_ABGR_EXT, GL_LUMINANCE, or GL_LUMINANCE_ALPHA and the Type parameter is not GL_FLOAT, each component is multiplied by the multiplier shown in the following table. If Type is GL_FLOAT, 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 28 -1 (28 -1)c
GL_BYTE 27 -1 [(27 -1)c-1]/2
GL_BITMAP 1 1
GL_UNSIGNED_SHORT 216 -1 (216 -1)c
GL_SHORT 215 -1 [(215 -1)c-1]/2
GL_UNSIGNED_INT 232 -1 (232 -1)c
GL_INT 231 -1 [(231 -1)c-1]/2
GL_FLOAT none c

Return values are placed in memory as follows. If the Format parameter is GL_COLOR_INDEX, GL_STENCIL_INDEX, GL_DEPTH_COMPONENT, GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA, or GL_LUMINANCE, 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, GL_RGBA and GL_ABGR_EXT return four values, and GL_LUMINANCE_ALPHA returns two values for each pixel, with all values corresponding to a single pixel occupying contiguous space in Pixels. Storage parameters set by glPixelStore, such as GL_PACK_SWAP_BYTES and GL_PACK_LSB_FIRST, affect the way that data is written into memory. See the glPixelStore subroutine for a description.

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

Format of GL_ABGR_EXT is part of the _extname (EXT_abgr) extension, not part of the core GL command set.

Error Codes

GL_INVALID_ENUM Format or Type is not an accepted value.
GL_INVALID_VALUE Width or Height is negative.
GL_INVALID_OPERATION Format is GL_COLOR_INDEX and the color buffers store RGBA color components.
GL_INVALID_OPERATION Format is GL_STENCIL_INDEX and there is no stencil buffer.
GL_INVALID_OPERATION Format is GL_DEPTH_COMPONENT and there is no depth buffer.
GL_INVALID_OPERATION The glReadPixels subroutine is called between a call to glBegin and the corresponding call to glEnd.

Associated Gets

Associated gets for the glReadPixels subroutine are as follows. (See the glGet subroutine for more information.)

glGet with argument GL_INDEX_MODE.

Files

/usr/include/GL/gl.h Contains C language constants, variable type definitions, and ANSI function prototypes for OpenGL.

Related Information

The glBegin or glEnd subroutine, glCopyPixels subroutine, glDrawPixels subroutine, glPixelMap subroutine, glPixelStore subroutine, glPixelTransfer subroutine, glReadBuffer subroutine.

OpenGL Overview.


[ Previous | Next | Contents | Glossary | Home | Search ]