CS 351: Assignment #1

Creating Images in C

Bluescreen Photos

Due midnight, Thursday 15 September

The purpose of this assignment is to familarize you with Unix, C and the mechanics of creating, manipulating, and writing images. For this assignment, work by yourself, but feel free to confer with other folks about the mechanics of editing and compiling.

If using a terminal or compiling a C program are new to you, please check out the Terminal and writing C tutorials.


Before you start writing code, please take a look at the coding style guide.

You probably want to keep your code on your network directory. If you are on a terminal on a mac, you can get to the working directory using

cd /Volumes/Personal/students/<initial>/<your user folder name here>

Within your personal directory make a vision working directory. It will behoove you to plan ahead when organizing your directory structure. You will be reusing your code from assignment to assignment, building upon it as we go. A suggested directory layout--created automatically by the code archive below--is as follows.

       |      |    |   |    |
      bin include lib src images
                   |   |
                  obj obj

We'll go over how to write a makefile that works with this directory structure. You can also take a look at the makefile tutorial.

To read/write PPM images, download the following code archive: iolib.tgz. To unpack the code, use the following command in an X-window terminal:

tar xvfz iolib.tgz

Note that the mac may automatically unzip the file, creating a file called iolib.tar. In that case, drop the z from the tar command.

The archive will create a set of working directories and put some basic code into the lib and src subdirectories. This code will allow you to easily read and write color ppm files. The file ppmmain.c in the src directory is an example of how to use the routines to read, manipulate, and write a PPM image.

To change directories, use the cd command. To see what is in your current directory, use the ls command. For example, after you untar iolib, if you type ls then you should see a directory called iolib. You can go into that directory using:

cd iolib

Then you can type ls again, and you should see directories called bin, images, include, lib, and src. Use the cd to move into any of those subdirectories.

To make the program, first go into the lib directory and type make -f makefile.lib (or rename makefile.lib to makefile and just type make).

Once the library is compiled, go into the src directory and type make. The executable ppmmain should end up in the bin directory. The terminal below shows the process.

You can run the program using the X-terminal window. For example, if you are in the images directory, you can use the command as below using the given ppm file.

../bin/ppmmain maineTree.ppm tmp.ppm

Make sure you know how to use xv, display, or some other general image viewing tool for unix. Gimp or photoshop are also nice tools, but they are like using a bulldozer when all you really need is a shovel. To start xv with an image (e.g. foo.tif), type xv foo.tif on the command line in X. Alternatively, you can just type xv and then use the Load button to select an image to load. (Hint, to get a load button, click on the startup image with the right mouse button.)

In xv, you can view the actual pixel values by clicking on the middle mouse button and moving the cursor around the image. On the macs there is also the DigitalColor Meter in the Applications:Utilities directory. It lets you access the pixel color of any pixel on the screen.


  1. Copy ppmmain.c to a new file (lab1.c, for example).

    cp ppmmain.c lab1.c

  2. Edit the makefile in the src directory by copying the two lines
    ppmtest: $(ODIR)/ppmmain.o
            $(CC) -o $(BINDIR)/$@ $^ $(LFLAGS) $(LIBS)
    and modifying the copied lines to look like the following.
    lab1: $(ODIR)/lab1.o
            $(CC) -o $(BINDIR)/$@ $^ $(LFLAGS) $(LIBS)
    Then just type make lab1 on the command line and the rule will be executed.
  3. Modify the for loop in the new main program so that you do something else to the image. Turn it green, turn it red, overlay a sine wave, multiply it by a ramp, whatever. Just remember that the values in an image need to be between [0, 255] in each channel.
  4. I'll set up the blue screen in the robot lab. Stop by and get your picture taken and a picture of something else you want to put into an image. Then do the following.
    • Use some kind of test (e.g. blue > 2*red and blue > green) to read in the bluescreen image and create and write out a mask that is 1 for the blue background and 0 for the foreground. Make this a command line program.
    • Write a program that takes in two images and a mask image and merges the two images using the mask as an alpha mask. The alpha blending function is I = alpha*image1 + (1 - alpha)*image2. Note that you'll need to interpolate the 0-255 values in the mask to the range [0, 1].
    • Extend your program to take two offsets, dx and dy, such that the second image is placed into the first with the given offset. The mask should also be offset, since it should align with image2.
    • Use your programs to do something interesting.


  1. Try to make your mask so that it has in-between values for pixels on the edge. See if you can estimate how blue the pixel.
  2. Add the ability to scale the image to your program.
  3. Grab a picture of fog, or an explosion, or something else and layer it on top of your scene in a semi-transparent manner. For example, you could use a mask with values of 30-50, randomly chosen, to add some transparent fog to a scene.


Make a central CS 351 wiki page in your home space. Then make a child page linked to it for each project.

For this project, make a short wiki page with your pictures and a description of the process you used to make them. If you did any extensions, describe the algorithm and show at least one example for each extension.


For each assignment, make a subdirectory with the number of the project in the name in your private subdirectory on the Academics server. Then put up a complete working copy of your assignment and the images you used, in the proper directory structure.