CS 351: Assignment #1

Creating Images in C

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/<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

In the first lab 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 24-bit color ppm files. The file ppmmain.c is the src directory are examples 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.


No extensions for the first exercise, just get something compiling and working.


Put your image and C file in your handin folder. (Note, it should be on fileserver1 in the Academics volume under COMP/CS 351. As of 6:30pm Monday it's not there yet. Hang on to your images and code until it is.)