Lab 1: PicFilter
Please fill out this form about your experience with Lab 1.
Due on Wednesday, February 6th at 11:59 PM. This is an individual lab. You are to complete this lab on your own, although you may discuss the lab concepts with your classmates. Remember the Academic Integrity Policy: do not show your code to anyone outside of the course staff and do not look at anyone else’s code for this lab. If you need help, please post on the Piazza forum or contact the instructor or ninjas. If you have any doubts about what is okay and what is not, it’s much safer to ask than to risk violating the Academic Integrity Policy.
- Overview
- Images
- The PicFilter Program
- Writing Your Program
- Coding Style Requirements
- Creating Your Own PPM Files
- Going Further
- Summary
- Acknowledgements
Overview
In this lab, you will write a program to apply filters to a particular image file format. You will become familiar with the following in C++:
- Command-line arguments
- Dynamically-allocated arrays
- Organizing your code into separate files
- Defensive programming (e.g. detecting invalid input)
As with the previous lab, you should clone your repository; the URL is git@github.swarthmore.edu:cs35-s19/lab01-<your-username>
; e.g.,
cd ~/cs35/
git clone git@github.swarthmore.edu:cs35-s19/lab01-userID1
cd lab01-userID1
Images
Image data on a computer is typically stored in a series of units called pixels, each of which represents a single colored dot. The image in the tomato figure to the right, for instance, was originally 8 pixels wide and 7 pixels tall (it has been magnified for demonstration). In reality, the individual pixels on a modern computer monitor are almost too small to see. By packing the colored dots together so tightly in a grid, we can render pictures, text, and so on.
Image data on computers may be stored in a variety of different formats; common formats are JPEG (often abbreviated “.jpg
”), PNG, and GIF. Each image formats has its own set of advantages. This lab will be using the PPM image format, the advantage of which is that it is quite simple.
The PicFilter Program
You will write a program called picfilter
which allows the user to manipulate PPM files from the command line. Your program will read a PPM file into memory as an object, perform a transformation on it, and then save it back to disk in another PPM file. Your program will take the input file, the transformation, and the output file as command-line arguments. For instance,
./picfilter old.ppm flipHorizontally new.ppm
will read the file old.ppm
, flip it horizontally (left to right, as in a mirror), and then save the result as new.ppm
.
Reading PPMs
To read a PPM file, you will pass its filename to three functions (these have been defined for you in ppmio.h/cpp
):
ppm_width
: Returns anint
describing the number of columns in the image.ppm_height
: Returns anint
describing the number of rows in the image.read_ppm
: Returns anint*
pointing to an appropriately-sized array of pixel data.
The array of pixels will be three times the size of the number of pixels in the image. For instance, a 100x100 PPM image would produce an array of size 30,000. This is because each pixel is represented by three numbers: the amount of red, green, and blue light to show for the pixel. Each number ranges from 0 (no light) to 255 (all the light). Here are some example pixel values:
0 0 0
: No red, green, or blue light. This color is black.255 255 255
: All red, green, and blue light. This is bright white.0 255 0
: No red or blue light; all green light. This is a bright neon green.255 255 0
: Red and green light, but no blue light. This winds up looking yellow.128 0 64
: No green light. Some red light and just a little blue light. This color is a dark reddish-purple.
The read_ppm
function takes care of opening a PPM file, reading the image data into it, and giving it back to you in a new array. Once you’ve read the PPM, you can change it and then write it back out into another file using the corresponding write_ppm
function. These functions are defined in ppmio.h
.
Transforming PPMs
Once you’ve read in your PPM as an array of integers, it’s up to you to change those integers to transform the image. You could loop over the entire image and set every number to 255, resulting in a giant white rectangle (since every pixel is now white). You could loop over each position in the first row and set each third number to 0, draining all of the blue light from the top line of the image.
As mentioned above, the user specifies an input file, a transformation, and an output file. Here are the transformations the user is allowed to request (and that you must implement):
noRed
: Every pixel’s red value is set to0
.noGreen
: Every pixel’s green value is set to0
.noBlue
: Every pixel’s blue value is set to0
(an example is given to the right).
invert
: All channels are subtracted from255
. For instance,255 128 0
would become0 127 255
.grayscale
: The channels of each pixel are averaged. For instance, the pixel255 128 0
would become127 127 127
(since(255+128+0)/3
is approximately127
).flipHorizontally
: The first pixel in each row becomes the last pixel in that row, the second pixel in each row becomes the second-to-last pixel in that row, and so on. The result is a mirror image of the original picture. If the image were centered on the origin of a Cartesian grid, this would correspond to negating the X coordinate of each pixel (note that array indices are very different from Cartesian coordinates) to flip it over the Y axis.flipVertically
: The first pixel in each column becomes the last pixel in that column, the second pixel in each column becomes the second-to-last pixel in that column, and so on. As a result, the image should appear upside-down. If the image were centered on the origin of a Cartesian grid, this would correspond to negating the Y coordinate of each pixel (note that array indices are very different from Cartesian coordinates) to flip it over the X axis.
For each of these transformations, you will write a void
function of the same name that takes an int*
(the array of image pixels) and makes the appropriate changes. You will then write a main
function which, based upon the command-line arguments to the program, loads the image, calls the right function, and then saves the result. If the user gives an invalid transformation (e.g. flipDiagonal
), your program should generate an appropriate error message and quit without saving a PPM file. You are allowed to ignore errors caused when the user gives non-existent or invalid filenames.
In the sub-directory called test_data
, we have provided a number of example PPM files. Take a look at these examples to get a better understanding of how each filter will transform images. For instance, you can view the image of a rose using the eog
program:
eog test_data/Rose.ppm
And then look at all of the ways the rose can be filtered by opening transformed version of the iamges (the name of the transformation is appended to the image name. You can view them all using the ls
command).
$ ls test_data/Rose*
test_data/Rose.ppm test_data/Rose__invert.ppm
test_data/Rose__flipHorizontally.ppm test_data/Rose__noBlue.ppm
test_data/Rose__flipVertically.ppm test_data/Rose__noGreen.ppm
test_data/Rose__grayscale.ppm test_data/Rose__noRed.ppm
$ eog test_data/Rose__grayscale.ppm
Writing Your Program
You should see the following starter files (those that are highlighted require modification):
Makefile
- instructions for compiling your program.image.h
,image.cpp
- declaration and implementation of functions that will manipulate images. This is where you will write your image transformation functions.ppmio.h
,ppmio.cpp
- functions for reading and writing PPM images. This has been provided for you and should not be modified.picfilter.cpp
- you will write yourmain
function heretest_data/
- a directory containing examples for testing your program Your starter code has been separated into several files:
Compiling Your Program
Your starter code contains a Makefile
. This file contains instructions to compile your code so you don’t have to mess with the details of calling clang++
yourself. You can compile your program by typing make picfilter
(or just make
); if it compiles successfully, you can then run ./picfilter
:
$ make picfilter
clang++ -g -std=c++11 -Werror -D_GLIBCXX_DEBUG -c -o ppmio.o ppmio.cpp
clang++ -g -std=c++11 -Werror -D_GLIBCXX_DEBUG -c -o image.o image.cpp
clang++ -g -std=c++11 -Werror -D_GLIBCXX_DEBUG -c -o picfilter.o picfilter.cpp
clang++ -g -std=c++11 -Werror -D_GLIBCXX_DEBUG -o picfilter picfilter.o ppmio.o image.o
$ ./picfilter inputFile.ppm transformName outputFile.ppm #run your main program
View your resulting output file using eog
to see if it looks right. In addition, we have provided a test that does an exact comparison between your
program output and the correct solution. You can validate your methods using
make tests
:
$ make tests
Transform: noRed
----------------------
Image: HorizontalGradient Result: files match :)
Image: VerticalGradient Result: files match :)
Image: Gerbil Result: files match :)
Image: Rose Result: files match :)
Image: MachuPicchu Result: files match :)
Transform: noGreen
----------------------
Image: HorizontalGradient Result: files match :)
Image: VerticalGradient Result: files match :)
Image: Gerbil Result: files match :)
Image: Rose Result: files match :)
Image: MachuPicchu Result: files match :)
(output truncated)
Each test will have one of three results:
files match :)
- congratulations! Your implementation is correct.no file (maybe not implemented yet?)
- no output was created. This is what you will get if you runmake tests
before implementing the method.files are different
- an output was found, but it was not correct. It is up to you to figure out what went wrong and fix your bug.
Getting Started
Remember: you should not write everything all at once. It’s often best to get a small amount of your code to work and then move on to the next part. You can follow these steps to complete your lab:
-
Start by writing your
main
function inpicfilter.cpp
. Write the code necessary to take filenames fromargv
, load the PPM image, and then save it again. In this step, don’t try to transform the images yet; just read the file in and write it back out. Once you have this small part working, you can add to it a little bit at a time. -
Complete the
pixelToIndex
helper function (see below). This function takes the width of the image and the X (column) and Y (row) coordinates of a pixel in the image and translates them to an index in an array. Writing this helper function will make it easier to write your image transformations. -
Write the header and body of a single transformation (e.g.
noRed
) into yourimage.h
andimage.cpp
files. Then, add anif
statement tomain
to call that function if the transformation matches it. If not, theelse
part of yourif
statement should print an error message. Make sure you don’t save an image if you don’t recognize the transformation. For now, e.g.noRed
will be the only transformation you can handle. -
One by one, implement more transformations, adding them to
main
as you complete them. Be sure to try each one of them out before you move on to the next one. -
Once you’ve finished writing your transformations, run
make tests
to see if the automated testing tools agree that your code is correct.
pixelToIndex
We encourage you to write the pixelToIndex
function because it allows you to write your other algorithms in terms of pixels rather than array indices. To explore the difference, let’s briefly consider how an image (a two dimensional grid of pixels where each pixel is three numbers) might be stored in an array (a single dimensional sequence of numbers).
According to long-standing computing traditions that are counterintuitive to geometry students everywhere, the first pixel is in the upper left of the image. We store this pixel as the first three numbers in the array in the order described above: red, then green, then blue. The next (fourth) number in the array is the red value for the next pixel, which is immediately to the right of the first pixel. In this way, all of the pixels in the first row appear in order in the array; afterward, the array contains all pixels in the next row, and so on.
For instance, let’s consider the grid below which represents an image which is three pixels wide and two pixels tall:
This image is probably best described by the following array diagram:
r | g | b | r | g | b | r | g | b | r | g | b | r | g | b | r | g | b | |
values | 255 | 255 | 255 | 0 | 255 | 0 | 0 | 0 | 255 | 0 | 0 | 0 | 255 | 255 | 255 | 255 | 192 | 192 |
indices | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 |
pixels | A (0,0) | B (1,0) | C (2,0) | D (0,1) | E (1,1) | F (2,1) |
The first two rows are the actual array you obtain when you call read_ppm
with three values per pixel (the second row being the c++ index into the array). The last row is the corresponding pixel from the image - with an rgb value for each pixel, and each row following the next.
So in this particular example, pixelToIndex(3,2,1)
- indicating the width of the image is 3 pixels, and we want the rgb values for the pixel in column 2 and row 1 - should return 15 (the beginning of the rgb values for pixel F)
and pixelToIndex(3,1,0)
should return 3 (pixel B). Before continuing, add calls to pixelToIndex
and print out the return value.
Do you get the exact some values as above?
Coding Style Requirements
For this lab, you will be required to observe some good coding practices:
-
You should pick meaningful variable names.
// Good int* pixels = new int[size]; // Bad int* p = new int[size];
-
You should use correct and consistent indentation. Lines of code within a block (that is, surrounded by
{
and}
) should be indented four spaces further than the lines surrounding them.// Good if (condition) { cout << "Test" << endl; } // Bad if (condition) { cout << "Test" << endl; }
-
You should use a block whenever possible, even if it’s not necessary. This helps you avoid subtle or messy bugs in the future.
// Good if (condition) { cout << "Something" << endl; } // Bad if (condition) cout << "Something" << endl;
Creating Your Own PPM Files
You can use ImageMagick, a package installed on the CS network, together with picfilter
to edit your own images! Start by converting your picture to PPM format:
convert my_image.jpg -compress none my_image.ppm
Then, do anything you want with picfilter
:
./picfilter my_image.ppm grayscale my_new_image.ppm
./picfilter my_new_image.ppm invert my_new_image_2.ppm
Finally, convert your new image back to some other format.
convert my_new_image_2.ppm my_new_image_2.jpg
There! You’ve just used your homework assignment to edit your own photos. :)
Going Further
If you like, you may implement additional filters in your picfilter
program (as long as doing so does not break any of the required filters above). This is not required and we will not assign extra credit for these filters, but you may find it interesting and fun to experiment with the images your program has loaded. Here are some suggestions:
-
spin2x2
: Rotate each 2x2 block of pixels in the image by 180 degrees. That is, we swap (0,0) with (1,1), (1,0) with (0,1), (2,0) with (3,1), (3,0) with (2,1), etc. -
faderight
: Brighten each pixel by an amount determined by how far to the left or right it is in the image. The leftmost pixels should all be their original color;the rightmost pixels should all be bright white. We can accomplish this using a weighted average between the original values and the value 255, where the weight is determined by the X coordinate. Since your weight will likely be a value between 0 and 1, you’ll want to use afloat
to calculate the weight. Similar to (but not quite the same as) Python, you can convert numeric values between different types in C++:int x = 4; float y = 6/float(x); // this is equal to 1.5 int z = int(y); // this is equal to 1 int a = 6/x; // this is also equal to 1 (int division)
-
blur
: For each pixel, set its values to the average of itself and those pixels adjacent to it. That is, the new red value of (1,1) is the average of the old red values of (1,1), (0,1), (1,0), (2,1), and (1,2). Note that this is a bit trickier than the other transformations, since you need the old values of all adjacent pixels. Think carefully about how you might be able to store that information before writing code.
You’re also welcome to make your own filters and try anything you like. Just make sure the filters required for the assignment work correctly!
Summary
To summarize the requirements of this lab:
- Your program must perform all of the image transformations listed above.
- Your program must take its input from command-line arguments and gracefully handle invalid transformations.
- You should be able to run
make tests
on the CS lab machines without any errors.
Acknowledgements
This lab writeup is based on Joshua Guerin and Debby Keen’s NIFTY 2012 submission titled “PPM Image Editor”.