fprintf(stderr, "Bad PGM file -- first word is not P2\n");
As always, there are executables for each part of the lab in the lab directory. Make sure you test them out and make sure that your answer matches them exactly. If you have simple questions about what your program should do, try checking the executable first rather than asking the TA. Often, the answer is there.
Unfortunately, most web browsers do not have support for PGM files. However, every environment has many ways of dealing with them. For example, Photoshop, Open Office and Gimp both support them as image types. I would guess that iphoto and whatever photo editor comes with Windows handles them as well.
Most Unix environments have a program called convert, which will convert pgm's to jpgs for easy viewing. For example, on our labs:
UNIX> cp /home/jplank/cs140/Labs/Lab2/Nigel.pgm . UNIX> convert Nigel.pgm Nigel.jpgYou may now view Nigel.jpg with a web browser. Test it out. It should look like:
Here are a bunch of sample PGM files that you can use with this lab. They contain some of my most important role models. Unfortunately, you shouldn't try to create your own PGM files with the above software, as most PGM converters convert to a different format, or they include comments, which I'm not requiring you to handle in this lab.
Also, the sizes of the JPG pictures below do not match the PGM files. I've shrunk some of them to make them display better.
Red.pgm Red Forman - Role model for being a father and husband. |
Phil.pgm Phil Dunphy - Ditto |
Hand.pgm Mr. Hand - Pedagogical role model |
Schalk.pgm Schalk Burger - Sportsmanship |
Frank.pgm Frank Slade - Dealing with children |
Kai.pgm Kai Li - Motivational |
Nigel.pgm Nigel Tufnel - Artistic |
Paris.pgm Paris Gellar - People skills |
Pike.pgm Rob Pike - Role model for answering questions during talks |
Reggie-Mike.pgm Reggie and Mike - Parental |
Rodney.pgm Rodney Farva - People skills |
Sheldon.pgm Sheldon Cooper - For what is Sheldon not a role model? |
A picture is simply a two-dimensional array of pixels. In a PGM file, a pixel is a gray value between 0 and 255. Zero is black, and 255 is white. Everything in between is a different shade of gray.
PGM files have a specific format. (Actually, they are more robust than this -- I've removed comments to make them easier for you). A PGM file is composed of words. The first word is ``P2''. Then the next two words define the number of pixels in the picture -- first is the number of columns in the two-dimensional array, and then the number of rows. The next word is the number 255. So, if you look at the beginning of Red.pgm, you'll see:
UNIX> head Red.pgm P2 235 183 255 67 74 76 80 76 77 70 67 75 82 102 119 129 133 130 127 128 130 134 131 130 129 130 133 132 125 116 109 101 102 105 105 113 119 128 133 138 137 137 137 141 143 143 144 140 143 146 144 140 138 128 122 118 107 106 104 106 107 115 124 127 121 114 108 103 98 97 108 122 138 140 140 139 129 123 125 127 141 145 147 147 146 147 147 147 149 150 150 150 155 168 185 195 198 202 201 201 200 200 199 201 199 200 201 207 205 206 203 204 205 206 206 205 206 207 207 207 207 207 UNIX>This means that Red.pgm is a 183 * 235 array of pixels. After the 255 come all the pixels. First, the pixels in the top row, then the pixels in the next row, etc. Note that the ASCII formatting of the file doesn't mean anything -- there could be one pixel per line and the file would still be a legal PGM file. In Red.pgm above, the first 235 pixels are those in the top row, then the next 235 are in the second row, and so on. There will be a total of 183*235 = 43005 pixels. After the last pixel, the file ends.
Before you go any further, create a PGM file of your own -- make it 10x10 and give the pixels any value you want. Take a look at it. Something like:
P2 10 10 255 0 10 20 30 40 50 60 70 80 90 10 20 30 40 50 60 70 80 90 100 20 30 40 50 60 70 80 90 100 110 30 40 50 60 70 80 90 100 110 120 40 50 60 70 80 90 100 110 120 130 50 60 70 80 90 100 110 120 130 140 60 70 80 90 100 110 120 130 140 150 70 80 90 100 110 120 130 140 150 160 80 90 100 110 120 130 140 150 160 170 90 100 110 120 130 140 150 160 170 180 |
This should look like:
Cool, no?
Here is an example of pgminfo running on some of the PGM files. Note, I have a few bad PGM files in Bad-1.pgm, etc. You need to make sure that your output matches mine exactly. And I mean exactly, meaning the same punctuation, same number of spaces, and same capitalization. Use printf(), not cout.
UNIX> ./pgminfo < Red.pgm # Rows: 183 # Columns: 235 # Pixels: 43005 Avg Pixel: 120.142 UNIX> ./pgminfo < Pike.pgm # Rows: 235 # Columns: 197 # Pixels: 46295 Avg Pixel: 99.932 UNIX> ./pgminfo < Bad-2.pgm Bad PGM file -- No column specification UNIX> ./pgminfo < Bad-5.pgm Bad PGM file -- pixel 99 is not a number between 0 and 255 UNIX> ./pgminfo < Bad-6.pgm Bad PGM file -- Extra stuff after the pixels UNIX>When I print a pixel number, it is zero-indexed. So the first pixel is pixel zero.
UNIX> ./bigwhite 20 10 > a.pgmThis will create a PGM file a.pgm, which has 20 rows and 10 columns of white pixels.
Your output in this and the next three programs should match mine exactly when there is an error. Otherwise, the PGM files that it emits should be equivalent to mine (they should make the same picture), but do not have to have the same output exactly.
For example, here is the negative of Red.pgm:
You'll have to use a vector for this program.
Here's the hflip of Red.pgm: