raster displays

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

Upload: trish

Post on 14-Jan-2016

71 views

Category:

Documents


1 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Raster Displays

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.

Page 2: Raster Displays

Scan Conversion (Rasterization)

• Determines individual pixel values.

• Rasterization in the GL graphics pipeline:

Page 3: Raster Displays

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.

Page 4: Raster Displays

Manipulating Pixmaps

• Pixmaps may be stored in regular memory or in the frame buffer (off-screen or on-screen).

• Rendering operations that draw into the frame buffer change the particular pixmap that is visible on the display.

Page 5: Raster Displays

Pixmap Operations: Copying

Page 6: Raster Displays

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.

Page 7: Raster Displays

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.

Page 8: Raster Displays

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.

Page 9: Raster Displays

Example

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

Page 10: Raster Displays

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

Page 11: Raster Displays

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 rules.– 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

Page 12: Raster Displays

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

Page 13: Raster Displays

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.

Page 14: Raster Displays

Pixmap Data Types (3)

• Default RGBpixmap constructor: make empty pixmap.

• Constructor creates a pixmap with r rows and c columns.

• 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);

Page 15: Raster Displays

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

Page 16: Raster Displays

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 book’s companion website.

Page 17: Raster Displays

Pixmap Application

• Fig. 9.4 shows code for an application to use pixmaps controlled by mouse and keyboard.– 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.

Page 18: Raster Displays

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

– Right mouse click clears screen.

• The SDL can use RGBpixmaps.

Page 19: Raster Displays

Examples

• Writing and scaling BMP text to the screen: 4 x 6, 6 x 8, 8 x 12, 12 x 16

Page 20: Raster Displays

Examples (2)

• Window scrolling: blank line replaces bottom, moving all lines up one.

Page 21: Raster Displays

Examples (3)

• Redrawing screen after menu use:

Page 22: Raster Displays

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

Page 23: Raster Displays

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.

Page 24: Raster Displays

Rotating Images

• Pixmaps may rotated by any amount.

• Rotating through 90o, 180o, 270o is simple: create a new pixmap and copy pixels from the original to the appropriate spot in the new pixmap.

Page 25: Raster Displays

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.

Page 26: Raster Displays

Rotating Images (2)

Page 27: Raster Displays

Combining Pixmaps

• Pixmaps are usually combined pixel by pixel (using corresponding pixels).– Average 2 images: add the corresponding

pixels and divide result by 2 ([A+B]/2)– Image differences: Subtract pixel in second

image from corresponding pixel in first ([A-B]/2)

– Boolean image: pixel = 1 if first image brighter than second, 0 else: A > B

Page 28: Raster Displays

Combining Pixmaps (2)

• Weighted average: (1 – f)*A + f*B for some fraction f < 1.0.– Allows "dissolving" one image into another as

f is varied from 0 to 1.

Page 29: Raster Displays

Read-Modify-Write

• When pixmap C is formed by combining pixmaps A and B, it may be stored in a new pixmap C or in one of the original pixmaps.– C = A operation B or A = A operation B

• OpenGL supplies an efficient operation for this when A is the frame buffer.

Page 30: Raster Displays

Alpha Channel and Image Blending

• Blending allows you to draw a partially transparent image over another image.

• We use the alpha value, the fourth component in specifying colors.– Alpha may have values in the range [0, 255].– 0 represents total transparency and 255

represents total opacity.– It is usually used as α/255, to give a value in

[0.0, 1.0].

Page 31: Raster Displays

Alpha Channel and Image Blending (2)

• Code: struct RGBA { public: unsigned char r, g, b, a; };

• To blend 2 images, let a be the alpha value in A and use B = aA + (1- a)B separately for each color (R, G, B).

Page 32: Raster Displays

Example

• Dragon has a = 255; mask has a = 128; rest have a = 0.

Page 33: Raster Displays

Code for Blending

• RGBpixmap must be extended to RGBApixmap.

• Then use B.draw(); A.blend();

Page 34: Raster Displays

Tools for Setting and Using the Alpha Channel (2)

• glBlendFunc may have arguments other than GL_SRC_ALPHA and GL_ONE_MINUS_SRC_ALPHA.– DST (destination) can replace SRC (source)– GL_ZERO and GL_ONE may also be used– using GL_DST_ALPHA and GL_ZERO

multiplies each color component of a source pixel by the level of the corresponding component in the destination.

Page 35: Raster Displays

Example: Cursor Viewing

Page 36: Raster Displays

Tools for Setting and Using the Alpha Channel

• glColor4f (r, g, b, a); a ranges from 0.0 (total transparency) to 1.0 (total opacity)– Fourth value (a) assigns an alpha value to all

subsequently defined vertices.

• Quadruples refl = [r, g, b, a] may be used in glMaterialfv (GL_FRONT, GL_DIFFUSE, refl) to make materials partially transparent.

Page 37: Raster Displays

Logical Combinations of Pixmaps

• Logical operations on pixmaps are used to combine pixmaps in various ways.

• glEnable(GL_COLOR_LOGIC_OP);

• glLogicOp (operator);

• Operator values are on the next slide.

Page 38: Raster Displays

OpenGL Logical Operators

Page 39: Raster Displays

Effects of Logical Operations

• Left to right and top to bottom:

• A; B; • A or B;• A xor B;• A nand B;• A and B.

Page 40: Raster Displays

Application: Rubberbanding Lines and Rectangles

• Rubber-banding allows a user to draw a line or rectangle with the mouse and be able to see intermediate versions.

• The user may adjust the line or rectangle to where it really should be.

• The line or (one) corner of the rectangle is attached to a fixed point, called the pivot.

Page 41: Raster Displays

Application: Rubberbanding Lines and Rectangles (2)

• The rubber-banded figures must be continually erased and redrawn

• XOR drawing draws the line or rectangle the first time it is invoked, but if you draw again in XOR mode, the line is erased.

Page 42: Raster Displays

Application: Rubber-banding Lines and Rectangles (3)

Page 43: Raster Displays

Application: Rubberbanding Lines and Rectangles (4)

• Code

Page 44: Raster Displays

Application: Rubberbanding Lines and Rectangles (5)

• Code (continued)

Page 45: Raster Displays

BitBlt Operation

• BitBlt (bit block-transfer) is a hardware operation which combines the draw, read, and copy operations.– Simplest form: source rectangle of pixels

copied to destination rectangle of same size, either in memory or on the screen, using some combination operation between the pixels if desired: D = D operation S.

– The destination is clipped to the BitBlt's clipping rectangle.