Sei sulla pagina 1di 26

Computer Graphics using OpenGL,

F. S. Hill, Jr. and S. Kelley
Chapter 9
Tools for Raster Displays

S. M. Lea
University of North Carolina at Greensboro
2007, Prentice Hall

Raster Displays
Images are composed of arrays of pixels
displayed on a raster device.
Two main ways to create images:
Scan and digitize an existing image.
Compute a value for each pixel procedurally.
The result may be stored as a pixmap, a
rectangular array of color values.

Scan Conversion (Rasterization)

Determines individual pixel values.
Rasterization in the GL graphics pipeline:

Scan Conversion (2)
Graphics pipeline actually produces
fragments: a color, a depth, and a texture
coordinate pair for each vertex.
A number of processing steps and tests are
performed on the fragments before they are
written to the screen.
We can also perform operations on
images on the screen using the fragment
operations portion of the pipeline.
Manipulating Pixmaps
Pixmaps may be stored in regular memory
or in the frame buffer (off-screen or on-
Rendering operations that draw into the
frame buffer change the particular pixmap
that is visible on the display.
Pixmap Operations: Copying
Pixmap Operations: Copying
glReadPixels () reads a portion of the
frame buffer into memory.
glCopyPixels() copies a region in one part
of the frame buffer into another region of
the frame buffer.
glDrawPixels() draws a given pixmap into
the frame buffer.
We can also copy a pixmap from memory
to memory.

Scaling Pixmaps
glPixelZoom(float sx, float sy);
Sets scale factors in x and y
Any floating point values are allowed for sx
and sy, even negative ones. The default
values are 1.0.
The scaling takes place about the current
raster position, pt.
Scale factors are applied to the image
drawn from a pixmap, not to the pixmap.

Scaling Pixmaps (2)
Roughly, the pixel in row r and column c of
the pixmap will be drawn as a rectangle of
width sx and height sy screen pixels, with
lower left corner at screen pixel (pt.x + sx *
r, pt.y + sy * c).
More precisely, any screen pixels whose
centers lie in this rectangle are drawn in
the color of this pixmap pixel.
Six scaled versions of the mandrill (scale factors
sx = -1.5, -1.0, -0.5, 1.5, 1.0, 0.5.)
To produce each image the new value of sx was
set, and glPixelZoom(sx, 1); glutPostRedisplay();
was executed.
Pixmap Operations (3)
We may rotate a pixmap.
We may compare two pixmaps e.g., to
detect tumors
We can describe regions within a pixmap
as circles, squares, and so on.
We may fill the interior of a region with a
Pixmap Data Types
A pixmap has a certain number of rows
and columns, and each pixel in the array
has its color stored according to certain
Bitmap: pixel = bit, 0 or 1 (black or white)
Gray-scale bitmap: pixel = byte, representing
gray levels from 0 (black) through 255 (white)
Pixel contains an index into a lookup table
(LUT); usually index is a byte
Pixmap Data Types (2)
RGB pixmap contains 3 bytes, one each for
red, green, and blue
RGBA pixmap contains 4 bytes, one each for
red, green, blue, and alpha (transparency)
Code: start by defining a color:
struct RGB { public: unsigned char r, g, b; };
// Holds one color triple

Pixmap Data Types (3)
OpenGL represents a pixmap as an array
pixel of pixel values stored row by row
from bottom to top and across each row
from left to right.
RGBpixmap class uses this storage
mechanism as well (code in Fig. 9.3).
Code uses GL functions to implement
class functions.
Pixmap Data Types (3)
Default RGBpixmap constructor: make empty
Constructor creates a pixmap with r rows and c
setPixel() sets a specific pixel value.
getPixel() reads a specific pixel value.
draw() copies pixmap to frame buffer, placing
lower left corner at current raster position.
set current raster position using glRasterPos2i (x, y);
Pixmap Data Types (4)
read() copies from frame buffer to memory
Lower left corner is at (x,y), and wid and ht
specify the size to be read.
copy() does a read followed by a draw,
without creating an intermediate pixmap
The region with lower left corner at (x,y), wid
by ht in size, is copied to the region whose
lower left corner is at the current raster

Pixmap Data Types (5)
readBMPFile() reads an image stored as a
(24-bit) BMP file into the pixmap,
allocating storage as necessary.
writeBMPFile() creates a (24-bit) BMP file
that contains the pixmap.
Code for both of these functions is given
online at the books companion website.
Pixmap Application
Fig. 9.4 shows code for an application to
use pixmaps controlled by mouse and
BMP files are copied into 2 pixmaps.
One image is displayed at the initial raster
position; a left mouse click draws it again at
the current mouse position.
Pixmap Application (2)
Pressing s toggles between the 2 images.
Pressing r reads a 200 x 200 area of the
screen and replaces the first pixmap by these
Right mouse click clears screen.
The SDL can use RGBpixmaps. See the
companion website for code.
Writing and scaling BMP text to the
screen: 4 x 6, 6 x 8, 8 x 12, 12 x 16

Examples (2)
Window scrolling: blank line replaces
bottom, moving all lines up one.

Scaling Pixmaps
Scale by s: output has s times as many
pixels in x and in y as input; if s is an
integer, scale with pixel replication to
enlarge. 6 x 8 to 12 x 16

Scaling Images (2)
To reduce by, for example, 1/3: take every
third row and column of the pixmap.
This method is usually not satisfactory.
Instead, we should average the values of the
9 pixels in each non-overlapping 3 x 3 array,
and use that value for the pixel.
Rotating Images
Pixmaps may rotated by any amount.
Rotating through 90
, 180
, 270
is simple:
create a new pixmap and copy pixels from
the original to the appropriate spot in the
new pixmap.

Rotating Images
Other rotations are difficult. Simplest
approach: pixel in transformed image is
set to color of pixel it was transformed
from in original.
This usually leads to bad results. You
really should average overall pixels which
transform (in part) to this pixel.
Rotating Images (2)