Reading, Writing, and Copying Pixel Data
OpenGL provides three basic commands that manipulate image data:
glReadPixels() - Reads a rectangular array of pixels from the framebuffer and stores the data
in processor memory.
glDrawPixels() - Writes a rectangular array of pixels from data kept in processor memory
into the framebuffer at the current raster position specified by glRasterPos*().
glCopyPixels() - Copies a rectangular array of pixels from one part of the framebuffer to
another. This command behaves similarly to a call to glReadPixels() followed by a call to
glDrawPixels(), but the data is never written into processor memory.
For the aforementioned commands, the order of pixel data processing operations is shown in Figure
8-3:
Figure 8-3 : Simplistic Diagram of Pixel Data Flow
The basic ideas in Figure 8-3 are correct. The coordinates of glRasterPos*(), which specify the
current raster position used by glDrawPixels() and glCopyPixels(), are transformed by the
geometric processing pipeline. Both glDrawPixels() and glCopyPixels() are affected by
rasterization and per-fragment operations. (But when drawing or copying a pixel rectangle, there’s
almost never a reason to have fog or texture enabled.)
However, additional steps arise because there are many kinds of framebuffer data, many ways to
store pixel information in computer memory, and various data conversions that can be performed
during the reading, writing, and copying operations. These possibilities translate to many different
modes of operation. If all your program does is copy images on the screen or read them into
memory temporarily so that they can be copied out later, you can ignore most of these modes.
However, if you want your program to modify the data while it’s in memory - for example, if you
have an image stored in one format but the window requires a different format - or if you want to
save image data to a file for future restoration in another session or on another kind of machine with
significantly different graphical capabilities, you have to understand the various modes.
The rest of this section describes the basic commands in detail. The following sections discuss the
details of the series of imaging operations that comprise the Imaging Pipeline: pixel-storage modes,
pixel-transfer operations, and pixel-mapping operations.
Reading Pixel Data from Frame Buffer to Processor Memory
void glReadPixels(GLint x, GLint y, GLsizei width, GLsizei height,
GLenum format, GLenum type, GLvoid *pixels);
Reads pixel data from the framebuffer rectangle whose lower-left corner is at (x, y) and
whose dimensions are width and height and stores it in the array pointed to by pixels. format
indicates the kind of pixel data elements that are read (an index value or an R, G, B, or A
component value, as listed in Table 8-1), and type indicates the data type of each element (see
Table 8-2).
If you are using glReadPixels() to obtain RGBA or color-index information, you may need to
clarify which buffer you are trying to access. For example, if you have a double-buffered window,
you need to specify whether you are reading data from the front buffer or back buffer. To control
the current read source buffer, call glReadBuffer().
Table 8-1 : Pixel Formats for glReadPixels() or glDrawPixels()
format Constant Pixel Format
GL_COLOR_INDEX A single color index
GL_RGB A red color component, followed by a green color component,
followed by a blue color component
GL_RGBA A red color component, followed by a green color component,
followed by a blue color component, followed by an alpha
color component
GL_RED A single red color component
GL_GREEN A single green color component
GL_BLUE A single blue color component
GL_ALPHA A single alpha color component
GL_LUMINANCE A single luminance component
GL_LUMINANCE_ALPHA A luminance component followed by an alpha color
component
GL_STENCIL_INDEX A single stencil index
GL_DEPTH_COMPONENT A single depth component
Table 8-2 : Data Types for