CPSC 324: Computer Graphics
Second Lab
February 13, 2001

Most of this lab is about using color and selections in the GIMP, with just a bit of OpenGL animation thrown in. All the exercises from this lab should be done by the time of the next lab, two weeks from today. The work that you do in GIMP should be added to your web portfolio. A printout of your OpenGL program should be turned in, along with a note telling me where the executable is, so that I can try it out.

To begin the lab, sign onto your account on the math/cs network using X-Win32, and copy the folder /home/cs324/lab2 into your home directory, or a subdirectory of your home directory. You can copy the folder with the command:

cp  -r  /home/cs324/lab2  .


OpenGL Animation

Simple OpenGL animations can be written using the idle callback function, as we discussed in class. Since the idle callback function has no parameters, and it must set up a different frame of the animation each time it is called, you have to keep information about the current frame in a global variables. One way to approach this is simply to keep a frame number in a global variable, and compute all the necessary information for the frame from that number. This is what I do in the sample program simpleanim.c. You will find a copy of this program in the lab2 folder that you copied into your directory.

The executable for this animation is called simpleanim. As we've seen, double-buffered OpenGL programs won't run well over a network unless the window is very small. Since the simpleanim program doesn't set a specific window size, you can set the size on the command line by supplying a "-geometry" option. Try running simpleanim with the command

simpleanim -geometry 100x100

This will run the program in a 100-by-100 pixel window.

The simpleanim program uses an animation loop in which the frame number starts at 1, goes up to a maximum value, and then loops back to 1, so the animation simply repeats over and over. In this example, the animation is "segmented" in that it has several segments in which different types of action take place. In the first segment, a line rotates 180 degrees. In the second, the line widens into a square and the color changes from black to red. In the third, the square shrinks back to a black line.

(As another example of animation, you can try running "moire -geometry 100x100". This program shows a moiré pattern, a kind of interference pattern. In this case, the animation is based on random motion. The program also demonstrates mouse interaction with an OpenGL program. You can start and stop the animation by clicking the window. If you right-click and drag, you can slide the pattern around by hand. You will find the source code, moire.cc, in the directory /home/cs324/mesa.)

Here is the OpenGL programming exercise for this lab. You shouldn't work on the programming during lab. (I expect that you will want to do it after you've turned in the first programming project next week.)

Exercise 1: Your assignment is to write a segmented animation based on simpleanim.c. You can use simpleanim.c as a starting point. The main (and perhaps only) change that you have to make is to the drawFrame() routine. One idea: Draw a house with green ground and blue sky. In one segment, the sun rises and the sky brightens. Then the sun sets and the sky darkens. Maybe a cloud goes across the sky? You can do this animation or something of similar complexity. I am not asking for a major program. I just want you to understand how to vary some numbers between frames to produce an animation. If you decide that you want to put one or more frames from your animation on your Web site, you can use the "Snapshot" program which is in the "Graphics" sub-menu of the main KDE "K" menu.


GIMP Colors and Tools

Although GIMP is often used to manipulate and enhance existing images, it does have some sophisticated drawing tools that can be used to create images from scratch. To start a new image from scratch, select the "New" command from the "File" menu in the GIMP toolbox window. In the "New Image" dialog, enter a reasonable size for the new image -- say width and height equal to 300 pixels -- and click OK.

Try out some of GIMP's drawing tools. Click on a tool to select it. Note that not all the tools in the toolbox are drawing tools. The ones at the top are for selecting areas in the image, and the last one is for measuring distances and angles.

Note that one way of drawing is to make a selection, using the rectangle or ellipse selection tool for example, and then to "stroke" the selection. To do this, choose one of the drawing tools, right-click on the image, and choose "Stroke" from the "Edit" menu. Selections are useful in another way: If there is a selection, then drawing can only occur inside the selection. This can be especially useful with the Bucket Fill tool.

In addition to tool icons, the GIMP toolbox window contains icons for foreground and background color, brush, pattern, and gradient. Clicking on any of these icons brings up a dialog box that allows you to choose a color, brush, pattern, or gradient. The icon in the toolbox window shows the currently selected value. These values are used by several of the drawing tools. For example, the pencil, brush, and airbrush tools all use the currently selected brush. You might wonder what a brush that says "The Gimp" or one that looks like a green pepper will do...

Double-click on a tool in the toolbox to bring up a dialog box with options for the tool. You will find a lot of possibilities. (Some of them, however, only work with pressure-sensitive input tablets, but not with a mouse.) For example, if you double-click the brush tool, the tool options dialog lets you select a "fade out" option that will make the brush run out of ink after a specified number of pixels. (Try it!) There is also an option that lets you draw with a gradient of colors rather than a plain color. In the tool options for the bucket tool, you will find the option of filling an area with a pattern instead of with a color. (Select the pattern that you want to use by clicking on the pattern icon in the toolbox window.) The "threshold" in this Bucket Tool Options window determines where the paint from the bucket will stop spreading. The paint only spreads to pixels whose color-values are "similar" to the color-values at the point where you click. The threshold determines how similar they have to be. If the threshold is maximal (255), the paint will fill the entire image (or just the selection, if there is one). If the value is small, the paint only spreads to very similarly colored pixels.

The gradient tool (officially called the "Blend tool") is particularly fun. In the Tool Options, you can choose several types of gradients, such as Linear, Radial, and Conical. You can also choose which type of "Blend" you want to use. Three of these use Foreground and Background colors from the toolbox window. The third selects the currently selected gradient shown in the toolbox window. (To avoid frustration, note that the gradient tool does not use this gradient unless you select the "Custom from Editor" option under "Blend" in the Tool Options window.) Gradients that fade to transparency are particularly interesting. Try the Flare_glow_radial_2 or Radial_eyeball_blue custom gradient with a radial gradient.

One of the main things I want you to look at is GIMP's color selection dialog box, since it illustrates some of the color theory that we have been discussing. Click the foreground color icon in the Toolbox window to bring up the color selection dialog box. GIMP provides four different ways to select a color. (Actually, it provides five, since it has a color palette dialog that you can bring up by typing CTRL-P.) The first, default method shows both HSV and RGB color values that you can adjust individually. Just to the left of this is a color spectrum showing all possible hues. You can click in this to select a hue. The colors shown in this spectrum are bright saturated colors with V = S = 1. The large square to the left shows all possible colors with the selected hue. Value increases from left to right and saturation increases from bottom to top. Along the bottom, where S = 0, are shades of gray. Along the top are fully saturated colors, with no gray component (that is, at least one of R, G, and B is zero).

In the "Triangle" method of color selection, the hue is shown as a colored ring, which better reflects the way hue actually works. Change the hue by dragging the white circle around the ring. The triangle in the center of the ring contains colors of the selected hue, with different values and saturation.

The "GTK" color input lets you select Value by clicking in a bar and Hue and Saturation by dragging in a colored disk. I'll let you figure out the cute "Watercolor" input method on your own. Note that none of the visual methods for inputting a color are based on RGB, which exists more for computer monitors than for people: In spite of the way our eyes work, we don't actually perceive colors as blends of red, green, and blue.

Exercise 2: Create one or more pictures using GIMP tools, starting from a blank canvas. Use a variety of tools, including at least a few of the following: Paintbrush tool with different brushes and colors. Bucket took with a pattern fill. Gradient tool with custom gradient. Smudge tool. Paintbrush with gradient. Paintbrush with fade out. Rectangles and ellipses made by "stroking" a selection.

Important note: If you want to save work that you have done in the GIMP and come back to work on it later, you have to save in ".xcf" format. Only XCF files retain the complete structure of a GIMP image, so that you can reload it and pick up where you left off. When you save in JPEG, GIF, or some other standard format, you only get a "view" of the image, without all the GIMP-specific structure. Save in these formats when you want an image that be used on the Web or in another program.


GIMP Layers and Transparency

In GIMP, an image can be a composite of several images that are in different "layers". There are several advantages to this. The image in each layer can be manipulated, edited, and moved. When part of a layer is covered by something in another layer, it's not lost and it will still be there if the obscuring layer is removed. And layers can "blend" with layers beneath them, rather than obscuring them. We say that layers can be partially transparent. (In fact, it is often the case that part of a layer is completely transparent. Note that GIMP shows transparent regions as gray/black checkboards.)

The "Layers, Channels, and Paths" dialog box is essential for working with layers. You can open it by typing CTRL-L. The "Layers" part of this dialog box shows a list of layers in the currently selected image window. Let's make an image with two layers.

Open the image hiroki.jpg. (This is another former student.) Also open dollar.jpg. (This is a scanned image of a dollar bill.) Select the entire dollar bill image (by typing CTRL-A or selecting "All" from the Select menu). Use CTRL-C to copy the selection. Now click on hiroki and use CTRL-V to paste the image of the dollar bill on top of the image of hiroki. In the Levels dialog, you will see that the dollar has been added in its own layer.

The layer that you get when you do a paste is a special kind of layer called a floating layer. You need to "unfloat" this layer by making it into a new layer or by adding its contents to an existing layer. (Until you do this, you won't be able to do anything else except work on the floating layer.) Here, we want to convert the floating layer into a new layer. Do this by clicking the new layer button, which can be found at the bottom left of the Layers dialog. You'll see that the dollar is in its own layer, and that layer is transparent except for the image of the dollar. (Note, by the way, that if you drag a selected area, it becomes a floating layer, and you have to treat it the same way as a pasted image.)

Here are a few things that you need to try, to understand some important aspects of the GIMP:


Funny Money

Image composition -- putting an image together out of pieces of other images -- is an important application of the GIMP. Let's try two relatively simple examples and look at a few more tools along the way. You will not be producing truly professional results or doing things in the most efficient ways, but it's a start.

The first project uses the image coxe.jpg and one of the currency images dollar.jpg or twenty.jpg. (My instructions will assume you are using the dollar bill.) The idea is to replace the sky behind coxe hall with a picture of money. Open the two images that you want to use.

Start with a blank window that is the same size as coxe.jpg. (To get the exact size of coxe.jpg, click on the image and call up the Image Info window with CTRL-SHIFT_I or the "Image Info" command in the View menu. This window shows the dimensions of the image.) Next, you need a dollar bill that will just fit horizontally in this window. To get this, scale image of the dollar bill. This can be done using the "Scale Image" command in the image window. Just type in the New Width and hit OK. (The height will be determined automatically since the X and Y dimensions are "locked" together.)

Once the dollar has been scaled, do a Select-All (CTRL-A) and then a Copy (CTRL-C), and then paste the dollar into your new window. Draw the dollar to the very top of the window, where the sky is in the image of Coxe hall. Do this before you unfloat the image. Since you don't need a new layer for the dollar, click the "Anchor" button in the Layers dialog. This will add the dollar bill to the existing background layer.

Now, you have to add Coxe hall to the composition. Select the entire coxe image, copy-and-paste it over the dollar bill, and click the New Layer button in the Layers dialog. Now, all we have to do is remove the sky from the coxe hall layer, so the dollar will show through. To so this, we must first make the sky into a selection. Then, we will remove the selection with CTRL-X. Fortunately, the sky is a very uniform color. (This is one reason why I selected this image.) This means that it can be selected using a select-by-color feature. Use the "By Color" command in the "Select" menu. This opens a dialog box. With this box open, click on a point in the coxe layer in the image window. You will see the selected area shown in white in the dialog box. I found that increasing the threshold to about 32 gives a better selection. (The threshold determines how large a range of color values will be selected.) You are very lucky: only the sky is selected, so you won't have to edit the selection. You can now close the "By Color Selection" dialog box.

Now, go back to the image window and hit CTRL-X (Cut). You will see the dollar where the sky used to be. However, the edge between the dollar and coxe hall is too sharp. We can improve on this. Use Undo (CTRL-Z) to restore the sky. Now select "Feather" from the "Select" menu (CTRL-SHIFT-F). To feather a selection means to make pixels around the border partially transparent, with a gradient of transparency between the inside and the outside. This will allow the two images to blend more smoothly. The default setting, 5 pixels, is fine, so just click OK in the dialog box that pops up. Now do CTRL-X again. Do you see the improvement?

By the way, the dotted lines around a selection can get in the way of seeing the image properly. You can turn off the dotted lines without turning off the selection by hitting CTRL-T. Turn them back on with another CTRL-T. (Don't forget to turn them back on, or you'll get confused. Remember that you can only draw inside a selection, so you want to remember that the selection is there.)

At this point, I decided that I wanted the dollar bill to get brighter towards the bottom. Here's how to do this: Make the coxe hall layer invisible and select the dollar bill layer. Double-click the Gradient tool to bring up the Tool options dialog. Under "Blend," select "FG to transparent". "Gradient" should be set to the default "Linear" and "Repeat" to the default "None". Since I want the gradient to blend from white to transparent, make white the foreground color. (A quick way to this, if white is, as usual, the background color, is to click the little double-headed arrow next to the color icons in the Toolbox window. This swaps the foreground and background colors.) Now, with the gradient tool selected, click near the bottom of the dollar bill and drag to the top. Remember that you can always Undo and try again if you don't like the exact effect. Toggle the visibility of the Coxe hall layer back on to see the final effect.

Don't forget to save your work. Save with an extension of .xcf if you want to keep the structure of layers and transparency. If you want to continue working on the image in the future, you need to save it in xcf format. Save with extension .jpg or .jpeg to get an image for your Web page. When you do this, you will be warned that you are losing information. You know this, so just hit the "Export" button, and you are done.


Here's another, similar sort of project that I will go through very quickly. You could probably figure out most of it on your own at this point. The goal in this case is to replace Andrew Jackson on the twenty dollar bill or George Washington on the one dollar bill with one of our favorite students, Sean or Hiroki. Let's say that you are putting Sean on the Twenty. Open the two files that you need.

You will need to scale Sean so his head fits in the space now occupied by Andrew Jackson. But how do you know how much to scale? This is a job for the Measure Tool. Select the Measure Tool from the toolbox. To use it, drag from one point to another on an image. The number of pixels between the two points will be displayed on the bottom of the image window. Use this to measure Jackson's head and an equivalent part of Sean, and use the data to calculate the scaling factor you need. (There is a calculator in the "K" menu under "Utilities".) You can use this value in the "Ratio X" box of the Scale Image dialog.

After scaling the image, cut out a rectangular section containing the part of Sean that you want to use. Make it bigger than you need, since you will trim the excess later. Copy-and-paste the selection over the dollar bill, and make it into a new layer. To help with positioning and trimming the layer, make it partly transparent, so you can see the dollar bill through it. Drag Sean into position. Use the elliptical selection tool to select just the part of Sean that covers George Washington. If you get the selection wrong, use Select:None (CTRL-SHIFT-A) to turn off the selection, and try again.

After making the selection, its a good idea to feather it (CTRL-SHIFT-F) to get good blending with the background. Now, we want to delete the outside of the selection. There is no command for this, but you can "invert" the selection, which makes the inside the outside and vice versa. Do this with the Select:Invert command (CTRL-I). Trim the excess with Cut (CTRL-X), and you should be done!

Possibly you don't like the fact that Sean is still in full color. If that bothers you, change him to black-and-white with the "Desaturate" command in the "Colors" sub-menu of the "Image" menu. This gives you an RGB image in which the R, G, and B components of each pixel are the same. Now, you want to colorize the image to make it a little green. One way to do this is with the Curves dialog, which you can access with the "Curves" command in the "Colors" sub-menu of the "Image" window. This is a rather sophisticated tool, but for now you might just try dragging the dots at the ends of the Value, Red, Green, and Blue curves, and see what happens. Try to make the colors match the colors of the dollar bill. You can start by dragging the dot at the bottom of the Green curve up a bit.

Exercise 3: Create at least one of the image compositions described here (money-behind-coxe and student-on-bill). In addition, make one or more image compositions that you design yourself, using the images that I have provided for you or images that you find yourself.


David Eck, February 2001