Manipulating Pixm aps CS 4 7 3 1 : Com put e r Gr a phics Pixm ap = rectangular array of num erical values � Le ct ur e 2 1 : Ra st e r Gr a phics Pa r t 2 Pixm ap copied to fram e buffer = rendered � Change fram e buffer entry = onscreen picture changes � Em m anuel Agu � Each pixel location has fixed num ber of bits (color depth) � Exam ple: if color depth is b bits, can store up to 2 b values Manipulating Pixm aps Manipulating Pixm aps � Operations of interest: � Data types for pixm aps � Copying pixm aps � Bitm ap: 1 bit, on or off • glReadPixels: frame buffer to off-screen memory � Gray scale: one byt e, values 0 - 255 • glCopyPixels: frame buffer to frame buffer � RGB: 3 byt es ( red, green, blue) • glDrawPixels: pixmap to frame buffer � RGBA: 4 byte ( red, green, blue, alpha) • memCopy: off-screen to off-screen � Declaration of RGB triple: � Com paring pixm aps � Represent ing and coloring regions in pixm ap class RGB{ public: unsigned char r, g, b; }; 1
RGBpixm ap Class RGBpixm ap Class OpenGL convention: pixm ap (bottom to top, left to right) / / … .. contd. � Add draw, read and copy m ethods (which use openGL) � void copy( ){ glCopyPixels(.. Parameters..); Class RGB{ public: unsigned char r, g, b; int readBMPFile(char *fname); void writeBMPFile(char *fname); RGBpixmap( ); // constructor }; void setPixel(int x, int y, RGB color); RGB getPixel(int x, y); void draw( ){ glDrawPixels(nCols, nRows, GL_RGB, Note: refer to Hill fig. 10.3 for full RGBPixm ap declaration GL_UNSIGNED_BYTE, pixel); void read( ){glReadPixels(x, y, nCols, nRows, GL_RGB, GL_UNSIGNED_BYTE, pixel); Scaling and Rotating I m ages Scaling and Rotating I m ages � glPixelZoom(float sx, float sy) � Scaling: want a pixm ap that has s tim es m ore pixels in x, y � s > 1: enlargem ent � Sets scale factors for subsequent glDrawPixels com m and � s < 1: reduct ion ( inform at ion is lost ! ) � Scaling is about current raster position, pt. � Pixel row r and colum n c of pixm ap � Drawn as rectangle with bottom left current screen coordinates � Draws (pt.x + sx * r, pt.y + sy.c) Original Original � 90, 180 and 270 degree rotations: Pixm ap Pixm ap s > 1 s < 1 elem ent � Copy one pixm ap to another doing m atrix transposes elem ent � General rotations: � openGL scaling: � affine transform of pixm ap points to get new pixm ap � glPixelZoom(float sx, float sy) � Sets scale factors for drawing pixm aps � Note: pixmaps not scaled, pictures drawn are scaled 2
Com bining Pixm aps Com bining Pixm aps Two pixmaps A and B combined pixelwise to form third Generalized weighting: � � pixel C � C[ i] [ j ] = ( 1- f).A[ i] [ j] + f.B[ i] [ j] i.e. C[ i] [ j] = A[ i] [ j] ⊗ B[ i] [ j] � Exam ple: � � Averaging: � A = (14, 246, 97), B = (82, 12, 190), f = 0.2 � C[ i] [ j] = ½ * (A[ i] [ j] + B[ i] [ j] ) � C = (27, 199, 115) = 0.8 A + 0.2 B � Subtraction: � Question: How to dissolve image A into B? � C[ i] [ j ] = A[ i] [ j ] - B[ i] [ j] � Generalized weighting: � C[ i] [ j ] = ( 1- f).A[ i] [ j] + f.B[ i] [ j] Alpha Channel and I m age Blending Alpha Channel and I m age Blending � Even m ore generalized weighting = blending/ com positing � Alpha channel: series of alpha values in a pixm ap � Blending: � openGL alpha blending: glBlendFunc( ) � draw part ially t ransparent im age over anot her � Other alpha blending applications: � Add 4 th com ponent , alpha value ( A) t o RGB � Sim ulat ing Chr om ak ey: forcing cert ain colors t o be � I nt erpret at ion: alpha specifies how opaque each pixel is transparent � Applying paint wit h a paint brush: apply percent age of new � Transparent ( A = 0) , Tot al opacit y ( A = 255) color wit h each m ouse st roke � Alpha m ost frequent ly used in scaling colors class RGB{ public: unsigned char r, g, b,a; }; 3
References Hill, chapter 10 � 4
Recommend
More recommend