Pgm_pixels() should not return a copy of the pixels, (in other words, it should not call malloc()). Instead, it should return a pointer to the actual pixels in your Pgm struct. In this way, one can manipulate the picture in p by calling pgm_pixels() and then messing with the pixels using the pointer that is returned.
#include <stdio.h> #include "pgm.h" main() { Pgm p; int i, rows, cols; int *pixels; p = read_pgm(0); if (p == 0) exit(1); rows = pgm_rows(p); cols = pgm_cols(p); pixels = pgm_pixels(p); foi (i = 0; i < rows * cols; i++) { pixels[i] = 255 - pixels[i]; } } write_pgm(p, 0); }
Red.pgm: Red Foreman. |
Hand.pgm: Mr. Hand (the one on the right, not Sean Penn). |
Charles.pgm: Sir Charles. |
Farva.pgm: Sir Rodney. |
kai.pgm: The Amazing Kai. |
tq.pgm: The Thunder Queen. |
Dave.pgm: Big Dave. |
Peg.pgm: Big Peg. |
Terry.pgm: The original Dr. Plank. |
Reverend.pgm: The Reverend. |
Pike.pgm: "Deadlock? Deadlock? Perhaps we should move on." |
Someday, Chuck Said's picture will be here. |
UNIX> pad padding padgrayPad takes a pgm file on standard input, and creates a pgm file on standard output that adds padding extra pixels of padding around the border of the input pgm file, and puts the output on outputfile. The grayness of the padding is specified by padgray, and should be a number from 0 to 255.
If that specification seems ambiguous, try out my example executable, and make sure you match its output.
UNIX> crop rows cols left topCrop takes a pgm file on standard input, and creates a pgm file on standard output that crops the pgm file. It creates a new file that has rows rows, and cols columns, and contains the portion of the picture of those dimensions starting left pixels from the left edge of the picture, and top pixels from the top of the picture. If left+cols is greater than the number of columns of the input picture, or if top+rows is greater than the number of rows of the input picture, crop should flag an error.
Again, use the example executable as a guide if you have questions on how your program should work.
UNIX> shade 1 1 little.pgm UNIX> pad 20 0 < little.pgm | pad 20 255 | pad 20 0 | pad 20 255 | pad 20 0 | pad 20 255 | crop 121 121 0 0 | pad 5 255 | pad 5 0 > junk.pgm UNIX> convert junk.pgm junk.gifThis makes the following file: