Class 18 — Tuesday, October 27

Thursday’s picture in ten minutes, then the bytes as they are: fwrite to stdout, fread from stdin, and a picture passed from one program to the next through a pipe.

Quiz 7 first, on Brightspace: one attempt now. The second comes near the end of class, when it is announced. Then the Opening Journal: its prompt is in the announcements.

Here on Thursday? Pair with someone else who was, and start at section 2.

Pairs protocol
  1. The navigator (not typing) says the examples first: each assert line. Then they say the steps the function takes.
  2. The driver types the above, and decides how each step becomes C.
  3. If either of you is stuck, drop the roles for a minute and talk it over.
  4. Swap at every program.

Every function gets the recipe’s one-line comment, and a function that hands back new memory says who frees it. A walk moves its pointer: no [ or ], and no *(p + i) with a counter.

1 Thursday, in ten minutes

Thursday’s page is Class 17. Everything today stands on four of its ideas:

  1. A picture is one array of bytes, three per pixel, red, green and blue, in rows from the top: new_image(w, h) hands back 3 * w * h of them, from malloc.
  2. The pixel in column x, row y starts at byte 3 * (y * w + x): set_pixel reaches a pixel by its column and row.
  3. A drawing that paints every pixel walks one pointer instead: put_pixel(&p, r, g, b) writes three bytes and moves p on. It takes p’s address, an unsigned char **, because it moves the caller’s pointer.
  4. write_p3 prints the picture as text, so ./draw > s.ppm makes a file and loupe s.ppm shows it.

1.1 draw.c, with stripes

Make draw.c from section 1.2 of Class 17, add put_pixel from its section 2, and write stripes as in its 2.1. Build it with the course Makefile beside it: make draw.

Done when ./draw > s.ppm opens in Loupe (loupe s.ppm) as red over white over blue.

2 The bytes as they are: P6

P3 writes each byte as up to three digits and a space. The binary form, P6, has the same three header lines with P6 in place of P3, and then the bytes themselves. The reading for today, [BGC] 9.6 — Binary File I/O, writes bytes with fwrite, and stdout, from [BGC] 9.1 — The FILE* Data Type, is where printf has been writing all along:

// writes the w by h image px to the output as a P6 file
void write_ppm(unsigned char * px, int w, int h) {
    printf("P6\n%d %d\n255\n", w, h);
    fwrite(px, 1, 3 * w * h, stdout);
}

Put it in draw.c in place of write_p3, and call it from main.

Done when ./draw > s.ppm opens in Loupe (loupe s.ppm) as the same stripes, and head -c 20 s.ppm shows the three header lines and then a few bytes that are not text.

3 A picture through a pipe

The shell’s | hands one program’s output to the next program’s input, so a picture can go through a chain of programs, each reading a picture and writing one. The first program to read a picture is pass, which changes nothing: it reads a picture from its input and writes it, as it was, to its output. If pass is right, ./draw | ./pass > c.ppm makes the same file as ./draw > s.ppm.

3.1 One library, two programs

pass will need new_image and write_ppm as much as draw does, so they move out of draw.c into a library of their own, image.c, with put_pixel. Their prototypes, with the comments, go in image.h:

// returns a new w by h image, all black; the caller frees it
unsigned char * new_image(int w, int h);

// 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);

// writes the w by h image px to the output as a P6 file
void write_ppm(unsigned char * px, int w, int h);

image.c and draw.c each start with #include "image.h", and draw.c keeps stripes and main. Two files now make one program, so add a rule for draw at the bottom of your Makefile, the course one beside draw.c, as Class 7 did for numbers:

draw: draw.c image.c image.h
	gcc $(CFLAGS) -o draw draw.c image.c

The first line names the files draw is built from, so a change to any of them rebuilds it. The second, which starts with a tab, is the command, and $(CFLAGS) is the flags from the Makefile’s CFLAGS line. Then make draw.

Done when ./draw > s.ppm opens in Loupe (loupe s.ppm) as the same stripes as before.

3.2 read_ppm, and pass.c

Add unsigned char * read_ppm(int * w, int * h) to the library, which reads a P6 picture from the input into a new image and hands it back, with its width and height through w and h, the way A5’s divide handed back two answers. Its comment ends the caller frees it. In order:

  • The header is one scanf, scanf("P6 %d %d %d", w, h, &max), which returns 3 when all three numbers were there: assert it.
  • Exactly one byte, a newline, sits between the 255 and the first pixel: getchar() takes it.
  • Then new_image(*w, *h), and one fread(px, 1, 3 * *w * *h, stdin) reads every byte at once. It returns how many it read, says [BGC] 9.6 — Binary File I/O: assert that it is all of them.

Then pass.c: its main reads a picture, writes it, and frees it. Add a rule for pass like the one for draw, and make pass.

Done when ./draw | ./pass > c.ppm opens in Loupe (loupe c.ppm) as the same stripes, and cmp s.ppm c.ppm prints nothing: the two files are the same, byte for byte.

3.3 One byte

Put // in front of the getchar() in read_ppm, and build pass again. Before you run it, predict in the Journal: will the flag report a mistake, will the picture look the same, or will it look different, and how? Then run ./draw | ./pass > n.ppm, and open it in Loupe: loupe n.ppm.

4 Keep going

For pairs who are done with section 3. Each is a program like pass: read a picture, change it, write it.

4.1 swaprb.c

Write void swap_rb(unsigned char * px, int w, int h), which swaps the red and the blue byte of every pixel. One pointer walks the pixels, p += 3.

Done when ./draw | ./swaprb > r.ppm opens in Loupe (loupe r.ppm) as blue over white over red.

4.2 rotate.c

Write void rotate180(unsigned char * px, int w, int h), which turns the picture upside down and back to front: its pixels in reverse order. Two pointers, front at the first pixel and back at the last, swap their three bytes and move toward each other until they meet, as Class 14’s flip reversed an array in place. Where is the last pixel? w * h pixels, so the last starts at byte 3 * (w * h - 1).

Done when ./draw | ./rotate > t.ppm opens in Loupe (loupe t.ppm) as blue over white over red, and ./draw | ./rotate | ./rotate > u.ppm gives back s.ppm byte for byte.

4.3 mirror.c

Write void mirror(unsigned char * px, int w, int h), which turns every row back to front. Each row is rotate180 in small: a left pointer at the row’s first pixel, a right pointer at its last, 3 * (w - 1) bytes further on. The next row starts 3 * w bytes after this one.

Stripes look the same mirrored, so check it on a picture whose rows are not symmetric: Class 17’s gradient, its 2.2, black at the left and white at the right.

Done when the gradient through ./mirror, saved as m.ppm, opens in Loupe (loupe m.ppm) white at the left.

4.4 Three programs in a row

./draw | ./mirror | ./rotate > v.ppm: before you run it, work out what the picture will be, and why. Then run it, loupe v.ppm, and run it on the gradient as well. What one program could have done the same, in one walk?

5 Before you leave