Assignment 8 — A picture is bytes

One image library and three programs on top of it, passing pictures to each other through a pipe: a drawing written out as a binary picture, a picture read back in and turned gray, and one cut to half its size.

Due Oct 28, 2026

In Class 17, a picture was one array of bytes, three per pixel, and set_pixel found a pixel by the formula 3 * (y * w + x). This assignment makes that code a library, image.c, and writes three programs on top of it that pass pictures to each other the way the shell has passed text all term:

./draw | ./gray | ./half > out.ppm

The reading for the week is [BGC] 9.1 — The FILE* Data Type and [BGC] 9.6 — Binary File I/O; Tuesday’s class uses them for steps 2 and 3, and Quiz 7 is on them.

You will hand in seven files: image.h, image.c, Makefile, draw.c, gray.c, half.c and notes.txt — and mandel.c, if you do the optional step 6.

Every program builds with make and no warnings, with -fsanitize=address in the Makefile, and runs with nothing reported. Every function you write gets the design recipe’s one-line comment above it, and a function that hands back new memory says so: the caller frees it. Every program frees every image it asked for. Still no [ or ] in any function but main, and no *(p + i) with a counter standing in for them: a walk moves its pointer, p++, or p += 3 a pixel at a time, or put_pixel. A pixel reached by its column and row, rather than walked to, goes through set_pixel’s formula.

  1. image.h, image.c and a Makefile — the library. Move new_image, set_pixel, put_pixel and write_p3 out of draw.c into image.c, with their prototypes and comments in image.h, so that every program here can use them. draw.c keeps its drawings and its main, and includes "image.h". Make main’s picture 321 by 201 — odd on purpose, for step 4. The Makefile, in the shape of Class 9’s, builds each program from its own .o and image.o; copy it as it is, tabs included:

    CFLAGS = -Wall -g -fsanitize=address
    PROGRAMS = draw gray half
    
    all: $(PROGRAMS)
    
    draw: draw.o image.o
    	gcc $(CFLAGS) -o draw draw.o image.o
    
    gray: gray.o image.o
    	gcc $(CFLAGS) -o gray gray.o image.o
    
    half: half.o image.o
    	gcc $(CFLAGS) -o half half.o image.o
    
    %.o: %.c image.h
    	gcc $(CFLAGS) -c $<
    
    clean:
    	rm -f $(PROGRAMS) *.o

    Until steps 3 and 4 exist, make draw builds the one program there is. Done when make draw builds it with no warnings, ./draw > s.ppm opens in Loupe as it did in class, and make draw again says there is nothing to do.

  2. write_ppm — the binary form, P6. A P3 file writes each byte as up to three digits and a space. The binary form, P6, writes the bytes as they are. Replace write_p3 with void write_ppm(unsigned char * px, int w, int h): the header is the same three lines with P6 in place of P3, printed with printf, and then one call writes every byte at once:

    fwrite(px, 1, 3 * w * h, stdout);

    [BGC] 9.6 — Binary File I/O is where fwrite comes from: 3 * w * h items of 1 byte each, from px, to the output. stdout is the FILE * that printf has been writing to all along, from 9.1. Done when ./draw > s.ppm opens in Loupe as before, and ls -l shows s.ppm about a third of the size it was.

  3. read_ppm and gray.c — a picture in, and turned gray. Write unsigned char * read_ppm(int * w, int * h) in image.c, which reads a P6 picture from the input into a new image and hands it back; the width and the height come out through w and h. Its comment ends the caller frees it. The header is one scanf:

    int n = scanf("P6 %d %d %d", w, h, &max);
    assert(n == 3);

    scanf returns how many numbers it read, and 3 means the header was there; w and h are already addresses. Exactly one whitespace byte separates the 255 from the first pixel, and getchar() takes it. Then fread(px, 1, 3 * *w * *h, stdin) reads every byte at once, from 9.6.

    Then gray.c: void gray(unsigned char * px, int w, int h) makes every pixel gray by giving its three bytes their average, and main reads a picture, grays it, writes it and frees it. Done when ./draw | ./gray > g.ppm opens in Loupe as three gray stripes — and the red stripe and the blue one come out the same gray, which step 5 asks about.

  4. half.c — a smaller copy. Write unsigned char * half(unsigned char * px, int w, int h), which hands back a new image w / 2 wide and h / 2 high, keeping every other pixel: column x, row y of the new picture is column 2 * x, row 2 * y of the old. Its comment ends the caller frees it. main reads a picture, halves it, writes the small one, and frees both: half’s result is new memory, and the picture read in is still there.

    Done when ./draw | ./gray | ./half > out.ppm opens in Loupe as a 160 by 100 gray picture, and the sanitizer reports nothing — no leak included.

  5. notes.txt — three short answers.

    1. In a picture 10 pixels wide, which byte, counting from 0, holds the green of the pixel in column 2, row 3? Show the arithmetic.
    2. In step 3, the red stripe and the blue stripe came out the same gray. Why, with the numbers?
    3. s.ppm from step 2 is about a third of the size the P3 file was, for the same picture. Where did the other two thirds go?
  6. mandel.c — optional: the Mandelbrot set. This step is optional and not needed for full marks. A point c of the plane is in the set when the sequence z = z * z + c, starting from z = 0, never gets further than 2 from the origin. The picture is one pixel per point: black for the points in the set, and a gray for the others that says how fast they escaped. The design, in order:

    • A point is two doubles, cr and ci, and so is z: zr and zi. One step of the sequence is zr' = zr * zr - zi * zi + cr and zi' = 2 * zr * zi + ci; compute the new zi with the old zr, so keep the new zr in a variable of its own first.
    • z is further than 2 from the origin when zr * zr + zi * zi > 4: no square root needed.
    • Write int steps(double cr, double ci), which counts the steps until that happens, and returns MAX, a #define of 100, if it has not happened by then. Two asserts in main, worked out below: steps(1.0, 0.0) == 3 and steps(-0.5, 0.0) == MAX.
    • Write void mandelbrot(unsigned char * px, int w, int h). Column x stands for cr = -2.0 + 3.0 * x / w and row y for ci = -1.2 + 2.4 * y / h — the 3.0 * first, so that the division is on doubles. A pixel gets 255 * n / MAX in all three bytes, where n is its steps, and 0 when n is MAX.
    • Two lines in the Makefile, in the shape of draw’s two, and mandel added to PROGRAMS. main draws 640 by 512.

    The worked example for the asserts: for c = 1 + 0i, z goes 0, then 1, then 2, then 5, and 5 * 5 is past 4, so steps is 3. For c = -0.5 + 0i, z goes 0, -0.5, -0.25, -0.4375 and settles there, never escaping: MAX.

    Done when ./mandel > m.ppm opens in Loupe as the set, black, with gray around it, and the two asserts pass. Notice, for later in the term, that the rows through the middle of the picture take longer to compute than the rows at the top and the bottom.