CS140 -- Lab 5


Wed Sep 12 13:17:56 EDT 2007

Read the lecture notes entitled "More Information Hiding with (void *)'s: PGM Files". Your job is going to be to write five programs using jpgm.h and jpgm.c from that lecture. As always, the makefile in this lab's directory will do everything for you. However -- when the TA's grade this, they will compile your code with the jpgm.h from this directory, and they will link it with jpgm.o from this directory. Therefore, you may not in any way modify jpgm.h or jpgm.c. You use them as-is to write programs like neg_pgm.c and hflip_pgm.c from that lecture.

vflip_pgm

This takes a PGM file on standard input and produces a PGM file on standard output that is flipped. So, if you do:
UNIX> vflip_pgm < Rodney.pgm > Rodney-Vflip.pgm
Here are the files:

Rodney.pgm
Rodney-Vflip.pgm

It should require you about ten key strokes in vi to change hflip_pgm.c to vflip_pgm.c.


rot_pgm

This takes a PGM file on standard input and produces a PGM file on standard output that is rotated 90 degrees clockwise. So, if you do:
UNIX> rot_pgm < Rodney.pgm > Rodney-Rot.pgm
Here are the files:

Rodney.pgm
Rodney-Rot.pgm


crop_pgm

This takes four command line arguments: fromleft, fromtop, width and height, and PGM file on standard input. It produces a PGM file on standard output that contains the cropped image of the original PGM file according to the command line arguments. The cropped image will start fromleft pixels from the left side of the original PGM file, and fromtop pixels from the top. Its width will be width and its height will be height. If the wrong number of command lines are given, or any of them are not integers, crop_pgm should print out a simple error message. If the correct number of command line arguments are given, and they are all numbers, but any of them is not correct (e.g., they are negative, or they are too big), then crop_pgm should print out an error message and the width and height of the original PGM file on standard error.

For example, try:

UNIX> crop_pgm -1 -1 -1 -1 < Rodney.pgm
usage: crop_pgm fromleft fromtop width height -- PGM file on standard input
PGM File has 145 rows and 123 columns
UNIX> crop_pgm 30 55 50 65 < Rodney.pgm > Rodney-Crop.pgm
Here are the files:

Rodney.pgm
Rodney-Crop.pgm


pad_pgm

This takes three command line arguments: lrpixels, tbpixels, and grey, and PGM file on standard input. It produces a PGM file on standard output the contains the original PGM file, framed with lrpixels pixels on its left and right side, and tbpixels on its top and bottom. All of these pixels will have a value of grey. Obviously, grey must have a value between 0 and 255.

For example, try:

UNIX> pad_pgm 10 25 100 < Rodney.pgm > Rodney-Pad.pgm
Here are the files:

Rodney.pgm
Rodney-Pad.pgm


panel_pgm

This takes two command line arguments: rows and columns, and a PGM file on standard input. It produces a PGM file on standard output that contains rows*columns copies of the original PGM file, laid out in a rectangular grid of rows rows and columns columns.

For example, try:

UNIX> panel_pgm 2 4 < Rodney.pgm > Rodney-Panel.pgm
Here are the files:

Rodney.pgm
Rodney-Panel.pgm


You'll note, that using standard input and standard ouptput allows us to compose these programs very nicely. For example
UNIX> crop_pgm 30 55 50 65 < Rodney.pgm | pad_pgm 6 6 0 | pad_pgm 6 6 255 | pad_pgm 3 3  127 | panel_pgm 10 10 > Too-Many-Rodneys.pgm
Here are the files:

Too-Many-Rodneys.pgm

Obviously, you may use any of the PGM files from previous lectures/labs for this lab, and they may have comments in them.