Class 17 — Thursday, October 22
First, the presentation of A7. Then the Opening Journal: its prompt is in the announcements on Brightspace.
1 A picture is bytes
1.1 A picture by hand
A PPM file is the simplest picture there is: a header, then three numbers per pixel. Make a file tiny.ppm holding exactly this:
P3 says what follows. 3 2 is the width and the height, and 255 is the largest value a color can have. Then each pixel is three numbers, red, green and blue, in rows from the top: six pixels, eighteen numbers. Open it with loupe tiny.ppm, and zoom in with Ctrl and the scroll wheel. Then change one number, save, and look again.
1.2 draw.c, then the formula swapped
A program keeps the same picture as one array of bytes, three per pixel. unsigned char is C’s one-byte type, 0 to 255. The pixel in column x of row y starts at byte 3 * (y * w + x): y whole rows of w pixels come before it, then x pixels of its own row, and each pixel is three bytes.
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
// returns a new w by h image, all black; the caller frees it
unsigned char * new_image(int w, int h) {
unsigned char * px = malloc(3 * w * h);
assert(px != NULL);
for (unsigned char * p = px; p < px + 3 * w * h; p++) {
*p = 0;
}
return px;
}
// sets the pixel in column x, row y of the w-wide image px to the color r, g, b
void set_pixel(unsigned char * px, int w, int x, int y, int r, int g, int b) {
unsigned char * p = px + 3 * (y * w + x);
*p = r;
*(p + 1) = g;
*(p + 2) = b;
}
// prints the w by h image px as a P3 file
void write_p3(unsigned char * px, int w, int h) {
printf("P3\n%d %d\n255\n", w, h);
for (unsigned char * p = px; p < px + 3 * w * h; p += 3) {
printf("%d %d %d\n", *p, *(p + 1), *(p + 2));
}
}
int main() {
int w = 4;
int h = 2;
unsigned char * px = new_image(w, h);
set_pixel(px, w, 3, 1, 255, 255, 255);
write_p3(px, w, h);
free(px);
}Build it with the course Makefile beside it, make draw, and run ./draw > one.ppm. cat one.ppm shows the numbers; loupe one.ppm shows the picture.
Now swap y * w + x for x * w + y in set_pixel. Nobody builds until it is announced. Predict first, in the Journal: will the white pixel move somewhere else in the picture, or will the flag report a mistake? Why? Then build and run again.
2 Drawing, in pairs
- The navigator (not typing) says the examples first: each
assertline. Then they say the steps the function takes. - The driver types the above, and decides how each step becomes C.
- If either of you is stuck, drop the roles for a minute and talk it over.
- Swap at every function.
A drawing visits every pixel in order, row by row, so it needs no formula: one pointer can walk the bytes, three at a time, the way pointers walked arrays in Class 13. put_pixel writes one pixel and moves the pointer on. To move the caller’s pointer it needs that pointer’s address, the way A5’s swap needed the addresses of its numbers: a pointer to a pointer, unsigned char **.
// writes the color r, g, b at *p, and moves *p on to the next pixel
void put_pixel(unsigned char ** p, int r, int g, int b) {
**p = r;
*(*p + 1) = g;
*(*p + 2) = b;
*p += 3;
}unsigned char * p = px; // at the first pixel
put_pixel(&p, 255, 0, 0); // paints it red; p is now at the secondEach drawing is a function in draw.c, with the recipe’s one-line comment above it, called from main. Every drawing walks with put_pixel: one pointer, and its address. main keeps one assert per drawing on a pixel you can work out, beside the one given. make draw && ./draw > out.ppm, then loupe out.ppm; after a change, run both again. w and h are main’s variables, and 320 by 200 is a good size.
FILE *
The > sends everything printf prints into a file, so draw.c needs no file of its own. If you would rather the program write the file itself, C gives every open file a FILE *. stdout, where printf has been writing all along, is one of them, and fprintf is printf with the file first:
// writes the w by h image px to the file f as a P3 file
void write_p3(FILE * f, unsigned char * px, int w, int h) {
fprintf(f, "P3\n%d %d\n255\n", w, h);
for (unsigned char * p = px; p < px + 3 * w * h; p += 3) {
fprintf(f, "%d %d %d\n", *p, *(p + 1), *(p + 2));
}
}In main, fopen opens the file and fclose closes it, the way malloc and free pair up. fopen hands back NULL when it cannot open the file, so it gets an assert too:
Then ./draw alone writes out.ppm, and write_p3(stdout, px, w, h) still prints to the screen: one function for both.
Reading, for section 3.2, is the same turn: fopen("tiny.ppm", "r"), then fscanf(f, ...) wherever scanf(...) was, and fclose(f) once the picture is read. Tuesday’s reading, [BGC] 9.1 — The FILE* Data Type, is about exactly this.
2.1 stripes
Write void stripes(unsigned char * px, int w, int h), which paints the top third of the picture red, the middle third white and the bottom third blue. A row is in the top third when y < h / 3. A pointer p starts at px; two loops, over the rows y and then over the columns x, give each pixel one call to put_pixel(&p, ...), which paints it and moves p on: no 3 * and no y * w in stripes itself.
int main() {
int w = 320;
int h = 200;
unsigned char * px = new_image(w, h);
stripes(px, w, h);
assert(*(px + 3 * ((h / 2) * w)) == 255);
write_p3(px, w, h);
free(px);
}The assert reads the red byte of the first pixel of the middle row. Then one of your own: a byte in the top third that must be 0.
Done when Loupe shows three stripes, red over white over blue, and the asserts pass.
2.2 gradient
Write void gradient(unsigned char * px, int w, int h): black at the left edge, white at the right, and every column x gets 255 * x / (w - 1) in all three of its bytes. Multiply first. x / (w - 1) * 255 is 0 in every column but the last, because / on two ints throws the fraction away.
The assert reads the last pixel of the top row. Then one of your own.
Done when Loupe shows a smooth ramp from black to white.
2.3 checkerboard
From here on, for pairs who are done with 2.1 and 2.2.
Write void checkerboard(unsigned char * px, int w, int h), with #define SQUARE 25 above it: a pixel is on a white square when (x / SQUARE + y / SQUARE) % 2 == 0, and on a black one otherwise.
Done when Loupe shows 13 squares across and 8 down, white at the top left.
2.4 disc
Write void disc(unsigned char * px, int w, int h, int cx, int cy, int r), which paints white every pixel within r of column cx, row cy. No square root is needed: the pixel is inside when (x - cx) * (x - cx) + (y - cy) * (y - cy) <= r * r. Call it at the centre with r of 60.
Done when Loupe shows a white disc in the middle of a black picture.
2.5 diagonal
Write void diagonal(unsigned char * px, int w, int h): a line from the top left corner to the bottom right. Walking every pixel with put_pixel, the white ones are where y == x * h / w: one in each column.
Done when Loupe shows one thin line, corner to corner.
3 Keep going
For pairs who are done with section 2.
3.1 invert
Write void invert(unsigned char * px, int w, int h): every byte becomes 255 minus itself. Walk the bytes with one pointer; no set_pixel here. Call it after stripes: the stripes come out cyan, black and yellow.
Done when that holds after stripes and then invert, and Loupe agrees.
3.2 A picture in
Write unsigned char * read_p3(int * w, int * h), which reads a P3 file from the input into a new image and hands it back; the width and the height come out through w and h, the way A5’s divide handed back two answers. Its comment ends the caller frees it. One scanf reads the whole header, scanf("P3 %d %d %d", w, h, &max): the letters match themselves, and w is already an address. Then one scanf("%d", &v) per byte.
Change main to read a picture, invert it and write it, and run ./draw < tiny.ppm > out.ppm: the picture you made by hand, read back.
Done when Loupe shows its six colors inverted: cyan, magenta, yellow over blue, black, white.
4 Before you leave
- A8 goes out today, due Wednesday, October 28.
- The reading for Tuesday is [BGC] 9.1 — The FILE* Data Type and [BGC] 9.6 — Binary File I/O, two short sections of a second book. Quiz 7 is on it, at 12:30.
- Two dates: Practice test for Midterm Part 2, Thursday, October 29, and Midterm Part 2, Tuesday, November 3. Both on a lab machine, as Part 1 was.
- The Closing Journal: its prompt is in the announcements on Brightspace.