Computer Graphics 8 Lee ByungGook 1242020 Computer Graphics
Computer Graphics 8 Lee Byung-Gook 12/4/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 1
Image Definition • An image is a 2 dimensional array of color intensity values • Each color value of the 2 dimensional array is called a pixel (short for pixel element) • The number of bits used to store each pixel determines the image's color depth A 10 pixel by 10 pixel image 12/4/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 2
Image Storage • bitmap : an image with one bit per pixel (0: background color, 1: foreground color; monochrome (one color) image) • gray-scale : an image with multiple bits per pixel representing various intensities of the same color (e. g. , using 8 bits, 256 shades can be represented. ) • pixmap : an image with multiple bits per pixel broken into more than one component representing various intensities of more than one color. (pixmap is short for pixel map) 12/4/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 3
Image Storage • • • RGB pixmap : a pixmap that has three values per pixel representing the percentage of red, green and blue color for that pixel. RGBA pixmap : a pixmap that has four values per pixel representing the percentage of red, green, blue, and alpha color for that pixel. (The alpha value typically represents transparency) color index image : each pixel value is an index into a table (an array) of color values 12/4/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 4
Pixel ordering • Each pixel value can be treated as a single unit or broken into its components. There are three basic options: 1. Keep all of the bits for a single pixel together 2. Keep all of the bits for a single component of a single pixel together 3. Separate all bits of a pixel into bitplanes. 12/4/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 5
Example • Given an RGB pixmap using 24 bits per pixel (8 bits for each color component), the options would look something like: • Keep all bits together red 1 green 1 01101011 11101111 blue 1 01100011 red 2 green 2 blue 2 10101011 11101111001 pixel 1 … pixel 2 • Keep all component bits together red 1 01101011 12/4/2020 red 2 10101011 green 1 … 11101111 green 2 11101111 blue 1 … 01100011 blue 2 01111001 Computer Graphics, Lee Byung-Gook, Dongseo Univ. … 6
Example • Store bit 1 of pixel 1, bit 1 of pixel 2, bit 1 of pixel 3, etc. . Then store bit 2 of pixel 1, bit 2 of pixel 2, bit 2 of pixel 3, etc. bit 1 of every pixel 01… … … bitplane 1 … bitplane 2 bit 2 of every pixel 10… … bit 3 of every pixel 11… … … bitplane 3 … bit 24 of every pixel 11… 12/4/2020 … … bitplane 24 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 7
Data Type Open. GL keyword GL_BYTE GL_UNSIGNED_BYTE GL_SHORT GL_UNSIGNED_SHORT GL_INT GL_UNSIGNED_INT GL_FLOAT GL_BITMAP GL_UNSIGNED_BYTE_3_3_2 Description signed 8 -bit integer (-128… 127) unsigned 8 -bit integer (0… 255) signed 16 -bit integer (-32768… 32767) unsigned 16 -bit integer (0… 65536) signed 32 -bit integer(-2147483648… 2147483647) unsigned 32 -bit integer (0… 4294967296) single precision floating point single bits packed into unsigned 8 -bit integers (0… 1) three values packed into a single byte; 3 bits for the 1 st value, 3 bits for the 2 nd value, and 2 bits for the 3 rd value. GL_UNSIGNED_SHORT_5_6_5 three values packed into two bytes; 5 bits for the 1 st value, 6 bits for the 2 nd value, and 5 bits for the 3 rd value. 12/4/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 8
Data Format Open. GL keyword GL_RGBA GL_RED GL_GREEN GL_BLUE GL_ALPHA GL_COLOR_INDEX GL_LUMINACE_ALPHA GL_STENCIL_INDEX GL_DEPTH_COMPONENT 12/4/2020 Description a red component, followed by a green and blue component a red component, followed by a green, blue, and alpha component a single red color component a single green color component a single blue color component a single alpha color component a single color index a single luminance component a luminance component followed by an alpha color component a single stencil index a single depth component Computer Graphics, Lee Byung-Gook, Dongseo Univ. 9
Image buffer • In C/C++, the address of a block of memory with no data type associated with it is stored in a "pointer to void" variable. • For example, to allocate a buffer for an image with 100 rows and 200 columns, where each pixel uses 24 bits (3 bytes): void *image. Buffer; int image. Width=100, image. Height=200; image. Buffer = malloc(image. Width*image. Height*3); // C image. Buffer = new(unsigned char[image. Width*image. Height *3]); // C++ 12/4/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 10
Image buffer • To access a specific pixel's values within the array, use the following array mapping formula (assuming 8 bits per component): index = (image. Width*row + column)*sizeof(pixel); red = image. Buffer[index]; green = image. Buffer[index+1]; blue = image. Buffer[index+2]; 12/4/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 11
Image commands gl. Read. Pixels() (from frame buffer to my program) My Program Image buffer within my program Screen (Frame Buffer) gl. Draw. Pixels() (from my program to the frame buffer) gl. Copy. Pixels() (from the frame buffer to the frame buffer) 12/4/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 12
Image commands • gl. Read. Pixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid *pixels); Screen (Frame Buffer) width Desired area to read image from. height (x, y) - in window coordinates (0, 0) - in window coordinates 12/4/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 13
Image commands • gl. Draw. Pixels(GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid *pixels); • gl. Raster. Pos{234}{sifd}(x, y, z, w); gl. Raster. Pos{234}{sifd}v(array); • gl. Copy. Pixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum which. Buffer); which. Buffer : GL_COLOR, GL_DEPTH, GL_STENCIL. 12/4/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 14
Lab 15 • Downloading void my. Mouse(int button, int state, int x, int y) { if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { gl. Raster. Pos 2 i(x, window. Height-y); gl. Draw. Pixels(my. Image. Width, my. Image. Height, GL_RGB, GL_UNSIGNED_BYTE, my. Image. Pixels); } else if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) { gl. Raster. Pos 2 i(x, window. Height-y); gl. Draw. Pixels(buffer. Width, buffer. Height, GL_RGB, GL_UNSIGNED_BYTE, image. Buffer); } else if(button == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) { gl. Read. Pixels(x, window. Height-y, buffer. Width, buffer. Height, GL_RGB, GL_UNSIGNED_BYTE, image. Buffer); } } 12/4/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 15
Manipulating images • • • Reflect about an axis Increase intensity (brighten) Decrease intensity (darken) Redistribute the color intensities Edge detection etc. , etc. 12/4/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 16
Scaling images • Scaling an image changes its size. There are two problems: • There is usually not a one-to-one mapping from the original image to the new image (e. g. , the original image is 10 pixels wide and the new image is 14 pixels wide). • How should the color of each pixel of the new scaled image be calculated? 12/4/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 17
The original 10 x 10 image in black The new 14 x 14 image in red. The two images supperimposed on each other Note the non-alignment 12/4/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 18
• For example, take the pixel (8, 6) in the 14 x 14 image and map it into the 10 x 10 image. You get: x. Scale = 10 / 14 = 0. 71 y. Scale = 10 / 14 = 0. 71 x. Map = 8 * 0. 71 = 5. 714 y. Map = 6 * 0. 71 = 4. 286 • What color should be assigned to the pixel location (8, 6) in the new image? We have two choices: 12/4/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 19
• Select the closest pixel to the mapped location in the original image. (Round the floating point values to their nearest integer values). GL_NEAREST • Calculate an average color value using an appropriate percentage of each of the surrounding pixel values. GL_LINEAR (5, 5) 0. 714 (6, 5) (0. 714*0. 286) = 0. 2 (0. 714*0. 714) = 0. 51 (5. 714, 4. 286) 0. 286 (5, 4) 0. 714 (0. 714*0. 286) = 0. 2 12/4/2020 0. 286 (6, 4) (0. 286*0. 286) = 0. 08 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 20
Combining images • Combining one image with another image is often calling compositing • Interesting effects can be obtained by – using different methods of composting – varying the amount of compositing over time • There is two general methods for compositing – use a percentage of each pixel from each image – use bitwise operators on the color values 12/4/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 21
Blending • This combines the pixels in one image with the pixels from another image. • Using Open. GL, this is done by drawing the first image into the frame buffer (color buffer) and then blending the second image with the first while drawing it into the frame buffer. 12/4/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 22
Blending • • • gl. Enable(GL_BLEND); // Draw all of the source and zero of the destination // destination = 1. 0*source + 0. 0*destination gl. Blend. Func(GL_ONE, GL_ZERO); gl. Raster. Pos*(…); // set the location of the images gl. Draw. Pixels(…); // draw the first image // Blend the second image using the alpha values // of the source image as percentages // destination = source*source. Alpha+ destination*(1. 0 -source. Alpha) gl. Blend. Func(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); gl. Draw. Pixels(…); // draw the second image 12/4/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 23
gl. Blend. Func • • void gl. Blend. Func( GLenum sfactor, GLenum dfactor ); determines the scale factors used on the source and destination pixels before they are combined sfactor : specifies how the red, green, blue, and alpha source-blending factors are computed. Nine symbolic constants are accepted: GL_ZERO, GL_ONE, GL_DST_COLOR, GL_ONE_MINUS_DST_COLOR, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA, and GL_SRC_ALPHA_SATURATE. dfactor : specifies how the red, green, blue, and alpha destinationblending factors are computed. Eight symbolic constants are accepted: GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA, and GL_ONE_MINUS_DST_ALPHA. 12/4/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 24
Bitwise logic operations on pixels • Open. GL supports the following bitwise operations while writing fragments to the frame buffer. Again, the source is the pixel to be written to the frame buffer, and the destination is the pixel already in the frame buffer. • The table describes the operations using C/C++ bitwise operators & (and), | (or), ^ (exclusive or), ~ (not). 12/4/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 25
Bitwise logic operations Open. GL logic keyword GL_CLEAR GL_COPY GL_NOOP GL_SET GL_AND GL_OR GL_NAND GL_NOR GL_XOR GL_EQUIV GL_INVERT GL_COPY_INVERTED GL_AND_REVERSE GL_AND_INVERTED GL_OR_REVERSE GL_OR_INVERTED 12/4/2020 Description 0 source destination 1 source & destination source | destination ~(source & destination) ~(source | destination) source ^ destination ~(source ^ destination) ~destination ~source & destination source | ~destination ~source | destination Computer Graphics, Lee Byung-Gook, Dongseo Univ. 26
Lab 16 • downloading void my. Mouse(int button, int state, int x, int y) { if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { if(x < buffer. Width && y > buffer. Height) first=0; else if(x > buffer. Width && y > buffer. Height) first=1; else if(x < buffer. Width && y < buffer. Height) first=2; gl. Enable(GL_COLOR_LOGIC_OP); gl. Logic. Op(GL_COPY); gl. Raster. Pos 2 i(buffer. Width, buffer. Height); gl. Draw. Pixels(my. Image[first]. Width, my. Image[first]. Height, GL_RGB, GL_UNSIGNED_BYTE, my. Image[first]. Pixels); gl. Disable(GL_COLOR_LOGIC_OP); } 12/4/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 27
Lab 16 else if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) { if(x < buffer. Width && y > buffer. Height) second=0; else if(x > buffer. Width && y > buffer. Height) second=1; else if(x < buffer. Width && y < buffer. Height) second=2; if(sign==-1) return; gl. Enable(GL_COLOR_LOGIC_OP); gl. Logic. Op(sign); gl. Raster. Pos 2 i(buffer. Width, buffer. Height); gl. Draw. Pixels(my. Image[second]. Width, my. Image[second]. Height, GL_RGB, GL_UNSIGNED_BYTE, my. Image[second]. Pixels); gl. Disable(GL_COLOR_LOGIC_OP); } glut. Post. Redisplay(); } 12/4/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 28
GLF • GLF is multiplatforming library for displaying text in Open. GL. • This library differs from other libraries by simplicity of use and only by one included file (glf. c and header glf. h). • For working of library it is necessary to have the file with the font, by which you will display symbols. You can to use up to 256 loaded fonts at once (setted by default or more if You want). 12/4/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 29
Exercise • Download GLF (ZIP format): glf-1. 4. zip • Test GLF Library 12/4/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 30
- Slides: 30