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.
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 computer graphics 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.
graphics | --------------------- | | | | | bin include lib src images | | obj obj
- bin: put executables in here
- include: put .h include files in here
- lib: put common code in here (lines, circles, polygons, math code, for example)
- src: put top level .c files here
- images: put your images in here
- obj: compiled object files will go here
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:
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 ppmtest 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 provided ppm file.
../bin/ppmtest maineTree.ppm tmp.ppm
To view a ppm file you can use xv (if installed), display (part of the ImageMagick toolkit), gimp, or Photoshop. Gimp and photoshop are nice tools, but they are like using a bulldozer when all you really need is a shovel. To start xv or display with an image (e.g. foo.tif), type xv foo.tif (or display foo.tiff) on the command line in X. Alternatively, you can just type the name of the program (xv or display) and then use the Load button to select an image to load. (Hint, to get a load button in xv, 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.
Copy ppmmain.c to a new file (lab1.c, for example).
cp ppmmain.c lab1.c
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.
- 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.
I'll set up the blue/green 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 to read in the blue/green screen image and create and write out a mask that is 1 for the blue/green 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.
- Try to make your mask so that it has in-between values for pixels on the edge. See if you can estimate how blue/green the pixel is.
- Add the ability to scale the image to your program.
- Add the ability to mirror or rotate the image to your program.
- 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 course wiki page in your home space. Then make a child page linked to it for each project. Please make it easy for me to click to each assignment you complete.
Your report for each project should contain the content listed below.
- A summary or abstract for the project. What was its purpose and what did you create?
- All required pictures and a brief description of how you created them. One paragraph per picture is sufficient. Point out anything interesting.
- Your two extra pictures and some accompanying description.
- Any extensions, including pictures and description.
- Collaborators and sources.
Give your wiki page the label: cs351s18project01
For each assignment, put your code in a subdirectory for the project inside the Private folder in your CS351 directory on the Courses server. I should be able to compile and run your code using a makefile, following instructions you provide in a README file.