CS35 Lab 1: Image Editor

Due 11:59pm Sunday, September 15, 2013

The goal of this lab is to introduce you to basic concepts in the C++ program language. Concepts you will be familiar with after this lab include:

In addition, you will become somewhat familiar with image processing and the concept of buffering.

A skeleton version the program will appear in your cs35/labs/01 directory when you run update35. The program handin35 will only submit files in this directory. In later labs you may have the opportunity to work with a partner, but for this lab you should work on your own. Useful links:



Introduction

This lab will have write a program to load and output image files. In addition, you will provide a user the option to perform some basic image manipulation, including color alteration and horizontal flipping. To begin, we will first learn about the PPM Image format. Click here for information about the PPM format (and how to parse it) as well as tips for viewing and creating PPM image files.

Reading about the PPM format may seem overwhelming, but it's actually fairly simple to get started. At a high level, your program will ask the user to provide both the name of the input PPM image and the name of the file they would like to save the modified PPM to. You will then ask the user which of several image alteration techniques they would like to apply, perform the operations, and save the resulting image. Here is a sample run of the program where the only options are to convert an image to greyscale and flip it horizontally:

$ g++ ppmEditor.cpp -o ppmEditor
$ ./ppmEditor 

Welcome to the Portable Pixmap (PPM) Image Editor

Enter name of image file: input/oberon.ppm
Enter name of output file: oberon-grey.ppm
Do you want to: 
	convert to greyscale? (y/n): y
	flip horizontally? (y/n): n
	
Session complete
The original and resulting image:

oberon.ppm oberon-grey.ppm

Program Requirements

I recommend implementing your program in 3 phases:

  1. File Input and Output - be able to read in a PPM image and output it without loss of information
  2. Image alteration functions - add functions for modifying the image
  3. Bring it all together with a menu - complete your program by adding user interaction to choose which image filters to apply to an image
Your program should use proper design. That is, your program will be tested on your use of modular functions and understanding of how to call, define, and declare functions in C++.


I. File Input and Output

Your program will need to handle opening, reading, closing, and writing to file.

Your output file can actually be formatted as you like. Whitespace includes newline characters, so you can put them in where you wish. The format allows for it. Test this with small files and large files. You can check to see if they are identical by loading them both into a text editor like gvim and comparing number by number. Load them into image viewers, as discussed in the PPM Image Format help page listed above. Example ppm files are available in the input folder in your labs directory for this week.

Review of requirements before moving forward:

II. Image Alteration Functions

Once you can exactly replicate a PPM image, you can begin writing functions to manipulate the image. At a minimum, you must implement a function to convert an image to greyscale, another to flip the image horizontally, a method to negate all red values, and at least 2 more of your choosing. Let us begin with negating red values.

Note that you are responsible for determining what parameters are needed for each of these functions (aside negateRed which has been provided for you.

Pick whatever sounds interesting for your other methods. Some ideas include:

A more difficult but fun function would be to add random noise to the image. There is plenty of information on how to generate random values in C++ online, or you can come see me or one of the ninjas.


III. Bring it all together with a menu

The overall design of your program should be as follows:
Greet user
Ask user for input file (check if valid)
Ask user for output file
Read header (check if valid)
Output header
Ask user what filters (image alterations) to apply
For each line of the image:
	Load image into buffer (check if still in valid state)
	Apply chosen filters
	Write image to output file
Close files, output message
The menu simply asks the user if they would like to apply each filter you developed. Using a series of yes or no questions. If the user enters an invalid value (you must define this) you should restate the question until a valid response is provided. At all points in your program, you should be defensive against potential user error

See the example at the top of the writeup for an example menu. Your program should apply each filter specified, in succession, one at a time. The filters must be cumulative (even if doesn't make sense in some cases); that is, if the user chooses to flatten all reds and flatten all blues, you should have a resulting picture of only green values.


Review of Requirements and Sample Results
You can find examples of the cake image here.

To have your output image automatically pop up before your program completes, you can invoke the system command. Note that this is not a requirement but can help save the annoyance of loading the image manually after every run. Simply add the following command after your output file is closed:

system(("viewnior "+outfilename).c_str());
viewnior is a image-viewing program and outfilename is a string containing the name of the output image. You will need to include an extra library at the top of your program to use this:
#include <stdlib.h>


Submit

Once you are satisfied with your code, hand it in by typing handin35. This will copy the code from your cs35/labs/01 to my grading directory. You may run handin35 as many times as you like, and only the most recent submission will be recorded.


Acknowledgments

This lab write up is based off of Joshua Guerin and Debby Keen's NIFTY 2012 submission titled PPM Image Editor. The pictures of the dog are by me. His name is Oberon. And yes, he does know how ridiculously cute he is, but he loves hearing it anyways.