glReadPixels

The glReadPixels function reads a block of pixels from the framebuffer.

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

Parameters

  • x, y
    The window coordinates of the first pixel that is read from the framebuffer. This location is the lower-left corner of a rectangular block of pixels.

  • width, height
    The dimensions of the pixel rectangle. The width and height parameters of one correspond to a single pixel.

  • format
    The format of the pixel data. The following symbolic values are accepted:

    • GL_COLOR_INDEX
      Color indexes are read from the color buffer selected by glReadBuffer. Each index is converted to fixed point, 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, 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 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_RED
      GL_GREEN
      GL_BLUE
      GL_ALPHA
      GL_RGB
      GL_RGBA
      GL_BGR_EXT
      GL_BGRA_EXT
      GL_LUMINANCE
      GL_LUMINANCE_ALPHA
      Processing differs depending on whether color buffers store color indexes or 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, 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 red, green, blue, and alpha 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 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 of 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.

  • type
    The data type of the pixel data. Must be one of the following values.

    Type Index mask Component conversion
    GL_UNSIGNED_BYTE 281 (281)c
    GL_BYTE 271 [271]c-1]/2
    GL_BITMAP 1 1
    GL_UNSIGNED_SHORT 2161 (2161) c
    GL_SHORT 2151 [(2151) c1] / 2
    GL_UNSIGNED_INT 2321 (2321) c
    GL_INT 2311 [(2311) c1] / 2
    GL_FLOAT none c
  • pixels
    Returns the pixel data.

Remarks

The glReadPixels function returns pixel data from the framebuffer, starting with the pixel whose lower-left corner is at location (xy), into client memory starting at location pixels. Several parameters control the processing of the pixel data before it is placed into client memory. These parameters are set with three commands: glPixelStore, glPixelTransfer, and glPixelMap. This topic describes the effects on glReadPixels of most, but not all of the parameters specified by these three commands.

The glReadPixels function returns values from each pixel with lower-left corner at (x + i, *y *+ j) for 0 &le; i < width and 0 &le; 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 shift, scale, bias, and lookup factors described above are all specified by glPixelTransfer. The lookup table contents are specified by glPixelMap.

The final step involves converting the indexes or components to the proper format, as specified by type. If format 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 type is GL_FLOAT, then each integer index is converted to single-precision floating-point format.

If format is GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA, GL_RGB, GL_RGBA, GL_BGR_EXT, GL_BGRA_EXT, GL_LUMINANCE, or GL_LUMINANCE_ALPHA and type is not GL_FLOAT, each component is multiplied by the multiplier shown in the preceding 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 OpenGL).

Return values are placed in memory as follows. If format 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 and GL_BGR_EXT return three values, GL_RGBA and GL_BGRA_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 glPixelStore for a description.

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

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

The following function retrieves information related to glReadPixels:

glGet with argument GL_INDEX_MODE

Error Codes

The following are the error codes generated and their conditions.

Error code Condition
GL_INVALID_ENUM format or type was not an accepted value.
GL_INVALID_VALUE either width or height was negative.
GL_INVALID_OPERATION format was GL_COLOR_INDEX and the color buffers stored RGBA or BGRA color components.
GL_INVALID_OPERATION format was GL_STENCIL_INDEX and there was no stencil buffer.
GL_INVALID_OPERATION format was GL_DEPTH_COMPONENT and there was no depth buffer.
GL_INVALID_OPERATION glReadPixels was called between a call to glBegin and the corresponding call to glEnd.

Requirements

**  Windows NT/2000:** Requires Windows NT 3.5 or later.
**  Windows 95/98:** Requires Windows 95 or later. Available as a redistributable for Windows 95.
**  Header:** Declared in Gl.h.
**  Library:** Use Opengl32.lib.

See Also

glBegin, glCopyPixels, glDrawPixels, glEnd, glPixelMap, glPixelStore, glPixelTransfer, glReadBuffer