display name.pgmwhere name.pgm is the name of your pgm file. You can find the following pgm files in /home/bvz/cs140/labs/lab3:
A picture is simply a two-dimensional array of pixels. In pgm files, a pixel is a grey value between 0 and 255. Zero is black, and 255 is white. Everything in between is grey.
Pgm files have a specific format. (Actually, I've tweaked them a little to make this easier for you). It 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 jsp.pgm, you'll see:
UNIX> head jsp.pgm P2 252 267 255 37 44 37 37 44 37 44 44 44 37 44 44 48 48 48 44 37 34 33 34 37 33 34 37 48 44 44 44 48 44 44 44 44 53 44 44 37 44 53 53 49 44 41 37 41 41 33 33 29 26 26 29 29 33 37 37 44 48 53 53 57 53 57 61 64 69 69 81 81 77 81 81 73 77 72 48 37 33 33 33 37 37 48 44 33 22 22 25 22 22 14 14 14 14 22 22 22 22 22 22 22 22 22 22 26 22 22 22 22 18 22 18 22 18 18 18 22 26 22This means that jsp.pgm is a 267 * 252 array of pixels. After the 255 come all the pixels. First, the top row, then 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 jsp.pgm above, the first 252 pixels are those in the top row, then the next 252 are in the second row, and so on. There will be a total of 267*252 = 67284 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 with display. Cool, no?
Now, your first pgm program should take two command line arguments: 1) an input pgm file, and 2) the name of the pgm file that you will create. It should read from the input pgm file and create an output pgm file that is the negative of the input file. Call this program neg.
Note, you don't need to call malloc() with this program. Just read in the first four words, print them out, then print out 255 minus each pixel. You can put a newline after each pixel if you want to make it really easy (this is what I did). Check out the output, and see how it looks:
UNIX> neg jsp.pgm jspneg.pgm UNIX> display jspneg.pgm
UNIX> hflip jsp.pgm jspneg.pgm UNIX> display jspneg.pgm
You'll have to use malloc() for this one. Read in the first four words, and then malloc() a row's worth of pixels. Then, for each row, read in the row and write it out backwards. Test it out!
Many pictorial files, such as jpegs or gif files, are stored in a compressed format that eliminates redundant information. This compression reduces the size of the files, which you may have noticed can be quite large, even when compressed. In this program you are going to take a compressed file of a maze and re-create the diagram for that maze. The maze will have walls that are drawn with a '#', corridors that are drawn with spaces, and players, monsters, and treasure that are drawn with various symbols. Here is a sample maze:
############################# # @ # # ##### ##### $ ###### # ##### ## # # ### !## ####### # # ## ##### ####### # # # # # O $ D # ##################################Our compressed representation of the maze will be as follows:
For example, the above maze would be represented by the input file (this file is named mazedata.txt in the lab3 directory):
9 29 0-28 29 0 28 34 0 4-8 12-16 28-33 34 0 4-8 13-14 33 34 0 5-7 13-14 20-26 33 34 0 6-7 12-16 20-26 33 34 0 7 33 34 0 33 34 0-33 @ 1 4 $ 2 21 ! 4 12 O 7 11 $ 7 19 D 7 25Your program will take a single argument, the name of the maze file, as input and output the appropriate maze diagram.
Required Design
Hints
Name your file maze.c. No error checking is required for this program. A sample invocation would be:
maze mazedata.txt
Submit the following files: