You will be writing a program designed to read in a text-based picture file, manipulating it, and then writing the manipulated picture file into an output file.
A PPM file is called a “Portable Pixmap”, which is essentially a plain-text file of numbers from 0-255 which describe the color channels of a picture.
The very first line is always “P3”, which identifies this as a PPM file. If this file does not contain P3, do not read it, and let the user know that there was an error.
The second line contains two values in order: width and height. These describe the width and height in pixels.
The third line contains one value: max intensity. This describes the maximum intensity of a color value starting at 0. This value is typically 255, but you cannot assume it is.
The rest of the lines contain three values: red, green, and blue. These are integers that describe the amount of color each pixel gets. These values must be in the range of [0..max intensity]. If you find a value that is > max intensity, WARN the user, and set the pixel’s value to max intensity.
ANY line beginning with a # is a comment, and it must be ignored by your program. You do not need to check if # is anywhere else in the file, such as after a value on the same line.
The red, green, and blue values may be on a line by themselves, or they may be on a single line, or may be split between a line, whitespace, comment, and another line. Your program must be able to handle all situations.
PPM file example:
1
2
3
4
5
6
7
8
9
10
11
12
13
P3
#width height
5 4
#maximum intensity
255
#row 0
2 9 4 100 200 100 7 9 1 91 99 94 18 44 19
#row 1
72 91 44 88 44 12 91 99 94 14 55 22 19 88 77
#row 2
44 12 22 91 99 94 78 44 19 191 215 222 177 199 121
#row 3
41 98 111 91 99 94 22 11 22 177 144 121 91 99 94
The PPM file example above contains the ‘P3’ identifying this as a PPM file. It then has a width of 5 pixels and a height of 4 pixels. This means that there should be exactly 20 pixels (5 x 4) laid out after the maximum intensity, which for this file, is 255. The pixels are both in a row and on lines by themselves. You cannot assume how the PPM file will store the pixels. So, your program must handle multiple pixels per line as well as multiple pixels on their own lines.
Notice that there are three values per pixel. In order, these are the values for the red, green, and blue color channels.
Create a Pixel structure that contains three members which will contain values between [0..max intensity]. Only positive numbers will be stored. One member will store the value of the red channel, one for the green, and lastly, one for the blue channel.
Create a Picture class that contains:
Remember that rows are essentially your Y value (top to bottom) and that columns are essentially your X value (left to right).
The first command line argument will specify the input PPM file to read or a dash ‘-‘ to read the PPM file from cin. This means that you will need to open the input file, check it, OR set it to cin inside of int main().
The second command line argument will specify the output PPM file to write or a dash ‘-‘ to write the PPM file to cout. This means that you will need to open the output file, check it, OR set it to cout inside of int main().
The third command line argument will be one of the following (case insensitive): I, Y, X, or it may not even be specified. If the value I is specified, this instructs your program to invert all pixels prior to writing the PPM output file. If the value Y is specified, this instructs your program to flip the PPM picture around the Y axis (left becomes right, right becomes left). If the value X is specified, this instructs your program to flip the PPM picture around the X axis (top becomes bottom, bottom becomes top). Finally, if no argument is specified, your program will read the input and simply write to the output file without inverting or flipping the picture.
Download the following samples: bee.ppm
The following is what this sample PPM file should look like when viewed through the online PPM viewer:
You can use my online PPM viewer at: http://web.eecs.utk.edu/~smarz1/pgmview/
Please remember that all labs are an individual effort. Please refresh yourself on the plagiarism policy located on the course syllabus.
Compile your lab using the following command:
1
g++ -Wall -O0 -g -std=c++11 -o lab11 lab11.cpp
Submit your lab11.cpp. Your lab11 MUST COMPILE. If it does not, it will NOT be graded.