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 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 reference page describes the effects on glReadPixels of most, but not all of the parameters specified by these three commands.When the GL_ARB_imaging extension is supported, the pixel data may be processed by additional operations including color table lookup, color matrix transformations, convolutions, histograms and minimum and maximun pixel value computations.
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 $i$th pixel in the $j$th row. Pixels are returned in row order from the lowest to the highest row, left to right in each row.
format specifies the for the returned pixel values; accepted values are:
GL_COLOR_INDEX
Color indices 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, indices 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, indices 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 and the maximum value maps to 1. Each component is then multiplied by GL_DEPTH_SCALE, added to GL_DEPTH_BIAS, and finally clamped to the range [0,1].
GL_LUMINANCE_ALPHA
Processing differs depending on whether color buffers store color indices or RGBA color components. If color indices 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. Indices are then replaced by the red, green, blue, and alpha values obtained by indexing the tables 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. Each table must be of size $2 sup n$, but $n$ may be different for different tables. Before an index is used to look up a value in a table of size $2 sup n$, it must be masked against $2 sup n - 1$.
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 RED, GREEN, BLUE, or ALPHA. Finally, if GL_MAP_COLOR is GL_TRUE, each component is clamped to the range [0, 1], scaled to the size of its corresponding table, and is then replaced by its mapping in the table GL_PIXEL_MAP_c_TO_c, where c is R, G, B, or A.
Unneeded data is then 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 final values are clamped to the range [0, 1].
The shift, scale, bias, and lookup factors just described are all specified by
glPixelTransfer. The lookup table contents themselves are specified by glPixelMap.
Finally, the indices or components are converted to the proper , 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 .
If format is GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA, GL_RGB, GL_BGR, GL_RGBA, GL_BGRA, GL_LUMINANCE, or GL_LUMINANCE_ALPHA 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 if it is different from the one used by the GL).
center tab(:); l cb cb l c c. _
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_BITMAP:$1$:$1$ GL_UNSIGNED_SHORT:$2"^"16 - 1$:$(2"^"16 - 1) c$ GL_SHORT:$2"^"15 - 1$:$[(2"^"16 - 1) c - 1] / 2$ GL_UNSIGNED_INT:$2"^"32 - 1$:$(2"^"32 - 1) c$ GL_INT:$2"^"31 - 1$:$[(2"^"32 - 1) c - 1] / 2$ GL_FLOAT:none:$c$ _
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 $i$th pixel in the $j$th row is placed in location $(j)~"width"~+~i$. GL_RGB and GL_BGR return three values, GL_RGBA and GL_BGRA 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_LSB_FIRST and GL_PACK_SWAP_BYTES, affect the way that data is written into memory. See glPixelStore for a description.